* src/keyboard.c (syms_of_keyboard): Doc fix. (Bug#30588)
[emacs.git] / src / xdisp.c
blobb003a2f9ccc29a5dd6840812def64c674c46be22
1 /* Display generation from window structure and buffer text.
3 Copyright (C) 1985-1988, 1993-1995, 1997-2018 Free Software Foundation,
4 Inc.
6 This file is part of GNU Emacs.
8 GNU Emacs is free software: you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation, either version 3 of the License, or (at
11 your option) any later version.
13 GNU Emacs is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GNU Emacs. If not, see <https://www.gnu.org/licenses/>. */
21 /* New redisplay written by Gerd Moellmann <gerd@gnu.org>.
23 Redisplay.
25 Emacs separates the task of updating the display from code
26 modifying global state, e.g. buffer text. This way functions
27 operating on buffers don't also have to be concerned with updating
28 the display.
30 Updating the display is triggered by the Lisp interpreter when it
31 decides it's time to do it. This is done either automatically for
32 you as part of the interpreter's command loop or as the result of
33 calling Lisp functions like `sit-for'. The C function `redisplay'
34 in xdisp.c is the only entry into the inner redisplay code.
36 The following diagram shows how redisplay code is invoked. As you
37 can see, Lisp calls redisplay and vice versa.
39 Under window systems like X, some portions of the redisplay code
40 are also called asynchronously, due to mouse movement or expose
41 events. "Asynchronously" in this context means that any C function
42 which calls maybe_quit or process_pending_signals could enter
43 redisplay via expose_frame and/or note_mouse_highlight, if X events
44 were recently reported to Emacs about mouse movements or frame(s)
45 that were exposed. And such redisplay could invoke the Lisp
46 interpreter, e.g. via the :eval forms in mode-line-format, and as
47 result the global state could change. It is therefore very
48 important that C functions which might cause such "asynchronous"
49 redisplay, but cannot tolerate the results, use
50 block_input/unblock_input around code fragments which assume that
51 global Lisp state doesn't change. If you don't follow this rule,
52 you will encounter bugs which are very hard to explain. One place
53 that needs to take such precautions is timer_check, some of whose
54 code cannot tolerate changes in timer alists while it processes
55 timers.
57 +--------------+ redisplay +----------------+
58 | Lisp machine |---------------->| Redisplay code |<--+
59 +--------------+ (xdisp.c) +----------------+ |
60 ^ | |
61 +----------------------------------+ |
62 Block input to prevent this when |
63 called asynchronously! |
65 note_mouse_highlight (asynchronous) |
67 X mouse events -----+
69 expose_frame (asynchronous) |
71 X expose events -----+
73 What does redisplay do? Obviously, it has to figure out somehow what
74 has been changed since the last time the display has been updated,
75 and to make these changes visible. Preferably it would do that in
76 a moderately intelligent way, i.e. fast.
78 Changes in buffer text can be deduced from window and buffer
79 structures, and from some global variables like `beg_unchanged' and
80 `end_unchanged'. The contents of the display are additionally
81 recorded in a `glyph matrix', a two-dimensional matrix of glyph
82 structures. Each row in such a matrix corresponds to a line on the
83 display, and each glyph in a row corresponds to a column displaying
84 a character, an image, or what else. This matrix is called the
85 `current glyph matrix' or `current matrix' in redisplay
86 terminology.
88 For buffer parts that have been changed since the last update, a
89 second glyph matrix is constructed, the so called `desired glyph
90 matrix' or short `desired matrix'. Current and desired matrix are
91 then compared to find a cheap way to update the display, e.g. by
92 reusing part of the display by scrolling lines.
94 You will find a lot of redisplay optimizations when you start
95 looking at the innards of redisplay. The overall goal of all these
96 optimizations is to make redisplay fast because it is done
97 frequently. Some of these optimizations are implemented by the
98 following functions:
100 . try_cursor_movement
102 This function tries to update the display if the text in the
103 window did not change and did not scroll, only point moved, and
104 it did not move off the displayed portion of the text.
106 . try_window_reusing_current_matrix
108 This function reuses the current matrix of a window when text
109 has not changed, but the window start changed (e.g., due to
110 scrolling).
112 . try_window_id
114 This function attempts to redisplay a window by reusing parts of
115 its existing display. It finds and reuses the part that was not
116 changed, and redraws the rest. (The "id" part in the function's
117 name stands for "insert/delete", not for "identification" or
118 somesuch.)
120 . try_window
122 This function performs the full redisplay of a single window
123 assuming that its fonts were not changed and that the cursor
124 will not end up in the scroll margins. (Loading fonts requires
125 re-adjustment of dimensions of glyph matrices, which makes this
126 method impossible to use.)
128 These optimizations are tried in sequence (some can be skipped if
129 it is known that they are not applicable). If none of the
130 optimizations were successful, redisplay calls redisplay_windows,
131 which performs a full redisplay of all windows.
133 Note that there's one more important optimization up Emacs's
134 sleeve, but it is related to actually redrawing the potentially
135 changed portions of the window/frame, not to reproducing the
136 desired matrices of those potentially changed portions. Namely,
137 the function update_frame and its subroutines, which you will find
138 in dispnew.c, compare the desired matrices with the current
139 matrices, and only redraw the portions that changed. So it could
140 happen that the functions in this file for some reason decide that
141 the entire desired matrix needs to be regenerated from scratch, and
142 still only parts of the Emacs display, or even nothing at all, will
143 be actually delivered to the glass, because update_frame has found
144 that the new and the old screen contents are similar or identical.
146 Desired matrices.
148 Desired matrices are always built per Emacs window. The function
149 `display_line' is the central function to look at if you are
150 interested. It constructs one row in a desired matrix given an
151 iterator structure containing both a buffer position and a
152 description of the environment in which the text is to be
153 displayed. But this is too early, read on.
155 Characters and pixmaps displayed for a range of buffer text depend
156 on various settings of buffers and windows, on overlays and text
157 properties, on display tables, on selective display. The good news
158 is that all this hairy stuff is hidden behind a small set of
159 interface functions taking an iterator structure (struct it)
160 argument.
162 Iteration over things to be displayed is then simple. It is
163 started by initializing an iterator with a call to init_iterator,
164 passing it the buffer position where to start iteration. For
165 iteration over strings, pass -1 as the position to init_iterator,
166 and call reseat_to_string when the string is ready, to initialize
167 the iterator for that string. Thereafter, calls to
168 get_next_display_element fill the iterator structure with relevant
169 information about the next thing to display. Calls to
170 set_iterator_to_next move the iterator to the next thing.
172 Besides this, an iterator also contains information about the
173 display environment in which glyphs for display elements are to be
174 produced. It has fields for the width and height of the display,
175 the information whether long lines are truncated or continued, a
176 current X and Y position, and lots of other stuff you can better
177 see in dispextern.h.
179 Glyphs in a desired matrix are normally constructed in a loop
180 calling get_next_display_element and then PRODUCE_GLYPHS. The call
181 to PRODUCE_GLYPHS will fill the iterator structure with pixel
182 information about the element being displayed and at the same time
183 produce glyphs for it. If the display element fits on the line
184 being displayed, set_iterator_to_next is called next, otherwise the
185 glyphs produced are discarded. The function display_line is the
186 workhorse of filling glyph rows in the desired matrix with glyphs.
187 In addition to producing glyphs, it also handles line truncation
188 and continuation, word wrap, and cursor positioning (for the
189 latter, see also set_cursor_from_row).
191 Frame matrices.
193 That just couldn't be all, could it? What about terminal types not
194 supporting operations on sub-windows of the screen? To update the
195 display on such a terminal, window-based glyph matrices are not
196 well suited. To be able to reuse part of the display (scrolling
197 lines up and down), we must instead have a view of the whole
198 screen. This is what `frame matrices' are for. They are a trick.
200 Frames on terminals like above have a glyph pool. Windows on such
201 a frame sub-allocate their glyph memory from their frame's glyph
202 pool. The frame itself is given its own glyph matrices. By
203 coincidence---or maybe something else---rows in window glyph
204 matrices are slices of corresponding rows in frame matrices. Thus
205 writing to window matrices implicitly updates a frame matrix which
206 provides us with the view of the whole screen that we originally
207 wanted to have without having to move many bytes around. To be
208 honest, there is a little bit more done, but not much more. If you
209 plan to extend that code, take a look at dispnew.c. The function
210 build_frame_matrix is a good starting point.
212 Bidirectional display.
214 Bidirectional display adds quite some hair to this already complex
215 design. The good news are that a large portion of that hairy stuff
216 is hidden in bidi.c behind only 3 interfaces. bidi.c implements a
217 reordering engine which is called by set_iterator_to_next and
218 returns the next character to display in the visual order. See
219 commentary on bidi.c for more details. As far as redisplay is
220 concerned, the effect of calling bidi_move_to_visually_next, the
221 main interface of the reordering engine, is that the iterator gets
222 magically placed on the buffer or string position that is to be
223 displayed next. In other words, a linear iteration through the
224 buffer/string is replaced with a non-linear one. All the rest of
225 the redisplay is oblivious to the bidi reordering.
227 Well, almost oblivious---there are still complications, most of
228 them due to the fact that buffer and string positions no longer
229 change monotonously with glyph indices in a glyph row. Moreover,
230 for continued lines, the buffer positions may not even be
231 monotonously changing with vertical positions. Also, accounting
232 for face changes, overlays, etc. becomes more complex because
233 non-linear iteration could potentially skip many positions with
234 changes, and then cross them again on the way back...
236 One other prominent effect of bidirectional display is that some
237 paragraphs of text need to be displayed starting at the right
238 margin of the window---the so-called right-to-left, or R2L
239 paragraphs. R2L paragraphs are displayed with R2L glyph rows,
240 which have their reversed_p flag set. The bidi reordering engine
241 produces characters in such rows starting from the character which
242 should be the rightmost on display. PRODUCE_GLYPHS then reverses
243 the order, when it fills up the glyph row whose reversed_p flag is
244 set, by prepending each new glyph to what is already there, instead
245 of appending it. When the glyph row is complete, the function
246 extend_face_to_end_of_line fills the empty space to the left of the
247 leftmost character with special glyphs, which will display as,
248 well, empty. On text terminals, these special glyphs are simply
249 blank characters. On graphics terminals, there's a single stretch
250 glyph of a suitably computed width. Both the blanks and the
251 stretch glyph are given the face of the background of the line.
252 This way, the terminal-specific back-end can still draw the glyphs
253 left to right, even for R2L lines.
255 Bidirectional display and character compositions
257 Some scripts cannot be displayed by drawing each character
258 individually, because adjacent characters change each other's shape
259 on display. For example, Arabic and Indic scripts belong to this
260 category.
262 Emacs display supports this by providing "character compositions",
263 most of which is implemented in composite.c. During the buffer
264 scan that delivers characters to PRODUCE_GLYPHS, if the next
265 character to be delivered is a composed character, the iteration
266 calls composition_reseat_it and next_element_from_composition. If
267 they succeed to compose the character with one or more of the
268 following characters, the whole sequence of characters that where
269 composed is recorded in the `struct composition_it' object that is
270 part of the buffer iterator. The composed sequence could produce
271 one or more font glyphs (called "grapheme clusters") on the screen.
272 Each of these grapheme clusters is then delivered to PRODUCE_GLYPHS
273 in the direction corresponding to the current bidi scan direction
274 (recorded in the scan_dir member of the `struct bidi_it' object
275 that is part of the buffer iterator). In particular, if the bidi
276 iterator currently scans the buffer backwards, the grapheme
277 clusters are delivered back to front. This reorders the grapheme
278 clusters as appropriate for the current bidi context. Note that
279 this means that the grapheme clusters are always stored in the
280 LGSTRING object (see composite.c) in the logical order.
282 Moving an iterator in bidirectional text
283 without producing glyphs
285 Note one important detail mentioned above: that the bidi reordering
286 engine, driven by the iterator, produces characters in R2L rows
287 starting at the character that will be the rightmost on display.
288 As far as the iterator is concerned, the geometry of such rows is
289 still left to right, i.e. the iterator "thinks" the first character
290 is at the leftmost pixel position. The iterator does not know that
291 PRODUCE_GLYPHS reverses the order of the glyphs that the iterator
292 delivers. This is important when functions from the move_it_*
293 family are used to get to certain screen position or to match
294 screen coordinates with buffer coordinates: these functions use the
295 iterator geometry, which is left to right even in R2L paragraphs.
296 This works well with most callers of move_it_*, because they need
297 to get to a specific column, and columns are still numbered in the
298 reading order, i.e. the rightmost character in a R2L paragraph is
299 still column zero. But some callers do not get well with this; a
300 notable example is mouse clicks that need to find the character
301 that corresponds to certain pixel coordinates. See
302 buffer_posn_from_coords in dispnew.c for how this is handled. */
304 #include <config.h>
305 #include <stdio.h>
306 #include <stdlib.h>
307 #include <limits.h>
308 #include <math.h>
310 #include "lisp.h"
311 #include "atimer.h"
312 #include "composite.h"
313 #include "keyboard.h"
314 #include "systime.h"
315 #include "frame.h"
316 #include "window.h"
317 #include "termchar.h"
318 #include "dispextern.h"
319 #include "character.h"
320 #include "buffer.h"
321 #include "charset.h"
322 #include "indent.h"
323 #include "commands.h"
324 #include "keymap.h"
325 #include "disptab.h"
326 #include "termhooks.h"
327 #include "termopts.h"
328 #include "intervals.h"
329 #include "coding.h"
330 #include "region-cache.h"
331 #include "font.h"
332 #include "fontset.h"
333 #include "blockinput.h"
334 #include "xwidget.h"
335 #ifdef HAVE_WINDOW_SYSTEM
336 #include TERM_HEADER
337 #endif /* HAVE_WINDOW_SYSTEM */
339 #ifndef FRAME_X_OUTPUT
340 #define FRAME_X_OUTPUT(f) ((f)->output_data.x)
341 #endif
343 #define DISP_INFINITY 10000000
345 /* Holds the list (error). */
346 static Lisp_Object list_of_error;
348 #ifdef HAVE_WINDOW_SYSTEM
350 /* Test if overflow newline into fringe. Called with iterator IT
351 at or past right window margin, and with IT->current_x set. */
353 #define IT_OVERFLOW_NEWLINE_INTO_FRINGE(IT) \
354 (!NILP (Voverflow_newline_into_fringe) \
355 && FRAME_WINDOW_P ((IT)->f) \
356 && ((IT)->bidi_it.paragraph_dir == R2L \
357 ? (WINDOW_LEFT_FRINGE_WIDTH ((IT)->w) > 0) \
358 : (WINDOW_RIGHT_FRINGE_WIDTH ((IT)->w) > 0)) \
359 && (IT)->current_x == (IT)->last_visible_x)
361 #else /* !HAVE_WINDOW_SYSTEM */
362 #define IT_OVERFLOW_NEWLINE_INTO_FRINGE(it) false
363 #endif /* HAVE_WINDOW_SYSTEM */
365 /* Test if the display element loaded in IT, or the underlying buffer
366 or string character, is a space or a TAB character. This is used
367 to determine where word wrapping can occur. */
369 #define IT_DISPLAYING_WHITESPACE(it) \
370 ((it->what == IT_CHARACTER && (it->c == ' ' || it->c == '\t')) \
371 || ((STRINGP (it->string) \
372 && (SREF (it->string, IT_STRING_BYTEPOS (*it)) == ' ' \
373 || SREF (it->string, IT_STRING_BYTEPOS (*it)) == '\t')) \
374 || (it->s \
375 && (it->s[IT_BYTEPOS (*it)] == ' ' \
376 || it->s[IT_BYTEPOS (*it)] == '\t')) \
377 || (IT_BYTEPOS (*it) < ZV_BYTE \
378 && (*BYTE_POS_ADDR (IT_BYTEPOS (*it)) == ' ' \
379 || *BYTE_POS_ADDR (IT_BYTEPOS (*it)) == '\t')))) \
381 /* True means print newline to stdout before next mini-buffer message. */
383 bool noninteractive_need_newline;
385 /* True means print newline to message log before next message. */
387 static bool message_log_need_newline;
389 /* Three markers that message_dolog uses.
390 It could allocate them itself, but that causes trouble
391 in handling memory-full errors. */
392 static Lisp_Object message_dolog_marker1;
393 static Lisp_Object message_dolog_marker2;
394 static Lisp_Object message_dolog_marker3;
396 /* The buffer position of the first character appearing entirely or
397 partially on the line of the selected window which contains the
398 cursor; <= 0 if not known. Set by set_cursor_from_row, used for
399 redisplay optimization in redisplay_internal. */
401 static struct text_pos this_line_start_pos;
403 /* Number of characters past the end of the line above, including the
404 terminating newline. */
406 static struct text_pos this_line_end_pos;
408 /* The vertical positions and the height of this line. */
410 static int this_line_vpos;
411 static int this_line_y;
412 static int this_line_pixel_height;
414 /* X position at which this display line starts. Usually zero;
415 negative if first character is partially visible. */
417 static int this_line_start_x;
419 /* The smallest character position seen by move_it_* functions as they
420 move across display lines. Used to set MATRIX_ROW_START_CHARPOS of
421 hscrolled lines, see display_line. */
423 static struct text_pos this_line_min_pos;
425 /* Buffer that this_line_.* variables are referring to. */
427 static struct buffer *this_line_buffer;
429 /* True if an overlay arrow has been displayed in this window. */
431 static bool overlay_arrow_seen;
433 /* Vector containing glyphs for an ellipsis `...'. */
435 static Lisp_Object default_invis_vector[3];
437 /* This is the window where the echo area message was displayed. It
438 is always a mini-buffer window, but it may not be the same window
439 currently active as a mini-buffer. */
441 Lisp_Object echo_area_window;
443 /* List of pairs (MESSAGE . MULTIBYTE). The function save_message
444 pushes the current message and the value of
445 message_enable_multibyte on the stack, the function restore_message
446 pops the stack and displays MESSAGE again. */
448 static Lisp_Object Vmessage_stack;
450 /* True means multibyte characters were enabled when the echo area
451 message was specified. */
453 static bool message_enable_multibyte;
455 /* At each redisplay cycle, we should refresh everything there is to refresh.
456 To do that efficiently, we use many optimizations that try to make sure we
457 don't waste too much time updating things that haven't changed.
458 The coarsest such optimization is that, in the most common cases, we only
459 look at the selected-window.
461 To know whether other windows should be considered for redisplay, we use the
462 variable windows_or_buffers_changed: as long as it is 0, it means that we
463 have not noticed anything that should require updating anything else than
464 the selected-window. If it is set to REDISPLAY_SOME, it means that since
465 last redisplay, some changes have been made which could impact other
466 windows. To know which ones need redisplay, every buffer, window, and frame
467 has a `redisplay' bit, which (if true) means that this object needs to be
468 redisplayed. If windows_or_buffers_changed is 0, we know there's no point
469 looking for those `redisplay' bits (actually, there might be some such bits
470 set, but then only on objects which aren't displayed anyway).
472 OTOH if it's non-zero we wil have to loop through all windows and then check
473 the `redisplay' bit of the corresponding window, frame, and buffer, in order
474 to decide whether that window needs attention or not. Note that we can't
475 just look at the frame's redisplay bit to decide that the whole frame can be
476 skipped, since even if the frame's redisplay bit is unset, some of its
477 windows's redisplay bits may be set.
479 Mostly for historical reasons, windows_or_buffers_changed can also take
480 other non-zero values. In that case, the precise value doesn't matter (it
481 encodes the cause of the setting but is only used for debugging purposes),
482 and what it means is that we shouldn't pay attention to any `redisplay' bits
483 and we should simply try and redisplay every window out there. */
485 int windows_or_buffers_changed;
487 /* Nonzero if we should redraw the mode lines on the next redisplay.
488 Similarly to `windows_or_buffers_changed', If it has value REDISPLAY_SOME,
489 then only redisplay the mode lines in those buffers/windows/frames where the
490 `redisplay' bit has been set.
491 For any other value, redisplay all mode lines (the number used is then only
492 used to track down the cause for this full-redisplay).
494 Since the frame title uses the same %-constructs as the mode line
495 (except %c, %C, and %l), if this variable is non-zero, we also consider
496 redisplaying the title of each frame, see x_consider_frame_title.
498 The `redisplay' bits are the same as those used for
499 windows_or_buffers_changed, and setting windows_or_buffers_changed also
500 causes recomputation of the mode lines of all those windows. IOW this
501 variable only has an effect if windows_or_buffers_changed is zero, in which
502 case we should only need to redisplay the mode-line of those objects with
503 a `redisplay' bit set but not the window's text content (tho we may still
504 need to refresh the text content of the selected-window). */
506 int update_mode_lines;
508 /* True after display_mode_line if %l was used and it displayed a
509 line number. */
511 static bool line_number_displayed;
513 /* The name of the *Messages* buffer, a string. */
515 static Lisp_Object Vmessages_buffer_name;
517 /* Current, index 0, and last displayed echo area message. Either
518 buffers from echo_buffers, or nil to indicate no message. */
520 Lisp_Object echo_area_buffer[2];
522 /* The buffers referenced from echo_area_buffer. */
524 static Lisp_Object echo_buffer[2];
526 /* A vector saved used in with_area_buffer to reduce consing. */
528 static Lisp_Object Vwith_echo_area_save_vector;
530 /* True means display_echo_area should display the last echo area
531 message again. Set by redisplay_preserve_echo_area. */
533 static bool display_last_displayed_message_p;
535 /* True if echo area is being used by print; false if being used by
536 message. */
538 static bool message_buf_print;
540 /* Set to true in clear_message to make redisplay_internal aware
541 of an emptied echo area. */
543 static bool message_cleared_p;
545 /* A scratch glyph row with contents used for generating truncation
546 glyphs. Also used in direct_output_for_insert. */
548 #define MAX_SCRATCH_GLYPHS 100
549 static struct glyph_row scratch_glyph_row;
550 static struct glyph scratch_glyphs[MAX_SCRATCH_GLYPHS];
552 /* Ascent and height of the last line processed by move_it_to. */
554 static int last_height;
556 /* True if there's a help-echo in the echo area. */
558 bool help_echo_showing_p;
560 /* The maximum distance to look ahead for text properties. Values
561 that are too small let us call compute_char_face and similar
562 functions too often which is expensive. Values that are too large
563 let us call compute_char_face and alike too often because we
564 might not be interested in text properties that far away. */
566 #define TEXT_PROP_DISTANCE_LIMIT 100
568 /* SAVE_IT and RESTORE_IT are called when we save a snapshot of the
569 iterator state and later restore it. This is needed because the
570 bidi iterator on bidi.c keeps a stacked cache of its states, which
571 is really a singleton. When we use scratch iterator objects to
572 move around the buffer, we can cause the bidi cache to be pushed or
573 popped, and therefore we need to restore the cache state when we
574 return to the original iterator. */
575 #define SAVE_IT(ITCOPY, ITORIG, CACHE) \
576 do { \
577 if (CACHE) \
578 bidi_unshelve_cache (CACHE, true); \
579 ITCOPY = ITORIG; \
580 CACHE = bidi_shelve_cache (); \
581 } while (false)
583 #define RESTORE_IT(pITORIG, pITCOPY, CACHE) \
584 do { \
585 if (pITORIG != pITCOPY) \
586 *(pITORIG) = *(pITCOPY); \
587 bidi_unshelve_cache (CACHE, false); \
588 CACHE = NULL; \
589 } while (false)
591 /* Functions to mark elements as needing redisplay. */
592 enum { REDISPLAY_SOME = 2}; /* Arbitrary choice. */
594 void
595 redisplay_other_windows (void)
597 if (!windows_or_buffers_changed)
598 windows_or_buffers_changed = REDISPLAY_SOME;
601 void
602 wset_redisplay (struct window *w)
604 /* Beware: selected_window can be nil during early stages. */
605 if (!EQ (make_lisp_ptr (w, Lisp_Vectorlike), selected_window))
606 redisplay_other_windows ();
607 w->redisplay = true;
610 void
611 fset_redisplay (struct frame *f)
613 redisplay_other_windows ();
614 f->redisplay = true;
617 void
618 bset_redisplay (struct buffer *b)
620 int count = buffer_window_count (b);
621 if (count > 0)
623 /* ... it's visible in other window than selected, */
624 if (count > 1 || b != XBUFFER (XWINDOW (selected_window)->contents))
625 redisplay_other_windows ();
626 /* Even if we don't set windows_or_buffers_changed, do set `redisplay'
627 so that if we later set windows_or_buffers_changed, this buffer will
628 not be omitted. */
629 b->text->redisplay = true;
633 void
634 bset_update_mode_line (struct buffer *b)
636 if (!update_mode_lines)
637 update_mode_lines = REDISPLAY_SOME;
638 b->text->redisplay = true;
641 DEFUN ("set-buffer-redisplay", Fset_buffer_redisplay,
642 Sset_buffer_redisplay, 4, 4, 0,
643 doc: /* Mark the current buffer for redisplay.
644 This function may be passed to `add-variable-watcher'. */)
645 (Lisp_Object symbol, Lisp_Object newval, Lisp_Object op, Lisp_Object where)
647 bset_update_mode_line (current_buffer);
648 current_buffer->prevent_redisplay_optimizations_p = true;
649 return Qnil;
652 #ifdef GLYPH_DEBUG
654 /* True means print traces of redisplay if compiled with
655 GLYPH_DEBUG defined. */
657 bool trace_redisplay_p;
659 #endif /* GLYPH_DEBUG */
661 #ifdef DEBUG_TRACE_MOVE
662 /* True means trace with TRACE_MOVE to stderr. */
663 static bool trace_move;
665 #define TRACE_MOVE(x) if (trace_move) fprintf x; else (void) 0
666 #else
667 #define TRACE_MOVE(x) (void) 0
668 #endif
670 /* Buffer being redisplayed -- for redisplay_window_error. */
672 static struct buffer *displayed_buffer;
674 /* Value returned from text property handlers (see below). */
676 enum prop_handled
678 HANDLED_NORMALLY,
679 HANDLED_RECOMPUTE_PROPS,
680 HANDLED_OVERLAY_STRING_CONSUMED,
681 HANDLED_RETURN
684 /* A description of text properties that redisplay is interested
685 in. */
687 struct props
689 /* The symbol index of the name of the property. */
690 short name;
692 /* A unique index for the property. */
693 enum prop_idx idx;
695 /* A handler function called to set up iterator IT from the property
696 at IT's current position. Value is used to steer handle_stop. */
697 enum prop_handled (*handler) (struct it *it);
700 static enum prop_handled handle_face_prop (struct it *);
701 static enum prop_handled handle_invisible_prop (struct it *);
702 static enum prop_handled handle_display_prop (struct it *);
703 static enum prop_handled handle_composition_prop (struct it *);
704 static enum prop_handled handle_overlay_change (struct it *);
705 static enum prop_handled handle_fontified_prop (struct it *);
707 /* Properties handled by iterators. */
709 static struct props it_props[] =
711 {SYMBOL_INDEX (Qfontified), FONTIFIED_PROP_IDX, handle_fontified_prop},
712 /* Handle `face' before `display' because some sub-properties of
713 `display' need to know the face. */
714 {SYMBOL_INDEX (Qface), FACE_PROP_IDX, handle_face_prop},
715 {SYMBOL_INDEX (Qdisplay), DISPLAY_PROP_IDX, handle_display_prop},
716 {SYMBOL_INDEX (Qinvisible), INVISIBLE_PROP_IDX, handle_invisible_prop},
717 {SYMBOL_INDEX (Qcomposition), COMPOSITION_PROP_IDX, handle_composition_prop},
718 {0, 0, NULL}
721 /* Value is the position described by X. If X is a marker, value is
722 the marker_position of X. Otherwise, value is X. */
724 #define COERCE_MARKER(X) (MARKERP ((X)) ? Fmarker_position (X) : (X))
726 /* Enumeration returned by some move_it_.* functions internally. */
728 enum move_it_result
730 /* Not used. Undefined value. */
731 MOVE_UNDEFINED,
733 /* Move ended at the requested buffer position or ZV. */
734 MOVE_POS_MATCH_OR_ZV,
736 /* Move ended at the requested X pixel position. */
737 MOVE_X_REACHED,
739 /* Move within a line ended at the end of a line that must be
740 continued. */
741 MOVE_LINE_CONTINUED,
743 /* Move within a line ended at the end of a line that would
744 be displayed truncated. */
745 MOVE_LINE_TRUNCATED,
747 /* Move within a line ended at a line end. */
748 MOVE_NEWLINE_OR_CR
751 /* This counter is used to clear the face cache every once in a while
752 in redisplay_internal. It is incremented for each redisplay.
753 Every CLEAR_FACE_CACHE_COUNT full redisplays, the face cache is
754 cleared. */
756 #define CLEAR_FACE_CACHE_COUNT 500
757 static int clear_face_cache_count;
759 /* Similarly for the image cache. */
761 #ifdef HAVE_WINDOW_SYSTEM
762 #define CLEAR_IMAGE_CACHE_COUNT 101
763 static int clear_image_cache_count;
765 /* Null glyph slice */
766 static struct glyph_slice null_glyph_slice = { 0, 0, 0, 0 };
767 #endif
769 /* True while redisplay_internal is in progress. */
771 bool redisplaying_p;
773 /* If a string, XTread_socket generates an event to display that string.
774 (The display is done in read_char.) */
776 Lisp_Object help_echo_string;
777 Lisp_Object help_echo_window;
778 Lisp_Object help_echo_object;
779 ptrdiff_t help_echo_pos;
781 /* Temporary variable for XTread_socket. */
783 Lisp_Object previous_help_echo_string;
785 /* Platform-independent portion of hourglass implementation. */
787 #ifdef HAVE_WINDOW_SYSTEM
789 /* True means an hourglass cursor is currently shown. */
790 static bool hourglass_shown_p;
792 /* If non-null, an asynchronous timer that, when it expires, displays
793 an hourglass cursor on all frames. */
794 static struct atimer *hourglass_atimer;
796 #endif /* HAVE_WINDOW_SYSTEM */
798 /* Default number of seconds to wait before displaying an hourglass
799 cursor. */
800 #define DEFAULT_HOURGLASS_DELAY 1
802 #ifdef HAVE_WINDOW_SYSTEM
804 /* Default pixel width of `thin-space' display method. */
805 #define THIN_SPACE_WIDTH 1
807 #endif /* HAVE_WINDOW_SYSTEM */
809 /* Function prototypes. */
811 static void setup_for_ellipsis (struct it *, int);
812 static void set_iterator_to_next (struct it *, bool);
813 static void mark_window_display_accurate_1 (struct window *, bool);
814 static bool row_for_charpos_p (struct glyph_row *, ptrdiff_t);
815 static bool cursor_row_p (struct glyph_row *);
816 static int redisplay_mode_lines (Lisp_Object, bool);
818 static void handle_line_prefix (struct it *);
820 static void handle_stop_backwards (struct it *, ptrdiff_t);
821 static void unwind_with_echo_area_buffer (Lisp_Object);
822 static Lisp_Object with_echo_area_buffer_unwind_data (struct window *);
823 static bool current_message_1 (ptrdiff_t, Lisp_Object);
824 static bool truncate_message_1 (ptrdiff_t, Lisp_Object);
825 static void set_message (Lisp_Object);
826 static bool set_message_1 (ptrdiff_t, Lisp_Object);
827 static bool display_echo_area_1 (ptrdiff_t, Lisp_Object);
828 static bool resize_mini_window_1 (ptrdiff_t, Lisp_Object);
829 static void unwind_redisplay (void);
830 static void extend_face_to_end_of_line (struct it *);
831 static intmax_t message_log_check_duplicate (ptrdiff_t, ptrdiff_t);
832 static void push_it (struct it *, struct text_pos *);
833 static void iterate_out_of_display_property (struct it *);
834 static void pop_it (struct it *);
835 static void redisplay_internal (void);
836 static void echo_area_display (bool);
837 static void block_buffer_flips (void);
838 static void unblock_buffer_flips (void);
839 static void redisplay_windows (Lisp_Object);
840 static void redisplay_window (Lisp_Object, bool);
841 static Lisp_Object redisplay_window_error (Lisp_Object);
842 static Lisp_Object redisplay_window_0 (Lisp_Object);
843 static Lisp_Object redisplay_window_1 (Lisp_Object);
844 static bool set_cursor_from_row (struct window *, struct glyph_row *,
845 struct glyph_matrix *, ptrdiff_t, ptrdiff_t,
846 int, int);
847 static bool cursor_row_fully_visible_p (struct window *, bool, bool);
848 static bool update_menu_bar (struct frame *, bool, bool);
849 static bool try_window_reusing_current_matrix (struct window *);
850 static int try_window_id (struct window *);
851 static void maybe_produce_line_number (struct it *);
852 static bool should_produce_line_number (struct it *);
853 static bool display_line (struct it *, int);
854 static int display_mode_lines (struct window *);
855 static int display_mode_line (struct window *, enum face_id, Lisp_Object);
856 static int display_mode_element (struct it *, int, int, int, Lisp_Object,
857 Lisp_Object, bool);
858 static int store_mode_line_string (const char *, Lisp_Object, bool, int, int,
859 Lisp_Object);
860 static const char *decode_mode_spec (struct window *, int, int, Lisp_Object *);
861 static void display_menu_bar (struct window *);
862 static ptrdiff_t display_count_lines (ptrdiff_t, ptrdiff_t, ptrdiff_t,
863 ptrdiff_t *);
864 static void pint2str (register char *, register int, register ptrdiff_t);
866 static int display_string (const char *, Lisp_Object, Lisp_Object,
867 ptrdiff_t, ptrdiff_t, struct it *, int, int, int, int);
868 static void compute_line_metrics (struct it *);
869 static void run_redisplay_end_trigger_hook (struct it *);
870 static bool get_overlay_strings (struct it *, ptrdiff_t);
871 static bool get_overlay_strings_1 (struct it *, ptrdiff_t, bool);
872 static void next_overlay_string (struct it *);
873 static void reseat (struct it *, struct text_pos, bool);
874 static void reseat_1 (struct it *, struct text_pos, bool);
875 static bool next_element_from_display_vector (struct it *);
876 static bool next_element_from_string (struct it *);
877 static bool next_element_from_c_string (struct it *);
878 static bool next_element_from_buffer (struct it *);
879 static bool next_element_from_composition (struct it *);
880 static bool next_element_from_image (struct it *);
881 static bool next_element_from_stretch (struct it *);
882 static bool next_element_from_xwidget (struct it *);
883 static void load_overlay_strings (struct it *, ptrdiff_t);
884 static bool get_next_display_element (struct it *);
885 static enum move_it_result
886 move_it_in_display_line_to (struct it *, ptrdiff_t, int,
887 enum move_operation_enum);
888 static void get_visually_first_element (struct it *);
889 static void compute_stop_pos (struct it *);
890 static int face_before_or_after_it_pos (struct it *, bool);
891 static ptrdiff_t next_overlay_change (ptrdiff_t);
892 static int handle_display_spec (struct it *, Lisp_Object, Lisp_Object,
893 Lisp_Object, struct text_pos *, ptrdiff_t, bool);
894 static int handle_single_display_spec (struct it *, Lisp_Object, Lisp_Object,
895 Lisp_Object, struct text_pos *,
896 ptrdiff_t, int, bool, bool);
897 static int underlying_face_id (struct it *);
899 #define face_before_it_pos(IT) face_before_or_after_it_pos (IT, true)
900 #define face_after_it_pos(IT) face_before_or_after_it_pos (IT, false)
902 #ifdef HAVE_WINDOW_SYSTEM
904 static void update_tool_bar (struct frame *, bool);
905 static void x_draw_bottom_divider (struct window *w);
906 static void notice_overwritten_cursor (struct window *,
907 enum glyph_row_area,
908 int, int, int, int);
909 static int normal_char_height (struct font *, int);
910 static void normal_char_ascent_descent (struct font *, int, int *, int *);
912 static void append_stretch_glyph (struct it *, Lisp_Object,
913 int, int, int);
915 static Lisp_Object get_it_property (struct it *, Lisp_Object);
916 static Lisp_Object calc_line_height_property (struct it *, Lisp_Object,
917 struct font *, int, bool);
919 #endif /* HAVE_WINDOW_SYSTEM */
921 static void produce_special_glyphs (struct it *, enum display_element_type);
922 static void show_mouse_face (Mouse_HLInfo *, enum draw_glyphs_face);
923 static bool coords_in_mouse_face_p (struct window *, int, int);
927 /***********************************************************************
928 Window display dimensions
929 ***********************************************************************/
931 /* Return the bottom boundary y-position for text lines in window W.
932 This is the first y position at which a line cannot start.
933 It is relative to the top of the window.
935 This is the height of W minus the height of a mode line, if any. */
938 window_text_bottom_y (struct window *w)
940 int height = WINDOW_PIXEL_HEIGHT (w);
942 height -= WINDOW_BOTTOM_DIVIDER_WIDTH (w);
944 if (window_wants_mode_line (w))
945 height -= CURRENT_MODE_LINE_HEIGHT (w);
947 height -= WINDOW_SCROLL_BAR_AREA_HEIGHT (w);
949 return height;
952 /* Return the pixel width of display area AREA of window W.
953 ANY_AREA means return the total width of W, not including
954 fringes to the left and right of the window. */
957 window_box_width (struct window *w, enum glyph_row_area area)
959 int width = w->pixel_width;
961 if (!w->pseudo_window_p)
963 width -= WINDOW_SCROLL_BAR_AREA_WIDTH (w);
964 width -= WINDOW_RIGHT_DIVIDER_WIDTH (w);
966 if (area == TEXT_AREA)
967 width -= (WINDOW_MARGINS_WIDTH (w)
968 + WINDOW_FRINGES_WIDTH (w));
969 else if (area == LEFT_MARGIN_AREA)
970 width = WINDOW_LEFT_MARGIN_WIDTH (w);
971 else if (area == RIGHT_MARGIN_AREA)
972 width = WINDOW_RIGHT_MARGIN_WIDTH (w);
975 /* With wide margins, fringes, etc. we might end up with a negative
976 width, correct that here. */
977 return max (0, width);
981 /* Return the pixel height of the display area of window W, not
982 including mode lines of W, if any. */
985 window_box_height (struct window *w)
987 struct frame *f = XFRAME (w->frame);
988 int height = WINDOW_PIXEL_HEIGHT (w);
990 eassert (height >= 0);
992 height -= WINDOW_BOTTOM_DIVIDER_WIDTH (w);
993 height -= WINDOW_SCROLL_BAR_AREA_HEIGHT (w);
995 /* Note: the code below that determines the mode-line/header-line
996 height is essentially the same as that contained in the macro
997 CURRENT_{MODE,HEADER}_LINE_HEIGHT, except that it checks whether
998 the appropriate glyph row has its `mode_line_p' flag set,
999 and if it doesn't, uses estimate_mode_line_height instead. */
1001 if (window_wants_mode_line (w))
1003 struct glyph_row *ml_row
1004 = (w->current_matrix && w->current_matrix->rows
1005 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
1006 : 0);
1007 if (ml_row && ml_row->mode_line_p)
1008 height -= ml_row->height;
1009 else
1010 height -= estimate_mode_line_height (f, CURRENT_MODE_LINE_FACE_ID (w));
1013 if (window_wants_header_line (w))
1015 struct glyph_row *hl_row
1016 = (w->current_matrix && w->current_matrix->rows
1017 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
1018 : 0);
1019 if (hl_row && hl_row->mode_line_p)
1020 height -= hl_row->height;
1021 else
1022 height -= estimate_mode_line_height (f, HEADER_LINE_FACE_ID);
1025 /* With a very small font and a mode-line that's taller than
1026 default, we might end up with a negative height. */
1027 return max (0, height);
1030 /* Return the window-relative coordinate of the left edge of display
1031 area AREA of window W. ANY_AREA means return the left edge of the
1032 whole window, to the right of the left fringe of W. */
1035 window_box_left_offset (struct window *w, enum glyph_row_area area)
1037 int x;
1039 if (w->pseudo_window_p)
1040 return 0;
1042 x = WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
1044 if (area == TEXT_AREA)
1045 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1046 + window_box_width (w, LEFT_MARGIN_AREA));
1047 else if (area == RIGHT_MARGIN_AREA)
1048 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1049 + window_box_width (w, LEFT_MARGIN_AREA)
1050 + window_box_width (w, TEXT_AREA)
1051 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
1053 : WINDOW_RIGHT_FRINGE_WIDTH (w)));
1054 else if (area == LEFT_MARGIN_AREA
1055 && WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w))
1056 x += WINDOW_LEFT_FRINGE_WIDTH (w);
1058 /* Don't return more than the window's pixel width. */
1059 return min (x, w->pixel_width);
1063 /* Return the window-relative coordinate of the right edge of display
1064 area AREA of window W. ANY_AREA means return the right edge of the
1065 whole window, to the left of the right fringe of W. */
1067 static int
1068 window_box_right_offset (struct window *w, enum glyph_row_area area)
1070 /* Don't return more than the window's pixel width. */
1071 return min (window_box_left_offset (w, area) + window_box_width (w, area),
1072 w->pixel_width);
1075 /* Return the frame-relative coordinate of the left edge of display
1076 area AREA of window W. ANY_AREA means return the left edge of the
1077 whole window, to the right of the left fringe of W. */
1080 window_box_left (struct window *w, enum glyph_row_area area)
1082 struct frame *f = XFRAME (w->frame);
1083 int x;
1085 if (w->pseudo_window_p)
1086 return FRAME_INTERNAL_BORDER_WIDTH (f);
1088 x = (WINDOW_LEFT_EDGE_X (w)
1089 + window_box_left_offset (w, area));
1091 return x;
1095 /* Return the frame-relative coordinate of the right edge of display
1096 area AREA of window W. ANY_AREA means return the right edge of the
1097 whole window, to the left of the right fringe of W. */
1100 window_box_right (struct window *w, enum glyph_row_area area)
1102 return window_box_left (w, area) + window_box_width (w, area);
1105 /* Get the bounding box of the display area AREA of window W, without
1106 mode lines, in frame-relative coordinates. ANY_AREA means the
1107 whole window, not including the left and right fringes of
1108 the window. Return in *BOX_X and *BOX_Y the frame-relative pixel
1109 coordinates of the upper-left corner of the box. Return in
1110 *BOX_WIDTH, and *BOX_HEIGHT the pixel width and height of the box. */
1112 void
1113 window_box (struct window *w, enum glyph_row_area area, int *box_x,
1114 int *box_y, int *box_width, int *box_height)
1116 if (box_width)
1117 *box_width = window_box_width (w, area);
1118 if (box_height)
1119 *box_height = window_box_height (w);
1120 if (box_x)
1121 *box_x = window_box_left (w, area);
1122 if (box_y)
1124 *box_y = WINDOW_TOP_EDGE_Y (w);
1125 if (window_wants_header_line (w))
1126 *box_y += CURRENT_HEADER_LINE_HEIGHT (w);
1130 #ifdef HAVE_WINDOW_SYSTEM
1132 /* Get the bounding box of the display area AREA of window W, without
1133 mode lines and both fringes of the window. Return in *TOP_LEFT_X
1134 and TOP_LEFT_Y the frame-relative pixel coordinates of the
1135 upper-left corner of the box. Return in *BOTTOM_RIGHT_X, and
1136 *BOTTOM_RIGHT_Y the coordinates of the bottom-right corner of the
1137 box. */
1139 static void
1140 window_box_edges (struct window *w, int *top_left_x, int *top_left_y,
1141 int *bottom_right_x, int *bottom_right_y)
1143 window_box (w, ANY_AREA, top_left_x, top_left_y,
1144 bottom_right_x, bottom_right_y);
1145 *bottom_right_x += *top_left_x;
1146 *bottom_right_y += *top_left_y;
1149 #endif /* HAVE_WINDOW_SYSTEM */
1151 /***********************************************************************
1152 Utilities
1153 ***********************************************************************/
1155 /* Return the bottom y-position of the line the iterator IT is in.
1156 This can modify IT's settings. */
1159 line_bottom_y (struct it *it)
1161 int line_height = it->max_ascent + it->max_descent;
1162 int line_top_y = it->current_y;
1164 if (line_height == 0)
1166 if (last_height)
1167 line_height = last_height;
1168 else if (IT_CHARPOS (*it) < ZV)
1170 move_it_by_lines (it, 1);
1171 line_height = (it->max_ascent || it->max_descent
1172 ? it->max_ascent + it->max_descent
1173 : last_height);
1175 else
1177 struct glyph_row *row = it->glyph_row;
1179 /* Use the default character height. */
1180 it->glyph_row = NULL;
1181 it->what = IT_CHARACTER;
1182 it->c = ' ';
1183 it->len = 1;
1184 PRODUCE_GLYPHS (it);
1185 line_height = it->ascent + it->descent;
1186 it->glyph_row = row;
1190 return line_top_y + line_height;
1193 DEFUN ("line-pixel-height", Fline_pixel_height,
1194 Sline_pixel_height, 0, 0, 0,
1195 doc: /* Return height in pixels of text line in the selected window.
1197 Value is the height in pixels of the line at point. */)
1198 (void)
1200 struct it it;
1201 struct text_pos pt;
1202 struct window *w = XWINDOW (selected_window);
1203 struct buffer *old_buffer = NULL;
1204 Lisp_Object result;
1206 if (XBUFFER (w->contents) != current_buffer)
1208 old_buffer = current_buffer;
1209 set_buffer_internal_1 (XBUFFER (w->contents));
1211 SET_TEXT_POS (pt, PT, PT_BYTE);
1212 start_display (&it, w, pt);
1213 /* Start from the beginning of the screen line, to make sure we
1214 traverse all of its display elements, and thus capture the
1215 correct metrics. */
1216 move_it_by_lines (&it, 0);
1217 it.vpos = it.current_y = 0;
1218 last_height = 0;
1219 result = make_number (line_bottom_y (&it));
1220 if (old_buffer)
1221 set_buffer_internal_1 (old_buffer);
1223 return result;
1226 /* Return the default pixel height of text lines in window W. The
1227 value is the canonical height of the W frame's default font, plus
1228 any extra space required by the line-spacing variable or frame
1229 parameter.
1231 Implementation note: this ignores any line-spacing text properties
1232 put on the newline characters. This is because those properties
1233 only affect the _screen_ line ending in the newline (i.e., in a
1234 continued line, only the last screen line will be affected), which
1235 means only a small number of lines in a buffer can ever use this
1236 feature. Since this function is used to compute the default pixel
1237 equivalent of text lines in a window, we can safely ignore those
1238 few lines. For the same reasons, we ignore the line-height
1239 properties. */
1241 default_line_pixel_height (struct window *w)
1243 struct frame *f = WINDOW_XFRAME (w);
1244 int height = FRAME_LINE_HEIGHT (f);
1246 if (!FRAME_INITIAL_P (f) && BUFFERP (w->contents))
1248 struct buffer *b = XBUFFER (w->contents);
1249 Lisp_Object val = BVAR (b, extra_line_spacing);
1251 if (NILP (val))
1252 val = BVAR (&buffer_defaults, extra_line_spacing);
1253 if (!NILP (val))
1255 if (RANGED_INTEGERP (0, val, INT_MAX))
1256 height += XFASTINT (val);
1257 else if (FLOATP (val))
1259 int addon = XFLOAT_DATA (val) * height + 0.5;
1261 if (addon >= 0)
1262 height += addon;
1265 else
1266 height += f->extra_line_spacing;
1269 return height;
1272 /* Subroutine of pos_visible_p below. Extracts a display string, if
1273 any, from the display spec given as its argument. */
1274 static Lisp_Object
1275 string_from_display_spec (Lisp_Object spec)
1277 if (VECTORP (spec))
1279 for (ptrdiff_t i = 0; i < ASIZE (spec); i++)
1280 if (STRINGP (AREF (spec, i)))
1281 return AREF (spec, i);
1283 else
1285 for (; CONSP (spec); spec = XCDR (spec))
1286 if (STRINGP (XCAR (spec)))
1287 return XCAR (spec);
1289 return spec;
1293 /* Limit insanely large values of W->hscroll on frame F to the largest
1294 value that will still prevent first_visible_x and last_visible_x of
1295 'struct it' from overflowing an int. */
1296 static int
1297 window_hscroll_limited (struct window *w, struct frame *f)
1299 ptrdiff_t window_hscroll = w->hscroll;
1300 int window_text_width = window_box_width (w, TEXT_AREA);
1301 int colwidth = FRAME_COLUMN_WIDTH (f);
1303 if (window_hscroll > (INT_MAX - window_text_width) / colwidth - 1)
1304 window_hscroll = (INT_MAX - window_text_width) / colwidth - 1;
1306 return window_hscroll;
1309 /* Return true if position CHARPOS is visible in window W.
1310 CHARPOS < 0 means return info about WINDOW_END position.
1311 If visible, set *X and *Y to pixel coordinates of top left corner.
1312 Set *RTOP and *RBOT to pixel height of an invisible area of glyph at POS.
1313 Set *ROWH and *VPOS to row's visible height and VPOS (row number). */
1315 bool
1316 pos_visible_p (struct window *w, ptrdiff_t charpos, int *x, int *y,
1317 int *rtop, int *rbot, int *rowh, int *vpos)
1319 struct it it;
1320 void *itdata = bidi_shelve_cache ();
1321 struct text_pos top;
1322 bool visible_p = false;
1323 struct buffer *old_buffer = NULL;
1324 bool r2l = false;
1326 if (FRAME_INITIAL_P (XFRAME (WINDOW_FRAME (w))))
1327 return visible_p;
1329 if (XBUFFER (w->contents) != current_buffer)
1331 old_buffer = current_buffer;
1332 set_buffer_internal_1 (XBUFFER (w->contents));
1335 SET_TEXT_POS_FROM_MARKER (top, w->start);
1336 /* Scrolling a minibuffer window via scroll bar when the echo area
1337 shows long text sometimes resets the minibuffer contents behind
1338 our backs. Also, someone might narrow-to-region and immediately
1339 call a scroll function. */
1340 if (CHARPOS (top) > ZV || CHARPOS (top) < BEGV)
1341 SET_TEXT_POS (top, BEGV, BEGV_BYTE);
1343 /* If the top of the window is after CHARPOS, the latter is surely
1344 not visible. */
1345 if (charpos >= 0 && CHARPOS (top) > charpos)
1346 return visible_p;
1348 /* Some Lisp hook could call us in the middle of redisplaying this
1349 very window. If, by some bad luck, we are retrying redisplay
1350 because we found that the mode-line height and/or header-line
1351 height needs to be updated, the assignment of mode_line_height
1352 and header_line_height below could disrupt that, due to the
1353 selected/nonselected window dance during mode-line display, and
1354 we could infloop. Avoid that. */
1355 int prev_mode_line_height = w->mode_line_height;
1356 int prev_header_line_height = w->header_line_height;
1357 /* Compute exact mode line heights. */
1358 if (window_wants_mode_line (w))
1360 Lisp_Object window_mode_line_format
1361 = window_parameter (w, Qmode_line_format);
1363 w->mode_line_height
1364 = display_mode_line (w, CURRENT_MODE_LINE_FACE_ID (w),
1365 NILP (window_mode_line_format)
1366 ? BVAR (current_buffer, mode_line_format)
1367 : window_mode_line_format);
1370 if (window_wants_header_line (w))
1372 Lisp_Object window_header_line_format
1373 = window_parameter (w, Qheader_line_format);
1375 w->header_line_height
1376 = display_mode_line (w, HEADER_LINE_FACE_ID,
1377 NILP (window_header_line_format)
1378 ? BVAR (current_buffer, header_line_format)
1379 : window_header_line_format);
1382 start_display (&it, w, top);
1383 move_it_to (&it, charpos, -1, it.last_visible_y - 1, -1,
1384 (charpos >= 0 ? MOVE_TO_POS : 0) | MOVE_TO_Y);
1386 if (charpos >= 0
1387 && (((!it.bidi_p || it.bidi_it.scan_dir != -1)
1388 && IT_CHARPOS (it) >= charpos)
1389 /* When scanning backwards under bidi iteration, move_it_to
1390 stops at or _before_ CHARPOS, because it stops at or to
1391 the _right_ of the character at CHARPOS. */
1392 || (it.bidi_p && it.bidi_it.scan_dir == -1
1393 && IT_CHARPOS (it) <= charpos)))
1395 /* We have reached CHARPOS, or passed it. How the call to
1396 move_it_to can overshoot: (i) If CHARPOS is on invisible text
1397 or covered by a display property, move_it_to stops at the end
1398 of the invisible text, to the right of CHARPOS. (ii) If
1399 CHARPOS is in a display vector, move_it_to stops on its last
1400 glyph. */
1401 int top_x = it.current_x;
1402 int top_y = it.current_y;
1403 int window_top_y = WINDOW_HEADER_LINE_HEIGHT (w);
1404 int bottom_y;
1405 struct it save_it;
1406 void *save_it_data = NULL;
1408 /* Calling line_bottom_y may change it.method, it.position, etc. */
1409 SAVE_IT (save_it, it, save_it_data);
1410 last_height = 0;
1411 bottom_y = line_bottom_y (&it);
1412 if (top_y < window_top_y)
1413 visible_p = bottom_y > window_top_y;
1414 else if (top_y < it.last_visible_y)
1415 visible_p = true;
1416 if (bottom_y >= it.last_visible_y
1417 && it.bidi_p && it.bidi_it.scan_dir == -1
1418 && IT_CHARPOS (it) < charpos)
1420 /* When the last line of the window is scanned backwards
1421 under bidi iteration, we could be duped into thinking
1422 that we have passed CHARPOS, when in fact move_it_to
1423 simply stopped short of CHARPOS because it reached
1424 last_visible_y. To see if that's what happened, we call
1425 move_it_to again with a slightly larger vertical limit,
1426 and see if it actually moved vertically; if it did, we
1427 didn't really reach CHARPOS, which is beyond window end. */
1428 /* Why 10? because we don't know how many canonical lines
1429 will the height of the next line(s) be. So we guess. */
1430 int ten_more_lines = 10 * default_line_pixel_height (w);
1432 move_it_to (&it, charpos, -1, bottom_y + ten_more_lines, -1,
1433 MOVE_TO_POS | MOVE_TO_Y);
1434 if (it.current_y > top_y)
1435 visible_p = false;
1438 RESTORE_IT (&it, &save_it, save_it_data);
1439 if (visible_p)
1441 if (it.method == GET_FROM_DISPLAY_VECTOR)
1443 /* We stopped on the last glyph of a display vector.
1444 Try and recompute. Hack alert! */
1445 if (charpos < 2 || top.charpos >= charpos)
1446 top_x = it.glyph_row->x;
1447 else
1449 struct it it2, it2_prev;
1450 /* The idea is to get to the previous buffer
1451 position, consume the character there, and use
1452 the pixel coordinates we get after that. But if
1453 the previous buffer position is also displayed
1454 from a display vector, we need to consume all of
1455 the glyphs from that display vector. */
1456 start_display (&it2, w, top);
1457 move_it_to (&it2, charpos - 1, -1, -1, -1, MOVE_TO_POS);
1458 /* If we didn't get to CHARPOS - 1, there's some
1459 replacing display property at that position, and
1460 we stopped after it. That is exactly the place
1461 whose coordinates we want. */
1462 if (IT_CHARPOS (it2) != charpos - 1)
1463 it2_prev = it2;
1464 else
1466 /* Iterate until we get out of the display
1467 vector that displays the character at
1468 CHARPOS - 1. */
1469 do {
1470 get_next_display_element (&it2);
1471 PRODUCE_GLYPHS (&it2);
1472 it2_prev = it2;
1473 set_iterator_to_next (&it2, true);
1474 } while (it2.method == GET_FROM_DISPLAY_VECTOR
1475 && IT_CHARPOS (it2) < charpos);
1477 if (ITERATOR_AT_END_OF_LINE_P (&it2_prev)
1478 || it2_prev.current_x > it2_prev.last_visible_x)
1479 top_x = it.glyph_row->x;
1480 else
1482 top_x = it2_prev.current_x;
1483 top_y = it2_prev.current_y;
1487 else if (IT_CHARPOS (it) != charpos)
1489 Lisp_Object cpos = make_number (charpos);
1490 Lisp_Object spec = Fget_char_property (cpos, Qdisplay, Qnil);
1491 Lisp_Object string = string_from_display_spec (spec);
1492 struct text_pos tpos;
1493 bool newline_in_string
1494 = (STRINGP (string)
1495 && memchr (SDATA (string), '\n', SBYTES (string)));
1497 SET_TEXT_POS (tpos, charpos, CHAR_TO_BYTE (charpos));
1498 bool replacing_spec_p
1499 = (!NILP (spec)
1500 && handle_display_spec (NULL, spec, Qnil, Qnil, &tpos,
1501 charpos, FRAME_WINDOW_P (it.f)));
1502 /* The tricky code below is needed because there's a
1503 discrepancy between move_it_to and how we set cursor
1504 when PT is at the beginning of a portion of text
1505 covered by a display property or an overlay with a
1506 display property, or the display line ends in a
1507 newline from a display string. move_it_to will stop
1508 _after_ such display strings, whereas
1509 set_cursor_from_row conspires with cursor_row_p to
1510 place the cursor on the first glyph produced from the
1511 display string. */
1513 /* We have overshoot PT because it is covered by a
1514 display property that replaces the text it covers.
1515 If the string includes embedded newlines, we are also
1516 in the wrong display line. Backtrack to the correct
1517 line, where the display property begins. */
1518 if (replacing_spec_p)
1520 Lisp_Object startpos, endpos;
1521 EMACS_INT start, end;
1522 struct it it3;
1524 /* Find the first and the last buffer positions
1525 covered by the display string. */
1526 endpos =
1527 Fnext_single_char_property_change (cpos, Qdisplay,
1528 Qnil, Qnil);
1529 startpos =
1530 Fprevious_single_char_property_change (endpos, Qdisplay,
1531 Qnil, Qnil);
1532 start = XFASTINT (startpos);
1533 end = XFASTINT (endpos);
1534 /* Move to the last buffer position before the
1535 display property. */
1536 start_display (&it3, w, top);
1537 if (start > CHARPOS (top))
1538 move_it_to (&it3, start - 1, -1, -1, -1, MOVE_TO_POS);
1539 /* Move forward one more line if the position before
1540 the display string is a newline or if it is the
1541 rightmost character on a line that is
1542 continued or word-wrapped. */
1543 if (it3.method == GET_FROM_BUFFER
1544 && (it3.c == '\n'
1545 || FETCH_BYTE (IT_BYTEPOS (it3)) == '\n'))
1546 move_it_by_lines (&it3, 1);
1547 else if (move_it_in_display_line_to (&it3, -1,
1548 it3.current_x
1549 + it3.pixel_width,
1550 MOVE_TO_X)
1551 == MOVE_LINE_CONTINUED)
1553 move_it_by_lines (&it3, 1);
1554 /* When we are under word-wrap, the #$@%!
1555 move_it_by_lines moves 2 lines, so we need to
1556 fix that up. */
1557 if (it3.line_wrap == WORD_WRAP)
1558 move_it_by_lines (&it3, -1);
1561 /* Record the vertical coordinate of the display
1562 line where we wound up. */
1563 top_y = it3.current_y;
1564 if (it3.bidi_p)
1566 /* When characters are reordered for display,
1567 the character displayed to the left of the
1568 display string could be _after_ the display
1569 property in the logical order. Use the
1570 smallest vertical position of these two. */
1571 start_display (&it3, w, top);
1572 move_it_to (&it3, end + 1, -1, -1, -1, MOVE_TO_POS);
1573 if (it3.current_y < top_y)
1574 top_y = it3.current_y;
1576 /* Move from the top of the window to the beginning
1577 of the display line where the display string
1578 begins. */
1579 start_display (&it3, w, top);
1580 move_it_to (&it3, -1, 0, top_y, -1, MOVE_TO_X | MOVE_TO_Y);
1581 /* If it3_moved stays false after the 'while' loop
1582 below, that means we already were at a newline
1583 before the loop (e.g., the display string begins
1584 with a newline), so we don't need to (and cannot)
1585 inspect the glyphs of it3.glyph_row, because
1586 PRODUCE_GLYPHS will not produce anything for a
1587 newline, and thus it3.glyph_row stays at its
1588 stale content it got at top of the window. */
1589 bool it3_moved = false;
1590 /* Finally, advance the iterator until we hit the
1591 first display element whose character position is
1592 CHARPOS, or until the first newline from the
1593 display string, which signals the end of the
1594 display line. */
1595 while (get_next_display_element (&it3))
1597 PRODUCE_GLYPHS (&it3);
1598 if (IT_CHARPOS (it3) == charpos
1599 || ITERATOR_AT_END_OF_LINE_P (&it3))
1600 break;
1601 it3_moved = true;
1602 set_iterator_to_next (&it3, false);
1604 top_x = it3.current_x - it3.pixel_width;
1605 /* Normally, we would exit the above loop because we
1606 found the display element whose character
1607 position is CHARPOS. For the contingency that we
1608 didn't, and stopped at the first newline from the
1609 display string, move back over the glyphs
1610 produced from the string, until we find the
1611 rightmost glyph not from the string. */
1612 if (it3_moved
1613 && newline_in_string
1614 && IT_CHARPOS (it3) != charpos && EQ (it3.object, string))
1616 struct glyph *g = it3.glyph_row->glyphs[TEXT_AREA]
1617 + it3.glyph_row->used[TEXT_AREA];
1619 while (EQ ((g - 1)->object, string))
1621 --g;
1622 top_x -= g->pixel_width;
1624 eassert (g < it3.glyph_row->glyphs[TEXT_AREA]
1625 + it3.glyph_row->used[TEXT_AREA]);
1630 *x = top_x;
1631 *y = max (top_y + max (0, it.max_ascent - it.ascent), window_top_y);
1632 *rtop = max (0, window_top_y - top_y);
1633 *rbot = max (0, bottom_y - it.last_visible_y);
1634 *rowh = max (0, (min (bottom_y, it.last_visible_y)
1635 - max (top_y, window_top_y)));
1636 *vpos = it.vpos;
1637 if (it.bidi_it.paragraph_dir == R2L)
1638 r2l = true;
1641 else
1643 /* Either we were asked to provide info about WINDOW_END, or
1644 CHARPOS is in the partially visible glyph row at end of
1645 window. */
1646 struct it it2;
1647 void *it2data = NULL;
1649 SAVE_IT (it2, it, it2data);
1650 if (IT_CHARPOS (it) < ZV && FETCH_BYTE (IT_BYTEPOS (it)) != '\n')
1651 move_it_by_lines (&it, 1);
1652 if (charpos < IT_CHARPOS (it)
1653 || (it.what == IT_EOB && charpos == IT_CHARPOS (it)))
1655 visible_p = true;
1656 RESTORE_IT (&it2, &it2, it2data);
1657 move_it_to (&it2, charpos, -1, -1, -1, MOVE_TO_POS);
1658 *x = it2.current_x;
1659 *y = it2.current_y + it2.max_ascent - it2.ascent;
1660 *rtop = max (0, -it2.current_y);
1661 *rbot = max (0, ((it2.current_y + it2.max_ascent + it2.max_descent)
1662 - it.last_visible_y));
1663 *rowh = max (0, (min (it2.current_y + it2.max_ascent + it2.max_descent,
1664 it.last_visible_y)
1665 - max (it2.current_y,
1666 WINDOW_HEADER_LINE_HEIGHT (w))));
1667 *vpos = it2.vpos;
1668 if (it2.bidi_it.paragraph_dir == R2L)
1669 r2l = true;
1671 else
1672 bidi_unshelve_cache (it2data, true);
1674 bidi_unshelve_cache (itdata, false);
1676 if (old_buffer)
1677 set_buffer_internal_1 (old_buffer);
1679 if (visible_p)
1681 if (w->hscroll > 0)
1682 *x -=
1683 window_hscroll_limited (w, WINDOW_XFRAME (w))
1684 * WINDOW_FRAME_COLUMN_WIDTH (w);
1685 /* For lines in an R2L paragraph, we need to mirror the X pixel
1686 coordinate wrt the text area. For the reasons, see the
1687 commentary in buffer_posn_from_coords and the explanation of
1688 the geometry used by the move_it_* functions at the end of
1689 the large commentary near the beginning of this file. */
1690 if (r2l)
1691 *x = window_box_width (w, TEXT_AREA) - *x - 1;
1694 #if false
1695 /* Debugging code. */
1696 if (visible_p)
1697 fprintf (stderr, "+pv pt=%d vs=%d --> x=%d y=%d rt=%d rb=%d rh=%d vp=%d\n",
1698 charpos, w->vscroll, *x, *y, *rtop, *rbot, *rowh, *vpos);
1699 else
1700 fprintf (stderr, "-pv pt=%d vs=%d\n", charpos, w->vscroll);
1701 #endif
1703 /* Restore potentially overwritten values. */
1704 w->mode_line_height = prev_mode_line_height;
1705 w->header_line_height = prev_header_line_height;
1707 return visible_p;
1711 /* Return the next character from STR. Return in *LEN the length of
1712 the character. This is like STRING_CHAR_AND_LENGTH but never
1713 returns an invalid character. If we find one, we return a `?', but
1714 with the length of the invalid character. */
1716 static int
1717 string_char_and_length (const unsigned char *str, int *len)
1719 int c;
1721 c = STRING_CHAR_AND_LENGTH (str, *len);
1722 if (!CHAR_VALID_P (c))
1723 /* We may not change the length here because other places in Emacs
1724 don't use this function, i.e. they silently accept invalid
1725 characters. */
1726 c = '?';
1728 return c;
1733 /* Given a position POS containing a valid character and byte position
1734 in STRING, return the position NCHARS ahead (NCHARS >= 0). */
1736 static struct text_pos
1737 string_pos_nchars_ahead (struct text_pos pos, Lisp_Object string, ptrdiff_t nchars)
1739 eassert (STRINGP (string) && nchars >= 0);
1741 if (STRING_MULTIBYTE (string))
1743 const unsigned char *p = SDATA (string) + BYTEPOS (pos);
1744 int len;
1746 while (nchars--)
1748 string_char_and_length (p, &len);
1749 p += len;
1750 CHARPOS (pos) += 1;
1751 BYTEPOS (pos) += len;
1754 else
1755 SET_TEXT_POS (pos, CHARPOS (pos) + nchars, BYTEPOS (pos) + nchars);
1757 return pos;
1761 /* Value is the text position, i.e. character and byte position,
1762 for character position CHARPOS in STRING. */
1764 static struct text_pos
1765 string_pos (ptrdiff_t charpos, Lisp_Object string)
1767 struct text_pos pos;
1768 eassert (STRINGP (string));
1769 eassert (charpos >= 0);
1770 SET_TEXT_POS (pos, charpos, string_char_to_byte (string, charpos));
1771 return pos;
1775 /* Value is a text position, i.e. character and byte position, for
1776 character position CHARPOS in C string S. MULTIBYTE_P
1777 means recognize multibyte characters. */
1779 static struct text_pos
1780 c_string_pos (ptrdiff_t charpos, const char *s, bool multibyte_p)
1782 struct text_pos pos;
1784 eassert (s != NULL);
1785 eassert (charpos >= 0);
1787 if (multibyte_p)
1789 int len;
1791 SET_TEXT_POS (pos, 0, 0);
1792 while (charpos--)
1794 string_char_and_length ((const unsigned char *) s, &len);
1795 s += len;
1796 CHARPOS (pos) += 1;
1797 BYTEPOS (pos) += len;
1800 else
1801 SET_TEXT_POS (pos, charpos, charpos);
1803 return pos;
1807 /* Value is the number of characters in C string S. MULTIBYTE_P
1808 means recognize multibyte characters. */
1810 static ptrdiff_t
1811 number_of_chars (const char *s, bool multibyte_p)
1813 ptrdiff_t nchars;
1815 if (multibyte_p)
1817 ptrdiff_t rest = strlen (s);
1818 int len;
1819 const unsigned char *p = (const unsigned char *) s;
1821 for (nchars = 0; rest > 0; ++nchars)
1823 string_char_and_length (p, &len);
1824 rest -= len, p += len;
1827 else
1828 nchars = strlen (s);
1830 return nchars;
1834 /* Compute byte position NEWPOS->bytepos corresponding to
1835 NEWPOS->charpos. POS is a known position in string STRING.
1836 NEWPOS->charpos must be >= POS.charpos. */
1838 static void
1839 compute_string_pos (struct text_pos *newpos, struct text_pos pos, Lisp_Object string)
1841 eassert (STRINGP (string));
1842 eassert (CHARPOS (*newpos) >= CHARPOS (pos));
1844 if (STRING_MULTIBYTE (string))
1845 *newpos = string_pos_nchars_ahead (pos, string,
1846 CHARPOS (*newpos) - CHARPOS (pos));
1847 else
1848 BYTEPOS (*newpos) = CHARPOS (*newpos);
1851 /* EXPORT:
1852 Return an estimation of the pixel height of mode or header lines on
1853 frame F. FACE_ID specifies what line's height to estimate. */
1856 estimate_mode_line_height (struct frame *f, enum face_id face_id)
1858 #ifdef HAVE_WINDOW_SYSTEM
1859 if (FRAME_WINDOW_P (f))
1861 int height = FONT_HEIGHT (FRAME_FONT (f));
1863 /* This function is called so early when Emacs starts that the face
1864 cache and mode line face are not yet initialized. */
1865 if (FRAME_FACE_CACHE (f))
1867 struct face *face = FACE_FROM_ID_OR_NULL (f, face_id);
1868 if (face)
1870 if (face->font)
1871 height = normal_char_height (face->font, -1);
1872 if (face->box_line_width > 0)
1873 height += 2 * face->box_line_width;
1877 return height;
1879 #endif
1881 return 1;
1884 /* Given a pixel position (PIX_X, PIX_Y) on frame F, return glyph
1885 co-ordinates in (*X, *Y). Set *BOUNDS to the rectangle that the
1886 glyph at X, Y occupies, if BOUNDS != 0. If NOCLIP, do
1887 not force the value into range. */
1889 void
1890 pixel_to_glyph_coords (struct frame *f, int pix_x, int pix_y, int *x, int *y,
1891 NativeRectangle *bounds, bool noclip)
1894 #ifdef HAVE_WINDOW_SYSTEM
1895 if (FRAME_WINDOW_P (f))
1897 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to round down
1898 even for negative values. */
1899 if (pix_x < 0)
1900 pix_x -= FRAME_COLUMN_WIDTH (f) - 1;
1901 if (pix_y < 0)
1902 pix_y -= FRAME_LINE_HEIGHT (f) - 1;
1904 pix_x = FRAME_PIXEL_X_TO_COL (f, pix_x);
1905 pix_y = FRAME_PIXEL_Y_TO_LINE (f, pix_y);
1907 if (bounds)
1908 STORE_NATIVE_RECT (*bounds,
1909 FRAME_COL_TO_PIXEL_X (f, pix_x),
1910 FRAME_LINE_TO_PIXEL_Y (f, pix_y),
1911 FRAME_COLUMN_WIDTH (f) - 1,
1912 FRAME_LINE_HEIGHT (f) - 1);
1914 /* PXW: Should we clip pixels before converting to columns/lines? */
1915 if (!noclip)
1917 if (pix_x < 0)
1918 pix_x = 0;
1919 else if (pix_x > FRAME_TOTAL_COLS (f))
1920 pix_x = FRAME_TOTAL_COLS (f);
1922 if (pix_y < 0)
1923 pix_y = 0;
1924 else if (pix_y > FRAME_TOTAL_LINES (f))
1925 pix_y = FRAME_TOTAL_LINES (f);
1928 #endif
1930 *x = pix_x;
1931 *y = pix_y;
1935 /* Find the glyph under window-relative coordinates X/Y in window W.
1936 Consider only glyphs from buffer text, i.e. no glyphs from overlay
1937 strings. Return in *HPOS and *VPOS the row and column number of
1938 the glyph found. Return in *AREA the glyph area containing X.
1939 Value is a pointer to the glyph found or null if X/Y is not on
1940 text, or we can't tell because W's current matrix is not up to
1941 date. */
1943 static struct glyph *
1944 x_y_to_hpos_vpos (struct window *w, int x, int y, int *hpos, int *vpos,
1945 int *dx, int *dy, int *area)
1947 struct glyph *glyph, *end;
1948 struct glyph_row *row = NULL;
1949 int x0, i;
1951 /* Find row containing Y. Give up if some row is not enabled. */
1952 for (i = 0; i < w->current_matrix->nrows; ++i)
1954 row = MATRIX_ROW (w->current_matrix, i);
1955 if (!row->enabled_p)
1956 return NULL;
1957 if (y >= row->y && y < MATRIX_ROW_BOTTOM_Y (row))
1958 break;
1961 *vpos = i;
1962 *hpos = 0;
1964 /* Give up if Y is not in the window. */
1965 if (i == w->current_matrix->nrows)
1966 return NULL;
1968 /* Get the glyph area containing X. */
1969 if (w->pseudo_window_p)
1971 *area = TEXT_AREA;
1972 x0 = 0;
1974 else
1976 if (x < window_box_left_offset (w, TEXT_AREA))
1978 *area = LEFT_MARGIN_AREA;
1979 x0 = window_box_left_offset (w, LEFT_MARGIN_AREA);
1981 else if (x < window_box_right_offset (w, TEXT_AREA))
1983 *area = TEXT_AREA;
1984 x0 = window_box_left_offset (w, TEXT_AREA) + min (row->x, 0);
1986 else
1988 *area = RIGHT_MARGIN_AREA;
1989 x0 = window_box_left_offset (w, RIGHT_MARGIN_AREA);
1993 /* Find glyph containing X. */
1994 glyph = row->glyphs[*area];
1995 end = glyph + row->used[*area];
1996 x -= x0;
1997 while (glyph < end && x >= glyph->pixel_width)
1999 x -= glyph->pixel_width;
2000 ++glyph;
2003 if (glyph == end)
2004 return NULL;
2006 if (dx)
2008 *dx = x;
2009 *dy = y - (row->y + row->ascent - glyph->ascent);
2012 *hpos = glyph - row->glyphs[*area];
2013 return glyph;
2016 /* Convert frame-relative x/y to coordinates relative to window W.
2017 Takes pseudo-windows into account. */
2019 static void
2020 frame_to_window_pixel_xy (struct window *w, int *x, int *y)
2022 if (w->pseudo_window_p)
2024 /* A pseudo-window is always full-width, and starts at the
2025 left edge of the frame, plus a frame border. */
2026 struct frame *f = XFRAME (w->frame);
2027 *x -= FRAME_INTERNAL_BORDER_WIDTH (f);
2028 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
2030 else
2032 *x -= WINDOW_LEFT_EDGE_X (w);
2033 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
2037 #ifdef HAVE_WINDOW_SYSTEM
2039 /* EXPORT:
2040 Return in RECTS[] at most N clipping rectangles for glyph string S.
2041 Return the number of stored rectangles. */
2044 get_glyph_string_clip_rects (struct glyph_string *s, NativeRectangle *rects, int n)
2046 XRectangle r;
2048 if (n <= 0)
2049 return 0;
2051 if (s->row->full_width_p)
2053 /* Draw full-width. X coordinates are relative to S->w->left_col. */
2054 r.x = WINDOW_LEFT_EDGE_X (s->w);
2055 if (s->row->mode_line_p)
2056 r.width = WINDOW_PIXEL_WIDTH (s->w) - WINDOW_RIGHT_DIVIDER_WIDTH (s->w);
2057 else
2058 r.width = WINDOW_PIXEL_WIDTH (s->w);
2060 /* Unless displaying a mode or menu bar line, which are always
2061 fully visible, clip to the visible part of the row. */
2062 if (s->w->pseudo_window_p)
2063 r.height = s->row->visible_height;
2064 else
2065 r.height = s->height;
2067 else
2069 /* This is a text line that may be partially visible. */
2070 r.x = window_box_left (s->w, s->area);
2071 r.width = window_box_width (s->w, s->area);
2072 r.height = s->row->visible_height;
2075 if (s->clip_head)
2076 if (r.x < s->clip_head->x)
2078 if (r.width >= s->clip_head->x - r.x)
2079 r.width -= s->clip_head->x - r.x;
2080 else
2081 r.width = 0;
2082 r.x = s->clip_head->x;
2084 if (s->clip_tail)
2085 if (r.x + r.width > s->clip_tail->x + s->clip_tail->background_width)
2087 if (s->clip_tail->x + s->clip_tail->background_width >= r.x)
2088 r.width = s->clip_tail->x + s->clip_tail->background_width - r.x;
2089 else
2090 r.width = 0;
2093 /* If S draws overlapping rows, it's sufficient to use the top and
2094 bottom of the window for clipping because this glyph string
2095 intentionally draws over other lines. */
2096 if (s->for_overlaps)
2098 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
2099 r.height = window_text_bottom_y (s->w) - r.y;
2101 /* Alas, the above simple strategy does not work for the
2102 environments with anti-aliased text: if the same text is
2103 drawn onto the same place multiple times, it gets thicker.
2104 If the overlap we are processing is for the erased cursor, we
2105 take the intersection with the rectangle of the cursor. */
2106 if (s->for_overlaps & OVERLAPS_ERASED_CURSOR)
2108 XRectangle rc, r_save = r;
2110 rc.x = WINDOW_TEXT_TO_FRAME_PIXEL_X (s->w, s->w->phys_cursor.x);
2111 rc.y = s->w->phys_cursor.y;
2112 rc.width = s->w->phys_cursor_width;
2113 rc.height = s->w->phys_cursor_height;
2115 x_intersect_rectangles (&r_save, &rc, &r);
2118 else
2120 /* Don't use S->y for clipping because it doesn't take partially
2121 visible lines into account. For example, it can be negative for
2122 partially visible lines at the top of a window. */
2123 if (!s->row->full_width_p
2124 && MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (s->w, s->row))
2125 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
2126 else
2127 r.y = max (0, s->row->y);
2130 r.y = WINDOW_TO_FRAME_PIXEL_Y (s->w, r.y);
2132 /* If drawing the cursor, don't let glyph draw outside its
2133 advertised boundaries. Cleartype does this under some circumstances. */
2134 if (s->hl == DRAW_CURSOR)
2136 struct glyph *glyph = s->first_glyph;
2137 int height, max_y;
2139 if (s->x > r.x)
2141 if (r.width >= s->x - r.x)
2142 r.width -= s->x - r.x;
2143 else /* R2L hscrolled row with cursor outside text area */
2144 r.width = 0;
2145 r.x = s->x;
2147 r.width = min (r.width, glyph->pixel_width);
2149 /* If r.y is below window bottom, ensure that we still see a cursor. */
2150 height = min (glyph->ascent + glyph->descent,
2151 min (FRAME_LINE_HEIGHT (s->f), s->row->visible_height));
2152 max_y = window_text_bottom_y (s->w) - height;
2153 max_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, max_y);
2154 if (s->ybase - glyph->ascent > max_y)
2156 r.y = max_y;
2157 r.height = height;
2159 else
2161 /* Don't draw cursor glyph taller than our actual glyph. */
2162 height = max (FRAME_LINE_HEIGHT (s->f), glyph->ascent + glyph->descent);
2163 if (height < r.height)
2165 max_y = r.y + r.height;
2166 r.y = min (max_y, max (r.y, s->ybase + glyph->descent - height));
2167 r.height = min (max_y - r.y, height);
2172 if (s->row->clip)
2174 XRectangle r_save = r;
2176 if (! x_intersect_rectangles (&r_save, s->row->clip, &r))
2177 r.width = 0;
2180 if ((s->for_overlaps & OVERLAPS_BOTH) == 0
2181 || ((s->for_overlaps & OVERLAPS_BOTH) == OVERLAPS_BOTH && n == 1))
2183 #ifdef CONVERT_FROM_XRECT
2184 CONVERT_FROM_XRECT (r, *rects);
2185 #else
2186 *rects = r;
2187 #endif
2188 return 1;
2190 else
2192 /* If we are processing overlapping and allowed to return
2193 multiple clipping rectangles, we exclude the row of the glyph
2194 string from the clipping rectangle. This is to avoid drawing
2195 the same text on the environment with anti-aliasing. */
2196 #ifdef CONVERT_FROM_XRECT
2197 XRectangle rs[2];
2198 #else
2199 XRectangle *rs = rects;
2200 #endif
2201 int i = 0, row_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, s->row->y);
2203 if (s->for_overlaps & OVERLAPS_PRED)
2205 rs[i] = r;
2206 if (r.y + r.height > row_y)
2208 if (r.y < row_y)
2209 rs[i].height = row_y - r.y;
2210 else
2211 rs[i].height = 0;
2213 i++;
2215 if (s->for_overlaps & OVERLAPS_SUCC)
2217 rs[i] = r;
2218 if (r.y < row_y + s->row->visible_height)
2220 if (r.y + r.height > row_y + s->row->visible_height)
2222 rs[i].y = row_y + s->row->visible_height;
2223 rs[i].height = r.y + r.height - rs[i].y;
2225 else
2226 rs[i].height = 0;
2228 i++;
2231 n = i;
2232 #ifdef CONVERT_FROM_XRECT
2233 for (i = 0; i < n; i++)
2234 CONVERT_FROM_XRECT (rs[i], rects[i]);
2235 #endif
2236 return n;
2240 /* EXPORT:
2241 Return in *NR the clipping rectangle for glyph string S. */
2243 void
2244 get_glyph_string_clip_rect (struct glyph_string *s, NativeRectangle *nr)
2246 get_glyph_string_clip_rects (s, nr, 1);
2250 /* EXPORT:
2251 Return the position and height of the phys cursor in window W.
2252 Set w->phys_cursor_width to width of phys cursor.
2255 void
2256 get_phys_cursor_geometry (struct window *w, struct glyph_row *row,
2257 struct glyph *glyph, int *xp, int *yp, int *heightp)
2259 struct frame *f = XFRAME (WINDOW_FRAME (w));
2260 int x, y, wd, h, h0, y0, ascent;
2262 /* Compute the width of the rectangle to draw. If on a stretch
2263 glyph, and `x-stretch-block-cursor' is nil, don't draw a
2264 rectangle as wide as the glyph, but use a canonical character
2265 width instead. */
2266 wd = glyph->pixel_width;
2268 x = w->phys_cursor.x;
2269 if (x < 0)
2271 wd += x;
2272 x = 0;
2275 if (glyph->type == STRETCH_GLYPH
2276 && !x_stretch_cursor_p)
2277 wd = min (FRAME_COLUMN_WIDTH (f), wd);
2278 w->phys_cursor_width = wd;
2280 /* Don't let the hollow cursor glyph descend below the glyph row's
2281 ascent value, lest the hollow cursor looks funny. */
2282 y = w->phys_cursor.y;
2283 ascent = row->ascent;
2284 if (row->ascent < glyph->ascent)
2286 y -= glyph->ascent - row->ascent;
2287 ascent = glyph->ascent;
2290 /* If y is below window bottom, ensure that we still see a cursor. */
2291 h0 = min (FRAME_LINE_HEIGHT (f), row->visible_height);
2293 h = max (h0, ascent + glyph->descent);
2294 h0 = min (h0, ascent + glyph->descent);
2296 y0 = WINDOW_HEADER_LINE_HEIGHT (w);
2297 if (y < y0)
2299 h = max (h - (y0 - y) + 1, h0);
2300 y = y0 - 1;
2302 else
2304 y0 = window_text_bottom_y (w) - h0;
2305 if (y > y0)
2307 h += y - y0;
2308 y = y0;
2312 *xp = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, x);
2313 *yp = WINDOW_TO_FRAME_PIXEL_Y (w, y);
2314 *heightp = h;
2318 * Remember which glyph the mouse is over.
2321 void
2322 remember_mouse_glyph (struct frame *f, int gx, int gy, NativeRectangle *rect)
2324 Lisp_Object window;
2325 struct window *w;
2326 struct glyph_row *r, *gr, *end_row;
2327 enum window_part part;
2328 enum glyph_row_area area;
2329 int x, y, width, height;
2331 /* Try to determine frame pixel position and size of the glyph under
2332 frame pixel coordinates X/Y on frame F. */
2334 if (window_resize_pixelwise)
2336 width = height = 1;
2337 goto virtual_glyph;
2339 else if (!f->glyphs_initialized_p
2340 || (window = window_from_coordinates (f, gx, gy, &part, false),
2341 NILP (window)))
2343 width = FRAME_SMALLEST_CHAR_WIDTH (f);
2344 height = FRAME_SMALLEST_FONT_HEIGHT (f);
2345 goto virtual_glyph;
2348 w = XWINDOW (window);
2349 width = WINDOW_FRAME_COLUMN_WIDTH (w);
2350 height = WINDOW_FRAME_LINE_HEIGHT (w);
2352 x = window_relative_x_coord (w, part, gx);
2353 y = gy - WINDOW_TOP_EDGE_Y (w);
2355 r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
2356 end_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
2358 if (w->pseudo_window_p)
2360 area = TEXT_AREA;
2361 part = ON_MODE_LINE; /* Don't adjust margin. */
2362 goto text_glyph;
2365 switch (part)
2367 case ON_LEFT_MARGIN:
2368 area = LEFT_MARGIN_AREA;
2369 goto text_glyph;
2371 case ON_RIGHT_MARGIN:
2372 area = RIGHT_MARGIN_AREA;
2373 goto text_glyph;
2375 case ON_HEADER_LINE:
2376 case ON_MODE_LINE:
2377 gr = (part == ON_HEADER_LINE
2378 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
2379 : MATRIX_MODE_LINE_ROW (w->current_matrix));
2380 gy = gr->y;
2381 area = TEXT_AREA;
2382 goto text_glyph_row_found;
2384 case ON_TEXT:
2385 area = TEXT_AREA;
2387 text_glyph:
2388 gr = 0; gy = 0;
2389 for (; r <= end_row && r->enabled_p; ++r)
2390 if (r->y + r->height > y)
2392 gr = r; gy = r->y;
2393 break;
2396 text_glyph_row_found:
2397 if (gr && gy <= y)
2399 struct glyph *g = gr->glyphs[area];
2400 struct glyph *end = g + gr->used[area];
2402 height = gr->height;
2403 for (gx = gr->x; g < end; gx += g->pixel_width, ++g)
2404 if (gx + g->pixel_width > x)
2405 break;
2407 if (g < end)
2409 if (g->type == IMAGE_GLYPH)
2411 /* Don't remember when mouse is over image, as
2412 image may have hot-spots. */
2413 STORE_NATIVE_RECT (*rect, 0, 0, 0, 0);
2414 return;
2416 width = g->pixel_width;
2418 else
2420 /* Use nominal char spacing at end of line. */
2421 x -= gx;
2422 gx += (x / width) * width;
2425 if (part != ON_MODE_LINE && part != ON_HEADER_LINE)
2427 gx += window_box_left_offset (w, area);
2428 /* Don't expand over the modeline to make sure the vertical
2429 drag cursor is shown early enough. */
2430 height = min (height,
2431 max (0, WINDOW_BOX_HEIGHT_NO_MODE_LINE (w) - gy));
2434 else
2436 /* Use nominal line height at end of window. */
2437 gx = (x / width) * width;
2438 y -= gy;
2439 gy += (y / height) * height;
2440 if (part != ON_MODE_LINE && part != ON_HEADER_LINE)
2441 /* See comment above. */
2442 height = min (height,
2443 max (0, WINDOW_BOX_HEIGHT_NO_MODE_LINE (w) - gy));
2445 break;
2447 case ON_LEFT_FRINGE:
2448 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2449 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w)
2450 : window_box_right_offset (w, LEFT_MARGIN_AREA));
2451 width = WINDOW_LEFT_FRINGE_WIDTH (w);
2452 goto row_glyph;
2454 case ON_RIGHT_FRINGE:
2455 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2456 ? window_box_right_offset (w, RIGHT_MARGIN_AREA)
2457 : window_box_right_offset (w, TEXT_AREA));
2458 if (WINDOW_RIGHT_DIVIDER_WIDTH (w) == 0
2459 && !WINDOW_HAS_VERTICAL_SCROLL_BAR (w)
2460 && !WINDOW_RIGHTMOST_P (w))
2461 if (gx < WINDOW_PIXEL_WIDTH (w) - width)
2462 /* Make sure the vertical border can get her own glyph to the
2463 right of the one we build here. */
2464 width = WINDOW_RIGHT_FRINGE_WIDTH (w) - width;
2465 else
2466 width = WINDOW_PIXEL_WIDTH (w) - gx;
2467 else
2468 width = WINDOW_RIGHT_FRINGE_WIDTH (w);
2470 goto row_glyph;
2472 case ON_VERTICAL_BORDER:
2473 gx = WINDOW_PIXEL_WIDTH (w) - width;
2474 goto row_glyph;
2476 case ON_VERTICAL_SCROLL_BAR:
2477 gx = (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w)
2479 : (window_box_right_offset (w, RIGHT_MARGIN_AREA)
2480 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2481 ? WINDOW_RIGHT_FRINGE_WIDTH (w)
2482 : 0)));
2483 width = WINDOW_SCROLL_BAR_AREA_WIDTH (w);
2485 row_glyph:
2486 gr = 0, gy = 0;
2487 for (; r <= end_row && r->enabled_p; ++r)
2488 if (r->y + r->height > y)
2490 gr = r; gy = r->y;
2491 break;
2494 if (gr && gy <= y)
2495 height = gr->height;
2496 else
2498 /* Use nominal line height at end of window. */
2499 y -= gy;
2500 gy += (y / height) * height;
2502 break;
2504 case ON_RIGHT_DIVIDER:
2505 gx = WINDOW_PIXEL_WIDTH (w) - WINDOW_RIGHT_DIVIDER_WIDTH (w);
2506 width = WINDOW_RIGHT_DIVIDER_WIDTH (w);
2507 gy = 0;
2508 /* The bottom divider prevails. */
2509 height = WINDOW_PIXEL_HEIGHT (w) - WINDOW_BOTTOM_DIVIDER_WIDTH (w);
2510 goto add_edge;
2512 case ON_BOTTOM_DIVIDER:
2513 gx = 0;
2514 width = WINDOW_PIXEL_WIDTH (w);
2515 gy = WINDOW_PIXEL_HEIGHT (w) - WINDOW_BOTTOM_DIVIDER_WIDTH (w);
2516 height = WINDOW_BOTTOM_DIVIDER_WIDTH (w);
2517 goto add_edge;
2519 default:
2521 virtual_glyph:
2522 /* If there is no glyph under the mouse, then we divide the screen
2523 into a grid of the smallest glyph in the frame, and use that
2524 as our "glyph". */
2526 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to
2527 round down even for negative values. */
2528 if (gx < 0)
2529 gx -= width - 1;
2530 if (gy < 0)
2531 gy -= height - 1;
2533 gx = (gx / width) * width;
2534 gy = (gy / height) * height;
2536 goto store_rect;
2539 add_edge:
2540 gx += WINDOW_LEFT_EDGE_X (w);
2541 gy += WINDOW_TOP_EDGE_Y (w);
2543 store_rect:
2544 STORE_NATIVE_RECT (*rect, gx, gy, width, height);
2546 /* Visible feedback for debugging. */
2547 #if false && defined HAVE_X_WINDOWS
2548 XDrawRectangle (FRAME_X_DISPLAY (f), FRAME_X_DRAWABLE (f),
2549 f->output_data.x->normal_gc,
2550 gx, gy, width, height);
2551 #endif
2555 #endif /* HAVE_WINDOW_SYSTEM */
2557 static void
2558 adjust_window_ends (struct window *w, struct glyph_row *row, bool current)
2560 eassert (w);
2561 w->window_end_pos = Z - MATRIX_ROW_END_CHARPOS (row);
2562 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
2563 w->window_end_vpos
2564 = MATRIX_ROW_VPOS (row, current ? w->current_matrix : w->desired_matrix);
2567 static bool
2568 hscrolling_current_line_p (struct window *w)
2570 return (!w->suspend_auto_hscroll
2571 && EQ (Fbuffer_local_value (Qauto_hscroll_mode, w->contents),
2572 Qcurrent_line));
2575 /***********************************************************************
2576 Lisp form evaluation
2577 ***********************************************************************/
2579 /* Error handler for safe_eval and safe_call. */
2581 static Lisp_Object
2582 safe_eval_handler (Lisp_Object arg, ptrdiff_t nargs, Lisp_Object *args)
2584 add_to_log ("Error during redisplay: %S signaled %S",
2585 Flist (nargs, args), arg);
2586 return Qnil;
2589 /* Call function FUNC with the rest of NARGS - 1 arguments
2590 following. Return the result, or nil if something went
2591 wrong. Prevent redisplay during the evaluation. */
2593 static Lisp_Object
2594 safe__call (bool inhibit_quit, ptrdiff_t nargs, Lisp_Object func, va_list ap)
2596 Lisp_Object val;
2598 if (inhibit_eval_during_redisplay)
2599 val = Qnil;
2600 else
2602 ptrdiff_t i;
2603 ptrdiff_t count = SPECPDL_INDEX ();
2604 Lisp_Object *args;
2605 USE_SAFE_ALLOCA;
2606 SAFE_ALLOCA_LISP (args, nargs);
2608 args[0] = func;
2609 for (i = 1; i < nargs; i++)
2610 args[i] = va_arg (ap, Lisp_Object);
2612 specbind (Qinhibit_redisplay, Qt);
2613 if (inhibit_quit)
2614 specbind (Qinhibit_quit, Qt);
2615 /* Use Qt to ensure debugger does not run,
2616 so there is no possibility of wanting to redisplay. */
2617 val = internal_condition_case_n (Ffuncall, nargs, args, Qt,
2618 safe_eval_handler);
2619 SAFE_FREE ();
2620 val = unbind_to (count, val);
2623 return val;
2626 Lisp_Object
2627 safe_call (ptrdiff_t nargs, Lisp_Object func, ...)
2629 Lisp_Object retval;
2630 va_list ap;
2632 va_start (ap, func);
2633 retval = safe__call (false, nargs, func, ap);
2634 va_end (ap);
2635 return retval;
2638 /* Call function FN with one argument ARG.
2639 Return the result, or nil if something went wrong. */
2641 Lisp_Object
2642 safe_call1 (Lisp_Object fn, Lisp_Object arg)
2644 return safe_call (2, fn, arg);
2647 static Lisp_Object
2648 safe__call1 (bool inhibit_quit, Lisp_Object fn, ...)
2650 Lisp_Object retval;
2651 va_list ap;
2653 va_start (ap, fn);
2654 retval = safe__call (inhibit_quit, 2, fn, ap);
2655 va_end (ap);
2656 return retval;
2659 Lisp_Object
2660 safe_eval (Lisp_Object sexpr)
2662 return safe__call1 (false, Qeval, sexpr);
2665 static Lisp_Object
2666 safe__eval (bool inhibit_quit, Lisp_Object sexpr)
2668 return safe__call1 (inhibit_quit, Qeval, sexpr);
2671 /* Call function FN with two arguments ARG1 and ARG2.
2672 Return the result, or nil if something went wrong. */
2674 Lisp_Object
2675 safe_call2 (Lisp_Object fn, Lisp_Object arg1, Lisp_Object arg2)
2677 return safe_call (3, fn, arg1, arg2);
2682 /***********************************************************************
2683 Debugging
2684 ***********************************************************************/
2686 /* Define CHECK_IT to perform sanity checks on iterators.
2687 This is for debugging. It is too slow to do unconditionally. */
2689 static void
2690 CHECK_IT (struct it *it)
2692 #if false
2693 if (it->method == GET_FROM_STRING)
2695 eassert (STRINGP (it->string));
2696 eassert (IT_STRING_CHARPOS (*it) >= 0);
2698 else
2700 eassert (IT_STRING_CHARPOS (*it) < 0);
2701 if (it->method == GET_FROM_BUFFER)
2703 /* Check that character and byte positions agree. */
2704 eassert (IT_CHARPOS (*it) == BYTE_TO_CHAR (IT_BYTEPOS (*it)));
2708 if (it->dpvec)
2709 eassert (it->current.dpvec_index >= 0);
2710 else
2711 eassert (it->current.dpvec_index < 0);
2712 #endif
2716 /* Check that the window end of window W is what we expect it
2717 to be---the last row in the current matrix displaying text. */
2719 static void
2720 CHECK_WINDOW_END (struct window *w)
2722 #if defined GLYPH_DEBUG && defined ENABLE_CHECKING
2723 if (!MINI_WINDOW_P (w) && w->window_end_valid)
2725 struct glyph_row *row;
2726 eassert ((row = MATRIX_ROW (w->current_matrix, w->window_end_vpos),
2727 !row->enabled_p
2728 || MATRIX_ROW_DISPLAYS_TEXT_P (row)
2729 || MATRIX_ROW_VPOS (row, w->current_matrix) == 0));
2731 #endif
2734 /***********************************************************************
2735 Iterator initialization
2736 ***********************************************************************/
2738 /* Initialize IT for displaying current_buffer in window W, starting
2739 at character position CHARPOS. CHARPOS < 0 means that no buffer
2740 position is specified which is useful when the iterator is assigned
2741 a position later. BYTEPOS is the byte position corresponding to
2742 CHARPOS.
2744 If ROW is not null, calls to produce_glyphs with IT as parameter
2745 will produce glyphs in that row.
2747 BASE_FACE_ID is the id of a base face to use. It must be one of
2748 DEFAULT_FACE_ID for normal text, MODE_LINE_FACE_ID,
2749 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID for displaying
2750 mode lines, or TOOL_BAR_FACE_ID for displaying the tool-bar.
2752 If ROW is null and BASE_FACE_ID is equal to MODE_LINE_FACE_ID,
2753 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID, the iterator
2754 will be initialized to use the corresponding mode line glyph row of
2755 the desired matrix of W. */
2757 void
2758 init_iterator (struct it *it, struct window *w,
2759 ptrdiff_t charpos, ptrdiff_t bytepos,
2760 struct glyph_row *row, enum face_id base_face_id)
2762 enum face_id remapped_base_face_id = base_face_id;
2764 /* Some precondition checks. */
2765 eassert (w != NULL && it != NULL);
2766 eassert (charpos < 0 || (charpos >= BUF_BEG (current_buffer)
2767 && charpos <= ZV));
2769 /* If face attributes have been changed since the last redisplay,
2770 free realized faces now because they depend on face definitions
2771 that might have changed. Don't free faces while there might be
2772 desired matrices pending which reference these faces. */
2773 if (!inhibit_free_realized_faces)
2775 if (face_change)
2777 face_change = false;
2778 XFRAME (w->frame)->face_change = 0;
2779 free_all_realized_faces (Qnil);
2781 else if (XFRAME (w->frame)->face_change)
2783 XFRAME (w->frame)->face_change = 0;
2784 free_all_realized_faces (w->frame);
2788 /* Perhaps remap BASE_FACE_ID to a user-specified alternative. */
2789 if (! NILP (Vface_remapping_alist))
2790 remapped_base_face_id
2791 = lookup_basic_face (XFRAME (w->frame), base_face_id);
2793 /* Use one of the mode line rows of W's desired matrix if
2794 appropriate. */
2795 if (row == NULL)
2797 if (base_face_id == MODE_LINE_FACE_ID
2798 || base_face_id == MODE_LINE_INACTIVE_FACE_ID)
2799 row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
2800 else if (base_face_id == HEADER_LINE_FACE_ID)
2801 row = MATRIX_HEADER_LINE_ROW (w->desired_matrix);
2804 /* Clear IT, and set it->object and other IT's Lisp objects to Qnil.
2805 Other parts of redisplay rely on that. */
2806 memclear (it, sizeof *it);
2807 it->current.overlay_string_index = -1;
2808 it->current.dpvec_index = -1;
2809 it->base_face_id = remapped_base_face_id;
2810 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
2811 it->paragraph_embedding = L2R;
2812 it->bidi_it.w = w;
2814 /* The window in which we iterate over current_buffer: */
2815 XSETWINDOW (it->window, w);
2816 it->w = w;
2817 it->f = XFRAME (w->frame);
2819 it->cmp_it.id = -1;
2821 /* Extra space between lines (on window systems only). */
2822 if (base_face_id == DEFAULT_FACE_ID
2823 && FRAME_WINDOW_P (it->f))
2825 if (NATNUMP (BVAR (current_buffer, extra_line_spacing)))
2826 it->extra_line_spacing = XFASTINT (BVAR (current_buffer, extra_line_spacing));
2827 else if (FLOATP (BVAR (current_buffer, extra_line_spacing)))
2828 it->extra_line_spacing = (XFLOAT_DATA (BVAR (current_buffer, extra_line_spacing))
2829 * FRAME_LINE_HEIGHT (it->f));
2830 else if (it->f->extra_line_spacing > 0)
2831 it->extra_line_spacing = it->f->extra_line_spacing;
2834 /* If realized faces have been removed, e.g. because of face
2835 attribute changes of named faces, recompute them. When running
2836 in batch mode, the face cache of the initial frame is null. If
2837 we happen to get called, make a dummy face cache. */
2838 if (FRAME_FACE_CACHE (it->f) == NULL)
2839 init_frame_faces (it->f);
2840 if (FRAME_FACE_CACHE (it->f)->used == 0)
2841 recompute_basic_faces (it->f);
2843 it->override_ascent = -1;
2845 /* Are control characters displayed as `^C'? */
2846 it->ctl_arrow_p = !NILP (BVAR (current_buffer, ctl_arrow));
2848 /* -1 means everything between a CR and the following line end
2849 is invisible. >0 means lines indented more than this value are
2850 invisible. */
2851 it->selective = (INTEGERP (BVAR (current_buffer, selective_display))
2852 ? (clip_to_bounds
2853 (-1, XINT (BVAR (current_buffer, selective_display)),
2854 PTRDIFF_MAX))
2855 : (!NILP (BVAR (current_buffer, selective_display))
2856 ? -1 : 0));
2857 it->selective_display_ellipsis_p
2858 = !NILP (BVAR (current_buffer, selective_display_ellipses));
2860 /* Display table to use. */
2861 it->dp = window_display_table (w);
2863 /* Are multibyte characters enabled in current_buffer? */
2864 it->multibyte_p = !NILP (BVAR (current_buffer, enable_multibyte_characters));
2866 /* Get the position at which the redisplay_end_trigger hook should
2867 be run, if it is to be run at all. */
2868 if (MARKERP (w->redisplay_end_trigger)
2869 && XMARKER (w->redisplay_end_trigger)->buffer != 0)
2870 it->redisplay_end_trigger_charpos
2871 = marker_position (w->redisplay_end_trigger);
2872 else if (INTEGERP (w->redisplay_end_trigger))
2873 it->redisplay_end_trigger_charpos
2874 = clip_to_bounds (PTRDIFF_MIN, XINT (w->redisplay_end_trigger),
2875 PTRDIFF_MAX);
2877 it->tab_width = SANE_TAB_WIDTH (current_buffer);
2879 /* Are lines in the display truncated? */
2880 if (TRUNCATE != 0)
2881 it->line_wrap = TRUNCATE;
2882 if (base_face_id == DEFAULT_FACE_ID
2883 && !it->w->hscroll
2884 && (WINDOW_FULL_WIDTH_P (it->w)
2885 || NILP (Vtruncate_partial_width_windows)
2886 || (INTEGERP (Vtruncate_partial_width_windows)
2887 /* PXW: Shall we do something about this? */
2888 && (XINT (Vtruncate_partial_width_windows)
2889 <= WINDOW_TOTAL_COLS (it->w))))
2890 && NILP (BVAR (current_buffer, truncate_lines)))
2891 it->line_wrap = NILP (BVAR (current_buffer, word_wrap))
2892 ? WINDOW_WRAP : WORD_WRAP;
2894 /* Get dimensions of truncation and continuation glyphs. These are
2895 displayed as fringe bitmaps under X, but we need them for such
2896 frames when the fringes are turned off. The no_special_glyphs slot
2897 of the iterator's frame, when set, suppresses their display - by
2898 default for tooltip frames and when set via the 'no-special-glyphs'
2899 frame parameter. */
2900 #ifdef HAVE_WINDOW_SYSTEM
2901 if (!(FRAME_WINDOW_P (it->f) && it->f->no_special_glyphs))
2902 #endif
2904 if (it->line_wrap == TRUNCATE)
2906 /* We will need the truncation glyph. */
2907 eassert (it->glyph_row == NULL);
2908 produce_special_glyphs (it, IT_TRUNCATION);
2909 it->truncation_pixel_width = it->pixel_width;
2911 else
2913 /* We will need the continuation glyph. */
2914 eassert (it->glyph_row == NULL);
2915 produce_special_glyphs (it, IT_CONTINUATION);
2916 it->continuation_pixel_width = it->pixel_width;
2920 /* Reset these values to zero because the produce_special_glyphs
2921 above has changed them. */
2922 it->pixel_width = it->ascent = it->descent = 0;
2923 it->phys_ascent = it->phys_descent = 0;
2925 /* Set this after getting the dimensions of truncation and
2926 continuation glyphs, so that we don't produce glyphs when calling
2927 produce_special_glyphs, above. */
2928 it->glyph_row = row;
2929 it->area = TEXT_AREA;
2931 /* Get the dimensions of the display area. The display area
2932 consists of the visible window area plus a horizontally scrolled
2933 part to the left of the window. All x-values are relative to the
2934 start of this total display area. */
2935 if (base_face_id != DEFAULT_FACE_ID)
2937 /* Mode lines, menu bar in terminal frames. */
2938 it->first_visible_x = 0;
2939 it->last_visible_x = WINDOW_PIXEL_WIDTH (w);
2941 else
2943 /* When hscrolling only the current line, don't apply the
2944 hscroll here, it will be applied by display_line when it gets
2945 to laying out the line showing point. However, if the
2946 window's min_hscroll is positive, the user specified a lower
2947 bound for automatic hscrolling, so they expect the
2948 non-current lines to obey that hscroll amount. */
2949 if (hscrolling_current_line_p (w))
2951 if (w->min_hscroll > 0)
2952 it->first_visible_x = w->min_hscroll * FRAME_COLUMN_WIDTH (it->f);
2953 else
2954 it->first_visible_x = 0;
2956 else
2957 it->first_visible_x =
2958 window_hscroll_limited (w, it->f) * FRAME_COLUMN_WIDTH (it->f);
2959 it->last_visible_x = (it->first_visible_x
2960 + window_box_width (w, TEXT_AREA));
2962 /* If we truncate lines, leave room for the truncation glyph(s) at
2963 the right margin. Otherwise, leave room for the continuation
2964 glyph(s). Done only if the window has no right fringe. */
2965 if (WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0)
2967 if (it->line_wrap == TRUNCATE)
2968 it->last_visible_x -= it->truncation_pixel_width;
2969 else
2970 it->last_visible_x -= it->continuation_pixel_width;
2973 it->header_line_p = window_wants_header_line (w);
2974 it->current_y = WINDOW_HEADER_LINE_HEIGHT (w) + w->vscroll;
2977 /* Leave room for a border glyph. */
2978 if (!FRAME_WINDOW_P (it->f)
2979 && !WINDOW_RIGHTMOST_P (it->w))
2980 it->last_visible_x -= 1;
2982 it->last_visible_y = window_text_bottom_y (w);
2984 /* For mode lines and alike, arrange for the first glyph having a
2985 left box line if the face specifies a box. */
2986 if (base_face_id != DEFAULT_FACE_ID)
2988 struct face *face;
2990 it->face_id = remapped_base_face_id;
2992 /* If we have a boxed mode line, make the first character appear
2993 with a left box line. */
2994 face = FACE_FROM_ID_OR_NULL (it->f, remapped_base_face_id);
2995 if (face && face->box != FACE_NO_BOX)
2996 it->start_of_box_run_p = true;
2999 /* If a buffer position was specified, set the iterator there,
3000 getting overlays and face properties from that position. */
3001 if (charpos >= BUF_BEG (current_buffer))
3003 it->stop_charpos = charpos;
3004 it->end_charpos = ZV;
3005 eassert (charpos == BYTE_TO_CHAR (bytepos));
3006 IT_CHARPOS (*it) = charpos;
3007 IT_BYTEPOS (*it) = bytepos;
3009 /* We will rely on `reseat' to set this up properly, via
3010 handle_face_prop. */
3011 it->face_id = it->base_face_id;
3013 it->start = it->current;
3014 /* Do we need to reorder bidirectional text? Not if this is a
3015 unibyte buffer: by definition, none of the single-byte
3016 characters are strong R2L, so no reordering is needed. And
3017 bidi.c doesn't support unibyte buffers anyway. Also, don't
3018 reorder while we are loading loadup.el, since the tables of
3019 character properties needed for reordering are not yet
3020 available. */
3021 it->bidi_p =
3022 !redisplay__inhibit_bidi
3023 && !NILP (BVAR (current_buffer, bidi_display_reordering))
3024 && it->multibyte_p;
3026 /* If we are to reorder bidirectional text, init the bidi
3027 iterator. */
3028 if (it->bidi_p)
3030 /* Since we don't know at this point whether there will be
3031 any R2L lines in the window, we reserve space for
3032 truncation/continuation glyphs even if only the left
3033 fringe is absent. */
3034 if (base_face_id == DEFAULT_FACE_ID
3035 && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0
3036 && WINDOW_RIGHT_FRINGE_WIDTH (it->w) != 0)
3038 if (it->line_wrap == TRUNCATE)
3039 it->last_visible_x -= it->truncation_pixel_width;
3040 else
3041 it->last_visible_x -= it->continuation_pixel_width;
3043 /* Note the paragraph direction that this buffer wants to
3044 use. */
3045 if (EQ (BVAR (current_buffer, bidi_paragraph_direction),
3046 Qleft_to_right))
3047 it->paragraph_embedding = L2R;
3048 else if (EQ (BVAR (current_buffer, bidi_paragraph_direction),
3049 Qright_to_left))
3050 it->paragraph_embedding = R2L;
3051 else
3052 it->paragraph_embedding = NEUTRAL_DIR;
3053 bidi_unshelve_cache (NULL, false);
3054 bidi_init_it (charpos, IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
3055 &it->bidi_it);
3058 /* Compute faces etc. */
3059 reseat (it, it->current.pos, true);
3062 CHECK_IT (it);
3066 /* Initialize IT for the display of window W with window start POS. */
3068 void
3069 start_display (struct it *it, struct window *w, struct text_pos pos)
3071 struct glyph_row *row;
3072 bool first_vpos = window_wants_header_line (w);
3074 row = w->desired_matrix->rows + first_vpos;
3075 init_iterator (it, w, CHARPOS (pos), BYTEPOS (pos), row, DEFAULT_FACE_ID);
3076 it->first_vpos = first_vpos;
3078 /* Don't reseat to previous visible line start if current start
3079 position is in a string or image. */
3080 if (it->method == GET_FROM_BUFFER && it->line_wrap != TRUNCATE)
3082 int first_y = it->current_y;
3084 /* If window start is not at a line start, skip forward to POS to
3085 get the correct continuation lines width. */
3086 bool start_at_line_beg_p = (CHARPOS (pos) == BEGV
3087 || FETCH_BYTE (BYTEPOS (pos) - 1) == '\n');
3088 if (!start_at_line_beg_p)
3090 int new_x;
3092 reseat_at_previous_visible_line_start (it);
3093 move_it_to (it, CHARPOS (pos), -1, -1, -1, MOVE_TO_POS);
3095 new_x = it->current_x + it->pixel_width;
3097 /* If lines are continued, this line may end in the middle
3098 of a multi-glyph character (e.g. a control character
3099 displayed as \003, or in the middle of an overlay
3100 string). In this case move_it_to above will not have
3101 taken us to the start of the continuation line but to the
3102 end of the continued line. */
3103 if (it->current_x > 0
3104 && it->line_wrap != TRUNCATE /* Lines are continued. */
3105 && (/* And glyph doesn't fit on the line. */
3106 new_x > it->last_visible_x
3107 /* Or it fits exactly and we're on a window
3108 system frame. */
3109 || (new_x == it->last_visible_x
3110 && FRAME_WINDOW_P (it->f)
3111 && ((it->bidi_p && it->bidi_it.paragraph_dir == R2L)
3112 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
3113 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)))))
3115 if ((it->current.dpvec_index >= 0
3116 || it->current.overlay_string_index >= 0)
3117 /* If we are on a newline from a display vector or
3118 overlay string, then we are already at the end of
3119 a screen line; no need to go to the next line in
3120 that case, as this line is not really continued.
3121 (If we do go to the next line, C-e will not DTRT.) */
3122 && it->c != '\n')
3124 set_iterator_to_next (it, true);
3125 move_it_in_display_line_to (it, -1, -1, 0);
3128 it->continuation_lines_width += it->current_x;
3130 /* If the character at POS is displayed via a display
3131 vector, move_it_to above stops at the final glyph of
3132 IT->dpvec. To make the caller redisplay that character
3133 again (a.k.a. start at POS), we need to reset the
3134 dpvec_index to the beginning of IT->dpvec. */
3135 else if (it->current.dpvec_index >= 0)
3136 it->current.dpvec_index = 0;
3138 /* We're starting a new display line, not affected by the
3139 height of the continued line, so clear the appropriate
3140 fields in the iterator structure. */
3141 it->max_ascent = it->max_descent = 0;
3142 it->max_phys_ascent = it->max_phys_descent = 0;
3144 it->current_y = first_y;
3145 it->vpos = 0;
3146 it->current_x = it->hpos = 0;
3152 /* Return true if POS is a position in ellipses displayed for invisible
3153 text. W is the window we display, for text property lookup. */
3155 static bool
3156 in_ellipses_for_invisible_text_p (struct display_pos *pos, struct window *w)
3158 Lisp_Object prop, window;
3159 bool ellipses_p = false;
3160 ptrdiff_t charpos = CHARPOS (pos->pos);
3162 /* If POS specifies a position in a display vector, this might
3163 be for an ellipsis displayed for invisible text. We won't
3164 get the iterator set up for delivering that ellipsis unless
3165 we make sure that it gets aware of the invisible text. */
3166 if (pos->dpvec_index >= 0
3167 && pos->overlay_string_index < 0
3168 && CHARPOS (pos->string_pos) < 0
3169 && charpos > BEGV
3170 && (XSETWINDOW (window, w),
3171 prop = Fget_char_property (make_number (charpos),
3172 Qinvisible, window),
3173 TEXT_PROP_MEANS_INVISIBLE (prop) == 0))
3175 prop = Fget_char_property (make_number (charpos - 1), Qinvisible,
3176 window);
3177 ellipses_p = 2 == TEXT_PROP_MEANS_INVISIBLE (prop);
3180 return ellipses_p;
3184 /* Initialize IT for stepping through current_buffer in window W,
3185 starting at position POS that includes overlay string and display
3186 vector/ control character translation position information. Value
3187 is false if there are overlay strings with newlines at POS. */
3189 static bool
3190 init_from_display_pos (struct it *it, struct window *w, struct display_pos *pos)
3192 ptrdiff_t charpos = CHARPOS (pos->pos), bytepos = BYTEPOS (pos->pos);
3193 int i;
3194 bool overlay_strings_with_newlines = false;
3196 /* If POS specifies a position in a display vector, this might
3197 be for an ellipsis displayed for invisible text. We won't
3198 get the iterator set up for delivering that ellipsis unless
3199 we make sure that it gets aware of the invisible text. */
3200 if (in_ellipses_for_invisible_text_p (pos, w))
3202 --charpos;
3203 bytepos = 0;
3206 /* Keep in mind: the call to reseat in init_iterator skips invisible
3207 text, so we might end up at a position different from POS. This
3208 is only a problem when POS is a row start after a newline and an
3209 overlay starts there with an after-string, and the overlay has an
3210 invisible property. Since we don't skip invisible text in
3211 display_line and elsewhere immediately after consuming the
3212 newline before the row start, such a POS will not be in a string,
3213 but the call to init_iterator below will move us to the
3214 after-string. */
3215 init_iterator (it, w, charpos, bytepos, NULL, DEFAULT_FACE_ID);
3217 /* This only scans the current chunk -- it should scan all chunks.
3218 However, OVERLAY_STRING_CHUNK_SIZE has been increased from 3 in 21.1
3219 to 16 in 22.1 to make this a lesser problem. */
3220 for (i = 0; i < it->n_overlay_strings && i < OVERLAY_STRING_CHUNK_SIZE; ++i)
3222 const char *s = SSDATA (it->overlay_strings[i]);
3223 const char *e = s + SBYTES (it->overlay_strings[i]);
3225 while (s < e && *s != '\n')
3226 ++s;
3228 if (s < e)
3230 overlay_strings_with_newlines = true;
3231 break;
3235 /* If position is within an overlay string, set up IT to the right
3236 overlay string. */
3237 if (pos->overlay_string_index >= 0)
3239 int relative_index;
3241 /* If the first overlay string happens to have a `display'
3242 property for an image, the iterator will be set up for that
3243 image, and we have to undo that setup first before we can
3244 correct the overlay string index. */
3245 if (it->method == GET_FROM_IMAGE)
3246 pop_it (it);
3248 /* We already have the first chunk of overlay strings in
3249 IT->overlay_strings. Load more until the one for
3250 pos->overlay_string_index is in IT->overlay_strings. */
3251 if (pos->overlay_string_index >= OVERLAY_STRING_CHUNK_SIZE)
3253 ptrdiff_t n = pos->overlay_string_index / OVERLAY_STRING_CHUNK_SIZE;
3254 it->current.overlay_string_index = 0;
3255 while (n--)
3257 load_overlay_strings (it, 0);
3258 it->current.overlay_string_index += OVERLAY_STRING_CHUNK_SIZE;
3262 it->current.overlay_string_index = pos->overlay_string_index;
3263 relative_index = (it->current.overlay_string_index
3264 % OVERLAY_STRING_CHUNK_SIZE);
3265 it->string = it->overlay_strings[relative_index];
3266 eassert (STRINGP (it->string));
3267 it->current.string_pos = pos->string_pos;
3268 it->method = GET_FROM_STRING;
3269 it->end_charpos = SCHARS (it->string);
3270 /* Set up the bidi iterator for this overlay string. */
3271 if (it->bidi_p)
3273 it->bidi_it.string.lstring = it->string;
3274 it->bidi_it.string.s = NULL;
3275 it->bidi_it.string.schars = SCHARS (it->string);
3276 it->bidi_it.string.bufpos = it->overlay_strings_charpos;
3277 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
3278 it->bidi_it.string.unibyte = !it->multibyte_p;
3279 it->bidi_it.w = it->w;
3280 bidi_init_it (IT_STRING_CHARPOS (*it), IT_STRING_BYTEPOS (*it),
3281 FRAME_WINDOW_P (it->f), &it->bidi_it);
3283 /* Synchronize the state of the bidi iterator with
3284 pos->string_pos. For any string position other than
3285 zero, this will be done automagically when we resume
3286 iteration over the string and get_visually_first_element
3287 is called. But if string_pos is zero, and the string is
3288 to be reordered for display, we need to resync manually,
3289 since it could be that the iteration state recorded in
3290 pos ended at string_pos of 0 moving backwards in string. */
3291 if (CHARPOS (pos->string_pos) == 0)
3293 get_visually_first_element (it);
3294 if (IT_STRING_CHARPOS (*it) != 0)
3295 do {
3296 /* Paranoia. */
3297 eassert (it->bidi_it.charpos < it->bidi_it.string.schars);
3298 bidi_move_to_visually_next (&it->bidi_it);
3299 } while (it->bidi_it.charpos != 0);
3301 eassert (IT_STRING_CHARPOS (*it) == it->bidi_it.charpos
3302 && IT_STRING_BYTEPOS (*it) == it->bidi_it.bytepos);
3306 if (CHARPOS (pos->string_pos) >= 0)
3308 /* Recorded position is not in an overlay string, but in another
3309 string. This can only be a string from a `display' property.
3310 IT should already be filled with that string. */
3311 it->current.string_pos = pos->string_pos;
3312 eassert (STRINGP (it->string));
3313 if (it->bidi_p)
3314 bidi_init_it (IT_STRING_CHARPOS (*it), IT_STRING_BYTEPOS (*it),
3315 FRAME_WINDOW_P (it->f), &it->bidi_it);
3318 /* Restore position in display vector translations, control
3319 character translations or ellipses. */
3320 if (pos->dpvec_index >= 0)
3322 if (it->dpvec == NULL)
3323 get_next_display_element (it);
3324 eassert (it->dpvec && it->current.dpvec_index == 0);
3325 it->current.dpvec_index = pos->dpvec_index;
3328 CHECK_IT (it);
3329 return !overlay_strings_with_newlines;
3333 /* Initialize IT for stepping through current_buffer in window W
3334 starting at ROW->start. */
3336 static void
3337 init_to_row_start (struct it *it, struct window *w, struct glyph_row *row)
3339 init_from_display_pos (it, w, &row->start);
3340 it->start = row->start;
3341 it->continuation_lines_width = row->continuation_lines_width;
3342 CHECK_IT (it);
3346 /* Initialize IT for stepping through current_buffer in window W
3347 starting in the line following ROW, i.e. starting at ROW->end.
3348 Value is false if there are overlay strings with newlines at ROW's
3349 end position. */
3351 static bool
3352 init_to_row_end (struct it *it, struct window *w, struct glyph_row *row)
3354 bool success = false;
3356 if (init_from_display_pos (it, w, &row->end))
3358 if (row->continued_p)
3359 it->continuation_lines_width
3360 = row->continuation_lines_width + row->pixel_width;
3361 CHECK_IT (it);
3362 success = true;
3365 return success;
3371 /***********************************************************************
3372 Text properties
3373 ***********************************************************************/
3375 /* Called when IT reaches IT->stop_charpos. Handle text property and
3376 overlay changes. Set IT->stop_charpos to the next position where
3377 to stop. */
3379 static void
3380 handle_stop (struct it *it)
3382 enum prop_handled handled;
3383 bool handle_overlay_change_p;
3384 struct props *p;
3386 it->dpvec = NULL;
3387 it->current.dpvec_index = -1;
3388 handle_overlay_change_p = !it->ignore_overlay_strings_at_pos_p;
3389 it->ellipsis_p = false;
3391 /* Use face of preceding text for ellipsis (if invisible) */
3392 if (it->selective_display_ellipsis_p)
3393 it->saved_face_id = it->face_id;
3395 /* Here's the description of the semantics of, and the logic behind,
3396 the various HANDLED_* statuses:
3398 HANDLED_NORMALLY means the handler did its job, and the loop
3399 should proceed to calling the next handler in order.
3401 HANDLED_RECOMPUTE_PROPS means the handler caused a significant
3402 change in the properties and overlays at current position, so the
3403 loop should be restarted, to re-invoke the handlers that were
3404 already called. This happens when fontification-functions were
3405 called by handle_fontified_prop, and actually fontified
3406 something. Another case where HANDLED_RECOMPUTE_PROPS is
3407 returned is when we discover overlay strings that need to be
3408 displayed right away. The loop below will continue for as long
3409 as the status is HANDLED_RECOMPUTE_PROPS.
3411 HANDLED_RETURN means return immediately to the caller, to
3412 continue iteration without calling any further handlers. This is
3413 used when we need to act on some property right away, for example
3414 when we need to display the ellipsis or a replacing display
3415 property, such as display string or image.
3417 HANDLED_OVERLAY_STRING_CONSUMED means an overlay string was just
3418 consumed, and the handler switched to the next overlay string.
3419 This signals the loop below to refrain from looking for more
3420 overlays before all the overlay strings of the current overlay
3421 are processed.
3423 Some of the handlers called by the loop push the iterator state
3424 onto the stack (see 'push_it'), and arrange for the iteration to
3425 continue with another object, such as an image, a display string,
3426 or an overlay string. In most such cases, it->stop_charpos is
3427 set to the first character of the string, so that when the
3428 iteration resumes, this function will immediately be called
3429 again, to examine the properties at the beginning of the string.
3431 When a display or overlay string is exhausted, the iterator state
3432 is popped (see 'pop_it'), and iteration continues with the
3433 previous object. Again, in many such cases this function is
3434 called again to find the next position where properties might
3435 change. */
3439 handled = HANDLED_NORMALLY;
3441 /* Call text property handlers. */
3442 for (p = it_props; p->handler; ++p)
3444 handled = p->handler (it);
3446 if (handled == HANDLED_RECOMPUTE_PROPS)
3447 break;
3448 else if (handled == HANDLED_RETURN)
3450 /* We still want to show before and after strings from
3451 overlays even if the actual buffer text is replaced. */
3452 if (!handle_overlay_change_p
3453 || it->sp > 1
3454 /* Don't call get_overlay_strings_1 if we already
3455 have overlay strings loaded, because doing so
3456 will load them again and push the iterator state
3457 onto the stack one more time, which is not
3458 expected by the rest of the code that processes
3459 overlay strings. */
3460 || (it->current.overlay_string_index < 0
3461 && !get_overlay_strings_1 (it, 0, false)))
3463 if (it->ellipsis_p)
3464 setup_for_ellipsis (it, 0);
3465 /* When handling a display spec, we might load an
3466 empty string. In that case, discard it here. We
3467 used to discard it in handle_single_display_spec,
3468 but that causes get_overlay_strings_1, above, to
3469 ignore overlay strings that we must check. */
3470 if (STRINGP (it->string) && !SCHARS (it->string))
3471 pop_it (it);
3472 return;
3474 else if (STRINGP (it->string) && !SCHARS (it->string))
3475 pop_it (it);
3476 else
3478 it->string_from_display_prop_p = false;
3479 it->from_disp_prop_p = false;
3480 handle_overlay_change_p = false;
3482 handled = HANDLED_RECOMPUTE_PROPS;
3483 break;
3485 else if (handled == HANDLED_OVERLAY_STRING_CONSUMED)
3486 handle_overlay_change_p = false;
3489 if (handled != HANDLED_RECOMPUTE_PROPS)
3491 /* Don't check for overlay strings below when set to deliver
3492 characters from a display vector. */
3493 if (it->method == GET_FROM_DISPLAY_VECTOR)
3494 handle_overlay_change_p = false;
3496 /* Handle overlay changes.
3497 This sets HANDLED to HANDLED_RECOMPUTE_PROPS
3498 if it finds overlays. */
3499 if (handle_overlay_change_p)
3500 handled = handle_overlay_change (it);
3503 if (it->ellipsis_p)
3505 setup_for_ellipsis (it, 0);
3506 break;
3509 while (handled == HANDLED_RECOMPUTE_PROPS);
3511 /* Determine where to stop next. */
3512 if (handled == HANDLED_NORMALLY)
3513 compute_stop_pos (it);
3517 /* Compute IT->stop_charpos from text property and overlay change
3518 information for IT's current position. */
3520 static void
3521 compute_stop_pos (struct it *it)
3523 register INTERVAL iv, next_iv;
3524 Lisp_Object object, limit, position;
3525 ptrdiff_t charpos, bytepos;
3527 if (STRINGP (it->string))
3529 /* Strings are usually short, so don't limit the search for
3530 properties. */
3531 it->stop_charpos = it->end_charpos;
3532 object = it->string;
3533 limit = Qnil;
3534 charpos = IT_STRING_CHARPOS (*it);
3535 bytepos = IT_STRING_BYTEPOS (*it);
3537 else
3539 ptrdiff_t pos;
3541 /* If end_charpos is out of range for some reason, such as a
3542 misbehaving display function, rationalize it (Bug#5984). */
3543 if (it->end_charpos > ZV)
3544 it->end_charpos = ZV;
3545 it->stop_charpos = it->end_charpos;
3547 /* If next overlay change is in front of the current stop pos
3548 (which is IT->end_charpos), stop there. Note: value of
3549 next_overlay_change is point-max if no overlay change
3550 follows. */
3551 charpos = IT_CHARPOS (*it);
3552 bytepos = IT_BYTEPOS (*it);
3553 pos = next_overlay_change (charpos);
3554 if (pos < it->stop_charpos)
3555 it->stop_charpos = pos;
3557 /* Set up variables for computing the stop position from text
3558 property changes. */
3559 XSETBUFFER (object, current_buffer);
3560 limit = make_number (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT);
3563 /* Get the interval containing IT's position. Value is a null
3564 interval if there isn't such an interval. */
3565 position = make_number (charpos);
3566 iv = validate_interval_range (object, &position, &position, false);
3567 if (iv)
3569 Lisp_Object values_here[LAST_PROP_IDX];
3570 struct props *p;
3572 /* Get properties here. */
3573 for (p = it_props; p->handler; ++p)
3574 values_here[p->idx] = textget (iv->plist,
3575 builtin_lisp_symbol (p->name));
3577 /* Look for an interval following iv that has different
3578 properties. */
3579 for (next_iv = next_interval (iv);
3580 (next_iv
3581 && (NILP (limit)
3582 || XFASTINT (limit) > next_iv->position));
3583 next_iv = next_interval (next_iv))
3585 for (p = it_props; p->handler; ++p)
3587 Lisp_Object new_value = textget (next_iv->plist,
3588 builtin_lisp_symbol (p->name));
3589 if (!EQ (values_here[p->idx], new_value))
3590 break;
3593 if (p->handler)
3594 break;
3597 if (next_iv)
3599 if (INTEGERP (limit)
3600 && next_iv->position >= XFASTINT (limit))
3601 /* No text property change up to limit. */
3602 it->stop_charpos = min (XFASTINT (limit), it->stop_charpos);
3603 else
3604 /* Text properties change in next_iv. */
3605 it->stop_charpos = min (it->stop_charpos, next_iv->position);
3609 if (it->cmp_it.id < 0)
3611 ptrdiff_t stoppos = it->end_charpos;
3613 if (it->bidi_p && it->bidi_it.scan_dir < 0)
3614 stoppos = -1;
3615 composition_compute_stop_pos (&it->cmp_it, charpos, bytepos,
3616 stoppos, it->string);
3619 eassert (STRINGP (it->string)
3620 || (it->stop_charpos >= BEGV
3621 && it->stop_charpos >= IT_CHARPOS (*it)));
3625 /* Return the position of the next overlay change after POS in
3626 current_buffer. Value is point-max if no overlay change
3627 follows. This is like `next-overlay-change' but doesn't use
3628 xmalloc. */
3630 static ptrdiff_t
3631 next_overlay_change (ptrdiff_t pos)
3633 ptrdiff_t i, noverlays;
3634 ptrdiff_t endpos;
3635 Lisp_Object *overlays;
3636 USE_SAFE_ALLOCA;
3638 /* Get all overlays at the given position. */
3639 GET_OVERLAYS_AT (pos, overlays, noverlays, &endpos, true);
3641 /* If any of these overlays ends before endpos,
3642 use its ending point instead. */
3643 for (i = 0; i < noverlays; ++i)
3645 Lisp_Object oend;
3646 ptrdiff_t oendpos;
3648 oend = OVERLAY_END (overlays[i]);
3649 oendpos = OVERLAY_POSITION (oend);
3650 endpos = min (endpos, oendpos);
3653 SAFE_FREE ();
3654 return endpos;
3657 /* How many characters forward to search for a display property or
3658 display string. Searching too far forward makes the bidi display
3659 sluggish, especially in small windows. */
3660 #define MAX_DISP_SCAN 250
3662 /* Return the character position of a display string at or after
3663 position specified by POSITION. If no display string exists at or
3664 after POSITION, return ZV. A display string is either an overlay
3665 with `display' property whose value is a string, or a `display'
3666 text property whose value is a string. STRING is data about the
3667 string to iterate; if STRING->lstring is nil, we are iterating a
3668 buffer. FRAME_WINDOW_P is true when we are displaying a window
3669 on a GUI frame. DISP_PROP is set to zero if we searched
3670 MAX_DISP_SCAN characters forward without finding any display
3671 strings, non-zero otherwise. It is set to 2 if the display string
3672 uses any kind of `(space ...)' spec that will produce a stretch of
3673 white space in the text area. */
3674 ptrdiff_t
3675 compute_display_string_pos (struct text_pos *position,
3676 struct bidi_string_data *string,
3677 struct window *w,
3678 bool frame_window_p, int *disp_prop)
3680 /* OBJECT = nil means current buffer. */
3681 Lisp_Object object, object1;
3682 Lisp_Object pos, spec, limpos;
3683 bool string_p = string && (STRINGP (string->lstring) || string->s);
3684 ptrdiff_t eob = string_p ? string->schars : ZV;
3685 ptrdiff_t begb = string_p ? 0 : BEGV;
3686 ptrdiff_t bufpos, charpos = CHARPOS (*position);
3687 ptrdiff_t lim =
3688 (charpos < eob - MAX_DISP_SCAN) ? charpos + MAX_DISP_SCAN : eob;
3689 struct text_pos tpos;
3690 int rv = 0;
3692 if (string && STRINGP (string->lstring))
3693 object1 = object = string->lstring;
3694 else if (w && !string_p)
3696 XSETWINDOW (object, w);
3697 object1 = Qnil;
3699 else
3700 object1 = object = Qnil;
3702 *disp_prop = 1;
3704 if (charpos >= eob
3705 /* We don't support display properties whose values are strings
3706 that have display string properties. */
3707 || string->from_disp_str
3708 /* C strings cannot have display properties. */
3709 || (string->s && !STRINGP (object)))
3711 *disp_prop = 0;
3712 return eob;
3715 /* If the character at CHARPOS is where the display string begins,
3716 return CHARPOS. */
3717 pos = make_number (charpos);
3718 if (STRINGP (object))
3719 bufpos = string->bufpos;
3720 else
3721 bufpos = charpos;
3722 tpos = *position;
3723 if (!NILP (spec = Fget_char_property (pos, Qdisplay, object))
3724 && (charpos <= begb
3725 || !EQ (Fget_char_property (make_number (charpos - 1), Qdisplay,
3726 object),
3727 spec))
3728 && (rv = handle_display_spec (NULL, spec, object, Qnil, &tpos, bufpos,
3729 frame_window_p)))
3731 if (rv == 2)
3732 *disp_prop = 2;
3733 return charpos;
3736 /* Look forward for the first character with a `display' property
3737 that will replace the underlying text when displayed. */
3738 limpos = make_number (lim);
3739 do {
3740 pos = Fnext_single_char_property_change (pos, Qdisplay, object1, limpos);
3741 CHARPOS (tpos) = XFASTINT (pos);
3742 if (CHARPOS (tpos) >= lim)
3744 *disp_prop = 0;
3745 break;
3747 if (STRINGP (object))
3748 BYTEPOS (tpos) = string_char_to_byte (object, CHARPOS (tpos));
3749 else
3750 BYTEPOS (tpos) = CHAR_TO_BYTE (CHARPOS (tpos));
3751 spec = Fget_char_property (pos, Qdisplay, object);
3752 if (!STRINGP (object))
3753 bufpos = CHARPOS (tpos);
3754 } while (NILP (spec)
3755 || !(rv = handle_display_spec (NULL, spec, object, Qnil, &tpos,
3756 bufpos, frame_window_p)));
3757 if (rv == 2)
3758 *disp_prop = 2;
3760 return CHARPOS (tpos);
3763 /* Return the character position of the end of the display string that
3764 started at CHARPOS. If there's no display string at CHARPOS,
3765 return -1. A display string is either an overlay with `display'
3766 property whose value is a string or a `display' text property whose
3767 value is a string. */
3768 ptrdiff_t
3769 compute_display_string_end (ptrdiff_t charpos, struct bidi_string_data *string)
3771 /* OBJECT = nil means current buffer. */
3772 Lisp_Object object =
3773 (string && STRINGP (string->lstring)) ? string->lstring : Qnil;
3774 Lisp_Object pos = make_number (charpos);
3775 ptrdiff_t eob =
3776 (STRINGP (object) || (string && string->s)) ? string->schars : ZV;
3778 if (charpos >= eob || (string->s && !STRINGP (object)))
3779 return eob;
3781 /* It could happen that the display property or overlay was removed
3782 since we found it in compute_display_string_pos above. One way
3783 this can happen is if JIT font-lock was called (through
3784 handle_fontified_prop), and jit-lock-functions remove text
3785 properties or overlays from the portion of buffer that includes
3786 CHARPOS. Muse mode is known to do that, for example. In this
3787 case, we return -1 to the caller, to signal that no display
3788 string is actually present at CHARPOS. See bidi_fetch_char for
3789 how this is handled.
3791 An alternative would be to never look for display properties past
3792 it->stop_charpos. But neither compute_display_string_pos nor
3793 bidi_fetch_char that calls it know or care where the next
3794 stop_charpos is. */
3795 if (NILP (Fget_char_property (pos, Qdisplay, object)))
3796 return -1;
3798 /* Look forward for the first character where the `display' property
3799 changes. */
3800 pos = Fnext_single_char_property_change (pos, Qdisplay, object, Qnil);
3802 return XFASTINT (pos);
3807 /***********************************************************************
3808 Fontification
3809 ***********************************************************************/
3811 /* Handle changes in the `fontified' property of the current buffer by
3812 calling hook functions from Qfontification_functions to fontify
3813 regions of text. */
3815 static enum prop_handled
3816 handle_fontified_prop (struct it *it)
3818 Lisp_Object prop, pos;
3819 enum prop_handled handled = HANDLED_NORMALLY;
3821 if (!NILP (Vmemory_full))
3822 return handled;
3824 /* Get the value of the `fontified' property at IT's current buffer
3825 position. (The `fontified' property doesn't have a special
3826 meaning in strings.) If the value is nil, call functions from
3827 Qfontification_functions. */
3828 if (!STRINGP (it->string)
3829 && it->s == NULL
3830 && !NILP (Vfontification_functions)
3831 && !NILP (Vrun_hooks)
3832 && (pos = make_number (IT_CHARPOS (*it)),
3833 prop = Fget_char_property (pos, Qfontified, Qnil),
3834 /* Ignore the special cased nil value always present at EOB since
3835 no amount of fontifying will be able to change it. */
3836 NILP (prop) && IT_CHARPOS (*it) < Z))
3838 ptrdiff_t count = SPECPDL_INDEX ();
3839 Lisp_Object val;
3840 struct buffer *obuf = current_buffer;
3841 ptrdiff_t begv = BEGV, zv = ZV;
3842 bool old_clip_changed = current_buffer->clip_changed;
3844 val = Vfontification_functions;
3845 specbind (Qfontification_functions, Qnil);
3847 eassert (it->end_charpos == ZV);
3849 if (!CONSP (val) || EQ (XCAR (val), Qlambda))
3850 safe_call1 (val, pos);
3851 else
3853 Lisp_Object fns, fn;
3855 fns = Qnil;
3857 for (; CONSP (val); val = XCDR (val))
3859 fn = XCAR (val);
3861 if (EQ (fn, Qt))
3863 /* A value of t indicates this hook has a local
3864 binding; it means to run the global binding too.
3865 In a global value, t should not occur. If it
3866 does, we must ignore it to avoid an endless
3867 loop. */
3868 for (fns = Fdefault_value (Qfontification_functions);
3869 CONSP (fns);
3870 fns = XCDR (fns))
3872 fn = XCAR (fns);
3873 if (!EQ (fn, Qt))
3874 safe_call1 (fn, pos);
3877 else
3878 safe_call1 (fn, pos);
3882 unbind_to (count, Qnil);
3884 /* Fontification functions routinely call `save-restriction'.
3885 Normally, this tags clip_changed, which can confuse redisplay
3886 (see discussion in Bug#6671). Since we don't perform any
3887 special handling of fontification changes in the case where
3888 `save-restriction' isn't called, there's no point doing so in
3889 this case either. So, if the buffer's restrictions are
3890 actually left unchanged, reset clip_changed. */
3891 if (obuf == current_buffer)
3893 if (begv == BEGV && zv == ZV)
3894 current_buffer->clip_changed = old_clip_changed;
3896 /* There isn't much we can reasonably do to protect against
3897 misbehaving fontification, but here's a fig leaf. */
3898 else if (BUFFER_LIVE_P (obuf))
3899 set_buffer_internal_1 (obuf);
3901 /* The fontification code may have added/removed text.
3902 It could do even a lot worse, but let's at least protect against
3903 the most obvious case where only the text past `pos' gets changed',
3904 as is/was done in grep.el where some escapes sequences are turned
3905 into face properties (bug#7876). */
3906 it->end_charpos = ZV;
3908 /* Return HANDLED_RECOMPUTE_PROPS only if function fontified
3909 something. This avoids an endless loop if they failed to
3910 fontify the text for which reason ever. */
3911 if (!NILP (Fget_char_property (pos, Qfontified, Qnil)))
3912 handled = HANDLED_RECOMPUTE_PROPS;
3915 return handled;
3920 /***********************************************************************
3921 Faces
3922 ***********************************************************************/
3924 /* Set up iterator IT from face properties at its current position.
3925 Called from handle_stop. */
3927 static enum prop_handled
3928 handle_face_prop (struct it *it)
3930 int new_face_id;
3931 ptrdiff_t next_stop;
3933 if (!STRINGP (it->string))
3935 new_face_id
3936 = face_at_buffer_position (it->w,
3937 IT_CHARPOS (*it),
3938 &next_stop,
3939 (IT_CHARPOS (*it)
3940 + TEXT_PROP_DISTANCE_LIMIT),
3941 false, it->base_face_id);
3943 /* Is this a start of a run of characters with box face?
3944 Caveat: this can be called for a freshly initialized
3945 iterator; face_id is -1 in this case. We know that the new
3946 face will not change until limit, i.e. if the new face has a
3947 box, all characters up to limit will have one. But, as
3948 usual, we don't know whether limit is really the end. */
3949 if (new_face_id != it->face_id)
3951 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3952 /* If it->face_id is -1, old_face below will be NULL, see
3953 the definition of FACE_FROM_ID_OR_NULL. This will happen
3954 if this is the initial call that gets the face. */
3955 struct face *old_face = FACE_FROM_ID_OR_NULL (it->f, it->face_id);
3957 /* If the value of face_id of the iterator is -1, we have to
3958 look in front of IT's position and see whether there is a
3959 face there that's different from new_face_id. */
3960 if (!old_face && IT_CHARPOS (*it) > BEG)
3962 int prev_face_id = face_before_it_pos (it);
3964 old_face = FACE_FROM_ID_OR_NULL (it->f, prev_face_id);
3967 /* If the new face has a box, but the old face does not,
3968 this is the start of a run of characters with box face,
3969 i.e. this character has a shadow on the left side. */
3970 it->start_of_box_run_p = (new_face->box != FACE_NO_BOX
3971 && (old_face == NULL || !old_face->box));
3972 it->face_box_p = new_face->box != FACE_NO_BOX;
3975 else
3977 int base_face_id;
3978 ptrdiff_t bufpos;
3979 int i;
3980 Lisp_Object from_overlay
3981 = (it->current.overlay_string_index >= 0
3982 ? it->string_overlays[it->current.overlay_string_index
3983 % OVERLAY_STRING_CHUNK_SIZE]
3984 : Qnil);
3986 /* See if we got to this string directly or indirectly from
3987 an overlay property. That includes the before-string or
3988 after-string of an overlay, strings in display properties
3989 provided by an overlay, their text properties, etc.
3991 FROM_OVERLAY is the overlay that brought us here, or nil if none. */
3992 if (! NILP (from_overlay))
3993 for (i = it->sp - 1; i >= 0; i--)
3995 if (it->stack[i].current.overlay_string_index >= 0)
3996 from_overlay
3997 = it->string_overlays[it->stack[i].current.overlay_string_index
3998 % OVERLAY_STRING_CHUNK_SIZE];
3999 else if (! NILP (it->stack[i].from_overlay))
4000 from_overlay = it->stack[i].from_overlay;
4002 if (!NILP (from_overlay))
4003 break;
4006 if (! NILP (from_overlay))
4008 bufpos = IT_CHARPOS (*it);
4009 /* For a string from an overlay, the base face depends
4010 only on text properties and ignores overlays. */
4011 base_face_id
4012 = face_for_overlay_string (it->w,
4013 IT_CHARPOS (*it),
4014 &next_stop,
4015 (IT_CHARPOS (*it)
4016 + TEXT_PROP_DISTANCE_LIMIT),
4017 false,
4018 from_overlay);
4020 else
4022 bufpos = 0;
4024 /* For strings from a `display' property, use the face at
4025 IT's current buffer position as the base face to merge
4026 with, so that overlay strings appear in the same face as
4027 surrounding text, unless they specify their own faces.
4028 For strings from wrap-prefix and line-prefix properties,
4029 use the default face, possibly remapped via
4030 Vface_remapping_alist. */
4031 /* Note that the fact that we use the face at _buffer_
4032 position means that a 'display' property on an overlay
4033 string will not inherit the face of that overlay string,
4034 but will instead revert to the face of buffer text
4035 covered by the overlay. This is visible, e.g., when the
4036 overlay specifies a box face, but neither the buffer nor
4037 the display string do. This sounds like a design bug,
4038 but Emacs always did that since v21.1, so changing that
4039 might be a big deal. */
4040 base_face_id = it->string_from_prefix_prop_p
4041 ? (!NILP (Vface_remapping_alist)
4042 ? lookup_basic_face (it->f, DEFAULT_FACE_ID)
4043 : DEFAULT_FACE_ID)
4044 : underlying_face_id (it);
4047 new_face_id = face_at_string_position (it->w,
4048 it->string,
4049 IT_STRING_CHARPOS (*it),
4050 bufpos,
4051 &next_stop,
4052 base_face_id, false);
4054 /* Is this a start of a run of characters with box? Caveat:
4055 this can be called for a freshly allocated iterator; face_id
4056 is -1 is this case. We know that the new face will not
4057 change until the next check pos, i.e. if the new face has a
4058 box, all characters up to that position will have a
4059 box. But, as usual, we don't know whether that position
4060 is really the end. */
4061 if (new_face_id != it->face_id)
4063 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
4064 struct face *old_face = FACE_FROM_ID_OR_NULL (it->f, it->face_id);
4066 /* If new face has a box but old face hasn't, this is the
4067 start of a run of characters with box, i.e. it has a
4068 shadow on the left side. */
4069 it->start_of_box_run_p
4070 = new_face->box && (old_face == NULL || !old_face->box);
4071 it->face_box_p = new_face->box != FACE_NO_BOX;
4075 it->face_id = new_face_id;
4076 return HANDLED_NORMALLY;
4080 /* Return the ID of the face ``underlying'' IT's current position,
4081 which is in a string. If the iterator is associated with a
4082 buffer, return the face at IT's current buffer position.
4083 Otherwise, use the iterator's base_face_id. */
4085 static int
4086 underlying_face_id (struct it *it)
4088 int face_id = it->base_face_id, i;
4090 eassert (STRINGP (it->string));
4092 for (i = it->sp - 1; i >= 0; --i)
4093 if (NILP (it->stack[i].string))
4094 face_id = it->stack[i].face_id;
4096 return face_id;
4100 /* Compute the face one character before or after the current position
4101 of IT, in the visual order. BEFORE_P means get the face
4102 in front (to the left in L2R paragraphs, to the right in R2L
4103 paragraphs) of IT's screen position. Value is the ID of the face. */
4105 static int
4106 face_before_or_after_it_pos (struct it *it, bool before_p)
4108 int face_id, limit;
4109 ptrdiff_t next_check_charpos;
4110 struct it it_copy;
4111 void *it_copy_data = NULL;
4113 eassert (it->s == NULL);
4115 if (STRINGP (it->string))
4117 ptrdiff_t bufpos, charpos;
4118 int base_face_id;
4120 /* No face change past the end of the string (for the case
4121 we are padding with spaces). No face change before the
4122 string start. */
4123 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string)
4124 || (IT_STRING_CHARPOS (*it) == 0 && before_p))
4125 return it->face_id;
4127 if (!it->bidi_p)
4129 /* Set charpos to the position before or after IT's current
4130 position, in the logical order, which in the non-bidi
4131 case is the same as the visual order. */
4132 if (before_p)
4133 charpos = IT_STRING_CHARPOS (*it) - 1;
4134 else if (it->what == IT_COMPOSITION)
4135 /* For composition, we must check the character after the
4136 composition. */
4137 charpos = IT_STRING_CHARPOS (*it) + it->cmp_it.nchars;
4138 else
4139 charpos = IT_STRING_CHARPOS (*it) + 1;
4141 else
4143 if (before_p)
4145 /* With bidi iteration, the character before the current
4146 in the visual order cannot be found by simple
4147 iteration, because "reverse" reordering is not
4148 supported. Instead, we need to start from the string
4149 beginning and go all the way to the current string
4150 position, remembering the previous position. */
4151 /* Ignore face changes before the first visible
4152 character on this display line. */
4153 if (it->current_x <= it->first_visible_x)
4154 return it->face_id;
4155 SAVE_IT (it_copy, *it, it_copy_data);
4156 IT_STRING_CHARPOS (it_copy) = 0;
4157 bidi_init_it (0, 0, FRAME_WINDOW_P (it_copy.f), &it_copy.bidi_it);
4161 charpos = IT_STRING_CHARPOS (it_copy);
4162 if (charpos >= SCHARS (it->string))
4163 break;
4164 bidi_move_to_visually_next (&it_copy.bidi_it);
4166 while (IT_STRING_CHARPOS (it_copy) != IT_STRING_CHARPOS (*it));
4168 RESTORE_IT (it, it, it_copy_data);
4170 else
4172 /* Set charpos to the string position of the character
4173 that comes after IT's current position in the visual
4174 order. */
4175 int n = (it->what == IT_COMPOSITION ? it->cmp_it.nchars : 1);
4177 it_copy = *it;
4178 while (n--)
4179 bidi_move_to_visually_next (&it_copy.bidi_it);
4181 charpos = it_copy.bidi_it.charpos;
4184 eassert (0 <= charpos && charpos <= SCHARS (it->string));
4186 if (it->current.overlay_string_index >= 0)
4187 bufpos = IT_CHARPOS (*it);
4188 else
4189 bufpos = 0;
4191 base_face_id = underlying_face_id (it);
4193 /* Get the face for ASCII, or unibyte. */
4194 face_id = face_at_string_position (it->w,
4195 it->string,
4196 charpos,
4197 bufpos,
4198 &next_check_charpos,
4199 base_face_id, false);
4201 /* Correct the face for charsets different from ASCII. Do it
4202 for the multibyte case only. The face returned above is
4203 suitable for unibyte text if IT->string is unibyte. */
4204 if (STRING_MULTIBYTE (it->string))
4206 struct text_pos pos1 = string_pos (charpos, it->string);
4207 const unsigned char *p = SDATA (it->string) + BYTEPOS (pos1);
4208 int c, len;
4209 struct face *face = FACE_FROM_ID (it->f, face_id);
4211 c = string_char_and_length (p, &len);
4212 face_id = FACE_FOR_CHAR (it->f, face, c, charpos, it->string);
4215 else
4217 struct text_pos pos;
4219 if ((IT_CHARPOS (*it) >= ZV && !before_p)
4220 || (IT_CHARPOS (*it) <= BEGV && before_p))
4221 return it->face_id;
4223 limit = IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT;
4224 pos = it->current.pos;
4226 if (!it->bidi_p)
4228 if (before_p)
4229 DEC_TEXT_POS (pos, it->multibyte_p);
4230 else
4232 if (it->what == IT_COMPOSITION)
4234 /* For composition, we must check the position after
4235 the composition. */
4236 pos.charpos += it->cmp_it.nchars;
4237 pos.bytepos += it->len;
4239 else
4240 INC_TEXT_POS (pos, it->multibyte_p);
4243 else
4245 if (before_p)
4247 int current_x;
4249 /* With bidi iteration, the character before the current
4250 in the visual order cannot be found by simple
4251 iteration, because "reverse" reordering is not
4252 supported. Instead, we need to use the move_it_*
4253 family of functions, and move to the previous
4254 character starting from the beginning of the visual
4255 line. */
4256 /* Ignore face changes before the first visible
4257 character on this display line. */
4258 if (it->current_x <= it->first_visible_x)
4259 return it->face_id;
4260 SAVE_IT (it_copy, *it, it_copy_data);
4261 /* Implementation note: Since move_it_in_display_line
4262 works in the iterator geometry, and thinks the first
4263 character is always the leftmost, even in R2L lines,
4264 we don't need to distinguish between the R2L and L2R
4265 cases here. */
4266 current_x = it_copy.current_x;
4267 move_it_vertically_backward (&it_copy, 0);
4268 move_it_in_display_line (&it_copy, ZV, current_x - 1, MOVE_TO_X);
4269 pos = it_copy.current.pos;
4270 RESTORE_IT (it, it, it_copy_data);
4272 else
4274 /* Set charpos to the buffer position of the character
4275 that comes after IT's current position in the visual
4276 order. */
4277 int n = (it->what == IT_COMPOSITION ? it->cmp_it.nchars : 1);
4279 it_copy = *it;
4280 while (n--)
4281 bidi_move_to_visually_next (&it_copy.bidi_it);
4283 SET_TEXT_POS (pos,
4284 it_copy.bidi_it.charpos, it_copy.bidi_it.bytepos);
4287 eassert (BEGV <= CHARPOS (pos) && CHARPOS (pos) <= ZV);
4289 /* Determine face for CHARSET_ASCII, or unibyte. */
4290 face_id = face_at_buffer_position (it->w,
4291 CHARPOS (pos),
4292 &next_check_charpos,
4293 limit, false, -1);
4295 /* Correct the face for charsets different from ASCII. Do it
4296 for the multibyte case only. The face returned above is
4297 suitable for unibyte text if current_buffer is unibyte. */
4298 if (it->multibyte_p)
4300 int c = FETCH_MULTIBYTE_CHAR (BYTEPOS (pos));
4301 struct face *face = FACE_FROM_ID (it->f, face_id);
4302 face_id = FACE_FOR_CHAR (it->f, face, c, CHARPOS (pos), Qnil);
4306 return face_id;
4311 /***********************************************************************
4312 Invisible text
4313 ***********************************************************************/
4315 /* Set up iterator IT from invisible properties at its current
4316 position. Called from handle_stop. */
4318 static enum prop_handled
4319 handle_invisible_prop (struct it *it)
4321 enum prop_handled handled = HANDLED_NORMALLY;
4322 int invis;
4323 Lisp_Object prop;
4325 if (STRINGP (it->string))
4327 Lisp_Object end_charpos, limit;
4329 /* Get the value of the invisible text property at the
4330 current position. Value will be nil if there is no such
4331 property. */
4332 end_charpos = make_number (IT_STRING_CHARPOS (*it));
4333 prop = Fget_text_property (end_charpos, Qinvisible, it->string);
4334 invis = TEXT_PROP_MEANS_INVISIBLE (prop);
4336 if (invis != 0 && IT_STRING_CHARPOS (*it) < it->end_charpos)
4338 /* Record whether we have to display an ellipsis for the
4339 invisible text. */
4340 bool display_ellipsis_p = (invis == 2);
4341 ptrdiff_t len, endpos;
4343 handled = HANDLED_RECOMPUTE_PROPS;
4345 /* Get the position at which the next visible text can be
4346 found in IT->string, if any. */
4347 endpos = len = SCHARS (it->string);
4348 XSETINT (limit, len);
4351 end_charpos
4352 = Fnext_single_property_change (end_charpos, Qinvisible,
4353 it->string, limit);
4354 /* Since LIMIT is always an integer, so should be the
4355 value returned by Fnext_single_property_change. */
4356 eassert (INTEGERP (end_charpos));
4357 if (INTEGERP (end_charpos))
4359 endpos = XFASTINT (end_charpos);
4360 prop = Fget_text_property (end_charpos, Qinvisible, it->string);
4361 invis = TEXT_PROP_MEANS_INVISIBLE (prop);
4362 if (invis == 2)
4363 display_ellipsis_p = true;
4365 else /* Should never happen; but if it does, exit the loop. */
4366 endpos = len;
4368 while (invis != 0 && endpos < len);
4370 if (display_ellipsis_p)
4371 it->ellipsis_p = true;
4373 if (endpos < len)
4375 /* Text at END_CHARPOS is visible. Move IT there. */
4376 struct text_pos old;
4377 ptrdiff_t oldpos;
4379 old = it->current.string_pos;
4380 oldpos = CHARPOS (old);
4381 if (it->bidi_p)
4383 if (it->bidi_it.first_elt
4384 && it->bidi_it.charpos < SCHARS (it->string))
4385 bidi_paragraph_init (it->paragraph_embedding,
4386 &it->bidi_it, true);
4387 /* Bidi-iterate out of the invisible text. */
4390 bidi_move_to_visually_next (&it->bidi_it);
4392 while (oldpos <= it->bidi_it.charpos
4393 && it->bidi_it.charpos < endpos
4394 && it->bidi_it.charpos < it->bidi_it.string.schars);
4396 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
4397 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
4398 if (IT_CHARPOS (*it) >= endpos)
4399 it->prev_stop = endpos;
4401 else
4403 IT_STRING_CHARPOS (*it) = endpos;
4404 compute_string_pos (&it->current.string_pos, old, it->string);
4407 else
4409 /* The rest of the string is invisible. If this is an
4410 overlay string, proceed with the next overlay string
4411 or whatever comes and return a character from there. */
4412 if (it->current.overlay_string_index >= 0
4413 && !display_ellipsis_p)
4415 next_overlay_string (it);
4416 /* Don't check for overlay strings when we just
4417 finished processing them. */
4418 handled = HANDLED_OVERLAY_STRING_CONSUMED;
4420 else
4422 IT_STRING_CHARPOS (*it) = SCHARS (it->string);
4423 IT_STRING_BYTEPOS (*it) = SBYTES (it->string);
4428 else
4430 ptrdiff_t newpos, next_stop, start_charpos, tem;
4431 Lisp_Object pos, overlay;
4433 /* First of all, is there invisible text at this position? */
4434 tem = start_charpos = IT_CHARPOS (*it);
4435 pos = make_number (tem);
4436 prop = get_char_property_and_overlay (pos, Qinvisible, it->window,
4437 &overlay);
4438 invis = TEXT_PROP_MEANS_INVISIBLE (prop);
4440 /* If we are on invisible text, skip over it. */
4441 if (invis != 0 && start_charpos < it->end_charpos)
4443 /* Record whether we have to display an ellipsis for the
4444 invisible text. */
4445 bool display_ellipsis_p = invis == 2;
4447 handled = HANDLED_RECOMPUTE_PROPS;
4449 /* Loop skipping over invisible text. The loop is left at
4450 ZV or with IT on the first char being visible again. */
4453 /* Try to skip some invisible text. Return value is the
4454 position reached which can be equal to where we start
4455 if there is nothing invisible there. This skips both
4456 over invisible text properties and overlays with
4457 invisible property. */
4458 newpos = skip_invisible (tem, &next_stop, ZV, it->window);
4460 /* If we skipped nothing at all we weren't at invisible
4461 text in the first place. If everything to the end of
4462 the buffer was skipped, end the loop. */
4463 if (newpos == tem || newpos >= ZV)
4464 invis = 0;
4465 else
4467 /* We skipped some characters but not necessarily
4468 all there are. Check if we ended up on visible
4469 text. Fget_char_property returns the property of
4470 the char before the given position, i.e. if we
4471 get invis = 0, this means that the char at
4472 newpos is visible. */
4473 pos = make_number (newpos);
4474 prop = Fget_char_property (pos, Qinvisible, it->window);
4475 invis = TEXT_PROP_MEANS_INVISIBLE (prop);
4478 /* If we ended up on invisible text, proceed to
4479 skip starting with next_stop. */
4480 if (invis != 0)
4481 tem = next_stop;
4483 /* If there are adjacent invisible texts, don't lose the
4484 second one's ellipsis. */
4485 if (invis == 2)
4486 display_ellipsis_p = true;
4488 while (invis != 0);
4490 /* The position newpos is now either ZV or on visible text. */
4491 if (it->bidi_p)
4493 ptrdiff_t bpos = CHAR_TO_BYTE (newpos);
4494 bool on_newline
4495 = bpos == ZV_BYTE || FETCH_BYTE (bpos) == '\n';
4496 bool after_newline
4497 = newpos <= BEGV || FETCH_BYTE (bpos - 1) == '\n';
4499 /* If the invisible text ends on a newline or on a
4500 character after a newline, we can avoid the costly,
4501 character by character, bidi iteration to NEWPOS, and
4502 instead simply reseat the iterator there. That's
4503 because all bidi reordering information is tossed at
4504 the newline. This is a big win for modes that hide
4505 complete lines, like Outline, Org, etc. */
4506 if (on_newline || after_newline)
4508 struct text_pos tpos;
4509 bidi_dir_t pdir = it->bidi_it.paragraph_dir;
4511 SET_TEXT_POS (tpos, newpos, bpos);
4512 reseat_1 (it, tpos, false);
4513 /* If we reseat on a newline/ZV, we need to prep the
4514 bidi iterator for advancing to the next character
4515 after the newline/EOB, keeping the current paragraph
4516 direction (so that PRODUCE_GLYPHS does TRT wrt
4517 prepending/appending glyphs to a glyph row). */
4518 if (on_newline)
4520 it->bidi_it.first_elt = false;
4521 it->bidi_it.paragraph_dir = pdir;
4522 it->bidi_it.ch = (bpos == ZV_BYTE) ? -1 : '\n';
4523 it->bidi_it.nchars = 1;
4524 it->bidi_it.ch_len = 1;
4527 else /* Must use the slow method. */
4529 /* With bidi iteration, the region of invisible text
4530 could start and/or end in the middle of a
4531 non-base embedding level. Therefore, we need to
4532 skip invisible text using the bidi iterator,
4533 starting at IT's current position, until we find
4534 ourselves outside of the invisible text.
4535 Skipping invisible text _after_ bidi iteration
4536 avoids affecting the visual order of the
4537 displayed text when invisible properties are
4538 added or removed. */
4539 if (it->bidi_it.first_elt && it->bidi_it.charpos < ZV)
4541 /* If we were `reseat'ed to a new paragraph,
4542 determine the paragraph base direction. We
4543 need to do it now because
4544 next_element_from_buffer may not have a
4545 chance to do it, if we are going to skip any
4546 text at the beginning, which resets the
4547 FIRST_ELT flag. */
4548 bidi_paragraph_init (it->paragraph_embedding,
4549 &it->bidi_it, true);
4553 bidi_move_to_visually_next (&it->bidi_it);
4555 while (it->stop_charpos <= it->bidi_it.charpos
4556 && it->bidi_it.charpos < newpos);
4557 IT_CHARPOS (*it) = it->bidi_it.charpos;
4558 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
4559 /* If we overstepped NEWPOS, record its position in
4560 the iterator, so that we skip invisible text if
4561 later the bidi iteration lands us in the
4562 invisible region again. */
4563 if (IT_CHARPOS (*it) >= newpos)
4564 it->prev_stop = newpos;
4567 else
4569 IT_CHARPOS (*it) = newpos;
4570 IT_BYTEPOS (*it) = CHAR_TO_BYTE (newpos);
4573 if (display_ellipsis_p)
4575 /* Make sure that the glyphs of the ellipsis will get
4576 correct `charpos' values. If we would not update
4577 it->position here, the glyphs would belong to the
4578 last visible character _before_ the invisible
4579 text, which confuses `set_cursor_from_row'.
4581 We use the last invisible position instead of the
4582 first because this way the cursor is always drawn on
4583 the first "." of the ellipsis, whenever PT is inside
4584 the invisible text. Otherwise the cursor would be
4585 placed _after_ the ellipsis when the point is after the
4586 first invisible character. */
4587 if (!STRINGP (it->object))
4589 it->position.charpos = newpos - 1;
4590 it->position.bytepos = CHAR_TO_BYTE (it->position.charpos);
4594 /* If there are before-strings at the start of invisible
4595 text, and the text is invisible because of a text
4596 property, arrange to show before-strings because 20.x did
4597 it that way. (If the text is invisible because of an
4598 overlay property instead of a text property, this is
4599 already handled in the overlay code.) */
4600 if (NILP (overlay)
4601 && get_overlay_strings (it, it->stop_charpos))
4603 handled = HANDLED_RECOMPUTE_PROPS;
4604 if (it->sp > 0)
4606 it->stack[it->sp - 1].display_ellipsis_p = display_ellipsis_p;
4607 /* The call to get_overlay_strings above recomputes
4608 it->stop_charpos, but it only considers changes
4609 in properties and overlays beyond iterator's
4610 current position. This causes us to miss changes
4611 that happen exactly where the invisible property
4612 ended. So we play it safe here and force the
4613 iterator to check for potential stop positions
4614 immediately after the invisible text. Note that
4615 if get_overlay_strings returns true, it
4616 normally also pushed the iterator stack, so we
4617 need to update the stop position in the slot
4618 below the current one. */
4619 it->stack[it->sp - 1].stop_charpos
4620 = CHARPOS (it->stack[it->sp - 1].current.pos);
4623 else if (display_ellipsis_p)
4625 it->ellipsis_p = true;
4626 /* Let the ellipsis display before
4627 considering any properties of the following char.
4628 Fixes jasonr@gnu.org 01 Oct 07 bug. */
4629 handled = HANDLED_RETURN;
4634 return handled;
4638 /* Make iterator IT return `...' next.
4639 Replaces LEN characters from buffer. */
4641 static void
4642 setup_for_ellipsis (struct it *it, int len)
4644 /* Use the display table definition for `...'. Invalid glyphs
4645 will be handled by the method returning elements from dpvec. */
4646 if (it->dp && VECTORP (DISP_INVIS_VECTOR (it->dp)))
4648 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
4649 it->dpvec = v->contents;
4650 it->dpend = v->contents + v->header.size;
4652 else
4654 /* Default `...'. */
4655 it->dpvec = default_invis_vector;
4656 it->dpend = default_invis_vector + 3;
4659 it->dpvec_char_len = len;
4660 it->current.dpvec_index = 0;
4661 it->dpvec_face_id = -1;
4663 /* Use IT->saved_face_id for the ellipsis, so that it has the same
4664 face as the preceding text. IT->saved_face_id was set in
4665 handle_stop to the face of the preceding character, and will be
4666 different from IT->face_id only if the invisible text skipped in
4667 handle_invisible_prop has some non-default face on its first
4668 character. We thus ignore the face of the invisible text when we
4669 display the ellipsis. IT's face is restored in set_iterator_to_next. */
4670 if (it->saved_face_id >= 0)
4671 it->face_id = it->saved_face_id;
4673 /* If the ellipsis represents buffer text, it means we advanced in
4674 the buffer, so we should no longer ignore overlay strings. */
4675 if (it->method == GET_FROM_BUFFER)
4676 it->ignore_overlay_strings_at_pos_p = false;
4678 it->method = GET_FROM_DISPLAY_VECTOR;
4679 it->ellipsis_p = true;
4684 /***********************************************************************
4685 'display' property
4686 ***********************************************************************/
4688 /* Set up iterator IT from `display' property at its current position.
4689 Called from handle_stop.
4690 We return HANDLED_RETURN if some part of the display property
4691 overrides the display of the buffer text itself.
4692 Otherwise we return HANDLED_NORMALLY. */
4694 static enum prop_handled
4695 handle_display_prop (struct it *it)
4697 Lisp_Object propval, object, overlay;
4698 struct text_pos *position;
4699 ptrdiff_t bufpos;
4700 /* Nonzero if some property replaces the display of the text itself. */
4701 int display_replaced = 0;
4703 if (STRINGP (it->string))
4705 object = it->string;
4706 position = &it->current.string_pos;
4707 bufpos = CHARPOS (it->current.pos);
4709 else
4711 XSETWINDOW (object, it->w);
4712 position = &it->current.pos;
4713 bufpos = CHARPOS (*position);
4716 /* Reset those iterator values set from display property values. */
4717 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
4718 it->space_width = Qnil;
4719 it->font_height = Qnil;
4720 it->voffset = 0;
4722 /* We don't support recursive `display' properties, i.e. string
4723 values that have a string `display' property, that have a string
4724 `display' property etc. */
4725 if (!it->string_from_display_prop_p)
4726 it->area = TEXT_AREA;
4728 propval = get_char_property_and_overlay (make_number (position->charpos),
4729 Qdisplay, object, &overlay);
4730 if (NILP (propval))
4731 return HANDLED_NORMALLY;
4732 /* Now OVERLAY is the overlay that gave us this property, or nil
4733 if it was a text property. */
4735 if (!STRINGP (it->string))
4736 object = it->w->contents;
4738 display_replaced = handle_display_spec (it, propval, object, overlay,
4739 position, bufpos,
4740 FRAME_WINDOW_P (it->f));
4741 return display_replaced != 0 ? HANDLED_RETURN : HANDLED_NORMALLY;
4744 /* Subroutine of handle_display_prop. Returns non-zero if the display
4745 specification in SPEC is a replacing specification, i.e. it would
4746 replace the text covered by `display' property with something else,
4747 such as an image or a display string. If SPEC includes any kind or
4748 `(space ...) specification, the value is 2; this is used by
4749 compute_display_string_pos, which see.
4751 See handle_single_display_spec for documentation of arguments.
4752 FRAME_WINDOW_P is true if the window being redisplayed is on a
4753 GUI frame; this argument is used only if IT is NULL, see below.
4755 IT can be NULL, if this is called by the bidi reordering code
4756 through compute_display_string_pos, which see. In that case, this
4757 function only examines SPEC, but does not otherwise "handle" it, in
4758 the sense that it doesn't set up members of IT from the display
4759 spec. */
4760 static int
4761 handle_display_spec (struct it *it, Lisp_Object spec, Lisp_Object object,
4762 Lisp_Object overlay, struct text_pos *position,
4763 ptrdiff_t bufpos, bool frame_window_p)
4765 int replacing = 0;
4766 bool enable_eval = true;
4768 /* Support (disable-eval PROP) which is used by enriched.el. */
4769 if (CONSP (spec) && EQ (XCAR (spec), Qdisable_eval))
4771 enable_eval = false;
4772 spec = XCAR (XCDR (spec));
4775 if (CONSP (spec)
4776 /* Simple specifications. */
4777 && !EQ (XCAR (spec), Qimage)
4778 #ifdef HAVE_XWIDGETS
4779 && !EQ (XCAR (spec), Qxwidget)
4780 #endif
4781 && !EQ (XCAR (spec), Qspace)
4782 && !EQ (XCAR (spec), Qwhen)
4783 && !EQ (XCAR (spec), Qslice)
4784 && !EQ (XCAR (spec), Qspace_width)
4785 && !EQ (XCAR (spec), Qheight)
4786 && !EQ (XCAR (spec), Qraise)
4787 /* Marginal area specifications. */
4788 && !(CONSP (XCAR (spec)) && EQ (XCAR (XCAR (spec)), Qmargin))
4789 && !EQ (XCAR (spec), Qleft_fringe)
4790 && !EQ (XCAR (spec), Qright_fringe)
4791 && !NILP (XCAR (spec)))
4793 for (; CONSP (spec); spec = XCDR (spec))
4795 int rv = handle_single_display_spec (it, XCAR (spec), object,
4796 overlay, position, bufpos,
4797 replacing, frame_window_p,
4798 enable_eval);
4799 if (rv != 0)
4801 replacing = rv;
4802 /* If some text in a string is replaced, `position' no
4803 longer points to the position of `object'. */
4804 if (!it || STRINGP (object))
4805 break;
4809 else if (VECTORP (spec))
4811 ptrdiff_t i;
4812 for (i = 0; i < ASIZE (spec); ++i)
4814 int rv = handle_single_display_spec (it, AREF (spec, i), object,
4815 overlay, position, bufpos,
4816 replacing, frame_window_p,
4817 enable_eval);
4818 if (rv != 0)
4820 replacing = rv;
4821 /* If some text in a string is replaced, `position' no
4822 longer points to the position of `object'. */
4823 if (!it || STRINGP (object))
4824 break;
4828 else
4829 replacing = handle_single_display_spec (it, spec, object, overlay, position,
4830 bufpos, 0, frame_window_p,
4831 enable_eval);
4832 return replacing;
4835 /* Value is the position of the end of the `display' property starting
4836 at START_POS in OBJECT. */
4838 static struct text_pos
4839 display_prop_end (struct it *it, Lisp_Object object, struct text_pos start_pos)
4841 Lisp_Object end;
4842 struct text_pos end_pos;
4844 end = Fnext_single_char_property_change (make_number (CHARPOS (start_pos)),
4845 Qdisplay, object, Qnil);
4846 CHARPOS (end_pos) = XFASTINT (end);
4847 if (STRINGP (object))
4848 compute_string_pos (&end_pos, start_pos, it->string);
4849 else
4850 BYTEPOS (end_pos) = CHAR_TO_BYTE (XFASTINT (end));
4852 return end_pos;
4856 /* Set up IT from a single `display' property specification SPEC. OBJECT
4857 is the object in which the `display' property was found. *POSITION
4858 is the position in OBJECT at which the `display' property was found.
4859 BUFPOS is the buffer position of OBJECT (different from POSITION if
4860 OBJECT is not a buffer). DISPLAY_REPLACED non-zero means that we
4861 previously saw a display specification which already replaced text
4862 display with something else, for example an image; we ignore such
4863 properties after the first one has been processed.
4865 OVERLAY is the overlay this `display' property came from,
4866 or nil if it was a text property.
4868 If SPEC is a `space' or `image' specification, and in some other
4869 cases too, set *POSITION to the position where the `display'
4870 property ends.
4872 If IT is NULL, only examine the property specification in SPEC, but
4873 don't set up IT. In that case, FRAME_WINDOW_P means SPEC
4874 is intended to be displayed in a window on a GUI frame.
4876 Enable evaluation of Lisp forms only if ENABLE_EVAL_P is true.
4878 Value is non-zero if something was found which replaces the display
4879 of buffer or string text. */
4881 static int
4882 handle_single_display_spec (struct it *it, Lisp_Object spec, Lisp_Object object,
4883 Lisp_Object overlay, struct text_pos *position,
4884 ptrdiff_t bufpos, int display_replaced,
4885 bool frame_window_p, bool enable_eval_p)
4887 Lisp_Object form;
4888 Lisp_Object location, value;
4889 struct text_pos start_pos = *position;
4891 /* If SPEC is a list of the form `(when FORM . VALUE)', evaluate FORM.
4892 If the result is non-nil, use VALUE instead of SPEC. */
4893 form = Qt;
4894 if (CONSP (spec) && EQ (XCAR (spec), Qwhen))
4896 spec = XCDR (spec);
4897 if (!CONSP (spec))
4898 return 0;
4899 form = XCAR (spec);
4900 spec = XCDR (spec);
4903 if (!NILP (form) && !EQ (form, Qt) && !enable_eval_p)
4904 form = Qnil;
4905 if (!NILP (form) && !EQ (form, Qt))
4907 ptrdiff_t count = SPECPDL_INDEX ();
4909 /* Bind `object' to the object having the `display' property, a
4910 buffer or string. Bind `position' to the position in the
4911 object where the property was found, and `buffer-position'
4912 to the current position in the buffer. */
4914 if (NILP (object))
4915 XSETBUFFER (object, current_buffer);
4916 specbind (Qobject, object);
4917 specbind (Qposition, make_number (CHARPOS (*position)));
4918 specbind (Qbuffer_position, make_number (bufpos));
4919 form = safe_eval (form);
4920 unbind_to (count, Qnil);
4923 if (NILP (form))
4924 return 0;
4926 /* Handle `(height HEIGHT)' specifications. */
4927 if (CONSP (spec)
4928 && EQ (XCAR (spec), Qheight)
4929 && CONSP (XCDR (spec)))
4931 if (it)
4933 if (!FRAME_WINDOW_P (it->f))
4934 return 0;
4936 it->font_height = XCAR (XCDR (spec));
4937 if (!NILP (it->font_height))
4939 int new_height = -1;
4941 if (CONSP (it->font_height)
4942 && (EQ (XCAR (it->font_height), Qplus)
4943 || EQ (XCAR (it->font_height), Qminus))
4944 && CONSP (XCDR (it->font_height))
4945 && RANGED_INTEGERP (0, XCAR (XCDR (it->font_height)), INT_MAX))
4947 /* `(+ N)' or `(- N)' where N is an integer. */
4948 int steps = XINT (XCAR (XCDR (it->font_height)));
4949 if (EQ (XCAR (it->font_height), Qplus))
4950 steps = - steps;
4951 it->face_id = smaller_face (it->f, it->face_id, steps);
4953 else if (FUNCTIONP (it->font_height) && enable_eval_p)
4955 /* Call function with current height as argument.
4956 Value is the new height. */
4957 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4958 Lisp_Object height;
4959 height = safe_call1 (it->font_height,
4960 face->lface[LFACE_HEIGHT_INDEX]);
4961 if (NUMBERP (height))
4962 new_height = XFLOATINT (height);
4964 else if (NUMBERP (it->font_height))
4966 /* Value is a multiple of the canonical char height. */
4967 struct face *f;
4969 f = FACE_FROM_ID (it->f,
4970 lookup_basic_face (it->f, DEFAULT_FACE_ID));
4971 new_height = (XFLOATINT (it->font_height)
4972 * XINT (f->lface[LFACE_HEIGHT_INDEX]));
4974 else if (enable_eval_p)
4976 /* Evaluate IT->font_height with `height' bound to the
4977 current specified height to get the new height. */
4978 ptrdiff_t count = SPECPDL_INDEX ();
4979 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4981 specbind (Qheight, face->lface[LFACE_HEIGHT_INDEX]);
4982 value = safe_eval (it->font_height);
4983 unbind_to (count, Qnil);
4985 if (NUMBERP (value))
4986 new_height = XFLOATINT (value);
4989 if (new_height > 0)
4990 it->face_id = face_with_height (it->f, it->face_id, new_height);
4994 return 0;
4997 /* Handle `(space-width WIDTH)'. */
4998 if (CONSP (spec)
4999 && EQ (XCAR (spec), Qspace_width)
5000 && CONSP (XCDR (spec)))
5002 if (it)
5004 if (!FRAME_WINDOW_P (it->f))
5005 return 0;
5007 value = XCAR (XCDR (spec));
5008 if (NUMBERP (value) && XFLOATINT (value) > 0)
5009 it->space_width = value;
5012 return 0;
5015 /* Handle `(slice X Y WIDTH HEIGHT)'. */
5016 if (CONSP (spec)
5017 && EQ (XCAR (spec), Qslice))
5019 Lisp_Object tem;
5021 if (it)
5023 if (!FRAME_WINDOW_P (it->f))
5024 return 0;
5026 if (tem = XCDR (spec), CONSP (tem))
5028 it->slice.x = XCAR (tem);
5029 if (tem = XCDR (tem), CONSP (tem))
5031 it->slice.y = XCAR (tem);
5032 if (tem = XCDR (tem), CONSP (tem))
5034 it->slice.width = XCAR (tem);
5035 if (tem = XCDR (tem), CONSP (tem))
5036 it->slice.height = XCAR (tem);
5042 return 0;
5045 /* Handle `(raise FACTOR)'. */
5046 if (CONSP (spec)
5047 && EQ (XCAR (spec), Qraise)
5048 && CONSP (XCDR (spec)))
5050 if (it)
5052 if (!FRAME_WINDOW_P (it->f))
5053 return 0;
5055 #ifdef HAVE_WINDOW_SYSTEM
5056 value = XCAR (XCDR (spec));
5057 if (NUMBERP (value))
5059 struct face *face = FACE_FROM_ID (it->f, it->face_id);
5060 it->voffset = - (XFLOATINT (value)
5061 * (normal_char_height (face->font, -1)));
5063 #endif /* HAVE_WINDOW_SYSTEM */
5066 return 0;
5069 /* Don't handle the other kinds of display specifications
5070 inside a string that we got from a `display' property. */
5071 if (it && it->string_from_display_prop_p)
5072 return 0;
5074 /* Characters having this form of property are not displayed, so
5075 we have to find the end of the property. */
5076 if (it)
5078 start_pos = *position;
5079 *position = display_prop_end (it, object, start_pos);
5080 /* If the display property comes from an overlay, don't consider
5081 any potential stop_charpos values before the end of that
5082 overlay. Since display_prop_end will happily find another
5083 'display' property coming from some other overlay or text
5084 property on buffer positions before this overlay's end, we
5085 need to ignore them, or else we risk displaying this
5086 overlay's display string/image twice. */
5087 if (!NILP (overlay))
5089 ptrdiff_t ovendpos = OVERLAY_POSITION (OVERLAY_END (overlay));
5091 /* Some borderline-sane Lisp might call us with the current
5092 buffer narrowed so that overlay-end is outside the
5093 POINT_MIN..POINT_MAX region, which will then cause
5094 various assertion violations and crashes down the road,
5095 starting with pop_it when it will attempt to use POSITION
5096 set below. Prevent that. */
5097 ovendpos = clip_to_bounds (BEGV, ovendpos, ZV);
5099 if (ovendpos > CHARPOS (*position))
5100 SET_TEXT_POS (*position, ovendpos, CHAR_TO_BYTE (ovendpos));
5103 value = Qnil;
5105 /* Stop the scan at that end position--we assume that all
5106 text properties change there. */
5107 if (it)
5108 it->stop_charpos = position->charpos;
5110 /* Handle `(left-fringe BITMAP [FACE])'
5111 and `(right-fringe BITMAP [FACE])'. */
5112 if (CONSP (spec)
5113 && (EQ (XCAR (spec), Qleft_fringe)
5114 || EQ (XCAR (spec), Qright_fringe))
5115 && CONSP (XCDR (spec)))
5117 if (it)
5119 if (!FRAME_WINDOW_P (it->f))
5120 /* If we return here, POSITION has been advanced
5121 across the text with this property. */
5123 /* Synchronize the bidi iterator with POSITION. This is
5124 needed because we are not going to push the iterator
5125 on behalf of this display property, so there will be
5126 no pop_it call to do this synchronization for us. */
5127 if (it->bidi_p)
5129 it->position = *position;
5130 iterate_out_of_display_property (it);
5131 *position = it->position;
5133 return 1;
5136 else if (!frame_window_p)
5137 return 1;
5139 #ifdef HAVE_WINDOW_SYSTEM
5140 value = XCAR (XCDR (spec));
5141 int fringe_bitmap = SYMBOLP (value) ? lookup_fringe_bitmap (value) : 0;
5142 if (! fringe_bitmap)
5143 /* If we return here, POSITION has been advanced
5144 across the text with this property. */
5146 if (it && it->bidi_p)
5148 it->position = *position;
5149 iterate_out_of_display_property (it);
5150 *position = it->position;
5152 return 1;
5155 if (it)
5157 int face_id = lookup_basic_face (it->f, DEFAULT_FACE_ID);
5159 if (CONSP (XCDR (XCDR (spec))))
5161 Lisp_Object face_name = XCAR (XCDR (XCDR (spec)));
5162 int face_id2 = lookup_derived_face (it->f, face_name,
5163 FRINGE_FACE_ID, false);
5164 if (face_id2 >= 0)
5165 face_id = face_id2;
5168 /* Save current settings of IT so that we can restore them
5169 when we are finished with the glyph property value. */
5170 push_it (it, position);
5172 it->area = TEXT_AREA;
5173 it->what = IT_IMAGE;
5174 it->image_id = -1; /* no image */
5175 it->position = start_pos;
5176 it->object = NILP (object) ? it->w->contents : object;
5177 it->method = GET_FROM_IMAGE;
5178 it->from_overlay = Qnil;
5179 it->face_id = face_id;
5180 it->from_disp_prop_p = true;
5182 /* Say that we haven't consumed the characters with
5183 `display' property yet. The call to pop_it in
5184 set_iterator_to_next will clean this up. */
5185 *position = start_pos;
5187 if (EQ (XCAR (spec), Qleft_fringe))
5189 it->left_user_fringe_bitmap = fringe_bitmap;
5190 it->left_user_fringe_face_id = face_id;
5192 else
5194 it->right_user_fringe_bitmap = fringe_bitmap;
5195 it->right_user_fringe_face_id = face_id;
5198 #endif /* HAVE_WINDOW_SYSTEM */
5199 return 1;
5202 /* Prepare to handle `((margin left-margin) ...)',
5203 `((margin right-margin) ...)' and `((margin nil) ...)'
5204 prefixes for display specifications. */
5205 location = Qunbound;
5206 if (CONSP (spec) && CONSP (XCAR (spec)))
5208 Lisp_Object tem;
5210 value = XCDR (spec);
5211 if (CONSP (value))
5212 value = XCAR (value);
5214 tem = XCAR (spec);
5215 if (EQ (XCAR (tem), Qmargin)
5216 && (tem = XCDR (tem),
5217 tem = CONSP (tem) ? XCAR (tem) : Qnil,
5218 (NILP (tem)
5219 || EQ (tem, Qleft_margin)
5220 || EQ (tem, Qright_margin))))
5221 location = tem;
5224 if (EQ (location, Qunbound))
5226 location = Qnil;
5227 value = spec;
5230 /* After this point, VALUE is the property after any
5231 margin prefix has been stripped. It must be a string,
5232 an image specification, or `(space ...)'.
5234 LOCATION specifies where to display: `left-margin',
5235 `right-margin' or nil. */
5237 bool valid_p = (STRINGP (value)
5238 #ifdef HAVE_WINDOW_SYSTEM
5239 || ((it ? FRAME_WINDOW_P (it->f) : frame_window_p)
5240 && valid_image_p (value))
5241 #endif /* not HAVE_WINDOW_SYSTEM */
5242 || (CONSP (value) && EQ (XCAR (value), Qspace))
5243 || ((it ? FRAME_WINDOW_P (it->f) : frame_window_p)
5244 && valid_xwidget_spec_p (value)));
5246 if (valid_p && display_replaced == 0)
5248 int retval = 1;
5250 if (!it)
5252 /* Callers need to know whether the display spec is any kind
5253 of `(space ...)' spec that is about to affect text-area
5254 display. */
5255 if (CONSP (value) && EQ (XCAR (value), Qspace) && NILP (location))
5256 retval = 2;
5257 return retval;
5260 /* Save current settings of IT so that we can restore them
5261 when we are finished with the glyph property value. */
5262 push_it (it, position);
5263 it->from_overlay = overlay;
5264 it->from_disp_prop_p = true;
5266 if (NILP (location))
5267 it->area = TEXT_AREA;
5268 else if (EQ (location, Qleft_margin))
5269 it->area = LEFT_MARGIN_AREA;
5270 else
5271 it->area = RIGHT_MARGIN_AREA;
5273 if (STRINGP (value))
5275 it->string = value;
5276 it->multibyte_p = STRING_MULTIBYTE (it->string);
5277 it->current.overlay_string_index = -1;
5278 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
5279 it->end_charpos = it->string_nchars = SCHARS (it->string);
5280 it->method = GET_FROM_STRING;
5281 it->stop_charpos = 0;
5282 it->prev_stop = 0;
5283 it->base_level_stop = 0;
5284 it->string_from_display_prop_p = true;
5285 it->cmp_it.id = -1;
5286 /* Say that we haven't consumed the characters with
5287 `display' property yet. The call to pop_it in
5288 set_iterator_to_next will clean this up. */
5289 if (BUFFERP (object))
5290 *position = start_pos;
5292 /* Force paragraph direction to be that of the parent
5293 object. If the parent object's paragraph direction is
5294 not yet determined, default to L2R. */
5295 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
5296 it->paragraph_embedding = it->bidi_it.paragraph_dir;
5297 else
5298 it->paragraph_embedding = L2R;
5300 /* Set up the bidi iterator for this display string. */
5301 if (it->bidi_p)
5303 it->bidi_it.string.lstring = it->string;
5304 it->bidi_it.string.s = NULL;
5305 it->bidi_it.string.schars = it->end_charpos;
5306 it->bidi_it.string.bufpos = bufpos;
5307 it->bidi_it.string.from_disp_str = true;
5308 it->bidi_it.string.unibyte = !it->multibyte_p;
5309 it->bidi_it.w = it->w;
5310 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
5313 else if (CONSP (value) && EQ (XCAR (value), Qspace))
5315 it->method = GET_FROM_STRETCH;
5316 it->object = value;
5317 *position = it->position = start_pos;
5318 retval = 1 + (it->area == TEXT_AREA);
5320 else if (valid_xwidget_spec_p (value))
5322 it->what = IT_XWIDGET;
5323 it->method = GET_FROM_XWIDGET;
5324 it->position = start_pos;
5325 it->object = NILP (object) ? it->w->contents : object;
5326 *position = start_pos;
5327 it->xwidget = lookup_xwidget (value);
5329 #ifdef HAVE_WINDOW_SYSTEM
5330 else
5332 it->what = IT_IMAGE;
5333 it->image_id = lookup_image (it->f, value);
5334 it->position = start_pos;
5335 it->object = NILP (object) ? it->w->contents : object;
5336 it->method = GET_FROM_IMAGE;
5338 /* Say that we haven't consumed the characters with
5339 `display' property yet. The call to pop_it in
5340 set_iterator_to_next will clean this up. */
5341 *position = start_pos;
5343 #endif /* HAVE_WINDOW_SYSTEM */
5345 return retval;
5348 /* Invalid property or property not supported. Restore
5349 POSITION to what it was before. */
5350 *position = start_pos;
5351 return 0;
5354 /* Check if PROP is a display property value whose text should be
5355 treated as intangible. OVERLAY is the overlay from which PROP
5356 came, or nil if it came from a text property. CHARPOS and BYTEPOS
5357 specify the buffer position covered by PROP. */
5359 bool
5360 display_prop_intangible_p (Lisp_Object prop, Lisp_Object overlay,
5361 ptrdiff_t charpos, ptrdiff_t bytepos)
5363 bool frame_window_p = FRAME_WINDOW_P (XFRAME (selected_frame));
5364 struct text_pos position;
5366 SET_TEXT_POS (position, charpos, bytepos);
5367 return (handle_display_spec (NULL, prop, Qnil, overlay,
5368 &position, charpos, frame_window_p)
5369 != 0);
5373 /* Return true if PROP is a display sub-property value containing STRING.
5375 Implementation note: this and the following function are really
5376 special cases of handle_display_spec and
5377 handle_single_display_spec, and should ideally use the same code.
5378 Until they do, these two pairs must be consistent and must be
5379 modified in sync. */
5381 static bool
5382 single_display_spec_string_p (Lisp_Object prop, Lisp_Object string)
5384 if (EQ (string, prop))
5385 return true;
5387 /* Skip over `when FORM'. */
5388 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
5390 prop = XCDR (prop);
5391 if (!CONSP (prop))
5392 return false;
5393 /* Actually, the condition following `when' should be eval'ed,
5394 like handle_single_display_spec does, and we should return
5395 false if it evaluates to nil. However, this function is
5396 called only when the buffer was already displayed and some
5397 glyph in the glyph matrix was found to come from a display
5398 string. Therefore, the condition was already evaluated, and
5399 the result was non-nil, otherwise the display string wouldn't
5400 have been displayed and we would have never been called for
5401 this property. Thus, we can skip the evaluation and assume
5402 its result is non-nil. */
5403 prop = XCDR (prop);
5406 if (CONSP (prop))
5407 /* Skip over `margin LOCATION'. */
5408 if (EQ (XCAR (prop), Qmargin))
5410 prop = XCDR (prop);
5411 if (!CONSP (prop))
5412 return false;
5414 prop = XCDR (prop);
5415 if (!CONSP (prop))
5416 return false;
5419 return EQ (prop, string) || (CONSP (prop) && EQ (XCAR (prop), string));
5423 /* Return true if STRING appears in the `display' property PROP. */
5425 static bool
5426 display_prop_string_p (Lisp_Object prop, Lisp_Object string)
5428 if (CONSP (prop)
5429 && !EQ (XCAR (prop), Qwhen)
5430 && !(CONSP (XCAR (prop)) && EQ (Qmargin, XCAR (XCAR (prop)))))
5432 /* A list of sub-properties. */
5433 while (CONSP (prop))
5435 if (single_display_spec_string_p (XCAR (prop), string))
5436 return true;
5437 prop = XCDR (prop);
5440 else if (VECTORP (prop))
5442 /* A vector of sub-properties. */
5443 ptrdiff_t i;
5444 for (i = 0; i < ASIZE (prop); ++i)
5445 if (single_display_spec_string_p (AREF (prop, i), string))
5446 return true;
5448 else
5449 return single_display_spec_string_p (prop, string);
5451 return false;
5454 /* Look for STRING in overlays and text properties in the current
5455 buffer, between character positions FROM and TO (excluding TO).
5456 BACK_P means look back (in this case, TO is supposed to be
5457 less than FROM).
5458 Value is the first character position where STRING was found, or
5459 zero if it wasn't found before hitting TO.
5461 This function may only use code that doesn't eval because it is
5462 called asynchronously from note_mouse_highlight. */
5464 static ptrdiff_t
5465 string_buffer_position_lim (Lisp_Object string,
5466 ptrdiff_t from, ptrdiff_t to, bool back_p)
5468 Lisp_Object limit, prop, pos;
5469 bool found = false;
5471 pos = make_number (max (from, BEGV));
5473 if (!back_p) /* looking forward */
5475 limit = make_number (min (to, ZV));
5476 while (!found && !EQ (pos, limit))
5478 prop = Fget_char_property (pos, Qdisplay, Qnil);
5479 if (!NILP (prop) && display_prop_string_p (prop, string))
5480 found = true;
5481 else
5482 pos = Fnext_single_char_property_change (pos, Qdisplay, Qnil,
5483 limit);
5486 else /* looking back */
5488 limit = make_number (max (to, BEGV));
5489 while (!found && !EQ (pos, limit))
5491 prop = Fget_char_property (pos, Qdisplay, Qnil);
5492 if (!NILP (prop) && display_prop_string_p (prop, string))
5493 found = true;
5494 else
5495 pos = Fprevious_single_char_property_change (pos, Qdisplay, Qnil,
5496 limit);
5500 return found ? XINT (pos) : 0;
5503 /* Determine which buffer position in current buffer STRING comes from.
5504 AROUND_CHARPOS is an approximate position where it could come from.
5505 Value is the buffer position or 0 if it couldn't be determined.
5507 This function is necessary because we don't record buffer positions
5508 in glyphs generated from strings (to keep struct glyph small).
5509 This function may only use code that doesn't eval because it is
5510 called asynchronously from note_mouse_highlight. */
5512 static ptrdiff_t
5513 string_buffer_position (Lisp_Object string, ptrdiff_t around_charpos)
5515 const int MAX_DISTANCE = 1000;
5516 ptrdiff_t found = string_buffer_position_lim (string, around_charpos,
5517 around_charpos + MAX_DISTANCE,
5518 false);
5520 if (!found)
5521 found = string_buffer_position_lim (string, around_charpos,
5522 around_charpos - MAX_DISTANCE, true);
5523 return found;
5528 /***********************************************************************
5529 `composition' property
5530 ***********************************************************************/
5532 /* Set up iterator IT from `composition' property at its current
5533 position. Called from handle_stop. */
5535 static enum prop_handled
5536 handle_composition_prop (struct it *it)
5538 Lisp_Object prop, string;
5539 ptrdiff_t pos, pos_byte, start, end;
5541 if (STRINGP (it->string))
5543 unsigned char *s;
5545 pos = IT_STRING_CHARPOS (*it);
5546 pos_byte = IT_STRING_BYTEPOS (*it);
5547 string = it->string;
5548 s = SDATA (string) + pos_byte;
5549 it->c = STRING_CHAR (s);
5551 else
5553 pos = IT_CHARPOS (*it);
5554 pos_byte = IT_BYTEPOS (*it);
5555 string = Qnil;
5556 it->c = FETCH_CHAR (pos_byte);
5559 /* If there's a valid composition and point is not inside of the
5560 composition (in the case that the composition is from the current
5561 buffer), draw a glyph composed from the composition components. */
5562 if (find_composition (pos, -1, &start, &end, &prop, string)
5563 && composition_valid_p (start, end, prop)
5564 && (STRINGP (it->string) || (PT <= start || PT >= end)))
5566 if (start < pos)
5567 /* As we can't handle this situation (perhaps font-lock added
5568 a new composition), we just return here hoping that next
5569 redisplay will detect this composition much earlier. */
5570 return HANDLED_NORMALLY;
5571 if (start != pos)
5573 if (STRINGP (it->string))
5574 pos_byte = string_char_to_byte (it->string, start);
5575 else
5576 pos_byte = CHAR_TO_BYTE (start);
5578 it->cmp_it.id = get_composition_id (start, pos_byte, end - start,
5579 prop, string);
5581 if (it->cmp_it.id >= 0)
5583 it->cmp_it.ch = -1;
5584 it->cmp_it.nchars = COMPOSITION_LENGTH (prop);
5585 it->cmp_it.nglyphs = -1;
5589 return HANDLED_NORMALLY;
5594 /***********************************************************************
5595 Overlay strings
5596 ***********************************************************************/
5598 /* The following structure is used to record overlay strings for
5599 later sorting in load_overlay_strings. */
5601 struct overlay_entry
5603 Lisp_Object overlay;
5604 Lisp_Object string;
5605 EMACS_INT priority;
5606 bool after_string_p;
5610 /* Set up iterator IT from overlay strings at its current position.
5611 Called from handle_stop. */
5613 static enum prop_handled
5614 handle_overlay_change (struct it *it)
5616 if (!STRINGP (it->string) && get_overlay_strings (it, 0))
5617 return HANDLED_RECOMPUTE_PROPS;
5618 else
5619 return HANDLED_NORMALLY;
5623 /* Set up the next overlay string for delivery by IT, if there is an
5624 overlay string to deliver. Called by set_iterator_to_next when the
5625 end of the current overlay string is reached. If there are more
5626 overlay strings to display, IT->string and
5627 IT->current.overlay_string_index are set appropriately here.
5628 Otherwise IT->string is set to nil. */
5630 static void
5631 next_overlay_string (struct it *it)
5633 ++it->current.overlay_string_index;
5634 if (it->current.overlay_string_index == it->n_overlay_strings)
5636 /* No more overlay strings. Restore IT's settings to what
5637 they were before overlay strings were processed, and
5638 continue to deliver from current_buffer. */
5640 it->ellipsis_p = it->stack[it->sp - 1].display_ellipsis_p;
5641 pop_it (it);
5642 eassert (it->sp > 0
5643 || (NILP (it->string)
5644 && it->method == GET_FROM_BUFFER
5645 && it->stop_charpos >= BEGV
5646 && it->stop_charpos <= it->end_charpos));
5647 it->current.overlay_string_index = -1;
5648 it->n_overlay_strings = 0;
5649 /* If there's an empty display string on the stack, pop the
5650 stack, to resync the bidi iterator with IT's position. Such
5651 empty strings are pushed onto the stack in
5652 get_overlay_strings_1. */
5653 if (it->sp > 0 && STRINGP (it->string) && !SCHARS (it->string))
5654 pop_it (it);
5656 /* Since we've exhausted overlay strings at this buffer
5657 position, set the flag to ignore overlays until we move to
5658 another position. (The flag will be reset in
5659 next_element_from_buffer.) But don't do that if the overlay
5660 strings were loaded at position other than the current one,
5661 which could happen if we called pop_it above, or if the
5662 overlay strings were loaded by handle_invisible_prop at the
5663 beginning of invisible text. */
5664 if (it->overlay_strings_charpos == IT_CHARPOS (*it))
5665 it->ignore_overlay_strings_at_pos_p = true;
5667 /* If we're at the end of the buffer, record that we have
5668 processed the overlay strings there already, so that
5669 next_element_from_buffer doesn't try it again. */
5670 if (NILP (it->string)
5671 && IT_CHARPOS (*it) >= it->end_charpos
5672 && it->overlay_strings_charpos >= it->end_charpos)
5673 it->overlay_strings_at_end_processed_p = true;
5674 /* Note: we reset overlay_strings_charpos only here, to make
5675 sure the just-processed overlays were indeed at EOB.
5676 Otherwise, overlays on text with invisible text property,
5677 which are processed with IT's position past the invisible
5678 text, might fool us into thinking the overlays at EOB were
5679 already processed (linum-mode can cause this, for
5680 example). */
5681 it->overlay_strings_charpos = -1;
5683 else
5685 /* There are more overlay strings to process. If
5686 IT->current.overlay_string_index has advanced to a position
5687 where we must load IT->overlay_strings with more strings, do
5688 it. We must load at the IT->overlay_strings_charpos where
5689 IT->n_overlay_strings was originally computed; when invisible
5690 text is present, this might not be IT_CHARPOS (Bug#7016). */
5691 int i = it->current.overlay_string_index % OVERLAY_STRING_CHUNK_SIZE;
5693 if (it->current.overlay_string_index && i == 0)
5694 load_overlay_strings (it, it->overlay_strings_charpos);
5696 /* Initialize IT to deliver display elements from the overlay
5697 string. */
5698 it->string = it->overlay_strings[i];
5699 it->multibyte_p = STRING_MULTIBYTE (it->string);
5700 SET_TEXT_POS (it->current.string_pos, 0, 0);
5701 it->method = GET_FROM_STRING;
5702 it->stop_charpos = 0;
5703 it->end_charpos = SCHARS (it->string);
5704 if (it->cmp_it.stop_pos >= 0)
5705 it->cmp_it.stop_pos = 0;
5706 it->prev_stop = 0;
5707 it->base_level_stop = 0;
5709 /* Set up the bidi iterator for this overlay string. */
5710 if (it->bidi_p)
5712 it->bidi_it.string.lstring = it->string;
5713 it->bidi_it.string.s = NULL;
5714 it->bidi_it.string.schars = SCHARS (it->string);
5715 it->bidi_it.string.bufpos = it->overlay_strings_charpos;
5716 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
5717 it->bidi_it.string.unibyte = !it->multibyte_p;
5718 it->bidi_it.w = it->w;
5719 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
5723 CHECK_IT (it);
5727 /* Compare two overlay_entry structures E1 and E2. Used as a
5728 comparison function for qsort in load_overlay_strings. Overlay
5729 strings for the same position are sorted so that
5731 1. All after-strings come in front of before-strings, except
5732 when they come from the same overlay.
5734 2. Within after-strings, strings are sorted so that overlay strings
5735 from overlays with higher priorities come first.
5737 2. Within before-strings, strings are sorted so that overlay
5738 strings from overlays with higher priorities come last.
5740 Value is analogous to strcmp. */
5743 static int
5744 compare_overlay_entries (const void *e1, const void *e2)
5746 struct overlay_entry const *entry1 = e1;
5747 struct overlay_entry const *entry2 = e2;
5748 int result;
5750 if (entry1->after_string_p != entry2->after_string_p)
5752 /* Let after-strings appear in front of before-strings if
5753 they come from different overlays. */
5754 if (EQ (entry1->overlay, entry2->overlay))
5755 result = entry1->after_string_p ? 1 : -1;
5756 else
5757 result = entry1->after_string_p ? -1 : 1;
5759 else if (entry1->priority != entry2->priority)
5761 if (entry1->after_string_p)
5762 /* After-strings sorted in order of decreasing priority. */
5763 result = entry2->priority < entry1->priority ? -1 : 1;
5764 else
5765 /* Before-strings sorted in order of increasing priority. */
5766 result = entry1->priority < entry2->priority ? -1 : 1;
5768 else
5769 result = 0;
5771 return result;
5775 /* Load the vector IT->overlay_strings with overlay strings from IT's
5776 current buffer position, or from CHARPOS if that is > 0. Set
5777 IT->n_overlays to the total number of overlay strings found.
5779 Overlay strings are processed OVERLAY_STRING_CHUNK_SIZE strings at
5780 a time. On entry into load_overlay_strings,
5781 IT->current.overlay_string_index gives the number of overlay
5782 strings that have already been loaded by previous calls to this
5783 function.
5785 IT->add_overlay_start contains an additional overlay start
5786 position to consider for taking overlay strings from, if non-zero.
5787 This position comes into play when the overlay has an `invisible'
5788 property, and both before and after-strings. When we've skipped to
5789 the end of the overlay, because of its `invisible' property, we
5790 nevertheless want its before-string to appear.
5791 IT->add_overlay_start will contain the overlay start position
5792 in this case.
5794 Overlay strings are sorted so that after-string strings come in
5795 front of before-string strings. Within before and after-strings,
5796 strings are sorted by overlay priority. See also function
5797 compare_overlay_entries. */
5799 static void
5800 load_overlay_strings (struct it *it, ptrdiff_t charpos)
5802 Lisp_Object overlay, window, str, invisible;
5803 struct Lisp_Overlay *ov;
5804 ptrdiff_t start, end;
5805 ptrdiff_t n = 0, i, j;
5806 int invis;
5807 struct overlay_entry entriesbuf[20];
5808 ptrdiff_t size = ARRAYELTS (entriesbuf);
5809 struct overlay_entry *entries = entriesbuf;
5810 USE_SAFE_ALLOCA;
5812 if (charpos <= 0)
5813 charpos = IT_CHARPOS (*it);
5815 /* Append the overlay string STRING of overlay OVERLAY to vector
5816 `entries' which has size `size' and currently contains `n'
5817 elements. AFTER_P means STRING is an after-string of
5818 OVERLAY. */
5819 #define RECORD_OVERLAY_STRING(OVERLAY, STRING, AFTER_P) \
5820 do \
5822 Lisp_Object priority; \
5824 if (n == size) \
5826 struct overlay_entry *old = entries; \
5827 SAFE_NALLOCA (entries, 2, size); \
5828 memcpy (entries, old, size * sizeof *entries); \
5829 size *= 2; \
5832 entries[n].string = (STRING); \
5833 entries[n].overlay = (OVERLAY); \
5834 priority = Foverlay_get ((OVERLAY), Qpriority); \
5835 entries[n].priority = INTEGERP (priority) ? XINT (priority) : 0; \
5836 entries[n].after_string_p = (AFTER_P); \
5837 ++n; \
5839 while (false)
5841 /* Process overlay before the overlay center. */
5842 for (ov = current_buffer->overlays_before; ov; ov = ov->next)
5844 XSETMISC (overlay, ov);
5845 eassert (OVERLAYP (overlay));
5846 start = OVERLAY_POSITION (OVERLAY_START (overlay));
5847 end = OVERLAY_POSITION (OVERLAY_END (overlay));
5849 if (end < charpos)
5850 break;
5852 /* Skip this overlay if it doesn't start or end at IT's current
5853 position. */
5854 if (end != charpos && start != charpos)
5855 continue;
5857 /* Skip this overlay if it doesn't apply to IT->w. */
5858 window = Foverlay_get (overlay, Qwindow);
5859 if (WINDOWP (window) && XWINDOW (window) != it->w)
5860 continue;
5862 /* If the text ``under'' the overlay is invisible, both before-
5863 and after-strings from this overlay are visible; start and
5864 end position are indistinguishable. */
5865 invisible = Foverlay_get (overlay, Qinvisible);
5866 invis = TEXT_PROP_MEANS_INVISIBLE (invisible);
5868 /* If overlay has a non-empty before-string, record it. */
5869 if ((start == charpos || (end == charpos && invis != 0))
5870 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
5871 && SCHARS (str))
5872 RECORD_OVERLAY_STRING (overlay, str, false);
5874 /* If overlay has a non-empty after-string, record it. */
5875 if ((end == charpos || (start == charpos && invis != 0))
5876 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
5877 && SCHARS (str))
5878 RECORD_OVERLAY_STRING (overlay, str, true);
5881 /* Process overlays after the overlay center. */
5882 for (ov = current_buffer->overlays_after; ov; ov = ov->next)
5884 XSETMISC (overlay, ov);
5885 eassert (OVERLAYP (overlay));
5886 start = OVERLAY_POSITION (OVERLAY_START (overlay));
5887 end = OVERLAY_POSITION (OVERLAY_END (overlay));
5889 if (start > charpos)
5890 break;
5892 /* Skip this overlay if it doesn't start or end at IT's current
5893 position. */
5894 if (end != charpos && start != charpos)
5895 continue;
5897 /* Skip this overlay if it doesn't apply to IT->w. */
5898 window = Foverlay_get (overlay, Qwindow);
5899 if (WINDOWP (window) && XWINDOW (window) != it->w)
5900 continue;
5902 /* If the text ``under'' the overlay is invisible, it has a zero
5903 dimension, and both before- and after-strings apply. */
5904 invisible = Foverlay_get (overlay, Qinvisible);
5905 invis = TEXT_PROP_MEANS_INVISIBLE (invisible);
5907 /* If overlay has a non-empty before-string, record it. */
5908 if ((start == charpos || (end == charpos && invis != 0))
5909 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
5910 && SCHARS (str))
5911 RECORD_OVERLAY_STRING (overlay, str, false);
5913 /* If overlay has a non-empty after-string, record it. */
5914 if ((end == charpos || (start == charpos && invis != 0))
5915 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
5916 && SCHARS (str))
5917 RECORD_OVERLAY_STRING (overlay, str, true);
5920 #undef RECORD_OVERLAY_STRING
5922 /* Sort entries. */
5923 if (n > 1)
5924 qsort (entries, n, sizeof *entries, compare_overlay_entries);
5926 /* Record number of overlay strings, and where we computed it. */
5927 it->n_overlay_strings = n;
5928 it->overlay_strings_charpos = charpos;
5930 /* IT->current.overlay_string_index is the number of overlay strings
5931 that have already been consumed by IT. Copy some of the
5932 remaining overlay strings to IT->overlay_strings. */
5933 i = 0;
5934 j = it->current.overlay_string_index;
5935 while (i < OVERLAY_STRING_CHUNK_SIZE && j < n)
5937 it->overlay_strings[i] = entries[j].string;
5938 it->string_overlays[i++] = entries[j++].overlay;
5941 CHECK_IT (it);
5942 SAFE_FREE ();
5946 /* Get the first chunk of overlay strings at IT's current buffer
5947 position, or at CHARPOS if that is > 0. Value is true if at
5948 least one overlay string was found. */
5950 static bool
5951 get_overlay_strings_1 (struct it *it, ptrdiff_t charpos, bool compute_stop_p)
5953 /* Get the first OVERLAY_STRING_CHUNK_SIZE overlay strings to
5954 process. This fills IT->overlay_strings with strings, and sets
5955 IT->n_overlay_strings to the total number of strings to process.
5956 IT->pos.overlay_string_index has to be set temporarily to zero
5957 because load_overlay_strings needs this; it must be set to -1
5958 when no overlay strings are found because a zero value would
5959 indicate a position in the first overlay string. */
5960 it->current.overlay_string_index = 0;
5961 load_overlay_strings (it, charpos);
5963 /* If we found overlay strings, set up IT to deliver display
5964 elements from the first one. Otherwise set up IT to deliver
5965 from current_buffer. */
5966 if (it->n_overlay_strings)
5968 /* Make sure we know settings in current_buffer, so that we can
5969 restore meaningful values when we're done with the overlay
5970 strings. */
5971 if (compute_stop_p)
5972 compute_stop_pos (it);
5973 eassert (it->face_id >= 0);
5975 /* Save IT's settings. They are restored after all overlay
5976 strings have been processed. */
5977 eassert (!compute_stop_p || it->sp == 0);
5979 /* When called from handle_stop, there might be an empty display
5980 string loaded. In that case, don't bother saving it. But
5981 don't use this optimization with the bidi iterator, since we
5982 need the corresponding pop_it call to resync the bidi
5983 iterator's position with IT's position, after we are done
5984 with the overlay strings. (The corresponding call to pop_it
5985 in case of an empty display string is in
5986 next_overlay_string.) */
5987 if (!(!it->bidi_p
5988 && STRINGP (it->string) && !SCHARS (it->string)))
5989 push_it (it, NULL);
5991 /* Set up IT to deliver display elements from the first overlay
5992 string. */
5993 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
5994 it->string = it->overlay_strings[0];
5995 it->from_overlay = Qnil;
5996 it->stop_charpos = 0;
5997 eassert (STRINGP (it->string));
5998 it->end_charpos = SCHARS (it->string);
5999 it->prev_stop = 0;
6000 it->base_level_stop = 0;
6001 it->multibyte_p = STRING_MULTIBYTE (it->string);
6002 it->method = GET_FROM_STRING;
6003 it->from_disp_prop_p = 0;
6004 it->cmp_it.id = -1;
6006 /* Force paragraph direction to be that of the parent
6007 buffer. */
6008 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
6009 it->paragraph_embedding = it->bidi_it.paragraph_dir;
6010 else
6011 it->paragraph_embedding = L2R;
6013 /* Set up the bidi iterator for this overlay string. */
6014 if (it->bidi_p)
6016 ptrdiff_t pos = (charpos > 0 ? charpos : IT_CHARPOS (*it));
6018 it->bidi_it.string.lstring = it->string;
6019 it->bidi_it.string.s = NULL;
6020 it->bidi_it.string.schars = SCHARS (it->string);
6021 it->bidi_it.string.bufpos = pos;
6022 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
6023 it->bidi_it.string.unibyte = !it->multibyte_p;
6024 it->bidi_it.w = it->w;
6025 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
6027 return true;
6030 it->current.overlay_string_index = -1;
6031 return false;
6034 static bool
6035 get_overlay_strings (struct it *it, ptrdiff_t charpos)
6037 it->string = Qnil;
6038 it->method = GET_FROM_BUFFER;
6040 get_overlay_strings_1 (it, charpos, true);
6042 CHECK_IT (it);
6044 /* Value is true if we found at least one overlay string. */
6045 return STRINGP (it->string);
6050 /***********************************************************************
6051 Saving and restoring state
6052 ***********************************************************************/
6054 /* Save current settings of IT on IT->stack. Called, for example,
6055 before setting up IT for an overlay string, to be able to restore
6056 IT's settings to what they were after the overlay string has been
6057 processed. If POSITION is non-NULL, it is the position to save on
6058 the stack instead of IT->position. */
6060 static void
6061 push_it (struct it *it, struct text_pos *position)
6063 struct iterator_stack_entry *p;
6065 eassert (it->sp < IT_STACK_SIZE);
6066 p = it->stack + it->sp;
6068 p->stop_charpos = it->stop_charpos;
6069 p->prev_stop = it->prev_stop;
6070 p->base_level_stop = it->base_level_stop;
6071 p->cmp_it = it->cmp_it;
6072 eassert (it->face_id >= 0);
6073 p->face_id = it->face_id;
6074 p->string = it->string;
6075 p->method = it->method;
6076 p->from_overlay = it->from_overlay;
6077 switch (p->method)
6079 case GET_FROM_IMAGE:
6080 p->u.image.object = it->object;
6081 p->u.image.image_id = it->image_id;
6082 p->u.image.slice = it->slice;
6083 break;
6084 case GET_FROM_STRETCH:
6085 p->u.stretch.object = it->object;
6086 break;
6087 case GET_FROM_XWIDGET:
6088 p->u.xwidget.object = it->object;
6089 break;
6090 case GET_FROM_BUFFER:
6091 case GET_FROM_DISPLAY_VECTOR:
6092 case GET_FROM_STRING:
6093 case GET_FROM_C_STRING:
6094 break;
6095 default:
6096 emacs_abort ();
6098 p->position = position ? *position : it->position;
6099 p->current = it->current;
6100 p->end_charpos = it->end_charpos;
6101 p->string_nchars = it->string_nchars;
6102 p->area = it->area;
6103 p->multibyte_p = it->multibyte_p;
6104 p->avoid_cursor_p = it->avoid_cursor_p;
6105 p->space_width = it->space_width;
6106 p->font_height = it->font_height;
6107 p->voffset = it->voffset;
6108 p->string_from_display_prop_p = it->string_from_display_prop_p;
6109 p->string_from_prefix_prop_p = it->string_from_prefix_prop_p;
6110 p->display_ellipsis_p = false;
6111 p->line_wrap = it->line_wrap;
6112 p->bidi_p = it->bidi_p;
6113 p->paragraph_embedding = it->paragraph_embedding;
6114 p->from_disp_prop_p = it->from_disp_prop_p;
6115 ++it->sp;
6117 /* Save the state of the bidi iterator as well. */
6118 if (it->bidi_p)
6119 bidi_push_it (&it->bidi_it);
6122 static void
6123 iterate_out_of_display_property (struct it *it)
6125 bool buffer_p = !STRINGP (it->string);
6126 ptrdiff_t eob = (buffer_p ? ZV : it->end_charpos);
6127 ptrdiff_t bob = (buffer_p ? BEGV : 0);
6129 eassert (eob >= CHARPOS (it->position) && CHARPOS (it->position) >= bob);
6131 /* Maybe initialize paragraph direction. If we are at the beginning
6132 of a new paragraph, next_element_from_buffer may not have a
6133 chance to do that. */
6134 if (it->bidi_it.first_elt && it->bidi_it.charpos < eob)
6135 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, true);
6136 /* prev_stop can be zero, so check against BEGV as well. */
6137 while (it->bidi_it.charpos >= bob
6138 && it->prev_stop <= it->bidi_it.charpos
6139 && it->bidi_it.charpos < CHARPOS (it->position)
6140 && it->bidi_it.charpos < eob)
6141 bidi_move_to_visually_next (&it->bidi_it);
6142 /* Record the stop_pos we just crossed, for when we cross it
6143 back, maybe. */
6144 if (it->bidi_it.charpos > CHARPOS (it->position))
6145 it->prev_stop = CHARPOS (it->position);
6146 /* If we ended up not where pop_it put us, resync IT's
6147 positional members with the bidi iterator. */
6148 if (it->bidi_it.charpos != CHARPOS (it->position))
6149 SET_TEXT_POS (it->position, it->bidi_it.charpos, it->bidi_it.bytepos);
6150 if (buffer_p)
6151 it->current.pos = it->position;
6152 else
6153 it->current.string_pos = it->position;
6156 /* Restore IT's settings from IT->stack. Called, for example, when no
6157 more overlay strings must be processed, and we return to delivering
6158 display elements from a buffer, or when the end of a string from a
6159 `display' property is reached and we return to delivering display
6160 elements from an overlay string, or from a buffer. */
6162 static void
6163 pop_it (struct it *it)
6165 struct iterator_stack_entry *p;
6166 bool from_display_prop = it->from_disp_prop_p;
6167 ptrdiff_t prev_pos = IT_CHARPOS (*it);
6169 eassert (it->sp > 0);
6170 --it->sp;
6171 p = it->stack + it->sp;
6172 it->stop_charpos = p->stop_charpos;
6173 it->prev_stop = p->prev_stop;
6174 it->base_level_stop = p->base_level_stop;
6175 it->cmp_it = p->cmp_it;
6176 it->face_id = p->face_id;
6177 it->current = p->current;
6178 it->position = p->position;
6179 it->string = p->string;
6180 it->from_overlay = p->from_overlay;
6181 if (NILP (it->string))
6182 SET_TEXT_POS (it->current.string_pos, -1, -1);
6183 it->method = p->method;
6184 switch (it->method)
6186 case GET_FROM_IMAGE:
6187 it->image_id = p->u.image.image_id;
6188 it->object = p->u.image.object;
6189 it->slice = p->u.image.slice;
6190 break;
6191 case GET_FROM_XWIDGET:
6192 it->object = p->u.xwidget.object;
6193 break;
6194 case GET_FROM_STRETCH:
6195 it->object = p->u.stretch.object;
6196 break;
6197 case GET_FROM_BUFFER:
6198 it->object = it->w->contents;
6199 break;
6200 case GET_FROM_STRING:
6202 struct face *face = FACE_FROM_ID_OR_NULL (it->f, it->face_id);
6204 /* Restore the face_box_p flag, since it could have been
6205 overwritten by the face of the object that we just finished
6206 displaying. */
6207 if (face)
6208 it->face_box_p = face->box != FACE_NO_BOX;
6209 it->object = it->string;
6211 break;
6212 case GET_FROM_DISPLAY_VECTOR:
6213 if (it->s)
6214 it->method = GET_FROM_C_STRING;
6215 else if (STRINGP (it->string))
6216 it->method = GET_FROM_STRING;
6217 else
6219 it->method = GET_FROM_BUFFER;
6220 it->object = it->w->contents;
6222 break;
6223 case GET_FROM_C_STRING:
6224 break;
6225 default:
6226 emacs_abort ();
6228 it->end_charpos = p->end_charpos;
6229 it->string_nchars = p->string_nchars;
6230 it->area = p->area;
6231 it->multibyte_p = p->multibyte_p;
6232 it->avoid_cursor_p = p->avoid_cursor_p;
6233 it->space_width = p->space_width;
6234 it->font_height = p->font_height;
6235 it->voffset = p->voffset;
6236 it->string_from_display_prop_p = p->string_from_display_prop_p;
6237 it->string_from_prefix_prop_p = p->string_from_prefix_prop_p;
6238 it->line_wrap = p->line_wrap;
6239 it->bidi_p = p->bidi_p;
6240 it->paragraph_embedding = p->paragraph_embedding;
6241 it->from_disp_prop_p = p->from_disp_prop_p;
6242 if (it->bidi_p)
6244 bidi_pop_it (&it->bidi_it);
6245 /* Bidi-iterate until we get out of the portion of text, if any,
6246 covered by a `display' text property or by an overlay with
6247 `display' property. (We cannot just jump there, because the
6248 internal coherency of the bidi iterator state can not be
6249 preserved across such jumps.) We also must determine the
6250 paragraph base direction if the overlay we just processed is
6251 at the beginning of a new paragraph. */
6252 if (from_display_prop
6253 && (it->method == GET_FROM_BUFFER || it->method == GET_FROM_STRING))
6254 iterate_out_of_display_property (it);
6256 eassert ((BUFFERP (it->object)
6257 && IT_CHARPOS (*it) == it->bidi_it.charpos
6258 && IT_BYTEPOS (*it) == it->bidi_it.bytepos)
6259 || (STRINGP (it->object)
6260 && IT_STRING_CHARPOS (*it) == it->bidi_it.charpos
6261 && IT_STRING_BYTEPOS (*it) == it->bidi_it.bytepos)
6262 || (CONSP (it->object) && it->method == GET_FROM_STRETCH));
6264 /* If we move the iterator over text covered by a display property
6265 to a new buffer position, any info about previously seen overlays
6266 is no longer valid. */
6267 if (from_display_prop && it->sp == 0 && CHARPOS (it->position) != prev_pos)
6268 it->ignore_overlay_strings_at_pos_p = false;
6273 /***********************************************************************
6274 Moving over lines
6275 ***********************************************************************/
6277 /* Set IT's current position to the previous line start. */
6279 static void
6280 back_to_previous_line_start (struct it *it)
6282 ptrdiff_t cp = IT_CHARPOS (*it), bp = IT_BYTEPOS (*it);
6284 DEC_BOTH (cp, bp);
6285 IT_CHARPOS (*it) = find_newline_no_quit (cp, bp, -1, &IT_BYTEPOS (*it));
6289 /* Move IT to the next line start.
6291 Value is true if a newline was found. Set *SKIPPED_P to true if
6292 we skipped over part of the text (as opposed to moving the iterator
6293 continuously over the text). Otherwise, don't change the value
6294 of *SKIPPED_P.
6296 If BIDI_IT_PREV is non-NULL, store into it the state of the bidi
6297 iterator on the newline, if it was found.
6299 Newlines may come from buffer text, overlay strings, or strings
6300 displayed via the `display' property. That's the reason we can't
6301 simply use find_newline_no_quit.
6303 Note that this function may not skip over invisible text that is so
6304 because of text properties and immediately follows a newline. If
6305 it would, function reseat_at_next_visible_line_start, when called
6306 from set_iterator_to_next, would effectively make invisible
6307 characters following a newline part of the wrong glyph row, which
6308 leads to wrong cursor motion. */
6310 static bool
6311 forward_to_next_line_start (struct it *it, bool *skipped_p,
6312 struct bidi_it *bidi_it_prev)
6314 ptrdiff_t old_selective;
6315 bool newline_found_p = false;
6316 int n;
6317 const int MAX_NEWLINE_DISTANCE = 500;
6319 /* If already on a newline, just consume it to avoid unintended
6320 skipping over invisible text below. */
6321 if (it->what == IT_CHARACTER
6322 && it->c == '\n'
6323 && CHARPOS (it->position) == IT_CHARPOS (*it))
6325 if (it->bidi_p && bidi_it_prev)
6326 *bidi_it_prev = it->bidi_it;
6327 set_iterator_to_next (it, false);
6328 it->c = 0;
6329 return true;
6332 /* Don't handle selective display in the following. It's (a)
6333 unnecessary because it's done by the caller, and (b) leads to an
6334 infinite recursion because next_element_from_ellipsis indirectly
6335 calls this function. */
6336 old_selective = it->selective;
6337 it->selective = 0;
6339 /* Scan for a newline within MAX_NEWLINE_DISTANCE display elements
6340 from buffer text. */
6341 for (n = 0;
6342 !newline_found_p && n < MAX_NEWLINE_DISTANCE;
6343 n += !STRINGP (it->string))
6345 if (!get_next_display_element (it))
6346 return false;
6347 newline_found_p = it->what == IT_CHARACTER && it->c == '\n';
6348 if (newline_found_p && it->bidi_p && bidi_it_prev)
6349 *bidi_it_prev = it->bidi_it;
6350 set_iterator_to_next (it, false);
6353 /* If we didn't find a newline near enough, see if we can use a
6354 short-cut. */
6355 if (!newline_found_p)
6357 ptrdiff_t bytepos, start = IT_CHARPOS (*it);
6358 ptrdiff_t limit = find_newline_no_quit (start, IT_BYTEPOS (*it),
6359 1, &bytepos);
6360 Lisp_Object pos;
6362 eassert (!STRINGP (it->string));
6364 /* If there isn't any `display' property in sight, and no
6365 overlays, we can just use the position of the newline in
6366 buffer text. */
6367 if (it->stop_charpos >= limit
6368 || ((pos = Fnext_single_property_change (make_number (start),
6369 Qdisplay, Qnil,
6370 make_number (limit)),
6371 NILP (pos))
6372 && next_overlay_change (start) == ZV))
6374 if (!it->bidi_p)
6376 IT_CHARPOS (*it) = limit;
6377 IT_BYTEPOS (*it) = bytepos;
6379 else
6381 struct bidi_it bprev;
6383 /* Help bidi.c avoid expensive searches for display
6384 properties and overlays, by telling it that there are
6385 none up to `limit'. */
6386 if (it->bidi_it.disp_pos < limit)
6388 it->bidi_it.disp_pos = limit;
6389 it->bidi_it.disp_prop = 0;
6391 do {
6392 bprev = it->bidi_it;
6393 bidi_move_to_visually_next (&it->bidi_it);
6394 } while (it->bidi_it.charpos != limit);
6395 IT_CHARPOS (*it) = limit;
6396 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6397 if (bidi_it_prev)
6398 *bidi_it_prev = bprev;
6400 *skipped_p = newline_found_p = true;
6402 else
6404 while (!newline_found_p)
6406 if (!get_next_display_element (it))
6407 break;
6408 newline_found_p = ITERATOR_AT_END_OF_LINE_P (it);
6409 if (newline_found_p && it->bidi_p && bidi_it_prev)
6410 *bidi_it_prev = it->bidi_it;
6411 set_iterator_to_next (it, false);
6416 it->selective = old_selective;
6417 return newline_found_p;
6421 /* Set IT's current position to the previous visible line start. Skip
6422 invisible text that is so either due to text properties or due to
6423 selective display. Caution: this does not change IT->current_x and
6424 IT->hpos. */
6426 static void
6427 back_to_previous_visible_line_start (struct it *it)
6429 while (IT_CHARPOS (*it) > BEGV)
6431 back_to_previous_line_start (it);
6433 if (IT_CHARPOS (*it) <= BEGV)
6434 break;
6436 /* If selective > 0, then lines indented more than its value are
6437 invisible. */
6438 if (it->selective > 0
6439 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
6440 it->selective))
6441 continue;
6443 /* Check the newline before point for invisibility. */
6445 Lisp_Object prop;
6446 prop = Fget_char_property (make_number (IT_CHARPOS (*it) - 1),
6447 Qinvisible, it->window);
6448 if (TEXT_PROP_MEANS_INVISIBLE (prop) != 0)
6449 continue;
6452 if (IT_CHARPOS (*it) <= BEGV)
6453 break;
6456 struct it it2;
6457 void *it2data = NULL;
6458 ptrdiff_t pos;
6459 ptrdiff_t beg, end;
6460 Lisp_Object val, overlay;
6462 SAVE_IT (it2, *it, it2data);
6464 /* If newline is part of a composition, continue from start of composition */
6465 if (find_composition (IT_CHARPOS (*it), -1, &beg, &end, &val, Qnil)
6466 && beg < IT_CHARPOS (*it))
6467 goto replaced;
6469 /* If newline is replaced by a display property, find start of overlay
6470 or interval and continue search from that point. */
6471 pos = --IT_CHARPOS (it2);
6472 --IT_BYTEPOS (it2);
6473 it2.sp = 0;
6474 bidi_unshelve_cache (NULL, false);
6475 it2.string_from_display_prop_p = false;
6476 it2.from_disp_prop_p = false;
6477 if (handle_display_prop (&it2) == HANDLED_RETURN
6478 && !NILP (val = get_char_property_and_overlay
6479 (make_number (pos), Qdisplay, Qnil, &overlay))
6480 && (OVERLAYP (overlay)
6481 ? (beg = OVERLAY_POSITION (OVERLAY_START (overlay)))
6482 : get_property_and_range (pos, Qdisplay, &val, &beg, &end, Qnil)))
6484 RESTORE_IT (it, it, it2data);
6485 goto replaced;
6488 /* Newline is not replaced by anything -- so we are done. */
6489 RESTORE_IT (it, it, it2data);
6490 break;
6492 replaced:
6493 if (beg < BEGV)
6494 beg = BEGV;
6495 IT_CHARPOS (*it) = beg;
6496 IT_BYTEPOS (*it) = buf_charpos_to_bytepos (current_buffer, beg);
6500 it->continuation_lines_width = 0;
6502 eassert (IT_CHARPOS (*it) >= BEGV);
6503 eassert (IT_CHARPOS (*it) == BEGV
6504 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
6505 CHECK_IT (it);
6509 /* Reseat iterator IT at the previous visible line start. Skip
6510 invisible text that is so either due to text properties or due to
6511 selective display. At the end, update IT's overlay information,
6512 face information etc. */
6514 void
6515 reseat_at_previous_visible_line_start (struct it *it)
6517 back_to_previous_visible_line_start (it);
6518 reseat (it, it->current.pos, true);
6519 CHECK_IT (it);
6523 /* Reseat iterator IT on the next visible line start in the current
6524 buffer. ON_NEWLINE_P means position IT on the newline
6525 preceding the line start. Skip over invisible text that is so
6526 because of selective display. Compute faces, overlays etc at the
6527 new position. Note that this function does not skip over text that
6528 is invisible because of text properties. */
6530 static void
6531 reseat_at_next_visible_line_start (struct it *it, bool on_newline_p)
6533 bool skipped_p = false;
6534 struct bidi_it bidi_it_prev;
6535 bool newline_found_p
6536 = forward_to_next_line_start (it, &skipped_p, &bidi_it_prev);
6538 /* Skip over lines that are invisible because they are indented
6539 more than the value of IT->selective. */
6540 if (it->selective > 0)
6541 while (IT_CHARPOS (*it) < ZV
6542 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
6543 it->selective))
6545 eassert (IT_BYTEPOS (*it) == BEGV
6546 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
6547 newline_found_p =
6548 forward_to_next_line_start (it, &skipped_p, &bidi_it_prev);
6551 /* Position on the newline if that's what's requested. */
6552 if (on_newline_p && newline_found_p)
6554 if (STRINGP (it->string))
6556 if (IT_STRING_CHARPOS (*it) > 0)
6558 if (!it->bidi_p)
6560 --IT_STRING_CHARPOS (*it);
6561 --IT_STRING_BYTEPOS (*it);
6563 else
6565 /* We need to restore the bidi iterator to the state
6566 it had on the newline, and resync the IT's
6567 position with that. */
6568 it->bidi_it = bidi_it_prev;
6569 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
6570 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
6574 else if (IT_CHARPOS (*it) > BEGV)
6576 if (!it->bidi_p)
6578 --IT_CHARPOS (*it);
6579 --IT_BYTEPOS (*it);
6581 else
6583 /* We need to restore the bidi iterator to the state it
6584 had on the newline and resync IT with that. */
6585 it->bidi_it = bidi_it_prev;
6586 IT_CHARPOS (*it) = it->bidi_it.charpos;
6587 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6589 reseat (it, it->current.pos, false);
6592 else if (skipped_p)
6593 reseat (it, it->current.pos, false);
6595 CHECK_IT (it);
6600 /***********************************************************************
6601 Changing an iterator's position
6602 ***********************************************************************/
6604 /* Change IT's current position to POS in current_buffer.
6605 If FORCE_P, always check for text properties at the new position.
6606 Otherwise, text properties are only looked up if POS >=
6607 IT->check_charpos of a property. */
6609 static void
6610 reseat (struct it *it, struct text_pos pos, bool force_p)
6612 ptrdiff_t original_pos = IT_CHARPOS (*it);
6614 reseat_1 (it, pos, false);
6616 /* Determine where to check text properties. Avoid doing it
6617 where possible because text property lookup is very expensive. */
6618 if (force_p
6619 || CHARPOS (pos) > it->stop_charpos
6620 || CHARPOS (pos) < original_pos)
6622 if (it->bidi_p)
6624 /* For bidi iteration, we need to prime prev_stop and
6625 base_level_stop with our best estimations. */
6626 /* Implementation note: Of course, POS is not necessarily a
6627 stop position, so assigning prev_pos to it is a lie; we
6628 should have called compute_stop_backwards. However, if
6629 the current buffer does not include any R2L characters,
6630 that call would be a waste of cycles, because the
6631 iterator will never move back, and thus never cross this
6632 "fake" stop position. So we delay that backward search
6633 until the time we really need it, in next_element_from_buffer. */
6634 if (CHARPOS (pos) != it->prev_stop)
6635 it->prev_stop = CHARPOS (pos);
6636 if (CHARPOS (pos) < it->base_level_stop)
6637 it->base_level_stop = 0; /* meaning it's unknown */
6638 handle_stop (it);
6640 else
6642 handle_stop (it);
6643 it->prev_stop = it->base_level_stop = 0;
6648 CHECK_IT (it);
6652 /* Change IT's buffer position to POS. SET_STOP_P means set
6653 IT->stop_pos to POS, also. */
6655 static void
6656 reseat_1 (struct it *it, struct text_pos pos, bool set_stop_p)
6658 /* Don't call this function when scanning a C string. */
6659 eassert (it->s == NULL);
6661 /* POS must be a reasonable value. */
6662 eassert (CHARPOS (pos) >= BEGV && CHARPOS (pos) <= ZV);
6664 it->current.pos = it->position = pos;
6665 it->end_charpos = ZV;
6666 it->dpvec = NULL;
6667 it->current.dpvec_index = -1;
6668 it->current.overlay_string_index = -1;
6669 IT_STRING_CHARPOS (*it) = -1;
6670 IT_STRING_BYTEPOS (*it) = -1;
6671 it->string = Qnil;
6672 it->method = GET_FROM_BUFFER;
6673 it->object = it->w->contents;
6674 it->area = TEXT_AREA;
6675 it->multibyte_p = !NILP (BVAR (current_buffer, enable_multibyte_characters));
6676 it->sp = 0;
6677 it->string_from_display_prop_p = false;
6678 it->string_from_prefix_prop_p = false;
6680 it->from_disp_prop_p = false;
6681 it->face_before_selective_p = false;
6682 if (it->bidi_p)
6684 bidi_init_it (IT_CHARPOS (*it), IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
6685 &it->bidi_it);
6686 bidi_unshelve_cache (NULL, false);
6687 it->bidi_it.paragraph_dir = NEUTRAL_DIR;
6688 it->bidi_it.string.s = NULL;
6689 it->bidi_it.string.lstring = Qnil;
6690 it->bidi_it.string.bufpos = 0;
6691 it->bidi_it.string.from_disp_str = false;
6692 it->bidi_it.string.unibyte = false;
6693 it->bidi_it.w = it->w;
6696 if (set_stop_p)
6698 it->stop_charpos = CHARPOS (pos);
6699 it->base_level_stop = CHARPOS (pos);
6701 /* This make the information stored in it->cmp_it invalidate. */
6702 it->cmp_it.id = -1;
6706 /* Set up IT for displaying a string, starting at CHARPOS in window W.
6707 If S is non-null, it is a C string to iterate over. Otherwise,
6708 STRING gives a Lisp string to iterate over.
6710 If PRECISION > 0, don't return more then PRECISION number of
6711 characters from the string.
6713 If FIELD_WIDTH > 0, return padding spaces until FIELD_WIDTH
6714 characters have been returned. FIELD_WIDTH < 0 means an infinite
6715 field width.
6717 MULTIBYTE = 0 means disable processing of multibyte characters,
6718 MULTIBYTE > 0 means enable it,
6719 MULTIBYTE < 0 means use IT->multibyte_p.
6721 IT must be initialized via a prior call to init_iterator before
6722 calling this function. */
6724 static void
6725 reseat_to_string (struct it *it, const char *s, Lisp_Object string,
6726 ptrdiff_t charpos, ptrdiff_t precision, int field_width,
6727 int multibyte)
6729 /* No text property checks performed by default, but see below. */
6730 it->stop_charpos = -1;
6732 /* Set iterator position and end position. */
6733 memset (&it->current, 0, sizeof it->current);
6734 it->current.overlay_string_index = -1;
6735 it->current.dpvec_index = -1;
6736 eassert (charpos >= 0);
6738 /* If STRING is specified, use its multibyteness, otherwise use the
6739 setting of MULTIBYTE, if specified. */
6740 if (multibyte >= 0)
6741 it->multibyte_p = multibyte > 0;
6743 /* Bidirectional reordering of strings is controlled by the default
6744 value of bidi-display-reordering. Don't try to reorder while
6745 loading loadup.el, as the necessary character property tables are
6746 not yet available. */
6747 it->bidi_p =
6748 !redisplay__inhibit_bidi
6749 && !NILP (BVAR (&buffer_defaults, bidi_display_reordering));
6751 if (s == NULL)
6753 eassert (STRINGP (string));
6754 it->string = string;
6755 it->s = NULL;
6756 it->end_charpos = it->string_nchars = SCHARS (string);
6757 it->method = GET_FROM_STRING;
6758 it->current.string_pos = string_pos (charpos, string);
6760 if (it->bidi_p)
6762 it->bidi_it.string.lstring = string;
6763 it->bidi_it.string.s = NULL;
6764 it->bidi_it.string.schars = it->end_charpos;
6765 it->bidi_it.string.bufpos = 0;
6766 it->bidi_it.string.from_disp_str = false;
6767 it->bidi_it.string.unibyte = !it->multibyte_p;
6768 it->bidi_it.w = it->w;
6769 bidi_init_it (charpos, IT_STRING_BYTEPOS (*it),
6770 FRAME_WINDOW_P (it->f), &it->bidi_it);
6773 else
6775 it->s = (const unsigned char *) s;
6776 it->string = Qnil;
6778 /* Note that we use IT->current.pos, not it->current.string_pos,
6779 for displaying C strings. */
6780 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
6781 if (it->multibyte_p)
6783 it->current.pos = c_string_pos (charpos, s, true);
6784 it->end_charpos = it->string_nchars = number_of_chars (s, true);
6786 else
6788 IT_CHARPOS (*it) = IT_BYTEPOS (*it) = charpos;
6789 it->end_charpos = it->string_nchars = strlen (s);
6792 if (it->bidi_p)
6794 it->bidi_it.string.lstring = Qnil;
6795 it->bidi_it.string.s = (const unsigned char *) s;
6796 it->bidi_it.string.schars = it->end_charpos;
6797 it->bidi_it.string.bufpos = 0;
6798 it->bidi_it.string.from_disp_str = false;
6799 it->bidi_it.string.unibyte = !it->multibyte_p;
6800 it->bidi_it.w = it->w;
6801 bidi_init_it (charpos, IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
6802 &it->bidi_it);
6804 it->method = GET_FROM_C_STRING;
6807 /* PRECISION > 0 means don't return more than PRECISION characters
6808 from the string. */
6809 if (precision > 0 && it->end_charpos - charpos > precision)
6811 it->end_charpos = it->string_nchars = charpos + precision;
6812 if (it->bidi_p)
6813 it->bidi_it.string.schars = it->end_charpos;
6816 /* FIELD_WIDTH > 0 means pad with spaces until FIELD_WIDTH
6817 characters have been returned. FIELD_WIDTH == 0 means don't pad,
6818 FIELD_WIDTH < 0 means infinite field width. This is useful for
6819 padding with `-' at the end of a mode line. */
6820 if (field_width < 0)
6821 field_width = DISP_INFINITY;
6822 /* Implementation note: We deliberately don't enlarge
6823 it->bidi_it.string.schars here to fit it->end_charpos, because
6824 the bidi iterator cannot produce characters out of thin air. */
6825 if (field_width > it->end_charpos - charpos)
6826 it->end_charpos = charpos + field_width;
6828 /* Use the standard display table for displaying strings. */
6829 if (DISP_TABLE_P (Vstandard_display_table))
6830 it->dp = XCHAR_TABLE (Vstandard_display_table);
6832 it->stop_charpos = charpos;
6833 it->prev_stop = charpos;
6834 it->base_level_stop = 0;
6835 if (it->bidi_p)
6837 it->bidi_it.first_elt = true;
6838 it->bidi_it.paragraph_dir = NEUTRAL_DIR;
6839 it->bidi_it.disp_pos = -1;
6841 if (s == NULL && it->multibyte_p)
6843 ptrdiff_t endpos = SCHARS (it->string);
6844 if (endpos > it->end_charpos)
6845 endpos = it->end_charpos;
6846 composition_compute_stop_pos (&it->cmp_it, charpos, -1, endpos,
6847 it->string);
6849 CHECK_IT (it);
6854 /***********************************************************************
6855 Iteration
6856 ***********************************************************************/
6858 /* Map enum it_method value to corresponding next_element_from_* function. */
6860 typedef bool (*next_element_function) (struct it *);
6862 static next_element_function const get_next_element[NUM_IT_METHODS] =
6864 next_element_from_buffer,
6865 next_element_from_display_vector,
6866 next_element_from_string,
6867 next_element_from_c_string,
6868 next_element_from_image,
6869 next_element_from_stretch,
6870 next_element_from_xwidget,
6873 #define GET_NEXT_DISPLAY_ELEMENT(it) (*get_next_element[(it)->method]) (it)
6876 /* Return true iff a character at CHARPOS (and BYTEPOS) is composed
6877 (possibly with the following characters). */
6879 #define CHAR_COMPOSED_P(IT,CHARPOS,BYTEPOS,END_CHARPOS) \
6880 ((IT)->cmp_it.id >= 0 \
6881 || ((IT)->cmp_it.stop_pos == (CHARPOS) \
6882 && composition_reseat_it (&(IT)->cmp_it, CHARPOS, BYTEPOS, \
6883 END_CHARPOS, (IT)->w, \
6884 FACE_FROM_ID_OR_NULL ((IT)->f, \
6885 (IT)->face_id), \
6886 (IT)->string)))
6889 /* Lookup the char-table Vglyphless_char_display for character C (-1
6890 if we want information for no-font case), and return the display
6891 method symbol. By side-effect, update it->what and
6892 it->glyphless_method. This function is called from
6893 get_next_display_element for each character element, and from
6894 x_produce_glyphs when no suitable font was found. */
6896 Lisp_Object
6897 lookup_glyphless_char_display (int c, struct it *it)
6899 Lisp_Object glyphless_method = Qnil;
6901 if (CHAR_TABLE_P (Vglyphless_char_display)
6902 && CHAR_TABLE_EXTRA_SLOTS (XCHAR_TABLE (Vglyphless_char_display)) >= 1)
6904 if (c >= 0)
6906 glyphless_method = CHAR_TABLE_REF (Vglyphless_char_display, c);
6907 if (CONSP (glyphless_method))
6908 glyphless_method = FRAME_WINDOW_P (it->f)
6909 ? XCAR (glyphless_method)
6910 : XCDR (glyphless_method);
6912 else
6913 glyphless_method = XCHAR_TABLE (Vglyphless_char_display)->extras[0];
6916 retry:
6917 if (NILP (glyphless_method))
6919 if (c >= 0)
6920 /* The default is to display the character by a proper font. */
6921 return Qnil;
6922 /* The default for the no-font case is to display an empty box. */
6923 glyphless_method = Qempty_box;
6925 if (EQ (glyphless_method, Qzero_width))
6927 if (c >= 0)
6928 return glyphless_method;
6929 /* This method can't be used for the no-font case. */
6930 glyphless_method = Qempty_box;
6932 if (EQ (glyphless_method, Qthin_space))
6933 it->glyphless_method = GLYPHLESS_DISPLAY_THIN_SPACE;
6934 else if (EQ (glyphless_method, Qempty_box))
6935 it->glyphless_method = GLYPHLESS_DISPLAY_EMPTY_BOX;
6936 else if (EQ (glyphless_method, Qhex_code))
6937 it->glyphless_method = GLYPHLESS_DISPLAY_HEX_CODE;
6938 else if (STRINGP (glyphless_method))
6939 it->glyphless_method = GLYPHLESS_DISPLAY_ACRONYM;
6940 else
6942 /* Invalid value. We use the default method. */
6943 glyphless_method = Qnil;
6944 goto retry;
6946 it->what = IT_GLYPHLESS;
6947 return glyphless_method;
6950 /* Merge escape glyph face and cache the result. */
6952 static struct frame *last_escape_glyph_frame = NULL;
6953 static int last_escape_glyph_face_id = (1 << FACE_ID_BITS);
6954 static int last_escape_glyph_merged_face_id = 0;
6956 static int
6957 merge_escape_glyph_face (struct it *it)
6959 int face_id;
6961 if (it->f == last_escape_glyph_frame
6962 && it->face_id == last_escape_glyph_face_id)
6963 face_id = last_escape_glyph_merged_face_id;
6964 else
6966 /* Merge the `escape-glyph' face into the current face. */
6967 face_id = merge_faces (it->f, Qescape_glyph, 0, it->face_id);
6968 last_escape_glyph_frame = it->f;
6969 last_escape_glyph_face_id = it->face_id;
6970 last_escape_glyph_merged_face_id = face_id;
6972 return face_id;
6975 /* Likewise for glyphless glyph face. */
6977 static struct frame *last_glyphless_glyph_frame = NULL;
6978 static int last_glyphless_glyph_face_id = (1 << FACE_ID_BITS);
6979 static int last_glyphless_glyph_merged_face_id = 0;
6982 merge_glyphless_glyph_face (struct it *it)
6984 int face_id;
6986 if (it->f == last_glyphless_glyph_frame
6987 && it->face_id == last_glyphless_glyph_face_id)
6988 face_id = last_glyphless_glyph_merged_face_id;
6989 else
6991 /* Merge the `glyphless-char' face into the current face. */
6992 face_id = merge_faces (it->f, Qglyphless_char, 0, it->face_id);
6993 last_glyphless_glyph_frame = it->f;
6994 last_glyphless_glyph_face_id = it->face_id;
6995 last_glyphless_glyph_merged_face_id = face_id;
6997 return face_id;
7000 /* Forget the `escape-glyph' and `glyphless-char' faces. This should
7001 be called before redisplaying windows, and when the frame's face
7002 cache is freed. */
7003 void
7004 forget_escape_and_glyphless_faces (void)
7006 last_escape_glyph_frame = NULL;
7007 last_escape_glyph_face_id = (1 << FACE_ID_BITS);
7008 last_glyphless_glyph_frame = NULL;
7009 last_glyphless_glyph_face_id = (1 << FACE_ID_BITS);
7012 /* Load IT's display element fields with information about the next
7013 display element from the current position of IT. Value is false if
7014 end of buffer (or C string) is reached. */
7016 static bool
7017 get_next_display_element (struct it *it)
7019 /* True means that we found a display element. False means that
7020 we hit the end of what we iterate over. Performance note: the
7021 function pointer `method' used here turns out to be faster than
7022 using a sequence of if-statements. */
7023 bool success_p;
7025 get_next:
7026 success_p = GET_NEXT_DISPLAY_ELEMENT (it);
7028 if (it->what == IT_CHARACTER)
7030 /* UAX#9, L4: "A character is depicted by a mirrored glyph if
7031 and only if (a) the resolved directionality of that character
7032 is R..." */
7033 /* FIXME: Do we need an exception for characters from display
7034 tables? */
7035 if (it->bidi_p && it->bidi_it.type == STRONG_R
7036 && !inhibit_bidi_mirroring)
7037 it->c = bidi_mirror_char (it->c);
7038 /* Map via display table or translate control characters.
7039 IT->c, IT->len etc. have been set to the next character by
7040 the function call above. If we have a display table, and it
7041 contains an entry for IT->c, translate it. Don't do this if
7042 IT->c itself comes from a display table, otherwise we could
7043 end up in an infinite recursion. (An alternative could be to
7044 count the recursion depth of this function and signal an
7045 error when a certain maximum depth is reached.) Is it worth
7046 it? */
7047 if (success_p && it->dpvec == NULL)
7049 Lisp_Object dv;
7050 struct charset *unibyte = CHARSET_FROM_ID (charset_unibyte);
7051 bool nonascii_space_p = false;
7052 bool nonascii_hyphen_p = false;
7053 int c = it->c; /* This is the character to display. */
7055 if (! it->multibyte_p && ! ASCII_CHAR_P (c))
7057 eassert (SINGLE_BYTE_CHAR_P (c));
7058 if (unibyte_display_via_language_environment)
7060 c = DECODE_CHAR (unibyte, c);
7061 if (c < 0)
7062 c = BYTE8_TO_CHAR (it->c);
7064 else
7065 c = BYTE8_TO_CHAR (it->c);
7068 if (it->dp
7069 && (dv = DISP_CHAR_VECTOR (it->dp, c),
7070 VECTORP (dv)))
7072 struct Lisp_Vector *v = XVECTOR (dv);
7074 /* Return the first character from the display table
7075 entry, if not empty. If empty, don't display the
7076 current character. */
7077 if (v->header.size)
7079 it->dpvec_char_len = it->len;
7080 it->dpvec = v->contents;
7081 it->dpend = v->contents + v->header.size;
7082 it->current.dpvec_index = 0;
7083 it->dpvec_face_id = -1;
7084 it->saved_face_id = it->face_id;
7085 it->method = GET_FROM_DISPLAY_VECTOR;
7086 it->ellipsis_p = false;
7088 else
7090 set_iterator_to_next (it, false);
7092 goto get_next;
7095 if (! NILP (lookup_glyphless_char_display (c, it)))
7097 if (it->what == IT_GLYPHLESS)
7098 goto done;
7099 /* Don't display this character. */
7100 set_iterator_to_next (it, false);
7101 goto get_next;
7104 /* If `nobreak-char-display' is non-nil, we display
7105 non-ASCII spaces and hyphens specially. */
7106 if (! ASCII_CHAR_P (c) && ! NILP (Vnobreak_char_display))
7108 if (c == NO_BREAK_SPACE)
7109 nonascii_space_p = true;
7110 else if (c == SOFT_HYPHEN || c == HYPHEN
7111 || c == NON_BREAKING_HYPHEN)
7112 nonascii_hyphen_p = true;
7115 /* Translate control characters into `\003' or `^C' form.
7116 Control characters coming from a display table entry are
7117 currently not translated because we use IT->dpvec to hold
7118 the translation. This could easily be changed but I
7119 don't believe that it is worth doing.
7121 The characters handled by `nobreak-char-display' must be
7122 translated too.
7124 Non-printable characters and raw-byte characters are also
7125 translated to octal or hexadecimal form. */
7126 if (((c < ' ' || c == 127) /* ASCII control chars. */
7127 ? (it->area != TEXT_AREA
7128 /* In mode line, treat \n, \t like other crl chars. */
7129 || (c != '\t'
7130 && it->glyph_row
7131 && (it->glyph_row->mode_line_p || it->avoid_cursor_p))
7132 || (c != '\n' && c != '\t'))
7133 : (nonascii_space_p
7134 || nonascii_hyphen_p
7135 || CHAR_BYTE8_P (c)
7136 || ! CHAR_PRINTABLE_P (c))))
7138 /* C is a control character, non-ASCII space/hyphen,
7139 raw-byte, or a non-printable character which must be
7140 displayed either as '\003' or as `^C' where the '\\'
7141 and '^' can be defined in the display table. Fill
7142 IT->ctl_chars with glyphs for what we have to
7143 display. Then, set IT->dpvec to these glyphs. */
7144 Lisp_Object gc;
7145 int ctl_len;
7146 int face_id;
7147 int lface_id = 0;
7148 int escape_glyph;
7150 /* Handle control characters with ^. */
7152 if (ASCII_CHAR_P (c) && it->ctl_arrow_p)
7154 int g;
7156 g = '^'; /* default glyph for Control */
7157 /* Set IT->ctl_chars[0] to the glyph for `^'. */
7158 if (it->dp
7159 && (gc = DISP_CTRL_GLYPH (it->dp), GLYPH_CODE_P (gc)))
7161 g = GLYPH_CODE_CHAR (gc);
7162 lface_id = GLYPH_CODE_FACE (gc);
7165 face_id = (lface_id
7166 ? merge_faces (it->f, Qt, lface_id, it->face_id)
7167 : merge_escape_glyph_face (it));
7169 XSETINT (it->ctl_chars[0], g);
7170 XSETINT (it->ctl_chars[1], c ^ 0100);
7171 ctl_len = 2;
7172 goto display_control;
7175 /* Handle non-ascii space in the mode where it only gets
7176 highlighting. */
7178 if (nonascii_space_p && EQ (Vnobreak_char_display, Qt))
7180 /* Merge `nobreak-space' into the current face. */
7181 face_id = merge_faces (it->f, Qnobreak_space, 0,
7182 it->face_id);
7183 XSETINT (it->ctl_chars[0], ' ');
7184 ctl_len = 1;
7185 goto display_control;
7188 /* Handle non-ascii hyphens in the mode where it only
7189 gets highlighting. */
7191 if (nonascii_hyphen_p && EQ (Vnobreak_char_display, Qt))
7193 /* Merge `nobreak-space' into the current face. */
7194 face_id = merge_faces (it->f, Qnobreak_hyphen, 0,
7195 it->face_id);
7196 XSETINT (it->ctl_chars[0], '-');
7197 ctl_len = 1;
7198 goto display_control;
7201 /* Handle sequences that start with the "escape glyph". */
7203 /* the default escape glyph is \. */
7204 escape_glyph = '\\';
7206 if (it->dp
7207 && (gc = DISP_ESCAPE_GLYPH (it->dp), GLYPH_CODE_P (gc)))
7209 escape_glyph = GLYPH_CODE_CHAR (gc);
7210 lface_id = GLYPH_CODE_FACE (gc);
7213 face_id = (lface_id
7214 ? merge_faces (it->f, Qt, lface_id, it->face_id)
7215 : merge_escape_glyph_face (it));
7217 /* Draw non-ASCII space/hyphen with escape glyph: */
7219 if (nonascii_space_p || nonascii_hyphen_p)
7221 XSETINT (it->ctl_chars[0], escape_glyph);
7222 XSETINT (it->ctl_chars[1], nonascii_space_p ? ' ' : '-');
7223 ctl_len = 2;
7224 goto display_control;
7228 char str[10];
7229 int len, i;
7231 if (CHAR_BYTE8_P (c))
7232 /* Display \200 or \x80 instead of \17777600. */
7233 c = CHAR_TO_BYTE8 (c);
7234 const char *format_string = display_raw_bytes_as_hex
7235 ? "x%02x"
7236 : "%03o";
7237 len = sprintf (str, format_string, c + 0u);
7239 XSETINT (it->ctl_chars[0], escape_glyph);
7240 for (i = 0; i < len; i++)
7241 XSETINT (it->ctl_chars[i + 1], str[i]);
7242 ctl_len = len + 1;
7245 display_control:
7246 /* Set up IT->dpvec and return first character from it. */
7247 it->dpvec_char_len = it->len;
7248 it->dpvec = it->ctl_chars;
7249 it->dpend = it->dpvec + ctl_len;
7250 it->current.dpvec_index = 0;
7251 it->dpvec_face_id = face_id;
7252 it->saved_face_id = it->face_id;
7253 it->method = GET_FROM_DISPLAY_VECTOR;
7254 it->ellipsis_p = false;
7255 goto get_next;
7257 it->char_to_display = c;
7259 else if (success_p)
7261 it->char_to_display = it->c;
7265 #ifdef HAVE_WINDOW_SYSTEM
7266 /* Adjust face id for a multibyte character. There are no multibyte
7267 character in unibyte text. */
7268 if ((it->what == IT_CHARACTER || it->what == IT_COMPOSITION)
7269 && it->multibyte_p
7270 && success_p
7271 && FRAME_WINDOW_P (it->f))
7273 struct face *face = FACE_FROM_ID (it->f, it->face_id);
7275 if (it->what == IT_COMPOSITION && it->cmp_it.ch >= 0)
7277 /* Automatic composition with glyph-string. */
7278 Lisp_Object gstring = composition_gstring_from_id (it->cmp_it.id);
7280 it->face_id = face_for_font (it->f, LGSTRING_FONT (gstring), face);
7282 else
7284 ptrdiff_t pos = (it->s ? -1
7285 : STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
7286 : IT_CHARPOS (*it));
7287 int c;
7289 if (it->what == IT_CHARACTER)
7290 c = it->char_to_display;
7291 else
7293 struct composition *cmp = composition_table[it->cmp_it.id];
7294 int i;
7296 c = ' ';
7297 for (i = 0; i < cmp->glyph_len; i++)
7298 /* TAB in a composition means display glyphs with
7299 padding space on the left or right. */
7300 if ((c = COMPOSITION_GLYPH (cmp, i)) != '\t')
7301 break;
7303 it->face_id = FACE_FOR_CHAR (it->f, face, c, pos, it->string);
7306 #endif /* HAVE_WINDOW_SYSTEM */
7308 done:
7309 /* Is this character the last one of a run of characters with
7310 box? If yes, set IT->end_of_box_run_p to true. */
7311 if (it->face_box_p
7312 && it->s == NULL)
7314 if (it->method == GET_FROM_STRING && it->sp)
7316 int face_id = underlying_face_id (it);
7317 struct face *face = FACE_FROM_ID_OR_NULL (it->f, face_id);
7319 if (face)
7321 if (face->box == FACE_NO_BOX)
7323 /* If the box comes from face properties in a
7324 display string, check faces in that string. */
7325 int string_face_id = face_after_it_pos (it);
7326 it->end_of_box_run_p
7327 = (FACE_FROM_ID (it->f, string_face_id)->box
7328 == FACE_NO_BOX);
7330 /* Otherwise, the box comes from the underlying face.
7331 If this is the last string character displayed, check
7332 the next buffer location. */
7333 else if ((IT_STRING_CHARPOS (*it) >= SCHARS (it->string) - 1)
7334 /* n_overlay_strings is unreliable unless
7335 overlay_string_index is non-negative. */
7336 && ((it->current.overlay_string_index >= 0
7337 && (it->current.overlay_string_index
7338 == it->n_overlay_strings - 1))
7339 /* A string from display property. */
7340 || it->from_disp_prop_p))
7342 ptrdiff_t ignore;
7343 int next_face_id;
7344 bool text_from_string = false;
7345 /* Normally, the next buffer location is stored in
7346 IT->current.pos... */
7347 struct text_pos pos = it->current.pos;
7349 /* ...but for a string from a display property, the
7350 next buffer position is stored in the 'position'
7351 member of the iteration stack slot below the
7352 current one, see handle_single_display_spec. By
7353 contrast, it->current.pos was not yet updated to
7354 point to that buffer position; that will happen
7355 in pop_it, after we finish displaying the current
7356 string. Note that we already checked above that
7357 it->sp is positive, so subtracting one from it is
7358 safe. */
7359 if (it->from_disp_prop_p)
7361 int stackp = it->sp - 1;
7363 /* Find the stack level with data from buffer. */
7364 while (stackp >= 0
7365 && STRINGP ((it->stack + stackp)->string))
7366 stackp--;
7367 if (stackp < 0)
7369 /* If no stack slot was found for iterating
7370 a buffer, we are displaying text from a
7371 string, most probably the mode line or
7372 the header line, and that string has a
7373 display string on some of its
7374 characters. */
7375 text_from_string = true;
7376 pos = it->stack[it->sp - 1].position;
7378 else
7379 pos = (it->stack + stackp)->position;
7381 else
7382 INC_TEXT_POS (pos, it->multibyte_p);
7384 if (text_from_string)
7386 Lisp_Object base_string = it->stack[it->sp - 1].string;
7388 if (CHARPOS (pos) >= SCHARS (base_string) - 1)
7389 it->end_of_box_run_p = true;
7390 else
7392 next_face_id
7393 = face_at_string_position (it->w, base_string,
7394 CHARPOS (pos), 0,
7395 &ignore, face_id, false);
7396 it->end_of_box_run_p
7397 = (FACE_FROM_ID (it->f, next_face_id)->box
7398 == FACE_NO_BOX);
7401 else if (CHARPOS (pos) >= ZV)
7402 it->end_of_box_run_p = true;
7403 else
7405 next_face_id =
7406 face_at_buffer_position (it->w, CHARPOS (pos), &ignore,
7407 CHARPOS (pos)
7408 + TEXT_PROP_DISTANCE_LIMIT,
7409 false, -1);
7410 it->end_of_box_run_p
7411 = (FACE_FROM_ID (it->f, next_face_id)->box
7412 == FACE_NO_BOX);
7417 /* next_element_from_display_vector sets this flag according to
7418 faces of the display vector glyphs, see there. */
7419 else if (it->method != GET_FROM_DISPLAY_VECTOR)
7421 int face_id = face_after_it_pos (it);
7422 it->end_of_box_run_p
7423 = (face_id != it->face_id
7424 && FACE_FROM_ID (it->f, face_id)->box == FACE_NO_BOX);
7427 /* If we reached the end of the object we've been iterating (e.g., a
7428 display string or an overlay string), and there's something on
7429 IT->stack, proceed with what's on the stack. It doesn't make
7430 sense to return false if there's unprocessed stuff on the stack,
7431 because otherwise that stuff will never be displayed. */
7432 if (!success_p && it->sp > 0)
7434 set_iterator_to_next (it, false);
7435 success_p = get_next_display_element (it);
7438 /* Value is false if end of buffer or string reached. */
7439 return success_p;
7443 /* Move IT to the next display element.
7445 RESEAT_P means if called on a newline in buffer text,
7446 skip to the next visible line start.
7448 Functions get_next_display_element and set_iterator_to_next are
7449 separate because I find this arrangement easier to handle than a
7450 get_next_display_element function that also increments IT's
7451 position. The way it is we can first look at an iterator's current
7452 display element, decide whether it fits on a line, and if it does,
7453 increment the iterator position. The other way around we probably
7454 would either need a flag indicating whether the iterator has to be
7455 incremented the next time, or we would have to implement a
7456 decrement position function which would not be easy to write. */
7458 void
7459 set_iterator_to_next (struct it *it, bool reseat_p)
7461 /* Reset flags indicating start and end of a sequence of characters
7462 with box. Reset them at the start of this function because
7463 moving the iterator to a new position might set them. */
7464 it->start_of_box_run_p = it->end_of_box_run_p = false;
7466 switch (it->method)
7468 case GET_FROM_BUFFER:
7469 /* The current display element of IT is a character from
7470 current_buffer. Advance in the buffer, and maybe skip over
7471 invisible lines that are so because of selective display. */
7472 if (ITERATOR_AT_END_OF_LINE_P (it) && reseat_p)
7473 reseat_at_next_visible_line_start (it, false);
7474 else if (it->cmp_it.id >= 0)
7476 /* We are currently getting glyphs from a composition. */
7477 if (! it->bidi_p)
7479 IT_CHARPOS (*it) += it->cmp_it.nchars;
7480 IT_BYTEPOS (*it) += it->cmp_it.nbytes;
7482 else
7484 int i;
7486 /* Update IT's char/byte positions to point to the first
7487 character of the next grapheme cluster, or to the
7488 character visually after the current composition. */
7489 for (i = 0; i < it->cmp_it.nchars; i++)
7490 bidi_move_to_visually_next (&it->bidi_it);
7491 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7492 IT_CHARPOS (*it) = it->bidi_it.charpos;
7495 if ((! it->bidi_p || ! it->cmp_it.reversed_p)
7496 && it->cmp_it.to < it->cmp_it.nglyphs)
7498 /* Composition created while scanning forward. Proceed
7499 to the next grapheme cluster. */
7500 it->cmp_it.from = it->cmp_it.to;
7502 else if ((it->bidi_p && it->cmp_it.reversed_p)
7503 && it->cmp_it.from > 0)
7505 /* Composition created while scanning backward. Proceed
7506 to the previous grapheme cluster. */
7507 it->cmp_it.to = it->cmp_it.from;
7509 else
7511 /* No more grapheme clusters in this composition.
7512 Find the next stop position. */
7513 ptrdiff_t stop = it->end_charpos;
7515 if (it->bidi_it.scan_dir < 0)
7516 /* Now we are scanning backward and don't know
7517 where to stop. */
7518 stop = -1;
7519 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
7520 IT_BYTEPOS (*it), stop, Qnil);
7523 else
7525 eassert (it->len != 0);
7527 if (!it->bidi_p)
7529 IT_BYTEPOS (*it) += it->len;
7530 IT_CHARPOS (*it) += 1;
7532 else
7534 int prev_scan_dir = it->bidi_it.scan_dir;
7535 /* If this is a new paragraph, determine its base
7536 direction (a.k.a. its base embedding level). */
7537 if (it->bidi_it.new_paragraph)
7538 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it,
7539 false);
7540 bidi_move_to_visually_next (&it->bidi_it);
7541 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7542 IT_CHARPOS (*it) = it->bidi_it.charpos;
7543 if (prev_scan_dir != it->bidi_it.scan_dir)
7545 /* As the scan direction was changed, we must
7546 re-compute the stop position for composition. */
7547 ptrdiff_t stop = it->end_charpos;
7548 if (it->bidi_it.scan_dir < 0)
7549 stop = -1;
7550 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
7551 IT_BYTEPOS (*it), stop, Qnil);
7554 eassert (IT_BYTEPOS (*it) == CHAR_TO_BYTE (IT_CHARPOS (*it)));
7556 break;
7558 case GET_FROM_C_STRING:
7559 /* Current display element of IT is from a C string. */
7560 if (!it->bidi_p
7561 /* If the string position is beyond string's end, it means
7562 next_element_from_c_string is padding the string with
7563 blanks, in which case we bypass the bidi iterator,
7564 because it cannot deal with such virtual characters. */
7565 || IT_CHARPOS (*it) >= it->bidi_it.string.schars)
7567 IT_BYTEPOS (*it) += it->len;
7568 IT_CHARPOS (*it) += 1;
7570 else
7572 bidi_move_to_visually_next (&it->bidi_it);
7573 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7574 IT_CHARPOS (*it) = it->bidi_it.charpos;
7576 break;
7578 case GET_FROM_DISPLAY_VECTOR:
7579 /* Current display element of IT is from a display table entry.
7580 Advance in the display table definition. Reset it to null if
7581 end reached, and continue with characters from buffers/
7582 strings. */
7583 ++it->current.dpvec_index;
7585 /* Restore face of the iterator to what they were before the
7586 display vector entry (these entries may contain faces). */
7587 it->face_id = it->saved_face_id;
7589 if (it->dpvec + it->current.dpvec_index >= it->dpend)
7591 bool recheck_faces = it->ellipsis_p;
7593 if (it->s)
7594 it->method = GET_FROM_C_STRING;
7595 else if (STRINGP (it->string))
7596 it->method = GET_FROM_STRING;
7597 else
7599 it->method = GET_FROM_BUFFER;
7600 it->object = it->w->contents;
7603 it->dpvec = NULL;
7604 it->current.dpvec_index = -1;
7606 /* Skip over characters which were displayed via IT->dpvec. */
7607 if (it->dpvec_char_len < 0)
7608 reseat_at_next_visible_line_start (it, true);
7609 else if (it->dpvec_char_len > 0)
7611 it->len = it->dpvec_char_len;
7612 set_iterator_to_next (it, reseat_p);
7615 /* Maybe recheck faces after display vector. */
7616 if (recheck_faces)
7618 if (it->method == GET_FROM_STRING)
7619 it->stop_charpos = IT_STRING_CHARPOS (*it);
7620 else
7621 it->stop_charpos = IT_CHARPOS (*it);
7624 break;
7626 case GET_FROM_STRING:
7627 /* Current display element is a character from a Lisp string. */
7628 eassert (it->s == NULL && STRINGP (it->string));
7629 /* Don't advance past string end. These conditions are true
7630 when set_iterator_to_next is called at the end of
7631 get_next_display_element, in which case the Lisp string is
7632 already exhausted, and all we want is pop the iterator
7633 stack. */
7634 if (it->current.overlay_string_index >= 0)
7636 /* This is an overlay string, so there's no padding with
7637 spaces, and the number of characters in the string is
7638 where the string ends. */
7639 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7640 goto consider_string_end;
7642 else
7644 /* Not an overlay string. There could be padding, so test
7645 against it->end_charpos. */
7646 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
7647 goto consider_string_end;
7649 if (it->cmp_it.id >= 0)
7651 /* We are delivering display elements from a composition.
7652 Update the string position past the grapheme cluster
7653 we've just processed. */
7654 if (! it->bidi_p)
7656 IT_STRING_CHARPOS (*it) += it->cmp_it.nchars;
7657 IT_STRING_BYTEPOS (*it) += it->cmp_it.nbytes;
7659 else
7661 int i;
7663 for (i = 0; i < it->cmp_it.nchars; i++)
7664 bidi_move_to_visually_next (&it->bidi_it);
7665 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7666 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7669 /* Did we exhaust all the grapheme clusters of this
7670 composition? */
7671 if ((! it->bidi_p || ! it->cmp_it.reversed_p)
7672 && (it->cmp_it.to < it->cmp_it.nglyphs))
7674 /* Not all the grapheme clusters were processed yet;
7675 advance to the next cluster. */
7676 it->cmp_it.from = it->cmp_it.to;
7678 else if ((it->bidi_p && it->cmp_it.reversed_p)
7679 && it->cmp_it.from > 0)
7681 /* Likewise: advance to the next cluster, but going in
7682 the reverse direction. */
7683 it->cmp_it.to = it->cmp_it.from;
7685 else
7687 /* This composition was fully processed; find the next
7688 candidate place for checking for composed
7689 characters. */
7690 /* Always limit string searches to the string length;
7691 any padding spaces are not part of the string, and
7692 there cannot be any compositions in that padding. */
7693 ptrdiff_t stop = SCHARS (it->string);
7695 if (it->bidi_p && it->bidi_it.scan_dir < 0)
7696 stop = -1;
7697 else if (it->end_charpos < stop)
7699 /* Cf. PRECISION in reseat_to_string: we might be
7700 limited in how many of the string characters we
7701 need to deliver. */
7702 stop = it->end_charpos;
7704 composition_compute_stop_pos (&it->cmp_it,
7705 IT_STRING_CHARPOS (*it),
7706 IT_STRING_BYTEPOS (*it), stop,
7707 it->string);
7710 else
7712 if (!it->bidi_p
7713 /* If the string position is beyond string's end, it
7714 means next_element_from_string is padding the string
7715 with blanks, in which case we bypass the bidi
7716 iterator, because it cannot deal with such virtual
7717 characters. */
7718 || IT_STRING_CHARPOS (*it) >= it->bidi_it.string.schars)
7720 IT_STRING_BYTEPOS (*it) += it->len;
7721 IT_STRING_CHARPOS (*it) += 1;
7723 else
7725 int prev_scan_dir = it->bidi_it.scan_dir;
7727 bidi_move_to_visually_next (&it->bidi_it);
7728 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7729 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7730 /* If the scan direction changes, we may need to update
7731 the place where to check for composed characters. */
7732 if (prev_scan_dir != it->bidi_it.scan_dir)
7734 ptrdiff_t stop = SCHARS (it->string);
7736 if (it->bidi_it.scan_dir < 0)
7737 stop = -1;
7738 else if (it->end_charpos < stop)
7739 stop = it->end_charpos;
7741 composition_compute_stop_pos (&it->cmp_it,
7742 IT_STRING_CHARPOS (*it),
7743 IT_STRING_BYTEPOS (*it), stop,
7744 it->string);
7749 consider_string_end:
7751 if (it->current.overlay_string_index >= 0)
7753 /* IT->string is an overlay string. Advance to the
7754 next, if there is one. */
7755 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7757 it->ellipsis_p = false;
7758 next_overlay_string (it);
7759 if (it->ellipsis_p)
7760 setup_for_ellipsis (it, 0);
7763 else
7765 /* IT->string is not an overlay string. If we reached
7766 its end, and there is something on IT->stack, proceed
7767 with what is on the stack. This can be either another
7768 string, this time an overlay string, or a buffer. */
7769 if (IT_STRING_CHARPOS (*it) == SCHARS (it->string)
7770 && it->sp > 0)
7772 pop_it (it);
7773 if (it->method == GET_FROM_STRING)
7774 goto consider_string_end;
7777 break;
7779 case GET_FROM_IMAGE:
7780 case GET_FROM_STRETCH:
7781 case GET_FROM_XWIDGET:
7783 /* The position etc with which we have to proceed are on
7784 the stack. The position may be at the end of a string,
7785 if the `display' property takes up the whole string. */
7786 eassert (it->sp > 0);
7787 pop_it (it);
7788 if (it->method == GET_FROM_STRING)
7789 goto consider_string_end;
7790 break;
7792 default:
7793 /* There are no other methods defined, so this should be a bug. */
7794 emacs_abort ();
7797 eassert (it->method != GET_FROM_STRING
7798 || (STRINGP (it->string)
7799 && IT_STRING_CHARPOS (*it) >= 0));
7802 /* Load IT's display element fields with information about the next
7803 display element which comes from a display table entry or from the
7804 result of translating a control character to one of the forms `^C'
7805 or `\003'.
7807 IT->dpvec holds the glyphs to return as characters.
7808 IT->saved_face_id holds the face id before the display vector--it
7809 is restored into IT->face_id in set_iterator_to_next. */
7811 static bool
7812 next_element_from_display_vector (struct it *it)
7814 Lisp_Object gc;
7815 int prev_face_id = it->face_id;
7816 int next_face_id;
7818 /* Precondition. */
7819 eassert (it->dpvec && it->current.dpvec_index >= 0);
7821 it->face_id = it->saved_face_id;
7823 /* KFS: This code used to check ip->dpvec[0] instead of the current element.
7824 That seemed totally bogus - so I changed it... */
7825 if (it->dpend - it->dpvec > 0 /* empty dpvec[] is invalid */
7826 && (gc = it->dpvec[it->current.dpvec_index], GLYPH_CODE_P (gc)))
7828 struct face *this_face, *prev_face, *next_face;
7830 it->c = GLYPH_CODE_CHAR (gc);
7831 it->len = CHAR_BYTES (it->c);
7833 /* The entry may contain a face id to use. Such a face id is
7834 the id of a Lisp face, not a realized face. A face id of
7835 zero means no face is specified. */
7836 if (it->dpvec_face_id >= 0)
7837 it->face_id = it->dpvec_face_id;
7838 else
7840 int lface_id = GLYPH_CODE_FACE (gc);
7841 if (lface_id > 0)
7842 it->face_id = merge_faces (it->f, Qt, lface_id,
7843 it->saved_face_id);
7846 /* Glyphs in the display vector could have the box face, so we
7847 need to set the related flags in the iterator, as
7848 appropriate. */
7849 this_face = FACE_FROM_ID_OR_NULL (it->f, it->face_id);
7850 prev_face = FACE_FROM_ID_OR_NULL (it->f, prev_face_id);
7852 /* Is this character the first character of a box-face run? */
7853 it->start_of_box_run_p = (this_face && this_face->box != FACE_NO_BOX
7854 && (!prev_face
7855 || prev_face->box == FACE_NO_BOX));
7857 /* For the last character of the box-face run, we need to look
7858 either at the next glyph from the display vector, or at the
7859 face we saw before the display vector. */
7860 next_face_id = it->saved_face_id;
7861 if (it->current.dpvec_index < it->dpend - it->dpvec - 1)
7863 if (it->dpvec_face_id >= 0)
7864 next_face_id = it->dpvec_face_id;
7865 else
7867 int lface_id =
7868 GLYPH_CODE_FACE (it->dpvec[it->current.dpvec_index + 1]);
7870 if (lface_id > 0)
7871 next_face_id = merge_faces (it->f, Qt, lface_id,
7872 it->saved_face_id);
7875 next_face = FACE_FROM_ID_OR_NULL (it->f, next_face_id);
7876 it->end_of_box_run_p = (this_face && this_face->box != FACE_NO_BOX
7877 && (!next_face
7878 || next_face->box == FACE_NO_BOX));
7879 it->face_box_p = this_face && this_face->box != FACE_NO_BOX;
7881 else
7882 /* Display table entry is invalid. Return a space. */
7883 it->c = ' ', it->len = 1;
7885 /* Don't change position and object of the iterator here. They are
7886 still the values of the character that had this display table
7887 entry or was translated, and that's what we want. */
7888 it->what = IT_CHARACTER;
7889 return true;
7892 /* Get the first element of string/buffer in the visual order, after
7893 being reseated to a new position in a string or a buffer. */
7894 static void
7895 get_visually_first_element (struct it *it)
7897 bool string_p = STRINGP (it->string) || it->s;
7898 ptrdiff_t eob = (string_p ? it->bidi_it.string.schars : ZV);
7899 ptrdiff_t bob = (string_p ? 0 : BEGV);
7901 if (STRINGP (it->string))
7903 it->bidi_it.charpos = IT_STRING_CHARPOS (*it);
7904 it->bidi_it.bytepos = IT_STRING_BYTEPOS (*it);
7906 else
7908 it->bidi_it.charpos = IT_CHARPOS (*it);
7909 it->bidi_it.bytepos = IT_BYTEPOS (*it);
7912 if (it->bidi_it.charpos == eob)
7914 /* Nothing to do, but reset the FIRST_ELT flag, like
7915 bidi_paragraph_init does, because we are not going to
7916 call it. */
7917 it->bidi_it.first_elt = false;
7919 else if (it->bidi_it.charpos == bob
7920 || (!string_p
7921 && (FETCH_CHAR (it->bidi_it.bytepos - 1) == '\n'
7922 || FETCH_CHAR (it->bidi_it.bytepos) == '\n')))
7924 /* If we are at the beginning of a line/string, we can produce
7925 the next element right away. */
7926 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, true);
7927 bidi_move_to_visually_next (&it->bidi_it);
7929 else
7931 ptrdiff_t orig_bytepos = it->bidi_it.bytepos;
7933 /* We need to prime the bidi iterator starting at the line's or
7934 string's beginning, before we will be able to produce the
7935 next element. */
7936 if (string_p)
7937 it->bidi_it.charpos = it->bidi_it.bytepos = 0;
7938 else
7939 it->bidi_it.charpos = find_newline_no_quit (IT_CHARPOS (*it),
7940 IT_BYTEPOS (*it), -1,
7941 &it->bidi_it.bytepos);
7942 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, true);
7945 /* Now return to buffer/string position where we were asked
7946 to get the next display element, and produce that. */
7947 bidi_move_to_visually_next (&it->bidi_it);
7949 while (it->bidi_it.bytepos != orig_bytepos
7950 && it->bidi_it.charpos < eob);
7953 /* Adjust IT's position information to where we ended up. */
7954 if (STRINGP (it->string))
7956 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7957 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7959 else
7961 IT_CHARPOS (*it) = it->bidi_it.charpos;
7962 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7965 if (STRINGP (it->string) || !it->s)
7967 ptrdiff_t stop, charpos, bytepos;
7969 if (STRINGP (it->string))
7971 eassert (!it->s);
7972 stop = SCHARS (it->string);
7973 if (stop > it->end_charpos)
7974 stop = it->end_charpos;
7975 charpos = IT_STRING_CHARPOS (*it);
7976 bytepos = IT_STRING_BYTEPOS (*it);
7978 else
7980 stop = it->end_charpos;
7981 charpos = IT_CHARPOS (*it);
7982 bytepos = IT_BYTEPOS (*it);
7984 if (it->bidi_it.scan_dir < 0)
7985 stop = -1;
7986 composition_compute_stop_pos (&it->cmp_it, charpos, bytepos, stop,
7987 it->string);
7991 /* Load IT with the next display element from Lisp string IT->string.
7992 IT->current.string_pos is the current position within the string.
7993 If IT->current.overlay_string_index >= 0, the Lisp string is an
7994 overlay string. */
7996 static bool
7997 next_element_from_string (struct it *it)
7999 struct text_pos position;
8001 eassert (STRINGP (it->string));
8002 eassert (!it->bidi_p || EQ (it->string, it->bidi_it.string.lstring));
8003 eassert (IT_STRING_CHARPOS (*it) >= 0);
8004 position = it->current.string_pos;
8006 /* With bidi reordering, the character to display might not be the
8007 character at IT_STRING_CHARPOS. BIDI_IT.FIRST_ELT means
8008 that we were reseat()ed to a new string, whose paragraph
8009 direction is not known. */
8010 if (it->bidi_p && it->bidi_it.first_elt)
8012 get_visually_first_element (it);
8013 SET_TEXT_POS (position, IT_STRING_CHARPOS (*it), IT_STRING_BYTEPOS (*it));
8016 /* Time to check for invisible text? */
8017 if (IT_STRING_CHARPOS (*it) < it->end_charpos)
8019 if (IT_STRING_CHARPOS (*it) >= it->stop_charpos)
8021 if (!(!it->bidi_p
8022 || BIDI_AT_BASE_LEVEL (it->bidi_it)
8023 || IT_STRING_CHARPOS (*it) == it->stop_charpos))
8025 /* With bidi non-linear iteration, we could find
8026 ourselves far beyond the last computed stop_charpos,
8027 with several other stop positions in between that we
8028 missed. Scan them all now, in buffer's logical
8029 order, until we find and handle the last stop_charpos
8030 that precedes our current position. */
8031 handle_stop_backwards (it, it->stop_charpos);
8032 return GET_NEXT_DISPLAY_ELEMENT (it);
8034 else
8036 if (it->bidi_p)
8038 /* Take note of the stop position we just moved
8039 across, for when we will move back across it. */
8040 it->prev_stop = it->stop_charpos;
8041 /* If we are at base paragraph embedding level, take
8042 note of the last stop position seen at this
8043 level. */
8044 if (BIDI_AT_BASE_LEVEL (it->bidi_it))
8045 it->base_level_stop = it->stop_charpos;
8047 handle_stop (it);
8049 /* Since a handler may have changed IT->method, we must
8050 recurse here. */
8051 return GET_NEXT_DISPLAY_ELEMENT (it);
8054 else if (it->bidi_p
8055 /* If we are before prev_stop, we may have overstepped
8056 on our way backwards a stop_pos, and if so, we need
8057 to handle that stop_pos. */
8058 && IT_STRING_CHARPOS (*it) < it->prev_stop
8059 /* We can sometimes back up for reasons that have nothing
8060 to do with bidi reordering. E.g., compositions. The
8061 code below is only needed when we are above the base
8062 embedding level, so test for that explicitly. */
8063 && !BIDI_AT_BASE_LEVEL (it->bidi_it))
8065 /* If we lost track of base_level_stop, we have no better
8066 place for handle_stop_backwards to start from than string
8067 beginning. This happens, e.g., when we were reseated to
8068 the previous screenful of text by vertical-motion. */
8069 if (it->base_level_stop <= 0
8070 || IT_STRING_CHARPOS (*it) < it->base_level_stop)
8071 it->base_level_stop = 0;
8072 handle_stop_backwards (it, it->base_level_stop);
8073 return GET_NEXT_DISPLAY_ELEMENT (it);
8077 if (it->current.overlay_string_index >= 0)
8079 /* Get the next character from an overlay string. In overlay
8080 strings, there is no field width or padding with spaces to
8081 do. */
8082 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
8084 it->what = IT_EOB;
8085 return false;
8087 else if (CHAR_COMPOSED_P (it, IT_STRING_CHARPOS (*it),
8088 IT_STRING_BYTEPOS (*it),
8089 it->bidi_it.scan_dir < 0
8090 ? -1
8091 : SCHARS (it->string))
8092 && next_element_from_composition (it))
8094 return true;
8096 else if (STRING_MULTIBYTE (it->string))
8098 const unsigned char *s = (SDATA (it->string)
8099 + IT_STRING_BYTEPOS (*it));
8100 it->c = string_char_and_length (s, &it->len);
8102 else
8104 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
8105 it->len = 1;
8108 else
8110 /* Get the next character from a Lisp string that is not an
8111 overlay string. Such strings come from the mode line, for
8112 example. We may have to pad with spaces, or truncate the
8113 string. See also next_element_from_c_string. */
8114 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
8116 it->what = IT_EOB;
8117 return false;
8119 else if (IT_STRING_CHARPOS (*it) >= it->string_nchars)
8121 /* Pad with spaces. */
8122 it->c = ' ', it->len = 1;
8123 CHARPOS (position) = BYTEPOS (position) = -1;
8125 else if (CHAR_COMPOSED_P (it, IT_STRING_CHARPOS (*it),
8126 IT_STRING_BYTEPOS (*it),
8127 it->bidi_it.scan_dir < 0
8128 ? -1
8129 : it->string_nchars)
8130 && next_element_from_composition (it))
8132 return true;
8134 else if (STRING_MULTIBYTE (it->string))
8136 const unsigned char *s = (SDATA (it->string)
8137 + IT_STRING_BYTEPOS (*it));
8138 it->c = string_char_and_length (s, &it->len);
8140 else
8142 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
8143 it->len = 1;
8147 /* Record what we have and where it came from. */
8148 it->what = IT_CHARACTER;
8149 it->object = it->string;
8150 it->position = position;
8151 return true;
8155 /* Load IT with next display element from C string IT->s.
8156 IT->string_nchars is the maximum number of characters to return
8157 from the string. IT->end_charpos may be greater than
8158 IT->string_nchars when this function is called, in which case we
8159 may have to return padding spaces. Value is false if end of string
8160 reached, including padding spaces. */
8162 static bool
8163 next_element_from_c_string (struct it *it)
8165 bool success_p = true;
8167 eassert (it->s);
8168 eassert (!it->bidi_p || it->s == it->bidi_it.string.s);
8169 it->what = IT_CHARACTER;
8170 BYTEPOS (it->position) = CHARPOS (it->position) = 0;
8171 it->object = make_number (0);
8173 /* With bidi reordering, the character to display might not be the
8174 character at IT_CHARPOS. BIDI_IT.FIRST_ELT means that
8175 we were reseated to a new string, whose paragraph direction is
8176 not known. */
8177 if (it->bidi_p && it->bidi_it.first_elt)
8178 get_visually_first_element (it);
8180 /* IT's position can be greater than IT->string_nchars in case a
8181 field width or precision has been specified when the iterator was
8182 initialized. */
8183 if (IT_CHARPOS (*it) >= it->end_charpos)
8185 /* End of the game. */
8186 it->what = IT_EOB;
8187 success_p = false;
8189 else if (IT_CHARPOS (*it) >= it->string_nchars)
8191 /* Pad with spaces. */
8192 it->c = ' ', it->len = 1;
8193 BYTEPOS (it->position) = CHARPOS (it->position) = -1;
8195 else if (it->multibyte_p)
8196 it->c = string_char_and_length (it->s + IT_BYTEPOS (*it), &it->len);
8197 else
8198 it->c = it->s[IT_BYTEPOS (*it)], it->len = 1;
8200 return success_p;
8204 /* Set up IT to return characters from an ellipsis, if appropriate.
8205 The definition of the ellipsis glyphs may come from a display table
8206 entry. This function fills IT with the first glyph from the
8207 ellipsis if an ellipsis is to be displayed. */
8209 static bool
8210 next_element_from_ellipsis (struct it *it)
8212 if (it->selective_display_ellipsis_p)
8213 setup_for_ellipsis (it, it->len);
8214 else
8216 /* The face at the current position may be different from the
8217 face we find after the invisible text. Remember what it
8218 was in IT->saved_face_id, and signal that it's there by
8219 setting face_before_selective_p. */
8220 it->saved_face_id = it->face_id;
8221 it->method = GET_FROM_BUFFER;
8222 it->object = it->w->contents;
8223 reseat_at_next_visible_line_start (it, true);
8224 it->face_before_selective_p = true;
8227 return GET_NEXT_DISPLAY_ELEMENT (it);
8231 /* Deliver an image display element. The iterator IT is already
8232 filled with image information (done in handle_display_prop). Value
8233 is always true. */
8236 static bool
8237 next_element_from_image (struct it *it)
8239 it->what = IT_IMAGE;
8240 return true;
8243 static bool
8244 next_element_from_xwidget (struct it *it)
8246 it->what = IT_XWIDGET;
8247 return true;
8251 /* Fill iterator IT with next display element from a stretch glyph
8252 property. IT->object is the value of the text property. Value is
8253 always true. */
8255 static bool
8256 next_element_from_stretch (struct it *it)
8258 it->what = IT_STRETCH;
8259 return true;
8262 /* Scan backwards from IT's current position until we find a stop
8263 position, or until BEGV. This is called when we find ourself
8264 before both the last known prev_stop and base_level_stop while
8265 reordering bidirectional text. */
8267 static void
8268 compute_stop_pos_backwards (struct it *it)
8270 const int SCAN_BACK_LIMIT = 1000;
8271 struct text_pos pos;
8272 struct display_pos save_current = it->current;
8273 struct text_pos save_position = it->position;
8274 ptrdiff_t charpos = IT_CHARPOS (*it);
8275 ptrdiff_t where_we_are = charpos;
8276 ptrdiff_t save_stop_pos = it->stop_charpos;
8277 ptrdiff_t save_end_pos = it->end_charpos;
8279 eassert (NILP (it->string) && !it->s);
8280 eassert (it->bidi_p);
8281 it->bidi_p = false;
8284 it->end_charpos = min (charpos + 1, ZV);
8285 charpos = max (charpos - SCAN_BACK_LIMIT, BEGV);
8286 SET_TEXT_POS (pos, charpos, CHAR_TO_BYTE (charpos));
8287 reseat_1 (it, pos, false);
8288 compute_stop_pos (it);
8289 /* We must advance forward, right? */
8290 if (it->stop_charpos <= charpos)
8291 emacs_abort ();
8293 while (charpos > BEGV && it->stop_charpos >= it->end_charpos);
8295 if (it->stop_charpos <= where_we_are)
8296 it->prev_stop = it->stop_charpos;
8297 else
8298 it->prev_stop = BEGV;
8299 it->bidi_p = true;
8300 it->current = save_current;
8301 it->position = save_position;
8302 it->stop_charpos = save_stop_pos;
8303 it->end_charpos = save_end_pos;
8306 /* Scan forward from CHARPOS in the current buffer/string, until we
8307 find a stop position > current IT's position. Then handle the stop
8308 position before that. This is called when we bump into a stop
8309 position while reordering bidirectional text. CHARPOS should be
8310 the last previously processed stop_pos (or BEGV/0, if none were
8311 processed yet) whose position is less that IT's current
8312 position. */
8314 static void
8315 handle_stop_backwards (struct it *it, ptrdiff_t charpos)
8317 bool bufp = !STRINGP (it->string);
8318 ptrdiff_t where_we_are = (bufp ? IT_CHARPOS (*it) : IT_STRING_CHARPOS (*it));
8319 struct display_pos save_current = it->current;
8320 struct text_pos save_position = it->position;
8321 struct text_pos pos1;
8322 ptrdiff_t next_stop;
8324 /* Scan in strict logical order. */
8325 eassert (it->bidi_p);
8326 it->bidi_p = false;
8329 it->prev_stop = charpos;
8330 if (bufp)
8332 SET_TEXT_POS (pos1, charpos, CHAR_TO_BYTE (charpos));
8333 reseat_1 (it, pos1, false);
8335 else
8336 it->current.string_pos = string_pos (charpos, it->string);
8337 compute_stop_pos (it);
8338 /* We must advance forward, right? */
8339 if (it->stop_charpos <= it->prev_stop)
8340 emacs_abort ();
8341 charpos = it->stop_charpos;
8343 while (charpos <= where_we_are);
8345 it->bidi_p = true;
8346 it->current = save_current;
8347 it->position = save_position;
8348 next_stop = it->stop_charpos;
8349 it->stop_charpos = it->prev_stop;
8350 handle_stop (it);
8351 it->stop_charpos = next_stop;
8354 /* Load IT with the next display element from current_buffer. Value
8355 is false if end of buffer reached. IT->stop_charpos is the next
8356 position at which to stop and check for text properties or buffer
8357 end. */
8359 static bool
8360 next_element_from_buffer (struct it *it)
8362 bool success_p = true;
8364 eassert (IT_CHARPOS (*it) >= BEGV);
8365 eassert (NILP (it->string) && !it->s);
8366 eassert (!it->bidi_p
8367 || (EQ (it->bidi_it.string.lstring, Qnil)
8368 && it->bidi_it.string.s == NULL));
8370 /* With bidi reordering, the character to display might not be the
8371 character at IT_CHARPOS. BIDI_IT.FIRST_ELT means that
8372 we were reseat()ed to a new buffer position, which is potentially
8373 a different paragraph. */
8374 if (it->bidi_p && it->bidi_it.first_elt)
8376 get_visually_first_element (it);
8377 SET_TEXT_POS (it->position, IT_CHARPOS (*it), IT_BYTEPOS (*it));
8380 if (IT_CHARPOS (*it) >= it->stop_charpos)
8382 if (IT_CHARPOS (*it) >= it->end_charpos)
8384 bool overlay_strings_follow_p;
8386 /* End of the game, except when overlay strings follow that
8387 haven't been returned yet. */
8388 if (it->overlay_strings_at_end_processed_p)
8389 overlay_strings_follow_p = false;
8390 else
8392 it->overlay_strings_at_end_processed_p = true;
8393 overlay_strings_follow_p = get_overlay_strings (it, 0);
8396 if (overlay_strings_follow_p)
8397 success_p = GET_NEXT_DISPLAY_ELEMENT (it);
8398 else
8400 it->what = IT_EOB;
8401 it->position = it->current.pos;
8402 success_p = false;
8405 else if (!(!it->bidi_p
8406 || BIDI_AT_BASE_LEVEL (it->bidi_it)
8407 || IT_CHARPOS (*it) == it->stop_charpos))
8409 /* With bidi non-linear iteration, we could find ourselves
8410 far beyond the last computed stop_charpos, with several
8411 other stop positions in between that we missed. Scan
8412 them all now, in buffer's logical order, until we find
8413 and handle the last stop_charpos that precedes our
8414 current position. */
8415 handle_stop_backwards (it, it->stop_charpos);
8416 it->ignore_overlay_strings_at_pos_p = false;
8417 return GET_NEXT_DISPLAY_ELEMENT (it);
8419 else
8421 if (it->bidi_p)
8423 /* Take note of the stop position we just moved across,
8424 for when we will move back across it. */
8425 it->prev_stop = it->stop_charpos;
8426 /* If we are at base paragraph embedding level, take
8427 note of the last stop position seen at this
8428 level. */
8429 if (BIDI_AT_BASE_LEVEL (it->bidi_it))
8430 it->base_level_stop = it->stop_charpos;
8432 handle_stop (it);
8433 it->ignore_overlay_strings_at_pos_p = false;
8434 return GET_NEXT_DISPLAY_ELEMENT (it);
8437 else if (it->bidi_p
8438 /* If we are before prev_stop, we may have overstepped on
8439 our way backwards a stop_pos, and if so, we need to
8440 handle that stop_pos. */
8441 && IT_CHARPOS (*it) < it->prev_stop
8442 /* We can sometimes back up for reasons that have nothing
8443 to do with bidi reordering. E.g., compositions. The
8444 code below is only needed when we are above the base
8445 embedding level, so test for that explicitly. */
8446 && !BIDI_AT_BASE_LEVEL (it->bidi_it))
8448 if (it->base_level_stop <= 0
8449 || IT_CHARPOS (*it) < it->base_level_stop)
8451 /* If we lost track of base_level_stop, we need to find
8452 prev_stop by looking backwards. This happens, e.g., when
8453 we were reseated to the previous screenful of text by
8454 vertical-motion. */
8455 it->base_level_stop = BEGV;
8456 compute_stop_pos_backwards (it);
8457 handle_stop_backwards (it, it->prev_stop);
8459 else
8460 handle_stop_backwards (it, it->base_level_stop);
8461 it->ignore_overlay_strings_at_pos_p = false;
8462 return GET_NEXT_DISPLAY_ELEMENT (it);
8464 else
8466 /* No face changes, overlays etc. in sight, so just return a
8467 character from current_buffer. */
8468 unsigned char *p;
8469 ptrdiff_t stop;
8471 /* We moved to the next buffer position, so any info about
8472 previously seen overlays is no longer valid. */
8473 it->ignore_overlay_strings_at_pos_p = false;
8475 /* Maybe run the redisplay end trigger hook. Performance note:
8476 This doesn't seem to cost measurable time. */
8477 if (it->redisplay_end_trigger_charpos
8478 && it->glyph_row
8479 && IT_CHARPOS (*it) >= it->redisplay_end_trigger_charpos)
8480 run_redisplay_end_trigger_hook (it);
8482 stop = it->bidi_it.scan_dir < 0 ? -1 : it->end_charpos;
8483 if (CHAR_COMPOSED_P (it, IT_CHARPOS (*it), IT_BYTEPOS (*it),
8484 stop)
8485 && next_element_from_composition (it))
8487 return true;
8490 /* Get the next character, maybe multibyte. */
8491 p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
8492 if (it->multibyte_p && !ASCII_CHAR_P (*p))
8493 it->c = STRING_CHAR_AND_LENGTH (p, it->len);
8494 else
8495 it->c = *p, it->len = 1;
8497 /* Record what we have and where it came from. */
8498 it->what = IT_CHARACTER;
8499 it->object = it->w->contents;
8500 it->position = it->current.pos;
8502 /* Normally we return the character found above, except when we
8503 really want to return an ellipsis for selective display. */
8504 if (it->selective)
8506 if (it->c == '\n')
8508 /* A value of selective > 0 means hide lines indented more
8509 than that number of columns. */
8510 if (it->selective > 0
8511 && IT_CHARPOS (*it) + 1 < ZV
8512 && indented_beyond_p (IT_CHARPOS (*it) + 1,
8513 IT_BYTEPOS (*it) + 1,
8514 it->selective))
8516 success_p = next_element_from_ellipsis (it);
8517 it->dpvec_char_len = -1;
8520 else if (it->c == '\r' && it->selective == -1)
8522 /* A value of selective == -1 means that everything from the
8523 CR to the end of the line is invisible, with maybe an
8524 ellipsis displayed for it. */
8525 success_p = next_element_from_ellipsis (it);
8526 it->dpvec_char_len = -1;
8531 /* Value is false if end of buffer reached. */
8532 eassert (!success_p || it->what != IT_CHARACTER || it->len > 0);
8533 return success_p;
8537 /* Run the redisplay end trigger hook for IT. */
8539 static void
8540 run_redisplay_end_trigger_hook (struct it *it)
8542 /* IT->glyph_row should be non-null, i.e. we should be actually
8543 displaying something, or otherwise we should not run the hook. */
8544 eassert (it->glyph_row);
8546 ptrdiff_t charpos = it->redisplay_end_trigger_charpos;
8547 it->redisplay_end_trigger_charpos = 0;
8549 /* Since we are *trying* to run these functions, don't try to run
8550 them again, even if they get an error. */
8551 wset_redisplay_end_trigger (it->w, Qnil);
8552 CALLN (Frun_hook_with_args, Qredisplay_end_trigger_functions, it->window,
8553 make_number (charpos));
8555 /* Notice if it changed the face of the character we are on. */
8556 handle_face_prop (it);
8560 /* Deliver a composition display element. Unlike the other
8561 next_element_from_XXX, this function is not registered in the array
8562 get_next_element[]. It is called from next_element_from_buffer and
8563 next_element_from_string when necessary. */
8565 static bool
8566 next_element_from_composition (struct it *it)
8568 it->what = IT_COMPOSITION;
8569 it->len = it->cmp_it.nbytes;
8570 if (STRINGP (it->string))
8572 if (it->c < 0)
8574 IT_STRING_CHARPOS (*it) += it->cmp_it.nchars;
8575 IT_STRING_BYTEPOS (*it) += it->cmp_it.nbytes;
8576 return false;
8578 it->position = it->current.string_pos;
8579 it->object = it->string;
8580 it->c = composition_update_it (&it->cmp_it, IT_STRING_CHARPOS (*it),
8581 IT_STRING_BYTEPOS (*it), it->string);
8583 else
8585 if (it->c < 0)
8587 IT_CHARPOS (*it) += it->cmp_it.nchars;
8588 IT_BYTEPOS (*it) += it->cmp_it.nbytes;
8589 if (it->bidi_p)
8591 if (it->bidi_it.new_paragraph)
8592 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it,
8593 false);
8594 /* Resync the bidi iterator with IT's new position.
8595 FIXME: this doesn't support bidirectional text. */
8596 while (it->bidi_it.charpos < IT_CHARPOS (*it))
8597 bidi_move_to_visually_next (&it->bidi_it);
8599 return false;
8601 it->position = it->current.pos;
8602 it->object = it->w->contents;
8603 it->c = composition_update_it (&it->cmp_it, IT_CHARPOS (*it),
8604 IT_BYTEPOS (*it), Qnil);
8606 return true;
8611 /***********************************************************************
8612 Moving an iterator without producing glyphs
8613 ***********************************************************************/
8615 /* Check if iterator is at a position corresponding to a valid buffer
8616 position after some move_it_ call. */
8618 #define IT_POS_VALID_AFTER_MOVE_P(it) \
8619 ((it)->method != GET_FROM_STRING || IT_STRING_CHARPOS (*it) == 0)
8622 /* Move iterator IT to a specified buffer or X position within one
8623 line on the display without producing glyphs.
8625 OP should be a bit mask including some or all of these bits:
8626 MOVE_TO_X: Stop upon reaching x-position TO_X.
8627 MOVE_TO_POS: Stop upon reaching buffer or string position TO_CHARPOS.
8628 Regardless of OP's value, stop upon reaching the end of the display line.
8630 TO_X is normally a value 0 <= TO_X <= IT->last_visible_x.
8631 This means, in particular, that TO_X includes window's horizontal
8632 scroll amount.
8634 The return value has several possible values that
8635 say what condition caused the scan to stop:
8637 MOVE_POS_MATCH_OR_ZV
8638 - when TO_POS or ZV was reached.
8640 MOVE_X_REACHED
8641 -when TO_X was reached before TO_POS or ZV were reached.
8643 MOVE_LINE_CONTINUED
8644 - when we reached the end of the display area and the line must
8645 be continued.
8647 MOVE_LINE_TRUNCATED
8648 - when we reached the end of the display area and the line is
8649 truncated.
8651 MOVE_NEWLINE_OR_CR
8652 - when we stopped at a line end, i.e. a newline or a CR and selective
8653 display is on. */
8655 static enum move_it_result
8656 move_it_in_display_line_to (struct it *it,
8657 ptrdiff_t to_charpos, int to_x,
8658 enum move_operation_enum op)
8660 enum move_it_result result = MOVE_UNDEFINED;
8661 struct glyph_row *saved_glyph_row;
8662 struct it wrap_it, atpos_it, atx_it, ppos_it;
8663 void *wrap_data = NULL, *atpos_data = NULL, *atx_data = NULL;
8664 void *ppos_data = NULL;
8665 bool may_wrap = false;
8666 enum it_method prev_method = it->method;
8667 ptrdiff_t closest_pos UNINIT;
8668 ptrdiff_t prev_pos = IT_CHARPOS (*it);
8669 bool saw_smaller_pos = prev_pos < to_charpos;
8670 bool line_number_pending = false;
8672 /* Don't produce glyphs in produce_glyphs. */
8673 saved_glyph_row = it->glyph_row;
8674 it->glyph_row = NULL;
8676 /* Use wrap_it to save a copy of IT wherever a word wrap could
8677 occur. Use atpos_it to save a copy of IT at the desired buffer
8678 position, if found, so that we can scan ahead and check if the
8679 word later overshoots the window edge. Use atx_it similarly, for
8680 pixel positions. */
8681 wrap_it.sp = -1;
8682 atpos_it.sp = -1;
8683 atx_it.sp = -1;
8685 /* Use ppos_it under bidi reordering to save a copy of IT for the
8686 initial position. We restore that position in IT when we have
8687 scanned the entire display line without finding a match for
8688 TO_CHARPOS and all the character positions are greater than
8689 TO_CHARPOS. We then restart the scan from the initial position,
8690 and stop at CLOSEST_POS, which is a position > TO_CHARPOS that is
8691 the closest to TO_CHARPOS. */
8692 if (it->bidi_p)
8694 if ((op & MOVE_TO_POS) && IT_CHARPOS (*it) >= to_charpos)
8696 SAVE_IT (ppos_it, *it, ppos_data);
8697 closest_pos = IT_CHARPOS (*it);
8699 else
8700 closest_pos = ZV;
8703 #define BUFFER_POS_REACHED_P() \
8704 ((op & MOVE_TO_POS) != 0 \
8705 && BUFFERP (it->object) \
8706 && (IT_CHARPOS (*it) == to_charpos \
8707 || ((!it->bidi_p \
8708 || BIDI_AT_BASE_LEVEL (it->bidi_it)) \
8709 && IT_CHARPOS (*it) > to_charpos) \
8710 || (it->what == IT_COMPOSITION \
8711 && ((IT_CHARPOS (*it) > to_charpos \
8712 && to_charpos >= it->cmp_it.charpos) \
8713 || (IT_CHARPOS (*it) < to_charpos \
8714 && to_charpos <= it->cmp_it.charpos)))) \
8715 && (it->method == GET_FROM_BUFFER \
8716 || (it->method == GET_FROM_DISPLAY_VECTOR \
8717 && it->dpvec + it->current.dpvec_index + 1 >= it->dpend)))
8719 if (it->hpos == 0)
8721 /* If line numbers are being displayed, produce a line number. */
8722 if (should_produce_line_number (it))
8724 if (it->current_x == it->first_visible_x)
8725 maybe_produce_line_number (it);
8726 else
8727 line_number_pending = true;
8729 /* If there's a line-/wrap-prefix, handle it. */
8730 if (it->method == GET_FROM_BUFFER)
8731 handle_line_prefix (it);
8734 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8735 SET_TEXT_POS (this_line_min_pos, IT_CHARPOS (*it), IT_BYTEPOS (*it));
8737 while (true)
8739 int x, i, ascent = 0, descent = 0;
8741 /* Utility macro to reset an iterator with x, ascent, and descent. */
8742 #define IT_RESET_X_ASCENT_DESCENT(IT) \
8743 ((IT)->current_x = x, (IT)->max_ascent = ascent, \
8744 (IT)->max_descent = descent)
8746 /* Stop if we move beyond TO_CHARPOS (after an image or a
8747 display string or stretch glyph). */
8748 if ((op & MOVE_TO_POS) != 0
8749 && BUFFERP (it->object)
8750 && it->method == GET_FROM_BUFFER
8751 && (((!it->bidi_p
8752 /* When the iterator is at base embedding level, we
8753 are guaranteed that characters are delivered for
8754 display in strictly increasing order of their
8755 buffer positions. */
8756 || BIDI_AT_BASE_LEVEL (it->bidi_it))
8757 && IT_CHARPOS (*it) > to_charpos)
8758 || (it->bidi_p
8759 && (prev_method == GET_FROM_IMAGE
8760 || prev_method == GET_FROM_STRETCH
8761 || prev_method == GET_FROM_STRING)
8762 /* Passed TO_CHARPOS from left to right. */
8763 && ((prev_pos < to_charpos
8764 && IT_CHARPOS (*it) > to_charpos)
8765 /* Passed TO_CHARPOS from right to left. */
8766 || (prev_pos > to_charpos
8767 && IT_CHARPOS (*it) < to_charpos)))))
8769 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8771 result = MOVE_POS_MATCH_OR_ZV;
8772 break;
8774 else if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
8775 /* If wrap_it is valid, the current position might be in a
8776 word that is wrapped. So, save the iterator in
8777 atpos_it and continue to see if wrapping happens. */
8778 SAVE_IT (atpos_it, *it, atpos_data);
8781 /* Stop when ZV reached.
8782 We used to stop here when TO_CHARPOS reached as well, but that is
8783 too soon if this glyph does not fit on this line. So we handle it
8784 explicitly below. */
8785 if (!get_next_display_element (it))
8787 result = MOVE_POS_MATCH_OR_ZV;
8788 break;
8791 if (it->line_wrap == TRUNCATE)
8793 if (BUFFER_POS_REACHED_P ())
8795 result = MOVE_POS_MATCH_OR_ZV;
8796 break;
8799 else
8801 if (it->line_wrap == WORD_WRAP && it->area == TEXT_AREA)
8803 if (IT_DISPLAYING_WHITESPACE (it))
8804 may_wrap = true;
8805 else if (may_wrap)
8807 /* We have reached a glyph that follows one or more
8808 whitespace characters. If the position is
8809 already found, we are done. */
8810 if (atpos_it.sp >= 0)
8812 RESTORE_IT (it, &atpos_it, atpos_data);
8813 result = MOVE_POS_MATCH_OR_ZV;
8814 goto done;
8816 if (atx_it.sp >= 0)
8818 RESTORE_IT (it, &atx_it, atx_data);
8819 result = MOVE_X_REACHED;
8820 goto done;
8822 /* Otherwise, we can wrap here. */
8823 SAVE_IT (wrap_it, *it, wrap_data);
8824 may_wrap = false;
8829 /* Remember the line height for the current line, in case
8830 the next element doesn't fit on the line. */
8831 ascent = it->max_ascent;
8832 descent = it->max_descent;
8834 /* The call to produce_glyphs will get the metrics of the
8835 display element IT is loaded with. Record the x-position
8836 before this display element, in case it doesn't fit on the
8837 line. */
8838 x = it->current_x;
8840 PRODUCE_GLYPHS (it);
8842 if (it->area != TEXT_AREA)
8844 prev_method = it->method;
8845 if (it->method == GET_FROM_BUFFER)
8846 prev_pos = IT_CHARPOS (*it);
8847 set_iterator_to_next (it, true);
8848 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8849 SET_TEXT_POS (this_line_min_pos,
8850 IT_CHARPOS (*it), IT_BYTEPOS (*it));
8851 if (it->bidi_p
8852 && (op & MOVE_TO_POS)
8853 && IT_CHARPOS (*it) > to_charpos
8854 && IT_CHARPOS (*it) < closest_pos)
8855 closest_pos = IT_CHARPOS (*it);
8856 continue;
8859 /* The number of glyphs we get back in IT->nglyphs will normally
8860 be 1 except when IT->c is (i) a TAB, or (ii) a multi-glyph
8861 character on a terminal frame, or (iii) a line end. For the
8862 second case, IT->nglyphs - 1 padding glyphs will be present.
8863 (On X frames, there is only one glyph produced for a
8864 composite character.)
8866 The behavior implemented below means, for continuation lines,
8867 that as many spaces of a TAB as fit on the current line are
8868 displayed there. For terminal frames, as many glyphs of a
8869 multi-glyph character are displayed in the current line, too.
8870 This is what the old redisplay code did, and we keep it that
8871 way. Under X, the whole shape of a complex character must
8872 fit on the line or it will be completely displayed in the
8873 next line.
8875 Note that both for tabs and padding glyphs, all glyphs have
8876 the same width. */
8877 if (it->nglyphs)
8879 /* More than one glyph or glyph doesn't fit on line. All
8880 glyphs have the same width. */
8881 int single_glyph_width = it->pixel_width / it->nglyphs;
8882 int new_x;
8883 int x_before_this_char = x;
8884 int hpos_before_this_char = it->hpos;
8886 for (i = 0; i < it->nglyphs; ++i, x = new_x)
8888 new_x = x + single_glyph_width;
8890 /* We want to leave anything reaching TO_X to the caller. */
8891 if ((op & MOVE_TO_X) && new_x > to_x)
8893 if (BUFFER_POS_REACHED_P ())
8895 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8896 goto buffer_pos_reached;
8897 if (atpos_it.sp < 0)
8899 SAVE_IT (atpos_it, *it, atpos_data);
8900 IT_RESET_X_ASCENT_DESCENT (&atpos_it);
8903 else
8905 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8907 it->current_x = x;
8908 result = MOVE_X_REACHED;
8909 break;
8911 if (atx_it.sp < 0)
8913 SAVE_IT (atx_it, *it, atx_data);
8914 IT_RESET_X_ASCENT_DESCENT (&atx_it);
8919 if (/* Lines are continued. */
8920 it->line_wrap != TRUNCATE
8921 && (/* And glyph doesn't fit on the line. */
8922 new_x > it->last_visible_x
8923 /* Or it fits exactly and we're on a window
8924 system frame. */
8925 || (new_x == it->last_visible_x
8926 && FRAME_WINDOW_P (it->f)
8927 && ((it->bidi_p && it->bidi_it.paragraph_dir == R2L)
8928 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
8929 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)))))
8931 bool moved_forward = false;
8933 if (/* IT->hpos == 0 means the very first glyph
8934 doesn't fit on the line, e.g. a wide image. */
8935 it->hpos == 0
8936 || (new_x == it->last_visible_x
8937 && FRAME_WINDOW_P (it->f)))
8939 ++it->hpos;
8940 it->current_x = new_x;
8942 /* The character's last glyph just barely fits
8943 in this row. */
8944 if (i == it->nglyphs - 1)
8946 /* If this is the destination position,
8947 return a position *before* it in this row,
8948 now that we know it fits in this row. */
8949 if (BUFFER_POS_REACHED_P ())
8951 bool can_wrap = true;
8953 /* If we are at a whitespace character
8954 that barely fits on this screen line,
8955 but the next character is also
8956 whitespace, we cannot wrap here. */
8957 if (it->line_wrap == WORD_WRAP
8958 && wrap_it.sp >= 0
8959 && may_wrap
8960 && IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
8962 struct it tem_it;
8963 void *tem_data = NULL;
8965 SAVE_IT (tem_it, *it, tem_data);
8966 set_iterator_to_next (it, true);
8967 if (get_next_display_element (it)
8968 && IT_DISPLAYING_WHITESPACE (it))
8969 can_wrap = false;
8970 RESTORE_IT (it, &tem_it, tem_data);
8972 if (it->line_wrap != WORD_WRAP
8973 || wrap_it.sp < 0
8974 /* If we've just found whitespace
8975 where we can wrap, effectively
8976 ignore the previous wrap point --
8977 it is no longer relevant, but we
8978 won't have an opportunity to
8979 update it, since we've reached
8980 the edge of this screen line. */
8981 || (may_wrap && can_wrap
8982 && IT_OVERFLOW_NEWLINE_INTO_FRINGE (it)))
8984 it->hpos = hpos_before_this_char;
8985 it->current_x = x_before_this_char;
8986 result = MOVE_POS_MATCH_OR_ZV;
8987 break;
8989 if (it->line_wrap == WORD_WRAP
8990 && atpos_it.sp < 0)
8992 SAVE_IT (atpos_it, *it, atpos_data);
8993 atpos_it.current_x = x_before_this_char;
8994 atpos_it.hpos = hpos_before_this_char;
8998 prev_method = it->method;
8999 if (it->method == GET_FROM_BUFFER)
9000 prev_pos = IT_CHARPOS (*it);
9001 set_iterator_to_next (it, true);
9002 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
9003 SET_TEXT_POS (this_line_min_pos,
9004 IT_CHARPOS (*it), IT_BYTEPOS (*it));
9005 /* On graphical terminals, newlines may
9006 "overflow" into the fringe if
9007 overflow-newline-into-fringe is non-nil.
9008 On text terminals, and on graphical
9009 terminals with no right margin, newlines
9010 may overflow into the last glyph on the
9011 display line.*/
9012 if (!FRAME_WINDOW_P (it->f)
9013 || ((it->bidi_p
9014 && it->bidi_it.paragraph_dir == R2L)
9015 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
9016 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0
9017 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
9019 if (!get_next_display_element (it))
9021 result = MOVE_POS_MATCH_OR_ZV;
9022 break;
9024 moved_forward = true;
9025 if (BUFFER_POS_REACHED_P ())
9027 if (ITERATOR_AT_END_OF_LINE_P (it))
9028 result = MOVE_POS_MATCH_OR_ZV;
9029 else
9030 result = MOVE_LINE_CONTINUED;
9031 break;
9033 if (ITERATOR_AT_END_OF_LINE_P (it)
9034 && (it->line_wrap != WORD_WRAP
9035 || wrap_it.sp < 0
9036 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it)))
9038 result = MOVE_NEWLINE_OR_CR;
9039 break;
9044 else
9045 IT_RESET_X_ASCENT_DESCENT (it);
9047 /* If the screen line ends with whitespace, and we
9048 are under word-wrap, don't use wrap_it: it is no
9049 longer relevant, but we won't have an opportunity
9050 to update it, since we are done with this screen
9051 line. */
9052 if (may_wrap && IT_OVERFLOW_NEWLINE_INTO_FRINGE (it)
9053 /* If the character after the one which set the
9054 may_wrap flag is also whitespace, we can't
9055 wrap here, since the screen line cannot be
9056 wrapped in the middle of whitespace.
9057 Therefore, wrap_it _is_ relevant in that
9058 case. */
9059 && !(moved_forward && IT_DISPLAYING_WHITESPACE (it)))
9061 /* If we've found TO_X, go back there, as we now
9062 know the last word fits on this screen line. */
9063 if ((op & MOVE_TO_X) && new_x == it->last_visible_x
9064 && atx_it.sp >= 0)
9066 RESTORE_IT (it, &atx_it, atx_data);
9067 atpos_it.sp = -1;
9068 atx_it.sp = -1;
9069 result = MOVE_X_REACHED;
9070 break;
9073 else if (wrap_it.sp >= 0)
9075 RESTORE_IT (it, &wrap_it, wrap_data);
9076 atpos_it.sp = -1;
9077 atx_it.sp = -1;
9080 TRACE_MOVE ((stderr, "move_it_in: continued at %d\n",
9081 IT_CHARPOS (*it)));
9082 result = MOVE_LINE_CONTINUED;
9083 break;
9086 if (BUFFER_POS_REACHED_P ())
9088 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
9089 goto buffer_pos_reached;
9090 if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
9092 SAVE_IT (atpos_it, *it, atpos_data);
9093 IT_RESET_X_ASCENT_DESCENT (&atpos_it);
9097 if (new_x > it->first_visible_x)
9099 /* If we have reached the visible portion of the
9100 screen line, produce the line number if needed. */
9101 if (line_number_pending)
9103 line_number_pending = false;
9104 it->current_x = it->first_visible_x;
9105 maybe_produce_line_number (it);
9106 it->current_x += new_x - it->first_visible_x;
9108 /* Glyph is visible. Increment number of glyphs that
9109 would be displayed. */
9110 ++it->hpos;
9114 if (result != MOVE_UNDEFINED)
9115 break;
9117 else if (BUFFER_POS_REACHED_P ())
9119 buffer_pos_reached:
9120 IT_RESET_X_ASCENT_DESCENT (it);
9121 result = MOVE_POS_MATCH_OR_ZV;
9122 break;
9124 else if ((op & MOVE_TO_X) && it->current_x >= to_x)
9126 /* Stop when TO_X specified and reached. This check is
9127 necessary here because of lines consisting of a line end,
9128 only. The line end will not produce any glyphs and we
9129 would never get MOVE_X_REACHED. */
9130 eassert (it->nglyphs == 0);
9131 result = MOVE_X_REACHED;
9132 break;
9135 /* Is this a line end? If yes, we're done. */
9136 if (ITERATOR_AT_END_OF_LINE_P (it))
9138 /* If we are past TO_CHARPOS, but never saw any character
9139 positions smaller than TO_CHARPOS, return
9140 MOVE_POS_MATCH_OR_ZV, like the unidirectional display
9141 did. */
9142 if (it->bidi_p && (op & MOVE_TO_POS) != 0)
9144 if (!saw_smaller_pos && IT_CHARPOS (*it) > to_charpos)
9146 if (closest_pos < ZV)
9148 RESTORE_IT (it, &ppos_it, ppos_data);
9149 /* Don't recurse if closest_pos is equal to
9150 to_charpos, since we have just tried that. */
9151 if (closest_pos != to_charpos)
9152 move_it_in_display_line_to (it, closest_pos, -1,
9153 MOVE_TO_POS);
9154 result = MOVE_POS_MATCH_OR_ZV;
9156 else
9157 goto buffer_pos_reached;
9159 else if (it->line_wrap == WORD_WRAP && atpos_it.sp >= 0
9160 && IT_CHARPOS (*it) > to_charpos)
9161 goto buffer_pos_reached;
9162 else
9163 result = MOVE_NEWLINE_OR_CR;
9165 else
9166 result = MOVE_NEWLINE_OR_CR;
9167 /* If we've processed the newline, make sure this flag is
9168 reset, as it must only be set when the newline itself is
9169 processed. */
9170 if (result == MOVE_NEWLINE_OR_CR)
9171 it->constrain_row_ascent_descent_p = false;
9172 break;
9175 prev_method = it->method;
9176 if (it->method == GET_FROM_BUFFER)
9177 prev_pos = IT_CHARPOS (*it);
9178 /* The current display element has been consumed. Advance
9179 to the next. */
9180 set_iterator_to_next (it, true);
9181 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
9182 SET_TEXT_POS (this_line_min_pos, IT_CHARPOS (*it), IT_BYTEPOS (*it));
9183 if (IT_CHARPOS (*it) < to_charpos)
9184 saw_smaller_pos = true;
9185 if (it->bidi_p
9186 && (op & MOVE_TO_POS)
9187 && IT_CHARPOS (*it) >= to_charpos
9188 && IT_CHARPOS (*it) < closest_pos)
9189 closest_pos = IT_CHARPOS (*it);
9191 /* Stop if lines are truncated and IT's current x-position is
9192 past the right edge of the window now. */
9193 if (it->line_wrap == TRUNCATE
9194 && it->current_x >= it->last_visible_x)
9196 if (!FRAME_WINDOW_P (it->f)
9197 || ((it->bidi_p && it->bidi_it.paragraph_dir == R2L)
9198 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
9199 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0
9200 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
9202 bool at_eob_p = false;
9204 if ((at_eob_p = !get_next_display_element (it))
9205 || BUFFER_POS_REACHED_P ()
9206 /* If we are past TO_CHARPOS, but never saw any
9207 character positions smaller than TO_CHARPOS,
9208 return MOVE_POS_MATCH_OR_ZV, like the
9209 unidirectional display did. */
9210 || (it->bidi_p && (op & MOVE_TO_POS) != 0
9211 && !saw_smaller_pos
9212 && IT_CHARPOS (*it) > to_charpos))
9214 if (it->bidi_p
9215 && !BUFFER_POS_REACHED_P ()
9216 && !at_eob_p && closest_pos < ZV)
9218 RESTORE_IT (it, &ppos_it, ppos_data);
9219 if (closest_pos != to_charpos)
9220 move_it_in_display_line_to (it, closest_pos, -1,
9221 MOVE_TO_POS);
9223 result = MOVE_POS_MATCH_OR_ZV;
9224 break;
9226 if (ITERATOR_AT_END_OF_LINE_P (it))
9228 result = MOVE_NEWLINE_OR_CR;
9229 break;
9232 else if (it->bidi_p && (op & MOVE_TO_POS) != 0
9233 && !saw_smaller_pos
9234 && IT_CHARPOS (*it) > to_charpos)
9236 if (closest_pos < ZV)
9238 RESTORE_IT (it, &ppos_it, ppos_data);
9239 if (closest_pos != to_charpos)
9240 move_it_in_display_line_to (it, closest_pos, -1,
9241 MOVE_TO_POS);
9243 result = MOVE_POS_MATCH_OR_ZV;
9244 break;
9246 result = MOVE_LINE_TRUNCATED;
9247 break;
9249 #undef IT_RESET_X_ASCENT_DESCENT
9252 #undef BUFFER_POS_REACHED_P
9254 /* If we scanned beyond TO_POS, restore the saved iterator either to
9255 the wrap point (if found), or to atpos/atx location. We decide which
9256 data to use to restore the saved iterator state by their X coordinates,
9257 since buffer positions might increase non-monotonically with screen
9258 coordinates due to bidi reordering. */
9259 if (result == MOVE_LINE_CONTINUED
9260 && it->line_wrap == WORD_WRAP
9261 && wrap_it.sp >= 0
9262 && ((atpos_it.sp >= 0 && wrap_it.current_x < atpos_it.current_x)
9263 || (atx_it.sp >= 0 && wrap_it.current_x < atx_it.current_x)))
9264 RESTORE_IT (it, &wrap_it, wrap_data);
9265 else if (atpos_it.sp >= 0)
9266 RESTORE_IT (it, &atpos_it, atpos_data);
9267 else if (atx_it.sp >= 0)
9268 RESTORE_IT (it, &atx_it, atx_data);
9270 done:
9272 if (atpos_data)
9273 bidi_unshelve_cache (atpos_data, true);
9274 if (atx_data)
9275 bidi_unshelve_cache (atx_data, true);
9276 if (wrap_data)
9277 bidi_unshelve_cache (wrap_data, true);
9278 if (ppos_data)
9279 bidi_unshelve_cache (ppos_data, true);
9281 /* Restore the iterator settings altered at the beginning of this
9282 function. */
9283 it->glyph_row = saved_glyph_row;
9284 return result;
9287 /* For external use. */
9288 void
9289 move_it_in_display_line (struct it *it,
9290 ptrdiff_t to_charpos, int to_x,
9291 enum move_operation_enum op)
9293 if (it->line_wrap == WORD_WRAP
9294 && (op & MOVE_TO_X))
9296 struct it save_it;
9297 void *save_data = NULL;
9298 int skip;
9300 SAVE_IT (save_it, *it, save_data);
9301 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
9302 /* When word-wrap is on, TO_X may lie past the end
9303 of a wrapped line. Then it->current is the
9304 character on the next line, so backtrack to the
9305 space before the wrap point. */
9306 if (skip == MOVE_LINE_CONTINUED)
9308 int prev_x = max (it->current_x - 1, 0);
9309 RESTORE_IT (it, &save_it, save_data);
9310 move_it_in_display_line_to
9311 (it, -1, prev_x, MOVE_TO_X);
9313 else
9314 bidi_unshelve_cache (save_data, true);
9316 else
9317 move_it_in_display_line_to (it, to_charpos, to_x, op);
9321 /* Move IT forward until it satisfies one or more of the criteria in
9322 TO_CHARPOS, TO_X, TO_Y, and TO_VPOS.
9324 OP is a bit-mask that specifies where to stop, and in particular,
9325 which of those four position arguments makes a difference. See the
9326 description of enum move_operation_enum.
9328 If TO_CHARPOS is in invisible text, e.g. a truncated part of a
9329 screen line, this function will set IT to the next position that is
9330 displayed to the right of TO_CHARPOS on the screen.
9332 Return the maximum pixel length of any line scanned but never more
9333 than it.last_visible_x. */
9336 move_it_to (struct it *it, ptrdiff_t to_charpos, int to_x, int to_y, int to_vpos, int op)
9338 enum move_it_result skip, skip2 = MOVE_X_REACHED;
9339 int line_height, line_start_x = 0, reached = 0;
9340 int max_current_x = 0;
9341 void *backup_data = NULL;
9343 for (;;)
9345 if (op & MOVE_TO_VPOS)
9347 /* If no TO_CHARPOS and no TO_X specified, stop at the
9348 start of the line TO_VPOS. */
9349 if ((op & (MOVE_TO_X | MOVE_TO_POS)) == 0)
9351 if (it->vpos == to_vpos)
9353 reached = 1;
9354 break;
9356 else
9357 skip = move_it_in_display_line_to (it, -1, -1, 0);
9359 else
9361 /* TO_VPOS >= 0 means stop at TO_X in the line at
9362 TO_VPOS, or at TO_POS, whichever comes first. */
9363 if (it->vpos == to_vpos)
9365 reached = 2;
9366 break;
9369 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
9371 if (skip == MOVE_POS_MATCH_OR_ZV || it->vpos == to_vpos)
9373 reached = 3;
9374 break;
9376 else if (skip == MOVE_X_REACHED && it->vpos != to_vpos)
9378 /* We have reached TO_X but not in the line we want. */
9379 skip = move_it_in_display_line_to (it, to_charpos,
9380 -1, MOVE_TO_POS);
9381 if (skip == MOVE_POS_MATCH_OR_ZV)
9383 reached = 4;
9384 break;
9389 else if (op & MOVE_TO_Y)
9391 struct it it_backup;
9393 if (it->line_wrap == WORD_WRAP)
9394 SAVE_IT (it_backup, *it, backup_data);
9396 /* TO_Y specified means stop at TO_X in the line containing
9397 TO_Y---or at TO_CHARPOS if this is reached first. The
9398 problem is that we can't really tell whether the line
9399 contains TO_Y before we have completely scanned it, and
9400 this may skip past TO_X. What we do is to first scan to
9401 TO_X.
9403 If TO_X is not specified, use a TO_X of zero. The reason
9404 is to make the outcome of this function more predictable.
9405 If we didn't use TO_X == 0, we would stop at the end of
9406 the line which is probably not what a caller would expect
9407 to happen. */
9408 skip = move_it_in_display_line_to
9409 (it, to_charpos, ((op & MOVE_TO_X) ? to_x : 0),
9410 (MOVE_TO_X | (op & MOVE_TO_POS)));
9412 /* If TO_CHARPOS is reached or ZV, we don't have to do more. */
9413 if (skip == MOVE_POS_MATCH_OR_ZV)
9414 reached = 5;
9415 else if (skip == MOVE_X_REACHED)
9417 /* If TO_X was reached, we want to know whether TO_Y is
9418 in the line. We know this is the case if the already
9419 scanned glyphs make the line tall enough. Otherwise,
9420 we must check by scanning the rest of the line. */
9421 line_height = it->max_ascent + it->max_descent;
9422 if (to_y >= it->current_y
9423 && to_y < it->current_y + line_height)
9425 reached = 6;
9426 break;
9428 SAVE_IT (it_backup, *it, backup_data);
9429 TRACE_MOVE ((stderr, "move_it: from %d\n", IT_CHARPOS (*it)));
9430 skip2 = move_it_in_display_line_to (it, to_charpos, -1,
9431 op & MOVE_TO_POS);
9432 TRACE_MOVE ((stderr, "move_it: to %d\n", IT_CHARPOS (*it)));
9433 line_height = it->max_ascent + it->max_descent;
9434 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
9436 if (to_y >= it->current_y
9437 && to_y < it->current_y + line_height)
9439 /* If TO_Y is in this line and TO_X was reached
9440 above, we scanned too far. We have to restore
9441 IT's settings to the ones before skipping. But
9442 keep the more accurate values of max_ascent and
9443 max_descent we've found while skipping the rest
9444 of the line, for the sake of callers, such as
9445 pos_visible_p, that need to know the line
9446 height. */
9447 int max_ascent = it->max_ascent;
9448 int max_descent = it->max_descent;
9450 RESTORE_IT (it, &it_backup, backup_data);
9451 it->max_ascent = max_ascent;
9452 it->max_descent = max_descent;
9453 reached = 6;
9455 else
9457 skip = skip2;
9458 if (skip == MOVE_POS_MATCH_OR_ZV)
9459 reached = 7;
9462 else
9464 /* Check whether TO_Y is in this line. */
9465 line_height = it->max_ascent + it->max_descent;
9466 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
9468 if (to_y >= it->current_y
9469 && to_y < it->current_y + line_height)
9471 if (to_y > it->current_y)
9472 max_current_x = max (it->current_x, max_current_x);
9474 /* When word-wrap is on, TO_X may lie past the end
9475 of a wrapped line. Then it->current is the
9476 character on the next line, so backtrack to the
9477 space before the wrap point. */
9478 if (skip == MOVE_LINE_CONTINUED
9479 && it->line_wrap == WORD_WRAP)
9481 int prev_x = max (it->current_x - 1, 0);
9482 RESTORE_IT (it, &it_backup, backup_data);
9483 skip = move_it_in_display_line_to
9484 (it, -1, prev_x, MOVE_TO_X);
9487 reached = 6;
9491 if (reached)
9493 max_current_x = max (it->current_x, max_current_x);
9494 break;
9497 else if (BUFFERP (it->object)
9498 && (it->method == GET_FROM_BUFFER
9499 || it->method == GET_FROM_STRETCH)
9500 && IT_CHARPOS (*it) >= to_charpos
9501 /* Under bidi iteration, a call to set_iterator_to_next
9502 can scan far beyond to_charpos if the initial
9503 portion of the next line needs to be reordered. In
9504 that case, give move_it_in_display_line_to another
9505 chance below. */
9506 && !(it->bidi_p
9507 && it->bidi_it.scan_dir == -1))
9508 skip = MOVE_POS_MATCH_OR_ZV;
9509 else
9510 skip = move_it_in_display_line_to (it, to_charpos, -1, MOVE_TO_POS);
9512 switch (skip)
9514 case MOVE_POS_MATCH_OR_ZV:
9515 max_current_x = max (it->current_x, max_current_x);
9516 reached = 8;
9517 goto out;
9519 case MOVE_NEWLINE_OR_CR:
9520 max_current_x = max (it->current_x, max_current_x);
9521 set_iterator_to_next (it, true);
9522 it->continuation_lines_width = 0;
9523 break;
9525 case MOVE_LINE_TRUNCATED:
9526 max_current_x = it->last_visible_x;
9527 it->continuation_lines_width = 0;
9528 reseat_at_next_visible_line_start (it, false);
9529 if ((op & MOVE_TO_POS) != 0
9530 && IT_CHARPOS (*it) > to_charpos)
9532 reached = 9;
9533 goto out;
9535 break;
9537 case MOVE_LINE_CONTINUED:
9538 max_current_x = it->last_visible_x;
9539 /* For continued lines ending in a tab, some of the glyphs
9540 associated with the tab are displayed on the current
9541 line. Since it->current_x does not include these glyphs,
9542 we use it->last_visible_x instead. */
9543 if (it->c == '\t')
9545 it->continuation_lines_width += it->last_visible_x;
9546 /* When moving by vpos, ensure that the iterator really
9547 advances to the next line (bug#847, bug#969). Fixme:
9548 do we need to do this in other circumstances? */
9549 if (it->current_x != it->last_visible_x
9550 && (op & MOVE_TO_VPOS)
9551 && !(op & (MOVE_TO_X | MOVE_TO_POS)))
9553 line_start_x = it->current_x + it->pixel_width
9554 - it->last_visible_x;
9555 if (FRAME_WINDOW_P (it->f))
9557 struct face *face = FACE_FROM_ID (it->f, it->face_id);
9558 struct font *face_font = face->font;
9560 /* When display_line produces a continued line
9561 that ends in a TAB, it skips a tab stop that
9562 is closer than the font's space character
9563 width (see x_produce_glyphs where it produces
9564 the stretch glyph which represents a TAB).
9565 We need to reproduce the same logic here. */
9566 eassert (face_font);
9567 if (face_font)
9569 if (line_start_x < face_font->space_width)
9570 line_start_x
9571 += it->tab_width * face_font->space_width;
9574 set_iterator_to_next (it, false);
9577 else
9578 it->continuation_lines_width += it->current_x;
9579 break;
9581 default:
9582 emacs_abort ();
9585 /* Reset/increment for the next run. */
9586 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
9587 it->current_x = line_start_x;
9588 line_start_x = 0;
9589 it->hpos = 0;
9590 it->current_y += it->max_ascent + it->max_descent;
9591 ++it->vpos;
9592 last_height = it->max_ascent + it->max_descent;
9593 it->max_ascent = it->max_descent = 0;
9596 out:
9598 /* On text terminals, we may stop at the end of a line in the middle
9599 of a multi-character glyph. If the glyph itself is continued,
9600 i.e. it is actually displayed on the next line, don't treat this
9601 stopping point as valid; move to the next line instead (unless
9602 that brings us offscreen). */
9603 if (!FRAME_WINDOW_P (it->f)
9604 && op & MOVE_TO_POS
9605 && IT_CHARPOS (*it) == to_charpos
9606 && it->what == IT_CHARACTER
9607 && it->nglyphs > 1
9608 && it->line_wrap == WINDOW_WRAP
9609 && it->current_x == it->last_visible_x - 1
9610 && it->c != '\n'
9611 && it->c != '\t'
9612 && it->w->window_end_valid
9613 && it->vpos < it->w->window_end_vpos)
9615 it->continuation_lines_width += it->current_x;
9616 it->current_x = it->hpos = it->max_ascent = it->max_descent = 0;
9617 it->current_y += it->max_ascent + it->max_descent;
9618 ++it->vpos;
9619 last_height = it->max_ascent + it->max_descent;
9622 if (backup_data)
9623 bidi_unshelve_cache (backup_data, true);
9625 TRACE_MOVE ((stderr, "move_it_to: reached %d\n", reached));
9627 return max_current_x;
9631 /* Move iterator IT backward by a specified y-distance DY, DY >= 0.
9633 If DY > 0, move IT backward at least that many pixels. DY = 0
9634 means move IT backward to the preceding line start or BEGV. This
9635 function may move over more than DY pixels if IT->current_y - DY
9636 ends up in the middle of a line; in this case IT->current_y will be
9637 set to the top of the line moved to. */
9639 void
9640 move_it_vertically_backward (struct it *it, int dy)
9642 int nlines, h;
9643 struct it it2, it3;
9644 void *it2data = NULL, *it3data = NULL;
9645 ptrdiff_t start_pos;
9646 int nchars_per_row
9647 = (it->last_visible_x - it->first_visible_x) / FRAME_COLUMN_WIDTH (it->f);
9648 ptrdiff_t pos_limit;
9650 move_further_back:
9651 eassert (dy >= 0);
9653 start_pos = IT_CHARPOS (*it);
9655 /* Estimate how many newlines we must move back. */
9656 nlines = max (1, dy / default_line_pixel_height (it->w));
9657 if (it->line_wrap == TRUNCATE || nchars_per_row == 0)
9658 pos_limit = BEGV;
9659 else
9660 pos_limit = max (start_pos - nlines * nchars_per_row, BEGV);
9662 /* Set the iterator's position that many lines back. But don't go
9663 back more than NLINES full screen lines -- this wins a day with
9664 buffers which have very long lines. */
9665 while (nlines-- && IT_CHARPOS (*it) > pos_limit)
9666 back_to_previous_visible_line_start (it);
9668 /* Reseat the iterator here. When moving backward, we don't want
9669 reseat to skip forward over invisible text, set up the iterator
9670 to deliver from overlay strings at the new position etc. So,
9671 use reseat_1 here. */
9672 reseat_1 (it, it->current.pos, true);
9674 /* We are now surely at a line start. */
9675 it->current_x = it->hpos = 0; /* FIXME: this is incorrect when bidi
9676 reordering is in effect. */
9677 it->continuation_lines_width = 0;
9679 /* Move forward and see what y-distance we moved. First move to the
9680 start of the next line so that we get its height. We need this
9681 height to be able to tell whether we reached the specified
9682 y-distance. */
9683 SAVE_IT (it2, *it, it2data);
9684 it2.max_ascent = it2.max_descent = 0;
9687 move_it_to (&it2, start_pos, -1, -1, it2.vpos + 1,
9688 MOVE_TO_POS | MOVE_TO_VPOS);
9690 while (!(IT_POS_VALID_AFTER_MOVE_P (&it2)
9691 /* If we are in a display string which starts at START_POS,
9692 and that display string includes a newline, and we are
9693 right after that newline (i.e. at the beginning of a
9694 display line), exit the loop, because otherwise we will
9695 infloop, since move_it_to will see that it is already at
9696 START_POS and will not move. */
9697 || (it2.method == GET_FROM_STRING
9698 && IT_CHARPOS (it2) == start_pos
9699 && SREF (it2.string, IT_STRING_BYTEPOS (it2) - 1) == '\n')));
9700 eassert (IT_CHARPOS (*it) >= BEGV);
9701 SAVE_IT (it3, it2, it3data);
9703 move_it_to (&it2, start_pos, -1, -1, -1, MOVE_TO_POS);
9704 eassert (IT_CHARPOS (*it) >= BEGV);
9705 /* H is the actual vertical distance from the position in *IT
9706 and the starting position. */
9707 h = it2.current_y - it->current_y;
9708 /* NLINES is the distance in number of lines. */
9709 nlines = it2.vpos - it->vpos;
9711 /* Correct IT's y and vpos position
9712 so that they are relative to the starting point. */
9713 it->vpos -= nlines;
9714 it->current_y -= h;
9716 if (dy == 0)
9718 /* DY == 0 means move to the start of the screen line. The
9719 value of nlines is > 0 if continuation lines were involved,
9720 or if the original IT position was at start of a line. */
9721 RESTORE_IT (it, it, it2data);
9722 if (nlines > 0)
9723 move_it_by_lines (it, nlines);
9724 /* The above code moves us to some position NLINES down,
9725 usually to its first glyph (leftmost in an L2R line), but
9726 that's not necessarily the start of the line, under bidi
9727 reordering. We want to get to the character position
9728 that is immediately after the newline of the previous
9729 line. */
9730 if (it->bidi_p
9731 && !it->continuation_lines_width
9732 && !STRINGP (it->string)
9733 && IT_CHARPOS (*it) > BEGV
9734 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
9736 ptrdiff_t cp = IT_CHARPOS (*it), bp = IT_BYTEPOS (*it);
9738 DEC_BOTH (cp, bp);
9739 cp = find_newline_no_quit (cp, bp, -1, NULL);
9740 move_it_to (it, cp, -1, -1, -1, MOVE_TO_POS);
9742 bidi_unshelve_cache (it3data, true);
9744 else
9746 /* The y-position we try to reach, relative to *IT.
9747 Note that H has been subtracted in front of the if-statement. */
9748 int target_y = it->current_y + h - dy;
9749 int y0 = it3.current_y;
9750 int y1;
9751 int line_height;
9753 RESTORE_IT (&it3, &it3, it3data);
9754 y1 = line_bottom_y (&it3);
9755 line_height = y1 - y0;
9756 RESTORE_IT (it, it, it2data);
9757 /* If we did not reach target_y, try to move further backward if
9758 we can. If we moved too far backward, try to move forward. */
9759 if (target_y < it->current_y
9760 /* This is heuristic. In a window that's 3 lines high, with
9761 a line height of 13 pixels each, recentering with point
9762 on the bottom line will try to move -39/2 = 19 pixels
9763 backward. Try to avoid moving into the first line. */
9764 && (it->current_y - target_y
9765 > min (window_box_height (it->w), line_height * 2 / 3))
9766 && IT_CHARPOS (*it) > BEGV)
9768 TRACE_MOVE ((stderr, " not far enough -> move_vert %d\n",
9769 target_y - it->current_y));
9770 dy = it->current_y - target_y;
9771 goto move_further_back;
9773 else if (target_y >= it->current_y + line_height
9774 && IT_CHARPOS (*it) < ZV)
9776 /* Should move forward by at least one line, maybe more.
9778 Note: Calling move_it_by_lines can be expensive on
9779 terminal frames, where compute_motion is used (via
9780 vmotion) to do the job, when there are very long lines
9781 and truncate-lines is nil. That's the reason for
9782 treating terminal frames specially here. */
9784 if (!FRAME_WINDOW_P (it->f))
9785 move_it_vertically (it, target_y - it->current_y);
9786 else
9790 move_it_by_lines (it, 1);
9792 while (target_y >= line_bottom_y (it) && IT_CHARPOS (*it) < ZV);
9799 /* Move IT by a specified amount of pixel lines DY. DY negative means
9800 move backwards. DY = 0 means move to start of screen line. At the
9801 end, IT will be on the start of a screen line. */
9803 void
9804 move_it_vertically (struct it *it, int dy)
9806 if (dy <= 0)
9807 move_it_vertically_backward (it, -dy);
9808 else
9810 TRACE_MOVE ((stderr, "move_it_v: from %d, %d\n", IT_CHARPOS (*it), dy));
9811 move_it_to (it, ZV, -1, it->current_y + dy, -1,
9812 MOVE_TO_POS | MOVE_TO_Y);
9813 TRACE_MOVE ((stderr, "move_it_v: to %d\n", IT_CHARPOS (*it)));
9815 /* If buffer ends in ZV without a newline, move to the start of
9816 the line to satisfy the post-condition. */
9817 if (IT_CHARPOS (*it) == ZV
9818 && ZV > BEGV
9819 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
9820 move_it_by_lines (it, 0);
9825 /* Move iterator IT past the end of the text line it is in. */
9827 void
9828 move_it_past_eol (struct it *it)
9830 enum move_it_result rc;
9832 rc = move_it_in_display_line_to (it, Z, 0, MOVE_TO_POS);
9833 if (rc == MOVE_NEWLINE_OR_CR)
9834 set_iterator_to_next (it, false);
9838 /* Move IT by a specified number DVPOS of screen lines down. DVPOS
9839 negative means move up. DVPOS == 0 means move to the start of the
9840 screen line.
9842 Optimization idea: If we would know that IT->f doesn't use
9843 a face with proportional font, we could be faster for
9844 truncate-lines nil. */
9846 void
9847 move_it_by_lines (struct it *it, ptrdiff_t dvpos)
9850 /* The commented-out optimization uses vmotion on terminals. This
9851 gives bad results, because elements like it->what, on which
9852 callers such as pos_visible_p rely, aren't updated. */
9853 /* struct position pos;
9854 if (!FRAME_WINDOW_P (it->f))
9856 struct text_pos textpos;
9858 pos = *vmotion (IT_CHARPOS (*it), dvpos, it->w);
9859 SET_TEXT_POS (textpos, pos.bufpos, pos.bytepos);
9860 reseat (it, textpos, true);
9861 it->vpos += pos.vpos;
9862 it->current_y += pos.vpos;
9864 else */
9866 if (dvpos == 0)
9868 /* DVPOS == 0 means move to the start of the screen line. */
9869 move_it_vertically_backward (it, 0);
9870 /* Let next call to line_bottom_y calculate real line height. */
9871 last_height = 0;
9873 else if (dvpos > 0)
9875 move_it_to (it, -1, -1, -1, it->vpos + dvpos, MOVE_TO_VPOS);
9876 if (!IT_POS_VALID_AFTER_MOVE_P (it))
9878 /* Only move to the next buffer position if we ended up in a
9879 string from display property, not in an overlay string
9880 (before-string or after-string). That is because the
9881 latter don't conceal the underlying buffer position, so
9882 we can ask to move the iterator to the exact position we
9883 are interested in. Note that, even if we are already at
9884 IT_CHARPOS (*it), the call below is not a no-op, as it
9885 will detect that we are at the end of the string, pop the
9886 iterator, and compute it->current_x and it->hpos
9887 correctly. */
9888 move_it_to (it, IT_CHARPOS (*it) + it->string_from_display_prop_p,
9889 -1, -1, -1, MOVE_TO_POS);
9892 else
9894 struct it it2;
9895 void *it2data = NULL;
9896 ptrdiff_t start_charpos, i;
9897 int nchars_per_row
9898 = (it->last_visible_x - it->first_visible_x) / FRAME_COLUMN_WIDTH (it->f);
9899 bool hit_pos_limit = false;
9900 ptrdiff_t pos_limit;
9902 /* Start at the beginning of the screen line containing IT's
9903 position. This may actually move vertically backwards,
9904 in case of overlays, so adjust dvpos accordingly. */
9905 dvpos += it->vpos;
9906 move_it_vertically_backward (it, 0);
9907 dvpos -= it->vpos;
9909 /* Go back -DVPOS buffer lines, but no farther than -DVPOS full
9910 screen lines, and reseat the iterator there. */
9911 start_charpos = IT_CHARPOS (*it);
9912 if (it->line_wrap == TRUNCATE || nchars_per_row == 0)
9913 pos_limit = BEGV;
9914 else
9915 pos_limit = max (start_charpos + dvpos * nchars_per_row, BEGV);
9917 for (i = -dvpos; i > 0 && IT_CHARPOS (*it) > pos_limit; --i)
9918 back_to_previous_visible_line_start (it);
9919 if (i > 0 && IT_CHARPOS (*it) <= pos_limit)
9920 hit_pos_limit = true;
9921 reseat (it, it->current.pos, true);
9923 /* Move further back if we end up in a string or an image. */
9924 while (!IT_POS_VALID_AFTER_MOVE_P (it))
9926 /* First try to move to start of display line. */
9927 dvpos += it->vpos;
9928 move_it_vertically_backward (it, 0);
9929 dvpos -= it->vpos;
9930 if (IT_POS_VALID_AFTER_MOVE_P (it))
9931 break;
9932 /* If start of line is still in string or image,
9933 move further back. */
9934 back_to_previous_visible_line_start (it);
9935 reseat (it, it->current.pos, true);
9936 dvpos--;
9939 it->current_x = it->hpos = 0;
9941 /* Above call may have moved too far if continuation lines
9942 are involved. Scan forward and see if it did. */
9943 SAVE_IT (it2, *it, it2data);
9944 it2.vpos = it2.current_y = 0;
9945 move_it_to (&it2, start_charpos, -1, -1, -1, MOVE_TO_POS);
9946 it->vpos -= it2.vpos;
9947 it->current_y -= it2.current_y;
9948 it->current_x = it->hpos = 0;
9950 /* If we moved too far back, move IT some lines forward. */
9951 if (it2.vpos > -dvpos)
9953 int delta = it2.vpos + dvpos;
9955 RESTORE_IT (&it2, &it2, it2data);
9956 SAVE_IT (it2, *it, it2data);
9957 move_it_to (it, -1, -1, -1, it->vpos + delta, MOVE_TO_VPOS);
9958 /* Move back again if we got too far ahead. */
9959 if (IT_CHARPOS (*it) >= start_charpos)
9960 RESTORE_IT (it, &it2, it2data);
9961 else
9962 bidi_unshelve_cache (it2data, true);
9964 else if (hit_pos_limit && pos_limit > BEGV
9965 && dvpos < 0 && it2.vpos < -dvpos)
9967 /* If we hit the limit, but still didn't make it far enough
9968 back, that means there's a display string with a newline
9969 covering a large chunk of text, and that caused
9970 back_to_previous_visible_line_start try to go too far.
9971 Punish those who commit such atrocities by going back
9972 until we've reached DVPOS, after lifting the limit, which
9973 could make it slow for very long lines. "If it hurts,
9974 don't do that!" */
9975 dvpos += it2.vpos;
9976 RESTORE_IT (it, it, it2data);
9977 for (i = -dvpos; i > 0; --i)
9979 back_to_previous_visible_line_start (it);
9980 it->vpos--;
9982 reseat_1 (it, it->current.pos, true);
9984 else
9985 RESTORE_IT (it, it, it2data);
9990 partial_line_height (struct it *it_origin)
9992 int partial_height;
9993 void *it_data = NULL;
9994 struct it it;
9995 SAVE_IT (it, *it_origin, it_data);
9996 move_it_to (&it, ZV, -1, it.last_visible_y, -1,
9997 MOVE_TO_POS | MOVE_TO_Y);
9998 if (it.what == IT_EOB)
10000 int vis_height = it.last_visible_y - it.current_y;
10001 int height = it.ascent + it.descent;
10002 partial_height = (vis_height < height) ? vis_height : 0;
10004 else
10006 int last_line_y = it.current_y;
10007 move_it_by_lines (&it, 1);
10008 partial_height = (it.current_y > it.last_visible_y)
10009 ? it.last_visible_y - last_line_y : 0;
10011 RESTORE_IT (&it, &it, it_data);
10012 return partial_height;
10015 /* Return true if IT points into the middle of a display vector. */
10017 bool
10018 in_display_vector_p (struct it *it)
10020 return (it->method == GET_FROM_DISPLAY_VECTOR
10021 && it->current.dpvec_index > 0
10022 && it->dpvec + it->current.dpvec_index != it->dpend);
10025 DEFUN ("window-text-pixel-size", Fwindow_text_pixel_size, Swindow_text_pixel_size, 0, 6, 0,
10026 doc: /* Return the size of the text of WINDOW's buffer in pixels.
10027 WINDOW must be a live window and defaults to the selected one. The
10028 return value is a cons of the maximum pixel-width of any text line and
10029 the maximum pixel-height of all text lines.
10031 The optional argument FROM, if non-nil, specifies the first text
10032 position and defaults to the minimum accessible position of the buffer.
10033 If FROM is t, use the minimum accessible position that starts a
10034 non-empty line. TO, if non-nil, specifies the last text position and
10035 defaults to the maximum accessible position of the buffer. If TO is t,
10036 use the maximum accessible position that ends a non-empty line.
10038 The optional argument X-LIMIT, if non-nil, specifies the maximum text
10039 width that can be returned. X-LIMIT nil or omitted, means to use the
10040 pixel-width of WINDOW's body; use this if you want to know how high
10041 WINDOW should be become in order to fit all of its buffer's text with
10042 the width of WINDOW unaltered. Use the maximum width WINDOW may assume
10043 if you intend to change WINDOW's width. In any case, text whose
10044 x-coordinate is beyond X-LIMIT is ignored. Since calculating the width
10045 of long lines can take some time, it's always a good idea to make this
10046 argument as small as possible; in particular, if the buffer contains
10047 long lines that shall be truncated anyway.
10049 The optional argument Y-LIMIT, if non-nil, specifies the maximum text
10050 height (excluding the height of the mode- or header-line, if any) that
10051 can be returned. Text lines whose y-coordinate is beyond Y-LIMIT are
10052 ignored. Since calculating the text height of a large buffer can take
10053 some time, it makes sense to specify this argument if the size of the
10054 buffer is large or unknown.
10056 Optional argument MODE-AND-HEADER-LINE nil or omitted means do not
10057 include the height of the mode- or header-line of WINDOW in the return
10058 value. If it is either the symbol `mode-line' or `header-line', include
10059 only the height of that line, if present, in the return value. If t,
10060 include the height of both, if present, in the return value. */)
10061 (Lisp_Object window, Lisp_Object from, Lisp_Object to, Lisp_Object x_limit,
10062 Lisp_Object y_limit, Lisp_Object mode_and_header_line)
10064 struct window *w = decode_live_window (window);
10065 Lisp_Object buffer = w->contents;
10066 struct buffer *b;
10067 struct it it;
10068 struct buffer *old_b = NULL;
10069 ptrdiff_t start, end, pos;
10070 struct text_pos startp;
10071 void *itdata = NULL;
10072 int c, max_x = 0, max_y = 0, x = 0, y = 0;
10074 CHECK_BUFFER (buffer);
10075 b = XBUFFER (buffer);
10077 if (b != current_buffer)
10079 old_b = current_buffer;
10080 set_buffer_internal (b);
10083 if (NILP (from))
10084 start = BEGV;
10085 else if (EQ (from, Qt))
10087 start = pos = BEGV;
10088 while ((pos++ < ZV) && (c = FETCH_CHAR (pos))
10089 && (c == ' ' || c == '\t' || c == '\n' || c == '\r'))
10090 start = pos;
10091 while ((pos-- > BEGV) && (c = FETCH_CHAR (pos)) && (c == ' ' || c == '\t'))
10092 start = pos;
10094 else
10096 CHECK_NUMBER_COERCE_MARKER (from);
10097 start = min (max (XINT (from), BEGV), ZV);
10100 if (NILP (to))
10101 end = ZV;
10102 else if (EQ (to, Qt))
10104 end = pos = ZV;
10105 while ((pos-- > BEGV) && (c = FETCH_CHAR (pos))
10106 && (c == ' ' || c == '\t' || c == '\n' || c == '\r'))
10107 end = pos;
10108 while ((pos++ < ZV) && (c = FETCH_CHAR (pos)) && (c == ' ' || c == '\t'))
10109 end = pos;
10111 else
10113 CHECK_NUMBER_COERCE_MARKER (to);
10114 end = max (start, min (XINT (to), ZV));
10117 if (!NILP (x_limit) && RANGED_INTEGERP (0, x_limit, INT_MAX))
10118 max_x = XINT (x_limit);
10120 if (NILP (y_limit))
10121 max_y = INT_MAX;
10122 else if (RANGED_INTEGERP (0, y_limit, INT_MAX))
10123 max_y = XINT (y_limit);
10125 itdata = bidi_shelve_cache ();
10126 SET_TEXT_POS (startp, start, CHAR_TO_BYTE (start));
10127 start_display (&it, w, startp);
10129 if (NILP (x_limit))
10130 x = move_it_to (&it, end, -1, max_y, -1, MOVE_TO_POS | MOVE_TO_Y);
10131 else
10133 it.last_visible_x = max_x;
10134 /* Actually, we never want move_it_to stop at to_x. But to make
10135 sure that move_it_in_display_line_to always moves far enough,
10136 we set it to INT_MAX and specify MOVE_TO_X. */
10137 x = move_it_to (&it, end, INT_MAX, max_y, -1,
10138 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
10139 /* Don't return more than X-LIMIT. */
10140 if (x > max_x)
10141 x = max_x;
10144 /* Subtract height of header-line which was counted automatically by
10145 start_display. */
10146 y = it.current_y + it.max_ascent + it.max_descent
10147 - WINDOW_HEADER_LINE_HEIGHT (w);
10148 /* Don't return more than Y-LIMIT. */
10149 if (y > max_y)
10150 y = max_y;
10152 if (EQ (mode_and_header_line, Qheader_line)
10153 || EQ (mode_and_header_line, Qt))
10154 /* Re-add height of header-line as requested. */
10155 y = y + WINDOW_HEADER_LINE_HEIGHT (w);
10157 if (EQ (mode_and_header_line, Qmode_line)
10158 || EQ (mode_and_header_line, Qt))
10159 /* Add height of mode-line as requested. */
10160 y = y + WINDOW_MODE_LINE_HEIGHT (w);
10162 bidi_unshelve_cache (itdata, false);
10164 if (old_b)
10165 set_buffer_internal (old_b);
10167 return Fcons (make_number (x), make_number (y));
10170 /***********************************************************************
10171 Messages
10172 ***********************************************************************/
10174 /* Return the number of arguments the format string FORMAT needs. */
10176 static ptrdiff_t
10177 format_nargs (char const *format)
10179 ptrdiff_t nargs = 0;
10180 for (char const *p = format; (p = strchr (p, '%')); p++)
10181 if (p[1] == '%')
10182 p++;
10183 else
10184 nargs++;
10185 return nargs;
10188 /* Add a message with format string FORMAT and formatted arguments
10189 to *Messages*. */
10191 void
10192 add_to_log (const char *format, ...)
10194 va_list ap;
10195 va_start (ap, format);
10196 vadd_to_log (format, ap);
10197 va_end (ap);
10200 void
10201 vadd_to_log (char const *format, va_list ap)
10203 ptrdiff_t form_nargs = format_nargs (format);
10204 ptrdiff_t nargs = 1 + form_nargs;
10205 Lisp_Object args[10];
10206 eassert (nargs <= ARRAYELTS (args));
10207 AUTO_STRING (args0, format);
10208 args[0] = args0;
10209 for (ptrdiff_t i = 1; i <= nargs; i++)
10210 args[i] = va_arg (ap, Lisp_Object);
10211 Lisp_Object msg = Qnil;
10212 msg = Fformat_message (nargs, args);
10214 ptrdiff_t len = SBYTES (msg) + 1;
10215 USE_SAFE_ALLOCA;
10216 char *buffer = SAFE_ALLOCA (len);
10217 memcpy (buffer, SDATA (msg), len);
10219 message_dolog (buffer, len - 1, true, STRING_MULTIBYTE (msg));
10220 SAFE_FREE ();
10224 /* Output a newline in the *Messages* buffer if "needs" one. */
10226 void
10227 message_log_maybe_newline (void)
10229 if (message_log_need_newline)
10230 message_dolog ("", 0, true, false);
10234 /* Add a string M of length NBYTES to the message log, optionally
10235 terminated with a newline when NLFLAG is true. MULTIBYTE, if
10236 true, means interpret the contents of M as multibyte. This
10237 function calls low-level routines in order to bypass text property
10238 hooks, etc. which might not be safe to run.
10240 This may GC (insert may run before/after change hooks),
10241 so the buffer M must NOT point to a Lisp string. */
10243 void
10244 message_dolog (const char *m, ptrdiff_t nbytes, bool nlflag, bool multibyte)
10246 const unsigned char *msg = (const unsigned char *) m;
10248 if (!NILP (Vmemory_full))
10249 return;
10251 if (!NILP (Vmessage_log_max))
10253 struct buffer *oldbuf;
10254 Lisp_Object oldpoint, oldbegv, oldzv;
10255 int old_windows_or_buffers_changed = windows_or_buffers_changed;
10256 ptrdiff_t point_at_end = 0;
10257 ptrdiff_t zv_at_end = 0;
10258 Lisp_Object old_deactivate_mark;
10260 old_deactivate_mark = Vdeactivate_mark;
10261 oldbuf = current_buffer;
10263 /* Ensure the Messages buffer exists, and switch to it.
10264 If we created it, set the major-mode. */
10265 bool newbuffer = NILP (Fget_buffer (Vmessages_buffer_name));
10266 Fset_buffer (Fget_buffer_create (Vmessages_buffer_name));
10267 if (newbuffer
10268 && !NILP (Ffboundp (intern ("messages-buffer-mode"))))
10269 call0 (intern ("messages-buffer-mode"));
10271 bset_undo_list (current_buffer, Qt);
10272 bset_cache_long_scans (current_buffer, Qnil);
10274 oldpoint = message_dolog_marker1;
10275 set_marker_restricted_both (oldpoint, Qnil, PT, PT_BYTE);
10276 oldbegv = message_dolog_marker2;
10277 set_marker_restricted_both (oldbegv, Qnil, BEGV, BEGV_BYTE);
10278 oldzv = message_dolog_marker3;
10279 set_marker_restricted_both (oldzv, Qnil, ZV, ZV_BYTE);
10281 if (PT == Z)
10282 point_at_end = 1;
10283 if (ZV == Z)
10284 zv_at_end = 1;
10286 BEGV = BEG;
10287 BEGV_BYTE = BEG_BYTE;
10288 ZV = Z;
10289 ZV_BYTE = Z_BYTE;
10290 TEMP_SET_PT_BOTH (Z, Z_BYTE);
10292 /* Insert the string--maybe converting multibyte to single byte
10293 or vice versa, so that all the text fits the buffer. */
10294 if (multibyte
10295 && NILP (BVAR (current_buffer, enable_multibyte_characters)))
10297 ptrdiff_t i;
10298 int c, char_bytes;
10299 char work[1];
10301 /* Convert a multibyte string to single-byte
10302 for the *Message* buffer. */
10303 for (i = 0; i < nbytes; i += char_bytes)
10305 c = string_char_and_length (msg + i, &char_bytes);
10306 work[0] = CHAR_TO_BYTE8 (c);
10307 insert_1_both (work, 1, 1, true, false, false);
10310 else if (! multibyte
10311 && ! NILP (BVAR (current_buffer, enable_multibyte_characters)))
10313 ptrdiff_t i;
10314 int c, char_bytes;
10315 unsigned char str[MAX_MULTIBYTE_LENGTH];
10316 /* Convert a single-byte string to multibyte
10317 for the *Message* buffer. */
10318 for (i = 0; i < nbytes; i++)
10320 c = msg[i];
10321 MAKE_CHAR_MULTIBYTE (c);
10322 char_bytes = CHAR_STRING (c, str);
10323 insert_1_both ((char *) str, 1, char_bytes, true, false, false);
10326 else if (nbytes)
10327 insert_1_both (m, chars_in_text (msg, nbytes), nbytes,
10328 true, false, false);
10330 if (nlflag)
10332 ptrdiff_t this_bol, this_bol_byte, prev_bol, prev_bol_byte;
10333 printmax_t dups;
10335 insert_1_both ("\n", 1, 1, true, false, false);
10337 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE, -2, false);
10338 this_bol = PT;
10339 this_bol_byte = PT_BYTE;
10341 /* See if this line duplicates the previous one.
10342 If so, combine duplicates. */
10343 if (this_bol > BEG)
10345 scan_newline (PT, PT_BYTE, BEG, BEG_BYTE, -2, false);
10346 prev_bol = PT;
10347 prev_bol_byte = PT_BYTE;
10349 dups = message_log_check_duplicate (prev_bol_byte,
10350 this_bol_byte);
10351 if (dups)
10353 del_range_both (prev_bol, prev_bol_byte,
10354 this_bol, this_bol_byte, false);
10355 if (dups > 1)
10357 char dupstr[sizeof " [ times]"
10358 + INT_STRLEN_BOUND (printmax_t)];
10360 /* If you change this format, don't forget to also
10361 change message_log_check_duplicate. */
10362 int duplen = sprintf (dupstr, " [%"pMd" times]", dups);
10363 TEMP_SET_PT_BOTH (Z - 1, Z_BYTE - 1);
10364 insert_1_both (dupstr, duplen, duplen,
10365 true, false, true);
10370 /* If we have more than the desired maximum number of lines
10371 in the *Messages* buffer now, delete the oldest ones.
10372 This is safe because we don't have undo in this buffer. */
10374 if (NATNUMP (Vmessage_log_max))
10376 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE,
10377 -XFASTINT (Vmessage_log_max) - 1, false);
10378 del_range_both (BEG, BEG_BYTE, PT, PT_BYTE, false);
10381 BEGV = marker_position (oldbegv);
10382 BEGV_BYTE = marker_byte_position (oldbegv);
10384 if (zv_at_end)
10386 ZV = Z;
10387 ZV_BYTE = Z_BYTE;
10389 else
10391 ZV = marker_position (oldzv);
10392 ZV_BYTE = marker_byte_position (oldzv);
10395 if (point_at_end)
10396 TEMP_SET_PT_BOTH (Z, Z_BYTE);
10397 else
10398 /* We can't do Fgoto_char (oldpoint) because it will run some
10399 Lisp code. */
10400 TEMP_SET_PT_BOTH (marker_position (oldpoint),
10401 marker_byte_position (oldpoint));
10403 unchain_marker (XMARKER (oldpoint));
10404 unchain_marker (XMARKER (oldbegv));
10405 unchain_marker (XMARKER (oldzv));
10407 /* We called insert_1_both above with its 5th argument (PREPARE)
10408 false, which prevents insert_1_both from calling
10409 prepare_to_modify_buffer, which in turns prevents us from
10410 incrementing windows_or_buffers_changed even if *Messages* is
10411 shown in some window. So we must manually set
10412 windows_or_buffers_changed here to make up for that. */
10413 windows_or_buffers_changed = old_windows_or_buffers_changed;
10414 bset_redisplay (current_buffer);
10416 set_buffer_internal (oldbuf);
10418 message_log_need_newline = !nlflag;
10419 Vdeactivate_mark = old_deactivate_mark;
10424 /* We are at the end of the buffer after just having inserted a newline.
10425 (Note: We depend on the fact we won't be crossing the gap.)
10426 Check to see if the most recent message looks a lot like the previous one.
10427 Return 0 if different, 1 if the new one should just replace it, or a
10428 value N > 1 if we should also append " [N times]". */
10430 static intmax_t
10431 message_log_check_duplicate (ptrdiff_t prev_bol_byte, ptrdiff_t this_bol_byte)
10433 ptrdiff_t i;
10434 ptrdiff_t len = Z_BYTE - 1 - this_bol_byte;
10435 bool seen_dots = false;
10436 unsigned char *p1 = BUF_BYTE_ADDRESS (current_buffer, prev_bol_byte);
10437 unsigned char *p2 = BUF_BYTE_ADDRESS (current_buffer, this_bol_byte);
10439 for (i = 0; i < len; i++)
10441 if (i >= 3 && p1[i - 3] == '.' && p1[i - 2] == '.' && p1[i - 1] == '.')
10442 seen_dots = true;
10443 if (p1[i] != p2[i])
10444 return seen_dots;
10446 p1 += len;
10447 if (*p1 == '\n')
10448 return 2;
10449 if (*p1++ == ' ' && *p1++ == '[')
10451 char *pend;
10452 intmax_t n = strtoimax ((char *) p1, &pend, 10);
10453 if (0 < n && n < INTMAX_MAX && strncmp (pend, " times]\n", 8) == 0)
10454 return n + 1;
10456 return 0;
10460 /* Display an echo area message M with a specified length of NBYTES
10461 bytes. The string may include null characters. If M is not a
10462 string, clear out any existing message, and let the mini-buffer
10463 text show through.
10465 This function cancels echoing. */
10467 void
10468 message3 (Lisp_Object m)
10470 clear_message (true, true);
10471 cancel_echoing ();
10473 /* First flush out any partial line written with print. */
10474 message_log_maybe_newline ();
10475 if (STRINGP (m))
10477 ptrdiff_t nbytes = SBYTES (m);
10478 bool multibyte = STRING_MULTIBYTE (m);
10479 char *buffer;
10480 USE_SAFE_ALLOCA;
10481 SAFE_ALLOCA_STRING (buffer, m);
10482 message_dolog (buffer, nbytes, true, multibyte);
10483 SAFE_FREE ();
10485 if (! inhibit_message)
10486 message3_nolog (m);
10489 /* Log the message M to stderr. Log an empty line if M is not a string. */
10491 static void
10492 message_to_stderr (Lisp_Object m)
10494 if (noninteractive_need_newline)
10496 noninteractive_need_newline = false;
10497 fputc ('\n', stderr);
10499 if (STRINGP (m))
10501 Lisp_Object coding_system = Vlocale_coding_system;
10502 Lisp_Object s;
10504 if (!NILP (Vcoding_system_for_write))
10505 coding_system = Vcoding_system_for_write;
10506 if (!NILP (coding_system))
10507 s = code_convert_string_norecord (m, coding_system, true);
10508 else
10509 s = m;
10511 fwrite (SDATA (s), SBYTES (s), 1, stderr);
10513 if (!cursor_in_echo_area)
10514 fputc ('\n', stderr);
10515 fflush (stderr);
10518 /* The non-logging version of message3.
10519 This does not cancel echoing, because it is used for echoing.
10520 Perhaps we need to make a separate function for echoing
10521 and make this cancel echoing. */
10523 void
10524 message3_nolog (Lisp_Object m)
10526 struct frame *sf = SELECTED_FRAME ();
10528 if (FRAME_INITIAL_P (sf))
10529 message_to_stderr (m);
10530 /* Error messages get reported properly by cmd_error, so this must be just an
10531 informative message; if the frame hasn't really been initialized yet, just
10532 toss it. */
10533 else if (INTERACTIVE && sf->glyphs_initialized_p)
10535 /* Get the frame containing the mini-buffer
10536 that the selected frame is using. */
10537 Lisp_Object mini_window = FRAME_MINIBUF_WINDOW (sf);
10538 Lisp_Object frame = XWINDOW (mini_window)->frame;
10539 struct frame *f = XFRAME (frame);
10541 if (FRAME_VISIBLE_P (sf) && !FRAME_VISIBLE_P (f))
10542 Fmake_frame_visible (frame);
10544 if (STRINGP (m) && SCHARS (m) > 0)
10546 set_message (m);
10547 if (minibuffer_auto_raise)
10548 Fraise_frame (frame);
10549 /* Assume we are not echoing.
10550 (If we are, echo_now will override this.) */
10551 echo_message_buffer = Qnil;
10553 else
10554 clear_message (true, true);
10556 do_pending_window_change (false);
10557 echo_area_display (true);
10558 do_pending_window_change (false);
10559 if (FRAME_TERMINAL (f)->frame_up_to_date_hook)
10560 (*FRAME_TERMINAL (f)->frame_up_to_date_hook) (f);
10565 /* Display a null-terminated echo area message M. If M is 0, clear
10566 out any existing message, and let the mini-buffer text show through.
10568 The buffer M must continue to exist until after the echo area gets
10569 cleared or some other message gets displayed there. Do not pass
10570 text that is stored in a Lisp string. Do not pass text in a buffer
10571 that was alloca'd. */
10573 void
10574 message1 (const char *m)
10576 message3 (m ? build_unibyte_string (m) : Qnil);
10580 /* The non-logging counterpart of message1. */
10582 void
10583 message1_nolog (const char *m)
10585 message3_nolog (m ? build_unibyte_string (m) : Qnil);
10588 /* Display a message M which contains a single %s
10589 which gets replaced with STRING. */
10591 void
10592 message_with_string (const char *m, Lisp_Object string, bool log)
10594 CHECK_STRING (string);
10596 bool need_message;
10597 if (noninteractive)
10598 need_message = !!m;
10599 else if (!INTERACTIVE)
10600 need_message = false;
10601 else
10603 /* The frame whose minibuffer we're going to display the message on.
10604 It may be larger than the selected frame, so we need
10605 to use its buffer, not the selected frame's buffer. */
10606 Lisp_Object mini_window;
10607 struct frame *f, *sf = SELECTED_FRAME ();
10609 /* Get the frame containing the minibuffer
10610 that the selected frame is using. */
10611 mini_window = FRAME_MINIBUF_WINDOW (sf);
10612 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
10614 /* Error messages get reported properly by cmd_error, so this must be
10615 just an informative message; if the frame hasn't really been
10616 initialized yet, just toss it. */
10617 need_message = f->glyphs_initialized_p;
10620 if (need_message)
10622 AUTO_STRING (fmt, m);
10623 Lisp_Object msg = CALLN (Fformat_message, fmt, string);
10625 if (noninteractive)
10626 message_to_stderr (msg);
10627 else
10629 if (log)
10630 message3 (msg);
10631 else
10632 message3_nolog (msg);
10634 /* Print should start at the beginning of the message
10635 buffer next time. */
10636 message_buf_print = false;
10642 /* Dump an informative message to the minibuf. If M is 0, clear out
10643 any existing message, and let the mini-buffer text show through.
10645 The message must be safe ASCII (because when Emacs is
10646 non-interactive the message is sent straight to stderr without
10647 encoding first) and the format must not contain ` or ' (because
10648 this function does not account for `text-quoting-style'). If your
10649 message and format do not fit into this category, convert your
10650 arguments to Lisp objects and use Fmessage instead. */
10652 static void ATTRIBUTE_FORMAT_PRINTF (1, 0)
10653 vmessage (const char *m, va_list ap)
10655 if (noninteractive)
10657 if (m)
10659 if (noninteractive_need_newline)
10660 putc ('\n', stderr);
10661 noninteractive_need_newline = false;
10662 vfprintf (stderr, m, ap);
10663 if (!cursor_in_echo_area)
10664 fprintf (stderr, "\n");
10665 fflush (stderr);
10668 else if (INTERACTIVE)
10670 /* The frame whose mini-buffer we're going to display the message
10671 on. It may be larger than the selected frame, so we need to
10672 use its buffer, not the selected frame's buffer. */
10673 Lisp_Object mini_window;
10674 struct frame *f, *sf = SELECTED_FRAME ();
10676 /* Get the frame containing the mini-buffer
10677 that the selected frame is using. */
10678 mini_window = FRAME_MINIBUF_WINDOW (sf);
10679 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
10681 /* Error messages get reported properly by cmd_error, so this must be
10682 just an informative message; if the frame hasn't really been
10683 initialized yet, just toss it. */
10684 if (f->glyphs_initialized_p)
10686 if (m)
10688 ptrdiff_t len;
10689 ptrdiff_t maxsize = FRAME_MESSAGE_BUF_SIZE (f);
10690 USE_SAFE_ALLOCA;
10691 char *message_buf = SAFE_ALLOCA (maxsize + 1);
10693 len = doprnt (message_buf, maxsize, m, 0, ap);
10695 message3 (make_string (message_buf, len));
10696 SAFE_FREE ();
10698 else
10699 message1 (0);
10701 /* Print should start at the beginning of the message
10702 buffer next time. */
10703 message_buf_print = false;
10708 /* See vmessage for restrictions on the text of the message. */
10709 void
10710 message (const char *m, ...)
10712 va_list ap;
10713 va_start (ap, m);
10714 vmessage (m, ap);
10715 va_end (ap);
10719 /* Display the current message in the current mini-buffer. This is
10720 only called from error handlers in process.c, and is not time
10721 critical. */
10723 void
10724 update_echo_area (void)
10726 if (!NILP (echo_area_buffer[0]))
10728 Lisp_Object string;
10729 string = Fcurrent_message ();
10730 message3 (string);
10735 /* Make sure echo area buffers in `echo_buffers' are live.
10736 If they aren't, make new ones. */
10738 static void
10739 ensure_echo_area_buffers (void)
10741 for (int i = 0; i < 2; i++)
10742 if (!BUFFERP (echo_buffer[i])
10743 || !BUFFER_LIVE_P (XBUFFER (echo_buffer[i])))
10745 Lisp_Object old_buffer = echo_buffer[i];
10746 static char const name_fmt[] = " *Echo Area %d*";
10747 char name[sizeof name_fmt + INT_STRLEN_BOUND (int)];
10748 AUTO_STRING_WITH_LEN (lname, name, sprintf (name, name_fmt, i));
10749 echo_buffer[i] = Fget_buffer_create (lname);
10750 bset_truncate_lines (XBUFFER (echo_buffer[i]), Qnil);
10751 /* to force word wrap in echo area -
10752 it was decided to postpone this*/
10753 /* XBUFFER (echo_buffer[i])->word_wrap = Qt; */
10755 for (int j = 0; j < 2; j++)
10756 if (EQ (old_buffer, echo_area_buffer[j]))
10757 echo_area_buffer[j] = echo_buffer[i];
10762 /* Call FN with args A1..A2 with either the current or last displayed
10763 echo_area_buffer as current buffer.
10765 WHICH zero means use the current message buffer
10766 echo_area_buffer[0]. If that is nil, choose a suitable buffer
10767 from echo_buffer[] and clear it.
10769 WHICH > 0 means use echo_area_buffer[1]. If that is nil, choose a
10770 suitable buffer from echo_buffer[] and clear it.
10772 If WHICH < 0, set echo_area_buffer[1] to echo_area_buffer[0], so
10773 that the current message becomes the last displayed one, choose a
10774 suitable buffer for echo_area_buffer[0], and clear it.
10776 Value is what FN returns. */
10778 static bool
10779 with_echo_area_buffer (struct window *w, int which,
10780 bool (*fn) (ptrdiff_t, Lisp_Object),
10781 ptrdiff_t a1, Lisp_Object a2)
10783 Lisp_Object buffer;
10784 bool this_one, the_other, clear_buffer_p, rc;
10785 ptrdiff_t count = SPECPDL_INDEX ();
10787 /* If buffers aren't live, make new ones. */
10788 ensure_echo_area_buffers ();
10790 clear_buffer_p = false;
10792 if (which == 0)
10793 this_one = false, the_other = true;
10794 else if (which > 0)
10795 this_one = true, the_other = false;
10796 else
10798 this_one = false, the_other = true;
10799 clear_buffer_p = true;
10801 /* We need a fresh one in case the current echo buffer equals
10802 the one containing the last displayed echo area message. */
10803 if (!NILP (echo_area_buffer[this_one])
10804 && EQ (echo_area_buffer[this_one], echo_area_buffer[the_other]))
10805 echo_area_buffer[this_one] = Qnil;
10808 /* Choose a suitable buffer from echo_buffer[] if we don't
10809 have one. */
10810 if (NILP (echo_area_buffer[this_one]))
10812 echo_area_buffer[this_one]
10813 = (EQ (echo_area_buffer[the_other], echo_buffer[this_one])
10814 ? echo_buffer[the_other]
10815 : echo_buffer[this_one]);
10816 clear_buffer_p = true;
10819 buffer = echo_area_buffer[this_one];
10821 /* Don't get confused by reusing the buffer used for echoing
10822 for a different purpose. */
10823 if (echo_kboard == NULL && EQ (buffer, echo_message_buffer))
10824 cancel_echoing ();
10826 record_unwind_protect (unwind_with_echo_area_buffer,
10827 with_echo_area_buffer_unwind_data (w));
10829 /* Make the echo area buffer current. Note that for display
10830 purposes, it is not necessary that the displayed window's buffer
10831 == current_buffer, except for text property lookup. So, let's
10832 only set that buffer temporarily here without doing a full
10833 Fset_window_buffer. We must also change w->pointm, though,
10834 because otherwise an assertions in unshow_buffer fails, and Emacs
10835 aborts. */
10836 set_buffer_internal_1 (XBUFFER (buffer));
10837 if (w)
10839 wset_buffer (w, buffer);
10840 set_marker_both (w->pointm, buffer, BEG, BEG_BYTE);
10841 set_marker_both (w->old_pointm, buffer, BEG, BEG_BYTE);
10844 bset_undo_list (current_buffer, Qt);
10845 bset_read_only (current_buffer, Qnil);
10846 specbind (Qinhibit_read_only, Qt);
10847 specbind (Qinhibit_modification_hooks, Qt);
10849 if (clear_buffer_p && Z > BEG)
10850 del_range (BEG, Z);
10852 eassert (BEGV >= BEG);
10853 eassert (ZV <= Z && ZV >= BEGV);
10855 rc = fn (a1, a2);
10857 eassert (BEGV >= BEG);
10858 eassert (ZV <= Z && ZV >= BEGV);
10860 unbind_to (count, Qnil);
10861 return rc;
10865 /* Save state that should be preserved around the call to the function
10866 FN called in with_echo_area_buffer. */
10868 static Lisp_Object
10869 with_echo_area_buffer_unwind_data (struct window *w)
10871 int i = 0;
10872 Lisp_Object vector, tmp;
10874 /* Reduce consing by keeping one vector in
10875 Vwith_echo_area_save_vector. */
10876 vector = Vwith_echo_area_save_vector;
10877 Vwith_echo_area_save_vector = Qnil;
10879 if (NILP (vector))
10880 vector = Fmake_vector (make_number (11), Qnil);
10882 XSETBUFFER (tmp, current_buffer); ASET (vector, i, tmp); ++i;
10883 ASET (vector, i, Vdeactivate_mark); ++i;
10884 ASET (vector, i, make_number (windows_or_buffers_changed)); ++i;
10886 if (w)
10888 XSETWINDOW (tmp, w); ASET (vector, i, tmp); ++i;
10889 ASET (vector, i, w->contents); ++i;
10890 ASET (vector, i, make_number (marker_position (w->pointm))); ++i;
10891 ASET (vector, i, make_number (marker_byte_position (w->pointm))); ++i;
10892 ASET (vector, i, make_number (marker_position (w->old_pointm))); ++i;
10893 ASET (vector, i, make_number (marker_byte_position (w->old_pointm))); ++i;
10894 ASET (vector, i, make_number (marker_position (w->start))); ++i;
10895 ASET (vector, i, make_number (marker_byte_position (w->start))); ++i;
10897 else
10899 int end = i + 8;
10900 for (; i < end; ++i)
10901 ASET (vector, i, Qnil);
10904 eassert (i == ASIZE (vector));
10905 return vector;
10909 /* Restore global state from VECTOR which was created by
10910 with_echo_area_buffer_unwind_data. */
10912 static void
10913 unwind_with_echo_area_buffer (Lisp_Object vector)
10915 set_buffer_internal_1 (XBUFFER (AREF (vector, 0)));
10916 Vdeactivate_mark = AREF (vector, 1);
10917 windows_or_buffers_changed = XFASTINT (AREF (vector, 2));
10919 if (WINDOWP (AREF (vector, 3)))
10921 struct window *w;
10922 Lisp_Object buffer;
10924 w = XWINDOW (AREF (vector, 3));
10925 buffer = AREF (vector, 4);
10927 wset_buffer (w, buffer);
10928 set_marker_both (w->pointm, buffer,
10929 XFASTINT (AREF (vector, 5)),
10930 XFASTINT (AREF (vector, 6)));
10931 set_marker_both (w->old_pointm, buffer,
10932 XFASTINT (AREF (vector, 7)),
10933 XFASTINT (AREF (vector, 8)));
10934 set_marker_both (w->start, buffer,
10935 XFASTINT (AREF (vector, 9)),
10936 XFASTINT (AREF (vector, 10)));
10939 Vwith_echo_area_save_vector = vector;
10943 /* Set up the echo area for use by print functions. MULTIBYTE_P
10944 means we will print multibyte. */
10946 void
10947 setup_echo_area_for_printing (bool multibyte_p)
10949 /* If we can't find an echo area any more, exit. */
10950 if (! FRAME_LIVE_P (XFRAME (selected_frame)))
10951 Fkill_emacs (Qnil);
10953 ensure_echo_area_buffers ();
10955 if (!message_buf_print)
10957 /* A message has been output since the last time we printed.
10958 Choose a fresh echo area buffer. */
10959 if (EQ (echo_area_buffer[1], echo_buffer[0]))
10960 echo_area_buffer[0] = echo_buffer[1];
10961 else
10962 echo_area_buffer[0] = echo_buffer[0];
10964 /* Switch to that buffer and clear it. */
10965 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
10966 bset_truncate_lines (current_buffer, Qnil);
10968 if (Z > BEG)
10970 ptrdiff_t count = SPECPDL_INDEX ();
10971 specbind (Qinhibit_read_only, Qt);
10972 /* Note that undo recording is always disabled. */
10973 del_range (BEG, Z);
10974 unbind_to (count, Qnil);
10976 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
10978 /* Set up the buffer for the multibyteness we need. */
10979 if (multibyte_p
10980 != !NILP (BVAR (current_buffer, enable_multibyte_characters)))
10981 Fset_buffer_multibyte (multibyte_p ? Qt : Qnil);
10983 /* Raise the frame containing the echo area. */
10984 if (minibuffer_auto_raise)
10986 struct frame *sf = SELECTED_FRAME ();
10987 Lisp_Object mini_window;
10988 mini_window = FRAME_MINIBUF_WINDOW (sf);
10989 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
10992 message_log_maybe_newline ();
10993 message_buf_print = true;
10995 else
10997 if (NILP (echo_area_buffer[0]))
10999 if (EQ (echo_area_buffer[1], echo_buffer[0]))
11000 echo_area_buffer[0] = echo_buffer[1];
11001 else
11002 echo_area_buffer[0] = echo_buffer[0];
11005 if (current_buffer != XBUFFER (echo_area_buffer[0]))
11007 /* Someone switched buffers between print requests. */
11008 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
11009 bset_truncate_lines (current_buffer, Qnil);
11015 /* Display an echo area message in window W. Value is true if W's
11016 height is changed. If display_last_displayed_message_p,
11017 display the message that was last displayed, otherwise
11018 display the current message. */
11020 static bool
11021 display_echo_area (struct window *w)
11023 bool no_message_p, window_height_changed_p;
11025 /* Temporarily disable garbage collections while displaying the echo
11026 area. This is done because a GC can print a message itself.
11027 That message would modify the echo area buffer's contents while a
11028 redisplay of the buffer is going on, and seriously confuse
11029 redisplay. */
11030 ptrdiff_t count = inhibit_garbage_collection ();
11032 /* If there is no message, we must call display_echo_area_1
11033 nevertheless because it resizes the window. But we will have to
11034 reset the echo_area_buffer in question to nil at the end because
11035 with_echo_area_buffer will sets it to an empty buffer. */
11036 bool i = display_last_displayed_message_p;
11037 /* According to the C99, C11 and C++11 standards, the integral value
11038 of a "bool" is always 0 or 1, so this array access is safe here,
11039 if oddly typed. */
11040 no_message_p = NILP (echo_area_buffer[i]);
11042 window_height_changed_p
11043 = with_echo_area_buffer (w, display_last_displayed_message_p,
11044 display_echo_area_1,
11045 (intptr_t) w, Qnil);
11047 if (no_message_p)
11048 echo_area_buffer[i] = Qnil;
11050 unbind_to (count, Qnil);
11051 return window_height_changed_p;
11055 /* Helper for display_echo_area. Display the current buffer which
11056 contains the current echo area message in window W, a mini-window,
11057 a pointer to which is passed in A1. A2..A4 are currently not used.
11058 Change the height of W so that all of the message is displayed.
11059 Value is true if height of W was changed. */
11061 static bool
11062 display_echo_area_1 (ptrdiff_t a1, Lisp_Object a2)
11064 intptr_t i1 = a1;
11065 struct window *w = (struct window *) i1;
11066 Lisp_Object window;
11067 struct text_pos start;
11069 /* We are about to enter redisplay without going through
11070 redisplay_internal, so we need to forget these faces by hand
11071 here. */
11072 forget_escape_and_glyphless_faces ();
11074 /* Do this before displaying, so that we have a large enough glyph
11075 matrix for the display. If we can't get enough space for the
11076 whole text, display the last N lines. That works by setting w->start. */
11077 bool window_height_changed_p = resize_mini_window (w, false);
11079 /* Use the starting position chosen by resize_mini_window. */
11080 SET_TEXT_POS_FROM_MARKER (start, w->start);
11082 /* Display. */
11083 clear_glyph_matrix (w->desired_matrix);
11084 XSETWINDOW (window, w);
11085 try_window (window, start, 0);
11087 return window_height_changed_p;
11091 /* Resize the echo area window to exactly the size needed for the
11092 currently displayed message, if there is one. If a mini-buffer
11093 is active, don't shrink it. */
11095 void
11096 resize_echo_area_exactly (void)
11098 if (BUFFERP (echo_area_buffer[0])
11099 && WINDOWP (echo_area_window))
11101 struct window *w = XWINDOW (echo_area_window);
11102 Lisp_Object resize_exactly = (minibuf_level == 0 ? Qt : Qnil);
11103 bool resized_p = with_echo_area_buffer (w, 0, resize_mini_window_1,
11104 (intptr_t) w, resize_exactly);
11105 if (resized_p)
11107 windows_or_buffers_changed = 42;
11108 update_mode_lines = 30;
11109 redisplay_internal ();
11115 /* Callback function for with_echo_area_buffer, when used from
11116 resize_echo_area_exactly. A1 contains a pointer to the window to
11117 resize, EXACTLY non-nil means resize the mini-window exactly to the
11118 size of the text displayed. A3 and A4 are not used. Value is what
11119 resize_mini_window returns. */
11121 static bool
11122 resize_mini_window_1 (ptrdiff_t a1, Lisp_Object exactly)
11124 intptr_t i1 = a1;
11125 return resize_mini_window ((struct window *) i1, !NILP (exactly));
11129 /* Resize mini-window W to fit the size of its contents. EXACT_P
11130 means size the window exactly to the size needed. Otherwise, it's
11131 only enlarged until W's buffer is empty.
11133 Set W->start to the right place to begin display. If the whole
11134 contents fit, start at the beginning. Otherwise, start so as
11135 to make the end of the contents appear. This is particularly
11136 important for y-or-n-p, but seems desirable generally.
11138 Value is true if the window height has been changed. */
11140 bool
11141 resize_mini_window (struct window *w, bool exact_p)
11143 struct frame *f = XFRAME (w->frame);
11144 bool window_height_changed_p = false;
11146 eassert (MINI_WINDOW_P (w));
11148 /* By default, start display at the beginning. */
11149 set_marker_both (w->start, w->contents,
11150 BUF_BEGV (XBUFFER (w->contents)),
11151 BUF_BEGV_BYTE (XBUFFER (w->contents)));
11153 /* Don't resize windows while redisplaying a window; it would
11154 confuse redisplay functions when the size of the window they are
11155 displaying changes from under them. Such a resizing can happen,
11156 for instance, when which-func prints a long message while
11157 we are running fontification-functions. We're running these
11158 functions with safe_call which binds inhibit-redisplay to t. */
11159 if (!NILP (Vinhibit_redisplay))
11160 return false;
11162 /* Nil means don't try to resize. */
11163 if (NILP (Vresize_mini_windows)
11164 || (FRAME_X_P (f) && FRAME_X_OUTPUT (f) == NULL))
11165 return false;
11167 if (!FRAME_MINIBUF_ONLY_P (f))
11169 struct it it;
11170 int total_height = (WINDOW_PIXEL_HEIGHT (XWINDOW (FRAME_ROOT_WINDOW (f)))
11171 + WINDOW_PIXEL_HEIGHT (w));
11172 int unit = FRAME_LINE_HEIGHT (f);
11173 int height, max_height;
11174 struct text_pos start;
11175 struct buffer *old_current_buffer = NULL;
11177 if (current_buffer != XBUFFER (w->contents))
11179 old_current_buffer = current_buffer;
11180 set_buffer_internal (XBUFFER (w->contents));
11183 init_iterator (&it, w, BEGV, BEGV_BYTE, NULL, DEFAULT_FACE_ID);
11185 /* Compute the max. number of lines specified by the user. */
11186 if (FLOATP (Vmax_mini_window_height))
11187 max_height = XFLOAT_DATA (Vmax_mini_window_height) * total_height;
11188 else if (INTEGERP (Vmax_mini_window_height))
11189 max_height = XINT (Vmax_mini_window_height) * unit;
11190 else
11191 max_height = total_height / 4;
11193 /* Correct that max. height if it's bogus. */
11194 max_height = clip_to_bounds (unit, max_height, total_height);
11196 /* Find out the height of the text in the window. */
11197 if (it.line_wrap == TRUNCATE)
11198 height = unit;
11199 else
11201 last_height = 0;
11202 move_it_to (&it, ZV, -1, -1, -1, MOVE_TO_POS);
11203 if (it.max_ascent == 0 && it.max_descent == 0)
11204 height = it.current_y + last_height;
11205 else
11206 height = it.current_y + it.max_ascent + it.max_descent;
11207 height -= min (it.extra_line_spacing, it.max_extra_line_spacing);
11210 /* Compute a suitable window start. */
11211 if (height > max_height)
11213 height = (max_height / unit) * unit;
11214 init_iterator (&it, w, ZV, ZV_BYTE, NULL, DEFAULT_FACE_ID);
11215 move_it_vertically_backward (&it, height - unit);
11216 start = it.current.pos;
11218 else
11219 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
11220 SET_MARKER_FROM_TEXT_POS (w->start, start);
11222 if (EQ (Vresize_mini_windows, Qgrow_only))
11224 /* Let it grow only, until we display an empty message, in which
11225 case the window shrinks again. */
11226 if (height > WINDOW_PIXEL_HEIGHT (w))
11228 int old_height = WINDOW_PIXEL_HEIGHT (w);
11230 FRAME_WINDOWS_FROZEN (f) = true;
11231 grow_mini_window (w, height - WINDOW_PIXEL_HEIGHT (w), true);
11232 window_height_changed_p = WINDOW_PIXEL_HEIGHT (w) != old_height;
11234 else if (height < WINDOW_PIXEL_HEIGHT (w)
11235 && (exact_p || BEGV == ZV))
11237 int old_height = WINDOW_PIXEL_HEIGHT (w);
11239 FRAME_WINDOWS_FROZEN (f) = false;
11240 shrink_mini_window (w, true);
11241 window_height_changed_p = WINDOW_PIXEL_HEIGHT (w) != old_height;
11244 else
11246 /* Always resize to exact size needed. */
11247 if (height > WINDOW_PIXEL_HEIGHT (w))
11249 int old_height = WINDOW_PIXEL_HEIGHT (w);
11251 FRAME_WINDOWS_FROZEN (f) = true;
11252 grow_mini_window (w, height - WINDOW_PIXEL_HEIGHT (w), true);
11253 window_height_changed_p = WINDOW_PIXEL_HEIGHT (w) != old_height;
11255 else if (height < WINDOW_PIXEL_HEIGHT (w))
11257 int old_height = WINDOW_PIXEL_HEIGHT (w);
11259 FRAME_WINDOWS_FROZEN (f) = false;
11260 shrink_mini_window (w, true);
11262 if (height)
11264 FRAME_WINDOWS_FROZEN (f) = true;
11265 grow_mini_window (w, height - WINDOW_PIXEL_HEIGHT (w), true);
11268 window_height_changed_p = WINDOW_PIXEL_HEIGHT (w) != old_height;
11272 if (old_current_buffer)
11273 set_buffer_internal (old_current_buffer);
11276 return window_height_changed_p;
11280 /* Value is the current message, a string, or nil if there is no
11281 current message. */
11283 Lisp_Object
11284 current_message (void)
11286 Lisp_Object msg;
11288 if (!BUFFERP (echo_area_buffer[0]))
11289 msg = Qnil;
11290 else
11292 with_echo_area_buffer (0, 0, current_message_1,
11293 (intptr_t) &msg, Qnil);
11294 if (NILP (msg))
11295 echo_area_buffer[0] = Qnil;
11298 return msg;
11302 static bool
11303 current_message_1 (ptrdiff_t a1, Lisp_Object a2)
11305 intptr_t i1 = a1;
11306 Lisp_Object *msg = (Lisp_Object *) i1;
11308 if (Z > BEG)
11309 *msg = make_buffer_string (BEG, Z, true);
11310 else
11311 *msg = Qnil;
11312 return false;
11316 /* Push the current message on Vmessage_stack for later restoration
11317 by restore_message. Value is true if the current message isn't
11318 empty. This is a relatively infrequent operation, so it's not
11319 worth optimizing. */
11321 bool
11322 push_message (void)
11324 Lisp_Object msg = current_message ();
11325 Vmessage_stack = Fcons (msg, Vmessage_stack);
11326 return STRINGP (msg);
11330 /* Restore message display from the top of Vmessage_stack. */
11332 void
11333 restore_message (void)
11335 eassert (CONSP (Vmessage_stack));
11336 message3_nolog (XCAR (Vmessage_stack));
11340 /* Handler for unwind-protect calling pop_message. */
11342 void
11343 pop_message_unwind (void)
11345 /* Pop the top-most entry off Vmessage_stack. */
11346 eassert (CONSP (Vmessage_stack));
11347 Vmessage_stack = XCDR (Vmessage_stack);
11351 /* Check that Vmessage_stack is nil. Called from emacs.c when Emacs
11352 exits. If the stack is not empty, we have a missing pop_message
11353 somewhere. */
11355 void
11356 check_message_stack (void)
11358 if (!NILP (Vmessage_stack))
11359 emacs_abort ();
11363 /* Truncate to NCHARS what will be displayed in the echo area the next
11364 time we display it---but don't redisplay it now. */
11366 void
11367 truncate_echo_area (ptrdiff_t nchars)
11369 if (nchars == 0)
11370 echo_area_buffer[0] = Qnil;
11371 else if (!noninteractive
11372 && INTERACTIVE
11373 && !NILP (echo_area_buffer[0]))
11375 struct frame *sf = SELECTED_FRAME ();
11376 /* Error messages get reported properly by cmd_error, so this must be
11377 just an informative message; if the frame hasn't really been
11378 initialized yet, just toss it. */
11379 if (sf->glyphs_initialized_p)
11380 with_echo_area_buffer (0, 0, truncate_message_1, nchars, Qnil);
11385 /* Helper function for truncate_echo_area. Truncate the current
11386 message to at most NCHARS characters. */
11388 static bool
11389 truncate_message_1 (ptrdiff_t nchars, Lisp_Object a2)
11391 if (BEG + nchars < Z)
11392 del_range (BEG + nchars, Z);
11393 if (Z == BEG)
11394 echo_area_buffer[0] = Qnil;
11395 return false;
11398 /* Set the current message to STRING. */
11400 static void
11401 set_message (Lisp_Object string)
11403 eassert (STRINGP (string));
11405 message_enable_multibyte = STRING_MULTIBYTE (string);
11407 with_echo_area_buffer (0, -1, set_message_1, 0, string);
11408 message_buf_print = false;
11409 help_echo_showing_p = false;
11411 if (STRINGP (Vdebug_on_message)
11412 && STRINGP (string)
11413 && fast_string_match (Vdebug_on_message, string) >= 0)
11414 call_debugger (list2 (Qerror, string));
11418 /* Helper function for set_message. First argument is ignored and second
11419 argument has the same meaning as for set_message.
11420 This function is called with the echo area buffer being current. */
11422 static bool
11423 set_message_1 (ptrdiff_t a1, Lisp_Object string)
11425 eassert (STRINGP (string));
11427 /* Change multibyteness of the echo buffer appropriately. */
11428 if (message_enable_multibyte
11429 != !NILP (BVAR (current_buffer, enable_multibyte_characters)))
11430 Fset_buffer_multibyte (message_enable_multibyte ? Qt : Qnil);
11432 bset_truncate_lines (current_buffer, message_truncate_lines ? Qt : Qnil);
11433 if (!NILP (BVAR (current_buffer, bidi_display_reordering)))
11434 bset_bidi_paragraph_direction (current_buffer, Qleft_to_right);
11436 /* Insert new message at BEG. */
11437 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
11439 /* This function takes care of single/multibyte conversion.
11440 We just have to ensure that the echo area buffer has the right
11441 setting of enable_multibyte_characters. */
11442 insert_from_string (string, 0, 0, SCHARS (string), SBYTES (string), true);
11444 return false;
11448 /* Clear messages. CURRENT_P means clear the current message.
11449 LAST_DISPLAYED_P means clear the message last displayed. */
11451 void
11452 clear_message (bool current_p, bool last_displayed_p)
11454 if (current_p)
11456 echo_area_buffer[0] = Qnil;
11457 message_cleared_p = true;
11460 if (last_displayed_p)
11461 echo_area_buffer[1] = Qnil;
11463 message_buf_print = false;
11466 /* Clear garbaged frames.
11468 This function is used where the old redisplay called
11469 redraw_garbaged_frames which in turn called redraw_frame which in
11470 turn called clear_frame. The call to clear_frame was a source of
11471 flickering. I believe a clear_frame is not necessary. It should
11472 suffice in the new redisplay to invalidate all current matrices,
11473 and ensure a complete redisplay of all windows. */
11475 static void
11476 clear_garbaged_frames (void)
11478 if (frame_garbaged)
11480 Lisp_Object tail, frame;
11481 struct frame *sf = SELECTED_FRAME ();
11483 FOR_EACH_FRAME (tail, frame)
11485 struct frame *f = XFRAME (frame);
11487 if (FRAME_VISIBLE_P (f) && FRAME_GARBAGED_P (f))
11489 if (f->resized_p
11490 /* It makes no sense to redraw a non-selected TTY
11491 frame, since that will actually clear the
11492 selected frame, and might leave the selected
11493 frame with corrupted display, if it happens not
11494 to be marked garbaged. */
11495 && !(f != sf && (FRAME_TERMCAP_P (f) || FRAME_MSDOS_P (f))))
11496 redraw_frame (f);
11497 else
11498 clear_current_matrices (f);
11500 #if defined (HAVE_WINDOW_SYSTEM) && !defined (HAVE_NS)
11501 x_clear_under_internal_border (f);
11502 #endif /* HAVE_WINDOW_SYSTEM && !HAVE_NS */
11504 fset_redisplay (f);
11505 f->garbaged = false;
11506 f->resized_p = false;
11510 frame_garbaged = false;
11515 /* Redisplay the echo area of the selected frame. If UPDATE_FRAME_P, update
11516 selected_frame. */
11518 static void
11519 echo_area_display (bool update_frame_p)
11521 Lisp_Object mini_window;
11522 struct window *w;
11523 struct frame *f;
11524 bool window_height_changed_p = false;
11525 struct frame *sf = SELECTED_FRAME ();
11527 mini_window = FRAME_MINIBUF_WINDOW (sf);
11528 if (NILP (mini_window))
11529 return;
11531 w = XWINDOW (mini_window);
11532 f = XFRAME (WINDOW_FRAME (w));
11534 /* Don't display if frame is invisible or not yet initialized. */
11535 if (!FRAME_VISIBLE_P (f) || !f->glyphs_initialized_p)
11536 return;
11538 #ifdef HAVE_WINDOW_SYSTEM
11539 /* When Emacs starts, selected_frame may be the initial terminal
11540 frame. If we let this through, a message would be displayed on
11541 the terminal. */
11542 if (FRAME_INITIAL_P (XFRAME (selected_frame)))
11543 return;
11544 #endif /* HAVE_WINDOW_SYSTEM */
11546 /* Redraw garbaged frames. */
11547 clear_garbaged_frames ();
11549 if (!NILP (echo_area_buffer[0]) || minibuf_level == 0)
11551 echo_area_window = mini_window;
11552 window_height_changed_p = display_echo_area (w);
11553 w->must_be_updated_p = true;
11555 /* Update the display, unless called from redisplay_internal.
11556 Also don't update the screen during redisplay itself. The
11557 update will happen at the end of redisplay, and an update
11558 here could cause confusion. */
11559 if (update_frame_p && !redisplaying_p)
11561 int n = 0;
11563 /* If the display update has been interrupted by pending
11564 input, update mode lines in the frame. Due to the
11565 pending input, it might have been that redisplay hasn't
11566 been called, so that mode lines above the echo area are
11567 garbaged. This looks odd, so we prevent it here. */
11568 if (!display_completed)
11570 n = redisplay_mode_lines (FRAME_ROOT_WINDOW (f), false);
11572 #if defined (HAVE_WINDOW_SYSTEM) && !defined (HAVE_NS)
11573 x_clear_under_internal_border (f);
11574 #endif /* HAVE_WINDOW_SYSTEM && !HAVE_NS */
11578 if (window_height_changed_p
11579 /* Don't do this if Emacs is shutting down. Redisplay
11580 needs to run hooks. */
11581 && !NILP (Vrun_hooks))
11583 /* Must update other windows. Likewise as in other
11584 cases, don't let this update be interrupted by
11585 pending input. */
11586 ptrdiff_t count = SPECPDL_INDEX ();
11587 specbind (Qredisplay_dont_pause, Qt);
11588 fset_redisplay (f);
11589 redisplay_internal ();
11590 unbind_to (count, Qnil);
11592 else if (FRAME_WINDOW_P (f) && n == 0)
11594 /* Window configuration is the same as before.
11595 Can do with a display update of the echo area,
11596 unless we displayed some mode lines. */
11597 update_single_window (w);
11598 flush_frame (f);
11600 else
11601 update_frame (f, true, true);
11603 /* If cursor is in the echo area, make sure that the next
11604 redisplay displays the minibuffer, so that the cursor will
11605 be replaced with what the minibuffer wants. */
11606 if (cursor_in_echo_area)
11607 wset_redisplay (XWINDOW (mini_window));
11610 else if (!EQ (mini_window, selected_window))
11611 wset_redisplay (XWINDOW (mini_window));
11613 /* Last displayed message is now the current message. */
11614 echo_area_buffer[1] = echo_area_buffer[0];
11615 /* Inform read_char that we're not echoing. */
11616 echo_message_buffer = Qnil;
11618 /* Prevent redisplay optimization in redisplay_internal by resetting
11619 this_line_start_pos. This is done because the mini-buffer now
11620 displays the message instead of its buffer text. */
11621 if (EQ (mini_window, selected_window))
11622 CHARPOS (this_line_start_pos) = 0;
11624 if (window_height_changed_p)
11626 fset_redisplay (f);
11628 /* If window configuration was changed, frames may have been
11629 marked garbaged. Clear them or we will experience
11630 surprises wrt scrolling.
11631 FIXME: How/why/when? */
11632 clear_garbaged_frames ();
11636 /* True if W's buffer was changed but not saved. */
11638 static bool
11639 window_buffer_changed (struct window *w)
11641 struct buffer *b = XBUFFER (w->contents);
11643 eassert (BUFFER_LIVE_P (b));
11645 return (BUF_SAVE_MODIFF (b) < BUF_MODIFF (b)) != w->last_had_star;
11648 /* True if W has %c or %C in its mode line and mode line should be updated. */
11650 static bool
11651 mode_line_update_needed (struct window *w)
11653 return (w->column_number_displayed != -1
11654 && !(PT == w->last_point && !window_outdated (w))
11655 && (w->column_number_displayed != current_column ()));
11658 /* True if window start of W is frozen and may not be changed during
11659 redisplay. */
11661 static bool
11662 window_frozen_p (struct window *w)
11664 if (FRAME_WINDOWS_FROZEN (XFRAME (WINDOW_FRAME (w))))
11666 Lisp_Object window;
11668 XSETWINDOW (window, w);
11669 if (MINI_WINDOW_P (w))
11670 return false;
11671 else if (EQ (window, selected_window))
11672 return false;
11673 else if (MINI_WINDOW_P (XWINDOW (selected_window))
11674 && EQ (window, Vminibuf_scroll_window))
11675 /* This special window can't be frozen too. */
11676 return false;
11677 else
11678 return true;
11680 return false;
11683 /***********************************************************************
11684 Mode Lines and Frame Titles
11685 ***********************************************************************/
11687 /* A buffer for constructing non-propertized mode-line strings and
11688 frame titles in it; allocated from the heap in init_xdisp and
11689 resized as needed in store_mode_line_noprop_char. */
11691 static char *mode_line_noprop_buf;
11693 /* The buffer's end, and a current output position in it. */
11695 static char *mode_line_noprop_buf_end;
11696 static char *mode_line_noprop_ptr;
11698 #define MODE_LINE_NOPROP_LEN(start) \
11699 ((mode_line_noprop_ptr - mode_line_noprop_buf) - start)
11701 static enum {
11702 MODE_LINE_DISPLAY = 0,
11703 MODE_LINE_TITLE,
11704 MODE_LINE_NOPROP,
11705 MODE_LINE_STRING
11706 } mode_line_target;
11708 /* Alist that caches the results of :propertize.
11709 Each element is (PROPERTIZED-STRING . PROPERTY-LIST). */
11710 static Lisp_Object mode_line_proptrans_alist;
11712 /* List of strings making up the mode-line. */
11713 static Lisp_Object mode_line_string_list;
11715 /* Base face property when building propertized mode line string. */
11716 static Lisp_Object mode_line_string_face;
11717 static Lisp_Object mode_line_string_face_prop;
11720 /* Unwind data for mode line strings */
11722 static Lisp_Object Vmode_line_unwind_vector;
11724 static Lisp_Object
11725 format_mode_line_unwind_data (struct frame *target_frame,
11726 struct buffer *obuf,
11727 Lisp_Object owin,
11728 bool save_proptrans)
11730 Lisp_Object vector, tmp;
11732 /* Reduce consing by keeping one vector in
11733 Vwith_echo_area_save_vector. */
11734 vector = Vmode_line_unwind_vector;
11735 Vmode_line_unwind_vector = Qnil;
11737 if (NILP (vector))
11738 vector = Fmake_vector (make_number (10), Qnil);
11740 ASET (vector, 0, make_number (mode_line_target));
11741 ASET (vector, 1, make_number (MODE_LINE_NOPROP_LEN (0)));
11742 ASET (vector, 2, mode_line_string_list);
11743 ASET (vector, 3, save_proptrans ? mode_line_proptrans_alist : Qt);
11744 ASET (vector, 4, mode_line_string_face);
11745 ASET (vector, 5, mode_line_string_face_prop);
11747 if (obuf)
11748 XSETBUFFER (tmp, obuf);
11749 else
11750 tmp = Qnil;
11751 ASET (vector, 6, tmp);
11752 ASET (vector, 7, owin);
11753 if (target_frame)
11755 /* Similarly to `with-selected-window', if the operation selects
11756 a window on another frame, we must restore that frame's
11757 selected window, and (for a tty) the top-frame. */
11758 ASET (vector, 8, target_frame->selected_window);
11759 if (FRAME_TERMCAP_P (target_frame))
11760 ASET (vector, 9, FRAME_TTY (target_frame)->top_frame);
11763 return vector;
11766 static void
11767 unwind_format_mode_line (Lisp_Object vector)
11769 Lisp_Object old_window = AREF (vector, 7);
11770 Lisp_Object target_frame_window = AREF (vector, 8);
11771 Lisp_Object old_top_frame = AREF (vector, 9);
11773 mode_line_target = XINT (AREF (vector, 0));
11774 mode_line_noprop_ptr = mode_line_noprop_buf + XINT (AREF (vector, 1));
11775 mode_line_string_list = AREF (vector, 2);
11776 if (! EQ (AREF (vector, 3), Qt))
11777 mode_line_proptrans_alist = AREF (vector, 3);
11778 mode_line_string_face = AREF (vector, 4);
11779 mode_line_string_face_prop = AREF (vector, 5);
11781 /* Select window before buffer, since it may change the buffer. */
11782 if (!NILP (old_window))
11784 /* If the operation that we are unwinding had selected a window
11785 on a different frame, reset its frame-selected-window. For a
11786 text terminal, reset its top-frame if necessary. */
11787 if (!NILP (target_frame_window))
11789 Lisp_Object frame
11790 = WINDOW_FRAME (XWINDOW (target_frame_window));
11792 if (!EQ (frame, WINDOW_FRAME (XWINDOW (old_window))))
11793 Fselect_window (target_frame_window, Qt);
11795 if (!NILP (old_top_frame) && !EQ (old_top_frame, frame))
11796 Fselect_frame (old_top_frame, Qt);
11799 Fselect_window (old_window, Qt);
11802 if (!NILP (AREF (vector, 6)))
11804 set_buffer_internal_1 (XBUFFER (AREF (vector, 6)));
11805 ASET (vector, 6, Qnil);
11808 Vmode_line_unwind_vector = vector;
11812 /* Store a single character C for the frame title in mode_line_noprop_buf.
11813 Re-allocate mode_line_noprop_buf if necessary. */
11815 static void
11816 store_mode_line_noprop_char (char c)
11818 /* If output position has reached the end of the allocated buffer,
11819 increase the buffer's size. */
11820 if (mode_line_noprop_ptr == mode_line_noprop_buf_end)
11822 ptrdiff_t len = MODE_LINE_NOPROP_LEN (0);
11823 ptrdiff_t size = len;
11824 mode_line_noprop_buf =
11825 xpalloc (mode_line_noprop_buf, &size, 1, STRING_BYTES_BOUND, 1);
11826 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
11827 mode_line_noprop_ptr = mode_line_noprop_buf + len;
11830 *mode_line_noprop_ptr++ = c;
11834 /* Store part of a frame title in mode_line_noprop_buf, beginning at
11835 mode_line_noprop_ptr. STRING is the string to store. Do not copy
11836 characters that yield more columns than PRECISION; PRECISION <= 0
11837 means copy the whole string. Pad with spaces until FIELD_WIDTH
11838 number of characters have been copied; FIELD_WIDTH <= 0 means don't
11839 pad. Called from display_mode_element when it is used to build a
11840 frame title. */
11842 static int
11843 store_mode_line_noprop (const char *string, int field_width, int precision)
11845 const unsigned char *str = (const unsigned char *) string;
11846 int n = 0;
11847 ptrdiff_t dummy, nbytes;
11849 /* Copy at most PRECISION chars from STR. */
11850 nbytes = strlen (string);
11851 n += c_string_width (str, nbytes, precision, &dummy, &nbytes);
11852 while (nbytes--)
11853 store_mode_line_noprop_char (*str++);
11855 /* Fill up with spaces until FIELD_WIDTH reached. */
11856 while (field_width > 0
11857 && n < field_width)
11859 store_mode_line_noprop_char (' ');
11860 ++n;
11863 return n;
11866 /***********************************************************************
11867 Frame Titles
11868 ***********************************************************************/
11870 #ifdef HAVE_WINDOW_SYSTEM
11872 /* Set the title of FRAME, if it has changed. The title format is
11873 Vicon_title_format if FRAME is iconified, otherwise it is
11874 frame_title_format. */
11876 static void
11877 x_consider_frame_title (Lisp_Object frame)
11879 struct frame *f = XFRAME (frame);
11881 if ((FRAME_WINDOW_P (f)
11882 || FRAME_MINIBUF_ONLY_P (f)
11883 || f->explicit_name)
11884 && NILP (Fframe_parameter (frame, Qtooltip)))
11886 /* Do we have more than one visible frame on this X display? */
11887 Lisp_Object tail, other_frame, fmt;
11888 ptrdiff_t title_start;
11889 char *title;
11890 ptrdiff_t len;
11891 struct it it;
11892 ptrdiff_t count = SPECPDL_INDEX ();
11894 FOR_EACH_FRAME (tail, other_frame)
11896 struct frame *tf = XFRAME (other_frame);
11898 if (tf != f
11899 && FRAME_KBOARD (tf) == FRAME_KBOARD (f)
11900 && !FRAME_MINIBUF_ONLY_P (tf)
11901 && !EQ (other_frame, tip_frame)
11902 && !FRAME_PARENT_FRAME (tf)
11903 && (FRAME_VISIBLE_P (tf) || FRAME_ICONIFIED_P (tf)))
11904 break;
11907 /* Set global variable indicating that multiple frames exist. */
11908 multiple_frames = CONSP (tail);
11910 /* Switch to the buffer of selected window of the frame. Set up
11911 mode_line_target so that display_mode_element will output into
11912 mode_line_noprop_buf; then display the title. */
11913 record_unwind_protect (unwind_format_mode_line,
11914 format_mode_line_unwind_data
11915 (f, current_buffer, selected_window, false));
11916 /* select-frame calls resize_mini_window, which could resize the
11917 mini-window and by that undo the effect of this redisplay
11918 cycle wrt minibuffer and echo-area display. Binding
11919 inhibit-redisplay to t makes the call to resize_mini_window a
11920 no-op, thus avoiding the adverse side effects. */
11921 specbind (Qinhibit_redisplay, Qt);
11923 Fselect_window (f->selected_window, Qt);
11924 set_buffer_internal_1
11925 (XBUFFER (XWINDOW (f->selected_window)->contents));
11926 fmt = FRAME_ICONIFIED_P (f) ? Vicon_title_format : Vframe_title_format;
11928 mode_line_target = MODE_LINE_TITLE;
11929 title_start = MODE_LINE_NOPROP_LEN (0);
11930 init_iterator (&it, XWINDOW (f->selected_window), -1, -1,
11931 NULL, DEFAULT_FACE_ID);
11932 display_mode_element (&it, 0, -1, -1, fmt, Qnil, false);
11933 len = MODE_LINE_NOPROP_LEN (title_start);
11934 title = mode_line_noprop_buf + title_start;
11935 unbind_to (count, Qnil);
11937 /* Set the title only if it's changed. This avoids consing in
11938 the common case where it hasn't. (If it turns out that we've
11939 already wasted too much time by walking through the list with
11940 display_mode_element, then we might need to optimize at a
11941 higher level than this.) */
11942 if (! STRINGP (f->name)
11943 || SBYTES (f->name) != len
11944 || memcmp (title, SDATA (f->name), len) != 0)
11945 x_implicitly_set_name (f, make_string (title, len), Qnil);
11949 #endif /* not HAVE_WINDOW_SYSTEM */
11952 /***********************************************************************
11953 Menu Bars
11954 ***********************************************************************/
11956 /* True if we will not redisplay all visible windows. */
11957 #define REDISPLAY_SOME_P() \
11958 ((windows_or_buffers_changed == 0 \
11959 || windows_or_buffers_changed == REDISPLAY_SOME) \
11960 && (update_mode_lines == 0 \
11961 || update_mode_lines == REDISPLAY_SOME))
11963 /* Prepare for redisplay by updating menu-bar item lists when
11964 appropriate. This can call eval. */
11966 static void
11967 prepare_menu_bars (void)
11969 bool all_windows = windows_or_buffers_changed || update_mode_lines;
11970 bool some_windows = REDISPLAY_SOME_P ();
11971 Lisp_Object tooltip_frame;
11973 #ifdef HAVE_WINDOW_SYSTEM
11974 tooltip_frame = tip_frame;
11975 #else
11976 tooltip_frame = Qnil;
11977 #endif
11979 if (FUNCTIONP (Vpre_redisplay_function))
11981 Lisp_Object windows = all_windows ? Qt : Qnil;
11982 if (all_windows && some_windows)
11984 Lisp_Object ws = window_list ();
11985 for (windows = Qnil; CONSP (ws); ws = XCDR (ws))
11987 Lisp_Object this = XCAR (ws);
11988 struct window *w = XWINDOW (this);
11989 if (w->redisplay
11990 || XFRAME (w->frame)->redisplay
11991 || XBUFFER (w->contents)->text->redisplay)
11993 windows = Fcons (this, windows);
11997 safe__call1 (true, Vpre_redisplay_function, windows);
12000 /* Update all frame titles based on their buffer names, etc. We do
12001 this before the menu bars so that the buffer-menu will show the
12002 up-to-date frame titles. */
12003 #ifdef HAVE_WINDOW_SYSTEM
12004 if (all_windows)
12006 Lisp_Object tail, frame;
12008 FOR_EACH_FRAME (tail, frame)
12010 struct frame *f = XFRAME (frame);
12011 struct window *w = XWINDOW (FRAME_SELECTED_WINDOW (f));
12012 if (some_windows
12013 && !f->redisplay
12014 && !w->redisplay
12015 && !XBUFFER (w->contents)->text->redisplay)
12016 continue;
12018 if (!EQ (frame, tooltip_frame)
12019 && !FRAME_PARENT_FRAME (f)
12020 && (FRAME_ICONIFIED_P (f)
12021 || FRAME_VISIBLE_P (f) == 1
12022 /* Exclude TTY frames that are obscured because they
12023 are not the top frame on their console. This is
12024 because x_consider_frame_title actually switches
12025 to the frame, which for TTY frames means it is
12026 marked as garbaged, and will be completely
12027 redrawn on the next redisplay cycle. This causes
12028 TTY frames to be completely redrawn, when there
12029 are more than one of them, even though nothing
12030 should be changed on display. */
12031 || (FRAME_VISIBLE_P (f) == 2 && FRAME_WINDOW_P (f))))
12032 x_consider_frame_title (frame);
12035 #endif /* HAVE_WINDOW_SYSTEM */
12037 /* Update the menu bar item lists, if appropriate. This has to be
12038 done before any actual redisplay or generation of display lines. */
12040 if (all_windows)
12042 Lisp_Object tail, frame;
12043 ptrdiff_t count = SPECPDL_INDEX ();
12044 /* True means that update_menu_bar has run its hooks
12045 so any further calls to update_menu_bar shouldn't do so again. */
12046 bool menu_bar_hooks_run = false;
12048 record_unwind_save_match_data ();
12050 FOR_EACH_FRAME (tail, frame)
12052 struct frame *f = XFRAME (frame);
12053 struct window *w = XWINDOW (FRAME_SELECTED_WINDOW (f));
12055 /* Ignore tooltip frame. */
12056 if (EQ (frame, tooltip_frame))
12057 continue;
12059 if (some_windows
12060 && !f->redisplay
12061 && !w->redisplay
12062 && !XBUFFER (w->contents)->text->redisplay)
12063 continue;
12065 run_window_size_change_functions (frame);
12067 if (FRAME_PARENT_FRAME (f))
12068 continue;
12070 menu_bar_hooks_run = update_menu_bar (f, false, menu_bar_hooks_run);
12071 #ifdef HAVE_WINDOW_SYSTEM
12072 update_tool_bar (f, false);
12073 #endif
12076 unbind_to (count, Qnil);
12078 else
12080 struct frame *sf = SELECTED_FRAME ();
12081 update_menu_bar (sf, true, false);
12082 #ifdef HAVE_WINDOW_SYSTEM
12083 update_tool_bar (sf, true);
12084 #endif
12089 /* Update the menu bar item list for frame F. This has to be done
12090 before we start to fill in any display lines, because it can call
12091 eval.
12093 If SAVE_MATCH_DATA, we must save and restore it here.
12095 If HOOKS_RUN, a previous call to update_menu_bar
12096 already ran the menu bar hooks for this redisplay, so there
12097 is no need to run them again. The return value is the
12098 updated value of this flag, to pass to the next call. */
12100 static bool
12101 update_menu_bar (struct frame *f, bool save_match_data, bool hooks_run)
12103 Lisp_Object window;
12104 struct window *w;
12106 /* If called recursively during a menu update, do nothing. This can
12107 happen when, for instance, an activate-menubar-hook causes a
12108 redisplay. */
12109 if (inhibit_menubar_update)
12110 return hooks_run;
12112 window = FRAME_SELECTED_WINDOW (f);
12113 w = XWINDOW (window);
12115 if (FRAME_WINDOW_P (f)
12117 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
12118 || defined (HAVE_NS) || defined (USE_GTK)
12119 FRAME_EXTERNAL_MENU_BAR (f)
12120 #else
12121 FRAME_MENU_BAR_LINES (f) > 0
12122 #endif
12123 : FRAME_MENU_BAR_LINES (f) > 0)
12125 /* If the user has switched buffers or windows, we need to
12126 recompute to reflect the new bindings. But we'll
12127 recompute when update_mode_lines is set too; that means
12128 that people can use force-mode-line-update to request
12129 that the menu bar be recomputed. The adverse effect on
12130 the rest of the redisplay algorithm is about the same as
12131 windows_or_buffers_changed anyway. */
12132 if (windows_or_buffers_changed
12133 /* This used to test w->update_mode_line, but we believe
12134 there is no need to recompute the menu in that case. */
12135 || update_mode_lines
12136 || window_buffer_changed (w))
12138 struct buffer *prev = current_buffer;
12139 ptrdiff_t count = SPECPDL_INDEX ();
12141 specbind (Qinhibit_menubar_update, Qt);
12143 set_buffer_internal_1 (XBUFFER (w->contents));
12144 if (save_match_data)
12145 record_unwind_save_match_data ();
12146 if (NILP (Voverriding_local_map_menu_flag))
12148 specbind (Qoverriding_terminal_local_map, Qnil);
12149 specbind (Qoverriding_local_map, Qnil);
12152 if (!hooks_run)
12154 /* Run the Lucid hook. */
12155 safe_run_hooks (Qactivate_menubar_hook);
12157 /* If it has changed current-menubar from previous value,
12158 really recompute the menu-bar from the value. */
12159 if (! NILP (Vlucid_menu_bar_dirty_flag))
12160 call0 (Qrecompute_lucid_menubar);
12162 safe_run_hooks (Qmenu_bar_update_hook);
12164 hooks_run = true;
12167 XSETFRAME (Vmenu_updating_frame, f);
12168 fset_menu_bar_items (f, menu_bar_items (FRAME_MENU_BAR_ITEMS (f)));
12170 /* Redisplay the menu bar in case we changed it. */
12171 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
12172 || defined (HAVE_NS) || defined (USE_GTK)
12173 if (FRAME_WINDOW_P (f))
12175 #if defined (HAVE_NS)
12176 /* All frames on Mac OS share the same menubar. So only
12177 the selected frame should be allowed to set it. */
12178 if (f == SELECTED_FRAME ())
12179 #endif
12180 set_frame_menubar (f, false, false);
12182 else
12183 /* On a terminal screen, the menu bar is an ordinary screen
12184 line, and this makes it get updated. */
12185 w->update_mode_line = true;
12186 #else /* ! (USE_X_TOOLKIT || HAVE_NTGUI || HAVE_NS || USE_GTK) */
12187 /* In the non-toolkit version, the menu bar is an ordinary screen
12188 line, and this makes it get updated. */
12189 w->update_mode_line = true;
12190 #endif /* ! (USE_X_TOOLKIT || HAVE_NTGUI || HAVE_NS || USE_GTK) */
12192 unbind_to (count, Qnil);
12193 set_buffer_internal_1 (prev);
12197 return hooks_run;
12200 /***********************************************************************
12201 Tool-bars
12202 ***********************************************************************/
12204 #ifdef HAVE_WINDOW_SYSTEM
12206 /* Select `frame' temporarily without running all the code in
12207 do_switch_frame.
12208 FIXME: Maybe do_switch_frame should be trimmed down similarly
12209 when `norecord' is set. */
12210 static void
12211 fast_set_selected_frame (Lisp_Object frame)
12213 if (!EQ (selected_frame, frame))
12215 selected_frame = frame;
12216 selected_window = XFRAME (frame)->selected_window;
12220 /* Update the tool-bar item list for frame F. This has to be done
12221 before we start to fill in any display lines. Called from
12222 prepare_menu_bars. If SAVE_MATCH_DATA, we must save
12223 and restore it here. */
12225 static void
12226 update_tool_bar (struct frame *f, bool save_match_data)
12228 #if defined (USE_GTK) || defined (HAVE_NS)
12229 bool do_update = FRAME_EXTERNAL_TOOL_BAR (f);
12230 #else
12231 bool do_update = (WINDOWP (f->tool_bar_window)
12232 && WINDOW_TOTAL_LINES (XWINDOW (f->tool_bar_window)) > 0);
12233 #endif
12235 if (do_update)
12237 Lisp_Object window;
12238 struct window *w;
12240 window = FRAME_SELECTED_WINDOW (f);
12241 w = XWINDOW (window);
12243 /* If the user has switched buffers or windows, we need to
12244 recompute to reflect the new bindings. But we'll
12245 recompute when update_mode_lines is set too; that means
12246 that people can use force-mode-line-update to request
12247 that the menu bar be recomputed. The adverse effect on
12248 the rest of the redisplay algorithm is about the same as
12249 windows_or_buffers_changed anyway. */
12250 if (windows_or_buffers_changed
12251 || w->update_mode_line
12252 || update_mode_lines
12253 || window_buffer_changed (w))
12255 struct buffer *prev = current_buffer;
12256 ptrdiff_t count = SPECPDL_INDEX ();
12257 Lisp_Object frame, new_tool_bar;
12258 int new_n_tool_bar;
12260 /* Set current_buffer to the buffer of the selected
12261 window of the frame, so that we get the right local
12262 keymaps. */
12263 set_buffer_internal_1 (XBUFFER (w->contents));
12265 /* Save match data, if we must. */
12266 if (save_match_data)
12267 record_unwind_save_match_data ();
12269 /* Make sure that we don't accidentally use bogus keymaps. */
12270 if (NILP (Voverriding_local_map_menu_flag))
12272 specbind (Qoverriding_terminal_local_map, Qnil);
12273 specbind (Qoverriding_local_map, Qnil);
12276 /* We must temporarily set the selected frame to this frame
12277 before calling tool_bar_items, because the calculation of
12278 the tool-bar keymap uses the selected frame (see
12279 `tool-bar-make-keymap' in tool-bar.el). */
12280 eassert (EQ (selected_window,
12281 /* Since we only explicitly preserve selected_frame,
12282 check that selected_window would be redundant. */
12283 XFRAME (selected_frame)->selected_window));
12284 record_unwind_protect (fast_set_selected_frame, selected_frame);
12285 XSETFRAME (frame, f);
12286 fast_set_selected_frame (frame);
12288 /* Build desired tool-bar items from keymaps. */
12289 new_tool_bar
12290 = tool_bar_items (Fcopy_sequence (f->tool_bar_items),
12291 &new_n_tool_bar);
12293 /* Redisplay the tool-bar if we changed it. */
12294 if (new_n_tool_bar != f->n_tool_bar_items
12295 || NILP (Fequal (new_tool_bar, f->tool_bar_items)))
12297 /* Redisplay that happens asynchronously due to an expose event
12298 may access f->tool_bar_items. Make sure we update both
12299 variables within BLOCK_INPUT so no such event interrupts. */
12300 block_input ();
12301 fset_tool_bar_items (f, new_tool_bar);
12302 f->n_tool_bar_items = new_n_tool_bar;
12303 w->update_mode_line = true;
12304 unblock_input ();
12307 unbind_to (count, Qnil);
12308 set_buffer_internal_1 (prev);
12313 #if ! defined (USE_GTK) && ! defined (HAVE_NS)
12315 /* Set F->desired_tool_bar_string to a Lisp string representing frame
12316 F's desired tool-bar contents. F->tool_bar_items must have
12317 been set up previously by calling prepare_menu_bars. */
12319 static void
12320 build_desired_tool_bar_string (struct frame *f)
12322 int i, size, size_needed;
12323 Lisp_Object image, plist;
12325 image = plist = Qnil;
12327 /* Prepare F->desired_tool_bar_string. If we can reuse it, do so.
12328 Otherwise, make a new string. */
12330 /* The size of the string we might be able to reuse. */
12331 size = (STRINGP (f->desired_tool_bar_string)
12332 ? SCHARS (f->desired_tool_bar_string)
12333 : 0);
12335 /* We need one space in the string for each image. */
12336 size_needed = f->n_tool_bar_items;
12338 /* Reuse f->desired_tool_bar_string, if possible. */
12339 if (size < size_needed || NILP (f->desired_tool_bar_string))
12340 fset_desired_tool_bar_string
12341 (f, Fmake_string (make_number (size_needed), make_number (' ')));
12342 else
12344 AUTO_LIST4 (props, Qdisplay, Qnil, Qmenu_item, Qnil);
12345 Fremove_text_properties (make_number (0), make_number (size),
12346 props, f->desired_tool_bar_string);
12349 /* Put a `display' property on the string for the images to display,
12350 put a `menu_item' property on tool-bar items with a value that
12351 is the index of the item in F's tool-bar item vector. */
12352 for (i = 0; i < f->n_tool_bar_items; ++i)
12354 #define PROP(IDX) \
12355 AREF (f->tool_bar_items, i * TOOL_BAR_ITEM_NSLOTS + (IDX))
12357 bool enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
12358 bool selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
12359 int hmargin, vmargin, relief, idx, end;
12361 /* If image is a vector, choose the image according to the
12362 button state. */
12363 image = PROP (TOOL_BAR_ITEM_IMAGES);
12364 if (VECTORP (image))
12366 if (enabled_p)
12367 idx = (selected_p
12368 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
12369 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
12370 else
12371 idx = (selected_p
12372 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
12373 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
12375 eassert (ASIZE (image) >= idx);
12376 image = AREF (image, idx);
12378 else
12379 idx = -1;
12381 /* Ignore invalid image specifications. */
12382 if (!valid_image_p (image))
12383 continue;
12385 /* Display the tool-bar button pressed, or depressed. */
12386 plist = Fcopy_sequence (XCDR (image));
12388 /* Compute margin and relief to draw. */
12389 relief = (tool_bar_button_relief >= 0
12390 ? tool_bar_button_relief
12391 : DEFAULT_TOOL_BAR_BUTTON_RELIEF);
12392 hmargin = vmargin = relief;
12394 if (RANGED_INTEGERP (1, Vtool_bar_button_margin,
12395 INT_MAX - max (hmargin, vmargin)))
12397 hmargin += XFASTINT (Vtool_bar_button_margin);
12398 vmargin += XFASTINT (Vtool_bar_button_margin);
12400 else if (CONSP (Vtool_bar_button_margin))
12402 if (RANGED_INTEGERP (1, XCAR (Vtool_bar_button_margin),
12403 INT_MAX - hmargin))
12404 hmargin += XFASTINT (XCAR (Vtool_bar_button_margin));
12406 if (RANGED_INTEGERP (1, XCDR (Vtool_bar_button_margin),
12407 INT_MAX - vmargin))
12408 vmargin += XFASTINT (XCDR (Vtool_bar_button_margin));
12411 if (auto_raise_tool_bar_buttons_p)
12413 /* Add a `:relief' property to the image spec if the item is
12414 selected. */
12415 if (selected_p)
12417 plist = Fplist_put (plist, QCrelief, make_number (-relief));
12418 hmargin -= relief;
12419 vmargin -= relief;
12422 else
12424 /* If image is selected, display it pressed, i.e. with a
12425 negative relief. If it's not selected, display it with a
12426 raised relief. */
12427 plist = Fplist_put (plist, QCrelief,
12428 (selected_p
12429 ? make_number (-relief)
12430 : make_number (relief)));
12431 hmargin -= relief;
12432 vmargin -= relief;
12435 /* Put a margin around the image. */
12436 if (hmargin || vmargin)
12438 if (hmargin == vmargin)
12439 plist = Fplist_put (plist, QCmargin, make_number (hmargin));
12440 else
12441 plist = Fplist_put (plist, QCmargin,
12442 Fcons (make_number (hmargin),
12443 make_number (vmargin)));
12446 /* If button is not enabled, and we don't have special images
12447 for the disabled state, make the image appear disabled by
12448 applying an appropriate algorithm to it. */
12449 if (!enabled_p && idx < 0)
12450 plist = Fplist_put (plist, QCconversion, Qdisabled);
12452 /* Put a `display' text property on the string for the image to
12453 display. Put a `menu-item' property on the string that gives
12454 the start of this item's properties in the tool-bar items
12455 vector. */
12456 image = Fcons (Qimage, plist);
12457 AUTO_LIST4 (props, Qdisplay, image, Qmenu_item,
12458 make_number (i * TOOL_BAR_ITEM_NSLOTS));
12460 /* Let the last image hide all remaining spaces in the tool bar
12461 string. The string can be longer than needed when we reuse a
12462 previous string. */
12463 if (i + 1 == f->n_tool_bar_items)
12464 end = SCHARS (f->desired_tool_bar_string);
12465 else
12466 end = i + 1;
12467 Fadd_text_properties (make_number (i), make_number (end),
12468 props, f->desired_tool_bar_string);
12469 #undef PROP
12474 /* Display one line of the tool-bar of frame IT->f.
12476 HEIGHT specifies the desired height of the tool-bar line.
12477 If the actual height of the glyph row is less than HEIGHT, the
12478 row's height is increased to HEIGHT, and the icons are centered
12479 vertically in the new height.
12481 If HEIGHT is -1, we are counting needed tool-bar lines, so don't
12482 count a final empty row in case the tool-bar width exactly matches
12483 the window width.
12486 static void
12487 display_tool_bar_line (struct it *it, int height)
12489 struct glyph_row *row = it->glyph_row;
12490 int max_x = it->last_visible_x;
12491 struct glyph *last;
12493 /* Don't extend on a previously drawn tool bar items (Bug#16058). */
12494 clear_glyph_row (row);
12495 row->enabled_p = true;
12496 row->y = it->current_y;
12498 /* Note that this isn't made use of if the face hasn't a box,
12499 so there's no need to check the face here. */
12500 it->start_of_box_run_p = true;
12502 while (it->current_x < max_x)
12504 int x, n_glyphs_before, i, nglyphs;
12505 struct it it_before;
12507 /* Get the next display element. */
12508 if (!get_next_display_element (it))
12510 /* Don't count empty row if we are counting needed tool-bar lines. */
12511 if (height < 0 && !it->hpos)
12512 return;
12513 break;
12516 /* Produce glyphs. */
12517 n_glyphs_before = row->used[TEXT_AREA];
12518 it_before = *it;
12520 PRODUCE_GLYPHS (it);
12522 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
12523 i = 0;
12524 x = it_before.current_x;
12525 while (i < nglyphs)
12527 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
12529 if (x + glyph->pixel_width > max_x)
12531 /* Glyph doesn't fit on line. Backtrack. */
12532 row->used[TEXT_AREA] = n_glyphs_before;
12533 *it = it_before;
12534 /* If this is the only glyph on this line, it will never fit on the
12535 tool-bar, so skip it. But ensure there is at least one glyph,
12536 so we don't accidentally disable the tool-bar. */
12537 if (n_glyphs_before == 0
12538 && (it->vpos > 0 || IT_STRING_CHARPOS (*it) < it->end_charpos-1))
12539 break;
12540 goto out;
12543 ++it->hpos;
12544 x += glyph->pixel_width;
12545 ++i;
12548 /* Stop at line end. */
12549 if (ITERATOR_AT_END_OF_LINE_P (it))
12550 break;
12552 set_iterator_to_next (it, true);
12555 out:;
12557 row->displays_text_p = row->used[TEXT_AREA] != 0;
12559 /* Use default face for the border below the tool bar.
12561 FIXME: When auto-resize-tool-bars is grow-only, there is
12562 no additional border below the possibly empty tool-bar lines.
12563 So to make the extra empty lines look "normal", we have to
12564 use the tool-bar face for the border too. */
12565 if (!MATRIX_ROW_DISPLAYS_TEXT_P (row)
12566 && !EQ (Vauto_resize_tool_bars, Qgrow_only))
12567 it->face_id = DEFAULT_FACE_ID;
12569 extend_face_to_end_of_line (it);
12570 last = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
12571 last->right_box_line_p = true;
12572 if (last == row->glyphs[TEXT_AREA])
12573 last->left_box_line_p = true;
12575 /* Make line the desired height and center it vertically. */
12576 if ((height -= it->max_ascent + it->max_descent) > 0)
12578 /* Don't add more than one line height. */
12579 height %= FRAME_LINE_HEIGHT (it->f);
12580 it->max_ascent += height / 2;
12581 it->max_descent += (height + 1) / 2;
12584 compute_line_metrics (it);
12586 /* If line is empty, make it occupy the rest of the tool-bar. */
12587 if (!MATRIX_ROW_DISPLAYS_TEXT_P (row))
12589 row->height = row->phys_height = it->last_visible_y - row->y;
12590 row->visible_height = row->height;
12591 row->ascent = row->phys_ascent = 0;
12592 row->extra_line_spacing = 0;
12595 row->full_width_p = true;
12596 row->continued_p = false;
12597 row->truncated_on_left_p = false;
12598 row->truncated_on_right_p = false;
12600 it->current_x = it->hpos = 0;
12601 it->current_y += row->height;
12602 ++it->vpos;
12603 ++it->glyph_row;
12607 /* Value is the number of pixels needed to make all tool-bar items of
12608 frame F visible. The actual number of glyph rows needed is
12609 returned in *N_ROWS if non-NULL. */
12610 static int
12611 tool_bar_height (struct frame *f, int *n_rows, bool pixelwise)
12613 struct window *w = XWINDOW (f->tool_bar_window);
12614 struct it it;
12615 /* tool_bar_height is called from redisplay_tool_bar after building
12616 the desired matrix, so use (unused) mode-line row as temporary row to
12617 avoid destroying the first tool-bar row. */
12618 struct glyph_row *temp_row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
12620 /* Initialize an iterator for iteration over
12621 F->desired_tool_bar_string in the tool-bar window of frame F. */
12622 init_iterator (&it, w, -1, -1, temp_row, TOOL_BAR_FACE_ID);
12623 temp_row->reversed_p = false;
12624 it.first_visible_x = 0;
12625 it.last_visible_x = WINDOW_PIXEL_WIDTH (w);
12626 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
12627 it.paragraph_embedding = L2R;
12629 while (!ITERATOR_AT_END_P (&it))
12631 clear_glyph_row (temp_row);
12632 it.glyph_row = temp_row;
12633 display_tool_bar_line (&it, -1);
12635 clear_glyph_row (temp_row);
12637 /* f->n_tool_bar_rows == 0 means "unknown"; -1 means no tool-bar. */
12638 if (n_rows)
12639 *n_rows = it.vpos > 0 ? it.vpos : -1;
12641 if (pixelwise)
12642 return it.current_y;
12643 else
12644 return (it.current_y + FRAME_LINE_HEIGHT (f) - 1) / FRAME_LINE_HEIGHT (f);
12647 #endif /* !USE_GTK && !HAVE_NS */
12649 DEFUN ("tool-bar-height", Ftool_bar_height, Stool_bar_height,
12650 0, 2, 0,
12651 doc: /* Return the number of lines occupied by the tool bar of FRAME.
12652 If FRAME is nil or omitted, use the selected frame. Optional argument
12653 PIXELWISE non-nil means return the height of the tool bar in pixels. */)
12654 (Lisp_Object frame, Lisp_Object pixelwise)
12656 int height = 0;
12658 #if ! defined (USE_GTK) && ! defined (HAVE_NS)
12659 struct frame *f = decode_any_frame (frame);
12661 if (WINDOWP (f->tool_bar_window)
12662 && WINDOW_PIXEL_HEIGHT (XWINDOW (f->tool_bar_window)) > 0)
12664 update_tool_bar (f, true);
12665 if (f->n_tool_bar_items)
12667 build_desired_tool_bar_string (f);
12668 height = tool_bar_height (f, NULL, !NILP (pixelwise));
12671 #endif
12673 return make_number (height);
12677 /* Display the tool-bar of frame F. Value is true if tool-bar's
12678 height should be changed. */
12679 static bool
12680 redisplay_tool_bar (struct frame *f)
12682 f->tool_bar_redisplayed = true;
12683 #if defined (USE_GTK) || defined (HAVE_NS)
12685 if (FRAME_EXTERNAL_TOOL_BAR (f))
12686 update_frame_tool_bar (f);
12687 return false;
12689 #else /* !USE_GTK && !HAVE_NS */
12691 struct window *w;
12692 struct it it;
12693 struct glyph_row *row;
12695 /* If frame hasn't a tool-bar window or if it is zero-height, don't
12696 do anything. This means you must start with tool-bar-lines
12697 non-zero to get the auto-sizing effect. Or in other words, you
12698 can turn off tool-bars by specifying tool-bar-lines zero. */
12699 if (!WINDOWP (f->tool_bar_window)
12700 || (w = XWINDOW (f->tool_bar_window),
12701 WINDOW_TOTAL_LINES (w) == 0))
12702 return false;
12704 /* Set up an iterator for the tool-bar window. */
12705 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
12706 it.first_visible_x = 0;
12707 it.last_visible_x = WINDOW_PIXEL_WIDTH (w);
12708 row = it.glyph_row;
12709 row->reversed_p = false;
12711 /* Build a string that represents the contents of the tool-bar. */
12712 build_desired_tool_bar_string (f);
12713 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
12714 /* FIXME: This should be controlled by a user option. But it
12715 doesn't make sense to have an R2L tool bar if the menu bar cannot
12716 be drawn also R2L, and making the menu bar R2L is tricky due
12717 toolkit-specific code that implements it. If an R2L tool bar is
12718 ever supported, display_tool_bar_line should also be augmented to
12719 call unproduce_glyphs like display_line and display_string
12720 do. */
12721 it.paragraph_embedding = L2R;
12723 if (f->n_tool_bar_rows == 0)
12725 int new_height = tool_bar_height (f, &f->n_tool_bar_rows, true);
12727 if (new_height != WINDOW_PIXEL_HEIGHT (w))
12729 x_change_tool_bar_height (f, new_height);
12730 frame_default_tool_bar_height = new_height;
12731 /* Always do that now. */
12732 clear_glyph_matrix (w->desired_matrix);
12733 f->fonts_changed = true;
12734 return true;
12738 /* Display as many lines as needed to display all tool-bar items. */
12740 if (f->n_tool_bar_rows > 0)
12742 int border, rows, height, extra;
12744 if (TYPE_RANGED_INTEGERP (int, Vtool_bar_border))
12745 border = XINT (Vtool_bar_border);
12746 else if (EQ (Vtool_bar_border, Qinternal_border_width))
12747 border = FRAME_INTERNAL_BORDER_WIDTH (f);
12748 else if (EQ (Vtool_bar_border, Qborder_width))
12749 border = f->border_width;
12750 else
12751 border = 0;
12752 if (border < 0)
12753 border = 0;
12755 rows = f->n_tool_bar_rows;
12756 height = max (1, (it.last_visible_y - border) / rows);
12757 extra = it.last_visible_y - border - height * rows;
12759 while (it.current_y < it.last_visible_y)
12761 int h = 0;
12762 if (extra > 0 && rows-- > 0)
12764 h = (extra + rows - 1) / rows;
12765 extra -= h;
12767 display_tool_bar_line (&it, height + h);
12770 else
12772 while (it.current_y < it.last_visible_y)
12773 display_tool_bar_line (&it, 0);
12776 /* It doesn't make much sense to try scrolling in the tool-bar
12777 window, so don't do it. */
12778 w->desired_matrix->no_scrolling_p = true;
12779 w->must_be_updated_p = true;
12781 if (!NILP (Vauto_resize_tool_bars))
12783 bool change_height_p = true;
12785 /* If we couldn't display everything, change the tool-bar's
12786 height if there is room for more. */
12787 if (IT_STRING_CHARPOS (it) < it.end_charpos)
12788 change_height_p = true;
12790 /* We subtract 1 because display_tool_bar_line advances the
12791 glyph_row pointer before returning to its caller. We want to
12792 examine the last glyph row produced by
12793 display_tool_bar_line. */
12794 row = it.glyph_row - 1;
12796 /* If there are blank lines at the end, except for a partially
12797 visible blank line at the end that is smaller than
12798 FRAME_LINE_HEIGHT, change the tool-bar's height. */
12799 if (!MATRIX_ROW_DISPLAYS_TEXT_P (row)
12800 && row->height >= FRAME_LINE_HEIGHT (f))
12801 change_height_p = true;
12803 /* If row displays tool-bar items, but is partially visible,
12804 change the tool-bar's height. */
12805 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
12806 && MATRIX_ROW_BOTTOM_Y (row) > it.last_visible_y)
12807 change_height_p = true;
12809 /* Resize windows as needed by changing the `tool-bar-lines'
12810 frame parameter. */
12811 if (change_height_p)
12813 int nrows;
12814 int new_height = tool_bar_height (f, &nrows, true);
12816 change_height_p = ((EQ (Vauto_resize_tool_bars, Qgrow_only)
12817 && !f->minimize_tool_bar_window_p)
12818 ? (new_height > WINDOW_PIXEL_HEIGHT (w))
12819 : (new_height != WINDOW_PIXEL_HEIGHT (w)));
12820 f->minimize_tool_bar_window_p = false;
12822 if (change_height_p)
12824 x_change_tool_bar_height (f, new_height);
12825 frame_default_tool_bar_height = new_height;
12826 clear_glyph_matrix (w->desired_matrix);
12827 f->n_tool_bar_rows = nrows;
12828 f->fonts_changed = true;
12830 return true;
12835 f->minimize_tool_bar_window_p = false;
12836 return false;
12838 #endif /* USE_GTK || HAVE_NS */
12841 #if ! defined (USE_GTK) && ! defined (HAVE_NS)
12843 /* Get information about the tool-bar item which is displayed in GLYPH
12844 on frame F. Return in *PROP_IDX the index where tool-bar item
12845 properties start in F->tool_bar_items. Value is false if
12846 GLYPH doesn't display a tool-bar item. */
12848 static bool
12849 tool_bar_item_info (struct frame *f, struct glyph *glyph, int *prop_idx)
12851 Lisp_Object prop;
12852 int charpos;
12854 /* This function can be called asynchronously, which means we must
12855 exclude any possibility that Fget_text_property signals an
12856 error. */
12857 charpos = min (SCHARS (f->current_tool_bar_string), glyph->charpos);
12858 charpos = max (0, charpos);
12860 /* Get the text property `menu-item' at pos. The value of that
12861 property is the start index of this item's properties in
12862 F->tool_bar_items. */
12863 prop = Fget_text_property (make_number (charpos),
12864 Qmenu_item, f->current_tool_bar_string);
12865 if (! INTEGERP (prop))
12866 return false;
12867 *prop_idx = XINT (prop);
12868 return true;
12872 /* Get information about the tool-bar item at position X/Y on frame F.
12873 Return in *GLYPH a pointer to the glyph of the tool-bar item in
12874 the current matrix of the tool-bar window of F, or NULL if not
12875 on a tool-bar item. Return in *PROP_IDX the index of the tool-bar
12876 item in F->tool_bar_items. Value is
12878 -1 if X/Y is not on a tool-bar item
12879 0 if X/Y is on the same item that was highlighted before.
12880 1 otherwise. */
12882 static int
12883 get_tool_bar_item (struct frame *f, int x, int y, struct glyph **glyph,
12884 int *hpos, int *vpos, int *prop_idx)
12886 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12887 struct window *w = XWINDOW (f->tool_bar_window);
12888 int area;
12890 /* Find the glyph under X/Y. */
12891 *glyph = x_y_to_hpos_vpos (w, x, y, hpos, vpos, 0, 0, &area);
12892 if (*glyph == NULL)
12893 return -1;
12895 /* Get the start of this tool-bar item's properties in
12896 f->tool_bar_items. */
12897 if (!tool_bar_item_info (f, *glyph, prop_idx))
12898 return -1;
12900 /* Is mouse on the highlighted item? */
12901 if (EQ (f->tool_bar_window, hlinfo->mouse_face_window)
12902 && *vpos >= hlinfo->mouse_face_beg_row
12903 && *vpos <= hlinfo->mouse_face_end_row
12904 && (*vpos > hlinfo->mouse_face_beg_row
12905 || *hpos >= hlinfo->mouse_face_beg_col)
12906 && (*vpos < hlinfo->mouse_face_end_row
12907 || *hpos < hlinfo->mouse_face_end_col
12908 || hlinfo->mouse_face_past_end))
12909 return 0;
12911 return 1;
12915 /* EXPORT:
12916 Handle mouse button event on the tool-bar of frame F, at
12917 frame-relative coordinates X/Y. DOWN_P is true for a button press,
12918 false for button release. MODIFIERS is event modifiers for button
12919 release. */
12921 void
12922 handle_tool_bar_click (struct frame *f, int x, int y, bool down_p,
12923 int modifiers)
12925 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12926 struct window *w = XWINDOW (f->tool_bar_window);
12927 int hpos, vpos, prop_idx;
12928 struct glyph *glyph;
12929 Lisp_Object enabled_p;
12930 int ts;
12932 /* If not on the highlighted tool-bar item, and mouse-highlight is
12933 non-nil, return. This is so we generate the tool-bar button
12934 click only when the mouse button is released on the same item as
12935 where it was pressed. However, when mouse-highlight is disabled,
12936 generate the click when the button is released regardless of the
12937 highlight, since tool-bar items are not highlighted in that
12938 case. */
12939 frame_to_window_pixel_xy (w, &x, &y);
12940 ts = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
12941 if (ts == -1
12942 || (ts != 0 && !NILP (Vmouse_highlight)))
12943 return;
12945 /* When mouse-highlight is off, generate the click for the item
12946 where the button was pressed, disregarding where it was
12947 released. */
12948 if (NILP (Vmouse_highlight) && !down_p)
12949 prop_idx = f->last_tool_bar_item;
12951 /* If item is disabled, do nothing. */
12952 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
12953 if (NILP (enabled_p))
12954 return;
12956 if (down_p)
12958 /* Show item in pressed state. */
12959 if (!NILP (Vmouse_highlight))
12960 show_mouse_face (hlinfo, DRAW_IMAGE_SUNKEN);
12961 f->last_tool_bar_item = prop_idx;
12963 else
12965 Lisp_Object key, frame;
12966 struct input_event event;
12967 EVENT_INIT (event);
12969 /* Show item in released state. */
12970 if (!NILP (Vmouse_highlight))
12971 show_mouse_face (hlinfo, DRAW_IMAGE_RAISED);
12973 key = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_KEY);
12975 XSETFRAME (frame, f);
12976 event.kind = TOOL_BAR_EVENT;
12977 event.frame_or_window = frame;
12978 event.arg = frame;
12979 kbd_buffer_store_event (&event);
12981 event.kind = TOOL_BAR_EVENT;
12982 event.frame_or_window = frame;
12983 event.arg = key;
12984 event.modifiers = modifiers;
12985 kbd_buffer_store_event (&event);
12986 f->last_tool_bar_item = -1;
12991 /* Possibly highlight a tool-bar item on frame F when mouse moves to
12992 tool-bar window-relative coordinates X/Y. Called from
12993 note_mouse_highlight. */
12995 static void
12996 note_tool_bar_highlight (struct frame *f, int x, int y)
12998 Lisp_Object window = f->tool_bar_window;
12999 struct window *w = XWINDOW (window);
13000 Display_Info *dpyinfo = FRAME_DISPLAY_INFO (f);
13001 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
13002 int hpos, vpos;
13003 struct glyph *glyph;
13004 struct glyph_row *row;
13005 int i;
13006 Lisp_Object enabled_p;
13007 int prop_idx;
13008 enum draw_glyphs_face draw = DRAW_IMAGE_RAISED;
13009 bool mouse_down_p;
13010 int rc;
13012 /* Function note_mouse_highlight is called with negative X/Y
13013 values when mouse moves outside of the frame. */
13014 if (x <= 0 || y <= 0)
13016 clear_mouse_face (hlinfo);
13017 return;
13020 rc = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
13021 if (rc < 0)
13023 /* Not on tool-bar item. */
13024 clear_mouse_face (hlinfo);
13025 return;
13027 else if (rc == 0)
13028 /* On same tool-bar item as before. */
13029 goto set_help_echo;
13031 clear_mouse_face (hlinfo);
13033 /* Mouse is down, but on different tool-bar item? */
13034 mouse_down_p = (x_mouse_grabbed (dpyinfo)
13035 && f == dpyinfo->last_mouse_frame);
13037 if (mouse_down_p && f->last_tool_bar_item != prop_idx)
13038 return;
13040 draw = mouse_down_p ? DRAW_IMAGE_SUNKEN : DRAW_IMAGE_RAISED;
13042 /* If tool-bar item is not enabled, don't highlight it. */
13043 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
13044 if (!NILP (enabled_p) && !NILP (Vmouse_highlight))
13046 /* Compute the x-position of the glyph. In front and past the
13047 image is a space. We include this in the highlighted area. */
13048 row = MATRIX_ROW (w->current_matrix, vpos);
13049 for (i = x = 0; i < hpos; ++i)
13050 x += row->glyphs[TEXT_AREA][i].pixel_width;
13052 /* Record this as the current active region. */
13053 hlinfo->mouse_face_beg_col = hpos;
13054 hlinfo->mouse_face_beg_row = vpos;
13055 hlinfo->mouse_face_beg_x = x;
13056 hlinfo->mouse_face_past_end = false;
13058 hlinfo->mouse_face_end_col = hpos + 1;
13059 hlinfo->mouse_face_end_row = vpos;
13060 hlinfo->mouse_face_end_x = x + glyph->pixel_width;
13061 hlinfo->mouse_face_window = window;
13062 hlinfo->mouse_face_face_id = TOOL_BAR_FACE_ID;
13064 /* Display it as active. */
13065 show_mouse_face (hlinfo, draw);
13068 set_help_echo:
13070 /* Set help_echo_string to a help string to display for this tool-bar item.
13071 XTread_socket does the rest. */
13072 help_echo_object = help_echo_window = Qnil;
13073 help_echo_pos = -1;
13074 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_HELP);
13075 if (NILP (help_echo_string))
13076 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_CAPTION);
13079 #endif /* !USE_GTK && !HAVE_NS */
13081 #endif /* HAVE_WINDOW_SYSTEM */
13085 /************************************************************************
13086 Horizontal scrolling
13087 ************************************************************************/
13089 /* For all leaf windows in the window tree rooted at WINDOW, set their
13090 hscroll value so that PT is (i) visible in the window, and (ii) so
13091 that it is not within a certain margin at the window's left and
13092 right border. Value is true if any window's hscroll has been
13093 changed. */
13095 static bool
13096 hscroll_window_tree (Lisp_Object window)
13098 bool hscrolled_p = false;
13099 bool hscroll_relative_p = FLOATP (Vhscroll_step);
13100 int hscroll_step_abs = 0;
13101 double hscroll_step_rel = 0;
13103 if (hscroll_relative_p)
13105 hscroll_step_rel = XFLOAT_DATA (Vhscroll_step);
13106 if (hscroll_step_rel < 0)
13108 hscroll_relative_p = false;
13109 hscroll_step_abs = 0;
13112 else if (TYPE_RANGED_INTEGERP (int, Vhscroll_step))
13114 hscroll_step_abs = XINT (Vhscroll_step);
13115 if (hscroll_step_abs < 0)
13116 hscroll_step_abs = 0;
13118 else
13119 hscroll_step_abs = 0;
13121 while (WINDOWP (window))
13123 struct window *w = XWINDOW (window);
13125 if (WINDOWP (w->contents))
13126 hscrolled_p |= hscroll_window_tree (w->contents);
13127 else if (w->cursor.vpos >= 0)
13129 int h_margin;
13130 int text_area_width;
13131 struct glyph_row *cursor_row;
13132 struct glyph_row *bottom_row;
13134 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->desired_matrix, w);
13135 if (w->cursor.vpos < bottom_row - w->desired_matrix->rows)
13136 cursor_row = MATRIX_ROW (w->desired_matrix, w->cursor.vpos);
13137 else
13138 cursor_row = bottom_row - 1;
13140 if (!cursor_row->enabled_p)
13142 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
13143 if (w->cursor.vpos < bottom_row - w->current_matrix->rows)
13144 cursor_row = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
13145 else
13146 cursor_row = bottom_row - 1;
13148 bool row_r2l_p = cursor_row->reversed_p;
13149 bool hscl = hscrolling_current_line_p (w);
13150 int x_offset = 0;
13151 /* When line numbers are displayed, we need to account for
13152 the horizontal space they consume. */
13153 if (!NILP (Vdisplay_line_numbers))
13155 struct glyph *g;
13156 if (!row_r2l_p)
13158 for (g = cursor_row->glyphs[TEXT_AREA];
13159 g < cursor_row->glyphs[TEXT_AREA]
13160 + cursor_row->used[TEXT_AREA];
13161 g++)
13163 if (!(NILP (g->object) && g->charpos < 0))
13164 break;
13165 x_offset += g->pixel_width;
13168 else
13170 for (g = cursor_row->glyphs[TEXT_AREA]
13171 + cursor_row->used[TEXT_AREA];
13172 g > cursor_row->glyphs[TEXT_AREA];
13173 g--)
13175 if (!(NILP ((g - 1)->object) && (g - 1)->charpos < 0))
13176 break;
13177 x_offset += (g - 1)->pixel_width;
13181 if (cursor_row->truncated_on_left_p)
13183 /* On TTY frames, don't count the left truncation glyph. */
13184 struct frame *f = XFRAME (WINDOW_FRAME (w));
13185 x_offset -= (FRAME_TERMCAP_P (f) || FRAME_MSDOS_P (f));
13188 text_area_width = window_box_width (w, TEXT_AREA);
13190 /* Scroll when cursor is inside this scroll margin. */
13191 h_margin = hscroll_margin * WINDOW_FRAME_COLUMN_WIDTH (w);
13193 /* If the position of this window's point has explicitly
13194 changed, no more suspend auto hscrolling. */
13195 if (w->suspend_auto_hscroll
13196 && NILP (Fequal (Fwindow_point (window),
13197 Fwindow_old_point (window))))
13199 w->suspend_auto_hscroll = false;
13200 /* When hscrolling just the current line, and the rest
13201 of lines were temporarily hscrolled, but no longer
13202 are, force thorough redisplay of this window, to show
13203 the effect of disabling hscroll suspension immediately. */
13204 if (w->min_hscroll == 0 && w->hscroll > 0
13205 && EQ (Fbuffer_local_value (Qauto_hscroll_mode, w->contents),
13206 Qcurrent_line))
13207 SET_FRAME_GARBAGED (XFRAME (w->frame));
13210 /* Remember window point. */
13211 Fset_marker (w->old_pointm,
13212 ((w == XWINDOW (selected_window))
13213 ? make_number (BUF_PT (XBUFFER (w->contents)))
13214 : Fmarker_position (w->pointm)),
13215 w->contents);
13217 if (!NILP (Fbuffer_local_value (Qauto_hscroll_mode, w->contents))
13218 && !w->suspend_auto_hscroll
13219 /* In some pathological cases, like restoring a window
13220 configuration into a frame that is much smaller than
13221 the one from which the configuration was saved, we
13222 get glyph rows whose start and end have zero buffer
13223 positions, which we cannot handle below. Just skip
13224 such windows. */
13225 && CHARPOS (cursor_row->start.pos) >= BUF_BEG (w->contents)
13226 /* For left-to-right rows, hscroll when cursor is either
13227 (i) inside the right hscroll margin, or (ii) if it is
13228 inside the left margin and the window is already
13229 hscrolled. */
13230 && ((!row_r2l_p
13231 && ((w->hscroll && w->cursor.x <= h_margin + x_offset)
13232 || (cursor_row->enabled_p
13233 && cursor_row->truncated_on_right_p
13234 && (w->cursor.x >= text_area_width - h_margin))))
13235 /* For right-to-left rows, the logic is similar,
13236 except that rules for scrolling to left and right
13237 are reversed. E.g., if cursor.x <= h_margin, we
13238 need to hscroll "to the right" unconditionally,
13239 and that will scroll the screen to the left so as
13240 to reveal the next portion of the row. */
13241 || (row_r2l_p
13242 && ((cursor_row->enabled_p
13243 /* FIXME: It is confusing to set the
13244 truncated_on_right_p flag when R2L rows
13245 are actually truncated on the left. */
13246 && cursor_row->truncated_on_right_p
13247 && w->cursor.x <= h_margin)
13248 || (w->hscroll
13249 && (w->cursor.x >= (text_area_width - h_margin
13250 - x_offset)))))
13251 /* This last condition is needed when moving
13252 vertically from an hscrolled line to a short line
13253 that doesn't need to be hscrolled. If we omit
13254 this condition, the line from which we move will
13255 remain hscrolled. */
13256 || (hscl
13257 && w->hscroll != w->min_hscroll
13258 && !cursor_row->truncated_on_left_p)))
13260 struct it it;
13261 ptrdiff_t hscroll;
13262 struct buffer *saved_current_buffer;
13263 ptrdiff_t pt;
13264 int wanted_x;
13266 /* Find point in a display of infinite width. */
13267 saved_current_buffer = current_buffer;
13268 current_buffer = XBUFFER (w->contents);
13270 if (w == XWINDOW (selected_window))
13271 pt = PT;
13272 else
13273 pt = clip_to_bounds (BEGV, marker_position (w->pointm), ZV);
13275 /* Move iterator to pt starting at cursor_row->start in
13276 a line with infinite width. */
13277 init_to_row_start (&it, w, cursor_row);
13278 if (hscl)
13279 it.first_visible_x = window_hscroll_limited (w, it.f)
13280 * FRAME_COLUMN_WIDTH (it.f);
13281 it.last_visible_x = DISP_INFINITY;
13282 move_it_in_display_line_to (&it, pt, -1, MOVE_TO_POS);
13283 /* If the line ends in an overlay string with a newline,
13284 we might infloop, because displaying the window will
13285 want to put the cursor after the overlay, i.e. at X
13286 coordinate of zero on the next screen line. So we
13287 use the buffer position prior to the overlay string
13288 instead. */
13289 if (it.method == GET_FROM_STRING && pt > 1)
13291 init_to_row_start (&it, w, cursor_row);
13292 if (hscl)
13293 it.first_visible_x = (window_hscroll_limited (w, it.f)
13294 * FRAME_COLUMN_WIDTH (it.f));
13295 move_it_in_display_line_to (&it, pt - 1, -1, MOVE_TO_POS);
13297 current_buffer = saved_current_buffer;
13299 /* Position cursor in window. */
13300 if (!hscroll_relative_p && hscroll_step_abs == 0)
13301 hscroll = max (0, (it.current_x
13302 - (ITERATOR_AT_END_OF_LINE_P (&it)
13303 ? (text_area_width - 4 * FRAME_COLUMN_WIDTH (it.f))
13304 : (text_area_width / 2))))
13305 / FRAME_COLUMN_WIDTH (it.f);
13306 else if ((!row_r2l_p
13307 && w->cursor.x >= text_area_width - h_margin)
13308 || (row_r2l_p && w->cursor.x <= h_margin))
13310 if (hscroll_relative_p)
13311 wanted_x = text_area_width * (1 - hscroll_step_rel)
13312 - h_margin;
13313 else
13314 wanted_x = text_area_width
13315 - hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
13316 - h_margin;
13317 hscroll
13318 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
13320 else
13322 if (hscroll_relative_p)
13323 wanted_x = text_area_width * hscroll_step_rel
13324 + h_margin;
13325 else
13326 wanted_x = hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
13327 + h_margin;
13328 hscroll
13329 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
13331 hscroll = max (hscroll, w->min_hscroll);
13333 /* Don't prevent redisplay optimizations if hscroll
13334 hasn't changed, as it will unnecessarily slow down
13335 redisplay. */
13336 if (w->hscroll != hscroll
13337 /* When hscrolling only the current line, we need to
13338 report hscroll even if its value is equal to the
13339 previous one, because the new line might need a
13340 different value. */
13341 || (hscl && w->last_cursor_vpos != w->cursor.vpos))
13343 struct buffer *b = XBUFFER (w->contents);
13344 b->prevent_redisplay_optimizations_p = true;
13345 w->hscroll = hscroll;
13346 hscrolled_p = true;
13351 window = w->next;
13354 /* Value is true if hscroll of any leaf window has been changed. */
13355 return hscrolled_p;
13359 /* Set hscroll so that cursor is visible and not inside horizontal
13360 scroll margins for all windows in the tree rooted at WINDOW. See
13361 also hscroll_window_tree above. Value is true if any window's
13362 hscroll has been changed. If it has, desired matrices on the frame
13363 of WINDOW are cleared. */
13365 static bool
13366 hscroll_windows (Lisp_Object window)
13368 bool hscrolled_p = hscroll_window_tree (window);
13369 if (hscrolled_p)
13370 clear_desired_matrices (XFRAME (WINDOW_FRAME (XWINDOW (window))));
13371 return hscrolled_p;
13376 /************************************************************************
13377 Redisplay
13378 ************************************************************************/
13380 /* Variables holding some state of redisplay if GLYPH_DEBUG is defined.
13381 This is sometimes handy to have in a debugger session. */
13383 #ifdef GLYPH_DEBUG
13385 /* First and last unchanged row for try_window_id. */
13387 static int debug_first_unchanged_at_end_vpos;
13388 static int debug_last_unchanged_at_beg_vpos;
13390 /* Delta vpos and y. */
13392 static int debug_dvpos, debug_dy;
13394 /* Delta in characters and bytes for try_window_id. */
13396 static ptrdiff_t debug_delta, debug_delta_bytes;
13398 /* Values of window_end_pos and window_end_vpos at the end of
13399 try_window_id. */
13401 static ptrdiff_t debug_end_vpos;
13403 /* Append a string to W->desired_matrix->method. FMT is a printf
13404 format string. If trace_redisplay_p is true also printf the
13405 resulting string to stderr. */
13407 static void debug_method_add (struct window *, char const *, ...)
13408 ATTRIBUTE_FORMAT_PRINTF (2, 3);
13410 static void
13411 debug_method_add (struct window *w, char const *fmt, ...)
13413 void *ptr = w;
13414 char *method = w->desired_matrix->method;
13415 int len = strlen (method);
13416 int size = sizeof w->desired_matrix->method;
13417 int remaining = size - len - 1;
13418 va_list ap;
13420 if (len && remaining)
13422 method[len] = '|';
13423 --remaining, ++len;
13426 va_start (ap, fmt);
13427 vsnprintf (method + len, remaining + 1, fmt, ap);
13428 va_end (ap);
13430 if (trace_redisplay_p)
13431 fprintf (stderr, "%p (%s): %s\n",
13432 ptr,
13433 ((BUFFERP (w->contents)
13434 && STRINGP (BVAR (XBUFFER (w->contents), name)))
13435 ? SSDATA (BVAR (XBUFFER (w->contents), name))
13436 : "no buffer"),
13437 method + len);
13440 #endif /* GLYPH_DEBUG */
13443 /* Value is true if all changes in window W, which displays
13444 current_buffer, are in the text between START and END. START is a
13445 buffer position, END is given as a distance from Z. Used in
13446 redisplay_internal for display optimization. */
13448 static bool
13449 text_outside_line_unchanged_p (struct window *w,
13450 ptrdiff_t start, ptrdiff_t end)
13452 bool unchanged_p = true;
13454 /* If text or overlays have changed, see where. */
13455 if (window_outdated (w))
13457 /* Gap in the line? */
13458 if (GPT < start || Z - GPT < end)
13459 unchanged_p = false;
13461 /* Changes start in front of the line, or end after it? */
13462 if (unchanged_p
13463 && (BEG_UNCHANGED < start - 1
13464 || END_UNCHANGED < end))
13465 unchanged_p = false;
13467 /* If selective display, can't optimize if changes start at the
13468 beginning of the line. */
13469 if (unchanged_p
13470 && INTEGERP (BVAR (current_buffer, selective_display))
13471 && XINT (BVAR (current_buffer, selective_display)) > 0
13472 && (BEG_UNCHANGED < start || GPT <= start))
13473 unchanged_p = false;
13475 /* If there are overlays at the start or end of the line, these
13476 may have overlay strings with newlines in them. A change at
13477 START, for instance, may actually concern the display of such
13478 overlay strings as well, and they are displayed on different
13479 lines. So, quickly rule out this case. (For the future, it
13480 might be desirable to implement something more telling than
13481 just BEG/END_UNCHANGED.) */
13482 if (unchanged_p)
13484 if (BEG + BEG_UNCHANGED == start
13485 && overlay_touches_p (start))
13486 unchanged_p = false;
13487 if (END_UNCHANGED == end
13488 && overlay_touches_p (Z - end))
13489 unchanged_p = false;
13492 /* Under bidi reordering, adding or deleting a character in the
13493 beginning of a paragraph, before the first strong directional
13494 character, can change the base direction of the paragraph (unless
13495 the buffer specifies a fixed paragraph direction), which will
13496 require redisplaying the whole paragraph. It might be worthwhile
13497 to find the paragraph limits and widen the range of redisplayed
13498 lines to that, but for now just give up this optimization. */
13499 if (!NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering))
13500 && NILP (BVAR (XBUFFER (w->contents), bidi_paragraph_direction)))
13501 unchanged_p = false;
13504 return unchanged_p;
13508 /* Do a frame update, taking possible shortcuts into account. This is
13509 the main external entry point for redisplay.
13511 If the last redisplay displayed an echo area message and that message
13512 is no longer requested, we clear the echo area or bring back the
13513 mini-buffer if that is in use. */
13515 void
13516 redisplay (void)
13518 redisplay_internal ();
13522 static Lisp_Object
13523 overlay_arrow_string_or_property (Lisp_Object var)
13525 Lisp_Object val;
13527 if (val = Fget (var, Qoverlay_arrow_string), STRINGP (val))
13528 return val;
13530 return Voverlay_arrow_string;
13533 /* Return true if there are any overlay-arrows in current_buffer. */
13534 static bool
13535 overlay_arrow_in_current_buffer_p (void)
13537 Lisp_Object vlist;
13539 for (vlist = Voverlay_arrow_variable_list;
13540 CONSP (vlist);
13541 vlist = XCDR (vlist))
13543 Lisp_Object var = XCAR (vlist);
13544 Lisp_Object val;
13546 if (!SYMBOLP (var))
13547 continue;
13548 val = find_symbol_value (var);
13549 if (MARKERP (val)
13550 && current_buffer == XMARKER (val)->buffer)
13551 return true;
13553 return false;
13557 /* Return true if any overlay_arrows have moved or overlay-arrow-string
13558 has changed.
13559 If SET_REDISPLAY is true, additionally, set the `redisplay' bit in those
13560 buffers that are affected. */
13562 static bool
13563 overlay_arrows_changed_p (bool set_redisplay)
13565 Lisp_Object vlist;
13566 bool changed = false;
13568 for (vlist = Voverlay_arrow_variable_list;
13569 CONSP (vlist);
13570 vlist = XCDR (vlist))
13572 Lisp_Object var = XCAR (vlist);
13573 Lisp_Object val, pstr;
13575 if (!SYMBOLP (var))
13576 continue;
13577 val = find_symbol_value (var);
13578 if (!MARKERP (val))
13579 continue;
13580 if (! EQ (COERCE_MARKER (val),
13581 /* FIXME: Don't we have a problem, using such a global
13582 * "last-position" if the variable is buffer-local? */
13583 Fget (var, Qlast_arrow_position))
13584 || ! (pstr = overlay_arrow_string_or_property (var),
13585 EQ (pstr, Fget (var, Qlast_arrow_string))))
13587 struct buffer *buf = XMARKER (val)->buffer;
13589 if (set_redisplay)
13591 if (buf)
13592 bset_redisplay (buf);
13593 changed = true;
13595 else
13596 return true;
13599 return changed;
13602 /* Mark overlay arrows to be updated on next redisplay. */
13604 static void
13605 update_overlay_arrows (int up_to_date)
13607 Lisp_Object vlist;
13609 for (vlist = Voverlay_arrow_variable_list;
13610 CONSP (vlist);
13611 vlist = XCDR (vlist))
13613 Lisp_Object var = XCAR (vlist);
13615 if (!SYMBOLP (var))
13616 continue;
13618 if (up_to_date > 0)
13620 Lisp_Object val = find_symbol_value (var);
13621 if (!MARKERP (val))
13622 continue;
13623 Fput (var, Qlast_arrow_position,
13624 COERCE_MARKER (val));
13625 Fput (var, Qlast_arrow_string,
13626 overlay_arrow_string_or_property (var));
13628 else if (up_to_date < 0
13629 || !NILP (Fget (var, Qlast_arrow_position)))
13631 Fput (var, Qlast_arrow_position, Qt);
13632 Fput (var, Qlast_arrow_string, Qt);
13638 /* Return overlay arrow string to display at row.
13639 Return integer (bitmap number) for arrow bitmap in left fringe.
13640 Return nil if no overlay arrow. */
13642 static Lisp_Object
13643 overlay_arrow_at_row (struct it *it, struct glyph_row *row)
13645 Lisp_Object vlist;
13647 for (vlist = Voverlay_arrow_variable_list;
13648 CONSP (vlist);
13649 vlist = XCDR (vlist))
13651 Lisp_Object var = XCAR (vlist);
13652 Lisp_Object val;
13654 if (!SYMBOLP (var))
13655 continue;
13657 val = find_symbol_value (var);
13659 if (MARKERP (val)
13660 && current_buffer == XMARKER (val)->buffer
13661 && (MATRIX_ROW_START_CHARPOS (row) == marker_position (val)))
13663 if (FRAME_WINDOW_P (it->f)
13664 /* FIXME: if ROW->reversed_p is set, this should test
13665 the right fringe, not the left one. */
13666 && WINDOW_LEFT_FRINGE_WIDTH (it->w) > 0)
13668 #ifdef HAVE_WINDOW_SYSTEM
13669 if (val = Fget (var, Qoverlay_arrow_bitmap), SYMBOLP (val))
13671 int fringe_bitmap = lookup_fringe_bitmap (val);
13672 if (fringe_bitmap != 0)
13673 return make_number (fringe_bitmap);
13675 #endif
13676 return make_number (-1); /* Use default arrow bitmap. */
13678 return overlay_arrow_string_or_property (var);
13682 return Qnil;
13685 /* Return true if point moved out of or into a composition. Otherwise
13686 return false. PREV_BUF and PREV_PT are the last point buffer and
13687 position. BUF and PT are the current point buffer and position. */
13689 static bool
13690 check_point_in_composition (struct buffer *prev_buf, ptrdiff_t prev_pt,
13691 struct buffer *buf, ptrdiff_t pt)
13693 ptrdiff_t start, end;
13694 Lisp_Object prop;
13695 Lisp_Object buffer;
13697 XSETBUFFER (buffer, buf);
13698 /* Check a composition at the last point if point moved within the
13699 same buffer. */
13700 if (prev_buf == buf)
13702 if (prev_pt == pt)
13703 /* Point didn't move. */
13704 return false;
13706 if (prev_pt > BUF_BEGV (buf) && prev_pt < BUF_ZV (buf)
13707 && find_composition (prev_pt, -1, &start, &end, &prop, buffer)
13708 && composition_valid_p (start, end, prop)
13709 && start < prev_pt && end > prev_pt)
13710 /* The last point was within the composition. Return true iff
13711 point moved out of the composition. */
13712 return (pt <= start || pt >= end);
13715 /* Check a composition at the current point. */
13716 return (pt > BUF_BEGV (buf) && pt < BUF_ZV (buf)
13717 && find_composition (pt, -1, &start, &end, &prop, buffer)
13718 && composition_valid_p (start, end, prop)
13719 && start < pt && end > pt);
13722 /* Reconsider the clip changes of buffer which is displayed in W. */
13724 static void
13725 reconsider_clip_changes (struct window *w)
13727 struct buffer *b = XBUFFER (w->contents);
13729 if (b->clip_changed
13730 && w->window_end_valid
13731 && w->current_matrix->buffer == b
13732 && w->current_matrix->zv == BUF_ZV (b)
13733 && w->current_matrix->begv == BUF_BEGV (b))
13734 b->clip_changed = false;
13736 /* If display wasn't paused, and W is not a tool bar window, see if
13737 point has been moved into or out of a composition. In that case,
13738 set b->clip_changed to force updating the screen. If
13739 b->clip_changed has already been set, skip this check. */
13740 if (!b->clip_changed && w->window_end_valid)
13742 ptrdiff_t pt = (w == XWINDOW (selected_window)
13743 ? PT : marker_position (w->pointm));
13745 if ((w->current_matrix->buffer != b || pt != w->last_point)
13746 && check_point_in_composition (w->current_matrix->buffer,
13747 w->last_point, b, pt))
13748 b->clip_changed = true;
13752 static void
13753 propagate_buffer_redisplay (void)
13754 { /* Resetting b->text->redisplay is problematic!
13755 We can't just reset it in the case that some window that displays
13756 it has not been redisplayed; and such a window can stay
13757 unredisplayed for a long time if it's currently invisible.
13758 But we do want to reset it at the end of redisplay otherwise
13759 its displayed windows will keep being redisplayed over and over
13760 again.
13761 So we copy all b->text->redisplay flags up to their windows here,
13762 such that mark_window_display_accurate can safely reset
13763 b->text->redisplay. */
13764 Lisp_Object ws = window_list ();
13765 for (; CONSP (ws); ws = XCDR (ws))
13767 struct window *thisw = XWINDOW (XCAR (ws));
13768 struct buffer *thisb = XBUFFER (thisw->contents);
13769 if (thisb->text->redisplay)
13770 thisw->redisplay = true;
13774 #define STOP_POLLING \
13775 do { if (! polling_stopped_here) stop_polling (); \
13776 polling_stopped_here = true; } while (false)
13778 #define RESUME_POLLING \
13779 do { if (polling_stopped_here) start_polling (); \
13780 polling_stopped_here = false; } while (false)
13783 /* Perhaps in the future avoid recentering windows if it
13784 is not necessary; currently that causes some problems. */
13786 static void
13787 redisplay_internal (void)
13789 struct window *w = XWINDOW (selected_window);
13790 struct window *sw;
13791 struct frame *fr;
13792 bool pending;
13793 bool must_finish = false, match_p;
13794 struct text_pos tlbufpos, tlendpos;
13795 int number_of_visible_frames;
13796 ptrdiff_t count;
13797 struct frame *sf;
13798 bool polling_stopped_here = false;
13799 Lisp_Object tail, frame;
13801 /* Set a limit to the number of retries we perform due to horizontal
13802 scrolling, this avoids getting stuck in an uninterruptible
13803 infinite loop (Bug #24633). */
13804 enum { MAX_HSCROLL_RETRIES = 16 };
13805 int hscroll_retries = 0;
13807 /* Limit the number of retries for when frame(s) become garbaged as
13808 result of redisplaying them. Some packages set various redisplay
13809 hooks, such as window-scroll-functions, to run Lisp that always
13810 calls APIs which cause the frame's garbaged flag to become set,
13811 so we loop indefinitely. */
13812 enum {MAX_GARBAGED_FRAME_RETRIES = 2 };
13813 int garbaged_frame_retries = 0;
13815 /* True means redisplay has to consider all windows on all
13816 frames. False, only selected_window is considered. */
13817 bool consider_all_windows_p;
13819 /* True means redisplay has to redisplay the miniwindow. */
13820 bool update_miniwindow_p = false;
13822 TRACE ((stderr, "redisplay_internal %d\n", redisplaying_p));
13824 /* No redisplay if running in batch mode or frame is not yet fully
13825 initialized, or redisplay is explicitly turned off by setting
13826 Vinhibit_redisplay. */
13827 if (FRAME_INITIAL_P (SELECTED_FRAME ())
13828 || !NILP (Vinhibit_redisplay))
13829 return;
13831 /* Don't examine these until after testing Vinhibit_redisplay.
13832 When Emacs is shutting down, perhaps because its connection to
13833 X has dropped, we should not look at them at all. */
13834 fr = XFRAME (w->frame);
13835 sf = SELECTED_FRAME ();
13837 if (!fr->glyphs_initialized_p)
13838 return;
13840 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS)
13841 if (popup_activated ())
13842 return;
13843 #endif
13845 /* I don't think this happens but let's be paranoid. */
13846 if (redisplaying_p)
13847 return;
13849 /* Record a function that clears redisplaying_p
13850 when we leave this function. */
13851 count = SPECPDL_INDEX ();
13852 record_unwind_protect_void (unwind_redisplay);
13853 redisplaying_p = true;
13854 block_buffer_flips ();
13855 specbind (Qinhibit_free_realized_faces, Qnil);
13857 /* Record this function, so it appears on the profiler's backtraces. */
13858 record_in_backtrace (Qredisplay_internal_xC_functionx, 0, 0);
13860 FOR_EACH_FRAME (tail, frame)
13861 XFRAME (frame)->already_hscrolled_p = false;
13863 retry:
13864 /* Remember the currently selected window. */
13865 sw = w;
13867 pending = false;
13868 forget_escape_and_glyphless_faces ();
13870 inhibit_free_realized_faces = false;
13872 /* If face_change, init_iterator will free all realized faces, which
13873 includes the faces referenced from current matrices. So, we
13874 can't reuse current matrices in this case. */
13875 if (face_change)
13876 windows_or_buffers_changed = 47;
13878 if ((FRAME_TERMCAP_P (sf) || FRAME_MSDOS_P (sf))
13879 && FRAME_TTY (sf)->previous_frame != sf)
13881 /* Since frames on a single ASCII terminal share the same
13882 display area, displaying a different frame means redisplay
13883 the whole thing. */
13884 SET_FRAME_GARBAGED (sf);
13885 #ifndef DOS_NT
13886 set_tty_color_mode (FRAME_TTY (sf), sf);
13887 #endif
13888 FRAME_TTY (sf)->previous_frame = sf;
13891 /* Set the visible flags for all frames. Do this before checking for
13892 resized or garbaged frames; they want to know if their frames are
13893 visible. See the comment in frame.h for FRAME_SAMPLE_VISIBILITY. */
13894 number_of_visible_frames = 0;
13896 FOR_EACH_FRAME (tail, frame)
13898 struct frame *f = XFRAME (frame);
13900 if (FRAME_VISIBLE_P (f))
13902 ++number_of_visible_frames;
13903 /* Adjust matrices for visible frames only. */
13904 if (f->fonts_changed)
13906 adjust_frame_glyphs (f);
13907 /* Disable all redisplay optimizations for this frame.
13908 This is because adjust_frame_glyphs resets the
13909 enabled_p flag for all glyph rows of all windows, so
13910 many optimizations will fail anyway, and some might
13911 fail to test that flag and do bogus things as
13912 result. */
13913 SET_FRAME_GARBAGED (f);
13914 f->fonts_changed = false;
13916 /* If cursor type has been changed on the frame
13917 other than selected, consider all frames. */
13918 if (f != sf && f->cursor_type_changed)
13919 fset_redisplay (f);
13921 clear_desired_matrices (f);
13924 /* Notice any pending interrupt request to change frame size. */
13925 do_pending_window_change (true);
13927 /* do_pending_window_change could change the selected_window due to
13928 frame resizing which makes the selected window too small. */
13929 if (WINDOWP (selected_window) && (w = XWINDOW (selected_window)) != sw)
13930 sw = w;
13932 /* Clear frames marked as garbaged. */
13933 clear_garbaged_frames ();
13935 /* Build menubar and tool-bar items. */
13936 if (NILP (Vmemory_full))
13937 prepare_menu_bars ();
13939 reconsider_clip_changes (w);
13941 /* In most cases selected window displays current buffer. */
13942 match_p = XBUFFER (w->contents) == current_buffer;
13943 if (match_p)
13945 /* Detect case that we need to write or remove a star in the mode line. */
13946 if ((SAVE_MODIFF < MODIFF) != w->last_had_star)
13947 w->update_mode_line = true;
13949 if (mode_line_update_needed (w))
13950 w->update_mode_line = true;
13952 /* If reconsider_clip_changes above decided that the narrowing
13953 in the current buffer changed, make sure all other windows
13954 showing that buffer will be redisplayed. */
13955 if (current_buffer->clip_changed)
13956 bset_update_mode_line (current_buffer);
13959 /* Normally the message* functions will have already displayed and
13960 updated the echo area, but the frame may have been trashed, or
13961 the update may have been preempted, so display the echo area
13962 again here. Checking message_cleared_p captures the case that
13963 the echo area should be cleared. */
13964 if ((!NILP (echo_area_buffer[0]) && !display_last_displayed_message_p)
13965 || (!NILP (echo_area_buffer[1]) && display_last_displayed_message_p)
13966 || (message_cleared_p
13967 && minibuf_level == 0
13968 /* If the mini-window is currently selected, this means the
13969 echo-area doesn't show through. */
13970 && !MINI_WINDOW_P (XWINDOW (selected_window))))
13972 echo_area_display (false);
13974 /* If echo_area_display resizes the mini-window, the redisplay and
13975 window_sizes_changed flags of the selected frame are set, but
13976 it's too late for the hooks in window-size-change-functions,
13977 which have been examined already in prepare_menu_bars. So in
13978 that case we call the hooks here only for the selected frame. */
13979 if (sf->redisplay)
13981 ptrdiff_t count1 = SPECPDL_INDEX ();
13983 record_unwind_save_match_data ();
13984 run_window_size_change_functions (selected_frame);
13985 unbind_to (count1, Qnil);
13988 if (message_cleared_p)
13989 update_miniwindow_p = true;
13991 must_finish = true;
13993 /* If we don't display the current message, don't clear the
13994 message_cleared_p flag, because, if we did, we wouldn't clear
13995 the echo area in the next redisplay which doesn't preserve
13996 the echo area. */
13997 if (!display_last_displayed_message_p)
13998 message_cleared_p = false;
14000 else if (EQ (selected_window, minibuf_window)
14001 && (current_buffer->clip_changed || window_outdated (w))
14002 && resize_mini_window (w, false))
14004 if (sf->redisplay)
14006 ptrdiff_t count1 = SPECPDL_INDEX ();
14008 record_unwind_save_match_data ();
14009 run_window_size_change_functions (selected_frame);
14010 unbind_to (count1, Qnil);
14013 /* Resized active mini-window to fit the size of what it is
14014 showing if its contents might have changed. */
14015 must_finish = true;
14017 /* If window configuration was changed, frames may have been
14018 marked garbaged. Clear them or we will experience
14019 surprises wrt scrolling. */
14020 clear_garbaged_frames ();
14023 if (windows_or_buffers_changed && !update_mode_lines)
14024 /* Code that sets windows_or_buffers_changed doesn't distinguish whether
14025 only the windows's contents needs to be refreshed, or whether the
14026 mode-lines also need a refresh. */
14027 update_mode_lines = (windows_or_buffers_changed == REDISPLAY_SOME
14028 ? REDISPLAY_SOME : 32);
14030 /* If specs for an arrow have changed, do thorough redisplay
14031 to ensure we remove any arrow that should no longer exist. */
14032 /* Apparently, this is the only case where we update other windows,
14033 without updating other mode-lines. */
14034 overlay_arrows_changed_p (true);
14036 consider_all_windows_p = (update_mode_lines
14037 || windows_or_buffers_changed);
14039 #define AINC(a,i) \
14041 Lisp_Object entry = Fgethash (make_number (i), a, make_number (0)); \
14042 if (INTEGERP (entry)) \
14043 Fputhash (make_number (i), make_number (1 + XINT (entry)), a); \
14046 AINC (Vredisplay__all_windows_cause, windows_or_buffers_changed);
14047 AINC (Vredisplay__mode_lines_cause, update_mode_lines);
14049 /* Optimize the case that only the line containing the cursor in the
14050 selected window has changed. Variables starting with this_ are
14051 set in display_line and record information about the line
14052 containing the cursor. */
14053 tlbufpos = this_line_start_pos;
14054 tlendpos = this_line_end_pos;
14055 if (!consider_all_windows_p
14056 && CHARPOS (tlbufpos) > 0
14057 && !w->update_mode_line
14058 && !current_buffer->clip_changed
14059 && !current_buffer->prevent_redisplay_optimizations_p
14060 && FRAME_VISIBLE_P (XFRAME (w->frame))
14061 && !FRAME_OBSCURED_P (XFRAME (w->frame))
14062 && !XFRAME (w->frame)->cursor_type_changed
14063 && !XFRAME (w->frame)->face_change
14064 /* Make sure recorded data applies to current buffer, etc. */
14065 && this_line_buffer == current_buffer
14066 && match_p
14067 && !w->force_start
14068 && !w->optional_new_start
14069 /* Point must be on the line that we have info recorded about. */
14070 && PT >= CHARPOS (tlbufpos)
14071 && PT <= Z - CHARPOS (tlendpos)
14072 /* All text outside that line, including its final newline,
14073 must be unchanged. */
14074 && text_outside_line_unchanged_p (w, CHARPOS (tlbufpos),
14075 CHARPOS (tlendpos)))
14077 if (CHARPOS (tlbufpos) > BEGV
14078 && FETCH_BYTE (BYTEPOS (tlbufpos) - 1) != '\n'
14079 && (CHARPOS (tlbufpos) == ZV
14080 || FETCH_BYTE (BYTEPOS (tlbufpos)) == '\n'))
14081 /* Former continuation line has disappeared by becoming empty. */
14082 goto cancel;
14083 else if (window_outdated (w) || MINI_WINDOW_P (w))
14085 /* We have to handle the case of continuation around a
14086 wide-column character (see the comment in indent.c around
14087 line 1340).
14089 For instance, in the following case:
14091 -------- Insert --------
14092 K_A_N_\\ `a' K_A_N_a\ `X_' are wide-column chars.
14093 J_I_ ==> J_I_ `^^' are cursors.
14094 ^^ ^^
14095 -------- --------
14097 As we have to redraw the line above, we cannot use this
14098 optimization. */
14100 struct it it;
14101 int line_height_before = this_line_pixel_height;
14103 /* Note that start_display will handle the case that the
14104 line starting at tlbufpos is a continuation line. */
14105 start_display (&it, w, tlbufpos);
14107 /* Implementation note: It this still necessary? */
14108 if (it.current_x != this_line_start_x)
14109 goto cancel;
14111 TRACE ((stderr, "trying display optimization 1\n"));
14112 w->cursor.vpos = -1;
14113 overlay_arrow_seen = false;
14114 it.vpos = this_line_vpos;
14115 it.current_y = this_line_y;
14116 it.glyph_row = MATRIX_ROW (w->desired_matrix, this_line_vpos);
14117 display_line (&it, -1);
14119 /* If line contains point, is not continued,
14120 and ends at same distance from eob as before, we win. */
14121 if (w->cursor.vpos >= 0
14122 /* Line is not continued, otherwise this_line_start_pos
14123 would have been set to 0 in display_line. */
14124 && CHARPOS (this_line_start_pos)
14125 /* Line ends as before. */
14126 && CHARPOS (this_line_end_pos) == CHARPOS (tlendpos)
14127 /* Line has same height as before. Otherwise other lines
14128 would have to be shifted up or down. */
14129 && this_line_pixel_height == line_height_before)
14131 /* If this is not the window's last line, we must adjust
14132 the charstarts of the lines below. */
14133 if (it.current_y < it.last_visible_y)
14135 struct glyph_row *row
14136 = MATRIX_ROW (w->current_matrix, this_line_vpos + 1);
14137 ptrdiff_t delta, delta_bytes;
14139 /* We used to distinguish between two cases here,
14140 conditioned by Z - CHARPOS (tlendpos) == ZV, for
14141 when the line ends in a newline or the end of the
14142 buffer's accessible portion. But both cases did
14143 the same, so they were collapsed. */
14144 delta = (Z
14145 - CHARPOS (tlendpos)
14146 - MATRIX_ROW_START_CHARPOS (row));
14147 delta_bytes = (Z_BYTE
14148 - BYTEPOS (tlendpos)
14149 - MATRIX_ROW_START_BYTEPOS (row));
14151 increment_matrix_positions (w->current_matrix,
14152 this_line_vpos + 1,
14153 w->current_matrix->nrows,
14154 delta, delta_bytes);
14157 /* If this row displays text now but previously didn't,
14158 or vice versa, w->window_end_vpos may have to be
14159 adjusted. */
14160 if (MATRIX_ROW_DISPLAYS_TEXT_P (it.glyph_row - 1))
14162 if (w->window_end_vpos < this_line_vpos)
14163 w->window_end_vpos = this_line_vpos;
14165 else if (w->window_end_vpos == this_line_vpos
14166 && this_line_vpos > 0)
14167 w->window_end_vpos = this_line_vpos - 1;
14168 w->window_end_valid = false;
14170 /* Update hint: No need to try to scroll in update_window. */
14171 w->desired_matrix->no_scrolling_p = true;
14173 #ifdef GLYPH_DEBUG
14174 *w->desired_matrix->method = 0;
14175 debug_method_add (w, "optimization 1");
14176 #endif
14177 #ifdef HAVE_WINDOW_SYSTEM
14178 update_window_fringes (w, false);
14179 #endif
14180 goto update;
14182 else
14183 goto cancel;
14185 else if (/* Cursor position hasn't changed. */
14186 PT == w->last_point
14187 /* Make sure the cursor was last displayed
14188 in this window. Otherwise we have to reposition it. */
14190 /* PXW: Must be converted to pixels, probably. */
14191 && 0 <= w->cursor.vpos
14192 && w->cursor.vpos < WINDOW_TOTAL_LINES (w))
14194 if (!must_finish)
14196 do_pending_window_change (true);
14197 /* If selected_window changed, redisplay again. */
14198 if (WINDOWP (selected_window)
14199 && (w = XWINDOW (selected_window)) != sw)
14200 goto retry;
14202 /* We used to always goto end_of_redisplay here, but this
14203 isn't enough if we have a blinking cursor. */
14204 if (w->cursor_off_p == w->last_cursor_off_p)
14205 goto end_of_redisplay;
14207 goto update;
14209 /* If highlighting the region, or if the cursor is in the echo area,
14210 then we can't just move the cursor. */
14211 else if (NILP (Vshow_trailing_whitespace)
14212 && !cursor_in_echo_area)
14214 struct it it;
14215 struct glyph_row *row;
14217 /* Skip from tlbufpos to PT and see where it is. Note that
14218 PT may be in invisible text. If so, we will end at the
14219 next visible position. */
14220 init_iterator (&it, w, CHARPOS (tlbufpos), BYTEPOS (tlbufpos),
14221 NULL, DEFAULT_FACE_ID);
14222 it.current_x = this_line_start_x;
14223 it.current_y = this_line_y;
14224 it.vpos = this_line_vpos;
14226 /* The call to move_it_to stops in front of PT, but
14227 moves over before-strings. */
14228 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
14230 if (it.vpos == this_line_vpos
14231 && (row = MATRIX_ROW (w->current_matrix, this_line_vpos),
14232 row->enabled_p))
14234 eassert (this_line_vpos == it.vpos);
14235 eassert (this_line_y == it.current_y);
14236 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
14237 if (cursor_row_fully_visible_p (w, false, true))
14239 #ifdef GLYPH_DEBUG
14240 *w->desired_matrix->method = 0;
14241 debug_method_add (w, "optimization 3");
14242 #endif
14243 goto update;
14245 else
14246 goto cancel;
14248 else
14249 goto cancel;
14252 cancel:
14253 /* Text changed drastically or point moved off of line. */
14254 SET_MATRIX_ROW_ENABLED_P (w->desired_matrix, this_line_vpos, false);
14257 CHARPOS (this_line_start_pos) = 0;
14258 ++clear_face_cache_count;
14259 #ifdef HAVE_WINDOW_SYSTEM
14260 ++clear_image_cache_count;
14261 #endif
14263 /* Build desired matrices, and update the display. If
14264 consider_all_windows_p, do it for all windows on all frames that
14265 require redisplay, as specified by their 'redisplay' flag.
14266 Otherwise do it for selected_window, only. */
14268 if (consider_all_windows_p)
14270 FOR_EACH_FRAME (tail, frame)
14271 XFRAME (frame)->updated_p = false;
14273 propagate_buffer_redisplay ();
14275 FOR_EACH_FRAME (tail, frame)
14277 struct frame *f = XFRAME (frame);
14279 /* We don't have to do anything for unselected terminal
14280 frames. */
14281 if ((FRAME_TERMCAP_P (f) || FRAME_MSDOS_P (f))
14282 && !EQ (FRAME_TTY (f)->top_frame, frame))
14283 continue;
14285 retry_frame:
14286 if (FRAME_WINDOW_P (f) || FRAME_TERMCAP_P (f) || f == sf)
14288 bool gcscrollbars
14289 /* Only GC scrollbars when we redisplay the whole frame. */
14290 = f->redisplay || !REDISPLAY_SOME_P ();
14291 bool f_redisplay_flag = f->redisplay;
14292 /* Mark all the scroll bars to be removed; we'll redeem
14293 the ones we want when we redisplay their windows. */
14294 if (gcscrollbars && FRAME_TERMINAL (f)->condemn_scroll_bars_hook)
14295 FRAME_TERMINAL (f)->condemn_scroll_bars_hook (f);
14297 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
14298 redisplay_windows (FRAME_ROOT_WINDOW (f));
14299 /* Remember that the invisible frames need to be redisplayed next
14300 time they're visible. */
14301 else if (!REDISPLAY_SOME_P ())
14302 f->redisplay = true;
14304 /* The X error handler may have deleted that frame. */
14305 if (!FRAME_LIVE_P (f))
14306 continue;
14308 /* Any scroll bars which redisplay_windows should have
14309 nuked should now go away. */
14310 if (gcscrollbars && FRAME_TERMINAL (f)->judge_scroll_bars_hook)
14311 FRAME_TERMINAL (f)->judge_scroll_bars_hook (f);
14313 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
14315 /* If fonts changed on visible frame, display again. */
14316 if (f->fonts_changed)
14318 adjust_frame_glyphs (f);
14319 /* Disable all redisplay optimizations for this
14320 frame. For the reasons, see the comment near
14321 the previous call to adjust_frame_glyphs above. */
14322 SET_FRAME_GARBAGED (f);
14323 f->fonts_changed = false;
14324 goto retry_frame;
14327 /* See if we have to hscroll. */
14328 if (!f->already_hscrolled_p)
14330 f->already_hscrolled_p = true;
14331 if (hscroll_retries <= MAX_HSCROLL_RETRIES
14332 && hscroll_windows (f->root_window))
14334 hscroll_retries++;
14335 goto retry_frame;
14339 /* If the frame's redisplay flag was not set before
14340 we went about redisplaying its windows, but it is
14341 set now, that means we employed some redisplay
14342 optimizations inside redisplay_windows, and
14343 bypassed producing some screen lines. But if
14344 f->redisplay is now set, it might mean the old
14345 faces are no longer valid (e.g., if redisplaying
14346 some window called some Lisp which defined a new
14347 face or redefined an existing face), so trying to
14348 use them in update_frame will segfault.
14349 Therefore, we must redisplay this frame. */
14350 if (!f_redisplay_flag && f->redisplay)
14351 goto retry_frame;
14352 /* In some case (e.g., window resize), we notice
14353 only during window updating that the window
14354 content changed unpredictably (e.g., a GTK
14355 scrollbar moved, or some Lisp hook that winds up
14356 calling adjust_frame_glyphs) and that our
14357 previous estimation of the frame content was
14358 garbage. We have to start over. These cases
14359 should be rare, so going all the way back to the
14360 top of redisplay should be good enough. */
14361 if (FRAME_GARBAGED_P (f)
14362 && garbaged_frame_retries++ < MAX_GARBAGED_FRAME_RETRIES)
14363 goto retry;
14365 #if defined (HAVE_WINDOW_SYSTEM) && !defined (HAVE_NS)
14366 x_clear_under_internal_border (f);
14367 #endif /* HAVE_WINDOW_SYSTEM && !HAVE_NS */
14369 /* Prevent various kinds of signals during display
14370 update. stdio is not robust about handling
14371 signals, which can cause an apparent I/O error. */
14372 if (interrupt_input)
14373 unrequest_sigio ();
14374 STOP_POLLING;
14376 pending |= update_frame (f, false, false);
14377 f->cursor_type_changed = false;
14378 f->updated_p = true;
14383 eassert (EQ (XFRAME (selected_frame)->selected_window, selected_window));
14385 if (!pending)
14387 /* Do the mark_window_display_accurate after all windows have
14388 been redisplayed because this call resets flags in buffers
14389 which are needed for proper redisplay. */
14390 FOR_EACH_FRAME (tail, frame)
14392 struct frame *f = XFRAME (frame);
14393 if (f->updated_p)
14395 f->redisplay = false;
14396 f->garbaged = false;
14397 mark_window_display_accurate (f->root_window, true);
14398 if (FRAME_TERMINAL (f)->frame_up_to_date_hook)
14399 FRAME_TERMINAL (f)->frame_up_to_date_hook (f);
14404 else if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
14406 displayed_buffer = XBUFFER (XWINDOW (selected_window)->contents);
14407 /* Use list_of_error, not Qerror, so that
14408 we catch only errors and don't run the debugger. */
14409 internal_condition_case_1 (redisplay_window_1, selected_window,
14410 list_of_error,
14411 redisplay_window_error);
14412 if (update_miniwindow_p)
14413 internal_condition_case_1 (redisplay_window_1,
14414 FRAME_MINIBUF_WINDOW (sf), list_of_error,
14415 redisplay_window_error);
14417 /* Compare desired and current matrices, perform output. */
14419 update:
14420 /* If fonts changed, display again. Likewise if redisplay_window_1
14421 above caused some change (e.g., a change in faces) that requires
14422 considering the entire frame again. */
14423 if (sf->fonts_changed || sf->redisplay)
14425 if (sf->redisplay)
14427 /* Set this to force a more thorough redisplay.
14428 Otherwise, we might immediately loop back to the
14429 above "else-if" clause (since all the conditions that
14430 led here might still be true), and we will then
14431 infloop, because the selected-frame's redisplay flag
14432 is not (and cannot be) reset. */
14433 windows_or_buffers_changed = 50;
14435 goto retry;
14438 /* Prevent freeing of realized faces, since desired matrices are
14439 pending that reference the faces we computed and cached. */
14440 inhibit_free_realized_faces = true;
14442 /* Prevent various kinds of signals during display update.
14443 stdio is not robust about handling signals,
14444 which can cause an apparent I/O error. */
14445 if (interrupt_input)
14446 unrequest_sigio ();
14447 STOP_POLLING;
14449 if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
14451 if (hscroll_retries <= MAX_HSCROLL_RETRIES
14452 && hscroll_windows (selected_window))
14454 hscroll_retries++;
14455 goto retry;
14458 XWINDOW (selected_window)->must_be_updated_p = true;
14459 pending = update_frame (sf, false, false);
14460 sf->cursor_type_changed = false;
14463 /* We may have called echo_area_display at the top of this
14464 function. If the echo area is on another frame, that may
14465 have put text on a frame other than the selected one, so the
14466 above call to update_frame would not have caught it. Catch
14467 it here. */
14468 Lisp_Object mini_window = FRAME_MINIBUF_WINDOW (sf);
14469 struct frame *mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
14471 if (mini_frame != sf && FRAME_WINDOW_P (mini_frame))
14473 XWINDOW (mini_window)->must_be_updated_p = true;
14474 pending |= update_frame (mini_frame, false, false);
14475 mini_frame->cursor_type_changed = false;
14476 if (!pending && hscroll_retries <= MAX_HSCROLL_RETRIES
14477 && hscroll_windows (mini_window))
14479 hscroll_retries++;
14480 goto retry;
14485 /* If display was paused because of pending input, make sure we do a
14486 thorough update the next time. */
14487 if (pending)
14489 /* Prevent the optimization at the beginning of
14490 redisplay_internal that tries a single-line update of the
14491 line containing the cursor in the selected window. */
14492 CHARPOS (this_line_start_pos) = 0;
14494 /* Let the overlay arrow be updated the next time. */
14495 update_overlay_arrows (0);
14497 /* If we pause after scrolling, some rows in the current
14498 matrices of some windows are not valid. */
14499 if (!WINDOW_FULL_WIDTH_P (w)
14500 && !FRAME_WINDOW_P (XFRAME (w->frame)))
14501 update_mode_lines = 36;
14503 else
14505 if (!consider_all_windows_p)
14507 /* This has already been done above if
14508 consider_all_windows_p is set. */
14509 if (XBUFFER (w->contents)->text->redisplay
14510 && buffer_window_count (XBUFFER (w->contents)) > 1)
14511 /* This can happen if b->text->redisplay was set during
14512 jit-lock. */
14513 propagate_buffer_redisplay ();
14514 mark_window_display_accurate_1 (w, true);
14516 /* Say overlay arrows are up to date. */
14517 update_overlay_arrows (1);
14519 if (FRAME_TERMINAL (sf)->frame_up_to_date_hook != 0)
14520 FRAME_TERMINAL (sf)->frame_up_to_date_hook (sf);
14523 update_mode_lines = 0;
14524 windows_or_buffers_changed = 0;
14527 /* Start SIGIO interrupts coming again. Having them off during the
14528 code above makes it less likely one will discard output, but not
14529 impossible, since there might be stuff in the system buffer here.
14530 But it is much hairier to try to do anything about that. */
14531 if (interrupt_input)
14532 request_sigio ();
14533 RESUME_POLLING;
14535 /* If a frame has become visible which was not before, redisplay
14536 again, so that we display it. Expose events for such a frame
14537 (which it gets when becoming visible) don't call the parts of
14538 redisplay constructing glyphs, so simply exposing a frame won't
14539 display anything in this case. So, we have to display these
14540 frames here explicitly. */
14541 if (!pending)
14543 int new_count = 0;
14545 FOR_EACH_FRAME (tail, frame)
14547 if (XFRAME (frame)->visible)
14548 new_count++;
14551 if (new_count != number_of_visible_frames)
14552 windows_or_buffers_changed = 52;
14555 /* Change frame size now if a change is pending. */
14556 do_pending_window_change (true);
14558 /* If we just did a pending size change, or have additional
14559 visible frames, or selected_window changed, redisplay again. */
14560 if ((windows_or_buffers_changed && !pending)
14561 || (WINDOWP (selected_window) && (w = XWINDOW (selected_window)) != sw))
14562 goto retry;
14564 /* Clear the face and image caches.
14566 We used to do this only if consider_all_windows_p. But the cache
14567 needs to be cleared if a timer creates images in the current
14568 buffer (e.g. the test case in Bug#6230). */
14570 if (clear_face_cache_count > CLEAR_FACE_CACHE_COUNT)
14572 clear_face_cache (false);
14573 clear_face_cache_count = 0;
14576 #ifdef HAVE_WINDOW_SYSTEM
14577 if (clear_image_cache_count > CLEAR_IMAGE_CACHE_COUNT)
14579 clear_image_caches (Qnil);
14580 clear_image_cache_count = 0;
14582 #endif /* HAVE_WINDOW_SYSTEM */
14584 end_of_redisplay:
14585 #ifdef HAVE_NS
14586 ns_set_doc_edited ();
14587 #endif
14588 if (interrupt_input && interrupts_deferred)
14589 request_sigio ();
14591 unbind_to (count, Qnil);
14592 RESUME_POLLING;
14595 static void
14596 unwind_redisplay_preserve_echo_area (void)
14598 unblock_buffer_flips ();
14601 /* Redisplay, but leave alone any recent echo area message unless
14602 another message has been requested in its place.
14604 This is useful in situations where you need to redisplay but no
14605 user action has occurred, making it inappropriate for the message
14606 area to be cleared. See tracking_off and
14607 wait_reading_process_output for examples of these situations.
14609 FROM_WHERE is an integer saying from where this function was
14610 called. This is useful for debugging. */
14612 void
14613 redisplay_preserve_echo_area (int from_where)
14615 TRACE ((stderr, "redisplay_preserve_echo_area (%d)\n", from_where));
14617 block_input ();
14618 ptrdiff_t count = SPECPDL_INDEX ();
14619 record_unwind_protect_void (unwind_redisplay_preserve_echo_area);
14620 block_buffer_flips ();
14621 unblock_input ();
14623 if (!NILP (echo_area_buffer[1]))
14625 /* We have a previously displayed message, but no current
14626 message. Redisplay the previous message. */
14627 display_last_displayed_message_p = true;
14628 redisplay_internal ();
14629 display_last_displayed_message_p = false;
14631 else
14632 redisplay_internal ();
14634 flush_frame (SELECTED_FRAME ());
14635 unbind_to (count, Qnil);
14639 /* Function registered with record_unwind_protect in redisplay_internal. */
14641 static void
14642 unwind_redisplay (void)
14644 redisplaying_p = false;
14645 unblock_buffer_flips ();
14649 /* Mark the display of leaf window W as accurate or inaccurate.
14650 If ACCURATE_P, mark display of W as accurate.
14651 If !ACCURATE_P, arrange for W to be redisplayed the next
14652 time redisplay_internal is called. */
14654 static void
14655 mark_window_display_accurate_1 (struct window *w, bool accurate_p)
14657 struct buffer *b = XBUFFER (w->contents);
14659 w->last_modified = accurate_p ? BUF_MODIFF (b) : 0;
14660 w->last_overlay_modified = accurate_p ? BUF_OVERLAY_MODIFF (b) : 0;
14661 w->last_had_star = BUF_MODIFF (b) > BUF_SAVE_MODIFF (b);
14663 if (accurate_p)
14665 b->clip_changed = false;
14666 b->prevent_redisplay_optimizations_p = false;
14667 eassert (buffer_window_count (b) > 0);
14668 /* Resetting b->text->redisplay is problematic!
14669 In order to make it safer to do it here, redisplay_internal must
14670 have copied all b->text->redisplay to their respective windows. */
14671 b->text->redisplay = false;
14673 BUF_UNCHANGED_MODIFIED (b) = BUF_MODIFF (b);
14674 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = BUF_OVERLAY_MODIFF (b);
14675 BUF_BEG_UNCHANGED (b) = BUF_GPT (b) - BUF_BEG (b);
14676 BUF_END_UNCHANGED (b) = BUF_Z (b) - BUF_GPT (b);
14678 w->current_matrix->buffer = b;
14679 w->current_matrix->begv = BUF_BEGV (b);
14680 w->current_matrix->zv = BUF_ZV (b);
14682 w->last_cursor_vpos = w->cursor.vpos;
14683 w->last_cursor_off_p = w->cursor_off_p;
14685 if (w == XWINDOW (selected_window))
14686 w->last_point = BUF_PT (b);
14687 else
14688 w->last_point = marker_position (w->pointm);
14690 w->window_end_valid = true;
14691 w->update_mode_line = false;
14694 w->redisplay = !accurate_p;
14698 /* Mark the display of windows in the window tree rooted at WINDOW as
14699 accurate or inaccurate. If ACCURATE_P, mark display of
14700 windows as accurate. If !ACCURATE_P, arrange for windows to
14701 be redisplayed the next time redisplay_internal is called. */
14703 void
14704 mark_window_display_accurate (Lisp_Object window, bool accurate_p)
14706 struct window *w;
14708 for (; !NILP (window); window = w->next)
14710 w = XWINDOW (window);
14711 if (WINDOWP (w->contents))
14712 mark_window_display_accurate (w->contents, accurate_p);
14713 else
14714 mark_window_display_accurate_1 (w, accurate_p);
14717 if (accurate_p)
14718 update_overlay_arrows (1);
14719 else
14720 /* Force a thorough redisplay the next time by setting
14721 last_arrow_position and last_arrow_string to t, which is
14722 unequal to any useful value of Voverlay_arrow_... */
14723 update_overlay_arrows (-1);
14727 /* Return value in display table DP (Lisp_Char_Table *) for character
14728 C. Since a display table doesn't have any parent, we don't have to
14729 follow parent. Do not call this function directly but use the
14730 macro DISP_CHAR_VECTOR. */
14732 Lisp_Object
14733 disp_char_vector (struct Lisp_Char_Table *dp, int c)
14735 Lisp_Object val;
14737 if (ASCII_CHAR_P (c))
14739 val = dp->ascii;
14740 if (SUB_CHAR_TABLE_P (val))
14741 val = XSUB_CHAR_TABLE (val)->contents[c];
14743 else
14745 Lisp_Object table;
14747 XSETCHAR_TABLE (table, dp);
14748 val = char_table_ref (table, c);
14750 if (NILP (val))
14751 val = dp->defalt;
14752 return val;
14755 static int buffer_flip_blocked_depth;
14757 static void
14758 block_buffer_flips (void)
14760 eassert (buffer_flip_blocked_depth >= 0);
14761 buffer_flip_blocked_depth++;
14764 static void
14765 unblock_buffer_flips (void)
14767 eassert (buffer_flip_blocked_depth > 0);
14768 if (--buffer_flip_blocked_depth == 0)
14770 Lisp_Object tail, frame;
14771 block_input ();
14772 FOR_EACH_FRAME (tail, frame)
14774 struct frame *f = XFRAME (frame);
14775 if (FRAME_TERMINAL (f)->buffer_flipping_unblocked_hook)
14776 (*FRAME_TERMINAL (f)->buffer_flipping_unblocked_hook) (f);
14778 unblock_input ();
14782 bool
14783 buffer_flipping_blocked_p (void)
14785 return buffer_flip_blocked_depth > 0;
14789 /***********************************************************************
14790 Window Redisplay
14791 ***********************************************************************/
14793 /* Redisplay all leaf windows in the window tree rooted at WINDOW. */
14795 static void
14796 redisplay_windows (Lisp_Object window)
14798 while (!NILP (window))
14800 struct window *w = XWINDOW (window);
14802 if (WINDOWP (w->contents))
14803 redisplay_windows (w->contents);
14804 else if (BUFFERP (w->contents))
14806 displayed_buffer = XBUFFER (w->contents);
14807 /* Use list_of_error, not Qerror, so that
14808 we catch only errors and don't run the debugger. */
14809 internal_condition_case_1 (redisplay_window_0, window,
14810 list_of_error,
14811 redisplay_window_error);
14814 window = w->next;
14818 static Lisp_Object
14819 redisplay_window_error (Lisp_Object ignore)
14821 displayed_buffer->display_error_modiff = BUF_MODIFF (displayed_buffer);
14822 return Qnil;
14825 static Lisp_Object
14826 redisplay_window_0 (Lisp_Object window)
14828 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
14829 redisplay_window (window, false);
14830 return Qnil;
14833 static Lisp_Object
14834 redisplay_window_1 (Lisp_Object window)
14836 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
14837 redisplay_window (window, true);
14838 return Qnil;
14842 /* Set cursor position of W. PT is assumed to be displayed in ROW.
14843 DELTA and DELTA_BYTES are the numbers of characters and bytes by
14844 which positions recorded in ROW differ from current buffer
14845 positions.
14847 Return true iff cursor is on this row. */
14849 static bool
14850 set_cursor_from_row (struct window *w, struct glyph_row *row,
14851 struct glyph_matrix *matrix,
14852 ptrdiff_t delta, ptrdiff_t delta_bytes,
14853 int dy, int dvpos)
14855 struct glyph *glyph = row->glyphs[TEXT_AREA];
14856 struct glyph *end = glyph + row->used[TEXT_AREA];
14857 struct glyph *cursor = NULL;
14858 /* The last known character position in row. */
14859 ptrdiff_t last_pos = MATRIX_ROW_START_CHARPOS (row) + delta;
14860 int x = row->x;
14861 ptrdiff_t pt_old = PT - delta;
14862 ptrdiff_t pos_before = MATRIX_ROW_START_CHARPOS (row) + delta;
14863 ptrdiff_t pos_after = MATRIX_ROW_END_CHARPOS (row) + delta;
14864 struct glyph *glyph_before = glyph - 1, *glyph_after = end;
14865 /* A glyph beyond the edge of TEXT_AREA which we should never
14866 touch. */
14867 struct glyph *glyphs_end = end;
14868 /* True means we've found a match for cursor position, but that
14869 glyph has the avoid_cursor_p flag set. */
14870 bool match_with_avoid_cursor = false;
14871 /* True means we've seen at least one glyph that came from a
14872 display string. */
14873 bool string_seen = false;
14874 /* Largest and smallest buffer positions seen so far during scan of
14875 glyph row. */
14876 ptrdiff_t bpos_max = pos_before;
14877 ptrdiff_t bpos_min = pos_after;
14878 /* Last buffer position covered by an overlay string with an integer
14879 `cursor' property. */
14880 ptrdiff_t bpos_covered = 0;
14881 /* True means the display string on which to display the cursor
14882 comes from a text property, not from an overlay. */
14883 bool string_from_text_prop = false;
14885 /* Don't even try doing anything if called for a mode-line or
14886 header-line row, since the rest of the code isn't prepared to
14887 deal with such calamities. */
14888 eassert (!row->mode_line_p);
14889 if (row->mode_line_p)
14890 return false;
14892 /* Skip over glyphs not having an object at the start and the end of
14893 the row. These are special glyphs like truncation marks on
14894 terminal frames. */
14895 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
14897 if (!row->reversed_p)
14899 while (glyph < end
14900 && NILP (glyph->object)
14901 && glyph->charpos < 0)
14903 x += glyph->pixel_width;
14904 ++glyph;
14906 while (end > glyph
14907 && NILP ((end - 1)->object)
14908 /* CHARPOS is zero for blanks and stretch glyphs
14909 inserted by extend_face_to_end_of_line. */
14910 && (end - 1)->charpos <= 0)
14911 --end;
14912 glyph_before = glyph - 1;
14913 glyph_after = end;
14915 else
14917 struct glyph *g;
14919 /* If the glyph row is reversed, we need to process it from back
14920 to front, so swap the edge pointers. */
14921 glyphs_end = end = glyph - 1;
14922 glyph += row->used[TEXT_AREA] - 1;
14924 while (glyph > end + 1
14925 && NILP (glyph->object)
14926 && glyph->charpos < 0)
14927 --glyph;
14928 if (NILP (glyph->object) && glyph->charpos < 0)
14929 --glyph;
14930 /* By default, in reversed rows we put the cursor on the
14931 rightmost (first in the reading order) glyph. */
14932 for (x = 0, g = end + 1; g < glyph; g++)
14933 x += g->pixel_width;
14934 while (end < glyph
14935 && NILP ((end + 1)->object)
14936 && (end + 1)->charpos <= 0)
14937 ++end;
14938 glyph_before = glyph + 1;
14939 glyph_after = end;
14942 else if (row->reversed_p)
14944 /* In R2L rows that don't display text, put the cursor on the
14945 rightmost glyph. Case in point: an empty last line that is
14946 part of an R2L paragraph. */
14947 cursor = end - 1;
14948 /* Avoid placing the cursor on the last glyph of the row, where
14949 on terminal frames we hold the vertical border between
14950 adjacent windows. */
14951 if (!FRAME_WINDOW_P (WINDOW_XFRAME (w))
14952 && !WINDOW_RIGHTMOST_P (w)
14953 && cursor == row->glyphs[LAST_AREA] - 1)
14954 cursor--;
14955 x = -1; /* will be computed below, at label compute_x */
14958 /* Step 1: Try to find the glyph whose character position
14959 corresponds to point. If that's not possible, find 2 glyphs
14960 whose character positions are the closest to point, one before
14961 point, the other after it. */
14962 if (!row->reversed_p)
14963 while (/* not marched to end of glyph row */
14964 glyph < end
14965 /* glyph was not inserted by redisplay for internal purposes */
14966 && !NILP (glyph->object))
14968 if (BUFFERP (glyph->object))
14970 ptrdiff_t dpos = glyph->charpos - pt_old;
14972 if (glyph->charpos > bpos_max)
14973 bpos_max = glyph->charpos;
14974 if (glyph->charpos < bpos_min)
14975 bpos_min = glyph->charpos;
14976 if (!glyph->avoid_cursor_p)
14978 /* If we hit point, we've found the glyph on which to
14979 display the cursor. */
14980 if (dpos == 0)
14982 match_with_avoid_cursor = false;
14983 break;
14985 /* See if we've found a better approximation to
14986 POS_BEFORE or to POS_AFTER. */
14987 if (0 > dpos && dpos > pos_before - pt_old)
14989 pos_before = glyph->charpos;
14990 glyph_before = glyph;
14992 else if (0 < dpos && dpos < pos_after - pt_old)
14994 pos_after = glyph->charpos;
14995 glyph_after = glyph;
14998 else if (dpos == 0)
14999 match_with_avoid_cursor = true;
15001 else if (STRINGP (glyph->object))
15003 Lisp_Object chprop;
15004 ptrdiff_t glyph_pos = glyph->charpos;
15006 chprop = Fget_char_property (make_number (glyph_pos), Qcursor,
15007 glyph->object);
15008 if (!NILP (chprop))
15010 /* If the string came from a `display' text property,
15011 look up the buffer position of that property and
15012 use that position to update bpos_max, as if we
15013 actually saw such a position in one of the row's
15014 glyphs. This helps with supporting integer values
15015 of `cursor' property on the display string in
15016 situations where most or all of the row's buffer
15017 text is completely covered by display properties,
15018 so that no glyph with valid buffer positions is
15019 ever seen in the row. */
15020 ptrdiff_t prop_pos =
15021 string_buffer_position_lim (glyph->object, pos_before,
15022 pos_after, false);
15024 if (prop_pos >= pos_before)
15025 bpos_max = prop_pos;
15027 if (INTEGERP (chprop))
15029 bpos_covered = bpos_max + XINT (chprop);
15030 /* If the `cursor' property covers buffer positions up
15031 to and including point, we should display cursor on
15032 this glyph. Note that, if a `cursor' property on one
15033 of the string's characters has an integer value, we
15034 will break out of the loop below _before_ we get to
15035 the position match above. IOW, integer values of
15036 the `cursor' property override the "exact match for
15037 point" strategy of positioning the cursor. */
15038 /* Implementation note: bpos_max == pt_old when, e.g.,
15039 we are in an empty line, where bpos_max is set to
15040 MATRIX_ROW_START_CHARPOS, see above. */
15041 if (bpos_max <= pt_old && bpos_covered >= pt_old)
15043 cursor = glyph;
15044 break;
15048 string_seen = true;
15050 x += glyph->pixel_width;
15051 ++glyph;
15053 else if (glyph > end) /* row is reversed */
15054 while (!NILP (glyph->object))
15056 if (BUFFERP (glyph->object))
15058 ptrdiff_t dpos = glyph->charpos - pt_old;
15060 if (glyph->charpos > bpos_max)
15061 bpos_max = glyph->charpos;
15062 if (glyph->charpos < bpos_min)
15063 bpos_min = glyph->charpos;
15064 if (!glyph->avoid_cursor_p)
15066 if (dpos == 0)
15068 match_with_avoid_cursor = false;
15069 break;
15071 if (0 > dpos && dpos > pos_before - pt_old)
15073 pos_before = glyph->charpos;
15074 glyph_before = glyph;
15076 else if (0 < dpos && dpos < pos_after - pt_old)
15078 pos_after = glyph->charpos;
15079 glyph_after = glyph;
15082 else if (dpos == 0)
15083 match_with_avoid_cursor = true;
15085 else if (STRINGP (glyph->object))
15087 Lisp_Object chprop;
15088 ptrdiff_t glyph_pos = glyph->charpos;
15090 chprop = Fget_char_property (make_number (glyph_pos), Qcursor,
15091 glyph->object);
15092 if (!NILP (chprop))
15094 ptrdiff_t prop_pos =
15095 string_buffer_position_lim (glyph->object, pos_before,
15096 pos_after, false);
15098 if (prop_pos >= pos_before)
15099 bpos_max = prop_pos;
15101 if (INTEGERP (chprop))
15103 bpos_covered = bpos_max + XINT (chprop);
15104 /* If the `cursor' property covers buffer positions up
15105 to and including point, we should display cursor on
15106 this glyph. */
15107 if (bpos_max <= pt_old && bpos_covered >= pt_old)
15109 cursor = glyph;
15110 break;
15113 string_seen = true;
15115 --glyph;
15116 if (glyph == glyphs_end) /* don't dereference outside TEXT_AREA */
15118 x--; /* can't use any pixel_width */
15119 break;
15121 x -= glyph->pixel_width;
15124 /* Step 2: If we didn't find an exact match for point, we need to
15125 look for a proper place to put the cursor among glyphs between
15126 GLYPH_BEFORE and GLYPH_AFTER. */
15127 if (!((row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end)
15128 && BUFFERP (glyph->object) && glyph->charpos == pt_old)
15129 && !(bpos_max <= pt_old && pt_old <= bpos_covered))
15131 /* An empty line has a single glyph whose OBJECT is nil and
15132 whose CHARPOS is the position of a newline on that line.
15133 Note that on a TTY, there are more glyphs after that, which
15134 were produced by extend_face_to_end_of_line, but their
15135 CHARPOS is zero or negative. */
15136 bool empty_line_p =
15137 ((row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end)
15138 && NILP (glyph->object) && glyph->charpos > 0
15139 /* On a TTY, continued and truncated rows also have a glyph at
15140 their end whose OBJECT is nil and whose CHARPOS is
15141 positive (the continuation and truncation glyphs), but such
15142 rows are obviously not "empty". */
15143 && !(row->continued_p || row->truncated_on_right_p));
15145 if (row->ends_in_ellipsis_p && pos_after == last_pos)
15147 ptrdiff_t ellipsis_pos;
15149 /* Scan back over the ellipsis glyphs. */
15150 if (!row->reversed_p)
15152 ellipsis_pos = (glyph - 1)->charpos;
15153 while (glyph > row->glyphs[TEXT_AREA]
15154 && (glyph - 1)->charpos == ellipsis_pos)
15155 glyph--, x -= glyph->pixel_width;
15156 /* That loop always goes one position too far, including
15157 the glyph before the ellipsis. So scan forward over
15158 that one. */
15159 x += glyph->pixel_width;
15160 glyph++;
15162 else /* row is reversed */
15164 ellipsis_pos = (glyph + 1)->charpos;
15165 while (glyph < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
15166 && (glyph + 1)->charpos == ellipsis_pos)
15167 glyph++, x += glyph->pixel_width;
15168 x -= glyph->pixel_width;
15169 glyph--;
15172 else if (match_with_avoid_cursor)
15174 cursor = glyph_after;
15175 x = -1;
15177 else if (string_seen)
15179 int incr = row->reversed_p ? -1 : +1;
15181 /* Need to find the glyph that came out of a string which is
15182 present at point. That glyph is somewhere between
15183 GLYPH_BEFORE and GLYPH_AFTER, and it came from a string
15184 positioned between POS_BEFORE and POS_AFTER in the
15185 buffer. */
15186 struct glyph *start, *stop;
15187 ptrdiff_t pos = pos_before;
15189 x = -1;
15191 /* If the row ends in a newline from a display string,
15192 reordering could have moved the glyphs belonging to the
15193 string out of the [GLYPH_BEFORE..GLYPH_AFTER] range. So
15194 in this case we extend the search to the last glyph in
15195 the row that was not inserted by redisplay. */
15196 if (row->ends_in_newline_from_string_p)
15198 glyph_after = end;
15199 pos_after = MATRIX_ROW_END_CHARPOS (row) + delta;
15202 /* GLYPH_BEFORE and GLYPH_AFTER are the glyphs that
15203 correspond to POS_BEFORE and POS_AFTER, respectively. We
15204 need START and STOP in the order that corresponds to the
15205 row's direction as given by its reversed_p flag. If the
15206 directionality of characters between POS_BEFORE and
15207 POS_AFTER is the opposite of the row's base direction,
15208 these characters will have been reordered for display,
15209 and we need to reverse START and STOP. */
15210 if (!row->reversed_p)
15212 start = min (glyph_before, glyph_after);
15213 stop = max (glyph_before, glyph_after);
15215 else
15217 start = max (glyph_before, glyph_after);
15218 stop = min (glyph_before, glyph_after);
15220 for (glyph = start + incr;
15221 row->reversed_p ? glyph > stop : glyph < stop; )
15224 /* Any glyphs that come from the buffer are here because
15225 of bidi reordering. Skip them, and only pay
15226 attention to glyphs that came from some string. */
15227 if (STRINGP (glyph->object))
15229 Lisp_Object str;
15230 ptrdiff_t tem;
15231 /* If the display property covers the newline, we
15232 need to search for it one position farther. */
15233 ptrdiff_t lim = pos_after
15234 + (pos_after == MATRIX_ROW_END_CHARPOS (row) + delta);
15236 string_from_text_prop = false;
15237 str = glyph->object;
15238 tem = string_buffer_position_lim (str, pos, lim, false);
15239 if (tem == 0 /* from overlay */
15240 || pos <= tem)
15242 /* If the string from which this glyph came is
15243 found in the buffer at point, or at position
15244 that is closer to point than pos_after, then
15245 we've found the glyph we've been looking for.
15246 If it comes from an overlay (tem == 0), and
15247 it has the `cursor' property on one of its
15248 glyphs, record that glyph as a candidate for
15249 displaying the cursor. (As in the
15250 unidirectional version, we will display the
15251 cursor on the last candidate we find.) */
15252 if (tem == 0
15253 || tem == pt_old
15254 || (tem - pt_old > 0 && tem < pos_after))
15256 /* The glyphs from this string could have
15257 been reordered. Find the one with the
15258 smallest string position. Or there could
15259 be a character in the string with the
15260 `cursor' property, which means display
15261 cursor on that character's glyph. */
15262 ptrdiff_t strpos = glyph->charpos;
15264 if (tem)
15266 cursor = glyph;
15267 string_from_text_prop = true;
15269 for ( ;
15270 (row->reversed_p ? glyph > stop : glyph < stop)
15271 && EQ (glyph->object, str);
15272 glyph += incr)
15274 Lisp_Object cprop;
15275 ptrdiff_t gpos = glyph->charpos;
15277 cprop = Fget_char_property (make_number (gpos),
15278 Qcursor,
15279 glyph->object);
15280 if (!NILP (cprop))
15282 cursor = glyph;
15283 break;
15285 if (tem && glyph->charpos < strpos)
15287 strpos = glyph->charpos;
15288 cursor = glyph;
15292 if (tem == pt_old
15293 || (tem - pt_old > 0 && tem < pos_after))
15294 goto compute_x;
15296 if (tem)
15297 pos = tem + 1; /* don't find previous instances */
15299 /* This string is not what we want; skip all of the
15300 glyphs that came from it. */
15301 while ((row->reversed_p ? glyph > stop : glyph < stop)
15302 && EQ (glyph->object, str))
15303 glyph += incr;
15305 else
15306 glyph += incr;
15309 /* If we reached the end of the line, and END was from a string,
15310 the cursor is not on this line. */
15311 if (cursor == NULL
15312 && (row->reversed_p ? glyph <= end : glyph >= end)
15313 && (row->reversed_p ? end > glyphs_end : end < glyphs_end)
15314 && STRINGP (end->object)
15315 && row->continued_p)
15316 return false;
15318 /* A truncated row may not include PT among its character positions.
15319 Setting the cursor inside the scroll margin will trigger
15320 recalculation of hscroll in hscroll_window_tree. But if a
15321 display string covers point, defer to the string-handling
15322 code below to figure this out. */
15323 else if (row->truncated_on_left_p && pt_old < bpos_min)
15325 cursor = glyph_before;
15326 x = -1;
15328 else if ((row->truncated_on_right_p && pt_old > bpos_max)
15329 /* Zero-width characters produce no glyphs. */
15330 || (!empty_line_p
15331 && (row->reversed_p
15332 ? glyph_after > glyphs_end
15333 : glyph_after < glyphs_end)))
15335 cursor = glyph_after;
15336 x = -1;
15340 compute_x:
15341 if (cursor != NULL)
15342 glyph = cursor;
15343 else if (glyph == glyphs_end
15344 && pos_before == pos_after
15345 && STRINGP ((row->reversed_p
15346 ? row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
15347 : row->glyphs[TEXT_AREA])->object))
15349 /* If all the glyphs of this row came from strings, put the
15350 cursor on the first glyph of the row. This avoids having the
15351 cursor outside of the text area in this very rare and hard
15352 use case. */
15353 glyph =
15354 row->reversed_p
15355 ? row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
15356 : row->glyphs[TEXT_AREA];
15358 if (x < 0)
15360 struct glyph *g;
15362 /* Need to compute x that corresponds to GLYPH. */
15363 for (g = row->glyphs[TEXT_AREA], x = row->x; g < glyph; g++)
15365 if (g >= row->glyphs[TEXT_AREA] + row->used[TEXT_AREA])
15366 emacs_abort ();
15367 x += g->pixel_width;
15371 /* ROW could be part of a continued line, which, under bidi
15372 reordering, might have other rows whose start and end charpos
15373 occlude point. Only set w->cursor if we found a better
15374 approximation to the cursor position than we have from previously
15375 examined candidate rows belonging to the same continued line. */
15376 if (/* We already have a candidate row. */
15377 w->cursor.vpos >= 0
15378 /* That candidate is not the row we are processing. */
15379 && MATRIX_ROW (matrix, w->cursor.vpos) != row
15380 /* Make sure cursor.vpos specifies a row whose start and end
15381 charpos occlude point, and it is valid candidate for being a
15382 cursor-row. This is because some callers of this function
15383 leave cursor.vpos at the row where the cursor was displayed
15384 during the last redisplay cycle. */
15385 && MATRIX_ROW_START_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos)) <= pt_old
15386 && pt_old <= MATRIX_ROW_END_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
15387 && cursor_row_p (MATRIX_ROW (matrix, w->cursor.vpos)))
15389 struct glyph *g1
15390 = MATRIX_ROW_GLYPH_START (matrix, w->cursor.vpos) + w->cursor.hpos;
15392 /* Don't consider glyphs that are outside TEXT_AREA. */
15393 if (!(row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end))
15394 return false;
15395 /* Keep the candidate whose buffer position is the closest to
15396 point or has the `cursor' property. */
15397 if (/* Previous candidate is a glyph in TEXT_AREA of that row. */
15398 w->cursor.hpos >= 0
15399 && w->cursor.hpos < MATRIX_ROW_USED (matrix, w->cursor.vpos)
15400 && ((BUFFERP (g1->object)
15401 && (g1->charpos == pt_old /* An exact match always wins. */
15402 || (BUFFERP (glyph->object)
15403 && eabs (g1->charpos - pt_old)
15404 < eabs (glyph->charpos - pt_old))))
15405 /* Previous candidate is a glyph from a string that has
15406 a non-nil `cursor' property. */
15407 || (STRINGP (g1->object)
15408 && (!NILP (Fget_char_property (make_number (g1->charpos),
15409 Qcursor, g1->object))
15410 /* Previous candidate is from the same display
15411 string as this one, and the display string
15412 came from a text property. */
15413 || (EQ (g1->object, glyph->object)
15414 && string_from_text_prop)
15415 /* this candidate is from newline and its
15416 position is not an exact match */
15417 || (NILP (glyph->object)
15418 && glyph->charpos != pt_old)))))
15419 return false;
15420 /* If this candidate gives an exact match, use that. */
15421 if (!((BUFFERP (glyph->object) && glyph->charpos == pt_old)
15422 /* If this candidate is a glyph created for the
15423 terminating newline of a line, and point is on that
15424 newline, it wins because it's an exact match. */
15425 || (!row->continued_p
15426 && NILP (glyph->object)
15427 && glyph->charpos == 0
15428 && pt_old == MATRIX_ROW_END_CHARPOS (row) - 1))
15429 /* Otherwise, keep the candidate that comes from a row
15430 spanning less buffer positions. This may win when one or
15431 both candidate positions are on glyphs that came from
15432 display strings, for which we cannot compare buffer
15433 positions. */
15434 && MATRIX_ROW_END_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
15435 - MATRIX_ROW_START_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
15436 < MATRIX_ROW_END_CHARPOS (row) - MATRIX_ROW_START_CHARPOS (row))
15437 return false;
15439 w->cursor.hpos = glyph - row->glyphs[TEXT_AREA];
15440 w->cursor.x = x;
15441 w->cursor.vpos = MATRIX_ROW_VPOS (row, matrix) + dvpos;
15442 w->cursor.y = row->y + dy;
15444 if (w == XWINDOW (selected_window))
15446 if (!row->continued_p
15447 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
15448 && row->x == 0)
15450 this_line_buffer = XBUFFER (w->contents);
15452 CHARPOS (this_line_start_pos)
15453 = MATRIX_ROW_START_CHARPOS (row) + delta;
15454 BYTEPOS (this_line_start_pos)
15455 = MATRIX_ROW_START_BYTEPOS (row) + delta_bytes;
15457 CHARPOS (this_line_end_pos)
15458 = Z - (MATRIX_ROW_END_CHARPOS (row) + delta);
15459 BYTEPOS (this_line_end_pos)
15460 = Z_BYTE - (MATRIX_ROW_END_BYTEPOS (row) + delta_bytes);
15462 this_line_y = w->cursor.y;
15463 this_line_pixel_height = row->height;
15464 this_line_vpos = w->cursor.vpos;
15465 this_line_start_x = row->x;
15467 else
15468 CHARPOS (this_line_start_pos) = 0;
15471 return true;
15475 /* Run window scroll functions, if any, for WINDOW with new window
15476 start STARTP. Sets the window start of WINDOW to that position.
15478 We assume that the window's buffer is really current. */
15480 static struct text_pos
15481 run_window_scroll_functions (Lisp_Object window, struct text_pos startp)
15483 struct window *w = XWINDOW (window);
15484 SET_MARKER_FROM_TEXT_POS (w->start, startp);
15486 eassert (current_buffer == XBUFFER (w->contents));
15488 if (!NILP (Vwindow_scroll_functions))
15490 run_hook_with_args_2 (Qwindow_scroll_functions, window,
15491 make_number (CHARPOS (startp)));
15492 SET_TEXT_POS_FROM_MARKER (startp, w->start);
15493 /* In case the hook functions switch buffers. */
15494 set_buffer_internal (XBUFFER (w->contents));
15497 return startp;
15501 /* Make sure the line containing the cursor is fully visible.
15502 A value of true means there is nothing to be done.
15503 (Either the line is fully visible, or it cannot be made so,
15504 or we cannot tell.)
15506 If FORCE_P, return false even if partial visible cursor row
15507 is higher than window.
15509 If CURRENT_MATRIX_P, use the information from the
15510 window's current glyph matrix; otherwise use the desired glyph
15511 matrix.
15513 A value of false means the caller should do scrolling
15514 as if point had gone off the screen. */
15516 static bool
15517 cursor_row_fully_visible_p (struct window *w, bool force_p,
15518 bool current_matrix_p)
15520 struct glyph_matrix *matrix;
15521 struct glyph_row *row;
15522 int window_height;
15524 if (!make_cursor_line_fully_visible_p)
15525 return true;
15527 /* It's not always possible to find the cursor, e.g, when a window
15528 is full of overlay strings. Don't do anything in that case. */
15529 if (w->cursor.vpos < 0)
15530 return true;
15532 matrix = current_matrix_p ? w->current_matrix : w->desired_matrix;
15533 row = MATRIX_ROW (matrix, w->cursor.vpos);
15535 /* If the cursor row is not partially visible, there's nothing to do. */
15536 if (!MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row))
15537 return true;
15539 /* If the row the cursor is in is taller than the window's height,
15540 it's not clear what to do, so do nothing. */
15541 window_height = window_box_height (w);
15542 if (row->height >= window_height)
15544 if (!force_p || MINI_WINDOW_P (w)
15545 || w->vscroll || w->cursor.vpos == 0)
15546 return true;
15548 return false;
15552 /* Try scrolling PT into view in window WINDOW. JUST_THIS_ONE_P
15553 means only WINDOW is redisplayed in redisplay_internal.
15554 TEMP_SCROLL_STEP has the same meaning as emacs_scroll_step, and is used
15555 in redisplay_window to bring a partially visible line into view in
15556 the case that only the cursor has moved.
15558 LAST_LINE_MISFIT should be true if we're scrolling because the
15559 last screen line's vertical height extends past the end of the screen.
15561 Value is
15563 1 if scrolling succeeded
15565 0 if scrolling didn't find point.
15567 -1 if new fonts have been loaded so that we must interrupt
15568 redisplay, adjust glyph matrices, and try again. */
15570 enum
15572 SCROLLING_SUCCESS,
15573 SCROLLING_FAILED,
15574 SCROLLING_NEED_LARGER_MATRICES
15577 /* If scroll-conservatively is more than this, never recenter.
15579 If you change this, don't forget to update the doc string of
15580 `scroll-conservatively' and the Emacs manual. */
15581 #define SCROLL_LIMIT 100
15583 static int
15584 try_scrolling (Lisp_Object window, bool just_this_one_p,
15585 ptrdiff_t arg_scroll_conservatively, ptrdiff_t scroll_step,
15586 bool temp_scroll_step, bool last_line_misfit)
15588 struct window *w = XWINDOW (window);
15589 struct text_pos pos, startp;
15590 struct it it;
15591 int this_scroll_margin, scroll_max, rc, height;
15592 int dy = 0, amount_to_scroll = 0;
15593 bool scroll_down_p = false;
15594 int extra_scroll_margin_lines = last_line_misfit;
15595 Lisp_Object aggressive;
15596 /* We will never try scrolling more than this number of lines. */
15597 int scroll_limit = SCROLL_LIMIT;
15598 int frame_line_height = default_line_pixel_height (w);
15600 #ifdef GLYPH_DEBUG
15601 debug_method_add (w, "try_scrolling");
15602 #endif
15604 SET_TEXT_POS_FROM_MARKER (startp, w->start);
15606 this_scroll_margin = window_scroll_margin (w, MARGIN_IN_PIXELS);
15608 /* Force arg_scroll_conservatively to have a reasonable value, to
15609 avoid scrolling too far away with slow move_it_* functions. Note
15610 that the user can supply scroll-conservatively equal to
15611 `most-positive-fixnum', which can be larger than INT_MAX. */
15612 if (arg_scroll_conservatively > scroll_limit)
15614 arg_scroll_conservatively = scroll_limit + 1;
15615 scroll_max = scroll_limit * frame_line_height;
15617 else if (scroll_step || arg_scroll_conservatively || temp_scroll_step)
15618 /* Compute how much we should try to scroll maximally to bring
15619 point into view. */
15620 scroll_max = (max (scroll_step,
15621 max (arg_scroll_conservatively, temp_scroll_step))
15622 * frame_line_height);
15623 else if (NUMBERP (BVAR (current_buffer, scroll_down_aggressively))
15624 || NUMBERP (BVAR (current_buffer, scroll_up_aggressively)))
15625 /* We're trying to scroll because of aggressive scrolling but no
15626 scroll_step is set. Choose an arbitrary one. */
15627 scroll_max = 10 * frame_line_height;
15628 else
15629 scroll_max = 0;
15631 too_near_end:
15633 /* Decide whether to scroll down. */
15634 if (PT > CHARPOS (startp))
15636 int scroll_margin_y;
15638 /* Compute the pixel ypos of the scroll margin, then move IT to
15639 either that ypos or PT, whichever comes first. */
15640 start_display (&it, w, startp);
15641 scroll_margin_y = it.last_visible_y - partial_line_height (&it)
15642 - this_scroll_margin
15643 - frame_line_height * extra_scroll_margin_lines;
15644 move_it_to (&it, PT, -1, scroll_margin_y - 1, -1,
15645 (MOVE_TO_POS | MOVE_TO_Y));
15647 if (PT > CHARPOS (it.current.pos))
15649 int y0 = line_bottom_y (&it);
15650 /* Compute how many pixels below window bottom to stop searching
15651 for PT. This avoids costly search for PT that is far away if
15652 the user limited scrolling by a small number of lines, but
15653 always finds PT if scroll_conservatively is set to a large
15654 number, such as most-positive-fixnum. */
15655 int slack = max (scroll_max, 10 * frame_line_height);
15656 int y_to_move = it.last_visible_y + slack;
15658 /* Compute the distance from the scroll margin to PT or to
15659 the scroll limit, whichever comes first. This should
15660 include the height of the cursor line, to make that line
15661 fully visible. */
15662 move_it_to (&it, PT, -1, y_to_move,
15663 -1, MOVE_TO_POS | MOVE_TO_Y);
15664 dy = line_bottom_y (&it) - y0;
15666 if (dy > scroll_max)
15667 return SCROLLING_FAILED;
15669 if (dy > 0)
15670 scroll_down_p = true;
15672 else if (PT == IT_CHARPOS (it)
15673 && IT_CHARPOS (it) < ZV
15674 && it.method == GET_FROM_STRING
15675 && arg_scroll_conservatively > scroll_limit
15676 && it.current_x == 0)
15678 enum move_it_result skip;
15679 int y1 = it.current_y;
15680 int vpos;
15682 /* A before-string that includes newlines and is displayed
15683 on the last visible screen line could fail us under
15684 scroll-conservatively > 100, because we will be unable to
15685 position the cursor on that last visible line. Try to
15686 recover by finding the first screen line that has some
15687 glyphs coming from the buffer text. */
15688 do {
15689 skip = move_it_in_display_line_to (&it, ZV, -1, MOVE_TO_POS);
15690 if (skip != MOVE_NEWLINE_OR_CR
15691 || IT_CHARPOS (it) != PT
15692 || it.method == GET_FROM_BUFFER)
15693 break;
15694 vpos = it.vpos;
15695 move_it_to (&it, -1, -1, -1, vpos + 1, MOVE_TO_VPOS);
15696 } while (it.vpos > vpos);
15698 dy = it.current_y - y1;
15700 if (dy > scroll_max)
15701 return SCROLLING_FAILED;
15703 if (dy > 0)
15704 scroll_down_p = true;
15708 if (scroll_down_p)
15710 /* Point is in or below the bottom scroll margin, so move the
15711 window start down. If scrolling conservatively, move it just
15712 enough down to make point visible. If scroll_step is set,
15713 move it down by scroll_step. */
15714 if (arg_scroll_conservatively)
15715 amount_to_scroll
15716 = min (max (dy, frame_line_height),
15717 frame_line_height * arg_scroll_conservatively);
15718 else if (scroll_step || temp_scroll_step)
15719 amount_to_scroll = scroll_max;
15720 else
15722 aggressive = BVAR (current_buffer, scroll_up_aggressively);
15723 height = WINDOW_BOX_TEXT_HEIGHT (w);
15724 if (NUMBERP (aggressive))
15726 double float_amount = XFLOATINT (aggressive) * height;
15727 int aggressive_scroll = float_amount;
15728 if (aggressive_scroll == 0 && float_amount > 0)
15729 aggressive_scroll = 1;
15730 /* Don't let point enter the scroll margin near top of
15731 the window. This could happen if the value of
15732 scroll_up_aggressively is too large and there are
15733 non-zero margins, because scroll_up_aggressively
15734 means put point that fraction of window height
15735 _from_the_bottom_margin_. */
15736 if (aggressive_scroll + 2 * this_scroll_margin > height)
15737 aggressive_scroll = height - 2 * this_scroll_margin;
15738 amount_to_scroll = dy + aggressive_scroll;
15742 if (amount_to_scroll <= 0)
15743 return SCROLLING_FAILED;
15745 start_display (&it, w, startp);
15746 if (arg_scroll_conservatively <= scroll_limit)
15747 move_it_vertically (&it, amount_to_scroll);
15748 else
15750 /* Extra precision for users who set scroll-conservatively
15751 to a large number: make sure the amount we scroll
15752 the window start is never less than amount_to_scroll,
15753 which was computed as distance from window bottom to
15754 point. This matters when lines at window top and lines
15755 below window bottom have different height. */
15756 struct it it1;
15757 void *it1data = NULL;
15758 /* We use a temporary it1 because line_bottom_y can modify
15759 its argument, if it moves one line down; see there. */
15760 int start_y;
15762 SAVE_IT (it1, it, it1data);
15763 start_y = line_bottom_y (&it1);
15764 do {
15765 RESTORE_IT (&it, &it, it1data);
15766 move_it_by_lines (&it, 1);
15767 SAVE_IT (it1, it, it1data);
15768 } while (IT_CHARPOS (it) < ZV
15769 && line_bottom_y (&it1) - start_y < amount_to_scroll);
15770 bidi_unshelve_cache (it1data, true);
15773 /* If STARTP is unchanged, move it down another screen line. */
15774 if (IT_CHARPOS (it) == CHARPOS (startp))
15775 move_it_by_lines (&it, 1);
15776 startp = it.current.pos;
15778 else
15780 struct text_pos scroll_margin_pos = startp;
15781 int y_offset = 0;
15783 /* See if point is inside the scroll margin at the top of the
15784 window. */
15785 if (this_scroll_margin)
15787 int y_start;
15789 start_display (&it, w, startp);
15790 y_start = it.current_y;
15791 move_it_vertically (&it, this_scroll_margin);
15792 scroll_margin_pos = it.current.pos;
15793 /* If we didn't move enough before hitting ZV, request
15794 additional amount of scroll, to move point out of the
15795 scroll margin. */
15796 if (IT_CHARPOS (it) == ZV
15797 && it.current_y - y_start < this_scroll_margin)
15798 y_offset = this_scroll_margin - (it.current_y - y_start);
15801 if (PT < CHARPOS (scroll_margin_pos))
15803 /* Point is in the scroll margin at the top of the window or
15804 above what is displayed in the window. */
15805 int y0, y_to_move;
15807 /* Compute the vertical distance from PT to the scroll
15808 margin position. Move as far as scroll_max allows, or
15809 one screenful, or 10 screen lines, whichever is largest.
15810 Give up if distance is greater than scroll_max or if we
15811 didn't reach the scroll margin position. */
15812 SET_TEXT_POS (pos, PT, PT_BYTE);
15813 start_display (&it, w, pos);
15814 y0 = it.current_y;
15815 y_to_move = max (it.last_visible_y,
15816 max (scroll_max, 10 * frame_line_height));
15817 move_it_to (&it, CHARPOS (scroll_margin_pos), 0,
15818 y_to_move, -1,
15819 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
15820 dy = it.current_y - y0;
15821 if (dy > scroll_max
15822 || IT_CHARPOS (it) < CHARPOS (scroll_margin_pos))
15823 return SCROLLING_FAILED;
15825 /* Additional scroll for when ZV was too close to point. */
15826 dy += y_offset;
15828 /* Compute new window start. */
15829 start_display (&it, w, startp);
15831 if (arg_scroll_conservatively)
15832 amount_to_scroll = max (dy, frame_line_height
15833 * max (scroll_step, temp_scroll_step));
15834 else if (scroll_step || temp_scroll_step)
15835 amount_to_scroll = scroll_max;
15836 else
15838 aggressive = BVAR (current_buffer, scroll_down_aggressively);
15839 height = WINDOW_BOX_TEXT_HEIGHT (w);
15840 if (NUMBERP (aggressive))
15842 double float_amount = XFLOATINT (aggressive) * height;
15843 int aggressive_scroll = float_amount;
15844 if (aggressive_scroll == 0 && float_amount > 0)
15845 aggressive_scroll = 1;
15846 /* Don't let point enter the scroll margin near
15847 bottom of the window, if the value of
15848 scroll_down_aggressively happens to be too
15849 large. */
15850 if (aggressive_scroll + 2 * this_scroll_margin > height)
15851 aggressive_scroll = height - 2 * this_scroll_margin;
15852 amount_to_scroll = dy + aggressive_scroll;
15856 if (amount_to_scroll <= 0)
15857 return SCROLLING_FAILED;
15859 move_it_vertically_backward (&it, amount_to_scroll);
15860 startp = it.current.pos;
15864 /* Run window scroll functions. */
15865 startp = run_window_scroll_functions (window, startp);
15867 /* Display the window. Give up if new fonts are loaded, or if point
15868 doesn't appear. */
15869 if (!try_window (window, startp, 0))
15870 rc = SCROLLING_NEED_LARGER_MATRICES;
15871 else if (w->cursor.vpos < 0)
15873 clear_glyph_matrix (w->desired_matrix);
15874 rc = SCROLLING_FAILED;
15876 else
15878 /* Maybe forget recorded base line for line number display. */
15879 if (!just_this_one_p
15880 || current_buffer->clip_changed
15881 || BEG_UNCHANGED < CHARPOS (startp))
15882 w->base_line_number = 0;
15884 /* If cursor ends up on a partially visible line,
15885 treat that as being off the bottom of the screen. */
15886 if (! cursor_row_fully_visible_p (w, extra_scroll_margin_lines <= 1,
15887 false)
15888 /* It's possible that the cursor is on the first line of the
15889 buffer, which is partially obscured due to a vscroll
15890 (Bug#7537). In that case, avoid looping forever. */
15891 && extra_scroll_margin_lines < w->desired_matrix->nrows - 1)
15893 clear_glyph_matrix (w->desired_matrix);
15894 ++extra_scroll_margin_lines;
15895 goto too_near_end;
15897 rc = SCROLLING_SUCCESS;
15900 return rc;
15904 /* Compute a suitable window start for window W if display of W starts
15905 on a continuation line. Value is true if a new window start
15906 was computed.
15908 The new window start will be computed, based on W's width, starting
15909 from the start of the continued line. It is the start of the
15910 screen line with the minimum distance from the old start W->start,
15911 which is still before point (otherwise point will definitely not
15912 be visible in the window). */
15914 static bool
15915 compute_window_start_on_continuation_line (struct window *w)
15917 struct text_pos pos, start_pos, pos_before_pt;
15918 bool window_start_changed_p = false;
15920 SET_TEXT_POS_FROM_MARKER (start_pos, w->start);
15922 /* If window start is on a continuation line... Window start may be
15923 < BEGV in case there's invisible text at the start of the
15924 buffer (M-x rmail, for example). */
15925 if (CHARPOS (start_pos) > BEGV
15926 && FETCH_BYTE (BYTEPOS (start_pos) - 1) != '\n')
15928 struct it it;
15929 struct glyph_row *row;
15931 /* Handle the case that the window start is out of range. */
15932 if (CHARPOS (start_pos) < BEGV)
15933 SET_TEXT_POS (start_pos, BEGV, BEGV_BYTE);
15934 else if (CHARPOS (start_pos) > ZV)
15935 SET_TEXT_POS (start_pos, ZV, ZV_BYTE);
15937 /* Find the start of the continued line. This should be fast
15938 because find_newline is fast (newline cache). */
15939 row = w->desired_matrix->rows + window_wants_header_line (w);
15940 init_iterator (&it, w, CHARPOS (start_pos), BYTEPOS (start_pos),
15941 row, DEFAULT_FACE_ID);
15942 reseat_at_previous_visible_line_start (&it);
15944 /* If the line start is "too far" away from the window start,
15945 say it takes too much time to compute a new window start.
15946 Also, give up if the line start is after point, as in that
15947 case point will not be visible with any window start we
15948 compute. */
15949 if (IT_CHARPOS (it) <= PT
15950 || (CHARPOS (start_pos) - IT_CHARPOS (it)
15951 /* PXW: Do we need upper bounds here? */
15952 < WINDOW_TOTAL_LINES (w) * WINDOW_TOTAL_COLS (w)))
15954 int min_distance, distance;
15956 /* Move forward by display lines to find the new window
15957 start. If window width was enlarged, the new start can
15958 be expected to be > the old start. If window width was
15959 decreased, the new window start will be < the old start.
15960 So, we're looking for the display line start with the
15961 minimum distance from the old window start. */
15962 pos_before_pt = pos = it.current.pos;
15963 min_distance = DISP_INFINITY;
15964 while ((distance = eabs (CHARPOS (start_pos) - IT_CHARPOS (it))),
15965 distance < min_distance)
15967 min_distance = distance;
15968 if (CHARPOS (pos) <= PT)
15969 pos_before_pt = pos;
15970 pos = it.current.pos;
15971 if (it.line_wrap == WORD_WRAP)
15973 /* Under WORD_WRAP, move_it_by_lines is likely to
15974 overshoot and stop not at the first, but the
15975 second character from the left margin. So in
15976 that case, we need a more tight control on the X
15977 coordinate of the iterator than move_it_by_lines
15978 promises in its contract. The method is to first
15979 go to the last (rightmost) visible character of a
15980 line, then move to the leftmost character on the
15981 next line in a separate call. */
15982 move_it_to (&it, ZV, it.last_visible_x, it.current_y, -1,
15983 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
15984 move_it_to (&it, ZV, 0,
15985 it.current_y + it.max_ascent + it.max_descent, -1,
15986 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
15988 else
15989 move_it_by_lines (&it, 1);
15992 /* It makes very little sense to make the new window start
15993 after point, as point won't be visible. If that's what
15994 the loop above finds, fall back on the candidate before
15995 or at point that is closest to the old window start. */
15996 if (CHARPOS (pos) > PT)
15997 pos = pos_before_pt;
15999 /* Set the window start there. */
16000 SET_MARKER_FROM_TEXT_POS (w->start, pos);
16001 window_start_changed_p = true;
16005 return window_start_changed_p;
16009 /* Try cursor movement in case text has not changed in window WINDOW,
16010 with window start STARTP. Value is
16012 CURSOR_MOVEMENT_SUCCESS if successful
16014 CURSOR_MOVEMENT_CANNOT_BE_USED if this method cannot be used
16016 CURSOR_MOVEMENT_MUST_SCROLL if we know we have to scroll the
16017 display. *SCROLL_STEP is set to true, under certain circumstances, if
16018 we want to scroll as if scroll-step were set to 1. See the code.
16020 CURSOR_MOVEMENT_NEED_LARGER_MATRICES if we need larger matrices, in
16021 which case we have to abort this redisplay, and adjust matrices
16022 first. */
16024 enum
16026 CURSOR_MOVEMENT_SUCCESS,
16027 CURSOR_MOVEMENT_CANNOT_BE_USED,
16028 CURSOR_MOVEMENT_MUST_SCROLL,
16029 CURSOR_MOVEMENT_NEED_LARGER_MATRICES
16032 static int
16033 try_cursor_movement (Lisp_Object window, struct text_pos startp,
16034 bool *scroll_step)
16036 struct window *w = XWINDOW (window);
16037 struct frame *f = XFRAME (w->frame);
16038 int rc = CURSOR_MOVEMENT_CANNOT_BE_USED;
16040 #ifdef GLYPH_DEBUG
16041 if (inhibit_try_cursor_movement)
16042 return rc;
16043 #endif
16045 /* Previously, there was a check for Lisp integer in the
16046 if-statement below. Now, this field is converted to
16047 ptrdiff_t, thus zero means invalid position in a buffer. */
16048 eassert (w->last_point > 0);
16049 /* Likewise there was a check whether window_end_vpos is nil or larger
16050 than the window. Now window_end_vpos is int and so never nil, but
16051 let's leave eassert to check whether it fits in the window. */
16052 eassert (!w->window_end_valid
16053 || w->window_end_vpos < w->current_matrix->nrows);
16055 /* Handle case where text has not changed, only point, and it has
16056 not moved off the frame. */
16057 if (/* Point may be in this window. */
16058 PT >= CHARPOS (startp)
16059 /* Selective display hasn't changed. */
16060 && !current_buffer->clip_changed
16061 /* Function force-mode-line-update is used to force a thorough
16062 redisplay. It sets either windows_or_buffers_changed or
16063 update_mode_lines. So don't take a shortcut here for these
16064 cases. */
16065 && !update_mode_lines
16066 && !windows_or_buffers_changed
16067 && !f->cursor_type_changed
16068 && NILP (Vshow_trailing_whitespace)
16069 /* When display-line-numbers is in relative mode, moving point
16070 requires to redraw the entire window. */
16071 && !EQ (Vdisplay_line_numbers, Qrelative)
16072 && !EQ (Vdisplay_line_numbers, Qvisual)
16073 /* When the current line number should be displayed in a
16074 distinct face, moving point cannot be handled in optimized
16075 way as below. */
16076 && !(!NILP (Vdisplay_line_numbers)
16077 && NILP (Finternal_lisp_face_equal_p (Qline_number,
16078 Qline_number_current_line,
16079 w->frame)))
16080 /* This code is not used for mini-buffer for the sake of the case
16081 of redisplaying to replace an echo area message; since in
16082 that case the mini-buffer contents per se are usually
16083 unchanged. This code is of no real use in the mini-buffer
16084 since the handling of this_line_start_pos, etc., in redisplay
16085 handles the same cases. */
16086 && !EQ (window, minibuf_window)
16087 /* When overlay arrow is shown in current buffer, point movement
16088 is no longer "simple", as it typically causes the overlay
16089 arrow to move as well. */
16090 && !overlay_arrow_in_current_buffer_p ())
16092 int this_scroll_margin, top_scroll_margin;
16093 struct glyph_row *row = NULL;
16095 #ifdef GLYPH_DEBUG
16096 debug_method_add (w, "cursor movement");
16097 #endif
16099 this_scroll_margin = window_scroll_margin (w, MARGIN_IN_PIXELS);
16101 top_scroll_margin = this_scroll_margin;
16102 if (window_wants_header_line (w))
16103 top_scroll_margin += CURRENT_HEADER_LINE_HEIGHT (w);
16105 /* Start with the row the cursor was displayed during the last
16106 not paused redisplay. Give up if that row is not valid. */
16107 if (w->last_cursor_vpos < 0
16108 || w->last_cursor_vpos >= w->current_matrix->nrows)
16109 rc = CURSOR_MOVEMENT_MUST_SCROLL;
16110 else
16112 row = MATRIX_ROW (w->current_matrix, w->last_cursor_vpos);
16113 if (row->mode_line_p)
16114 ++row;
16115 if (!row->enabled_p)
16116 rc = CURSOR_MOVEMENT_MUST_SCROLL;
16119 if (rc == CURSOR_MOVEMENT_CANNOT_BE_USED)
16121 bool scroll_p = false, must_scroll = false;
16122 int last_y = window_text_bottom_y (w) - this_scroll_margin;
16124 if (PT > w->last_point)
16126 /* Point has moved forward. */
16127 while (MATRIX_ROW_END_CHARPOS (row) < PT
16128 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
16130 eassert (row->enabled_p);
16131 ++row;
16134 /* If the end position of a row equals the start
16135 position of the next row, and PT is at that position,
16136 we would rather display cursor in the next line. */
16137 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
16138 && MATRIX_ROW_END_CHARPOS (row) == PT
16139 && row < MATRIX_MODE_LINE_ROW (w->current_matrix)
16140 && MATRIX_ROW_START_CHARPOS (row+1) == PT
16141 && !cursor_row_p (row))
16142 ++row;
16144 /* If within the scroll margin, scroll. Note that
16145 MATRIX_ROW_BOTTOM_Y gives the pixel position at which
16146 the next line would be drawn, and that
16147 this_scroll_margin can be zero. */
16148 if (MATRIX_ROW_BOTTOM_Y (row) > last_y
16149 || PT > MATRIX_ROW_END_CHARPOS (row)
16150 /* Line is completely visible last line in window
16151 and PT is to be set in the next line. */
16152 || (MATRIX_ROW_BOTTOM_Y (row) == last_y
16153 && PT == MATRIX_ROW_END_CHARPOS (row)
16154 && !row->ends_at_zv_p
16155 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
16156 scroll_p = true;
16158 else if (PT < w->last_point)
16160 /* Cursor has to be moved backward. Note that PT >=
16161 CHARPOS (startp) because of the outer if-statement. */
16162 while (!row->mode_line_p
16163 && (MATRIX_ROW_START_CHARPOS (row) > PT
16164 || (MATRIX_ROW_START_CHARPOS (row) == PT
16165 && (MATRIX_ROW_STARTS_IN_MIDDLE_OF_CHAR_P (row)
16166 || (/* STARTS_IN_MIDDLE_OF_STRING_P (row) */
16167 row > w->current_matrix->rows
16168 && (row-1)->ends_in_newline_from_string_p))))
16169 && (row->y > top_scroll_margin
16170 || CHARPOS (startp) == BEGV))
16172 eassert (row->enabled_p);
16173 --row;
16176 /* Consider the following case: Window starts at BEGV,
16177 there is invisible, intangible text at BEGV, so that
16178 display starts at some point START > BEGV. It can
16179 happen that we are called with PT somewhere between
16180 BEGV and START. Try to handle that case. */
16181 if (row < w->current_matrix->rows
16182 || row->mode_line_p)
16184 row = w->current_matrix->rows;
16185 if (row->mode_line_p)
16186 ++row;
16189 /* Due to newlines in overlay strings, we may have to
16190 skip forward over overlay strings. */
16191 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
16192 && MATRIX_ROW_END_CHARPOS (row) == PT
16193 && !cursor_row_p (row))
16194 ++row;
16196 /* If within the scroll margin, scroll. */
16197 if (row->y < top_scroll_margin
16198 && CHARPOS (startp) != BEGV)
16199 scroll_p = true;
16201 else
16203 /* Cursor did not move. So don't scroll even if cursor line
16204 is partially visible, as it was so before. */
16205 rc = CURSOR_MOVEMENT_SUCCESS;
16208 if (PT < MATRIX_ROW_START_CHARPOS (row)
16209 || PT > MATRIX_ROW_END_CHARPOS (row))
16211 /* if PT is not in the glyph row, give up. */
16212 rc = CURSOR_MOVEMENT_MUST_SCROLL;
16213 must_scroll = true;
16215 else if (rc != CURSOR_MOVEMENT_SUCCESS
16216 && !NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering)))
16218 struct glyph_row *row1;
16220 /* If rows are bidi-reordered and point moved, back up
16221 until we find a row that does not belong to a
16222 continuation line. This is because we must consider
16223 all rows of a continued line as candidates for the
16224 new cursor positioning, since row start and end
16225 positions change non-linearly with vertical position
16226 in such rows. */
16227 /* FIXME: Revisit this when glyph ``spilling'' in
16228 continuation lines' rows is implemented for
16229 bidi-reordered rows. */
16230 for (row1 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
16231 MATRIX_ROW_CONTINUATION_LINE_P (row);
16232 --row)
16234 /* If we hit the beginning of the displayed portion
16235 without finding the first row of a continued
16236 line, give up. */
16237 if (row <= row1)
16239 rc = CURSOR_MOVEMENT_MUST_SCROLL;
16240 break;
16242 eassert (row->enabled_p);
16245 if (must_scroll)
16247 else if (rc != CURSOR_MOVEMENT_SUCCESS
16248 && MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row)
16249 /* Make sure this isn't a header line by any chance, since
16250 then MATRIX_ROW_PARTIALLY_VISIBLE_P might yield true. */
16251 && !row->mode_line_p
16252 && make_cursor_line_fully_visible_p)
16254 if (PT == MATRIX_ROW_END_CHARPOS (row)
16255 && !row->ends_at_zv_p
16256 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
16257 rc = CURSOR_MOVEMENT_MUST_SCROLL;
16258 else if (row->height > window_box_height (w))
16260 /* If we end up in a partially visible line, let's
16261 make it fully visible, except when it's taller
16262 than the window, in which case we can't do much
16263 about it. */
16264 *scroll_step = true;
16265 rc = CURSOR_MOVEMENT_MUST_SCROLL;
16267 else
16269 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
16270 if (!cursor_row_fully_visible_p (w, false, true))
16271 rc = CURSOR_MOVEMENT_MUST_SCROLL;
16272 else
16273 rc = CURSOR_MOVEMENT_SUCCESS;
16276 else if (scroll_p)
16277 rc = CURSOR_MOVEMENT_MUST_SCROLL;
16278 else if (rc != CURSOR_MOVEMENT_SUCCESS
16279 && !NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering)))
16281 /* With bidi-reordered rows, there could be more than
16282 one candidate row whose start and end positions
16283 occlude point. We need to let set_cursor_from_row
16284 find the best candidate. */
16285 /* FIXME: Revisit this when glyph ``spilling'' in
16286 continuation lines' rows is implemented for
16287 bidi-reordered rows. */
16288 bool rv = false;
16292 bool at_zv_p = false, exact_match_p = false;
16294 if (MATRIX_ROW_START_CHARPOS (row) <= PT
16295 && PT <= MATRIX_ROW_END_CHARPOS (row)
16296 && cursor_row_p (row))
16297 rv |= set_cursor_from_row (w, row, w->current_matrix,
16298 0, 0, 0, 0);
16299 /* As soon as we've found the exact match for point,
16300 or the first suitable row whose ends_at_zv_p flag
16301 is set, we are done. */
16302 if (rv)
16304 at_zv_p = MATRIX_ROW (w->current_matrix,
16305 w->cursor.vpos)->ends_at_zv_p;
16306 if (!at_zv_p
16307 && w->cursor.hpos >= 0
16308 && w->cursor.hpos < MATRIX_ROW_USED (w->current_matrix,
16309 w->cursor.vpos))
16311 struct glyph_row *candidate =
16312 MATRIX_ROW (w->current_matrix, w->cursor.vpos);
16313 struct glyph *g =
16314 candidate->glyphs[TEXT_AREA] + w->cursor.hpos;
16315 ptrdiff_t endpos = MATRIX_ROW_END_CHARPOS (candidate);
16317 exact_match_p =
16318 (BUFFERP (g->object) && g->charpos == PT)
16319 || (NILP (g->object)
16320 && (g->charpos == PT
16321 || (g->charpos == 0 && endpos - 1 == PT)));
16323 if (at_zv_p || exact_match_p)
16325 rc = CURSOR_MOVEMENT_SUCCESS;
16326 break;
16329 if (MATRIX_ROW_BOTTOM_Y (row) == last_y)
16330 break;
16331 ++row;
16333 while (((MATRIX_ROW_CONTINUATION_LINE_P (row)
16334 || row->continued_p)
16335 && MATRIX_ROW_BOTTOM_Y (row) <= last_y)
16336 || (MATRIX_ROW_START_CHARPOS (row) == PT
16337 && MATRIX_ROW_BOTTOM_Y (row) < last_y));
16338 /* If we didn't find any candidate rows, or exited the
16339 loop before all the candidates were examined, signal
16340 to the caller that this method failed. */
16341 if (rc != CURSOR_MOVEMENT_SUCCESS
16342 && !(rv
16343 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
16344 && !row->continued_p))
16345 rc = CURSOR_MOVEMENT_MUST_SCROLL;
16346 else if (rv)
16347 rc = CURSOR_MOVEMENT_SUCCESS;
16349 else
16353 if (set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0))
16355 rc = CURSOR_MOVEMENT_SUCCESS;
16356 break;
16358 ++row;
16360 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
16361 && MATRIX_ROW_START_CHARPOS (row) == PT
16362 && cursor_row_p (row));
16367 return rc;
16371 void
16372 set_vertical_scroll_bar (struct window *w)
16374 ptrdiff_t start, end, whole;
16376 /* Calculate the start and end positions for the current window.
16377 At some point, it would be nice to choose between scrollbars
16378 which reflect the whole buffer size, with special markers
16379 indicating narrowing, and scrollbars which reflect only the
16380 visible region.
16382 Note that mini-buffers sometimes aren't displaying any text. */
16383 if (!MINI_WINDOW_P (w)
16384 || (w == XWINDOW (minibuf_window)
16385 && NILP (echo_area_buffer[0])))
16387 struct buffer *buf = XBUFFER (w->contents);
16388 whole = BUF_ZV (buf) - BUF_BEGV (buf);
16389 start = marker_position (w->start) - BUF_BEGV (buf);
16390 /* I don't think this is guaranteed to be right. For the
16391 moment, we'll pretend it is. */
16392 end = BUF_Z (buf) - w->window_end_pos - BUF_BEGV (buf);
16394 if (end < start)
16395 end = start;
16396 if (whole < (end - start))
16397 whole = end - start;
16399 else
16400 start = end = whole = 0;
16402 /* Indicate what this scroll bar ought to be displaying now. */
16403 if (FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
16404 (*FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
16405 (w, end - start, whole, start);
16409 void
16410 set_horizontal_scroll_bar (struct window *w)
16412 int start, end, whole, portion;
16414 if (!MINI_WINDOW_P (w)
16415 || (w == XWINDOW (minibuf_window)
16416 && NILP (echo_area_buffer[0])))
16418 struct buffer *b = XBUFFER (w->contents);
16419 struct buffer *old_buffer = NULL;
16420 struct it it;
16421 struct text_pos startp;
16423 if (b != current_buffer)
16425 old_buffer = current_buffer;
16426 set_buffer_internal (b);
16429 SET_TEXT_POS_FROM_MARKER (startp, w->start);
16430 start_display (&it, w, startp);
16431 it.last_visible_x = INT_MAX;
16432 whole = move_it_to (&it, -1, INT_MAX, window_box_height (w), -1,
16433 MOVE_TO_X | MOVE_TO_Y);
16434 /* whole = move_it_to (&it, w->window_end_pos, INT_MAX,
16435 window_box_height (w), -1,
16436 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y); */
16438 start = w->hscroll * FRAME_COLUMN_WIDTH (WINDOW_XFRAME (w));
16439 end = start + window_box_width (w, TEXT_AREA);
16440 portion = end - start;
16441 /* After enlarging a horizontally scrolled window such that it
16442 gets at least as wide as the text it contains, make sure that
16443 the thumb doesn't fill the entire scroll bar so we can still
16444 drag it back to see the entire text. */
16445 whole = max (whole, end);
16447 if (it.bidi_p)
16449 Lisp_Object pdir;
16451 pdir = Fcurrent_bidi_paragraph_direction (Qnil);
16452 if (EQ (pdir, Qright_to_left))
16454 start = whole - end;
16455 end = start + portion;
16459 if (old_buffer)
16460 set_buffer_internal (old_buffer);
16462 else
16463 start = end = whole = portion = 0;
16465 w->hscroll_whole = whole;
16467 /* Indicate what this scroll bar ought to be displaying now. */
16468 if (FRAME_TERMINAL (XFRAME (w->frame))->set_horizontal_scroll_bar_hook)
16469 (*FRAME_TERMINAL (XFRAME (w->frame))->set_horizontal_scroll_bar_hook)
16470 (w, portion, whole, start);
16474 /* Redisplay leaf window WINDOW. JUST_THIS_ONE_P means only
16475 selected_window is redisplayed.
16477 We can return without actually redisplaying the window if fonts has been
16478 changed on window's frame. In that case, redisplay_internal will retry.
16480 As one of the important parts of redisplaying a window, we need to
16481 decide whether the previous window-start position (stored in the
16482 window's w->start marker position) is still valid, and if it isn't,
16483 recompute it. Some details about that:
16485 . The previous window-start could be in a continuation line, in
16486 which case we need to recompute it when the window width
16487 changes. See compute_window_start_on_continuation_line and its
16488 call below.
16490 . The text that changed since last redisplay could include the
16491 previous window-start position. In that case, we try to salvage
16492 what we can from the current glyph matrix by calling
16493 try_scrolling, which see.
16495 . Some Emacs command could force us to use a specific window-start
16496 position by setting the window's force_start flag, or gently
16497 propose doing that by setting the window's optional_new_start
16498 flag. In these cases, we try using the specified start point if
16499 that succeeds (i.e. the window desired matrix is successfully
16500 recomputed, and point location is within the window). In case
16501 of optional_new_start, we first check if the specified start
16502 position is feasible, i.e. if it will allow point to be
16503 displayed in the window. If using the specified start point
16504 fails, e.g., if new fonts are needed to be loaded, we abort the
16505 redisplay cycle and leave it up to the next cycle to figure out
16506 things.
16508 . Note that the window's force_start flag is sometimes set by
16509 redisplay itself, when it decides that the previous window start
16510 point is fine and should be kept. Search for "goto force_start"
16511 below to see the details. Like the values of window-start
16512 specified outside of redisplay, these internally-deduced values
16513 are tested for feasibility, and ignored if found to be
16514 unfeasible.
16516 . Note that the function try_window, used to completely redisplay
16517 a window, accepts the window's start point as its argument.
16518 This is used several times in the redisplay code to control
16519 where the window start will be, according to user options such
16520 as scroll-conservatively, and also to ensure the screen line
16521 showing point will be fully (as opposed to partially) visible on
16522 display. */
16524 static void
16525 redisplay_window (Lisp_Object window, bool just_this_one_p)
16527 struct window *w = XWINDOW (window);
16528 struct frame *f = XFRAME (w->frame);
16529 struct buffer *buffer = XBUFFER (w->contents);
16530 struct buffer *old = current_buffer;
16531 struct text_pos lpoint, opoint, startp;
16532 bool update_mode_line;
16533 int tem;
16534 struct it it;
16535 /* Record it now because it's overwritten. */
16536 bool current_matrix_up_to_date_p = false;
16537 bool used_current_matrix_p = false;
16538 /* This is less strict than current_matrix_up_to_date_p.
16539 It indicates that the buffer contents and narrowing are unchanged. */
16540 bool buffer_unchanged_p = false;
16541 bool temp_scroll_step = false;
16542 ptrdiff_t count = SPECPDL_INDEX ();
16543 int rc;
16544 int centering_position = -1;
16545 bool last_line_misfit = false;
16546 ptrdiff_t beg_unchanged, end_unchanged;
16547 int frame_line_height, margin;
16548 bool use_desired_matrix;
16549 void *itdata = NULL;
16551 SET_TEXT_POS (lpoint, PT, PT_BYTE);
16552 opoint = lpoint;
16554 #ifdef GLYPH_DEBUG
16555 *w->desired_matrix->method = 0;
16556 #endif
16558 if (!just_this_one_p
16559 && REDISPLAY_SOME_P ()
16560 && !w->redisplay
16561 && !w->update_mode_line
16562 && !f->face_change
16563 && !f->redisplay
16564 && !buffer->text->redisplay
16565 && BUF_PT (buffer) == w->last_point)
16566 return;
16568 /* Make sure that both W's markers are valid. */
16569 eassert (XMARKER (w->start)->buffer == buffer);
16570 eassert (XMARKER (w->pointm)->buffer == buffer);
16572 reconsider_clip_changes (w);
16573 frame_line_height = default_line_pixel_height (w);
16574 margin = window_scroll_margin (w, MARGIN_IN_LINES);
16577 /* Has the mode line to be updated? */
16578 update_mode_line = (w->update_mode_line
16579 || update_mode_lines
16580 || buffer->clip_changed
16581 || buffer->prevent_redisplay_optimizations_p);
16583 if (!just_this_one_p)
16584 /* If `just_this_one_p' is set, we apparently set must_be_updated_p more
16585 cleverly elsewhere. */
16586 w->must_be_updated_p = true;
16588 if (MINI_WINDOW_P (w))
16590 if (w == XWINDOW (echo_area_window)
16591 && !NILP (echo_area_buffer[0]))
16593 if (update_mode_line)
16594 /* We may have to update a tty frame's menu bar or a
16595 tool-bar. Example `M-x C-h C-h C-g'. */
16596 goto finish_menu_bars;
16597 else
16598 /* We've already displayed the echo area glyphs in this window. */
16599 goto finish_scroll_bars;
16601 else if ((w != XWINDOW (minibuf_window)
16602 || minibuf_level == 0)
16603 /* When buffer is nonempty, redisplay window normally. */
16604 && BUF_Z (XBUFFER (w->contents)) == BUF_BEG (XBUFFER (w->contents))
16605 /* Quail displays non-mini buffers in minibuffer window.
16606 In that case, redisplay the window normally. */
16607 && !NILP (Fmemq (w->contents, Vminibuffer_list)))
16609 /* W is a mini-buffer window, but it's not active, so clear
16610 it. */
16611 int yb = window_text_bottom_y (w);
16612 struct glyph_row *row;
16613 int y;
16615 for (y = 0, row = w->desired_matrix->rows;
16616 y < yb;
16617 y += row->height, ++row)
16618 blank_row (w, row, y);
16619 goto finish_scroll_bars;
16622 clear_glyph_matrix (w->desired_matrix);
16625 /* Otherwise set up data on this window; select its buffer and point
16626 value. */
16627 /* Really select the buffer, for the sake of buffer-local
16628 variables. */
16629 set_buffer_internal_1 (XBUFFER (w->contents));
16631 current_matrix_up_to_date_p
16632 = (w->window_end_valid
16633 && !current_buffer->clip_changed
16634 && !current_buffer->prevent_redisplay_optimizations_p
16635 && !window_outdated (w)
16636 && !hscrolling_current_line_p (w));
16638 beg_unchanged = BEG_UNCHANGED;
16639 end_unchanged = END_UNCHANGED;
16641 SET_TEXT_POS (opoint, PT, PT_BYTE);
16643 specbind (Qinhibit_point_motion_hooks, Qt);
16645 buffer_unchanged_p
16646 = (w->window_end_valid
16647 && !current_buffer->clip_changed
16648 && !window_outdated (w));
16650 /* When windows_or_buffers_changed is non-zero, we can't rely
16651 on the window end being valid, so set it to zero there. */
16652 if (windows_or_buffers_changed)
16654 /* If window starts on a continuation line, maybe adjust the
16655 window start in case the window's width changed. */
16656 if (XMARKER (w->start)->buffer == current_buffer)
16657 compute_window_start_on_continuation_line (w);
16659 w->window_end_valid = false;
16660 /* If so, we also can't rely on current matrix
16661 and should not fool try_cursor_movement below. */
16662 current_matrix_up_to_date_p = false;
16665 /* Some sanity checks. */
16666 CHECK_WINDOW_END (w);
16667 if (Z == Z_BYTE && CHARPOS (opoint) != BYTEPOS (opoint))
16668 emacs_abort ();
16669 if (BYTEPOS (opoint) < CHARPOS (opoint))
16670 emacs_abort ();
16672 if (mode_line_update_needed (w))
16673 update_mode_line = true;
16675 /* Point refers normally to the selected window. For any other
16676 window, set up appropriate value. */
16677 if (!EQ (window, selected_window))
16679 ptrdiff_t new_pt = marker_position (w->pointm);
16680 ptrdiff_t new_pt_byte = marker_byte_position (w->pointm);
16682 if (new_pt < BEGV)
16684 new_pt = BEGV;
16685 new_pt_byte = BEGV_BYTE;
16686 set_marker_both (w->pointm, Qnil, BEGV, BEGV_BYTE);
16688 else if (new_pt > (ZV - 1))
16690 new_pt = ZV;
16691 new_pt_byte = ZV_BYTE;
16692 set_marker_both (w->pointm, Qnil, ZV, ZV_BYTE);
16695 /* We don't use SET_PT so that the point-motion hooks don't run. */
16696 TEMP_SET_PT_BOTH (new_pt, new_pt_byte);
16699 /* If any of the character widths specified in the display table
16700 have changed, invalidate the width run cache. It's true that
16701 this may be a bit late to catch such changes, but the rest of
16702 redisplay goes (non-fatally) haywire when the display table is
16703 changed, so why should we worry about doing any better? */
16704 if (current_buffer->width_run_cache
16705 || (current_buffer->base_buffer
16706 && current_buffer->base_buffer->width_run_cache))
16708 struct Lisp_Char_Table *disptab = buffer_display_table ();
16710 if (! disptab_matches_widthtab
16711 (disptab, XVECTOR (BVAR (current_buffer, width_table))))
16713 struct buffer *buf = current_buffer;
16715 if (buf->base_buffer)
16716 buf = buf->base_buffer;
16717 invalidate_region_cache (buf, buf->width_run_cache, BEG, Z);
16718 recompute_width_table (current_buffer, disptab);
16722 /* If window-start is screwed up, choose a new one. */
16723 if (XMARKER (w->start)->buffer != current_buffer)
16724 goto recenter;
16726 SET_TEXT_POS_FROM_MARKER (startp, w->start);
16728 /* If someone specified a new starting point but did not insist,
16729 check whether it can be used. */
16730 if ((w->optional_new_start || window_frozen_p (w))
16731 && CHARPOS (startp) >= BEGV
16732 && CHARPOS (startp) <= ZV)
16734 ptrdiff_t it_charpos;
16736 w->optional_new_start = false;
16737 start_display (&it, w, startp);
16738 move_it_to (&it, PT, 0, it.last_visible_y, -1,
16739 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
16740 /* Record IT's position now, since line_bottom_y might change
16741 that. */
16742 it_charpos = IT_CHARPOS (it);
16743 /* Make sure we set the force_start flag only if the cursor row
16744 will be fully visible. Otherwise, the code under force_start
16745 label below will try to move point back into view, which is
16746 not what the code which sets optional_new_start wants. */
16747 if ((it.current_y == 0 || line_bottom_y (&it) < it.last_visible_y)
16748 && !w->force_start)
16750 if (it_charpos == PT)
16751 w->force_start = true;
16752 /* IT may overshoot PT if text at PT is invisible. */
16753 else if (it_charpos > PT && CHARPOS (startp) <= PT)
16754 w->force_start = true;
16755 #ifdef GLYPH_DEBUG
16756 if (w->force_start)
16758 if (window_frozen_p (w))
16759 debug_method_add (w, "set force_start from frozen window start");
16760 else
16761 debug_method_add (w, "set force_start from optional_new_start");
16763 #endif
16767 force_start:
16769 /* Handle case where place to start displaying has been specified,
16770 unless the specified location is outside the accessible range. */
16771 if (w->force_start)
16773 /* We set this later on if we have to adjust point. */
16774 int new_vpos = -1;
16776 w->force_start = false;
16777 w->vscroll = 0;
16778 w->window_end_valid = false;
16780 /* Forget any recorded base line for line number display. */
16781 if (!buffer_unchanged_p)
16782 w->base_line_number = 0;
16784 /* Redisplay the mode line. Select the buffer properly for that.
16785 Also, run the hook window-scroll-functions
16786 because we have scrolled. */
16787 /* Note, we do this after clearing force_start because
16788 if there's an error, it is better to forget about force_start
16789 than to get into an infinite loop calling the hook functions
16790 and having them get more errors. */
16791 if (!update_mode_line
16792 || ! NILP (Vwindow_scroll_functions))
16794 update_mode_line = true;
16795 w->update_mode_line = true;
16796 startp = run_window_scroll_functions (window, startp);
16799 if (CHARPOS (startp) < BEGV)
16800 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
16801 else if (CHARPOS (startp) > ZV)
16802 SET_TEXT_POS (startp, ZV, ZV_BYTE);
16804 /* Redisplay, then check if cursor has been set during the
16805 redisplay. Give up if new fonts were loaded. */
16806 /* We used to issue a CHECK_MARGINS argument to try_window here,
16807 but this causes scrolling to fail when point begins inside
16808 the scroll margin (bug#148) -- cyd */
16809 if (!try_window (window, startp, 0))
16811 w->force_start = true;
16812 clear_glyph_matrix (w->desired_matrix);
16813 goto need_larger_matrices;
16816 if (w->cursor.vpos < 0)
16818 /* If point does not appear, try to move point so it does
16819 appear. The desired matrix has been built above, so we
16820 can use it here. First see if point is in invisible
16821 text, and if so, move it to the first visible buffer
16822 position past that. */
16823 struct glyph_row *r = NULL;
16824 Lisp_Object invprop =
16825 get_char_property_and_overlay (make_number (PT), Qinvisible,
16826 Qnil, NULL);
16828 if (TEXT_PROP_MEANS_INVISIBLE (invprop) != 0)
16830 ptrdiff_t alt_pt;
16831 Lisp_Object invprop_end =
16832 Fnext_single_char_property_change (make_number (PT), Qinvisible,
16833 Qnil, Qnil);
16835 if (NATNUMP (invprop_end))
16836 alt_pt = XFASTINT (invprop_end);
16837 else
16838 alt_pt = ZV;
16839 r = row_containing_pos (w, alt_pt, w->desired_matrix->rows,
16840 NULL, 0);
16842 if (r)
16843 new_vpos = MATRIX_ROW_BOTTOM_Y (r);
16844 else /* Give up and just move to the middle of the window. */
16845 new_vpos = window_box_height (w) / 2;
16848 if (!cursor_row_fully_visible_p (w, false, false))
16850 /* Point does appear, but on a line partly visible at end of window.
16851 Move it back to a fully-visible line. */
16852 new_vpos = window_box_height (w);
16853 /* But if window_box_height suggests a Y coordinate that is
16854 not less than we already have, that line will clearly not
16855 be fully visible, so give up and scroll the display.
16856 This can happen when the default face uses a font whose
16857 dimensions are different from the frame's default
16858 font. */
16859 if (new_vpos >= w->cursor.y)
16861 w->cursor.vpos = -1;
16862 clear_glyph_matrix (w->desired_matrix);
16863 goto try_to_scroll;
16866 else if (w->cursor.vpos >= 0)
16868 /* Some people insist on not letting point enter the scroll
16869 margin, even though this part handles windows that didn't
16870 scroll at all. */
16871 int pixel_margin = margin * frame_line_height;
16872 bool header_line = window_wants_header_line (w);
16874 /* Note: We add an extra FRAME_LINE_HEIGHT, because the loop
16875 below, which finds the row to move point to, advances by
16876 the Y coordinate of the _next_ row, see the definition of
16877 MATRIX_ROW_BOTTOM_Y. */
16878 if (w->cursor.vpos < margin + header_line)
16880 w->cursor.vpos = -1;
16881 clear_glyph_matrix (w->desired_matrix);
16882 goto try_to_scroll;
16884 else
16886 int window_height = window_box_height (w);
16888 if (header_line)
16889 window_height += CURRENT_HEADER_LINE_HEIGHT (w);
16890 if (w->cursor.y >= window_height - pixel_margin)
16892 w->cursor.vpos = -1;
16893 clear_glyph_matrix (w->desired_matrix);
16894 goto try_to_scroll;
16899 /* If we need to move point for either of the above reasons,
16900 now actually do it. */
16901 if (new_vpos >= 0)
16903 struct glyph_row *row;
16905 row = MATRIX_FIRST_TEXT_ROW (w->desired_matrix);
16906 while (MATRIX_ROW_BOTTOM_Y (row) < new_vpos)
16907 ++row;
16909 TEMP_SET_PT_BOTH (MATRIX_ROW_START_CHARPOS (row),
16910 MATRIX_ROW_START_BYTEPOS (row));
16912 if (w != XWINDOW (selected_window))
16913 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
16914 else if (current_buffer == old)
16915 SET_TEXT_POS (lpoint, PT, PT_BYTE);
16917 set_cursor_from_row (w, row, w->desired_matrix, 0, 0, 0, 0);
16919 /* Re-run pre-redisplay-function so it can update the region
16920 according to the new position of point. */
16921 /* Other than the cursor, w's redisplay is done so we can set its
16922 redisplay to false. Also the buffer's redisplay can be set to
16923 false, since propagate_buffer_redisplay should have already
16924 propagated its info to `w' anyway. */
16925 w->redisplay = false;
16926 XBUFFER (w->contents)->text->redisplay = false;
16927 safe__call1 (true, Vpre_redisplay_function, Fcons (window, Qnil));
16929 if (w->redisplay || XBUFFER (w->contents)->text->redisplay
16930 || ((EQ (Vdisplay_line_numbers, Qrelative)
16931 || EQ (Vdisplay_line_numbers, Qvisual))
16932 && row != MATRIX_FIRST_TEXT_ROW (w->desired_matrix)))
16934 /* Either pre-redisplay-function made changes (e.g. move
16935 the region), or we moved point in a window that is
16936 under display-line-numbers = relative mode. We need
16937 another round of redisplay. */
16938 clear_glyph_matrix (w->desired_matrix);
16939 if (!try_window (window, startp, 0))
16940 goto need_larger_matrices;
16943 if (w->cursor.vpos < 0 || !cursor_row_fully_visible_p (w, false, false))
16945 clear_glyph_matrix (w->desired_matrix);
16946 goto try_to_scroll;
16949 #ifdef GLYPH_DEBUG
16950 debug_method_add (w, "forced window start");
16951 #endif
16952 goto done;
16955 /* Handle case where text has not changed, only point, and it has
16956 not moved off the frame, and we are not retrying after hscroll.
16957 (current_matrix_up_to_date_p is true when retrying.) */
16958 if (current_matrix_up_to_date_p
16959 && (rc = try_cursor_movement (window, startp, &temp_scroll_step),
16960 rc != CURSOR_MOVEMENT_CANNOT_BE_USED))
16962 switch (rc)
16964 case CURSOR_MOVEMENT_SUCCESS:
16965 used_current_matrix_p = true;
16966 goto done;
16968 case CURSOR_MOVEMENT_MUST_SCROLL:
16969 goto try_to_scroll;
16971 default:
16972 emacs_abort ();
16975 /* If current starting point was originally the beginning of a line
16976 but no longer is, find a new starting point. */
16977 else if (w->start_at_line_beg
16978 && !(CHARPOS (startp) <= BEGV
16979 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n'))
16981 #ifdef GLYPH_DEBUG
16982 debug_method_add (w, "recenter 1");
16983 #endif
16984 goto recenter;
16987 /* Try scrolling with try_window_id. Value is > 0 if update has
16988 been done, it is -1 if we know that the same window start will
16989 not work. It is 0 if unsuccessful for some other reason. */
16990 else if ((tem = try_window_id (w)) != 0)
16992 #ifdef GLYPH_DEBUG
16993 debug_method_add (w, "try_window_id %d", tem);
16994 #endif
16996 if (f->fonts_changed)
16997 goto need_larger_matrices;
16998 if (tem > 0)
16999 goto done;
17001 /* Otherwise try_window_id has returned -1 which means that we
17002 don't want the alternative below this comment to execute. */
17004 else if (CHARPOS (startp) >= BEGV
17005 && CHARPOS (startp) <= ZV
17006 && PT >= CHARPOS (startp)
17007 && (CHARPOS (startp) < ZV
17008 /* Avoid starting at end of buffer. */
17009 || CHARPOS (startp) == BEGV
17010 || !window_outdated (w)))
17012 int d1, d2, d5, d6;
17013 int rtop, rbot;
17015 /* If first window line is a continuation line, and window start
17016 is inside the modified region, but the first change is before
17017 current window start, we must select a new window start.
17019 However, if this is the result of a down-mouse event (e.g. by
17020 extending the mouse-drag-overlay), we don't want to select a
17021 new window start, since that would change the position under
17022 the mouse, resulting in an unwanted mouse-movement rather
17023 than a simple mouse-click. */
17024 if (!w->start_at_line_beg
17025 && NILP (do_mouse_tracking)
17026 && CHARPOS (startp) > BEGV
17027 && CHARPOS (startp) > BEG + beg_unchanged
17028 && CHARPOS (startp) <= Z - end_unchanged
17029 /* Even if w->start_at_line_beg is nil, a new window may
17030 start at a line_beg, since that's how set_buffer_window
17031 sets it. So, we need to check the return value of
17032 compute_window_start_on_continuation_line. (See also
17033 bug#197). */
17034 && XMARKER (w->start)->buffer == current_buffer
17035 && compute_window_start_on_continuation_line (w)
17036 /* It doesn't make sense to force the window start like we
17037 do at label force_start if it is already known that point
17038 will not be fully visible in the resulting window, because
17039 doing so will move point from its correct position
17040 instead of scrolling the window to bring point into view.
17041 See bug#9324. */
17042 && pos_visible_p (w, PT, &d1, &d2, &rtop, &rbot, &d5, &d6)
17043 /* A very tall row could need more than the window height,
17044 in which case we accept that it is partially visible. */
17045 && (rtop != 0) == (rbot != 0))
17047 w->force_start = true;
17048 SET_TEXT_POS_FROM_MARKER (startp, w->start);
17049 #ifdef GLYPH_DEBUG
17050 debug_method_add (w, "recomputed window start in continuation line");
17051 #endif
17052 goto force_start;
17055 #ifdef GLYPH_DEBUG
17056 debug_method_add (w, "same window start");
17057 #endif
17059 /* Try to redisplay starting at same place as before.
17060 If point has not moved off frame, accept the results. */
17061 if (!current_matrix_up_to_date_p
17062 /* Don't use try_window_reusing_current_matrix in this case
17063 because a window scroll function can have changed the
17064 buffer. */
17065 || !NILP (Vwindow_scroll_functions)
17066 || MINI_WINDOW_P (w)
17067 || !(used_current_matrix_p
17068 = try_window_reusing_current_matrix (w)))
17070 IF_DEBUG (debug_method_add (w, "1"));
17071 clear_glyph_matrix (w->desired_matrix);
17072 if (try_window (window, startp, TRY_WINDOW_CHECK_MARGINS) < 0)
17073 /* -1 means we need to scroll.
17074 0 means we need new matrices, but fonts_changed
17075 is set in that case, so we will detect it below. */
17076 goto try_to_scroll;
17079 if (f->fonts_changed)
17080 goto need_larger_matrices;
17082 if (w->cursor.vpos >= 0)
17084 if (!just_this_one_p
17085 || current_buffer->clip_changed
17086 || BEG_UNCHANGED < CHARPOS (startp))
17087 /* Forget any recorded base line for line number display. */
17088 w->base_line_number = 0;
17090 if (!cursor_row_fully_visible_p (w, true, false))
17092 clear_glyph_matrix (w->desired_matrix);
17093 last_line_misfit = true;
17095 /* Drop through and scroll. */
17096 else
17097 goto done;
17099 else
17100 clear_glyph_matrix (w->desired_matrix);
17103 try_to_scroll:
17105 /* Redisplay the mode line. Select the buffer properly for that. */
17106 if (!update_mode_line)
17108 update_mode_line = true;
17109 w->update_mode_line = true;
17112 /* Try to scroll by specified few lines. */
17113 if ((scroll_conservatively
17114 || emacs_scroll_step
17115 || temp_scroll_step
17116 || NUMBERP (BVAR (current_buffer, scroll_up_aggressively))
17117 || NUMBERP (BVAR (current_buffer, scroll_down_aggressively)))
17118 && CHARPOS (startp) >= BEGV
17119 && CHARPOS (startp) <= ZV)
17121 /* The function returns -1 if new fonts were loaded, 1 if
17122 successful, 0 if not successful. */
17123 int ss = try_scrolling (window, just_this_one_p,
17124 scroll_conservatively,
17125 emacs_scroll_step,
17126 temp_scroll_step, last_line_misfit);
17127 switch (ss)
17129 case SCROLLING_SUCCESS:
17130 goto done;
17132 case SCROLLING_NEED_LARGER_MATRICES:
17133 goto need_larger_matrices;
17135 case SCROLLING_FAILED:
17136 break;
17138 default:
17139 emacs_abort ();
17143 /* Finally, just choose a place to start which positions point
17144 according to user preferences. */
17146 recenter:
17148 #ifdef GLYPH_DEBUG
17149 debug_method_add (w, "recenter");
17150 #endif
17152 /* Forget any previously recorded base line for line number display. */
17153 if (!buffer_unchanged_p)
17154 w->base_line_number = 0;
17156 /* Determine the window start relative to point. */
17157 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
17158 it.current_y = it.last_visible_y;
17159 if (centering_position < 0)
17161 ptrdiff_t margin_pos = CHARPOS (startp);
17162 Lisp_Object aggressive;
17163 bool scrolling_up;
17165 /* If there is a scroll margin at the top of the window, find
17166 its character position. */
17167 if (margin
17168 /* Cannot call start_display if startp is not in the
17169 accessible region of the buffer. This can happen when we
17170 have just switched to a different buffer and/or changed
17171 its restriction. In that case, startp is initialized to
17172 the character position 1 (BEGV) because we did not yet
17173 have chance to display the buffer even once. */
17174 && BEGV <= CHARPOS (startp) && CHARPOS (startp) <= ZV)
17176 struct it it1;
17177 void *it1data = NULL;
17179 SAVE_IT (it1, it, it1data);
17180 start_display (&it1, w, startp);
17181 move_it_vertically (&it1, margin * frame_line_height);
17182 margin_pos = IT_CHARPOS (it1);
17183 RESTORE_IT (&it, &it, it1data);
17185 scrolling_up = PT > margin_pos;
17186 aggressive =
17187 scrolling_up
17188 ? BVAR (current_buffer, scroll_up_aggressively)
17189 : BVAR (current_buffer, scroll_down_aggressively);
17191 if (!MINI_WINDOW_P (w)
17192 && (scroll_conservatively > SCROLL_LIMIT || NUMBERP (aggressive)))
17194 int pt_offset = 0;
17196 /* Setting scroll-conservatively overrides
17197 scroll-*-aggressively. */
17198 if (!scroll_conservatively && NUMBERP (aggressive))
17200 double float_amount = XFLOATINT (aggressive);
17202 pt_offset = float_amount * WINDOW_BOX_TEXT_HEIGHT (w);
17203 if (pt_offset == 0 && float_amount > 0)
17204 pt_offset = 1;
17205 if (pt_offset && margin > 0)
17206 margin -= 1;
17208 /* Compute how much to move the window start backward from
17209 point so that point will be displayed where the user
17210 wants it. */
17211 if (scrolling_up)
17213 centering_position = it.last_visible_y;
17214 if (pt_offset)
17215 centering_position -= pt_offset;
17216 centering_position -=
17217 (frame_line_height * (1 + margin + last_line_misfit)
17218 + WINDOW_HEADER_LINE_HEIGHT (w));
17219 /* Don't let point enter the scroll margin near top of
17220 the window. */
17221 if (centering_position < margin * frame_line_height)
17222 centering_position = margin * frame_line_height;
17224 else
17225 centering_position = margin * frame_line_height + pt_offset;
17227 else
17228 /* Set the window start half the height of the window backward
17229 from point. */
17230 centering_position = window_box_height (w) / 2;
17232 move_it_vertically_backward (&it, centering_position);
17234 eassert (IT_CHARPOS (it) >= BEGV);
17236 /* The function move_it_vertically_backward may move over more
17237 than the specified y-distance. If it->w is small, e.g. a
17238 mini-buffer window, we may end up in front of the window's
17239 display area. Start displaying at the start of the line
17240 containing PT in this case. */
17241 if (it.current_y <= 0)
17243 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
17244 move_it_vertically_backward (&it, 0);
17245 it.current_y = 0;
17248 it.current_x = it.hpos = 0;
17250 /* Set the window start position here explicitly, to avoid an
17251 infinite loop in case the functions in window-scroll-functions
17252 get errors. */
17253 set_marker_both (w->start, Qnil, IT_CHARPOS (it), IT_BYTEPOS (it));
17255 /* Run scroll hooks. */
17256 startp = run_window_scroll_functions (window, it.current.pos);
17258 /* We invoke try_window and try_window_reusing_current_matrix below,
17259 and they manipulate the bidi cache. Save and restore the cache
17260 state of our iterator, so we could continue using it after that. */
17261 itdata = bidi_shelve_cache ();
17263 /* Redisplay the window. */
17264 use_desired_matrix = false;
17265 if (!current_matrix_up_to_date_p
17266 || windows_or_buffers_changed
17267 || f->cursor_type_changed
17268 /* Don't use try_window_reusing_current_matrix in this case
17269 because it can have changed the buffer. */
17270 || !NILP (Vwindow_scroll_functions)
17271 || !just_this_one_p
17272 || MINI_WINDOW_P (w)
17273 || !(used_current_matrix_p
17274 = try_window_reusing_current_matrix (w)))
17275 use_desired_matrix = (try_window (window, startp, 0) == 1);
17277 bidi_unshelve_cache (itdata, false);
17279 /* If new fonts have been loaded (due to fontsets), give up. We
17280 have to start a new redisplay since we need to re-adjust glyph
17281 matrices. */
17282 if (f->fonts_changed)
17283 goto need_larger_matrices;
17285 /* If cursor did not appear assume that the middle of the window is
17286 in the first line of the window. Do it again with the next line.
17287 (Imagine a window of height 100, displaying two lines of height
17288 60. Moving back 50 from it->last_visible_y will end in the first
17289 line.) */
17290 if (w->cursor.vpos < 0)
17292 if (w->window_end_valid && PT >= Z - w->window_end_pos)
17294 clear_glyph_matrix (w->desired_matrix);
17295 move_it_by_lines (&it, 1);
17296 try_window (window, it.current.pos, 0);
17298 else if (PT < IT_CHARPOS (it))
17300 clear_glyph_matrix (w->desired_matrix);
17301 move_it_by_lines (&it, -1);
17302 try_window (window, it.current.pos, 0);
17304 else if (scroll_conservatively > SCROLL_LIMIT
17305 && (it.method == GET_FROM_STRING
17306 || overlay_touches_p (IT_CHARPOS (it)))
17307 && IT_CHARPOS (it) < ZV)
17309 /* If the window starts with a before-string that spans more
17310 than one screen line, using that position to display the
17311 window might fail to bring point into the view, because
17312 start_display will always start by displaying the string,
17313 whereas the code above determines where to set w->start
17314 by the buffer position of the place where it takes screen
17315 coordinates. Try to recover by finding the next screen
17316 line that displays buffer text. */
17317 ptrdiff_t pos0 = IT_CHARPOS (it);
17319 clear_glyph_matrix (w->desired_matrix);
17320 do {
17321 move_it_by_lines (&it, 1);
17322 } while (IT_CHARPOS (it) == pos0);
17323 try_window (window, it.current.pos, 0);
17325 else
17327 /* Not much we can do about it. */
17331 /* Consider the following case: Window starts at BEGV, there is
17332 invisible, intangible text at BEGV, so that display starts at
17333 some point START > BEGV. It can happen that we are called with
17334 PT somewhere between BEGV and START. Try to handle that case,
17335 and similar ones. */
17336 if (w->cursor.vpos < 0)
17338 /* Prefer the desired matrix to the current matrix, if possible,
17339 in the fallback calculations below. This is because using
17340 the current matrix might completely goof, e.g. if its first
17341 row is after point. */
17342 struct glyph_matrix *matrix =
17343 use_desired_matrix ? w->desired_matrix : w->current_matrix;
17344 /* First, try locating the proper glyph row for PT. */
17345 struct glyph_row *row =
17346 row_containing_pos (w, PT, matrix->rows, NULL, 0);
17348 /* Sometimes point is at the beginning of invisible text that is
17349 before the 1st character displayed in the row. In that case,
17350 row_containing_pos fails to find the row, because no glyphs
17351 with appropriate buffer positions are present in the row.
17352 Therefore, we next try to find the row which shows the 1st
17353 position after the invisible text. */
17354 if (!row)
17356 Lisp_Object val =
17357 get_char_property_and_overlay (make_number (PT), Qinvisible,
17358 Qnil, NULL);
17360 if (TEXT_PROP_MEANS_INVISIBLE (val) != 0)
17362 ptrdiff_t alt_pos;
17363 Lisp_Object invis_end =
17364 Fnext_single_char_property_change (make_number (PT), Qinvisible,
17365 Qnil, Qnil);
17367 if (NATNUMP (invis_end))
17368 alt_pos = XFASTINT (invis_end);
17369 else
17370 alt_pos = ZV;
17371 row = row_containing_pos (w, alt_pos, matrix->rows, NULL, 0);
17374 /* Finally, fall back on the first row of the window after the
17375 header line (if any). This is slightly better than not
17376 displaying the cursor at all. */
17377 if (!row)
17379 row = matrix->rows;
17380 if (row->mode_line_p)
17381 ++row;
17383 set_cursor_from_row (w, row, matrix, 0, 0, 0, 0);
17386 if (!cursor_row_fully_visible_p (w, false, false))
17388 /* If vscroll is enabled, disable it and try again. */
17389 if (w->vscroll)
17391 w->vscroll = 0;
17392 clear_glyph_matrix (w->desired_matrix);
17393 goto recenter;
17396 /* Users who set scroll-conservatively to a large number want
17397 point just above/below the scroll margin. If we ended up
17398 with point's row partially visible, move the window start to
17399 make that row fully visible and out of the margin. */
17400 if (scroll_conservatively > SCROLL_LIMIT)
17402 int window_total_lines
17403 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
17404 bool move_down = w->cursor.vpos >= window_total_lines / 2;
17406 move_it_by_lines (&it, move_down ? margin + 1 : -(margin + 1));
17407 clear_glyph_matrix (w->desired_matrix);
17408 if (1 == try_window (window, it.current.pos,
17409 TRY_WINDOW_CHECK_MARGINS))
17410 goto done;
17413 /* If centering point failed to make the whole line visible,
17414 put point at the top instead. That has to make the whole line
17415 visible, if it can be done. */
17416 if (centering_position == 0)
17417 goto done;
17419 clear_glyph_matrix (w->desired_matrix);
17420 centering_position = 0;
17421 goto recenter;
17424 done:
17426 SET_TEXT_POS_FROM_MARKER (startp, w->start);
17427 w->start_at_line_beg = (CHARPOS (startp) == BEGV
17428 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n');
17430 /* Display the mode line, if we must. */
17431 if ((update_mode_line
17432 /* If window not full width, must redo its mode line
17433 if (a) the window to its side is being redone and
17434 (b) we do a frame-based redisplay. This is a consequence
17435 of how inverted lines are drawn in frame-based redisplay. */
17436 || (!just_this_one_p
17437 && !FRAME_WINDOW_P (f)
17438 && !WINDOW_FULL_WIDTH_P (w))
17439 /* Line number to display. */
17440 || w->base_line_pos > 0
17441 /* Column number is displayed and different from the one displayed. */
17442 || (w->column_number_displayed != -1
17443 && (w->column_number_displayed != current_column ())))
17444 /* This means that the window has a mode line. */
17445 && (window_wants_mode_line (w)
17446 || window_wants_header_line (w)))
17449 display_mode_lines (w);
17451 /* If mode line height has changed, arrange for a thorough
17452 immediate redisplay using the correct mode line height. */
17453 if (window_wants_mode_line (w)
17454 && CURRENT_MODE_LINE_HEIGHT (w) != DESIRED_MODE_LINE_HEIGHT (w))
17456 f->fonts_changed = true;
17457 w->mode_line_height = -1;
17458 MATRIX_MODE_LINE_ROW (w->current_matrix)->height
17459 = DESIRED_MODE_LINE_HEIGHT (w);
17462 /* If header line height has changed, arrange for a thorough
17463 immediate redisplay using the correct header line height. */
17464 if (window_wants_header_line (w)
17465 && CURRENT_HEADER_LINE_HEIGHT (w) != DESIRED_HEADER_LINE_HEIGHT (w))
17467 f->fonts_changed = true;
17468 w->header_line_height = -1;
17469 MATRIX_HEADER_LINE_ROW (w->current_matrix)->height
17470 = DESIRED_HEADER_LINE_HEIGHT (w);
17473 if (f->fonts_changed)
17474 goto need_larger_matrices;
17477 if (!line_number_displayed && w->base_line_pos != -1)
17479 w->base_line_pos = 0;
17480 w->base_line_number = 0;
17483 finish_menu_bars:
17485 /* When we reach a frame's selected window, redo the frame's menu
17486 bar and the frame's title. */
17487 if (update_mode_line
17488 && EQ (FRAME_SELECTED_WINDOW (f), window))
17490 bool redisplay_menu_p;
17492 if (FRAME_WINDOW_P (f))
17494 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
17495 || defined (HAVE_NS) || defined (USE_GTK)
17496 redisplay_menu_p = FRAME_EXTERNAL_MENU_BAR (f);
17497 #else
17498 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
17499 #endif
17501 else
17502 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
17504 if (redisplay_menu_p)
17505 display_menu_bar (w);
17507 #ifdef HAVE_WINDOW_SYSTEM
17508 if (FRAME_WINDOW_P (f))
17510 #if defined (USE_GTK) || defined (HAVE_NS)
17511 if (FRAME_EXTERNAL_TOOL_BAR (f))
17512 redisplay_tool_bar (f);
17513 #else
17514 if (WINDOWP (f->tool_bar_window)
17515 && (FRAME_TOOL_BAR_LINES (f) > 0
17516 || !NILP (Vauto_resize_tool_bars))
17517 && redisplay_tool_bar (f))
17518 ignore_mouse_drag_p = true;
17519 #endif
17521 x_consider_frame_title (w->frame);
17522 #endif
17525 #ifdef HAVE_WINDOW_SYSTEM
17526 if (FRAME_WINDOW_P (f)
17527 && update_window_fringes (w, (just_this_one_p
17528 || (!used_current_matrix_p && !overlay_arrow_seen)
17529 || w->pseudo_window_p)))
17531 update_begin (f);
17532 block_input ();
17533 if (draw_window_fringes (w, true))
17535 if (WINDOW_RIGHT_DIVIDER_WIDTH (w))
17536 x_draw_right_divider (w);
17537 else
17538 x_draw_vertical_border (w);
17540 unblock_input ();
17541 update_end (f);
17544 if (WINDOW_BOTTOM_DIVIDER_WIDTH (w))
17545 x_draw_bottom_divider (w);
17546 #endif /* HAVE_WINDOW_SYSTEM */
17548 /* We go to this label, with fonts_changed set, if it is
17549 necessary to try again using larger glyph matrices.
17550 We have to redeem the scroll bar even in this case,
17551 because the loop in redisplay_internal expects that. */
17552 need_larger_matrices:
17554 finish_scroll_bars:
17556 if (WINDOW_HAS_VERTICAL_SCROLL_BAR (w) || WINDOW_HAS_HORIZONTAL_SCROLL_BAR (w))
17558 if (WINDOW_HAS_VERTICAL_SCROLL_BAR (w))
17559 /* Set the thumb's position and size. */
17560 set_vertical_scroll_bar (w);
17562 if (WINDOW_HAS_HORIZONTAL_SCROLL_BAR (w))
17563 /* Set the thumb's position and size. */
17564 set_horizontal_scroll_bar (w);
17566 /* Note that we actually used the scroll bar attached to this
17567 window, so it shouldn't be deleted at the end of redisplay. */
17568 if (FRAME_TERMINAL (f)->redeem_scroll_bar_hook)
17569 (*FRAME_TERMINAL (f)->redeem_scroll_bar_hook) (w);
17572 /* Restore current_buffer and value of point in it. The window
17573 update may have changed the buffer, so first make sure `opoint'
17574 is still valid (Bug#6177). */
17575 if (CHARPOS (opoint) < BEGV)
17576 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
17577 else if (CHARPOS (opoint) > ZV)
17578 TEMP_SET_PT_BOTH (Z, Z_BYTE);
17579 else
17580 TEMP_SET_PT_BOTH (CHARPOS (opoint), BYTEPOS (opoint));
17582 set_buffer_internal_1 (old);
17583 /* Avoid an abort in TEMP_SET_PT_BOTH if the buffer has become
17584 shorter. This can be caused by log truncation in *Messages*. */
17585 if (CHARPOS (lpoint) <= ZV)
17586 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
17588 unbind_to (count, Qnil);
17592 /* Build the complete desired matrix of WINDOW with a window start
17593 buffer position POS.
17595 Value is 1 if successful. It is zero if fonts were loaded during
17596 redisplay which makes re-adjusting glyph matrices necessary, and -1
17597 if point would appear in the scroll margins.
17598 (We check the former only if TRY_WINDOW_IGNORE_FONTS_CHANGE is
17599 unset in FLAGS, and the latter only if TRY_WINDOW_CHECK_MARGINS is
17600 set in FLAGS.) */
17603 try_window (Lisp_Object window, struct text_pos pos, int flags)
17605 struct window *w = XWINDOW (window);
17606 struct it it;
17607 struct glyph_row *last_text_row = NULL;
17608 struct frame *f = XFRAME (w->frame);
17609 int cursor_vpos = w->cursor.vpos;
17611 /* Make POS the new window start. */
17612 set_marker_both (w->start, Qnil, CHARPOS (pos), BYTEPOS (pos));
17614 /* Mark cursor position as unknown. No overlay arrow seen. */
17615 w->cursor.vpos = -1;
17616 overlay_arrow_seen = false;
17618 /* Initialize iterator and info to start at POS. */
17619 start_display (&it, w, pos);
17620 it.glyph_row->reversed_p = false;
17622 /* Display all lines of W. */
17623 while (it.current_y < it.last_visible_y)
17625 if (display_line (&it, cursor_vpos))
17626 last_text_row = it.glyph_row - 1;
17627 if (f->fonts_changed && !(flags & TRY_WINDOW_IGNORE_FONTS_CHANGE))
17628 return 0;
17631 /* Save the character position of 'it' before we call
17632 'start_display' again. */
17633 ptrdiff_t it_charpos = IT_CHARPOS (it);
17635 /* Don't let the cursor end in the scroll margins. */
17636 if ((flags & TRY_WINDOW_CHECK_MARGINS)
17637 && !MINI_WINDOW_P (w))
17639 int this_scroll_margin = window_scroll_margin (w, MARGIN_IN_PIXELS);
17640 start_display (&it, w, pos);
17642 if ((w->cursor.y >= 0 /* not vscrolled */
17643 && w->cursor.y < this_scroll_margin
17644 && CHARPOS (pos) > BEGV
17645 && it_charpos < ZV)
17646 /* rms: considering make_cursor_line_fully_visible_p here
17647 seems to give wrong results. We don't want to recenter
17648 when the last line is partly visible, we want to allow
17649 that case to be handled in the usual way. */
17650 || w->cursor.y > (it.last_visible_y - partial_line_height (&it)
17651 - this_scroll_margin - 1))
17653 w->cursor.vpos = -1;
17654 clear_glyph_matrix (w->desired_matrix);
17655 return -1;
17659 /* If bottom moved off end of frame, change mode line percentage. */
17660 if (w->window_end_pos <= 0 && Z != it_charpos)
17661 w->update_mode_line = true;
17663 /* Set window_end_pos to the offset of the last character displayed
17664 on the window from the end of current_buffer. Set
17665 window_end_vpos to its row number. */
17666 if (last_text_row)
17668 eassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_text_row));
17669 adjust_window_ends (w, last_text_row, false);
17670 eassert
17671 (MATRIX_ROW_DISPLAYS_TEXT_P (MATRIX_ROW (w->desired_matrix,
17672 w->window_end_vpos)));
17674 else
17676 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
17677 w->window_end_pos = Z - ZV;
17678 w->window_end_vpos = 0;
17681 /* But that is not valid info until redisplay finishes. */
17682 w->window_end_valid = false;
17683 return 1;
17688 /************************************************************************
17689 Window redisplay reusing current matrix when buffer has not changed
17690 ************************************************************************/
17692 /* Try redisplay of window W showing an unchanged buffer with a
17693 different window start than the last time it was displayed by
17694 reusing its current matrix. Value is true if successful.
17695 W->start is the new window start. */
17697 static bool
17698 try_window_reusing_current_matrix (struct window *w)
17700 struct frame *f = XFRAME (w->frame);
17701 struct glyph_row *bottom_row;
17702 struct it it;
17703 struct run run;
17704 struct text_pos start, new_start;
17705 int nrows_scrolled, i;
17706 struct glyph_row *last_text_row;
17707 struct glyph_row *last_reused_text_row;
17708 struct glyph_row *start_row;
17709 int start_vpos, min_y, max_y;
17711 #ifdef GLYPH_DEBUG
17712 if (inhibit_try_window_reusing)
17713 return false;
17714 #endif
17716 if (/* This function doesn't handle terminal frames. */
17717 !FRAME_WINDOW_P (f)
17718 /* Don't try to reuse the display if windows have been split
17719 or such. */
17720 || windows_or_buffers_changed
17721 || f->cursor_type_changed
17722 /* This function cannot handle buffers where the overlay arrow
17723 is shown on the fringes, because if the arrow position
17724 changes, we cannot just reuse the current matrix. */
17725 || overlay_arrow_in_current_buffer_p ())
17726 return false;
17728 /* Can't do this if showing trailing whitespace. */
17729 if (!NILP (Vshow_trailing_whitespace))
17730 return false;
17732 /* If top-line visibility has changed, give up. */
17733 if (window_wants_header_line (w)
17734 != MATRIX_HEADER_LINE_ROW (w->current_matrix)->mode_line_p)
17735 return false;
17737 /* Give up if old or new display is scrolled vertically. We could
17738 make this function handle this, but right now it doesn't. */
17739 start_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
17740 if (w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row))
17741 return false;
17743 /* Clear the desired matrix for the display below. */
17744 clear_glyph_matrix (w->desired_matrix);
17746 /* Give up if line numbers are being displayed, because reusing the
17747 current matrix might use the wrong width for line-number
17748 display. */
17749 if (!NILP (Vdisplay_line_numbers))
17750 return false;
17752 /* The variable new_start now holds the new window start. The old
17753 start `start' can be determined from the current matrix. */
17754 SET_TEXT_POS_FROM_MARKER (new_start, w->start);
17755 start = start_row->minpos;
17756 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
17758 if (CHARPOS (new_start) <= CHARPOS (start))
17760 /* Don't use this method if the display starts with an ellipsis
17761 displayed for invisible text. It's not easy to handle that case
17762 below, and it's certainly not worth the effort since this is
17763 not a frequent case. */
17764 if (in_ellipses_for_invisible_text_p (&start_row->start, w))
17765 return false;
17767 IF_DEBUG (debug_method_add (w, "twu1"));
17769 /* Display up to a row that can be reused. The variable
17770 last_text_row is set to the last row displayed that displays
17771 text. Note that it.vpos == 0 if or if not there is a
17772 header-line; it's not the same as the MATRIX_ROW_VPOS! */
17773 start_display (&it, w, new_start);
17774 w->cursor.vpos = -1;
17775 last_text_row = last_reused_text_row = NULL;
17777 while (it.current_y < it.last_visible_y && !f->fonts_changed)
17779 /* If we have reached into the characters in the START row,
17780 that means the line boundaries have changed. So we
17781 can't start copying with the row START. Maybe it will
17782 work to start copying with the following row. */
17783 while (IT_CHARPOS (it) > CHARPOS (start))
17785 /* Advance to the next row as the "start". */
17786 start_row++;
17787 start = start_row->minpos;
17788 /* If there are no more rows to try, or just one, give up. */
17789 if (start_row == MATRIX_MODE_LINE_ROW (w->current_matrix) - 1
17790 || w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row)
17791 || CHARPOS (start) == ZV)
17793 clear_glyph_matrix (w->desired_matrix);
17794 return false;
17797 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
17799 /* If we have reached alignment, we can copy the rest of the
17800 rows. */
17801 if (IT_CHARPOS (it) == CHARPOS (start)
17802 /* Don't accept "alignment" inside a display vector,
17803 since start_row could have started in the middle of
17804 that same display vector (thus their character
17805 positions match), and we have no way of telling if
17806 that is the case. */
17807 && it.current.dpvec_index < 0)
17808 break;
17810 it.glyph_row->reversed_p = false;
17811 if (display_line (&it, -1))
17812 last_text_row = it.glyph_row - 1;
17816 /* A value of current_y < last_visible_y means that we stopped
17817 at the previous window start, which in turn means that we
17818 have at least one reusable row. */
17819 if (it.current_y < it.last_visible_y)
17821 struct glyph_row *row;
17823 /* IT.vpos always starts from 0; it counts text lines. */
17824 nrows_scrolled = it.vpos - (start_row - MATRIX_FIRST_TEXT_ROW (w->current_matrix));
17826 /* Find PT if not already found in the lines displayed. */
17827 if (w->cursor.vpos < 0)
17829 int dy = it.current_y - start_row->y;
17831 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
17832 row = row_containing_pos (w, PT, row, NULL, dy);
17833 if (row)
17834 set_cursor_from_row (w, row, w->current_matrix, 0, 0,
17835 dy, nrows_scrolled);
17836 else
17838 clear_glyph_matrix (w->desired_matrix);
17839 return false;
17843 /* Scroll the display. Do it before the current matrix is
17844 changed. The problem here is that update has not yet
17845 run, i.e. part of the current matrix is not up to date.
17846 scroll_run_hook will clear the cursor, and use the
17847 current matrix to get the height of the row the cursor is
17848 in. */
17849 run.current_y = start_row->y;
17850 run.desired_y = it.current_y;
17851 run.height = it.last_visible_y - it.current_y;
17853 if (run.height > 0 && run.current_y != run.desired_y)
17855 update_begin (f);
17856 FRAME_RIF (f)->update_window_begin_hook (w);
17857 FRAME_RIF (f)->clear_window_mouse_face (w);
17858 FRAME_RIF (f)->scroll_run_hook (w, &run);
17859 FRAME_RIF (f)->update_window_end_hook (w, false, false);
17860 update_end (f);
17863 /* Shift current matrix down by nrows_scrolled lines. */
17864 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
17865 rotate_matrix (w->current_matrix,
17866 start_vpos,
17867 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
17868 nrows_scrolled);
17870 /* Disable lines that must be updated. */
17871 for (i = 0; i < nrows_scrolled; ++i)
17872 (start_row + i)->enabled_p = false;
17874 /* Re-compute Y positions. */
17875 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
17876 max_y = it.last_visible_y;
17877 for (row = start_row + nrows_scrolled;
17878 row < bottom_row;
17879 ++row)
17881 row->y = it.current_y;
17882 row->visible_height = row->height;
17884 if (row->y < min_y)
17885 row->visible_height -= min_y - row->y;
17886 if (row->y + row->height > max_y)
17887 row->visible_height -= row->y + row->height - max_y;
17888 if (row->fringe_bitmap_periodic_p)
17889 row->redraw_fringe_bitmaps_p = true;
17891 it.current_y += row->height;
17893 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
17894 last_reused_text_row = row;
17895 if (MATRIX_ROW_BOTTOM_Y (row) >= it.last_visible_y)
17896 break;
17899 /* Disable lines in the current matrix which are now
17900 below the window. */
17901 for (++row; row < bottom_row; ++row)
17902 row->enabled_p = row->mode_line_p = false;
17905 /* Update window_end_pos etc.; last_reused_text_row is the last
17906 reused row from the current matrix containing text, if any.
17907 The value of last_text_row is the last displayed line
17908 containing text. */
17909 if (last_reused_text_row)
17910 adjust_window_ends (w, last_reused_text_row, true);
17911 else if (last_text_row)
17912 adjust_window_ends (w, last_text_row, false);
17913 else
17915 /* This window must be completely empty. */
17916 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
17917 w->window_end_pos = Z - ZV;
17918 w->window_end_vpos = 0;
17920 w->window_end_valid = false;
17922 /* Update hint: don't try scrolling again in update_window. */
17923 w->desired_matrix->no_scrolling_p = true;
17925 #ifdef GLYPH_DEBUG
17926 debug_method_add (w, "try_window_reusing_current_matrix 1");
17927 #endif
17928 return true;
17930 else if (CHARPOS (new_start) > CHARPOS (start))
17932 struct glyph_row *pt_row, *row;
17933 struct glyph_row *first_reusable_row;
17934 struct glyph_row *first_row_to_display;
17935 int dy;
17936 int yb = window_text_bottom_y (w);
17938 /* Find the row starting at new_start, if there is one. Don't
17939 reuse a partially visible line at the end. */
17940 first_reusable_row = start_row;
17941 while (first_reusable_row->enabled_p
17942 && MATRIX_ROW_BOTTOM_Y (first_reusable_row) < yb
17943 && (MATRIX_ROW_START_CHARPOS (first_reusable_row)
17944 < CHARPOS (new_start)))
17945 ++first_reusable_row;
17947 /* Give up if there is no row to reuse. */
17948 if (MATRIX_ROW_BOTTOM_Y (first_reusable_row) >= yb
17949 || !first_reusable_row->enabled_p
17950 || (MATRIX_ROW_START_CHARPOS (first_reusable_row)
17951 != CHARPOS (new_start)))
17952 return false;
17954 /* We can reuse fully visible rows beginning with
17955 first_reusable_row to the end of the window. Set
17956 first_row_to_display to the first row that cannot be reused.
17957 Set pt_row to the row containing point, if there is any. */
17958 pt_row = NULL;
17959 for (first_row_to_display = first_reusable_row;
17960 MATRIX_ROW_BOTTOM_Y (first_row_to_display) < yb;
17961 ++first_row_to_display)
17963 if (PT >= MATRIX_ROW_START_CHARPOS (first_row_to_display)
17964 && (PT < MATRIX_ROW_END_CHARPOS (first_row_to_display)
17965 || (PT == MATRIX_ROW_END_CHARPOS (first_row_to_display)
17966 && first_row_to_display->ends_at_zv_p
17967 && pt_row == NULL)))
17968 pt_row = first_row_to_display;
17971 /* Start displaying at the start of first_row_to_display. */
17972 eassert (first_row_to_display->y < yb);
17973 init_to_row_start (&it, w, first_row_to_display);
17975 nrows_scrolled = (MATRIX_ROW_VPOS (first_reusable_row, w->current_matrix)
17976 - start_vpos);
17977 it.vpos = (MATRIX_ROW_VPOS (first_row_to_display, w->current_matrix)
17978 - nrows_scrolled);
17979 it.current_y = (first_row_to_display->y - first_reusable_row->y
17980 + WINDOW_HEADER_LINE_HEIGHT (w));
17982 /* Display lines beginning with first_row_to_display in the
17983 desired matrix. Set last_text_row to the last row displayed
17984 that displays text. */
17985 it.glyph_row = MATRIX_ROW (w->desired_matrix, it.vpos);
17986 if (pt_row == NULL)
17987 w->cursor.vpos = -1;
17988 last_text_row = NULL;
17989 while (it.current_y < it.last_visible_y && !f->fonts_changed)
17990 if (display_line (&it, w->cursor.vpos))
17991 last_text_row = it.glyph_row - 1;
17993 /* If point is in a reused row, adjust y and vpos of the cursor
17994 position. */
17995 if (pt_row)
17997 w->cursor.vpos -= nrows_scrolled;
17998 w->cursor.y -= first_reusable_row->y - start_row->y;
18001 /* Give up if point isn't in a row displayed or reused. (This
18002 also handles the case where w->cursor.vpos < nrows_scrolled
18003 after the calls to display_line, which can happen with scroll
18004 margins. See bug#1295.) */
18005 if (w->cursor.vpos < 0)
18007 clear_glyph_matrix (w->desired_matrix);
18008 return false;
18011 /* Scroll the display. */
18012 run.current_y = first_reusable_row->y;
18013 run.desired_y = WINDOW_HEADER_LINE_HEIGHT (w);
18014 run.height = it.last_visible_y - run.current_y;
18015 dy = run.current_y - run.desired_y;
18017 if (run.height)
18019 update_begin (f);
18020 FRAME_RIF (f)->update_window_begin_hook (w);
18021 FRAME_RIF (f)->clear_window_mouse_face (w);
18022 FRAME_RIF (f)->scroll_run_hook (w, &run);
18023 FRAME_RIF (f)->update_window_end_hook (w, false, false);
18024 update_end (f);
18027 /* Adjust Y positions of reused rows. */
18028 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
18029 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
18030 max_y = it.last_visible_y;
18031 for (row = first_reusable_row; row < first_row_to_display; ++row)
18033 row->y -= dy;
18034 row->visible_height = row->height;
18035 if (row->y < min_y)
18036 row->visible_height -= min_y - row->y;
18037 if (row->y + row->height > max_y)
18038 row->visible_height -= row->y + row->height - max_y;
18039 if (row->fringe_bitmap_periodic_p)
18040 row->redraw_fringe_bitmaps_p = true;
18043 /* Scroll the current matrix. */
18044 eassert (nrows_scrolled > 0);
18045 rotate_matrix (w->current_matrix,
18046 start_vpos,
18047 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
18048 -nrows_scrolled);
18050 /* Disable rows not reused. */
18051 for (row -= nrows_scrolled; row < bottom_row; ++row)
18052 row->enabled_p = false;
18054 /* Point may have moved to a different line, so we cannot assume that
18055 the previous cursor position is valid; locate the correct row. */
18056 if (pt_row)
18058 for (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
18059 row < bottom_row
18060 && PT >= MATRIX_ROW_END_CHARPOS (row)
18061 && !row->ends_at_zv_p;
18062 row++)
18064 w->cursor.vpos++;
18065 w->cursor.y = row->y;
18067 if (row < bottom_row)
18069 /* Can't simply scan the row for point with
18070 bidi-reordered glyph rows. Let set_cursor_from_row
18071 figure out where to put the cursor, and if it fails,
18072 give up. */
18073 if (!NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering)))
18075 if (!set_cursor_from_row (w, row, w->current_matrix,
18076 0, 0, 0, 0))
18078 clear_glyph_matrix (w->desired_matrix);
18079 return false;
18082 else
18084 struct glyph *glyph = row->glyphs[TEXT_AREA] + w->cursor.hpos;
18085 struct glyph *end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
18087 for (; glyph < end
18088 && (!BUFFERP (glyph->object)
18089 || glyph->charpos < PT);
18090 glyph++)
18092 w->cursor.hpos++;
18093 w->cursor.x += glyph->pixel_width;
18099 /* Adjust window end. A null value of last_text_row means that
18100 the window end is in reused rows which in turn means that
18101 only its vpos can have changed. */
18102 if (last_text_row)
18103 adjust_window_ends (w, last_text_row, false);
18104 else
18105 w->window_end_vpos -= nrows_scrolled;
18107 w->window_end_valid = false;
18108 w->desired_matrix->no_scrolling_p = true;
18110 #ifdef GLYPH_DEBUG
18111 debug_method_add (w, "try_window_reusing_current_matrix 2");
18112 #endif
18113 return true;
18116 return false;
18121 /************************************************************************
18122 Window redisplay reusing current matrix when buffer has changed
18123 ************************************************************************/
18125 static struct glyph_row *find_last_unchanged_at_beg_row (struct window *);
18126 static struct glyph_row *find_first_unchanged_at_end_row (struct window *,
18127 ptrdiff_t *, ptrdiff_t *);
18128 static struct glyph_row *
18129 find_last_row_displaying_text (struct glyph_matrix *, struct it *,
18130 struct glyph_row *);
18133 /* Return the last row in MATRIX displaying text. If row START is
18134 non-null, start searching with that row. IT gives the dimensions
18135 of the display. Value is null if matrix is empty; otherwise it is
18136 a pointer to the row found. */
18138 static struct glyph_row *
18139 find_last_row_displaying_text (struct glyph_matrix *matrix, struct it *it,
18140 struct glyph_row *start)
18142 struct glyph_row *row, *row_found;
18144 /* Set row_found to the last row in IT->w's current matrix
18145 displaying text. The loop looks funny but think of partially
18146 visible lines. */
18147 row_found = NULL;
18148 row = start ? start : MATRIX_FIRST_TEXT_ROW (matrix);
18149 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
18151 eassert (row->enabled_p);
18152 row_found = row;
18153 if (MATRIX_ROW_BOTTOM_Y (row) >= it->last_visible_y)
18154 break;
18155 ++row;
18158 return row_found;
18162 /* Return the last row in the current matrix of W that is not affected
18163 by changes at the start of current_buffer that occurred since W's
18164 current matrix was built. Value is null if no such row exists.
18166 BEG_UNCHANGED us the number of characters unchanged at the start of
18167 current_buffer. BEG + BEG_UNCHANGED is the buffer position of the
18168 first changed character in current_buffer. Characters at positions <
18169 BEG + BEG_UNCHANGED are at the same buffer positions as they were
18170 when the current matrix was built. */
18172 static struct glyph_row *
18173 find_last_unchanged_at_beg_row (struct window *w)
18175 ptrdiff_t first_changed_pos = BEG + BEG_UNCHANGED;
18176 struct glyph_row *row;
18177 struct glyph_row *row_found = NULL;
18178 int yb = window_text_bottom_y (w);
18180 /* Find the last row displaying unchanged text. */
18181 for (row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
18182 MATRIX_ROW_DISPLAYS_TEXT_P (row)
18183 && MATRIX_ROW_START_CHARPOS (row) < first_changed_pos;
18184 ++row)
18186 if (/* If row ends before first_changed_pos, it is unchanged,
18187 except in some case. */
18188 MATRIX_ROW_END_CHARPOS (row) <= first_changed_pos
18189 /* When row ends in ZV and we write at ZV it is not
18190 unchanged. */
18191 && !row->ends_at_zv_p
18192 /* When first_changed_pos is the end of a continued line,
18193 row is not unchanged because it may be no longer
18194 continued. */
18195 && !(MATRIX_ROW_END_CHARPOS (row) == first_changed_pos
18196 && (row->continued_p
18197 || row->exact_window_width_line_p))
18198 /* If ROW->end is beyond ZV, then ROW->end is outdated and
18199 needs to be recomputed, so don't consider this row as
18200 unchanged. This happens when the last line was
18201 bidi-reordered and was killed immediately before this
18202 redisplay cycle. In that case, ROW->end stores the
18203 buffer position of the first visual-order character of
18204 the killed text, which is now beyond ZV. */
18205 && CHARPOS (row->end.pos) <= ZV)
18206 row_found = row;
18208 /* Stop if last visible row. */
18209 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
18210 break;
18213 return row_found;
18217 /* Find the first glyph row in the current matrix of W that is not
18218 affected by changes at the end of current_buffer since the
18219 time W's current matrix was built.
18221 Return in *DELTA the number of chars by which buffer positions in
18222 unchanged text at the end of current_buffer must be adjusted.
18224 Return in *DELTA_BYTES the corresponding number of bytes.
18226 Value is null if no such row exists, i.e. all rows are affected by
18227 changes. */
18229 static struct glyph_row *
18230 find_first_unchanged_at_end_row (struct window *w,
18231 ptrdiff_t *delta, ptrdiff_t *delta_bytes)
18233 struct glyph_row *row;
18234 struct glyph_row *row_found = NULL;
18236 *delta = *delta_bytes = 0;
18238 /* Display must not have been paused, otherwise the current matrix
18239 is not up to date. */
18240 eassert (w->window_end_valid);
18242 /* A value of window_end_pos >= END_UNCHANGED means that the window
18243 end is in the range of changed text. If so, there is no
18244 unchanged row at the end of W's current matrix. */
18245 if (w->window_end_pos >= END_UNCHANGED)
18246 return NULL;
18248 /* Set row to the last row in W's current matrix displaying text. */
18249 row = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
18251 /* If matrix is entirely empty, no unchanged row exists. */
18252 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
18254 /* The value of row is the last glyph row in the matrix having a
18255 meaningful buffer position in it. The end position of row
18256 corresponds to window_end_pos. This allows us to translate
18257 buffer positions in the current matrix to current buffer
18258 positions for characters not in changed text. */
18259 ptrdiff_t Z_old =
18260 MATRIX_ROW_END_CHARPOS (row) + w->window_end_pos;
18261 ptrdiff_t Z_BYTE_old =
18262 MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
18263 ptrdiff_t last_unchanged_pos, last_unchanged_pos_old;
18264 struct glyph_row *first_text_row
18265 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
18267 *delta = Z - Z_old;
18268 *delta_bytes = Z_BYTE - Z_BYTE_old;
18270 /* Set last_unchanged_pos to the buffer position of the last
18271 character in the buffer that has not been changed. Z is the
18272 index + 1 of the last character in current_buffer, i.e. by
18273 subtracting END_UNCHANGED we get the index of the last
18274 unchanged character, and we have to add BEG to get its buffer
18275 position. */
18276 last_unchanged_pos = Z - END_UNCHANGED + BEG;
18277 last_unchanged_pos_old = last_unchanged_pos - *delta;
18279 /* Search backward from ROW for a row displaying a line that
18280 starts at a minimum position >= last_unchanged_pos_old. */
18281 for (; row > first_text_row; --row)
18283 /* This used to abort, but it can happen.
18284 It is ok to just stop the search instead here. KFS. */
18285 if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
18286 break;
18288 if (MATRIX_ROW_START_CHARPOS (row) >= last_unchanged_pos_old)
18289 row_found = row;
18293 eassert (!row_found || MATRIX_ROW_DISPLAYS_TEXT_P (row_found));
18295 return row_found;
18299 /* Make sure that glyph rows in the current matrix of window W
18300 reference the same glyph memory as corresponding rows in the
18301 frame's frame matrix. This function is called after scrolling W's
18302 current matrix on a terminal frame in try_window_id and
18303 try_window_reusing_current_matrix. */
18305 static void
18306 sync_frame_with_window_matrix_rows (struct window *w)
18308 struct frame *f = XFRAME (w->frame);
18309 struct glyph_row *window_row, *window_row_end, *frame_row;
18311 /* Preconditions: W must be a leaf window and full-width. Its frame
18312 must have a frame matrix. */
18313 eassert (BUFFERP (w->contents));
18314 eassert (WINDOW_FULL_WIDTH_P (w));
18315 eassert (!FRAME_WINDOW_P (f));
18317 /* If W is a full-width window, glyph pointers in W's current matrix
18318 have, by definition, to be the same as glyph pointers in the
18319 corresponding frame matrix. Note that frame matrices have no
18320 marginal areas (see build_frame_matrix). */
18321 window_row = w->current_matrix->rows;
18322 window_row_end = window_row + w->current_matrix->nrows;
18323 frame_row = f->current_matrix->rows + WINDOW_TOP_EDGE_LINE (w);
18324 while (window_row < window_row_end)
18326 struct glyph *start = window_row->glyphs[LEFT_MARGIN_AREA];
18327 struct glyph *end = window_row->glyphs[LAST_AREA];
18329 frame_row->glyphs[LEFT_MARGIN_AREA] = start;
18330 frame_row->glyphs[TEXT_AREA] = start;
18331 frame_row->glyphs[RIGHT_MARGIN_AREA] = end;
18332 frame_row->glyphs[LAST_AREA] = end;
18334 /* Disable frame rows whose corresponding window rows have
18335 been disabled in try_window_id. */
18336 if (!window_row->enabled_p)
18337 frame_row->enabled_p = false;
18339 ++window_row, ++frame_row;
18344 /* Find the glyph row in window W containing CHARPOS. Consider all
18345 rows between START and END (not inclusive). END null means search
18346 all rows to the end of the display area of W. Value is the row
18347 containing CHARPOS or null. */
18349 struct glyph_row *
18350 row_containing_pos (struct window *w, ptrdiff_t charpos,
18351 struct glyph_row *start, struct glyph_row *end, int dy)
18353 struct glyph_row *row = start;
18354 struct glyph_row *best_row = NULL;
18355 ptrdiff_t mindif = BUF_ZV (XBUFFER (w->contents)) + 1;
18356 int last_y;
18358 /* If we happen to start on a header-line, skip that. */
18359 if (row->mode_line_p)
18360 ++row;
18362 if ((end && row >= end) || !row->enabled_p)
18363 return NULL;
18365 last_y = window_text_bottom_y (w) - dy;
18367 while (true)
18369 /* Give up if we have gone too far. */
18370 if ((end && row >= end) || !row->enabled_p)
18371 return NULL;
18372 /* This formerly returned if they were equal.
18373 I think that both quantities are of a "last plus one" type;
18374 if so, when they are equal, the row is within the screen. -- rms. */
18375 if (MATRIX_ROW_BOTTOM_Y (row) > last_y)
18376 return NULL;
18378 /* If it is in this row, return this row. */
18379 if (! (MATRIX_ROW_END_CHARPOS (row) < charpos
18380 || (MATRIX_ROW_END_CHARPOS (row) == charpos
18381 /* The end position of a row equals the start
18382 position of the next row. If CHARPOS is there, we
18383 would rather consider it displayed in the next
18384 line, except when this line ends in ZV. */
18385 && !row_for_charpos_p (row, charpos)))
18386 && charpos >= MATRIX_ROW_START_CHARPOS (row))
18388 struct glyph *g;
18390 if (NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering))
18391 || (!best_row && !row->continued_p))
18392 return row;
18393 /* In bidi-reordered rows, there could be several rows whose
18394 edges surround CHARPOS, all of these rows belonging to
18395 the same continued line. We need to find the row which
18396 fits CHARPOS the best. */
18397 for (g = row->glyphs[TEXT_AREA];
18398 g < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
18399 g++)
18401 if (!STRINGP (g->object))
18403 if (g->charpos > 0 && eabs (g->charpos - charpos) < mindif)
18405 mindif = eabs (g->charpos - charpos);
18406 best_row = row;
18407 /* Exact match always wins. */
18408 if (mindif == 0)
18409 return best_row;
18414 else if (best_row && !row->continued_p)
18415 return best_row;
18416 ++row;
18421 /* Try to redisplay window W by reusing its existing display. W's
18422 current matrix must be up to date when this function is called,
18423 i.e., window_end_valid must be true.
18425 Value is
18427 >= 1 if successful, i.e. display has been updated
18428 specifically:
18429 1 means the changes were in front of a newline that precedes
18430 the window start, and the whole current matrix was reused
18431 2 means the changes were after the last position displayed
18432 in the window, and the whole current matrix was reused
18433 3 means portions of the current matrix were reused, while
18434 some of the screen lines were redrawn
18435 -1 if redisplay with same window start is known not to succeed
18436 0 if otherwise unsuccessful
18438 The following steps are performed:
18440 1. Find the last row in the current matrix of W that is not
18441 affected by changes at the start of current_buffer. If no such row
18442 is found, give up.
18444 2. Find the first row in W's current matrix that is not affected by
18445 changes at the end of current_buffer. Maybe there is no such row.
18447 3. Display lines beginning with the row + 1 found in step 1 to the
18448 row found in step 2 or, if step 2 didn't find a row, to the end of
18449 the window.
18451 4. If cursor is not known to appear on the window, give up.
18453 5. If display stopped at the row found in step 2, scroll the
18454 display and current matrix as needed.
18456 6. Maybe display some lines at the end of W, if we must. This can
18457 happen under various circumstances, like a partially visible line
18458 becoming fully visible, or because newly displayed lines are displayed
18459 in smaller font sizes.
18461 7. Update W's window end information. */
18463 static int
18464 try_window_id (struct window *w)
18466 struct frame *f = XFRAME (w->frame);
18467 struct glyph_matrix *current_matrix = w->current_matrix;
18468 struct glyph_matrix *desired_matrix = w->desired_matrix;
18469 struct glyph_row *last_unchanged_at_beg_row;
18470 struct glyph_row *first_unchanged_at_end_row;
18471 struct glyph_row *row;
18472 struct glyph_row *bottom_row;
18473 int bottom_vpos;
18474 struct it it;
18475 ptrdiff_t delta = 0, delta_bytes = 0, stop_pos;
18476 int dvpos, dy;
18477 struct text_pos start_pos;
18478 struct run run;
18479 int first_unchanged_at_end_vpos = 0;
18480 struct glyph_row *last_text_row, *last_text_row_at_end;
18481 struct text_pos start;
18482 ptrdiff_t first_changed_charpos, last_changed_charpos;
18484 #ifdef GLYPH_DEBUG
18485 if (inhibit_try_window_id)
18486 return 0;
18487 #endif
18489 /* This is handy for debugging. */
18490 #if false
18491 #define GIVE_UP(X) \
18492 do { \
18493 TRACE ((stderr, "try_window_id give up %d\n", (X))); \
18494 return 0; \
18495 } while (false)
18496 #else
18497 #define GIVE_UP(X) return 0
18498 #endif
18500 SET_TEXT_POS_FROM_MARKER (start, w->start);
18502 /* Don't use this for mini-windows because these can show
18503 messages and mini-buffers, and we don't handle that here. */
18504 if (MINI_WINDOW_P (w))
18505 GIVE_UP (1);
18507 /* This flag is used to prevent redisplay optimizations. */
18508 if (windows_or_buffers_changed || f->cursor_type_changed)
18509 GIVE_UP (2);
18511 /* This function's optimizations cannot be used if overlays have
18512 changed in the buffer displayed by the window, so give up if they
18513 have. */
18514 if (w->last_overlay_modified != OVERLAY_MODIFF)
18515 GIVE_UP (200);
18517 /* Verify that narrowing has not changed.
18518 Also verify that we were not told to prevent redisplay optimizations.
18519 It would be nice to further
18520 reduce the number of cases where this prevents try_window_id. */
18521 if (current_buffer->clip_changed
18522 || current_buffer->prevent_redisplay_optimizations_p)
18523 GIVE_UP (3);
18525 /* Window must either use window-based redisplay or be full width. */
18526 if (!FRAME_WINDOW_P (f)
18527 && (!FRAME_LINE_INS_DEL_OK (f)
18528 || !WINDOW_FULL_WIDTH_P (w)))
18529 GIVE_UP (4);
18531 /* Give up if point is known NOT to appear in W. */
18532 if (PT < CHARPOS (start))
18533 GIVE_UP (5);
18535 /* Another way to prevent redisplay optimizations. */
18536 if (w->last_modified == 0)
18537 GIVE_UP (6);
18539 /* Verify that window is not hscrolled. */
18540 if (w->hscroll != 0)
18541 GIVE_UP (7);
18543 /* Verify that display wasn't paused. */
18544 if (!w->window_end_valid)
18545 GIVE_UP (8);
18547 /* Likewise if highlighting trailing whitespace. */
18548 if (!NILP (Vshow_trailing_whitespace))
18549 GIVE_UP (11);
18551 /* Can't use this if overlay arrow position and/or string have
18552 changed. */
18553 if (overlay_arrows_changed_p (false))
18554 GIVE_UP (12);
18556 /* When word-wrap is on, adding a space to the first word of a
18557 wrapped line can change the wrap position, altering the line
18558 above it. It might be worthwhile to handle this more
18559 intelligently, but for now just redisplay from scratch. */
18560 if (!NILP (BVAR (XBUFFER (w->contents), word_wrap)))
18561 GIVE_UP (21);
18563 /* Under bidi reordering, adding or deleting a character in the
18564 beginning of a paragraph, before the first strong directional
18565 character, can change the base direction of the paragraph (unless
18566 the buffer specifies a fixed paragraph direction), which will
18567 require redisplaying the whole paragraph. It might be worthwhile
18568 to find the paragraph limits and widen the range of redisplayed
18569 lines to that, but for now just give up this optimization and
18570 redisplay from scratch. */
18571 if (!NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering))
18572 && NILP (BVAR (XBUFFER (w->contents), bidi_paragraph_direction)))
18573 GIVE_UP (22);
18575 /* Give up if the buffer has line-spacing set, as Lisp-level changes
18576 to that variable require thorough redisplay. */
18577 if (!NILP (BVAR (XBUFFER (w->contents), extra_line_spacing)))
18578 GIVE_UP (23);
18580 /* Give up if display-line-numbers is in relative mode, or when the
18581 current line's number needs to be displayed in a distinct face. */
18582 if (EQ (Vdisplay_line_numbers, Qrelative)
18583 || EQ (Vdisplay_line_numbers, Qvisual)
18584 || (!NILP (Vdisplay_line_numbers)
18585 && NILP (Finternal_lisp_face_equal_p (Qline_number,
18586 Qline_number_current_line,
18587 w->frame))))
18588 GIVE_UP (24);
18590 /* Make sure beg_unchanged and end_unchanged are up to date. Do it
18591 only if buffer has really changed. The reason is that the gap is
18592 initially at Z for freshly visited files. The code below would
18593 set end_unchanged to 0 in that case. */
18594 if (MODIFF > SAVE_MODIFF
18595 /* This seems to happen sometimes after saving a buffer. */
18596 || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
18598 if (GPT - BEG < BEG_UNCHANGED)
18599 BEG_UNCHANGED = GPT - BEG;
18600 if (Z - GPT < END_UNCHANGED)
18601 END_UNCHANGED = Z - GPT;
18604 /* The position of the first and last character that has been changed. */
18605 first_changed_charpos = BEG + BEG_UNCHANGED;
18606 last_changed_charpos = Z - END_UNCHANGED;
18608 /* If window starts after a line end, and the last change is in
18609 front of that newline, then changes don't affect the display.
18610 This case happens with stealth-fontification. Note that although
18611 the display is unchanged, glyph positions in the matrix have to
18612 be adjusted, of course. */
18613 row = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
18614 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
18615 && ((last_changed_charpos < CHARPOS (start)
18616 && CHARPOS (start) == BEGV)
18617 || (last_changed_charpos < CHARPOS (start) - 1
18618 && FETCH_BYTE (BYTEPOS (start) - 1) == '\n')))
18620 ptrdiff_t Z_old, Z_delta, Z_BYTE_old, Z_delta_bytes;
18621 struct glyph_row *r0;
18623 /* Compute how many chars/bytes have been added to or removed
18624 from the buffer. */
18625 Z_old = MATRIX_ROW_END_CHARPOS (row) + w->window_end_pos;
18626 Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
18627 Z_delta = Z - Z_old;
18628 Z_delta_bytes = Z_BYTE - Z_BYTE_old;
18630 /* Give up if PT is not in the window. Note that it already has
18631 been checked at the start of try_window_id that PT is not in
18632 front of the window start. */
18633 if (PT >= MATRIX_ROW_END_CHARPOS (row) + Z_delta)
18634 GIVE_UP (13);
18636 /* If window start is unchanged, we can reuse the whole matrix
18637 as is, after adjusting glyph positions. No need to compute
18638 the window end again, since its offset from Z hasn't changed. */
18639 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
18640 if (CHARPOS (start) == MATRIX_ROW_START_CHARPOS (r0) + Z_delta
18641 && BYTEPOS (start) == MATRIX_ROW_START_BYTEPOS (r0) + Z_delta_bytes
18642 /* PT must not be in a partially visible line. */
18643 && !(PT >= MATRIX_ROW_START_CHARPOS (row) + Z_delta
18644 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
18646 /* Adjust positions in the glyph matrix. */
18647 if (Z_delta || Z_delta_bytes)
18649 struct glyph_row *r1
18650 = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
18651 increment_matrix_positions (w->current_matrix,
18652 MATRIX_ROW_VPOS (r0, current_matrix),
18653 MATRIX_ROW_VPOS (r1, current_matrix),
18654 Z_delta, Z_delta_bytes);
18657 /* Set the cursor. */
18658 row = row_containing_pos (w, PT, r0, NULL, 0);
18659 if (row)
18660 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
18661 return 1;
18665 /* Handle the case that changes are all below what is displayed in
18666 the window, and that PT is in the window. This shortcut cannot
18667 be taken if ZV is visible in the window, and text has been added
18668 there that is visible in the window. */
18669 if (first_changed_charpos >= MATRIX_ROW_END_CHARPOS (row)
18670 /* ZV is not visible in the window, or there are no
18671 changes at ZV, actually. */
18672 && (current_matrix->zv > MATRIX_ROW_END_CHARPOS (row)
18673 || first_changed_charpos == last_changed_charpos))
18675 struct glyph_row *r0;
18677 /* Give up if PT is not in the window. Note that it already has
18678 been checked at the start of try_window_id that PT is not in
18679 front of the window start. */
18680 if (PT >= MATRIX_ROW_END_CHARPOS (row))
18681 GIVE_UP (14);
18683 /* If window start is unchanged, we can reuse the whole matrix
18684 as is, without changing glyph positions since no text has
18685 been added/removed in front of the window end. */
18686 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
18687 if (TEXT_POS_EQUAL_P (start, r0->minpos)
18688 /* PT must not be in a partially visible line. */
18689 && !(PT >= MATRIX_ROW_START_CHARPOS (row)
18690 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
18692 /* We have to compute the window end anew since text
18693 could have been added/removed after it. */
18694 w->window_end_pos = Z - MATRIX_ROW_END_CHARPOS (row);
18695 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
18697 /* Set the cursor. */
18698 row = row_containing_pos (w, PT, r0, NULL, 0);
18699 if (row)
18700 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
18701 return 2;
18705 /* Give up if window start is in the changed area.
18707 The condition used to read
18709 (BEG_UNCHANGED + END_UNCHANGED != Z - BEG && ...)
18711 but why that was tested escapes me at the moment. */
18712 if (CHARPOS (start) >= first_changed_charpos
18713 && CHARPOS (start) <= last_changed_charpos)
18714 GIVE_UP (15);
18716 /* Check that window start agrees with the start of the first glyph
18717 row in its current matrix. Check this after we know the window
18718 start is not in changed text, otherwise positions would not be
18719 comparable. */
18720 row = MATRIX_FIRST_TEXT_ROW (current_matrix);
18721 if (!TEXT_POS_EQUAL_P (start, row->minpos))
18722 GIVE_UP (16);
18724 /* Give up if the window ends in strings. Overlay strings
18725 at the end are difficult to handle, so don't try. */
18726 row = MATRIX_ROW (current_matrix, w->window_end_vpos);
18727 if (MATRIX_ROW_START_CHARPOS (row) == MATRIX_ROW_END_CHARPOS (row))
18728 GIVE_UP (20);
18730 /* Compute the position at which we have to start displaying new
18731 lines. Some of the lines at the top of the window might be
18732 reusable because they are not displaying changed text. Find the
18733 last row in W's current matrix not affected by changes at the
18734 start of current_buffer. Value is null if changes start in the
18735 first line of window. */
18736 last_unchanged_at_beg_row = find_last_unchanged_at_beg_row (w);
18737 if (last_unchanged_at_beg_row)
18739 /* Avoid starting to display in the middle of a character, a TAB
18740 for instance. This is easier than to set up the iterator
18741 exactly, and it's not a frequent case, so the additional
18742 effort wouldn't really pay off. */
18743 while ((MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row)
18744 || last_unchanged_at_beg_row->ends_in_newline_from_string_p)
18745 && last_unchanged_at_beg_row > w->current_matrix->rows)
18746 --last_unchanged_at_beg_row;
18748 if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row))
18749 GIVE_UP (17);
18751 if (! init_to_row_end (&it, w, last_unchanged_at_beg_row))
18752 GIVE_UP (18);
18753 start_pos = it.current.pos;
18755 /* Start displaying new lines in the desired matrix at the same
18756 vpos we would use in the current matrix, i.e. below
18757 last_unchanged_at_beg_row. */
18758 it.vpos = 1 + MATRIX_ROW_VPOS (last_unchanged_at_beg_row,
18759 current_matrix);
18760 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
18761 it.current_y = MATRIX_ROW_BOTTOM_Y (last_unchanged_at_beg_row);
18763 eassert (it.hpos == 0 && it.current_x == 0);
18765 else
18767 /* There are no reusable lines at the start of the window.
18768 Start displaying in the first text line. */
18769 start_display (&it, w, start);
18770 it.vpos = it.first_vpos;
18771 start_pos = it.current.pos;
18774 /* Find the first row that is not affected by changes at the end of
18775 the buffer. Value will be null if there is no unchanged row, in
18776 which case we must redisplay to the end of the window. delta
18777 will be set to the value by which buffer positions beginning with
18778 first_unchanged_at_end_row have to be adjusted due to text
18779 changes. */
18780 first_unchanged_at_end_row
18781 = find_first_unchanged_at_end_row (w, &delta, &delta_bytes);
18782 IF_DEBUG (debug_delta = delta);
18783 IF_DEBUG (debug_delta_bytes = delta_bytes);
18785 /* Set stop_pos to the buffer position up to which we will have to
18786 display new lines. If first_unchanged_at_end_row != NULL, this
18787 is the buffer position of the start of the line displayed in that
18788 row. For first_unchanged_at_end_row == NULL, use 0 to indicate
18789 that we don't stop at a buffer position. */
18790 stop_pos = 0;
18791 if (first_unchanged_at_end_row)
18793 eassert (last_unchanged_at_beg_row == NULL
18794 || first_unchanged_at_end_row >= last_unchanged_at_beg_row);
18796 /* If this is a continuation line, move forward to the next one
18797 that isn't. Changes in lines above affect this line.
18798 Caution: this may move first_unchanged_at_end_row to a row
18799 not displaying text. */
18800 while (MATRIX_ROW_CONTINUATION_LINE_P (first_unchanged_at_end_row)
18801 && MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
18802 && (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
18803 < it.last_visible_y))
18804 ++first_unchanged_at_end_row;
18806 if (!MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
18807 || (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
18808 >= it.last_visible_y))
18809 first_unchanged_at_end_row = NULL;
18810 else
18812 stop_pos = (MATRIX_ROW_START_CHARPOS (first_unchanged_at_end_row)
18813 + delta);
18814 first_unchanged_at_end_vpos
18815 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, current_matrix);
18816 eassert (stop_pos >= Z - END_UNCHANGED);
18819 else if (last_unchanged_at_beg_row == NULL)
18820 GIVE_UP (19);
18823 #ifdef GLYPH_DEBUG
18825 /* Either there is no unchanged row at the end, or the one we have
18826 now displays text. This is a necessary condition for the window
18827 end pos calculation at the end of this function. */
18828 eassert (first_unchanged_at_end_row == NULL
18829 || MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
18831 debug_last_unchanged_at_beg_vpos
18832 = (last_unchanged_at_beg_row
18833 ? MATRIX_ROW_VPOS (last_unchanged_at_beg_row, current_matrix)
18834 : -1);
18835 debug_first_unchanged_at_end_vpos = first_unchanged_at_end_vpos;
18837 #endif /* GLYPH_DEBUG */
18840 /* Display new lines. Set last_text_row to the last new line
18841 displayed which has text on it, i.e. might end up as being the
18842 line where the window_end_vpos is. */
18843 w->cursor.vpos = -1;
18844 last_text_row = NULL;
18845 overlay_arrow_seen = false;
18846 if (it.current_y < it.last_visible_y
18847 && !f->fonts_changed
18848 && (first_unchanged_at_end_row == NULL
18849 || IT_CHARPOS (it) < stop_pos))
18850 it.glyph_row->reversed_p = false;
18851 while (it.current_y < it.last_visible_y
18852 && !f->fonts_changed
18853 && (first_unchanged_at_end_row == NULL
18854 || IT_CHARPOS (it) < stop_pos))
18856 if (display_line (&it, -1))
18857 last_text_row = it.glyph_row - 1;
18860 if (f->fonts_changed)
18861 return -1;
18863 /* The redisplay iterations in display_line above could have
18864 triggered font-lock, which could have done something that
18865 invalidates IT->w window's end-point information, on which we
18866 rely below. E.g., one package, which will remain unnamed, used
18867 to install a font-lock-fontify-region-function that called
18868 bury-buffer, whose side effect is to switch the buffer displayed
18869 by IT->w, and that predictably resets IT->w's window_end_valid
18870 flag, which we already tested at the entry to this function.
18871 Amply punish such packages/modes by giving up on this
18872 optimization in those cases. */
18873 if (!w->window_end_valid)
18875 clear_glyph_matrix (w->desired_matrix);
18876 return -1;
18879 /* Compute differences in buffer positions, y-positions etc. for
18880 lines reused at the bottom of the window. Compute what we can
18881 scroll. */
18882 if (first_unchanged_at_end_row
18883 /* No lines reused because we displayed everything up to the
18884 bottom of the window. */
18885 && it.current_y < it.last_visible_y)
18887 dvpos = (it.vpos
18888 - MATRIX_ROW_VPOS (first_unchanged_at_end_row,
18889 current_matrix));
18890 dy = it.current_y - first_unchanged_at_end_row->y;
18891 run.current_y = first_unchanged_at_end_row->y;
18892 run.desired_y = run.current_y + dy;
18893 run.height = it.last_visible_y - max (run.current_y, run.desired_y);
18895 else
18897 delta = delta_bytes = dvpos = dy
18898 = run.current_y = run.desired_y = run.height = 0;
18899 first_unchanged_at_end_row = NULL;
18901 IF_DEBUG ((debug_dvpos = dvpos, debug_dy = dy));
18904 /* Find the cursor if not already found. We have to decide whether
18905 PT will appear on this window (it sometimes doesn't, but this is
18906 not a very frequent case.) This decision has to be made before
18907 the current matrix is altered. A value of cursor.vpos < 0 means
18908 that PT is either in one of the lines beginning at
18909 first_unchanged_at_end_row or below the window. Don't care for
18910 lines that might be displayed later at the window end; as
18911 mentioned, this is not a frequent case. */
18912 if (w->cursor.vpos < 0)
18914 /* Cursor in unchanged rows at the top? */
18915 if (PT < CHARPOS (start_pos)
18916 && last_unchanged_at_beg_row)
18918 row = row_containing_pos (w, PT,
18919 MATRIX_FIRST_TEXT_ROW (w->current_matrix),
18920 last_unchanged_at_beg_row + 1, 0);
18921 if (row)
18922 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
18925 /* Start from first_unchanged_at_end_row looking for PT. */
18926 else if (first_unchanged_at_end_row)
18928 row = row_containing_pos (w, PT - delta,
18929 first_unchanged_at_end_row, NULL, 0);
18930 if (row)
18931 set_cursor_from_row (w, row, w->current_matrix, delta,
18932 delta_bytes, dy, dvpos);
18935 /* Give up if cursor was not found. */
18936 if (w->cursor.vpos < 0)
18938 clear_glyph_matrix (w->desired_matrix);
18939 return -1;
18943 /* Don't let the cursor end in the scroll margins. */
18945 int this_scroll_margin = window_scroll_margin (w, MARGIN_IN_PIXELS);
18946 int cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
18948 if ((w->cursor.y < this_scroll_margin
18949 && CHARPOS (start) > BEGV)
18950 /* Old redisplay didn't take scroll margin into account at the bottom,
18951 but then global-hl-line-mode doesn't scroll. KFS 2004-06-14 */
18952 || (w->cursor.y + (make_cursor_line_fully_visible_p
18953 ? cursor_height + this_scroll_margin
18954 : 1)) > it.last_visible_y)
18956 w->cursor.vpos = -1;
18957 clear_glyph_matrix (w->desired_matrix);
18958 return -1;
18962 /* Scroll the display. Do it before changing the current matrix so
18963 that xterm.c doesn't get confused about where the cursor glyph is
18964 found. */
18965 if (dy && run.height)
18967 update_begin (f);
18969 if (FRAME_WINDOW_P (f))
18971 FRAME_RIF (f)->update_window_begin_hook (w);
18972 FRAME_RIF (f)->clear_window_mouse_face (w);
18973 FRAME_RIF (f)->scroll_run_hook (w, &run);
18974 FRAME_RIF (f)->update_window_end_hook (w, false, false);
18976 else
18978 /* Terminal frame. In this case, dvpos gives the number of
18979 lines to scroll by; dvpos < 0 means scroll up. */
18980 int from_vpos
18981 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, w->current_matrix);
18982 int from = WINDOW_TOP_EDGE_LINE (w) + from_vpos;
18983 int end = (WINDOW_TOP_EDGE_LINE (w)
18984 + window_wants_header_line (w)
18985 + window_internal_height (w));
18987 #if defined (HAVE_GPM) || defined (MSDOS)
18988 x_clear_window_mouse_face (w);
18989 #endif
18990 /* Perform the operation on the screen. */
18991 if (dvpos > 0)
18993 /* Scroll last_unchanged_at_beg_row to the end of the
18994 window down dvpos lines. */
18995 set_terminal_window (f, end);
18997 /* On dumb terminals delete dvpos lines at the end
18998 before inserting dvpos empty lines. */
18999 if (!FRAME_SCROLL_REGION_OK (f))
19000 ins_del_lines (f, end - dvpos, -dvpos);
19002 /* Insert dvpos empty lines in front of
19003 last_unchanged_at_beg_row. */
19004 ins_del_lines (f, from, dvpos);
19006 else if (dvpos < 0)
19008 /* Scroll up last_unchanged_at_beg_vpos to the end of
19009 the window to last_unchanged_at_beg_vpos - |dvpos|. */
19010 set_terminal_window (f, end);
19012 /* Delete dvpos lines in front of
19013 last_unchanged_at_beg_vpos. ins_del_lines will set
19014 the cursor to the given vpos and emit |dvpos| delete
19015 line sequences. */
19016 ins_del_lines (f, from + dvpos, dvpos);
19018 /* On a dumb terminal insert dvpos empty lines at the
19019 end. */
19020 if (!FRAME_SCROLL_REGION_OK (f))
19021 ins_del_lines (f, end + dvpos, -dvpos);
19024 set_terminal_window (f, 0);
19027 update_end (f);
19030 /* Shift reused rows of the current matrix to the right position.
19031 BOTTOM_ROW is the last + 1 row in the current matrix reserved for
19032 text. */
19033 bottom_row = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
19034 bottom_vpos = MATRIX_ROW_VPOS (bottom_row, current_matrix);
19035 if (dvpos < 0)
19037 rotate_matrix (current_matrix, first_unchanged_at_end_vpos + dvpos,
19038 bottom_vpos, dvpos);
19039 clear_glyph_matrix_rows (current_matrix, bottom_vpos + dvpos,
19040 bottom_vpos);
19042 else if (dvpos > 0)
19044 rotate_matrix (current_matrix, first_unchanged_at_end_vpos,
19045 bottom_vpos, dvpos);
19046 clear_glyph_matrix_rows (current_matrix, first_unchanged_at_end_vpos,
19047 first_unchanged_at_end_vpos + dvpos);
19050 /* For frame-based redisplay, make sure that current frame and window
19051 matrix are in sync with respect to glyph memory. */
19052 if (!FRAME_WINDOW_P (f))
19053 sync_frame_with_window_matrix_rows (w);
19055 /* Adjust buffer positions in reused rows. */
19056 if (delta || delta_bytes)
19057 increment_matrix_positions (current_matrix,
19058 first_unchanged_at_end_vpos + dvpos,
19059 bottom_vpos, delta, delta_bytes);
19061 /* Adjust Y positions. */
19062 if (dy)
19063 shift_glyph_matrix (w, current_matrix,
19064 first_unchanged_at_end_vpos + dvpos,
19065 bottom_vpos, dy);
19067 if (first_unchanged_at_end_row)
19069 first_unchanged_at_end_row += dvpos;
19070 if (first_unchanged_at_end_row->y >= it.last_visible_y
19071 || !MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row))
19072 first_unchanged_at_end_row = NULL;
19075 /* If scrolling up, there may be some lines to display at the end of
19076 the window. */
19077 last_text_row_at_end = NULL;
19078 if (dy < 0)
19080 /* Scrolling up can leave for example a partially visible line
19081 at the end of the window to be redisplayed. */
19082 /* Set last_row to the glyph row in the current matrix where the
19083 window end line is found. It has been moved up or down in
19084 the matrix by dvpos. */
19085 int last_vpos = w->window_end_vpos + dvpos;
19086 struct glyph_row *last_row = MATRIX_ROW (current_matrix, last_vpos);
19088 /* If last_row is the window end line, it should display text. */
19089 eassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_row));
19091 /* If window end line was partially visible before, begin
19092 displaying at that line. Otherwise begin displaying with the
19093 line following it. */
19094 if (MATRIX_ROW_BOTTOM_Y (last_row) - dy >= it.last_visible_y)
19096 init_to_row_start (&it, w, last_row);
19097 it.vpos = last_vpos;
19098 it.current_y = last_row->y;
19100 else
19102 init_to_row_end (&it, w, last_row);
19103 it.vpos = 1 + last_vpos;
19104 it.current_y = MATRIX_ROW_BOTTOM_Y (last_row);
19105 ++last_row;
19108 /* We may start in a continuation line. If so, we have to
19109 get the right continuation_lines_width and current_x. */
19110 it.continuation_lines_width = last_row->continuation_lines_width;
19111 it.hpos = it.current_x = 0;
19113 /* Display the rest of the lines at the window end. */
19114 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
19115 while (it.current_y < it.last_visible_y && !f->fonts_changed)
19117 /* Is it always sure that the display agrees with lines in
19118 the current matrix? I don't think so, so we mark rows
19119 displayed invalid in the current matrix by setting their
19120 enabled_p flag to false. */
19121 SET_MATRIX_ROW_ENABLED_P (w->current_matrix, it.vpos, false);
19122 if (display_line (&it, w->cursor.vpos))
19123 last_text_row_at_end = it.glyph_row - 1;
19127 /* Update window_end_pos and window_end_vpos. */
19128 if (first_unchanged_at_end_row && !last_text_row_at_end)
19130 /* Window end line if one of the preserved rows from the current
19131 matrix. Set row to the last row displaying text in current
19132 matrix starting at first_unchanged_at_end_row, after
19133 scrolling. */
19134 eassert (MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
19135 row = find_last_row_displaying_text (w->current_matrix, &it,
19136 first_unchanged_at_end_row);
19137 eassume (row && MATRIX_ROW_DISPLAYS_TEXT_P (row));
19138 adjust_window_ends (w, row, true);
19139 eassert (w->window_end_bytepos >= 0);
19140 IF_DEBUG (debug_method_add (w, "A"));
19142 else if (last_text_row_at_end)
19144 adjust_window_ends (w, last_text_row_at_end, false);
19145 eassert (w->window_end_bytepos >= 0);
19146 IF_DEBUG (debug_method_add (w, "B"));
19148 else if (last_text_row)
19150 /* We have displayed either to the end of the window or at the
19151 end of the window, i.e. the last row with text is to be found
19152 in the desired matrix. */
19153 adjust_window_ends (w, last_text_row, false);
19154 eassert (w->window_end_bytepos >= 0);
19156 else if (first_unchanged_at_end_row == NULL
19157 && last_text_row == NULL
19158 && last_text_row_at_end == NULL)
19160 /* Displayed to end of window, but no line containing text was
19161 displayed. Lines were deleted at the end of the window. */
19162 bool first_vpos = window_wants_header_line (w);
19163 int vpos = w->window_end_vpos;
19164 struct glyph_row *current_row = current_matrix->rows + vpos;
19165 struct glyph_row *desired_row = desired_matrix->rows + vpos;
19167 for (row = NULL; !row; --vpos, --current_row, --desired_row)
19169 eassert (first_vpos <= vpos);
19170 if (desired_row->enabled_p)
19172 if (MATRIX_ROW_DISPLAYS_TEXT_P (desired_row))
19173 row = desired_row;
19175 else if (MATRIX_ROW_DISPLAYS_TEXT_P (current_row))
19176 row = current_row;
19179 w->window_end_vpos = vpos + 1;
19180 w->window_end_pos = Z - MATRIX_ROW_END_CHARPOS (row);
19181 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
19182 eassert (w->window_end_bytepos >= 0);
19183 IF_DEBUG (debug_method_add (w, "C"));
19185 else
19186 emacs_abort ();
19188 IF_DEBUG ((debug_end_pos = w->window_end_pos,
19189 debug_end_vpos = w->window_end_vpos));
19191 /* Record that display has not been completed. */
19192 w->window_end_valid = false;
19193 w->desired_matrix->no_scrolling_p = true;
19194 return 3;
19196 #undef GIVE_UP
19201 /***********************************************************************
19202 More debugging support
19203 ***********************************************************************/
19205 #ifdef GLYPH_DEBUG
19207 void dump_glyph_row (struct glyph_row *, int, int) EXTERNALLY_VISIBLE;
19208 void dump_glyph_matrix (struct glyph_matrix *, int) EXTERNALLY_VISIBLE;
19209 void dump_glyph (struct glyph_row *, struct glyph *, int) EXTERNALLY_VISIBLE;
19212 /* Dump the contents of glyph matrix MATRIX on stderr.
19214 GLYPHS 0 means don't show glyph contents.
19215 GLYPHS 1 means show glyphs in short form
19216 GLYPHS > 1 means show glyphs in long form. */
19218 void
19219 dump_glyph_matrix (struct glyph_matrix *matrix, int glyphs)
19221 int i;
19222 for (i = 0; i < matrix->nrows; ++i)
19223 dump_glyph_row (MATRIX_ROW (matrix, i), i, glyphs);
19227 /* Dump contents of glyph GLYPH to stderr. ROW and AREA are
19228 the glyph row and area where the glyph comes from. */
19230 void
19231 dump_glyph (struct glyph_row *row, struct glyph *glyph, int area)
19233 if (glyph->type == CHAR_GLYPH
19234 || glyph->type == GLYPHLESS_GLYPH)
19236 fprintf (stderr,
19237 " %5"pD"d %c %9"pD"d %c %3d 0x%06x %c %4d %1.1d%1.1d\n",
19238 glyph - row->glyphs[TEXT_AREA],
19239 (glyph->type == CHAR_GLYPH
19240 ? 'C'
19241 : 'G'),
19242 glyph->charpos,
19243 (BUFFERP (glyph->object)
19244 ? 'B'
19245 : (STRINGP (glyph->object)
19246 ? 'S'
19247 : (NILP (glyph->object)
19248 ? '0'
19249 : '-'))),
19250 glyph->pixel_width,
19251 glyph->u.ch,
19252 (glyph->u.ch < 0x80 && glyph->u.ch >= ' '
19253 ? (int) glyph->u.ch
19254 : '.'),
19255 glyph->face_id,
19256 glyph->left_box_line_p,
19257 glyph->right_box_line_p);
19259 else if (glyph->type == STRETCH_GLYPH)
19261 fprintf (stderr,
19262 " %5"pD"d %c %9"pD"d %c %3d 0x%06x %c %4d %1.1d%1.1d\n",
19263 glyph - row->glyphs[TEXT_AREA],
19264 'S',
19265 glyph->charpos,
19266 (BUFFERP (glyph->object)
19267 ? 'B'
19268 : (STRINGP (glyph->object)
19269 ? 'S'
19270 : (NILP (glyph->object)
19271 ? '0'
19272 : '-'))),
19273 glyph->pixel_width,
19275 ' ',
19276 glyph->face_id,
19277 glyph->left_box_line_p,
19278 glyph->right_box_line_p);
19280 else if (glyph->type == IMAGE_GLYPH)
19282 fprintf (stderr,
19283 " %5"pD"d %c %9"pD"d %c %3d 0x%06x %c %4d %1.1d%1.1d\n",
19284 glyph - row->glyphs[TEXT_AREA],
19285 'I',
19286 glyph->charpos,
19287 (BUFFERP (glyph->object)
19288 ? 'B'
19289 : (STRINGP (glyph->object)
19290 ? 'S'
19291 : (NILP (glyph->object)
19292 ? '0'
19293 : '-'))),
19294 glyph->pixel_width,
19295 (unsigned int) glyph->u.img_id,
19296 '.',
19297 glyph->face_id,
19298 glyph->left_box_line_p,
19299 glyph->right_box_line_p);
19301 else if (glyph->type == COMPOSITE_GLYPH)
19303 fprintf (stderr,
19304 " %5"pD"d %c %9"pD"d %c %3d 0x%06x",
19305 glyph - row->glyphs[TEXT_AREA],
19306 '+',
19307 glyph->charpos,
19308 (BUFFERP (glyph->object)
19309 ? 'B'
19310 : (STRINGP (glyph->object)
19311 ? 'S'
19312 : (NILP (glyph->object)
19313 ? '0'
19314 : '-'))),
19315 glyph->pixel_width,
19316 (unsigned int) glyph->u.cmp.id);
19317 if (glyph->u.cmp.automatic)
19318 fprintf (stderr,
19319 "[%d-%d]",
19320 glyph->slice.cmp.from, glyph->slice.cmp.to);
19321 fprintf (stderr, " . %4d %1.1d%1.1d\n",
19322 glyph->face_id,
19323 glyph->left_box_line_p,
19324 glyph->right_box_line_p);
19326 else if (glyph->type == XWIDGET_GLYPH)
19328 #ifndef HAVE_XWIDGETS
19329 eassume (false);
19330 #else
19331 fprintf (stderr,
19332 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
19333 glyph - row->glyphs[TEXT_AREA],
19334 'X',
19335 glyph->charpos,
19336 (BUFFERP (glyph->object)
19337 ? 'B'
19338 : (STRINGP (glyph->object)
19339 ? 'S'
19340 : '-')),
19341 glyph->pixel_width,
19342 glyph->u.xwidget,
19343 '.',
19344 glyph->face_id,
19345 glyph->left_box_line_p,
19346 glyph->right_box_line_p);
19347 #endif
19352 /* Dump the contents of glyph row at VPOS in MATRIX to stderr.
19353 GLYPHS 0 means don't show glyph contents.
19354 GLYPHS 1 means show glyphs in short form
19355 GLYPHS > 1 means show glyphs in long form. */
19357 void
19358 dump_glyph_row (struct glyph_row *row, int vpos, int glyphs)
19360 if (glyphs != 1)
19362 fprintf (stderr, "Row Start End Used oE><\\CTZFesm X Y W H V A P\n");
19363 fprintf (stderr, "==============================================================================\n");
19365 fprintf (stderr, "%3d %9"pD"d %9"pD"d %4d %1.1d%1.1d%1.1d%1.1d\
19366 %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d %4d %4d %4d %4d %4d %4d %4d\n",
19367 vpos,
19368 MATRIX_ROW_START_CHARPOS (row),
19369 MATRIX_ROW_END_CHARPOS (row),
19370 row->used[TEXT_AREA],
19371 row->contains_overlapping_glyphs_p,
19372 row->enabled_p,
19373 row->truncated_on_left_p,
19374 row->truncated_on_right_p,
19375 row->continued_p,
19376 MATRIX_ROW_CONTINUATION_LINE_P (row),
19377 MATRIX_ROW_DISPLAYS_TEXT_P (row),
19378 row->ends_at_zv_p,
19379 row->fill_line_p,
19380 row->ends_in_middle_of_char_p,
19381 row->starts_in_middle_of_char_p,
19382 row->mouse_face_p,
19383 row->x,
19384 row->y,
19385 row->pixel_width,
19386 row->height,
19387 row->visible_height,
19388 row->ascent,
19389 row->phys_ascent);
19390 /* The next 3 lines should align to "Start" in the header. */
19391 fprintf (stderr, " %9"pD"d %9"pD"d\t%5d\n", row->start.overlay_string_index,
19392 row->end.overlay_string_index,
19393 row->continuation_lines_width);
19394 fprintf (stderr, " %9"pD"d %9"pD"d\n",
19395 CHARPOS (row->start.string_pos),
19396 CHARPOS (row->end.string_pos));
19397 fprintf (stderr, " %9d %9d\n", row->start.dpvec_index,
19398 row->end.dpvec_index);
19401 if (glyphs > 1)
19403 int area;
19405 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
19407 struct glyph *glyph = row->glyphs[area];
19408 struct glyph *glyph_end = glyph + row->used[area];
19410 /* Glyph for a line end in text. */
19411 if (area == TEXT_AREA && glyph == glyph_end && glyph->charpos > 0)
19412 ++glyph_end;
19414 if (glyph < glyph_end)
19415 fprintf (stderr, " Glyph# Type Pos O W Code C Face LR\n");
19417 for (; glyph < glyph_end; ++glyph)
19418 dump_glyph (row, glyph, area);
19421 else if (glyphs == 1)
19423 int area;
19424 char s[SHRT_MAX + 4];
19426 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
19428 int i;
19430 for (i = 0; i < row->used[area]; ++i)
19432 struct glyph *glyph = row->glyphs[area] + i;
19433 if (i == row->used[area] - 1
19434 && area == TEXT_AREA
19435 && NILP (glyph->object)
19436 && glyph->type == CHAR_GLYPH
19437 && glyph->u.ch == ' ')
19439 strcpy (&s[i], "[\\n]");
19440 i += 4;
19442 else if (glyph->type == CHAR_GLYPH
19443 && glyph->u.ch < 0x80
19444 && glyph->u.ch >= ' ')
19445 s[i] = glyph->u.ch;
19446 else
19447 s[i] = '.';
19450 s[i] = '\0';
19451 fprintf (stderr, "%3d: (%d) '%s'\n", vpos, row->enabled_p, s);
19457 DEFUN ("dump-glyph-matrix", Fdump_glyph_matrix,
19458 Sdump_glyph_matrix, 0, 1, "p",
19459 doc: /* Dump the current matrix of the selected window to stderr.
19460 Shows contents of glyph row structures. With non-nil
19461 parameter GLYPHS, dump glyphs as well. If GLYPHS is 1 show
19462 glyphs in short form, otherwise show glyphs in long form.
19464 Interactively, no argument means show glyphs in short form;
19465 with numeric argument, its value is passed as the GLYPHS flag. */)
19466 (Lisp_Object glyphs)
19468 struct window *w = XWINDOW (selected_window);
19469 struct buffer *buffer = XBUFFER (w->contents);
19471 fprintf (stderr, "PT = %"pD"d, BEGV = %"pD"d. ZV = %"pD"d\n",
19472 BUF_PT (buffer), BUF_BEGV (buffer), BUF_ZV (buffer));
19473 fprintf (stderr, "Cursor x = %d, y = %d, hpos = %d, vpos = %d\n",
19474 w->cursor.x, w->cursor.y, w->cursor.hpos, w->cursor.vpos);
19475 fprintf (stderr, "=============================================\n");
19476 dump_glyph_matrix (w->current_matrix,
19477 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 0);
19478 return Qnil;
19482 DEFUN ("dump-frame-glyph-matrix", Fdump_frame_glyph_matrix,
19483 Sdump_frame_glyph_matrix, 0, 0, "", doc: /* Dump the current glyph matrix of the selected frame to stderr.
19484 Only text-mode frames have frame glyph matrices. */)
19485 (void)
19487 struct frame *f = XFRAME (selected_frame);
19489 if (f->current_matrix)
19490 dump_glyph_matrix (f->current_matrix, 1);
19491 else
19492 fprintf (stderr, "*** This frame doesn't have a frame glyph matrix ***\n");
19493 return Qnil;
19497 DEFUN ("dump-glyph-row", Fdump_glyph_row, Sdump_glyph_row, 1, 2, "P",
19498 doc: /* Dump glyph row ROW to stderr.
19499 Interactively, ROW is the prefix numeric argument and defaults to
19500 the row which displays point.
19501 Optional argument GLYPHS 0 means don't dump glyphs.
19502 GLYPHS 1 means dump glyphs in short form.
19503 GLYPHS > 1 or omitted means dump glyphs in long form. */)
19504 (Lisp_Object row, Lisp_Object glyphs)
19506 struct glyph_matrix *matrix;
19507 EMACS_INT vpos;
19509 if (NILP (row))
19511 int d1, d2, d3, d4, d5, ypos;
19512 bool visible_p = pos_visible_p (XWINDOW (selected_window), PT,
19513 &d1, &d2, &d3, &d4, &d5, &ypos);
19514 if (visible_p)
19515 vpos = ypos;
19516 else
19517 vpos = 0;
19519 else
19521 CHECK_NUMBER (row);
19522 vpos = XINT (row);
19524 matrix = XWINDOW (selected_window)->current_matrix;
19525 if (vpos >= 0 && vpos < matrix->nrows)
19526 dump_glyph_row (MATRIX_ROW (matrix, vpos),
19527 vpos,
19528 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 2);
19529 return Qnil;
19533 DEFUN ("dump-tool-bar-row", Fdump_tool_bar_row, Sdump_tool_bar_row, 1, 2, "P",
19534 doc: /* Dump glyph row ROW of the tool-bar of the current frame to stderr.
19535 Interactively, ROW is the prefix numeric argument and defaults to zero.
19536 GLYPHS 0 means don't dump glyphs.
19537 GLYPHS 1 means dump glyphs in short form.
19538 GLYPHS > 1 or omitted means dump glyphs in long form.
19540 If there's no tool-bar, or if the tool-bar is not drawn by Emacs,
19541 do nothing. */)
19542 (Lisp_Object row, Lisp_Object glyphs)
19544 #if defined (HAVE_WINDOW_SYSTEM) && ! defined (USE_GTK) && ! defined (HAVE_NS)
19545 struct frame *sf = SELECTED_FRAME ();
19546 struct glyph_matrix *m = XWINDOW (sf->tool_bar_window)->current_matrix;
19547 EMACS_INT vpos;
19549 if (NILP (row))
19550 vpos = 0;
19551 else
19553 CHECK_NUMBER (row);
19554 vpos = XINT (row);
19556 if (vpos >= 0 && vpos < m->nrows)
19557 dump_glyph_row (MATRIX_ROW (m, vpos), vpos,
19558 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 2);
19559 #endif
19560 return Qnil;
19564 DEFUN ("trace-redisplay", Ftrace_redisplay, Strace_redisplay, 0, 1, "P",
19565 doc: /* Toggle tracing of redisplay.
19566 With ARG, turn tracing on if and only if ARG is positive. */)
19567 (Lisp_Object arg)
19569 if (NILP (arg))
19570 trace_redisplay_p = !trace_redisplay_p;
19571 else
19573 arg = Fprefix_numeric_value (arg);
19574 trace_redisplay_p = XINT (arg) > 0;
19577 return Qnil;
19581 DEFUN ("trace-to-stderr", Ftrace_to_stderr, Strace_to_stderr, 1, MANY, "",
19582 doc: /* Like `format', but print result to stderr.
19583 usage: (trace-to-stderr STRING &rest OBJECTS) */)
19584 (ptrdiff_t nargs, Lisp_Object *args)
19586 Lisp_Object s = Fformat (nargs, args);
19587 fwrite (SDATA (s), 1, SBYTES (s), stderr);
19588 return Qnil;
19591 #endif /* GLYPH_DEBUG */
19595 /***********************************************************************
19596 Building Desired Matrix Rows
19597 ***********************************************************************/
19599 /* Return a temporary glyph row holding the glyphs of an overlay arrow.
19600 Used for non-window-redisplay windows, and for windows w/o left fringe. */
19602 static struct glyph_row *
19603 get_overlay_arrow_glyph_row (struct window *w, Lisp_Object overlay_arrow_string)
19605 struct frame *f = XFRAME (WINDOW_FRAME (w));
19606 struct buffer *buffer = XBUFFER (w->contents);
19607 struct buffer *old = current_buffer;
19608 const unsigned char *arrow_string = SDATA (overlay_arrow_string);
19609 ptrdiff_t arrow_len = SCHARS (overlay_arrow_string);
19610 const unsigned char *arrow_end = arrow_string + arrow_len;
19611 const unsigned char *p;
19612 struct it it;
19613 bool multibyte_p;
19614 int n_glyphs_before;
19616 set_buffer_temp (buffer);
19617 init_iterator (&it, w, -1, -1, &scratch_glyph_row, DEFAULT_FACE_ID);
19618 scratch_glyph_row.reversed_p = false;
19619 it.glyph_row->used[TEXT_AREA] = 0;
19620 SET_TEXT_POS (it.position, 0, 0);
19622 multibyte_p = !NILP (BVAR (buffer, enable_multibyte_characters));
19623 p = arrow_string;
19624 while (p < arrow_end)
19626 Lisp_Object face, ilisp;
19628 /* Get the next character. */
19629 if (multibyte_p)
19630 it.c = it.char_to_display = string_char_and_length (p, &it.len);
19631 else
19633 it.c = it.char_to_display = *p, it.len = 1;
19634 if (! ASCII_CHAR_P (it.c))
19635 it.char_to_display = BYTE8_TO_CHAR (it.c);
19637 p += it.len;
19639 /* Get its face. */
19640 ilisp = make_number (p - arrow_string);
19641 face = Fget_text_property (ilisp, Qface, overlay_arrow_string);
19642 it.face_id = compute_char_face (f, it.char_to_display, face);
19644 /* Compute its width, get its glyphs. */
19645 n_glyphs_before = it.glyph_row->used[TEXT_AREA];
19646 SET_TEXT_POS (it.position, -1, -1);
19647 PRODUCE_GLYPHS (&it);
19649 /* If this character doesn't fit any more in the line, we have
19650 to remove some glyphs. */
19651 if (it.current_x > it.last_visible_x)
19653 it.glyph_row->used[TEXT_AREA] = n_glyphs_before;
19654 break;
19658 set_buffer_temp (old);
19659 return it.glyph_row;
19663 /* Insert truncation glyphs at the start of IT->glyph_row. Which
19664 glyphs to insert is determined by produce_special_glyphs. */
19666 static void
19667 insert_left_trunc_glyphs (struct it *it)
19669 struct it truncate_it;
19670 struct glyph *from, *end, *to, *toend;
19672 eassert (!FRAME_WINDOW_P (it->f)
19673 || (!it->glyph_row->reversed_p
19674 && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0)
19675 || (it->glyph_row->reversed_p
19676 && WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0));
19678 /* Get the truncation glyphs. */
19679 truncate_it = *it;
19680 truncate_it.current_x = 0;
19681 truncate_it.face_id = DEFAULT_FACE_ID;
19682 truncate_it.glyph_row = &scratch_glyph_row;
19683 truncate_it.area = TEXT_AREA;
19684 truncate_it.glyph_row->used[TEXT_AREA] = 0;
19685 CHARPOS (truncate_it.position) = BYTEPOS (truncate_it.position) = -1;
19686 truncate_it.object = Qnil;
19687 produce_special_glyphs (&truncate_it, IT_TRUNCATION);
19689 /* Overwrite glyphs from IT with truncation glyphs. */
19690 if (!it->glyph_row->reversed_p)
19692 short tused = truncate_it.glyph_row->used[TEXT_AREA];
19694 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
19695 end = from + tused;
19696 to = it->glyph_row->glyphs[TEXT_AREA];
19697 toend = to + it->glyph_row->used[TEXT_AREA];
19698 if (FRAME_WINDOW_P (it->f))
19700 /* On GUI frames, when variable-size fonts are displayed,
19701 the truncation glyphs may need more pixels than the row's
19702 glyphs they overwrite. We overwrite more glyphs to free
19703 enough screen real estate, and enlarge the stretch glyph
19704 on the right (see display_line), if there is one, to
19705 preserve the screen position of the truncation glyphs on
19706 the right. */
19707 int w = 0;
19708 struct glyph *g = to;
19709 short used;
19711 /* The first glyph could be partially visible, in which case
19712 it->glyph_row->x will be negative. But we want the left
19713 truncation glyphs to be aligned at the left margin of the
19714 window, so we override the x coordinate at which the row
19715 will begin. */
19716 it->glyph_row->x = 0;
19717 while (g < toend && w < it->truncation_pixel_width)
19719 w += g->pixel_width;
19720 ++g;
19722 if (g - to - tused > 0)
19724 memmove (to + tused, g, (toend - g) * sizeof(*g));
19725 it->glyph_row->used[TEXT_AREA] -= g - to - tused;
19727 used = it->glyph_row->used[TEXT_AREA];
19728 if (it->glyph_row->truncated_on_right_p
19729 && WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0
19730 && it->glyph_row->glyphs[TEXT_AREA][used - 2].type
19731 == STRETCH_GLYPH)
19733 int extra = w - it->truncation_pixel_width;
19735 it->glyph_row->glyphs[TEXT_AREA][used - 2].pixel_width += extra;
19739 while (from < end)
19740 *to++ = *from++;
19742 /* There may be padding glyphs left over. Overwrite them too. */
19743 if (!FRAME_WINDOW_P (it->f))
19745 while (to < toend && CHAR_GLYPH_PADDING_P (*to))
19747 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
19748 while (from < end)
19749 *to++ = *from++;
19753 if (to > toend)
19754 it->glyph_row->used[TEXT_AREA] = to - it->glyph_row->glyphs[TEXT_AREA];
19756 else
19758 short tused = truncate_it.glyph_row->used[TEXT_AREA];
19760 /* In R2L rows, overwrite the last (rightmost) glyphs, and do
19761 that back to front. */
19762 end = truncate_it.glyph_row->glyphs[TEXT_AREA];
19763 from = end + truncate_it.glyph_row->used[TEXT_AREA] - 1;
19764 toend = it->glyph_row->glyphs[TEXT_AREA];
19765 to = toend + it->glyph_row->used[TEXT_AREA] - 1;
19766 if (FRAME_WINDOW_P (it->f))
19768 int w = 0;
19769 struct glyph *g = to;
19771 while (g >= toend && w < it->truncation_pixel_width)
19773 w += g->pixel_width;
19774 --g;
19776 if (to - g - tused > 0)
19777 to = g + tused;
19778 if (it->glyph_row->truncated_on_right_p
19779 && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0
19780 && it->glyph_row->glyphs[TEXT_AREA][1].type == STRETCH_GLYPH)
19782 int extra = w - it->truncation_pixel_width;
19784 it->glyph_row->glyphs[TEXT_AREA][1].pixel_width += extra;
19788 while (from >= end && to >= toend)
19789 *to-- = *from--;
19790 if (!FRAME_WINDOW_P (it->f))
19792 while (to >= toend && CHAR_GLYPH_PADDING_P (*to))
19794 from =
19795 truncate_it.glyph_row->glyphs[TEXT_AREA]
19796 + truncate_it.glyph_row->used[TEXT_AREA] - 1;
19797 while (from >= end && to >= toend)
19798 *to-- = *from--;
19801 if (from >= end)
19803 /* Need to free some room before prepending additional
19804 glyphs. */
19805 int move_by = from - end + 1;
19806 struct glyph *g0 = it->glyph_row->glyphs[TEXT_AREA];
19807 struct glyph *g = g0 + it->glyph_row->used[TEXT_AREA] - 1;
19809 for ( ; g >= g0; g--)
19810 g[move_by] = *g;
19811 while (from >= end)
19812 *to-- = *from--;
19813 it->glyph_row->used[TEXT_AREA] += move_by;
19818 /* Compute the hash code for ROW. */
19819 unsigned
19820 row_hash (struct glyph_row *row)
19822 int area, k;
19823 unsigned hashval = 0;
19825 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
19826 for (k = 0; k < row->used[area]; ++k)
19827 hashval = ((((hashval << 4) + (hashval >> 24)) & 0x0fffffff)
19828 + row->glyphs[area][k].u.val
19829 + row->glyphs[area][k].face_id
19830 + row->glyphs[area][k].padding_p
19831 + (row->glyphs[area][k].type << 2));
19833 return hashval;
19836 /* Compute the pixel height and width of IT->glyph_row.
19838 Most of the time, ascent and height of a display line will be equal
19839 to the max_ascent and max_height values of the display iterator
19840 structure. This is not the case if
19842 1. We hit ZV without displaying anything. In this case, max_ascent
19843 and max_height will be zero.
19845 2. We have some glyphs that don't contribute to the line height.
19846 (The glyph row flag contributes_to_line_height_p is for future
19847 pixmap extensions).
19849 The first case is easily covered by using default values because in
19850 these cases, the line height does not really matter, except that it
19851 must not be zero. */
19853 static void
19854 compute_line_metrics (struct it *it)
19856 struct glyph_row *row = it->glyph_row;
19858 if (FRAME_WINDOW_P (it->f))
19860 int i, min_y, max_y;
19862 /* The line may consist of one space only, that was added to
19863 place the cursor on it. If so, the row's height hasn't been
19864 computed yet. */
19865 if (row->height == 0)
19867 if (it->max_ascent + it->max_descent == 0)
19868 it->max_descent = it->max_phys_descent = FRAME_LINE_HEIGHT (it->f);
19869 row->ascent = it->max_ascent;
19870 row->height = it->max_ascent + it->max_descent;
19871 row->phys_ascent = it->max_phys_ascent;
19872 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
19873 row->extra_line_spacing = it->max_extra_line_spacing;
19876 /* Compute the width of this line. */
19877 row->pixel_width = row->x;
19878 for (i = 0; i < row->used[TEXT_AREA]; ++i)
19879 row->pixel_width += row->glyphs[TEXT_AREA][i].pixel_width;
19881 eassert (row->pixel_width >= 0);
19882 eassert (row->ascent >= 0 && row->height > 0);
19884 row->overlapping_p = (MATRIX_ROW_OVERLAPS_SUCC_P (row)
19885 || MATRIX_ROW_OVERLAPS_PRED_P (row));
19887 /* If first line's physical ascent is larger than its logical
19888 ascent, use the physical ascent, and make the row taller.
19889 This makes accented characters fully visible. */
19890 if (row == MATRIX_FIRST_TEXT_ROW (it->w->desired_matrix)
19891 && row->phys_ascent > row->ascent)
19893 row->height += row->phys_ascent - row->ascent;
19894 row->ascent = row->phys_ascent;
19897 /* Compute how much of the line is visible. */
19898 row->visible_height = row->height;
19900 min_y = WINDOW_HEADER_LINE_HEIGHT (it->w);
19901 max_y = WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w);
19903 if (row->y < min_y)
19904 row->visible_height -= min_y - row->y;
19905 if (row->y + row->height > max_y)
19906 row->visible_height -= row->y + row->height - max_y;
19908 else
19910 row->pixel_width = row->used[TEXT_AREA];
19911 if (row->continued_p)
19912 row->pixel_width -= it->continuation_pixel_width;
19913 else if (row->truncated_on_right_p)
19914 row->pixel_width -= it->truncation_pixel_width;
19915 row->ascent = row->phys_ascent = 0;
19916 row->height = row->phys_height = row->visible_height = 1;
19917 row->extra_line_spacing = 0;
19920 /* Compute a hash code for this row. */
19921 row->hash = row_hash (row);
19923 it->max_ascent = it->max_descent = 0;
19924 it->max_phys_ascent = it->max_phys_descent = 0;
19928 /* Append one space to the glyph row of iterator IT if doing a
19929 window-based redisplay. The space has the same face as
19930 IT->face_id. Value is true if a space was added.
19932 This function is called to make sure that there is always one glyph
19933 at the end of a glyph row that the cursor can be set on under
19934 window-systems. (If there weren't such a glyph we would not know
19935 how wide and tall a box cursor should be displayed).
19937 At the same time this space let's a nicely handle clearing to the
19938 end of the line if the row ends in italic text. */
19940 static bool
19941 append_space_for_newline (struct it *it, bool default_face_p)
19943 if (FRAME_WINDOW_P (it->f))
19945 int n = it->glyph_row->used[TEXT_AREA];
19947 if (it->glyph_row->glyphs[TEXT_AREA] + n
19948 < it->glyph_row->glyphs[1 + TEXT_AREA])
19950 /* Save some values that must not be changed.
19951 Must save IT->c and IT->len because otherwise
19952 ITERATOR_AT_END_P wouldn't work anymore after
19953 append_space_for_newline has been called. */
19954 enum display_element_type saved_what = it->what;
19955 int saved_c = it->c, saved_len = it->len;
19956 int saved_char_to_display = it->char_to_display;
19957 int saved_x = it->current_x;
19958 int saved_face_id = it->face_id;
19959 bool saved_box_end = it->end_of_box_run_p;
19960 struct text_pos saved_pos;
19961 Lisp_Object saved_object;
19962 struct face *face;
19964 saved_object = it->object;
19965 saved_pos = it->position;
19967 it->what = IT_CHARACTER;
19968 memset (&it->position, 0, sizeof it->position);
19969 it->object = Qnil;
19970 it->c = it->char_to_display = ' ';
19971 it->len = 1;
19973 /* If the default face was remapped, be sure to use the
19974 remapped face for the appended newline. */
19975 if (default_face_p)
19976 it->face_id = lookup_basic_face (it->f, DEFAULT_FACE_ID);
19977 else if (it->face_before_selective_p)
19978 it->face_id = it->saved_face_id;
19979 face = FACE_FROM_ID (it->f, it->face_id);
19980 it->face_id = FACE_FOR_CHAR (it->f, face, 0, -1, Qnil);
19981 /* In R2L rows, we will prepend a stretch glyph that will
19982 have the end_of_box_run_p flag set for it, so there's no
19983 need for the appended newline glyph to have that flag
19984 set. */
19985 if (it->glyph_row->reversed_p
19986 /* But if the appended newline glyph goes all the way to
19987 the end of the row, there will be no stretch glyph,
19988 so leave the box flag set. */
19989 && saved_x + FRAME_COLUMN_WIDTH (it->f) < it->last_visible_x)
19990 it->end_of_box_run_p = false;
19992 PRODUCE_GLYPHS (it);
19994 #ifdef HAVE_WINDOW_SYSTEM
19995 /* Make sure this space glyph has the right ascent and
19996 descent values, or else cursor at end of line will look
19997 funny, and height of empty lines will be incorrect. */
19998 struct glyph *g = it->glyph_row->glyphs[TEXT_AREA] + n;
19999 struct font *font = face->font ? face->font : FRAME_FONT (it->f);
20000 if (n == 0)
20002 Lisp_Object height, total_height;
20003 int extra_line_spacing = it->extra_line_spacing;
20004 int boff = font->baseline_offset;
20006 if (font->vertical_centering)
20007 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
20009 it->object = saved_object; /* get_it_property needs this */
20010 normal_char_ascent_descent (font, -1, &it->ascent, &it->descent);
20011 /* Must do a subset of line height processing from
20012 x_produce_glyph for newline characters. */
20013 height = get_it_property (it, Qline_height);
20014 if (CONSP (height)
20015 && CONSP (XCDR (height))
20016 && NILP (XCDR (XCDR (height))))
20018 total_height = XCAR (XCDR (height));
20019 height = XCAR (height);
20021 else
20022 total_height = Qnil;
20023 height = calc_line_height_property (it, height, font, boff, true);
20025 if (it->override_ascent >= 0)
20027 it->ascent = it->override_ascent;
20028 it->descent = it->override_descent;
20029 boff = it->override_boff;
20031 if (EQ (height, Qt))
20032 extra_line_spacing = 0;
20033 else
20035 Lisp_Object spacing;
20037 it->phys_ascent = it->ascent;
20038 it->phys_descent = it->descent;
20039 if (!NILP (height)
20040 && XINT (height) > it->ascent + it->descent)
20041 it->ascent = XINT (height) - it->descent;
20043 if (!NILP (total_height))
20044 spacing = calc_line_height_property (it, total_height, font,
20045 boff, false);
20046 else
20048 spacing = get_it_property (it, Qline_spacing);
20049 spacing = calc_line_height_property (it, spacing, font,
20050 boff, false);
20052 if (INTEGERP (spacing))
20054 extra_line_spacing = XINT (spacing);
20055 if (!NILP (total_height))
20056 extra_line_spacing -= (it->phys_ascent + it->phys_descent);
20059 if (extra_line_spacing > 0)
20061 it->descent += extra_line_spacing;
20062 if (extra_line_spacing > it->max_extra_line_spacing)
20063 it->max_extra_line_spacing = extra_line_spacing;
20065 it->max_ascent = it->ascent;
20066 it->max_descent = it->descent;
20067 /* Make sure compute_line_metrics recomputes the row height. */
20068 it->glyph_row->height = 0;
20071 g->ascent = it->max_ascent;
20072 g->descent = it->max_descent;
20073 #endif
20075 it->override_ascent = -1;
20076 it->constrain_row_ascent_descent_p = false;
20077 it->current_x = saved_x;
20078 it->object = saved_object;
20079 it->position = saved_pos;
20080 it->what = saved_what;
20081 it->face_id = saved_face_id;
20082 it->len = saved_len;
20083 it->c = saved_c;
20084 it->char_to_display = saved_char_to_display;
20085 it->end_of_box_run_p = saved_box_end;
20086 return true;
20090 return false;
20094 /* Extend the face of the last glyph in the text area of IT->glyph_row
20095 to the end of the display line. Called from display_line. If the
20096 glyph row is empty, add a space glyph to it so that we know the
20097 face to draw. Set the glyph row flag fill_line_p. If the glyph
20098 row is R2L, prepend a stretch glyph to cover the empty space to the
20099 left of the leftmost glyph. */
20101 static void
20102 extend_face_to_end_of_line (struct it *it)
20104 struct face *face, *default_face;
20105 struct frame *f = it->f;
20107 /* If line is already filled, do nothing. Non window-system frames
20108 get a grace of one more ``pixel'' because their characters are
20109 1-``pixel'' wide, so they hit the equality too early. This grace
20110 is needed only for R2L rows that are not continued, to produce
20111 one extra blank where we could display the cursor. */
20112 if ((it->current_x >= it->last_visible_x
20113 + (!FRAME_WINDOW_P (f)
20114 && it->glyph_row->reversed_p
20115 && !it->glyph_row->continued_p))
20116 /* If the window has display margins, we will need to extend
20117 their face even if the text area is filled. */
20118 && !(WINDOW_LEFT_MARGIN_WIDTH (it->w) > 0
20119 || WINDOW_RIGHT_MARGIN_WIDTH (it->w) > 0))
20120 return;
20122 /* The default face, possibly remapped. */
20123 default_face = FACE_FROM_ID_OR_NULL (f,
20124 lookup_basic_face (f, DEFAULT_FACE_ID));
20126 /* Face extension extends the background and box of IT->face_id
20127 to the end of the line. If the background equals the background
20128 of the frame, we don't have to do anything. */
20129 face = FACE_FROM_ID (f, (it->face_before_selective_p
20130 ? it->saved_face_id
20131 : it->face_id));
20133 if (FRAME_WINDOW_P (f)
20134 && MATRIX_ROW_DISPLAYS_TEXT_P (it->glyph_row)
20135 && face->box == FACE_NO_BOX
20136 && face->background == FRAME_BACKGROUND_PIXEL (f)
20137 #ifdef HAVE_WINDOW_SYSTEM
20138 && !face->stipple
20139 #endif
20140 && !it->glyph_row->reversed_p)
20141 return;
20143 /* Set the glyph row flag indicating that the face of the last glyph
20144 in the text area has to be drawn to the end of the text area. */
20145 it->glyph_row->fill_line_p = true;
20147 /* If current character of IT is not ASCII, make sure we have the
20148 ASCII face. This will be automatically undone the next time
20149 get_next_display_element returns a multibyte character. Note
20150 that the character will always be single byte in unibyte
20151 text. */
20152 if (!ASCII_CHAR_P (it->c))
20154 it->face_id = FACE_FOR_CHAR (f, face, 0, -1, Qnil);
20157 if (FRAME_WINDOW_P (f))
20159 /* If the row is empty, add a space with the current face of IT,
20160 so that we know which face to draw. */
20161 if (it->glyph_row->used[TEXT_AREA] == 0)
20163 it->glyph_row->glyphs[TEXT_AREA][0] = space_glyph;
20164 it->glyph_row->glyphs[TEXT_AREA][0].face_id = face->id;
20165 it->glyph_row->used[TEXT_AREA] = 1;
20167 /* Mode line and the header line don't have margins, and
20168 likewise the frame's tool-bar window, if there is any. */
20169 if (!(it->glyph_row->mode_line_p
20170 #if defined (HAVE_WINDOW_SYSTEM) && ! defined (USE_GTK) && ! defined (HAVE_NS)
20171 || (WINDOWP (f->tool_bar_window)
20172 && it->w == XWINDOW (f->tool_bar_window))
20173 #endif
20176 if (WINDOW_LEFT_MARGIN_WIDTH (it->w) > 0
20177 && it->glyph_row->used[LEFT_MARGIN_AREA] == 0)
20179 it->glyph_row->glyphs[LEFT_MARGIN_AREA][0] = space_glyph;
20180 it->glyph_row->glyphs[LEFT_MARGIN_AREA][0].face_id =
20181 default_face->id;
20182 it->glyph_row->used[LEFT_MARGIN_AREA] = 1;
20184 if (WINDOW_RIGHT_MARGIN_WIDTH (it->w) > 0
20185 && it->glyph_row->used[RIGHT_MARGIN_AREA] == 0)
20187 it->glyph_row->glyphs[RIGHT_MARGIN_AREA][0] = space_glyph;
20188 it->glyph_row->glyphs[RIGHT_MARGIN_AREA][0].face_id =
20189 default_face->id;
20190 it->glyph_row->used[RIGHT_MARGIN_AREA] = 1;
20193 #ifdef HAVE_WINDOW_SYSTEM
20194 if (it->glyph_row->reversed_p)
20196 /* Prepend a stretch glyph to the row, such that the
20197 rightmost glyph will be drawn flushed all the way to the
20198 right margin of the window. The stretch glyph that will
20199 occupy the empty space, if any, to the left of the
20200 glyphs. */
20201 struct font *font = face->font ? face->font : FRAME_FONT (f);
20202 struct glyph *row_start = it->glyph_row->glyphs[TEXT_AREA];
20203 struct glyph *row_end = row_start + it->glyph_row->used[TEXT_AREA];
20204 struct glyph *g;
20205 int row_width, stretch_ascent, stretch_width;
20206 struct text_pos saved_pos;
20207 int saved_face_id;
20208 bool saved_avoid_cursor, saved_box_start;
20210 for (row_width = 0, g = row_start; g < row_end; g++)
20211 row_width += g->pixel_width;
20213 /* FIXME: There are various minor display glitches in R2L
20214 rows when only one of the fringes is missing. The
20215 strange condition below produces the least bad effect. */
20216 if ((WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0)
20217 == (WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0)
20218 || WINDOW_RIGHT_FRINGE_WIDTH (it->w) != 0)
20219 stretch_width = window_box_width (it->w, TEXT_AREA);
20220 else
20221 stretch_width = it->last_visible_x - it->first_visible_x;
20222 stretch_width -= row_width;
20224 if (stretch_width > 0)
20226 stretch_ascent =
20227 (((it->ascent + it->descent)
20228 * FONT_BASE (font)) / FONT_HEIGHT (font));
20229 saved_pos = it->position;
20230 memset (&it->position, 0, sizeof it->position);
20231 saved_avoid_cursor = it->avoid_cursor_p;
20232 it->avoid_cursor_p = true;
20233 saved_face_id = it->face_id;
20234 saved_box_start = it->start_of_box_run_p;
20235 /* The last row's stretch glyph should get the default
20236 face, to avoid painting the rest of the window with
20237 the region face, if the region ends at ZV. */
20238 if (it->glyph_row->ends_at_zv_p)
20239 it->face_id = default_face->id;
20240 else
20241 it->face_id = face->id;
20242 it->start_of_box_run_p = false;
20243 append_stretch_glyph (it, Qnil, stretch_width,
20244 it->ascent + it->descent, stretch_ascent);
20245 it->position = saved_pos;
20246 it->avoid_cursor_p = saved_avoid_cursor;
20247 it->face_id = saved_face_id;
20248 it->start_of_box_run_p = saved_box_start;
20250 /* If stretch_width comes out negative, it means that the
20251 last glyph is only partially visible. In R2L rows, we
20252 want the leftmost glyph to be partially visible, so we
20253 need to give the row the corresponding left offset. */
20254 if (stretch_width < 0)
20255 it->glyph_row->x = stretch_width;
20257 #endif /* HAVE_WINDOW_SYSTEM */
20259 else
20261 /* Save some values that must not be changed. */
20262 int saved_x = it->current_x;
20263 struct text_pos saved_pos;
20264 Lisp_Object saved_object;
20265 enum display_element_type saved_what = it->what;
20266 int saved_face_id = it->face_id;
20268 saved_object = it->object;
20269 saved_pos = it->position;
20271 it->what = IT_CHARACTER;
20272 memset (&it->position, 0, sizeof it->position);
20273 it->object = Qnil;
20274 it->c = it->char_to_display = ' ';
20275 it->len = 1;
20277 if (WINDOW_LEFT_MARGIN_WIDTH (it->w) > 0
20278 && (it->glyph_row->used[LEFT_MARGIN_AREA]
20279 < WINDOW_LEFT_MARGIN_WIDTH (it->w))
20280 && !it->glyph_row->mode_line_p
20281 && default_face->background != FRAME_BACKGROUND_PIXEL (f))
20283 struct glyph *g = it->glyph_row->glyphs[LEFT_MARGIN_AREA];
20284 struct glyph *e = g + it->glyph_row->used[LEFT_MARGIN_AREA];
20286 for (it->current_x = 0; g < e; g++)
20287 it->current_x += g->pixel_width;
20289 it->area = LEFT_MARGIN_AREA;
20290 it->face_id = default_face->id;
20291 while (it->glyph_row->used[LEFT_MARGIN_AREA]
20292 < WINDOW_LEFT_MARGIN_WIDTH (it->w)
20293 && g < it->glyph_row->glyphs[TEXT_AREA])
20295 PRODUCE_GLYPHS (it);
20296 /* term.c:produce_glyphs advances it->current_x only for
20297 TEXT_AREA. */
20298 it->current_x += it->pixel_width;
20299 g++;
20302 it->current_x = saved_x;
20303 it->area = TEXT_AREA;
20306 /* The last row's blank glyphs should get the default face, to
20307 avoid painting the rest of the window with the region face,
20308 if the region ends at ZV. */
20309 if (it->glyph_row->ends_at_zv_p)
20310 it->face_id = default_face->id;
20311 else
20312 it->face_id = face->id;
20313 PRODUCE_GLYPHS (it);
20315 while (it->current_x <= it->last_visible_x)
20316 PRODUCE_GLYPHS (it);
20318 if (WINDOW_RIGHT_MARGIN_WIDTH (it->w) > 0
20319 && (it->glyph_row->used[RIGHT_MARGIN_AREA]
20320 < WINDOW_RIGHT_MARGIN_WIDTH (it->w))
20321 && !it->glyph_row->mode_line_p
20322 && default_face->background != FRAME_BACKGROUND_PIXEL (f))
20324 struct glyph *g = it->glyph_row->glyphs[RIGHT_MARGIN_AREA];
20325 struct glyph *e = g + it->glyph_row->used[RIGHT_MARGIN_AREA];
20327 for ( ; g < e; g++)
20328 it->current_x += g->pixel_width;
20330 it->area = RIGHT_MARGIN_AREA;
20331 it->face_id = default_face->id;
20332 while (it->glyph_row->used[RIGHT_MARGIN_AREA]
20333 < WINDOW_RIGHT_MARGIN_WIDTH (it->w)
20334 && g < it->glyph_row->glyphs[LAST_AREA])
20336 PRODUCE_GLYPHS (it);
20337 it->current_x += it->pixel_width;
20338 g++;
20341 it->area = TEXT_AREA;
20344 /* Don't count these blanks really. It would let us insert a left
20345 truncation glyph below and make us set the cursor on them, maybe. */
20346 it->current_x = saved_x;
20347 it->object = saved_object;
20348 it->position = saved_pos;
20349 it->what = saved_what;
20350 it->face_id = saved_face_id;
20355 /* Value is true if text starting at CHARPOS in current_buffer is
20356 trailing whitespace. */
20358 static bool
20359 trailing_whitespace_p (ptrdiff_t charpos)
20361 ptrdiff_t bytepos = CHAR_TO_BYTE (charpos);
20362 int c = 0;
20364 while (bytepos < ZV_BYTE
20365 && (c = FETCH_CHAR (bytepos),
20366 c == ' ' || c == '\t'))
20367 ++bytepos;
20369 if (bytepos >= ZV_BYTE || c == '\n' || c == '\r')
20371 if (bytepos != PT_BYTE)
20372 return true;
20374 return false;
20378 /* Highlight trailing whitespace, if any, in ROW. */
20380 static void
20381 highlight_trailing_whitespace (struct frame *f, struct glyph_row *row)
20383 int used = row->used[TEXT_AREA];
20385 if (used)
20387 struct glyph *start = row->glyphs[TEXT_AREA];
20388 struct glyph *glyph = start + used - 1;
20390 if (row->reversed_p)
20392 /* Right-to-left rows need to be processed in the opposite
20393 direction, so swap the edge pointers. */
20394 glyph = start;
20395 start = row->glyphs[TEXT_AREA] + used - 1;
20398 /* Skip over glyphs inserted to display the cursor at the
20399 end of a line, for extending the face of the last glyph
20400 to the end of the line on terminals, and for truncation
20401 and continuation glyphs. */
20402 if (!row->reversed_p)
20404 while (glyph >= start
20405 && glyph->type == CHAR_GLYPH
20406 && NILP (glyph->object))
20407 --glyph;
20409 else
20411 while (glyph <= start
20412 && glyph->type == CHAR_GLYPH
20413 && NILP (glyph->object))
20414 ++glyph;
20417 /* If last glyph is a space or stretch, and it's trailing
20418 whitespace, set the face of all trailing whitespace glyphs in
20419 IT->glyph_row to `trailing-whitespace'. */
20420 if ((row->reversed_p ? glyph <= start : glyph >= start)
20421 && BUFFERP (glyph->object)
20422 && (glyph->type == STRETCH_GLYPH
20423 || (glyph->type == CHAR_GLYPH
20424 && glyph->u.ch == ' '))
20425 && trailing_whitespace_p (glyph->charpos))
20427 int face_id = lookup_named_face (f, Qtrailing_whitespace, false);
20428 if (face_id < 0)
20429 return;
20431 if (!row->reversed_p)
20433 while (glyph >= start
20434 && BUFFERP (glyph->object)
20435 && (glyph->type == STRETCH_GLYPH
20436 || (glyph->type == CHAR_GLYPH
20437 && glyph->u.ch == ' ')))
20438 (glyph--)->face_id = face_id;
20440 else
20442 while (glyph <= start
20443 && BUFFERP (glyph->object)
20444 && (glyph->type == STRETCH_GLYPH
20445 || (glyph->type == CHAR_GLYPH
20446 && glyph->u.ch == ' ')))
20447 (glyph++)->face_id = face_id;
20454 /* Value is true if glyph row ROW should be
20455 considered to hold the buffer position CHARPOS. */
20457 static bool
20458 row_for_charpos_p (struct glyph_row *row, ptrdiff_t charpos)
20460 bool result = true;
20462 if (charpos == CHARPOS (row->end.pos)
20463 || charpos == MATRIX_ROW_END_CHARPOS (row))
20465 /* Suppose the row ends on a string.
20466 Unless the row is continued, that means it ends on a newline
20467 in the string. If it's anything other than a display string
20468 (e.g., a before-string from an overlay), we don't want the
20469 cursor there. (This heuristic seems to give the optimal
20470 behavior for the various types of multi-line strings.)
20471 One exception: if the string has `cursor' property on one of
20472 its characters, we _do_ want the cursor there. */
20473 if (CHARPOS (row->end.string_pos) >= 0)
20475 if (row->continued_p)
20476 result = true;
20477 else
20479 /* Check for `display' property. */
20480 struct glyph *beg = row->glyphs[TEXT_AREA];
20481 struct glyph *end = beg + row->used[TEXT_AREA] - 1;
20482 struct glyph *glyph;
20484 result = false;
20485 for (glyph = end; glyph >= beg; --glyph)
20486 if (STRINGP (glyph->object))
20488 Lisp_Object prop
20489 = Fget_char_property (make_number (charpos),
20490 Qdisplay, Qnil);
20491 result =
20492 (!NILP (prop)
20493 && display_prop_string_p (prop, glyph->object));
20494 /* If there's a `cursor' property on one of the
20495 string's characters, this row is a cursor row,
20496 even though this is not a display string. */
20497 if (!result)
20499 Lisp_Object s = glyph->object;
20501 for ( ; glyph >= beg && EQ (glyph->object, s); --glyph)
20503 ptrdiff_t gpos = glyph->charpos;
20505 if (!NILP (Fget_char_property (make_number (gpos),
20506 Qcursor, s)))
20508 result = true;
20509 break;
20513 break;
20517 else if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
20519 /* If the row ends in middle of a real character,
20520 and the line is continued, we want the cursor here.
20521 That's because CHARPOS (ROW->end.pos) would equal
20522 PT if PT is before the character. */
20523 if (!row->ends_in_ellipsis_p)
20524 result = row->continued_p;
20525 else
20526 /* If the row ends in an ellipsis, then
20527 CHARPOS (ROW->end.pos) will equal point after the
20528 invisible text. We want that position to be displayed
20529 after the ellipsis. */
20530 result = false;
20532 /* If the row ends at ZV, display the cursor at the end of that
20533 row instead of at the start of the row below. */
20534 else
20535 result = row->ends_at_zv_p;
20538 return result;
20541 /* Value is true if glyph row ROW should be
20542 used to hold the cursor. */
20544 static bool
20545 cursor_row_p (struct glyph_row *row)
20547 return row_for_charpos_p (row, PT);
20552 /* Push the property PROP so that it will be rendered at the current
20553 position in IT. Return true if PROP was successfully pushed, false
20554 otherwise. Called from handle_line_prefix to handle the
20555 `line-prefix' and `wrap-prefix' properties. */
20557 static bool
20558 push_prefix_prop (struct it *it, Lisp_Object prop)
20560 struct text_pos pos =
20561 STRINGP (it->string) ? it->current.string_pos : it->current.pos;
20563 eassert (it->method == GET_FROM_BUFFER
20564 || it->method == GET_FROM_DISPLAY_VECTOR
20565 || it->method == GET_FROM_STRING
20566 || it->method == GET_FROM_IMAGE);
20568 /* We need to save the current buffer/string position, so it will be
20569 restored by pop_it, because iterate_out_of_display_property
20570 depends on that being set correctly, but some situations leave
20571 it->position not yet set when this function is called. */
20572 push_it (it, &pos);
20574 if (STRINGP (prop))
20576 if (SCHARS (prop) == 0)
20578 pop_it (it);
20579 return false;
20582 it->string = prop;
20583 it->string_from_prefix_prop_p = true;
20584 it->multibyte_p = STRING_MULTIBYTE (it->string);
20585 it->current.overlay_string_index = -1;
20586 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
20587 it->end_charpos = it->string_nchars = SCHARS (it->string);
20588 it->method = GET_FROM_STRING;
20589 it->stop_charpos = 0;
20590 it->prev_stop = 0;
20591 it->base_level_stop = 0;
20592 it->cmp_it.id = -1;
20594 /* Force paragraph direction to be that of the parent
20595 buffer/string. */
20596 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
20597 it->paragraph_embedding = it->bidi_it.paragraph_dir;
20598 else
20599 it->paragraph_embedding = L2R;
20601 /* Set up the bidi iterator for this display string. */
20602 if (it->bidi_p)
20604 it->bidi_it.string.lstring = it->string;
20605 it->bidi_it.string.s = NULL;
20606 it->bidi_it.string.schars = it->end_charpos;
20607 it->bidi_it.string.bufpos = IT_CHARPOS (*it);
20608 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
20609 it->bidi_it.string.unibyte = !it->multibyte_p;
20610 it->bidi_it.w = it->w;
20611 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
20614 else if (CONSP (prop) && EQ (XCAR (prop), Qspace))
20616 it->method = GET_FROM_STRETCH;
20617 it->object = prop;
20619 #ifdef HAVE_WINDOW_SYSTEM
20620 else if (IMAGEP (prop))
20622 it->what = IT_IMAGE;
20623 it->image_id = lookup_image (it->f, prop);
20624 it->method = GET_FROM_IMAGE;
20626 #endif /* HAVE_WINDOW_SYSTEM */
20627 else
20629 pop_it (it); /* bogus display property, give up */
20630 return false;
20633 return true;
20636 /* Return the character-property PROP at the current position in IT. */
20638 static Lisp_Object
20639 get_it_property (struct it *it, Lisp_Object prop)
20641 Lisp_Object position, object = it->object;
20643 if (STRINGP (object))
20644 position = make_number (IT_STRING_CHARPOS (*it));
20645 else if (BUFFERP (object))
20647 position = make_number (IT_CHARPOS (*it));
20648 object = it->window;
20650 else
20651 return Qnil;
20653 return Fget_char_property (position, prop, object);
20656 /* See if there's a line- or wrap-prefix, and if so, push it on IT. */
20658 static void
20659 handle_line_prefix (struct it *it)
20661 Lisp_Object prefix;
20663 if (it->continuation_lines_width > 0)
20665 prefix = get_it_property (it, Qwrap_prefix);
20666 if (NILP (prefix))
20667 prefix = Vwrap_prefix;
20669 else
20671 prefix = get_it_property (it, Qline_prefix);
20672 if (NILP (prefix))
20673 prefix = Vline_prefix;
20675 if (! NILP (prefix) && push_prefix_prop (it, prefix))
20677 /* If the prefix is wider than the window, and we try to wrap
20678 it, it would acquire its own wrap prefix, and so on till the
20679 iterator stack overflows. So, don't wrap the prefix. */
20680 it->line_wrap = TRUNCATE;
20681 it->avoid_cursor_p = true;
20687 /* Remove N glyphs at the start of a reversed IT->glyph_row. Called
20688 only for R2L lines from display_line and display_string, when they
20689 decide that too many glyphs were produced by PRODUCE_GLYPHS, and
20690 the line/string needs to be continued on the next glyph row. */
20691 static void
20692 unproduce_glyphs (struct it *it, int n)
20694 struct glyph *glyph, *end;
20696 eassert (it->glyph_row);
20697 eassert (it->glyph_row->reversed_p);
20698 eassert (it->area == TEXT_AREA);
20699 eassert (n <= it->glyph_row->used[TEXT_AREA]);
20701 if (n > it->glyph_row->used[TEXT_AREA])
20702 n = it->glyph_row->used[TEXT_AREA];
20703 glyph = it->glyph_row->glyphs[TEXT_AREA] + n;
20704 end = it->glyph_row->glyphs[TEXT_AREA] + it->glyph_row->used[TEXT_AREA];
20705 for ( ; glyph < end; glyph++)
20706 glyph[-n] = *glyph;
20709 /* Find the positions in a bidi-reordered ROW to serve as ROW->minpos
20710 and ROW->maxpos. */
20711 static void
20712 find_row_edges (struct it *it, struct glyph_row *row,
20713 ptrdiff_t min_pos, ptrdiff_t min_bpos,
20714 ptrdiff_t max_pos, ptrdiff_t max_bpos)
20716 /* FIXME: Revisit this when glyph ``spilling'' in continuation
20717 lines' rows is implemented for bidi-reordered rows. */
20719 /* ROW->minpos is the value of min_pos, the minimal buffer position
20720 we have in ROW, or ROW->start.pos if that is smaller. */
20721 if (min_pos <= ZV && min_pos < row->start.pos.charpos)
20722 SET_TEXT_POS (row->minpos, min_pos, min_bpos);
20723 else
20724 /* We didn't find buffer positions smaller than ROW->start, or
20725 didn't find _any_ valid buffer positions in any of the glyphs,
20726 so we must trust the iterator's computed positions. */
20727 row->minpos = row->start.pos;
20728 if (max_pos <= 0)
20730 max_pos = CHARPOS (it->current.pos);
20731 max_bpos = BYTEPOS (it->current.pos);
20734 /* Here are the various use-cases for ending the row, and the
20735 corresponding values for ROW->maxpos:
20737 Line ends in a newline from buffer eol_pos + 1
20738 Line is continued from buffer max_pos + 1
20739 Line is truncated on right it->current.pos
20740 Line ends in a newline from string max_pos + 1(*)
20741 (*) + 1 only when line ends in a forward scan
20742 Line is continued from string max_pos
20743 Line is continued from display vector max_pos
20744 Line is entirely from a string min_pos == max_pos
20745 Line is entirely from a display vector min_pos == max_pos
20746 Line that ends at ZV ZV
20748 If you discover other use-cases, please add them here as
20749 appropriate. */
20750 if (row->ends_at_zv_p)
20751 row->maxpos = it->current.pos;
20752 else if (row->used[TEXT_AREA])
20754 bool seen_this_string = false;
20755 struct glyph_row *r1 = row - 1;
20757 /* Did we see the same display string on the previous row? */
20758 if (STRINGP (it->object)
20759 /* this is not the first row */
20760 && row > it->w->desired_matrix->rows
20761 /* previous row is not the header line */
20762 && !r1->mode_line_p
20763 /* previous row also ends in a newline from a string */
20764 && r1->ends_in_newline_from_string_p)
20766 struct glyph *start, *end;
20768 /* Search for the last glyph of the previous row that came
20769 from buffer or string. Depending on whether the row is
20770 L2R or R2L, we need to process it front to back or the
20771 other way round. */
20772 if (!r1->reversed_p)
20774 start = r1->glyphs[TEXT_AREA];
20775 end = start + r1->used[TEXT_AREA];
20776 /* Glyphs inserted by redisplay have nil as their object. */
20777 while (end > start
20778 && NILP ((end - 1)->object)
20779 && (end - 1)->charpos <= 0)
20780 --end;
20781 if (end > start)
20783 if (EQ ((end - 1)->object, it->object))
20784 seen_this_string = true;
20786 else
20787 /* If all the glyphs of the previous row were inserted
20788 by redisplay, it means the previous row was
20789 produced from a single newline, which is only
20790 possible if that newline came from the same string
20791 as the one which produced this ROW. */
20792 seen_this_string = true;
20794 else
20796 end = r1->glyphs[TEXT_AREA] - 1;
20797 start = end + r1->used[TEXT_AREA];
20798 while (end < start
20799 && NILP ((end + 1)->object)
20800 && (end + 1)->charpos <= 0)
20801 ++end;
20802 if (end < start)
20804 if (EQ ((end + 1)->object, it->object))
20805 seen_this_string = true;
20807 else
20808 seen_this_string = true;
20811 /* Take note of each display string that covers a newline only
20812 once, the first time we see it. This is for when a display
20813 string includes more than one newline in it. */
20814 if (row->ends_in_newline_from_string_p && !seen_this_string)
20816 /* If we were scanning the buffer forward when we displayed
20817 the string, we want to account for at least one buffer
20818 position that belongs to this row (position covered by
20819 the display string), so that cursor positioning will
20820 consider this row as a candidate when point is at the end
20821 of the visual line represented by this row. This is not
20822 required when scanning back, because max_pos will already
20823 have a much larger value. */
20824 if (CHARPOS (row->end.pos) > max_pos)
20825 INC_BOTH (max_pos, max_bpos);
20826 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
20828 else if (CHARPOS (it->eol_pos) > 0)
20829 SET_TEXT_POS (row->maxpos,
20830 CHARPOS (it->eol_pos) + 1, BYTEPOS (it->eol_pos) + 1);
20831 else if (row->continued_p)
20833 /* If max_pos is different from IT's current position, it
20834 means IT->method does not belong to the display element
20835 at max_pos. However, it also means that the display
20836 element at max_pos was displayed in its entirety on this
20837 line, which is equivalent to saying that the next line
20838 starts at the next buffer position. */
20839 if (IT_CHARPOS (*it) == max_pos && it->method != GET_FROM_BUFFER)
20840 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
20841 else
20843 INC_BOTH (max_pos, max_bpos);
20844 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
20847 else if (row->truncated_on_right_p)
20848 /* display_line already called reseat_at_next_visible_line_start,
20849 which puts the iterator at the beginning of the next line, in
20850 the logical order. */
20851 row->maxpos = it->current.pos;
20852 else if (max_pos == min_pos && it->method != GET_FROM_BUFFER)
20853 /* A line that is entirely from a string/image/stretch... */
20854 row->maxpos = row->minpos;
20855 else
20856 emacs_abort ();
20858 else
20859 row->maxpos = it->current.pos;
20862 /* Like display_count_lines, but capable of counting outside of the
20863 current narrowed region. */
20864 static ptrdiff_t
20865 display_count_lines_logically (ptrdiff_t start_byte, ptrdiff_t limit_byte,
20866 ptrdiff_t count, ptrdiff_t *byte_pos_ptr)
20868 if (!display_line_numbers_widen || (BEGV == BEG && ZV == Z))
20869 return display_count_lines (start_byte, limit_byte, count, byte_pos_ptr);
20871 ptrdiff_t val;
20872 ptrdiff_t pdl_count = SPECPDL_INDEX ();
20873 record_unwind_protect (save_restriction_restore, save_restriction_save ());
20874 Fwiden ();
20875 val = display_count_lines (start_byte, limit_byte, count, byte_pos_ptr);
20876 unbind_to (pdl_count, Qnil);
20877 return val;
20880 /* Count the number of screen lines in window IT->w between character
20881 position IT_CHARPOS(*IT) and the line showing that window's point. */
20882 static ptrdiff_t
20883 display_count_lines_visually (struct it *it)
20885 struct it tem_it;
20886 ptrdiff_t to;
20887 struct text_pos from;
20889 /* If we already calculated a relative line number, use that. This
20890 trick relies on the fact that visual lines (a.k.a. "glyph rows")
20891 are laid out sequentially, one by one, for each sequence of calls
20892 to display_line or other similar function that follows a call to
20893 init_iterator. */
20894 if (it->lnum_bytepos > 0)
20895 return it->lnum + 1;
20896 else
20898 ptrdiff_t count = SPECPDL_INDEX ();
20900 if (IT_CHARPOS (*it) <= PT)
20902 from = it->current.pos;
20903 to = PT;
20905 else
20907 SET_TEXT_POS (from, PT, PT_BYTE);
20908 to = IT_CHARPOS (*it);
20910 start_display (&tem_it, it->w, from);
20911 /* Need to disable visual mode temporarily, since otherwise the
20912 call to move_it_to will cause infinite recursion. */
20913 specbind (Qdisplay_line_numbers, Qrelative);
20914 /* Some redisplay optimizations could invoke us very far from
20915 PT, which will make the caller painfully slow. There should
20916 be no need to go too far beyond the window's bottom, as any
20917 such optimization will fail to show point anyway. */
20918 move_it_to (&tem_it, to, -1,
20919 tem_it.last_visible_y
20920 + (SCROLL_LIMIT + 10) * FRAME_LINE_HEIGHT (tem_it.f),
20921 -1, MOVE_TO_POS | MOVE_TO_Y);
20922 unbind_to (count, Qnil);
20923 return IT_CHARPOS (*it) <= PT ? -tem_it.vpos : tem_it.vpos;
20927 /* Produce the line-number glyphs for the current glyph_row. If
20928 IT->glyph_row is non-NULL, populate the row with the produced
20929 glyphs. */
20930 static void
20931 maybe_produce_line_number (struct it *it)
20933 ptrdiff_t last_line = it->lnum;
20934 ptrdiff_t start_from, bytepos;
20935 ptrdiff_t this_line;
20936 bool first_time = false;
20937 ptrdiff_t beg_byte = display_line_numbers_widen ? BEG_BYTE : BEGV_BYTE;
20938 ptrdiff_t z_byte = display_line_numbers_widen ? Z_BYTE : ZV_BYTE;
20939 void *itdata = bidi_shelve_cache ();
20941 if (EQ (Vdisplay_line_numbers, Qvisual))
20942 this_line = display_count_lines_visually (it);
20943 else
20945 if (!last_line)
20947 /* If possible, reuse data cached by line-number-mode. */
20948 if (it->w->base_line_number > 0
20949 && it->w->base_line_pos > 0
20950 && it->w->base_line_pos <= IT_CHARPOS (*it)
20951 /* line-number-mode always displays narrowed line
20952 numbers, so we cannot use its data if the user wants
20953 line numbers that disregard narrowing, or if the
20954 buffer's narrowing has just changed. */
20955 && !(display_line_numbers_widen
20956 && (BEG_BYTE != BEGV_BYTE || Z_BYTE != ZV_BYTE))
20957 && !current_buffer->clip_changed)
20959 start_from = CHAR_TO_BYTE (it->w->base_line_pos);
20960 last_line = it->w->base_line_number - 1;
20962 else
20963 start_from = beg_byte;
20964 if (!it->lnum_bytepos)
20965 first_time = true;
20967 else
20968 start_from = it->lnum_bytepos;
20970 /* Paranoia: what if someone changes the narrowing since the
20971 last time display_line was called? Shouldn't really happen,
20972 but who knows what some crazy Lisp invoked by :eval could do? */
20973 if (!(beg_byte <= start_from && start_from <= z_byte))
20975 last_line = 0;
20976 start_from = beg_byte;
20979 this_line =
20980 last_line + display_count_lines_logically (start_from,
20981 IT_BYTEPOS (*it),
20982 IT_CHARPOS (*it), &bytepos);
20983 eassert (this_line > 0 || (this_line == 0 && start_from == beg_byte));
20984 eassert (bytepos == IT_BYTEPOS (*it));
20987 /* Record the line number information. */
20988 if (this_line != last_line || !it->lnum_bytepos)
20990 it->lnum = this_line;
20991 it->lnum_bytepos = IT_BYTEPOS (*it);
20994 /* Produce the glyphs for the line number. */
20995 struct it tem_it;
20996 char lnum_buf[INT_STRLEN_BOUND (ptrdiff_t) + 1];
20997 bool beyond_zv = IT_BYTEPOS (*it) >= ZV_BYTE ? true : false;
20998 ptrdiff_t lnum_offset = -1; /* to produce 1-based line numbers */
20999 int lnum_face_id = merge_faces (it->f, Qline_number, 0, DEFAULT_FACE_ID);
21000 int current_lnum_face_id
21001 = merge_faces (it->f, Qline_number_current_line, 0, DEFAULT_FACE_ID);
21002 /* Compute point's line number if needed. */
21003 if ((EQ (Vdisplay_line_numbers, Qrelative)
21004 || EQ (Vdisplay_line_numbers, Qvisual)
21005 || lnum_face_id != current_lnum_face_id)
21006 && !it->pt_lnum)
21008 ptrdiff_t ignored;
21009 if (PT_BYTE > it->lnum_bytepos && !EQ (Vdisplay_line_numbers, Qvisual))
21010 it->pt_lnum =
21011 this_line + display_count_lines_logically (it->lnum_bytepos, PT_BYTE,
21012 PT, &ignored);
21013 else
21014 it->pt_lnum = display_count_lines_logically (beg_byte, PT_BYTE, PT,
21015 &ignored);
21017 /* Compute the required width if needed. */
21018 if (!it->lnum_width)
21020 if (NATNUMP (Vdisplay_line_numbers_width))
21021 it->lnum_width = XFASTINT (Vdisplay_line_numbers_width);
21023 /* Max line number to be displayed cannot be more than the one
21024 corresponding to the last row of the desired matrix. */
21025 ptrdiff_t max_lnum;
21027 if (NILP (Vdisplay_line_numbers_current_absolute)
21028 && (EQ (Vdisplay_line_numbers, Qrelative)
21029 || EQ (Vdisplay_line_numbers, Qvisual)))
21030 /* We subtract one more because the current line is always
21031 zero in this mode. */
21032 max_lnum = it->w->desired_matrix->nrows - 2;
21033 else if (EQ (Vdisplay_line_numbers, Qvisual))
21034 max_lnum = it->pt_lnum + it->w->desired_matrix->nrows - 1;
21035 else
21036 max_lnum = this_line + it->w->desired_matrix->nrows - 1 - it->vpos;
21037 max_lnum = max (1, max_lnum);
21038 it->lnum_width = max (it->lnum_width, log10 (max_lnum) + 1);
21039 eassert (it->lnum_width > 0);
21041 if (EQ (Vdisplay_line_numbers, Qrelative))
21042 lnum_offset = it->pt_lnum;
21043 else if (EQ (Vdisplay_line_numbers, Qvisual))
21044 lnum_offset = 0;
21046 /* Under 'relative', display the absolute line number for the
21047 current line, unless the user requests otherwise. */
21048 ptrdiff_t lnum_to_display = eabs (this_line - lnum_offset);
21049 if ((EQ (Vdisplay_line_numbers, Qrelative)
21050 || EQ (Vdisplay_line_numbers, Qvisual))
21051 && lnum_to_display == 0
21052 && !NILP (Vdisplay_line_numbers_current_absolute))
21053 lnum_to_display = it->pt_lnum + 1;
21054 /* In L2R rows we need to append the blank separator, in R2L
21055 rows we need to prepend it. But this function is usually
21056 called when no display elements were produced from the
21057 following line, so the paragraph direction might be unknown.
21058 Therefore we cheat and add 2 blanks, one on either side. */
21059 pint2str (lnum_buf, it->lnum_width + 1, lnum_to_display);
21060 strcat (lnum_buf, " ");
21062 /* Setup for producing the glyphs. */
21063 init_iterator (&tem_it, it->w, -1, -1, &scratch_glyph_row,
21064 /* FIXME: Use specialized face. */
21065 DEFAULT_FACE_ID);
21066 scratch_glyph_row.reversed_p = false;
21067 scratch_glyph_row.used[TEXT_AREA] = 0;
21068 SET_TEXT_POS (tem_it.position, 0, 0);
21069 tem_it.avoid_cursor_p = true;
21070 tem_it.bidi_p = true;
21071 tem_it.bidi_it.type = WEAK_EN;
21072 /* According to UAX#9, EN goes up 2 levels in L2R paragraph and
21073 1 level in R2L paragraphs. Emulate that, assuming we are in
21074 an L2R paragraph. */
21075 tem_it.bidi_it.resolved_level = 2;
21077 /* Produce glyphs for the line number in a scratch glyph_row. */
21078 int n_glyphs_before;
21079 for (const char *p = lnum_buf; *p; p++)
21081 /* For continuation lines and lines after ZV, instead of a line
21082 number, produce a blank prefix of the same width. Use the
21083 default face for the blank field beyond ZV. */
21084 if (beyond_zv)
21085 tem_it.face_id = it->base_face_id;
21086 else if (lnum_face_id != current_lnum_face_id
21087 && (EQ (Vdisplay_line_numbers, Qvisual)
21088 ? this_line == 0
21089 : this_line == it->pt_lnum))
21090 tem_it.face_id = current_lnum_face_id;
21091 else
21092 tem_it.face_id = lnum_face_id;
21093 if (beyond_zv
21094 /* Don't display the same line number more than once. */
21095 || (!EQ (Vdisplay_line_numbers, Qvisual)
21096 && (it->continuation_lines_width > 0
21097 || (this_line == last_line && !first_time))))
21098 tem_it.c = tem_it.char_to_display = ' ';
21099 else
21100 tem_it.c = tem_it.char_to_display = *p;
21101 tem_it.len = 1;
21102 n_glyphs_before = scratch_glyph_row.used[TEXT_AREA];
21103 /* Make sure these glyphs will have a "position" of -1. */
21104 SET_TEXT_POS (tem_it.position, -1, -1);
21105 PRODUCE_GLYPHS (&tem_it);
21107 /* Stop producing glyphs if we don't have enough space on
21108 this line. FIXME: should we refrain from producing the
21109 line number at all in that case? */
21110 if (tem_it.current_x > tem_it.last_visible_x)
21112 scratch_glyph_row.used[TEXT_AREA] = n_glyphs_before;
21113 break;
21117 /* Record the width in pixels we need for the line number display. */
21118 it->lnum_pixel_width = tem_it.current_x;
21119 /* Copy the produced glyphs into IT's glyph_row. */
21120 struct glyph *g = scratch_glyph_row.glyphs[TEXT_AREA];
21121 struct glyph *e = g + scratch_glyph_row.used[TEXT_AREA];
21122 struct glyph *p = it->glyph_row ? it->glyph_row->glyphs[TEXT_AREA] : NULL;
21123 short *u = it->glyph_row ? &it->glyph_row->used[TEXT_AREA] : NULL;
21125 eassert (it->glyph_row == NULL || it->glyph_row->used[TEXT_AREA] == 0);
21127 for ( ; g < e; g++)
21129 it->current_x += g->pixel_width;
21130 /* The following is important when this function is called
21131 from move_it_in_display_line_to: HPOS is incremented only
21132 when we are in the visible portion of the glyph row. */
21133 if (it->current_x > it->first_visible_x)
21134 it->hpos++;
21135 if (p)
21137 *p++ = *g;
21138 (*u)++;
21142 /* Update IT's metrics due to glyphs produced for line numbers. */
21143 if (it->glyph_row)
21145 struct glyph_row *row = it->glyph_row;
21147 it->max_ascent = max (row->ascent, tem_it.max_ascent);
21148 it->max_descent = max (row->height - row->ascent, tem_it.max_descent);
21149 it->max_phys_ascent = max (row->phys_ascent, tem_it.max_phys_ascent);
21150 it->max_phys_descent = max (row->phys_height - row->phys_ascent,
21151 tem_it.max_phys_descent);
21153 else
21155 it->max_ascent = max (it->max_ascent, tem_it.max_ascent);
21156 it->max_descent = max (it->max_descent, tem_it.max_descent);
21157 it->max_phys_ascent = max (it->max_phys_ascent, tem_it.max_phys_ascent);
21158 it->max_phys_descent = max (it->max_phys_descent, tem_it.max_phys_descent);
21161 bidi_unshelve_cache (itdata, false);
21164 /* Return true if this glyph row needs a line number to be produced
21165 for it. */
21166 static bool
21167 should_produce_line_number (struct it *it)
21169 if (NILP (Vdisplay_line_numbers))
21170 return false;
21172 /* Don't display line numbers in minibuffer windows. */
21173 if (MINI_WINDOW_P (it->w))
21174 return false;
21176 #ifdef HAVE_WINDOW_SYSTEM
21177 /* Don't display line number in tooltip frames. */
21178 if (FRAMEP (tip_frame) && EQ (WINDOW_FRAME (it->w), tip_frame)
21179 #ifdef USE_GTK
21180 /* GTK builds store in tip_frame the frame that shows the tip,
21181 so we need an additional test. */
21182 && !NILP (Fframe_parameter (tip_frame, Qtooltip))
21183 #endif
21185 return false;
21186 #endif
21188 /* If the character at current position has a non-nil special
21189 property, disable line numbers for this row. This is for
21190 packages such as company-mode, which need this for their tricky
21191 layout, where line numbers get in the way. */
21192 Lisp_Object val = Fget_char_property (make_number (IT_CHARPOS (*it)),
21193 Qdisplay_line_numbers_disable,
21194 it->window);
21195 /* For ZV, we need to also look in empty overlays at that point,
21196 because get-char-property always returns nil for ZV, except if
21197 the property is in 'default-text-properties'. */
21198 if (NILP (val) && IT_CHARPOS (*it) >= ZV)
21199 val = disable_line_numbers_overlay_at_eob ();
21200 return NILP (val) ? true : false;
21203 /* Return true if ROW has no glyphs except those inserted by the
21204 display engine. This is needed for indicate-empty-lines and
21205 similar features when the glyph row starts with glyphs which didn't
21206 come from buffer or string. */
21207 static bool
21208 row_text_area_empty (struct glyph_row *row)
21210 if (!row->reversed_p)
21212 for (struct glyph *g = row->glyphs[TEXT_AREA];
21213 g < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
21214 g++)
21215 if (!NILP (g->object) || g->charpos > 0)
21216 return false;
21218 else
21220 for (struct glyph *g = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
21221 g > row->glyphs[TEXT_AREA];
21222 g--)
21223 if (!NILP ((g - 1)->object) || (g - 1)->charpos > 0)
21224 return false;
21227 return true;
21230 /* Construct the glyph row IT->glyph_row in the desired matrix of
21231 IT->w from text at the current position of IT. See dispextern.h
21232 for an overview of struct it. Value is true if
21233 IT->glyph_row displays text, as opposed to a line displaying ZV
21234 only. CURSOR_VPOS is the window-relative vertical position of
21235 the glyph row displaying the cursor, or -1 if unknown. */
21237 static bool
21238 display_line (struct it *it, int cursor_vpos)
21240 struct glyph_row *row = it->glyph_row;
21241 Lisp_Object overlay_arrow_string;
21242 struct it wrap_it;
21243 void *wrap_data = NULL;
21244 bool may_wrap = false;
21245 int wrap_x UNINIT;
21246 int wrap_row_used = -1;
21247 int wrap_row_ascent UNINIT, wrap_row_height UNINIT;
21248 int wrap_row_phys_ascent UNINIT, wrap_row_phys_height UNINIT;
21249 int wrap_row_extra_line_spacing UNINIT;
21250 ptrdiff_t wrap_row_min_pos UNINIT, wrap_row_min_bpos UNINIT;
21251 ptrdiff_t wrap_row_max_pos UNINIT, wrap_row_max_bpos UNINIT;
21252 int cvpos;
21253 ptrdiff_t min_pos = ZV + 1, max_pos = 0;
21254 ptrdiff_t min_bpos UNINIT, max_bpos UNINIT;
21255 bool pending_handle_line_prefix = false;
21256 int header_line = window_wants_header_line (it->w);
21257 bool hscroll_this_line = (cursor_vpos >= 0
21258 && it->vpos == cursor_vpos - header_line
21259 && hscrolling_current_line_p (it->w));
21260 int first_visible_x = it->first_visible_x;
21261 int last_visible_x = it->last_visible_x;
21262 int x_incr = 0;
21264 /* We always start displaying at hpos zero even if hscrolled. */
21265 eassert (it->hpos == 0 && it->current_x == 0);
21267 if (MATRIX_ROW_VPOS (row, it->w->desired_matrix)
21268 >= it->w->desired_matrix->nrows)
21270 it->w->nrows_scale_factor++;
21271 it->f->fonts_changed = true;
21272 return false;
21275 /* Clear the result glyph row and enable it. */
21276 prepare_desired_row (it->w, row, false);
21278 row->y = it->current_y;
21279 row->start = it->start;
21280 row->continuation_lines_width = it->continuation_lines_width;
21281 row->displays_text_p = true;
21282 row->starts_in_middle_of_char_p = it->starts_in_middle_of_char_p;
21283 it->starts_in_middle_of_char_p = false;
21285 /* Arrange the overlays nicely for our purposes. Usually, we call
21286 display_line on only one line at a time, in which case this
21287 can't really hurt too much, or we call it on lines which appear
21288 one after another in the buffer, in which case all calls to
21289 recenter_overlay_lists but the first will be pretty cheap. */
21290 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
21292 /* If we are going to display the cursor's line, account for the
21293 hscroll of that line. We subtract the window's min_hscroll,
21294 because that was already accounted for in init_iterator. */
21295 if (hscroll_this_line)
21296 x_incr =
21297 (window_hscroll_limited (it->w, it->f) - it->w->min_hscroll)
21298 * FRAME_COLUMN_WIDTH (it->f);
21300 bool line_number_needed = should_produce_line_number (it);
21302 /* Move over display elements that are not visible because we are
21303 hscrolled. This may stop at an x-position < first_visible_x
21304 if the first glyph is partially visible or if we hit a line end. */
21305 if (it->current_x < it->first_visible_x + x_incr)
21307 enum move_it_result move_result;
21309 this_line_min_pos = row->start.pos;
21310 if (hscroll_this_line)
21312 it->first_visible_x += x_incr;
21313 it->last_visible_x += x_incr;
21315 move_result = move_it_in_display_line_to (it, ZV, it->first_visible_x,
21316 MOVE_TO_POS | MOVE_TO_X);
21317 /* If we are under a large hscroll, move_it_in_display_line_to
21318 could hit the end of the line without reaching
21319 first_visible_x. Pretend that we did reach it. This is
21320 especially important on a TTY, where we will call
21321 extend_face_to_end_of_line, which needs to know how many
21322 blank glyphs to produce. */
21323 if (it->current_x < it->first_visible_x
21324 && (move_result == MOVE_NEWLINE_OR_CR
21325 || move_result == MOVE_POS_MATCH_OR_ZV))
21326 it->current_x = it->first_visible_x;
21328 /* Record the smallest positions seen while we moved over
21329 display elements that are not visible. This is needed by
21330 redisplay_internal for optimizing the case where the cursor
21331 stays inside the same line. The rest of this function only
21332 considers positions that are actually displayed, so
21333 RECORD_MAX_MIN_POS will not otherwise record positions that
21334 are hscrolled to the left of the left edge of the window. */
21335 min_pos = CHARPOS (this_line_min_pos);
21336 min_bpos = BYTEPOS (this_line_min_pos);
21338 /* Produce line number, if needed. */
21339 if (line_number_needed)
21340 maybe_produce_line_number (it);
21342 else if (it->area == TEXT_AREA)
21344 /* Line numbers should precede the line-prefix or wrap-prefix. */
21345 if (line_number_needed)
21346 maybe_produce_line_number (it);
21348 /* We only do this when not calling move_it_in_display_line_to
21349 above, because that function calls itself handle_line_prefix. */
21350 handle_line_prefix (it);
21352 else
21354 /* Line-prefix and wrap-prefix are always displayed in the text
21355 area. But if this is the first call to display_line after
21356 init_iterator, the iterator might have been set up to write
21357 into a marginal area, e.g. if the line begins with some
21358 display property that writes to the margins. So we need to
21359 wait with the call to handle_line_prefix until whatever
21360 writes to the margin has done its job. */
21361 pending_handle_line_prefix = true;
21364 /* Get the initial row height. This is either the height of the
21365 text hscrolled, if there is any, or zero. */
21366 row->ascent = it->max_ascent;
21367 row->height = it->max_ascent + it->max_descent;
21368 row->phys_ascent = it->max_phys_ascent;
21369 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
21370 row->extra_line_spacing = it->max_extra_line_spacing;
21372 /* Utility macro to record max and min buffer positions seen until now. */
21373 #define RECORD_MAX_MIN_POS(IT) \
21374 do \
21376 bool composition_p \
21377 = !STRINGP ((IT)->string) && ((IT)->what == IT_COMPOSITION); \
21378 ptrdiff_t current_pos = \
21379 composition_p ? (IT)->cmp_it.charpos \
21380 : IT_CHARPOS (*(IT)); \
21381 ptrdiff_t current_bpos = \
21382 composition_p ? CHAR_TO_BYTE (current_pos) \
21383 : IT_BYTEPOS (*(IT)); \
21384 if (current_pos < min_pos) \
21386 min_pos = current_pos; \
21387 min_bpos = current_bpos; \
21389 if (IT_CHARPOS (*it) > max_pos) \
21391 max_pos = IT_CHARPOS (*it); \
21392 max_bpos = IT_BYTEPOS (*it); \
21395 while (false)
21397 /* Loop generating characters. The loop is left with IT on the next
21398 character to display. */
21399 while (true)
21401 int n_glyphs_before, hpos_before, x_before;
21402 int x, nglyphs;
21403 int ascent = 0, descent = 0, phys_ascent = 0, phys_descent = 0;
21405 /* Retrieve the next thing to display. Value is false if end of
21406 buffer reached. */
21407 if (!get_next_display_element (it))
21409 bool row_has_glyphs = false;
21410 /* Maybe add a space at the end of this line that is used to
21411 display the cursor there under X. Set the charpos of the
21412 first glyph of blank lines not corresponding to any text
21413 to -1. */
21414 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
21415 row->exact_window_width_line_p = true;
21416 else if ((append_space_for_newline (it, true)
21417 && row->used[TEXT_AREA] == 1)
21418 || row->used[TEXT_AREA] == 0
21419 || (row_has_glyphs = row_text_area_empty (row)))
21421 row->glyphs[TEXT_AREA]->charpos = -1;
21422 /* Don't reset the displays_text_p flag if we are
21423 displaying line numbers or line-prefix. */
21424 if (!row_has_glyphs)
21425 row->displays_text_p = false;
21427 if (!NILP (BVAR (XBUFFER (it->w->contents), indicate_empty_lines))
21428 && (!MINI_WINDOW_P (it->w)))
21429 row->indicate_empty_line_p = true;
21432 it->continuation_lines_width = 0;
21433 /* Reset those iterator values set from display property
21434 values. This is for the case when the display property
21435 ends at ZV, and is not a replacing property, so pop_it is
21436 not called. */
21437 it->font_height = Qnil;
21438 it->voffset = 0;
21439 row->ends_at_zv_p = true;
21440 /* A row that displays right-to-left text must always have
21441 its last face extended all the way to the end of line,
21442 even if this row ends in ZV, because we still write to
21443 the screen left to right. We also need to extend the
21444 last face if the default face is remapped to some
21445 different face, otherwise the functions that clear
21446 portions of the screen will clear with the default face's
21447 background color. */
21448 if (row->reversed_p
21449 || lookup_basic_face (it->f, DEFAULT_FACE_ID) != DEFAULT_FACE_ID)
21450 extend_face_to_end_of_line (it);
21451 break;
21454 /* Now, get the metrics of what we want to display. This also
21455 generates glyphs in `row' (which is IT->glyph_row). */
21456 n_glyphs_before = row->used[TEXT_AREA];
21457 x = it->current_x;
21459 /* Remember the line height so far in case the next element doesn't
21460 fit on the line. */
21461 if (it->line_wrap != TRUNCATE)
21463 ascent = it->max_ascent;
21464 descent = it->max_descent;
21465 phys_ascent = it->max_phys_ascent;
21466 phys_descent = it->max_phys_descent;
21468 if (it->line_wrap == WORD_WRAP && it->area == TEXT_AREA)
21470 if (IT_DISPLAYING_WHITESPACE (it))
21471 may_wrap = true;
21472 else if (may_wrap)
21474 SAVE_IT (wrap_it, *it, wrap_data);
21475 wrap_x = x;
21476 wrap_row_used = row->used[TEXT_AREA];
21477 wrap_row_ascent = row->ascent;
21478 wrap_row_height = row->height;
21479 wrap_row_phys_ascent = row->phys_ascent;
21480 wrap_row_phys_height = row->phys_height;
21481 wrap_row_extra_line_spacing = row->extra_line_spacing;
21482 wrap_row_min_pos = min_pos;
21483 wrap_row_min_bpos = min_bpos;
21484 wrap_row_max_pos = max_pos;
21485 wrap_row_max_bpos = max_bpos;
21486 may_wrap = false;
21491 PRODUCE_GLYPHS (it);
21493 /* If this display element was in marginal areas, continue with
21494 the next one. */
21495 if (it->area != TEXT_AREA)
21497 row->ascent = max (row->ascent, it->max_ascent);
21498 row->height = max (row->height, it->max_ascent + it->max_descent);
21499 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
21500 row->phys_height = max (row->phys_height,
21501 it->max_phys_ascent + it->max_phys_descent);
21502 row->extra_line_spacing = max (row->extra_line_spacing,
21503 it->max_extra_line_spacing);
21504 set_iterator_to_next (it, true);
21505 /* If we didn't handle the line/wrap prefix above, and the
21506 call to set_iterator_to_next just switched to TEXT_AREA,
21507 process the prefix now. */
21508 if (it->area == TEXT_AREA && pending_handle_line_prefix)
21510 /* Line numbers should precede the line-prefix or wrap-prefix. */
21511 if (line_number_needed)
21512 maybe_produce_line_number (it);
21514 pending_handle_line_prefix = false;
21515 handle_line_prefix (it);
21517 continue;
21520 /* Does the display element fit on the line? If we truncate
21521 lines, we should draw past the right edge of the window. If
21522 we don't truncate, we want to stop so that we can display the
21523 continuation glyph before the right margin. If lines are
21524 continued, there are two possible strategies for characters
21525 resulting in more than 1 glyph (e.g. tabs): Display as many
21526 glyphs as possible in this line and leave the rest for the
21527 continuation line, or display the whole element in the next
21528 line. Original redisplay did the former, so we do it also. */
21529 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
21530 hpos_before = it->hpos;
21531 x_before = x;
21533 if (/* Not a newline. */
21534 nglyphs > 0
21535 /* Glyphs produced fit entirely in the line. */
21536 && it->current_x < it->last_visible_x)
21538 it->hpos += nglyphs;
21539 row->ascent = max (row->ascent, it->max_ascent);
21540 row->height = max (row->height, it->max_ascent + it->max_descent);
21541 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
21542 row->phys_height = max (row->phys_height,
21543 it->max_phys_ascent + it->max_phys_descent);
21544 row->extra_line_spacing = max (row->extra_line_spacing,
21545 it->max_extra_line_spacing);
21546 if (it->current_x - it->pixel_width < it->first_visible_x
21547 /* In R2L rows, we arrange in extend_face_to_end_of_line
21548 to add a right offset to the line, by a suitable
21549 change to the stretch glyph that is the leftmost
21550 glyph of the line. */
21551 && !row->reversed_p)
21552 row->x = x - it->first_visible_x;
21553 /* Record the maximum and minimum buffer positions seen so
21554 far in glyphs that will be displayed by this row. */
21555 if (it->bidi_p)
21556 RECORD_MAX_MIN_POS (it);
21558 else
21560 int i, new_x;
21561 struct glyph *glyph;
21563 for (i = 0; i < nglyphs; ++i, x = new_x)
21565 /* Identify the glyphs added by the last call to
21566 PRODUCE_GLYPHS. In R2L rows, they are prepended to
21567 the previous glyphs. */
21568 if (!row->reversed_p)
21569 glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
21570 else
21571 glyph = row->glyphs[TEXT_AREA] + nglyphs - 1 - i;
21572 new_x = x + glyph->pixel_width;
21574 if (/* Lines are continued. */
21575 it->line_wrap != TRUNCATE
21576 && (/* Glyph doesn't fit on the line. */
21577 new_x > it->last_visible_x
21578 /* Or it fits exactly on a window system frame. */
21579 || (new_x == it->last_visible_x
21580 && FRAME_WINDOW_P (it->f)
21581 && (row->reversed_p
21582 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
21583 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)))))
21585 /* End of a continued line. */
21587 if (it->hpos == 0
21588 || (new_x == it->last_visible_x
21589 && FRAME_WINDOW_P (it->f)
21590 && (row->reversed_p
21591 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
21592 : WINDOW_RIGHT_FRINGE_WIDTH (it->w))))
21594 /* Current glyph is the only one on the line or
21595 fits exactly on the line. We must continue
21596 the line because we can't draw the cursor
21597 after the glyph. */
21598 row->continued_p = true;
21599 it->current_x = new_x;
21600 it->continuation_lines_width += new_x;
21601 ++it->hpos;
21602 if (i == nglyphs - 1)
21604 /* If line-wrap is on, check if a previous
21605 wrap point was found. */
21606 if (!IT_OVERFLOW_NEWLINE_INTO_FRINGE (it)
21607 && wrap_row_used > 0
21608 /* Even if there is a previous wrap
21609 point, continue the line here as
21610 usual, if (i) the previous character
21611 was a space or tab AND (ii) the
21612 current character is not. */
21613 && (!may_wrap
21614 || IT_DISPLAYING_WHITESPACE (it)))
21615 goto back_to_wrap;
21617 /* Record the maximum and minimum buffer
21618 positions seen so far in glyphs that will be
21619 displayed by this row. */
21620 if (it->bidi_p)
21621 RECORD_MAX_MIN_POS (it);
21622 set_iterator_to_next (it, true);
21623 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
21625 if (!get_next_display_element (it))
21627 row->exact_window_width_line_p = true;
21628 it->continuation_lines_width = 0;
21629 it->font_height = Qnil;
21630 it->voffset = 0;
21631 row->continued_p = false;
21632 row->ends_at_zv_p = true;
21634 else if (ITERATOR_AT_END_OF_LINE_P (it))
21636 row->continued_p = false;
21637 row->exact_window_width_line_p = true;
21639 /* If line-wrap is on, check if a
21640 previous wrap point was found. */
21641 else if (wrap_row_used > 0
21642 /* Even if there is a previous wrap
21643 point, continue the line here as
21644 usual, if (i) the previous character
21645 was a space or tab AND (ii) the
21646 current character is not. */
21647 && (!may_wrap
21648 || IT_DISPLAYING_WHITESPACE (it)))
21649 goto back_to_wrap;
21653 else if (it->bidi_p)
21654 RECORD_MAX_MIN_POS (it);
21655 if (WINDOW_LEFT_MARGIN_WIDTH (it->w) > 0
21656 || WINDOW_RIGHT_MARGIN_WIDTH (it->w) > 0)
21657 extend_face_to_end_of_line (it);
21659 else if (CHAR_GLYPH_PADDING_P (*glyph)
21660 && !FRAME_WINDOW_P (it->f))
21662 /* A padding glyph that doesn't fit on this line.
21663 This means the whole character doesn't fit
21664 on the line. */
21665 if (row->reversed_p)
21666 unproduce_glyphs (it, row->used[TEXT_AREA]
21667 - n_glyphs_before);
21668 row->used[TEXT_AREA] = n_glyphs_before;
21670 /* Fill the rest of the row with continuation
21671 glyphs like in 20.x. */
21672 while (row->glyphs[TEXT_AREA] + row->used[TEXT_AREA]
21673 < row->glyphs[1 + TEXT_AREA])
21674 produce_special_glyphs (it, IT_CONTINUATION);
21676 row->continued_p = true;
21677 it->current_x = x_before;
21678 it->continuation_lines_width += x_before;
21680 /* Restore the height to what it was before the
21681 element not fitting on the line. */
21682 it->max_ascent = ascent;
21683 it->max_descent = descent;
21684 it->max_phys_ascent = phys_ascent;
21685 it->max_phys_descent = phys_descent;
21686 if (WINDOW_LEFT_MARGIN_WIDTH (it->w) > 0
21687 || WINDOW_RIGHT_MARGIN_WIDTH (it->w) > 0)
21688 extend_face_to_end_of_line (it);
21690 else if (wrap_row_used > 0)
21692 back_to_wrap:
21693 if (row->reversed_p)
21694 unproduce_glyphs (it,
21695 row->used[TEXT_AREA] - wrap_row_used);
21696 RESTORE_IT (it, &wrap_it, wrap_data);
21697 it->continuation_lines_width += wrap_x;
21698 row->used[TEXT_AREA] = wrap_row_used;
21699 row->ascent = wrap_row_ascent;
21700 row->height = wrap_row_height;
21701 row->phys_ascent = wrap_row_phys_ascent;
21702 row->phys_height = wrap_row_phys_height;
21703 row->extra_line_spacing = wrap_row_extra_line_spacing;
21704 min_pos = wrap_row_min_pos;
21705 min_bpos = wrap_row_min_bpos;
21706 max_pos = wrap_row_max_pos;
21707 max_bpos = wrap_row_max_bpos;
21708 row->continued_p = true;
21709 row->ends_at_zv_p = false;
21710 row->exact_window_width_line_p = false;
21712 /* Make sure that a non-default face is extended
21713 up to the right margin of the window. */
21714 extend_face_to_end_of_line (it);
21716 else if ((it->what == IT_CHARACTER
21717 || it->what == IT_STRETCH
21718 || it->what == IT_COMPOSITION)
21719 && it->c == '\t' && FRAME_WINDOW_P (it->f))
21721 /* A TAB that extends past the right edge of the
21722 window. This produces a single glyph on
21723 window system frames. We leave the glyph in
21724 this row and let it fill the row, but don't
21725 consume the TAB. */
21726 if ((row->reversed_p
21727 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
21728 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
21729 produce_special_glyphs (it, IT_CONTINUATION);
21730 it->continuation_lines_width += it->last_visible_x;
21731 row->ends_in_middle_of_char_p = true;
21732 row->continued_p = true;
21733 glyph->pixel_width = it->last_visible_x - x;
21734 it->starts_in_middle_of_char_p = true;
21735 if (WINDOW_LEFT_MARGIN_WIDTH (it->w) > 0
21736 || WINDOW_RIGHT_MARGIN_WIDTH (it->w) > 0)
21737 extend_face_to_end_of_line (it);
21739 else
21741 /* Something other than a TAB that draws past
21742 the right edge of the window. Restore
21743 positions to values before the element. */
21744 if (row->reversed_p)
21745 unproduce_glyphs (it, row->used[TEXT_AREA]
21746 - (n_glyphs_before + i));
21747 row->used[TEXT_AREA] = n_glyphs_before + i;
21749 /* Display continuation glyphs. */
21750 it->current_x = x_before;
21751 it->continuation_lines_width += x;
21752 if (!FRAME_WINDOW_P (it->f)
21753 || (row->reversed_p
21754 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
21755 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
21756 produce_special_glyphs (it, IT_CONTINUATION);
21757 row->continued_p = true;
21759 extend_face_to_end_of_line (it);
21761 if (nglyphs > 1 && i > 0)
21763 row->ends_in_middle_of_char_p = true;
21764 it->starts_in_middle_of_char_p = true;
21767 /* Restore the height to what it was before the
21768 element not fitting on the line. */
21769 it->max_ascent = ascent;
21770 it->max_descent = descent;
21771 it->max_phys_ascent = phys_ascent;
21772 it->max_phys_descent = phys_descent;
21775 break;
21777 else if (new_x > it->first_visible_x)
21779 /* Increment number of glyphs actually displayed. */
21780 ++it->hpos;
21782 /* Record the maximum and minimum buffer positions
21783 seen so far in glyphs that will be displayed by
21784 this row. */
21785 if (it->bidi_p)
21786 RECORD_MAX_MIN_POS (it);
21788 if (x < it->first_visible_x && !row->reversed_p)
21789 /* Glyph is partially visible, i.e. row starts at
21790 negative X position. Don't do that in R2L
21791 rows, where we arrange to add a right offset to
21792 the line in extend_face_to_end_of_line, by a
21793 suitable change to the stretch glyph that is
21794 the leftmost glyph of the line. */
21795 row->x = x - it->first_visible_x;
21796 /* When the last glyph of an R2L row only fits
21797 partially on the line, we need to set row->x to a
21798 negative offset, so that the leftmost glyph is
21799 the one that is partially visible. But if we are
21800 going to produce the truncation glyph, this will
21801 be taken care of in produce_special_glyphs. */
21802 if (row->reversed_p
21803 && new_x > it->last_visible_x
21804 && !(it->line_wrap == TRUNCATE
21805 && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0))
21807 eassert (FRAME_WINDOW_P (it->f));
21808 row->x = it->last_visible_x - new_x;
21811 else
21813 /* Glyph is completely off the left margin of the
21814 window. This should not happen because of the
21815 move_it_in_display_line at the start of this
21816 function, unless the text display area of the
21817 window is empty. */
21818 eassert (it->first_visible_x <= it->last_visible_x);
21821 /* Even if this display element produced no glyphs at all,
21822 we want to record its position. */
21823 if (it->bidi_p && nglyphs == 0)
21824 RECORD_MAX_MIN_POS (it);
21826 row->ascent = max (row->ascent, it->max_ascent);
21827 row->height = max (row->height, it->max_ascent + it->max_descent);
21828 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
21829 row->phys_height = max (row->phys_height,
21830 it->max_phys_ascent + it->max_phys_descent);
21831 row->extra_line_spacing = max (row->extra_line_spacing,
21832 it->max_extra_line_spacing);
21834 /* End of this display line if row is continued. */
21835 if (row->continued_p || row->ends_at_zv_p)
21836 break;
21839 at_end_of_line:
21840 /* Is this a line end? If yes, we're also done, after making
21841 sure that a non-default face is extended up to the right
21842 margin of the window. */
21843 if (ITERATOR_AT_END_OF_LINE_P (it))
21845 int used_before = row->used[TEXT_AREA];
21847 row->ends_in_newline_from_string_p = STRINGP (it->object);
21849 /* Add a space at the end of the line that is used to
21850 display the cursor there. */
21851 if (!IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
21852 append_space_for_newline (it, false);
21854 /* Extend the face to the end of the line. */
21855 extend_face_to_end_of_line (it);
21857 /* Make sure we have the position. */
21858 if (used_before == 0)
21859 row->glyphs[TEXT_AREA]->charpos = CHARPOS (it->position);
21861 /* Record the position of the newline, for use in
21862 find_row_edges. */
21863 it->eol_pos = it->current.pos;
21865 /* Consume the line end. This skips over invisible lines. */
21866 set_iterator_to_next (it, true);
21867 it->continuation_lines_width = 0;
21868 break;
21871 /* Proceed with next display element. Note that this skips
21872 over lines invisible because of selective display. */
21873 set_iterator_to_next (it, true);
21875 /* If we truncate lines, we are done when the last displayed
21876 glyphs reach past the right margin of the window. */
21877 if (it->line_wrap == TRUNCATE
21878 && ((FRAME_WINDOW_P (it->f)
21879 /* Images are preprocessed in produce_image_glyph such
21880 that they are cropped at the right edge of the
21881 window, so an image glyph will always end exactly at
21882 last_visible_x, even if there's no right fringe. */
21883 && ((row->reversed_p
21884 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
21885 : WINDOW_RIGHT_FRINGE_WIDTH (it->w))
21886 || it->what == IT_IMAGE))
21887 ? (it->current_x >= it->last_visible_x)
21888 : (it->current_x > it->last_visible_x)))
21890 /* Maybe add truncation glyphs. */
21891 if (!FRAME_WINDOW_P (it->f)
21892 || (row->reversed_p
21893 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
21894 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
21896 int i, n;
21898 if (!row->reversed_p)
21900 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
21901 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
21902 break;
21904 else
21906 for (i = 0; i < row->used[TEXT_AREA]; i++)
21907 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
21908 break;
21909 /* Remove any padding glyphs at the front of ROW, to
21910 make room for the truncation glyphs we will be
21911 adding below. The loop below always inserts at
21912 least one truncation glyph, so also remove the
21913 last glyph added to ROW. */
21914 unproduce_glyphs (it, i + 1);
21915 /* Adjust i for the loop below. */
21916 i = row->used[TEXT_AREA] - (i + 1);
21919 /* produce_special_glyphs overwrites the last glyph, so
21920 we don't want that if we want to keep that last
21921 glyph, which means it's an image. */
21922 if (it->current_x > it->last_visible_x)
21924 it->current_x = x_before;
21925 if (!FRAME_WINDOW_P (it->f))
21927 for (n = row->used[TEXT_AREA]; i < n; ++i)
21929 row->used[TEXT_AREA] = i;
21930 produce_special_glyphs (it, IT_TRUNCATION);
21933 else
21935 row->used[TEXT_AREA] = i;
21936 produce_special_glyphs (it, IT_TRUNCATION);
21938 it->hpos = hpos_before;
21941 else if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
21943 /* Don't truncate if we can overflow newline into fringe. */
21944 if (!get_next_display_element (it))
21946 it->continuation_lines_width = 0;
21947 it->font_height = Qnil;
21948 it->voffset = 0;
21949 row->ends_at_zv_p = true;
21950 row->exact_window_width_line_p = true;
21951 break;
21953 if (ITERATOR_AT_END_OF_LINE_P (it))
21955 row->exact_window_width_line_p = true;
21956 goto at_end_of_line;
21958 it->current_x = x_before;
21959 it->hpos = hpos_before;
21962 row->truncated_on_right_p = true;
21963 it->continuation_lines_width = 0;
21964 reseat_at_next_visible_line_start (it, false);
21965 /* We insist below that IT's position be at ZV because in
21966 bidi-reordered lines the character at visible line start
21967 might not be the character that follows the newline in
21968 the logical order. */
21969 if (IT_BYTEPOS (*it) > BEG_BYTE)
21970 row->ends_at_zv_p =
21971 IT_BYTEPOS (*it) >= ZV_BYTE && FETCH_BYTE (ZV_BYTE - 1) != '\n';
21972 else
21973 row->ends_at_zv_p = false;
21974 break;
21978 if (wrap_data)
21979 bidi_unshelve_cache (wrap_data, true);
21981 /* If line is not empty and hscrolled, maybe insert truncation glyphs
21982 at the left window margin. */
21983 if (it->first_visible_x
21984 && IT_CHARPOS (*it) != CHARPOS (row->start.pos))
21986 if (!FRAME_WINDOW_P (it->f)
21987 || (((row->reversed_p
21988 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
21989 : WINDOW_LEFT_FRINGE_WIDTH (it->w)) == 0)
21990 /* Don't let insert_left_trunc_glyphs overwrite the
21991 first glyph of the row if it is an image. */
21992 && row->glyphs[TEXT_AREA]->type != IMAGE_GLYPH))
21993 insert_left_trunc_glyphs (it);
21994 row->truncated_on_left_p = true;
21997 /* Remember the position at which this line ends.
21999 BIDI Note: any code that needs MATRIX_ROW_START/END_CHARPOS
22000 cannot be before the call to find_row_edges below, since that is
22001 where these positions are determined. */
22002 row->end = it->current;
22003 if (!it->bidi_p)
22005 row->minpos = row->start.pos;
22006 row->maxpos = row->end.pos;
22008 else
22010 /* ROW->minpos and ROW->maxpos must be the smallest and
22011 `1 + the largest' buffer positions in ROW. But if ROW was
22012 bidi-reordered, these two positions can be anywhere in the
22013 row, so we must determine them now. */
22014 find_row_edges (it, row, min_pos, min_bpos, max_pos, max_bpos);
22017 /* If the start of this line is the overlay arrow-position, then
22018 mark this glyph row as the one containing the overlay arrow.
22019 This is clearly a mess with variable size fonts. It would be
22020 better to let it be displayed like cursors under X. */
22021 if ((MATRIX_ROW_DISPLAYS_TEXT_P (row) || !overlay_arrow_seen)
22022 && (overlay_arrow_string = overlay_arrow_at_row (it, row),
22023 !NILP (overlay_arrow_string)))
22025 /* Overlay arrow in window redisplay is a fringe bitmap. */
22026 if (STRINGP (overlay_arrow_string))
22028 struct glyph_row *arrow_row
22029 = get_overlay_arrow_glyph_row (it->w, overlay_arrow_string);
22030 struct glyph *glyph = arrow_row->glyphs[TEXT_AREA];
22031 struct glyph *arrow_end = glyph + arrow_row->used[TEXT_AREA];
22032 struct glyph *p = row->glyphs[TEXT_AREA];
22033 struct glyph *p2, *end;
22035 /* Copy the arrow glyphs. */
22036 while (glyph < arrow_end)
22037 *p++ = *glyph++;
22039 /* Throw away padding glyphs. */
22040 p2 = p;
22041 end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
22042 while (p2 < end && CHAR_GLYPH_PADDING_P (*p2))
22043 ++p2;
22044 if (p2 > p)
22046 while (p2 < end)
22047 *p++ = *p2++;
22048 row->used[TEXT_AREA] = p2 - row->glyphs[TEXT_AREA];
22051 else
22053 eassert (INTEGERP (overlay_arrow_string));
22054 row->overlay_arrow_bitmap = XINT (overlay_arrow_string);
22056 overlay_arrow_seen = true;
22059 /* Highlight trailing whitespace. */
22060 if (!NILP (Vshow_trailing_whitespace))
22061 highlight_trailing_whitespace (it->f, it->glyph_row);
22063 /* Compute pixel dimensions of this line. */
22064 compute_line_metrics (it);
22066 /* Implementation note: No changes in the glyphs of ROW or in their
22067 faces can be done past this point, because compute_line_metrics
22068 computes ROW's hash value and stores it within the glyph_row
22069 structure. */
22071 /* Record whether this row ends inside an ellipsis. */
22072 row->ends_in_ellipsis_p
22073 = (it->method == GET_FROM_DISPLAY_VECTOR
22074 && it->ellipsis_p);
22076 /* Save fringe bitmaps in this row. */
22077 row->left_user_fringe_bitmap = it->left_user_fringe_bitmap;
22078 row->left_user_fringe_face_id = it->left_user_fringe_face_id;
22079 row->right_user_fringe_bitmap = it->right_user_fringe_bitmap;
22080 row->right_user_fringe_face_id = it->right_user_fringe_face_id;
22082 it->left_user_fringe_bitmap = 0;
22083 it->left_user_fringe_face_id = 0;
22084 it->right_user_fringe_bitmap = 0;
22085 it->right_user_fringe_face_id = 0;
22087 /* Maybe set the cursor. */
22088 cvpos = it->w->cursor.vpos;
22089 if ((cvpos < 0
22090 /* In bidi-reordered rows, keep checking for proper cursor
22091 position even if one has been found already, because buffer
22092 positions in such rows change non-linearly with ROW->VPOS,
22093 when a line is continued. One exception: when we are at ZV,
22094 display cursor on the first suitable glyph row, since all
22095 the empty rows after that also have their position set to ZV. */
22096 /* FIXME: Revisit this when glyph ``spilling'' in continuation
22097 lines' rows is implemented for bidi-reordered rows. */
22098 || (it->bidi_p
22099 && !MATRIX_ROW (it->w->desired_matrix, cvpos)->ends_at_zv_p))
22100 && PT >= MATRIX_ROW_START_CHARPOS (row)
22101 && PT <= MATRIX_ROW_END_CHARPOS (row)
22102 && cursor_row_p (row))
22103 set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
22105 /* Prepare for the next line. This line starts horizontally at (X
22106 HPOS) = (0 0). Vertical positions are incremented. As a
22107 convenience for the caller, IT->glyph_row is set to the next
22108 row to be used. */
22109 it->current_x = it->hpos = 0;
22110 it->current_y += row->height;
22111 /* Restore the first and last visible X if we adjusted them for
22112 current-line hscrolling. */
22113 if (hscroll_this_line)
22115 it->first_visible_x = first_visible_x;
22116 it->last_visible_x = last_visible_x;
22118 SET_TEXT_POS (it->eol_pos, 0, 0);
22119 ++it->vpos;
22120 ++it->glyph_row;
22121 /* The next row should by default use the same value of the
22122 reversed_p flag as this one. set_iterator_to_next decides when
22123 it's a new paragraph, and PRODUCE_GLYPHS recomputes the value of
22124 the flag accordingly. */
22125 if (it->glyph_row < MATRIX_BOTTOM_TEXT_ROW (it->w->desired_matrix, it->w))
22126 it->glyph_row->reversed_p = row->reversed_p;
22127 it->start = row->end;
22128 return MATRIX_ROW_DISPLAYS_TEXT_P (row);
22130 #undef RECORD_MAX_MIN_POS
22133 DEFUN ("current-bidi-paragraph-direction", Fcurrent_bidi_paragraph_direction,
22134 Scurrent_bidi_paragraph_direction, 0, 1, 0,
22135 doc: /* Return paragraph direction at point in BUFFER.
22136 Value is either `left-to-right' or `right-to-left'.
22137 If BUFFER is omitted or nil, it defaults to the current buffer.
22139 Paragraph direction determines how the text in the paragraph is displayed.
22140 In left-to-right paragraphs, text begins at the left margin of the window
22141 and the reading direction is generally left to right. In right-to-left
22142 paragraphs, text begins at the right margin and is read from right to left.
22144 See also `bidi-paragraph-direction'. */)
22145 (Lisp_Object buffer)
22147 struct buffer *buf = current_buffer;
22148 struct buffer *old = buf;
22150 if (! NILP (buffer))
22152 CHECK_BUFFER (buffer);
22153 buf = XBUFFER (buffer);
22156 if (NILP (BVAR (buf, bidi_display_reordering))
22157 || NILP (BVAR (buf, enable_multibyte_characters))
22158 /* When we are loading loadup.el, the character property tables
22159 needed for bidi iteration are not yet available. */
22160 || redisplay__inhibit_bidi)
22161 return Qleft_to_right;
22162 else if (!NILP (BVAR (buf, bidi_paragraph_direction)))
22163 return BVAR (buf, bidi_paragraph_direction);
22164 else
22166 /* Determine the direction from buffer text. We could try to
22167 use current_matrix if it is up to date, but this seems fast
22168 enough as it is. */
22169 struct bidi_it itb;
22170 ptrdiff_t pos = BUF_PT (buf);
22171 ptrdiff_t bytepos = BUF_PT_BYTE (buf);
22172 int c;
22173 void *itb_data = bidi_shelve_cache ();
22175 set_buffer_temp (buf);
22176 /* bidi_paragraph_init finds the base direction of the paragraph
22177 by searching forward from paragraph start. We need the base
22178 direction of the current or _previous_ paragraph, so we need
22179 to make sure we are within that paragraph. To that end, find
22180 the previous non-empty line. */
22181 if (pos >= ZV && pos > BEGV)
22182 DEC_BOTH (pos, bytepos);
22183 AUTO_STRING (trailing_white_space, "[\f\t ]*\n");
22184 if (fast_looking_at (trailing_white_space,
22185 pos, bytepos, ZV, ZV_BYTE, Qnil) > 0)
22187 while ((c = FETCH_BYTE (bytepos)) == '\n'
22188 || c == ' ' || c == '\t' || c == '\f')
22190 if (bytepos <= BEGV_BYTE)
22191 break;
22192 bytepos--;
22193 pos--;
22195 while (!CHAR_HEAD_P (FETCH_BYTE (bytepos)))
22196 bytepos--;
22198 bidi_init_it (pos, bytepos, FRAME_WINDOW_P (SELECTED_FRAME ()), &itb);
22199 itb.paragraph_dir = NEUTRAL_DIR;
22200 itb.string.s = NULL;
22201 itb.string.lstring = Qnil;
22202 itb.string.bufpos = 0;
22203 itb.string.from_disp_str = false;
22204 itb.string.unibyte = false;
22205 /* We have no window to use here for ignoring window-specific
22206 overlays. Using NULL for window pointer will cause
22207 compute_display_string_pos to use the current buffer. */
22208 itb.w = NULL;
22209 bidi_paragraph_init (NEUTRAL_DIR, &itb, true);
22210 bidi_unshelve_cache (itb_data, false);
22211 set_buffer_temp (old);
22212 switch (itb.paragraph_dir)
22214 case L2R:
22215 return Qleft_to_right;
22216 break;
22217 case R2L:
22218 return Qright_to_left;
22219 break;
22220 default:
22221 emacs_abort ();
22226 DEFUN ("bidi-find-overridden-directionality",
22227 Fbidi_find_overridden_directionality,
22228 Sbidi_find_overridden_directionality, 2, 3, 0,
22229 doc: /* Return position between FROM and TO where directionality was overridden.
22231 This function returns the first character position in the specified
22232 region of OBJECT where there is a character whose `bidi-class' property
22233 is `L', but which was forced to display as `R' by a directional
22234 override, and likewise with characters whose `bidi-class' is `R'
22235 or `AL' that were forced to display as `L'.
22237 If no such character is found, the function returns nil.
22239 OBJECT is a Lisp string or buffer to search for overridden
22240 directionality, and defaults to the current buffer if nil or omitted.
22241 OBJECT can also be a window, in which case the function will search
22242 the buffer displayed in that window. Passing the window instead of
22243 a buffer is preferable when the buffer is displayed in some window,
22244 because this function will then be able to correctly account for
22245 window-specific overlays, which can affect the results.
22247 Strong directional characters `L', `R', and `AL' can have their
22248 intrinsic directionality overridden by directional override
22249 control characters RLO (u+202e) and LRO (u+202d). See the
22250 function `get-char-code-property' for a way to inquire about
22251 the `bidi-class' property of a character. */)
22252 (Lisp_Object from, Lisp_Object to, Lisp_Object object)
22254 struct buffer *buf = current_buffer;
22255 struct buffer *old = buf;
22256 struct window *w = NULL;
22257 bool frame_window_p = FRAME_WINDOW_P (SELECTED_FRAME ());
22258 struct bidi_it itb;
22259 ptrdiff_t from_pos, to_pos, from_bpos;
22260 void *itb_data;
22262 if (!NILP (object))
22264 if (BUFFERP (object))
22265 buf = XBUFFER (object);
22266 else if (WINDOWP (object))
22268 w = decode_live_window (object);
22269 buf = XBUFFER (w->contents);
22270 frame_window_p = FRAME_WINDOW_P (XFRAME (w->frame));
22272 else
22273 CHECK_STRING (object);
22276 if (STRINGP (object))
22278 /* Characters in unibyte strings are always treated by bidi.c as
22279 strong LTR. */
22280 if (!STRING_MULTIBYTE (object)
22281 /* When we are loading loadup.el, the character property
22282 tables needed for bidi iteration are not yet
22283 available. */
22284 || redisplay__inhibit_bidi)
22285 return Qnil;
22287 validate_subarray (object, from, to, SCHARS (object), &from_pos, &to_pos);
22288 if (from_pos >= SCHARS (object))
22289 return Qnil;
22291 /* Set up the bidi iterator. */
22292 itb_data = bidi_shelve_cache ();
22293 itb.paragraph_dir = NEUTRAL_DIR;
22294 itb.string.lstring = object;
22295 itb.string.s = NULL;
22296 itb.string.schars = SCHARS (object);
22297 itb.string.bufpos = 0;
22298 itb.string.from_disp_str = false;
22299 itb.string.unibyte = false;
22300 itb.w = w;
22301 bidi_init_it (0, 0, frame_window_p, &itb);
22303 else
22305 /* Nothing this fancy can happen in unibyte buffers, or in a
22306 buffer that disabled reordering, or if FROM is at EOB. */
22307 if (NILP (BVAR (buf, bidi_display_reordering))
22308 || NILP (BVAR (buf, enable_multibyte_characters))
22309 /* When we are loading loadup.el, the character property
22310 tables needed for bidi iteration are not yet
22311 available. */
22312 || redisplay__inhibit_bidi)
22313 return Qnil;
22315 set_buffer_temp (buf);
22316 validate_region (&from, &to);
22317 from_pos = XINT (from);
22318 to_pos = XINT (to);
22319 if (from_pos >= ZV)
22320 return Qnil;
22322 /* Set up the bidi iterator. */
22323 itb_data = bidi_shelve_cache ();
22324 from_bpos = CHAR_TO_BYTE (from_pos);
22325 if (from_pos == BEGV)
22327 itb.charpos = BEGV;
22328 itb.bytepos = BEGV_BYTE;
22330 else if (FETCH_CHAR (from_bpos - 1) == '\n')
22332 itb.charpos = from_pos;
22333 itb.bytepos = from_bpos;
22335 else
22336 itb.charpos = find_newline_no_quit (from_pos, CHAR_TO_BYTE (from_pos),
22337 -1, &itb.bytepos);
22338 itb.paragraph_dir = NEUTRAL_DIR;
22339 itb.string.s = NULL;
22340 itb.string.lstring = Qnil;
22341 itb.string.bufpos = 0;
22342 itb.string.from_disp_str = false;
22343 itb.string.unibyte = false;
22344 itb.w = w;
22345 bidi_init_it (itb.charpos, itb.bytepos, frame_window_p, &itb);
22348 ptrdiff_t found;
22349 do {
22350 /* For the purposes of this function, the actual base direction of
22351 the paragraph doesn't matter, so just set it to L2R. */
22352 bidi_paragraph_init (L2R, &itb, false);
22353 while ((found = bidi_find_first_overridden (&itb)) < from_pos)
22355 } while (found == ZV && itb.ch == '\n' && itb.charpos < to_pos);
22357 bidi_unshelve_cache (itb_data, false);
22358 set_buffer_temp (old);
22360 return (from_pos <= found && found < to_pos) ? make_number (found) : Qnil;
22363 DEFUN ("move-point-visually", Fmove_point_visually,
22364 Smove_point_visually, 1, 1, 0,
22365 doc: /* Move point in the visual order in the specified DIRECTION.
22366 DIRECTION can be 1, meaning move to the right, or -1, which moves to the
22367 left.
22369 Value is the new character position of point. */)
22370 (Lisp_Object direction)
22372 struct window *w = XWINDOW (selected_window);
22373 struct buffer *b = XBUFFER (w->contents);
22374 struct glyph_row *row;
22375 int dir;
22376 Lisp_Object paragraph_dir;
22378 #define ROW_GLYPH_NEWLINE_P(ROW,GLYPH) \
22379 (!(ROW)->continued_p \
22380 && NILP ((GLYPH)->object) \
22381 && (GLYPH)->type == CHAR_GLYPH \
22382 && (GLYPH)->u.ch == ' ' \
22383 && (GLYPH)->charpos >= 0 \
22384 && !(GLYPH)->avoid_cursor_p)
22386 CHECK_NUMBER (direction);
22387 dir = XINT (direction);
22388 if (dir > 0)
22389 dir = 1;
22390 else
22391 dir = -1;
22393 /* If current matrix is up-to-date, we can use the information
22394 recorded in the glyphs, at least as long as the goal is on the
22395 screen. */
22396 if (w->window_end_valid
22397 && !windows_or_buffers_changed
22398 && b
22399 && !b->clip_changed
22400 && !b->prevent_redisplay_optimizations_p
22401 && !window_outdated (w)
22402 /* We rely below on the cursor coordinates to be up to date, but
22403 we cannot trust them if some command moved point since the
22404 last complete redisplay. */
22405 && w->last_point == BUF_PT (b)
22406 && w->cursor.vpos >= 0
22407 && w->cursor.vpos < w->current_matrix->nrows
22408 && (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos))->enabled_p)
22410 struct glyph *g = row->glyphs[TEXT_AREA];
22411 struct glyph *e = dir > 0 ? g + row->used[TEXT_AREA] : g - 1;
22412 struct glyph *gpt = g + w->cursor.hpos;
22414 for (g = gpt + dir; (dir > 0 ? g < e : g > e); g += dir)
22416 if (BUFFERP (g->object) && g->charpos != PT)
22418 SET_PT (g->charpos);
22419 w->cursor.vpos = -1;
22420 return make_number (PT);
22422 else if (!NILP (g->object) && !EQ (g->object, gpt->object))
22424 ptrdiff_t new_pos;
22426 if (BUFFERP (gpt->object))
22428 new_pos = PT;
22429 if ((gpt->resolved_level - row->reversed_p) % 2 == 0)
22430 new_pos += (row->reversed_p ? -dir : dir);
22431 else
22432 new_pos -= (row->reversed_p ? -dir : dir);
22434 else if (BUFFERP (g->object))
22435 new_pos = g->charpos;
22436 else
22437 break;
22438 SET_PT (new_pos);
22439 w->cursor.vpos = -1;
22440 return make_number (PT);
22442 else if (ROW_GLYPH_NEWLINE_P (row, g))
22444 /* Glyphs inserted at the end of a non-empty line for
22445 positioning the cursor have zero charpos, so we must
22446 deduce the value of point by other means. */
22447 if (g->charpos > 0)
22448 SET_PT (g->charpos);
22449 else if (row->ends_at_zv_p && PT != ZV)
22450 SET_PT (ZV);
22451 else if (PT != MATRIX_ROW_END_CHARPOS (row) - 1)
22452 SET_PT (MATRIX_ROW_END_CHARPOS (row) - 1);
22453 else
22454 break;
22455 w->cursor.vpos = -1;
22456 return make_number (PT);
22459 if (g == e || NILP (g->object))
22461 if (row->truncated_on_left_p || row->truncated_on_right_p)
22462 goto simulate_display;
22463 if (!row->reversed_p)
22464 row += dir;
22465 else
22466 row -= dir;
22467 if (!(MATRIX_FIRST_TEXT_ROW (w->current_matrix) <= row
22468 && row < MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w)))
22469 goto simulate_display;
22471 if (dir > 0)
22473 if (row->reversed_p && !row->continued_p)
22475 SET_PT (MATRIX_ROW_END_CHARPOS (row) - 1);
22476 w->cursor.vpos = -1;
22477 return make_number (PT);
22479 g = row->glyphs[TEXT_AREA];
22480 e = g + row->used[TEXT_AREA];
22481 for ( ; g < e; g++)
22483 if (BUFFERP (g->object)
22484 /* Empty lines have only one glyph, which stands
22485 for the newline, and whose charpos is the
22486 buffer position of the newline. */
22487 || ROW_GLYPH_NEWLINE_P (row, g)
22488 /* When the buffer ends in a newline, the line at
22489 EOB also has one glyph, but its charpos is -1. */
22490 || (row->ends_at_zv_p
22491 && !row->reversed_p
22492 && NILP (g->object)
22493 && g->type == CHAR_GLYPH
22494 && g->u.ch == ' '))
22496 if (g->charpos > 0)
22497 SET_PT (g->charpos);
22498 else if (!row->reversed_p
22499 && row->ends_at_zv_p
22500 && PT != ZV)
22501 SET_PT (ZV);
22502 else
22503 continue;
22504 w->cursor.vpos = -1;
22505 return make_number (PT);
22509 else
22511 if (!row->reversed_p && !row->continued_p)
22513 SET_PT (MATRIX_ROW_END_CHARPOS (row) - 1);
22514 w->cursor.vpos = -1;
22515 return make_number (PT);
22517 e = row->glyphs[TEXT_AREA];
22518 g = e + row->used[TEXT_AREA] - 1;
22519 for ( ; g >= e; g--)
22521 if (BUFFERP (g->object)
22522 || (ROW_GLYPH_NEWLINE_P (row, g)
22523 && g->charpos > 0)
22524 /* Empty R2L lines on GUI frames have the buffer
22525 position of the newline stored in the stretch
22526 glyph. */
22527 || g->type == STRETCH_GLYPH
22528 || (row->ends_at_zv_p
22529 && row->reversed_p
22530 && NILP (g->object)
22531 && g->type == CHAR_GLYPH
22532 && g->u.ch == ' '))
22534 if (g->charpos > 0)
22535 SET_PT (g->charpos);
22536 else if (row->reversed_p
22537 && row->ends_at_zv_p
22538 && PT != ZV)
22539 SET_PT (ZV);
22540 else
22541 continue;
22542 w->cursor.vpos = -1;
22543 return make_number (PT);
22550 simulate_display:
22552 /* If we wind up here, we failed to move by using the glyphs, so we
22553 need to simulate display instead. */
22555 if (b)
22556 paragraph_dir = Fcurrent_bidi_paragraph_direction (w->contents);
22557 else
22558 paragraph_dir = Qleft_to_right;
22559 if (EQ (paragraph_dir, Qright_to_left))
22560 dir = -dir;
22561 if (PT <= BEGV && dir < 0)
22562 xsignal0 (Qbeginning_of_buffer);
22563 else if (PT >= ZV && dir > 0)
22564 xsignal0 (Qend_of_buffer);
22565 else
22567 struct text_pos pt;
22568 struct it it;
22569 int pt_x, target_x, pixel_width, pt_vpos;
22570 bool at_eol_p;
22571 bool overshoot_expected = false;
22572 bool target_is_eol_p = false;
22574 /* Setup the arena. */
22575 SET_TEXT_POS (pt, PT, PT_BYTE);
22576 start_display (&it, w, pt);
22577 /* When lines are truncated, we could be called with point
22578 outside of the windows edges, in which case move_it_*
22579 functions either prematurely stop at window's edge or jump to
22580 the next screen line, whereas we rely below on our ability to
22581 reach point, in order to start from its X coordinate. So we
22582 need to disregard the window's horizontal extent in that case. */
22583 if (it.line_wrap == TRUNCATE)
22584 it.last_visible_x = DISP_INFINITY;
22586 if (it.cmp_it.id < 0
22587 && it.method == GET_FROM_STRING
22588 && it.area == TEXT_AREA
22589 && it.string_from_display_prop_p
22590 && (it.sp > 0 && it.stack[it.sp - 1].method == GET_FROM_BUFFER))
22591 overshoot_expected = true;
22593 /* Find the X coordinate of point. We start from the beginning
22594 of this or previous line to make sure we are before point in
22595 the logical order (since the move_it_* functions can only
22596 move forward). */
22597 reseat:
22598 reseat_at_previous_visible_line_start (&it);
22599 it.current_x = it.hpos = it.current_y = it.vpos = 0;
22600 if (IT_CHARPOS (it) != PT)
22602 move_it_to (&it, overshoot_expected ? PT - 1 : PT,
22603 -1, -1, -1, MOVE_TO_POS);
22604 /* If we missed point because the character there is
22605 displayed out of a display vector that has more than one
22606 glyph, retry expecting overshoot. */
22607 if (it.method == GET_FROM_DISPLAY_VECTOR
22608 && it.current.dpvec_index > 0
22609 && !overshoot_expected)
22611 overshoot_expected = true;
22612 goto reseat;
22614 else if (IT_CHARPOS (it) != PT && !overshoot_expected)
22615 move_it_in_display_line (&it, PT, -1, MOVE_TO_POS);
22617 pt_x = it.current_x;
22618 pt_vpos = it.vpos;
22619 if (dir > 0 || overshoot_expected)
22621 struct glyph_row *row = it.glyph_row;
22623 /* When point is at beginning of line, we don't have
22624 information about the glyph there loaded into struct
22625 it. Calling get_next_display_element fixes that. */
22626 if (pt_x == 0)
22627 get_next_display_element (&it);
22628 at_eol_p = ITERATOR_AT_END_OF_LINE_P (&it);
22629 it.glyph_row = NULL;
22630 PRODUCE_GLYPHS (&it); /* compute it.pixel_width */
22631 it.glyph_row = row;
22632 /* PRODUCE_GLYPHS advances it.current_x, so we must restore
22633 it, lest it will become out of sync with it's buffer
22634 position. */
22635 it.current_x = pt_x;
22637 else
22638 at_eol_p = ITERATOR_AT_END_OF_LINE_P (&it);
22639 pixel_width = it.pixel_width;
22640 if (overshoot_expected && at_eol_p)
22641 pixel_width = 0;
22642 else if (pixel_width <= 0)
22643 pixel_width = 1;
22645 /* If there's a display string (or something similar) at point,
22646 we are actually at the glyph to the left of point, so we need
22647 to correct the X coordinate. */
22648 if (overshoot_expected)
22650 if (it.bidi_p)
22651 pt_x += pixel_width * it.bidi_it.scan_dir;
22652 else
22653 pt_x += pixel_width;
22656 /* Compute target X coordinate, either to the left or to the
22657 right of point. On TTY frames, all characters have the same
22658 pixel width of 1, so we can use that. On GUI frames we don't
22659 have an easy way of getting at the pixel width of the
22660 character to the left of point, so we use a different method
22661 of getting to that place. */
22662 if (dir > 0)
22663 target_x = pt_x + pixel_width;
22664 else
22665 target_x = pt_x - (!FRAME_WINDOW_P (it.f)) * pixel_width;
22667 /* Target X coordinate could be one line above or below the line
22668 of point, in which case we need to adjust the target X
22669 coordinate. Also, if moving to the left, we need to begin at
22670 the left edge of the point's screen line. */
22671 if (dir < 0)
22673 if (pt_x > 0)
22675 start_display (&it, w, pt);
22676 if (it.line_wrap == TRUNCATE)
22677 it.last_visible_x = DISP_INFINITY;
22678 reseat_at_previous_visible_line_start (&it);
22679 it.current_x = it.current_y = it.hpos = 0;
22680 if (pt_vpos != 0)
22681 move_it_by_lines (&it, pt_vpos);
22683 else
22685 move_it_by_lines (&it, -1);
22686 target_x = it.last_visible_x - !FRAME_WINDOW_P (it.f);
22687 target_is_eol_p = true;
22688 /* Under word-wrap, we don't know the x coordinate of
22689 the last character displayed on the previous line,
22690 which immediately precedes the wrap point. To find
22691 out its x coordinate, we try moving to the right
22692 margin of the window, which will stop at the wrap
22693 point, and then reset target_x to point at the
22694 character that precedes the wrap point. This is not
22695 needed on GUI frames, because (see below) there we
22696 move from the left margin one grapheme cluster at a
22697 time, and stop when we hit the wrap point. */
22698 if (!FRAME_WINDOW_P (it.f) && it.line_wrap == WORD_WRAP)
22700 void *it_data = NULL;
22701 struct it it2;
22703 SAVE_IT (it2, it, it_data);
22704 move_it_in_display_line_to (&it, ZV, target_x,
22705 MOVE_TO_POS | MOVE_TO_X);
22706 /* If we arrived at target_x, that _is_ the last
22707 character on the previous line. */
22708 if (it.current_x != target_x)
22709 target_x = it.current_x - 1;
22710 RESTORE_IT (&it, &it2, it_data);
22714 else
22716 if (at_eol_p
22717 || (target_x >= it.last_visible_x
22718 && it.line_wrap != TRUNCATE))
22720 if (pt_x > 0)
22721 move_it_by_lines (&it, 0);
22722 move_it_by_lines (&it, 1);
22723 target_x = 0;
22727 /* Move to the target X coordinate. */
22728 /* On GUI frames, as we don't know the X coordinate of the
22729 character to the left of point, moving point to the left
22730 requires walking, one grapheme cluster at a time, until we
22731 find ourself at a place immediately to the left of the
22732 character at point. */
22733 if (FRAME_WINDOW_P (it.f) && dir < 0)
22735 struct text_pos new_pos;
22736 enum move_it_result rc = MOVE_X_REACHED;
22738 if (it.current_x == 0)
22739 get_next_display_element (&it);
22740 if (it.what == IT_COMPOSITION)
22742 new_pos.charpos = it.cmp_it.charpos;
22743 new_pos.bytepos = -1;
22745 else
22746 new_pos = it.current.pos;
22748 while (it.current_x + it.pixel_width <= target_x
22749 && (rc == MOVE_X_REACHED
22750 /* Under word-wrap, move_it_in_display_line_to
22751 stops at correct coordinates, but sometimes
22752 returns MOVE_POS_MATCH_OR_ZV. */
22753 || (it.line_wrap == WORD_WRAP
22754 && rc == MOVE_POS_MATCH_OR_ZV)))
22756 int new_x = it.current_x + it.pixel_width;
22758 /* For composed characters, we want the position of the
22759 first character in the grapheme cluster (usually, the
22760 composition's base character), whereas it.current
22761 might give us the position of the _last_ one, e.g. if
22762 the composition is rendered in reverse due to bidi
22763 reordering. */
22764 if (it.what == IT_COMPOSITION)
22766 new_pos.charpos = it.cmp_it.charpos;
22767 new_pos.bytepos = -1;
22769 else
22770 new_pos = it.current.pos;
22771 if (new_x == it.current_x)
22772 new_x++;
22773 rc = move_it_in_display_line_to (&it, ZV, new_x,
22774 MOVE_TO_POS | MOVE_TO_X);
22775 if (ITERATOR_AT_END_OF_LINE_P (&it) && !target_is_eol_p)
22776 break;
22778 /* The previous position we saw in the loop is the one we
22779 want. */
22780 if (new_pos.bytepos == -1)
22781 new_pos.bytepos = CHAR_TO_BYTE (new_pos.charpos);
22782 it.current.pos = new_pos;
22784 else if (it.current_x != target_x)
22785 move_it_in_display_line_to (&it, ZV, target_x, MOVE_TO_POS | MOVE_TO_X);
22787 /* If we ended up in a display string that covers point, move to
22788 buffer position to the right in the visual order. */
22789 if (dir > 0)
22791 while (IT_CHARPOS (it) == PT)
22793 set_iterator_to_next (&it, false);
22794 if (!get_next_display_element (&it))
22795 break;
22799 /* Move point to that position. */
22800 SET_PT_BOTH (IT_CHARPOS (it), IT_BYTEPOS (it));
22803 return make_number (PT);
22805 #undef ROW_GLYPH_NEWLINE_P
22808 DEFUN ("bidi-resolved-levels", Fbidi_resolved_levels,
22809 Sbidi_resolved_levels, 0, 1, 0,
22810 doc: /* Return the resolved bidirectional levels of characters at VPOS.
22812 The resolved levels are produced by the Emacs bidi reordering engine
22813 that implements the UBA, the Unicode Bidirectional Algorithm. Please
22814 read the Unicode Standard Annex 9 (UAX#9) for background information
22815 about these levels.
22817 VPOS is the zero-based number of the current window's screen line
22818 for which to produce the resolved levels. If VPOS is nil or omitted,
22819 it defaults to the screen line of point. If the window displays a
22820 header line, VPOS of zero will report on the header line, and first
22821 line of text in the window will have VPOS of 1.
22823 Value is an array of resolved levels, indexed by glyph number.
22824 Glyphs are numbered from zero starting from the beginning of the
22825 screen line, i.e. the left edge of the window for left-to-right lines
22826 and from the right edge for right-to-left lines. The resolved levels
22827 are produced only for the window's text area; text in display margins
22828 is not included.
22830 If the selected window's display is not up-to-date, or if the specified
22831 screen line does not display text, this function returns nil. It is
22832 highly recommended to bind this function to some simple key, like F8,
22833 in order to avoid these problems.
22835 This function exists mainly for testing the correctness of the
22836 Emacs UBA implementation, in particular with the test suite. */)
22837 (Lisp_Object vpos)
22839 struct window *w = XWINDOW (selected_window);
22840 struct buffer *b = XBUFFER (w->contents);
22841 int nrow;
22842 struct glyph_row *row;
22844 if (NILP (vpos))
22846 int d1, d2, d3, d4, d5;
22848 pos_visible_p (w, PT, &d1, &d2, &d3, &d4, &d5, &nrow);
22850 else
22852 CHECK_NUMBER_COERCE_MARKER (vpos);
22853 nrow = XINT (vpos);
22856 /* We require up-to-date glyph matrix for this window. */
22857 if (w->window_end_valid
22858 && !windows_or_buffers_changed
22859 && b
22860 && !b->clip_changed
22861 && !b->prevent_redisplay_optimizations_p
22862 && !window_outdated (w)
22863 && nrow >= 0
22864 && nrow < w->current_matrix->nrows
22865 && (row = MATRIX_ROW (w->current_matrix, nrow))->enabled_p
22866 && MATRIX_ROW_DISPLAYS_TEXT_P (row))
22868 struct glyph *g, *e, *g1;
22869 int nglyphs, i;
22870 Lisp_Object levels;
22872 if (!row->reversed_p) /* Left-to-right glyph row. */
22874 g = g1 = row->glyphs[TEXT_AREA];
22875 e = g + row->used[TEXT_AREA];
22877 /* Skip over glyphs at the start of the row that was
22878 generated by redisplay for its own needs. */
22879 while (g < e
22880 && NILP (g->object)
22881 && g->charpos < 0)
22882 g++;
22883 g1 = g;
22885 /* Count the "interesting" glyphs in this row. */
22886 for (nglyphs = 0; g < e && !NILP (g->object); g++)
22887 nglyphs++;
22889 /* Create and fill the array. */
22890 levels = make_uninit_vector (nglyphs);
22891 for (i = 0; g1 < g; i++, g1++)
22892 ASET (levels, i, make_number (g1->resolved_level));
22894 else /* Right-to-left glyph row. */
22896 g = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
22897 e = row->glyphs[TEXT_AREA] - 1;
22898 while (g > e
22899 && NILP (g->object)
22900 && g->charpos < 0)
22901 g--;
22902 g1 = g;
22903 for (nglyphs = 0; g > e && !NILP (g->object); g--)
22904 nglyphs++;
22905 levels = make_uninit_vector (nglyphs);
22906 for (i = 0; g1 > g; i++, g1--)
22907 ASET (levels, i, make_number (g1->resolved_level));
22909 return levels;
22911 else
22912 return Qnil;
22917 /***********************************************************************
22918 Menu Bar
22919 ***********************************************************************/
22921 /* Redisplay the menu bar in the frame for window W.
22923 The menu bar of X frames that don't have X toolkit support is
22924 displayed in a special window W->frame->menu_bar_window.
22926 The menu bar of terminal frames is treated specially as far as
22927 glyph matrices are concerned. Menu bar lines are not part of
22928 windows, so the update is done directly on the frame matrix rows
22929 for the menu bar. */
22931 static void
22932 display_menu_bar (struct window *w)
22934 struct frame *f = XFRAME (WINDOW_FRAME (w));
22935 struct it it;
22936 Lisp_Object items;
22937 int i;
22939 /* Don't do all this for graphical frames. */
22940 #ifdef HAVE_NTGUI
22941 if (FRAME_W32_P (f))
22942 return;
22943 #endif
22944 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
22945 if (FRAME_X_P (f))
22946 return;
22947 #endif
22949 #ifdef HAVE_NS
22950 if (FRAME_NS_P (f))
22951 return;
22952 #endif /* HAVE_NS */
22954 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
22955 eassert (!FRAME_WINDOW_P (f));
22956 init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MENU_FACE_ID);
22957 it.first_visible_x = 0;
22958 it.last_visible_x = FRAME_PIXEL_WIDTH (f);
22959 #elif defined (HAVE_X_WINDOWS) /* X without toolkit. */
22960 if (FRAME_WINDOW_P (f))
22962 /* Menu bar lines are displayed in the desired matrix of the
22963 dummy window menu_bar_window. */
22964 struct window *menu_w;
22965 menu_w = XWINDOW (f->menu_bar_window);
22966 init_iterator (&it, menu_w, -1, -1, menu_w->desired_matrix->rows,
22967 MENU_FACE_ID);
22968 it.first_visible_x = 0;
22969 it.last_visible_x = FRAME_PIXEL_WIDTH (f);
22971 else
22972 #endif /* not USE_X_TOOLKIT and not USE_GTK */
22974 /* This is a TTY frame, i.e. character hpos/vpos are used as
22975 pixel x/y. */
22976 init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
22977 MENU_FACE_ID);
22978 it.first_visible_x = 0;
22979 it.last_visible_x = FRAME_COLS (f);
22982 /* FIXME: This should be controlled by a user option. See the
22983 comments in redisplay_tool_bar and display_mode_line about
22984 this. */
22985 it.paragraph_embedding = L2R;
22987 /* Clear all rows of the menu bar. */
22988 for (i = 0; i < FRAME_MENU_BAR_LINES (f); ++i)
22990 struct glyph_row *row = it.glyph_row + i;
22991 clear_glyph_row (row);
22992 row->enabled_p = true;
22993 row->full_width_p = true;
22994 row->reversed_p = false;
22997 /* Display all items of the menu bar. */
22998 items = FRAME_MENU_BAR_ITEMS (it.f);
22999 for (i = 0; i < ASIZE (items); i += 4)
23001 Lisp_Object string;
23003 /* Stop at nil string. */
23004 string = AREF (items, i + 1);
23005 if (NILP (string))
23006 break;
23008 /* Remember where item was displayed. */
23009 ASET (items, i + 3, make_number (it.hpos));
23011 /* Display the item, pad with one space. */
23012 if (it.current_x < it.last_visible_x)
23013 display_string (NULL, string, Qnil, 0, 0, &it,
23014 SCHARS (string) + 1, 0, 0, -1);
23017 /* Fill out the line with spaces. */
23018 if (it.current_x < it.last_visible_x)
23019 display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
23021 /* Compute the total height of the lines. */
23022 compute_line_metrics (&it);
23025 /* Deep copy of a glyph row, including the glyphs. */
23026 static void
23027 deep_copy_glyph_row (struct glyph_row *to, struct glyph_row *from)
23029 struct glyph *pointers[1 + LAST_AREA];
23030 int to_used = to->used[TEXT_AREA];
23032 /* Save glyph pointers of TO. */
23033 memcpy (pointers, to->glyphs, sizeof to->glyphs);
23035 /* Do a structure assignment. */
23036 *to = *from;
23038 /* Restore original glyph pointers of TO. */
23039 memcpy (to->glyphs, pointers, sizeof to->glyphs);
23041 /* Copy the glyphs. */
23042 memcpy (to->glyphs[TEXT_AREA], from->glyphs[TEXT_AREA],
23043 min (from->used[TEXT_AREA], to_used) * sizeof (struct glyph));
23045 /* If we filled only part of the TO row, fill the rest with
23046 space_glyph (which will display as empty space). */
23047 if (to_used > from->used[TEXT_AREA])
23048 fill_up_frame_row_with_spaces (to, to_used);
23051 /* Display one menu item on a TTY, by overwriting the glyphs in the
23052 frame F's desired glyph matrix with glyphs produced from the menu
23053 item text. Called from term.c to display TTY drop-down menus one
23054 item at a time.
23056 ITEM_TEXT is the menu item text as a C string.
23058 FACE_ID is the face ID to be used for this menu item. FACE_ID
23059 could specify one of 3 faces: a face for an enabled item, a face
23060 for a disabled item, or a face for a selected item.
23062 X and Y are coordinates of the first glyph in the frame's desired
23063 matrix to be overwritten by the menu item. Since this is a TTY, Y
23064 is the zero-based number of the glyph row and X is the zero-based
23065 glyph number in the row, starting from left, where to start
23066 displaying the item.
23068 SUBMENU means this menu item drops down a submenu, which
23069 should be indicated by displaying a proper visual cue after the
23070 item text. */
23072 void
23073 display_tty_menu_item (const char *item_text, int width, int face_id,
23074 int x, int y, bool submenu)
23076 struct it it;
23077 struct frame *f = SELECTED_FRAME ();
23078 struct window *w = XWINDOW (f->selected_window);
23079 struct glyph_row *row;
23080 size_t item_len = strlen (item_text);
23082 eassert (FRAME_TERMCAP_P (f));
23084 /* Don't write beyond the matrix's last row. This can happen for
23085 TTY screens that are not high enough to show the entire menu.
23086 (This is actually a bit of defensive programming, as
23087 tty_menu_display already limits the number of menu items to one
23088 less than the number of screen lines.) */
23089 if (y >= f->desired_matrix->nrows)
23090 return;
23092 init_iterator (&it, w, -1, -1, f->desired_matrix->rows + y, MENU_FACE_ID);
23093 it.first_visible_x = 0;
23094 it.last_visible_x = FRAME_COLS (f) - 1;
23095 row = it.glyph_row;
23096 /* Start with the row contents from the current matrix. */
23097 deep_copy_glyph_row (row, f->current_matrix->rows + y);
23098 bool saved_width = row->full_width_p;
23099 row->full_width_p = true;
23100 bool saved_reversed = row->reversed_p;
23101 row->reversed_p = false;
23102 row->enabled_p = true;
23104 /* Arrange for the menu item glyphs to start at (X,Y) and have the
23105 desired face. */
23106 eassert (x < f->desired_matrix->matrix_w);
23107 it.current_x = it.hpos = x;
23108 it.current_y = it.vpos = y;
23109 int saved_used = row->used[TEXT_AREA];
23110 bool saved_truncated = row->truncated_on_right_p;
23111 row->used[TEXT_AREA] = x;
23112 it.face_id = face_id;
23113 it.line_wrap = TRUNCATE;
23115 /* FIXME: This should be controlled by a user option. See the
23116 comments in redisplay_tool_bar and display_mode_line about this.
23117 Also, if paragraph_embedding could ever be R2L, changes will be
23118 needed to avoid shifting to the right the row characters in
23119 term.c:append_glyph. */
23120 it.paragraph_embedding = L2R;
23122 /* Pad with a space on the left. */
23123 display_string (" ", Qnil, Qnil, 0, 0, &it, 1, 0, FRAME_COLS (f) - 1, -1);
23124 width--;
23125 /* Display the menu item, pad with spaces to WIDTH. */
23126 if (submenu)
23128 display_string (item_text, Qnil, Qnil, 0, 0, &it,
23129 item_len, 0, FRAME_COLS (f) - 1, -1);
23130 width -= item_len;
23131 /* Indicate with " >" that there's a submenu. */
23132 display_string (" >", Qnil, Qnil, 0, 0, &it, width, 0,
23133 FRAME_COLS (f) - 1, -1);
23135 else
23136 display_string (item_text, Qnil, Qnil, 0, 0, &it,
23137 width, 0, FRAME_COLS (f) - 1, -1);
23139 row->used[TEXT_AREA] = max (saved_used, row->used[TEXT_AREA]);
23140 row->truncated_on_right_p = saved_truncated;
23141 row->hash = row_hash (row);
23142 row->full_width_p = saved_width;
23143 row->reversed_p = saved_reversed;
23146 /***********************************************************************
23147 Mode Line
23148 ***********************************************************************/
23150 /* Redisplay mode lines in the window tree whose root is WINDOW.
23151 If FORCE, redisplay mode lines unconditionally.
23152 Otherwise, redisplay only mode lines that are garbaged. Value is
23153 the number of windows whose mode lines were redisplayed. */
23155 static int
23156 redisplay_mode_lines (Lisp_Object window, bool force)
23158 int nwindows = 0;
23160 while (!NILP (window))
23162 struct window *w = XWINDOW (window);
23164 if (WINDOWP (w->contents))
23165 nwindows += redisplay_mode_lines (w->contents, force);
23166 else if (force
23167 || FRAME_GARBAGED_P (XFRAME (w->frame))
23168 || !MATRIX_MODE_LINE_ROW (w->current_matrix)->enabled_p)
23170 struct text_pos lpoint;
23171 struct buffer *old = current_buffer;
23173 /* Set the window's buffer for the mode line display. */
23174 SET_TEXT_POS (lpoint, PT, PT_BYTE);
23175 set_buffer_internal_1 (XBUFFER (w->contents));
23177 /* Point refers normally to the selected window. For any
23178 other window, set up appropriate value. */
23179 if (!EQ (window, selected_window))
23181 struct text_pos pt;
23183 CLIP_TEXT_POS_FROM_MARKER (pt, w->pointm);
23184 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
23187 /* Display mode lines. */
23188 clear_glyph_matrix (w->desired_matrix);
23189 if (display_mode_lines (w))
23190 ++nwindows;
23192 /* Restore old settings. */
23193 set_buffer_internal_1 (old);
23194 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
23197 window = w->next;
23200 return nwindows;
23204 /* Display the mode and/or header line of window W. Value is the
23205 sum number of mode lines and header lines displayed. */
23207 static int
23208 display_mode_lines (struct window *w)
23210 Lisp_Object old_selected_window = selected_window;
23211 Lisp_Object old_selected_frame = selected_frame;
23212 Lisp_Object new_frame = w->frame;
23213 Lisp_Object old_frame_selected_window = XFRAME (new_frame)->selected_window;
23214 int n = 0;
23216 selected_frame = new_frame;
23217 /* FIXME: If we were to allow the mode-line's computation changing the buffer
23218 or window's point, then we'd need select_window_1 here as well. */
23219 XSETWINDOW (selected_window, w);
23220 XFRAME (new_frame)->selected_window = selected_window;
23222 /* These will be set while the mode line specs are processed. */
23223 line_number_displayed = false;
23224 w->column_number_displayed = -1;
23226 if (window_wants_mode_line (w))
23228 Lisp_Object window_mode_line_format
23229 = window_parameter (w, Qmode_line_format);
23231 struct window *sel_w = XWINDOW (old_selected_window);
23233 /* Select mode line face based on the real selected window. */
23234 display_mode_line (w, CURRENT_MODE_LINE_FACE_ID_3 (sel_w, sel_w, w),
23235 NILP (window_mode_line_format)
23236 ? BVAR (current_buffer, mode_line_format)
23237 : window_mode_line_format);
23238 ++n;
23241 if (window_wants_header_line (w))
23243 Lisp_Object window_header_line_format
23244 = window_parameter (w, Qheader_line_format);
23246 display_mode_line (w, HEADER_LINE_FACE_ID,
23247 NILP (window_header_line_format)
23248 ? BVAR (current_buffer, header_line_format)
23249 : window_header_line_format);
23250 ++n;
23253 XFRAME (new_frame)->selected_window = old_frame_selected_window;
23254 selected_frame = old_selected_frame;
23255 selected_window = old_selected_window;
23256 if (n > 0)
23257 w->must_be_updated_p = true;
23258 return n;
23262 /* Display mode or header line of window W. FACE_ID specifies which
23263 line to display; it is either MODE_LINE_FACE_ID or
23264 HEADER_LINE_FACE_ID. FORMAT is the mode/header line format to
23265 display. Value is the pixel height of the mode/header line
23266 displayed. */
23268 static int
23269 display_mode_line (struct window *w, enum face_id face_id, Lisp_Object format)
23271 struct it it;
23272 struct face *face;
23273 ptrdiff_t count = SPECPDL_INDEX ();
23275 init_iterator (&it, w, -1, -1, NULL, face_id);
23276 /* Don't extend on a previously drawn mode-line.
23277 This may happen if called from pos_visible_p. */
23278 it.glyph_row->enabled_p = false;
23279 prepare_desired_row (w, it.glyph_row, true);
23281 it.glyph_row->mode_line_p = true;
23283 /* FIXME: This should be controlled by a user option. But
23284 supporting such an option is not trivial, since the mode line is
23285 made up of many separate strings. */
23286 it.paragraph_embedding = L2R;
23288 record_unwind_protect (unwind_format_mode_line,
23289 format_mode_line_unwind_data (NULL, NULL,
23290 Qnil, false));
23292 mode_line_target = MODE_LINE_DISPLAY;
23294 /* Temporarily make frame's keyboard the current kboard so that
23295 kboard-local variables in the mode_line_format will get the right
23296 values. */
23297 push_kboard (FRAME_KBOARD (it.f));
23298 record_unwind_save_match_data ();
23299 display_mode_element (&it, 0, 0, 0, format, Qnil, false);
23300 pop_kboard ();
23302 unbind_to (count, Qnil);
23304 /* Fill up with spaces. */
23305 display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);
23307 compute_line_metrics (&it);
23308 it.glyph_row->full_width_p = true;
23309 it.glyph_row->continued_p = false;
23310 it.glyph_row->truncated_on_left_p = false;
23311 it.glyph_row->truncated_on_right_p = false;
23313 /* Make a 3D mode-line have a shadow at its right end. */
23314 face = FACE_FROM_ID (it.f, face_id);
23315 extend_face_to_end_of_line (&it);
23316 if (face->box != FACE_NO_BOX)
23318 struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
23319 + it.glyph_row->used[TEXT_AREA] - 1);
23320 last->right_box_line_p = true;
23323 return it.glyph_row->height;
23326 /* Move element ELT in LIST to the front of LIST.
23327 Return the updated list. */
23329 static Lisp_Object
23330 move_elt_to_front (Lisp_Object elt, Lisp_Object list)
23332 register Lisp_Object tail, prev;
23333 register Lisp_Object tem;
23335 tail = list;
23336 prev = Qnil;
23337 while (CONSP (tail))
23339 tem = XCAR (tail);
23341 if (EQ (elt, tem))
23343 /* Splice out the link TAIL. */
23344 if (NILP (prev))
23345 list = XCDR (tail);
23346 else
23347 Fsetcdr (prev, XCDR (tail));
23349 /* Now make it the first. */
23350 Fsetcdr (tail, list);
23351 return tail;
23353 else
23354 prev = tail;
23355 tail = XCDR (tail);
23356 maybe_quit ();
23359 /* Not found--return unchanged LIST. */
23360 return list;
23363 /* Contribute ELT to the mode line for window IT->w. How it
23364 translates into text depends on its data type.
23366 IT describes the display environment in which we display, as usual.
23368 DEPTH is the depth in recursion. It is used to prevent
23369 infinite recursion here.
23371 FIELD_WIDTH is the number of characters the display of ELT should
23372 occupy in the mode line, and PRECISION is the maximum number of
23373 characters to display from ELT's representation. See
23374 display_string for details.
23376 Returns the hpos of the end of the text generated by ELT.
23378 PROPS is a property list to add to any string we encounter.
23380 If RISKY, remove (disregard) any properties in any string
23381 we encounter, and ignore :eval and :propertize.
23383 The global variable `mode_line_target' determines whether the
23384 output is passed to `store_mode_line_noprop',
23385 `store_mode_line_string', or `display_string'. */
23387 static int
23388 display_mode_element (struct it *it, int depth, int field_width, int precision,
23389 Lisp_Object elt, Lisp_Object props, bool risky)
23391 int n = 0, field, prec;
23392 bool literal = false;
23394 tail_recurse:
23395 if (depth > 100)
23396 elt = build_string ("*too-deep*");
23398 depth++;
23400 switch (XTYPE (elt))
23402 case Lisp_String:
23404 /* A string: output it and check for %-constructs within it. */
23405 unsigned char c;
23406 ptrdiff_t offset = 0;
23408 if (SCHARS (elt) > 0
23409 && (!NILP (props) || risky))
23411 Lisp_Object oprops, aelt;
23412 oprops = Ftext_properties_at (make_number (0), elt);
23414 /* If the starting string's properties are not what
23415 we want, translate the string. Also, if the string
23416 is risky, do that anyway. */
23418 if (NILP (Fequal (props, oprops)) || risky)
23420 /* If the starting string has properties,
23421 merge the specified ones onto the existing ones. */
23422 if (! NILP (oprops) && !risky)
23424 Lisp_Object tem;
23426 oprops = Fcopy_sequence (oprops);
23427 tem = props;
23428 while (CONSP (tem))
23430 oprops = Fplist_put (oprops, XCAR (tem),
23431 XCAR (XCDR (tem)));
23432 tem = XCDR (XCDR (tem));
23434 props = oprops;
23437 aelt = Fassoc (elt, mode_line_proptrans_alist, Qnil);
23438 if (! NILP (aelt) && !NILP (Fequal (props, XCDR (aelt))))
23440 /* AELT is what we want. Move it to the front
23441 without consing. */
23442 elt = XCAR (aelt);
23443 mode_line_proptrans_alist
23444 = move_elt_to_front (aelt, mode_line_proptrans_alist);
23446 else
23448 Lisp_Object tem;
23450 /* If AELT has the wrong props, it is useless.
23451 so get rid of it. */
23452 if (! NILP (aelt))
23453 mode_line_proptrans_alist
23454 = Fdelq (aelt, mode_line_proptrans_alist);
23456 elt = Fcopy_sequence (elt);
23457 Fset_text_properties (make_number (0), Flength (elt),
23458 props, elt);
23459 /* Add this item to mode_line_proptrans_alist. */
23460 mode_line_proptrans_alist
23461 = Fcons (Fcons (elt, props),
23462 mode_line_proptrans_alist);
23463 /* Truncate mode_line_proptrans_alist
23464 to at most 50 elements. */
23465 tem = Fnthcdr (make_number (50),
23466 mode_line_proptrans_alist);
23467 if (! NILP (tem))
23468 XSETCDR (tem, Qnil);
23473 offset = 0;
23475 if (literal)
23477 prec = precision - n;
23478 switch (mode_line_target)
23480 case MODE_LINE_NOPROP:
23481 case MODE_LINE_TITLE:
23482 n += store_mode_line_noprop (SSDATA (elt), -1, prec);
23483 break;
23484 case MODE_LINE_STRING:
23485 n += store_mode_line_string (NULL, elt, true, 0, prec, Qnil);
23486 break;
23487 case MODE_LINE_DISPLAY:
23488 n += display_string (NULL, elt, Qnil, 0, 0, it,
23489 0, prec, 0, STRING_MULTIBYTE (elt));
23490 break;
23493 break;
23496 /* Handle the non-literal case. */
23498 while ((precision <= 0 || n < precision)
23499 && SREF (elt, offset) != 0
23500 && (mode_line_target != MODE_LINE_DISPLAY
23501 || it->current_x < it->last_visible_x))
23503 ptrdiff_t last_offset = offset;
23505 /* Advance to end of string or next format specifier. */
23506 while ((c = SREF (elt, offset++)) != '\0' && c != '%')
23509 if (offset - 1 != last_offset)
23511 ptrdiff_t nchars, nbytes;
23513 /* Output to end of string or up to '%'. Field width
23514 is length of string. Don't output more than
23515 PRECISION allows us. */
23516 offset--;
23518 prec = c_string_width (SDATA (elt) + last_offset,
23519 offset - last_offset, precision - n,
23520 &nchars, &nbytes);
23522 switch (mode_line_target)
23524 case MODE_LINE_NOPROP:
23525 case MODE_LINE_TITLE:
23526 n += store_mode_line_noprop (SSDATA (elt) + last_offset, 0, prec);
23527 break;
23528 case MODE_LINE_STRING:
23530 ptrdiff_t bytepos = last_offset;
23531 ptrdiff_t charpos = string_byte_to_char (elt, bytepos);
23532 ptrdiff_t endpos = (precision <= 0
23533 ? string_byte_to_char (elt, offset)
23534 : charpos + nchars);
23535 Lisp_Object mode_string
23536 = Fsubstring (elt, make_number (charpos),
23537 make_number (endpos));
23538 n += store_mode_line_string (NULL, mode_string, false,
23539 0, 0, Qnil);
23541 break;
23542 case MODE_LINE_DISPLAY:
23544 ptrdiff_t bytepos = last_offset;
23545 ptrdiff_t charpos = string_byte_to_char (elt, bytepos);
23547 if (precision <= 0)
23548 nchars = string_byte_to_char (elt, offset) - charpos;
23549 n += display_string (NULL, elt, Qnil, 0, charpos,
23550 it, 0, nchars, 0,
23551 STRING_MULTIBYTE (elt));
23553 break;
23556 else /* c == '%' */
23558 ptrdiff_t percent_position = offset;
23560 /* Get the specified minimum width. Zero means
23561 don't pad. */
23562 field = 0;
23563 while ((c = SREF (elt, offset++)) >= '0' && c <= '9')
23564 field = field * 10 + c - '0';
23566 /* Don't pad beyond the total padding allowed. */
23567 if (field_width - n > 0 && field > field_width - n)
23568 field = field_width - n;
23570 /* Note that either PRECISION <= 0 or N < PRECISION. */
23571 prec = precision - n;
23573 if (c == 'M')
23574 n += display_mode_element (it, depth, field, prec,
23575 Vglobal_mode_string, props,
23576 risky);
23577 else if (c != 0)
23579 bool multibyte;
23580 ptrdiff_t bytepos, charpos;
23581 const char *spec;
23582 Lisp_Object string;
23584 bytepos = percent_position;
23585 charpos = (STRING_MULTIBYTE (elt)
23586 ? string_byte_to_char (elt, bytepos)
23587 : bytepos);
23588 spec = decode_mode_spec (it->w, c, field, &string);
23589 multibyte = STRINGP (string) && STRING_MULTIBYTE (string);
23591 switch (mode_line_target)
23593 case MODE_LINE_NOPROP:
23594 case MODE_LINE_TITLE:
23595 n += store_mode_line_noprop (spec, field, prec);
23596 break;
23597 case MODE_LINE_STRING:
23599 Lisp_Object tem = build_string (spec);
23600 props = Ftext_properties_at (make_number (charpos), elt);
23601 /* Should only keep face property in props */
23602 n += store_mode_line_string (NULL, tem, false,
23603 field, prec, props);
23605 break;
23606 case MODE_LINE_DISPLAY:
23608 int nglyphs_before, nwritten;
23610 nglyphs_before = it->glyph_row->used[TEXT_AREA];
23611 nwritten = display_string (spec, string, elt,
23612 charpos, 0, it,
23613 field, prec, 0,
23614 multibyte);
23616 /* Assign to the glyphs written above the
23617 string where the `%x' came from, position
23618 of the `%'. */
23619 if (nwritten > 0)
23621 struct glyph *glyph
23622 = (it->glyph_row->glyphs[TEXT_AREA]
23623 + nglyphs_before);
23624 int i;
23626 for (i = 0; i < nwritten; ++i)
23628 glyph[i].object = elt;
23629 glyph[i].charpos = charpos;
23632 n += nwritten;
23635 break;
23638 else /* c == 0 */
23639 break;
23643 break;
23645 case Lisp_Symbol:
23646 /* A symbol: process the value of the symbol recursively
23647 as if it appeared here directly. Avoid error if symbol void.
23648 Special case: if value of symbol is a string, output the string
23649 literally. */
23651 register Lisp_Object tem;
23653 /* If the variable is not marked as risky to set
23654 then its contents are risky to use. */
23655 if (NILP (Fget (elt, Qrisky_local_variable)))
23656 risky = true;
23658 tem = Fboundp (elt);
23659 if (!NILP (tem))
23661 tem = Fsymbol_value (elt);
23662 /* If value is a string, output that string literally:
23663 don't check for % within it. */
23664 if (STRINGP (tem))
23665 literal = true;
23667 if (!EQ (tem, elt))
23669 /* Give up right away for nil or t. */
23670 elt = tem;
23671 goto tail_recurse;
23675 break;
23677 case Lisp_Cons:
23679 register Lisp_Object car, tem;
23681 /* A cons cell: five distinct cases.
23682 If first element is :eval or :propertize, do something special.
23683 If first element is a string or a cons, process all the elements
23684 and effectively concatenate them.
23685 If first element is a negative number, truncate displaying cdr to
23686 at most that many characters. If positive, pad (with spaces)
23687 to at least that many characters.
23688 If first element is a symbol, process the cadr or caddr recursively
23689 according to whether the symbol's value is non-nil or nil. */
23690 car = XCAR (elt);
23691 if (EQ (car, QCeval))
23693 /* An element of the form (:eval FORM) means evaluate FORM
23694 and use the result as mode line elements. */
23696 if (risky)
23697 break;
23699 if (CONSP (XCDR (elt)))
23701 Lisp_Object spec;
23702 spec = safe__eval (true, XCAR (XCDR (elt)));
23703 /* The :eval form could delete the frame stored in the
23704 iterator, which will cause a crash if we try to
23705 access faces and other fields (e.g., FRAME_KBOARD)
23706 on that frame. This is a nonsensical thing to do,
23707 and signaling an error from redisplay might be
23708 dangerous, but we cannot continue with an invalid frame. */
23709 if (!FRAME_LIVE_P (it->f))
23710 signal_error (":eval deleted the frame being displayed", elt);
23711 n += display_mode_element (it, depth, field_width - n,
23712 precision - n, spec, props,
23713 risky);
23716 else if (EQ (car, QCpropertize))
23718 /* An element of the form (:propertize ELT PROPS...)
23719 means display ELT but applying properties PROPS. */
23721 if (risky)
23722 break;
23724 if (CONSP (XCDR (elt)))
23725 n += display_mode_element (it, depth, field_width - n,
23726 precision - n, XCAR (XCDR (elt)),
23727 XCDR (XCDR (elt)), risky);
23729 else if (SYMBOLP (car))
23731 tem = Fboundp (car);
23732 elt = XCDR (elt);
23733 if (!CONSP (elt))
23734 goto invalid;
23735 /* elt is now the cdr, and we know it is a cons cell.
23736 Use its car if CAR has a non-nil value. */
23737 if (!NILP (tem))
23739 tem = Fsymbol_value (car);
23740 if (!NILP (tem))
23742 elt = XCAR (elt);
23743 goto tail_recurse;
23746 /* Symbol's value is nil (or symbol is unbound)
23747 Get the cddr of the original list
23748 and if possible find the caddr and use that. */
23749 elt = XCDR (elt);
23750 if (NILP (elt))
23751 break;
23752 else if (!CONSP (elt))
23753 goto invalid;
23754 elt = XCAR (elt);
23755 goto tail_recurse;
23757 else if (INTEGERP (car))
23759 register int lim = XINT (car);
23760 elt = XCDR (elt);
23761 if (lim < 0)
23763 /* Negative int means reduce maximum width. */
23764 if (precision <= 0)
23765 precision = -lim;
23766 else
23767 precision = min (precision, -lim);
23769 else if (lim > 0)
23771 /* Padding specified. Don't let it be more than
23772 current maximum. */
23773 if (precision > 0)
23774 lim = min (precision, lim);
23776 /* If that's more padding than already wanted, queue it.
23777 But don't reduce padding already specified even if
23778 that is beyond the current truncation point. */
23779 field_width = max (lim, field_width);
23781 goto tail_recurse;
23783 else if (STRINGP (car) || CONSP (car))
23784 FOR_EACH_TAIL_SAFE (elt)
23786 if (0 < precision && precision <= n)
23787 break;
23788 n += display_mode_element (it, depth,
23789 /* Pad after only the last
23790 list element. */
23791 (! CONSP (XCDR (elt))
23792 ? field_width - n
23793 : 0),
23794 precision - n, XCAR (elt),
23795 props, risky);
23798 break;
23800 default:
23801 invalid:
23802 elt = build_string ("*invalid*");
23803 goto tail_recurse;
23806 /* Pad to FIELD_WIDTH. */
23807 if (field_width > 0 && n < field_width)
23809 switch (mode_line_target)
23811 case MODE_LINE_NOPROP:
23812 case MODE_LINE_TITLE:
23813 n += store_mode_line_noprop ("", field_width - n, 0);
23814 break;
23815 case MODE_LINE_STRING:
23816 n += store_mode_line_string ("", Qnil, false, field_width - n, 0,
23817 Qnil);
23818 break;
23819 case MODE_LINE_DISPLAY:
23820 n += display_string ("", Qnil, Qnil, 0, 0, it, field_width - n,
23821 0, 0, 0);
23822 break;
23826 return n;
23829 /* Store a mode-line string element in mode_line_string_list.
23831 If STRING is non-null, display that C string. Otherwise, the Lisp
23832 string LISP_STRING is displayed.
23834 FIELD_WIDTH is the minimum number of output glyphs to produce.
23835 If STRING has fewer characters than FIELD_WIDTH, pad to the right
23836 with spaces. FIELD_WIDTH <= 0 means don't pad.
23838 PRECISION is the maximum number of characters to output from
23839 STRING. PRECISION <= 0 means don't truncate the string.
23841 If COPY_STRING, make a copy of LISP_STRING before adding
23842 properties to the string.
23844 PROPS are the properties to add to the string.
23845 The mode_line_string_face face property is always added to the string.
23848 static int
23849 store_mode_line_string (const char *string, Lisp_Object lisp_string,
23850 bool copy_string,
23851 int field_width, int precision, Lisp_Object props)
23853 ptrdiff_t len;
23854 int n = 0;
23856 if (string != NULL)
23858 len = strlen (string);
23859 if (precision > 0 && len > precision)
23860 len = precision;
23861 lisp_string = make_string (string, len);
23862 if (NILP (props))
23863 props = mode_line_string_face_prop;
23864 else if (!NILP (mode_line_string_face))
23866 Lisp_Object face = Fplist_get (props, Qface);
23867 props = Fcopy_sequence (props);
23868 if (NILP (face))
23869 face = mode_line_string_face;
23870 else
23871 face = list2 (face, mode_line_string_face);
23872 props = Fplist_put (props, Qface, face);
23874 Fadd_text_properties (make_number (0), make_number (len),
23875 props, lisp_string);
23877 else
23879 len = XFASTINT (Flength (lisp_string));
23880 if (precision > 0 && len > precision)
23882 len = precision;
23883 lisp_string = Fsubstring (lisp_string, make_number (0), make_number (len));
23884 precision = -1;
23886 if (!NILP (mode_line_string_face))
23888 Lisp_Object face;
23889 if (NILP (props))
23890 props = Ftext_properties_at (make_number (0), lisp_string);
23891 face = Fplist_get (props, Qface);
23892 if (NILP (face))
23893 face = mode_line_string_face;
23894 else
23895 face = list2 (face, mode_line_string_face);
23896 props = list2 (Qface, face);
23897 if (copy_string)
23898 lisp_string = Fcopy_sequence (lisp_string);
23900 if (!NILP (props))
23901 Fadd_text_properties (make_number (0), make_number (len),
23902 props, lisp_string);
23905 if (len > 0)
23907 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
23908 n += len;
23911 if (field_width > len)
23913 field_width -= len;
23914 lisp_string = Fmake_string (make_number (field_width), make_number (' '));
23915 if (!NILP (props))
23916 Fadd_text_properties (make_number (0), make_number (field_width),
23917 props, lisp_string);
23918 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
23919 n += field_width;
23922 return n;
23926 DEFUN ("format-mode-line", Fformat_mode_line, Sformat_mode_line,
23927 1, 4, 0,
23928 doc: /* Format a string out of a mode line format specification.
23929 First arg FORMAT specifies the mode line format (see `mode-line-format'
23930 for details) to use.
23932 By default, the format is evaluated for the currently selected window.
23934 Optional second arg FACE specifies the face property to put on all
23935 characters for which no face is specified. The value nil means the
23936 default face. The value t means whatever face the window's mode line
23937 currently uses (either `mode-line' or `mode-line-inactive',
23938 depending on whether the window is the selected window or not).
23939 An integer value means the value string has no text
23940 properties.
23942 Optional third and fourth args WINDOW and BUFFER specify the window
23943 and buffer to use as the context for the formatting (defaults
23944 are the selected window and the WINDOW's buffer). */)
23945 (Lisp_Object format, Lisp_Object face,
23946 Lisp_Object window, Lisp_Object buffer)
23948 struct it it;
23949 int len;
23950 struct window *w;
23951 struct buffer *old_buffer = NULL;
23952 int face_id;
23953 bool no_props = INTEGERP (face);
23954 ptrdiff_t count = SPECPDL_INDEX ();
23955 Lisp_Object str;
23956 int string_start = 0;
23958 w = decode_any_window (window);
23959 XSETWINDOW (window, w);
23961 if (NILP (buffer))
23962 buffer = w->contents;
23963 CHECK_BUFFER (buffer);
23965 /* Make formatting the modeline a non-op when noninteractive, otherwise
23966 there will be problems later caused by a partially initialized frame. */
23967 if (NILP (format) || noninteractive)
23968 return empty_unibyte_string;
23970 if (no_props)
23971 face = Qnil;
23973 face_id = (NILP (face) || EQ (face, Qdefault)) ? DEFAULT_FACE_ID
23974 : EQ (face, Qt) ? (EQ (window, selected_window)
23975 ? MODE_LINE_FACE_ID : MODE_LINE_INACTIVE_FACE_ID)
23976 : EQ (face, Qmode_line) ? MODE_LINE_FACE_ID
23977 : EQ (face, Qmode_line_inactive) ? MODE_LINE_INACTIVE_FACE_ID
23978 : EQ (face, Qheader_line) ? HEADER_LINE_FACE_ID
23979 : EQ (face, Qtool_bar) ? TOOL_BAR_FACE_ID
23980 : DEFAULT_FACE_ID;
23982 old_buffer = current_buffer;
23984 /* Save things including mode_line_proptrans_alist,
23985 and set that to nil so that we don't alter the outer value. */
23986 record_unwind_protect (unwind_format_mode_line,
23987 format_mode_line_unwind_data
23988 (XFRAME (WINDOW_FRAME (w)),
23989 old_buffer, selected_window, true));
23990 mode_line_proptrans_alist = Qnil;
23992 Fselect_window (window, Qt);
23993 set_buffer_internal_1 (XBUFFER (buffer));
23995 init_iterator (&it, w, -1, -1, NULL, face_id);
23997 if (no_props)
23999 mode_line_target = MODE_LINE_NOPROP;
24000 mode_line_string_face_prop = Qnil;
24001 mode_line_string_list = Qnil;
24002 string_start = MODE_LINE_NOPROP_LEN (0);
24004 else
24006 mode_line_target = MODE_LINE_STRING;
24007 mode_line_string_list = Qnil;
24008 mode_line_string_face = face;
24009 mode_line_string_face_prop
24010 = NILP (face) ? Qnil : list2 (Qface, face);
24013 push_kboard (FRAME_KBOARD (it.f));
24014 display_mode_element (&it, 0, 0, 0, format, Qnil, false);
24015 pop_kboard ();
24017 if (no_props)
24019 len = MODE_LINE_NOPROP_LEN (string_start);
24020 str = make_string (mode_line_noprop_buf + string_start, len);
24022 else
24024 mode_line_string_list = Fnreverse (mode_line_string_list);
24025 str = Fmapconcat (Qidentity, mode_line_string_list,
24026 empty_unibyte_string);
24029 unbind_to (count, Qnil);
24030 return str;
24033 /* Write a null-terminated, right justified decimal representation of
24034 the positive integer D to BUF using a minimal field width WIDTH. */
24036 static void
24037 pint2str (register char *buf, register int width, register ptrdiff_t d)
24039 register char *p = buf;
24041 if (d <= 0)
24042 *p++ = '0';
24043 else
24045 while (d > 0)
24047 *p++ = d % 10 + '0';
24048 d /= 10;
24052 for (width -= (int) (p - buf); width > 0; --width)
24053 *p++ = ' ';
24054 *p-- = '\0';
24055 while (p > buf)
24057 d = *buf;
24058 *buf++ = *p;
24059 *p-- = d;
24063 /* Write a null-terminated, right justified decimal and "human
24064 readable" representation of the nonnegative integer D to BUF using
24065 a minimal field width WIDTH. D should be smaller than 999.5e24. */
24067 static const char power_letter[] =
24069 0, /* no letter */
24070 'k', /* kilo */
24071 'M', /* mega */
24072 'G', /* giga */
24073 'T', /* tera */
24074 'P', /* peta */
24075 'E', /* exa */
24076 'Z', /* zetta */
24077 'Y' /* yotta */
24080 static void
24081 pint2hrstr (char *buf, int width, ptrdiff_t d)
24083 /* We aim to represent the nonnegative integer D as
24084 QUOTIENT.TENTHS * 10 ^ (3 * EXPONENT). */
24085 ptrdiff_t quotient = d;
24086 int remainder = 0;
24087 /* -1 means: do not use TENTHS. */
24088 int tenths = -1;
24089 int exponent = 0;
24091 /* Length of QUOTIENT.TENTHS as a string. */
24092 int length;
24094 char * psuffix;
24095 char * p;
24097 if (quotient >= 1000)
24099 /* Scale to the appropriate EXPONENT. */
24102 remainder = quotient % 1000;
24103 quotient /= 1000;
24104 exponent++;
24106 while (quotient >= 1000);
24108 /* Round to nearest and decide whether to use TENTHS or not. */
24109 if (quotient <= 9)
24111 tenths = remainder / 100;
24112 if (remainder % 100 >= 50)
24114 if (tenths < 9)
24115 tenths++;
24116 else
24118 quotient++;
24119 if (quotient == 10)
24120 tenths = -1;
24121 else
24122 tenths = 0;
24126 else
24127 if (remainder >= 500)
24129 if (quotient < 999)
24130 quotient++;
24131 else
24133 quotient = 1;
24134 exponent++;
24135 tenths = 0;
24140 /* Calculate the LENGTH of QUOTIENT.TENTHS as a string. */
24141 if (tenths == -1 && quotient <= 99)
24142 if (quotient <= 9)
24143 length = 1;
24144 else
24145 length = 2;
24146 else
24147 length = 3;
24148 p = psuffix = buf + max (width, length);
24150 /* Print EXPONENT. */
24151 *psuffix++ = power_letter[exponent];
24152 *psuffix = '\0';
24154 /* Print TENTHS. */
24155 if (tenths >= 0)
24157 *--p = '0' + tenths;
24158 *--p = '.';
24161 /* Print QUOTIENT. */
24164 int digit = quotient % 10;
24165 *--p = '0' + digit;
24167 while ((quotient /= 10) != 0);
24169 /* Print leading spaces. */
24170 while (buf < p)
24171 *--p = ' ';
24174 /* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
24175 If EOL_FLAG, set also a mnemonic character for end-of-line
24176 type of CODING_SYSTEM. Return updated pointer into BUF. */
24178 static unsigned char invalid_eol_type[] = "(*invalid*)";
24180 static char *
24181 decode_mode_spec_coding (Lisp_Object coding_system, char *buf, bool eol_flag)
24183 Lisp_Object val;
24184 bool multibyte = !NILP (BVAR (current_buffer, enable_multibyte_characters));
24185 const unsigned char *eol_str;
24186 int eol_str_len;
24187 /* The EOL conversion we are using. */
24188 Lisp_Object eoltype;
24190 val = CODING_SYSTEM_SPEC (coding_system);
24191 eoltype = Qnil;
24193 if (!VECTORP (val)) /* Not yet decided. */
24195 *buf++ = multibyte ? '-' : ' ';
24196 if (eol_flag)
24197 eoltype = eol_mnemonic_undecided;
24198 /* Don't mention EOL conversion if it isn't decided. */
24200 else
24202 Lisp_Object attrs;
24203 Lisp_Object eolvalue;
24205 attrs = AREF (val, 0);
24206 eolvalue = AREF (val, 2);
24208 *buf++ = multibyte
24209 ? XFASTINT (CODING_ATTR_MNEMONIC (attrs))
24210 : ' ';
24212 if (eol_flag)
24214 /* The EOL conversion that is normal on this system. */
24216 if (NILP (eolvalue)) /* Not yet decided. */
24217 eoltype = eol_mnemonic_undecided;
24218 else if (VECTORP (eolvalue)) /* Not yet decided. */
24219 eoltype = eol_mnemonic_undecided;
24220 else /* eolvalue is Qunix, Qdos, or Qmac. */
24221 eoltype = (EQ (eolvalue, Qunix)
24222 ? eol_mnemonic_unix
24223 : EQ (eolvalue, Qdos)
24224 ? eol_mnemonic_dos : eol_mnemonic_mac);
24228 if (eol_flag)
24230 /* Mention the EOL conversion if it is not the usual one. */
24231 if (STRINGP (eoltype))
24233 eol_str = SDATA (eoltype);
24234 eol_str_len = SBYTES (eoltype);
24236 else if (CHARACTERP (eoltype))
24238 int c = XFASTINT (eoltype);
24239 return buf + CHAR_STRING (c, (unsigned char *) buf);
24241 else
24243 eol_str = invalid_eol_type;
24244 eol_str_len = sizeof (invalid_eol_type) - 1;
24246 memcpy (buf, eol_str, eol_str_len);
24247 buf += eol_str_len;
24250 return buf;
24253 /* Return the approximate percentage N is of D (rounding upward), or 99,
24254 whichever is less. Assume 0 < D and 0 <= N <= D * INT_MAX / 100. */
24256 static int
24257 percent99 (ptrdiff_t n, ptrdiff_t d)
24259 int percent = (d - 1 + 100.0 * n) / d;
24260 return min (percent, 99);
24263 /* Return a string for the output of a mode line %-spec for window W,
24264 generated by character C. FIELD_WIDTH > 0 means pad the string
24265 returned with spaces to that value. Return a Lisp string in
24266 *STRING if the resulting string is taken from that Lisp string.
24268 Note we operate on the current buffer for most purposes. */
24270 static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
24272 static const char *
24273 decode_mode_spec (struct window *w, register int c, int field_width,
24274 Lisp_Object *string)
24276 Lisp_Object obj;
24277 struct frame *f = XFRAME (WINDOW_FRAME (w));
24278 char *decode_mode_spec_buf = f->decode_mode_spec_buffer;
24279 /* We are going to use f->decode_mode_spec_buffer as the buffer to
24280 produce strings from numerical values, so limit preposterously
24281 large values of FIELD_WIDTH to avoid overrunning the buffer's
24282 end. The size of the buffer is enough for FRAME_MESSAGE_BUF_SIZE
24283 bytes plus the terminating null. */
24284 int width = min (field_width, FRAME_MESSAGE_BUF_SIZE (f));
24285 struct buffer *b = current_buffer;
24287 obj = Qnil;
24288 *string = Qnil;
24290 switch (c)
24292 case '*':
24293 if (!NILP (BVAR (b, read_only)))
24294 return "%";
24295 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
24296 return "*";
24297 return "-";
24299 case '+':
24300 /* This differs from %* only for a modified read-only buffer. */
24301 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
24302 return "*";
24303 if (!NILP (BVAR (b, read_only)))
24304 return "%";
24305 return "-";
24307 case '&':
24308 /* This differs from %* in ignoring read-only-ness. */
24309 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
24310 return "*";
24311 return "-";
24313 case '%':
24314 return "%";
24316 case '[':
24318 int i;
24319 char *p;
24321 if (command_loop_level > 5)
24322 return "[[[... ";
24323 p = decode_mode_spec_buf;
24324 for (i = 0; i < command_loop_level; i++)
24325 *p++ = '[';
24326 *p = 0;
24327 return decode_mode_spec_buf;
24330 case ']':
24332 int i;
24333 char *p;
24335 if (command_loop_level > 5)
24336 return " ...]]]";
24337 p = decode_mode_spec_buf;
24338 for (i = 0; i < command_loop_level; i++)
24339 *p++ = ']';
24340 *p = 0;
24341 return decode_mode_spec_buf;
24344 case '-':
24346 register int i;
24348 /* Let lots_of_dashes be a string of infinite length. */
24349 if (mode_line_target == MODE_LINE_NOPROP
24350 || mode_line_target == MODE_LINE_STRING)
24351 return "--";
24352 if (field_width <= 0
24353 || field_width > sizeof (lots_of_dashes))
24355 for (i = 0; i < FRAME_MESSAGE_BUF_SIZE (f) - 1; ++i)
24356 decode_mode_spec_buf[i] = '-';
24357 decode_mode_spec_buf[i] = '\0';
24358 return decode_mode_spec_buf;
24360 else
24361 return lots_of_dashes;
24364 case 'b':
24365 obj = BVAR (b, name);
24366 break;
24368 case 'c':
24369 case 'C':
24370 /* %c, %C, and %l are ignored in `frame-title-format'.
24371 (In redisplay_internal, the frame title is drawn _before_ the
24372 windows are updated, so the stuff which depends on actual
24373 window contents (such as %l) may fail to render properly, or
24374 even crash emacs.) */
24375 if (mode_line_target == MODE_LINE_TITLE)
24376 return "";
24377 else
24379 ptrdiff_t col = current_column ();
24380 int disp_col = (c == 'C') ? col + 1 : col;
24381 w->column_number_displayed = col;
24382 pint2str (decode_mode_spec_buf, width, disp_col);
24383 return decode_mode_spec_buf;
24386 case 'e':
24387 #if !defined SYSTEM_MALLOC && !defined HYBRID_MALLOC
24389 if (NILP (Vmemory_full))
24390 return "";
24391 else
24392 return "!MEM FULL! ";
24394 #else
24395 return "";
24396 #endif
24398 case 'F':
24399 /* %F displays the frame name. */
24400 if (!NILP (f->title))
24401 return SSDATA (f->title);
24402 if (f->explicit_name || ! FRAME_WINDOW_P (f))
24403 return SSDATA (f->name);
24404 return "Emacs";
24406 case 'f':
24407 obj = BVAR (b, filename);
24408 break;
24410 case 'i':
24412 ptrdiff_t size = ZV - BEGV;
24413 pint2str (decode_mode_spec_buf, width, size);
24414 return decode_mode_spec_buf;
24417 case 'I':
24419 ptrdiff_t size = ZV - BEGV;
24420 pint2hrstr (decode_mode_spec_buf, width, size);
24421 return decode_mode_spec_buf;
24424 case 'l':
24426 ptrdiff_t startpos, startpos_byte, line, linepos, linepos_byte;
24427 ptrdiff_t topline, nlines, height;
24428 ptrdiff_t junk;
24430 /* %c, %C, and %l are ignored in `frame-title-format'. */
24431 if (mode_line_target == MODE_LINE_TITLE)
24432 return "";
24434 startpos = marker_position (w->start);
24435 startpos_byte = marker_byte_position (w->start);
24436 height = WINDOW_TOTAL_LINES (w);
24438 /* If we decided that this buffer isn't suitable for line numbers,
24439 don't forget that too fast. */
24440 if (w->base_line_pos == -1)
24441 goto no_value;
24443 /* If the buffer is very big, don't waste time. */
24444 if (INTEGERP (Vline_number_display_limit)
24445 && BUF_ZV (b) - BUF_BEGV (b) > XINT (Vline_number_display_limit))
24447 w->base_line_pos = 0;
24448 w->base_line_number = 0;
24449 goto no_value;
24452 if (w->base_line_number > 0
24453 && w->base_line_pos > 0
24454 && w->base_line_pos <= startpos)
24456 line = w->base_line_number;
24457 linepos = w->base_line_pos;
24458 linepos_byte = buf_charpos_to_bytepos (b, linepos);
24460 else
24462 line = 1;
24463 linepos = BUF_BEGV (b);
24464 linepos_byte = BUF_BEGV_BYTE (b);
24467 /* Count lines from base line to window start position. */
24468 nlines = display_count_lines (linepos_byte,
24469 startpos_byte,
24470 startpos, &junk);
24472 topline = nlines + line;
24474 /* Determine a new base line, if the old one is too close
24475 or too far away, or if we did not have one.
24476 "Too close" means it's plausible a scroll-down would
24477 go back past it. */
24478 if (startpos == BUF_BEGV (b))
24480 w->base_line_number = topline;
24481 w->base_line_pos = BUF_BEGV (b);
24483 else if (nlines < height + 25 || nlines > height * 3 + 50
24484 || linepos == BUF_BEGV (b))
24486 ptrdiff_t limit = BUF_BEGV (b);
24487 ptrdiff_t limit_byte = BUF_BEGV_BYTE (b);
24488 ptrdiff_t position;
24489 ptrdiff_t distance =
24490 (height * 2 + 30) * line_number_display_limit_width;
24492 if (startpos - distance > limit)
24494 limit = startpos - distance;
24495 limit_byte = CHAR_TO_BYTE (limit);
24498 nlines = display_count_lines (startpos_byte,
24499 limit_byte,
24500 - (height * 2 + 30),
24501 &position);
24502 /* If we couldn't find the lines we wanted within
24503 line_number_display_limit_width chars per line,
24504 give up on line numbers for this window. */
24505 if (position == limit_byte && limit == startpos - distance)
24507 w->base_line_pos = -1;
24508 w->base_line_number = 0;
24509 goto no_value;
24512 w->base_line_number = topline - nlines;
24513 w->base_line_pos = BYTE_TO_CHAR (position);
24516 /* Now count lines from the start pos to point. */
24517 nlines = display_count_lines (startpos_byte,
24518 PT_BYTE, PT, &junk);
24520 /* Record that we did display the line number. */
24521 line_number_displayed = true;
24523 /* Make the string to show. */
24524 pint2str (decode_mode_spec_buf, width, topline + nlines);
24525 return decode_mode_spec_buf;
24526 no_value:
24528 char *p = decode_mode_spec_buf;
24529 int pad = width - 2;
24530 while (pad-- > 0)
24531 *p++ = ' ';
24532 *p++ = '?';
24533 *p++ = '?';
24534 *p = '\0';
24535 return decode_mode_spec_buf;
24538 break;
24540 case 'm':
24541 obj = BVAR (b, mode_name);
24542 break;
24544 case 'n':
24545 if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
24546 return " Narrow";
24547 break;
24549 /* Display the "degree of travel" of the window through the buffer. */
24550 case 'o':
24552 ptrdiff_t toppos = marker_position (w->start);
24553 ptrdiff_t botpos = BUF_Z (b) - w->window_end_pos;
24554 ptrdiff_t begv = BUF_BEGV (b);
24555 ptrdiff_t zv = BUF_ZV (b);
24557 if (zv <= botpos)
24558 return toppos <= begv ? "All" : "Bottom";
24559 else if (toppos <= begv)
24560 return "Top";
24561 else
24563 sprintf (decode_mode_spec_buf, "%2d%%",
24564 percent99 (toppos - begv, (toppos - begv) + (zv - botpos)));
24565 return decode_mode_spec_buf;
24569 /* Display percentage of buffer above the top of the screen. */
24570 case 'p':
24572 ptrdiff_t pos = marker_position (w->start);
24573 ptrdiff_t begv = BUF_BEGV (b);
24574 ptrdiff_t zv = BUF_ZV (b);
24576 if (w->window_end_pos <= BUF_Z (b) - zv)
24577 return pos <= begv ? "All" : "Bottom";
24578 else if (pos <= begv)
24579 return "Top";
24580 else
24582 sprintf (decode_mode_spec_buf, "%2d%%",
24583 percent99 (pos - begv, zv - begv));
24584 return decode_mode_spec_buf;
24588 /* Display percentage of size above the bottom of the screen. */
24589 case 'P':
24591 ptrdiff_t toppos = marker_position (w->start);
24592 ptrdiff_t botpos = BUF_Z (b) - w->window_end_pos;
24593 ptrdiff_t begv = BUF_BEGV (b);
24594 ptrdiff_t zv = BUF_ZV (b);
24596 if (zv <= botpos)
24597 return toppos <= begv ? "All" : "Bottom";
24598 else
24600 sprintf (decode_mode_spec_buf,
24601 &"Top%2d%%"[begv < toppos ? sizeof "Top" - 1 : 0],
24602 percent99 (botpos - begv, zv - begv));
24603 return decode_mode_spec_buf;
24607 /* Display percentage offsets of top and bottom of the window,
24608 using "All" (but not "Top" or "Bottom") where appropriate. */
24609 case 'q':
24611 ptrdiff_t toppos = marker_position (w->start);
24612 ptrdiff_t botpos = BUF_Z (b) - w->window_end_pos;
24613 ptrdiff_t begv = BUF_BEGV (b);
24614 ptrdiff_t zv = BUF_ZV (b);
24615 int top_perc, bot_perc;
24617 if ((toppos <= begv) && (zv <= botpos))
24618 return "All ";
24620 top_perc = toppos <= begv ? 0 : percent99 (toppos - begv, zv - begv);
24621 bot_perc = zv <= botpos ? 100 : percent99 (botpos - begv, zv - begv);
24623 if (top_perc == bot_perc)
24624 sprintf (decode_mode_spec_buf, "%d%%", top_perc);
24625 else
24626 sprintf (decode_mode_spec_buf, "%d-%d%%", top_perc, bot_perc);
24628 return decode_mode_spec_buf;
24631 case 's':
24632 /* status of process */
24633 obj = Fget_buffer_process (Fcurrent_buffer ());
24634 if (NILP (obj))
24635 return "no process";
24636 #ifndef MSDOS
24637 obj = Fsymbol_name (Fprocess_status (obj));
24638 #endif
24639 break;
24641 case '@':
24643 ptrdiff_t count = inhibit_garbage_collection ();
24644 Lisp_Object curdir = BVAR (current_buffer, directory);
24645 Lisp_Object val = Qnil;
24647 if (STRINGP (curdir))
24648 val = call1 (intern ("file-remote-p"), curdir);
24650 unbind_to (count, Qnil);
24652 if (NILP (val))
24653 return "-";
24654 else
24655 return "@";
24658 case 'z':
24659 /* coding-system (not including end-of-line format) */
24660 case 'Z':
24661 /* coding-system (including end-of-line type) */
24663 bool eol_flag = (c == 'Z');
24664 char *p = decode_mode_spec_buf;
24666 if (! FRAME_WINDOW_P (f))
24668 /* No need to mention EOL here--the terminal never needs
24669 to do EOL conversion. */
24670 p = decode_mode_spec_coding (CODING_ID_NAME
24671 (FRAME_KEYBOARD_CODING (f)->id),
24672 p, false);
24673 p = decode_mode_spec_coding (CODING_ID_NAME
24674 (FRAME_TERMINAL_CODING (f)->id),
24675 p, false);
24677 p = decode_mode_spec_coding (BVAR (b, buffer_file_coding_system),
24678 p, eol_flag);
24680 #if false /* This proves to be annoying; I think we can do without. -- rms. */
24681 #ifdef subprocesses
24682 obj = Fget_buffer_process (Fcurrent_buffer ());
24683 if (PROCESSP (obj))
24685 p = decode_mode_spec_coding
24686 (XPROCESS (obj)->decode_coding_system, p, eol_flag);
24687 p = decode_mode_spec_coding
24688 (XPROCESS (obj)->encode_coding_system, p, eol_flag);
24690 #endif /* subprocesses */
24691 #endif /* false */
24692 *p = 0;
24693 return decode_mode_spec_buf;
24697 if (STRINGP (obj))
24699 *string = obj;
24700 return SSDATA (obj);
24702 else
24703 return "";
24707 /* Count up to COUNT lines starting from START_BYTE. COUNT negative
24708 means count lines back from START_BYTE. But don't go beyond
24709 LIMIT_BYTE. Return the number of lines thus found (always
24710 nonnegative).
24712 Set *BYTE_POS_PTR to the byte position where we stopped. This is
24713 either the position COUNT lines after/before START_BYTE, if we
24714 found COUNT lines, or LIMIT_BYTE if we hit the limit before finding
24715 COUNT lines. */
24717 static ptrdiff_t
24718 display_count_lines (ptrdiff_t start_byte,
24719 ptrdiff_t limit_byte, ptrdiff_t count,
24720 ptrdiff_t *byte_pos_ptr)
24722 register unsigned char *cursor;
24723 unsigned char *base;
24725 register ptrdiff_t ceiling;
24726 register unsigned char *ceiling_addr;
24727 ptrdiff_t orig_count = count;
24729 /* If we are not in selective display mode,
24730 check only for newlines. */
24731 bool selective_display
24732 = (!NILP (BVAR (current_buffer, selective_display))
24733 && !INTEGERP (BVAR (current_buffer, selective_display)));
24735 if (count > 0)
24737 while (start_byte < limit_byte)
24739 ceiling = BUFFER_CEILING_OF (start_byte);
24740 ceiling = min (limit_byte - 1, ceiling);
24741 ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
24742 base = (cursor = BYTE_POS_ADDR (start_byte));
24746 if (selective_display)
24748 while (*cursor != '\n' && *cursor != 015
24749 && ++cursor != ceiling_addr)
24750 continue;
24751 if (cursor == ceiling_addr)
24752 break;
24754 else
24756 cursor = memchr (cursor, '\n', ceiling_addr - cursor);
24757 if (! cursor)
24758 break;
24761 cursor++;
24763 if (--count == 0)
24765 start_byte += cursor - base;
24766 *byte_pos_ptr = start_byte;
24767 return orig_count;
24770 while (cursor < ceiling_addr);
24772 start_byte += ceiling_addr - base;
24775 else
24777 while (start_byte > limit_byte)
24779 ceiling = BUFFER_FLOOR_OF (start_byte - 1);
24780 ceiling = max (limit_byte, ceiling);
24781 ceiling_addr = BYTE_POS_ADDR (ceiling);
24782 base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
24783 while (true)
24785 if (selective_display)
24787 while (--cursor >= ceiling_addr
24788 && *cursor != '\n' && *cursor != 015)
24789 continue;
24790 if (cursor < ceiling_addr)
24791 break;
24793 else
24795 cursor = memrchr (ceiling_addr, '\n', cursor - ceiling_addr);
24796 if (! cursor)
24797 break;
24800 if (++count == 0)
24802 start_byte += cursor - base + 1;
24803 *byte_pos_ptr = start_byte;
24804 /* When scanning backwards, we should
24805 not count the newline posterior to which we stop. */
24806 return - orig_count - 1;
24809 start_byte += ceiling_addr - base;
24813 *byte_pos_ptr = limit_byte;
24815 if (count < 0)
24816 return - orig_count + count;
24817 return orig_count - count;
24823 /***********************************************************************
24824 Displaying strings
24825 ***********************************************************************/
24827 /* Display a NUL-terminated string, starting with index START.
24829 If STRING is non-null, display that C string. Otherwise, the Lisp
24830 string LISP_STRING is displayed. There's a case that STRING is
24831 non-null and LISP_STRING is not nil. It means STRING is a string
24832 data of LISP_STRING. In that case, we display LISP_STRING while
24833 ignoring its text properties.
24835 If FACE_STRING is not nil, FACE_STRING_POS is a position in
24836 FACE_STRING. Display STRING or LISP_STRING with the face at
24837 FACE_STRING_POS in FACE_STRING:
24839 Display the string in the environment given by IT, but use the
24840 standard display table, temporarily.
24842 FIELD_WIDTH is the minimum number of output glyphs to produce.
24843 If STRING has fewer characters than FIELD_WIDTH, pad to the right
24844 with spaces. If STRING has more characters, more than FIELD_WIDTH
24845 glyphs will be produced. FIELD_WIDTH <= 0 means don't pad.
24847 PRECISION is the maximum number of characters to output from
24848 STRING. PRECISION < 0 means don't truncate the string.
24850 This is roughly equivalent to printf format specifiers:
24852 FIELD_WIDTH PRECISION PRINTF
24853 ----------------------------------------
24854 -1 -1 %s
24855 -1 10 %.10s
24856 10 -1 %10s
24857 20 10 %20.10s
24859 MULTIBYTE zero means do not display multibyte chars, > 0 means do
24860 display them, and < 0 means obey the current buffer's value of
24861 enable_multibyte_characters.
24863 Value is the number of columns displayed. */
24865 static int
24866 display_string (const char *string, Lisp_Object lisp_string, Lisp_Object face_string,
24867 ptrdiff_t face_string_pos, ptrdiff_t start, struct it *it,
24868 int field_width, int precision, int max_x, int multibyte)
24870 int hpos_at_start = it->hpos;
24871 int saved_face_id = it->face_id;
24872 struct glyph_row *row = it->glyph_row;
24873 ptrdiff_t it_charpos;
24875 /* Initialize the iterator IT for iteration over STRING beginning
24876 with index START. */
24877 reseat_to_string (it, NILP (lisp_string) ? string : NULL, lisp_string, start,
24878 precision, field_width, multibyte);
24879 if (string && STRINGP (lisp_string))
24880 /* LISP_STRING is the one returned by decode_mode_spec. We should
24881 ignore its text properties. */
24882 it->stop_charpos = it->end_charpos;
24884 /* If displaying STRING, set up the face of the iterator from
24885 FACE_STRING, if that's given. */
24886 if (STRINGP (face_string))
24888 ptrdiff_t endptr;
24889 struct face *face;
24891 it->face_id
24892 = face_at_string_position (it->w, face_string, face_string_pos,
24893 0, &endptr, it->base_face_id, false);
24894 face = FACE_FROM_ID (it->f, it->face_id);
24895 it->face_box_p = face->box != FACE_NO_BOX;
24898 /* Set max_x to the maximum allowed X position. Don't let it go
24899 beyond the right edge of the window. */
24900 if (max_x <= 0)
24901 max_x = it->last_visible_x;
24902 else
24903 max_x = min (max_x, it->last_visible_x);
24905 /* Skip over display elements that are not visible. because IT->w is
24906 hscrolled. */
24907 if (it->current_x < it->first_visible_x)
24908 move_it_in_display_line_to (it, 100000, it->first_visible_x,
24909 MOVE_TO_POS | MOVE_TO_X);
24911 row->ascent = it->max_ascent;
24912 row->height = it->max_ascent + it->max_descent;
24913 row->phys_ascent = it->max_phys_ascent;
24914 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
24915 row->extra_line_spacing = it->max_extra_line_spacing;
24917 if (STRINGP (it->string))
24918 it_charpos = IT_STRING_CHARPOS (*it);
24919 else
24920 it_charpos = IT_CHARPOS (*it);
24922 /* This condition is for the case that we are called with current_x
24923 past last_visible_x. */
24924 while (it->current_x < max_x)
24926 int x_before, x, n_glyphs_before, i, nglyphs;
24928 /* Get the next display element. */
24929 if (!get_next_display_element (it))
24930 break;
24932 /* Produce glyphs. */
24933 x_before = it->current_x;
24934 n_glyphs_before = row->used[TEXT_AREA];
24935 PRODUCE_GLYPHS (it);
24937 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
24938 i = 0;
24939 x = x_before;
24940 while (i < nglyphs)
24942 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
24944 if (it->line_wrap != TRUNCATE
24945 && x + glyph->pixel_width > max_x)
24947 /* End of continued line or max_x reached. */
24948 if (CHAR_GLYPH_PADDING_P (*glyph))
24950 /* A wide character is unbreakable. */
24951 if (row->reversed_p)
24952 unproduce_glyphs (it, row->used[TEXT_AREA]
24953 - n_glyphs_before);
24954 row->used[TEXT_AREA] = n_glyphs_before;
24955 it->current_x = x_before;
24957 else
24959 if (row->reversed_p)
24960 unproduce_glyphs (it, row->used[TEXT_AREA]
24961 - (n_glyphs_before + i));
24962 row->used[TEXT_AREA] = n_glyphs_before + i;
24963 it->current_x = x;
24965 break;
24967 else if (x + glyph->pixel_width >= it->first_visible_x)
24969 /* Glyph is at least partially visible. */
24970 ++it->hpos;
24971 if (x < it->first_visible_x)
24972 row->x = x - it->first_visible_x;
24974 else
24976 /* Glyph is off the left margin of the display area.
24977 Should not happen. */
24978 emacs_abort ();
24981 row->ascent = max (row->ascent, it->max_ascent);
24982 row->height = max (row->height, it->max_ascent + it->max_descent);
24983 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
24984 row->phys_height = max (row->phys_height,
24985 it->max_phys_ascent + it->max_phys_descent);
24986 row->extra_line_spacing = max (row->extra_line_spacing,
24987 it->max_extra_line_spacing);
24988 x += glyph->pixel_width;
24989 ++i;
24992 /* Stop if max_x reached. */
24993 if (i < nglyphs)
24994 break;
24996 /* Stop at line ends. */
24997 if (ITERATOR_AT_END_OF_LINE_P (it))
24999 it->continuation_lines_width = 0;
25000 break;
25003 set_iterator_to_next (it, true);
25004 if (STRINGP (it->string))
25005 it_charpos = IT_STRING_CHARPOS (*it);
25006 else
25007 it_charpos = IT_CHARPOS (*it);
25009 /* Stop if truncating at the right edge. */
25010 if (it->line_wrap == TRUNCATE
25011 && it->current_x >= it->last_visible_x)
25013 /* Add truncation mark, but don't do it if the line is
25014 truncated at a padding space. */
25015 if (it_charpos < it->string_nchars)
25017 if (!FRAME_WINDOW_P (it->f))
25019 int ii, n;
25021 if (it->current_x > it->last_visible_x)
25023 if (!row->reversed_p)
25025 for (ii = row->used[TEXT_AREA] - 1; ii > 0; --ii)
25026 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][ii]))
25027 break;
25029 else
25031 for (ii = 0; ii < row->used[TEXT_AREA]; ii++)
25032 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][ii]))
25033 break;
25034 unproduce_glyphs (it, ii + 1);
25035 ii = row->used[TEXT_AREA] - (ii + 1);
25037 for (n = row->used[TEXT_AREA]; ii < n; ++ii)
25039 row->used[TEXT_AREA] = ii;
25040 produce_special_glyphs (it, IT_TRUNCATION);
25043 produce_special_glyphs (it, IT_TRUNCATION);
25045 row->truncated_on_right_p = true;
25047 break;
25051 /* Maybe insert a truncation at the left. */
25052 if (it->first_visible_x
25053 && it_charpos > 0)
25055 if (!FRAME_WINDOW_P (it->f)
25056 || (row->reversed_p
25057 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
25058 : WINDOW_LEFT_FRINGE_WIDTH (it->w)) == 0)
25059 insert_left_trunc_glyphs (it);
25060 row->truncated_on_left_p = true;
25063 it->face_id = saved_face_id;
25065 /* Value is number of columns displayed. */
25066 return it->hpos - hpos_at_start;
25071 /* This is like a combination of memq and assq. Return 1/2 if PROPVAL
25072 appears as an element of LIST or as the car of an element of LIST.
25073 If PROPVAL is a list, compare each element against LIST in that
25074 way, and return 1/2 if any element of PROPVAL is found in LIST.
25075 Otherwise return 0. This function cannot quit.
25076 The return value is 2 if the text is invisible but with an ellipsis
25077 and 1 if it's invisible and without an ellipsis. */
25080 invisible_prop (Lisp_Object propval, Lisp_Object list)
25082 Lisp_Object tail, proptail;
25084 for (tail = list; CONSP (tail); tail = XCDR (tail))
25086 register Lisp_Object tem;
25087 tem = XCAR (tail);
25088 if (EQ (propval, tem))
25089 return 1;
25090 if (CONSP (tem) && EQ (propval, XCAR (tem)))
25091 return NILP (XCDR (tem)) ? 1 : 2;
25094 if (CONSP (propval))
25096 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
25098 Lisp_Object propelt;
25099 propelt = XCAR (proptail);
25100 for (tail = list; CONSP (tail); tail = XCDR (tail))
25102 register Lisp_Object tem;
25103 tem = XCAR (tail);
25104 if (EQ (propelt, tem))
25105 return 1;
25106 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
25107 return NILP (XCDR (tem)) ? 1 : 2;
25112 return 0;
25115 DEFUN ("invisible-p", Finvisible_p, Sinvisible_p, 1, 1, 0,
25116 doc: /* Non-nil if text properties at POS cause text there to be currently invisible.
25117 POS should be a marker or a buffer position; the value of the `invisible'
25118 property at that position in the current buffer is examined.
25119 POS can also be the actual value of the `invisible' text or overlay
25120 property of the text of interest, in which case the value itself is
25121 examined.
25123 The non-nil value returned can be t for currently invisible text that is
25124 entirely hidden on display, or some other non-nil, non-t value if the
25125 text is replaced by an ellipsis.
25127 Note that whether text with `invisible' property is actually hidden on
25128 display may depend on `buffer-invisibility-spec', which see. */)
25129 (Lisp_Object pos)
25131 Lisp_Object prop
25132 = (NATNUMP (pos) || MARKERP (pos)
25133 ? Fget_char_property (pos, Qinvisible, Qnil)
25134 : pos);
25135 int invis = TEXT_PROP_MEANS_INVISIBLE (prop);
25136 return (invis == 0 ? Qnil
25137 : invis == 1 ? Qt
25138 : make_number (invis));
25141 /* Calculate a width or height in pixels from a specification using
25142 the following elements:
25144 SPEC ::=
25145 NUM - a (fractional) multiple of the default font width/height
25146 (NUM) - specifies exactly NUM pixels
25147 UNIT - a fixed number of pixels, see below.
25148 ELEMENT - size of a display element in pixels, see below.
25149 (NUM . SPEC) - equals NUM * SPEC
25150 (+ SPEC SPEC ...) - add pixel values
25151 (- SPEC SPEC ...) - subtract pixel values
25152 (- SPEC) - negate pixel value
25154 NUM ::=
25155 INT or FLOAT - a number constant
25156 SYMBOL - use symbol's (buffer local) variable binding.
25158 UNIT ::=
25159 in - pixels per inch *)
25160 mm - pixels per 1/1000 meter *)
25161 cm - pixels per 1/100 meter *)
25162 width - width of current font in pixels.
25163 height - height of current font in pixels.
25165 *) using the ratio(s) defined in display-pixels-per-inch.
25167 ELEMENT ::=
25169 left-fringe - left fringe width in pixels
25170 right-fringe - right fringe width in pixels
25172 left-margin - left margin width in pixels
25173 right-margin - right margin width in pixels
25175 scroll-bar - scroll-bar area width in pixels
25177 Examples:
25179 Pixels corresponding to 5 inches:
25180 (5 . in)
25182 Total width of non-text areas on left side of window (if scroll-bar is on left):
25183 '(space :width (+ left-fringe left-margin scroll-bar))
25185 Align to first text column (in header line):
25186 '(space :align-to 0)
25188 Align to middle of text area minus half the width of variable `my-image'
25189 containing a loaded image:
25190 '(space :align-to (0.5 . (- text my-image)))
25192 Width of left margin minus width of 1 character in the default font:
25193 '(space :width (- left-margin 1))
25195 Width of left margin minus width of 2 characters in the current font:
25196 '(space :width (- left-margin (2 . width)))
25198 Center 1 character over left-margin (in header line):
25199 '(space :align-to (+ left-margin (0.5 . left-margin) -0.5))
25201 Different ways to express width of left fringe plus left margin minus one pixel:
25202 '(space :width (- (+ left-fringe left-margin) (1)))
25203 '(space :width (+ left-fringe left-margin (- (1))))
25204 '(space :width (+ left-fringe left-margin (-1)))
25206 If ALIGN_TO is NULL, returns the result in *RES. If ALIGN_TO is
25207 non-NULL, the value of *ALIGN_TO is a window-relative pixel
25208 coordinate, and *RES is the additional pixel width from that point
25209 till the end of the stretch glyph.
25211 WIDTH_P non-zero means take the width dimension or X coordinate of
25212 the object specified by PROP, WIDTH_P zero means take the height
25213 dimension or the Y coordinate. (Therefore, if ALIGN_TO is
25214 non-NULL, WIDTH_P should be non-zero.)
25216 FONT is the font of the face of the surrounding text.
25218 The return value is non-zero if width or height were successfully
25219 calculated, i.e. if PROP is a valid spec. */
25221 static bool
25222 calc_pixel_width_or_height (double *res, struct it *it, Lisp_Object prop,
25223 struct font *font, bool width_p, int *align_to)
25225 double pixels;
25227 # define OK_PIXELS(val) (*res = (val), true)
25228 # define OK_ALIGN_TO(val) (*align_to = (val), true)
25230 if (NILP (prop))
25231 return OK_PIXELS (0);
25233 eassert (FRAME_LIVE_P (it->f));
25235 if (SYMBOLP (prop))
25237 if (SCHARS (SYMBOL_NAME (prop)) == 2)
25239 char *unit = SSDATA (SYMBOL_NAME (prop));
25241 /* The UNIT expression, e.g. as part of (NUM . UNIT). */
25242 if (unit[0] == 'i' && unit[1] == 'n')
25243 pixels = 1.0;
25244 else if (unit[0] == 'm' && unit[1] == 'm')
25245 pixels = 25.4;
25246 else if (unit[0] == 'c' && unit[1] == 'm')
25247 pixels = 2.54;
25248 else
25249 pixels = 0;
25250 if (pixels > 0)
25252 double ppi = (width_p ? FRAME_RES_X (it->f)
25253 : FRAME_RES_Y (it->f));
25255 if (ppi > 0)
25256 return OK_PIXELS (ppi / pixels);
25257 return false;
25261 #ifdef HAVE_WINDOW_SYSTEM
25262 /* 'height': the height of FONT. */
25263 if (EQ (prop, Qheight))
25264 return OK_PIXELS (font
25265 ? normal_char_height (font, -1)
25266 : FRAME_LINE_HEIGHT (it->f));
25267 /* 'width': the width of FONT. */
25268 if (EQ (prop, Qwidth))
25269 return OK_PIXELS (font
25270 ? FONT_WIDTH (font)
25271 : FRAME_COLUMN_WIDTH (it->f));
25272 #else
25273 if (EQ (prop, Qheight) || EQ (prop, Qwidth))
25274 return OK_PIXELS (1);
25275 #endif
25277 /* 'text': the width or height of the text area. */
25278 if (EQ (prop, Qtext))
25279 return OK_PIXELS (width_p
25280 ? (window_box_width (it->w, TEXT_AREA)
25281 - it->lnum_pixel_width)
25282 : WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w));
25284 /* ':align_to'. First time we compute the value, window
25285 elements are interpreted as the position of the element's
25286 left edge. */
25287 if (align_to && *align_to < 0)
25289 *res = 0;
25290 /* 'left': left edge of the text area. */
25291 if (EQ (prop, Qleft))
25292 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA)
25293 + it->lnum_pixel_width);
25294 /* 'right': right edge of the text area. */
25295 if (EQ (prop, Qright))
25296 return OK_ALIGN_TO (window_box_right_offset (it->w, TEXT_AREA));
25297 /* 'center': the center of the text area. */
25298 if (EQ (prop, Qcenter))
25299 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA)
25300 + it->lnum_pixel_width
25301 + window_box_width (it->w, TEXT_AREA) / 2);
25302 /* 'left-fringe': left edge of the left fringe. */
25303 if (EQ (prop, Qleft_fringe))
25304 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
25305 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (it->w)
25306 : window_box_right_offset (it->w, LEFT_MARGIN_AREA));
25307 /* 'right-fringe': left edge of the right fringe. */
25308 if (EQ (prop, Qright_fringe))
25309 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
25310 ? window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
25311 : window_box_right_offset (it->w, TEXT_AREA));
25312 /* 'left-margin': left edge of the left display margin. */
25313 if (EQ (prop, Qleft_margin))
25314 return OK_ALIGN_TO (window_box_left_offset (it->w, LEFT_MARGIN_AREA));
25315 /* 'right-margin': left edge of the right display margin. */
25316 if (EQ (prop, Qright_margin))
25317 return OK_ALIGN_TO (window_box_left_offset (it->w, RIGHT_MARGIN_AREA));
25318 /* 'scroll-bar': left edge of the vertical scroll bar. */
25319 if (EQ (prop, Qscroll_bar))
25320 return OK_ALIGN_TO (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (it->w)
25322 : (window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
25323 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
25324 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
25325 : 0)));
25327 else
25329 /* Otherwise, the elements stand for their width. */
25330 if (EQ (prop, Qleft_fringe))
25331 return OK_PIXELS (WINDOW_LEFT_FRINGE_WIDTH (it->w));
25332 if (EQ (prop, Qright_fringe))
25333 return OK_PIXELS (WINDOW_RIGHT_FRINGE_WIDTH (it->w));
25334 if (EQ (prop, Qleft_margin))
25335 return OK_PIXELS (WINDOW_LEFT_MARGIN_WIDTH (it->w));
25336 if (EQ (prop, Qright_margin))
25337 return OK_PIXELS (WINDOW_RIGHT_MARGIN_WIDTH (it->w));
25338 if (EQ (prop, Qscroll_bar))
25339 return OK_PIXELS (WINDOW_SCROLL_BAR_AREA_WIDTH (it->w));
25342 prop = buffer_local_value (prop, it->w->contents);
25343 if (EQ (prop, Qunbound))
25344 prop = Qnil;
25347 if (NUMBERP (prop))
25349 int base_unit = (width_p
25350 ? FRAME_COLUMN_WIDTH (it->f)
25351 : FRAME_LINE_HEIGHT (it->f));
25352 if (width_p && align_to && *align_to < 0)
25353 return OK_PIXELS (XFLOATINT (prop) * base_unit + it->lnum_pixel_width);
25354 return OK_PIXELS (XFLOATINT (prop) * base_unit);
25357 if (CONSP (prop))
25359 Lisp_Object car = XCAR (prop);
25360 Lisp_Object cdr = XCDR (prop);
25362 if (SYMBOLP (car))
25364 #ifdef HAVE_WINDOW_SYSTEM
25365 /* '(image PROPS...)': width or height of the specified image. */
25366 if (FRAME_WINDOW_P (it->f)
25367 && valid_image_p (prop))
25369 ptrdiff_t id = lookup_image (it->f, prop);
25370 struct image *img = IMAGE_FROM_ID (it->f, id);
25372 return OK_PIXELS (width_p ? img->width : img->height);
25374 /* '(xwidget PROPS...)': dimensions of the specified xwidget. */
25375 if (FRAME_WINDOW_P (it->f) && valid_xwidget_spec_p (prop))
25377 /* TODO: Don't return dummy size. */
25378 return OK_PIXELS (100);
25380 #endif
25381 /* '(+ EXPR...)' or '(- EXPR...)' add or subtract
25382 recursively calculated values. */
25383 if (EQ (car, Qplus) || EQ (car, Qminus))
25385 bool first = true;
25386 double px;
25388 pixels = 0;
25389 while (CONSP (cdr))
25391 if (!calc_pixel_width_or_height (&px, it, XCAR (cdr),
25392 font, width_p, align_to))
25393 return false;
25394 if (first)
25395 pixels = (EQ (car, Qplus) ? px : -px), first = false;
25396 else
25397 pixels += px;
25398 cdr = XCDR (cdr);
25400 if (EQ (car, Qminus))
25401 pixels = -pixels;
25402 return OK_PIXELS (pixels);
25405 car = buffer_local_value (car, it->w->contents);
25406 if (EQ (car, Qunbound))
25407 car = Qnil;
25410 /* '(NUM)': absolute number of pixels. */
25411 if (NUMBERP (car))
25413 double fact;
25414 int offset =
25415 width_p && align_to && *align_to < 0 ? it->lnum_pixel_width : 0;
25416 pixels = XFLOATINT (car);
25417 if (NILP (cdr))
25418 return OK_PIXELS (pixels + offset);
25419 if (calc_pixel_width_or_height (&fact, it, cdr,
25420 font, width_p, align_to))
25421 return OK_PIXELS (pixels * fact + offset);
25422 return false;
25425 return false;
25428 return false;
25431 void
25432 get_font_ascent_descent (struct font *font, int *ascent, int *descent)
25434 #ifdef HAVE_WINDOW_SYSTEM
25435 normal_char_ascent_descent (font, -1, ascent, descent);
25436 #else
25437 *ascent = 1;
25438 *descent = 0;
25439 #endif
25443 /***********************************************************************
25444 Glyph Display
25445 ***********************************************************************/
25447 #ifdef HAVE_WINDOW_SYSTEM
25449 #ifdef GLYPH_DEBUG
25451 void
25452 dump_glyph_string (struct glyph_string *s)
25454 fprintf (stderr, "glyph string\n");
25455 fprintf (stderr, " x, y, w, h = %d, %d, %d, %d\n",
25456 s->x, s->y, s->width, s->height);
25457 fprintf (stderr, " ybase = %d\n", s->ybase);
25458 fprintf (stderr, " hl = %u\n", s->hl);
25459 fprintf (stderr, " left overhang = %d, right = %d\n",
25460 s->left_overhang, s->right_overhang);
25461 fprintf (stderr, " nchars = %d\n", s->nchars);
25462 fprintf (stderr, " extends to end of line = %d\n",
25463 s->extends_to_end_of_line_p);
25464 fprintf (stderr, " font height = %d\n", FONT_HEIGHT (s->font));
25465 fprintf (stderr, " bg width = %d\n", s->background_width);
25468 #endif /* GLYPH_DEBUG */
25470 /* Initialize glyph string S. CHAR2B is a suitably allocated vector
25471 of XChar2b structures for S; it can't be allocated in
25472 init_glyph_string because it must be allocated via `alloca'. W
25473 is the window on which S is drawn. ROW and AREA are the glyph row
25474 and area within the row from which S is constructed. START is the
25475 index of the first glyph structure covered by S. HL is a
25476 face-override for drawing S. */
25478 #ifdef HAVE_NTGUI
25479 #define OPTIONAL_HDC(hdc) HDC hdc,
25480 #define DECLARE_HDC(hdc) HDC hdc;
25481 #define ALLOCATE_HDC(hdc, f) hdc = get_frame_dc ((f))
25482 #define RELEASE_HDC(hdc, f) release_frame_dc ((f), (hdc))
25483 #endif
25485 #ifndef OPTIONAL_HDC
25486 #define OPTIONAL_HDC(hdc)
25487 #define DECLARE_HDC(hdc)
25488 #define ALLOCATE_HDC(hdc, f)
25489 #define RELEASE_HDC(hdc, f)
25490 #endif
25492 static void
25493 init_glyph_string (struct glyph_string *s,
25494 OPTIONAL_HDC (hdc)
25495 XChar2b *char2b, struct window *w, struct glyph_row *row,
25496 enum glyph_row_area area, int start, enum draw_glyphs_face hl)
25498 memset (s, 0, sizeof *s);
25499 s->w = w;
25500 s->f = XFRAME (w->frame);
25501 #ifdef HAVE_NTGUI
25502 s->hdc = hdc;
25503 #endif
25504 s->display = FRAME_X_DISPLAY (s->f);
25505 s->char2b = char2b;
25506 s->hl = hl;
25507 s->row = row;
25508 s->area = area;
25509 s->first_glyph = row->glyphs[area] + start;
25510 s->height = row->height;
25511 s->y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
25512 s->ybase = s->y + row->ascent;
25516 /* Append the list of glyph strings with head H and tail T to the list
25517 with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the result. */
25519 static void
25520 append_glyph_string_lists (struct glyph_string **head, struct glyph_string **tail,
25521 struct glyph_string *h, struct glyph_string *t)
25523 if (h)
25525 if (*head)
25526 (*tail)->next = h;
25527 else
25528 *head = h;
25529 h->prev = *tail;
25530 *tail = t;
25535 /* Prepend the list of glyph strings with head H and tail T to the
25536 list with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the
25537 result. */
25539 static void
25540 prepend_glyph_string_lists (struct glyph_string **head, struct glyph_string **tail,
25541 struct glyph_string *h, struct glyph_string *t)
25543 if (h)
25545 if (*head)
25546 (*head)->prev = t;
25547 else
25548 *tail = t;
25549 t->next = *head;
25550 *head = h;
25555 /* Append glyph string S to the list with head *HEAD and tail *TAIL.
25556 Set *HEAD and *TAIL to the resulting list. */
25558 static void
25559 append_glyph_string (struct glyph_string **head, struct glyph_string **tail,
25560 struct glyph_string *s)
25562 s->next = s->prev = NULL;
25563 append_glyph_string_lists (head, tail, s, s);
25567 /* Get face and two-byte form of character C in face FACE_ID on frame F.
25568 The encoding of C is returned in *CHAR2B. DISPLAY_P means
25569 make sure that X resources for the face returned are allocated.
25570 Value is a pointer to a realized face that is ready for display if
25571 DISPLAY_P. */
25573 static struct face *
25574 get_char_face_and_encoding (struct frame *f, int c, int face_id,
25575 XChar2b *char2b, bool display_p)
25577 struct face *face = FACE_FROM_ID (f, face_id);
25578 unsigned code = 0;
25580 if (face->font)
25582 code = face->font->driver->encode_char (face->font, c);
25584 if (code == FONT_INVALID_CODE)
25585 code = 0;
25587 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
25589 /* Make sure X resources of the face are allocated. */
25590 #ifdef HAVE_X_WINDOWS
25591 if (display_p)
25592 #endif
25594 eassert (face != NULL);
25595 prepare_face_for_display (f, face);
25598 return face;
25602 /* Get face and two-byte form of character glyph GLYPH on frame F.
25603 The encoding of GLYPH->u.ch is returned in *CHAR2B. Value is
25604 a pointer to a realized face that is ready for display. */
25606 static struct face *
25607 get_glyph_face_and_encoding (struct frame *f, struct glyph *glyph,
25608 XChar2b *char2b)
25610 struct face *face;
25611 unsigned code = 0;
25613 eassert (glyph->type == CHAR_GLYPH);
25614 face = FACE_FROM_ID (f, glyph->face_id);
25616 /* Make sure X resources of the face are allocated. */
25617 prepare_face_for_display (f, face);
25619 if (face->font)
25621 if (CHAR_BYTE8_P (glyph->u.ch))
25622 code = CHAR_TO_BYTE8 (glyph->u.ch);
25623 else
25624 code = face->font->driver->encode_char (face->font, glyph->u.ch);
25626 if (code == FONT_INVALID_CODE)
25627 code = 0;
25630 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
25631 return face;
25635 /* Get glyph code of character C in FONT in the two-byte form CHAR2B.
25636 Return true iff FONT has a glyph for C. */
25638 static bool
25639 get_char_glyph_code (int c, struct font *font, XChar2b *char2b)
25641 unsigned code;
25643 if (CHAR_BYTE8_P (c))
25644 code = CHAR_TO_BYTE8 (c);
25645 else
25646 code = font->driver->encode_char (font, c);
25648 if (code == FONT_INVALID_CODE)
25649 return false;
25650 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
25651 return true;
25655 /* Fill glyph string S with composition components specified by S->cmp.
25657 BASE_FACE is the base face of the composition.
25658 S->cmp_from is the index of the first component for S.
25660 OVERLAPS non-zero means S should draw the foreground only, and use
25661 its physical height for clipping. See also draw_glyphs.
25663 Value is the index of a component not in S. */
25665 static int
25666 fill_composite_glyph_string (struct glyph_string *s, struct face *base_face,
25667 int overlaps)
25669 int i;
25670 /* For all glyphs of this composition, starting at the offset
25671 S->cmp_from, until we reach the end of the definition or encounter a
25672 glyph that requires the different face, add it to S. */
25673 struct face *face;
25675 eassert (s);
25677 s->for_overlaps = overlaps;
25678 s->face = NULL;
25679 s->font = NULL;
25680 for (i = s->cmp_from; i < s->cmp->glyph_len; i++)
25682 int c = COMPOSITION_GLYPH (s->cmp, i);
25684 /* TAB in a composition means display glyphs with padding space
25685 on the left or right. */
25686 if (c != '\t')
25688 int face_id = FACE_FOR_CHAR (s->f, base_face->ascii_face, c,
25689 -1, Qnil);
25691 face = get_char_face_and_encoding (s->f, c, face_id,
25692 s->char2b + i, true);
25693 if (face)
25695 if (! s->face)
25697 s->face = face;
25698 s->font = s->face->font;
25700 else if (s->face != face)
25701 break;
25704 ++s->nchars;
25706 s->cmp_to = i;
25708 if (s->face == NULL)
25710 s->face = base_face->ascii_face;
25711 s->font = s->face->font;
25714 /* All glyph strings for the same composition has the same width,
25715 i.e. the width set for the first component of the composition. */
25716 s->width = s->first_glyph->pixel_width;
25718 /* If the specified font could not be loaded, use the frame's
25719 default font, but record the fact that we couldn't load it in
25720 the glyph string so that we can draw rectangles for the
25721 characters of the glyph string. */
25722 if (s->font == NULL)
25724 s->font_not_found_p = true;
25725 s->font = FRAME_FONT (s->f);
25728 /* Adjust base line for subscript/superscript text. */
25729 s->ybase += s->first_glyph->voffset;
25731 return s->cmp_to;
25734 static int
25735 fill_gstring_glyph_string (struct glyph_string *s, int face_id,
25736 int start, int end, int overlaps)
25738 struct glyph *glyph, *last;
25739 Lisp_Object lgstring;
25740 int i;
25742 s->for_overlaps = overlaps;
25743 glyph = s->row->glyphs[s->area] + start;
25744 last = s->row->glyphs[s->area] + end;
25745 s->cmp_id = glyph->u.cmp.id;
25746 s->cmp_from = glyph->slice.cmp.from;
25747 s->cmp_to = glyph->slice.cmp.to + 1;
25748 s->face = FACE_FROM_ID (s->f, face_id);
25749 lgstring = composition_gstring_from_id (s->cmp_id);
25750 s->font = XFONT_OBJECT (LGSTRING_FONT (lgstring));
25751 glyph++;
25752 while (glyph < last
25753 && glyph->u.cmp.automatic
25754 && glyph->u.cmp.id == s->cmp_id
25755 && s->cmp_to == glyph->slice.cmp.from)
25756 s->cmp_to = (glyph++)->slice.cmp.to + 1;
25758 for (i = s->cmp_from; i < s->cmp_to; i++)
25760 Lisp_Object lglyph = LGSTRING_GLYPH (lgstring, i);
25761 unsigned code = LGLYPH_CODE (lglyph);
25763 STORE_XCHAR2B ((s->char2b + i), code >> 8, code & 0xFF);
25765 s->width = composition_gstring_width (lgstring, s->cmp_from, s->cmp_to, NULL);
25766 return glyph - s->row->glyphs[s->area];
25770 /* Fill glyph string S from a sequence glyphs for glyphless characters.
25771 See the comment of fill_glyph_string for arguments.
25772 Value is the index of the first glyph not in S. */
25775 static int
25776 fill_glyphless_glyph_string (struct glyph_string *s, int face_id,
25777 int start, int end, int overlaps)
25779 struct glyph *glyph, *last;
25780 int voffset;
25782 eassert (s->first_glyph->type == GLYPHLESS_GLYPH);
25783 s->for_overlaps = overlaps;
25784 glyph = s->row->glyphs[s->area] + start;
25785 last = s->row->glyphs[s->area] + end;
25786 voffset = glyph->voffset;
25787 s->face = FACE_FROM_ID (s->f, face_id);
25788 s->font = s->face->font ? s->face->font : FRAME_FONT (s->f);
25789 s->nchars = 1;
25790 s->width = glyph->pixel_width;
25791 glyph++;
25792 while (glyph < last
25793 && glyph->type == GLYPHLESS_GLYPH
25794 && glyph->voffset == voffset
25795 && glyph->face_id == face_id)
25797 s->nchars++;
25798 s->width += glyph->pixel_width;
25799 glyph++;
25801 s->ybase += voffset;
25802 return glyph - s->row->glyphs[s->area];
25806 /* Fill glyph string S from a sequence of character glyphs.
25808 FACE_ID is the face id of the string. START is the index of the
25809 first glyph to consider, END is the index of the last + 1.
25810 OVERLAPS non-zero means S should draw the foreground only, and use
25811 its physical height for clipping. See also draw_glyphs.
25813 Value is the index of the first glyph not in S. */
25815 static int
25816 fill_glyph_string (struct glyph_string *s, int face_id,
25817 int start, int end, int overlaps)
25819 struct glyph *glyph, *last;
25820 int voffset;
25821 bool glyph_not_available_p;
25823 eassert (s->f == XFRAME (s->w->frame));
25824 eassert (s->nchars == 0);
25825 eassert (start >= 0 && end > start);
25827 s->for_overlaps = overlaps;
25828 glyph = s->row->glyphs[s->area] + start;
25829 last = s->row->glyphs[s->area] + end;
25830 voffset = glyph->voffset;
25831 s->padding_p = glyph->padding_p;
25832 glyph_not_available_p = glyph->glyph_not_available_p;
25834 while (glyph < last
25835 && glyph->type == CHAR_GLYPH
25836 && glyph->voffset == voffset
25837 /* Same face id implies same font, nowadays. */
25838 && glyph->face_id == face_id
25839 && glyph->glyph_not_available_p == glyph_not_available_p)
25841 s->face = get_glyph_face_and_encoding (s->f, glyph,
25842 s->char2b + s->nchars);
25843 ++s->nchars;
25844 eassert (s->nchars <= end - start);
25845 s->width += glyph->pixel_width;
25846 if (glyph++->padding_p != s->padding_p)
25847 break;
25850 s->font = s->face->font;
25852 /* If the specified font could not be loaded, use the frame's font,
25853 but record the fact that we couldn't load it in
25854 S->font_not_found_p so that we can draw rectangles for the
25855 characters of the glyph string. */
25856 if (s->font == NULL || glyph_not_available_p)
25858 s->font_not_found_p = true;
25859 s->font = FRAME_FONT (s->f);
25862 /* Adjust base line for subscript/superscript text. */
25863 s->ybase += voffset;
25865 eassert (s->face && s->face->gc);
25866 return glyph - s->row->glyphs[s->area];
25870 /* Fill glyph string S from image glyph S->first_glyph. */
25872 static void
25873 fill_image_glyph_string (struct glyph_string *s)
25875 eassert (s->first_glyph->type == IMAGE_GLYPH);
25876 s->img = IMAGE_FROM_ID (s->f, s->first_glyph->u.img_id);
25877 eassert (s->img);
25878 s->slice = s->first_glyph->slice.img;
25879 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
25880 s->font = s->face->font;
25881 s->width = s->first_glyph->pixel_width;
25883 /* Adjust base line for subscript/superscript text. */
25884 s->ybase += s->first_glyph->voffset;
25888 #ifdef HAVE_XWIDGETS
25889 static void
25890 fill_xwidget_glyph_string (struct glyph_string *s)
25892 eassert (s->first_glyph->type == XWIDGET_GLYPH);
25893 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
25894 s->font = s->face->font;
25895 s->width = s->first_glyph->pixel_width;
25896 s->ybase += s->first_glyph->voffset;
25897 s->xwidget = s->first_glyph->u.xwidget;
25899 #endif
25900 /* Fill glyph string S from a sequence of stretch glyphs.
25902 START is the index of the first glyph to consider,
25903 END is the index of the last + 1.
25905 Value is the index of the first glyph not in S. */
25907 static int
25908 fill_stretch_glyph_string (struct glyph_string *s, int start, int end)
25910 struct glyph *glyph, *last;
25911 int voffset, face_id;
25913 eassert (s->first_glyph->type == STRETCH_GLYPH);
25915 glyph = s->row->glyphs[s->area] + start;
25916 last = s->row->glyphs[s->area] + end;
25917 face_id = glyph->face_id;
25918 s->face = FACE_FROM_ID (s->f, face_id);
25919 s->font = s->face->font;
25920 s->width = glyph->pixel_width;
25921 s->nchars = 1;
25922 voffset = glyph->voffset;
25924 for (++glyph;
25925 (glyph < last
25926 && glyph->type == STRETCH_GLYPH
25927 && glyph->voffset == voffset
25928 && glyph->face_id == face_id);
25929 ++glyph)
25930 s->width += glyph->pixel_width;
25932 /* Adjust base line for subscript/superscript text. */
25933 s->ybase += voffset;
25935 /* The case that face->gc == 0 is handled when drawing the glyph
25936 string by calling prepare_face_for_display. */
25937 eassert (s->face);
25938 return glyph - s->row->glyphs[s->area];
25941 static struct font_metrics *
25942 get_per_char_metric (struct font *font, XChar2b *char2b)
25944 static struct font_metrics metrics;
25945 unsigned code;
25947 if (! font)
25948 return NULL;
25949 code = (XCHAR2B_BYTE1 (char2b) << 8) | XCHAR2B_BYTE2 (char2b);
25950 if (code == FONT_INVALID_CODE)
25951 return NULL;
25952 font->driver->text_extents (font, &code, 1, &metrics);
25953 return &metrics;
25956 /* A subroutine that computes "normal" values of ASCENT and DESCENT
25957 for FONT. Values are taken from font-global ones, except for fonts
25958 that claim preposterously large values, but whose glyphs actually
25959 have reasonable dimensions. C is the character to use for metrics
25960 if the font-global values are too large; if C is negative, the
25961 function selects a default character. */
25962 static void
25963 normal_char_ascent_descent (struct font *font, int c, int *ascent, int *descent)
25965 *ascent = FONT_BASE (font);
25966 *descent = FONT_DESCENT (font);
25968 if (FONT_TOO_HIGH (font))
25970 XChar2b char2b;
25972 /* Get metrics of C, defaulting to a reasonably sized ASCII
25973 character. */
25974 if (get_char_glyph_code (c >= 0 ? c : '{', font, &char2b))
25976 struct font_metrics *pcm = get_per_char_metric (font, &char2b);
25978 if (!(pcm->width == 0 && pcm->rbearing == 0 && pcm->lbearing == 0))
25980 /* We add 1 pixel to character dimensions as heuristics
25981 that produces nicer display, e.g. when the face has
25982 the box attribute. */
25983 *ascent = pcm->ascent + 1;
25984 *descent = pcm->descent + 1;
25990 /* A subroutine that computes a reasonable "normal character height"
25991 for fonts that claim preposterously large vertical dimensions, but
25992 whose glyphs are actually reasonably sized. C is the character
25993 whose metrics to use for those fonts, or -1 for default
25994 character. */
25995 static int
25996 normal_char_height (struct font *font, int c)
25998 int ascent, descent;
26000 normal_char_ascent_descent (font, c, &ascent, &descent);
26002 return ascent + descent;
26005 /* EXPORT for RIF:
26006 Set *LEFT and *RIGHT to the left and right overhang of GLYPH on
26007 frame F. Overhangs of glyphs other than type CHAR_GLYPH are
26008 assumed to be zero. */
26010 void
26011 x_get_glyph_overhangs (struct glyph *glyph, struct frame *f, int *left, int *right)
26013 *left = *right = 0;
26015 if (glyph->type == CHAR_GLYPH)
26017 XChar2b char2b;
26018 struct face *face = get_glyph_face_and_encoding (f, glyph, &char2b);
26019 if (face->font)
26021 struct font_metrics *pcm = get_per_char_metric (face->font, &char2b);
26022 if (pcm)
26024 if (pcm->rbearing > pcm->width)
26025 *right = pcm->rbearing - pcm->width;
26026 if (pcm->lbearing < 0)
26027 *left = -pcm->lbearing;
26031 else if (glyph->type == COMPOSITE_GLYPH)
26033 if (! glyph->u.cmp.automatic)
26035 struct composition *cmp = composition_table[glyph->u.cmp.id];
26037 if (cmp->rbearing > cmp->pixel_width)
26038 *right = cmp->rbearing - cmp->pixel_width;
26039 if (cmp->lbearing < 0)
26040 *left = - cmp->lbearing;
26042 else
26044 Lisp_Object gstring = composition_gstring_from_id (glyph->u.cmp.id);
26045 struct font_metrics metrics;
26047 composition_gstring_width (gstring, glyph->slice.cmp.from,
26048 glyph->slice.cmp.to + 1, &metrics);
26049 if (metrics.rbearing > metrics.width)
26050 *right = metrics.rbearing - metrics.width;
26051 if (metrics.lbearing < 0)
26052 *left = - metrics.lbearing;
26058 /* Return the index of the first glyph preceding glyph string S that
26059 is overwritten by S because of S's left overhang. Value is -1
26060 if no glyphs are overwritten. */
26062 static int
26063 left_overwritten (struct glyph_string *s)
26065 int k;
26067 if (s->left_overhang)
26069 int x = 0, i;
26070 struct glyph *glyphs = s->row->glyphs[s->area];
26071 int first = s->first_glyph - glyphs;
26073 for (i = first - 1; i >= 0 && x > -s->left_overhang; --i)
26074 x -= glyphs[i].pixel_width;
26076 k = i + 1;
26078 else
26079 k = -1;
26081 return k;
26085 /* Return the index of the first glyph preceding glyph string S that
26086 is overwriting S because of its right overhang. Value is -1 if no
26087 glyph in front of S overwrites S. */
26089 static int
26090 left_overwriting (struct glyph_string *s)
26092 int i, k, x;
26093 struct glyph *glyphs = s->row->glyphs[s->area];
26094 int first = s->first_glyph - glyphs;
26096 k = -1;
26097 x = 0;
26098 for (i = first - 1; i >= 0; --i)
26100 int left, right;
26101 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
26102 if (x + right > 0)
26103 k = i;
26104 x -= glyphs[i].pixel_width;
26107 return k;
26111 /* Return the index of the last glyph following glyph string S that is
26112 overwritten by S because of S's right overhang. Value is -1 if
26113 no such glyph is found. */
26115 static int
26116 right_overwritten (struct glyph_string *s)
26118 int k = -1;
26120 if (s->right_overhang)
26122 int x = 0, i;
26123 struct glyph *glyphs = s->row->glyphs[s->area];
26124 int first = (s->first_glyph - glyphs
26125 + (s->first_glyph->type == COMPOSITE_GLYPH ? 1 : s->nchars));
26126 int end = s->row->used[s->area];
26128 for (i = first; i < end && s->right_overhang > x; ++i)
26129 x += glyphs[i].pixel_width;
26131 k = i;
26134 return k;
26138 /* Return the index of the last glyph following glyph string S that
26139 overwrites S because of its left overhang. Value is negative
26140 if no such glyph is found. */
26142 static int
26143 right_overwriting (struct glyph_string *s)
26145 int i, k, x;
26146 int end = s->row->used[s->area];
26147 struct glyph *glyphs = s->row->glyphs[s->area];
26148 int first = (s->first_glyph - glyphs
26149 + (s->first_glyph->type == COMPOSITE_GLYPH ? 1 : s->nchars));
26151 k = -1;
26152 x = 0;
26153 for (i = first; i < end; ++i)
26155 int left, right;
26156 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
26157 if (x - left < 0)
26158 k = i;
26159 x += glyphs[i].pixel_width;
26162 return k;
26166 /* Set background width of glyph string S. START is the index of the
26167 first glyph following S. LAST_X is the right-most x-position + 1
26168 in the drawing area. */
26170 static void
26171 set_glyph_string_background_width (struct glyph_string *s, int start, int last_x)
26173 /* If the face of this glyph string has to be drawn to the end of
26174 the drawing area, set S->extends_to_end_of_line_p. */
26176 if (start == s->row->used[s->area]
26177 && ((s->row->fill_line_p
26178 && (s->hl == DRAW_NORMAL_TEXT
26179 || s->hl == DRAW_IMAGE_RAISED
26180 || s->hl == DRAW_IMAGE_SUNKEN))
26181 || s->hl == DRAW_MOUSE_FACE))
26182 s->extends_to_end_of_line_p = true;
26184 /* If S extends its face to the end of the line, set its
26185 background_width to the distance to the right edge of the drawing
26186 area. */
26187 if (s->extends_to_end_of_line_p)
26188 s->background_width = last_x - s->x + 1;
26189 else
26190 s->background_width = s->width;
26194 /* Return glyph string that shares background with glyph string S and
26195 whose `background_width' member has been set. */
26197 static struct glyph_string *
26198 glyph_string_containing_background_width (struct glyph_string *s)
26200 if (s->cmp)
26201 while (s->cmp_from)
26202 s = s->prev;
26204 return s;
26208 /* Compute overhangs and x-positions for glyph string S and its
26209 predecessors, or successors. X is the starting x-position for S.
26210 BACKWARD_P means process predecessors. */
26212 static void
26213 compute_overhangs_and_x (struct glyph_string *s, int x, bool backward_p)
26215 if (backward_p)
26217 while (s)
26219 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
26220 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
26221 if (!s->cmp || s->cmp_to == s->cmp->glyph_len)
26222 x -= s->width;
26223 s->x = x;
26224 s = s->prev;
26227 else
26229 while (s)
26231 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
26232 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
26233 s->x = x;
26234 if (!s->cmp || s->cmp_to == s->cmp->glyph_len)
26235 x += s->width;
26236 s = s->next;
26243 /* The following macros are only called from draw_glyphs below.
26244 They reference the following parameters of that function directly:
26245 `w', `row', `area', and `overlap_p'
26246 as well as the following local variables:
26247 `s', `f', and `hdc' (in W32) */
26249 #ifdef HAVE_NTGUI
26250 /* On W32, silently add local `hdc' variable to argument list of
26251 init_glyph_string. */
26252 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
26253 init_glyph_string (s, hdc, char2b, w, row, area, start, hl)
26254 #else
26255 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
26256 init_glyph_string (s, char2b, w, row, area, start, hl)
26257 #endif
26259 /* Add a glyph string for a stretch glyph to the list of strings
26260 between HEAD and TAIL. START is the index of the stretch glyph in
26261 row area AREA of glyph row ROW. END is the index of the last glyph
26262 in that glyph row area. X is the current output position assigned
26263 to the new glyph string constructed. HL overrides that face of the
26264 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
26265 is the right-most x-position of the drawing area. */
26267 /* SunOS 4 bundled cc, barfed on continuations in the arg lists here
26268 and below -- keep them on one line. */
26269 #define BUILD_STRETCH_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
26270 do \
26272 s = alloca (sizeof *s); \
26273 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
26274 START = fill_stretch_glyph_string (s, START, END); \
26275 append_glyph_string (&HEAD, &TAIL, s); \
26276 s->x = (X); \
26278 while (false)
26281 /* Add a glyph string for an image glyph to the list of strings
26282 between HEAD and TAIL. START is the index of the image glyph in
26283 row area AREA of glyph row ROW. END is the index of the last glyph
26284 in that glyph row area. X is the current output position assigned
26285 to the new glyph string constructed. HL overrides that face of the
26286 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
26287 is the right-most x-position of the drawing area. */
26289 #define BUILD_IMAGE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
26290 do \
26292 s = alloca (sizeof *s); \
26293 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
26294 fill_image_glyph_string (s); \
26295 append_glyph_string (&HEAD, &TAIL, s); \
26296 ++START; \
26297 s->x = (X); \
26299 while (false)
26301 #ifndef HAVE_XWIDGETS
26302 # define BUILD_XWIDGET_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
26303 eassume (false)
26304 #else
26305 # define BUILD_XWIDGET_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
26306 do \
26308 s = alloca (sizeof *s); \
26309 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
26310 fill_xwidget_glyph_string (s); \
26311 append_glyph_string (&(HEAD), &(TAIL), s); \
26312 ++(START); \
26313 s->x = (X); \
26315 while (false)
26316 #endif
26318 /* Add a glyph string for a sequence of character glyphs to the list
26319 of strings between HEAD and TAIL. START is the index of the first
26320 glyph in row area AREA of glyph row ROW that is part of the new
26321 glyph string. END is the index of the last glyph in that glyph row
26322 area. X is the current output position assigned to the new glyph
26323 string constructed. HL overrides that face of the glyph; e.g. it
26324 is DRAW_CURSOR if a cursor has to be drawn. LAST_X is the
26325 right-most x-position of the drawing area. */
26327 #define BUILD_CHAR_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
26328 do \
26330 int face_id; \
26331 XChar2b *char2b; \
26333 face_id = (row)->glyphs[area][START].face_id; \
26335 s = alloca (sizeof *s); \
26336 SAFE_NALLOCA (char2b, 1, (END) - (START)); \
26337 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
26338 append_glyph_string (&HEAD, &TAIL, s); \
26339 s->x = (X); \
26340 START = fill_glyph_string (s, face_id, START, END, overlaps); \
26342 while (false)
26345 /* Add a glyph string for a composite sequence to the list of strings
26346 between HEAD and TAIL. START is the index of the first glyph in
26347 row area AREA of glyph row ROW that is part of the new glyph
26348 string. END is the index of the last glyph in that glyph row area.
26349 X is the current output position assigned to the new glyph string
26350 constructed. HL overrides that face of the glyph; e.g. it is
26351 DRAW_CURSOR if a cursor has to be drawn. LAST_X is the right-most
26352 x-position of the drawing area. */
26354 #define BUILD_COMPOSITE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
26355 do { \
26356 int face_id = (row)->glyphs[area][START].face_id; \
26357 struct face *base_face = FACE_FROM_ID (f, face_id); \
26358 ptrdiff_t cmp_id = (row)->glyphs[area][START].u.cmp.id; \
26359 struct composition *cmp = composition_table[cmp_id]; \
26360 XChar2b *char2b; \
26361 struct glyph_string *first_s = NULL; \
26362 int n; \
26364 SAFE_NALLOCA (char2b, 1, cmp->glyph_len); \
26366 /* Make glyph_strings for each glyph sequence that is drawable by \
26367 the same face, and append them to HEAD/TAIL. */ \
26368 for (n = 0; n < cmp->glyph_len;) \
26370 s = alloca (sizeof *s); \
26371 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
26372 append_glyph_string (&(HEAD), &(TAIL), s); \
26373 s->cmp = cmp; \
26374 s->cmp_from = n; \
26375 s->x = (X); \
26376 if (n == 0) \
26377 first_s = s; \
26378 n = fill_composite_glyph_string (s, base_face, overlaps); \
26381 ++START; \
26382 s = first_s; \
26383 } while (false)
26386 /* Add a glyph string for a glyph-string sequence to the list of strings
26387 between HEAD and TAIL. */
26389 #define BUILD_GSTRING_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
26390 do { \
26391 int face_id; \
26392 XChar2b *char2b; \
26393 Lisp_Object gstring; \
26395 face_id = (row)->glyphs[area][START].face_id; \
26396 gstring = (composition_gstring_from_id \
26397 ((row)->glyphs[area][START].u.cmp.id)); \
26398 s = alloca (sizeof *s); \
26399 SAFE_NALLOCA (char2b, 1, LGSTRING_GLYPH_LEN (gstring)); \
26400 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
26401 append_glyph_string (&(HEAD), &(TAIL), s); \
26402 s->x = (X); \
26403 START = fill_gstring_glyph_string (s, face_id, START, END, overlaps); \
26404 } while (false)
26407 /* Add a glyph string for a sequence of glyphless character's glyphs
26408 to the list of strings between HEAD and TAIL. The meanings of
26409 arguments are the same as those of BUILD_CHAR_GLYPH_STRINGS. */
26411 #define BUILD_GLYPHLESS_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
26412 do \
26414 int face_id; \
26416 face_id = (row)->glyphs[area][START].face_id; \
26418 s = alloca (sizeof *s); \
26419 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
26420 append_glyph_string (&HEAD, &TAIL, s); \
26421 s->x = (X); \
26422 START = fill_glyphless_glyph_string (s, face_id, START, END, \
26423 overlaps); \
26425 while (false)
26428 /* Build a list of glyph strings between HEAD and TAIL for the glyphs
26429 of AREA of glyph row ROW on window W between indices START and END.
26430 HL overrides the face for drawing glyph strings, e.g. it is
26431 DRAW_CURSOR to draw a cursor. X and LAST_X are start and end
26432 x-positions of the drawing area.
26434 This is an ugly monster macro construct because we must use alloca
26435 to allocate glyph strings (because draw_glyphs can be called
26436 asynchronously). */
26438 #define BUILD_GLYPH_STRINGS_1(START, END, HEAD, TAIL, HL, X, LAST_X) \
26439 do \
26441 HEAD = TAIL = NULL; \
26442 while (START < END) \
26444 struct glyph *first_glyph = (row)->glyphs[area] + START; \
26445 switch (first_glyph->type) \
26447 case CHAR_GLYPH: \
26448 BUILD_CHAR_GLYPH_STRINGS (START, END, HEAD, TAIL, \
26449 HL, X, LAST_X); \
26450 break; \
26452 case COMPOSITE_GLYPH: \
26453 if (first_glyph->u.cmp.automatic) \
26454 BUILD_GSTRING_GLYPH_STRING (START, END, HEAD, TAIL, \
26455 HL, X, LAST_X); \
26456 else \
26457 BUILD_COMPOSITE_GLYPH_STRING (START, END, HEAD, TAIL, \
26458 HL, X, LAST_X); \
26459 break; \
26461 case STRETCH_GLYPH: \
26462 BUILD_STRETCH_GLYPH_STRING (START, END, HEAD, TAIL, \
26463 HL, X, LAST_X); \
26464 break; \
26466 case IMAGE_GLYPH: \
26467 BUILD_IMAGE_GLYPH_STRING (START, END, HEAD, TAIL, \
26468 HL, X, LAST_X); \
26469 break;
26471 #define BUILD_GLYPH_STRINGS_XW(START, END, HEAD, TAIL, HL, X, LAST_X) \
26472 case XWIDGET_GLYPH: \
26473 BUILD_XWIDGET_GLYPH_STRING (START, END, HEAD, TAIL, \
26474 HL, X, LAST_X); \
26475 break;
26477 #define BUILD_GLYPH_STRINGS_2(START, END, HEAD, TAIL, HL, X, LAST_X) \
26478 case GLYPHLESS_GLYPH: \
26479 BUILD_GLYPHLESS_GLYPH_STRING (START, END, HEAD, TAIL, \
26480 HL, X, LAST_X); \
26481 break; \
26483 default: \
26484 emacs_abort (); \
26487 if (s) \
26489 set_glyph_string_background_width (s, START, LAST_X); \
26490 (X) += s->width; \
26493 } while (false)
26496 #define BUILD_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
26497 BUILD_GLYPH_STRINGS_1(START, END, HEAD, TAIL, HL, X, LAST_X) \
26498 BUILD_GLYPH_STRINGS_XW(START, END, HEAD, TAIL, HL, X, LAST_X) \
26499 BUILD_GLYPH_STRINGS_2(START, END, HEAD, TAIL, HL, X, LAST_X)
26502 /* Draw glyphs between START and END in AREA of ROW on window W,
26503 starting at x-position X. X is relative to AREA in W. HL is a
26504 face-override with the following meaning:
26506 DRAW_NORMAL_TEXT draw normally
26507 DRAW_CURSOR draw in cursor face
26508 DRAW_MOUSE_FACE draw in mouse face.
26509 DRAW_INVERSE_VIDEO draw in mode line face
26510 DRAW_IMAGE_SUNKEN draw an image with a sunken relief around it
26511 DRAW_IMAGE_RAISED draw an image with a raised relief around it
26513 If OVERLAPS is non-zero, draw only the foreground of characters and
26514 clip to the physical height of ROW. Non-zero value also defines
26515 the overlapping part to be drawn:
26517 OVERLAPS_PRED overlap with preceding rows
26518 OVERLAPS_SUCC overlap with succeeding rows
26519 OVERLAPS_BOTH overlap with both preceding/succeeding rows
26520 OVERLAPS_ERASED_CURSOR overlap with erased cursor area
26522 Value is the x-position reached, relative to AREA of W. */
26524 static int
26525 draw_glyphs (struct window *w, int x, struct glyph_row *row,
26526 enum glyph_row_area area, ptrdiff_t start, ptrdiff_t end,
26527 enum draw_glyphs_face hl, int overlaps)
26529 struct glyph_string *head, *tail;
26530 struct glyph_string *s;
26531 struct glyph_string *clip_head = NULL, *clip_tail = NULL;
26532 int i, j, x_reached, last_x, area_left = 0;
26533 struct frame *f = XFRAME (WINDOW_FRAME (w));
26534 DECLARE_HDC (hdc);
26536 ALLOCATE_HDC (hdc, f);
26538 /* Let's rather be paranoid than getting a SEGV. */
26539 end = min (end, row->used[area]);
26540 start = clip_to_bounds (0, start, end);
26542 /* Translate X to frame coordinates. Set last_x to the right
26543 end of the drawing area. */
26544 if (row->full_width_p)
26546 /* X is relative to the left edge of W, without scroll bars
26547 or fringes. */
26548 area_left = WINDOW_LEFT_EDGE_X (w);
26549 last_x = (WINDOW_LEFT_EDGE_X (w) + WINDOW_PIXEL_WIDTH (w)
26550 - (row->mode_line_p ? WINDOW_RIGHT_DIVIDER_WIDTH (w) : 0));
26552 else
26554 area_left = window_box_left (w, area);
26555 last_x = area_left + window_box_width (w, area);
26557 x += area_left;
26559 /* Build a doubly-linked list of glyph_string structures between
26560 head and tail from what we have to draw. Note that the macro
26561 BUILD_GLYPH_STRINGS will modify its start parameter. That's
26562 the reason we use a separate variable `i'. */
26563 i = start;
26564 USE_SAFE_ALLOCA;
26565 BUILD_GLYPH_STRINGS (i, end, head, tail, hl, x, last_x);
26566 if (tail)
26568 s = glyph_string_containing_background_width (tail);
26569 x_reached = s->x + s->background_width;
26571 else
26572 x_reached = x;
26574 /* If there are any glyphs with lbearing < 0 or rbearing > width in
26575 the row, redraw some glyphs in front or following the glyph
26576 strings built above. */
26577 if (head && !overlaps && row->contains_overlapping_glyphs_p)
26579 struct glyph_string *h, *t;
26580 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
26581 int mouse_beg_col UNINIT, mouse_end_col UNINIT;
26582 bool check_mouse_face = false;
26583 int dummy_x = 0;
26585 /* If mouse highlighting is on, we may need to draw adjacent
26586 glyphs using mouse-face highlighting. */
26587 if (area == TEXT_AREA && row->mouse_face_p
26588 && hlinfo->mouse_face_beg_row >= 0
26589 && hlinfo->mouse_face_end_row >= 0)
26591 ptrdiff_t row_vpos = MATRIX_ROW_VPOS (row, w->current_matrix);
26593 if (row_vpos >= hlinfo->mouse_face_beg_row
26594 && row_vpos <= hlinfo->mouse_face_end_row)
26596 check_mouse_face = true;
26597 mouse_beg_col = (row_vpos == hlinfo->mouse_face_beg_row)
26598 ? hlinfo->mouse_face_beg_col : 0;
26599 mouse_end_col = (row_vpos == hlinfo->mouse_face_end_row)
26600 ? hlinfo->mouse_face_end_col
26601 : row->used[TEXT_AREA];
26605 /* Compute overhangs for all glyph strings. */
26606 if (FRAME_RIF (f)->compute_glyph_string_overhangs)
26607 for (s = head; s; s = s->next)
26608 FRAME_RIF (f)->compute_glyph_string_overhangs (s);
26610 /* Prepend glyph strings for glyphs in front of the first glyph
26611 string that are overwritten because of the first glyph
26612 string's left overhang. The background of all strings
26613 prepended must be drawn because the first glyph string
26614 draws over it. */
26615 i = left_overwritten (head);
26616 if (i >= 0)
26618 enum draw_glyphs_face overlap_hl;
26620 /* If this row contains mouse highlighting, attempt to draw
26621 the overlapped glyphs with the correct highlight. This
26622 code fails if the overlap encompasses more than one glyph
26623 and mouse-highlight spans only some of these glyphs.
26624 However, making it work perfectly involves a lot more
26625 code, and I don't know if the pathological case occurs in
26626 practice, so we'll stick to this for now. --- cyd */
26627 if (check_mouse_face
26628 && mouse_beg_col < start && mouse_end_col > i)
26629 overlap_hl = DRAW_MOUSE_FACE;
26630 else
26631 overlap_hl = DRAW_NORMAL_TEXT;
26633 if (hl != overlap_hl)
26634 clip_head = head;
26635 j = i;
26636 BUILD_GLYPH_STRINGS (j, start, h, t,
26637 overlap_hl, dummy_x, last_x);
26638 start = i;
26639 compute_overhangs_and_x (t, head->x, true);
26640 prepend_glyph_string_lists (&head, &tail, h, t);
26641 if (clip_head == NULL)
26642 clip_head = head;
26645 /* Prepend glyph strings for glyphs in front of the first glyph
26646 string that overwrite that glyph string because of their
26647 right overhang. For these strings, only the foreground must
26648 be drawn, because it draws over the glyph string at `head'.
26649 The background must not be drawn because this would overwrite
26650 right overhangs of preceding glyphs for which no glyph
26651 strings exist. */
26652 i = left_overwriting (head);
26653 if (i >= 0)
26655 enum draw_glyphs_face overlap_hl;
26657 if (check_mouse_face
26658 && mouse_beg_col < start && mouse_end_col > i)
26659 overlap_hl = DRAW_MOUSE_FACE;
26660 else
26661 overlap_hl = DRAW_NORMAL_TEXT;
26663 if (hl == overlap_hl || clip_head == NULL)
26664 clip_head = head;
26665 BUILD_GLYPH_STRINGS (i, start, h, t,
26666 overlap_hl, dummy_x, last_x);
26667 for (s = h; s; s = s->next)
26668 s->background_filled_p = true;
26669 compute_overhangs_and_x (t, head->x, true);
26670 prepend_glyph_string_lists (&head, &tail, h, t);
26673 /* Append glyphs strings for glyphs following the last glyph
26674 string tail that are overwritten by tail. The background of
26675 these strings has to be drawn because tail's foreground draws
26676 over it. */
26677 i = right_overwritten (tail);
26678 if (i >= 0)
26680 enum draw_glyphs_face overlap_hl;
26682 if (check_mouse_face
26683 && mouse_beg_col < i && mouse_end_col > end)
26684 overlap_hl = DRAW_MOUSE_FACE;
26685 else
26686 overlap_hl = DRAW_NORMAL_TEXT;
26688 if (hl != overlap_hl)
26689 clip_tail = tail;
26690 BUILD_GLYPH_STRINGS (end, i, h, t,
26691 overlap_hl, x, last_x);
26692 /* Because BUILD_GLYPH_STRINGS updates the first argument,
26693 we don't have `end = i;' here. */
26694 compute_overhangs_and_x (h, tail->x + tail->width, false);
26695 append_glyph_string_lists (&head, &tail, h, t);
26696 if (clip_tail == NULL)
26697 clip_tail = tail;
26700 /* Append glyph strings for glyphs following the last glyph
26701 string tail that overwrite tail. The foreground of such
26702 glyphs has to be drawn because it writes into the background
26703 of tail. The background must not be drawn because it could
26704 paint over the foreground of following glyphs. */
26705 i = right_overwriting (tail);
26706 if (i >= 0)
26708 enum draw_glyphs_face overlap_hl;
26709 if (check_mouse_face
26710 && mouse_beg_col < i && mouse_end_col > end)
26711 overlap_hl = DRAW_MOUSE_FACE;
26712 else
26713 overlap_hl = DRAW_NORMAL_TEXT;
26715 if (hl == overlap_hl || clip_tail == NULL)
26716 clip_tail = tail;
26717 i++; /* We must include the Ith glyph. */
26718 BUILD_GLYPH_STRINGS (end, i, h, t,
26719 overlap_hl, x, last_x);
26720 for (s = h; s; s = s->next)
26721 s->background_filled_p = true;
26722 compute_overhangs_and_x (h, tail->x + tail->width, false);
26723 append_glyph_string_lists (&head, &tail, h, t);
26725 tail = glyph_string_containing_background_width (tail);
26726 if (clip_tail)
26727 clip_tail = glyph_string_containing_background_width (clip_tail);
26728 if (clip_head || clip_tail)
26729 for (s = head; s; s = s->next)
26731 s->clip_head = clip_head;
26732 s->clip_tail = clip_tail;
26736 /* Draw all strings. */
26737 for (s = head; s; s = s->next)
26738 FRAME_RIF (f)->draw_glyph_string (s);
26740 #ifndef HAVE_NS
26741 /* When focus a sole frame and move horizontally, this clears on_p
26742 causing a failure to erase prev cursor position. */
26743 if (area == TEXT_AREA
26744 && !row->full_width_p
26745 /* When drawing overlapping rows, only the glyph strings'
26746 foreground is drawn, which doesn't erase a cursor
26747 completely. */
26748 && !overlaps)
26750 int x0 = clip_head ? clip_head->x : (head ? head->x : x);
26751 int x1 = (clip_tail ? clip_tail->x + clip_tail->background_width
26752 : (tail ? tail->x + tail->background_width : x));
26753 x0 -= area_left;
26754 x1 -= area_left;
26756 notice_overwritten_cursor (w, TEXT_AREA, x0, x1,
26757 row->y, MATRIX_ROW_BOTTOM_Y (row));
26759 #endif
26761 /* Value is the x-position up to which drawn, relative to AREA of W.
26762 This doesn't include parts drawn because of overhangs. */
26763 if (row->full_width_p)
26764 x_reached = FRAME_TO_WINDOW_PIXEL_X (w, x_reached);
26765 else
26766 x_reached -= area_left;
26768 RELEASE_HDC (hdc, f);
26770 SAFE_FREE ();
26771 return x_reached;
26774 /* Find the first glyph in the run of underlined glyphs preceding the
26775 beginning of glyph string S, and return its font (which could be
26776 NULL). This is needed because that font determines the underline
26777 position and thickness for the entire run of the underlined glyphs.
26778 This function is called from the draw_glyph_string method of GUI
26779 frame's redisplay interface (RIF) when it needs to draw in an
26780 underlined face. */
26781 struct font *
26782 font_for_underline_metrics (struct glyph_string *s)
26784 struct glyph *g0 = s->row->glyphs[s->area], *g;
26786 for (g = s->first_glyph - 1; g >= g0; g--)
26788 struct face *prev_face = FACE_FROM_ID (s->f, g->face_id);
26789 if (!(prev_face && prev_face->underline_p))
26790 break;
26793 /* If preceding glyphs are not underlined, use the font of S. */
26794 if (g == s->first_glyph - 1)
26795 return s->font;
26796 else
26798 /* Otherwise use the font of the last glyph we saw in the above
26799 loop whose face had the underline_p flag set. */
26800 return FACE_FROM_ID (s->f, g[1].face_id)->font;
26804 /* Expand row matrix if too narrow. Don't expand if area
26805 is not present. */
26807 #define IT_EXPAND_MATRIX_WIDTH(it, area) \
26809 if (!it->f->fonts_changed \
26810 && (it->glyph_row->glyphs[area] \
26811 < it->glyph_row->glyphs[area + 1])) \
26813 it->w->ncols_scale_factor++; \
26814 it->f->fonts_changed = true; \
26818 /* Store one glyph for IT->char_to_display in IT->glyph_row.
26819 Called from x_produce_glyphs when IT->glyph_row is non-null. */
26821 static void
26822 append_glyph (struct it *it)
26824 struct glyph *glyph;
26825 enum glyph_row_area area = it->area;
26827 eassert (it->glyph_row);
26828 eassert (it->char_to_display != '\n' && it->char_to_display != '\t');
26830 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
26831 if (glyph < it->glyph_row->glyphs[area + 1])
26833 /* If the glyph row is reversed, we need to prepend the glyph
26834 rather than append it. */
26835 if (it->glyph_row->reversed_p && area == TEXT_AREA)
26837 struct glyph *g;
26839 /* Make room for the additional glyph. */
26840 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
26841 g[1] = *g;
26842 glyph = it->glyph_row->glyphs[area];
26844 glyph->charpos = CHARPOS (it->position);
26845 glyph->object = it->object;
26846 if (it->pixel_width > 0)
26848 eassert (it->pixel_width <= SHRT_MAX);
26849 glyph->pixel_width = it->pixel_width;
26850 glyph->padding_p = false;
26852 else
26854 /* Assure at least 1-pixel width. Otherwise, cursor can't
26855 be displayed correctly. */
26856 glyph->pixel_width = 1;
26857 glyph->padding_p = true;
26859 glyph->ascent = it->ascent;
26860 glyph->descent = it->descent;
26861 glyph->voffset = it->voffset;
26862 glyph->type = CHAR_GLYPH;
26863 glyph->avoid_cursor_p = it->avoid_cursor_p;
26864 glyph->multibyte_p = it->multibyte_p;
26865 if (it->glyph_row->reversed_p && area == TEXT_AREA)
26867 /* In R2L rows, the left and the right box edges need to be
26868 drawn in reverse direction. */
26869 glyph->right_box_line_p = it->start_of_box_run_p;
26870 glyph->left_box_line_p = it->end_of_box_run_p;
26872 else
26874 glyph->left_box_line_p = it->start_of_box_run_p;
26875 glyph->right_box_line_p = it->end_of_box_run_p;
26877 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
26878 || it->phys_descent > it->descent);
26879 glyph->glyph_not_available_p = it->glyph_not_available_p;
26880 glyph->face_id = it->face_id;
26881 glyph->u.ch = it->char_to_display;
26882 glyph->slice.img = null_glyph_slice;
26883 glyph->font_type = FONT_TYPE_UNKNOWN;
26884 if (it->bidi_p)
26886 glyph->resolved_level = it->bidi_it.resolved_level;
26887 eassert ((it->bidi_it.type & 7) == it->bidi_it.type);
26888 glyph->bidi_type = it->bidi_it.type;
26890 else
26892 glyph->resolved_level = 0;
26893 glyph->bidi_type = UNKNOWN_BT;
26895 ++it->glyph_row->used[area];
26897 else
26898 IT_EXPAND_MATRIX_WIDTH (it, area);
26901 /* Store one glyph for the composition IT->cmp_it.id in
26902 IT->glyph_row. Called from x_produce_glyphs when IT->glyph_row is
26903 non-null. */
26905 static void
26906 append_composite_glyph (struct it *it)
26908 struct glyph *glyph;
26909 enum glyph_row_area area = it->area;
26911 eassert (it->glyph_row);
26913 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
26914 if (glyph < it->glyph_row->glyphs[area + 1])
26916 /* If the glyph row is reversed, we need to prepend the glyph
26917 rather than append it. */
26918 if (it->glyph_row->reversed_p && it->area == TEXT_AREA)
26920 struct glyph *g;
26922 /* Make room for the new glyph. */
26923 for (g = glyph - 1; g >= it->glyph_row->glyphs[it->area]; g--)
26924 g[1] = *g;
26925 glyph = it->glyph_row->glyphs[it->area];
26927 glyph->charpos = it->cmp_it.charpos;
26928 glyph->object = it->object;
26929 eassert (it->pixel_width <= SHRT_MAX);
26930 glyph->pixel_width = it->pixel_width;
26931 glyph->ascent = it->ascent;
26932 glyph->descent = it->descent;
26933 glyph->voffset = it->voffset;
26934 glyph->type = COMPOSITE_GLYPH;
26935 if (it->cmp_it.ch < 0)
26937 glyph->u.cmp.automatic = false;
26938 glyph->u.cmp.id = it->cmp_it.id;
26939 glyph->slice.cmp.from = glyph->slice.cmp.to = 0;
26941 else
26943 glyph->u.cmp.automatic = true;
26944 glyph->u.cmp.id = it->cmp_it.id;
26945 glyph->slice.cmp.from = it->cmp_it.from;
26946 glyph->slice.cmp.to = it->cmp_it.to - 1;
26948 glyph->avoid_cursor_p = it->avoid_cursor_p;
26949 glyph->multibyte_p = it->multibyte_p;
26950 if (it->glyph_row->reversed_p && area == TEXT_AREA)
26952 /* In R2L rows, the left and the right box edges need to be
26953 drawn in reverse direction. */
26954 glyph->right_box_line_p = it->start_of_box_run_p;
26955 glyph->left_box_line_p = it->end_of_box_run_p;
26957 else
26959 glyph->left_box_line_p = it->start_of_box_run_p;
26960 glyph->right_box_line_p = it->end_of_box_run_p;
26962 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
26963 || it->phys_descent > it->descent);
26964 glyph->padding_p = false;
26965 glyph->glyph_not_available_p = false;
26966 glyph->face_id = it->face_id;
26967 glyph->font_type = FONT_TYPE_UNKNOWN;
26968 if (it->bidi_p)
26970 glyph->resolved_level = it->bidi_it.resolved_level;
26971 eassert ((it->bidi_it.type & 7) == it->bidi_it.type);
26972 glyph->bidi_type = it->bidi_it.type;
26974 ++it->glyph_row->used[area];
26976 else
26977 IT_EXPAND_MATRIX_WIDTH (it, area);
26981 /* Change IT->ascent and IT->height according to the setting of
26982 IT->voffset. */
26984 static void
26985 take_vertical_position_into_account (struct it *it)
26987 if (it->voffset)
26989 if (it->voffset < 0)
26990 /* Increase the ascent so that we can display the text higher
26991 in the line. */
26992 it->ascent -= it->voffset;
26993 else
26994 /* Increase the descent so that we can display the text lower
26995 in the line. */
26996 it->descent += it->voffset;
27001 /* Produce glyphs/get display metrics for the image IT is loaded with.
27002 See the description of struct display_iterator in dispextern.h for
27003 an overview of struct display_iterator. */
27005 static void
27006 produce_image_glyph (struct it *it)
27008 struct image *img;
27009 struct face *face;
27010 int glyph_ascent, crop;
27011 struct glyph_slice slice;
27013 eassert (it->what == IT_IMAGE);
27015 face = FACE_FROM_ID (it->f, it->face_id);
27016 /* Make sure X resources of the face is loaded. */
27017 prepare_face_for_display (it->f, face);
27019 if (it->image_id < 0)
27021 /* Fringe bitmap. */
27022 it->ascent = it->phys_ascent = 0;
27023 it->descent = it->phys_descent = 0;
27024 it->pixel_width = 0;
27025 it->nglyphs = 0;
27026 return;
27029 img = IMAGE_FROM_ID (it->f, it->image_id);
27030 /* Make sure X resources of the image is loaded. */
27031 prepare_image_for_display (it->f, img);
27033 slice.x = slice.y = 0;
27034 slice.width = img->width;
27035 slice.height = img->height;
27037 if (INTEGERP (it->slice.x))
27038 slice.x = XINT (it->slice.x);
27039 else if (FLOATP (it->slice.x))
27040 slice.x = XFLOAT_DATA (it->slice.x) * img->width;
27042 if (INTEGERP (it->slice.y))
27043 slice.y = XINT (it->slice.y);
27044 else if (FLOATP (it->slice.y))
27045 slice.y = XFLOAT_DATA (it->slice.y) * img->height;
27047 if (INTEGERP (it->slice.width))
27048 slice.width = XINT (it->slice.width);
27049 else if (FLOATP (it->slice.width))
27050 slice.width = XFLOAT_DATA (it->slice.width) * img->width;
27052 if (INTEGERP (it->slice.height))
27053 slice.height = XINT (it->slice.height);
27054 else if (FLOATP (it->slice.height))
27055 slice.height = XFLOAT_DATA (it->slice.height) * img->height;
27057 if (slice.x >= img->width)
27058 slice.x = img->width;
27059 if (slice.y >= img->height)
27060 slice.y = img->height;
27061 if (slice.x + slice.width >= img->width)
27062 slice.width = img->width - slice.x;
27063 if (slice.y + slice.height > img->height)
27064 slice.height = img->height - slice.y;
27066 if (slice.width == 0 || slice.height == 0)
27067 return;
27069 it->ascent = it->phys_ascent = glyph_ascent = image_ascent (img, face, &slice);
27071 it->descent = slice.height - glyph_ascent;
27072 if (slice.y == 0)
27073 it->descent += img->vmargin;
27074 if (slice.y + slice.height == img->height)
27075 it->descent += img->vmargin;
27076 it->phys_descent = it->descent;
27078 it->pixel_width = slice.width;
27079 if (slice.x == 0)
27080 it->pixel_width += img->hmargin;
27081 if (slice.x + slice.width == img->width)
27082 it->pixel_width += img->hmargin;
27084 /* It's quite possible for images to have an ascent greater than
27085 their height, so don't get confused in that case. */
27086 if (it->descent < 0)
27087 it->descent = 0;
27089 it->nglyphs = 1;
27091 if (face->box != FACE_NO_BOX)
27093 if (face->box_line_width > 0)
27095 if (slice.y == 0)
27096 it->ascent += face->box_line_width;
27097 if (slice.y + slice.height == img->height)
27098 it->descent += face->box_line_width;
27101 if (it->start_of_box_run_p && slice.x == 0)
27102 it->pixel_width += eabs (face->box_line_width);
27103 if (it->end_of_box_run_p && slice.x + slice.width == img->width)
27104 it->pixel_width += eabs (face->box_line_width);
27107 take_vertical_position_into_account (it);
27109 /* Automatically crop wide image glyphs at right edge so we can
27110 draw the cursor on same display row. */
27111 if ((crop = it->pixel_width - (it->last_visible_x - it->current_x), crop > 0)
27112 && (it->hpos == 0 || it->pixel_width > it->last_visible_x / 4))
27114 it->pixel_width -= crop;
27115 slice.width -= crop;
27118 if (it->glyph_row)
27120 struct glyph *glyph;
27121 enum glyph_row_area area = it->area;
27123 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
27124 if (it->glyph_row->reversed_p)
27126 struct glyph *g;
27128 /* Make room for the new glyph. */
27129 for (g = glyph - 1; g >= it->glyph_row->glyphs[it->area]; g--)
27130 g[1] = *g;
27131 glyph = it->glyph_row->glyphs[it->area];
27133 if (glyph < it->glyph_row->glyphs[area + 1])
27135 glyph->charpos = CHARPOS (it->position);
27136 glyph->object = it->object;
27137 glyph->pixel_width = clip_to_bounds (-1, it->pixel_width, SHRT_MAX);
27138 glyph->ascent = glyph_ascent;
27139 glyph->descent = it->descent;
27140 glyph->voffset = it->voffset;
27141 glyph->type = IMAGE_GLYPH;
27142 glyph->avoid_cursor_p = it->avoid_cursor_p;
27143 glyph->multibyte_p = it->multibyte_p;
27144 if (it->glyph_row->reversed_p && area == TEXT_AREA)
27146 /* In R2L rows, the left and the right box edges need to be
27147 drawn in reverse direction. */
27148 glyph->right_box_line_p = it->start_of_box_run_p;
27149 glyph->left_box_line_p = it->end_of_box_run_p;
27151 else
27153 glyph->left_box_line_p = it->start_of_box_run_p;
27154 glyph->right_box_line_p = it->end_of_box_run_p;
27156 glyph->overlaps_vertically_p = false;
27157 glyph->padding_p = false;
27158 glyph->glyph_not_available_p = false;
27159 glyph->face_id = it->face_id;
27160 glyph->u.img_id = img->id;
27161 glyph->slice.img = slice;
27162 glyph->font_type = FONT_TYPE_UNKNOWN;
27163 if (it->bidi_p)
27165 glyph->resolved_level = it->bidi_it.resolved_level;
27166 eassert ((it->bidi_it.type & 7) == it->bidi_it.type);
27167 glyph->bidi_type = it->bidi_it.type;
27169 ++it->glyph_row->used[area];
27171 else
27172 IT_EXPAND_MATRIX_WIDTH (it, area);
27176 static void
27177 produce_xwidget_glyph (struct it *it)
27179 #ifdef HAVE_XWIDGETS
27180 struct xwidget *xw;
27181 int glyph_ascent, crop;
27182 eassert (it->what == IT_XWIDGET);
27184 struct face *face = FACE_FROM_ID (it->f, it->face_id);
27185 /* Make sure X resources of the face is loaded. */
27186 prepare_face_for_display (it->f, face);
27188 xw = it->xwidget;
27189 it->ascent = it->phys_ascent = glyph_ascent = xw->height/2;
27190 it->descent = xw->height/2;
27191 it->phys_descent = it->descent;
27192 it->pixel_width = xw->width;
27193 /* It's quite possible for images to have an ascent greater than
27194 their height, so don't get confused in that case. */
27195 if (it->descent < 0)
27196 it->descent = 0;
27198 it->nglyphs = 1;
27200 if (face->box != FACE_NO_BOX)
27202 if (face->box_line_width > 0)
27204 it->ascent += face->box_line_width;
27205 it->descent += face->box_line_width;
27208 if (it->start_of_box_run_p)
27209 it->pixel_width += eabs (face->box_line_width);
27210 it->pixel_width += eabs (face->box_line_width);
27213 take_vertical_position_into_account (it);
27215 /* Automatically crop wide image glyphs at right edge so we can
27216 draw the cursor on same display row. */
27217 crop = it->pixel_width - (it->last_visible_x - it->current_x);
27218 if (crop > 0 && (it->hpos == 0 || it->pixel_width > it->last_visible_x / 4))
27219 it->pixel_width -= crop;
27221 if (it->glyph_row)
27223 enum glyph_row_area area = it->area;
27224 struct glyph *glyph
27225 = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
27227 if (it->glyph_row->reversed_p)
27229 struct glyph *g;
27231 /* Make room for the new glyph. */
27232 for (g = glyph - 1; g >= it->glyph_row->glyphs[it->area]; g--)
27233 g[1] = *g;
27234 glyph = it->glyph_row->glyphs[it->area];
27236 if (glyph < it->glyph_row->glyphs[area + 1])
27238 glyph->charpos = CHARPOS (it->position);
27239 glyph->object = it->object;
27240 glyph->pixel_width = clip_to_bounds (-1, it->pixel_width, SHRT_MAX);
27241 glyph->ascent = glyph_ascent;
27242 glyph->descent = it->descent;
27243 glyph->voffset = it->voffset;
27244 glyph->type = XWIDGET_GLYPH;
27245 glyph->avoid_cursor_p = it->avoid_cursor_p;
27246 glyph->multibyte_p = it->multibyte_p;
27247 if (it->glyph_row->reversed_p && area == TEXT_AREA)
27249 /* In R2L rows, the left and the right box edges need to be
27250 drawn in reverse direction. */
27251 glyph->right_box_line_p = it->start_of_box_run_p;
27252 glyph->left_box_line_p = it->end_of_box_run_p;
27254 else
27256 glyph->left_box_line_p = it->start_of_box_run_p;
27257 glyph->right_box_line_p = it->end_of_box_run_p;
27259 glyph->overlaps_vertically_p = 0;
27260 glyph->padding_p = 0;
27261 glyph->glyph_not_available_p = 0;
27262 glyph->face_id = it->face_id;
27263 glyph->u.xwidget = it->xwidget;
27264 glyph->font_type = FONT_TYPE_UNKNOWN;
27265 if (it->bidi_p)
27267 glyph->resolved_level = it->bidi_it.resolved_level;
27268 eassert ((it->bidi_it.type & 7) == it->bidi_it.type);
27269 glyph->bidi_type = it->bidi_it.type;
27271 ++it->glyph_row->used[area];
27273 else
27274 IT_EXPAND_MATRIX_WIDTH (it, area);
27276 #endif
27279 /* Append a stretch glyph to IT->glyph_row. OBJECT is the source
27280 of the glyph, WIDTH and HEIGHT are the width and height of the
27281 stretch. ASCENT is the ascent of the glyph (0 <= ASCENT <= HEIGHT). */
27283 static void
27284 append_stretch_glyph (struct it *it, Lisp_Object object,
27285 int width, int height, int ascent)
27287 struct glyph *glyph;
27288 enum glyph_row_area area = it->area;
27290 eassert (ascent >= 0 && ascent <= height);
27292 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
27293 if (glyph < it->glyph_row->glyphs[area + 1])
27295 /* If the glyph row is reversed, we need to prepend the glyph
27296 rather than append it. */
27297 if (it->glyph_row->reversed_p && area == TEXT_AREA)
27299 struct glyph *g;
27301 /* Make room for the additional glyph. */
27302 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
27303 g[1] = *g;
27304 glyph = it->glyph_row->glyphs[area];
27306 /* Decrease the width of the first glyph of the row that
27307 begins before first_visible_x (e.g., due to hscroll).
27308 This is so the overall width of the row becomes smaller
27309 by the scroll amount, and the stretch glyph appended by
27310 extend_face_to_end_of_line will be wider, to shift the
27311 row glyphs to the right. (In L2R rows, the corresponding
27312 left-shift effect is accomplished by setting row->x to a
27313 negative value, which won't work with R2L rows.)
27315 This must leave us with a positive value of WIDTH, since
27316 otherwise the call to move_it_in_display_line_to at the
27317 beginning of display_line would have got past the entire
27318 first glyph, and then it->current_x would have been
27319 greater or equal to it->first_visible_x. */
27320 if (it->current_x < it->first_visible_x)
27321 width -= it->first_visible_x - it->current_x;
27322 eassert (width > 0);
27324 glyph->charpos = CHARPOS (it->position);
27325 glyph->object = object;
27326 /* FIXME: It would be better to use TYPE_MAX here, but
27327 __typeof__ is not portable enough... */
27328 glyph->pixel_width = clip_to_bounds (-1, width, SHRT_MAX);
27329 glyph->ascent = ascent;
27330 glyph->descent = height - ascent;
27331 glyph->voffset = it->voffset;
27332 glyph->type = STRETCH_GLYPH;
27333 glyph->avoid_cursor_p = it->avoid_cursor_p;
27334 glyph->multibyte_p = it->multibyte_p;
27335 if (it->glyph_row->reversed_p && area == TEXT_AREA)
27337 /* In R2L rows, the left and the right box edges need to be
27338 drawn in reverse direction. */
27339 glyph->right_box_line_p = it->start_of_box_run_p;
27340 glyph->left_box_line_p = it->end_of_box_run_p;
27342 else
27344 glyph->left_box_line_p = it->start_of_box_run_p;
27345 glyph->right_box_line_p = it->end_of_box_run_p;
27347 glyph->overlaps_vertically_p = false;
27348 glyph->padding_p = false;
27349 glyph->glyph_not_available_p = false;
27350 glyph->face_id = it->face_id;
27351 glyph->u.stretch.ascent = ascent;
27352 glyph->u.stretch.height = height;
27353 glyph->slice.img = null_glyph_slice;
27354 glyph->font_type = FONT_TYPE_UNKNOWN;
27355 if (it->bidi_p)
27357 glyph->resolved_level = it->bidi_it.resolved_level;
27358 eassert ((it->bidi_it.type & 7) == it->bidi_it.type);
27359 glyph->bidi_type = it->bidi_it.type;
27361 else
27363 glyph->resolved_level = 0;
27364 glyph->bidi_type = UNKNOWN_BT;
27366 ++it->glyph_row->used[area];
27368 else
27369 IT_EXPAND_MATRIX_WIDTH (it, area);
27372 #endif /* HAVE_WINDOW_SYSTEM */
27374 /* Produce a stretch glyph for iterator IT. IT->object is the value
27375 of the glyph property displayed. The value must be a list
27376 `(space KEYWORD VALUE ...)' with the following KEYWORD/VALUE pairs
27377 being recognized:
27379 1. `:width WIDTH' specifies that the space should be WIDTH *
27380 canonical char width wide. WIDTH may be an integer or floating
27381 point number.
27383 2. `:relative-width FACTOR' specifies that the width of the stretch
27384 should be computed from the width of the first character having the
27385 `glyph' property, and should be FACTOR times that width.
27387 3. `:align-to HPOS' specifies that the space should be wide enough
27388 to reach HPOS, a value in canonical character units.
27390 Exactly one of the above pairs must be present.
27392 4. `:height HEIGHT' specifies that the height of the stretch produced
27393 should be HEIGHT, measured in canonical character units.
27395 5. `:relative-height FACTOR' specifies that the height of the
27396 stretch should be FACTOR times the height of the characters having
27397 the glyph property.
27399 Either none or exactly one of 4 or 5 must be present.
27401 6. `:ascent ASCENT' specifies that ASCENT percent of the height
27402 of the stretch should be used for the ascent of the stretch.
27403 ASCENT must be in the range 0 <= ASCENT <= 100. */
27405 void
27406 produce_stretch_glyph (struct it *it)
27408 /* (space :width WIDTH :height HEIGHT ...) */
27409 Lisp_Object prop, plist;
27410 int width = 0, height = 0, align_to = -1;
27411 bool zero_width_ok_p = false;
27412 double tem;
27413 struct font *font = NULL;
27415 #ifdef HAVE_WINDOW_SYSTEM
27416 int ascent = 0;
27417 bool zero_height_ok_p = false;
27419 if (FRAME_WINDOW_P (it->f))
27421 struct face *face = FACE_FROM_ID (it->f, it->face_id);
27422 font = face->font ? face->font : FRAME_FONT (it->f);
27423 prepare_face_for_display (it->f, face);
27425 #endif
27427 /* List should start with `space'. */
27428 eassert (CONSP (it->object) && EQ (XCAR (it->object), Qspace));
27429 plist = XCDR (it->object);
27431 /* Compute the width of the stretch. */
27432 if ((prop = Fplist_get (plist, QCwidth), !NILP (prop))
27433 && calc_pixel_width_or_height (&tem, it, prop, font, true, 0))
27435 /* Absolute width `:width WIDTH' specified and valid. */
27436 zero_width_ok_p = true;
27437 width = (int)tem;
27439 else if (prop = Fplist_get (plist, QCrelative_width), NUMVAL (prop) > 0)
27441 /* Relative width `:relative-width FACTOR' specified and valid.
27442 Compute the width of the characters having the `glyph'
27443 property. */
27444 struct it it2;
27445 unsigned char *p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
27447 it2 = *it;
27448 if (it->multibyte_p)
27449 it2.c = it2.char_to_display = STRING_CHAR_AND_LENGTH (p, it2.len);
27450 else
27452 it2.c = it2.char_to_display = *p, it2.len = 1;
27453 if (! ASCII_CHAR_P (it2.c))
27454 it2.char_to_display = BYTE8_TO_CHAR (it2.c);
27457 it2.glyph_row = NULL;
27458 it2.what = IT_CHARACTER;
27459 PRODUCE_GLYPHS (&it2);
27460 width = NUMVAL (prop) * it2.pixel_width;
27462 else if ((prop = Fplist_get (plist, QCalign_to), !NILP (prop))
27463 && calc_pixel_width_or_height (&tem, it, prop, font, true,
27464 &align_to))
27466 if (it->glyph_row == NULL || !it->glyph_row->mode_line_p)
27467 align_to = (align_to < 0
27469 : align_to - window_box_left_offset (it->w, TEXT_AREA));
27470 else if (align_to < 0)
27471 align_to = window_box_left_offset (it->w, TEXT_AREA);
27472 width = max (0, (int)tem + align_to - it->current_x);
27473 zero_width_ok_p = true;
27475 else
27476 /* Nothing specified -> width defaults to canonical char width. */
27477 width = FRAME_COLUMN_WIDTH (it->f);
27479 if (width <= 0 && (width < 0 || !zero_width_ok_p))
27480 width = 1;
27482 #ifdef HAVE_WINDOW_SYSTEM
27483 /* Compute height. */
27484 if (FRAME_WINDOW_P (it->f))
27486 int default_height = normal_char_height (font, ' ');
27488 if ((prop = Fplist_get (plist, QCheight), !NILP (prop))
27489 && calc_pixel_width_or_height (&tem, it, prop, font, false, 0))
27491 height = (int)tem;
27492 zero_height_ok_p = true;
27494 else if (prop = Fplist_get (plist, QCrelative_height),
27495 NUMVAL (prop) > 0)
27496 height = default_height * NUMVAL (prop);
27497 else
27498 height = default_height;
27500 if (height <= 0 && (height < 0 || !zero_height_ok_p))
27501 height = 1;
27503 /* Compute percentage of height used for ascent. If
27504 `:ascent ASCENT' is present and valid, use that. Otherwise,
27505 derive the ascent from the font in use. */
27506 if (prop = Fplist_get (plist, QCascent),
27507 NUMVAL (prop) > 0 && NUMVAL (prop) <= 100)
27508 ascent = height * NUMVAL (prop) / 100.0;
27509 else if (!NILP (prop)
27510 && calc_pixel_width_or_height (&tem, it, prop, font, false, 0))
27511 ascent = min (max (0, (int)tem), height);
27512 else
27513 ascent = (height * FONT_BASE (font)) / FONT_HEIGHT (font);
27515 else
27516 #endif /* HAVE_WINDOW_SYSTEM */
27517 height = 1;
27519 if (width > 0 && it->line_wrap != TRUNCATE
27520 && it->current_x + width > it->last_visible_x)
27522 width = it->last_visible_x - it->current_x;
27523 #ifdef HAVE_WINDOW_SYSTEM
27524 /* Subtract one more pixel from the stretch width, but only on
27525 GUI frames, since on a TTY each glyph is one "pixel" wide. */
27526 width -= FRAME_WINDOW_P (it->f);
27527 #endif
27530 if (width > 0 && height > 0 && it->glyph_row)
27532 Lisp_Object o_object = it->object;
27533 Lisp_Object object = it->stack[it->sp - 1].string;
27534 int n = width;
27536 if (!STRINGP (object))
27537 object = it->w->contents;
27538 #ifdef HAVE_WINDOW_SYSTEM
27539 if (FRAME_WINDOW_P (it->f))
27540 append_stretch_glyph (it, object, width, height, ascent);
27541 else
27542 #endif
27544 it->object = object;
27545 it->char_to_display = ' ';
27546 it->pixel_width = it->len = 1;
27547 while (n--)
27548 tty_append_glyph (it);
27549 it->object = o_object;
27553 it->pixel_width = width;
27554 #ifdef HAVE_WINDOW_SYSTEM
27555 if (FRAME_WINDOW_P (it->f))
27557 it->ascent = it->phys_ascent = ascent;
27558 it->descent = it->phys_descent = height - it->ascent;
27559 it->nglyphs = width > 0 && height > 0;
27560 take_vertical_position_into_account (it);
27562 else
27563 #endif
27564 it->nglyphs = width;
27567 /* Get information about special display element WHAT in an
27568 environment described by IT. WHAT is one of IT_TRUNCATION or
27569 IT_CONTINUATION. Maybe produce glyphs for WHAT if IT has a
27570 non-null glyph_row member. This function ensures that fields like
27571 face_id, c, len of IT are left untouched. */
27573 static void
27574 produce_special_glyphs (struct it *it, enum display_element_type what)
27576 struct it temp_it;
27577 Lisp_Object gc;
27578 GLYPH glyph;
27580 temp_it = *it;
27581 temp_it.object = Qnil;
27582 memset (&temp_it.current, 0, sizeof temp_it.current);
27584 if (what == IT_CONTINUATION)
27586 /* Continuation glyph. For R2L lines, we mirror it by hand. */
27587 if (it->bidi_it.paragraph_dir == R2L)
27588 SET_GLYPH_FROM_CHAR (glyph, '/');
27589 else
27590 SET_GLYPH_FROM_CHAR (glyph, '\\');
27591 if (it->dp
27592 && (gc = DISP_CONTINUE_GLYPH (it->dp), GLYPH_CODE_P (gc)))
27594 /* FIXME: Should we mirror GC for R2L lines? */
27595 SET_GLYPH_FROM_GLYPH_CODE (glyph, gc);
27596 spec_glyph_lookup_face (XWINDOW (it->window), &glyph);
27599 else if (what == IT_TRUNCATION)
27601 /* Truncation glyph. */
27602 SET_GLYPH_FROM_CHAR (glyph, '$');
27603 if (it->dp
27604 && (gc = DISP_TRUNC_GLYPH (it->dp), GLYPH_CODE_P (gc)))
27606 /* FIXME: Should we mirror GC for R2L lines? */
27607 SET_GLYPH_FROM_GLYPH_CODE (glyph, gc);
27608 spec_glyph_lookup_face (XWINDOW (it->window), &glyph);
27611 else
27612 emacs_abort ();
27614 #ifdef HAVE_WINDOW_SYSTEM
27615 /* On a GUI frame, when the right fringe (left fringe for R2L rows)
27616 is turned off, we precede the truncation/continuation glyphs by a
27617 stretch glyph whose width is computed such that these special
27618 glyphs are aligned at the window margin, even when very different
27619 fonts are used in different glyph rows. */
27620 if (FRAME_WINDOW_P (temp_it.f)
27621 /* init_iterator calls this with it->glyph_row == NULL, and it
27622 wants only the pixel width of the truncation/continuation
27623 glyphs. */
27624 && temp_it.glyph_row
27625 /* insert_left_trunc_glyphs calls us at the beginning of the
27626 row, and it has its own calculation of the stretch glyph
27627 width. */
27628 && temp_it.glyph_row->used[TEXT_AREA] > 0
27629 && (temp_it.glyph_row->reversed_p
27630 ? WINDOW_LEFT_FRINGE_WIDTH (temp_it.w)
27631 : WINDOW_RIGHT_FRINGE_WIDTH (temp_it.w)) == 0)
27633 int stretch_width = temp_it.last_visible_x - temp_it.current_x;
27635 if (stretch_width > 0)
27637 struct face *face = FACE_FROM_ID (temp_it.f, temp_it.face_id);
27638 struct font *font =
27639 face->font ? face->font : FRAME_FONT (temp_it.f);
27640 int stretch_ascent =
27641 (((temp_it.ascent + temp_it.descent)
27642 * FONT_BASE (font)) / FONT_HEIGHT (font));
27644 append_stretch_glyph (&temp_it, Qnil, stretch_width,
27645 temp_it.ascent + temp_it.descent,
27646 stretch_ascent);
27649 #endif
27651 temp_it.dp = NULL;
27652 temp_it.what = IT_CHARACTER;
27653 temp_it.c = temp_it.char_to_display = GLYPH_CHAR (glyph);
27654 temp_it.face_id = GLYPH_FACE (glyph);
27655 temp_it.len = CHAR_BYTES (temp_it.c);
27657 PRODUCE_GLYPHS (&temp_it);
27658 it->pixel_width = temp_it.pixel_width;
27659 it->nglyphs = temp_it.nglyphs;
27662 #ifdef HAVE_WINDOW_SYSTEM
27664 /* Calculate line-height and line-spacing properties.
27665 An integer value specifies explicit pixel value.
27666 A float value specifies relative value to current face height.
27667 A cons (float . face-name) specifies relative value to
27668 height of specified face font.
27670 Returns height in pixels, or nil. */
27672 static Lisp_Object
27673 calc_line_height_property (struct it *it, Lisp_Object val, struct font *font,
27674 int boff, bool override)
27676 Lisp_Object face_name = Qnil;
27677 int ascent, descent, height;
27679 if (NILP (val) || INTEGERP (val) || (override && EQ (val, Qt)))
27680 return val;
27682 if (CONSP (val))
27684 face_name = XCAR (val);
27685 val = XCDR (val);
27686 if (!NUMBERP (val))
27687 val = make_number (1);
27688 if (NILP (face_name))
27690 height = it->ascent + it->descent;
27691 goto scale;
27695 if (NILP (face_name))
27697 font = FRAME_FONT (it->f);
27698 boff = FRAME_BASELINE_OFFSET (it->f);
27700 else if (EQ (face_name, Qt))
27702 override = false;
27704 else
27706 int face_id;
27707 struct face *face;
27709 face_id = lookup_named_face (it->f, face_name, false);
27710 face = FACE_FROM_ID_OR_NULL (it->f, face_id);
27711 if (face == NULL || ((font = face->font) == NULL))
27712 return make_number (-1);
27713 boff = font->baseline_offset;
27714 if (font->vertical_centering)
27715 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
27718 normal_char_ascent_descent (font, -1, &ascent, &descent);
27720 if (override)
27722 it->override_ascent = ascent;
27723 it->override_descent = descent;
27724 it->override_boff = boff;
27727 height = ascent + descent;
27729 scale:
27730 if (FLOATP (val))
27731 height = (int)(XFLOAT_DATA (val) * height);
27732 else if (INTEGERP (val))
27733 height *= XINT (val);
27735 return make_number (height);
27739 /* Append a glyph for a glyphless character to IT->glyph_row. FACE_ID
27740 is a face ID to be used for the glyph. FOR_NO_FONT is true if
27741 and only if this is for a character for which no font was found.
27743 If the display method (it->glyphless_method) is
27744 GLYPHLESS_DISPLAY_ACRONYM or GLYPHLESS_DISPLAY_HEX_CODE, LEN is a
27745 length of the acronym or the hexadecimal string, UPPER_XOFF and
27746 UPPER_YOFF are pixel offsets for the upper part of the string,
27747 LOWER_XOFF and LOWER_YOFF are for the lower part.
27749 For the other display methods, LEN through LOWER_YOFF are zero. */
27751 static void
27752 append_glyphless_glyph (struct it *it, int face_id, bool for_no_font, int len,
27753 short upper_xoff, short upper_yoff,
27754 short lower_xoff, short lower_yoff)
27756 struct glyph *glyph;
27757 enum glyph_row_area area = it->area;
27759 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
27760 if (glyph < it->glyph_row->glyphs[area + 1])
27762 /* If the glyph row is reversed, we need to prepend the glyph
27763 rather than append it. */
27764 if (it->glyph_row->reversed_p && area == TEXT_AREA)
27766 struct glyph *g;
27768 /* Make room for the additional glyph. */
27769 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
27770 g[1] = *g;
27771 glyph = it->glyph_row->glyphs[area];
27773 glyph->charpos = CHARPOS (it->position);
27774 glyph->object = it->object;
27775 eassert (it->pixel_width <= SHRT_MAX);
27776 glyph->pixel_width = it->pixel_width;
27777 glyph->ascent = it->ascent;
27778 glyph->descent = it->descent;
27779 glyph->voffset = it->voffset;
27780 glyph->type = GLYPHLESS_GLYPH;
27781 glyph->u.glyphless.method = it->glyphless_method;
27782 glyph->u.glyphless.for_no_font = for_no_font;
27783 glyph->u.glyphless.len = len;
27784 glyph->u.glyphless.ch = it->c;
27785 glyph->slice.glyphless.upper_xoff = upper_xoff;
27786 glyph->slice.glyphless.upper_yoff = upper_yoff;
27787 glyph->slice.glyphless.lower_xoff = lower_xoff;
27788 glyph->slice.glyphless.lower_yoff = lower_yoff;
27789 glyph->avoid_cursor_p = it->avoid_cursor_p;
27790 glyph->multibyte_p = it->multibyte_p;
27791 if (it->glyph_row->reversed_p && area == TEXT_AREA)
27793 /* In R2L rows, the left and the right box edges need to be
27794 drawn in reverse direction. */
27795 glyph->right_box_line_p = it->start_of_box_run_p;
27796 glyph->left_box_line_p = it->end_of_box_run_p;
27798 else
27800 glyph->left_box_line_p = it->start_of_box_run_p;
27801 glyph->right_box_line_p = it->end_of_box_run_p;
27803 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
27804 || it->phys_descent > it->descent);
27805 glyph->padding_p = false;
27806 glyph->glyph_not_available_p = false;
27807 glyph->face_id = face_id;
27808 glyph->font_type = FONT_TYPE_UNKNOWN;
27809 if (it->bidi_p)
27811 glyph->resolved_level = it->bidi_it.resolved_level;
27812 eassert ((it->bidi_it.type & 7) == it->bidi_it.type);
27813 glyph->bidi_type = it->bidi_it.type;
27815 ++it->glyph_row->used[area];
27817 else
27818 IT_EXPAND_MATRIX_WIDTH (it, area);
27822 /* Produce a glyph for a glyphless character for iterator IT.
27823 IT->glyphless_method specifies which method to use for displaying
27824 the character. See the description of enum
27825 glyphless_display_method in dispextern.h for the detail.
27827 FOR_NO_FONT is true if and only if this is for a character for
27828 which no font was found. ACRONYM, if non-nil, is an acronym string
27829 for the character. */
27831 static void
27832 produce_glyphless_glyph (struct it *it, bool for_no_font, Lisp_Object acronym)
27834 int face_id;
27835 struct face *face;
27836 struct font *font;
27837 int base_width, base_height, width, height;
27838 short upper_xoff, upper_yoff, lower_xoff, lower_yoff;
27839 int len;
27841 /* Get the metrics of the base font. We always refer to the current
27842 ASCII face. */
27843 face = FACE_FROM_ID (it->f, it->face_id)->ascii_face;
27844 font = face->font ? face->font : FRAME_FONT (it->f);
27845 normal_char_ascent_descent (font, -1, &it->ascent, &it->descent);
27846 it->ascent += font->baseline_offset;
27847 it->descent -= font->baseline_offset;
27848 base_height = it->ascent + it->descent;
27849 base_width = font->average_width;
27851 face_id = merge_glyphless_glyph_face (it);
27853 if (it->glyphless_method == GLYPHLESS_DISPLAY_THIN_SPACE)
27855 it->pixel_width = THIN_SPACE_WIDTH;
27856 len = 0;
27857 upper_xoff = upper_yoff = lower_xoff = lower_yoff = 0;
27859 else if (it->glyphless_method == GLYPHLESS_DISPLAY_EMPTY_BOX)
27861 width = CHARACTER_WIDTH (it->c);
27862 if (width == 0)
27863 width = 1;
27864 else if (width > 4)
27865 width = 4;
27866 it->pixel_width = base_width * width;
27867 len = 0;
27868 upper_xoff = upper_yoff = lower_xoff = lower_yoff = 0;
27870 else
27872 char buf[7];
27873 const char *str;
27874 unsigned int code[6];
27875 int upper_len;
27876 int ascent, descent;
27877 struct font_metrics metrics_upper, metrics_lower;
27879 face = FACE_FROM_ID (it->f, face_id);
27880 font = face->font ? face->font : FRAME_FONT (it->f);
27881 prepare_face_for_display (it->f, face);
27883 if (it->glyphless_method == GLYPHLESS_DISPLAY_ACRONYM)
27885 if (! STRINGP (acronym) && CHAR_TABLE_P (Vglyphless_char_display))
27886 acronym = CHAR_TABLE_REF (Vglyphless_char_display, it->c);
27887 if (CONSP (acronym))
27888 acronym = XCAR (acronym);
27889 str = STRINGP (acronym) ? SSDATA (acronym) : "";
27891 else
27893 eassert (it->glyphless_method == GLYPHLESS_DISPLAY_HEX_CODE);
27894 sprintf (buf, "%0*X", it->c < 0x10000 ? 4 : 6, it->c + 0u);
27895 str = buf;
27897 for (len = 0; str[len] && ASCII_CHAR_P (str[len]) && len < 6; len++)
27898 code[len] = font->driver->encode_char (font, str[len]);
27899 upper_len = (len + 1) / 2;
27900 font->driver->text_extents (font, code, upper_len,
27901 &metrics_upper);
27902 font->driver->text_extents (font, code + upper_len, len - upper_len,
27903 &metrics_lower);
27907 /* +4 is for vertical bars of a box plus 1-pixel spaces at both side. */
27908 width = max (metrics_upper.width, metrics_lower.width) + 4;
27909 upper_xoff = upper_yoff = 2; /* the typical case */
27910 if (base_width >= width)
27912 /* Align the upper to the left, the lower to the right. */
27913 it->pixel_width = base_width;
27914 lower_xoff = base_width - 2 - metrics_lower.width;
27916 else
27918 /* Center the shorter one. */
27919 it->pixel_width = width;
27920 if (metrics_upper.width >= metrics_lower.width)
27921 lower_xoff = (width - metrics_lower.width) / 2;
27922 else
27924 /* FIXME: This code doesn't look right. It formerly was
27925 missing the "lower_xoff = 0;", which couldn't have
27926 been right since it left lower_xoff uninitialized. */
27927 lower_xoff = 0;
27928 upper_xoff = (width - metrics_upper.width) / 2;
27932 /* +5 is for horizontal bars of a box plus 1-pixel spaces at
27933 top, bottom, and between upper and lower strings. */
27934 height = (metrics_upper.ascent + metrics_upper.descent
27935 + metrics_lower.ascent + metrics_lower.descent) + 5;
27936 /* Center vertically.
27937 H:base_height, D:base_descent
27938 h:height, ld:lower_descent, la:lower_ascent, ud:upper_descent
27940 ascent = - (D - H/2 - h/2 + 1); "+ 1" for rounding up
27941 descent = D - H/2 + h/2;
27942 lower_yoff = descent - 2 - ld;
27943 upper_yoff = lower_yoff - la - 1 - ud; */
27944 ascent = - (it->descent - (base_height + height + 1) / 2);
27945 descent = it->descent - (base_height - height) / 2;
27946 lower_yoff = descent - 2 - metrics_lower.descent;
27947 upper_yoff = (lower_yoff - metrics_lower.ascent - 1
27948 - metrics_upper.descent);
27949 /* Don't make the height shorter than the base height. */
27950 if (height > base_height)
27952 it->ascent = ascent;
27953 it->descent = descent;
27957 it->phys_ascent = it->ascent;
27958 it->phys_descent = it->descent;
27959 if (it->glyph_row)
27960 append_glyphless_glyph (it, face_id, for_no_font, len,
27961 upper_xoff, upper_yoff,
27962 lower_xoff, lower_yoff);
27963 it->nglyphs = 1;
27964 take_vertical_position_into_account (it);
27968 /* RIF:
27969 Produce glyphs/get display metrics for the display element IT is
27970 loaded with. See the description of struct it in dispextern.h
27971 for an overview of struct it. */
27973 void
27974 x_produce_glyphs (struct it *it)
27976 int extra_line_spacing = it->extra_line_spacing;
27978 it->glyph_not_available_p = false;
27980 if (it->what == IT_CHARACTER)
27982 XChar2b char2b;
27983 struct face *face = FACE_FROM_ID (it->f, it->face_id);
27984 struct font *font = face->font;
27985 struct font_metrics *pcm = NULL;
27986 int boff; /* Baseline offset. */
27988 if (font == NULL)
27990 /* When no suitable font is found, display this character by
27991 the method specified in the first extra slot of
27992 Vglyphless_char_display. */
27993 Lisp_Object acronym = lookup_glyphless_char_display (-1, it);
27995 eassert (it->what == IT_GLYPHLESS);
27996 produce_glyphless_glyph (it, true,
27997 STRINGP (acronym) ? acronym : Qnil);
27998 goto done;
28001 boff = font->baseline_offset;
28002 if (font->vertical_centering)
28003 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
28005 if (it->char_to_display != '\n' && it->char_to_display != '\t')
28007 it->nglyphs = 1;
28009 if (it->override_ascent >= 0)
28011 it->ascent = it->override_ascent;
28012 it->descent = it->override_descent;
28013 boff = it->override_boff;
28015 else
28017 it->ascent = FONT_BASE (font) + boff;
28018 it->descent = FONT_DESCENT (font) - boff;
28021 if (get_char_glyph_code (it->char_to_display, font, &char2b))
28023 pcm = get_per_char_metric (font, &char2b);
28024 if (pcm->width == 0
28025 && pcm->rbearing == 0 && pcm->lbearing == 0)
28026 pcm = NULL;
28029 if (pcm)
28031 it->phys_ascent = pcm->ascent + boff;
28032 it->phys_descent = pcm->descent - boff;
28033 it->pixel_width = pcm->width;
28034 /* Don't use font-global values for ascent and descent
28035 if they result in an exceedingly large line height. */
28036 if (it->override_ascent < 0)
28038 if (FONT_TOO_HIGH (font))
28040 it->ascent = it->phys_ascent;
28041 it->descent = it->phys_descent;
28042 /* These limitations are enforced by an
28043 assertion near the end of this function. */
28044 if (it->ascent < 0)
28045 it->ascent = 0;
28046 if (it->descent < 0)
28047 it->descent = 0;
28051 else
28053 it->glyph_not_available_p = true;
28054 it->phys_ascent = it->ascent;
28055 it->phys_descent = it->descent;
28056 it->pixel_width = font->space_width;
28059 if (it->constrain_row_ascent_descent_p)
28061 if (it->descent > it->max_descent)
28063 it->ascent += it->descent - it->max_descent;
28064 it->descent = it->max_descent;
28066 if (it->ascent > it->max_ascent)
28068 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
28069 it->ascent = it->max_ascent;
28071 it->phys_ascent = min (it->phys_ascent, it->ascent);
28072 it->phys_descent = min (it->phys_descent, it->descent);
28073 extra_line_spacing = 0;
28076 /* If this is a space inside a region of text with
28077 `space-width' property, change its width. */
28078 bool stretched_p
28079 = it->char_to_display == ' ' && !NILP (it->space_width);
28080 if (stretched_p)
28081 it->pixel_width *= XFLOATINT (it->space_width);
28083 /* If face has a box, add the box thickness to the character
28084 height. If character has a box line to the left and/or
28085 right, add the box line width to the character's width. */
28086 if (face->box != FACE_NO_BOX)
28088 int thick = face->box_line_width;
28090 if (thick > 0)
28092 it->ascent += thick;
28093 it->descent += thick;
28095 else
28096 thick = -thick;
28098 if (it->start_of_box_run_p)
28099 it->pixel_width += thick;
28100 if (it->end_of_box_run_p)
28101 it->pixel_width += thick;
28104 /* If face has an overline, add the height of the overline
28105 (1 pixel) and a 1 pixel margin to the character height. */
28106 if (face->overline_p)
28107 it->ascent += overline_margin;
28109 if (it->constrain_row_ascent_descent_p)
28111 if (it->ascent > it->max_ascent)
28112 it->ascent = it->max_ascent;
28113 if (it->descent > it->max_descent)
28114 it->descent = it->max_descent;
28117 take_vertical_position_into_account (it);
28119 /* If we have to actually produce glyphs, do it. */
28120 if (it->glyph_row)
28122 if (stretched_p)
28124 /* Translate a space with a `space-width' property
28125 into a stretch glyph. */
28126 int ascent = (((it->ascent + it->descent) * FONT_BASE (font))
28127 / FONT_HEIGHT (font));
28128 append_stretch_glyph (it, it->object, it->pixel_width,
28129 it->ascent + it->descent, ascent);
28131 else
28132 append_glyph (it);
28134 /* If characters with lbearing or rbearing are displayed
28135 in this line, record that fact in a flag of the
28136 glyph row. This is used to optimize X output code. */
28137 if (pcm && (pcm->lbearing < 0 || pcm->rbearing > pcm->width))
28138 it->glyph_row->contains_overlapping_glyphs_p = true;
28140 if (! stretched_p && it->pixel_width == 0)
28141 /* We assure that all visible glyphs have at least 1-pixel
28142 width. */
28143 it->pixel_width = 1;
28145 else if (it->char_to_display == '\n')
28147 /* A newline has no width, but we need the height of the
28148 line. But if previous part of the line sets a height,
28149 don't increase that height. */
28151 Lisp_Object height;
28152 Lisp_Object total_height = Qnil;
28154 it->override_ascent = -1;
28155 it->pixel_width = 0;
28156 it->nglyphs = 0;
28158 height = get_it_property (it, Qline_height);
28159 /* Split (line-height total-height) list. */
28160 if (CONSP (height)
28161 && CONSP (XCDR (height))
28162 && NILP (XCDR (XCDR (height))))
28164 total_height = XCAR (XCDR (height));
28165 height = XCAR (height);
28167 height = calc_line_height_property (it, height, font, boff, true);
28169 if (it->override_ascent >= 0)
28171 it->ascent = it->override_ascent;
28172 it->descent = it->override_descent;
28173 boff = it->override_boff;
28175 else
28177 if (FONT_TOO_HIGH (font))
28179 it->ascent = font->pixel_size + boff - 1;
28180 it->descent = -boff + 1;
28181 if (it->descent < 0)
28182 it->descent = 0;
28184 else
28186 it->ascent = FONT_BASE (font) + boff;
28187 it->descent = FONT_DESCENT (font) - boff;
28191 if (EQ (height, Qt))
28193 if (it->descent > it->max_descent)
28195 it->ascent += it->descent - it->max_descent;
28196 it->descent = it->max_descent;
28198 if (it->ascent > it->max_ascent)
28200 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
28201 it->ascent = it->max_ascent;
28203 it->phys_ascent = min (it->phys_ascent, it->ascent);
28204 it->phys_descent = min (it->phys_descent, it->descent);
28205 it->constrain_row_ascent_descent_p = true;
28206 extra_line_spacing = 0;
28208 else
28210 Lisp_Object spacing;
28212 it->phys_ascent = it->ascent;
28213 it->phys_descent = it->descent;
28215 if ((it->max_ascent > 0 || it->max_descent > 0)
28216 && face->box != FACE_NO_BOX
28217 && face->box_line_width > 0)
28219 it->ascent += face->box_line_width;
28220 it->descent += face->box_line_width;
28222 if (!NILP (height)
28223 && XINT (height) > it->ascent + it->descent)
28224 it->ascent = XINT (height) - it->descent;
28226 if (!NILP (total_height))
28227 spacing = calc_line_height_property (it, total_height, font,
28228 boff, false);
28229 else
28231 spacing = get_it_property (it, Qline_spacing);
28232 spacing = calc_line_height_property (it, spacing, font,
28233 boff, false);
28235 if (INTEGERP (spacing))
28237 extra_line_spacing = XINT (spacing);
28238 if (!NILP (total_height))
28239 extra_line_spacing -= (it->phys_ascent + it->phys_descent);
28243 else /* i.e. (it->char_to_display == '\t') */
28245 if (font->space_width > 0)
28247 int tab_width = it->tab_width * font->space_width;
28248 int x = it->current_x + it->continuation_lines_width;
28249 int x0 = x;
28250 /* Adjust for line numbers, if needed. */
28251 if (!NILP (Vdisplay_line_numbers) && x0 >= it->lnum_pixel_width)
28252 x -= it->lnum_pixel_width;
28253 int next_tab_x = ((1 + x + tab_width - 1) / tab_width) * tab_width;
28255 /* If the distance from the current position to the next tab
28256 stop is less than a space character width, use the
28257 tab stop after that. */
28258 if (next_tab_x - x < font->space_width)
28259 next_tab_x += tab_width;
28260 if (!NILP (Vdisplay_line_numbers) && x0 >= it->lnum_pixel_width)
28261 next_tab_x += (it->lnum_pixel_width
28262 - ((it->w->hscroll * font->space_width)
28263 % tab_width));
28265 it->pixel_width = next_tab_x - x0;
28266 it->nglyphs = 1;
28267 if (FONT_TOO_HIGH (font))
28269 if (get_char_glyph_code (' ', font, &char2b))
28271 pcm = get_per_char_metric (font, &char2b);
28272 if (pcm->width == 0
28273 && pcm->rbearing == 0 && pcm->lbearing == 0)
28274 pcm = NULL;
28277 if (pcm)
28279 it->ascent = pcm->ascent + boff;
28280 it->descent = pcm->descent - boff;
28282 else
28284 it->ascent = font->pixel_size + boff - 1;
28285 it->descent = -boff + 1;
28287 if (it->ascent < 0)
28288 it->ascent = 0;
28289 if (it->descent < 0)
28290 it->descent = 0;
28292 else
28294 it->ascent = FONT_BASE (font) + boff;
28295 it->descent = FONT_DESCENT (font) - boff;
28297 it->phys_ascent = it->ascent;
28298 it->phys_descent = it->descent;
28300 if (it->glyph_row)
28302 append_stretch_glyph (it, it->object, it->pixel_width,
28303 it->ascent + it->descent, it->ascent);
28306 else
28308 it->pixel_width = 0;
28309 it->nglyphs = 1;
28313 if (FONT_TOO_HIGH (font))
28315 int font_ascent, font_descent;
28317 /* For very large fonts, where we ignore the declared font
28318 dimensions, and go by per-character metrics instead,
28319 don't let the row ascent and descent values (and the row
28320 height computed from them) be smaller than the "normal"
28321 character metrics. This avoids unpleasant effects
28322 whereby lines on display would change their height
28323 depending on which characters are shown. */
28324 normal_char_ascent_descent (font, -1, &font_ascent, &font_descent);
28325 it->max_ascent = max (it->max_ascent, font_ascent);
28326 it->max_descent = max (it->max_descent, font_descent);
28329 else if (it->what == IT_COMPOSITION && it->cmp_it.ch < 0)
28331 /* A static composition.
28333 Note: A composition is represented as one glyph in the
28334 glyph matrix. There are no padding glyphs.
28336 Important note: pixel_width, ascent, and descent are the
28337 values of what is drawn by draw_glyphs (i.e. the values of
28338 the overall glyphs composed). */
28339 struct face *face = FACE_FROM_ID (it->f, it->face_id);
28340 int boff; /* baseline offset */
28341 struct composition *cmp = composition_table[it->cmp_it.id];
28342 int glyph_len = cmp->glyph_len;
28343 struct font *font = face->font;
28345 it->nglyphs = 1;
28347 /* If we have not yet calculated pixel size data of glyphs of
28348 the composition for the current face font, calculate them
28349 now. Theoretically, we have to check all fonts for the
28350 glyphs, but that requires much time and memory space. So,
28351 here we check only the font of the first glyph. This may
28352 lead to incorrect display, but it's very rare, and C-l
28353 (recenter-top-bottom) can correct the display anyway. */
28354 if (! cmp->font || cmp->font != font)
28356 /* Ascent and descent of the font of the first character
28357 of this composition (adjusted by baseline offset).
28358 Ascent and descent of overall glyphs should not be less
28359 than these, respectively. */
28360 int font_ascent, font_descent, font_height;
28361 /* Bounding box of the overall glyphs. */
28362 int leftmost, rightmost, lowest, highest;
28363 int lbearing, rbearing;
28364 int i, width, ascent, descent;
28365 int c;
28366 XChar2b char2b;
28367 struct font_metrics *pcm;
28368 ptrdiff_t pos;
28370 eassume (0 < glyph_len); /* See Bug#8512. */
28372 c = COMPOSITION_GLYPH (cmp, glyph_len - 1);
28373 while (c == '\t' && 0 < --glyph_len);
28375 bool right_padded = glyph_len < cmp->glyph_len;
28376 for (i = 0; i < glyph_len; i++)
28378 c = COMPOSITION_GLYPH (cmp, i);
28379 if (c != '\t')
28380 break;
28381 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
28383 bool left_padded = i > 0;
28385 pos = (STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
28386 : IT_CHARPOS (*it));
28387 /* If no suitable font is found, use the default font. */
28388 bool font_not_found_p = font == NULL;
28389 if (font_not_found_p)
28391 face = face->ascii_face;
28392 font = face->font;
28394 boff = font->baseline_offset;
28395 if (font->vertical_centering)
28396 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
28397 normal_char_ascent_descent (font, -1, &font_ascent, &font_descent);
28398 font_ascent += boff;
28399 font_descent -= boff;
28400 font_height = font_ascent + font_descent;
28402 cmp->font = font;
28404 pcm = NULL;
28405 if (! font_not_found_p)
28407 get_char_face_and_encoding (it->f, c, it->face_id,
28408 &char2b, false);
28409 pcm = get_per_char_metric (font, &char2b);
28412 /* Initialize the bounding box. */
28413 if (pcm)
28415 width = cmp->glyph_len > 0 ? pcm->width : 0;
28416 ascent = pcm->ascent;
28417 descent = pcm->descent;
28418 lbearing = pcm->lbearing;
28419 rbearing = pcm->rbearing;
28421 else
28423 width = cmp->glyph_len > 0 ? font->space_width : 0;
28424 ascent = FONT_BASE (font);
28425 descent = FONT_DESCENT (font);
28426 lbearing = 0;
28427 rbearing = width;
28430 rightmost = width;
28431 leftmost = 0;
28432 lowest = - descent + boff;
28433 highest = ascent + boff;
28435 if (! font_not_found_p
28436 && font->default_ascent
28437 && CHAR_TABLE_P (Vuse_default_ascent)
28438 && !NILP (Faref (Vuse_default_ascent,
28439 make_number (it->char_to_display))))
28440 highest = font->default_ascent + boff;
28442 /* Draw the first glyph at the normal position. It may be
28443 shifted to right later if some other glyphs are drawn
28444 at the left. */
28445 cmp->offsets[i * 2] = 0;
28446 cmp->offsets[i * 2 + 1] = boff;
28447 cmp->lbearing = lbearing;
28448 cmp->rbearing = rbearing;
28450 /* Set cmp->offsets for the remaining glyphs. */
28451 for (i++; i < glyph_len; i++)
28453 int left, right, btm, top;
28454 int ch = COMPOSITION_GLYPH (cmp, i);
28455 int face_id;
28456 struct face *this_face;
28458 if (ch == '\t')
28459 ch = ' ';
28460 face_id = FACE_FOR_CHAR (it->f, face, ch, pos, it->string);
28461 this_face = FACE_FROM_ID (it->f, face_id);
28462 font = this_face->font;
28464 if (font == NULL)
28465 pcm = NULL;
28466 else
28468 get_char_face_and_encoding (it->f, ch, face_id,
28469 &char2b, false);
28470 pcm = get_per_char_metric (font, &char2b);
28472 if (! pcm)
28473 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
28474 else
28476 width = pcm->width;
28477 ascent = pcm->ascent;
28478 descent = pcm->descent;
28479 lbearing = pcm->lbearing;
28480 rbearing = pcm->rbearing;
28481 if (cmp->method != COMPOSITION_WITH_RULE_ALTCHARS)
28483 /* Relative composition with or without
28484 alternate chars. */
28485 left = (leftmost + rightmost - width) / 2;
28486 btm = - descent + boff;
28487 if (font->relative_compose
28488 && (! CHAR_TABLE_P (Vignore_relative_composition)
28489 || NILP (Faref (Vignore_relative_composition,
28490 make_number (ch)))))
28493 if (- descent >= font->relative_compose)
28494 /* One extra pixel between two glyphs. */
28495 btm = highest + 1;
28496 else if (ascent <= 0)
28497 /* One extra pixel between two glyphs. */
28498 btm = lowest - 1 - ascent - descent;
28501 else
28503 /* A composition rule is specified by an integer
28504 value that encodes global and new reference
28505 points (GREF and NREF). GREF and NREF are
28506 specified by numbers as below:
28508 0---1---2 -- ascent
28512 9--10--11 -- center
28514 ---3---4---5--- baseline
28516 6---7---8 -- descent
28518 int rule = COMPOSITION_RULE (cmp, i);
28519 int gref, nref, grefx, grefy, nrefx, nrefy, xoff, yoff;
28521 COMPOSITION_DECODE_RULE (rule, gref, nref, xoff, yoff);
28522 grefx = gref % 3, nrefx = nref % 3;
28523 grefy = gref / 3, nrefy = nref / 3;
28524 if (xoff)
28525 xoff = font_height * (xoff - 128) / 256;
28526 if (yoff)
28527 yoff = font_height * (yoff - 128) / 256;
28529 left = (leftmost
28530 + grefx * (rightmost - leftmost) / 2
28531 - nrefx * width / 2
28532 + xoff);
28534 btm = ((grefy == 0 ? highest
28535 : grefy == 1 ? 0
28536 : grefy == 2 ? lowest
28537 : (highest + lowest) / 2)
28538 - (nrefy == 0 ? ascent + descent
28539 : nrefy == 1 ? descent - boff
28540 : nrefy == 2 ? 0
28541 : (ascent + descent) / 2)
28542 + yoff);
28545 cmp->offsets[i * 2] = left;
28546 cmp->offsets[i * 2 + 1] = btm + descent;
28548 /* Update the bounding box of the overall glyphs. */
28549 if (width > 0)
28551 right = left + width;
28552 if (left < leftmost)
28553 leftmost = left;
28554 if (right > rightmost)
28555 rightmost = right;
28557 top = btm + descent + ascent;
28558 if (top > highest)
28559 highest = top;
28560 if (btm < lowest)
28561 lowest = btm;
28563 if (cmp->lbearing > left + lbearing)
28564 cmp->lbearing = left + lbearing;
28565 if (cmp->rbearing < left + rbearing)
28566 cmp->rbearing = left + rbearing;
28570 /* If there are glyphs whose x-offsets are negative,
28571 shift all glyphs to the right and make all x-offsets
28572 non-negative. */
28573 if (leftmost < 0)
28575 for (i = 0; i < cmp->glyph_len; i++)
28576 cmp->offsets[i * 2] -= leftmost;
28577 rightmost -= leftmost;
28578 cmp->lbearing -= leftmost;
28579 cmp->rbearing -= leftmost;
28582 if (left_padded && cmp->lbearing < 0)
28584 for (i = 0; i < cmp->glyph_len; i++)
28585 cmp->offsets[i * 2] -= cmp->lbearing;
28586 rightmost -= cmp->lbearing;
28587 cmp->rbearing -= cmp->lbearing;
28588 cmp->lbearing = 0;
28590 if (right_padded && rightmost < cmp->rbearing)
28592 rightmost = cmp->rbearing;
28595 cmp->pixel_width = rightmost;
28596 cmp->ascent = highest;
28597 cmp->descent = - lowest;
28598 if (cmp->ascent < font_ascent)
28599 cmp->ascent = font_ascent;
28600 if (cmp->descent < font_descent)
28601 cmp->descent = font_descent;
28604 if (it->glyph_row
28605 && (cmp->lbearing < 0
28606 || cmp->rbearing > cmp->pixel_width))
28607 it->glyph_row->contains_overlapping_glyphs_p = true;
28609 it->pixel_width = cmp->pixel_width;
28610 it->ascent = it->phys_ascent = cmp->ascent;
28611 it->descent = it->phys_descent = cmp->descent;
28612 if (face->box != FACE_NO_BOX)
28614 int thick = face->box_line_width;
28616 if (thick > 0)
28618 it->ascent += thick;
28619 it->descent += thick;
28621 else
28622 thick = - thick;
28624 if (it->start_of_box_run_p)
28625 it->pixel_width += thick;
28626 if (it->end_of_box_run_p)
28627 it->pixel_width += thick;
28630 /* If face has an overline, add the height of the overline
28631 (1 pixel) and a 1 pixel margin to the character height. */
28632 if (face->overline_p)
28633 it->ascent += overline_margin;
28635 take_vertical_position_into_account (it);
28636 if (it->ascent < 0)
28637 it->ascent = 0;
28638 if (it->descent < 0)
28639 it->descent = 0;
28641 if (it->glyph_row && cmp->glyph_len > 0)
28642 append_composite_glyph (it);
28644 else if (it->what == IT_COMPOSITION)
28646 /* A dynamic (automatic) composition. */
28647 struct face *face = FACE_FROM_ID (it->f, it->face_id);
28648 Lisp_Object gstring;
28649 struct font_metrics metrics;
28651 it->nglyphs = 1;
28653 gstring = composition_gstring_from_id (it->cmp_it.id);
28654 it->pixel_width
28655 = composition_gstring_width (gstring, it->cmp_it.from, it->cmp_it.to,
28656 &metrics);
28657 if (it->glyph_row
28658 && (metrics.lbearing < 0 || metrics.rbearing > metrics.width))
28659 it->glyph_row->contains_overlapping_glyphs_p = true;
28660 it->ascent = it->phys_ascent = metrics.ascent;
28661 it->descent = it->phys_descent = metrics.descent;
28662 if (face->box != FACE_NO_BOX)
28664 int thick = face->box_line_width;
28666 if (thick > 0)
28668 it->ascent += thick;
28669 it->descent += thick;
28671 else
28672 thick = - thick;
28674 if (it->start_of_box_run_p)
28675 it->pixel_width += thick;
28676 if (it->end_of_box_run_p)
28677 it->pixel_width += thick;
28679 /* If face has an overline, add the height of the overline
28680 (1 pixel) and a 1 pixel margin to the character height. */
28681 if (face->overline_p)
28682 it->ascent += overline_margin;
28683 take_vertical_position_into_account (it);
28684 if (it->ascent < 0)
28685 it->ascent = 0;
28686 if (it->descent < 0)
28687 it->descent = 0;
28689 if (it->glyph_row)
28690 append_composite_glyph (it);
28692 else if (it->what == IT_GLYPHLESS)
28693 produce_glyphless_glyph (it, false, Qnil);
28694 else if (it->what == IT_IMAGE)
28695 produce_image_glyph (it);
28696 else if (it->what == IT_STRETCH)
28697 produce_stretch_glyph (it);
28698 else if (it->what == IT_XWIDGET)
28699 produce_xwidget_glyph (it);
28701 done:
28702 /* Accumulate dimensions. Note: can't assume that it->descent > 0
28703 because this isn't true for images with `:ascent 100'. */
28704 eassert (it->ascent >= 0 && it->descent >= 0);
28705 if (it->area == TEXT_AREA)
28706 it->current_x += it->pixel_width;
28708 if (extra_line_spacing > 0)
28710 it->descent += extra_line_spacing;
28711 if (extra_line_spacing > it->max_extra_line_spacing)
28712 it->max_extra_line_spacing = extra_line_spacing;
28715 it->max_ascent = max (it->max_ascent, it->ascent);
28716 it->max_descent = max (it->max_descent, it->descent);
28717 it->max_phys_ascent = max (it->max_phys_ascent, it->phys_ascent);
28718 it->max_phys_descent = max (it->max_phys_descent, it->phys_descent);
28721 /* EXPORT for RIF:
28722 Output LEN glyphs starting at START at the nominal cursor position.
28723 Advance the nominal cursor over the text. UPDATED_ROW is the glyph row
28724 being updated, and UPDATED_AREA is the area of that row being updated. */
28726 void
28727 x_write_glyphs (struct window *w, struct glyph_row *updated_row,
28728 struct glyph *start, enum glyph_row_area updated_area, int len)
28730 int x, hpos, chpos = w->phys_cursor.hpos;
28732 eassert (updated_row);
28733 /* When the window is hscrolled, cursor hpos can legitimately be out
28734 of bounds, but we draw the cursor at the corresponding window
28735 margin in that case. */
28736 if (!updated_row->reversed_p && chpos < 0)
28737 chpos = 0;
28738 if (updated_row->reversed_p && chpos >= updated_row->used[TEXT_AREA])
28739 chpos = updated_row->used[TEXT_AREA] - 1;
28741 block_input ();
28743 /* Write glyphs. */
28745 hpos = start - updated_row->glyphs[updated_area];
28746 x = draw_glyphs (w, w->output_cursor.x,
28747 updated_row, updated_area,
28748 hpos, hpos + len,
28749 DRAW_NORMAL_TEXT, 0);
28751 /* Invalidate old phys cursor if the glyph at its hpos is redrawn. */
28752 if (updated_area == TEXT_AREA
28753 && w->phys_cursor_on_p
28754 && w->phys_cursor.vpos == w->output_cursor.vpos
28755 && chpos >= hpos
28756 && chpos < hpos + len)
28757 w->phys_cursor_on_p = false;
28759 unblock_input ();
28761 /* Advance the output cursor. */
28762 w->output_cursor.hpos += len;
28763 w->output_cursor.x = x;
28767 /* EXPORT for RIF:
28768 Insert LEN glyphs from START at the nominal cursor position. */
28770 void
28771 x_insert_glyphs (struct window *w, struct glyph_row *updated_row,
28772 struct glyph *start, enum glyph_row_area updated_area, int len)
28774 struct frame *f;
28775 int line_height, shift_by_width, shifted_region_width;
28776 struct glyph_row *row;
28777 struct glyph *glyph;
28778 int frame_x, frame_y;
28779 ptrdiff_t hpos;
28781 eassert (updated_row);
28782 block_input ();
28783 f = XFRAME (WINDOW_FRAME (w));
28785 /* Get the height of the line we are in. */
28786 row = updated_row;
28787 line_height = row->height;
28789 /* Get the width of the glyphs to insert. */
28790 shift_by_width = 0;
28791 for (glyph = start; glyph < start + len; ++glyph)
28792 shift_by_width += glyph->pixel_width;
28794 /* Get the width of the region to shift right. */
28795 shifted_region_width = (window_box_width (w, updated_area)
28796 - w->output_cursor.x
28797 - shift_by_width);
28799 /* Shift right. */
28800 frame_x = window_box_left (w, updated_area) + w->output_cursor.x;
28801 frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, w->output_cursor.y);
28803 FRAME_RIF (f)->shift_glyphs_for_insert (f, frame_x, frame_y, shifted_region_width,
28804 line_height, shift_by_width);
28806 /* Write the glyphs. */
28807 hpos = start - row->glyphs[updated_area];
28808 draw_glyphs (w, w->output_cursor.x, row, updated_area,
28809 hpos, hpos + len,
28810 DRAW_NORMAL_TEXT, 0);
28812 /* Advance the output cursor. */
28813 w->output_cursor.hpos += len;
28814 w->output_cursor.x += shift_by_width;
28815 unblock_input ();
28819 /* EXPORT for RIF:
28820 Erase the current text line from the nominal cursor position
28821 (inclusive) to pixel column TO_X (exclusive). The idea is that
28822 everything from TO_X onward is already erased.
28824 TO_X is a pixel position relative to UPDATED_AREA of currently
28825 updated window W. TO_X == -1 means clear to the end of this area. */
28827 void
28828 x_clear_end_of_line (struct window *w, struct glyph_row *updated_row,
28829 enum glyph_row_area updated_area, int to_x)
28831 struct frame *f;
28832 int max_x, min_y, max_y;
28833 int from_x, from_y, to_y;
28835 eassert (updated_row);
28836 f = XFRAME (w->frame);
28838 if (updated_row->full_width_p)
28839 max_x = (WINDOW_PIXEL_WIDTH (w)
28840 - (updated_row->mode_line_p ? WINDOW_RIGHT_DIVIDER_WIDTH (w) : 0));
28841 else
28842 max_x = window_box_width (w, updated_area);
28843 max_y = window_text_bottom_y (w);
28845 /* TO_X == 0 means don't do anything. TO_X < 0 means clear to end
28846 of window. For TO_X > 0, truncate to end of drawing area. */
28847 if (to_x == 0)
28848 return;
28849 else if (to_x < 0)
28850 to_x = max_x;
28851 else
28852 to_x = min (to_x, max_x);
28854 to_y = min (max_y, w->output_cursor.y + updated_row->height);
28856 /* Notice if the cursor will be cleared by this operation. */
28857 if (!updated_row->full_width_p)
28858 notice_overwritten_cursor (w, updated_area,
28859 w->output_cursor.x, -1,
28860 updated_row->y,
28861 MATRIX_ROW_BOTTOM_Y (updated_row));
28863 from_x = w->output_cursor.x;
28865 /* Translate to frame coordinates. */
28866 if (updated_row->full_width_p)
28868 from_x = WINDOW_TO_FRAME_PIXEL_X (w, from_x);
28869 to_x = WINDOW_TO_FRAME_PIXEL_X (w, to_x);
28871 else
28873 int area_left = window_box_left (w, updated_area);
28874 from_x += area_left;
28875 to_x += area_left;
28878 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
28879 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, max (min_y, w->output_cursor.y));
28880 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, to_y);
28882 /* Prevent inadvertently clearing to end of the X window. */
28883 if (to_x > from_x && to_y > from_y)
28885 block_input ();
28886 FRAME_RIF (f)->clear_frame_area (f, from_x, from_y,
28887 to_x - from_x, to_y - from_y);
28888 unblock_input ();
28892 #endif /* HAVE_WINDOW_SYSTEM */
28896 /***********************************************************************
28897 Cursor types
28898 ***********************************************************************/
28900 /* Value is the internal representation of the specified cursor type
28901 ARG. If type is BAR_CURSOR, return in *WIDTH the specified width
28902 of the bar cursor. */
28904 static enum text_cursor_kinds
28905 get_specified_cursor_type (Lisp_Object arg, int *width)
28907 enum text_cursor_kinds type;
28909 if (NILP (arg))
28910 return NO_CURSOR;
28912 if (EQ (arg, Qbox))
28913 return FILLED_BOX_CURSOR;
28915 if (EQ (arg, Qhollow))
28916 return HOLLOW_BOX_CURSOR;
28918 if (EQ (arg, Qbar))
28920 *width = 2;
28921 return BAR_CURSOR;
28924 if (CONSP (arg)
28925 && EQ (XCAR (arg), Qbar)
28926 && RANGED_INTEGERP (0, XCDR (arg), INT_MAX))
28928 *width = XINT (XCDR (arg));
28929 return BAR_CURSOR;
28932 if (EQ (arg, Qhbar))
28934 *width = 2;
28935 return HBAR_CURSOR;
28938 if (CONSP (arg)
28939 && EQ (XCAR (arg), Qhbar)
28940 && RANGED_INTEGERP (0, XCDR (arg), INT_MAX))
28942 *width = XINT (XCDR (arg));
28943 return HBAR_CURSOR;
28946 /* Treat anything unknown as "hollow box cursor".
28947 It was bad to signal an error; people have trouble fixing
28948 .Xdefaults with Emacs, when it has something bad in it. */
28949 type = HOLLOW_BOX_CURSOR;
28951 return type;
28954 /* Set the default cursor types for specified frame. */
28955 void
28956 set_frame_cursor_types (struct frame *f, Lisp_Object arg)
28958 int width = 1;
28959 Lisp_Object tem;
28961 FRAME_DESIRED_CURSOR (f) = get_specified_cursor_type (arg, &width);
28962 FRAME_CURSOR_WIDTH (f) = width;
28964 /* By default, set up the blink-off state depending on the on-state. */
28966 tem = Fassoc (arg, Vblink_cursor_alist, Qnil);
28967 if (!NILP (tem))
28969 FRAME_BLINK_OFF_CURSOR (f)
28970 = get_specified_cursor_type (XCDR (tem), &width);
28971 FRAME_BLINK_OFF_CURSOR_WIDTH (f) = width;
28973 else
28974 FRAME_BLINK_OFF_CURSOR (f) = DEFAULT_CURSOR;
28976 /* Make sure the cursor gets redrawn. */
28977 f->cursor_type_changed = true;
28981 #ifdef HAVE_WINDOW_SYSTEM
28983 /* Return the cursor we want to be displayed in window W. Return
28984 width of bar/hbar cursor through WIDTH arg. Return with
28985 ACTIVE_CURSOR arg set to true if cursor in window W is `active'
28986 (i.e. if the `system caret' should track this cursor).
28988 In a mini-buffer window, we want the cursor only to appear if we
28989 are reading input from this window. For the selected window, we
28990 want the cursor type given by the frame parameter or buffer local
28991 setting of cursor-type. If explicitly marked off, draw no cursor.
28992 In all other cases, we want a hollow box cursor. */
28994 static enum text_cursor_kinds
28995 get_window_cursor_type (struct window *w, struct glyph *glyph, int *width,
28996 bool *active_cursor)
28998 struct frame *f = XFRAME (w->frame);
28999 struct buffer *b = XBUFFER (w->contents);
29000 int cursor_type = DEFAULT_CURSOR;
29001 Lisp_Object alt_cursor;
29002 bool non_selected = false;
29004 *active_cursor = true;
29006 /* Echo area */
29007 if (cursor_in_echo_area
29008 && FRAME_HAS_MINIBUF_P (f)
29009 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
29011 if (w == XWINDOW (echo_area_window))
29013 if (EQ (BVAR (b, cursor_type), Qt) || NILP (BVAR (b, cursor_type)))
29015 *width = FRAME_CURSOR_WIDTH (f);
29016 return FRAME_DESIRED_CURSOR (f);
29018 else
29019 return get_specified_cursor_type (BVAR (b, cursor_type), width);
29022 *active_cursor = false;
29023 non_selected = true;
29026 /* Detect a nonselected window or nonselected frame. */
29027 else if (w != XWINDOW (f->selected_window)
29028 || f != FRAME_DISPLAY_INFO (f)->x_highlight_frame)
29030 *active_cursor = false;
29032 if (MINI_WINDOW_P (w) && minibuf_level == 0)
29033 return NO_CURSOR;
29035 non_selected = true;
29038 /* Never display a cursor in a window in which cursor-type is nil. */
29039 if (NILP (BVAR (b, cursor_type)))
29040 return NO_CURSOR;
29042 /* Get the normal cursor type for this window. */
29043 if (EQ (BVAR (b, cursor_type), Qt))
29045 cursor_type = FRAME_DESIRED_CURSOR (f);
29046 *width = FRAME_CURSOR_WIDTH (f);
29048 else
29049 cursor_type = get_specified_cursor_type (BVAR (b, cursor_type), width);
29051 /* Use cursor-in-non-selected-windows instead
29052 for non-selected window or frame. */
29053 if (non_selected)
29055 alt_cursor = BVAR (b, cursor_in_non_selected_windows);
29056 if (!EQ (Qt, alt_cursor))
29057 return get_specified_cursor_type (alt_cursor, width);
29058 /* t means modify the normal cursor type. */
29059 if (cursor_type == FILLED_BOX_CURSOR)
29060 cursor_type = HOLLOW_BOX_CURSOR;
29061 else if (cursor_type == BAR_CURSOR && *width > 1)
29062 --*width;
29063 return cursor_type;
29066 /* Use normal cursor if not blinked off. */
29067 if (!w->cursor_off_p)
29069 if (glyph != NULL && glyph->type == XWIDGET_GLYPH)
29070 return NO_CURSOR;
29071 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
29073 if (cursor_type == FILLED_BOX_CURSOR)
29075 /* Using a block cursor on large images can be very annoying.
29076 So use a hollow cursor for "large" images.
29077 If image is not transparent (no mask), also use hollow cursor. */
29078 struct image *img = IMAGE_OPT_FROM_ID (f, glyph->u.img_id);
29079 if (img != NULL && IMAGEP (img->spec))
29081 /* Arbitrarily, interpret "Large" as >32x32 and >NxN
29082 where N = size of default frame font size.
29083 This should cover most of the "tiny" icons people may use. */
29084 if (!img->mask
29085 || img->width > max (32, WINDOW_FRAME_COLUMN_WIDTH (w))
29086 || img->height > max (32, WINDOW_FRAME_LINE_HEIGHT (w)))
29087 cursor_type = HOLLOW_BOX_CURSOR;
29090 else if (cursor_type != NO_CURSOR)
29092 /* Display current only supports BOX and HOLLOW cursors for images.
29093 So for now, unconditionally use a HOLLOW cursor when cursor is
29094 not a solid box cursor. */
29095 cursor_type = HOLLOW_BOX_CURSOR;
29098 return cursor_type;
29101 /* Cursor is blinked off, so determine how to "toggle" it. */
29103 /* First look for an entry matching the buffer's cursor-type in blink-cursor-alist. */
29104 if ((alt_cursor = Fassoc (BVAR (b, cursor_type), Vblink_cursor_alist, Qnil), !NILP (alt_cursor)))
29105 return get_specified_cursor_type (XCDR (alt_cursor), width);
29107 /* Then see if frame has specified a specific blink off cursor type. */
29108 if (FRAME_BLINK_OFF_CURSOR (f) != DEFAULT_CURSOR)
29110 *width = FRAME_BLINK_OFF_CURSOR_WIDTH (f);
29111 return FRAME_BLINK_OFF_CURSOR (f);
29114 #if false
29115 /* Some people liked having a permanently visible blinking cursor,
29116 while others had very strong opinions against it. So it was
29117 decided to remove it. KFS 2003-09-03 */
29119 /* Finally perform built-in cursor blinking:
29120 filled box <-> hollow box
29121 wide [h]bar <-> narrow [h]bar
29122 narrow [h]bar <-> no cursor
29123 other type <-> no cursor */
29125 if (cursor_type == FILLED_BOX_CURSOR)
29126 return HOLLOW_BOX_CURSOR;
29128 if ((cursor_type == BAR_CURSOR || cursor_type == HBAR_CURSOR) && *width > 1)
29130 *width = 1;
29131 return cursor_type;
29133 #endif
29135 return NO_CURSOR;
29139 /* Notice when the text cursor of window W has been completely
29140 overwritten by a drawing operation that outputs glyphs in AREA
29141 starting at X0 and ending at X1 in the line starting at Y0 and
29142 ending at Y1. X coordinates are area-relative. X1 < 0 means all
29143 the rest of the line after X0 has been written. Y coordinates
29144 are window-relative. */
29146 static void
29147 notice_overwritten_cursor (struct window *w, enum glyph_row_area area,
29148 int x0, int x1, int y0, int y1)
29150 int cx0, cx1, cy0, cy1;
29151 struct glyph_row *row;
29153 if (!w->phys_cursor_on_p)
29154 return;
29155 if (area != TEXT_AREA)
29156 return;
29158 if (w->phys_cursor.vpos < 0
29159 || w->phys_cursor.vpos >= w->current_matrix->nrows
29160 || (row = w->current_matrix->rows + w->phys_cursor.vpos,
29161 !(row->enabled_p && MATRIX_ROW_DISPLAYS_TEXT_P (row))))
29162 return;
29164 if (row->cursor_in_fringe_p)
29166 row->cursor_in_fringe_p = false;
29167 draw_fringe_bitmap (w, row, row->reversed_p);
29168 w->phys_cursor_on_p = false;
29169 return;
29172 cx0 = w->phys_cursor.x;
29173 cx1 = cx0 + w->phys_cursor_width;
29174 if (x0 > cx0 || (x1 >= 0 && x1 < cx1))
29175 return;
29177 /* The cursor image will be completely removed from the
29178 screen if the output area intersects the cursor area in
29179 y-direction. When we draw in [y0 y1[, and some part of
29180 the cursor is at y < y0, that part must have been drawn
29181 before. When scrolling, the cursor is erased before
29182 actually scrolling, so we don't come here. When not
29183 scrolling, the rows above the old cursor row must have
29184 changed, and in this case these rows must have written
29185 over the cursor image.
29187 Likewise if part of the cursor is below y1, with the
29188 exception of the cursor being in the first blank row at
29189 the buffer and window end because update_text_area
29190 doesn't draw that row. (Except when it does, but
29191 that's handled in update_text_area.) */
29193 cy0 = w->phys_cursor.y;
29194 cy1 = cy0 + w->phys_cursor_height;
29195 if ((y0 < cy0 || y0 >= cy1) && (y1 <= cy0 || y1 >= cy1))
29196 return;
29198 w->phys_cursor_on_p = false;
29201 #endif /* HAVE_WINDOW_SYSTEM */
29204 /************************************************************************
29205 Mouse Face
29206 ************************************************************************/
29208 #ifdef HAVE_WINDOW_SYSTEM
29210 /* EXPORT for RIF:
29211 Fix the display of area AREA of overlapping row ROW in window W
29212 with respect to the overlapping part OVERLAPS. */
29214 void
29215 x_fix_overlapping_area (struct window *w, struct glyph_row *row,
29216 enum glyph_row_area area, int overlaps)
29218 int i, x;
29220 block_input ();
29222 x = 0;
29223 for (i = 0; i < row->used[area];)
29225 if (row->glyphs[area][i].overlaps_vertically_p)
29227 int start = i, start_x = x;
29231 x += row->glyphs[area][i].pixel_width;
29232 ++i;
29234 while (i < row->used[area]
29235 && row->glyphs[area][i].overlaps_vertically_p);
29237 draw_glyphs (w, start_x, row, area,
29238 start, i,
29239 DRAW_NORMAL_TEXT, overlaps);
29241 else
29243 x += row->glyphs[area][i].pixel_width;
29244 ++i;
29248 unblock_input ();
29252 /* EXPORT:
29253 Draw the cursor glyph of window W in glyph row ROW. See the
29254 comment of draw_glyphs for the meaning of HL. */
29256 void
29257 draw_phys_cursor_glyph (struct window *w, struct glyph_row *row,
29258 enum draw_glyphs_face hl)
29260 /* If cursor hpos is out of bounds, don't draw garbage. This can
29261 happen in mini-buffer windows when switching between echo area
29262 glyphs and mini-buffer. */
29263 if ((row->reversed_p
29264 ? (w->phys_cursor.hpos >= 0)
29265 : (w->phys_cursor.hpos < row->used[TEXT_AREA])))
29267 bool on_p = w->phys_cursor_on_p;
29268 int x1;
29269 int hpos = w->phys_cursor.hpos;
29271 /* When the window is hscrolled, cursor hpos can legitimately be
29272 out of bounds, but we draw the cursor at the corresponding
29273 window margin in that case. */
29274 if (!row->reversed_p && hpos < 0)
29275 hpos = 0;
29276 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
29277 hpos = row->used[TEXT_AREA] - 1;
29279 x1 = draw_glyphs (w, w->phys_cursor.x, row, TEXT_AREA, hpos, hpos + 1,
29280 hl, 0);
29281 w->phys_cursor_on_p = on_p;
29283 if (hl == DRAW_CURSOR)
29284 w->phys_cursor_width = x1 - w->phys_cursor.x;
29285 /* When we erase the cursor, and ROW is overlapped by other
29286 rows, make sure that these overlapping parts of other rows
29287 are redrawn. */
29288 else if (hl == DRAW_NORMAL_TEXT && row->overlapped_p)
29290 w->phys_cursor_width = x1 - w->phys_cursor.x;
29292 if (row > w->current_matrix->rows
29293 && MATRIX_ROW_OVERLAPS_SUCC_P (row - 1))
29294 x_fix_overlapping_area (w, row - 1, TEXT_AREA,
29295 OVERLAPS_ERASED_CURSOR);
29297 if (MATRIX_ROW_BOTTOM_Y (row) < window_text_bottom_y (w)
29298 && MATRIX_ROW_OVERLAPS_PRED_P (row + 1))
29299 x_fix_overlapping_area (w, row + 1, TEXT_AREA,
29300 OVERLAPS_ERASED_CURSOR);
29306 /* Erase the image of a cursor of window W from the screen. */
29308 void
29309 erase_phys_cursor (struct window *w)
29311 struct frame *f = XFRAME (w->frame);
29312 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
29313 int hpos = w->phys_cursor.hpos;
29314 int vpos = w->phys_cursor.vpos;
29315 bool mouse_face_here_p = false;
29316 struct glyph_matrix *active_glyphs = w->current_matrix;
29317 struct glyph_row *cursor_row;
29318 struct glyph *cursor_glyph;
29319 enum draw_glyphs_face hl;
29321 /* No cursor displayed or row invalidated => nothing to do on the
29322 screen. */
29323 if (w->phys_cursor_type == NO_CURSOR)
29324 goto mark_cursor_off;
29326 /* VPOS >= active_glyphs->nrows means that window has been resized.
29327 Don't bother to erase the cursor. */
29328 if (vpos >= active_glyphs->nrows)
29329 goto mark_cursor_off;
29331 /* If row containing cursor is marked invalid, there is nothing we
29332 can do. */
29333 cursor_row = MATRIX_ROW (active_glyphs, vpos);
29334 if (!cursor_row->enabled_p)
29335 goto mark_cursor_off;
29337 /* If line spacing is > 0, old cursor may only be partially visible in
29338 window after split-window. So adjust visible height. */
29339 cursor_row->visible_height = min (cursor_row->visible_height,
29340 window_text_bottom_y (w) - cursor_row->y);
29342 /* If row is completely invisible, don't attempt to delete a cursor which
29343 isn't there. This can happen if cursor is at top of a window, and
29344 we switch to a buffer with a header line in that window. */
29345 if (cursor_row->visible_height <= 0)
29346 goto mark_cursor_off;
29348 /* If cursor is in the fringe, erase by drawing actual bitmap there. */
29349 if (cursor_row->cursor_in_fringe_p)
29351 cursor_row->cursor_in_fringe_p = false;
29352 draw_fringe_bitmap (w, cursor_row, cursor_row->reversed_p);
29353 goto mark_cursor_off;
29356 /* This can happen when the new row is shorter than the old one.
29357 In this case, either draw_glyphs or clear_end_of_line
29358 should have cleared the cursor. Note that we wouldn't be
29359 able to erase the cursor in this case because we don't have a
29360 cursor glyph at hand. */
29361 if ((cursor_row->reversed_p
29362 ? (w->phys_cursor.hpos < 0)
29363 : (w->phys_cursor.hpos >= cursor_row->used[TEXT_AREA])))
29364 goto mark_cursor_off;
29366 /* When the window is hscrolled, cursor hpos can legitimately be out
29367 of bounds, but we draw the cursor at the corresponding window
29368 margin in that case. */
29369 if (!cursor_row->reversed_p && hpos < 0)
29370 hpos = 0;
29371 if (cursor_row->reversed_p && hpos >= cursor_row->used[TEXT_AREA])
29372 hpos = cursor_row->used[TEXT_AREA] - 1;
29374 /* If the cursor is in the mouse face area, redisplay that when
29375 we clear the cursor. */
29376 if (! NILP (hlinfo->mouse_face_window)
29377 && coords_in_mouse_face_p (w, hpos, vpos)
29378 /* Don't redraw the cursor's spot in mouse face if it is at the
29379 end of a line (on a newline). The cursor appears there, but
29380 mouse highlighting does not. */
29381 && cursor_row->used[TEXT_AREA] > hpos && hpos >= 0)
29382 mouse_face_here_p = true;
29384 /* Maybe clear the display under the cursor. */
29385 if (w->phys_cursor_type == HOLLOW_BOX_CURSOR)
29387 int x, y;
29388 int header_line_height = WINDOW_HEADER_LINE_HEIGHT (w);
29389 int width;
29391 cursor_glyph = get_phys_cursor_glyph (w);
29392 if (cursor_glyph == NULL)
29393 goto mark_cursor_off;
29395 width = cursor_glyph->pixel_width;
29396 x = w->phys_cursor.x;
29397 if (x < 0)
29399 width += x;
29400 x = 0;
29402 width = min (width, window_box_width (w, TEXT_AREA) - x);
29403 y = WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height, cursor_row->y));
29404 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, x);
29406 if (width > 0)
29407 FRAME_RIF (f)->clear_frame_area (f, x, y, width, cursor_row->visible_height);
29410 /* Erase the cursor by redrawing the character underneath it. */
29411 if (mouse_face_here_p)
29412 hl = DRAW_MOUSE_FACE;
29413 else
29414 hl = DRAW_NORMAL_TEXT;
29415 draw_phys_cursor_glyph (w, cursor_row, hl);
29417 mark_cursor_off:
29418 w->phys_cursor_on_p = false;
29419 w->phys_cursor_type = NO_CURSOR;
29423 /* Display or clear cursor of window W. If !ON, clear the cursor.
29424 If ON, display the cursor; where to put the cursor is specified by
29425 HPOS, VPOS, X and Y. */
29427 void
29428 display_and_set_cursor (struct window *w, bool on,
29429 int hpos, int vpos, int x, int y)
29431 struct frame *f = XFRAME (w->frame);
29432 int new_cursor_type;
29433 int new_cursor_width;
29434 bool active_cursor;
29435 struct glyph_row *glyph_row;
29436 struct glyph *glyph;
29438 /* This is pointless on invisible frames, and dangerous on garbaged
29439 windows and frames; in the latter case, the frame or window may
29440 be in the midst of changing its size, and x and y may be off the
29441 window. */
29442 if (! FRAME_VISIBLE_P (f)
29443 || vpos >= w->current_matrix->nrows
29444 || hpos >= w->current_matrix->matrix_w)
29445 return;
29447 /* If cursor is off and we want it off, return quickly. */
29448 if (!on && !w->phys_cursor_on_p)
29449 return;
29451 glyph_row = MATRIX_ROW (w->current_matrix, vpos);
29452 /* If cursor row is not enabled, we don't really know where to
29453 display the cursor. */
29454 if (!glyph_row->enabled_p)
29456 w->phys_cursor_on_p = false;
29457 return;
29460 /* A frame might be marked garbaged even though its cursor position
29461 is correct, and will not change upon subsequent redisplay. This
29462 happens in some rare situations, like toggling the sort order in
29463 Dired windows. We've already established that VPOS is valid, so
29464 it shouldn't do any harm to record the cursor position, as we are
29465 going to return without acting on it anyway. Otherwise, expose
29466 events might come in and call update_window_cursor, which will
29467 blindly use outdated values in w->phys_cursor. */
29468 if (FRAME_GARBAGED_P (f))
29470 if (on)
29472 w->phys_cursor.x = x;
29473 w->phys_cursor.y = glyph_row->y;
29474 w->phys_cursor.hpos = hpos;
29475 w->phys_cursor.vpos = vpos;
29477 return;
29480 glyph = NULL;
29481 if (0 <= hpos && hpos < glyph_row->used[TEXT_AREA])
29482 glyph = glyph_row->glyphs[TEXT_AREA] + hpos;
29484 eassert (input_blocked_p ());
29486 /* Set new_cursor_type to the cursor we want to be displayed. */
29487 new_cursor_type = get_window_cursor_type (w, glyph,
29488 &new_cursor_width, &active_cursor);
29490 /* If cursor is currently being shown and we don't want it to be or
29491 it is in the wrong place, or the cursor type is not what we want,
29492 erase it. */
29493 if (w->phys_cursor_on_p
29494 && (!on
29495 || w->phys_cursor.x != x
29496 || w->phys_cursor.y != y
29497 /* HPOS can be negative in R2L rows whose
29498 exact_window_width_line_p flag is set (i.e. their newline
29499 would "overflow into the fringe"). */
29500 || hpos < 0
29501 || new_cursor_type != w->phys_cursor_type
29502 || ((new_cursor_type == BAR_CURSOR || new_cursor_type == HBAR_CURSOR)
29503 && new_cursor_width != w->phys_cursor_width)))
29504 erase_phys_cursor (w);
29506 /* Don't check phys_cursor_on_p here because that flag is only set
29507 to false in some cases where we know that the cursor has been
29508 completely erased, to avoid the extra work of erasing the cursor
29509 twice. In other words, phys_cursor_on_p can be true and the cursor
29510 still not be visible, or it has only been partly erased. */
29511 if (on)
29513 w->phys_cursor_ascent = glyph_row->ascent;
29514 w->phys_cursor_height = glyph_row->height;
29516 /* Set phys_cursor_.* before x_draw_.* is called because some
29517 of them may need the information. */
29518 w->phys_cursor.x = x;
29519 w->phys_cursor.y = glyph_row->y;
29520 w->phys_cursor.hpos = hpos;
29521 w->phys_cursor.vpos = vpos;
29524 FRAME_RIF (f)->draw_window_cursor (w, glyph_row, x, y,
29525 new_cursor_type, new_cursor_width,
29526 on, active_cursor);
29530 /* Switch the display of W's cursor on or off, according to the value
29531 of ON. */
29533 static void
29534 update_window_cursor (struct window *w, bool on)
29536 /* Don't update cursor in windows whose frame is in the process
29537 of being deleted. */
29538 if (w->current_matrix)
29540 int hpos = w->phys_cursor.hpos;
29541 int vpos = w->phys_cursor.vpos;
29542 struct glyph_row *row;
29544 if (vpos >= w->current_matrix->nrows
29545 || hpos >= w->current_matrix->matrix_w)
29546 return;
29548 row = MATRIX_ROW (w->current_matrix, vpos);
29550 /* When the window is hscrolled, cursor hpos can legitimately be
29551 out of bounds, but we draw the cursor at the corresponding
29552 window margin in that case. */
29553 if (!row->reversed_p && hpos < 0)
29554 hpos = 0;
29555 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
29556 hpos = row->used[TEXT_AREA] - 1;
29558 block_input ();
29559 display_and_set_cursor (w, on, hpos, vpos,
29560 w->phys_cursor.x, w->phys_cursor.y);
29561 unblock_input ();
29566 /* Call update_window_cursor with parameter ON_P on all leaf windows
29567 in the window tree rooted at W. */
29569 static void
29570 update_cursor_in_window_tree (struct window *w, bool on_p)
29572 while (w)
29574 if (WINDOWP (w->contents))
29575 update_cursor_in_window_tree (XWINDOW (w->contents), on_p);
29576 else
29577 update_window_cursor (w, on_p);
29579 w = NILP (w->next) ? 0 : XWINDOW (w->next);
29584 /* EXPORT:
29585 Display the cursor on window W, or clear it, according to ON_P.
29586 Don't change the cursor's position. */
29588 void
29589 x_update_cursor (struct frame *f, bool on_p)
29591 update_cursor_in_window_tree (XWINDOW (f->root_window), on_p);
29595 /* EXPORT:
29596 Clear the cursor of window W to background color, and mark the
29597 cursor as not shown. This is used when the text where the cursor
29598 is about to be rewritten. */
29600 void
29601 x_clear_cursor (struct window *w)
29603 if (FRAME_VISIBLE_P (XFRAME (w->frame)) && w->phys_cursor_on_p)
29604 update_window_cursor (w, false);
29607 #endif /* HAVE_WINDOW_SYSTEM */
29609 /* Implementation of draw_row_with_mouse_face for GUI sessions, GPM,
29610 and MSDOS. */
29611 static void
29612 draw_row_with_mouse_face (struct window *w, int start_x, struct glyph_row *row,
29613 int start_hpos, int end_hpos,
29614 enum draw_glyphs_face draw)
29616 #ifdef HAVE_WINDOW_SYSTEM
29617 if (FRAME_WINDOW_P (XFRAME (w->frame)))
29619 draw_glyphs (w, start_x, row, TEXT_AREA, start_hpos, end_hpos, draw, 0);
29620 return;
29622 #endif
29623 #if defined (HAVE_GPM) || defined (MSDOS) || defined (WINDOWSNT)
29624 tty_draw_row_with_mouse_face (w, row, start_hpos, end_hpos, draw);
29625 #endif
29628 /* Display the active region described by mouse_face_* according to DRAW. */
29630 static void
29631 show_mouse_face (Mouse_HLInfo *hlinfo, enum draw_glyphs_face draw)
29633 struct window *w = XWINDOW (hlinfo->mouse_face_window);
29634 struct frame *f = XFRAME (WINDOW_FRAME (w));
29636 if (/* If window is in the process of being destroyed, don't bother
29637 to do anything. */
29638 w->current_matrix != NULL
29639 /* Don't update mouse highlight if hidden. */
29640 && (draw != DRAW_MOUSE_FACE || !hlinfo->mouse_face_hidden)
29641 /* Recognize when we are called to operate on rows that don't exist
29642 anymore. This can happen when a window is split. */
29643 && hlinfo->mouse_face_end_row < w->current_matrix->nrows)
29645 bool phys_cursor_on_p = w->phys_cursor_on_p;
29646 struct glyph_row *row, *first, *last;
29648 first = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_beg_row);
29649 last = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_end_row);
29651 for (row = first; row <= last && row->enabled_p; ++row)
29653 int start_hpos, end_hpos, start_x;
29655 /* For all but the first row, the highlight starts at column 0. */
29656 if (row == first)
29658 /* R2L rows have BEG and END in reversed order, but the
29659 screen drawing geometry is always left to right. So
29660 we need to mirror the beginning and end of the
29661 highlighted area in R2L rows. */
29662 if (!row->reversed_p)
29664 start_hpos = hlinfo->mouse_face_beg_col;
29665 start_x = hlinfo->mouse_face_beg_x;
29667 else if (row == last)
29669 start_hpos = hlinfo->mouse_face_end_col;
29670 start_x = hlinfo->mouse_face_end_x;
29672 else
29674 start_hpos = 0;
29675 start_x = 0;
29678 else if (row->reversed_p && row == last)
29680 start_hpos = hlinfo->mouse_face_end_col;
29681 start_x = hlinfo->mouse_face_end_x;
29683 else
29685 start_hpos = 0;
29686 start_x = 0;
29689 if (row == last)
29691 if (!row->reversed_p)
29692 end_hpos = hlinfo->mouse_face_end_col;
29693 else if (row == first)
29694 end_hpos = hlinfo->mouse_face_beg_col;
29695 else
29697 end_hpos = row->used[TEXT_AREA];
29698 if (draw == DRAW_NORMAL_TEXT)
29699 row->fill_line_p = true; /* Clear to end of line. */
29702 else if (row->reversed_p && row == first)
29703 end_hpos = hlinfo->mouse_face_beg_col;
29704 else
29706 end_hpos = row->used[TEXT_AREA];
29707 if (draw == DRAW_NORMAL_TEXT)
29708 row->fill_line_p = true; /* Clear to end of line. */
29711 if (end_hpos > start_hpos)
29713 draw_row_with_mouse_face (w, start_x, row,
29714 start_hpos, end_hpos, draw);
29716 row->mouse_face_p
29717 = draw == DRAW_MOUSE_FACE || draw == DRAW_IMAGE_RAISED;
29721 /* When we've written over the cursor, arrange for it to
29722 be displayed again. */
29723 if (FRAME_WINDOW_P (f)
29724 && phys_cursor_on_p && !w->phys_cursor_on_p)
29726 #ifdef HAVE_WINDOW_SYSTEM
29727 int hpos = w->phys_cursor.hpos;
29729 /* When the window is hscrolled, cursor hpos can legitimately be
29730 out of bounds, but we draw the cursor at the corresponding
29731 window margin in that case. */
29732 if (!row->reversed_p && hpos < 0)
29733 hpos = 0;
29734 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
29735 hpos = row->used[TEXT_AREA] - 1;
29737 block_input ();
29738 display_and_set_cursor (w, true, hpos, w->phys_cursor.vpos,
29739 w->phys_cursor.x, w->phys_cursor.y);
29740 unblock_input ();
29741 #endif /* HAVE_WINDOW_SYSTEM */
29745 #ifdef HAVE_WINDOW_SYSTEM
29746 /* Change the mouse cursor. */
29747 if (FRAME_WINDOW_P (f) && NILP (do_mouse_tracking))
29749 #if ! defined (USE_GTK) && ! defined (HAVE_NS)
29750 if (draw == DRAW_NORMAL_TEXT
29751 && !EQ (hlinfo->mouse_face_window, f->tool_bar_window))
29752 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->text_cursor);
29753 else
29754 #endif
29755 if (draw == DRAW_MOUSE_FACE)
29756 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->hand_cursor);
29757 else
29758 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->nontext_cursor);
29760 #endif /* HAVE_WINDOW_SYSTEM */
29763 /* EXPORT:
29764 Clear out the mouse-highlighted active region.
29765 Redraw it un-highlighted first. Value is true if mouse
29766 face was actually drawn unhighlighted. */
29768 bool
29769 clear_mouse_face (Mouse_HLInfo *hlinfo)
29771 bool cleared
29772 = !hlinfo->mouse_face_hidden && !NILP (hlinfo->mouse_face_window);
29773 if (cleared)
29774 show_mouse_face (hlinfo, DRAW_NORMAL_TEXT);
29775 hlinfo->mouse_face_beg_row = hlinfo->mouse_face_beg_col = -1;
29776 hlinfo->mouse_face_end_row = hlinfo->mouse_face_end_col = -1;
29777 hlinfo->mouse_face_window = Qnil;
29778 hlinfo->mouse_face_overlay = Qnil;
29779 return cleared;
29782 /* Return true if the coordinates HPOS and VPOS on windows W are
29783 within the mouse face on that window. */
29784 static bool
29785 coords_in_mouse_face_p (struct window *w, int hpos, int vpos)
29787 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (XFRAME (w->frame));
29789 /* Quickly resolve the easy cases. */
29790 if (!(WINDOWP (hlinfo->mouse_face_window)
29791 && XWINDOW (hlinfo->mouse_face_window) == w))
29792 return false;
29793 if (vpos < hlinfo->mouse_face_beg_row
29794 || vpos > hlinfo->mouse_face_end_row)
29795 return false;
29796 if (vpos > hlinfo->mouse_face_beg_row
29797 && vpos < hlinfo->mouse_face_end_row)
29798 return true;
29800 if (!MATRIX_ROW (w->current_matrix, vpos)->reversed_p)
29802 if (hlinfo->mouse_face_beg_row == hlinfo->mouse_face_end_row)
29804 if (hlinfo->mouse_face_beg_col <= hpos && hpos < hlinfo->mouse_face_end_col)
29805 return true;
29807 else if ((vpos == hlinfo->mouse_face_beg_row
29808 && hpos >= hlinfo->mouse_face_beg_col)
29809 || (vpos == hlinfo->mouse_face_end_row
29810 && hpos < hlinfo->mouse_face_end_col))
29811 return true;
29813 else
29815 if (hlinfo->mouse_face_beg_row == hlinfo->mouse_face_end_row)
29817 if (hlinfo->mouse_face_end_col < hpos && hpos <= hlinfo->mouse_face_beg_col)
29818 return true;
29820 else if ((vpos == hlinfo->mouse_face_beg_row
29821 && hpos <= hlinfo->mouse_face_beg_col)
29822 || (vpos == hlinfo->mouse_face_end_row
29823 && hpos > hlinfo->mouse_face_end_col))
29824 return true;
29826 return false;
29830 /* EXPORT:
29831 True if physical cursor of window W is within mouse face. */
29833 bool
29834 cursor_in_mouse_face_p (struct window *w)
29836 int hpos = w->phys_cursor.hpos;
29837 int vpos = w->phys_cursor.vpos;
29838 struct glyph_row *row = MATRIX_ROW (w->current_matrix, vpos);
29840 /* When the window is hscrolled, cursor hpos can legitimately be out
29841 of bounds, but we draw the cursor at the corresponding window
29842 margin in that case. */
29843 if (!row->reversed_p && hpos < 0)
29844 hpos = 0;
29845 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
29846 hpos = row->used[TEXT_AREA] - 1;
29848 return coords_in_mouse_face_p (w, hpos, vpos);
29853 /* Find the glyph rows START_ROW and END_ROW of window W that display
29854 characters between buffer positions START_CHARPOS and END_CHARPOS
29855 (excluding END_CHARPOS). DISP_STRING is a display string that
29856 covers these buffer positions. This is similar to
29857 row_containing_pos, but is more accurate when bidi reordering makes
29858 buffer positions change non-linearly with glyph rows. */
29859 static void
29860 rows_from_pos_range (struct window *w,
29861 ptrdiff_t start_charpos, ptrdiff_t end_charpos,
29862 Lisp_Object disp_string,
29863 struct glyph_row **start, struct glyph_row **end)
29865 struct glyph_row *first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
29866 int last_y = window_text_bottom_y (w);
29867 struct glyph_row *row;
29869 *start = NULL;
29870 *end = NULL;
29872 while (!first->enabled_p
29873 && first < MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w))
29874 first++;
29876 /* Find the START row. */
29877 for (row = first;
29878 row->enabled_p && MATRIX_ROW_BOTTOM_Y (row) <= last_y;
29879 row++)
29881 /* A row can potentially be the START row if the range of the
29882 characters it displays intersects the range
29883 [START_CHARPOS..END_CHARPOS). */
29884 if (! ((start_charpos < MATRIX_ROW_START_CHARPOS (row)
29885 && end_charpos < MATRIX_ROW_START_CHARPOS (row))
29886 /* See the commentary in row_containing_pos, for the
29887 explanation of the complicated way to check whether
29888 some position is beyond the end of the characters
29889 displayed by a row. */
29890 || ((start_charpos > MATRIX_ROW_END_CHARPOS (row)
29891 || (start_charpos == MATRIX_ROW_END_CHARPOS (row)
29892 && !row->ends_at_zv_p
29893 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
29894 && (end_charpos > MATRIX_ROW_END_CHARPOS (row)
29895 || (end_charpos == MATRIX_ROW_END_CHARPOS (row)
29896 && !row->ends_at_zv_p
29897 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))))))
29899 /* Found a candidate row. Now make sure at least one of the
29900 glyphs it displays has a charpos from the range
29901 [START_CHARPOS..END_CHARPOS).
29903 This is not obvious because bidi reordering could make
29904 buffer positions of a row be 1,2,3,102,101,100, and if we
29905 want to highlight characters in [50..60), we don't want
29906 this row, even though [50..60) does intersect [1..103),
29907 the range of character positions given by the row's start
29908 and end positions. */
29909 struct glyph *g = row->glyphs[TEXT_AREA];
29910 struct glyph *e = g + row->used[TEXT_AREA];
29912 while (g < e)
29914 if (((BUFFERP (g->object) || NILP (g->object))
29915 && start_charpos <= g->charpos && g->charpos < end_charpos)
29916 /* A glyph that comes from DISP_STRING is by
29917 definition to be highlighted. */
29918 || EQ (g->object, disp_string))
29919 *start = row;
29920 g++;
29922 if (*start)
29923 break;
29927 /* Find the END row. */
29928 if (!*start
29929 /* If the last row is partially visible, start looking for END
29930 from that row, instead of starting from FIRST. */
29931 && !(row->enabled_p
29932 && row->y < last_y && MATRIX_ROW_BOTTOM_Y (row) > last_y))
29933 row = first;
29934 for ( ; row->enabled_p && MATRIX_ROW_BOTTOM_Y (row) <= last_y; row++)
29936 struct glyph_row *next = row + 1;
29937 ptrdiff_t next_start = MATRIX_ROW_START_CHARPOS (next);
29939 if (!next->enabled_p
29940 || next >= MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w)
29941 /* The first row >= START whose range of displayed characters
29942 does NOT intersect the range [START_CHARPOS..END_CHARPOS]
29943 is the row END + 1. */
29944 || (start_charpos < next_start
29945 && end_charpos < next_start)
29946 || ((start_charpos > MATRIX_ROW_END_CHARPOS (next)
29947 || (start_charpos == MATRIX_ROW_END_CHARPOS (next)
29948 && !next->ends_at_zv_p
29949 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (next)))
29950 && (end_charpos > MATRIX_ROW_END_CHARPOS (next)
29951 || (end_charpos == MATRIX_ROW_END_CHARPOS (next)
29952 && !next->ends_at_zv_p
29953 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (next)))))
29955 *end = row;
29956 break;
29958 else
29960 /* If the next row's edges intersect [START_CHARPOS..END_CHARPOS],
29961 but none of the characters it displays are in the range, it is
29962 also END + 1. */
29963 struct glyph *g = next->glyphs[TEXT_AREA];
29964 struct glyph *s = g;
29965 struct glyph *e = g + next->used[TEXT_AREA];
29967 while (g < e)
29969 if (((BUFFERP (g->object) || NILP (g->object))
29970 && ((start_charpos <= g->charpos && g->charpos < end_charpos)
29971 /* If the buffer position of the first glyph in
29972 the row is equal to END_CHARPOS, it means
29973 the last character to be highlighted is the
29974 newline of ROW, and we must consider NEXT as
29975 END, not END+1. */
29976 || (((!next->reversed_p && g == s)
29977 || (next->reversed_p && g == e - 1))
29978 && (g->charpos == end_charpos
29979 /* Special case for when NEXT is an
29980 empty line at ZV. */
29981 || (g->charpos == -1
29982 && !row->ends_at_zv_p
29983 && next_start == end_charpos)))))
29984 /* A glyph that comes from DISP_STRING is by
29985 definition to be highlighted. */
29986 || EQ (g->object, disp_string))
29987 break;
29988 g++;
29990 if (g == e)
29992 *end = row;
29993 break;
29995 /* The first row that ends at ZV must be the last to be
29996 highlighted. */
29997 else if (next->ends_at_zv_p)
29999 *end = next;
30000 break;
30006 /* This function sets the mouse_face_* elements of HLINFO, assuming
30007 the mouse cursor is on a glyph with buffer charpos MOUSE_CHARPOS in
30008 window WINDOW. START_CHARPOS and END_CHARPOS are buffer positions
30009 for the overlay or run of text properties specifying the mouse
30010 face. BEFORE_STRING and AFTER_STRING, if non-nil, are a
30011 before-string and after-string that must also be highlighted.
30012 DISP_STRING, if non-nil, is a display string that may cover some
30013 or all of the highlighted text. */
30015 static void
30016 mouse_face_from_buffer_pos (Lisp_Object window,
30017 Mouse_HLInfo *hlinfo,
30018 ptrdiff_t mouse_charpos,
30019 ptrdiff_t start_charpos,
30020 ptrdiff_t end_charpos,
30021 Lisp_Object before_string,
30022 Lisp_Object after_string,
30023 Lisp_Object disp_string)
30025 struct window *w = XWINDOW (window);
30026 struct glyph_row *first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
30027 struct glyph_row *r1, *r2;
30028 struct glyph *glyph, *end;
30029 ptrdiff_t ignore, pos;
30030 int x;
30032 eassert (NILP (disp_string) || STRINGP (disp_string));
30033 eassert (NILP (before_string) || STRINGP (before_string));
30034 eassert (NILP (after_string) || STRINGP (after_string));
30036 /* Find the rows corresponding to START_CHARPOS and END_CHARPOS. */
30037 rows_from_pos_range (w, start_charpos, end_charpos, disp_string, &r1, &r2);
30038 if (r1 == NULL)
30039 r1 = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
30040 /* If the before-string or display-string contains newlines,
30041 rows_from_pos_range skips to its last row. Move back. */
30042 if (!NILP (before_string) || !NILP (disp_string))
30044 struct glyph_row *prev;
30045 while ((prev = r1 - 1, prev >= first)
30046 && MATRIX_ROW_END_CHARPOS (prev) == start_charpos
30047 && prev->used[TEXT_AREA] > 0)
30049 struct glyph *beg = prev->glyphs[TEXT_AREA];
30050 glyph = beg + prev->used[TEXT_AREA];
30051 while (--glyph >= beg && NILP (glyph->object));
30052 if (glyph < beg
30053 || !(EQ (glyph->object, before_string)
30054 || EQ (glyph->object, disp_string)))
30055 break;
30056 r1 = prev;
30059 if (r2 == NULL)
30061 r2 = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
30062 hlinfo->mouse_face_past_end = true;
30064 else if (!NILP (after_string))
30066 /* If the after-string has newlines, advance to its last row. */
30067 struct glyph_row *next;
30068 struct glyph_row *last
30069 = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
30071 for (next = r2 + 1;
30072 next <= last
30073 && next->used[TEXT_AREA] > 0
30074 && EQ (next->glyphs[TEXT_AREA]->object, after_string);
30075 ++next)
30076 r2 = next;
30078 /* The rest of the display engine assumes that mouse_face_beg_row is
30079 either above mouse_face_end_row or identical to it. But with
30080 bidi-reordered continued lines, the row for START_CHARPOS could
30081 be below the row for END_CHARPOS. If so, swap the rows and store
30082 them in correct order. */
30083 if (r1->y > r2->y)
30085 struct glyph_row *tem = r2;
30087 r2 = r1;
30088 r1 = tem;
30091 hlinfo->mouse_face_beg_row = MATRIX_ROW_VPOS (r1, w->current_matrix);
30092 hlinfo->mouse_face_end_row = MATRIX_ROW_VPOS (r2, w->current_matrix);
30094 /* For a bidi-reordered row, the positions of BEFORE_STRING,
30095 AFTER_STRING, DISP_STRING, START_CHARPOS, and END_CHARPOS
30096 could be anywhere in the row and in any order. The strategy
30097 below is to find the leftmost and the rightmost glyph that
30098 belongs to either of these 3 strings, or whose position is
30099 between START_CHARPOS and END_CHARPOS, and highlight all the
30100 glyphs between those two. This may cover more than just the text
30101 between START_CHARPOS and END_CHARPOS if the range of characters
30102 strides the bidi level boundary, e.g. if the beginning is in R2L
30103 text while the end is in L2R text or vice versa. */
30104 if (!r1->reversed_p)
30106 /* This row is in a left to right paragraph. Scan it left to
30107 right. */
30108 glyph = r1->glyphs[TEXT_AREA];
30109 end = glyph + r1->used[TEXT_AREA];
30110 x = r1->x;
30112 /* Skip truncation glyphs at the start of the glyph row. */
30113 if (MATRIX_ROW_DISPLAYS_TEXT_P (r1))
30114 for (; glyph < end
30115 && NILP (glyph->object)
30116 && glyph->charpos < 0;
30117 ++glyph)
30118 x += glyph->pixel_width;
30120 /* Scan the glyph row, looking for BEFORE_STRING, AFTER_STRING,
30121 or DISP_STRING, and the first glyph from buffer whose
30122 position is between START_CHARPOS and END_CHARPOS. */
30123 for (; glyph < end
30124 && !NILP (glyph->object)
30125 && !EQ (glyph->object, disp_string)
30126 && !(BUFFERP (glyph->object)
30127 && (glyph->charpos >= start_charpos
30128 && glyph->charpos < end_charpos));
30129 ++glyph)
30131 /* BEFORE_STRING or AFTER_STRING are only relevant if they
30132 are present at buffer positions between START_CHARPOS and
30133 END_CHARPOS, or if they come from an overlay. */
30134 if (EQ (glyph->object, before_string))
30136 pos = string_buffer_position (before_string,
30137 start_charpos);
30138 /* If pos == 0, it means before_string came from an
30139 overlay, not from a buffer position. */
30140 if (!pos || (pos >= start_charpos && pos < end_charpos))
30141 break;
30143 else if (EQ (glyph->object, after_string))
30145 pos = string_buffer_position (after_string, end_charpos);
30146 if (!pos || (pos >= start_charpos && pos < end_charpos))
30147 break;
30149 x += glyph->pixel_width;
30151 hlinfo->mouse_face_beg_x = x;
30152 hlinfo->mouse_face_beg_col = glyph - r1->glyphs[TEXT_AREA];
30154 else
30156 /* This row is in a right to left paragraph. Scan it right to
30157 left. */
30158 struct glyph *g;
30160 end = r1->glyphs[TEXT_AREA] - 1;
30161 glyph = end + r1->used[TEXT_AREA];
30163 /* Skip truncation glyphs at the start of the glyph row. */
30164 if (MATRIX_ROW_DISPLAYS_TEXT_P (r1))
30165 for (; glyph > end
30166 && NILP (glyph->object)
30167 && glyph->charpos < 0;
30168 --glyph)
30171 /* Scan the glyph row, looking for BEFORE_STRING, AFTER_STRING,
30172 or DISP_STRING, and the first glyph from buffer whose
30173 position is between START_CHARPOS and END_CHARPOS. */
30174 for (; glyph > end
30175 && !NILP (glyph->object)
30176 && !EQ (glyph->object, disp_string)
30177 && !(BUFFERP (glyph->object)
30178 && (glyph->charpos >= start_charpos
30179 && glyph->charpos < end_charpos));
30180 --glyph)
30182 /* BEFORE_STRING or AFTER_STRING are only relevant if they
30183 are present at buffer positions between START_CHARPOS and
30184 END_CHARPOS, or if they come from an overlay. */
30185 if (EQ (glyph->object, before_string))
30187 pos = string_buffer_position (before_string, start_charpos);
30188 /* If pos == 0, it means before_string came from an
30189 overlay, not from a buffer position. */
30190 if (!pos || (pos >= start_charpos && pos < end_charpos))
30191 break;
30193 else if (EQ (glyph->object, after_string))
30195 pos = string_buffer_position (after_string, end_charpos);
30196 if (!pos || (pos >= start_charpos && pos < end_charpos))
30197 break;
30201 glyph++; /* first glyph to the right of the highlighted area */
30202 for (g = r1->glyphs[TEXT_AREA], x = r1->x; g < glyph; g++)
30203 x += g->pixel_width;
30204 hlinfo->mouse_face_beg_x = x;
30205 hlinfo->mouse_face_beg_col = glyph - r1->glyphs[TEXT_AREA];
30208 /* If the highlight ends in a different row, compute GLYPH and END
30209 for the end row. Otherwise, reuse the values computed above for
30210 the row where the highlight begins. */
30211 if (r2 != r1)
30213 if (!r2->reversed_p)
30215 glyph = r2->glyphs[TEXT_AREA];
30216 end = glyph + r2->used[TEXT_AREA];
30217 x = r2->x;
30219 else
30221 end = r2->glyphs[TEXT_AREA] - 1;
30222 glyph = end + r2->used[TEXT_AREA];
30226 if (!r2->reversed_p)
30228 /* Skip truncation and continuation glyphs near the end of the
30229 row, and also blanks and stretch glyphs inserted by
30230 extend_face_to_end_of_line. */
30231 while (end > glyph
30232 && NILP ((end - 1)->object))
30233 --end;
30234 /* Scan the rest of the glyph row from the end, looking for the
30235 first glyph that comes from BEFORE_STRING, AFTER_STRING, or
30236 DISP_STRING, or whose position is between START_CHARPOS
30237 and END_CHARPOS */
30238 for (--end;
30239 end > glyph
30240 && !NILP (end->object)
30241 && !EQ (end->object, disp_string)
30242 && !(BUFFERP (end->object)
30243 && (end->charpos >= start_charpos
30244 && end->charpos < end_charpos));
30245 --end)
30247 /* BEFORE_STRING or AFTER_STRING are only relevant if they
30248 are present at buffer positions between START_CHARPOS and
30249 END_CHARPOS, or if they come from an overlay. */
30250 if (EQ (end->object, before_string))
30252 pos = string_buffer_position (before_string, start_charpos);
30253 if (!pos || (pos >= start_charpos && pos < end_charpos))
30254 break;
30256 else if (EQ (end->object, after_string))
30258 pos = string_buffer_position (after_string, end_charpos);
30259 if (!pos || (pos >= start_charpos && pos < end_charpos))
30260 break;
30263 /* Find the X coordinate of the last glyph to be highlighted. */
30264 for (; glyph <= end; ++glyph)
30265 x += glyph->pixel_width;
30267 hlinfo->mouse_face_end_x = x;
30268 hlinfo->mouse_face_end_col = glyph - r2->glyphs[TEXT_AREA];
30270 else
30272 /* Skip truncation and continuation glyphs near the end of the
30273 row, and also blanks and stretch glyphs inserted by
30274 extend_face_to_end_of_line. */
30275 x = r2->x;
30276 end++;
30277 while (end < glyph
30278 && NILP (end->object))
30280 x += end->pixel_width;
30281 ++end;
30283 /* Scan the rest of the glyph row from the end, looking for the
30284 first glyph that comes from BEFORE_STRING, AFTER_STRING, or
30285 DISP_STRING, or whose position is between START_CHARPOS
30286 and END_CHARPOS */
30287 for ( ;
30288 end < glyph
30289 && !NILP (end->object)
30290 && !EQ (end->object, disp_string)
30291 && !(BUFFERP (end->object)
30292 && (end->charpos >= start_charpos
30293 && end->charpos < end_charpos));
30294 ++end)
30296 /* BEFORE_STRING or AFTER_STRING are only relevant if they
30297 are present at buffer positions between START_CHARPOS and
30298 END_CHARPOS, or if they come from an overlay. */
30299 if (EQ (end->object, before_string))
30301 pos = string_buffer_position (before_string, start_charpos);
30302 if (!pos || (pos >= start_charpos && pos < end_charpos))
30303 break;
30305 else if (EQ (end->object, after_string))
30307 pos = string_buffer_position (after_string, end_charpos);
30308 if (!pos || (pos >= start_charpos && pos < end_charpos))
30309 break;
30311 x += end->pixel_width;
30313 /* If we exited the above loop because we arrived at the last
30314 glyph of the row, and its buffer position is still not in
30315 range, it means the last character in range is the preceding
30316 newline. Bump the end column and x values to get past the
30317 last glyph. */
30318 if (end == glyph
30319 && BUFFERP (end->object)
30320 && (end->charpos < start_charpos
30321 || end->charpos >= end_charpos))
30323 x += end->pixel_width;
30324 ++end;
30326 hlinfo->mouse_face_end_x = x;
30327 hlinfo->mouse_face_end_col = end - r2->glyphs[TEXT_AREA];
30330 hlinfo->mouse_face_window = window;
30331 hlinfo->mouse_face_face_id
30332 = face_at_buffer_position (w, mouse_charpos, &ignore,
30333 mouse_charpos + 1,
30334 !hlinfo->mouse_face_hidden, -1);
30335 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
30338 /* The following function is not used anymore (replaced with
30339 mouse_face_from_string_pos), but I leave it here for the time
30340 being, in case someone would. */
30342 #if false /* not used */
30344 /* Find the position of the glyph for position POS in OBJECT in
30345 window W's current matrix, and return in *X, *Y the pixel
30346 coordinates, and return in *HPOS, *VPOS the column/row of the glyph.
30348 RIGHT_P means return the position of the right edge of the glyph.
30349 !RIGHT_P means return the left edge position.
30351 If no glyph for POS exists in the matrix, return the position of
30352 the glyph with the next smaller position that is in the matrix, if
30353 RIGHT_P is false. If RIGHT_P, and no glyph for POS
30354 exists in the matrix, return the position of the glyph with the
30355 next larger position in OBJECT.
30357 Value is true if a glyph was found. */
30359 static bool
30360 fast_find_string_pos (struct window *w, ptrdiff_t pos, Lisp_Object object,
30361 int *hpos, int *vpos, int *x, int *y, bool right_p)
30363 int yb = window_text_bottom_y (w);
30364 struct glyph_row *r;
30365 struct glyph *best_glyph = NULL;
30366 struct glyph_row *best_row = NULL;
30367 int best_x = 0;
30369 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
30370 r->enabled_p && r->y < yb;
30371 ++r)
30373 struct glyph *g = r->glyphs[TEXT_AREA];
30374 struct glyph *e = g + r->used[TEXT_AREA];
30375 int gx;
30377 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
30378 if (EQ (g->object, object))
30380 if (g->charpos == pos)
30382 best_glyph = g;
30383 best_x = gx;
30384 best_row = r;
30385 goto found;
30387 else if (best_glyph == NULL
30388 || ((eabs (g->charpos - pos)
30389 < eabs (best_glyph->charpos - pos))
30390 && (right_p
30391 ? g->charpos < pos
30392 : g->charpos > pos)))
30394 best_glyph = g;
30395 best_x = gx;
30396 best_row = r;
30401 found:
30403 if (best_glyph)
30405 *x = best_x;
30406 *hpos = best_glyph - best_row->glyphs[TEXT_AREA];
30408 if (right_p)
30410 *x += best_glyph->pixel_width;
30411 ++*hpos;
30414 *y = best_row->y;
30415 *vpos = MATRIX_ROW_VPOS (best_row, w->current_matrix);
30418 return best_glyph != NULL;
30420 #endif /* not used */
30422 /* Find the positions of the first and the last glyphs in window W's
30423 current matrix that occlude positions [STARTPOS..ENDPOS) in OBJECT
30424 (assumed to be a string), and return in HLINFO's mouse_face_*
30425 members the pixel and column/row coordinates of those glyphs. */
30427 static void
30428 mouse_face_from_string_pos (struct window *w, Mouse_HLInfo *hlinfo,
30429 Lisp_Object object,
30430 ptrdiff_t startpos, ptrdiff_t endpos)
30432 int yb = window_text_bottom_y (w);
30433 struct glyph_row *r;
30434 struct glyph *g, *e;
30435 int gx;
30436 bool found = false;
30438 /* Find the glyph row with at least one position in the range
30439 [STARTPOS..ENDPOS), and the first glyph in that row whose
30440 position belongs to that range. */
30441 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
30442 r->enabled_p && r->y < yb;
30443 ++r)
30445 if (!r->reversed_p)
30447 g = r->glyphs[TEXT_AREA];
30448 e = g + r->used[TEXT_AREA];
30449 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
30450 if (EQ (g->object, object)
30451 && startpos <= g->charpos && g->charpos < endpos)
30453 hlinfo->mouse_face_beg_row
30454 = MATRIX_ROW_VPOS (r, w->current_matrix);
30455 hlinfo->mouse_face_beg_col = g - r->glyphs[TEXT_AREA];
30456 hlinfo->mouse_face_beg_x = gx;
30457 found = true;
30458 break;
30461 else
30463 struct glyph *g1;
30465 e = r->glyphs[TEXT_AREA];
30466 g = e + r->used[TEXT_AREA];
30467 for ( ; g > e; --g)
30468 if (EQ ((g-1)->object, object)
30469 && startpos <= (g-1)->charpos && (g-1)->charpos < endpos)
30471 hlinfo->mouse_face_beg_row
30472 = MATRIX_ROW_VPOS (r, w->current_matrix);
30473 hlinfo->mouse_face_beg_col = g - r->glyphs[TEXT_AREA];
30474 for (gx = r->x, g1 = r->glyphs[TEXT_AREA]; g1 < g; ++g1)
30475 gx += g1->pixel_width;
30476 hlinfo->mouse_face_beg_x = gx;
30477 found = true;
30478 break;
30481 if (found)
30482 break;
30485 if (!found)
30486 return;
30488 /* Starting with the next row, look for the first row which does NOT
30489 include any glyphs whose positions are in the range. */
30490 for (++r; r->enabled_p && r->y < yb; ++r)
30492 g = r->glyphs[TEXT_AREA];
30493 e = g + r->used[TEXT_AREA];
30494 found = false;
30495 for ( ; g < e; ++g)
30496 if (EQ (g->object, object)
30497 && startpos <= g->charpos && g->charpos < endpos)
30499 found = true;
30500 break;
30502 if (!found)
30503 break;
30506 /* The highlighted region ends on the previous row. */
30507 r--;
30509 /* Set the end row. */
30510 hlinfo->mouse_face_end_row = MATRIX_ROW_VPOS (r, w->current_matrix);
30512 /* Compute and set the end column and the end column's horizontal
30513 pixel coordinate. */
30514 if (!r->reversed_p)
30516 g = r->glyphs[TEXT_AREA];
30517 e = g + r->used[TEXT_AREA];
30518 for ( ; e > g; --e)
30519 if (EQ ((e-1)->object, object)
30520 && startpos <= (e-1)->charpos && (e-1)->charpos < endpos)
30521 break;
30522 hlinfo->mouse_face_end_col = e - g;
30524 for (gx = r->x; g < e; ++g)
30525 gx += g->pixel_width;
30526 hlinfo->mouse_face_end_x = gx;
30528 else
30530 e = r->glyphs[TEXT_AREA];
30531 g = e + r->used[TEXT_AREA];
30532 for (gx = r->x ; e < g; ++e)
30534 if (EQ (e->object, object)
30535 && startpos <= e->charpos && e->charpos < endpos)
30536 break;
30537 gx += e->pixel_width;
30539 hlinfo->mouse_face_end_col = e - r->glyphs[TEXT_AREA];
30540 hlinfo->mouse_face_end_x = gx;
30544 #ifdef HAVE_WINDOW_SYSTEM
30546 /* See if position X, Y is within a hot-spot of an image. */
30548 static bool
30549 on_hot_spot_p (Lisp_Object hot_spot, int x, int y)
30551 if (!CONSP (hot_spot))
30552 return false;
30554 if (EQ (XCAR (hot_spot), Qrect))
30556 /* CDR is (Top-Left . Bottom-Right) = ((x0 . y0) . (x1 . y1)) */
30557 Lisp_Object rect = XCDR (hot_spot);
30558 Lisp_Object tem;
30559 if (!CONSP (rect))
30560 return false;
30561 if (!CONSP (XCAR (rect)))
30562 return false;
30563 if (!CONSP (XCDR (rect)))
30564 return false;
30565 if (!(tem = XCAR (XCAR (rect)), INTEGERP (tem) && x >= XINT (tem)))
30566 return false;
30567 if (!(tem = XCDR (XCAR (rect)), INTEGERP (tem) && y >= XINT (tem)))
30568 return false;
30569 if (!(tem = XCAR (XCDR (rect)), INTEGERP (tem) && x <= XINT (tem)))
30570 return false;
30571 if (!(tem = XCDR (XCDR (rect)), INTEGERP (tem) && y <= XINT (tem)))
30572 return false;
30573 return true;
30575 else if (EQ (XCAR (hot_spot), Qcircle))
30577 /* CDR is (Center . Radius) = ((x0 . y0) . r) */
30578 Lisp_Object circ = XCDR (hot_spot);
30579 Lisp_Object lr, lx0, ly0;
30580 if (CONSP (circ)
30581 && CONSP (XCAR (circ))
30582 && (lr = XCDR (circ), NUMBERP (lr))
30583 && (lx0 = XCAR (XCAR (circ)), INTEGERP (lx0))
30584 && (ly0 = XCDR (XCAR (circ)), INTEGERP (ly0)))
30586 double r = XFLOATINT (lr);
30587 double dx = XINT (lx0) - x;
30588 double dy = XINT (ly0) - y;
30589 return (dx * dx + dy * dy <= r * r);
30592 else if (EQ (XCAR (hot_spot), Qpoly))
30594 /* CDR is [x0 y0 x1 y1 x2 y2 ...x(n-1) y(n-1)] */
30595 if (VECTORP (XCDR (hot_spot)))
30597 struct Lisp_Vector *v = XVECTOR (XCDR (hot_spot));
30598 Lisp_Object *poly = v->contents;
30599 ptrdiff_t n = v->header.size;
30600 ptrdiff_t i;
30601 bool inside = false;
30602 Lisp_Object lx, ly;
30603 int x0, y0;
30605 /* Need an even number of coordinates, and at least 3 edges. */
30606 if (n < 6 || n & 1)
30607 return false;
30609 /* Count edge segments intersecting line from (X,Y) to (X,infinity).
30610 If count is odd, we are inside polygon. Pixels on edges
30611 may or may not be included depending on actual geometry of the
30612 polygon. */
30613 if ((lx = poly[n-2], !INTEGERP (lx))
30614 || (ly = poly[n-1], !INTEGERP (lx)))
30615 return false;
30616 x0 = XINT (lx), y0 = XINT (ly);
30617 for (i = 0; i < n; i += 2)
30619 int x1 = x0, y1 = y0;
30620 if ((lx = poly[i], !INTEGERP (lx))
30621 || (ly = poly[i+1], !INTEGERP (ly)))
30622 return false;
30623 x0 = XINT (lx), y0 = XINT (ly);
30625 /* Does this segment cross the X line? */
30626 if (x0 >= x)
30628 if (x1 >= x)
30629 continue;
30631 else if (x1 < x)
30632 continue;
30633 if (y > y0 && y > y1)
30634 continue;
30635 if (y < y0 + ((y1 - y0) * (x - x0)) / (x1 - x0))
30636 inside = !inside;
30638 return inside;
30641 return false;
30644 Lisp_Object
30645 find_hot_spot (Lisp_Object map, int x, int y)
30647 while (CONSP (map))
30649 if (CONSP (XCAR (map))
30650 && on_hot_spot_p (XCAR (XCAR (map)), x, y))
30651 return XCAR (map);
30652 map = XCDR (map);
30655 return Qnil;
30658 DEFUN ("lookup-image-map", Flookup_image_map, Slookup_image_map,
30659 3, 3, 0,
30660 doc: /* Lookup in image map MAP coordinates X and Y.
30661 An image map is an alist where each element has the format (AREA ID PLIST).
30662 An AREA is specified as either a rectangle, a circle, or a polygon:
30663 A rectangle is a cons (rect . ((x0 . y0) . (x1 . y1))) specifying the
30664 pixel coordinates of the upper left and bottom right corners.
30665 A circle is a cons (circle . ((x0 . y0) . r)) specifying the center
30666 and the radius of the circle; r may be a float or integer.
30667 A polygon is a cons (poly . [x0 y0 x1 y1 ...]) where each pair in the
30668 vector describes one corner in the polygon.
30669 Returns the alist element for the first matching AREA in MAP. */)
30670 (Lisp_Object map, Lisp_Object x, Lisp_Object y)
30672 if (NILP (map))
30673 return Qnil;
30675 CHECK_NUMBER (x);
30676 CHECK_NUMBER (y);
30678 return find_hot_spot (map,
30679 clip_to_bounds (INT_MIN, XINT (x), INT_MAX),
30680 clip_to_bounds (INT_MIN, XINT (y), INT_MAX));
30682 #endif /* HAVE_WINDOW_SYSTEM */
30685 /* Display frame CURSOR, optionally using shape defined by POINTER. */
30686 static void
30687 define_frame_cursor1 (struct frame *f, Cursor cursor, Lisp_Object pointer)
30689 #ifdef HAVE_WINDOW_SYSTEM
30690 if (!FRAME_WINDOW_P (f))
30691 return;
30693 /* Do not change cursor shape while dragging mouse. */
30694 if (EQ (do_mouse_tracking, Qdragging))
30695 return;
30697 if (!NILP (pointer))
30699 if (EQ (pointer, Qarrow))
30700 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
30701 else if (EQ (pointer, Qhand))
30702 cursor = FRAME_X_OUTPUT (f)->hand_cursor;
30703 else if (EQ (pointer, Qtext))
30704 cursor = FRAME_X_OUTPUT (f)->text_cursor;
30705 else if (EQ (pointer, intern ("hdrag")))
30706 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
30707 else if (EQ (pointer, intern ("nhdrag")))
30708 cursor = FRAME_X_OUTPUT (f)->vertical_drag_cursor;
30709 # ifdef HAVE_X_WINDOWS
30710 else if (EQ (pointer, intern ("vdrag")))
30711 cursor = FRAME_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
30712 # endif
30713 else if (EQ (pointer, intern ("hourglass")))
30714 cursor = FRAME_X_OUTPUT (f)->hourglass_cursor;
30715 else if (EQ (pointer, Qmodeline))
30716 cursor = FRAME_X_OUTPUT (f)->modeline_cursor;
30717 else
30718 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
30721 if (cursor != No_Cursor)
30722 FRAME_RIF (f)->define_frame_cursor (f, cursor);
30723 #endif
30726 /* Take proper action when mouse has moved to the mode or header line
30727 or marginal area AREA of window W, x-position X and y-position Y.
30728 X is relative to the start of the text display area of W, so the
30729 width of bitmap areas and scroll bars must be subtracted to get a
30730 position relative to the start of the mode line. */
30732 static void
30733 note_mode_line_or_margin_highlight (Lisp_Object window, int x, int y,
30734 enum window_part area)
30736 struct window *w = XWINDOW (window);
30737 struct frame *f = XFRAME (w->frame);
30738 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
30739 #ifdef HAVE_WINDOW_SYSTEM
30740 Display_Info *dpyinfo;
30741 #endif
30742 Cursor cursor = No_Cursor;
30743 Lisp_Object pointer = Qnil;
30744 int dx, dy, width, height;
30745 ptrdiff_t charpos;
30746 Lisp_Object string, object = Qnil;
30747 Lisp_Object pos UNINIT;
30748 Lisp_Object mouse_face;
30749 int original_x_pixel = x;
30750 struct glyph * glyph = NULL, * row_start_glyph = NULL;
30751 struct glyph_row *row UNINIT;
30753 if (area == ON_MODE_LINE || area == ON_HEADER_LINE)
30755 int x0;
30756 struct glyph *end;
30758 /* Kludge alert: mode_line_string takes X/Y in pixels, but
30759 returns them in row/column units! */
30760 string = mode_line_string (w, area, &x, &y, &charpos,
30761 &object, &dx, &dy, &width, &height);
30763 row = (area == ON_MODE_LINE
30764 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
30765 : MATRIX_HEADER_LINE_ROW (w->current_matrix));
30767 /* Find the glyph under the mouse pointer. */
30768 if (row->mode_line_p && row->enabled_p)
30770 glyph = row_start_glyph = row->glyphs[TEXT_AREA];
30771 end = glyph + row->used[TEXT_AREA];
30773 for (x0 = original_x_pixel;
30774 glyph < end && x0 >= glyph->pixel_width;
30775 ++glyph)
30776 x0 -= glyph->pixel_width;
30778 if (glyph >= end)
30779 glyph = NULL;
30782 else
30784 x -= WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
30785 /* Kludge alert: marginal_area_string takes X/Y in pixels, but
30786 returns them in row/column units! */
30787 string = marginal_area_string (w, area, &x, &y, &charpos,
30788 &object, &dx, &dy, &width, &height);
30791 Lisp_Object help = Qnil;
30793 #ifdef HAVE_WINDOW_SYSTEM
30794 if (IMAGEP (object))
30796 Lisp_Object image_map, hotspot;
30797 if ((image_map = Fplist_get (XCDR (object), QCmap),
30798 !NILP (image_map))
30799 && (hotspot = find_hot_spot (image_map, dx, dy),
30800 CONSP (hotspot))
30801 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
30803 Lisp_Object plist;
30805 /* Could check XCAR (hotspot) to see if we enter/leave this hot-spot.
30806 If so, we could look for mouse-enter, mouse-leave
30807 properties in PLIST (and do something...). */
30808 hotspot = XCDR (hotspot);
30809 if (CONSP (hotspot)
30810 && (plist = XCAR (hotspot), CONSP (plist)))
30812 pointer = Fplist_get (plist, Qpointer);
30813 if (NILP (pointer))
30814 pointer = Qhand;
30815 help = Fplist_get (plist, Qhelp_echo);
30816 if (!NILP (help))
30818 help_echo_string = help;
30819 XSETWINDOW (help_echo_window, w);
30820 help_echo_object = w->contents;
30821 help_echo_pos = charpos;
30825 if (NILP (pointer))
30826 pointer = Fplist_get (XCDR (object), QCpointer);
30828 #endif /* HAVE_WINDOW_SYSTEM */
30830 if (STRINGP (string))
30831 pos = make_number (charpos);
30833 /* Set the help text and mouse pointer. If the mouse is on a part
30834 of the mode line without any text (e.g. past the right edge of
30835 the mode line text), use the default help text and pointer. */
30836 if (STRINGP (string) || area == ON_MODE_LINE)
30838 /* Arrange to display the help by setting the global variables
30839 help_echo_string, help_echo_object, and help_echo_pos. */
30840 if (NILP (help))
30842 if (STRINGP (string))
30843 help = Fget_text_property (pos, Qhelp_echo, string);
30845 if (!NILP (help))
30847 help_echo_string = help;
30848 XSETWINDOW (help_echo_window, w);
30849 help_echo_object = string;
30850 help_echo_pos = charpos;
30852 else if (area == ON_MODE_LINE)
30854 Lisp_Object default_help
30855 = buffer_local_value (Qmode_line_default_help_echo,
30856 w->contents);
30858 if (STRINGP (default_help))
30860 help_echo_string = default_help;
30861 XSETWINDOW (help_echo_window, w);
30862 help_echo_object = Qnil;
30863 help_echo_pos = -1;
30868 #ifdef HAVE_WINDOW_SYSTEM
30869 /* Change the mouse pointer according to what is under it. */
30870 if (FRAME_WINDOW_P (f))
30872 bool draggable = (! WINDOW_BOTTOMMOST_P (w)
30873 || minibuf_level
30874 || NILP (Vresize_mini_windows));
30876 dpyinfo = FRAME_DISPLAY_INFO (f);
30877 if (STRINGP (string))
30879 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
30881 if (NILP (pointer))
30882 pointer = Fget_text_property (pos, Qpointer, string);
30884 /* Change the mouse pointer according to what is under X/Y. */
30885 if (NILP (pointer)
30886 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE)))
30888 Lisp_Object map;
30889 map = Fget_text_property (pos, Qlocal_map, string);
30890 if (!KEYMAPP (map))
30891 map = Fget_text_property (pos, Qkeymap, string);
30892 if (!KEYMAPP (map) && draggable)
30893 cursor = dpyinfo->vertical_scroll_bar_cursor;
30896 else if (draggable)
30897 /* Default mode-line pointer. */
30898 cursor = FRAME_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
30900 #endif
30903 /* Change the mouse face according to what is under X/Y. */
30904 bool mouse_face_shown = false;
30905 if (STRINGP (string))
30907 mouse_face = Fget_text_property (pos, Qmouse_face, string);
30908 if (!NILP (Vmouse_highlight) && !NILP (mouse_face)
30909 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
30910 && glyph)
30912 Lisp_Object b, e;
30914 struct glyph * tmp_glyph;
30916 int gpos;
30917 int gseq_length;
30918 int total_pixel_width;
30919 ptrdiff_t begpos, endpos, ignore;
30921 int vpos, hpos;
30923 b = Fprevious_single_property_change (make_number (charpos + 1),
30924 Qmouse_face, string, Qnil);
30925 if (NILP (b))
30926 begpos = 0;
30927 else
30928 begpos = XINT (b);
30930 e = Fnext_single_property_change (pos, Qmouse_face, string, Qnil);
30931 if (NILP (e))
30932 endpos = SCHARS (string);
30933 else
30934 endpos = XINT (e);
30936 /* Calculate the glyph position GPOS of GLYPH in the
30937 displayed string, relative to the beginning of the
30938 highlighted part of the string.
30940 Note: GPOS is different from CHARPOS. CHARPOS is the
30941 position of GLYPH in the internal string object. A mode
30942 line string format has structures which are converted to
30943 a flattened string by the Emacs Lisp interpreter. The
30944 internal string is an element of those structures. The
30945 displayed string is the flattened string. */
30946 tmp_glyph = row_start_glyph;
30947 while (tmp_glyph < glyph
30948 && (!(EQ (tmp_glyph->object, glyph->object)
30949 && begpos <= tmp_glyph->charpos
30950 && tmp_glyph->charpos < endpos)))
30951 tmp_glyph++;
30952 gpos = glyph - tmp_glyph;
30954 /* Calculate the length GSEQ_LENGTH of the glyph sequence of
30955 the highlighted part of the displayed string to which
30956 GLYPH belongs. Note: GSEQ_LENGTH is different from
30957 SCHARS (STRING), because the latter returns the length of
30958 the internal string. */
30959 for (tmp_glyph = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
30960 tmp_glyph > glyph
30961 && (!(EQ (tmp_glyph->object, glyph->object)
30962 && begpos <= tmp_glyph->charpos
30963 && tmp_glyph->charpos < endpos));
30964 tmp_glyph--)
30966 gseq_length = gpos + (tmp_glyph - glyph) + 1;
30968 /* Calculate the total pixel width of all the glyphs between
30969 the beginning of the highlighted area and GLYPH. */
30970 total_pixel_width = 0;
30971 for (tmp_glyph = glyph - gpos; tmp_glyph != glyph; tmp_glyph++)
30972 total_pixel_width += tmp_glyph->pixel_width;
30974 /* Pre calculation of re-rendering position. Note: X is in
30975 column units here, after the call to mode_line_string or
30976 marginal_area_string. */
30977 hpos = x - gpos;
30978 vpos = (area == ON_MODE_LINE
30979 ? (w->current_matrix)->nrows - 1
30980 : 0);
30982 /* If GLYPH's position is included in the region that is
30983 already drawn in mouse face, we have nothing to do. */
30984 if ( EQ (window, hlinfo->mouse_face_window)
30985 && (!row->reversed_p
30986 ? (hlinfo->mouse_face_beg_col <= hpos
30987 && hpos < hlinfo->mouse_face_end_col)
30988 /* In R2L rows we swap BEG and END, see below. */
30989 : (hlinfo->mouse_face_end_col <= hpos
30990 && hpos < hlinfo->mouse_face_beg_col))
30991 && hlinfo->mouse_face_beg_row == vpos )
30992 return;
30994 if (clear_mouse_face (hlinfo))
30995 cursor = No_Cursor;
30997 if (!row->reversed_p)
30999 hlinfo->mouse_face_beg_col = hpos;
31000 hlinfo->mouse_face_beg_x = original_x_pixel
31001 - (total_pixel_width + dx);
31002 hlinfo->mouse_face_end_col = hpos + gseq_length;
31003 hlinfo->mouse_face_end_x = 0;
31005 else
31007 /* In R2L rows, show_mouse_face expects BEG and END
31008 coordinates to be swapped. */
31009 hlinfo->mouse_face_end_col = hpos;
31010 hlinfo->mouse_face_end_x = original_x_pixel
31011 - (total_pixel_width + dx);
31012 hlinfo->mouse_face_beg_col = hpos + gseq_length;
31013 hlinfo->mouse_face_beg_x = 0;
31016 hlinfo->mouse_face_beg_row = vpos;
31017 hlinfo->mouse_face_end_row = hlinfo->mouse_face_beg_row;
31018 hlinfo->mouse_face_past_end = false;
31019 hlinfo->mouse_face_window = window;
31021 hlinfo->mouse_face_face_id = face_at_string_position (w, string,
31022 charpos,
31023 0, &ignore,
31024 glyph->face_id,
31025 true);
31026 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
31027 mouse_face_shown = true;
31029 if (NILP (pointer))
31030 pointer = Qhand;
31034 /* If mouse-face doesn't need to be shown, clear any existing
31035 mouse-face. */
31036 if ((area == ON_MODE_LINE || area == ON_HEADER_LINE) && !mouse_face_shown)
31037 clear_mouse_face (hlinfo);
31039 define_frame_cursor1 (f, cursor, pointer);
31043 /* EXPORT:
31044 Take proper action when the mouse has moved to position X, Y on
31045 frame F with regards to highlighting portions of display that have
31046 mouse-face properties. Also de-highlight portions of display where
31047 the mouse was before, set the mouse pointer shape as appropriate
31048 for the mouse coordinates, and activate help echo (tooltips).
31049 X and Y can be negative or out of range. */
31051 void
31052 note_mouse_highlight (struct frame *f, int x, int y)
31054 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
31055 enum window_part part = ON_NOTHING;
31056 Lisp_Object window;
31057 struct window *w;
31058 Cursor cursor = No_Cursor;
31059 Lisp_Object pointer = Qnil; /* Takes precedence over cursor! */
31060 struct buffer *b;
31062 /* When a menu is active, don't highlight because this looks odd. */
31063 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS) || defined (MSDOS)
31064 if (popup_activated ())
31065 return;
31066 #endif
31068 if (!f->glyphs_initialized_p
31069 || f->pointer_invisible)
31070 return;
31072 hlinfo->mouse_face_mouse_x = x;
31073 hlinfo->mouse_face_mouse_y = y;
31074 hlinfo->mouse_face_mouse_frame = f;
31076 if (hlinfo->mouse_face_defer)
31077 return;
31079 /* Which window is that in? */
31080 window = window_from_coordinates (f, x, y, &part, true);
31082 /* If displaying active text in another window, clear that. */
31083 if (! EQ (window, hlinfo->mouse_face_window)
31084 /* Also clear if we move out of text area in same window. */
31085 || (!NILP (hlinfo->mouse_face_window)
31086 && !NILP (window)
31087 && part != ON_TEXT
31088 && part != ON_MODE_LINE
31089 && part != ON_HEADER_LINE))
31090 clear_mouse_face (hlinfo);
31092 /* Reset help_echo_string. It will get recomputed below. */
31093 help_echo_string = Qnil;
31095 #ifdef HAVE_WINDOW_SYSTEM
31096 /* If the cursor is on the internal border of FRAME and FRAME's
31097 internal border is draggable, provide some visual feedback. */
31098 if (FRAME_INTERNAL_BORDER_WIDTH (f) > 0
31099 && !NILP (get_frame_param (f, Qdrag_internal_border)))
31101 enum internal_border_part part = frame_internal_border_part (f, x, y);
31103 switch (part)
31105 case INTERNAL_BORDER_NONE:
31106 if (cursor != FRAME_X_OUTPUT (f)->nontext_cursor)
31107 /* Reset cursor. */
31108 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
31109 break;
31110 case INTERNAL_BORDER_LEFT_EDGE:
31111 cursor = FRAME_X_OUTPUT (f)->left_edge_cursor;
31112 break;
31113 case INTERNAL_BORDER_TOP_LEFT_CORNER:
31114 cursor = FRAME_X_OUTPUT (f)->top_left_corner_cursor;
31115 break;
31116 case INTERNAL_BORDER_TOP_EDGE:
31117 cursor = FRAME_X_OUTPUT (f)->top_edge_cursor;
31118 break;
31119 case INTERNAL_BORDER_TOP_RIGHT_CORNER:
31120 cursor = FRAME_X_OUTPUT (f)->top_right_corner_cursor;
31121 break;
31122 case INTERNAL_BORDER_RIGHT_EDGE:
31123 cursor = FRAME_X_OUTPUT (f)->right_edge_cursor;
31124 break;
31125 case INTERNAL_BORDER_BOTTOM_RIGHT_CORNER:
31126 cursor = FRAME_X_OUTPUT (f)->bottom_right_corner_cursor;
31127 break;
31128 case INTERNAL_BORDER_BOTTOM_EDGE:
31129 cursor = FRAME_X_OUTPUT (f)->bottom_edge_cursor;
31130 break;
31131 case INTERNAL_BORDER_BOTTOM_LEFT_CORNER:
31132 cursor = FRAME_X_OUTPUT (f)->bottom_left_corner_cursor;
31133 break;
31134 default:
31135 /* This should not happen. */
31136 if (cursor != FRAME_X_OUTPUT (f)->nontext_cursor)
31137 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
31140 if (cursor != FRAME_X_OUTPUT (f)->nontext_cursor)
31142 /* Do we really want a help echo here? */
31143 help_echo_string = build_string ("drag-mouse-1: resize frame");
31144 goto set_cursor;
31147 #endif /* HAVE_WINDOW_SYSTEM */
31149 /* Not on a window -> return. */
31150 if (!WINDOWP (window))
31151 return;
31153 /* Convert to window-relative pixel coordinates. */
31154 w = XWINDOW (window);
31155 frame_to_window_pixel_xy (w, &x, &y);
31157 #if defined (HAVE_WINDOW_SYSTEM) && ! defined (USE_GTK) && ! defined (HAVE_NS)
31158 /* Handle tool-bar window differently since it doesn't display a
31159 buffer. */
31160 if (EQ (window, f->tool_bar_window))
31162 note_tool_bar_highlight (f, x, y);
31163 return;
31165 #endif
31167 /* Mouse is on the mode, header line or margin? */
31168 if (part == ON_MODE_LINE || part == ON_HEADER_LINE
31169 || part == ON_LEFT_MARGIN || part == ON_RIGHT_MARGIN)
31171 note_mode_line_or_margin_highlight (window, x, y, part);
31173 #ifdef HAVE_WINDOW_SYSTEM
31174 if (part == ON_LEFT_MARGIN || part == ON_RIGHT_MARGIN)
31176 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
31177 /* Show non-text cursor (Bug#16647). */
31178 goto set_cursor;
31180 else
31181 #endif
31182 return;
31185 #ifdef HAVE_WINDOW_SYSTEM
31186 if (part == ON_VERTICAL_BORDER)
31188 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
31189 help_echo_string = build_string ("drag-mouse-1: resize");
31190 goto set_cursor;
31192 else if (part == ON_RIGHT_DIVIDER)
31194 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
31195 help_echo_string = build_string ("drag-mouse-1: resize");
31196 goto set_cursor;
31198 else if (part == ON_BOTTOM_DIVIDER)
31199 if (! WINDOW_BOTTOMMOST_P (w)
31200 || minibuf_level
31201 || NILP (Vresize_mini_windows))
31203 cursor = FRAME_X_OUTPUT (f)->vertical_drag_cursor;
31204 help_echo_string = build_string ("drag-mouse-1: resize");
31205 goto set_cursor;
31207 else
31208 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
31209 else if (part == ON_LEFT_FRINGE || part == ON_RIGHT_FRINGE
31210 || part == ON_VERTICAL_SCROLL_BAR
31211 || part == ON_HORIZONTAL_SCROLL_BAR)
31212 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
31213 else
31214 cursor = FRAME_X_OUTPUT (f)->text_cursor;
31215 #endif
31217 /* Are we in a window whose display is up to date?
31218 And verify the buffer's text has not changed. */
31219 b = XBUFFER (w->contents);
31220 if (part == ON_TEXT && w->window_end_valid && !window_outdated (w))
31222 int hpos, vpos, dx, dy, area = LAST_AREA;
31223 ptrdiff_t pos;
31224 struct glyph *glyph;
31225 Lisp_Object object;
31226 Lisp_Object mouse_face = Qnil, position;
31227 Lisp_Object *overlay_vec = NULL;
31228 ptrdiff_t i, noverlays;
31229 struct buffer *obuf;
31230 ptrdiff_t obegv, ozv;
31231 bool same_region;
31233 /* Find the glyph under X/Y. */
31234 glyph = x_y_to_hpos_vpos (w, x, y, &hpos, &vpos, &dx, &dy, &area);
31236 #ifdef HAVE_WINDOW_SYSTEM
31237 /* Look for :pointer property on image. */
31238 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
31240 struct image *img = IMAGE_OPT_FROM_ID (f, glyph->u.img_id);
31241 if (img != NULL && IMAGEP (img->spec))
31243 Lisp_Object image_map, hotspot;
31244 if ((image_map = Fplist_get (XCDR (img->spec), QCmap),
31245 !NILP (image_map))
31246 && (hotspot = find_hot_spot (image_map,
31247 glyph->slice.img.x + dx,
31248 glyph->slice.img.y + dy),
31249 CONSP (hotspot))
31250 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
31252 Lisp_Object plist;
31254 /* Could check XCAR (hotspot) to see if we enter/leave
31255 this hot-spot.
31256 If so, we could look for mouse-enter, mouse-leave
31257 properties in PLIST (and do something...). */
31258 hotspot = XCDR (hotspot);
31259 if (CONSP (hotspot)
31260 && (plist = XCAR (hotspot), CONSP (plist)))
31262 pointer = Fplist_get (plist, Qpointer);
31263 if (NILP (pointer))
31264 pointer = Qhand;
31265 help_echo_string = Fplist_get (plist, Qhelp_echo);
31266 if (!NILP (help_echo_string))
31268 help_echo_window = window;
31269 help_echo_object = glyph->object;
31270 help_echo_pos = glyph->charpos;
31274 if (NILP (pointer))
31275 pointer = Fplist_get (XCDR (img->spec), QCpointer);
31278 #endif /* HAVE_WINDOW_SYSTEM */
31280 /* Clear mouse face if X/Y not over text. */
31281 if (glyph == NULL
31282 || area != TEXT_AREA
31283 || !MATRIX_ROW_DISPLAYS_TEXT_P (MATRIX_ROW (w->current_matrix, vpos))
31284 /* Glyph's OBJECT is nil for glyphs inserted by the
31285 display engine for its internal purposes, like truncation
31286 and continuation glyphs and blanks beyond the end of
31287 line's text on text terminals. If we are over such a
31288 glyph, we are not over any text. */
31289 || NILP (glyph->object)
31290 /* R2L rows have a stretch glyph at their front, which
31291 stands for no text, whereas L2R rows have no glyphs at
31292 all beyond the end of text. Treat such stretch glyphs
31293 like we do with NULL glyphs in L2R rows. */
31294 || (MATRIX_ROW (w->current_matrix, vpos)->reversed_p
31295 && glyph == MATRIX_ROW_GLYPH_START (w->current_matrix, vpos)
31296 && glyph->type == STRETCH_GLYPH
31297 && glyph->avoid_cursor_p))
31299 if (clear_mouse_face (hlinfo))
31300 cursor = No_Cursor;
31301 if (FRAME_WINDOW_P (f) && NILP (pointer))
31303 #ifdef HAVE_WINDOW_SYSTEM
31304 if (area != TEXT_AREA)
31305 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
31306 else
31307 pointer = Vvoid_text_area_pointer;
31308 #endif
31310 goto set_cursor;
31313 pos = glyph->charpos;
31314 object = glyph->object;
31315 if (!STRINGP (object) && !BUFFERP (object))
31316 goto set_cursor;
31318 /* If we get an out-of-range value, return now; avoid an error. */
31319 if (BUFFERP (object) && pos > BUF_Z (b))
31320 goto set_cursor;
31322 /* Make the window's buffer temporarily current for
31323 overlays_at and compute_char_face. */
31324 obuf = current_buffer;
31325 current_buffer = b;
31326 obegv = BEGV;
31327 ozv = ZV;
31328 BEGV = BEG;
31329 ZV = Z;
31331 /* Is this char mouse-active or does it have help-echo? */
31332 position = make_number (pos);
31334 USE_SAFE_ALLOCA;
31336 if (BUFFERP (object))
31338 /* Put all the overlays we want in a vector in overlay_vec. */
31339 GET_OVERLAYS_AT (pos, overlay_vec, noverlays, NULL, false);
31340 /* Sort overlays into increasing priority order. */
31341 noverlays = sort_overlays (overlay_vec, noverlays, w);
31343 else
31344 noverlays = 0;
31346 if (NILP (Vmouse_highlight))
31348 clear_mouse_face (hlinfo);
31349 goto check_help_echo;
31352 same_region = coords_in_mouse_face_p (w, hpos, vpos);
31354 if (same_region)
31355 cursor = No_Cursor;
31357 /* Check mouse-face highlighting. */
31358 if (! same_region
31359 /* If there exists an overlay with mouse-face overlapping
31360 the one we are currently highlighting, we have to check
31361 if we enter the overlapping overlay, and then highlight
31362 only that. Skip the check when mouse-face highlighting
31363 is currently hidden to avoid Bug#30519. */
31364 || (!hlinfo->mouse_face_hidden
31365 && OVERLAYP (hlinfo->mouse_face_overlay)
31366 && mouse_face_overlay_overlaps (hlinfo->mouse_face_overlay)))
31368 /* Find the highest priority overlay with a mouse-face. */
31369 Lisp_Object overlay = Qnil;
31370 for (i = noverlays - 1; i >= 0 && NILP (overlay); --i)
31372 mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
31373 if (!NILP (mouse_face))
31374 overlay = overlay_vec[i];
31377 /* If we're highlighting the same overlay as before, there's
31378 no need to do that again. */
31379 if (!NILP (overlay) && EQ (overlay, hlinfo->mouse_face_overlay))
31380 goto check_help_echo;
31382 /* Clear the display of the old active region, if any. */
31383 if (clear_mouse_face (hlinfo))
31384 cursor = No_Cursor;
31386 /* Record the overlay, if any, to be highlighted. */
31387 hlinfo->mouse_face_overlay = overlay;
31389 /* If no overlay applies, get a text property. */
31390 if (NILP (overlay))
31391 mouse_face = Fget_text_property (position, Qmouse_face, object);
31393 /* Next, compute the bounds of the mouse highlighting and
31394 display it. */
31395 if (!NILP (mouse_face) && STRINGP (object))
31397 /* The mouse-highlighting comes from a display string
31398 with a mouse-face. */
31399 Lisp_Object s, e;
31400 ptrdiff_t ignore;
31402 s = Fprevious_single_property_change
31403 (make_number (pos + 1), Qmouse_face, object, Qnil);
31404 e = Fnext_single_property_change
31405 (position, Qmouse_face, object, Qnil);
31406 if (NILP (s))
31407 s = make_number (0);
31408 if (NILP (e))
31409 e = make_number (SCHARS (object));
31410 mouse_face_from_string_pos (w, hlinfo, object,
31411 XINT (s), XINT (e));
31412 hlinfo->mouse_face_past_end = false;
31413 hlinfo->mouse_face_window = window;
31414 hlinfo->mouse_face_face_id
31415 = face_at_string_position (w, object, pos, 0, &ignore,
31416 glyph->face_id, true);
31417 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
31418 cursor = No_Cursor;
31420 else
31422 /* The mouse-highlighting, if any, comes from an overlay
31423 or text property in the buffer. */
31424 Lisp_Object buffer UNINIT;
31425 Lisp_Object disp_string UNINIT;
31427 if (STRINGP (object))
31429 /* If we are on a display string with no mouse-face,
31430 check if the text under it has one. */
31431 struct glyph_row *r = MATRIX_ROW (w->current_matrix, vpos);
31432 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
31433 pos = string_buffer_position (object, start);
31434 if (pos > 0)
31436 mouse_face = get_char_property_and_overlay
31437 (make_number (pos), Qmouse_face, w->contents, &overlay);
31438 buffer = w->contents;
31439 disp_string = object;
31442 else
31444 buffer = object;
31445 disp_string = Qnil;
31448 if (!NILP (mouse_face))
31450 Lisp_Object before, after;
31451 Lisp_Object before_string, after_string;
31452 /* To correctly find the limits of mouse highlight
31453 in a bidi-reordered buffer, we must not use the
31454 optimization of limiting the search in
31455 previous-single-property-change and
31456 next-single-property-change, because
31457 rows_from_pos_range needs the real start and end
31458 positions to DTRT in this case. That's because
31459 the first row visible in a window does not
31460 necessarily display the character whose position
31461 is the smallest. */
31462 Lisp_Object lim1
31463 = NILP (BVAR (XBUFFER (buffer), bidi_display_reordering))
31464 ? Fmarker_position (w->start)
31465 : Qnil;
31466 Lisp_Object lim2
31467 = NILP (BVAR (XBUFFER (buffer), bidi_display_reordering))
31468 ? make_number (BUF_Z (XBUFFER (buffer))
31469 - w->window_end_pos)
31470 : Qnil;
31472 if (NILP (overlay))
31474 /* Handle the text property case. */
31475 before = Fprevious_single_property_change
31476 (make_number (pos + 1), Qmouse_face, buffer, lim1);
31477 after = Fnext_single_property_change
31478 (make_number (pos), Qmouse_face, buffer, lim2);
31479 before_string = after_string = Qnil;
31481 else
31483 /* Handle the overlay case. */
31484 before = Foverlay_start (overlay);
31485 after = Foverlay_end (overlay);
31486 before_string = Foverlay_get (overlay, Qbefore_string);
31487 after_string = Foverlay_get (overlay, Qafter_string);
31489 if (!STRINGP (before_string)) before_string = Qnil;
31490 if (!STRINGP (after_string)) after_string = Qnil;
31493 mouse_face_from_buffer_pos (window, hlinfo, pos,
31494 NILP (before)
31496 : XFASTINT (before),
31497 NILP (after)
31498 ? BUF_Z (XBUFFER (buffer))
31499 : XFASTINT (after),
31500 before_string, after_string,
31501 disp_string);
31502 cursor = No_Cursor;
31507 check_help_echo:
31509 /* Look for a `help-echo' property. */
31510 if (NILP (help_echo_string)) {
31511 Lisp_Object help, overlay;
31513 /* Check overlays first. */
31514 help = overlay = Qnil;
31515 for (i = noverlays - 1; i >= 0 && NILP (help); --i)
31517 overlay = overlay_vec[i];
31518 help = Foverlay_get (overlay, Qhelp_echo);
31521 if (!NILP (help))
31523 help_echo_string = help;
31524 help_echo_window = window;
31525 help_echo_object = overlay;
31526 help_echo_pos = pos;
31528 else
31530 Lisp_Object obj = glyph->object;
31531 ptrdiff_t charpos = glyph->charpos;
31533 /* Try text properties. */
31534 if (STRINGP (obj)
31535 && charpos >= 0
31536 && charpos < SCHARS (obj))
31538 help = Fget_text_property (make_number (charpos),
31539 Qhelp_echo, obj);
31540 if (NILP (help))
31542 /* If the string itself doesn't specify a help-echo,
31543 see if the buffer text ``under'' it does. */
31544 struct glyph_row *r
31545 = MATRIX_ROW (w->current_matrix, vpos);
31546 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
31547 ptrdiff_t p = string_buffer_position (obj, start);
31548 if (p > 0)
31550 help = Fget_char_property (make_number (p),
31551 Qhelp_echo, w->contents);
31552 if (!NILP (help))
31554 charpos = p;
31555 obj = w->contents;
31560 else if (BUFFERP (obj)
31561 && charpos >= BEGV
31562 && charpos < ZV)
31563 help = Fget_text_property (make_number (charpos), Qhelp_echo,
31564 obj);
31566 if (!NILP (help))
31568 help_echo_string = help;
31569 help_echo_window = window;
31570 help_echo_object = obj;
31571 help_echo_pos = charpos;
31576 #ifdef HAVE_WINDOW_SYSTEM
31577 /* Look for a `pointer' property. */
31578 if (FRAME_WINDOW_P (f) && NILP (pointer))
31580 /* Check overlays first. */
31581 for (i = noverlays - 1; i >= 0 && NILP (pointer); --i)
31582 pointer = Foverlay_get (overlay_vec[i], Qpointer);
31584 if (NILP (pointer))
31586 Lisp_Object obj = glyph->object;
31587 ptrdiff_t charpos = glyph->charpos;
31589 /* Try text properties. */
31590 if (STRINGP (obj)
31591 && charpos >= 0
31592 && charpos < SCHARS (obj))
31594 pointer = Fget_text_property (make_number (charpos),
31595 Qpointer, obj);
31596 if (NILP (pointer))
31598 /* If the string itself doesn't specify a pointer,
31599 see if the buffer text ``under'' it does. */
31600 struct glyph_row *r
31601 = MATRIX_ROW (w->current_matrix, vpos);
31602 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
31603 ptrdiff_t p = string_buffer_position (obj, start);
31604 if (p > 0)
31605 pointer = Fget_char_property (make_number (p),
31606 Qpointer, w->contents);
31609 else if (BUFFERP (obj)
31610 && charpos >= BEGV
31611 && charpos < ZV)
31612 pointer = Fget_text_property (make_number (charpos),
31613 Qpointer, obj);
31616 #endif /* HAVE_WINDOW_SYSTEM */
31618 BEGV = obegv;
31619 ZV = ozv;
31620 current_buffer = obuf;
31621 SAFE_FREE ();
31624 set_cursor:
31625 define_frame_cursor1 (f, cursor, pointer);
31629 /* EXPORT for RIF:
31630 Clear any mouse-face on window W. This function is part of the
31631 redisplay interface, and is called from try_window_id and similar
31632 functions to ensure the mouse-highlight is off. */
31634 void
31635 x_clear_window_mouse_face (struct window *w)
31637 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (XFRAME (w->frame));
31638 Lisp_Object window;
31640 block_input ();
31641 XSETWINDOW (window, w);
31642 if (EQ (window, hlinfo->mouse_face_window))
31643 clear_mouse_face (hlinfo);
31644 unblock_input ();
31648 /* EXPORT:
31649 Just discard the mouse face information for frame F, if any.
31650 This is used when the size of F is changed. */
31652 void
31653 cancel_mouse_face (struct frame *f)
31655 Lisp_Object window;
31656 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
31658 window = hlinfo->mouse_face_window;
31659 if (! NILP (window) && XFRAME (XWINDOW (window)->frame) == f)
31660 reset_mouse_highlight (hlinfo);
31665 /***********************************************************************
31666 Exposure Events
31667 ***********************************************************************/
31669 #ifdef HAVE_WINDOW_SYSTEM
31671 /* Redraw the part of glyph row area AREA of glyph row ROW on window W
31672 which intersects rectangle R. R is in window-relative coordinates. */
31674 static void
31675 expose_area (struct window *w, struct glyph_row *row, XRectangle *r,
31676 enum glyph_row_area area)
31678 struct glyph *first = row->glyphs[area];
31679 struct glyph *end = row->glyphs[area] + row->used[area];
31680 struct glyph *last;
31681 int first_x, start_x, x;
31683 if (area == TEXT_AREA && row->fill_line_p)
31684 /* If row extends face to end of line write the whole line. */
31685 draw_glyphs (w, 0, row, area,
31686 0, row->used[area],
31687 DRAW_NORMAL_TEXT, 0);
31688 else
31690 /* Set START_X to the window-relative start position for drawing glyphs of
31691 AREA. The first glyph of the text area can be partially visible.
31692 The first glyphs of other areas cannot. */
31693 start_x = window_box_left_offset (w, area);
31694 x = start_x;
31695 if (area == TEXT_AREA)
31696 x += row->x;
31698 /* Find the first glyph that must be redrawn. */
31699 while (first < end
31700 && x + first->pixel_width < r->x)
31702 x += first->pixel_width;
31703 ++first;
31706 /* Find the last one. */
31707 last = first;
31708 first_x = x;
31709 /* Use a signed int intermediate value to avoid catastrophic
31710 failures due to comparison between signed and unsigned, when
31711 x is negative (can happen for wide images that are hscrolled). */
31712 int r_end = r->x + r->width;
31713 while (last < end && x < r_end)
31715 x += last->pixel_width;
31716 ++last;
31719 /* Repaint. */
31720 if (last > first)
31721 draw_glyphs (w, first_x - start_x, row, area,
31722 first - row->glyphs[area], last - row->glyphs[area],
31723 DRAW_NORMAL_TEXT, 0);
31728 /* Redraw the parts of the glyph row ROW on window W intersecting
31729 rectangle R. R is in window-relative coordinates. Value is
31730 true if mouse-face was overwritten. */
31732 static bool
31733 expose_line (struct window *w, struct glyph_row *row, XRectangle *r)
31735 eassert (row->enabled_p);
31737 if (row->mode_line_p || w->pseudo_window_p)
31738 draw_glyphs (w, 0, row, TEXT_AREA,
31739 0, row->used[TEXT_AREA],
31740 DRAW_NORMAL_TEXT, 0);
31741 else
31743 if (row->used[LEFT_MARGIN_AREA])
31744 expose_area (w, row, r, LEFT_MARGIN_AREA);
31745 if (row->used[TEXT_AREA])
31746 expose_area (w, row, r, TEXT_AREA);
31747 if (row->used[RIGHT_MARGIN_AREA])
31748 expose_area (w, row, r, RIGHT_MARGIN_AREA);
31749 draw_row_fringe_bitmaps (w, row);
31752 return row->mouse_face_p;
31756 /* Redraw those parts of glyphs rows during expose event handling that
31757 overlap other rows. Redrawing of an exposed line writes over parts
31758 of lines overlapping that exposed line; this function fixes that.
31760 W is the window being exposed. FIRST_OVERLAPPING_ROW is the first
31761 row in W's current matrix that is exposed and overlaps other rows.
31762 LAST_OVERLAPPING_ROW is the last such row. */
31764 static void
31765 expose_overlaps (struct window *w,
31766 struct glyph_row *first_overlapping_row,
31767 struct glyph_row *last_overlapping_row,
31768 XRectangle *r)
31770 struct glyph_row *row;
31772 for (row = first_overlapping_row; row <= last_overlapping_row; ++row)
31773 if (row->overlapping_p)
31775 eassert (row->enabled_p && !row->mode_line_p);
31777 row->clip = r;
31778 if (row->used[LEFT_MARGIN_AREA])
31779 x_fix_overlapping_area (w, row, LEFT_MARGIN_AREA, OVERLAPS_BOTH);
31781 if (row->used[TEXT_AREA])
31782 x_fix_overlapping_area (w, row, TEXT_AREA, OVERLAPS_BOTH);
31784 if (row->used[RIGHT_MARGIN_AREA])
31785 x_fix_overlapping_area (w, row, RIGHT_MARGIN_AREA, OVERLAPS_BOTH);
31786 row->clip = NULL;
31791 /* Return true if W's cursor intersects rectangle R. */
31793 static bool
31794 phys_cursor_in_rect_p (struct window *w, XRectangle *r)
31796 XRectangle cr, result;
31797 struct glyph *cursor_glyph;
31798 struct glyph_row *row;
31800 if (w->phys_cursor.vpos >= 0
31801 && w->phys_cursor.vpos < w->current_matrix->nrows
31802 && (row = MATRIX_ROW (w->current_matrix, w->phys_cursor.vpos),
31803 row->enabled_p)
31804 && row->cursor_in_fringe_p)
31806 /* Cursor is in the fringe. */
31807 cr.x = window_box_right_offset (w,
31808 (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
31809 ? RIGHT_MARGIN_AREA
31810 : TEXT_AREA));
31811 cr.y = row->y;
31812 cr.width = WINDOW_RIGHT_FRINGE_WIDTH (w);
31813 cr.height = row->height;
31814 return x_intersect_rectangles (&cr, r, &result);
31817 cursor_glyph = get_phys_cursor_glyph (w);
31818 if (cursor_glyph)
31820 /* r is relative to W's box, but w->phys_cursor.x is relative
31821 to left edge of W's TEXT area. Adjust it. */
31822 cr.x = window_box_left_offset (w, TEXT_AREA) + w->phys_cursor.x;
31823 cr.y = w->phys_cursor.y;
31824 cr.width = cursor_glyph->pixel_width;
31825 cr.height = w->phys_cursor_height;
31826 /* ++KFS: W32 version used W32-specific IntersectRect here, but
31827 I assume the effect is the same -- and this is portable. */
31828 return x_intersect_rectangles (&cr, r, &result);
31830 /* If we don't understand the format, pretend we're not in the hot-spot. */
31831 return false;
31835 /* EXPORT:
31836 Draw a vertical window border to the right of window W if W doesn't
31837 have vertical scroll bars. */
31839 void
31840 x_draw_vertical_border (struct window *w)
31842 struct frame *f = XFRAME (WINDOW_FRAME (w));
31844 /* We could do better, if we knew what type of scroll-bar the adjacent
31845 windows (on either side) have... But we don't :-(
31846 However, I think this works ok. ++KFS 2003-04-25 */
31848 /* Redraw borders between horizontally adjacent windows. Don't
31849 do it for frames with vertical scroll bars because either the
31850 right scroll bar of a window, or the left scroll bar of its
31851 neighbor will suffice as a border. */
31852 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f) || FRAME_RIGHT_DIVIDER_WIDTH (f))
31853 return;
31855 /* Note: It is necessary to redraw both the left and the right
31856 borders, for when only this single window W is being
31857 redisplayed. */
31858 if (!WINDOW_RIGHTMOST_P (w)
31859 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w))
31861 int x0, x1, y0, y1;
31863 window_box_edges (w, &x0, &y0, &x1, &y1);
31864 y1 -= 1;
31866 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
31867 x1 -= 1;
31869 FRAME_RIF (f)->draw_vertical_window_border (w, x1, y0, y1);
31872 if (!WINDOW_LEFTMOST_P (w)
31873 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w))
31875 int x0, x1, y0, y1;
31877 window_box_edges (w, &x0, &y0, &x1, &y1);
31878 y1 -= 1;
31880 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
31881 x0 -= 1;
31883 FRAME_RIF (f)->draw_vertical_window_border (w, x0, y0, y1);
31888 /* Draw window dividers for window W. */
31890 void
31891 x_draw_right_divider (struct window *w)
31893 struct frame *f = WINDOW_XFRAME (w);
31895 if (w->mini || w->pseudo_window_p)
31896 return;
31897 else if (WINDOW_RIGHT_DIVIDER_WIDTH (w))
31899 int x0 = WINDOW_RIGHT_EDGE_X (w) - WINDOW_RIGHT_DIVIDER_WIDTH (w);
31900 int x1 = WINDOW_RIGHT_EDGE_X (w);
31901 int y0 = WINDOW_TOP_EDGE_Y (w);
31902 int y1 = WINDOW_BOTTOM_EDGE_Y (w);
31904 /* If W is horizontally combined and has a right sibling, don't
31905 draw over any bottom divider. */
31906 if (WINDOW_BOTTOM_DIVIDER_WIDTH (w)
31907 && !NILP (w->parent)
31908 && WINDOW_HORIZONTAL_COMBINATION_P (XWINDOW (w->parent))
31909 && !NILP (w->next))
31910 y1 -= WINDOW_BOTTOM_DIVIDER_WIDTH (w);
31912 FRAME_RIF (f)->draw_window_divider (w, x0, x1, y0, y1);
31916 static void
31917 x_draw_bottom_divider (struct window *w)
31919 struct frame *f = XFRAME (WINDOW_FRAME (w));
31921 if (w->mini || w->pseudo_window_p)
31922 return;
31923 else if (WINDOW_BOTTOM_DIVIDER_WIDTH (w))
31925 int x0 = WINDOW_LEFT_EDGE_X (w);
31926 int x1 = WINDOW_RIGHT_EDGE_X (w);
31927 int y0 = WINDOW_BOTTOM_EDGE_Y (w) - WINDOW_BOTTOM_DIVIDER_WIDTH (w);
31928 int y1 = WINDOW_BOTTOM_EDGE_Y (w);
31929 struct window *p = !NILP (w->parent) ? XWINDOW (w->parent) : false;
31931 /* If W is vertically combined and has a sibling below, don't draw
31932 over any right divider. */
31933 if (WINDOW_RIGHT_DIVIDER_WIDTH (w)
31934 && p
31935 && ((WINDOW_VERTICAL_COMBINATION_P (p)
31936 && !NILP (w->next))
31937 || (WINDOW_HORIZONTAL_COMBINATION_P (p)
31938 && NILP (w->next)
31939 && !NILP (p->parent)
31940 && WINDOW_VERTICAL_COMBINATION_P (XWINDOW (p->parent))
31941 && !NILP (XWINDOW (p->parent)->next))))
31942 x1 -= WINDOW_RIGHT_DIVIDER_WIDTH (w);
31944 FRAME_RIF (f)->draw_window_divider (w, x0, x1, y0, y1);
31948 /* Redraw the part of window W intersection rectangle FR. Pixel
31949 coordinates in FR are frame-relative. Call this function with
31950 input blocked. Value is true if the exposure overwrites
31951 mouse-face. */
31953 static bool
31954 expose_window (struct window *w, XRectangle *fr)
31956 struct frame *f = XFRAME (w->frame);
31957 XRectangle wr, r;
31958 bool mouse_face_overwritten_p = false;
31960 /* If window is not yet fully initialized, do nothing. This can
31961 happen when toolkit scroll bars are used and a window is split.
31962 Reconfiguring the scroll bar will generate an expose for a newly
31963 created window. */
31964 if (w->current_matrix == NULL)
31965 return false;
31967 /* When we're currently updating the window, display and current
31968 matrix usually don't agree. Arrange for a thorough display
31969 later. */
31970 if (w->must_be_updated_p)
31972 SET_FRAME_GARBAGED (f);
31973 return false;
31976 /* Frame-relative pixel rectangle of W. */
31977 wr.x = WINDOW_LEFT_EDGE_X (w);
31978 wr.y = WINDOW_TOP_EDGE_Y (w);
31979 wr.width = WINDOW_PIXEL_WIDTH (w);
31980 wr.height = WINDOW_PIXEL_HEIGHT (w);
31982 if (x_intersect_rectangles (fr, &wr, &r))
31984 int yb = window_text_bottom_y (w);
31985 struct glyph_row *row;
31986 struct glyph_row *first_overlapping_row, *last_overlapping_row;
31988 TRACE ((stderr, "expose_window (%d, %d, %d, %d)\n",
31989 r.x, r.y, r.width, r.height));
31991 /* Convert to window coordinates. */
31992 r.x -= WINDOW_LEFT_EDGE_X (w);
31993 r.y -= WINDOW_TOP_EDGE_Y (w);
31995 /* Turn off the cursor. */
31996 bool cursor_cleared_p = (!w->pseudo_window_p
31997 && phys_cursor_in_rect_p (w, &r));
31998 if (cursor_cleared_p)
31999 x_clear_cursor (w);
32001 /* If the row containing the cursor extends face to end of line,
32002 then expose_area might overwrite the cursor outside the
32003 rectangle and thus notice_overwritten_cursor might clear
32004 w->phys_cursor_on_p. We remember the original value and
32005 check later if it is changed. */
32006 bool phys_cursor_on_p = w->phys_cursor_on_p;
32008 /* Use a signed int intermediate value to avoid catastrophic
32009 failures due to comparison between signed and unsigned, when
32010 y0 or y1 is negative (can happen for tall images). */
32011 int r_bottom = r.y + r.height;
32013 /* Update lines intersecting rectangle R. */
32014 first_overlapping_row = last_overlapping_row = NULL;
32015 for (row = w->current_matrix->rows;
32016 row->enabled_p;
32017 ++row)
32019 int y0 = row->y;
32020 int y1 = MATRIX_ROW_BOTTOM_Y (row);
32022 if ((y0 >= r.y && y0 < r_bottom)
32023 || (y1 > r.y && y1 < r_bottom)
32024 || (r.y >= y0 && r.y < y1)
32025 || (r_bottom > y0 && r_bottom < y1))
32027 /* A header line may be overlapping, but there is no need
32028 to fix overlapping areas for them. KFS 2005-02-12 */
32029 if (row->overlapping_p && !row->mode_line_p)
32031 if (first_overlapping_row == NULL)
32032 first_overlapping_row = row;
32033 last_overlapping_row = row;
32036 row->clip = fr;
32037 if (expose_line (w, row, &r))
32038 mouse_face_overwritten_p = true;
32039 row->clip = NULL;
32041 else if (row->overlapping_p)
32043 /* We must redraw a row overlapping the exposed area. */
32044 if (y0 < r.y
32045 ? y0 + row->phys_height > r.y
32046 : y0 + row->ascent - row->phys_ascent < r.y +r.height)
32048 if (first_overlapping_row == NULL)
32049 first_overlapping_row = row;
32050 last_overlapping_row = row;
32054 if (y1 >= yb)
32055 break;
32058 /* Display the mode line if there is one. */
32059 if (window_wants_mode_line (w)
32060 && (row = MATRIX_MODE_LINE_ROW (w->current_matrix),
32061 row->enabled_p)
32062 && row->y < r_bottom)
32064 if (expose_line (w, row, &r))
32065 mouse_face_overwritten_p = true;
32068 if (!w->pseudo_window_p)
32070 /* Fix the display of overlapping rows. */
32071 if (first_overlapping_row)
32072 expose_overlaps (w, first_overlapping_row, last_overlapping_row,
32073 fr);
32075 /* Draw border between windows. */
32076 if (WINDOW_RIGHT_DIVIDER_WIDTH (w))
32077 x_draw_right_divider (w);
32078 else
32079 x_draw_vertical_border (w);
32081 if (WINDOW_BOTTOM_DIVIDER_WIDTH (w))
32082 x_draw_bottom_divider (w);
32084 /* Turn the cursor on again. */
32085 if (cursor_cleared_p
32086 || (phys_cursor_on_p && !w->phys_cursor_on_p))
32087 update_window_cursor (w, true);
32091 return mouse_face_overwritten_p;
32096 /* Redraw (parts) of all windows in the window tree rooted at W that
32097 intersect R. R contains frame pixel coordinates. Value is
32098 true if the exposure overwrites mouse-face. */
32100 static bool
32101 expose_window_tree (struct window *w, XRectangle *r)
32103 struct frame *f = XFRAME (w->frame);
32104 bool mouse_face_overwritten_p = false;
32106 while (w && !FRAME_GARBAGED_P (f))
32108 mouse_face_overwritten_p
32109 |= (WINDOWP (w->contents)
32110 ? expose_window_tree (XWINDOW (w->contents), r)
32111 : expose_window (w, r));
32113 w = NILP (w->next) ? NULL : XWINDOW (w->next);
32116 return mouse_face_overwritten_p;
32120 /* EXPORT:
32121 Redisplay an exposed area of frame F. X and Y are the upper-left
32122 corner of the exposed rectangle. W and H are width and height of
32123 the exposed area. All are pixel values. W or H zero means redraw
32124 the entire frame. */
32126 void
32127 expose_frame (struct frame *f, int x, int y, int w, int h)
32129 XRectangle r;
32130 bool mouse_face_overwritten_p = false;
32132 TRACE ((stderr, "expose_frame "));
32134 /* No need to redraw if frame will be redrawn soon. */
32135 if (FRAME_GARBAGED_P (f))
32137 TRACE ((stderr, " garbaged\n"));
32138 return;
32141 /* If basic faces haven't been realized yet, there is no point in
32142 trying to redraw anything. This can happen when we get an expose
32143 event while Emacs is starting, e.g. by moving another window. */
32144 if (FRAME_FACE_CACHE (f) == NULL
32145 || FRAME_FACE_CACHE (f)->used < BASIC_FACE_ID_SENTINEL)
32147 TRACE ((stderr, " no faces\n"));
32148 return;
32151 if (w == 0 || h == 0)
32153 r.x = r.y = 0;
32154 r.width = FRAME_TEXT_WIDTH (f);
32155 r.height = FRAME_TEXT_HEIGHT (f);
32157 else
32159 r.x = x;
32160 r.y = y;
32161 r.width = w;
32162 r.height = h;
32165 TRACE ((stderr, "(%d, %d, %d, %d)\n", r.x, r.y, r.width, r.height));
32166 mouse_face_overwritten_p = expose_window_tree (XWINDOW (f->root_window), &r);
32168 #if ! defined (USE_GTK) && ! defined (HAVE_NS)
32169 if (WINDOWP (f->tool_bar_window))
32170 mouse_face_overwritten_p
32171 |= expose_window (XWINDOW (f->tool_bar_window), &r);
32172 #endif
32174 #ifdef HAVE_X_WINDOWS
32175 #ifndef MSDOS
32176 #if ! defined (USE_X_TOOLKIT) && ! defined (USE_GTK)
32177 if (WINDOWP (f->menu_bar_window))
32178 mouse_face_overwritten_p
32179 |= expose_window (XWINDOW (f->menu_bar_window), &r);
32180 #endif /* not USE_X_TOOLKIT and not USE_GTK */
32181 #endif
32182 #endif
32184 /* Some window managers support a focus-follows-mouse style with
32185 delayed raising of frames. Imagine a partially obscured frame,
32186 and moving the mouse into partially obscured mouse-face on that
32187 frame. The visible part of the mouse-face will be highlighted,
32188 then the WM raises the obscured frame. With at least one WM, KDE
32189 2.1, Emacs is not getting any event for the raising of the frame
32190 (even tried with SubstructureRedirectMask), only Expose events.
32191 These expose events will draw text normally, i.e. not
32192 highlighted. Which means we must redo the highlight here.
32193 Subsume it under ``we love X''. --gerd 2001-08-15 */
32194 /* Included in Windows version because Windows most likely does not
32195 do the right thing if any third party tool offers
32196 focus-follows-mouse with delayed raise. --jason 2001-10-12 */
32197 if (mouse_face_overwritten_p && !FRAME_GARBAGED_P (f))
32199 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
32200 if (f == hlinfo->mouse_face_mouse_frame)
32202 int mouse_x = hlinfo->mouse_face_mouse_x;
32203 int mouse_y = hlinfo->mouse_face_mouse_y;
32204 clear_mouse_face (hlinfo);
32205 note_mouse_highlight (f, mouse_x, mouse_y);
32211 /* EXPORT:
32212 Determine the intersection of two rectangles R1 and R2. Return
32213 the intersection in *RESULT. Value is true if RESULT is not
32214 empty. */
32216 bool
32217 x_intersect_rectangles (XRectangle *r1, XRectangle *r2, XRectangle *result)
32219 XRectangle *left, *right;
32220 XRectangle *upper, *lower;
32221 bool intersection_p = false;
32223 /* Rearrange so that R1 is the left-most rectangle. */
32224 if (r1->x < r2->x)
32225 left = r1, right = r2;
32226 else
32227 left = r2, right = r1;
32229 /* X0 of the intersection is right.x0, if this is inside R1,
32230 otherwise there is no intersection. */
32231 if (right->x <= left->x + left->width)
32233 result->x = right->x;
32235 /* The right end of the intersection is the minimum of
32236 the right ends of left and right. */
32237 result->width = (min (left->x + left->width, right->x + right->width)
32238 - result->x);
32240 /* Same game for Y. */
32241 if (r1->y < r2->y)
32242 upper = r1, lower = r2;
32243 else
32244 upper = r2, lower = r1;
32246 /* The upper end of the intersection is lower.y0, if this is inside
32247 of upper. Otherwise, there is no intersection. */
32248 if (lower->y <= upper->y + upper->height)
32250 result->y = lower->y;
32252 /* The lower end of the intersection is the minimum of the lower
32253 ends of upper and lower. */
32254 result->height = (min (lower->y + lower->height,
32255 upper->y + upper->height)
32256 - result->y);
32257 intersection_p = true;
32261 return intersection_p;
32264 #endif /* HAVE_WINDOW_SYSTEM */
32267 /***********************************************************************
32268 Initialization
32269 ***********************************************************************/
32271 void
32272 syms_of_xdisp (void)
32274 Vwith_echo_area_save_vector = Qnil;
32275 staticpro (&Vwith_echo_area_save_vector);
32277 Vmessage_stack = Qnil;
32278 staticpro (&Vmessage_stack);
32280 /* Non-nil means don't actually do any redisplay. */
32281 DEFSYM (Qinhibit_redisplay, "inhibit-redisplay");
32283 DEFSYM (Qredisplay_internal_xC_functionx, "redisplay_internal (C function)");
32285 DEFVAR_BOOL("inhibit-message", inhibit_message,
32286 doc: /* Non-nil means calls to `message' are not displayed.
32287 They are still logged to the *Messages* buffer. */);
32288 inhibit_message = 0;
32290 message_dolog_marker1 = Fmake_marker ();
32291 staticpro (&message_dolog_marker1);
32292 message_dolog_marker2 = Fmake_marker ();
32293 staticpro (&message_dolog_marker2);
32294 message_dolog_marker3 = Fmake_marker ();
32295 staticpro (&message_dolog_marker3);
32297 defsubr (&Sset_buffer_redisplay);
32298 #ifdef GLYPH_DEBUG
32299 defsubr (&Sdump_frame_glyph_matrix);
32300 defsubr (&Sdump_glyph_matrix);
32301 defsubr (&Sdump_glyph_row);
32302 defsubr (&Sdump_tool_bar_row);
32303 defsubr (&Strace_redisplay);
32304 defsubr (&Strace_to_stderr);
32305 #endif
32306 #ifdef HAVE_WINDOW_SYSTEM
32307 defsubr (&Stool_bar_height);
32308 defsubr (&Slookup_image_map);
32309 #endif
32310 defsubr (&Sline_pixel_height);
32311 defsubr (&Sformat_mode_line);
32312 defsubr (&Sinvisible_p);
32313 defsubr (&Scurrent_bidi_paragraph_direction);
32314 defsubr (&Swindow_text_pixel_size);
32315 defsubr (&Smove_point_visually);
32316 defsubr (&Sbidi_find_overridden_directionality);
32318 DEFSYM (Qmenu_bar_update_hook, "menu-bar-update-hook");
32319 DEFSYM (Qoverriding_terminal_local_map, "overriding-terminal-local-map");
32320 DEFSYM (Qoverriding_local_map, "overriding-local-map");
32321 DEFSYM (Qwindow_scroll_functions, "window-scroll-functions");
32322 DEFSYM (Qredisplay_end_trigger_functions, "redisplay-end-trigger-functions");
32323 DEFSYM (Qinhibit_point_motion_hooks, "inhibit-point-motion-hooks");
32324 DEFSYM (Qeval, "eval");
32325 DEFSYM (QCdata, ":data");
32327 /* Names of text properties relevant for redisplay. */
32328 DEFSYM (Qdisplay, "display");
32329 DEFSYM (Qspace_width, "space-width");
32330 DEFSYM (Qraise, "raise");
32331 DEFSYM (Qslice, "slice");
32332 DEFSYM (Qspace, "space");
32333 DEFSYM (Qmargin, "margin");
32334 DEFSYM (Qpointer, "pointer");
32335 DEFSYM (Qleft_margin, "left-margin");
32336 DEFSYM (Qright_margin, "right-margin");
32337 DEFSYM (Qcenter, "center");
32338 DEFSYM (Qline_height, "line-height");
32339 DEFSYM (QCalign_to, ":align-to");
32340 DEFSYM (QCrelative_width, ":relative-width");
32341 DEFSYM (QCrelative_height, ":relative-height");
32342 DEFSYM (QCeval, ":eval");
32343 DEFSYM (QCpropertize, ":propertize");
32344 DEFSYM (QCfile, ":file");
32345 DEFSYM (Qfontified, "fontified");
32346 DEFSYM (Qfontification_functions, "fontification-functions");
32348 /* Name of the symbol which disables Lisp evaluation in 'display'
32349 properties. This is used by enriched.el. */
32350 DEFSYM (Qdisable_eval, "disable-eval");
32352 /* Name of the face used to highlight trailing whitespace. */
32353 DEFSYM (Qtrailing_whitespace, "trailing-whitespace");
32355 /* Names of the faces used to display line numbers. */
32356 DEFSYM (Qline_number, "line-number");
32357 DEFSYM (Qline_number_current_line, "line-number-current-line");
32358 /* Name of a text property which disables line-number display. */
32359 DEFSYM (Qdisplay_line_numbers_disable, "display-line-numbers-disable");
32361 /* Name and number of the face used to highlight escape glyphs. */
32362 DEFSYM (Qescape_glyph, "escape-glyph");
32364 /* Name and number of the face used to highlight non-breaking
32365 spaces/hyphens. */
32366 DEFSYM (Qnobreak_space, "nobreak-space");
32367 DEFSYM (Qnobreak_hyphen, "nobreak-hyphen");
32369 /* The symbol 'image' which is the car of the lists used to represent
32370 images in Lisp. Also a tool bar style. */
32371 DEFSYM (Qimage, "image");
32373 /* Tool bar styles. */
32374 DEFSYM (Qtext, "text");
32375 DEFSYM (Qboth, "both");
32376 DEFSYM (Qboth_horiz, "both-horiz");
32377 DEFSYM (Qtext_image_horiz, "text-image-horiz");
32379 /* The image map types. */
32380 DEFSYM (QCmap, ":map");
32381 DEFSYM (QCpointer, ":pointer");
32382 DEFSYM (Qrect, "rect");
32383 DEFSYM (Qcircle, "circle");
32384 DEFSYM (Qpoly, "poly");
32386 DEFSYM (Qinhibit_menubar_update, "inhibit-menubar-update");
32388 DEFSYM (Qgrow_only, "grow-only");
32389 DEFSYM (Qinhibit_eval_during_redisplay, "inhibit-eval-during-redisplay");
32390 DEFSYM (Qposition, "position");
32391 DEFSYM (Qbuffer_position, "buffer-position");
32392 DEFSYM (Qobject, "object");
32394 /* Cursor shapes. */
32395 DEFSYM (Qbar, "bar");
32396 DEFSYM (Qhbar, "hbar");
32397 DEFSYM (Qbox, "box");
32398 DEFSYM (Qhollow, "hollow");
32400 /* Pointer shapes. */
32401 DEFSYM (Qhand, "hand");
32402 DEFSYM (Qarrow, "arrow");
32403 /* also Qtext */
32405 DEFSYM (Qdragging, "dragging");
32407 DEFSYM (Qinhibit_free_realized_faces, "inhibit-free-realized-faces");
32409 list_of_error = list1 (list2 (Qerror, Qvoid_variable));
32410 staticpro (&list_of_error);
32412 /* Values of those variables at last redisplay are stored as
32413 properties on 'overlay-arrow-position' symbol. However, if
32414 Voverlay_arrow_position is a marker, last-arrow-position is its
32415 numerical position. */
32416 DEFSYM (Qlast_arrow_position, "last-arrow-position");
32417 DEFSYM (Qlast_arrow_string, "last-arrow-string");
32419 /* Alternative overlay-arrow-string and overlay-arrow-bitmap
32420 properties on a symbol in overlay-arrow-variable-list. */
32421 DEFSYM (Qoverlay_arrow_string, "overlay-arrow-string");
32422 DEFSYM (Qoverlay_arrow_bitmap, "overlay-arrow-bitmap");
32424 echo_buffer[0] = echo_buffer[1] = Qnil;
32425 staticpro (&echo_buffer[0]);
32426 staticpro (&echo_buffer[1]);
32428 echo_area_buffer[0] = echo_area_buffer[1] = Qnil;
32429 staticpro (&echo_area_buffer[0]);
32430 staticpro (&echo_area_buffer[1]);
32432 Vmessages_buffer_name = build_pure_c_string ("*Messages*");
32433 staticpro (&Vmessages_buffer_name);
32435 mode_line_proptrans_alist = Qnil;
32436 staticpro (&mode_line_proptrans_alist);
32437 mode_line_string_list = Qnil;
32438 staticpro (&mode_line_string_list);
32439 mode_line_string_face = Qnil;
32440 staticpro (&mode_line_string_face);
32441 mode_line_string_face_prop = Qnil;
32442 staticpro (&mode_line_string_face_prop);
32443 Vmode_line_unwind_vector = Qnil;
32444 staticpro (&Vmode_line_unwind_vector);
32446 DEFSYM (Qmode_line_default_help_echo, "mode-line-default-help-echo");
32448 help_echo_string = Qnil;
32449 staticpro (&help_echo_string);
32450 help_echo_object = Qnil;
32451 staticpro (&help_echo_object);
32452 help_echo_window = Qnil;
32453 staticpro (&help_echo_window);
32454 previous_help_echo_string = Qnil;
32455 staticpro (&previous_help_echo_string);
32456 help_echo_pos = -1;
32458 DEFSYM (Qright_to_left, "right-to-left");
32459 DEFSYM (Qleft_to_right, "left-to-right");
32460 defsubr (&Sbidi_resolved_levels);
32462 #ifdef HAVE_WINDOW_SYSTEM
32463 DEFVAR_BOOL ("x-stretch-cursor", x_stretch_cursor_p,
32464 doc: /* Non-nil means draw block cursor as wide as the glyph under it.
32465 For example, if a block cursor is over a tab, it will be drawn as
32466 wide as that tab on the display. */);
32467 x_stretch_cursor_p = 0;
32468 #endif
32470 DEFVAR_LISP ("show-trailing-whitespace", Vshow_trailing_whitespace,
32471 doc: /* Non-nil means highlight trailing whitespace.
32472 The face used for trailing whitespace is `trailing-whitespace'. */);
32473 Vshow_trailing_whitespace = Qnil;
32475 DEFVAR_LISP ("nobreak-char-display", Vnobreak_char_display,
32476 doc: /* Control highlighting of non-ASCII space and hyphen chars.
32477 If the value is t, Emacs highlights non-ASCII chars which have the
32478 same appearance as an ASCII space or hyphen, using the `nobreak-space'
32479 or `nobreak-hyphen' face respectively.
32481 U+00A0 (no-break space), U+00AD (soft hyphen), U+2010 (hyphen), and
32482 U+2011 (non-breaking hyphen) are affected.
32484 Any other non-nil value means to display these characters as an escape
32485 glyph followed by an ordinary space or hyphen.
32487 A value of nil means no special handling of these characters. */);
32488 Vnobreak_char_display = Qt;
32490 DEFVAR_LISP ("void-text-area-pointer", Vvoid_text_area_pointer,
32491 doc: /* The pointer shape to show in void text areas.
32492 A value of nil means to show the text pointer. Other options are
32493 `arrow', `text', `hand', `vdrag', `hdrag', `nhdrag', `modeline', and
32494 `hourglass'. */);
32495 Vvoid_text_area_pointer = Qarrow;
32497 DEFVAR_LISP ("inhibit-redisplay", Vinhibit_redisplay,
32498 doc: /* Non-nil means don't actually do any redisplay.
32499 This is used for internal purposes. */);
32500 Vinhibit_redisplay = Qnil;
32502 DEFVAR_LISP ("global-mode-string", Vglobal_mode_string,
32503 doc: /* String (or mode line construct) included (normally) in `mode-line-format'. */);
32504 Vglobal_mode_string = Qnil;
32506 DEFVAR_LISP ("overlay-arrow-position", Voverlay_arrow_position,
32507 doc: /* Marker for where to display an arrow on top of the buffer text.
32508 This must be the beginning of a line in order to work.
32509 See also `overlay-arrow-string'. */);
32510 Voverlay_arrow_position = Qnil;
32512 DEFVAR_LISP ("overlay-arrow-string", Voverlay_arrow_string,
32513 doc: /* String to display as an arrow in non-window frames.
32514 See also `overlay-arrow-position'. */);
32515 Voverlay_arrow_string = build_pure_c_string ("=>");
32517 DEFVAR_LISP ("overlay-arrow-variable-list", Voverlay_arrow_variable_list,
32518 doc: /* List of variables (symbols) which hold markers for overlay arrows.
32519 The symbols on this list are examined during redisplay to determine
32520 where to display overlay arrows. */);
32521 Voverlay_arrow_variable_list
32522 = list1 (intern_c_string ("overlay-arrow-position"));
32524 DEFVAR_INT ("scroll-step", emacs_scroll_step,
32525 doc: /* The number of lines to try scrolling a window by when point moves out.
32526 If that fails to bring point back on frame, point is centered instead.
32527 If this is zero, point is always centered after it moves off frame.
32528 If you want scrolling to always be a line at a time, you should set
32529 `scroll-conservatively' to a large value rather than set this to 1. */);
32531 DEFVAR_INT ("scroll-conservatively", scroll_conservatively,
32532 doc: /* Scroll up to this many lines, to bring point back on screen.
32533 If point moves off-screen, redisplay will scroll by up to
32534 `scroll-conservatively' lines in order to bring point just barely
32535 onto the screen again. If that cannot be done, then redisplay
32536 recenters point as usual.
32538 If the value is greater than 100, redisplay will never recenter point,
32539 but will always scroll just enough text to bring point into view, even
32540 if you move far away.
32542 A value of zero means always recenter point if it moves off screen. */);
32543 scroll_conservatively = 0;
32545 DEFVAR_INT ("scroll-margin", scroll_margin,
32546 doc: /* Number of lines of margin at the top and bottom of a window.
32547 Trigger automatic scrolling whenever point gets within this many lines
32548 of the top or bottom of the window (see info node `Auto Scrolling'). */);
32549 scroll_margin = 0;
32551 DEFVAR_LISP ("maximum-scroll-margin", Vmaximum_scroll_margin,
32552 doc: /* Maximum effective value of `scroll-margin'.
32553 Given as a fraction of the current window's lines. The value should
32554 be a floating point number between 0.0 and 0.5. The effective maximum
32555 is limited to (/ (1- window-lines) 2). Non-float values for this
32556 variable are ignored and the default 0.25 is used instead. */);
32557 Vmaximum_scroll_margin = make_float (0.25);
32559 DEFVAR_LISP ("display-pixels-per-inch", Vdisplay_pixels_per_inch,
32560 doc: /* Pixels per inch value for non-window system displays.
32561 Value is a number or a cons (WIDTH-DPI . HEIGHT-DPI). */);
32562 Vdisplay_pixels_per_inch = make_float (72.0);
32564 #ifdef GLYPH_DEBUG
32565 DEFVAR_INT ("debug-end-pos", debug_end_pos, doc: /* Don't ask. */);
32566 #endif
32568 DEFVAR_LISP ("truncate-partial-width-windows",
32569 Vtruncate_partial_width_windows,
32570 doc: /* Non-nil means truncate lines in windows narrower than the frame.
32571 For an integer value, truncate lines in each window narrower than the
32572 full frame width, provided the total window width in column units is less
32573 than that integer; otherwise, respect the value of `truncate-lines'.
32574 The total width of the window is as returned by `window-total-width', it
32575 includes the fringes, the continuation and truncation glyphs, the
32576 display margins (if any), and the scroll bar
32578 For any other non-nil value, truncate lines in all windows that do
32579 not span the full frame width.
32581 A value of nil means to respect the value of `truncate-lines'.
32583 If `word-wrap' is enabled, you might want to reduce this. */);
32584 Vtruncate_partial_width_windows = make_number (50);
32586 DEFVAR_LISP ("line-number-display-limit", Vline_number_display_limit,
32587 doc: /* Maximum buffer size for which line number should be displayed.
32588 If the buffer is bigger than this, the line number does not appear
32589 in the mode line. A value of nil means no limit. */);
32590 Vline_number_display_limit = Qnil;
32592 DEFVAR_INT ("line-number-display-limit-width",
32593 line_number_display_limit_width,
32594 doc: /* Maximum line width (in characters) for line number display.
32595 If the average length of the lines near point is bigger than this, then the
32596 line number may be omitted from the mode line. */);
32597 line_number_display_limit_width = 200;
32599 DEFVAR_BOOL ("highlight-nonselected-windows", highlight_nonselected_windows,
32600 doc: /* Non-nil means highlight region even in nonselected windows. */);
32601 highlight_nonselected_windows = false;
32603 DEFVAR_BOOL ("multiple-frames", multiple_frames,
32604 doc: /* Non-nil if more than one frame is visible on this display.
32605 Minibuffer-only frames don't count, but iconified frames do.
32606 This variable is not guaranteed to be accurate except while processing
32607 `frame-title-format' and `icon-title-format'. */);
32609 DEFVAR_LISP ("frame-title-format", Vframe_title_format,
32610 doc: /* Template for displaying the title bar of visible frames.
32611 \(Assuming the window manager supports this feature.)
32613 This variable has the same structure as `mode-line-format', except that
32614 the %c, %C, and %l constructs are ignored. It is used only on frames for
32615 which no explicit name has been set (see `modify-frame-parameters'). */);
32617 DEFVAR_LISP ("icon-title-format", Vicon_title_format,
32618 doc: /* Template for displaying the title bar of an iconified frame.
32619 \(Assuming the window manager supports this feature.)
32620 This variable has the same structure as `mode-line-format' (which see),
32621 and is used only on frames for which no explicit name has been set
32622 \(see `modify-frame-parameters'). */);
32623 Vicon_title_format
32624 = Vframe_title_format
32625 = listn (CONSTYPE_PURE, 3,
32626 intern_c_string ("multiple-frames"),
32627 build_pure_c_string ("%b"),
32628 listn (CONSTYPE_PURE, 4,
32629 empty_unibyte_string,
32630 intern_c_string ("invocation-name"),
32631 build_pure_c_string ("@"),
32632 intern_c_string ("system-name")));
32634 DEFVAR_LISP ("message-log-max", Vmessage_log_max,
32635 doc: /* Maximum number of lines to keep in the message log buffer.
32636 If nil, disable message logging. If t, log messages but don't truncate
32637 the buffer when it becomes large. */);
32638 Vmessage_log_max = make_number (1000);
32640 DEFVAR_LISP ("window-scroll-functions", Vwindow_scroll_functions,
32641 doc: /* List of functions to call before redisplaying a window with scrolling.
32642 Each function is called with two arguments, the window and its new
32643 display-start position.
32644 These functions are called whenever the `window-start' marker is modified,
32645 either to point into another buffer (e.g. via `set-window-buffer') or another
32646 place in the same buffer.
32647 When each function is called, the `window-start' marker of its window
32648 argument has been already set to the new value, and the buffer which that
32649 window will display is set to be the current buffer.
32650 Note that the value of `window-end' is not valid when these functions are
32651 called.
32653 Warning: Do not use this feature to alter the way the window
32654 is scrolled. It is not designed for that, and such use probably won't
32655 work. */);
32656 Vwindow_scroll_functions = Qnil;
32658 DEFVAR_LISP ("redisplay-end-trigger-functions", Vredisplay_end_trigger_functions,
32659 doc: /* Functions called when redisplay of a window reaches the end trigger.
32660 Each function is called with two arguments, the window and the end trigger value.
32661 See `set-window-redisplay-end-trigger'. */);
32662 Vredisplay_end_trigger_functions = Qnil;
32664 DEFVAR_LISP ("mouse-autoselect-window", Vmouse_autoselect_window,
32665 doc: /* Non-nil means autoselect window with mouse pointer.
32666 If nil, do not autoselect windows.
32667 A positive number means delay autoselection by that many seconds: a
32668 window is autoselected only after the mouse has remained in that
32669 window for the duration of the delay.
32670 A negative number has a similar effect, but causes windows to be
32671 autoselected only after the mouse has stopped moving. (Because of
32672 the way Emacs compares mouse events, you will occasionally wait twice
32673 that time before the window gets selected.)
32674 Any other value means to autoselect window instantaneously when the
32675 mouse pointer enters it.
32677 Autoselection selects the minibuffer only if it is active, and never
32678 unselects the minibuffer if it is active.
32680 When customizing this variable make sure that the actual value of
32681 `focus-follows-mouse' matches the behavior of your window manager. */);
32682 Vmouse_autoselect_window = Qnil;
32684 DEFVAR_LISP ("auto-resize-tool-bars", Vauto_resize_tool_bars,
32685 doc: /* Non-nil means automatically resize tool-bars.
32686 This dynamically changes the tool-bar's height to the minimum height
32687 that is needed to make all tool-bar items visible.
32688 If value is `grow-only', the tool-bar's height is only increased
32689 automatically; to decrease the tool-bar height, use \\[recenter]. */);
32690 Vauto_resize_tool_bars = Qt;
32692 DEFVAR_BOOL ("auto-raise-tool-bar-buttons", auto_raise_tool_bar_buttons_p,
32693 doc: /* Non-nil means raise tool-bar buttons when the mouse moves over them. */);
32694 auto_raise_tool_bar_buttons_p = true;
32696 DEFVAR_BOOL ("make-cursor-line-fully-visible", make_cursor_line_fully_visible_p,
32697 doc: /* Non-nil means to scroll (recenter) cursor line if it is not fully visible. */);
32698 make_cursor_line_fully_visible_p = true;
32700 DEFVAR_LISP ("tool-bar-border", Vtool_bar_border,
32701 doc: /* Border below tool-bar in pixels.
32702 If an integer, use it as the height of the border.
32703 If it is one of `internal-border-width' or `border-width', use the
32704 value of the corresponding frame parameter.
32705 Otherwise, no border is added below the tool-bar. */);
32706 Vtool_bar_border = Qinternal_border_width;
32708 DEFVAR_LISP ("tool-bar-button-margin", Vtool_bar_button_margin,
32709 doc: /* Margin around tool-bar buttons in pixels.
32710 If an integer, use that for both horizontal and vertical margins.
32711 Otherwise, value should be a pair of integers `(HORZ . VERT)' with
32712 HORZ specifying the horizontal margin, and VERT specifying the
32713 vertical margin. */);
32714 Vtool_bar_button_margin = make_number (DEFAULT_TOOL_BAR_BUTTON_MARGIN);
32716 DEFVAR_INT ("tool-bar-button-relief", tool_bar_button_relief,
32717 doc: /* Relief thickness of tool-bar buttons. */);
32718 tool_bar_button_relief = DEFAULT_TOOL_BAR_BUTTON_RELIEF;
32720 DEFVAR_LISP ("tool-bar-style", Vtool_bar_style,
32721 doc: /* Tool bar style to use.
32722 It can be one of
32723 image - show images only
32724 text - show text only
32725 both - show both, text below image
32726 both-horiz - show text to the right of the image
32727 text-image-horiz - show text to the left of the image
32728 any other - use system default or image if no system default.
32730 This variable only affects the GTK+ toolkit version of Emacs. */);
32731 Vtool_bar_style = Qnil;
32733 DEFVAR_INT ("tool-bar-max-label-size", tool_bar_max_label_size,
32734 doc: /* Maximum number of characters a label can have to be shown.
32735 The tool bar style must also show labels for this to have any effect, see
32736 `tool-bar-style'. */);
32737 tool_bar_max_label_size = DEFAULT_TOOL_BAR_LABEL_SIZE;
32739 DEFVAR_LISP ("fontification-functions", Vfontification_functions,
32740 doc: /* List of functions to call to fontify regions of text.
32741 Each function is called with one argument POS. Functions must
32742 fontify a region starting at POS in the current buffer, and give
32743 fontified regions the property `fontified'. */);
32744 Vfontification_functions = Qnil;
32745 Fmake_variable_buffer_local (Qfontification_functions);
32747 DEFVAR_BOOL ("unibyte-display-via-language-environment",
32748 unibyte_display_via_language_environment,
32749 doc: /* Non-nil means display unibyte text according to language environment.
32750 Specifically, this means that raw bytes in the range 160-255 decimal
32751 are displayed by converting them to the equivalent multibyte characters
32752 according to the current language environment. As a result, they are
32753 displayed according to the current fontset.
32755 Note that this variable affects only how these bytes are displayed,
32756 but does not change the fact they are interpreted as raw bytes. */);
32757 unibyte_display_via_language_environment = false;
32759 DEFVAR_LISP ("max-mini-window-height", Vmax_mini_window_height,
32760 doc: /* Maximum height for resizing mini-windows (the minibuffer and the echo area).
32761 If a float, it specifies a fraction of the mini-window frame's height.
32762 If an integer, it specifies a number of lines. */);
32763 Vmax_mini_window_height = make_float (0.25);
32765 DEFVAR_LISP ("resize-mini-windows", Vresize_mini_windows,
32766 doc: /* How to resize mini-windows (the minibuffer and the echo area).
32767 A value of nil means don't automatically resize mini-windows.
32768 A value of t means resize them to fit the text displayed in them.
32769 A value of `grow-only', the default, means let mini-windows grow only;
32770 they return to their normal size when the minibuffer is closed, or the
32771 echo area becomes empty. */);
32772 /* Contrary to the doc string, we initialize this to nil, so that
32773 loading loadup.el won't try to resize windows before loading
32774 window.el, where some functions we need to call for this live.
32775 We assign the 'grow-only' value right after loading window.el
32776 during loadup. */
32777 Vresize_mini_windows = Qnil;
32779 DEFVAR_LISP ("blink-cursor-alist", Vblink_cursor_alist,
32780 doc: /* Alist specifying how to blink the cursor off.
32781 Each element has the form (ON-STATE . OFF-STATE). Whenever the
32782 `cursor-type' frame-parameter or variable equals ON-STATE,
32783 comparing using `equal', Emacs uses OFF-STATE to specify
32784 how to blink it off. ON-STATE and OFF-STATE are values for
32785 the `cursor-type' frame parameter.
32787 If a frame's ON-STATE has no entry in this list,
32788 the frame's other specifications determine how to blink the cursor off. */);
32789 Vblink_cursor_alist = Qnil;
32791 DEFVAR_LISP ("auto-hscroll-mode", automatic_hscrolling,
32792 doc: /* Allow or disallow automatic horizontal scrolling of windows.
32793 The value `current-line' means the line displaying point in each window
32794 is automatically scrolled horizontally to make point visible.
32795 Any other non-nil value means all the lines in a window are automatically
32796 scrolled horizontally to make point visible. */);
32797 automatic_hscrolling = Qt;
32798 DEFSYM (Qauto_hscroll_mode, "auto-hscroll-mode");
32799 DEFSYM (Qcurrent_line, "current-line");
32801 DEFVAR_INT ("hscroll-margin", hscroll_margin,
32802 doc: /* How many columns away from the window edge point is allowed to get
32803 before automatic hscrolling will horizontally scroll the window. */);
32804 hscroll_margin = 5;
32806 DEFVAR_LISP ("hscroll-step", Vhscroll_step,
32807 doc: /* How many columns to scroll the window when point gets too close to the edge.
32808 When point is less than `hscroll-margin' columns from the window
32809 edge, automatic hscrolling will scroll the window by the amount of columns
32810 determined by this variable. If its value is a positive integer, scroll that
32811 many columns. If it's a positive floating-point number, it specifies the
32812 fraction of the window's width to scroll. If it's nil or zero, point will be
32813 centered horizontally after the scroll. Any other value, including negative
32814 numbers, are treated as if the value were zero.
32816 Automatic hscrolling always moves point outside the scroll margin, so if
32817 point was more than scroll step columns inside the margin, the window will
32818 scroll more than the value given by the scroll step.
32820 Note that the lower bound for automatic hscrolling specified by `scroll-left'
32821 and `scroll-right' overrides this variable's effect. */);
32822 Vhscroll_step = make_number (0);
32824 DEFVAR_BOOL ("message-truncate-lines", message_truncate_lines,
32825 doc: /* If non-nil, messages are truncated instead of resizing the echo area.
32826 Bind this around calls to `message' to let it take effect. */);
32827 message_truncate_lines = false;
32829 DEFVAR_LISP ("menu-bar-update-hook", Vmenu_bar_update_hook,
32830 doc: /* Normal hook run to update the menu bar definitions.
32831 Redisplay runs this hook before it redisplays the menu bar.
32832 This is used to update menus such as Buffers, whose contents depend on
32833 various data. */);
32834 Vmenu_bar_update_hook = Qnil;
32836 DEFVAR_LISP ("menu-updating-frame", Vmenu_updating_frame,
32837 doc: /* Frame for which we are updating a menu.
32838 The enable predicate for a menu binding should check this variable. */);
32839 Vmenu_updating_frame = Qnil;
32841 DEFVAR_BOOL ("inhibit-menubar-update", inhibit_menubar_update,
32842 doc: /* Non-nil means don't update menu bars. Internal use only. */);
32843 inhibit_menubar_update = false;
32845 DEFVAR_LISP ("wrap-prefix", Vwrap_prefix,
32846 doc: /* Prefix prepended to all continuation lines at display time.
32847 The value may be a string, an image, or a stretch-glyph; it is
32848 interpreted in the same way as the value of a `display' text property.
32850 This variable is overridden by any `wrap-prefix' text or overlay
32851 property.
32853 To add a prefix to non-continuation lines, use `line-prefix'. */);
32854 Vwrap_prefix = Qnil;
32855 DEFSYM (Qwrap_prefix, "wrap-prefix");
32856 Fmake_variable_buffer_local (Qwrap_prefix);
32858 DEFVAR_LISP ("line-prefix", Vline_prefix,
32859 doc: /* Prefix prepended to all non-continuation lines at display time.
32860 The value may be a string, an image, or a stretch-glyph; it is
32861 interpreted in the same way as the value of a `display' text property.
32863 This variable is overridden by any `line-prefix' text or overlay
32864 property.
32866 To add a prefix to continuation lines, use `wrap-prefix'. */);
32867 Vline_prefix = Qnil;
32868 DEFSYM (Qline_prefix, "line-prefix");
32869 Fmake_variable_buffer_local (Qline_prefix);
32871 DEFVAR_LISP ("display-line-numbers", Vdisplay_line_numbers,
32872 doc: /* Non-nil means display line numbers.
32873 If the value is t, display the absolute number of each line of a buffer
32874 shown in a window. Absolute line numbers count from the beginning of
32875 the current narrowing, or from buffer beginning. If the value is
32876 `relative', display for each line not containing the window's point its
32877 relative number instead, i.e. the number of the line relative to the
32878 line showing the window's point.
32880 In either case, line numbers are displayed at the beginning of each
32881 non-continuation line that displays buffer text, i.e. after each newline
32882 character that comes from the buffer. The value `visual' is like
32883 `relative' but counts screen lines instead of buffer lines. In practice
32884 this means that continuation lines count as well when calculating the
32885 relative number of a line.
32887 Lisp programs can disable display of a line number of a particular
32888 buffer line by putting the `display-line-numbers-disable' text property
32889 or overlay property on the first visible character of that line. */);
32890 Vdisplay_line_numbers = Qnil;
32891 DEFSYM (Qdisplay_line_numbers, "display-line-numbers");
32892 Fmake_variable_buffer_local (Qdisplay_line_numbers);
32893 DEFSYM (Qrelative, "relative");
32894 DEFSYM (Qvisual, "visual");
32896 DEFVAR_LISP ("display-line-numbers-width", Vdisplay_line_numbers_width,
32897 doc: /* Minimum width of space reserved for line number display.
32898 A positive number means reserve that many columns for line numbers,
32899 even if the actual number needs less space.
32900 The default value of nil means compute the space dynamically.
32901 Any other value is treated as nil. */);
32902 Vdisplay_line_numbers_width = Qnil;
32903 DEFSYM (Qdisplay_line_numbers_width, "display-line-numbers-width");
32904 Fmake_variable_buffer_local (Qdisplay_line_numbers_width);
32906 DEFVAR_LISP ("display-line-numbers-current-absolute",
32907 Vdisplay_line_numbers_current_absolute,
32908 doc: /* Non-nil means display absolute number of current line.
32909 This variable has effect only when `display-line-numbers' is
32910 either `relative' or `visual'. */);
32911 Vdisplay_line_numbers_current_absolute = Qt;
32913 DEFVAR_BOOL ("display-line-numbers-widen", display_line_numbers_widen,
32914 doc: /* Non-nil means display line numbers disregarding any narrowing. */);
32915 display_line_numbers_widen = false;
32916 DEFSYM (Qdisplay_line_numbers_widen, "display-line-numbers-widen");
32917 Fmake_variable_buffer_local (Qdisplay_line_numbers_widen);
32919 DEFVAR_BOOL ("inhibit-eval-during-redisplay", inhibit_eval_during_redisplay,
32920 doc: /* Non-nil means don't eval Lisp during redisplay. */);
32921 inhibit_eval_during_redisplay = false;
32923 DEFVAR_BOOL ("inhibit-free-realized-faces", inhibit_free_realized_faces,
32924 doc: /* Non-nil means don't free realized faces. Internal use only. */);
32925 inhibit_free_realized_faces = false;
32927 DEFVAR_BOOL ("inhibit-bidi-mirroring", inhibit_bidi_mirroring,
32928 doc: /* Non-nil means don't mirror characters even when bidi context requires that.
32929 Intended for use during debugging and for testing bidi display;
32930 see biditest.el in the test suite. */);
32931 inhibit_bidi_mirroring = false;
32933 #ifdef GLYPH_DEBUG
32934 DEFVAR_BOOL ("inhibit-try-window-id", inhibit_try_window_id,
32935 doc: /* Inhibit try_window_id display optimization. */);
32936 inhibit_try_window_id = false;
32938 DEFVAR_BOOL ("inhibit-try-window-reusing", inhibit_try_window_reusing,
32939 doc: /* Inhibit try_window_reusing display optimization. */);
32940 inhibit_try_window_reusing = false;
32942 DEFVAR_BOOL ("inhibit-try-cursor-movement", inhibit_try_cursor_movement,
32943 doc: /* Inhibit try_cursor_movement display optimization. */);
32944 inhibit_try_cursor_movement = false;
32945 #endif /* GLYPH_DEBUG */
32947 DEFVAR_INT ("overline-margin", overline_margin,
32948 doc: /* Space between overline and text, in pixels.
32949 The default value is 2: the height of the overline (1 pixel) plus 1 pixel
32950 margin to the character height. */);
32951 overline_margin = 2;
32953 DEFVAR_INT ("underline-minimum-offset",
32954 underline_minimum_offset,
32955 doc: /* Minimum distance between baseline and underline.
32956 This can improve legibility of underlined text at small font sizes,
32957 particularly when using variable `x-use-underline-position-properties'
32958 with fonts that specify an UNDERLINE_POSITION relatively close to the
32959 baseline. The default value is 1. */);
32960 underline_minimum_offset = 1;
32962 DEFVAR_BOOL ("display-hourglass", display_hourglass_p,
32963 doc: /* Non-nil means show an hourglass pointer, when Emacs is busy.
32964 This feature only works when on a window system that can change
32965 cursor shapes. */);
32966 display_hourglass_p = true;
32968 DEFVAR_LISP ("hourglass-delay", Vhourglass_delay,
32969 doc: /* Seconds to wait before displaying an hourglass pointer when Emacs is busy. */);
32970 Vhourglass_delay = make_number (DEFAULT_HOURGLASS_DELAY);
32972 #ifdef HAVE_WINDOW_SYSTEM
32973 hourglass_atimer = NULL;
32974 hourglass_shown_p = false;
32975 #endif /* HAVE_WINDOW_SYSTEM */
32977 /* Name of the face used to display glyphless characters. */
32978 DEFSYM (Qglyphless_char, "glyphless-char");
32980 /* Method symbols for Vglyphless_char_display. */
32981 DEFSYM (Qhex_code, "hex-code");
32982 DEFSYM (Qempty_box, "empty-box");
32983 DEFSYM (Qthin_space, "thin-space");
32984 DEFSYM (Qzero_width, "zero-width");
32986 DEFVAR_LISP ("pre-redisplay-function", Vpre_redisplay_function,
32987 doc: /* Function run just before redisplay.
32988 It is called with one argument, which is the set of windows that are to
32989 be redisplayed. This set can be nil (meaning, only the selected window),
32990 or t (meaning all windows). */);
32991 Vpre_redisplay_function = intern ("ignore");
32993 /* Symbol for the purpose of Vglyphless_char_display. */
32994 DEFSYM (Qglyphless_char_display, "glyphless-char-display");
32995 Fput (Qglyphless_char_display, Qchar_table_extra_slots, make_number (1));
32997 DEFVAR_LISP ("glyphless-char-display", Vglyphless_char_display,
32998 doc: /* Char-table defining glyphless characters.
32999 Each element, if non-nil, should be one of the following:
33000 an ASCII acronym string: display this string in a box
33001 `hex-code': display the hexadecimal code of a character in a box
33002 `empty-box': display as an empty box
33003 `thin-space': display as 1-pixel width space
33004 `zero-width': don't display
33005 An element may also be a cons cell (GRAPHICAL . TEXT), which specifies the
33006 display method for graphical terminals and text terminals respectively.
33007 GRAPHICAL and TEXT should each have one of the values listed above.
33009 The char-table has one extra slot to control the display of a character for
33010 which no font is found. This slot only takes effect on graphical terminals.
33011 Its value should be an ASCII acronym string, `hex-code', `empty-box', or
33012 `thin-space'. The default is `empty-box'.
33014 If a character has a non-nil entry in an active display table, the
33015 display table takes effect; in this case, Emacs does not consult
33016 `glyphless-char-display' at all. */);
33017 Vglyphless_char_display = Fmake_char_table (Qglyphless_char_display, Qnil);
33018 Fset_char_table_extra_slot (Vglyphless_char_display, make_number (0),
33019 Qempty_box);
33021 DEFVAR_LISP ("debug-on-message", Vdebug_on_message,
33022 doc: /* If non-nil, debug if a message matching this regexp is displayed. */);
33023 Vdebug_on_message = Qnil;
33025 DEFVAR_LISP ("redisplay--all-windows-cause", Vredisplay__all_windows_cause,
33026 doc: /* */);
33027 Vredisplay__all_windows_cause = Fmake_hash_table (0, NULL);
33029 DEFVAR_LISP ("redisplay--mode-lines-cause", Vredisplay__mode_lines_cause,
33030 doc: /* */);
33031 Vredisplay__mode_lines_cause = Fmake_hash_table (0, NULL);
33033 DEFVAR_BOOL ("redisplay--inhibit-bidi", redisplay__inhibit_bidi,
33034 doc: /* Non-nil means it is not safe to attempt bidi reordering for display. */);
33035 /* Initialize to t, since we need to disable reordering until
33036 loadup.el successfully loads charprop.el. */
33037 redisplay__inhibit_bidi = true;
33039 DEFVAR_BOOL ("display-raw-bytes-as-hex", display_raw_bytes_as_hex,
33040 doc: /* Non-nil means display raw bytes in hexadecimal format.
33041 The default is to use octal format (\200) whereas hexadecimal (\x80)
33042 may be more familiar to users. */);
33043 display_raw_bytes_as_hex = false;
33048 /* Initialize this module when Emacs starts. */
33050 void
33051 init_xdisp (void)
33053 CHARPOS (this_line_start_pos) = 0;
33055 if (!noninteractive)
33057 struct window *m = XWINDOW (minibuf_window);
33058 Lisp_Object frame = m->frame;
33059 struct frame *f = XFRAME (frame);
33060 Lisp_Object root = FRAME_ROOT_WINDOW (f);
33061 struct window *r = XWINDOW (root);
33062 int i;
33064 echo_area_window = minibuf_window;
33066 r->top_line = FRAME_TOP_MARGIN (f);
33067 r->pixel_top = r->top_line * FRAME_LINE_HEIGHT (f);
33068 r->total_cols = FRAME_COLS (f);
33069 r->pixel_width = r->total_cols * FRAME_COLUMN_WIDTH (f);
33070 r->total_lines = FRAME_TOTAL_LINES (f) - 1 - FRAME_TOP_MARGIN (f);
33071 r->pixel_height = r->total_lines * FRAME_LINE_HEIGHT (f);
33073 m->top_line = FRAME_TOTAL_LINES (f) - 1;
33074 m->pixel_top = m->top_line * FRAME_LINE_HEIGHT (f);
33075 m->total_cols = FRAME_COLS (f);
33076 m->pixel_width = m->total_cols * FRAME_COLUMN_WIDTH (f);
33077 m->total_lines = 1;
33078 m->pixel_height = m->total_lines * FRAME_LINE_HEIGHT (f);
33080 scratch_glyph_row.glyphs[TEXT_AREA] = scratch_glyphs;
33081 scratch_glyph_row.glyphs[TEXT_AREA + 1]
33082 = scratch_glyphs + MAX_SCRATCH_GLYPHS;
33084 /* The default ellipsis glyphs `...'. */
33085 for (i = 0; i < 3; ++i)
33086 default_invis_vector[i] = make_number ('.');
33090 /* Allocate the buffer for frame titles.
33091 Also used for `format-mode-line'. */
33092 int size = 100;
33093 mode_line_noprop_buf = xmalloc (size);
33094 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
33095 mode_line_noprop_ptr = mode_line_noprop_buf;
33096 mode_line_target = MODE_LINE_DISPLAY;
33099 help_echo_showing_p = false;
33102 #ifdef HAVE_WINDOW_SYSTEM
33104 /* Platform-independent portion of hourglass implementation. */
33106 /* Timer function of hourglass_atimer. */
33108 static void
33109 show_hourglass (struct atimer *timer)
33111 /* The timer implementation will cancel this timer automatically
33112 after this function has run. Set hourglass_atimer to null
33113 so that we know the timer doesn't have to be canceled. */
33114 hourglass_atimer = NULL;
33116 if (!hourglass_shown_p)
33118 Lisp_Object tail, frame;
33120 block_input ();
33122 FOR_EACH_FRAME (tail, frame)
33124 struct frame *f = XFRAME (frame);
33126 if (FRAME_LIVE_P (f) && FRAME_WINDOW_P (f)
33127 && FRAME_RIF (f)->show_hourglass)
33128 FRAME_RIF (f)->show_hourglass (f);
33131 hourglass_shown_p = true;
33132 unblock_input ();
33136 /* Cancel a currently active hourglass timer, and start a new one. */
33138 void
33139 start_hourglass (void)
33141 struct timespec delay;
33143 cancel_hourglass ();
33145 if (INTEGERP (Vhourglass_delay)
33146 && XINT (Vhourglass_delay) > 0)
33147 delay = make_timespec (min (XINT (Vhourglass_delay),
33148 TYPE_MAXIMUM (time_t)),
33150 else if (FLOATP (Vhourglass_delay)
33151 && XFLOAT_DATA (Vhourglass_delay) > 0)
33152 delay = dtotimespec (XFLOAT_DATA (Vhourglass_delay));
33153 else
33154 delay = make_timespec (DEFAULT_HOURGLASS_DELAY, 0);
33156 hourglass_atimer = start_atimer (ATIMER_RELATIVE, delay,
33157 show_hourglass, NULL);
33160 /* Cancel the hourglass cursor timer if active, hide a busy cursor if
33161 shown. */
33163 void
33164 cancel_hourglass (void)
33166 if (hourglass_atimer)
33168 cancel_atimer (hourglass_atimer);
33169 hourglass_atimer = NULL;
33172 if (hourglass_shown_p)
33174 Lisp_Object tail, frame;
33176 block_input ();
33178 FOR_EACH_FRAME (tail, frame)
33180 struct frame *f = XFRAME (frame);
33182 if (FRAME_LIVE_P (f) && FRAME_WINDOW_P (f)
33183 && FRAME_RIF (f)->hide_hourglass)
33184 FRAME_RIF (f)->hide_hourglass (f);
33185 #ifdef HAVE_NTGUI
33186 /* No cursors on non GUI frames - restore to stock arrow cursor. */
33187 else if (!FRAME_W32_P (f))
33188 w32_arrow_cursor ();
33189 #endif
33192 hourglass_shown_p = false;
33193 unblock_input ();
33197 #endif /* HAVE_WINDOW_SYSTEM */