Revert "Add customizable to display raw bytes as hex"
[emacs.git] / src / xdisp.c
blobeaa701e9cf13ebb4917dd435292bb8cfec009576
1 /* Display generation from window structure and buffer text.
3 Copyright (C) 1985-1988, 1993-1995, 1997-2017 Free Software Foundation,
4 Inc.
6 This file is part of GNU Emacs.
8 GNU Emacs is free software: you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation, either version 3 of the License, or (at
11 your option) any later version.
13 GNU Emacs is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
21 /* New redisplay written by Gerd Moellmann <gerd@gnu.org>.
23 Redisplay.
25 Emacs separates the task of updating the display from code
26 modifying global state, e.g. buffer text. This way functions
27 operating on buffers don't also have to be concerned with updating
28 the display.
30 Updating the display is triggered by the Lisp interpreter when it
31 decides it's time to do it. This is done either automatically for
32 you as part of the interpreter's command loop or as the result of
33 calling Lisp functions like `sit-for'. The C function `redisplay'
34 in xdisp.c is the only entry into the inner redisplay code.
36 The following diagram shows how redisplay code is invoked. As you
37 can see, Lisp calls redisplay and vice versa. Under window systems
38 like X, some portions of the redisplay code are also called
39 asynchronously during mouse movement or expose events. It is very
40 important that these code parts do NOT use the C library (malloc,
41 free) because many C libraries under Unix are not reentrant. They
42 may also NOT call functions of the Lisp interpreter which could
43 change the interpreter's state. If you don't follow these rules,
44 you will encounter bugs which are very hard to explain.
46 +--------------+ redisplay +----------------+
47 | Lisp machine |---------------->| Redisplay code |<--+
48 +--------------+ (xdisp.c) +----------------+ |
49 ^ | |
50 +----------------------------------+ |
51 Don't use this path when called |
52 asynchronously! |
54 expose_window (asynchronous) |
56 X expose events -----+
58 What does redisplay do? Obviously, it has to figure out somehow what
59 has been changed since the last time the display has been updated,
60 and to make these changes visible. Preferably it would do that in
61 a moderately intelligent way, i.e. fast.
63 Changes in buffer text can be deduced from window and buffer
64 structures, and from some global variables like `beg_unchanged' and
65 `end_unchanged'. The contents of the display are additionally
66 recorded in a `glyph matrix', a two-dimensional matrix of glyph
67 structures. Each row in such a matrix corresponds to a line on the
68 display, and each glyph in a row corresponds to a column displaying
69 a character, an image, or what else. This matrix is called the
70 `current glyph matrix' or `current matrix' in redisplay
71 terminology.
73 For buffer parts that have been changed since the last update, a
74 second glyph matrix is constructed, the so called `desired glyph
75 matrix' or short `desired matrix'. Current and desired matrix are
76 then compared to find a cheap way to update the display, e.g. by
77 reusing part of the display by scrolling lines.
79 You will find a lot of redisplay optimizations when you start
80 looking at the innards of redisplay. The overall goal of all these
81 optimizations is to make redisplay fast because it is done
82 frequently. Some of these optimizations are implemented by the
83 following functions:
85 . try_cursor_movement
87 This function tries to update the display if the text in the
88 window did not change and did not scroll, only point moved, and
89 it did not move off the displayed portion of the text.
91 . try_window_reusing_current_matrix
93 This function reuses the current matrix of a window when text
94 has not changed, but the window start changed (e.g., due to
95 scrolling).
97 . try_window_id
99 This function attempts to redisplay a window by reusing parts of
100 its existing display. It finds and reuses the part that was not
101 changed, and redraws the rest. (The "id" part in the function's
102 name stands for "insert/delete", not for "identification" or
103 somesuch.)
105 . try_window
107 This function performs the full redisplay of a single window
108 assuming that its fonts were not changed and that the cursor
109 will not end up in the scroll margins. (Loading fonts requires
110 re-adjustment of dimensions of glyph matrices, which makes this
111 method impossible to use.)
113 These optimizations are tried in sequence (some can be skipped if
114 it is known that they are not applicable). If none of the
115 optimizations were successful, redisplay calls redisplay_windows,
116 which performs a full redisplay of all windows.
118 Note that there's one more important optimization up Emacs's
119 sleeve, but it is related to actually redrawing the potentially
120 changed portions of the window/frame, not to reproducing the
121 desired matrices of those potentially changed portions. Namely,
122 the function update_frame and its subroutines, which you will find
123 in dispnew.c, compare the desired matrices with the current
124 matrices, and only redraw the portions that changed. So it could
125 happen that the functions in this file for some reason decide that
126 the entire desired matrix needs to be regenerated from scratch, and
127 still only parts of the Emacs display, or even nothing at all, will
128 be actually delivered to the glass, because update_frame has found
129 that the new and the old screen contents are similar or identical.
131 Desired matrices.
133 Desired matrices are always built per Emacs window. The function
134 `display_line' is the central function to look at if you are
135 interested. It constructs one row in a desired matrix given an
136 iterator structure containing both a buffer position and a
137 description of the environment in which the text is to be
138 displayed. But this is too early, read on.
140 Characters and pixmaps displayed for a range of buffer text depend
141 on various settings of buffers and windows, on overlays and text
142 properties, on display tables, on selective display. The good news
143 is that all this hairy stuff is hidden behind a small set of
144 interface functions taking an iterator structure (struct it)
145 argument.
147 Iteration over things to be displayed is then simple. It is
148 started by initializing an iterator with a call to init_iterator,
149 passing it the buffer position where to start iteration. For
150 iteration over strings, pass -1 as the position to init_iterator,
151 and call reseat_to_string when the string is ready, to initialize
152 the iterator for that string. Thereafter, calls to
153 get_next_display_element fill the iterator structure with relevant
154 information about the next thing to display. Calls to
155 set_iterator_to_next move the iterator to the next thing.
157 Besides this, an iterator also contains information about the
158 display environment in which glyphs for display elements are to be
159 produced. It has fields for the width and height of the display,
160 the information whether long lines are truncated or continued, a
161 current X and Y position, and lots of other stuff you can better
162 see in dispextern.h.
164 Glyphs in a desired matrix are normally constructed in a loop
165 calling get_next_display_element and then PRODUCE_GLYPHS. The call
166 to PRODUCE_GLYPHS will fill the iterator structure with pixel
167 information about the element being displayed and at the same time
168 produce glyphs for it. If the display element fits on the line
169 being displayed, set_iterator_to_next is called next, otherwise the
170 glyphs produced are discarded. The function display_line is the
171 workhorse of filling glyph rows in the desired matrix with glyphs.
172 In addition to producing glyphs, it also handles line truncation
173 and continuation, word wrap, and cursor positioning (for the
174 latter, see also set_cursor_from_row).
176 Frame matrices.
178 That just couldn't be all, could it? What about terminal types not
179 supporting operations on sub-windows of the screen? To update the
180 display on such a terminal, window-based glyph matrices are not
181 well suited. To be able to reuse part of the display (scrolling
182 lines up and down), we must instead have a view of the whole
183 screen. This is what `frame matrices' are for. They are a trick.
185 Frames on terminals like above have a glyph pool. Windows on such
186 a frame sub-allocate their glyph memory from their frame's glyph
187 pool. The frame itself is given its own glyph matrices. By
188 coincidence---or maybe something else---rows in window glyph
189 matrices are slices of corresponding rows in frame matrices. Thus
190 writing to window matrices implicitly updates a frame matrix which
191 provides us with the view of the whole screen that we originally
192 wanted to have without having to move many bytes around. To be
193 honest, there is a little bit more done, but not much more. If you
194 plan to extend that code, take a look at dispnew.c. The function
195 build_frame_matrix is a good starting point.
197 Bidirectional display.
199 Bidirectional display adds quite some hair to this already complex
200 design. The good news are that a large portion of that hairy stuff
201 is hidden in bidi.c behind only 3 interfaces. bidi.c implements a
202 reordering engine which is called by set_iterator_to_next and
203 returns the next character to display in the visual order. See
204 commentary on bidi.c for more details. As far as redisplay is
205 concerned, the effect of calling bidi_move_to_visually_next, the
206 main interface of the reordering engine, is that the iterator gets
207 magically placed on the buffer or string position that is to be
208 displayed next. In other words, a linear iteration through the
209 buffer/string is replaced with a non-linear one. All the rest of
210 the redisplay is oblivious to the bidi reordering.
212 Well, almost oblivious---there are still complications, most of
213 them due to the fact that buffer and string positions no longer
214 change monotonously with glyph indices in a glyph row. Moreover,
215 for continued lines, the buffer positions may not even be
216 monotonously changing with vertical positions. Also, accounting
217 for face changes, overlays, etc. becomes more complex because
218 non-linear iteration could potentially skip many positions with
219 changes, and then cross them again on the way back...
221 One other prominent effect of bidirectional display is that some
222 paragraphs of text need to be displayed starting at the right
223 margin of the window---the so-called right-to-left, or R2L
224 paragraphs. R2L paragraphs are displayed with R2L glyph rows,
225 which have their reversed_p flag set. The bidi reordering engine
226 produces characters in such rows starting from the character which
227 should be the rightmost on display. PRODUCE_GLYPHS then reverses
228 the order, when it fills up the glyph row whose reversed_p flag is
229 set, by prepending each new glyph to what is already there, instead
230 of appending it. When the glyph row is complete, the function
231 extend_face_to_end_of_line fills the empty space to the left of the
232 leftmost character with special glyphs, which will display as,
233 well, empty. On text terminals, these special glyphs are simply
234 blank characters. On graphics terminals, there's a single stretch
235 glyph of a suitably computed width. Both the blanks and the
236 stretch glyph are given the face of the background of the line.
237 This way, the terminal-specific back-end can still draw the glyphs
238 left to right, even for R2L lines.
240 Bidirectional display and character compositions
242 Some scripts cannot be displayed by drawing each character
243 individually, because adjacent characters change each other's shape
244 on display. For example, Arabic and Indic scripts belong to this
245 category.
247 Emacs display supports this by providing "character compositions",
248 most of which is implemented in composite.c. During the buffer
249 scan that delivers characters to PRODUCE_GLYPHS, if the next
250 character to be delivered is a composed character, the iteration
251 calls composition_reseat_it and next_element_from_composition. If
252 they succeed to compose the character with one or more of the
253 following characters, the whole sequence of characters that where
254 composed is recorded in the `struct composition_it' object that is
255 part of the buffer iterator. The composed sequence could produce
256 one or more font glyphs (called "grapheme clusters") on the screen.
257 Each of these grapheme clusters is then delivered to PRODUCE_GLYPHS
258 in the direction corresponding to the current bidi scan direction
259 (recorded in the scan_dir member of the `struct bidi_it' object
260 that is part of the buffer iterator). In particular, if the bidi
261 iterator currently scans the buffer backwards, the grapheme
262 clusters are delivered back to front. This reorders the grapheme
263 clusters as appropriate for the current bidi context. Note that
264 this means that the grapheme clusters are always stored in the
265 LGSTRING object (see composite.c) in the logical order.
267 Moving an iterator in bidirectional text
268 without producing glyphs
270 Note one important detail mentioned above: that the bidi reordering
271 engine, driven by the iterator, produces characters in R2L rows
272 starting at the character that will be the rightmost on display.
273 As far as the iterator is concerned, the geometry of such rows is
274 still left to right, i.e. the iterator "thinks" the first character
275 is at the leftmost pixel position. The iterator does not know that
276 PRODUCE_GLYPHS reverses the order of the glyphs that the iterator
277 delivers. This is important when functions from the move_it_*
278 family are used to get to certain screen position or to match
279 screen coordinates with buffer coordinates: these functions use the
280 iterator geometry, which is left to right even in R2L paragraphs.
281 This works well with most callers of move_it_*, because they need
282 to get to a specific column, and columns are still numbered in the
283 reading order, i.e. the rightmost character in a R2L paragraph is
284 still column zero. But some callers do not get well with this; a
285 notable example is mouse clicks that need to find the character
286 that corresponds to certain pixel coordinates. See
287 buffer_posn_from_coords in dispnew.c for how this is handled. */
289 #include <config.h>
290 #include <stdio.h>
291 #include <stdlib.h>
292 #include <limits.h>
294 #include "lisp.h"
295 #include "atimer.h"
296 #include "composite.h"
297 #include "keyboard.h"
298 #include "systime.h"
299 #include "frame.h"
300 #include "window.h"
301 #include "termchar.h"
302 #include "dispextern.h"
303 #include "character.h"
304 #include "buffer.h"
305 #include "charset.h"
306 #include "indent.h"
307 #include "commands.h"
308 #include "keymap.h"
309 #include "disptab.h"
310 #include "termhooks.h"
311 #include "termopts.h"
312 #include "intervals.h"
313 #include "coding.h"
314 #include "region-cache.h"
315 #include "font.h"
316 #include "fontset.h"
317 #include "blockinput.h"
318 #include "xwidget.h"
319 #ifdef HAVE_WINDOW_SYSTEM
320 #include TERM_HEADER
321 #endif /* HAVE_WINDOW_SYSTEM */
323 #ifndef FRAME_X_OUTPUT
324 #define FRAME_X_OUTPUT(f) ((f)->output_data.x)
325 #endif
327 #define INFINITY 10000000
329 /* Holds the list (error). */
330 static Lisp_Object list_of_error;
332 #ifdef HAVE_WINDOW_SYSTEM
334 /* Test if overflow newline into fringe. Called with iterator IT
335 at or past right window margin, and with IT->current_x set. */
337 #define IT_OVERFLOW_NEWLINE_INTO_FRINGE(IT) \
338 (!NILP (Voverflow_newline_into_fringe) \
339 && FRAME_WINDOW_P ((IT)->f) \
340 && ((IT)->bidi_it.paragraph_dir == R2L \
341 ? (WINDOW_LEFT_FRINGE_WIDTH ((IT)->w) > 0) \
342 : (WINDOW_RIGHT_FRINGE_WIDTH ((IT)->w) > 0)) \
343 && (IT)->current_x == (IT)->last_visible_x)
345 #else /* !HAVE_WINDOW_SYSTEM */
346 #define IT_OVERFLOW_NEWLINE_INTO_FRINGE(it) false
347 #endif /* HAVE_WINDOW_SYSTEM */
349 /* Test if the display element loaded in IT, or the underlying buffer
350 or string character, is a space or a TAB character. This is used
351 to determine where word wrapping can occur. */
353 #define IT_DISPLAYING_WHITESPACE(it) \
354 ((it->what == IT_CHARACTER && (it->c == ' ' || it->c == '\t')) \
355 || ((STRINGP (it->string) \
356 && (SREF (it->string, IT_STRING_BYTEPOS (*it)) == ' ' \
357 || SREF (it->string, IT_STRING_BYTEPOS (*it)) == '\t')) \
358 || (it->s \
359 && (it->s[IT_BYTEPOS (*it)] == ' ' \
360 || it->s[IT_BYTEPOS (*it)] == '\t')) \
361 || (IT_BYTEPOS (*it) < ZV_BYTE \
362 && (*BYTE_POS_ADDR (IT_BYTEPOS (*it)) == ' ' \
363 || *BYTE_POS_ADDR (IT_BYTEPOS (*it)) == '\t')))) \
365 /* True means print newline to stdout before next mini-buffer message. */
367 bool noninteractive_need_newline;
369 /* True means print newline to message log before next message. */
371 static bool message_log_need_newline;
373 /* Three markers that message_dolog uses.
374 It could allocate them itself, but that causes trouble
375 in handling memory-full errors. */
376 static Lisp_Object message_dolog_marker1;
377 static Lisp_Object message_dolog_marker2;
378 static Lisp_Object message_dolog_marker3;
380 /* The buffer position of the first character appearing entirely or
381 partially on the line of the selected window which contains the
382 cursor; <= 0 if not known. Set by set_cursor_from_row, used for
383 redisplay optimization in redisplay_internal. */
385 static struct text_pos this_line_start_pos;
387 /* Number of characters past the end of the line above, including the
388 terminating newline. */
390 static struct text_pos this_line_end_pos;
392 /* The vertical positions and the height of this line. */
394 static int this_line_vpos;
395 static int this_line_y;
396 static int this_line_pixel_height;
398 /* X position at which this display line starts. Usually zero;
399 negative if first character is partially visible. */
401 static int this_line_start_x;
403 /* The smallest character position seen by move_it_* functions as they
404 move across display lines. Used to set MATRIX_ROW_START_CHARPOS of
405 hscrolled lines, see display_line. */
407 static struct text_pos this_line_min_pos;
409 /* Buffer that this_line_.* variables are referring to. */
411 static struct buffer *this_line_buffer;
413 /* True if an overlay arrow has been displayed in this window. */
415 static bool overlay_arrow_seen;
417 /* Vector containing glyphs for an ellipsis `...'. */
419 static Lisp_Object default_invis_vector[3];
421 /* This is the window where the echo area message was displayed. It
422 is always a mini-buffer window, but it may not be the same window
423 currently active as a mini-buffer. */
425 Lisp_Object echo_area_window;
427 /* List of pairs (MESSAGE . MULTIBYTE). The function save_message
428 pushes the current message and the value of
429 message_enable_multibyte on the stack, the function restore_message
430 pops the stack and displays MESSAGE again. */
432 static Lisp_Object Vmessage_stack;
434 /* True means multibyte characters were enabled when the echo area
435 message was specified. */
437 static bool message_enable_multibyte;
439 /* At each redisplay cycle, we should refresh everything there is to refresh.
440 To do that efficiently, we use many optimizations that try to make sure we
441 don't waste too much time updating things that haven't changed.
442 The coarsest such optimization is that, in the most common cases, we only
443 look at the selected-window.
445 To know whether other windows should be considered for redisplay, we use the
446 variable windows_or_buffers_changed: as long as it is 0, it means that we
447 have not noticed anything that should require updating anything else than
448 the selected-window. If it is set to REDISPLAY_SOME, it means that since
449 last redisplay, some changes have been made which could impact other
450 windows. To know which ones need redisplay, every buffer, window, and frame
451 has a `redisplay' bit, which (if true) means that this object needs to be
452 redisplayed. If windows_or_buffers_changed is 0, we know there's no point
453 looking for those `redisplay' bits (actually, there might be some such bits
454 set, but then only on objects which aren't displayed anyway).
456 OTOH if it's non-zero we wil have to loop through all windows and then check
457 the `redisplay' bit of the corresponding window, frame, and buffer, in order
458 to decide whether that window needs attention or not. Note that we can't
459 just look at the frame's redisplay bit to decide that the whole frame can be
460 skipped, since even if the frame's redisplay bit is unset, some of its
461 windows's redisplay bits may be set.
463 Mostly for historical reasons, windows_or_buffers_changed can also take
464 other non-zero values. In that case, the precise value doesn't matter (it
465 encodes the cause of the setting but is only used for debugging purposes),
466 and what it means is that we shouldn't pay attention to any `redisplay' bits
467 and we should simply try and redisplay every window out there. */
469 int windows_or_buffers_changed;
471 /* Nonzero if we should redraw the mode lines on the next redisplay.
472 Similarly to `windows_or_buffers_changed', If it has value REDISPLAY_SOME,
473 then only redisplay the mode lines in those buffers/windows/frames where the
474 `redisplay' bit has been set.
475 For any other value, redisplay all mode lines (the number used is then only
476 used to track down the cause for this full-redisplay).
478 Since the frame title uses the same %-constructs as the mode line
479 (except %c, %C, and %l), if this variable is non-zero, we also consider
480 redisplaying the title of each frame, see x_consider_frame_title.
482 The `redisplay' bits are the same as those used for
483 windows_or_buffers_changed, and setting windows_or_buffers_changed also
484 causes recomputation of the mode lines of all those windows. IOW this
485 variable only has an effect if windows_or_buffers_changed is zero, in which
486 case we should only need to redisplay the mode-line of those objects with
487 a `redisplay' bit set but not the window's text content (tho we may still
488 need to refresh the text content of the selected-window). */
490 int update_mode_lines;
492 /* True after display_mode_line if %l was used and it displayed a
493 line number. */
495 static bool line_number_displayed;
497 /* The name of the *Messages* buffer, a string. */
499 static Lisp_Object Vmessages_buffer_name;
501 /* Current, index 0, and last displayed echo area message. Either
502 buffers from echo_buffers, or nil to indicate no message. */
504 Lisp_Object echo_area_buffer[2];
506 /* The buffers referenced from echo_area_buffer. */
508 static Lisp_Object echo_buffer[2];
510 /* A vector saved used in with_area_buffer to reduce consing. */
512 static Lisp_Object Vwith_echo_area_save_vector;
514 /* True means display_echo_area should display the last echo area
515 message again. Set by redisplay_preserve_echo_area. */
517 static bool display_last_displayed_message_p;
519 /* True if echo area is being used by print; false if being used by
520 message. */
522 static bool message_buf_print;
524 /* Set to true in clear_message to make redisplay_internal aware
525 of an emptied echo area. */
527 static bool message_cleared_p;
529 /* A scratch glyph row with contents used for generating truncation
530 glyphs. Also used in direct_output_for_insert. */
532 #define MAX_SCRATCH_GLYPHS 100
533 static struct glyph_row scratch_glyph_row;
534 static struct glyph scratch_glyphs[MAX_SCRATCH_GLYPHS];
536 /* Ascent and height of the last line processed by move_it_to. */
538 static int last_height;
540 /* True if there's a help-echo in the echo area. */
542 bool help_echo_showing_p;
544 /* The maximum distance to look ahead for text properties. Values
545 that are too small let us call compute_char_face and similar
546 functions too often which is expensive. Values that are too large
547 let us call compute_char_face and alike too often because we
548 might not be interested in text properties that far away. */
550 #define TEXT_PROP_DISTANCE_LIMIT 100
552 /* SAVE_IT and RESTORE_IT are called when we save a snapshot of the
553 iterator state and later restore it. This is needed because the
554 bidi iterator on bidi.c keeps a stacked cache of its states, which
555 is really a singleton. When we use scratch iterator objects to
556 move around the buffer, we can cause the bidi cache to be pushed or
557 popped, and therefore we need to restore the cache state when we
558 return to the original iterator. */
559 #define SAVE_IT(ITCOPY, ITORIG, CACHE) \
560 do { \
561 if (CACHE) \
562 bidi_unshelve_cache (CACHE, true); \
563 ITCOPY = ITORIG; \
564 CACHE = bidi_shelve_cache (); \
565 } while (false)
567 #define RESTORE_IT(pITORIG, pITCOPY, CACHE) \
568 do { \
569 if (pITORIG != pITCOPY) \
570 *(pITORIG) = *(pITCOPY); \
571 bidi_unshelve_cache (CACHE, false); \
572 CACHE = NULL; \
573 } while (false)
575 /* Functions to mark elements as needing redisplay. */
576 enum { REDISPLAY_SOME = 2}; /* Arbitrary choice. */
578 void
579 redisplay_other_windows (void)
581 if (!windows_or_buffers_changed)
582 windows_or_buffers_changed = REDISPLAY_SOME;
585 void
586 wset_redisplay (struct window *w)
588 /* Beware: selected_window can be nil during early stages. */
589 if (!EQ (make_lisp_ptr (w, Lisp_Vectorlike), selected_window))
590 redisplay_other_windows ();
591 w->redisplay = true;
594 void
595 fset_redisplay (struct frame *f)
597 redisplay_other_windows ();
598 f->redisplay = true;
601 void
602 bset_redisplay (struct buffer *b)
604 int count = buffer_window_count (b);
605 if (count > 0)
607 /* ... it's visible in other window than selected, */
608 if (count > 1 || b != XBUFFER (XWINDOW (selected_window)->contents))
609 redisplay_other_windows ();
610 /* Even if we don't set windows_or_buffers_changed, do set `redisplay'
611 so that if we later set windows_or_buffers_changed, this buffer will
612 not be omitted. */
613 b->text->redisplay = true;
617 void
618 bset_update_mode_line (struct buffer *b)
620 if (!update_mode_lines)
621 update_mode_lines = REDISPLAY_SOME;
622 b->text->redisplay = true;
625 DEFUN ("set-buffer-redisplay", Fset_buffer_redisplay,
626 Sset_buffer_redisplay, 4, 4, 0,
627 doc: /* Mark the current buffer for redisplay.
628 This function may be passed to `add-variable-watcher'. */)
629 (Lisp_Object symbol, Lisp_Object newval, Lisp_Object op, Lisp_Object where)
631 bset_update_mode_line (current_buffer);
632 current_buffer->prevent_redisplay_optimizations_p = true;
633 return Qnil;
636 #ifdef GLYPH_DEBUG
638 /* True means print traces of redisplay if compiled with
639 GLYPH_DEBUG defined. */
641 bool trace_redisplay_p;
643 #endif /* GLYPH_DEBUG */
645 #ifdef DEBUG_TRACE_MOVE
646 /* True means trace with TRACE_MOVE to stderr. */
647 static bool trace_move;
649 #define TRACE_MOVE(x) if (trace_move) fprintf x; else (void) 0
650 #else
651 #define TRACE_MOVE(x) (void) 0
652 #endif
654 /* Buffer being redisplayed -- for redisplay_window_error. */
656 static struct buffer *displayed_buffer;
658 /* Value returned from text property handlers (see below). */
660 enum prop_handled
662 HANDLED_NORMALLY,
663 HANDLED_RECOMPUTE_PROPS,
664 HANDLED_OVERLAY_STRING_CONSUMED,
665 HANDLED_RETURN
668 /* A description of text properties that redisplay is interested
669 in. */
671 struct props
673 /* The symbol index of the name of the property. */
674 short name;
676 /* A unique index for the property. */
677 enum prop_idx idx;
679 /* A handler function called to set up iterator IT from the property
680 at IT's current position. Value is used to steer handle_stop. */
681 enum prop_handled (*handler) (struct it *it);
684 static enum prop_handled handle_face_prop (struct it *);
685 static enum prop_handled handle_invisible_prop (struct it *);
686 static enum prop_handled handle_display_prop (struct it *);
687 static enum prop_handled handle_composition_prop (struct it *);
688 static enum prop_handled handle_overlay_change (struct it *);
689 static enum prop_handled handle_fontified_prop (struct it *);
691 /* Properties handled by iterators. */
693 static struct props it_props[] =
695 {SYMBOL_INDEX (Qfontified), FONTIFIED_PROP_IDX, handle_fontified_prop},
696 /* Handle `face' before `display' because some sub-properties of
697 `display' need to know the face. */
698 {SYMBOL_INDEX (Qface), FACE_PROP_IDX, handle_face_prop},
699 {SYMBOL_INDEX (Qdisplay), DISPLAY_PROP_IDX, handle_display_prop},
700 {SYMBOL_INDEX (Qinvisible), INVISIBLE_PROP_IDX, handle_invisible_prop},
701 {SYMBOL_INDEX (Qcomposition), COMPOSITION_PROP_IDX, handle_composition_prop},
702 {0, 0, NULL}
705 /* Value is the position described by X. If X is a marker, value is
706 the marker_position of X. Otherwise, value is X. */
708 #define COERCE_MARKER(X) (MARKERP ((X)) ? Fmarker_position (X) : (X))
710 /* Enumeration returned by some move_it_.* functions internally. */
712 enum move_it_result
714 /* Not used. Undefined value. */
715 MOVE_UNDEFINED,
717 /* Move ended at the requested buffer position or ZV. */
718 MOVE_POS_MATCH_OR_ZV,
720 /* Move ended at the requested X pixel position. */
721 MOVE_X_REACHED,
723 /* Move within a line ended at the end of a line that must be
724 continued. */
725 MOVE_LINE_CONTINUED,
727 /* Move within a line ended at the end of a line that would
728 be displayed truncated. */
729 MOVE_LINE_TRUNCATED,
731 /* Move within a line ended at a line end. */
732 MOVE_NEWLINE_OR_CR
735 /* This counter is used to clear the face cache every once in a while
736 in redisplay_internal. It is incremented for each redisplay.
737 Every CLEAR_FACE_CACHE_COUNT full redisplays, the face cache is
738 cleared. */
740 #define CLEAR_FACE_CACHE_COUNT 500
741 static int clear_face_cache_count;
743 /* Similarly for the image cache. */
745 #ifdef HAVE_WINDOW_SYSTEM
746 #define CLEAR_IMAGE_CACHE_COUNT 101
747 static int clear_image_cache_count;
749 /* Null glyph slice */
750 static struct glyph_slice null_glyph_slice = { 0, 0, 0, 0 };
751 #endif
753 /* True while redisplay_internal is in progress. */
755 bool redisplaying_p;
757 /* If a string, XTread_socket generates an event to display that string.
758 (The display is done in read_char.) */
760 Lisp_Object help_echo_string;
761 Lisp_Object help_echo_window;
762 Lisp_Object help_echo_object;
763 ptrdiff_t help_echo_pos;
765 /* Temporary variable for XTread_socket. */
767 Lisp_Object previous_help_echo_string;
769 /* Platform-independent portion of hourglass implementation. */
771 #ifdef HAVE_WINDOW_SYSTEM
773 /* True means an hourglass cursor is currently shown. */
774 static bool hourglass_shown_p;
776 /* If non-null, an asynchronous timer that, when it expires, displays
777 an hourglass cursor on all frames. */
778 static struct atimer *hourglass_atimer;
780 #endif /* HAVE_WINDOW_SYSTEM */
782 /* Default number of seconds to wait before displaying an hourglass
783 cursor. */
784 #define DEFAULT_HOURGLASS_DELAY 1
786 #ifdef HAVE_WINDOW_SYSTEM
788 /* Default pixel width of `thin-space' display method. */
789 #define THIN_SPACE_WIDTH 1
791 #endif /* HAVE_WINDOW_SYSTEM */
793 /* Function prototypes. */
795 static void setup_for_ellipsis (struct it *, int);
796 static void set_iterator_to_next (struct it *, bool);
797 static void mark_window_display_accurate_1 (struct window *, bool);
798 static bool row_for_charpos_p (struct glyph_row *, ptrdiff_t);
799 static bool cursor_row_p (struct glyph_row *);
800 static int redisplay_mode_lines (Lisp_Object, bool);
802 static void handle_line_prefix (struct it *);
804 static void handle_stop_backwards (struct it *, ptrdiff_t);
805 static void unwind_with_echo_area_buffer (Lisp_Object);
806 static Lisp_Object with_echo_area_buffer_unwind_data (struct window *);
807 static bool current_message_1 (ptrdiff_t, Lisp_Object);
808 static bool truncate_message_1 (ptrdiff_t, Lisp_Object);
809 static void set_message (Lisp_Object);
810 static bool set_message_1 (ptrdiff_t, Lisp_Object);
811 static bool display_echo_area_1 (ptrdiff_t, Lisp_Object);
812 static bool resize_mini_window_1 (ptrdiff_t, Lisp_Object);
813 static void unwind_redisplay (void);
814 static void extend_face_to_end_of_line (struct it *);
815 static intmax_t message_log_check_duplicate (ptrdiff_t, ptrdiff_t);
816 static void push_it (struct it *, struct text_pos *);
817 static void iterate_out_of_display_property (struct it *);
818 static void pop_it (struct it *);
819 static void redisplay_internal (void);
820 static void echo_area_display (bool);
821 static void block_buffer_flips (void);
822 static void unblock_buffer_flips (void);
823 static void redisplay_windows (Lisp_Object);
824 static void redisplay_window (Lisp_Object, bool);
825 static Lisp_Object redisplay_window_error (Lisp_Object);
826 static Lisp_Object redisplay_window_0 (Lisp_Object);
827 static Lisp_Object redisplay_window_1 (Lisp_Object);
828 static bool set_cursor_from_row (struct window *, struct glyph_row *,
829 struct glyph_matrix *, ptrdiff_t, ptrdiff_t,
830 int, int);
831 static bool cursor_row_fully_visible_p (struct window *, bool, bool);
832 static bool update_menu_bar (struct frame *, bool, bool);
833 static bool try_window_reusing_current_matrix (struct window *);
834 static int try_window_id (struct window *);
835 static bool display_line (struct it *, int);
836 static int display_mode_lines (struct window *);
837 static int display_mode_line (struct window *, enum face_id, Lisp_Object);
838 static int display_mode_element (struct it *, int, int, int, Lisp_Object,
839 Lisp_Object, bool);
840 static int store_mode_line_string (const char *, Lisp_Object, bool, int, int,
841 Lisp_Object);
842 static const char *decode_mode_spec (struct window *, int, int, Lisp_Object *);
843 static void display_menu_bar (struct window *);
844 static ptrdiff_t display_count_lines (ptrdiff_t, ptrdiff_t, ptrdiff_t,
845 ptrdiff_t *);
846 static int display_string (const char *, Lisp_Object, Lisp_Object,
847 ptrdiff_t, ptrdiff_t, struct it *, int, int, int, int);
848 static void compute_line_metrics (struct it *);
849 static void run_redisplay_end_trigger_hook (struct it *);
850 static bool get_overlay_strings (struct it *, ptrdiff_t);
851 static bool get_overlay_strings_1 (struct it *, ptrdiff_t, bool);
852 static void next_overlay_string (struct it *);
853 static void reseat (struct it *, struct text_pos, bool);
854 static void reseat_1 (struct it *, struct text_pos, bool);
855 static bool next_element_from_display_vector (struct it *);
856 static bool next_element_from_string (struct it *);
857 static bool next_element_from_c_string (struct it *);
858 static bool next_element_from_buffer (struct it *);
859 static bool next_element_from_composition (struct it *);
860 static bool next_element_from_image (struct it *);
861 static bool next_element_from_stretch (struct it *);
862 static bool next_element_from_xwidget (struct it *);
863 static void load_overlay_strings (struct it *, ptrdiff_t);
864 static bool get_next_display_element (struct it *);
865 static enum move_it_result
866 move_it_in_display_line_to (struct it *, ptrdiff_t, int,
867 enum move_operation_enum);
868 static void get_visually_first_element (struct it *);
869 static void compute_stop_pos (struct it *);
870 static int face_before_or_after_it_pos (struct it *, bool);
871 static ptrdiff_t next_overlay_change (ptrdiff_t);
872 static int handle_display_spec (struct it *, Lisp_Object, Lisp_Object,
873 Lisp_Object, struct text_pos *, ptrdiff_t, bool);
874 static int handle_single_display_spec (struct it *, Lisp_Object,
875 Lisp_Object, Lisp_Object,
876 struct text_pos *, ptrdiff_t, int, bool);
877 static int underlying_face_id (struct it *);
879 #define face_before_it_pos(IT) face_before_or_after_it_pos (IT, true)
880 #define face_after_it_pos(IT) face_before_or_after_it_pos (IT, false)
882 #ifdef HAVE_WINDOW_SYSTEM
884 static void update_tool_bar (struct frame *, bool);
885 static void x_draw_bottom_divider (struct window *w);
886 static void notice_overwritten_cursor (struct window *,
887 enum glyph_row_area,
888 int, int, int, int);
889 static int normal_char_height (struct font *, int);
890 static void normal_char_ascent_descent (struct font *, int, int *, int *);
892 static void append_stretch_glyph (struct it *, Lisp_Object,
893 int, int, int);
895 static Lisp_Object get_it_property (struct it *, Lisp_Object);
896 static Lisp_Object calc_line_height_property (struct it *, Lisp_Object,
897 struct font *, int, bool);
899 #endif /* HAVE_WINDOW_SYSTEM */
901 static void produce_special_glyphs (struct it *, enum display_element_type);
902 static void show_mouse_face (Mouse_HLInfo *, enum draw_glyphs_face);
903 static bool coords_in_mouse_face_p (struct window *, int, int);
907 /***********************************************************************
908 Window display dimensions
909 ***********************************************************************/
911 /* Return the bottom boundary y-position for text lines in window W.
912 This is the first y position at which a line cannot start.
913 It is relative to the top of the window.
915 This is the height of W minus the height of a mode line, if any. */
918 window_text_bottom_y (struct window *w)
920 int height = WINDOW_PIXEL_HEIGHT (w);
922 height -= WINDOW_BOTTOM_DIVIDER_WIDTH (w);
924 if (WINDOW_WANTS_MODELINE_P (w))
925 height -= CURRENT_MODE_LINE_HEIGHT (w);
927 height -= WINDOW_SCROLL_BAR_AREA_HEIGHT (w);
929 return height;
932 /* Return the pixel width of display area AREA of window W.
933 ANY_AREA means return the total width of W, not including
934 fringes to the left and right of the window. */
937 window_box_width (struct window *w, enum glyph_row_area area)
939 int width = w->pixel_width;
941 if (!w->pseudo_window_p)
943 width -= WINDOW_SCROLL_BAR_AREA_WIDTH (w);
944 width -= WINDOW_RIGHT_DIVIDER_WIDTH (w);
946 if (area == TEXT_AREA)
947 width -= (WINDOW_MARGINS_WIDTH (w)
948 + WINDOW_FRINGES_WIDTH (w));
949 else if (area == LEFT_MARGIN_AREA)
950 width = WINDOW_LEFT_MARGIN_WIDTH (w);
951 else if (area == RIGHT_MARGIN_AREA)
952 width = WINDOW_RIGHT_MARGIN_WIDTH (w);
955 /* With wide margins, fringes, etc. we might end up with a negative
956 width, correct that here. */
957 return max (0, width);
961 /* Return the pixel height of the display area of window W, not
962 including mode lines of W, if any. */
965 window_box_height (struct window *w)
967 struct frame *f = XFRAME (w->frame);
968 int height = WINDOW_PIXEL_HEIGHT (w);
970 eassert (height >= 0);
972 height -= WINDOW_BOTTOM_DIVIDER_WIDTH (w);
973 height -= WINDOW_SCROLL_BAR_AREA_HEIGHT (w);
975 /* Note: the code below that determines the mode-line/header-line
976 height is essentially the same as that contained in the macro
977 CURRENT_{MODE,HEADER}_LINE_HEIGHT, except that it checks whether
978 the appropriate glyph row has its `mode_line_p' flag set,
979 and if it doesn't, uses estimate_mode_line_height instead. */
981 if (WINDOW_WANTS_MODELINE_P (w))
983 struct glyph_row *ml_row
984 = (w->current_matrix && w->current_matrix->rows
985 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
986 : 0);
987 if (ml_row && ml_row->mode_line_p)
988 height -= ml_row->height;
989 else
990 height -= estimate_mode_line_height (f, CURRENT_MODE_LINE_FACE_ID (w));
993 if (WINDOW_WANTS_HEADER_LINE_P (w))
995 struct glyph_row *hl_row
996 = (w->current_matrix && w->current_matrix->rows
997 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
998 : 0);
999 if (hl_row && hl_row->mode_line_p)
1000 height -= hl_row->height;
1001 else
1002 height -= estimate_mode_line_height (f, HEADER_LINE_FACE_ID);
1005 /* With a very small font and a mode-line that's taller than
1006 default, we might end up with a negative height. */
1007 return max (0, height);
1010 /* Return the window-relative coordinate of the left edge of display
1011 area AREA of window W. ANY_AREA means return the left edge of the
1012 whole window, to the right of the left fringe of W. */
1015 window_box_left_offset (struct window *w, enum glyph_row_area area)
1017 int x;
1019 if (w->pseudo_window_p)
1020 return 0;
1022 x = WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
1024 if (area == TEXT_AREA)
1025 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1026 + window_box_width (w, LEFT_MARGIN_AREA));
1027 else if (area == RIGHT_MARGIN_AREA)
1028 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1029 + window_box_width (w, LEFT_MARGIN_AREA)
1030 + window_box_width (w, TEXT_AREA)
1031 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
1033 : WINDOW_RIGHT_FRINGE_WIDTH (w)));
1034 else if (area == LEFT_MARGIN_AREA
1035 && WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w))
1036 x += WINDOW_LEFT_FRINGE_WIDTH (w);
1038 /* Don't return more than the window's pixel width. */
1039 return min (x, w->pixel_width);
1043 /* Return the window-relative coordinate of the right edge of display
1044 area AREA of window W. ANY_AREA means return the right edge of the
1045 whole window, to the left of the right fringe of W. */
1047 static int
1048 window_box_right_offset (struct window *w, enum glyph_row_area area)
1050 /* Don't return more than the window's pixel width. */
1051 return min (window_box_left_offset (w, area) + window_box_width (w, area),
1052 w->pixel_width);
1055 /* Return the frame-relative coordinate of the left edge of display
1056 area AREA of window W. ANY_AREA means return the left edge of the
1057 whole window, to the right of the left fringe of W. */
1060 window_box_left (struct window *w, enum glyph_row_area area)
1062 struct frame *f = XFRAME (w->frame);
1063 int x;
1065 if (w->pseudo_window_p)
1066 return FRAME_INTERNAL_BORDER_WIDTH (f);
1068 x = (WINDOW_LEFT_EDGE_X (w)
1069 + window_box_left_offset (w, area));
1071 return x;
1075 /* Return the frame-relative coordinate of the right edge of display
1076 area AREA of window W. ANY_AREA means return the right edge of the
1077 whole window, to the left of the right fringe of W. */
1080 window_box_right (struct window *w, enum glyph_row_area area)
1082 return window_box_left (w, area) + window_box_width (w, area);
1085 /* Get the bounding box of the display area AREA of window W, without
1086 mode lines, in frame-relative coordinates. ANY_AREA means the
1087 whole window, not including the left and right fringes of
1088 the window. Return in *BOX_X and *BOX_Y the frame-relative pixel
1089 coordinates of the upper-left corner of the box. Return in
1090 *BOX_WIDTH, and *BOX_HEIGHT the pixel width and height of the box. */
1092 void
1093 window_box (struct window *w, enum glyph_row_area area, int *box_x,
1094 int *box_y, int *box_width, int *box_height)
1096 if (box_width)
1097 *box_width = window_box_width (w, area);
1098 if (box_height)
1099 *box_height = window_box_height (w);
1100 if (box_x)
1101 *box_x = window_box_left (w, area);
1102 if (box_y)
1104 *box_y = WINDOW_TOP_EDGE_Y (w);
1105 if (WINDOW_WANTS_HEADER_LINE_P (w))
1106 *box_y += CURRENT_HEADER_LINE_HEIGHT (w);
1110 #ifdef HAVE_WINDOW_SYSTEM
1112 /* Get the bounding box of the display area AREA of window W, without
1113 mode lines and both fringes of the window. Return in *TOP_LEFT_X
1114 and TOP_LEFT_Y the frame-relative pixel coordinates of the
1115 upper-left corner of the box. Return in *BOTTOM_RIGHT_X, and
1116 *BOTTOM_RIGHT_Y the coordinates of the bottom-right corner of the
1117 box. */
1119 static void
1120 window_box_edges (struct window *w, int *top_left_x, int *top_left_y,
1121 int *bottom_right_x, int *bottom_right_y)
1123 window_box (w, ANY_AREA, top_left_x, top_left_y,
1124 bottom_right_x, bottom_right_y);
1125 *bottom_right_x += *top_left_x;
1126 *bottom_right_y += *top_left_y;
1129 #endif /* HAVE_WINDOW_SYSTEM */
1131 /***********************************************************************
1132 Utilities
1133 ***********************************************************************/
1135 /* Return the bottom y-position of the line the iterator IT is in.
1136 This can modify IT's settings. */
1139 line_bottom_y (struct it *it)
1141 int line_height = it->max_ascent + it->max_descent;
1142 int line_top_y = it->current_y;
1144 if (line_height == 0)
1146 if (last_height)
1147 line_height = last_height;
1148 else if (IT_CHARPOS (*it) < ZV)
1150 move_it_by_lines (it, 1);
1151 line_height = (it->max_ascent || it->max_descent
1152 ? it->max_ascent + it->max_descent
1153 : last_height);
1155 else
1157 struct glyph_row *row = it->glyph_row;
1159 /* Use the default character height. */
1160 it->glyph_row = NULL;
1161 it->what = IT_CHARACTER;
1162 it->c = ' ';
1163 it->len = 1;
1164 PRODUCE_GLYPHS (it);
1165 line_height = it->ascent + it->descent;
1166 it->glyph_row = row;
1170 return line_top_y + line_height;
1173 DEFUN ("line-pixel-height", Fline_pixel_height,
1174 Sline_pixel_height, 0, 0, 0,
1175 doc: /* Return height in pixels of text line in the selected window.
1177 Value is the height in pixels of the line at point. */)
1178 (void)
1180 struct it it;
1181 struct text_pos pt;
1182 struct window *w = XWINDOW (selected_window);
1183 struct buffer *old_buffer = NULL;
1184 Lisp_Object result;
1186 if (XBUFFER (w->contents) != current_buffer)
1188 old_buffer = current_buffer;
1189 set_buffer_internal_1 (XBUFFER (w->contents));
1191 SET_TEXT_POS (pt, PT, PT_BYTE);
1192 start_display (&it, w, pt);
1193 it.vpos = it.current_y = 0;
1194 last_height = 0;
1195 result = make_number (line_bottom_y (&it));
1196 if (old_buffer)
1197 set_buffer_internal_1 (old_buffer);
1199 return result;
1202 /* Return the default pixel height of text lines in window W. The
1203 value is the canonical height of the W frame's default font, plus
1204 any extra space required by the line-spacing variable or frame
1205 parameter.
1207 Implementation note: this ignores any line-spacing text properties
1208 put on the newline characters. This is because those properties
1209 only affect the _screen_ line ending in the newline (i.e., in a
1210 continued line, only the last screen line will be affected), which
1211 means only a small number of lines in a buffer can ever use this
1212 feature. Since this function is used to compute the default pixel
1213 equivalent of text lines in a window, we can safely ignore those
1214 few lines. For the same reasons, we ignore the line-height
1215 properties. */
1217 default_line_pixel_height (struct window *w)
1219 struct frame *f = WINDOW_XFRAME (w);
1220 int height = FRAME_LINE_HEIGHT (f);
1222 if (!FRAME_INITIAL_P (f) && BUFFERP (w->contents))
1224 struct buffer *b = XBUFFER (w->contents);
1225 Lisp_Object val = BVAR (b, extra_line_spacing);
1227 if (NILP (val))
1228 val = BVAR (&buffer_defaults, extra_line_spacing);
1229 if (!NILP (val))
1231 if (RANGED_INTEGERP (0, val, INT_MAX))
1232 height += XFASTINT (val);
1233 else if (FLOATP (val))
1235 int addon = XFLOAT_DATA (val) * height + 0.5;
1237 if (addon >= 0)
1238 height += addon;
1241 else
1242 height += f->extra_line_spacing;
1245 return height;
1248 /* Subroutine of pos_visible_p below. Extracts a display string, if
1249 any, from the display spec given as its argument. */
1250 static Lisp_Object
1251 string_from_display_spec (Lisp_Object spec)
1253 if (VECTORP (spec))
1255 for (ptrdiff_t i = 0; i < ASIZE (spec); i++)
1256 if (STRINGP (AREF (spec, i)))
1257 return AREF (spec, i);
1259 else
1261 for (; CONSP (spec); spec = XCDR (spec))
1262 if (STRINGP (XCAR (spec)))
1263 return XCAR (spec);
1265 return spec;
1269 /* Limit insanely large values of W->hscroll on frame F to the largest
1270 value that will still prevent first_visible_x and last_visible_x of
1271 'struct it' from overflowing an int. */
1272 static int
1273 window_hscroll_limited (struct window *w, struct frame *f)
1275 ptrdiff_t window_hscroll = w->hscroll;
1276 int window_text_width = window_box_width (w, TEXT_AREA);
1277 int colwidth = FRAME_COLUMN_WIDTH (f);
1279 if (window_hscroll > (INT_MAX - window_text_width) / colwidth - 1)
1280 window_hscroll = (INT_MAX - window_text_width) / colwidth - 1;
1282 return window_hscroll;
1285 /* Return true if position CHARPOS is visible in window W.
1286 CHARPOS < 0 means return info about WINDOW_END position.
1287 If visible, set *X and *Y to pixel coordinates of top left corner.
1288 Set *RTOP and *RBOT to pixel height of an invisible area of glyph at POS.
1289 Set *ROWH and *VPOS to row's visible height and VPOS (row number). */
1291 bool
1292 pos_visible_p (struct window *w, ptrdiff_t charpos, int *x, int *y,
1293 int *rtop, int *rbot, int *rowh, int *vpos)
1295 struct it it;
1296 void *itdata = bidi_shelve_cache ();
1297 struct text_pos top;
1298 bool visible_p = false;
1299 struct buffer *old_buffer = NULL;
1300 bool r2l = false;
1302 if (FRAME_INITIAL_P (XFRAME (WINDOW_FRAME (w))))
1303 return visible_p;
1305 if (XBUFFER (w->contents) != current_buffer)
1307 old_buffer = current_buffer;
1308 set_buffer_internal_1 (XBUFFER (w->contents));
1311 SET_TEXT_POS_FROM_MARKER (top, w->start);
1312 /* Scrolling a minibuffer window via scroll bar when the echo area
1313 shows long text sometimes resets the minibuffer contents behind
1314 our backs. Also, someone might narrow-to-region and immediately
1315 call a scroll function. */
1316 if (CHARPOS (top) > ZV || CHARPOS (top) < BEGV)
1317 SET_TEXT_POS (top, BEGV, BEGV_BYTE);
1319 /* If the top of the window is after CHARPOS, the latter is surely
1320 not visible. */
1321 if (charpos >= 0 && CHARPOS (top) > charpos)
1322 return visible_p;
1324 /* Compute exact mode line heights. */
1325 if (WINDOW_WANTS_MODELINE_P (w))
1326 w->mode_line_height
1327 = display_mode_line (w, CURRENT_MODE_LINE_FACE_ID (w),
1328 BVAR (current_buffer, mode_line_format));
1330 if (WINDOW_WANTS_HEADER_LINE_P (w))
1331 w->header_line_height
1332 = display_mode_line (w, HEADER_LINE_FACE_ID,
1333 BVAR (current_buffer, header_line_format));
1335 start_display (&it, w, top);
1336 move_it_to (&it, charpos, -1, it.last_visible_y - 1, -1,
1337 (charpos >= 0 ? MOVE_TO_POS : 0) | MOVE_TO_Y);
1339 if (charpos >= 0
1340 && (((!it.bidi_p || it.bidi_it.scan_dir != -1)
1341 && IT_CHARPOS (it) >= charpos)
1342 /* When scanning backwards under bidi iteration, move_it_to
1343 stops at or _before_ CHARPOS, because it stops at or to
1344 the _right_ of the character at CHARPOS. */
1345 || (it.bidi_p && it.bidi_it.scan_dir == -1
1346 && IT_CHARPOS (it) <= charpos)))
1348 /* We have reached CHARPOS, or passed it. How the call to
1349 move_it_to can overshoot: (i) If CHARPOS is on invisible text
1350 or covered by a display property, move_it_to stops at the end
1351 of the invisible text, to the right of CHARPOS. (ii) If
1352 CHARPOS is in a display vector, move_it_to stops on its last
1353 glyph. */
1354 int top_x = it.current_x;
1355 int top_y = it.current_y;
1356 int window_top_y = WINDOW_HEADER_LINE_HEIGHT (w);
1357 int bottom_y;
1358 struct it save_it;
1359 void *save_it_data = NULL;
1361 /* Calling line_bottom_y may change it.method, it.position, etc. */
1362 SAVE_IT (save_it, it, save_it_data);
1363 last_height = 0;
1364 bottom_y = line_bottom_y (&it);
1365 if (top_y < window_top_y)
1366 visible_p = bottom_y > window_top_y;
1367 else if (top_y < it.last_visible_y)
1368 visible_p = true;
1369 if (bottom_y >= it.last_visible_y
1370 && it.bidi_p && it.bidi_it.scan_dir == -1
1371 && IT_CHARPOS (it) < charpos)
1373 /* When the last line of the window is scanned backwards
1374 under bidi iteration, we could be duped into thinking
1375 that we have passed CHARPOS, when in fact move_it_to
1376 simply stopped short of CHARPOS because it reached
1377 last_visible_y. To see if that's what happened, we call
1378 move_it_to again with a slightly larger vertical limit,
1379 and see if it actually moved vertically; if it did, we
1380 didn't really reach CHARPOS, which is beyond window end. */
1381 /* Why 10? because we don't know how many canonical lines
1382 will the height of the next line(s) be. So we guess. */
1383 int ten_more_lines = 10 * default_line_pixel_height (w);
1385 move_it_to (&it, charpos, -1, bottom_y + ten_more_lines, -1,
1386 MOVE_TO_POS | MOVE_TO_Y);
1387 if (it.current_y > top_y)
1388 visible_p = false;
1391 RESTORE_IT (&it, &save_it, save_it_data);
1392 if (visible_p)
1394 if (it.method == GET_FROM_DISPLAY_VECTOR)
1396 /* We stopped on the last glyph of a display vector.
1397 Try and recompute. Hack alert! */
1398 if (charpos < 2 || top.charpos >= charpos)
1399 top_x = it.glyph_row->x;
1400 else
1402 struct it it2, it2_prev;
1403 /* The idea is to get to the previous buffer
1404 position, consume the character there, and use
1405 the pixel coordinates we get after that. But if
1406 the previous buffer position is also displayed
1407 from a display vector, we need to consume all of
1408 the glyphs from that display vector. */
1409 start_display (&it2, w, top);
1410 move_it_to (&it2, charpos - 1, -1, -1, -1, MOVE_TO_POS);
1411 /* If we didn't get to CHARPOS - 1, there's some
1412 replacing display property at that position, and
1413 we stopped after it. That is exactly the place
1414 whose coordinates we want. */
1415 if (IT_CHARPOS (it2) != charpos - 1)
1416 it2_prev = it2;
1417 else
1419 /* Iterate until we get out of the display
1420 vector that displays the character at
1421 CHARPOS - 1. */
1422 do {
1423 get_next_display_element (&it2);
1424 PRODUCE_GLYPHS (&it2);
1425 it2_prev = it2;
1426 set_iterator_to_next (&it2, true);
1427 } while (it2.method == GET_FROM_DISPLAY_VECTOR
1428 && IT_CHARPOS (it2) < charpos);
1430 if (ITERATOR_AT_END_OF_LINE_P (&it2_prev)
1431 || it2_prev.current_x > it2_prev.last_visible_x)
1432 top_x = it.glyph_row->x;
1433 else
1435 top_x = it2_prev.current_x;
1436 top_y = it2_prev.current_y;
1440 else if (IT_CHARPOS (it) != charpos)
1442 Lisp_Object cpos = make_number (charpos);
1443 Lisp_Object spec = Fget_char_property (cpos, Qdisplay, Qnil);
1444 Lisp_Object string = string_from_display_spec (spec);
1445 struct text_pos tpos;
1446 bool newline_in_string
1447 = (STRINGP (string)
1448 && memchr (SDATA (string), '\n', SBYTES (string)));
1450 SET_TEXT_POS (tpos, charpos, CHAR_TO_BYTE (charpos));
1451 bool replacing_spec_p
1452 = (!NILP (spec)
1453 && handle_display_spec (NULL, spec, Qnil, Qnil, &tpos,
1454 charpos, FRAME_WINDOW_P (it.f)));
1455 /* The tricky code below is needed because there's a
1456 discrepancy between move_it_to and how we set cursor
1457 when PT is at the beginning of a portion of text
1458 covered by a display property or an overlay with a
1459 display property, or the display line ends in a
1460 newline from a display string. move_it_to will stop
1461 _after_ such display strings, whereas
1462 set_cursor_from_row conspires with cursor_row_p to
1463 place the cursor on the first glyph produced from the
1464 display string. */
1466 /* We have overshoot PT because it is covered by a
1467 display property that replaces the text it covers.
1468 If the string includes embedded newlines, we are also
1469 in the wrong display line. Backtrack to the correct
1470 line, where the display property begins. */
1471 if (replacing_spec_p)
1473 Lisp_Object startpos, endpos;
1474 EMACS_INT start, end;
1475 struct it it3;
1477 /* Find the first and the last buffer positions
1478 covered by the display string. */
1479 endpos =
1480 Fnext_single_char_property_change (cpos, Qdisplay,
1481 Qnil, Qnil);
1482 startpos =
1483 Fprevious_single_char_property_change (endpos, Qdisplay,
1484 Qnil, Qnil);
1485 start = XFASTINT (startpos);
1486 end = XFASTINT (endpos);
1487 /* Move to the last buffer position before the
1488 display property. */
1489 start_display (&it3, w, top);
1490 if (start > CHARPOS (top))
1491 move_it_to (&it3, start - 1, -1, -1, -1, MOVE_TO_POS);
1492 /* Move forward one more line if the position before
1493 the display string is a newline or if it is the
1494 rightmost character on a line that is
1495 continued or word-wrapped. */
1496 if (it3.method == GET_FROM_BUFFER
1497 && (it3.c == '\n'
1498 || FETCH_BYTE (IT_BYTEPOS (it3)) == '\n'))
1499 move_it_by_lines (&it3, 1);
1500 else if (move_it_in_display_line_to (&it3, -1,
1501 it3.current_x
1502 + it3.pixel_width,
1503 MOVE_TO_X)
1504 == MOVE_LINE_CONTINUED)
1506 move_it_by_lines (&it3, 1);
1507 /* When we are under word-wrap, the #$@%!
1508 move_it_by_lines moves 2 lines, so we need to
1509 fix that up. */
1510 if (it3.line_wrap == WORD_WRAP)
1511 move_it_by_lines (&it3, -1);
1514 /* Record the vertical coordinate of the display
1515 line where we wound up. */
1516 top_y = it3.current_y;
1517 if (it3.bidi_p)
1519 /* When characters are reordered for display,
1520 the character displayed to the left of the
1521 display string could be _after_ the display
1522 property in the logical order. Use the
1523 smallest vertical position of these two. */
1524 start_display (&it3, w, top);
1525 move_it_to (&it3, end + 1, -1, -1, -1, MOVE_TO_POS);
1526 if (it3.current_y < top_y)
1527 top_y = it3.current_y;
1529 /* Move from the top of the window to the beginning
1530 of the display line where the display string
1531 begins. */
1532 start_display (&it3, w, top);
1533 move_it_to (&it3, -1, 0, top_y, -1, MOVE_TO_X | MOVE_TO_Y);
1534 /* If it3_moved stays false after the 'while' loop
1535 below, that means we already were at a newline
1536 before the loop (e.g., the display string begins
1537 with a newline), so we don't need to (and cannot)
1538 inspect the glyphs of it3.glyph_row, because
1539 PRODUCE_GLYPHS will not produce anything for a
1540 newline, and thus it3.glyph_row stays at its
1541 stale content it got at top of the window. */
1542 bool it3_moved = false;
1543 /* Finally, advance the iterator until we hit the
1544 first display element whose character position is
1545 CHARPOS, or until the first newline from the
1546 display string, which signals the end of the
1547 display line. */
1548 while (get_next_display_element (&it3))
1550 PRODUCE_GLYPHS (&it3);
1551 if (IT_CHARPOS (it3) == charpos
1552 || ITERATOR_AT_END_OF_LINE_P (&it3))
1553 break;
1554 it3_moved = true;
1555 set_iterator_to_next (&it3, false);
1557 top_x = it3.current_x - it3.pixel_width;
1558 /* Normally, we would exit the above loop because we
1559 found the display element whose character
1560 position is CHARPOS. For the contingency that we
1561 didn't, and stopped at the first newline from the
1562 display string, move back over the glyphs
1563 produced from the string, until we find the
1564 rightmost glyph not from the string. */
1565 if (it3_moved
1566 && newline_in_string
1567 && IT_CHARPOS (it3) != charpos && EQ (it3.object, string))
1569 struct glyph *g = it3.glyph_row->glyphs[TEXT_AREA]
1570 + it3.glyph_row->used[TEXT_AREA];
1572 while (EQ ((g - 1)->object, string))
1574 --g;
1575 top_x -= g->pixel_width;
1577 eassert (g < it3.glyph_row->glyphs[TEXT_AREA]
1578 + it3.glyph_row->used[TEXT_AREA]);
1583 *x = top_x;
1584 *y = max (top_y + max (0, it.max_ascent - it.ascent), window_top_y);
1585 *rtop = max (0, window_top_y - top_y);
1586 *rbot = max (0, bottom_y - it.last_visible_y);
1587 *rowh = max (0, (min (bottom_y, it.last_visible_y)
1588 - max (top_y, window_top_y)));
1589 *vpos = it.vpos;
1590 if (it.bidi_it.paragraph_dir == R2L)
1591 r2l = true;
1594 else
1596 /* Either we were asked to provide info about WINDOW_END, or
1597 CHARPOS is in the partially visible glyph row at end of
1598 window. */
1599 struct it it2;
1600 void *it2data = NULL;
1602 SAVE_IT (it2, it, it2data);
1603 if (IT_CHARPOS (it) < ZV && FETCH_BYTE (IT_BYTEPOS (it)) != '\n')
1604 move_it_by_lines (&it, 1);
1605 if (charpos < IT_CHARPOS (it)
1606 || (it.what == IT_EOB && charpos == IT_CHARPOS (it)))
1608 visible_p = true;
1609 RESTORE_IT (&it2, &it2, it2data);
1610 move_it_to (&it2, charpos, -1, -1, -1, MOVE_TO_POS);
1611 *x = it2.current_x;
1612 *y = it2.current_y + it2.max_ascent - it2.ascent;
1613 *rtop = max (0, -it2.current_y);
1614 *rbot = max (0, ((it2.current_y + it2.max_ascent + it2.max_descent)
1615 - it.last_visible_y));
1616 *rowh = max (0, (min (it2.current_y + it2.max_ascent + it2.max_descent,
1617 it.last_visible_y)
1618 - max (it2.current_y,
1619 WINDOW_HEADER_LINE_HEIGHT (w))));
1620 *vpos = it2.vpos;
1621 if (it2.bidi_it.paragraph_dir == R2L)
1622 r2l = true;
1624 else
1625 bidi_unshelve_cache (it2data, true);
1627 bidi_unshelve_cache (itdata, false);
1629 if (old_buffer)
1630 set_buffer_internal_1 (old_buffer);
1632 if (visible_p)
1634 if (w->hscroll > 0)
1635 *x -=
1636 window_hscroll_limited (w, WINDOW_XFRAME (w))
1637 * WINDOW_FRAME_COLUMN_WIDTH (w);
1638 /* For lines in an R2L paragraph, we need to mirror the X pixel
1639 coordinate wrt the text area. For the reasons, see the
1640 commentary in buffer_posn_from_coords and the explanation of
1641 the geometry used by the move_it_* functions at the end of
1642 the large commentary near the beginning of this file. */
1643 if (r2l)
1644 *x = window_box_width (w, TEXT_AREA) - *x - 1;
1647 #if false
1648 /* Debugging code. */
1649 if (visible_p)
1650 fprintf (stderr, "+pv pt=%d vs=%d --> x=%d y=%d rt=%d rb=%d rh=%d vp=%d\n",
1651 charpos, w->vscroll, *x, *y, *rtop, *rbot, *rowh, *vpos);
1652 else
1653 fprintf (stderr, "-pv pt=%d vs=%d\n", charpos, w->vscroll);
1654 #endif
1656 return visible_p;
1660 /* Return the next character from STR. Return in *LEN the length of
1661 the character. This is like STRING_CHAR_AND_LENGTH but never
1662 returns an invalid character. If we find one, we return a `?', but
1663 with the length of the invalid character. */
1665 static int
1666 string_char_and_length (const unsigned char *str, int *len)
1668 int c;
1670 c = STRING_CHAR_AND_LENGTH (str, *len);
1671 if (!CHAR_VALID_P (c))
1672 /* We may not change the length here because other places in Emacs
1673 don't use this function, i.e. they silently accept invalid
1674 characters. */
1675 c = '?';
1677 return c;
1682 /* Given a position POS containing a valid character and byte position
1683 in STRING, return the position NCHARS ahead (NCHARS >= 0). */
1685 static struct text_pos
1686 string_pos_nchars_ahead (struct text_pos pos, Lisp_Object string, ptrdiff_t nchars)
1688 eassert (STRINGP (string) && nchars >= 0);
1690 if (STRING_MULTIBYTE (string))
1692 const unsigned char *p = SDATA (string) + BYTEPOS (pos);
1693 int len;
1695 while (nchars--)
1697 string_char_and_length (p, &len);
1698 p += len;
1699 CHARPOS (pos) += 1;
1700 BYTEPOS (pos) += len;
1703 else
1704 SET_TEXT_POS (pos, CHARPOS (pos) + nchars, BYTEPOS (pos) + nchars);
1706 return pos;
1710 /* Value is the text position, i.e. character and byte position,
1711 for character position CHARPOS in STRING. */
1713 static struct text_pos
1714 string_pos (ptrdiff_t charpos, Lisp_Object string)
1716 struct text_pos pos;
1717 eassert (STRINGP (string));
1718 eassert (charpos >= 0);
1719 SET_TEXT_POS (pos, charpos, string_char_to_byte (string, charpos));
1720 return pos;
1724 /* Value is a text position, i.e. character and byte position, for
1725 character position CHARPOS in C string S. MULTIBYTE_P
1726 means recognize multibyte characters. */
1728 static struct text_pos
1729 c_string_pos (ptrdiff_t charpos, const char *s, bool multibyte_p)
1731 struct text_pos pos;
1733 eassert (s != NULL);
1734 eassert (charpos >= 0);
1736 if (multibyte_p)
1738 int len;
1740 SET_TEXT_POS (pos, 0, 0);
1741 while (charpos--)
1743 string_char_and_length ((const unsigned char *) s, &len);
1744 s += len;
1745 CHARPOS (pos) += 1;
1746 BYTEPOS (pos) += len;
1749 else
1750 SET_TEXT_POS (pos, charpos, charpos);
1752 return pos;
1756 /* Value is the number of characters in C string S. MULTIBYTE_P
1757 means recognize multibyte characters. */
1759 static ptrdiff_t
1760 number_of_chars (const char *s, bool multibyte_p)
1762 ptrdiff_t nchars;
1764 if (multibyte_p)
1766 ptrdiff_t rest = strlen (s);
1767 int len;
1768 const unsigned char *p = (const unsigned char *) s;
1770 for (nchars = 0; rest > 0; ++nchars)
1772 string_char_and_length (p, &len);
1773 rest -= len, p += len;
1776 else
1777 nchars = strlen (s);
1779 return nchars;
1783 /* Compute byte position NEWPOS->bytepos corresponding to
1784 NEWPOS->charpos. POS is a known position in string STRING.
1785 NEWPOS->charpos must be >= POS.charpos. */
1787 static void
1788 compute_string_pos (struct text_pos *newpos, struct text_pos pos, Lisp_Object string)
1790 eassert (STRINGP (string));
1791 eassert (CHARPOS (*newpos) >= CHARPOS (pos));
1793 if (STRING_MULTIBYTE (string))
1794 *newpos = string_pos_nchars_ahead (pos, string,
1795 CHARPOS (*newpos) - CHARPOS (pos));
1796 else
1797 BYTEPOS (*newpos) = CHARPOS (*newpos);
1800 /* EXPORT:
1801 Return an estimation of the pixel height of mode or header lines on
1802 frame F. FACE_ID specifies what line's height to estimate. */
1805 estimate_mode_line_height (struct frame *f, enum face_id face_id)
1807 #ifdef HAVE_WINDOW_SYSTEM
1808 if (FRAME_WINDOW_P (f))
1810 int height = FONT_HEIGHT (FRAME_FONT (f));
1812 /* This function is called so early when Emacs starts that the face
1813 cache and mode line face are not yet initialized. */
1814 if (FRAME_FACE_CACHE (f))
1816 struct face *face = FACE_FROM_ID_OR_NULL (f, face_id);
1817 if (face)
1819 if (face->font)
1820 height = normal_char_height (face->font, -1);
1821 if (face->box_line_width > 0)
1822 height += 2 * face->box_line_width;
1826 return height;
1828 #endif
1830 return 1;
1833 /* Given a pixel position (PIX_X, PIX_Y) on frame F, return glyph
1834 co-ordinates in (*X, *Y). Set *BOUNDS to the rectangle that the
1835 glyph at X, Y occupies, if BOUNDS != 0. If NOCLIP, do
1836 not force the value into range. */
1838 void
1839 pixel_to_glyph_coords (struct frame *f, int pix_x, int pix_y, int *x, int *y,
1840 NativeRectangle *bounds, bool noclip)
1843 #ifdef HAVE_WINDOW_SYSTEM
1844 if (FRAME_WINDOW_P (f))
1846 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to round down
1847 even for negative values. */
1848 if (pix_x < 0)
1849 pix_x -= FRAME_COLUMN_WIDTH (f) - 1;
1850 if (pix_y < 0)
1851 pix_y -= FRAME_LINE_HEIGHT (f) - 1;
1853 pix_x = FRAME_PIXEL_X_TO_COL (f, pix_x);
1854 pix_y = FRAME_PIXEL_Y_TO_LINE (f, pix_y);
1856 if (bounds)
1857 STORE_NATIVE_RECT (*bounds,
1858 FRAME_COL_TO_PIXEL_X (f, pix_x),
1859 FRAME_LINE_TO_PIXEL_Y (f, pix_y),
1860 FRAME_COLUMN_WIDTH (f) - 1,
1861 FRAME_LINE_HEIGHT (f) - 1);
1863 /* PXW: Should we clip pixels before converting to columns/lines? */
1864 if (!noclip)
1866 if (pix_x < 0)
1867 pix_x = 0;
1868 else if (pix_x > FRAME_TOTAL_COLS (f))
1869 pix_x = FRAME_TOTAL_COLS (f);
1871 if (pix_y < 0)
1872 pix_y = 0;
1873 else if (pix_y > FRAME_TOTAL_LINES (f))
1874 pix_y = FRAME_TOTAL_LINES (f);
1877 #endif
1879 *x = pix_x;
1880 *y = pix_y;
1884 /* Find the glyph under window-relative coordinates X/Y in window W.
1885 Consider only glyphs from buffer text, i.e. no glyphs from overlay
1886 strings. Return in *HPOS and *VPOS the row and column number of
1887 the glyph found. Return in *AREA the glyph area containing X.
1888 Value is a pointer to the glyph found or null if X/Y is not on
1889 text, or we can't tell because W's current matrix is not up to
1890 date. */
1892 static struct glyph *
1893 x_y_to_hpos_vpos (struct window *w, int x, int y, int *hpos, int *vpos,
1894 int *dx, int *dy, int *area)
1896 struct glyph *glyph, *end;
1897 struct glyph_row *row = NULL;
1898 int x0, i;
1900 /* Find row containing Y. Give up if some row is not enabled. */
1901 for (i = 0; i < w->current_matrix->nrows; ++i)
1903 row = MATRIX_ROW (w->current_matrix, i);
1904 if (!row->enabled_p)
1905 return NULL;
1906 if (y >= row->y && y < MATRIX_ROW_BOTTOM_Y (row))
1907 break;
1910 *vpos = i;
1911 *hpos = 0;
1913 /* Give up if Y is not in the window. */
1914 if (i == w->current_matrix->nrows)
1915 return NULL;
1917 /* Get the glyph area containing X. */
1918 if (w->pseudo_window_p)
1920 *area = TEXT_AREA;
1921 x0 = 0;
1923 else
1925 if (x < window_box_left_offset (w, TEXT_AREA))
1927 *area = LEFT_MARGIN_AREA;
1928 x0 = window_box_left_offset (w, LEFT_MARGIN_AREA);
1930 else if (x < window_box_right_offset (w, TEXT_AREA))
1932 *area = TEXT_AREA;
1933 x0 = window_box_left_offset (w, TEXT_AREA) + min (row->x, 0);
1935 else
1937 *area = RIGHT_MARGIN_AREA;
1938 x0 = window_box_left_offset (w, RIGHT_MARGIN_AREA);
1942 /* Find glyph containing X. */
1943 glyph = row->glyphs[*area];
1944 end = glyph + row->used[*area];
1945 x -= x0;
1946 while (glyph < end && x >= glyph->pixel_width)
1948 x -= glyph->pixel_width;
1949 ++glyph;
1952 if (glyph == end)
1953 return NULL;
1955 if (dx)
1957 *dx = x;
1958 *dy = y - (row->y + row->ascent - glyph->ascent);
1961 *hpos = glyph - row->glyphs[*area];
1962 return glyph;
1965 /* Convert frame-relative x/y to coordinates relative to window W.
1966 Takes pseudo-windows into account. */
1968 static void
1969 frame_to_window_pixel_xy (struct window *w, int *x, int *y)
1971 if (w->pseudo_window_p)
1973 /* A pseudo-window is always full-width, and starts at the
1974 left edge of the frame, plus a frame border. */
1975 struct frame *f = XFRAME (w->frame);
1976 *x -= FRAME_INTERNAL_BORDER_WIDTH (f);
1977 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1979 else
1981 *x -= WINDOW_LEFT_EDGE_X (w);
1982 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1986 #ifdef HAVE_WINDOW_SYSTEM
1988 /* EXPORT:
1989 Return in RECTS[] at most N clipping rectangles for glyph string S.
1990 Return the number of stored rectangles. */
1993 get_glyph_string_clip_rects (struct glyph_string *s, NativeRectangle *rects, int n)
1995 XRectangle r;
1997 if (n <= 0)
1998 return 0;
2000 if (s->row->full_width_p)
2002 /* Draw full-width. X coordinates are relative to S->w->left_col. */
2003 r.x = WINDOW_LEFT_EDGE_X (s->w);
2004 if (s->row->mode_line_p)
2005 r.width = WINDOW_PIXEL_WIDTH (s->w) - WINDOW_RIGHT_DIVIDER_WIDTH (s->w);
2006 else
2007 r.width = WINDOW_PIXEL_WIDTH (s->w);
2009 /* Unless displaying a mode or menu bar line, which are always
2010 fully visible, clip to the visible part of the row. */
2011 if (s->w->pseudo_window_p)
2012 r.height = s->row->visible_height;
2013 else
2014 r.height = s->height;
2016 else
2018 /* This is a text line that may be partially visible. */
2019 r.x = window_box_left (s->w, s->area);
2020 r.width = window_box_width (s->w, s->area);
2021 r.height = s->row->visible_height;
2024 if (s->clip_head)
2025 if (r.x < s->clip_head->x)
2027 if (r.width >= s->clip_head->x - r.x)
2028 r.width -= s->clip_head->x - r.x;
2029 else
2030 r.width = 0;
2031 r.x = s->clip_head->x;
2033 if (s->clip_tail)
2034 if (r.x + r.width > s->clip_tail->x + s->clip_tail->background_width)
2036 if (s->clip_tail->x + s->clip_tail->background_width >= r.x)
2037 r.width = s->clip_tail->x + s->clip_tail->background_width - r.x;
2038 else
2039 r.width = 0;
2042 /* If S draws overlapping rows, it's sufficient to use the top and
2043 bottom of the window for clipping because this glyph string
2044 intentionally draws over other lines. */
2045 if (s->for_overlaps)
2047 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
2048 r.height = window_text_bottom_y (s->w) - r.y;
2050 /* Alas, the above simple strategy does not work for the
2051 environments with anti-aliased text: if the same text is
2052 drawn onto the same place multiple times, it gets thicker.
2053 If the overlap we are processing is for the erased cursor, we
2054 take the intersection with the rectangle of the cursor. */
2055 if (s->for_overlaps & OVERLAPS_ERASED_CURSOR)
2057 XRectangle rc, r_save = r;
2059 rc.x = WINDOW_TEXT_TO_FRAME_PIXEL_X (s->w, s->w->phys_cursor.x);
2060 rc.y = s->w->phys_cursor.y;
2061 rc.width = s->w->phys_cursor_width;
2062 rc.height = s->w->phys_cursor_height;
2064 x_intersect_rectangles (&r_save, &rc, &r);
2067 else
2069 /* Don't use S->y for clipping because it doesn't take partially
2070 visible lines into account. For example, it can be negative for
2071 partially visible lines at the top of a window. */
2072 if (!s->row->full_width_p
2073 && MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (s->w, s->row))
2074 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
2075 else
2076 r.y = max (0, s->row->y);
2079 r.y = WINDOW_TO_FRAME_PIXEL_Y (s->w, r.y);
2081 /* If drawing the cursor, don't let glyph draw outside its
2082 advertised boundaries. Cleartype does this under some circumstances. */
2083 if (s->hl == DRAW_CURSOR)
2085 struct glyph *glyph = s->first_glyph;
2086 int height, max_y;
2088 if (s->x > r.x)
2090 if (r.width >= s->x - r.x)
2091 r.width -= s->x - r.x;
2092 else /* R2L hscrolled row with cursor outside text area */
2093 r.width = 0;
2094 r.x = s->x;
2096 r.width = min (r.width, glyph->pixel_width);
2098 /* If r.y is below window bottom, ensure that we still see a cursor. */
2099 height = min (glyph->ascent + glyph->descent,
2100 min (FRAME_LINE_HEIGHT (s->f), s->row->visible_height));
2101 max_y = window_text_bottom_y (s->w) - height;
2102 max_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, max_y);
2103 if (s->ybase - glyph->ascent > max_y)
2105 r.y = max_y;
2106 r.height = height;
2108 else
2110 /* Don't draw cursor glyph taller than our actual glyph. */
2111 height = max (FRAME_LINE_HEIGHT (s->f), glyph->ascent + glyph->descent);
2112 if (height < r.height)
2114 max_y = r.y + r.height;
2115 r.y = min (max_y, max (r.y, s->ybase + glyph->descent - height));
2116 r.height = min (max_y - r.y, height);
2121 if (s->row->clip)
2123 XRectangle r_save = r;
2125 if (! x_intersect_rectangles (&r_save, s->row->clip, &r))
2126 r.width = 0;
2129 if ((s->for_overlaps & OVERLAPS_BOTH) == 0
2130 || ((s->for_overlaps & OVERLAPS_BOTH) == OVERLAPS_BOTH && n == 1))
2132 #ifdef CONVERT_FROM_XRECT
2133 CONVERT_FROM_XRECT (r, *rects);
2134 #else
2135 *rects = r;
2136 #endif
2137 return 1;
2139 else
2141 /* If we are processing overlapping and allowed to return
2142 multiple clipping rectangles, we exclude the row of the glyph
2143 string from the clipping rectangle. This is to avoid drawing
2144 the same text on the environment with anti-aliasing. */
2145 #ifdef CONVERT_FROM_XRECT
2146 XRectangle rs[2];
2147 #else
2148 XRectangle *rs = rects;
2149 #endif
2150 int i = 0, row_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, s->row->y);
2152 if (s->for_overlaps & OVERLAPS_PRED)
2154 rs[i] = r;
2155 if (r.y + r.height > row_y)
2157 if (r.y < row_y)
2158 rs[i].height = row_y - r.y;
2159 else
2160 rs[i].height = 0;
2162 i++;
2164 if (s->for_overlaps & OVERLAPS_SUCC)
2166 rs[i] = r;
2167 if (r.y < row_y + s->row->visible_height)
2169 if (r.y + r.height > row_y + s->row->visible_height)
2171 rs[i].y = row_y + s->row->visible_height;
2172 rs[i].height = r.y + r.height - rs[i].y;
2174 else
2175 rs[i].height = 0;
2177 i++;
2180 n = i;
2181 #ifdef CONVERT_FROM_XRECT
2182 for (i = 0; i < n; i++)
2183 CONVERT_FROM_XRECT (rs[i], rects[i]);
2184 #endif
2185 return n;
2189 /* EXPORT:
2190 Return in *NR the clipping rectangle for glyph string S. */
2192 void
2193 get_glyph_string_clip_rect (struct glyph_string *s, NativeRectangle *nr)
2195 get_glyph_string_clip_rects (s, nr, 1);
2199 /* EXPORT:
2200 Return the position and height of the phys cursor in window W.
2201 Set w->phys_cursor_width to width of phys cursor.
2204 void
2205 get_phys_cursor_geometry (struct window *w, struct glyph_row *row,
2206 struct glyph *glyph, int *xp, int *yp, int *heightp)
2208 struct frame *f = XFRAME (WINDOW_FRAME (w));
2209 int x, y, wd, h, h0, y0, ascent;
2211 /* Compute the width of the rectangle to draw. If on a stretch
2212 glyph, and `x-stretch-block-cursor' is nil, don't draw a
2213 rectangle as wide as the glyph, but use a canonical character
2214 width instead. */
2215 wd = glyph->pixel_width;
2217 x = w->phys_cursor.x;
2218 if (x < 0)
2220 wd += x;
2221 x = 0;
2224 if (glyph->type == STRETCH_GLYPH
2225 && !x_stretch_cursor_p)
2226 wd = min (FRAME_COLUMN_WIDTH (f), wd);
2227 w->phys_cursor_width = wd;
2229 /* Don't let the hollow cursor glyph descend below the glyph row's
2230 ascent value, lest the hollow cursor looks funny. */
2231 y = w->phys_cursor.y;
2232 ascent = row->ascent;
2233 if (row->ascent < glyph->ascent)
2235 y -= glyph->ascent - row->ascent;
2236 ascent = glyph->ascent;
2239 /* If y is below window bottom, ensure that we still see a cursor. */
2240 h0 = min (FRAME_LINE_HEIGHT (f), row->visible_height);
2242 h = max (h0, ascent + glyph->descent);
2243 h0 = min (h0, ascent + glyph->descent);
2245 y0 = WINDOW_HEADER_LINE_HEIGHT (w);
2246 if (y < y0)
2248 h = max (h - (y0 - y) + 1, h0);
2249 y = y0 - 1;
2251 else
2253 y0 = window_text_bottom_y (w) - h0;
2254 if (y > y0)
2256 h += y - y0;
2257 y = y0;
2261 *xp = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, x);
2262 *yp = WINDOW_TO_FRAME_PIXEL_Y (w, y);
2263 *heightp = h;
2267 * Remember which glyph the mouse is over.
2270 void
2271 remember_mouse_glyph (struct frame *f, int gx, int gy, NativeRectangle *rect)
2273 Lisp_Object window;
2274 struct window *w;
2275 struct glyph_row *r, *gr, *end_row;
2276 enum window_part part;
2277 enum glyph_row_area area;
2278 int x, y, width, height;
2280 /* Try to determine frame pixel position and size of the glyph under
2281 frame pixel coordinates X/Y on frame F. */
2283 if (window_resize_pixelwise)
2285 width = height = 1;
2286 goto virtual_glyph;
2288 else if (!f->glyphs_initialized_p
2289 || (window = window_from_coordinates (f, gx, gy, &part, false),
2290 NILP (window)))
2292 width = FRAME_SMALLEST_CHAR_WIDTH (f);
2293 height = FRAME_SMALLEST_FONT_HEIGHT (f);
2294 goto virtual_glyph;
2297 w = XWINDOW (window);
2298 width = WINDOW_FRAME_COLUMN_WIDTH (w);
2299 height = WINDOW_FRAME_LINE_HEIGHT (w);
2301 x = window_relative_x_coord (w, part, gx);
2302 y = gy - WINDOW_TOP_EDGE_Y (w);
2304 r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
2305 end_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
2307 if (w->pseudo_window_p)
2309 area = TEXT_AREA;
2310 part = ON_MODE_LINE; /* Don't adjust margin. */
2311 goto text_glyph;
2314 switch (part)
2316 case ON_LEFT_MARGIN:
2317 area = LEFT_MARGIN_AREA;
2318 goto text_glyph;
2320 case ON_RIGHT_MARGIN:
2321 area = RIGHT_MARGIN_AREA;
2322 goto text_glyph;
2324 case ON_HEADER_LINE:
2325 case ON_MODE_LINE:
2326 gr = (part == ON_HEADER_LINE
2327 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
2328 : MATRIX_MODE_LINE_ROW (w->current_matrix));
2329 gy = gr->y;
2330 area = TEXT_AREA;
2331 goto text_glyph_row_found;
2333 case ON_TEXT:
2334 area = TEXT_AREA;
2336 text_glyph:
2337 gr = 0; gy = 0;
2338 for (; r <= end_row && r->enabled_p; ++r)
2339 if (r->y + r->height > y)
2341 gr = r; gy = r->y;
2342 break;
2345 text_glyph_row_found:
2346 if (gr && gy <= y)
2348 struct glyph *g = gr->glyphs[area];
2349 struct glyph *end = g + gr->used[area];
2351 height = gr->height;
2352 for (gx = gr->x; g < end; gx += g->pixel_width, ++g)
2353 if (gx + g->pixel_width > x)
2354 break;
2356 if (g < end)
2358 if (g->type == IMAGE_GLYPH)
2360 /* Don't remember when mouse is over image, as
2361 image may have hot-spots. */
2362 STORE_NATIVE_RECT (*rect, 0, 0, 0, 0);
2363 return;
2365 width = g->pixel_width;
2367 else
2369 /* Use nominal char spacing at end of line. */
2370 x -= gx;
2371 gx += (x / width) * width;
2374 if (part != ON_MODE_LINE && part != ON_HEADER_LINE)
2376 gx += window_box_left_offset (w, area);
2377 /* Don't expand over the modeline to make sure the vertical
2378 drag cursor is shown early enough. */
2379 height = min (height,
2380 max (0, WINDOW_BOX_HEIGHT_NO_MODE_LINE (w) - gy));
2383 else
2385 /* Use nominal line height at end of window. */
2386 gx = (x / width) * width;
2387 y -= gy;
2388 gy += (y / height) * height;
2389 if (part != ON_MODE_LINE && part != ON_HEADER_LINE)
2390 /* See comment above. */
2391 height = min (height,
2392 max (0, WINDOW_BOX_HEIGHT_NO_MODE_LINE (w) - gy));
2394 break;
2396 case ON_LEFT_FRINGE:
2397 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2398 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w)
2399 : window_box_right_offset (w, LEFT_MARGIN_AREA));
2400 width = WINDOW_LEFT_FRINGE_WIDTH (w);
2401 goto row_glyph;
2403 case ON_RIGHT_FRINGE:
2404 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2405 ? window_box_right_offset (w, RIGHT_MARGIN_AREA)
2406 : window_box_right_offset (w, TEXT_AREA));
2407 if (WINDOW_RIGHT_DIVIDER_WIDTH (w) == 0
2408 && !WINDOW_HAS_VERTICAL_SCROLL_BAR (w)
2409 && !WINDOW_RIGHTMOST_P (w))
2410 if (gx < WINDOW_PIXEL_WIDTH (w) - width)
2411 /* Make sure the vertical border can get her own glyph to the
2412 right of the one we build here. */
2413 width = WINDOW_RIGHT_FRINGE_WIDTH (w) - width;
2414 else
2415 width = WINDOW_PIXEL_WIDTH (w) - gx;
2416 else
2417 width = WINDOW_RIGHT_FRINGE_WIDTH (w);
2419 goto row_glyph;
2421 case ON_VERTICAL_BORDER:
2422 gx = WINDOW_PIXEL_WIDTH (w) - width;
2423 goto row_glyph;
2425 case ON_VERTICAL_SCROLL_BAR:
2426 gx = (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w)
2428 : (window_box_right_offset (w, RIGHT_MARGIN_AREA)
2429 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2430 ? WINDOW_RIGHT_FRINGE_WIDTH (w)
2431 : 0)));
2432 width = WINDOW_SCROLL_BAR_AREA_WIDTH (w);
2434 row_glyph:
2435 gr = 0, gy = 0;
2436 for (; r <= end_row && r->enabled_p; ++r)
2437 if (r->y + r->height > y)
2439 gr = r; gy = r->y;
2440 break;
2443 if (gr && gy <= y)
2444 height = gr->height;
2445 else
2447 /* Use nominal line height at end of window. */
2448 y -= gy;
2449 gy += (y / height) * height;
2451 break;
2453 case ON_RIGHT_DIVIDER:
2454 gx = WINDOW_PIXEL_WIDTH (w) - WINDOW_RIGHT_DIVIDER_WIDTH (w);
2455 width = WINDOW_RIGHT_DIVIDER_WIDTH (w);
2456 gy = 0;
2457 /* The bottom divider prevails. */
2458 height = WINDOW_PIXEL_HEIGHT (w) - WINDOW_BOTTOM_DIVIDER_WIDTH (w);
2459 goto add_edge;
2461 case ON_BOTTOM_DIVIDER:
2462 gx = 0;
2463 width = WINDOW_PIXEL_WIDTH (w);
2464 gy = WINDOW_PIXEL_HEIGHT (w) - WINDOW_BOTTOM_DIVIDER_WIDTH (w);
2465 height = WINDOW_BOTTOM_DIVIDER_WIDTH (w);
2466 goto add_edge;
2468 default:
2470 virtual_glyph:
2471 /* If there is no glyph under the mouse, then we divide the screen
2472 into a grid of the smallest glyph in the frame, and use that
2473 as our "glyph". */
2475 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to
2476 round down even for negative values. */
2477 if (gx < 0)
2478 gx -= width - 1;
2479 if (gy < 0)
2480 gy -= height - 1;
2482 gx = (gx / width) * width;
2483 gy = (gy / height) * height;
2485 goto store_rect;
2488 add_edge:
2489 gx += WINDOW_LEFT_EDGE_X (w);
2490 gy += WINDOW_TOP_EDGE_Y (w);
2492 store_rect:
2493 STORE_NATIVE_RECT (*rect, gx, gy, width, height);
2495 /* Visible feedback for debugging. */
2496 #if false && defined HAVE_X_WINDOWS
2497 XDrawRectangle (FRAME_X_DISPLAY (f), FRAME_X_DRAWABLE (f),
2498 f->output_data.x->normal_gc,
2499 gx, gy, width, height);
2500 #endif
2504 #endif /* HAVE_WINDOW_SYSTEM */
2506 static void
2507 adjust_window_ends (struct window *w, struct glyph_row *row, bool current)
2509 eassert (w);
2510 w->window_end_pos = Z - MATRIX_ROW_END_CHARPOS (row);
2511 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
2512 w->window_end_vpos
2513 = MATRIX_ROW_VPOS (row, current ? w->current_matrix : w->desired_matrix);
2516 static bool
2517 hscrolling_current_line_p (struct window *w)
2519 return (!w->suspend_auto_hscroll
2520 && EQ (Fbuffer_local_value (Qauto_hscroll_mode, w->contents),
2521 Qcurrent_line));
2524 /***********************************************************************
2525 Lisp form evaluation
2526 ***********************************************************************/
2528 /* Error handler for safe_eval and safe_call. */
2530 static Lisp_Object
2531 safe_eval_handler (Lisp_Object arg, ptrdiff_t nargs, Lisp_Object *args)
2533 add_to_log ("Error during redisplay: %S signaled %S",
2534 Flist (nargs, args), arg);
2535 return Qnil;
2538 /* Call function FUNC with the rest of NARGS - 1 arguments
2539 following. Return the result, or nil if something went
2540 wrong. Prevent redisplay during the evaluation. */
2542 static Lisp_Object
2543 safe__call (bool inhibit_quit, ptrdiff_t nargs, Lisp_Object func, va_list ap)
2545 Lisp_Object val;
2547 if (inhibit_eval_during_redisplay)
2548 val = Qnil;
2549 else
2551 ptrdiff_t i;
2552 ptrdiff_t count = SPECPDL_INDEX ();
2553 Lisp_Object *args;
2554 USE_SAFE_ALLOCA;
2555 SAFE_ALLOCA_LISP (args, nargs);
2557 args[0] = func;
2558 for (i = 1; i < nargs; i++)
2559 args[i] = va_arg (ap, Lisp_Object);
2561 specbind (Qinhibit_redisplay, Qt);
2562 if (inhibit_quit)
2563 specbind (Qinhibit_quit, Qt);
2564 /* Use Qt to ensure debugger does not run,
2565 so there is no possibility of wanting to redisplay. */
2566 val = internal_condition_case_n (Ffuncall, nargs, args, Qt,
2567 safe_eval_handler);
2568 SAFE_FREE ();
2569 val = unbind_to (count, val);
2572 return val;
2575 Lisp_Object
2576 safe_call (ptrdiff_t nargs, Lisp_Object func, ...)
2578 Lisp_Object retval;
2579 va_list ap;
2581 va_start (ap, func);
2582 retval = safe__call (false, nargs, func, ap);
2583 va_end (ap);
2584 return retval;
2587 /* Call function FN with one argument ARG.
2588 Return the result, or nil if something went wrong. */
2590 Lisp_Object
2591 safe_call1 (Lisp_Object fn, Lisp_Object arg)
2593 return safe_call (2, fn, arg);
2596 static Lisp_Object
2597 safe__call1 (bool inhibit_quit, Lisp_Object fn, ...)
2599 Lisp_Object retval;
2600 va_list ap;
2602 va_start (ap, fn);
2603 retval = safe__call (inhibit_quit, 2, fn, ap);
2604 va_end (ap);
2605 return retval;
2608 Lisp_Object
2609 safe_eval (Lisp_Object sexpr)
2611 return safe__call1 (false, Qeval, sexpr);
2614 static Lisp_Object
2615 safe__eval (bool inhibit_quit, Lisp_Object sexpr)
2617 return safe__call1 (inhibit_quit, Qeval, sexpr);
2620 /* Call function FN with two arguments ARG1 and ARG2.
2621 Return the result, or nil if something went wrong. */
2623 Lisp_Object
2624 safe_call2 (Lisp_Object fn, Lisp_Object arg1, Lisp_Object arg2)
2626 return safe_call (3, fn, arg1, arg2);
2631 /***********************************************************************
2632 Debugging
2633 ***********************************************************************/
2635 /* Define CHECK_IT to perform sanity checks on iterators.
2636 This is for debugging. It is too slow to do unconditionally. */
2638 static void
2639 CHECK_IT (struct it *it)
2641 #if false
2642 if (it->method == GET_FROM_STRING)
2644 eassert (STRINGP (it->string));
2645 eassert (IT_STRING_CHARPOS (*it) >= 0);
2647 else
2649 eassert (IT_STRING_CHARPOS (*it) < 0);
2650 if (it->method == GET_FROM_BUFFER)
2652 /* Check that character and byte positions agree. */
2653 eassert (IT_CHARPOS (*it) == BYTE_TO_CHAR (IT_BYTEPOS (*it)));
2657 if (it->dpvec)
2658 eassert (it->current.dpvec_index >= 0);
2659 else
2660 eassert (it->current.dpvec_index < 0);
2661 #endif
2665 /* Check that the window end of window W is what we expect it
2666 to be---the last row in the current matrix displaying text. */
2668 static void
2669 CHECK_WINDOW_END (struct window *w)
2671 #if defined GLYPH_DEBUG && defined ENABLE_CHECKING
2672 if (!MINI_WINDOW_P (w) && w->window_end_valid)
2674 struct glyph_row *row;
2675 eassert ((row = MATRIX_ROW (w->current_matrix, w->window_end_vpos),
2676 !row->enabled_p
2677 || MATRIX_ROW_DISPLAYS_TEXT_P (row)
2678 || MATRIX_ROW_VPOS (row, w->current_matrix) == 0));
2680 #endif
2683 /***********************************************************************
2684 Iterator initialization
2685 ***********************************************************************/
2687 /* Initialize IT for displaying current_buffer in window W, starting
2688 at character position CHARPOS. CHARPOS < 0 means that no buffer
2689 position is specified which is useful when the iterator is assigned
2690 a position later. BYTEPOS is the byte position corresponding to
2691 CHARPOS.
2693 If ROW is not null, calls to produce_glyphs with IT as parameter
2694 will produce glyphs in that row.
2696 BASE_FACE_ID is the id of a base face to use. It must be one of
2697 DEFAULT_FACE_ID for normal text, MODE_LINE_FACE_ID,
2698 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID for displaying
2699 mode lines, or TOOL_BAR_FACE_ID for displaying the tool-bar.
2701 If ROW is null and BASE_FACE_ID is equal to MODE_LINE_FACE_ID,
2702 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID, the iterator
2703 will be initialized to use the corresponding mode line glyph row of
2704 the desired matrix of W. */
2706 void
2707 init_iterator (struct it *it, struct window *w,
2708 ptrdiff_t charpos, ptrdiff_t bytepos,
2709 struct glyph_row *row, enum face_id base_face_id)
2711 enum face_id remapped_base_face_id = base_face_id;
2713 /* Some precondition checks. */
2714 eassert (w != NULL && it != NULL);
2715 eassert (charpos < 0 || (charpos >= BUF_BEG (current_buffer)
2716 && charpos <= ZV));
2718 /* If face attributes have been changed since the last redisplay,
2719 free realized faces now because they depend on face definitions
2720 that might have changed. Don't free faces while there might be
2721 desired matrices pending which reference these faces. */
2722 if (!inhibit_free_realized_faces)
2724 if (face_change)
2726 face_change = false;
2727 XFRAME (w->frame)->face_change = 0;
2728 free_all_realized_faces (Qnil);
2730 else if (XFRAME (w->frame)->face_change)
2732 XFRAME (w->frame)->face_change = 0;
2733 free_all_realized_faces (w->frame);
2737 /* Perhaps remap BASE_FACE_ID to a user-specified alternative. */
2738 if (! NILP (Vface_remapping_alist))
2739 remapped_base_face_id
2740 = lookup_basic_face (XFRAME (w->frame), base_face_id);
2742 /* Use one of the mode line rows of W's desired matrix if
2743 appropriate. */
2744 if (row == NULL)
2746 if (base_face_id == MODE_LINE_FACE_ID
2747 || base_face_id == MODE_LINE_INACTIVE_FACE_ID)
2748 row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
2749 else if (base_face_id == HEADER_LINE_FACE_ID)
2750 row = MATRIX_HEADER_LINE_ROW (w->desired_matrix);
2753 /* Clear IT, and set it->object and other IT's Lisp objects to Qnil.
2754 Other parts of redisplay rely on that. */
2755 memclear (it, sizeof *it);
2756 it->current.overlay_string_index = -1;
2757 it->current.dpvec_index = -1;
2758 it->base_face_id = remapped_base_face_id;
2759 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
2760 it->paragraph_embedding = L2R;
2761 it->bidi_it.w = w;
2763 /* The window in which we iterate over current_buffer: */
2764 XSETWINDOW (it->window, w);
2765 it->w = w;
2766 it->f = XFRAME (w->frame);
2768 it->cmp_it.id = -1;
2770 /* Extra space between lines (on window systems only). */
2771 if (base_face_id == DEFAULT_FACE_ID
2772 && FRAME_WINDOW_P (it->f))
2774 if (NATNUMP (BVAR (current_buffer, extra_line_spacing)))
2775 it->extra_line_spacing = XFASTINT (BVAR (current_buffer, extra_line_spacing));
2776 else if (FLOATP (BVAR (current_buffer, extra_line_spacing)))
2777 it->extra_line_spacing = (XFLOAT_DATA (BVAR (current_buffer, extra_line_spacing))
2778 * FRAME_LINE_HEIGHT (it->f));
2779 else if (it->f->extra_line_spacing > 0)
2780 it->extra_line_spacing = it->f->extra_line_spacing;
2783 /* If realized faces have been removed, e.g. because of face
2784 attribute changes of named faces, recompute them. When running
2785 in batch mode, the face cache of the initial frame is null. If
2786 we happen to get called, make a dummy face cache. */
2787 if (FRAME_FACE_CACHE (it->f) == NULL)
2788 init_frame_faces (it->f);
2789 if (FRAME_FACE_CACHE (it->f)->used == 0)
2790 recompute_basic_faces (it->f);
2792 it->override_ascent = -1;
2794 /* Are control characters displayed as `^C'? */
2795 it->ctl_arrow_p = !NILP (BVAR (current_buffer, ctl_arrow));
2797 /* -1 means everything between a CR and the following line end
2798 is invisible. >0 means lines indented more than this value are
2799 invisible. */
2800 it->selective = (INTEGERP (BVAR (current_buffer, selective_display))
2801 ? (clip_to_bounds
2802 (-1, XINT (BVAR (current_buffer, selective_display)),
2803 PTRDIFF_MAX))
2804 : (!NILP (BVAR (current_buffer, selective_display))
2805 ? -1 : 0));
2806 it->selective_display_ellipsis_p
2807 = !NILP (BVAR (current_buffer, selective_display_ellipses));
2809 /* Display table to use. */
2810 it->dp = window_display_table (w);
2812 /* Are multibyte characters enabled in current_buffer? */
2813 it->multibyte_p = !NILP (BVAR (current_buffer, enable_multibyte_characters));
2815 /* Get the position at which the redisplay_end_trigger hook should
2816 be run, if it is to be run at all. */
2817 if (MARKERP (w->redisplay_end_trigger)
2818 && XMARKER (w->redisplay_end_trigger)->buffer != 0)
2819 it->redisplay_end_trigger_charpos
2820 = marker_position (w->redisplay_end_trigger);
2821 else if (INTEGERP (w->redisplay_end_trigger))
2822 it->redisplay_end_trigger_charpos
2823 = clip_to_bounds (PTRDIFF_MIN, XINT (w->redisplay_end_trigger),
2824 PTRDIFF_MAX);
2826 it->tab_width = SANE_TAB_WIDTH (current_buffer);
2828 /* Are lines in the display truncated? */
2829 if (TRUNCATE != 0)
2830 it->line_wrap = TRUNCATE;
2831 if (base_face_id == DEFAULT_FACE_ID
2832 && !it->w->hscroll
2833 && (WINDOW_FULL_WIDTH_P (it->w)
2834 || NILP (Vtruncate_partial_width_windows)
2835 || (INTEGERP (Vtruncate_partial_width_windows)
2836 /* PXW: Shall we do something about this? */
2837 && (XINT (Vtruncate_partial_width_windows)
2838 <= WINDOW_TOTAL_COLS (it->w))))
2839 && NILP (BVAR (current_buffer, truncate_lines)))
2840 it->line_wrap = NILP (BVAR (current_buffer, word_wrap))
2841 ? WINDOW_WRAP : WORD_WRAP;
2843 /* Get dimensions of truncation and continuation glyphs. These are
2844 displayed as fringe bitmaps under X, but we need them for such
2845 frames when the fringes are turned off. But leave the dimensions
2846 zero for tooltip frames, as these glyphs look ugly there and also
2847 sabotage calculations of tooltip dimensions in x-show-tip. */
2848 #ifdef HAVE_WINDOW_SYSTEM
2849 if (!(FRAME_WINDOW_P (it->f)
2850 && FRAMEP (tip_frame)
2851 && it->f == XFRAME (tip_frame)))
2852 #endif
2854 if (it->line_wrap == TRUNCATE)
2856 /* We will need the truncation glyph. */
2857 eassert (it->glyph_row == NULL);
2858 produce_special_glyphs (it, IT_TRUNCATION);
2859 it->truncation_pixel_width = it->pixel_width;
2861 else
2863 /* We will need the continuation glyph. */
2864 eassert (it->glyph_row == NULL);
2865 produce_special_glyphs (it, IT_CONTINUATION);
2866 it->continuation_pixel_width = it->pixel_width;
2870 /* Reset these values to zero because the produce_special_glyphs
2871 above has changed them. */
2872 it->pixel_width = it->ascent = it->descent = 0;
2873 it->phys_ascent = it->phys_descent = 0;
2875 /* Set this after getting the dimensions of truncation and
2876 continuation glyphs, so that we don't produce glyphs when calling
2877 produce_special_glyphs, above. */
2878 it->glyph_row = row;
2879 it->area = TEXT_AREA;
2881 /* Get the dimensions of the display area. The display area
2882 consists of the visible window area plus a horizontally scrolled
2883 part to the left of the window. All x-values are relative to the
2884 start of this total display area. */
2885 if (base_face_id != DEFAULT_FACE_ID)
2887 /* Mode lines, menu bar in terminal frames. */
2888 it->first_visible_x = 0;
2889 it->last_visible_x = WINDOW_PIXEL_WIDTH (w);
2891 else
2893 /* When hscrolling only the current line, don't apply the
2894 hscroll here, it will be applied by display_line when it gets
2895 to laying out the line showing point. However, if the
2896 window's min_hscroll is positive, the user specified a lower
2897 bound for automatic hscrolling, so they expect the
2898 non-current lines to obey that hscroll amount. */
2899 if (hscrolling_current_line_p (w))
2901 if (w->min_hscroll > 0)
2902 it->first_visible_x = w->min_hscroll * FRAME_COLUMN_WIDTH (it->f);
2903 else
2904 it->first_visible_x = 0;
2906 else
2907 it->first_visible_x =
2908 window_hscroll_limited (w, it->f) * FRAME_COLUMN_WIDTH (it->f);
2909 it->last_visible_x = (it->first_visible_x
2910 + window_box_width (w, TEXT_AREA));
2912 /* If we truncate lines, leave room for the truncation glyph(s) at
2913 the right margin. Otherwise, leave room for the continuation
2914 glyph(s). Done only if the window has no right fringe. */
2915 if (WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0)
2917 if (it->line_wrap == TRUNCATE)
2918 it->last_visible_x -= it->truncation_pixel_width;
2919 else
2920 it->last_visible_x -= it->continuation_pixel_width;
2923 it->header_line_p = WINDOW_WANTS_HEADER_LINE_P (w);
2924 it->current_y = WINDOW_HEADER_LINE_HEIGHT (w) + w->vscroll;
2927 /* Leave room for a border glyph. */
2928 if (!FRAME_WINDOW_P (it->f)
2929 && !WINDOW_RIGHTMOST_P (it->w))
2930 it->last_visible_x -= 1;
2932 it->last_visible_y = window_text_bottom_y (w);
2934 /* For mode lines and alike, arrange for the first glyph having a
2935 left box line if the face specifies a box. */
2936 if (base_face_id != DEFAULT_FACE_ID)
2938 struct face *face;
2940 it->face_id = remapped_base_face_id;
2942 /* If we have a boxed mode line, make the first character appear
2943 with a left box line. */
2944 face = FACE_FROM_ID_OR_NULL (it->f, remapped_base_face_id);
2945 if (face && face->box != FACE_NO_BOX)
2946 it->start_of_box_run_p = true;
2949 /* If a buffer position was specified, set the iterator there,
2950 getting overlays and face properties from that position. */
2951 if (charpos >= BUF_BEG (current_buffer))
2953 it->stop_charpos = charpos;
2954 it->end_charpos = ZV;
2955 eassert (charpos == BYTE_TO_CHAR (bytepos));
2956 IT_CHARPOS (*it) = charpos;
2957 IT_BYTEPOS (*it) = bytepos;
2959 /* We will rely on `reseat' to set this up properly, via
2960 handle_face_prop. */
2961 it->face_id = it->base_face_id;
2963 it->start = it->current;
2964 /* Do we need to reorder bidirectional text? Not if this is a
2965 unibyte buffer: by definition, none of the single-byte
2966 characters are strong R2L, so no reordering is needed. And
2967 bidi.c doesn't support unibyte buffers anyway. Also, don't
2968 reorder while we are loading loadup.el, since the tables of
2969 character properties needed for reordering are not yet
2970 available. */
2971 it->bidi_p =
2972 !redisplay__inhibit_bidi
2973 && !NILP (BVAR (current_buffer, bidi_display_reordering))
2974 && it->multibyte_p;
2976 /* If we are to reorder bidirectional text, init the bidi
2977 iterator. */
2978 if (it->bidi_p)
2980 /* Since we don't know at this point whether there will be
2981 any R2L lines in the window, we reserve space for
2982 truncation/continuation glyphs even if only the left
2983 fringe is absent. */
2984 if (base_face_id == DEFAULT_FACE_ID
2985 && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0
2986 && WINDOW_RIGHT_FRINGE_WIDTH (it->w) != 0)
2988 if (it->line_wrap == TRUNCATE)
2989 it->last_visible_x -= it->truncation_pixel_width;
2990 else
2991 it->last_visible_x -= it->continuation_pixel_width;
2993 /* Note the paragraph direction that this buffer wants to
2994 use. */
2995 if (EQ (BVAR (current_buffer, bidi_paragraph_direction),
2996 Qleft_to_right))
2997 it->paragraph_embedding = L2R;
2998 else if (EQ (BVAR (current_buffer, bidi_paragraph_direction),
2999 Qright_to_left))
3000 it->paragraph_embedding = R2L;
3001 else
3002 it->paragraph_embedding = NEUTRAL_DIR;
3003 bidi_unshelve_cache (NULL, false);
3004 bidi_init_it (charpos, IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
3005 &it->bidi_it);
3008 /* Compute faces etc. */
3009 reseat (it, it->current.pos, true);
3012 CHECK_IT (it);
3016 /* Initialize IT for the display of window W with window start POS. */
3018 void
3019 start_display (struct it *it, struct window *w, struct text_pos pos)
3021 struct glyph_row *row;
3022 bool first_vpos = WINDOW_WANTS_HEADER_LINE_P (w);
3024 row = w->desired_matrix->rows + first_vpos;
3025 init_iterator (it, w, CHARPOS (pos), BYTEPOS (pos), row, DEFAULT_FACE_ID);
3026 it->first_vpos = first_vpos;
3028 /* Don't reseat to previous visible line start if current start
3029 position is in a string or image. */
3030 if (it->method == GET_FROM_BUFFER && it->line_wrap != TRUNCATE)
3032 int first_y = it->current_y;
3034 /* If window start is not at a line start, skip forward to POS to
3035 get the correct continuation lines width. */
3036 bool start_at_line_beg_p = (CHARPOS (pos) == BEGV
3037 || FETCH_BYTE (BYTEPOS (pos) - 1) == '\n');
3038 if (!start_at_line_beg_p)
3040 int new_x;
3042 reseat_at_previous_visible_line_start (it);
3043 move_it_to (it, CHARPOS (pos), -1, -1, -1, MOVE_TO_POS);
3045 new_x = it->current_x + it->pixel_width;
3047 /* If lines are continued, this line may end in the middle
3048 of a multi-glyph character (e.g. a control character
3049 displayed as \003, or in the middle of an overlay
3050 string). In this case move_it_to above will not have
3051 taken us to the start of the continuation line but to the
3052 end of the continued line. */
3053 if (it->current_x > 0
3054 && it->line_wrap != TRUNCATE /* Lines are continued. */
3055 && (/* And glyph doesn't fit on the line. */
3056 new_x > it->last_visible_x
3057 /* Or it fits exactly and we're on a window
3058 system frame. */
3059 || (new_x == it->last_visible_x
3060 && FRAME_WINDOW_P (it->f)
3061 && ((it->bidi_p && it->bidi_it.paragraph_dir == R2L)
3062 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
3063 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)))))
3065 if ((it->current.dpvec_index >= 0
3066 || it->current.overlay_string_index >= 0)
3067 /* If we are on a newline from a display vector or
3068 overlay string, then we are already at the end of
3069 a screen line; no need to go to the next line in
3070 that case, as this line is not really continued.
3071 (If we do go to the next line, C-e will not DTRT.) */
3072 && it->c != '\n')
3074 set_iterator_to_next (it, true);
3075 move_it_in_display_line_to (it, -1, -1, 0);
3078 it->continuation_lines_width += it->current_x;
3080 /* If the character at POS is displayed via a display
3081 vector, move_it_to above stops at the final glyph of
3082 IT->dpvec. To make the caller redisplay that character
3083 again (a.k.a. start at POS), we need to reset the
3084 dpvec_index to the beginning of IT->dpvec. */
3085 else if (it->current.dpvec_index >= 0)
3086 it->current.dpvec_index = 0;
3088 /* We're starting a new display line, not affected by the
3089 height of the continued line, so clear the appropriate
3090 fields in the iterator structure. */
3091 it->max_ascent = it->max_descent = 0;
3092 it->max_phys_ascent = it->max_phys_descent = 0;
3094 it->current_y = first_y;
3095 it->vpos = 0;
3096 it->current_x = it->hpos = 0;
3102 /* Return true if POS is a position in ellipses displayed for invisible
3103 text. W is the window we display, for text property lookup. */
3105 static bool
3106 in_ellipses_for_invisible_text_p (struct display_pos *pos, struct window *w)
3108 Lisp_Object prop, window;
3109 bool ellipses_p = false;
3110 ptrdiff_t charpos = CHARPOS (pos->pos);
3112 /* If POS specifies a position in a display vector, this might
3113 be for an ellipsis displayed for invisible text. We won't
3114 get the iterator set up for delivering that ellipsis unless
3115 we make sure that it gets aware of the invisible text. */
3116 if (pos->dpvec_index >= 0
3117 && pos->overlay_string_index < 0
3118 && CHARPOS (pos->string_pos) < 0
3119 && charpos > BEGV
3120 && (XSETWINDOW (window, w),
3121 prop = Fget_char_property (make_number (charpos),
3122 Qinvisible, window),
3123 TEXT_PROP_MEANS_INVISIBLE (prop) == 0))
3125 prop = Fget_char_property (make_number (charpos - 1), Qinvisible,
3126 window);
3127 ellipses_p = 2 == TEXT_PROP_MEANS_INVISIBLE (prop);
3130 return ellipses_p;
3134 /* Initialize IT for stepping through current_buffer in window W,
3135 starting at position POS that includes overlay string and display
3136 vector/ control character translation position information. Value
3137 is false if there are overlay strings with newlines at POS. */
3139 static bool
3140 init_from_display_pos (struct it *it, struct window *w, struct display_pos *pos)
3142 ptrdiff_t charpos = CHARPOS (pos->pos), bytepos = BYTEPOS (pos->pos);
3143 int i;
3144 bool overlay_strings_with_newlines = false;
3146 /* If POS specifies a position in a display vector, this might
3147 be for an ellipsis displayed for invisible text. We won't
3148 get the iterator set up for delivering that ellipsis unless
3149 we make sure that it gets aware of the invisible text. */
3150 if (in_ellipses_for_invisible_text_p (pos, w))
3152 --charpos;
3153 bytepos = 0;
3156 /* Keep in mind: the call to reseat in init_iterator skips invisible
3157 text, so we might end up at a position different from POS. This
3158 is only a problem when POS is a row start after a newline and an
3159 overlay starts there with an after-string, and the overlay has an
3160 invisible property. Since we don't skip invisible text in
3161 display_line and elsewhere immediately after consuming the
3162 newline before the row start, such a POS will not be in a string,
3163 but the call to init_iterator below will move us to the
3164 after-string. */
3165 init_iterator (it, w, charpos, bytepos, NULL, DEFAULT_FACE_ID);
3167 /* This only scans the current chunk -- it should scan all chunks.
3168 However, OVERLAY_STRING_CHUNK_SIZE has been increased from 3 in 21.1
3169 to 16 in 22.1 to make this a lesser problem. */
3170 for (i = 0; i < it->n_overlay_strings && i < OVERLAY_STRING_CHUNK_SIZE; ++i)
3172 const char *s = SSDATA (it->overlay_strings[i]);
3173 const char *e = s + SBYTES (it->overlay_strings[i]);
3175 while (s < e && *s != '\n')
3176 ++s;
3178 if (s < e)
3180 overlay_strings_with_newlines = true;
3181 break;
3185 /* If position is within an overlay string, set up IT to the right
3186 overlay string. */
3187 if (pos->overlay_string_index >= 0)
3189 int relative_index;
3191 /* If the first overlay string happens to have a `display'
3192 property for an image, the iterator will be set up for that
3193 image, and we have to undo that setup first before we can
3194 correct the overlay string index. */
3195 if (it->method == GET_FROM_IMAGE)
3196 pop_it (it);
3198 /* We already have the first chunk of overlay strings in
3199 IT->overlay_strings. Load more until the one for
3200 pos->overlay_string_index is in IT->overlay_strings. */
3201 if (pos->overlay_string_index >= OVERLAY_STRING_CHUNK_SIZE)
3203 ptrdiff_t n = pos->overlay_string_index / OVERLAY_STRING_CHUNK_SIZE;
3204 it->current.overlay_string_index = 0;
3205 while (n--)
3207 load_overlay_strings (it, 0);
3208 it->current.overlay_string_index += OVERLAY_STRING_CHUNK_SIZE;
3212 it->current.overlay_string_index = pos->overlay_string_index;
3213 relative_index = (it->current.overlay_string_index
3214 % OVERLAY_STRING_CHUNK_SIZE);
3215 it->string = it->overlay_strings[relative_index];
3216 eassert (STRINGP (it->string));
3217 it->current.string_pos = pos->string_pos;
3218 it->method = GET_FROM_STRING;
3219 it->end_charpos = SCHARS (it->string);
3220 /* Set up the bidi iterator for this overlay string. */
3221 if (it->bidi_p)
3223 it->bidi_it.string.lstring = it->string;
3224 it->bidi_it.string.s = NULL;
3225 it->bidi_it.string.schars = SCHARS (it->string);
3226 it->bidi_it.string.bufpos = it->overlay_strings_charpos;
3227 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
3228 it->bidi_it.string.unibyte = !it->multibyte_p;
3229 it->bidi_it.w = it->w;
3230 bidi_init_it (IT_STRING_CHARPOS (*it), IT_STRING_BYTEPOS (*it),
3231 FRAME_WINDOW_P (it->f), &it->bidi_it);
3233 /* Synchronize the state of the bidi iterator with
3234 pos->string_pos. For any string position other than
3235 zero, this will be done automagically when we resume
3236 iteration over the string and get_visually_first_element
3237 is called. But if string_pos is zero, and the string is
3238 to be reordered for display, we need to resync manually,
3239 since it could be that the iteration state recorded in
3240 pos ended at string_pos of 0 moving backwards in string. */
3241 if (CHARPOS (pos->string_pos) == 0)
3243 get_visually_first_element (it);
3244 if (IT_STRING_CHARPOS (*it) != 0)
3245 do {
3246 /* Paranoia. */
3247 eassert (it->bidi_it.charpos < it->bidi_it.string.schars);
3248 bidi_move_to_visually_next (&it->bidi_it);
3249 } while (it->bidi_it.charpos != 0);
3251 eassert (IT_STRING_CHARPOS (*it) == it->bidi_it.charpos
3252 && IT_STRING_BYTEPOS (*it) == it->bidi_it.bytepos);
3256 if (CHARPOS (pos->string_pos) >= 0)
3258 /* Recorded position is not in an overlay string, but in another
3259 string. This can only be a string from a `display' property.
3260 IT should already be filled with that string. */
3261 it->current.string_pos = pos->string_pos;
3262 eassert (STRINGP (it->string));
3263 if (it->bidi_p)
3264 bidi_init_it (IT_STRING_CHARPOS (*it), IT_STRING_BYTEPOS (*it),
3265 FRAME_WINDOW_P (it->f), &it->bidi_it);
3268 /* Restore position in display vector translations, control
3269 character translations or ellipses. */
3270 if (pos->dpvec_index >= 0)
3272 if (it->dpvec == NULL)
3273 get_next_display_element (it);
3274 eassert (it->dpvec && it->current.dpvec_index == 0);
3275 it->current.dpvec_index = pos->dpvec_index;
3278 CHECK_IT (it);
3279 return !overlay_strings_with_newlines;
3283 /* Initialize IT for stepping through current_buffer in window W
3284 starting at ROW->start. */
3286 static void
3287 init_to_row_start (struct it *it, struct window *w, struct glyph_row *row)
3289 init_from_display_pos (it, w, &row->start);
3290 it->start = row->start;
3291 it->continuation_lines_width = row->continuation_lines_width;
3292 CHECK_IT (it);
3296 /* Initialize IT for stepping through current_buffer in window W
3297 starting in the line following ROW, i.e. starting at ROW->end.
3298 Value is false if there are overlay strings with newlines at ROW's
3299 end position. */
3301 static bool
3302 init_to_row_end (struct it *it, struct window *w, struct glyph_row *row)
3304 bool success = false;
3306 if (init_from_display_pos (it, w, &row->end))
3308 if (row->continued_p)
3309 it->continuation_lines_width
3310 = row->continuation_lines_width + row->pixel_width;
3311 CHECK_IT (it);
3312 success = true;
3315 return success;
3321 /***********************************************************************
3322 Text properties
3323 ***********************************************************************/
3325 /* Called when IT reaches IT->stop_charpos. Handle text property and
3326 overlay changes. Set IT->stop_charpos to the next position where
3327 to stop. */
3329 static void
3330 handle_stop (struct it *it)
3332 enum prop_handled handled;
3333 bool handle_overlay_change_p;
3334 struct props *p;
3336 it->dpvec = NULL;
3337 it->current.dpvec_index = -1;
3338 handle_overlay_change_p = !it->ignore_overlay_strings_at_pos_p;
3339 it->ellipsis_p = false;
3341 /* Use face of preceding text for ellipsis (if invisible) */
3342 if (it->selective_display_ellipsis_p)
3343 it->saved_face_id = it->face_id;
3345 /* Here's the description of the semantics of, and the logic behind,
3346 the various HANDLED_* statuses:
3348 HANDLED_NORMALLY means the handler did its job, and the loop
3349 should proceed to calling the next handler in order.
3351 HANDLED_RECOMPUTE_PROPS means the handler caused a significant
3352 change in the properties and overlays at current position, so the
3353 loop should be restarted, to re-invoke the handlers that were
3354 already called. This happens when fontification-functions were
3355 called by handle_fontified_prop, and actually fontified
3356 something. Another case where HANDLED_RECOMPUTE_PROPS is
3357 returned is when we discover overlay strings that need to be
3358 displayed right away. The loop below will continue for as long
3359 as the status is HANDLED_RECOMPUTE_PROPS.
3361 HANDLED_RETURN means return immediately to the caller, to
3362 continue iteration without calling any further handlers. This is
3363 used when we need to act on some property right away, for example
3364 when we need to display the ellipsis or a replacing display
3365 property, such as display string or image.
3367 HANDLED_OVERLAY_STRING_CONSUMED means an overlay string was just
3368 consumed, and the handler switched to the next overlay string.
3369 This signals the loop below to refrain from looking for more
3370 overlays before all the overlay strings of the current overlay
3371 are processed.
3373 Some of the handlers called by the loop push the iterator state
3374 onto the stack (see 'push_it'), and arrange for the iteration to
3375 continue with another object, such as an image, a display string,
3376 or an overlay string. In most such cases, it->stop_charpos is
3377 set to the first character of the string, so that when the
3378 iteration resumes, this function will immediately be called
3379 again, to examine the properties at the beginning of the string.
3381 When a display or overlay string is exhausted, the iterator state
3382 is popped (see 'pop_it'), and iteration continues with the
3383 previous object. Again, in many such cases this function is
3384 called again to find the next position where properties might
3385 change. */
3389 handled = HANDLED_NORMALLY;
3391 /* Call text property handlers. */
3392 for (p = it_props; p->handler; ++p)
3394 handled = p->handler (it);
3396 if (handled == HANDLED_RECOMPUTE_PROPS)
3397 break;
3398 else if (handled == HANDLED_RETURN)
3400 /* We still want to show before and after strings from
3401 overlays even if the actual buffer text is replaced. */
3402 if (!handle_overlay_change_p
3403 || it->sp > 1
3404 /* Don't call get_overlay_strings_1 if we already
3405 have overlay strings loaded, because doing so
3406 will load them again and push the iterator state
3407 onto the stack one more time, which is not
3408 expected by the rest of the code that processes
3409 overlay strings. */
3410 || (it->current.overlay_string_index < 0
3411 && !get_overlay_strings_1 (it, 0, false)))
3413 if (it->ellipsis_p)
3414 setup_for_ellipsis (it, 0);
3415 /* When handling a display spec, we might load an
3416 empty string. In that case, discard it here. We
3417 used to discard it in handle_single_display_spec,
3418 but that causes get_overlay_strings_1, above, to
3419 ignore overlay strings that we must check. */
3420 if (STRINGP (it->string) && !SCHARS (it->string))
3421 pop_it (it);
3422 return;
3424 else if (STRINGP (it->string) && !SCHARS (it->string))
3425 pop_it (it);
3426 else
3428 it->string_from_display_prop_p = false;
3429 it->from_disp_prop_p = false;
3430 handle_overlay_change_p = false;
3432 handled = HANDLED_RECOMPUTE_PROPS;
3433 break;
3435 else if (handled == HANDLED_OVERLAY_STRING_CONSUMED)
3436 handle_overlay_change_p = false;
3439 if (handled != HANDLED_RECOMPUTE_PROPS)
3441 /* Don't check for overlay strings below when set to deliver
3442 characters from a display vector. */
3443 if (it->method == GET_FROM_DISPLAY_VECTOR)
3444 handle_overlay_change_p = false;
3446 /* Handle overlay changes.
3447 This sets HANDLED to HANDLED_RECOMPUTE_PROPS
3448 if it finds overlays. */
3449 if (handle_overlay_change_p)
3450 handled = handle_overlay_change (it);
3453 if (it->ellipsis_p)
3455 setup_for_ellipsis (it, 0);
3456 break;
3459 while (handled == HANDLED_RECOMPUTE_PROPS);
3461 /* Determine where to stop next. */
3462 if (handled == HANDLED_NORMALLY)
3463 compute_stop_pos (it);
3467 /* Compute IT->stop_charpos from text property and overlay change
3468 information for IT's current position. */
3470 static void
3471 compute_stop_pos (struct it *it)
3473 register INTERVAL iv, next_iv;
3474 Lisp_Object object, limit, position;
3475 ptrdiff_t charpos, bytepos;
3477 if (STRINGP (it->string))
3479 /* Strings are usually short, so don't limit the search for
3480 properties. */
3481 it->stop_charpos = it->end_charpos;
3482 object = it->string;
3483 limit = Qnil;
3484 charpos = IT_STRING_CHARPOS (*it);
3485 bytepos = IT_STRING_BYTEPOS (*it);
3487 else
3489 ptrdiff_t pos;
3491 /* If end_charpos is out of range for some reason, such as a
3492 misbehaving display function, rationalize it (Bug#5984). */
3493 if (it->end_charpos > ZV)
3494 it->end_charpos = ZV;
3495 it->stop_charpos = it->end_charpos;
3497 /* If next overlay change is in front of the current stop pos
3498 (which is IT->end_charpos), stop there. Note: value of
3499 next_overlay_change is point-max if no overlay change
3500 follows. */
3501 charpos = IT_CHARPOS (*it);
3502 bytepos = IT_BYTEPOS (*it);
3503 pos = next_overlay_change (charpos);
3504 if (pos < it->stop_charpos)
3505 it->stop_charpos = pos;
3507 /* Set up variables for computing the stop position from text
3508 property changes. */
3509 XSETBUFFER (object, current_buffer);
3510 limit = make_number (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT);
3513 /* Get the interval containing IT's position. Value is a null
3514 interval if there isn't such an interval. */
3515 position = make_number (charpos);
3516 iv = validate_interval_range (object, &position, &position, false);
3517 if (iv)
3519 Lisp_Object values_here[LAST_PROP_IDX];
3520 struct props *p;
3522 /* Get properties here. */
3523 for (p = it_props; p->handler; ++p)
3524 values_here[p->idx] = textget (iv->plist,
3525 builtin_lisp_symbol (p->name));
3527 /* Look for an interval following iv that has different
3528 properties. */
3529 for (next_iv = next_interval (iv);
3530 (next_iv
3531 && (NILP (limit)
3532 || XFASTINT (limit) > next_iv->position));
3533 next_iv = next_interval (next_iv))
3535 for (p = it_props; p->handler; ++p)
3537 Lisp_Object new_value = textget (next_iv->plist,
3538 builtin_lisp_symbol (p->name));
3539 if (!EQ (values_here[p->idx], new_value))
3540 break;
3543 if (p->handler)
3544 break;
3547 if (next_iv)
3549 if (INTEGERP (limit)
3550 && next_iv->position >= XFASTINT (limit))
3551 /* No text property change up to limit. */
3552 it->stop_charpos = min (XFASTINT (limit), it->stop_charpos);
3553 else
3554 /* Text properties change in next_iv. */
3555 it->stop_charpos = min (it->stop_charpos, next_iv->position);
3559 if (it->cmp_it.id < 0)
3561 ptrdiff_t stoppos = it->end_charpos;
3563 if (it->bidi_p && it->bidi_it.scan_dir < 0)
3564 stoppos = -1;
3565 composition_compute_stop_pos (&it->cmp_it, charpos, bytepos,
3566 stoppos, it->string);
3569 eassert (STRINGP (it->string)
3570 || (it->stop_charpos >= BEGV
3571 && it->stop_charpos >= IT_CHARPOS (*it)));
3575 /* Return the position of the next overlay change after POS in
3576 current_buffer. Value is point-max if no overlay change
3577 follows. This is like `next-overlay-change' but doesn't use
3578 xmalloc. */
3580 static ptrdiff_t
3581 next_overlay_change (ptrdiff_t pos)
3583 ptrdiff_t i, noverlays;
3584 ptrdiff_t endpos;
3585 Lisp_Object *overlays;
3586 USE_SAFE_ALLOCA;
3588 /* Get all overlays at the given position. */
3589 GET_OVERLAYS_AT (pos, overlays, noverlays, &endpos, true);
3591 /* If any of these overlays ends before endpos,
3592 use its ending point instead. */
3593 for (i = 0; i < noverlays; ++i)
3595 Lisp_Object oend;
3596 ptrdiff_t oendpos;
3598 oend = OVERLAY_END (overlays[i]);
3599 oendpos = OVERLAY_POSITION (oend);
3600 endpos = min (endpos, oendpos);
3603 SAFE_FREE ();
3604 return endpos;
3607 /* How many characters forward to search for a display property or
3608 display string. Searching too far forward makes the bidi display
3609 sluggish, especially in small windows. */
3610 #define MAX_DISP_SCAN 250
3612 /* Return the character position of a display string at or after
3613 position specified by POSITION. If no display string exists at or
3614 after POSITION, return ZV. A display string is either an overlay
3615 with `display' property whose value is a string, or a `display'
3616 text property whose value is a string. STRING is data about the
3617 string to iterate; if STRING->lstring is nil, we are iterating a
3618 buffer. FRAME_WINDOW_P is true when we are displaying a window
3619 on a GUI frame. DISP_PROP is set to zero if we searched
3620 MAX_DISP_SCAN characters forward without finding any display
3621 strings, non-zero otherwise. It is set to 2 if the display string
3622 uses any kind of `(space ...)' spec that will produce a stretch of
3623 white space in the text area. */
3624 ptrdiff_t
3625 compute_display_string_pos (struct text_pos *position,
3626 struct bidi_string_data *string,
3627 struct window *w,
3628 bool frame_window_p, int *disp_prop)
3630 /* OBJECT = nil means current buffer. */
3631 Lisp_Object object, object1;
3632 Lisp_Object pos, spec, limpos;
3633 bool string_p = string && (STRINGP (string->lstring) || string->s);
3634 ptrdiff_t eob = string_p ? string->schars : ZV;
3635 ptrdiff_t begb = string_p ? 0 : BEGV;
3636 ptrdiff_t bufpos, charpos = CHARPOS (*position);
3637 ptrdiff_t lim =
3638 (charpos < eob - MAX_DISP_SCAN) ? charpos + MAX_DISP_SCAN : eob;
3639 struct text_pos tpos;
3640 int rv = 0;
3642 if (string && STRINGP (string->lstring))
3643 object1 = object = string->lstring;
3644 else if (w && !string_p)
3646 XSETWINDOW (object, w);
3647 object1 = Qnil;
3649 else
3650 object1 = object = Qnil;
3652 *disp_prop = 1;
3654 if (charpos >= eob
3655 /* We don't support display properties whose values are strings
3656 that have display string properties. */
3657 || string->from_disp_str
3658 /* C strings cannot have display properties. */
3659 || (string->s && !STRINGP (object)))
3661 *disp_prop = 0;
3662 return eob;
3665 /* If the character at CHARPOS is where the display string begins,
3666 return CHARPOS. */
3667 pos = make_number (charpos);
3668 if (STRINGP (object))
3669 bufpos = string->bufpos;
3670 else
3671 bufpos = charpos;
3672 tpos = *position;
3673 if (!NILP (spec = Fget_char_property (pos, Qdisplay, object))
3674 && (charpos <= begb
3675 || !EQ (Fget_char_property (make_number (charpos - 1), Qdisplay,
3676 object),
3677 spec))
3678 && (rv = handle_display_spec (NULL, spec, object, Qnil, &tpos, bufpos,
3679 frame_window_p)))
3681 if (rv == 2)
3682 *disp_prop = 2;
3683 return charpos;
3686 /* Look forward for the first character with a `display' property
3687 that will replace the underlying text when displayed. */
3688 limpos = make_number (lim);
3689 do {
3690 pos = Fnext_single_char_property_change (pos, Qdisplay, object1, limpos);
3691 CHARPOS (tpos) = XFASTINT (pos);
3692 if (CHARPOS (tpos) >= lim)
3694 *disp_prop = 0;
3695 break;
3697 if (STRINGP (object))
3698 BYTEPOS (tpos) = string_char_to_byte (object, CHARPOS (tpos));
3699 else
3700 BYTEPOS (tpos) = CHAR_TO_BYTE (CHARPOS (tpos));
3701 spec = Fget_char_property (pos, Qdisplay, object);
3702 if (!STRINGP (object))
3703 bufpos = CHARPOS (tpos);
3704 } while (NILP (spec)
3705 || !(rv = handle_display_spec (NULL, spec, object, Qnil, &tpos,
3706 bufpos, frame_window_p)));
3707 if (rv == 2)
3708 *disp_prop = 2;
3710 return CHARPOS (tpos);
3713 /* Return the character position of the end of the display string that
3714 started at CHARPOS. If there's no display string at CHARPOS,
3715 return -1. A display string is either an overlay with `display'
3716 property whose value is a string or a `display' text property whose
3717 value is a string. */
3718 ptrdiff_t
3719 compute_display_string_end (ptrdiff_t charpos, struct bidi_string_data *string)
3721 /* OBJECT = nil means current buffer. */
3722 Lisp_Object object =
3723 (string && STRINGP (string->lstring)) ? string->lstring : Qnil;
3724 Lisp_Object pos = make_number (charpos);
3725 ptrdiff_t eob =
3726 (STRINGP (object) || (string && string->s)) ? string->schars : ZV;
3728 if (charpos >= eob || (string->s && !STRINGP (object)))
3729 return eob;
3731 /* It could happen that the display property or overlay was removed
3732 since we found it in compute_display_string_pos above. One way
3733 this can happen is if JIT font-lock was called (through
3734 handle_fontified_prop), and jit-lock-functions remove text
3735 properties or overlays from the portion of buffer that includes
3736 CHARPOS. Muse mode is known to do that, for example. In this
3737 case, we return -1 to the caller, to signal that no display
3738 string is actually present at CHARPOS. See bidi_fetch_char for
3739 how this is handled.
3741 An alternative would be to never look for display properties past
3742 it->stop_charpos. But neither compute_display_string_pos nor
3743 bidi_fetch_char that calls it know or care where the next
3744 stop_charpos is. */
3745 if (NILP (Fget_char_property (pos, Qdisplay, object)))
3746 return -1;
3748 /* Look forward for the first character where the `display' property
3749 changes. */
3750 pos = Fnext_single_char_property_change (pos, Qdisplay, object, Qnil);
3752 return XFASTINT (pos);
3757 /***********************************************************************
3758 Fontification
3759 ***********************************************************************/
3761 /* Handle changes in the `fontified' property of the current buffer by
3762 calling hook functions from Qfontification_functions to fontify
3763 regions of text. */
3765 static enum prop_handled
3766 handle_fontified_prop (struct it *it)
3768 Lisp_Object prop, pos;
3769 enum prop_handled handled = HANDLED_NORMALLY;
3771 if (!NILP (Vmemory_full))
3772 return handled;
3774 /* Get the value of the `fontified' property at IT's current buffer
3775 position. (The `fontified' property doesn't have a special
3776 meaning in strings.) If the value is nil, call functions from
3777 Qfontification_functions. */
3778 if (!STRINGP (it->string)
3779 && it->s == NULL
3780 && !NILP (Vfontification_functions)
3781 && !NILP (Vrun_hooks)
3782 && (pos = make_number (IT_CHARPOS (*it)),
3783 prop = Fget_char_property (pos, Qfontified, Qnil),
3784 /* Ignore the special cased nil value always present at EOB since
3785 no amount of fontifying will be able to change it. */
3786 NILP (prop) && IT_CHARPOS (*it) < Z))
3788 ptrdiff_t count = SPECPDL_INDEX ();
3789 Lisp_Object val;
3790 struct buffer *obuf = current_buffer;
3791 ptrdiff_t begv = BEGV, zv = ZV;
3792 bool old_clip_changed = current_buffer->clip_changed;
3794 val = Vfontification_functions;
3795 specbind (Qfontification_functions, Qnil);
3797 eassert (it->end_charpos == ZV);
3799 if (!CONSP (val) || EQ (XCAR (val), Qlambda))
3800 safe_call1 (val, pos);
3801 else
3803 Lisp_Object fns, fn;
3805 fns = Qnil;
3807 for (; CONSP (val); val = XCDR (val))
3809 fn = XCAR (val);
3811 if (EQ (fn, Qt))
3813 /* A value of t indicates this hook has a local
3814 binding; it means to run the global binding too.
3815 In a global value, t should not occur. If it
3816 does, we must ignore it to avoid an endless
3817 loop. */
3818 for (fns = Fdefault_value (Qfontification_functions);
3819 CONSP (fns);
3820 fns = XCDR (fns))
3822 fn = XCAR (fns);
3823 if (!EQ (fn, Qt))
3824 safe_call1 (fn, pos);
3827 else
3828 safe_call1 (fn, pos);
3832 unbind_to (count, Qnil);
3834 /* Fontification functions routinely call `save-restriction'.
3835 Normally, this tags clip_changed, which can confuse redisplay
3836 (see discussion in Bug#6671). Since we don't perform any
3837 special handling of fontification changes in the case where
3838 `save-restriction' isn't called, there's no point doing so in
3839 this case either. So, if the buffer's restrictions are
3840 actually left unchanged, reset clip_changed. */
3841 if (obuf == current_buffer)
3843 if (begv == BEGV && zv == ZV)
3844 current_buffer->clip_changed = old_clip_changed;
3846 /* There isn't much we can reasonably do to protect against
3847 misbehaving fontification, but here's a fig leaf. */
3848 else if (BUFFER_LIVE_P (obuf))
3849 set_buffer_internal_1 (obuf);
3851 /* The fontification code may have added/removed text.
3852 It could do even a lot worse, but let's at least protect against
3853 the most obvious case where only the text past `pos' gets changed',
3854 as is/was done in grep.el where some escapes sequences are turned
3855 into face properties (bug#7876). */
3856 it->end_charpos = ZV;
3858 /* Return HANDLED_RECOMPUTE_PROPS only if function fontified
3859 something. This avoids an endless loop if they failed to
3860 fontify the text for which reason ever. */
3861 if (!NILP (Fget_char_property (pos, Qfontified, Qnil)))
3862 handled = HANDLED_RECOMPUTE_PROPS;
3865 return handled;
3870 /***********************************************************************
3871 Faces
3872 ***********************************************************************/
3874 /* Set up iterator IT from face properties at its current position.
3875 Called from handle_stop. */
3877 static enum prop_handled
3878 handle_face_prop (struct it *it)
3880 int new_face_id;
3881 ptrdiff_t next_stop;
3883 if (!STRINGP (it->string))
3885 new_face_id
3886 = face_at_buffer_position (it->w,
3887 IT_CHARPOS (*it),
3888 &next_stop,
3889 (IT_CHARPOS (*it)
3890 + TEXT_PROP_DISTANCE_LIMIT),
3891 false, it->base_face_id);
3893 /* Is this a start of a run of characters with box face?
3894 Caveat: this can be called for a freshly initialized
3895 iterator; face_id is -1 in this case. We know that the new
3896 face will not change until limit, i.e. if the new face has a
3897 box, all characters up to limit will have one. But, as
3898 usual, we don't know whether limit is really the end. */
3899 if (new_face_id != it->face_id)
3901 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3902 /* If it->face_id is -1, old_face below will be NULL, see
3903 the definition of FACE_FROM_ID_OR_NULL. This will happen
3904 if this is the initial call that gets the face. */
3905 struct face *old_face = FACE_FROM_ID_OR_NULL (it->f, it->face_id);
3907 /* If the value of face_id of the iterator is -1, we have to
3908 look in front of IT's position and see whether there is a
3909 face there that's different from new_face_id. */
3910 if (!old_face && IT_CHARPOS (*it) > BEG)
3912 int prev_face_id = face_before_it_pos (it);
3914 old_face = FACE_FROM_ID_OR_NULL (it->f, prev_face_id);
3917 /* If the new face has a box, but the old face does not,
3918 this is the start of a run of characters with box face,
3919 i.e. this character has a shadow on the left side. */
3920 it->start_of_box_run_p = (new_face->box != FACE_NO_BOX
3921 && (old_face == NULL || !old_face->box));
3922 it->face_box_p = new_face->box != FACE_NO_BOX;
3925 else
3927 int base_face_id;
3928 ptrdiff_t bufpos;
3929 int i;
3930 Lisp_Object from_overlay
3931 = (it->current.overlay_string_index >= 0
3932 ? it->string_overlays[it->current.overlay_string_index
3933 % OVERLAY_STRING_CHUNK_SIZE]
3934 : Qnil);
3936 /* See if we got to this string directly or indirectly from
3937 an overlay property. That includes the before-string or
3938 after-string of an overlay, strings in display properties
3939 provided by an overlay, their text properties, etc.
3941 FROM_OVERLAY is the overlay that brought us here, or nil if none. */
3942 if (! NILP (from_overlay))
3943 for (i = it->sp - 1; i >= 0; i--)
3945 if (it->stack[i].current.overlay_string_index >= 0)
3946 from_overlay
3947 = it->string_overlays[it->stack[i].current.overlay_string_index
3948 % OVERLAY_STRING_CHUNK_SIZE];
3949 else if (! NILP (it->stack[i].from_overlay))
3950 from_overlay = it->stack[i].from_overlay;
3952 if (!NILP (from_overlay))
3953 break;
3956 if (! NILP (from_overlay))
3958 bufpos = IT_CHARPOS (*it);
3959 /* For a string from an overlay, the base face depends
3960 only on text properties and ignores overlays. */
3961 base_face_id
3962 = face_for_overlay_string (it->w,
3963 IT_CHARPOS (*it),
3964 &next_stop,
3965 (IT_CHARPOS (*it)
3966 + TEXT_PROP_DISTANCE_LIMIT),
3967 false,
3968 from_overlay);
3970 else
3972 bufpos = 0;
3974 /* For strings from a `display' property, use the face at
3975 IT's current buffer position as the base face to merge
3976 with, so that overlay strings appear in the same face as
3977 surrounding text, unless they specify their own faces.
3978 For strings from wrap-prefix and line-prefix properties,
3979 use the default face, possibly remapped via
3980 Vface_remapping_alist. */
3981 /* Note that the fact that we use the face at _buffer_
3982 position means that a 'display' property on an overlay
3983 string will not inherit the face of that overlay string,
3984 but will instead revert to the face of buffer text
3985 covered by the overlay. This is visible, e.g., when the
3986 overlay specifies a box face, but neither the buffer nor
3987 the display string do. This sounds like a design bug,
3988 but Emacs always did that since v21.1, so changing that
3989 might be a big deal. */
3990 base_face_id = it->string_from_prefix_prop_p
3991 ? (!NILP (Vface_remapping_alist)
3992 ? lookup_basic_face (it->f, DEFAULT_FACE_ID)
3993 : DEFAULT_FACE_ID)
3994 : underlying_face_id (it);
3997 new_face_id = face_at_string_position (it->w,
3998 it->string,
3999 IT_STRING_CHARPOS (*it),
4000 bufpos,
4001 &next_stop,
4002 base_face_id, false);
4004 /* Is this a start of a run of characters with box? Caveat:
4005 this can be called for a freshly allocated iterator; face_id
4006 is -1 is this case. We know that the new face will not
4007 change until the next check pos, i.e. if the new face has a
4008 box, all characters up to that position will have a
4009 box. But, as usual, we don't know whether that position
4010 is really the end. */
4011 if (new_face_id != it->face_id)
4013 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
4014 struct face *old_face = FACE_FROM_ID_OR_NULL (it->f, it->face_id);
4016 /* If new face has a box but old face hasn't, this is the
4017 start of a run of characters with box, i.e. it has a
4018 shadow on the left side. */
4019 it->start_of_box_run_p
4020 = new_face->box && (old_face == NULL || !old_face->box);
4021 it->face_box_p = new_face->box != FACE_NO_BOX;
4025 it->face_id = new_face_id;
4026 return HANDLED_NORMALLY;
4030 /* Return the ID of the face ``underlying'' IT's current position,
4031 which is in a string. If the iterator is associated with a
4032 buffer, return the face at IT's current buffer position.
4033 Otherwise, use the iterator's base_face_id. */
4035 static int
4036 underlying_face_id (struct it *it)
4038 int face_id = it->base_face_id, i;
4040 eassert (STRINGP (it->string));
4042 for (i = it->sp - 1; i >= 0; --i)
4043 if (NILP (it->stack[i].string))
4044 face_id = it->stack[i].face_id;
4046 return face_id;
4050 /* Compute the face one character before or after the current position
4051 of IT, in the visual order. BEFORE_P means get the face
4052 in front (to the left in L2R paragraphs, to the right in R2L
4053 paragraphs) of IT's screen position. Value is the ID of the face. */
4055 static int
4056 face_before_or_after_it_pos (struct it *it, bool before_p)
4058 int face_id, limit;
4059 ptrdiff_t next_check_charpos;
4060 struct it it_copy;
4061 void *it_copy_data = NULL;
4063 eassert (it->s == NULL);
4065 if (STRINGP (it->string))
4067 ptrdiff_t bufpos, charpos;
4068 int base_face_id;
4070 /* No face change past the end of the string (for the case
4071 we are padding with spaces). No face change before the
4072 string start. */
4073 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string)
4074 || (IT_STRING_CHARPOS (*it) == 0 && before_p))
4075 return it->face_id;
4077 if (!it->bidi_p)
4079 /* Set charpos to the position before or after IT's current
4080 position, in the logical order, which in the non-bidi
4081 case is the same as the visual order. */
4082 if (before_p)
4083 charpos = IT_STRING_CHARPOS (*it) - 1;
4084 else if (it->what == IT_COMPOSITION)
4085 /* For composition, we must check the character after the
4086 composition. */
4087 charpos = IT_STRING_CHARPOS (*it) + it->cmp_it.nchars;
4088 else
4089 charpos = IT_STRING_CHARPOS (*it) + 1;
4091 else
4093 if (before_p)
4095 /* With bidi iteration, the character before the current
4096 in the visual order cannot be found by simple
4097 iteration, because "reverse" reordering is not
4098 supported. Instead, we need to start from the string
4099 beginning and go all the way to the current string
4100 position, remembering the previous position. */
4101 /* Ignore face changes before the first visible
4102 character on this display line. */
4103 if (it->current_x <= it->first_visible_x)
4104 return it->face_id;
4105 SAVE_IT (it_copy, *it, it_copy_data);
4106 IT_STRING_CHARPOS (it_copy) = 0;
4107 bidi_init_it (0, 0, FRAME_WINDOW_P (it_copy.f), &it_copy.bidi_it);
4111 charpos = IT_STRING_CHARPOS (it_copy);
4112 if (charpos >= SCHARS (it->string))
4113 break;
4114 bidi_move_to_visually_next (&it_copy.bidi_it);
4116 while (IT_STRING_CHARPOS (it_copy) != IT_STRING_CHARPOS (*it));
4118 RESTORE_IT (it, it, it_copy_data);
4120 else
4122 /* Set charpos to the string position of the character
4123 that comes after IT's current position in the visual
4124 order. */
4125 int n = (it->what == IT_COMPOSITION ? it->cmp_it.nchars : 1);
4127 it_copy = *it;
4128 while (n--)
4129 bidi_move_to_visually_next (&it_copy.bidi_it);
4131 charpos = it_copy.bidi_it.charpos;
4134 eassert (0 <= charpos && charpos <= SCHARS (it->string));
4136 if (it->current.overlay_string_index >= 0)
4137 bufpos = IT_CHARPOS (*it);
4138 else
4139 bufpos = 0;
4141 base_face_id = underlying_face_id (it);
4143 /* Get the face for ASCII, or unibyte. */
4144 face_id = face_at_string_position (it->w,
4145 it->string,
4146 charpos,
4147 bufpos,
4148 &next_check_charpos,
4149 base_face_id, false);
4151 /* Correct the face for charsets different from ASCII. Do it
4152 for the multibyte case only. The face returned above is
4153 suitable for unibyte text if IT->string is unibyte. */
4154 if (STRING_MULTIBYTE (it->string))
4156 struct text_pos pos1 = string_pos (charpos, it->string);
4157 const unsigned char *p = SDATA (it->string) + BYTEPOS (pos1);
4158 int c, len;
4159 struct face *face = FACE_FROM_ID (it->f, face_id);
4161 c = string_char_and_length (p, &len);
4162 face_id = FACE_FOR_CHAR (it->f, face, c, charpos, it->string);
4165 else
4167 struct text_pos pos;
4169 if ((IT_CHARPOS (*it) >= ZV && !before_p)
4170 || (IT_CHARPOS (*it) <= BEGV && before_p))
4171 return it->face_id;
4173 limit = IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT;
4174 pos = it->current.pos;
4176 if (!it->bidi_p)
4178 if (before_p)
4179 DEC_TEXT_POS (pos, it->multibyte_p);
4180 else
4182 if (it->what == IT_COMPOSITION)
4184 /* For composition, we must check the position after
4185 the composition. */
4186 pos.charpos += it->cmp_it.nchars;
4187 pos.bytepos += it->len;
4189 else
4190 INC_TEXT_POS (pos, it->multibyte_p);
4193 else
4195 if (before_p)
4197 int current_x;
4199 /* With bidi iteration, the character before the current
4200 in the visual order cannot be found by simple
4201 iteration, because "reverse" reordering is not
4202 supported. Instead, we need to use the move_it_*
4203 family of functions, and move to the previous
4204 character starting from the beginning of the visual
4205 line. */
4206 /* Ignore face changes before the first visible
4207 character on this display line. */
4208 if (it->current_x <= it->first_visible_x)
4209 return it->face_id;
4210 SAVE_IT (it_copy, *it, it_copy_data);
4211 /* Implementation note: Since move_it_in_display_line
4212 works in the iterator geometry, and thinks the first
4213 character is always the leftmost, even in R2L lines,
4214 we don't need to distinguish between the R2L and L2R
4215 cases here. */
4216 current_x = it_copy.current_x;
4217 move_it_vertically_backward (&it_copy, 0);
4218 move_it_in_display_line (&it_copy, ZV, current_x - 1, MOVE_TO_X);
4219 pos = it_copy.current.pos;
4220 RESTORE_IT (it, it, it_copy_data);
4222 else
4224 /* Set charpos to the buffer position of the character
4225 that comes after IT's current position in the visual
4226 order. */
4227 int n = (it->what == IT_COMPOSITION ? it->cmp_it.nchars : 1);
4229 it_copy = *it;
4230 while (n--)
4231 bidi_move_to_visually_next (&it_copy.bidi_it);
4233 SET_TEXT_POS (pos,
4234 it_copy.bidi_it.charpos, it_copy.bidi_it.bytepos);
4237 eassert (BEGV <= CHARPOS (pos) && CHARPOS (pos) <= ZV);
4239 /* Determine face for CHARSET_ASCII, or unibyte. */
4240 face_id = face_at_buffer_position (it->w,
4241 CHARPOS (pos),
4242 &next_check_charpos,
4243 limit, false, -1);
4245 /* Correct the face for charsets different from ASCII. Do it
4246 for the multibyte case only. The face returned above is
4247 suitable for unibyte text if current_buffer is unibyte. */
4248 if (it->multibyte_p)
4250 int c = FETCH_MULTIBYTE_CHAR (BYTEPOS (pos));
4251 struct face *face = FACE_FROM_ID (it->f, face_id);
4252 face_id = FACE_FOR_CHAR (it->f, face, c, CHARPOS (pos), Qnil);
4256 return face_id;
4261 /***********************************************************************
4262 Invisible text
4263 ***********************************************************************/
4265 /* Set up iterator IT from invisible properties at its current
4266 position. Called from handle_stop. */
4268 static enum prop_handled
4269 handle_invisible_prop (struct it *it)
4271 enum prop_handled handled = HANDLED_NORMALLY;
4272 int invis;
4273 Lisp_Object prop;
4275 if (STRINGP (it->string))
4277 Lisp_Object end_charpos, limit;
4279 /* Get the value of the invisible text property at the
4280 current position. Value will be nil if there is no such
4281 property. */
4282 end_charpos = make_number (IT_STRING_CHARPOS (*it));
4283 prop = Fget_text_property (end_charpos, Qinvisible, it->string);
4284 invis = TEXT_PROP_MEANS_INVISIBLE (prop);
4286 if (invis != 0 && IT_STRING_CHARPOS (*it) < it->end_charpos)
4288 /* Record whether we have to display an ellipsis for the
4289 invisible text. */
4290 bool display_ellipsis_p = (invis == 2);
4291 ptrdiff_t len, endpos;
4293 handled = HANDLED_RECOMPUTE_PROPS;
4295 /* Get the position at which the next visible text can be
4296 found in IT->string, if any. */
4297 endpos = len = SCHARS (it->string);
4298 XSETINT (limit, len);
4301 end_charpos
4302 = Fnext_single_property_change (end_charpos, Qinvisible,
4303 it->string, limit);
4304 /* Since LIMIT is always an integer, so should be the
4305 value returned by Fnext_single_property_change. */
4306 eassert (INTEGERP (end_charpos));
4307 if (INTEGERP (end_charpos))
4309 endpos = XFASTINT (end_charpos);
4310 prop = Fget_text_property (end_charpos, Qinvisible, it->string);
4311 invis = TEXT_PROP_MEANS_INVISIBLE (prop);
4312 if (invis == 2)
4313 display_ellipsis_p = true;
4315 else /* Should never happen; but if it does, exit the loop. */
4316 endpos = len;
4318 while (invis != 0 && endpos < len);
4320 if (display_ellipsis_p)
4321 it->ellipsis_p = true;
4323 if (endpos < len)
4325 /* Text at END_CHARPOS is visible. Move IT there. */
4326 struct text_pos old;
4327 ptrdiff_t oldpos;
4329 old = it->current.string_pos;
4330 oldpos = CHARPOS (old);
4331 if (it->bidi_p)
4333 if (it->bidi_it.first_elt
4334 && it->bidi_it.charpos < SCHARS (it->string))
4335 bidi_paragraph_init (it->paragraph_embedding,
4336 &it->bidi_it, true);
4337 /* Bidi-iterate out of the invisible text. */
4340 bidi_move_to_visually_next (&it->bidi_it);
4342 while (oldpos <= it->bidi_it.charpos
4343 && it->bidi_it.charpos < endpos
4344 && it->bidi_it.charpos < it->bidi_it.string.schars);
4346 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
4347 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
4348 if (IT_CHARPOS (*it) >= endpos)
4349 it->prev_stop = endpos;
4351 else
4353 IT_STRING_CHARPOS (*it) = endpos;
4354 compute_string_pos (&it->current.string_pos, old, it->string);
4357 else
4359 /* The rest of the string is invisible. If this is an
4360 overlay string, proceed with the next overlay string
4361 or whatever comes and return a character from there. */
4362 if (it->current.overlay_string_index >= 0
4363 && !display_ellipsis_p)
4365 next_overlay_string (it);
4366 /* Don't check for overlay strings when we just
4367 finished processing them. */
4368 handled = HANDLED_OVERLAY_STRING_CONSUMED;
4370 else
4372 IT_STRING_CHARPOS (*it) = SCHARS (it->string);
4373 IT_STRING_BYTEPOS (*it) = SBYTES (it->string);
4378 else
4380 ptrdiff_t newpos, next_stop, start_charpos, tem;
4381 Lisp_Object pos, overlay;
4383 /* First of all, is there invisible text at this position? */
4384 tem = start_charpos = IT_CHARPOS (*it);
4385 pos = make_number (tem);
4386 prop = get_char_property_and_overlay (pos, Qinvisible, it->window,
4387 &overlay);
4388 invis = TEXT_PROP_MEANS_INVISIBLE (prop);
4390 /* If we are on invisible text, skip over it. */
4391 if (invis != 0 && start_charpos < it->end_charpos)
4393 /* Record whether we have to display an ellipsis for the
4394 invisible text. */
4395 bool display_ellipsis_p = invis == 2;
4397 handled = HANDLED_RECOMPUTE_PROPS;
4399 /* Loop skipping over invisible text. The loop is left at
4400 ZV or with IT on the first char being visible again. */
4403 /* Try to skip some invisible text. Return value is the
4404 position reached which can be equal to where we start
4405 if there is nothing invisible there. This skips both
4406 over invisible text properties and overlays with
4407 invisible property. */
4408 newpos = skip_invisible (tem, &next_stop, ZV, it->window);
4410 /* If we skipped nothing at all we weren't at invisible
4411 text in the first place. If everything to the end of
4412 the buffer was skipped, end the loop. */
4413 if (newpos == tem || newpos >= ZV)
4414 invis = 0;
4415 else
4417 /* We skipped some characters but not necessarily
4418 all there are. Check if we ended up on visible
4419 text. Fget_char_property returns the property of
4420 the char before the given position, i.e. if we
4421 get invis = 0, this means that the char at
4422 newpos is visible. */
4423 pos = make_number (newpos);
4424 prop = Fget_char_property (pos, Qinvisible, it->window);
4425 invis = TEXT_PROP_MEANS_INVISIBLE (prop);
4428 /* If we ended up on invisible text, proceed to
4429 skip starting with next_stop. */
4430 if (invis != 0)
4431 tem = next_stop;
4433 /* If there are adjacent invisible texts, don't lose the
4434 second one's ellipsis. */
4435 if (invis == 2)
4436 display_ellipsis_p = true;
4438 while (invis != 0);
4440 /* The position newpos is now either ZV or on visible text. */
4441 if (it->bidi_p)
4443 ptrdiff_t bpos = CHAR_TO_BYTE (newpos);
4444 bool on_newline
4445 = bpos == ZV_BYTE || FETCH_BYTE (bpos) == '\n';
4446 bool after_newline
4447 = newpos <= BEGV || FETCH_BYTE (bpos - 1) == '\n';
4449 /* If the invisible text ends on a newline or on a
4450 character after a newline, we can avoid the costly,
4451 character by character, bidi iteration to NEWPOS, and
4452 instead simply reseat the iterator there. That's
4453 because all bidi reordering information is tossed at
4454 the newline. This is a big win for modes that hide
4455 complete lines, like Outline, Org, etc. */
4456 if (on_newline || after_newline)
4458 struct text_pos tpos;
4459 bidi_dir_t pdir = it->bidi_it.paragraph_dir;
4461 SET_TEXT_POS (tpos, newpos, bpos);
4462 reseat_1 (it, tpos, false);
4463 /* If we reseat on a newline/ZV, we need to prep the
4464 bidi iterator for advancing to the next character
4465 after the newline/EOB, keeping the current paragraph
4466 direction (so that PRODUCE_GLYPHS does TRT wrt
4467 prepending/appending glyphs to a glyph row). */
4468 if (on_newline)
4470 it->bidi_it.first_elt = false;
4471 it->bidi_it.paragraph_dir = pdir;
4472 it->bidi_it.ch = (bpos == ZV_BYTE) ? -1 : '\n';
4473 it->bidi_it.nchars = 1;
4474 it->bidi_it.ch_len = 1;
4477 else /* Must use the slow method. */
4479 /* With bidi iteration, the region of invisible text
4480 could start and/or end in the middle of a
4481 non-base embedding level. Therefore, we need to
4482 skip invisible text using the bidi iterator,
4483 starting at IT's current position, until we find
4484 ourselves outside of the invisible text.
4485 Skipping invisible text _after_ bidi iteration
4486 avoids affecting the visual order of the
4487 displayed text when invisible properties are
4488 added or removed. */
4489 if (it->bidi_it.first_elt && it->bidi_it.charpos < ZV)
4491 /* If we were `reseat'ed to a new paragraph,
4492 determine the paragraph base direction. We
4493 need to do it now because
4494 next_element_from_buffer may not have a
4495 chance to do it, if we are going to skip any
4496 text at the beginning, which resets the
4497 FIRST_ELT flag. */
4498 bidi_paragraph_init (it->paragraph_embedding,
4499 &it->bidi_it, true);
4503 bidi_move_to_visually_next (&it->bidi_it);
4505 while (it->stop_charpos <= it->bidi_it.charpos
4506 && it->bidi_it.charpos < newpos);
4507 IT_CHARPOS (*it) = it->bidi_it.charpos;
4508 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
4509 /* If we overstepped NEWPOS, record its position in
4510 the iterator, so that we skip invisible text if
4511 later the bidi iteration lands us in the
4512 invisible region again. */
4513 if (IT_CHARPOS (*it) >= newpos)
4514 it->prev_stop = newpos;
4517 else
4519 IT_CHARPOS (*it) = newpos;
4520 IT_BYTEPOS (*it) = CHAR_TO_BYTE (newpos);
4523 if (display_ellipsis_p)
4525 /* Make sure that the glyphs of the ellipsis will get
4526 correct `charpos' values. If we would not update
4527 it->position here, the glyphs would belong to the
4528 last visible character _before_ the invisible
4529 text, which confuses `set_cursor_from_row'.
4531 We use the last invisible position instead of the
4532 first because this way the cursor is always drawn on
4533 the first "." of the ellipsis, whenever PT is inside
4534 the invisible text. Otherwise the cursor would be
4535 placed _after_ the ellipsis when the point is after the
4536 first invisible character. */
4537 if (!STRINGP (it->object))
4539 it->position.charpos = newpos - 1;
4540 it->position.bytepos = CHAR_TO_BYTE (it->position.charpos);
4544 /* If there are before-strings at the start of invisible
4545 text, and the text is invisible because of a text
4546 property, arrange to show before-strings because 20.x did
4547 it that way. (If the text is invisible because of an
4548 overlay property instead of a text property, this is
4549 already handled in the overlay code.) */
4550 if (NILP (overlay)
4551 && get_overlay_strings (it, it->stop_charpos))
4553 handled = HANDLED_RECOMPUTE_PROPS;
4554 if (it->sp > 0)
4556 it->stack[it->sp - 1].display_ellipsis_p = display_ellipsis_p;
4557 /* The call to get_overlay_strings above recomputes
4558 it->stop_charpos, but it only considers changes
4559 in properties and overlays beyond iterator's
4560 current position. This causes us to miss changes
4561 that happen exactly where the invisible property
4562 ended. So we play it safe here and force the
4563 iterator to check for potential stop positions
4564 immediately after the invisible text. Note that
4565 if get_overlay_strings returns true, it
4566 normally also pushed the iterator stack, so we
4567 need to update the stop position in the slot
4568 below the current one. */
4569 it->stack[it->sp - 1].stop_charpos
4570 = CHARPOS (it->stack[it->sp - 1].current.pos);
4573 else if (display_ellipsis_p)
4575 it->ellipsis_p = true;
4576 /* Let the ellipsis display before
4577 considering any properties of the following char.
4578 Fixes jasonr@gnu.org 01 Oct 07 bug. */
4579 handled = HANDLED_RETURN;
4584 return handled;
4588 /* Make iterator IT return `...' next.
4589 Replaces LEN characters from buffer. */
4591 static void
4592 setup_for_ellipsis (struct it *it, int len)
4594 /* Use the display table definition for `...'. Invalid glyphs
4595 will be handled by the method returning elements from dpvec. */
4596 if (it->dp && VECTORP (DISP_INVIS_VECTOR (it->dp)))
4598 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
4599 it->dpvec = v->contents;
4600 it->dpend = v->contents + v->header.size;
4602 else
4604 /* Default `...'. */
4605 it->dpvec = default_invis_vector;
4606 it->dpend = default_invis_vector + 3;
4609 it->dpvec_char_len = len;
4610 it->current.dpvec_index = 0;
4611 it->dpvec_face_id = -1;
4613 /* Use IT->saved_face_id for the ellipsis, so that it has the same
4614 face as the preceding text. IT->saved_face_id was set in
4615 handle_stop to the face of the preceding character, and will be
4616 different from IT->face_id only if the invisible text skipped in
4617 handle_invisible_prop has some non-default face on its first
4618 character. We thus ignore the face of the invisible text when we
4619 display the ellipsis. IT's face is restored in set_iterator_to_next. */
4620 if (it->saved_face_id >= 0)
4621 it->face_id = it->saved_face_id;
4623 /* If the ellipsis represents buffer text, it means we advanced in
4624 the buffer, so we should no longer ignore overlay strings. */
4625 if (it->method == GET_FROM_BUFFER)
4626 it->ignore_overlay_strings_at_pos_p = false;
4628 it->method = GET_FROM_DISPLAY_VECTOR;
4629 it->ellipsis_p = true;
4634 /***********************************************************************
4635 'display' property
4636 ***********************************************************************/
4638 /* Set up iterator IT from `display' property at its current position.
4639 Called from handle_stop.
4640 We return HANDLED_RETURN if some part of the display property
4641 overrides the display of the buffer text itself.
4642 Otherwise we return HANDLED_NORMALLY. */
4644 static enum prop_handled
4645 handle_display_prop (struct it *it)
4647 Lisp_Object propval, object, overlay;
4648 struct text_pos *position;
4649 ptrdiff_t bufpos;
4650 /* Nonzero if some property replaces the display of the text itself. */
4651 int display_replaced = 0;
4653 if (STRINGP (it->string))
4655 object = it->string;
4656 position = &it->current.string_pos;
4657 bufpos = CHARPOS (it->current.pos);
4659 else
4661 XSETWINDOW (object, it->w);
4662 position = &it->current.pos;
4663 bufpos = CHARPOS (*position);
4666 /* Reset those iterator values set from display property values. */
4667 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
4668 it->space_width = Qnil;
4669 it->font_height = Qnil;
4670 it->voffset = 0;
4672 /* We don't support recursive `display' properties, i.e. string
4673 values that have a string `display' property, that have a string
4674 `display' property etc. */
4675 if (!it->string_from_display_prop_p)
4676 it->area = TEXT_AREA;
4678 propval = get_char_property_and_overlay (make_number (position->charpos),
4679 Qdisplay, object, &overlay);
4680 if (NILP (propval))
4681 return HANDLED_NORMALLY;
4682 /* Now OVERLAY is the overlay that gave us this property, or nil
4683 if it was a text property. */
4685 if (!STRINGP (it->string))
4686 object = it->w->contents;
4688 display_replaced = handle_display_spec (it, propval, object, overlay,
4689 position, bufpos,
4690 FRAME_WINDOW_P (it->f));
4691 return display_replaced != 0 ? HANDLED_RETURN : HANDLED_NORMALLY;
4694 /* Subroutine of handle_display_prop. Returns non-zero if the display
4695 specification in SPEC is a replacing specification, i.e. it would
4696 replace the text covered by `display' property with something else,
4697 such as an image or a display string. If SPEC includes any kind or
4698 `(space ...) specification, the value is 2; this is used by
4699 compute_display_string_pos, which see.
4701 See handle_single_display_spec for documentation of arguments.
4702 FRAME_WINDOW_P is true if the window being redisplayed is on a
4703 GUI frame; this argument is used only if IT is NULL, see below.
4705 IT can be NULL, if this is called by the bidi reordering code
4706 through compute_display_string_pos, which see. In that case, this
4707 function only examines SPEC, but does not otherwise "handle" it, in
4708 the sense that it doesn't set up members of IT from the display
4709 spec. */
4710 static int
4711 handle_display_spec (struct it *it, Lisp_Object spec, Lisp_Object object,
4712 Lisp_Object overlay, struct text_pos *position,
4713 ptrdiff_t bufpos, bool frame_window_p)
4715 int replacing = 0;
4717 if (CONSP (spec)
4718 /* Simple specifications. */
4719 && !EQ (XCAR (spec), Qimage)
4720 #ifdef HAVE_XWIDGETS
4721 && !EQ (XCAR (spec), Qxwidget)
4722 #endif
4723 && !EQ (XCAR (spec), Qspace)
4724 && !EQ (XCAR (spec), Qwhen)
4725 && !EQ (XCAR (spec), Qslice)
4726 && !EQ (XCAR (spec), Qspace_width)
4727 && !EQ (XCAR (spec), Qheight)
4728 && !EQ (XCAR (spec), Qraise)
4729 /* Marginal area specifications. */
4730 && !(CONSP (XCAR (spec)) && EQ (XCAR (XCAR (spec)), Qmargin))
4731 && !EQ (XCAR (spec), Qleft_fringe)
4732 && !EQ (XCAR (spec), Qright_fringe)
4733 && !NILP (XCAR (spec)))
4735 for (; CONSP (spec); spec = XCDR (spec))
4737 int rv = handle_single_display_spec (it, XCAR (spec), object,
4738 overlay, position, bufpos,
4739 replacing, frame_window_p);
4740 if (rv != 0)
4742 replacing = rv;
4743 /* If some text in a string is replaced, `position' no
4744 longer points to the position of `object'. */
4745 if (!it || STRINGP (object))
4746 break;
4750 else if (VECTORP (spec))
4752 ptrdiff_t i;
4753 for (i = 0; i < ASIZE (spec); ++i)
4755 int rv = handle_single_display_spec (it, AREF (spec, i), object,
4756 overlay, position, bufpos,
4757 replacing, frame_window_p);
4758 if (rv != 0)
4760 replacing = rv;
4761 /* If some text in a string is replaced, `position' no
4762 longer points to the position of `object'. */
4763 if (!it || STRINGP (object))
4764 break;
4768 else
4769 replacing = handle_single_display_spec (it, spec, object, overlay, position,
4770 bufpos, 0, frame_window_p);
4771 return replacing;
4774 /* Value is the position of the end of the `display' property starting
4775 at START_POS in OBJECT. */
4777 static struct text_pos
4778 display_prop_end (struct it *it, Lisp_Object object, struct text_pos start_pos)
4780 Lisp_Object end;
4781 struct text_pos end_pos;
4783 end = Fnext_single_char_property_change (make_number (CHARPOS (start_pos)),
4784 Qdisplay, object, Qnil);
4785 CHARPOS (end_pos) = XFASTINT (end);
4786 if (STRINGP (object))
4787 compute_string_pos (&end_pos, start_pos, it->string);
4788 else
4789 BYTEPOS (end_pos) = CHAR_TO_BYTE (XFASTINT (end));
4791 return end_pos;
4795 /* Set up IT from a single `display' property specification SPEC. OBJECT
4796 is the object in which the `display' property was found. *POSITION
4797 is the position in OBJECT at which the `display' property was found.
4798 BUFPOS is the buffer position of OBJECT (different from POSITION if
4799 OBJECT is not a buffer). DISPLAY_REPLACED non-zero means that we
4800 previously saw a display specification which already replaced text
4801 display with something else, for example an image; we ignore such
4802 properties after the first one has been processed.
4804 OVERLAY is the overlay this `display' property came from,
4805 or nil if it was a text property.
4807 If SPEC is a `space' or `image' specification, and in some other
4808 cases too, set *POSITION to the position where the `display'
4809 property ends.
4811 If IT is NULL, only examine the property specification in SPEC, but
4812 don't set up IT. In that case, FRAME_WINDOW_P means SPEC
4813 is intended to be displayed in a window on a GUI frame.
4815 Value is non-zero if something was found which replaces the display
4816 of buffer or string text. */
4818 static int
4819 handle_single_display_spec (struct it *it, Lisp_Object spec, Lisp_Object object,
4820 Lisp_Object overlay, struct text_pos *position,
4821 ptrdiff_t bufpos, int display_replaced,
4822 bool frame_window_p)
4824 Lisp_Object form;
4825 Lisp_Object location, value;
4826 struct text_pos start_pos = *position;
4828 /* If SPEC is a list of the form `(when FORM . VALUE)', evaluate FORM.
4829 If the result is non-nil, use VALUE instead of SPEC. */
4830 form = Qt;
4831 if (CONSP (spec) && EQ (XCAR (spec), Qwhen))
4833 spec = XCDR (spec);
4834 if (!CONSP (spec))
4835 return 0;
4836 form = XCAR (spec);
4837 spec = XCDR (spec);
4840 if (!NILP (form) && !EQ (form, Qt))
4842 ptrdiff_t count = SPECPDL_INDEX ();
4844 /* Bind `object' to the object having the `display' property, a
4845 buffer or string. Bind `position' to the position in the
4846 object where the property was found, and `buffer-position'
4847 to the current position in the buffer. */
4849 if (NILP (object))
4850 XSETBUFFER (object, current_buffer);
4851 specbind (Qobject, object);
4852 specbind (Qposition, make_number (CHARPOS (*position)));
4853 specbind (Qbuffer_position, make_number (bufpos));
4854 form = safe_eval (form);
4855 unbind_to (count, Qnil);
4858 if (NILP (form))
4859 return 0;
4861 /* Handle `(height HEIGHT)' specifications. */
4862 if (CONSP (spec)
4863 && EQ (XCAR (spec), Qheight)
4864 && CONSP (XCDR (spec)))
4866 if (it)
4868 if (!FRAME_WINDOW_P (it->f))
4869 return 0;
4871 it->font_height = XCAR (XCDR (spec));
4872 if (!NILP (it->font_height))
4874 int new_height = -1;
4876 if (CONSP (it->font_height)
4877 && (EQ (XCAR (it->font_height), Qplus)
4878 || EQ (XCAR (it->font_height), Qminus))
4879 && CONSP (XCDR (it->font_height))
4880 && RANGED_INTEGERP (0, XCAR (XCDR (it->font_height)), INT_MAX))
4882 /* `(+ N)' or `(- N)' where N is an integer. */
4883 int steps = XINT (XCAR (XCDR (it->font_height)));
4884 if (EQ (XCAR (it->font_height), Qplus))
4885 steps = - steps;
4886 it->face_id = smaller_face (it->f, it->face_id, steps);
4888 else if (FUNCTIONP (it->font_height))
4890 /* Call function with current height as argument.
4891 Value is the new height. */
4892 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4893 Lisp_Object height;
4894 height = safe_call1 (it->font_height,
4895 face->lface[LFACE_HEIGHT_INDEX]);
4896 if (NUMBERP (height))
4897 new_height = XFLOATINT (height);
4899 else if (NUMBERP (it->font_height))
4901 /* Value is a multiple of the canonical char height. */
4902 struct face *f;
4904 f = FACE_FROM_ID (it->f,
4905 lookup_basic_face (it->f, DEFAULT_FACE_ID));
4906 new_height = (XFLOATINT (it->font_height)
4907 * XINT (f->lface[LFACE_HEIGHT_INDEX]));
4909 else
4911 /* Evaluate IT->font_height with `height' bound to the
4912 current specified height to get the new height. */
4913 ptrdiff_t count = SPECPDL_INDEX ();
4914 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4916 specbind (Qheight, face->lface[LFACE_HEIGHT_INDEX]);
4917 value = safe_eval (it->font_height);
4918 unbind_to (count, Qnil);
4920 if (NUMBERP (value))
4921 new_height = XFLOATINT (value);
4924 if (new_height > 0)
4925 it->face_id = face_with_height (it->f, it->face_id, new_height);
4929 return 0;
4932 /* Handle `(space-width WIDTH)'. */
4933 if (CONSP (spec)
4934 && EQ (XCAR (spec), Qspace_width)
4935 && CONSP (XCDR (spec)))
4937 if (it)
4939 if (!FRAME_WINDOW_P (it->f))
4940 return 0;
4942 value = XCAR (XCDR (spec));
4943 if (NUMBERP (value) && XFLOATINT (value) > 0)
4944 it->space_width = value;
4947 return 0;
4950 /* Handle `(slice X Y WIDTH HEIGHT)'. */
4951 if (CONSP (spec)
4952 && EQ (XCAR (spec), Qslice))
4954 Lisp_Object tem;
4956 if (it)
4958 if (!FRAME_WINDOW_P (it->f))
4959 return 0;
4961 if (tem = XCDR (spec), CONSP (tem))
4963 it->slice.x = XCAR (tem);
4964 if (tem = XCDR (tem), CONSP (tem))
4966 it->slice.y = XCAR (tem);
4967 if (tem = XCDR (tem), CONSP (tem))
4969 it->slice.width = XCAR (tem);
4970 if (tem = XCDR (tem), CONSP (tem))
4971 it->slice.height = XCAR (tem);
4977 return 0;
4980 /* Handle `(raise FACTOR)'. */
4981 if (CONSP (spec)
4982 && EQ (XCAR (spec), Qraise)
4983 && CONSP (XCDR (spec)))
4985 if (it)
4987 if (!FRAME_WINDOW_P (it->f))
4988 return 0;
4990 #ifdef HAVE_WINDOW_SYSTEM
4991 value = XCAR (XCDR (spec));
4992 if (NUMBERP (value))
4994 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4995 it->voffset = - (XFLOATINT (value)
4996 * (normal_char_height (face->font, -1)));
4998 #endif /* HAVE_WINDOW_SYSTEM */
5001 return 0;
5004 /* Don't handle the other kinds of display specifications
5005 inside a string that we got from a `display' property. */
5006 if (it && it->string_from_display_prop_p)
5007 return 0;
5009 /* Characters having this form of property are not displayed, so
5010 we have to find the end of the property. */
5011 if (it)
5013 start_pos = *position;
5014 *position = display_prop_end (it, object, start_pos);
5015 /* If the display property comes from an overlay, don't consider
5016 any potential stop_charpos values before the end of that
5017 overlay. Since display_prop_end will happily find another
5018 'display' property coming from some other overlay or text
5019 property on buffer positions before this overlay's end, we
5020 need to ignore them, or else we risk displaying this
5021 overlay's display string/image twice. */
5022 if (!NILP (overlay))
5024 ptrdiff_t ovendpos = OVERLAY_POSITION (OVERLAY_END (overlay));
5026 /* Some borderline-sane Lisp might call us with the current
5027 buffer narrowed so that overlay-end is outside the
5028 POINT_MIN..POINT_MAX region, which will then cause
5029 various assertion violations and crashes down the road,
5030 starting with pop_it when it will attempt to use POSITION
5031 set below. Prevent that. */
5032 ovendpos = clip_to_bounds (BEGV, ovendpos, ZV);
5034 if (ovendpos > CHARPOS (*position))
5035 SET_TEXT_POS (*position, ovendpos, CHAR_TO_BYTE (ovendpos));
5038 value = Qnil;
5040 /* Stop the scan at that end position--we assume that all
5041 text properties change there. */
5042 if (it)
5043 it->stop_charpos = position->charpos;
5045 /* Handle `(left-fringe BITMAP [FACE])'
5046 and `(right-fringe BITMAP [FACE])'. */
5047 if (CONSP (spec)
5048 && (EQ (XCAR (spec), Qleft_fringe)
5049 || EQ (XCAR (spec), Qright_fringe))
5050 && CONSP (XCDR (spec)))
5052 if (it)
5054 if (!FRAME_WINDOW_P (it->f))
5055 /* If we return here, POSITION has been advanced
5056 across the text with this property. */
5058 /* Synchronize the bidi iterator with POSITION. This is
5059 needed because we are not going to push the iterator
5060 on behalf of this display property, so there will be
5061 no pop_it call to do this synchronization for us. */
5062 if (it->bidi_p)
5064 it->position = *position;
5065 iterate_out_of_display_property (it);
5066 *position = it->position;
5068 return 1;
5071 else if (!frame_window_p)
5072 return 1;
5074 #ifdef HAVE_WINDOW_SYSTEM
5075 value = XCAR (XCDR (spec));
5076 int fringe_bitmap = SYMBOLP (value) ? lookup_fringe_bitmap (value) : 0;
5077 if (! fringe_bitmap)
5078 /* If we return here, POSITION has been advanced
5079 across the text with this property. */
5081 if (it && it->bidi_p)
5083 it->position = *position;
5084 iterate_out_of_display_property (it);
5085 *position = it->position;
5087 return 1;
5090 if (it)
5092 int face_id = lookup_basic_face (it->f, DEFAULT_FACE_ID);
5094 if (CONSP (XCDR (XCDR (spec))))
5096 Lisp_Object face_name = XCAR (XCDR (XCDR (spec)));
5097 int face_id2 = lookup_derived_face (it->f, face_name,
5098 FRINGE_FACE_ID, false);
5099 if (face_id2 >= 0)
5100 face_id = face_id2;
5103 /* Save current settings of IT so that we can restore them
5104 when we are finished with the glyph property value. */
5105 push_it (it, position);
5107 it->area = TEXT_AREA;
5108 it->what = IT_IMAGE;
5109 it->image_id = -1; /* no image */
5110 it->position = start_pos;
5111 it->object = NILP (object) ? it->w->contents : object;
5112 it->method = GET_FROM_IMAGE;
5113 it->from_overlay = Qnil;
5114 it->face_id = face_id;
5115 it->from_disp_prop_p = true;
5117 /* Say that we haven't consumed the characters with
5118 `display' property yet. The call to pop_it in
5119 set_iterator_to_next will clean this up. */
5120 *position = start_pos;
5122 if (EQ (XCAR (spec), Qleft_fringe))
5124 it->left_user_fringe_bitmap = fringe_bitmap;
5125 it->left_user_fringe_face_id = face_id;
5127 else
5129 it->right_user_fringe_bitmap = fringe_bitmap;
5130 it->right_user_fringe_face_id = face_id;
5133 #endif /* HAVE_WINDOW_SYSTEM */
5134 return 1;
5137 /* Prepare to handle `((margin left-margin) ...)',
5138 `((margin right-margin) ...)' and `((margin nil) ...)'
5139 prefixes for display specifications. */
5140 location = Qunbound;
5141 if (CONSP (spec) && CONSP (XCAR (spec)))
5143 Lisp_Object tem;
5145 value = XCDR (spec);
5146 if (CONSP (value))
5147 value = XCAR (value);
5149 tem = XCAR (spec);
5150 if (EQ (XCAR (tem), Qmargin)
5151 && (tem = XCDR (tem),
5152 tem = CONSP (tem) ? XCAR (tem) : Qnil,
5153 (NILP (tem)
5154 || EQ (tem, Qleft_margin)
5155 || EQ (tem, Qright_margin))))
5156 location = tem;
5159 if (EQ (location, Qunbound))
5161 location = Qnil;
5162 value = spec;
5165 /* After this point, VALUE is the property after any
5166 margin prefix has been stripped. It must be a string,
5167 an image specification, or `(space ...)'.
5169 LOCATION specifies where to display: `left-margin',
5170 `right-margin' or nil. */
5172 bool valid_p = (STRINGP (value)
5173 #ifdef HAVE_WINDOW_SYSTEM
5174 || ((it ? FRAME_WINDOW_P (it->f) : frame_window_p)
5175 && valid_image_p (value))
5176 #endif /* not HAVE_WINDOW_SYSTEM */
5177 || (CONSP (value) && EQ (XCAR (value), Qspace))
5178 || ((it ? FRAME_WINDOW_P (it->f) : frame_window_p)
5179 && valid_xwidget_spec_p (value)));
5181 if (valid_p && display_replaced == 0)
5183 int retval = 1;
5185 if (!it)
5187 /* Callers need to know whether the display spec is any kind
5188 of `(space ...)' spec that is about to affect text-area
5189 display. */
5190 if (CONSP (value) && EQ (XCAR (value), Qspace) && NILP (location))
5191 retval = 2;
5192 return retval;
5195 /* Save current settings of IT so that we can restore them
5196 when we are finished with the glyph property value. */
5197 push_it (it, position);
5198 it->from_overlay = overlay;
5199 it->from_disp_prop_p = true;
5201 if (NILP (location))
5202 it->area = TEXT_AREA;
5203 else if (EQ (location, Qleft_margin))
5204 it->area = LEFT_MARGIN_AREA;
5205 else
5206 it->area = RIGHT_MARGIN_AREA;
5208 if (STRINGP (value))
5210 it->string = value;
5211 it->multibyte_p = STRING_MULTIBYTE (it->string);
5212 it->current.overlay_string_index = -1;
5213 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
5214 it->end_charpos = it->string_nchars = SCHARS (it->string);
5215 it->method = GET_FROM_STRING;
5216 it->stop_charpos = 0;
5217 it->prev_stop = 0;
5218 it->base_level_stop = 0;
5219 it->string_from_display_prop_p = true;
5220 /* Say that we haven't consumed the characters with
5221 `display' property yet. The call to pop_it in
5222 set_iterator_to_next will clean this up. */
5223 if (BUFFERP (object))
5224 *position = start_pos;
5226 /* Force paragraph direction to be that of the parent
5227 object. If the parent object's paragraph direction is
5228 not yet determined, default to L2R. */
5229 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
5230 it->paragraph_embedding = it->bidi_it.paragraph_dir;
5231 else
5232 it->paragraph_embedding = L2R;
5234 /* Set up the bidi iterator for this display string. */
5235 if (it->bidi_p)
5237 it->bidi_it.string.lstring = it->string;
5238 it->bidi_it.string.s = NULL;
5239 it->bidi_it.string.schars = it->end_charpos;
5240 it->bidi_it.string.bufpos = bufpos;
5241 it->bidi_it.string.from_disp_str = true;
5242 it->bidi_it.string.unibyte = !it->multibyte_p;
5243 it->bidi_it.w = it->w;
5244 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
5247 else if (CONSP (value) && EQ (XCAR (value), Qspace))
5249 it->method = GET_FROM_STRETCH;
5250 it->object = value;
5251 *position = it->position = start_pos;
5252 retval = 1 + (it->area == TEXT_AREA);
5254 else if (valid_xwidget_spec_p (value))
5256 it->what = IT_XWIDGET;
5257 it->method = GET_FROM_XWIDGET;
5258 it->position = start_pos;
5259 it->object = NILP (object) ? it->w->contents : object;
5260 *position = start_pos;
5261 it->xwidget = lookup_xwidget (value);
5263 #ifdef HAVE_WINDOW_SYSTEM
5264 else
5266 it->what = IT_IMAGE;
5267 it->image_id = lookup_image (it->f, value);
5268 it->position = start_pos;
5269 it->object = NILP (object) ? it->w->contents : object;
5270 it->method = GET_FROM_IMAGE;
5272 /* Say that we haven't consumed the characters with
5273 `display' property yet. The call to pop_it in
5274 set_iterator_to_next will clean this up. */
5275 *position = start_pos;
5277 #endif /* HAVE_WINDOW_SYSTEM */
5279 return retval;
5282 /* Invalid property or property not supported. Restore
5283 POSITION to what it was before. */
5284 *position = start_pos;
5285 return 0;
5288 /* Check if PROP is a display property value whose text should be
5289 treated as intangible. OVERLAY is the overlay from which PROP
5290 came, or nil if it came from a text property. CHARPOS and BYTEPOS
5291 specify the buffer position covered by PROP. */
5293 bool
5294 display_prop_intangible_p (Lisp_Object prop, Lisp_Object overlay,
5295 ptrdiff_t charpos, ptrdiff_t bytepos)
5297 bool frame_window_p = FRAME_WINDOW_P (XFRAME (selected_frame));
5298 struct text_pos position;
5300 SET_TEXT_POS (position, charpos, bytepos);
5301 return (handle_display_spec (NULL, prop, Qnil, overlay,
5302 &position, charpos, frame_window_p)
5303 != 0);
5307 /* Return true if PROP is a display sub-property value containing STRING.
5309 Implementation note: this and the following function are really
5310 special cases of handle_display_spec and
5311 handle_single_display_spec, and should ideally use the same code.
5312 Until they do, these two pairs must be consistent and must be
5313 modified in sync. */
5315 static bool
5316 single_display_spec_string_p (Lisp_Object prop, Lisp_Object string)
5318 if (EQ (string, prop))
5319 return true;
5321 /* Skip over `when FORM'. */
5322 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
5324 prop = XCDR (prop);
5325 if (!CONSP (prop))
5326 return false;
5327 /* Actually, the condition following `when' should be eval'ed,
5328 like handle_single_display_spec does, and we should return
5329 false if it evaluates to nil. However, this function is
5330 called only when the buffer was already displayed and some
5331 glyph in the glyph matrix was found to come from a display
5332 string. Therefore, the condition was already evaluated, and
5333 the result was non-nil, otherwise the display string wouldn't
5334 have been displayed and we would have never been called for
5335 this property. Thus, we can skip the evaluation and assume
5336 its result is non-nil. */
5337 prop = XCDR (prop);
5340 if (CONSP (prop))
5341 /* Skip over `margin LOCATION'. */
5342 if (EQ (XCAR (prop), Qmargin))
5344 prop = XCDR (prop);
5345 if (!CONSP (prop))
5346 return false;
5348 prop = XCDR (prop);
5349 if (!CONSP (prop))
5350 return false;
5353 return EQ (prop, string) || (CONSP (prop) && EQ (XCAR (prop), string));
5357 /* Return true if STRING appears in the `display' property PROP. */
5359 static bool
5360 display_prop_string_p (Lisp_Object prop, Lisp_Object string)
5362 if (CONSP (prop)
5363 && !EQ (XCAR (prop), Qwhen)
5364 && !(CONSP (XCAR (prop)) && EQ (Qmargin, XCAR (XCAR (prop)))))
5366 /* A list of sub-properties. */
5367 while (CONSP (prop))
5369 if (single_display_spec_string_p (XCAR (prop), string))
5370 return true;
5371 prop = XCDR (prop);
5374 else if (VECTORP (prop))
5376 /* A vector of sub-properties. */
5377 ptrdiff_t i;
5378 for (i = 0; i < ASIZE (prop); ++i)
5379 if (single_display_spec_string_p (AREF (prop, i), string))
5380 return true;
5382 else
5383 return single_display_spec_string_p (prop, string);
5385 return false;
5388 /* Look for STRING in overlays and text properties in the current
5389 buffer, between character positions FROM and TO (excluding TO).
5390 BACK_P means look back (in this case, TO is supposed to be
5391 less than FROM).
5392 Value is the first character position where STRING was found, or
5393 zero if it wasn't found before hitting TO.
5395 This function may only use code that doesn't eval because it is
5396 called asynchronously from note_mouse_highlight. */
5398 static ptrdiff_t
5399 string_buffer_position_lim (Lisp_Object string,
5400 ptrdiff_t from, ptrdiff_t to, bool back_p)
5402 Lisp_Object limit, prop, pos;
5403 bool found = false;
5405 pos = make_number (max (from, BEGV));
5407 if (!back_p) /* looking forward */
5409 limit = make_number (min (to, ZV));
5410 while (!found && !EQ (pos, limit))
5412 prop = Fget_char_property (pos, Qdisplay, Qnil);
5413 if (!NILP (prop) && display_prop_string_p (prop, string))
5414 found = true;
5415 else
5416 pos = Fnext_single_char_property_change (pos, Qdisplay, Qnil,
5417 limit);
5420 else /* looking back */
5422 limit = make_number (max (to, BEGV));
5423 while (!found && !EQ (pos, limit))
5425 prop = Fget_char_property (pos, Qdisplay, Qnil);
5426 if (!NILP (prop) && display_prop_string_p (prop, string))
5427 found = true;
5428 else
5429 pos = Fprevious_single_char_property_change (pos, Qdisplay, Qnil,
5430 limit);
5434 return found ? XINT (pos) : 0;
5437 /* Determine which buffer position in current buffer STRING comes from.
5438 AROUND_CHARPOS is an approximate position where it could come from.
5439 Value is the buffer position or 0 if it couldn't be determined.
5441 This function is necessary because we don't record buffer positions
5442 in glyphs generated from strings (to keep struct glyph small).
5443 This function may only use code that doesn't eval because it is
5444 called asynchronously from note_mouse_highlight. */
5446 static ptrdiff_t
5447 string_buffer_position (Lisp_Object string, ptrdiff_t around_charpos)
5449 const int MAX_DISTANCE = 1000;
5450 ptrdiff_t found = string_buffer_position_lim (string, around_charpos,
5451 around_charpos + MAX_DISTANCE,
5452 false);
5454 if (!found)
5455 found = string_buffer_position_lim (string, around_charpos,
5456 around_charpos - MAX_DISTANCE, true);
5457 return found;
5462 /***********************************************************************
5463 `composition' property
5464 ***********************************************************************/
5466 /* Set up iterator IT from `composition' property at its current
5467 position. Called from handle_stop. */
5469 static enum prop_handled
5470 handle_composition_prop (struct it *it)
5472 Lisp_Object prop, string;
5473 ptrdiff_t pos, pos_byte, start, end;
5475 if (STRINGP (it->string))
5477 unsigned char *s;
5479 pos = IT_STRING_CHARPOS (*it);
5480 pos_byte = IT_STRING_BYTEPOS (*it);
5481 string = it->string;
5482 s = SDATA (string) + pos_byte;
5483 it->c = STRING_CHAR (s);
5485 else
5487 pos = IT_CHARPOS (*it);
5488 pos_byte = IT_BYTEPOS (*it);
5489 string = Qnil;
5490 it->c = FETCH_CHAR (pos_byte);
5493 /* If there's a valid composition and point is not inside of the
5494 composition (in the case that the composition is from the current
5495 buffer), draw a glyph composed from the composition components. */
5496 if (find_composition (pos, -1, &start, &end, &prop, string)
5497 && composition_valid_p (start, end, prop)
5498 && (STRINGP (it->string) || (PT <= start || PT >= end)))
5500 if (start < pos)
5501 /* As we can't handle this situation (perhaps font-lock added
5502 a new composition), we just return here hoping that next
5503 redisplay will detect this composition much earlier. */
5504 return HANDLED_NORMALLY;
5505 if (start != pos)
5507 if (STRINGP (it->string))
5508 pos_byte = string_char_to_byte (it->string, start);
5509 else
5510 pos_byte = CHAR_TO_BYTE (start);
5512 it->cmp_it.id = get_composition_id (start, pos_byte, end - start,
5513 prop, string);
5515 if (it->cmp_it.id >= 0)
5517 it->cmp_it.ch = -1;
5518 it->cmp_it.nchars = COMPOSITION_LENGTH (prop);
5519 it->cmp_it.nglyphs = -1;
5523 return HANDLED_NORMALLY;
5528 /***********************************************************************
5529 Overlay strings
5530 ***********************************************************************/
5532 /* The following structure is used to record overlay strings for
5533 later sorting in load_overlay_strings. */
5535 struct overlay_entry
5537 Lisp_Object overlay;
5538 Lisp_Object string;
5539 EMACS_INT priority;
5540 bool after_string_p;
5544 /* Set up iterator IT from overlay strings at its current position.
5545 Called from handle_stop. */
5547 static enum prop_handled
5548 handle_overlay_change (struct it *it)
5550 if (!STRINGP (it->string) && get_overlay_strings (it, 0))
5551 return HANDLED_RECOMPUTE_PROPS;
5552 else
5553 return HANDLED_NORMALLY;
5557 /* Set up the next overlay string for delivery by IT, if there is an
5558 overlay string to deliver. Called by set_iterator_to_next when the
5559 end of the current overlay string is reached. If there are more
5560 overlay strings to display, IT->string and
5561 IT->current.overlay_string_index are set appropriately here.
5562 Otherwise IT->string is set to nil. */
5564 static void
5565 next_overlay_string (struct it *it)
5567 ++it->current.overlay_string_index;
5568 if (it->current.overlay_string_index == it->n_overlay_strings)
5570 /* No more overlay strings. Restore IT's settings to what
5571 they were before overlay strings were processed, and
5572 continue to deliver from current_buffer. */
5574 it->ellipsis_p = it->stack[it->sp - 1].display_ellipsis_p;
5575 pop_it (it);
5576 eassert (it->sp > 0
5577 || (NILP (it->string)
5578 && it->method == GET_FROM_BUFFER
5579 && it->stop_charpos >= BEGV
5580 && it->stop_charpos <= it->end_charpos));
5581 it->current.overlay_string_index = -1;
5582 it->n_overlay_strings = 0;
5583 /* If there's an empty display string on the stack, pop the
5584 stack, to resync the bidi iterator with IT's position. Such
5585 empty strings are pushed onto the stack in
5586 get_overlay_strings_1. */
5587 if (it->sp > 0 && STRINGP (it->string) && !SCHARS (it->string))
5588 pop_it (it);
5590 /* Since we've exhausted overlay strings at this buffer
5591 position, set the flag to ignore overlays until we move to
5592 another position. (The flag will be reset in
5593 next_element_from_buffer.) But don't do that if the overlay
5594 strings were loaded at position other than the current one,
5595 which could happen if we called pop_it above, or if the
5596 overlay strings were loaded by handle_invisible_prop at the
5597 beginning of invisible text. */
5598 if (it->overlay_strings_charpos == IT_CHARPOS (*it))
5599 it->ignore_overlay_strings_at_pos_p = true;
5601 /* If we're at the end of the buffer, record that we have
5602 processed the overlay strings there already, so that
5603 next_element_from_buffer doesn't try it again. */
5604 if (NILP (it->string)
5605 && IT_CHARPOS (*it) >= it->end_charpos
5606 && it->overlay_strings_charpos >= it->end_charpos)
5607 it->overlay_strings_at_end_processed_p = true;
5608 /* Note: we reset overlay_strings_charpos only here, to make
5609 sure the just-processed overlays were indeed at EOB.
5610 Otherwise, overlays on text with invisible text property,
5611 which are processed with IT's position past the invisible
5612 text, might fool us into thinking the overlays at EOB were
5613 already processed (linum-mode can cause this, for
5614 example). */
5615 it->overlay_strings_charpos = -1;
5617 else
5619 /* There are more overlay strings to process. If
5620 IT->current.overlay_string_index has advanced to a position
5621 where we must load IT->overlay_strings with more strings, do
5622 it. We must load at the IT->overlay_strings_charpos where
5623 IT->n_overlay_strings was originally computed; when invisible
5624 text is present, this might not be IT_CHARPOS (Bug#7016). */
5625 int i = it->current.overlay_string_index % OVERLAY_STRING_CHUNK_SIZE;
5627 if (it->current.overlay_string_index && i == 0)
5628 load_overlay_strings (it, it->overlay_strings_charpos);
5630 /* Initialize IT to deliver display elements from the overlay
5631 string. */
5632 it->string = it->overlay_strings[i];
5633 it->multibyte_p = STRING_MULTIBYTE (it->string);
5634 SET_TEXT_POS (it->current.string_pos, 0, 0);
5635 it->method = GET_FROM_STRING;
5636 it->stop_charpos = 0;
5637 it->end_charpos = SCHARS (it->string);
5638 if (it->cmp_it.stop_pos >= 0)
5639 it->cmp_it.stop_pos = 0;
5640 it->prev_stop = 0;
5641 it->base_level_stop = 0;
5643 /* Set up the bidi iterator for this overlay string. */
5644 if (it->bidi_p)
5646 it->bidi_it.string.lstring = it->string;
5647 it->bidi_it.string.s = NULL;
5648 it->bidi_it.string.schars = SCHARS (it->string);
5649 it->bidi_it.string.bufpos = it->overlay_strings_charpos;
5650 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
5651 it->bidi_it.string.unibyte = !it->multibyte_p;
5652 it->bidi_it.w = it->w;
5653 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
5657 CHECK_IT (it);
5661 /* Compare two overlay_entry structures E1 and E2. Used as a
5662 comparison function for qsort in load_overlay_strings. Overlay
5663 strings for the same position are sorted so that
5665 1. All after-strings come in front of before-strings, except
5666 when they come from the same overlay.
5668 2. Within after-strings, strings are sorted so that overlay strings
5669 from overlays with higher priorities come first.
5671 2. Within before-strings, strings are sorted so that overlay
5672 strings from overlays with higher priorities come last.
5674 Value is analogous to strcmp. */
5677 static int
5678 compare_overlay_entries (const void *e1, const void *e2)
5680 struct overlay_entry const *entry1 = e1;
5681 struct overlay_entry const *entry2 = e2;
5682 int result;
5684 if (entry1->after_string_p != entry2->after_string_p)
5686 /* Let after-strings appear in front of before-strings if
5687 they come from different overlays. */
5688 if (EQ (entry1->overlay, entry2->overlay))
5689 result = entry1->after_string_p ? 1 : -1;
5690 else
5691 result = entry1->after_string_p ? -1 : 1;
5693 else if (entry1->priority != entry2->priority)
5695 if (entry1->after_string_p)
5696 /* After-strings sorted in order of decreasing priority. */
5697 result = entry2->priority < entry1->priority ? -1 : 1;
5698 else
5699 /* Before-strings sorted in order of increasing priority. */
5700 result = entry1->priority < entry2->priority ? -1 : 1;
5702 else
5703 result = 0;
5705 return result;
5709 /* Load the vector IT->overlay_strings with overlay strings from IT's
5710 current buffer position, or from CHARPOS if that is > 0. Set
5711 IT->n_overlays to the total number of overlay strings found.
5713 Overlay strings are processed OVERLAY_STRING_CHUNK_SIZE strings at
5714 a time. On entry into load_overlay_strings,
5715 IT->current.overlay_string_index gives the number of overlay
5716 strings that have already been loaded by previous calls to this
5717 function.
5719 IT->add_overlay_start contains an additional overlay start
5720 position to consider for taking overlay strings from, if non-zero.
5721 This position comes into play when the overlay has an `invisible'
5722 property, and both before and after-strings. When we've skipped to
5723 the end of the overlay, because of its `invisible' property, we
5724 nevertheless want its before-string to appear.
5725 IT->add_overlay_start will contain the overlay start position
5726 in this case.
5728 Overlay strings are sorted so that after-string strings come in
5729 front of before-string strings. Within before and after-strings,
5730 strings are sorted by overlay priority. See also function
5731 compare_overlay_entries. */
5733 static void
5734 load_overlay_strings (struct it *it, ptrdiff_t charpos)
5736 Lisp_Object overlay, window, str, invisible;
5737 struct Lisp_Overlay *ov;
5738 ptrdiff_t start, end;
5739 ptrdiff_t n = 0, i, j;
5740 int invis;
5741 struct overlay_entry entriesbuf[20];
5742 ptrdiff_t size = ARRAYELTS (entriesbuf);
5743 struct overlay_entry *entries = entriesbuf;
5744 USE_SAFE_ALLOCA;
5746 if (charpos <= 0)
5747 charpos = IT_CHARPOS (*it);
5749 /* Append the overlay string STRING of overlay OVERLAY to vector
5750 `entries' which has size `size' and currently contains `n'
5751 elements. AFTER_P means STRING is an after-string of
5752 OVERLAY. */
5753 #define RECORD_OVERLAY_STRING(OVERLAY, STRING, AFTER_P) \
5754 do \
5756 Lisp_Object priority; \
5758 if (n == size) \
5760 struct overlay_entry *old = entries; \
5761 SAFE_NALLOCA (entries, 2, size); \
5762 memcpy (entries, old, size * sizeof *entries); \
5763 size *= 2; \
5766 entries[n].string = (STRING); \
5767 entries[n].overlay = (OVERLAY); \
5768 priority = Foverlay_get ((OVERLAY), Qpriority); \
5769 entries[n].priority = INTEGERP (priority) ? XINT (priority) : 0; \
5770 entries[n].after_string_p = (AFTER_P); \
5771 ++n; \
5773 while (false)
5775 /* Process overlay before the overlay center. */
5776 for (ov = current_buffer->overlays_before; ov; ov = ov->next)
5778 XSETMISC (overlay, ov);
5779 eassert (OVERLAYP (overlay));
5780 start = OVERLAY_POSITION (OVERLAY_START (overlay));
5781 end = OVERLAY_POSITION (OVERLAY_END (overlay));
5783 if (end < charpos)
5784 break;
5786 /* Skip this overlay if it doesn't start or end at IT's current
5787 position. */
5788 if (end != charpos && start != charpos)
5789 continue;
5791 /* Skip this overlay if it doesn't apply to IT->w. */
5792 window = Foverlay_get (overlay, Qwindow);
5793 if (WINDOWP (window) && XWINDOW (window) != it->w)
5794 continue;
5796 /* If the text ``under'' the overlay is invisible, both before-
5797 and after-strings from this overlay are visible; start and
5798 end position are indistinguishable. */
5799 invisible = Foverlay_get (overlay, Qinvisible);
5800 invis = TEXT_PROP_MEANS_INVISIBLE (invisible);
5802 /* If overlay has a non-empty before-string, record it. */
5803 if ((start == charpos || (end == charpos && invis != 0))
5804 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
5805 && SCHARS (str))
5806 RECORD_OVERLAY_STRING (overlay, str, false);
5808 /* If overlay has a non-empty after-string, record it. */
5809 if ((end == charpos || (start == charpos && invis != 0))
5810 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
5811 && SCHARS (str))
5812 RECORD_OVERLAY_STRING (overlay, str, true);
5815 /* Process overlays after the overlay center. */
5816 for (ov = current_buffer->overlays_after; ov; ov = ov->next)
5818 XSETMISC (overlay, ov);
5819 eassert (OVERLAYP (overlay));
5820 start = OVERLAY_POSITION (OVERLAY_START (overlay));
5821 end = OVERLAY_POSITION (OVERLAY_END (overlay));
5823 if (start > charpos)
5824 break;
5826 /* Skip this overlay if it doesn't start or end at IT's current
5827 position. */
5828 if (end != charpos && start != charpos)
5829 continue;
5831 /* Skip this overlay if it doesn't apply to IT->w. */
5832 window = Foverlay_get (overlay, Qwindow);
5833 if (WINDOWP (window) && XWINDOW (window) != it->w)
5834 continue;
5836 /* If the text ``under'' the overlay is invisible, it has a zero
5837 dimension, and both before- and after-strings apply. */
5838 invisible = Foverlay_get (overlay, Qinvisible);
5839 invis = TEXT_PROP_MEANS_INVISIBLE (invisible);
5841 /* If overlay has a non-empty before-string, record it. */
5842 if ((start == charpos || (end == charpos && invis != 0))
5843 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
5844 && SCHARS (str))
5845 RECORD_OVERLAY_STRING (overlay, str, false);
5847 /* If overlay has a non-empty after-string, record it. */
5848 if ((end == charpos || (start == charpos && invis != 0))
5849 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
5850 && SCHARS (str))
5851 RECORD_OVERLAY_STRING (overlay, str, true);
5854 #undef RECORD_OVERLAY_STRING
5856 /* Sort entries. */
5857 if (n > 1)
5858 qsort (entries, n, sizeof *entries, compare_overlay_entries);
5860 /* Record number of overlay strings, and where we computed it. */
5861 it->n_overlay_strings = n;
5862 it->overlay_strings_charpos = charpos;
5864 /* IT->current.overlay_string_index is the number of overlay strings
5865 that have already been consumed by IT. Copy some of the
5866 remaining overlay strings to IT->overlay_strings. */
5867 i = 0;
5868 j = it->current.overlay_string_index;
5869 while (i < OVERLAY_STRING_CHUNK_SIZE && j < n)
5871 it->overlay_strings[i] = entries[j].string;
5872 it->string_overlays[i++] = entries[j++].overlay;
5875 CHECK_IT (it);
5876 SAFE_FREE ();
5880 /* Get the first chunk of overlay strings at IT's current buffer
5881 position, or at CHARPOS if that is > 0. Value is true if at
5882 least one overlay string was found. */
5884 static bool
5885 get_overlay_strings_1 (struct it *it, ptrdiff_t charpos, bool compute_stop_p)
5887 /* Get the first OVERLAY_STRING_CHUNK_SIZE overlay strings to
5888 process. This fills IT->overlay_strings with strings, and sets
5889 IT->n_overlay_strings to the total number of strings to process.
5890 IT->pos.overlay_string_index has to be set temporarily to zero
5891 because load_overlay_strings needs this; it must be set to -1
5892 when no overlay strings are found because a zero value would
5893 indicate a position in the first overlay string. */
5894 it->current.overlay_string_index = 0;
5895 load_overlay_strings (it, charpos);
5897 /* If we found overlay strings, set up IT to deliver display
5898 elements from the first one. Otherwise set up IT to deliver
5899 from current_buffer. */
5900 if (it->n_overlay_strings)
5902 /* Make sure we know settings in current_buffer, so that we can
5903 restore meaningful values when we're done with the overlay
5904 strings. */
5905 if (compute_stop_p)
5906 compute_stop_pos (it);
5907 eassert (it->face_id >= 0);
5909 /* Save IT's settings. They are restored after all overlay
5910 strings have been processed. */
5911 eassert (!compute_stop_p || it->sp == 0);
5913 /* When called from handle_stop, there might be an empty display
5914 string loaded. In that case, don't bother saving it. But
5915 don't use this optimization with the bidi iterator, since we
5916 need the corresponding pop_it call to resync the bidi
5917 iterator's position with IT's position, after we are done
5918 with the overlay strings. (The corresponding call to pop_it
5919 in case of an empty display string is in
5920 next_overlay_string.) */
5921 if (!(!it->bidi_p
5922 && STRINGP (it->string) && !SCHARS (it->string)))
5923 push_it (it, NULL);
5925 /* Set up IT to deliver display elements from the first overlay
5926 string. */
5927 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
5928 it->string = it->overlay_strings[0];
5929 it->from_overlay = Qnil;
5930 it->stop_charpos = 0;
5931 eassert (STRINGP (it->string));
5932 it->end_charpos = SCHARS (it->string);
5933 it->prev_stop = 0;
5934 it->base_level_stop = 0;
5935 it->multibyte_p = STRING_MULTIBYTE (it->string);
5936 it->method = GET_FROM_STRING;
5937 it->from_disp_prop_p = 0;
5939 /* Force paragraph direction to be that of the parent
5940 buffer. */
5941 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
5942 it->paragraph_embedding = it->bidi_it.paragraph_dir;
5943 else
5944 it->paragraph_embedding = L2R;
5946 /* Set up the bidi iterator for this overlay string. */
5947 if (it->bidi_p)
5949 ptrdiff_t pos = (charpos > 0 ? charpos : IT_CHARPOS (*it));
5951 it->bidi_it.string.lstring = it->string;
5952 it->bidi_it.string.s = NULL;
5953 it->bidi_it.string.schars = SCHARS (it->string);
5954 it->bidi_it.string.bufpos = pos;
5955 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
5956 it->bidi_it.string.unibyte = !it->multibyte_p;
5957 it->bidi_it.w = it->w;
5958 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
5960 return true;
5963 it->current.overlay_string_index = -1;
5964 return false;
5967 static bool
5968 get_overlay_strings (struct it *it, ptrdiff_t charpos)
5970 it->string = Qnil;
5971 it->method = GET_FROM_BUFFER;
5973 get_overlay_strings_1 (it, charpos, true);
5975 CHECK_IT (it);
5977 /* Value is true if we found at least one overlay string. */
5978 return STRINGP (it->string);
5983 /***********************************************************************
5984 Saving and restoring state
5985 ***********************************************************************/
5987 /* Save current settings of IT on IT->stack. Called, for example,
5988 before setting up IT for an overlay string, to be able to restore
5989 IT's settings to what they were after the overlay string has been
5990 processed. If POSITION is non-NULL, it is the position to save on
5991 the stack instead of IT->position. */
5993 static void
5994 push_it (struct it *it, struct text_pos *position)
5996 struct iterator_stack_entry *p;
5998 eassert (it->sp < IT_STACK_SIZE);
5999 p = it->stack + it->sp;
6001 p->stop_charpos = it->stop_charpos;
6002 p->prev_stop = it->prev_stop;
6003 p->base_level_stop = it->base_level_stop;
6004 p->cmp_it = it->cmp_it;
6005 eassert (it->face_id >= 0);
6006 p->face_id = it->face_id;
6007 p->string = it->string;
6008 p->method = it->method;
6009 p->from_overlay = it->from_overlay;
6010 switch (p->method)
6012 case GET_FROM_IMAGE:
6013 p->u.image.object = it->object;
6014 p->u.image.image_id = it->image_id;
6015 p->u.image.slice = it->slice;
6016 break;
6017 case GET_FROM_STRETCH:
6018 p->u.stretch.object = it->object;
6019 break;
6020 case GET_FROM_XWIDGET:
6021 p->u.xwidget.object = it->object;
6022 break;
6023 case GET_FROM_BUFFER:
6024 case GET_FROM_DISPLAY_VECTOR:
6025 case GET_FROM_STRING:
6026 case GET_FROM_C_STRING:
6027 break;
6028 default:
6029 emacs_abort ();
6031 p->position = position ? *position : it->position;
6032 p->current = it->current;
6033 p->end_charpos = it->end_charpos;
6034 p->string_nchars = it->string_nchars;
6035 p->area = it->area;
6036 p->multibyte_p = it->multibyte_p;
6037 p->avoid_cursor_p = it->avoid_cursor_p;
6038 p->space_width = it->space_width;
6039 p->font_height = it->font_height;
6040 p->voffset = it->voffset;
6041 p->string_from_display_prop_p = it->string_from_display_prop_p;
6042 p->string_from_prefix_prop_p = it->string_from_prefix_prop_p;
6043 p->display_ellipsis_p = false;
6044 p->line_wrap = it->line_wrap;
6045 p->bidi_p = it->bidi_p;
6046 p->paragraph_embedding = it->paragraph_embedding;
6047 p->from_disp_prop_p = it->from_disp_prop_p;
6048 ++it->sp;
6050 /* Save the state of the bidi iterator as well. */
6051 if (it->bidi_p)
6052 bidi_push_it (&it->bidi_it);
6055 static void
6056 iterate_out_of_display_property (struct it *it)
6058 bool buffer_p = !STRINGP (it->string);
6059 ptrdiff_t eob = (buffer_p ? ZV : it->end_charpos);
6060 ptrdiff_t bob = (buffer_p ? BEGV : 0);
6062 eassert (eob >= CHARPOS (it->position) && CHARPOS (it->position) >= bob);
6064 /* Maybe initialize paragraph direction. If we are at the beginning
6065 of a new paragraph, next_element_from_buffer may not have a
6066 chance to do that. */
6067 if (it->bidi_it.first_elt && it->bidi_it.charpos < eob)
6068 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, true);
6069 /* prev_stop can be zero, so check against BEGV as well. */
6070 while (it->bidi_it.charpos >= bob
6071 && it->prev_stop <= it->bidi_it.charpos
6072 && it->bidi_it.charpos < CHARPOS (it->position)
6073 && it->bidi_it.charpos < eob)
6074 bidi_move_to_visually_next (&it->bidi_it);
6075 /* Record the stop_pos we just crossed, for when we cross it
6076 back, maybe. */
6077 if (it->bidi_it.charpos > CHARPOS (it->position))
6078 it->prev_stop = CHARPOS (it->position);
6079 /* If we ended up not where pop_it put us, resync IT's
6080 positional members with the bidi iterator. */
6081 if (it->bidi_it.charpos != CHARPOS (it->position))
6082 SET_TEXT_POS (it->position, it->bidi_it.charpos, it->bidi_it.bytepos);
6083 if (buffer_p)
6084 it->current.pos = it->position;
6085 else
6086 it->current.string_pos = it->position;
6089 /* Restore IT's settings from IT->stack. Called, for example, when no
6090 more overlay strings must be processed, and we return to delivering
6091 display elements from a buffer, or when the end of a string from a
6092 `display' property is reached and we return to delivering display
6093 elements from an overlay string, or from a buffer. */
6095 static void
6096 pop_it (struct it *it)
6098 struct iterator_stack_entry *p;
6099 bool from_display_prop = it->from_disp_prop_p;
6100 ptrdiff_t prev_pos = IT_CHARPOS (*it);
6102 eassert (it->sp > 0);
6103 --it->sp;
6104 p = it->stack + it->sp;
6105 it->stop_charpos = p->stop_charpos;
6106 it->prev_stop = p->prev_stop;
6107 it->base_level_stop = p->base_level_stop;
6108 it->cmp_it = p->cmp_it;
6109 it->face_id = p->face_id;
6110 it->current = p->current;
6111 it->position = p->position;
6112 it->string = p->string;
6113 it->from_overlay = p->from_overlay;
6114 if (NILP (it->string))
6115 SET_TEXT_POS (it->current.string_pos, -1, -1);
6116 it->method = p->method;
6117 switch (it->method)
6119 case GET_FROM_IMAGE:
6120 it->image_id = p->u.image.image_id;
6121 it->object = p->u.image.object;
6122 it->slice = p->u.image.slice;
6123 break;
6124 case GET_FROM_XWIDGET:
6125 it->object = p->u.xwidget.object;
6126 break;
6127 case GET_FROM_STRETCH:
6128 it->object = p->u.stretch.object;
6129 break;
6130 case GET_FROM_BUFFER:
6131 it->object = it->w->contents;
6132 break;
6133 case GET_FROM_STRING:
6135 struct face *face = FACE_FROM_ID_OR_NULL (it->f, it->face_id);
6137 /* Restore the face_box_p flag, since it could have been
6138 overwritten by the face of the object that we just finished
6139 displaying. */
6140 if (face)
6141 it->face_box_p = face->box != FACE_NO_BOX;
6142 it->object = it->string;
6144 break;
6145 case GET_FROM_DISPLAY_VECTOR:
6146 if (it->s)
6147 it->method = GET_FROM_C_STRING;
6148 else if (STRINGP (it->string))
6149 it->method = GET_FROM_STRING;
6150 else
6152 it->method = GET_FROM_BUFFER;
6153 it->object = it->w->contents;
6155 break;
6156 case GET_FROM_C_STRING:
6157 break;
6158 default:
6159 emacs_abort ();
6161 it->end_charpos = p->end_charpos;
6162 it->string_nchars = p->string_nchars;
6163 it->area = p->area;
6164 it->multibyte_p = p->multibyte_p;
6165 it->avoid_cursor_p = p->avoid_cursor_p;
6166 it->space_width = p->space_width;
6167 it->font_height = p->font_height;
6168 it->voffset = p->voffset;
6169 it->string_from_display_prop_p = p->string_from_display_prop_p;
6170 it->string_from_prefix_prop_p = p->string_from_prefix_prop_p;
6171 it->line_wrap = p->line_wrap;
6172 it->bidi_p = p->bidi_p;
6173 it->paragraph_embedding = p->paragraph_embedding;
6174 it->from_disp_prop_p = p->from_disp_prop_p;
6175 if (it->bidi_p)
6177 bidi_pop_it (&it->bidi_it);
6178 /* Bidi-iterate until we get out of the portion of text, if any,
6179 covered by a `display' text property or by an overlay with
6180 `display' property. (We cannot just jump there, because the
6181 internal coherency of the bidi iterator state can not be
6182 preserved across such jumps.) We also must determine the
6183 paragraph base direction if the overlay we just processed is
6184 at the beginning of a new paragraph. */
6185 if (from_display_prop
6186 && (it->method == GET_FROM_BUFFER || it->method == GET_FROM_STRING))
6187 iterate_out_of_display_property (it);
6189 eassert ((BUFFERP (it->object)
6190 && IT_CHARPOS (*it) == it->bidi_it.charpos
6191 && IT_BYTEPOS (*it) == it->bidi_it.bytepos)
6192 || (STRINGP (it->object)
6193 && IT_STRING_CHARPOS (*it) == it->bidi_it.charpos
6194 && IT_STRING_BYTEPOS (*it) == it->bidi_it.bytepos)
6195 || (CONSP (it->object) && it->method == GET_FROM_STRETCH));
6197 /* If we move the iterator over text covered by a display property
6198 to a new buffer position, any info about previously seen overlays
6199 is no longer valid. */
6200 if (from_display_prop && it->sp == 0 && CHARPOS (it->position) != prev_pos)
6201 it->ignore_overlay_strings_at_pos_p = false;
6206 /***********************************************************************
6207 Moving over lines
6208 ***********************************************************************/
6210 /* Set IT's current position to the previous line start. */
6212 static void
6213 back_to_previous_line_start (struct it *it)
6215 ptrdiff_t cp = IT_CHARPOS (*it), bp = IT_BYTEPOS (*it);
6217 DEC_BOTH (cp, bp);
6218 IT_CHARPOS (*it) = find_newline_no_quit (cp, bp, -1, &IT_BYTEPOS (*it));
6222 /* Move IT to the next line start.
6224 Value is true if a newline was found. Set *SKIPPED_P to true if
6225 we skipped over part of the text (as opposed to moving the iterator
6226 continuously over the text). Otherwise, don't change the value
6227 of *SKIPPED_P.
6229 If BIDI_IT_PREV is non-NULL, store into it the state of the bidi
6230 iterator on the newline, if it was found.
6232 Newlines may come from buffer text, overlay strings, or strings
6233 displayed via the `display' property. That's the reason we can't
6234 simply use find_newline_no_quit.
6236 Note that this function may not skip over invisible text that is so
6237 because of text properties and immediately follows a newline. If
6238 it would, function reseat_at_next_visible_line_start, when called
6239 from set_iterator_to_next, would effectively make invisible
6240 characters following a newline part of the wrong glyph row, which
6241 leads to wrong cursor motion. */
6243 static bool
6244 forward_to_next_line_start (struct it *it, bool *skipped_p,
6245 struct bidi_it *bidi_it_prev)
6247 ptrdiff_t old_selective;
6248 bool newline_found_p = false;
6249 int n;
6250 const int MAX_NEWLINE_DISTANCE = 500;
6252 /* If already on a newline, just consume it to avoid unintended
6253 skipping over invisible text below. */
6254 if (it->what == IT_CHARACTER
6255 && it->c == '\n'
6256 && CHARPOS (it->position) == IT_CHARPOS (*it))
6258 if (it->bidi_p && bidi_it_prev)
6259 *bidi_it_prev = it->bidi_it;
6260 set_iterator_to_next (it, false);
6261 it->c = 0;
6262 return true;
6265 /* Don't handle selective display in the following. It's (a)
6266 unnecessary because it's done by the caller, and (b) leads to an
6267 infinite recursion because next_element_from_ellipsis indirectly
6268 calls this function. */
6269 old_selective = it->selective;
6270 it->selective = 0;
6272 /* Scan for a newline within MAX_NEWLINE_DISTANCE display elements
6273 from buffer text. */
6274 for (n = 0;
6275 !newline_found_p && n < MAX_NEWLINE_DISTANCE;
6276 n += !STRINGP (it->string))
6278 if (!get_next_display_element (it))
6279 return false;
6280 newline_found_p = it->what == IT_CHARACTER && it->c == '\n';
6281 if (newline_found_p && it->bidi_p && bidi_it_prev)
6282 *bidi_it_prev = it->bidi_it;
6283 set_iterator_to_next (it, false);
6286 /* If we didn't find a newline near enough, see if we can use a
6287 short-cut. */
6288 if (!newline_found_p)
6290 ptrdiff_t bytepos, start = IT_CHARPOS (*it);
6291 ptrdiff_t limit = find_newline_no_quit (start, IT_BYTEPOS (*it),
6292 1, &bytepos);
6293 Lisp_Object pos;
6295 eassert (!STRINGP (it->string));
6297 /* If there isn't any `display' property in sight, and no
6298 overlays, we can just use the position of the newline in
6299 buffer text. */
6300 if (it->stop_charpos >= limit
6301 || ((pos = Fnext_single_property_change (make_number (start),
6302 Qdisplay, Qnil,
6303 make_number (limit)),
6304 NILP (pos))
6305 && next_overlay_change (start) == ZV))
6307 if (!it->bidi_p)
6309 IT_CHARPOS (*it) = limit;
6310 IT_BYTEPOS (*it) = bytepos;
6312 else
6314 struct bidi_it bprev;
6316 /* Help bidi.c avoid expensive searches for display
6317 properties and overlays, by telling it that there are
6318 none up to `limit'. */
6319 if (it->bidi_it.disp_pos < limit)
6321 it->bidi_it.disp_pos = limit;
6322 it->bidi_it.disp_prop = 0;
6324 do {
6325 bprev = it->bidi_it;
6326 bidi_move_to_visually_next (&it->bidi_it);
6327 } while (it->bidi_it.charpos != limit);
6328 IT_CHARPOS (*it) = limit;
6329 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6330 if (bidi_it_prev)
6331 *bidi_it_prev = bprev;
6333 *skipped_p = newline_found_p = true;
6335 else
6337 while (!newline_found_p)
6339 if (!get_next_display_element (it))
6340 break;
6341 newline_found_p = ITERATOR_AT_END_OF_LINE_P (it);
6342 if (newline_found_p && it->bidi_p && bidi_it_prev)
6343 *bidi_it_prev = it->bidi_it;
6344 set_iterator_to_next (it, false);
6349 it->selective = old_selective;
6350 return newline_found_p;
6354 /* Set IT's current position to the previous visible line start. Skip
6355 invisible text that is so either due to text properties or due to
6356 selective display. Caution: this does not change IT->current_x and
6357 IT->hpos. */
6359 static void
6360 back_to_previous_visible_line_start (struct it *it)
6362 while (IT_CHARPOS (*it) > BEGV)
6364 back_to_previous_line_start (it);
6366 if (IT_CHARPOS (*it) <= BEGV)
6367 break;
6369 /* If selective > 0, then lines indented more than its value are
6370 invisible. */
6371 if (it->selective > 0
6372 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
6373 it->selective))
6374 continue;
6376 /* Check the newline before point for invisibility. */
6378 Lisp_Object prop;
6379 prop = Fget_char_property (make_number (IT_CHARPOS (*it) - 1),
6380 Qinvisible, it->window);
6381 if (TEXT_PROP_MEANS_INVISIBLE (prop) != 0)
6382 continue;
6385 if (IT_CHARPOS (*it) <= BEGV)
6386 break;
6389 struct it it2;
6390 void *it2data = NULL;
6391 ptrdiff_t pos;
6392 ptrdiff_t beg, end;
6393 Lisp_Object val, overlay;
6395 SAVE_IT (it2, *it, it2data);
6397 /* If newline is part of a composition, continue from start of composition */
6398 if (find_composition (IT_CHARPOS (*it), -1, &beg, &end, &val, Qnil)
6399 && beg < IT_CHARPOS (*it))
6400 goto replaced;
6402 /* If newline is replaced by a display property, find start of overlay
6403 or interval and continue search from that point. */
6404 pos = --IT_CHARPOS (it2);
6405 --IT_BYTEPOS (it2);
6406 it2.sp = 0;
6407 bidi_unshelve_cache (NULL, false);
6408 it2.string_from_display_prop_p = false;
6409 it2.from_disp_prop_p = false;
6410 if (handle_display_prop (&it2) == HANDLED_RETURN
6411 && !NILP (val = get_char_property_and_overlay
6412 (make_number (pos), Qdisplay, Qnil, &overlay))
6413 && (OVERLAYP (overlay)
6414 ? (beg = OVERLAY_POSITION (OVERLAY_START (overlay)))
6415 : get_property_and_range (pos, Qdisplay, &val, &beg, &end, Qnil)))
6417 RESTORE_IT (it, it, it2data);
6418 goto replaced;
6421 /* Newline is not replaced by anything -- so we are done. */
6422 RESTORE_IT (it, it, it2data);
6423 break;
6425 replaced:
6426 if (beg < BEGV)
6427 beg = BEGV;
6428 IT_CHARPOS (*it) = beg;
6429 IT_BYTEPOS (*it) = buf_charpos_to_bytepos (current_buffer, beg);
6433 it->continuation_lines_width = 0;
6435 eassert (IT_CHARPOS (*it) >= BEGV);
6436 eassert (IT_CHARPOS (*it) == BEGV
6437 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
6438 CHECK_IT (it);
6442 /* Reseat iterator IT at the previous visible line start. Skip
6443 invisible text that is so either due to text properties or due to
6444 selective display. At the end, update IT's overlay information,
6445 face information etc. */
6447 void
6448 reseat_at_previous_visible_line_start (struct it *it)
6450 back_to_previous_visible_line_start (it);
6451 reseat (it, it->current.pos, true);
6452 CHECK_IT (it);
6456 /* Reseat iterator IT on the next visible line start in the current
6457 buffer. ON_NEWLINE_P means position IT on the newline
6458 preceding the line start. Skip over invisible text that is so
6459 because of selective display. Compute faces, overlays etc at the
6460 new position. Note that this function does not skip over text that
6461 is invisible because of text properties. */
6463 static void
6464 reseat_at_next_visible_line_start (struct it *it, bool on_newline_p)
6466 bool skipped_p = false;
6467 struct bidi_it bidi_it_prev;
6468 bool newline_found_p
6469 = forward_to_next_line_start (it, &skipped_p, &bidi_it_prev);
6471 /* Skip over lines that are invisible because they are indented
6472 more than the value of IT->selective. */
6473 if (it->selective > 0)
6474 while (IT_CHARPOS (*it) < ZV
6475 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
6476 it->selective))
6478 eassert (IT_BYTEPOS (*it) == BEGV
6479 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
6480 newline_found_p =
6481 forward_to_next_line_start (it, &skipped_p, &bidi_it_prev);
6484 /* Position on the newline if that's what's requested. */
6485 if (on_newline_p && newline_found_p)
6487 if (STRINGP (it->string))
6489 if (IT_STRING_CHARPOS (*it) > 0)
6491 if (!it->bidi_p)
6493 --IT_STRING_CHARPOS (*it);
6494 --IT_STRING_BYTEPOS (*it);
6496 else
6498 /* We need to restore the bidi iterator to the state
6499 it had on the newline, and resync the IT's
6500 position with that. */
6501 it->bidi_it = bidi_it_prev;
6502 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
6503 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
6507 else if (IT_CHARPOS (*it) > BEGV)
6509 if (!it->bidi_p)
6511 --IT_CHARPOS (*it);
6512 --IT_BYTEPOS (*it);
6514 else
6516 /* We need to restore the bidi iterator to the state it
6517 had on the newline and resync IT with that. */
6518 it->bidi_it = bidi_it_prev;
6519 IT_CHARPOS (*it) = it->bidi_it.charpos;
6520 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6522 reseat (it, it->current.pos, false);
6525 else if (skipped_p)
6526 reseat (it, it->current.pos, false);
6528 CHECK_IT (it);
6533 /***********************************************************************
6534 Changing an iterator's position
6535 ***********************************************************************/
6537 /* Change IT's current position to POS in current_buffer.
6538 If FORCE_P, always check for text properties at the new position.
6539 Otherwise, text properties are only looked up if POS >=
6540 IT->check_charpos of a property. */
6542 static void
6543 reseat (struct it *it, struct text_pos pos, bool force_p)
6545 ptrdiff_t original_pos = IT_CHARPOS (*it);
6547 reseat_1 (it, pos, false);
6549 /* Determine where to check text properties. Avoid doing it
6550 where possible because text property lookup is very expensive. */
6551 if (force_p
6552 || CHARPOS (pos) > it->stop_charpos
6553 || CHARPOS (pos) < original_pos)
6555 if (it->bidi_p)
6557 /* For bidi iteration, we need to prime prev_stop and
6558 base_level_stop with our best estimations. */
6559 /* Implementation note: Of course, POS is not necessarily a
6560 stop position, so assigning prev_pos to it is a lie; we
6561 should have called compute_stop_backwards. However, if
6562 the current buffer does not include any R2L characters,
6563 that call would be a waste of cycles, because the
6564 iterator will never move back, and thus never cross this
6565 "fake" stop position. So we delay that backward search
6566 until the time we really need it, in next_element_from_buffer. */
6567 if (CHARPOS (pos) != it->prev_stop)
6568 it->prev_stop = CHARPOS (pos);
6569 if (CHARPOS (pos) < it->base_level_stop)
6570 it->base_level_stop = 0; /* meaning it's unknown */
6571 handle_stop (it);
6573 else
6575 handle_stop (it);
6576 it->prev_stop = it->base_level_stop = 0;
6581 CHECK_IT (it);
6585 /* Change IT's buffer position to POS. SET_STOP_P means set
6586 IT->stop_pos to POS, also. */
6588 static void
6589 reseat_1 (struct it *it, struct text_pos pos, bool set_stop_p)
6591 /* Don't call this function when scanning a C string. */
6592 eassert (it->s == NULL);
6594 /* POS must be a reasonable value. */
6595 eassert (CHARPOS (pos) >= BEGV && CHARPOS (pos) <= ZV);
6597 it->current.pos = it->position = pos;
6598 it->end_charpos = ZV;
6599 it->dpvec = NULL;
6600 it->current.dpvec_index = -1;
6601 it->current.overlay_string_index = -1;
6602 IT_STRING_CHARPOS (*it) = -1;
6603 IT_STRING_BYTEPOS (*it) = -1;
6604 it->string = Qnil;
6605 it->method = GET_FROM_BUFFER;
6606 it->object = it->w->contents;
6607 it->area = TEXT_AREA;
6608 it->multibyte_p = !NILP (BVAR (current_buffer, enable_multibyte_characters));
6609 it->sp = 0;
6610 it->string_from_display_prop_p = false;
6611 it->string_from_prefix_prop_p = false;
6613 it->from_disp_prop_p = false;
6614 it->face_before_selective_p = false;
6615 if (it->bidi_p)
6617 bidi_init_it (IT_CHARPOS (*it), IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
6618 &it->bidi_it);
6619 bidi_unshelve_cache (NULL, false);
6620 it->bidi_it.paragraph_dir = NEUTRAL_DIR;
6621 it->bidi_it.string.s = NULL;
6622 it->bidi_it.string.lstring = Qnil;
6623 it->bidi_it.string.bufpos = 0;
6624 it->bidi_it.string.from_disp_str = false;
6625 it->bidi_it.string.unibyte = false;
6626 it->bidi_it.w = it->w;
6629 if (set_stop_p)
6631 it->stop_charpos = CHARPOS (pos);
6632 it->base_level_stop = CHARPOS (pos);
6634 /* This make the information stored in it->cmp_it invalidate. */
6635 it->cmp_it.id = -1;
6639 /* Set up IT for displaying a string, starting at CHARPOS in window W.
6640 If S is non-null, it is a C string to iterate over. Otherwise,
6641 STRING gives a Lisp string to iterate over.
6643 If PRECISION > 0, don't return more then PRECISION number of
6644 characters from the string.
6646 If FIELD_WIDTH > 0, return padding spaces until FIELD_WIDTH
6647 characters have been returned. FIELD_WIDTH < 0 means an infinite
6648 field width.
6650 MULTIBYTE = 0 means disable processing of multibyte characters,
6651 MULTIBYTE > 0 means enable it,
6652 MULTIBYTE < 0 means use IT->multibyte_p.
6654 IT must be initialized via a prior call to init_iterator before
6655 calling this function. */
6657 static void
6658 reseat_to_string (struct it *it, const char *s, Lisp_Object string,
6659 ptrdiff_t charpos, ptrdiff_t precision, int field_width,
6660 int multibyte)
6662 /* No text property checks performed by default, but see below. */
6663 it->stop_charpos = -1;
6665 /* Set iterator position and end position. */
6666 memset (&it->current, 0, sizeof it->current);
6667 it->current.overlay_string_index = -1;
6668 it->current.dpvec_index = -1;
6669 eassert (charpos >= 0);
6671 /* If STRING is specified, use its multibyteness, otherwise use the
6672 setting of MULTIBYTE, if specified. */
6673 if (multibyte >= 0)
6674 it->multibyte_p = multibyte > 0;
6676 /* Bidirectional reordering of strings is controlled by the default
6677 value of bidi-display-reordering. Don't try to reorder while
6678 loading loadup.el, as the necessary character property tables are
6679 not yet available. */
6680 it->bidi_p =
6681 !redisplay__inhibit_bidi
6682 && !NILP (BVAR (&buffer_defaults, bidi_display_reordering));
6684 if (s == NULL)
6686 eassert (STRINGP (string));
6687 it->string = string;
6688 it->s = NULL;
6689 it->end_charpos = it->string_nchars = SCHARS (string);
6690 it->method = GET_FROM_STRING;
6691 it->current.string_pos = string_pos (charpos, string);
6693 if (it->bidi_p)
6695 it->bidi_it.string.lstring = string;
6696 it->bidi_it.string.s = NULL;
6697 it->bidi_it.string.schars = it->end_charpos;
6698 it->bidi_it.string.bufpos = 0;
6699 it->bidi_it.string.from_disp_str = false;
6700 it->bidi_it.string.unibyte = !it->multibyte_p;
6701 it->bidi_it.w = it->w;
6702 bidi_init_it (charpos, IT_STRING_BYTEPOS (*it),
6703 FRAME_WINDOW_P (it->f), &it->bidi_it);
6706 else
6708 it->s = (const unsigned char *) s;
6709 it->string = Qnil;
6711 /* Note that we use IT->current.pos, not it->current.string_pos,
6712 for displaying C strings. */
6713 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
6714 if (it->multibyte_p)
6716 it->current.pos = c_string_pos (charpos, s, true);
6717 it->end_charpos = it->string_nchars = number_of_chars (s, true);
6719 else
6721 IT_CHARPOS (*it) = IT_BYTEPOS (*it) = charpos;
6722 it->end_charpos = it->string_nchars = strlen (s);
6725 if (it->bidi_p)
6727 it->bidi_it.string.lstring = Qnil;
6728 it->bidi_it.string.s = (const unsigned char *) s;
6729 it->bidi_it.string.schars = it->end_charpos;
6730 it->bidi_it.string.bufpos = 0;
6731 it->bidi_it.string.from_disp_str = false;
6732 it->bidi_it.string.unibyte = !it->multibyte_p;
6733 it->bidi_it.w = it->w;
6734 bidi_init_it (charpos, IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
6735 &it->bidi_it);
6737 it->method = GET_FROM_C_STRING;
6740 /* PRECISION > 0 means don't return more than PRECISION characters
6741 from the string. */
6742 if (precision > 0 && it->end_charpos - charpos > precision)
6744 it->end_charpos = it->string_nchars = charpos + precision;
6745 if (it->bidi_p)
6746 it->bidi_it.string.schars = it->end_charpos;
6749 /* FIELD_WIDTH > 0 means pad with spaces until FIELD_WIDTH
6750 characters have been returned. FIELD_WIDTH == 0 means don't pad,
6751 FIELD_WIDTH < 0 means infinite field width. This is useful for
6752 padding with `-' at the end of a mode line. */
6753 if (field_width < 0)
6754 field_width = INFINITY;
6755 /* Implementation note: We deliberately don't enlarge
6756 it->bidi_it.string.schars here to fit it->end_charpos, because
6757 the bidi iterator cannot produce characters out of thin air. */
6758 if (field_width > it->end_charpos - charpos)
6759 it->end_charpos = charpos + field_width;
6761 /* Use the standard display table for displaying strings. */
6762 if (DISP_TABLE_P (Vstandard_display_table))
6763 it->dp = XCHAR_TABLE (Vstandard_display_table);
6765 it->stop_charpos = charpos;
6766 it->prev_stop = charpos;
6767 it->base_level_stop = 0;
6768 if (it->bidi_p)
6770 it->bidi_it.first_elt = true;
6771 it->bidi_it.paragraph_dir = NEUTRAL_DIR;
6772 it->bidi_it.disp_pos = -1;
6774 if (s == NULL && it->multibyte_p)
6776 ptrdiff_t endpos = SCHARS (it->string);
6777 if (endpos > it->end_charpos)
6778 endpos = it->end_charpos;
6779 composition_compute_stop_pos (&it->cmp_it, charpos, -1, endpos,
6780 it->string);
6782 CHECK_IT (it);
6787 /***********************************************************************
6788 Iteration
6789 ***********************************************************************/
6791 /* Map enum it_method value to corresponding next_element_from_* function. */
6793 typedef bool (*next_element_function) (struct it *);
6795 static next_element_function const get_next_element[NUM_IT_METHODS] =
6797 next_element_from_buffer,
6798 next_element_from_display_vector,
6799 next_element_from_string,
6800 next_element_from_c_string,
6801 next_element_from_image,
6802 next_element_from_stretch,
6803 next_element_from_xwidget,
6806 #define GET_NEXT_DISPLAY_ELEMENT(it) (*get_next_element[(it)->method]) (it)
6809 /* Return true iff a character at CHARPOS (and BYTEPOS) is composed
6810 (possibly with the following characters). */
6812 #define CHAR_COMPOSED_P(IT,CHARPOS,BYTEPOS,END_CHARPOS) \
6813 ((IT)->cmp_it.id >= 0 \
6814 || ((IT)->cmp_it.stop_pos == (CHARPOS) \
6815 && composition_reseat_it (&(IT)->cmp_it, CHARPOS, BYTEPOS, \
6816 END_CHARPOS, (IT)->w, \
6817 FACE_FROM_ID_OR_NULL ((IT)->f, \
6818 (IT)->face_id), \
6819 (IT)->string)))
6822 /* Lookup the char-table Vglyphless_char_display for character C (-1
6823 if we want information for no-font case), and return the display
6824 method symbol. By side-effect, update it->what and
6825 it->glyphless_method. This function is called from
6826 get_next_display_element for each character element, and from
6827 x_produce_glyphs when no suitable font was found. */
6829 Lisp_Object
6830 lookup_glyphless_char_display (int c, struct it *it)
6832 Lisp_Object glyphless_method = Qnil;
6834 if (CHAR_TABLE_P (Vglyphless_char_display)
6835 && CHAR_TABLE_EXTRA_SLOTS (XCHAR_TABLE (Vglyphless_char_display)) >= 1)
6837 if (c >= 0)
6839 glyphless_method = CHAR_TABLE_REF (Vglyphless_char_display, c);
6840 if (CONSP (glyphless_method))
6841 glyphless_method = FRAME_WINDOW_P (it->f)
6842 ? XCAR (glyphless_method)
6843 : XCDR (glyphless_method);
6845 else
6846 glyphless_method = XCHAR_TABLE (Vglyphless_char_display)->extras[0];
6849 retry:
6850 if (NILP (glyphless_method))
6852 if (c >= 0)
6853 /* The default is to display the character by a proper font. */
6854 return Qnil;
6855 /* The default for the no-font case is to display an empty box. */
6856 glyphless_method = Qempty_box;
6858 if (EQ (glyphless_method, Qzero_width))
6860 if (c >= 0)
6861 return glyphless_method;
6862 /* This method can't be used for the no-font case. */
6863 glyphless_method = Qempty_box;
6865 if (EQ (glyphless_method, Qthin_space))
6866 it->glyphless_method = GLYPHLESS_DISPLAY_THIN_SPACE;
6867 else if (EQ (glyphless_method, Qempty_box))
6868 it->glyphless_method = GLYPHLESS_DISPLAY_EMPTY_BOX;
6869 else if (EQ (glyphless_method, Qhex_code))
6870 it->glyphless_method = GLYPHLESS_DISPLAY_HEX_CODE;
6871 else if (STRINGP (glyphless_method))
6872 it->glyphless_method = GLYPHLESS_DISPLAY_ACRONYM;
6873 else
6875 /* Invalid value. We use the default method. */
6876 glyphless_method = Qnil;
6877 goto retry;
6879 it->what = IT_GLYPHLESS;
6880 return glyphless_method;
6883 /* Merge escape glyph face and cache the result. */
6885 static struct frame *last_escape_glyph_frame = NULL;
6886 static int last_escape_glyph_face_id = (1 << FACE_ID_BITS);
6887 static int last_escape_glyph_merged_face_id = 0;
6889 static int
6890 merge_escape_glyph_face (struct it *it)
6892 int face_id;
6894 if (it->f == last_escape_glyph_frame
6895 && it->face_id == last_escape_glyph_face_id)
6896 face_id = last_escape_glyph_merged_face_id;
6897 else
6899 /* Merge the `escape-glyph' face into the current face. */
6900 face_id = merge_faces (it->f, Qescape_glyph, 0, it->face_id);
6901 last_escape_glyph_frame = it->f;
6902 last_escape_glyph_face_id = it->face_id;
6903 last_escape_glyph_merged_face_id = face_id;
6905 return face_id;
6908 /* Likewise for glyphless glyph face. */
6910 static struct frame *last_glyphless_glyph_frame = NULL;
6911 static int last_glyphless_glyph_face_id = (1 << FACE_ID_BITS);
6912 static int last_glyphless_glyph_merged_face_id = 0;
6915 merge_glyphless_glyph_face (struct it *it)
6917 int face_id;
6919 if (it->f == last_glyphless_glyph_frame
6920 && it->face_id == last_glyphless_glyph_face_id)
6921 face_id = last_glyphless_glyph_merged_face_id;
6922 else
6924 /* Merge the `glyphless-char' face into the current face. */
6925 face_id = merge_faces (it->f, Qglyphless_char, 0, it->face_id);
6926 last_glyphless_glyph_frame = it->f;
6927 last_glyphless_glyph_face_id = it->face_id;
6928 last_glyphless_glyph_merged_face_id = face_id;
6930 return face_id;
6933 /* Forget the `escape-glyph' and `glyphless-char' faces. This should
6934 be called before redisplaying windows, and when the frame's face
6935 cache is freed. */
6936 void
6937 forget_escape_and_glyphless_faces (void)
6939 last_escape_glyph_frame = NULL;
6940 last_escape_glyph_face_id = (1 << FACE_ID_BITS);
6941 last_glyphless_glyph_frame = NULL;
6942 last_glyphless_glyph_face_id = (1 << FACE_ID_BITS);
6945 /* Load IT's display element fields with information about the next
6946 display element from the current position of IT. Value is false if
6947 end of buffer (or C string) is reached. */
6949 static bool
6950 get_next_display_element (struct it *it)
6952 /* True means that we found a display element. False means that
6953 we hit the end of what we iterate over. Performance note: the
6954 function pointer `method' used here turns out to be faster than
6955 using a sequence of if-statements. */
6956 bool success_p;
6958 get_next:
6959 success_p = GET_NEXT_DISPLAY_ELEMENT (it);
6961 if (it->what == IT_CHARACTER)
6963 /* UAX#9, L4: "A character is depicted by a mirrored glyph if
6964 and only if (a) the resolved directionality of that character
6965 is R..." */
6966 /* FIXME: Do we need an exception for characters from display
6967 tables? */
6968 if (it->bidi_p && it->bidi_it.type == STRONG_R
6969 && !inhibit_bidi_mirroring)
6970 it->c = bidi_mirror_char (it->c);
6971 /* Map via display table or translate control characters.
6972 IT->c, IT->len etc. have been set to the next character by
6973 the function call above. If we have a display table, and it
6974 contains an entry for IT->c, translate it. Don't do this if
6975 IT->c itself comes from a display table, otherwise we could
6976 end up in an infinite recursion. (An alternative could be to
6977 count the recursion depth of this function and signal an
6978 error when a certain maximum depth is reached.) Is it worth
6979 it? */
6980 if (success_p && it->dpvec == NULL)
6982 Lisp_Object dv;
6983 struct charset *unibyte = CHARSET_FROM_ID (charset_unibyte);
6984 bool nonascii_space_p = false;
6985 bool nonascii_hyphen_p = false;
6986 int c = it->c; /* This is the character to display. */
6988 if (! it->multibyte_p && ! ASCII_CHAR_P (c))
6990 eassert (SINGLE_BYTE_CHAR_P (c));
6991 if (unibyte_display_via_language_environment)
6993 c = DECODE_CHAR (unibyte, c);
6994 if (c < 0)
6995 c = BYTE8_TO_CHAR (it->c);
6997 else
6998 c = BYTE8_TO_CHAR (it->c);
7001 if (it->dp
7002 && (dv = DISP_CHAR_VECTOR (it->dp, c),
7003 VECTORP (dv)))
7005 struct Lisp_Vector *v = XVECTOR (dv);
7007 /* Return the first character from the display table
7008 entry, if not empty. If empty, don't display the
7009 current character. */
7010 if (v->header.size)
7012 it->dpvec_char_len = it->len;
7013 it->dpvec = v->contents;
7014 it->dpend = v->contents + v->header.size;
7015 it->current.dpvec_index = 0;
7016 it->dpvec_face_id = -1;
7017 it->saved_face_id = it->face_id;
7018 it->method = GET_FROM_DISPLAY_VECTOR;
7019 it->ellipsis_p = false;
7021 else
7023 set_iterator_to_next (it, false);
7025 goto get_next;
7028 if (! NILP (lookup_glyphless_char_display (c, it)))
7030 if (it->what == IT_GLYPHLESS)
7031 goto done;
7032 /* Don't display this character. */
7033 set_iterator_to_next (it, false);
7034 goto get_next;
7037 /* If `nobreak-char-display' is non-nil, we display
7038 non-ASCII spaces and hyphens specially. */
7039 if (! ASCII_CHAR_P (c) && ! NILP (Vnobreak_char_display))
7041 if (c == NO_BREAK_SPACE)
7042 nonascii_space_p = true;
7043 else if (c == SOFT_HYPHEN || c == HYPHEN
7044 || c == NON_BREAKING_HYPHEN)
7045 nonascii_hyphen_p = true;
7048 /* Translate control characters into `\003' or `^C' form.
7049 Control characters coming from a display table entry are
7050 currently not translated because we use IT->dpvec to hold
7051 the translation. This could easily be changed but I
7052 don't believe that it is worth doing.
7054 The characters handled by `nobreak-char-display' must be
7055 translated too.
7057 Non-printable characters and raw-byte characters are also
7058 translated to octal form. */
7059 if (((c < ' ' || c == 127) /* ASCII control chars. */
7060 ? (it->area != TEXT_AREA
7061 /* In mode line, treat \n, \t like other crl chars. */
7062 || (c != '\t'
7063 && it->glyph_row
7064 && (it->glyph_row->mode_line_p || it->avoid_cursor_p))
7065 || (c != '\n' && c != '\t'))
7066 : (nonascii_space_p
7067 || nonascii_hyphen_p
7068 || CHAR_BYTE8_P (c)
7069 || ! CHAR_PRINTABLE_P (c))))
7071 /* C is a control character, non-ASCII space/hyphen,
7072 raw-byte, or a non-printable character which must be
7073 displayed either as '\003' or as `^C' where the '\\'
7074 and '^' can be defined in the display table. Fill
7075 IT->ctl_chars with glyphs for what we have to
7076 display. Then, set IT->dpvec to these glyphs. */
7077 Lisp_Object gc;
7078 int ctl_len;
7079 int face_id;
7080 int lface_id = 0;
7081 int escape_glyph;
7083 /* Handle control characters with ^. */
7085 if (ASCII_CHAR_P (c) && it->ctl_arrow_p)
7087 int g;
7089 g = '^'; /* default glyph for Control */
7090 /* Set IT->ctl_chars[0] to the glyph for `^'. */
7091 if (it->dp
7092 && (gc = DISP_CTRL_GLYPH (it->dp), GLYPH_CODE_P (gc)))
7094 g = GLYPH_CODE_CHAR (gc);
7095 lface_id = GLYPH_CODE_FACE (gc);
7098 face_id = (lface_id
7099 ? merge_faces (it->f, Qt, lface_id, it->face_id)
7100 : merge_escape_glyph_face (it));
7102 XSETINT (it->ctl_chars[0], g);
7103 XSETINT (it->ctl_chars[1], c ^ 0100);
7104 ctl_len = 2;
7105 goto display_control;
7108 /* Handle non-ascii space in the mode where it only gets
7109 highlighting. */
7111 if (nonascii_space_p && EQ (Vnobreak_char_display, Qt))
7113 /* Merge `nobreak-space' into the current face. */
7114 face_id = merge_faces (it->f, Qnobreak_space, 0,
7115 it->face_id);
7116 XSETINT (it->ctl_chars[0], ' ');
7117 ctl_len = 1;
7118 goto display_control;
7121 /* Handle non-ascii hyphens in the mode where it only
7122 gets highlighting. */
7124 if (nonascii_hyphen_p && EQ (Vnobreak_char_display, Qt))
7126 /* Merge `nobreak-space' into the current face. */
7127 face_id = merge_faces (it->f, Qnobreak_hyphen, 0,
7128 it->face_id);
7129 XSETINT (it->ctl_chars[0], '-');
7130 ctl_len = 1;
7131 goto display_control;
7134 /* Handle sequences that start with the "escape glyph". */
7136 /* the default escape glyph is \. */
7137 escape_glyph = '\\';
7139 if (it->dp
7140 && (gc = DISP_ESCAPE_GLYPH (it->dp), GLYPH_CODE_P (gc)))
7142 escape_glyph = GLYPH_CODE_CHAR (gc);
7143 lface_id = GLYPH_CODE_FACE (gc);
7146 face_id = (lface_id
7147 ? merge_faces (it->f, Qt, lface_id, it->face_id)
7148 : merge_escape_glyph_face (it));
7150 /* Draw non-ASCII space/hyphen with escape glyph: */
7152 if (nonascii_space_p || nonascii_hyphen_p)
7154 XSETINT (it->ctl_chars[0], escape_glyph);
7155 XSETINT (it->ctl_chars[1], nonascii_space_p ? ' ' : '-');
7156 ctl_len = 2;
7157 goto display_control;
7161 char str[10];
7162 int len, i;
7164 if (CHAR_BYTE8_P (c))
7165 /* Display \200 instead of \17777600. */
7166 c = CHAR_TO_BYTE8 (c);
7167 len = sprintf (str, "%03o", c + 0u);
7169 XSETINT (it->ctl_chars[0], escape_glyph);
7170 for (i = 0; i < len; i++)
7171 XSETINT (it->ctl_chars[i + 1], str[i]);
7172 ctl_len = len + 1;
7175 display_control:
7176 /* Set up IT->dpvec and return first character from it. */
7177 it->dpvec_char_len = it->len;
7178 it->dpvec = it->ctl_chars;
7179 it->dpend = it->dpvec + ctl_len;
7180 it->current.dpvec_index = 0;
7181 it->dpvec_face_id = face_id;
7182 it->saved_face_id = it->face_id;
7183 it->method = GET_FROM_DISPLAY_VECTOR;
7184 it->ellipsis_p = false;
7185 goto get_next;
7187 it->char_to_display = c;
7189 else if (success_p)
7191 it->char_to_display = it->c;
7195 #ifdef HAVE_WINDOW_SYSTEM
7196 /* Adjust face id for a multibyte character. There are no multibyte
7197 character in unibyte text. */
7198 if ((it->what == IT_CHARACTER || it->what == IT_COMPOSITION)
7199 && it->multibyte_p
7200 && success_p
7201 && FRAME_WINDOW_P (it->f))
7203 struct face *face = FACE_FROM_ID (it->f, it->face_id);
7205 if (it->what == IT_COMPOSITION && it->cmp_it.ch >= 0)
7207 /* Automatic composition with glyph-string. */
7208 Lisp_Object gstring = composition_gstring_from_id (it->cmp_it.id);
7210 it->face_id = face_for_font (it->f, LGSTRING_FONT (gstring), face);
7212 else
7214 ptrdiff_t pos = (it->s ? -1
7215 : STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
7216 : IT_CHARPOS (*it));
7217 int c;
7219 if (it->what == IT_CHARACTER)
7220 c = it->char_to_display;
7221 else
7223 struct composition *cmp = composition_table[it->cmp_it.id];
7224 int i;
7226 c = ' ';
7227 for (i = 0; i < cmp->glyph_len; i++)
7228 /* TAB in a composition means display glyphs with
7229 padding space on the left or right. */
7230 if ((c = COMPOSITION_GLYPH (cmp, i)) != '\t')
7231 break;
7233 it->face_id = FACE_FOR_CHAR (it->f, face, c, pos, it->string);
7236 #endif /* HAVE_WINDOW_SYSTEM */
7238 done:
7239 /* Is this character the last one of a run of characters with
7240 box? If yes, set IT->end_of_box_run_p to true. */
7241 if (it->face_box_p
7242 && it->s == NULL)
7244 if (it->method == GET_FROM_STRING && it->sp)
7246 int face_id = underlying_face_id (it);
7247 struct face *face = FACE_FROM_ID_OR_NULL (it->f, face_id);
7249 if (face)
7251 if (face->box == FACE_NO_BOX)
7253 /* If the box comes from face properties in a
7254 display string, check faces in that string. */
7255 int string_face_id = face_after_it_pos (it);
7256 it->end_of_box_run_p
7257 = (FACE_FROM_ID (it->f, string_face_id)->box
7258 == FACE_NO_BOX);
7260 /* Otherwise, the box comes from the underlying face.
7261 If this is the last string character displayed, check
7262 the next buffer location. */
7263 else if ((IT_STRING_CHARPOS (*it) >= SCHARS (it->string) - 1)
7264 /* n_overlay_strings is unreliable unless
7265 overlay_string_index is non-negative. */
7266 && ((it->current.overlay_string_index >= 0
7267 && (it->current.overlay_string_index
7268 == it->n_overlay_strings - 1))
7269 /* A string from display property. */
7270 || it->from_disp_prop_p))
7272 ptrdiff_t ignore;
7273 int next_face_id;
7274 bool text_from_string = false;
7275 /* Normally, the next buffer location is stored in
7276 IT->current.pos... */
7277 struct text_pos pos = it->current.pos;
7279 /* ...but for a string from a display property, the
7280 next buffer position is stored in the 'position'
7281 member of the iteration stack slot below the
7282 current one, see handle_single_display_spec. By
7283 contrast, it->current.pos was not yet updated to
7284 point to that buffer position; that will happen
7285 in pop_it, after we finish displaying the current
7286 string. Note that we already checked above that
7287 it->sp is positive, so subtracting one from it is
7288 safe. */
7289 if (it->from_disp_prop_p)
7291 int stackp = it->sp - 1;
7293 /* Find the stack level with data from buffer. */
7294 while (stackp >= 0
7295 && STRINGP ((it->stack + stackp)->string))
7296 stackp--;
7297 if (stackp < 0)
7299 /* If no stack slot was found for iterating
7300 a buffer, we are displaying text from a
7301 string, most probably the mode line or
7302 the header line, and that string has a
7303 display string on some of its
7304 characters. */
7305 text_from_string = true;
7306 pos = it->stack[it->sp - 1].position;
7308 else
7309 pos = (it->stack + stackp)->position;
7311 else
7312 INC_TEXT_POS (pos, it->multibyte_p);
7314 if (text_from_string)
7316 Lisp_Object base_string = it->stack[it->sp - 1].string;
7318 if (CHARPOS (pos) >= SCHARS (base_string) - 1)
7319 it->end_of_box_run_p = true;
7320 else
7322 next_face_id
7323 = face_at_string_position (it->w, base_string,
7324 CHARPOS (pos), 0,
7325 &ignore, face_id, false);
7326 it->end_of_box_run_p
7327 = (FACE_FROM_ID (it->f, next_face_id)->box
7328 == FACE_NO_BOX);
7331 else if (CHARPOS (pos) >= ZV)
7332 it->end_of_box_run_p = true;
7333 else
7335 next_face_id =
7336 face_at_buffer_position (it->w, CHARPOS (pos), &ignore,
7337 CHARPOS (pos)
7338 + TEXT_PROP_DISTANCE_LIMIT,
7339 false, -1);
7340 it->end_of_box_run_p
7341 = (FACE_FROM_ID (it->f, next_face_id)->box
7342 == FACE_NO_BOX);
7347 /* next_element_from_display_vector sets this flag according to
7348 faces of the display vector glyphs, see there. */
7349 else if (it->method != GET_FROM_DISPLAY_VECTOR)
7351 int face_id = face_after_it_pos (it);
7352 it->end_of_box_run_p
7353 = (face_id != it->face_id
7354 && FACE_FROM_ID (it->f, face_id)->box == FACE_NO_BOX);
7357 /* If we reached the end of the object we've been iterating (e.g., a
7358 display string or an overlay string), and there's something on
7359 IT->stack, proceed with what's on the stack. It doesn't make
7360 sense to return false if there's unprocessed stuff on the stack,
7361 because otherwise that stuff will never be displayed. */
7362 if (!success_p && it->sp > 0)
7364 set_iterator_to_next (it, false);
7365 success_p = get_next_display_element (it);
7368 /* Value is false if end of buffer or string reached. */
7369 return success_p;
7373 /* Move IT to the next display element.
7375 RESEAT_P means if called on a newline in buffer text,
7376 skip to the next visible line start.
7378 Functions get_next_display_element and set_iterator_to_next are
7379 separate because I find this arrangement easier to handle than a
7380 get_next_display_element function that also increments IT's
7381 position. The way it is we can first look at an iterator's current
7382 display element, decide whether it fits on a line, and if it does,
7383 increment the iterator position. The other way around we probably
7384 would either need a flag indicating whether the iterator has to be
7385 incremented the next time, or we would have to implement a
7386 decrement position function which would not be easy to write. */
7388 void
7389 set_iterator_to_next (struct it *it, bool reseat_p)
7391 /* Reset flags indicating start and end of a sequence of characters
7392 with box. Reset them at the start of this function because
7393 moving the iterator to a new position might set them. */
7394 it->start_of_box_run_p = it->end_of_box_run_p = false;
7396 switch (it->method)
7398 case GET_FROM_BUFFER:
7399 /* The current display element of IT is a character from
7400 current_buffer. Advance in the buffer, and maybe skip over
7401 invisible lines that are so because of selective display. */
7402 if (ITERATOR_AT_END_OF_LINE_P (it) && reseat_p)
7403 reseat_at_next_visible_line_start (it, false);
7404 else if (it->cmp_it.id >= 0)
7406 /* We are currently getting glyphs from a composition. */
7407 if (! it->bidi_p)
7409 IT_CHARPOS (*it) += it->cmp_it.nchars;
7410 IT_BYTEPOS (*it) += it->cmp_it.nbytes;
7412 else
7414 int i;
7416 /* Update IT's char/byte positions to point to the first
7417 character of the next grapheme cluster, or to the
7418 character visually after the current composition. */
7419 for (i = 0; i < it->cmp_it.nchars; i++)
7420 bidi_move_to_visually_next (&it->bidi_it);
7421 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7422 IT_CHARPOS (*it) = it->bidi_it.charpos;
7425 if ((! it->bidi_p || ! it->cmp_it.reversed_p)
7426 && it->cmp_it.to < it->cmp_it.nglyphs)
7428 /* Composition created while scanning forward. Proceed
7429 to the next grapheme cluster. */
7430 it->cmp_it.from = it->cmp_it.to;
7432 else if ((it->bidi_p && it->cmp_it.reversed_p)
7433 && it->cmp_it.from > 0)
7435 /* Composition created while scanning backward. Proceed
7436 to the previous grapheme cluster. */
7437 it->cmp_it.to = it->cmp_it.from;
7439 else
7441 /* No more grapheme clusters in this composition.
7442 Find the next stop position. */
7443 ptrdiff_t stop = it->end_charpos;
7445 if (it->bidi_it.scan_dir < 0)
7446 /* Now we are scanning backward and don't know
7447 where to stop. */
7448 stop = -1;
7449 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
7450 IT_BYTEPOS (*it), stop, Qnil);
7453 else
7455 eassert (it->len != 0);
7457 if (!it->bidi_p)
7459 IT_BYTEPOS (*it) += it->len;
7460 IT_CHARPOS (*it) += 1;
7462 else
7464 int prev_scan_dir = it->bidi_it.scan_dir;
7465 /* If this is a new paragraph, determine its base
7466 direction (a.k.a. its base embedding level). */
7467 if (it->bidi_it.new_paragraph)
7468 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it,
7469 false);
7470 bidi_move_to_visually_next (&it->bidi_it);
7471 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7472 IT_CHARPOS (*it) = it->bidi_it.charpos;
7473 if (prev_scan_dir != it->bidi_it.scan_dir)
7475 /* As the scan direction was changed, we must
7476 re-compute the stop position for composition. */
7477 ptrdiff_t stop = it->end_charpos;
7478 if (it->bidi_it.scan_dir < 0)
7479 stop = -1;
7480 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
7481 IT_BYTEPOS (*it), stop, Qnil);
7484 eassert (IT_BYTEPOS (*it) == CHAR_TO_BYTE (IT_CHARPOS (*it)));
7486 break;
7488 case GET_FROM_C_STRING:
7489 /* Current display element of IT is from a C string. */
7490 if (!it->bidi_p
7491 /* If the string position is beyond string's end, it means
7492 next_element_from_c_string is padding the string with
7493 blanks, in which case we bypass the bidi iterator,
7494 because it cannot deal with such virtual characters. */
7495 || IT_CHARPOS (*it) >= it->bidi_it.string.schars)
7497 IT_BYTEPOS (*it) += it->len;
7498 IT_CHARPOS (*it) += 1;
7500 else
7502 bidi_move_to_visually_next (&it->bidi_it);
7503 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7504 IT_CHARPOS (*it) = it->bidi_it.charpos;
7506 break;
7508 case GET_FROM_DISPLAY_VECTOR:
7509 /* Current display element of IT is from a display table entry.
7510 Advance in the display table definition. Reset it to null if
7511 end reached, and continue with characters from buffers/
7512 strings. */
7513 ++it->current.dpvec_index;
7515 /* Restore face of the iterator to what they were before the
7516 display vector entry (these entries may contain faces). */
7517 it->face_id = it->saved_face_id;
7519 if (it->dpvec + it->current.dpvec_index >= it->dpend)
7521 bool recheck_faces = it->ellipsis_p;
7523 if (it->s)
7524 it->method = GET_FROM_C_STRING;
7525 else if (STRINGP (it->string))
7526 it->method = GET_FROM_STRING;
7527 else
7529 it->method = GET_FROM_BUFFER;
7530 it->object = it->w->contents;
7533 it->dpvec = NULL;
7534 it->current.dpvec_index = -1;
7536 /* Skip over characters which were displayed via IT->dpvec. */
7537 if (it->dpvec_char_len < 0)
7538 reseat_at_next_visible_line_start (it, true);
7539 else if (it->dpvec_char_len > 0)
7541 it->len = it->dpvec_char_len;
7542 set_iterator_to_next (it, reseat_p);
7545 /* Maybe recheck faces after display vector. */
7546 if (recheck_faces)
7548 if (it->method == GET_FROM_STRING)
7549 it->stop_charpos = IT_STRING_CHARPOS (*it);
7550 else
7551 it->stop_charpos = IT_CHARPOS (*it);
7554 break;
7556 case GET_FROM_STRING:
7557 /* Current display element is a character from a Lisp string. */
7558 eassert (it->s == NULL && STRINGP (it->string));
7559 /* Don't advance past string end. These conditions are true
7560 when set_iterator_to_next is called at the end of
7561 get_next_display_element, in which case the Lisp string is
7562 already exhausted, and all we want is pop the iterator
7563 stack. */
7564 if (it->current.overlay_string_index >= 0)
7566 /* This is an overlay string, so there's no padding with
7567 spaces, and the number of characters in the string is
7568 where the string ends. */
7569 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7570 goto consider_string_end;
7572 else
7574 /* Not an overlay string. There could be padding, so test
7575 against it->end_charpos. */
7576 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
7577 goto consider_string_end;
7579 if (it->cmp_it.id >= 0)
7581 /* We are delivering display elements from a composition.
7582 Update the string position past the grapheme cluster
7583 we've just processed. */
7584 if (! it->bidi_p)
7586 IT_STRING_CHARPOS (*it) += it->cmp_it.nchars;
7587 IT_STRING_BYTEPOS (*it) += it->cmp_it.nbytes;
7589 else
7591 int i;
7593 for (i = 0; i < it->cmp_it.nchars; i++)
7594 bidi_move_to_visually_next (&it->bidi_it);
7595 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7596 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7599 /* Did we exhaust all the grapheme clusters of this
7600 composition? */
7601 if ((! it->bidi_p || ! it->cmp_it.reversed_p)
7602 && (it->cmp_it.to < it->cmp_it.nglyphs))
7604 /* Not all the grapheme clusters were processed yet;
7605 advance to the next cluster. */
7606 it->cmp_it.from = it->cmp_it.to;
7608 else if ((it->bidi_p && it->cmp_it.reversed_p)
7609 && it->cmp_it.from > 0)
7611 /* Likewise: advance to the next cluster, but going in
7612 the reverse direction. */
7613 it->cmp_it.to = it->cmp_it.from;
7615 else
7617 /* This composition was fully processed; find the next
7618 candidate place for checking for composed
7619 characters. */
7620 /* Always limit string searches to the string length;
7621 any padding spaces are not part of the string, and
7622 there cannot be any compositions in that padding. */
7623 ptrdiff_t stop = SCHARS (it->string);
7625 if (it->bidi_p && it->bidi_it.scan_dir < 0)
7626 stop = -1;
7627 else if (it->end_charpos < stop)
7629 /* Cf. PRECISION in reseat_to_string: we might be
7630 limited in how many of the string characters we
7631 need to deliver. */
7632 stop = it->end_charpos;
7634 composition_compute_stop_pos (&it->cmp_it,
7635 IT_STRING_CHARPOS (*it),
7636 IT_STRING_BYTEPOS (*it), stop,
7637 it->string);
7640 else
7642 if (!it->bidi_p
7643 /* If the string position is beyond string's end, it
7644 means next_element_from_string is padding the string
7645 with blanks, in which case we bypass the bidi
7646 iterator, because it cannot deal with such virtual
7647 characters. */
7648 || IT_STRING_CHARPOS (*it) >= it->bidi_it.string.schars)
7650 IT_STRING_BYTEPOS (*it) += it->len;
7651 IT_STRING_CHARPOS (*it) += 1;
7653 else
7655 int prev_scan_dir = it->bidi_it.scan_dir;
7657 bidi_move_to_visually_next (&it->bidi_it);
7658 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7659 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7660 /* If the scan direction changes, we may need to update
7661 the place where to check for composed characters. */
7662 if (prev_scan_dir != it->bidi_it.scan_dir)
7664 ptrdiff_t stop = SCHARS (it->string);
7666 if (it->bidi_it.scan_dir < 0)
7667 stop = -1;
7668 else if (it->end_charpos < stop)
7669 stop = it->end_charpos;
7671 composition_compute_stop_pos (&it->cmp_it,
7672 IT_STRING_CHARPOS (*it),
7673 IT_STRING_BYTEPOS (*it), stop,
7674 it->string);
7679 consider_string_end:
7681 if (it->current.overlay_string_index >= 0)
7683 /* IT->string is an overlay string. Advance to the
7684 next, if there is one. */
7685 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7687 it->ellipsis_p = false;
7688 next_overlay_string (it);
7689 if (it->ellipsis_p)
7690 setup_for_ellipsis (it, 0);
7693 else
7695 /* IT->string is not an overlay string. If we reached
7696 its end, and there is something on IT->stack, proceed
7697 with what is on the stack. This can be either another
7698 string, this time an overlay string, or a buffer. */
7699 if (IT_STRING_CHARPOS (*it) == SCHARS (it->string)
7700 && it->sp > 0)
7702 pop_it (it);
7703 if (it->method == GET_FROM_STRING)
7704 goto consider_string_end;
7707 break;
7709 case GET_FROM_IMAGE:
7710 case GET_FROM_STRETCH:
7711 case GET_FROM_XWIDGET:
7713 /* The position etc with which we have to proceed are on
7714 the stack. The position may be at the end of a string,
7715 if the `display' property takes up the whole string. */
7716 eassert (it->sp > 0);
7717 pop_it (it);
7718 if (it->method == GET_FROM_STRING)
7719 goto consider_string_end;
7720 break;
7722 default:
7723 /* There are no other methods defined, so this should be a bug. */
7724 emacs_abort ();
7727 eassert (it->method != GET_FROM_STRING
7728 || (STRINGP (it->string)
7729 && IT_STRING_CHARPOS (*it) >= 0));
7732 /* Load IT's display element fields with information about the next
7733 display element which comes from a display table entry or from the
7734 result of translating a control character to one of the forms `^C'
7735 or `\003'.
7737 IT->dpvec holds the glyphs to return as characters.
7738 IT->saved_face_id holds the face id before the display vector--it
7739 is restored into IT->face_id in set_iterator_to_next. */
7741 static bool
7742 next_element_from_display_vector (struct it *it)
7744 Lisp_Object gc;
7745 int prev_face_id = it->face_id;
7746 int next_face_id;
7748 /* Precondition. */
7749 eassert (it->dpvec && it->current.dpvec_index >= 0);
7751 it->face_id = it->saved_face_id;
7753 /* KFS: This code used to check ip->dpvec[0] instead of the current element.
7754 That seemed totally bogus - so I changed it... */
7755 gc = it->dpvec[it->current.dpvec_index];
7757 if (GLYPH_CODE_P (gc))
7759 struct face *this_face, *prev_face, *next_face;
7761 it->c = GLYPH_CODE_CHAR (gc);
7762 it->len = CHAR_BYTES (it->c);
7764 /* The entry may contain a face id to use. Such a face id is
7765 the id of a Lisp face, not a realized face. A face id of
7766 zero means no face is specified. */
7767 if (it->dpvec_face_id >= 0)
7768 it->face_id = it->dpvec_face_id;
7769 else
7771 int lface_id = GLYPH_CODE_FACE (gc);
7772 if (lface_id > 0)
7773 it->face_id = merge_faces (it->f, Qt, lface_id,
7774 it->saved_face_id);
7777 /* Glyphs in the display vector could have the box face, so we
7778 need to set the related flags in the iterator, as
7779 appropriate. */
7780 this_face = FACE_FROM_ID_OR_NULL (it->f, it->face_id);
7781 prev_face = FACE_FROM_ID_OR_NULL (it->f, prev_face_id);
7783 /* Is this character the first character of a box-face run? */
7784 it->start_of_box_run_p = (this_face && this_face->box != FACE_NO_BOX
7785 && (!prev_face
7786 || prev_face->box == FACE_NO_BOX));
7788 /* For the last character of the box-face run, we need to look
7789 either at the next glyph from the display vector, or at the
7790 face we saw before the display vector. */
7791 next_face_id = it->saved_face_id;
7792 if (it->current.dpvec_index < it->dpend - it->dpvec - 1)
7794 if (it->dpvec_face_id >= 0)
7795 next_face_id = it->dpvec_face_id;
7796 else
7798 int lface_id =
7799 GLYPH_CODE_FACE (it->dpvec[it->current.dpvec_index + 1]);
7801 if (lface_id > 0)
7802 next_face_id = merge_faces (it->f, Qt, lface_id,
7803 it->saved_face_id);
7806 next_face = FACE_FROM_ID_OR_NULL (it->f, next_face_id);
7807 it->end_of_box_run_p = (this_face && this_face->box != FACE_NO_BOX
7808 && (!next_face
7809 || next_face->box == FACE_NO_BOX));
7810 it->face_box_p = this_face && this_face->box != FACE_NO_BOX;
7812 else
7813 /* Display table entry is invalid. Return a space. */
7814 it->c = ' ', it->len = 1;
7816 /* Don't change position and object of the iterator here. They are
7817 still the values of the character that had this display table
7818 entry or was translated, and that's what we want. */
7819 it->what = IT_CHARACTER;
7820 return true;
7823 /* Get the first element of string/buffer in the visual order, after
7824 being reseated to a new position in a string or a buffer. */
7825 static void
7826 get_visually_first_element (struct it *it)
7828 bool string_p = STRINGP (it->string) || it->s;
7829 ptrdiff_t eob = (string_p ? it->bidi_it.string.schars : ZV);
7830 ptrdiff_t bob = (string_p ? 0 : BEGV);
7832 if (STRINGP (it->string))
7834 it->bidi_it.charpos = IT_STRING_CHARPOS (*it);
7835 it->bidi_it.bytepos = IT_STRING_BYTEPOS (*it);
7837 else
7839 it->bidi_it.charpos = IT_CHARPOS (*it);
7840 it->bidi_it.bytepos = IT_BYTEPOS (*it);
7843 if (it->bidi_it.charpos == eob)
7845 /* Nothing to do, but reset the FIRST_ELT flag, like
7846 bidi_paragraph_init does, because we are not going to
7847 call it. */
7848 it->bidi_it.first_elt = false;
7850 else if (it->bidi_it.charpos == bob
7851 || (!string_p
7852 && (FETCH_CHAR (it->bidi_it.bytepos - 1) == '\n'
7853 || FETCH_CHAR (it->bidi_it.bytepos) == '\n')))
7855 /* If we are at the beginning of a line/string, we can produce
7856 the next element right away. */
7857 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, true);
7858 bidi_move_to_visually_next (&it->bidi_it);
7860 else
7862 ptrdiff_t orig_bytepos = it->bidi_it.bytepos;
7864 /* We need to prime the bidi iterator starting at the line's or
7865 string's beginning, before we will be able to produce the
7866 next element. */
7867 if (string_p)
7868 it->bidi_it.charpos = it->bidi_it.bytepos = 0;
7869 else
7870 it->bidi_it.charpos = find_newline_no_quit (IT_CHARPOS (*it),
7871 IT_BYTEPOS (*it), -1,
7872 &it->bidi_it.bytepos);
7873 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, true);
7876 /* Now return to buffer/string position where we were asked
7877 to get the next display element, and produce that. */
7878 bidi_move_to_visually_next (&it->bidi_it);
7880 while (it->bidi_it.bytepos != orig_bytepos
7881 && it->bidi_it.charpos < eob);
7884 /* Adjust IT's position information to where we ended up. */
7885 if (STRINGP (it->string))
7887 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7888 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7890 else
7892 IT_CHARPOS (*it) = it->bidi_it.charpos;
7893 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7896 if (STRINGP (it->string) || !it->s)
7898 ptrdiff_t stop, charpos, bytepos;
7900 if (STRINGP (it->string))
7902 eassert (!it->s);
7903 stop = SCHARS (it->string);
7904 if (stop > it->end_charpos)
7905 stop = it->end_charpos;
7906 charpos = IT_STRING_CHARPOS (*it);
7907 bytepos = IT_STRING_BYTEPOS (*it);
7909 else
7911 stop = it->end_charpos;
7912 charpos = IT_CHARPOS (*it);
7913 bytepos = IT_BYTEPOS (*it);
7915 if (it->bidi_it.scan_dir < 0)
7916 stop = -1;
7917 composition_compute_stop_pos (&it->cmp_it, charpos, bytepos, stop,
7918 it->string);
7922 /* Load IT with the next display element from Lisp string IT->string.
7923 IT->current.string_pos is the current position within the string.
7924 If IT->current.overlay_string_index >= 0, the Lisp string is an
7925 overlay string. */
7927 static bool
7928 next_element_from_string (struct it *it)
7930 struct text_pos position;
7932 eassert (STRINGP (it->string));
7933 eassert (!it->bidi_p || EQ (it->string, it->bidi_it.string.lstring));
7934 eassert (IT_STRING_CHARPOS (*it) >= 0);
7935 position = it->current.string_pos;
7937 /* With bidi reordering, the character to display might not be the
7938 character at IT_STRING_CHARPOS. BIDI_IT.FIRST_ELT means
7939 that we were reseat()ed to a new string, whose paragraph
7940 direction is not known. */
7941 if (it->bidi_p && it->bidi_it.first_elt)
7943 get_visually_first_element (it);
7944 SET_TEXT_POS (position, IT_STRING_CHARPOS (*it), IT_STRING_BYTEPOS (*it));
7947 /* Time to check for invisible text? */
7948 if (IT_STRING_CHARPOS (*it) < it->end_charpos)
7950 if (IT_STRING_CHARPOS (*it) >= it->stop_charpos)
7952 if (!(!it->bidi_p
7953 || BIDI_AT_BASE_LEVEL (it->bidi_it)
7954 || IT_STRING_CHARPOS (*it) == it->stop_charpos))
7956 /* With bidi non-linear iteration, we could find
7957 ourselves far beyond the last computed stop_charpos,
7958 with several other stop positions in between that we
7959 missed. Scan them all now, in buffer's logical
7960 order, until we find and handle the last stop_charpos
7961 that precedes our current position. */
7962 handle_stop_backwards (it, it->stop_charpos);
7963 return GET_NEXT_DISPLAY_ELEMENT (it);
7965 else
7967 if (it->bidi_p)
7969 /* Take note of the stop position we just moved
7970 across, for when we will move back across it. */
7971 it->prev_stop = it->stop_charpos;
7972 /* If we are at base paragraph embedding level, take
7973 note of the last stop position seen at this
7974 level. */
7975 if (BIDI_AT_BASE_LEVEL (it->bidi_it))
7976 it->base_level_stop = it->stop_charpos;
7978 handle_stop (it);
7980 /* Since a handler may have changed IT->method, we must
7981 recurse here. */
7982 return GET_NEXT_DISPLAY_ELEMENT (it);
7985 else if (it->bidi_p
7986 /* If we are before prev_stop, we may have overstepped
7987 on our way backwards a stop_pos, and if so, we need
7988 to handle that stop_pos. */
7989 && IT_STRING_CHARPOS (*it) < it->prev_stop
7990 /* We can sometimes back up for reasons that have nothing
7991 to do with bidi reordering. E.g., compositions. The
7992 code below is only needed when we are above the base
7993 embedding level, so test for that explicitly. */
7994 && !BIDI_AT_BASE_LEVEL (it->bidi_it))
7996 /* If we lost track of base_level_stop, we have no better
7997 place for handle_stop_backwards to start from than string
7998 beginning. This happens, e.g., when we were reseated to
7999 the previous screenful of text by vertical-motion. */
8000 if (it->base_level_stop <= 0
8001 || IT_STRING_CHARPOS (*it) < it->base_level_stop)
8002 it->base_level_stop = 0;
8003 handle_stop_backwards (it, it->base_level_stop);
8004 return GET_NEXT_DISPLAY_ELEMENT (it);
8008 if (it->current.overlay_string_index >= 0)
8010 /* Get the next character from an overlay string. In overlay
8011 strings, there is no field width or padding with spaces to
8012 do. */
8013 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
8015 it->what = IT_EOB;
8016 return false;
8018 else if (CHAR_COMPOSED_P (it, IT_STRING_CHARPOS (*it),
8019 IT_STRING_BYTEPOS (*it),
8020 it->bidi_it.scan_dir < 0
8021 ? -1
8022 : SCHARS (it->string))
8023 && next_element_from_composition (it))
8025 return true;
8027 else if (STRING_MULTIBYTE (it->string))
8029 const unsigned char *s = (SDATA (it->string)
8030 + IT_STRING_BYTEPOS (*it));
8031 it->c = string_char_and_length (s, &it->len);
8033 else
8035 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
8036 it->len = 1;
8039 else
8041 /* Get the next character from a Lisp string that is not an
8042 overlay string. Such strings come from the mode line, for
8043 example. We may have to pad with spaces, or truncate the
8044 string. See also next_element_from_c_string. */
8045 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
8047 it->what = IT_EOB;
8048 return false;
8050 else if (IT_STRING_CHARPOS (*it) >= it->string_nchars)
8052 /* Pad with spaces. */
8053 it->c = ' ', it->len = 1;
8054 CHARPOS (position) = BYTEPOS (position) = -1;
8056 else if (CHAR_COMPOSED_P (it, IT_STRING_CHARPOS (*it),
8057 IT_STRING_BYTEPOS (*it),
8058 it->bidi_it.scan_dir < 0
8059 ? -1
8060 : it->string_nchars)
8061 && next_element_from_composition (it))
8063 return true;
8065 else if (STRING_MULTIBYTE (it->string))
8067 const unsigned char *s = (SDATA (it->string)
8068 + IT_STRING_BYTEPOS (*it));
8069 it->c = string_char_and_length (s, &it->len);
8071 else
8073 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
8074 it->len = 1;
8078 /* Record what we have and where it came from. */
8079 it->what = IT_CHARACTER;
8080 it->object = it->string;
8081 it->position = position;
8082 return true;
8086 /* Load IT with next display element from C string IT->s.
8087 IT->string_nchars is the maximum number of characters to return
8088 from the string. IT->end_charpos may be greater than
8089 IT->string_nchars when this function is called, in which case we
8090 may have to return padding spaces. Value is false if end of string
8091 reached, including padding spaces. */
8093 static bool
8094 next_element_from_c_string (struct it *it)
8096 bool success_p = true;
8098 eassert (it->s);
8099 eassert (!it->bidi_p || it->s == it->bidi_it.string.s);
8100 it->what = IT_CHARACTER;
8101 BYTEPOS (it->position) = CHARPOS (it->position) = 0;
8102 it->object = make_number (0);
8104 /* With bidi reordering, the character to display might not be the
8105 character at IT_CHARPOS. BIDI_IT.FIRST_ELT means that
8106 we were reseated to a new string, whose paragraph direction is
8107 not known. */
8108 if (it->bidi_p && it->bidi_it.first_elt)
8109 get_visually_first_element (it);
8111 /* IT's position can be greater than IT->string_nchars in case a
8112 field width or precision has been specified when the iterator was
8113 initialized. */
8114 if (IT_CHARPOS (*it) >= it->end_charpos)
8116 /* End of the game. */
8117 it->what = IT_EOB;
8118 success_p = false;
8120 else if (IT_CHARPOS (*it) >= it->string_nchars)
8122 /* Pad with spaces. */
8123 it->c = ' ', it->len = 1;
8124 BYTEPOS (it->position) = CHARPOS (it->position) = -1;
8126 else if (it->multibyte_p)
8127 it->c = string_char_and_length (it->s + IT_BYTEPOS (*it), &it->len);
8128 else
8129 it->c = it->s[IT_BYTEPOS (*it)], it->len = 1;
8131 return success_p;
8135 /* Set up IT to return characters from an ellipsis, if appropriate.
8136 The definition of the ellipsis glyphs may come from a display table
8137 entry. This function fills IT with the first glyph from the
8138 ellipsis if an ellipsis is to be displayed. */
8140 static bool
8141 next_element_from_ellipsis (struct it *it)
8143 if (it->selective_display_ellipsis_p)
8144 setup_for_ellipsis (it, it->len);
8145 else
8147 /* The face at the current position may be different from the
8148 face we find after the invisible text. Remember what it
8149 was in IT->saved_face_id, and signal that it's there by
8150 setting face_before_selective_p. */
8151 it->saved_face_id = it->face_id;
8152 it->method = GET_FROM_BUFFER;
8153 it->object = it->w->contents;
8154 reseat_at_next_visible_line_start (it, true);
8155 it->face_before_selective_p = true;
8158 return GET_NEXT_DISPLAY_ELEMENT (it);
8162 /* Deliver an image display element. The iterator IT is already
8163 filled with image information (done in handle_display_prop). Value
8164 is always true. */
8167 static bool
8168 next_element_from_image (struct it *it)
8170 it->what = IT_IMAGE;
8171 return true;
8174 static bool
8175 next_element_from_xwidget (struct it *it)
8177 it->what = IT_XWIDGET;
8178 return true;
8182 /* Fill iterator IT with next display element from a stretch glyph
8183 property. IT->object is the value of the text property. Value is
8184 always true. */
8186 static bool
8187 next_element_from_stretch (struct it *it)
8189 it->what = IT_STRETCH;
8190 return true;
8193 /* Scan backwards from IT's current position until we find a stop
8194 position, or until BEGV. This is called when we find ourself
8195 before both the last known prev_stop and base_level_stop while
8196 reordering bidirectional text. */
8198 static void
8199 compute_stop_pos_backwards (struct it *it)
8201 const int SCAN_BACK_LIMIT = 1000;
8202 struct text_pos pos;
8203 struct display_pos save_current = it->current;
8204 struct text_pos save_position = it->position;
8205 ptrdiff_t charpos = IT_CHARPOS (*it);
8206 ptrdiff_t where_we_are = charpos;
8207 ptrdiff_t save_stop_pos = it->stop_charpos;
8208 ptrdiff_t save_end_pos = it->end_charpos;
8210 eassert (NILP (it->string) && !it->s);
8211 eassert (it->bidi_p);
8212 it->bidi_p = false;
8215 it->end_charpos = min (charpos + 1, ZV);
8216 charpos = max (charpos - SCAN_BACK_LIMIT, BEGV);
8217 SET_TEXT_POS (pos, charpos, CHAR_TO_BYTE (charpos));
8218 reseat_1 (it, pos, false);
8219 compute_stop_pos (it);
8220 /* We must advance forward, right? */
8221 if (it->stop_charpos <= charpos)
8222 emacs_abort ();
8224 while (charpos > BEGV && it->stop_charpos >= it->end_charpos);
8226 if (it->stop_charpos <= where_we_are)
8227 it->prev_stop = it->stop_charpos;
8228 else
8229 it->prev_stop = BEGV;
8230 it->bidi_p = true;
8231 it->current = save_current;
8232 it->position = save_position;
8233 it->stop_charpos = save_stop_pos;
8234 it->end_charpos = save_end_pos;
8237 /* Scan forward from CHARPOS in the current buffer/string, until we
8238 find a stop position > current IT's position. Then handle the stop
8239 position before that. This is called when we bump into a stop
8240 position while reordering bidirectional text. CHARPOS should be
8241 the last previously processed stop_pos (or BEGV/0, if none were
8242 processed yet) whose position is less that IT's current
8243 position. */
8245 static void
8246 handle_stop_backwards (struct it *it, ptrdiff_t charpos)
8248 bool bufp = !STRINGP (it->string);
8249 ptrdiff_t where_we_are = (bufp ? IT_CHARPOS (*it) : IT_STRING_CHARPOS (*it));
8250 struct display_pos save_current = it->current;
8251 struct text_pos save_position = it->position;
8252 struct text_pos pos1;
8253 ptrdiff_t next_stop;
8255 /* Scan in strict logical order. */
8256 eassert (it->bidi_p);
8257 it->bidi_p = false;
8260 it->prev_stop = charpos;
8261 if (bufp)
8263 SET_TEXT_POS (pos1, charpos, CHAR_TO_BYTE (charpos));
8264 reseat_1 (it, pos1, false);
8266 else
8267 it->current.string_pos = string_pos (charpos, it->string);
8268 compute_stop_pos (it);
8269 /* We must advance forward, right? */
8270 if (it->stop_charpos <= it->prev_stop)
8271 emacs_abort ();
8272 charpos = it->stop_charpos;
8274 while (charpos <= where_we_are);
8276 it->bidi_p = true;
8277 it->current = save_current;
8278 it->position = save_position;
8279 next_stop = it->stop_charpos;
8280 it->stop_charpos = it->prev_stop;
8281 handle_stop (it);
8282 it->stop_charpos = next_stop;
8285 /* Load IT with the next display element from current_buffer. Value
8286 is false if end of buffer reached. IT->stop_charpos is the next
8287 position at which to stop and check for text properties or buffer
8288 end. */
8290 static bool
8291 next_element_from_buffer (struct it *it)
8293 bool success_p = true;
8295 eassert (IT_CHARPOS (*it) >= BEGV);
8296 eassert (NILP (it->string) && !it->s);
8297 eassert (!it->bidi_p
8298 || (EQ (it->bidi_it.string.lstring, Qnil)
8299 && it->bidi_it.string.s == NULL));
8301 /* With bidi reordering, the character to display might not be the
8302 character at IT_CHARPOS. BIDI_IT.FIRST_ELT means that
8303 we were reseat()ed to a new buffer position, which is potentially
8304 a different paragraph. */
8305 if (it->bidi_p && it->bidi_it.first_elt)
8307 get_visually_first_element (it);
8308 SET_TEXT_POS (it->position, IT_CHARPOS (*it), IT_BYTEPOS (*it));
8311 if (IT_CHARPOS (*it) >= it->stop_charpos)
8313 if (IT_CHARPOS (*it) >= it->end_charpos)
8315 bool overlay_strings_follow_p;
8317 /* End of the game, except when overlay strings follow that
8318 haven't been returned yet. */
8319 if (it->overlay_strings_at_end_processed_p)
8320 overlay_strings_follow_p = false;
8321 else
8323 it->overlay_strings_at_end_processed_p = true;
8324 overlay_strings_follow_p = get_overlay_strings (it, 0);
8327 if (overlay_strings_follow_p)
8328 success_p = GET_NEXT_DISPLAY_ELEMENT (it);
8329 else
8331 it->what = IT_EOB;
8332 it->position = it->current.pos;
8333 success_p = false;
8336 else if (!(!it->bidi_p
8337 || BIDI_AT_BASE_LEVEL (it->bidi_it)
8338 || IT_CHARPOS (*it) == it->stop_charpos))
8340 /* With bidi non-linear iteration, we could find ourselves
8341 far beyond the last computed stop_charpos, with several
8342 other stop positions in between that we missed. Scan
8343 them all now, in buffer's logical order, until we find
8344 and handle the last stop_charpos that precedes our
8345 current position. */
8346 handle_stop_backwards (it, it->stop_charpos);
8347 it->ignore_overlay_strings_at_pos_p = false;
8348 return GET_NEXT_DISPLAY_ELEMENT (it);
8350 else
8352 if (it->bidi_p)
8354 /* Take note of the stop position we just moved across,
8355 for when we will move back across it. */
8356 it->prev_stop = it->stop_charpos;
8357 /* If we are at base paragraph embedding level, take
8358 note of the last stop position seen at this
8359 level. */
8360 if (BIDI_AT_BASE_LEVEL (it->bidi_it))
8361 it->base_level_stop = it->stop_charpos;
8363 handle_stop (it);
8364 it->ignore_overlay_strings_at_pos_p = false;
8365 return GET_NEXT_DISPLAY_ELEMENT (it);
8368 else if (it->bidi_p
8369 /* If we are before prev_stop, we may have overstepped on
8370 our way backwards a stop_pos, and if so, we need to
8371 handle that stop_pos. */
8372 && IT_CHARPOS (*it) < it->prev_stop
8373 /* We can sometimes back up for reasons that have nothing
8374 to do with bidi reordering. E.g., compositions. The
8375 code below is only needed when we are above the base
8376 embedding level, so test for that explicitly. */
8377 && !BIDI_AT_BASE_LEVEL (it->bidi_it))
8379 if (it->base_level_stop <= 0
8380 || IT_CHARPOS (*it) < it->base_level_stop)
8382 /* If we lost track of base_level_stop, we need to find
8383 prev_stop by looking backwards. This happens, e.g., when
8384 we were reseated to the previous screenful of text by
8385 vertical-motion. */
8386 it->base_level_stop = BEGV;
8387 compute_stop_pos_backwards (it);
8388 handle_stop_backwards (it, it->prev_stop);
8390 else
8391 handle_stop_backwards (it, it->base_level_stop);
8392 it->ignore_overlay_strings_at_pos_p = false;
8393 return GET_NEXT_DISPLAY_ELEMENT (it);
8395 else
8397 /* No face changes, overlays etc. in sight, so just return a
8398 character from current_buffer. */
8399 unsigned char *p;
8400 ptrdiff_t stop;
8402 /* We moved to the next buffer position, so any info about
8403 previously seen overlays is no longer valid. */
8404 it->ignore_overlay_strings_at_pos_p = false;
8406 /* Maybe run the redisplay end trigger hook. Performance note:
8407 This doesn't seem to cost measurable time. */
8408 if (it->redisplay_end_trigger_charpos
8409 && it->glyph_row
8410 && IT_CHARPOS (*it) >= it->redisplay_end_trigger_charpos)
8411 run_redisplay_end_trigger_hook (it);
8413 stop = it->bidi_it.scan_dir < 0 ? -1 : it->end_charpos;
8414 if (CHAR_COMPOSED_P (it, IT_CHARPOS (*it), IT_BYTEPOS (*it),
8415 stop)
8416 && next_element_from_composition (it))
8418 return true;
8421 /* Get the next character, maybe multibyte. */
8422 p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
8423 if (it->multibyte_p && !ASCII_CHAR_P (*p))
8424 it->c = STRING_CHAR_AND_LENGTH (p, it->len);
8425 else
8426 it->c = *p, it->len = 1;
8428 /* Record what we have and where it came from. */
8429 it->what = IT_CHARACTER;
8430 it->object = it->w->contents;
8431 it->position = it->current.pos;
8433 /* Normally we return the character found above, except when we
8434 really want to return an ellipsis for selective display. */
8435 if (it->selective)
8437 if (it->c == '\n')
8439 /* A value of selective > 0 means hide lines indented more
8440 than that number of columns. */
8441 if (it->selective > 0
8442 && IT_CHARPOS (*it) + 1 < ZV
8443 && indented_beyond_p (IT_CHARPOS (*it) + 1,
8444 IT_BYTEPOS (*it) + 1,
8445 it->selective))
8447 success_p = next_element_from_ellipsis (it);
8448 it->dpvec_char_len = -1;
8451 else if (it->c == '\r' && it->selective == -1)
8453 /* A value of selective == -1 means that everything from the
8454 CR to the end of the line is invisible, with maybe an
8455 ellipsis displayed for it. */
8456 success_p = next_element_from_ellipsis (it);
8457 it->dpvec_char_len = -1;
8462 /* Value is false if end of buffer reached. */
8463 eassert (!success_p || it->what != IT_CHARACTER || it->len > 0);
8464 return success_p;
8468 /* Run the redisplay end trigger hook for IT. */
8470 static void
8471 run_redisplay_end_trigger_hook (struct it *it)
8473 /* IT->glyph_row should be non-null, i.e. we should be actually
8474 displaying something, or otherwise we should not run the hook. */
8475 eassert (it->glyph_row);
8477 ptrdiff_t charpos = it->redisplay_end_trigger_charpos;
8478 it->redisplay_end_trigger_charpos = 0;
8480 /* Since we are *trying* to run these functions, don't try to run
8481 them again, even if they get an error. */
8482 wset_redisplay_end_trigger (it->w, Qnil);
8483 CALLN (Frun_hook_with_args, Qredisplay_end_trigger_functions, it->window,
8484 make_number (charpos));
8486 /* Notice if it changed the face of the character we are on. */
8487 handle_face_prop (it);
8491 /* Deliver a composition display element. Unlike the other
8492 next_element_from_XXX, this function is not registered in the array
8493 get_next_element[]. It is called from next_element_from_buffer and
8494 next_element_from_string when necessary. */
8496 static bool
8497 next_element_from_composition (struct it *it)
8499 it->what = IT_COMPOSITION;
8500 it->len = it->cmp_it.nbytes;
8501 if (STRINGP (it->string))
8503 if (it->c < 0)
8505 IT_STRING_CHARPOS (*it) += it->cmp_it.nchars;
8506 IT_STRING_BYTEPOS (*it) += it->cmp_it.nbytes;
8507 return false;
8509 it->position = it->current.string_pos;
8510 it->object = it->string;
8511 it->c = composition_update_it (&it->cmp_it, IT_STRING_CHARPOS (*it),
8512 IT_STRING_BYTEPOS (*it), it->string);
8514 else
8516 if (it->c < 0)
8518 IT_CHARPOS (*it) += it->cmp_it.nchars;
8519 IT_BYTEPOS (*it) += it->cmp_it.nbytes;
8520 if (it->bidi_p)
8522 if (it->bidi_it.new_paragraph)
8523 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it,
8524 false);
8525 /* Resync the bidi iterator with IT's new position.
8526 FIXME: this doesn't support bidirectional text. */
8527 while (it->bidi_it.charpos < IT_CHARPOS (*it))
8528 bidi_move_to_visually_next (&it->bidi_it);
8530 return false;
8532 it->position = it->current.pos;
8533 it->object = it->w->contents;
8534 it->c = composition_update_it (&it->cmp_it, IT_CHARPOS (*it),
8535 IT_BYTEPOS (*it), Qnil);
8537 return true;
8542 /***********************************************************************
8543 Moving an iterator without producing glyphs
8544 ***********************************************************************/
8546 /* Check if iterator is at a position corresponding to a valid buffer
8547 position after some move_it_ call. */
8549 #define IT_POS_VALID_AFTER_MOVE_P(it) \
8550 ((it)->method != GET_FROM_STRING || IT_STRING_CHARPOS (*it) == 0)
8553 /* Move iterator IT to a specified buffer or X position within one
8554 line on the display without producing glyphs.
8556 OP should be a bit mask including some or all of these bits:
8557 MOVE_TO_X: Stop upon reaching x-position TO_X.
8558 MOVE_TO_POS: Stop upon reaching buffer or string position TO_CHARPOS.
8559 Regardless of OP's value, stop upon reaching the end of the display line.
8561 TO_X is normally a value 0 <= TO_X <= IT->last_visible_x.
8562 This means, in particular, that TO_X includes window's horizontal
8563 scroll amount.
8565 The return value has several possible values that
8566 say what condition caused the scan to stop:
8568 MOVE_POS_MATCH_OR_ZV
8569 - when TO_POS or ZV was reached.
8571 MOVE_X_REACHED
8572 -when TO_X was reached before TO_POS or ZV were reached.
8574 MOVE_LINE_CONTINUED
8575 - when we reached the end of the display area and the line must
8576 be continued.
8578 MOVE_LINE_TRUNCATED
8579 - when we reached the end of the display area and the line is
8580 truncated.
8582 MOVE_NEWLINE_OR_CR
8583 - when we stopped at a line end, i.e. a newline or a CR and selective
8584 display is on. */
8586 static enum move_it_result
8587 move_it_in_display_line_to (struct it *it,
8588 ptrdiff_t to_charpos, int to_x,
8589 enum move_operation_enum op)
8591 enum move_it_result result = MOVE_UNDEFINED;
8592 struct glyph_row *saved_glyph_row;
8593 struct it wrap_it, atpos_it, atx_it, ppos_it;
8594 void *wrap_data = NULL, *atpos_data = NULL, *atx_data = NULL;
8595 void *ppos_data = NULL;
8596 bool may_wrap = false;
8597 enum it_method prev_method = it->method;
8598 ptrdiff_t closest_pos UNINIT;
8599 ptrdiff_t prev_pos = IT_CHARPOS (*it);
8600 bool saw_smaller_pos = prev_pos < to_charpos;
8602 /* Don't produce glyphs in produce_glyphs. */
8603 saved_glyph_row = it->glyph_row;
8604 it->glyph_row = NULL;
8606 /* Use wrap_it to save a copy of IT wherever a word wrap could
8607 occur. Use atpos_it to save a copy of IT at the desired buffer
8608 position, if found, so that we can scan ahead and check if the
8609 word later overshoots the window edge. Use atx_it similarly, for
8610 pixel positions. */
8611 wrap_it.sp = -1;
8612 atpos_it.sp = -1;
8613 atx_it.sp = -1;
8615 /* Use ppos_it under bidi reordering to save a copy of IT for the
8616 initial position. We restore that position in IT when we have
8617 scanned the entire display line without finding a match for
8618 TO_CHARPOS and all the character positions are greater than
8619 TO_CHARPOS. We then restart the scan from the initial position,
8620 and stop at CLOSEST_POS, which is a position > TO_CHARPOS that is
8621 the closest to TO_CHARPOS. */
8622 if (it->bidi_p)
8624 if ((op & MOVE_TO_POS) && IT_CHARPOS (*it) >= to_charpos)
8626 SAVE_IT (ppos_it, *it, ppos_data);
8627 closest_pos = IT_CHARPOS (*it);
8629 else
8630 closest_pos = ZV;
8633 #define BUFFER_POS_REACHED_P() \
8634 ((op & MOVE_TO_POS) != 0 \
8635 && BUFFERP (it->object) \
8636 && (IT_CHARPOS (*it) == to_charpos \
8637 || ((!it->bidi_p \
8638 || BIDI_AT_BASE_LEVEL (it->bidi_it)) \
8639 && IT_CHARPOS (*it) > to_charpos) \
8640 || (it->what == IT_COMPOSITION \
8641 && ((IT_CHARPOS (*it) > to_charpos \
8642 && to_charpos >= it->cmp_it.charpos) \
8643 || (IT_CHARPOS (*it) < to_charpos \
8644 && to_charpos <= it->cmp_it.charpos)))) \
8645 && (it->method == GET_FROM_BUFFER \
8646 || (it->method == GET_FROM_DISPLAY_VECTOR \
8647 && it->dpvec + it->current.dpvec_index + 1 >= it->dpend)))
8649 /* If there's a line-/wrap-prefix, handle it. */
8650 if (it->hpos == 0 && it->method == GET_FROM_BUFFER)
8651 handle_line_prefix (it);
8653 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8654 SET_TEXT_POS (this_line_min_pos, IT_CHARPOS (*it), IT_BYTEPOS (*it));
8656 while (true)
8658 int x, i, ascent = 0, descent = 0;
8660 /* Utility macro to reset an iterator with x, ascent, and descent. */
8661 #define IT_RESET_X_ASCENT_DESCENT(IT) \
8662 ((IT)->current_x = x, (IT)->max_ascent = ascent, \
8663 (IT)->max_descent = descent)
8665 /* Stop if we move beyond TO_CHARPOS (after an image or a
8666 display string or stretch glyph). */
8667 if ((op & MOVE_TO_POS) != 0
8668 && BUFFERP (it->object)
8669 && it->method == GET_FROM_BUFFER
8670 && (((!it->bidi_p
8671 /* When the iterator is at base embedding level, we
8672 are guaranteed that characters are delivered for
8673 display in strictly increasing order of their
8674 buffer positions. */
8675 || BIDI_AT_BASE_LEVEL (it->bidi_it))
8676 && IT_CHARPOS (*it) > to_charpos)
8677 || (it->bidi_p
8678 && (prev_method == GET_FROM_IMAGE
8679 || prev_method == GET_FROM_STRETCH
8680 || prev_method == GET_FROM_STRING)
8681 /* Passed TO_CHARPOS from left to right. */
8682 && ((prev_pos < to_charpos
8683 && IT_CHARPOS (*it) > to_charpos)
8684 /* Passed TO_CHARPOS from right to left. */
8685 || (prev_pos > to_charpos
8686 && IT_CHARPOS (*it) < to_charpos)))))
8688 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8690 result = MOVE_POS_MATCH_OR_ZV;
8691 break;
8693 else if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
8694 /* If wrap_it is valid, the current position might be in a
8695 word that is wrapped. So, save the iterator in
8696 atpos_it and continue to see if wrapping happens. */
8697 SAVE_IT (atpos_it, *it, atpos_data);
8700 /* Stop when ZV reached.
8701 We used to stop here when TO_CHARPOS reached as well, but that is
8702 too soon if this glyph does not fit on this line. So we handle it
8703 explicitly below. */
8704 if (!get_next_display_element (it))
8706 result = MOVE_POS_MATCH_OR_ZV;
8707 break;
8710 if (it->line_wrap == TRUNCATE)
8712 if (BUFFER_POS_REACHED_P ())
8714 result = MOVE_POS_MATCH_OR_ZV;
8715 break;
8718 else
8720 if (it->line_wrap == WORD_WRAP && it->area == TEXT_AREA)
8722 if (IT_DISPLAYING_WHITESPACE (it))
8723 may_wrap = true;
8724 else if (may_wrap)
8726 /* We have reached a glyph that follows one or more
8727 whitespace characters. If the position is
8728 already found, we are done. */
8729 if (atpos_it.sp >= 0)
8731 RESTORE_IT (it, &atpos_it, atpos_data);
8732 result = MOVE_POS_MATCH_OR_ZV;
8733 goto done;
8735 if (atx_it.sp >= 0)
8737 RESTORE_IT (it, &atx_it, atx_data);
8738 result = MOVE_X_REACHED;
8739 goto done;
8741 /* Otherwise, we can wrap here. */
8742 SAVE_IT (wrap_it, *it, wrap_data);
8743 may_wrap = false;
8748 /* Remember the line height for the current line, in case
8749 the next element doesn't fit on the line. */
8750 ascent = it->max_ascent;
8751 descent = it->max_descent;
8753 /* The call to produce_glyphs will get the metrics of the
8754 display element IT is loaded with. Record the x-position
8755 before this display element, in case it doesn't fit on the
8756 line. */
8757 x = it->current_x;
8759 PRODUCE_GLYPHS (it);
8761 if (it->area != TEXT_AREA)
8763 prev_method = it->method;
8764 if (it->method == GET_FROM_BUFFER)
8765 prev_pos = IT_CHARPOS (*it);
8766 set_iterator_to_next (it, true);
8767 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8768 SET_TEXT_POS (this_line_min_pos,
8769 IT_CHARPOS (*it), IT_BYTEPOS (*it));
8770 if (it->bidi_p
8771 && (op & MOVE_TO_POS)
8772 && IT_CHARPOS (*it) > to_charpos
8773 && IT_CHARPOS (*it) < closest_pos)
8774 closest_pos = IT_CHARPOS (*it);
8775 continue;
8778 /* The number of glyphs we get back in IT->nglyphs will normally
8779 be 1 except when IT->c is (i) a TAB, or (ii) a multi-glyph
8780 character on a terminal frame, or (iii) a line end. For the
8781 second case, IT->nglyphs - 1 padding glyphs will be present.
8782 (On X frames, there is only one glyph produced for a
8783 composite character.)
8785 The behavior implemented below means, for continuation lines,
8786 that as many spaces of a TAB as fit on the current line are
8787 displayed there. For terminal frames, as many glyphs of a
8788 multi-glyph character are displayed in the current line, too.
8789 This is what the old redisplay code did, and we keep it that
8790 way. Under X, the whole shape of a complex character must
8791 fit on the line or it will be completely displayed in the
8792 next line.
8794 Note that both for tabs and padding glyphs, all glyphs have
8795 the same width. */
8796 if (it->nglyphs)
8798 /* More than one glyph or glyph doesn't fit on line. All
8799 glyphs have the same width. */
8800 int single_glyph_width = it->pixel_width / it->nglyphs;
8801 int new_x;
8802 int x_before_this_char = x;
8803 int hpos_before_this_char = it->hpos;
8805 for (i = 0; i < it->nglyphs; ++i, x = new_x)
8807 new_x = x + single_glyph_width;
8809 /* We want to leave anything reaching TO_X to the caller. */
8810 if ((op & MOVE_TO_X) && new_x > to_x)
8812 if (BUFFER_POS_REACHED_P ())
8814 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8815 goto buffer_pos_reached;
8816 if (atpos_it.sp < 0)
8818 SAVE_IT (atpos_it, *it, atpos_data);
8819 IT_RESET_X_ASCENT_DESCENT (&atpos_it);
8822 else
8824 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8826 it->current_x = x;
8827 result = MOVE_X_REACHED;
8828 break;
8830 if (atx_it.sp < 0)
8832 SAVE_IT (atx_it, *it, atx_data);
8833 IT_RESET_X_ASCENT_DESCENT (&atx_it);
8838 if (/* Lines are continued. */
8839 it->line_wrap != TRUNCATE
8840 && (/* And glyph doesn't fit on the line. */
8841 new_x > it->last_visible_x
8842 /* Or it fits exactly and we're on a window
8843 system frame. */
8844 || (new_x == it->last_visible_x
8845 && FRAME_WINDOW_P (it->f)
8846 && ((it->bidi_p && it->bidi_it.paragraph_dir == R2L)
8847 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
8848 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)))))
8850 bool moved_forward = false;
8852 if (/* IT->hpos == 0 means the very first glyph
8853 doesn't fit on the line, e.g. a wide image. */
8854 it->hpos == 0
8855 || (new_x == it->last_visible_x
8856 && FRAME_WINDOW_P (it->f)))
8858 ++it->hpos;
8859 it->current_x = new_x;
8861 /* The character's last glyph just barely fits
8862 in this row. */
8863 if (i == it->nglyphs - 1)
8865 /* If this is the destination position,
8866 return a position *before* it in this row,
8867 now that we know it fits in this row. */
8868 if (BUFFER_POS_REACHED_P ())
8870 bool can_wrap = true;
8872 /* If we are at a whitespace character
8873 that barely fits on this screen line,
8874 but the next character is also
8875 whitespace, we cannot wrap here. */
8876 if (it->line_wrap == WORD_WRAP
8877 && wrap_it.sp >= 0
8878 && may_wrap
8879 && IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
8881 struct it tem_it;
8882 void *tem_data = NULL;
8884 SAVE_IT (tem_it, *it, tem_data);
8885 set_iterator_to_next (it, true);
8886 if (get_next_display_element (it)
8887 && IT_DISPLAYING_WHITESPACE (it))
8888 can_wrap = false;
8889 RESTORE_IT (it, &tem_it, tem_data);
8891 if (it->line_wrap != WORD_WRAP
8892 || wrap_it.sp < 0
8893 /* If we've just found whitespace
8894 where we can wrap, effectively
8895 ignore the previous wrap point --
8896 it is no longer relevant, but we
8897 won't have an opportunity to
8898 update it, since we've reached
8899 the edge of this screen line. */
8900 || (may_wrap && can_wrap
8901 && IT_OVERFLOW_NEWLINE_INTO_FRINGE (it)))
8903 it->hpos = hpos_before_this_char;
8904 it->current_x = x_before_this_char;
8905 result = MOVE_POS_MATCH_OR_ZV;
8906 break;
8908 if (it->line_wrap == WORD_WRAP
8909 && atpos_it.sp < 0)
8911 SAVE_IT (atpos_it, *it, atpos_data);
8912 atpos_it.current_x = x_before_this_char;
8913 atpos_it.hpos = hpos_before_this_char;
8917 prev_method = it->method;
8918 if (it->method == GET_FROM_BUFFER)
8919 prev_pos = IT_CHARPOS (*it);
8920 set_iterator_to_next (it, true);
8921 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8922 SET_TEXT_POS (this_line_min_pos,
8923 IT_CHARPOS (*it), IT_BYTEPOS (*it));
8924 /* On graphical terminals, newlines may
8925 "overflow" into the fringe if
8926 overflow-newline-into-fringe is non-nil.
8927 On text terminals, and on graphical
8928 terminals with no right margin, newlines
8929 may overflow into the last glyph on the
8930 display line.*/
8931 if (!FRAME_WINDOW_P (it->f)
8932 || ((it->bidi_p
8933 && it->bidi_it.paragraph_dir == R2L)
8934 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
8935 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0
8936 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
8938 if (!get_next_display_element (it))
8940 result = MOVE_POS_MATCH_OR_ZV;
8941 break;
8943 moved_forward = true;
8944 if (BUFFER_POS_REACHED_P ())
8946 if (ITERATOR_AT_END_OF_LINE_P (it))
8947 result = MOVE_POS_MATCH_OR_ZV;
8948 else
8949 result = MOVE_LINE_CONTINUED;
8950 break;
8952 if (ITERATOR_AT_END_OF_LINE_P (it)
8953 && (it->line_wrap != WORD_WRAP
8954 || wrap_it.sp < 0
8955 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it)))
8957 result = MOVE_NEWLINE_OR_CR;
8958 break;
8963 else
8964 IT_RESET_X_ASCENT_DESCENT (it);
8966 /* If the screen line ends with whitespace, and we
8967 are under word-wrap, don't use wrap_it: it is no
8968 longer relevant, but we won't have an opportunity
8969 to update it, since we are done with this screen
8970 line. */
8971 if (may_wrap && IT_OVERFLOW_NEWLINE_INTO_FRINGE (it)
8972 /* If the character after the one which set the
8973 may_wrap flag is also whitespace, we can't
8974 wrap here, since the screen line cannot be
8975 wrapped in the middle of whitespace.
8976 Therefore, wrap_it _is_ relevant in that
8977 case. */
8978 && !(moved_forward && IT_DISPLAYING_WHITESPACE (it)))
8980 /* If we've found TO_X, go back there, as we now
8981 know the last word fits on this screen line. */
8982 if ((op & MOVE_TO_X) && new_x == it->last_visible_x
8983 && atx_it.sp >= 0)
8985 RESTORE_IT (it, &atx_it, atx_data);
8986 atpos_it.sp = -1;
8987 atx_it.sp = -1;
8988 result = MOVE_X_REACHED;
8989 break;
8992 else if (wrap_it.sp >= 0)
8994 RESTORE_IT (it, &wrap_it, wrap_data);
8995 atpos_it.sp = -1;
8996 atx_it.sp = -1;
8999 TRACE_MOVE ((stderr, "move_it_in: continued at %d\n",
9000 IT_CHARPOS (*it)));
9001 result = MOVE_LINE_CONTINUED;
9002 break;
9005 if (BUFFER_POS_REACHED_P ())
9007 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
9008 goto buffer_pos_reached;
9009 if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
9011 SAVE_IT (atpos_it, *it, atpos_data);
9012 IT_RESET_X_ASCENT_DESCENT (&atpos_it);
9016 if (new_x > it->first_visible_x)
9018 /* Glyph is visible. Increment number of glyphs that
9019 would be displayed. */
9020 ++it->hpos;
9024 if (result != MOVE_UNDEFINED)
9025 break;
9027 else if (BUFFER_POS_REACHED_P ())
9029 buffer_pos_reached:
9030 IT_RESET_X_ASCENT_DESCENT (it);
9031 result = MOVE_POS_MATCH_OR_ZV;
9032 break;
9034 else if ((op & MOVE_TO_X) && it->current_x >= to_x)
9036 /* Stop when TO_X specified and reached. This check is
9037 necessary here because of lines consisting of a line end,
9038 only. The line end will not produce any glyphs and we
9039 would never get MOVE_X_REACHED. */
9040 eassert (it->nglyphs == 0);
9041 result = MOVE_X_REACHED;
9042 break;
9045 /* Is this a line end? If yes, we're done. */
9046 if (ITERATOR_AT_END_OF_LINE_P (it))
9048 /* If we are past TO_CHARPOS, but never saw any character
9049 positions smaller than TO_CHARPOS, return
9050 MOVE_POS_MATCH_OR_ZV, like the unidirectional display
9051 did. */
9052 if (it->bidi_p && (op & MOVE_TO_POS) != 0)
9054 if (!saw_smaller_pos && IT_CHARPOS (*it) > to_charpos)
9056 if (closest_pos < ZV)
9058 RESTORE_IT (it, &ppos_it, ppos_data);
9059 /* Don't recurse if closest_pos is equal to
9060 to_charpos, since we have just tried that. */
9061 if (closest_pos != to_charpos)
9062 move_it_in_display_line_to (it, closest_pos, -1,
9063 MOVE_TO_POS);
9064 result = MOVE_POS_MATCH_OR_ZV;
9066 else
9067 goto buffer_pos_reached;
9069 else if (it->line_wrap == WORD_WRAP && atpos_it.sp >= 0
9070 && IT_CHARPOS (*it) > to_charpos)
9071 goto buffer_pos_reached;
9072 else
9073 result = MOVE_NEWLINE_OR_CR;
9075 else
9076 result = MOVE_NEWLINE_OR_CR;
9077 /* If we've processed the newline, make sure this flag is
9078 reset, as it must only be set when the newline itself is
9079 processed. */
9080 if (result == MOVE_NEWLINE_OR_CR)
9081 it->constrain_row_ascent_descent_p = false;
9082 break;
9085 prev_method = it->method;
9086 if (it->method == GET_FROM_BUFFER)
9087 prev_pos = IT_CHARPOS (*it);
9088 /* The current display element has been consumed. Advance
9089 to the next. */
9090 set_iterator_to_next (it, true);
9091 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
9092 SET_TEXT_POS (this_line_min_pos, IT_CHARPOS (*it), IT_BYTEPOS (*it));
9093 if (IT_CHARPOS (*it) < to_charpos)
9094 saw_smaller_pos = true;
9095 if (it->bidi_p
9096 && (op & MOVE_TO_POS)
9097 && IT_CHARPOS (*it) >= to_charpos
9098 && IT_CHARPOS (*it) < closest_pos)
9099 closest_pos = IT_CHARPOS (*it);
9101 /* Stop if lines are truncated and IT's current x-position is
9102 past the right edge of the window now. */
9103 if (it->line_wrap == TRUNCATE
9104 && it->current_x >= it->last_visible_x)
9106 if (!FRAME_WINDOW_P (it->f)
9107 || ((it->bidi_p && it->bidi_it.paragraph_dir == R2L)
9108 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
9109 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0
9110 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
9112 bool at_eob_p = false;
9114 if ((at_eob_p = !get_next_display_element (it))
9115 || BUFFER_POS_REACHED_P ()
9116 /* If we are past TO_CHARPOS, but never saw any
9117 character positions smaller than TO_CHARPOS,
9118 return MOVE_POS_MATCH_OR_ZV, like the
9119 unidirectional display did. */
9120 || (it->bidi_p && (op & MOVE_TO_POS) != 0
9121 && !saw_smaller_pos
9122 && IT_CHARPOS (*it) > to_charpos))
9124 if (it->bidi_p
9125 && !BUFFER_POS_REACHED_P ()
9126 && !at_eob_p && closest_pos < ZV)
9128 RESTORE_IT (it, &ppos_it, ppos_data);
9129 if (closest_pos != to_charpos)
9130 move_it_in_display_line_to (it, closest_pos, -1,
9131 MOVE_TO_POS);
9133 result = MOVE_POS_MATCH_OR_ZV;
9134 break;
9136 if (ITERATOR_AT_END_OF_LINE_P (it))
9138 result = MOVE_NEWLINE_OR_CR;
9139 break;
9142 else if (it->bidi_p && (op & MOVE_TO_POS) != 0
9143 && !saw_smaller_pos
9144 && IT_CHARPOS (*it) > to_charpos)
9146 if (closest_pos < ZV)
9148 RESTORE_IT (it, &ppos_it, ppos_data);
9149 if (closest_pos != to_charpos)
9150 move_it_in_display_line_to (it, closest_pos, -1,
9151 MOVE_TO_POS);
9153 result = MOVE_POS_MATCH_OR_ZV;
9154 break;
9156 result = MOVE_LINE_TRUNCATED;
9157 break;
9159 #undef IT_RESET_X_ASCENT_DESCENT
9162 #undef BUFFER_POS_REACHED_P
9164 /* If we scanned beyond TO_POS, restore the saved iterator either to
9165 the wrap point (if found), or to atpos/atx location. We decide which
9166 data to use to restore the saved iterator state by their X coordinates,
9167 since buffer positions might increase non-monotonically with screen
9168 coordinates due to bidi reordering. */
9169 if (result == MOVE_LINE_CONTINUED
9170 && it->line_wrap == WORD_WRAP
9171 && wrap_it.sp >= 0
9172 && ((atpos_it.sp >= 0 && wrap_it.current_x < atpos_it.current_x)
9173 || (atx_it.sp >= 0 && wrap_it.current_x < atx_it.current_x)))
9174 RESTORE_IT (it, &wrap_it, wrap_data);
9175 else if (atpos_it.sp >= 0)
9176 RESTORE_IT (it, &atpos_it, atpos_data);
9177 else if (atx_it.sp >= 0)
9178 RESTORE_IT (it, &atx_it, atx_data);
9180 done:
9182 if (atpos_data)
9183 bidi_unshelve_cache (atpos_data, true);
9184 if (atx_data)
9185 bidi_unshelve_cache (atx_data, true);
9186 if (wrap_data)
9187 bidi_unshelve_cache (wrap_data, true);
9188 if (ppos_data)
9189 bidi_unshelve_cache (ppos_data, true);
9191 /* Restore the iterator settings altered at the beginning of this
9192 function. */
9193 it->glyph_row = saved_glyph_row;
9194 return result;
9197 /* For external use. */
9198 void
9199 move_it_in_display_line (struct it *it,
9200 ptrdiff_t to_charpos, int to_x,
9201 enum move_operation_enum op)
9203 if (it->line_wrap == WORD_WRAP
9204 && (op & MOVE_TO_X))
9206 struct it save_it;
9207 void *save_data = NULL;
9208 int skip;
9210 SAVE_IT (save_it, *it, save_data);
9211 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
9212 /* When word-wrap is on, TO_X may lie past the end
9213 of a wrapped line. Then it->current is the
9214 character on the next line, so backtrack to the
9215 space before the wrap point. */
9216 if (skip == MOVE_LINE_CONTINUED)
9218 int prev_x = max (it->current_x - 1, 0);
9219 RESTORE_IT (it, &save_it, save_data);
9220 move_it_in_display_line_to
9221 (it, -1, prev_x, MOVE_TO_X);
9223 else
9224 bidi_unshelve_cache (save_data, true);
9226 else
9227 move_it_in_display_line_to (it, to_charpos, to_x, op);
9231 /* Move IT forward until it satisfies one or more of the criteria in
9232 TO_CHARPOS, TO_X, TO_Y, and TO_VPOS.
9234 OP is a bit-mask that specifies where to stop, and in particular,
9235 which of those four position arguments makes a difference. See the
9236 description of enum move_operation_enum.
9238 If TO_CHARPOS is in invisible text, e.g. a truncated part of a
9239 screen line, this function will set IT to the next position that is
9240 displayed to the right of TO_CHARPOS on the screen.
9242 Return the maximum pixel length of any line scanned but never more
9243 than it.last_visible_x. */
9246 move_it_to (struct it *it, ptrdiff_t to_charpos, int to_x, int to_y, int to_vpos, int op)
9248 enum move_it_result skip, skip2 = MOVE_X_REACHED;
9249 int line_height, line_start_x = 0, reached = 0;
9250 int max_current_x = 0;
9251 void *backup_data = NULL;
9253 for (;;)
9255 if (op & MOVE_TO_VPOS)
9257 /* If no TO_CHARPOS and no TO_X specified, stop at the
9258 start of the line TO_VPOS. */
9259 if ((op & (MOVE_TO_X | MOVE_TO_POS)) == 0)
9261 if (it->vpos == to_vpos)
9263 reached = 1;
9264 break;
9266 else
9267 skip = move_it_in_display_line_to (it, -1, -1, 0);
9269 else
9271 /* TO_VPOS >= 0 means stop at TO_X in the line at
9272 TO_VPOS, or at TO_POS, whichever comes first. */
9273 if (it->vpos == to_vpos)
9275 reached = 2;
9276 break;
9279 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
9281 if (skip == MOVE_POS_MATCH_OR_ZV || it->vpos == to_vpos)
9283 reached = 3;
9284 break;
9286 else if (skip == MOVE_X_REACHED && it->vpos != to_vpos)
9288 /* We have reached TO_X but not in the line we want. */
9289 skip = move_it_in_display_line_to (it, to_charpos,
9290 -1, MOVE_TO_POS);
9291 if (skip == MOVE_POS_MATCH_OR_ZV)
9293 reached = 4;
9294 break;
9299 else if (op & MOVE_TO_Y)
9301 struct it it_backup;
9303 if (it->line_wrap == WORD_WRAP)
9304 SAVE_IT (it_backup, *it, backup_data);
9306 /* TO_Y specified means stop at TO_X in the line containing
9307 TO_Y---or at TO_CHARPOS if this is reached first. The
9308 problem is that we can't really tell whether the line
9309 contains TO_Y before we have completely scanned it, and
9310 this may skip past TO_X. What we do is to first scan to
9311 TO_X.
9313 If TO_X is not specified, use a TO_X of zero. The reason
9314 is to make the outcome of this function more predictable.
9315 If we didn't use TO_X == 0, we would stop at the end of
9316 the line which is probably not what a caller would expect
9317 to happen. */
9318 skip = move_it_in_display_line_to
9319 (it, to_charpos, ((op & MOVE_TO_X) ? to_x : 0),
9320 (MOVE_TO_X | (op & MOVE_TO_POS)));
9322 /* If TO_CHARPOS is reached or ZV, we don't have to do more. */
9323 if (skip == MOVE_POS_MATCH_OR_ZV)
9324 reached = 5;
9325 else if (skip == MOVE_X_REACHED)
9327 /* If TO_X was reached, we want to know whether TO_Y is
9328 in the line. We know this is the case if the already
9329 scanned glyphs make the line tall enough. Otherwise,
9330 we must check by scanning the rest of the line. */
9331 line_height = it->max_ascent + it->max_descent;
9332 if (to_y >= it->current_y
9333 && to_y < it->current_y + line_height)
9335 reached = 6;
9336 break;
9338 SAVE_IT (it_backup, *it, backup_data);
9339 TRACE_MOVE ((stderr, "move_it: from %d\n", IT_CHARPOS (*it)));
9340 skip2 = move_it_in_display_line_to (it, to_charpos, -1,
9341 op & MOVE_TO_POS);
9342 TRACE_MOVE ((stderr, "move_it: to %d\n", IT_CHARPOS (*it)));
9343 line_height = it->max_ascent + it->max_descent;
9344 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
9346 if (to_y >= it->current_y
9347 && to_y < it->current_y + line_height)
9349 /* If TO_Y is in this line and TO_X was reached
9350 above, we scanned too far. We have to restore
9351 IT's settings to the ones before skipping. But
9352 keep the more accurate values of max_ascent and
9353 max_descent we've found while skipping the rest
9354 of the line, for the sake of callers, such as
9355 pos_visible_p, that need to know the line
9356 height. */
9357 int max_ascent = it->max_ascent;
9358 int max_descent = it->max_descent;
9360 RESTORE_IT (it, &it_backup, backup_data);
9361 it->max_ascent = max_ascent;
9362 it->max_descent = max_descent;
9363 reached = 6;
9365 else
9367 skip = skip2;
9368 if (skip == MOVE_POS_MATCH_OR_ZV)
9369 reached = 7;
9372 else
9374 /* Check whether TO_Y is in this line. */
9375 line_height = it->max_ascent + it->max_descent;
9376 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
9378 if (to_y >= it->current_y
9379 && to_y < it->current_y + line_height)
9381 if (to_y > it->current_y)
9382 max_current_x = max (it->current_x, max_current_x);
9384 /* When word-wrap is on, TO_X may lie past the end
9385 of a wrapped line. Then it->current is the
9386 character on the next line, so backtrack to the
9387 space before the wrap point. */
9388 if (skip == MOVE_LINE_CONTINUED
9389 && it->line_wrap == WORD_WRAP)
9391 int prev_x = max (it->current_x - 1, 0);
9392 RESTORE_IT (it, &it_backup, backup_data);
9393 skip = move_it_in_display_line_to
9394 (it, -1, prev_x, MOVE_TO_X);
9397 reached = 6;
9401 if (reached)
9403 max_current_x = max (it->current_x, max_current_x);
9404 break;
9407 else if (BUFFERP (it->object)
9408 && (it->method == GET_FROM_BUFFER
9409 || it->method == GET_FROM_STRETCH)
9410 && IT_CHARPOS (*it) >= to_charpos
9411 /* Under bidi iteration, a call to set_iterator_to_next
9412 can scan far beyond to_charpos if the initial
9413 portion of the next line needs to be reordered. In
9414 that case, give move_it_in_display_line_to another
9415 chance below. */
9416 && !(it->bidi_p
9417 && it->bidi_it.scan_dir == -1))
9418 skip = MOVE_POS_MATCH_OR_ZV;
9419 else
9420 skip = move_it_in_display_line_to (it, to_charpos, -1, MOVE_TO_POS);
9422 switch (skip)
9424 case MOVE_POS_MATCH_OR_ZV:
9425 max_current_x = max (it->current_x, max_current_x);
9426 reached = 8;
9427 goto out;
9429 case MOVE_NEWLINE_OR_CR:
9430 max_current_x = max (it->current_x, max_current_x);
9431 set_iterator_to_next (it, true);
9432 it->continuation_lines_width = 0;
9433 break;
9435 case MOVE_LINE_TRUNCATED:
9436 max_current_x = it->last_visible_x;
9437 it->continuation_lines_width = 0;
9438 reseat_at_next_visible_line_start (it, false);
9439 if ((op & MOVE_TO_POS) != 0
9440 && IT_CHARPOS (*it) > to_charpos)
9442 reached = 9;
9443 goto out;
9445 break;
9447 case MOVE_LINE_CONTINUED:
9448 max_current_x = it->last_visible_x;
9449 /* For continued lines ending in a tab, some of the glyphs
9450 associated with the tab are displayed on the current
9451 line. Since it->current_x does not include these glyphs,
9452 we use it->last_visible_x instead. */
9453 if (it->c == '\t')
9455 it->continuation_lines_width += it->last_visible_x;
9456 /* When moving by vpos, ensure that the iterator really
9457 advances to the next line (bug#847, bug#969). Fixme:
9458 do we need to do this in other circumstances? */
9459 if (it->current_x != it->last_visible_x
9460 && (op & MOVE_TO_VPOS)
9461 && !(op & (MOVE_TO_X | MOVE_TO_POS)))
9463 line_start_x = it->current_x + it->pixel_width
9464 - it->last_visible_x;
9465 if (FRAME_WINDOW_P (it->f))
9467 struct face *face = FACE_FROM_ID (it->f, it->face_id);
9468 struct font *face_font = face->font;
9470 /* When display_line produces a continued line
9471 that ends in a TAB, it skips a tab stop that
9472 is closer than the font's space character
9473 width (see x_produce_glyphs where it produces
9474 the stretch glyph which represents a TAB).
9475 We need to reproduce the same logic here. */
9476 eassert (face_font);
9477 if (face_font)
9479 if (line_start_x < face_font->space_width)
9480 line_start_x
9481 += it->tab_width * face_font->space_width;
9484 set_iterator_to_next (it, false);
9487 else
9488 it->continuation_lines_width += it->current_x;
9489 break;
9491 default:
9492 emacs_abort ();
9495 /* Reset/increment for the next run. */
9496 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
9497 it->current_x = line_start_x;
9498 line_start_x = 0;
9499 it->hpos = 0;
9500 it->current_y += it->max_ascent + it->max_descent;
9501 ++it->vpos;
9502 last_height = it->max_ascent + it->max_descent;
9503 it->max_ascent = it->max_descent = 0;
9506 out:
9508 /* On text terminals, we may stop at the end of a line in the middle
9509 of a multi-character glyph. If the glyph itself is continued,
9510 i.e. it is actually displayed on the next line, don't treat this
9511 stopping point as valid; move to the next line instead (unless
9512 that brings us offscreen). */
9513 if (!FRAME_WINDOW_P (it->f)
9514 && op & MOVE_TO_POS
9515 && IT_CHARPOS (*it) == to_charpos
9516 && it->what == IT_CHARACTER
9517 && it->nglyphs > 1
9518 && it->line_wrap == WINDOW_WRAP
9519 && it->current_x == it->last_visible_x - 1
9520 && it->c != '\n'
9521 && it->c != '\t'
9522 && it->w->window_end_valid
9523 && it->vpos < it->w->window_end_vpos)
9525 it->continuation_lines_width += it->current_x;
9526 it->current_x = it->hpos = it->max_ascent = it->max_descent = 0;
9527 it->current_y += it->max_ascent + it->max_descent;
9528 ++it->vpos;
9529 last_height = it->max_ascent + it->max_descent;
9532 if (backup_data)
9533 bidi_unshelve_cache (backup_data, true);
9535 TRACE_MOVE ((stderr, "move_it_to: reached %d\n", reached));
9537 return max_current_x;
9541 /* Move iterator IT backward by a specified y-distance DY, DY >= 0.
9543 If DY > 0, move IT backward at least that many pixels. DY = 0
9544 means move IT backward to the preceding line start or BEGV. This
9545 function may move over more than DY pixels if IT->current_y - DY
9546 ends up in the middle of a line; in this case IT->current_y will be
9547 set to the top of the line moved to. */
9549 void
9550 move_it_vertically_backward (struct it *it, int dy)
9552 int nlines, h;
9553 struct it it2, it3;
9554 void *it2data = NULL, *it3data = NULL;
9555 ptrdiff_t start_pos;
9556 int nchars_per_row
9557 = (it->last_visible_x - it->first_visible_x) / FRAME_COLUMN_WIDTH (it->f);
9558 ptrdiff_t pos_limit;
9560 move_further_back:
9561 eassert (dy >= 0);
9563 start_pos = IT_CHARPOS (*it);
9565 /* Estimate how many newlines we must move back. */
9566 nlines = max (1, dy / default_line_pixel_height (it->w));
9567 if (it->line_wrap == TRUNCATE || nchars_per_row == 0)
9568 pos_limit = BEGV;
9569 else
9570 pos_limit = max (start_pos - nlines * nchars_per_row, BEGV);
9572 /* Set the iterator's position that many lines back. But don't go
9573 back more than NLINES full screen lines -- this wins a day with
9574 buffers which have very long lines. */
9575 while (nlines-- && IT_CHARPOS (*it) > pos_limit)
9576 back_to_previous_visible_line_start (it);
9578 /* Reseat the iterator here. When moving backward, we don't want
9579 reseat to skip forward over invisible text, set up the iterator
9580 to deliver from overlay strings at the new position etc. So,
9581 use reseat_1 here. */
9582 reseat_1 (it, it->current.pos, true);
9584 /* We are now surely at a line start. */
9585 it->current_x = it->hpos = 0; /* FIXME: this is incorrect when bidi
9586 reordering is in effect. */
9587 it->continuation_lines_width = 0;
9589 /* Move forward and see what y-distance we moved. First move to the
9590 start of the next line so that we get its height. We need this
9591 height to be able to tell whether we reached the specified
9592 y-distance. */
9593 SAVE_IT (it2, *it, it2data);
9594 it2.max_ascent = it2.max_descent = 0;
9597 move_it_to (&it2, start_pos, -1, -1, it2.vpos + 1,
9598 MOVE_TO_POS | MOVE_TO_VPOS);
9600 while (!(IT_POS_VALID_AFTER_MOVE_P (&it2)
9601 /* If we are in a display string which starts at START_POS,
9602 and that display string includes a newline, and we are
9603 right after that newline (i.e. at the beginning of a
9604 display line), exit the loop, because otherwise we will
9605 infloop, since move_it_to will see that it is already at
9606 START_POS and will not move. */
9607 || (it2.method == GET_FROM_STRING
9608 && IT_CHARPOS (it2) == start_pos
9609 && SREF (it2.string, IT_STRING_BYTEPOS (it2) - 1) == '\n')));
9610 eassert (IT_CHARPOS (*it) >= BEGV);
9611 SAVE_IT (it3, it2, it3data);
9613 move_it_to (&it2, start_pos, -1, -1, -1, MOVE_TO_POS);
9614 eassert (IT_CHARPOS (*it) >= BEGV);
9615 /* H is the actual vertical distance from the position in *IT
9616 and the starting position. */
9617 h = it2.current_y - it->current_y;
9618 /* NLINES is the distance in number of lines. */
9619 nlines = it2.vpos - it->vpos;
9621 /* Correct IT's y and vpos position
9622 so that they are relative to the starting point. */
9623 it->vpos -= nlines;
9624 it->current_y -= h;
9626 if (dy == 0)
9628 /* DY == 0 means move to the start of the screen line. The
9629 value of nlines is > 0 if continuation lines were involved,
9630 or if the original IT position was at start of a line. */
9631 RESTORE_IT (it, it, it2data);
9632 if (nlines > 0)
9633 move_it_by_lines (it, nlines);
9634 /* The above code moves us to some position NLINES down,
9635 usually to its first glyph (leftmost in an L2R line), but
9636 that's not necessarily the start of the line, under bidi
9637 reordering. We want to get to the character position
9638 that is immediately after the newline of the previous
9639 line. */
9640 if (it->bidi_p
9641 && !it->continuation_lines_width
9642 && !STRINGP (it->string)
9643 && IT_CHARPOS (*it) > BEGV
9644 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
9646 ptrdiff_t cp = IT_CHARPOS (*it), bp = IT_BYTEPOS (*it);
9648 DEC_BOTH (cp, bp);
9649 cp = find_newline_no_quit (cp, bp, -1, NULL);
9650 move_it_to (it, cp, -1, -1, -1, MOVE_TO_POS);
9652 bidi_unshelve_cache (it3data, true);
9654 else
9656 /* The y-position we try to reach, relative to *IT.
9657 Note that H has been subtracted in front of the if-statement. */
9658 int target_y = it->current_y + h - dy;
9659 int y0 = it3.current_y;
9660 int y1;
9661 int line_height;
9663 RESTORE_IT (&it3, &it3, it3data);
9664 y1 = line_bottom_y (&it3);
9665 line_height = y1 - y0;
9666 RESTORE_IT (it, it, it2data);
9667 /* If we did not reach target_y, try to move further backward if
9668 we can. If we moved too far backward, try to move forward. */
9669 if (target_y < it->current_y
9670 /* This is heuristic. In a window that's 3 lines high, with
9671 a line height of 13 pixels each, recentering with point
9672 on the bottom line will try to move -39/2 = 19 pixels
9673 backward. Try to avoid moving into the first line. */
9674 && (it->current_y - target_y
9675 > min (window_box_height (it->w), line_height * 2 / 3))
9676 && IT_CHARPOS (*it) > BEGV)
9678 TRACE_MOVE ((stderr, " not far enough -> move_vert %d\n",
9679 target_y - it->current_y));
9680 dy = it->current_y - target_y;
9681 goto move_further_back;
9683 else if (target_y >= it->current_y + line_height
9684 && IT_CHARPOS (*it) < ZV)
9686 /* Should move forward by at least one line, maybe more.
9688 Note: Calling move_it_by_lines can be expensive on
9689 terminal frames, where compute_motion is used (via
9690 vmotion) to do the job, when there are very long lines
9691 and truncate-lines is nil. That's the reason for
9692 treating terminal frames specially here. */
9694 if (!FRAME_WINDOW_P (it->f))
9695 move_it_vertically (it, target_y - it->current_y);
9696 else
9700 move_it_by_lines (it, 1);
9702 while (target_y >= line_bottom_y (it) && IT_CHARPOS (*it) < ZV);
9709 /* Move IT by a specified amount of pixel lines DY. DY negative means
9710 move backwards. DY = 0 means move to start of screen line. At the
9711 end, IT will be on the start of a screen line. */
9713 void
9714 move_it_vertically (struct it *it, int dy)
9716 if (dy <= 0)
9717 move_it_vertically_backward (it, -dy);
9718 else
9720 TRACE_MOVE ((stderr, "move_it_v: from %d, %d\n", IT_CHARPOS (*it), dy));
9721 move_it_to (it, ZV, -1, it->current_y + dy, -1,
9722 MOVE_TO_POS | MOVE_TO_Y);
9723 TRACE_MOVE ((stderr, "move_it_v: to %d\n", IT_CHARPOS (*it)));
9725 /* If buffer ends in ZV without a newline, move to the start of
9726 the line to satisfy the post-condition. */
9727 if (IT_CHARPOS (*it) == ZV
9728 && ZV > BEGV
9729 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
9730 move_it_by_lines (it, 0);
9735 /* Move iterator IT past the end of the text line it is in. */
9737 void
9738 move_it_past_eol (struct it *it)
9740 enum move_it_result rc;
9742 rc = move_it_in_display_line_to (it, Z, 0, MOVE_TO_POS);
9743 if (rc == MOVE_NEWLINE_OR_CR)
9744 set_iterator_to_next (it, false);
9748 /* Move IT by a specified number DVPOS of screen lines down. DVPOS
9749 negative means move up. DVPOS == 0 means move to the start of the
9750 screen line.
9752 Optimization idea: If we would know that IT->f doesn't use
9753 a face with proportional font, we could be faster for
9754 truncate-lines nil. */
9756 void
9757 move_it_by_lines (struct it *it, ptrdiff_t dvpos)
9760 /* The commented-out optimization uses vmotion on terminals. This
9761 gives bad results, because elements like it->what, on which
9762 callers such as pos_visible_p rely, aren't updated. */
9763 /* struct position pos;
9764 if (!FRAME_WINDOW_P (it->f))
9766 struct text_pos textpos;
9768 pos = *vmotion (IT_CHARPOS (*it), dvpos, it->w);
9769 SET_TEXT_POS (textpos, pos.bufpos, pos.bytepos);
9770 reseat (it, textpos, true);
9771 it->vpos += pos.vpos;
9772 it->current_y += pos.vpos;
9774 else */
9776 if (dvpos == 0)
9778 /* DVPOS == 0 means move to the start of the screen line. */
9779 move_it_vertically_backward (it, 0);
9780 /* Let next call to line_bottom_y calculate real line height. */
9781 last_height = 0;
9783 else if (dvpos > 0)
9785 move_it_to (it, -1, -1, -1, it->vpos + dvpos, MOVE_TO_VPOS);
9786 if (!IT_POS_VALID_AFTER_MOVE_P (it))
9788 /* Only move to the next buffer position if we ended up in a
9789 string from display property, not in an overlay string
9790 (before-string or after-string). That is because the
9791 latter don't conceal the underlying buffer position, so
9792 we can ask to move the iterator to the exact position we
9793 are interested in. Note that, even if we are already at
9794 IT_CHARPOS (*it), the call below is not a no-op, as it
9795 will detect that we are at the end of the string, pop the
9796 iterator, and compute it->current_x and it->hpos
9797 correctly. */
9798 move_it_to (it, IT_CHARPOS (*it) + it->string_from_display_prop_p,
9799 -1, -1, -1, MOVE_TO_POS);
9802 else
9804 struct it it2;
9805 void *it2data = NULL;
9806 ptrdiff_t start_charpos, i;
9807 int nchars_per_row
9808 = (it->last_visible_x - it->first_visible_x) / FRAME_COLUMN_WIDTH (it->f);
9809 bool hit_pos_limit = false;
9810 ptrdiff_t pos_limit;
9812 /* Start at the beginning of the screen line containing IT's
9813 position. This may actually move vertically backwards,
9814 in case of overlays, so adjust dvpos accordingly. */
9815 dvpos += it->vpos;
9816 move_it_vertically_backward (it, 0);
9817 dvpos -= it->vpos;
9819 /* Go back -DVPOS buffer lines, but no farther than -DVPOS full
9820 screen lines, and reseat the iterator there. */
9821 start_charpos = IT_CHARPOS (*it);
9822 if (it->line_wrap == TRUNCATE || nchars_per_row == 0)
9823 pos_limit = BEGV;
9824 else
9825 pos_limit = max (start_charpos + dvpos * nchars_per_row, BEGV);
9827 for (i = -dvpos; i > 0 && IT_CHARPOS (*it) > pos_limit; --i)
9828 back_to_previous_visible_line_start (it);
9829 if (i > 0 && IT_CHARPOS (*it) <= pos_limit)
9830 hit_pos_limit = true;
9831 reseat (it, it->current.pos, true);
9833 /* Move further back if we end up in a string or an image. */
9834 while (!IT_POS_VALID_AFTER_MOVE_P (it))
9836 /* First try to move to start of display line. */
9837 dvpos += it->vpos;
9838 move_it_vertically_backward (it, 0);
9839 dvpos -= it->vpos;
9840 if (IT_POS_VALID_AFTER_MOVE_P (it))
9841 break;
9842 /* If start of line is still in string or image,
9843 move further back. */
9844 back_to_previous_visible_line_start (it);
9845 reseat (it, it->current.pos, true);
9846 dvpos--;
9849 it->current_x = it->hpos = 0;
9851 /* Above call may have moved too far if continuation lines
9852 are involved. Scan forward and see if it did. */
9853 SAVE_IT (it2, *it, it2data);
9854 it2.vpos = it2.current_y = 0;
9855 move_it_to (&it2, start_charpos, -1, -1, -1, MOVE_TO_POS);
9856 it->vpos -= it2.vpos;
9857 it->current_y -= it2.current_y;
9858 it->current_x = it->hpos = 0;
9860 /* If we moved too far back, move IT some lines forward. */
9861 if (it2.vpos > -dvpos)
9863 int delta = it2.vpos + dvpos;
9865 RESTORE_IT (&it2, &it2, it2data);
9866 SAVE_IT (it2, *it, it2data);
9867 move_it_to (it, -1, -1, -1, it->vpos + delta, MOVE_TO_VPOS);
9868 /* Move back again if we got too far ahead. */
9869 if (IT_CHARPOS (*it) >= start_charpos)
9870 RESTORE_IT (it, &it2, it2data);
9871 else
9872 bidi_unshelve_cache (it2data, true);
9874 else if (hit_pos_limit && pos_limit > BEGV
9875 && dvpos < 0 && it2.vpos < -dvpos)
9877 /* If we hit the limit, but still didn't make it far enough
9878 back, that means there's a display string with a newline
9879 covering a large chunk of text, and that caused
9880 back_to_previous_visible_line_start try to go too far.
9881 Punish those who commit such atrocities by going back
9882 until we've reached DVPOS, after lifting the limit, which
9883 could make it slow for very long lines. "If it hurts,
9884 don't do that!" */
9885 dvpos += it2.vpos;
9886 RESTORE_IT (it, it, it2data);
9887 for (i = -dvpos; i > 0; --i)
9889 back_to_previous_visible_line_start (it);
9890 it->vpos--;
9892 reseat_1 (it, it->current.pos, true);
9894 else
9895 RESTORE_IT (it, it, it2data);
9900 partial_line_height (struct it *it_origin)
9902 int partial_height;
9903 void *it_data = NULL;
9904 struct it it;
9905 SAVE_IT (it, *it_origin, it_data);
9906 move_it_to (&it, ZV, -1, it.last_visible_y, -1,
9907 MOVE_TO_POS | MOVE_TO_Y);
9908 if (it.what == IT_EOB)
9910 int vis_height = it.last_visible_y - it.current_y;
9911 int height = it.ascent + it.descent;
9912 partial_height = (vis_height < height) ? vis_height : 0;
9914 else
9916 int last_line_y = it.current_y;
9917 move_it_by_lines (&it, 1);
9918 partial_height = (it.current_y > it.last_visible_y)
9919 ? it.last_visible_y - last_line_y : 0;
9921 RESTORE_IT (&it, &it, it_data);
9922 return partial_height;
9925 /* Return true if IT points into the middle of a display vector. */
9927 bool
9928 in_display_vector_p (struct it *it)
9930 return (it->method == GET_FROM_DISPLAY_VECTOR
9931 && it->current.dpvec_index > 0
9932 && it->dpvec + it->current.dpvec_index != it->dpend);
9935 DEFUN ("window-text-pixel-size", Fwindow_text_pixel_size, Swindow_text_pixel_size, 0, 6, 0,
9936 doc: /* Return the size of the text of WINDOW's buffer in pixels.
9937 WINDOW must be a live window and defaults to the selected one. The
9938 return value is a cons of the maximum pixel-width of any text line and
9939 the maximum pixel-height of all text lines.
9941 The optional argument FROM, if non-nil, specifies the first text
9942 position and defaults to the minimum accessible position of the buffer.
9943 If FROM is t, use the minimum accessible position that starts a
9944 non-empty line. TO, if non-nil, specifies the last text position and
9945 defaults to the maximum accessible position of the buffer. If TO is t,
9946 use the maximum accessible position that ends a non-empty line.
9948 The optional argument X-LIMIT, if non-nil, specifies the maximum text
9949 width that can be returned. X-LIMIT nil or omitted, means to use the
9950 pixel-width of WINDOW's body; use this if you want to know how high
9951 WINDOW should be become in order to fit all of its buffer's text with
9952 the width of WINDOW unaltered. Use the maximum width WINDOW may assume
9953 if you intend to change WINDOW's width. In any case, text whose
9954 x-coordinate is beyond X-LIMIT is ignored. Since calculating the width
9955 of long lines can take some time, it's always a good idea to make this
9956 argument as small as possible; in particular, if the buffer contains
9957 long lines that shall be truncated anyway.
9959 The optional argument Y-LIMIT, if non-nil, specifies the maximum text
9960 height (excluding the height of the mode- or header-line, if any) that
9961 can be returned. Text lines whose y-coordinate is beyond Y-LIMIT are
9962 ignored. Since calculating the text height of a large buffer can take
9963 some time, it makes sense to specify this argument if the size of the
9964 buffer is large or unknown.
9966 Optional argument MODE-AND-HEADER-LINE nil or omitted means do not
9967 include the height of the mode- or header-line of WINDOW in the return
9968 value. If it is either the symbol `mode-line' or `header-line', include
9969 only the height of that line, if present, in the return value. If t,
9970 include the height of both, if present, in the return value. */)
9971 (Lisp_Object window, Lisp_Object from, Lisp_Object to, Lisp_Object x_limit,
9972 Lisp_Object y_limit, Lisp_Object mode_and_header_line)
9974 struct window *w = decode_live_window (window);
9975 Lisp_Object buffer = w->contents;
9976 struct buffer *b;
9977 struct it it;
9978 struct buffer *old_b = NULL;
9979 ptrdiff_t start, end, pos;
9980 struct text_pos startp;
9981 void *itdata = NULL;
9982 int c, max_x = 0, max_y = 0, x = 0, y = 0;
9984 CHECK_BUFFER (buffer);
9985 b = XBUFFER (buffer);
9987 if (b != current_buffer)
9989 old_b = current_buffer;
9990 set_buffer_internal (b);
9993 if (NILP (from))
9994 start = BEGV;
9995 else if (EQ (from, Qt))
9997 start = pos = BEGV;
9998 while ((pos++ < ZV) && (c = FETCH_CHAR (pos))
9999 && (c == ' ' || c == '\t' || c == '\n' || c == '\r'))
10000 start = pos;
10001 while ((pos-- > BEGV) && (c = FETCH_CHAR (pos)) && (c == ' ' || c == '\t'))
10002 start = pos;
10004 else
10006 CHECK_NUMBER_COERCE_MARKER (from);
10007 start = min (max (XINT (from), BEGV), ZV);
10010 if (NILP (to))
10011 end = ZV;
10012 else if (EQ (to, Qt))
10014 end = pos = ZV;
10015 while ((pos-- > BEGV) && (c = FETCH_CHAR (pos))
10016 && (c == ' ' || c == '\t' || c == '\n' || c == '\r'))
10017 end = pos;
10018 while ((pos++ < ZV) && (c = FETCH_CHAR (pos)) && (c == ' ' || c == '\t'))
10019 end = pos;
10021 else
10023 CHECK_NUMBER_COERCE_MARKER (to);
10024 end = max (start, min (XINT (to), ZV));
10027 if (!NILP (x_limit) && RANGED_INTEGERP (0, x_limit, INT_MAX))
10028 max_x = XINT (x_limit);
10030 if (NILP (y_limit))
10031 max_y = INT_MAX;
10032 else if (RANGED_INTEGERP (0, y_limit, INT_MAX))
10033 max_y = XINT (y_limit);
10035 itdata = bidi_shelve_cache ();
10036 SET_TEXT_POS (startp, start, CHAR_TO_BYTE (start));
10037 start_display (&it, w, startp);
10039 if (NILP (x_limit))
10040 x = move_it_to (&it, end, -1, max_y, -1, MOVE_TO_POS | MOVE_TO_Y);
10041 else
10043 it.last_visible_x = max_x;
10044 /* Actually, we never want move_it_to stop at to_x. But to make
10045 sure that move_it_in_display_line_to always moves far enough,
10046 we set it to INT_MAX and specify MOVE_TO_X. */
10047 x = move_it_to (&it, end, INT_MAX, max_y, -1,
10048 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
10049 /* Don't return more than X-LIMIT. */
10050 if (x > max_x)
10051 x = max_x;
10054 /* Subtract height of header-line which was counted automatically by
10055 start_display. */
10056 y = it.current_y + it.max_ascent + it.max_descent
10057 - WINDOW_HEADER_LINE_HEIGHT (w);
10058 /* Don't return more than Y-LIMIT. */
10059 if (y > max_y)
10060 y = max_y;
10062 if (EQ (mode_and_header_line, Qheader_line)
10063 || EQ (mode_and_header_line, Qt))
10064 /* Re-add height of header-line as requested. */
10065 y = y + WINDOW_HEADER_LINE_HEIGHT (w);
10067 if (EQ (mode_and_header_line, Qmode_line)
10068 || EQ (mode_and_header_line, Qt))
10069 /* Add height of mode-line as requested. */
10070 y = y + WINDOW_MODE_LINE_HEIGHT (w);
10072 bidi_unshelve_cache (itdata, false);
10074 if (old_b)
10075 set_buffer_internal (old_b);
10077 return Fcons (make_number (x), make_number (y));
10080 /***********************************************************************
10081 Messages
10082 ***********************************************************************/
10084 /* Return the number of arguments the format string FORMAT needs. */
10086 static ptrdiff_t
10087 format_nargs (char const *format)
10089 ptrdiff_t nargs = 0;
10090 for (char const *p = format; (p = strchr (p, '%')); p++)
10091 if (p[1] == '%')
10092 p++;
10093 else
10094 nargs++;
10095 return nargs;
10098 /* Add a message with format string FORMAT and formatted arguments
10099 to *Messages*. */
10101 void
10102 add_to_log (const char *format, ...)
10104 va_list ap;
10105 va_start (ap, format);
10106 vadd_to_log (format, ap);
10107 va_end (ap);
10110 void
10111 vadd_to_log (char const *format, va_list ap)
10113 ptrdiff_t form_nargs = format_nargs (format);
10114 ptrdiff_t nargs = 1 + form_nargs;
10115 Lisp_Object args[10];
10116 eassert (nargs <= ARRAYELTS (args));
10117 AUTO_STRING (args0, format);
10118 args[0] = args0;
10119 for (ptrdiff_t i = 1; i <= nargs; i++)
10120 args[i] = va_arg (ap, Lisp_Object);
10121 Lisp_Object msg = Qnil;
10122 msg = Fformat_message (nargs, args);
10124 ptrdiff_t len = SBYTES (msg) + 1;
10125 USE_SAFE_ALLOCA;
10126 char *buffer = SAFE_ALLOCA (len);
10127 memcpy (buffer, SDATA (msg), len);
10129 message_dolog (buffer, len - 1, true, STRING_MULTIBYTE (msg));
10130 SAFE_FREE ();
10134 /* Output a newline in the *Messages* buffer if "needs" one. */
10136 void
10137 message_log_maybe_newline (void)
10139 if (message_log_need_newline)
10140 message_dolog ("", 0, true, false);
10144 /* Add a string M of length NBYTES to the message log, optionally
10145 terminated with a newline when NLFLAG is true. MULTIBYTE, if
10146 true, means interpret the contents of M as multibyte. This
10147 function calls low-level routines in order to bypass text property
10148 hooks, etc. which might not be safe to run.
10150 This may GC (insert may run before/after change hooks),
10151 so the buffer M must NOT point to a Lisp string. */
10153 void
10154 message_dolog (const char *m, ptrdiff_t nbytes, bool nlflag, bool multibyte)
10156 const unsigned char *msg = (const unsigned char *) m;
10158 if (!NILP (Vmemory_full))
10159 return;
10161 if (!NILP (Vmessage_log_max))
10163 struct buffer *oldbuf;
10164 Lisp_Object oldpoint, oldbegv, oldzv;
10165 int old_windows_or_buffers_changed = windows_or_buffers_changed;
10166 ptrdiff_t point_at_end = 0;
10167 ptrdiff_t zv_at_end = 0;
10168 Lisp_Object old_deactivate_mark;
10170 old_deactivate_mark = Vdeactivate_mark;
10171 oldbuf = current_buffer;
10173 /* Ensure the Messages buffer exists, and switch to it.
10174 If we created it, set the major-mode. */
10175 bool newbuffer = NILP (Fget_buffer (Vmessages_buffer_name));
10176 Fset_buffer (Fget_buffer_create (Vmessages_buffer_name));
10177 if (newbuffer
10178 && !NILP (Ffboundp (intern ("messages-buffer-mode"))))
10179 call0 (intern ("messages-buffer-mode"));
10181 bset_undo_list (current_buffer, Qt);
10182 bset_cache_long_scans (current_buffer, Qnil);
10184 oldpoint = message_dolog_marker1;
10185 set_marker_restricted_both (oldpoint, Qnil, PT, PT_BYTE);
10186 oldbegv = message_dolog_marker2;
10187 set_marker_restricted_both (oldbegv, Qnil, BEGV, BEGV_BYTE);
10188 oldzv = message_dolog_marker3;
10189 set_marker_restricted_both (oldzv, Qnil, ZV, ZV_BYTE);
10191 if (PT == Z)
10192 point_at_end = 1;
10193 if (ZV == Z)
10194 zv_at_end = 1;
10196 BEGV = BEG;
10197 BEGV_BYTE = BEG_BYTE;
10198 ZV = Z;
10199 ZV_BYTE = Z_BYTE;
10200 TEMP_SET_PT_BOTH (Z, Z_BYTE);
10202 /* Insert the string--maybe converting multibyte to single byte
10203 or vice versa, so that all the text fits the buffer. */
10204 if (multibyte
10205 && NILP (BVAR (current_buffer, enable_multibyte_characters)))
10207 ptrdiff_t i;
10208 int c, char_bytes;
10209 char work[1];
10211 /* Convert a multibyte string to single-byte
10212 for the *Message* buffer. */
10213 for (i = 0; i < nbytes; i += char_bytes)
10215 c = string_char_and_length (msg + i, &char_bytes);
10216 work[0] = CHAR_TO_BYTE8 (c);
10217 insert_1_both (work, 1, 1, true, false, false);
10220 else if (! multibyte
10221 && ! NILP (BVAR (current_buffer, enable_multibyte_characters)))
10223 ptrdiff_t i;
10224 int c, char_bytes;
10225 unsigned char str[MAX_MULTIBYTE_LENGTH];
10226 /* Convert a single-byte string to multibyte
10227 for the *Message* buffer. */
10228 for (i = 0; i < nbytes; i++)
10230 c = msg[i];
10231 MAKE_CHAR_MULTIBYTE (c);
10232 char_bytes = CHAR_STRING (c, str);
10233 insert_1_both ((char *) str, 1, char_bytes, true, false, false);
10236 else if (nbytes)
10237 insert_1_both (m, chars_in_text (msg, nbytes), nbytes,
10238 true, false, false);
10240 if (nlflag)
10242 ptrdiff_t this_bol, this_bol_byte, prev_bol, prev_bol_byte;
10243 printmax_t dups;
10245 insert_1_both ("\n", 1, 1, true, false, false);
10247 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE, -2, false);
10248 this_bol = PT;
10249 this_bol_byte = PT_BYTE;
10251 /* See if this line duplicates the previous one.
10252 If so, combine duplicates. */
10253 if (this_bol > BEG)
10255 scan_newline (PT, PT_BYTE, BEG, BEG_BYTE, -2, false);
10256 prev_bol = PT;
10257 prev_bol_byte = PT_BYTE;
10259 dups = message_log_check_duplicate (prev_bol_byte,
10260 this_bol_byte);
10261 if (dups)
10263 del_range_both (prev_bol, prev_bol_byte,
10264 this_bol, this_bol_byte, false);
10265 if (dups > 1)
10267 char dupstr[sizeof " [ times]"
10268 + INT_STRLEN_BOUND (printmax_t)];
10270 /* If you change this format, don't forget to also
10271 change message_log_check_duplicate. */
10272 int duplen = sprintf (dupstr, " [%"pMd" times]", dups);
10273 TEMP_SET_PT_BOTH (Z - 1, Z_BYTE - 1);
10274 insert_1_both (dupstr, duplen, duplen,
10275 true, false, true);
10280 /* If we have more than the desired maximum number of lines
10281 in the *Messages* buffer now, delete the oldest ones.
10282 This is safe because we don't have undo in this buffer. */
10284 if (NATNUMP (Vmessage_log_max))
10286 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE,
10287 -XFASTINT (Vmessage_log_max) - 1, false);
10288 del_range_both (BEG, BEG_BYTE, PT, PT_BYTE, false);
10291 BEGV = marker_position (oldbegv);
10292 BEGV_BYTE = marker_byte_position (oldbegv);
10294 if (zv_at_end)
10296 ZV = Z;
10297 ZV_BYTE = Z_BYTE;
10299 else
10301 ZV = marker_position (oldzv);
10302 ZV_BYTE = marker_byte_position (oldzv);
10305 if (point_at_end)
10306 TEMP_SET_PT_BOTH (Z, Z_BYTE);
10307 else
10308 /* We can't do Fgoto_char (oldpoint) because it will run some
10309 Lisp code. */
10310 TEMP_SET_PT_BOTH (marker_position (oldpoint),
10311 marker_byte_position (oldpoint));
10313 unchain_marker (XMARKER (oldpoint));
10314 unchain_marker (XMARKER (oldbegv));
10315 unchain_marker (XMARKER (oldzv));
10317 /* We called insert_1_both above with its 5th argument (PREPARE)
10318 false, which prevents insert_1_both from calling
10319 prepare_to_modify_buffer, which in turns prevents us from
10320 incrementing windows_or_buffers_changed even if *Messages* is
10321 shown in some window. So we must manually set
10322 windows_or_buffers_changed here to make up for that. */
10323 windows_or_buffers_changed = old_windows_or_buffers_changed;
10324 bset_redisplay (current_buffer);
10326 set_buffer_internal (oldbuf);
10328 message_log_need_newline = !nlflag;
10329 Vdeactivate_mark = old_deactivate_mark;
10334 /* We are at the end of the buffer after just having inserted a newline.
10335 (Note: We depend on the fact we won't be crossing the gap.)
10336 Check to see if the most recent message looks a lot like the previous one.
10337 Return 0 if different, 1 if the new one should just replace it, or a
10338 value N > 1 if we should also append " [N times]". */
10340 static intmax_t
10341 message_log_check_duplicate (ptrdiff_t prev_bol_byte, ptrdiff_t this_bol_byte)
10343 ptrdiff_t i;
10344 ptrdiff_t len = Z_BYTE - 1 - this_bol_byte;
10345 bool seen_dots = false;
10346 unsigned char *p1 = BUF_BYTE_ADDRESS (current_buffer, prev_bol_byte);
10347 unsigned char *p2 = BUF_BYTE_ADDRESS (current_buffer, this_bol_byte);
10349 for (i = 0; i < len; i++)
10351 if (i >= 3 && p1[i - 3] == '.' && p1[i - 2] == '.' && p1[i - 1] == '.')
10352 seen_dots = true;
10353 if (p1[i] != p2[i])
10354 return seen_dots;
10356 p1 += len;
10357 if (*p1 == '\n')
10358 return 2;
10359 if (*p1++ == ' ' && *p1++ == '[')
10361 char *pend;
10362 intmax_t n = strtoimax ((char *) p1, &pend, 10);
10363 if (0 < n && n < INTMAX_MAX && strncmp (pend, " times]\n", 8) == 0)
10364 return n + 1;
10366 return 0;
10370 /* Display an echo area message M with a specified length of NBYTES
10371 bytes. The string may include null characters. If M is not a
10372 string, clear out any existing message, and let the mini-buffer
10373 text show through.
10375 This function cancels echoing. */
10377 void
10378 message3 (Lisp_Object m)
10380 clear_message (true, true);
10381 cancel_echoing ();
10383 /* First flush out any partial line written with print. */
10384 message_log_maybe_newline ();
10385 if (STRINGP (m))
10387 ptrdiff_t nbytes = SBYTES (m);
10388 bool multibyte = STRING_MULTIBYTE (m);
10389 char *buffer;
10390 USE_SAFE_ALLOCA;
10391 SAFE_ALLOCA_STRING (buffer, m);
10392 message_dolog (buffer, nbytes, true, multibyte);
10393 SAFE_FREE ();
10395 if (! inhibit_message)
10396 message3_nolog (m);
10399 /* Log the message M to stderr. Log an empty line if M is not a string. */
10401 static void
10402 message_to_stderr (Lisp_Object m)
10404 if (noninteractive_need_newline)
10406 noninteractive_need_newline = false;
10407 fputc ('\n', stderr);
10409 if (STRINGP (m))
10411 Lisp_Object coding_system = Vlocale_coding_system;
10412 Lisp_Object s;
10414 if (!NILP (Vcoding_system_for_write))
10415 coding_system = Vcoding_system_for_write;
10416 if (!NILP (coding_system))
10417 s = code_convert_string_norecord (m, coding_system, true);
10418 else
10419 s = m;
10421 fwrite (SDATA (s), SBYTES (s), 1, stderr);
10423 if (!cursor_in_echo_area)
10424 fputc ('\n', stderr);
10425 fflush (stderr);
10428 /* The non-logging version of message3.
10429 This does not cancel echoing, because it is used for echoing.
10430 Perhaps we need to make a separate function for echoing
10431 and make this cancel echoing. */
10433 void
10434 message3_nolog (Lisp_Object m)
10436 struct frame *sf = SELECTED_FRAME ();
10438 if (FRAME_INITIAL_P (sf))
10439 message_to_stderr (m);
10440 /* Error messages get reported properly by cmd_error, so this must be just an
10441 informative message; if the frame hasn't really been initialized yet, just
10442 toss it. */
10443 else if (INTERACTIVE && sf->glyphs_initialized_p)
10445 /* Get the frame containing the mini-buffer
10446 that the selected frame is using. */
10447 Lisp_Object mini_window = FRAME_MINIBUF_WINDOW (sf);
10448 Lisp_Object frame = XWINDOW (mini_window)->frame;
10449 struct frame *f = XFRAME (frame);
10451 if (FRAME_VISIBLE_P (sf) && !FRAME_VISIBLE_P (f))
10452 Fmake_frame_visible (frame);
10454 if (STRINGP (m) && SCHARS (m) > 0)
10456 set_message (m);
10457 if (minibuffer_auto_raise)
10458 Fraise_frame (frame);
10459 /* Assume we are not echoing.
10460 (If we are, echo_now will override this.) */
10461 echo_message_buffer = Qnil;
10463 else
10464 clear_message (true, true);
10466 do_pending_window_change (false);
10467 echo_area_display (true);
10468 do_pending_window_change (false);
10469 if (FRAME_TERMINAL (f)->frame_up_to_date_hook)
10470 (*FRAME_TERMINAL (f)->frame_up_to_date_hook) (f);
10475 /* Display a null-terminated echo area message M. If M is 0, clear
10476 out any existing message, and let the mini-buffer text show through.
10478 The buffer M must continue to exist until after the echo area gets
10479 cleared or some other message gets displayed there. Do not pass
10480 text that is stored in a Lisp string. Do not pass text in a buffer
10481 that was alloca'd. */
10483 void
10484 message1 (const char *m)
10486 message3 (m ? build_unibyte_string (m) : Qnil);
10490 /* The non-logging counterpart of message1. */
10492 void
10493 message1_nolog (const char *m)
10495 message3_nolog (m ? build_unibyte_string (m) : Qnil);
10498 /* Display a message M which contains a single %s
10499 which gets replaced with STRING. */
10501 void
10502 message_with_string (const char *m, Lisp_Object string, bool log)
10504 CHECK_STRING (string);
10506 bool need_message;
10507 if (noninteractive)
10508 need_message = !!m;
10509 else if (!INTERACTIVE)
10510 need_message = false;
10511 else
10513 /* The frame whose minibuffer we're going to display the message on.
10514 It may be larger than the selected frame, so we need
10515 to use its buffer, not the selected frame's buffer. */
10516 Lisp_Object mini_window;
10517 struct frame *f, *sf = SELECTED_FRAME ();
10519 /* Get the frame containing the minibuffer
10520 that the selected frame is using. */
10521 mini_window = FRAME_MINIBUF_WINDOW (sf);
10522 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
10524 /* Error messages get reported properly by cmd_error, so this must be
10525 just an informative message; if the frame hasn't really been
10526 initialized yet, just toss it. */
10527 need_message = f->glyphs_initialized_p;
10530 if (need_message)
10532 AUTO_STRING (fmt, m);
10533 Lisp_Object msg = CALLN (Fformat_message, fmt, string);
10535 if (noninteractive)
10536 message_to_stderr (msg);
10537 else
10539 if (log)
10540 message3 (msg);
10541 else
10542 message3_nolog (msg);
10544 /* Print should start at the beginning of the message
10545 buffer next time. */
10546 message_buf_print = false;
10552 /* Dump an informative message to the minibuf. If M is 0, clear out
10553 any existing message, and let the mini-buffer text show through.
10555 The message must be safe ASCII (because when Emacs is
10556 non-interactive the message is sent straight to stderr without
10557 encoding first) and the format must not contain ` or ' (because
10558 this function does not account for `text-quoting-style'). If your
10559 message and format do not fit into this category, convert your
10560 arguments to Lisp objects and use Fmessage instead. */
10562 static void ATTRIBUTE_FORMAT_PRINTF (1, 0)
10563 vmessage (const char *m, va_list ap)
10565 if (noninteractive)
10567 if (m)
10569 if (noninteractive_need_newline)
10570 putc ('\n', stderr);
10571 noninteractive_need_newline = false;
10572 vfprintf (stderr, m, ap);
10573 if (!cursor_in_echo_area)
10574 fprintf (stderr, "\n");
10575 fflush (stderr);
10578 else if (INTERACTIVE)
10580 /* The frame whose mini-buffer we're going to display the message
10581 on. It may be larger than the selected frame, so we need to
10582 use its buffer, not the selected frame's buffer. */
10583 Lisp_Object mini_window;
10584 struct frame *f, *sf = SELECTED_FRAME ();
10586 /* Get the frame containing the mini-buffer
10587 that the selected frame is using. */
10588 mini_window = FRAME_MINIBUF_WINDOW (sf);
10589 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
10591 /* Error messages get reported properly by cmd_error, so this must be
10592 just an informative message; if the frame hasn't really been
10593 initialized yet, just toss it. */
10594 if (f->glyphs_initialized_p)
10596 if (m)
10598 ptrdiff_t len;
10599 ptrdiff_t maxsize = FRAME_MESSAGE_BUF_SIZE (f);
10600 USE_SAFE_ALLOCA;
10601 char *message_buf = SAFE_ALLOCA (maxsize + 1);
10603 len = doprnt (message_buf, maxsize, m, 0, ap);
10605 message3 (make_string (message_buf, len));
10606 SAFE_FREE ();
10608 else
10609 message1 (0);
10611 /* Print should start at the beginning of the message
10612 buffer next time. */
10613 message_buf_print = false;
10618 /* See vmessage for restrictions on the text of the message. */
10619 void
10620 message (const char *m, ...)
10622 va_list ap;
10623 va_start (ap, m);
10624 vmessage (m, ap);
10625 va_end (ap);
10629 /* Display the current message in the current mini-buffer. This is
10630 only called from error handlers in process.c, and is not time
10631 critical. */
10633 void
10634 update_echo_area (void)
10636 if (!NILP (echo_area_buffer[0]))
10638 Lisp_Object string;
10639 string = Fcurrent_message ();
10640 message3 (string);
10645 /* Make sure echo area buffers in `echo_buffers' are live.
10646 If they aren't, make new ones. */
10648 static void
10649 ensure_echo_area_buffers (void)
10651 for (int i = 0; i < 2; i++)
10652 if (!BUFFERP (echo_buffer[i])
10653 || !BUFFER_LIVE_P (XBUFFER (echo_buffer[i])))
10655 Lisp_Object old_buffer = echo_buffer[i];
10656 static char const name_fmt[] = " *Echo Area %d*";
10657 char name[sizeof name_fmt + INT_STRLEN_BOUND (int)];
10658 AUTO_STRING_WITH_LEN (lname, name, sprintf (name, name_fmt, i));
10659 echo_buffer[i] = Fget_buffer_create (lname);
10660 bset_truncate_lines (XBUFFER (echo_buffer[i]), Qnil);
10661 /* to force word wrap in echo area -
10662 it was decided to postpone this*/
10663 /* XBUFFER (echo_buffer[i])->word_wrap = Qt; */
10665 for (int j = 0; j < 2; j++)
10666 if (EQ (old_buffer, echo_area_buffer[j]))
10667 echo_area_buffer[j] = echo_buffer[i];
10672 /* Call FN with args A1..A2 with either the current or last displayed
10673 echo_area_buffer as current buffer.
10675 WHICH zero means use the current message buffer
10676 echo_area_buffer[0]. If that is nil, choose a suitable buffer
10677 from echo_buffer[] and clear it.
10679 WHICH > 0 means use echo_area_buffer[1]. If that is nil, choose a
10680 suitable buffer from echo_buffer[] and clear it.
10682 If WHICH < 0, set echo_area_buffer[1] to echo_area_buffer[0], so
10683 that the current message becomes the last displayed one, choose a
10684 suitable buffer for echo_area_buffer[0], and clear it.
10686 Value is what FN returns. */
10688 static bool
10689 with_echo_area_buffer (struct window *w, int which,
10690 bool (*fn) (ptrdiff_t, Lisp_Object),
10691 ptrdiff_t a1, Lisp_Object a2)
10693 Lisp_Object buffer;
10694 bool this_one, the_other, clear_buffer_p, rc;
10695 ptrdiff_t count = SPECPDL_INDEX ();
10697 /* If buffers aren't live, make new ones. */
10698 ensure_echo_area_buffers ();
10700 clear_buffer_p = false;
10702 if (which == 0)
10703 this_one = false, the_other = true;
10704 else if (which > 0)
10705 this_one = true, the_other = false;
10706 else
10708 this_one = false, the_other = true;
10709 clear_buffer_p = true;
10711 /* We need a fresh one in case the current echo buffer equals
10712 the one containing the last displayed echo area message. */
10713 if (!NILP (echo_area_buffer[this_one])
10714 && EQ (echo_area_buffer[this_one], echo_area_buffer[the_other]))
10715 echo_area_buffer[this_one] = Qnil;
10718 /* Choose a suitable buffer from echo_buffer[] if we don't
10719 have one. */
10720 if (NILP (echo_area_buffer[this_one]))
10722 echo_area_buffer[this_one]
10723 = (EQ (echo_area_buffer[the_other], echo_buffer[this_one])
10724 ? echo_buffer[the_other]
10725 : echo_buffer[this_one]);
10726 clear_buffer_p = true;
10729 buffer = echo_area_buffer[this_one];
10731 /* Don't get confused by reusing the buffer used for echoing
10732 for a different purpose. */
10733 if (echo_kboard == NULL && EQ (buffer, echo_message_buffer))
10734 cancel_echoing ();
10736 record_unwind_protect (unwind_with_echo_area_buffer,
10737 with_echo_area_buffer_unwind_data (w));
10739 /* Make the echo area buffer current. Note that for display
10740 purposes, it is not necessary that the displayed window's buffer
10741 == current_buffer, except for text property lookup. So, let's
10742 only set that buffer temporarily here without doing a full
10743 Fset_window_buffer. We must also change w->pointm, though,
10744 because otherwise an assertions in unshow_buffer fails, and Emacs
10745 aborts. */
10746 set_buffer_internal_1 (XBUFFER (buffer));
10747 if (w)
10749 wset_buffer (w, buffer);
10750 set_marker_both (w->pointm, buffer, BEG, BEG_BYTE);
10751 set_marker_both (w->old_pointm, buffer, BEG, BEG_BYTE);
10754 bset_undo_list (current_buffer, Qt);
10755 bset_read_only (current_buffer, Qnil);
10756 specbind (Qinhibit_read_only, Qt);
10757 specbind (Qinhibit_modification_hooks, Qt);
10759 if (clear_buffer_p && Z > BEG)
10760 del_range (BEG, Z);
10762 eassert (BEGV >= BEG);
10763 eassert (ZV <= Z && ZV >= BEGV);
10765 rc = fn (a1, a2);
10767 eassert (BEGV >= BEG);
10768 eassert (ZV <= Z && ZV >= BEGV);
10770 unbind_to (count, Qnil);
10771 return rc;
10775 /* Save state that should be preserved around the call to the function
10776 FN called in with_echo_area_buffer. */
10778 static Lisp_Object
10779 with_echo_area_buffer_unwind_data (struct window *w)
10781 int i = 0;
10782 Lisp_Object vector, tmp;
10784 /* Reduce consing by keeping one vector in
10785 Vwith_echo_area_save_vector. */
10786 vector = Vwith_echo_area_save_vector;
10787 Vwith_echo_area_save_vector = Qnil;
10789 if (NILP (vector))
10790 vector = Fmake_vector (make_number (11), Qnil);
10792 XSETBUFFER (tmp, current_buffer); ASET (vector, i, tmp); ++i;
10793 ASET (vector, i, Vdeactivate_mark); ++i;
10794 ASET (vector, i, make_number (windows_or_buffers_changed)); ++i;
10796 if (w)
10798 XSETWINDOW (tmp, w); ASET (vector, i, tmp); ++i;
10799 ASET (vector, i, w->contents); ++i;
10800 ASET (vector, i, make_number (marker_position (w->pointm))); ++i;
10801 ASET (vector, i, make_number (marker_byte_position (w->pointm))); ++i;
10802 ASET (vector, i, make_number (marker_position (w->old_pointm))); ++i;
10803 ASET (vector, i, make_number (marker_byte_position (w->old_pointm))); ++i;
10804 ASET (vector, i, make_number (marker_position (w->start))); ++i;
10805 ASET (vector, i, make_number (marker_byte_position (w->start))); ++i;
10807 else
10809 int end = i + 8;
10810 for (; i < end; ++i)
10811 ASET (vector, i, Qnil);
10814 eassert (i == ASIZE (vector));
10815 return vector;
10819 /* Restore global state from VECTOR which was created by
10820 with_echo_area_buffer_unwind_data. */
10822 static void
10823 unwind_with_echo_area_buffer (Lisp_Object vector)
10825 set_buffer_internal_1 (XBUFFER (AREF (vector, 0)));
10826 Vdeactivate_mark = AREF (vector, 1);
10827 windows_or_buffers_changed = XFASTINT (AREF (vector, 2));
10829 if (WINDOWP (AREF (vector, 3)))
10831 struct window *w;
10832 Lisp_Object buffer;
10834 w = XWINDOW (AREF (vector, 3));
10835 buffer = AREF (vector, 4);
10837 wset_buffer (w, buffer);
10838 set_marker_both (w->pointm, buffer,
10839 XFASTINT (AREF (vector, 5)),
10840 XFASTINT (AREF (vector, 6)));
10841 set_marker_both (w->old_pointm, buffer,
10842 XFASTINT (AREF (vector, 7)),
10843 XFASTINT (AREF (vector, 8)));
10844 set_marker_both (w->start, buffer,
10845 XFASTINT (AREF (vector, 9)),
10846 XFASTINT (AREF (vector, 10)));
10849 Vwith_echo_area_save_vector = vector;
10853 /* Set up the echo area for use by print functions. MULTIBYTE_P
10854 means we will print multibyte. */
10856 void
10857 setup_echo_area_for_printing (bool multibyte_p)
10859 /* If we can't find an echo area any more, exit. */
10860 if (! FRAME_LIVE_P (XFRAME (selected_frame)))
10861 Fkill_emacs (Qnil);
10863 ensure_echo_area_buffers ();
10865 if (!message_buf_print)
10867 /* A message has been output since the last time we printed.
10868 Choose a fresh echo area buffer. */
10869 if (EQ (echo_area_buffer[1], echo_buffer[0]))
10870 echo_area_buffer[0] = echo_buffer[1];
10871 else
10872 echo_area_buffer[0] = echo_buffer[0];
10874 /* Switch to that buffer and clear it. */
10875 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
10876 bset_truncate_lines (current_buffer, Qnil);
10878 if (Z > BEG)
10880 ptrdiff_t count = SPECPDL_INDEX ();
10881 specbind (Qinhibit_read_only, Qt);
10882 /* Note that undo recording is always disabled. */
10883 del_range (BEG, Z);
10884 unbind_to (count, Qnil);
10886 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
10888 /* Set up the buffer for the multibyteness we need. */
10889 if (multibyte_p
10890 != !NILP (BVAR (current_buffer, enable_multibyte_characters)))
10891 Fset_buffer_multibyte (multibyte_p ? Qt : Qnil);
10893 /* Raise the frame containing the echo area. */
10894 if (minibuffer_auto_raise)
10896 struct frame *sf = SELECTED_FRAME ();
10897 Lisp_Object mini_window;
10898 mini_window = FRAME_MINIBUF_WINDOW (sf);
10899 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
10902 message_log_maybe_newline ();
10903 message_buf_print = true;
10905 else
10907 if (NILP (echo_area_buffer[0]))
10909 if (EQ (echo_area_buffer[1], echo_buffer[0]))
10910 echo_area_buffer[0] = echo_buffer[1];
10911 else
10912 echo_area_buffer[0] = echo_buffer[0];
10915 if (current_buffer != XBUFFER (echo_area_buffer[0]))
10917 /* Someone switched buffers between print requests. */
10918 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
10919 bset_truncate_lines (current_buffer, Qnil);
10925 /* Display an echo area message in window W. Value is true if W's
10926 height is changed. If display_last_displayed_message_p,
10927 display the message that was last displayed, otherwise
10928 display the current message. */
10930 static bool
10931 display_echo_area (struct window *w)
10933 bool no_message_p, window_height_changed_p;
10935 /* Temporarily disable garbage collections while displaying the echo
10936 area. This is done because a GC can print a message itself.
10937 That message would modify the echo area buffer's contents while a
10938 redisplay of the buffer is going on, and seriously confuse
10939 redisplay. */
10940 ptrdiff_t count = inhibit_garbage_collection ();
10942 /* If there is no message, we must call display_echo_area_1
10943 nevertheless because it resizes the window. But we will have to
10944 reset the echo_area_buffer in question to nil at the end because
10945 with_echo_area_buffer will sets it to an empty buffer. */
10946 bool i = display_last_displayed_message_p;
10947 /* According to the C99, C11 and C++11 standards, the integral value
10948 of a "bool" is always 0 or 1, so this array access is safe here,
10949 if oddly typed. */
10950 no_message_p = NILP (echo_area_buffer[i]);
10952 window_height_changed_p
10953 = with_echo_area_buffer (w, display_last_displayed_message_p,
10954 display_echo_area_1,
10955 (intptr_t) w, Qnil);
10957 if (no_message_p)
10958 echo_area_buffer[i] = Qnil;
10960 unbind_to (count, Qnil);
10961 return window_height_changed_p;
10965 /* Helper for display_echo_area. Display the current buffer which
10966 contains the current echo area message in window W, a mini-window,
10967 a pointer to which is passed in A1. A2..A4 are currently not used.
10968 Change the height of W so that all of the message is displayed.
10969 Value is true if height of W was changed. */
10971 static bool
10972 display_echo_area_1 (ptrdiff_t a1, Lisp_Object a2)
10974 intptr_t i1 = a1;
10975 struct window *w = (struct window *) i1;
10976 Lisp_Object window;
10977 struct text_pos start;
10979 /* We are about to enter redisplay without going through
10980 redisplay_internal, so we need to forget these faces by hand
10981 here. */
10982 forget_escape_and_glyphless_faces ();
10984 /* Do this before displaying, so that we have a large enough glyph
10985 matrix for the display. If we can't get enough space for the
10986 whole text, display the last N lines. That works by setting w->start. */
10987 bool window_height_changed_p = resize_mini_window (w, false);
10989 /* Use the starting position chosen by resize_mini_window. */
10990 SET_TEXT_POS_FROM_MARKER (start, w->start);
10992 /* Display. */
10993 clear_glyph_matrix (w->desired_matrix);
10994 XSETWINDOW (window, w);
10995 try_window (window, start, 0);
10997 return window_height_changed_p;
11001 /* Resize the echo area window to exactly the size needed for the
11002 currently displayed message, if there is one. If a mini-buffer
11003 is active, don't shrink it. */
11005 void
11006 resize_echo_area_exactly (void)
11008 if (BUFFERP (echo_area_buffer[0])
11009 && WINDOWP (echo_area_window))
11011 struct window *w = XWINDOW (echo_area_window);
11012 Lisp_Object resize_exactly = (minibuf_level == 0 ? Qt : Qnil);
11013 bool resized_p = with_echo_area_buffer (w, 0, resize_mini_window_1,
11014 (intptr_t) w, resize_exactly);
11015 if (resized_p)
11017 windows_or_buffers_changed = 42;
11018 update_mode_lines = 30;
11019 redisplay_internal ();
11025 /* Callback function for with_echo_area_buffer, when used from
11026 resize_echo_area_exactly. A1 contains a pointer to the window to
11027 resize, EXACTLY non-nil means resize the mini-window exactly to the
11028 size of the text displayed. A3 and A4 are not used. Value is what
11029 resize_mini_window returns. */
11031 static bool
11032 resize_mini_window_1 (ptrdiff_t a1, Lisp_Object exactly)
11034 intptr_t i1 = a1;
11035 return resize_mini_window ((struct window *) i1, !NILP (exactly));
11039 /* Resize mini-window W to fit the size of its contents. EXACT_P
11040 means size the window exactly to the size needed. Otherwise, it's
11041 only enlarged until W's buffer is empty.
11043 Set W->start to the right place to begin display. If the whole
11044 contents fit, start at the beginning. Otherwise, start so as
11045 to make the end of the contents appear. This is particularly
11046 important for y-or-n-p, but seems desirable generally.
11048 Value is true if the window height has been changed. */
11050 bool
11051 resize_mini_window (struct window *w, bool exact_p)
11053 struct frame *f = XFRAME (w->frame);
11054 bool window_height_changed_p = false;
11056 eassert (MINI_WINDOW_P (w));
11058 /* By default, start display at the beginning. */
11059 set_marker_both (w->start, w->contents,
11060 BUF_BEGV (XBUFFER (w->contents)),
11061 BUF_BEGV_BYTE (XBUFFER (w->contents)));
11063 /* Don't resize windows while redisplaying a window; it would
11064 confuse redisplay functions when the size of the window they are
11065 displaying changes from under them. Such a resizing can happen,
11066 for instance, when which-func prints a long message while
11067 we are running fontification-functions. We're running these
11068 functions with safe_call which binds inhibit-redisplay to t. */
11069 if (!NILP (Vinhibit_redisplay))
11070 return false;
11072 /* Nil means don't try to resize. */
11073 if (NILP (Vresize_mini_windows)
11074 || (FRAME_X_P (f) && FRAME_X_OUTPUT (f) == NULL))
11075 return false;
11077 if (!FRAME_MINIBUF_ONLY_P (f))
11079 struct it it;
11080 int total_height = (WINDOW_PIXEL_HEIGHT (XWINDOW (FRAME_ROOT_WINDOW (f)))
11081 + WINDOW_PIXEL_HEIGHT (w));
11082 int unit = FRAME_LINE_HEIGHT (f);
11083 int height, max_height;
11084 struct text_pos start;
11085 struct buffer *old_current_buffer = NULL;
11087 if (current_buffer != XBUFFER (w->contents))
11089 old_current_buffer = current_buffer;
11090 set_buffer_internal (XBUFFER (w->contents));
11093 init_iterator (&it, w, BEGV, BEGV_BYTE, NULL, DEFAULT_FACE_ID);
11095 /* Compute the max. number of lines specified by the user. */
11096 if (FLOATP (Vmax_mini_window_height))
11097 max_height = XFLOAT_DATA (Vmax_mini_window_height) * total_height;
11098 else if (INTEGERP (Vmax_mini_window_height))
11099 max_height = XINT (Vmax_mini_window_height) * unit;
11100 else
11101 max_height = total_height / 4;
11103 /* Correct that max. height if it's bogus. */
11104 max_height = clip_to_bounds (unit, max_height, total_height);
11106 /* Find out the height of the text in the window. */
11107 if (it.line_wrap == TRUNCATE)
11108 height = unit;
11109 else
11111 last_height = 0;
11112 move_it_to (&it, ZV, -1, -1, -1, MOVE_TO_POS);
11113 if (it.max_ascent == 0 && it.max_descent == 0)
11114 height = it.current_y + last_height;
11115 else
11116 height = it.current_y + it.max_ascent + it.max_descent;
11117 height -= min (it.extra_line_spacing, it.max_extra_line_spacing);
11120 /* Compute a suitable window start. */
11121 if (height > max_height)
11123 height = (max_height / unit) * unit;
11124 init_iterator (&it, w, ZV, ZV_BYTE, NULL, DEFAULT_FACE_ID);
11125 move_it_vertically_backward (&it, height - unit);
11126 start = it.current.pos;
11128 else
11129 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
11130 SET_MARKER_FROM_TEXT_POS (w->start, start);
11132 if (EQ (Vresize_mini_windows, Qgrow_only))
11134 /* Let it grow only, until we display an empty message, in which
11135 case the window shrinks again. */
11136 if (height > WINDOW_PIXEL_HEIGHT (w))
11138 int old_height = WINDOW_PIXEL_HEIGHT (w);
11140 FRAME_WINDOWS_FROZEN (f) = true;
11141 grow_mini_window (w, height - WINDOW_PIXEL_HEIGHT (w), true);
11142 window_height_changed_p = WINDOW_PIXEL_HEIGHT (w) != old_height;
11144 else if (height < WINDOW_PIXEL_HEIGHT (w)
11145 && (exact_p || BEGV == ZV))
11147 int old_height = WINDOW_PIXEL_HEIGHT (w);
11149 FRAME_WINDOWS_FROZEN (f) = false;
11150 shrink_mini_window (w, true);
11151 window_height_changed_p = WINDOW_PIXEL_HEIGHT (w) != old_height;
11154 else
11156 /* Always resize to exact size needed. */
11157 if (height > WINDOW_PIXEL_HEIGHT (w))
11159 int old_height = WINDOW_PIXEL_HEIGHT (w);
11161 FRAME_WINDOWS_FROZEN (f) = true;
11162 grow_mini_window (w, height - WINDOW_PIXEL_HEIGHT (w), true);
11163 window_height_changed_p = WINDOW_PIXEL_HEIGHT (w) != old_height;
11165 else if (height < WINDOW_PIXEL_HEIGHT (w))
11167 int old_height = WINDOW_PIXEL_HEIGHT (w);
11169 FRAME_WINDOWS_FROZEN (f) = false;
11170 shrink_mini_window (w, true);
11172 if (height)
11174 FRAME_WINDOWS_FROZEN (f) = true;
11175 grow_mini_window (w, height - WINDOW_PIXEL_HEIGHT (w), true);
11178 window_height_changed_p = WINDOW_PIXEL_HEIGHT (w) != old_height;
11182 if (old_current_buffer)
11183 set_buffer_internal (old_current_buffer);
11186 return window_height_changed_p;
11190 /* Value is the current message, a string, or nil if there is no
11191 current message. */
11193 Lisp_Object
11194 current_message (void)
11196 Lisp_Object msg;
11198 if (!BUFFERP (echo_area_buffer[0]))
11199 msg = Qnil;
11200 else
11202 with_echo_area_buffer (0, 0, current_message_1,
11203 (intptr_t) &msg, Qnil);
11204 if (NILP (msg))
11205 echo_area_buffer[0] = Qnil;
11208 return msg;
11212 static bool
11213 current_message_1 (ptrdiff_t a1, Lisp_Object a2)
11215 intptr_t i1 = a1;
11216 Lisp_Object *msg = (Lisp_Object *) i1;
11218 if (Z > BEG)
11219 *msg = make_buffer_string (BEG, Z, true);
11220 else
11221 *msg = Qnil;
11222 return false;
11226 /* Push the current message on Vmessage_stack for later restoration
11227 by restore_message. Value is true if the current message isn't
11228 empty. This is a relatively infrequent operation, so it's not
11229 worth optimizing. */
11231 bool
11232 push_message (void)
11234 Lisp_Object msg = current_message ();
11235 Vmessage_stack = Fcons (msg, Vmessage_stack);
11236 return STRINGP (msg);
11240 /* Restore message display from the top of Vmessage_stack. */
11242 void
11243 restore_message (void)
11245 eassert (CONSP (Vmessage_stack));
11246 message3_nolog (XCAR (Vmessage_stack));
11250 /* Handler for unwind-protect calling pop_message. */
11252 void
11253 pop_message_unwind (void)
11255 /* Pop the top-most entry off Vmessage_stack. */
11256 eassert (CONSP (Vmessage_stack));
11257 Vmessage_stack = XCDR (Vmessage_stack);
11261 /* Check that Vmessage_stack is nil. Called from emacs.c when Emacs
11262 exits. If the stack is not empty, we have a missing pop_message
11263 somewhere. */
11265 void
11266 check_message_stack (void)
11268 if (!NILP (Vmessage_stack))
11269 emacs_abort ();
11273 /* Truncate to NCHARS what will be displayed in the echo area the next
11274 time we display it---but don't redisplay it now. */
11276 void
11277 truncate_echo_area (ptrdiff_t nchars)
11279 if (nchars == 0)
11280 echo_area_buffer[0] = Qnil;
11281 else if (!noninteractive
11282 && INTERACTIVE
11283 && !NILP (echo_area_buffer[0]))
11285 struct frame *sf = SELECTED_FRAME ();
11286 /* Error messages get reported properly by cmd_error, so this must be
11287 just an informative message; if the frame hasn't really been
11288 initialized yet, just toss it. */
11289 if (sf->glyphs_initialized_p)
11290 with_echo_area_buffer (0, 0, truncate_message_1, nchars, Qnil);
11295 /* Helper function for truncate_echo_area. Truncate the current
11296 message to at most NCHARS characters. */
11298 static bool
11299 truncate_message_1 (ptrdiff_t nchars, Lisp_Object a2)
11301 if (BEG + nchars < Z)
11302 del_range (BEG + nchars, Z);
11303 if (Z == BEG)
11304 echo_area_buffer[0] = Qnil;
11305 return false;
11308 /* Set the current message to STRING. */
11310 static void
11311 set_message (Lisp_Object string)
11313 eassert (STRINGP (string));
11315 message_enable_multibyte = STRING_MULTIBYTE (string);
11317 with_echo_area_buffer (0, -1, set_message_1, 0, string);
11318 message_buf_print = false;
11319 help_echo_showing_p = false;
11321 if (STRINGP (Vdebug_on_message)
11322 && STRINGP (string)
11323 && fast_string_match (Vdebug_on_message, string) >= 0)
11324 call_debugger (list2 (Qerror, string));
11328 /* Helper function for set_message. First argument is ignored and second
11329 argument has the same meaning as for set_message.
11330 This function is called with the echo area buffer being current. */
11332 static bool
11333 set_message_1 (ptrdiff_t a1, Lisp_Object string)
11335 eassert (STRINGP (string));
11337 /* Change multibyteness of the echo buffer appropriately. */
11338 if (message_enable_multibyte
11339 != !NILP (BVAR (current_buffer, enable_multibyte_characters)))
11340 Fset_buffer_multibyte (message_enable_multibyte ? Qt : Qnil);
11342 bset_truncate_lines (current_buffer, message_truncate_lines ? Qt : Qnil);
11343 if (!NILP (BVAR (current_buffer, bidi_display_reordering)))
11344 bset_bidi_paragraph_direction (current_buffer, Qleft_to_right);
11346 /* Insert new message at BEG. */
11347 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
11349 /* This function takes care of single/multibyte conversion.
11350 We just have to ensure that the echo area buffer has the right
11351 setting of enable_multibyte_characters. */
11352 insert_from_string (string, 0, 0, SCHARS (string), SBYTES (string), true);
11354 return false;
11358 /* Clear messages. CURRENT_P means clear the current message.
11359 LAST_DISPLAYED_P means clear the message last displayed. */
11361 void
11362 clear_message (bool current_p, bool last_displayed_p)
11364 if (current_p)
11366 echo_area_buffer[0] = Qnil;
11367 message_cleared_p = true;
11370 if (last_displayed_p)
11371 echo_area_buffer[1] = Qnil;
11373 message_buf_print = false;
11376 /* Clear garbaged frames.
11378 This function is used where the old redisplay called
11379 redraw_garbaged_frames which in turn called redraw_frame which in
11380 turn called clear_frame. The call to clear_frame was a source of
11381 flickering. I believe a clear_frame is not necessary. It should
11382 suffice in the new redisplay to invalidate all current matrices,
11383 and ensure a complete redisplay of all windows. */
11385 static void
11386 clear_garbaged_frames (void)
11388 if (frame_garbaged)
11390 Lisp_Object tail, frame;
11391 struct frame *sf = SELECTED_FRAME ();
11393 FOR_EACH_FRAME (tail, frame)
11395 struct frame *f = XFRAME (frame);
11397 if (FRAME_VISIBLE_P (f) && FRAME_GARBAGED_P (f))
11399 if (f->resized_p
11400 /* It makes no sense to redraw a non-selected TTY
11401 frame, since that will actually clear the
11402 selected frame, and might leave the selected
11403 frame with corrupted display, if it happens not
11404 to be marked garbaged. */
11405 && !(f != sf && (FRAME_TERMCAP_P (f) || FRAME_MSDOS_P (f))))
11406 redraw_frame (f);
11407 else
11408 clear_current_matrices (f);
11410 #if defined (HAVE_WINDOW_SYSTEM) && !defined (HAVE_NS)
11411 x_clear_under_internal_border (f);
11412 #endif /* HAVE_WINDOW_SYSTEM && !HAVE_NS */
11414 fset_redisplay (f);
11415 f->garbaged = false;
11416 f->resized_p = false;
11420 frame_garbaged = false;
11425 /* Redisplay the echo area of the selected frame. If UPDATE_FRAME_P, update
11426 selected_frame. */
11428 static void
11429 echo_area_display (bool update_frame_p)
11431 Lisp_Object mini_window;
11432 struct window *w;
11433 struct frame *f;
11434 bool window_height_changed_p = false;
11435 struct frame *sf = SELECTED_FRAME ();
11437 mini_window = FRAME_MINIBUF_WINDOW (sf);
11438 w = XWINDOW (mini_window);
11439 f = XFRAME (WINDOW_FRAME (w));
11441 /* Don't display if frame is invisible or not yet initialized. */
11442 if (!FRAME_VISIBLE_P (f) || !f->glyphs_initialized_p)
11443 return;
11445 #ifdef HAVE_WINDOW_SYSTEM
11446 /* When Emacs starts, selected_frame may be the initial terminal
11447 frame. If we let this through, a message would be displayed on
11448 the terminal. */
11449 if (FRAME_INITIAL_P (XFRAME (selected_frame)))
11450 return;
11451 #endif /* HAVE_WINDOW_SYSTEM */
11453 /* Redraw garbaged frames. */
11454 clear_garbaged_frames ();
11456 if (!NILP (echo_area_buffer[0]) || minibuf_level == 0)
11458 echo_area_window = mini_window;
11459 window_height_changed_p = display_echo_area (w);
11460 w->must_be_updated_p = true;
11462 /* Update the display, unless called from redisplay_internal.
11463 Also don't update the screen during redisplay itself. The
11464 update will happen at the end of redisplay, and an update
11465 here could cause confusion. */
11466 if (update_frame_p && !redisplaying_p)
11468 int n = 0;
11470 /* If the display update has been interrupted by pending
11471 input, update mode lines in the frame. Due to the
11472 pending input, it might have been that redisplay hasn't
11473 been called, so that mode lines above the echo area are
11474 garbaged. This looks odd, so we prevent it here. */
11475 if (!display_completed)
11477 n = redisplay_mode_lines (FRAME_ROOT_WINDOW (f), false);
11479 #if defined (HAVE_WINDOW_SYSTEM) && !defined (HAVE_NS)
11480 x_clear_under_internal_border (f);
11481 #endif /* HAVE_WINDOW_SYSTEM && !HAVE_NS */
11485 if (window_height_changed_p
11486 /* Don't do this if Emacs is shutting down. Redisplay
11487 needs to run hooks. */
11488 && !NILP (Vrun_hooks))
11490 /* Must update other windows. Likewise as in other
11491 cases, don't let this update be interrupted by
11492 pending input. */
11493 ptrdiff_t count = SPECPDL_INDEX ();
11494 specbind (Qredisplay_dont_pause, Qt);
11495 fset_redisplay (f);
11496 redisplay_internal ();
11497 unbind_to (count, Qnil);
11499 else if (FRAME_WINDOW_P (f) && n == 0)
11501 /* Window configuration is the same as before.
11502 Can do with a display update of the echo area,
11503 unless we displayed some mode lines. */
11504 update_single_window (w);
11505 flush_frame (f);
11507 else
11508 update_frame (f, true, true);
11510 /* If cursor is in the echo area, make sure that the next
11511 redisplay displays the minibuffer, so that the cursor will
11512 be replaced with what the minibuffer wants. */
11513 if (cursor_in_echo_area)
11514 wset_redisplay (XWINDOW (mini_window));
11517 else if (!EQ (mini_window, selected_window))
11518 wset_redisplay (XWINDOW (mini_window));
11520 /* Last displayed message is now the current message. */
11521 echo_area_buffer[1] = echo_area_buffer[0];
11522 /* Inform read_char that we're not echoing. */
11523 echo_message_buffer = Qnil;
11525 /* Prevent redisplay optimization in redisplay_internal by resetting
11526 this_line_start_pos. This is done because the mini-buffer now
11527 displays the message instead of its buffer text. */
11528 if (EQ (mini_window, selected_window))
11529 CHARPOS (this_line_start_pos) = 0;
11531 if (window_height_changed_p)
11533 fset_redisplay (f);
11535 /* If window configuration was changed, frames may have been
11536 marked garbaged. Clear them or we will experience
11537 surprises wrt scrolling.
11538 FIXME: How/why/when? */
11539 clear_garbaged_frames ();
11543 /* True if W's buffer was changed but not saved. */
11545 static bool
11546 window_buffer_changed (struct window *w)
11548 struct buffer *b = XBUFFER (w->contents);
11550 eassert (BUFFER_LIVE_P (b));
11552 return (BUF_SAVE_MODIFF (b) < BUF_MODIFF (b)) != w->last_had_star;
11555 /* True if W has %c or %C in its mode line and mode line should be updated. */
11557 static bool
11558 mode_line_update_needed (struct window *w)
11560 return (w->column_number_displayed != -1
11561 && !(PT == w->last_point && !window_outdated (w))
11562 && (w->column_number_displayed != current_column ()));
11565 /* True if window start of W is frozen and may not be changed during
11566 redisplay. */
11568 static bool
11569 window_frozen_p (struct window *w)
11571 if (FRAME_WINDOWS_FROZEN (XFRAME (WINDOW_FRAME (w))))
11573 Lisp_Object window;
11575 XSETWINDOW (window, w);
11576 if (MINI_WINDOW_P (w))
11577 return false;
11578 else if (EQ (window, selected_window))
11579 return false;
11580 else if (MINI_WINDOW_P (XWINDOW (selected_window))
11581 && EQ (window, Vminibuf_scroll_window))
11582 /* This special window can't be frozen too. */
11583 return false;
11584 else
11585 return true;
11587 return false;
11590 /***********************************************************************
11591 Mode Lines and Frame Titles
11592 ***********************************************************************/
11594 /* A buffer for constructing non-propertized mode-line strings and
11595 frame titles in it; allocated from the heap in init_xdisp and
11596 resized as needed in store_mode_line_noprop_char. */
11598 static char *mode_line_noprop_buf;
11600 /* The buffer's end, and a current output position in it. */
11602 static char *mode_line_noprop_buf_end;
11603 static char *mode_line_noprop_ptr;
11605 #define MODE_LINE_NOPROP_LEN(start) \
11606 ((mode_line_noprop_ptr - mode_line_noprop_buf) - start)
11608 static enum {
11609 MODE_LINE_DISPLAY = 0,
11610 MODE_LINE_TITLE,
11611 MODE_LINE_NOPROP,
11612 MODE_LINE_STRING
11613 } mode_line_target;
11615 /* Alist that caches the results of :propertize.
11616 Each element is (PROPERTIZED-STRING . PROPERTY-LIST). */
11617 static Lisp_Object mode_line_proptrans_alist;
11619 /* List of strings making up the mode-line. */
11620 static Lisp_Object mode_line_string_list;
11622 /* Base face property when building propertized mode line string. */
11623 static Lisp_Object mode_line_string_face;
11624 static Lisp_Object mode_line_string_face_prop;
11627 /* Unwind data for mode line strings */
11629 static Lisp_Object Vmode_line_unwind_vector;
11631 static Lisp_Object
11632 format_mode_line_unwind_data (struct frame *target_frame,
11633 struct buffer *obuf,
11634 Lisp_Object owin,
11635 bool save_proptrans)
11637 Lisp_Object vector, tmp;
11639 /* Reduce consing by keeping one vector in
11640 Vwith_echo_area_save_vector. */
11641 vector = Vmode_line_unwind_vector;
11642 Vmode_line_unwind_vector = Qnil;
11644 if (NILP (vector))
11645 vector = Fmake_vector (make_number (10), Qnil);
11647 ASET (vector, 0, make_number (mode_line_target));
11648 ASET (vector, 1, make_number (MODE_LINE_NOPROP_LEN (0)));
11649 ASET (vector, 2, mode_line_string_list);
11650 ASET (vector, 3, save_proptrans ? mode_line_proptrans_alist : Qt);
11651 ASET (vector, 4, mode_line_string_face);
11652 ASET (vector, 5, mode_line_string_face_prop);
11654 if (obuf)
11655 XSETBUFFER (tmp, obuf);
11656 else
11657 tmp = Qnil;
11658 ASET (vector, 6, tmp);
11659 ASET (vector, 7, owin);
11660 if (target_frame)
11662 /* Similarly to `with-selected-window', if the operation selects
11663 a window on another frame, we must restore that frame's
11664 selected window, and (for a tty) the top-frame. */
11665 ASET (vector, 8, target_frame->selected_window);
11666 if (FRAME_TERMCAP_P (target_frame))
11667 ASET (vector, 9, FRAME_TTY (target_frame)->top_frame);
11670 return vector;
11673 static void
11674 unwind_format_mode_line (Lisp_Object vector)
11676 Lisp_Object old_window = AREF (vector, 7);
11677 Lisp_Object target_frame_window = AREF (vector, 8);
11678 Lisp_Object old_top_frame = AREF (vector, 9);
11680 mode_line_target = XINT (AREF (vector, 0));
11681 mode_line_noprop_ptr = mode_line_noprop_buf + XINT (AREF (vector, 1));
11682 mode_line_string_list = AREF (vector, 2);
11683 if (! EQ (AREF (vector, 3), Qt))
11684 mode_line_proptrans_alist = AREF (vector, 3);
11685 mode_line_string_face = AREF (vector, 4);
11686 mode_line_string_face_prop = AREF (vector, 5);
11688 /* Select window before buffer, since it may change the buffer. */
11689 if (!NILP (old_window))
11691 /* If the operation that we are unwinding had selected a window
11692 on a different frame, reset its frame-selected-window. For a
11693 text terminal, reset its top-frame if necessary. */
11694 if (!NILP (target_frame_window))
11696 Lisp_Object frame
11697 = WINDOW_FRAME (XWINDOW (target_frame_window));
11699 if (!EQ (frame, WINDOW_FRAME (XWINDOW (old_window))))
11700 Fselect_window (target_frame_window, Qt);
11702 if (!NILP (old_top_frame) && !EQ (old_top_frame, frame))
11703 Fselect_frame (old_top_frame, Qt);
11706 Fselect_window (old_window, Qt);
11709 if (!NILP (AREF (vector, 6)))
11711 set_buffer_internal_1 (XBUFFER (AREF (vector, 6)));
11712 ASET (vector, 6, Qnil);
11715 Vmode_line_unwind_vector = vector;
11719 /* Store a single character C for the frame title in mode_line_noprop_buf.
11720 Re-allocate mode_line_noprop_buf if necessary. */
11722 static void
11723 store_mode_line_noprop_char (char c)
11725 /* If output position has reached the end of the allocated buffer,
11726 increase the buffer's size. */
11727 if (mode_line_noprop_ptr == mode_line_noprop_buf_end)
11729 ptrdiff_t len = MODE_LINE_NOPROP_LEN (0);
11730 ptrdiff_t size = len;
11731 mode_line_noprop_buf =
11732 xpalloc (mode_line_noprop_buf, &size, 1, STRING_BYTES_BOUND, 1);
11733 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
11734 mode_line_noprop_ptr = mode_line_noprop_buf + len;
11737 *mode_line_noprop_ptr++ = c;
11741 /* Store part of a frame title in mode_line_noprop_buf, beginning at
11742 mode_line_noprop_ptr. STRING is the string to store. Do not copy
11743 characters that yield more columns than PRECISION; PRECISION <= 0
11744 means copy the whole string. Pad with spaces until FIELD_WIDTH
11745 number of characters have been copied; FIELD_WIDTH <= 0 means don't
11746 pad. Called from display_mode_element when it is used to build a
11747 frame title. */
11749 static int
11750 store_mode_line_noprop (const char *string, int field_width, int precision)
11752 const unsigned char *str = (const unsigned char *) string;
11753 int n = 0;
11754 ptrdiff_t dummy, nbytes;
11756 /* Copy at most PRECISION chars from STR. */
11757 nbytes = strlen (string);
11758 n += c_string_width (str, nbytes, precision, &dummy, &nbytes);
11759 while (nbytes--)
11760 store_mode_line_noprop_char (*str++);
11762 /* Fill up with spaces until FIELD_WIDTH reached. */
11763 while (field_width > 0
11764 && n < field_width)
11766 store_mode_line_noprop_char (' ');
11767 ++n;
11770 return n;
11773 /***********************************************************************
11774 Frame Titles
11775 ***********************************************************************/
11777 #ifdef HAVE_WINDOW_SYSTEM
11779 /* Set the title of FRAME, if it has changed. The title format is
11780 Vicon_title_format if FRAME is iconified, otherwise it is
11781 frame_title_format. */
11783 static void
11784 x_consider_frame_title (Lisp_Object frame)
11786 struct frame *f = XFRAME (frame);
11788 if ((FRAME_WINDOW_P (f)
11789 || FRAME_MINIBUF_ONLY_P (f)
11790 || f->explicit_name)
11791 && NILP (Fframe_parameter (frame, Qtooltip)))
11793 /* Do we have more than one visible frame on this X display? */
11794 Lisp_Object tail, other_frame, fmt;
11795 ptrdiff_t title_start;
11796 char *title;
11797 ptrdiff_t len;
11798 struct it it;
11799 ptrdiff_t count = SPECPDL_INDEX ();
11801 FOR_EACH_FRAME (tail, other_frame)
11803 struct frame *tf = XFRAME (other_frame);
11805 if (tf != f
11806 && FRAME_KBOARD (tf) == FRAME_KBOARD (f)
11807 && !FRAME_MINIBUF_ONLY_P (tf)
11808 && !EQ (other_frame, tip_frame)
11809 && !FRAME_PARENT_FRAME (tf)
11810 && (FRAME_VISIBLE_P (tf) || FRAME_ICONIFIED_P (tf)))
11811 break;
11814 /* Set global variable indicating that multiple frames exist. */
11815 multiple_frames = CONSP (tail);
11817 /* Switch to the buffer of selected window of the frame. Set up
11818 mode_line_target so that display_mode_element will output into
11819 mode_line_noprop_buf; then display the title. */
11820 record_unwind_protect (unwind_format_mode_line,
11821 format_mode_line_unwind_data
11822 (f, current_buffer, selected_window, false));
11823 /* select-frame calls resize_mini_window, which could resize the
11824 mini-window and by that undo the effect of this redisplay
11825 cycle wrt minibuffer and echo-area display. Binding
11826 inhibit-redisplay to t makes the call to resize_mini_window a
11827 no-op, thus avoiding the adverse side effects. */
11828 specbind (Qinhibit_redisplay, Qt);
11830 Fselect_window (f->selected_window, Qt);
11831 set_buffer_internal_1
11832 (XBUFFER (XWINDOW (f->selected_window)->contents));
11833 fmt = FRAME_ICONIFIED_P (f) ? Vicon_title_format : Vframe_title_format;
11835 mode_line_target = MODE_LINE_TITLE;
11836 title_start = MODE_LINE_NOPROP_LEN (0);
11837 init_iterator (&it, XWINDOW (f->selected_window), -1, -1,
11838 NULL, DEFAULT_FACE_ID);
11839 display_mode_element (&it, 0, -1, -1, fmt, Qnil, false);
11840 len = MODE_LINE_NOPROP_LEN (title_start);
11841 title = mode_line_noprop_buf + title_start;
11842 unbind_to (count, Qnil);
11844 /* Set the title only if it's changed. This avoids consing in
11845 the common case where it hasn't. (If it turns out that we've
11846 already wasted too much time by walking through the list with
11847 display_mode_element, then we might need to optimize at a
11848 higher level than this.) */
11849 if (! STRINGP (f->name)
11850 || SBYTES (f->name) != len
11851 || memcmp (title, SDATA (f->name), len) != 0)
11852 x_implicitly_set_name (f, make_string (title, len), Qnil);
11856 #endif /* not HAVE_WINDOW_SYSTEM */
11859 /***********************************************************************
11860 Menu Bars
11861 ***********************************************************************/
11863 /* True if we will not redisplay all visible windows. */
11864 #define REDISPLAY_SOME_P() \
11865 ((windows_or_buffers_changed == 0 \
11866 || windows_or_buffers_changed == REDISPLAY_SOME) \
11867 && (update_mode_lines == 0 \
11868 || update_mode_lines == REDISPLAY_SOME))
11870 /* Prepare for redisplay by updating menu-bar item lists when
11871 appropriate. This can call eval. */
11873 static void
11874 prepare_menu_bars (void)
11876 bool all_windows = windows_or_buffers_changed || update_mode_lines;
11877 bool some_windows = REDISPLAY_SOME_P ();
11878 Lisp_Object tooltip_frame;
11880 #ifdef HAVE_WINDOW_SYSTEM
11881 tooltip_frame = tip_frame;
11882 #else
11883 tooltip_frame = Qnil;
11884 #endif
11886 if (FUNCTIONP (Vpre_redisplay_function))
11888 Lisp_Object windows = all_windows ? Qt : Qnil;
11889 if (all_windows && some_windows)
11891 Lisp_Object ws = window_list ();
11892 for (windows = Qnil; CONSP (ws); ws = XCDR (ws))
11894 Lisp_Object this = XCAR (ws);
11895 struct window *w = XWINDOW (this);
11896 if (w->redisplay
11897 || XFRAME (w->frame)->redisplay
11898 || XBUFFER (w->contents)->text->redisplay)
11900 windows = Fcons (this, windows);
11904 safe__call1 (true, Vpre_redisplay_function, windows);
11907 /* Update all frame titles based on their buffer names, etc. We do
11908 this before the menu bars so that the buffer-menu will show the
11909 up-to-date frame titles. */
11910 #ifdef HAVE_WINDOW_SYSTEM
11911 if (all_windows)
11913 Lisp_Object tail, frame;
11915 FOR_EACH_FRAME (tail, frame)
11917 struct frame *f = XFRAME (frame);
11918 struct window *w = XWINDOW (FRAME_SELECTED_WINDOW (f));
11919 if (some_windows
11920 && !f->redisplay
11921 && !w->redisplay
11922 && !XBUFFER (w->contents)->text->redisplay)
11923 continue;
11925 if (!EQ (frame, tooltip_frame)
11926 && !FRAME_PARENT_FRAME (f)
11927 && (FRAME_ICONIFIED_P (f)
11928 || FRAME_VISIBLE_P (f) == 1
11929 /* Exclude TTY frames that are obscured because they
11930 are not the top frame on their console. This is
11931 because x_consider_frame_title actually switches
11932 to the frame, which for TTY frames means it is
11933 marked as garbaged, and will be completely
11934 redrawn on the next redisplay cycle. This causes
11935 TTY frames to be completely redrawn, when there
11936 are more than one of them, even though nothing
11937 should be changed on display. */
11938 || (FRAME_VISIBLE_P (f) == 2 && FRAME_WINDOW_P (f))))
11939 x_consider_frame_title (frame);
11942 #endif /* HAVE_WINDOW_SYSTEM */
11944 /* Update the menu bar item lists, if appropriate. This has to be
11945 done before any actual redisplay or generation of display lines. */
11947 if (all_windows)
11949 Lisp_Object tail, frame;
11950 ptrdiff_t count = SPECPDL_INDEX ();
11951 /* True means that update_menu_bar has run its hooks
11952 so any further calls to update_menu_bar shouldn't do so again. */
11953 bool menu_bar_hooks_run = false;
11955 record_unwind_save_match_data ();
11957 FOR_EACH_FRAME (tail, frame)
11959 struct frame *f = XFRAME (frame);
11960 struct window *w = XWINDOW (FRAME_SELECTED_WINDOW (f));
11962 /* Ignore tooltip frame. */
11963 if (EQ (frame, tooltip_frame))
11964 continue;
11966 if (some_windows
11967 && !f->redisplay
11968 && !w->redisplay
11969 && !XBUFFER (w->contents)->text->redisplay)
11970 continue;
11972 run_window_size_change_functions (frame);
11974 if (FRAME_PARENT_FRAME (f))
11975 continue;
11977 menu_bar_hooks_run = update_menu_bar (f, false, menu_bar_hooks_run);
11978 #ifdef HAVE_WINDOW_SYSTEM
11979 update_tool_bar (f, false);
11980 #endif
11983 unbind_to (count, Qnil);
11985 else
11987 struct frame *sf = SELECTED_FRAME ();
11988 update_menu_bar (sf, true, false);
11989 #ifdef HAVE_WINDOW_SYSTEM
11990 update_tool_bar (sf, true);
11991 #endif
11996 /* Update the menu bar item list for frame F. This has to be done
11997 before we start to fill in any display lines, because it can call
11998 eval.
12000 If SAVE_MATCH_DATA, we must save and restore it here.
12002 If HOOKS_RUN, a previous call to update_menu_bar
12003 already ran the menu bar hooks for this redisplay, so there
12004 is no need to run them again. The return value is the
12005 updated value of this flag, to pass to the next call. */
12007 static bool
12008 update_menu_bar (struct frame *f, bool save_match_data, bool hooks_run)
12010 Lisp_Object window;
12011 struct window *w;
12013 /* If called recursively during a menu update, do nothing. This can
12014 happen when, for instance, an activate-menubar-hook causes a
12015 redisplay. */
12016 if (inhibit_menubar_update)
12017 return hooks_run;
12019 window = FRAME_SELECTED_WINDOW (f);
12020 w = XWINDOW (window);
12022 if (FRAME_WINDOW_P (f)
12024 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
12025 || defined (HAVE_NS) || defined (USE_GTK)
12026 FRAME_EXTERNAL_MENU_BAR (f)
12027 #else
12028 FRAME_MENU_BAR_LINES (f) > 0
12029 #endif
12030 : FRAME_MENU_BAR_LINES (f) > 0)
12032 /* If the user has switched buffers or windows, we need to
12033 recompute to reflect the new bindings. But we'll
12034 recompute when update_mode_lines is set too; that means
12035 that people can use force-mode-line-update to request
12036 that the menu bar be recomputed. The adverse effect on
12037 the rest of the redisplay algorithm is about the same as
12038 windows_or_buffers_changed anyway. */
12039 if (windows_or_buffers_changed
12040 /* This used to test w->update_mode_line, but we believe
12041 there is no need to recompute the menu in that case. */
12042 || update_mode_lines
12043 || window_buffer_changed (w))
12045 struct buffer *prev = current_buffer;
12046 ptrdiff_t count = SPECPDL_INDEX ();
12048 specbind (Qinhibit_menubar_update, Qt);
12050 set_buffer_internal_1 (XBUFFER (w->contents));
12051 if (save_match_data)
12052 record_unwind_save_match_data ();
12053 if (NILP (Voverriding_local_map_menu_flag))
12055 specbind (Qoverriding_terminal_local_map, Qnil);
12056 specbind (Qoverriding_local_map, Qnil);
12059 if (!hooks_run)
12061 /* Run the Lucid hook. */
12062 safe_run_hooks (Qactivate_menubar_hook);
12064 /* If it has changed current-menubar from previous value,
12065 really recompute the menu-bar from the value. */
12066 if (! NILP (Vlucid_menu_bar_dirty_flag))
12067 call0 (Qrecompute_lucid_menubar);
12069 safe_run_hooks (Qmenu_bar_update_hook);
12071 hooks_run = true;
12074 XSETFRAME (Vmenu_updating_frame, f);
12075 fset_menu_bar_items (f, menu_bar_items (FRAME_MENU_BAR_ITEMS (f)));
12077 /* Redisplay the menu bar in case we changed it. */
12078 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
12079 || defined (HAVE_NS) || defined (USE_GTK)
12080 if (FRAME_WINDOW_P (f))
12082 #if defined (HAVE_NS)
12083 /* All frames on Mac OS share the same menubar. So only
12084 the selected frame should be allowed to set it. */
12085 if (f == SELECTED_FRAME ())
12086 #endif
12087 set_frame_menubar (f, false, false);
12089 else
12090 /* On a terminal screen, the menu bar is an ordinary screen
12091 line, and this makes it get updated. */
12092 w->update_mode_line = true;
12093 #else /* ! (USE_X_TOOLKIT || HAVE_NTGUI || HAVE_NS || USE_GTK) */
12094 /* In the non-toolkit version, the menu bar is an ordinary screen
12095 line, and this makes it get updated. */
12096 w->update_mode_line = true;
12097 #endif /* ! (USE_X_TOOLKIT || HAVE_NTGUI || HAVE_NS || USE_GTK) */
12099 unbind_to (count, Qnil);
12100 set_buffer_internal_1 (prev);
12104 return hooks_run;
12107 /***********************************************************************
12108 Tool-bars
12109 ***********************************************************************/
12111 #ifdef HAVE_WINDOW_SYSTEM
12113 /* Select `frame' temporarily without running all the code in
12114 do_switch_frame.
12115 FIXME: Maybe do_switch_frame should be trimmed down similarly
12116 when `norecord' is set. */
12117 static void
12118 fast_set_selected_frame (Lisp_Object frame)
12120 if (!EQ (selected_frame, frame))
12122 selected_frame = frame;
12123 selected_window = XFRAME (frame)->selected_window;
12127 /* Update the tool-bar item list for frame F. This has to be done
12128 before we start to fill in any display lines. Called from
12129 prepare_menu_bars. If SAVE_MATCH_DATA, we must save
12130 and restore it here. */
12132 static void
12133 update_tool_bar (struct frame *f, bool save_match_data)
12135 #if defined (USE_GTK) || defined (HAVE_NS)
12136 bool do_update = FRAME_EXTERNAL_TOOL_BAR (f);
12137 #else
12138 bool do_update = (WINDOWP (f->tool_bar_window)
12139 && WINDOW_TOTAL_LINES (XWINDOW (f->tool_bar_window)) > 0);
12140 #endif
12142 if (do_update)
12144 Lisp_Object window;
12145 struct window *w;
12147 window = FRAME_SELECTED_WINDOW (f);
12148 w = XWINDOW (window);
12150 /* If the user has switched buffers or windows, we need to
12151 recompute to reflect the new bindings. But we'll
12152 recompute when update_mode_lines is set too; that means
12153 that people can use force-mode-line-update to request
12154 that the menu bar be recomputed. The adverse effect on
12155 the rest of the redisplay algorithm is about the same as
12156 windows_or_buffers_changed anyway. */
12157 if (windows_or_buffers_changed
12158 || w->update_mode_line
12159 || update_mode_lines
12160 || window_buffer_changed (w))
12162 struct buffer *prev = current_buffer;
12163 ptrdiff_t count = SPECPDL_INDEX ();
12164 Lisp_Object frame, new_tool_bar;
12165 int new_n_tool_bar;
12167 /* Set current_buffer to the buffer of the selected
12168 window of the frame, so that we get the right local
12169 keymaps. */
12170 set_buffer_internal_1 (XBUFFER (w->contents));
12172 /* Save match data, if we must. */
12173 if (save_match_data)
12174 record_unwind_save_match_data ();
12176 /* Make sure that we don't accidentally use bogus keymaps. */
12177 if (NILP (Voverriding_local_map_menu_flag))
12179 specbind (Qoverriding_terminal_local_map, Qnil);
12180 specbind (Qoverriding_local_map, Qnil);
12183 /* We must temporarily set the selected frame to this frame
12184 before calling tool_bar_items, because the calculation of
12185 the tool-bar keymap uses the selected frame (see
12186 `tool-bar-make-keymap' in tool-bar.el). */
12187 eassert (EQ (selected_window,
12188 /* Since we only explicitly preserve selected_frame,
12189 check that selected_window would be redundant. */
12190 XFRAME (selected_frame)->selected_window));
12191 record_unwind_protect (fast_set_selected_frame, selected_frame);
12192 XSETFRAME (frame, f);
12193 fast_set_selected_frame (frame);
12195 /* Build desired tool-bar items from keymaps. */
12196 new_tool_bar
12197 = tool_bar_items (Fcopy_sequence (f->tool_bar_items),
12198 &new_n_tool_bar);
12200 /* Redisplay the tool-bar if we changed it. */
12201 if (new_n_tool_bar != f->n_tool_bar_items
12202 || NILP (Fequal (new_tool_bar, f->tool_bar_items)))
12204 /* Redisplay that happens asynchronously due to an expose event
12205 may access f->tool_bar_items. Make sure we update both
12206 variables within BLOCK_INPUT so no such event interrupts. */
12207 block_input ();
12208 fset_tool_bar_items (f, new_tool_bar);
12209 f->n_tool_bar_items = new_n_tool_bar;
12210 w->update_mode_line = true;
12211 unblock_input ();
12214 unbind_to (count, Qnil);
12215 set_buffer_internal_1 (prev);
12220 #if ! defined (USE_GTK) && ! defined (HAVE_NS)
12222 /* Set F->desired_tool_bar_string to a Lisp string representing frame
12223 F's desired tool-bar contents. F->tool_bar_items must have
12224 been set up previously by calling prepare_menu_bars. */
12226 static void
12227 build_desired_tool_bar_string (struct frame *f)
12229 int i, size, size_needed;
12230 Lisp_Object image, plist;
12232 image = plist = Qnil;
12234 /* Prepare F->desired_tool_bar_string. If we can reuse it, do so.
12235 Otherwise, make a new string. */
12237 /* The size of the string we might be able to reuse. */
12238 size = (STRINGP (f->desired_tool_bar_string)
12239 ? SCHARS (f->desired_tool_bar_string)
12240 : 0);
12242 /* We need one space in the string for each image. */
12243 size_needed = f->n_tool_bar_items;
12245 /* Reuse f->desired_tool_bar_string, if possible. */
12246 if (size < size_needed || NILP (f->desired_tool_bar_string))
12247 fset_desired_tool_bar_string
12248 (f, Fmake_string (make_number (size_needed), make_number (' ')));
12249 else
12251 AUTO_LIST4 (props, Qdisplay, Qnil, Qmenu_item, Qnil);
12252 Fremove_text_properties (make_number (0), make_number (size),
12253 props, f->desired_tool_bar_string);
12256 /* Put a `display' property on the string for the images to display,
12257 put a `menu_item' property on tool-bar items with a value that
12258 is the index of the item in F's tool-bar item vector. */
12259 for (i = 0; i < f->n_tool_bar_items; ++i)
12261 #define PROP(IDX) \
12262 AREF (f->tool_bar_items, i * TOOL_BAR_ITEM_NSLOTS + (IDX))
12264 bool enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
12265 bool selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
12266 int hmargin, vmargin, relief, idx, end;
12268 /* If image is a vector, choose the image according to the
12269 button state. */
12270 image = PROP (TOOL_BAR_ITEM_IMAGES);
12271 if (VECTORP (image))
12273 if (enabled_p)
12274 idx = (selected_p
12275 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
12276 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
12277 else
12278 idx = (selected_p
12279 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
12280 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
12282 eassert (ASIZE (image) >= idx);
12283 image = AREF (image, idx);
12285 else
12286 idx = -1;
12288 /* Ignore invalid image specifications. */
12289 if (!valid_image_p (image))
12290 continue;
12292 /* Display the tool-bar button pressed, or depressed. */
12293 plist = Fcopy_sequence (XCDR (image));
12295 /* Compute margin and relief to draw. */
12296 relief = (tool_bar_button_relief >= 0
12297 ? tool_bar_button_relief
12298 : DEFAULT_TOOL_BAR_BUTTON_RELIEF);
12299 hmargin = vmargin = relief;
12301 if (RANGED_INTEGERP (1, Vtool_bar_button_margin,
12302 INT_MAX - max (hmargin, vmargin)))
12304 hmargin += XFASTINT (Vtool_bar_button_margin);
12305 vmargin += XFASTINT (Vtool_bar_button_margin);
12307 else if (CONSP (Vtool_bar_button_margin))
12309 if (RANGED_INTEGERP (1, XCAR (Vtool_bar_button_margin),
12310 INT_MAX - hmargin))
12311 hmargin += XFASTINT (XCAR (Vtool_bar_button_margin));
12313 if (RANGED_INTEGERP (1, XCDR (Vtool_bar_button_margin),
12314 INT_MAX - vmargin))
12315 vmargin += XFASTINT (XCDR (Vtool_bar_button_margin));
12318 if (auto_raise_tool_bar_buttons_p)
12320 /* Add a `:relief' property to the image spec if the item is
12321 selected. */
12322 if (selected_p)
12324 plist = Fplist_put (plist, QCrelief, make_number (-relief));
12325 hmargin -= relief;
12326 vmargin -= relief;
12329 else
12331 /* If image is selected, display it pressed, i.e. with a
12332 negative relief. If it's not selected, display it with a
12333 raised relief. */
12334 plist = Fplist_put (plist, QCrelief,
12335 (selected_p
12336 ? make_number (-relief)
12337 : make_number (relief)));
12338 hmargin -= relief;
12339 vmargin -= relief;
12342 /* Put a margin around the image. */
12343 if (hmargin || vmargin)
12345 if (hmargin == vmargin)
12346 plist = Fplist_put (plist, QCmargin, make_number (hmargin));
12347 else
12348 plist = Fplist_put (plist, QCmargin,
12349 Fcons (make_number (hmargin),
12350 make_number (vmargin)));
12353 /* If button is not enabled, and we don't have special images
12354 for the disabled state, make the image appear disabled by
12355 applying an appropriate algorithm to it. */
12356 if (!enabled_p && idx < 0)
12357 plist = Fplist_put (plist, QCconversion, Qdisabled);
12359 /* Put a `display' text property on the string for the image to
12360 display. Put a `menu-item' property on the string that gives
12361 the start of this item's properties in the tool-bar items
12362 vector. */
12363 image = Fcons (Qimage, plist);
12364 AUTO_LIST4 (props, Qdisplay, image, Qmenu_item,
12365 make_number (i * TOOL_BAR_ITEM_NSLOTS));
12367 /* Let the last image hide all remaining spaces in the tool bar
12368 string. The string can be longer than needed when we reuse a
12369 previous string. */
12370 if (i + 1 == f->n_tool_bar_items)
12371 end = SCHARS (f->desired_tool_bar_string);
12372 else
12373 end = i + 1;
12374 Fadd_text_properties (make_number (i), make_number (end),
12375 props, f->desired_tool_bar_string);
12376 #undef PROP
12381 /* Display one line of the tool-bar of frame IT->f.
12383 HEIGHT specifies the desired height of the tool-bar line.
12384 If the actual height of the glyph row is less than HEIGHT, the
12385 row's height is increased to HEIGHT, and the icons are centered
12386 vertically in the new height.
12388 If HEIGHT is -1, we are counting needed tool-bar lines, so don't
12389 count a final empty row in case the tool-bar width exactly matches
12390 the window width.
12393 static void
12394 display_tool_bar_line (struct it *it, int height)
12396 struct glyph_row *row = it->glyph_row;
12397 int max_x = it->last_visible_x;
12398 struct glyph *last;
12400 /* Don't extend on a previously drawn tool bar items (Bug#16058). */
12401 clear_glyph_row (row);
12402 row->enabled_p = true;
12403 row->y = it->current_y;
12405 /* Note that this isn't made use of if the face hasn't a box,
12406 so there's no need to check the face here. */
12407 it->start_of_box_run_p = true;
12409 while (it->current_x < max_x)
12411 int x, n_glyphs_before, i, nglyphs;
12412 struct it it_before;
12414 /* Get the next display element. */
12415 if (!get_next_display_element (it))
12417 /* Don't count empty row if we are counting needed tool-bar lines. */
12418 if (height < 0 && !it->hpos)
12419 return;
12420 break;
12423 /* Produce glyphs. */
12424 n_glyphs_before = row->used[TEXT_AREA];
12425 it_before = *it;
12427 PRODUCE_GLYPHS (it);
12429 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
12430 i = 0;
12431 x = it_before.current_x;
12432 while (i < nglyphs)
12434 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
12436 if (x + glyph->pixel_width > max_x)
12438 /* Glyph doesn't fit on line. Backtrack. */
12439 row->used[TEXT_AREA] = n_glyphs_before;
12440 *it = it_before;
12441 /* If this is the only glyph on this line, it will never fit on the
12442 tool-bar, so skip it. But ensure there is at least one glyph,
12443 so we don't accidentally disable the tool-bar. */
12444 if (n_glyphs_before == 0
12445 && (it->vpos > 0 || IT_STRING_CHARPOS (*it) < it->end_charpos-1))
12446 break;
12447 goto out;
12450 ++it->hpos;
12451 x += glyph->pixel_width;
12452 ++i;
12455 /* Stop at line end. */
12456 if (ITERATOR_AT_END_OF_LINE_P (it))
12457 break;
12459 set_iterator_to_next (it, true);
12462 out:;
12464 row->displays_text_p = row->used[TEXT_AREA] != 0;
12466 /* Use default face for the border below the tool bar.
12468 FIXME: When auto-resize-tool-bars is grow-only, there is
12469 no additional border below the possibly empty tool-bar lines.
12470 So to make the extra empty lines look "normal", we have to
12471 use the tool-bar face for the border too. */
12472 if (!MATRIX_ROW_DISPLAYS_TEXT_P (row)
12473 && !EQ (Vauto_resize_tool_bars, Qgrow_only))
12474 it->face_id = DEFAULT_FACE_ID;
12476 extend_face_to_end_of_line (it);
12477 last = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
12478 last->right_box_line_p = true;
12479 if (last == row->glyphs[TEXT_AREA])
12480 last->left_box_line_p = true;
12482 /* Make line the desired height and center it vertically. */
12483 if ((height -= it->max_ascent + it->max_descent) > 0)
12485 /* Don't add more than one line height. */
12486 height %= FRAME_LINE_HEIGHT (it->f);
12487 it->max_ascent += height / 2;
12488 it->max_descent += (height + 1) / 2;
12491 compute_line_metrics (it);
12493 /* If line is empty, make it occupy the rest of the tool-bar. */
12494 if (!MATRIX_ROW_DISPLAYS_TEXT_P (row))
12496 row->height = row->phys_height = it->last_visible_y - row->y;
12497 row->visible_height = row->height;
12498 row->ascent = row->phys_ascent = 0;
12499 row->extra_line_spacing = 0;
12502 row->full_width_p = true;
12503 row->continued_p = false;
12504 row->truncated_on_left_p = false;
12505 row->truncated_on_right_p = false;
12507 it->current_x = it->hpos = 0;
12508 it->current_y += row->height;
12509 ++it->vpos;
12510 ++it->glyph_row;
12514 /* Value is the number of pixels needed to make all tool-bar items of
12515 frame F visible. The actual number of glyph rows needed is
12516 returned in *N_ROWS if non-NULL. */
12517 static int
12518 tool_bar_height (struct frame *f, int *n_rows, bool pixelwise)
12520 struct window *w = XWINDOW (f->tool_bar_window);
12521 struct it it;
12522 /* tool_bar_height is called from redisplay_tool_bar after building
12523 the desired matrix, so use (unused) mode-line row as temporary row to
12524 avoid destroying the first tool-bar row. */
12525 struct glyph_row *temp_row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
12527 /* Initialize an iterator for iteration over
12528 F->desired_tool_bar_string in the tool-bar window of frame F. */
12529 init_iterator (&it, w, -1, -1, temp_row, TOOL_BAR_FACE_ID);
12530 temp_row->reversed_p = false;
12531 it.first_visible_x = 0;
12532 it.last_visible_x = WINDOW_PIXEL_WIDTH (w);
12533 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
12534 it.paragraph_embedding = L2R;
12536 while (!ITERATOR_AT_END_P (&it))
12538 clear_glyph_row (temp_row);
12539 it.glyph_row = temp_row;
12540 display_tool_bar_line (&it, -1);
12542 clear_glyph_row (temp_row);
12544 /* f->n_tool_bar_rows == 0 means "unknown"; -1 means no tool-bar. */
12545 if (n_rows)
12546 *n_rows = it.vpos > 0 ? it.vpos : -1;
12548 if (pixelwise)
12549 return it.current_y;
12550 else
12551 return (it.current_y + FRAME_LINE_HEIGHT (f) - 1) / FRAME_LINE_HEIGHT (f);
12554 #endif /* !USE_GTK && !HAVE_NS */
12556 DEFUN ("tool-bar-height", Ftool_bar_height, Stool_bar_height,
12557 0, 2, 0,
12558 doc: /* Return the number of lines occupied by the tool bar of FRAME.
12559 If FRAME is nil or omitted, use the selected frame. Optional argument
12560 PIXELWISE non-nil means return the height of the tool bar in pixels. */)
12561 (Lisp_Object frame, Lisp_Object pixelwise)
12563 int height = 0;
12565 #if ! defined (USE_GTK) && ! defined (HAVE_NS)
12566 struct frame *f = decode_any_frame (frame);
12568 if (WINDOWP (f->tool_bar_window)
12569 && WINDOW_PIXEL_HEIGHT (XWINDOW (f->tool_bar_window)) > 0)
12571 update_tool_bar (f, true);
12572 if (f->n_tool_bar_items)
12574 build_desired_tool_bar_string (f);
12575 height = tool_bar_height (f, NULL, !NILP (pixelwise));
12578 #endif
12580 return make_number (height);
12584 /* Display the tool-bar of frame F. Value is true if tool-bar's
12585 height should be changed. */
12586 static bool
12587 redisplay_tool_bar (struct frame *f)
12589 f->tool_bar_redisplayed = true;
12590 #if defined (USE_GTK) || defined (HAVE_NS)
12592 if (FRAME_EXTERNAL_TOOL_BAR (f))
12593 update_frame_tool_bar (f);
12594 return false;
12596 #else /* !USE_GTK && !HAVE_NS */
12598 struct window *w;
12599 struct it it;
12600 struct glyph_row *row;
12602 /* If frame hasn't a tool-bar window or if it is zero-height, don't
12603 do anything. This means you must start with tool-bar-lines
12604 non-zero to get the auto-sizing effect. Or in other words, you
12605 can turn off tool-bars by specifying tool-bar-lines zero. */
12606 if (!WINDOWP (f->tool_bar_window)
12607 || (w = XWINDOW (f->tool_bar_window),
12608 WINDOW_TOTAL_LINES (w) == 0))
12609 return false;
12611 /* Set up an iterator for the tool-bar window. */
12612 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
12613 it.first_visible_x = 0;
12614 it.last_visible_x = WINDOW_PIXEL_WIDTH (w);
12615 row = it.glyph_row;
12616 row->reversed_p = false;
12618 /* Build a string that represents the contents of the tool-bar. */
12619 build_desired_tool_bar_string (f);
12620 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
12621 /* FIXME: This should be controlled by a user option. But it
12622 doesn't make sense to have an R2L tool bar if the menu bar cannot
12623 be drawn also R2L, and making the menu bar R2L is tricky due
12624 toolkit-specific code that implements it. If an R2L tool bar is
12625 ever supported, display_tool_bar_line should also be augmented to
12626 call unproduce_glyphs like display_line and display_string
12627 do. */
12628 it.paragraph_embedding = L2R;
12630 if (f->n_tool_bar_rows == 0)
12632 int new_height = tool_bar_height (f, &f->n_tool_bar_rows, true);
12634 if (new_height != WINDOW_PIXEL_HEIGHT (w))
12636 x_change_tool_bar_height (f, new_height);
12637 frame_default_tool_bar_height = new_height;
12638 /* Always do that now. */
12639 clear_glyph_matrix (w->desired_matrix);
12640 f->fonts_changed = true;
12641 return true;
12645 /* Display as many lines as needed to display all tool-bar items. */
12647 if (f->n_tool_bar_rows > 0)
12649 int border, rows, height, extra;
12651 if (TYPE_RANGED_INTEGERP (int, Vtool_bar_border))
12652 border = XINT (Vtool_bar_border);
12653 else if (EQ (Vtool_bar_border, Qinternal_border_width))
12654 border = FRAME_INTERNAL_BORDER_WIDTH (f);
12655 else if (EQ (Vtool_bar_border, Qborder_width))
12656 border = f->border_width;
12657 else
12658 border = 0;
12659 if (border < 0)
12660 border = 0;
12662 rows = f->n_tool_bar_rows;
12663 height = max (1, (it.last_visible_y - border) / rows);
12664 extra = it.last_visible_y - border - height * rows;
12666 while (it.current_y < it.last_visible_y)
12668 int h = 0;
12669 if (extra > 0 && rows-- > 0)
12671 h = (extra + rows - 1) / rows;
12672 extra -= h;
12674 display_tool_bar_line (&it, height + h);
12677 else
12679 while (it.current_y < it.last_visible_y)
12680 display_tool_bar_line (&it, 0);
12683 /* It doesn't make much sense to try scrolling in the tool-bar
12684 window, so don't do it. */
12685 w->desired_matrix->no_scrolling_p = true;
12686 w->must_be_updated_p = true;
12688 if (!NILP (Vauto_resize_tool_bars))
12690 bool change_height_p = true;
12692 /* If we couldn't display everything, change the tool-bar's
12693 height if there is room for more. */
12694 if (IT_STRING_CHARPOS (it) < it.end_charpos)
12695 change_height_p = true;
12697 /* We subtract 1 because display_tool_bar_line advances the
12698 glyph_row pointer before returning to its caller. We want to
12699 examine the last glyph row produced by
12700 display_tool_bar_line. */
12701 row = it.glyph_row - 1;
12703 /* If there are blank lines at the end, except for a partially
12704 visible blank line at the end that is smaller than
12705 FRAME_LINE_HEIGHT, change the tool-bar's height. */
12706 if (!MATRIX_ROW_DISPLAYS_TEXT_P (row)
12707 && row->height >= FRAME_LINE_HEIGHT (f))
12708 change_height_p = true;
12710 /* If row displays tool-bar items, but is partially visible,
12711 change the tool-bar's height. */
12712 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
12713 && MATRIX_ROW_BOTTOM_Y (row) > it.last_visible_y)
12714 change_height_p = true;
12716 /* Resize windows as needed by changing the `tool-bar-lines'
12717 frame parameter. */
12718 if (change_height_p)
12720 int nrows;
12721 int new_height = tool_bar_height (f, &nrows, true);
12723 change_height_p = ((EQ (Vauto_resize_tool_bars, Qgrow_only)
12724 && !f->minimize_tool_bar_window_p)
12725 ? (new_height > WINDOW_PIXEL_HEIGHT (w))
12726 : (new_height != WINDOW_PIXEL_HEIGHT (w)));
12727 f->minimize_tool_bar_window_p = false;
12729 if (change_height_p)
12731 x_change_tool_bar_height (f, new_height);
12732 frame_default_tool_bar_height = new_height;
12733 clear_glyph_matrix (w->desired_matrix);
12734 f->n_tool_bar_rows = nrows;
12735 f->fonts_changed = true;
12737 return true;
12742 f->minimize_tool_bar_window_p = false;
12743 return false;
12745 #endif /* USE_GTK || HAVE_NS */
12748 #if ! defined (USE_GTK) && ! defined (HAVE_NS)
12750 /* Get information about the tool-bar item which is displayed in GLYPH
12751 on frame F. Return in *PROP_IDX the index where tool-bar item
12752 properties start in F->tool_bar_items. Value is false if
12753 GLYPH doesn't display a tool-bar item. */
12755 static bool
12756 tool_bar_item_info (struct frame *f, struct glyph *glyph, int *prop_idx)
12758 Lisp_Object prop;
12759 int charpos;
12761 /* This function can be called asynchronously, which means we must
12762 exclude any possibility that Fget_text_property signals an
12763 error. */
12764 charpos = min (SCHARS (f->current_tool_bar_string), glyph->charpos);
12765 charpos = max (0, charpos);
12767 /* Get the text property `menu-item' at pos. The value of that
12768 property is the start index of this item's properties in
12769 F->tool_bar_items. */
12770 prop = Fget_text_property (make_number (charpos),
12771 Qmenu_item, f->current_tool_bar_string);
12772 if (! INTEGERP (prop))
12773 return false;
12774 *prop_idx = XINT (prop);
12775 return true;
12779 /* Get information about the tool-bar item at position X/Y on frame F.
12780 Return in *GLYPH a pointer to the glyph of the tool-bar item in
12781 the current matrix of the tool-bar window of F, or NULL if not
12782 on a tool-bar item. Return in *PROP_IDX the index of the tool-bar
12783 item in F->tool_bar_items. Value is
12785 -1 if X/Y is not on a tool-bar item
12786 0 if X/Y is on the same item that was highlighted before.
12787 1 otherwise. */
12789 static int
12790 get_tool_bar_item (struct frame *f, int x, int y, struct glyph **glyph,
12791 int *hpos, int *vpos, int *prop_idx)
12793 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12794 struct window *w = XWINDOW (f->tool_bar_window);
12795 int area;
12797 /* Find the glyph under X/Y. */
12798 *glyph = x_y_to_hpos_vpos (w, x, y, hpos, vpos, 0, 0, &area);
12799 if (*glyph == NULL)
12800 return -1;
12802 /* Get the start of this tool-bar item's properties in
12803 f->tool_bar_items. */
12804 if (!tool_bar_item_info (f, *glyph, prop_idx))
12805 return -1;
12807 /* Is mouse on the highlighted item? */
12808 if (EQ (f->tool_bar_window, hlinfo->mouse_face_window)
12809 && *vpos >= hlinfo->mouse_face_beg_row
12810 && *vpos <= hlinfo->mouse_face_end_row
12811 && (*vpos > hlinfo->mouse_face_beg_row
12812 || *hpos >= hlinfo->mouse_face_beg_col)
12813 && (*vpos < hlinfo->mouse_face_end_row
12814 || *hpos < hlinfo->mouse_face_end_col
12815 || hlinfo->mouse_face_past_end))
12816 return 0;
12818 return 1;
12822 /* EXPORT:
12823 Handle mouse button event on the tool-bar of frame F, at
12824 frame-relative coordinates X/Y. DOWN_P is true for a button press,
12825 false for button release. MODIFIERS is event modifiers for button
12826 release. */
12828 void
12829 handle_tool_bar_click (struct frame *f, int x, int y, bool down_p,
12830 int modifiers)
12832 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12833 struct window *w = XWINDOW (f->tool_bar_window);
12834 int hpos, vpos, prop_idx;
12835 struct glyph *glyph;
12836 Lisp_Object enabled_p;
12837 int ts;
12839 /* If not on the highlighted tool-bar item, and mouse-highlight is
12840 non-nil, return. This is so we generate the tool-bar button
12841 click only when the mouse button is released on the same item as
12842 where it was pressed. However, when mouse-highlight is disabled,
12843 generate the click when the button is released regardless of the
12844 highlight, since tool-bar items are not highlighted in that
12845 case. */
12846 frame_to_window_pixel_xy (w, &x, &y);
12847 ts = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
12848 if (ts == -1
12849 || (ts != 0 && !NILP (Vmouse_highlight)))
12850 return;
12852 /* When mouse-highlight is off, generate the click for the item
12853 where the button was pressed, disregarding where it was
12854 released. */
12855 if (NILP (Vmouse_highlight) && !down_p)
12856 prop_idx = f->last_tool_bar_item;
12858 /* If item is disabled, do nothing. */
12859 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
12860 if (NILP (enabled_p))
12861 return;
12863 if (down_p)
12865 /* Show item in pressed state. */
12866 if (!NILP (Vmouse_highlight))
12867 show_mouse_face (hlinfo, DRAW_IMAGE_SUNKEN);
12868 f->last_tool_bar_item = prop_idx;
12870 else
12872 Lisp_Object key, frame;
12873 struct input_event event;
12874 EVENT_INIT (event);
12876 /* Show item in released state. */
12877 if (!NILP (Vmouse_highlight))
12878 show_mouse_face (hlinfo, DRAW_IMAGE_RAISED);
12880 key = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_KEY);
12882 XSETFRAME (frame, f);
12883 event.kind = TOOL_BAR_EVENT;
12884 event.frame_or_window = frame;
12885 event.arg = frame;
12886 kbd_buffer_store_event (&event);
12888 event.kind = TOOL_BAR_EVENT;
12889 event.frame_or_window = frame;
12890 event.arg = key;
12891 event.modifiers = modifiers;
12892 kbd_buffer_store_event (&event);
12893 f->last_tool_bar_item = -1;
12898 /* Possibly highlight a tool-bar item on frame F when mouse moves to
12899 tool-bar window-relative coordinates X/Y. Called from
12900 note_mouse_highlight. */
12902 static void
12903 note_tool_bar_highlight (struct frame *f, int x, int y)
12905 Lisp_Object window = f->tool_bar_window;
12906 struct window *w = XWINDOW (window);
12907 Display_Info *dpyinfo = FRAME_DISPLAY_INFO (f);
12908 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12909 int hpos, vpos;
12910 struct glyph *glyph;
12911 struct glyph_row *row;
12912 int i;
12913 Lisp_Object enabled_p;
12914 int prop_idx;
12915 enum draw_glyphs_face draw = DRAW_IMAGE_RAISED;
12916 bool mouse_down_p;
12917 int rc;
12919 /* Function note_mouse_highlight is called with negative X/Y
12920 values when mouse moves outside of the frame. */
12921 if (x <= 0 || y <= 0)
12923 clear_mouse_face (hlinfo);
12924 return;
12927 rc = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
12928 if (rc < 0)
12930 /* Not on tool-bar item. */
12931 clear_mouse_face (hlinfo);
12932 return;
12934 else if (rc == 0)
12935 /* On same tool-bar item as before. */
12936 goto set_help_echo;
12938 clear_mouse_face (hlinfo);
12940 /* Mouse is down, but on different tool-bar item? */
12941 mouse_down_p = (x_mouse_grabbed (dpyinfo)
12942 && f == dpyinfo->last_mouse_frame);
12944 if (mouse_down_p && f->last_tool_bar_item != prop_idx)
12945 return;
12947 draw = mouse_down_p ? DRAW_IMAGE_SUNKEN : DRAW_IMAGE_RAISED;
12949 /* If tool-bar item is not enabled, don't highlight it. */
12950 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
12951 if (!NILP (enabled_p) && !NILP (Vmouse_highlight))
12953 /* Compute the x-position of the glyph. In front and past the
12954 image is a space. We include this in the highlighted area. */
12955 row = MATRIX_ROW (w->current_matrix, vpos);
12956 for (i = x = 0; i < hpos; ++i)
12957 x += row->glyphs[TEXT_AREA][i].pixel_width;
12959 /* Record this as the current active region. */
12960 hlinfo->mouse_face_beg_col = hpos;
12961 hlinfo->mouse_face_beg_row = vpos;
12962 hlinfo->mouse_face_beg_x = x;
12963 hlinfo->mouse_face_past_end = false;
12965 hlinfo->mouse_face_end_col = hpos + 1;
12966 hlinfo->mouse_face_end_row = vpos;
12967 hlinfo->mouse_face_end_x = x + glyph->pixel_width;
12968 hlinfo->mouse_face_window = window;
12969 hlinfo->mouse_face_face_id = TOOL_BAR_FACE_ID;
12971 /* Display it as active. */
12972 show_mouse_face (hlinfo, draw);
12975 set_help_echo:
12977 /* Set help_echo_string to a help string to display for this tool-bar item.
12978 XTread_socket does the rest. */
12979 help_echo_object = help_echo_window = Qnil;
12980 help_echo_pos = -1;
12981 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_HELP);
12982 if (NILP (help_echo_string))
12983 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_CAPTION);
12986 #endif /* !USE_GTK && !HAVE_NS */
12988 #endif /* HAVE_WINDOW_SYSTEM */
12992 /************************************************************************
12993 Horizontal scrolling
12994 ************************************************************************/
12996 /* For all leaf windows in the window tree rooted at WINDOW, set their
12997 hscroll value so that PT is (i) visible in the window, and (ii) so
12998 that it is not within a certain margin at the window's left and
12999 right border. Value is true if any window's hscroll has been
13000 changed. */
13002 static bool
13003 hscroll_window_tree (Lisp_Object window)
13005 bool hscrolled_p = false;
13006 bool hscroll_relative_p = FLOATP (Vhscroll_step);
13007 int hscroll_step_abs = 0;
13008 double hscroll_step_rel = 0;
13010 if (hscroll_relative_p)
13012 hscroll_step_rel = XFLOAT_DATA (Vhscroll_step);
13013 if (hscroll_step_rel < 0)
13015 hscroll_relative_p = false;
13016 hscroll_step_abs = 0;
13019 else if (TYPE_RANGED_INTEGERP (int, Vhscroll_step))
13021 hscroll_step_abs = XINT (Vhscroll_step);
13022 if (hscroll_step_abs < 0)
13023 hscroll_step_abs = 0;
13025 else
13026 hscroll_step_abs = 0;
13028 while (WINDOWP (window))
13030 struct window *w = XWINDOW (window);
13032 if (WINDOWP (w->contents))
13033 hscrolled_p |= hscroll_window_tree (w->contents);
13034 else if (w->cursor.vpos >= 0)
13036 int h_margin;
13037 int text_area_width;
13038 struct glyph_row *cursor_row;
13039 struct glyph_row *bottom_row;
13041 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->desired_matrix, w);
13042 if (w->cursor.vpos < bottom_row - w->desired_matrix->rows)
13043 cursor_row = MATRIX_ROW (w->desired_matrix, w->cursor.vpos);
13044 else
13045 cursor_row = bottom_row - 1;
13047 if (!cursor_row->enabled_p)
13049 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
13050 if (w->cursor.vpos < bottom_row - w->current_matrix->rows)
13051 cursor_row = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
13052 else
13053 cursor_row = bottom_row - 1;
13055 bool row_r2l_p = cursor_row->reversed_p;
13056 bool hscl = hscrolling_current_line_p (w);
13058 text_area_width = window_box_width (w, TEXT_AREA);
13060 /* Scroll when cursor is inside this scroll margin. */
13061 h_margin = hscroll_margin * WINDOW_FRAME_COLUMN_WIDTH (w);
13063 /* If the position of this window's point has explicitly
13064 changed, no more suspend auto hscrolling. */
13065 if (NILP (Fequal (Fwindow_point (window), Fwindow_old_point (window))))
13066 w->suspend_auto_hscroll = false;
13068 /* Remember window point. */
13069 Fset_marker (w->old_pointm,
13070 ((w == XWINDOW (selected_window))
13071 ? make_number (BUF_PT (XBUFFER (w->contents)))
13072 : Fmarker_position (w->pointm)),
13073 w->contents);
13075 if (!NILP (Fbuffer_local_value (Qauto_hscroll_mode, w->contents))
13076 && !w->suspend_auto_hscroll
13077 /* In some pathological cases, like restoring a window
13078 configuration into a frame that is much smaller than
13079 the one from which the configuration was saved, we
13080 get glyph rows whose start and end have zero buffer
13081 positions, which we cannot handle below. Just skip
13082 such windows. */
13083 && CHARPOS (cursor_row->start.pos) >= BUF_BEG (w->contents)
13084 /* For left-to-right rows, hscroll when cursor is either
13085 (i) inside the right hscroll margin, or (ii) if it is
13086 inside the left margin and the window is already
13087 hscrolled. */
13088 && ((!row_r2l_p
13089 && ((w->hscroll && w->cursor.x <= h_margin)
13090 || (cursor_row->enabled_p
13091 && cursor_row->truncated_on_right_p
13092 && (w->cursor.x >= text_area_width - h_margin))))
13093 /* For right-to-left rows, the logic is similar,
13094 except that rules for scrolling to left and right
13095 are reversed. E.g., if cursor.x <= h_margin, we
13096 need to hscroll "to the right" unconditionally,
13097 and that will scroll the screen to the left so as
13098 to reveal the next portion of the row. */
13099 || (row_r2l_p
13100 && ((cursor_row->enabled_p
13101 /* FIXME: It is confusing to set the
13102 truncated_on_right_p flag when R2L rows
13103 are actually truncated on the left. */
13104 && cursor_row->truncated_on_right_p
13105 && w->cursor.x <= h_margin)
13106 || (w->hscroll
13107 && (w->cursor.x >= text_area_width - h_margin))))
13108 /* This last condition is needed when moving
13109 vertically from an hscrolled line to a short line
13110 that doesn't need to be hscrolled. If we omit
13111 this condition, the line from which we move will
13112 remain hscrolled. */
13113 || (hscl
13114 && w->hscroll != w->min_hscroll
13115 && !cursor_row->truncated_on_left_p)))
13117 struct it it;
13118 ptrdiff_t hscroll;
13119 struct buffer *saved_current_buffer;
13120 ptrdiff_t pt;
13121 int wanted_x;
13123 /* Find point in a display of infinite width. */
13124 saved_current_buffer = current_buffer;
13125 current_buffer = XBUFFER (w->contents);
13127 if (w == XWINDOW (selected_window))
13128 pt = PT;
13129 else
13130 pt = clip_to_bounds (BEGV, marker_position (w->pointm), ZV);
13132 /* Move iterator to pt starting at cursor_row->start in
13133 a line with infinite width. */
13134 init_to_row_start (&it, w, cursor_row);
13135 if (hscl)
13136 it.first_visible_x = window_hscroll_limited (w, it.f)
13137 * FRAME_COLUMN_WIDTH (it.f);
13138 it.last_visible_x = INFINITY;
13139 move_it_in_display_line_to (&it, pt, -1, MOVE_TO_POS);
13140 /* If the line ends in an overlay string with a newline,
13141 we might infloop, because displaying the window will
13142 want to put the cursor after the overlay, i.e. at X
13143 coordinate of zero on the next screen line. So we
13144 use the buffer position prior to the overlay string
13145 instead. */
13146 if (it.method == GET_FROM_STRING && pt > 1)
13148 init_to_row_start (&it, w, cursor_row);
13149 if (hscl)
13150 it.first_visible_x = (window_hscroll_limited (w, it.f)
13151 * FRAME_COLUMN_WIDTH (it.f));
13152 move_it_in_display_line_to (&it, pt - 1, -1, MOVE_TO_POS);
13154 current_buffer = saved_current_buffer;
13156 /* Position cursor in window. */
13157 if (!hscroll_relative_p && hscroll_step_abs == 0)
13158 hscroll = max (0, (it.current_x
13159 - (ITERATOR_AT_END_OF_LINE_P (&it)
13160 ? (text_area_width - 4 * FRAME_COLUMN_WIDTH (it.f))
13161 : (text_area_width / 2))))
13162 / FRAME_COLUMN_WIDTH (it.f);
13163 else if ((!row_r2l_p
13164 && w->cursor.x >= text_area_width - h_margin)
13165 || (row_r2l_p && w->cursor.x <= h_margin))
13167 if (hscroll_relative_p)
13168 wanted_x = text_area_width * (1 - hscroll_step_rel)
13169 - h_margin;
13170 else
13171 wanted_x = text_area_width
13172 - hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
13173 - h_margin;
13174 hscroll
13175 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
13177 else
13179 if (hscroll_relative_p)
13180 wanted_x = text_area_width * hscroll_step_rel
13181 + h_margin;
13182 else
13183 wanted_x = hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
13184 + h_margin;
13185 hscroll
13186 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
13188 hscroll = max (hscroll, w->min_hscroll);
13190 /* Don't prevent redisplay optimizations if hscroll
13191 hasn't changed, as it will unnecessarily slow down
13192 redisplay. */
13193 if (w->hscroll != hscroll
13194 /* When hscrolling only the current line, we need to
13195 report hscroll even if its value is equal to the
13196 previous one, because the new line might need a
13197 different value. */
13198 || (hscl && w->last_cursor_vpos != w->cursor.vpos))
13200 struct buffer *b = XBUFFER (w->contents);
13201 b->prevent_redisplay_optimizations_p = true;
13202 w->hscroll = hscroll;
13203 hscrolled_p = true;
13208 window = w->next;
13211 /* Value is true if hscroll of any leaf window has been changed. */
13212 return hscrolled_p;
13216 /* Set hscroll so that cursor is visible and not inside horizontal
13217 scroll margins for all windows in the tree rooted at WINDOW. See
13218 also hscroll_window_tree above. Value is true if any window's
13219 hscroll has been changed. If it has, desired matrices on the frame
13220 of WINDOW are cleared. */
13222 static bool
13223 hscroll_windows (Lisp_Object window)
13225 bool hscrolled_p = hscroll_window_tree (window);
13226 if (hscrolled_p)
13227 clear_desired_matrices (XFRAME (WINDOW_FRAME (XWINDOW (window))));
13228 return hscrolled_p;
13233 /************************************************************************
13234 Redisplay
13235 ************************************************************************/
13237 /* Variables holding some state of redisplay if GLYPH_DEBUG is defined.
13238 This is sometimes handy to have in a debugger session. */
13240 #ifdef GLYPH_DEBUG
13242 /* First and last unchanged row for try_window_id. */
13244 static int debug_first_unchanged_at_end_vpos;
13245 static int debug_last_unchanged_at_beg_vpos;
13247 /* Delta vpos and y. */
13249 static int debug_dvpos, debug_dy;
13251 /* Delta in characters and bytes for try_window_id. */
13253 static ptrdiff_t debug_delta, debug_delta_bytes;
13255 /* Values of window_end_pos and window_end_vpos at the end of
13256 try_window_id. */
13258 static ptrdiff_t debug_end_vpos;
13260 /* Append a string to W->desired_matrix->method. FMT is a printf
13261 format string. If trace_redisplay_p is true also printf the
13262 resulting string to stderr. */
13264 static void debug_method_add (struct window *, char const *, ...)
13265 ATTRIBUTE_FORMAT_PRINTF (2, 3);
13267 static void
13268 debug_method_add (struct window *w, char const *fmt, ...)
13270 void *ptr = w;
13271 char *method = w->desired_matrix->method;
13272 int len = strlen (method);
13273 int size = sizeof w->desired_matrix->method;
13274 int remaining = size - len - 1;
13275 va_list ap;
13277 if (len && remaining)
13279 method[len] = '|';
13280 --remaining, ++len;
13283 va_start (ap, fmt);
13284 vsnprintf (method + len, remaining + 1, fmt, ap);
13285 va_end (ap);
13287 if (trace_redisplay_p)
13288 fprintf (stderr, "%p (%s): %s\n",
13289 ptr,
13290 ((BUFFERP (w->contents)
13291 && STRINGP (BVAR (XBUFFER (w->contents), name)))
13292 ? SSDATA (BVAR (XBUFFER (w->contents), name))
13293 : "no buffer"),
13294 method + len);
13297 #endif /* GLYPH_DEBUG */
13300 /* Value is true if all changes in window W, which displays
13301 current_buffer, are in the text between START and END. START is a
13302 buffer position, END is given as a distance from Z. Used in
13303 redisplay_internal for display optimization. */
13305 static bool
13306 text_outside_line_unchanged_p (struct window *w,
13307 ptrdiff_t start, ptrdiff_t end)
13309 bool unchanged_p = true;
13311 /* If text or overlays have changed, see where. */
13312 if (window_outdated (w))
13314 /* Gap in the line? */
13315 if (GPT < start || Z - GPT < end)
13316 unchanged_p = false;
13318 /* Changes start in front of the line, or end after it? */
13319 if (unchanged_p
13320 && (BEG_UNCHANGED < start - 1
13321 || END_UNCHANGED < end))
13322 unchanged_p = false;
13324 /* If selective display, can't optimize if changes start at the
13325 beginning of the line. */
13326 if (unchanged_p
13327 && INTEGERP (BVAR (current_buffer, selective_display))
13328 && XINT (BVAR (current_buffer, selective_display)) > 0
13329 && (BEG_UNCHANGED < start || GPT <= start))
13330 unchanged_p = false;
13332 /* If there are overlays at the start or end of the line, these
13333 may have overlay strings with newlines in them. A change at
13334 START, for instance, may actually concern the display of such
13335 overlay strings as well, and they are displayed on different
13336 lines. So, quickly rule out this case. (For the future, it
13337 might be desirable to implement something more telling than
13338 just BEG/END_UNCHANGED.) */
13339 if (unchanged_p)
13341 if (BEG + BEG_UNCHANGED == start
13342 && overlay_touches_p (start))
13343 unchanged_p = false;
13344 if (END_UNCHANGED == end
13345 && overlay_touches_p (Z - end))
13346 unchanged_p = false;
13349 /* Under bidi reordering, adding or deleting a character in the
13350 beginning of a paragraph, before the first strong directional
13351 character, can change the base direction of the paragraph (unless
13352 the buffer specifies a fixed paragraph direction), which will
13353 require redisplaying the whole paragraph. It might be worthwhile
13354 to find the paragraph limits and widen the range of redisplayed
13355 lines to that, but for now just give up this optimization. */
13356 if (!NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering))
13357 && NILP (BVAR (XBUFFER (w->contents), bidi_paragraph_direction)))
13358 unchanged_p = false;
13361 return unchanged_p;
13365 /* Do a frame update, taking possible shortcuts into account. This is
13366 the main external entry point for redisplay.
13368 If the last redisplay displayed an echo area message and that message
13369 is no longer requested, we clear the echo area or bring back the
13370 mini-buffer if that is in use. */
13372 void
13373 redisplay (void)
13375 redisplay_internal ();
13379 static Lisp_Object
13380 overlay_arrow_string_or_property (Lisp_Object var)
13382 Lisp_Object val;
13384 if (val = Fget (var, Qoverlay_arrow_string), STRINGP (val))
13385 return val;
13387 return Voverlay_arrow_string;
13390 /* Return true if there are any overlay-arrows in current_buffer. */
13391 static bool
13392 overlay_arrow_in_current_buffer_p (void)
13394 Lisp_Object vlist;
13396 for (vlist = Voverlay_arrow_variable_list;
13397 CONSP (vlist);
13398 vlist = XCDR (vlist))
13400 Lisp_Object var = XCAR (vlist);
13401 Lisp_Object val;
13403 if (!SYMBOLP (var))
13404 continue;
13405 val = find_symbol_value (var);
13406 if (MARKERP (val)
13407 && current_buffer == XMARKER (val)->buffer)
13408 return true;
13410 return false;
13414 /* Return true if any overlay_arrows have moved or overlay-arrow-string
13415 has changed.
13416 If SET_REDISPLAY is true, additionally, set the `redisplay' bit in those
13417 buffers that are affected. */
13419 static bool
13420 overlay_arrows_changed_p (bool set_redisplay)
13422 Lisp_Object vlist;
13423 bool changed = false;
13425 for (vlist = Voverlay_arrow_variable_list;
13426 CONSP (vlist);
13427 vlist = XCDR (vlist))
13429 Lisp_Object var = XCAR (vlist);
13430 Lisp_Object val, pstr;
13432 if (!SYMBOLP (var))
13433 continue;
13434 val = find_symbol_value (var);
13435 if (!MARKERP (val))
13436 continue;
13437 if (! EQ (COERCE_MARKER (val),
13438 /* FIXME: Don't we have a problem, using such a global
13439 * "last-position" if the variable is buffer-local? */
13440 Fget (var, Qlast_arrow_position))
13441 || ! (pstr = overlay_arrow_string_or_property (var),
13442 EQ (pstr, Fget (var, Qlast_arrow_string))))
13444 struct buffer *buf = XMARKER (val)->buffer;
13446 if (set_redisplay)
13448 if (buf)
13449 bset_redisplay (buf);
13450 changed = true;
13452 else
13453 return true;
13456 return changed;
13459 /* Mark overlay arrows to be updated on next redisplay. */
13461 static void
13462 update_overlay_arrows (int up_to_date)
13464 Lisp_Object vlist;
13466 for (vlist = Voverlay_arrow_variable_list;
13467 CONSP (vlist);
13468 vlist = XCDR (vlist))
13470 Lisp_Object var = XCAR (vlist);
13472 if (!SYMBOLP (var))
13473 continue;
13475 if (up_to_date > 0)
13477 Lisp_Object val = find_symbol_value (var);
13478 if (!MARKERP (val))
13479 continue;
13480 Fput (var, Qlast_arrow_position,
13481 COERCE_MARKER (val));
13482 Fput (var, Qlast_arrow_string,
13483 overlay_arrow_string_or_property (var));
13485 else if (up_to_date < 0
13486 || !NILP (Fget (var, Qlast_arrow_position)))
13488 Fput (var, Qlast_arrow_position, Qt);
13489 Fput (var, Qlast_arrow_string, Qt);
13495 /* Return overlay arrow string to display at row.
13496 Return integer (bitmap number) for arrow bitmap in left fringe.
13497 Return nil if no overlay arrow. */
13499 static Lisp_Object
13500 overlay_arrow_at_row (struct it *it, struct glyph_row *row)
13502 Lisp_Object vlist;
13504 for (vlist = Voverlay_arrow_variable_list;
13505 CONSP (vlist);
13506 vlist = XCDR (vlist))
13508 Lisp_Object var = XCAR (vlist);
13509 Lisp_Object val;
13511 if (!SYMBOLP (var))
13512 continue;
13514 val = find_symbol_value (var);
13516 if (MARKERP (val)
13517 && current_buffer == XMARKER (val)->buffer
13518 && (MATRIX_ROW_START_CHARPOS (row) == marker_position (val)))
13520 if (FRAME_WINDOW_P (it->f)
13521 /* FIXME: if ROW->reversed_p is set, this should test
13522 the right fringe, not the left one. */
13523 && WINDOW_LEFT_FRINGE_WIDTH (it->w) > 0)
13525 #ifdef HAVE_WINDOW_SYSTEM
13526 if (val = Fget (var, Qoverlay_arrow_bitmap), SYMBOLP (val))
13528 int fringe_bitmap = lookup_fringe_bitmap (val);
13529 if (fringe_bitmap != 0)
13530 return make_number (fringe_bitmap);
13532 #endif
13533 return make_number (-1); /* Use default arrow bitmap. */
13535 return overlay_arrow_string_or_property (var);
13539 return Qnil;
13542 /* Return true if point moved out of or into a composition. Otherwise
13543 return false. PREV_BUF and PREV_PT are the last point buffer and
13544 position. BUF and PT are the current point buffer and position. */
13546 static bool
13547 check_point_in_composition (struct buffer *prev_buf, ptrdiff_t prev_pt,
13548 struct buffer *buf, ptrdiff_t pt)
13550 ptrdiff_t start, end;
13551 Lisp_Object prop;
13552 Lisp_Object buffer;
13554 XSETBUFFER (buffer, buf);
13555 /* Check a composition at the last point if point moved within the
13556 same buffer. */
13557 if (prev_buf == buf)
13559 if (prev_pt == pt)
13560 /* Point didn't move. */
13561 return false;
13563 if (prev_pt > BUF_BEGV (buf) && prev_pt < BUF_ZV (buf)
13564 && find_composition (prev_pt, -1, &start, &end, &prop, buffer)
13565 && composition_valid_p (start, end, prop)
13566 && start < prev_pt && end > prev_pt)
13567 /* The last point was within the composition. Return true iff
13568 point moved out of the composition. */
13569 return (pt <= start || pt >= end);
13572 /* Check a composition at the current point. */
13573 return (pt > BUF_BEGV (buf) && pt < BUF_ZV (buf)
13574 && find_composition (pt, -1, &start, &end, &prop, buffer)
13575 && composition_valid_p (start, end, prop)
13576 && start < pt && end > pt);
13579 /* Reconsider the clip changes of buffer which is displayed in W. */
13581 static void
13582 reconsider_clip_changes (struct window *w)
13584 struct buffer *b = XBUFFER (w->contents);
13586 if (b->clip_changed
13587 && w->window_end_valid
13588 && w->current_matrix->buffer == b
13589 && w->current_matrix->zv == BUF_ZV (b)
13590 && w->current_matrix->begv == BUF_BEGV (b))
13591 b->clip_changed = false;
13593 /* If display wasn't paused, and W is not a tool bar window, see if
13594 point has been moved into or out of a composition. In that case,
13595 set b->clip_changed to force updating the screen. If
13596 b->clip_changed has already been set, skip this check. */
13597 if (!b->clip_changed && w->window_end_valid)
13599 ptrdiff_t pt = (w == XWINDOW (selected_window)
13600 ? PT : marker_position (w->pointm));
13602 if ((w->current_matrix->buffer != b || pt != w->last_point)
13603 && check_point_in_composition (w->current_matrix->buffer,
13604 w->last_point, b, pt))
13605 b->clip_changed = true;
13609 static void
13610 propagate_buffer_redisplay (void)
13611 { /* Resetting b->text->redisplay is problematic!
13612 We can't just reset it in the case that some window that displays
13613 it has not been redisplayed; and such a window can stay
13614 unredisplayed for a long time if it's currently invisible.
13615 But we do want to reset it at the end of redisplay otherwise
13616 its displayed windows will keep being redisplayed over and over
13617 again.
13618 So we copy all b->text->redisplay flags up to their windows here,
13619 such that mark_window_display_accurate can safely reset
13620 b->text->redisplay. */
13621 Lisp_Object ws = window_list ();
13622 for (; CONSP (ws); ws = XCDR (ws))
13624 struct window *thisw = XWINDOW (XCAR (ws));
13625 struct buffer *thisb = XBUFFER (thisw->contents);
13626 if (thisb->text->redisplay)
13627 thisw->redisplay = true;
13631 #define STOP_POLLING \
13632 do { if (! polling_stopped_here) stop_polling (); \
13633 polling_stopped_here = true; } while (false)
13635 #define RESUME_POLLING \
13636 do { if (polling_stopped_here) start_polling (); \
13637 polling_stopped_here = false; } while (false)
13640 /* Perhaps in the future avoid recentering windows if it
13641 is not necessary; currently that causes some problems. */
13643 static void
13644 redisplay_internal (void)
13646 struct window *w = XWINDOW (selected_window);
13647 struct window *sw;
13648 struct frame *fr;
13649 bool pending;
13650 bool must_finish = false, match_p;
13651 struct text_pos tlbufpos, tlendpos;
13652 int number_of_visible_frames;
13653 ptrdiff_t count;
13654 struct frame *sf;
13655 bool polling_stopped_here = false;
13656 Lisp_Object tail, frame;
13658 /* Set a limit to the number of retries we perform due to horizontal
13659 scrolling, this avoids getting stuck in an uninterruptible
13660 infinite loop (Bug #24633). */
13661 enum { MAX_HSCROLL_RETRIES = 16 };
13662 int hscroll_retries = 0;
13664 /* Limit the number of retries for when frame(s) become garbaged as
13665 result of redisplaying them. Some packages set various redisplay
13666 hooks, such as window-scroll-functions, to run Lisp that always
13667 calls APIs which cause the frame's garbaged flag to become set,
13668 so we loop indefinitely. */
13669 enum {MAX_GARBAGED_FRAME_RETRIES = 2 };
13670 int garbaged_frame_retries = 0;
13672 /* True means redisplay has to consider all windows on all
13673 frames. False, only selected_window is considered. */
13674 bool consider_all_windows_p;
13676 /* True means redisplay has to redisplay the miniwindow. */
13677 bool update_miniwindow_p = false;
13679 TRACE ((stderr, "redisplay_internal %d\n", redisplaying_p));
13681 /* No redisplay if running in batch mode or frame is not yet fully
13682 initialized, or redisplay is explicitly turned off by setting
13683 Vinhibit_redisplay. */
13684 if (FRAME_INITIAL_P (SELECTED_FRAME ())
13685 || !NILP (Vinhibit_redisplay))
13686 return;
13688 /* Don't examine these until after testing Vinhibit_redisplay.
13689 When Emacs is shutting down, perhaps because its connection to
13690 X has dropped, we should not look at them at all. */
13691 fr = XFRAME (w->frame);
13692 sf = SELECTED_FRAME ();
13694 if (!fr->glyphs_initialized_p)
13695 return;
13697 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS)
13698 if (popup_activated ())
13699 return;
13700 #endif
13702 /* I don't think this happens but let's be paranoid. */
13703 if (redisplaying_p)
13704 return;
13706 /* Record a function that clears redisplaying_p
13707 when we leave this function. */
13708 count = SPECPDL_INDEX ();
13709 record_unwind_protect_void (unwind_redisplay);
13710 redisplaying_p = true;
13711 block_buffer_flips ();
13712 specbind (Qinhibit_free_realized_faces, Qnil);
13714 /* Record this function, so it appears on the profiler's backtraces. */
13715 record_in_backtrace (Qredisplay_internal_xC_functionx, 0, 0);
13717 FOR_EACH_FRAME (tail, frame)
13718 XFRAME (frame)->already_hscrolled_p = false;
13720 retry:
13721 /* Remember the currently selected window. */
13722 sw = w;
13724 pending = false;
13725 forget_escape_and_glyphless_faces ();
13727 inhibit_free_realized_faces = false;
13729 /* If face_change, init_iterator will free all realized faces, which
13730 includes the faces referenced from current matrices. So, we
13731 can't reuse current matrices in this case. */
13732 if (face_change)
13733 windows_or_buffers_changed = 47;
13735 if ((FRAME_TERMCAP_P (sf) || FRAME_MSDOS_P (sf))
13736 && FRAME_TTY (sf)->previous_frame != sf)
13738 /* Since frames on a single ASCII terminal share the same
13739 display area, displaying a different frame means redisplay
13740 the whole thing. */
13741 SET_FRAME_GARBAGED (sf);
13742 #ifndef DOS_NT
13743 set_tty_color_mode (FRAME_TTY (sf), sf);
13744 #endif
13745 FRAME_TTY (sf)->previous_frame = sf;
13748 /* Set the visible flags for all frames. Do this before checking for
13749 resized or garbaged frames; they want to know if their frames are
13750 visible. See the comment in frame.h for FRAME_SAMPLE_VISIBILITY. */
13751 number_of_visible_frames = 0;
13753 FOR_EACH_FRAME (tail, frame)
13755 struct frame *f = XFRAME (frame);
13757 if (FRAME_VISIBLE_P (f))
13759 ++number_of_visible_frames;
13760 /* Adjust matrices for visible frames only. */
13761 if (f->fonts_changed)
13763 adjust_frame_glyphs (f);
13764 /* Disable all redisplay optimizations for this frame.
13765 This is because adjust_frame_glyphs resets the
13766 enabled_p flag for all glyph rows of all windows, so
13767 many optimizations will fail anyway, and some might
13768 fail to test that flag and do bogus things as
13769 result. */
13770 SET_FRAME_GARBAGED (f);
13771 f->fonts_changed = false;
13773 /* If cursor type has been changed on the frame
13774 other than selected, consider all frames. */
13775 if (f != sf && f->cursor_type_changed)
13776 fset_redisplay (f);
13778 clear_desired_matrices (f);
13781 /* Notice any pending interrupt request to change frame size. */
13782 do_pending_window_change (true);
13784 /* do_pending_window_change could change the selected_window due to
13785 frame resizing which makes the selected window too small. */
13786 if (WINDOWP (selected_window) && (w = XWINDOW (selected_window)) != sw)
13787 sw = w;
13789 /* Clear frames marked as garbaged. */
13790 clear_garbaged_frames ();
13792 /* Build menubar and tool-bar items. */
13793 if (NILP (Vmemory_full))
13794 prepare_menu_bars ();
13796 reconsider_clip_changes (w);
13798 /* In most cases selected window displays current buffer. */
13799 match_p = XBUFFER (w->contents) == current_buffer;
13800 if (match_p)
13802 /* Detect case that we need to write or remove a star in the mode line. */
13803 if ((SAVE_MODIFF < MODIFF) != w->last_had_star)
13804 w->update_mode_line = true;
13806 if (mode_line_update_needed (w))
13807 w->update_mode_line = true;
13809 /* If reconsider_clip_changes above decided that the narrowing
13810 in the current buffer changed, make sure all other windows
13811 showing that buffer will be redisplayed. */
13812 if (current_buffer->clip_changed)
13813 bset_update_mode_line (current_buffer);
13816 /* Normally the message* functions will have already displayed and
13817 updated the echo area, but the frame may have been trashed, or
13818 the update may have been preempted, so display the echo area
13819 again here. Checking message_cleared_p captures the case that
13820 the echo area should be cleared. */
13821 if ((!NILP (echo_area_buffer[0]) && !display_last_displayed_message_p)
13822 || (!NILP (echo_area_buffer[1]) && display_last_displayed_message_p)
13823 || (message_cleared_p
13824 && minibuf_level == 0
13825 /* If the mini-window is currently selected, this means the
13826 echo-area doesn't show through. */
13827 && !MINI_WINDOW_P (XWINDOW (selected_window))))
13829 echo_area_display (false);
13831 /* If echo_area_display resizes the mini-window, the redisplay and
13832 window_sizes_changed flags of the selected frame are set, but
13833 it's too late for the hooks in window-size-change-functions,
13834 which have been examined already in prepare_menu_bars. So in
13835 that case we call the hooks here only for the selected frame. */
13836 if (sf->redisplay)
13838 ptrdiff_t count1 = SPECPDL_INDEX ();
13840 record_unwind_save_match_data ();
13841 run_window_size_change_functions (selected_frame);
13842 unbind_to (count1, Qnil);
13845 if (message_cleared_p)
13846 update_miniwindow_p = true;
13848 must_finish = true;
13850 /* If we don't display the current message, don't clear the
13851 message_cleared_p flag, because, if we did, we wouldn't clear
13852 the echo area in the next redisplay which doesn't preserve
13853 the echo area. */
13854 if (!display_last_displayed_message_p)
13855 message_cleared_p = false;
13857 else if (EQ (selected_window, minibuf_window)
13858 && (current_buffer->clip_changed || window_outdated (w))
13859 && resize_mini_window (w, false))
13861 if (sf->redisplay)
13863 ptrdiff_t count1 = SPECPDL_INDEX ();
13865 record_unwind_save_match_data ();
13866 run_window_size_change_functions (selected_frame);
13867 unbind_to (count1, Qnil);
13870 /* Resized active mini-window to fit the size of what it is
13871 showing if its contents might have changed. */
13872 must_finish = true;
13874 /* If window configuration was changed, frames may have been
13875 marked garbaged. Clear them or we will experience
13876 surprises wrt scrolling. */
13877 clear_garbaged_frames ();
13880 if (windows_or_buffers_changed && !update_mode_lines)
13881 /* Code that sets windows_or_buffers_changed doesn't distinguish whether
13882 only the windows's contents needs to be refreshed, or whether the
13883 mode-lines also need a refresh. */
13884 update_mode_lines = (windows_or_buffers_changed == REDISPLAY_SOME
13885 ? REDISPLAY_SOME : 32);
13887 /* If specs for an arrow have changed, do thorough redisplay
13888 to ensure we remove any arrow that should no longer exist. */
13889 /* Apparently, this is the only case where we update other windows,
13890 without updating other mode-lines. */
13891 overlay_arrows_changed_p (true);
13893 consider_all_windows_p = (update_mode_lines
13894 || windows_or_buffers_changed);
13896 #define AINC(a,i) \
13898 Lisp_Object entry = Fgethash (make_number (i), a, make_number (0)); \
13899 if (INTEGERP (entry)) \
13900 Fputhash (make_number (i), make_number (1 + XINT (entry)), a); \
13903 AINC (Vredisplay__all_windows_cause, windows_or_buffers_changed);
13904 AINC (Vredisplay__mode_lines_cause, update_mode_lines);
13906 /* Optimize the case that only the line containing the cursor in the
13907 selected window has changed. Variables starting with this_ are
13908 set in display_line and record information about the line
13909 containing the cursor. */
13910 tlbufpos = this_line_start_pos;
13911 tlendpos = this_line_end_pos;
13912 if (!consider_all_windows_p
13913 && CHARPOS (tlbufpos) > 0
13914 && !w->update_mode_line
13915 && !current_buffer->clip_changed
13916 && !current_buffer->prevent_redisplay_optimizations_p
13917 && FRAME_VISIBLE_P (XFRAME (w->frame))
13918 && !FRAME_OBSCURED_P (XFRAME (w->frame))
13919 && !XFRAME (w->frame)->cursor_type_changed
13920 && !XFRAME (w->frame)->face_change
13921 /* Make sure recorded data applies to current buffer, etc. */
13922 && this_line_buffer == current_buffer
13923 && match_p
13924 && !w->force_start
13925 && !w->optional_new_start
13926 /* Point must be on the line that we have info recorded about. */
13927 && PT >= CHARPOS (tlbufpos)
13928 && PT <= Z - CHARPOS (tlendpos)
13929 /* All text outside that line, including its final newline,
13930 must be unchanged. */
13931 && text_outside_line_unchanged_p (w, CHARPOS (tlbufpos),
13932 CHARPOS (tlendpos)))
13934 if (CHARPOS (tlbufpos) > BEGV
13935 && FETCH_BYTE (BYTEPOS (tlbufpos) - 1) != '\n'
13936 && (CHARPOS (tlbufpos) == ZV
13937 || FETCH_BYTE (BYTEPOS (tlbufpos)) == '\n'))
13938 /* Former continuation line has disappeared by becoming empty. */
13939 goto cancel;
13940 else if (window_outdated (w) || MINI_WINDOW_P (w))
13942 /* We have to handle the case of continuation around a
13943 wide-column character (see the comment in indent.c around
13944 line 1340).
13946 For instance, in the following case:
13948 -------- Insert --------
13949 K_A_N_\\ `a' K_A_N_a\ `X_' are wide-column chars.
13950 J_I_ ==> J_I_ `^^' are cursors.
13951 ^^ ^^
13952 -------- --------
13954 As we have to redraw the line above, we cannot use this
13955 optimization. */
13957 struct it it;
13958 int line_height_before = this_line_pixel_height;
13960 /* Note that start_display will handle the case that the
13961 line starting at tlbufpos is a continuation line. */
13962 start_display (&it, w, tlbufpos);
13964 /* Implementation note: It this still necessary? */
13965 if (it.current_x != this_line_start_x)
13966 goto cancel;
13968 TRACE ((stderr, "trying display optimization 1\n"));
13969 w->cursor.vpos = -1;
13970 overlay_arrow_seen = false;
13971 it.vpos = this_line_vpos;
13972 it.current_y = this_line_y;
13973 it.glyph_row = MATRIX_ROW (w->desired_matrix, this_line_vpos);
13974 display_line (&it, -1);
13976 /* If line contains point, is not continued,
13977 and ends at same distance from eob as before, we win. */
13978 if (w->cursor.vpos >= 0
13979 /* Line is not continued, otherwise this_line_start_pos
13980 would have been set to 0 in display_line. */
13981 && CHARPOS (this_line_start_pos)
13982 /* Line ends as before. */
13983 && CHARPOS (this_line_end_pos) == CHARPOS (tlendpos)
13984 /* Line has same height as before. Otherwise other lines
13985 would have to be shifted up or down. */
13986 && this_line_pixel_height == line_height_before)
13988 /* If this is not the window's last line, we must adjust
13989 the charstarts of the lines below. */
13990 if (it.current_y < it.last_visible_y)
13992 struct glyph_row *row
13993 = MATRIX_ROW (w->current_matrix, this_line_vpos + 1);
13994 ptrdiff_t delta, delta_bytes;
13996 /* We used to distinguish between two cases here,
13997 conditioned by Z - CHARPOS (tlendpos) == ZV, for
13998 when the line ends in a newline or the end of the
13999 buffer's accessible portion. But both cases did
14000 the same, so they were collapsed. */
14001 delta = (Z
14002 - CHARPOS (tlendpos)
14003 - MATRIX_ROW_START_CHARPOS (row));
14004 delta_bytes = (Z_BYTE
14005 - BYTEPOS (tlendpos)
14006 - MATRIX_ROW_START_BYTEPOS (row));
14008 increment_matrix_positions (w->current_matrix,
14009 this_line_vpos + 1,
14010 w->current_matrix->nrows,
14011 delta, delta_bytes);
14014 /* If this row displays text now but previously didn't,
14015 or vice versa, w->window_end_vpos may have to be
14016 adjusted. */
14017 if (MATRIX_ROW_DISPLAYS_TEXT_P (it.glyph_row - 1))
14019 if (w->window_end_vpos < this_line_vpos)
14020 w->window_end_vpos = this_line_vpos;
14022 else if (w->window_end_vpos == this_line_vpos
14023 && this_line_vpos > 0)
14024 w->window_end_vpos = this_line_vpos - 1;
14025 w->window_end_valid = false;
14027 /* Update hint: No need to try to scroll in update_window. */
14028 w->desired_matrix->no_scrolling_p = true;
14030 #ifdef GLYPH_DEBUG
14031 *w->desired_matrix->method = 0;
14032 debug_method_add (w, "optimization 1");
14033 #endif
14034 #ifdef HAVE_WINDOW_SYSTEM
14035 update_window_fringes (w, false);
14036 #endif
14037 goto update;
14039 else
14040 goto cancel;
14042 else if (/* Cursor position hasn't changed. */
14043 PT == w->last_point
14044 /* Make sure the cursor was last displayed
14045 in this window. Otherwise we have to reposition it. */
14047 /* PXW: Must be converted to pixels, probably. */
14048 && 0 <= w->cursor.vpos
14049 && w->cursor.vpos < WINDOW_TOTAL_LINES (w))
14051 if (!must_finish)
14053 do_pending_window_change (true);
14054 /* If selected_window changed, redisplay again. */
14055 if (WINDOWP (selected_window)
14056 && (w = XWINDOW (selected_window)) != sw)
14057 goto retry;
14059 /* We used to always goto end_of_redisplay here, but this
14060 isn't enough if we have a blinking cursor. */
14061 if (w->cursor_off_p == w->last_cursor_off_p)
14062 goto end_of_redisplay;
14064 goto update;
14066 /* If highlighting the region, or if the cursor is in the echo area,
14067 then we can't just move the cursor. */
14068 else if (NILP (Vshow_trailing_whitespace)
14069 && !cursor_in_echo_area)
14071 struct it it;
14072 struct glyph_row *row;
14074 /* Skip from tlbufpos to PT and see where it is. Note that
14075 PT may be in invisible text. If so, we will end at the
14076 next visible position. */
14077 init_iterator (&it, w, CHARPOS (tlbufpos), BYTEPOS (tlbufpos),
14078 NULL, DEFAULT_FACE_ID);
14079 it.current_x = this_line_start_x;
14080 it.current_y = this_line_y;
14081 it.vpos = this_line_vpos;
14083 /* The call to move_it_to stops in front of PT, but
14084 moves over before-strings. */
14085 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
14087 if (it.vpos == this_line_vpos
14088 && (row = MATRIX_ROW (w->current_matrix, this_line_vpos),
14089 row->enabled_p))
14091 eassert (this_line_vpos == it.vpos);
14092 eassert (this_line_y == it.current_y);
14093 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
14094 if (cursor_row_fully_visible_p (w, false, true))
14096 #ifdef GLYPH_DEBUG
14097 *w->desired_matrix->method = 0;
14098 debug_method_add (w, "optimization 3");
14099 #endif
14100 goto update;
14102 else
14103 goto cancel;
14105 else
14106 goto cancel;
14109 cancel:
14110 /* Text changed drastically or point moved off of line. */
14111 SET_MATRIX_ROW_ENABLED_P (w->desired_matrix, this_line_vpos, false);
14114 CHARPOS (this_line_start_pos) = 0;
14115 ++clear_face_cache_count;
14116 #ifdef HAVE_WINDOW_SYSTEM
14117 ++clear_image_cache_count;
14118 #endif
14120 /* Build desired matrices, and update the display. If
14121 consider_all_windows_p, do it for all windows on all frames that
14122 require redisplay, as specified by their 'redisplay' flag.
14123 Otherwise do it for selected_window, only. */
14125 if (consider_all_windows_p)
14127 FOR_EACH_FRAME (tail, frame)
14128 XFRAME (frame)->updated_p = false;
14130 propagate_buffer_redisplay ();
14132 FOR_EACH_FRAME (tail, frame)
14134 struct frame *f = XFRAME (frame);
14136 /* We don't have to do anything for unselected terminal
14137 frames. */
14138 if ((FRAME_TERMCAP_P (f) || FRAME_MSDOS_P (f))
14139 && !EQ (FRAME_TTY (f)->top_frame, frame))
14140 continue;
14142 retry_frame:
14143 if (FRAME_WINDOW_P (f) || FRAME_TERMCAP_P (f) || f == sf)
14145 bool gcscrollbars
14146 /* Only GC scrollbars when we redisplay the whole frame. */
14147 = f->redisplay || !REDISPLAY_SOME_P ();
14148 bool f_redisplay_flag = f->redisplay;
14149 /* Mark all the scroll bars to be removed; we'll redeem
14150 the ones we want when we redisplay their windows. */
14151 if (gcscrollbars && FRAME_TERMINAL (f)->condemn_scroll_bars_hook)
14152 FRAME_TERMINAL (f)->condemn_scroll_bars_hook (f);
14154 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
14155 redisplay_windows (FRAME_ROOT_WINDOW (f));
14156 /* Remember that the invisible frames need to be redisplayed next
14157 time they're visible. */
14158 else if (!REDISPLAY_SOME_P ())
14159 f->redisplay = true;
14161 /* The X error handler may have deleted that frame. */
14162 if (!FRAME_LIVE_P (f))
14163 continue;
14165 /* Any scroll bars which redisplay_windows should have
14166 nuked should now go away. */
14167 if (gcscrollbars && FRAME_TERMINAL (f)->judge_scroll_bars_hook)
14168 FRAME_TERMINAL (f)->judge_scroll_bars_hook (f);
14170 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
14172 /* If fonts changed on visible frame, display again. */
14173 if (f->fonts_changed)
14175 adjust_frame_glyphs (f);
14176 /* Disable all redisplay optimizations for this
14177 frame. For the reasons, see the comment near
14178 the previous call to adjust_frame_glyphs above. */
14179 SET_FRAME_GARBAGED (f);
14180 f->fonts_changed = false;
14181 goto retry_frame;
14184 /* See if we have to hscroll. */
14185 if (!f->already_hscrolled_p)
14187 f->already_hscrolled_p = true;
14188 if (hscroll_retries <= MAX_HSCROLL_RETRIES
14189 && hscroll_windows (f->root_window))
14191 hscroll_retries++;
14192 goto retry_frame;
14196 /* If the frame's redisplay flag was not set before
14197 we went about redisplaying its windows, but it is
14198 set now, that means we employed some redisplay
14199 optimizations inside redisplay_windows, and
14200 bypassed producing some screen lines. But if
14201 f->redisplay is now set, it might mean the old
14202 faces are no longer valid (e.g., if redisplaying
14203 some window called some Lisp which defined a new
14204 face or redefined an existing face), so trying to
14205 use them in update_frame will segfault.
14206 Therefore, we must redisplay this frame. */
14207 if (!f_redisplay_flag && f->redisplay)
14208 goto retry_frame;
14209 /* In some case (e.g., window resize), we notice
14210 only during window updating that the window
14211 content changed unpredictably (e.g., a GTK
14212 scrollbar moved, or some Lisp hook that winds up
14213 calling adjust_frame_glyphs) and that our
14214 previous estimation of the frame content was
14215 garbage. We have to start over. These cases
14216 should be rare, so going all the way back to the
14217 top of redisplay should be good enough. */
14218 if (FRAME_GARBAGED_P (f)
14219 && garbaged_frame_retries++ < MAX_GARBAGED_FRAME_RETRIES)
14220 goto retry;
14222 #if defined (HAVE_WINDOW_SYSTEM) && !defined (HAVE_NS)
14223 x_clear_under_internal_border (f);
14224 #endif /* HAVE_WINDOW_SYSTEM && !HAVE_NS */
14226 /* Prevent various kinds of signals during display
14227 update. stdio is not robust about handling
14228 signals, which can cause an apparent I/O error. */
14229 if (interrupt_input)
14230 unrequest_sigio ();
14231 STOP_POLLING;
14233 pending |= update_frame (f, false, false);
14234 f->cursor_type_changed = false;
14235 f->updated_p = true;
14240 eassert (EQ (XFRAME (selected_frame)->selected_window, selected_window));
14242 if (!pending)
14244 /* Do the mark_window_display_accurate after all windows have
14245 been redisplayed because this call resets flags in buffers
14246 which are needed for proper redisplay. */
14247 FOR_EACH_FRAME (tail, frame)
14249 struct frame *f = XFRAME (frame);
14250 if (f->updated_p)
14252 f->redisplay = false;
14253 f->garbaged = false;
14254 mark_window_display_accurate (f->root_window, true);
14255 if (FRAME_TERMINAL (f)->frame_up_to_date_hook)
14256 FRAME_TERMINAL (f)->frame_up_to_date_hook (f);
14261 else if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
14263 displayed_buffer = XBUFFER (XWINDOW (selected_window)->contents);
14264 /* Use list_of_error, not Qerror, so that
14265 we catch only errors and don't run the debugger. */
14266 internal_condition_case_1 (redisplay_window_1, selected_window,
14267 list_of_error,
14268 redisplay_window_error);
14269 if (update_miniwindow_p)
14270 internal_condition_case_1 (redisplay_window_1,
14271 FRAME_MINIBUF_WINDOW (sf), list_of_error,
14272 redisplay_window_error);
14274 /* Compare desired and current matrices, perform output. */
14276 update:
14277 /* If fonts changed, display again. Likewise if redisplay_window_1
14278 above caused some change (e.g., a change in faces) that requires
14279 considering the entire frame again. */
14280 if (sf->fonts_changed || sf->redisplay)
14282 if (sf->redisplay)
14284 /* Set this to force a more thorough redisplay.
14285 Otherwise, we might immediately loop back to the
14286 above "else-if" clause (since all the conditions that
14287 led here might still be true), and we will then
14288 infloop, because the selected-frame's redisplay flag
14289 is not (and cannot be) reset. */
14290 windows_or_buffers_changed = 50;
14292 goto retry;
14295 /* Prevent freeing of realized faces, since desired matrices are
14296 pending that reference the faces we computed and cached. */
14297 inhibit_free_realized_faces = true;
14299 /* Prevent various kinds of signals during display update.
14300 stdio is not robust about handling signals,
14301 which can cause an apparent I/O error. */
14302 if (interrupt_input)
14303 unrequest_sigio ();
14304 STOP_POLLING;
14306 if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
14308 if (hscroll_retries <= MAX_HSCROLL_RETRIES
14309 && hscroll_windows (selected_window))
14311 hscroll_retries++;
14312 goto retry;
14315 XWINDOW (selected_window)->must_be_updated_p = true;
14316 pending = update_frame (sf, false, false);
14317 sf->cursor_type_changed = false;
14320 /* We may have called echo_area_display at the top of this
14321 function. If the echo area is on another frame, that may
14322 have put text on a frame other than the selected one, so the
14323 above call to update_frame would not have caught it. Catch
14324 it here. */
14325 Lisp_Object mini_window = FRAME_MINIBUF_WINDOW (sf);
14326 struct frame *mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
14328 if (mini_frame != sf && FRAME_WINDOW_P (mini_frame))
14330 XWINDOW (mini_window)->must_be_updated_p = true;
14331 pending |= update_frame (mini_frame, false, false);
14332 mini_frame->cursor_type_changed = false;
14333 if (!pending && hscroll_retries <= MAX_HSCROLL_RETRIES
14334 && hscroll_windows (mini_window))
14336 hscroll_retries++;
14337 goto retry;
14342 /* If display was paused because of pending input, make sure we do a
14343 thorough update the next time. */
14344 if (pending)
14346 /* Prevent the optimization at the beginning of
14347 redisplay_internal that tries a single-line update of the
14348 line containing the cursor in the selected window. */
14349 CHARPOS (this_line_start_pos) = 0;
14351 /* Let the overlay arrow be updated the next time. */
14352 update_overlay_arrows (0);
14354 /* If we pause after scrolling, some rows in the current
14355 matrices of some windows are not valid. */
14356 if (!WINDOW_FULL_WIDTH_P (w)
14357 && !FRAME_WINDOW_P (XFRAME (w->frame)))
14358 update_mode_lines = 36;
14360 else
14362 if (!consider_all_windows_p)
14364 /* This has already been done above if
14365 consider_all_windows_p is set. */
14366 if (XBUFFER (w->contents)->text->redisplay
14367 && buffer_window_count (XBUFFER (w->contents)) > 1)
14368 /* This can happen if b->text->redisplay was set during
14369 jit-lock. */
14370 propagate_buffer_redisplay ();
14371 mark_window_display_accurate_1 (w, true);
14373 /* Say overlay arrows are up to date. */
14374 update_overlay_arrows (1);
14376 if (FRAME_TERMINAL (sf)->frame_up_to_date_hook != 0)
14377 FRAME_TERMINAL (sf)->frame_up_to_date_hook (sf);
14380 update_mode_lines = 0;
14381 windows_or_buffers_changed = 0;
14384 /* Start SIGIO interrupts coming again. Having them off during the
14385 code above makes it less likely one will discard output, but not
14386 impossible, since there might be stuff in the system buffer here.
14387 But it is much hairier to try to do anything about that. */
14388 if (interrupt_input)
14389 request_sigio ();
14390 RESUME_POLLING;
14392 /* If a frame has become visible which was not before, redisplay
14393 again, so that we display it. Expose events for such a frame
14394 (which it gets when becoming visible) don't call the parts of
14395 redisplay constructing glyphs, so simply exposing a frame won't
14396 display anything in this case. So, we have to display these
14397 frames here explicitly. */
14398 if (!pending)
14400 int new_count = 0;
14402 FOR_EACH_FRAME (tail, frame)
14404 if (XFRAME (frame)->visible)
14405 new_count++;
14408 if (new_count != number_of_visible_frames)
14409 windows_or_buffers_changed = 52;
14412 /* Change frame size now if a change is pending. */
14413 do_pending_window_change (true);
14415 /* If we just did a pending size change, or have additional
14416 visible frames, or selected_window changed, redisplay again. */
14417 if ((windows_or_buffers_changed && !pending)
14418 || (WINDOWP (selected_window) && (w = XWINDOW (selected_window)) != sw))
14419 goto retry;
14421 /* Clear the face and image caches.
14423 We used to do this only if consider_all_windows_p. But the cache
14424 needs to be cleared if a timer creates images in the current
14425 buffer (e.g. the test case in Bug#6230). */
14427 if (clear_face_cache_count > CLEAR_FACE_CACHE_COUNT)
14429 clear_face_cache (false);
14430 clear_face_cache_count = 0;
14433 #ifdef HAVE_WINDOW_SYSTEM
14434 if (clear_image_cache_count > CLEAR_IMAGE_CACHE_COUNT)
14436 clear_image_caches (Qnil);
14437 clear_image_cache_count = 0;
14439 #endif /* HAVE_WINDOW_SYSTEM */
14441 end_of_redisplay:
14442 #ifdef HAVE_NS
14443 ns_set_doc_edited ();
14444 #endif
14445 if (interrupt_input && interrupts_deferred)
14446 request_sigio ();
14448 unbind_to (count, Qnil);
14449 RESUME_POLLING;
14452 static void
14453 unwind_redisplay_preserve_echo_area (void)
14455 unblock_buffer_flips ();
14458 /* Redisplay, but leave alone any recent echo area message unless
14459 another message has been requested in its place.
14461 This is useful in situations where you need to redisplay but no
14462 user action has occurred, making it inappropriate for the message
14463 area to be cleared. See tracking_off and
14464 wait_reading_process_output for examples of these situations.
14466 FROM_WHERE is an integer saying from where this function was
14467 called. This is useful for debugging. */
14469 void
14470 redisplay_preserve_echo_area (int from_where)
14472 TRACE ((stderr, "redisplay_preserve_echo_area (%d)\n", from_where));
14474 block_input ();
14475 ptrdiff_t count = SPECPDL_INDEX ();
14476 record_unwind_protect_void (unwind_redisplay_preserve_echo_area);
14477 block_buffer_flips ();
14478 unblock_input ();
14480 if (!NILP (echo_area_buffer[1]))
14482 /* We have a previously displayed message, but no current
14483 message. Redisplay the previous message. */
14484 display_last_displayed_message_p = true;
14485 redisplay_internal ();
14486 display_last_displayed_message_p = false;
14488 else
14489 redisplay_internal ();
14491 flush_frame (SELECTED_FRAME ());
14492 unbind_to (count, Qnil);
14496 /* Function registered with record_unwind_protect in redisplay_internal. */
14498 static void
14499 unwind_redisplay (void)
14501 redisplaying_p = false;
14502 unblock_buffer_flips ();
14506 /* Mark the display of leaf window W as accurate or inaccurate.
14507 If ACCURATE_P, mark display of W as accurate.
14508 If !ACCURATE_P, arrange for W to be redisplayed the next
14509 time redisplay_internal is called. */
14511 static void
14512 mark_window_display_accurate_1 (struct window *w, bool accurate_p)
14514 struct buffer *b = XBUFFER (w->contents);
14516 w->last_modified = accurate_p ? BUF_MODIFF (b) : 0;
14517 w->last_overlay_modified = accurate_p ? BUF_OVERLAY_MODIFF (b) : 0;
14518 w->last_had_star = BUF_MODIFF (b) > BUF_SAVE_MODIFF (b);
14520 if (accurate_p)
14522 b->clip_changed = false;
14523 b->prevent_redisplay_optimizations_p = false;
14524 eassert (buffer_window_count (b) > 0);
14525 /* Resetting b->text->redisplay is problematic!
14526 In order to make it safer to do it here, redisplay_internal must
14527 have copied all b->text->redisplay to their respective windows. */
14528 b->text->redisplay = false;
14530 BUF_UNCHANGED_MODIFIED (b) = BUF_MODIFF (b);
14531 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = BUF_OVERLAY_MODIFF (b);
14532 BUF_BEG_UNCHANGED (b) = BUF_GPT (b) - BUF_BEG (b);
14533 BUF_END_UNCHANGED (b) = BUF_Z (b) - BUF_GPT (b);
14535 w->current_matrix->buffer = b;
14536 w->current_matrix->begv = BUF_BEGV (b);
14537 w->current_matrix->zv = BUF_ZV (b);
14539 w->last_cursor_vpos = w->cursor.vpos;
14540 w->last_cursor_off_p = w->cursor_off_p;
14542 if (w == XWINDOW (selected_window))
14543 w->last_point = BUF_PT (b);
14544 else
14545 w->last_point = marker_position (w->pointm);
14547 w->window_end_valid = true;
14548 w->update_mode_line = false;
14551 w->redisplay = !accurate_p;
14555 /* Mark the display of windows in the window tree rooted at WINDOW as
14556 accurate or inaccurate. If ACCURATE_P, mark display of
14557 windows as accurate. If !ACCURATE_P, arrange for windows to
14558 be redisplayed the next time redisplay_internal is called. */
14560 void
14561 mark_window_display_accurate (Lisp_Object window, bool accurate_p)
14563 struct window *w;
14565 for (; !NILP (window); window = w->next)
14567 w = XWINDOW (window);
14568 if (WINDOWP (w->contents))
14569 mark_window_display_accurate (w->contents, accurate_p);
14570 else
14571 mark_window_display_accurate_1 (w, accurate_p);
14574 if (accurate_p)
14575 update_overlay_arrows (1);
14576 else
14577 /* Force a thorough redisplay the next time by setting
14578 last_arrow_position and last_arrow_string to t, which is
14579 unequal to any useful value of Voverlay_arrow_... */
14580 update_overlay_arrows (-1);
14584 /* Return value in display table DP (Lisp_Char_Table *) for character
14585 C. Since a display table doesn't have any parent, we don't have to
14586 follow parent. Do not call this function directly but use the
14587 macro DISP_CHAR_VECTOR. */
14589 Lisp_Object
14590 disp_char_vector (struct Lisp_Char_Table *dp, int c)
14592 Lisp_Object val;
14594 if (ASCII_CHAR_P (c))
14596 val = dp->ascii;
14597 if (SUB_CHAR_TABLE_P (val))
14598 val = XSUB_CHAR_TABLE (val)->contents[c];
14600 else
14602 Lisp_Object table;
14604 XSETCHAR_TABLE (table, dp);
14605 val = char_table_ref (table, c);
14607 if (NILP (val))
14608 val = dp->defalt;
14609 return val;
14612 static int buffer_flip_blocked_depth;
14614 static void
14615 block_buffer_flips (void)
14617 eassert (buffer_flip_blocked_depth >= 0);
14618 buffer_flip_blocked_depth++;
14621 static void
14622 unblock_buffer_flips (void)
14624 eassert (buffer_flip_blocked_depth > 0);
14625 if (--buffer_flip_blocked_depth == 0)
14627 Lisp_Object tail, frame;
14628 block_input ();
14629 FOR_EACH_FRAME (tail, frame)
14631 struct frame *f = XFRAME (frame);
14632 if (FRAME_TERMINAL (f)->buffer_flipping_unblocked_hook)
14633 (*FRAME_TERMINAL (f)->buffer_flipping_unblocked_hook) (f);
14635 unblock_input ();
14639 bool
14640 buffer_flipping_blocked_p (void)
14642 return buffer_flip_blocked_depth > 0;
14646 /***********************************************************************
14647 Window Redisplay
14648 ***********************************************************************/
14650 /* Redisplay all leaf windows in the window tree rooted at WINDOW. */
14652 static void
14653 redisplay_windows (Lisp_Object window)
14655 while (!NILP (window))
14657 struct window *w = XWINDOW (window);
14659 if (WINDOWP (w->contents))
14660 redisplay_windows (w->contents);
14661 else if (BUFFERP (w->contents))
14663 displayed_buffer = XBUFFER (w->contents);
14664 /* Use list_of_error, not Qerror, so that
14665 we catch only errors and don't run the debugger. */
14666 internal_condition_case_1 (redisplay_window_0, window,
14667 list_of_error,
14668 redisplay_window_error);
14671 window = w->next;
14675 static Lisp_Object
14676 redisplay_window_error (Lisp_Object ignore)
14678 displayed_buffer->display_error_modiff = BUF_MODIFF (displayed_buffer);
14679 return Qnil;
14682 static Lisp_Object
14683 redisplay_window_0 (Lisp_Object window)
14685 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
14686 redisplay_window (window, false);
14687 return Qnil;
14690 static Lisp_Object
14691 redisplay_window_1 (Lisp_Object window)
14693 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
14694 redisplay_window (window, true);
14695 return Qnil;
14699 /* Set cursor position of W. PT is assumed to be displayed in ROW.
14700 DELTA and DELTA_BYTES are the numbers of characters and bytes by
14701 which positions recorded in ROW differ from current buffer
14702 positions.
14704 Return true iff cursor is on this row. */
14706 static bool
14707 set_cursor_from_row (struct window *w, struct glyph_row *row,
14708 struct glyph_matrix *matrix,
14709 ptrdiff_t delta, ptrdiff_t delta_bytes,
14710 int dy, int dvpos)
14712 struct glyph *glyph = row->glyphs[TEXT_AREA];
14713 struct glyph *end = glyph + row->used[TEXT_AREA];
14714 struct glyph *cursor = NULL;
14715 /* The last known character position in row. */
14716 ptrdiff_t last_pos = MATRIX_ROW_START_CHARPOS (row) + delta;
14717 int x = row->x;
14718 ptrdiff_t pt_old = PT - delta;
14719 ptrdiff_t pos_before = MATRIX_ROW_START_CHARPOS (row) + delta;
14720 ptrdiff_t pos_after = MATRIX_ROW_END_CHARPOS (row) + delta;
14721 struct glyph *glyph_before = glyph - 1, *glyph_after = end;
14722 /* A glyph beyond the edge of TEXT_AREA which we should never
14723 touch. */
14724 struct glyph *glyphs_end = end;
14725 /* True means we've found a match for cursor position, but that
14726 glyph has the avoid_cursor_p flag set. */
14727 bool match_with_avoid_cursor = false;
14728 /* True means we've seen at least one glyph that came from a
14729 display string. */
14730 bool string_seen = false;
14731 /* Largest and smallest buffer positions seen so far during scan of
14732 glyph row. */
14733 ptrdiff_t bpos_max = pos_before;
14734 ptrdiff_t bpos_min = pos_after;
14735 /* Last buffer position covered by an overlay string with an integer
14736 `cursor' property. */
14737 ptrdiff_t bpos_covered = 0;
14738 /* True means the display string on which to display the cursor
14739 comes from a text property, not from an overlay. */
14740 bool string_from_text_prop = false;
14742 /* Don't even try doing anything if called for a mode-line or
14743 header-line row, since the rest of the code isn't prepared to
14744 deal with such calamities. */
14745 eassert (!row->mode_line_p);
14746 if (row->mode_line_p)
14747 return false;
14749 /* Skip over glyphs not having an object at the start and the end of
14750 the row. These are special glyphs like truncation marks on
14751 terminal frames. */
14752 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
14754 if (!row->reversed_p)
14756 while (glyph < end
14757 && NILP (glyph->object)
14758 && glyph->charpos < 0)
14760 x += glyph->pixel_width;
14761 ++glyph;
14763 while (end > glyph
14764 && NILP ((end - 1)->object)
14765 /* CHARPOS is zero for blanks and stretch glyphs
14766 inserted by extend_face_to_end_of_line. */
14767 && (end - 1)->charpos <= 0)
14768 --end;
14769 glyph_before = glyph - 1;
14770 glyph_after = end;
14772 else
14774 struct glyph *g;
14776 /* If the glyph row is reversed, we need to process it from back
14777 to front, so swap the edge pointers. */
14778 glyphs_end = end = glyph - 1;
14779 glyph += row->used[TEXT_AREA] - 1;
14781 while (glyph > end + 1
14782 && NILP (glyph->object)
14783 && glyph->charpos < 0)
14785 --glyph;
14786 x -= glyph->pixel_width;
14788 if (NILP (glyph->object) && glyph->charpos < 0)
14789 --glyph;
14790 /* By default, in reversed rows we put the cursor on the
14791 rightmost (first in the reading order) glyph. */
14792 for (g = end + 1; g < glyph; g++)
14793 x += g->pixel_width;
14794 while (end < glyph
14795 && NILP ((end + 1)->object)
14796 && (end + 1)->charpos <= 0)
14797 ++end;
14798 glyph_before = glyph + 1;
14799 glyph_after = end;
14802 else if (row->reversed_p)
14804 /* In R2L rows that don't display text, put the cursor on the
14805 rightmost glyph. Case in point: an empty last line that is
14806 part of an R2L paragraph. */
14807 cursor = end - 1;
14808 /* Avoid placing the cursor on the last glyph of the row, where
14809 on terminal frames we hold the vertical border between
14810 adjacent windows. */
14811 if (!FRAME_WINDOW_P (WINDOW_XFRAME (w))
14812 && !WINDOW_RIGHTMOST_P (w)
14813 && cursor == row->glyphs[LAST_AREA] - 1)
14814 cursor--;
14815 x = -1; /* will be computed below, at label compute_x */
14818 /* Step 1: Try to find the glyph whose character position
14819 corresponds to point. If that's not possible, find 2 glyphs
14820 whose character positions are the closest to point, one before
14821 point, the other after it. */
14822 if (!row->reversed_p)
14823 while (/* not marched to end of glyph row */
14824 glyph < end
14825 /* glyph was not inserted by redisplay for internal purposes */
14826 && !NILP (glyph->object))
14828 if (BUFFERP (glyph->object))
14830 ptrdiff_t dpos = glyph->charpos - pt_old;
14832 if (glyph->charpos > bpos_max)
14833 bpos_max = glyph->charpos;
14834 if (glyph->charpos < bpos_min)
14835 bpos_min = glyph->charpos;
14836 if (!glyph->avoid_cursor_p)
14838 /* If we hit point, we've found the glyph on which to
14839 display the cursor. */
14840 if (dpos == 0)
14842 match_with_avoid_cursor = false;
14843 break;
14845 /* See if we've found a better approximation to
14846 POS_BEFORE or to POS_AFTER. */
14847 if (0 > dpos && dpos > pos_before - pt_old)
14849 pos_before = glyph->charpos;
14850 glyph_before = glyph;
14852 else if (0 < dpos && dpos < pos_after - pt_old)
14854 pos_after = glyph->charpos;
14855 glyph_after = glyph;
14858 else if (dpos == 0)
14859 match_with_avoid_cursor = true;
14861 else if (STRINGP (glyph->object))
14863 Lisp_Object chprop;
14864 ptrdiff_t glyph_pos = glyph->charpos;
14866 chprop = Fget_char_property (make_number (glyph_pos), Qcursor,
14867 glyph->object);
14868 if (!NILP (chprop))
14870 /* If the string came from a `display' text property,
14871 look up the buffer position of that property and
14872 use that position to update bpos_max, as if we
14873 actually saw such a position in one of the row's
14874 glyphs. This helps with supporting integer values
14875 of `cursor' property on the display string in
14876 situations where most or all of the row's buffer
14877 text is completely covered by display properties,
14878 so that no glyph with valid buffer positions is
14879 ever seen in the row. */
14880 ptrdiff_t prop_pos =
14881 string_buffer_position_lim (glyph->object, pos_before,
14882 pos_after, false);
14884 if (prop_pos >= pos_before)
14885 bpos_max = prop_pos;
14887 if (INTEGERP (chprop))
14889 bpos_covered = bpos_max + XINT (chprop);
14890 /* If the `cursor' property covers buffer positions up
14891 to and including point, we should display cursor on
14892 this glyph. Note that, if a `cursor' property on one
14893 of the string's characters has an integer value, we
14894 will break out of the loop below _before_ we get to
14895 the position match above. IOW, integer values of
14896 the `cursor' property override the "exact match for
14897 point" strategy of positioning the cursor. */
14898 /* Implementation note: bpos_max == pt_old when, e.g.,
14899 we are in an empty line, where bpos_max is set to
14900 MATRIX_ROW_START_CHARPOS, see above. */
14901 if (bpos_max <= pt_old && bpos_covered >= pt_old)
14903 cursor = glyph;
14904 break;
14908 string_seen = true;
14910 x += glyph->pixel_width;
14911 ++glyph;
14913 else if (glyph > end) /* row is reversed */
14914 while (!NILP (glyph->object))
14916 if (BUFFERP (glyph->object))
14918 ptrdiff_t dpos = glyph->charpos - pt_old;
14920 if (glyph->charpos > bpos_max)
14921 bpos_max = glyph->charpos;
14922 if (glyph->charpos < bpos_min)
14923 bpos_min = glyph->charpos;
14924 if (!glyph->avoid_cursor_p)
14926 if (dpos == 0)
14928 match_with_avoid_cursor = false;
14929 break;
14931 if (0 > dpos && dpos > pos_before - pt_old)
14933 pos_before = glyph->charpos;
14934 glyph_before = glyph;
14936 else if (0 < dpos && dpos < pos_after - pt_old)
14938 pos_after = glyph->charpos;
14939 glyph_after = glyph;
14942 else if (dpos == 0)
14943 match_with_avoid_cursor = true;
14945 else if (STRINGP (glyph->object))
14947 Lisp_Object chprop;
14948 ptrdiff_t glyph_pos = glyph->charpos;
14950 chprop = Fget_char_property (make_number (glyph_pos), Qcursor,
14951 glyph->object);
14952 if (!NILP (chprop))
14954 ptrdiff_t prop_pos =
14955 string_buffer_position_lim (glyph->object, pos_before,
14956 pos_after, false);
14958 if (prop_pos >= pos_before)
14959 bpos_max = prop_pos;
14961 if (INTEGERP (chprop))
14963 bpos_covered = bpos_max + XINT (chprop);
14964 /* If the `cursor' property covers buffer positions up
14965 to and including point, we should display cursor on
14966 this glyph. */
14967 if (bpos_max <= pt_old && bpos_covered >= pt_old)
14969 cursor = glyph;
14970 break;
14973 string_seen = true;
14975 --glyph;
14976 if (glyph == glyphs_end) /* don't dereference outside TEXT_AREA */
14978 x--; /* can't use any pixel_width */
14979 break;
14981 x -= glyph->pixel_width;
14984 /* Step 2: If we didn't find an exact match for point, we need to
14985 look for a proper place to put the cursor among glyphs between
14986 GLYPH_BEFORE and GLYPH_AFTER. */
14987 if (!((row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end)
14988 && BUFFERP (glyph->object) && glyph->charpos == pt_old)
14989 && !(bpos_max <= pt_old && pt_old <= bpos_covered))
14991 /* An empty line has a single glyph whose OBJECT is nil and
14992 whose CHARPOS is the position of a newline on that line.
14993 Note that on a TTY, there are more glyphs after that, which
14994 were produced by extend_face_to_end_of_line, but their
14995 CHARPOS is zero or negative. */
14996 bool empty_line_p =
14997 ((row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end)
14998 && NILP (glyph->object) && glyph->charpos > 0
14999 /* On a TTY, continued and truncated rows also have a glyph at
15000 their end whose OBJECT is nil and whose CHARPOS is
15001 positive (the continuation and truncation glyphs), but such
15002 rows are obviously not "empty". */
15003 && !(row->continued_p || row->truncated_on_right_p));
15005 if (row->ends_in_ellipsis_p && pos_after == last_pos)
15007 ptrdiff_t ellipsis_pos;
15009 /* Scan back over the ellipsis glyphs. */
15010 if (!row->reversed_p)
15012 ellipsis_pos = (glyph - 1)->charpos;
15013 while (glyph > row->glyphs[TEXT_AREA]
15014 && (glyph - 1)->charpos == ellipsis_pos)
15015 glyph--, x -= glyph->pixel_width;
15016 /* That loop always goes one position too far, including
15017 the glyph before the ellipsis. So scan forward over
15018 that one. */
15019 x += glyph->pixel_width;
15020 glyph++;
15022 else /* row is reversed */
15024 ellipsis_pos = (glyph + 1)->charpos;
15025 while (glyph < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
15026 && (glyph + 1)->charpos == ellipsis_pos)
15027 glyph++, x += glyph->pixel_width;
15028 x -= glyph->pixel_width;
15029 glyph--;
15032 else if (match_with_avoid_cursor)
15034 cursor = glyph_after;
15035 x = -1;
15037 else if (string_seen)
15039 int incr = row->reversed_p ? -1 : +1;
15041 /* Need to find the glyph that came out of a string which is
15042 present at point. That glyph is somewhere between
15043 GLYPH_BEFORE and GLYPH_AFTER, and it came from a string
15044 positioned between POS_BEFORE and POS_AFTER in the
15045 buffer. */
15046 struct glyph *start, *stop;
15047 ptrdiff_t pos = pos_before;
15049 x = -1;
15051 /* If the row ends in a newline from a display string,
15052 reordering could have moved the glyphs belonging to the
15053 string out of the [GLYPH_BEFORE..GLYPH_AFTER] range. So
15054 in this case we extend the search to the last glyph in
15055 the row that was not inserted by redisplay. */
15056 if (row->ends_in_newline_from_string_p)
15058 glyph_after = end;
15059 pos_after = MATRIX_ROW_END_CHARPOS (row) + delta;
15062 /* GLYPH_BEFORE and GLYPH_AFTER are the glyphs that
15063 correspond to POS_BEFORE and POS_AFTER, respectively. We
15064 need START and STOP in the order that corresponds to the
15065 row's direction as given by its reversed_p flag. If the
15066 directionality of characters between POS_BEFORE and
15067 POS_AFTER is the opposite of the row's base direction,
15068 these characters will have been reordered for display,
15069 and we need to reverse START and STOP. */
15070 if (!row->reversed_p)
15072 start = min (glyph_before, glyph_after);
15073 stop = max (glyph_before, glyph_after);
15075 else
15077 start = max (glyph_before, glyph_after);
15078 stop = min (glyph_before, glyph_after);
15080 for (glyph = start + incr;
15081 row->reversed_p ? glyph > stop : glyph < stop; )
15084 /* Any glyphs that come from the buffer are here because
15085 of bidi reordering. Skip them, and only pay
15086 attention to glyphs that came from some string. */
15087 if (STRINGP (glyph->object))
15089 Lisp_Object str;
15090 ptrdiff_t tem;
15091 /* If the display property covers the newline, we
15092 need to search for it one position farther. */
15093 ptrdiff_t lim = pos_after
15094 + (pos_after == MATRIX_ROW_END_CHARPOS (row) + delta);
15096 string_from_text_prop = false;
15097 str = glyph->object;
15098 tem = string_buffer_position_lim (str, pos, lim, false);
15099 if (tem == 0 /* from overlay */
15100 || pos <= tem)
15102 /* If the string from which this glyph came is
15103 found in the buffer at point, or at position
15104 that is closer to point than pos_after, then
15105 we've found the glyph we've been looking for.
15106 If it comes from an overlay (tem == 0), and
15107 it has the `cursor' property on one of its
15108 glyphs, record that glyph as a candidate for
15109 displaying the cursor. (As in the
15110 unidirectional version, we will display the
15111 cursor on the last candidate we find.) */
15112 if (tem == 0
15113 || tem == pt_old
15114 || (tem - pt_old > 0 && tem < pos_after))
15116 /* The glyphs from this string could have
15117 been reordered. Find the one with the
15118 smallest string position. Or there could
15119 be a character in the string with the
15120 `cursor' property, which means display
15121 cursor on that character's glyph. */
15122 ptrdiff_t strpos = glyph->charpos;
15124 if (tem)
15126 cursor = glyph;
15127 string_from_text_prop = true;
15129 for ( ;
15130 (row->reversed_p ? glyph > stop : glyph < stop)
15131 && EQ (glyph->object, str);
15132 glyph += incr)
15134 Lisp_Object cprop;
15135 ptrdiff_t gpos = glyph->charpos;
15137 cprop = Fget_char_property (make_number (gpos),
15138 Qcursor,
15139 glyph->object);
15140 if (!NILP (cprop))
15142 cursor = glyph;
15143 break;
15145 if (tem && glyph->charpos < strpos)
15147 strpos = glyph->charpos;
15148 cursor = glyph;
15152 if (tem == pt_old
15153 || (tem - pt_old > 0 && tem < pos_after))
15154 goto compute_x;
15156 if (tem)
15157 pos = tem + 1; /* don't find previous instances */
15159 /* This string is not what we want; skip all of the
15160 glyphs that came from it. */
15161 while ((row->reversed_p ? glyph > stop : glyph < stop)
15162 && EQ (glyph->object, str))
15163 glyph += incr;
15165 else
15166 glyph += incr;
15169 /* If we reached the end of the line, and END was from a string,
15170 the cursor is not on this line. */
15171 if (cursor == NULL
15172 && (row->reversed_p ? glyph <= end : glyph >= end)
15173 && (row->reversed_p ? end > glyphs_end : end < glyphs_end)
15174 && STRINGP (end->object)
15175 && row->continued_p)
15176 return false;
15178 /* A truncated row may not include PT among its character positions.
15179 Setting the cursor inside the scroll margin will trigger
15180 recalculation of hscroll in hscroll_window_tree. But if a
15181 display string covers point, defer to the string-handling
15182 code below to figure this out. */
15183 else if (row->truncated_on_left_p && pt_old < bpos_min)
15185 cursor = glyph_before;
15186 x = -1;
15188 else if ((row->truncated_on_right_p && pt_old > bpos_max)
15189 /* Zero-width characters produce no glyphs. */
15190 || (!empty_line_p
15191 && (row->reversed_p
15192 ? glyph_after > glyphs_end
15193 : glyph_after < glyphs_end)))
15195 cursor = glyph_after;
15196 x = -1;
15200 compute_x:
15201 if (cursor != NULL)
15202 glyph = cursor;
15203 else if (glyph == glyphs_end
15204 && pos_before == pos_after
15205 && STRINGP ((row->reversed_p
15206 ? row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
15207 : row->glyphs[TEXT_AREA])->object))
15209 /* If all the glyphs of this row came from strings, put the
15210 cursor on the first glyph of the row. This avoids having the
15211 cursor outside of the text area in this very rare and hard
15212 use case. */
15213 glyph =
15214 row->reversed_p
15215 ? row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
15216 : row->glyphs[TEXT_AREA];
15218 if (x < 0)
15220 struct glyph *g;
15222 /* Need to compute x that corresponds to GLYPH. */
15223 for (g = row->glyphs[TEXT_AREA], x = row->x; g < glyph; g++)
15225 if (g >= row->glyphs[TEXT_AREA] + row->used[TEXT_AREA])
15226 emacs_abort ();
15227 x += g->pixel_width;
15231 /* ROW could be part of a continued line, which, under bidi
15232 reordering, might have other rows whose start and end charpos
15233 occlude point. Only set w->cursor if we found a better
15234 approximation to the cursor position than we have from previously
15235 examined candidate rows belonging to the same continued line. */
15236 if (/* We already have a candidate row. */
15237 w->cursor.vpos >= 0
15238 /* That candidate is not the row we are processing. */
15239 && MATRIX_ROW (matrix, w->cursor.vpos) != row
15240 /* Make sure cursor.vpos specifies a row whose start and end
15241 charpos occlude point, and it is valid candidate for being a
15242 cursor-row. This is because some callers of this function
15243 leave cursor.vpos at the row where the cursor was displayed
15244 during the last redisplay cycle. */
15245 && MATRIX_ROW_START_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos)) <= pt_old
15246 && pt_old <= MATRIX_ROW_END_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
15247 && cursor_row_p (MATRIX_ROW (matrix, w->cursor.vpos)))
15249 struct glyph *g1
15250 = MATRIX_ROW_GLYPH_START (matrix, w->cursor.vpos) + w->cursor.hpos;
15252 /* Don't consider glyphs that are outside TEXT_AREA. */
15253 if (!(row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end))
15254 return false;
15255 /* Keep the candidate whose buffer position is the closest to
15256 point or has the `cursor' property. */
15257 if (/* Previous candidate is a glyph in TEXT_AREA of that row. */
15258 w->cursor.hpos >= 0
15259 && w->cursor.hpos < MATRIX_ROW_USED (matrix, w->cursor.vpos)
15260 && ((BUFFERP (g1->object)
15261 && (g1->charpos == pt_old /* An exact match always wins. */
15262 || (BUFFERP (glyph->object)
15263 && eabs (g1->charpos - pt_old)
15264 < eabs (glyph->charpos - pt_old))))
15265 /* Previous candidate is a glyph from a string that has
15266 a non-nil `cursor' property. */
15267 || (STRINGP (g1->object)
15268 && (!NILP (Fget_char_property (make_number (g1->charpos),
15269 Qcursor, g1->object))
15270 /* Previous candidate is from the same display
15271 string as this one, and the display string
15272 came from a text property. */
15273 || (EQ (g1->object, glyph->object)
15274 && string_from_text_prop)
15275 /* this candidate is from newline and its
15276 position is not an exact match */
15277 || (NILP (glyph->object)
15278 && glyph->charpos != pt_old)))))
15279 return false;
15280 /* If this candidate gives an exact match, use that. */
15281 if (!((BUFFERP (glyph->object) && glyph->charpos == pt_old)
15282 /* If this candidate is a glyph created for the
15283 terminating newline of a line, and point is on that
15284 newline, it wins because it's an exact match. */
15285 || (!row->continued_p
15286 && NILP (glyph->object)
15287 && glyph->charpos == 0
15288 && pt_old == MATRIX_ROW_END_CHARPOS (row) - 1))
15289 /* Otherwise, keep the candidate that comes from a row
15290 spanning less buffer positions. This may win when one or
15291 both candidate positions are on glyphs that came from
15292 display strings, for which we cannot compare buffer
15293 positions. */
15294 && MATRIX_ROW_END_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
15295 - MATRIX_ROW_START_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
15296 < MATRIX_ROW_END_CHARPOS (row) - MATRIX_ROW_START_CHARPOS (row))
15297 return false;
15299 w->cursor.hpos = glyph - row->glyphs[TEXT_AREA];
15300 w->cursor.x = x;
15301 w->cursor.vpos = MATRIX_ROW_VPOS (row, matrix) + dvpos;
15302 w->cursor.y = row->y + dy;
15304 if (w == XWINDOW (selected_window))
15306 if (!row->continued_p
15307 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
15308 && row->x == 0)
15310 this_line_buffer = XBUFFER (w->contents);
15312 CHARPOS (this_line_start_pos)
15313 = MATRIX_ROW_START_CHARPOS (row) + delta;
15314 BYTEPOS (this_line_start_pos)
15315 = MATRIX_ROW_START_BYTEPOS (row) + delta_bytes;
15317 CHARPOS (this_line_end_pos)
15318 = Z - (MATRIX_ROW_END_CHARPOS (row) + delta);
15319 BYTEPOS (this_line_end_pos)
15320 = Z_BYTE - (MATRIX_ROW_END_BYTEPOS (row) + delta_bytes);
15322 this_line_y = w->cursor.y;
15323 this_line_pixel_height = row->height;
15324 this_line_vpos = w->cursor.vpos;
15325 this_line_start_x = row->x;
15327 else
15328 CHARPOS (this_line_start_pos) = 0;
15331 return true;
15335 /* Run window scroll functions, if any, for WINDOW with new window
15336 start STARTP. Sets the window start of WINDOW to that position.
15338 We assume that the window's buffer is really current. */
15340 static struct text_pos
15341 run_window_scroll_functions (Lisp_Object window, struct text_pos startp)
15343 struct window *w = XWINDOW (window);
15344 SET_MARKER_FROM_TEXT_POS (w->start, startp);
15346 eassert (current_buffer == XBUFFER (w->contents));
15348 if (!NILP (Vwindow_scroll_functions))
15350 run_hook_with_args_2 (Qwindow_scroll_functions, window,
15351 make_number (CHARPOS (startp)));
15352 SET_TEXT_POS_FROM_MARKER (startp, w->start);
15353 /* In case the hook functions switch buffers. */
15354 set_buffer_internal (XBUFFER (w->contents));
15357 return startp;
15361 /* Make sure the line containing the cursor is fully visible.
15362 A value of true means there is nothing to be done.
15363 (Either the line is fully visible, or it cannot be made so,
15364 or we cannot tell.)
15366 If FORCE_P, return false even if partial visible cursor row
15367 is higher than window.
15369 If CURRENT_MATRIX_P, use the information from the
15370 window's current glyph matrix; otherwise use the desired glyph
15371 matrix.
15373 A value of false means the caller should do scrolling
15374 as if point had gone off the screen. */
15376 static bool
15377 cursor_row_fully_visible_p (struct window *w, bool force_p,
15378 bool current_matrix_p)
15380 struct glyph_matrix *matrix;
15381 struct glyph_row *row;
15382 int window_height;
15384 if (!make_cursor_line_fully_visible_p)
15385 return true;
15387 /* It's not always possible to find the cursor, e.g, when a window
15388 is full of overlay strings. Don't do anything in that case. */
15389 if (w->cursor.vpos < 0)
15390 return true;
15392 matrix = current_matrix_p ? w->current_matrix : w->desired_matrix;
15393 row = MATRIX_ROW (matrix, w->cursor.vpos);
15395 /* If the cursor row is not partially visible, there's nothing to do. */
15396 if (!MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row))
15397 return true;
15399 /* If the row the cursor is in is taller than the window's height,
15400 it's not clear what to do, so do nothing. */
15401 window_height = window_box_height (w);
15402 if (row->height >= window_height)
15404 if (!force_p || MINI_WINDOW_P (w)
15405 || w->vscroll || w->cursor.vpos == 0)
15406 return true;
15408 return false;
15412 /* Try scrolling PT into view in window WINDOW. JUST_THIS_ONE_P
15413 means only WINDOW is redisplayed in redisplay_internal.
15414 TEMP_SCROLL_STEP has the same meaning as emacs_scroll_step, and is used
15415 in redisplay_window to bring a partially visible line into view in
15416 the case that only the cursor has moved.
15418 LAST_LINE_MISFIT should be true if we're scrolling because the
15419 last screen line's vertical height extends past the end of the screen.
15421 Value is
15423 1 if scrolling succeeded
15425 0 if scrolling didn't find point.
15427 -1 if new fonts have been loaded so that we must interrupt
15428 redisplay, adjust glyph matrices, and try again. */
15430 enum
15432 SCROLLING_SUCCESS,
15433 SCROLLING_FAILED,
15434 SCROLLING_NEED_LARGER_MATRICES
15437 /* If scroll-conservatively is more than this, never recenter.
15439 If you change this, don't forget to update the doc string of
15440 `scroll-conservatively' and the Emacs manual. */
15441 #define SCROLL_LIMIT 100
15443 static int
15444 try_scrolling (Lisp_Object window, bool just_this_one_p,
15445 ptrdiff_t arg_scroll_conservatively, ptrdiff_t scroll_step,
15446 bool temp_scroll_step, bool last_line_misfit)
15448 struct window *w = XWINDOW (window);
15449 struct text_pos pos, startp;
15450 struct it it;
15451 int this_scroll_margin, scroll_max, rc, height;
15452 int dy = 0, amount_to_scroll = 0;
15453 bool scroll_down_p = false;
15454 int extra_scroll_margin_lines = last_line_misfit;
15455 Lisp_Object aggressive;
15456 /* We will never try scrolling more than this number of lines. */
15457 int scroll_limit = SCROLL_LIMIT;
15458 int frame_line_height = default_line_pixel_height (w);
15460 #ifdef GLYPH_DEBUG
15461 debug_method_add (w, "try_scrolling");
15462 #endif
15464 SET_TEXT_POS_FROM_MARKER (startp, w->start);
15466 this_scroll_margin = window_scroll_margin (w, MARGIN_IN_PIXELS);
15468 /* Force arg_scroll_conservatively to have a reasonable value, to
15469 avoid scrolling too far away with slow move_it_* functions. Note
15470 that the user can supply scroll-conservatively equal to
15471 `most-positive-fixnum', which can be larger than INT_MAX. */
15472 if (arg_scroll_conservatively > scroll_limit)
15474 arg_scroll_conservatively = scroll_limit + 1;
15475 scroll_max = scroll_limit * frame_line_height;
15477 else if (scroll_step || arg_scroll_conservatively || temp_scroll_step)
15478 /* Compute how much we should try to scroll maximally to bring
15479 point into view. */
15480 scroll_max = (max (scroll_step,
15481 max (arg_scroll_conservatively, temp_scroll_step))
15482 * frame_line_height);
15483 else if (NUMBERP (BVAR (current_buffer, scroll_down_aggressively))
15484 || NUMBERP (BVAR (current_buffer, scroll_up_aggressively)))
15485 /* We're trying to scroll because of aggressive scrolling but no
15486 scroll_step is set. Choose an arbitrary one. */
15487 scroll_max = 10 * frame_line_height;
15488 else
15489 scroll_max = 0;
15491 too_near_end:
15493 /* Decide whether to scroll down. */
15494 if (PT > CHARPOS (startp))
15496 int scroll_margin_y;
15498 /* Compute the pixel ypos of the scroll margin, then move IT to
15499 either that ypos or PT, whichever comes first. */
15500 start_display (&it, w, startp);
15501 scroll_margin_y = it.last_visible_y - partial_line_height (&it)
15502 - this_scroll_margin
15503 - frame_line_height * extra_scroll_margin_lines;
15504 move_it_to (&it, PT, -1, scroll_margin_y - 1, -1,
15505 (MOVE_TO_POS | MOVE_TO_Y));
15507 if (PT > CHARPOS (it.current.pos))
15509 int y0 = line_bottom_y (&it);
15510 /* Compute how many pixels below window bottom to stop searching
15511 for PT. This avoids costly search for PT that is far away if
15512 the user limited scrolling by a small number of lines, but
15513 always finds PT if scroll_conservatively is set to a large
15514 number, such as most-positive-fixnum. */
15515 int slack = max (scroll_max, 10 * frame_line_height);
15516 int y_to_move = it.last_visible_y + slack;
15518 /* Compute the distance from the scroll margin to PT or to
15519 the scroll limit, whichever comes first. This should
15520 include the height of the cursor line, to make that line
15521 fully visible. */
15522 move_it_to (&it, PT, -1, y_to_move,
15523 -1, MOVE_TO_POS | MOVE_TO_Y);
15524 dy = line_bottom_y (&it) - y0;
15526 if (dy > scroll_max)
15527 return SCROLLING_FAILED;
15529 if (dy > 0)
15530 scroll_down_p = true;
15532 else if (PT == IT_CHARPOS (it)
15533 && IT_CHARPOS (it) < ZV
15534 && it.method == GET_FROM_STRING
15535 && arg_scroll_conservatively > scroll_limit
15536 && it.current_x == 0)
15538 enum move_it_result skip;
15539 int y1 = it.current_y;
15540 int vpos;
15542 /* A before-string that includes newlines and is displayed
15543 on the last visible screen line could fail us under
15544 scroll-conservatively > 100, because we will be unable to
15545 position the cursor on that last visible line. Try to
15546 recover by finding the first screen line that has some
15547 glyphs coming from the buffer text. */
15548 do {
15549 skip = move_it_in_display_line_to (&it, ZV, -1, MOVE_TO_POS);
15550 if (skip != MOVE_NEWLINE_OR_CR
15551 || IT_CHARPOS (it) != PT
15552 || it.method == GET_FROM_BUFFER)
15553 break;
15554 vpos = it.vpos;
15555 move_it_to (&it, -1, -1, -1, vpos + 1, MOVE_TO_VPOS);
15556 } while (it.vpos > vpos);
15558 dy = it.current_y - y1;
15560 if (dy > scroll_max)
15561 return SCROLLING_FAILED;
15563 if (dy > 0)
15564 scroll_down_p = true;
15568 if (scroll_down_p)
15570 /* Point is in or below the bottom scroll margin, so move the
15571 window start down. If scrolling conservatively, move it just
15572 enough down to make point visible. If scroll_step is set,
15573 move it down by scroll_step. */
15574 if (arg_scroll_conservatively)
15575 amount_to_scroll
15576 = min (max (dy, frame_line_height),
15577 frame_line_height * arg_scroll_conservatively);
15578 else if (scroll_step || temp_scroll_step)
15579 amount_to_scroll = scroll_max;
15580 else
15582 aggressive = BVAR (current_buffer, scroll_up_aggressively);
15583 height = WINDOW_BOX_TEXT_HEIGHT (w);
15584 if (NUMBERP (aggressive))
15586 double float_amount = XFLOATINT (aggressive) * height;
15587 int aggressive_scroll = float_amount;
15588 if (aggressive_scroll == 0 && float_amount > 0)
15589 aggressive_scroll = 1;
15590 /* Don't let point enter the scroll margin near top of
15591 the window. This could happen if the value of
15592 scroll_up_aggressively is too large and there are
15593 non-zero margins, because scroll_up_aggressively
15594 means put point that fraction of window height
15595 _from_the_bottom_margin_. */
15596 if (aggressive_scroll + 2 * this_scroll_margin > height)
15597 aggressive_scroll = height - 2 * this_scroll_margin;
15598 amount_to_scroll = dy + aggressive_scroll;
15602 if (amount_to_scroll <= 0)
15603 return SCROLLING_FAILED;
15605 start_display (&it, w, startp);
15606 if (arg_scroll_conservatively <= scroll_limit)
15607 move_it_vertically (&it, amount_to_scroll);
15608 else
15610 /* Extra precision for users who set scroll-conservatively
15611 to a large number: make sure the amount we scroll
15612 the window start is never less than amount_to_scroll,
15613 which was computed as distance from window bottom to
15614 point. This matters when lines at window top and lines
15615 below window bottom have different height. */
15616 struct it it1;
15617 void *it1data = NULL;
15618 /* We use a temporary it1 because line_bottom_y can modify
15619 its argument, if it moves one line down; see there. */
15620 int start_y;
15622 SAVE_IT (it1, it, it1data);
15623 start_y = line_bottom_y (&it1);
15624 do {
15625 RESTORE_IT (&it, &it, it1data);
15626 move_it_by_lines (&it, 1);
15627 SAVE_IT (it1, it, it1data);
15628 } while (IT_CHARPOS (it) < ZV
15629 && line_bottom_y (&it1) - start_y < amount_to_scroll);
15630 bidi_unshelve_cache (it1data, true);
15633 /* If STARTP is unchanged, move it down another screen line. */
15634 if (IT_CHARPOS (it) == CHARPOS (startp))
15635 move_it_by_lines (&it, 1);
15636 startp = it.current.pos;
15638 else
15640 struct text_pos scroll_margin_pos = startp;
15641 int y_offset = 0;
15643 /* See if point is inside the scroll margin at the top of the
15644 window. */
15645 if (this_scroll_margin)
15647 int y_start;
15649 start_display (&it, w, startp);
15650 y_start = it.current_y;
15651 move_it_vertically (&it, this_scroll_margin);
15652 scroll_margin_pos = it.current.pos;
15653 /* If we didn't move enough before hitting ZV, request
15654 additional amount of scroll, to move point out of the
15655 scroll margin. */
15656 if (IT_CHARPOS (it) == ZV
15657 && it.current_y - y_start < this_scroll_margin)
15658 y_offset = this_scroll_margin - (it.current_y - y_start);
15661 if (PT < CHARPOS (scroll_margin_pos))
15663 /* Point is in the scroll margin at the top of the window or
15664 above what is displayed in the window. */
15665 int y0, y_to_move;
15667 /* Compute the vertical distance from PT to the scroll
15668 margin position. Move as far as scroll_max allows, or
15669 one screenful, or 10 screen lines, whichever is largest.
15670 Give up if distance is greater than scroll_max or if we
15671 didn't reach the scroll margin position. */
15672 SET_TEXT_POS (pos, PT, PT_BYTE);
15673 start_display (&it, w, pos);
15674 y0 = it.current_y;
15675 y_to_move = max (it.last_visible_y,
15676 max (scroll_max, 10 * frame_line_height));
15677 move_it_to (&it, CHARPOS (scroll_margin_pos), 0,
15678 y_to_move, -1,
15679 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
15680 dy = it.current_y - y0;
15681 if (dy > scroll_max
15682 || IT_CHARPOS (it) < CHARPOS (scroll_margin_pos))
15683 return SCROLLING_FAILED;
15685 /* Additional scroll for when ZV was too close to point. */
15686 dy += y_offset;
15688 /* Compute new window start. */
15689 start_display (&it, w, startp);
15691 if (arg_scroll_conservatively)
15692 amount_to_scroll = max (dy, frame_line_height
15693 * max (scroll_step, temp_scroll_step));
15694 else if (scroll_step || temp_scroll_step)
15695 amount_to_scroll = scroll_max;
15696 else
15698 aggressive = BVAR (current_buffer, scroll_down_aggressively);
15699 height = WINDOW_BOX_TEXT_HEIGHT (w);
15700 if (NUMBERP (aggressive))
15702 double float_amount = XFLOATINT (aggressive) * height;
15703 int aggressive_scroll = float_amount;
15704 if (aggressive_scroll == 0 && float_amount > 0)
15705 aggressive_scroll = 1;
15706 /* Don't let point enter the scroll margin near
15707 bottom of the window, if the value of
15708 scroll_down_aggressively happens to be too
15709 large. */
15710 if (aggressive_scroll + 2 * this_scroll_margin > height)
15711 aggressive_scroll = height - 2 * this_scroll_margin;
15712 amount_to_scroll = dy + aggressive_scroll;
15716 if (amount_to_scroll <= 0)
15717 return SCROLLING_FAILED;
15719 move_it_vertically_backward (&it, amount_to_scroll);
15720 startp = it.current.pos;
15724 /* Run window scroll functions. */
15725 startp = run_window_scroll_functions (window, startp);
15727 /* Display the window. Give up if new fonts are loaded, or if point
15728 doesn't appear. */
15729 if (!try_window (window, startp, 0))
15730 rc = SCROLLING_NEED_LARGER_MATRICES;
15731 else if (w->cursor.vpos < 0)
15733 clear_glyph_matrix (w->desired_matrix);
15734 rc = SCROLLING_FAILED;
15736 else
15738 /* Maybe forget recorded base line for line number display. */
15739 if (!just_this_one_p
15740 || current_buffer->clip_changed
15741 || BEG_UNCHANGED < CHARPOS (startp))
15742 w->base_line_number = 0;
15744 /* If cursor ends up on a partially visible line,
15745 treat that as being off the bottom of the screen. */
15746 if (! cursor_row_fully_visible_p (w, extra_scroll_margin_lines <= 1,
15747 false)
15748 /* It's possible that the cursor is on the first line of the
15749 buffer, which is partially obscured due to a vscroll
15750 (Bug#7537). In that case, avoid looping forever. */
15751 && extra_scroll_margin_lines < w->desired_matrix->nrows - 1)
15753 clear_glyph_matrix (w->desired_matrix);
15754 ++extra_scroll_margin_lines;
15755 goto too_near_end;
15757 rc = SCROLLING_SUCCESS;
15760 return rc;
15764 /* Compute a suitable window start for window W if display of W starts
15765 on a continuation line. Value is true if a new window start
15766 was computed.
15768 The new window start will be computed, based on W's width, starting
15769 from the start of the continued line. It is the start of the
15770 screen line with the minimum distance from the old start W->start,
15771 which is still before point (otherwise point will definitely not
15772 be visible in the window). */
15774 static bool
15775 compute_window_start_on_continuation_line (struct window *w)
15777 struct text_pos pos, start_pos, pos_before_pt;
15778 bool window_start_changed_p = false;
15780 SET_TEXT_POS_FROM_MARKER (start_pos, w->start);
15782 /* If window start is on a continuation line... Window start may be
15783 < BEGV in case there's invisible text at the start of the
15784 buffer (M-x rmail, for example). */
15785 if (CHARPOS (start_pos) > BEGV
15786 && FETCH_BYTE (BYTEPOS (start_pos) - 1) != '\n')
15788 struct it it;
15789 struct glyph_row *row;
15791 /* Handle the case that the window start is out of range. */
15792 if (CHARPOS (start_pos) < BEGV)
15793 SET_TEXT_POS (start_pos, BEGV, BEGV_BYTE);
15794 else if (CHARPOS (start_pos) > ZV)
15795 SET_TEXT_POS (start_pos, ZV, ZV_BYTE);
15797 /* Find the start of the continued line. This should be fast
15798 because find_newline is fast (newline cache). */
15799 row = w->desired_matrix->rows + WINDOW_WANTS_HEADER_LINE_P (w);
15800 init_iterator (&it, w, CHARPOS (start_pos), BYTEPOS (start_pos),
15801 row, DEFAULT_FACE_ID);
15802 reseat_at_previous_visible_line_start (&it);
15804 /* If the line start is "too far" away from the window start,
15805 say it takes too much time to compute a new window start.
15806 Also, give up if the line start is after point, as in that
15807 case point will not be visible with any window start we
15808 compute. */
15809 if (IT_CHARPOS (it) <= PT
15810 || (CHARPOS (start_pos) - IT_CHARPOS (it)
15811 /* PXW: Do we need upper bounds here? */
15812 < WINDOW_TOTAL_LINES (w) * WINDOW_TOTAL_COLS (w)))
15814 int min_distance, distance;
15816 /* Move forward by display lines to find the new window
15817 start. If window width was enlarged, the new start can
15818 be expected to be > the old start. If window width was
15819 decreased, the new window start will be < the old start.
15820 So, we're looking for the display line start with the
15821 minimum distance from the old window start. */
15822 pos_before_pt = pos = it.current.pos;
15823 min_distance = INFINITY;
15824 while ((distance = eabs (CHARPOS (start_pos) - IT_CHARPOS (it))),
15825 distance < min_distance)
15827 min_distance = distance;
15828 if (CHARPOS (pos) <= PT)
15829 pos_before_pt = pos;
15830 pos = it.current.pos;
15831 if (it.line_wrap == WORD_WRAP)
15833 /* Under WORD_WRAP, move_it_by_lines is likely to
15834 overshoot and stop not at the first, but the
15835 second character from the left margin. So in
15836 that case, we need a more tight control on the X
15837 coordinate of the iterator than move_it_by_lines
15838 promises in its contract. The method is to first
15839 go to the last (rightmost) visible character of a
15840 line, then move to the leftmost character on the
15841 next line in a separate call. */
15842 move_it_to (&it, ZV, it.last_visible_x, it.current_y, -1,
15843 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
15844 move_it_to (&it, ZV, 0,
15845 it.current_y + it.max_ascent + it.max_descent, -1,
15846 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
15848 else
15849 move_it_by_lines (&it, 1);
15852 /* It makes very little sense to make the new window start
15853 after point, as point won't be visible. If that's what
15854 the loop above finds, fall back on the candidate before
15855 or at point that is closest to the old window start. */
15856 if (CHARPOS (pos) > PT)
15857 pos = pos_before_pt;
15859 /* Set the window start there. */
15860 SET_MARKER_FROM_TEXT_POS (w->start, pos);
15861 window_start_changed_p = true;
15865 return window_start_changed_p;
15869 /* Try cursor movement in case text has not changed in window WINDOW,
15870 with window start STARTP. Value is
15872 CURSOR_MOVEMENT_SUCCESS if successful
15874 CURSOR_MOVEMENT_CANNOT_BE_USED if this method cannot be used
15876 CURSOR_MOVEMENT_MUST_SCROLL if we know we have to scroll the
15877 display. *SCROLL_STEP is set to true, under certain circumstances, if
15878 we want to scroll as if scroll-step were set to 1. See the code.
15880 CURSOR_MOVEMENT_NEED_LARGER_MATRICES if we need larger matrices, in
15881 which case we have to abort this redisplay, and adjust matrices
15882 first. */
15884 enum
15886 CURSOR_MOVEMENT_SUCCESS,
15887 CURSOR_MOVEMENT_CANNOT_BE_USED,
15888 CURSOR_MOVEMENT_MUST_SCROLL,
15889 CURSOR_MOVEMENT_NEED_LARGER_MATRICES
15892 static int
15893 try_cursor_movement (Lisp_Object window, struct text_pos startp,
15894 bool *scroll_step)
15896 struct window *w = XWINDOW (window);
15897 struct frame *f = XFRAME (w->frame);
15898 int rc = CURSOR_MOVEMENT_CANNOT_BE_USED;
15900 #ifdef GLYPH_DEBUG
15901 if (inhibit_try_cursor_movement)
15902 return rc;
15903 #endif
15905 /* Previously, there was a check for Lisp integer in the
15906 if-statement below. Now, this field is converted to
15907 ptrdiff_t, thus zero means invalid position in a buffer. */
15908 eassert (w->last_point > 0);
15909 /* Likewise there was a check whether window_end_vpos is nil or larger
15910 than the window. Now window_end_vpos is int and so never nil, but
15911 let's leave eassert to check whether it fits in the window. */
15912 eassert (!w->window_end_valid
15913 || w->window_end_vpos < w->current_matrix->nrows);
15915 /* Handle case where text has not changed, only point, and it has
15916 not moved off the frame. */
15917 if (/* Point may be in this window. */
15918 PT >= CHARPOS (startp)
15919 /* Selective display hasn't changed. */
15920 && !current_buffer->clip_changed
15921 /* Function force-mode-line-update is used to force a thorough
15922 redisplay. It sets either windows_or_buffers_changed or
15923 update_mode_lines. So don't take a shortcut here for these
15924 cases. */
15925 && !update_mode_lines
15926 && !windows_or_buffers_changed
15927 && !f->cursor_type_changed
15928 && NILP (Vshow_trailing_whitespace)
15929 /* This code is not used for mini-buffer for the sake of the case
15930 of redisplaying to replace an echo area message; since in
15931 that case the mini-buffer contents per se are usually
15932 unchanged. This code is of no real use in the mini-buffer
15933 since the handling of this_line_start_pos, etc., in redisplay
15934 handles the same cases. */
15935 && !EQ (window, minibuf_window)
15936 && (FRAME_WINDOW_P (f)
15937 || !overlay_arrow_in_current_buffer_p ()))
15939 int this_scroll_margin, top_scroll_margin;
15940 struct glyph_row *row = NULL;
15942 #ifdef GLYPH_DEBUG
15943 debug_method_add (w, "cursor movement");
15944 #endif
15946 this_scroll_margin = window_scroll_margin (w, MARGIN_IN_PIXELS);
15948 top_scroll_margin = this_scroll_margin;
15949 if (WINDOW_WANTS_HEADER_LINE_P (w))
15950 top_scroll_margin += CURRENT_HEADER_LINE_HEIGHT (w);
15952 /* Start with the row the cursor was displayed during the last
15953 not paused redisplay. Give up if that row is not valid. */
15954 if (w->last_cursor_vpos < 0
15955 || w->last_cursor_vpos >= w->current_matrix->nrows)
15956 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15957 else
15959 row = MATRIX_ROW (w->current_matrix, w->last_cursor_vpos);
15960 if (row->mode_line_p)
15961 ++row;
15962 if (!row->enabled_p)
15963 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15966 if (rc == CURSOR_MOVEMENT_CANNOT_BE_USED)
15968 bool scroll_p = false, must_scroll = false;
15969 int last_y = window_text_bottom_y (w) - this_scroll_margin;
15971 if (PT > w->last_point)
15973 /* Point has moved forward. */
15974 while (MATRIX_ROW_END_CHARPOS (row) < PT
15975 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
15977 eassert (row->enabled_p);
15978 ++row;
15981 /* If the end position of a row equals the start
15982 position of the next row, and PT is at that position,
15983 we would rather display cursor in the next line. */
15984 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
15985 && MATRIX_ROW_END_CHARPOS (row) == PT
15986 && row < MATRIX_MODE_LINE_ROW (w->current_matrix)
15987 && MATRIX_ROW_START_CHARPOS (row+1) == PT
15988 && !cursor_row_p (row))
15989 ++row;
15991 /* If within the scroll margin, scroll. Note that
15992 MATRIX_ROW_BOTTOM_Y gives the pixel position at which
15993 the next line would be drawn, and that
15994 this_scroll_margin can be zero. */
15995 if (MATRIX_ROW_BOTTOM_Y (row) > last_y
15996 || PT > MATRIX_ROW_END_CHARPOS (row)
15997 /* Line is completely visible last line in window
15998 and PT is to be set in the next line. */
15999 || (MATRIX_ROW_BOTTOM_Y (row) == last_y
16000 && PT == MATRIX_ROW_END_CHARPOS (row)
16001 && !row->ends_at_zv_p
16002 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
16003 scroll_p = true;
16005 else if (PT < w->last_point)
16007 /* Cursor has to be moved backward. Note that PT >=
16008 CHARPOS (startp) because of the outer if-statement. */
16009 while (!row->mode_line_p
16010 && (MATRIX_ROW_START_CHARPOS (row) > PT
16011 || (MATRIX_ROW_START_CHARPOS (row) == PT
16012 && (MATRIX_ROW_STARTS_IN_MIDDLE_OF_CHAR_P (row)
16013 || (/* STARTS_IN_MIDDLE_OF_STRING_P (row) */
16014 row > w->current_matrix->rows
16015 && (row-1)->ends_in_newline_from_string_p))))
16016 && (row->y > top_scroll_margin
16017 || CHARPOS (startp) == BEGV))
16019 eassert (row->enabled_p);
16020 --row;
16023 /* Consider the following case: Window starts at BEGV,
16024 there is invisible, intangible text at BEGV, so that
16025 display starts at some point START > BEGV. It can
16026 happen that we are called with PT somewhere between
16027 BEGV and START. Try to handle that case. */
16028 if (row < w->current_matrix->rows
16029 || row->mode_line_p)
16031 row = w->current_matrix->rows;
16032 if (row->mode_line_p)
16033 ++row;
16036 /* Due to newlines in overlay strings, we may have to
16037 skip forward over overlay strings. */
16038 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
16039 && MATRIX_ROW_END_CHARPOS (row) == PT
16040 && !cursor_row_p (row))
16041 ++row;
16043 /* If within the scroll margin, scroll. */
16044 if (row->y < top_scroll_margin
16045 && CHARPOS (startp) != BEGV)
16046 scroll_p = true;
16048 else
16050 /* Cursor did not move. So don't scroll even if cursor line
16051 is partially visible, as it was so before. */
16052 rc = CURSOR_MOVEMENT_SUCCESS;
16055 if (PT < MATRIX_ROW_START_CHARPOS (row)
16056 || PT > MATRIX_ROW_END_CHARPOS (row))
16058 /* if PT is not in the glyph row, give up. */
16059 rc = CURSOR_MOVEMENT_MUST_SCROLL;
16060 must_scroll = true;
16062 else if (rc != CURSOR_MOVEMENT_SUCCESS
16063 && !NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering)))
16065 struct glyph_row *row1;
16067 /* If rows are bidi-reordered and point moved, back up
16068 until we find a row that does not belong to a
16069 continuation line. This is because we must consider
16070 all rows of a continued line as candidates for the
16071 new cursor positioning, since row start and end
16072 positions change non-linearly with vertical position
16073 in such rows. */
16074 /* FIXME: Revisit this when glyph ``spilling'' in
16075 continuation lines' rows is implemented for
16076 bidi-reordered rows. */
16077 for (row1 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
16078 MATRIX_ROW_CONTINUATION_LINE_P (row);
16079 --row)
16081 /* If we hit the beginning of the displayed portion
16082 without finding the first row of a continued
16083 line, give up. */
16084 if (row <= row1)
16086 rc = CURSOR_MOVEMENT_MUST_SCROLL;
16087 break;
16089 eassert (row->enabled_p);
16092 if (must_scroll)
16094 else if (rc != CURSOR_MOVEMENT_SUCCESS
16095 && MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row)
16096 /* Make sure this isn't a header line by any chance, since
16097 then MATRIX_ROW_PARTIALLY_VISIBLE_P might yield true. */
16098 && !row->mode_line_p
16099 && make_cursor_line_fully_visible_p)
16101 if (PT == MATRIX_ROW_END_CHARPOS (row)
16102 && !row->ends_at_zv_p
16103 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
16104 rc = CURSOR_MOVEMENT_MUST_SCROLL;
16105 else if (row->height > window_box_height (w))
16107 /* If we end up in a partially visible line, let's
16108 make it fully visible, except when it's taller
16109 than the window, in which case we can't do much
16110 about it. */
16111 *scroll_step = true;
16112 rc = CURSOR_MOVEMENT_MUST_SCROLL;
16114 else
16116 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
16117 if (!cursor_row_fully_visible_p (w, false, true))
16118 rc = CURSOR_MOVEMENT_MUST_SCROLL;
16119 else
16120 rc = CURSOR_MOVEMENT_SUCCESS;
16123 else if (scroll_p)
16124 rc = CURSOR_MOVEMENT_MUST_SCROLL;
16125 else if (rc != CURSOR_MOVEMENT_SUCCESS
16126 && !NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering)))
16128 /* With bidi-reordered rows, there could be more than
16129 one candidate row whose start and end positions
16130 occlude point. We need to let set_cursor_from_row
16131 find the best candidate. */
16132 /* FIXME: Revisit this when glyph ``spilling'' in
16133 continuation lines' rows is implemented for
16134 bidi-reordered rows. */
16135 bool rv = false;
16139 bool at_zv_p = false, exact_match_p = false;
16141 if (MATRIX_ROW_START_CHARPOS (row) <= PT
16142 && PT <= MATRIX_ROW_END_CHARPOS (row)
16143 && cursor_row_p (row))
16144 rv |= set_cursor_from_row (w, row, w->current_matrix,
16145 0, 0, 0, 0);
16146 /* As soon as we've found the exact match for point,
16147 or the first suitable row whose ends_at_zv_p flag
16148 is set, we are done. */
16149 if (rv)
16151 at_zv_p = MATRIX_ROW (w->current_matrix,
16152 w->cursor.vpos)->ends_at_zv_p;
16153 if (!at_zv_p
16154 && w->cursor.hpos >= 0
16155 && w->cursor.hpos < MATRIX_ROW_USED (w->current_matrix,
16156 w->cursor.vpos))
16158 struct glyph_row *candidate =
16159 MATRIX_ROW (w->current_matrix, w->cursor.vpos);
16160 struct glyph *g =
16161 candidate->glyphs[TEXT_AREA] + w->cursor.hpos;
16162 ptrdiff_t endpos = MATRIX_ROW_END_CHARPOS (candidate);
16164 exact_match_p =
16165 (BUFFERP (g->object) && g->charpos == PT)
16166 || (NILP (g->object)
16167 && (g->charpos == PT
16168 || (g->charpos == 0 && endpos - 1 == PT)));
16170 if (at_zv_p || exact_match_p)
16172 rc = CURSOR_MOVEMENT_SUCCESS;
16173 break;
16176 if (MATRIX_ROW_BOTTOM_Y (row) == last_y)
16177 break;
16178 ++row;
16180 while (((MATRIX_ROW_CONTINUATION_LINE_P (row)
16181 || row->continued_p)
16182 && MATRIX_ROW_BOTTOM_Y (row) <= last_y)
16183 || (MATRIX_ROW_START_CHARPOS (row) == PT
16184 && MATRIX_ROW_BOTTOM_Y (row) < last_y));
16185 /* If we didn't find any candidate rows, or exited the
16186 loop before all the candidates were examined, signal
16187 to the caller that this method failed. */
16188 if (rc != CURSOR_MOVEMENT_SUCCESS
16189 && !(rv
16190 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
16191 && !row->continued_p))
16192 rc = CURSOR_MOVEMENT_MUST_SCROLL;
16193 else if (rv)
16194 rc = CURSOR_MOVEMENT_SUCCESS;
16196 else
16200 if (set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0))
16202 rc = CURSOR_MOVEMENT_SUCCESS;
16203 break;
16205 ++row;
16207 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
16208 && MATRIX_ROW_START_CHARPOS (row) == PT
16209 && cursor_row_p (row));
16214 return rc;
16218 void
16219 set_vertical_scroll_bar (struct window *w)
16221 ptrdiff_t start, end, whole;
16223 /* Calculate the start and end positions for the current window.
16224 At some point, it would be nice to choose between scrollbars
16225 which reflect the whole buffer size, with special markers
16226 indicating narrowing, and scrollbars which reflect only the
16227 visible region.
16229 Note that mini-buffers sometimes aren't displaying any text. */
16230 if (!MINI_WINDOW_P (w)
16231 || (w == XWINDOW (minibuf_window)
16232 && NILP (echo_area_buffer[0])))
16234 struct buffer *buf = XBUFFER (w->contents);
16235 whole = BUF_ZV (buf) - BUF_BEGV (buf);
16236 start = marker_position (w->start) - BUF_BEGV (buf);
16237 /* I don't think this is guaranteed to be right. For the
16238 moment, we'll pretend it is. */
16239 end = BUF_Z (buf) - w->window_end_pos - BUF_BEGV (buf);
16241 if (end < start)
16242 end = start;
16243 if (whole < (end - start))
16244 whole = end - start;
16246 else
16247 start = end = whole = 0;
16249 /* Indicate what this scroll bar ought to be displaying now. */
16250 if (FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
16251 (*FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
16252 (w, end - start, whole, start);
16256 void
16257 set_horizontal_scroll_bar (struct window *w)
16259 int start, end, whole, portion;
16261 if (!MINI_WINDOW_P (w)
16262 || (w == XWINDOW (minibuf_window)
16263 && NILP (echo_area_buffer[0])))
16265 struct buffer *b = XBUFFER (w->contents);
16266 struct buffer *old_buffer = NULL;
16267 struct it it;
16268 struct text_pos startp;
16270 if (b != current_buffer)
16272 old_buffer = current_buffer;
16273 set_buffer_internal (b);
16276 SET_TEXT_POS_FROM_MARKER (startp, w->start);
16277 start_display (&it, w, startp);
16278 it.last_visible_x = INT_MAX;
16279 whole = move_it_to (&it, -1, INT_MAX, window_box_height (w), -1,
16280 MOVE_TO_X | MOVE_TO_Y);
16281 /* whole = move_it_to (&it, w->window_end_pos, INT_MAX,
16282 window_box_height (w), -1,
16283 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y); */
16285 start = w->hscroll * FRAME_COLUMN_WIDTH (WINDOW_XFRAME (w));
16286 end = start + window_box_width (w, TEXT_AREA);
16287 portion = end - start;
16288 /* After enlarging a horizontally scrolled window such that it
16289 gets at least as wide as the text it contains, make sure that
16290 the thumb doesn't fill the entire scroll bar so we can still
16291 drag it back to see the entire text. */
16292 whole = max (whole, end);
16294 if (it.bidi_p)
16296 Lisp_Object pdir;
16298 pdir = Fcurrent_bidi_paragraph_direction (Qnil);
16299 if (EQ (pdir, Qright_to_left))
16301 start = whole - end;
16302 end = start + portion;
16306 if (old_buffer)
16307 set_buffer_internal (old_buffer);
16309 else
16310 start = end = whole = portion = 0;
16312 w->hscroll_whole = whole;
16314 /* Indicate what this scroll bar ought to be displaying now. */
16315 if (FRAME_TERMINAL (XFRAME (w->frame))->set_horizontal_scroll_bar_hook)
16316 (*FRAME_TERMINAL (XFRAME (w->frame))->set_horizontal_scroll_bar_hook)
16317 (w, portion, whole, start);
16321 /* Redisplay leaf window WINDOW. JUST_THIS_ONE_P means only
16322 selected_window is redisplayed.
16324 We can return without actually redisplaying the window if fonts has been
16325 changed on window's frame. In that case, redisplay_internal will retry.
16327 As one of the important parts of redisplaying a window, we need to
16328 decide whether the previous window-start position (stored in the
16329 window's w->start marker position) is still valid, and if it isn't,
16330 recompute it. Some details about that:
16332 . The previous window-start could be in a continuation line, in
16333 which case we need to recompute it when the window width
16334 changes. See compute_window_start_on_continuation_line and its
16335 call below.
16337 . The text that changed since last redisplay could include the
16338 previous window-start position. In that case, we try to salvage
16339 what we can from the current glyph matrix by calling
16340 try_scrolling, which see.
16342 . Some Emacs command could force us to use a specific window-start
16343 position by setting the window's force_start flag, or gently
16344 propose doing that by setting the window's optional_new_start
16345 flag. In these cases, we try using the specified start point if
16346 that succeeds (i.e. the window desired matrix is successfully
16347 recomputed, and point location is within the window). In case
16348 of optional_new_start, we first check if the specified start
16349 position is feasible, i.e. if it will allow point to be
16350 displayed in the window. If using the specified start point
16351 fails, e.g., if new fonts are needed to be loaded, we abort the
16352 redisplay cycle and leave it up to the next cycle to figure out
16353 things.
16355 . Note that the window's force_start flag is sometimes set by
16356 redisplay itself, when it decides that the previous window start
16357 point is fine and should be kept. Search for "goto force_start"
16358 below to see the details. Like the values of window-start
16359 specified outside of redisplay, these internally-deduced values
16360 are tested for feasibility, and ignored if found to be
16361 unfeasible.
16363 . Note that the function try_window, used to completely redisplay
16364 a window, accepts the window's start point as its argument.
16365 This is used several times in the redisplay code to control
16366 where the window start will be, according to user options such
16367 as scroll-conservatively, and also to ensure the screen line
16368 showing point will be fully (as opposed to partially) visible on
16369 display. */
16371 static void
16372 redisplay_window (Lisp_Object window, bool just_this_one_p)
16374 struct window *w = XWINDOW (window);
16375 struct frame *f = XFRAME (w->frame);
16376 struct buffer *buffer = XBUFFER (w->contents);
16377 struct buffer *old = current_buffer;
16378 struct text_pos lpoint, opoint, startp;
16379 bool update_mode_line;
16380 int tem;
16381 struct it it;
16382 /* Record it now because it's overwritten. */
16383 bool current_matrix_up_to_date_p = false;
16384 bool used_current_matrix_p = false;
16385 /* This is less strict than current_matrix_up_to_date_p.
16386 It indicates that the buffer contents and narrowing are unchanged. */
16387 bool buffer_unchanged_p = false;
16388 bool temp_scroll_step = false;
16389 ptrdiff_t count = SPECPDL_INDEX ();
16390 int rc;
16391 int centering_position = -1;
16392 bool last_line_misfit = false;
16393 ptrdiff_t beg_unchanged, end_unchanged;
16394 int frame_line_height, margin;
16395 bool use_desired_matrix;
16396 void *itdata = NULL;
16398 SET_TEXT_POS (lpoint, PT, PT_BYTE);
16399 opoint = lpoint;
16401 #ifdef GLYPH_DEBUG
16402 *w->desired_matrix->method = 0;
16403 #endif
16405 if (!just_this_one_p
16406 && REDISPLAY_SOME_P ()
16407 && !w->redisplay
16408 && !w->update_mode_line
16409 && !f->face_change
16410 && !f->redisplay
16411 && !buffer->text->redisplay
16412 && BUF_PT (buffer) == w->last_point)
16413 return;
16415 /* Make sure that both W's markers are valid. */
16416 eassert (XMARKER (w->start)->buffer == buffer);
16417 eassert (XMARKER (w->pointm)->buffer == buffer);
16419 /* We come here again if we need to run window-text-change-functions
16420 below. */
16421 restart:
16422 reconsider_clip_changes (w);
16423 frame_line_height = default_line_pixel_height (w);
16424 margin = window_scroll_margin (w, MARGIN_IN_LINES);
16427 /* Has the mode line to be updated? */
16428 update_mode_line = (w->update_mode_line
16429 || update_mode_lines
16430 || buffer->clip_changed
16431 || buffer->prevent_redisplay_optimizations_p);
16433 if (!just_this_one_p)
16434 /* If `just_this_one_p' is set, we apparently set must_be_updated_p more
16435 cleverly elsewhere. */
16436 w->must_be_updated_p = true;
16438 if (MINI_WINDOW_P (w))
16440 if (w == XWINDOW (echo_area_window)
16441 && !NILP (echo_area_buffer[0]))
16443 if (update_mode_line)
16444 /* We may have to update a tty frame's menu bar or a
16445 tool-bar. Example `M-x C-h C-h C-g'. */
16446 goto finish_menu_bars;
16447 else
16448 /* We've already displayed the echo area glyphs in this window. */
16449 goto finish_scroll_bars;
16451 else if ((w != XWINDOW (minibuf_window)
16452 || minibuf_level == 0)
16453 /* When buffer is nonempty, redisplay window normally. */
16454 && BUF_Z (XBUFFER (w->contents)) == BUF_BEG (XBUFFER (w->contents))
16455 /* Quail displays non-mini buffers in minibuffer window.
16456 In that case, redisplay the window normally. */
16457 && !NILP (Fmemq (w->contents, Vminibuffer_list)))
16459 /* W is a mini-buffer window, but it's not active, so clear
16460 it. */
16461 int yb = window_text_bottom_y (w);
16462 struct glyph_row *row;
16463 int y;
16465 for (y = 0, row = w->desired_matrix->rows;
16466 y < yb;
16467 y += row->height, ++row)
16468 blank_row (w, row, y);
16469 goto finish_scroll_bars;
16472 clear_glyph_matrix (w->desired_matrix);
16475 /* Otherwise set up data on this window; select its buffer and point
16476 value. */
16477 /* Really select the buffer, for the sake of buffer-local
16478 variables. */
16479 set_buffer_internal_1 (XBUFFER (w->contents));
16481 current_matrix_up_to_date_p
16482 = (w->window_end_valid
16483 && !current_buffer->clip_changed
16484 && !current_buffer->prevent_redisplay_optimizations_p
16485 && !window_outdated (w)
16486 && !hscrolling_current_line_p (w));
16488 /* Run the window-text-change-functions
16489 if it is possible that the text on the screen has changed
16490 (either due to modification of the text, or any other reason). */
16491 if (!current_matrix_up_to_date_p
16492 && !NILP (Vwindow_text_change_functions))
16494 safe_run_hooks (Qwindow_text_change_functions);
16495 goto restart;
16498 beg_unchanged = BEG_UNCHANGED;
16499 end_unchanged = END_UNCHANGED;
16501 SET_TEXT_POS (opoint, PT, PT_BYTE);
16503 specbind (Qinhibit_point_motion_hooks, Qt);
16505 buffer_unchanged_p
16506 = (w->window_end_valid
16507 && !current_buffer->clip_changed
16508 && !window_outdated (w));
16510 /* When windows_or_buffers_changed is non-zero, we can't rely
16511 on the window end being valid, so set it to zero there. */
16512 if (windows_or_buffers_changed)
16514 /* If window starts on a continuation line, maybe adjust the
16515 window start in case the window's width changed. */
16516 if (XMARKER (w->start)->buffer == current_buffer)
16517 compute_window_start_on_continuation_line (w);
16519 w->window_end_valid = false;
16520 /* If so, we also can't rely on current matrix
16521 and should not fool try_cursor_movement below. */
16522 current_matrix_up_to_date_p = false;
16525 /* Some sanity checks. */
16526 CHECK_WINDOW_END (w);
16527 if (Z == Z_BYTE && CHARPOS (opoint) != BYTEPOS (opoint))
16528 emacs_abort ();
16529 if (BYTEPOS (opoint) < CHARPOS (opoint))
16530 emacs_abort ();
16532 if (mode_line_update_needed (w))
16533 update_mode_line = true;
16535 /* Point refers normally to the selected window. For any other
16536 window, set up appropriate value. */
16537 if (!EQ (window, selected_window))
16539 ptrdiff_t new_pt = marker_position (w->pointm);
16540 ptrdiff_t new_pt_byte = marker_byte_position (w->pointm);
16542 if (new_pt < BEGV)
16544 new_pt = BEGV;
16545 new_pt_byte = BEGV_BYTE;
16546 set_marker_both (w->pointm, Qnil, BEGV, BEGV_BYTE);
16548 else if (new_pt > (ZV - 1))
16550 new_pt = ZV;
16551 new_pt_byte = ZV_BYTE;
16552 set_marker_both (w->pointm, Qnil, ZV, ZV_BYTE);
16555 /* We don't use SET_PT so that the point-motion hooks don't run. */
16556 TEMP_SET_PT_BOTH (new_pt, new_pt_byte);
16559 /* If any of the character widths specified in the display table
16560 have changed, invalidate the width run cache. It's true that
16561 this may be a bit late to catch such changes, but the rest of
16562 redisplay goes (non-fatally) haywire when the display table is
16563 changed, so why should we worry about doing any better? */
16564 if (current_buffer->width_run_cache
16565 || (current_buffer->base_buffer
16566 && current_buffer->base_buffer->width_run_cache))
16568 struct Lisp_Char_Table *disptab = buffer_display_table ();
16570 if (! disptab_matches_widthtab
16571 (disptab, XVECTOR (BVAR (current_buffer, width_table))))
16573 struct buffer *buf = current_buffer;
16575 if (buf->base_buffer)
16576 buf = buf->base_buffer;
16577 invalidate_region_cache (buf, buf->width_run_cache, BEG, Z);
16578 recompute_width_table (current_buffer, disptab);
16582 /* If window-start is screwed up, choose a new one. */
16583 if (XMARKER (w->start)->buffer != current_buffer)
16584 goto recenter;
16586 SET_TEXT_POS_FROM_MARKER (startp, w->start);
16588 /* If someone specified a new starting point but did not insist,
16589 check whether it can be used. */
16590 if ((w->optional_new_start || window_frozen_p (w))
16591 && CHARPOS (startp) >= BEGV
16592 && CHARPOS (startp) <= ZV)
16594 ptrdiff_t it_charpos;
16596 w->optional_new_start = false;
16597 start_display (&it, w, startp);
16598 move_it_to (&it, PT, 0, it.last_visible_y, -1,
16599 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
16600 /* Record IT's position now, since line_bottom_y might change
16601 that. */
16602 it_charpos = IT_CHARPOS (it);
16603 /* Make sure we set the force_start flag only if the cursor row
16604 will be fully visible. Otherwise, the code under force_start
16605 label below will try to move point back into view, which is
16606 not what the code which sets optional_new_start wants. */
16607 if ((it.current_y == 0 || line_bottom_y (&it) < it.last_visible_y)
16608 && !w->force_start)
16610 if (it_charpos == PT)
16611 w->force_start = true;
16612 /* IT may overshoot PT if text at PT is invisible. */
16613 else if (it_charpos > PT && CHARPOS (startp) <= PT)
16614 w->force_start = true;
16615 #ifdef GLYPH_DEBUG
16616 if (w->force_start)
16618 if (window_frozen_p (w))
16619 debug_method_add (w, "set force_start from frozen window start");
16620 else
16621 debug_method_add (w, "set force_start from optional_new_start");
16623 #endif
16627 force_start:
16629 /* Handle case where place to start displaying has been specified,
16630 unless the specified location is outside the accessible range. */
16631 if (w->force_start)
16633 /* We set this later on if we have to adjust point. */
16634 int new_vpos = -1;
16636 w->force_start = false;
16637 w->vscroll = 0;
16638 w->window_end_valid = false;
16640 /* Forget any recorded base line for line number display. */
16641 if (!buffer_unchanged_p)
16642 w->base_line_number = 0;
16644 /* Redisplay the mode line. Select the buffer properly for that.
16645 Also, run the hook window-scroll-functions
16646 because we have scrolled. */
16647 /* Note, we do this after clearing force_start because
16648 if there's an error, it is better to forget about force_start
16649 than to get into an infinite loop calling the hook functions
16650 and having them get more errors. */
16651 if (!update_mode_line
16652 || ! NILP (Vwindow_scroll_functions))
16654 update_mode_line = true;
16655 w->update_mode_line = true;
16656 startp = run_window_scroll_functions (window, startp);
16659 if (CHARPOS (startp) < BEGV)
16660 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
16661 else if (CHARPOS (startp) > ZV)
16662 SET_TEXT_POS (startp, ZV, ZV_BYTE);
16664 /* Redisplay, then check if cursor has been set during the
16665 redisplay. Give up if new fonts were loaded. */
16666 /* We used to issue a CHECK_MARGINS argument to try_window here,
16667 but this causes scrolling to fail when point begins inside
16668 the scroll margin (bug#148) -- cyd */
16669 if (!try_window (window, startp, 0))
16671 w->force_start = true;
16672 clear_glyph_matrix (w->desired_matrix);
16673 goto need_larger_matrices;
16676 if (w->cursor.vpos < 0)
16678 /* If point does not appear, try to move point so it does
16679 appear. The desired matrix has been built above, so we
16680 can use it here. First see if point is in invisible
16681 text, and if so, move it to the first visible buffer
16682 position past that. */
16683 struct glyph_row *r = NULL;
16684 Lisp_Object invprop =
16685 get_char_property_and_overlay (make_number (PT), Qinvisible,
16686 Qnil, NULL);
16688 if (TEXT_PROP_MEANS_INVISIBLE (invprop) != 0)
16690 ptrdiff_t alt_pt;
16691 Lisp_Object invprop_end =
16692 Fnext_single_char_property_change (make_number (PT), Qinvisible,
16693 Qnil, Qnil);
16695 if (NATNUMP (invprop_end))
16696 alt_pt = XFASTINT (invprop_end);
16697 else
16698 alt_pt = ZV;
16699 r = row_containing_pos (w, alt_pt, w->desired_matrix->rows,
16700 NULL, 0);
16702 if (r)
16703 new_vpos = MATRIX_ROW_BOTTOM_Y (r);
16704 else /* Give up and just move to the middle of the window. */
16705 new_vpos = window_box_height (w) / 2;
16708 if (!cursor_row_fully_visible_p (w, false, false))
16710 /* Point does appear, but on a line partly visible at end of window.
16711 Move it back to a fully-visible line. */
16712 new_vpos = window_box_height (w);
16713 /* But if window_box_height suggests a Y coordinate that is
16714 not less than we already have, that line will clearly not
16715 be fully visible, so give up and scroll the display.
16716 This can happen when the default face uses a font whose
16717 dimensions are different from the frame's default
16718 font. */
16719 if (new_vpos >= w->cursor.y)
16721 w->cursor.vpos = -1;
16722 clear_glyph_matrix (w->desired_matrix);
16723 goto try_to_scroll;
16726 else if (w->cursor.vpos >= 0)
16728 /* Some people insist on not letting point enter the scroll
16729 margin, even though this part handles windows that didn't
16730 scroll at all. */
16731 int pixel_margin = margin * frame_line_height;
16732 bool header_line = WINDOW_WANTS_HEADER_LINE_P (w);
16734 /* Note: We add an extra FRAME_LINE_HEIGHT, because the loop
16735 below, which finds the row to move point to, advances by
16736 the Y coordinate of the _next_ row, see the definition of
16737 MATRIX_ROW_BOTTOM_Y. */
16738 if (w->cursor.vpos < margin + header_line)
16740 w->cursor.vpos = -1;
16741 clear_glyph_matrix (w->desired_matrix);
16742 goto try_to_scroll;
16744 else
16746 int window_height = window_box_height (w);
16748 if (header_line)
16749 window_height += CURRENT_HEADER_LINE_HEIGHT (w);
16750 if (w->cursor.y >= window_height - pixel_margin)
16752 w->cursor.vpos = -1;
16753 clear_glyph_matrix (w->desired_matrix);
16754 goto try_to_scroll;
16759 /* If we need to move point for either of the above reasons,
16760 now actually do it. */
16761 if (new_vpos >= 0)
16763 struct glyph_row *row;
16765 row = MATRIX_FIRST_TEXT_ROW (w->desired_matrix);
16766 while (MATRIX_ROW_BOTTOM_Y (row) < new_vpos)
16767 ++row;
16769 TEMP_SET_PT_BOTH (MATRIX_ROW_START_CHARPOS (row),
16770 MATRIX_ROW_START_BYTEPOS (row));
16772 if (w != XWINDOW (selected_window))
16773 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
16774 else if (current_buffer == old)
16775 SET_TEXT_POS (lpoint, PT, PT_BYTE);
16777 set_cursor_from_row (w, row, w->desired_matrix, 0, 0, 0, 0);
16779 /* Re-run pre-redisplay-function so it can update the region
16780 according to the new position of point. */
16781 /* Other than the cursor, w's redisplay is done so we can set its
16782 redisplay to false. Also the buffer's redisplay can be set to
16783 false, since propagate_buffer_redisplay should have already
16784 propagated its info to `w' anyway. */
16785 w->redisplay = false;
16786 XBUFFER (w->contents)->text->redisplay = false;
16787 safe__call1 (true, Vpre_redisplay_function, Fcons (window, Qnil));
16789 if (w->redisplay || XBUFFER (w->contents)->text->redisplay)
16791 /* pre-redisplay-function made changes (e.g. move the region)
16792 that require another round of redisplay. */
16793 clear_glyph_matrix (w->desired_matrix);
16794 if (!try_window (window, startp, 0))
16795 goto need_larger_matrices;
16798 if (w->cursor.vpos < 0 || !cursor_row_fully_visible_p (w, false, false))
16800 clear_glyph_matrix (w->desired_matrix);
16801 goto try_to_scroll;
16804 #ifdef GLYPH_DEBUG
16805 debug_method_add (w, "forced window start");
16806 #endif
16807 goto done;
16810 /* Handle case where text has not changed, only point, and it has
16811 not moved off the frame, and we are not retrying after hscroll.
16812 (current_matrix_up_to_date_p is true when retrying.) */
16813 if (current_matrix_up_to_date_p
16814 && (rc = try_cursor_movement (window, startp, &temp_scroll_step),
16815 rc != CURSOR_MOVEMENT_CANNOT_BE_USED))
16817 switch (rc)
16819 case CURSOR_MOVEMENT_SUCCESS:
16820 used_current_matrix_p = true;
16821 goto done;
16823 case CURSOR_MOVEMENT_MUST_SCROLL:
16824 goto try_to_scroll;
16826 default:
16827 emacs_abort ();
16830 /* If current starting point was originally the beginning of a line
16831 but no longer is, find a new starting point. */
16832 else if (w->start_at_line_beg
16833 && !(CHARPOS (startp) <= BEGV
16834 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n'))
16836 #ifdef GLYPH_DEBUG
16837 debug_method_add (w, "recenter 1");
16838 #endif
16839 goto recenter;
16842 /* Try scrolling with try_window_id. Value is > 0 if update has
16843 been done, it is -1 if we know that the same window start will
16844 not work. It is 0 if unsuccessful for some other reason. */
16845 else if ((tem = try_window_id (w)) != 0)
16847 #ifdef GLYPH_DEBUG
16848 debug_method_add (w, "try_window_id %d", tem);
16849 #endif
16851 if (f->fonts_changed)
16852 goto need_larger_matrices;
16853 if (tem > 0)
16854 goto done;
16856 /* Otherwise try_window_id has returned -1 which means that we
16857 don't want the alternative below this comment to execute. */
16859 else if (CHARPOS (startp) >= BEGV
16860 && CHARPOS (startp) <= ZV
16861 && PT >= CHARPOS (startp)
16862 && (CHARPOS (startp) < ZV
16863 /* Avoid starting at end of buffer. */
16864 || CHARPOS (startp) == BEGV
16865 || !window_outdated (w)))
16867 int d1, d2, d5, d6;
16868 int rtop, rbot;
16870 /* If first window line is a continuation line, and window start
16871 is inside the modified region, but the first change is before
16872 current window start, we must select a new window start.
16874 However, if this is the result of a down-mouse event (e.g. by
16875 extending the mouse-drag-overlay), we don't want to select a
16876 new window start, since that would change the position under
16877 the mouse, resulting in an unwanted mouse-movement rather
16878 than a simple mouse-click. */
16879 if (!w->start_at_line_beg
16880 && NILP (do_mouse_tracking)
16881 && CHARPOS (startp) > BEGV
16882 && CHARPOS (startp) > BEG + beg_unchanged
16883 && CHARPOS (startp) <= Z - end_unchanged
16884 /* Even if w->start_at_line_beg is nil, a new window may
16885 start at a line_beg, since that's how set_buffer_window
16886 sets it. So, we need to check the return value of
16887 compute_window_start_on_continuation_line. (See also
16888 bug#197). */
16889 && XMARKER (w->start)->buffer == current_buffer
16890 && compute_window_start_on_continuation_line (w)
16891 /* It doesn't make sense to force the window start like we
16892 do at label force_start if it is already known that point
16893 will not be fully visible in the resulting window, because
16894 doing so will move point from its correct position
16895 instead of scrolling the window to bring point into view.
16896 See bug#9324. */
16897 && pos_visible_p (w, PT, &d1, &d2, &rtop, &rbot, &d5, &d6)
16898 /* A very tall row could need more than the window height,
16899 in which case we accept that it is partially visible. */
16900 && (rtop != 0) == (rbot != 0))
16902 w->force_start = true;
16903 SET_TEXT_POS_FROM_MARKER (startp, w->start);
16904 #ifdef GLYPH_DEBUG
16905 debug_method_add (w, "recomputed window start in continuation line");
16906 #endif
16907 goto force_start;
16910 #ifdef GLYPH_DEBUG
16911 debug_method_add (w, "same window start");
16912 #endif
16914 /* Try to redisplay starting at same place as before.
16915 If point has not moved off frame, accept the results. */
16916 if (!current_matrix_up_to_date_p
16917 /* Don't use try_window_reusing_current_matrix in this case
16918 because a window scroll function can have changed the
16919 buffer. */
16920 || !NILP (Vwindow_scroll_functions)
16921 || MINI_WINDOW_P (w)
16922 || !(used_current_matrix_p
16923 = try_window_reusing_current_matrix (w)))
16925 IF_DEBUG (debug_method_add (w, "1"));
16926 if (try_window (window, startp, TRY_WINDOW_CHECK_MARGINS) < 0)
16927 /* -1 means we need to scroll.
16928 0 means we need new matrices, but fonts_changed
16929 is set in that case, so we will detect it below. */
16930 goto try_to_scroll;
16933 if (f->fonts_changed)
16934 goto need_larger_matrices;
16936 if (w->cursor.vpos >= 0)
16938 if (!just_this_one_p
16939 || current_buffer->clip_changed
16940 || BEG_UNCHANGED < CHARPOS (startp))
16941 /* Forget any recorded base line for line number display. */
16942 w->base_line_number = 0;
16944 if (!cursor_row_fully_visible_p (w, true, false))
16946 clear_glyph_matrix (w->desired_matrix);
16947 last_line_misfit = true;
16949 /* Drop through and scroll. */
16950 else
16951 goto done;
16953 else
16954 clear_glyph_matrix (w->desired_matrix);
16957 try_to_scroll:
16959 /* Redisplay the mode line. Select the buffer properly for that. */
16960 if (!update_mode_line)
16962 update_mode_line = true;
16963 w->update_mode_line = true;
16966 /* Try to scroll by specified few lines. */
16967 if ((scroll_conservatively
16968 || emacs_scroll_step
16969 || temp_scroll_step
16970 || NUMBERP (BVAR (current_buffer, scroll_up_aggressively))
16971 || NUMBERP (BVAR (current_buffer, scroll_down_aggressively)))
16972 && CHARPOS (startp) >= BEGV
16973 && CHARPOS (startp) <= ZV)
16975 /* The function returns -1 if new fonts were loaded, 1 if
16976 successful, 0 if not successful. */
16977 int ss = try_scrolling (window, just_this_one_p,
16978 scroll_conservatively,
16979 emacs_scroll_step,
16980 temp_scroll_step, last_line_misfit);
16981 switch (ss)
16983 case SCROLLING_SUCCESS:
16984 goto done;
16986 case SCROLLING_NEED_LARGER_MATRICES:
16987 goto need_larger_matrices;
16989 case SCROLLING_FAILED:
16990 break;
16992 default:
16993 emacs_abort ();
16997 /* Finally, just choose a place to start which positions point
16998 according to user preferences. */
17000 recenter:
17002 #ifdef GLYPH_DEBUG
17003 debug_method_add (w, "recenter");
17004 #endif
17006 /* Forget any previously recorded base line for line number display. */
17007 if (!buffer_unchanged_p)
17008 w->base_line_number = 0;
17010 /* Determine the window start relative to point. */
17011 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
17012 it.current_y = it.last_visible_y;
17013 if (centering_position < 0)
17015 ptrdiff_t margin_pos = CHARPOS (startp);
17016 Lisp_Object aggressive;
17017 bool scrolling_up;
17019 /* If there is a scroll margin at the top of the window, find
17020 its character position. */
17021 if (margin
17022 /* Cannot call start_display if startp is not in the
17023 accessible region of the buffer. This can happen when we
17024 have just switched to a different buffer and/or changed
17025 its restriction. In that case, startp is initialized to
17026 the character position 1 (BEGV) because we did not yet
17027 have chance to display the buffer even once. */
17028 && BEGV <= CHARPOS (startp) && CHARPOS (startp) <= ZV)
17030 struct it it1;
17031 void *it1data = NULL;
17033 SAVE_IT (it1, it, it1data);
17034 start_display (&it1, w, startp);
17035 move_it_vertically (&it1, margin * frame_line_height);
17036 margin_pos = IT_CHARPOS (it1);
17037 RESTORE_IT (&it, &it, it1data);
17039 scrolling_up = PT > margin_pos;
17040 aggressive =
17041 scrolling_up
17042 ? BVAR (current_buffer, scroll_up_aggressively)
17043 : BVAR (current_buffer, scroll_down_aggressively);
17045 if (!MINI_WINDOW_P (w)
17046 && (scroll_conservatively > SCROLL_LIMIT || NUMBERP (aggressive)))
17048 int pt_offset = 0;
17050 /* Setting scroll-conservatively overrides
17051 scroll-*-aggressively. */
17052 if (!scroll_conservatively && NUMBERP (aggressive))
17054 double float_amount = XFLOATINT (aggressive);
17056 pt_offset = float_amount * WINDOW_BOX_TEXT_HEIGHT (w);
17057 if (pt_offset == 0 && float_amount > 0)
17058 pt_offset = 1;
17059 if (pt_offset && margin > 0)
17060 margin -= 1;
17062 /* Compute how much to move the window start backward from
17063 point so that point will be displayed where the user
17064 wants it. */
17065 if (scrolling_up)
17067 centering_position = it.last_visible_y;
17068 if (pt_offset)
17069 centering_position -= pt_offset;
17070 centering_position -=
17071 (frame_line_height * (1 + margin + last_line_misfit)
17072 + WINDOW_HEADER_LINE_HEIGHT (w));
17073 /* Don't let point enter the scroll margin near top of
17074 the window. */
17075 if (centering_position < margin * frame_line_height)
17076 centering_position = margin * frame_line_height;
17078 else
17079 centering_position = margin * frame_line_height + pt_offset;
17081 else
17082 /* Set the window start half the height of the window backward
17083 from point. */
17084 centering_position = window_box_height (w) / 2;
17086 move_it_vertically_backward (&it, centering_position);
17088 eassert (IT_CHARPOS (it) >= BEGV);
17090 /* The function move_it_vertically_backward may move over more
17091 than the specified y-distance. If it->w is small, e.g. a
17092 mini-buffer window, we may end up in front of the window's
17093 display area. Start displaying at the start of the line
17094 containing PT in this case. */
17095 if (it.current_y <= 0)
17097 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
17098 move_it_vertically_backward (&it, 0);
17099 it.current_y = 0;
17102 it.current_x = it.hpos = 0;
17104 /* Set the window start position here explicitly, to avoid an
17105 infinite loop in case the functions in window-scroll-functions
17106 get errors. */
17107 set_marker_both (w->start, Qnil, IT_CHARPOS (it), IT_BYTEPOS (it));
17109 /* Run scroll hooks. */
17110 startp = run_window_scroll_functions (window, it.current.pos);
17112 /* We invoke try_window and try_window_reusing_current_matrix below,
17113 and they manipulate the bidi cache. Save and restore the cache
17114 state of our iterator, so we could continue using it after that. */
17115 itdata = bidi_shelve_cache ();
17117 /* Redisplay the window. */
17118 use_desired_matrix = false;
17119 if (!current_matrix_up_to_date_p
17120 || windows_or_buffers_changed
17121 || f->cursor_type_changed
17122 /* Don't use try_window_reusing_current_matrix in this case
17123 because it can have changed the buffer. */
17124 || !NILP (Vwindow_scroll_functions)
17125 || !just_this_one_p
17126 || MINI_WINDOW_P (w)
17127 || !(used_current_matrix_p
17128 = try_window_reusing_current_matrix (w)))
17129 use_desired_matrix = (try_window (window, startp, 0) == 1);
17131 bidi_unshelve_cache (itdata, false);
17133 /* If new fonts have been loaded (due to fontsets), give up. We
17134 have to start a new redisplay since we need to re-adjust glyph
17135 matrices. */
17136 if (f->fonts_changed)
17137 goto need_larger_matrices;
17139 /* If cursor did not appear assume that the middle of the window is
17140 in the first line of the window. Do it again with the next line.
17141 (Imagine a window of height 100, displaying two lines of height
17142 60. Moving back 50 from it->last_visible_y will end in the first
17143 line.) */
17144 if (w->cursor.vpos < 0)
17146 if (w->window_end_valid && PT >= Z - w->window_end_pos)
17148 clear_glyph_matrix (w->desired_matrix);
17149 move_it_by_lines (&it, 1);
17150 try_window (window, it.current.pos, 0);
17152 else if (PT < IT_CHARPOS (it))
17154 clear_glyph_matrix (w->desired_matrix);
17155 move_it_by_lines (&it, -1);
17156 try_window (window, it.current.pos, 0);
17158 else if (scroll_conservatively > SCROLL_LIMIT
17159 && (it.method == GET_FROM_STRING
17160 || overlay_touches_p (IT_CHARPOS (it)))
17161 && IT_CHARPOS (it) < ZV)
17163 /* If the window starts with a before-string that spans more
17164 than one screen line, using that position to display the
17165 window might fail to bring point into the view, because
17166 start_display will always start by displaying the string,
17167 whereas the code above determines where to set w->start
17168 by the buffer position of the place where it takes screen
17169 coordinates. Try to recover by finding the next screen
17170 line that displays buffer text. */
17171 ptrdiff_t pos0 = IT_CHARPOS (it);
17173 clear_glyph_matrix (w->desired_matrix);
17174 do {
17175 move_it_by_lines (&it, 1);
17176 } while (IT_CHARPOS (it) == pos0);
17177 try_window (window, it.current.pos, 0);
17179 else
17181 /* Not much we can do about it. */
17185 /* Consider the following case: Window starts at BEGV, there is
17186 invisible, intangible text at BEGV, so that display starts at
17187 some point START > BEGV. It can happen that we are called with
17188 PT somewhere between BEGV and START. Try to handle that case,
17189 and similar ones. */
17190 if (w->cursor.vpos < 0)
17192 /* Prefer the desired matrix to the current matrix, if possible,
17193 in the fallback calculations below. This is because using
17194 the current matrix might completely goof, e.g. if its first
17195 row is after point. */
17196 struct glyph_matrix *matrix =
17197 use_desired_matrix ? w->desired_matrix : w->current_matrix;
17198 /* First, try locating the proper glyph row for PT. */
17199 struct glyph_row *row =
17200 row_containing_pos (w, PT, matrix->rows, NULL, 0);
17202 /* Sometimes point is at the beginning of invisible text that is
17203 before the 1st character displayed in the row. In that case,
17204 row_containing_pos fails to find the row, because no glyphs
17205 with appropriate buffer positions are present in the row.
17206 Therefore, we next try to find the row which shows the 1st
17207 position after the invisible text. */
17208 if (!row)
17210 Lisp_Object val =
17211 get_char_property_and_overlay (make_number (PT), Qinvisible,
17212 Qnil, NULL);
17214 if (TEXT_PROP_MEANS_INVISIBLE (val) != 0)
17216 ptrdiff_t alt_pos;
17217 Lisp_Object invis_end =
17218 Fnext_single_char_property_change (make_number (PT), Qinvisible,
17219 Qnil, Qnil);
17221 if (NATNUMP (invis_end))
17222 alt_pos = XFASTINT (invis_end);
17223 else
17224 alt_pos = ZV;
17225 row = row_containing_pos (w, alt_pos, matrix->rows, NULL, 0);
17228 /* Finally, fall back on the first row of the window after the
17229 header line (if any). This is slightly better than not
17230 displaying the cursor at all. */
17231 if (!row)
17233 row = matrix->rows;
17234 if (row->mode_line_p)
17235 ++row;
17237 set_cursor_from_row (w, row, matrix, 0, 0, 0, 0);
17240 if (!cursor_row_fully_visible_p (w, false, false))
17242 /* If vscroll is enabled, disable it and try again. */
17243 if (w->vscroll)
17245 w->vscroll = 0;
17246 clear_glyph_matrix (w->desired_matrix);
17247 goto recenter;
17250 /* Users who set scroll-conservatively to a large number want
17251 point just above/below the scroll margin. If we ended up
17252 with point's row partially visible, move the window start to
17253 make that row fully visible and out of the margin. */
17254 if (scroll_conservatively > SCROLL_LIMIT)
17256 int window_total_lines
17257 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
17258 bool move_down = w->cursor.vpos >= window_total_lines / 2;
17260 move_it_by_lines (&it, move_down ? margin + 1 : -(margin + 1));
17261 clear_glyph_matrix (w->desired_matrix);
17262 if (1 == try_window (window, it.current.pos,
17263 TRY_WINDOW_CHECK_MARGINS))
17264 goto done;
17267 /* If centering point failed to make the whole line visible,
17268 put point at the top instead. That has to make the whole line
17269 visible, if it can be done. */
17270 if (centering_position == 0)
17271 goto done;
17273 clear_glyph_matrix (w->desired_matrix);
17274 centering_position = 0;
17275 goto recenter;
17278 done:
17280 SET_TEXT_POS_FROM_MARKER (startp, w->start);
17281 w->start_at_line_beg = (CHARPOS (startp) == BEGV
17282 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n');
17284 /* Display the mode line, if we must. */
17285 if ((update_mode_line
17286 /* If window not full width, must redo its mode line
17287 if (a) the window to its side is being redone and
17288 (b) we do a frame-based redisplay. This is a consequence
17289 of how inverted lines are drawn in frame-based redisplay. */
17290 || (!just_this_one_p
17291 && !FRAME_WINDOW_P (f)
17292 && !WINDOW_FULL_WIDTH_P (w))
17293 /* Line number to display. */
17294 || w->base_line_pos > 0
17295 /* Column number is displayed and different from the one displayed. */
17296 || (w->column_number_displayed != -1
17297 && (w->column_number_displayed != current_column ())))
17298 /* This means that the window has a mode line. */
17299 && (WINDOW_WANTS_MODELINE_P (w)
17300 || WINDOW_WANTS_HEADER_LINE_P (w)))
17303 display_mode_lines (w);
17305 /* If mode line height has changed, arrange for a thorough
17306 immediate redisplay using the correct mode line height. */
17307 if (WINDOW_WANTS_MODELINE_P (w)
17308 && CURRENT_MODE_LINE_HEIGHT (w) != DESIRED_MODE_LINE_HEIGHT (w))
17310 f->fonts_changed = true;
17311 w->mode_line_height = -1;
17312 MATRIX_MODE_LINE_ROW (w->current_matrix)->height
17313 = DESIRED_MODE_LINE_HEIGHT (w);
17316 /* If header line height has changed, arrange for a thorough
17317 immediate redisplay using the correct header line height. */
17318 if (WINDOW_WANTS_HEADER_LINE_P (w)
17319 && CURRENT_HEADER_LINE_HEIGHT (w) != DESIRED_HEADER_LINE_HEIGHT (w))
17321 f->fonts_changed = true;
17322 w->header_line_height = -1;
17323 MATRIX_HEADER_LINE_ROW (w->current_matrix)->height
17324 = DESIRED_HEADER_LINE_HEIGHT (w);
17327 if (f->fonts_changed)
17328 goto need_larger_matrices;
17331 if (!line_number_displayed && w->base_line_pos != -1)
17333 w->base_line_pos = 0;
17334 w->base_line_number = 0;
17337 finish_menu_bars:
17339 /* When we reach a frame's selected window, redo the frame's menu
17340 bar and the frame's title. */
17341 if (update_mode_line
17342 && EQ (FRAME_SELECTED_WINDOW (f), window))
17344 bool redisplay_menu_p;
17346 if (FRAME_WINDOW_P (f))
17348 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
17349 || defined (HAVE_NS) || defined (USE_GTK)
17350 redisplay_menu_p = FRAME_EXTERNAL_MENU_BAR (f);
17351 #else
17352 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
17353 #endif
17355 else
17356 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
17358 if (redisplay_menu_p)
17359 display_menu_bar (w);
17361 #ifdef HAVE_WINDOW_SYSTEM
17362 if (FRAME_WINDOW_P (f))
17364 #if defined (USE_GTK) || defined (HAVE_NS)
17365 if (FRAME_EXTERNAL_TOOL_BAR (f))
17366 redisplay_tool_bar (f);
17367 #else
17368 if (WINDOWP (f->tool_bar_window)
17369 && (FRAME_TOOL_BAR_LINES (f) > 0
17370 || !NILP (Vauto_resize_tool_bars))
17371 && redisplay_tool_bar (f))
17372 ignore_mouse_drag_p = true;
17373 #endif
17375 x_consider_frame_title (w->frame);
17376 #endif
17379 #ifdef HAVE_WINDOW_SYSTEM
17380 if (FRAME_WINDOW_P (f)
17381 && update_window_fringes (w, (just_this_one_p
17382 || (!used_current_matrix_p && !overlay_arrow_seen)
17383 || w->pseudo_window_p)))
17385 update_begin (f);
17386 block_input ();
17387 if (draw_window_fringes (w, true))
17389 if (WINDOW_RIGHT_DIVIDER_WIDTH (w))
17390 x_draw_right_divider (w);
17391 else
17392 x_draw_vertical_border (w);
17394 unblock_input ();
17395 update_end (f);
17398 if (WINDOW_BOTTOM_DIVIDER_WIDTH (w))
17399 x_draw_bottom_divider (w);
17400 #endif /* HAVE_WINDOW_SYSTEM */
17402 /* We go to this label, with fonts_changed set, if it is
17403 necessary to try again using larger glyph matrices.
17404 We have to redeem the scroll bar even in this case,
17405 because the loop in redisplay_internal expects that. */
17406 need_larger_matrices:
17408 finish_scroll_bars:
17410 if (WINDOW_HAS_VERTICAL_SCROLL_BAR (w) || WINDOW_HAS_HORIZONTAL_SCROLL_BAR (w))
17412 if (WINDOW_HAS_VERTICAL_SCROLL_BAR (w))
17413 /* Set the thumb's position and size. */
17414 set_vertical_scroll_bar (w);
17416 if (WINDOW_HAS_HORIZONTAL_SCROLL_BAR (w))
17417 /* Set the thumb's position and size. */
17418 set_horizontal_scroll_bar (w);
17420 /* Note that we actually used the scroll bar attached to this
17421 window, so it shouldn't be deleted at the end of redisplay. */
17422 if (FRAME_TERMINAL (f)->redeem_scroll_bar_hook)
17423 (*FRAME_TERMINAL (f)->redeem_scroll_bar_hook) (w);
17426 /* Restore current_buffer and value of point in it. The window
17427 update may have changed the buffer, so first make sure `opoint'
17428 is still valid (Bug#6177). */
17429 if (CHARPOS (opoint) < BEGV)
17430 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
17431 else if (CHARPOS (opoint) > ZV)
17432 TEMP_SET_PT_BOTH (Z, Z_BYTE);
17433 else
17434 TEMP_SET_PT_BOTH (CHARPOS (opoint), BYTEPOS (opoint));
17436 set_buffer_internal_1 (old);
17437 /* Avoid an abort in TEMP_SET_PT_BOTH if the buffer has become
17438 shorter. This can be caused by log truncation in *Messages*. */
17439 if (CHARPOS (lpoint) <= ZV)
17440 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
17442 unbind_to (count, Qnil);
17446 /* Build the complete desired matrix of WINDOW with a window start
17447 buffer position POS.
17449 Value is 1 if successful. It is zero if fonts were loaded during
17450 redisplay which makes re-adjusting glyph matrices necessary, and -1
17451 if point would appear in the scroll margins.
17452 (We check the former only if TRY_WINDOW_IGNORE_FONTS_CHANGE is
17453 unset in FLAGS, and the latter only if TRY_WINDOW_CHECK_MARGINS is
17454 set in FLAGS.) */
17457 try_window (Lisp_Object window, struct text_pos pos, int flags)
17459 struct window *w = XWINDOW (window);
17460 struct it it;
17461 struct glyph_row *last_text_row = NULL;
17462 struct frame *f = XFRAME (w->frame);
17463 int cursor_vpos = w->cursor.vpos;
17465 /* Make POS the new window start. */
17466 set_marker_both (w->start, Qnil, CHARPOS (pos), BYTEPOS (pos));
17468 /* Mark cursor position as unknown. No overlay arrow seen. */
17469 w->cursor.vpos = -1;
17470 overlay_arrow_seen = false;
17472 /* Initialize iterator and info to start at POS. */
17473 start_display (&it, w, pos);
17474 it.glyph_row->reversed_p = false;
17476 /* Display all lines of W. */
17477 while (it.current_y < it.last_visible_y)
17479 if (display_line (&it, cursor_vpos))
17480 last_text_row = it.glyph_row - 1;
17481 if (f->fonts_changed && !(flags & TRY_WINDOW_IGNORE_FONTS_CHANGE))
17482 return 0;
17485 /* Save the character position of 'it' before we call
17486 'start_display' again. */
17487 ptrdiff_t it_charpos = IT_CHARPOS (it);
17489 /* Don't let the cursor end in the scroll margins. */
17490 if ((flags & TRY_WINDOW_CHECK_MARGINS)
17491 && !MINI_WINDOW_P (w))
17493 int this_scroll_margin = window_scroll_margin (w, MARGIN_IN_PIXELS);
17494 start_display (&it, w, pos);
17496 if ((w->cursor.y >= 0 /* not vscrolled */
17497 && w->cursor.y < this_scroll_margin
17498 && CHARPOS (pos) > BEGV
17499 && it_charpos < ZV)
17500 /* rms: considering make_cursor_line_fully_visible_p here
17501 seems to give wrong results. We don't want to recenter
17502 when the last line is partly visible, we want to allow
17503 that case to be handled in the usual way. */
17504 || w->cursor.y > (it.last_visible_y - partial_line_height (&it)
17505 - this_scroll_margin - 1))
17507 w->cursor.vpos = -1;
17508 clear_glyph_matrix (w->desired_matrix);
17509 return -1;
17513 /* If bottom moved off end of frame, change mode line percentage. */
17514 if (w->window_end_pos <= 0 && Z != it_charpos)
17515 w->update_mode_line = true;
17517 /* Set window_end_pos to the offset of the last character displayed
17518 on the window from the end of current_buffer. Set
17519 window_end_vpos to its row number. */
17520 if (last_text_row)
17522 eassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_text_row));
17523 adjust_window_ends (w, last_text_row, false);
17524 eassert
17525 (MATRIX_ROW_DISPLAYS_TEXT_P (MATRIX_ROW (w->desired_matrix,
17526 w->window_end_vpos)));
17528 else
17530 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
17531 w->window_end_pos = Z - ZV;
17532 w->window_end_vpos = 0;
17535 /* But that is not valid info until redisplay finishes. */
17536 w->window_end_valid = false;
17537 return 1;
17542 /************************************************************************
17543 Window redisplay reusing current matrix when buffer has not changed
17544 ************************************************************************/
17546 /* Try redisplay of window W showing an unchanged buffer with a
17547 different window start than the last time it was displayed by
17548 reusing its current matrix. Value is true if successful.
17549 W->start is the new window start. */
17551 static bool
17552 try_window_reusing_current_matrix (struct window *w)
17554 struct frame *f = XFRAME (w->frame);
17555 struct glyph_row *bottom_row;
17556 struct it it;
17557 struct run run;
17558 struct text_pos start, new_start;
17559 int nrows_scrolled, i;
17560 struct glyph_row *last_text_row;
17561 struct glyph_row *last_reused_text_row;
17562 struct glyph_row *start_row;
17563 int start_vpos, min_y, max_y;
17565 #ifdef GLYPH_DEBUG
17566 if (inhibit_try_window_reusing)
17567 return false;
17568 #endif
17570 if (/* This function doesn't handle terminal frames. */
17571 !FRAME_WINDOW_P (f)
17572 /* Don't try to reuse the display if windows have been split
17573 or such. */
17574 || windows_or_buffers_changed
17575 || f->cursor_type_changed)
17576 return false;
17578 /* Can't do this if showing trailing whitespace. */
17579 if (!NILP (Vshow_trailing_whitespace))
17580 return false;
17582 /* If top-line visibility has changed, give up. */
17583 if (WINDOW_WANTS_HEADER_LINE_P (w)
17584 != MATRIX_HEADER_LINE_ROW (w->current_matrix)->mode_line_p)
17585 return false;
17587 /* Give up if old or new display is scrolled vertically. We could
17588 make this function handle this, but right now it doesn't. */
17589 start_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
17590 if (w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row))
17591 return false;
17593 /* The variable new_start now holds the new window start. The old
17594 start `start' can be determined from the current matrix. */
17595 SET_TEXT_POS_FROM_MARKER (new_start, w->start);
17596 start = start_row->minpos;
17597 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
17599 /* Clear the desired matrix for the display below. */
17600 clear_glyph_matrix (w->desired_matrix);
17602 if (CHARPOS (new_start) <= CHARPOS (start))
17604 /* Don't use this method if the display starts with an ellipsis
17605 displayed for invisible text. It's not easy to handle that case
17606 below, and it's certainly not worth the effort since this is
17607 not a frequent case. */
17608 if (in_ellipses_for_invisible_text_p (&start_row->start, w))
17609 return false;
17611 IF_DEBUG (debug_method_add (w, "twu1"));
17613 /* Display up to a row that can be reused. The variable
17614 last_text_row is set to the last row displayed that displays
17615 text. Note that it.vpos == 0 if or if not there is a
17616 header-line; it's not the same as the MATRIX_ROW_VPOS! */
17617 start_display (&it, w, new_start);
17618 w->cursor.vpos = -1;
17619 last_text_row = last_reused_text_row = NULL;
17621 while (it.current_y < it.last_visible_y && !f->fonts_changed)
17623 /* If we have reached into the characters in the START row,
17624 that means the line boundaries have changed. So we
17625 can't start copying with the row START. Maybe it will
17626 work to start copying with the following row. */
17627 while (IT_CHARPOS (it) > CHARPOS (start))
17629 /* Advance to the next row as the "start". */
17630 start_row++;
17631 start = start_row->minpos;
17632 /* If there are no more rows to try, or just one, give up. */
17633 if (start_row == MATRIX_MODE_LINE_ROW (w->current_matrix) - 1
17634 || w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row)
17635 || CHARPOS (start) == ZV)
17637 clear_glyph_matrix (w->desired_matrix);
17638 return false;
17641 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
17643 /* If we have reached alignment, we can copy the rest of the
17644 rows. */
17645 if (IT_CHARPOS (it) == CHARPOS (start)
17646 /* Don't accept "alignment" inside a display vector,
17647 since start_row could have started in the middle of
17648 that same display vector (thus their character
17649 positions match), and we have no way of telling if
17650 that is the case. */
17651 && it.current.dpvec_index < 0)
17652 break;
17654 it.glyph_row->reversed_p = false;
17655 if (display_line (&it, -1))
17656 last_text_row = it.glyph_row - 1;
17660 /* A value of current_y < last_visible_y means that we stopped
17661 at the previous window start, which in turn means that we
17662 have at least one reusable row. */
17663 if (it.current_y < it.last_visible_y)
17665 struct glyph_row *row;
17667 /* IT.vpos always starts from 0; it counts text lines. */
17668 nrows_scrolled = it.vpos - (start_row - MATRIX_FIRST_TEXT_ROW (w->current_matrix));
17670 /* Find PT if not already found in the lines displayed. */
17671 if (w->cursor.vpos < 0)
17673 int dy = it.current_y - start_row->y;
17675 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
17676 row = row_containing_pos (w, PT, row, NULL, dy);
17677 if (row)
17678 set_cursor_from_row (w, row, w->current_matrix, 0, 0,
17679 dy, nrows_scrolled);
17680 else
17682 clear_glyph_matrix (w->desired_matrix);
17683 return false;
17687 /* Scroll the display. Do it before the current matrix is
17688 changed. The problem here is that update has not yet
17689 run, i.e. part of the current matrix is not up to date.
17690 scroll_run_hook will clear the cursor, and use the
17691 current matrix to get the height of the row the cursor is
17692 in. */
17693 run.current_y = start_row->y;
17694 run.desired_y = it.current_y;
17695 run.height = it.last_visible_y - it.current_y;
17697 if (run.height > 0 && run.current_y != run.desired_y)
17699 update_begin (f);
17700 FRAME_RIF (f)->update_window_begin_hook (w);
17701 FRAME_RIF (f)->clear_window_mouse_face (w);
17702 FRAME_RIF (f)->scroll_run_hook (w, &run);
17703 FRAME_RIF (f)->update_window_end_hook (w, false, false);
17704 update_end (f);
17707 /* Shift current matrix down by nrows_scrolled lines. */
17708 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
17709 rotate_matrix (w->current_matrix,
17710 start_vpos,
17711 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
17712 nrows_scrolled);
17714 /* Disable lines that must be updated. */
17715 for (i = 0; i < nrows_scrolled; ++i)
17716 (start_row + i)->enabled_p = false;
17718 /* Re-compute Y positions. */
17719 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
17720 max_y = it.last_visible_y;
17721 for (row = start_row + nrows_scrolled;
17722 row < bottom_row;
17723 ++row)
17725 row->y = it.current_y;
17726 row->visible_height = row->height;
17728 if (row->y < min_y)
17729 row->visible_height -= min_y - row->y;
17730 if (row->y + row->height > max_y)
17731 row->visible_height -= row->y + row->height - max_y;
17732 if (row->fringe_bitmap_periodic_p)
17733 row->redraw_fringe_bitmaps_p = true;
17735 it.current_y += row->height;
17737 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
17738 last_reused_text_row = row;
17739 if (MATRIX_ROW_BOTTOM_Y (row) >= it.last_visible_y)
17740 break;
17743 /* Disable lines in the current matrix which are now
17744 below the window. */
17745 for (++row; row < bottom_row; ++row)
17746 row->enabled_p = row->mode_line_p = false;
17749 /* Update window_end_pos etc.; last_reused_text_row is the last
17750 reused row from the current matrix containing text, if any.
17751 The value of last_text_row is the last displayed line
17752 containing text. */
17753 if (last_reused_text_row)
17754 adjust_window_ends (w, last_reused_text_row, true);
17755 else if (last_text_row)
17756 adjust_window_ends (w, last_text_row, false);
17757 else
17759 /* This window must be completely empty. */
17760 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
17761 w->window_end_pos = Z - ZV;
17762 w->window_end_vpos = 0;
17764 w->window_end_valid = false;
17766 /* Update hint: don't try scrolling again in update_window. */
17767 w->desired_matrix->no_scrolling_p = true;
17769 #ifdef GLYPH_DEBUG
17770 debug_method_add (w, "try_window_reusing_current_matrix 1");
17771 #endif
17772 return true;
17774 else if (CHARPOS (new_start) > CHARPOS (start))
17776 struct glyph_row *pt_row, *row;
17777 struct glyph_row *first_reusable_row;
17778 struct glyph_row *first_row_to_display;
17779 int dy;
17780 int yb = window_text_bottom_y (w);
17782 /* Find the row starting at new_start, if there is one. Don't
17783 reuse a partially visible line at the end. */
17784 first_reusable_row = start_row;
17785 while (first_reusable_row->enabled_p
17786 && MATRIX_ROW_BOTTOM_Y (first_reusable_row) < yb
17787 && (MATRIX_ROW_START_CHARPOS (first_reusable_row)
17788 < CHARPOS (new_start)))
17789 ++first_reusable_row;
17791 /* Give up if there is no row to reuse. */
17792 if (MATRIX_ROW_BOTTOM_Y (first_reusable_row) >= yb
17793 || !first_reusable_row->enabled_p
17794 || (MATRIX_ROW_START_CHARPOS (first_reusable_row)
17795 != CHARPOS (new_start)))
17796 return false;
17798 /* We can reuse fully visible rows beginning with
17799 first_reusable_row to the end of the window. Set
17800 first_row_to_display to the first row that cannot be reused.
17801 Set pt_row to the row containing point, if there is any. */
17802 pt_row = NULL;
17803 for (first_row_to_display = first_reusable_row;
17804 MATRIX_ROW_BOTTOM_Y (first_row_to_display) < yb;
17805 ++first_row_to_display)
17807 if (PT >= MATRIX_ROW_START_CHARPOS (first_row_to_display)
17808 && (PT < MATRIX_ROW_END_CHARPOS (first_row_to_display)
17809 || (PT == MATRIX_ROW_END_CHARPOS (first_row_to_display)
17810 && first_row_to_display->ends_at_zv_p
17811 && pt_row == NULL)))
17812 pt_row = first_row_to_display;
17815 /* Start displaying at the start of first_row_to_display. */
17816 eassert (first_row_to_display->y < yb);
17817 init_to_row_start (&it, w, first_row_to_display);
17819 nrows_scrolled = (MATRIX_ROW_VPOS (first_reusable_row, w->current_matrix)
17820 - start_vpos);
17821 it.vpos = (MATRIX_ROW_VPOS (first_row_to_display, w->current_matrix)
17822 - nrows_scrolled);
17823 it.current_y = (first_row_to_display->y - first_reusable_row->y
17824 + WINDOW_HEADER_LINE_HEIGHT (w));
17826 /* Display lines beginning with first_row_to_display in the
17827 desired matrix. Set last_text_row to the last row displayed
17828 that displays text. */
17829 it.glyph_row = MATRIX_ROW (w->desired_matrix, it.vpos);
17830 if (pt_row == NULL)
17831 w->cursor.vpos = -1;
17832 last_text_row = NULL;
17833 while (it.current_y < it.last_visible_y && !f->fonts_changed)
17834 if (display_line (&it, w->cursor.vpos))
17835 last_text_row = it.glyph_row - 1;
17837 /* If point is in a reused row, adjust y and vpos of the cursor
17838 position. */
17839 if (pt_row)
17841 w->cursor.vpos -= nrows_scrolled;
17842 w->cursor.y -= first_reusable_row->y - start_row->y;
17845 /* Give up if point isn't in a row displayed or reused. (This
17846 also handles the case where w->cursor.vpos < nrows_scrolled
17847 after the calls to display_line, which can happen with scroll
17848 margins. See bug#1295.) */
17849 if (w->cursor.vpos < 0)
17851 clear_glyph_matrix (w->desired_matrix);
17852 return false;
17855 /* Scroll the display. */
17856 run.current_y = first_reusable_row->y;
17857 run.desired_y = WINDOW_HEADER_LINE_HEIGHT (w);
17858 run.height = it.last_visible_y - run.current_y;
17859 dy = run.current_y - run.desired_y;
17861 if (run.height)
17863 update_begin (f);
17864 FRAME_RIF (f)->update_window_begin_hook (w);
17865 FRAME_RIF (f)->clear_window_mouse_face (w);
17866 FRAME_RIF (f)->scroll_run_hook (w, &run);
17867 FRAME_RIF (f)->update_window_end_hook (w, false, false);
17868 update_end (f);
17871 /* Adjust Y positions of reused rows. */
17872 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
17873 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
17874 max_y = it.last_visible_y;
17875 for (row = first_reusable_row; row < first_row_to_display; ++row)
17877 row->y -= dy;
17878 row->visible_height = row->height;
17879 if (row->y < min_y)
17880 row->visible_height -= min_y - row->y;
17881 if (row->y + row->height > max_y)
17882 row->visible_height -= row->y + row->height - max_y;
17883 if (row->fringe_bitmap_periodic_p)
17884 row->redraw_fringe_bitmaps_p = true;
17887 /* Scroll the current matrix. */
17888 eassert (nrows_scrolled > 0);
17889 rotate_matrix (w->current_matrix,
17890 start_vpos,
17891 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
17892 -nrows_scrolled);
17894 /* Disable rows not reused. */
17895 for (row -= nrows_scrolled; row < bottom_row; ++row)
17896 row->enabled_p = false;
17898 /* Point may have moved to a different line, so we cannot assume that
17899 the previous cursor position is valid; locate the correct row. */
17900 if (pt_row)
17902 for (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
17903 row < bottom_row
17904 && PT >= MATRIX_ROW_END_CHARPOS (row)
17905 && !row->ends_at_zv_p;
17906 row++)
17908 w->cursor.vpos++;
17909 w->cursor.y = row->y;
17911 if (row < bottom_row)
17913 /* Can't simply scan the row for point with
17914 bidi-reordered glyph rows. Let set_cursor_from_row
17915 figure out where to put the cursor, and if it fails,
17916 give up. */
17917 if (!NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering)))
17919 if (!set_cursor_from_row (w, row, w->current_matrix,
17920 0, 0, 0, 0))
17922 clear_glyph_matrix (w->desired_matrix);
17923 return false;
17926 else
17928 struct glyph *glyph = row->glyphs[TEXT_AREA] + w->cursor.hpos;
17929 struct glyph *end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
17931 for (; glyph < end
17932 && (!BUFFERP (glyph->object)
17933 || glyph->charpos < PT);
17934 glyph++)
17936 w->cursor.hpos++;
17937 w->cursor.x += glyph->pixel_width;
17943 /* Adjust window end. A null value of last_text_row means that
17944 the window end is in reused rows which in turn means that
17945 only its vpos can have changed. */
17946 if (last_text_row)
17947 adjust_window_ends (w, last_text_row, false);
17948 else
17949 w->window_end_vpos -= nrows_scrolled;
17951 w->window_end_valid = false;
17952 w->desired_matrix->no_scrolling_p = true;
17954 #ifdef GLYPH_DEBUG
17955 debug_method_add (w, "try_window_reusing_current_matrix 2");
17956 #endif
17957 return true;
17960 return false;
17965 /************************************************************************
17966 Window redisplay reusing current matrix when buffer has changed
17967 ************************************************************************/
17969 static struct glyph_row *find_last_unchanged_at_beg_row (struct window *);
17970 static struct glyph_row *find_first_unchanged_at_end_row (struct window *,
17971 ptrdiff_t *, ptrdiff_t *);
17972 static struct glyph_row *
17973 find_last_row_displaying_text (struct glyph_matrix *, struct it *,
17974 struct glyph_row *);
17977 /* Return the last row in MATRIX displaying text. If row START is
17978 non-null, start searching with that row. IT gives the dimensions
17979 of the display. Value is null if matrix is empty; otherwise it is
17980 a pointer to the row found. */
17982 static struct glyph_row *
17983 find_last_row_displaying_text (struct glyph_matrix *matrix, struct it *it,
17984 struct glyph_row *start)
17986 struct glyph_row *row, *row_found;
17988 /* Set row_found to the last row in IT->w's current matrix
17989 displaying text. The loop looks funny but think of partially
17990 visible lines. */
17991 row_found = NULL;
17992 row = start ? start : MATRIX_FIRST_TEXT_ROW (matrix);
17993 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
17995 eassert (row->enabled_p);
17996 row_found = row;
17997 if (MATRIX_ROW_BOTTOM_Y (row) >= it->last_visible_y)
17998 break;
17999 ++row;
18002 return row_found;
18006 /* Return the last row in the current matrix of W that is not affected
18007 by changes at the start of current_buffer that occurred since W's
18008 current matrix was built. Value is null if no such row exists.
18010 BEG_UNCHANGED us the number of characters unchanged at the start of
18011 current_buffer. BEG + BEG_UNCHANGED is the buffer position of the
18012 first changed character in current_buffer. Characters at positions <
18013 BEG + BEG_UNCHANGED are at the same buffer positions as they were
18014 when the current matrix was built. */
18016 static struct glyph_row *
18017 find_last_unchanged_at_beg_row (struct window *w)
18019 ptrdiff_t first_changed_pos = BEG + BEG_UNCHANGED;
18020 struct glyph_row *row;
18021 struct glyph_row *row_found = NULL;
18022 int yb = window_text_bottom_y (w);
18024 /* Find the last row displaying unchanged text. */
18025 for (row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
18026 MATRIX_ROW_DISPLAYS_TEXT_P (row)
18027 && MATRIX_ROW_START_CHARPOS (row) < first_changed_pos;
18028 ++row)
18030 if (/* If row ends before first_changed_pos, it is unchanged,
18031 except in some case. */
18032 MATRIX_ROW_END_CHARPOS (row) <= first_changed_pos
18033 /* When row ends in ZV and we write at ZV it is not
18034 unchanged. */
18035 && !row->ends_at_zv_p
18036 /* When first_changed_pos is the end of a continued line,
18037 row is not unchanged because it may be no longer
18038 continued. */
18039 && !(MATRIX_ROW_END_CHARPOS (row) == first_changed_pos
18040 && (row->continued_p
18041 || row->exact_window_width_line_p))
18042 /* If ROW->end is beyond ZV, then ROW->end is outdated and
18043 needs to be recomputed, so don't consider this row as
18044 unchanged. This happens when the last line was
18045 bidi-reordered and was killed immediately before this
18046 redisplay cycle. In that case, ROW->end stores the
18047 buffer position of the first visual-order character of
18048 the killed text, which is now beyond ZV. */
18049 && CHARPOS (row->end.pos) <= ZV)
18050 row_found = row;
18052 /* Stop if last visible row. */
18053 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
18054 break;
18057 return row_found;
18061 /* Find the first glyph row in the current matrix of W that is not
18062 affected by changes at the end of current_buffer since the
18063 time W's current matrix was built.
18065 Return in *DELTA the number of chars by which buffer positions in
18066 unchanged text at the end of current_buffer must be adjusted.
18068 Return in *DELTA_BYTES the corresponding number of bytes.
18070 Value is null if no such row exists, i.e. all rows are affected by
18071 changes. */
18073 static struct glyph_row *
18074 find_first_unchanged_at_end_row (struct window *w,
18075 ptrdiff_t *delta, ptrdiff_t *delta_bytes)
18077 struct glyph_row *row;
18078 struct glyph_row *row_found = NULL;
18080 *delta = *delta_bytes = 0;
18082 /* Display must not have been paused, otherwise the current matrix
18083 is not up to date. */
18084 eassert (w->window_end_valid);
18086 /* A value of window_end_pos >= END_UNCHANGED means that the window
18087 end is in the range of changed text. If so, there is no
18088 unchanged row at the end of W's current matrix. */
18089 if (w->window_end_pos >= END_UNCHANGED)
18090 return NULL;
18092 /* Set row to the last row in W's current matrix displaying text. */
18093 row = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
18095 /* If matrix is entirely empty, no unchanged row exists. */
18096 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
18098 /* The value of row is the last glyph row in the matrix having a
18099 meaningful buffer position in it. The end position of row
18100 corresponds to window_end_pos. This allows us to translate
18101 buffer positions in the current matrix to current buffer
18102 positions for characters not in changed text. */
18103 ptrdiff_t Z_old =
18104 MATRIX_ROW_END_CHARPOS (row) + w->window_end_pos;
18105 ptrdiff_t Z_BYTE_old =
18106 MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
18107 ptrdiff_t last_unchanged_pos, last_unchanged_pos_old;
18108 struct glyph_row *first_text_row
18109 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
18111 *delta = Z - Z_old;
18112 *delta_bytes = Z_BYTE - Z_BYTE_old;
18114 /* Set last_unchanged_pos to the buffer position of the last
18115 character in the buffer that has not been changed. Z is the
18116 index + 1 of the last character in current_buffer, i.e. by
18117 subtracting END_UNCHANGED we get the index of the last
18118 unchanged character, and we have to add BEG to get its buffer
18119 position. */
18120 last_unchanged_pos = Z - END_UNCHANGED + BEG;
18121 last_unchanged_pos_old = last_unchanged_pos - *delta;
18123 /* Search backward from ROW for a row displaying a line that
18124 starts at a minimum position >= last_unchanged_pos_old. */
18125 for (; row > first_text_row; --row)
18127 /* This used to abort, but it can happen.
18128 It is ok to just stop the search instead here. KFS. */
18129 if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
18130 break;
18132 if (MATRIX_ROW_START_CHARPOS (row) >= last_unchanged_pos_old)
18133 row_found = row;
18137 eassert (!row_found || MATRIX_ROW_DISPLAYS_TEXT_P (row_found));
18139 return row_found;
18143 /* Make sure that glyph rows in the current matrix of window W
18144 reference the same glyph memory as corresponding rows in the
18145 frame's frame matrix. This function is called after scrolling W's
18146 current matrix on a terminal frame in try_window_id and
18147 try_window_reusing_current_matrix. */
18149 static void
18150 sync_frame_with_window_matrix_rows (struct window *w)
18152 struct frame *f = XFRAME (w->frame);
18153 struct glyph_row *window_row, *window_row_end, *frame_row;
18155 /* Preconditions: W must be a leaf window and full-width. Its frame
18156 must have a frame matrix. */
18157 eassert (BUFFERP (w->contents));
18158 eassert (WINDOW_FULL_WIDTH_P (w));
18159 eassert (!FRAME_WINDOW_P (f));
18161 /* If W is a full-width window, glyph pointers in W's current matrix
18162 have, by definition, to be the same as glyph pointers in the
18163 corresponding frame matrix. Note that frame matrices have no
18164 marginal areas (see build_frame_matrix). */
18165 window_row = w->current_matrix->rows;
18166 window_row_end = window_row + w->current_matrix->nrows;
18167 frame_row = f->current_matrix->rows + WINDOW_TOP_EDGE_LINE (w);
18168 while (window_row < window_row_end)
18170 struct glyph *start = window_row->glyphs[LEFT_MARGIN_AREA];
18171 struct glyph *end = window_row->glyphs[LAST_AREA];
18173 frame_row->glyphs[LEFT_MARGIN_AREA] = start;
18174 frame_row->glyphs[TEXT_AREA] = start;
18175 frame_row->glyphs[RIGHT_MARGIN_AREA] = end;
18176 frame_row->glyphs[LAST_AREA] = end;
18178 /* Disable frame rows whose corresponding window rows have
18179 been disabled in try_window_id. */
18180 if (!window_row->enabled_p)
18181 frame_row->enabled_p = false;
18183 ++window_row, ++frame_row;
18188 /* Find the glyph row in window W containing CHARPOS. Consider all
18189 rows between START and END (not inclusive). END null means search
18190 all rows to the end of the display area of W. Value is the row
18191 containing CHARPOS or null. */
18193 struct glyph_row *
18194 row_containing_pos (struct window *w, ptrdiff_t charpos,
18195 struct glyph_row *start, struct glyph_row *end, int dy)
18197 struct glyph_row *row = start;
18198 struct glyph_row *best_row = NULL;
18199 ptrdiff_t mindif = BUF_ZV (XBUFFER (w->contents)) + 1;
18200 int last_y;
18202 /* If we happen to start on a header-line, skip that. */
18203 if (row->mode_line_p)
18204 ++row;
18206 if ((end && row >= end) || !row->enabled_p)
18207 return NULL;
18209 last_y = window_text_bottom_y (w) - dy;
18211 while (true)
18213 /* Give up if we have gone too far. */
18214 if ((end && row >= end) || !row->enabled_p)
18215 return NULL;
18216 /* This formerly returned if they were equal.
18217 I think that both quantities are of a "last plus one" type;
18218 if so, when they are equal, the row is within the screen. -- rms. */
18219 if (MATRIX_ROW_BOTTOM_Y (row) > last_y)
18220 return NULL;
18222 /* If it is in this row, return this row. */
18223 if (! (MATRIX_ROW_END_CHARPOS (row) < charpos
18224 || (MATRIX_ROW_END_CHARPOS (row) == charpos
18225 /* The end position of a row equals the start
18226 position of the next row. If CHARPOS is there, we
18227 would rather consider it displayed in the next
18228 line, except when this line ends in ZV. */
18229 && !row_for_charpos_p (row, charpos)))
18230 && charpos >= MATRIX_ROW_START_CHARPOS (row))
18232 struct glyph *g;
18234 if (NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering))
18235 || (!best_row && !row->continued_p))
18236 return row;
18237 /* In bidi-reordered rows, there could be several rows whose
18238 edges surround CHARPOS, all of these rows belonging to
18239 the same continued line. We need to find the row which
18240 fits CHARPOS the best. */
18241 for (g = row->glyphs[TEXT_AREA];
18242 g < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
18243 g++)
18245 if (!STRINGP (g->object))
18247 if (g->charpos > 0 && eabs (g->charpos - charpos) < mindif)
18249 mindif = eabs (g->charpos - charpos);
18250 best_row = row;
18251 /* Exact match always wins. */
18252 if (mindif == 0)
18253 return best_row;
18258 else if (best_row && !row->continued_p)
18259 return best_row;
18260 ++row;
18265 /* Try to redisplay window W by reusing its existing display. W's
18266 current matrix must be up to date when this function is called,
18267 i.e., window_end_valid must be true.
18269 Value is
18271 >= 1 if successful, i.e. display has been updated
18272 specifically:
18273 1 means the changes were in front of a newline that precedes
18274 the window start, and the whole current matrix was reused
18275 2 means the changes were after the last position displayed
18276 in the window, and the whole current matrix was reused
18277 3 means portions of the current matrix were reused, while
18278 some of the screen lines were redrawn
18279 -1 if redisplay with same window start is known not to succeed
18280 0 if otherwise unsuccessful
18282 The following steps are performed:
18284 1. Find the last row in the current matrix of W that is not
18285 affected by changes at the start of current_buffer. If no such row
18286 is found, give up.
18288 2. Find the first row in W's current matrix that is not affected by
18289 changes at the end of current_buffer. Maybe there is no such row.
18291 3. Display lines beginning with the row + 1 found in step 1 to the
18292 row found in step 2 or, if step 2 didn't find a row, to the end of
18293 the window.
18295 4. If cursor is not known to appear on the window, give up.
18297 5. If display stopped at the row found in step 2, scroll the
18298 display and current matrix as needed.
18300 6. Maybe display some lines at the end of W, if we must. This can
18301 happen under various circumstances, like a partially visible line
18302 becoming fully visible, or because newly displayed lines are displayed
18303 in smaller font sizes.
18305 7. Update W's window end information. */
18307 static int
18308 try_window_id (struct window *w)
18310 struct frame *f = XFRAME (w->frame);
18311 struct glyph_matrix *current_matrix = w->current_matrix;
18312 struct glyph_matrix *desired_matrix = w->desired_matrix;
18313 struct glyph_row *last_unchanged_at_beg_row;
18314 struct glyph_row *first_unchanged_at_end_row;
18315 struct glyph_row *row;
18316 struct glyph_row *bottom_row;
18317 int bottom_vpos;
18318 struct it it;
18319 ptrdiff_t delta = 0, delta_bytes = 0, stop_pos;
18320 int dvpos, dy;
18321 struct text_pos start_pos;
18322 struct run run;
18323 int first_unchanged_at_end_vpos = 0;
18324 struct glyph_row *last_text_row, *last_text_row_at_end;
18325 struct text_pos start;
18326 ptrdiff_t first_changed_charpos, last_changed_charpos;
18328 #ifdef GLYPH_DEBUG
18329 if (inhibit_try_window_id)
18330 return 0;
18331 #endif
18333 /* This is handy for debugging. */
18334 #if false
18335 #define GIVE_UP(X) \
18336 do { \
18337 TRACE ((stderr, "try_window_id give up %d\n", (X))); \
18338 return 0; \
18339 } while (false)
18340 #else
18341 #define GIVE_UP(X) return 0
18342 #endif
18344 SET_TEXT_POS_FROM_MARKER (start, w->start);
18346 /* Don't use this for mini-windows because these can show
18347 messages and mini-buffers, and we don't handle that here. */
18348 if (MINI_WINDOW_P (w))
18349 GIVE_UP (1);
18351 /* This flag is used to prevent redisplay optimizations. */
18352 if (windows_or_buffers_changed || f->cursor_type_changed)
18353 GIVE_UP (2);
18355 /* This function's optimizations cannot be used if overlays have
18356 changed in the buffer displayed by the window, so give up if they
18357 have. */
18358 if (w->last_overlay_modified != OVERLAY_MODIFF)
18359 GIVE_UP (200);
18361 /* Verify that narrowing has not changed.
18362 Also verify that we were not told to prevent redisplay optimizations.
18363 It would be nice to further
18364 reduce the number of cases where this prevents try_window_id. */
18365 if (current_buffer->clip_changed
18366 || current_buffer->prevent_redisplay_optimizations_p)
18367 GIVE_UP (3);
18369 /* Window must either use window-based redisplay or be full width. */
18370 if (!FRAME_WINDOW_P (f)
18371 && (!FRAME_LINE_INS_DEL_OK (f)
18372 || !WINDOW_FULL_WIDTH_P (w)))
18373 GIVE_UP (4);
18375 /* Give up if point is known NOT to appear in W. */
18376 if (PT < CHARPOS (start))
18377 GIVE_UP (5);
18379 /* Another way to prevent redisplay optimizations. */
18380 if (w->last_modified == 0)
18381 GIVE_UP (6);
18383 /* Verify that window is not hscrolled. */
18384 if (w->hscroll != 0)
18385 GIVE_UP (7);
18387 /* Verify that display wasn't paused. */
18388 if (!w->window_end_valid)
18389 GIVE_UP (8);
18391 /* Likewise if highlighting trailing whitespace. */
18392 if (!NILP (Vshow_trailing_whitespace))
18393 GIVE_UP (11);
18395 /* Can't use this if overlay arrow position and/or string have
18396 changed. */
18397 if (overlay_arrows_changed_p (false))
18398 GIVE_UP (12);
18400 /* When word-wrap is on, adding a space to the first word of a
18401 wrapped line can change the wrap position, altering the line
18402 above it. It might be worthwhile to handle this more
18403 intelligently, but for now just redisplay from scratch. */
18404 if (!NILP (BVAR (XBUFFER (w->contents), word_wrap)))
18405 GIVE_UP (21);
18407 /* Under bidi reordering, adding or deleting a character in the
18408 beginning of a paragraph, before the first strong directional
18409 character, can change the base direction of the paragraph (unless
18410 the buffer specifies a fixed paragraph direction), which will
18411 require redisplaying the whole paragraph. It might be worthwhile
18412 to find the paragraph limits and widen the range of redisplayed
18413 lines to that, but for now just give up this optimization and
18414 redisplay from scratch. */
18415 if (!NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering))
18416 && NILP (BVAR (XBUFFER (w->contents), bidi_paragraph_direction)))
18417 GIVE_UP (22);
18419 /* Give up if the buffer has line-spacing set, as Lisp-level changes
18420 to that variable require thorough redisplay. */
18421 if (!NILP (BVAR (XBUFFER (w->contents), extra_line_spacing)))
18422 GIVE_UP (23);
18424 /* Make sure beg_unchanged and end_unchanged are up to date. Do it
18425 only if buffer has really changed. The reason is that the gap is
18426 initially at Z for freshly visited files. The code below would
18427 set end_unchanged to 0 in that case. */
18428 if (MODIFF > SAVE_MODIFF
18429 /* This seems to happen sometimes after saving a buffer. */
18430 || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
18432 if (GPT - BEG < BEG_UNCHANGED)
18433 BEG_UNCHANGED = GPT - BEG;
18434 if (Z - GPT < END_UNCHANGED)
18435 END_UNCHANGED = Z - GPT;
18438 /* The position of the first and last character that has been changed. */
18439 first_changed_charpos = BEG + BEG_UNCHANGED;
18440 last_changed_charpos = Z - END_UNCHANGED;
18442 /* If window starts after a line end, and the last change is in
18443 front of that newline, then changes don't affect the display.
18444 This case happens with stealth-fontification. Note that although
18445 the display is unchanged, glyph positions in the matrix have to
18446 be adjusted, of course. */
18447 row = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
18448 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
18449 && ((last_changed_charpos < CHARPOS (start)
18450 && CHARPOS (start) == BEGV)
18451 || (last_changed_charpos < CHARPOS (start) - 1
18452 && FETCH_BYTE (BYTEPOS (start) - 1) == '\n')))
18454 ptrdiff_t Z_old, Z_delta, Z_BYTE_old, Z_delta_bytes;
18455 struct glyph_row *r0;
18457 /* Compute how many chars/bytes have been added to or removed
18458 from the buffer. */
18459 Z_old = MATRIX_ROW_END_CHARPOS (row) + w->window_end_pos;
18460 Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
18461 Z_delta = Z - Z_old;
18462 Z_delta_bytes = Z_BYTE - Z_BYTE_old;
18464 /* Give up if PT is not in the window. Note that it already has
18465 been checked at the start of try_window_id that PT is not in
18466 front of the window start. */
18467 if (PT >= MATRIX_ROW_END_CHARPOS (row) + Z_delta)
18468 GIVE_UP (13);
18470 /* If window start is unchanged, we can reuse the whole matrix
18471 as is, after adjusting glyph positions. No need to compute
18472 the window end again, since its offset from Z hasn't changed. */
18473 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
18474 if (CHARPOS (start) == MATRIX_ROW_START_CHARPOS (r0) + Z_delta
18475 && BYTEPOS (start) == MATRIX_ROW_START_BYTEPOS (r0) + Z_delta_bytes
18476 /* PT must not be in a partially visible line. */
18477 && !(PT >= MATRIX_ROW_START_CHARPOS (row) + Z_delta
18478 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
18480 /* Adjust positions in the glyph matrix. */
18481 if (Z_delta || Z_delta_bytes)
18483 struct glyph_row *r1
18484 = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
18485 increment_matrix_positions (w->current_matrix,
18486 MATRIX_ROW_VPOS (r0, current_matrix),
18487 MATRIX_ROW_VPOS (r1, current_matrix),
18488 Z_delta, Z_delta_bytes);
18491 /* Set the cursor. */
18492 row = row_containing_pos (w, PT, r0, NULL, 0);
18493 if (row)
18494 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
18495 return 1;
18499 /* Handle the case that changes are all below what is displayed in
18500 the window, and that PT is in the window. This shortcut cannot
18501 be taken if ZV is visible in the window, and text has been added
18502 there that is visible in the window. */
18503 if (first_changed_charpos >= MATRIX_ROW_END_CHARPOS (row)
18504 /* ZV is not visible in the window, or there are no
18505 changes at ZV, actually. */
18506 && (current_matrix->zv > MATRIX_ROW_END_CHARPOS (row)
18507 || first_changed_charpos == last_changed_charpos))
18509 struct glyph_row *r0;
18511 /* Give up if PT is not in the window. Note that it already has
18512 been checked at the start of try_window_id that PT is not in
18513 front of the window start. */
18514 if (PT >= MATRIX_ROW_END_CHARPOS (row))
18515 GIVE_UP (14);
18517 /* If window start is unchanged, we can reuse the whole matrix
18518 as is, without changing glyph positions since no text has
18519 been added/removed in front of the window end. */
18520 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
18521 if (TEXT_POS_EQUAL_P (start, r0->minpos)
18522 /* PT must not be in a partially visible line. */
18523 && !(PT >= MATRIX_ROW_START_CHARPOS (row)
18524 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
18526 /* We have to compute the window end anew since text
18527 could have been added/removed after it. */
18528 w->window_end_pos = Z - MATRIX_ROW_END_CHARPOS (row);
18529 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
18531 /* Set the cursor. */
18532 row = row_containing_pos (w, PT, r0, NULL, 0);
18533 if (row)
18534 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
18535 return 2;
18539 /* Give up if window start is in the changed area.
18541 The condition used to read
18543 (BEG_UNCHANGED + END_UNCHANGED != Z - BEG && ...)
18545 but why that was tested escapes me at the moment. */
18546 if (CHARPOS (start) >= first_changed_charpos
18547 && CHARPOS (start) <= last_changed_charpos)
18548 GIVE_UP (15);
18550 /* Check that window start agrees with the start of the first glyph
18551 row in its current matrix. Check this after we know the window
18552 start is not in changed text, otherwise positions would not be
18553 comparable. */
18554 row = MATRIX_FIRST_TEXT_ROW (current_matrix);
18555 if (!TEXT_POS_EQUAL_P (start, row->minpos))
18556 GIVE_UP (16);
18558 /* Give up if the window ends in strings. Overlay strings
18559 at the end are difficult to handle, so don't try. */
18560 row = MATRIX_ROW (current_matrix, w->window_end_vpos);
18561 if (MATRIX_ROW_START_CHARPOS (row) == MATRIX_ROW_END_CHARPOS (row))
18562 GIVE_UP (20);
18564 /* Compute the position at which we have to start displaying new
18565 lines. Some of the lines at the top of the window might be
18566 reusable because they are not displaying changed text. Find the
18567 last row in W's current matrix not affected by changes at the
18568 start of current_buffer. Value is null if changes start in the
18569 first line of window. */
18570 last_unchanged_at_beg_row = find_last_unchanged_at_beg_row (w);
18571 if (last_unchanged_at_beg_row)
18573 /* Avoid starting to display in the middle of a character, a TAB
18574 for instance. This is easier than to set up the iterator
18575 exactly, and it's not a frequent case, so the additional
18576 effort wouldn't really pay off. */
18577 while ((MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row)
18578 || last_unchanged_at_beg_row->ends_in_newline_from_string_p)
18579 && last_unchanged_at_beg_row > w->current_matrix->rows)
18580 --last_unchanged_at_beg_row;
18582 if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row))
18583 GIVE_UP (17);
18585 if (! init_to_row_end (&it, w, last_unchanged_at_beg_row))
18586 GIVE_UP (18);
18587 start_pos = it.current.pos;
18589 /* Start displaying new lines in the desired matrix at the same
18590 vpos we would use in the current matrix, i.e. below
18591 last_unchanged_at_beg_row. */
18592 it.vpos = 1 + MATRIX_ROW_VPOS (last_unchanged_at_beg_row,
18593 current_matrix);
18594 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
18595 it.current_y = MATRIX_ROW_BOTTOM_Y (last_unchanged_at_beg_row);
18597 eassert (it.hpos == 0 && it.current_x == 0);
18599 else
18601 /* There are no reusable lines at the start of the window.
18602 Start displaying in the first text line. */
18603 start_display (&it, w, start);
18604 it.vpos = it.first_vpos;
18605 start_pos = it.current.pos;
18608 /* Find the first row that is not affected by changes at the end of
18609 the buffer. Value will be null if there is no unchanged row, in
18610 which case we must redisplay to the end of the window. delta
18611 will be set to the value by which buffer positions beginning with
18612 first_unchanged_at_end_row have to be adjusted due to text
18613 changes. */
18614 first_unchanged_at_end_row
18615 = find_first_unchanged_at_end_row (w, &delta, &delta_bytes);
18616 IF_DEBUG (debug_delta = delta);
18617 IF_DEBUG (debug_delta_bytes = delta_bytes);
18619 /* Set stop_pos to the buffer position up to which we will have to
18620 display new lines. If first_unchanged_at_end_row != NULL, this
18621 is the buffer position of the start of the line displayed in that
18622 row. For first_unchanged_at_end_row == NULL, use 0 to indicate
18623 that we don't stop at a buffer position. */
18624 stop_pos = 0;
18625 if (first_unchanged_at_end_row)
18627 eassert (last_unchanged_at_beg_row == NULL
18628 || first_unchanged_at_end_row >= last_unchanged_at_beg_row);
18630 /* If this is a continuation line, move forward to the next one
18631 that isn't. Changes in lines above affect this line.
18632 Caution: this may move first_unchanged_at_end_row to a row
18633 not displaying text. */
18634 while (MATRIX_ROW_CONTINUATION_LINE_P (first_unchanged_at_end_row)
18635 && MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
18636 && (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
18637 < it.last_visible_y))
18638 ++first_unchanged_at_end_row;
18640 if (!MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
18641 || (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
18642 >= it.last_visible_y))
18643 first_unchanged_at_end_row = NULL;
18644 else
18646 stop_pos = (MATRIX_ROW_START_CHARPOS (first_unchanged_at_end_row)
18647 + delta);
18648 first_unchanged_at_end_vpos
18649 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, current_matrix);
18650 eassert (stop_pos >= Z - END_UNCHANGED);
18653 else if (last_unchanged_at_beg_row == NULL)
18654 GIVE_UP (19);
18657 #ifdef GLYPH_DEBUG
18659 /* Either there is no unchanged row at the end, or the one we have
18660 now displays text. This is a necessary condition for the window
18661 end pos calculation at the end of this function. */
18662 eassert (first_unchanged_at_end_row == NULL
18663 || MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
18665 debug_last_unchanged_at_beg_vpos
18666 = (last_unchanged_at_beg_row
18667 ? MATRIX_ROW_VPOS (last_unchanged_at_beg_row, current_matrix)
18668 : -1);
18669 debug_first_unchanged_at_end_vpos = first_unchanged_at_end_vpos;
18671 #endif /* GLYPH_DEBUG */
18674 /* Display new lines. Set last_text_row to the last new line
18675 displayed which has text on it, i.e. might end up as being the
18676 line where the window_end_vpos is. */
18677 w->cursor.vpos = -1;
18678 last_text_row = NULL;
18679 overlay_arrow_seen = false;
18680 if (it.current_y < it.last_visible_y
18681 && !f->fonts_changed
18682 && (first_unchanged_at_end_row == NULL
18683 || IT_CHARPOS (it) < stop_pos))
18684 it.glyph_row->reversed_p = false;
18685 while (it.current_y < it.last_visible_y
18686 && !f->fonts_changed
18687 && (first_unchanged_at_end_row == NULL
18688 || IT_CHARPOS (it) < stop_pos))
18690 if (display_line (&it, -1))
18691 last_text_row = it.glyph_row - 1;
18694 if (f->fonts_changed)
18695 return -1;
18697 /* The redisplay iterations in display_line above could have
18698 triggered font-lock, which could have done something that
18699 invalidates IT->w window's end-point information, on which we
18700 rely below. E.g., one package, which will remain unnamed, used
18701 to install a font-lock-fontify-region-function that called
18702 bury-buffer, whose side effect is to switch the buffer displayed
18703 by IT->w, and that predictably resets IT->w's window_end_valid
18704 flag, which we already tested at the entry to this function.
18705 Amply punish such packages/modes by giving up on this
18706 optimization in those cases. */
18707 if (!w->window_end_valid)
18709 clear_glyph_matrix (w->desired_matrix);
18710 return -1;
18713 /* Compute differences in buffer positions, y-positions etc. for
18714 lines reused at the bottom of the window. Compute what we can
18715 scroll. */
18716 if (first_unchanged_at_end_row
18717 /* No lines reused because we displayed everything up to the
18718 bottom of the window. */
18719 && it.current_y < it.last_visible_y)
18721 dvpos = (it.vpos
18722 - MATRIX_ROW_VPOS (first_unchanged_at_end_row,
18723 current_matrix));
18724 dy = it.current_y - first_unchanged_at_end_row->y;
18725 run.current_y = first_unchanged_at_end_row->y;
18726 run.desired_y = run.current_y + dy;
18727 run.height = it.last_visible_y - max (run.current_y, run.desired_y);
18729 else
18731 delta = delta_bytes = dvpos = dy
18732 = run.current_y = run.desired_y = run.height = 0;
18733 first_unchanged_at_end_row = NULL;
18735 IF_DEBUG ((debug_dvpos = dvpos, debug_dy = dy));
18738 /* Find the cursor if not already found. We have to decide whether
18739 PT will appear on this window (it sometimes doesn't, but this is
18740 not a very frequent case.) This decision has to be made before
18741 the current matrix is altered. A value of cursor.vpos < 0 means
18742 that PT is either in one of the lines beginning at
18743 first_unchanged_at_end_row or below the window. Don't care for
18744 lines that might be displayed later at the window end; as
18745 mentioned, this is not a frequent case. */
18746 if (w->cursor.vpos < 0)
18748 /* Cursor in unchanged rows at the top? */
18749 if (PT < CHARPOS (start_pos)
18750 && last_unchanged_at_beg_row)
18752 row = row_containing_pos (w, PT,
18753 MATRIX_FIRST_TEXT_ROW (w->current_matrix),
18754 last_unchanged_at_beg_row + 1, 0);
18755 if (row)
18756 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
18759 /* Start from first_unchanged_at_end_row looking for PT. */
18760 else if (first_unchanged_at_end_row)
18762 row = row_containing_pos (w, PT - delta,
18763 first_unchanged_at_end_row, NULL, 0);
18764 if (row)
18765 set_cursor_from_row (w, row, w->current_matrix, delta,
18766 delta_bytes, dy, dvpos);
18769 /* Give up if cursor was not found. */
18770 if (w->cursor.vpos < 0)
18772 clear_glyph_matrix (w->desired_matrix);
18773 return -1;
18777 /* Don't let the cursor end in the scroll margins. */
18779 int this_scroll_margin = window_scroll_margin (w, MARGIN_IN_PIXELS);
18780 int cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
18782 if ((w->cursor.y < this_scroll_margin
18783 && CHARPOS (start) > BEGV)
18784 /* Old redisplay didn't take scroll margin into account at the bottom,
18785 but then global-hl-line-mode doesn't scroll. KFS 2004-06-14 */
18786 || (w->cursor.y + (make_cursor_line_fully_visible_p
18787 ? cursor_height + this_scroll_margin
18788 : 1)) > it.last_visible_y)
18790 w->cursor.vpos = -1;
18791 clear_glyph_matrix (w->desired_matrix);
18792 return -1;
18796 /* Scroll the display. Do it before changing the current matrix so
18797 that xterm.c doesn't get confused about where the cursor glyph is
18798 found. */
18799 if (dy && run.height)
18801 update_begin (f);
18803 if (FRAME_WINDOW_P (f))
18805 FRAME_RIF (f)->update_window_begin_hook (w);
18806 FRAME_RIF (f)->clear_window_mouse_face (w);
18807 FRAME_RIF (f)->scroll_run_hook (w, &run);
18808 FRAME_RIF (f)->update_window_end_hook (w, false, false);
18810 else
18812 /* Terminal frame. In this case, dvpos gives the number of
18813 lines to scroll by; dvpos < 0 means scroll up. */
18814 int from_vpos
18815 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, w->current_matrix);
18816 int from = WINDOW_TOP_EDGE_LINE (w) + from_vpos;
18817 int end = (WINDOW_TOP_EDGE_LINE (w)
18818 + WINDOW_WANTS_HEADER_LINE_P (w)
18819 + window_internal_height (w));
18821 #if defined (HAVE_GPM) || defined (MSDOS)
18822 x_clear_window_mouse_face (w);
18823 #endif
18824 /* Perform the operation on the screen. */
18825 if (dvpos > 0)
18827 /* Scroll last_unchanged_at_beg_row to the end of the
18828 window down dvpos lines. */
18829 set_terminal_window (f, end);
18831 /* On dumb terminals delete dvpos lines at the end
18832 before inserting dvpos empty lines. */
18833 if (!FRAME_SCROLL_REGION_OK (f))
18834 ins_del_lines (f, end - dvpos, -dvpos);
18836 /* Insert dvpos empty lines in front of
18837 last_unchanged_at_beg_row. */
18838 ins_del_lines (f, from, dvpos);
18840 else if (dvpos < 0)
18842 /* Scroll up last_unchanged_at_beg_vpos to the end of
18843 the window to last_unchanged_at_beg_vpos - |dvpos|. */
18844 set_terminal_window (f, end);
18846 /* Delete dvpos lines in front of
18847 last_unchanged_at_beg_vpos. ins_del_lines will set
18848 the cursor to the given vpos and emit |dvpos| delete
18849 line sequences. */
18850 ins_del_lines (f, from + dvpos, dvpos);
18852 /* On a dumb terminal insert dvpos empty lines at the
18853 end. */
18854 if (!FRAME_SCROLL_REGION_OK (f))
18855 ins_del_lines (f, end + dvpos, -dvpos);
18858 set_terminal_window (f, 0);
18861 update_end (f);
18864 /* Shift reused rows of the current matrix to the right position.
18865 BOTTOM_ROW is the last + 1 row in the current matrix reserved for
18866 text. */
18867 bottom_row = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
18868 bottom_vpos = MATRIX_ROW_VPOS (bottom_row, current_matrix);
18869 if (dvpos < 0)
18871 rotate_matrix (current_matrix, first_unchanged_at_end_vpos + dvpos,
18872 bottom_vpos, dvpos);
18873 clear_glyph_matrix_rows (current_matrix, bottom_vpos + dvpos,
18874 bottom_vpos);
18876 else if (dvpos > 0)
18878 rotate_matrix (current_matrix, first_unchanged_at_end_vpos,
18879 bottom_vpos, dvpos);
18880 clear_glyph_matrix_rows (current_matrix, first_unchanged_at_end_vpos,
18881 first_unchanged_at_end_vpos + dvpos);
18884 /* For frame-based redisplay, make sure that current frame and window
18885 matrix are in sync with respect to glyph memory. */
18886 if (!FRAME_WINDOW_P (f))
18887 sync_frame_with_window_matrix_rows (w);
18889 /* Adjust buffer positions in reused rows. */
18890 if (delta || delta_bytes)
18891 increment_matrix_positions (current_matrix,
18892 first_unchanged_at_end_vpos + dvpos,
18893 bottom_vpos, delta, delta_bytes);
18895 /* Adjust Y positions. */
18896 if (dy)
18897 shift_glyph_matrix (w, current_matrix,
18898 first_unchanged_at_end_vpos + dvpos,
18899 bottom_vpos, dy);
18901 if (first_unchanged_at_end_row)
18903 first_unchanged_at_end_row += dvpos;
18904 if (first_unchanged_at_end_row->y >= it.last_visible_y
18905 || !MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row))
18906 first_unchanged_at_end_row = NULL;
18909 /* If scrolling up, there may be some lines to display at the end of
18910 the window. */
18911 last_text_row_at_end = NULL;
18912 if (dy < 0)
18914 /* Scrolling up can leave for example a partially visible line
18915 at the end of the window to be redisplayed. */
18916 /* Set last_row to the glyph row in the current matrix where the
18917 window end line is found. It has been moved up or down in
18918 the matrix by dvpos. */
18919 int last_vpos = w->window_end_vpos + dvpos;
18920 struct glyph_row *last_row = MATRIX_ROW (current_matrix, last_vpos);
18922 /* If last_row is the window end line, it should display text. */
18923 eassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_row));
18925 /* If window end line was partially visible before, begin
18926 displaying at that line. Otherwise begin displaying with the
18927 line following it. */
18928 if (MATRIX_ROW_BOTTOM_Y (last_row) - dy >= it.last_visible_y)
18930 init_to_row_start (&it, w, last_row);
18931 it.vpos = last_vpos;
18932 it.current_y = last_row->y;
18934 else
18936 init_to_row_end (&it, w, last_row);
18937 it.vpos = 1 + last_vpos;
18938 it.current_y = MATRIX_ROW_BOTTOM_Y (last_row);
18939 ++last_row;
18942 /* We may start in a continuation line. If so, we have to
18943 get the right continuation_lines_width and current_x. */
18944 it.continuation_lines_width = last_row->continuation_lines_width;
18945 it.hpos = it.current_x = 0;
18947 /* Display the rest of the lines at the window end. */
18948 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
18949 while (it.current_y < it.last_visible_y && !f->fonts_changed)
18951 /* Is it always sure that the display agrees with lines in
18952 the current matrix? I don't think so, so we mark rows
18953 displayed invalid in the current matrix by setting their
18954 enabled_p flag to false. */
18955 SET_MATRIX_ROW_ENABLED_P (w->current_matrix, it.vpos, false);
18956 if (display_line (&it, w->cursor.vpos))
18957 last_text_row_at_end = it.glyph_row - 1;
18961 /* Update window_end_pos and window_end_vpos. */
18962 if (first_unchanged_at_end_row && !last_text_row_at_end)
18964 /* Window end line if one of the preserved rows from the current
18965 matrix. Set row to the last row displaying text in current
18966 matrix starting at first_unchanged_at_end_row, after
18967 scrolling. */
18968 eassert (MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
18969 row = find_last_row_displaying_text (w->current_matrix, &it,
18970 first_unchanged_at_end_row);
18971 eassume (row && MATRIX_ROW_DISPLAYS_TEXT_P (row));
18972 adjust_window_ends (w, row, true);
18973 eassert (w->window_end_bytepos >= 0);
18974 IF_DEBUG (debug_method_add (w, "A"));
18976 else if (last_text_row_at_end)
18978 adjust_window_ends (w, last_text_row_at_end, false);
18979 eassert (w->window_end_bytepos >= 0);
18980 IF_DEBUG (debug_method_add (w, "B"));
18982 else if (last_text_row)
18984 /* We have displayed either to the end of the window or at the
18985 end of the window, i.e. the last row with text is to be found
18986 in the desired matrix. */
18987 adjust_window_ends (w, last_text_row, false);
18988 eassert (w->window_end_bytepos >= 0);
18990 else if (first_unchanged_at_end_row == NULL
18991 && last_text_row == NULL
18992 && last_text_row_at_end == NULL)
18994 /* Displayed to end of window, but no line containing text was
18995 displayed. Lines were deleted at the end of the window. */
18996 bool first_vpos = WINDOW_WANTS_HEADER_LINE_P (w);
18997 int vpos = w->window_end_vpos;
18998 struct glyph_row *current_row = current_matrix->rows + vpos;
18999 struct glyph_row *desired_row = desired_matrix->rows + vpos;
19001 for (row = NULL; !row; --vpos, --current_row, --desired_row)
19003 eassert (first_vpos <= vpos);
19004 if (desired_row->enabled_p)
19006 if (MATRIX_ROW_DISPLAYS_TEXT_P (desired_row))
19007 row = desired_row;
19009 else if (MATRIX_ROW_DISPLAYS_TEXT_P (current_row))
19010 row = current_row;
19013 w->window_end_vpos = vpos + 1;
19014 w->window_end_pos = Z - MATRIX_ROW_END_CHARPOS (row);
19015 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
19016 eassert (w->window_end_bytepos >= 0);
19017 IF_DEBUG (debug_method_add (w, "C"));
19019 else
19020 emacs_abort ();
19022 IF_DEBUG ((debug_end_pos = w->window_end_pos,
19023 debug_end_vpos = w->window_end_vpos));
19025 /* Record that display has not been completed. */
19026 w->window_end_valid = false;
19027 w->desired_matrix->no_scrolling_p = true;
19028 return 3;
19030 #undef GIVE_UP
19035 /***********************************************************************
19036 More debugging support
19037 ***********************************************************************/
19039 #ifdef GLYPH_DEBUG
19041 void dump_glyph_row (struct glyph_row *, int, int) EXTERNALLY_VISIBLE;
19042 void dump_glyph_matrix (struct glyph_matrix *, int) EXTERNALLY_VISIBLE;
19043 void dump_glyph (struct glyph_row *, struct glyph *, int) EXTERNALLY_VISIBLE;
19046 /* Dump the contents of glyph matrix MATRIX on stderr.
19048 GLYPHS 0 means don't show glyph contents.
19049 GLYPHS 1 means show glyphs in short form
19050 GLYPHS > 1 means show glyphs in long form. */
19052 void
19053 dump_glyph_matrix (struct glyph_matrix *matrix, int glyphs)
19055 int i;
19056 for (i = 0; i < matrix->nrows; ++i)
19057 dump_glyph_row (MATRIX_ROW (matrix, i), i, glyphs);
19061 /* Dump contents of glyph GLYPH to stderr. ROW and AREA are
19062 the glyph row and area where the glyph comes from. */
19064 void
19065 dump_glyph (struct glyph_row *row, struct glyph *glyph, int area)
19067 if (glyph->type == CHAR_GLYPH
19068 || glyph->type == GLYPHLESS_GLYPH)
19070 fprintf (stderr,
19071 " %5"pD"d %c %9"pI"d %c %3d 0x%06x %c %4d %1.1d%1.1d\n",
19072 glyph - row->glyphs[TEXT_AREA],
19073 (glyph->type == CHAR_GLYPH
19074 ? 'C'
19075 : 'G'),
19076 glyph->charpos,
19077 (BUFFERP (glyph->object)
19078 ? 'B'
19079 : (STRINGP (glyph->object)
19080 ? 'S'
19081 : (NILP (glyph->object)
19082 ? '0'
19083 : '-'))),
19084 glyph->pixel_width,
19085 glyph->u.ch,
19086 (glyph->u.ch < 0x80 && glyph->u.ch >= ' '
19087 ? (int) glyph->u.ch
19088 : '.'),
19089 glyph->face_id,
19090 glyph->left_box_line_p,
19091 glyph->right_box_line_p);
19093 else if (glyph->type == STRETCH_GLYPH)
19095 fprintf (stderr,
19096 " %5"pD"d %c %9"pI"d %c %3d 0x%06x %c %4d %1.1d%1.1d\n",
19097 glyph - row->glyphs[TEXT_AREA],
19098 'S',
19099 glyph->charpos,
19100 (BUFFERP (glyph->object)
19101 ? 'B'
19102 : (STRINGP (glyph->object)
19103 ? 'S'
19104 : (NILP (glyph->object)
19105 ? '0'
19106 : '-'))),
19107 glyph->pixel_width,
19109 ' ',
19110 glyph->face_id,
19111 glyph->left_box_line_p,
19112 glyph->right_box_line_p);
19114 else if (glyph->type == IMAGE_GLYPH)
19116 fprintf (stderr,
19117 " %5"pD"d %c %9"pI"d %c %3d 0x%06x %c %4d %1.1d%1.1d\n",
19118 glyph - row->glyphs[TEXT_AREA],
19119 'I',
19120 glyph->charpos,
19121 (BUFFERP (glyph->object)
19122 ? 'B'
19123 : (STRINGP (glyph->object)
19124 ? 'S'
19125 : (NILP (glyph->object)
19126 ? '0'
19127 : '-'))),
19128 glyph->pixel_width,
19129 (unsigned int) glyph->u.img_id,
19130 '.',
19131 glyph->face_id,
19132 glyph->left_box_line_p,
19133 glyph->right_box_line_p);
19135 else if (glyph->type == COMPOSITE_GLYPH)
19137 fprintf (stderr,
19138 " %5"pD"d %c %9"pI"d %c %3d 0x%06x",
19139 glyph - row->glyphs[TEXT_AREA],
19140 '+',
19141 glyph->charpos,
19142 (BUFFERP (glyph->object)
19143 ? 'B'
19144 : (STRINGP (glyph->object)
19145 ? 'S'
19146 : (NILP (glyph->object)
19147 ? '0'
19148 : '-'))),
19149 glyph->pixel_width,
19150 (unsigned int) glyph->u.cmp.id);
19151 if (glyph->u.cmp.automatic)
19152 fprintf (stderr,
19153 "[%d-%d]",
19154 glyph->slice.cmp.from, glyph->slice.cmp.to);
19155 fprintf (stderr, " . %4d %1.1d%1.1d\n",
19156 glyph->face_id,
19157 glyph->left_box_line_p,
19158 glyph->right_box_line_p);
19160 else if (glyph->type == XWIDGET_GLYPH)
19162 #ifndef HAVE_XWIDGETS
19163 eassume (false);
19164 #else
19165 fprintf (stderr,
19166 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
19167 glyph - row->glyphs[TEXT_AREA],
19168 'X',
19169 glyph->charpos,
19170 (BUFFERP (glyph->object)
19171 ? 'B'
19172 : (STRINGP (glyph->object)
19173 ? 'S'
19174 : '-')),
19175 glyph->pixel_width,
19176 glyph->u.xwidget,
19177 '.',
19178 glyph->face_id,
19179 glyph->left_box_line_p,
19180 glyph->right_box_line_p);
19181 #endif
19186 /* Dump the contents of glyph row at VPOS in MATRIX to stderr.
19187 GLYPHS 0 means don't show glyph contents.
19188 GLYPHS 1 means show glyphs in short form
19189 GLYPHS > 1 means show glyphs in long form. */
19191 void
19192 dump_glyph_row (struct glyph_row *row, int vpos, int glyphs)
19194 if (glyphs != 1)
19196 fprintf (stderr, "Row Start End Used oE><\\CTZFesm X Y W H V A P\n");
19197 fprintf (stderr, "==============================================================================\n");
19199 fprintf (stderr, "%3d %9"pI"d %9"pI"d %4d %1.1d%1.1d%1.1d%1.1d\
19200 %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d %4d %4d %4d %4d %4d %4d %4d\n",
19201 vpos,
19202 MATRIX_ROW_START_CHARPOS (row),
19203 MATRIX_ROW_END_CHARPOS (row),
19204 row->used[TEXT_AREA],
19205 row->contains_overlapping_glyphs_p,
19206 row->enabled_p,
19207 row->truncated_on_left_p,
19208 row->truncated_on_right_p,
19209 row->continued_p,
19210 MATRIX_ROW_CONTINUATION_LINE_P (row),
19211 MATRIX_ROW_DISPLAYS_TEXT_P (row),
19212 row->ends_at_zv_p,
19213 row->fill_line_p,
19214 row->ends_in_middle_of_char_p,
19215 row->starts_in_middle_of_char_p,
19216 row->mouse_face_p,
19217 row->x,
19218 row->y,
19219 row->pixel_width,
19220 row->height,
19221 row->visible_height,
19222 row->ascent,
19223 row->phys_ascent);
19224 /* The next 3 lines should align to "Start" in the header. */
19225 fprintf (stderr, " %9"pD"d %9"pD"d\t%5d\n", row->start.overlay_string_index,
19226 row->end.overlay_string_index,
19227 row->continuation_lines_width);
19228 fprintf (stderr, " %9"pI"d %9"pI"d\n",
19229 CHARPOS (row->start.string_pos),
19230 CHARPOS (row->end.string_pos));
19231 fprintf (stderr, " %9d %9d\n", row->start.dpvec_index,
19232 row->end.dpvec_index);
19235 if (glyphs > 1)
19237 int area;
19239 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
19241 struct glyph *glyph = row->glyphs[area];
19242 struct glyph *glyph_end = glyph + row->used[area];
19244 /* Glyph for a line end in text. */
19245 if (area == TEXT_AREA && glyph == glyph_end && glyph->charpos > 0)
19246 ++glyph_end;
19248 if (glyph < glyph_end)
19249 fprintf (stderr, " Glyph# Type Pos O W Code C Face LR\n");
19251 for (; glyph < glyph_end; ++glyph)
19252 dump_glyph (row, glyph, area);
19255 else if (glyphs == 1)
19257 int area;
19258 char s[SHRT_MAX + 4];
19260 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
19262 int i;
19264 for (i = 0; i < row->used[area]; ++i)
19266 struct glyph *glyph = row->glyphs[area] + i;
19267 if (i == row->used[area] - 1
19268 && area == TEXT_AREA
19269 && NILP (glyph->object)
19270 && glyph->type == CHAR_GLYPH
19271 && glyph->u.ch == ' ')
19273 strcpy (&s[i], "[\\n]");
19274 i += 4;
19276 else if (glyph->type == CHAR_GLYPH
19277 && glyph->u.ch < 0x80
19278 && glyph->u.ch >= ' ')
19279 s[i] = glyph->u.ch;
19280 else
19281 s[i] = '.';
19284 s[i] = '\0';
19285 fprintf (stderr, "%3d: (%d) '%s'\n", vpos, row->enabled_p, s);
19291 DEFUN ("dump-glyph-matrix", Fdump_glyph_matrix,
19292 Sdump_glyph_matrix, 0, 1, "p",
19293 doc: /* Dump the current matrix of the selected window to stderr.
19294 Shows contents of glyph row structures. With non-nil
19295 parameter GLYPHS, dump glyphs as well. If GLYPHS is 1 show
19296 glyphs in short form, otherwise show glyphs in long form.
19298 Interactively, no argument means show glyphs in short form;
19299 with numeric argument, its value is passed as the GLYPHS flag. */)
19300 (Lisp_Object glyphs)
19302 struct window *w = XWINDOW (selected_window);
19303 struct buffer *buffer = XBUFFER (w->contents);
19305 fprintf (stderr, "PT = %"pI"d, BEGV = %"pI"d. ZV = %"pI"d\n",
19306 BUF_PT (buffer), BUF_BEGV (buffer), BUF_ZV (buffer));
19307 fprintf (stderr, "Cursor x = %d, y = %d, hpos = %d, vpos = %d\n",
19308 w->cursor.x, w->cursor.y, w->cursor.hpos, w->cursor.vpos);
19309 fprintf (stderr, "=============================================\n");
19310 dump_glyph_matrix (w->current_matrix,
19311 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 0);
19312 return Qnil;
19316 DEFUN ("dump-frame-glyph-matrix", Fdump_frame_glyph_matrix,
19317 Sdump_frame_glyph_matrix, 0, 0, "", doc: /* Dump the current glyph matrix of the selected frame to stderr.
19318 Only text-mode frames have frame glyph matrices. */)
19319 (void)
19321 struct frame *f = XFRAME (selected_frame);
19323 if (f->current_matrix)
19324 dump_glyph_matrix (f->current_matrix, 1);
19325 else
19326 fprintf (stderr, "*** This frame doesn't have a frame glyph matrix ***\n");
19327 return Qnil;
19331 DEFUN ("dump-glyph-row", Fdump_glyph_row, Sdump_glyph_row, 1, 2, "",
19332 doc: /* Dump glyph row ROW to stderr.
19333 GLYPH 0 means don't dump glyphs.
19334 GLYPH 1 means dump glyphs in short form.
19335 GLYPH > 1 or omitted means dump glyphs in long form. */)
19336 (Lisp_Object row, Lisp_Object glyphs)
19338 struct glyph_matrix *matrix;
19339 EMACS_INT vpos;
19341 CHECK_NUMBER (row);
19342 matrix = XWINDOW (selected_window)->current_matrix;
19343 vpos = XINT (row);
19344 if (vpos >= 0 && vpos < matrix->nrows)
19345 dump_glyph_row (MATRIX_ROW (matrix, vpos),
19346 vpos,
19347 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 2);
19348 return Qnil;
19352 DEFUN ("dump-tool-bar-row", Fdump_tool_bar_row, Sdump_tool_bar_row, 1, 2, "",
19353 doc: /* Dump glyph row ROW of the tool-bar of the current frame to stderr.
19354 GLYPH 0 means don't dump glyphs.
19355 GLYPH 1 means dump glyphs in short form.
19356 GLYPH > 1 or omitted means dump glyphs in long form.
19358 If there's no tool-bar, or if the tool-bar is not drawn by Emacs,
19359 do nothing. */)
19360 (Lisp_Object row, Lisp_Object glyphs)
19362 #if defined (HAVE_WINDOW_SYSTEM) && ! defined (USE_GTK) && ! defined (HAVE_NS)
19363 struct frame *sf = SELECTED_FRAME ();
19364 struct glyph_matrix *m = XWINDOW (sf->tool_bar_window)->current_matrix;
19365 EMACS_INT vpos;
19367 CHECK_NUMBER (row);
19368 vpos = XINT (row);
19369 if (vpos >= 0 && vpos < m->nrows)
19370 dump_glyph_row (MATRIX_ROW (m, vpos), vpos,
19371 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 2);
19372 #endif
19373 return Qnil;
19377 DEFUN ("trace-redisplay", Ftrace_redisplay, Strace_redisplay, 0, 1, "P",
19378 doc: /* Toggle tracing of redisplay.
19379 With ARG, turn tracing on if and only if ARG is positive. */)
19380 (Lisp_Object arg)
19382 if (NILP (arg))
19383 trace_redisplay_p = !trace_redisplay_p;
19384 else
19386 arg = Fprefix_numeric_value (arg);
19387 trace_redisplay_p = XINT (arg) > 0;
19390 return Qnil;
19394 DEFUN ("trace-to-stderr", Ftrace_to_stderr, Strace_to_stderr, 1, MANY, "",
19395 doc: /* Like `format', but print result to stderr.
19396 usage: (trace-to-stderr STRING &rest OBJECTS) */)
19397 (ptrdiff_t nargs, Lisp_Object *args)
19399 Lisp_Object s = Fformat (nargs, args);
19400 fwrite (SDATA (s), 1, SBYTES (s), stderr);
19401 return Qnil;
19404 #endif /* GLYPH_DEBUG */
19408 /***********************************************************************
19409 Building Desired Matrix Rows
19410 ***********************************************************************/
19412 /* Return a temporary glyph row holding the glyphs of an overlay arrow.
19413 Used for non-window-redisplay windows, and for windows w/o left fringe. */
19415 static struct glyph_row *
19416 get_overlay_arrow_glyph_row (struct window *w, Lisp_Object overlay_arrow_string)
19418 struct frame *f = XFRAME (WINDOW_FRAME (w));
19419 struct buffer *buffer = XBUFFER (w->contents);
19420 struct buffer *old = current_buffer;
19421 const unsigned char *arrow_string = SDATA (overlay_arrow_string);
19422 ptrdiff_t arrow_len = SCHARS (overlay_arrow_string);
19423 const unsigned char *arrow_end = arrow_string + arrow_len;
19424 const unsigned char *p;
19425 struct it it;
19426 bool multibyte_p;
19427 int n_glyphs_before;
19429 set_buffer_temp (buffer);
19430 init_iterator (&it, w, -1, -1, &scratch_glyph_row, DEFAULT_FACE_ID);
19431 scratch_glyph_row.reversed_p = false;
19432 it.glyph_row->used[TEXT_AREA] = 0;
19433 SET_TEXT_POS (it.position, 0, 0);
19435 multibyte_p = !NILP (BVAR (buffer, enable_multibyte_characters));
19436 p = arrow_string;
19437 while (p < arrow_end)
19439 Lisp_Object face, ilisp;
19441 /* Get the next character. */
19442 if (multibyte_p)
19443 it.c = it.char_to_display = string_char_and_length (p, &it.len);
19444 else
19446 it.c = it.char_to_display = *p, it.len = 1;
19447 if (! ASCII_CHAR_P (it.c))
19448 it.char_to_display = BYTE8_TO_CHAR (it.c);
19450 p += it.len;
19452 /* Get its face. */
19453 ilisp = make_number (p - arrow_string);
19454 face = Fget_text_property (ilisp, Qface, overlay_arrow_string);
19455 it.face_id = compute_char_face (f, it.char_to_display, face);
19457 /* Compute its width, get its glyphs. */
19458 n_glyphs_before = it.glyph_row->used[TEXT_AREA];
19459 SET_TEXT_POS (it.position, -1, -1);
19460 PRODUCE_GLYPHS (&it);
19462 /* If this character doesn't fit any more in the line, we have
19463 to remove some glyphs. */
19464 if (it.current_x > it.last_visible_x)
19466 it.glyph_row->used[TEXT_AREA] = n_glyphs_before;
19467 break;
19471 set_buffer_temp (old);
19472 return it.glyph_row;
19476 /* Insert truncation glyphs at the start of IT->glyph_row. Which
19477 glyphs to insert is determined by produce_special_glyphs. */
19479 static void
19480 insert_left_trunc_glyphs (struct it *it)
19482 struct it truncate_it;
19483 struct glyph *from, *end, *to, *toend;
19485 eassert (!FRAME_WINDOW_P (it->f)
19486 || (!it->glyph_row->reversed_p
19487 && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0)
19488 || (it->glyph_row->reversed_p
19489 && WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0));
19491 /* Get the truncation glyphs. */
19492 truncate_it = *it;
19493 truncate_it.current_x = 0;
19494 truncate_it.face_id = DEFAULT_FACE_ID;
19495 truncate_it.glyph_row = &scratch_glyph_row;
19496 truncate_it.area = TEXT_AREA;
19497 truncate_it.glyph_row->used[TEXT_AREA] = 0;
19498 CHARPOS (truncate_it.position) = BYTEPOS (truncate_it.position) = -1;
19499 truncate_it.object = Qnil;
19500 produce_special_glyphs (&truncate_it, IT_TRUNCATION);
19502 /* Overwrite glyphs from IT with truncation glyphs. */
19503 if (!it->glyph_row->reversed_p)
19505 short tused = truncate_it.glyph_row->used[TEXT_AREA];
19507 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
19508 end = from + tused;
19509 to = it->glyph_row->glyphs[TEXT_AREA];
19510 toend = to + it->glyph_row->used[TEXT_AREA];
19511 if (FRAME_WINDOW_P (it->f))
19513 /* On GUI frames, when variable-size fonts are displayed,
19514 the truncation glyphs may need more pixels than the row's
19515 glyphs they overwrite. We overwrite more glyphs to free
19516 enough screen real estate, and enlarge the stretch glyph
19517 on the right (see display_line), if there is one, to
19518 preserve the screen position of the truncation glyphs on
19519 the right. */
19520 int w = 0;
19521 struct glyph *g = to;
19522 short used;
19524 /* The first glyph could be partially visible, in which case
19525 it->glyph_row->x will be negative. But we want the left
19526 truncation glyphs to be aligned at the left margin of the
19527 window, so we override the x coordinate at which the row
19528 will begin. */
19529 it->glyph_row->x = 0;
19530 while (g < toend && w < it->truncation_pixel_width)
19532 w += g->pixel_width;
19533 ++g;
19535 if (g - to - tused > 0)
19537 memmove (to + tused, g, (toend - g) * sizeof(*g));
19538 it->glyph_row->used[TEXT_AREA] -= g - to - tused;
19540 used = it->glyph_row->used[TEXT_AREA];
19541 if (it->glyph_row->truncated_on_right_p
19542 && WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0
19543 && it->glyph_row->glyphs[TEXT_AREA][used - 2].type
19544 == STRETCH_GLYPH)
19546 int extra = w - it->truncation_pixel_width;
19548 it->glyph_row->glyphs[TEXT_AREA][used - 2].pixel_width += extra;
19552 while (from < end)
19553 *to++ = *from++;
19555 /* There may be padding glyphs left over. Overwrite them too. */
19556 if (!FRAME_WINDOW_P (it->f))
19558 while (to < toend && CHAR_GLYPH_PADDING_P (*to))
19560 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
19561 while (from < end)
19562 *to++ = *from++;
19566 if (to > toend)
19567 it->glyph_row->used[TEXT_AREA] = to - it->glyph_row->glyphs[TEXT_AREA];
19569 else
19571 short tused = truncate_it.glyph_row->used[TEXT_AREA];
19573 /* In R2L rows, overwrite the last (rightmost) glyphs, and do
19574 that back to front. */
19575 end = truncate_it.glyph_row->glyphs[TEXT_AREA];
19576 from = end + truncate_it.glyph_row->used[TEXT_AREA] - 1;
19577 toend = it->glyph_row->glyphs[TEXT_AREA];
19578 to = toend + it->glyph_row->used[TEXT_AREA] - 1;
19579 if (FRAME_WINDOW_P (it->f))
19581 int w = 0;
19582 struct glyph *g = to;
19584 while (g >= toend && w < it->truncation_pixel_width)
19586 w += g->pixel_width;
19587 --g;
19589 if (to - g - tused > 0)
19590 to = g + tused;
19591 if (it->glyph_row->truncated_on_right_p
19592 && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0
19593 && it->glyph_row->glyphs[TEXT_AREA][1].type == STRETCH_GLYPH)
19595 int extra = w - it->truncation_pixel_width;
19597 it->glyph_row->glyphs[TEXT_AREA][1].pixel_width += extra;
19601 while (from >= end && to >= toend)
19602 *to-- = *from--;
19603 if (!FRAME_WINDOW_P (it->f))
19605 while (to >= toend && CHAR_GLYPH_PADDING_P (*to))
19607 from =
19608 truncate_it.glyph_row->glyphs[TEXT_AREA]
19609 + truncate_it.glyph_row->used[TEXT_AREA] - 1;
19610 while (from >= end && to >= toend)
19611 *to-- = *from--;
19614 if (from >= end)
19616 /* Need to free some room before prepending additional
19617 glyphs. */
19618 int move_by = from - end + 1;
19619 struct glyph *g0 = it->glyph_row->glyphs[TEXT_AREA];
19620 struct glyph *g = g0 + it->glyph_row->used[TEXT_AREA] - 1;
19622 for ( ; g >= g0; g--)
19623 g[move_by] = *g;
19624 while (from >= end)
19625 *to-- = *from--;
19626 it->glyph_row->used[TEXT_AREA] += move_by;
19631 /* Compute the hash code for ROW. */
19632 unsigned
19633 row_hash (struct glyph_row *row)
19635 int area, k;
19636 unsigned hashval = 0;
19638 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
19639 for (k = 0; k < row->used[area]; ++k)
19640 hashval = ((((hashval << 4) + (hashval >> 24)) & 0x0fffffff)
19641 + row->glyphs[area][k].u.val
19642 + row->glyphs[area][k].face_id
19643 + row->glyphs[area][k].padding_p
19644 + (row->glyphs[area][k].type << 2));
19646 return hashval;
19649 /* Compute the pixel height and width of IT->glyph_row.
19651 Most of the time, ascent and height of a display line will be equal
19652 to the max_ascent and max_height values of the display iterator
19653 structure. This is not the case if
19655 1. We hit ZV without displaying anything. In this case, max_ascent
19656 and max_height will be zero.
19658 2. We have some glyphs that don't contribute to the line height.
19659 (The glyph row flag contributes_to_line_height_p is for future
19660 pixmap extensions).
19662 The first case is easily covered by using default values because in
19663 these cases, the line height does not really matter, except that it
19664 must not be zero. */
19666 static void
19667 compute_line_metrics (struct it *it)
19669 struct glyph_row *row = it->glyph_row;
19671 if (FRAME_WINDOW_P (it->f))
19673 int i, min_y, max_y;
19675 /* The line may consist of one space only, that was added to
19676 place the cursor on it. If so, the row's height hasn't been
19677 computed yet. */
19678 if (row->height == 0)
19680 if (it->max_ascent + it->max_descent == 0)
19681 it->max_descent = it->max_phys_descent = FRAME_LINE_HEIGHT (it->f);
19682 row->ascent = it->max_ascent;
19683 row->height = it->max_ascent + it->max_descent;
19684 row->phys_ascent = it->max_phys_ascent;
19685 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
19686 row->extra_line_spacing = it->max_extra_line_spacing;
19689 /* Compute the width of this line. */
19690 row->pixel_width = row->x;
19691 for (i = 0; i < row->used[TEXT_AREA]; ++i)
19692 row->pixel_width += row->glyphs[TEXT_AREA][i].pixel_width;
19694 eassert (row->pixel_width >= 0);
19695 eassert (row->ascent >= 0 && row->height > 0);
19697 row->overlapping_p = (MATRIX_ROW_OVERLAPS_SUCC_P (row)
19698 || MATRIX_ROW_OVERLAPS_PRED_P (row));
19700 /* If first line's physical ascent is larger than its logical
19701 ascent, use the physical ascent, and make the row taller.
19702 This makes accented characters fully visible. */
19703 if (row == MATRIX_FIRST_TEXT_ROW (it->w->desired_matrix)
19704 && row->phys_ascent > row->ascent)
19706 row->height += row->phys_ascent - row->ascent;
19707 row->ascent = row->phys_ascent;
19710 /* Compute how much of the line is visible. */
19711 row->visible_height = row->height;
19713 min_y = WINDOW_HEADER_LINE_HEIGHT (it->w);
19714 max_y = WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w);
19716 if (row->y < min_y)
19717 row->visible_height -= min_y - row->y;
19718 if (row->y + row->height > max_y)
19719 row->visible_height -= row->y + row->height - max_y;
19721 else
19723 row->pixel_width = row->used[TEXT_AREA];
19724 if (row->continued_p)
19725 row->pixel_width -= it->continuation_pixel_width;
19726 else if (row->truncated_on_right_p)
19727 row->pixel_width -= it->truncation_pixel_width;
19728 row->ascent = row->phys_ascent = 0;
19729 row->height = row->phys_height = row->visible_height = 1;
19730 row->extra_line_spacing = 0;
19733 /* Compute a hash code for this row. */
19734 row->hash = row_hash (row);
19736 it->max_ascent = it->max_descent = 0;
19737 it->max_phys_ascent = it->max_phys_descent = 0;
19741 /* Append one space to the glyph row of iterator IT if doing a
19742 window-based redisplay. The space has the same face as
19743 IT->face_id. Value is true if a space was added.
19745 This function is called to make sure that there is always one glyph
19746 at the end of a glyph row that the cursor can be set on under
19747 window-systems. (If there weren't such a glyph we would not know
19748 how wide and tall a box cursor should be displayed).
19750 At the same time this space let's a nicely handle clearing to the
19751 end of the line if the row ends in italic text. */
19753 static bool
19754 append_space_for_newline (struct it *it, bool default_face_p)
19756 if (FRAME_WINDOW_P (it->f))
19758 int n = it->glyph_row->used[TEXT_AREA];
19760 if (it->glyph_row->glyphs[TEXT_AREA] + n
19761 < it->glyph_row->glyphs[1 + TEXT_AREA])
19763 /* Save some values that must not be changed.
19764 Must save IT->c and IT->len because otherwise
19765 ITERATOR_AT_END_P wouldn't work anymore after
19766 append_space_for_newline has been called. */
19767 enum display_element_type saved_what = it->what;
19768 int saved_c = it->c, saved_len = it->len;
19769 int saved_char_to_display = it->char_to_display;
19770 int saved_x = it->current_x;
19771 int saved_face_id = it->face_id;
19772 bool saved_box_end = it->end_of_box_run_p;
19773 struct text_pos saved_pos;
19774 Lisp_Object saved_object;
19775 struct face *face;
19777 saved_object = it->object;
19778 saved_pos = it->position;
19780 it->what = IT_CHARACTER;
19781 memset (&it->position, 0, sizeof it->position);
19782 it->object = Qnil;
19783 it->c = it->char_to_display = ' ';
19784 it->len = 1;
19786 /* If the default face was remapped, be sure to use the
19787 remapped face for the appended newline. */
19788 if (default_face_p)
19789 it->face_id = lookup_basic_face (it->f, DEFAULT_FACE_ID);
19790 else if (it->face_before_selective_p)
19791 it->face_id = it->saved_face_id;
19792 face = FACE_FROM_ID (it->f, it->face_id);
19793 it->face_id = FACE_FOR_CHAR (it->f, face, 0, -1, Qnil);
19794 /* In R2L rows, we will prepend a stretch glyph that will
19795 have the end_of_box_run_p flag set for it, so there's no
19796 need for the appended newline glyph to have that flag
19797 set. */
19798 if (it->glyph_row->reversed_p
19799 /* But if the appended newline glyph goes all the way to
19800 the end of the row, there will be no stretch glyph,
19801 so leave the box flag set. */
19802 && saved_x + FRAME_COLUMN_WIDTH (it->f) < it->last_visible_x)
19803 it->end_of_box_run_p = false;
19805 PRODUCE_GLYPHS (it);
19807 #ifdef HAVE_WINDOW_SYSTEM
19808 /* Make sure this space glyph has the right ascent and
19809 descent values, or else cursor at end of line will look
19810 funny, and height of empty lines will be incorrect. */
19811 struct glyph *g = it->glyph_row->glyphs[TEXT_AREA] + n;
19812 struct font *font = face->font ? face->font : FRAME_FONT (it->f);
19813 if (n == 0)
19815 Lisp_Object height, total_height;
19816 int extra_line_spacing = it->extra_line_spacing;
19817 int boff = font->baseline_offset;
19819 if (font->vertical_centering)
19820 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
19822 it->object = saved_object; /* get_it_property needs this */
19823 normal_char_ascent_descent (font, -1, &it->ascent, &it->descent);
19824 /* Must do a subset of line height processing from
19825 x_produce_glyph for newline characters. */
19826 height = get_it_property (it, Qline_height);
19827 if (CONSP (height)
19828 && CONSP (XCDR (height))
19829 && NILP (XCDR (XCDR (height))))
19831 total_height = XCAR (XCDR (height));
19832 height = XCAR (height);
19834 else
19835 total_height = Qnil;
19836 height = calc_line_height_property (it, height, font, boff, true);
19838 if (it->override_ascent >= 0)
19840 it->ascent = it->override_ascent;
19841 it->descent = it->override_descent;
19842 boff = it->override_boff;
19844 if (EQ (height, Qt))
19845 extra_line_spacing = 0;
19846 else
19848 Lisp_Object spacing;
19850 it->phys_ascent = it->ascent;
19851 it->phys_descent = it->descent;
19852 if (!NILP (height)
19853 && XINT (height) > it->ascent + it->descent)
19854 it->ascent = XINT (height) - it->descent;
19856 if (!NILP (total_height))
19857 spacing = calc_line_height_property (it, total_height, font,
19858 boff, false);
19859 else
19861 spacing = get_it_property (it, Qline_spacing);
19862 spacing = calc_line_height_property (it, spacing, font,
19863 boff, false);
19865 if (INTEGERP (spacing))
19867 extra_line_spacing = XINT (spacing);
19868 if (!NILP (total_height))
19869 extra_line_spacing -= (it->phys_ascent + it->phys_descent);
19872 if (extra_line_spacing > 0)
19874 it->descent += extra_line_spacing;
19875 if (extra_line_spacing > it->max_extra_line_spacing)
19876 it->max_extra_line_spacing = extra_line_spacing;
19878 it->max_ascent = it->ascent;
19879 it->max_descent = it->descent;
19880 /* Make sure compute_line_metrics recomputes the row height. */
19881 it->glyph_row->height = 0;
19884 g->ascent = it->max_ascent;
19885 g->descent = it->max_descent;
19886 #endif
19888 it->override_ascent = -1;
19889 it->constrain_row_ascent_descent_p = false;
19890 it->current_x = saved_x;
19891 it->object = saved_object;
19892 it->position = saved_pos;
19893 it->what = saved_what;
19894 it->face_id = saved_face_id;
19895 it->len = saved_len;
19896 it->c = saved_c;
19897 it->char_to_display = saved_char_to_display;
19898 it->end_of_box_run_p = saved_box_end;
19899 return true;
19903 return false;
19907 /* Extend the face of the last glyph in the text area of IT->glyph_row
19908 to the end of the display line. Called from display_line. If the
19909 glyph row is empty, add a space glyph to it so that we know the
19910 face to draw. Set the glyph row flag fill_line_p. If the glyph
19911 row is R2L, prepend a stretch glyph to cover the empty space to the
19912 left of the leftmost glyph. */
19914 static void
19915 extend_face_to_end_of_line (struct it *it)
19917 struct face *face, *default_face;
19918 struct frame *f = it->f;
19920 /* If line is already filled, do nothing. Non window-system frames
19921 get a grace of one more ``pixel'' because their characters are
19922 1-``pixel'' wide, so they hit the equality too early. This grace
19923 is needed only for R2L rows that are not continued, to produce
19924 one extra blank where we could display the cursor. */
19925 if ((it->current_x >= it->last_visible_x
19926 + (!FRAME_WINDOW_P (f)
19927 && it->glyph_row->reversed_p
19928 && !it->glyph_row->continued_p))
19929 /* If the window has display margins, we will need to extend
19930 their face even if the text area is filled. */
19931 && !(WINDOW_LEFT_MARGIN_WIDTH (it->w) > 0
19932 || WINDOW_RIGHT_MARGIN_WIDTH (it->w) > 0))
19933 return;
19935 /* The default face, possibly remapped. */
19936 default_face = FACE_FROM_ID_OR_NULL (f,
19937 lookup_basic_face (f, DEFAULT_FACE_ID));
19939 /* Face extension extends the background and box of IT->face_id
19940 to the end of the line. If the background equals the background
19941 of the frame, we don't have to do anything. */
19942 face = FACE_FROM_ID (f, (it->face_before_selective_p
19943 ? it->saved_face_id
19944 : it->face_id));
19946 if (FRAME_WINDOW_P (f)
19947 && MATRIX_ROW_DISPLAYS_TEXT_P (it->glyph_row)
19948 && face->box == FACE_NO_BOX
19949 && face->background == FRAME_BACKGROUND_PIXEL (f)
19950 #ifdef HAVE_WINDOW_SYSTEM
19951 && !face->stipple
19952 #endif
19953 && !it->glyph_row->reversed_p)
19954 return;
19956 /* Set the glyph row flag indicating that the face of the last glyph
19957 in the text area has to be drawn to the end of the text area. */
19958 it->glyph_row->fill_line_p = true;
19960 /* If current character of IT is not ASCII, make sure we have the
19961 ASCII face. This will be automatically undone the next time
19962 get_next_display_element returns a multibyte character. Note
19963 that the character will always be single byte in unibyte
19964 text. */
19965 if (!ASCII_CHAR_P (it->c))
19967 it->face_id = FACE_FOR_CHAR (f, face, 0, -1, Qnil);
19970 if (FRAME_WINDOW_P (f))
19972 /* If the row is empty, add a space with the current face of IT,
19973 so that we know which face to draw. */
19974 if (it->glyph_row->used[TEXT_AREA] == 0)
19976 it->glyph_row->glyphs[TEXT_AREA][0] = space_glyph;
19977 it->glyph_row->glyphs[TEXT_AREA][0].face_id = face->id;
19978 it->glyph_row->used[TEXT_AREA] = 1;
19980 /* Mode line and the header line don't have margins, and
19981 likewise the frame's tool-bar window, if there is any. */
19982 if (!(it->glyph_row->mode_line_p
19983 #if defined (HAVE_WINDOW_SYSTEM) && ! defined (USE_GTK) && ! defined (HAVE_NS)
19984 || (WINDOWP (f->tool_bar_window)
19985 && it->w == XWINDOW (f->tool_bar_window))
19986 #endif
19989 if (WINDOW_LEFT_MARGIN_WIDTH (it->w) > 0
19990 && it->glyph_row->used[LEFT_MARGIN_AREA] == 0)
19992 it->glyph_row->glyphs[LEFT_MARGIN_AREA][0] = space_glyph;
19993 it->glyph_row->glyphs[LEFT_MARGIN_AREA][0].face_id =
19994 default_face->id;
19995 it->glyph_row->used[LEFT_MARGIN_AREA] = 1;
19997 if (WINDOW_RIGHT_MARGIN_WIDTH (it->w) > 0
19998 && it->glyph_row->used[RIGHT_MARGIN_AREA] == 0)
20000 it->glyph_row->glyphs[RIGHT_MARGIN_AREA][0] = space_glyph;
20001 it->glyph_row->glyphs[RIGHT_MARGIN_AREA][0].face_id =
20002 default_face->id;
20003 it->glyph_row->used[RIGHT_MARGIN_AREA] = 1;
20006 #ifdef HAVE_WINDOW_SYSTEM
20007 if (it->glyph_row->reversed_p)
20009 /* Prepend a stretch glyph to the row, such that the
20010 rightmost glyph will be drawn flushed all the way to the
20011 right margin of the window. The stretch glyph that will
20012 occupy the empty space, if any, to the left of the
20013 glyphs. */
20014 struct font *font = face->font ? face->font : FRAME_FONT (f);
20015 struct glyph *row_start = it->glyph_row->glyphs[TEXT_AREA];
20016 struct glyph *row_end = row_start + it->glyph_row->used[TEXT_AREA];
20017 struct glyph *g;
20018 int row_width, stretch_ascent, stretch_width;
20019 struct text_pos saved_pos;
20020 int saved_face_id;
20021 bool saved_avoid_cursor, saved_box_start;
20023 for (row_width = 0, g = row_start; g < row_end; g++)
20024 row_width += g->pixel_width;
20026 /* FIXME: There are various minor display glitches in R2L
20027 rows when only one of the fringes is missing. The
20028 strange condition below produces the least bad effect. */
20029 if ((WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0)
20030 == (WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0)
20031 || WINDOW_RIGHT_FRINGE_WIDTH (it->w) != 0)
20032 stretch_width = window_box_width (it->w, TEXT_AREA);
20033 else
20034 stretch_width = it->last_visible_x - it->first_visible_x;
20035 stretch_width -= row_width;
20037 if (stretch_width > 0)
20039 stretch_ascent =
20040 (((it->ascent + it->descent)
20041 * FONT_BASE (font)) / FONT_HEIGHT (font));
20042 saved_pos = it->position;
20043 memset (&it->position, 0, sizeof it->position);
20044 saved_avoid_cursor = it->avoid_cursor_p;
20045 it->avoid_cursor_p = true;
20046 saved_face_id = it->face_id;
20047 saved_box_start = it->start_of_box_run_p;
20048 /* The last row's stretch glyph should get the default
20049 face, to avoid painting the rest of the window with
20050 the region face, if the region ends at ZV. */
20051 if (it->glyph_row->ends_at_zv_p)
20052 it->face_id = default_face->id;
20053 else
20054 it->face_id = face->id;
20055 it->start_of_box_run_p = false;
20056 append_stretch_glyph (it, Qnil, stretch_width,
20057 it->ascent + it->descent, stretch_ascent);
20058 it->position = saved_pos;
20059 it->avoid_cursor_p = saved_avoid_cursor;
20060 it->face_id = saved_face_id;
20061 it->start_of_box_run_p = saved_box_start;
20063 /* If stretch_width comes out negative, it means that the
20064 last glyph is only partially visible. In R2L rows, we
20065 want the leftmost glyph to be partially visible, so we
20066 need to give the row the corresponding left offset. */
20067 if (stretch_width < 0)
20068 it->glyph_row->x = stretch_width;
20070 #endif /* HAVE_WINDOW_SYSTEM */
20072 else
20074 /* Save some values that must not be changed. */
20075 int saved_x = it->current_x;
20076 struct text_pos saved_pos;
20077 Lisp_Object saved_object;
20078 enum display_element_type saved_what = it->what;
20079 int saved_face_id = it->face_id;
20081 saved_object = it->object;
20082 saved_pos = it->position;
20084 it->what = IT_CHARACTER;
20085 memset (&it->position, 0, sizeof it->position);
20086 it->object = Qnil;
20087 it->c = it->char_to_display = ' ';
20088 it->len = 1;
20090 if (WINDOW_LEFT_MARGIN_WIDTH (it->w) > 0
20091 && (it->glyph_row->used[LEFT_MARGIN_AREA]
20092 < WINDOW_LEFT_MARGIN_WIDTH (it->w))
20093 && !it->glyph_row->mode_line_p
20094 && default_face->background != FRAME_BACKGROUND_PIXEL (f))
20096 struct glyph *g = it->glyph_row->glyphs[LEFT_MARGIN_AREA];
20097 struct glyph *e = g + it->glyph_row->used[LEFT_MARGIN_AREA];
20099 for (it->current_x = 0; g < e; g++)
20100 it->current_x += g->pixel_width;
20102 it->area = LEFT_MARGIN_AREA;
20103 it->face_id = default_face->id;
20104 while (it->glyph_row->used[LEFT_MARGIN_AREA]
20105 < WINDOW_LEFT_MARGIN_WIDTH (it->w))
20107 PRODUCE_GLYPHS (it);
20108 /* term.c:produce_glyphs advances it->current_x only for
20109 TEXT_AREA. */
20110 it->current_x += it->pixel_width;
20113 it->current_x = saved_x;
20114 it->area = TEXT_AREA;
20117 /* The last row's blank glyphs should get the default face, to
20118 avoid painting the rest of the window with the region face,
20119 if the region ends at ZV. */
20120 if (it->glyph_row->ends_at_zv_p)
20121 it->face_id = default_face->id;
20122 else
20123 it->face_id = face->id;
20124 PRODUCE_GLYPHS (it);
20126 while (it->current_x <= it->last_visible_x)
20127 PRODUCE_GLYPHS (it);
20129 if (WINDOW_RIGHT_MARGIN_WIDTH (it->w) > 0
20130 && (it->glyph_row->used[RIGHT_MARGIN_AREA]
20131 < WINDOW_RIGHT_MARGIN_WIDTH (it->w))
20132 && !it->glyph_row->mode_line_p
20133 && default_face->background != FRAME_BACKGROUND_PIXEL (f))
20135 struct glyph *g = it->glyph_row->glyphs[RIGHT_MARGIN_AREA];
20136 struct glyph *e = g + it->glyph_row->used[RIGHT_MARGIN_AREA];
20138 for ( ; g < e; g++)
20139 it->current_x += g->pixel_width;
20141 it->area = RIGHT_MARGIN_AREA;
20142 it->face_id = default_face->id;
20143 while (it->glyph_row->used[RIGHT_MARGIN_AREA]
20144 < WINDOW_RIGHT_MARGIN_WIDTH (it->w))
20146 PRODUCE_GLYPHS (it);
20147 it->current_x += it->pixel_width;
20150 it->area = TEXT_AREA;
20153 /* Don't count these blanks really. It would let us insert a left
20154 truncation glyph below and make us set the cursor on them, maybe. */
20155 it->current_x = saved_x;
20156 it->object = saved_object;
20157 it->position = saved_pos;
20158 it->what = saved_what;
20159 it->face_id = saved_face_id;
20164 /* Value is true if text starting at CHARPOS in current_buffer is
20165 trailing whitespace. */
20167 static bool
20168 trailing_whitespace_p (ptrdiff_t charpos)
20170 ptrdiff_t bytepos = CHAR_TO_BYTE (charpos);
20171 int c = 0;
20173 while (bytepos < ZV_BYTE
20174 && (c = FETCH_CHAR (bytepos),
20175 c == ' ' || c == '\t'))
20176 ++bytepos;
20178 if (bytepos >= ZV_BYTE || c == '\n' || c == '\r')
20180 if (bytepos != PT_BYTE)
20181 return true;
20183 return false;
20187 /* Highlight trailing whitespace, if any, in ROW. */
20189 static void
20190 highlight_trailing_whitespace (struct frame *f, struct glyph_row *row)
20192 int used = row->used[TEXT_AREA];
20194 if (used)
20196 struct glyph *start = row->glyphs[TEXT_AREA];
20197 struct glyph *glyph = start + used - 1;
20199 if (row->reversed_p)
20201 /* Right-to-left rows need to be processed in the opposite
20202 direction, so swap the edge pointers. */
20203 glyph = start;
20204 start = row->glyphs[TEXT_AREA] + used - 1;
20207 /* Skip over glyphs inserted to display the cursor at the
20208 end of a line, for extending the face of the last glyph
20209 to the end of the line on terminals, and for truncation
20210 and continuation glyphs. */
20211 if (!row->reversed_p)
20213 while (glyph >= start
20214 && glyph->type == CHAR_GLYPH
20215 && NILP (glyph->object))
20216 --glyph;
20218 else
20220 while (glyph <= start
20221 && glyph->type == CHAR_GLYPH
20222 && NILP (glyph->object))
20223 ++glyph;
20226 /* If last glyph is a space or stretch, and it's trailing
20227 whitespace, set the face of all trailing whitespace glyphs in
20228 IT->glyph_row to `trailing-whitespace'. */
20229 if ((row->reversed_p ? glyph <= start : glyph >= start)
20230 && BUFFERP (glyph->object)
20231 && (glyph->type == STRETCH_GLYPH
20232 || (glyph->type == CHAR_GLYPH
20233 && glyph->u.ch == ' '))
20234 && trailing_whitespace_p (glyph->charpos))
20236 int face_id = lookup_named_face (f, Qtrailing_whitespace, false);
20237 if (face_id < 0)
20238 return;
20240 if (!row->reversed_p)
20242 while (glyph >= start
20243 && BUFFERP (glyph->object)
20244 && (glyph->type == STRETCH_GLYPH
20245 || (glyph->type == CHAR_GLYPH
20246 && glyph->u.ch == ' ')))
20247 (glyph--)->face_id = face_id;
20249 else
20251 while (glyph <= start
20252 && BUFFERP (glyph->object)
20253 && (glyph->type == STRETCH_GLYPH
20254 || (glyph->type == CHAR_GLYPH
20255 && glyph->u.ch == ' ')))
20256 (glyph++)->face_id = face_id;
20263 /* Value is true if glyph row ROW should be
20264 considered to hold the buffer position CHARPOS. */
20266 static bool
20267 row_for_charpos_p (struct glyph_row *row, ptrdiff_t charpos)
20269 bool result = true;
20271 if (charpos == CHARPOS (row->end.pos)
20272 || charpos == MATRIX_ROW_END_CHARPOS (row))
20274 /* Suppose the row ends on a string.
20275 Unless the row is continued, that means it ends on a newline
20276 in the string. If it's anything other than a display string
20277 (e.g., a before-string from an overlay), we don't want the
20278 cursor there. (This heuristic seems to give the optimal
20279 behavior for the various types of multi-line strings.)
20280 One exception: if the string has `cursor' property on one of
20281 its characters, we _do_ want the cursor there. */
20282 if (CHARPOS (row->end.string_pos) >= 0)
20284 if (row->continued_p)
20285 result = true;
20286 else
20288 /* Check for `display' property. */
20289 struct glyph *beg = row->glyphs[TEXT_AREA];
20290 struct glyph *end = beg + row->used[TEXT_AREA] - 1;
20291 struct glyph *glyph;
20293 result = false;
20294 for (glyph = end; glyph >= beg; --glyph)
20295 if (STRINGP (glyph->object))
20297 Lisp_Object prop
20298 = Fget_char_property (make_number (charpos),
20299 Qdisplay, Qnil);
20300 result =
20301 (!NILP (prop)
20302 && display_prop_string_p (prop, glyph->object));
20303 /* If there's a `cursor' property on one of the
20304 string's characters, this row is a cursor row,
20305 even though this is not a display string. */
20306 if (!result)
20308 Lisp_Object s = glyph->object;
20310 for ( ; glyph >= beg && EQ (glyph->object, s); --glyph)
20312 ptrdiff_t gpos = glyph->charpos;
20314 if (!NILP (Fget_char_property (make_number (gpos),
20315 Qcursor, s)))
20317 result = true;
20318 break;
20322 break;
20326 else if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
20328 /* If the row ends in middle of a real character,
20329 and the line is continued, we want the cursor here.
20330 That's because CHARPOS (ROW->end.pos) would equal
20331 PT if PT is before the character. */
20332 if (!row->ends_in_ellipsis_p)
20333 result = row->continued_p;
20334 else
20335 /* If the row ends in an ellipsis, then
20336 CHARPOS (ROW->end.pos) will equal point after the
20337 invisible text. We want that position to be displayed
20338 after the ellipsis. */
20339 result = false;
20341 /* If the row ends at ZV, display the cursor at the end of that
20342 row instead of at the start of the row below. */
20343 else
20344 result = row->ends_at_zv_p;
20347 return result;
20350 /* Value is true if glyph row ROW should be
20351 used to hold the cursor. */
20353 static bool
20354 cursor_row_p (struct glyph_row *row)
20356 return row_for_charpos_p (row, PT);
20361 /* Push the property PROP so that it will be rendered at the current
20362 position in IT. Return true if PROP was successfully pushed, false
20363 otherwise. Called from handle_line_prefix to handle the
20364 `line-prefix' and `wrap-prefix' properties. */
20366 static bool
20367 push_prefix_prop (struct it *it, Lisp_Object prop)
20369 struct text_pos pos =
20370 STRINGP (it->string) ? it->current.string_pos : it->current.pos;
20372 eassert (it->method == GET_FROM_BUFFER
20373 || it->method == GET_FROM_DISPLAY_VECTOR
20374 || it->method == GET_FROM_STRING
20375 || it->method == GET_FROM_IMAGE);
20377 /* We need to save the current buffer/string position, so it will be
20378 restored by pop_it, because iterate_out_of_display_property
20379 depends on that being set correctly, but some situations leave
20380 it->position not yet set when this function is called. */
20381 push_it (it, &pos);
20383 if (STRINGP (prop))
20385 if (SCHARS (prop) == 0)
20387 pop_it (it);
20388 return false;
20391 it->string = prop;
20392 it->string_from_prefix_prop_p = true;
20393 it->multibyte_p = STRING_MULTIBYTE (it->string);
20394 it->current.overlay_string_index = -1;
20395 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
20396 it->end_charpos = it->string_nchars = SCHARS (it->string);
20397 it->method = GET_FROM_STRING;
20398 it->stop_charpos = 0;
20399 it->prev_stop = 0;
20400 it->base_level_stop = 0;
20402 /* Force paragraph direction to be that of the parent
20403 buffer/string. */
20404 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
20405 it->paragraph_embedding = it->bidi_it.paragraph_dir;
20406 else
20407 it->paragraph_embedding = L2R;
20409 /* Set up the bidi iterator for this display string. */
20410 if (it->bidi_p)
20412 it->bidi_it.string.lstring = it->string;
20413 it->bidi_it.string.s = NULL;
20414 it->bidi_it.string.schars = it->end_charpos;
20415 it->bidi_it.string.bufpos = IT_CHARPOS (*it);
20416 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
20417 it->bidi_it.string.unibyte = !it->multibyte_p;
20418 it->bidi_it.w = it->w;
20419 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
20422 else if (CONSP (prop) && EQ (XCAR (prop), Qspace))
20424 it->method = GET_FROM_STRETCH;
20425 it->object = prop;
20427 #ifdef HAVE_WINDOW_SYSTEM
20428 else if (IMAGEP (prop))
20430 it->what = IT_IMAGE;
20431 it->image_id = lookup_image (it->f, prop);
20432 it->method = GET_FROM_IMAGE;
20434 #endif /* HAVE_WINDOW_SYSTEM */
20435 else
20437 pop_it (it); /* bogus display property, give up */
20438 return false;
20441 return true;
20444 /* Return the character-property PROP at the current position in IT. */
20446 static Lisp_Object
20447 get_it_property (struct it *it, Lisp_Object prop)
20449 Lisp_Object position, object = it->object;
20451 if (STRINGP (object))
20452 position = make_number (IT_STRING_CHARPOS (*it));
20453 else if (BUFFERP (object))
20455 position = make_number (IT_CHARPOS (*it));
20456 object = it->window;
20458 else
20459 return Qnil;
20461 return Fget_char_property (position, prop, object);
20464 /* See if there's a line- or wrap-prefix, and if so, push it on IT. */
20466 static void
20467 handle_line_prefix (struct it *it)
20469 Lisp_Object prefix;
20471 if (it->continuation_lines_width > 0)
20473 prefix = get_it_property (it, Qwrap_prefix);
20474 if (NILP (prefix))
20475 prefix = Vwrap_prefix;
20477 else
20479 prefix = get_it_property (it, Qline_prefix);
20480 if (NILP (prefix))
20481 prefix = Vline_prefix;
20483 if (! NILP (prefix) && push_prefix_prop (it, prefix))
20485 /* If the prefix is wider than the window, and we try to wrap
20486 it, it would acquire its own wrap prefix, and so on till the
20487 iterator stack overflows. So, don't wrap the prefix. */
20488 it->line_wrap = TRUNCATE;
20489 it->avoid_cursor_p = true;
20495 /* Remove N glyphs at the start of a reversed IT->glyph_row. Called
20496 only for R2L lines from display_line and display_string, when they
20497 decide that too many glyphs were produced by PRODUCE_GLYPHS, and
20498 the line/string needs to be continued on the next glyph row. */
20499 static void
20500 unproduce_glyphs (struct it *it, int n)
20502 struct glyph *glyph, *end;
20504 eassert (it->glyph_row);
20505 eassert (it->glyph_row->reversed_p);
20506 eassert (it->area == TEXT_AREA);
20507 eassert (n <= it->glyph_row->used[TEXT_AREA]);
20509 if (n > it->glyph_row->used[TEXT_AREA])
20510 n = it->glyph_row->used[TEXT_AREA];
20511 glyph = it->glyph_row->glyphs[TEXT_AREA] + n;
20512 end = it->glyph_row->glyphs[TEXT_AREA] + it->glyph_row->used[TEXT_AREA];
20513 for ( ; glyph < end; glyph++)
20514 glyph[-n] = *glyph;
20517 /* Find the positions in a bidi-reordered ROW to serve as ROW->minpos
20518 and ROW->maxpos. */
20519 static void
20520 find_row_edges (struct it *it, struct glyph_row *row,
20521 ptrdiff_t min_pos, ptrdiff_t min_bpos,
20522 ptrdiff_t max_pos, ptrdiff_t max_bpos)
20524 /* FIXME: Revisit this when glyph ``spilling'' in continuation
20525 lines' rows is implemented for bidi-reordered rows. */
20527 /* ROW->minpos is the value of min_pos, the minimal buffer position
20528 we have in ROW, or ROW->start.pos if that is smaller. */
20529 if (min_pos <= ZV && min_pos < row->start.pos.charpos)
20530 SET_TEXT_POS (row->minpos, min_pos, min_bpos);
20531 else
20532 /* We didn't find buffer positions smaller than ROW->start, or
20533 didn't find _any_ valid buffer positions in any of the glyphs,
20534 so we must trust the iterator's computed positions. */
20535 row->minpos = row->start.pos;
20536 if (max_pos <= 0)
20538 max_pos = CHARPOS (it->current.pos);
20539 max_bpos = BYTEPOS (it->current.pos);
20542 /* Here are the various use-cases for ending the row, and the
20543 corresponding values for ROW->maxpos:
20545 Line ends in a newline from buffer eol_pos + 1
20546 Line is continued from buffer max_pos + 1
20547 Line is truncated on right it->current.pos
20548 Line ends in a newline from string max_pos + 1(*)
20549 (*) + 1 only when line ends in a forward scan
20550 Line is continued from string max_pos
20551 Line is continued from display vector max_pos
20552 Line is entirely from a string min_pos == max_pos
20553 Line is entirely from a display vector min_pos == max_pos
20554 Line that ends at ZV ZV
20556 If you discover other use-cases, please add them here as
20557 appropriate. */
20558 if (row->ends_at_zv_p)
20559 row->maxpos = it->current.pos;
20560 else if (row->used[TEXT_AREA])
20562 bool seen_this_string = false;
20563 struct glyph_row *r1 = row - 1;
20565 /* Did we see the same display string on the previous row? */
20566 if (STRINGP (it->object)
20567 /* this is not the first row */
20568 && row > it->w->desired_matrix->rows
20569 /* previous row is not the header line */
20570 && !r1->mode_line_p
20571 /* previous row also ends in a newline from a string */
20572 && r1->ends_in_newline_from_string_p)
20574 struct glyph *start, *end;
20576 /* Search for the last glyph of the previous row that came
20577 from buffer or string. Depending on whether the row is
20578 L2R or R2L, we need to process it front to back or the
20579 other way round. */
20580 if (!r1->reversed_p)
20582 start = r1->glyphs[TEXT_AREA];
20583 end = start + r1->used[TEXT_AREA];
20584 /* Glyphs inserted by redisplay have nil as their object. */
20585 while (end > start
20586 && NILP ((end - 1)->object)
20587 && (end - 1)->charpos <= 0)
20588 --end;
20589 if (end > start)
20591 if (EQ ((end - 1)->object, it->object))
20592 seen_this_string = true;
20594 else
20595 /* If all the glyphs of the previous row were inserted
20596 by redisplay, it means the previous row was
20597 produced from a single newline, which is only
20598 possible if that newline came from the same string
20599 as the one which produced this ROW. */
20600 seen_this_string = true;
20602 else
20604 end = r1->glyphs[TEXT_AREA] - 1;
20605 start = end + r1->used[TEXT_AREA];
20606 while (end < start
20607 && NILP ((end + 1)->object)
20608 && (end + 1)->charpos <= 0)
20609 ++end;
20610 if (end < start)
20612 if (EQ ((end + 1)->object, it->object))
20613 seen_this_string = true;
20615 else
20616 seen_this_string = true;
20619 /* Take note of each display string that covers a newline only
20620 once, the first time we see it. This is for when a display
20621 string includes more than one newline in it. */
20622 if (row->ends_in_newline_from_string_p && !seen_this_string)
20624 /* If we were scanning the buffer forward when we displayed
20625 the string, we want to account for at least one buffer
20626 position that belongs to this row (position covered by
20627 the display string), so that cursor positioning will
20628 consider this row as a candidate when point is at the end
20629 of the visual line represented by this row. This is not
20630 required when scanning back, because max_pos will already
20631 have a much larger value. */
20632 if (CHARPOS (row->end.pos) > max_pos)
20633 INC_BOTH (max_pos, max_bpos);
20634 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
20636 else if (CHARPOS (it->eol_pos) > 0)
20637 SET_TEXT_POS (row->maxpos,
20638 CHARPOS (it->eol_pos) + 1, BYTEPOS (it->eol_pos) + 1);
20639 else if (row->continued_p)
20641 /* If max_pos is different from IT's current position, it
20642 means IT->method does not belong to the display element
20643 at max_pos. However, it also means that the display
20644 element at max_pos was displayed in its entirety on this
20645 line, which is equivalent to saying that the next line
20646 starts at the next buffer position. */
20647 if (IT_CHARPOS (*it) == max_pos && it->method != GET_FROM_BUFFER)
20648 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
20649 else
20651 INC_BOTH (max_pos, max_bpos);
20652 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
20655 else if (row->truncated_on_right_p)
20656 /* display_line already called reseat_at_next_visible_line_start,
20657 which puts the iterator at the beginning of the next line, in
20658 the logical order. */
20659 row->maxpos = it->current.pos;
20660 else if (max_pos == min_pos && it->method != GET_FROM_BUFFER)
20661 /* A line that is entirely from a string/image/stretch... */
20662 row->maxpos = row->minpos;
20663 else
20664 emacs_abort ();
20666 else
20667 row->maxpos = it->current.pos;
20670 /* Construct the glyph row IT->glyph_row in the desired matrix of
20671 IT->w from text at the current position of IT. See dispextern.h
20672 for an overview of struct it. Value is true if
20673 IT->glyph_row displays text, as opposed to a line displaying ZV
20674 only. CURSOR_VPOS is the window-relative vertical position of
20675 the glyph row displaying the cursor, or -1 if unknown. */
20677 static bool
20678 display_line (struct it *it, int cursor_vpos)
20680 struct glyph_row *row = it->glyph_row;
20681 Lisp_Object overlay_arrow_string;
20682 struct it wrap_it;
20683 void *wrap_data = NULL;
20684 bool may_wrap = false;
20685 int wrap_x UNINIT;
20686 int wrap_row_used = -1;
20687 int wrap_row_ascent UNINIT, wrap_row_height UNINIT;
20688 int wrap_row_phys_ascent UNINIT, wrap_row_phys_height UNINIT;
20689 int wrap_row_extra_line_spacing UNINIT;
20690 ptrdiff_t wrap_row_min_pos UNINIT, wrap_row_min_bpos UNINIT;
20691 ptrdiff_t wrap_row_max_pos UNINIT, wrap_row_max_bpos UNINIT;
20692 int cvpos;
20693 ptrdiff_t min_pos = ZV + 1, max_pos = 0;
20694 ptrdiff_t min_bpos UNINIT, max_bpos UNINIT;
20695 bool pending_handle_line_prefix = false;
20696 int header_line = WINDOW_WANTS_HEADER_LINE_P (it->w);
20697 bool hscroll_this_line = (cursor_vpos >= 0
20698 && it->vpos == cursor_vpos - header_line
20699 && hscrolling_current_line_p (it->w));
20700 int first_visible_x = it->first_visible_x;
20701 int last_visible_x = it->last_visible_x;
20702 int x_incr = 0;
20704 /* We always start displaying at hpos zero even if hscrolled. */
20705 eassert (it->hpos == 0 && it->current_x == 0);
20707 if (MATRIX_ROW_VPOS (row, it->w->desired_matrix)
20708 >= it->w->desired_matrix->nrows)
20710 it->w->nrows_scale_factor++;
20711 it->f->fonts_changed = true;
20712 return false;
20715 /* Clear the result glyph row and enable it. */
20716 prepare_desired_row (it->w, row, false);
20718 row->y = it->current_y;
20719 row->start = it->start;
20720 row->continuation_lines_width = it->continuation_lines_width;
20721 row->displays_text_p = true;
20722 row->starts_in_middle_of_char_p = it->starts_in_middle_of_char_p;
20723 it->starts_in_middle_of_char_p = false;
20725 /* Arrange the overlays nicely for our purposes. Usually, we call
20726 display_line on only one line at a time, in which case this
20727 can't really hurt too much, or we call it on lines which appear
20728 one after another in the buffer, in which case all calls to
20729 recenter_overlay_lists but the first will be pretty cheap. */
20730 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
20732 /* If we are going to display the cursor's line, account for the
20733 hscroll of that line. We subtract the window's min_hscroll,
20734 because that was already accounted for in init_iterator. */
20735 if (hscroll_this_line)
20736 x_incr =
20737 (window_hscroll_limited (it->w, it->f) - it->w->min_hscroll)
20738 * FRAME_COLUMN_WIDTH (it->f);
20740 /* Move over display elements that are not visible because we are
20741 hscrolled. This may stop at an x-position < first_visible_x
20742 if the first glyph is partially visible or if we hit a line end. */
20743 if (it->current_x < it->first_visible_x + x_incr)
20745 enum move_it_result move_result;
20747 this_line_min_pos = row->start.pos;
20748 if (hscroll_this_line)
20750 it->first_visible_x += x_incr;
20751 it->last_visible_x += x_incr;
20753 move_result = move_it_in_display_line_to (it, ZV, it->first_visible_x,
20754 MOVE_TO_POS | MOVE_TO_X);
20755 /* If we are under a large hscroll, move_it_in_display_line_to
20756 could hit the end of the line without reaching
20757 first_visible_x. Pretend that we did reach it. This is
20758 especially important on a TTY, where we will call
20759 extend_face_to_end_of_line, which needs to know how many
20760 blank glyphs to produce. */
20761 if (it->current_x < it->first_visible_x
20762 && (move_result == MOVE_NEWLINE_OR_CR
20763 || move_result == MOVE_POS_MATCH_OR_ZV))
20764 it->current_x = it->first_visible_x;
20766 /* Record the smallest positions seen while we moved over
20767 display elements that are not visible. This is needed by
20768 redisplay_internal for optimizing the case where the cursor
20769 stays inside the same line. The rest of this function only
20770 considers positions that are actually displayed, so
20771 RECORD_MAX_MIN_POS will not otherwise record positions that
20772 are hscrolled to the left of the left edge of the window. */
20773 min_pos = CHARPOS (this_line_min_pos);
20774 min_bpos = BYTEPOS (this_line_min_pos);
20776 else if (it->area == TEXT_AREA)
20778 /* We only do this when not calling move_it_in_display_line_to
20779 above, because that function calls itself handle_line_prefix. */
20780 handle_line_prefix (it);
20782 else
20784 /* Line-prefix and wrap-prefix are always displayed in the text
20785 area. But if this is the first call to display_line after
20786 init_iterator, the iterator might have been set up to write
20787 into a marginal area, e.g. if the line begins with some
20788 display property that writes to the margins. So we need to
20789 wait with the call to handle_line_prefix until whatever
20790 writes to the margin has done its job. */
20791 pending_handle_line_prefix = true;
20794 /* Get the initial row height. This is either the height of the
20795 text hscrolled, if there is any, or zero. */
20796 row->ascent = it->max_ascent;
20797 row->height = it->max_ascent + it->max_descent;
20798 row->phys_ascent = it->max_phys_ascent;
20799 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
20800 row->extra_line_spacing = it->max_extra_line_spacing;
20802 /* Utility macro to record max and min buffer positions seen until now. */
20803 #define RECORD_MAX_MIN_POS(IT) \
20804 do \
20806 bool composition_p \
20807 = !STRINGP ((IT)->string) && ((IT)->what == IT_COMPOSITION); \
20808 ptrdiff_t current_pos = \
20809 composition_p ? (IT)->cmp_it.charpos \
20810 : IT_CHARPOS (*(IT)); \
20811 ptrdiff_t current_bpos = \
20812 composition_p ? CHAR_TO_BYTE (current_pos) \
20813 : IT_BYTEPOS (*(IT)); \
20814 if (current_pos < min_pos) \
20816 min_pos = current_pos; \
20817 min_bpos = current_bpos; \
20819 if (IT_CHARPOS (*it) > max_pos) \
20821 max_pos = IT_CHARPOS (*it); \
20822 max_bpos = IT_BYTEPOS (*it); \
20825 while (false)
20827 /* Loop generating characters. The loop is left with IT on the next
20828 character to display. */
20829 while (true)
20831 int n_glyphs_before, hpos_before, x_before;
20832 int x, nglyphs;
20833 int ascent = 0, descent = 0, phys_ascent = 0, phys_descent = 0;
20835 /* Retrieve the next thing to display. Value is false if end of
20836 buffer reached. */
20837 if (!get_next_display_element (it))
20839 /* Maybe add a space at the end of this line that is used to
20840 display the cursor there under X. Set the charpos of the
20841 first glyph of blank lines not corresponding to any text
20842 to -1. */
20843 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
20844 row->exact_window_width_line_p = true;
20845 else if ((append_space_for_newline (it, true)
20846 && row->used[TEXT_AREA] == 1)
20847 || row->used[TEXT_AREA] == 0)
20849 row->glyphs[TEXT_AREA]->charpos = -1;
20850 row->displays_text_p = false;
20852 if (!NILP (BVAR (XBUFFER (it->w->contents), indicate_empty_lines))
20853 && (!MINI_WINDOW_P (it->w)
20854 || (minibuf_level && EQ (it->window, minibuf_window))))
20855 row->indicate_empty_line_p = true;
20858 it->continuation_lines_width = 0;
20859 /* Reset those iterator values set from display property
20860 values. This is for the case when the display property
20861 ends at ZV, and is not a replacing property, so pop_it is
20862 not called. */
20863 it->font_height = Qnil;
20864 it->voffset = 0;
20865 row->ends_at_zv_p = true;
20866 /* A row that displays right-to-left text must always have
20867 its last face extended all the way to the end of line,
20868 even if this row ends in ZV, because we still write to
20869 the screen left to right. We also need to extend the
20870 last face if the default face is remapped to some
20871 different face, otherwise the functions that clear
20872 portions of the screen will clear with the default face's
20873 background color. */
20874 if (row->reversed_p
20875 || lookup_basic_face (it->f, DEFAULT_FACE_ID) != DEFAULT_FACE_ID)
20876 extend_face_to_end_of_line (it);
20877 break;
20880 /* Now, get the metrics of what we want to display. This also
20881 generates glyphs in `row' (which is IT->glyph_row). */
20882 n_glyphs_before = row->used[TEXT_AREA];
20883 x = it->current_x;
20885 /* Remember the line height so far in case the next element doesn't
20886 fit on the line. */
20887 if (it->line_wrap != TRUNCATE)
20889 ascent = it->max_ascent;
20890 descent = it->max_descent;
20891 phys_ascent = it->max_phys_ascent;
20892 phys_descent = it->max_phys_descent;
20894 if (it->line_wrap == WORD_WRAP && it->area == TEXT_AREA)
20896 if (IT_DISPLAYING_WHITESPACE (it))
20897 may_wrap = true;
20898 else if (may_wrap)
20900 SAVE_IT (wrap_it, *it, wrap_data);
20901 wrap_x = x;
20902 wrap_row_used = row->used[TEXT_AREA];
20903 wrap_row_ascent = row->ascent;
20904 wrap_row_height = row->height;
20905 wrap_row_phys_ascent = row->phys_ascent;
20906 wrap_row_phys_height = row->phys_height;
20907 wrap_row_extra_line_spacing = row->extra_line_spacing;
20908 wrap_row_min_pos = min_pos;
20909 wrap_row_min_bpos = min_bpos;
20910 wrap_row_max_pos = max_pos;
20911 wrap_row_max_bpos = max_bpos;
20912 may_wrap = false;
20917 PRODUCE_GLYPHS (it);
20919 /* If this display element was in marginal areas, continue with
20920 the next one. */
20921 if (it->area != TEXT_AREA)
20923 row->ascent = max (row->ascent, it->max_ascent);
20924 row->height = max (row->height, it->max_ascent + it->max_descent);
20925 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
20926 row->phys_height = max (row->phys_height,
20927 it->max_phys_ascent + it->max_phys_descent);
20928 row->extra_line_spacing = max (row->extra_line_spacing,
20929 it->max_extra_line_spacing);
20930 set_iterator_to_next (it, true);
20931 /* If we didn't handle the line/wrap prefix above, and the
20932 call to set_iterator_to_next just switched to TEXT_AREA,
20933 process the prefix now. */
20934 if (it->area == TEXT_AREA && pending_handle_line_prefix)
20936 pending_handle_line_prefix = false;
20937 handle_line_prefix (it);
20939 continue;
20942 /* Does the display element fit on the line? If we truncate
20943 lines, we should draw past the right edge of the window. If
20944 we don't truncate, we want to stop so that we can display the
20945 continuation glyph before the right margin. If lines are
20946 continued, there are two possible strategies for characters
20947 resulting in more than 1 glyph (e.g. tabs): Display as many
20948 glyphs as possible in this line and leave the rest for the
20949 continuation line, or display the whole element in the next
20950 line. Original redisplay did the former, so we do it also. */
20951 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
20952 hpos_before = it->hpos;
20953 x_before = x;
20955 if (/* Not a newline. */
20956 nglyphs > 0
20957 /* Glyphs produced fit entirely in the line. */
20958 && it->current_x < it->last_visible_x)
20960 it->hpos += nglyphs;
20961 row->ascent = max (row->ascent, it->max_ascent);
20962 row->height = max (row->height, it->max_ascent + it->max_descent);
20963 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
20964 row->phys_height = max (row->phys_height,
20965 it->max_phys_ascent + it->max_phys_descent);
20966 row->extra_line_spacing = max (row->extra_line_spacing,
20967 it->max_extra_line_spacing);
20968 if (it->current_x - it->pixel_width < it->first_visible_x
20969 /* In R2L rows, we arrange in extend_face_to_end_of_line
20970 to add a right offset to the line, by a suitable
20971 change to the stretch glyph that is the leftmost
20972 glyph of the line. */
20973 && !row->reversed_p)
20974 row->x = x - it->first_visible_x;
20975 /* Record the maximum and minimum buffer positions seen so
20976 far in glyphs that will be displayed by this row. */
20977 if (it->bidi_p)
20978 RECORD_MAX_MIN_POS (it);
20980 else
20982 int i, new_x;
20983 struct glyph *glyph;
20985 for (i = 0; i < nglyphs; ++i, x = new_x)
20987 /* Identify the glyphs added by the last call to
20988 PRODUCE_GLYPHS. In R2L rows, they are prepended to
20989 the previous glyphs. */
20990 if (!row->reversed_p)
20991 glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
20992 else
20993 glyph = row->glyphs[TEXT_AREA] + nglyphs - 1 - i;
20994 new_x = x + glyph->pixel_width;
20996 if (/* Lines are continued. */
20997 it->line_wrap != TRUNCATE
20998 && (/* Glyph doesn't fit on the line. */
20999 new_x > it->last_visible_x
21000 /* Or it fits exactly on a window system frame. */
21001 || (new_x == it->last_visible_x
21002 && FRAME_WINDOW_P (it->f)
21003 && (row->reversed_p
21004 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
21005 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)))))
21007 /* End of a continued line. */
21009 if (it->hpos == 0
21010 || (new_x == it->last_visible_x
21011 && FRAME_WINDOW_P (it->f)
21012 && (row->reversed_p
21013 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
21014 : WINDOW_RIGHT_FRINGE_WIDTH (it->w))))
21016 /* Current glyph is the only one on the line or
21017 fits exactly on the line. We must continue
21018 the line because we can't draw the cursor
21019 after the glyph. */
21020 row->continued_p = true;
21021 it->current_x = new_x;
21022 it->continuation_lines_width += new_x;
21023 ++it->hpos;
21024 if (i == nglyphs - 1)
21026 /* If line-wrap is on, check if a previous
21027 wrap point was found. */
21028 if (!IT_OVERFLOW_NEWLINE_INTO_FRINGE (it)
21029 && wrap_row_used > 0
21030 /* Even if there is a previous wrap
21031 point, continue the line here as
21032 usual, if (i) the previous character
21033 was a space or tab AND (ii) the
21034 current character is not. */
21035 && (!may_wrap
21036 || IT_DISPLAYING_WHITESPACE (it)))
21037 goto back_to_wrap;
21039 /* Record the maximum and minimum buffer
21040 positions seen so far in glyphs that will be
21041 displayed by this row. */
21042 if (it->bidi_p)
21043 RECORD_MAX_MIN_POS (it);
21044 set_iterator_to_next (it, true);
21045 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
21047 if (!get_next_display_element (it))
21049 row->exact_window_width_line_p = true;
21050 it->continuation_lines_width = 0;
21051 it->font_height = Qnil;
21052 it->voffset = 0;
21053 row->continued_p = false;
21054 row->ends_at_zv_p = true;
21056 else if (ITERATOR_AT_END_OF_LINE_P (it))
21058 row->continued_p = false;
21059 row->exact_window_width_line_p = true;
21061 /* If line-wrap is on, check if a
21062 previous wrap point was found. */
21063 else if (wrap_row_used > 0
21064 /* Even if there is a previous wrap
21065 point, continue the line here as
21066 usual, if (i) the previous character
21067 was a space or tab AND (ii) the
21068 current character is not. */
21069 && (!may_wrap
21070 || IT_DISPLAYING_WHITESPACE (it)))
21071 goto back_to_wrap;
21075 else if (it->bidi_p)
21076 RECORD_MAX_MIN_POS (it);
21077 if (WINDOW_LEFT_MARGIN_WIDTH (it->w) > 0
21078 || WINDOW_RIGHT_MARGIN_WIDTH (it->w) > 0)
21079 extend_face_to_end_of_line (it);
21081 else if (CHAR_GLYPH_PADDING_P (*glyph)
21082 && !FRAME_WINDOW_P (it->f))
21084 /* A padding glyph that doesn't fit on this line.
21085 This means the whole character doesn't fit
21086 on the line. */
21087 if (row->reversed_p)
21088 unproduce_glyphs (it, row->used[TEXT_AREA]
21089 - n_glyphs_before);
21090 row->used[TEXT_AREA] = n_glyphs_before;
21092 /* Fill the rest of the row with continuation
21093 glyphs like in 20.x. */
21094 while (row->glyphs[TEXT_AREA] + row->used[TEXT_AREA]
21095 < row->glyphs[1 + TEXT_AREA])
21096 produce_special_glyphs (it, IT_CONTINUATION);
21098 row->continued_p = true;
21099 it->current_x = x_before;
21100 it->continuation_lines_width += x_before;
21102 /* Restore the height to what it was before the
21103 element not fitting on the line. */
21104 it->max_ascent = ascent;
21105 it->max_descent = descent;
21106 it->max_phys_ascent = phys_ascent;
21107 it->max_phys_descent = phys_descent;
21108 if (WINDOW_LEFT_MARGIN_WIDTH (it->w) > 0
21109 || WINDOW_RIGHT_MARGIN_WIDTH (it->w) > 0)
21110 extend_face_to_end_of_line (it);
21112 else if (wrap_row_used > 0)
21114 back_to_wrap:
21115 if (row->reversed_p)
21116 unproduce_glyphs (it,
21117 row->used[TEXT_AREA] - wrap_row_used);
21118 RESTORE_IT (it, &wrap_it, wrap_data);
21119 it->continuation_lines_width += wrap_x;
21120 row->used[TEXT_AREA] = wrap_row_used;
21121 row->ascent = wrap_row_ascent;
21122 row->height = wrap_row_height;
21123 row->phys_ascent = wrap_row_phys_ascent;
21124 row->phys_height = wrap_row_phys_height;
21125 row->extra_line_spacing = wrap_row_extra_line_spacing;
21126 min_pos = wrap_row_min_pos;
21127 min_bpos = wrap_row_min_bpos;
21128 max_pos = wrap_row_max_pos;
21129 max_bpos = wrap_row_max_bpos;
21130 row->continued_p = true;
21131 row->ends_at_zv_p = false;
21132 row->exact_window_width_line_p = false;
21133 it->continuation_lines_width += x;
21135 /* Make sure that a non-default face is extended
21136 up to the right margin of the window. */
21137 extend_face_to_end_of_line (it);
21139 else if ((it->what == IT_CHARACTER
21140 || it->what == IT_STRETCH
21141 || it->what == IT_COMPOSITION)
21142 && it->c == '\t' && FRAME_WINDOW_P (it->f))
21144 /* A TAB that extends past the right edge of the
21145 window. This produces a single glyph on
21146 window system frames. We leave the glyph in
21147 this row and let it fill the row, but don't
21148 consume the TAB. */
21149 if ((row->reversed_p
21150 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
21151 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
21152 produce_special_glyphs (it, IT_CONTINUATION);
21153 it->continuation_lines_width += it->last_visible_x;
21154 row->ends_in_middle_of_char_p = true;
21155 row->continued_p = true;
21156 glyph->pixel_width = it->last_visible_x - x;
21157 it->starts_in_middle_of_char_p = true;
21158 if (WINDOW_LEFT_MARGIN_WIDTH (it->w) > 0
21159 || WINDOW_RIGHT_MARGIN_WIDTH (it->w) > 0)
21160 extend_face_to_end_of_line (it);
21162 else
21164 /* Something other than a TAB that draws past
21165 the right edge of the window. Restore
21166 positions to values before the element. */
21167 if (row->reversed_p)
21168 unproduce_glyphs (it, row->used[TEXT_AREA]
21169 - (n_glyphs_before + i));
21170 row->used[TEXT_AREA] = n_glyphs_before + i;
21172 /* Display continuation glyphs. */
21173 it->current_x = x_before;
21174 it->continuation_lines_width += x;
21175 if (!FRAME_WINDOW_P (it->f)
21176 || (row->reversed_p
21177 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
21178 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
21179 produce_special_glyphs (it, IT_CONTINUATION);
21180 row->continued_p = true;
21182 extend_face_to_end_of_line (it);
21184 if (nglyphs > 1 && i > 0)
21186 row->ends_in_middle_of_char_p = true;
21187 it->starts_in_middle_of_char_p = true;
21190 /* Restore the height to what it was before the
21191 element not fitting on the line. */
21192 it->max_ascent = ascent;
21193 it->max_descent = descent;
21194 it->max_phys_ascent = phys_ascent;
21195 it->max_phys_descent = phys_descent;
21198 break;
21200 else if (new_x > it->first_visible_x)
21202 /* Increment number of glyphs actually displayed. */
21203 ++it->hpos;
21205 /* Record the maximum and minimum buffer positions
21206 seen so far in glyphs that will be displayed by
21207 this row. */
21208 if (it->bidi_p)
21209 RECORD_MAX_MIN_POS (it);
21211 if (x < it->first_visible_x && !row->reversed_p)
21212 /* Glyph is partially visible, i.e. row starts at
21213 negative X position. Don't do that in R2L
21214 rows, where we arrange to add a right offset to
21215 the line in extend_face_to_end_of_line, by a
21216 suitable change to the stretch glyph that is
21217 the leftmost glyph of the line. */
21218 row->x = x - it->first_visible_x;
21219 /* When the last glyph of an R2L row only fits
21220 partially on the line, we need to set row->x to a
21221 negative offset, so that the leftmost glyph is
21222 the one that is partially visible. But if we are
21223 going to produce the truncation glyph, this will
21224 be taken care of in produce_special_glyphs. */
21225 if (row->reversed_p
21226 && new_x > it->last_visible_x
21227 && !(it->line_wrap == TRUNCATE
21228 && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0))
21230 eassert (FRAME_WINDOW_P (it->f));
21231 row->x = it->last_visible_x - new_x;
21234 else
21236 /* Glyph is completely off the left margin of the
21237 window. This should not happen because of the
21238 move_it_in_display_line at the start of this
21239 function, unless the text display area of the
21240 window is empty. */
21241 eassert (it->first_visible_x <= it->last_visible_x);
21244 /* Even if this display element produced no glyphs at all,
21245 we want to record its position. */
21246 if (it->bidi_p && nglyphs == 0)
21247 RECORD_MAX_MIN_POS (it);
21249 row->ascent = max (row->ascent, it->max_ascent);
21250 row->height = max (row->height, it->max_ascent + it->max_descent);
21251 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
21252 row->phys_height = max (row->phys_height,
21253 it->max_phys_ascent + it->max_phys_descent);
21254 row->extra_line_spacing = max (row->extra_line_spacing,
21255 it->max_extra_line_spacing);
21257 /* End of this display line if row is continued. */
21258 if (row->continued_p || row->ends_at_zv_p)
21259 break;
21262 at_end_of_line:
21263 /* Is this a line end? If yes, we're also done, after making
21264 sure that a non-default face is extended up to the right
21265 margin of the window. */
21266 if (ITERATOR_AT_END_OF_LINE_P (it))
21268 int used_before = row->used[TEXT_AREA];
21270 row->ends_in_newline_from_string_p = STRINGP (it->object);
21272 /* Add a space at the end of the line that is used to
21273 display the cursor there. */
21274 if (!IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
21275 append_space_for_newline (it, false);
21277 /* Extend the face to the end of the line. */
21278 extend_face_to_end_of_line (it);
21280 /* Make sure we have the position. */
21281 if (used_before == 0)
21282 row->glyphs[TEXT_AREA]->charpos = CHARPOS (it->position);
21284 /* Record the position of the newline, for use in
21285 find_row_edges. */
21286 it->eol_pos = it->current.pos;
21288 /* Consume the line end. This skips over invisible lines. */
21289 set_iterator_to_next (it, true);
21290 it->continuation_lines_width = 0;
21291 break;
21294 /* Proceed with next display element. Note that this skips
21295 over lines invisible because of selective display. */
21296 set_iterator_to_next (it, true);
21298 /* If we truncate lines, we are done when the last displayed
21299 glyphs reach past the right margin of the window. */
21300 if (it->line_wrap == TRUNCATE
21301 && ((FRAME_WINDOW_P (it->f)
21302 /* Images are preprocessed in produce_image_glyph such
21303 that they are cropped at the right edge of the
21304 window, so an image glyph will always end exactly at
21305 last_visible_x, even if there's no right fringe. */
21306 && ((row->reversed_p
21307 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
21308 : WINDOW_RIGHT_FRINGE_WIDTH (it->w))
21309 || it->what == IT_IMAGE))
21310 ? (it->current_x >= it->last_visible_x)
21311 : (it->current_x > it->last_visible_x)))
21313 /* Maybe add truncation glyphs. */
21314 if (!FRAME_WINDOW_P (it->f)
21315 || (row->reversed_p
21316 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
21317 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
21319 int i, n;
21321 if (!row->reversed_p)
21323 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
21324 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
21325 break;
21327 else
21329 for (i = 0; i < row->used[TEXT_AREA]; i++)
21330 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
21331 break;
21332 /* Remove any padding glyphs at the front of ROW, to
21333 make room for the truncation glyphs we will be
21334 adding below. The loop below always inserts at
21335 least one truncation glyph, so also remove the
21336 last glyph added to ROW. */
21337 unproduce_glyphs (it, i + 1);
21338 /* Adjust i for the loop below. */
21339 i = row->used[TEXT_AREA] - (i + 1);
21342 /* produce_special_glyphs overwrites the last glyph, so
21343 we don't want that if we want to keep that last
21344 glyph, which means it's an image. */
21345 if (it->current_x > it->last_visible_x)
21347 it->current_x = x_before;
21348 if (!FRAME_WINDOW_P (it->f))
21350 for (n = row->used[TEXT_AREA]; i < n; ++i)
21352 row->used[TEXT_AREA] = i;
21353 produce_special_glyphs (it, IT_TRUNCATION);
21356 else
21358 row->used[TEXT_AREA] = i;
21359 produce_special_glyphs (it, IT_TRUNCATION);
21361 it->hpos = hpos_before;
21364 else if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
21366 /* Don't truncate if we can overflow newline into fringe. */
21367 if (!get_next_display_element (it))
21369 it->continuation_lines_width = 0;
21370 it->font_height = Qnil;
21371 it->voffset = 0;
21372 row->ends_at_zv_p = true;
21373 row->exact_window_width_line_p = true;
21374 break;
21376 if (ITERATOR_AT_END_OF_LINE_P (it))
21378 row->exact_window_width_line_p = true;
21379 goto at_end_of_line;
21381 it->current_x = x_before;
21382 it->hpos = hpos_before;
21385 row->truncated_on_right_p = true;
21386 it->continuation_lines_width = 0;
21387 reseat_at_next_visible_line_start (it, false);
21388 /* We insist below that IT's position be at ZV because in
21389 bidi-reordered lines the character at visible line start
21390 might not be the character that follows the newline in
21391 the logical order. */
21392 if (IT_BYTEPOS (*it) > BEG_BYTE)
21393 row->ends_at_zv_p =
21394 IT_BYTEPOS (*it) >= ZV_BYTE && FETCH_BYTE (ZV_BYTE - 1) != '\n';
21395 else
21396 row->ends_at_zv_p = false;
21397 break;
21401 if (wrap_data)
21402 bidi_unshelve_cache (wrap_data, true);
21404 /* If line is not empty and hscrolled, maybe insert truncation glyphs
21405 at the left window margin. */
21406 if (it->first_visible_x
21407 && IT_CHARPOS (*it) != CHARPOS (row->start.pos))
21409 if (!FRAME_WINDOW_P (it->f)
21410 || (((row->reversed_p
21411 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
21412 : WINDOW_LEFT_FRINGE_WIDTH (it->w)) == 0)
21413 /* Don't let insert_left_trunc_glyphs overwrite the
21414 first glyph of the row if it is an image. */
21415 && row->glyphs[TEXT_AREA]->type != IMAGE_GLYPH))
21416 insert_left_trunc_glyphs (it);
21417 row->truncated_on_left_p = true;
21420 /* Remember the position at which this line ends.
21422 BIDI Note: any code that needs MATRIX_ROW_START/END_CHARPOS
21423 cannot be before the call to find_row_edges below, since that is
21424 where these positions are determined. */
21425 row->end = it->current;
21426 if (!it->bidi_p)
21428 row->minpos = row->start.pos;
21429 row->maxpos = row->end.pos;
21431 else
21433 /* ROW->minpos and ROW->maxpos must be the smallest and
21434 `1 + the largest' buffer positions in ROW. But if ROW was
21435 bidi-reordered, these two positions can be anywhere in the
21436 row, so we must determine them now. */
21437 find_row_edges (it, row, min_pos, min_bpos, max_pos, max_bpos);
21440 /* If the start of this line is the overlay arrow-position, then
21441 mark this glyph row as the one containing the overlay arrow.
21442 This is clearly a mess with variable size fonts. It would be
21443 better to let it be displayed like cursors under X. */
21444 if ((MATRIX_ROW_DISPLAYS_TEXT_P (row) || !overlay_arrow_seen)
21445 && (overlay_arrow_string = overlay_arrow_at_row (it, row),
21446 !NILP (overlay_arrow_string)))
21448 /* Overlay arrow in window redisplay is a fringe bitmap. */
21449 if (STRINGP (overlay_arrow_string))
21451 struct glyph_row *arrow_row
21452 = get_overlay_arrow_glyph_row (it->w, overlay_arrow_string);
21453 struct glyph *glyph = arrow_row->glyphs[TEXT_AREA];
21454 struct glyph *arrow_end = glyph + arrow_row->used[TEXT_AREA];
21455 struct glyph *p = row->glyphs[TEXT_AREA];
21456 struct glyph *p2, *end;
21458 /* Copy the arrow glyphs. */
21459 while (glyph < arrow_end)
21460 *p++ = *glyph++;
21462 /* Throw away padding glyphs. */
21463 p2 = p;
21464 end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
21465 while (p2 < end && CHAR_GLYPH_PADDING_P (*p2))
21466 ++p2;
21467 if (p2 > p)
21469 while (p2 < end)
21470 *p++ = *p2++;
21471 row->used[TEXT_AREA] = p2 - row->glyphs[TEXT_AREA];
21474 else
21476 eassert (INTEGERP (overlay_arrow_string));
21477 row->overlay_arrow_bitmap = XINT (overlay_arrow_string);
21479 overlay_arrow_seen = true;
21482 /* Highlight trailing whitespace. */
21483 if (!NILP (Vshow_trailing_whitespace))
21484 highlight_trailing_whitespace (it->f, it->glyph_row);
21486 /* Compute pixel dimensions of this line. */
21487 compute_line_metrics (it);
21489 /* Implementation note: No changes in the glyphs of ROW or in their
21490 faces can be done past this point, because compute_line_metrics
21491 computes ROW's hash value and stores it within the glyph_row
21492 structure. */
21494 /* Record whether this row ends inside an ellipsis. */
21495 row->ends_in_ellipsis_p
21496 = (it->method == GET_FROM_DISPLAY_VECTOR
21497 && it->ellipsis_p);
21499 /* Save fringe bitmaps in this row. */
21500 row->left_user_fringe_bitmap = it->left_user_fringe_bitmap;
21501 row->left_user_fringe_face_id = it->left_user_fringe_face_id;
21502 row->right_user_fringe_bitmap = it->right_user_fringe_bitmap;
21503 row->right_user_fringe_face_id = it->right_user_fringe_face_id;
21505 it->left_user_fringe_bitmap = 0;
21506 it->left_user_fringe_face_id = 0;
21507 it->right_user_fringe_bitmap = 0;
21508 it->right_user_fringe_face_id = 0;
21510 /* Maybe set the cursor. */
21511 cvpos = it->w->cursor.vpos;
21512 if ((cvpos < 0
21513 /* In bidi-reordered rows, keep checking for proper cursor
21514 position even if one has been found already, because buffer
21515 positions in such rows change non-linearly with ROW->VPOS,
21516 when a line is continued. One exception: when we are at ZV,
21517 display cursor on the first suitable glyph row, since all
21518 the empty rows after that also have their position set to ZV. */
21519 /* FIXME: Revisit this when glyph ``spilling'' in continuation
21520 lines' rows is implemented for bidi-reordered rows. */
21521 || (it->bidi_p
21522 && !MATRIX_ROW (it->w->desired_matrix, cvpos)->ends_at_zv_p))
21523 && PT >= MATRIX_ROW_START_CHARPOS (row)
21524 && PT <= MATRIX_ROW_END_CHARPOS (row)
21525 && cursor_row_p (row))
21526 set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
21528 /* Prepare for the next line. This line starts horizontally at (X
21529 HPOS) = (0 0). Vertical positions are incremented. As a
21530 convenience for the caller, IT->glyph_row is set to the next
21531 row to be used. */
21532 it->current_x = it->hpos = 0;
21533 it->current_y += row->height;
21534 /* Restore the first and last visible X if we adjusted them for
21535 current-line hscrolling. */
21536 if (hscroll_this_line)
21538 it->first_visible_x = first_visible_x;
21539 it->last_visible_x = last_visible_x;
21541 SET_TEXT_POS (it->eol_pos, 0, 0);
21542 ++it->vpos;
21543 ++it->glyph_row;
21544 /* The next row should by default use the same value of the
21545 reversed_p flag as this one. set_iterator_to_next decides when
21546 it's a new paragraph, and PRODUCE_GLYPHS recomputes the value of
21547 the flag accordingly. */
21548 if (it->glyph_row < MATRIX_BOTTOM_TEXT_ROW (it->w->desired_matrix, it->w))
21549 it->glyph_row->reversed_p = row->reversed_p;
21550 it->start = row->end;
21551 return MATRIX_ROW_DISPLAYS_TEXT_P (row);
21553 #undef RECORD_MAX_MIN_POS
21556 DEFUN ("current-bidi-paragraph-direction", Fcurrent_bidi_paragraph_direction,
21557 Scurrent_bidi_paragraph_direction, 0, 1, 0,
21558 doc: /* Return paragraph direction at point in BUFFER.
21559 Value is either `left-to-right' or `right-to-left'.
21560 If BUFFER is omitted or nil, it defaults to the current buffer.
21562 Paragraph direction determines how the text in the paragraph is displayed.
21563 In left-to-right paragraphs, text begins at the left margin of the window
21564 and the reading direction is generally left to right. In right-to-left
21565 paragraphs, text begins at the right margin and is read from right to left.
21567 See also `bidi-paragraph-direction'. */)
21568 (Lisp_Object buffer)
21570 struct buffer *buf = current_buffer;
21571 struct buffer *old = buf;
21573 if (! NILP (buffer))
21575 CHECK_BUFFER (buffer);
21576 buf = XBUFFER (buffer);
21579 if (NILP (BVAR (buf, bidi_display_reordering))
21580 || NILP (BVAR (buf, enable_multibyte_characters))
21581 /* When we are loading loadup.el, the character property tables
21582 needed for bidi iteration are not yet available. */
21583 || redisplay__inhibit_bidi)
21584 return Qleft_to_right;
21585 else if (!NILP (BVAR (buf, bidi_paragraph_direction)))
21586 return BVAR (buf, bidi_paragraph_direction);
21587 else
21589 /* Determine the direction from buffer text. We could try to
21590 use current_matrix if it is up to date, but this seems fast
21591 enough as it is. */
21592 struct bidi_it itb;
21593 ptrdiff_t pos = BUF_PT (buf);
21594 ptrdiff_t bytepos = BUF_PT_BYTE (buf);
21595 int c;
21596 void *itb_data = bidi_shelve_cache ();
21598 set_buffer_temp (buf);
21599 /* bidi_paragraph_init finds the base direction of the paragraph
21600 by searching forward from paragraph start. We need the base
21601 direction of the current or _previous_ paragraph, so we need
21602 to make sure we are within that paragraph. To that end, find
21603 the previous non-empty line. */
21604 if (pos >= ZV && pos > BEGV)
21605 DEC_BOTH (pos, bytepos);
21606 AUTO_STRING (trailing_white_space, "[\f\t ]*\n");
21607 if (fast_looking_at (trailing_white_space,
21608 pos, bytepos, ZV, ZV_BYTE, Qnil) > 0)
21610 while ((c = FETCH_BYTE (bytepos)) == '\n'
21611 || c == ' ' || c == '\t' || c == '\f')
21613 if (bytepos <= BEGV_BYTE)
21614 break;
21615 bytepos--;
21616 pos--;
21618 while (!CHAR_HEAD_P (FETCH_BYTE (bytepos)))
21619 bytepos--;
21621 bidi_init_it (pos, bytepos, FRAME_WINDOW_P (SELECTED_FRAME ()), &itb);
21622 itb.paragraph_dir = NEUTRAL_DIR;
21623 itb.string.s = NULL;
21624 itb.string.lstring = Qnil;
21625 itb.string.bufpos = 0;
21626 itb.string.from_disp_str = false;
21627 itb.string.unibyte = false;
21628 /* We have no window to use here for ignoring window-specific
21629 overlays. Using NULL for window pointer will cause
21630 compute_display_string_pos to use the current buffer. */
21631 itb.w = NULL;
21632 bidi_paragraph_init (NEUTRAL_DIR, &itb, true);
21633 bidi_unshelve_cache (itb_data, false);
21634 set_buffer_temp (old);
21635 switch (itb.paragraph_dir)
21637 case L2R:
21638 return Qleft_to_right;
21639 break;
21640 case R2L:
21641 return Qright_to_left;
21642 break;
21643 default:
21644 emacs_abort ();
21649 DEFUN ("bidi-find-overridden-directionality",
21650 Fbidi_find_overridden_directionality,
21651 Sbidi_find_overridden_directionality, 2, 3, 0,
21652 doc: /* Return position between FROM and TO where directionality was overridden.
21654 This function returns the first character position in the specified
21655 region of OBJECT where there is a character whose `bidi-class' property
21656 is `L', but which was forced to display as `R' by a directional
21657 override, and likewise with characters whose `bidi-class' is `R'
21658 or `AL' that were forced to display as `L'.
21660 If no such character is found, the function returns nil.
21662 OBJECT is a Lisp string or buffer to search for overridden
21663 directionality, and defaults to the current buffer if nil or omitted.
21664 OBJECT can also be a window, in which case the function will search
21665 the buffer displayed in that window. Passing the window instead of
21666 a buffer is preferable when the buffer is displayed in some window,
21667 because this function will then be able to correctly account for
21668 window-specific overlays, which can affect the results.
21670 Strong directional characters `L', `R', and `AL' can have their
21671 intrinsic directionality overridden by directional override
21672 control characters RLO (u+202e) and LRO (u+202d). See the
21673 function `get-char-code-property' for a way to inquire about
21674 the `bidi-class' property of a character. */)
21675 (Lisp_Object from, Lisp_Object to, Lisp_Object object)
21677 struct buffer *buf = current_buffer;
21678 struct buffer *old = buf;
21679 struct window *w = NULL;
21680 bool frame_window_p = FRAME_WINDOW_P (SELECTED_FRAME ());
21681 struct bidi_it itb;
21682 ptrdiff_t from_pos, to_pos, from_bpos;
21683 void *itb_data;
21685 if (!NILP (object))
21687 if (BUFFERP (object))
21688 buf = XBUFFER (object);
21689 else if (WINDOWP (object))
21691 w = decode_live_window (object);
21692 buf = XBUFFER (w->contents);
21693 frame_window_p = FRAME_WINDOW_P (XFRAME (w->frame));
21695 else
21696 CHECK_STRING (object);
21699 if (STRINGP (object))
21701 /* Characters in unibyte strings are always treated by bidi.c as
21702 strong LTR. */
21703 if (!STRING_MULTIBYTE (object)
21704 /* When we are loading loadup.el, the character property
21705 tables needed for bidi iteration are not yet
21706 available. */
21707 || redisplay__inhibit_bidi)
21708 return Qnil;
21710 validate_subarray (object, from, to, SCHARS (object), &from_pos, &to_pos);
21711 if (from_pos >= SCHARS (object))
21712 return Qnil;
21714 /* Set up the bidi iterator. */
21715 itb_data = bidi_shelve_cache ();
21716 itb.paragraph_dir = NEUTRAL_DIR;
21717 itb.string.lstring = object;
21718 itb.string.s = NULL;
21719 itb.string.schars = SCHARS (object);
21720 itb.string.bufpos = 0;
21721 itb.string.from_disp_str = false;
21722 itb.string.unibyte = false;
21723 itb.w = w;
21724 bidi_init_it (0, 0, frame_window_p, &itb);
21726 else
21728 /* Nothing this fancy can happen in unibyte buffers, or in a
21729 buffer that disabled reordering, or if FROM is at EOB. */
21730 if (NILP (BVAR (buf, bidi_display_reordering))
21731 || NILP (BVAR (buf, enable_multibyte_characters))
21732 /* When we are loading loadup.el, the character property
21733 tables needed for bidi iteration are not yet
21734 available. */
21735 || redisplay__inhibit_bidi)
21736 return Qnil;
21738 set_buffer_temp (buf);
21739 validate_region (&from, &to);
21740 from_pos = XINT (from);
21741 to_pos = XINT (to);
21742 if (from_pos >= ZV)
21743 return Qnil;
21745 /* Set up the bidi iterator. */
21746 itb_data = bidi_shelve_cache ();
21747 from_bpos = CHAR_TO_BYTE (from_pos);
21748 if (from_pos == BEGV)
21750 itb.charpos = BEGV;
21751 itb.bytepos = BEGV_BYTE;
21753 else if (FETCH_CHAR (from_bpos - 1) == '\n')
21755 itb.charpos = from_pos;
21756 itb.bytepos = from_bpos;
21758 else
21759 itb.charpos = find_newline_no_quit (from_pos, CHAR_TO_BYTE (from_pos),
21760 -1, &itb.bytepos);
21761 itb.paragraph_dir = NEUTRAL_DIR;
21762 itb.string.s = NULL;
21763 itb.string.lstring = Qnil;
21764 itb.string.bufpos = 0;
21765 itb.string.from_disp_str = false;
21766 itb.string.unibyte = false;
21767 itb.w = w;
21768 bidi_init_it (itb.charpos, itb.bytepos, frame_window_p, &itb);
21771 ptrdiff_t found;
21772 do {
21773 /* For the purposes of this function, the actual base direction of
21774 the paragraph doesn't matter, so just set it to L2R. */
21775 bidi_paragraph_init (L2R, &itb, false);
21776 while ((found = bidi_find_first_overridden (&itb)) < from_pos)
21778 } while (found == ZV && itb.ch == '\n' && itb.charpos < to_pos);
21780 bidi_unshelve_cache (itb_data, false);
21781 set_buffer_temp (old);
21783 return (from_pos <= found && found < to_pos) ? make_number (found) : Qnil;
21786 DEFUN ("move-point-visually", Fmove_point_visually,
21787 Smove_point_visually, 1, 1, 0,
21788 doc: /* Move point in the visual order in the specified DIRECTION.
21789 DIRECTION can be 1, meaning move to the right, or -1, which moves to the
21790 left.
21792 Value is the new character position of point. */)
21793 (Lisp_Object direction)
21795 struct window *w = XWINDOW (selected_window);
21796 struct buffer *b = XBUFFER (w->contents);
21797 struct glyph_row *row;
21798 int dir;
21799 Lisp_Object paragraph_dir;
21801 #define ROW_GLYPH_NEWLINE_P(ROW,GLYPH) \
21802 (!(ROW)->continued_p \
21803 && NILP ((GLYPH)->object) \
21804 && (GLYPH)->type == CHAR_GLYPH \
21805 && (GLYPH)->u.ch == ' ' \
21806 && (GLYPH)->charpos >= 0 \
21807 && !(GLYPH)->avoid_cursor_p)
21809 CHECK_NUMBER (direction);
21810 dir = XINT (direction);
21811 if (dir > 0)
21812 dir = 1;
21813 else
21814 dir = -1;
21816 /* If current matrix is up-to-date, we can use the information
21817 recorded in the glyphs, at least as long as the goal is on the
21818 screen. */
21819 if (w->window_end_valid
21820 && !windows_or_buffers_changed
21821 && b
21822 && !b->clip_changed
21823 && !b->prevent_redisplay_optimizations_p
21824 && !window_outdated (w)
21825 /* We rely below on the cursor coordinates to be up to date, but
21826 we cannot trust them if some command moved point since the
21827 last complete redisplay. */
21828 && w->last_point == BUF_PT (b)
21829 && w->cursor.vpos >= 0
21830 && w->cursor.vpos < w->current_matrix->nrows
21831 && (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos))->enabled_p)
21833 struct glyph *g = row->glyphs[TEXT_AREA];
21834 struct glyph *e = dir > 0 ? g + row->used[TEXT_AREA] : g - 1;
21835 struct glyph *gpt = g + w->cursor.hpos;
21837 for (g = gpt + dir; (dir > 0 ? g < e : g > e); g += dir)
21839 if (BUFFERP (g->object) && g->charpos != PT)
21841 SET_PT (g->charpos);
21842 w->cursor.vpos = -1;
21843 return make_number (PT);
21845 else if (!NILP (g->object) && !EQ (g->object, gpt->object))
21847 ptrdiff_t new_pos;
21849 if (BUFFERP (gpt->object))
21851 new_pos = PT;
21852 if ((gpt->resolved_level - row->reversed_p) % 2 == 0)
21853 new_pos += (row->reversed_p ? -dir : dir);
21854 else
21855 new_pos -= (row->reversed_p ? -dir : dir);
21857 else if (BUFFERP (g->object))
21858 new_pos = g->charpos;
21859 else
21860 break;
21861 SET_PT (new_pos);
21862 w->cursor.vpos = -1;
21863 return make_number (PT);
21865 else if (ROW_GLYPH_NEWLINE_P (row, g))
21867 /* Glyphs inserted at the end of a non-empty line for
21868 positioning the cursor have zero charpos, so we must
21869 deduce the value of point by other means. */
21870 if (g->charpos > 0)
21871 SET_PT (g->charpos);
21872 else if (row->ends_at_zv_p && PT != ZV)
21873 SET_PT (ZV);
21874 else if (PT != MATRIX_ROW_END_CHARPOS (row) - 1)
21875 SET_PT (MATRIX_ROW_END_CHARPOS (row) - 1);
21876 else
21877 break;
21878 w->cursor.vpos = -1;
21879 return make_number (PT);
21882 if (g == e || NILP (g->object))
21884 if (row->truncated_on_left_p || row->truncated_on_right_p)
21885 goto simulate_display;
21886 if (!row->reversed_p)
21887 row += dir;
21888 else
21889 row -= dir;
21890 if (row < MATRIX_FIRST_TEXT_ROW (w->current_matrix)
21891 || row > MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w))
21892 goto simulate_display;
21894 if (dir > 0)
21896 if (row->reversed_p && !row->continued_p)
21898 SET_PT (MATRIX_ROW_END_CHARPOS (row) - 1);
21899 w->cursor.vpos = -1;
21900 return make_number (PT);
21902 g = row->glyphs[TEXT_AREA];
21903 e = g + row->used[TEXT_AREA];
21904 for ( ; g < e; g++)
21906 if (BUFFERP (g->object)
21907 /* Empty lines have only one glyph, which stands
21908 for the newline, and whose charpos is the
21909 buffer position of the newline. */
21910 || ROW_GLYPH_NEWLINE_P (row, g)
21911 /* When the buffer ends in a newline, the line at
21912 EOB also has one glyph, but its charpos is -1. */
21913 || (row->ends_at_zv_p
21914 && !row->reversed_p
21915 && NILP (g->object)
21916 && g->type == CHAR_GLYPH
21917 && g->u.ch == ' '))
21919 if (g->charpos > 0)
21920 SET_PT (g->charpos);
21921 else if (!row->reversed_p
21922 && row->ends_at_zv_p
21923 && PT != ZV)
21924 SET_PT (ZV);
21925 else
21926 continue;
21927 w->cursor.vpos = -1;
21928 return make_number (PT);
21932 else
21934 if (!row->reversed_p && !row->continued_p)
21936 SET_PT (MATRIX_ROW_END_CHARPOS (row) - 1);
21937 w->cursor.vpos = -1;
21938 return make_number (PT);
21940 e = row->glyphs[TEXT_AREA];
21941 g = e + row->used[TEXT_AREA] - 1;
21942 for ( ; g >= e; g--)
21944 if (BUFFERP (g->object)
21945 || (ROW_GLYPH_NEWLINE_P (row, g)
21946 && g->charpos > 0)
21947 /* Empty R2L lines on GUI frames have the buffer
21948 position of the newline stored in the stretch
21949 glyph. */
21950 || g->type == STRETCH_GLYPH
21951 || (row->ends_at_zv_p
21952 && row->reversed_p
21953 && NILP (g->object)
21954 && g->type == CHAR_GLYPH
21955 && g->u.ch == ' '))
21957 if (g->charpos > 0)
21958 SET_PT (g->charpos);
21959 else if (row->reversed_p
21960 && row->ends_at_zv_p
21961 && PT != ZV)
21962 SET_PT (ZV);
21963 else
21964 continue;
21965 w->cursor.vpos = -1;
21966 return make_number (PT);
21973 simulate_display:
21975 /* If we wind up here, we failed to move by using the glyphs, so we
21976 need to simulate display instead. */
21978 if (b)
21979 paragraph_dir = Fcurrent_bidi_paragraph_direction (w->contents);
21980 else
21981 paragraph_dir = Qleft_to_right;
21982 if (EQ (paragraph_dir, Qright_to_left))
21983 dir = -dir;
21984 if (PT <= BEGV && dir < 0)
21985 xsignal0 (Qbeginning_of_buffer);
21986 else if (PT >= ZV && dir > 0)
21987 xsignal0 (Qend_of_buffer);
21988 else
21990 struct text_pos pt;
21991 struct it it;
21992 int pt_x, target_x, pixel_width, pt_vpos;
21993 bool at_eol_p;
21994 bool overshoot_expected = false;
21995 bool target_is_eol_p = false;
21997 /* Setup the arena. */
21998 SET_TEXT_POS (pt, PT, PT_BYTE);
21999 start_display (&it, w, pt);
22000 /* When lines are truncated, we could be called with point
22001 outside of the windows edges, in which case move_it_*
22002 functions either prematurely stop at window's edge or jump to
22003 the next screen line, whereas we rely below on our ability to
22004 reach point, in order to start from its X coordinate. So we
22005 need to disregard the window's horizontal extent in that case. */
22006 if (it.line_wrap == TRUNCATE)
22007 it.last_visible_x = INFINITY;
22009 if (it.cmp_it.id < 0
22010 && it.method == GET_FROM_STRING
22011 && it.area == TEXT_AREA
22012 && it.string_from_display_prop_p
22013 && (it.sp > 0 && it.stack[it.sp - 1].method == GET_FROM_BUFFER))
22014 overshoot_expected = true;
22016 /* Find the X coordinate of point. We start from the beginning
22017 of this or previous line to make sure we are before point in
22018 the logical order (since the move_it_* functions can only
22019 move forward). */
22020 reseat:
22021 reseat_at_previous_visible_line_start (&it);
22022 it.current_x = it.hpos = it.current_y = it.vpos = 0;
22023 if (IT_CHARPOS (it) != PT)
22025 move_it_to (&it, overshoot_expected ? PT - 1 : PT,
22026 -1, -1, -1, MOVE_TO_POS);
22027 /* If we missed point because the character there is
22028 displayed out of a display vector that has more than one
22029 glyph, retry expecting overshoot. */
22030 if (it.method == GET_FROM_DISPLAY_VECTOR
22031 && it.current.dpvec_index > 0
22032 && !overshoot_expected)
22034 overshoot_expected = true;
22035 goto reseat;
22037 else if (IT_CHARPOS (it) != PT && !overshoot_expected)
22038 move_it_in_display_line (&it, PT, -1, MOVE_TO_POS);
22040 pt_x = it.current_x;
22041 pt_vpos = it.vpos;
22042 if (dir > 0 || overshoot_expected)
22044 struct glyph_row *row = it.glyph_row;
22046 /* When point is at beginning of line, we don't have
22047 information about the glyph there loaded into struct
22048 it. Calling get_next_display_element fixes that. */
22049 if (pt_x == 0)
22050 get_next_display_element (&it);
22051 at_eol_p = ITERATOR_AT_END_OF_LINE_P (&it);
22052 it.glyph_row = NULL;
22053 PRODUCE_GLYPHS (&it); /* compute it.pixel_width */
22054 it.glyph_row = row;
22055 /* PRODUCE_GLYPHS advances it.current_x, so we must restore
22056 it, lest it will become out of sync with it's buffer
22057 position. */
22058 it.current_x = pt_x;
22060 else
22061 at_eol_p = ITERATOR_AT_END_OF_LINE_P (&it);
22062 pixel_width = it.pixel_width;
22063 if (overshoot_expected && at_eol_p)
22064 pixel_width = 0;
22065 else if (pixel_width <= 0)
22066 pixel_width = 1;
22068 /* If there's a display string (or something similar) at point,
22069 we are actually at the glyph to the left of point, so we need
22070 to correct the X coordinate. */
22071 if (overshoot_expected)
22073 if (it.bidi_p)
22074 pt_x += pixel_width * it.bidi_it.scan_dir;
22075 else
22076 pt_x += pixel_width;
22079 /* Compute target X coordinate, either to the left or to the
22080 right of point. On TTY frames, all characters have the same
22081 pixel width of 1, so we can use that. On GUI frames we don't
22082 have an easy way of getting at the pixel width of the
22083 character to the left of point, so we use a different method
22084 of getting to that place. */
22085 if (dir > 0)
22086 target_x = pt_x + pixel_width;
22087 else
22088 target_x = pt_x - (!FRAME_WINDOW_P (it.f)) * pixel_width;
22090 /* Target X coordinate could be one line above or below the line
22091 of point, in which case we need to adjust the target X
22092 coordinate. Also, if moving to the left, we need to begin at
22093 the left edge of the point's screen line. */
22094 if (dir < 0)
22096 if (pt_x > 0)
22098 start_display (&it, w, pt);
22099 if (it.line_wrap == TRUNCATE)
22100 it.last_visible_x = INFINITY;
22101 reseat_at_previous_visible_line_start (&it);
22102 it.current_x = it.current_y = it.hpos = 0;
22103 if (pt_vpos != 0)
22104 move_it_by_lines (&it, pt_vpos);
22106 else
22108 move_it_by_lines (&it, -1);
22109 target_x = it.last_visible_x - !FRAME_WINDOW_P (it.f);
22110 target_is_eol_p = true;
22111 /* Under word-wrap, we don't know the x coordinate of
22112 the last character displayed on the previous line,
22113 which immediately precedes the wrap point. To find
22114 out its x coordinate, we try moving to the right
22115 margin of the window, which will stop at the wrap
22116 point, and then reset target_x to point at the
22117 character that precedes the wrap point. This is not
22118 needed on GUI frames, because (see below) there we
22119 move from the left margin one grapheme cluster at a
22120 time, and stop when we hit the wrap point. */
22121 if (!FRAME_WINDOW_P (it.f) && it.line_wrap == WORD_WRAP)
22123 void *it_data = NULL;
22124 struct it it2;
22126 SAVE_IT (it2, it, it_data);
22127 move_it_in_display_line_to (&it, ZV, target_x,
22128 MOVE_TO_POS | MOVE_TO_X);
22129 /* If we arrived at target_x, that _is_ the last
22130 character on the previous line. */
22131 if (it.current_x != target_x)
22132 target_x = it.current_x - 1;
22133 RESTORE_IT (&it, &it2, it_data);
22137 else
22139 if (at_eol_p
22140 || (target_x >= it.last_visible_x
22141 && it.line_wrap != TRUNCATE))
22143 if (pt_x > 0)
22144 move_it_by_lines (&it, 0);
22145 move_it_by_lines (&it, 1);
22146 target_x = 0;
22150 /* Move to the target X coordinate. */
22151 /* On GUI frames, as we don't know the X coordinate of the
22152 character to the left of point, moving point to the left
22153 requires walking, one grapheme cluster at a time, until we
22154 find ourself at a place immediately to the left of the
22155 character at point. */
22156 if (FRAME_WINDOW_P (it.f) && dir < 0)
22158 struct text_pos new_pos;
22159 enum move_it_result rc = MOVE_X_REACHED;
22161 if (it.current_x == 0)
22162 get_next_display_element (&it);
22163 if (it.what == IT_COMPOSITION)
22165 new_pos.charpos = it.cmp_it.charpos;
22166 new_pos.bytepos = -1;
22168 else
22169 new_pos = it.current.pos;
22171 while (it.current_x + it.pixel_width <= target_x
22172 && (rc == MOVE_X_REACHED
22173 /* Under word-wrap, move_it_in_display_line_to
22174 stops at correct coordinates, but sometimes
22175 returns MOVE_POS_MATCH_OR_ZV. */
22176 || (it.line_wrap == WORD_WRAP
22177 && rc == MOVE_POS_MATCH_OR_ZV)))
22179 int new_x = it.current_x + it.pixel_width;
22181 /* For composed characters, we want the position of the
22182 first character in the grapheme cluster (usually, the
22183 composition's base character), whereas it.current
22184 might give us the position of the _last_ one, e.g. if
22185 the composition is rendered in reverse due to bidi
22186 reordering. */
22187 if (it.what == IT_COMPOSITION)
22189 new_pos.charpos = it.cmp_it.charpos;
22190 new_pos.bytepos = -1;
22192 else
22193 new_pos = it.current.pos;
22194 if (new_x == it.current_x)
22195 new_x++;
22196 rc = move_it_in_display_line_to (&it, ZV, new_x,
22197 MOVE_TO_POS | MOVE_TO_X);
22198 if (ITERATOR_AT_END_OF_LINE_P (&it) && !target_is_eol_p)
22199 break;
22201 /* The previous position we saw in the loop is the one we
22202 want. */
22203 if (new_pos.bytepos == -1)
22204 new_pos.bytepos = CHAR_TO_BYTE (new_pos.charpos);
22205 it.current.pos = new_pos;
22207 else if (it.current_x != target_x)
22208 move_it_in_display_line_to (&it, ZV, target_x, MOVE_TO_POS | MOVE_TO_X);
22210 /* If we ended up in a display string that covers point, move to
22211 buffer position to the right in the visual order. */
22212 if (dir > 0)
22214 while (IT_CHARPOS (it) == PT)
22216 set_iterator_to_next (&it, false);
22217 if (!get_next_display_element (&it))
22218 break;
22222 /* Move point to that position. */
22223 SET_PT_BOTH (IT_CHARPOS (it), IT_BYTEPOS (it));
22226 return make_number (PT);
22228 #undef ROW_GLYPH_NEWLINE_P
22231 DEFUN ("bidi-resolved-levels", Fbidi_resolved_levels,
22232 Sbidi_resolved_levels, 0, 1, 0,
22233 doc: /* Return the resolved bidirectional levels of characters at VPOS.
22235 The resolved levels are produced by the Emacs bidi reordering engine
22236 that implements the UBA, the Unicode Bidirectional Algorithm. Please
22237 read the Unicode Standard Annex 9 (UAX#9) for background information
22238 about these levels.
22240 VPOS is the zero-based number of the current window's screen line
22241 for which to produce the resolved levels. If VPOS is nil or omitted,
22242 it defaults to the screen line of point. If the window displays a
22243 header line, VPOS of zero will report on the header line, and first
22244 line of text in the window will have VPOS of 1.
22246 Value is an array of resolved levels, indexed by glyph number.
22247 Glyphs are numbered from zero starting from the beginning of the
22248 screen line, i.e. the left edge of the window for left-to-right lines
22249 and from the right edge for right-to-left lines. The resolved levels
22250 are produced only for the window's text area; text in display margins
22251 is not included.
22253 If the selected window's display is not up-to-date, or if the specified
22254 screen line does not display text, this function returns nil. It is
22255 highly recommended to bind this function to some simple key, like F8,
22256 in order to avoid these problems.
22258 This function exists mainly for testing the correctness of the
22259 Emacs UBA implementation, in particular with the test suite. */)
22260 (Lisp_Object vpos)
22262 struct window *w = XWINDOW (selected_window);
22263 struct buffer *b = XBUFFER (w->contents);
22264 int nrow;
22265 struct glyph_row *row;
22267 if (NILP (vpos))
22269 int d1, d2, d3, d4, d5;
22271 pos_visible_p (w, PT, &d1, &d2, &d3, &d4, &d5, &nrow);
22273 else
22275 CHECK_NUMBER_COERCE_MARKER (vpos);
22276 nrow = XINT (vpos);
22279 /* We require up-to-date glyph matrix for this window. */
22280 if (w->window_end_valid
22281 && !windows_or_buffers_changed
22282 && b
22283 && !b->clip_changed
22284 && !b->prevent_redisplay_optimizations_p
22285 && !window_outdated (w)
22286 && nrow >= 0
22287 && nrow < w->current_matrix->nrows
22288 && (row = MATRIX_ROW (w->current_matrix, nrow))->enabled_p
22289 && MATRIX_ROW_DISPLAYS_TEXT_P (row))
22291 struct glyph *g, *e, *g1;
22292 int nglyphs, i;
22293 Lisp_Object levels;
22295 if (!row->reversed_p) /* Left-to-right glyph row. */
22297 g = g1 = row->glyphs[TEXT_AREA];
22298 e = g + row->used[TEXT_AREA];
22300 /* Skip over glyphs at the start of the row that was
22301 generated by redisplay for its own needs. */
22302 while (g < e
22303 && NILP (g->object)
22304 && g->charpos < 0)
22305 g++;
22306 g1 = g;
22308 /* Count the "interesting" glyphs in this row. */
22309 for (nglyphs = 0; g < e && !NILP (g->object); g++)
22310 nglyphs++;
22312 /* Create and fill the array. */
22313 levels = make_uninit_vector (nglyphs);
22314 for (i = 0; g1 < g; i++, g1++)
22315 ASET (levels, i, make_number (g1->resolved_level));
22317 else /* Right-to-left glyph row. */
22319 g = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
22320 e = row->glyphs[TEXT_AREA] - 1;
22321 while (g > e
22322 && NILP (g->object)
22323 && g->charpos < 0)
22324 g--;
22325 g1 = g;
22326 for (nglyphs = 0; g > e && !NILP (g->object); g--)
22327 nglyphs++;
22328 levels = make_uninit_vector (nglyphs);
22329 for (i = 0; g1 > g; i++, g1--)
22330 ASET (levels, i, make_number (g1->resolved_level));
22332 return levels;
22334 else
22335 return Qnil;
22340 /***********************************************************************
22341 Menu Bar
22342 ***********************************************************************/
22344 /* Redisplay the menu bar in the frame for window W.
22346 The menu bar of X frames that don't have X toolkit support is
22347 displayed in a special window W->frame->menu_bar_window.
22349 The menu bar of terminal frames is treated specially as far as
22350 glyph matrices are concerned. Menu bar lines are not part of
22351 windows, so the update is done directly on the frame matrix rows
22352 for the menu bar. */
22354 static void
22355 display_menu_bar (struct window *w)
22357 struct frame *f = XFRAME (WINDOW_FRAME (w));
22358 struct it it;
22359 Lisp_Object items;
22360 int i;
22362 /* Don't do all this for graphical frames. */
22363 #ifdef HAVE_NTGUI
22364 if (FRAME_W32_P (f))
22365 return;
22366 #endif
22367 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
22368 if (FRAME_X_P (f))
22369 return;
22370 #endif
22372 #ifdef HAVE_NS
22373 if (FRAME_NS_P (f))
22374 return;
22375 #endif /* HAVE_NS */
22377 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
22378 eassert (!FRAME_WINDOW_P (f));
22379 init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MENU_FACE_ID);
22380 it.first_visible_x = 0;
22381 it.last_visible_x = FRAME_PIXEL_WIDTH (f);
22382 #elif defined (HAVE_X_WINDOWS) /* X without toolkit. */
22383 if (FRAME_WINDOW_P (f))
22385 /* Menu bar lines are displayed in the desired matrix of the
22386 dummy window menu_bar_window. */
22387 struct window *menu_w;
22388 menu_w = XWINDOW (f->menu_bar_window);
22389 init_iterator (&it, menu_w, -1, -1, menu_w->desired_matrix->rows,
22390 MENU_FACE_ID);
22391 it.first_visible_x = 0;
22392 it.last_visible_x = FRAME_PIXEL_WIDTH (f);
22394 else
22395 #endif /* not USE_X_TOOLKIT and not USE_GTK */
22397 /* This is a TTY frame, i.e. character hpos/vpos are used as
22398 pixel x/y. */
22399 init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
22400 MENU_FACE_ID);
22401 it.first_visible_x = 0;
22402 it.last_visible_x = FRAME_COLS (f);
22405 /* FIXME: This should be controlled by a user option. See the
22406 comments in redisplay_tool_bar and display_mode_line about
22407 this. */
22408 it.paragraph_embedding = L2R;
22410 /* Clear all rows of the menu bar. */
22411 for (i = 0; i < FRAME_MENU_BAR_LINES (f); ++i)
22413 struct glyph_row *row = it.glyph_row + i;
22414 clear_glyph_row (row);
22415 row->enabled_p = true;
22416 row->full_width_p = true;
22417 row->reversed_p = false;
22420 /* Display all items of the menu bar. */
22421 items = FRAME_MENU_BAR_ITEMS (it.f);
22422 for (i = 0; i < ASIZE (items); i += 4)
22424 Lisp_Object string;
22426 /* Stop at nil string. */
22427 string = AREF (items, i + 1);
22428 if (NILP (string))
22429 break;
22431 /* Remember where item was displayed. */
22432 ASET (items, i + 3, make_number (it.hpos));
22434 /* Display the item, pad with one space. */
22435 if (it.current_x < it.last_visible_x)
22436 display_string (NULL, string, Qnil, 0, 0, &it,
22437 SCHARS (string) + 1, 0, 0, -1);
22440 /* Fill out the line with spaces. */
22441 if (it.current_x < it.last_visible_x)
22442 display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
22444 /* Compute the total height of the lines. */
22445 compute_line_metrics (&it);
22448 /* Deep copy of a glyph row, including the glyphs. */
22449 static void
22450 deep_copy_glyph_row (struct glyph_row *to, struct glyph_row *from)
22452 struct glyph *pointers[1 + LAST_AREA];
22453 int to_used = to->used[TEXT_AREA];
22455 /* Save glyph pointers of TO. */
22456 memcpy (pointers, to->glyphs, sizeof to->glyphs);
22458 /* Do a structure assignment. */
22459 *to = *from;
22461 /* Restore original glyph pointers of TO. */
22462 memcpy (to->glyphs, pointers, sizeof to->glyphs);
22464 /* Copy the glyphs. */
22465 memcpy (to->glyphs[TEXT_AREA], from->glyphs[TEXT_AREA],
22466 min (from->used[TEXT_AREA], to_used) * sizeof (struct glyph));
22468 /* If we filled only part of the TO row, fill the rest with
22469 space_glyph (which will display as empty space). */
22470 if (to_used > from->used[TEXT_AREA])
22471 fill_up_frame_row_with_spaces (to, to_used);
22474 /* Display one menu item on a TTY, by overwriting the glyphs in the
22475 frame F's desired glyph matrix with glyphs produced from the menu
22476 item text. Called from term.c to display TTY drop-down menus one
22477 item at a time.
22479 ITEM_TEXT is the menu item text as a C string.
22481 FACE_ID is the face ID to be used for this menu item. FACE_ID
22482 could specify one of 3 faces: a face for an enabled item, a face
22483 for a disabled item, or a face for a selected item.
22485 X and Y are coordinates of the first glyph in the frame's desired
22486 matrix to be overwritten by the menu item. Since this is a TTY, Y
22487 is the zero-based number of the glyph row and X is the zero-based
22488 glyph number in the row, starting from left, where to start
22489 displaying the item.
22491 SUBMENU means this menu item drops down a submenu, which
22492 should be indicated by displaying a proper visual cue after the
22493 item text. */
22495 void
22496 display_tty_menu_item (const char *item_text, int width, int face_id,
22497 int x, int y, bool submenu)
22499 struct it it;
22500 struct frame *f = SELECTED_FRAME ();
22501 struct window *w = XWINDOW (f->selected_window);
22502 struct glyph_row *row;
22503 size_t item_len = strlen (item_text);
22505 eassert (FRAME_TERMCAP_P (f));
22507 /* Don't write beyond the matrix's last row. This can happen for
22508 TTY screens that are not high enough to show the entire menu.
22509 (This is actually a bit of defensive programming, as
22510 tty_menu_display already limits the number of menu items to one
22511 less than the number of screen lines.) */
22512 if (y >= f->desired_matrix->nrows)
22513 return;
22515 init_iterator (&it, w, -1, -1, f->desired_matrix->rows + y, MENU_FACE_ID);
22516 it.first_visible_x = 0;
22517 it.last_visible_x = FRAME_COLS (f) - 1;
22518 row = it.glyph_row;
22519 /* Start with the row contents from the current matrix. */
22520 deep_copy_glyph_row (row, f->current_matrix->rows + y);
22521 bool saved_width = row->full_width_p;
22522 row->full_width_p = true;
22523 bool saved_reversed = row->reversed_p;
22524 row->reversed_p = false;
22525 row->enabled_p = true;
22527 /* Arrange for the menu item glyphs to start at (X,Y) and have the
22528 desired face. */
22529 eassert (x < f->desired_matrix->matrix_w);
22530 it.current_x = it.hpos = x;
22531 it.current_y = it.vpos = y;
22532 int saved_used = row->used[TEXT_AREA];
22533 bool saved_truncated = row->truncated_on_right_p;
22534 row->used[TEXT_AREA] = x;
22535 it.face_id = face_id;
22536 it.line_wrap = TRUNCATE;
22538 /* FIXME: This should be controlled by a user option. See the
22539 comments in redisplay_tool_bar and display_mode_line about this.
22540 Also, if paragraph_embedding could ever be R2L, changes will be
22541 needed to avoid shifting to the right the row characters in
22542 term.c:append_glyph. */
22543 it.paragraph_embedding = L2R;
22545 /* Pad with a space on the left. */
22546 display_string (" ", Qnil, Qnil, 0, 0, &it, 1, 0, FRAME_COLS (f) - 1, -1);
22547 width--;
22548 /* Display the menu item, pad with spaces to WIDTH. */
22549 if (submenu)
22551 display_string (item_text, Qnil, Qnil, 0, 0, &it,
22552 item_len, 0, FRAME_COLS (f) - 1, -1);
22553 width -= item_len;
22554 /* Indicate with " >" that there's a submenu. */
22555 display_string (" >", Qnil, Qnil, 0, 0, &it, width, 0,
22556 FRAME_COLS (f) - 1, -1);
22558 else
22559 display_string (item_text, Qnil, Qnil, 0, 0, &it,
22560 width, 0, FRAME_COLS (f) - 1, -1);
22562 row->used[TEXT_AREA] = max (saved_used, row->used[TEXT_AREA]);
22563 row->truncated_on_right_p = saved_truncated;
22564 row->hash = row_hash (row);
22565 row->full_width_p = saved_width;
22566 row->reversed_p = saved_reversed;
22569 /***********************************************************************
22570 Mode Line
22571 ***********************************************************************/
22573 /* Redisplay mode lines in the window tree whose root is WINDOW.
22574 If FORCE, redisplay mode lines unconditionally.
22575 Otherwise, redisplay only mode lines that are garbaged. Value is
22576 the number of windows whose mode lines were redisplayed. */
22578 static int
22579 redisplay_mode_lines (Lisp_Object window, bool force)
22581 int nwindows = 0;
22583 while (!NILP (window))
22585 struct window *w = XWINDOW (window);
22587 if (WINDOWP (w->contents))
22588 nwindows += redisplay_mode_lines (w->contents, force);
22589 else if (force
22590 || FRAME_GARBAGED_P (XFRAME (w->frame))
22591 || !MATRIX_MODE_LINE_ROW (w->current_matrix)->enabled_p)
22593 struct text_pos lpoint;
22594 struct buffer *old = current_buffer;
22596 /* Set the window's buffer for the mode line display. */
22597 SET_TEXT_POS (lpoint, PT, PT_BYTE);
22598 set_buffer_internal_1 (XBUFFER (w->contents));
22600 /* Point refers normally to the selected window. For any
22601 other window, set up appropriate value. */
22602 if (!EQ (window, selected_window))
22604 struct text_pos pt;
22606 CLIP_TEXT_POS_FROM_MARKER (pt, w->pointm);
22607 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
22610 /* Display mode lines. */
22611 clear_glyph_matrix (w->desired_matrix);
22612 if (display_mode_lines (w))
22613 ++nwindows;
22615 /* Restore old settings. */
22616 set_buffer_internal_1 (old);
22617 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
22620 window = w->next;
22623 return nwindows;
22627 /* Display the mode and/or header line of window W. Value is the
22628 sum number of mode lines and header lines displayed. */
22630 static int
22631 display_mode_lines (struct window *w)
22633 Lisp_Object old_selected_window = selected_window;
22634 Lisp_Object old_selected_frame = selected_frame;
22635 Lisp_Object new_frame = w->frame;
22636 Lisp_Object old_frame_selected_window = XFRAME (new_frame)->selected_window;
22637 int n = 0;
22639 selected_frame = new_frame;
22640 /* FIXME: If we were to allow the mode-line's computation changing the buffer
22641 or window's point, then we'd need select_window_1 here as well. */
22642 XSETWINDOW (selected_window, w);
22643 XFRAME (new_frame)->selected_window = selected_window;
22645 /* These will be set while the mode line specs are processed. */
22646 line_number_displayed = false;
22647 w->column_number_displayed = -1;
22649 if (WINDOW_WANTS_MODELINE_P (w))
22651 struct window *sel_w = XWINDOW (old_selected_window);
22653 /* Select mode line face based on the real selected window. */
22654 display_mode_line (w, CURRENT_MODE_LINE_FACE_ID_3 (sel_w, sel_w, w),
22655 BVAR (current_buffer, mode_line_format));
22656 ++n;
22659 if (WINDOW_WANTS_HEADER_LINE_P (w))
22661 display_mode_line (w, HEADER_LINE_FACE_ID,
22662 BVAR (current_buffer, header_line_format));
22663 ++n;
22666 XFRAME (new_frame)->selected_window = old_frame_selected_window;
22667 selected_frame = old_selected_frame;
22668 selected_window = old_selected_window;
22669 if (n > 0)
22670 w->must_be_updated_p = true;
22671 return n;
22675 /* Display mode or header line of window W. FACE_ID specifies which
22676 line to display; it is either MODE_LINE_FACE_ID or
22677 HEADER_LINE_FACE_ID. FORMAT is the mode/header line format to
22678 display. Value is the pixel height of the mode/header line
22679 displayed. */
22681 static int
22682 display_mode_line (struct window *w, enum face_id face_id, Lisp_Object format)
22684 struct it it;
22685 struct face *face;
22686 ptrdiff_t count = SPECPDL_INDEX ();
22688 init_iterator (&it, w, -1, -1, NULL, face_id);
22689 /* Don't extend on a previously drawn mode-line.
22690 This may happen if called from pos_visible_p. */
22691 it.glyph_row->enabled_p = false;
22692 prepare_desired_row (w, it.glyph_row, true);
22694 it.glyph_row->mode_line_p = true;
22696 /* FIXME: This should be controlled by a user option. But
22697 supporting such an option is not trivial, since the mode line is
22698 made up of many separate strings. */
22699 it.paragraph_embedding = L2R;
22701 record_unwind_protect (unwind_format_mode_line,
22702 format_mode_line_unwind_data (NULL, NULL,
22703 Qnil, false));
22705 mode_line_target = MODE_LINE_DISPLAY;
22707 /* Temporarily make frame's keyboard the current kboard so that
22708 kboard-local variables in the mode_line_format will get the right
22709 values. */
22710 push_kboard (FRAME_KBOARD (it.f));
22711 record_unwind_save_match_data ();
22712 display_mode_element (&it, 0, 0, 0, format, Qnil, false);
22713 pop_kboard ();
22715 unbind_to (count, Qnil);
22717 /* Fill up with spaces. */
22718 display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);
22720 compute_line_metrics (&it);
22721 it.glyph_row->full_width_p = true;
22722 it.glyph_row->continued_p = false;
22723 it.glyph_row->truncated_on_left_p = false;
22724 it.glyph_row->truncated_on_right_p = false;
22726 /* Make a 3D mode-line have a shadow at its right end. */
22727 face = FACE_FROM_ID (it.f, face_id);
22728 extend_face_to_end_of_line (&it);
22729 if (face->box != FACE_NO_BOX)
22731 struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
22732 + it.glyph_row->used[TEXT_AREA] - 1);
22733 last->right_box_line_p = true;
22736 return it.glyph_row->height;
22739 /* Move element ELT in LIST to the front of LIST.
22740 Return the updated list. */
22742 static Lisp_Object
22743 move_elt_to_front (Lisp_Object elt, Lisp_Object list)
22745 register Lisp_Object tail, prev;
22746 register Lisp_Object tem;
22748 tail = list;
22749 prev = Qnil;
22750 while (CONSP (tail))
22752 tem = XCAR (tail);
22754 if (EQ (elt, tem))
22756 /* Splice out the link TAIL. */
22757 if (NILP (prev))
22758 list = XCDR (tail);
22759 else
22760 Fsetcdr (prev, XCDR (tail));
22762 /* Now make it the first. */
22763 Fsetcdr (tail, list);
22764 return tail;
22766 else
22767 prev = tail;
22768 tail = XCDR (tail);
22769 maybe_quit ();
22772 /* Not found--return unchanged LIST. */
22773 return list;
22776 /* Contribute ELT to the mode line for window IT->w. How it
22777 translates into text depends on its data type.
22779 IT describes the display environment in which we display, as usual.
22781 DEPTH is the depth in recursion. It is used to prevent
22782 infinite recursion here.
22784 FIELD_WIDTH is the number of characters the display of ELT should
22785 occupy in the mode line, and PRECISION is the maximum number of
22786 characters to display from ELT's representation. See
22787 display_string for details.
22789 Returns the hpos of the end of the text generated by ELT.
22791 PROPS is a property list to add to any string we encounter.
22793 If RISKY, remove (disregard) any properties in any string
22794 we encounter, and ignore :eval and :propertize.
22796 The global variable `mode_line_target' determines whether the
22797 output is passed to `store_mode_line_noprop',
22798 `store_mode_line_string', or `display_string'. */
22800 static int
22801 display_mode_element (struct it *it, int depth, int field_width, int precision,
22802 Lisp_Object elt, Lisp_Object props, bool risky)
22804 int n = 0, field, prec;
22805 bool literal = false;
22807 tail_recurse:
22808 if (depth > 100)
22809 elt = build_string ("*too-deep*");
22811 depth++;
22813 switch (XTYPE (elt))
22815 case Lisp_String:
22817 /* A string: output it and check for %-constructs within it. */
22818 unsigned char c;
22819 ptrdiff_t offset = 0;
22821 if (SCHARS (elt) > 0
22822 && (!NILP (props) || risky))
22824 Lisp_Object oprops, aelt;
22825 oprops = Ftext_properties_at (make_number (0), elt);
22827 /* If the starting string's properties are not what
22828 we want, translate the string. Also, if the string
22829 is risky, do that anyway. */
22831 if (NILP (Fequal (props, oprops)) || risky)
22833 /* If the starting string has properties,
22834 merge the specified ones onto the existing ones. */
22835 if (! NILP (oprops) && !risky)
22837 Lisp_Object tem;
22839 oprops = Fcopy_sequence (oprops);
22840 tem = props;
22841 while (CONSP (tem))
22843 oprops = Fplist_put (oprops, XCAR (tem),
22844 XCAR (XCDR (tem)));
22845 tem = XCDR (XCDR (tem));
22847 props = oprops;
22850 aelt = Fassoc (elt, mode_line_proptrans_alist);
22851 if (! NILP (aelt) && !NILP (Fequal (props, XCDR (aelt))))
22853 /* AELT is what we want. Move it to the front
22854 without consing. */
22855 elt = XCAR (aelt);
22856 mode_line_proptrans_alist
22857 = move_elt_to_front (aelt, mode_line_proptrans_alist);
22859 else
22861 Lisp_Object tem;
22863 /* If AELT has the wrong props, it is useless.
22864 so get rid of it. */
22865 if (! NILP (aelt))
22866 mode_line_proptrans_alist
22867 = Fdelq (aelt, mode_line_proptrans_alist);
22869 elt = Fcopy_sequence (elt);
22870 Fset_text_properties (make_number (0), Flength (elt),
22871 props, elt);
22872 /* Add this item to mode_line_proptrans_alist. */
22873 mode_line_proptrans_alist
22874 = Fcons (Fcons (elt, props),
22875 mode_line_proptrans_alist);
22876 /* Truncate mode_line_proptrans_alist
22877 to at most 50 elements. */
22878 tem = Fnthcdr (make_number (50),
22879 mode_line_proptrans_alist);
22880 if (! NILP (tem))
22881 XSETCDR (tem, Qnil);
22886 offset = 0;
22888 if (literal)
22890 prec = precision - n;
22891 switch (mode_line_target)
22893 case MODE_LINE_NOPROP:
22894 case MODE_LINE_TITLE:
22895 n += store_mode_line_noprop (SSDATA (elt), -1, prec);
22896 break;
22897 case MODE_LINE_STRING:
22898 n += store_mode_line_string (NULL, elt, true, 0, prec, Qnil);
22899 break;
22900 case MODE_LINE_DISPLAY:
22901 n += display_string (NULL, elt, Qnil, 0, 0, it,
22902 0, prec, 0, STRING_MULTIBYTE (elt));
22903 break;
22906 break;
22909 /* Handle the non-literal case. */
22911 while ((precision <= 0 || n < precision)
22912 && SREF (elt, offset) != 0
22913 && (mode_line_target != MODE_LINE_DISPLAY
22914 || it->current_x < it->last_visible_x))
22916 ptrdiff_t last_offset = offset;
22918 /* Advance to end of string or next format specifier. */
22919 while ((c = SREF (elt, offset++)) != '\0' && c != '%')
22922 if (offset - 1 != last_offset)
22924 ptrdiff_t nchars, nbytes;
22926 /* Output to end of string or up to '%'. Field width
22927 is length of string. Don't output more than
22928 PRECISION allows us. */
22929 offset--;
22931 prec = c_string_width (SDATA (elt) + last_offset,
22932 offset - last_offset, precision - n,
22933 &nchars, &nbytes);
22935 switch (mode_line_target)
22937 case MODE_LINE_NOPROP:
22938 case MODE_LINE_TITLE:
22939 n += store_mode_line_noprop (SSDATA (elt) + last_offset, 0, prec);
22940 break;
22941 case MODE_LINE_STRING:
22943 ptrdiff_t bytepos = last_offset;
22944 ptrdiff_t charpos = string_byte_to_char (elt, bytepos);
22945 ptrdiff_t endpos = (precision <= 0
22946 ? string_byte_to_char (elt, offset)
22947 : charpos + nchars);
22948 Lisp_Object mode_string
22949 = Fsubstring (elt, make_number (charpos),
22950 make_number (endpos));
22951 n += store_mode_line_string (NULL, mode_string, false,
22952 0, 0, Qnil);
22954 break;
22955 case MODE_LINE_DISPLAY:
22957 ptrdiff_t bytepos = last_offset;
22958 ptrdiff_t charpos = string_byte_to_char (elt, bytepos);
22960 if (precision <= 0)
22961 nchars = string_byte_to_char (elt, offset) - charpos;
22962 n += display_string (NULL, elt, Qnil, 0, charpos,
22963 it, 0, nchars, 0,
22964 STRING_MULTIBYTE (elt));
22966 break;
22969 else /* c == '%' */
22971 ptrdiff_t percent_position = offset;
22973 /* Get the specified minimum width. Zero means
22974 don't pad. */
22975 field = 0;
22976 while ((c = SREF (elt, offset++)) >= '0' && c <= '9')
22977 field = field * 10 + c - '0';
22979 /* Don't pad beyond the total padding allowed. */
22980 if (field_width - n > 0 && field > field_width - n)
22981 field = field_width - n;
22983 /* Note that either PRECISION <= 0 or N < PRECISION. */
22984 prec = precision - n;
22986 if (c == 'M')
22987 n += display_mode_element (it, depth, field, prec,
22988 Vglobal_mode_string, props,
22989 risky);
22990 else if (c != 0)
22992 bool multibyte;
22993 ptrdiff_t bytepos, charpos;
22994 const char *spec;
22995 Lisp_Object string;
22997 bytepos = percent_position;
22998 charpos = (STRING_MULTIBYTE (elt)
22999 ? string_byte_to_char (elt, bytepos)
23000 : bytepos);
23001 spec = decode_mode_spec (it->w, c, field, &string);
23002 multibyte = STRINGP (string) && STRING_MULTIBYTE (string);
23004 switch (mode_line_target)
23006 case MODE_LINE_NOPROP:
23007 case MODE_LINE_TITLE:
23008 n += store_mode_line_noprop (spec, field, prec);
23009 break;
23010 case MODE_LINE_STRING:
23012 Lisp_Object tem = build_string (spec);
23013 props = Ftext_properties_at (make_number (charpos), elt);
23014 /* Should only keep face property in props */
23015 n += store_mode_line_string (NULL, tem, false,
23016 field, prec, props);
23018 break;
23019 case MODE_LINE_DISPLAY:
23021 int nglyphs_before, nwritten;
23023 nglyphs_before = it->glyph_row->used[TEXT_AREA];
23024 nwritten = display_string (spec, string, elt,
23025 charpos, 0, it,
23026 field, prec, 0,
23027 multibyte);
23029 /* Assign to the glyphs written above the
23030 string where the `%x' came from, position
23031 of the `%'. */
23032 if (nwritten > 0)
23034 struct glyph *glyph
23035 = (it->glyph_row->glyphs[TEXT_AREA]
23036 + nglyphs_before);
23037 int i;
23039 for (i = 0; i < nwritten; ++i)
23041 glyph[i].object = elt;
23042 glyph[i].charpos = charpos;
23045 n += nwritten;
23048 break;
23051 else /* c == 0 */
23052 break;
23056 break;
23058 case Lisp_Symbol:
23059 /* A symbol: process the value of the symbol recursively
23060 as if it appeared here directly. Avoid error if symbol void.
23061 Special case: if value of symbol is a string, output the string
23062 literally. */
23064 register Lisp_Object tem;
23066 /* If the variable is not marked as risky to set
23067 then its contents are risky to use. */
23068 if (NILP (Fget (elt, Qrisky_local_variable)))
23069 risky = true;
23071 tem = Fboundp (elt);
23072 if (!NILP (tem))
23074 tem = Fsymbol_value (elt);
23075 /* If value is a string, output that string literally:
23076 don't check for % within it. */
23077 if (STRINGP (tem))
23078 literal = true;
23080 if (!EQ (tem, elt))
23082 /* Give up right away for nil or t. */
23083 elt = tem;
23084 goto tail_recurse;
23088 break;
23090 case Lisp_Cons:
23092 register Lisp_Object car, tem;
23094 /* A cons cell: five distinct cases.
23095 If first element is :eval or :propertize, do something special.
23096 If first element is a string or a cons, process all the elements
23097 and effectively concatenate them.
23098 If first element is a negative number, truncate displaying cdr to
23099 at most that many characters. If positive, pad (with spaces)
23100 to at least that many characters.
23101 If first element is a symbol, process the cadr or caddr recursively
23102 according to whether the symbol's value is non-nil or nil. */
23103 car = XCAR (elt);
23104 if (EQ (car, QCeval))
23106 /* An element of the form (:eval FORM) means evaluate FORM
23107 and use the result as mode line elements. */
23109 if (risky)
23110 break;
23112 if (CONSP (XCDR (elt)))
23114 Lisp_Object spec;
23115 spec = safe__eval (true, XCAR (XCDR (elt)));
23116 n += display_mode_element (it, depth, field_width - n,
23117 precision - n, spec, props,
23118 risky);
23121 else if (EQ (car, QCpropertize))
23123 /* An element of the form (:propertize ELT PROPS...)
23124 means display ELT but applying properties PROPS. */
23126 if (risky)
23127 break;
23129 if (CONSP (XCDR (elt)))
23130 n += display_mode_element (it, depth, field_width - n,
23131 precision - n, XCAR (XCDR (elt)),
23132 XCDR (XCDR (elt)), risky);
23134 else if (SYMBOLP (car))
23136 tem = Fboundp (car);
23137 elt = XCDR (elt);
23138 if (!CONSP (elt))
23139 goto invalid;
23140 /* elt is now the cdr, and we know it is a cons cell.
23141 Use its car if CAR has a non-nil value. */
23142 if (!NILP (tem))
23144 tem = Fsymbol_value (car);
23145 if (!NILP (tem))
23147 elt = XCAR (elt);
23148 goto tail_recurse;
23151 /* Symbol's value is nil (or symbol is unbound)
23152 Get the cddr of the original list
23153 and if possible find the caddr and use that. */
23154 elt = XCDR (elt);
23155 if (NILP (elt))
23156 break;
23157 else if (!CONSP (elt))
23158 goto invalid;
23159 elt = XCAR (elt);
23160 goto tail_recurse;
23162 else if (INTEGERP (car))
23164 register int lim = XINT (car);
23165 elt = XCDR (elt);
23166 if (lim < 0)
23168 /* Negative int means reduce maximum width. */
23169 if (precision <= 0)
23170 precision = -lim;
23171 else
23172 precision = min (precision, -lim);
23174 else if (lim > 0)
23176 /* Padding specified. Don't let it be more than
23177 current maximum. */
23178 if (precision > 0)
23179 lim = min (precision, lim);
23181 /* If that's more padding than already wanted, queue it.
23182 But don't reduce padding already specified even if
23183 that is beyond the current truncation point. */
23184 field_width = max (lim, field_width);
23186 goto tail_recurse;
23188 else if (STRINGP (car) || CONSP (car))
23189 FOR_EACH_TAIL_SAFE (elt)
23191 if (0 < precision && precision <= n)
23192 break;
23193 n += display_mode_element (it, depth,
23194 /* Pad after only the last
23195 list element. */
23196 (! CONSP (XCDR (elt))
23197 ? field_width - n
23198 : 0),
23199 precision - n, XCAR (elt),
23200 props, risky);
23203 break;
23205 default:
23206 invalid:
23207 elt = build_string ("*invalid*");
23208 goto tail_recurse;
23211 /* Pad to FIELD_WIDTH. */
23212 if (field_width > 0 && n < field_width)
23214 switch (mode_line_target)
23216 case MODE_LINE_NOPROP:
23217 case MODE_LINE_TITLE:
23218 n += store_mode_line_noprop ("", field_width - n, 0);
23219 break;
23220 case MODE_LINE_STRING:
23221 n += store_mode_line_string ("", Qnil, false, field_width - n, 0,
23222 Qnil);
23223 break;
23224 case MODE_LINE_DISPLAY:
23225 n += display_string ("", Qnil, Qnil, 0, 0, it, field_width - n,
23226 0, 0, 0);
23227 break;
23231 return n;
23234 /* Store a mode-line string element in mode_line_string_list.
23236 If STRING is non-null, display that C string. Otherwise, the Lisp
23237 string LISP_STRING is displayed.
23239 FIELD_WIDTH is the minimum number of output glyphs to produce.
23240 If STRING has fewer characters than FIELD_WIDTH, pad to the right
23241 with spaces. FIELD_WIDTH <= 0 means don't pad.
23243 PRECISION is the maximum number of characters to output from
23244 STRING. PRECISION <= 0 means don't truncate the string.
23246 If COPY_STRING, make a copy of LISP_STRING before adding
23247 properties to the string.
23249 PROPS are the properties to add to the string.
23250 The mode_line_string_face face property is always added to the string.
23253 static int
23254 store_mode_line_string (const char *string, Lisp_Object lisp_string,
23255 bool copy_string,
23256 int field_width, int precision, Lisp_Object props)
23258 ptrdiff_t len;
23259 int n = 0;
23261 if (string != NULL)
23263 len = strlen (string);
23264 if (precision > 0 && len > precision)
23265 len = precision;
23266 lisp_string = make_string (string, len);
23267 if (NILP (props))
23268 props = mode_line_string_face_prop;
23269 else if (!NILP (mode_line_string_face))
23271 Lisp_Object face = Fplist_get (props, Qface);
23272 props = Fcopy_sequence (props);
23273 if (NILP (face))
23274 face = mode_line_string_face;
23275 else
23276 face = list2 (face, mode_line_string_face);
23277 props = Fplist_put (props, Qface, face);
23279 Fadd_text_properties (make_number (0), make_number (len),
23280 props, lisp_string);
23282 else
23284 len = XFASTINT (Flength (lisp_string));
23285 if (precision > 0 && len > precision)
23287 len = precision;
23288 lisp_string = Fsubstring (lisp_string, make_number (0), make_number (len));
23289 precision = -1;
23291 if (!NILP (mode_line_string_face))
23293 Lisp_Object face;
23294 if (NILP (props))
23295 props = Ftext_properties_at (make_number (0), lisp_string);
23296 face = Fplist_get (props, Qface);
23297 if (NILP (face))
23298 face = mode_line_string_face;
23299 else
23300 face = list2 (face, mode_line_string_face);
23301 props = list2 (Qface, face);
23302 if (copy_string)
23303 lisp_string = Fcopy_sequence (lisp_string);
23305 if (!NILP (props))
23306 Fadd_text_properties (make_number (0), make_number (len),
23307 props, lisp_string);
23310 if (len > 0)
23312 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
23313 n += len;
23316 if (field_width > len)
23318 field_width -= len;
23319 lisp_string = Fmake_string (make_number (field_width), make_number (' '));
23320 if (!NILP (props))
23321 Fadd_text_properties (make_number (0), make_number (field_width),
23322 props, lisp_string);
23323 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
23324 n += field_width;
23327 return n;
23331 DEFUN ("format-mode-line", Fformat_mode_line, Sformat_mode_line,
23332 1, 4, 0,
23333 doc: /* Format a string out of a mode line format specification.
23334 First arg FORMAT specifies the mode line format (see `mode-line-format'
23335 for details) to use.
23337 By default, the format is evaluated for the currently selected window.
23339 Optional second arg FACE specifies the face property to put on all
23340 characters for which no face is specified. The value nil means the
23341 default face. The value t means whatever face the window's mode line
23342 currently uses (either `mode-line' or `mode-line-inactive',
23343 depending on whether the window is the selected window or not).
23344 An integer value means the value string has no text
23345 properties.
23347 Optional third and fourth args WINDOW and BUFFER specify the window
23348 and buffer to use as the context for the formatting (defaults
23349 are the selected window and the WINDOW's buffer). */)
23350 (Lisp_Object format, Lisp_Object face,
23351 Lisp_Object window, Lisp_Object buffer)
23353 struct it it;
23354 int len;
23355 struct window *w;
23356 struct buffer *old_buffer = NULL;
23357 int face_id;
23358 bool no_props = INTEGERP (face);
23359 ptrdiff_t count = SPECPDL_INDEX ();
23360 Lisp_Object str;
23361 int string_start = 0;
23363 w = decode_any_window (window);
23364 XSETWINDOW (window, w);
23366 if (NILP (buffer))
23367 buffer = w->contents;
23368 CHECK_BUFFER (buffer);
23370 /* Make formatting the modeline a non-op when noninteractive, otherwise
23371 there will be problems later caused by a partially initialized frame. */
23372 if (NILP (format) || noninteractive)
23373 return empty_unibyte_string;
23375 if (no_props)
23376 face = Qnil;
23378 face_id = (NILP (face) || EQ (face, Qdefault)) ? DEFAULT_FACE_ID
23379 : EQ (face, Qt) ? (EQ (window, selected_window)
23380 ? MODE_LINE_FACE_ID : MODE_LINE_INACTIVE_FACE_ID)
23381 : EQ (face, Qmode_line) ? MODE_LINE_FACE_ID
23382 : EQ (face, Qmode_line_inactive) ? MODE_LINE_INACTIVE_FACE_ID
23383 : EQ (face, Qheader_line) ? HEADER_LINE_FACE_ID
23384 : EQ (face, Qtool_bar) ? TOOL_BAR_FACE_ID
23385 : DEFAULT_FACE_ID;
23387 old_buffer = current_buffer;
23389 /* Save things including mode_line_proptrans_alist,
23390 and set that to nil so that we don't alter the outer value. */
23391 record_unwind_protect (unwind_format_mode_line,
23392 format_mode_line_unwind_data
23393 (XFRAME (WINDOW_FRAME (w)),
23394 old_buffer, selected_window, true));
23395 mode_line_proptrans_alist = Qnil;
23397 Fselect_window (window, Qt);
23398 set_buffer_internal_1 (XBUFFER (buffer));
23400 init_iterator (&it, w, -1, -1, NULL, face_id);
23402 if (no_props)
23404 mode_line_target = MODE_LINE_NOPROP;
23405 mode_line_string_face_prop = Qnil;
23406 mode_line_string_list = Qnil;
23407 string_start = MODE_LINE_NOPROP_LEN (0);
23409 else
23411 mode_line_target = MODE_LINE_STRING;
23412 mode_line_string_list = Qnil;
23413 mode_line_string_face = face;
23414 mode_line_string_face_prop
23415 = NILP (face) ? Qnil : list2 (Qface, face);
23418 push_kboard (FRAME_KBOARD (it.f));
23419 display_mode_element (&it, 0, 0, 0, format, Qnil, false);
23420 pop_kboard ();
23422 if (no_props)
23424 len = MODE_LINE_NOPROP_LEN (string_start);
23425 str = make_string (mode_line_noprop_buf + string_start, len);
23427 else
23429 mode_line_string_list = Fnreverse (mode_line_string_list);
23430 str = Fmapconcat (Qidentity, mode_line_string_list,
23431 empty_unibyte_string);
23434 unbind_to (count, Qnil);
23435 return str;
23438 /* Write a null-terminated, right justified decimal representation of
23439 the positive integer D to BUF using a minimal field width WIDTH. */
23441 static void
23442 pint2str (register char *buf, register int width, register ptrdiff_t d)
23444 register char *p = buf;
23446 if (d <= 0)
23447 *p++ = '0';
23448 else
23450 while (d > 0)
23452 *p++ = d % 10 + '0';
23453 d /= 10;
23457 for (width -= (int) (p - buf); width > 0; --width)
23458 *p++ = ' ';
23459 *p-- = '\0';
23460 while (p > buf)
23462 d = *buf;
23463 *buf++ = *p;
23464 *p-- = d;
23468 /* Write a null-terminated, right justified decimal and "human
23469 readable" representation of the nonnegative integer D to BUF using
23470 a minimal field width WIDTH. D should be smaller than 999.5e24. */
23472 static const char power_letter[] =
23474 0, /* no letter */
23475 'k', /* kilo */
23476 'M', /* mega */
23477 'G', /* giga */
23478 'T', /* tera */
23479 'P', /* peta */
23480 'E', /* exa */
23481 'Z', /* zetta */
23482 'Y' /* yotta */
23485 static void
23486 pint2hrstr (char *buf, int width, ptrdiff_t d)
23488 /* We aim to represent the nonnegative integer D as
23489 QUOTIENT.TENTHS * 10 ^ (3 * EXPONENT). */
23490 ptrdiff_t quotient = d;
23491 int remainder = 0;
23492 /* -1 means: do not use TENTHS. */
23493 int tenths = -1;
23494 int exponent = 0;
23496 /* Length of QUOTIENT.TENTHS as a string. */
23497 int length;
23499 char * psuffix;
23500 char * p;
23502 if (quotient >= 1000)
23504 /* Scale to the appropriate EXPONENT. */
23507 remainder = quotient % 1000;
23508 quotient /= 1000;
23509 exponent++;
23511 while (quotient >= 1000);
23513 /* Round to nearest and decide whether to use TENTHS or not. */
23514 if (quotient <= 9)
23516 tenths = remainder / 100;
23517 if (remainder % 100 >= 50)
23519 if (tenths < 9)
23520 tenths++;
23521 else
23523 quotient++;
23524 if (quotient == 10)
23525 tenths = -1;
23526 else
23527 tenths = 0;
23531 else
23532 if (remainder >= 500)
23534 if (quotient < 999)
23535 quotient++;
23536 else
23538 quotient = 1;
23539 exponent++;
23540 tenths = 0;
23545 /* Calculate the LENGTH of QUOTIENT.TENTHS as a string. */
23546 if (tenths == -1 && quotient <= 99)
23547 if (quotient <= 9)
23548 length = 1;
23549 else
23550 length = 2;
23551 else
23552 length = 3;
23553 p = psuffix = buf + max (width, length);
23555 /* Print EXPONENT. */
23556 *psuffix++ = power_letter[exponent];
23557 *psuffix = '\0';
23559 /* Print TENTHS. */
23560 if (tenths >= 0)
23562 *--p = '0' + tenths;
23563 *--p = '.';
23566 /* Print QUOTIENT. */
23569 int digit = quotient % 10;
23570 *--p = '0' + digit;
23572 while ((quotient /= 10) != 0);
23574 /* Print leading spaces. */
23575 while (buf < p)
23576 *--p = ' ';
23579 /* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
23580 If EOL_FLAG, set also a mnemonic character for end-of-line
23581 type of CODING_SYSTEM. Return updated pointer into BUF. */
23583 static unsigned char invalid_eol_type[] = "(*invalid*)";
23585 static char *
23586 decode_mode_spec_coding (Lisp_Object coding_system, char *buf, bool eol_flag)
23588 Lisp_Object val;
23589 bool multibyte = !NILP (BVAR (current_buffer, enable_multibyte_characters));
23590 const unsigned char *eol_str;
23591 int eol_str_len;
23592 /* The EOL conversion we are using. */
23593 Lisp_Object eoltype;
23595 val = CODING_SYSTEM_SPEC (coding_system);
23596 eoltype = Qnil;
23598 if (!VECTORP (val)) /* Not yet decided. */
23600 *buf++ = multibyte ? '-' : ' ';
23601 if (eol_flag)
23602 eoltype = eol_mnemonic_undecided;
23603 /* Don't mention EOL conversion if it isn't decided. */
23605 else
23607 Lisp_Object attrs;
23608 Lisp_Object eolvalue;
23610 attrs = AREF (val, 0);
23611 eolvalue = AREF (val, 2);
23613 *buf++ = multibyte
23614 ? XFASTINT (CODING_ATTR_MNEMONIC (attrs))
23615 : ' ';
23617 if (eol_flag)
23619 /* The EOL conversion that is normal on this system. */
23621 if (NILP (eolvalue)) /* Not yet decided. */
23622 eoltype = eol_mnemonic_undecided;
23623 else if (VECTORP (eolvalue)) /* Not yet decided. */
23624 eoltype = eol_mnemonic_undecided;
23625 else /* eolvalue is Qunix, Qdos, or Qmac. */
23626 eoltype = (EQ (eolvalue, Qunix)
23627 ? eol_mnemonic_unix
23628 : EQ (eolvalue, Qdos)
23629 ? eol_mnemonic_dos : eol_mnemonic_mac);
23633 if (eol_flag)
23635 /* Mention the EOL conversion if it is not the usual one. */
23636 if (STRINGP (eoltype))
23638 eol_str = SDATA (eoltype);
23639 eol_str_len = SBYTES (eoltype);
23641 else if (CHARACTERP (eoltype))
23643 int c = XFASTINT (eoltype);
23644 return buf + CHAR_STRING (c, (unsigned char *) buf);
23646 else
23648 eol_str = invalid_eol_type;
23649 eol_str_len = sizeof (invalid_eol_type) - 1;
23651 memcpy (buf, eol_str, eol_str_len);
23652 buf += eol_str_len;
23655 return buf;
23658 /* Return the approximate percentage N is of D (rounding upward), or 99,
23659 whichever is less. Assume 0 < D and 0 <= N <= D * INT_MAX / 100. */
23661 static int
23662 percent99 (ptrdiff_t n, ptrdiff_t d)
23664 int percent = (d - 1 + 100.0 * n) / d;
23665 return min (percent, 99);
23668 /* Return a string for the output of a mode line %-spec for window W,
23669 generated by character C. FIELD_WIDTH > 0 means pad the string
23670 returned with spaces to that value. Return a Lisp string in
23671 *STRING if the resulting string is taken from that Lisp string.
23673 Note we operate on the current buffer for most purposes. */
23675 static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
23677 static const char *
23678 decode_mode_spec (struct window *w, register int c, int field_width,
23679 Lisp_Object *string)
23681 Lisp_Object obj;
23682 struct frame *f = XFRAME (WINDOW_FRAME (w));
23683 char *decode_mode_spec_buf = f->decode_mode_spec_buffer;
23684 /* We are going to use f->decode_mode_spec_buffer as the buffer to
23685 produce strings from numerical values, so limit preposterously
23686 large values of FIELD_WIDTH to avoid overrunning the buffer's
23687 end. The size of the buffer is enough for FRAME_MESSAGE_BUF_SIZE
23688 bytes plus the terminating null. */
23689 int width = min (field_width, FRAME_MESSAGE_BUF_SIZE (f));
23690 struct buffer *b = current_buffer;
23692 obj = Qnil;
23693 *string = Qnil;
23695 switch (c)
23697 case '*':
23698 if (!NILP (BVAR (b, read_only)))
23699 return "%";
23700 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
23701 return "*";
23702 return "-";
23704 case '+':
23705 /* This differs from %* only for a modified read-only buffer. */
23706 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
23707 return "*";
23708 if (!NILP (BVAR (b, read_only)))
23709 return "%";
23710 return "-";
23712 case '&':
23713 /* This differs from %* in ignoring read-only-ness. */
23714 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
23715 return "*";
23716 return "-";
23718 case '%':
23719 return "%";
23721 case '[':
23723 int i;
23724 char *p;
23726 if (command_loop_level > 5)
23727 return "[[[... ";
23728 p = decode_mode_spec_buf;
23729 for (i = 0; i < command_loop_level; i++)
23730 *p++ = '[';
23731 *p = 0;
23732 return decode_mode_spec_buf;
23735 case ']':
23737 int i;
23738 char *p;
23740 if (command_loop_level > 5)
23741 return " ...]]]";
23742 p = decode_mode_spec_buf;
23743 for (i = 0; i < command_loop_level; i++)
23744 *p++ = ']';
23745 *p = 0;
23746 return decode_mode_spec_buf;
23749 case '-':
23751 register int i;
23753 /* Let lots_of_dashes be a string of infinite length. */
23754 if (mode_line_target == MODE_LINE_NOPROP
23755 || mode_line_target == MODE_LINE_STRING)
23756 return "--";
23757 if (field_width <= 0
23758 || field_width > sizeof (lots_of_dashes))
23760 for (i = 0; i < FRAME_MESSAGE_BUF_SIZE (f) - 1; ++i)
23761 decode_mode_spec_buf[i] = '-';
23762 decode_mode_spec_buf[i] = '\0';
23763 return decode_mode_spec_buf;
23765 else
23766 return lots_of_dashes;
23769 case 'b':
23770 obj = BVAR (b, name);
23771 break;
23773 case 'c':
23774 case 'C':
23775 /* %c, %C, and %l are ignored in `frame-title-format'.
23776 (In redisplay_internal, the frame title is drawn _before_ the
23777 windows are updated, so the stuff which depends on actual
23778 window contents (such as %l) may fail to render properly, or
23779 even crash emacs.) */
23780 if (mode_line_target == MODE_LINE_TITLE)
23781 return "";
23782 else
23784 ptrdiff_t col = current_column ();
23785 int disp_col = (c == 'C') ? col + 1 : col;
23786 w->column_number_displayed = col;
23787 pint2str (decode_mode_spec_buf, width, disp_col);
23788 return decode_mode_spec_buf;
23791 case 'e':
23792 #if !defined SYSTEM_MALLOC && !defined HYBRID_MALLOC
23794 if (NILP (Vmemory_full))
23795 return "";
23796 else
23797 return "!MEM FULL! ";
23799 #else
23800 return "";
23801 #endif
23803 case 'F':
23804 /* %F displays the frame name. */
23805 if (!NILP (f->title))
23806 return SSDATA (f->title);
23807 if (f->explicit_name || ! FRAME_WINDOW_P (f))
23808 return SSDATA (f->name);
23809 return "Emacs";
23811 case 'f':
23812 obj = BVAR (b, filename);
23813 break;
23815 case 'i':
23817 ptrdiff_t size = ZV - BEGV;
23818 pint2str (decode_mode_spec_buf, width, size);
23819 return decode_mode_spec_buf;
23822 case 'I':
23824 ptrdiff_t size = ZV - BEGV;
23825 pint2hrstr (decode_mode_spec_buf, width, size);
23826 return decode_mode_spec_buf;
23829 case 'l':
23831 ptrdiff_t startpos, startpos_byte, line, linepos, linepos_byte;
23832 ptrdiff_t topline, nlines, height;
23833 ptrdiff_t junk;
23835 /* %c, %C, and %l are ignored in `frame-title-format'. */
23836 if (mode_line_target == MODE_LINE_TITLE)
23837 return "";
23839 startpos = marker_position (w->start);
23840 startpos_byte = marker_byte_position (w->start);
23841 height = WINDOW_TOTAL_LINES (w);
23843 /* If we decided that this buffer isn't suitable for line numbers,
23844 don't forget that too fast. */
23845 if (w->base_line_pos == -1)
23846 goto no_value;
23848 /* If the buffer is very big, don't waste time. */
23849 if (INTEGERP (Vline_number_display_limit)
23850 && BUF_ZV (b) - BUF_BEGV (b) > XINT (Vline_number_display_limit))
23852 w->base_line_pos = 0;
23853 w->base_line_number = 0;
23854 goto no_value;
23857 if (w->base_line_number > 0
23858 && w->base_line_pos > 0
23859 && w->base_line_pos <= startpos)
23861 line = w->base_line_number;
23862 linepos = w->base_line_pos;
23863 linepos_byte = buf_charpos_to_bytepos (b, linepos);
23865 else
23867 line = 1;
23868 linepos = BUF_BEGV (b);
23869 linepos_byte = BUF_BEGV_BYTE (b);
23872 /* Count lines from base line to window start position. */
23873 nlines = display_count_lines (linepos_byte,
23874 startpos_byte,
23875 startpos, &junk);
23877 topline = nlines + line;
23879 /* Determine a new base line, if the old one is too close
23880 or too far away, or if we did not have one.
23881 "Too close" means it's plausible a scroll-down would
23882 go back past it. */
23883 if (startpos == BUF_BEGV (b))
23885 w->base_line_number = topline;
23886 w->base_line_pos = BUF_BEGV (b);
23888 else if (nlines < height + 25 || nlines > height * 3 + 50
23889 || linepos == BUF_BEGV (b))
23891 ptrdiff_t limit = BUF_BEGV (b);
23892 ptrdiff_t limit_byte = BUF_BEGV_BYTE (b);
23893 ptrdiff_t position;
23894 ptrdiff_t distance =
23895 (height * 2 + 30) * line_number_display_limit_width;
23897 if (startpos - distance > limit)
23899 limit = startpos - distance;
23900 limit_byte = CHAR_TO_BYTE (limit);
23903 nlines = display_count_lines (startpos_byte,
23904 limit_byte,
23905 - (height * 2 + 30),
23906 &position);
23907 /* If we couldn't find the lines we wanted within
23908 line_number_display_limit_width chars per line,
23909 give up on line numbers for this window. */
23910 if (position == limit_byte && limit == startpos - distance)
23912 w->base_line_pos = -1;
23913 w->base_line_number = 0;
23914 goto no_value;
23917 w->base_line_number = topline - nlines;
23918 w->base_line_pos = BYTE_TO_CHAR (position);
23921 /* Now count lines from the start pos to point. */
23922 nlines = display_count_lines (startpos_byte,
23923 PT_BYTE, PT, &junk);
23925 /* Record that we did display the line number. */
23926 line_number_displayed = true;
23928 /* Make the string to show. */
23929 pint2str (decode_mode_spec_buf, width, topline + nlines);
23930 return decode_mode_spec_buf;
23931 no_value:
23933 char *p = decode_mode_spec_buf;
23934 int pad = width - 2;
23935 while (pad-- > 0)
23936 *p++ = ' ';
23937 *p++ = '?';
23938 *p++ = '?';
23939 *p = '\0';
23940 return decode_mode_spec_buf;
23943 break;
23945 case 'm':
23946 obj = BVAR (b, mode_name);
23947 break;
23949 case 'n':
23950 if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
23951 return " Narrow";
23952 break;
23954 /* Display the "degree of travel" of the window through the buffer. */
23955 case 'o':
23957 ptrdiff_t toppos = marker_position (w->start);
23958 ptrdiff_t botpos = BUF_Z (b) - w->window_end_pos;
23959 ptrdiff_t begv = BUF_BEGV (b);
23960 ptrdiff_t zv = BUF_ZV (b);
23962 if (zv <= botpos)
23963 return toppos <= begv ? "All" : "Bottom";
23964 else if (toppos <= begv)
23965 return "Top";
23966 else
23968 sprintf (decode_mode_spec_buf, "%2d%%",
23969 percent99 (toppos - begv, (toppos - begv) + (zv - botpos)));
23970 return decode_mode_spec_buf;
23974 /* Display percentage of buffer above the top of the screen. */
23975 case 'p':
23977 ptrdiff_t pos = marker_position (w->start);
23978 ptrdiff_t begv = BUF_BEGV (b);
23979 ptrdiff_t zv = BUF_ZV (b);
23981 if (w->window_end_pos <= BUF_Z (b) - zv)
23982 return pos <= begv ? "All" : "Bottom";
23983 else if (pos <= begv)
23984 return "Top";
23985 else
23987 sprintf (decode_mode_spec_buf, "%2d%%",
23988 percent99 (pos - begv, zv - begv));
23989 return decode_mode_spec_buf;
23993 /* Display percentage of size above the bottom of the screen. */
23994 case 'P':
23996 ptrdiff_t toppos = marker_position (w->start);
23997 ptrdiff_t botpos = BUF_Z (b) - w->window_end_pos;
23998 ptrdiff_t begv = BUF_BEGV (b);
23999 ptrdiff_t zv = BUF_ZV (b);
24001 if (zv <= botpos)
24002 return toppos <= begv ? "All" : "Bottom";
24003 else
24005 sprintf (decode_mode_spec_buf,
24006 &"Top%2d%%"[begv < toppos ? sizeof "Top" - 1 : 0],
24007 percent99 (botpos - begv, zv - begv));
24008 return decode_mode_spec_buf;
24012 /* Display percentage offsets of top and bottom of the window,
24013 using "All" (but not "Top" or "Bottom") where appropriate. */
24014 case 'q':
24016 ptrdiff_t toppos = marker_position (w->start);
24017 ptrdiff_t botpos = BUF_Z (b) - w->window_end_pos;
24018 ptrdiff_t begv = BUF_BEGV (b);
24019 ptrdiff_t zv = BUF_ZV (b);
24020 int top_perc, bot_perc;
24022 if ((toppos <= begv) && (zv <= botpos))
24023 return "All ";
24025 top_perc = toppos <= begv ? 0 : percent99 (toppos - begv, zv - begv);
24026 bot_perc = zv <= botpos ? 100 : percent99 (botpos - begv, zv - begv);
24028 if (top_perc == bot_perc)
24029 sprintf (decode_mode_spec_buf, "%d%%", top_perc);
24030 else
24031 sprintf (decode_mode_spec_buf, "%d-%d%%", top_perc, bot_perc);
24033 return decode_mode_spec_buf;
24036 case 's':
24037 /* status of process */
24038 obj = Fget_buffer_process (Fcurrent_buffer ());
24039 if (NILP (obj))
24040 return "no process";
24041 #ifndef MSDOS
24042 obj = Fsymbol_name (Fprocess_status (obj));
24043 #endif
24044 break;
24046 case '@':
24048 ptrdiff_t count = inhibit_garbage_collection ();
24049 Lisp_Object curdir = BVAR (current_buffer, directory);
24050 Lisp_Object val = Qnil;
24052 if (STRINGP (curdir))
24053 val = call1 (intern ("file-remote-p"), curdir);
24055 unbind_to (count, Qnil);
24057 if (NILP (val))
24058 return "-";
24059 else
24060 return "@";
24063 case 'z':
24064 /* coding-system (not including end-of-line format) */
24065 case 'Z':
24066 /* coding-system (including end-of-line type) */
24068 bool eol_flag = (c == 'Z');
24069 char *p = decode_mode_spec_buf;
24071 if (! FRAME_WINDOW_P (f))
24073 /* No need to mention EOL here--the terminal never needs
24074 to do EOL conversion. */
24075 p = decode_mode_spec_coding (CODING_ID_NAME
24076 (FRAME_KEYBOARD_CODING (f)->id),
24077 p, false);
24078 p = decode_mode_spec_coding (CODING_ID_NAME
24079 (FRAME_TERMINAL_CODING (f)->id),
24080 p, false);
24082 p = decode_mode_spec_coding (BVAR (b, buffer_file_coding_system),
24083 p, eol_flag);
24085 #if false /* This proves to be annoying; I think we can do without. -- rms. */
24086 #ifdef subprocesses
24087 obj = Fget_buffer_process (Fcurrent_buffer ());
24088 if (PROCESSP (obj))
24090 p = decode_mode_spec_coding
24091 (XPROCESS (obj)->decode_coding_system, p, eol_flag);
24092 p = decode_mode_spec_coding
24093 (XPROCESS (obj)->encode_coding_system, p, eol_flag);
24095 #endif /* subprocesses */
24096 #endif /* false */
24097 *p = 0;
24098 return decode_mode_spec_buf;
24102 if (STRINGP (obj))
24104 *string = obj;
24105 return SSDATA (obj);
24107 else
24108 return "";
24112 /* Count up to COUNT lines starting from START_BYTE. COUNT negative
24113 means count lines back from START_BYTE. But don't go beyond
24114 LIMIT_BYTE. Return the number of lines thus found (always
24115 nonnegative).
24117 Set *BYTE_POS_PTR to the byte position where we stopped. This is
24118 either the position COUNT lines after/before START_BYTE, if we
24119 found COUNT lines, or LIMIT_BYTE if we hit the limit before finding
24120 COUNT lines. */
24122 static ptrdiff_t
24123 display_count_lines (ptrdiff_t start_byte,
24124 ptrdiff_t limit_byte, ptrdiff_t count,
24125 ptrdiff_t *byte_pos_ptr)
24127 register unsigned char *cursor;
24128 unsigned char *base;
24130 register ptrdiff_t ceiling;
24131 register unsigned char *ceiling_addr;
24132 ptrdiff_t orig_count = count;
24134 /* If we are not in selective display mode,
24135 check only for newlines. */
24136 bool selective_display
24137 = (!NILP (BVAR (current_buffer, selective_display))
24138 && !INTEGERP (BVAR (current_buffer, selective_display)));
24140 if (count > 0)
24142 while (start_byte < limit_byte)
24144 ceiling = BUFFER_CEILING_OF (start_byte);
24145 ceiling = min (limit_byte - 1, ceiling);
24146 ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
24147 base = (cursor = BYTE_POS_ADDR (start_byte));
24151 if (selective_display)
24153 while (*cursor != '\n' && *cursor != 015
24154 && ++cursor != ceiling_addr)
24155 continue;
24156 if (cursor == ceiling_addr)
24157 break;
24159 else
24161 cursor = memchr (cursor, '\n', ceiling_addr - cursor);
24162 if (! cursor)
24163 break;
24166 cursor++;
24168 if (--count == 0)
24170 start_byte += cursor - base;
24171 *byte_pos_ptr = start_byte;
24172 return orig_count;
24175 while (cursor < ceiling_addr);
24177 start_byte += ceiling_addr - base;
24180 else
24182 while (start_byte > limit_byte)
24184 ceiling = BUFFER_FLOOR_OF (start_byte - 1);
24185 ceiling = max (limit_byte, ceiling);
24186 ceiling_addr = BYTE_POS_ADDR (ceiling);
24187 base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
24188 while (true)
24190 if (selective_display)
24192 while (--cursor >= ceiling_addr
24193 && *cursor != '\n' && *cursor != 015)
24194 continue;
24195 if (cursor < ceiling_addr)
24196 break;
24198 else
24200 cursor = memrchr (ceiling_addr, '\n', cursor - ceiling_addr);
24201 if (! cursor)
24202 break;
24205 if (++count == 0)
24207 start_byte += cursor - base + 1;
24208 *byte_pos_ptr = start_byte;
24209 /* When scanning backwards, we should
24210 not count the newline posterior to which we stop. */
24211 return - orig_count - 1;
24214 start_byte += ceiling_addr - base;
24218 *byte_pos_ptr = limit_byte;
24220 if (count < 0)
24221 return - orig_count + count;
24222 return orig_count - count;
24228 /***********************************************************************
24229 Displaying strings
24230 ***********************************************************************/
24232 /* Display a NUL-terminated string, starting with index START.
24234 If STRING is non-null, display that C string. Otherwise, the Lisp
24235 string LISP_STRING is displayed. There's a case that STRING is
24236 non-null and LISP_STRING is not nil. It means STRING is a string
24237 data of LISP_STRING. In that case, we display LISP_STRING while
24238 ignoring its text properties.
24240 If FACE_STRING is not nil, FACE_STRING_POS is a position in
24241 FACE_STRING. Display STRING or LISP_STRING with the face at
24242 FACE_STRING_POS in FACE_STRING:
24244 Display the string in the environment given by IT, but use the
24245 standard display table, temporarily.
24247 FIELD_WIDTH is the minimum number of output glyphs to produce.
24248 If STRING has fewer characters than FIELD_WIDTH, pad to the right
24249 with spaces. If STRING has more characters, more than FIELD_WIDTH
24250 glyphs will be produced. FIELD_WIDTH <= 0 means don't pad.
24252 PRECISION is the maximum number of characters to output from
24253 STRING. PRECISION < 0 means don't truncate the string.
24255 This is roughly equivalent to printf format specifiers:
24257 FIELD_WIDTH PRECISION PRINTF
24258 ----------------------------------------
24259 -1 -1 %s
24260 -1 10 %.10s
24261 10 -1 %10s
24262 20 10 %20.10s
24264 MULTIBYTE zero means do not display multibyte chars, > 0 means do
24265 display them, and < 0 means obey the current buffer's value of
24266 enable_multibyte_characters.
24268 Value is the number of columns displayed. */
24270 static int
24271 display_string (const char *string, Lisp_Object lisp_string, Lisp_Object face_string,
24272 ptrdiff_t face_string_pos, ptrdiff_t start, struct it *it,
24273 int field_width, int precision, int max_x, int multibyte)
24275 int hpos_at_start = it->hpos;
24276 int saved_face_id = it->face_id;
24277 struct glyph_row *row = it->glyph_row;
24278 ptrdiff_t it_charpos;
24280 /* Initialize the iterator IT for iteration over STRING beginning
24281 with index START. */
24282 reseat_to_string (it, NILP (lisp_string) ? string : NULL, lisp_string, start,
24283 precision, field_width, multibyte);
24284 if (string && STRINGP (lisp_string))
24285 /* LISP_STRING is the one returned by decode_mode_spec. We should
24286 ignore its text properties. */
24287 it->stop_charpos = it->end_charpos;
24289 /* If displaying STRING, set up the face of the iterator from
24290 FACE_STRING, if that's given. */
24291 if (STRINGP (face_string))
24293 ptrdiff_t endptr;
24294 struct face *face;
24296 it->face_id
24297 = face_at_string_position (it->w, face_string, face_string_pos,
24298 0, &endptr, it->base_face_id, false);
24299 face = FACE_FROM_ID (it->f, it->face_id);
24300 it->face_box_p = face->box != FACE_NO_BOX;
24303 /* Set max_x to the maximum allowed X position. Don't let it go
24304 beyond the right edge of the window. */
24305 if (max_x <= 0)
24306 max_x = it->last_visible_x;
24307 else
24308 max_x = min (max_x, it->last_visible_x);
24310 /* Skip over display elements that are not visible. because IT->w is
24311 hscrolled. */
24312 if (it->current_x < it->first_visible_x)
24313 move_it_in_display_line_to (it, 100000, it->first_visible_x,
24314 MOVE_TO_POS | MOVE_TO_X);
24316 row->ascent = it->max_ascent;
24317 row->height = it->max_ascent + it->max_descent;
24318 row->phys_ascent = it->max_phys_ascent;
24319 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
24320 row->extra_line_spacing = it->max_extra_line_spacing;
24322 if (STRINGP (it->string))
24323 it_charpos = IT_STRING_CHARPOS (*it);
24324 else
24325 it_charpos = IT_CHARPOS (*it);
24327 /* This condition is for the case that we are called with current_x
24328 past last_visible_x. */
24329 while (it->current_x < max_x)
24331 int x_before, x, n_glyphs_before, i, nglyphs;
24333 /* Get the next display element. */
24334 if (!get_next_display_element (it))
24335 break;
24337 /* Produce glyphs. */
24338 x_before = it->current_x;
24339 n_glyphs_before = row->used[TEXT_AREA];
24340 PRODUCE_GLYPHS (it);
24342 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
24343 i = 0;
24344 x = x_before;
24345 while (i < nglyphs)
24347 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
24349 if (it->line_wrap != TRUNCATE
24350 && x + glyph->pixel_width > max_x)
24352 /* End of continued line or max_x reached. */
24353 if (CHAR_GLYPH_PADDING_P (*glyph))
24355 /* A wide character is unbreakable. */
24356 if (row->reversed_p)
24357 unproduce_glyphs (it, row->used[TEXT_AREA]
24358 - n_glyphs_before);
24359 row->used[TEXT_AREA] = n_glyphs_before;
24360 it->current_x = x_before;
24362 else
24364 if (row->reversed_p)
24365 unproduce_glyphs (it, row->used[TEXT_AREA]
24366 - (n_glyphs_before + i));
24367 row->used[TEXT_AREA] = n_glyphs_before + i;
24368 it->current_x = x;
24370 break;
24372 else if (x + glyph->pixel_width >= it->first_visible_x)
24374 /* Glyph is at least partially visible. */
24375 ++it->hpos;
24376 if (x < it->first_visible_x)
24377 row->x = x - it->first_visible_x;
24379 else
24381 /* Glyph is off the left margin of the display area.
24382 Should not happen. */
24383 emacs_abort ();
24386 row->ascent = max (row->ascent, it->max_ascent);
24387 row->height = max (row->height, it->max_ascent + it->max_descent);
24388 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
24389 row->phys_height = max (row->phys_height,
24390 it->max_phys_ascent + it->max_phys_descent);
24391 row->extra_line_spacing = max (row->extra_line_spacing,
24392 it->max_extra_line_spacing);
24393 x += glyph->pixel_width;
24394 ++i;
24397 /* Stop if max_x reached. */
24398 if (i < nglyphs)
24399 break;
24401 /* Stop at line ends. */
24402 if (ITERATOR_AT_END_OF_LINE_P (it))
24404 it->continuation_lines_width = 0;
24405 break;
24408 set_iterator_to_next (it, true);
24409 if (STRINGP (it->string))
24410 it_charpos = IT_STRING_CHARPOS (*it);
24411 else
24412 it_charpos = IT_CHARPOS (*it);
24414 /* Stop if truncating at the right edge. */
24415 if (it->line_wrap == TRUNCATE
24416 && it->current_x >= it->last_visible_x)
24418 /* Add truncation mark, but don't do it if the line is
24419 truncated at a padding space. */
24420 if (it_charpos < it->string_nchars)
24422 if (!FRAME_WINDOW_P (it->f))
24424 int ii, n;
24426 if (it->current_x > it->last_visible_x)
24428 if (!row->reversed_p)
24430 for (ii = row->used[TEXT_AREA] - 1; ii > 0; --ii)
24431 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][ii]))
24432 break;
24434 else
24436 for (ii = 0; ii < row->used[TEXT_AREA]; ii++)
24437 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][ii]))
24438 break;
24439 unproduce_glyphs (it, ii + 1);
24440 ii = row->used[TEXT_AREA] - (ii + 1);
24442 for (n = row->used[TEXT_AREA]; ii < n; ++ii)
24444 row->used[TEXT_AREA] = ii;
24445 produce_special_glyphs (it, IT_TRUNCATION);
24448 produce_special_glyphs (it, IT_TRUNCATION);
24450 row->truncated_on_right_p = true;
24452 break;
24456 /* Maybe insert a truncation at the left. */
24457 if (it->first_visible_x
24458 && it_charpos > 0)
24460 if (!FRAME_WINDOW_P (it->f)
24461 || (row->reversed_p
24462 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
24463 : WINDOW_LEFT_FRINGE_WIDTH (it->w)) == 0)
24464 insert_left_trunc_glyphs (it);
24465 row->truncated_on_left_p = true;
24468 it->face_id = saved_face_id;
24470 /* Value is number of columns displayed. */
24471 return it->hpos - hpos_at_start;
24476 /* This is like a combination of memq and assq. Return 1/2 if PROPVAL
24477 appears as an element of LIST or as the car of an element of LIST.
24478 If PROPVAL is a list, compare each element against LIST in that
24479 way, and return 1/2 if any element of PROPVAL is found in LIST.
24480 Otherwise return 0. This function cannot quit.
24481 The return value is 2 if the text is invisible but with an ellipsis
24482 and 1 if it's invisible and without an ellipsis. */
24485 invisible_prop (Lisp_Object propval, Lisp_Object list)
24487 Lisp_Object tail, proptail;
24489 for (tail = list; CONSP (tail); tail = XCDR (tail))
24491 register Lisp_Object tem;
24492 tem = XCAR (tail);
24493 if (EQ (propval, tem))
24494 return 1;
24495 if (CONSP (tem) && EQ (propval, XCAR (tem)))
24496 return NILP (XCDR (tem)) ? 1 : 2;
24499 if (CONSP (propval))
24501 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
24503 Lisp_Object propelt;
24504 propelt = XCAR (proptail);
24505 for (tail = list; CONSP (tail); tail = XCDR (tail))
24507 register Lisp_Object tem;
24508 tem = XCAR (tail);
24509 if (EQ (propelt, tem))
24510 return 1;
24511 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
24512 return NILP (XCDR (tem)) ? 1 : 2;
24517 return 0;
24520 DEFUN ("invisible-p", Finvisible_p, Sinvisible_p, 1, 1, 0,
24521 doc: /* Non-nil if the property makes the text invisible.
24522 POS-OR-PROP can be a marker or number, in which case it is taken to be
24523 a position in the current buffer and the value of the `invisible' property
24524 is checked; or it can be some other value, which is then presumed to be the
24525 value of the `invisible' property of the text of interest.
24526 The non-nil value returned can be t for truly invisible text or something
24527 else if the text is replaced by an ellipsis. */)
24528 (Lisp_Object pos_or_prop)
24530 Lisp_Object prop
24531 = (NATNUMP (pos_or_prop) || MARKERP (pos_or_prop)
24532 ? Fget_char_property (pos_or_prop, Qinvisible, Qnil)
24533 : pos_or_prop);
24534 int invis = TEXT_PROP_MEANS_INVISIBLE (prop);
24535 return (invis == 0 ? Qnil
24536 : invis == 1 ? Qt
24537 : make_number (invis));
24540 /* Calculate a width or height in pixels from a specification using
24541 the following elements:
24543 SPEC ::=
24544 NUM - a (fractional) multiple of the default font width/height
24545 (NUM) - specifies exactly NUM pixels
24546 UNIT - a fixed number of pixels, see below.
24547 ELEMENT - size of a display element in pixels, see below.
24548 (NUM . SPEC) - equals NUM * SPEC
24549 (+ SPEC SPEC ...) - add pixel values
24550 (- SPEC SPEC ...) - subtract pixel values
24551 (- SPEC) - negate pixel value
24553 NUM ::=
24554 INT or FLOAT - a number constant
24555 SYMBOL - use symbol's (buffer local) variable binding.
24557 UNIT ::=
24558 in - pixels per inch *)
24559 mm - pixels per 1/1000 meter *)
24560 cm - pixels per 1/100 meter *)
24561 width - width of current font in pixels.
24562 height - height of current font in pixels.
24564 *) using the ratio(s) defined in display-pixels-per-inch.
24566 ELEMENT ::=
24568 left-fringe - left fringe width in pixels
24569 right-fringe - right fringe width in pixels
24571 left-margin - left margin width in pixels
24572 right-margin - right margin width in pixels
24574 scroll-bar - scroll-bar area width in pixels
24576 Examples:
24578 Pixels corresponding to 5 inches:
24579 (5 . in)
24581 Total width of non-text areas on left side of window (if scroll-bar is on left):
24582 '(space :width (+ left-fringe left-margin scroll-bar))
24584 Align to first text column (in header line):
24585 '(space :align-to 0)
24587 Align to middle of text area minus half the width of variable `my-image'
24588 containing a loaded image:
24589 '(space :align-to (0.5 . (- text my-image)))
24591 Width of left margin minus width of 1 character in the default font:
24592 '(space :width (- left-margin 1))
24594 Width of left margin minus width of 2 characters in the current font:
24595 '(space :width (- left-margin (2 . width)))
24597 Center 1 character over left-margin (in header line):
24598 '(space :align-to (+ left-margin (0.5 . left-margin) -0.5))
24600 Different ways to express width of left fringe plus left margin minus one pixel:
24601 '(space :width (- (+ left-fringe left-margin) (1)))
24602 '(space :width (+ left-fringe left-margin (- (1))))
24603 '(space :width (+ left-fringe left-margin (-1)))
24607 static bool
24608 calc_pixel_width_or_height (double *res, struct it *it, Lisp_Object prop,
24609 struct font *font, bool width_p, int *align_to)
24611 double pixels;
24613 # define OK_PIXELS(val) (*res = (val), true)
24614 # define OK_ALIGN_TO(val) (*align_to = (val), true)
24616 if (NILP (prop))
24617 return OK_PIXELS (0);
24619 eassert (FRAME_LIVE_P (it->f));
24621 if (SYMBOLP (prop))
24623 if (SCHARS (SYMBOL_NAME (prop)) == 2)
24625 char *unit = SSDATA (SYMBOL_NAME (prop));
24627 if (unit[0] == 'i' && unit[1] == 'n')
24628 pixels = 1.0;
24629 else if (unit[0] == 'm' && unit[1] == 'm')
24630 pixels = 25.4;
24631 else if (unit[0] == 'c' && unit[1] == 'm')
24632 pixels = 2.54;
24633 else
24634 pixels = 0;
24635 if (pixels > 0)
24637 double ppi = (width_p ? FRAME_RES_X (it->f)
24638 : FRAME_RES_Y (it->f));
24640 if (ppi > 0)
24641 return OK_PIXELS (ppi / pixels);
24642 return false;
24646 #ifdef HAVE_WINDOW_SYSTEM
24647 if (EQ (prop, Qheight))
24648 return OK_PIXELS (font
24649 ? normal_char_height (font, -1)
24650 : FRAME_LINE_HEIGHT (it->f));
24651 if (EQ (prop, Qwidth))
24652 return OK_PIXELS (font
24653 ? FONT_WIDTH (font)
24654 : FRAME_COLUMN_WIDTH (it->f));
24655 #else
24656 if (EQ (prop, Qheight) || EQ (prop, Qwidth))
24657 return OK_PIXELS (1);
24658 #endif
24660 if (EQ (prop, Qtext))
24661 return OK_PIXELS (width_p
24662 ? window_box_width (it->w, TEXT_AREA)
24663 : WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w));
24665 if (align_to && *align_to < 0)
24667 *res = 0;
24668 if (EQ (prop, Qleft))
24669 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA));
24670 if (EQ (prop, Qright))
24671 return OK_ALIGN_TO (window_box_right_offset (it->w, TEXT_AREA));
24672 if (EQ (prop, Qcenter))
24673 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA)
24674 + window_box_width (it->w, TEXT_AREA) / 2);
24675 if (EQ (prop, Qleft_fringe))
24676 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
24677 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (it->w)
24678 : window_box_right_offset (it->w, LEFT_MARGIN_AREA));
24679 if (EQ (prop, Qright_fringe))
24680 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
24681 ? window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
24682 : window_box_right_offset (it->w, TEXT_AREA));
24683 if (EQ (prop, Qleft_margin))
24684 return OK_ALIGN_TO (window_box_left_offset (it->w, LEFT_MARGIN_AREA));
24685 if (EQ (prop, Qright_margin))
24686 return OK_ALIGN_TO (window_box_left_offset (it->w, RIGHT_MARGIN_AREA));
24687 if (EQ (prop, Qscroll_bar))
24688 return OK_ALIGN_TO (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (it->w)
24690 : (window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
24691 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
24692 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
24693 : 0)));
24695 else
24697 if (EQ (prop, Qleft_fringe))
24698 return OK_PIXELS (WINDOW_LEFT_FRINGE_WIDTH (it->w));
24699 if (EQ (prop, Qright_fringe))
24700 return OK_PIXELS (WINDOW_RIGHT_FRINGE_WIDTH (it->w));
24701 if (EQ (prop, Qleft_margin))
24702 return OK_PIXELS (WINDOW_LEFT_MARGIN_WIDTH (it->w));
24703 if (EQ (prop, Qright_margin))
24704 return OK_PIXELS (WINDOW_RIGHT_MARGIN_WIDTH (it->w));
24705 if (EQ (prop, Qscroll_bar))
24706 return OK_PIXELS (WINDOW_SCROLL_BAR_AREA_WIDTH (it->w));
24709 prop = buffer_local_value (prop, it->w->contents);
24710 if (EQ (prop, Qunbound))
24711 prop = Qnil;
24714 if (NUMBERP (prop))
24716 int base_unit = (width_p
24717 ? FRAME_COLUMN_WIDTH (it->f)
24718 : FRAME_LINE_HEIGHT (it->f));
24719 return OK_PIXELS (XFLOATINT (prop) * base_unit);
24722 if (CONSP (prop))
24724 Lisp_Object car = XCAR (prop);
24725 Lisp_Object cdr = XCDR (prop);
24727 if (SYMBOLP (car))
24729 #ifdef HAVE_WINDOW_SYSTEM
24730 if (FRAME_WINDOW_P (it->f)
24731 && valid_image_p (prop))
24733 ptrdiff_t id = lookup_image (it->f, prop);
24734 struct image *img = IMAGE_FROM_ID (it->f, id);
24736 return OK_PIXELS (width_p ? img->width : img->height);
24738 if (FRAME_WINDOW_P (it->f) && valid_xwidget_spec_p (prop))
24740 /* TODO: Don't return dummy size. */
24741 return OK_PIXELS (100);
24743 #endif
24744 if (EQ (car, Qplus) || EQ (car, Qminus))
24746 bool first = true;
24747 double px;
24749 pixels = 0;
24750 while (CONSP (cdr))
24752 if (!calc_pixel_width_or_height (&px, it, XCAR (cdr),
24753 font, width_p, align_to))
24754 return false;
24755 if (first)
24756 pixels = (EQ (car, Qplus) ? px : -px), first = false;
24757 else
24758 pixels += px;
24759 cdr = XCDR (cdr);
24761 if (EQ (car, Qminus))
24762 pixels = -pixels;
24763 return OK_PIXELS (pixels);
24766 car = buffer_local_value (car, it->w->contents);
24767 if (EQ (car, Qunbound))
24768 car = Qnil;
24771 if (NUMBERP (car))
24773 double fact;
24774 pixels = XFLOATINT (car);
24775 if (NILP (cdr))
24776 return OK_PIXELS (pixels);
24777 if (calc_pixel_width_or_height (&fact, it, cdr,
24778 font, width_p, align_to))
24779 return OK_PIXELS (pixels * fact);
24780 return false;
24783 return false;
24786 return false;
24789 void
24790 get_font_ascent_descent (struct font *font, int *ascent, int *descent)
24792 #ifdef HAVE_WINDOW_SYSTEM
24793 normal_char_ascent_descent (font, -1, ascent, descent);
24794 #else
24795 *ascent = 1;
24796 *descent = 0;
24797 #endif
24801 /***********************************************************************
24802 Glyph Display
24803 ***********************************************************************/
24805 #ifdef HAVE_WINDOW_SYSTEM
24807 #ifdef GLYPH_DEBUG
24809 void
24810 dump_glyph_string (struct glyph_string *s)
24812 fprintf (stderr, "glyph string\n");
24813 fprintf (stderr, " x, y, w, h = %d, %d, %d, %d\n",
24814 s->x, s->y, s->width, s->height);
24815 fprintf (stderr, " ybase = %d\n", s->ybase);
24816 fprintf (stderr, " hl = %u\n", s->hl);
24817 fprintf (stderr, " left overhang = %d, right = %d\n",
24818 s->left_overhang, s->right_overhang);
24819 fprintf (stderr, " nchars = %d\n", s->nchars);
24820 fprintf (stderr, " extends to end of line = %d\n",
24821 s->extends_to_end_of_line_p);
24822 fprintf (stderr, " font height = %d\n", FONT_HEIGHT (s->font));
24823 fprintf (stderr, " bg width = %d\n", s->background_width);
24826 #endif /* GLYPH_DEBUG */
24828 /* Initialize glyph string S. CHAR2B is a suitably allocated vector
24829 of XChar2b structures for S; it can't be allocated in
24830 init_glyph_string because it must be allocated via `alloca'. W
24831 is the window on which S is drawn. ROW and AREA are the glyph row
24832 and area within the row from which S is constructed. START is the
24833 index of the first glyph structure covered by S. HL is a
24834 face-override for drawing S. */
24836 #ifdef HAVE_NTGUI
24837 #define OPTIONAL_HDC(hdc) HDC hdc,
24838 #define DECLARE_HDC(hdc) HDC hdc;
24839 #define ALLOCATE_HDC(hdc, f) hdc = get_frame_dc ((f))
24840 #define RELEASE_HDC(hdc, f) release_frame_dc ((f), (hdc))
24841 #endif
24843 #ifndef OPTIONAL_HDC
24844 #define OPTIONAL_HDC(hdc)
24845 #define DECLARE_HDC(hdc)
24846 #define ALLOCATE_HDC(hdc, f)
24847 #define RELEASE_HDC(hdc, f)
24848 #endif
24850 static void
24851 init_glyph_string (struct glyph_string *s,
24852 OPTIONAL_HDC (hdc)
24853 XChar2b *char2b, struct window *w, struct glyph_row *row,
24854 enum glyph_row_area area, int start, enum draw_glyphs_face hl)
24856 memset (s, 0, sizeof *s);
24857 s->w = w;
24858 s->f = XFRAME (w->frame);
24859 #ifdef HAVE_NTGUI
24860 s->hdc = hdc;
24861 #endif
24862 s->display = FRAME_X_DISPLAY (s->f);
24863 s->char2b = char2b;
24864 s->hl = hl;
24865 s->row = row;
24866 s->area = area;
24867 s->first_glyph = row->glyphs[area] + start;
24868 s->height = row->height;
24869 s->y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
24870 s->ybase = s->y + row->ascent;
24874 /* Append the list of glyph strings with head H and tail T to the list
24875 with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the result. */
24877 static void
24878 append_glyph_string_lists (struct glyph_string **head, struct glyph_string **tail,
24879 struct glyph_string *h, struct glyph_string *t)
24881 if (h)
24883 if (*head)
24884 (*tail)->next = h;
24885 else
24886 *head = h;
24887 h->prev = *tail;
24888 *tail = t;
24893 /* Prepend the list of glyph strings with head H and tail T to the
24894 list with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the
24895 result. */
24897 static void
24898 prepend_glyph_string_lists (struct glyph_string **head, struct glyph_string **tail,
24899 struct glyph_string *h, struct glyph_string *t)
24901 if (h)
24903 if (*head)
24904 (*head)->prev = t;
24905 else
24906 *tail = t;
24907 t->next = *head;
24908 *head = h;
24913 /* Append glyph string S to the list with head *HEAD and tail *TAIL.
24914 Set *HEAD and *TAIL to the resulting list. */
24916 static void
24917 append_glyph_string (struct glyph_string **head, struct glyph_string **tail,
24918 struct glyph_string *s)
24920 s->next = s->prev = NULL;
24921 append_glyph_string_lists (head, tail, s, s);
24925 /* Get face and two-byte form of character C in face FACE_ID on frame F.
24926 The encoding of C is returned in *CHAR2B. DISPLAY_P means
24927 make sure that X resources for the face returned are allocated.
24928 Value is a pointer to a realized face that is ready for display if
24929 DISPLAY_P. */
24931 static struct face *
24932 get_char_face_and_encoding (struct frame *f, int c, int face_id,
24933 XChar2b *char2b, bool display_p)
24935 struct face *face = FACE_FROM_ID (f, face_id);
24936 unsigned code = 0;
24938 if (face->font)
24940 code = face->font->driver->encode_char (face->font, c);
24942 if (code == FONT_INVALID_CODE)
24943 code = 0;
24945 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
24947 /* Make sure X resources of the face are allocated. */
24948 #ifdef HAVE_X_WINDOWS
24949 if (display_p)
24950 #endif
24952 eassert (face != NULL);
24953 prepare_face_for_display (f, face);
24956 return face;
24960 /* Get face and two-byte form of character glyph GLYPH on frame F.
24961 The encoding of GLYPH->u.ch is returned in *CHAR2B. Value is
24962 a pointer to a realized face that is ready for display. */
24964 static struct face *
24965 get_glyph_face_and_encoding (struct frame *f, struct glyph *glyph,
24966 XChar2b *char2b)
24968 struct face *face;
24969 unsigned code = 0;
24971 eassert (glyph->type == CHAR_GLYPH);
24972 face = FACE_FROM_ID (f, glyph->face_id);
24974 /* Make sure X resources of the face are allocated. */
24975 prepare_face_for_display (f, face);
24977 if (face->font)
24979 if (CHAR_BYTE8_P (glyph->u.ch))
24980 code = CHAR_TO_BYTE8 (glyph->u.ch);
24981 else
24982 code = face->font->driver->encode_char (face->font, glyph->u.ch);
24984 if (code == FONT_INVALID_CODE)
24985 code = 0;
24988 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
24989 return face;
24993 /* Get glyph code of character C in FONT in the two-byte form CHAR2B.
24994 Return true iff FONT has a glyph for C. */
24996 static bool
24997 get_char_glyph_code (int c, struct font *font, XChar2b *char2b)
24999 unsigned code;
25001 if (CHAR_BYTE8_P (c))
25002 code = CHAR_TO_BYTE8 (c);
25003 else
25004 code = font->driver->encode_char (font, c);
25006 if (code == FONT_INVALID_CODE)
25007 return false;
25008 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
25009 return true;
25013 /* Fill glyph string S with composition components specified by S->cmp.
25015 BASE_FACE is the base face of the composition.
25016 S->cmp_from is the index of the first component for S.
25018 OVERLAPS non-zero means S should draw the foreground only, and use
25019 its physical height for clipping. See also draw_glyphs.
25021 Value is the index of a component not in S. */
25023 static int
25024 fill_composite_glyph_string (struct glyph_string *s, struct face *base_face,
25025 int overlaps)
25027 int i;
25028 /* For all glyphs of this composition, starting at the offset
25029 S->cmp_from, until we reach the end of the definition or encounter a
25030 glyph that requires the different face, add it to S. */
25031 struct face *face;
25033 eassert (s);
25035 s->for_overlaps = overlaps;
25036 s->face = NULL;
25037 s->font = NULL;
25038 for (i = s->cmp_from; i < s->cmp->glyph_len; i++)
25040 int c = COMPOSITION_GLYPH (s->cmp, i);
25042 /* TAB in a composition means display glyphs with padding space
25043 on the left or right. */
25044 if (c != '\t')
25046 int face_id = FACE_FOR_CHAR (s->f, base_face->ascii_face, c,
25047 -1, Qnil);
25049 face = get_char_face_and_encoding (s->f, c, face_id,
25050 s->char2b + i, true);
25051 if (face)
25053 if (! s->face)
25055 s->face = face;
25056 s->font = s->face->font;
25058 else if (s->face != face)
25059 break;
25062 ++s->nchars;
25064 s->cmp_to = i;
25066 if (s->face == NULL)
25068 s->face = base_face->ascii_face;
25069 s->font = s->face->font;
25072 /* All glyph strings for the same composition has the same width,
25073 i.e. the width set for the first component of the composition. */
25074 s->width = s->first_glyph->pixel_width;
25076 /* If the specified font could not be loaded, use the frame's
25077 default font, but record the fact that we couldn't load it in
25078 the glyph string so that we can draw rectangles for the
25079 characters of the glyph string. */
25080 if (s->font == NULL)
25082 s->font_not_found_p = true;
25083 s->font = FRAME_FONT (s->f);
25086 /* Adjust base line for subscript/superscript text. */
25087 s->ybase += s->first_glyph->voffset;
25089 return s->cmp_to;
25092 static int
25093 fill_gstring_glyph_string (struct glyph_string *s, int face_id,
25094 int start, int end, int overlaps)
25096 struct glyph *glyph, *last;
25097 Lisp_Object lgstring;
25098 int i;
25100 s->for_overlaps = overlaps;
25101 glyph = s->row->glyphs[s->area] + start;
25102 last = s->row->glyphs[s->area] + end;
25103 s->cmp_id = glyph->u.cmp.id;
25104 s->cmp_from = glyph->slice.cmp.from;
25105 s->cmp_to = glyph->slice.cmp.to + 1;
25106 s->face = FACE_FROM_ID (s->f, face_id);
25107 lgstring = composition_gstring_from_id (s->cmp_id);
25108 s->font = XFONT_OBJECT (LGSTRING_FONT (lgstring));
25109 glyph++;
25110 while (glyph < last
25111 && glyph->u.cmp.automatic
25112 && glyph->u.cmp.id == s->cmp_id
25113 && s->cmp_to == glyph->slice.cmp.from)
25114 s->cmp_to = (glyph++)->slice.cmp.to + 1;
25116 for (i = s->cmp_from; i < s->cmp_to; i++)
25118 Lisp_Object lglyph = LGSTRING_GLYPH (lgstring, i);
25119 unsigned code = LGLYPH_CODE (lglyph);
25121 STORE_XCHAR2B ((s->char2b + i), code >> 8, code & 0xFF);
25123 s->width = composition_gstring_width (lgstring, s->cmp_from, s->cmp_to, NULL);
25124 return glyph - s->row->glyphs[s->area];
25128 /* Fill glyph string S from a sequence glyphs for glyphless characters.
25129 See the comment of fill_glyph_string for arguments.
25130 Value is the index of the first glyph not in S. */
25133 static int
25134 fill_glyphless_glyph_string (struct glyph_string *s, int face_id,
25135 int start, int end, int overlaps)
25137 struct glyph *glyph, *last;
25138 int voffset;
25140 eassert (s->first_glyph->type == GLYPHLESS_GLYPH);
25141 s->for_overlaps = overlaps;
25142 glyph = s->row->glyphs[s->area] + start;
25143 last = s->row->glyphs[s->area] + end;
25144 voffset = glyph->voffset;
25145 s->face = FACE_FROM_ID (s->f, face_id);
25146 s->font = s->face->font ? s->face->font : FRAME_FONT (s->f);
25147 s->nchars = 1;
25148 s->width = glyph->pixel_width;
25149 glyph++;
25150 while (glyph < last
25151 && glyph->type == GLYPHLESS_GLYPH
25152 && glyph->voffset == voffset
25153 && glyph->face_id == face_id)
25155 s->nchars++;
25156 s->width += glyph->pixel_width;
25157 glyph++;
25159 s->ybase += voffset;
25160 return glyph - s->row->glyphs[s->area];
25164 /* Fill glyph string S from a sequence of character glyphs.
25166 FACE_ID is the face id of the string. START is the index of the
25167 first glyph to consider, END is the index of the last + 1.
25168 OVERLAPS non-zero means S should draw the foreground only, and use
25169 its physical height for clipping. See also draw_glyphs.
25171 Value is the index of the first glyph not in S. */
25173 static int
25174 fill_glyph_string (struct glyph_string *s, int face_id,
25175 int start, int end, int overlaps)
25177 struct glyph *glyph, *last;
25178 int voffset;
25179 bool glyph_not_available_p;
25181 eassert (s->f == XFRAME (s->w->frame));
25182 eassert (s->nchars == 0);
25183 eassert (start >= 0 && end > start);
25185 s->for_overlaps = overlaps;
25186 glyph = s->row->glyphs[s->area] + start;
25187 last = s->row->glyphs[s->area] + end;
25188 voffset = glyph->voffset;
25189 s->padding_p = glyph->padding_p;
25190 glyph_not_available_p = glyph->glyph_not_available_p;
25192 while (glyph < last
25193 && glyph->type == CHAR_GLYPH
25194 && glyph->voffset == voffset
25195 /* Same face id implies same font, nowadays. */
25196 && glyph->face_id == face_id
25197 && glyph->glyph_not_available_p == glyph_not_available_p)
25199 s->face = get_glyph_face_and_encoding (s->f, glyph,
25200 s->char2b + s->nchars);
25201 ++s->nchars;
25202 eassert (s->nchars <= end - start);
25203 s->width += glyph->pixel_width;
25204 if (glyph++->padding_p != s->padding_p)
25205 break;
25208 s->font = s->face->font;
25210 /* If the specified font could not be loaded, use the frame's font,
25211 but record the fact that we couldn't load it in
25212 S->font_not_found_p so that we can draw rectangles for the
25213 characters of the glyph string. */
25214 if (s->font == NULL || glyph_not_available_p)
25216 s->font_not_found_p = true;
25217 s->font = FRAME_FONT (s->f);
25220 /* Adjust base line for subscript/superscript text. */
25221 s->ybase += voffset;
25223 eassert (s->face && s->face->gc);
25224 return glyph - s->row->glyphs[s->area];
25228 /* Fill glyph string S from image glyph S->first_glyph. */
25230 static void
25231 fill_image_glyph_string (struct glyph_string *s)
25233 eassert (s->first_glyph->type == IMAGE_GLYPH);
25234 s->img = IMAGE_FROM_ID (s->f, s->first_glyph->u.img_id);
25235 eassert (s->img);
25236 s->slice = s->first_glyph->slice.img;
25237 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
25238 s->font = s->face->font;
25239 s->width = s->first_glyph->pixel_width;
25241 /* Adjust base line for subscript/superscript text. */
25242 s->ybase += s->first_glyph->voffset;
25246 #ifdef HAVE_XWIDGETS
25247 static void
25248 fill_xwidget_glyph_string (struct glyph_string *s)
25250 eassert (s->first_glyph->type == XWIDGET_GLYPH);
25251 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
25252 s->font = s->face->font;
25253 s->width = s->first_glyph->pixel_width;
25254 s->ybase += s->first_glyph->voffset;
25255 s->xwidget = s->first_glyph->u.xwidget;
25257 #endif
25258 /* Fill glyph string S from a sequence of stretch glyphs.
25260 START is the index of the first glyph to consider,
25261 END is the index of the last + 1.
25263 Value is the index of the first glyph not in S. */
25265 static int
25266 fill_stretch_glyph_string (struct glyph_string *s, int start, int end)
25268 struct glyph *glyph, *last;
25269 int voffset, face_id;
25271 eassert (s->first_glyph->type == STRETCH_GLYPH);
25273 glyph = s->row->glyphs[s->area] + start;
25274 last = s->row->glyphs[s->area] + end;
25275 face_id = glyph->face_id;
25276 s->face = FACE_FROM_ID (s->f, face_id);
25277 s->font = s->face->font;
25278 s->width = glyph->pixel_width;
25279 s->nchars = 1;
25280 voffset = glyph->voffset;
25282 for (++glyph;
25283 (glyph < last
25284 && glyph->type == STRETCH_GLYPH
25285 && glyph->voffset == voffset
25286 && glyph->face_id == face_id);
25287 ++glyph)
25288 s->width += glyph->pixel_width;
25290 /* Adjust base line for subscript/superscript text. */
25291 s->ybase += voffset;
25293 /* The case that face->gc == 0 is handled when drawing the glyph
25294 string by calling prepare_face_for_display. */
25295 eassert (s->face);
25296 return glyph - s->row->glyphs[s->area];
25299 static struct font_metrics *
25300 get_per_char_metric (struct font *font, XChar2b *char2b)
25302 static struct font_metrics metrics;
25303 unsigned code;
25305 if (! font)
25306 return NULL;
25307 code = (XCHAR2B_BYTE1 (char2b) << 8) | XCHAR2B_BYTE2 (char2b);
25308 if (code == FONT_INVALID_CODE)
25309 return NULL;
25310 font->driver->text_extents (font, &code, 1, &metrics);
25311 return &metrics;
25314 /* A subroutine that computes "normal" values of ASCENT and DESCENT
25315 for FONT. Values are taken from font-global ones, except for fonts
25316 that claim preposterously large values, but whose glyphs actually
25317 have reasonable dimensions. C is the character to use for metrics
25318 if the font-global values are too large; if C is negative, the
25319 function selects a default character. */
25320 static void
25321 normal_char_ascent_descent (struct font *font, int c, int *ascent, int *descent)
25323 *ascent = FONT_BASE (font);
25324 *descent = FONT_DESCENT (font);
25326 if (FONT_TOO_HIGH (font))
25328 XChar2b char2b;
25330 /* Get metrics of C, defaulting to a reasonably sized ASCII
25331 character. */
25332 if (get_char_glyph_code (c >= 0 ? c : '{', font, &char2b))
25334 struct font_metrics *pcm = get_per_char_metric (font, &char2b);
25336 if (!(pcm->width == 0 && pcm->rbearing == 0 && pcm->lbearing == 0))
25338 /* We add 1 pixel to character dimensions as heuristics
25339 that produces nicer display, e.g. when the face has
25340 the box attribute. */
25341 *ascent = pcm->ascent + 1;
25342 *descent = pcm->descent + 1;
25348 /* A subroutine that computes a reasonable "normal character height"
25349 for fonts that claim preposterously large vertical dimensions, but
25350 whose glyphs are actually reasonably sized. C is the character
25351 whose metrics to use for those fonts, or -1 for default
25352 character. */
25353 static int
25354 normal_char_height (struct font *font, int c)
25356 int ascent, descent;
25358 normal_char_ascent_descent (font, c, &ascent, &descent);
25360 return ascent + descent;
25363 /* EXPORT for RIF:
25364 Set *LEFT and *RIGHT to the left and right overhang of GLYPH on
25365 frame F. Overhangs of glyphs other than type CHAR_GLYPH are
25366 assumed to be zero. */
25368 void
25369 x_get_glyph_overhangs (struct glyph *glyph, struct frame *f, int *left, int *right)
25371 *left = *right = 0;
25373 if (glyph->type == CHAR_GLYPH)
25375 XChar2b char2b;
25376 struct face *face = get_glyph_face_and_encoding (f, glyph, &char2b);
25377 if (face->font)
25379 struct font_metrics *pcm = get_per_char_metric (face->font, &char2b);
25380 if (pcm)
25382 if (pcm->rbearing > pcm->width)
25383 *right = pcm->rbearing - pcm->width;
25384 if (pcm->lbearing < 0)
25385 *left = -pcm->lbearing;
25389 else if (glyph->type == COMPOSITE_GLYPH)
25391 if (! glyph->u.cmp.automatic)
25393 struct composition *cmp = composition_table[glyph->u.cmp.id];
25395 if (cmp->rbearing > cmp->pixel_width)
25396 *right = cmp->rbearing - cmp->pixel_width;
25397 if (cmp->lbearing < 0)
25398 *left = - cmp->lbearing;
25400 else
25402 Lisp_Object gstring = composition_gstring_from_id (glyph->u.cmp.id);
25403 struct font_metrics metrics;
25405 composition_gstring_width (gstring, glyph->slice.cmp.from,
25406 glyph->slice.cmp.to + 1, &metrics);
25407 if (metrics.rbearing > metrics.width)
25408 *right = metrics.rbearing - metrics.width;
25409 if (metrics.lbearing < 0)
25410 *left = - metrics.lbearing;
25416 /* Return the index of the first glyph preceding glyph string S that
25417 is overwritten by S because of S's left overhang. Value is -1
25418 if no glyphs are overwritten. */
25420 static int
25421 left_overwritten (struct glyph_string *s)
25423 int k;
25425 if (s->left_overhang)
25427 int x = 0, i;
25428 struct glyph *glyphs = s->row->glyphs[s->area];
25429 int first = s->first_glyph - glyphs;
25431 for (i = first - 1; i >= 0 && x > -s->left_overhang; --i)
25432 x -= glyphs[i].pixel_width;
25434 k = i + 1;
25436 else
25437 k = -1;
25439 return k;
25443 /* Return the index of the first glyph preceding glyph string S that
25444 is overwriting S because of its right overhang. Value is -1 if no
25445 glyph in front of S overwrites S. */
25447 static int
25448 left_overwriting (struct glyph_string *s)
25450 int i, k, x;
25451 struct glyph *glyphs = s->row->glyphs[s->area];
25452 int first = s->first_glyph - glyphs;
25454 k = -1;
25455 x = 0;
25456 for (i = first - 1; i >= 0; --i)
25458 int left, right;
25459 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
25460 if (x + right > 0)
25461 k = i;
25462 x -= glyphs[i].pixel_width;
25465 return k;
25469 /* Return the index of the last glyph following glyph string S that is
25470 overwritten by S because of S's right overhang. Value is -1 if
25471 no such glyph is found. */
25473 static int
25474 right_overwritten (struct glyph_string *s)
25476 int k = -1;
25478 if (s->right_overhang)
25480 int x = 0, i;
25481 struct glyph *glyphs = s->row->glyphs[s->area];
25482 int first = (s->first_glyph - glyphs
25483 + (s->first_glyph->type == COMPOSITE_GLYPH ? 1 : s->nchars));
25484 int end = s->row->used[s->area];
25486 for (i = first; i < end && s->right_overhang > x; ++i)
25487 x += glyphs[i].pixel_width;
25489 k = i;
25492 return k;
25496 /* Return the index of the last glyph following glyph string S that
25497 overwrites S because of its left overhang. Value is negative
25498 if no such glyph is found. */
25500 static int
25501 right_overwriting (struct glyph_string *s)
25503 int i, k, x;
25504 int end = s->row->used[s->area];
25505 struct glyph *glyphs = s->row->glyphs[s->area];
25506 int first = (s->first_glyph - glyphs
25507 + (s->first_glyph->type == COMPOSITE_GLYPH ? 1 : s->nchars));
25509 k = -1;
25510 x = 0;
25511 for (i = first; i < end; ++i)
25513 int left, right;
25514 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
25515 if (x - left < 0)
25516 k = i;
25517 x += glyphs[i].pixel_width;
25520 return k;
25524 /* Set background width of glyph string S. START is the index of the
25525 first glyph following S. LAST_X is the right-most x-position + 1
25526 in the drawing area. */
25528 static void
25529 set_glyph_string_background_width (struct glyph_string *s, int start, int last_x)
25531 /* If the face of this glyph string has to be drawn to the end of
25532 the drawing area, set S->extends_to_end_of_line_p. */
25534 if (start == s->row->used[s->area]
25535 && ((s->row->fill_line_p
25536 && (s->hl == DRAW_NORMAL_TEXT
25537 || s->hl == DRAW_IMAGE_RAISED
25538 || s->hl == DRAW_IMAGE_SUNKEN))
25539 || s->hl == DRAW_MOUSE_FACE))
25540 s->extends_to_end_of_line_p = true;
25542 /* If S extends its face to the end of the line, set its
25543 background_width to the distance to the right edge of the drawing
25544 area. */
25545 if (s->extends_to_end_of_line_p)
25546 s->background_width = last_x - s->x + 1;
25547 else
25548 s->background_width = s->width;
25552 /* Return glyph string that shares background with glyph string S and
25553 whose `background_width' member has been set. */
25555 static struct glyph_string *
25556 glyph_string_containing_background_width (struct glyph_string *s)
25558 if (s->cmp)
25559 while (s->cmp_from)
25560 s = s->prev;
25562 return s;
25566 /* Compute overhangs and x-positions for glyph string S and its
25567 predecessors, or successors. X is the starting x-position for S.
25568 BACKWARD_P means process predecessors. */
25570 static void
25571 compute_overhangs_and_x (struct glyph_string *s, int x, bool backward_p)
25573 if (backward_p)
25575 while (s)
25577 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
25578 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
25579 if (!s->cmp || s->cmp_to == s->cmp->glyph_len)
25580 x -= s->width;
25581 s->x = x;
25582 s = s->prev;
25585 else
25587 while (s)
25589 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
25590 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
25591 s->x = x;
25592 if (!s->cmp || s->cmp_to == s->cmp->glyph_len)
25593 x += s->width;
25594 s = s->next;
25601 /* The following macros are only called from draw_glyphs below.
25602 They reference the following parameters of that function directly:
25603 `w', `row', `area', and `overlap_p'
25604 as well as the following local variables:
25605 `s', `f', and `hdc' (in W32) */
25607 #ifdef HAVE_NTGUI
25608 /* On W32, silently add local `hdc' variable to argument list of
25609 init_glyph_string. */
25610 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
25611 init_glyph_string (s, hdc, char2b, w, row, area, start, hl)
25612 #else
25613 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
25614 init_glyph_string (s, char2b, w, row, area, start, hl)
25615 #endif
25617 /* Add a glyph string for a stretch glyph to the list of strings
25618 between HEAD and TAIL. START is the index of the stretch glyph in
25619 row area AREA of glyph row ROW. END is the index of the last glyph
25620 in that glyph row area. X is the current output position assigned
25621 to the new glyph string constructed. HL overrides that face of the
25622 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
25623 is the right-most x-position of the drawing area. */
25625 /* SunOS 4 bundled cc, barfed on continuations in the arg lists here
25626 and below -- keep them on one line. */
25627 #define BUILD_STRETCH_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
25628 do \
25630 s = alloca (sizeof *s); \
25631 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
25632 START = fill_stretch_glyph_string (s, START, END); \
25633 append_glyph_string (&HEAD, &TAIL, s); \
25634 s->x = (X); \
25636 while (false)
25639 /* Add a glyph string for an image glyph to the list of strings
25640 between HEAD and TAIL. START is the index of the image glyph in
25641 row area AREA of glyph row ROW. END is the index of the last glyph
25642 in that glyph row area. X is the current output position assigned
25643 to the new glyph string constructed. HL overrides that face of the
25644 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
25645 is the right-most x-position of the drawing area. */
25647 #define BUILD_IMAGE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
25648 do \
25650 s = alloca (sizeof *s); \
25651 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
25652 fill_image_glyph_string (s); \
25653 append_glyph_string (&HEAD, &TAIL, s); \
25654 ++START; \
25655 s->x = (X); \
25657 while (false)
25659 #ifndef HAVE_XWIDGETS
25660 # define BUILD_XWIDGET_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
25661 eassume (false)
25662 #else
25663 # define BUILD_XWIDGET_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
25664 do \
25666 s = alloca (sizeof *s); \
25667 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
25668 fill_xwidget_glyph_string (s); \
25669 append_glyph_string (&(HEAD), &(TAIL), s); \
25670 ++(START); \
25671 s->x = (X); \
25673 while (false)
25674 #endif
25676 /* Add a glyph string for a sequence of character glyphs to the list
25677 of strings between HEAD and TAIL. START is the index of the first
25678 glyph in row area AREA of glyph row ROW that is part of the new
25679 glyph string. END is the index of the last glyph in that glyph row
25680 area. X is the current output position assigned to the new glyph
25681 string constructed. HL overrides that face of the glyph; e.g. it
25682 is DRAW_CURSOR if a cursor has to be drawn. LAST_X is the
25683 right-most x-position of the drawing area. */
25685 #define BUILD_CHAR_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
25686 do \
25688 int face_id; \
25689 XChar2b *char2b; \
25691 face_id = (row)->glyphs[area][START].face_id; \
25693 s = alloca (sizeof *s); \
25694 SAFE_NALLOCA (char2b, 1, (END) - (START)); \
25695 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
25696 append_glyph_string (&HEAD, &TAIL, s); \
25697 s->x = (X); \
25698 START = fill_glyph_string (s, face_id, START, END, overlaps); \
25700 while (false)
25703 /* Add a glyph string for a composite sequence to the list of strings
25704 between HEAD and TAIL. START is the index of the first glyph in
25705 row area AREA of glyph row ROW that is part of the new glyph
25706 string. END is the index of the last glyph in that glyph row area.
25707 X is the current output position assigned to the new glyph string
25708 constructed. HL overrides that face of the glyph; e.g. it is
25709 DRAW_CURSOR if a cursor has to be drawn. LAST_X is the right-most
25710 x-position of the drawing area. */
25712 #define BUILD_COMPOSITE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
25713 do { \
25714 int face_id = (row)->glyphs[area][START].face_id; \
25715 struct face *base_face = FACE_FROM_ID (f, face_id); \
25716 ptrdiff_t cmp_id = (row)->glyphs[area][START].u.cmp.id; \
25717 struct composition *cmp = composition_table[cmp_id]; \
25718 XChar2b *char2b; \
25719 struct glyph_string *first_s = NULL; \
25720 int n; \
25722 SAFE_NALLOCA (char2b, 1, cmp->glyph_len); \
25724 /* Make glyph_strings for each glyph sequence that is drawable by \
25725 the same face, and append them to HEAD/TAIL. */ \
25726 for (n = 0; n < cmp->glyph_len;) \
25728 s = alloca (sizeof *s); \
25729 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
25730 append_glyph_string (&(HEAD), &(TAIL), s); \
25731 s->cmp = cmp; \
25732 s->cmp_from = n; \
25733 s->x = (X); \
25734 if (n == 0) \
25735 first_s = s; \
25736 n = fill_composite_glyph_string (s, base_face, overlaps); \
25739 ++START; \
25740 s = first_s; \
25741 } while (false)
25744 /* Add a glyph string for a glyph-string sequence to the list of strings
25745 between HEAD and TAIL. */
25747 #define BUILD_GSTRING_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
25748 do { \
25749 int face_id; \
25750 XChar2b *char2b; \
25751 Lisp_Object gstring; \
25753 face_id = (row)->glyphs[area][START].face_id; \
25754 gstring = (composition_gstring_from_id \
25755 ((row)->glyphs[area][START].u.cmp.id)); \
25756 s = alloca (sizeof *s); \
25757 SAFE_NALLOCA (char2b, 1, LGSTRING_GLYPH_LEN (gstring)); \
25758 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
25759 append_glyph_string (&(HEAD), &(TAIL), s); \
25760 s->x = (X); \
25761 START = fill_gstring_glyph_string (s, face_id, START, END, overlaps); \
25762 } while (false)
25765 /* Add a glyph string for a sequence of glyphless character's glyphs
25766 to the list of strings between HEAD and TAIL. The meanings of
25767 arguments are the same as those of BUILD_CHAR_GLYPH_STRINGS. */
25769 #define BUILD_GLYPHLESS_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
25770 do \
25772 int face_id; \
25774 face_id = (row)->glyphs[area][START].face_id; \
25776 s = alloca (sizeof *s); \
25777 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
25778 append_glyph_string (&HEAD, &TAIL, s); \
25779 s->x = (X); \
25780 START = fill_glyphless_glyph_string (s, face_id, START, END, \
25781 overlaps); \
25783 while (false)
25786 /* Build a list of glyph strings between HEAD and TAIL for the glyphs
25787 of AREA of glyph row ROW on window W between indices START and END.
25788 HL overrides the face for drawing glyph strings, e.g. it is
25789 DRAW_CURSOR to draw a cursor. X and LAST_X are start and end
25790 x-positions of the drawing area.
25792 This is an ugly monster macro construct because we must use alloca
25793 to allocate glyph strings (because draw_glyphs can be called
25794 asynchronously). */
25796 #define BUILD_GLYPH_STRINGS_1(START, END, HEAD, TAIL, HL, X, LAST_X) \
25797 do \
25799 HEAD = TAIL = NULL; \
25800 while (START < END) \
25802 struct glyph *first_glyph = (row)->glyphs[area] + START; \
25803 switch (first_glyph->type) \
25805 case CHAR_GLYPH: \
25806 BUILD_CHAR_GLYPH_STRINGS (START, END, HEAD, TAIL, \
25807 HL, X, LAST_X); \
25808 break; \
25810 case COMPOSITE_GLYPH: \
25811 if (first_glyph->u.cmp.automatic) \
25812 BUILD_GSTRING_GLYPH_STRING (START, END, HEAD, TAIL, \
25813 HL, X, LAST_X); \
25814 else \
25815 BUILD_COMPOSITE_GLYPH_STRING (START, END, HEAD, TAIL, \
25816 HL, X, LAST_X); \
25817 break; \
25819 case STRETCH_GLYPH: \
25820 BUILD_STRETCH_GLYPH_STRING (START, END, HEAD, TAIL, \
25821 HL, X, LAST_X); \
25822 break; \
25824 case IMAGE_GLYPH: \
25825 BUILD_IMAGE_GLYPH_STRING (START, END, HEAD, TAIL, \
25826 HL, X, LAST_X); \
25827 break;
25829 #define BUILD_GLYPH_STRINGS_XW(START, END, HEAD, TAIL, HL, X, LAST_X) \
25830 case XWIDGET_GLYPH: \
25831 BUILD_XWIDGET_GLYPH_STRING (START, END, HEAD, TAIL, \
25832 HL, X, LAST_X); \
25833 break;
25835 #define BUILD_GLYPH_STRINGS_2(START, END, HEAD, TAIL, HL, X, LAST_X) \
25836 case GLYPHLESS_GLYPH: \
25837 BUILD_GLYPHLESS_GLYPH_STRING (START, END, HEAD, TAIL, \
25838 HL, X, LAST_X); \
25839 break; \
25841 default: \
25842 emacs_abort (); \
25845 if (s) \
25847 set_glyph_string_background_width (s, START, LAST_X); \
25848 (X) += s->width; \
25851 } while (false)
25854 #define BUILD_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
25855 BUILD_GLYPH_STRINGS_1(START, END, HEAD, TAIL, HL, X, LAST_X) \
25856 BUILD_GLYPH_STRINGS_XW(START, END, HEAD, TAIL, HL, X, LAST_X) \
25857 BUILD_GLYPH_STRINGS_2(START, END, HEAD, TAIL, HL, X, LAST_X)
25860 /* Draw glyphs between START and END in AREA of ROW on window W,
25861 starting at x-position X. X is relative to AREA in W. HL is a
25862 face-override with the following meaning:
25864 DRAW_NORMAL_TEXT draw normally
25865 DRAW_CURSOR draw in cursor face
25866 DRAW_MOUSE_FACE draw in mouse face.
25867 DRAW_INVERSE_VIDEO draw in mode line face
25868 DRAW_IMAGE_SUNKEN draw an image with a sunken relief around it
25869 DRAW_IMAGE_RAISED draw an image with a raised relief around it
25871 If OVERLAPS is non-zero, draw only the foreground of characters and
25872 clip to the physical height of ROW. Non-zero value also defines
25873 the overlapping part to be drawn:
25875 OVERLAPS_PRED overlap with preceding rows
25876 OVERLAPS_SUCC overlap with succeeding rows
25877 OVERLAPS_BOTH overlap with both preceding/succeeding rows
25878 OVERLAPS_ERASED_CURSOR overlap with erased cursor area
25880 Value is the x-position reached, relative to AREA of W. */
25882 static int
25883 draw_glyphs (struct window *w, int x, struct glyph_row *row,
25884 enum glyph_row_area area, ptrdiff_t start, ptrdiff_t end,
25885 enum draw_glyphs_face hl, int overlaps)
25887 struct glyph_string *head, *tail;
25888 struct glyph_string *s;
25889 struct glyph_string *clip_head = NULL, *clip_tail = NULL;
25890 int i, j, x_reached, last_x, area_left = 0;
25891 struct frame *f = XFRAME (WINDOW_FRAME (w));
25892 DECLARE_HDC (hdc);
25894 ALLOCATE_HDC (hdc, f);
25896 /* Let's rather be paranoid than getting a SEGV. */
25897 end = min (end, row->used[area]);
25898 start = clip_to_bounds (0, start, end);
25900 /* Translate X to frame coordinates. Set last_x to the right
25901 end of the drawing area. */
25902 if (row->full_width_p)
25904 /* X is relative to the left edge of W, without scroll bars
25905 or fringes. */
25906 area_left = WINDOW_LEFT_EDGE_X (w);
25907 last_x = (WINDOW_LEFT_EDGE_X (w) + WINDOW_PIXEL_WIDTH (w)
25908 - (row->mode_line_p ? WINDOW_RIGHT_DIVIDER_WIDTH (w) : 0));
25910 else
25912 area_left = window_box_left (w, area);
25913 last_x = area_left + window_box_width (w, area);
25915 x += area_left;
25917 /* Build a doubly-linked list of glyph_string structures between
25918 head and tail from what we have to draw. Note that the macro
25919 BUILD_GLYPH_STRINGS will modify its start parameter. That's
25920 the reason we use a separate variable `i'. */
25921 i = start;
25922 USE_SAFE_ALLOCA;
25923 BUILD_GLYPH_STRINGS (i, end, head, tail, hl, x, last_x);
25924 if (tail)
25926 s = glyph_string_containing_background_width (tail);
25927 x_reached = s->x + s->background_width;
25929 else
25930 x_reached = x;
25932 /* If there are any glyphs with lbearing < 0 or rbearing > width in
25933 the row, redraw some glyphs in front or following the glyph
25934 strings built above. */
25935 if (head && !overlaps && row->contains_overlapping_glyphs_p)
25937 struct glyph_string *h, *t;
25938 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
25939 int mouse_beg_col UNINIT, mouse_end_col UNINIT;
25940 bool check_mouse_face = false;
25941 int dummy_x = 0;
25943 /* If mouse highlighting is on, we may need to draw adjacent
25944 glyphs using mouse-face highlighting. */
25945 if (area == TEXT_AREA && row->mouse_face_p
25946 && hlinfo->mouse_face_beg_row >= 0
25947 && hlinfo->mouse_face_end_row >= 0)
25949 ptrdiff_t row_vpos = MATRIX_ROW_VPOS (row, w->current_matrix);
25951 if (row_vpos >= hlinfo->mouse_face_beg_row
25952 && row_vpos <= hlinfo->mouse_face_end_row)
25954 check_mouse_face = true;
25955 mouse_beg_col = (row_vpos == hlinfo->mouse_face_beg_row)
25956 ? hlinfo->mouse_face_beg_col : 0;
25957 mouse_end_col = (row_vpos == hlinfo->mouse_face_end_row)
25958 ? hlinfo->mouse_face_end_col
25959 : row->used[TEXT_AREA];
25963 /* Compute overhangs for all glyph strings. */
25964 if (FRAME_RIF (f)->compute_glyph_string_overhangs)
25965 for (s = head; s; s = s->next)
25966 FRAME_RIF (f)->compute_glyph_string_overhangs (s);
25968 /* Prepend glyph strings for glyphs in front of the first glyph
25969 string that are overwritten because of the first glyph
25970 string's left overhang. The background of all strings
25971 prepended must be drawn because the first glyph string
25972 draws over it. */
25973 i = left_overwritten (head);
25974 if (i >= 0)
25976 enum draw_glyphs_face overlap_hl;
25978 /* If this row contains mouse highlighting, attempt to draw
25979 the overlapped glyphs with the correct highlight. This
25980 code fails if the overlap encompasses more than one glyph
25981 and mouse-highlight spans only some of these glyphs.
25982 However, making it work perfectly involves a lot more
25983 code, and I don't know if the pathological case occurs in
25984 practice, so we'll stick to this for now. --- cyd */
25985 if (check_mouse_face
25986 && mouse_beg_col < start && mouse_end_col > i)
25987 overlap_hl = DRAW_MOUSE_FACE;
25988 else
25989 overlap_hl = DRAW_NORMAL_TEXT;
25991 if (hl != overlap_hl)
25992 clip_head = head;
25993 j = i;
25994 BUILD_GLYPH_STRINGS (j, start, h, t,
25995 overlap_hl, dummy_x, last_x);
25996 start = i;
25997 compute_overhangs_and_x (t, head->x, true);
25998 prepend_glyph_string_lists (&head, &tail, h, t);
25999 if (clip_head == NULL)
26000 clip_head = head;
26003 /* Prepend glyph strings for glyphs in front of the first glyph
26004 string that overwrite that glyph string because of their
26005 right overhang. For these strings, only the foreground must
26006 be drawn, because it draws over the glyph string at `head'.
26007 The background must not be drawn because this would overwrite
26008 right overhangs of preceding glyphs for which no glyph
26009 strings exist. */
26010 i = left_overwriting (head);
26011 if (i >= 0)
26013 enum draw_glyphs_face overlap_hl;
26015 if (check_mouse_face
26016 && mouse_beg_col < start && mouse_end_col > i)
26017 overlap_hl = DRAW_MOUSE_FACE;
26018 else
26019 overlap_hl = DRAW_NORMAL_TEXT;
26021 if (hl == overlap_hl || clip_head == NULL)
26022 clip_head = head;
26023 BUILD_GLYPH_STRINGS (i, start, h, t,
26024 overlap_hl, dummy_x, last_x);
26025 for (s = h; s; s = s->next)
26026 s->background_filled_p = true;
26027 compute_overhangs_and_x (t, head->x, true);
26028 prepend_glyph_string_lists (&head, &tail, h, t);
26031 /* Append glyphs strings for glyphs following the last glyph
26032 string tail that are overwritten by tail. The background of
26033 these strings has to be drawn because tail's foreground draws
26034 over it. */
26035 i = right_overwritten (tail);
26036 if (i >= 0)
26038 enum draw_glyphs_face overlap_hl;
26040 if (check_mouse_face
26041 && mouse_beg_col < i && mouse_end_col > end)
26042 overlap_hl = DRAW_MOUSE_FACE;
26043 else
26044 overlap_hl = DRAW_NORMAL_TEXT;
26046 if (hl != overlap_hl)
26047 clip_tail = tail;
26048 BUILD_GLYPH_STRINGS (end, i, h, t,
26049 overlap_hl, x, last_x);
26050 /* Because BUILD_GLYPH_STRINGS updates the first argument,
26051 we don't have `end = i;' here. */
26052 compute_overhangs_and_x (h, tail->x + tail->width, false);
26053 append_glyph_string_lists (&head, &tail, h, t);
26054 if (clip_tail == NULL)
26055 clip_tail = tail;
26058 /* Append glyph strings for glyphs following the last glyph
26059 string tail that overwrite tail. The foreground of such
26060 glyphs has to be drawn because it writes into the background
26061 of tail. The background must not be drawn because it could
26062 paint over the foreground of following glyphs. */
26063 i = right_overwriting (tail);
26064 if (i >= 0)
26066 enum draw_glyphs_face overlap_hl;
26067 if (check_mouse_face
26068 && mouse_beg_col < i && mouse_end_col > end)
26069 overlap_hl = DRAW_MOUSE_FACE;
26070 else
26071 overlap_hl = DRAW_NORMAL_TEXT;
26073 if (hl == overlap_hl || clip_tail == NULL)
26074 clip_tail = tail;
26075 i++; /* We must include the Ith glyph. */
26076 BUILD_GLYPH_STRINGS (end, i, h, t,
26077 overlap_hl, x, last_x);
26078 for (s = h; s; s = s->next)
26079 s->background_filled_p = true;
26080 compute_overhangs_and_x (h, tail->x + tail->width, false);
26081 append_glyph_string_lists (&head, &tail, h, t);
26083 tail = glyph_string_containing_background_width (tail);
26084 if (clip_tail)
26085 clip_tail = glyph_string_containing_background_width (clip_tail);
26086 if (clip_head || clip_tail)
26087 for (s = head; s; s = s->next)
26089 s->clip_head = clip_head;
26090 s->clip_tail = clip_tail;
26094 /* Draw all strings. */
26095 for (s = head; s; s = s->next)
26096 FRAME_RIF (f)->draw_glyph_string (s);
26098 #ifndef HAVE_NS
26099 /* When focus a sole frame and move horizontally, this clears on_p
26100 causing a failure to erase prev cursor position. */
26101 if (area == TEXT_AREA
26102 && !row->full_width_p
26103 /* When drawing overlapping rows, only the glyph strings'
26104 foreground is drawn, which doesn't erase a cursor
26105 completely. */
26106 && !overlaps)
26108 int x0 = clip_head ? clip_head->x : (head ? head->x : x);
26109 int x1 = (clip_tail ? clip_tail->x + clip_tail->background_width
26110 : (tail ? tail->x + tail->background_width : x));
26111 x0 -= area_left;
26112 x1 -= area_left;
26114 notice_overwritten_cursor (w, TEXT_AREA, x0, x1,
26115 row->y, MATRIX_ROW_BOTTOM_Y (row));
26117 #endif
26119 /* Value is the x-position up to which drawn, relative to AREA of W.
26120 This doesn't include parts drawn because of overhangs. */
26121 if (row->full_width_p)
26122 x_reached = FRAME_TO_WINDOW_PIXEL_X (w, x_reached);
26123 else
26124 x_reached -= area_left;
26126 RELEASE_HDC (hdc, f);
26128 SAFE_FREE ();
26129 return x_reached;
26132 /* Find the first glyph in the run of underlined glyphs preceding the
26133 beginning of glyph string S, and return its font (which could be
26134 NULL). This is needed because that font determines the underline
26135 position and thickness for the entire run of the underlined glyphs.
26136 This function is called from the draw_glyph_string method of GUI
26137 frame's redisplay interface (RIF) when it needs to draw in an
26138 underlined face. */
26139 struct font *
26140 font_for_underline_metrics (struct glyph_string *s)
26142 struct glyph *g0 = s->row->glyphs[s->area], *g;
26144 for (g = s->first_glyph - 1; g >= g0; g--)
26146 struct face *prev_face = FACE_FROM_ID (s->f, g->face_id);
26147 if (!(prev_face && prev_face->underline_p))
26148 break;
26151 /* If preceding glyphs are not underlined, use the font of S. */
26152 if (g == s->first_glyph - 1)
26153 return s->font;
26154 else
26156 /* Otherwise use the font of the last glyph we saw in the above
26157 loop whose face had the underline_p flag set. */
26158 return FACE_FROM_ID (s->f, g[1].face_id)->font;
26162 /* Expand row matrix if too narrow. Don't expand if area
26163 is not present. */
26165 #define IT_EXPAND_MATRIX_WIDTH(it, area) \
26167 if (!it->f->fonts_changed \
26168 && (it->glyph_row->glyphs[area] \
26169 < it->glyph_row->glyphs[area + 1])) \
26171 it->w->ncols_scale_factor++; \
26172 it->f->fonts_changed = true; \
26176 /* Store one glyph for IT->char_to_display in IT->glyph_row.
26177 Called from x_produce_glyphs when IT->glyph_row is non-null. */
26179 static void
26180 append_glyph (struct it *it)
26182 struct glyph *glyph;
26183 enum glyph_row_area area = it->area;
26185 eassert (it->glyph_row);
26186 eassert (it->char_to_display != '\n' && it->char_to_display != '\t');
26188 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
26189 if (glyph < it->glyph_row->glyphs[area + 1])
26191 /* If the glyph row is reversed, we need to prepend the glyph
26192 rather than append it. */
26193 if (it->glyph_row->reversed_p && area == TEXT_AREA)
26195 struct glyph *g;
26197 /* Make room for the additional glyph. */
26198 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
26199 g[1] = *g;
26200 glyph = it->glyph_row->glyphs[area];
26202 glyph->charpos = CHARPOS (it->position);
26203 glyph->object = it->object;
26204 if (it->pixel_width > 0)
26206 eassert (it->pixel_width <= SHRT_MAX);
26207 glyph->pixel_width = it->pixel_width;
26208 glyph->padding_p = false;
26210 else
26212 /* Assure at least 1-pixel width. Otherwise, cursor can't
26213 be displayed correctly. */
26214 glyph->pixel_width = 1;
26215 glyph->padding_p = true;
26217 glyph->ascent = it->ascent;
26218 glyph->descent = it->descent;
26219 glyph->voffset = it->voffset;
26220 glyph->type = CHAR_GLYPH;
26221 glyph->avoid_cursor_p = it->avoid_cursor_p;
26222 glyph->multibyte_p = it->multibyte_p;
26223 if (it->glyph_row->reversed_p && area == TEXT_AREA)
26225 /* In R2L rows, the left and the right box edges need to be
26226 drawn in reverse direction. */
26227 glyph->right_box_line_p = it->start_of_box_run_p;
26228 glyph->left_box_line_p = it->end_of_box_run_p;
26230 else
26232 glyph->left_box_line_p = it->start_of_box_run_p;
26233 glyph->right_box_line_p = it->end_of_box_run_p;
26235 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
26236 || it->phys_descent > it->descent);
26237 glyph->glyph_not_available_p = it->glyph_not_available_p;
26238 glyph->face_id = it->face_id;
26239 glyph->u.ch = it->char_to_display;
26240 glyph->slice.img = null_glyph_slice;
26241 glyph->font_type = FONT_TYPE_UNKNOWN;
26242 if (it->bidi_p)
26244 glyph->resolved_level = it->bidi_it.resolved_level;
26245 eassert ((it->bidi_it.type & 7) == it->bidi_it.type);
26246 glyph->bidi_type = it->bidi_it.type;
26248 else
26250 glyph->resolved_level = 0;
26251 glyph->bidi_type = UNKNOWN_BT;
26253 ++it->glyph_row->used[area];
26255 else
26256 IT_EXPAND_MATRIX_WIDTH (it, area);
26259 /* Store one glyph for the composition IT->cmp_it.id in
26260 IT->glyph_row. Called from x_produce_glyphs when IT->glyph_row is
26261 non-null. */
26263 static void
26264 append_composite_glyph (struct it *it)
26266 struct glyph *glyph;
26267 enum glyph_row_area area = it->area;
26269 eassert (it->glyph_row);
26271 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
26272 if (glyph < it->glyph_row->glyphs[area + 1])
26274 /* If the glyph row is reversed, we need to prepend the glyph
26275 rather than append it. */
26276 if (it->glyph_row->reversed_p && it->area == TEXT_AREA)
26278 struct glyph *g;
26280 /* Make room for the new glyph. */
26281 for (g = glyph - 1; g >= it->glyph_row->glyphs[it->area]; g--)
26282 g[1] = *g;
26283 glyph = it->glyph_row->glyphs[it->area];
26285 glyph->charpos = it->cmp_it.charpos;
26286 glyph->object = it->object;
26287 eassert (it->pixel_width <= SHRT_MAX);
26288 glyph->pixel_width = it->pixel_width;
26289 glyph->ascent = it->ascent;
26290 glyph->descent = it->descent;
26291 glyph->voffset = it->voffset;
26292 glyph->type = COMPOSITE_GLYPH;
26293 if (it->cmp_it.ch < 0)
26295 glyph->u.cmp.automatic = false;
26296 glyph->u.cmp.id = it->cmp_it.id;
26297 glyph->slice.cmp.from = glyph->slice.cmp.to = 0;
26299 else
26301 glyph->u.cmp.automatic = true;
26302 glyph->u.cmp.id = it->cmp_it.id;
26303 glyph->slice.cmp.from = it->cmp_it.from;
26304 glyph->slice.cmp.to = it->cmp_it.to - 1;
26306 glyph->avoid_cursor_p = it->avoid_cursor_p;
26307 glyph->multibyte_p = it->multibyte_p;
26308 if (it->glyph_row->reversed_p && area == TEXT_AREA)
26310 /* In R2L rows, the left and the right box edges need to be
26311 drawn in reverse direction. */
26312 glyph->right_box_line_p = it->start_of_box_run_p;
26313 glyph->left_box_line_p = it->end_of_box_run_p;
26315 else
26317 glyph->left_box_line_p = it->start_of_box_run_p;
26318 glyph->right_box_line_p = it->end_of_box_run_p;
26320 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
26321 || it->phys_descent > it->descent);
26322 glyph->padding_p = false;
26323 glyph->glyph_not_available_p = false;
26324 glyph->face_id = it->face_id;
26325 glyph->font_type = FONT_TYPE_UNKNOWN;
26326 if (it->bidi_p)
26328 glyph->resolved_level = it->bidi_it.resolved_level;
26329 eassert ((it->bidi_it.type & 7) == it->bidi_it.type);
26330 glyph->bidi_type = it->bidi_it.type;
26332 ++it->glyph_row->used[area];
26334 else
26335 IT_EXPAND_MATRIX_WIDTH (it, area);
26339 /* Change IT->ascent and IT->height according to the setting of
26340 IT->voffset. */
26342 static void
26343 take_vertical_position_into_account (struct it *it)
26345 if (it->voffset)
26347 if (it->voffset < 0)
26348 /* Increase the ascent so that we can display the text higher
26349 in the line. */
26350 it->ascent -= it->voffset;
26351 else
26352 /* Increase the descent so that we can display the text lower
26353 in the line. */
26354 it->descent += it->voffset;
26359 /* Produce glyphs/get display metrics for the image IT is loaded with.
26360 See the description of struct display_iterator in dispextern.h for
26361 an overview of struct display_iterator. */
26363 static void
26364 produce_image_glyph (struct it *it)
26366 struct image *img;
26367 struct face *face;
26368 int glyph_ascent, crop;
26369 struct glyph_slice slice;
26371 eassert (it->what == IT_IMAGE);
26373 face = FACE_FROM_ID (it->f, it->face_id);
26374 /* Make sure X resources of the face is loaded. */
26375 prepare_face_for_display (it->f, face);
26377 if (it->image_id < 0)
26379 /* Fringe bitmap. */
26380 it->ascent = it->phys_ascent = 0;
26381 it->descent = it->phys_descent = 0;
26382 it->pixel_width = 0;
26383 it->nglyphs = 0;
26384 return;
26387 img = IMAGE_FROM_ID (it->f, it->image_id);
26388 /* Make sure X resources of the image is loaded. */
26389 prepare_image_for_display (it->f, img);
26391 slice.x = slice.y = 0;
26392 slice.width = img->width;
26393 slice.height = img->height;
26395 if (INTEGERP (it->slice.x))
26396 slice.x = XINT (it->slice.x);
26397 else if (FLOATP (it->slice.x))
26398 slice.x = XFLOAT_DATA (it->slice.x) * img->width;
26400 if (INTEGERP (it->slice.y))
26401 slice.y = XINT (it->slice.y);
26402 else if (FLOATP (it->slice.y))
26403 slice.y = XFLOAT_DATA (it->slice.y) * img->height;
26405 if (INTEGERP (it->slice.width))
26406 slice.width = XINT (it->slice.width);
26407 else if (FLOATP (it->slice.width))
26408 slice.width = XFLOAT_DATA (it->slice.width) * img->width;
26410 if (INTEGERP (it->slice.height))
26411 slice.height = XINT (it->slice.height);
26412 else if (FLOATP (it->slice.height))
26413 slice.height = XFLOAT_DATA (it->slice.height) * img->height;
26415 if (slice.x >= img->width)
26416 slice.x = img->width;
26417 if (slice.y >= img->height)
26418 slice.y = img->height;
26419 if (slice.x + slice.width >= img->width)
26420 slice.width = img->width - slice.x;
26421 if (slice.y + slice.height > img->height)
26422 slice.height = img->height - slice.y;
26424 if (slice.width == 0 || slice.height == 0)
26425 return;
26427 it->ascent = it->phys_ascent = glyph_ascent = image_ascent (img, face, &slice);
26429 it->descent = slice.height - glyph_ascent;
26430 if (slice.y == 0)
26431 it->descent += img->vmargin;
26432 if (slice.y + slice.height == img->height)
26433 it->descent += img->vmargin;
26434 it->phys_descent = it->descent;
26436 it->pixel_width = slice.width;
26437 if (slice.x == 0)
26438 it->pixel_width += img->hmargin;
26439 if (slice.x + slice.width == img->width)
26440 it->pixel_width += img->hmargin;
26442 /* It's quite possible for images to have an ascent greater than
26443 their height, so don't get confused in that case. */
26444 if (it->descent < 0)
26445 it->descent = 0;
26447 it->nglyphs = 1;
26449 if (face->box != FACE_NO_BOX)
26451 if (face->box_line_width > 0)
26453 if (slice.y == 0)
26454 it->ascent += face->box_line_width;
26455 if (slice.y + slice.height == img->height)
26456 it->descent += face->box_line_width;
26459 if (it->start_of_box_run_p && slice.x == 0)
26460 it->pixel_width += eabs (face->box_line_width);
26461 if (it->end_of_box_run_p && slice.x + slice.width == img->width)
26462 it->pixel_width += eabs (face->box_line_width);
26465 take_vertical_position_into_account (it);
26467 /* Automatically crop wide image glyphs at right edge so we can
26468 draw the cursor on same display row. */
26469 if ((crop = it->pixel_width - (it->last_visible_x - it->current_x), crop > 0)
26470 && (it->hpos == 0 || it->pixel_width > it->last_visible_x / 4))
26472 it->pixel_width -= crop;
26473 slice.width -= crop;
26476 if (it->glyph_row)
26478 struct glyph *glyph;
26479 enum glyph_row_area area = it->area;
26481 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
26482 if (it->glyph_row->reversed_p)
26484 struct glyph *g;
26486 /* Make room for the new glyph. */
26487 for (g = glyph - 1; g >= it->glyph_row->glyphs[it->area]; g--)
26488 g[1] = *g;
26489 glyph = it->glyph_row->glyphs[it->area];
26491 if (glyph < it->glyph_row->glyphs[area + 1])
26493 glyph->charpos = CHARPOS (it->position);
26494 glyph->object = it->object;
26495 glyph->pixel_width = clip_to_bounds (-1, it->pixel_width, SHRT_MAX);
26496 glyph->ascent = glyph_ascent;
26497 glyph->descent = it->descent;
26498 glyph->voffset = it->voffset;
26499 glyph->type = IMAGE_GLYPH;
26500 glyph->avoid_cursor_p = it->avoid_cursor_p;
26501 glyph->multibyte_p = it->multibyte_p;
26502 if (it->glyph_row->reversed_p && area == TEXT_AREA)
26504 /* In R2L rows, the left and the right box edges need to be
26505 drawn in reverse direction. */
26506 glyph->right_box_line_p = it->start_of_box_run_p;
26507 glyph->left_box_line_p = it->end_of_box_run_p;
26509 else
26511 glyph->left_box_line_p = it->start_of_box_run_p;
26512 glyph->right_box_line_p = it->end_of_box_run_p;
26514 glyph->overlaps_vertically_p = false;
26515 glyph->padding_p = false;
26516 glyph->glyph_not_available_p = false;
26517 glyph->face_id = it->face_id;
26518 glyph->u.img_id = img->id;
26519 glyph->slice.img = slice;
26520 glyph->font_type = FONT_TYPE_UNKNOWN;
26521 if (it->bidi_p)
26523 glyph->resolved_level = it->bidi_it.resolved_level;
26524 eassert ((it->bidi_it.type & 7) == it->bidi_it.type);
26525 glyph->bidi_type = it->bidi_it.type;
26527 ++it->glyph_row->used[area];
26529 else
26530 IT_EXPAND_MATRIX_WIDTH (it, area);
26534 static void
26535 produce_xwidget_glyph (struct it *it)
26537 #ifdef HAVE_XWIDGETS
26538 struct xwidget *xw;
26539 int glyph_ascent, crop;
26540 eassert (it->what == IT_XWIDGET);
26542 struct face *face = FACE_FROM_ID (it->f, it->face_id);
26543 /* Make sure X resources of the face is loaded. */
26544 prepare_face_for_display (it->f, face);
26546 xw = it->xwidget;
26547 it->ascent = it->phys_ascent = glyph_ascent = xw->height/2;
26548 it->descent = xw->height/2;
26549 it->phys_descent = it->descent;
26550 it->pixel_width = xw->width;
26551 /* It's quite possible for images to have an ascent greater than
26552 their height, so don't get confused in that case. */
26553 if (it->descent < 0)
26554 it->descent = 0;
26556 it->nglyphs = 1;
26558 if (face->box != FACE_NO_BOX)
26560 if (face->box_line_width > 0)
26562 it->ascent += face->box_line_width;
26563 it->descent += face->box_line_width;
26566 if (it->start_of_box_run_p)
26567 it->pixel_width += eabs (face->box_line_width);
26568 it->pixel_width += eabs (face->box_line_width);
26571 take_vertical_position_into_account (it);
26573 /* Automatically crop wide image glyphs at right edge so we can
26574 draw the cursor on same display row. */
26575 crop = it->pixel_width - (it->last_visible_x - it->current_x);
26576 if (crop > 0 && (it->hpos == 0 || it->pixel_width > it->last_visible_x / 4))
26577 it->pixel_width -= crop;
26579 if (it->glyph_row)
26581 enum glyph_row_area area = it->area;
26582 struct glyph *glyph
26583 = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
26585 if (it->glyph_row->reversed_p)
26587 struct glyph *g;
26589 /* Make room for the new glyph. */
26590 for (g = glyph - 1; g >= it->glyph_row->glyphs[it->area]; g--)
26591 g[1] = *g;
26592 glyph = it->glyph_row->glyphs[it->area];
26594 if (glyph < it->glyph_row->glyphs[area + 1])
26596 glyph->charpos = CHARPOS (it->position);
26597 glyph->object = it->object;
26598 glyph->pixel_width = clip_to_bounds (-1, it->pixel_width, SHRT_MAX);
26599 glyph->ascent = glyph_ascent;
26600 glyph->descent = it->descent;
26601 glyph->voffset = it->voffset;
26602 glyph->type = XWIDGET_GLYPH;
26603 glyph->avoid_cursor_p = it->avoid_cursor_p;
26604 glyph->multibyte_p = it->multibyte_p;
26605 if (it->glyph_row->reversed_p && area == TEXT_AREA)
26607 /* In R2L rows, the left and the right box edges need to be
26608 drawn in reverse direction. */
26609 glyph->right_box_line_p = it->start_of_box_run_p;
26610 glyph->left_box_line_p = it->end_of_box_run_p;
26612 else
26614 glyph->left_box_line_p = it->start_of_box_run_p;
26615 glyph->right_box_line_p = it->end_of_box_run_p;
26617 glyph->overlaps_vertically_p = 0;
26618 glyph->padding_p = 0;
26619 glyph->glyph_not_available_p = 0;
26620 glyph->face_id = it->face_id;
26621 glyph->u.xwidget = it->xwidget;
26622 glyph->font_type = FONT_TYPE_UNKNOWN;
26623 if (it->bidi_p)
26625 glyph->resolved_level = it->bidi_it.resolved_level;
26626 eassert ((it->bidi_it.type & 7) == it->bidi_it.type);
26627 glyph->bidi_type = it->bidi_it.type;
26629 ++it->glyph_row->used[area];
26631 else
26632 IT_EXPAND_MATRIX_WIDTH (it, area);
26634 #endif
26637 /* Append a stretch glyph to IT->glyph_row. OBJECT is the source
26638 of the glyph, WIDTH and HEIGHT are the width and height of the
26639 stretch. ASCENT is the ascent of the glyph (0 <= ASCENT <= HEIGHT). */
26641 static void
26642 append_stretch_glyph (struct it *it, Lisp_Object object,
26643 int width, int height, int ascent)
26645 struct glyph *glyph;
26646 enum glyph_row_area area = it->area;
26648 eassert (ascent >= 0 && ascent <= height);
26650 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
26651 if (glyph < it->glyph_row->glyphs[area + 1])
26653 /* If the glyph row is reversed, we need to prepend the glyph
26654 rather than append it. */
26655 if (it->glyph_row->reversed_p && area == TEXT_AREA)
26657 struct glyph *g;
26659 /* Make room for the additional glyph. */
26660 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
26661 g[1] = *g;
26662 glyph = it->glyph_row->glyphs[area];
26664 /* Decrease the width of the first glyph of the row that
26665 begins before first_visible_x (e.g., due to hscroll).
26666 This is so the overall width of the row becomes smaller
26667 by the scroll amount, and the stretch glyph appended by
26668 extend_face_to_end_of_line will be wider, to shift the
26669 row glyphs to the right. (In L2R rows, the corresponding
26670 left-shift effect is accomplished by setting row->x to a
26671 negative value, which won't work with R2L rows.)
26673 This must leave us with a positive value of WIDTH, since
26674 otherwise the call to move_it_in_display_line_to at the
26675 beginning of display_line would have got past the entire
26676 first glyph, and then it->current_x would have been
26677 greater or equal to it->first_visible_x. */
26678 if (it->current_x < it->first_visible_x)
26679 width -= it->first_visible_x - it->current_x;
26680 eassert (width > 0);
26682 glyph->charpos = CHARPOS (it->position);
26683 glyph->object = object;
26684 /* FIXME: It would be better to use TYPE_MAX here, but
26685 __typeof__ is not portable enough... */
26686 glyph->pixel_width = clip_to_bounds (-1, width, SHRT_MAX);
26687 glyph->ascent = ascent;
26688 glyph->descent = height - ascent;
26689 glyph->voffset = it->voffset;
26690 glyph->type = STRETCH_GLYPH;
26691 glyph->avoid_cursor_p = it->avoid_cursor_p;
26692 glyph->multibyte_p = it->multibyte_p;
26693 if (it->glyph_row->reversed_p && area == TEXT_AREA)
26695 /* In R2L rows, the left and the right box edges need to be
26696 drawn in reverse direction. */
26697 glyph->right_box_line_p = it->start_of_box_run_p;
26698 glyph->left_box_line_p = it->end_of_box_run_p;
26700 else
26702 glyph->left_box_line_p = it->start_of_box_run_p;
26703 glyph->right_box_line_p = it->end_of_box_run_p;
26705 glyph->overlaps_vertically_p = false;
26706 glyph->padding_p = false;
26707 glyph->glyph_not_available_p = false;
26708 glyph->face_id = it->face_id;
26709 glyph->u.stretch.ascent = ascent;
26710 glyph->u.stretch.height = height;
26711 glyph->slice.img = null_glyph_slice;
26712 glyph->font_type = FONT_TYPE_UNKNOWN;
26713 if (it->bidi_p)
26715 glyph->resolved_level = it->bidi_it.resolved_level;
26716 eassert ((it->bidi_it.type & 7) == it->bidi_it.type);
26717 glyph->bidi_type = it->bidi_it.type;
26719 else
26721 glyph->resolved_level = 0;
26722 glyph->bidi_type = UNKNOWN_BT;
26724 ++it->glyph_row->used[area];
26726 else
26727 IT_EXPAND_MATRIX_WIDTH (it, area);
26730 #endif /* HAVE_WINDOW_SYSTEM */
26732 /* Produce a stretch glyph for iterator IT. IT->object is the value
26733 of the glyph property displayed. The value must be a list
26734 `(space KEYWORD VALUE ...)' with the following KEYWORD/VALUE pairs
26735 being recognized:
26737 1. `:width WIDTH' specifies that the space should be WIDTH *
26738 canonical char width wide. WIDTH may be an integer or floating
26739 point number.
26741 2. `:relative-width FACTOR' specifies that the width of the stretch
26742 should be computed from the width of the first character having the
26743 `glyph' property, and should be FACTOR times that width.
26745 3. `:align-to HPOS' specifies that the space should be wide enough
26746 to reach HPOS, a value in canonical character units.
26748 Exactly one of the above pairs must be present.
26750 4. `:height HEIGHT' specifies that the height of the stretch produced
26751 should be HEIGHT, measured in canonical character units.
26753 5. `:relative-height FACTOR' specifies that the height of the
26754 stretch should be FACTOR times the height of the characters having
26755 the glyph property.
26757 Either none or exactly one of 4 or 5 must be present.
26759 6. `:ascent ASCENT' specifies that ASCENT percent of the height
26760 of the stretch should be used for the ascent of the stretch.
26761 ASCENT must be in the range 0 <= ASCENT <= 100. */
26763 void
26764 produce_stretch_glyph (struct it *it)
26766 /* (space :width WIDTH :height HEIGHT ...) */
26767 Lisp_Object prop, plist;
26768 int width = 0, height = 0, align_to = -1;
26769 bool zero_width_ok_p = false;
26770 double tem;
26771 struct font *font = NULL;
26773 #ifdef HAVE_WINDOW_SYSTEM
26774 int ascent = 0;
26775 bool zero_height_ok_p = false;
26777 if (FRAME_WINDOW_P (it->f))
26779 struct face *face = FACE_FROM_ID (it->f, it->face_id);
26780 font = face->font ? face->font : FRAME_FONT (it->f);
26781 prepare_face_for_display (it->f, face);
26783 #endif
26785 /* List should start with `space'. */
26786 eassert (CONSP (it->object) && EQ (XCAR (it->object), Qspace));
26787 plist = XCDR (it->object);
26789 /* Compute the width of the stretch. */
26790 if ((prop = Fplist_get (plist, QCwidth), !NILP (prop))
26791 && calc_pixel_width_or_height (&tem, it, prop, font, true, 0))
26793 /* Absolute width `:width WIDTH' specified and valid. */
26794 zero_width_ok_p = true;
26795 width = (int)tem;
26797 else if (prop = Fplist_get (plist, QCrelative_width), NUMVAL (prop) > 0)
26799 /* Relative width `:relative-width FACTOR' specified and valid.
26800 Compute the width of the characters having the `glyph'
26801 property. */
26802 struct it it2;
26803 unsigned char *p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
26805 it2 = *it;
26806 if (it->multibyte_p)
26807 it2.c = it2.char_to_display = STRING_CHAR_AND_LENGTH (p, it2.len);
26808 else
26810 it2.c = it2.char_to_display = *p, it2.len = 1;
26811 if (! ASCII_CHAR_P (it2.c))
26812 it2.char_to_display = BYTE8_TO_CHAR (it2.c);
26815 it2.glyph_row = NULL;
26816 it2.what = IT_CHARACTER;
26817 PRODUCE_GLYPHS (&it2);
26818 width = NUMVAL (prop) * it2.pixel_width;
26820 else if ((prop = Fplist_get (plist, QCalign_to), !NILP (prop))
26821 && calc_pixel_width_or_height (&tem, it, prop, font, true,
26822 &align_to))
26824 if (it->glyph_row == NULL || !it->glyph_row->mode_line_p)
26825 align_to = (align_to < 0
26827 : align_to - window_box_left_offset (it->w, TEXT_AREA));
26828 else if (align_to < 0)
26829 align_to = window_box_left_offset (it->w, TEXT_AREA);
26830 width = max (0, (int)tem + align_to - it->current_x);
26831 zero_width_ok_p = true;
26833 else
26834 /* Nothing specified -> width defaults to canonical char width. */
26835 width = FRAME_COLUMN_WIDTH (it->f);
26837 if (width <= 0 && (width < 0 || !zero_width_ok_p))
26838 width = 1;
26840 #ifdef HAVE_WINDOW_SYSTEM
26841 /* Compute height. */
26842 if (FRAME_WINDOW_P (it->f))
26844 int default_height = normal_char_height (font, ' ');
26846 if ((prop = Fplist_get (plist, QCheight), !NILP (prop))
26847 && calc_pixel_width_or_height (&tem, it, prop, font, false, 0))
26849 height = (int)tem;
26850 zero_height_ok_p = true;
26852 else if (prop = Fplist_get (plist, QCrelative_height),
26853 NUMVAL (prop) > 0)
26854 height = default_height * NUMVAL (prop);
26855 else
26856 height = default_height;
26858 if (height <= 0 && (height < 0 || !zero_height_ok_p))
26859 height = 1;
26861 /* Compute percentage of height used for ascent. If
26862 `:ascent ASCENT' is present and valid, use that. Otherwise,
26863 derive the ascent from the font in use. */
26864 if (prop = Fplist_get (plist, QCascent),
26865 NUMVAL (prop) > 0 && NUMVAL (prop) <= 100)
26866 ascent = height * NUMVAL (prop) / 100.0;
26867 else if (!NILP (prop)
26868 && calc_pixel_width_or_height (&tem, it, prop, font, false, 0))
26869 ascent = min (max (0, (int)tem), height);
26870 else
26871 ascent = (height * FONT_BASE (font)) / FONT_HEIGHT (font);
26873 else
26874 #endif /* HAVE_WINDOW_SYSTEM */
26875 height = 1;
26877 if (width > 0 && it->line_wrap != TRUNCATE
26878 && it->current_x + width > it->last_visible_x)
26880 width = it->last_visible_x - it->current_x;
26881 #ifdef HAVE_WINDOW_SYSTEM
26882 /* Subtract one more pixel from the stretch width, but only on
26883 GUI frames, since on a TTY each glyph is one "pixel" wide. */
26884 width -= FRAME_WINDOW_P (it->f);
26885 #endif
26888 if (width > 0 && height > 0 && it->glyph_row)
26890 Lisp_Object o_object = it->object;
26891 Lisp_Object object = it->stack[it->sp - 1].string;
26892 int n = width;
26894 if (!STRINGP (object))
26895 object = it->w->contents;
26896 #ifdef HAVE_WINDOW_SYSTEM
26897 if (FRAME_WINDOW_P (it->f))
26898 append_stretch_glyph (it, object, width, height, ascent);
26899 else
26900 #endif
26902 it->object = object;
26903 it->char_to_display = ' ';
26904 it->pixel_width = it->len = 1;
26905 while (n--)
26906 tty_append_glyph (it);
26907 it->object = o_object;
26911 it->pixel_width = width;
26912 #ifdef HAVE_WINDOW_SYSTEM
26913 if (FRAME_WINDOW_P (it->f))
26915 it->ascent = it->phys_ascent = ascent;
26916 it->descent = it->phys_descent = height - it->ascent;
26917 it->nglyphs = width > 0 && height > 0;
26918 take_vertical_position_into_account (it);
26920 else
26921 #endif
26922 it->nglyphs = width;
26925 /* Get information about special display element WHAT in an
26926 environment described by IT. WHAT is one of IT_TRUNCATION or
26927 IT_CONTINUATION. Maybe produce glyphs for WHAT if IT has a
26928 non-null glyph_row member. This function ensures that fields like
26929 face_id, c, len of IT are left untouched. */
26931 static void
26932 produce_special_glyphs (struct it *it, enum display_element_type what)
26934 struct it temp_it;
26935 Lisp_Object gc;
26936 GLYPH glyph;
26938 temp_it = *it;
26939 temp_it.object = Qnil;
26940 memset (&temp_it.current, 0, sizeof temp_it.current);
26942 if (what == IT_CONTINUATION)
26944 /* Continuation glyph. For R2L lines, we mirror it by hand. */
26945 if (it->bidi_it.paragraph_dir == R2L)
26946 SET_GLYPH_FROM_CHAR (glyph, '/');
26947 else
26948 SET_GLYPH_FROM_CHAR (glyph, '\\');
26949 if (it->dp
26950 && (gc = DISP_CONTINUE_GLYPH (it->dp), GLYPH_CODE_P (gc)))
26952 /* FIXME: Should we mirror GC for R2L lines? */
26953 SET_GLYPH_FROM_GLYPH_CODE (glyph, gc);
26954 spec_glyph_lookup_face (XWINDOW (it->window), &glyph);
26957 else if (what == IT_TRUNCATION)
26959 /* Truncation glyph. */
26960 SET_GLYPH_FROM_CHAR (glyph, '$');
26961 if (it->dp
26962 && (gc = DISP_TRUNC_GLYPH (it->dp), GLYPH_CODE_P (gc)))
26964 /* FIXME: Should we mirror GC for R2L lines? */
26965 SET_GLYPH_FROM_GLYPH_CODE (glyph, gc);
26966 spec_glyph_lookup_face (XWINDOW (it->window), &glyph);
26969 else
26970 emacs_abort ();
26972 #ifdef HAVE_WINDOW_SYSTEM
26973 /* On a GUI frame, when the right fringe (left fringe for R2L rows)
26974 is turned off, we precede the truncation/continuation glyphs by a
26975 stretch glyph whose width is computed such that these special
26976 glyphs are aligned at the window margin, even when very different
26977 fonts are used in different glyph rows. */
26978 if (FRAME_WINDOW_P (temp_it.f)
26979 /* init_iterator calls this with it->glyph_row == NULL, and it
26980 wants only the pixel width of the truncation/continuation
26981 glyphs. */
26982 && temp_it.glyph_row
26983 /* insert_left_trunc_glyphs calls us at the beginning of the
26984 row, and it has its own calculation of the stretch glyph
26985 width. */
26986 && temp_it.glyph_row->used[TEXT_AREA] > 0
26987 && (temp_it.glyph_row->reversed_p
26988 ? WINDOW_LEFT_FRINGE_WIDTH (temp_it.w)
26989 : WINDOW_RIGHT_FRINGE_WIDTH (temp_it.w)) == 0)
26991 int stretch_width = temp_it.last_visible_x - temp_it.current_x;
26993 if (stretch_width > 0)
26995 struct face *face = FACE_FROM_ID (temp_it.f, temp_it.face_id);
26996 struct font *font =
26997 face->font ? face->font : FRAME_FONT (temp_it.f);
26998 int stretch_ascent =
26999 (((temp_it.ascent + temp_it.descent)
27000 * FONT_BASE (font)) / FONT_HEIGHT (font));
27002 append_stretch_glyph (&temp_it, Qnil, stretch_width,
27003 temp_it.ascent + temp_it.descent,
27004 stretch_ascent);
27007 #endif
27009 temp_it.dp = NULL;
27010 temp_it.what = IT_CHARACTER;
27011 temp_it.c = temp_it.char_to_display = GLYPH_CHAR (glyph);
27012 temp_it.face_id = GLYPH_FACE (glyph);
27013 temp_it.len = CHAR_BYTES (temp_it.c);
27015 PRODUCE_GLYPHS (&temp_it);
27016 it->pixel_width = temp_it.pixel_width;
27017 it->nglyphs = temp_it.nglyphs;
27020 #ifdef HAVE_WINDOW_SYSTEM
27022 /* Calculate line-height and line-spacing properties.
27023 An integer value specifies explicit pixel value.
27024 A float value specifies relative value to current face height.
27025 A cons (float . face-name) specifies relative value to
27026 height of specified face font.
27028 Returns height in pixels, or nil. */
27030 static Lisp_Object
27031 calc_line_height_property (struct it *it, Lisp_Object val, struct font *font,
27032 int boff, bool override)
27034 Lisp_Object face_name = Qnil;
27035 int ascent, descent, height;
27037 if (NILP (val) || INTEGERP (val) || (override && EQ (val, Qt)))
27038 return val;
27040 if (CONSP (val))
27042 face_name = XCAR (val);
27043 val = XCDR (val);
27044 if (!NUMBERP (val))
27045 val = make_number (1);
27046 if (NILP (face_name))
27048 height = it->ascent + it->descent;
27049 goto scale;
27053 if (NILP (face_name))
27055 font = FRAME_FONT (it->f);
27056 boff = FRAME_BASELINE_OFFSET (it->f);
27058 else if (EQ (face_name, Qt))
27060 override = false;
27062 else
27064 int face_id;
27065 struct face *face;
27067 face_id = lookup_named_face (it->f, face_name, false);
27068 face = FACE_FROM_ID_OR_NULL (it->f, face_id);
27069 if (face == NULL || ((font = face->font) == NULL))
27070 return make_number (-1);
27071 boff = font->baseline_offset;
27072 if (font->vertical_centering)
27073 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
27076 normal_char_ascent_descent (font, -1, &ascent, &descent);
27078 if (override)
27080 it->override_ascent = ascent;
27081 it->override_descent = descent;
27082 it->override_boff = boff;
27085 height = ascent + descent;
27087 scale:
27088 if (FLOATP (val))
27089 height = (int)(XFLOAT_DATA (val) * height);
27090 else if (INTEGERP (val))
27091 height *= XINT (val);
27093 return make_number (height);
27097 /* Append a glyph for a glyphless character to IT->glyph_row. FACE_ID
27098 is a face ID to be used for the glyph. FOR_NO_FONT is true if
27099 and only if this is for a character for which no font was found.
27101 If the display method (it->glyphless_method) is
27102 GLYPHLESS_DISPLAY_ACRONYM or GLYPHLESS_DISPLAY_HEX_CODE, LEN is a
27103 length of the acronym or the hexadecimal string, UPPER_XOFF and
27104 UPPER_YOFF are pixel offsets for the upper part of the string,
27105 LOWER_XOFF and LOWER_YOFF are for the lower part.
27107 For the other display methods, LEN through LOWER_YOFF are zero. */
27109 static void
27110 append_glyphless_glyph (struct it *it, int face_id, bool for_no_font, int len,
27111 short upper_xoff, short upper_yoff,
27112 short lower_xoff, short lower_yoff)
27114 struct glyph *glyph;
27115 enum glyph_row_area area = it->area;
27117 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
27118 if (glyph < it->glyph_row->glyphs[area + 1])
27120 /* If the glyph row is reversed, we need to prepend the glyph
27121 rather than append it. */
27122 if (it->glyph_row->reversed_p && area == TEXT_AREA)
27124 struct glyph *g;
27126 /* Make room for the additional glyph. */
27127 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
27128 g[1] = *g;
27129 glyph = it->glyph_row->glyphs[area];
27131 glyph->charpos = CHARPOS (it->position);
27132 glyph->object = it->object;
27133 eassert (it->pixel_width <= SHRT_MAX);
27134 glyph->pixel_width = it->pixel_width;
27135 glyph->ascent = it->ascent;
27136 glyph->descent = it->descent;
27137 glyph->voffset = it->voffset;
27138 glyph->type = GLYPHLESS_GLYPH;
27139 glyph->u.glyphless.method = it->glyphless_method;
27140 glyph->u.glyphless.for_no_font = for_no_font;
27141 glyph->u.glyphless.len = len;
27142 glyph->u.glyphless.ch = it->c;
27143 glyph->slice.glyphless.upper_xoff = upper_xoff;
27144 glyph->slice.glyphless.upper_yoff = upper_yoff;
27145 glyph->slice.glyphless.lower_xoff = lower_xoff;
27146 glyph->slice.glyphless.lower_yoff = lower_yoff;
27147 glyph->avoid_cursor_p = it->avoid_cursor_p;
27148 glyph->multibyte_p = it->multibyte_p;
27149 if (it->glyph_row->reversed_p && area == TEXT_AREA)
27151 /* In R2L rows, the left and the right box edges need to be
27152 drawn in reverse direction. */
27153 glyph->right_box_line_p = it->start_of_box_run_p;
27154 glyph->left_box_line_p = it->end_of_box_run_p;
27156 else
27158 glyph->left_box_line_p = it->start_of_box_run_p;
27159 glyph->right_box_line_p = it->end_of_box_run_p;
27161 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
27162 || it->phys_descent > it->descent);
27163 glyph->padding_p = false;
27164 glyph->glyph_not_available_p = false;
27165 glyph->face_id = face_id;
27166 glyph->font_type = FONT_TYPE_UNKNOWN;
27167 if (it->bidi_p)
27169 glyph->resolved_level = it->bidi_it.resolved_level;
27170 eassert ((it->bidi_it.type & 7) == it->bidi_it.type);
27171 glyph->bidi_type = it->bidi_it.type;
27173 ++it->glyph_row->used[area];
27175 else
27176 IT_EXPAND_MATRIX_WIDTH (it, area);
27180 /* Produce a glyph for a glyphless character for iterator IT.
27181 IT->glyphless_method specifies which method to use for displaying
27182 the character. See the description of enum
27183 glyphless_display_method in dispextern.h for the detail.
27185 FOR_NO_FONT is true if and only if this is for a character for
27186 which no font was found. ACRONYM, if non-nil, is an acronym string
27187 for the character. */
27189 static void
27190 produce_glyphless_glyph (struct it *it, bool for_no_font, Lisp_Object acronym)
27192 int face_id;
27193 struct face *face;
27194 struct font *font;
27195 int base_width, base_height, width, height;
27196 short upper_xoff, upper_yoff, lower_xoff, lower_yoff;
27197 int len;
27199 /* Get the metrics of the base font. We always refer to the current
27200 ASCII face. */
27201 face = FACE_FROM_ID (it->f, it->face_id)->ascii_face;
27202 font = face->font ? face->font : FRAME_FONT (it->f);
27203 normal_char_ascent_descent (font, -1, &it->ascent, &it->descent);
27204 it->ascent += font->baseline_offset;
27205 it->descent -= font->baseline_offset;
27206 base_height = it->ascent + it->descent;
27207 base_width = font->average_width;
27209 face_id = merge_glyphless_glyph_face (it);
27211 if (it->glyphless_method == GLYPHLESS_DISPLAY_THIN_SPACE)
27213 it->pixel_width = THIN_SPACE_WIDTH;
27214 len = 0;
27215 upper_xoff = upper_yoff = lower_xoff = lower_yoff = 0;
27217 else if (it->glyphless_method == GLYPHLESS_DISPLAY_EMPTY_BOX)
27219 width = CHARACTER_WIDTH (it->c);
27220 if (width == 0)
27221 width = 1;
27222 else if (width > 4)
27223 width = 4;
27224 it->pixel_width = base_width * width;
27225 len = 0;
27226 upper_xoff = upper_yoff = lower_xoff = lower_yoff = 0;
27228 else
27230 char buf[7];
27231 const char *str;
27232 unsigned int code[6];
27233 int upper_len;
27234 int ascent, descent;
27235 struct font_metrics metrics_upper, metrics_lower;
27237 face = FACE_FROM_ID (it->f, face_id);
27238 font = face->font ? face->font : FRAME_FONT (it->f);
27239 prepare_face_for_display (it->f, face);
27241 if (it->glyphless_method == GLYPHLESS_DISPLAY_ACRONYM)
27243 if (! STRINGP (acronym) && CHAR_TABLE_P (Vglyphless_char_display))
27244 acronym = CHAR_TABLE_REF (Vglyphless_char_display, it->c);
27245 if (CONSP (acronym))
27246 acronym = XCAR (acronym);
27247 str = STRINGP (acronym) ? SSDATA (acronym) : "";
27249 else
27251 eassert (it->glyphless_method == GLYPHLESS_DISPLAY_HEX_CODE);
27252 sprintf (buf, "%0*X", it->c < 0x10000 ? 4 : 6, it->c + 0u);
27253 str = buf;
27255 for (len = 0; str[len] && ASCII_CHAR_P (str[len]) && len < 6; len++)
27256 code[len] = font->driver->encode_char (font, str[len]);
27257 upper_len = (len + 1) / 2;
27258 font->driver->text_extents (font, code, upper_len,
27259 &metrics_upper);
27260 font->driver->text_extents (font, code + upper_len, len - upper_len,
27261 &metrics_lower);
27265 /* +4 is for vertical bars of a box plus 1-pixel spaces at both side. */
27266 width = max (metrics_upper.width, metrics_lower.width) + 4;
27267 upper_xoff = upper_yoff = 2; /* the typical case */
27268 if (base_width >= width)
27270 /* Align the upper to the left, the lower to the right. */
27271 it->pixel_width = base_width;
27272 lower_xoff = base_width - 2 - metrics_lower.width;
27274 else
27276 /* Center the shorter one. */
27277 it->pixel_width = width;
27278 if (metrics_upper.width >= metrics_lower.width)
27279 lower_xoff = (width - metrics_lower.width) / 2;
27280 else
27282 /* FIXME: This code doesn't look right. It formerly was
27283 missing the "lower_xoff = 0;", which couldn't have
27284 been right since it left lower_xoff uninitialized. */
27285 lower_xoff = 0;
27286 upper_xoff = (width - metrics_upper.width) / 2;
27290 /* +5 is for horizontal bars of a box plus 1-pixel spaces at
27291 top, bottom, and between upper and lower strings. */
27292 height = (metrics_upper.ascent + metrics_upper.descent
27293 + metrics_lower.ascent + metrics_lower.descent) + 5;
27294 /* Center vertically.
27295 H:base_height, D:base_descent
27296 h:height, ld:lower_descent, la:lower_ascent, ud:upper_descent
27298 ascent = - (D - H/2 - h/2 + 1); "+ 1" for rounding up
27299 descent = D - H/2 + h/2;
27300 lower_yoff = descent - 2 - ld;
27301 upper_yoff = lower_yoff - la - 1 - ud; */
27302 ascent = - (it->descent - (base_height + height + 1) / 2);
27303 descent = it->descent - (base_height - height) / 2;
27304 lower_yoff = descent - 2 - metrics_lower.descent;
27305 upper_yoff = (lower_yoff - metrics_lower.ascent - 1
27306 - metrics_upper.descent);
27307 /* Don't make the height shorter than the base height. */
27308 if (height > base_height)
27310 it->ascent = ascent;
27311 it->descent = descent;
27315 it->phys_ascent = it->ascent;
27316 it->phys_descent = it->descent;
27317 if (it->glyph_row)
27318 append_glyphless_glyph (it, face_id, for_no_font, len,
27319 upper_xoff, upper_yoff,
27320 lower_xoff, lower_yoff);
27321 it->nglyphs = 1;
27322 take_vertical_position_into_account (it);
27326 /* RIF:
27327 Produce glyphs/get display metrics for the display element IT is
27328 loaded with. See the description of struct it in dispextern.h
27329 for an overview of struct it. */
27331 void
27332 x_produce_glyphs (struct it *it)
27334 int extra_line_spacing = it->extra_line_spacing;
27336 it->glyph_not_available_p = false;
27338 if (it->what == IT_CHARACTER)
27340 XChar2b char2b;
27341 struct face *face = FACE_FROM_ID (it->f, it->face_id);
27342 struct font *font = face->font;
27343 struct font_metrics *pcm = NULL;
27344 int boff; /* Baseline offset. */
27346 if (font == NULL)
27348 /* When no suitable font is found, display this character by
27349 the method specified in the first extra slot of
27350 Vglyphless_char_display. */
27351 Lisp_Object acronym = lookup_glyphless_char_display (-1, it);
27353 eassert (it->what == IT_GLYPHLESS);
27354 produce_glyphless_glyph (it, true,
27355 STRINGP (acronym) ? acronym : Qnil);
27356 goto done;
27359 boff = font->baseline_offset;
27360 if (font->vertical_centering)
27361 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
27363 if (it->char_to_display != '\n' && it->char_to_display != '\t')
27365 it->nglyphs = 1;
27367 if (it->override_ascent >= 0)
27369 it->ascent = it->override_ascent;
27370 it->descent = it->override_descent;
27371 boff = it->override_boff;
27373 else
27375 it->ascent = FONT_BASE (font) + boff;
27376 it->descent = FONT_DESCENT (font) - boff;
27379 if (get_char_glyph_code (it->char_to_display, font, &char2b))
27381 pcm = get_per_char_metric (font, &char2b);
27382 if (pcm->width == 0
27383 && pcm->rbearing == 0 && pcm->lbearing == 0)
27384 pcm = NULL;
27387 if (pcm)
27389 it->phys_ascent = pcm->ascent + boff;
27390 it->phys_descent = pcm->descent - boff;
27391 it->pixel_width = pcm->width;
27392 /* Don't use font-global values for ascent and descent
27393 if they result in an exceedingly large line height. */
27394 if (it->override_ascent < 0)
27396 if (FONT_TOO_HIGH (font))
27398 it->ascent = it->phys_ascent;
27399 it->descent = it->phys_descent;
27400 /* These limitations are enforced by an
27401 assertion near the end of this function. */
27402 if (it->ascent < 0)
27403 it->ascent = 0;
27404 if (it->descent < 0)
27405 it->descent = 0;
27409 else
27411 it->glyph_not_available_p = true;
27412 it->phys_ascent = it->ascent;
27413 it->phys_descent = it->descent;
27414 it->pixel_width = font->space_width;
27417 if (it->constrain_row_ascent_descent_p)
27419 if (it->descent > it->max_descent)
27421 it->ascent += it->descent - it->max_descent;
27422 it->descent = it->max_descent;
27424 if (it->ascent > it->max_ascent)
27426 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
27427 it->ascent = it->max_ascent;
27429 it->phys_ascent = min (it->phys_ascent, it->ascent);
27430 it->phys_descent = min (it->phys_descent, it->descent);
27431 extra_line_spacing = 0;
27434 /* If this is a space inside a region of text with
27435 `space-width' property, change its width. */
27436 bool stretched_p
27437 = it->char_to_display == ' ' && !NILP (it->space_width);
27438 if (stretched_p)
27439 it->pixel_width *= XFLOATINT (it->space_width);
27441 /* If face has a box, add the box thickness to the character
27442 height. If character has a box line to the left and/or
27443 right, add the box line width to the character's width. */
27444 if (face->box != FACE_NO_BOX)
27446 int thick = face->box_line_width;
27448 if (thick > 0)
27450 it->ascent += thick;
27451 it->descent += thick;
27453 else
27454 thick = -thick;
27456 if (it->start_of_box_run_p)
27457 it->pixel_width += thick;
27458 if (it->end_of_box_run_p)
27459 it->pixel_width += thick;
27462 /* If face has an overline, add the height of the overline
27463 (1 pixel) and a 1 pixel margin to the character height. */
27464 if (face->overline_p)
27465 it->ascent += overline_margin;
27467 if (it->constrain_row_ascent_descent_p)
27469 if (it->ascent > it->max_ascent)
27470 it->ascent = it->max_ascent;
27471 if (it->descent > it->max_descent)
27472 it->descent = it->max_descent;
27475 take_vertical_position_into_account (it);
27477 /* If we have to actually produce glyphs, do it. */
27478 if (it->glyph_row)
27480 if (stretched_p)
27482 /* Translate a space with a `space-width' property
27483 into a stretch glyph. */
27484 int ascent = (((it->ascent + it->descent) * FONT_BASE (font))
27485 / FONT_HEIGHT (font));
27486 append_stretch_glyph (it, it->object, it->pixel_width,
27487 it->ascent + it->descent, ascent);
27489 else
27490 append_glyph (it);
27492 /* If characters with lbearing or rbearing are displayed
27493 in this line, record that fact in a flag of the
27494 glyph row. This is used to optimize X output code. */
27495 if (pcm && (pcm->lbearing < 0 || pcm->rbearing > pcm->width))
27496 it->glyph_row->contains_overlapping_glyphs_p = true;
27498 if (! stretched_p && it->pixel_width == 0)
27499 /* We assure that all visible glyphs have at least 1-pixel
27500 width. */
27501 it->pixel_width = 1;
27503 else if (it->char_to_display == '\n')
27505 /* A newline has no width, but we need the height of the
27506 line. But if previous part of the line sets a height,
27507 don't increase that height. */
27509 Lisp_Object height;
27510 Lisp_Object total_height = Qnil;
27512 it->override_ascent = -1;
27513 it->pixel_width = 0;
27514 it->nglyphs = 0;
27516 height = get_it_property (it, Qline_height);
27517 /* Split (line-height total-height) list. */
27518 if (CONSP (height)
27519 && CONSP (XCDR (height))
27520 && NILP (XCDR (XCDR (height))))
27522 total_height = XCAR (XCDR (height));
27523 height = XCAR (height);
27525 height = calc_line_height_property (it, height, font, boff, true);
27527 if (it->override_ascent >= 0)
27529 it->ascent = it->override_ascent;
27530 it->descent = it->override_descent;
27531 boff = it->override_boff;
27533 else
27535 if (FONT_TOO_HIGH (font))
27537 it->ascent = font->pixel_size + boff - 1;
27538 it->descent = -boff + 1;
27539 if (it->descent < 0)
27540 it->descent = 0;
27542 else
27544 it->ascent = FONT_BASE (font) + boff;
27545 it->descent = FONT_DESCENT (font) - boff;
27549 if (EQ (height, Qt))
27551 if (it->descent > it->max_descent)
27553 it->ascent += it->descent - it->max_descent;
27554 it->descent = it->max_descent;
27556 if (it->ascent > it->max_ascent)
27558 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
27559 it->ascent = it->max_ascent;
27561 it->phys_ascent = min (it->phys_ascent, it->ascent);
27562 it->phys_descent = min (it->phys_descent, it->descent);
27563 it->constrain_row_ascent_descent_p = true;
27564 extra_line_spacing = 0;
27566 else
27568 Lisp_Object spacing;
27570 it->phys_ascent = it->ascent;
27571 it->phys_descent = it->descent;
27573 if ((it->max_ascent > 0 || it->max_descent > 0)
27574 && face->box != FACE_NO_BOX
27575 && face->box_line_width > 0)
27577 it->ascent += face->box_line_width;
27578 it->descent += face->box_line_width;
27580 if (!NILP (height)
27581 && XINT (height) > it->ascent + it->descent)
27582 it->ascent = XINT (height) - it->descent;
27584 if (!NILP (total_height))
27585 spacing = calc_line_height_property (it, total_height, font,
27586 boff, false);
27587 else
27589 spacing = get_it_property (it, Qline_spacing);
27590 spacing = calc_line_height_property (it, spacing, font,
27591 boff, false);
27593 if (INTEGERP (spacing))
27595 extra_line_spacing = XINT (spacing);
27596 if (!NILP (total_height))
27597 extra_line_spacing -= (it->phys_ascent + it->phys_descent);
27601 else /* i.e. (it->char_to_display == '\t') */
27603 if (font->space_width > 0)
27605 int tab_width = it->tab_width * font->space_width;
27606 int x = it->current_x + it->continuation_lines_width;
27607 int next_tab_x = ((1 + x + tab_width - 1) / tab_width) * tab_width;
27609 /* If the distance from the current position to the next tab
27610 stop is less than a space character width, use the
27611 tab stop after that. */
27612 if (next_tab_x - x < font->space_width)
27613 next_tab_x += tab_width;
27615 it->pixel_width = next_tab_x - x;
27616 it->nglyphs = 1;
27617 if (FONT_TOO_HIGH (font))
27619 if (get_char_glyph_code (' ', font, &char2b))
27621 pcm = get_per_char_metric (font, &char2b);
27622 if (pcm->width == 0
27623 && pcm->rbearing == 0 && pcm->lbearing == 0)
27624 pcm = NULL;
27627 if (pcm)
27629 it->ascent = pcm->ascent + boff;
27630 it->descent = pcm->descent - boff;
27632 else
27634 it->ascent = font->pixel_size + boff - 1;
27635 it->descent = -boff + 1;
27637 if (it->ascent < 0)
27638 it->ascent = 0;
27639 if (it->descent < 0)
27640 it->descent = 0;
27642 else
27644 it->ascent = FONT_BASE (font) + boff;
27645 it->descent = FONT_DESCENT (font) - boff;
27647 it->phys_ascent = it->ascent;
27648 it->phys_descent = it->descent;
27650 if (it->glyph_row)
27652 append_stretch_glyph (it, it->object, it->pixel_width,
27653 it->ascent + it->descent, it->ascent);
27656 else
27658 it->pixel_width = 0;
27659 it->nglyphs = 1;
27663 if (FONT_TOO_HIGH (font))
27665 int font_ascent, font_descent;
27667 /* For very large fonts, where we ignore the declared font
27668 dimensions, and go by per-character metrics instead,
27669 don't let the row ascent and descent values (and the row
27670 height computed from them) be smaller than the "normal"
27671 character metrics. This avoids unpleasant effects
27672 whereby lines on display would change their height
27673 depending on which characters are shown. */
27674 normal_char_ascent_descent (font, -1, &font_ascent, &font_descent);
27675 it->max_ascent = max (it->max_ascent, font_ascent);
27676 it->max_descent = max (it->max_descent, font_descent);
27679 else if (it->what == IT_COMPOSITION && it->cmp_it.ch < 0)
27681 /* A static composition.
27683 Note: A composition is represented as one glyph in the
27684 glyph matrix. There are no padding glyphs.
27686 Important note: pixel_width, ascent, and descent are the
27687 values of what is drawn by draw_glyphs (i.e. the values of
27688 the overall glyphs composed). */
27689 struct face *face = FACE_FROM_ID (it->f, it->face_id);
27690 int boff; /* baseline offset */
27691 struct composition *cmp = composition_table[it->cmp_it.id];
27692 int glyph_len = cmp->glyph_len;
27693 struct font *font = face->font;
27695 it->nglyphs = 1;
27697 /* If we have not yet calculated pixel size data of glyphs of
27698 the composition for the current face font, calculate them
27699 now. Theoretically, we have to check all fonts for the
27700 glyphs, but that requires much time and memory space. So,
27701 here we check only the font of the first glyph. This may
27702 lead to incorrect display, but it's very rare, and C-l
27703 (recenter-top-bottom) can correct the display anyway. */
27704 if (! cmp->font || cmp->font != font)
27706 /* Ascent and descent of the font of the first character
27707 of this composition (adjusted by baseline offset).
27708 Ascent and descent of overall glyphs should not be less
27709 than these, respectively. */
27710 int font_ascent, font_descent, font_height;
27711 /* Bounding box of the overall glyphs. */
27712 int leftmost, rightmost, lowest, highest;
27713 int lbearing, rbearing;
27714 int i, width, ascent, descent;
27715 int c;
27716 XChar2b char2b;
27717 struct font_metrics *pcm;
27718 ptrdiff_t pos;
27720 eassume (0 < glyph_len); /* See Bug#8512. */
27722 c = COMPOSITION_GLYPH (cmp, glyph_len - 1);
27723 while (c == '\t' && 0 < --glyph_len);
27725 bool right_padded = glyph_len < cmp->glyph_len;
27726 for (i = 0; i < glyph_len; i++)
27728 c = COMPOSITION_GLYPH (cmp, i);
27729 if (c != '\t')
27730 break;
27731 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
27733 bool left_padded = i > 0;
27735 pos = (STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
27736 : IT_CHARPOS (*it));
27737 /* If no suitable font is found, use the default font. */
27738 bool font_not_found_p = font == NULL;
27739 if (font_not_found_p)
27741 face = face->ascii_face;
27742 font = face->font;
27744 boff = font->baseline_offset;
27745 if (font->vertical_centering)
27746 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
27747 normal_char_ascent_descent (font, -1, &font_ascent, &font_descent);
27748 font_ascent += boff;
27749 font_descent -= boff;
27750 font_height = font_ascent + font_descent;
27752 cmp->font = font;
27754 pcm = NULL;
27755 if (! font_not_found_p)
27757 get_char_face_and_encoding (it->f, c, it->face_id,
27758 &char2b, false);
27759 pcm = get_per_char_metric (font, &char2b);
27762 /* Initialize the bounding box. */
27763 if (pcm)
27765 width = cmp->glyph_len > 0 ? pcm->width : 0;
27766 ascent = pcm->ascent;
27767 descent = pcm->descent;
27768 lbearing = pcm->lbearing;
27769 rbearing = pcm->rbearing;
27771 else
27773 width = cmp->glyph_len > 0 ? font->space_width : 0;
27774 ascent = FONT_BASE (font);
27775 descent = FONT_DESCENT (font);
27776 lbearing = 0;
27777 rbearing = width;
27780 rightmost = width;
27781 leftmost = 0;
27782 lowest = - descent + boff;
27783 highest = ascent + boff;
27785 if (! font_not_found_p
27786 && font->default_ascent
27787 && CHAR_TABLE_P (Vuse_default_ascent)
27788 && !NILP (Faref (Vuse_default_ascent,
27789 make_number (it->char_to_display))))
27790 highest = font->default_ascent + boff;
27792 /* Draw the first glyph at the normal position. It may be
27793 shifted to right later if some other glyphs are drawn
27794 at the left. */
27795 cmp->offsets[i * 2] = 0;
27796 cmp->offsets[i * 2 + 1] = boff;
27797 cmp->lbearing = lbearing;
27798 cmp->rbearing = rbearing;
27800 /* Set cmp->offsets for the remaining glyphs. */
27801 for (i++; i < glyph_len; i++)
27803 int left, right, btm, top;
27804 int ch = COMPOSITION_GLYPH (cmp, i);
27805 int face_id;
27806 struct face *this_face;
27808 if (ch == '\t')
27809 ch = ' ';
27810 face_id = FACE_FOR_CHAR (it->f, face, ch, pos, it->string);
27811 this_face = FACE_FROM_ID (it->f, face_id);
27812 font = this_face->font;
27814 if (font == NULL)
27815 pcm = NULL;
27816 else
27818 get_char_face_and_encoding (it->f, ch, face_id,
27819 &char2b, false);
27820 pcm = get_per_char_metric (font, &char2b);
27822 if (! pcm)
27823 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
27824 else
27826 width = pcm->width;
27827 ascent = pcm->ascent;
27828 descent = pcm->descent;
27829 lbearing = pcm->lbearing;
27830 rbearing = pcm->rbearing;
27831 if (cmp->method != COMPOSITION_WITH_RULE_ALTCHARS)
27833 /* Relative composition with or without
27834 alternate chars. */
27835 left = (leftmost + rightmost - width) / 2;
27836 btm = - descent + boff;
27837 if (font->relative_compose
27838 && (! CHAR_TABLE_P (Vignore_relative_composition)
27839 || NILP (Faref (Vignore_relative_composition,
27840 make_number (ch)))))
27843 if (- descent >= font->relative_compose)
27844 /* One extra pixel between two glyphs. */
27845 btm = highest + 1;
27846 else if (ascent <= 0)
27847 /* One extra pixel between two glyphs. */
27848 btm = lowest - 1 - ascent - descent;
27851 else
27853 /* A composition rule is specified by an integer
27854 value that encodes global and new reference
27855 points (GREF and NREF). GREF and NREF are
27856 specified by numbers as below:
27858 0---1---2 -- ascent
27862 9--10--11 -- center
27864 ---3---4---5--- baseline
27866 6---7---8 -- descent
27868 int rule = COMPOSITION_RULE (cmp, i);
27869 int gref, nref, grefx, grefy, nrefx, nrefy, xoff, yoff;
27871 COMPOSITION_DECODE_RULE (rule, gref, nref, xoff, yoff);
27872 grefx = gref % 3, nrefx = nref % 3;
27873 grefy = gref / 3, nrefy = nref / 3;
27874 if (xoff)
27875 xoff = font_height * (xoff - 128) / 256;
27876 if (yoff)
27877 yoff = font_height * (yoff - 128) / 256;
27879 left = (leftmost
27880 + grefx * (rightmost - leftmost) / 2
27881 - nrefx * width / 2
27882 + xoff);
27884 btm = ((grefy == 0 ? highest
27885 : grefy == 1 ? 0
27886 : grefy == 2 ? lowest
27887 : (highest + lowest) / 2)
27888 - (nrefy == 0 ? ascent + descent
27889 : nrefy == 1 ? descent - boff
27890 : nrefy == 2 ? 0
27891 : (ascent + descent) / 2)
27892 + yoff);
27895 cmp->offsets[i * 2] = left;
27896 cmp->offsets[i * 2 + 1] = btm + descent;
27898 /* Update the bounding box of the overall glyphs. */
27899 if (width > 0)
27901 right = left + width;
27902 if (left < leftmost)
27903 leftmost = left;
27904 if (right > rightmost)
27905 rightmost = right;
27907 top = btm + descent + ascent;
27908 if (top > highest)
27909 highest = top;
27910 if (btm < lowest)
27911 lowest = btm;
27913 if (cmp->lbearing > left + lbearing)
27914 cmp->lbearing = left + lbearing;
27915 if (cmp->rbearing < left + rbearing)
27916 cmp->rbearing = left + rbearing;
27920 /* If there are glyphs whose x-offsets are negative,
27921 shift all glyphs to the right and make all x-offsets
27922 non-negative. */
27923 if (leftmost < 0)
27925 for (i = 0; i < cmp->glyph_len; i++)
27926 cmp->offsets[i * 2] -= leftmost;
27927 rightmost -= leftmost;
27928 cmp->lbearing -= leftmost;
27929 cmp->rbearing -= leftmost;
27932 if (left_padded && cmp->lbearing < 0)
27934 for (i = 0; i < cmp->glyph_len; i++)
27935 cmp->offsets[i * 2] -= cmp->lbearing;
27936 rightmost -= cmp->lbearing;
27937 cmp->rbearing -= cmp->lbearing;
27938 cmp->lbearing = 0;
27940 if (right_padded && rightmost < cmp->rbearing)
27942 rightmost = cmp->rbearing;
27945 cmp->pixel_width = rightmost;
27946 cmp->ascent = highest;
27947 cmp->descent = - lowest;
27948 if (cmp->ascent < font_ascent)
27949 cmp->ascent = font_ascent;
27950 if (cmp->descent < font_descent)
27951 cmp->descent = font_descent;
27954 if (it->glyph_row
27955 && (cmp->lbearing < 0
27956 || cmp->rbearing > cmp->pixel_width))
27957 it->glyph_row->contains_overlapping_glyphs_p = true;
27959 it->pixel_width = cmp->pixel_width;
27960 it->ascent = it->phys_ascent = cmp->ascent;
27961 it->descent = it->phys_descent = cmp->descent;
27962 if (face->box != FACE_NO_BOX)
27964 int thick = face->box_line_width;
27966 if (thick > 0)
27968 it->ascent += thick;
27969 it->descent += thick;
27971 else
27972 thick = - thick;
27974 if (it->start_of_box_run_p)
27975 it->pixel_width += thick;
27976 if (it->end_of_box_run_p)
27977 it->pixel_width += thick;
27980 /* If face has an overline, add the height of the overline
27981 (1 pixel) and a 1 pixel margin to the character height. */
27982 if (face->overline_p)
27983 it->ascent += overline_margin;
27985 take_vertical_position_into_account (it);
27986 if (it->ascent < 0)
27987 it->ascent = 0;
27988 if (it->descent < 0)
27989 it->descent = 0;
27991 if (it->glyph_row && cmp->glyph_len > 0)
27992 append_composite_glyph (it);
27994 else if (it->what == IT_COMPOSITION)
27996 /* A dynamic (automatic) composition. */
27997 struct face *face = FACE_FROM_ID (it->f, it->face_id);
27998 Lisp_Object gstring;
27999 struct font_metrics metrics;
28001 it->nglyphs = 1;
28003 gstring = composition_gstring_from_id (it->cmp_it.id);
28004 it->pixel_width
28005 = composition_gstring_width (gstring, it->cmp_it.from, it->cmp_it.to,
28006 &metrics);
28007 if (it->glyph_row
28008 && (metrics.lbearing < 0 || metrics.rbearing > metrics.width))
28009 it->glyph_row->contains_overlapping_glyphs_p = true;
28010 it->ascent = it->phys_ascent = metrics.ascent;
28011 it->descent = it->phys_descent = metrics.descent;
28012 if (face->box != FACE_NO_BOX)
28014 int thick = face->box_line_width;
28016 if (thick > 0)
28018 it->ascent += thick;
28019 it->descent += thick;
28021 else
28022 thick = - thick;
28024 if (it->start_of_box_run_p)
28025 it->pixel_width += thick;
28026 if (it->end_of_box_run_p)
28027 it->pixel_width += thick;
28029 /* If face has an overline, add the height of the overline
28030 (1 pixel) and a 1 pixel margin to the character height. */
28031 if (face->overline_p)
28032 it->ascent += overline_margin;
28033 take_vertical_position_into_account (it);
28034 if (it->ascent < 0)
28035 it->ascent = 0;
28036 if (it->descent < 0)
28037 it->descent = 0;
28039 if (it->glyph_row)
28040 append_composite_glyph (it);
28042 else if (it->what == IT_GLYPHLESS)
28043 produce_glyphless_glyph (it, false, Qnil);
28044 else if (it->what == IT_IMAGE)
28045 produce_image_glyph (it);
28046 else if (it->what == IT_STRETCH)
28047 produce_stretch_glyph (it);
28048 else if (it->what == IT_XWIDGET)
28049 produce_xwidget_glyph (it);
28051 done:
28052 /* Accumulate dimensions. Note: can't assume that it->descent > 0
28053 because this isn't true for images with `:ascent 100'. */
28054 eassert (it->ascent >= 0 && it->descent >= 0);
28055 if (it->area == TEXT_AREA)
28056 it->current_x += it->pixel_width;
28058 if (extra_line_spacing > 0)
28060 it->descent += extra_line_spacing;
28061 if (extra_line_spacing > it->max_extra_line_spacing)
28062 it->max_extra_line_spacing = extra_line_spacing;
28065 it->max_ascent = max (it->max_ascent, it->ascent);
28066 it->max_descent = max (it->max_descent, it->descent);
28067 it->max_phys_ascent = max (it->max_phys_ascent, it->phys_ascent);
28068 it->max_phys_descent = max (it->max_phys_descent, it->phys_descent);
28071 /* EXPORT for RIF:
28072 Output LEN glyphs starting at START at the nominal cursor position.
28073 Advance the nominal cursor over the text. UPDATED_ROW is the glyph row
28074 being updated, and UPDATED_AREA is the area of that row being updated. */
28076 void
28077 x_write_glyphs (struct window *w, struct glyph_row *updated_row,
28078 struct glyph *start, enum glyph_row_area updated_area, int len)
28080 int x, hpos, chpos = w->phys_cursor.hpos;
28082 eassert (updated_row);
28083 /* When the window is hscrolled, cursor hpos can legitimately be out
28084 of bounds, but we draw the cursor at the corresponding window
28085 margin in that case. */
28086 if (!updated_row->reversed_p && chpos < 0)
28087 chpos = 0;
28088 if (updated_row->reversed_p && chpos >= updated_row->used[TEXT_AREA])
28089 chpos = updated_row->used[TEXT_AREA] - 1;
28091 block_input ();
28093 /* Write glyphs. */
28095 hpos = start - updated_row->glyphs[updated_area];
28096 x = draw_glyphs (w, w->output_cursor.x,
28097 updated_row, updated_area,
28098 hpos, hpos + len,
28099 DRAW_NORMAL_TEXT, 0);
28101 /* Invalidate old phys cursor if the glyph at its hpos is redrawn. */
28102 if (updated_area == TEXT_AREA
28103 && w->phys_cursor_on_p
28104 && w->phys_cursor.vpos == w->output_cursor.vpos
28105 && chpos >= hpos
28106 && chpos < hpos + len)
28107 w->phys_cursor_on_p = false;
28109 unblock_input ();
28111 /* Advance the output cursor. */
28112 w->output_cursor.hpos += len;
28113 w->output_cursor.x = x;
28117 /* EXPORT for RIF:
28118 Insert LEN glyphs from START at the nominal cursor position. */
28120 void
28121 x_insert_glyphs (struct window *w, struct glyph_row *updated_row,
28122 struct glyph *start, enum glyph_row_area updated_area, int len)
28124 struct frame *f;
28125 int line_height, shift_by_width, shifted_region_width;
28126 struct glyph_row *row;
28127 struct glyph *glyph;
28128 int frame_x, frame_y;
28129 ptrdiff_t hpos;
28131 eassert (updated_row);
28132 block_input ();
28133 f = XFRAME (WINDOW_FRAME (w));
28135 /* Get the height of the line we are in. */
28136 row = updated_row;
28137 line_height = row->height;
28139 /* Get the width of the glyphs to insert. */
28140 shift_by_width = 0;
28141 for (glyph = start; glyph < start + len; ++glyph)
28142 shift_by_width += glyph->pixel_width;
28144 /* Get the width of the region to shift right. */
28145 shifted_region_width = (window_box_width (w, updated_area)
28146 - w->output_cursor.x
28147 - shift_by_width);
28149 /* Shift right. */
28150 frame_x = window_box_left (w, updated_area) + w->output_cursor.x;
28151 frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, w->output_cursor.y);
28153 FRAME_RIF (f)->shift_glyphs_for_insert (f, frame_x, frame_y, shifted_region_width,
28154 line_height, shift_by_width);
28156 /* Write the glyphs. */
28157 hpos = start - row->glyphs[updated_area];
28158 draw_glyphs (w, w->output_cursor.x, row, updated_area,
28159 hpos, hpos + len,
28160 DRAW_NORMAL_TEXT, 0);
28162 /* Advance the output cursor. */
28163 w->output_cursor.hpos += len;
28164 w->output_cursor.x += shift_by_width;
28165 unblock_input ();
28169 /* EXPORT for RIF:
28170 Erase the current text line from the nominal cursor position
28171 (inclusive) to pixel column TO_X (exclusive). The idea is that
28172 everything from TO_X onward is already erased.
28174 TO_X is a pixel position relative to UPDATED_AREA of currently
28175 updated window W. TO_X == -1 means clear to the end of this area. */
28177 void
28178 x_clear_end_of_line (struct window *w, struct glyph_row *updated_row,
28179 enum glyph_row_area updated_area, int to_x)
28181 struct frame *f;
28182 int max_x, min_y, max_y;
28183 int from_x, from_y, to_y;
28185 eassert (updated_row);
28186 f = XFRAME (w->frame);
28188 if (updated_row->full_width_p)
28189 max_x = (WINDOW_PIXEL_WIDTH (w)
28190 - (updated_row->mode_line_p ? WINDOW_RIGHT_DIVIDER_WIDTH (w) : 0));
28191 else
28192 max_x = window_box_width (w, updated_area);
28193 max_y = window_text_bottom_y (w);
28195 /* TO_X == 0 means don't do anything. TO_X < 0 means clear to end
28196 of window. For TO_X > 0, truncate to end of drawing area. */
28197 if (to_x == 0)
28198 return;
28199 else if (to_x < 0)
28200 to_x = max_x;
28201 else
28202 to_x = min (to_x, max_x);
28204 to_y = min (max_y, w->output_cursor.y + updated_row->height);
28206 /* Notice if the cursor will be cleared by this operation. */
28207 if (!updated_row->full_width_p)
28208 notice_overwritten_cursor (w, updated_area,
28209 w->output_cursor.x, -1,
28210 updated_row->y,
28211 MATRIX_ROW_BOTTOM_Y (updated_row));
28213 from_x = w->output_cursor.x;
28215 /* Translate to frame coordinates. */
28216 if (updated_row->full_width_p)
28218 from_x = WINDOW_TO_FRAME_PIXEL_X (w, from_x);
28219 to_x = WINDOW_TO_FRAME_PIXEL_X (w, to_x);
28221 else
28223 int area_left = window_box_left (w, updated_area);
28224 from_x += area_left;
28225 to_x += area_left;
28228 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
28229 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, max (min_y, w->output_cursor.y));
28230 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, to_y);
28232 /* Prevent inadvertently clearing to end of the X window. */
28233 if (to_x > from_x && to_y > from_y)
28235 block_input ();
28236 FRAME_RIF (f)->clear_frame_area (f, from_x, from_y,
28237 to_x - from_x, to_y - from_y);
28238 unblock_input ();
28242 #endif /* HAVE_WINDOW_SYSTEM */
28246 /***********************************************************************
28247 Cursor types
28248 ***********************************************************************/
28250 /* Value is the internal representation of the specified cursor type
28251 ARG. If type is BAR_CURSOR, return in *WIDTH the specified width
28252 of the bar cursor. */
28254 static enum text_cursor_kinds
28255 get_specified_cursor_type (Lisp_Object arg, int *width)
28257 enum text_cursor_kinds type;
28259 if (NILP (arg))
28260 return NO_CURSOR;
28262 if (EQ (arg, Qbox))
28263 return FILLED_BOX_CURSOR;
28265 if (EQ (arg, Qhollow))
28266 return HOLLOW_BOX_CURSOR;
28268 if (EQ (arg, Qbar))
28270 *width = 2;
28271 return BAR_CURSOR;
28274 if (CONSP (arg)
28275 && EQ (XCAR (arg), Qbar)
28276 && RANGED_INTEGERP (0, XCDR (arg), INT_MAX))
28278 *width = XINT (XCDR (arg));
28279 return BAR_CURSOR;
28282 if (EQ (arg, Qhbar))
28284 *width = 2;
28285 return HBAR_CURSOR;
28288 if (CONSP (arg)
28289 && EQ (XCAR (arg), Qhbar)
28290 && RANGED_INTEGERP (0, XCDR (arg), INT_MAX))
28292 *width = XINT (XCDR (arg));
28293 return HBAR_CURSOR;
28296 /* Treat anything unknown as "hollow box cursor".
28297 It was bad to signal an error; people have trouble fixing
28298 .Xdefaults with Emacs, when it has something bad in it. */
28299 type = HOLLOW_BOX_CURSOR;
28301 return type;
28304 /* Set the default cursor types for specified frame. */
28305 void
28306 set_frame_cursor_types (struct frame *f, Lisp_Object arg)
28308 int width = 1;
28309 Lisp_Object tem;
28311 FRAME_DESIRED_CURSOR (f) = get_specified_cursor_type (arg, &width);
28312 FRAME_CURSOR_WIDTH (f) = width;
28314 /* By default, set up the blink-off state depending on the on-state. */
28316 tem = Fassoc (arg, Vblink_cursor_alist);
28317 if (!NILP (tem))
28319 FRAME_BLINK_OFF_CURSOR (f)
28320 = get_specified_cursor_type (XCDR (tem), &width);
28321 FRAME_BLINK_OFF_CURSOR_WIDTH (f) = width;
28323 else
28324 FRAME_BLINK_OFF_CURSOR (f) = DEFAULT_CURSOR;
28326 /* Make sure the cursor gets redrawn. */
28327 f->cursor_type_changed = true;
28331 #ifdef HAVE_WINDOW_SYSTEM
28333 /* Return the cursor we want to be displayed in window W. Return
28334 width of bar/hbar cursor through WIDTH arg. Return with
28335 ACTIVE_CURSOR arg set to true if cursor in window W is `active'
28336 (i.e. if the `system caret' should track this cursor).
28338 In a mini-buffer window, we want the cursor only to appear if we
28339 are reading input from this window. For the selected window, we
28340 want the cursor type given by the frame parameter or buffer local
28341 setting of cursor-type. If explicitly marked off, draw no cursor.
28342 In all other cases, we want a hollow box cursor. */
28344 static enum text_cursor_kinds
28345 get_window_cursor_type (struct window *w, struct glyph *glyph, int *width,
28346 bool *active_cursor)
28348 struct frame *f = XFRAME (w->frame);
28349 struct buffer *b = XBUFFER (w->contents);
28350 int cursor_type = DEFAULT_CURSOR;
28351 Lisp_Object alt_cursor;
28352 bool non_selected = false;
28354 *active_cursor = true;
28356 /* Echo area */
28357 if (cursor_in_echo_area
28358 && FRAME_HAS_MINIBUF_P (f)
28359 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
28361 if (w == XWINDOW (echo_area_window))
28363 if (EQ (BVAR (b, cursor_type), Qt) || NILP (BVAR (b, cursor_type)))
28365 *width = FRAME_CURSOR_WIDTH (f);
28366 return FRAME_DESIRED_CURSOR (f);
28368 else
28369 return get_specified_cursor_type (BVAR (b, cursor_type), width);
28372 *active_cursor = false;
28373 non_selected = true;
28376 /* Detect a nonselected window or nonselected frame. */
28377 else if (w != XWINDOW (f->selected_window)
28378 || f != FRAME_DISPLAY_INFO (f)->x_highlight_frame)
28380 *active_cursor = false;
28382 if (MINI_WINDOW_P (w) && minibuf_level == 0)
28383 return NO_CURSOR;
28385 non_selected = true;
28388 /* Never display a cursor in a window in which cursor-type is nil. */
28389 if (NILP (BVAR (b, cursor_type)))
28390 return NO_CURSOR;
28392 /* Get the normal cursor type for this window. */
28393 if (EQ (BVAR (b, cursor_type), Qt))
28395 cursor_type = FRAME_DESIRED_CURSOR (f);
28396 *width = FRAME_CURSOR_WIDTH (f);
28398 else
28399 cursor_type = get_specified_cursor_type (BVAR (b, cursor_type), width);
28401 /* Use cursor-in-non-selected-windows instead
28402 for non-selected window or frame. */
28403 if (non_selected)
28405 alt_cursor = BVAR (b, cursor_in_non_selected_windows);
28406 if (!EQ (Qt, alt_cursor))
28407 return get_specified_cursor_type (alt_cursor, width);
28408 /* t means modify the normal cursor type. */
28409 if (cursor_type == FILLED_BOX_CURSOR)
28410 cursor_type = HOLLOW_BOX_CURSOR;
28411 else if (cursor_type == BAR_CURSOR && *width > 1)
28412 --*width;
28413 return cursor_type;
28416 /* Use normal cursor if not blinked off. */
28417 if (!w->cursor_off_p)
28419 if (glyph != NULL && glyph->type == XWIDGET_GLYPH)
28420 return NO_CURSOR;
28421 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
28423 if (cursor_type == FILLED_BOX_CURSOR)
28425 /* Using a block cursor on large images can be very annoying.
28426 So use a hollow cursor for "large" images.
28427 If image is not transparent (no mask), also use hollow cursor. */
28428 struct image *img = IMAGE_OPT_FROM_ID (f, glyph->u.img_id);
28429 if (img != NULL && IMAGEP (img->spec))
28431 /* Arbitrarily, interpret "Large" as >32x32 and >NxN
28432 where N = size of default frame font size.
28433 This should cover most of the "tiny" icons people may use. */
28434 if (!img->mask
28435 || img->width > max (32, WINDOW_FRAME_COLUMN_WIDTH (w))
28436 || img->height > max (32, WINDOW_FRAME_LINE_HEIGHT (w)))
28437 cursor_type = HOLLOW_BOX_CURSOR;
28440 else if (cursor_type != NO_CURSOR)
28442 /* Display current only supports BOX and HOLLOW cursors for images.
28443 So for now, unconditionally use a HOLLOW cursor when cursor is
28444 not a solid box cursor. */
28445 cursor_type = HOLLOW_BOX_CURSOR;
28448 return cursor_type;
28451 /* Cursor is blinked off, so determine how to "toggle" it. */
28453 /* First look for an entry matching the buffer's cursor-type in blink-cursor-alist. */
28454 if ((alt_cursor = Fassoc (BVAR (b, cursor_type), Vblink_cursor_alist), !NILP (alt_cursor)))
28455 return get_specified_cursor_type (XCDR (alt_cursor), width);
28457 /* Then see if frame has specified a specific blink off cursor type. */
28458 if (FRAME_BLINK_OFF_CURSOR (f) != DEFAULT_CURSOR)
28460 *width = FRAME_BLINK_OFF_CURSOR_WIDTH (f);
28461 return FRAME_BLINK_OFF_CURSOR (f);
28464 #if false
28465 /* Some people liked having a permanently visible blinking cursor,
28466 while others had very strong opinions against it. So it was
28467 decided to remove it. KFS 2003-09-03 */
28469 /* Finally perform built-in cursor blinking:
28470 filled box <-> hollow box
28471 wide [h]bar <-> narrow [h]bar
28472 narrow [h]bar <-> no cursor
28473 other type <-> no cursor */
28475 if (cursor_type == FILLED_BOX_CURSOR)
28476 return HOLLOW_BOX_CURSOR;
28478 if ((cursor_type == BAR_CURSOR || cursor_type == HBAR_CURSOR) && *width > 1)
28480 *width = 1;
28481 return cursor_type;
28483 #endif
28485 return NO_CURSOR;
28489 /* Notice when the text cursor of window W has been completely
28490 overwritten by a drawing operation that outputs glyphs in AREA
28491 starting at X0 and ending at X1 in the line starting at Y0 and
28492 ending at Y1. X coordinates are area-relative. X1 < 0 means all
28493 the rest of the line after X0 has been written. Y coordinates
28494 are window-relative. */
28496 static void
28497 notice_overwritten_cursor (struct window *w, enum glyph_row_area area,
28498 int x0, int x1, int y0, int y1)
28500 int cx0, cx1, cy0, cy1;
28501 struct glyph_row *row;
28503 if (!w->phys_cursor_on_p)
28504 return;
28505 if (area != TEXT_AREA)
28506 return;
28508 if (w->phys_cursor.vpos < 0
28509 || w->phys_cursor.vpos >= w->current_matrix->nrows
28510 || (row = w->current_matrix->rows + w->phys_cursor.vpos,
28511 !(row->enabled_p && MATRIX_ROW_DISPLAYS_TEXT_P (row))))
28512 return;
28514 if (row->cursor_in_fringe_p)
28516 row->cursor_in_fringe_p = false;
28517 draw_fringe_bitmap (w, row, row->reversed_p);
28518 w->phys_cursor_on_p = false;
28519 return;
28522 cx0 = w->phys_cursor.x;
28523 cx1 = cx0 + w->phys_cursor_width;
28524 if (x0 > cx0 || (x1 >= 0 && x1 < cx1))
28525 return;
28527 /* The cursor image will be completely removed from the
28528 screen if the output area intersects the cursor area in
28529 y-direction. When we draw in [y0 y1[, and some part of
28530 the cursor is at y < y0, that part must have been drawn
28531 before. When scrolling, the cursor is erased before
28532 actually scrolling, so we don't come here. When not
28533 scrolling, the rows above the old cursor row must have
28534 changed, and in this case these rows must have written
28535 over the cursor image.
28537 Likewise if part of the cursor is below y1, with the
28538 exception of the cursor being in the first blank row at
28539 the buffer and window end because update_text_area
28540 doesn't draw that row. (Except when it does, but
28541 that's handled in update_text_area.) */
28543 cy0 = w->phys_cursor.y;
28544 cy1 = cy0 + w->phys_cursor_height;
28545 if ((y0 < cy0 || y0 >= cy1) && (y1 <= cy0 || y1 >= cy1))
28546 return;
28548 w->phys_cursor_on_p = false;
28551 #endif /* HAVE_WINDOW_SYSTEM */
28554 /************************************************************************
28555 Mouse Face
28556 ************************************************************************/
28558 #ifdef HAVE_WINDOW_SYSTEM
28560 /* EXPORT for RIF:
28561 Fix the display of area AREA of overlapping row ROW in window W
28562 with respect to the overlapping part OVERLAPS. */
28564 void
28565 x_fix_overlapping_area (struct window *w, struct glyph_row *row,
28566 enum glyph_row_area area, int overlaps)
28568 int i, x;
28570 block_input ();
28572 x = 0;
28573 for (i = 0; i < row->used[area];)
28575 if (row->glyphs[area][i].overlaps_vertically_p)
28577 int start = i, start_x = x;
28581 x += row->glyphs[area][i].pixel_width;
28582 ++i;
28584 while (i < row->used[area]
28585 && row->glyphs[area][i].overlaps_vertically_p);
28587 draw_glyphs (w, start_x, row, area,
28588 start, i,
28589 DRAW_NORMAL_TEXT, overlaps);
28591 else
28593 x += row->glyphs[area][i].pixel_width;
28594 ++i;
28598 unblock_input ();
28602 /* EXPORT:
28603 Draw the cursor glyph of window W in glyph row ROW. See the
28604 comment of draw_glyphs for the meaning of HL. */
28606 void
28607 draw_phys_cursor_glyph (struct window *w, struct glyph_row *row,
28608 enum draw_glyphs_face hl)
28610 /* If cursor hpos is out of bounds, don't draw garbage. This can
28611 happen in mini-buffer windows when switching between echo area
28612 glyphs and mini-buffer. */
28613 if ((row->reversed_p
28614 ? (w->phys_cursor.hpos >= 0)
28615 : (w->phys_cursor.hpos < row->used[TEXT_AREA])))
28617 bool on_p = w->phys_cursor_on_p;
28618 int x1;
28619 int hpos = w->phys_cursor.hpos;
28621 /* When the window is hscrolled, cursor hpos can legitimately be
28622 out of bounds, but we draw the cursor at the corresponding
28623 window margin in that case. */
28624 if (!row->reversed_p && hpos < 0)
28625 hpos = 0;
28626 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
28627 hpos = row->used[TEXT_AREA] - 1;
28629 x1 = draw_glyphs (w, w->phys_cursor.x, row, TEXT_AREA, hpos, hpos + 1,
28630 hl, 0);
28631 w->phys_cursor_on_p = on_p;
28633 if (hl == DRAW_CURSOR)
28634 w->phys_cursor_width = x1 - w->phys_cursor.x;
28635 /* When we erase the cursor, and ROW is overlapped by other
28636 rows, make sure that these overlapping parts of other rows
28637 are redrawn. */
28638 else if (hl == DRAW_NORMAL_TEXT && row->overlapped_p)
28640 w->phys_cursor_width = x1 - w->phys_cursor.x;
28642 if (row > w->current_matrix->rows
28643 && MATRIX_ROW_OVERLAPS_SUCC_P (row - 1))
28644 x_fix_overlapping_area (w, row - 1, TEXT_AREA,
28645 OVERLAPS_ERASED_CURSOR);
28647 if (MATRIX_ROW_BOTTOM_Y (row) < window_text_bottom_y (w)
28648 && MATRIX_ROW_OVERLAPS_PRED_P (row + 1))
28649 x_fix_overlapping_area (w, row + 1, TEXT_AREA,
28650 OVERLAPS_ERASED_CURSOR);
28656 /* Erase the image of a cursor of window W from the screen. */
28658 void
28659 erase_phys_cursor (struct window *w)
28661 struct frame *f = XFRAME (w->frame);
28662 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
28663 int hpos = w->phys_cursor.hpos;
28664 int vpos = w->phys_cursor.vpos;
28665 bool mouse_face_here_p = false;
28666 struct glyph_matrix *active_glyphs = w->current_matrix;
28667 struct glyph_row *cursor_row;
28668 struct glyph *cursor_glyph;
28669 enum draw_glyphs_face hl;
28671 /* No cursor displayed or row invalidated => nothing to do on the
28672 screen. */
28673 if (w->phys_cursor_type == NO_CURSOR)
28674 goto mark_cursor_off;
28676 /* VPOS >= active_glyphs->nrows means that window has been resized.
28677 Don't bother to erase the cursor. */
28678 if (vpos >= active_glyphs->nrows)
28679 goto mark_cursor_off;
28681 /* If row containing cursor is marked invalid, there is nothing we
28682 can do. */
28683 cursor_row = MATRIX_ROW (active_glyphs, vpos);
28684 if (!cursor_row->enabled_p)
28685 goto mark_cursor_off;
28687 /* If line spacing is > 0, old cursor may only be partially visible in
28688 window after split-window. So adjust visible height. */
28689 cursor_row->visible_height = min (cursor_row->visible_height,
28690 window_text_bottom_y (w) - cursor_row->y);
28692 /* If row is completely invisible, don't attempt to delete a cursor which
28693 isn't there. This can happen if cursor is at top of a window, and
28694 we switch to a buffer with a header line in that window. */
28695 if (cursor_row->visible_height <= 0)
28696 goto mark_cursor_off;
28698 /* If cursor is in the fringe, erase by drawing actual bitmap there. */
28699 if (cursor_row->cursor_in_fringe_p)
28701 cursor_row->cursor_in_fringe_p = false;
28702 draw_fringe_bitmap (w, cursor_row, cursor_row->reversed_p);
28703 goto mark_cursor_off;
28706 /* This can happen when the new row is shorter than the old one.
28707 In this case, either draw_glyphs or clear_end_of_line
28708 should have cleared the cursor. Note that we wouldn't be
28709 able to erase the cursor in this case because we don't have a
28710 cursor glyph at hand. */
28711 if ((cursor_row->reversed_p
28712 ? (w->phys_cursor.hpos < 0)
28713 : (w->phys_cursor.hpos >= cursor_row->used[TEXT_AREA])))
28714 goto mark_cursor_off;
28716 /* When the window is hscrolled, cursor hpos can legitimately be out
28717 of bounds, but we draw the cursor at the corresponding window
28718 margin in that case. */
28719 if (!cursor_row->reversed_p && hpos < 0)
28720 hpos = 0;
28721 if (cursor_row->reversed_p && hpos >= cursor_row->used[TEXT_AREA])
28722 hpos = cursor_row->used[TEXT_AREA] - 1;
28724 /* If the cursor is in the mouse face area, redisplay that when
28725 we clear the cursor. */
28726 if (! NILP (hlinfo->mouse_face_window)
28727 && coords_in_mouse_face_p (w, hpos, vpos)
28728 /* Don't redraw the cursor's spot in mouse face if it is at the
28729 end of a line (on a newline). The cursor appears there, but
28730 mouse highlighting does not. */
28731 && cursor_row->used[TEXT_AREA] > hpos && hpos >= 0)
28732 mouse_face_here_p = true;
28734 /* Maybe clear the display under the cursor. */
28735 if (w->phys_cursor_type == HOLLOW_BOX_CURSOR)
28737 int x, y;
28738 int header_line_height = WINDOW_HEADER_LINE_HEIGHT (w);
28739 int width;
28741 cursor_glyph = get_phys_cursor_glyph (w);
28742 if (cursor_glyph == NULL)
28743 goto mark_cursor_off;
28745 width = cursor_glyph->pixel_width;
28746 x = w->phys_cursor.x;
28747 if (x < 0)
28749 width += x;
28750 x = 0;
28752 width = min (width, window_box_width (w, TEXT_AREA) - x);
28753 y = WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height, cursor_row->y));
28754 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, x);
28756 if (width > 0)
28757 FRAME_RIF (f)->clear_frame_area (f, x, y, width, cursor_row->visible_height);
28760 /* Erase the cursor by redrawing the character underneath it. */
28761 if (mouse_face_here_p)
28762 hl = DRAW_MOUSE_FACE;
28763 else
28764 hl = DRAW_NORMAL_TEXT;
28765 draw_phys_cursor_glyph (w, cursor_row, hl);
28767 mark_cursor_off:
28768 w->phys_cursor_on_p = false;
28769 w->phys_cursor_type = NO_CURSOR;
28773 /* Display or clear cursor of window W. If !ON, clear the cursor.
28774 If ON, display the cursor; where to put the cursor is specified by
28775 HPOS, VPOS, X and Y. */
28777 void
28778 display_and_set_cursor (struct window *w, bool on,
28779 int hpos, int vpos, int x, int y)
28781 struct frame *f = XFRAME (w->frame);
28782 int new_cursor_type;
28783 int new_cursor_width;
28784 bool active_cursor;
28785 struct glyph_row *glyph_row;
28786 struct glyph *glyph;
28788 /* This is pointless on invisible frames, and dangerous on garbaged
28789 windows and frames; in the latter case, the frame or window may
28790 be in the midst of changing its size, and x and y may be off the
28791 window. */
28792 if (! FRAME_VISIBLE_P (f)
28793 || FRAME_GARBAGED_P (f)
28794 || vpos >= w->current_matrix->nrows
28795 || hpos >= w->current_matrix->matrix_w)
28796 return;
28798 /* If cursor is off and we want it off, return quickly. */
28799 if (!on && !w->phys_cursor_on_p)
28800 return;
28802 glyph_row = MATRIX_ROW (w->current_matrix, vpos);
28803 /* If cursor row is not enabled, we don't really know where to
28804 display the cursor. */
28805 if (!glyph_row->enabled_p)
28807 w->phys_cursor_on_p = false;
28808 return;
28811 glyph = NULL;
28812 if (0 <= hpos && hpos < glyph_row->used[TEXT_AREA])
28813 glyph = glyph_row->glyphs[TEXT_AREA] + hpos;
28815 eassert (input_blocked_p ());
28817 /* Set new_cursor_type to the cursor we want to be displayed. */
28818 new_cursor_type = get_window_cursor_type (w, glyph,
28819 &new_cursor_width, &active_cursor);
28821 /* If cursor is currently being shown and we don't want it to be or
28822 it is in the wrong place, or the cursor type is not what we want,
28823 erase it. */
28824 if (w->phys_cursor_on_p
28825 && (!on
28826 || w->phys_cursor.x != x
28827 || w->phys_cursor.y != y
28828 /* HPOS can be negative in R2L rows whose
28829 exact_window_width_line_p flag is set (i.e. their newline
28830 would "overflow into the fringe"). */
28831 || hpos < 0
28832 || new_cursor_type != w->phys_cursor_type
28833 || ((new_cursor_type == BAR_CURSOR || new_cursor_type == HBAR_CURSOR)
28834 && new_cursor_width != w->phys_cursor_width)))
28835 erase_phys_cursor (w);
28837 /* Don't check phys_cursor_on_p here because that flag is only set
28838 to false in some cases where we know that the cursor has been
28839 completely erased, to avoid the extra work of erasing the cursor
28840 twice. In other words, phys_cursor_on_p can be true and the cursor
28841 still not be visible, or it has only been partly erased. */
28842 if (on)
28844 w->phys_cursor_ascent = glyph_row->ascent;
28845 w->phys_cursor_height = glyph_row->height;
28847 /* Set phys_cursor_.* before x_draw_.* is called because some
28848 of them may need the information. */
28849 w->phys_cursor.x = x;
28850 w->phys_cursor.y = glyph_row->y;
28851 w->phys_cursor.hpos = hpos;
28852 w->phys_cursor.vpos = vpos;
28855 FRAME_RIF (f)->draw_window_cursor (w, glyph_row, x, y,
28856 new_cursor_type, new_cursor_width,
28857 on, active_cursor);
28861 /* Switch the display of W's cursor on or off, according to the value
28862 of ON. */
28864 static void
28865 update_window_cursor (struct window *w, bool on)
28867 /* Don't update cursor in windows whose frame is in the process
28868 of being deleted. */
28869 if (w->current_matrix)
28871 int hpos = w->phys_cursor.hpos;
28872 int vpos = w->phys_cursor.vpos;
28873 struct glyph_row *row;
28875 if (vpos >= w->current_matrix->nrows
28876 || hpos >= w->current_matrix->matrix_w)
28877 return;
28879 row = MATRIX_ROW (w->current_matrix, vpos);
28881 /* When the window is hscrolled, cursor hpos can legitimately be
28882 out of bounds, but we draw the cursor at the corresponding
28883 window margin in that case. */
28884 if (!row->reversed_p && hpos < 0)
28885 hpos = 0;
28886 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
28887 hpos = row->used[TEXT_AREA] - 1;
28889 block_input ();
28890 display_and_set_cursor (w, on, hpos, vpos,
28891 w->phys_cursor.x, w->phys_cursor.y);
28892 unblock_input ();
28897 /* Call update_window_cursor with parameter ON_P on all leaf windows
28898 in the window tree rooted at W. */
28900 static void
28901 update_cursor_in_window_tree (struct window *w, bool on_p)
28903 while (w)
28905 if (WINDOWP (w->contents))
28906 update_cursor_in_window_tree (XWINDOW (w->contents), on_p);
28907 else
28908 update_window_cursor (w, on_p);
28910 w = NILP (w->next) ? 0 : XWINDOW (w->next);
28915 /* EXPORT:
28916 Display the cursor on window W, or clear it, according to ON_P.
28917 Don't change the cursor's position. */
28919 void
28920 x_update_cursor (struct frame *f, bool on_p)
28922 update_cursor_in_window_tree (XWINDOW (f->root_window), on_p);
28926 /* EXPORT:
28927 Clear the cursor of window W to background color, and mark the
28928 cursor as not shown. This is used when the text where the cursor
28929 is about to be rewritten. */
28931 void
28932 x_clear_cursor (struct window *w)
28934 if (FRAME_VISIBLE_P (XFRAME (w->frame)) && w->phys_cursor_on_p)
28935 update_window_cursor (w, false);
28938 #endif /* HAVE_WINDOW_SYSTEM */
28940 /* Implementation of draw_row_with_mouse_face for GUI sessions, GPM,
28941 and MSDOS. */
28942 static void
28943 draw_row_with_mouse_face (struct window *w, int start_x, struct glyph_row *row,
28944 int start_hpos, int end_hpos,
28945 enum draw_glyphs_face draw)
28947 #ifdef HAVE_WINDOW_SYSTEM
28948 if (FRAME_WINDOW_P (XFRAME (w->frame)))
28950 draw_glyphs (w, start_x, row, TEXT_AREA, start_hpos, end_hpos, draw, 0);
28951 return;
28953 #endif
28954 #if defined (HAVE_GPM) || defined (MSDOS) || defined (WINDOWSNT)
28955 tty_draw_row_with_mouse_face (w, row, start_hpos, end_hpos, draw);
28956 #endif
28959 /* Display the active region described by mouse_face_* according to DRAW. */
28961 static void
28962 show_mouse_face (Mouse_HLInfo *hlinfo, enum draw_glyphs_face draw)
28964 struct window *w = XWINDOW (hlinfo->mouse_face_window);
28965 struct frame *f = XFRAME (WINDOW_FRAME (w));
28967 if (/* If window is in the process of being destroyed, don't bother
28968 to do anything. */
28969 w->current_matrix != NULL
28970 /* Don't update mouse highlight if hidden. */
28971 && (draw != DRAW_MOUSE_FACE || !hlinfo->mouse_face_hidden)
28972 /* Recognize when we are called to operate on rows that don't exist
28973 anymore. This can happen when a window is split. */
28974 && hlinfo->mouse_face_end_row < w->current_matrix->nrows)
28976 bool phys_cursor_on_p = w->phys_cursor_on_p;
28977 struct glyph_row *row, *first, *last;
28979 first = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_beg_row);
28980 last = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_end_row);
28982 for (row = first; row <= last && row->enabled_p; ++row)
28984 int start_hpos, end_hpos, start_x;
28986 /* For all but the first row, the highlight starts at column 0. */
28987 if (row == first)
28989 /* R2L rows have BEG and END in reversed order, but the
28990 screen drawing geometry is always left to right. So
28991 we need to mirror the beginning and end of the
28992 highlighted area in R2L rows. */
28993 if (!row->reversed_p)
28995 start_hpos = hlinfo->mouse_face_beg_col;
28996 start_x = hlinfo->mouse_face_beg_x;
28998 else if (row == last)
29000 start_hpos = hlinfo->mouse_face_end_col;
29001 start_x = hlinfo->mouse_face_end_x;
29003 else
29005 start_hpos = 0;
29006 start_x = 0;
29009 else if (row->reversed_p && row == last)
29011 start_hpos = hlinfo->mouse_face_end_col;
29012 start_x = hlinfo->mouse_face_end_x;
29014 else
29016 start_hpos = 0;
29017 start_x = 0;
29020 if (row == last)
29022 if (!row->reversed_p)
29023 end_hpos = hlinfo->mouse_face_end_col;
29024 else if (row == first)
29025 end_hpos = hlinfo->mouse_face_beg_col;
29026 else
29028 end_hpos = row->used[TEXT_AREA];
29029 if (draw == DRAW_NORMAL_TEXT)
29030 row->fill_line_p = true; /* Clear to end of line. */
29033 else if (row->reversed_p && row == first)
29034 end_hpos = hlinfo->mouse_face_beg_col;
29035 else
29037 end_hpos = row->used[TEXT_AREA];
29038 if (draw == DRAW_NORMAL_TEXT)
29039 row->fill_line_p = true; /* Clear to end of line. */
29042 if (end_hpos > start_hpos)
29044 draw_row_with_mouse_face (w, start_x, row,
29045 start_hpos, end_hpos, draw);
29047 row->mouse_face_p
29048 = draw == DRAW_MOUSE_FACE || draw == DRAW_IMAGE_RAISED;
29052 /* When we've written over the cursor, arrange for it to
29053 be displayed again. */
29054 if (FRAME_WINDOW_P (f)
29055 && phys_cursor_on_p && !w->phys_cursor_on_p)
29057 #ifdef HAVE_WINDOW_SYSTEM
29058 int hpos = w->phys_cursor.hpos;
29060 /* When the window is hscrolled, cursor hpos can legitimately be
29061 out of bounds, but we draw the cursor at the corresponding
29062 window margin in that case. */
29063 if (!row->reversed_p && hpos < 0)
29064 hpos = 0;
29065 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
29066 hpos = row->used[TEXT_AREA] - 1;
29068 block_input ();
29069 display_and_set_cursor (w, true, hpos, w->phys_cursor.vpos,
29070 w->phys_cursor.x, w->phys_cursor.y);
29071 unblock_input ();
29072 #endif /* HAVE_WINDOW_SYSTEM */
29076 #ifdef HAVE_WINDOW_SYSTEM
29077 /* Change the mouse cursor. */
29078 if (FRAME_WINDOW_P (f) && NILP (do_mouse_tracking))
29080 #if ! defined (USE_GTK) && ! defined (HAVE_NS)
29081 if (draw == DRAW_NORMAL_TEXT
29082 && !EQ (hlinfo->mouse_face_window, f->tool_bar_window))
29083 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->text_cursor);
29084 else
29085 #endif
29086 if (draw == DRAW_MOUSE_FACE)
29087 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->hand_cursor);
29088 else
29089 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->nontext_cursor);
29091 #endif /* HAVE_WINDOW_SYSTEM */
29094 /* EXPORT:
29095 Clear out the mouse-highlighted active region.
29096 Redraw it un-highlighted first. Value is true if mouse
29097 face was actually drawn unhighlighted. */
29099 bool
29100 clear_mouse_face (Mouse_HLInfo *hlinfo)
29102 bool cleared
29103 = !hlinfo->mouse_face_hidden && !NILP (hlinfo->mouse_face_window);
29104 if (cleared)
29105 show_mouse_face (hlinfo, DRAW_NORMAL_TEXT);
29106 hlinfo->mouse_face_beg_row = hlinfo->mouse_face_beg_col = -1;
29107 hlinfo->mouse_face_end_row = hlinfo->mouse_face_end_col = -1;
29108 hlinfo->mouse_face_window = Qnil;
29109 hlinfo->mouse_face_overlay = Qnil;
29110 return cleared;
29113 /* Return true if the coordinates HPOS and VPOS on windows W are
29114 within the mouse face on that window. */
29115 static bool
29116 coords_in_mouse_face_p (struct window *w, int hpos, int vpos)
29118 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (XFRAME (w->frame));
29120 /* Quickly resolve the easy cases. */
29121 if (!(WINDOWP (hlinfo->mouse_face_window)
29122 && XWINDOW (hlinfo->mouse_face_window) == w))
29123 return false;
29124 if (vpos < hlinfo->mouse_face_beg_row
29125 || vpos > hlinfo->mouse_face_end_row)
29126 return false;
29127 if (vpos > hlinfo->mouse_face_beg_row
29128 && vpos < hlinfo->mouse_face_end_row)
29129 return true;
29131 if (!MATRIX_ROW (w->current_matrix, vpos)->reversed_p)
29133 if (hlinfo->mouse_face_beg_row == hlinfo->mouse_face_end_row)
29135 if (hlinfo->mouse_face_beg_col <= hpos && hpos < hlinfo->mouse_face_end_col)
29136 return true;
29138 else if ((vpos == hlinfo->mouse_face_beg_row
29139 && hpos >= hlinfo->mouse_face_beg_col)
29140 || (vpos == hlinfo->mouse_face_end_row
29141 && hpos < hlinfo->mouse_face_end_col))
29142 return true;
29144 else
29146 if (hlinfo->mouse_face_beg_row == hlinfo->mouse_face_end_row)
29148 if (hlinfo->mouse_face_end_col < hpos && hpos <= hlinfo->mouse_face_beg_col)
29149 return true;
29151 else if ((vpos == hlinfo->mouse_face_beg_row
29152 && hpos <= hlinfo->mouse_face_beg_col)
29153 || (vpos == hlinfo->mouse_face_end_row
29154 && hpos > hlinfo->mouse_face_end_col))
29155 return true;
29157 return false;
29161 /* EXPORT:
29162 True if physical cursor of window W is within mouse face. */
29164 bool
29165 cursor_in_mouse_face_p (struct window *w)
29167 int hpos = w->phys_cursor.hpos;
29168 int vpos = w->phys_cursor.vpos;
29169 struct glyph_row *row = MATRIX_ROW (w->current_matrix, vpos);
29171 /* When the window is hscrolled, cursor hpos can legitimately be out
29172 of bounds, but we draw the cursor at the corresponding window
29173 margin in that case. */
29174 if (!row->reversed_p && hpos < 0)
29175 hpos = 0;
29176 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
29177 hpos = row->used[TEXT_AREA] - 1;
29179 return coords_in_mouse_face_p (w, hpos, vpos);
29184 /* Find the glyph rows START_ROW and END_ROW of window W that display
29185 characters between buffer positions START_CHARPOS and END_CHARPOS
29186 (excluding END_CHARPOS). DISP_STRING is a display string that
29187 covers these buffer positions. This is similar to
29188 row_containing_pos, but is more accurate when bidi reordering makes
29189 buffer positions change non-linearly with glyph rows. */
29190 static void
29191 rows_from_pos_range (struct window *w,
29192 ptrdiff_t start_charpos, ptrdiff_t end_charpos,
29193 Lisp_Object disp_string,
29194 struct glyph_row **start, struct glyph_row **end)
29196 struct glyph_row *first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
29197 int last_y = window_text_bottom_y (w);
29198 struct glyph_row *row;
29200 *start = NULL;
29201 *end = NULL;
29203 while (!first->enabled_p
29204 && first < MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w))
29205 first++;
29207 /* Find the START row. */
29208 for (row = first;
29209 row->enabled_p && MATRIX_ROW_BOTTOM_Y (row) <= last_y;
29210 row++)
29212 /* A row can potentially be the START row if the range of the
29213 characters it displays intersects the range
29214 [START_CHARPOS..END_CHARPOS). */
29215 if (! ((start_charpos < MATRIX_ROW_START_CHARPOS (row)
29216 && end_charpos < MATRIX_ROW_START_CHARPOS (row))
29217 /* See the commentary in row_containing_pos, for the
29218 explanation of the complicated way to check whether
29219 some position is beyond the end of the characters
29220 displayed by a row. */
29221 || ((start_charpos > MATRIX_ROW_END_CHARPOS (row)
29222 || (start_charpos == MATRIX_ROW_END_CHARPOS (row)
29223 && !row->ends_at_zv_p
29224 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
29225 && (end_charpos > MATRIX_ROW_END_CHARPOS (row)
29226 || (end_charpos == MATRIX_ROW_END_CHARPOS (row)
29227 && !row->ends_at_zv_p
29228 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))))))
29230 /* Found a candidate row. Now make sure at least one of the
29231 glyphs it displays has a charpos from the range
29232 [START_CHARPOS..END_CHARPOS).
29234 This is not obvious because bidi reordering could make
29235 buffer positions of a row be 1,2,3,102,101,100, and if we
29236 want to highlight characters in [50..60), we don't want
29237 this row, even though [50..60) does intersect [1..103),
29238 the range of character positions given by the row's start
29239 and end positions. */
29240 struct glyph *g = row->glyphs[TEXT_AREA];
29241 struct glyph *e = g + row->used[TEXT_AREA];
29243 while (g < e)
29245 if (((BUFFERP (g->object) || NILP (g->object))
29246 && start_charpos <= g->charpos && g->charpos < end_charpos)
29247 /* A glyph that comes from DISP_STRING is by
29248 definition to be highlighted. */
29249 || EQ (g->object, disp_string))
29250 *start = row;
29251 g++;
29253 if (*start)
29254 break;
29258 /* Find the END row. */
29259 if (!*start
29260 /* If the last row is partially visible, start looking for END
29261 from that row, instead of starting from FIRST. */
29262 && !(row->enabled_p
29263 && row->y < last_y && MATRIX_ROW_BOTTOM_Y (row) > last_y))
29264 row = first;
29265 for ( ; row->enabled_p && MATRIX_ROW_BOTTOM_Y (row) <= last_y; row++)
29267 struct glyph_row *next = row + 1;
29268 ptrdiff_t next_start = MATRIX_ROW_START_CHARPOS (next);
29270 if (!next->enabled_p
29271 || next >= MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w)
29272 /* The first row >= START whose range of displayed characters
29273 does NOT intersect the range [START_CHARPOS..END_CHARPOS]
29274 is the row END + 1. */
29275 || (start_charpos < next_start
29276 && end_charpos < next_start)
29277 || ((start_charpos > MATRIX_ROW_END_CHARPOS (next)
29278 || (start_charpos == MATRIX_ROW_END_CHARPOS (next)
29279 && !next->ends_at_zv_p
29280 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (next)))
29281 && (end_charpos > MATRIX_ROW_END_CHARPOS (next)
29282 || (end_charpos == MATRIX_ROW_END_CHARPOS (next)
29283 && !next->ends_at_zv_p
29284 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (next)))))
29286 *end = row;
29287 break;
29289 else
29291 /* If the next row's edges intersect [START_CHARPOS..END_CHARPOS],
29292 but none of the characters it displays are in the range, it is
29293 also END + 1. */
29294 struct glyph *g = next->glyphs[TEXT_AREA];
29295 struct glyph *s = g;
29296 struct glyph *e = g + next->used[TEXT_AREA];
29298 while (g < e)
29300 if (((BUFFERP (g->object) || NILP (g->object))
29301 && ((start_charpos <= g->charpos && g->charpos < end_charpos)
29302 /* If the buffer position of the first glyph in
29303 the row is equal to END_CHARPOS, it means
29304 the last character to be highlighted is the
29305 newline of ROW, and we must consider NEXT as
29306 END, not END+1. */
29307 || (((!next->reversed_p && g == s)
29308 || (next->reversed_p && g == e - 1))
29309 && (g->charpos == end_charpos
29310 /* Special case for when NEXT is an
29311 empty line at ZV. */
29312 || (g->charpos == -1
29313 && !row->ends_at_zv_p
29314 && next_start == end_charpos)))))
29315 /* A glyph that comes from DISP_STRING is by
29316 definition to be highlighted. */
29317 || EQ (g->object, disp_string))
29318 break;
29319 g++;
29321 if (g == e)
29323 *end = row;
29324 break;
29326 /* The first row that ends at ZV must be the last to be
29327 highlighted. */
29328 else if (next->ends_at_zv_p)
29330 *end = next;
29331 break;
29337 /* This function sets the mouse_face_* elements of HLINFO, assuming
29338 the mouse cursor is on a glyph with buffer charpos MOUSE_CHARPOS in
29339 window WINDOW. START_CHARPOS and END_CHARPOS are buffer positions
29340 for the overlay or run of text properties specifying the mouse
29341 face. BEFORE_STRING and AFTER_STRING, if non-nil, are a
29342 before-string and after-string that must also be highlighted.
29343 DISP_STRING, if non-nil, is a display string that may cover some
29344 or all of the highlighted text. */
29346 static void
29347 mouse_face_from_buffer_pos (Lisp_Object window,
29348 Mouse_HLInfo *hlinfo,
29349 ptrdiff_t mouse_charpos,
29350 ptrdiff_t start_charpos,
29351 ptrdiff_t end_charpos,
29352 Lisp_Object before_string,
29353 Lisp_Object after_string,
29354 Lisp_Object disp_string)
29356 struct window *w = XWINDOW (window);
29357 struct glyph_row *first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
29358 struct glyph_row *r1, *r2;
29359 struct glyph *glyph, *end;
29360 ptrdiff_t ignore, pos;
29361 int x;
29363 eassert (NILP (disp_string) || STRINGP (disp_string));
29364 eassert (NILP (before_string) || STRINGP (before_string));
29365 eassert (NILP (after_string) || STRINGP (after_string));
29367 /* Find the rows corresponding to START_CHARPOS and END_CHARPOS. */
29368 rows_from_pos_range (w, start_charpos, end_charpos, disp_string, &r1, &r2);
29369 if (r1 == NULL)
29370 r1 = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
29371 /* If the before-string or display-string contains newlines,
29372 rows_from_pos_range skips to its last row. Move back. */
29373 if (!NILP (before_string) || !NILP (disp_string))
29375 struct glyph_row *prev;
29376 while ((prev = r1 - 1, prev >= first)
29377 && MATRIX_ROW_END_CHARPOS (prev) == start_charpos
29378 && prev->used[TEXT_AREA] > 0)
29380 struct glyph *beg = prev->glyphs[TEXT_AREA];
29381 glyph = beg + prev->used[TEXT_AREA];
29382 while (--glyph >= beg && NILP (glyph->object));
29383 if (glyph < beg
29384 || !(EQ (glyph->object, before_string)
29385 || EQ (glyph->object, disp_string)))
29386 break;
29387 r1 = prev;
29390 if (r2 == NULL)
29392 r2 = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
29393 hlinfo->mouse_face_past_end = true;
29395 else if (!NILP (after_string))
29397 /* If the after-string has newlines, advance to its last row. */
29398 struct glyph_row *next;
29399 struct glyph_row *last
29400 = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
29402 for (next = r2 + 1;
29403 next <= last
29404 && next->used[TEXT_AREA] > 0
29405 && EQ (next->glyphs[TEXT_AREA]->object, after_string);
29406 ++next)
29407 r2 = next;
29409 /* The rest of the display engine assumes that mouse_face_beg_row is
29410 either above mouse_face_end_row or identical to it. But with
29411 bidi-reordered continued lines, the row for START_CHARPOS could
29412 be below the row for END_CHARPOS. If so, swap the rows and store
29413 them in correct order. */
29414 if (r1->y > r2->y)
29416 struct glyph_row *tem = r2;
29418 r2 = r1;
29419 r1 = tem;
29422 hlinfo->mouse_face_beg_row = MATRIX_ROW_VPOS (r1, w->current_matrix);
29423 hlinfo->mouse_face_end_row = MATRIX_ROW_VPOS (r2, w->current_matrix);
29425 /* For a bidi-reordered row, the positions of BEFORE_STRING,
29426 AFTER_STRING, DISP_STRING, START_CHARPOS, and END_CHARPOS
29427 could be anywhere in the row and in any order. The strategy
29428 below is to find the leftmost and the rightmost glyph that
29429 belongs to either of these 3 strings, or whose position is
29430 between START_CHARPOS and END_CHARPOS, and highlight all the
29431 glyphs between those two. This may cover more than just the text
29432 between START_CHARPOS and END_CHARPOS if the range of characters
29433 strides the bidi level boundary, e.g. if the beginning is in R2L
29434 text while the end is in L2R text or vice versa. */
29435 if (!r1->reversed_p)
29437 /* This row is in a left to right paragraph. Scan it left to
29438 right. */
29439 glyph = r1->glyphs[TEXT_AREA];
29440 end = glyph + r1->used[TEXT_AREA];
29441 x = r1->x;
29443 /* Skip truncation glyphs at the start of the glyph row. */
29444 if (MATRIX_ROW_DISPLAYS_TEXT_P (r1))
29445 for (; glyph < end
29446 && NILP (glyph->object)
29447 && glyph->charpos < 0;
29448 ++glyph)
29449 x += glyph->pixel_width;
29451 /* Scan the glyph row, looking for BEFORE_STRING, AFTER_STRING,
29452 or DISP_STRING, and the first glyph from buffer whose
29453 position is between START_CHARPOS and END_CHARPOS. */
29454 for (; glyph < end
29455 && !NILP (glyph->object)
29456 && !EQ (glyph->object, disp_string)
29457 && !(BUFFERP (glyph->object)
29458 && (glyph->charpos >= start_charpos
29459 && glyph->charpos < end_charpos));
29460 ++glyph)
29462 /* BEFORE_STRING or AFTER_STRING are only relevant if they
29463 are present at buffer positions between START_CHARPOS and
29464 END_CHARPOS, or if they come from an overlay. */
29465 if (EQ (glyph->object, before_string))
29467 pos = string_buffer_position (before_string,
29468 start_charpos);
29469 /* If pos == 0, it means before_string came from an
29470 overlay, not from a buffer position. */
29471 if (!pos || (pos >= start_charpos && pos < end_charpos))
29472 break;
29474 else if (EQ (glyph->object, after_string))
29476 pos = string_buffer_position (after_string, end_charpos);
29477 if (!pos || (pos >= start_charpos && pos < end_charpos))
29478 break;
29480 x += glyph->pixel_width;
29482 hlinfo->mouse_face_beg_x = x;
29483 hlinfo->mouse_face_beg_col = glyph - r1->glyphs[TEXT_AREA];
29485 else
29487 /* This row is in a right to left paragraph. Scan it right to
29488 left. */
29489 struct glyph *g;
29491 end = r1->glyphs[TEXT_AREA] - 1;
29492 glyph = end + r1->used[TEXT_AREA];
29494 /* Skip truncation glyphs at the start of the glyph row. */
29495 if (MATRIX_ROW_DISPLAYS_TEXT_P (r1))
29496 for (; glyph > end
29497 && NILP (glyph->object)
29498 && glyph->charpos < 0;
29499 --glyph)
29502 /* Scan the glyph row, looking for BEFORE_STRING, AFTER_STRING,
29503 or DISP_STRING, and the first glyph from buffer whose
29504 position is between START_CHARPOS and END_CHARPOS. */
29505 for (; glyph > end
29506 && !NILP (glyph->object)
29507 && !EQ (glyph->object, disp_string)
29508 && !(BUFFERP (glyph->object)
29509 && (glyph->charpos >= start_charpos
29510 && glyph->charpos < end_charpos));
29511 --glyph)
29513 /* BEFORE_STRING or AFTER_STRING are only relevant if they
29514 are present at buffer positions between START_CHARPOS and
29515 END_CHARPOS, or if they come from an overlay. */
29516 if (EQ (glyph->object, before_string))
29518 pos = string_buffer_position (before_string, start_charpos);
29519 /* If pos == 0, it means before_string came from an
29520 overlay, not from a buffer position. */
29521 if (!pos || (pos >= start_charpos && pos < end_charpos))
29522 break;
29524 else if (EQ (glyph->object, after_string))
29526 pos = string_buffer_position (after_string, end_charpos);
29527 if (!pos || (pos >= start_charpos && pos < end_charpos))
29528 break;
29532 glyph++; /* first glyph to the right of the highlighted area */
29533 for (g = r1->glyphs[TEXT_AREA], x = r1->x; g < glyph; g++)
29534 x += g->pixel_width;
29535 hlinfo->mouse_face_beg_x = x;
29536 hlinfo->mouse_face_beg_col = glyph - r1->glyphs[TEXT_AREA];
29539 /* If the highlight ends in a different row, compute GLYPH and END
29540 for the end row. Otherwise, reuse the values computed above for
29541 the row where the highlight begins. */
29542 if (r2 != r1)
29544 if (!r2->reversed_p)
29546 glyph = r2->glyphs[TEXT_AREA];
29547 end = glyph + r2->used[TEXT_AREA];
29548 x = r2->x;
29550 else
29552 end = r2->glyphs[TEXT_AREA] - 1;
29553 glyph = end + r2->used[TEXT_AREA];
29557 if (!r2->reversed_p)
29559 /* Skip truncation and continuation glyphs near the end of the
29560 row, and also blanks and stretch glyphs inserted by
29561 extend_face_to_end_of_line. */
29562 while (end > glyph
29563 && NILP ((end - 1)->object))
29564 --end;
29565 /* Scan the rest of the glyph row from the end, looking for the
29566 first glyph that comes from BEFORE_STRING, AFTER_STRING, or
29567 DISP_STRING, or whose position is between START_CHARPOS
29568 and END_CHARPOS */
29569 for (--end;
29570 end > glyph
29571 && !NILP (end->object)
29572 && !EQ (end->object, disp_string)
29573 && !(BUFFERP (end->object)
29574 && (end->charpos >= start_charpos
29575 && end->charpos < end_charpos));
29576 --end)
29578 /* BEFORE_STRING or AFTER_STRING are only relevant if they
29579 are present at buffer positions between START_CHARPOS and
29580 END_CHARPOS, or if they come from an overlay. */
29581 if (EQ (end->object, before_string))
29583 pos = string_buffer_position (before_string, start_charpos);
29584 if (!pos || (pos >= start_charpos && pos < end_charpos))
29585 break;
29587 else if (EQ (end->object, after_string))
29589 pos = string_buffer_position (after_string, end_charpos);
29590 if (!pos || (pos >= start_charpos && pos < end_charpos))
29591 break;
29594 /* Find the X coordinate of the last glyph to be highlighted. */
29595 for (; glyph <= end; ++glyph)
29596 x += glyph->pixel_width;
29598 hlinfo->mouse_face_end_x = x;
29599 hlinfo->mouse_face_end_col = glyph - r2->glyphs[TEXT_AREA];
29601 else
29603 /* Skip truncation and continuation glyphs near the end of the
29604 row, and also blanks and stretch glyphs inserted by
29605 extend_face_to_end_of_line. */
29606 x = r2->x;
29607 end++;
29608 while (end < glyph
29609 && NILP (end->object))
29611 x += end->pixel_width;
29612 ++end;
29614 /* Scan the rest of the glyph row from the end, looking for the
29615 first glyph that comes from BEFORE_STRING, AFTER_STRING, or
29616 DISP_STRING, or whose position is between START_CHARPOS
29617 and END_CHARPOS */
29618 for ( ;
29619 end < glyph
29620 && !NILP (end->object)
29621 && !EQ (end->object, disp_string)
29622 && !(BUFFERP (end->object)
29623 && (end->charpos >= start_charpos
29624 && end->charpos < end_charpos));
29625 ++end)
29627 /* BEFORE_STRING or AFTER_STRING are only relevant if they
29628 are present at buffer positions between START_CHARPOS and
29629 END_CHARPOS, or if they come from an overlay. */
29630 if (EQ (end->object, before_string))
29632 pos = string_buffer_position (before_string, start_charpos);
29633 if (!pos || (pos >= start_charpos && pos < end_charpos))
29634 break;
29636 else if (EQ (end->object, after_string))
29638 pos = string_buffer_position (after_string, end_charpos);
29639 if (!pos || (pos >= start_charpos && pos < end_charpos))
29640 break;
29642 x += end->pixel_width;
29644 /* If we exited the above loop because we arrived at the last
29645 glyph of the row, and its buffer position is still not in
29646 range, it means the last character in range is the preceding
29647 newline. Bump the end column and x values to get past the
29648 last glyph. */
29649 if (end == glyph
29650 && BUFFERP (end->object)
29651 && (end->charpos < start_charpos
29652 || end->charpos >= end_charpos))
29654 x += end->pixel_width;
29655 ++end;
29657 hlinfo->mouse_face_end_x = x;
29658 hlinfo->mouse_face_end_col = end - r2->glyphs[TEXT_AREA];
29661 hlinfo->mouse_face_window = window;
29662 hlinfo->mouse_face_face_id
29663 = face_at_buffer_position (w, mouse_charpos, &ignore,
29664 mouse_charpos + 1,
29665 !hlinfo->mouse_face_hidden, -1);
29666 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
29669 /* The following function is not used anymore (replaced with
29670 mouse_face_from_string_pos), but I leave it here for the time
29671 being, in case someone would. */
29673 #if false /* not used */
29675 /* Find the position of the glyph for position POS in OBJECT in
29676 window W's current matrix, and return in *X, *Y the pixel
29677 coordinates, and return in *HPOS, *VPOS the column/row of the glyph.
29679 RIGHT_P means return the position of the right edge of the glyph.
29680 !RIGHT_P means return the left edge position.
29682 If no glyph for POS exists in the matrix, return the position of
29683 the glyph with the next smaller position that is in the matrix, if
29684 RIGHT_P is false. If RIGHT_P, and no glyph for POS
29685 exists in the matrix, return the position of the glyph with the
29686 next larger position in OBJECT.
29688 Value is true if a glyph was found. */
29690 static bool
29691 fast_find_string_pos (struct window *w, ptrdiff_t pos, Lisp_Object object,
29692 int *hpos, int *vpos, int *x, int *y, bool right_p)
29694 int yb = window_text_bottom_y (w);
29695 struct glyph_row *r;
29696 struct glyph *best_glyph = NULL;
29697 struct glyph_row *best_row = NULL;
29698 int best_x = 0;
29700 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
29701 r->enabled_p && r->y < yb;
29702 ++r)
29704 struct glyph *g = r->glyphs[TEXT_AREA];
29705 struct glyph *e = g + r->used[TEXT_AREA];
29706 int gx;
29708 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
29709 if (EQ (g->object, object))
29711 if (g->charpos == pos)
29713 best_glyph = g;
29714 best_x = gx;
29715 best_row = r;
29716 goto found;
29718 else if (best_glyph == NULL
29719 || ((eabs (g->charpos - pos)
29720 < eabs (best_glyph->charpos - pos))
29721 && (right_p
29722 ? g->charpos < pos
29723 : g->charpos > pos)))
29725 best_glyph = g;
29726 best_x = gx;
29727 best_row = r;
29732 found:
29734 if (best_glyph)
29736 *x = best_x;
29737 *hpos = best_glyph - best_row->glyphs[TEXT_AREA];
29739 if (right_p)
29741 *x += best_glyph->pixel_width;
29742 ++*hpos;
29745 *y = best_row->y;
29746 *vpos = MATRIX_ROW_VPOS (best_row, w->current_matrix);
29749 return best_glyph != NULL;
29751 #endif /* not used */
29753 /* Find the positions of the first and the last glyphs in window W's
29754 current matrix that occlude positions [STARTPOS..ENDPOS) in OBJECT
29755 (assumed to be a string), and return in HLINFO's mouse_face_*
29756 members the pixel and column/row coordinates of those glyphs. */
29758 static void
29759 mouse_face_from_string_pos (struct window *w, Mouse_HLInfo *hlinfo,
29760 Lisp_Object object,
29761 ptrdiff_t startpos, ptrdiff_t endpos)
29763 int yb = window_text_bottom_y (w);
29764 struct glyph_row *r;
29765 struct glyph *g, *e;
29766 int gx;
29767 bool found = false;
29769 /* Find the glyph row with at least one position in the range
29770 [STARTPOS..ENDPOS), and the first glyph in that row whose
29771 position belongs to that range. */
29772 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
29773 r->enabled_p && r->y < yb;
29774 ++r)
29776 if (!r->reversed_p)
29778 g = r->glyphs[TEXT_AREA];
29779 e = g + r->used[TEXT_AREA];
29780 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
29781 if (EQ (g->object, object)
29782 && startpos <= g->charpos && g->charpos < endpos)
29784 hlinfo->mouse_face_beg_row
29785 = MATRIX_ROW_VPOS (r, w->current_matrix);
29786 hlinfo->mouse_face_beg_col = g - r->glyphs[TEXT_AREA];
29787 hlinfo->mouse_face_beg_x = gx;
29788 found = true;
29789 break;
29792 else
29794 struct glyph *g1;
29796 e = r->glyphs[TEXT_AREA];
29797 g = e + r->used[TEXT_AREA];
29798 for ( ; g > e; --g)
29799 if (EQ ((g-1)->object, object)
29800 && startpos <= (g-1)->charpos && (g-1)->charpos < endpos)
29802 hlinfo->mouse_face_beg_row
29803 = MATRIX_ROW_VPOS (r, w->current_matrix);
29804 hlinfo->mouse_face_beg_col = g - r->glyphs[TEXT_AREA];
29805 for (gx = r->x, g1 = r->glyphs[TEXT_AREA]; g1 < g; ++g1)
29806 gx += g1->pixel_width;
29807 hlinfo->mouse_face_beg_x = gx;
29808 found = true;
29809 break;
29812 if (found)
29813 break;
29816 if (!found)
29817 return;
29819 /* Starting with the next row, look for the first row which does NOT
29820 include any glyphs whose positions are in the range. */
29821 for (++r; r->enabled_p && r->y < yb; ++r)
29823 g = r->glyphs[TEXT_AREA];
29824 e = g + r->used[TEXT_AREA];
29825 found = false;
29826 for ( ; g < e; ++g)
29827 if (EQ (g->object, object)
29828 && startpos <= g->charpos && g->charpos < endpos)
29830 found = true;
29831 break;
29833 if (!found)
29834 break;
29837 /* The highlighted region ends on the previous row. */
29838 r--;
29840 /* Set the end row. */
29841 hlinfo->mouse_face_end_row = MATRIX_ROW_VPOS (r, w->current_matrix);
29843 /* Compute and set the end column and the end column's horizontal
29844 pixel coordinate. */
29845 if (!r->reversed_p)
29847 g = r->glyphs[TEXT_AREA];
29848 e = g + r->used[TEXT_AREA];
29849 for ( ; e > g; --e)
29850 if (EQ ((e-1)->object, object)
29851 && startpos <= (e-1)->charpos && (e-1)->charpos < endpos)
29852 break;
29853 hlinfo->mouse_face_end_col = e - g;
29855 for (gx = r->x; g < e; ++g)
29856 gx += g->pixel_width;
29857 hlinfo->mouse_face_end_x = gx;
29859 else
29861 e = r->glyphs[TEXT_AREA];
29862 g = e + r->used[TEXT_AREA];
29863 for (gx = r->x ; e < g; ++e)
29865 if (EQ (e->object, object)
29866 && startpos <= e->charpos && e->charpos < endpos)
29867 break;
29868 gx += e->pixel_width;
29870 hlinfo->mouse_face_end_col = e - r->glyphs[TEXT_AREA];
29871 hlinfo->mouse_face_end_x = gx;
29875 #ifdef HAVE_WINDOW_SYSTEM
29877 /* See if position X, Y is within a hot-spot of an image. */
29879 static bool
29880 on_hot_spot_p (Lisp_Object hot_spot, int x, int y)
29882 if (!CONSP (hot_spot))
29883 return false;
29885 if (EQ (XCAR (hot_spot), Qrect))
29887 /* CDR is (Top-Left . Bottom-Right) = ((x0 . y0) . (x1 . y1)) */
29888 Lisp_Object rect = XCDR (hot_spot);
29889 Lisp_Object tem;
29890 if (!CONSP (rect))
29891 return false;
29892 if (!CONSP (XCAR (rect)))
29893 return false;
29894 if (!CONSP (XCDR (rect)))
29895 return false;
29896 if (!(tem = XCAR (XCAR (rect)), INTEGERP (tem) && x >= XINT (tem)))
29897 return false;
29898 if (!(tem = XCDR (XCAR (rect)), INTEGERP (tem) && y >= XINT (tem)))
29899 return false;
29900 if (!(tem = XCAR (XCDR (rect)), INTEGERP (tem) && x <= XINT (tem)))
29901 return false;
29902 if (!(tem = XCDR (XCDR (rect)), INTEGERP (tem) && y <= XINT (tem)))
29903 return false;
29904 return true;
29906 else if (EQ (XCAR (hot_spot), Qcircle))
29908 /* CDR is (Center . Radius) = ((x0 . y0) . r) */
29909 Lisp_Object circ = XCDR (hot_spot);
29910 Lisp_Object lr, lx0, ly0;
29911 if (CONSP (circ)
29912 && CONSP (XCAR (circ))
29913 && (lr = XCDR (circ), NUMBERP (lr))
29914 && (lx0 = XCAR (XCAR (circ)), INTEGERP (lx0))
29915 && (ly0 = XCDR (XCAR (circ)), INTEGERP (ly0)))
29917 double r = XFLOATINT (lr);
29918 double dx = XINT (lx0) - x;
29919 double dy = XINT (ly0) - y;
29920 return (dx * dx + dy * dy <= r * r);
29923 else if (EQ (XCAR (hot_spot), Qpoly))
29925 /* CDR is [x0 y0 x1 y1 x2 y2 ...x(n-1) y(n-1)] */
29926 if (VECTORP (XCDR (hot_spot)))
29928 struct Lisp_Vector *v = XVECTOR (XCDR (hot_spot));
29929 Lisp_Object *poly = v->contents;
29930 ptrdiff_t n = v->header.size;
29931 ptrdiff_t i;
29932 bool inside = false;
29933 Lisp_Object lx, ly;
29934 int x0, y0;
29936 /* Need an even number of coordinates, and at least 3 edges. */
29937 if (n < 6 || n & 1)
29938 return false;
29940 /* Count edge segments intersecting line from (X,Y) to (X,infinity).
29941 If count is odd, we are inside polygon. Pixels on edges
29942 may or may not be included depending on actual geometry of the
29943 polygon. */
29944 if ((lx = poly[n-2], !INTEGERP (lx))
29945 || (ly = poly[n-1], !INTEGERP (lx)))
29946 return false;
29947 x0 = XINT (lx), y0 = XINT (ly);
29948 for (i = 0; i < n; i += 2)
29950 int x1 = x0, y1 = y0;
29951 if ((lx = poly[i], !INTEGERP (lx))
29952 || (ly = poly[i+1], !INTEGERP (ly)))
29953 return false;
29954 x0 = XINT (lx), y0 = XINT (ly);
29956 /* Does this segment cross the X line? */
29957 if (x0 >= x)
29959 if (x1 >= x)
29960 continue;
29962 else if (x1 < x)
29963 continue;
29964 if (y > y0 && y > y1)
29965 continue;
29966 if (y < y0 + ((y1 - y0) * (x - x0)) / (x1 - x0))
29967 inside = !inside;
29969 return inside;
29972 return false;
29975 Lisp_Object
29976 find_hot_spot (Lisp_Object map, int x, int y)
29978 while (CONSP (map))
29980 if (CONSP (XCAR (map))
29981 && on_hot_spot_p (XCAR (XCAR (map)), x, y))
29982 return XCAR (map);
29983 map = XCDR (map);
29986 return Qnil;
29989 DEFUN ("lookup-image-map", Flookup_image_map, Slookup_image_map,
29990 3, 3, 0,
29991 doc: /* Lookup in image map MAP coordinates X and Y.
29992 An image map is an alist where each element has the format (AREA ID PLIST).
29993 An AREA is specified as either a rectangle, a circle, or a polygon:
29994 A rectangle is a cons (rect . ((x0 . y0) . (x1 . y1))) specifying the
29995 pixel coordinates of the upper left and bottom right corners.
29996 A circle is a cons (circle . ((x0 . y0) . r)) specifying the center
29997 and the radius of the circle; r may be a float or integer.
29998 A polygon is a cons (poly . [x0 y0 x1 y1 ...]) where each pair in the
29999 vector describes one corner in the polygon.
30000 Returns the alist element for the first matching AREA in MAP. */)
30001 (Lisp_Object map, Lisp_Object x, Lisp_Object y)
30003 if (NILP (map))
30004 return Qnil;
30006 CHECK_NUMBER (x);
30007 CHECK_NUMBER (y);
30009 return find_hot_spot (map,
30010 clip_to_bounds (INT_MIN, XINT (x), INT_MAX),
30011 clip_to_bounds (INT_MIN, XINT (y), INT_MAX));
30013 #endif /* HAVE_WINDOW_SYSTEM */
30016 /* Display frame CURSOR, optionally using shape defined by POINTER. */
30017 static void
30018 define_frame_cursor1 (struct frame *f, Cursor cursor, Lisp_Object pointer)
30020 #ifdef HAVE_WINDOW_SYSTEM
30021 if (!FRAME_WINDOW_P (f))
30022 return;
30024 /* Do not change cursor shape while dragging mouse. */
30025 if (EQ (do_mouse_tracking, Qdragging))
30026 return;
30028 if (!NILP (pointer))
30030 if (EQ (pointer, Qarrow))
30031 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
30032 else if (EQ (pointer, Qhand))
30033 cursor = FRAME_X_OUTPUT (f)->hand_cursor;
30034 else if (EQ (pointer, Qtext))
30035 cursor = FRAME_X_OUTPUT (f)->text_cursor;
30036 else if (EQ (pointer, intern ("hdrag")))
30037 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
30038 else if (EQ (pointer, intern ("nhdrag")))
30039 cursor = FRAME_X_OUTPUT (f)->vertical_drag_cursor;
30040 # ifdef HAVE_X_WINDOWS
30041 else if (EQ (pointer, intern ("vdrag")))
30042 cursor = FRAME_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
30043 # endif
30044 else if (EQ (pointer, intern ("hourglass")))
30045 cursor = FRAME_X_OUTPUT (f)->hourglass_cursor;
30046 else if (EQ (pointer, Qmodeline))
30047 cursor = FRAME_X_OUTPUT (f)->modeline_cursor;
30048 else
30049 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
30052 if (cursor != No_Cursor)
30053 FRAME_RIF (f)->define_frame_cursor (f, cursor);
30054 #endif
30057 /* Take proper action when mouse has moved to the mode or header line
30058 or marginal area AREA of window W, x-position X and y-position Y.
30059 X is relative to the start of the text display area of W, so the
30060 width of bitmap areas and scroll bars must be subtracted to get a
30061 position relative to the start of the mode line. */
30063 static void
30064 note_mode_line_or_margin_highlight (Lisp_Object window, int x, int y,
30065 enum window_part area)
30067 struct window *w = XWINDOW (window);
30068 struct frame *f = XFRAME (w->frame);
30069 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
30070 #ifdef HAVE_WINDOW_SYSTEM
30071 Display_Info *dpyinfo;
30072 #endif
30073 Cursor cursor = No_Cursor;
30074 Lisp_Object pointer = Qnil;
30075 int dx, dy, width, height;
30076 ptrdiff_t charpos;
30077 Lisp_Object string, object = Qnil;
30078 Lisp_Object pos UNINIT;
30079 Lisp_Object mouse_face;
30080 int original_x_pixel = x;
30081 struct glyph * glyph = NULL, * row_start_glyph = NULL;
30082 struct glyph_row *row UNINIT;
30084 if (area == ON_MODE_LINE || area == ON_HEADER_LINE)
30086 int x0;
30087 struct glyph *end;
30089 /* Kludge alert: mode_line_string takes X/Y in pixels, but
30090 returns them in row/column units! */
30091 string = mode_line_string (w, area, &x, &y, &charpos,
30092 &object, &dx, &dy, &width, &height);
30094 row = (area == ON_MODE_LINE
30095 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
30096 : MATRIX_HEADER_LINE_ROW (w->current_matrix));
30098 /* Find the glyph under the mouse pointer. */
30099 if (row->mode_line_p && row->enabled_p)
30101 glyph = row_start_glyph = row->glyphs[TEXT_AREA];
30102 end = glyph + row->used[TEXT_AREA];
30104 for (x0 = original_x_pixel;
30105 glyph < end && x0 >= glyph->pixel_width;
30106 ++glyph)
30107 x0 -= glyph->pixel_width;
30109 if (glyph >= end)
30110 glyph = NULL;
30113 else
30115 x -= WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
30116 /* Kludge alert: marginal_area_string takes X/Y in pixels, but
30117 returns them in row/column units! */
30118 string = marginal_area_string (w, area, &x, &y, &charpos,
30119 &object, &dx, &dy, &width, &height);
30122 Lisp_Object help = Qnil;
30124 #ifdef HAVE_WINDOW_SYSTEM
30125 if (IMAGEP (object))
30127 Lisp_Object image_map, hotspot;
30128 if ((image_map = Fplist_get (XCDR (object), QCmap),
30129 !NILP (image_map))
30130 && (hotspot = find_hot_spot (image_map, dx, dy),
30131 CONSP (hotspot))
30132 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
30134 Lisp_Object plist;
30136 /* Could check XCAR (hotspot) to see if we enter/leave this hot-spot.
30137 If so, we could look for mouse-enter, mouse-leave
30138 properties in PLIST (and do something...). */
30139 hotspot = XCDR (hotspot);
30140 if (CONSP (hotspot)
30141 && (plist = XCAR (hotspot), CONSP (plist)))
30143 pointer = Fplist_get (plist, Qpointer);
30144 if (NILP (pointer))
30145 pointer = Qhand;
30146 help = Fplist_get (plist, Qhelp_echo);
30147 if (!NILP (help))
30149 help_echo_string = help;
30150 XSETWINDOW (help_echo_window, w);
30151 help_echo_object = w->contents;
30152 help_echo_pos = charpos;
30156 if (NILP (pointer))
30157 pointer = Fplist_get (XCDR (object), QCpointer);
30159 #endif /* HAVE_WINDOW_SYSTEM */
30161 if (STRINGP (string))
30162 pos = make_number (charpos);
30164 /* Set the help text and mouse pointer. If the mouse is on a part
30165 of the mode line without any text (e.g. past the right edge of
30166 the mode line text), use the default help text and pointer. */
30167 if (STRINGP (string) || area == ON_MODE_LINE)
30169 /* Arrange to display the help by setting the global variables
30170 help_echo_string, help_echo_object, and help_echo_pos. */
30171 if (NILP (help))
30173 if (STRINGP (string))
30174 help = Fget_text_property (pos, Qhelp_echo, string);
30176 if (!NILP (help))
30178 help_echo_string = help;
30179 XSETWINDOW (help_echo_window, w);
30180 help_echo_object = string;
30181 help_echo_pos = charpos;
30183 else if (area == ON_MODE_LINE)
30185 Lisp_Object default_help
30186 = buffer_local_value (Qmode_line_default_help_echo,
30187 w->contents);
30189 if (STRINGP (default_help))
30191 help_echo_string = default_help;
30192 XSETWINDOW (help_echo_window, w);
30193 help_echo_object = Qnil;
30194 help_echo_pos = -1;
30199 #ifdef HAVE_WINDOW_SYSTEM
30200 /* Change the mouse pointer according to what is under it. */
30201 if (FRAME_WINDOW_P (f))
30203 bool draggable = (! WINDOW_BOTTOMMOST_P (w)
30204 || minibuf_level
30205 || NILP (Vresize_mini_windows));
30207 dpyinfo = FRAME_DISPLAY_INFO (f);
30208 if (STRINGP (string))
30210 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
30212 if (NILP (pointer))
30213 pointer = Fget_text_property (pos, Qpointer, string);
30215 /* Change the mouse pointer according to what is under X/Y. */
30216 if (NILP (pointer)
30217 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE)))
30219 Lisp_Object map;
30220 map = Fget_text_property (pos, Qlocal_map, string);
30221 if (!KEYMAPP (map))
30222 map = Fget_text_property (pos, Qkeymap, string);
30223 if (!KEYMAPP (map) && draggable)
30224 cursor = dpyinfo->vertical_scroll_bar_cursor;
30227 else if (draggable)
30228 /* Default mode-line pointer. */
30229 cursor = FRAME_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
30231 #endif
30234 /* Change the mouse face according to what is under X/Y. */
30235 bool mouse_face_shown = false;
30236 if (STRINGP (string))
30238 mouse_face = Fget_text_property (pos, Qmouse_face, string);
30239 if (!NILP (Vmouse_highlight) && !NILP (mouse_face)
30240 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
30241 && glyph)
30243 Lisp_Object b, e;
30245 struct glyph * tmp_glyph;
30247 int gpos;
30248 int gseq_length;
30249 int total_pixel_width;
30250 ptrdiff_t begpos, endpos, ignore;
30252 int vpos, hpos;
30254 b = Fprevious_single_property_change (make_number (charpos + 1),
30255 Qmouse_face, string, Qnil);
30256 if (NILP (b))
30257 begpos = 0;
30258 else
30259 begpos = XINT (b);
30261 e = Fnext_single_property_change (pos, Qmouse_face, string, Qnil);
30262 if (NILP (e))
30263 endpos = SCHARS (string);
30264 else
30265 endpos = XINT (e);
30267 /* Calculate the glyph position GPOS of GLYPH in the
30268 displayed string, relative to the beginning of the
30269 highlighted part of the string.
30271 Note: GPOS is different from CHARPOS. CHARPOS is the
30272 position of GLYPH in the internal string object. A mode
30273 line string format has structures which are converted to
30274 a flattened string by the Emacs Lisp interpreter. The
30275 internal string is an element of those structures. The
30276 displayed string is the flattened string. */
30277 tmp_glyph = row_start_glyph;
30278 while (tmp_glyph < glyph
30279 && (!(EQ (tmp_glyph->object, glyph->object)
30280 && begpos <= tmp_glyph->charpos
30281 && tmp_glyph->charpos < endpos)))
30282 tmp_glyph++;
30283 gpos = glyph - tmp_glyph;
30285 /* Calculate the length GSEQ_LENGTH of the glyph sequence of
30286 the highlighted part of the displayed string to which
30287 GLYPH belongs. Note: GSEQ_LENGTH is different from
30288 SCHARS (STRING), because the latter returns the length of
30289 the internal string. */
30290 for (tmp_glyph = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
30291 tmp_glyph > glyph
30292 && (!(EQ (tmp_glyph->object, glyph->object)
30293 && begpos <= tmp_glyph->charpos
30294 && tmp_glyph->charpos < endpos));
30295 tmp_glyph--)
30297 gseq_length = gpos + (tmp_glyph - glyph) + 1;
30299 /* Calculate the total pixel width of all the glyphs between
30300 the beginning of the highlighted area and GLYPH. */
30301 total_pixel_width = 0;
30302 for (tmp_glyph = glyph - gpos; tmp_glyph != glyph; tmp_glyph++)
30303 total_pixel_width += tmp_glyph->pixel_width;
30305 /* Pre calculation of re-rendering position. Note: X is in
30306 column units here, after the call to mode_line_string or
30307 marginal_area_string. */
30308 hpos = x - gpos;
30309 vpos = (area == ON_MODE_LINE
30310 ? (w->current_matrix)->nrows - 1
30311 : 0);
30313 /* If GLYPH's position is included in the region that is
30314 already drawn in mouse face, we have nothing to do. */
30315 if ( EQ (window, hlinfo->mouse_face_window)
30316 && (!row->reversed_p
30317 ? (hlinfo->mouse_face_beg_col <= hpos
30318 && hpos < hlinfo->mouse_face_end_col)
30319 /* In R2L rows we swap BEG and END, see below. */
30320 : (hlinfo->mouse_face_end_col <= hpos
30321 && hpos < hlinfo->mouse_face_beg_col))
30322 && hlinfo->mouse_face_beg_row == vpos )
30323 return;
30325 if (clear_mouse_face (hlinfo))
30326 cursor = No_Cursor;
30328 if (!row->reversed_p)
30330 hlinfo->mouse_face_beg_col = hpos;
30331 hlinfo->mouse_face_beg_x = original_x_pixel
30332 - (total_pixel_width + dx);
30333 hlinfo->mouse_face_end_col = hpos + gseq_length;
30334 hlinfo->mouse_face_end_x = 0;
30336 else
30338 /* In R2L rows, show_mouse_face expects BEG and END
30339 coordinates to be swapped. */
30340 hlinfo->mouse_face_end_col = hpos;
30341 hlinfo->mouse_face_end_x = original_x_pixel
30342 - (total_pixel_width + dx);
30343 hlinfo->mouse_face_beg_col = hpos + gseq_length;
30344 hlinfo->mouse_face_beg_x = 0;
30347 hlinfo->mouse_face_beg_row = vpos;
30348 hlinfo->mouse_face_end_row = hlinfo->mouse_face_beg_row;
30349 hlinfo->mouse_face_past_end = false;
30350 hlinfo->mouse_face_window = window;
30352 hlinfo->mouse_face_face_id = face_at_string_position (w, string,
30353 charpos,
30354 0, &ignore,
30355 glyph->face_id,
30356 true);
30357 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
30358 mouse_face_shown = true;
30360 if (NILP (pointer))
30361 pointer = Qhand;
30365 /* If mouse-face doesn't need to be shown, clear any existing
30366 mouse-face. */
30367 if ((area == ON_MODE_LINE || area == ON_HEADER_LINE) && !mouse_face_shown)
30368 clear_mouse_face (hlinfo);
30370 define_frame_cursor1 (f, cursor, pointer);
30374 /* EXPORT:
30375 Take proper action when the mouse has moved to position X, Y on
30376 frame F with regards to highlighting portions of display that have
30377 mouse-face properties. Also de-highlight portions of display where
30378 the mouse was before, set the mouse pointer shape as appropriate
30379 for the mouse coordinates, and activate help echo (tooltips).
30380 X and Y can be negative or out of range. */
30382 void
30383 note_mouse_highlight (struct frame *f, int x, int y)
30385 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
30386 enum window_part part = ON_NOTHING;
30387 Lisp_Object window;
30388 struct window *w;
30389 Cursor cursor = No_Cursor;
30390 Lisp_Object pointer = Qnil; /* Takes precedence over cursor! */
30391 struct buffer *b;
30393 /* When a menu is active, don't highlight because this looks odd. */
30394 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS) || defined (MSDOS)
30395 if (popup_activated ())
30396 return;
30397 #endif
30399 if (!f->glyphs_initialized_p
30400 || f->pointer_invisible)
30401 return;
30403 hlinfo->mouse_face_mouse_x = x;
30404 hlinfo->mouse_face_mouse_y = y;
30405 hlinfo->mouse_face_mouse_frame = f;
30407 if (hlinfo->mouse_face_defer)
30408 return;
30410 /* Which window is that in? */
30411 window = window_from_coordinates (f, x, y, &part, true);
30413 /* If displaying active text in another window, clear that. */
30414 if (! EQ (window, hlinfo->mouse_face_window)
30415 /* Also clear if we move out of text area in same window. */
30416 || (!NILP (hlinfo->mouse_face_window)
30417 && !NILP (window)
30418 && part != ON_TEXT
30419 && part != ON_MODE_LINE
30420 && part != ON_HEADER_LINE))
30421 clear_mouse_face (hlinfo);
30423 /* Not on a window -> return. */
30424 if (!WINDOWP (window))
30425 return;
30427 /* Reset help_echo_string. It will get recomputed below. */
30428 help_echo_string = Qnil;
30430 /* Convert to window-relative pixel coordinates. */
30431 w = XWINDOW (window);
30432 frame_to_window_pixel_xy (w, &x, &y);
30434 #if defined (HAVE_WINDOW_SYSTEM) && ! defined (USE_GTK) && ! defined (HAVE_NS)
30435 /* Handle tool-bar window differently since it doesn't display a
30436 buffer. */
30437 if (EQ (window, f->tool_bar_window))
30439 note_tool_bar_highlight (f, x, y);
30440 return;
30442 #endif
30444 /* Mouse is on the mode, header line or margin? */
30445 if (part == ON_MODE_LINE || part == ON_HEADER_LINE
30446 || part == ON_LEFT_MARGIN || part == ON_RIGHT_MARGIN)
30448 note_mode_line_or_margin_highlight (window, x, y, part);
30450 #ifdef HAVE_WINDOW_SYSTEM
30451 if (part == ON_LEFT_MARGIN || part == ON_RIGHT_MARGIN)
30453 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
30454 /* Show non-text cursor (Bug#16647). */
30455 goto set_cursor;
30457 else
30458 #endif
30459 return;
30462 #ifdef HAVE_WINDOW_SYSTEM
30463 if (part == ON_VERTICAL_BORDER)
30465 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
30466 help_echo_string = build_string ("drag-mouse-1: resize");
30468 else if (part == ON_RIGHT_DIVIDER)
30470 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
30471 help_echo_string = build_string ("drag-mouse-1: resize");
30473 else if (part == ON_BOTTOM_DIVIDER)
30474 if (! WINDOW_BOTTOMMOST_P (w)
30475 || minibuf_level
30476 || NILP (Vresize_mini_windows))
30478 cursor = FRAME_X_OUTPUT (f)->vertical_drag_cursor;
30479 help_echo_string = build_string ("drag-mouse-1: resize");
30481 else
30482 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
30483 else if (part == ON_LEFT_FRINGE || part == ON_RIGHT_FRINGE
30484 || part == ON_VERTICAL_SCROLL_BAR
30485 || part == ON_HORIZONTAL_SCROLL_BAR)
30486 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
30487 else
30488 cursor = FRAME_X_OUTPUT (f)->text_cursor;
30489 #endif
30491 /* Are we in a window whose display is up to date?
30492 And verify the buffer's text has not changed. */
30493 b = XBUFFER (w->contents);
30494 if (part == ON_TEXT && w->window_end_valid && !window_outdated (w))
30496 int hpos, vpos, dx, dy, area = LAST_AREA;
30497 ptrdiff_t pos;
30498 struct glyph *glyph;
30499 Lisp_Object object;
30500 Lisp_Object mouse_face = Qnil, position;
30501 Lisp_Object *overlay_vec = NULL;
30502 ptrdiff_t i, noverlays;
30503 struct buffer *obuf;
30504 ptrdiff_t obegv, ozv;
30505 bool same_region;
30507 /* Find the glyph under X/Y. */
30508 glyph = x_y_to_hpos_vpos (w, x, y, &hpos, &vpos, &dx, &dy, &area);
30510 #ifdef HAVE_WINDOW_SYSTEM
30511 /* Look for :pointer property on image. */
30512 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
30514 struct image *img = IMAGE_OPT_FROM_ID (f, glyph->u.img_id);
30515 if (img != NULL && IMAGEP (img->spec))
30517 Lisp_Object image_map, hotspot;
30518 if ((image_map = Fplist_get (XCDR (img->spec), QCmap),
30519 !NILP (image_map))
30520 && (hotspot = find_hot_spot (image_map,
30521 glyph->slice.img.x + dx,
30522 glyph->slice.img.y + dy),
30523 CONSP (hotspot))
30524 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
30526 Lisp_Object plist;
30528 /* Could check XCAR (hotspot) to see if we enter/leave
30529 this hot-spot.
30530 If so, we could look for mouse-enter, mouse-leave
30531 properties in PLIST (and do something...). */
30532 hotspot = XCDR (hotspot);
30533 if (CONSP (hotspot)
30534 && (plist = XCAR (hotspot), CONSP (plist)))
30536 pointer = Fplist_get (plist, Qpointer);
30537 if (NILP (pointer))
30538 pointer = Qhand;
30539 help_echo_string = Fplist_get (plist, Qhelp_echo);
30540 if (!NILP (help_echo_string))
30542 help_echo_window = window;
30543 help_echo_object = glyph->object;
30544 help_echo_pos = glyph->charpos;
30548 if (NILP (pointer))
30549 pointer = Fplist_get (XCDR (img->spec), QCpointer);
30552 #endif /* HAVE_WINDOW_SYSTEM */
30554 /* Clear mouse face if X/Y not over text. */
30555 if (glyph == NULL
30556 || area != TEXT_AREA
30557 || !MATRIX_ROW_DISPLAYS_TEXT_P (MATRIX_ROW (w->current_matrix, vpos))
30558 /* Glyph's OBJECT is nil for glyphs inserted by the
30559 display engine for its internal purposes, like truncation
30560 and continuation glyphs and blanks beyond the end of
30561 line's text on text terminals. If we are over such a
30562 glyph, we are not over any text. */
30563 || NILP (glyph->object)
30564 /* R2L rows have a stretch glyph at their front, which
30565 stands for no text, whereas L2R rows have no glyphs at
30566 all beyond the end of text. Treat such stretch glyphs
30567 like we do with NULL glyphs in L2R rows. */
30568 || (MATRIX_ROW (w->current_matrix, vpos)->reversed_p
30569 && glyph == MATRIX_ROW_GLYPH_START (w->current_matrix, vpos)
30570 && glyph->type == STRETCH_GLYPH
30571 && glyph->avoid_cursor_p))
30573 if (clear_mouse_face (hlinfo))
30574 cursor = No_Cursor;
30575 if (FRAME_WINDOW_P (f) && NILP (pointer))
30577 #ifdef HAVE_WINDOW_SYSTEM
30578 if (area != TEXT_AREA)
30579 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
30580 else
30581 pointer = Vvoid_text_area_pointer;
30582 #endif
30584 goto set_cursor;
30587 pos = glyph->charpos;
30588 object = glyph->object;
30589 if (!STRINGP (object) && !BUFFERP (object))
30590 goto set_cursor;
30592 /* If we get an out-of-range value, return now; avoid an error. */
30593 if (BUFFERP (object) && pos > BUF_Z (b))
30594 goto set_cursor;
30596 /* Make the window's buffer temporarily current for
30597 overlays_at and compute_char_face. */
30598 obuf = current_buffer;
30599 current_buffer = b;
30600 obegv = BEGV;
30601 ozv = ZV;
30602 BEGV = BEG;
30603 ZV = Z;
30605 /* Is this char mouse-active or does it have help-echo? */
30606 position = make_number (pos);
30608 USE_SAFE_ALLOCA;
30610 if (BUFFERP (object))
30612 /* Put all the overlays we want in a vector in overlay_vec. */
30613 GET_OVERLAYS_AT (pos, overlay_vec, noverlays, NULL, false);
30614 /* Sort overlays into increasing priority order. */
30615 noverlays = sort_overlays (overlay_vec, noverlays, w);
30617 else
30618 noverlays = 0;
30620 if (NILP (Vmouse_highlight))
30622 clear_mouse_face (hlinfo);
30623 goto check_help_echo;
30626 same_region = coords_in_mouse_face_p (w, hpos, vpos);
30628 if (same_region)
30629 cursor = No_Cursor;
30631 /* Check mouse-face highlighting. */
30632 if (! same_region
30633 /* If there exists an overlay with mouse-face overlapping
30634 the one we are currently highlighting, we have to
30635 check if we enter the overlapping overlay, and then
30636 highlight only that. */
30637 || (OVERLAYP (hlinfo->mouse_face_overlay)
30638 && mouse_face_overlay_overlaps (hlinfo->mouse_face_overlay)))
30640 /* Find the highest priority overlay with a mouse-face. */
30641 Lisp_Object overlay = Qnil;
30642 for (i = noverlays - 1; i >= 0 && NILP (overlay); --i)
30644 mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
30645 if (!NILP (mouse_face))
30646 overlay = overlay_vec[i];
30649 /* If we're highlighting the same overlay as before, there's
30650 no need to do that again. */
30651 if (!NILP (overlay) && EQ (overlay, hlinfo->mouse_face_overlay))
30652 goto check_help_echo;
30654 /* Clear the display of the old active region, if any. */
30655 if (clear_mouse_face (hlinfo))
30656 cursor = No_Cursor;
30658 /* Record the overlay, if any, to be highlighted. */
30659 hlinfo->mouse_face_overlay = overlay;
30661 /* If no overlay applies, get a text property. */
30662 if (NILP (overlay))
30663 mouse_face = Fget_text_property (position, Qmouse_face, object);
30665 /* Next, compute the bounds of the mouse highlighting and
30666 display it. */
30667 if (!NILP (mouse_face) && STRINGP (object))
30669 /* The mouse-highlighting comes from a display string
30670 with a mouse-face. */
30671 Lisp_Object s, e;
30672 ptrdiff_t ignore;
30674 s = Fprevious_single_property_change
30675 (make_number (pos + 1), Qmouse_face, object, Qnil);
30676 e = Fnext_single_property_change
30677 (position, Qmouse_face, object, Qnil);
30678 if (NILP (s))
30679 s = make_number (0);
30680 if (NILP (e))
30681 e = make_number (SCHARS (object));
30682 mouse_face_from_string_pos (w, hlinfo, object,
30683 XINT (s), XINT (e));
30684 hlinfo->mouse_face_past_end = false;
30685 hlinfo->mouse_face_window = window;
30686 hlinfo->mouse_face_face_id
30687 = face_at_string_position (w, object, pos, 0, &ignore,
30688 glyph->face_id, true);
30689 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
30690 cursor = No_Cursor;
30692 else
30694 /* The mouse-highlighting, if any, comes from an overlay
30695 or text property in the buffer. */
30696 Lisp_Object buffer UNINIT;
30697 Lisp_Object disp_string UNINIT;
30699 if (STRINGP (object))
30701 /* If we are on a display string with no mouse-face,
30702 check if the text under it has one. */
30703 struct glyph_row *r = MATRIX_ROW (w->current_matrix, vpos);
30704 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
30705 pos = string_buffer_position (object, start);
30706 if (pos > 0)
30708 mouse_face = get_char_property_and_overlay
30709 (make_number (pos), Qmouse_face, w->contents, &overlay);
30710 buffer = w->contents;
30711 disp_string = object;
30714 else
30716 buffer = object;
30717 disp_string = Qnil;
30720 if (!NILP (mouse_face))
30722 Lisp_Object before, after;
30723 Lisp_Object before_string, after_string;
30724 /* To correctly find the limits of mouse highlight
30725 in a bidi-reordered buffer, we must not use the
30726 optimization of limiting the search in
30727 previous-single-property-change and
30728 next-single-property-change, because
30729 rows_from_pos_range needs the real start and end
30730 positions to DTRT in this case. That's because
30731 the first row visible in a window does not
30732 necessarily display the character whose position
30733 is the smallest. */
30734 Lisp_Object lim1
30735 = NILP (BVAR (XBUFFER (buffer), bidi_display_reordering))
30736 ? Fmarker_position (w->start)
30737 : Qnil;
30738 Lisp_Object lim2
30739 = NILP (BVAR (XBUFFER (buffer), bidi_display_reordering))
30740 ? make_number (BUF_Z (XBUFFER (buffer))
30741 - w->window_end_pos)
30742 : Qnil;
30744 if (NILP (overlay))
30746 /* Handle the text property case. */
30747 before = Fprevious_single_property_change
30748 (make_number (pos + 1), Qmouse_face, buffer, lim1);
30749 after = Fnext_single_property_change
30750 (make_number (pos), Qmouse_face, buffer, lim2);
30751 before_string = after_string = Qnil;
30753 else
30755 /* Handle the overlay case. */
30756 before = Foverlay_start (overlay);
30757 after = Foverlay_end (overlay);
30758 before_string = Foverlay_get (overlay, Qbefore_string);
30759 after_string = Foverlay_get (overlay, Qafter_string);
30761 if (!STRINGP (before_string)) before_string = Qnil;
30762 if (!STRINGP (after_string)) after_string = Qnil;
30765 mouse_face_from_buffer_pos (window, hlinfo, pos,
30766 NILP (before)
30768 : XFASTINT (before),
30769 NILP (after)
30770 ? BUF_Z (XBUFFER (buffer))
30771 : XFASTINT (after),
30772 before_string, after_string,
30773 disp_string);
30774 cursor = No_Cursor;
30779 check_help_echo:
30781 /* Look for a `help-echo' property. */
30782 if (NILP (help_echo_string)) {
30783 Lisp_Object help, overlay;
30785 /* Check overlays first. */
30786 help = overlay = Qnil;
30787 for (i = noverlays - 1; i >= 0 && NILP (help); --i)
30789 overlay = overlay_vec[i];
30790 help = Foverlay_get (overlay, Qhelp_echo);
30793 if (!NILP (help))
30795 help_echo_string = help;
30796 help_echo_window = window;
30797 help_echo_object = overlay;
30798 help_echo_pos = pos;
30800 else
30802 Lisp_Object obj = glyph->object;
30803 ptrdiff_t charpos = glyph->charpos;
30805 /* Try text properties. */
30806 if (STRINGP (obj)
30807 && charpos >= 0
30808 && charpos < SCHARS (obj))
30810 help = Fget_text_property (make_number (charpos),
30811 Qhelp_echo, obj);
30812 if (NILP (help))
30814 /* If the string itself doesn't specify a help-echo,
30815 see if the buffer text ``under'' it does. */
30816 struct glyph_row *r
30817 = MATRIX_ROW (w->current_matrix, vpos);
30818 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
30819 ptrdiff_t p = string_buffer_position (obj, start);
30820 if (p > 0)
30822 help = Fget_char_property (make_number (p),
30823 Qhelp_echo, w->contents);
30824 if (!NILP (help))
30826 charpos = p;
30827 obj = w->contents;
30832 else if (BUFFERP (obj)
30833 && charpos >= BEGV
30834 && charpos < ZV)
30835 help = Fget_text_property (make_number (charpos), Qhelp_echo,
30836 obj);
30838 if (!NILP (help))
30840 help_echo_string = help;
30841 help_echo_window = window;
30842 help_echo_object = obj;
30843 help_echo_pos = charpos;
30848 #ifdef HAVE_WINDOW_SYSTEM
30849 /* Look for a `pointer' property. */
30850 if (FRAME_WINDOW_P (f) && NILP (pointer))
30852 /* Check overlays first. */
30853 for (i = noverlays - 1; i >= 0 && NILP (pointer); --i)
30854 pointer = Foverlay_get (overlay_vec[i], Qpointer);
30856 if (NILP (pointer))
30858 Lisp_Object obj = glyph->object;
30859 ptrdiff_t charpos = glyph->charpos;
30861 /* Try text properties. */
30862 if (STRINGP (obj)
30863 && charpos >= 0
30864 && charpos < SCHARS (obj))
30866 pointer = Fget_text_property (make_number (charpos),
30867 Qpointer, obj);
30868 if (NILP (pointer))
30870 /* If the string itself doesn't specify a pointer,
30871 see if the buffer text ``under'' it does. */
30872 struct glyph_row *r
30873 = MATRIX_ROW (w->current_matrix, vpos);
30874 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
30875 ptrdiff_t p = string_buffer_position (obj, start);
30876 if (p > 0)
30877 pointer = Fget_char_property (make_number (p),
30878 Qpointer, w->contents);
30881 else if (BUFFERP (obj)
30882 && charpos >= BEGV
30883 && charpos < ZV)
30884 pointer = Fget_text_property (make_number (charpos),
30885 Qpointer, obj);
30888 #endif /* HAVE_WINDOW_SYSTEM */
30890 BEGV = obegv;
30891 ZV = ozv;
30892 current_buffer = obuf;
30893 SAFE_FREE ();
30896 set_cursor:
30897 define_frame_cursor1 (f, cursor, pointer);
30901 /* EXPORT for RIF:
30902 Clear any mouse-face on window W. This function is part of the
30903 redisplay interface, and is called from try_window_id and similar
30904 functions to ensure the mouse-highlight is off. */
30906 void
30907 x_clear_window_mouse_face (struct window *w)
30909 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (XFRAME (w->frame));
30910 Lisp_Object window;
30912 block_input ();
30913 XSETWINDOW (window, w);
30914 if (EQ (window, hlinfo->mouse_face_window))
30915 clear_mouse_face (hlinfo);
30916 unblock_input ();
30920 /* EXPORT:
30921 Just discard the mouse face information for frame F, if any.
30922 This is used when the size of F is changed. */
30924 void
30925 cancel_mouse_face (struct frame *f)
30927 Lisp_Object window;
30928 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
30930 window = hlinfo->mouse_face_window;
30931 if (! NILP (window) && XFRAME (XWINDOW (window)->frame) == f)
30932 reset_mouse_highlight (hlinfo);
30937 /***********************************************************************
30938 Exposure Events
30939 ***********************************************************************/
30941 #ifdef HAVE_WINDOW_SYSTEM
30943 /* Redraw the part of glyph row area AREA of glyph row ROW on window W
30944 which intersects rectangle R. R is in window-relative coordinates. */
30946 static void
30947 expose_area (struct window *w, struct glyph_row *row, XRectangle *r,
30948 enum glyph_row_area area)
30950 struct glyph *first = row->glyphs[area];
30951 struct glyph *end = row->glyphs[area] + row->used[area];
30952 struct glyph *last;
30953 int first_x, start_x, x;
30955 if (area == TEXT_AREA && row->fill_line_p)
30956 /* If row extends face to end of line write the whole line. */
30957 draw_glyphs (w, 0, row, area,
30958 0, row->used[area],
30959 DRAW_NORMAL_TEXT, 0);
30960 else
30962 /* Set START_X to the window-relative start position for drawing glyphs of
30963 AREA. The first glyph of the text area can be partially visible.
30964 The first glyphs of other areas cannot. */
30965 start_x = window_box_left_offset (w, area);
30966 x = start_x;
30967 if (area == TEXT_AREA)
30968 x += row->x;
30970 /* Find the first glyph that must be redrawn. */
30971 while (first < end
30972 && x + first->pixel_width < r->x)
30974 x += first->pixel_width;
30975 ++first;
30978 /* Find the last one. */
30979 last = first;
30980 first_x = x;
30981 /* Use a signed int intermediate value to avoid catastrophic
30982 failures due to comparison between signed and unsigned, when
30983 x is negative (can happen for wide images that are hscrolled). */
30984 int r_end = r->x + r->width;
30985 while (last < end && x < r_end)
30987 x += last->pixel_width;
30988 ++last;
30991 /* Repaint. */
30992 if (last > first)
30993 draw_glyphs (w, first_x - start_x, row, area,
30994 first - row->glyphs[area], last - row->glyphs[area],
30995 DRAW_NORMAL_TEXT, 0);
31000 /* Redraw the parts of the glyph row ROW on window W intersecting
31001 rectangle R. R is in window-relative coordinates. Value is
31002 true if mouse-face was overwritten. */
31004 static bool
31005 expose_line (struct window *w, struct glyph_row *row, XRectangle *r)
31007 eassert (row->enabled_p);
31009 if (row->mode_line_p || w->pseudo_window_p)
31010 draw_glyphs (w, 0, row, TEXT_AREA,
31011 0, row->used[TEXT_AREA],
31012 DRAW_NORMAL_TEXT, 0);
31013 else
31015 if (row->used[LEFT_MARGIN_AREA])
31016 expose_area (w, row, r, LEFT_MARGIN_AREA);
31017 if (row->used[TEXT_AREA])
31018 expose_area (w, row, r, TEXT_AREA);
31019 if (row->used[RIGHT_MARGIN_AREA])
31020 expose_area (w, row, r, RIGHT_MARGIN_AREA);
31021 draw_row_fringe_bitmaps (w, row);
31024 return row->mouse_face_p;
31028 /* Redraw those parts of glyphs rows during expose event handling that
31029 overlap other rows. Redrawing of an exposed line writes over parts
31030 of lines overlapping that exposed line; this function fixes that.
31032 W is the window being exposed. FIRST_OVERLAPPING_ROW is the first
31033 row in W's current matrix that is exposed and overlaps other rows.
31034 LAST_OVERLAPPING_ROW is the last such row. */
31036 static void
31037 expose_overlaps (struct window *w,
31038 struct glyph_row *first_overlapping_row,
31039 struct glyph_row *last_overlapping_row,
31040 XRectangle *r)
31042 struct glyph_row *row;
31044 for (row = first_overlapping_row; row <= last_overlapping_row; ++row)
31045 if (row->overlapping_p)
31047 eassert (row->enabled_p && !row->mode_line_p);
31049 row->clip = r;
31050 if (row->used[LEFT_MARGIN_AREA])
31051 x_fix_overlapping_area (w, row, LEFT_MARGIN_AREA, OVERLAPS_BOTH);
31053 if (row->used[TEXT_AREA])
31054 x_fix_overlapping_area (w, row, TEXT_AREA, OVERLAPS_BOTH);
31056 if (row->used[RIGHT_MARGIN_AREA])
31057 x_fix_overlapping_area (w, row, RIGHT_MARGIN_AREA, OVERLAPS_BOTH);
31058 row->clip = NULL;
31063 /* Return true if W's cursor intersects rectangle R. */
31065 static bool
31066 phys_cursor_in_rect_p (struct window *w, XRectangle *r)
31068 XRectangle cr, result;
31069 struct glyph *cursor_glyph;
31070 struct glyph_row *row;
31072 if (w->phys_cursor.vpos >= 0
31073 && w->phys_cursor.vpos < w->current_matrix->nrows
31074 && (row = MATRIX_ROW (w->current_matrix, w->phys_cursor.vpos),
31075 row->enabled_p)
31076 && row->cursor_in_fringe_p)
31078 /* Cursor is in the fringe. */
31079 cr.x = window_box_right_offset (w,
31080 (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
31081 ? RIGHT_MARGIN_AREA
31082 : TEXT_AREA));
31083 cr.y = row->y;
31084 cr.width = WINDOW_RIGHT_FRINGE_WIDTH (w);
31085 cr.height = row->height;
31086 return x_intersect_rectangles (&cr, r, &result);
31089 cursor_glyph = get_phys_cursor_glyph (w);
31090 if (cursor_glyph)
31092 /* r is relative to W's box, but w->phys_cursor.x is relative
31093 to left edge of W's TEXT area. Adjust it. */
31094 cr.x = window_box_left_offset (w, TEXT_AREA) + w->phys_cursor.x;
31095 cr.y = w->phys_cursor.y;
31096 cr.width = cursor_glyph->pixel_width;
31097 cr.height = w->phys_cursor_height;
31098 /* ++KFS: W32 version used W32-specific IntersectRect here, but
31099 I assume the effect is the same -- and this is portable. */
31100 return x_intersect_rectangles (&cr, r, &result);
31102 /* If we don't understand the format, pretend we're not in the hot-spot. */
31103 return false;
31107 /* EXPORT:
31108 Draw a vertical window border to the right of window W if W doesn't
31109 have vertical scroll bars. */
31111 void
31112 x_draw_vertical_border (struct window *w)
31114 struct frame *f = XFRAME (WINDOW_FRAME (w));
31116 /* We could do better, if we knew what type of scroll-bar the adjacent
31117 windows (on either side) have... But we don't :-(
31118 However, I think this works ok. ++KFS 2003-04-25 */
31120 /* Redraw borders between horizontally adjacent windows. Don't
31121 do it for frames with vertical scroll bars because either the
31122 right scroll bar of a window, or the left scroll bar of its
31123 neighbor will suffice as a border. */
31124 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f) || FRAME_RIGHT_DIVIDER_WIDTH (f))
31125 return;
31127 /* Note: It is necessary to redraw both the left and the right
31128 borders, for when only this single window W is being
31129 redisplayed. */
31130 if (!WINDOW_RIGHTMOST_P (w)
31131 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w))
31133 int x0, x1, y0, y1;
31135 window_box_edges (w, &x0, &y0, &x1, &y1);
31136 y1 -= 1;
31138 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
31139 x1 -= 1;
31141 FRAME_RIF (f)->draw_vertical_window_border (w, x1, y0, y1);
31144 if (!WINDOW_LEFTMOST_P (w)
31145 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w))
31147 int x0, x1, y0, y1;
31149 window_box_edges (w, &x0, &y0, &x1, &y1);
31150 y1 -= 1;
31152 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
31153 x0 -= 1;
31155 FRAME_RIF (f)->draw_vertical_window_border (w, x0, y0, y1);
31160 /* Draw window dividers for window W. */
31162 void
31163 x_draw_right_divider (struct window *w)
31165 struct frame *f = WINDOW_XFRAME (w);
31167 if (w->mini || w->pseudo_window_p)
31168 return;
31169 else if (WINDOW_RIGHT_DIVIDER_WIDTH (w))
31171 int x0 = WINDOW_RIGHT_EDGE_X (w) - WINDOW_RIGHT_DIVIDER_WIDTH (w);
31172 int x1 = WINDOW_RIGHT_EDGE_X (w);
31173 int y0 = WINDOW_TOP_EDGE_Y (w);
31174 /* The bottom divider prevails. */
31175 int y1 = WINDOW_BOTTOM_EDGE_Y (w) - WINDOW_BOTTOM_DIVIDER_WIDTH (w);
31177 FRAME_RIF (f)->draw_window_divider (w, x0, x1, y0, y1);
31181 static void
31182 x_draw_bottom_divider (struct window *w)
31184 struct frame *f = XFRAME (WINDOW_FRAME (w));
31186 if (w->mini || w->pseudo_window_p)
31187 return;
31188 else if (WINDOW_BOTTOM_DIVIDER_WIDTH (w))
31190 int x0 = WINDOW_LEFT_EDGE_X (w);
31191 int x1 = WINDOW_RIGHT_EDGE_X (w);
31192 int y0 = WINDOW_BOTTOM_EDGE_Y (w) - WINDOW_BOTTOM_DIVIDER_WIDTH (w);
31193 int y1 = WINDOW_BOTTOM_EDGE_Y (w);
31195 FRAME_RIF (f)->draw_window_divider (w, x0, x1, y0, y1);
31199 /* Redraw the part of window W intersection rectangle FR. Pixel
31200 coordinates in FR are frame-relative. Call this function with
31201 input blocked. Value is true if the exposure overwrites
31202 mouse-face. */
31204 static bool
31205 expose_window (struct window *w, XRectangle *fr)
31207 struct frame *f = XFRAME (w->frame);
31208 XRectangle wr, r;
31209 bool mouse_face_overwritten_p = false;
31211 /* If window is not yet fully initialized, do nothing. This can
31212 happen when toolkit scroll bars are used and a window is split.
31213 Reconfiguring the scroll bar will generate an expose for a newly
31214 created window. */
31215 if (w->current_matrix == NULL)
31216 return false;
31218 /* When we're currently updating the window, display and current
31219 matrix usually don't agree. Arrange for a thorough display
31220 later. */
31221 if (w->must_be_updated_p)
31223 SET_FRAME_GARBAGED (f);
31224 return false;
31227 /* Frame-relative pixel rectangle of W. */
31228 wr.x = WINDOW_LEFT_EDGE_X (w);
31229 wr.y = WINDOW_TOP_EDGE_Y (w);
31230 wr.width = WINDOW_PIXEL_WIDTH (w);
31231 wr.height = WINDOW_PIXEL_HEIGHT (w);
31233 if (x_intersect_rectangles (fr, &wr, &r))
31235 int yb = window_text_bottom_y (w);
31236 struct glyph_row *row;
31237 struct glyph_row *first_overlapping_row, *last_overlapping_row;
31239 TRACE ((stderr, "expose_window (%d, %d, %d, %d)\n",
31240 r.x, r.y, r.width, r.height));
31242 /* Convert to window coordinates. */
31243 r.x -= WINDOW_LEFT_EDGE_X (w);
31244 r.y -= WINDOW_TOP_EDGE_Y (w);
31246 /* Turn off the cursor. */
31247 bool cursor_cleared_p = (!w->pseudo_window_p
31248 && phys_cursor_in_rect_p (w, &r));
31249 if (cursor_cleared_p)
31250 x_clear_cursor (w);
31252 /* If the row containing the cursor extends face to end of line,
31253 then expose_area might overwrite the cursor outside the
31254 rectangle and thus notice_overwritten_cursor might clear
31255 w->phys_cursor_on_p. We remember the original value and
31256 check later if it is changed. */
31257 bool phys_cursor_on_p = w->phys_cursor_on_p;
31259 /* Use a signed int intermediate value to avoid catastrophic
31260 failures due to comparison between signed and unsigned, when
31261 y0 or y1 is negative (can happen for tall images). */
31262 int r_bottom = r.y + r.height;
31264 /* Update lines intersecting rectangle R. */
31265 first_overlapping_row = last_overlapping_row = NULL;
31266 for (row = w->current_matrix->rows;
31267 row->enabled_p;
31268 ++row)
31270 int y0 = row->y;
31271 int y1 = MATRIX_ROW_BOTTOM_Y (row);
31273 if ((y0 >= r.y && y0 < r_bottom)
31274 || (y1 > r.y && y1 < r_bottom)
31275 || (r.y >= y0 && r.y < y1)
31276 || (r_bottom > y0 && r_bottom < y1))
31278 /* A header line may be overlapping, but there is no need
31279 to fix overlapping areas for them. KFS 2005-02-12 */
31280 if (row->overlapping_p && !row->mode_line_p)
31282 if (first_overlapping_row == NULL)
31283 first_overlapping_row = row;
31284 last_overlapping_row = row;
31287 row->clip = fr;
31288 if (expose_line (w, row, &r))
31289 mouse_face_overwritten_p = true;
31290 row->clip = NULL;
31292 else if (row->overlapping_p)
31294 /* We must redraw a row overlapping the exposed area. */
31295 if (y0 < r.y
31296 ? y0 + row->phys_height > r.y
31297 : y0 + row->ascent - row->phys_ascent < r.y +r.height)
31299 if (first_overlapping_row == NULL)
31300 first_overlapping_row = row;
31301 last_overlapping_row = row;
31305 if (y1 >= yb)
31306 break;
31309 /* Display the mode line if there is one. */
31310 if (WINDOW_WANTS_MODELINE_P (w)
31311 && (row = MATRIX_MODE_LINE_ROW (w->current_matrix),
31312 row->enabled_p)
31313 && row->y < r_bottom)
31315 if (expose_line (w, row, &r))
31316 mouse_face_overwritten_p = true;
31319 if (!w->pseudo_window_p)
31321 /* Fix the display of overlapping rows. */
31322 if (first_overlapping_row)
31323 expose_overlaps (w, first_overlapping_row, last_overlapping_row,
31324 fr);
31326 /* Draw border between windows. */
31327 if (WINDOW_RIGHT_DIVIDER_WIDTH (w))
31328 x_draw_right_divider (w);
31329 else
31330 x_draw_vertical_border (w);
31332 if (WINDOW_BOTTOM_DIVIDER_WIDTH (w))
31333 x_draw_bottom_divider (w);
31335 /* Turn the cursor on again. */
31336 if (cursor_cleared_p
31337 || (phys_cursor_on_p && !w->phys_cursor_on_p))
31338 update_window_cursor (w, true);
31342 return mouse_face_overwritten_p;
31347 /* Redraw (parts) of all windows in the window tree rooted at W that
31348 intersect R. R contains frame pixel coordinates. Value is
31349 true if the exposure overwrites mouse-face. */
31351 static bool
31352 expose_window_tree (struct window *w, XRectangle *r)
31354 struct frame *f = XFRAME (w->frame);
31355 bool mouse_face_overwritten_p = false;
31357 while (w && !FRAME_GARBAGED_P (f))
31359 mouse_face_overwritten_p
31360 |= (WINDOWP (w->contents)
31361 ? expose_window_tree (XWINDOW (w->contents), r)
31362 : expose_window (w, r));
31364 w = NILP (w->next) ? NULL : XWINDOW (w->next);
31367 return mouse_face_overwritten_p;
31371 /* EXPORT:
31372 Redisplay an exposed area of frame F. X and Y are the upper-left
31373 corner of the exposed rectangle. W and H are width and height of
31374 the exposed area. All are pixel values. W or H zero means redraw
31375 the entire frame. */
31377 void
31378 expose_frame (struct frame *f, int x, int y, int w, int h)
31380 XRectangle r;
31381 bool mouse_face_overwritten_p = false;
31383 TRACE ((stderr, "expose_frame "));
31385 /* No need to redraw if frame will be redrawn soon. */
31386 if (FRAME_GARBAGED_P (f))
31388 TRACE ((stderr, " garbaged\n"));
31389 return;
31392 /* If basic faces haven't been realized yet, there is no point in
31393 trying to redraw anything. This can happen when we get an expose
31394 event while Emacs is starting, e.g. by moving another window. */
31395 if (FRAME_FACE_CACHE (f) == NULL
31396 || FRAME_FACE_CACHE (f)->used < BASIC_FACE_ID_SENTINEL)
31398 TRACE ((stderr, " no faces\n"));
31399 return;
31402 if (w == 0 || h == 0)
31404 r.x = r.y = 0;
31405 r.width = FRAME_TEXT_WIDTH (f);
31406 r.height = FRAME_TEXT_HEIGHT (f);
31408 else
31410 r.x = x;
31411 r.y = y;
31412 r.width = w;
31413 r.height = h;
31416 TRACE ((stderr, "(%d, %d, %d, %d)\n", r.x, r.y, r.width, r.height));
31417 mouse_face_overwritten_p = expose_window_tree (XWINDOW (f->root_window), &r);
31419 #if ! defined (USE_GTK) && ! defined (HAVE_NS)
31420 if (WINDOWP (f->tool_bar_window))
31421 mouse_face_overwritten_p
31422 |= expose_window (XWINDOW (f->tool_bar_window), &r);
31423 #endif
31425 #ifdef HAVE_X_WINDOWS
31426 #ifndef MSDOS
31427 #if ! defined (USE_X_TOOLKIT) && ! defined (USE_GTK)
31428 if (WINDOWP (f->menu_bar_window))
31429 mouse_face_overwritten_p
31430 |= expose_window (XWINDOW (f->menu_bar_window), &r);
31431 #endif /* not USE_X_TOOLKIT and not USE_GTK */
31432 #endif
31433 #endif
31435 /* Some window managers support a focus-follows-mouse style with
31436 delayed raising of frames. Imagine a partially obscured frame,
31437 and moving the mouse into partially obscured mouse-face on that
31438 frame. The visible part of the mouse-face will be highlighted,
31439 then the WM raises the obscured frame. With at least one WM, KDE
31440 2.1, Emacs is not getting any event for the raising of the frame
31441 (even tried with SubstructureRedirectMask), only Expose events.
31442 These expose events will draw text normally, i.e. not
31443 highlighted. Which means we must redo the highlight here.
31444 Subsume it under ``we love X''. --gerd 2001-08-15 */
31445 /* Included in Windows version because Windows most likely does not
31446 do the right thing if any third party tool offers
31447 focus-follows-mouse with delayed raise. --jason 2001-10-12 */
31448 if (mouse_face_overwritten_p && !FRAME_GARBAGED_P (f))
31450 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
31451 if (f == hlinfo->mouse_face_mouse_frame)
31453 int mouse_x = hlinfo->mouse_face_mouse_x;
31454 int mouse_y = hlinfo->mouse_face_mouse_y;
31455 clear_mouse_face (hlinfo);
31456 note_mouse_highlight (f, mouse_x, mouse_y);
31462 /* EXPORT:
31463 Determine the intersection of two rectangles R1 and R2. Return
31464 the intersection in *RESULT. Value is true if RESULT is not
31465 empty. */
31467 bool
31468 x_intersect_rectangles (XRectangle *r1, XRectangle *r2, XRectangle *result)
31470 XRectangle *left, *right;
31471 XRectangle *upper, *lower;
31472 bool intersection_p = false;
31474 /* Rearrange so that R1 is the left-most rectangle. */
31475 if (r1->x < r2->x)
31476 left = r1, right = r2;
31477 else
31478 left = r2, right = r1;
31480 /* X0 of the intersection is right.x0, if this is inside R1,
31481 otherwise there is no intersection. */
31482 if (right->x <= left->x + left->width)
31484 result->x = right->x;
31486 /* The right end of the intersection is the minimum of
31487 the right ends of left and right. */
31488 result->width = (min (left->x + left->width, right->x + right->width)
31489 - result->x);
31491 /* Same game for Y. */
31492 if (r1->y < r2->y)
31493 upper = r1, lower = r2;
31494 else
31495 upper = r2, lower = r1;
31497 /* The upper end of the intersection is lower.y0, if this is inside
31498 of upper. Otherwise, there is no intersection. */
31499 if (lower->y <= upper->y + upper->height)
31501 result->y = lower->y;
31503 /* The lower end of the intersection is the minimum of the lower
31504 ends of upper and lower. */
31505 result->height = (min (lower->y + lower->height,
31506 upper->y + upper->height)
31507 - result->y);
31508 intersection_p = true;
31512 return intersection_p;
31515 #endif /* HAVE_WINDOW_SYSTEM */
31518 /***********************************************************************
31519 Initialization
31520 ***********************************************************************/
31522 void
31523 syms_of_xdisp (void)
31525 Vwith_echo_area_save_vector = Qnil;
31526 staticpro (&Vwith_echo_area_save_vector);
31528 Vmessage_stack = Qnil;
31529 staticpro (&Vmessage_stack);
31531 /* Non-nil means don't actually do any redisplay. */
31532 DEFSYM (Qinhibit_redisplay, "inhibit-redisplay");
31534 DEFSYM (Qredisplay_internal_xC_functionx, "redisplay_internal (C function)");
31536 DEFVAR_BOOL("inhibit-message", inhibit_message,
31537 doc: /* Non-nil means calls to `message' are not displayed.
31538 They are still logged to the *Messages* buffer. */);
31539 inhibit_message = 0;
31541 message_dolog_marker1 = Fmake_marker ();
31542 staticpro (&message_dolog_marker1);
31543 message_dolog_marker2 = Fmake_marker ();
31544 staticpro (&message_dolog_marker2);
31545 message_dolog_marker3 = Fmake_marker ();
31546 staticpro (&message_dolog_marker3);
31548 defsubr (&Sset_buffer_redisplay);
31549 #ifdef GLYPH_DEBUG
31550 defsubr (&Sdump_frame_glyph_matrix);
31551 defsubr (&Sdump_glyph_matrix);
31552 defsubr (&Sdump_glyph_row);
31553 defsubr (&Sdump_tool_bar_row);
31554 defsubr (&Strace_redisplay);
31555 defsubr (&Strace_to_stderr);
31556 #endif
31557 #ifdef HAVE_WINDOW_SYSTEM
31558 defsubr (&Stool_bar_height);
31559 defsubr (&Slookup_image_map);
31560 #endif
31561 defsubr (&Sline_pixel_height);
31562 defsubr (&Sformat_mode_line);
31563 defsubr (&Sinvisible_p);
31564 defsubr (&Scurrent_bidi_paragraph_direction);
31565 defsubr (&Swindow_text_pixel_size);
31566 defsubr (&Smove_point_visually);
31567 defsubr (&Sbidi_find_overridden_directionality);
31569 DEFSYM (Qmenu_bar_update_hook, "menu-bar-update-hook");
31570 DEFSYM (Qoverriding_terminal_local_map, "overriding-terminal-local-map");
31571 DEFSYM (Qoverriding_local_map, "overriding-local-map");
31572 DEFSYM (Qwindow_scroll_functions, "window-scroll-functions");
31573 DEFSYM (Qwindow_text_change_functions, "window-text-change-functions");
31574 DEFSYM (Qredisplay_end_trigger_functions, "redisplay-end-trigger-functions");
31575 DEFSYM (Qinhibit_point_motion_hooks, "inhibit-point-motion-hooks");
31576 DEFSYM (Qeval, "eval");
31577 DEFSYM (QCdata, ":data");
31579 /* Names of text properties relevant for redisplay. */
31580 DEFSYM (Qdisplay, "display");
31581 DEFSYM (Qspace_width, "space-width");
31582 DEFSYM (Qraise, "raise");
31583 DEFSYM (Qslice, "slice");
31584 DEFSYM (Qspace, "space");
31585 DEFSYM (Qmargin, "margin");
31586 DEFSYM (Qpointer, "pointer");
31587 DEFSYM (Qleft_margin, "left-margin");
31588 DEFSYM (Qright_margin, "right-margin");
31589 DEFSYM (Qcenter, "center");
31590 DEFSYM (Qline_height, "line-height");
31591 DEFSYM (QCalign_to, ":align-to");
31592 DEFSYM (QCrelative_width, ":relative-width");
31593 DEFSYM (QCrelative_height, ":relative-height");
31594 DEFSYM (QCeval, ":eval");
31595 DEFSYM (QCpropertize, ":propertize");
31596 DEFSYM (QCfile, ":file");
31597 DEFSYM (Qfontified, "fontified");
31598 DEFSYM (Qfontification_functions, "fontification-functions");
31600 /* Name of the face used to highlight trailing whitespace. */
31601 DEFSYM (Qtrailing_whitespace, "trailing-whitespace");
31603 /* Name and number of the face used to highlight escape glyphs. */
31604 DEFSYM (Qescape_glyph, "escape-glyph");
31606 /* Name and number of the face used to highlight non-breaking
31607 spaces/hyphens. */
31608 DEFSYM (Qnobreak_space, "nobreak-space");
31609 DEFSYM (Qnobreak_hyphen, "nobreak-hyphen");
31611 /* The symbol 'image' which is the car of the lists used to represent
31612 images in Lisp. Also a tool bar style. */
31613 DEFSYM (Qimage, "image");
31615 /* Tool bar styles. */
31616 DEFSYM (Qtext, "text");
31617 DEFSYM (Qboth, "both");
31618 DEFSYM (Qboth_horiz, "both-horiz");
31619 DEFSYM (Qtext_image_horiz, "text-image-horiz");
31621 /* The image map types. */
31622 DEFSYM (QCmap, ":map");
31623 DEFSYM (QCpointer, ":pointer");
31624 DEFSYM (Qrect, "rect");
31625 DEFSYM (Qcircle, "circle");
31626 DEFSYM (Qpoly, "poly");
31628 DEFSYM (Qinhibit_menubar_update, "inhibit-menubar-update");
31630 DEFSYM (Qgrow_only, "grow-only");
31631 DEFSYM (Qinhibit_eval_during_redisplay, "inhibit-eval-during-redisplay");
31632 DEFSYM (Qposition, "position");
31633 DEFSYM (Qbuffer_position, "buffer-position");
31634 DEFSYM (Qobject, "object");
31636 /* Cursor shapes. */
31637 DEFSYM (Qbar, "bar");
31638 DEFSYM (Qhbar, "hbar");
31639 DEFSYM (Qbox, "box");
31640 DEFSYM (Qhollow, "hollow");
31642 /* Pointer shapes. */
31643 DEFSYM (Qhand, "hand");
31644 DEFSYM (Qarrow, "arrow");
31645 /* also Qtext */
31647 DEFSYM (Qdragging, "dragging");
31649 DEFSYM (Qinhibit_free_realized_faces, "inhibit-free-realized-faces");
31651 list_of_error = list1 (list2 (Qerror, Qvoid_variable));
31652 staticpro (&list_of_error);
31654 /* Values of those variables at last redisplay are stored as
31655 properties on 'overlay-arrow-position' symbol. However, if
31656 Voverlay_arrow_position is a marker, last-arrow-position is its
31657 numerical position. */
31658 DEFSYM (Qlast_arrow_position, "last-arrow-position");
31659 DEFSYM (Qlast_arrow_string, "last-arrow-string");
31661 /* Alternative overlay-arrow-string and overlay-arrow-bitmap
31662 properties on a symbol in overlay-arrow-variable-list. */
31663 DEFSYM (Qoverlay_arrow_string, "overlay-arrow-string");
31664 DEFSYM (Qoverlay_arrow_bitmap, "overlay-arrow-bitmap");
31666 echo_buffer[0] = echo_buffer[1] = Qnil;
31667 staticpro (&echo_buffer[0]);
31668 staticpro (&echo_buffer[1]);
31670 echo_area_buffer[0] = echo_area_buffer[1] = Qnil;
31671 staticpro (&echo_area_buffer[0]);
31672 staticpro (&echo_area_buffer[1]);
31674 Vmessages_buffer_name = build_pure_c_string ("*Messages*");
31675 staticpro (&Vmessages_buffer_name);
31677 mode_line_proptrans_alist = Qnil;
31678 staticpro (&mode_line_proptrans_alist);
31679 mode_line_string_list = Qnil;
31680 staticpro (&mode_line_string_list);
31681 mode_line_string_face = Qnil;
31682 staticpro (&mode_line_string_face);
31683 mode_line_string_face_prop = Qnil;
31684 staticpro (&mode_line_string_face_prop);
31685 Vmode_line_unwind_vector = Qnil;
31686 staticpro (&Vmode_line_unwind_vector);
31688 DEFSYM (Qmode_line_default_help_echo, "mode-line-default-help-echo");
31690 help_echo_string = Qnil;
31691 staticpro (&help_echo_string);
31692 help_echo_object = Qnil;
31693 staticpro (&help_echo_object);
31694 help_echo_window = Qnil;
31695 staticpro (&help_echo_window);
31696 previous_help_echo_string = Qnil;
31697 staticpro (&previous_help_echo_string);
31698 help_echo_pos = -1;
31700 DEFSYM (Qright_to_left, "right-to-left");
31701 DEFSYM (Qleft_to_right, "left-to-right");
31702 defsubr (&Sbidi_resolved_levels);
31704 #ifdef HAVE_WINDOW_SYSTEM
31705 DEFVAR_BOOL ("x-stretch-cursor", x_stretch_cursor_p,
31706 doc: /* Non-nil means draw block cursor as wide as the glyph under it.
31707 For example, if a block cursor is over a tab, it will be drawn as
31708 wide as that tab on the display. */);
31709 x_stretch_cursor_p = 0;
31710 #endif
31712 DEFVAR_LISP ("show-trailing-whitespace", Vshow_trailing_whitespace,
31713 doc: /* Non-nil means highlight trailing whitespace.
31714 The face used for trailing whitespace is `trailing-whitespace'. */);
31715 Vshow_trailing_whitespace = Qnil;
31717 DEFVAR_LISP ("nobreak-char-display", Vnobreak_char_display,
31718 doc: /* Control highlighting of non-ASCII space and hyphen chars.
31719 If the value is t, Emacs highlights non-ASCII chars which have the
31720 same appearance as an ASCII space or hyphen, using the `nobreak-space'
31721 or `nobreak-hyphen' face respectively.
31723 U+00A0 (no-break space), U+00AD (soft hyphen), U+2010 (hyphen), and
31724 U+2011 (non-breaking hyphen) are affected.
31726 Any other non-nil value means to display these characters as a escape
31727 glyph followed by an ordinary space or hyphen.
31729 A value of nil means no special handling of these characters. */);
31730 Vnobreak_char_display = Qt;
31732 DEFVAR_LISP ("void-text-area-pointer", Vvoid_text_area_pointer,
31733 doc: /* The pointer shape to show in void text areas.
31734 A value of nil means to show the text pointer. Other options are
31735 `arrow', `text', `hand', `vdrag', `hdrag', `nhdrag', `modeline', and
31736 `hourglass'. */);
31737 Vvoid_text_area_pointer = Qarrow;
31739 DEFVAR_LISP ("inhibit-redisplay", Vinhibit_redisplay,
31740 doc: /* Non-nil means don't actually do any redisplay.
31741 This is used for internal purposes. */);
31742 Vinhibit_redisplay = Qnil;
31744 DEFVAR_LISP ("global-mode-string", Vglobal_mode_string,
31745 doc: /* String (or mode line construct) included (normally) in `mode-line-format'. */);
31746 Vglobal_mode_string = Qnil;
31748 DEFVAR_LISP ("overlay-arrow-position", Voverlay_arrow_position,
31749 doc: /* Marker for where to display an arrow on top of the buffer text.
31750 This must be the beginning of a line in order to work.
31751 See also `overlay-arrow-string'. */);
31752 Voverlay_arrow_position = Qnil;
31754 DEFVAR_LISP ("overlay-arrow-string", Voverlay_arrow_string,
31755 doc: /* String to display as an arrow in non-window frames.
31756 See also `overlay-arrow-position'. */);
31757 Voverlay_arrow_string = build_pure_c_string ("=>");
31759 DEFVAR_LISP ("overlay-arrow-variable-list", Voverlay_arrow_variable_list,
31760 doc: /* List of variables (symbols) which hold markers for overlay arrows.
31761 The symbols on this list are examined during redisplay to determine
31762 where to display overlay arrows. */);
31763 Voverlay_arrow_variable_list
31764 = list1 (intern_c_string ("overlay-arrow-position"));
31766 DEFVAR_INT ("scroll-step", emacs_scroll_step,
31767 doc: /* The number of lines to try scrolling a window by when point moves out.
31768 If that fails to bring point back on frame, point is centered instead.
31769 If this is zero, point is always centered after it moves off frame.
31770 If you want scrolling to always be a line at a time, you should set
31771 `scroll-conservatively' to a large value rather than set this to 1. */);
31773 DEFVAR_INT ("scroll-conservatively", scroll_conservatively,
31774 doc: /* Scroll up to this many lines, to bring point back on screen.
31775 If point moves off-screen, redisplay will scroll by up to
31776 `scroll-conservatively' lines in order to bring point just barely
31777 onto the screen again. If that cannot be done, then redisplay
31778 recenters point as usual.
31780 If the value is greater than 100, redisplay will never recenter point,
31781 but will always scroll just enough text to bring point into view, even
31782 if you move far away.
31784 A value of zero means always recenter point if it moves off screen. */);
31785 scroll_conservatively = 0;
31787 DEFVAR_INT ("scroll-margin", scroll_margin,
31788 doc: /* Number of lines of margin at the top and bottom of a window.
31789 Recenter the window whenever point gets within this many lines
31790 of the top or bottom of the window. */);
31791 scroll_margin = 0;
31793 DEFVAR_LISP ("maximum-scroll-margin", Vmaximum_scroll_margin,
31794 doc: /* Maximum effective value of `scroll-margin'.
31795 Given as a fraction of the current window's lines. The value should
31796 be a floating point number between 0.0 and 0.5. The effective maximum
31797 is limited to (/ (1- window-lines) 2). Non-float values for this
31798 variable are ignored and the default 0.25 is used instead. */);
31799 Vmaximum_scroll_margin = make_float (0.25);
31801 DEFVAR_LISP ("display-pixels-per-inch", Vdisplay_pixels_per_inch,
31802 doc: /* Pixels per inch value for non-window system displays.
31803 Value is a number or a cons (WIDTH-DPI . HEIGHT-DPI). */);
31804 Vdisplay_pixels_per_inch = make_float (72.0);
31806 #ifdef GLYPH_DEBUG
31807 DEFVAR_INT ("debug-end-pos", debug_end_pos, doc: /* Don't ask. */);
31808 #endif
31810 DEFVAR_LISP ("truncate-partial-width-windows",
31811 Vtruncate_partial_width_windows,
31812 doc: /* Non-nil means truncate lines in windows narrower than the frame.
31813 For an integer value, truncate lines in each window narrower than the
31814 full frame width, provided the total window width in column units is less
31815 than that integer; otherwise, respect the value of `truncate-lines'.
31816 The total width of the window is as returned by `window-total-width', it
31817 includes the fringes, the continuation and truncation glyphs, the
31818 display margins (if any), and the scroll bar
31820 For any other non-nil value, truncate lines in all windows that do
31821 not span the full frame width.
31823 A value of nil means to respect the value of `truncate-lines'.
31825 If `word-wrap' is enabled, you might want to reduce this. */);
31826 Vtruncate_partial_width_windows = make_number (50);
31828 DEFVAR_LISP ("line-number-display-limit", Vline_number_display_limit,
31829 doc: /* Maximum buffer size for which line number should be displayed.
31830 If the buffer is bigger than this, the line number does not appear
31831 in the mode line. A value of nil means no limit. */);
31832 Vline_number_display_limit = Qnil;
31834 DEFVAR_INT ("line-number-display-limit-width",
31835 line_number_display_limit_width,
31836 doc: /* Maximum line width (in characters) for line number display.
31837 If the average length of the lines near point is bigger than this, then the
31838 line number may be omitted from the mode line. */);
31839 line_number_display_limit_width = 200;
31841 DEFVAR_BOOL ("highlight-nonselected-windows", highlight_nonselected_windows,
31842 doc: /* Non-nil means highlight region even in nonselected windows. */);
31843 highlight_nonselected_windows = false;
31845 DEFVAR_BOOL ("multiple-frames", multiple_frames,
31846 doc: /* Non-nil if more than one frame is visible on this display.
31847 Minibuffer-only frames don't count, but iconified frames do.
31848 This variable is not guaranteed to be accurate except while processing
31849 `frame-title-format' and `icon-title-format'. */);
31851 DEFVAR_LISP ("frame-title-format", Vframe_title_format,
31852 doc: /* Template for displaying the title bar of visible frames.
31853 \(Assuming the window manager supports this feature.)
31855 This variable has the same structure as `mode-line-format', except that
31856 the %c, %C, and %l constructs are ignored. It is used only on frames for
31857 which no explicit name has been set (see `modify-frame-parameters'). */);
31859 DEFVAR_LISP ("icon-title-format", Vicon_title_format,
31860 doc: /* Template for displaying the title bar of an iconified frame.
31861 \(Assuming the window manager supports this feature.)
31862 This variable has the same structure as `mode-line-format' (which see),
31863 and is used only on frames for which no explicit name has been set
31864 \(see `modify-frame-parameters'). */);
31865 Vicon_title_format
31866 = Vframe_title_format
31867 = listn (CONSTYPE_PURE, 3,
31868 intern_c_string ("multiple-frames"),
31869 build_pure_c_string ("%b"),
31870 listn (CONSTYPE_PURE, 4,
31871 empty_unibyte_string,
31872 intern_c_string ("invocation-name"),
31873 build_pure_c_string ("@"),
31874 intern_c_string ("system-name")));
31876 DEFVAR_LISP ("message-log-max", Vmessage_log_max,
31877 doc: /* Maximum number of lines to keep in the message log buffer.
31878 If nil, disable message logging. If t, log messages but don't truncate
31879 the buffer when it becomes large. */);
31880 Vmessage_log_max = make_number (1000);
31882 DEFVAR_LISP ("window-scroll-functions", Vwindow_scroll_functions,
31883 doc: /* List of functions to call before redisplaying a window with scrolling.
31884 Each function is called with two arguments, the window and its new
31885 display-start position.
31886 These functions are called whenever the `window-start' marker is modified,
31887 either to point into another buffer (e.g. via `set-window-buffer') or another
31888 place in the same buffer.
31889 Note that the value of `window-end' is not valid when these functions are
31890 called.
31892 Warning: Do not use this feature to alter the way the window
31893 is scrolled. It is not designed for that, and such use probably won't
31894 work. */);
31895 Vwindow_scroll_functions = Qnil;
31897 DEFVAR_LISP ("window-text-change-functions",
31898 Vwindow_text_change_functions,
31899 doc: /* Functions to call in redisplay when text in the window might change. */);
31900 Vwindow_text_change_functions = Qnil;
31902 DEFVAR_LISP ("redisplay-end-trigger-functions", Vredisplay_end_trigger_functions,
31903 doc: /* Functions called when redisplay of a window reaches the end trigger.
31904 Each function is called with two arguments, the window and the end trigger value.
31905 See `set-window-redisplay-end-trigger'. */);
31906 Vredisplay_end_trigger_functions = Qnil;
31908 DEFVAR_LISP ("mouse-autoselect-window", Vmouse_autoselect_window,
31909 doc: /* Non-nil means autoselect window with mouse pointer.
31910 If nil, do not autoselect windows.
31911 A positive number means delay autoselection by that many seconds: a
31912 window is autoselected only after the mouse has remained in that
31913 window for the duration of the delay.
31914 A negative number has a similar effect, but causes windows to be
31915 autoselected only after the mouse has stopped moving. (Because of
31916 the way Emacs compares mouse events, you will occasionally wait twice
31917 that time before the window gets selected.)
31918 Any other value means to autoselect window instantaneously when the
31919 mouse pointer enters it.
31921 Autoselection selects the minibuffer only if it is active, and never
31922 unselects the minibuffer if it is active.
31924 When customizing this variable make sure that the actual value of
31925 `focus-follows-mouse' matches the behavior of your window manager. */);
31926 Vmouse_autoselect_window = Qnil;
31928 DEFVAR_LISP ("auto-resize-tool-bars", Vauto_resize_tool_bars,
31929 doc: /* Non-nil means automatically resize tool-bars.
31930 This dynamically changes the tool-bar's height to the minimum height
31931 that is needed to make all tool-bar items visible.
31932 If value is `grow-only', the tool-bar's height is only increased
31933 automatically; to decrease the tool-bar height, use \\[recenter]. */);
31934 Vauto_resize_tool_bars = Qt;
31936 DEFVAR_BOOL ("auto-raise-tool-bar-buttons", auto_raise_tool_bar_buttons_p,
31937 doc: /* Non-nil means raise tool-bar buttons when the mouse moves over them. */);
31938 auto_raise_tool_bar_buttons_p = true;
31940 DEFVAR_BOOL ("make-cursor-line-fully-visible", make_cursor_line_fully_visible_p,
31941 doc: /* Non-nil means to scroll (recenter) cursor line if it is not fully visible. */);
31942 make_cursor_line_fully_visible_p = true;
31944 DEFVAR_LISP ("tool-bar-border", Vtool_bar_border,
31945 doc: /* Border below tool-bar in pixels.
31946 If an integer, use it as the height of the border.
31947 If it is one of `internal-border-width' or `border-width', use the
31948 value of the corresponding frame parameter.
31949 Otherwise, no border is added below the tool-bar. */);
31950 Vtool_bar_border = Qinternal_border_width;
31952 DEFVAR_LISP ("tool-bar-button-margin", Vtool_bar_button_margin,
31953 doc: /* Margin around tool-bar buttons in pixels.
31954 If an integer, use that for both horizontal and vertical margins.
31955 Otherwise, value should be a pair of integers `(HORZ . VERT)' with
31956 HORZ specifying the horizontal margin, and VERT specifying the
31957 vertical margin. */);
31958 Vtool_bar_button_margin = make_number (DEFAULT_TOOL_BAR_BUTTON_MARGIN);
31960 DEFVAR_INT ("tool-bar-button-relief", tool_bar_button_relief,
31961 doc: /* Relief thickness of tool-bar buttons. */);
31962 tool_bar_button_relief = DEFAULT_TOOL_BAR_BUTTON_RELIEF;
31964 DEFVAR_LISP ("tool-bar-style", Vtool_bar_style,
31965 doc: /* Tool bar style to use.
31966 It can be one of
31967 image - show images only
31968 text - show text only
31969 both - show both, text below image
31970 both-horiz - show text to the right of the image
31971 text-image-horiz - show text to the left of the image
31972 any other - use system default or image if no system default.
31974 This variable only affects the GTK+ toolkit version of Emacs. */);
31975 Vtool_bar_style = Qnil;
31977 DEFVAR_INT ("tool-bar-max-label-size", tool_bar_max_label_size,
31978 doc: /* Maximum number of characters a label can have to be shown.
31979 The tool bar style must also show labels for this to have any effect, see
31980 `tool-bar-style'. */);
31981 tool_bar_max_label_size = DEFAULT_TOOL_BAR_LABEL_SIZE;
31983 DEFVAR_LISP ("fontification-functions", Vfontification_functions,
31984 doc: /* List of functions to call to fontify regions of text.
31985 Each function is called with one argument POS. Functions must
31986 fontify a region starting at POS in the current buffer, and give
31987 fontified regions the property `fontified'. */);
31988 Vfontification_functions = Qnil;
31989 Fmake_variable_buffer_local (Qfontification_functions);
31991 DEFVAR_BOOL ("unibyte-display-via-language-environment",
31992 unibyte_display_via_language_environment,
31993 doc: /* Non-nil means display unibyte text according to language environment.
31994 Specifically, this means that raw bytes in the range 160-255 decimal
31995 are displayed by converting them to the equivalent multibyte characters
31996 according to the current language environment. As a result, they are
31997 displayed according to the current fontset.
31999 Note that this variable affects only how these bytes are displayed,
32000 but does not change the fact they are interpreted as raw bytes. */);
32001 unibyte_display_via_language_environment = false;
32003 DEFVAR_LISP ("max-mini-window-height", Vmax_mini_window_height,
32004 doc: /* Maximum height for resizing mini-windows (the minibuffer and the echo area).
32005 If a float, it specifies a fraction of the mini-window frame's height.
32006 If an integer, it specifies a number of lines. */);
32007 Vmax_mini_window_height = make_float (0.25);
32009 DEFVAR_LISP ("resize-mini-windows", Vresize_mini_windows,
32010 doc: /* How to resize mini-windows (the minibuffer and the echo area).
32011 A value of nil means don't automatically resize mini-windows.
32012 A value of t means resize them to fit the text displayed in them.
32013 A value of `grow-only', the default, means let mini-windows grow only;
32014 they return to their normal size when the minibuffer is closed, or the
32015 echo area becomes empty. */);
32016 /* Contrary to the doc string, we initialize this to nil, so that
32017 loading loadup.el won't try to resize windows before loading
32018 window.el, where some functions we need to call for this live.
32019 We assign the 'grow-only' value right after loading window.el
32020 during loadup. */
32021 Vresize_mini_windows = Qnil;
32023 DEFVAR_LISP ("blink-cursor-alist", Vblink_cursor_alist,
32024 doc: /* Alist specifying how to blink the cursor off.
32025 Each element has the form (ON-STATE . OFF-STATE). Whenever the
32026 `cursor-type' frame-parameter or variable equals ON-STATE,
32027 comparing using `equal', Emacs uses OFF-STATE to specify
32028 how to blink it off. ON-STATE and OFF-STATE are values for
32029 the `cursor-type' frame parameter.
32031 If a frame's ON-STATE has no entry in this list,
32032 the frame's other specifications determine how to blink the cursor off. */);
32033 Vblink_cursor_alist = Qnil;
32035 DEFVAR_LISP ("auto-hscroll-mode", automatic_hscrolling,
32036 doc: /* Allow or disallow automatic horizontal scrolling of windows.
32037 The value `current-line' means the line displaying point in each window
32038 is automatically scrolled horizontally to make point visible.
32039 Any other non-nil value means all the lines in a window are automatically
32040 scrolled horizontally to make point visible. */);
32041 automatic_hscrolling = Qt;
32042 DEFSYM (Qauto_hscroll_mode, "auto-hscroll-mode");
32043 DEFSYM (Qcurrent_line, "current-line");
32045 DEFVAR_INT ("hscroll-margin", hscroll_margin,
32046 doc: /* How many columns away from the window edge point is allowed to get
32047 before automatic hscrolling will horizontally scroll the window. */);
32048 hscroll_margin = 5;
32050 DEFVAR_LISP ("hscroll-step", Vhscroll_step,
32051 doc: /* How many columns to scroll the window when point gets too close to the edge.
32052 When point is less than `hscroll-margin' columns from the window
32053 edge, automatic hscrolling will scroll the window by the amount of columns
32054 determined by this variable. If its value is a positive integer, scroll that
32055 many columns. If it's a positive floating-point number, it specifies the
32056 fraction of the window's width to scroll. If it's nil or zero, point will be
32057 centered horizontally after the scroll. Any other value, including negative
32058 numbers, are treated as if the value were zero.
32060 Automatic hscrolling always moves point outside the scroll margin, so if
32061 point was more than scroll step columns inside the margin, the window will
32062 scroll more than the value given by the scroll step.
32064 Note that the lower bound for automatic hscrolling specified by `scroll-left'
32065 and `scroll-right' overrides this variable's effect. */);
32066 Vhscroll_step = make_number (0);
32068 DEFVAR_BOOL ("message-truncate-lines", message_truncate_lines,
32069 doc: /* If non-nil, messages are truncated instead of resizing the echo area.
32070 Bind this around calls to `message' to let it take effect. */);
32071 message_truncate_lines = false;
32073 DEFVAR_LISP ("menu-bar-update-hook", Vmenu_bar_update_hook,
32074 doc: /* Normal hook run to update the menu bar definitions.
32075 Redisplay runs this hook before it redisplays the menu bar.
32076 This is used to update menus such as Buffers, whose contents depend on
32077 various data. */);
32078 Vmenu_bar_update_hook = Qnil;
32080 DEFVAR_LISP ("menu-updating-frame", Vmenu_updating_frame,
32081 doc: /* Frame for which we are updating a menu.
32082 The enable predicate for a menu binding should check this variable. */);
32083 Vmenu_updating_frame = Qnil;
32085 DEFVAR_BOOL ("inhibit-menubar-update", inhibit_menubar_update,
32086 doc: /* Non-nil means don't update menu bars. Internal use only. */);
32087 inhibit_menubar_update = false;
32089 DEFVAR_LISP ("wrap-prefix", Vwrap_prefix,
32090 doc: /* Prefix prepended to all continuation lines at display time.
32091 The value may be a string, an image, or a stretch-glyph; it is
32092 interpreted in the same way as the value of a `display' text property.
32094 This variable is overridden by any `wrap-prefix' text or overlay
32095 property.
32097 To add a prefix to non-continuation lines, use `line-prefix'. */);
32098 Vwrap_prefix = Qnil;
32099 DEFSYM (Qwrap_prefix, "wrap-prefix");
32100 Fmake_variable_buffer_local (Qwrap_prefix);
32102 DEFVAR_LISP ("line-prefix", Vline_prefix,
32103 doc: /* Prefix prepended to all non-continuation lines at display time.
32104 The value may be a string, an image, or a stretch-glyph; it is
32105 interpreted in the same way as the value of a `display' text property.
32107 This variable is overridden by any `line-prefix' text or overlay
32108 property.
32110 To add a prefix to continuation lines, use `wrap-prefix'. */);
32111 Vline_prefix = Qnil;
32112 DEFSYM (Qline_prefix, "line-prefix");
32113 Fmake_variable_buffer_local (Qline_prefix);
32115 DEFVAR_BOOL ("inhibit-eval-during-redisplay", inhibit_eval_during_redisplay,
32116 doc: /* Non-nil means don't eval Lisp during redisplay. */);
32117 inhibit_eval_during_redisplay = false;
32119 DEFVAR_BOOL ("inhibit-free-realized-faces", inhibit_free_realized_faces,
32120 doc: /* Non-nil means don't free realized faces. Internal use only. */);
32121 inhibit_free_realized_faces = false;
32123 DEFVAR_BOOL ("inhibit-bidi-mirroring", inhibit_bidi_mirroring,
32124 doc: /* Non-nil means don't mirror characters even when bidi context requires that.
32125 Intended for use during debugging and for testing bidi display;
32126 see biditest.el in the test suite. */);
32127 inhibit_bidi_mirroring = false;
32129 #ifdef GLYPH_DEBUG
32130 DEFVAR_BOOL ("inhibit-try-window-id", inhibit_try_window_id,
32131 doc: /* Inhibit try_window_id display optimization. */);
32132 inhibit_try_window_id = false;
32134 DEFVAR_BOOL ("inhibit-try-window-reusing", inhibit_try_window_reusing,
32135 doc: /* Inhibit try_window_reusing display optimization. */);
32136 inhibit_try_window_reusing = false;
32138 DEFVAR_BOOL ("inhibit-try-cursor-movement", inhibit_try_cursor_movement,
32139 doc: /* Inhibit try_cursor_movement display optimization. */);
32140 inhibit_try_cursor_movement = false;
32141 #endif /* GLYPH_DEBUG */
32143 DEFVAR_INT ("overline-margin", overline_margin,
32144 doc: /* Space between overline and text, in pixels.
32145 The default value is 2: the height of the overline (1 pixel) plus 1 pixel
32146 margin to the character height. */);
32147 overline_margin = 2;
32149 DEFVAR_INT ("underline-minimum-offset",
32150 underline_minimum_offset,
32151 doc: /* Minimum distance between baseline and underline.
32152 This can improve legibility of underlined text at small font sizes,
32153 particularly when using variable `x-use-underline-position-properties'
32154 with fonts that specify an UNDERLINE_POSITION relatively close to the
32155 baseline. The default value is 1. */);
32156 underline_minimum_offset = 1;
32158 DEFVAR_BOOL ("display-hourglass", display_hourglass_p,
32159 doc: /* Non-nil means show an hourglass pointer, when Emacs is busy.
32160 This feature only works when on a window system that can change
32161 cursor shapes. */);
32162 display_hourglass_p = true;
32164 DEFVAR_LISP ("hourglass-delay", Vhourglass_delay,
32165 doc: /* Seconds to wait before displaying an hourglass pointer when Emacs is busy. */);
32166 Vhourglass_delay = make_number (DEFAULT_HOURGLASS_DELAY);
32168 #ifdef HAVE_WINDOW_SYSTEM
32169 hourglass_atimer = NULL;
32170 hourglass_shown_p = false;
32171 #endif /* HAVE_WINDOW_SYSTEM */
32173 /* Name of the face used to display glyphless characters. */
32174 DEFSYM (Qglyphless_char, "glyphless-char");
32176 /* Method symbols for Vglyphless_char_display. */
32177 DEFSYM (Qhex_code, "hex-code");
32178 DEFSYM (Qempty_box, "empty-box");
32179 DEFSYM (Qthin_space, "thin-space");
32180 DEFSYM (Qzero_width, "zero-width");
32182 DEFVAR_LISP ("pre-redisplay-function", Vpre_redisplay_function,
32183 doc: /* Function run just before redisplay.
32184 It is called with one argument, which is the set of windows that are to
32185 be redisplayed. This set can be nil (meaning, only the selected window),
32186 or t (meaning all windows). */);
32187 Vpre_redisplay_function = intern ("ignore");
32189 /* Symbol for the purpose of Vglyphless_char_display. */
32190 DEFSYM (Qglyphless_char_display, "glyphless-char-display");
32191 Fput (Qglyphless_char_display, Qchar_table_extra_slots, make_number (1));
32193 DEFVAR_LISP ("glyphless-char-display", Vglyphless_char_display,
32194 doc: /* Char-table defining glyphless characters.
32195 Each element, if non-nil, should be one of the following:
32196 an ASCII acronym string: display this string in a box
32197 `hex-code': display the hexadecimal code of a character in a box
32198 `empty-box': display as an empty box
32199 `thin-space': display as 1-pixel width space
32200 `zero-width': don't display
32201 An element may also be a cons cell (GRAPHICAL . TEXT), which specifies the
32202 display method for graphical terminals and text terminals respectively.
32203 GRAPHICAL and TEXT should each have one of the values listed above.
32205 The char-table has one extra slot to control the display of a character for
32206 which no font is found. This slot only takes effect on graphical terminals.
32207 Its value should be an ASCII acronym string, `hex-code', `empty-box', or
32208 `thin-space'. The default is `empty-box'.
32210 If a character has a non-nil entry in an active display table, the
32211 display table takes effect; in this case, Emacs does not consult
32212 `glyphless-char-display' at all. */);
32213 Vglyphless_char_display = Fmake_char_table (Qglyphless_char_display, Qnil);
32214 Fset_char_table_extra_slot (Vglyphless_char_display, make_number (0),
32215 Qempty_box);
32217 DEFVAR_LISP ("debug-on-message", Vdebug_on_message,
32218 doc: /* If non-nil, debug if a message matching this regexp is displayed. */);
32219 Vdebug_on_message = Qnil;
32221 DEFVAR_LISP ("redisplay--all-windows-cause", Vredisplay__all_windows_cause,
32222 doc: /* */);
32223 Vredisplay__all_windows_cause = Fmake_hash_table (0, NULL);
32225 DEFVAR_LISP ("redisplay--mode-lines-cause", Vredisplay__mode_lines_cause,
32226 doc: /* */);
32227 Vredisplay__mode_lines_cause = Fmake_hash_table (0, NULL);
32229 DEFVAR_BOOL ("redisplay--inhibit-bidi", redisplay__inhibit_bidi,
32230 doc: /* Non-nil means it is not safe to attempt bidi reordering for display. */);
32231 /* Initialize to t, since we need to disable reordering until
32232 loadup.el successfully loads charprop.el. */
32233 redisplay__inhibit_bidi = true;
32237 /* Initialize this module when Emacs starts. */
32239 void
32240 init_xdisp (void)
32242 CHARPOS (this_line_start_pos) = 0;
32244 if (!noninteractive)
32246 struct window *m = XWINDOW (minibuf_window);
32247 Lisp_Object frame = m->frame;
32248 struct frame *f = XFRAME (frame);
32249 Lisp_Object root = FRAME_ROOT_WINDOW (f);
32250 struct window *r = XWINDOW (root);
32251 int i;
32253 echo_area_window = minibuf_window;
32255 r->top_line = FRAME_TOP_MARGIN (f);
32256 r->pixel_top = r->top_line * FRAME_LINE_HEIGHT (f);
32257 r->total_cols = FRAME_COLS (f);
32258 r->pixel_width = r->total_cols * FRAME_COLUMN_WIDTH (f);
32259 r->total_lines = FRAME_TOTAL_LINES (f) - 1 - FRAME_TOP_MARGIN (f);
32260 r->pixel_height = r->total_lines * FRAME_LINE_HEIGHT (f);
32262 m->top_line = FRAME_TOTAL_LINES (f) - 1;
32263 m->pixel_top = m->top_line * FRAME_LINE_HEIGHT (f);
32264 m->total_cols = FRAME_COLS (f);
32265 m->pixel_width = m->total_cols * FRAME_COLUMN_WIDTH (f);
32266 m->total_lines = 1;
32267 m->pixel_height = m->total_lines * FRAME_LINE_HEIGHT (f);
32269 scratch_glyph_row.glyphs[TEXT_AREA] = scratch_glyphs;
32270 scratch_glyph_row.glyphs[TEXT_AREA + 1]
32271 = scratch_glyphs + MAX_SCRATCH_GLYPHS;
32273 /* The default ellipsis glyphs `...'. */
32274 for (i = 0; i < 3; ++i)
32275 default_invis_vector[i] = make_number ('.');
32279 /* Allocate the buffer for frame titles.
32280 Also used for `format-mode-line'. */
32281 int size = 100;
32282 mode_line_noprop_buf = xmalloc (size);
32283 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
32284 mode_line_noprop_ptr = mode_line_noprop_buf;
32285 mode_line_target = MODE_LINE_DISPLAY;
32288 help_echo_showing_p = false;
32291 #ifdef HAVE_WINDOW_SYSTEM
32293 /* Platform-independent portion of hourglass implementation. */
32295 /* Timer function of hourglass_atimer. */
32297 static void
32298 show_hourglass (struct atimer *timer)
32300 /* The timer implementation will cancel this timer automatically
32301 after this function has run. Set hourglass_atimer to null
32302 so that we know the timer doesn't have to be canceled. */
32303 hourglass_atimer = NULL;
32305 if (!hourglass_shown_p)
32307 Lisp_Object tail, frame;
32309 block_input ();
32311 FOR_EACH_FRAME (tail, frame)
32313 struct frame *f = XFRAME (frame);
32315 if (FRAME_LIVE_P (f) && FRAME_WINDOW_P (f)
32316 && FRAME_RIF (f)->show_hourglass)
32317 FRAME_RIF (f)->show_hourglass (f);
32320 hourglass_shown_p = true;
32321 unblock_input ();
32325 /* Cancel a currently active hourglass timer, and start a new one. */
32327 void
32328 start_hourglass (void)
32330 struct timespec delay;
32332 cancel_hourglass ();
32334 if (INTEGERP (Vhourglass_delay)
32335 && XINT (Vhourglass_delay) > 0)
32336 delay = make_timespec (min (XINT (Vhourglass_delay),
32337 TYPE_MAXIMUM (time_t)),
32339 else if (FLOATP (Vhourglass_delay)
32340 && XFLOAT_DATA (Vhourglass_delay) > 0)
32341 delay = dtotimespec (XFLOAT_DATA (Vhourglass_delay));
32342 else
32343 delay = make_timespec (DEFAULT_HOURGLASS_DELAY, 0);
32345 hourglass_atimer = start_atimer (ATIMER_RELATIVE, delay,
32346 show_hourglass, NULL);
32349 /* Cancel the hourglass cursor timer if active, hide a busy cursor if
32350 shown. */
32352 void
32353 cancel_hourglass (void)
32355 if (hourglass_atimer)
32357 cancel_atimer (hourglass_atimer);
32358 hourglass_atimer = NULL;
32361 if (hourglass_shown_p)
32363 Lisp_Object tail, frame;
32365 block_input ();
32367 FOR_EACH_FRAME (tail, frame)
32369 struct frame *f = XFRAME (frame);
32371 if (FRAME_LIVE_P (f) && FRAME_WINDOW_P (f)
32372 && FRAME_RIF (f)->hide_hourglass)
32373 FRAME_RIF (f)->hide_hourglass (f);
32374 #ifdef HAVE_NTGUI
32375 /* No cursors on non GUI frames - restore to stock arrow cursor. */
32376 else if (!FRAME_W32_P (f))
32377 w32_arrow_cursor ();
32378 #endif
32381 hourglass_shown_p = false;
32382 unblock_input ();
32386 #endif /* HAVE_WINDOW_SYSTEM */