; * ChangeLog.3: Update.
[emacs.git] / src / xdisp.c
blobd6aabd06189c90bff56ad6b95ae4bfbb0bedd3da
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 /* Clear frames marked as garbaged. */
13928 clear_garbaged_frames ();
13930 /* Build menubar and tool-bar items. */
13931 if (NILP (Vmemory_full))
13932 prepare_menu_bars ();
13934 /* do_pending_window_change could change the selected_window due to
13935 frame resizing which makes the selected window too small.
13936 prepare_menu_bars may call lisp hooks and hence also change the
13937 selected_window. */
13938 if (WINDOWP (selected_window) && (w = XWINDOW (selected_window)) != sw)
13939 sw = w;
13941 reconsider_clip_changes (w);
13943 /* In most cases selected window displays current buffer. */
13944 match_p = XBUFFER (w->contents) == current_buffer;
13945 if (match_p)
13947 /* Detect case that we need to write or remove a star in the mode line. */
13948 if ((SAVE_MODIFF < MODIFF) != w->last_had_star)
13949 w->update_mode_line = true;
13951 if (mode_line_update_needed (w))
13952 w->update_mode_line = true;
13954 /* If reconsider_clip_changes above decided that the narrowing
13955 in the current buffer changed, make sure all other windows
13956 showing that buffer will be redisplayed. */
13957 if (current_buffer->clip_changed)
13958 bset_update_mode_line (current_buffer);
13961 /* Normally the message* functions will have already displayed and
13962 updated the echo area, but the frame may have been trashed, or
13963 the update may have been preempted, so display the echo area
13964 again here. Checking message_cleared_p captures the case that
13965 the echo area should be cleared. */
13966 if ((!NILP (echo_area_buffer[0]) && !display_last_displayed_message_p)
13967 || (!NILP (echo_area_buffer[1]) && display_last_displayed_message_p)
13968 || (message_cleared_p
13969 && minibuf_level == 0
13970 /* If the mini-window is currently selected, this means the
13971 echo-area doesn't show through. */
13972 && !MINI_WINDOW_P (XWINDOW (selected_window))))
13974 echo_area_display (false);
13976 /* If echo_area_display resizes the mini-window, the redisplay and
13977 window_sizes_changed flags of the selected frame are set, but
13978 it's too late for the hooks in window-size-change-functions,
13979 which have been examined already in prepare_menu_bars. So in
13980 that case we call the hooks here only for the selected frame. */
13981 if (sf->redisplay)
13983 ptrdiff_t count1 = SPECPDL_INDEX ();
13985 record_unwind_save_match_data ();
13986 run_window_size_change_functions (selected_frame);
13987 unbind_to (count1, Qnil);
13990 if (message_cleared_p)
13991 update_miniwindow_p = true;
13993 must_finish = true;
13995 /* If we don't display the current message, don't clear the
13996 message_cleared_p flag, because, if we did, we wouldn't clear
13997 the echo area in the next redisplay which doesn't preserve
13998 the echo area. */
13999 if (!display_last_displayed_message_p)
14000 message_cleared_p = false;
14002 else if (EQ (selected_window, minibuf_window)
14003 && (current_buffer->clip_changed || window_outdated (w))
14004 && resize_mini_window (w, false))
14006 if (sf->redisplay)
14008 ptrdiff_t count1 = SPECPDL_INDEX ();
14010 record_unwind_save_match_data ();
14011 run_window_size_change_functions (selected_frame);
14012 unbind_to (count1, Qnil);
14015 /* Resized active mini-window to fit the size of what it is
14016 showing if its contents might have changed. */
14017 must_finish = true;
14019 /* If window configuration was changed, frames may have been
14020 marked garbaged. Clear them or we will experience
14021 surprises wrt scrolling. */
14022 clear_garbaged_frames ();
14025 if (windows_or_buffers_changed && !update_mode_lines)
14026 /* Code that sets windows_or_buffers_changed doesn't distinguish whether
14027 only the windows's contents needs to be refreshed, or whether the
14028 mode-lines also need a refresh. */
14029 update_mode_lines = (windows_or_buffers_changed == REDISPLAY_SOME
14030 ? REDISPLAY_SOME : 32);
14032 /* If specs for an arrow have changed, do thorough redisplay
14033 to ensure we remove any arrow that should no longer exist. */
14034 /* Apparently, this is the only case where we update other windows,
14035 without updating other mode-lines. */
14036 overlay_arrows_changed_p (true);
14038 consider_all_windows_p = (update_mode_lines
14039 || windows_or_buffers_changed);
14041 #define AINC(a,i) \
14043 Lisp_Object entry = Fgethash (make_number (i), a, make_number (0)); \
14044 if (INTEGERP (entry)) \
14045 Fputhash (make_number (i), make_number (1 + XINT (entry)), a); \
14048 AINC (Vredisplay__all_windows_cause, windows_or_buffers_changed);
14049 AINC (Vredisplay__mode_lines_cause, update_mode_lines);
14051 /* Optimize the case that only the line containing the cursor in the
14052 selected window has changed. Variables starting with this_ are
14053 set in display_line and record information about the line
14054 containing the cursor. */
14055 tlbufpos = this_line_start_pos;
14056 tlendpos = this_line_end_pos;
14057 if (!consider_all_windows_p
14058 && CHARPOS (tlbufpos) > 0
14059 && !w->update_mode_line
14060 && !current_buffer->clip_changed
14061 && !current_buffer->prevent_redisplay_optimizations_p
14062 && FRAME_VISIBLE_P (XFRAME (w->frame))
14063 && !FRAME_OBSCURED_P (XFRAME (w->frame))
14064 && !XFRAME (w->frame)->cursor_type_changed
14065 && !XFRAME (w->frame)->face_change
14066 /* Make sure recorded data applies to current buffer, etc. */
14067 && this_line_buffer == current_buffer
14068 && match_p
14069 && !w->force_start
14070 && !w->optional_new_start
14071 /* Point must be on the line that we have info recorded about. */
14072 && PT >= CHARPOS (tlbufpos)
14073 && PT <= Z - CHARPOS (tlendpos)
14074 /* All text outside that line, including its final newline,
14075 must be unchanged. */
14076 && text_outside_line_unchanged_p (w, CHARPOS (tlbufpos),
14077 CHARPOS (tlendpos)))
14079 if (CHARPOS (tlbufpos) > BEGV
14080 && FETCH_BYTE (BYTEPOS (tlbufpos) - 1) != '\n'
14081 && (CHARPOS (tlbufpos) == ZV
14082 || FETCH_BYTE (BYTEPOS (tlbufpos)) == '\n'))
14083 /* Former continuation line has disappeared by becoming empty. */
14084 goto cancel;
14085 else if (window_outdated (w) || MINI_WINDOW_P (w))
14087 /* We have to handle the case of continuation around a
14088 wide-column character (see the comment in indent.c around
14089 line 1340).
14091 For instance, in the following case:
14093 -------- Insert --------
14094 K_A_N_\\ `a' K_A_N_a\ `X_' are wide-column chars.
14095 J_I_ ==> J_I_ `^^' are cursors.
14096 ^^ ^^
14097 -------- --------
14099 As we have to redraw the line above, we cannot use this
14100 optimization. */
14102 struct it it;
14103 int line_height_before = this_line_pixel_height;
14105 /* Note that start_display will handle the case that the
14106 line starting at tlbufpos is a continuation line. */
14107 start_display (&it, w, tlbufpos);
14109 /* Implementation note: It this still necessary? */
14110 if (it.current_x != this_line_start_x)
14111 goto cancel;
14113 TRACE ((stderr, "trying display optimization 1\n"));
14114 w->cursor.vpos = -1;
14115 overlay_arrow_seen = false;
14116 it.vpos = this_line_vpos;
14117 it.current_y = this_line_y;
14118 it.glyph_row = MATRIX_ROW (w->desired_matrix, this_line_vpos);
14119 display_line (&it, -1);
14121 /* If line contains point, is not continued,
14122 and ends at same distance from eob as before, we win. */
14123 if (w->cursor.vpos >= 0
14124 /* Line is not continued, otherwise this_line_start_pos
14125 would have been set to 0 in display_line. */
14126 && CHARPOS (this_line_start_pos)
14127 /* Line ends as before. */
14128 && CHARPOS (this_line_end_pos) == CHARPOS (tlendpos)
14129 /* Line has same height as before. Otherwise other lines
14130 would have to be shifted up or down. */
14131 && this_line_pixel_height == line_height_before)
14133 /* If this is not the window's last line, we must adjust
14134 the charstarts of the lines below. */
14135 if (it.current_y < it.last_visible_y)
14137 struct glyph_row *row
14138 = MATRIX_ROW (w->current_matrix, this_line_vpos + 1);
14139 ptrdiff_t delta, delta_bytes;
14141 /* We used to distinguish between two cases here,
14142 conditioned by Z - CHARPOS (tlendpos) == ZV, for
14143 when the line ends in a newline or the end of the
14144 buffer's accessible portion. But both cases did
14145 the same, so they were collapsed. */
14146 delta = (Z
14147 - CHARPOS (tlendpos)
14148 - MATRIX_ROW_START_CHARPOS (row));
14149 delta_bytes = (Z_BYTE
14150 - BYTEPOS (tlendpos)
14151 - MATRIX_ROW_START_BYTEPOS (row));
14153 increment_matrix_positions (w->current_matrix,
14154 this_line_vpos + 1,
14155 w->current_matrix->nrows,
14156 delta, delta_bytes);
14159 /* If this row displays text now but previously didn't,
14160 or vice versa, w->window_end_vpos may have to be
14161 adjusted. */
14162 if (MATRIX_ROW_DISPLAYS_TEXT_P (it.glyph_row - 1))
14164 if (w->window_end_vpos < this_line_vpos)
14165 w->window_end_vpos = this_line_vpos;
14167 else if (w->window_end_vpos == this_line_vpos
14168 && this_line_vpos > 0)
14169 w->window_end_vpos = this_line_vpos - 1;
14170 w->window_end_valid = false;
14172 /* Update hint: No need to try to scroll in update_window. */
14173 w->desired_matrix->no_scrolling_p = true;
14175 #ifdef GLYPH_DEBUG
14176 *w->desired_matrix->method = 0;
14177 debug_method_add (w, "optimization 1");
14178 #endif
14179 #ifdef HAVE_WINDOW_SYSTEM
14180 update_window_fringes (w, false);
14181 #endif
14182 goto update;
14184 else
14185 goto cancel;
14187 else if (/* Cursor position hasn't changed. */
14188 PT == w->last_point
14189 /* Make sure the cursor was last displayed
14190 in this window. Otherwise we have to reposition it. */
14192 /* PXW: Must be converted to pixels, probably. */
14193 && 0 <= w->cursor.vpos
14194 && w->cursor.vpos < WINDOW_TOTAL_LINES (w))
14196 if (!must_finish)
14198 do_pending_window_change (true);
14199 /* If selected_window changed, redisplay again. */
14200 if (WINDOWP (selected_window)
14201 && (w = XWINDOW (selected_window)) != sw)
14202 goto retry;
14204 /* We used to always goto end_of_redisplay here, but this
14205 isn't enough if we have a blinking cursor. */
14206 if (w->cursor_off_p == w->last_cursor_off_p)
14207 goto end_of_redisplay;
14209 goto update;
14211 /* If highlighting the region, or if the cursor is in the echo area,
14212 then we can't just move the cursor. */
14213 else if (NILP (Vshow_trailing_whitespace)
14214 && !cursor_in_echo_area)
14216 struct it it;
14217 struct glyph_row *row;
14219 /* Skip from tlbufpos to PT and see where it is. Note that
14220 PT may be in invisible text. If so, we will end at the
14221 next visible position. */
14222 init_iterator (&it, w, CHARPOS (tlbufpos), BYTEPOS (tlbufpos),
14223 NULL, DEFAULT_FACE_ID);
14224 it.current_x = this_line_start_x;
14225 it.current_y = this_line_y;
14226 it.vpos = this_line_vpos;
14228 /* The call to move_it_to stops in front of PT, but
14229 moves over before-strings. */
14230 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
14232 if (it.vpos == this_line_vpos
14233 && (row = MATRIX_ROW (w->current_matrix, this_line_vpos),
14234 row->enabled_p))
14236 eassert (this_line_vpos == it.vpos);
14237 eassert (this_line_y == it.current_y);
14238 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
14239 if (cursor_row_fully_visible_p (w, false, true))
14241 #ifdef GLYPH_DEBUG
14242 *w->desired_matrix->method = 0;
14243 debug_method_add (w, "optimization 3");
14244 #endif
14245 goto update;
14247 else
14248 goto cancel;
14250 else
14251 goto cancel;
14254 cancel:
14255 /* Text changed drastically or point moved off of line. */
14256 SET_MATRIX_ROW_ENABLED_P (w->desired_matrix, this_line_vpos, false);
14259 CHARPOS (this_line_start_pos) = 0;
14260 ++clear_face_cache_count;
14261 #ifdef HAVE_WINDOW_SYSTEM
14262 ++clear_image_cache_count;
14263 #endif
14265 /* Build desired matrices, and update the display. If
14266 consider_all_windows_p, do it for all windows on all frames that
14267 require redisplay, as specified by their 'redisplay' flag.
14268 Otherwise do it for selected_window, only. */
14270 if (consider_all_windows_p)
14272 FOR_EACH_FRAME (tail, frame)
14273 XFRAME (frame)->updated_p = false;
14275 propagate_buffer_redisplay ();
14277 FOR_EACH_FRAME (tail, frame)
14279 struct frame *f = XFRAME (frame);
14281 /* We don't have to do anything for unselected terminal
14282 frames. */
14283 if ((FRAME_TERMCAP_P (f) || FRAME_MSDOS_P (f))
14284 && !EQ (FRAME_TTY (f)->top_frame, frame))
14285 continue;
14287 retry_frame:
14288 if (FRAME_WINDOW_P (f) || FRAME_TERMCAP_P (f) || f == sf)
14290 bool gcscrollbars
14291 /* Only GC scrollbars when we redisplay the whole frame. */
14292 = f->redisplay || !REDISPLAY_SOME_P ();
14293 bool f_redisplay_flag = f->redisplay;
14294 /* Mark all the scroll bars to be removed; we'll redeem
14295 the ones we want when we redisplay their windows. */
14296 if (gcscrollbars && FRAME_TERMINAL (f)->condemn_scroll_bars_hook)
14297 FRAME_TERMINAL (f)->condemn_scroll_bars_hook (f);
14299 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
14300 redisplay_windows (FRAME_ROOT_WINDOW (f));
14301 /* Remember that the invisible frames need to be redisplayed next
14302 time they're visible. */
14303 else if (!REDISPLAY_SOME_P ())
14304 f->redisplay = true;
14306 /* The X error handler may have deleted that frame. */
14307 if (!FRAME_LIVE_P (f))
14308 continue;
14310 /* Any scroll bars which redisplay_windows should have
14311 nuked should now go away. */
14312 if (gcscrollbars && FRAME_TERMINAL (f)->judge_scroll_bars_hook)
14313 FRAME_TERMINAL (f)->judge_scroll_bars_hook (f);
14315 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
14317 /* If fonts changed on visible frame, display again. */
14318 if (f->fonts_changed)
14320 adjust_frame_glyphs (f);
14321 /* Disable all redisplay optimizations for this
14322 frame. For the reasons, see the comment near
14323 the previous call to adjust_frame_glyphs above. */
14324 SET_FRAME_GARBAGED (f);
14325 f->fonts_changed = false;
14326 goto retry_frame;
14329 /* See if we have to hscroll. */
14330 if (!f->already_hscrolled_p)
14332 f->already_hscrolled_p = true;
14333 if (hscroll_retries <= MAX_HSCROLL_RETRIES
14334 && hscroll_windows (f->root_window))
14336 hscroll_retries++;
14337 goto retry_frame;
14341 /* If the frame's redisplay flag was not set before
14342 we went about redisplaying its windows, but it is
14343 set now, that means we employed some redisplay
14344 optimizations inside redisplay_windows, and
14345 bypassed producing some screen lines. But if
14346 f->redisplay is now set, it might mean the old
14347 faces are no longer valid (e.g., if redisplaying
14348 some window called some Lisp which defined a new
14349 face or redefined an existing face), so trying to
14350 use them in update_frame will segfault.
14351 Therefore, we must redisplay this frame. */
14352 if (!f_redisplay_flag && f->redisplay)
14353 goto retry_frame;
14354 /* In some case (e.g., window resize), we notice
14355 only during window updating that the window
14356 content changed unpredictably (e.g., a GTK
14357 scrollbar moved, or some Lisp hook that winds up
14358 calling adjust_frame_glyphs) and that our
14359 previous estimation of the frame content was
14360 garbage. We have to start over. These cases
14361 should be rare, so going all the way back to the
14362 top of redisplay should be good enough. */
14363 if (FRAME_GARBAGED_P (f)
14364 && garbaged_frame_retries++ < MAX_GARBAGED_FRAME_RETRIES)
14365 goto retry;
14367 #if defined (HAVE_WINDOW_SYSTEM) && !defined (HAVE_NS)
14368 x_clear_under_internal_border (f);
14369 #endif /* HAVE_WINDOW_SYSTEM && !HAVE_NS */
14371 /* Prevent various kinds of signals during display
14372 update. stdio is not robust about handling
14373 signals, which can cause an apparent I/O error. */
14374 if (interrupt_input)
14375 unrequest_sigio ();
14376 STOP_POLLING;
14378 pending |= update_frame (f, false, false);
14379 f->cursor_type_changed = false;
14380 f->updated_p = true;
14385 eassert (EQ (XFRAME (selected_frame)->selected_window, selected_window));
14387 if (!pending)
14389 /* Do the mark_window_display_accurate after all windows have
14390 been redisplayed because this call resets flags in buffers
14391 which are needed for proper redisplay. */
14392 FOR_EACH_FRAME (tail, frame)
14394 struct frame *f = XFRAME (frame);
14395 if (f->updated_p)
14397 f->redisplay = false;
14398 f->garbaged = false;
14399 mark_window_display_accurate (f->root_window, true);
14400 if (FRAME_TERMINAL (f)->frame_up_to_date_hook)
14401 FRAME_TERMINAL (f)->frame_up_to_date_hook (f);
14406 else if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
14408 displayed_buffer = XBUFFER (XWINDOW (selected_window)->contents);
14409 /* Use list_of_error, not Qerror, so that
14410 we catch only errors and don't run the debugger. */
14411 internal_condition_case_1 (redisplay_window_1, selected_window,
14412 list_of_error,
14413 redisplay_window_error);
14414 if (update_miniwindow_p)
14415 internal_condition_case_1 (redisplay_window_1,
14416 FRAME_MINIBUF_WINDOW (sf), list_of_error,
14417 redisplay_window_error);
14419 /* Compare desired and current matrices, perform output. */
14421 update:
14422 /* If fonts changed, display again. Likewise if redisplay_window_1
14423 above caused some change (e.g., a change in faces) that requires
14424 considering the entire frame again. */
14425 if (sf->fonts_changed || sf->redisplay)
14427 if (sf->redisplay)
14429 /* Set this to force a more thorough redisplay.
14430 Otherwise, we might immediately loop back to the
14431 above "else-if" clause (since all the conditions that
14432 led here might still be true), and we will then
14433 infloop, because the selected-frame's redisplay flag
14434 is not (and cannot be) reset. */
14435 windows_or_buffers_changed = 50;
14437 goto retry;
14440 /* Prevent freeing of realized faces, since desired matrices are
14441 pending that reference the faces we computed and cached. */
14442 inhibit_free_realized_faces = true;
14444 /* Prevent various kinds of signals during display update.
14445 stdio is not robust about handling signals,
14446 which can cause an apparent I/O error. */
14447 if (interrupt_input)
14448 unrequest_sigio ();
14449 STOP_POLLING;
14451 if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
14453 if (hscroll_retries <= MAX_HSCROLL_RETRIES
14454 && hscroll_windows (selected_window))
14456 hscroll_retries++;
14457 goto retry;
14460 XWINDOW (selected_window)->must_be_updated_p = true;
14461 pending = update_frame (sf, false, false);
14462 sf->cursor_type_changed = false;
14465 /* We may have called echo_area_display at the top of this
14466 function. If the echo area is on another frame, that may
14467 have put text on a frame other than the selected one, so the
14468 above call to update_frame would not have caught it. Catch
14469 it here. */
14470 Lisp_Object mini_window = FRAME_MINIBUF_WINDOW (sf);
14471 struct frame *mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
14473 if (mini_frame != sf && FRAME_WINDOW_P (mini_frame))
14475 XWINDOW (mini_window)->must_be_updated_p = true;
14476 pending |= update_frame (mini_frame, false, false);
14477 mini_frame->cursor_type_changed = false;
14478 if (!pending && hscroll_retries <= MAX_HSCROLL_RETRIES
14479 && hscroll_windows (mini_window))
14481 hscroll_retries++;
14482 goto retry;
14487 /* If display was paused because of pending input, make sure we do a
14488 thorough update the next time. */
14489 if (pending)
14491 /* Prevent the optimization at the beginning of
14492 redisplay_internal that tries a single-line update of the
14493 line containing the cursor in the selected window. */
14494 CHARPOS (this_line_start_pos) = 0;
14496 /* Let the overlay arrow be updated the next time. */
14497 update_overlay_arrows (0);
14499 /* If we pause after scrolling, some rows in the current
14500 matrices of some windows are not valid. */
14501 if (!WINDOW_FULL_WIDTH_P (w)
14502 && !FRAME_WINDOW_P (XFRAME (w->frame)))
14503 update_mode_lines = 36;
14505 else
14507 if (!consider_all_windows_p)
14509 /* This has already been done above if
14510 consider_all_windows_p is set. */
14511 if (XBUFFER (w->contents)->text->redisplay
14512 && buffer_window_count (XBUFFER (w->contents)) > 1)
14513 /* This can happen if b->text->redisplay was set during
14514 jit-lock. */
14515 propagate_buffer_redisplay ();
14516 mark_window_display_accurate_1 (w, true);
14518 /* Say overlay arrows are up to date. */
14519 update_overlay_arrows (1);
14521 if (FRAME_TERMINAL (sf)->frame_up_to_date_hook != 0)
14522 FRAME_TERMINAL (sf)->frame_up_to_date_hook (sf);
14525 update_mode_lines = 0;
14526 windows_or_buffers_changed = 0;
14529 /* Start SIGIO interrupts coming again. Having them off during the
14530 code above makes it less likely one will discard output, but not
14531 impossible, since there might be stuff in the system buffer here.
14532 But it is much hairier to try to do anything about that. */
14533 if (interrupt_input)
14534 request_sigio ();
14535 RESUME_POLLING;
14537 /* If a frame has become visible which was not before, redisplay
14538 again, so that we display it. Expose events for such a frame
14539 (which it gets when becoming visible) don't call the parts of
14540 redisplay constructing glyphs, so simply exposing a frame won't
14541 display anything in this case. So, we have to display these
14542 frames here explicitly. */
14543 if (!pending)
14545 int new_count = 0;
14547 FOR_EACH_FRAME (tail, frame)
14549 if (XFRAME (frame)->visible)
14550 new_count++;
14553 if (new_count != number_of_visible_frames)
14554 windows_or_buffers_changed = 52;
14557 /* Change frame size now if a change is pending. */
14558 do_pending_window_change (true);
14560 /* If we just did a pending size change, or have additional
14561 visible frames, or selected_window changed, redisplay again. */
14562 if ((windows_or_buffers_changed && !pending)
14563 || (WINDOWP (selected_window) && (w = XWINDOW (selected_window)) != sw))
14564 goto retry;
14566 /* Clear the face and image caches.
14568 We used to do this only if consider_all_windows_p. But the cache
14569 needs to be cleared if a timer creates images in the current
14570 buffer (e.g. the test case in Bug#6230). */
14572 if (clear_face_cache_count > CLEAR_FACE_CACHE_COUNT)
14574 clear_face_cache (false);
14575 clear_face_cache_count = 0;
14578 #ifdef HAVE_WINDOW_SYSTEM
14579 if (clear_image_cache_count > CLEAR_IMAGE_CACHE_COUNT)
14581 clear_image_caches (Qnil);
14582 clear_image_cache_count = 0;
14584 #endif /* HAVE_WINDOW_SYSTEM */
14586 end_of_redisplay:
14587 #ifdef HAVE_NS
14588 ns_set_doc_edited ();
14589 #endif
14590 if (interrupt_input && interrupts_deferred)
14591 request_sigio ();
14593 unbind_to (count, Qnil);
14594 RESUME_POLLING;
14597 static void
14598 unwind_redisplay_preserve_echo_area (void)
14600 unblock_buffer_flips ();
14603 /* Redisplay, but leave alone any recent echo area message unless
14604 another message has been requested in its place.
14606 This is useful in situations where you need to redisplay but no
14607 user action has occurred, making it inappropriate for the message
14608 area to be cleared. See tracking_off and
14609 wait_reading_process_output for examples of these situations.
14611 FROM_WHERE is an integer saying from where this function was
14612 called. This is useful for debugging. */
14614 void
14615 redisplay_preserve_echo_area (int from_where)
14617 TRACE ((stderr, "redisplay_preserve_echo_area (%d)\n", from_where));
14619 block_input ();
14620 ptrdiff_t count = SPECPDL_INDEX ();
14621 record_unwind_protect_void (unwind_redisplay_preserve_echo_area);
14622 block_buffer_flips ();
14623 unblock_input ();
14625 if (!NILP (echo_area_buffer[1]))
14627 /* We have a previously displayed message, but no current
14628 message. Redisplay the previous message. */
14629 display_last_displayed_message_p = true;
14630 redisplay_internal ();
14631 display_last_displayed_message_p = false;
14633 else
14634 redisplay_internal ();
14636 flush_frame (SELECTED_FRAME ());
14637 unbind_to (count, Qnil);
14641 /* Function registered with record_unwind_protect in redisplay_internal. */
14643 static void
14644 unwind_redisplay (void)
14646 redisplaying_p = false;
14647 unblock_buffer_flips ();
14651 /* Mark the display of leaf window W as accurate or inaccurate.
14652 If ACCURATE_P, mark display of W as accurate.
14653 If !ACCURATE_P, arrange for W to be redisplayed the next
14654 time redisplay_internal is called. */
14656 static void
14657 mark_window_display_accurate_1 (struct window *w, bool accurate_p)
14659 struct buffer *b = XBUFFER (w->contents);
14661 w->last_modified = accurate_p ? BUF_MODIFF (b) : 0;
14662 w->last_overlay_modified = accurate_p ? BUF_OVERLAY_MODIFF (b) : 0;
14663 w->last_had_star = BUF_MODIFF (b) > BUF_SAVE_MODIFF (b);
14665 if (accurate_p)
14667 b->clip_changed = false;
14668 b->prevent_redisplay_optimizations_p = false;
14669 eassert (buffer_window_count (b) > 0);
14670 /* Resetting b->text->redisplay is problematic!
14671 In order to make it safer to do it here, redisplay_internal must
14672 have copied all b->text->redisplay to their respective windows. */
14673 b->text->redisplay = false;
14675 BUF_UNCHANGED_MODIFIED (b) = BUF_MODIFF (b);
14676 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = BUF_OVERLAY_MODIFF (b);
14677 BUF_BEG_UNCHANGED (b) = BUF_GPT (b) - BUF_BEG (b);
14678 BUF_END_UNCHANGED (b) = BUF_Z (b) - BUF_GPT (b);
14680 w->current_matrix->buffer = b;
14681 w->current_matrix->begv = BUF_BEGV (b);
14682 w->current_matrix->zv = BUF_ZV (b);
14684 w->last_cursor_vpos = w->cursor.vpos;
14685 w->last_cursor_off_p = w->cursor_off_p;
14687 if (w == XWINDOW (selected_window))
14688 w->last_point = BUF_PT (b);
14689 else
14690 w->last_point = marker_position (w->pointm);
14692 w->window_end_valid = true;
14693 w->update_mode_line = false;
14696 w->redisplay = !accurate_p;
14700 /* Mark the display of windows in the window tree rooted at WINDOW as
14701 accurate or inaccurate. If ACCURATE_P, mark display of
14702 windows as accurate. If !ACCURATE_P, arrange for windows to
14703 be redisplayed the next time redisplay_internal is called. */
14705 void
14706 mark_window_display_accurate (Lisp_Object window, bool accurate_p)
14708 struct window *w;
14710 for (; !NILP (window); window = w->next)
14712 w = XWINDOW (window);
14713 if (WINDOWP (w->contents))
14714 mark_window_display_accurate (w->contents, accurate_p);
14715 else
14716 mark_window_display_accurate_1 (w, accurate_p);
14719 if (accurate_p)
14720 update_overlay_arrows (1);
14721 else
14722 /* Force a thorough redisplay the next time by setting
14723 last_arrow_position and last_arrow_string to t, which is
14724 unequal to any useful value of Voverlay_arrow_... */
14725 update_overlay_arrows (-1);
14729 /* Return value in display table DP (Lisp_Char_Table *) for character
14730 C. Since a display table doesn't have any parent, we don't have to
14731 follow parent. Do not call this function directly but use the
14732 macro DISP_CHAR_VECTOR. */
14734 Lisp_Object
14735 disp_char_vector (struct Lisp_Char_Table *dp, int c)
14737 Lisp_Object val;
14739 if (ASCII_CHAR_P (c))
14741 val = dp->ascii;
14742 if (SUB_CHAR_TABLE_P (val))
14743 val = XSUB_CHAR_TABLE (val)->contents[c];
14745 else
14747 Lisp_Object table;
14749 XSETCHAR_TABLE (table, dp);
14750 val = char_table_ref (table, c);
14752 if (NILP (val))
14753 val = dp->defalt;
14754 return val;
14757 static int buffer_flip_blocked_depth;
14759 static void
14760 block_buffer_flips (void)
14762 eassert (buffer_flip_blocked_depth >= 0);
14763 buffer_flip_blocked_depth++;
14766 static void
14767 unblock_buffer_flips (void)
14769 eassert (buffer_flip_blocked_depth > 0);
14770 if (--buffer_flip_blocked_depth == 0)
14772 Lisp_Object tail, frame;
14773 block_input ();
14774 FOR_EACH_FRAME (tail, frame)
14776 struct frame *f = XFRAME (frame);
14777 if (FRAME_TERMINAL (f)->buffer_flipping_unblocked_hook)
14778 (*FRAME_TERMINAL (f)->buffer_flipping_unblocked_hook) (f);
14780 unblock_input ();
14784 bool
14785 buffer_flipping_blocked_p (void)
14787 return buffer_flip_blocked_depth > 0;
14791 /***********************************************************************
14792 Window Redisplay
14793 ***********************************************************************/
14795 /* Redisplay all leaf windows in the window tree rooted at WINDOW. */
14797 static void
14798 redisplay_windows (Lisp_Object window)
14800 while (!NILP (window))
14802 struct window *w = XWINDOW (window);
14804 if (WINDOWP (w->contents))
14805 redisplay_windows (w->contents);
14806 else if (BUFFERP (w->contents))
14808 displayed_buffer = XBUFFER (w->contents);
14809 /* Use list_of_error, not Qerror, so that
14810 we catch only errors and don't run the debugger. */
14811 internal_condition_case_1 (redisplay_window_0, window,
14812 list_of_error,
14813 redisplay_window_error);
14816 window = w->next;
14820 static Lisp_Object
14821 redisplay_window_error (Lisp_Object ignore)
14823 displayed_buffer->display_error_modiff = BUF_MODIFF (displayed_buffer);
14824 return Qnil;
14827 static Lisp_Object
14828 redisplay_window_0 (Lisp_Object window)
14830 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
14831 redisplay_window (window, false);
14832 return Qnil;
14835 static Lisp_Object
14836 redisplay_window_1 (Lisp_Object window)
14838 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
14839 redisplay_window (window, true);
14840 return Qnil;
14844 /* Set cursor position of W. PT is assumed to be displayed in ROW.
14845 DELTA and DELTA_BYTES are the numbers of characters and bytes by
14846 which positions recorded in ROW differ from current buffer
14847 positions.
14849 Return true iff cursor is on this row. */
14851 static bool
14852 set_cursor_from_row (struct window *w, struct glyph_row *row,
14853 struct glyph_matrix *matrix,
14854 ptrdiff_t delta, ptrdiff_t delta_bytes,
14855 int dy, int dvpos)
14857 struct glyph *glyph = row->glyphs[TEXT_AREA];
14858 struct glyph *end = glyph + row->used[TEXT_AREA];
14859 struct glyph *cursor = NULL;
14860 /* The last known character position in row. */
14861 ptrdiff_t last_pos = MATRIX_ROW_START_CHARPOS (row) + delta;
14862 int x = row->x;
14863 ptrdiff_t pt_old = PT - delta;
14864 ptrdiff_t pos_before = MATRIX_ROW_START_CHARPOS (row) + delta;
14865 ptrdiff_t pos_after = MATRIX_ROW_END_CHARPOS (row) + delta;
14866 struct glyph *glyph_before = glyph - 1, *glyph_after = end;
14867 /* A glyph beyond the edge of TEXT_AREA which we should never
14868 touch. */
14869 struct glyph *glyphs_end = end;
14870 /* True means we've found a match for cursor position, but that
14871 glyph has the avoid_cursor_p flag set. */
14872 bool match_with_avoid_cursor = false;
14873 /* True means we've seen at least one glyph that came from a
14874 display string. */
14875 bool string_seen = false;
14876 /* Largest and smallest buffer positions seen so far during scan of
14877 glyph row. */
14878 ptrdiff_t bpos_max = pos_before;
14879 ptrdiff_t bpos_min = pos_after;
14880 /* Last buffer position covered by an overlay string with an integer
14881 `cursor' property. */
14882 ptrdiff_t bpos_covered = 0;
14883 /* True means the display string on which to display the cursor
14884 comes from a text property, not from an overlay. */
14885 bool string_from_text_prop = false;
14887 /* Don't even try doing anything if called for a mode-line or
14888 header-line row, since the rest of the code isn't prepared to
14889 deal with such calamities. */
14890 eassert (!row->mode_line_p);
14891 if (row->mode_line_p)
14892 return false;
14894 /* Skip over glyphs not having an object at the start and the end of
14895 the row. These are special glyphs like truncation marks on
14896 terminal frames. */
14897 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
14899 if (!row->reversed_p)
14901 while (glyph < end
14902 && NILP (glyph->object)
14903 && glyph->charpos < 0)
14905 x += glyph->pixel_width;
14906 ++glyph;
14908 while (end > glyph
14909 && NILP ((end - 1)->object)
14910 /* CHARPOS is zero for blanks and stretch glyphs
14911 inserted by extend_face_to_end_of_line. */
14912 && (end - 1)->charpos <= 0)
14913 --end;
14914 glyph_before = glyph - 1;
14915 glyph_after = end;
14917 else
14919 struct glyph *g;
14921 /* If the glyph row is reversed, we need to process it from back
14922 to front, so swap the edge pointers. */
14923 glyphs_end = end = glyph - 1;
14924 glyph += row->used[TEXT_AREA] - 1;
14926 while (glyph > end + 1
14927 && NILP (glyph->object)
14928 && glyph->charpos < 0)
14929 --glyph;
14930 if (NILP (glyph->object) && glyph->charpos < 0)
14931 --glyph;
14932 /* By default, in reversed rows we put the cursor on the
14933 rightmost (first in the reading order) glyph. */
14934 for (x = 0, g = end + 1; g < glyph; g++)
14935 x += g->pixel_width;
14936 while (end < glyph
14937 && NILP ((end + 1)->object)
14938 && (end + 1)->charpos <= 0)
14939 ++end;
14940 glyph_before = glyph + 1;
14941 glyph_after = end;
14944 else if (row->reversed_p)
14946 /* In R2L rows that don't display text, put the cursor on the
14947 rightmost glyph. Case in point: an empty last line that is
14948 part of an R2L paragraph. */
14949 cursor = end - 1;
14950 /* Avoid placing the cursor on the last glyph of the row, where
14951 on terminal frames we hold the vertical border between
14952 adjacent windows. */
14953 if (!FRAME_WINDOW_P (WINDOW_XFRAME (w))
14954 && !WINDOW_RIGHTMOST_P (w)
14955 && cursor == row->glyphs[LAST_AREA] - 1)
14956 cursor--;
14957 x = -1; /* will be computed below, at label compute_x */
14960 /* Step 1: Try to find the glyph whose character position
14961 corresponds to point. If that's not possible, find 2 glyphs
14962 whose character positions are the closest to point, one before
14963 point, the other after it. */
14964 if (!row->reversed_p)
14965 while (/* not marched to end of glyph row */
14966 glyph < end
14967 /* glyph was not inserted by redisplay for internal purposes */
14968 && !NILP (glyph->object))
14970 if (BUFFERP (glyph->object))
14972 ptrdiff_t dpos = glyph->charpos - pt_old;
14974 if (glyph->charpos > bpos_max)
14975 bpos_max = glyph->charpos;
14976 if (glyph->charpos < bpos_min)
14977 bpos_min = glyph->charpos;
14978 if (!glyph->avoid_cursor_p)
14980 /* If we hit point, we've found the glyph on which to
14981 display the cursor. */
14982 if (dpos == 0)
14984 match_with_avoid_cursor = false;
14985 break;
14987 /* See if we've found a better approximation to
14988 POS_BEFORE or to POS_AFTER. */
14989 if (0 > dpos && dpos > pos_before - pt_old)
14991 pos_before = glyph->charpos;
14992 glyph_before = glyph;
14994 else if (0 < dpos && dpos < pos_after - pt_old)
14996 pos_after = glyph->charpos;
14997 glyph_after = glyph;
15000 else if (dpos == 0)
15001 match_with_avoid_cursor = true;
15003 else if (STRINGP (glyph->object))
15005 Lisp_Object chprop;
15006 ptrdiff_t glyph_pos = glyph->charpos;
15008 chprop = Fget_char_property (make_number (glyph_pos), Qcursor,
15009 glyph->object);
15010 if (!NILP (chprop))
15012 /* If the string came from a `display' text property,
15013 look up the buffer position of that property and
15014 use that position to update bpos_max, as if we
15015 actually saw such a position in one of the row's
15016 glyphs. This helps with supporting integer values
15017 of `cursor' property on the display string in
15018 situations where most or all of the row's buffer
15019 text is completely covered by display properties,
15020 so that no glyph with valid buffer positions is
15021 ever seen in the row. */
15022 ptrdiff_t prop_pos =
15023 string_buffer_position_lim (glyph->object, pos_before,
15024 pos_after, false);
15026 if (prop_pos >= pos_before)
15027 bpos_max = prop_pos;
15029 if (INTEGERP (chprop))
15031 bpos_covered = bpos_max + XINT (chprop);
15032 /* If the `cursor' property covers buffer positions up
15033 to and including point, we should display cursor on
15034 this glyph. Note that, if a `cursor' property on one
15035 of the string's characters has an integer value, we
15036 will break out of the loop below _before_ we get to
15037 the position match above. IOW, integer values of
15038 the `cursor' property override the "exact match for
15039 point" strategy of positioning the cursor. */
15040 /* Implementation note: bpos_max == pt_old when, e.g.,
15041 we are in an empty line, where bpos_max is set to
15042 MATRIX_ROW_START_CHARPOS, see above. */
15043 if (bpos_max <= pt_old && bpos_covered >= pt_old)
15045 cursor = glyph;
15046 break;
15050 string_seen = true;
15052 x += glyph->pixel_width;
15053 ++glyph;
15055 else if (glyph > end) /* row is reversed */
15056 while (!NILP (glyph->object))
15058 if (BUFFERP (glyph->object))
15060 ptrdiff_t dpos = glyph->charpos - pt_old;
15062 if (glyph->charpos > bpos_max)
15063 bpos_max = glyph->charpos;
15064 if (glyph->charpos < bpos_min)
15065 bpos_min = glyph->charpos;
15066 if (!glyph->avoid_cursor_p)
15068 if (dpos == 0)
15070 match_with_avoid_cursor = false;
15071 break;
15073 if (0 > dpos && dpos > pos_before - pt_old)
15075 pos_before = glyph->charpos;
15076 glyph_before = glyph;
15078 else if (0 < dpos && dpos < pos_after - pt_old)
15080 pos_after = glyph->charpos;
15081 glyph_after = glyph;
15084 else if (dpos == 0)
15085 match_with_avoid_cursor = true;
15087 else if (STRINGP (glyph->object))
15089 Lisp_Object chprop;
15090 ptrdiff_t glyph_pos = glyph->charpos;
15092 chprop = Fget_char_property (make_number (glyph_pos), Qcursor,
15093 glyph->object);
15094 if (!NILP (chprop))
15096 ptrdiff_t prop_pos =
15097 string_buffer_position_lim (glyph->object, pos_before,
15098 pos_after, false);
15100 if (prop_pos >= pos_before)
15101 bpos_max = prop_pos;
15103 if (INTEGERP (chprop))
15105 bpos_covered = bpos_max + XINT (chprop);
15106 /* If the `cursor' property covers buffer positions up
15107 to and including point, we should display cursor on
15108 this glyph. */
15109 if (bpos_max <= pt_old && bpos_covered >= pt_old)
15111 cursor = glyph;
15112 break;
15115 string_seen = true;
15117 --glyph;
15118 if (glyph == glyphs_end) /* don't dereference outside TEXT_AREA */
15120 x--; /* can't use any pixel_width */
15121 break;
15123 x -= glyph->pixel_width;
15126 /* Step 2: If we didn't find an exact match for point, we need to
15127 look for a proper place to put the cursor among glyphs between
15128 GLYPH_BEFORE and GLYPH_AFTER. */
15129 if (!((row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end)
15130 && BUFFERP (glyph->object) && glyph->charpos == pt_old)
15131 && !(bpos_max <= pt_old && pt_old <= bpos_covered))
15133 /* An empty line has a single glyph whose OBJECT is nil and
15134 whose CHARPOS is the position of a newline on that line.
15135 Note that on a TTY, there are more glyphs after that, which
15136 were produced by extend_face_to_end_of_line, but their
15137 CHARPOS is zero or negative. */
15138 bool empty_line_p =
15139 ((row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end)
15140 && NILP (glyph->object) && glyph->charpos > 0
15141 /* On a TTY, continued and truncated rows also have a glyph at
15142 their end whose OBJECT is nil and whose CHARPOS is
15143 positive (the continuation and truncation glyphs), but such
15144 rows are obviously not "empty". */
15145 && !(row->continued_p || row->truncated_on_right_p));
15147 if (row->ends_in_ellipsis_p && pos_after == last_pos)
15149 ptrdiff_t ellipsis_pos;
15151 /* Scan back over the ellipsis glyphs. */
15152 if (!row->reversed_p)
15154 ellipsis_pos = (glyph - 1)->charpos;
15155 while (glyph > row->glyphs[TEXT_AREA]
15156 && (glyph - 1)->charpos == ellipsis_pos)
15157 glyph--, x -= glyph->pixel_width;
15158 /* That loop always goes one position too far, including
15159 the glyph before the ellipsis. So scan forward over
15160 that one. */
15161 x += glyph->pixel_width;
15162 glyph++;
15164 else /* row is reversed */
15166 ellipsis_pos = (glyph + 1)->charpos;
15167 while (glyph < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
15168 && (glyph + 1)->charpos == ellipsis_pos)
15169 glyph++, x += glyph->pixel_width;
15170 x -= glyph->pixel_width;
15171 glyph--;
15174 else if (match_with_avoid_cursor)
15176 cursor = glyph_after;
15177 x = -1;
15179 else if (string_seen)
15181 int incr = row->reversed_p ? -1 : +1;
15183 /* Need to find the glyph that came out of a string which is
15184 present at point. That glyph is somewhere between
15185 GLYPH_BEFORE and GLYPH_AFTER, and it came from a string
15186 positioned between POS_BEFORE and POS_AFTER in the
15187 buffer. */
15188 struct glyph *start, *stop;
15189 ptrdiff_t pos = pos_before;
15191 x = -1;
15193 /* If the row ends in a newline from a display string,
15194 reordering could have moved the glyphs belonging to the
15195 string out of the [GLYPH_BEFORE..GLYPH_AFTER] range. So
15196 in this case we extend the search to the last glyph in
15197 the row that was not inserted by redisplay. */
15198 if (row->ends_in_newline_from_string_p)
15200 glyph_after = end;
15201 pos_after = MATRIX_ROW_END_CHARPOS (row) + delta;
15204 /* GLYPH_BEFORE and GLYPH_AFTER are the glyphs that
15205 correspond to POS_BEFORE and POS_AFTER, respectively. We
15206 need START and STOP in the order that corresponds to the
15207 row's direction as given by its reversed_p flag. If the
15208 directionality of characters between POS_BEFORE and
15209 POS_AFTER is the opposite of the row's base direction,
15210 these characters will have been reordered for display,
15211 and we need to reverse START and STOP. */
15212 if (!row->reversed_p)
15214 start = min (glyph_before, glyph_after);
15215 stop = max (glyph_before, glyph_after);
15217 else
15219 start = max (glyph_before, glyph_after);
15220 stop = min (glyph_before, glyph_after);
15222 for (glyph = start + incr;
15223 row->reversed_p ? glyph > stop : glyph < stop; )
15226 /* Any glyphs that come from the buffer are here because
15227 of bidi reordering. Skip them, and only pay
15228 attention to glyphs that came from some string. */
15229 if (STRINGP (glyph->object))
15231 Lisp_Object str;
15232 ptrdiff_t tem;
15233 /* If the display property covers the newline, we
15234 need to search for it one position farther. */
15235 ptrdiff_t lim = pos_after
15236 + (pos_after == MATRIX_ROW_END_CHARPOS (row) + delta);
15238 string_from_text_prop = false;
15239 str = glyph->object;
15240 tem = string_buffer_position_lim (str, pos, lim, false);
15241 if (tem == 0 /* from overlay */
15242 || pos <= tem)
15244 /* If the string from which this glyph came is
15245 found in the buffer at point, or at position
15246 that is closer to point than pos_after, then
15247 we've found the glyph we've been looking for.
15248 If it comes from an overlay (tem == 0), and
15249 it has the `cursor' property on one of its
15250 glyphs, record that glyph as a candidate for
15251 displaying the cursor. (As in the
15252 unidirectional version, we will display the
15253 cursor on the last candidate we find.) */
15254 if (tem == 0
15255 || tem == pt_old
15256 || (tem - pt_old > 0 && tem < pos_after))
15258 /* The glyphs from this string could have
15259 been reordered. Find the one with the
15260 smallest string position. Or there could
15261 be a character in the string with the
15262 `cursor' property, which means display
15263 cursor on that character's glyph. */
15264 ptrdiff_t strpos = glyph->charpos;
15266 if (tem)
15268 cursor = glyph;
15269 string_from_text_prop = true;
15271 for ( ;
15272 (row->reversed_p ? glyph > stop : glyph < stop)
15273 && EQ (glyph->object, str);
15274 glyph += incr)
15276 Lisp_Object cprop;
15277 ptrdiff_t gpos = glyph->charpos;
15279 cprop = Fget_char_property (make_number (gpos),
15280 Qcursor,
15281 glyph->object);
15282 if (!NILP (cprop))
15284 cursor = glyph;
15285 break;
15287 if (tem && glyph->charpos < strpos)
15289 strpos = glyph->charpos;
15290 cursor = glyph;
15294 if (tem == pt_old
15295 || (tem - pt_old > 0 && tem < pos_after))
15296 goto compute_x;
15298 if (tem)
15299 pos = tem + 1; /* don't find previous instances */
15301 /* This string is not what we want; skip all of the
15302 glyphs that came from it. */
15303 while ((row->reversed_p ? glyph > stop : glyph < stop)
15304 && EQ (glyph->object, str))
15305 glyph += incr;
15307 else
15308 glyph += incr;
15311 /* If we reached the end of the line, and END was from a string,
15312 the cursor is not on this line. */
15313 if (cursor == NULL
15314 && (row->reversed_p ? glyph <= end : glyph >= end)
15315 && (row->reversed_p ? end > glyphs_end : end < glyphs_end)
15316 && STRINGP (end->object)
15317 && row->continued_p)
15318 return false;
15320 /* A truncated row may not include PT among its character positions.
15321 Setting the cursor inside the scroll margin will trigger
15322 recalculation of hscroll in hscroll_window_tree. But if a
15323 display string covers point, defer to the string-handling
15324 code below to figure this out. */
15325 else if (row->truncated_on_left_p && pt_old < bpos_min)
15327 cursor = glyph_before;
15328 x = -1;
15330 else if ((row->truncated_on_right_p && pt_old > bpos_max)
15331 /* Zero-width characters produce no glyphs. */
15332 || (!empty_line_p
15333 && (row->reversed_p
15334 ? glyph_after > glyphs_end
15335 : glyph_after < glyphs_end)))
15337 cursor = glyph_after;
15338 x = -1;
15342 compute_x:
15343 if (cursor != NULL)
15344 glyph = cursor;
15345 else if (glyph == glyphs_end
15346 && pos_before == pos_after
15347 && STRINGP ((row->reversed_p
15348 ? row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
15349 : row->glyphs[TEXT_AREA])->object))
15351 /* If all the glyphs of this row came from strings, put the
15352 cursor on the first glyph of the row. This avoids having the
15353 cursor outside of the text area in this very rare and hard
15354 use case. */
15355 glyph =
15356 row->reversed_p
15357 ? row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
15358 : row->glyphs[TEXT_AREA];
15360 if (x < 0)
15362 struct glyph *g;
15364 /* Need to compute x that corresponds to GLYPH. */
15365 for (g = row->glyphs[TEXT_AREA], x = row->x; g < glyph; g++)
15367 if (g >= row->glyphs[TEXT_AREA] + row->used[TEXT_AREA])
15368 emacs_abort ();
15369 x += g->pixel_width;
15373 /* ROW could be part of a continued line, which, under bidi
15374 reordering, might have other rows whose start and end charpos
15375 occlude point. Only set w->cursor if we found a better
15376 approximation to the cursor position than we have from previously
15377 examined candidate rows belonging to the same continued line. */
15378 if (/* We already have a candidate row. */
15379 w->cursor.vpos >= 0
15380 /* That candidate is not the row we are processing. */
15381 && MATRIX_ROW (matrix, w->cursor.vpos) != row
15382 /* Make sure cursor.vpos specifies a row whose start and end
15383 charpos occlude point, and it is valid candidate for being a
15384 cursor-row. This is because some callers of this function
15385 leave cursor.vpos at the row where the cursor was displayed
15386 during the last redisplay cycle. */
15387 && MATRIX_ROW_START_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos)) <= pt_old
15388 && pt_old <= MATRIX_ROW_END_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
15389 && cursor_row_p (MATRIX_ROW (matrix, w->cursor.vpos)))
15391 struct glyph *g1
15392 = MATRIX_ROW_GLYPH_START (matrix, w->cursor.vpos) + w->cursor.hpos;
15394 /* Don't consider glyphs that are outside TEXT_AREA. */
15395 if (!(row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end))
15396 return false;
15397 /* Keep the candidate whose buffer position is the closest to
15398 point or has the `cursor' property. */
15399 if (/* Previous candidate is a glyph in TEXT_AREA of that row. */
15400 w->cursor.hpos >= 0
15401 && w->cursor.hpos < MATRIX_ROW_USED (matrix, w->cursor.vpos)
15402 && ((BUFFERP (g1->object)
15403 && (g1->charpos == pt_old /* An exact match always wins. */
15404 || (BUFFERP (glyph->object)
15405 && eabs (g1->charpos - pt_old)
15406 < eabs (glyph->charpos - pt_old))))
15407 /* Previous candidate is a glyph from a string that has
15408 a non-nil `cursor' property. */
15409 || (STRINGP (g1->object)
15410 && (!NILP (Fget_char_property (make_number (g1->charpos),
15411 Qcursor, g1->object))
15412 /* Previous candidate is from the same display
15413 string as this one, and the display string
15414 came from a text property. */
15415 || (EQ (g1->object, glyph->object)
15416 && string_from_text_prop)
15417 /* this candidate is from newline and its
15418 position is not an exact match */
15419 || (NILP (glyph->object)
15420 && glyph->charpos != pt_old)))))
15421 return false;
15422 /* If this candidate gives an exact match, use that. */
15423 if (!((BUFFERP (glyph->object) && glyph->charpos == pt_old)
15424 /* If this candidate is a glyph created for the
15425 terminating newline of a line, and point is on that
15426 newline, it wins because it's an exact match. */
15427 || (!row->continued_p
15428 && NILP (glyph->object)
15429 && glyph->charpos == 0
15430 && pt_old == MATRIX_ROW_END_CHARPOS (row) - 1))
15431 /* Otherwise, keep the candidate that comes from a row
15432 spanning less buffer positions. This may win when one or
15433 both candidate positions are on glyphs that came from
15434 display strings, for which we cannot compare buffer
15435 positions. */
15436 && MATRIX_ROW_END_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
15437 - MATRIX_ROW_START_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
15438 < MATRIX_ROW_END_CHARPOS (row) - MATRIX_ROW_START_CHARPOS (row))
15439 return false;
15441 w->cursor.hpos = glyph - row->glyphs[TEXT_AREA];
15442 w->cursor.x = x;
15443 w->cursor.vpos = MATRIX_ROW_VPOS (row, matrix) + dvpos;
15444 w->cursor.y = row->y + dy;
15446 if (w == XWINDOW (selected_window))
15448 if (!row->continued_p
15449 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
15450 && row->x == 0)
15452 this_line_buffer = XBUFFER (w->contents);
15454 CHARPOS (this_line_start_pos)
15455 = MATRIX_ROW_START_CHARPOS (row) + delta;
15456 BYTEPOS (this_line_start_pos)
15457 = MATRIX_ROW_START_BYTEPOS (row) + delta_bytes;
15459 CHARPOS (this_line_end_pos)
15460 = Z - (MATRIX_ROW_END_CHARPOS (row) + delta);
15461 BYTEPOS (this_line_end_pos)
15462 = Z_BYTE - (MATRIX_ROW_END_BYTEPOS (row) + delta_bytes);
15464 this_line_y = w->cursor.y;
15465 this_line_pixel_height = row->height;
15466 this_line_vpos = w->cursor.vpos;
15467 this_line_start_x = row->x;
15469 else
15470 CHARPOS (this_line_start_pos) = 0;
15473 return true;
15477 /* Run window scroll functions, if any, for WINDOW with new window
15478 start STARTP. Sets the window start of WINDOW to that position.
15480 We assume that the window's buffer is really current. */
15482 static struct text_pos
15483 run_window_scroll_functions (Lisp_Object window, struct text_pos startp)
15485 struct window *w = XWINDOW (window);
15486 SET_MARKER_FROM_TEXT_POS (w->start, startp);
15488 eassert (current_buffer == XBUFFER (w->contents));
15490 if (!NILP (Vwindow_scroll_functions))
15492 run_hook_with_args_2 (Qwindow_scroll_functions, window,
15493 make_number (CHARPOS (startp)));
15494 SET_TEXT_POS_FROM_MARKER (startp, w->start);
15495 /* In case the hook functions switch buffers. */
15496 set_buffer_internal (XBUFFER (w->contents));
15499 return startp;
15503 /* Make sure the line containing the cursor is fully visible.
15504 A value of true means there is nothing to be done.
15505 (Either the line is fully visible, or it cannot be made so,
15506 or we cannot tell.)
15508 If FORCE_P, return false even if partial visible cursor row
15509 is higher than window.
15511 If CURRENT_MATRIX_P, use the information from the
15512 window's current glyph matrix; otherwise use the desired glyph
15513 matrix.
15515 A value of false means the caller should do scrolling
15516 as if point had gone off the screen. */
15518 static bool
15519 cursor_row_fully_visible_p (struct window *w, bool force_p,
15520 bool current_matrix_p)
15522 struct glyph_matrix *matrix;
15523 struct glyph_row *row;
15524 int window_height;
15526 if (!make_cursor_line_fully_visible_p)
15527 return true;
15529 /* It's not always possible to find the cursor, e.g, when a window
15530 is full of overlay strings. Don't do anything in that case. */
15531 if (w->cursor.vpos < 0)
15532 return true;
15534 matrix = current_matrix_p ? w->current_matrix : w->desired_matrix;
15535 row = MATRIX_ROW (matrix, w->cursor.vpos);
15537 /* If the cursor row is not partially visible, there's nothing to do. */
15538 if (!MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row))
15539 return true;
15541 /* If the row the cursor is in is taller than the window's height,
15542 it's not clear what to do, so do nothing. */
15543 window_height = window_box_height (w);
15544 if (row->height >= window_height)
15546 if (!force_p || MINI_WINDOW_P (w)
15547 || w->vscroll || w->cursor.vpos == 0)
15548 return true;
15550 return false;
15554 /* Try scrolling PT into view in window WINDOW. JUST_THIS_ONE_P
15555 means only WINDOW is redisplayed in redisplay_internal.
15556 TEMP_SCROLL_STEP has the same meaning as emacs_scroll_step, and is used
15557 in redisplay_window to bring a partially visible line into view in
15558 the case that only the cursor has moved.
15560 LAST_LINE_MISFIT should be true if we're scrolling because the
15561 last screen line's vertical height extends past the end of the screen.
15563 Value is
15565 1 if scrolling succeeded
15567 0 if scrolling didn't find point.
15569 -1 if new fonts have been loaded so that we must interrupt
15570 redisplay, adjust glyph matrices, and try again. */
15572 enum
15574 SCROLLING_SUCCESS,
15575 SCROLLING_FAILED,
15576 SCROLLING_NEED_LARGER_MATRICES
15579 /* If scroll-conservatively is more than this, never recenter.
15581 If you change this, don't forget to update the doc string of
15582 `scroll-conservatively' and the Emacs manual. */
15583 #define SCROLL_LIMIT 100
15585 static int
15586 try_scrolling (Lisp_Object window, bool just_this_one_p,
15587 ptrdiff_t arg_scroll_conservatively, ptrdiff_t scroll_step,
15588 bool temp_scroll_step, bool last_line_misfit)
15590 struct window *w = XWINDOW (window);
15591 struct text_pos pos, startp;
15592 struct it it;
15593 int this_scroll_margin, scroll_max, rc, height;
15594 int dy = 0, amount_to_scroll = 0;
15595 bool scroll_down_p = false;
15596 int extra_scroll_margin_lines = last_line_misfit;
15597 Lisp_Object aggressive;
15598 /* We will never try scrolling more than this number of lines. */
15599 int scroll_limit = SCROLL_LIMIT;
15600 int frame_line_height = default_line_pixel_height (w);
15602 #ifdef GLYPH_DEBUG
15603 debug_method_add (w, "try_scrolling");
15604 #endif
15606 SET_TEXT_POS_FROM_MARKER (startp, w->start);
15608 this_scroll_margin = window_scroll_margin (w, MARGIN_IN_PIXELS);
15610 /* Force arg_scroll_conservatively to have a reasonable value, to
15611 avoid scrolling too far away with slow move_it_* functions. Note
15612 that the user can supply scroll-conservatively equal to
15613 `most-positive-fixnum', which can be larger than INT_MAX. */
15614 if (arg_scroll_conservatively > scroll_limit)
15616 arg_scroll_conservatively = scroll_limit + 1;
15617 scroll_max = scroll_limit * frame_line_height;
15619 else if (scroll_step || arg_scroll_conservatively || temp_scroll_step)
15620 /* Compute how much we should try to scroll maximally to bring
15621 point into view. */
15622 scroll_max = (max (scroll_step,
15623 max (arg_scroll_conservatively, temp_scroll_step))
15624 * frame_line_height);
15625 else if (NUMBERP (BVAR (current_buffer, scroll_down_aggressively))
15626 || NUMBERP (BVAR (current_buffer, scroll_up_aggressively)))
15627 /* We're trying to scroll because of aggressive scrolling but no
15628 scroll_step is set. Choose an arbitrary one. */
15629 scroll_max = 10 * frame_line_height;
15630 else
15631 scroll_max = 0;
15633 too_near_end:
15635 /* Decide whether to scroll down. */
15636 if (PT > CHARPOS (startp))
15638 int scroll_margin_y;
15640 /* Compute the pixel ypos of the scroll margin, then move IT to
15641 either that ypos or PT, whichever comes first. */
15642 start_display (&it, w, startp);
15643 scroll_margin_y = it.last_visible_y - partial_line_height (&it)
15644 - this_scroll_margin
15645 - frame_line_height * extra_scroll_margin_lines;
15646 move_it_to (&it, PT, -1, scroll_margin_y - 1, -1,
15647 (MOVE_TO_POS | MOVE_TO_Y));
15649 if (PT > CHARPOS (it.current.pos))
15651 int y0 = line_bottom_y (&it);
15652 /* Compute how many pixels below window bottom to stop searching
15653 for PT. This avoids costly search for PT that is far away if
15654 the user limited scrolling by a small number of lines, but
15655 always finds PT if scroll_conservatively is set to a large
15656 number, such as most-positive-fixnum. */
15657 int slack = max (scroll_max, 10 * frame_line_height);
15658 int y_to_move = it.last_visible_y + slack;
15660 /* Compute the distance from the scroll margin to PT or to
15661 the scroll limit, whichever comes first. This should
15662 include the height of the cursor line, to make that line
15663 fully visible. */
15664 move_it_to (&it, PT, -1, y_to_move,
15665 -1, MOVE_TO_POS | MOVE_TO_Y);
15666 dy = line_bottom_y (&it) - y0;
15668 if (dy > scroll_max)
15669 return SCROLLING_FAILED;
15671 if (dy > 0)
15672 scroll_down_p = true;
15674 else if (PT == IT_CHARPOS (it)
15675 && IT_CHARPOS (it) < ZV
15676 && it.method == GET_FROM_STRING
15677 && arg_scroll_conservatively > scroll_limit
15678 && it.current_x == 0)
15680 enum move_it_result skip;
15681 int y1 = it.current_y;
15682 int vpos;
15684 /* A before-string that includes newlines and is displayed
15685 on the last visible screen line could fail us under
15686 scroll-conservatively > 100, because we will be unable to
15687 position the cursor on that last visible line. Try to
15688 recover by finding the first screen line that has some
15689 glyphs coming from the buffer text. */
15690 do {
15691 skip = move_it_in_display_line_to (&it, ZV, -1, MOVE_TO_POS);
15692 if (skip != MOVE_NEWLINE_OR_CR
15693 || IT_CHARPOS (it) != PT
15694 || it.method == GET_FROM_BUFFER)
15695 break;
15696 vpos = it.vpos;
15697 move_it_to (&it, -1, -1, -1, vpos + 1, MOVE_TO_VPOS);
15698 } while (it.vpos > vpos);
15700 dy = it.current_y - y1;
15702 if (dy > scroll_max)
15703 return SCROLLING_FAILED;
15705 if (dy > 0)
15706 scroll_down_p = true;
15710 if (scroll_down_p)
15712 /* Point is in or below the bottom scroll margin, so move the
15713 window start down. If scrolling conservatively, move it just
15714 enough down to make point visible. If scroll_step is set,
15715 move it down by scroll_step. */
15716 if (arg_scroll_conservatively)
15717 amount_to_scroll
15718 = min (max (dy, frame_line_height),
15719 frame_line_height * arg_scroll_conservatively);
15720 else if (scroll_step || temp_scroll_step)
15721 amount_to_scroll = scroll_max;
15722 else
15724 aggressive = BVAR (current_buffer, scroll_up_aggressively);
15725 height = WINDOW_BOX_TEXT_HEIGHT (w);
15726 if (NUMBERP (aggressive))
15728 double float_amount = XFLOATINT (aggressive) * height;
15729 int aggressive_scroll = float_amount;
15730 if (aggressive_scroll == 0 && float_amount > 0)
15731 aggressive_scroll = 1;
15732 /* Don't let point enter the scroll margin near top of
15733 the window. This could happen if the value of
15734 scroll_up_aggressively is too large and there are
15735 non-zero margins, because scroll_up_aggressively
15736 means put point that fraction of window height
15737 _from_the_bottom_margin_. */
15738 if (aggressive_scroll + 2 * this_scroll_margin > height)
15739 aggressive_scroll = height - 2 * this_scroll_margin;
15740 amount_to_scroll = dy + aggressive_scroll;
15744 if (amount_to_scroll <= 0)
15745 return SCROLLING_FAILED;
15747 start_display (&it, w, startp);
15748 if (arg_scroll_conservatively <= scroll_limit)
15749 move_it_vertically (&it, amount_to_scroll);
15750 else
15752 /* Extra precision for users who set scroll-conservatively
15753 to a large number: make sure the amount we scroll
15754 the window start is never less than amount_to_scroll,
15755 which was computed as distance from window bottom to
15756 point. This matters when lines at window top and lines
15757 below window bottom have different height. */
15758 struct it it1;
15759 void *it1data = NULL;
15760 /* We use a temporary it1 because line_bottom_y can modify
15761 its argument, if it moves one line down; see there. */
15762 int start_y;
15764 SAVE_IT (it1, it, it1data);
15765 start_y = line_bottom_y (&it1);
15766 do {
15767 RESTORE_IT (&it, &it, it1data);
15768 move_it_by_lines (&it, 1);
15769 SAVE_IT (it1, it, it1data);
15770 } while (IT_CHARPOS (it) < ZV
15771 && line_bottom_y (&it1) - start_y < amount_to_scroll);
15772 bidi_unshelve_cache (it1data, true);
15775 /* If STARTP is unchanged, move it down another screen line. */
15776 if (IT_CHARPOS (it) == CHARPOS (startp))
15777 move_it_by_lines (&it, 1);
15778 startp = it.current.pos;
15780 else
15782 struct text_pos scroll_margin_pos = startp;
15783 int y_offset = 0;
15785 /* See if point is inside the scroll margin at the top of the
15786 window. */
15787 if (this_scroll_margin)
15789 int y_start;
15791 start_display (&it, w, startp);
15792 y_start = it.current_y;
15793 move_it_vertically (&it, this_scroll_margin);
15794 scroll_margin_pos = it.current.pos;
15795 /* If we didn't move enough before hitting ZV, request
15796 additional amount of scroll, to move point out of the
15797 scroll margin. */
15798 if (IT_CHARPOS (it) == ZV
15799 && it.current_y - y_start < this_scroll_margin)
15800 y_offset = this_scroll_margin - (it.current_y - y_start);
15803 if (PT < CHARPOS (scroll_margin_pos))
15805 /* Point is in the scroll margin at the top of the window or
15806 above what is displayed in the window. */
15807 int y0, y_to_move;
15809 /* Compute the vertical distance from PT to the scroll
15810 margin position. Move as far as scroll_max allows, or
15811 one screenful, or 10 screen lines, whichever is largest.
15812 Give up if distance is greater than scroll_max or if we
15813 didn't reach the scroll margin position. */
15814 SET_TEXT_POS (pos, PT, PT_BYTE);
15815 start_display (&it, w, pos);
15816 y0 = it.current_y;
15817 y_to_move = max (it.last_visible_y,
15818 max (scroll_max, 10 * frame_line_height));
15819 move_it_to (&it, CHARPOS (scroll_margin_pos), 0,
15820 y_to_move, -1,
15821 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
15822 dy = it.current_y - y0;
15823 if (dy > scroll_max
15824 || IT_CHARPOS (it) < CHARPOS (scroll_margin_pos))
15825 return SCROLLING_FAILED;
15827 /* Additional scroll for when ZV was too close to point. */
15828 dy += y_offset;
15830 /* Compute new window start. */
15831 start_display (&it, w, startp);
15833 if (arg_scroll_conservatively)
15834 amount_to_scroll = max (dy, frame_line_height
15835 * max (scroll_step, temp_scroll_step));
15836 else if (scroll_step || temp_scroll_step)
15837 amount_to_scroll = scroll_max;
15838 else
15840 aggressive = BVAR (current_buffer, scroll_down_aggressively);
15841 height = WINDOW_BOX_TEXT_HEIGHT (w);
15842 if (NUMBERP (aggressive))
15844 double float_amount = XFLOATINT (aggressive) * height;
15845 int aggressive_scroll = float_amount;
15846 if (aggressive_scroll == 0 && float_amount > 0)
15847 aggressive_scroll = 1;
15848 /* Don't let point enter the scroll margin near
15849 bottom of the window, if the value of
15850 scroll_down_aggressively happens to be too
15851 large. */
15852 if (aggressive_scroll + 2 * this_scroll_margin > height)
15853 aggressive_scroll = height - 2 * this_scroll_margin;
15854 amount_to_scroll = dy + aggressive_scroll;
15858 if (amount_to_scroll <= 0)
15859 return SCROLLING_FAILED;
15861 move_it_vertically_backward (&it, amount_to_scroll);
15862 startp = it.current.pos;
15866 /* Run window scroll functions. */
15867 startp = run_window_scroll_functions (window, startp);
15869 /* Display the window. Give up if new fonts are loaded, or if point
15870 doesn't appear. */
15871 if (!try_window (window, startp, 0))
15872 rc = SCROLLING_NEED_LARGER_MATRICES;
15873 else if (w->cursor.vpos < 0)
15875 clear_glyph_matrix (w->desired_matrix);
15876 rc = SCROLLING_FAILED;
15878 else
15880 /* Maybe forget recorded base line for line number display. */
15881 if (!just_this_one_p
15882 || current_buffer->clip_changed
15883 || BEG_UNCHANGED < CHARPOS (startp))
15884 w->base_line_number = 0;
15886 /* If cursor ends up on a partially visible line,
15887 treat that as being off the bottom of the screen. */
15888 if (! cursor_row_fully_visible_p (w, extra_scroll_margin_lines <= 1,
15889 false)
15890 /* It's possible that the cursor is on the first line of the
15891 buffer, which is partially obscured due to a vscroll
15892 (Bug#7537). In that case, avoid looping forever. */
15893 && extra_scroll_margin_lines < w->desired_matrix->nrows - 1)
15895 clear_glyph_matrix (w->desired_matrix);
15896 ++extra_scroll_margin_lines;
15897 goto too_near_end;
15899 rc = SCROLLING_SUCCESS;
15902 return rc;
15906 /* Compute a suitable window start for window W if display of W starts
15907 on a continuation line. Value is true if a new window start
15908 was computed.
15910 The new window start will be computed, based on W's width, starting
15911 from the start of the continued line. It is the start of the
15912 screen line with the minimum distance from the old start W->start,
15913 which is still before point (otherwise point will definitely not
15914 be visible in the window). */
15916 static bool
15917 compute_window_start_on_continuation_line (struct window *w)
15919 struct text_pos pos, start_pos, pos_before_pt;
15920 bool window_start_changed_p = false;
15922 SET_TEXT_POS_FROM_MARKER (start_pos, w->start);
15924 /* If window start is on a continuation line... Window start may be
15925 < BEGV in case there's invisible text at the start of the
15926 buffer (M-x rmail, for example). */
15927 if (CHARPOS (start_pos) > BEGV
15928 && FETCH_BYTE (BYTEPOS (start_pos) - 1) != '\n')
15930 struct it it;
15931 struct glyph_row *row;
15933 /* Handle the case that the window start is out of range. */
15934 if (CHARPOS (start_pos) < BEGV)
15935 SET_TEXT_POS (start_pos, BEGV, BEGV_BYTE);
15936 else if (CHARPOS (start_pos) > ZV)
15937 SET_TEXT_POS (start_pos, ZV, ZV_BYTE);
15939 /* Find the start of the continued line. This should be fast
15940 because find_newline is fast (newline cache). */
15941 row = w->desired_matrix->rows + window_wants_header_line (w);
15942 init_iterator (&it, w, CHARPOS (start_pos), BYTEPOS (start_pos),
15943 row, DEFAULT_FACE_ID);
15944 reseat_at_previous_visible_line_start (&it);
15946 /* If the line start is "too far" away from the window start,
15947 say it takes too much time to compute a new window start.
15948 Also, give up if the line start is after point, as in that
15949 case point will not be visible with any window start we
15950 compute. */
15951 if (IT_CHARPOS (it) <= PT
15952 || (CHARPOS (start_pos) - IT_CHARPOS (it)
15953 /* PXW: Do we need upper bounds here? */
15954 < WINDOW_TOTAL_LINES (w) * WINDOW_TOTAL_COLS (w)))
15956 int min_distance, distance;
15958 /* Move forward by display lines to find the new window
15959 start. If window width was enlarged, the new start can
15960 be expected to be > the old start. If window width was
15961 decreased, the new window start will be < the old start.
15962 So, we're looking for the display line start with the
15963 minimum distance from the old window start. */
15964 pos_before_pt = pos = it.current.pos;
15965 min_distance = DISP_INFINITY;
15966 while ((distance = eabs (CHARPOS (start_pos) - IT_CHARPOS (it))),
15967 distance < min_distance)
15969 min_distance = distance;
15970 if (CHARPOS (pos) <= PT)
15971 pos_before_pt = pos;
15972 pos = it.current.pos;
15973 if (it.line_wrap == WORD_WRAP)
15975 /* Under WORD_WRAP, move_it_by_lines is likely to
15976 overshoot and stop not at the first, but the
15977 second character from the left margin. So in
15978 that case, we need a more tight control on the X
15979 coordinate of the iterator than move_it_by_lines
15980 promises in its contract. The method is to first
15981 go to the last (rightmost) visible character of a
15982 line, then move to the leftmost character on the
15983 next line in a separate call. */
15984 move_it_to (&it, ZV, it.last_visible_x, it.current_y, -1,
15985 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
15986 move_it_to (&it, ZV, 0,
15987 it.current_y + it.max_ascent + it.max_descent, -1,
15988 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
15990 else
15991 move_it_by_lines (&it, 1);
15994 /* It makes very little sense to make the new window start
15995 after point, as point won't be visible. If that's what
15996 the loop above finds, fall back on the candidate before
15997 or at point that is closest to the old window start. */
15998 if (CHARPOS (pos) > PT)
15999 pos = pos_before_pt;
16001 /* Set the window start there. */
16002 SET_MARKER_FROM_TEXT_POS (w->start, pos);
16003 window_start_changed_p = true;
16007 return window_start_changed_p;
16011 /* Try cursor movement in case text has not changed in window WINDOW,
16012 with window start STARTP. Value is
16014 CURSOR_MOVEMENT_SUCCESS if successful
16016 CURSOR_MOVEMENT_CANNOT_BE_USED if this method cannot be used
16018 CURSOR_MOVEMENT_MUST_SCROLL if we know we have to scroll the
16019 display. *SCROLL_STEP is set to true, under certain circumstances, if
16020 we want to scroll as if scroll-step were set to 1. See the code.
16022 CURSOR_MOVEMENT_NEED_LARGER_MATRICES if we need larger matrices, in
16023 which case we have to abort this redisplay, and adjust matrices
16024 first. */
16026 enum
16028 CURSOR_MOVEMENT_SUCCESS,
16029 CURSOR_MOVEMENT_CANNOT_BE_USED,
16030 CURSOR_MOVEMENT_MUST_SCROLL,
16031 CURSOR_MOVEMENT_NEED_LARGER_MATRICES
16034 static int
16035 try_cursor_movement (Lisp_Object window, struct text_pos startp,
16036 bool *scroll_step)
16038 struct window *w = XWINDOW (window);
16039 struct frame *f = XFRAME (w->frame);
16040 int rc = CURSOR_MOVEMENT_CANNOT_BE_USED;
16042 #ifdef GLYPH_DEBUG
16043 if (inhibit_try_cursor_movement)
16044 return rc;
16045 #endif
16047 /* Previously, there was a check for Lisp integer in the
16048 if-statement below. Now, this field is converted to
16049 ptrdiff_t, thus zero means invalid position in a buffer. */
16050 eassert (w->last_point > 0);
16051 /* Likewise there was a check whether window_end_vpos is nil or larger
16052 than the window. Now window_end_vpos is int and so never nil, but
16053 let's leave eassert to check whether it fits in the window. */
16054 eassert (!w->window_end_valid
16055 || w->window_end_vpos < w->current_matrix->nrows);
16057 /* Handle case where text has not changed, only point, and it has
16058 not moved off the frame. */
16059 if (/* Point may be in this window. */
16060 PT >= CHARPOS (startp)
16061 /* Selective display hasn't changed. */
16062 && !current_buffer->clip_changed
16063 /* Function force-mode-line-update is used to force a thorough
16064 redisplay. It sets either windows_or_buffers_changed or
16065 update_mode_lines. So don't take a shortcut here for these
16066 cases. */
16067 && !update_mode_lines
16068 && !windows_or_buffers_changed
16069 && !f->cursor_type_changed
16070 && NILP (Vshow_trailing_whitespace)
16071 /* When display-line-numbers is in relative mode, moving point
16072 requires to redraw the entire window. */
16073 && !EQ (Vdisplay_line_numbers, Qrelative)
16074 && !EQ (Vdisplay_line_numbers, Qvisual)
16075 /* When the current line number should be displayed in a
16076 distinct face, moving point cannot be handled in optimized
16077 way as below. */
16078 && !(!NILP (Vdisplay_line_numbers)
16079 && NILP (Finternal_lisp_face_equal_p (Qline_number,
16080 Qline_number_current_line,
16081 w->frame)))
16082 /* This code is not used for mini-buffer for the sake of the case
16083 of redisplaying to replace an echo area message; since in
16084 that case the mini-buffer contents per se are usually
16085 unchanged. This code is of no real use in the mini-buffer
16086 since the handling of this_line_start_pos, etc., in redisplay
16087 handles the same cases. */
16088 && !EQ (window, minibuf_window)
16089 /* When overlay arrow is shown in current buffer, point movement
16090 is no longer "simple", as it typically causes the overlay
16091 arrow to move as well. */
16092 && !overlay_arrow_in_current_buffer_p ())
16094 int this_scroll_margin, top_scroll_margin;
16095 struct glyph_row *row = NULL;
16097 #ifdef GLYPH_DEBUG
16098 debug_method_add (w, "cursor movement");
16099 #endif
16101 this_scroll_margin = window_scroll_margin (w, MARGIN_IN_PIXELS);
16103 top_scroll_margin = this_scroll_margin;
16104 if (window_wants_header_line (w))
16105 top_scroll_margin += CURRENT_HEADER_LINE_HEIGHT (w);
16107 /* Start with the row the cursor was displayed during the last
16108 not paused redisplay. Give up if that row is not valid. */
16109 if (w->last_cursor_vpos < 0
16110 || w->last_cursor_vpos >= w->current_matrix->nrows)
16111 rc = CURSOR_MOVEMENT_MUST_SCROLL;
16112 else
16114 row = MATRIX_ROW (w->current_matrix, w->last_cursor_vpos);
16115 if (row->mode_line_p)
16116 ++row;
16117 if (!row->enabled_p)
16118 rc = CURSOR_MOVEMENT_MUST_SCROLL;
16121 if (rc == CURSOR_MOVEMENT_CANNOT_BE_USED)
16123 bool scroll_p = false, must_scroll = false;
16124 int last_y = window_text_bottom_y (w) - this_scroll_margin;
16126 if (PT > w->last_point)
16128 /* Point has moved forward. */
16129 while (MATRIX_ROW_END_CHARPOS (row) < PT
16130 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
16132 eassert (row->enabled_p);
16133 ++row;
16136 /* If the end position of a row equals the start
16137 position of the next row, and PT is at that position,
16138 we would rather display cursor in the next line. */
16139 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
16140 && MATRIX_ROW_END_CHARPOS (row) == PT
16141 && row < MATRIX_MODE_LINE_ROW (w->current_matrix)
16142 && MATRIX_ROW_START_CHARPOS (row+1) == PT
16143 && !cursor_row_p (row))
16144 ++row;
16146 /* If within the scroll margin, scroll. Note that
16147 MATRIX_ROW_BOTTOM_Y gives the pixel position at which
16148 the next line would be drawn, and that
16149 this_scroll_margin can be zero. */
16150 if (MATRIX_ROW_BOTTOM_Y (row) > last_y
16151 || PT > MATRIX_ROW_END_CHARPOS (row)
16152 /* Line is completely visible last line in window
16153 and PT is to be set in the next line. */
16154 || (MATRIX_ROW_BOTTOM_Y (row) == last_y
16155 && PT == MATRIX_ROW_END_CHARPOS (row)
16156 && !row->ends_at_zv_p
16157 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
16158 scroll_p = true;
16160 else if (PT < w->last_point)
16162 /* Cursor has to be moved backward. Note that PT >=
16163 CHARPOS (startp) because of the outer if-statement. */
16164 while (!row->mode_line_p
16165 && (MATRIX_ROW_START_CHARPOS (row) > PT
16166 || (MATRIX_ROW_START_CHARPOS (row) == PT
16167 && (MATRIX_ROW_STARTS_IN_MIDDLE_OF_CHAR_P (row)
16168 || (/* STARTS_IN_MIDDLE_OF_STRING_P (row) */
16169 row > w->current_matrix->rows
16170 && (row-1)->ends_in_newline_from_string_p))))
16171 && (row->y > top_scroll_margin
16172 || CHARPOS (startp) == BEGV))
16174 eassert (row->enabled_p);
16175 --row;
16178 /* Consider the following case: Window starts at BEGV,
16179 there is invisible, intangible text at BEGV, so that
16180 display starts at some point START > BEGV. It can
16181 happen that we are called with PT somewhere between
16182 BEGV and START. Try to handle that case. */
16183 if (row < w->current_matrix->rows
16184 || row->mode_line_p)
16186 row = w->current_matrix->rows;
16187 if (row->mode_line_p)
16188 ++row;
16191 /* Due to newlines in overlay strings, we may have to
16192 skip forward over overlay strings. */
16193 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
16194 && MATRIX_ROW_END_CHARPOS (row) == PT
16195 && !cursor_row_p (row))
16196 ++row;
16198 /* If within the scroll margin, scroll. */
16199 if (row->y < top_scroll_margin
16200 && CHARPOS (startp) != BEGV)
16201 scroll_p = true;
16203 else
16205 /* Cursor did not move. So don't scroll even if cursor line
16206 is partially visible, as it was so before. */
16207 rc = CURSOR_MOVEMENT_SUCCESS;
16210 if (PT < MATRIX_ROW_START_CHARPOS (row)
16211 || PT > MATRIX_ROW_END_CHARPOS (row))
16213 /* if PT is not in the glyph row, give up. */
16214 rc = CURSOR_MOVEMENT_MUST_SCROLL;
16215 must_scroll = true;
16217 else if (rc != CURSOR_MOVEMENT_SUCCESS
16218 && !NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering)))
16220 struct glyph_row *row1;
16222 /* If rows are bidi-reordered and point moved, back up
16223 until we find a row that does not belong to a
16224 continuation line. This is because we must consider
16225 all rows of a continued line as candidates for the
16226 new cursor positioning, since row start and end
16227 positions change non-linearly with vertical position
16228 in such rows. */
16229 /* FIXME: Revisit this when glyph ``spilling'' in
16230 continuation lines' rows is implemented for
16231 bidi-reordered rows. */
16232 for (row1 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
16233 MATRIX_ROW_CONTINUATION_LINE_P (row);
16234 --row)
16236 /* If we hit the beginning of the displayed portion
16237 without finding the first row of a continued
16238 line, give up. */
16239 if (row <= row1)
16241 rc = CURSOR_MOVEMENT_MUST_SCROLL;
16242 break;
16244 eassert (row->enabled_p);
16247 if (must_scroll)
16249 else if (rc != CURSOR_MOVEMENT_SUCCESS
16250 && MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row)
16251 /* Make sure this isn't a header line by any chance, since
16252 then MATRIX_ROW_PARTIALLY_VISIBLE_P might yield true. */
16253 && !row->mode_line_p
16254 && make_cursor_line_fully_visible_p)
16256 if (PT == MATRIX_ROW_END_CHARPOS (row)
16257 && !row->ends_at_zv_p
16258 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
16259 rc = CURSOR_MOVEMENT_MUST_SCROLL;
16260 else if (row->height > window_box_height (w))
16262 /* If we end up in a partially visible line, let's
16263 make it fully visible, except when it's taller
16264 than the window, in which case we can't do much
16265 about it. */
16266 *scroll_step = true;
16267 rc = CURSOR_MOVEMENT_MUST_SCROLL;
16269 else
16271 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
16272 if (!cursor_row_fully_visible_p (w, false, true))
16273 rc = CURSOR_MOVEMENT_MUST_SCROLL;
16274 else
16275 rc = CURSOR_MOVEMENT_SUCCESS;
16278 else if (scroll_p)
16279 rc = CURSOR_MOVEMENT_MUST_SCROLL;
16280 else if (rc != CURSOR_MOVEMENT_SUCCESS
16281 && !NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering)))
16283 /* With bidi-reordered rows, there could be more than
16284 one candidate row whose start and end positions
16285 occlude point. We need to let set_cursor_from_row
16286 find the best candidate. */
16287 /* FIXME: Revisit this when glyph ``spilling'' in
16288 continuation lines' rows is implemented for
16289 bidi-reordered rows. */
16290 bool rv = false;
16294 bool at_zv_p = false, exact_match_p = false;
16296 if (MATRIX_ROW_START_CHARPOS (row) <= PT
16297 && PT <= MATRIX_ROW_END_CHARPOS (row)
16298 && cursor_row_p (row))
16299 rv |= set_cursor_from_row (w, row, w->current_matrix,
16300 0, 0, 0, 0);
16301 /* As soon as we've found the exact match for point,
16302 or the first suitable row whose ends_at_zv_p flag
16303 is set, we are done. */
16304 if (rv)
16306 at_zv_p = MATRIX_ROW (w->current_matrix,
16307 w->cursor.vpos)->ends_at_zv_p;
16308 if (!at_zv_p
16309 && w->cursor.hpos >= 0
16310 && w->cursor.hpos < MATRIX_ROW_USED (w->current_matrix,
16311 w->cursor.vpos))
16313 struct glyph_row *candidate =
16314 MATRIX_ROW (w->current_matrix, w->cursor.vpos);
16315 struct glyph *g =
16316 candidate->glyphs[TEXT_AREA] + w->cursor.hpos;
16317 ptrdiff_t endpos = MATRIX_ROW_END_CHARPOS (candidate);
16319 exact_match_p =
16320 (BUFFERP (g->object) && g->charpos == PT)
16321 || (NILP (g->object)
16322 && (g->charpos == PT
16323 || (g->charpos == 0 && endpos - 1 == PT)));
16325 if (at_zv_p || exact_match_p)
16327 rc = CURSOR_MOVEMENT_SUCCESS;
16328 break;
16331 if (MATRIX_ROW_BOTTOM_Y (row) == last_y)
16332 break;
16333 ++row;
16335 while (((MATRIX_ROW_CONTINUATION_LINE_P (row)
16336 || row->continued_p)
16337 && MATRIX_ROW_BOTTOM_Y (row) <= last_y)
16338 || (MATRIX_ROW_START_CHARPOS (row) == PT
16339 && MATRIX_ROW_BOTTOM_Y (row) < last_y));
16340 /* If we didn't find any candidate rows, or exited the
16341 loop before all the candidates were examined, signal
16342 to the caller that this method failed. */
16343 if (rc != CURSOR_MOVEMENT_SUCCESS
16344 && !(rv
16345 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
16346 && !row->continued_p))
16347 rc = CURSOR_MOVEMENT_MUST_SCROLL;
16348 else if (rv)
16349 rc = CURSOR_MOVEMENT_SUCCESS;
16351 else
16355 if (set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0))
16357 rc = CURSOR_MOVEMENT_SUCCESS;
16358 break;
16360 ++row;
16362 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
16363 && MATRIX_ROW_START_CHARPOS (row) == PT
16364 && cursor_row_p (row));
16369 return rc;
16373 void
16374 set_vertical_scroll_bar (struct window *w)
16376 ptrdiff_t start, end, whole;
16378 /* Calculate the start and end positions for the current window.
16379 At some point, it would be nice to choose between scrollbars
16380 which reflect the whole buffer size, with special markers
16381 indicating narrowing, and scrollbars which reflect only the
16382 visible region.
16384 Note that mini-buffers sometimes aren't displaying any text. */
16385 if (!MINI_WINDOW_P (w)
16386 || (w == XWINDOW (minibuf_window)
16387 && NILP (echo_area_buffer[0])))
16389 struct buffer *buf = XBUFFER (w->contents);
16390 whole = BUF_ZV (buf) - BUF_BEGV (buf);
16391 start = marker_position (w->start) - BUF_BEGV (buf);
16392 /* I don't think this is guaranteed to be right. For the
16393 moment, we'll pretend it is. */
16394 end = BUF_Z (buf) - w->window_end_pos - BUF_BEGV (buf);
16396 if (end < start)
16397 end = start;
16398 if (whole < (end - start))
16399 whole = end - start;
16401 else
16402 start = end = whole = 0;
16404 /* Indicate what this scroll bar ought to be displaying now. */
16405 if (FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
16406 (*FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
16407 (w, end - start, whole, start);
16411 void
16412 set_horizontal_scroll_bar (struct window *w)
16414 int start, end, whole, portion;
16416 if (!MINI_WINDOW_P (w)
16417 || (w == XWINDOW (minibuf_window)
16418 && NILP (echo_area_buffer[0])))
16420 struct buffer *b = XBUFFER (w->contents);
16421 struct buffer *old_buffer = NULL;
16422 struct it it;
16423 struct text_pos startp;
16425 if (b != current_buffer)
16427 old_buffer = current_buffer;
16428 set_buffer_internal (b);
16431 SET_TEXT_POS_FROM_MARKER (startp, w->start);
16432 start_display (&it, w, startp);
16433 it.last_visible_x = INT_MAX;
16434 whole = move_it_to (&it, -1, INT_MAX, window_box_height (w), -1,
16435 MOVE_TO_X | MOVE_TO_Y);
16436 /* whole = move_it_to (&it, w->window_end_pos, INT_MAX,
16437 window_box_height (w), -1,
16438 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y); */
16440 start = w->hscroll * FRAME_COLUMN_WIDTH (WINDOW_XFRAME (w));
16441 end = start + window_box_width (w, TEXT_AREA);
16442 portion = end - start;
16443 /* After enlarging a horizontally scrolled window such that it
16444 gets at least as wide as the text it contains, make sure that
16445 the thumb doesn't fill the entire scroll bar so we can still
16446 drag it back to see the entire text. */
16447 whole = max (whole, end);
16449 if (it.bidi_p)
16451 Lisp_Object pdir;
16453 pdir = Fcurrent_bidi_paragraph_direction (Qnil);
16454 if (EQ (pdir, Qright_to_left))
16456 start = whole - end;
16457 end = start + portion;
16461 if (old_buffer)
16462 set_buffer_internal (old_buffer);
16464 else
16465 start = end = whole = portion = 0;
16467 w->hscroll_whole = whole;
16469 /* Indicate what this scroll bar ought to be displaying now. */
16470 if (FRAME_TERMINAL (XFRAME (w->frame))->set_horizontal_scroll_bar_hook)
16471 (*FRAME_TERMINAL (XFRAME (w->frame))->set_horizontal_scroll_bar_hook)
16472 (w, portion, whole, start);
16476 /* Redisplay leaf window WINDOW. JUST_THIS_ONE_P means only
16477 selected_window is redisplayed.
16479 We can return without actually redisplaying the window if fonts has been
16480 changed on window's frame. In that case, redisplay_internal will retry.
16482 As one of the important parts of redisplaying a window, we need to
16483 decide whether the previous window-start position (stored in the
16484 window's w->start marker position) is still valid, and if it isn't,
16485 recompute it. Some details about that:
16487 . The previous window-start could be in a continuation line, in
16488 which case we need to recompute it when the window width
16489 changes. See compute_window_start_on_continuation_line and its
16490 call below.
16492 . The text that changed since last redisplay could include the
16493 previous window-start position. In that case, we try to salvage
16494 what we can from the current glyph matrix by calling
16495 try_scrolling, which see.
16497 . Some Emacs command could force us to use a specific window-start
16498 position by setting the window's force_start flag, or gently
16499 propose doing that by setting the window's optional_new_start
16500 flag. In these cases, we try using the specified start point if
16501 that succeeds (i.e. the window desired matrix is successfully
16502 recomputed, and point location is within the window). In case
16503 of optional_new_start, we first check if the specified start
16504 position is feasible, i.e. if it will allow point to be
16505 displayed in the window. If using the specified start point
16506 fails, e.g., if new fonts are needed to be loaded, we abort the
16507 redisplay cycle and leave it up to the next cycle to figure out
16508 things.
16510 . Note that the window's force_start flag is sometimes set by
16511 redisplay itself, when it decides that the previous window start
16512 point is fine and should be kept. Search for "goto force_start"
16513 below to see the details. Like the values of window-start
16514 specified outside of redisplay, these internally-deduced values
16515 are tested for feasibility, and ignored if found to be
16516 unfeasible.
16518 . Note that the function try_window, used to completely redisplay
16519 a window, accepts the window's start point as its argument.
16520 This is used several times in the redisplay code to control
16521 where the window start will be, according to user options such
16522 as scroll-conservatively, and also to ensure the screen line
16523 showing point will be fully (as opposed to partially) visible on
16524 display. */
16526 static void
16527 redisplay_window (Lisp_Object window, bool just_this_one_p)
16529 struct window *w = XWINDOW (window);
16530 struct frame *f = XFRAME (w->frame);
16531 struct buffer *buffer = XBUFFER (w->contents);
16532 struct buffer *old = current_buffer;
16533 struct text_pos lpoint, opoint, startp;
16534 bool update_mode_line;
16535 int tem;
16536 struct it it;
16537 /* Record it now because it's overwritten. */
16538 bool current_matrix_up_to_date_p = false;
16539 bool used_current_matrix_p = false;
16540 /* This is less strict than current_matrix_up_to_date_p.
16541 It indicates that the buffer contents and narrowing are unchanged. */
16542 bool buffer_unchanged_p = false;
16543 bool temp_scroll_step = false;
16544 ptrdiff_t count = SPECPDL_INDEX ();
16545 int rc;
16546 int centering_position = -1;
16547 bool last_line_misfit = false;
16548 ptrdiff_t beg_unchanged, end_unchanged;
16549 int frame_line_height, margin;
16550 bool use_desired_matrix;
16551 void *itdata = NULL;
16553 SET_TEXT_POS (lpoint, PT, PT_BYTE);
16554 opoint = lpoint;
16556 #ifdef GLYPH_DEBUG
16557 *w->desired_matrix->method = 0;
16558 #endif
16560 if (!just_this_one_p
16561 && REDISPLAY_SOME_P ()
16562 && !w->redisplay
16563 && !w->update_mode_line
16564 && !f->face_change
16565 && !f->redisplay
16566 && !buffer->text->redisplay
16567 && BUF_PT (buffer) == w->last_point)
16568 return;
16570 /* Make sure that both W's markers are valid. */
16571 eassert (XMARKER (w->start)->buffer == buffer);
16572 eassert (XMARKER (w->pointm)->buffer == buffer);
16574 reconsider_clip_changes (w);
16575 frame_line_height = default_line_pixel_height (w);
16576 margin = window_scroll_margin (w, MARGIN_IN_LINES);
16579 /* Has the mode line to be updated? */
16580 update_mode_line = (w->update_mode_line
16581 || update_mode_lines
16582 || buffer->clip_changed
16583 || buffer->prevent_redisplay_optimizations_p);
16585 if (!just_this_one_p)
16586 /* If `just_this_one_p' is set, we apparently set must_be_updated_p more
16587 cleverly elsewhere. */
16588 w->must_be_updated_p = true;
16590 if (MINI_WINDOW_P (w))
16592 if (w == XWINDOW (echo_area_window)
16593 && !NILP (echo_area_buffer[0]))
16595 if (update_mode_line)
16596 /* We may have to update a tty frame's menu bar or a
16597 tool-bar. Example `M-x C-h C-h C-g'. */
16598 goto finish_menu_bars;
16599 else
16600 /* We've already displayed the echo area glyphs in this window. */
16601 goto finish_scroll_bars;
16603 else if ((w != XWINDOW (minibuf_window)
16604 || minibuf_level == 0)
16605 /* When buffer is nonempty, redisplay window normally. */
16606 && BUF_Z (XBUFFER (w->contents)) == BUF_BEG (XBUFFER (w->contents))
16607 /* Quail displays non-mini buffers in minibuffer window.
16608 In that case, redisplay the window normally. */
16609 && !NILP (Fmemq (w->contents, Vminibuffer_list)))
16611 /* W is a mini-buffer window, but it's not active, so clear
16612 it. */
16613 int yb = window_text_bottom_y (w);
16614 struct glyph_row *row;
16615 int y;
16617 for (y = 0, row = w->desired_matrix->rows;
16618 y < yb;
16619 y += row->height, ++row)
16620 blank_row (w, row, y);
16621 goto finish_scroll_bars;
16624 clear_glyph_matrix (w->desired_matrix);
16627 /* Otherwise set up data on this window; select its buffer and point
16628 value. */
16629 /* Really select the buffer, for the sake of buffer-local
16630 variables. */
16631 set_buffer_internal_1 (XBUFFER (w->contents));
16633 current_matrix_up_to_date_p
16634 = (w->window_end_valid
16635 && !current_buffer->clip_changed
16636 && !current_buffer->prevent_redisplay_optimizations_p
16637 && !window_outdated (w)
16638 && !hscrolling_current_line_p (w));
16640 beg_unchanged = BEG_UNCHANGED;
16641 end_unchanged = END_UNCHANGED;
16643 SET_TEXT_POS (opoint, PT, PT_BYTE);
16645 specbind (Qinhibit_point_motion_hooks, Qt);
16647 buffer_unchanged_p
16648 = (w->window_end_valid
16649 && !current_buffer->clip_changed
16650 && !window_outdated (w));
16652 /* When windows_or_buffers_changed is non-zero, we can't rely
16653 on the window end being valid, so set it to zero there. */
16654 if (windows_or_buffers_changed)
16656 /* If window starts on a continuation line, maybe adjust the
16657 window start in case the window's width changed. */
16658 if (XMARKER (w->start)->buffer == current_buffer)
16659 compute_window_start_on_continuation_line (w);
16661 w->window_end_valid = false;
16662 /* If so, we also can't rely on current matrix
16663 and should not fool try_cursor_movement below. */
16664 current_matrix_up_to_date_p = false;
16667 /* Some sanity checks. */
16668 CHECK_WINDOW_END (w);
16669 if (Z == Z_BYTE && CHARPOS (opoint) != BYTEPOS (opoint))
16670 emacs_abort ();
16671 if (BYTEPOS (opoint) < CHARPOS (opoint))
16672 emacs_abort ();
16674 if (mode_line_update_needed (w))
16675 update_mode_line = true;
16677 /* Point refers normally to the selected window. For any other
16678 window, set up appropriate value. */
16679 if (!EQ (window, selected_window))
16681 ptrdiff_t new_pt = marker_position (w->pointm);
16682 ptrdiff_t new_pt_byte = marker_byte_position (w->pointm);
16684 if (new_pt < BEGV)
16686 new_pt = BEGV;
16687 new_pt_byte = BEGV_BYTE;
16688 set_marker_both (w->pointm, Qnil, BEGV, BEGV_BYTE);
16690 else if (new_pt > (ZV - 1))
16692 new_pt = ZV;
16693 new_pt_byte = ZV_BYTE;
16694 set_marker_both (w->pointm, Qnil, ZV, ZV_BYTE);
16697 /* We don't use SET_PT so that the point-motion hooks don't run. */
16698 TEMP_SET_PT_BOTH (new_pt, new_pt_byte);
16701 /* If any of the character widths specified in the display table
16702 have changed, invalidate the width run cache. It's true that
16703 this may be a bit late to catch such changes, but the rest of
16704 redisplay goes (non-fatally) haywire when the display table is
16705 changed, so why should we worry about doing any better? */
16706 if (current_buffer->width_run_cache
16707 || (current_buffer->base_buffer
16708 && current_buffer->base_buffer->width_run_cache))
16710 struct Lisp_Char_Table *disptab = buffer_display_table ();
16712 if (! disptab_matches_widthtab
16713 (disptab, XVECTOR (BVAR (current_buffer, width_table))))
16715 struct buffer *buf = current_buffer;
16717 if (buf->base_buffer)
16718 buf = buf->base_buffer;
16719 invalidate_region_cache (buf, buf->width_run_cache, BEG, Z);
16720 recompute_width_table (current_buffer, disptab);
16724 /* If window-start is screwed up, choose a new one. */
16725 if (XMARKER (w->start)->buffer != current_buffer)
16726 goto recenter;
16728 SET_TEXT_POS_FROM_MARKER (startp, w->start);
16730 /* If someone specified a new starting point but did not insist,
16731 check whether it can be used. */
16732 if ((w->optional_new_start || window_frozen_p (w))
16733 && CHARPOS (startp) >= BEGV
16734 && CHARPOS (startp) <= ZV)
16736 ptrdiff_t it_charpos;
16738 w->optional_new_start = false;
16739 start_display (&it, w, startp);
16740 move_it_to (&it, PT, 0, it.last_visible_y, -1,
16741 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
16742 /* Record IT's position now, since line_bottom_y might change
16743 that. */
16744 it_charpos = IT_CHARPOS (it);
16745 /* Make sure we set the force_start flag only if the cursor row
16746 will be fully visible. Otherwise, the code under force_start
16747 label below will try to move point back into view, which is
16748 not what the code which sets optional_new_start wants. */
16749 if ((it.current_y == 0 || line_bottom_y (&it) < it.last_visible_y)
16750 && !w->force_start)
16752 if (it_charpos == PT)
16753 w->force_start = true;
16754 /* IT may overshoot PT if text at PT is invisible. */
16755 else if (it_charpos > PT && CHARPOS (startp) <= PT)
16756 w->force_start = true;
16757 #ifdef GLYPH_DEBUG
16758 if (w->force_start)
16760 if (window_frozen_p (w))
16761 debug_method_add (w, "set force_start from frozen window start");
16762 else
16763 debug_method_add (w, "set force_start from optional_new_start");
16765 #endif
16769 force_start:
16771 /* Handle case where place to start displaying has been specified,
16772 unless the specified location is outside the accessible range. */
16773 if (w->force_start)
16775 /* We set this later on if we have to adjust point. */
16776 int new_vpos = -1;
16778 w->force_start = false;
16779 w->vscroll = 0;
16780 w->window_end_valid = false;
16782 /* Forget any recorded base line for line number display. */
16783 if (!buffer_unchanged_p)
16784 w->base_line_number = 0;
16786 /* Redisplay the mode line. Select the buffer properly for that.
16787 Also, run the hook window-scroll-functions
16788 because we have scrolled. */
16789 /* Note, we do this after clearing force_start because
16790 if there's an error, it is better to forget about force_start
16791 than to get into an infinite loop calling the hook functions
16792 and having them get more errors. */
16793 if (!update_mode_line
16794 || ! NILP (Vwindow_scroll_functions))
16796 update_mode_line = true;
16797 w->update_mode_line = true;
16798 startp = run_window_scroll_functions (window, startp);
16801 if (CHARPOS (startp) < BEGV)
16802 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
16803 else if (CHARPOS (startp) > ZV)
16804 SET_TEXT_POS (startp, ZV, ZV_BYTE);
16806 /* Redisplay, then check if cursor has been set during the
16807 redisplay. Give up if new fonts were loaded. */
16808 /* We used to issue a CHECK_MARGINS argument to try_window here,
16809 but this causes scrolling to fail when point begins inside
16810 the scroll margin (bug#148) -- cyd */
16811 if (!try_window (window, startp, 0))
16813 w->force_start = true;
16814 clear_glyph_matrix (w->desired_matrix);
16815 goto need_larger_matrices;
16818 if (w->cursor.vpos < 0)
16820 /* If point does not appear, try to move point so it does
16821 appear. The desired matrix has been built above, so we
16822 can use it here. First see if point is in invisible
16823 text, and if so, move it to the first visible buffer
16824 position past that. */
16825 struct glyph_row *r = NULL;
16826 Lisp_Object invprop =
16827 get_char_property_and_overlay (make_number (PT), Qinvisible,
16828 Qnil, NULL);
16830 if (TEXT_PROP_MEANS_INVISIBLE (invprop) != 0)
16832 ptrdiff_t alt_pt;
16833 Lisp_Object invprop_end =
16834 Fnext_single_char_property_change (make_number (PT), Qinvisible,
16835 Qnil, Qnil);
16837 if (NATNUMP (invprop_end))
16838 alt_pt = XFASTINT (invprop_end);
16839 else
16840 alt_pt = ZV;
16841 r = row_containing_pos (w, alt_pt, w->desired_matrix->rows,
16842 NULL, 0);
16844 if (r)
16845 new_vpos = MATRIX_ROW_BOTTOM_Y (r);
16846 else /* Give up and just move to the middle of the window. */
16847 new_vpos = window_box_height (w) / 2;
16850 if (!cursor_row_fully_visible_p (w, false, false))
16852 /* Point does appear, but on a line partly visible at end of window.
16853 Move it back to a fully-visible line. */
16854 new_vpos = window_box_height (w);
16855 /* But if window_box_height suggests a Y coordinate that is
16856 not less than we already have, that line will clearly not
16857 be fully visible, so give up and scroll the display.
16858 This can happen when the default face uses a font whose
16859 dimensions are different from the frame's default
16860 font. */
16861 if (new_vpos >= w->cursor.y)
16863 w->cursor.vpos = -1;
16864 clear_glyph_matrix (w->desired_matrix);
16865 goto try_to_scroll;
16868 else if (w->cursor.vpos >= 0)
16870 /* Some people insist on not letting point enter the scroll
16871 margin, even though this part handles windows that didn't
16872 scroll at all. */
16873 int pixel_margin = margin * frame_line_height;
16874 bool header_line = window_wants_header_line (w);
16876 /* Note: We add an extra FRAME_LINE_HEIGHT, because the loop
16877 below, which finds the row to move point to, advances by
16878 the Y coordinate of the _next_ row, see the definition of
16879 MATRIX_ROW_BOTTOM_Y. */
16880 if (w->cursor.vpos < margin + header_line)
16882 w->cursor.vpos = -1;
16883 clear_glyph_matrix (w->desired_matrix);
16884 goto try_to_scroll;
16886 else
16888 int window_height = window_box_height (w);
16890 if (header_line)
16891 window_height += CURRENT_HEADER_LINE_HEIGHT (w);
16892 if (w->cursor.y >= window_height - pixel_margin)
16894 w->cursor.vpos = -1;
16895 clear_glyph_matrix (w->desired_matrix);
16896 goto try_to_scroll;
16901 /* If we need to move point for either of the above reasons,
16902 now actually do it. */
16903 if (new_vpos >= 0)
16905 struct glyph_row *row;
16907 row = MATRIX_FIRST_TEXT_ROW (w->desired_matrix);
16908 while (MATRIX_ROW_BOTTOM_Y (row) < new_vpos)
16909 ++row;
16911 TEMP_SET_PT_BOTH (MATRIX_ROW_START_CHARPOS (row),
16912 MATRIX_ROW_START_BYTEPOS (row));
16914 if (w != XWINDOW (selected_window))
16915 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
16916 else if (current_buffer == old)
16917 SET_TEXT_POS (lpoint, PT, PT_BYTE);
16919 set_cursor_from_row (w, row, w->desired_matrix, 0, 0, 0, 0);
16921 /* Re-run pre-redisplay-function so it can update the region
16922 according to the new position of point. */
16923 /* Other than the cursor, w's redisplay is done so we can set its
16924 redisplay to false. Also the buffer's redisplay can be set to
16925 false, since propagate_buffer_redisplay should have already
16926 propagated its info to `w' anyway. */
16927 w->redisplay = false;
16928 XBUFFER (w->contents)->text->redisplay = false;
16929 safe__call1 (true, Vpre_redisplay_function, Fcons (window, Qnil));
16931 if (w->redisplay || XBUFFER (w->contents)->text->redisplay
16932 || ((EQ (Vdisplay_line_numbers, Qrelative)
16933 || EQ (Vdisplay_line_numbers, Qvisual))
16934 && row != MATRIX_FIRST_TEXT_ROW (w->desired_matrix)))
16936 /* Either pre-redisplay-function made changes (e.g. move
16937 the region), or we moved point in a window that is
16938 under display-line-numbers = relative mode. We need
16939 another round of redisplay. */
16940 clear_glyph_matrix (w->desired_matrix);
16941 if (!try_window (window, startp, 0))
16942 goto need_larger_matrices;
16945 if (w->cursor.vpos < 0 || !cursor_row_fully_visible_p (w, false, false))
16947 clear_glyph_matrix (w->desired_matrix);
16948 goto try_to_scroll;
16951 #ifdef GLYPH_DEBUG
16952 debug_method_add (w, "forced window start");
16953 #endif
16954 goto done;
16957 /* Handle case where text has not changed, only point, and it has
16958 not moved off the frame, and we are not retrying after hscroll.
16959 (current_matrix_up_to_date_p is true when retrying.) */
16960 if (current_matrix_up_to_date_p
16961 && (rc = try_cursor_movement (window, startp, &temp_scroll_step),
16962 rc != CURSOR_MOVEMENT_CANNOT_BE_USED))
16964 switch (rc)
16966 case CURSOR_MOVEMENT_SUCCESS:
16967 used_current_matrix_p = true;
16968 goto done;
16970 case CURSOR_MOVEMENT_MUST_SCROLL:
16971 goto try_to_scroll;
16973 default:
16974 emacs_abort ();
16977 /* If current starting point was originally the beginning of a line
16978 but no longer is, find a new starting point. */
16979 else if (w->start_at_line_beg
16980 && !(CHARPOS (startp) <= BEGV
16981 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n'))
16983 #ifdef GLYPH_DEBUG
16984 debug_method_add (w, "recenter 1");
16985 #endif
16986 goto recenter;
16989 /* Try scrolling with try_window_id. Value is > 0 if update has
16990 been done, it is -1 if we know that the same window start will
16991 not work. It is 0 if unsuccessful for some other reason. */
16992 else if ((tem = try_window_id (w)) != 0)
16994 #ifdef GLYPH_DEBUG
16995 debug_method_add (w, "try_window_id %d", tem);
16996 #endif
16998 if (f->fonts_changed)
16999 goto need_larger_matrices;
17000 if (tem > 0)
17001 goto done;
17003 /* Otherwise try_window_id has returned -1 which means that we
17004 don't want the alternative below this comment to execute. */
17006 else if (CHARPOS (startp) >= BEGV
17007 && CHARPOS (startp) <= ZV
17008 && PT >= CHARPOS (startp)
17009 && (CHARPOS (startp) < ZV
17010 /* Avoid starting at end of buffer. */
17011 || CHARPOS (startp) == BEGV
17012 || !window_outdated (w)))
17014 int d1, d2, d5, d6;
17015 int rtop, rbot;
17017 /* If first window line is a continuation line, and window start
17018 is inside the modified region, but the first change is before
17019 current window start, we must select a new window start.
17021 However, if this is the result of a down-mouse event (e.g. by
17022 extending the mouse-drag-overlay), we don't want to select a
17023 new window start, since that would change the position under
17024 the mouse, resulting in an unwanted mouse-movement rather
17025 than a simple mouse-click. */
17026 if (!w->start_at_line_beg
17027 && NILP (do_mouse_tracking)
17028 && CHARPOS (startp) > BEGV
17029 && CHARPOS (startp) > BEG + beg_unchanged
17030 && CHARPOS (startp) <= Z - end_unchanged
17031 /* Even if w->start_at_line_beg is nil, a new window may
17032 start at a line_beg, since that's how set_buffer_window
17033 sets it. So, we need to check the return value of
17034 compute_window_start_on_continuation_line. (See also
17035 bug#197). */
17036 && XMARKER (w->start)->buffer == current_buffer
17037 && compute_window_start_on_continuation_line (w)
17038 /* It doesn't make sense to force the window start like we
17039 do at label force_start if it is already known that point
17040 will not be fully visible in the resulting window, because
17041 doing so will move point from its correct position
17042 instead of scrolling the window to bring point into view.
17043 See bug#9324. */
17044 && pos_visible_p (w, PT, &d1, &d2, &rtop, &rbot, &d5, &d6)
17045 /* A very tall row could need more than the window height,
17046 in which case we accept that it is partially visible. */
17047 && (rtop != 0) == (rbot != 0))
17049 w->force_start = true;
17050 SET_TEXT_POS_FROM_MARKER (startp, w->start);
17051 #ifdef GLYPH_DEBUG
17052 debug_method_add (w, "recomputed window start in continuation line");
17053 #endif
17054 goto force_start;
17057 #ifdef GLYPH_DEBUG
17058 debug_method_add (w, "same window start");
17059 #endif
17061 /* Try to redisplay starting at same place as before.
17062 If point has not moved off frame, accept the results. */
17063 if (!current_matrix_up_to_date_p
17064 /* Don't use try_window_reusing_current_matrix in this case
17065 because a window scroll function can have changed the
17066 buffer. */
17067 || !NILP (Vwindow_scroll_functions)
17068 || MINI_WINDOW_P (w)
17069 || !(used_current_matrix_p
17070 = try_window_reusing_current_matrix (w)))
17072 IF_DEBUG (debug_method_add (w, "1"));
17073 clear_glyph_matrix (w->desired_matrix);
17074 if (try_window (window, startp, TRY_WINDOW_CHECK_MARGINS) < 0)
17075 /* -1 means we need to scroll.
17076 0 means we need new matrices, but fonts_changed
17077 is set in that case, so we will detect it below. */
17078 goto try_to_scroll;
17081 if (f->fonts_changed)
17082 goto need_larger_matrices;
17084 if (w->cursor.vpos >= 0)
17086 if (!just_this_one_p
17087 || current_buffer->clip_changed
17088 || BEG_UNCHANGED < CHARPOS (startp))
17089 /* Forget any recorded base line for line number display. */
17090 w->base_line_number = 0;
17092 if (!cursor_row_fully_visible_p (w, true, false))
17094 clear_glyph_matrix (w->desired_matrix);
17095 last_line_misfit = true;
17097 /* Drop through and scroll. */
17098 else
17099 goto done;
17101 else
17102 clear_glyph_matrix (w->desired_matrix);
17105 try_to_scroll:
17107 /* Redisplay the mode line. Select the buffer properly for that. */
17108 if (!update_mode_line)
17110 update_mode_line = true;
17111 w->update_mode_line = true;
17114 /* Try to scroll by specified few lines. */
17115 if ((scroll_conservatively
17116 || emacs_scroll_step
17117 || temp_scroll_step
17118 || NUMBERP (BVAR (current_buffer, scroll_up_aggressively))
17119 || NUMBERP (BVAR (current_buffer, scroll_down_aggressively)))
17120 && CHARPOS (startp) >= BEGV
17121 && CHARPOS (startp) <= ZV)
17123 /* The function returns -1 if new fonts were loaded, 1 if
17124 successful, 0 if not successful. */
17125 int ss = try_scrolling (window, just_this_one_p,
17126 scroll_conservatively,
17127 emacs_scroll_step,
17128 temp_scroll_step, last_line_misfit);
17129 switch (ss)
17131 case SCROLLING_SUCCESS:
17132 goto done;
17134 case SCROLLING_NEED_LARGER_MATRICES:
17135 goto need_larger_matrices;
17137 case SCROLLING_FAILED:
17138 break;
17140 default:
17141 emacs_abort ();
17145 /* Finally, just choose a place to start which positions point
17146 according to user preferences. */
17148 recenter:
17150 #ifdef GLYPH_DEBUG
17151 debug_method_add (w, "recenter");
17152 #endif
17154 /* Forget any previously recorded base line for line number display. */
17155 if (!buffer_unchanged_p)
17156 w->base_line_number = 0;
17158 /* Determine the window start relative to point. */
17159 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
17160 it.current_y = it.last_visible_y;
17161 if (centering_position < 0)
17163 ptrdiff_t margin_pos = CHARPOS (startp);
17164 Lisp_Object aggressive;
17165 bool scrolling_up;
17167 /* If there is a scroll margin at the top of the window, find
17168 its character position. */
17169 if (margin
17170 /* Cannot call start_display if startp is not in the
17171 accessible region of the buffer. This can happen when we
17172 have just switched to a different buffer and/or changed
17173 its restriction. In that case, startp is initialized to
17174 the character position 1 (BEGV) because we did not yet
17175 have chance to display the buffer even once. */
17176 && BEGV <= CHARPOS (startp) && CHARPOS (startp) <= ZV)
17178 struct it it1;
17179 void *it1data = NULL;
17181 SAVE_IT (it1, it, it1data);
17182 start_display (&it1, w, startp);
17183 move_it_vertically (&it1, margin * frame_line_height);
17184 margin_pos = IT_CHARPOS (it1);
17185 RESTORE_IT (&it, &it, it1data);
17187 scrolling_up = PT > margin_pos;
17188 aggressive =
17189 scrolling_up
17190 ? BVAR (current_buffer, scroll_up_aggressively)
17191 : BVAR (current_buffer, scroll_down_aggressively);
17193 if (!MINI_WINDOW_P (w)
17194 && (scroll_conservatively > SCROLL_LIMIT || NUMBERP (aggressive)))
17196 int pt_offset = 0;
17198 /* Setting scroll-conservatively overrides
17199 scroll-*-aggressively. */
17200 if (!scroll_conservatively && NUMBERP (aggressive))
17202 double float_amount = XFLOATINT (aggressive);
17204 pt_offset = float_amount * WINDOW_BOX_TEXT_HEIGHT (w);
17205 if (pt_offset == 0 && float_amount > 0)
17206 pt_offset = 1;
17207 if (pt_offset && margin > 0)
17208 margin -= 1;
17210 /* Compute how much to move the window start backward from
17211 point so that point will be displayed where the user
17212 wants it. */
17213 if (scrolling_up)
17215 centering_position = it.last_visible_y;
17216 if (pt_offset)
17217 centering_position -= pt_offset;
17218 centering_position -=
17219 (frame_line_height * (1 + margin + last_line_misfit)
17220 + WINDOW_HEADER_LINE_HEIGHT (w));
17221 /* Don't let point enter the scroll margin near top of
17222 the window. */
17223 if (centering_position < margin * frame_line_height)
17224 centering_position = margin * frame_line_height;
17226 else
17227 centering_position = margin * frame_line_height + pt_offset;
17229 else
17230 /* Set the window start half the height of the window backward
17231 from point. */
17232 centering_position = window_box_height (w) / 2;
17234 move_it_vertically_backward (&it, centering_position);
17236 eassert (IT_CHARPOS (it) >= BEGV);
17238 /* The function move_it_vertically_backward may move over more
17239 than the specified y-distance. If it->w is small, e.g. a
17240 mini-buffer window, we may end up in front of the window's
17241 display area. Start displaying at the start of the line
17242 containing PT in this case. */
17243 if (it.current_y <= 0)
17245 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
17246 move_it_vertically_backward (&it, 0);
17247 it.current_y = 0;
17250 it.current_x = it.hpos = 0;
17252 /* Set the window start position here explicitly, to avoid an
17253 infinite loop in case the functions in window-scroll-functions
17254 get errors. */
17255 set_marker_both (w->start, Qnil, IT_CHARPOS (it), IT_BYTEPOS (it));
17257 /* Run scroll hooks. */
17258 startp = run_window_scroll_functions (window, it.current.pos);
17260 /* We invoke try_window and try_window_reusing_current_matrix below,
17261 and they manipulate the bidi cache. Save and restore the cache
17262 state of our iterator, so we could continue using it after that. */
17263 itdata = bidi_shelve_cache ();
17265 /* Redisplay the window. */
17266 use_desired_matrix = false;
17267 if (!current_matrix_up_to_date_p
17268 || windows_or_buffers_changed
17269 || f->cursor_type_changed
17270 /* Don't use try_window_reusing_current_matrix in this case
17271 because it can have changed the buffer. */
17272 || !NILP (Vwindow_scroll_functions)
17273 || !just_this_one_p
17274 || MINI_WINDOW_P (w)
17275 || !(used_current_matrix_p
17276 = try_window_reusing_current_matrix (w)))
17277 use_desired_matrix = (try_window (window, startp, 0) == 1);
17279 bidi_unshelve_cache (itdata, false);
17281 /* If new fonts have been loaded (due to fontsets), give up. We
17282 have to start a new redisplay since we need to re-adjust glyph
17283 matrices. */
17284 if (f->fonts_changed)
17285 goto need_larger_matrices;
17287 /* If cursor did not appear assume that the middle of the window is
17288 in the first line of the window. Do it again with the next line.
17289 (Imagine a window of height 100, displaying two lines of height
17290 60. Moving back 50 from it->last_visible_y will end in the first
17291 line.) */
17292 if (w->cursor.vpos < 0)
17294 if (w->window_end_valid && PT >= Z - w->window_end_pos)
17296 clear_glyph_matrix (w->desired_matrix);
17297 move_it_by_lines (&it, 1);
17298 try_window (window, it.current.pos, 0);
17300 else if (PT < IT_CHARPOS (it))
17302 clear_glyph_matrix (w->desired_matrix);
17303 move_it_by_lines (&it, -1);
17304 try_window (window, it.current.pos, 0);
17306 else if (scroll_conservatively > SCROLL_LIMIT
17307 && (it.method == GET_FROM_STRING
17308 || overlay_touches_p (IT_CHARPOS (it)))
17309 && IT_CHARPOS (it) < ZV)
17311 /* If the window starts with a before-string that spans more
17312 than one screen line, using that position to display the
17313 window might fail to bring point into the view, because
17314 start_display will always start by displaying the string,
17315 whereas the code above determines where to set w->start
17316 by the buffer position of the place where it takes screen
17317 coordinates. Try to recover by finding the next screen
17318 line that displays buffer text. */
17319 ptrdiff_t pos0 = IT_CHARPOS (it);
17321 clear_glyph_matrix (w->desired_matrix);
17322 do {
17323 move_it_by_lines (&it, 1);
17324 } while (IT_CHARPOS (it) == pos0);
17325 try_window (window, it.current.pos, 0);
17327 else
17329 /* Not much we can do about it. */
17333 /* Consider the following case: Window starts at BEGV, there is
17334 invisible, intangible text at BEGV, so that display starts at
17335 some point START > BEGV. It can happen that we are called with
17336 PT somewhere between BEGV and START. Try to handle that case,
17337 and similar ones. */
17338 if (w->cursor.vpos < 0)
17340 /* Prefer the desired matrix to the current matrix, if possible,
17341 in the fallback calculations below. This is because using
17342 the current matrix might completely goof, e.g. if its first
17343 row is after point. */
17344 struct glyph_matrix *matrix =
17345 use_desired_matrix ? w->desired_matrix : w->current_matrix;
17346 /* First, try locating the proper glyph row for PT. */
17347 struct glyph_row *row =
17348 row_containing_pos (w, PT, matrix->rows, NULL, 0);
17350 /* Sometimes point is at the beginning of invisible text that is
17351 before the 1st character displayed in the row. In that case,
17352 row_containing_pos fails to find the row, because no glyphs
17353 with appropriate buffer positions are present in the row.
17354 Therefore, we next try to find the row which shows the 1st
17355 position after the invisible text. */
17356 if (!row)
17358 Lisp_Object val =
17359 get_char_property_and_overlay (make_number (PT), Qinvisible,
17360 Qnil, NULL);
17362 if (TEXT_PROP_MEANS_INVISIBLE (val) != 0)
17364 ptrdiff_t alt_pos;
17365 Lisp_Object invis_end =
17366 Fnext_single_char_property_change (make_number (PT), Qinvisible,
17367 Qnil, Qnil);
17369 if (NATNUMP (invis_end))
17370 alt_pos = XFASTINT (invis_end);
17371 else
17372 alt_pos = ZV;
17373 row = row_containing_pos (w, alt_pos, matrix->rows, NULL, 0);
17376 /* Finally, fall back on the first row of the window after the
17377 header line (if any). This is slightly better than not
17378 displaying the cursor at all. */
17379 if (!row)
17381 row = matrix->rows;
17382 if (row->mode_line_p)
17383 ++row;
17385 set_cursor_from_row (w, row, matrix, 0, 0, 0, 0);
17388 if (!cursor_row_fully_visible_p (w, false, false))
17390 /* If vscroll is enabled, disable it and try again. */
17391 if (w->vscroll)
17393 w->vscroll = 0;
17394 clear_glyph_matrix (w->desired_matrix);
17395 goto recenter;
17398 /* Users who set scroll-conservatively to a large number want
17399 point just above/below the scroll margin. If we ended up
17400 with point's row partially visible, move the window start to
17401 make that row fully visible and out of the margin. */
17402 if (scroll_conservatively > SCROLL_LIMIT)
17404 int window_total_lines
17405 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
17406 bool move_down = w->cursor.vpos >= window_total_lines / 2;
17408 move_it_by_lines (&it, move_down ? margin + 1 : -(margin + 1));
17409 clear_glyph_matrix (w->desired_matrix);
17410 if (1 == try_window (window, it.current.pos,
17411 TRY_WINDOW_CHECK_MARGINS))
17412 goto done;
17415 /* If centering point failed to make the whole line visible,
17416 put point at the top instead. That has to make the whole line
17417 visible, if it can be done. */
17418 if (centering_position == 0)
17419 goto done;
17421 clear_glyph_matrix (w->desired_matrix);
17422 centering_position = 0;
17423 goto recenter;
17426 done:
17428 SET_TEXT_POS_FROM_MARKER (startp, w->start);
17429 w->start_at_line_beg = (CHARPOS (startp) == BEGV
17430 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n');
17432 /* Display the mode line, if we must. */
17433 if ((update_mode_line
17434 /* If window not full width, must redo its mode line
17435 if (a) the window to its side is being redone and
17436 (b) we do a frame-based redisplay. This is a consequence
17437 of how inverted lines are drawn in frame-based redisplay. */
17438 || (!just_this_one_p
17439 && !FRAME_WINDOW_P (f)
17440 && !WINDOW_FULL_WIDTH_P (w))
17441 /* Line number to display. */
17442 || w->base_line_pos > 0
17443 /* Column number is displayed and different from the one displayed. */
17444 || (w->column_number_displayed != -1
17445 && (w->column_number_displayed != current_column ())))
17446 /* This means that the window has a mode line. */
17447 && (window_wants_mode_line (w)
17448 || window_wants_header_line (w)))
17451 display_mode_lines (w);
17453 /* If mode line height has changed, arrange for a thorough
17454 immediate redisplay using the correct mode line height. */
17455 if (window_wants_mode_line (w)
17456 && CURRENT_MODE_LINE_HEIGHT (w) != DESIRED_MODE_LINE_HEIGHT (w))
17458 f->fonts_changed = true;
17459 w->mode_line_height = -1;
17460 MATRIX_MODE_LINE_ROW (w->current_matrix)->height
17461 = DESIRED_MODE_LINE_HEIGHT (w);
17464 /* If header line height has changed, arrange for a thorough
17465 immediate redisplay using the correct header line height. */
17466 if (window_wants_header_line (w)
17467 && CURRENT_HEADER_LINE_HEIGHT (w) != DESIRED_HEADER_LINE_HEIGHT (w))
17469 f->fonts_changed = true;
17470 w->header_line_height = -1;
17471 MATRIX_HEADER_LINE_ROW (w->current_matrix)->height
17472 = DESIRED_HEADER_LINE_HEIGHT (w);
17475 if (f->fonts_changed)
17476 goto need_larger_matrices;
17479 if (!line_number_displayed && w->base_line_pos != -1)
17481 w->base_line_pos = 0;
17482 w->base_line_number = 0;
17485 finish_menu_bars:
17487 /* When we reach a frame's selected window, redo the frame's menu
17488 bar and the frame's title. */
17489 if (update_mode_line
17490 && EQ (FRAME_SELECTED_WINDOW (f), window))
17492 bool redisplay_menu_p;
17494 if (FRAME_WINDOW_P (f))
17496 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
17497 || defined (HAVE_NS) || defined (USE_GTK)
17498 redisplay_menu_p = FRAME_EXTERNAL_MENU_BAR (f);
17499 #else
17500 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
17501 #endif
17503 else
17504 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
17506 if (redisplay_menu_p)
17507 display_menu_bar (w);
17509 #ifdef HAVE_WINDOW_SYSTEM
17510 if (FRAME_WINDOW_P (f))
17512 #if defined (USE_GTK) || defined (HAVE_NS)
17513 if (FRAME_EXTERNAL_TOOL_BAR (f))
17514 redisplay_tool_bar (f);
17515 #else
17516 if (WINDOWP (f->tool_bar_window)
17517 && (FRAME_TOOL_BAR_LINES (f) > 0
17518 || !NILP (Vauto_resize_tool_bars))
17519 && redisplay_tool_bar (f))
17520 ignore_mouse_drag_p = true;
17521 #endif
17523 x_consider_frame_title (w->frame);
17524 #endif
17527 #ifdef HAVE_WINDOW_SYSTEM
17528 if (FRAME_WINDOW_P (f)
17529 && update_window_fringes (w, (just_this_one_p
17530 || (!used_current_matrix_p && !overlay_arrow_seen)
17531 || w->pseudo_window_p)))
17533 update_begin (f);
17534 block_input ();
17535 if (draw_window_fringes (w, true))
17537 if (WINDOW_RIGHT_DIVIDER_WIDTH (w))
17538 x_draw_right_divider (w);
17539 else
17540 x_draw_vertical_border (w);
17542 unblock_input ();
17543 update_end (f);
17546 if (WINDOW_BOTTOM_DIVIDER_WIDTH (w))
17547 x_draw_bottom_divider (w);
17548 #endif /* HAVE_WINDOW_SYSTEM */
17550 /* We go to this label, with fonts_changed set, if it is
17551 necessary to try again using larger glyph matrices.
17552 We have to redeem the scroll bar even in this case,
17553 because the loop in redisplay_internal expects that. */
17554 need_larger_matrices:
17556 finish_scroll_bars:
17558 if (WINDOW_HAS_VERTICAL_SCROLL_BAR (w) || WINDOW_HAS_HORIZONTAL_SCROLL_BAR (w))
17560 if (WINDOW_HAS_VERTICAL_SCROLL_BAR (w))
17561 /* Set the thumb's position and size. */
17562 set_vertical_scroll_bar (w);
17564 if (WINDOW_HAS_HORIZONTAL_SCROLL_BAR (w))
17565 /* Set the thumb's position and size. */
17566 set_horizontal_scroll_bar (w);
17568 /* Note that we actually used the scroll bar attached to this
17569 window, so it shouldn't be deleted at the end of redisplay. */
17570 if (FRAME_TERMINAL (f)->redeem_scroll_bar_hook)
17571 (*FRAME_TERMINAL (f)->redeem_scroll_bar_hook) (w);
17574 /* Restore current_buffer and value of point in it. The window
17575 update may have changed the buffer, so first make sure `opoint'
17576 is still valid (Bug#6177). */
17577 if (CHARPOS (opoint) < BEGV)
17578 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
17579 else if (CHARPOS (opoint) > ZV)
17580 TEMP_SET_PT_BOTH (Z, Z_BYTE);
17581 else
17582 TEMP_SET_PT_BOTH (CHARPOS (opoint), BYTEPOS (opoint));
17584 set_buffer_internal_1 (old);
17585 /* Avoid an abort in TEMP_SET_PT_BOTH if the buffer has become
17586 shorter. This can be caused by log truncation in *Messages*. */
17587 if (CHARPOS (lpoint) <= ZV)
17588 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
17590 unbind_to (count, Qnil);
17594 /* Build the complete desired matrix of WINDOW with a window start
17595 buffer position POS.
17597 Value is 1 if successful. It is zero if fonts were loaded during
17598 redisplay which makes re-adjusting glyph matrices necessary, and -1
17599 if point would appear in the scroll margins.
17600 (We check the former only if TRY_WINDOW_IGNORE_FONTS_CHANGE is
17601 unset in FLAGS, and the latter only if TRY_WINDOW_CHECK_MARGINS is
17602 set in FLAGS.) */
17605 try_window (Lisp_Object window, struct text_pos pos, int flags)
17607 struct window *w = XWINDOW (window);
17608 struct it it;
17609 struct glyph_row *last_text_row = NULL;
17610 struct frame *f = XFRAME (w->frame);
17611 int cursor_vpos = w->cursor.vpos;
17613 /* Make POS the new window start. */
17614 set_marker_both (w->start, Qnil, CHARPOS (pos), BYTEPOS (pos));
17616 /* Mark cursor position as unknown. No overlay arrow seen. */
17617 w->cursor.vpos = -1;
17618 overlay_arrow_seen = false;
17620 /* Initialize iterator and info to start at POS. */
17621 start_display (&it, w, pos);
17622 it.glyph_row->reversed_p = false;
17624 /* Display all lines of W. */
17625 while (it.current_y < it.last_visible_y)
17627 if (display_line (&it, cursor_vpos))
17628 last_text_row = it.glyph_row - 1;
17629 if (f->fonts_changed && !(flags & TRY_WINDOW_IGNORE_FONTS_CHANGE))
17630 return 0;
17633 /* Save the character position of 'it' before we call
17634 'start_display' again. */
17635 ptrdiff_t it_charpos = IT_CHARPOS (it);
17637 /* Don't let the cursor end in the scroll margins. */
17638 if ((flags & TRY_WINDOW_CHECK_MARGINS)
17639 && !MINI_WINDOW_P (w))
17641 int this_scroll_margin = window_scroll_margin (w, MARGIN_IN_PIXELS);
17642 start_display (&it, w, pos);
17644 if ((w->cursor.y >= 0 /* not vscrolled */
17645 && w->cursor.y < this_scroll_margin
17646 && CHARPOS (pos) > BEGV
17647 && it_charpos < ZV)
17648 /* rms: considering make_cursor_line_fully_visible_p here
17649 seems to give wrong results. We don't want to recenter
17650 when the last line is partly visible, we want to allow
17651 that case to be handled in the usual way. */
17652 || w->cursor.y > (it.last_visible_y - partial_line_height (&it)
17653 - this_scroll_margin - 1))
17655 w->cursor.vpos = -1;
17656 clear_glyph_matrix (w->desired_matrix);
17657 return -1;
17661 /* If bottom moved off end of frame, change mode line percentage. */
17662 if (w->window_end_pos <= 0 && Z != it_charpos)
17663 w->update_mode_line = true;
17665 /* Set window_end_pos to the offset of the last character displayed
17666 on the window from the end of current_buffer. Set
17667 window_end_vpos to its row number. */
17668 if (last_text_row)
17670 eassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_text_row));
17671 adjust_window_ends (w, last_text_row, false);
17672 eassert
17673 (MATRIX_ROW_DISPLAYS_TEXT_P (MATRIX_ROW (w->desired_matrix,
17674 w->window_end_vpos)));
17676 else
17678 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
17679 w->window_end_pos = Z - ZV;
17680 w->window_end_vpos = 0;
17683 /* But that is not valid info until redisplay finishes. */
17684 w->window_end_valid = false;
17685 return 1;
17690 /************************************************************************
17691 Window redisplay reusing current matrix when buffer has not changed
17692 ************************************************************************/
17694 /* Try redisplay of window W showing an unchanged buffer with a
17695 different window start than the last time it was displayed by
17696 reusing its current matrix. Value is true if successful.
17697 W->start is the new window start. */
17699 static bool
17700 try_window_reusing_current_matrix (struct window *w)
17702 struct frame *f = XFRAME (w->frame);
17703 struct glyph_row *bottom_row;
17704 struct it it;
17705 struct run run;
17706 struct text_pos start, new_start;
17707 int nrows_scrolled, i;
17708 struct glyph_row *last_text_row;
17709 struct glyph_row *last_reused_text_row;
17710 struct glyph_row *start_row;
17711 int start_vpos, min_y, max_y;
17713 #ifdef GLYPH_DEBUG
17714 if (inhibit_try_window_reusing)
17715 return false;
17716 #endif
17718 if (/* This function doesn't handle terminal frames. */
17719 !FRAME_WINDOW_P (f)
17720 /* Don't try to reuse the display if windows have been split
17721 or such. */
17722 || windows_or_buffers_changed
17723 || f->cursor_type_changed
17724 /* This function cannot handle buffers where the overlay arrow
17725 is shown on the fringes, because if the arrow position
17726 changes, we cannot just reuse the current matrix. */
17727 || overlay_arrow_in_current_buffer_p ())
17728 return false;
17730 /* Can't do this if showing trailing whitespace. */
17731 if (!NILP (Vshow_trailing_whitespace))
17732 return false;
17734 /* If top-line visibility has changed, give up. */
17735 if (window_wants_header_line (w)
17736 != MATRIX_HEADER_LINE_ROW (w->current_matrix)->mode_line_p)
17737 return false;
17739 /* Give up if old or new display is scrolled vertically. We could
17740 make this function handle this, but right now it doesn't. */
17741 start_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
17742 if (w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row))
17743 return false;
17745 /* Clear the desired matrix for the display below. */
17746 clear_glyph_matrix (w->desired_matrix);
17748 /* Give up if line numbers are being displayed, because reusing the
17749 current matrix might use the wrong width for line-number
17750 display. */
17751 if (!NILP (Vdisplay_line_numbers))
17752 return false;
17754 /* The variable new_start now holds the new window start. The old
17755 start `start' can be determined from the current matrix. */
17756 SET_TEXT_POS_FROM_MARKER (new_start, w->start);
17757 start = start_row->minpos;
17758 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
17760 if (CHARPOS (new_start) <= CHARPOS (start))
17762 /* Don't use this method if the display starts with an ellipsis
17763 displayed for invisible text. It's not easy to handle that case
17764 below, and it's certainly not worth the effort since this is
17765 not a frequent case. */
17766 if (in_ellipses_for_invisible_text_p (&start_row->start, w))
17767 return false;
17769 IF_DEBUG (debug_method_add (w, "twu1"));
17771 /* Display up to a row that can be reused. The variable
17772 last_text_row is set to the last row displayed that displays
17773 text. Note that it.vpos == 0 if or if not there is a
17774 header-line; it's not the same as the MATRIX_ROW_VPOS! */
17775 start_display (&it, w, new_start);
17776 w->cursor.vpos = -1;
17777 last_text_row = last_reused_text_row = NULL;
17779 while (it.current_y < it.last_visible_y && !f->fonts_changed)
17781 /* If we have reached into the characters in the START row,
17782 that means the line boundaries have changed. So we
17783 can't start copying with the row START. Maybe it will
17784 work to start copying with the following row. */
17785 while (IT_CHARPOS (it) > CHARPOS (start))
17787 /* Advance to the next row as the "start". */
17788 start_row++;
17789 start = start_row->minpos;
17790 /* If there are no more rows to try, or just one, give up. */
17791 if (start_row == MATRIX_MODE_LINE_ROW (w->current_matrix) - 1
17792 || w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row)
17793 || CHARPOS (start) == ZV)
17795 clear_glyph_matrix (w->desired_matrix);
17796 return false;
17799 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
17801 /* If we have reached alignment, we can copy the rest of the
17802 rows. */
17803 if (IT_CHARPOS (it) == CHARPOS (start)
17804 /* Don't accept "alignment" inside a display vector,
17805 since start_row could have started in the middle of
17806 that same display vector (thus their character
17807 positions match), and we have no way of telling if
17808 that is the case. */
17809 && it.current.dpvec_index < 0)
17810 break;
17812 it.glyph_row->reversed_p = false;
17813 if (display_line (&it, -1))
17814 last_text_row = it.glyph_row - 1;
17818 /* A value of current_y < last_visible_y means that we stopped
17819 at the previous window start, which in turn means that we
17820 have at least one reusable row. */
17821 if (it.current_y < it.last_visible_y)
17823 struct glyph_row *row;
17825 /* IT.vpos always starts from 0; it counts text lines. */
17826 nrows_scrolled = it.vpos - (start_row - MATRIX_FIRST_TEXT_ROW (w->current_matrix));
17828 /* Find PT if not already found in the lines displayed. */
17829 if (w->cursor.vpos < 0)
17831 int dy = it.current_y - start_row->y;
17833 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
17834 row = row_containing_pos (w, PT, row, NULL, dy);
17835 if (row)
17836 set_cursor_from_row (w, row, w->current_matrix, 0, 0,
17837 dy, nrows_scrolled);
17838 else
17840 clear_glyph_matrix (w->desired_matrix);
17841 return false;
17845 /* Scroll the display. Do it before the current matrix is
17846 changed. The problem here is that update has not yet
17847 run, i.e. part of the current matrix is not up to date.
17848 scroll_run_hook will clear the cursor, and use the
17849 current matrix to get the height of the row the cursor is
17850 in. */
17851 run.current_y = start_row->y;
17852 run.desired_y = it.current_y;
17853 run.height = it.last_visible_y - it.current_y;
17855 if (run.height > 0 && run.current_y != run.desired_y)
17857 update_begin (f);
17858 FRAME_RIF (f)->update_window_begin_hook (w);
17859 FRAME_RIF (f)->clear_window_mouse_face (w);
17860 FRAME_RIF (f)->scroll_run_hook (w, &run);
17861 FRAME_RIF (f)->update_window_end_hook (w, false, false);
17862 update_end (f);
17865 /* Shift current matrix down by nrows_scrolled lines. */
17866 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
17867 rotate_matrix (w->current_matrix,
17868 start_vpos,
17869 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
17870 nrows_scrolled);
17872 /* Disable lines that must be updated. */
17873 for (i = 0; i < nrows_scrolled; ++i)
17874 (start_row + i)->enabled_p = false;
17876 /* Re-compute Y positions. */
17877 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
17878 max_y = it.last_visible_y;
17879 for (row = start_row + nrows_scrolled;
17880 row < bottom_row;
17881 ++row)
17883 row->y = it.current_y;
17884 row->visible_height = row->height;
17886 if (row->y < min_y)
17887 row->visible_height -= min_y - row->y;
17888 if (row->y + row->height > max_y)
17889 row->visible_height -= row->y + row->height - max_y;
17890 if (row->fringe_bitmap_periodic_p)
17891 row->redraw_fringe_bitmaps_p = true;
17893 it.current_y += row->height;
17895 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
17896 last_reused_text_row = row;
17897 if (MATRIX_ROW_BOTTOM_Y (row) >= it.last_visible_y)
17898 break;
17901 /* Disable lines in the current matrix which are now
17902 below the window. */
17903 for (++row; row < bottom_row; ++row)
17904 row->enabled_p = row->mode_line_p = false;
17907 /* Update window_end_pos etc.; last_reused_text_row is the last
17908 reused row from the current matrix containing text, if any.
17909 The value of last_text_row is the last displayed line
17910 containing text. */
17911 if (last_reused_text_row)
17912 adjust_window_ends (w, last_reused_text_row, true);
17913 else if (last_text_row)
17914 adjust_window_ends (w, last_text_row, false);
17915 else
17917 /* This window must be completely empty. */
17918 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
17919 w->window_end_pos = Z - ZV;
17920 w->window_end_vpos = 0;
17922 w->window_end_valid = false;
17924 /* Update hint: don't try scrolling again in update_window. */
17925 w->desired_matrix->no_scrolling_p = true;
17927 #ifdef GLYPH_DEBUG
17928 debug_method_add (w, "try_window_reusing_current_matrix 1");
17929 #endif
17930 return true;
17932 else if (CHARPOS (new_start) > CHARPOS (start))
17934 struct glyph_row *pt_row, *row;
17935 struct glyph_row *first_reusable_row;
17936 struct glyph_row *first_row_to_display;
17937 int dy;
17938 int yb = window_text_bottom_y (w);
17940 /* Find the row starting at new_start, if there is one. Don't
17941 reuse a partially visible line at the end. */
17942 first_reusable_row = start_row;
17943 while (first_reusable_row->enabled_p
17944 && MATRIX_ROW_BOTTOM_Y (first_reusable_row) < yb
17945 && (MATRIX_ROW_START_CHARPOS (first_reusable_row)
17946 < CHARPOS (new_start)))
17947 ++first_reusable_row;
17949 /* Give up if there is no row to reuse. */
17950 if (MATRIX_ROW_BOTTOM_Y (first_reusable_row) >= yb
17951 || !first_reusable_row->enabled_p
17952 || (MATRIX_ROW_START_CHARPOS (first_reusable_row)
17953 != CHARPOS (new_start)))
17954 return false;
17956 /* We can reuse fully visible rows beginning with
17957 first_reusable_row to the end of the window. Set
17958 first_row_to_display to the first row that cannot be reused.
17959 Set pt_row to the row containing point, if there is any. */
17960 pt_row = NULL;
17961 for (first_row_to_display = first_reusable_row;
17962 MATRIX_ROW_BOTTOM_Y (first_row_to_display) < yb;
17963 ++first_row_to_display)
17965 if (PT >= MATRIX_ROW_START_CHARPOS (first_row_to_display)
17966 && (PT < MATRIX_ROW_END_CHARPOS (first_row_to_display)
17967 || (PT == MATRIX_ROW_END_CHARPOS (first_row_to_display)
17968 && first_row_to_display->ends_at_zv_p
17969 && pt_row == NULL)))
17970 pt_row = first_row_to_display;
17973 /* Start displaying at the start of first_row_to_display. */
17974 eassert (first_row_to_display->y < yb);
17975 init_to_row_start (&it, w, first_row_to_display);
17977 nrows_scrolled = (MATRIX_ROW_VPOS (first_reusable_row, w->current_matrix)
17978 - start_vpos);
17979 it.vpos = (MATRIX_ROW_VPOS (first_row_to_display, w->current_matrix)
17980 - nrows_scrolled);
17981 it.current_y = (first_row_to_display->y - first_reusable_row->y
17982 + WINDOW_HEADER_LINE_HEIGHT (w));
17984 /* Display lines beginning with first_row_to_display in the
17985 desired matrix. Set last_text_row to the last row displayed
17986 that displays text. */
17987 it.glyph_row = MATRIX_ROW (w->desired_matrix, it.vpos);
17988 if (pt_row == NULL)
17989 w->cursor.vpos = -1;
17990 last_text_row = NULL;
17991 while (it.current_y < it.last_visible_y && !f->fonts_changed)
17992 if (display_line (&it, w->cursor.vpos))
17993 last_text_row = it.glyph_row - 1;
17995 /* If point is in a reused row, adjust y and vpos of the cursor
17996 position. */
17997 if (pt_row)
17999 w->cursor.vpos -= nrows_scrolled;
18000 w->cursor.y -= first_reusable_row->y - start_row->y;
18003 /* Give up if point isn't in a row displayed or reused. (This
18004 also handles the case where w->cursor.vpos < nrows_scrolled
18005 after the calls to display_line, which can happen with scroll
18006 margins. See bug#1295.) */
18007 if (w->cursor.vpos < 0)
18009 clear_glyph_matrix (w->desired_matrix);
18010 return false;
18013 /* Scroll the display. */
18014 run.current_y = first_reusable_row->y;
18015 run.desired_y = WINDOW_HEADER_LINE_HEIGHT (w);
18016 run.height = it.last_visible_y - run.current_y;
18017 dy = run.current_y - run.desired_y;
18019 if (run.height)
18021 update_begin (f);
18022 FRAME_RIF (f)->update_window_begin_hook (w);
18023 FRAME_RIF (f)->clear_window_mouse_face (w);
18024 FRAME_RIF (f)->scroll_run_hook (w, &run);
18025 FRAME_RIF (f)->update_window_end_hook (w, false, false);
18026 update_end (f);
18029 /* Adjust Y positions of reused rows. */
18030 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
18031 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
18032 max_y = it.last_visible_y;
18033 for (row = first_reusable_row; row < first_row_to_display; ++row)
18035 row->y -= dy;
18036 row->visible_height = row->height;
18037 if (row->y < min_y)
18038 row->visible_height -= min_y - row->y;
18039 if (row->y + row->height > max_y)
18040 row->visible_height -= row->y + row->height - max_y;
18041 if (row->fringe_bitmap_periodic_p)
18042 row->redraw_fringe_bitmaps_p = true;
18045 /* Scroll the current matrix. */
18046 eassert (nrows_scrolled > 0);
18047 rotate_matrix (w->current_matrix,
18048 start_vpos,
18049 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
18050 -nrows_scrolled);
18052 /* Disable rows not reused. */
18053 for (row -= nrows_scrolled; row < bottom_row; ++row)
18054 row->enabled_p = false;
18056 /* Point may have moved to a different line, so we cannot assume that
18057 the previous cursor position is valid; locate the correct row. */
18058 if (pt_row)
18060 for (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
18061 row < bottom_row
18062 && PT >= MATRIX_ROW_END_CHARPOS (row)
18063 && !row->ends_at_zv_p;
18064 row++)
18066 w->cursor.vpos++;
18067 w->cursor.y = row->y;
18069 if (row < bottom_row)
18071 /* Can't simply scan the row for point with
18072 bidi-reordered glyph rows. Let set_cursor_from_row
18073 figure out where to put the cursor, and if it fails,
18074 give up. */
18075 if (!NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering)))
18077 if (!set_cursor_from_row (w, row, w->current_matrix,
18078 0, 0, 0, 0))
18080 clear_glyph_matrix (w->desired_matrix);
18081 return false;
18084 else
18086 struct glyph *glyph = row->glyphs[TEXT_AREA] + w->cursor.hpos;
18087 struct glyph *end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
18089 for (; glyph < end
18090 && (!BUFFERP (glyph->object)
18091 || glyph->charpos < PT);
18092 glyph++)
18094 w->cursor.hpos++;
18095 w->cursor.x += glyph->pixel_width;
18101 /* Adjust window end. A null value of last_text_row means that
18102 the window end is in reused rows which in turn means that
18103 only its vpos can have changed. */
18104 if (last_text_row)
18105 adjust_window_ends (w, last_text_row, false);
18106 else
18107 w->window_end_vpos -= nrows_scrolled;
18109 w->window_end_valid = false;
18110 w->desired_matrix->no_scrolling_p = true;
18112 #ifdef GLYPH_DEBUG
18113 debug_method_add (w, "try_window_reusing_current_matrix 2");
18114 #endif
18115 return true;
18118 return false;
18123 /************************************************************************
18124 Window redisplay reusing current matrix when buffer has changed
18125 ************************************************************************/
18127 static struct glyph_row *find_last_unchanged_at_beg_row (struct window *);
18128 static struct glyph_row *find_first_unchanged_at_end_row (struct window *,
18129 ptrdiff_t *, ptrdiff_t *);
18130 static struct glyph_row *
18131 find_last_row_displaying_text (struct glyph_matrix *, struct it *,
18132 struct glyph_row *);
18135 /* Return the last row in MATRIX displaying text. If row START is
18136 non-null, start searching with that row. IT gives the dimensions
18137 of the display. Value is null if matrix is empty; otherwise it is
18138 a pointer to the row found. */
18140 static struct glyph_row *
18141 find_last_row_displaying_text (struct glyph_matrix *matrix, struct it *it,
18142 struct glyph_row *start)
18144 struct glyph_row *row, *row_found;
18146 /* Set row_found to the last row in IT->w's current matrix
18147 displaying text. The loop looks funny but think of partially
18148 visible lines. */
18149 row_found = NULL;
18150 row = start ? start : MATRIX_FIRST_TEXT_ROW (matrix);
18151 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
18153 eassert (row->enabled_p);
18154 row_found = row;
18155 if (MATRIX_ROW_BOTTOM_Y (row) >= it->last_visible_y)
18156 break;
18157 ++row;
18160 return row_found;
18164 /* Return the last row in the current matrix of W that is not affected
18165 by changes at the start of current_buffer that occurred since W's
18166 current matrix was built. Value is null if no such row exists.
18168 BEG_UNCHANGED us the number of characters unchanged at the start of
18169 current_buffer. BEG + BEG_UNCHANGED is the buffer position of the
18170 first changed character in current_buffer. Characters at positions <
18171 BEG + BEG_UNCHANGED are at the same buffer positions as they were
18172 when the current matrix was built. */
18174 static struct glyph_row *
18175 find_last_unchanged_at_beg_row (struct window *w)
18177 ptrdiff_t first_changed_pos = BEG + BEG_UNCHANGED;
18178 struct glyph_row *row;
18179 struct glyph_row *row_found = NULL;
18180 int yb = window_text_bottom_y (w);
18182 /* Find the last row displaying unchanged text. */
18183 for (row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
18184 MATRIX_ROW_DISPLAYS_TEXT_P (row)
18185 && MATRIX_ROW_START_CHARPOS (row) < first_changed_pos;
18186 ++row)
18188 if (/* If row ends before first_changed_pos, it is unchanged,
18189 except in some case. */
18190 MATRIX_ROW_END_CHARPOS (row) <= first_changed_pos
18191 /* When row ends in ZV and we write at ZV it is not
18192 unchanged. */
18193 && !row->ends_at_zv_p
18194 /* When first_changed_pos is the end of a continued line,
18195 row is not unchanged because it may be no longer
18196 continued. */
18197 && !(MATRIX_ROW_END_CHARPOS (row) == first_changed_pos
18198 && (row->continued_p
18199 || row->exact_window_width_line_p))
18200 /* If ROW->end is beyond ZV, then ROW->end is outdated and
18201 needs to be recomputed, so don't consider this row as
18202 unchanged. This happens when the last line was
18203 bidi-reordered and was killed immediately before this
18204 redisplay cycle. In that case, ROW->end stores the
18205 buffer position of the first visual-order character of
18206 the killed text, which is now beyond ZV. */
18207 && CHARPOS (row->end.pos) <= ZV)
18208 row_found = row;
18210 /* Stop if last visible row. */
18211 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
18212 break;
18215 return row_found;
18219 /* Find the first glyph row in the current matrix of W that is not
18220 affected by changes at the end of current_buffer since the
18221 time W's current matrix was built.
18223 Return in *DELTA the number of chars by which buffer positions in
18224 unchanged text at the end of current_buffer must be adjusted.
18226 Return in *DELTA_BYTES the corresponding number of bytes.
18228 Value is null if no such row exists, i.e. all rows are affected by
18229 changes. */
18231 static struct glyph_row *
18232 find_first_unchanged_at_end_row (struct window *w,
18233 ptrdiff_t *delta, ptrdiff_t *delta_bytes)
18235 struct glyph_row *row;
18236 struct glyph_row *row_found = NULL;
18238 *delta = *delta_bytes = 0;
18240 /* Display must not have been paused, otherwise the current matrix
18241 is not up to date. */
18242 eassert (w->window_end_valid);
18244 /* A value of window_end_pos >= END_UNCHANGED means that the window
18245 end is in the range of changed text. If so, there is no
18246 unchanged row at the end of W's current matrix. */
18247 if (w->window_end_pos >= END_UNCHANGED)
18248 return NULL;
18250 /* Set row to the last row in W's current matrix displaying text. */
18251 row = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
18253 /* If matrix is entirely empty, no unchanged row exists. */
18254 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
18256 /* The value of row is the last glyph row in the matrix having a
18257 meaningful buffer position in it. The end position of row
18258 corresponds to window_end_pos. This allows us to translate
18259 buffer positions in the current matrix to current buffer
18260 positions for characters not in changed text. */
18261 ptrdiff_t Z_old =
18262 MATRIX_ROW_END_CHARPOS (row) + w->window_end_pos;
18263 ptrdiff_t Z_BYTE_old =
18264 MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
18265 ptrdiff_t last_unchanged_pos, last_unchanged_pos_old;
18266 struct glyph_row *first_text_row
18267 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
18269 *delta = Z - Z_old;
18270 *delta_bytes = Z_BYTE - Z_BYTE_old;
18272 /* Set last_unchanged_pos to the buffer position of the last
18273 character in the buffer that has not been changed. Z is the
18274 index + 1 of the last character in current_buffer, i.e. by
18275 subtracting END_UNCHANGED we get the index of the last
18276 unchanged character, and we have to add BEG to get its buffer
18277 position. */
18278 last_unchanged_pos = Z - END_UNCHANGED + BEG;
18279 last_unchanged_pos_old = last_unchanged_pos - *delta;
18281 /* Search backward from ROW for a row displaying a line that
18282 starts at a minimum position >= last_unchanged_pos_old. */
18283 for (; row > first_text_row; --row)
18285 /* This used to abort, but it can happen.
18286 It is ok to just stop the search instead here. KFS. */
18287 if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
18288 break;
18290 if (MATRIX_ROW_START_CHARPOS (row) >= last_unchanged_pos_old)
18291 row_found = row;
18295 eassert (!row_found || MATRIX_ROW_DISPLAYS_TEXT_P (row_found));
18297 return row_found;
18301 /* Make sure that glyph rows in the current matrix of window W
18302 reference the same glyph memory as corresponding rows in the
18303 frame's frame matrix. This function is called after scrolling W's
18304 current matrix on a terminal frame in try_window_id and
18305 try_window_reusing_current_matrix. */
18307 static void
18308 sync_frame_with_window_matrix_rows (struct window *w)
18310 struct frame *f = XFRAME (w->frame);
18311 struct glyph_row *window_row, *window_row_end, *frame_row;
18313 /* Preconditions: W must be a leaf window and full-width. Its frame
18314 must have a frame matrix. */
18315 eassert (BUFFERP (w->contents));
18316 eassert (WINDOW_FULL_WIDTH_P (w));
18317 eassert (!FRAME_WINDOW_P (f));
18319 /* If W is a full-width window, glyph pointers in W's current matrix
18320 have, by definition, to be the same as glyph pointers in the
18321 corresponding frame matrix. Note that frame matrices have no
18322 marginal areas (see build_frame_matrix). */
18323 window_row = w->current_matrix->rows;
18324 window_row_end = window_row + w->current_matrix->nrows;
18325 frame_row = f->current_matrix->rows + WINDOW_TOP_EDGE_LINE (w);
18326 while (window_row < window_row_end)
18328 struct glyph *start = window_row->glyphs[LEFT_MARGIN_AREA];
18329 struct glyph *end = window_row->glyphs[LAST_AREA];
18331 frame_row->glyphs[LEFT_MARGIN_AREA] = start;
18332 frame_row->glyphs[TEXT_AREA] = start;
18333 frame_row->glyphs[RIGHT_MARGIN_AREA] = end;
18334 frame_row->glyphs[LAST_AREA] = end;
18336 /* Disable frame rows whose corresponding window rows have
18337 been disabled in try_window_id. */
18338 if (!window_row->enabled_p)
18339 frame_row->enabled_p = false;
18341 ++window_row, ++frame_row;
18346 /* Find the glyph row in window W containing CHARPOS. Consider all
18347 rows between START and END (not inclusive). END null means search
18348 all rows to the end of the display area of W. Value is the row
18349 containing CHARPOS or null. */
18351 struct glyph_row *
18352 row_containing_pos (struct window *w, ptrdiff_t charpos,
18353 struct glyph_row *start, struct glyph_row *end, int dy)
18355 struct glyph_row *row = start;
18356 struct glyph_row *best_row = NULL;
18357 ptrdiff_t mindif = BUF_ZV (XBUFFER (w->contents)) + 1;
18358 int last_y;
18360 /* If we happen to start on a header-line, skip that. */
18361 if (row->mode_line_p)
18362 ++row;
18364 if ((end && row >= end) || !row->enabled_p)
18365 return NULL;
18367 last_y = window_text_bottom_y (w) - dy;
18369 while (true)
18371 /* Give up if we have gone too far. */
18372 if ((end && row >= end) || !row->enabled_p)
18373 return NULL;
18374 /* This formerly returned if they were equal.
18375 I think that both quantities are of a "last plus one" type;
18376 if so, when they are equal, the row is within the screen. -- rms. */
18377 if (MATRIX_ROW_BOTTOM_Y (row) > last_y)
18378 return NULL;
18380 /* If it is in this row, return this row. */
18381 if (! (MATRIX_ROW_END_CHARPOS (row) < charpos
18382 || (MATRIX_ROW_END_CHARPOS (row) == charpos
18383 /* The end position of a row equals the start
18384 position of the next row. If CHARPOS is there, we
18385 would rather consider it displayed in the next
18386 line, except when this line ends in ZV. */
18387 && !row_for_charpos_p (row, charpos)))
18388 && charpos >= MATRIX_ROW_START_CHARPOS (row))
18390 struct glyph *g;
18392 if (NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering))
18393 || (!best_row && !row->continued_p))
18394 return row;
18395 /* In bidi-reordered rows, there could be several rows whose
18396 edges surround CHARPOS, all of these rows belonging to
18397 the same continued line. We need to find the row which
18398 fits CHARPOS the best. */
18399 for (g = row->glyphs[TEXT_AREA];
18400 g < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
18401 g++)
18403 if (!STRINGP (g->object))
18405 if (g->charpos > 0 && eabs (g->charpos - charpos) < mindif)
18407 mindif = eabs (g->charpos - charpos);
18408 best_row = row;
18409 /* Exact match always wins. */
18410 if (mindif == 0)
18411 return best_row;
18416 else if (best_row && !row->continued_p)
18417 return best_row;
18418 ++row;
18423 /* Try to redisplay window W by reusing its existing display. W's
18424 current matrix must be up to date when this function is called,
18425 i.e., window_end_valid must be true.
18427 Value is
18429 >= 1 if successful, i.e. display has been updated
18430 specifically:
18431 1 means the changes were in front of a newline that precedes
18432 the window start, and the whole current matrix was reused
18433 2 means the changes were after the last position displayed
18434 in the window, and the whole current matrix was reused
18435 3 means portions of the current matrix were reused, while
18436 some of the screen lines were redrawn
18437 -1 if redisplay with same window start is known not to succeed
18438 0 if otherwise unsuccessful
18440 The following steps are performed:
18442 1. Find the last row in the current matrix of W that is not
18443 affected by changes at the start of current_buffer. If no such row
18444 is found, give up.
18446 2. Find the first row in W's current matrix that is not affected by
18447 changes at the end of current_buffer. Maybe there is no such row.
18449 3. Display lines beginning with the row + 1 found in step 1 to the
18450 row found in step 2 or, if step 2 didn't find a row, to the end of
18451 the window.
18453 4. If cursor is not known to appear on the window, give up.
18455 5. If display stopped at the row found in step 2, scroll the
18456 display and current matrix as needed.
18458 6. Maybe display some lines at the end of W, if we must. This can
18459 happen under various circumstances, like a partially visible line
18460 becoming fully visible, or because newly displayed lines are displayed
18461 in smaller font sizes.
18463 7. Update W's window end information. */
18465 static int
18466 try_window_id (struct window *w)
18468 struct frame *f = XFRAME (w->frame);
18469 struct glyph_matrix *current_matrix = w->current_matrix;
18470 struct glyph_matrix *desired_matrix = w->desired_matrix;
18471 struct glyph_row *last_unchanged_at_beg_row;
18472 struct glyph_row *first_unchanged_at_end_row;
18473 struct glyph_row *row;
18474 struct glyph_row *bottom_row;
18475 int bottom_vpos;
18476 struct it it;
18477 ptrdiff_t delta = 0, delta_bytes = 0, stop_pos;
18478 int dvpos, dy;
18479 struct text_pos start_pos;
18480 struct run run;
18481 int first_unchanged_at_end_vpos = 0;
18482 struct glyph_row *last_text_row, *last_text_row_at_end;
18483 struct text_pos start;
18484 ptrdiff_t first_changed_charpos, last_changed_charpos;
18486 #ifdef GLYPH_DEBUG
18487 if (inhibit_try_window_id)
18488 return 0;
18489 #endif
18491 /* This is handy for debugging. */
18492 #if false
18493 #define GIVE_UP(X) \
18494 do { \
18495 TRACE ((stderr, "try_window_id give up %d\n", (X))); \
18496 return 0; \
18497 } while (false)
18498 #else
18499 #define GIVE_UP(X) return 0
18500 #endif
18502 SET_TEXT_POS_FROM_MARKER (start, w->start);
18504 /* Don't use this for mini-windows because these can show
18505 messages and mini-buffers, and we don't handle that here. */
18506 if (MINI_WINDOW_P (w))
18507 GIVE_UP (1);
18509 /* This flag is used to prevent redisplay optimizations. */
18510 if (windows_or_buffers_changed || f->cursor_type_changed)
18511 GIVE_UP (2);
18513 /* This function's optimizations cannot be used if overlays have
18514 changed in the buffer displayed by the window, so give up if they
18515 have. */
18516 if (w->last_overlay_modified != OVERLAY_MODIFF)
18517 GIVE_UP (200);
18519 /* Verify that narrowing has not changed.
18520 Also verify that we were not told to prevent redisplay optimizations.
18521 It would be nice to further
18522 reduce the number of cases where this prevents try_window_id. */
18523 if (current_buffer->clip_changed
18524 || current_buffer->prevent_redisplay_optimizations_p)
18525 GIVE_UP (3);
18527 /* Window must either use window-based redisplay or be full width. */
18528 if (!FRAME_WINDOW_P (f)
18529 && (!FRAME_LINE_INS_DEL_OK (f)
18530 || !WINDOW_FULL_WIDTH_P (w)))
18531 GIVE_UP (4);
18533 /* Give up if point is known NOT to appear in W. */
18534 if (PT < CHARPOS (start))
18535 GIVE_UP (5);
18537 /* Another way to prevent redisplay optimizations. */
18538 if (w->last_modified == 0)
18539 GIVE_UP (6);
18541 /* Verify that window is not hscrolled. */
18542 if (w->hscroll != 0)
18543 GIVE_UP (7);
18545 /* Verify that display wasn't paused. */
18546 if (!w->window_end_valid)
18547 GIVE_UP (8);
18549 /* Likewise if highlighting trailing whitespace. */
18550 if (!NILP (Vshow_trailing_whitespace))
18551 GIVE_UP (11);
18553 /* Can't use this if overlay arrow position and/or string have
18554 changed. */
18555 if (overlay_arrows_changed_p (false))
18556 GIVE_UP (12);
18558 /* When word-wrap is on, adding a space to the first word of a
18559 wrapped line can change the wrap position, altering the line
18560 above it. It might be worthwhile to handle this more
18561 intelligently, but for now just redisplay from scratch. */
18562 if (!NILP (BVAR (XBUFFER (w->contents), word_wrap)))
18563 GIVE_UP (21);
18565 /* Under bidi reordering, adding or deleting a character in the
18566 beginning of a paragraph, before the first strong directional
18567 character, can change the base direction of the paragraph (unless
18568 the buffer specifies a fixed paragraph direction), which will
18569 require redisplaying the whole paragraph. It might be worthwhile
18570 to find the paragraph limits and widen the range of redisplayed
18571 lines to that, but for now just give up this optimization and
18572 redisplay from scratch. */
18573 if (!NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering))
18574 && NILP (BVAR (XBUFFER (w->contents), bidi_paragraph_direction)))
18575 GIVE_UP (22);
18577 /* Give up if the buffer has line-spacing set, as Lisp-level changes
18578 to that variable require thorough redisplay. */
18579 if (!NILP (BVAR (XBUFFER (w->contents), extra_line_spacing)))
18580 GIVE_UP (23);
18582 /* Give up if display-line-numbers is in relative mode, or when the
18583 current line's number needs to be displayed in a distinct face. */
18584 if (EQ (Vdisplay_line_numbers, Qrelative)
18585 || EQ (Vdisplay_line_numbers, Qvisual)
18586 || (!NILP (Vdisplay_line_numbers)
18587 && NILP (Finternal_lisp_face_equal_p (Qline_number,
18588 Qline_number_current_line,
18589 w->frame))))
18590 GIVE_UP (24);
18592 /* Make sure beg_unchanged and end_unchanged are up to date. Do it
18593 only if buffer has really changed. The reason is that the gap is
18594 initially at Z for freshly visited files. The code below would
18595 set end_unchanged to 0 in that case. */
18596 if (MODIFF > SAVE_MODIFF
18597 /* This seems to happen sometimes after saving a buffer. */
18598 || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
18600 if (GPT - BEG < BEG_UNCHANGED)
18601 BEG_UNCHANGED = GPT - BEG;
18602 if (Z - GPT < END_UNCHANGED)
18603 END_UNCHANGED = Z - GPT;
18606 /* The position of the first and last character that has been changed. */
18607 first_changed_charpos = BEG + BEG_UNCHANGED;
18608 last_changed_charpos = Z - END_UNCHANGED;
18610 /* If window starts after a line end, and the last change is in
18611 front of that newline, then changes don't affect the display.
18612 This case happens with stealth-fontification. Note that although
18613 the display is unchanged, glyph positions in the matrix have to
18614 be adjusted, of course. */
18615 row = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
18616 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
18617 && ((last_changed_charpos < CHARPOS (start)
18618 && CHARPOS (start) == BEGV)
18619 || (last_changed_charpos < CHARPOS (start) - 1
18620 && FETCH_BYTE (BYTEPOS (start) - 1) == '\n')))
18622 ptrdiff_t Z_old, Z_delta, Z_BYTE_old, Z_delta_bytes;
18623 struct glyph_row *r0;
18625 /* Compute how many chars/bytes have been added to or removed
18626 from the buffer. */
18627 Z_old = MATRIX_ROW_END_CHARPOS (row) + w->window_end_pos;
18628 Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
18629 Z_delta = Z - Z_old;
18630 Z_delta_bytes = Z_BYTE - Z_BYTE_old;
18632 /* Give up if PT is not in the window. Note that it already has
18633 been checked at the start of try_window_id that PT is not in
18634 front of the window start. */
18635 if (PT >= MATRIX_ROW_END_CHARPOS (row) + Z_delta)
18636 GIVE_UP (13);
18638 /* If window start is unchanged, we can reuse the whole matrix
18639 as is, after adjusting glyph positions. No need to compute
18640 the window end again, since its offset from Z hasn't changed. */
18641 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
18642 if (CHARPOS (start) == MATRIX_ROW_START_CHARPOS (r0) + Z_delta
18643 && BYTEPOS (start) == MATRIX_ROW_START_BYTEPOS (r0) + Z_delta_bytes
18644 /* PT must not be in a partially visible line. */
18645 && !(PT >= MATRIX_ROW_START_CHARPOS (row) + Z_delta
18646 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
18648 /* Adjust positions in the glyph matrix. */
18649 if (Z_delta || Z_delta_bytes)
18651 struct glyph_row *r1
18652 = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
18653 increment_matrix_positions (w->current_matrix,
18654 MATRIX_ROW_VPOS (r0, current_matrix),
18655 MATRIX_ROW_VPOS (r1, current_matrix),
18656 Z_delta, Z_delta_bytes);
18659 /* Set the cursor. */
18660 row = row_containing_pos (w, PT, r0, NULL, 0);
18661 if (row)
18662 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
18663 return 1;
18667 /* Handle the case that changes are all below what is displayed in
18668 the window, and that PT is in the window. This shortcut cannot
18669 be taken if ZV is visible in the window, and text has been added
18670 there that is visible in the window. */
18671 if (first_changed_charpos >= MATRIX_ROW_END_CHARPOS (row)
18672 /* ZV is not visible in the window, or there are no
18673 changes at ZV, actually. */
18674 && (current_matrix->zv > MATRIX_ROW_END_CHARPOS (row)
18675 || first_changed_charpos == last_changed_charpos))
18677 struct glyph_row *r0;
18679 /* Give up if PT is not in the window. Note that it already has
18680 been checked at the start of try_window_id that PT is not in
18681 front of the window start. */
18682 if (PT >= MATRIX_ROW_END_CHARPOS (row))
18683 GIVE_UP (14);
18685 /* If window start is unchanged, we can reuse the whole matrix
18686 as is, without changing glyph positions since no text has
18687 been added/removed in front of the window end. */
18688 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
18689 if (TEXT_POS_EQUAL_P (start, r0->minpos)
18690 /* PT must not be in a partially visible line. */
18691 && !(PT >= MATRIX_ROW_START_CHARPOS (row)
18692 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
18694 /* We have to compute the window end anew since text
18695 could have been added/removed after it. */
18696 w->window_end_pos = Z - MATRIX_ROW_END_CHARPOS (row);
18697 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
18699 /* Set the cursor. */
18700 row = row_containing_pos (w, PT, r0, NULL, 0);
18701 if (row)
18702 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
18703 return 2;
18707 /* Give up if window start is in the changed area.
18709 The condition used to read
18711 (BEG_UNCHANGED + END_UNCHANGED != Z - BEG && ...)
18713 but why that was tested escapes me at the moment. */
18714 if (CHARPOS (start) >= first_changed_charpos
18715 && CHARPOS (start) <= last_changed_charpos)
18716 GIVE_UP (15);
18718 /* Check that window start agrees with the start of the first glyph
18719 row in its current matrix. Check this after we know the window
18720 start is not in changed text, otherwise positions would not be
18721 comparable. */
18722 row = MATRIX_FIRST_TEXT_ROW (current_matrix);
18723 if (!TEXT_POS_EQUAL_P (start, row->minpos))
18724 GIVE_UP (16);
18726 /* Give up if the window ends in strings. Overlay strings
18727 at the end are difficult to handle, so don't try. */
18728 row = MATRIX_ROW (current_matrix, w->window_end_vpos);
18729 if (MATRIX_ROW_START_CHARPOS (row) == MATRIX_ROW_END_CHARPOS (row))
18730 GIVE_UP (20);
18732 /* Compute the position at which we have to start displaying new
18733 lines. Some of the lines at the top of the window might be
18734 reusable because they are not displaying changed text. Find the
18735 last row in W's current matrix not affected by changes at the
18736 start of current_buffer. Value is null if changes start in the
18737 first line of window. */
18738 last_unchanged_at_beg_row = find_last_unchanged_at_beg_row (w);
18739 if (last_unchanged_at_beg_row)
18741 /* Avoid starting to display in the middle of a character, a TAB
18742 for instance. This is easier than to set up the iterator
18743 exactly, and it's not a frequent case, so the additional
18744 effort wouldn't really pay off. */
18745 while ((MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row)
18746 || last_unchanged_at_beg_row->ends_in_newline_from_string_p)
18747 && last_unchanged_at_beg_row > w->current_matrix->rows)
18748 --last_unchanged_at_beg_row;
18750 if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row))
18751 GIVE_UP (17);
18753 if (! init_to_row_end (&it, w, last_unchanged_at_beg_row))
18754 GIVE_UP (18);
18755 start_pos = it.current.pos;
18757 /* Start displaying new lines in the desired matrix at the same
18758 vpos we would use in the current matrix, i.e. below
18759 last_unchanged_at_beg_row. */
18760 it.vpos = 1 + MATRIX_ROW_VPOS (last_unchanged_at_beg_row,
18761 current_matrix);
18762 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
18763 it.current_y = MATRIX_ROW_BOTTOM_Y (last_unchanged_at_beg_row);
18765 eassert (it.hpos == 0 && it.current_x == 0);
18767 else
18769 /* There are no reusable lines at the start of the window.
18770 Start displaying in the first text line. */
18771 start_display (&it, w, start);
18772 it.vpos = it.first_vpos;
18773 start_pos = it.current.pos;
18776 /* Find the first row that is not affected by changes at the end of
18777 the buffer. Value will be null if there is no unchanged row, in
18778 which case we must redisplay to the end of the window. delta
18779 will be set to the value by which buffer positions beginning with
18780 first_unchanged_at_end_row have to be adjusted due to text
18781 changes. */
18782 first_unchanged_at_end_row
18783 = find_first_unchanged_at_end_row (w, &delta, &delta_bytes);
18784 IF_DEBUG (debug_delta = delta);
18785 IF_DEBUG (debug_delta_bytes = delta_bytes);
18787 /* Set stop_pos to the buffer position up to which we will have to
18788 display new lines. If first_unchanged_at_end_row != NULL, this
18789 is the buffer position of the start of the line displayed in that
18790 row. For first_unchanged_at_end_row == NULL, use 0 to indicate
18791 that we don't stop at a buffer position. */
18792 stop_pos = 0;
18793 if (first_unchanged_at_end_row)
18795 eassert (last_unchanged_at_beg_row == NULL
18796 || first_unchanged_at_end_row >= last_unchanged_at_beg_row);
18798 /* If this is a continuation line, move forward to the next one
18799 that isn't. Changes in lines above affect this line.
18800 Caution: this may move first_unchanged_at_end_row to a row
18801 not displaying text. */
18802 while (MATRIX_ROW_CONTINUATION_LINE_P (first_unchanged_at_end_row)
18803 && MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
18804 && (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
18805 < it.last_visible_y))
18806 ++first_unchanged_at_end_row;
18808 if (!MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
18809 || (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
18810 >= it.last_visible_y))
18811 first_unchanged_at_end_row = NULL;
18812 else
18814 stop_pos = (MATRIX_ROW_START_CHARPOS (first_unchanged_at_end_row)
18815 + delta);
18816 first_unchanged_at_end_vpos
18817 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, current_matrix);
18818 eassert (stop_pos >= Z - END_UNCHANGED);
18821 else if (last_unchanged_at_beg_row == NULL)
18822 GIVE_UP (19);
18825 #ifdef GLYPH_DEBUG
18827 /* Either there is no unchanged row at the end, or the one we have
18828 now displays text. This is a necessary condition for the window
18829 end pos calculation at the end of this function. */
18830 eassert (first_unchanged_at_end_row == NULL
18831 || MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
18833 debug_last_unchanged_at_beg_vpos
18834 = (last_unchanged_at_beg_row
18835 ? MATRIX_ROW_VPOS (last_unchanged_at_beg_row, current_matrix)
18836 : -1);
18837 debug_first_unchanged_at_end_vpos = first_unchanged_at_end_vpos;
18839 #endif /* GLYPH_DEBUG */
18842 /* Display new lines. Set last_text_row to the last new line
18843 displayed which has text on it, i.e. might end up as being the
18844 line where the window_end_vpos is. */
18845 w->cursor.vpos = -1;
18846 last_text_row = NULL;
18847 overlay_arrow_seen = false;
18848 if (it.current_y < it.last_visible_y
18849 && !f->fonts_changed
18850 && (first_unchanged_at_end_row == NULL
18851 || IT_CHARPOS (it) < stop_pos))
18852 it.glyph_row->reversed_p = false;
18853 while (it.current_y < it.last_visible_y
18854 && !f->fonts_changed
18855 && (first_unchanged_at_end_row == NULL
18856 || IT_CHARPOS (it) < stop_pos))
18858 if (display_line (&it, -1))
18859 last_text_row = it.glyph_row - 1;
18862 if (f->fonts_changed)
18863 return -1;
18865 /* The redisplay iterations in display_line above could have
18866 triggered font-lock, which could have done something that
18867 invalidates IT->w window's end-point information, on which we
18868 rely below. E.g., one package, which will remain unnamed, used
18869 to install a font-lock-fontify-region-function that called
18870 bury-buffer, whose side effect is to switch the buffer displayed
18871 by IT->w, and that predictably resets IT->w's window_end_valid
18872 flag, which we already tested at the entry to this function.
18873 Amply punish such packages/modes by giving up on this
18874 optimization in those cases. */
18875 if (!w->window_end_valid)
18877 clear_glyph_matrix (w->desired_matrix);
18878 return -1;
18881 /* Compute differences in buffer positions, y-positions etc. for
18882 lines reused at the bottom of the window. Compute what we can
18883 scroll. */
18884 if (first_unchanged_at_end_row
18885 /* No lines reused because we displayed everything up to the
18886 bottom of the window. */
18887 && it.current_y < it.last_visible_y)
18889 dvpos = (it.vpos
18890 - MATRIX_ROW_VPOS (first_unchanged_at_end_row,
18891 current_matrix));
18892 dy = it.current_y - first_unchanged_at_end_row->y;
18893 run.current_y = first_unchanged_at_end_row->y;
18894 run.desired_y = run.current_y + dy;
18895 run.height = it.last_visible_y - max (run.current_y, run.desired_y);
18897 else
18899 delta = delta_bytes = dvpos = dy
18900 = run.current_y = run.desired_y = run.height = 0;
18901 first_unchanged_at_end_row = NULL;
18903 IF_DEBUG ((debug_dvpos = dvpos, debug_dy = dy));
18906 /* Find the cursor if not already found. We have to decide whether
18907 PT will appear on this window (it sometimes doesn't, but this is
18908 not a very frequent case.) This decision has to be made before
18909 the current matrix is altered. A value of cursor.vpos < 0 means
18910 that PT is either in one of the lines beginning at
18911 first_unchanged_at_end_row or below the window. Don't care for
18912 lines that might be displayed later at the window end; as
18913 mentioned, this is not a frequent case. */
18914 if (w->cursor.vpos < 0)
18916 /* Cursor in unchanged rows at the top? */
18917 if (PT < CHARPOS (start_pos)
18918 && last_unchanged_at_beg_row)
18920 row = row_containing_pos (w, PT,
18921 MATRIX_FIRST_TEXT_ROW (w->current_matrix),
18922 last_unchanged_at_beg_row + 1, 0);
18923 if (row)
18924 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
18927 /* Start from first_unchanged_at_end_row looking for PT. */
18928 else if (first_unchanged_at_end_row)
18930 row = row_containing_pos (w, PT - delta,
18931 first_unchanged_at_end_row, NULL, 0);
18932 if (row)
18933 set_cursor_from_row (w, row, w->current_matrix, delta,
18934 delta_bytes, dy, dvpos);
18937 /* Give up if cursor was not found. */
18938 if (w->cursor.vpos < 0)
18940 clear_glyph_matrix (w->desired_matrix);
18941 return -1;
18945 /* Don't let the cursor end in the scroll margins. */
18947 int this_scroll_margin = window_scroll_margin (w, MARGIN_IN_PIXELS);
18948 int cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
18950 if ((w->cursor.y < this_scroll_margin
18951 && CHARPOS (start) > BEGV)
18952 /* Old redisplay didn't take scroll margin into account at the bottom,
18953 but then global-hl-line-mode doesn't scroll. KFS 2004-06-14 */
18954 || (w->cursor.y + (make_cursor_line_fully_visible_p
18955 ? cursor_height + this_scroll_margin
18956 : 1)) > it.last_visible_y)
18958 w->cursor.vpos = -1;
18959 clear_glyph_matrix (w->desired_matrix);
18960 return -1;
18964 /* Scroll the display. Do it before changing the current matrix so
18965 that xterm.c doesn't get confused about where the cursor glyph is
18966 found. */
18967 if (dy && run.height)
18969 update_begin (f);
18971 if (FRAME_WINDOW_P (f))
18973 FRAME_RIF (f)->update_window_begin_hook (w);
18974 FRAME_RIF (f)->clear_window_mouse_face (w);
18975 FRAME_RIF (f)->scroll_run_hook (w, &run);
18976 FRAME_RIF (f)->update_window_end_hook (w, false, false);
18978 else
18980 /* Terminal frame. In this case, dvpos gives the number of
18981 lines to scroll by; dvpos < 0 means scroll up. */
18982 int from_vpos
18983 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, w->current_matrix);
18984 int from = WINDOW_TOP_EDGE_LINE (w) + from_vpos;
18985 int end = (WINDOW_TOP_EDGE_LINE (w)
18986 + window_wants_header_line (w)
18987 + window_internal_height (w));
18989 #if defined (HAVE_GPM) || defined (MSDOS)
18990 x_clear_window_mouse_face (w);
18991 #endif
18992 /* Perform the operation on the screen. */
18993 if (dvpos > 0)
18995 /* Scroll last_unchanged_at_beg_row to the end of the
18996 window down dvpos lines. */
18997 set_terminal_window (f, end);
18999 /* On dumb terminals delete dvpos lines at the end
19000 before inserting dvpos empty lines. */
19001 if (!FRAME_SCROLL_REGION_OK (f))
19002 ins_del_lines (f, end - dvpos, -dvpos);
19004 /* Insert dvpos empty lines in front of
19005 last_unchanged_at_beg_row. */
19006 ins_del_lines (f, from, dvpos);
19008 else if (dvpos < 0)
19010 /* Scroll up last_unchanged_at_beg_vpos to the end of
19011 the window to last_unchanged_at_beg_vpos - |dvpos|. */
19012 set_terminal_window (f, end);
19014 /* Delete dvpos lines in front of
19015 last_unchanged_at_beg_vpos. ins_del_lines will set
19016 the cursor to the given vpos and emit |dvpos| delete
19017 line sequences. */
19018 ins_del_lines (f, from + dvpos, dvpos);
19020 /* On a dumb terminal insert dvpos empty lines at the
19021 end. */
19022 if (!FRAME_SCROLL_REGION_OK (f))
19023 ins_del_lines (f, end + dvpos, -dvpos);
19026 set_terminal_window (f, 0);
19029 update_end (f);
19032 /* Shift reused rows of the current matrix to the right position.
19033 BOTTOM_ROW is the last + 1 row in the current matrix reserved for
19034 text. */
19035 bottom_row = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
19036 bottom_vpos = MATRIX_ROW_VPOS (bottom_row, current_matrix);
19037 if (dvpos < 0)
19039 rotate_matrix (current_matrix, first_unchanged_at_end_vpos + dvpos,
19040 bottom_vpos, dvpos);
19041 clear_glyph_matrix_rows (current_matrix, bottom_vpos + dvpos,
19042 bottom_vpos);
19044 else if (dvpos > 0)
19046 rotate_matrix (current_matrix, first_unchanged_at_end_vpos,
19047 bottom_vpos, dvpos);
19048 clear_glyph_matrix_rows (current_matrix, first_unchanged_at_end_vpos,
19049 first_unchanged_at_end_vpos + dvpos);
19052 /* For frame-based redisplay, make sure that current frame and window
19053 matrix are in sync with respect to glyph memory. */
19054 if (!FRAME_WINDOW_P (f))
19055 sync_frame_with_window_matrix_rows (w);
19057 /* Adjust buffer positions in reused rows. */
19058 if (delta || delta_bytes)
19059 increment_matrix_positions (current_matrix,
19060 first_unchanged_at_end_vpos + dvpos,
19061 bottom_vpos, delta, delta_bytes);
19063 /* Adjust Y positions. */
19064 if (dy)
19065 shift_glyph_matrix (w, current_matrix,
19066 first_unchanged_at_end_vpos + dvpos,
19067 bottom_vpos, dy);
19069 if (first_unchanged_at_end_row)
19071 first_unchanged_at_end_row += dvpos;
19072 if (first_unchanged_at_end_row->y >= it.last_visible_y
19073 || !MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row))
19074 first_unchanged_at_end_row = NULL;
19077 /* If scrolling up, there may be some lines to display at the end of
19078 the window. */
19079 last_text_row_at_end = NULL;
19080 if (dy < 0)
19082 /* Scrolling up can leave for example a partially visible line
19083 at the end of the window to be redisplayed. */
19084 /* Set last_row to the glyph row in the current matrix where the
19085 window end line is found. It has been moved up or down in
19086 the matrix by dvpos. */
19087 int last_vpos = w->window_end_vpos + dvpos;
19088 struct glyph_row *last_row = MATRIX_ROW (current_matrix, last_vpos);
19090 /* If last_row is the window end line, it should display text. */
19091 eassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_row));
19093 /* If window end line was partially visible before, begin
19094 displaying at that line. Otherwise begin displaying with the
19095 line following it. */
19096 if (MATRIX_ROW_BOTTOM_Y (last_row) - dy >= it.last_visible_y)
19098 init_to_row_start (&it, w, last_row);
19099 it.vpos = last_vpos;
19100 it.current_y = last_row->y;
19102 else
19104 init_to_row_end (&it, w, last_row);
19105 it.vpos = 1 + last_vpos;
19106 it.current_y = MATRIX_ROW_BOTTOM_Y (last_row);
19107 ++last_row;
19110 /* We may start in a continuation line. If so, we have to
19111 get the right continuation_lines_width and current_x. */
19112 it.continuation_lines_width = last_row->continuation_lines_width;
19113 it.hpos = it.current_x = 0;
19115 /* Display the rest of the lines at the window end. */
19116 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
19117 while (it.current_y < it.last_visible_y && !f->fonts_changed)
19119 /* Is it always sure that the display agrees with lines in
19120 the current matrix? I don't think so, so we mark rows
19121 displayed invalid in the current matrix by setting their
19122 enabled_p flag to false. */
19123 SET_MATRIX_ROW_ENABLED_P (w->current_matrix, it.vpos, false);
19124 if (display_line (&it, w->cursor.vpos))
19125 last_text_row_at_end = it.glyph_row - 1;
19129 /* Update window_end_pos and window_end_vpos. */
19130 if (first_unchanged_at_end_row && !last_text_row_at_end)
19132 /* Window end line if one of the preserved rows from the current
19133 matrix. Set row to the last row displaying text in current
19134 matrix starting at first_unchanged_at_end_row, after
19135 scrolling. */
19136 eassert (MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
19137 row = find_last_row_displaying_text (w->current_matrix, &it,
19138 first_unchanged_at_end_row);
19139 eassume (row && MATRIX_ROW_DISPLAYS_TEXT_P (row));
19140 adjust_window_ends (w, row, true);
19141 eassert (w->window_end_bytepos >= 0);
19142 IF_DEBUG (debug_method_add (w, "A"));
19144 else if (last_text_row_at_end)
19146 adjust_window_ends (w, last_text_row_at_end, false);
19147 eassert (w->window_end_bytepos >= 0);
19148 IF_DEBUG (debug_method_add (w, "B"));
19150 else if (last_text_row)
19152 /* We have displayed either to the end of the window or at the
19153 end of the window, i.e. the last row with text is to be found
19154 in the desired matrix. */
19155 adjust_window_ends (w, last_text_row, false);
19156 eassert (w->window_end_bytepos >= 0);
19158 else if (first_unchanged_at_end_row == NULL
19159 && last_text_row == NULL
19160 && last_text_row_at_end == NULL)
19162 /* Displayed to end of window, but no line containing text was
19163 displayed. Lines were deleted at the end of the window. */
19164 bool first_vpos = window_wants_header_line (w);
19165 int vpos = w->window_end_vpos;
19166 struct glyph_row *current_row = current_matrix->rows + vpos;
19167 struct glyph_row *desired_row = desired_matrix->rows + vpos;
19169 for (row = NULL; !row; --vpos, --current_row, --desired_row)
19171 eassert (first_vpos <= vpos);
19172 if (desired_row->enabled_p)
19174 if (MATRIX_ROW_DISPLAYS_TEXT_P (desired_row))
19175 row = desired_row;
19177 else if (MATRIX_ROW_DISPLAYS_TEXT_P (current_row))
19178 row = current_row;
19181 w->window_end_vpos = vpos + 1;
19182 w->window_end_pos = Z - MATRIX_ROW_END_CHARPOS (row);
19183 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
19184 eassert (w->window_end_bytepos >= 0);
19185 IF_DEBUG (debug_method_add (w, "C"));
19187 else
19188 emacs_abort ();
19190 IF_DEBUG ((debug_end_pos = w->window_end_pos,
19191 debug_end_vpos = w->window_end_vpos));
19193 /* Record that display has not been completed. */
19194 w->window_end_valid = false;
19195 w->desired_matrix->no_scrolling_p = true;
19196 return 3;
19198 #undef GIVE_UP
19203 /***********************************************************************
19204 More debugging support
19205 ***********************************************************************/
19207 #ifdef GLYPH_DEBUG
19209 void dump_glyph_row (struct glyph_row *, int, int) EXTERNALLY_VISIBLE;
19210 void dump_glyph_matrix (struct glyph_matrix *, int) EXTERNALLY_VISIBLE;
19211 void dump_glyph (struct glyph_row *, struct glyph *, int) EXTERNALLY_VISIBLE;
19214 /* Dump the contents of glyph matrix MATRIX on stderr.
19216 GLYPHS 0 means don't show glyph contents.
19217 GLYPHS 1 means show glyphs in short form
19218 GLYPHS > 1 means show glyphs in long form. */
19220 void
19221 dump_glyph_matrix (struct glyph_matrix *matrix, int glyphs)
19223 int i;
19224 for (i = 0; i < matrix->nrows; ++i)
19225 dump_glyph_row (MATRIX_ROW (matrix, i), i, glyphs);
19229 /* Dump contents of glyph GLYPH to stderr. ROW and AREA are
19230 the glyph row and area where the glyph comes from. */
19232 void
19233 dump_glyph (struct glyph_row *row, struct glyph *glyph, int area)
19235 if (glyph->type == CHAR_GLYPH
19236 || glyph->type == GLYPHLESS_GLYPH)
19238 fprintf (stderr,
19239 " %5"pD"d %c %9"pD"d %c %3d 0x%06x %c %4d %1.1d%1.1d\n",
19240 glyph - row->glyphs[TEXT_AREA],
19241 (glyph->type == CHAR_GLYPH
19242 ? 'C'
19243 : 'G'),
19244 glyph->charpos,
19245 (BUFFERP (glyph->object)
19246 ? 'B'
19247 : (STRINGP (glyph->object)
19248 ? 'S'
19249 : (NILP (glyph->object)
19250 ? '0'
19251 : '-'))),
19252 glyph->pixel_width,
19253 glyph->u.ch,
19254 (glyph->u.ch < 0x80 && glyph->u.ch >= ' '
19255 ? (int) glyph->u.ch
19256 : '.'),
19257 glyph->face_id,
19258 glyph->left_box_line_p,
19259 glyph->right_box_line_p);
19261 else if (glyph->type == STRETCH_GLYPH)
19263 fprintf (stderr,
19264 " %5"pD"d %c %9"pD"d %c %3d 0x%06x %c %4d %1.1d%1.1d\n",
19265 glyph - row->glyphs[TEXT_AREA],
19266 'S',
19267 glyph->charpos,
19268 (BUFFERP (glyph->object)
19269 ? 'B'
19270 : (STRINGP (glyph->object)
19271 ? 'S'
19272 : (NILP (glyph->object)
19273 ? '0'
19274 : '-'))),
19275 glyph->pixel_width,
19277 ' ',
19278 glyph->face_id,
19279 glyph->left_box_line_p,
19280 glyph->right_box_line_p);
19282 else if (glyph->type == IMAGE_GLYPH)
19284 fprintf (stderr,
19285 " %5"pD"d %c %9"pD"d %c %3d 0x%06x %c %4d %1.1d%1.1d\n",
19286 glyph - row->glyphs[TEXT_AREA],
19287 'I',
19288 glyph->charpos,
19289 (BUFFERP (glyph->object)
19290 ? 'B'
19291 : (STRINGP (glyph->object)
19292 ? 'S'
19293 : (NILP (glyph->object)
19294 ? '0'
19295 : '-'))),
19296 glyph->pixel_width,
19297 (unsigned int) glyph->u.img_id,
19298 '.',
19299 glyph->face_id,
19300 glyph->left_box_line_p,
19301 glyph->right_box_line_p);
19303 else if (glyph->type == COMPOSITE_GLYPH)
19305 fprintf (stderr,
19306 " %5"pD"d %c %9"pD"d %c %3d 0x%06x",
19307 glyph - row->glyphs[TEXT_AREA],
19308 '+',
19309 glyph->charpos,
19310 (BUFFERP (glyph->object)
19311 ? 'B'
19312 : (STRINGP (glyph->object)
19313 ? 'S'
19314 : (NILP (glyph->object)
19315 ? '0'
19316 : '-'))),
19317 glyph->pixel_width,
19318 (unsigned int) glyph->u.cmp.id);
19319 if (glyph->u.cmp.automatic)
19320 fprintf (stderr,
19321 "[%d-%d]",
19322 glyph->slice.cmp.from, glyph->slice.cmp.to);
19323 fprintf (stderr, " . %4d %1.1d%1.1d\n",
19324 glyph->face_id,
19325 glyph->left_box_line_p,
19326 glyph->right_box_line_p);
19328 else if (glyph->type == XWIDGET_GLYPH)
19330 #ifndef HAVE_XWIDGETS
19331 eassume (false);
19332 #else
19333 fprintf (stderr,
19334 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
19335 glyph - row->glyphs[TEXT_AREA],
19336 'X',
19337 glyph->charpos,
19338 (BUFFERP (glyph->object)
19339 ? 'B'
19340 : (STRINGP (glyph->object)
19341 ? 'S'
19342 : '-')),
19343 glyph->pixel_width,
19344 glyph->u.xwidget,
19345 '.',
19346 glyph->face_id,
19347 glyph->left_box_line_p,
19348 glyph->right_box_line_p);
19349 #endif
19354 /* Dump the contents of glyph row at VPOS in MATRIX to stderr.
19355 GLYPHS 0 means don't show glyph contents.
19356 GLYPHS 1 means show glyphs in short form
19357 GLYPHS > 1 means show glyphs in long form. */
19359 void
19360 dump_glyph_row (struct glyph_row *row, int vpos, int glyphs)
19362 if (glyphs != 1)
19364 fprintf (stderr, "Row Start End Used oE><\\CTZFesm X Y W H V A P\n");
19365 fprintf (stderr, "==============================================================================\n");
19367 fprintf (stderr, "%3d %9"pD"d %9"pD"d %4d %1.1d%1.1d%1.1d%1.1d\
19368 %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d %4d %4d %4d %4d %4d %4d %4d\n",
19369 vpos,
19370 MATRIX_ROW_START_CHARPOS (row),
19371 MATRIX_ROW_END_CHARPOS (row),
19372 row->used[TEXT_AREA],
19373 row->contains_overlapping_glyphs_p,
19374 row->enabled_p,
19375 row->truncated_on_left_p,
19376 row->truncated_on_right_p,
19377 row->continued_p,
19378 MATRIX_ROW_CONTINUATION_LINE_P (row),
19379 MATRIX_ROW_DISPLAYS_TEXT_P (row),
19380 row->ends_at_zv_p,
19381 row->fill_line_p,
19382 row->ends_in_middle_of_char_p,
19383 row->starts_in_middle_of_char_p,
19384 row->mouse_face_p,
19385 row->x,
19386 row->y,
19387 row->pixel_width,
19388 row->height,
19389 row->visible_height,
19390 row->ascent,
19391 row->phys_ascent);
19392 /* The next 3 lines should align to "Start" in the header. */
19393 fprintf (stderr, " %9"pD"d %9"pD"d\t%5d\n", row->start.overlay_string_index,
19394 row->end.overlay_string_index,
19395 row->continuation_lines_width);
19396 fprintf (stderr, " %9"pD"d %9"pD"d\n",
19397 CHARPOS (row->start.string_pos),
19398 CHARPOS (row->end.string_pos));
19399 fprintf (stderr, " %9d %9d\n", row->start.dpvec_index,
19400 row->end.dpvec_index);
19403 if (glyphs > 1)
19405 int area;
19407 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
19409 struct glyph *glyph = row->glyphs[area];
19410 struct glyph *glyph_end = glyph + row->used[area];
19412 /* Glyph for a line end in text. */
19413 if (area == TEXT_AREA && glyph == glyph_end && glyph->charpos > 0)
19414 ++glyph_end;
19416 if (glyph < glyph_end)
19417 fprintf (stderr, " Glyph# Type Pos O W Code C Face LR\n");
19419 for (; glyph < glyph_end; ++glyph)
19420 dump_glyph (row, glyph, area);
19423 else if (glyphs == 1)
19425 int area;
19426 char s[SHRT_MAX + 4];
19428 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
19430 int i;
19432 for (i = 0; i < row->used[area]; ++i)
19434 struct glyph *glyph = row->glyphs[area] + i;
19435 if (i == row->used[area] - 1
19436 && area == TEXT_AREA
19437 && NILP (glyph->object)
19438 && glyph->type == CHAR_GLYPH
19439 && glyph->u.ch == ' ')
19441 strcpy (&s[i], "[\\n]");
19442 i += 4;
19444 else if (glyph->type == CHAR_GLYPH
19445 && glyph->u.ch < 0x80
19446 && glyph->u.ch >= ' ')
19447 s[i] = glyph->u.ch;
19448 else
19449 s[i] = '.';
19452 s[i] = '\0';
19453 fprintf (stderr, "%3d: (%d) '%s'\n", vpos, row->enabled_p, s);
19459 DEFUN ("dump-glyph-matrix", Fdump_glyph_matrix,
19460 Sdump_glyph_matrix, 0, 1, "p",
19461 doc: /* Dump the current matrix of the selected window to stderr.
19462 Shows contents of glyph row structures. With non-nil
19463 parameter GLYPHS, dump glyphs as well. If GLYPHS is 1 show
19464 glyphs in short form, otherwise show glyphs in long form.
19466 Interactively, no argument means show glyphs in short form;
19467 with numeric argument, its value is passed as the GLYPHS flag. */)
19468 (Lisp_Object glyphs)
19470 struct window *w = XWINDOW (selected_window);
19471 struct buffer *buffer = XBUFFER (w->contents);
19473 fprintf (stderr, "PT = %"pD"d, BEGV = %"pD"d. ZV = %"pD"d\n",
19474 BUF_PT (buffer), BUF_BEGV (buffer), BUF_ZV (buffer));
19475 fprintf (stderr, "Cursor x = %d, y = %d, hpos = %d, vpos = %d\n",
19476 w->cursor.x, w->cursor.y, w->cursor.hpos, w->cursor.vpos);
19477 fprintf (stderr, "=============================================\n");
19478 dump_glyph_matrix (w->current_matrix,
19479 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 0);
19480 return Qnil;
19484 DEFUN ("dump-frame-glyph-matrix", Fdump_frame_glyph_matrix,
19485 Sdump_frame_glyph_matrix, 0, 0, "", doc: /* Dump the current glyph matrix of the selected frame to stderr.
19486 Only text-mode frames have frame glyph matrices. */)
19487 (void)
19489 struct frame *f = XFRAME (selected_frame);
19491 if (f->current_matrix)
19492 dump_glyph_matrix (f->current_matrix, 1);
19493 else
19494 fprintf (stderr, "*** This frame doesn't have a frame glyph matrix ***\n");
19495 return Qnil;
19499 DEFUN ("dump-glyph-row", Fdump_glyph_row, Sdump_glyph_row, 1, 2, "P",
19500 doc: /* Dump glyph row ROW to stderr.
19501 Interactively, ROW is the prefix numeric argument and defaults to
19502 the row which displays point.
19503 Optional argument GLYPHS 0 means don't dump glyphs.
19504 GLYPHS 1 means dump glyphs in short form.
19505 GLYPHS > 1 or omitted means dump glyphs in long form. */)
19506 (Lisp_Object row, Lisp_Object glyphs)
19508 struct glyph_matrix *matrix;
19509 EMACS_INT vpos;
19511 if (NILP (row))
19513 int d1, d2, d3, d4, d5, ypos;
19514 bool visible_p = pos_visible_p (XWINDOW (selected_window), PT,
19515 &d1, &d2, &d3, &d4, &d5, &ypos);
19516 if (visible_p)
19517 vpos = ypos;
19518 else
19519 vpos = 0;
19521 else
19523 CHECK_NUMBER (row);
19524 vpos = XINT (row);
19526 matrix = XWINDOW (selected_window)->current_matrix;
19527 if (vpos >= 0 && vpos < matrix->nrows)
19528 dump_glyph_row (MATRIX_ROW (matrix, vpos),
19529 vpos,
19530 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 2);
19531 return Qnil;
19535 DEFUN ("dump-tool-bar-row", Fdump_tool_bar_row, Sdump_tool_bar_row, 1, 2, "P",
19536 doc: /* Dump glyph row ROW of the tool-bar of the current frame to stderr.
19537 Interactively, ROW is the prefix numeric argument and defaults to zero.
19538 GLYPHS 0 means don't dump glyphs.
19539 GLYPHS 1 means dump glyphs in short form.
19540 GLYPHS > 1 or omitted means dump glyphs in long form.
19542 If there's no tool-bar, or if the tool-bar is not drawn by Emacs,
19543 do nothing. */)
19544 (Lisp_Object row, Lisp_Object glyphs)
19546 #if defined (HAVE_WINDOW_SYSTEM) && ! defined (USE_GTK) && ! defined (HAVE_NS)
19547 struct frame *sf = SELECTED_FRAME ();
19548 struct glyph_matrix *m = XWINDOW (sf->tool_bar_window)->current_matrix;
19549 EMACS_INT vpos;
19551 if (NILP (row))
19552 vpos = 0;
19553 else
19555 CHECK_NUMBER (row);
19556 vpos = XINT (row);
19558 if (vpos >= 0 && vpos < m->nrows)
19559 dump_glyph_row (MATRIX_ROW (m, vpos), vpos,
19560 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 2);
19561 #endif
19562 return Qnil;
19566 DEFUN ("trace-redisplay", Ftrace_redisplay, Strace_redisplay, 0, 1, "P",
19567 doc: /* Toggle tracing of redisplay.
19568 With ARG, turn tracing on if and only if ARG is positive. */)
19569 (Lisp_Object arg)
19571 if (NILP (arg))
19572 trace_redisplay_p = !trace_redisplay_p;
19573 else
19575 arg = Fprefix_numeric_value (arg);
19576 trace_redisplay_p = XINT (arg) > 0;
19579 return Qnil;
19583 DEFUN ("trace-to-stderr", Ftrace_to_stderr, Strace_to_stderr, 1, MANY, "",
19584 doc: /* Like `format', but print result to stderr.
19585 usage: (trace-to-stderr STRING &rest OBJECTS) */)
19586 (ptrdiff_t nargs, Lisp_Object *args)
19588 Lisp_Object s = Fformat (nargs, args);
19589 fwrite (SDATA (s), 1, SBYTES (s), stderr);
19590 return Qnil;
19593 #endif /* GLYPH_DEBUG */
19597 /***********************************************************************
19598 Building Desired Matrix Rows
19599 ***********************************************************************/
19601 /* Return a temporary glyph row holding the glyphs of an overlay arrow.
19602 Used for non-window-redisplay windows, and for windows w/o left fringe. */
19604 static struct glyph_row *
19605 get_overlay_arrow_glyph_row (struct window *w, Lisp_Object overlay_arrow_string)
19607 struct frame *f = XFRAME (WINDOW_FRAME (w));
19608 struct buffer *buffer = XBUFFER (w->contents);
19609 struct buffer *old = current_buffer;
19610 const unsigned char *arrow_string = SDATA (overlay_arrow_string);
19611 ptrdiff_t arrow_len = SCHARS (overlay_arrow_string);
19612 const unsigned char *arrow_end = arrow_string + arrow_len;
19613 const unsigned char *p;
19614 struct it it;
19615 bool multibyte_p;
19616 int n_glyphs_before;
19618 set_buffer_temp (buffer);
19619 init_iterator (&it, w, -1, -1, &scratch_glyph_row, DEFAULT_FACE_ID);
19620 scratch_glyph_row.reversed_p = false;
19621 it.glyph_row->used[TEXT_AREA] = 0;
19622 SET_TEXT_POS (it.position, 0, 0);
19624 multibyte_p = !NILP (BVAR (buffer, enable_multibyte_characters));
19625 p = arrow_string;
19626 while (p < arrow_end)
19628 Lisp_Object face, ilisp;
19630 /* Get the next character. */
19631 if (multibyte_p)
19632 it.c = it.char_to_display = string_char_and_length (p, &it.len);
19633 else
19635 it.c = it.char_to_display = *p, it.len = 1;
19636 if (! ASCII_CHAR_P (it.c))
19637 it.char_to_display = BYTE8_TO_CHAR (it.c);
19639 p += it.len;
19641 /* Get its face. */
19642 ilisp = make_number (p - arrow_string);
19643 face = Fget_text_property (ilisp, Qface, overlay_arrow_string);
19644 it.face_id = compute_char_face (f, it.char_to_display, face);
19646 /* Compute its width, get its glyphs. */
19647 n_glyphs_before = it.glyph_row->used[TEXT_AREA];
19648 SET_TEXT_POS (it.position, -1, -1);
19649 PRODUCE_GLYPHS (&it);
19651 /* If this character doesn't fit any more in the line, we have
19652 to remove some glyphs. */
19653 if (it.current_x > it.last_visible_x)
19655 it.glyph_row->used[TEXT_AREA] = n_glyphs_before;
19656 break;
19660 set_buffer_temp (old);
19661 return it.glyph_row;
19665 /* Insert truncation glyphs at the start of IT->glyph_row. Which
19666 glyphs to insert is determined by produce_special_glyphs. */
19668 static void
19669 insert_left_trunc_glyphs (struct it *it)
19671 struct it truncate_it;
19672 struct glyph *from, *end, *to, *toend;
19674 eassert (!FRAME_WINDOW_P (it->f)
19675 || (!it->glyph_row->reversed_p
19676 && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0)
19677 || (it->glyph_row->reversed_p
19678 && WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0));
19680 /* Get the truncation glyphs. */
19681 truncate_it = *it;
19682 truncate_it.current_x = 0;
19683 truncate_it.face_id = DEFAULT_FACE_ID;
19684 truncate_it.glyph_row = &scratch_glyph_row;
19685 truncate_it.area = TEXT_AREA;
19686 truncate_it.glyph_row->used[TEXT_AREA] = 0;
19687 CHARPOS (truncate_it.position) = BYTEPOS (truncate_it.position) = -1;
19688 truncate_it.object = Qnil;
19689 produce_special_glyphs (&truncate_it, IT_TRUNCATION);
19691 /* Overwrite glyphs from IT with truncation glyphs. */
19692 if (!it->glyph_row->reversed_p)
19694 short tused = truncate_it.glyph_row->used[TEXT_AREA];
19696 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
19697 end = from + tused;
19698 to = it->glyph_row->glyphs[TEXT_AREA];
19699 toend = to + it->glyph_row->used[TEXT_AREA];
19700 if (FRAME_WINDOW_P (it->f))
19702 /* On GUI frames, when variable-size fonts are displayed,
19703 the truncation glyphs may need more pixels than the row's
19704 glyphs they overwrite. We overwrite more glyphs to free
19705 enough screen real estate, and enlarge the stretch glyph
19706 on the right (see display_line), if there is one, to
19707 preserve the screen position of the truncation glyphs on
19708 the right. */
19709 int w = 0;
19710 struct glyph *g = to;
19711 short used;
19713 /* The first glyph could be partially visible, in which case
19714 it->glyph_row->x will be negative. But we want the left
19715 truncation glyphs to be aligned at the left margin of the
19716 window, so we override the x coordinate at which the row
19717 will begin. */
19718 it->glyph_row->x = 0;
19719 while (g < toend && w < it->truncation_pixel_width)
19721 w += g->pixel_width;
19722 ++g;
19724 if (g - to - tused > 0)
19726 memmove (to + tused, g, (toend - g) * sizeof(*g));
19727 it->glyph_row->used[TEXT_AREA] -= g - to - tused;
19729 used = it->glyph_row->used[TEXT_AREA];
19730 if (it->glyph_row->truncated_on_right_p
19731 && WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0
19732 && it->glyph_row->glyphs[TEXT_AREA][used - 2].type
19733 == STRETCH_GLYPH)
19735 int extra = w - it->truncation_pixel_width;
19737 it->glyph_row->glyphs[TEXT_AREA][used - 2].pixel_width += extra;
19741 while (from < end)
19742 *to++ = *from++;
19744 /* There may be padding glyphs left over. Overwrite them too. */
19745 if (!FRAME_WINDOW_P (it->f))
19747 while (to < toend && CHAR_GLYPH_PADDING_P (*to))
19749 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
19750 while (from < end)
19751 *to++ = *from++;
19755 if (to > toend)
19756 it->glyph_row->used[TEXT_AREA] = to - it->glyph_row->glyphs[TEXT_AREA];
19758 else
19760 short tused = truncate_it.glyph_row->used[TEXT_AREA];
19762 /* In R2L rows, overwrite the last (rightmost) glyphs, and do
19763 that back to front. */
19764 end = truncate_it.glyph_row->glyphs[TEXT_AREA];
19765 from = end + truncate_it.glyph_row->used[TEXT_AREA] - 1;
19766 toend = it->glyph_row->glyphs[TEXT_AREA];
19767 to = toend + it->glyph_row->used[TEXT_AREA] - 1;
19768 if (FRAME_WINDOW_P (it->f))
19770 int w = 0;
19771 struct glyph *g = to;
19773 while (g >= toend && w < it->truncation_pixel_width)
19775 w += g->pixel_width;
19776 --g;
19778 if (to - g - tused > 0)
19779 to = g + tused;
19780 if (it->glyph_row->truncated_on_right_p
19781 && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0
19782 && it->glyph_row->glyphs[TEXT_AREA][1].type == STRETCH_GLYPH)
19784 int extra = w - it->truncation_pixel_width;
19786 it->glyph_row->glyphs[TEXT_AREA][1].pixel_width += extra;
19790 while (from >= end && to >= toend)
19791 *to-- = *from--;
19792 if (!FRAME_WINDOW_P (it->f))
19794 while (to >= toend && CHAR_GLYPH_PADDING_P (*to))
19796 from =
19797 truncate_it.glyph_row->glyphs[TEXT_AREA]
19798 + truncate_it.glyph_row->used[TEXT_AREA] - 1;
19799 while (from >= end && to >= toend)
19800 *to-- = *from--;
19803 if (from >= end)
19805 /* Need to free some room before prepending additional
19806 glyphs. */
19807 int move_by = from - end + 1;
19808 struct glyph *g0 = it->glyph_row->glyphs[TEXT_AREA];
19809 struct glyph *g = g0 + it->glyph_row->used[TEXT_AREA] - 1;
19811 for ( ; g >= g0; g--)
19812 g[move_by] = *g;
19813 while (from >= end)
19814 *to-- = *from--;
19815 it->glyph_row->used[TEXT_AREA] += move_by;
19820 /* Compute the hash code for ROW. */
19821 unsigned
19822 row_hash (struct glyph_row *row)
19824 int area, k;
19825 unsigned hashval = 0;
19827 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
19828 for (k = 0; k < row->used[area]; ++k)
19829 hashval = ((((hashval << 4) + (hashval >> 24)) & 0x0fffffff)
19830 + row->glyphs[area][k].u.val
19831 + row->glyphs[area][k].face_id
19832 + row->glyphs[area][k].padding_p
19833 + (row->glyphs[area][k].type << 2));
19835 return hashval;
19838 /* Compute the pixel height and width of IT->glyph_row.
19840 Most of the time, ascent and height of a display line will be equal
19841 to the max_ascent and max_height values of the display iterator
19842 structure. This is not the case if
19844 1. We hit ZV without displaying anything. In this case, max_ascent
19845 and max_height will be zero.
19847 2. We have some glyphs that don't contribute to the line height.
19848 (The glyph row flag contributes_to_line_height_p is for future
19849 pixmap extensions).
19851 The first case is easily covered by using default values because in
19852 these cases, the line height does not really matter, except that it
19853 must not be zero. */
19855 static void
19856 compute_line_metrics (struct it *it)
19858 struct glyph_row *row = it->glyph_row;
19860 if (FRAME_WINDOW_P (it->f))
19862 int i, min_y, max_y;
19864 /* The line may consist of one space only, that was added to
19865 place the cursor on it. If so, the row's height hasn't been
19866 computed yet. */
19867 if (row->height == 0)
19869 if (it->max_ascent + it->max_descent == 0)
19870 it->max_descent = it->max_phys_descent = FRAME_LINE_HEIGHT (it->f);
19871 row->ascent = it->max_ascent;
19872 row->height = it->max_ascent + it->max_descent;
19873 row->phys_ascent = it->max_phys_ascent;
19874 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
19875 row->extra_line_spacing = it->max_extra_line_spacing;
19878 /* Compute the width of this line. */
19879 row->pixel_width = row->x;
19880 for (i = 0; i < row->used[TEXT_AREA]; ++i)
19881 row->pixel_width += row->glyphs[TEXT_AREA][i].pixel_width;
19883 eassert (row->pixel_width >= 0);
19884 eassert (row->ascent >= 0 && row->height > 0);
19886 row->overlapping_p = (MATRIX_ROW_OVERLAPS_SUCC_P (row)
19887 || MATRIX_ROW_OVERLAPS_PRED_P (row));
19889 /* If first line's physical ascent is larger than its logical
19890 ascent, use the physical ascent, and make the row taller.
19891 This makes accented characters fully visible. */
19892 if (row == MATRIX_FIRST_TEXT_ROW (it->w->desired_matrix)
19893 && row->phys_ascent > row->ascent)
19895 row->height += row->phys_ascent - row->ascent;
19896 row->ascent = row->phys_ascent;
19899 /* Compute how much of the line is visible. */
19900 row->visible_height = row->height;
19902 min_y = WINDOW_HEADER_LINE_HEIGHT (it->w);
19903 max_y = WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w);
19905 if (row->y < min_y)
19906 row->visible_height -= min_y - row->y;
19907 if (row->y + row->height > max_y)
19908 row->visible_height -= row->y + row->height - max_y;
19910 else
19912 row->pixel_width = row->used[TEXT_AREA];
19913 if (row->continued_p)
19914 row->pixel_width -= it->continuation_pixel_width;
19915 else if (row->truncated_on_right_p)
19916 row->pixel_width -= it->truncation_pixel_width;
19917 row->ascent = row->phys_ascent = 0;
19918 row->height = row->phys_height = row->visible_height = 1;
19919 row->extra_line_spacing = 0;
19922 /* Compute a hash code for this row. */
19923 row->hash = row_hash (row);
19925 it->max_ascent = it->max_descent = 0;
19926 it->max_phys_ascent = it->max_phys_descent = 0;
19930 /* Append one space to the glyph row of iterator IT if doing a
19931 window-based redisplay. The space has the same face as
19932 IT->face_id. Value is true if a space was added.
19934 This function is called to make sure that there is always one glyph
19935 at the end of a glyph row that the cursor can be set on under
19936 window-systems. (If there weren't such a glyph we would not know
19937 how wide and tall a box cursor should be displayed).
19939 At the same time this space let's a nicely handle clearing to the
19940 end of the line if the row ends in italic text. */
19942 static bool
19943 append_space_for_newline (struct it *it, bool default_face_p)
19945 if (FRAME_WINDOW_P (it->f))
19947 int n = it->glyph_row->used[TEXT_AREA];
19949 if (it->glyph_row->glyphs[TEXT_AREA] + n
19950 < it->glyph_row->glyphs[1 + TEXT_AREA])
19952 /* Save some values that must not be changed.
19953 Must save IT->c and IT->len because otherwise
19954 ITERATOR_AT_END_P wouldn't work anymore after
19955 append_space_for_newline has been called. */
19956 enum display_element_type saved_what = it->what;
19957 int saved_c = it->c, saved_len = it->len;
19958 int saved_char_to_display = it->char_to_display;
19959 int saved_x = it->current_x;
19960 int saved_face_id = it->face_id;
19961 bool saved_box_end = it->end_of_box_run_p;
19962 struct text_pos saved_pos;
19963 Lisp_Object saved_object;
19964 struct face *face;
19966 saved_object = it->object;
19967 saved_pos = it->position;
19969 it->what = IT_CHARACTER;
19970 memset (&it->position, 0, sizeof it->position);
19971 it->object = Qnil;
19972 it->c = it->char_to_display = ' ';
19973 it->len = 1;
19975 /* If the default face was remapped, be sure to use the
19976 remapped face for the appended newline. */
19977 if (default_face_p)
19978 it->face_id = lookup_basic_face (it->f, DEFAULT_FACE_ID);
19979 else if (it->face_before_selective_p)
19980 it->face_id = it->saved_face_id;
19981 face = FACE_FROM_ID (it->f, it->face_id);
19982 it->face_id = FACE_FOR_CHAR (it->f, face, 0, -1, Qnil);
19983 /* In R2L rows, we will prepend a stretch glyph that will
19984 have the end_of_box_run_p flag set for it, so there's no
19985 need for the appended newline glyph to have that flag
19986 set. */
19987 if (it->glyph_row->reversed_p
19988 /* But if the appended newline glyph goes all the way to
19989 the end of the row, there will be no stretch glyph,
19990 so leave the box flag set. */
19991 && saved_x + FRAME_COLUMN_WIDTH (it->f) < it->last_visible_x)
19992 it->end_of_box_run_p = false;
19994 PRODUCE_GLYPHS (it);
19996 #ifdef HAVE_WINDOW_SYSTEM
19997 /* Make sure this space glyph has the right ascent and
19998 descent values, or else cursor at end of line will look
19999 funny, and height of empty lines will be incorrect. */
20000 struct glyph *g = it->glyph_row->glyphs[TEXT_AREA] + n;
20001 struct font *font = face->font ? face->font : FRAME_FONT (it->f);
20002 if (n == 0)
20004 Lisp_Object height, total_height;
20005 int extra_line_spacing = it->extra_line_spacing;
20006 int boff = font->baseline_offset;
20008 if (font->vertical_centering)
20009 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
20011 it->object = saved_object; /* get_it_property needs this */
20012 normal_char_ascent_descent (font, -1, &it->ascent, &it->descent);
20013 /* Must do a subset of line height processing from
20014 x_produce_glyph for newline characters. */
20015 height = get_it_property (it, Qline_height);
20016 if (CONSP (height)
20017 && CONSP (XCDR (height))
20018 && NILP (XCDR (XCDR (height))))
20020 total_height = XCAR (XCDR (height));
20021 height = XCAR (height);
20023 else
20024 total_height = Qnil;
20025 height = calc_line_height_property (it, height, font, boff, true);
20027 if (it->override_ascent >= 0)
20029 it->ascent = it->override_ascent;
20030 it->descent = it->override_descent;
20031 boff = it->override_boff;
20033 if (EQ (height, Qt))
20034 extra_line_spacing = 0;
20035 else
20037 Lisp_Object spacing;
20039 it->phys_ascent = it->ascent;
20040 it->phys_descent = it->descent;
20041 if (!NILP (height)
20042 && XINT (height) > it->ascent + it->descent)
20043 it->ascent = XINT (height) - it->descent;
20045 if (!NILP (total_height))
20046 spacing = calc_line_height_property (it, total_height, font,
20047 boff, false);
20048 else
20050 spacing = get_it_property (it, Qline_spacing);
20051 spacing = calc_line_height_property (it, spacing, font,
20052 boff, false);
20054 if (INTEGERP (spacing))
20056 extra_line_spacing = XINT (spacing);
20057 if (!NILP (total_height))
20058 extra_line_spacing -= (it->phys_ascent + it->phys_descent);
20061 if (extra_line_spacing > 0)
20063 it->descent += extra_line_spacing;
20064 if (extra_line_spacing > it->max_extra_line_spacing)
20065 it->max_extra_line_spacing = extra_line_spacing;
20067 it->max_ascent = it->ascent;
20068 it->max_descent = it->descent;
20069 /* Make sure compute_line_metrics recomputes the row height. */
20070 it->glyph_row->height = 0;
20073 g->ascent = it->max_ascent;
20074 g->descent = it->max_descent;
20075 #endif
20077 it->override_ascent = -1;
20078 it->constrain_row_ascent_descent_p = false;
20079 it->current_x = saved_x;
20080 it->object = saved_object;
20081 it->position = saved_pos;
20082 it->what = saved_what;
20083 it->face_id = saved_face_id;
20084 it->len = saved_len;
20085 it->c = saved_c;
20086 it->char_to_display = saved_char_to_display;
20087 it->end_of_box_run_p = saved_box_end;
20088 return true;
20092 return false;
20096 /* Extend the face of the last glyph in the text area of IT->glyph_row
20097 to the end of the display line. Called from display_line. If the
20098 glyph row is empty, add a space glyph to it so that we know the
20099 face to draw. Set the glyph row flag fill_line_p. If the glyph
20100 row is R2L, prepend a stretch glyph to cover the empty space to the
20101 left of the leftmost glyph. */
20103 static void
20104 extend_face_to_end_of_line (struct it *it)
20106 struct face *face, *default_face;
20107 struct frame *f = it->f;
20109 /* If line is already filled, do nothing. Non window-system frames
20110 get a grace of one more ``pixel'' because their characters are
20111 1-``pixel'' wide, so they hit the equality too early. This grace
20112 is needed only for R2L rows that are not continued, to produce
20113 one extra blank where we could display the cursor. */
20114 if ((it->current_x >= it->last_visible_x
20115 + (!FRAME_WINDOW_P (f)
20116 && it->glyph_row->reversed_p
20117 && !it->glyph_row->continued_p))
20118 /* If the window has display margins, we will need to extend
20119 their face even if the text area is filled. */
20120 && !(WINDOW_LEFT_MARGIN_WIDTH (it->w) > 0
20121 || WINDOW_RIGHT_MARGIN_WIDTH (it->w) > 0))
20122 return;
20124 /* The default face, possibly remapped. */
20125 default_face = FACE_FROM_ID_OR_NULL (f,
20126 lookup_basic_face (f, DEFAULT_FACE_ID));
20128 /* Face extension extends the background and box of IT->face_id
20129 to the end of the line. If the background equals the background
20130 of the frame, we don't have to do anything. */
20131 face = FACE_FROM_ID (f, (it->face_before_selective_p
20132 ? it->saved_face_id
20133 : it->face_id));
20135 if (FRAME_WINDOW_P (f)
20136 && MATRIX_ROW_DISPLAYS_TEXT_P (it->glyph_row)
20137 && face->box == FACE_NO_BOX
20138 && face->background == FRAME_BACKGROUND_PIXEL (f)
20139 #ifdef HAVE_WINDOW_SYSTEM
20140 && !face->stipple
20141 #endif
20142 && !it->glyph_row->reversed_p)
20143 return;
20145 /* Set the glyph row flag indicating that the face of the last glyph
20146 in the text area has to be drawn to the end of the text area. */
20147 it->glyph_row->fill_line_p = true;
20149 /* If current character of IT is not ASCII, make sure we have the
20150 ASCII face. This will be automatically undone the next time
20151 get_next_display_element returns a multibyte character. Note
20152 that the character will always be single byte in unibyte
20153 text. */
20154 if (!ASCII_CHAR_P (it->c))
20156 it->face_id = FACE_FOR_CHAR (f, face, 0, -1, Qnil);
20159 if (FRAME_WINDOW_P (f))
20161 /* If the row is empty, add a space with the current face of IT,
20162 so that we know which face to draw. */
20163 if (it->glyph_row->used[TEXT_AREA] == 0)
20165 it->glyph_row->glyphs[TEXT_AREA][0] = space_glyph;
20166 it->glyph_row->glyphs[TEXT_AREA][0].face_id = face->id;
20167 it->glyph_row->used[TEXT_AREA] = 1;
20169 /* Mode line and the header line don't have margins, and
20170 likewise the frame's tool-bar window, if there is any. */
20171 if (!(it->glyph_row->mode_line_p
20172 #if defined (HAVE_WINDOW_SYSTEM) && ! defined (USE_GTK) && ! defined (HAVE_NS)
20173 || (WINDOWP (f->tool_bar_window)
20174 && it->w == XWINDOW (f->tool_bar_window))
20175 #endif
20178 if (WINDOW_LEFT_MARGIN_WIDTH (it->w) > 0
20179 && it->glyph_row->used[LEFT_MARGIN_AREA] == 0)
20181 it->glyph_row->glyphs[LEFT_MARGIN_AREA][0] = space_glyph;
20182 it->glyph_row->glyphs[LEFT_MARGIN_AREA][0].face_id =
20183 default_face->id;
20184 it->glyph_row->used[LEFT_MARGIN_AREA] = 1;
20186 if (WINDOW_RIGHT_MARGIN_WIDTH (it->w) > 0
20187 && it->glyph_row->used[RIGHT_MARGIN_AREA] == 0)
20189 it->glyph_row->glyphs[RIGHT_MARGIN_AREA][0] = space_glyph;
20190 it->glyph_row->glyphs[RIGHT_MARGIN_AREA][0].face_id =
20191 default_face->id;
20192 it->glyph_row->used[RIGHT_MARGIN_AREA] = 1;
20195 #ifdef HAVE_WINDOW_SYSTEM
20196 if (it->glyph_row->reversed_p)
20198 /* Prepend a stretch glyph to the row, such that the
20199 rightmost glyph will be drawn flushed all the way to the
20200 right margin of the window. The stretch glyph that will
20201 occupy the empty space, if any, to the left of the
20202 glyphs. */
20203 struct font *font = face->font ? face->font : FRAME_FONT (f);
20204 struct glyph *row_start = it->glyph_row->glyphs[TEXT_AREA];
20205 struct glyph *row_end = row_start + it->glyph_row->used[TEXT_AREA];
20206 struct glyph *g;
20207 int row_width, stretch_ascent, stretch_width;
20208 struct text_pos saved_pos;
20209 int saved_face_id;
20210 bool saved_avoid_cursor, saved_box_start;
20212 for (row_width = 0, g = row_start; g < row_end; g++)
20213 row_width += g->pixel_width;
20215 /* FIXME: There are various minor display glitches in R2L
20216 rows when only one of the fringes is missing. The
20217 strange condition below produces the least bad effect. */
20218 if ((WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0)
20219 == (WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0)
20220 || WINDOW_RIGHT_FRINGE_WIDTH (it->w) != 0)
20221 stretch_width = window_box_width (it->w, TEXT_AREA);
20222 else
20223 stretch_width = it->last_visible_x - it->first_visible_x;
20224 stretch_width -= row_width;
20226 if (stretch_width > 0)
20228 stretch_ascent =
20229 (((it->ascent + it->descent)
20230 * FONT_BASE (font)) / FONT_HEIGHT (font));
20231 saved_pos = it->position;
20232 memset (&it->position, 0, sizeof it->position);
20233 saved_avoid_cursor = it->avoid_cursor_p;
20234 it->avoid_cursor_p = true;
20235 saved_face_id = it->face_id;
20236 saved_box_start = it->start_of_box_run_p;
20237 /* The last row's stretch glyph should get the default
20238 face, to avoid painting the rest of the window with
20239 the region face, if the region ends at ZV. */
20240 if (it->glyph_row->ends_at_zv_p)
20241 it->face_id = default_face->id;
20242 else
20243 it->face_id = face->id;
20244 it->start_of_box_run_p = false;
20245 append_stretch_glyph (it, Qnil, stretch_width,
20246 it->ascent + it->descent, stretch_ascent);
20247 it->position = saved_pos;
20248 it->avoid_cursor_p = saved_avoid_cursor;
20249 it->face_id = saved_face_id;
20250 it->start_of_box_run_p = saved_box_start;
20252 /* If stretch_width comes out negative, it means that the
20253 last glyph is only partially visible. In R2L rows, we
20254 want the leftmost glyph to be partially visible, so we
20255 need to give the row the corresponding left offset. */
20256 if (stretch_width < 0)
20257 it->glyph_row->x = stretch_width;
20259 #endif /* HAVE_WINDOW_SYSTEM */
20261 else
20263 /* Save some values that must not be changed. */
20264 int saved_x = it->current_x;
20265 struct text_pos saved_pos;
20266 Lisp_Object saved_object;
20267 enum display_element_type saved_what = it->what;
20268 int saved_face_id = it->face_id;
20270 saved_object = it->object;
20271 saved_pos = it->position;
20273 it->what = IT_CHARACTER;
20274 memset (&it->position, 0, sizeof it->position);
20275 it->object = Qnil;
20276 it->c = it->char_to_display = ' ';
20277 it->len = 1;
20279 if (WINDOW_LEFT_MARGIN_WIDTH (it->w) > 0
20280 && (it->glyph_row->used[LEFT_MARGIN_AREA]
20281 < WINDOW_LEFT_MARGIN_WIDTH (it->w))
20282 && !it->glyph_row->mode_line_p
20283 && default_face->background != FRAME_BACKGROUND_PIXEL (f))
20285 struct glyph *g = it->glyph_row->glyphs[LEFT_MARGIN_AREA];
20286 struct glyph *e = g + it->glyph_row->used[LEFT_MARGIN_AREA];
20288 for (it->current_x = 0; g < e; g++)
20289 it->current_x += g->pixel_width;
20291 it->area = LEFT_MARGIN_AREA;
20292 it->face_id = default_face->id;
20293 while (it->glyph_row->used[LEFT_MARGIN_AREA]
20294 < WINDOW_LEFT_MARGIN_WIDTH (it->w)
20295 && g < it->glyph_row->glyphs[TEXT_AREA])
20297 PRODUCE_GLYPHS (it);
20298 /* term.c:produce_glyphs advances it->current_x only for
20299 TEXT_AREA. */
20300 it->current_x += it->pixel_width;
20301 g++;
20304 it->current_x = saved_x;
20305 it->area = TEXT_AREA;
20308 /* The last row's blank glyphs should get the default face, to
20309 avoid painting the rest of the window with the region face,
20310 if the region ends at ZV. */
20311 if (it->glyph_row->ends_at_zv_p)
20312 it->face_id = default_face->id;
20313 else
20314 it->face_id = face->id;
20315 PRODUCE_GLYPHS (it);
20317 while (it->current_x <= it->last_visible_x)
20318 PRODUCE_GLYPHS (it);
20320 if (WINDOW_RIGHT_MARGIN_WIDTH (it->w) > 0
20321 && (it->glyph_row->used[RIGHT_MARGIN_AREA]
20322 < WINDOW_RIGHT_MARGIN_WIDTH (it->w))
20323 && !it->glyph_row->mode_line_p
20324 && default_face->background != FRAME_BACKGROUND_PIXEL (f))
20326 struct glyph *g = it->glyph_row->glyphs[RIGHT_MARGIN_AREA];
20327 struct glyph *e = g + it->glyph_row->used[RIGHT_MARGIN_AREA];
20329 for ( ; g < e; g++)
20330 it->current_x += g->pixel_width;
20332 it->area = RIGHT_MARGIN_AREA;
20333 it->face_id = default_face->id;
20334 while (it->glyph_row->used[RIGHT_MARGIN_AREA]
20335 < WINDOW_RIGHT_MARGIN_WIDTH (it->w)
20336 && g < it->glyph_row->glyphs[LAST_AREA])
20338 PRODUCE_GLYPHS (it);
20339 it->current_x += it->pixel_width;
20340 g++;
20343 it->area = TEXT_AREA;
20346 /* Don't count these blanks really. It would let us insert a left
20347 truncation glyph below and make us set the cursor on them, maybe. */
20348 it->current_x = saved_x;
20349 it->object = saved_object;
20350 it->position = saved_pos;
20351 it->what = saved_what;
20352 it->face_id = saved_face_id;
20357 /* Value is true if text starting at CHARPOS in current_buffer is
20358 trailing whitespace. */
20360 static bool
20361 trailing_whitespace_p (ptrdiff_t charpos)
20363 ptrdiff_t bytepos = CHAR_TO_BYTE (charpos);
20364 int c = 0;
20366 while (bytepos < ZV_BYTE
20367 && (c = FETCH_CHAR (bytepos),
20368 c == ' ' || c == '\t'))
20369 ++bytepos;
20371 if (bytepos >= ZV_BYTE || c == '\n' || c == '\r')
20373 if (bytepos != PT_BYTE)
20374 return true;
20376 return false;
20380 /* Highlight trailing whitespace, if any, in ROW. */
20382 static void
20383 highlight_trailing_whitespace (struct frame *f, struct glyph_row *row)
20385 int used = row->used[TEXT_AREA];
20387 if (used)
20389 struct glyph *start = row->glyphs[TEXT_AREA];
20390 struct glyph *glyph = start + used - 1;
20392 if (row->reversed_p)
20394 /* Right-to-left rows need to be processed in the opposite
20395 direction, so swap the edge pointers. */
20396 glyph = start;
20397 start = row->glyphs[TEXT_AREA] + used - 1;
20400 /* Skip over glyphs inserted to display the cursor at the
20401 end of a line, for extending the face of the last glyph
20402 to the end of the line on terminals, and for truncation
20403 and continuation glyphs. */
20404 if (!row->reversed_p)
20406 while (glyph >= start
20407 && glyph->type == CHAR_GLYPH
20408 && NILP (glyph->object))
20409 --glyph;
20411 else
20413 while (glyph <= start
20414 && glyph->type == CHAR_GLYPH
20415 && NILP (glyph->object))
20416 ++glyph;
20419 /* If last glyph is a space or stretch, and it's trailing
20420 whitespace, set the face of all trailing whitespace glyphs in
20421 IT->glyph_row to `trailing-whitespace'. */
20422 if ((row->reversed_p ? glyph <= start : glyph >= start)
20423 && BUFFERP (glyph->object)
20424 && (glyph->type == STRETCH_GLYPH
20425 || (glyph->type == CHAR_GLYPH
20426 && glyph->u.ch == ' '))
20427 && trailing_whitespace_p (glyph->charpos))
20429 int face_id = lookup_named_face (f, Qtrailing_whitespace, false);
20430 if (face_id < 0)
20431 return;
20433 if (!row->reversed_p)
20435 while (glyph >= start
20436 && BUFFERP (glyph->object)
20437 && (glyph->type == STRETCH_GLYPH
20438 || (glyph->type == CHAR_GLYPH
20439 && glyph->u.ch == ' ')))
20440 (glyph--)->face_id = face_id;
20442 else
20444 while (glyph <= start
20445 && BUFFERP (glyph->object)
20446 && (glyph->type == STRETCH_GLYPH
20447 || (glyph->type == CHAR_GLYPH
20448 && glyph->u.ch == ' ')))
20449 (glyph++)->face_id = face_id;
20456 /* Value is true if glyph row ROW should be
20457 considered to hold the buffer position CHARPOS. */
20459 static bool
20460 row_for_charpos_p (struct glyph_row *row, ptrdiff_t charpos)
20462 bool result = true;
20464 if (charpos == CHARPOS (row->end.pos)
20465 || charpos == MATRIX_ROW_END_CHARPOS (row))
20467 /* Suppose the row ends on a string.
20468 Unless the row is continued, that means it ends on a newline
20469 in the string. If it's anything other than a display string
20470 (e.g., a before-string from an overlay), we don't want the
20471 cursor there. (This heuristic seems to give the optimal
20472 behavior for the various types of multi-line strings.)
20473 One exception: if the string has `cursor' property on one of
20474 its characters, we _do_ want the cursor there. */
20475 if (CHARPOS (row->end.string_pos) >= 0)
20477 if (row->continued_p)
20478 result = true;
20479 else
20481 /* Check for `display' property. */
20482 struct glyph *beg = row->glyphs[TEXT_AREA];
20483 struct glyph *end = beg + row->used[TEXT_AREA] - 1;
20484 struct glyph *glyph;
20486 result = false;
20487 for (glyph = end; glyph >= beg; --glyph)
20488 if (STRINGP (glyph->object))
20490 Lisp_Object prop
20491 = Fget_char_property (make_number (charpos),
20492 Qdisplay, Qnil);
20493 result =
20494 (!NILP (prop)
20495 && display_prop_string_p (prop, glyph->object));
20496 /* If there's a `cursor' property on one of the
20497 string's characters, this row is a cursor row,
20498 even though this is not a display string. */
20499 if (!result)
20501 Lisp_Object s = glyph->object;
20503 for ( ; glyph >= beg && EQ (glyph->object, s); --glyph)
20505 ptrdiff_t gpos = glyph->charpos;
20507 if (!NILP (Fget_char_property (make_number (gpos),
20508 Qcursor, s)))
20510 result = true;
20511 break;
20515 break;
20519 else if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
20521 /* If the row ends in middle of a real character,
20522 and the line is continued, we want the cursor here.
20523 That's because CHARPOS (ROW->end.pos) would equal
20524 PT if PT is before the character. */
20525 if (!row->ends_in_ellipsis_p)
20526 result = row->continued_p;
20527 else
20528 /* If the row ends in an ellipsis, then
20529 CHARPOS (ROW->end.pos) will equal point after the
20530 invisible text. We want that position to be displayed
20531 after the ellipsis. */
20532 result = false;
20534 /* If the row ends at ZV, display the cursor at the end of that
20535 row instead of at the start of the row below. */
20536 else
20537 result = row->ends_at_zv_p;
20540 return result;
20543 /* Value is true if glyph row ROW should be
20544 used to hold the cursor. */
20546 static bool
20547 cursor_row_p (struct glyph_row *row)
20549 return row_for_charpos_p (row, PT);
20554 /* Push the property PROP so that it will be rendered at the current
20555 position in IT. Return true if PROP was successfully pushed, false
20556 otherwise. Called from handle_line_prefix to handle the
20557 `line-prefix' and `wrap-prefix' properties. */
20559 static bool
20560 push_prefix_prop (struct it *it, Lisp_Object prop)
20562 struct text_pos pos =
20563 STRINGP (it->string) ? it->current.string_pos : it->current.pos;
20565 eassert (it->method == GET_FROM_BUFFER
20566 || it->method == GET_FROM_DISPLAY_VECTOR
20567 || it->method == GET_FROM_STRING
20568 || it->method == GET_FROM_IMAGE);
20570 /* We need to save the current buffer/string position, so it will be
20571 restored by pop_it, because iterate_out_of_display_property
20572 depends on that being set correctly, but some situations leave
20573 it->position not yet set when this function is called. */
20574 push_it (it, &pos);
20576 if (STRINGP (prop))
20578 if (SCHARS (prop) == 0)
20580 pop_it (it);
20581 return false;
20584 it->string = prop;
20585 it->string_from_prefix_prop_p = true;
20586 it->multibyte_p = STRING_MULTIBYTE (it->string);
20587 it->current.overlay_string_index = -1;
20588 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
20589 it->end_charpos = it->string_nchars = SCHARS (it->string);
20590 it->method = GET_FROM_STRING;
20591 it->stop_charpos = 0;
20592 it->prev_stop = 0;
20593 it->base_level_stop = 0;
20594 it->cmp_it.id = -1;
20596 /* Force paragraph direction to be that of the parent
20597 buffer/string. */
20598 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
20599 it->paragraph_embedding = it->bidi_it.paragraph_dir;
20600 else
20601 it->paragraph_embedding = L2R;
20603 /* Set up the bidi iterator for this display string. */
20604 if (it->bidi_p)
20606 it->bidi_it.string.lstring = it->string;
20607 it->bidi_it.string.s = NULL;
20608 it->bidi_it.string.schars = it->end_charpos;
20609 it->bidi_it.string.bufpos = IT_CHARPOS (*it);
20610 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
20611 it->bidi_it.string.unibyte = !it->multibyte_p;
20612 it->bidi_it.w = it->w;
20613 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
20616 else if (CONSP (prop) && EQ (XCAR (prop), Qspace))
20618 it->method = GET_FROM_STRETCH;
20619 it->object = prop;
20621 #ifdef HAVE_WINDOW_SYSTEM
20622 else if (IMAGEP (prop))
20624 it->what = IT_IMAGE;
20625 it->image_id = lookup_image (it->f, prop);
20626 it->method = GET_FROM_IMAGE;
20628 #endif /* HAVE_WINDOW_SYSTEM */
20629 else
20631 pop_it (it); /* bogus display property, give up */
20632 return false;
20635 return true;
20638 /* Return the character-property PROP at the current position in IT. */
20640 static Lisp_Object
20641 get_it_property (struct it *it, Lisp_Object prop)
20643 Lisp_Object position, object = it->object;
20645 if (STRINGP (object))
20646 position = make_number (IT_STRING_CHARPOS (*it));
20647 else if (BUFFERP (object))
20649 position = make_number (IT_CHARPOS (*it));
20650 object = it->window;
20652 else
20653 return Qnil;
20655 return Fget_char_property (position, prop, object);
20658 /* See if there's a line- or wrap-prefix, and if so, push it on IT. */
20660 static void
20661 handle_line_prefix (struct it *it)
20663 Lisp_Object prefix;
20665 if (it->continuation_lines_width > 0)
20667 prefix = get_it_property (it, Qwrap_prefix);
20668 if (NILP (prefix))
20669 prefix = Vwrap_prefix;
20671 else
20673 prefix = get_it_property (it, Qline_prefix);
20674 if (NILP (prefix))
20675 prefix = Vline_prefix;
20677 if (! NILP (prefix) && push_prefix_prop (it, prefix))
20679 /* If the prefix is wider than the window, and we try to wrap
20680 it, it would acquire its own wrap prefix, and so on till the
20681 iterator stack overflows. So, don't wrap the prefix. */
20682 it->line_wrap = TRUNCATE;
20683 it->avoid_cursor_p = true;
20689 /* Remove N glyphs at the start of a reversed IT->glyph_row. Called
20690 only for R2L lines from display_line and display_string, when they
20691 decide that too many glyphs were produced by PRODUCE_GLYPHS, and
20692 the line/string needs to be continued on the next glyph row. */
20693 static void
20694 unproduce_glyphs (struct it *it, int n)
20696 struct glyph *glyph, *end;
20698 eassert (it->glyph_row);
20699 eassert (it->glyph_row->reversed_p);
20700 eassert (it->area == TEXT_AREA);
20701 eassert (n <= it->glyph_row->used[TEXT_AREA]);
20703 if (n > it->glyph_row->used[TEXT_AREA])
20704 n = it->glyph_row->used[TEXT_AREA];
20705 glyph = it->glyph_row->glyphs[TEXT_AREA] + n;
20706 end = it->glyph_row->glyphs[TEXT_AREA] + it->glyph_row->used[TEXT_AREA];
20707 for ( ; glyph < end; glyph++)
20708 glyph[-n] = *glyph;
20711 /* Find the positions in a bidi-reordered ROW to serve as ROW->minpos
20712 and ROW->maxpos. */
20713 static void
20714 find_row_edges (struct it *it, struct glyph_row *row,
20715 ptrdiff_t min_pos, ptrdiff_t min_bpos,
20716 ptrdiff_t max_pos, ptrdiff_t max_bpos)
20718 /* FIXME: Revisit this when glyph ``spilling'' in continuation
20719 lines' rows is implemented for bidi-reordered rows. */
20721 /* ROW->minpos is the value of min_pos, the minimal buffer position
20722 we have in ROW, or ROW->start.pos if that is smaller. */
20723 if (min_pos <= ZV && min_pos < row->start.pos.charpos)
20724 SET_TEXT_POS (row->minpos, min_pos, min_bpos);
20725 else
20726 /* We didn't find buffer positions smaller than ROW->start, or
20727 didn't find _any_ valid buffer positions in any of the glyphs,
20728 so we must trust the iterator's computed positions. */
20729 row->minpos = row->start.pos;
20730 if (max_pos <= 0)
20732 max_pos = CHARPOS (it->current.pos);
20733 max_bpos = BYTEPOS (it->current.pos);
20736 /* Here are the various use-cases for ending the row, and the
20737 corresponding values for ROW->maxpos:
20739 Line ends in a newline from buffer eol_pos + 1
20740 Line is continued from buffer max_pos + 1
20741 Line is truncated on right it->current.pos
20742 Line ends in a newline from string max_pos + 1(*)
20743 (*) + 1 only when line ends in a forward scan
20744 Line is continued from string max_pos
20745 Line is continued from display vector max_pos
20746 Line is entirely from a string min_pos == max_pos
20747 Line is entirely from a display vector min_pos == max_pos
20748 Line that ends at ZV ZV
20750 If you discover other use-cases, please add them here as
20751 appropriate. */
20752 if (row->ends_at_zv_p)
20753 row->maxpos = it->current.pos;
20754 else if (row->used[TEXT_AREA])
20756 bool seen_this_string = false;
20757 struct glyph_row *r1 = row - 1;
20759 /* Did we see the same display string on the previous row? */
20760 if (STRINGP (it->object)
20761 /* this is not the first row */
20762 && row > it->w->desired_matrix->rows
20763 /* previous row is not the header line */
20764 && !r1->mode_line_p
20765 /* previous row also ends in a newline from a string */
20766 && r1->ends_in_newline_from_string_p)
20768 struct glyph *start, *end;
20770 /* Search for the last glyph of the previous row that came
20771 from buffer or string. Depending on whether the row is
20772 L2R or R2L, we need to process it front to back or the
20773 other way round. */
20774 if (!r1->reversed_p)
20776 start = r1->glyphs[TEXT_AREA];
20777 end = start + r1->used[TEXT_AREA];
20778 /* Glyphs inserted by redisplay have nil as their object. */
20779 while (end > start
20780 && NILP ((end - 1)->object)
20781 && (end - 1)->charpos <= 0)
20782 --end;
20783 if (end > start)
20785 if (EQ ((end - 1)->object, it->object))
20786 seen_this_string = true;
20788 else
20789 /* If all the glyphs of the previous row were inserted
20790 by redisplay, it means the previous row was
20791 produced from a single newline, which is only
20792 possible if that newline came from the same string
20793 as the one which produced this ROW. */
20794 seen_this_string = true;
20796 else
20798 end = r1->glyphs[TEXT_AREA] - 1;
20799 start = end + r1->used[TEXT_AREA];
20800 while (end < start
20801 && NILP ((end + 1)->object)
20802 && (end + 1)->charpos <= 0)
20803 ++end;
20804 if (end < start)
20806 if (EQ ((end + 1)->object, it->object))
20807 seen_this_string = true;
20809 else
20810 seen_this_string = true;
20813 /* Take note of each display string that covers a newline only
20814 once, the first time we see it. This is for when a display
20815 string includes more than one newline in it. */
20816 if (row->ends_in_newline_from_string_p && !seen_this_string)
20818 /* If we were scanning the buffer forward when we displayed
20819 the string, we want to account for at least one buffer
20820 position that belongs to this row (position covered by
20821 the display string), so that cursor positioning will
20822 consider this row as a candidate when point is at the end
20823 of the visual line represented by this row. This is not
20824 required when scanning back, because max_pos will already
20825 have a much larger value. */
20826 if (CHARPOS (row->end.pos) > max_pos)
20827 INC_BOTH (max_pos, max_bpos);
20828 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
20830 else if (CHARPOS (it->eol_pos) > 0)
20831 SET_TEXT_POS (row->maxpos,
20832 CHARPOS (it->eol_pos) + 1, BYTEPOS (it->eol_pos) + 1);
20833 else if (row->continued_p)
20835 /* If max_pos is different from IT's current position, it
20836 means IT->method does not belong to the display element
20837 at max_pos. However, it also means that the display
20838 element at max_pos was displayed in its entirety on this
20839 line, which is equivalent to saying that the next line
20840 starts at the next buffer position. */
20841 if (IT_CHARPOS (*it) == max_pos && it->method != GET_FROM_BUFFER)
20842 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
20843 else
20845 INC_BOTH (max_pos, max_bpos);
20846 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
20849 else if (row->truncated_on_right_p)
20850 /* display_line already called reseat_at_next_visible_line_start,
20851 which puts the iterator at the beginning of the next line, in
20852 the logical order. */
20853 row->maxpos = it->current.pos;
20854 else if (max_pos == min_pos && it->method != GET_FROM_BUFFER)
20855 /* A line that is entirely from a string/image/stretch... */
20856 row->maxpos = row->minpos;
20857 else
20858 emacs_abort ();
20860 else
20861 row->maxpos = it->current.pos;
20864 /* Like display_count_lines, but capable of counting outside of the
20865 current narrowed region. */
20866 static ptrdiff_t
20867 display_count_lines_logically (ptrdiff_t start_byte, ptrdiff_t limit_byte,
20868 ptrdiff_t count, ptrdiff_t *byte_pos_ptr)
20870 if (!display_line_numbers_widen || (BEGV == BEG && ZV == Z))
20871 return display_count_lines (start_byte, limit_byte, count, byte_pos_ptr);
20873 ptrdiff_t val;
20874 ptrdiff_t pdl_count = SPECPDL_INDEX ();
20875 record_unwind_protect (save_restriction_restore, save_restriction_save ());
20876 Fwiden ();
20877 val = display_count_lines (start_byte, limit_byte, count, byte_pos_ptr);
20878 unbind_to (pdl_count, Qnil);
20879 return val;
20882 /* Count the number of screen lines in window IT->w between character
20883 position IT_CHARPOS(*IT) and the line showing that window's point. */
20884 static ptrdiff_t
20885 display_count_lines_visually (struct it *it)
20887 struct it tem_it;
20888 ptrdiff_t to;
20889 struct text_pos from;
20891 /* If we already calculated a relative line number, use that. This
20892 trick relies on the fact that visual lines (a.k.a. "glyph rows")
20893 are laid out sequentially, one by one, for each sequence of calls
20894 to display_line or other similar function that follows a call to
20895 init_iterator. */
20896 if (it->lnum_bytepos > 0)
20897 return it->lnum + 1;
20898 else
20900 ptrdiff_t count = SPECPDL_INDEX ();
20902 if (IT_CHARPOS (*it) <= PT)
20904 from = it->current.pos;
20905 to = PT;
20907 else
20909 SET_TEXT_POS (from, PT, PT_BYTE);
20910 to = IT_CHARPOS (*it);
20912 start_display (&tem_it, it->w, from);
20913 /* Need to disable visual mode temporarily, since otherwise the
20914 call to move_it_to will cause infinite recursion. */
20915 specbind (Qdisplay_line_numbers, Qrelative);
20916 /* Some redisplay optimizations could invoke us very far from
20917 PT, which will make the caller painfully slow. There should
20918 be no need to go too far beyond the window's bottom, as any
20919 such optimization will fail to show point anyway. */
20920 move_it_to (&tem_it, to, -1,
20921 tem_it.last_visible_y
20922 + (SCROLL_LIMIT + 10) * FRAME_LINE_HEIGHT (tem_it.f),
20923 -1, MOVE_TO_POS | MOVE_TO_Y);
20924 unbind_to (count, Qnil);
20925 return IT_CHARPOS (*it) <= PT ? -tem_it.vpos : tem_it.vpos;
20929 /* Produce the line-number glyphs for the current glyph_row. If
20930 IT->glyph_row is non-NULL, populate the row with the produced
20931 glyphs. */
20932 static void
20933 maybe_produce_line_number (struct it *it)
20935 ptrdiff_t last_line = it->lnum;
20936 ptrdiff_t start_from, bytepos;
20937 ptrdiff_t this_line;
20938 bool first_time = false;
20939 ptrdiff_t beg_byte = display_line_numbers_widen ? BEG_BYTE : BEGV_BYTE;
20940 ptrdiff_t z_byte = display_line_numbers_widen ? Z_BYTE : ZV_BYTE;
20941 void *itdata = bidi_shelve_cache ();
20943 if (EQ (Vdisplay_line_numbers, Qvisual))
20944 this_line = display_count_lines_visually (it);
20945 else
20947 if (!last_line)
20949 /* If possible, reuse data cached by line-number-mode. */
20950 if (it->w->base_line_number > 0
20951 && it->w->base_line_pos > 0
20952 && it->w->base_line_pos <= IT_CHARPOS (*it)
20953 /* line-number-mode always displays narrowed line
20954 numbers, so we cannot use its data if the user wants
20955 line numbers that disregard narrowing, or if the
20956 buffer's narrowing has just changed. */
20957 && !(display_line_numbers_widen
20958 && (BEG_BYTE != BEGV_BYTE || Z_BYTE != ZV_BYTE))
20959 && !current_buffer->clip_changed)
20961 start_from = CHAR_TO_BYTE (it->w->base_line_pos);
20962 last_line = it->w->base_line_number - 1;
20964 else
20965 start_from = beg_byte;
20966 if (!it->lnum_bytepos)
20967 first_time = true;
20969 else
20970 start_from = it->lnum_bytepos;
20972 /* Paranoia: what if someone changes the narrowing since the
20973 last time display_line was called? Shouldn't really happen,
20974 but who knows what some crazy Lisp invoked by :eval could do? */
20975 if (!(beg_byte <= start_from && start_from <= z_byte))
20977 last_line = 0;
20978 start_from = beg_byte;
20981 this_line =
20982 last_line + display_count_lines_logically (start_from,
20983 IT_BYTEPOS (*it),
20984 IT_CHARPOS (*it), &bytepos);
20985 eassert (this_line > 0 || (this_line == 0 && start_from == beg_byte));
20986 eassert (bytepos == IT_BYTEPOS (*it));
20989 /* Record the line number information. */
20990 if (this_line != last_line || !it->lnum_bytepos)
20992 it->lnum = this_line;
20993 it->lnum_bytepos = IT_BYTEPOS (*it);
20996 /* Produce the glyphs for the line number. */
20997 struct it tem_it;
20998 char lnum_buf[INT_STRLEN_BOUND (ptrdiff_t) + 1];
20999 bool beyond_zv = IT_BYTEPOS (*it) >= ZV_BYTE ? true : false;
21000 ptrdiff_t lnum_offset = -1; /* to produce 1-based line numbers */
21001 int lnum_face_id = merge_faces (it->f, Qline_number, 0, DEFAULT_FACE_ID);
21002 int current_lnum_face_id
21003 = merge_faces (it->f, Qline_number_current_line, 0, DEFAULT_FACE_ID);
21004 /* Compute point's line number if needed. */
21005 if ((EQ (Vdisplay_line_numbers, Qrelative)
21006 || EQ (Vdisplay_line_numbers, Qvisual)
21007 || lnum_face_id != current_lnum_face_id)
21008 && !it->pt_lnum)
21010 ptrdiff_t ignored;
21011 if (PT_BYTE > it->lnum_bytepos && !EQ (Vdisplay_line_numbers, Qvisual))
21012 it->pt_lnum =
21013 this_line + display_count_lines_logically (it->lnum_bytepos, PT_BYTE,
21014 PT, &ignored);
21015 else
21016 it->pt_lnum = display_count_lines_logically (beg_byte, PT_BYTE, PT,
21017 &ignored);
21019 /* Compute the required width if needed. */
21020 if (!it->lnum_width)
21022 if (NATNUMP (Vdisplay_line_numbers_width))
21023 it->lnum_width = XFASTINT (Vdisplay_line_numbers_width);
21025 /* Max line number to be displayed cannot be more than the one
21026 corresponding to the last row of the desired matrix. */
21027 ptrdiff_t max_lnum;
21029 if (NILP (Vdisplay_line_numbers_current_absolute)
21030 && (EQ (Vdisplay_line_numbers, Qrelative)
21031 || EQ (Vdisplay_line_numbers, Qvisual)))
21032 /* We subtract one more because the current line is always
21033 zero in this mode. */
21034 max_lnum = it->w->desired_matrix->nrows - 2;
21035 else if (EQ (Vdisplay_line_numbers, Qvisual))
21036 max_lnum = it->pt_lnum + it->w->desired_matrix->nrows - 1;
21037 else
21038 max_lnum = this_line + it->w->desired_matrix->nrows - 1 - it->vpos;
21039 max_lnum = max (1, max_lnum);
21040 it->lnum_width = max (it->lnum_width, log10 (max_lnum) + 1);
21041 eassert (it->lnum_width > 0);
21043 if (EQ (Vdisplay_line_numbers, Qrelative))
21044 lnum_offset = it->pt_lnum;
21045 else if (EQ (Vdisplay_line_numbers, Qvisual))
21046 lnum_offset = 0;
21048 /* Under 'relative', display the absolute line number for the
21049 current line, unless the user requests otherwise. */
21050 ptrdiff_t lnum_to_display = eabs (this_line - lnum_offset);
21051 if ((EQ (Vdisplay_line_numbers, Qrelative)
21052 || EQ (Vdisplay_line_numbers, Qvisual))
21053 && lnum_to_display == 0
21054 && !NILP (Vdisplay_line_numbers_current_absolute))
21055 lnum_to_display = it->pt_lnum + 1;
21056 /* In L2R rows we need to append the blank separator, in R2L
21057 rows we need to prepend it. But this function is usually
21058 called when no display elements were produced from the
21059 following line, so the paragraph direction might be unknown.
21060 Therefore we cheat and add 2 blanks, one on either side. */
21061 pint2str (lnum_buf, it->lnum_width + 1, lnum_to_display);
21062 strcat (lnum_buf, " ");
21064 /* Setup for producing the glyphs. */
21065 init_iterator (&tem_it, it->w, -1, -1, &scratch_glyph_row,
21066 /* FIXME: Use specialized face. */
21067 DEFAULT_FACE_ID);
21068 scratch_glyph_row.reversed_p = false;
21069 scratch_glyph_row.used[TEXT_AREA] = 0;
21070 SET_TEXT_POS (tem_it.position, 0, 0);
21071 tem_it.avoid_cursor_p = true;
21072 tem_it.bidi_p = true;
21073 tem_it.bidi_it.type = WEAK_EN;
21074 /* According to UAX#9, EN goes up 2 levels in L2R paragraph and
21075 1 level in R2L paragraphs. Emulate that, assuming we are in
21076 an L2R paragraph. */
21077 tem_it.bidi_it.resolved_level = 2;
21079 /* Produce glyphs for the line number in a scratch glyph_row. */
21080 int n_glyphs_before;
21081 for (const char *p = lnum_buf; *p; p++)
21083 /* For continuation lines and lines after ZV, instead of a line
21084 number, produce a blank prefix of the same width. Use the
21085 default face for the blank field beyond ZV. */
21086 if (beyond_zv)
21087 tem_it.face_id = it->base_face_id;
21088 else if (lnum_face_id != current_lnum_face_id
21089 && (EQ (Vdisplay_line_numbers, Qvisual)
21090 ? this_line == 0
21091 : this_line == it->pt_lnum))
21092 tem_it.face_id = current_lnum_face_id;
21093 else
21094 tem_it.face_id = lnum_face_id;
21095 if (beyond_zv
21096 /* Don't display the same line number more than once. */
21097 || (!EQ (Vdisplay_line_numbers, Qvisual)
21098 && (it->continuation_lines_width > 0
21099 || (this_line == last_line && !first_time))))
21100 tem_it.c = tem_it.char_to_display = ' ';
21101 else
21102 tem_it.c = tem_it.char_to_display = *p;
21103 tem_it.len = 1;
21104 n_glyphs_before = scratch_glyph_row.used[TEXT_AREA];
21105 /* Make sure these glyphs will have a "position" of -1. */
21106 SET_TEXT_POS (tem_it.position, -1, -1);
21107 PRODUCE_GLYPHS (&tem_it);
21109 /* Stop producing glyphs if we don't have enough space on
21110 this line. FIXME: should we refrain from producing the
21111 line number at all in that case? */
21112 if (tem_it.current_x > tem_it.last_visible_x)
21114 scratch_glyph_row.used[TEXT_AREA] = n_glyphs_before;
21115 break;
21119 /* Record the width in pixels we need for the line number display. */
21120 it->lnum_pixel_width = tem_it.current_x;
21121 /* Copy the produced glyphs into IT's glyph_row. */
21122 struct glyph *g = scratch_glyph_row.glyphs[TEXT_AREA];
21123 struct glyph *e = g + scratch_glyph_row.used[TEXT_AREA];
21124 struct glyph *p = it->glyph_row ? it->glyph_row->glyphs[TEXT_AREA] : NULL;
21125 short *u = it->glyph_row ? &it->glyph_row->used[TEXT_AREA] : NULL;
21127 eassert (it->glyph_row == NULL || it->glyph_row->used[TEXT_AREA] == 0);
21129 for ( ; g < e; g++)
21131 it->current_x += g->pixel_width;
21132 /* The following is important when this function is called
21133 from move_it_in_display_line_to: HPOS is incremented only
21134 when we are in the visible portion of the glyph row. */
21135 if (it->current_x > it->first_visible_x)
21136 it->hpos++;
21137 if (p)
21139 *p++ = *g;
21140 (*u)++;
21144 /* Update IT's metrics due to glyphs produced for line numbers. */
21145 if (it->glyph_row)
21147 struct glyph_row *row = it->glyph_row;
21149 it->max_ascent = max (row->ascent, tem_it.max_ascent);
21150 it->max_descent = max (row->height - row->ascent, tem_it.max_descent);
21151 it->max_phys_ascent = max (row->phys_ascent, tem_it.max_phys_ascent);
21152 it->max_phys_descent = max (row->phys_height - row->phys_ascent,
21153 tem_it.max_phys_descent);
21155 else
21157 it->max_ascent = max (it->max_ascent, tem_it.max_ascent);
21158 it->max_descent = max (it->max_descent, tem_it.max_descent);
21159 it->max_phys_ascent = max (it->max_phys_ascent, tem_it.max_phys_ascent);
21160 it->max_phys_descent = max (it->max_phys_descent, tem_it.max_phys_descent);
21163 bidi_unshelve_cache (itdata, false);
21166 /* Return true if this glyph row needs a line number to be produced
21167 for it. */
21168 static bool
21169 should_produce_line_number (struct it *it)
21171 if (NILP (Vdisplay_line_numbers))
21172 return false;
21174 /* Don't display line numbers in minibuffer windows. */
21175 if (MINI_WINDOW_P (it->w))
21176 return false;
21178 #ifdef HAVE_WINDOW_SYSTEM
21179 /* Don't display line number in tooltip frames. */
21180 if (FRAMEP (tip_frame) && EQ (WINDOW_FRAME (it->w), tip_frame)
21181 #ifdef USE_GTK
21182 /* GTK builds store in tip_frame the frame that shows the tip,
21183 so we need an additional test. */
21184 && !NILP (Fframe_parameter (tip_frame, Qtooltip))
21185 #endif
21187 return false;
21188 #endif
21190 /* If the character at current position has a non-nil special
21191 property, disable line numbers for this row. This is for
21192 packages such as company-mode, which need this for their tricky
21193 layout, where line numbers get in the way. */
21194 Lisp_Object val = Fget_char_property (make_number (IT_CHARPOS (*it)),
21195 Qdisplay_line_numbers_disable,
21196 it->window);
21197 /* For ZV, we need to also look in empty overlays at that point,
21198 because get-char-property always returns nil for ZV, except if
21199 the property is in 'default-text-properties'. */
21200 if (NILP (val) && IT_CHARPOS (*it) >= ZV)
21201 val = disable_line_numbers_overlay_at_eob ();
21202 return NILP (val) ? true : false;
21205 /* Return true if ROW has no glyphs except those inserted by the
21206 display engine. This is needed for indicate-empty-lines and
21207 similar features when the glyph row starts with glyphs which didn't
21208 come from buffer or string. */
21209 static bool
21210 row_text_area_empty (struct glyph_row *row)
21212 if (!row->reversed_p)
21214 for (struct glyph *g = row->glyphs[TEXT_AREA];
21215 g < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
21216 g++)
21217 if (!NILP (g->object) || g->charpos > 0)
21218 return false;
21220 else
21222 for (struct glyph *g = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
21223 g > row->glyphs[TEXT_AREA];
21224 g--)
21225 if (!NILP ((g - 1)->object) || (g - 1)->charpos > 0)
21226 return false;
21229 return true;
21232 /* Construct the glyph row IT->glyph_row in the desired matrix of
21233 IT->w from text at the current position of IT. See dispextern.h
21234 for an overview of struct it. Value is true if
21235 IT->glyph_row displays text, as opposed to a line displaying ZV
21236 only. CURSOR_VPOS is the window-relative vertical position of
21237 the glyph row displaying the cursor, or -1 if unknown. */
21239 static bool
21240 display_line (struct it *it, int cursor_vpos)
21242 struct glyph_row *row = it->glyph_row;
21243 Lisp_Object overlay_arrow_string;
21244 struct it wrap_it;
21245 void *wrap_data = NULL;
21246 bool may_wrap = false;
21247 int wrap_x UNINIT;
21248 int wrap_row_used = -1;
21249 int wrap_row_ascent UNINIT, wrap_row_height UNINIT;
21250 int wrap_row_phys_ascent UNINIT, wrap_row_phys_height UNINIT;
21251 int wrap_row_extra_line_spacing UNINIT;
21252 ptrdiff_t wrap_row_min_pos UNINIT, wrap_row_min_bpos UNINIT;
21253 ptrdiff_t wrap_row_max_pos UNINIT, wrap_row_max_bpos UNINIT;
21254 int cvpos;
21255 ptrdiff_t min_pos = ZV + 1, max_pos = 0;
21256 ptrdiff_t min_bpos UNINIT, max_bpos UNINIT;
21257 bool pending_handle_line_prefix = false;
21258 int header_line = window_wants_header_line (it->w);
21259 bool hscroll_this_line = (cursor_vpos >= 0
21260 && it->vpos == cursor_vpos - header_line
21261 && hscrolling_current_line_p (it->w));
21262 int first_visible_x = it->first_visible_x;
21263 int last_visible_x = it->last_visible_x;
21264 int x_incr = 0;
21266 /* We always start displaying at hpos zero even if hscrolled. */
21267 eassert (it->hpos == 0 && it->current_x == 0);
21269 if (MATRIX_ROW_VPOS (row, it->w->desired_matrix)
21270 >= it->w->desired_matrix->nrows)
21272 it->w->nrows_scale_factor++;
21273 it->f->fonts_changed = true;
21274 return false;
21277 /* Clear the result glyph row and enable it. */
21278 prepare_desired_row (it->w, row, false);
21280 row->y = it->current_y;
21281 row->start = it->start;
21282 row->continuation_lines_width = it->continuation_lines_width;
21283 row->displays_text_p = true;
21284 row->starts_in_middle_of_char_p = it->starts_in_middle_of_char_p;
21285 it->starts_in_middle_of_char_p = false;
21287 /* Arrange the overlays nicely for our purposes. Usually, we call
21288 display_line on only one line at a time, in which case this
21289 can't really hurt too much, or we call it on lines which appear
21290 one after another in the buffer, in which case all calls to
21291 recenter_overlay_lists but the first will be pretty cheap. */
21292 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
21294 /* If we are going to display the cursor's line, account for the
21295 hscroll of that line. We subtract the window's min_hscroll,
21296 because that was already accounted for in init_iterator. */
21297 if (hscroll_this_line)
21298 x_incr =
21299 (window_hscroll_limited (it->w, it->f) - it->w->min_hscroll)
21300 * FRAME_COLUMN_WIDTH (it->f);
21302 bool line_number_needed = should_produce_line_number (it);
21304 /* Move over display elements that are not visible because we are
21305 hscrolled. This may stop at an x-position < first_visible_x
21306 if the first glyph is partially visible or if we hit a line end. */
21307 if (it->current_x < it->first_visible_x + x_incr)
21309 enum move_it_result move_result;
21311 this_line_min_pos = row->start.pos;
21312 if (hscroll_this_line)
21314 it->first_visible_x += x_incr;
21315 it->last_visible_x += x_incr;
21317 move_result = move_it_in_display_line_to (it, ZV, it->first_visible_x,
21318 MOVE_TO_POS | MOVE_TO_X);
21319 /* If we are under a large hscroll, move_it_in_display_line_to
21320 could hit the end of the line without reaching
21321 first_visible_x. Pretend that we did reach it. This is
21322 especially important on a TTY, where we will call
21323 extend_face_to_end_of_line, which needs to know how many
21324 blank glyphs to produce. */
21325 if (it->current_x < it->first_visible_x
21326 && (move_result == MOVE_NEWLINE_OR_CR
21327 || move_result == MOVE_POS_MATCH_OR_ZV))
21328 it->current_x = it->first_visible_x;
21330 /* Record the smallest positions seen while we moved over
21331 display elements that are not visible. This is needed by
21332 redisplay_internal for optimizing the case where the cursor
21333 stays inside the same line. The rest of this function only
21334 considers positions that are actually displayed, so
21335 RECORD_MAX_MIN_POS will not otherwise record positions that
21336 are hscrolled to the left of the left edge of the window. */
21337 min_pos = CHARPOS (this_line_min_pos);
21338 min_bpos = BYTEPOS (this_line_min_pos);
21340 /* Produce line number, if needed. */
21341 if (line_number_needed)
21342 maybe_produce_line_number (it);
21344 else if (it->area == TEXT_AREA)
21346 /* Line numbers should precede the line-prefix or wrap-prefix. */
21347 if (line_number_needed)
21348 maybe_produce_line_number (it);
21350 /* We only do this when not calling move_it_in_display_line_to
21351 above, because that function calls itself handle_line_prefix. */
21352 handle_line_prefix (it);
21354 else
21356 /* Line-prefix and wrap-prefix are always displayed in the text
21357 area. But if this is the first call to display_line after
21358 init_iterator, the iterator might have been set up to write
21359 into a marginal area, e.g. if the line begins with some
21360 display property that writes to the margins. So we need to
21361 wait with the call to handle_line_prefix until whatever
21362 writes to the margin has done its job. */
21363 pending_handle_line_prefix = true;
21366 /* Get the initial row height. This is either the height of the
21367 text hscrolled, if there is any, or zero. */
21368 row->ascent = it->max_ascent;
21369 row->height = it->max_ascent + it->max_descent;
21370 row->phys_ascent = it->max_phys_ascent;
21371 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
21372 row->extra_line_spacing = it->max_extra_line_spacing;
21374 /* Utility macro to record max and min buffer positions seen until now. */
21375 #define RECORD_MAX_MIN_POS(IT) \
21376 do \
21378 bool composition_p \
21379 = !STRINGP ((IT)->string) && ((IT)->what == IT_COMPOSITION); \
21380 ptrdiff_t current_pos = \
21381 composition_p ? (IT)->cmp_it.charpos \
21382 : IT_CHARPOS (*(IT)); \
21383 ptrdiff_t current_bpos = \
21384 composition_p ? CHAR_TO_BYTE (current_pos) \
21385 : IT_BYTEPOS (*(IT)); \
21386 if (current_pos < min_pos) \
21388 min_pos = current_pos; \
21389 min_bpos = current_bpos; \
21391 if (IT_CHARPOS (*it) > max_pos) \
21393 max_pos = IT_CHARPOS (*it); \
21394 max_bpos = IT_BYTEPOS (*it); \
21397 while (false)
21399 /* Loop generating characters. The loop is left with IT on the next
21400 character to display. */
21401 while (true)
21403 int n_glyphs_before, hpos_before, x_before;
21404 int x, nglyphs;
21405 int ascent = 0, descent = 0, phys_ascent = 0, phys_descent = 0;
21407 /* Retrieve the next thing to display. Value is false if end of
21408 buffer reached. */
21409 if (!get_next_display_element (it))
21411 bool row_has_glyphs = false;
21412 /* Maybe add a space at the end of this line that is used to
21413 display the cursor there under X. Set the charpos of the
21414 first glyph of blank lines not corresponding to any text
21415 to -1. */
21416 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
21417 row->exact_window_width_line_p = true;
21418 else if ((append_space_for_newline (it, true)
21419 && row->used[TEXT_AREA] == 1)
21420 || row->used[TEXT_AREA] == 0
21421 || (row_has_glyphs = row_text_area_empty (row)))
21423 row->glyphs[TEXT_AREA]->charpos = -1;
21424 /* Don't reset the displays_text_p flag if we are
21425 displaying line numbers or line-prefix. */
21426 if (!row_has_glyphs)
21427 row->displays_text_p = false;
21429 if (!NILP (BVAR (XBUFFER (it->w->contents), indicate_empty_lines))
21430 && (!MINI_WINDOW_P (it->w)))
21431 row->indicate_empty_line_p = true;
21434 it->continuation_lines_width = 0;
21435 /* Reset those iterator values set from display property
21436 values. This is for the case when the display property
21437 ends at ZV, and is not a replacing property, so pop_it is
21438 not called. */
21439 it->font_height = Qnil;
21440 it->voffset = 0;
21441 row->ends_at_zv_p = true;
21442 /* A row that displays right-to-left text must always have
21443 its last face extended all the way to the end of line,
21444 even if this row ends in ZV, because we still write to
21445 the screen left to right. We also need to extend the
21446 last face if the default face is remapped to some
21447 different face, otherwise the functions that clear
21448 portions of the screen will clear with the default face's
21449 background color. */
21450 if (row->reversed_p
21451 || lookup_basic_face (it->f, DEFAULT_FACE_ID) != DEFAULT_FACE_ID)
21452 extend_face_to_end_of_line (it);
21453 break;
21456 /* Now, get the metrics of what we want to display. This also
21457 generates glyphs in `row' (which is IT->glyph_row). */
21458 n_glyphs_before = row->used[TEXT_AREA];
21459 x = it->current_x;
21461 /* Remember the line height so far in case the next element doesn't
21462 fit on the line. */
21463 if (it->line_wrap != TRUNCATE)
21465 ascent = it->max_ascent;
21466 descent = it->max_descent;
21467 phys_ascent = it->max_phys_ascent;
21468 phys_descent = it->max_phys_descent;
21470 if (it->line_wrap == WORD_WRAP && it->area == TEXT_AREA)
21472 if (IT_DISPLAYING_WHITESPACE (it))
21473 may_wrap = true;
21474 else if (may_wrap)
21476 SAVE_IT (wrap_it, *it, wrap_data);
21477 wrap_x = x;
21478 wrap_row_used = row->used[TEXT_AREA];
21479 wrap_row_ascent = row->ascent;
21480 wrap_row_height = row->height;
21481 wrap_row_phys_ascent = row->phys_ascent;
21482 wrap_row_phys_height = row->phys_height;
21483 wrap_row_extra_line_spacing = row->extra_line_spacing;
21484 wrap_row_min_pos = min_pos;
21485 wrap_row_min_bpos = min_bpos;
21486 wrap_row_max_pos = max_pos;
21487 wrap_row_max_bpos = max_bpos;
21488 may_wrap = false;
21493 PRODUCE_GLYPHS (it);
21495 /* If this display element was in marginal areas, continue with
21496 the next one. */
21497 if (it->area != TEXT_AREA)
21499 row->ascent = max (row->ascent, it->max_ascent);
21500 row->height = max (row->height, it->max_ascent + it->max_descent);
21501 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
21502 row->phys_height = max (row->phys_height,
21503 it->max_phys_ascent + it->max_phys_descent);
21504 row->extra_line_spacing = max (row->extra_line_spacing,
21505 it->max_extra_line_spacing);
21506 set_iterator_to_next (it, true);
21507 /* If we didn't handle the line/wrap prefix above, and the
21508 call to set_iterator_to_next just switched to TEXT_AREA,
21509 process the prefix now. */
21510 if (it->area == TEXT_AREA && pending_handle_line_prefix)
21512 /* Line numbers should precede the line-prefix or wrap-prefix. */
21513 if (line_number_needed)
21514 maybe_produce_line_number (it);
21516 pending_handle_line_prefix = false;
21517 handle_line_prefix (it);
21519 continue;
21522 /* Does the display element fit on the line? If we truncate
21523 lines, we should draw past the right edge of the window. If
21524 we don't truncate, we want to stop so that we can display the
21525 continuation glyph before the right margin. If lines are
21526 continued, there are two possible strategies for characters
21527 resulting in more than 1 glyph (e.g. tabs): Display as many
21528 glyphs as possible in this line and leave the rest for the
21529 continuation line, or display the whole element in the next
21530 line. Original redisplay did the former, so we do it also. */
21531 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
21532 hpos_before = it->hpos;
21533 x_before = x;
21535 if (/* Not a newline. */
21536 nglyphs > 0
21537 /* Glyphs produced fit entirely in the line. */
21538 && it->current_x < it->last_visible_x)
21540 it->hpos += nglyphs;
21541 row->ascent = max (row->ascent, it->max_ascent);
21542 row->height = max (row->height, it->max_ascent + it->max_descent);
21543 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
21544 row->phys_height = max (row->phys_height,
21545 it->max_phys_ascent + it->max_phys_descent);
21546 row->extra_line_spacing = max (row->extra_line_spacing,
21547 it->max_extra_line_spacing);
21548 if (it->current_x - it->pixel_width < it->first_visible_x
21549 /* In R2L rows, we arrange in extend_face_to_end_of_line
21550 to add a right offset to the line, by a suitable
21551 change to the stretch glyph that is the leftmost
21552 glyph of the line. */
21553 && !row->reversed_p)
21554 row->x = x - it->first_visible_x;
21555 /* Record the maximum and minimum buffer positions seen so
21556 far in glyphs that will be displayed by this row. */
21557 if (it->bidi_p)
21558 RECORD_MAX_MIN_POS (it);
21560 else
21562 int i, new_x;
21563 struct glyph *glyph;
21565 for (i = 0; i < nglyphs; ++i, x = new_x)
21567 /* Identify the glyphs added by the last call to
21568 PRODUCE_GLYPHS. In R2L rows, they are prepended to
21569 the previous glyphs. */
21570 if (!row->reversed_p)
21571 glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
21572 else
21573 glyph = row->glyphs[TEXT_AREA] + nglyphs - 1 - i;
21574 new_x = x + glyph->pixel_width;
21576 if (/* Lines are continued. */
21577 it->line_wrap != TRUNCATE
21578 && (/* Glyph doesn't fit on the line. */
21579 new_x > it->last_visible_x
21580 /* Or it fits exactly on a window system frame. */
21581 || (new_x == it->last_visible_x
21582 && FRAME_WINDOW_P (it->f)
21583 && (row->reversed_p
21584 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
21585 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)))))
21587 /* End of a continued line. */
21589 if (it->hpos == 0
21590 || (new_x == it->last_visible_x
21591 && FRAME_WINDOW_P (it->f)
21592 && (row->reversed_p
21593 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
21594 : WINDOW_RIGHT_FRINGE_WIDTH (it->w))))
21596 /* Current glyph is the only one on the line or
21597 fits exactly on the line. We must continue
21598 the line because we can't draw the cursor
21599 after the glyph. */
21600 row->continued_p = true;
21601 it->current_x = new_x;
21602 it->continuation_lines_width += new_x;
21603 ++it->hpos;
21604 if (i == nglyphs - 1)
21606 /* If line-wrap is on, check if a previous
21607 wrap point was found. */
21608 if (!IT_OVERFLOW_NEWLINE_INTO_FRINGE (it)
21609 && wrap_row_used > 0
21610 /* Even if there is a previous wrap
21611 point, continue the line here as
21612 usual, if (i) the previous character
21613 was a space or tab AND (ii) the
21614 current character is not. */
21615 && (!may_wrap
21616 || IT_DISPLAYING_WHITESPACE (it)))
21617 goto back_to_wrap;
21619 /* Record the maximum and minimum buffer
21620 positions seen so far in glyphs that will be
21621 displayed by this row. */
21622 if (it->bidi_p)
21623 RECORD_MAX_MIN_POS (it);
21624 set_iterator_to_next (it, true);
21625 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
21627 if (!get_next_display_element (it))
21629 row->exact_window_width_line_p = true;
21630 it->continuation_lines_width = 0;
21631 it->font_height = Qnil;
21632 it->voffset = 0;
21633 row->continued_p = false;
21634 row->ends_at_zv_p = true;
21636 else if (ITERATOR_AT_END_OF_LINE_P (it))
21638 row->continued_p = false;
21639 row->exact_window_width_line_p = true;
21641 /* If line-wrap is on, check if a
21642 previous wrap point was found. */
21643 else if (wrap_row_used > 0
21644 /* Even if there is a previous wrap
21645 point, continue the line here as
21646 usual, if (i) the previous character
21647 was a space or tab AND (ii) the
21648 current character is not. */
21649 && (!may_wrap
21650 || IT_DISPLAYING_WHITESPACE (it)))
21651 goto back_to_wrap;
21655 else if (it->bidi_p)
21656 RECORD_MAX_MIN_POS (it);
21657 if (WINDOW_LEFT_MARGIN_WIDTH (it->w) > 0
21658 || WINDOW_RIGHT_MARGIN_WIDTH (it->w) > 0)
21659 extend_face_to_end_of_line (it);
21661 else if (CHAR_GLYPH_PADDING_P (*glyph)
21662 && !FRAME_WINDOW_P (it->f))
21664 /* A padding glyph that doesn't fit on this line.
21665 This means the whole character doesn't fit
21666 on the line. */
21667 if (row->reversed_p)
21668 unproduce_glyphs (it, row->used[TEXT_AREA]
21669 - n_glyphs_before);
21670 row->used[TEXT_AREA] = n_glyphs_before;
21672 /* Fill the rest of the row with continuation
21673 glyphs like in 20.x. */
21674 while (row->glyphs[TEXT_AREA] + row->used[TEXT_AREA]
21675 < row->glyphs[1 + TEXT_AREA])
21676 produce_special_glyphs (it, IT_CONTINUATION);
21678 row->continued_p = true;
21679 it->current_x = x_before;
21680 it->continuation_lines_width += x_before;
21682 /* Restore the height to what it was before the
21683 element not fitting on the line. */
21684 it->max_ascent = ascent;
21685 it->max_descent = descent;
21686 it->max_phys_ascent = phys_ascent;
21687 it->max_phys_descent = phys_descent;
21688 if (WINDOW_LEFT_MARGIN_WIDTH (it->w) > 0
21689 || WINDOW_RIGHT_MARGIN_WIDTH (it->w) > 0)
21690 extend_face_to_end_of_line (it);
21692 else if (wrap_row_used > 0)
21694 back_to_wrap:
21695 if (row->reversed_p)
21696 unproduce_glyphs (it,
21697 row->used[TEXT_AREA] - wrap_row_used);
21698 RESTORE_IT (it, &wrap_it, wrap_data);
21699 it->continuation_lines_width += wrap_x;
21700 row->used[TEXT_AREA] = wrap_row_used;
21701 row->ascent = wrap_row_ascent;
21702 row->height = wrap_row_height;
21703 row->phys_ascent = wrap_row_phys_ascent;
21704 row->phys_height = wrap_row_phys_height;
21705 row->extra_line_spacing = wrap_row_extra_line_spacing;
21706 min_pos = wrap_row_min_pos;
21707 min_bpos = wrap_row_min_bpos;
21708 max_pos = wrap_row_max_pos;
21709 max_bpos = wrap_row_max_bpos;
21710 row->continued_p = true;
21711 row->ends_at_zv_p = false;
21712 row->exact_window_width_line_p = false;
21714 /* Make sure that a non-default face is extended
21715 up to the right margin of the window. */
21716 extend_face_to_end_of_line (it);
21718 else if ((it->what == IT_CHARACTER
21719 || it->what == IT_STRETCH
21720 || it->what == IT_COMPOSITION)
21721 && it->c == '\t' && FRAME_WINDOW_P (it->f))
21723 /* A TAB that extends past the right edge of the
21724 window. This produces a single glyph on
21725 window system frames. We leave the glyph in
21726 this row and let it fill the row, but don't
21727 consume the TAB. */
21728 if ((row->reversed_p
21729 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
21730 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
21731 produce_special_glyphs (it, IT_CONTINUATION);
21732 it->continuation_lines_width += it->last_visible_x;
21733 row->ends_in_middle_of_char_p = true;
21734 row->continued_p = true;
21735 glyph->pixel_width = it->last_visible_x - x;
21736 it->starts_in_middle_of_char_p = true;
21737 if (WINDOW_LEFT_MARGIN_WIDTH (it->w) > 0
21738 || WINDOW_RIGHT_MARGIN_WIDTH (it->w) > 0)
21739 extend_face_to_end_of_line (it);
21741 else
21743 /* Something other than a TAB that draws past
21744 the right edge of the window. Restore
21745 positions to values before the element. */
21746 if (row->reversed_p)
21747 unproduce_glyphs (it, row->used[TEXT_AREA]
21748 - (n_glyphs_before + i));
21749 row->used[TEXT_AREA] = n_glyphs_before + i;
21751 /* Display continuation glyphs. */
21752 it->current_x = x_before;
21753 it->continuation_lines_width += x;
21754 if (!FRAME_WINDOW_P (it->f)
21755 || (row->reversed_p
21756 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
21757 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
21758 produce_special_glyphs (it, IT_CONTINUATION);
21759 row->continued_p = true;
21761 extend_face_to_end_of_line (it);
21763 if (nglyphs > 1 && i > 0)
21765 row->ends_in_middle_of_char_p = true;
21766 it->starts_in_middle_of_char_p = true;
21769 /* Restore the height to what it was before the
21770 element not fitting on the line. */
21771 it->max_ascent = ascent;
21772 it->max_descent = descent;
21773 it->max_phys_ascent = phys_ascent;
21774 it->max_phys_descent = phys_descent;
21777 break;
21779 else if (new_x > it->first_visible_x)
21781 /* Increment number of glyphs actually displayed. */
21782 ++it->hpos;
21784 /* Record the maximum and minimum buffer positions
21785 seen so far in glyphs that will be displayed by
21786 this row. */
21787 if (it->bidi_p)
21788 RECORD_MAX_MIN_POS (it);
21790 if (x < it->first_visible_x && !row->reversed_p)
21791 /* Glyph is partially visible, i.e. row starts at
21792 negative X position. Don't do that in R2L
21793 rows, where we arrange to add a right offset to
21794 the line in extend_face_to_end_of_line, by a
21795 suitable change to the stretch glyph that is
21796 the leftmost glyph of the line. */
21797 row->x = x - it->first_visible_x;
21798 /* When the last glyph of an R2L row only fits
21799 partially on the line, we need to set row->x to a
21800 negative offset, so that the leftmost glyph is
21801 the one that is partially visible. But if we are
21802 going to produce the truncation glyph, this will
21803 be taken care of in produce_special_glyphs. */
21804 if (row->reversed_p
21805 && new_x > it->last_visible_x
21806 && !(it->line_wrap == TRUNCATE
21807 && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0))
21809 eassert (FRAME_WINDOW_P (it->f));
21810 row->x = it->last_visible_x - new_x;
21813 else
21815 /* Glyph is completely off the left margin of the
21816 window. This should not happen because of the
21817 move_it_in_display_line at the start of this
21818 function, unless the text display area of the
21819 window is empty. */
21820 eassert (it->first_visible_x <= it->last_visible_x);
21823 /* Even if this display element produced no glyphs at all,
21824 we want to record its position. */
21825 if (it->bidi_p && nglyphs == 0)
21826 RECORD_MAX_MIN_POS (it);
21828 row->ascent = max (row->ascent, it->max_ascent);
21829 row->height = max (row->height, it->max_ascent + it->max_descent);
21830 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
21831 row->phys_height = max (row->phys_height,
21832 it->max_phys_ascent + it->max_phys_descent);
21833 row->extra_line_spacing = max (row->extra_line_spacing,
21834 it->max_extra_line_spacing);
21836 /* End of this display line if row is continued. */
21837 if (row->continued_p || row->ends_at_zv_p)
21838 break;
21841 at_end_of_line:
21842 /* Is this a line end? If yes, we're also done, after making
21843 sure that a non-default face is extended up to the right
21844 margin of the window. */
21845 if (ITERATOR_AT_END_OF_LINE_P (it))
21847 int used_before = row->used[TEXT_AREA];
21849 row->ends_in_newline_from_string_p = STRINGP (it->object);
21851 /* Add a space at the end of the line that is used to
21852 display the cursor there. */
21853 if (!IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
21854 append_space_for_newline (it, false);
21856 /* Extend the face to the end of the line. */
21857 extend_face_to_end_of_line (it);
21859 /* Make sure we have the position. */
21860 if (used_before == 0)
21861 row->glyphs[TEXT_AREA]->charpos = CHARPOS (it->position);
21863 /* Record the position of the newline, for use in
21864 find_row_edges. */
21865 it->eol_pos = it->current.pos;
21867 /* Consume the line end. This skips over invisible lines. */
21868 set_iterator_to_next (it, true);
21869 it->continuation_lines_width = 0;
21870 break;
21873 /* Proceed with next display element. Note that this skips
21874 over lines invisible because of selective display. */
21875 set_iterator_to_next (it, true);
21877 /* If we truncate lines, we are done when the last displayed
21878 glyphs reach past the right margin of the window. */
21879 if (it->line_wrap == TRUNCATE
21880 && ((FRAME_WINDOW_P (it->f)
21881 /* Images are preprocessed in produce_image_glyph such
21882 that they are cropped at the right edge of the
21883 window, so an image glyph will always end exactly at
21884 last_visible_x, even if there's no right fringe. */
21885 && ((row->reversed_p
21886 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
21887 : WINDOW_RIGHT_FRINGE_WIDTH (it->w))
21888 || it->what == IT_IMAGE))
21889 ? (it->current_x >= it->last_visible_x)
21890 : (it->current_x > it->last_visible_x)))
21892 /* Maybe add truncation glyphs. */
21893 if (!FRAME_WINDOW_P (it->f)
21894 || (row->reversed_p
21895 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
21896 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
21898 int i, n;
21900 if (!row->reversed_p)
21902 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
21903 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
21904 break;
21906 else
21908 for (i = 0; i < row->used[TEXT_AREA]; i++)
21909 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
21910 break;
21911 /* Remove any padding glyphs at the front of ROW, to
21912 make room for the truncation glyphs we will be
21913 adding below. The loop below always inserts at
21914 least one truncation glyph, so also remove the
21915 last glyph added to ROW. */
21916 unproduce_glyphs (it, i + 1);
21917 /* Adjust i for the loop below. */
21918 i = row->used[TEXT_AREA] - (i + 1);
21921 /* produce_special_glyphs overwrites the last glyph, so
21922 we don't want that if we want to keep that last
21923 glyph, which means it's an image. */
21924 if (it->current_x > it->last_visible_x)
21926 it->current_x = x_before;
21927 if (!FRAME_WINDOW_P (it->f))
21929 for (n = row->used[TEXT_AREA]; i < n; ++i)
21931 row->used[TEXT_AREA] = i;
21932 produce_special_glyphs (it, IT_TRUNCATION);
21935 else
21937 row->used[TEXT_AREA] = i;
21938 produce_special_glyphs (it, IT_TRUNCATION);
21940 it->hpos = hpos_before;
21943 else if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
21945 /* Don't truncate if we can overflow newline into fringe. */
21946 if (!get_next_display_element (it))
21948 it->continuation_lines_width = 0;
21949 it->font_height = Qnil;
21950 it->voffset = 0;
21951 row->ends_at_zv_p = true;
21952 row->exact_window_width_line_p = true;
21953 break;
21955 if (ITERATOR_AT_END_OF_LINE_P (it))
21957 row->exact_window_width_line_p = true;
21958 goto at_end_of_line;
21960 it->current_x = x_before;
21961 it->hpos = hpos_before;
21964 row->truncated_on_right_p = true;
21965 it->continuation_lines_width = 0;
21966 reseat_at_next_visible_line_start (it, false);
21967 /* We insist below that IT's position be at ZV because in
21968 bidi-reordered lines the character at visible line start
21969 might not be the character that follows the newline in
21970 the logical order. */
21971 if (IT_BYTEPOS (*it) > BEG_BYTE)
21972 row->ends_at_zv_p =
21973 IT_BYTEPOS (*it) >= ZV_BYTE && FETCH_BYTE (ZV_BYTE - 1) != '\n';
21974 else
21975 row->ends_at_zv_p = false;
21976 break;
21980 if (wrap_data)
21981 bidi_unshelve_cache (wrap_data, true);
21983 /* If line is not empty and hscrolled, maybe insert truncation glyphs
21984 at the left window margin. */
21985 if (it->first_visible_x
21986 && IT_CHARPOS (*it) != CHARPOS (row->start.pos))
21988 if (!FRAME_WINDOW_P (it->f)
21989 || (((row->reversed_p
21990 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
21991 : WINDOW_LEFT_FRINGE_WIDTH (it->w)) == 0)
21992 /* Don't let insert_left_trunc_glyphs overwrite the
21993 first glyph of the row if it is an image. */
21994 && row->glyphs[TEXT_AREA]->type != IMAGE_GLYPH))
21995 insert_left_trunc_glyphs (it);
21996 row->truncated_on_left_p = true;
21999 /* Remember the position at which this line ends.
22001 BIDI Note: any code that needs MATRIX_ROW_START/END_CHARPOS
22002 cannot be before the call to find_row_edges below, since that is
22003 where these positions are determined. */
22004 row->end = it->current;
22005 if (!it->bidi_p)
22007 row->minpos = row->start.pos;
22008 row->maxpos = row->end.pos;
22010 else
22012 /* ROW->minpos and ROW->maxpos must be the smallest and
22013 `1 + the largest' buffer positions in ROW. But if ROW was
22014 bidi-reordered, these two positions can be anywhere in the
22015 row, so we must determine them now. */
22016 find_row_edges (it, row, min_pos, min_bpos, max_pos, max_bpos);
22019 /* If the start of this line is the overlay arrow-position, then
22020 mark this glyph row as the one containing the overlay arrow.
22021 This is clearly a mess with variable size fonts. It would be
22022 better to let it be displayed like cursors under X. */
22023 if ((MATRIX_ROW_DISPLAYS_TEXT_P (row) || !overlay_arrow_seen)
22024 && (overlay_arrow_string = overlay_arrow_at_row (it, row),
22025 !NILP (overlay_arrow_string)))
22027 /* Overlay arrow in window redisplay is a fringe bitmap. */
22028 if (STRINGP (overlay_arrow_string))
22030 struct glyph_row *arrow_row
22031 = get_overlay_arrow_glyph_row (it->w, overlay_arrow_string);
22032 struct glyph *glyph = arrow_row->glyphs[TEXT_AREA];
22033 struct glyph *arrow_end = glyph + arrow_row->used[TEXT_AREA];
22034 struct glyph *p = row->glyphs[TEXT_AREA];
22035 struct glyph *p2, *end;
22037 /* Copy the arrow glyphs. */
22038 while (glyph < arrow_end)
22039 *p++ = *glyph++;
22041 /* Throw away padding glyphs. */
22042 p2 = p;
22043 end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
22044 while (p2 < end && CHAR_GLYPH_PADDING_P (*p2))
22045 ++p2;
22046 if (p2 > p)
22048 while (p2 < end)
22049 *p++ = *p2++;
22050 row->used[TEXT_AREA] = p2 - row->glyphs[TEXT_AREA];
22053 else
22055 eassert (INTEGERP (overlay_arrow_string));
22056 row->overlay_arrow_bitmap = XINT (overlay_arrow_string);
22058 overlay_arrow_seen = true;
22061 /* Highlight trailing whitespace. */
22062 if (!NILP (Vshow_trailing_whitespace))
22063 highlight_trailing_whitespace (it->f, it->glyph_row);
22065 /* Compute pixel dimensions of this line. */
22066 compute_line_metrics (it);
22068 /* Implementation note: No changes in the glyphs of ROW or in their
22069 faces can be done past this point, because compute_line_metrics
22070 computes ROW's hash value and stores it within the glyph_row
22071 structure. */
22073 /* Record whether this row ends inside an ellipsis. */
22074 row->ends_in_ellipsis_p
22075 = (it->method == GET_FROM_DISPLAY_VECTOR
22076 && it->ellipsis_p);
22078 /* Save fringe bitmaps in this row. */
22079 row->left_user_fringe_bitmap = it->left_user_fringe_bitmap;
22080 row->left_user_fringe_face_id = it->left_user_fringe_face_id;
22081 row->right_user_fringe_bitmap = it->right_user_fringe_bitmap;
22082 row->right_user_fringe_face_id = it->right_user_fringe_face_id;
22084 it->left_user_fringe_bitmap = 0;
22085 it->left_user_fringe_face_id = 0;
22086 it->right_user_fringe_bitmap = 0;
22087 it->right_user_fringe_face_id = 0;
22089 /* Maybe set the cursor. */
22090 cvpos = it->w->cursor.vpos;
22091 if ((cvpos < 0
22092 /* In bidi-reordered rows, keep checking for proper cursor
22093 position even if one has been found already, because buffer
22094 positions in such rows change non-linearly with ROW->VPOS,
22095 when a line is continued. One exception: when we are at ZV,
22096 display cursor on the first suitable glyph row, since all
22097 the empty rows after that also have their position set to ZV. */
22098 /* FIXME: Revisit this when glyph ``spilling'' in continuation
22099 lines' rows is implemented for bidi-reordered rows. */
22100 || (it->bidi_p
22101 && !MATRIX_ROW (it->w->desired_matrix, cvpos)->ends_at_zv_p))
22102 && PT >= MATRIX_ROW_START_CHARPOS (row)
22103 && PT <= MATRIX_ROW_END_CHARPOS (row)
22104 && cursor_row_p (row))
22105 set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
22107 /* Prepare for the next line. This line starts horizontally at (X
22108 HPOS) = (0 0). Vertical positions are incremented. As a
22109 convenience for the caller, IT->glyph_row is set to the next
22110 row to be used. */
22111 it->current_x = it->hpos = 0;
22112 it->current_y += row->height;
22113 /* Restore the first and last visible X if we adjusted them for
22114 current-line hscrolling. */
22115 if (hscroll_this_line)
22117 it->first_visible_x = first_visible_x;
22118 it->last_visible_x = last_visible_x;
22120 SET_TEXT_POS (it->eol_pos, 0, 0);
22121 ++it->vpos;
22122 ++it->glyph_row;
22123 /* The next row should by default use the same value of the
22124 reversed_p flag as this one. set_iterator_to_next decides when
22125 it's a new paragraph, and PRODUCE_GLYPHS recomputes the value of
22126 the flag accordingly. */
22127 if (it->glyph_row < MATRIX_BOTTOM_TEXT_ROW (it->w->desired_matrix, it->w))
22128 it->glyph_row->reversed_p = row->reversed_p;
22129 it->start = row->end;
22130 return MATRIX_ROW_DISPLAYS_TEXT_P (row);
22132 #undef RECORD_MAX_MIN_POS
22135 DEFUN ("current-bidi-paragraph-direction", Fcurrent_bidi_paragraph_direction,
22136 Scurrent_bidi_paragraph_direction, 0, 1, 0,
22137 doc: /* Return paragraph direction at point in BUFFER.
22138 Value is either `left-to-right' or `right-to-left'.
22139 If BUFFER is omitted or nil, it defaults to the current buffer.
22141 Paragraph direction determines how the text in the paragraph is displayed.
22142 In left-to-right paragraphs, text begins at the left margin of the window
22143 and the reading direction is generally left to right. In right-to-left
22144 paragraphs, text begins at the right margin and is read from right to left.
22146 See also `bidi-paragraph-direction'. */)
22147 (Lisp_Object buffer)
22149 struct buffer *buf = current_buffer;
22150 struct buffer *old = buf;
22152 if (! NILP (buffer))
22154 CHECK_BUFFER (buffer);
22155 buf = XBUFFER (buffer);
22158 if (NILP (BVAR (buf, bidi_display_reordering))
22159 || NILP (BVAR (buf, enable_multibyte_characters))
22160 /* When we are loading loadup.el, the character property tables
22161 needed for bidi iteration are not yet available. */
22162 || redisplay__inhibit_bidi)
22163 return Qleft_to_right;
22164 else if (!NILP (BVAR (buf, bidi_paragraph_direction)))
22165 return BVAR (buf, bidi_paragraph_direction);
22166 else
22168 /* Determine the direction from buffer text. We could try to
22169 use current_matrix if it is up to date, but this seems fast
22170 enough as it is. */
22171 struct bidi_it itb;
22172 ptrdiff_t pos = BUF_PT (buf);
22173 ptrdiff_t bytepos = BUF_PT_BYTE (buf);
22174 int c;
22175 void *itb_data = bidi_shelve_cache ();
22177 set_buffer_temp (buf);
22178 /* bidi_paragraph_init finds the base direction of the paragraph
22179 by searching forward from paragraph start. We need the base
22180 direction of the current or _previous_ paragraph, so we need
22181 to make sure we are within that paragraph. To that end, find
22182 the previous non-empty line. */
22183 if (pos >= ZV && pos > BEGV)
22184 DEC_BOTH (pos, bytepos);
22185 AUTO_STRING (trailing_white_space, "[\f\t ]*\n");
22186 if (fast_looking_at (trailing_white_space,
22187 pos, bytepos, ZV, ZV_BYTE, Qnil) > 0)
22189 while ((c = FETCH_BYTE (bytepos)) == '\n'
22190 || c == ' ' || c == '\t' || c == '\f')
22192 if (bytepos <= BEGV_BYTE)
22193 break;
22194 bytepos--;
22195 pos--;
22197 while (!CHAR_HEAD_P (FETCH_BYTE (bytepos)))
22198 bytepos--;
22200 bidi_init_it (pos, bytepos, FRAME_WINDOW_P (SELECTED_FRAME ()), &itb);
22201 itb.paragraph_dir = NEUTRAL_DIR;
22202 itb.string.s = NULL;
22203 itb.string.lstring = Qnil;
22204 itb.string.bufpos = 0;
22205 itb.string.from_disp_str = false;
22206 itb.string.unibyte = false;
22207 /* We have no window to use here for ignoring window-specific
22208 overlays. Using NULL for window pointer will cause
22209 compute_display_string_pos to use the current buffer. */
22210 itb.w = NULL;
22211 bidi_paragraph_init (NEUTRAL_DIR, &itb, true);
22212 bidi_unshelve_cache (itb_data, false);
22213 set_buffer_temp (old);
22214 switch (itb.paragraph_dir)
22216 case L2R:
22217 return Qleft_to_right;
22218 break;
22219 case R2L:
22220 return Qright_to_left;
22221 break;
22222 default:
22223 emacs_abort ();
22228 DEFUN ("bidi-find-overridden-directionality",
22229 Fbidi_find_overridden_directionality,
22230 Sbidi_find_overridden_directionality, 2, 3, 0,
22231 doc: /* Return position between FROM and TO where directionality was overridden.
22233 This function returns the first character position in the specified
22234 region of OBJECT where there is a character whose `bidi-class' property
22235 is `L', but which was forced to display as `R' by a directional
22236 override, and likewise with characters whose `bidi-class' is `R'
22237 or `AL' that were forced to display as `L'.
22239 If no such character is found, the function returns nil.
22241 OBJECT is a Lisp string or buffer to search for overridden
22242 directionality, and defaults to the current buffer if nil or omitted.
22243 OBJECT can also be a window, in which case the function will search
22244 the buffer displayed in that window. Passing the window instead of
22245 a buffer is preferable when the buffer is displayed in some window,
22246 because this function will then be able to correctly account for
22247 window-specific overlays, which can affect the results.
22249 Strong directional characters `L', `R', and `AL' can have their
22250 intrinsic directionality overridden by directional override
22251 control characters RLO (u+202e) and LRO (u+202d). See the
22252 function `get-char-code-property' for a way to inquire about
22253 the `bidi-class' property of a character. */)
22254 (Lisp_Object from, Lisp_Object to, Lisp_Object object)
22256 struct buffer *buf = current_buffer;
22257 struct buffer *old = buf;
22258 struct window *w = NULL;
22259 bool frame_window_p = FRAME_WINDOW_P (SELECTED_FRAME ());
22260 struct bidi_it itb;
22261 ptrdiff_t from_pos, to_pos, from_bpos;
22262 void *itb_data;
22264 if (!NILP (object))
22266 if (BUFFERP (object))
22267 buf = XBUFFER (object);
22268 else if (WINDOWP (object))
22270 w = decode_live_window (object);
22271 buf = XBUFFER (w->contents);
22272 frame_window_p = FRAME_WINDOW_P (XFRAME (w->frame));
22274 else
22275 CHECK_STRING (object);
22278 if (STRINGP (object))
22280 /* Characters in unibyte strings are always treated by bidi.c as
22281 strong LTR. */
22282 if (!STRING_MULTIBYTE (object)
22283 /* When we are loading loadup.el, the character property
22284 tables needed for bidi iteration are not yet
22285 available. */
22286 || redisplay__inhibit_bidi)
22287 return Qnil;
22289 validate_subarray (object, from, to, SCHARS (object), &from_pos, &to_pos);
22290 if (from_pos >= SCHARS (object))
22291 return Qnil;
22293 /* Set up the bidi iterator. */
22294 itb_data = bidi_shelve_cache ();
22295 itb.paragraph_dir = NEUTRAL_DIR;
22296 itb.string.lstring = object;
22297 itb.string.s = NULL;
22298 itb.string.schars = SCHARS (object);
22299 itb.string.bufpos = 0;
22300 itb.string.from_disp_str = false;
22301 itb.string.unibyte = false;
22302 itb.w = w;
22303 bidi_init_it (0, 0, frame_window_p, &itb);
22305 else
22307 /* Nothing this fancy can happen in unibyte buffers, or in a
22308 buffer that disabled reordering, or if FROM is at EOB. */
22309 if (NILP (BVAR (buf, bidi_display_reordering))
22310 || NILP (BVAR (buf, enable_multibyte_characters))
22311 /* When we are loading loadup.el, the character property
22312 tables needed for bidi iteration are not yet
22313 available. */
22314 || redisplay__inhibit_bidi)
22315 return Qnil;
22317 set_buffer_temp (buf);
22318 validate_region (&from, &to);
22319 from_pos = XINT (from);
22320 to_pos = XINT (to);
22321 if (from_pos >= ZV)
22322 return Qnil;
22324 /* Set up the bidi iterator. */
22325 itb_data = bidi_shelve_cache ();
22326 from_bpos = CHAR_TO_BYTE (from_pos);
22327 if (from_pos == BEGV)
22329 itb.charpos = BEGV;
22330 itb.bytepos = BEGV_BYTE;
22332 else if (FETCH_CHAR (from_bpos - 1) == '\n')
22334 itb.charpos = from_pos;
22335 itb.bytepos = from_bpos;
22337 else
22338 itb.charpos = find_newline_no_quit (from_pos, CHAR_TO_BYTE (from_pos),
22339 -1, &itb.bytepos);
22340 itb.paragraph_dir = NEUTRAL_DIR;
22341 itb.string.s = NULL;
22342 itb.string.lstring = Qnil;
22343 itb.string.bufpos = 0;
22344 itb.string.from_disp_str = false;
22345 itb.string.unibyte = false;
22346 itb.w = w;
22347 bidi_init_it (itb.charpos, itb.bytepos, frame_window_p, &itb);
22350 ptrdiff_t found;
22351 do {
22352 /* For the purposes of this function, the actual base direction of
22353 the paragraph doesn't matter, so just set it to L2R. */
22354 bidi_paragraph_init (L2R, &itb, false);
22355 while ((found = bidi_find_first_overridden (&itb)) < from_pos)
22357 } while (found == ZV && itb.ch == '\n' && itb.charpos < to_pos);
22359 bidi_unshelve_cache (itb_data, false);
22360 set_buffer_temp (old);
22362 return (from_pos <= found && found < to_pos) ? make_number (found) : Qnil;
22365 DEFUN ("move-point-visually", Fmove_point_visually,
22366 Smove_point_visually, 1, 1, 0,
22367 doc: /* Move point in the visual order in the specified DIRECTION.
22368 DIRECTION can be 1, meaning move to the right, or -1, which moves to the
22369 left.
22371 Value is the new character position of point. */)
22372 (Lisp_Object direction)
22374 struct window *w = XWINDOW (selected_window);
22375 struct buffer *b = XBUFFER (w->contents);
22376 struct glyph_row *row;
22377 int dir;
22378 Lisp_Object paragraph_dir;
22380 #define ROW_GLYPH_NEWLINE_P(ROW,GLYPH) \
22381 (!(ROW)->continued_p \
22382 && NILP ((GLYPH)->object) \
22383 && (GLYPH)->type == CHAR_GLYPH \
22384 && (GLYPH)->u.ch == ' ' \
22385 && (GLYPH)->charpos >= 0 \
22386 && !(GLYPH)->avoid_cursor_p)
22388 CHECK_NUMBER (direction);
22389 dir = XINT (direction);
22390 if (dir > 0)
22391 dir = 1;
22392 else
22393 dir = -1;
22395 /* If current matrix is up-to-date, we can use the information
22396 recorded in the glyphs, at least as long as the goal is on the
22397 screen. */
22398 if (w->window_end_valid
22399 && !windows_or_buffers_changed
22400 && b
22401 && !b->clip_changed
22402 && !b->prevent_redisplay_optimizations_p
22403 && !window_outdated (w)
22404 /* We rely below on the cursor coordinates to be up to date, but
22405 we cannot trust them if some command moved point since the
22406 last complete redisplay. */
22407 && w->last_point == BUF_PT (b)
22408 && w->cursor.vpos >= 0
22409 && w->cursor.vpos < w->current_matrix->nrows
22410 && (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos))->enabled_p)
22412 struct glyph *g = row->glyphs[TEXT_AREA];
22413 struct glyph *e = dir > 0 ? g + row->used[TEXT_AREA] : g - 1;
22414 struct glyph *gpt = g + w->cursor.hpos;
22416 for (g = gpt + dir; (dir > 0 ? g < e : g > e); g += dir)
22418 if (BUFFERP (g->object) && g->charpos != PT)
22420 SET_PT (g->charpos);
22421 w->cursor.vpos = -1;
22422 return make_number (PT);
22424 else if (!NILP (g->object) && !EQ (g->object, gpt->object))
22426 ptrdiff_t new_pos;
22428 if (BUFFERP (gpt->object))
22430 new_pos = PT;
22431 if ((gpt->resolved_level - row->reversed_p) % 2 == 0)
22432 new_pos += (row->reversed_p ? -dir : dir);
22433 else
22434 new_pos -= (row->reversed_p ? -dir : dir);
22435 new_pos = clip_to_bounds (BEGV, new_pos, ZV);
22436 /* If we didn't move, we've hit BEGV or ZV, so we
22437 need to signal a suitable error. */
22438 if (new_pos == PT)
22439 break;
22441 else if (BUFFERP (g->object))
22442 new_pos = g->charpos;
22443 else
22444 break;
22445 SET_PT (new_pos);
22446 w->cursor.vpos = -1;
22447 return make_number (PT);
22449 else if (ROW_GLYPH_NEWLINE_P (row, g))
22451 /* Glyphs inserted at the end of a non-empty line for
22452 positioning the cursor have zero charpos, so we must
22453 deduce the value of point by other means. */
22454 if (g->charpos > 0)
22455 SET_PT (g->charpos);
22456 else if (row->ends_at_zv_p && PT != ZV)
22457 SET_PT (ZV);
22458 else if (PT != MATRIX_ROW_END_CHARPOS (row) - 1)
22459 SET_PT (MATRIX_ROW_END_CHARPOS (row) - 1);
22460 else
22461 break;
22462 w->cursor.vpos = -1;
22463 return make_number (PT);
22466 if (g == e || NILP (g->object))
22468 if (row->truncated_on_left_p || row->truncated_on_right_p)
22469 goto simulate_display;
22470 if (!row->reversed_p)
22471 row += dir;
22472 else
22473 row -= dir;
22474 if (!(MATRIX_FIRST_TEXT_ROW (w->current_matrix) <= row
22475 && row < MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w)))
22476 goto simulate_display;
22478 if (dir > 0)
22480 if (row->reversed_p && !row->continued_p)
22482 SET_PT (MATRIX_ROW_END_CHARPOS (row) - 1);
22483 w->cursor.vpos = -1;
22484 return make_number (PT);
22486 g = row->glyphs[TEXT_AREA];
22487 e = g + row->used[TEXT_AREA];
22488 for ( ; g < e; g++)
22490 if (BUFFERP (g->object)
22491 /* Empty lines have only one glyph, which stands
22492 for the newline, and whose charpos is the
22493 buffer position of the newline. */
22494 || ROW_GLYPH_NEWLINE_P (row, g)
22495 /* When the buffer ends in a newline, the line at
22496 EOB also has one glyph, but its charpos is -1. */
22497 || (row->ends_at_zv_p
22498 && !row->reversed_p
22499 && NILP (g->object)
22500 && g->type == CHAR_GLYPH
22501 && g->u.ch == ' '))
22503 if (g->charpos > 0)
22504 SET_PT (g->charpos);
22505 else if (!row->reversed_p
22506 && row->ends_at_zv_p
22507 && PT != ZV)
22508 SET_PT (ZV);
22509 else
22510 continue;
22511 w->cursor.vpos = -1;
22512 return make_number (PT);
22516 else
22518 if (!row->reversed_p && !row->continued_p)
22520 SET_PT (MATRIX_ROW_END_CHARPOS (row) - 1);
22521 w->cursor.vpos = -1;
22522 return make_number (PT);
22524 e = row->glyphs[TEXT_AREA];
22525 g = e + row->used[TEXT_AREA] - 1;
22526 for ( ; g >= e; g--)
22528 if (BUFFERP (g->object)
22529 || (ROW_GLYPH_NEWLINE_P (row, g)
22530 && g->charpos > 0)
22531 /* Empty R2L lines on GUI frames have the buffer
22532 position of the newline stored in the stretch
22533 glyph. */
22534 || g->type == STRETCH_GLYPH
22535 || (row->ends_at_zv_p
22536 && row->reversed_p
22537 && NILP (g->object)
22538 && g->type == CHAR_GLYPH
22539 && g->u.ch == ' '))
22541 if (g->charpos > 0)
22542 SET_PT (g->charpos);
22543 else if (row->reversed_p
22544 && row->ends_at_zv_p
22545 && PT != ZV)
22546 SET_PT (ZV);
22547 else
22548 continue;
22549 w->cursor.vpos = -1;
22550 return make_number (PT);
22557 simulate_display:
22559 /* If we wind up here, we failed to move by using the glyphs, so we
22560 need to simulate display instead. */
22562 if (b)
22563 paragraph_dir = Fcurrent_bidi_paragraph_direction (w->contents);
22564 else
22565 paragraph_dir = Qleft_to_right;
22566 if (EQ (paragraph_dir, Qright_to_left))
22567 dir = -dir;
22568 if (PT <= BEGV && dir < 0)
22569 xsignal0 (Qbeginning_of_buffer);
22570 else if (PT >= ZV && dir > 0)
22571 xsignal0 (Qend_of_buffer);
22572 else
22574 struct text_pos pt;
22575 struct it it;
22576 int pt_x, target_x, pixel_width, pt_vpos;
22577 bool at_eol_p;
22578 bool overshoot_expected = false;
22579 bool target_is_eol_p = false;
22581 /* Setup the arena. */
22582 SET_TEXT_POS (pt, PT, PT_BYTE);
22583 start_display (&it, w, pt);
22584 /* When lines are truncated, we could be called with point
22585 outside of the windows edges, in which case move_it_*
22586 functions either prematurely stop at window's edge or jump to
22587 the next screen line, whereas we rely below on our ability to
22588 reach point, in order to start from its X coordinate. So we
22589 need to disregard the window's horizontal extent in that case. */
22590 if (it.line_wrap == TRUNCATE)
22591 it.last_visible_x = DISP_INFINITY;
22593 if (it.cmp_it.id < 0
22594 && it.method == GET_FROM_STRING
22595 && it.area == TEXT_AREA
22596 && it.string_from_display_prop_p
22597 && (it.sp > 0 && it.stack[it.sp - 1].method == GET_FROM_BUFFER))
22598 overshoot_expected = true;
22600 /* Find the X coordinate of point. We start from the beginning
22601 of this or previous line to make sure we are before point in
22602 the logical order (since the move_it_* functions can only
22603 move forward). */
22604 reseat:
22605 reseat_at_previous_visible_line_start (&it);
22606 it.current_x = it.hpos = it.current_y = it.vpos = 0;
22607 if (IT_CHARPOS (it) != PT)
22609 move_it_to (&it, overshoot_expected ? PT - 1 : PT,
22610 -1, -1, -1, MOVE_TO_POS);
22611 /* If we missed point because the character there is
22612 displayed out of a display vector that has more than one
22613 glyph, retry expecting overshoot. */
22614 if (it.method == GET_FROM_DISPLAY_VECTOR
22615 && it.current.dpvec_index > 0
22616 && !overshoot_expected)
22618 overshoot_expected = true;
22619 goto reseat;
22621 else if (IT_CHARPOS (it) != PT && !overshoot_expected)
22622 move_it_in_display_line (&it, PT, -1, MOVE_TO_POS);
22624 pt_x = it.current_x;
22625 pt_vpos = it.vpos;
22626 if (dir > 0 || overshoot_expected)
22628 struct glyph_row *row = it.glyph_row;
22630 /* When point is at beginning of line, we don't have
22631 information about the glyph there loaded into struct
22632 it. Calling get_next_display_element fixes that. */
22633 if (pt_x == 0)
22634 get_next_display_element (&it);
22635 at_eol_p = ITERATOR_AT_END_OF_LINE_P (&it);
22636 it.glyph_row = NULL;
22637 PRODUCE_GLYPHS (&it); /* compute it.pixel_width */
22638 it.glyph_row = row;
22639 /* PRODUCE_GLYPHS advances it.current_x, so we must restore
22640 it, lest it will become out of sync with it's buffer
22641 position. */
22642 it.current_x = pt_x;
22644 else
22645 at_eol_p = ITERATOR_AT_END_OF_LINE_P (&it);
22646 pixel_width = it.pixel_width;
22647 if (overshoot_expected && at_eol_p)
22648 pixel_width = 0;
22649 else if (pixel_width <= 0)
22650 pixel_width = 1;
22652 /* If there's a display string (or something similar) at point,
22653 we are actually at the glyph to the left of point, so we need
22654 to correct the X coordinate. */
22655 if (overshoot_expected)
22657 if (it.bidi_p)
22658 pt_x += pixel_width * it.bidi_it.scan_dir;
22659 else
22660 pt_x += pixel_width;
22663 /* Compute target X coordinate, either to the left or to the
22664 right of point. On TTY frames, all characters have the same
22665 pixel width of 1, so we can use that. On GUI frames we don't
22666 have an easy way of getting at the pixel width of the
22667 character to the left of point, so we use a different method
22668 of getting to that place. */
22669 if (dir > 0)
22670 target_x = pt_x + pixel_width;
22671 else
22672 target_x = pt_x - (!FRAME_WINDOW_P (it.f)) * pixel_width;
22674 /* Target X coordinate could be one line above or below the line
22675 of point, in which case we need to adjust the target X
22676 coordinate. Also, if moving to the left, we need to begin at
22677 the left edge of the point's screen line. */
22678 if (dir < 0)
22680 if (pt_x > 0)
22682 start_display (&it, w, pt);
22683 if (it.line_wrap == TRUNCATE)
22684 it.last_visible_x = DISP_INFINITY;
22685 reseat_at_previous_visible_line_start (&it);
22686 it.current_x = it.current_y = it.hpos = 0;
22687 if (pt_vpos != 0)
22688 move_it_by_lines (&it, pt_vpos);
22690 else
22692 move_it_by_lines (&it, -1);
22693 target_x = it.last_visible_x - !FRAME_WINDOW_P (it.f);
22694 target_is_eol_p = true;
22695 /* Under word-wrap, we don't know the x coordinate of
22696 the last character displayed on the previous line,
22697 which immediately precedes the wrap point. To find
22698 out its x coordinate, we try moving to the right
22699 margin of the window, which will stop at the wrap
22700 point, and then reset target_x to point at the
22701 character that precedes the wrap point. This is not
22702 needed on GUI frames, because (see below) there we
22703 move from the left margin one grapheme cluster at a
22704 time, and stop when we hit the wrap point. */
22705 if (!FRAME_WINDOW_P (it.f) && it.line_wrap == WORD_WRAP)
22707 void *it_data = NULL;
22708 struct it it2;
22710 SAVE_IT (it2, it, it_data);
22711 move_it_in_display_line_to (&it, ZV, target_x,
22712 MOVE_TO_POS | MOVE_TO_X);
22713 /* If we arrived at target_x, that _is_ the last
22714 character on the previous line. */
22715 if (it.current_x != target_x)
22716 target_x = it.current_x - 1;
22717 RESTORE_IT (&it, &it2, it_data);
22721 else
22723 if (at_eol_p
22724 || (target_x >= it.last_visible_x
22725 && it.line_wrap != TRUNCATE))
22727 if (pt_x > 0)
22728 move_it_by_lines (&it, 0);
22729 move_it_by_lines (&it, 1);
22730 target_x = 0;
22734 /* Move to the target X coordinate. */
22735 /* On GUI frames, as we don't know the X coordinate of the
22736 character to the left of point, moving point to the left
22737 requires walking, one grapheme cluster at a time, until we
22738 find ourself at a place immediately to the left of the
22739 character at point. */
22740 if (FRAME_WINDOW_P (it.f) && dir < 0)
22742 struct text_pos new_pos;
22743 enum move_it_result rc = MOVE_X_REACHED;
22745 if (it.current_x == 0)
22746 get_next_display_element (&it);
22747 if (it.what == IT_COMPOSITION)
22749 new_pos.charpos = it.cmp_it.charpos;
22750 new_pos.bytepos = -1;
22752 else
22753 new_pos = it.current.pos;
22755 while (it.current_x + it.pixel_width <= target_x
22756 && (rc == MOVE_X_REACHED
22757 /* Under word-wrap, move_it_in_display_line_to
22758 stops at correct coordinates, but sometimes
22759 returns MOVE_POS_MATCH_OR_ZV. */
22760 || (it.line_wrap == WORD_WRAP
22761 && rc == MOVE_POS_MATCH_OR_ZV)))
22763 int new_x = it.current_x + it.pixel_width;
22765 /* For composed characters, we want the position of the
22766 first character in the grapheme cluster (usually, the
22767 composition's base character), whereas it.current
22768 might give us the position of the _last_ one, e.g. if
22769 the composition is rendered in reverse due to bidi
22770 reordering. */
22771 if (it.what == IT_COMPOSITION)
22773 new_pos.charpos = it.cmp_it.charpos;
22774 new_pos.bytepos = -1;
22776 else
22777 new_pos = it.current.pos;
22778 if (new_x == it.current_x)
22779 new_x++;
22780 rc = move_it_in_display_line_to (&it, ZV, new_x,
22781 MOVE_TO_POS | MOVE_TO_X);
22782 if (ITERATOR_AT_END_OF_LINE_P (&it) && !target_is_eol_p)
22783 break;
22785 /* The previous position we saw in the loop is the one we
22786 want. */
22787 if (new_pos.bytepos == -1)
22788 new_pos.bytepos = CHAR_TO_BYTE (new_pos.charpos);
22789 it.current.pos = new_pos;
22791 else if (it.current_x != target_x)
22792 move_it_in_display_line_to (&it, ZV, target_x, MOVE_TO_POS | MOVE_TO_X);
22794 /* If we ended up in a display string that covers point, move to
22795 buffer position to the right in the visual order. */
22796 if (dir > 0)
22798 while (IT_CHARPOS (it) == PT)
22800 set_iterator_to_next (&it, false);
22801 if (!get_next_display_element (&it))
22802 break;
22806 /* Move point to that position. */
22807 SET_PT_BOTH (IT_CHARPOS (it), IT_BYTEPOS (it));
22810 return make_number (PT);
22812 #undef ROW_GLYPH_NEWLINE_P
22815 DEFUN ("bidi-resolved-levels", Fbidi_resolved_levels,
22816 Sbidi_resolved_levels, 0, 1, 0,
22817 doc: /* Return the resolved bidirectional levels of characters at VPOS.
22819 The resolved levels are produced by the Emacs bidi reordering engine
22820 that implements the UBA, the Unicode Bidirectional Algorithm. Please
22821 read the Unicode Standard Annex 9 (UAX#9) for background information
22822 about these levels.
22824 VPOS is the zero-based number of the current window's screen line
22825 for which to produce the resolved levels. If VPOS is nil or omitted,
22826 it defaults to the screen line of point. If the window displays a
22827 header line, VPOS of zero will report on the header line, and first
22828 line of text in the window will have VPOS of 1.
22830 Value is an array of resolved levels, indexed by glyph number.
22831 Glyphs are numbered from zero starting from the beginning of the
22832 screen line, i.e. the left edge of the window for left-to-right lines
22833 and from the right edge for right-to-left lines. The resolved levels
22834 are produced only for the window's text area; text in display margins
22835 is not included.
22837 If the selected window's display is not up-to-date, or if the specified
22838 screen line does not display text, this function returns nil. It is
22839 highly recommended to bind this function to some simple key, like F8,
22840 in order to avoid these problems.
22842 This function exists mainly for testing the correctness of the
22843 Emacs UBA implementation, in particular with the test suite. */)
22844 (Lisp_Object vpos)
22846 struct window *w = XWINDOW (selected_window);
22847 struct buffer *b = XBUFFER (w->contents);
22848 int nrow;
22849 struct glyph_row *row;
22851 if (NILP (vpos))
22853 int d1, d2, d3, d4, d5;
22855 pos_visible_p (w, PT, &d1, &d2, &d3, &d4, &d5, &nrow);
22857 else
22859 CHECK_NUMBER_COERCE_MARKER (vpos);
22860 nrow = XINT (vpos);
22863 /* We require up-to-date glyph matrix for this window. */
22864 if (w->window_end_valid
22865 && !windows_or_buffers_changed
22866 && b
22867 && !b->clip_changed
22868 && !b->prevent_redisplay_optimizations_p
22869 && !window_outdated (w)
22870 && nrow >= 0
22871 && nrow < w->current_matrix->nrows
22872 && (row = MATRIX_ROW (w->current_matrix, nrow))->enabled_p
22873 && MATRIX_ROW_DISPLAYS_TEXT_P (row))
22875 struct glyph *g, *e, *g1;
22876 int nglyphs, i;
22877 Lisp_Object levels;
22879 if (!row->reversed_p) /* Left-to-right glyph row. */
22881 g = g1 = row->glyphs[TEXT_AREA];
22882 e = g + row->used[TEXT_AREA];
22884 /* Skip over glyphs at the start of the row that was
22885 generated by redisplay for its own needs. */
22886 while (g < e
22887 && NILP (g->object)
22888 && g->charpos < 0)
22889 g++;
22890 g1 = g;
22892 /* Count the "interesting" glyphs in this row. */
22893 for (nglyphs = 0; g < e && !NILP (g->object); g++)
22894 nglyphs++;
22896 /* Create and fill the array. */
22897 levels = make_uninit_vector (nglyphs);
22898 for (i = 0; g1 < g; i++, g1++)
22899 ASET (levels, i, make_number (g1->resolved_level));
22901 else /* Right-to-left glyph row. */
22903 g = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
22904 e = row->glyphs[TEXT_AREA] - 1;
22905 while (g > e
22906 && NILP (g->object)
22907 && g->charpos < 0)
22908 g--;
22909 g1 = g;
22910 for (nglyphs = 0; g > e && !NILP (g->object); g--)
22911 nglyphs++;
22912 levels = make_uninit_vector (nglyphs);
22913 for (i = 0; g1 > g; i++, g1--)
22914 ASET (levels, i, make_number (g1->resolved_level));
22916 return levels;
22918 else
22919 return Qnil;
22924 /***********************************************************************
22925 Menu Bar
22926 ***********************************************************************/
22928 /* Redisplay the menu bar in the frame for window W.
22930 The menu bar of X frames that don't have X toolkit support is
22931 displayed in a special window W->frame->menu_bar_window.
22933 The menu bar of terminal frames is treated specially as far as
22934 glyph matrices are concerned. Menu bar lines are not part of
22935 windows, so the update is done directly on the frame matrix rows
22936 for the menu bar. */
22938 static void
22939 display_menu_bar (struct window *w)
22941 struct frame *f = XFRAME (WINDOW_FRAME (w));
22942 struct it it;
22943 Lisp_Object items;
22944 int i;
22946 /* Don't do all this for graphical frames. */
22947 #ifdef HAVE_NTGUI
22948 if (FRAME_W32_P (f))
22949 return;
22950 #endif
22951 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
22952 if (FRAME_X_P (f))
22953 return;
22954 #endif
22956 #ifdef HAVE_NS
22957 if (FRAME_NS_P (f))
22958 return;
22959 #endif /* HAVE_NS */
22961 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
22962 eassert (!FRAME_WINDOW_P (f));
22963 init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MENU_FACE_ID);
22964 it.first_visible_x = 0;
22965 it.last_visible_x = FRAME_PIXEL_WIDTH (f);
22966 #elif defined (HAVE_X_WINDOWS) /* X without toolkit. */
22967 if (FRAME_WINDOW_P (f))
22969 /* Menu bar lines are displayed in the desired matrix of the
22970 dummy window menu_bar_window. */
22971 struct window *menu_w;
22972 menu_w = XWINDOW (f->menu_bar_window);
22973 init_iterator (&it, menu_w, -1, -1, menu_w->desired_matrix->rows,
22974 MENU_FACE_ID);
22975 it.first_visible_x = 0;
22976 it.last_visible_x = FRAME_PIXEL_WIDTH (f);
22978 else
22979 #endif /* not USE_X_TOOLKIT and not USE_GTK */
22981 /* This is a TTY frame, i.e. character hpos/vpos are used as
22982 pixel x/y. */
22983 init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
22984 MENU_FACE_ID);
22985 it.first_visible_x = 0;
22986 it.last_visible_x = FRAME_COLS (f);
22989 /* FIXME: This should be controlled by a user option. See the
22990 comments in redisplay_tool_bar and display_mode_line about
22991 this. */
22992 it.paragraph_embedding = L2R;
22994 /* Clear all rows of the menu bar. */
22995 for (i = 0; i < FRAME_MENU_BAR_LINES (f); ++i)
22997 struct glyph_row *row = it.glyph_row + i;
22998 clear_glyph_row (row);
22999 row->enabled_p = true;
23000 row->full_width_p = true;
23001 row->reversed_p = false;
23004 /* Display all items of the menu bar. */
23005 items = FRAME_MENU_BAR_ITEMS (it.f);
23006 for (i = 0; i < ASIZE (items); i += 4)
23008 Lisp_Object string;
23010 /* Stop at nil string. */
23011 string = AREF (items, i + 1);
23012 if (NILP (string))
23013 break;
23015 /* Remember where item was displayed. */
23016 ASET (items, i + 3, make_number (it.hpos));
23018 /* Display the item, pad with one space. */
23019 if (it.current_x < it.last_visible_x)
23020 display_string (NULL, string, Qnil, 0, 0, &it,
23021 SCHARS (string) + 1, 0, 0, -1);
23024 /* Fill out the line with spaces. */
23025 if (it.current_x < it.last_visible_x)
23026 display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
23028 /* Compute the total height of the lines. */
23029 compute_line_metrics (&it);
23032 /* Deep copy of a glyph row, including the glyphs. */
23033 static void
23034 deep_copy_glyph_row (struct glyph_row *to, struct glyph_row *from)
23036 struct glyph *pointers[1 + LAST_AREA];
23037 int to_used = to->used[TEXT_AREA];
23039 /* Save glyph pointers of TO. */
23040 memcpy (pointers, to->glyphs, sizeof to->glyphs);
23042 /* Do a structure assignment. */
23043 *to = *from;
23045 /* Restore original glyph pointers of TO. */
23046 memcpy (to->glyphs, pointers, sizeof to->glyphs);
23048 /* Copy the glyphs. */
23049 memcpy (to->glyphs[TEXT_AREA], from->glyphs[TEXT_AREA],
23050 min (from->used[TEXT_AREA], to_used) * sizeof (struct glyph));
23052 /* If we filled only part of the TO row, fill the rest with
23053 space_glyph (which will display as empty space). */
23054 if (to_used > from->used[TEXT_AREA])
23055 fill_up_frame_row_with_spaces (to, to_used);
23058 /* Display one menu item on a TTY, by overwriting the glyphs in the
23059 frame F's desired glyph matrix with glyphs produced from the menu
23060 item text. Called from term.c to display TTY drop-down menus one
23061 item at a time.
23063 ITEM_TEXT is the menu item text as a C string.
23065 FACE_ID is the face ID to be used for this menu item. FACE_ID
23066 could specify one of 3 faces: a face for an enabled item, a face
23067 for a disabled item, or a face for a selected item.
23069 X and Y are coordinates of the first glyph in the frame's desired
23070 matrix to be overwritten by the menu item. Since this is a TTY, Y
23071 is the zero-based number of the glyph row and X is the zero-based
23072 glyph number in the row, starting from left, where to start
23073 displaying the item.
23075 SUBMENU means this menu item drops down a submenu, which
23076 should be indicated by displaying a proper visual cue after the
23077 item text. */
23079 void
23080 display_tty_menu_item (const char *item_text, int width, int face_id,
23081 int x, int y, bool submenu)
23083 struct it it;
23084 struct frame *f = SELECTED_FRAME ();
23085 struct window *w = XWINDOW (f->selected_window);
23086 struct glyph_row *row;
23087 size_t item_len = strlen (item_text);
23089 eassert (FRAME_TERMCAP_P (f));
23091 /* Don't write beyond the matrix's last row. This can happen for
23092 TTY screens that are not high enough to show the entire menu.
23093 (This is actually a bit of defensive programming, as
23094 tty_menu_display already limits the number of menu items to one
23095 less than the number of screen lines.) */
23096 if (y >= f->desired_matrix->nrows)
23097 return;
23099 init_iterator (&it, w, -1, -1, f->desired_matrix->rows + y, MENU_FACE_ID);
23100 it.first_visible_x = 0;
23101 it.last_visible_x = FRAME_COLS (f) - 1;
23102 row = it.glyph_row;
23103 /* Start with the row contents from the current matrix. */
23104 deep_copy_glyph_row (row, f->current_matrix->rows + y);
23105 bool saved_width = row->full_width_p;
23106 row->full_width_p = true;
23107 bool saved_reversed = row->reversed_p;
23108 row->reversed_p = false;
23109 row->enabled_p = true;
23111 /* Arrange for the menu item glyphs to start at (X,Y) and have the
23112 desired face. */
23113 eassert (x < f->desired_matrix->matrix_w);
23114 it.current_x = it.hpos = x;
23115 it.current_y = it.vpos = y;
23116 int saved_used = row->used[TEXT_AREA];
23117 bool saved_truncated = row->truncated_on_right_p;
23118 row->used[TEXT_AREA] = x;
23119 it.face_id = face_id;
23120 it.line_wrap = TRUNCATE;
23122 /* FIXME: This should be controlled by a user option. See the
23123 comments in redisplay_tool_bar and display_mode_line about this.
23124 Also, if paragraph_embedding could ever be R2L, changes will be
23125 needed to avoid shifting to the right the row characters in
23126 term.c:append_glyph. */
23127 it.paragraph_embedding = L2R;
23129 /* Pad with a space on the left. */
23130 display_string (" ", Qnil, Qnil, 0, 0, &it, 1, 0, FRAME_COLS (f) - 1, -1);
23131 width--;
23132 /* Display the menu item, pad with spaces to WIDTH. */
23133 if (submenu)
23135 display_string (item_text, Qnil, Qnil, 0, 0, &it,
23136 item_len, 0, FRAME_COLS (f) - 1, -1);
23137 width -= item_len;
23138 /* Indicate with " >" that there's a submenu. */
23139 display_string (" >", Qnil, Qnil, 0, 0, &it, width, 0,
23140 FRAME_COLS (f) - 1, -1);
23142 else
23143 display_string (item_text, Qnil, Qnil, 0, 0, &it,
23144 width, 0, FRAME_COLS (f) - 1, -1);
23146 row->used[TEXT_AREA] = max (saved_used, row->used[TEXT_AREA]);
23147 row->truncated_on_right_p = saved_truncated;
23148 row->hash = row_hash (row);
23149 row->full_width_p = saved_width;
23150 row->reversed_p = saved_reversed;
23153 /***********************************************************************
23154 Mode Line
23155 ***********************************************************************/
23157 /* Redisplay mode lines in the window tree whose root is WINDOW.
23158 If FORCE, redisplay mode lines unconditionally.
23159 Otherwise, redisplay only mode lines that are garbaged. Value is
23160 the number of windows whose mode lines were redisplayed. */
23162 static int
23163 redisplay_mode_lines (Lisp_Object window, bool force)
23165 int nwindows = 0;
23167 while (!NILP (window))
23169 struct window *w = XWINDOW (window);
23171 if (WINDOWP (w->contents))
23172 nwindows += redisplay_mode_lines (w->contents, force);
23173 else if (force
23174 || FRAME_GARBAGED_P (XFRAME (w->frame))
23175 || !MATRIX_MODE_LINE_ROW (w->current_matrix)->enabled_p)
23177 struct text_pos lpoint;
23178 struct buffer *old = current_buffer;
23180 /* Set the window's buffer for the mode line display. */
23181 SET_TEXT_POS (lpoint, PT, PT_BYTE);
23182 set_buffer_internal_1 (XBUFFER (w->contents));
23184 /* Point refers normally to the selected window. For any
23185 other window, set up appropriate value. */
23186 if (!EQ (window, selected_window))
23188 struct text_pos pt;
23190 CLIP_TEXT_POS_FROM_MARKER (pt, w->pointm);
23191 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
23194 /* Display mode lines. */
23195 clear_glyph_matrix (w->desired_matrix);
23196 if (display_mode_lines (w))
23197 ++nwindows;
23199 /* Restore old settings. */
23200 set_buffer_internal_1 (old);
23201 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
23204 window = w->next;
23207 return nwindows;
23211 /* Display the mode and/or header line of window W. Value is the
23212 sum number of mode lines and header lines displayed. */
23214 static int
23215 display_mode_lines (struct window *w)
23217 Lisp_Object old_selected_window = selected_window;
23218 Lisp_Object old_selected_frame = selected_frame;
23219 Lisp_Object new_frame = w->frame;
23220 Lisp_Object old_frame_selected_window = XFRAME (new_frame)->selected_window;
23221 int n = 0;
23223 selected_frame = new_frame;
23224 /* FIXME: If we were to allow the mode-line's computation changing the buffer
23225 or window's point, then we'd need select_window_1 here as well. */
23226 XSETWINDOW (selected_window, w);
23227 XFRAME (new_frame)->selected_window = selected_window;
23229 /* These will be set while the mode line specs are processed. */
23230 line_number_displayed = false;
23231 w->column_number_displayed = -1;
23233 if (window_wants_mode_line (w))
23235 Lisp_Object window_mode_line_format
23236 = window_parameter (w, Qmode_line_format);
23238 struct window *sel_w = XWINDOW (old_selected_window);
23240 /* Select mode line face based on the real selected window. */
23241 display_mode_line (w, CURRENT_MODE_LINE_FACE_ID_3 (sel_w, sel_w, w),
23242 NILP (window_mode_line_format)
23243 ? BVAR (current_buffer, mode_line_format)
23244 : window_mode_line_format);
23245 ++n;
23248 if (window_wants_header_line (w))
23250 Lisp_Object window_header_line_format
23251 = window_parameter (w, Qheader_line_format);
23253 display_mode_line (w, HEADER_LINE_FACE_ID,
23254 NILP (window_header_line_format)
23255 ? BVAR (current_buffer, header_line_format)
23256 : window_header_line_format);
23257 ++n;
23260 XFRAME (new_frame)->selected_window = old_frame_selected_window;
23261 selected_frame = old_selected_frame;
23262 selected_window = old_selected_window;
23263 if (n > 0)
23264 w->must_be_updated_p = true;
23265 return n;
23269 /* Display mode or header line of window W. FACE_ID specifies which
23270 line to display; it is either MODE_LINE_FACE_ID or
23271 HEADER_LINE_FACE_ID. FORMAT is the mode/header line format to
23272 display. Value is the pixel height of the mode/header line
23273 displayed. */
23275 static int
23276 display_mode_line (struct window *w, enum face_id face_id, Lisp_Object format)
23278 struct it it;
23279 struct face *face;
23280 ptrdiff_t count = SPECPDL_INDEX ();
23282 init_iterator (&it, w, -1, -1, NULL, face_id);
23283 /* Don't extend on a previously drawn mode-line.
23284 This may happen if called from pos_visible_p. */
23285 it.glyph_row->enabled_p = false;
23286 prepare_desired_row (w, it.glyph_row, true);
23288 it.glyph_row->mode_line_p = true;
23290 /* FIXME: This should be controlled by a user option. But
23291 supporting such an option is not trivial, since the mode line is
23292 made up of many separate strings. */
23293 it.paragraph_embedding = L2R;
23295 record_unwind_protect (unwind_format_mode_line,
23296 format_mode_line_unwind_data (NULL, NULL,
23297 Qnil, false));
23299 mode_line_target = MODE_LINE_DISPLAY;
23301 /* Temporarily make frame's keyboard the current kboard so that
23302 kboard-local variables in the mode_line_format will get the right
23303 values. */
23304 push_kboard (FRAME_KBOARD (it.f));
23305 record_unwind_save_match_data ();
23306 display_mode_element (&it, 0, 0, 0, format, Qnil, false);
23307 pop_kboard ();
23309 unbind_to (count, Qnil);
23311 /* Fill up with spaces. */
23312 display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);
23314 compute_line_metrics (&it);
23315 it.glyph_row->full_width_p = true;
23316 it.glyph_row->continued_p = false;
23317 it.glyph_row->truncated_on_left_p = false;
23318 it.glyph_row->truncated_on_right_p = false;
23320 /* Make a 3D mode-line have a shadow at its right end. */
23321 face = FACE_FROM_ID (it.f, face_id);
23322 extend_face_to_end_of_line (&it);
23323 if (face->box != FACE_NO_BOX)
23325 struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
23326 + it.glyph_row->used[TEXT_AREA] - 1);
23327 last->right_box_line_p = true;
23330 return it.glyph_row->height;
23333 /* Move element ELT in LIST to the front of LIST.
23334 Return the updated list. */
23336 static Lisp_Object
23337 move_elt_to_front (Lisp_Object elt, Lisp_Object list)
23339 register Lisp_Object tail, prev;
23340 register Lisp_Object tem;
23342 tail = list;
23343 prev = Qnil;
23344 while (CONSP (tail))
23346 tem = XCAR (tail);
23348 if (EQ (elt, tem))
23350 /* Splice out the link TAIL. */
23351 if (NILP (prev))
23352 list = XCDR (tail);
23353 else
23354 Fsetcdr (prev, XCDR (tail));
23356 /* Now make it the first. */
23357 Fsetcdr (tail, list);
23358 return tail;
23360 else
23361 prev = tail;
23362 tail = XCDR (tail);
23363 maybe_quit ();
23366 /* Not found--return unchanged LIST. */
23367 return list;
23370 /* Contribute ELT to the mode line for window IT->w. How it
23371 translates into text depends on its data type.
23373 IT describes the display environment in which we display, as usual.
23375 DEPTH is the depth in recursion. It is used to prevent
23376 infinite recursion here.
23378 FIELD_WIDTH is the number of characters the display of ELT should
23379 occupy in the mode line, and PRECISION is the maximum number of
23380 characters to display from ELT's representation. See
23381 display_string for details.
23383 Returns the hpos of the end of the text generated by ELT.
23385 PROPS is a property list to add to any string we encounter.
23387 If RISKY, remove (disregard) any properties in any string
23388 we encounter, and ignore :eval and :propertize.
23390 The global variable `mode_line_target' determines whether the
23391 output is passed to `store_mode_line_noprop',
23392 `store_mode_line_string', or `display_string'. */
23394 static int
23395 display_mode_element (struct it *it, int depth, int field_width, int precision,
23396 Lisp_Object elt, Lisp_Object props, bool risky)
23398 int n = 0, field, prec;
23399 bool literal = false;
23401 tail_recurse:
23402 if (depth > 100)
23403 elt = build_string ("*too-deep*");
23405 depth++;
23407 switch (XTYPE (elt))
23409 case Lisp_String:
23411 /* A string: output it and check for %-constructs within it. */
23412 unsigned char c;
23413 ptrdiff_t offset = 0;
23415 if (SCHARS (elt) > 0
23416 && (!NILP (props) || risky))
23418 Lisp_Object oprops, aelt;
23419 oprops = Ftext_properties_at (make_number (0), elt);
23421 /* If the starting string's properties are not what
23422 we want, translate the string. Also, if the string
23423 is risky, do that anyway. */
23425 if (NILP (Fequal (props, oprops)) || risky)
23427 /* If the starting string has properties,
23428 merge the specified ones onto the existing ones. */
23429 if (! NILP (oprops) && !risky)
23431 Lisp_Object tem;
23433 oprops = Fcopy_sequence (oprops);
23434 tem = props;
23435 while (CONSP (tem))
23437 oprops = Fplist_put (oprops, XCAR (tem),
23438 XCAR (XCDR (tem)));
23439 tem = XCDR (XCDR (tem));
23441 props = oprops;
23444 aelt = Fassoc (elt, mode_line_proptrans_alist, Qnil);
23445 if (! NILP (aelt) && !NILP (Fequal (props, XCDR (aelt))))
23447 /* AELT is what we want. Move it to the front
23448 without consing. */
23449 elt = XCAR (aelt);
23450 mode_line_proptrans_alist
23451 = move_elt_to_front (aelt, mode_line_proptrans_alist);
23453 else
23455 Lisp_Object tem;
23457 /* If AELT has the wrong props, it is useless.
23458 so get rid of it. */
23459 if (! NILP (aelt))
23460 mode_line_proptrans_alist
23461 = Fdelq (aelt, mode_line_proptrans_alist);
23463 elt = Fcopy_sequence (elt);
23464 Fset_text_properties (make_number (0), Flength (elt),
23465 props, elt);
23466 /* Add this item to mode_line_proptrans_alist. */
23467 mode_line_proptrans_alist
23468 = Fcons (Fcons (elt, props),
23469 mode_line_proptrans_alist);
23470 /* Truncate mode_line_proptrans_alist
23471 to at most 50 elements. */
23472 tem = Fnthcdr (make_number (50),
23473 mode_line_proptrans_alist);
23474 if (! NILP (tem))
23475 XSETCDR (tem, Qnil);
23480 offset = 0;
23482 if (literal)
23484 prec = precision - n;
23485 switch (mode_line_target)
23487 case MODE_LINE_NOPROP:
23488 case MODE_LINE_TITLE:
23489 n += store_mode_line_noprop (SSDATA (elt), -1, prec);
23490 break;
23491 case MODE_LINE_STRING:
23492 n += store_mode_line_string (NULL, elt, true, 0, prec, Qnil);
23493 break;
23494 case MODE_LINE_DISPLAY:
23495 n += display_string (NULL, elt, Qnil, 0, 0, it,
23496 0, prec, 0, STRING_MULTIBYTE (elt));
23497 break;
23500 break;
23503 /* Handle the non-literal case. */
23505 while ((precision <= 0 || n < precision)
23506 && SREF (elt, offset) != 0
23507 && (mode_line_target != MODE_LINE_DISPLAY
23508 || it->current_x < it->last_visible_x))
23510 ptrdiff_t last_offset = offset;
23512 /* Advance to end of string or next format specifier. */
23513 while ((c = SREF (elt, offset++)) != '\0' && c != '%')
23516 if (offset - 1 != last_offset)
23518 ptrdiff_t nchars, nbytes;
23520 /* Output to end of string or up to '%'. Field width
23521 is length of string. Don't output more than
23522 PRECISION allows us. */
23523 offset--;
23525 prec = c_string_width (SDATA (elt) + last_offset,
23526 offset - last_offset, precision - n,
23527 &nchars, &nbytes);
23529 switch (mode_line_target)
23531 case MODE_LINE_NOPROP:
23532 case MODE_LINE_TITLE:
23533 n += store_mode_line_noprop (SSDATA (elt) + last_offset, 0, prec);
23534 break;
23535 case MODE_LINE_STRING:
23537 ptrdiff_t bytepos = last_offset;
23538 ptrdiff_t charpos = string_byte_to_char (elt, bytepos);
23539 ptrdiff_t endpos = (precision <= 0
23540 ? string_byte_to_char (elt, offset)
23541 : charpos + nchars);
23542 Lisp_Object mode_string
23543 = Fsubstring (elt, make_number (charpos),
23544 make_number (endpos));
23545 n += store_mode_line_string (NULL, mode_string, false,
23546 0, 0, Qnil);
23548 break;
23549 case MODE_LINE_DISPLAY:
23551 ptrdiff_t bytepos = last_offset;
23552 ptrdiff_t charpos = string_byte_to_char (elt, bytepos);
23554 if (precision <= 0)
23555 nchars = string_byte_to_char (elt, offset) - charpos;
23556 n += display_string (NULL, elt, Qnil, 0, charpos,
23557 it, 0, nchars, 0,
23558 STRING_MULTIBYTE (elt));
23560 break;
23563 else /* c == '%' */
23565 ptrdiff_t percent_position = offset;
23567 /* Get the specified minimum width. Zero means
23568 don't pad. */
23569 field = 0;
23570 while ((c = SREF (elt, offset++)) >= '0' && c <= '9')
23571 field = field * 10 + c - '0';
23573 /* Don't pad beyond the total padding allowed. */
23574 if (field_width - n > 0 && field > field_width - n)
23575 field = field_width - n;
23577 /* Note that either PRECISION <= 0 or N < PRECISION. */
23578 prec = precision - n;
23580 if (c == 'M')
23581 n += display_mode_element (it, depth, field, prec,
23582 Vglobal_mode_string, props,
23583 risky);
23584 else if (c != 0)
23586 bool multibyte;
23587 ptrdiff_t bytepos, charpos;
23588 const char *spec;
23589 Lisp_Object string;
23591 bytepos = percent_position;
23592 charpos = (STRING_MULTIBYTE (elt)
23593 ? string_byte_to_char (elt, bytepos)
23594 : bytepos);
23595 spec = decode_mode_spec (it->w, c, field, &string);
23596 multibyte = STRINGP (string) && STRING_MULTIBYTE (string);
23598 switch (mode_line_target)
23600 case MODE_LINE_NOPROP:
23601 case MODE_LINE_TITLE:
23602 n += store_mode_line_noprop (spec, field, prec);
23603 break;
23604 case MODE_LINE_STRING:
23606 Lisp_Object tem = build_string (spec);
23607 props = Ftext_properties_at (make_number (charpos), elt);
23608 /* Should only keep face property in props */
23609 n += store_mode_line_string (NULL, tem, false,
23610 field, prec, props);
23612 break;
23613 case MODE_LINE_DISPLAY:
23615 int nglyphs_before, nwritten;
23617 nglyphs_before = it->glyph_row->used[TEXT_AREA];
23618 nwritten = display_string (spec, string, elt,
23619 charpos, 0, it,
23620 field, prec, 0,
23621 multibyte);
23623 /* Assign to the glyphs written above the
23624 string where the `%x' came from, position
23625 of the `%'. */
23626 if (nwritten > 0)
23628 struct glyph *glyph
23629 = (it->glyph_row->glyphs[TEXT_AREA]
23630 + nglyphs_before);
23631 int i;
23633 for (i = 0; i < nwritten; ++i)
23635 glyph[i].object = elt;
23636 glyph[i].charpos = charpos;
23639 n += nwritten;
23642 break;
23645 else /* c == 0 */
23646 break;
23650 break;
23652 case Lisp_Symbol:
23653 /* A symbol: process the value of the symbol recursively
23654 as if it appeared here directly. Avoid error if symbol void.
23655 Special case: if value of symbol is a string, output the string
23656 literally. */
23658 register Lisp_Object tem;
23660 /* If the variable is not marked as risky to set
23661 then its contents are risky to use. */
23662 if (NILP (Fget (elt, Qrisky_local_variable)))
23663 risky = true;
23665 tem = Fboundp (elt);
23666 if (!NILP (tem))
23668 tem = Fsymbol_value (elt);
23669 /* If value is a string, output that string literally:
23670 don't check for % within it. */
23671 if (STRINGP (tem))
23672 literal = true;
23674 if (!EQ (tem, elt))
23676 /* Give up right away for nil or t. */
23677 elt = tem;
23678 goto tail_recurse;
23682 break;
23684 case Lisp_Cons:
23686 register Lisp_Object car, tem;
23688 /* A cons cell: five distinct cases.
23689 If first element is :eval or :propertize, do something special.
23690 If first element is a string or a cons, process all the elements
23691 and effectively concatenate them.
23692 If first element is a negative number, truncate displaying cdr to
23693 at most that many characters. If positive, pad (with spaces)
23694 to at least that many characters.
23695 If first element is a symbol, process the cadr or caddr recursively
23696 according to whether the symbol's value is non-nil or nil. */
23697 car = XCAR (elt);
23698 if (EQ (car, QCeval))
23700 /* An element of the form (:eval FORM) means evaluate FORM
23701 and use the result as mode line elements. */
23703 if (risky)
23704 break;
23706 if (CONSP (XCDR (elt)))
23708 Lisp_Object spec;
23709 spec = safe__eval (true, XCAR (XCDR (elt)));
23710 /* The :eval form could delete the frame stored in the
23711 iterator, which will cause a crash if we try to
23712 access faces and other fields (e.g., FRAME_KBOARD)
23713 on that frame. This is a nonsensical thing to do,
23714 and signaling an error from redisplay might be
23715 dangerous, but we cannot continue with an invalid frame. */
23716 if (!FRAME_LIVE_P (it->f))
23717 signal_error (":eval deleted the frame being displayed", elt);
23718 n += display_mode_element (it, depth, field_width - n,
23719 precision - n, spec, props,
23720 risky);
23723 else if (EQ (car, QCpropertize))
23725 /* An element of the form (:propertize ELT PROPS...)
23726 means display ELT but applying properties PROPS. */
23728 if (risky)
23729 break;
23731 if (CONSP (XCDR (elt)))
23732 n += display_mode_element (it, depth, field_width - n,
23733 precision - n, XCAR (XCDR (elt)),
23734 XCDR (XCDR (elt)), risky);
23736 else if (SYMBOLP (car))
23738 tem = Fboundp (car);
23739 elt = XCDR (elt);
23740 if (!CONSP (elt))
23741 goto invalid;
23742 /* elt is now the cdr, and we know it is a cons cell.
23743 Use its car if CAR has a non-nil value. */
23744 if (!NILP (tem))
23746 tem = Fsymbol_value (car);
23747 if (!NILP (tem))
23749 elt = XCAR (elt);
23750 goto tail_recurse;
23753 /* Symbol's value is nil (or symbol is unbound)
23754 Get the cddr of the original list
23755 and if possible find the caddr and use that. */
23756 elt = XCDR (elt);
23757 if (NILP (elt))
23758 break;
23759 else if (!CONSP (elt))
23760 goto invalid;
23761 elt = XCAR (elt);
23762 goto tail_recurse;
23764 else if (INTEGERP (car))
23766 register int lim = XINT (car);
23767 elt = XCDR (elt);
23768 if (lim < 0)
23770 /* Negative int means reduce maximum width. */
23771 if (precision <= 0)
23772 precision = -lim;
23773 else
23774 precision = min (precision, -lim);
23776 else if (lim > 0)
23778 /* Padding specified. Don't let it be more than
23779 current maximum. */
23780 if (precision > 0)
23781 lim = min (precision, lim);
23783 /* If that's more padding than already wanted, queue it.
23784 But don't reduce padding already specified even if
23785 that is beyond the current truncation point. */
23786 field_width = max (lim, field_width);
23788 goto tail_recurse;
23790 else if (STRINGP (car) || CONSP (car))
23791 FOR_EACH_TAIL_SAFE (elt)
23793 if (0 < precision && precision <= n)
23794 break;
23795 n += display_mode_element (it, depth,
23796 /* Pad after only the last
23797 list element. */
23798 (! CONSP (XCDR (elt))
23799 ? field_width - n
23800 : 0),
23801 precision - n, XCAR (elt),
23802 props, risky);
23805 break;
23807 default:
23808 invalid:
23809 elt = build_string ("*invalid*");
23810 goto tail_recurse;
23813 /* Pad to FIELD_WIDTH. */
23814 if (field_width > 0 && n < field_width)
23816 switch (mode_line_target)
23818 case MODE_LINE_NOPROP:
23819 case MODE_LINE_TITLE:
23820 n += store_mode_line_noprop ("", field_width - n, 0);
23821 break;
23822 case MODE_LINE_STRING:
23823 n += store_mode_line_string ("", Qnil, false, field_width - n, 0,
23824 Qnil);
23825 break;
23826 case MODE_LINE_DISPLAY:
23827 n += display_string ("", Qnil, Qnil, 0, 0, it, field_width - n,
23828 0, 0, 0);
23829 break;
23833 return n;
23836 /* Store a mode-line string element in mode_line_string_list.
23838 If STRING is non-null, display that C string. Otherwise, the Lisp
23839 string LISP_STRING is displayed.
23841 FIELD_WIDTH is the minimum number of output glyphs to produce.
23842 If STRING has fewer characters than FIELD_WIDTH, pad to the right
23843 with spaces. FIELD_WIDTH <= 0 means don't pad.
23845 PRECISION is the maximum number of characters to output from
23846 STRING. PRECISION <= 0 means don't truncate the string.
23848 If COPY_STRING, make a copy of LISP_STRING before adding
23849 properties to the string.
23851 PROPS are the properties to add to the string.
23852 The mode_line_string_face face property is always added to the string.
23855 static int
23856 store_mode_line_string (const char *string, Lisp_Object lisp_string,
23857 bool copy_string,
23858 int field_width, int precision, Lisp_Object props)
23860 ptrdiff_t len;
23861 int n = 0;
23863 if (string != NULL)
23865 len = strlen (string);
23866 if (precision > 0 && len > precision)
23867 len = precision;
23868 lisp_string = make_string (string, len);
23869 if (NILP (props))
23870 props = mode_line_string_face_prop;
23871 else if (!NILP (mode_line_string_face))
23873 Lisp_Object face = Fplist_get (props, Qface);
23874 props = Fcopy_sequence (props);
23875 if (NILP (face))
23876 face = mode_line_string_face;
23877 else
23878 face = list2 (face, mode_line_string_face);
23879 props = Fplist_put (props, Qface, face);
23881 Fadd_text_properties (make_number (0), make_number (len),
23882 props, lisp_string);
23884 else
23886 len = XFASTINT (Flength (lisp_string));
23887 if (precision > 0 && len > precision)
23889 len = precision;
23890 lisp_string = Fsubstring (lisp_string, make_number (0), make_number (len));
23891 precision = -1;
23893 if (!NILP (mode_line_string_face))
23895 Lisp_Object face;
23896 if (NILP (props))
23897 props = Ftext_properties_at (make_number (0), lisp_string);
23898 face = Fplist_get (props, Qface);
23899 if (NILP (face))
23900 face = mode_line_string_face;
23901 else
23902 face = list2 (face, mode_line_string_face);
23903 props = list2 (Qface, face);
23904 if (copy_string)
23905 lisp_string = Fcopy_sequence (lisp_string);
23907 if (!NILP (props))
23908 Fadd_text_properties (make_number (0), make_number (len),
23909 props, lisp_string);
23912 if (len > 0)
23914 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
23915 n += len;
23918 if (field_width > len)
23920 field_width -= len;
23921 lisp_string = Fmake_string (make_number (field_width), make_number (' '));
23922 if (!NILP (props))
23923 Fadd_text_properties (make_number (0), make_number (field_width),
23924 props, lisp_string);
23925 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
23926 n += field_width;
23929 return n;
23933 DEFUN ("format-mode-line", Fformat_mode_line, Sformat_mode_line,
23934 1, 4, 0,
23935 doc: /* Format a string out of a mode line format specification.
23936 First arg FORMAT specifies the mode line format (see `mode-line-format'
23937 for details) to use.
23939 By default, the format is evaluated for the currently selected window.
23941 Optional second arg FACE specifies the face property to put on all
23942 characters for which no face is specified. The value nil means the
23943 default face. The value t means whatever face the window's mode line
23944 currently uses (either `mode-line' or `mode-line-inactive',
23945 depending on whether the window is the selected window or not).
23946 An integer value means the value string has no text
23947 properties.
23949 Optional third and fourth args WINDOW and BUFFER specify the window
23950 and buffer to use as the context for the formatting (defaults
23951 are the selected window and the WINDOW's buffer). */)
23952 (Lisp_Object format, Lisp_Object face,
23953 Lisp_Object window, Lisp_Object buffer)
23955 struct it it;
23956 int len;
23957 struct window *w;
23958 struct buffer *old_buffer = NULL;
23959 int face_id;
23960 bool no_props = INTEGERP (face);
23961 ptrdiff_t count = SPECPDL_INDEX ();
23962 Lisp_Object str;
23963 int string_start = 0;
23965 w = decode_any_window (window);
23966 XSETWINDOW (window, w);
23968 if (NILP (buffer))
23969 buffer = w->contents;
23970 CHECK_BUFFER (buffer);
23972 /* Make formatting the modeline a non-op when noninteractive, otherwise
23973 there will be problems later caused by a partially initialized frame. */
23974 if (NILP (format) || noninteractive)
23975 return empty_unibyte_string;
23977 if (no_props)
23978 face = Qnil;
23980 face_id = (NILP (face) || EQ (face, Qdefault)) ? DEFAULT_FACE_ID
23981 : EQ (face, Qt) ? (EQ (window, selected_window)
23982 ? MODE_LINE_FACE_ID : MODE_LINE_INACTIVE_FACE_ID)
23983 : EQ (face, Qmode_line) ? MODE_LINE_FACE_ID
23984 : EQ (face, Qmode_line_inactive) ? MODE_LINE_INACTIVE_FACE_ID
23985 : EQ (face, Qheader_line) ? HEADER_LINE_FACE_ID
23986 : EQ (face, Qtool_bar) ? TOOL_BAR_FACE_ID
23987 : DEFAULT_FACE_ID;
23989 old_buffer = current_buffer;
23991 /* Save things including mode_line_proptrans_alist,
23992 and set that to nil so that we don't alter the outer value. */
23993 record_unwind_protect (unwind_format_mode_line,
23994 format_mode_line_unwind_data
23995 (XFRAME (WINDOW_FRAME (w)),
23996 old_buffer, selected_window, true));
23997 mode_line_proptrans_alist = Qnil;
23999 Fselect_window (window, Qt);
24000 set_buffer_internal_1 (XBUFFER (buffer));
24002 init_iterator (&it, w, -1, -1, NULL, face_id);
24004 if (no_props)
24006 mode_line_target = MODE_LINE_NOPROP;
24007 mode_line_string_face_prop = Qnil;
24008 mode_line_string_list = Qnil;
24009 string_start = MODE_LINE_NOPROP_LEN (0);
24011 else
24013 mode_line_target = MODE_LINE_STRING;
24014 mode_line_string_list = Qnil;
24015 mode_line_string_face = face;
24016 mode_line_string_face_prop
24017 = NILP (face) ? Qnil : list2 (Qface, face);
24020 push_kboard (FRAME_KBOARD (it.f));
24021 display_mode_element (&it, 0, 0, 0, format, Qnil, false);
24022 pop_kboard ();
24024 if (no_props)
24026 len = MODE_LINE_NOPROP_LEN (string_start);
24027 str = make_string (mode_line_noprop_buf + string_start, len);
24029 else
24031 mode_line_string_list = Fnreverse (mode_line_string_list);
24032 str = Fmapconcat (Qidentity, mode_line_string_list,
24033 empty_unibyte_string);
24036 unbind_to (count, Qnil);
24037 return str;
24040 /* Write a null-terminated, right justified decimal representation of
24041 the positive integer D to BUF using a minimal field width WIDTH. */
24043 static void
24044 pint2str (register char *buf, register int width, register ptrdiff_t d)
24046 register char *p = buf;
24048 if (d <= 0)
24049 *p++ = '0';
24050 else
24052 while (d > 0)
24054 *p++ = d % 10 + '0';
24055 d /= 10;
24059 for (width -= (int) (p - buf); width > 0; --width)
24060 *p++ = ' ';
24061 *p-- = '\0';
24062 while (p > buf)
24064 d = *buf;
24065 *buf++ = *p;
24066 *p-- = d;
24070 /* Write a null-terminated, right justified decimal and "human
24071 readable" representation of the nonnegative integer D to BUF using
24072 a minimal field width WIDTH. D should be smaller than 999.5e24. */
24074 static const char power_letter[] =
24076 0, /* no letter */
24077 'k', /* kilo */
24078 'M', /* mega */
24079 'G', /* giga */
24080 'T', /* tera */
24081 'P', /* peta */
24082 'E', /* exa */
24083 'Z', /* zetta */
24084 'Y' /* yotta */
24087 static void
24088 pint2hrstr (char *buf, int width, ptrdiff_t d)
24090 /* We aim to represent the nonnegative integer D as
24091 QUOTIENT.TENTHS * 10 ^ (3 * EXPONENT). */
24092 ptrdiff_t quotient = d;
24093 int remainder = 0;
24094 /* -1 means: do not use TENTHS. */
24095 int tenths = -1;
24096 int exponent = 0;
24098 /* Length of QUOTIENT.TENTHS as a string. */
24099 int length;
24101 char * psuffix;
24102 char * p;
24104 if (quotient >= 1000)
24106 /* Scale to the appropriate EXPONENT. */
24109 remainder = quotient % 1000;
24110 quotient /= 1000;
24111 exponent++;
24113 while (quotient >= 1000);
24115 /* Round to nearest and decide whether to use TENTHS or not. */
24116 if (quotient <= 9)
24118 tenths = remainder / 100;
24119 if (remainder % 100 >= 50)
24121 if (tenths < 9)
24122 tenths++;
24123 else
24125 quotient++;
24126 if (quotient == 10)
24127 tenths = -1;
24128 else
24129 tenths = 0;
24133 else
24134 if (remainder >= 500)
24136 if (quotient < 999)
24137 quotient++;
24138 else
24140 quotient = 1;
24141 exponent++;
24142 tenths = 0;
24147 /* Calculate the LENGTH of QUOTIENT.TENTHS as a string. */
24148 if (tenths == -1 && quotient <= 99)
24149 if (quotient <= 9)
24150 length = 1;
24151 else
24152 length = 2;
24153 else
24154 length = 3;
24155 p = psuffix = buf + max (width, length);
24157 /* Print EXPONENT. */
24158 *psuffix++ = power_letter[exponent];
24159 *psuffix = '\0';
24161 /* Print TENTHS. */
24162 if (tenths >= 0)
24164 *--p = '0' + tenths;
24165 *--p = '.';
24168 /* Print QUOTIENT. */
24171 int digit = quotient % 10;
24172 *--p = '0' + digit;
24174 while ((quotient /= 10) != 0);
24176 /* Print leading spaces. */
24177 while (buf < p)
24178 *--p = ' ';
24181 /* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
24182 If EOL_FLAG, set also a mnemonic character for end-of-line
24183 type of CODING_SYSTEM. Return updated pointer into BUF. */
24185 static unsigned char invalid_eol_type[] = "(*invalid*)";
24187 static char *
24188 decode_mode_spec_coding (Lisp_Object coding_system, char *buf, bool eol_flag)
24190 Lisp_Object val;
24191 bool multibyte = !NILP (BVAR (current_buffer, enable_multibyte_characters));
24192 const unsigned char *eol_str;
24193 int eol_str_len;
24194 /* The EOL conversion we are using. */
24195 Lisp_Object eoltype;
24197 val = CODING_SYSTEM_SPEC (coding_system);
24198 eoltype = Qnil;
24200 if (!VECTORP (val)) /* Not yet decided. */
24202 *buf++ = multibyte ? '-' : ' ';
24203 if (eol_flag)
24204 eoltype = eol_mnemonic_undecided;
24205 /* Don't mention EOL conversion if it isn't decided. */
24207 else
24209 Lisp_Object attrs;
24210 Lisp_Object eolvalue;
24212 attrs = AREF (val, 0);
24213 eolvalue = AREF (val, 2);
24215 *buf++ = multibyte
24216 ? XFASTINT (CODING_ATTR_MNEMONIC (attrs))
24217 : ' ';
24219 if (eol_flag)
24221 /* The EOL conversion that is normal on this system. */
24223 if (NILP (eolvalue)) /* Not yet decided. */
24224 eoltype = eol_mnemonic_undecided;
24225 else if (VECTORP (eolvalue)) /* Not yet decided. */
24226 eoltype = eol_mnemonic_undecided;
24227 else /* eolvalue is Qunix, Qdos, or Qmac. */
24228 eoltype = (EQ (eolvalue, Qunix)
24229 ? eol_mnemonic_unix
24230 : EQ (eolvalue, Qdos)
24231 ? eol_mnemonic_dos : eol_mnemonic_mac);
24235 if (eol_flag)
24237 /* Mention the EOL conversion if it is not the usual one. */
24238 if (STRINGP (eoltype))
24240 eol_str = SDATA (eoltype);
24241 eol_str_len = SBYTES (eoltype);
24243 else if (CHARACTERP (eoltype))
24245 int c = XFASTINT (eoltype);
24246 return buf + CHAR_STRING (c, (unsigned char *) buf);
24248 else
24250 eol_str = invalid_eol_type;
24251 eol_str_len = sizeof (invalid_eol_type) - 1;
24253 memcpy (buf, eol_str, eol_str_len);
24254 buf += eol_str_len;
24257 return buf;
24260 /* Return the approximate percentage N is of D (rounding upward), or 99,
24261 whichever is less. Assume 0 < D and 0 <= N <= D * INT_MAX / 100. */
24263 static int
24264 percent99 (ptrdiff_t n, ptrdiff_t d)
24266 int percent = (d - 1 + 100.0 * n) / d;
24267 return min (percent, 99);
24270 /* Return a string for the output of a mode line %-spec for window W,
24271 generated by character C. FIELD_WIDTH > 0 means pad the string
24272 returned with spaces to that value. Return a Lisp string in
24273 *STRING if the resulting string is taken from that Lisp string.
24275 Note we operate on the current buffer for most purposes. */
24277 static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
24279 static const char *
24280 decode_mode_spec (struct window *w, register int c, int field_width,
24281 Lisp_Object *string)
24283 Lisp_Object obj;
24284 struct frame *f = XFRAME (WINDOW_FRAME (w));
24285 char *decode_mode_spec_buf = f->decode_mode_spec_buffer;
24286 /* We are going to use f->decode_mode_spec_buffer as the buffer to
24287 produce strings from numerical values, so limit preposterously
24288 large values of FIELD_WIDTH to avoid overrunning the buffer's
24289 end. The size of the buffer is enough for FRAME_MESSAGE_BUF_SIZE
24290 bytes plus the terminating null. */
24291 int width = min (field_width, FRAME_MESSAGE_BUF_SIZE (f));
24292 struct buffer *b = current_buffer;
24294 obj = Qnil;
24295 *string = Qnil;
24297 switch (c)
24299 case '*':
24300 if (!NILP (BVAR (b, read_only)))
24301 return "%";
24302 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
24303 return "*";
24304 return "-";
24306 case '+':
24307 /* This differs from %* only for a modified read-only buffer. */
24308 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
24309 return "*";
24310 if (!NILP (BVAR (b, read_only)))
24311 return "%";
24312 return "-";
24314 case '&':
24315 /* This differs from %* in ignoring read-only-ness. */
24316 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
24317 return "*";
24318 return "-";
24320 case '%':
24321 return "%";
24323 case '[':
24325 int i;
24326 char *p;
24328 if (command_loop_level > 5)
24329 return "[[[... ";
24330 p = decode_mode_spec_buf;
24331 for (i = 0; i < command_loop_level; i++)
24332 *p++ = '[';
24333 *p = 0;
24334 return decode_mode_spec_buf;
24337 case ']':
24339 int i;
24340 char *p;
24342 if (command_loop_level > 5)
24343 return " ...]]]";
24344 p = decode_mode_spec_buf;
24345 for (i = 0; i < command_loop_level; i++)
24346 *p++ = ']';
24347 *p = 0;
24348 return decode_mode_spec_buf;
24351 case '-':
24353 register int i;
24355 /* Let lots_of_dashes be a string of infinite length. */
24356 if (mode_line_target == MODE_LINE_NOPROP
24357 || mode_line_target == MODE_LINE_STRING)
24358 return "--";
24359 if (field_width <= 0
24360 || field_width > sizeof (lots_of_dashes))
24362 for (i = 0; i < FRAME_MESSAGE_BUF_SIZE (f) - 1; ++i)
24363 decode_mode_spec_buf[i] = '-';
24364 decode_mode_spec_buf[i] = '\0';
24365 return decode_mode_spec_buf;
24367 else
24368 return lots_of_dashes;
24371 case 'b':
24372 obj = BVAR (b, name);
24373 break;
24375 case 'c':
24376 case 'C':
24377 /* %c, %C, and %l are ignored in `frame-title-format'.
24378 (In redisplay_internal, the frame title is drawn _before_ the
24379 windows are updated, so the stuff which depends on actual
24380 window contents (such as %l) may fail to render properly, or
24381 even crash emacs.) */
24382 if (mode_line_target == MODE_LINE_TITLE)
24383 return "";
24384 else
24386 ptrdiff_t col = current_column ();
24387 int disp_col = (c == 'C') ? col + 1 : col;
24388 w->column_number_displayed = col;
24389 pint2str (decode_mode_spec_buf, width, disp_col);
24390 return decode_mode_spec_buf;
24393 case 'e':
24394 #if !defined SYSTEM_MALLOC && !defined HYBRID_MALLOC
24396 if (NILP (Vmemory_full))
24397 return "";
24398 else
24399 return "!MEM FULL! ";
24401 #else
24402 return "";
24403 #endif
24405 case 'F':
24406 /* %F displays the frame name. */
24407 if (!NILP (f->title))
24408 return SSDATA (f->title);
24409 if (f->explicit_name || ! FRAME_WINDOW_P (f))
24410 return SSDATA (f->name);
24411 return "Emacs";
24413 case 'f':
24414 obj = BVAR (b, filename);
24415 break;
24417 case 'i':
24419 ptrdiff_t size = ZV - BEGV;
24420 pint2str (decode_mode_spec_buf, width, size);
24421 return decode_mode_spec_buf;
24424 case 'I':
24426 ptrdiff_t size = ZV - BEGV;
24427 pint2hrstr (decode_mode_spec_buf, width, size);
24428 return decode_mode_spec_buf;
24431 case 'l':
24433 ptrdiff_t startpos, startpos_byte, line, linepos, linepos_byte;
24434 ptrdiff_t topline, nlines, height;
24435 ptrdiff_t junk;
24437 /* %c, %C, and %l are ignored in `frame-title-format'. */
24438 if (mode_line_target == MODE_LINE_TITLE)
24439 return "";
24441 startpos = marker_position (w->start);
24442 startpos_byte = marker_byte_position (w->start);
24443 height = WINDOW_TOTAL_LINES (w);
24445 /* If we decided that this buffer isn't suitable for line numbers,
24446 don't forget that too fast. */
24447 if (w->base_line_pos == -1)
24448 goto no_value;
24450 /* If the buffer is very big, don't waste time. */
24451 if (INTEGERP (Vline_number_display_limit)
24452 && BUF_ZV (b) - BUF_BEGV (b) > XINT (Vline_number_display_limit))
24454 w->base_line_pos = 0;
24455 w->base_line_number = 0;
24456 goto no_value;
24459 if (w->base_line_number > 0
24460 && w->base_line_pos > 0
24461 && w->base_line_pos <= startpos)
24463 line = w->base_line_number;
24464 linepos = w->base_line_pos;
24465 linepos_byte = buf_charpos_to_bytepos (b, linepos);
24467 else
24469 line = 1;
24470 linepos = BUF_BEGV (b);
24471 linepos_byte = BUF_BEGV_BYTE (b);
24474 /* Count lines from base line to window start position. */
24475 nlines = display_count_lines (linepos_byte,
24476 startpos_byte,
24477 startpos, &junk);
24479 topline = nlines + line;
24481 /* Determine a new base line, if the old one is too close
24482 or too far away, or if we did not have one.
24483 "Too close" means it's plausible a scroll-down would
24484 go back past it. */
24485 if (startpos == BUF_BEGV (b))
24487 w->base_line_number = topline;
24488 w->base_line_pos = BUF_BEGV (b);
24490 else if (nlines < height + 25 || nlines > height * 3 + 50
24491 || linepos == BUF_BEGV (b))
24493 ptrdiff_t limit = BUF_BEGV (b);
24494 ptrdiff_t limit_byte = BUF_BEGV_BYTE (b);
24495 ptrdiff_t position;
24496 ptrdiff_t distance =
24497 (height * 2 + 30) * line_number_display_limit_width;
24499 if (startpos - distance > limit)
24501 limit = startpos - distance;
24502 limit_byte = CHAR_TO_BYTE (limit);
24505 nlines = display_count_lines (startpos_byte,
24506 limit_byte,
24507 - (height * 2 + 30),
24508 &position);
24509 /* If we couldn't find the lines we wanted within
24510 line_number_display_limit_width chars per line,
24511 give up on line numbers for this window. */
24512 if (position == limit_byte && limit == startpos - distance)
24514 w->base_line_pos = -1;
24515 w->base_line_number = 0;
24516 goto no_value;
24519 w->base_line_number = topline - nlines;
24520 w->base_line_pos = BYTE_TO_CHAR (position);
24523 /* Now count lines from the start pos to point. */
24524 nlines = display_count_lines (startpos_byte,
24525 PT_BYTE, PT, &junk);
24527 /* Record that we did display the line number. */
24528 line_number_displayed = true;
24530 /* Make the string to show. */
24531 pint2str (decode_mode_spec_buf, width, topline + nlines);
24532 return decode_mode_spec_buf;
24533 no_value:
24535 char *p = decode_mode_spec_buf;
24536 int pad = width - 2;
24537 while (pad-- > 0)
24538 *p++ = ' ';
24539 *p++ = '?';
24540 *p++ = '?';
24541 *p = '\0';
24542 return decode_mode_spec_buf;
24545 break;
24547 case 'm':
24548 obj = BVAR (b, mode_name);
24549 break;
24551 case 'n':
24552 if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
24553 return " Narrow";
24554 break;
24556 /* Display the "degree of travel" of the window through the buffer. */
24557 case 'o':
24559 ptrdiff_t toppos = marker_position (w->start);
24560 ptrdiff_t botpos = BUF_Z (b) - w->window_end_pos;
24561 ptrdiff_t begv = BUF_BEGV (b);
24562 ptrdiff_t zv = BUF_ZV (b);
24564 if (zv <= botpos)
24565 return toppos <= begv ? "All" : "Bottom";
24566 else if (toppos <= begv)
24567 return "Top";
24568 else
24570 sprintf (decode_mode_spec_buf, "%2d%%",
24571 percent99 (toppos - begv, (toppos - begv) + (zv - botpos)));
24572 return decode_mode_spec_buf;
24576 /* Display percentage of buffer above the top of the screen. */
24577 case 'p':
24579 ptrdiff_t pos = marker_position (w->start);
24580 ptrdiff_t begv = BUF_BEGV (b);
24581 ptrdiff_t zv = BUF_ZV (b);
24583 if (w->window_end_pos <= BUF_Z (b) - zv)
24584 return pos <= begv ? "All" : "Bottom";
24585 else if (pos <= begv)
24586 return "Top";
24587 else
24589 sprintf (decode_mode_spec_buf, "%2d%%",
24590 percent99 (pos - begv, zv - begv));
24591 return decode_mode_spec_buf;
24595 /* Display percentage of size above the bottom of the screen. */
24596 case 'P':
24598 ptrdiff_t toppos = marker_position (w->start);
24599 ptrdiff_t botpos = BUF_Z (b) - w->window_end_pos;
24600 ptrdiff_t begv = BUF_BEGV (b);
24601 ptrdiff_t zv = BUF_ZV (b);
24603 if (zv <= botpos)
24604 return toppos <= begv ? "All" : "Bottom";
24605 else
24607 sprintf (decode_mode_spec_buf,
24608 &"Top%2d%%"[begv < toppos ? sizeof "Top" - 1 : 0],
24609 percent99 (botpos - begv, zv - begv));
24610 return decode_mode_spec_buf;
24614 /* Display percentage offsets of top and bottom of the window,
24615 using "All" (but not "Top" or "Bottom") where appropriate. */
24616 case 'q':
24618 ptrdiff_t toppos = marker_position (w->start);
24619 ptrdiff_t botpos = BUF_Z (b) - w->window_end_pos;
24620 ptrdiff_t begv = BUF_BEGV (b);
24621 ptrdiff_t zv = BUF_ZV (b);
24622 int top_perc, bot_perc;
24624 if ((toppos <= begv) && (zv <= botpos))
24625 return "All ";
24627 top_perc = toppos <= begv ? 0 : percent99 (toppos - begv, zv - begv);
24628 bot_perc = zv <= botpos ? 100 : percent99 (botpos - begv, zv - begv);
24630 if (top_perc == bot_perc)
24631 sprintf (decode_mode_spec_buf, "%d%%", top_perc);
24632 else
24633 sprintf (decode_mode_spec_buf, "%d-%d%%", top_perc, bot_perc);
24635 return decode_mode_spec_buf;
24638 case 's':
24639 /* status of process */
24640 obj = Fget_buffer_process (Fcurrent_buffer ());
24641 if (NILP (obj))
24642 return "no process";
24643 #ifndef MSDOS
24644 obj = Fsymbol_name (Fprocess_status (obj));
24645 #endif
24646 break;
24648 case '@':
24650 ptrdiff_t count = inhibit_garbage_collection ();
24651 Lisp_Object curdir = BVAR (current_buffer, directory);
24652 Lisp_Object val = Qnil;
24654 if (STRINGP (curdir))
24655 val = call1 (intern ("file-remote-p"), curdir);
24657 unbind_to (count, Qnil);
24659 if (NILP (val))
24660 return "-";
24661 else
24662 return "@";
24665 case 'z':
24666 /* coding-system (not including end-of-line format) */
24667 case 'Z':
24668 /* coding-system (including end-of-line type) */
24670 bool eol_flag = (c == 'Z');
24671 char *p = decode_mode_spec_buf;
24673 if (! FRAME_WINDOW_P (f))
24675 /* No need to mention EOL here--the terminal never needs
24676 to do EOL conversion. */
24677 p = decode_mode_spec_coding (CODING_ID_NAME
24678 (FRAME_KEYBOARD_CODING (f)->id),
24679 p, false);
24680 p = decode_mode_spec_coding (CODING_ID_NAME
24681 (FRAME_TERMINAL_CODING (f)->id),
24682 p, false);
24684 p = decode_mode_spec_coding (BVAR (b, buffer_file_coding_system),
24685 p, eol_flag);
24687 #if false /* This proves to be annoying; I think we can do without. -- rms. */
24688 #ifdef subprocesses
24689 obj = Fget_buffer_process (Fcurrent_buffer ());
24690 if (PROCESSP (obj))
24692 p = decode_mode_spec_coding
24693 (XPROCESS (obj)->decode_coding_system, p, eol_flag);
24694 p = decode_mode_spec_coding
24695 (XPROCESS (obj)->encode_coding_system, p, eol_flag);
24697 #endif /* subprocesses */
24698 #endif /* false */
24699 *p = 0;
24700 return decode_mode_spec_buf;
24704 if (STRINGP (obj))
24706 *string = obj;
24707 return SSDATA (obj);
24709 else
24710 return "";
24714 /* Count up to COUNT lines starting from START_BYTE. COUNT negative
24715 means count lines back from START_BYTE. But don't go beyond
24716 LIMIT_BYTE. Return the number of lines thus found (always
24717 nonnegative).
24719 Set *BYTE_POS_PTR to the byte position where we stopped. This is
24720 either the position COUNT lines after/before START_BYTE, if we
24721 found COUNT lines, or LIMIT_BYTE if we hit the limit before finding
24722 COUNT lines. */
24724 static ptrdiff_t
24725 display_count_lines (ptrdiff_t start_byte,
24726 ptrdiff_t limit_byte, ptrdiff_t count,
24727 ptrdiff_t *byte_pos_ptr)
24729 register unsigned char *cursor;
24730 unsigned char *base;
24732 register ptrdiff_t ceiling;
24733 register unsigned char *ceiling_addr;
24734 ptrdiff_t orig_count = count;
24736 /* If we are not in selective display mode,
24737 check only for newlines. */
24738 bool selective_display
24739 = (!NILP (BVAR (current_buffer, selective_display))
24740 && !INTEGERP (BVAR (current_buffer, selective_display)));
24742 if (count > 0)
24744 while (start_byte < limit_byte)
24746 ceiling = BUFFER_CEILING_OF (start_byte);
24747 ceiling = min (limit_byte - 1, ceiling);
24748 ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
24749 base = (cursor = BYTE_POS_ADDR (start_byte));
24753 if (selective_display)
24755 while (*cursor != '\n' && *cursor != 015
24756 && ++cursor != ceiling_addr)
24757 continue;
24758 if (cursor == ceiling_addr)
24759 break;
24761 else
24763 cursor = memchr (cursor, '\n', ceiling_addr - cursor);
24764 if (! cursor)
24765 break;
24768 cursor++;
24770 if (--count == 0)
24772 start_byte += cursor - base;
24773 *byte_pos_ptr = start_byte;
24774 return orig_count;
24777 while (cursor < ceiling_addr);
24779 start_byte += ceiling_addr - base;
24782 else
24784 while (start_byte > limit_byte)
24786 ceiling = BUFFER_FLOOR_OF (start_byte - 1);
24787 ceiling = max (limit_byte, ceiling);
24788 ceiling_addr = BYTE_POS_ADDR (ceiling);
24789 base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
24790 while (true)
24792 if (selective_display)
24794 while (--cursor >= ceiling_addr
24795 && *cursor != '\n' && *cursor != 015)
24796 continue;
24797 if (cursor < ceiling_addr)
24798 break;
24800 else
24802 cursor = memrchr (ceiling_addr, '\n', cursor - ceiling_addr);
24803 if (! cursor)
24804 break;
24807 if (++count == 0)
24809 start_byte += cursor - base + 1;
24810 *byte_pos_ptr = start_byte;
24811 /* When scanning backwards, we should
24812 not count the newline posterior to which we stop. */
24813 return - orig_count - 1;
24816 start_byte += ceiling_addr - base;
24820 *byte_pos_ptr = limit_byte;
24822 if (count < 0)
24823 return - orig_count + count;
24824 return orig_count - count;
24830 /***********************************************************************
24831 Displaying strings
24832 ***********************************************************************/
24834 /* Display a NUL-terminated string, starting with index START.
24836 If STRING is non-null, display that C string. Otherwise, the Lisp
24837 string LISP_STRING is displayed. There's a case that STRING is
24838 non-null and LISP_STRING is not nil. It means STRING is a string
24839 data of LISP_STRING. In that case, we display LISP_STRING while
24840 ignoring its text properties.
24842 If FACE_STRING is not nil, FACE_STRING_POS is a position in
24843 FACE_STRING. Display STRING or LISP_STRING with the face at
24844 FACE_STRING_POS in FACE_STRING:
24846 Display the string in the environment given by IT, but use the
24847 standard display table, temporarily.
24849 FIELD_WIDTH is the minimum number of output glyphs to produce.
24850 If STRING has fewer characters than FIELD_WIDTH, pad to the right
24851 with spaces. If STRING has more characters, more than FIELD_WIDTH
24852 glyphs will be produced. FIELD_WIDTH <= 0 means don't pad.
24854 PRECISION is the maximum number of characters to output from
24855 STRING. PRECISION < 0 means don't truncate the string.
24857 This is roughly equivalent to printf format specifiers:
24859 FIELD_WIDTH PRECISION PRINTF
24860 ----------------------------------------
24861 -1 -1 %s
24862 -1 10 %.10s
24863 10 -1 %10s
24864 20 10 %20.10s
24866 MULTIBYTE zero means do not display multibyte chars, > 0 means do
24867 display them, and < 0 means obey the current buffer's value of
24868 enable_multibyte_characters.
24870 Value is the number of columns displayed. */
24872 static int
24873 display_string (const char *string, Lisp_Object lisp_string, Lisp_Object face_string,
24874 ptrdiff_t face_string_pos, ptrdiff_t start, struct it *it,
24875 int field_width, int precision, int max_x, int multibyte)
24877 int hpos_at_start = it->hpos;
24878 int saved_face_id = it->face_id;
24879 struct glyph_row *row = it->glyph_row;
24880 ptrdiff_t it_charpos;
24882 /* Initialize the iterator IT for iteration over STRING beginning
24883 with index START. */
24884 reseat_to_string (it, NILP (lisp_string) ? string : NULL, lisp_string, start,
24885 precision, field_width, multibyte);
24886 if (string && STRINGP (lisp_string))
24887 /* LISP_STRING is the one returned by decode_mode_spec. We should
24888 ignore its text properties. */
24889 it->stop_charpos = it->end_charpos;
24891 /* If displaying STRING, set up the face of the iterator from
24892 FACE_STRING, if that's given. */
24893 if (STRINGP (face_string))
24895 ptrdiff_t endptr;
24896 struct face *face;
24898 it->face_id
24899 = face_at_string_position (it->w, face_string, face_string_pos,
24900 0, &endptr, it->base_face_id, false);
24901 face = FACE_FROM_ID (it->f, it->face_id);
24902 it->face_box_p = face->box != FACE_NO_BOX;
24905 /* Set max_x to the maximum allowed X position. Don't let it go
24906 beyond the right edge of the window. */
24907 if (max_x <= 0)
24908 max_x = it->last_visible_x;
24909 else
24910 max_x = min (max_x, it->last_visible_x);
24912 /* Skip over display elements that are not visible. because IT->w is
24913 hscrolled. */
24914 if (it->current_x < it->first_visible_x)
24915 move_it_in_display_line_to (it, 100000, it->first_visible_x,
24916 MOVE_TO_POS | MOVE_TO_X);
24918 row->ascent = it->max_ascent;
24919 row->height = it->max_ascent + it->max_descent;
24920 row->phys_ascent = it->max_phys_ascent;
24921 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
24922 row->extra_line_spacing = it->max_extra_line_spacing;
24924 if (STRINGP (it->string))
24925 it_charpos = IT_STRING_CHARPOS (*it);
24926 else
24927 it_charpos = IT_CHARPOS (*it);
24929 /* This condition is for the case that we are called with current_x
24930 past last_visible_x. */
24931 while (it->current_x < max_x)
24933 int x_before, x, n_glyphs_before, i, nglyphs;
24935 /* Get the next display element. */
24936 if (!get_next_display_element (it))
24937 break;
24939 /* Produce glyphs. */
24940 x_before = it->current_x;
24941 n_glyphs_before = row->used[TEXT_AREA];
24942 PRODUCE_GLYPHS (it);
24944 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
24945 i = 0;
24946 x = x_before;
24947 while (i < nglyphs)
24949 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
24951 if (it->line_wrap != TRUNCATE
24952 && x + glyph->pixel_width > max_x)
24954 /* End of continued line or max_x reached. */
24955 if (CHAR_GLYPH_PADDING_P (*glyph))
24957 /* A wide character is unbreakable. */
24958 if (row->reversed_p)
24959 unproduce_glyphs (it, row->used[TEXT_AREA]
24960 - n_glyphs_before);
24961 row->used[TEXT_AREA] = n_glyphs_before;
24962 it->current_x = x_before;
24964 else
24966 if (row->reversed_p)
24967 unproduce_glyphs (it, row->used[TEXT_AREA]
24968 - (n_glyphs_before + i));
24969 row->used[TEXT_AREA] = n_glyphs_before + i;
24970 it->current_x = x;
24972 break;
24974 else if (x + glyph->pixel_width >= it->first_visible_x)
24976 /* Glyph is at least partially visible. */
24977 ++it->hpos;
24978 if (x < it->first_visible_x)
24979 row->x = x - it->first_visible_x;
24981 else
24983 /* Glyph is off the left margin of the display area.
24984 Should not happen. */
24985 emacs_abort ();
24988 row->ascent = max (row->ascent, it->max_ascent);
24989 row->height = max (row->height, it->max_ascent + it->max_descent);
24990 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
24991 row->phys_height = max (row->phys_height,
24992 it->max_phys_ascent + it->max_phys_descent);
24993 row->extra_line_spacing = max (row->extra_line_spacing,
24994 it->max_extra_line_spacing);
24995 x += glyph->pixel_width;
24996 ++i;
24999 /* Stop if max_x reached. */
25000 if (i < nglyphs)
25001 break;
25003 /* Stop at line ends. */
25004 if (ITERATOR_AT_END_OF_LINE_P (it))
25006 it->continuation_lines_width = 0;
25007 break;
25010 set_iterator_to_next (it, true);
25011 if (STRINGP (it->string))
25012 it_charpos = IT_STRING_CHARPOS (*it);
25013 else
25014 it_charpos = IT_CHARPOS (*it);
25016 /* Stop if truncating at the right edge. */
25017 if (it->line_wrap == TRUNCATE
25018 && it->current_x >= it->last_visible_x)
25020 /* Add truncation mark, but don't do it if the line is
25021 truncated at a padding space. */
25022 if (it_charpos < it->string_nchars)
25024 if (!FRAME_WINDOW_P (it->f))
25026 int ii, n;
25028 if (it->current_x > it->last_visible_x)
25030 if (!row->reversed_p)
25032 for (ii = row->used[TEXT_AREA] - 1; ii > 0; --ii)
25033 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][ii]))
25034 break;
25036 else
25038 for (ii = 0; ii < row->used[TEXT_AREA]; ii++)
25039 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][ii]))
25040 break;
25041 unproduce_glyphs (it, ii + 1);
25042 ii = row->used[TEXT_AREA] - (ii + 1);
25044 for (n = row->used[TEXT_AREA]; ii < n; ++ii)
25046 row->used[TEXT_AREA] = ii;
25047 produce_special_glyphs (it, IT_TRUNCATION);
25050 produce_special_glyphs (it, IT_TRUNCATION);
25052 row->truncated_on_right_p = true;
25054 break;
25058 /* Maybe insert a truncation at the left. */
25059 if (it->first_visible_x
25060 && it_charpos > 0)
25062 if (!FRAME_WINDOW_P (it->f)
25063 || (row->reversed_p
25064 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
25065 : WINDOW_LEFT_FRINGE_WIDTH (it->w)) == 0)
25066 insert_left_trunc_glyphs (it);
25067 row->truncated_on_left_p = true;
25070 it->face_id = saved_face_id;
25072 /* Value is number of columns displayed. */
25073 return it->hpos - hpos_at_start;
25078 /* This is like a combination of memq and assq. Return 1/2 if PROPVAL
25079 appears as an element of LIST or as the car of an element of LIST.
25080 If PROPVAL is a list, compare each element against LIST in that
25081 way, and return 1/2 if any element of PROPVAL is found in LIST.
25082 Otherwise return 0. This function cannot quit.
25083 The return value is 2 if the text is invisible but with an ellipsis
25084 and 1 if it's invisible and without an ellipsis. */
25087 invisible_prop (Lisp_Object propval, Lisp_Object list)
25089 Lisp_Object tail, proptail;
25091 for (tail = list; CONSP (tail); tail = XCDR (tail))
25093 register Lisp_Object tem;
25094 tem = XCAR (tail);
25095 if (EQ (propval, tem))
25096 return 1;
25097 if (CONSP (tem) && EQ (propval, XCAR (tem)))
25098 return NILP (XCDR (tem)) ? 1 : 2;
25101 if (CONSP (propval))
25103 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
25105 Lisp_Object propelt;
25106 propelt = XCAR (proptail);
25107 for (tail = list; CONSP (tail); tail = XCDR (tail))
25109 register Lisp_Object tem;
25110 tem = XCAR (tail);
25111 if (EQ (propelt, tem))
25112 return 1;
25113 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
25114 return NILP (XCDR (tem)) ? 1 : 2;
25119 return 0;
25122 DEFUN ("invisible-p", Finvisible_p, Sinvisible_p, 1, 1, 0,
25123 doc: /* Non-nil if text properties at POS cause text there to be currently invisible.
25124 POS should be a marker or a buffer position; the value of the `invisible'
25125 property at that position in the current buffer is examined.
25126 POS can also be the actual value of the `invisible' text or overlay
25127 property of the text of interest, in which case the value itself is
25128 examined.
25130 The non-nil value returned can be t for currently invisible text that is
25131 entirely hidden on display, or some other non-nil, non-t value if the
25132 text is replaced by an ellipsis.
25134 Note that whether text with `invisible' property is actually hidden on
25135 display may depend on `buffer-invisibility-spec', which see. */)
25136 (Lisp_Object pos)
25138 Lisp_Object prop
25139 = (NATNUMP (pos) || MARKERP (pos)
25140 ? Fget_char_property (pos, Qinvisible, Qnil)
25141 : pos);
25142 int invis = TEXT_PROP_MEANS_INVISIBLE (prop);
25143 return (invis == 0 ? Qnil
25144 : invis == 1 ? Qt
25145 : make_number (invis));
25148 /* Calculate a width or height in pixels from a specification using
25149 the following elements:
25151 SPEC ::=
25152 NUM - a (fractional) multiple of the default font width/height
25153 (NUM) - specifies exactly NUM pixels
25154 UNIT - a fixed number of pixels, see below.
25155 ELEMENT - size of a display element in pixels, see below.
25156 (NUM . SPEC) - equals NUM * SPEC
25157 (+ SPEC SPEC ...) - add pixel values
25158 (- SPEC SPEC ...) - subtract pixel values
25159 (- SPEC) - negate pixel value
25161 NUM ::=
25162 INT or FLOAT - a number constant
25163 SYMBOL - use symbol's (buffer local) variable binding.
25165 UNIT ::=
25166 in - pixels per inch *)
25167 mm - pixels per 1/1000 meter *)
25168 cm - pixels per 1/100 meter *)
25169 width - width of current font in pixels.
25170 height - height of current font in pixels.
25172 *) using the ratio(s) defined in display-pixels-per-inch.
25174 ELEMENT ::=
25176 left-fringe - left fringe width in pixels
25177 right-fringe - right fringe width in pixels
25179 left-margin - left margin width in pixels
25180 right-margin - right margin width in pixels
25182 scroll-bar - scroll-bar area width in pixels
25184 Examples:
25186 Pixels corresponding to 5 inches:
25187 (5 . in)
25189 Total width of non-text areas on left side of window (if scroll-bar is on left):
25190 '(space :width (+ left-fringe left-margin scroll-bar))
25192 Align to first text column (in header line):
25193 '(space :align-to 0)
25195 Align to middle of text area minus half the width of variable `my-image'
25196 containing a loaded image:
25197 '(space :align-to (0.5 . (- text my-image)))
25199 Width of left margin minus width of 1 character in the default font:
25200 '(space :width (- left-margin 1))
25202 Width of left margin minus width of 2 characters in the current font:
25203 '(space :width (- left-margin (2 . width)))
25205 Center 1 character over left-margin (in header line):
25206 '(space :align-to (+ left-margin (0.5 . left-margin) -0.5))
25208 Different ways to express width of left fringe plus left margin minus one pixel:
25209 '(space :width (- (+ left-fringe left-margin) (1)))
25210 '(space :width (+ left-fringe left-margin (- (1))))
25211 '(space :width (+ left-fringe left-margin (-1)))
25213 If ALIGN_TO is NULL, returns the result in *RES. If ALIGN_TO is
25214 non-NULL, the value of *ALIGN_TO is a window-relative pixel
25215 coordinate, and *RES is the additional pixel width from that point
25216 till the end of the stretch glyph.
25218 WIDTH_P non-zero means take the width dimension or X coordinate of
25219 the object specified by PROP, WIDTH_P zero means take the height
25220 dimension or the Y coordinate. (Therefore, if ALIGN_TO is
25221 non-NULL, WIDTH_P should be non-zero.)
25223 FONT is the font of the face of the surrounding text.
25225 The return value is non-zero if width or height were successfully
25226 calculated, i.e. if PROP is a valid spec. */
25228 static bool
25229 calc_pixel_width_or_height (double *res, struct it *it, Lisp_Object prop,
25230 struct font *font, bool width_p, int *align_to)
25232 double pixels;
25234 # define OK_PIXELS(val) (*res = (val), true)
25235 # define OK_ALIGN_TO(val) (*align_to = (val), true)
25237 if (NILP (prop))
25238 return OK_PIXELS (0);
25240 eassert (FRAME_LIVE_P (it->f));
25242 if (SYMBOLP (prop))
25244 if (SCHARS (SYMBOL_NAME (prop)) == 2)
25246 char *unit = SSDATA (SYMBOL_NAME (prop));
25248 /* The UNIT expression, e.g. as part of (NUM . UNIT). */
25249 if (unit[0] == 'i' && unit[1] == 'n')
25250 pixels = 1.0;
25251 else if (unit[0] == 'm' && unit[1] == 'm')
25252 pixels = 25.4;
25253 else if (unit[0] == 'c' && unit[1] == 'm')
25254 pixels = 2.54;
25255 else
25256 pixels = 0;
25257 if (pixels > 0)
25259 double ppi = (width_p ? FRAME_RES_X (it->f)
25260 : FRAME_RES_Y (it->f));
25262 if (ppi > 0)
25263 return OK_PIXELS (ppi / pixels);
25264 return false;
25268 #ifdef HAVE_WINDOW_SYSTEM
25269 /* 'height': the height of FONT. */
25270 if (EQ (prop, Qheight))
25271 return OK_PIXELS (font
25272 ? normal_char_height (font, -1)
25273 : FRAME_LINE_HEIGHT (it->f));
25274 /* 'width': the width of FONT. */
25275 if (EQ (prop, Qwidth))
25276 return OK_PIXELS (font
25277 ? FONT_WIDTH (font)
25278 : FRAME_COLUMN_WIDTH (it->f));
25279 #else
25280 if (EQ (prop, Qheight) || EQ (prop, Qwidth))
25281 return OK_PIXELS (1);
25282 #endif
25284 /* 'text': the width or height of the text area. */
25285 if (EQ (prop, Qtext))
25286 return OK_PIXELS (width_p
25287 ? (window_box_width (it->w, TEXT_AREA)
25288 - it->lnum_pixel_width)
25289 : WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w));
25291 /* ':align_to'. First time we compute the value, window
25292 elements are interpreted as the position of the element's
25293 left edge. */
25294 if (align_to && *align_to < 0)
25296 *res = 0;
25297 /* 'left': left edge of the text area. */
25298 if (EQ (prop, Qleft))
25299 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA)
25300 + it->lnum_pixel_width);
25301 /* 'right': right edge of the text area. */
25302 if (EQ (prop, Qright))
25303 return OK_ALIGN_TO (window_box_right_offset (it->w, TEXT_AREA));
25304 /* 'center': the center of the text area. */
25305 if (EQ (prop, Qcenter))
25306 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA)
25307 + it->lnum_pixel_width
25308 + window_box_width (it->w, TEXT_AREA) / 2);
25309 /* 'left-fringe': left edge of the left fringe. */
25310 if (EQ (prop, Qleft_fringe))
25311 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
25312 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (it->w)
25313 : window_box_right_offset (it->w, LEFT_MARGIN_AREA));
25314 /* 'right-fringe': left edge of the right fringe. */
25315 if (EQ (prop, Qright_fringe))
25316 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
25317 ? window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
25318 : window_box_right_offset (it->w, TEXT_AREA));
25319 /* 'left-margin': left edge of the left display margin. */
25320 if (EQ (prop, Qleft_margin))
25321 return OK_ALIGN_TO (window_box_left_offset (it->w, LEFT_MARGIN_AREA));
25322 /* 'right-margin': left edge of the right display margin. */
25323 if (EQ (prop, Qright_margin))
25324 return OK_ALIGN_TO (window_box_left_offset (it->w, RIGHT_MARGIN_AREA));
25325 /* 'scroll-bar': left edge of the vertical scroll bar. */
25326 if (EQ (prop, Qscroll_bar))
25327 return OK_ALIGN_TO (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (it->w)
25329 : (window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
25330 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
25331 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
25332 : 0)));
25334 else
25336 /* Otherwise, the elements stand for their width. */
25337 if (EQ (prop, Qleft_fringe))
25338 return OK_PIXELS (WINDOW_LEFT_FRINGE_WIDTH (it->w));
25339 if (EQ (prop, Qright_fringe))
25340 return OK_PIXELS (WINDOW_RIGHT_FRINGE_WIDTH (it->w));
25341 if (EQ (prop, Qleft_margin))
25342 return OK_PIXELS (WINDOW_LEFT_MARGIN_WIDTH (it->w));
25343 if (EQ (prop, Qright_margin))
25344 return OK_PIXELS (WINDOW_RIGHT_MARGIN_WIDTH (it->w));
25345 if (EQ (prop, Qscroll_bar))
25346 return OK_PIXELS (WINDOW_SCROLL_BAR_AREA_WIDTH (it->w));
25349 prop = buffer_local_value (prop, it->w->contents);
25350 if (EQ (prop, Qunbound))
25351 prop = Qnil;
25354 if (NUMBERP (prop))
25356 int base_unit = (width_p
25357 ? FRAME_COLUMN_WIDTH (it->f)
25358 : FRAME_LINE_HEIGHT (it->f));
25359 if (width_p && align_to && *align_to < 0)
25360 return OK_PIXELS (XFLOATINT (prop) * base_unit + it->lnum_pixel_width);
25361 return OK_PIXELS (XFLOATINT (prop) * base_unit);
25364 if (CONSP (prop))
25366 Lisp_Object car = XCAR (prop);
25367 Lisp_Object cdr = XCDR (prop);
25369 if (SYMBOLP (car))
25371 #ifdef HAVE_WINDOW_SYSTEM
25372 /* '(image PROPS...)': width or height of the specified image. */
25373 if (FRAME_WINDOW_P (it->f)
25374 && valid_image_p (prop))
25376 ptrdiff_t id = lookup_image (it->f, prop);
25377 struct image *img = IMAGE_FROM_ID (it->f, id);
25379 return OK_PIXELS (width_p ? img->width : img->height);
25381 /* '(xwidget PROPS...)': dimensions of the specified xwidget. */
25382 if (FRAME_WINDOW_P (it->f) && valid_xwidget_spec_p (prop))
25384 /* TODO: Don't return dummy size. */
25385 return OK_PIXELS (100);
25387 #endif
25388 /* '(+ EXPR...)' or '(- EXPR...)' add or subtract
25389 recursively calculated values. */
25390 if (EQ (car, Qplus) || EQ (car, Qminus))
25392 bool first = true;
25393 double px;
25395 pixels = 0;
25396 while (CONSP (cdr))
25398 if (!calc_pixel_width_or_height (&px, it, XCAR (cdr),
25399 font, width_p, align_to))
25400 return false;
25401 if (first)
25402 pixels = (EQ (car, Qplus) ? px : -px), first = false;
25403 else
25404 pixels += px;
25405 cdr = XCDR (cdr);
25407 if (EQ (car, Qminus))
25408 pixels = -pixels;
25409 return OK_PIXELS (pixels);
25412 car = buffer_local_value (car, it->w->contents);
25413 if (EQ (car, Qunbound))
25414 car = Qnil;
25417 /* '(NUM)': absolute number of pixels. */
25418 if (NUMBERP (car))
25420 double fact;
25421 int offset =
25422 width_p && align_to && *align_to < 0 ? it->lnum_pixel_width : 0;
25423 pixels = XFLOATINT (car);
25424 if (NILP (cdr))
25425 return OK_PIXELS (pixels + offset);
25426 if (calc_pixel_width_or_height (&fact, it, cdr,
25427 font, width_p, align_to))
25428 return OK_PIXELS (pixels * fact + offset);
25429 return false;
25432 return false;
25435 return false;
25438 void
25439 get_font_ascent_descent (struct font *font, int *ascent, int *descent)
25441 #ifdef HAVE_WINDOW_SYSTEM
25442 normal_char_ascent_descent (font, -1, ascent, descent);
25443 #else
25444 *ascent = 1;
25445 *descent = 0;
25446 #endif
25450 /***********************************************************************
25451 Glyph Display
25452 ***********************************************************************/
25454 #ifdef HAVE_WINDOW_SYSTEM
25456 #ifdef GLYPH_DEBUG
25458 void
25459 dump_glyph_string (struct glyph_string *s)
25461 fprintf (stderr, "glyph string\n");
25462 fprintf (stderr, " x, y, w, h = %d, %d, %d, %d\n",
25463 s->x, s->y, s->width, s->height);
25464 fprintf (stderr, " ybase = %d\n", s->ybase);
25465 fprintf (stderr, " hl = %u\n", s->hl);
25466 fprintf (stderr, " left overhang = %d, right = %d\n",
25467 s->left_overhang, s->right_overhang);
25468 fprintf (stderr, " nchars = %d\n", s->nchars);
25469 fprintf (stderr, " extends to end of line = %d\n",
25470 s->extends_to_end_of_line_p);
25471 fprintf (stderr, " font height = %d\n", FONT_HEIGHT (s->font));
25472 fprintf (stderr, " bg width = %d\n", s->background_width);
25475 #endif /* GLYPH_DEBUG */
25477 /* Initialize glyph string S. CHAR2B is a suitably allocated vector
25478 of XChar2b structures for S; it can't be allocated in
25479 init_glyph_string because it must be allocated via `alloca'. W
25480 is the window on which S is drawn. ROW and AREA are the glyph row
25481 and area within the row from which S is constructed. START is the
25482 index of the first glyph structure covered by S. HL is a
25483 face-override for drawing S. */
25485 #ifdef HAVE_NTGUI
25486 #define OPTIONAL_HDC(hdc) HDC hdc,
25487 #define DECLARE_HDC(hdc) HDC hdc;
25488 #define ALLOCATE_HDC(hdc, f) hdc = get_frame_dc ((f))
25489 #define RELEASE_HDC(hdc, f) release_frame_dc ((f), (hdc))
25490 #endif
25492 #ifndef OPTIONAL_HDC
25493 #define OPTIONAL_HDC(hdc)
25494 #define DECLARE_HDC(hdc)
25495 #define ALLOCATE_HDC(hdc, f)
25496 #define RELEASE_HDC(hdc, f)
25497 #endif
25499 static void
25500 init_glyph_string (struct glyph_string *s,
25501 OPTIONAL_HDC (hdc)
25502 XChar2b *char2b, struct window *w, struct glyph_row *row,
25503 enum glyph_row_area area, int start, enum draw_glyphs_face hl)
25505 memset (s, 0, sizeof *s);
25506 s->w = w;
25507 s->f = XFRAME (w->frame);
25508 #ifdef HAVE_NTGUI
25509 s->hdc = hdc;
25510 #endif
25511 s->display = FRAME_X_DISPLAY (s->f);
25512 s->char2b = char2b;
25513 s->hl = hl;
25514 s->row = row;
25515 s->area = area;
25516 s->first_glyph = row->glyphs[area] + start;
25517 s->height = row->height;
25518 s->y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
25519 s->ybase = s->y + row->ascent;
25523 /* Append the list of glyph strings with head H and tail T to the list
25524 with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the result. */
25526 static void
25527 append_glyph_string_lists (struct glyph_string **head, struct glyph_string **tail,
25528 struct glyph_string *h, struct glyph_string *t)
25530 if (h)
25532 if (*head)
25533 (*tail)->next = h;
25534 else
25535 *head = h;
25536 h->prev = *tail;
25537 *tail = t;
25542 /* Prepend the list of glyph strings with head H and tail T to the
25543 list with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the
25544 result. */
25546 static void
25547 prepend_glyph_string_lists (struct glyph_string **head, struct glyph_string **tail,
25548 struct glyph_string *h, struct glyph_string *t)
25550 if (h)
25552 if (*head)
25553 (*head)->prev = t;
25554 else
25555 *tail = t;
25556 t->next = *head;
25557 *head = h;
25562 /* Append glyph string S to the list with head *HEAD and tail *TAIL.
25563 Set *HEAD and *TAIL to the resulting list. */
25565 static void
25566 append_glyph_string (struct glyph_string **head, struct glyph_string **tail,
25567 struct glyph_string *s)
25569 s->next = s->prev = NULL;
25570 append_glyph_string_lists (head, tail, s, s);
25574 /* Get face and two-byte form of character C in face FACE_ID on frame F.
25575 The encoding of C is returned in *CHAR2B. DISPLAY_P means
25576 make sure that X resources for the face returned are allocated.
25577 Value is a pointer to a realized face that is ready for display if
25578 DISPLAY_P. */
25580 static struct face *
25581 get_char_face_and_encoding (struct frame *f, int c, int face_id,
25582 XChar2b *char2b, bool display_p)
25584 struct face *face = FACE_FROM_ID (f, face_id);
25585 unsigned code = 0;
25587 if (face->font)
25589 code = face->font->driver->encode_char (face->font, c);
25591 if (code == FONT_INVALID_CODE)
25592 code = 0;
25594 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
25596 /* Make sure X resources of the face are allocated. */
25597 #ifdef HAVE_X_WINDOWS
25598 if (display_p)
25599 #endif
25601 eassert (face != NULL);
25602 prepare_face_for_display (f, face);
25605 return face;
25609 /* Get face and two-byte form of character glyph GLYPH on frame F.
25610 The encoding of GLYPH->u.ch is returned in *CHAR2B. Value is
25611 a pointer to a realized face that is ready for display. */
25613 static struct face *
25614 get_glyph_face_and_encoding (struct frame *f, struct glyph *glyph,
25615 XChar2b *char2b)
25617 struct face *face;
25618 unsigned code = 0;
25620 eassert (glyph->type == CHAR_GLYPH);
25621 face = FACE_FROM_ID (f, glyph->face_id);
25623 /* Make sure X resources of the face are allocated. */
25624 prepare_face_for_display (f, face);
25626 if (face->font)
25628 if (CHAR_BYTE8_P (glyph->u.ch))
25629 code = CHAR_TO_BYTE8 (glyph->u.ch);
25630 else
25631 code = face->font->driver->encode_char (face->font, glyph->u.ch);
25633 if (code == FONT_INVALID_CODE)
25634 code = 0;
25637 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
25638 return face;
25642 /* Get glyph code of character C in FONT in the two-byte form CHAR2B.
25643 Return true iff FONT has a glyph for C. */
25645 static bool
25646 get_char_glyph_code (int c, struct font *font, XChar2b *char2b)
25648 unsigned code;
25650 if (CHAR_BYTE8_P (c))
25651 code = CHAR_TO_BYTE8 (c);
25652 else
25653 code = font->driver->encode_char (font, c);
25655 if (code == FONT_INVALID_CODE)
25656 return false;
25657 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
25658 return true;
25662 /* Fill glyph string S with composition components specified by S->cmp.
25664 BASE_FACE is the base face of the composition.
25665 S->cmp_from is the index of the first component for S.
25667 OVERLAPS non-zero means S should draw the foreground only, and use
25668 its physical height for clipping. See also draw_glyphs.
25670 Value is the index of a component not in S. */
25672 static int
25673 fill_composite_glyph_string (struct glyph_string *s, struct face *base_face,
25674 int overlaps)
25676 int i;
25677 /* For all glyphs of this composition, starting at the offset
25678 S->cmp_from, until we reach the end of the definition or encounter a
25679 glyph that requires the different face, add it to S. */
25680 struct face *face;
25682 eassert (s);
25684 s->for_overlaps = overlaps;
25685 s->face = NULL;
25686 s->font = NULL;
25687 for (i = s->cmp_from; i < s->cmp->glyph_len; i++)
25689 int c = COMPOSITION_GLYPH (s->cmp, i);
25691 /* TAB in a composition means display glyphs with padding space
25692 on the left or right. */
25693 if (c != '\t')
25695 int face_id = FACE_FOR_CHAR (s->f, base_face->ascii_face, c,
25696 -1, Qnil);
25698 face = get_char_face_and_encoding (s->f, c, face_id,
25699 s->char2b + i, true);
25700 if (face)
25702 if (! s->face)
25704 s->face = face;
25705 s->font = s->face->font;
25707 else if (s->face != face)
25708 break;
25711 ++s->nchars;
25713 s->cmp_to = i;
25715 if (s->face == NULL)
25717 s->face = base_face->ascii_face;
25718 s->font = s->face->font;
25721 /* All glyph strings for the same composition has the same width,
25722 i.e. the width set for the first component of the composition. */
25723 s->width = s->first_glyph->pixel_width;
25725 /* If the specified font could not be loaded, use the frame's
25726 default font, but record the fact that we couldn't load it in
25727 the glyph string so that we can draw rectangles for the
25728 characters of the glyph string. */
25729 if (s->font == NULL)
25731 s->font_not_found_p = true;
25732 s->font = FRAME_FONT (s->f);
25735 /* Adjust base line for subscript/superscript text. */
25736 s->ybase += s->first_glyph->voffset;
25738 return s->cmp_to;
25741 static int
25742 fill_gstring_glyph_string (struct glyph_string *s, int face_id,
25743 int start, int end, int overlaps)
25745 struct glyph *glyph, *last;
25746 Lisp_Object lgstring;
25747 int i;
25749 s->for_overlaps = overlaps;
25750 glyph = s->row->glyphs[s->area] + start;
25751 last = s->row->glyphs[s->area] + end;
25752 s->cmp_id = glyph->u.cmp.id;
25753 s->cmp_from = glyph->slice.cmp.from;
25754 s->cmp_to = glyph->slice.cmp.to + 1;
25755 s->face = FACE_FROM_ID (s->f, face_id);
25756 lgstring = composition_gstring_from_id (s->cmp_id);
25757 s->font = XFONT_OBJECT (LGSTRING_FONT (lgstring));
25758 glyph++;
25759 while (glyph < last
25760 && glyph->u.cmp.automatic
25761 && glyph->u.cmp.id == s->cmp_id
25762 && s->cmp_to == glyph->slice.cmp.from)
25763 s->cmp_to = (glyph++)->slice.cmp.to + 1;
25765 for (i = s->cmp_from; i < s->cmp_to; i++)
25767 Lisp_Object lglyph = LGSTRING_GLYPH (lgstring, i);
25768 unsigned code = LGLYPH_CODE (lglyph);
25770 STORE_XCHAR2B ((s->char2b + i), code >> 8, code & 0xFF);
25772 s->width = composition_gstring_width (lgstring, s->cmp_from, s->cmp_to, NULL);
25773 return glyph - s->row->glyphs[s->area];
25777 /* Fill glyph string S from a sequence glyphs for glyphless characters.
25778 See the comment of fill_glyph_string for arguments.
25779 Value is the index of the first glyph not in S. */
25782 static int
25783 fill_glyphless_glyph_string (struct glyph_string *s, int face_id,
25784 int start, int end, int overlaps)
25786 struct glyph *glyph, *last;
25787 int voffset;
25789 eassert (s->first_glyph->type == GLYPHLESS_GLYPH);
25790 s->for_overlaps = overlaps;
25791 glyph = s->row->glyphs[s->area] + start;
25792 last = s->row->glyphs[s->area] + end;
25793 voffset = glyph->voffset;
25794 s->face = FACE_FROM_ID (s->f, face_id);
25795 s->font = s->face->font ? s->face->font : FRAME_FONT (s->f);
25796 s->nchars = 1;
25797 s->width = glyph->pixel_width;
25798 glyph++;
25799 while (glyph < last
25800 && glyph->type == GLYPHLESS_GLYPH
25801 && glyph->voffset == voffset
25802 && glyph->face_id == face_id)
25804 s->nchars++;
25805 s->width += glyph->pixel_width;
25806 glyph++;
25808 s->ybase += voffset;
25809 return glyph - s->row->glyphs[s->area];
25813 /* Fill glyph string S from a sequence of character glyphs.
25815 FACE_ID is the face id of the string. START is the index of the
25816 first glyph to consider, END is the index of the last + 1.
25817 OVERLAPS non-zero means S should draw the foreground only, and use
25818 its physical height for clipping. See also draw_glyphs.
25820 Value is the index of the first glyph not in S. */
25822 static int
25823 fill_glyph_string (struct glyph_string *s, int face_id,
25824 int start, int end, int overlaps)
25826 struct glyph *glyph, *last;
25827 int voffset;
25828 bool glyph_not_available_p;
25830 eassert (s->f == XFRAME (s->w->frame));
25831 eassert (s->nchars == 0);
25832 eassert (start >= 0 && end > start);
25834 s->for_overlaps = overlaps;
25835 glyph = s->row->glyphs[s->area] + start;
25836 last = s->row->glyphs[s->area] + end;
25837 voffset = glyph->voffset;
25838 s->padding_p = glyph->padding_p;
25839 glyph_not_available_p = glyph->glyph_not_available_p;
25841 while (glyph < last
25842 && glyph->type == CHAR_GLYPH
25843 && glyph->voffset == voffset
25844 /* Same face id implies same font, nowadays. */
25845 && glyph->face_id == face_id
25846 && glyph->glyph_not_available_p == glyph_not_available_p)
25848 s->face = get_glyph_face_and_encoding (s->f, glyph,
25849 s->char2b + s->nchars);
25850 ++s->nchars;
25851 eassert (s->nchars <= end - start);
25852 s->width += glyph->pixel_width;
25853 if (glyph++->padding_p != s->padding_p)
25854 break;
25857 s->font = s->face->font;
25859 /* If the specified font could not be loaded, use the frame's font,
25860 but record the fact that we couldn't load it in
25861 S->font_not_found_p so that we can draw rectangles for the
25862 characters of the glyph string. */
25863 if (s->font == NULL || glyph_not_available_p)
25865 s->font_not_found_p = true;
25866 s->font = FRAME_FONT (s->f);
25869 /* Adjust base line for subscript/superscript text. */
25870 s->ybase += voffset;
25872 eassert (s->face && s->face->gc);
25873 return glyph - s->row->glyphs[s->area];
25877 /* Fill glyph string S from image glyph S->first_glyph. */
25879 static void
25880 fill_image_glyph_string (struct glyph_string *s)
25882 eassert (s->first_glyph->type == IMAGE_GLYPH);
25883 s->img = IMAGE_FROM_ID (s->f, s->first_glyph->u.img_id);
25884 eassert (s->img);
25885 s->slice = s->first_glyph->slice.img;
25886 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
25887 s->font = s->face->font;
25888 s->width = s->first_glyph->pixel_width;
25890 /* Adjust base line for subscript/superscript text. */
25891 s->ybase += s->first_glyph->voffset;
25895 #ifdef HAVE_XWIDGETS
25896 static void
25897 fill_xwidget_glyph_string (struct glyph_string *s)
25899 eassert (s->first_glyph->type == XWIDGET_GLYPH);
25900 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
25901 s->font = s->face->font;
25902 s->width = s->first_glyph->pixel_width;
25903 s->ybase += s->first_glyph->voffset;
25904 s->xwidget = s->first_glyph->u.xwidget;
25906 #endif
25907 /* Fill glyph string S from a sequence of stretch glyphs.
25909 START is the index of the first glyph to consider,
25910 END is the index of the last + 1.
25912 Value is the index of the first glyph not in S. */
25914 static int
25915 fill_stretch_glyph_string (struct glyph_string *s, int start, int end)
25917 struct glyph *glyph, *last;
25918 int voffset, face_id;
25920 eassert (s->first_glyph->type == STRETCH_GLYPH);
25922 glyph = s->row->glyphs[s->area] + start;
25923 last = s->row->glyphs[s->area] + end;
25924 face_id = glyph->face_id;
25925 s->face = FACE_FROM_ID (s->f, face_id);
25926 s->font = s->face->font;
25927 s->width = glyph->pixel_width;
25928 s->nchars = 1;
25929 voffset = glyph->voffset;
25931 for (++glyph;
25932 (glyph < last
25933 && glyph->type == STRETCH_GLYPH
25934 && glyph->voffset == voffset
25935 && glyph->face_id == face_id);
25936 ++glyph)
25937 s->width += glyph->pixel_width;
25939 /* Adjust base line for subscript/superscript text. */
25940 s->ybase += voffset;
25942 /* The case that face->gc == 0 is handled when drawing the glyph
25943 string by calling prepare_face_for_display. */
25944 eassert (s->face);
25945 return glyph - s->row->glyphs[s->area];
25948 static struct font_metrics *
25949 get_per_char_metric (struct font *font, XChar2b *char2b)
25951 static struct font_metrics metrics;
25952 unsigned code;
25954 if (! font)
25955 return NULL;
25956 code = (XCHAR2B_BYTE1 (char2b) << 8) | XCHAR2B_BYTE2 (char2b);
25957 if (code == FONT_INVALID_CODE)
25958 return NULL;
25959 font->driver->text_extents (font, &code, 1, &metrics);
25960 return &metrics;
25963 /* A subroutine that computes "normal" values of ASCENT and DESCENT
25964 for FONT. Values are taken from font-global ones, except for fonts
25965 that claim preposterously large values, but whose glyphs actually
25966 have reasonable dimensions. C is the character to use for metrics
25967 if the font-global values are too large; if C is negative, the
25968 function selects a default character. */
25969 static void
25970 normal_char_ascent_descent (struct font *font, int c, int *ascent, int *descent)
25972 *ascent = FONT_BASE (font);
25973 *descent = FONT_DESCENT (font);
25975 if (FONT_TOO_HIGH (font))
25977 XChar2b char2b;
25979 /* Get metrics of C, defaulting to a reasonably sized ASCII
25980 character. */
25981 if (get_char_glyph_code (c >= 0 ? c : '{', font, &char2b))
25983 struct font_metrics *pcm = get_per_char_metric (font, &char2b);
25985 if (!(pcm->width == 0 && pcm->rbearing == 0 && pcm->lbearing == 0))
25987 /* We add 1 pixel to character dimensions as heuristics
25988 that produces nicer display, e.g. when the face has
25989 the box attribute. */
25990 *ascent = pcm->ascent + 1;
25991 *descent = pcm->descent + 1;
25997 /* A subroutine that computes a reasonable "normal character height"
25998 for fonts that claim preposterously large vertical dimensions, but
25999 whose glyphs are actually reasonably sized. C is the character
26000 whose metrics to use for those fonts, or -1 for default
26001 character. */
26002 static int
26003 normal_char_height (struct font *font, int c)
26005 int ascent, descent;
26007 normal_char_ascent_descent (font, c, &ascent, &descent);
26009 return ascent + descent;
26012 /* EXPORT for RIF:
26013 Set *LEFT and *RIGHT to the left and right overhang of GLYPH on
26014 frame F. Overhangs of glyphs other than type CHAR_GLYPH are
26015 assumed to be zero. */
26017 void
26018 x_get_glyph_overhangs (struct glyph *glyph, struct frame *f, int *left, int *right)
26020 *left = *right = 0;
26022 if (glyph->type == CHAR_GLYPH)
26024 XChar2b char2b;
26025 struct face *face = get_glyph_face_and_encoding (f, glyph, &char2b);
26026 if (face->font)
26028 struct font_metrics *pcm = get_per_char_metric (face->font, &char2b);
26029 if (pcm)
26031 if (pcm->rbearing > pcm->width)
26032 *right = pcm->rbearing - pcm->width;
26033 if (pcm->lbearing < 0)
26034 *left = -pcm->lbearing;
26038 else if (glyph->type == COMPOSITE_GLYPH)
26040 if (! glyph->u.cmp.automatic)
26042 struct composition *cmp = composition_table[glyph->u.cmp.id];
26044 if (cmp->rbearing > cmp->pixel_width)
26045 *right = cmp->rbearing - cmp->pixel_width;
26046 if (cmp->lbearing < 0)
26047 *left = - cmp->lbearing;
26049 else
26051 Lisp_Object gstring = composition_gstring_from_id (glyph->u.cmp.id);
26052 struct font_metrics metrics;
26054 composition_gstring_width (gstring, glyph->slice.cmp.from,
26055 glyph->slice.cmp.to + 1, &metrics);
26056 if (metrics.rbearing > metrics.width)
26057 *right = metrics.rbearing - metrics.width;
26058 if (metrics.lbearing < 0)
26059 *left = - metrics.lbearing;
26065 /* Return the index of the first glyph preceding glyph string S that
26066 is overwritten by S because of S's left overhang. Value is -1
26067 if no glyphs are overwritten. */
26069 static int
26070 left_overwritten (struct glyph_string *s)
26072 int k;
26074 if (s->left_overhang)
26076 int x = 0, i;
26077 struct glyph *glyphs = s->row->glyphs[s->area];
26078 int first = s->first_glyph - glyphs;
26080 for (i = first - 1; i >= 0 && x > -s->left_overhang; --i)
26081 x -= glyphs[i].pixel_width;
26083 k = i + 1;
26085 else
26086 k = -1;
26088 return k;
26092 /* Return the index of the first glyph preceding glyph string S that
26093 is overwriting S because of its right overhang. Value is -1 if no
26094 glyph in front of S overwrites S. */
26096 static int
26097 left_overwriting (struct glyph_string *s)
26099 int i, k, x;
26100 struct glyph *glyphs = s->row->glyphs[s->area];
26101 int first = s->first_glyph - glyphs;
26103 k = -1;
26104 x = 0;
26105 for (i = first - 1; i >= 0; --i)
26107 int left, right;
26108 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
26109 if (x + right > 0)
26110 k = i;
26111 x -= glyphs[i].pixel_width;
26114 return k;
26118 /* Return the index of the last glyph following glyph string S that is
26119 overwritten by S because of S's right overhang. Value is -1 if
26120 no such glyph is found. */
26122 static int
26123 right_overwritten (struct glyph_string *s)
26125 int k = -1;
26127 if (s->right_overhang)
26129 int x = 0, i;
26130 struct glyph *glyphs = s->row->glyphs[s->area];
26131 int first = (s->first_glyph - glyphs
26132 + (s->first_glyph->type == COMPOSITE_GLYPH ? 1 : s->nchars));
26133 int end = s->row->used[s->area];
26135 for (i = first; i < end && s->right_overhang > x; ++i)
26136 x += glyphs[i].pixel_width;
26138 k = i;
26141 return k;
26145 /* Return the index of the last glyph following glyph string S that
26146 overwrites S because of its left overhang. Value is negative
26147 if no such glyph is found. */
26149 static int
26150 right_overwriting (struct glyph_string *s)
26152 int i, k, x;
26153 int end = s->row->used[s->area];
26154 struct glyph *glyphs = s->row->glyphs[s->area];
26155 int first = (s->first_glyph - glyphs
26156 + (s->first_glyph->type == COMPOSITE_GLYPH ? 1 : s->nchars));
26158 k = -1;
26159 x = 0;
26160 for (i = first; i < end; ++i)
26162 int left, right;
26163 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
26164 if (x - left < 0)
26165 k = i;
26166 x += glyphs[i].pixel_width;
26169 return k;
26173 /* Set background width of glyph string S. START is the index of the
26174 first glyph following S. LAST_X is the right-most x-position + 1
26175 in the drawing area. */
26177 static void
26178 set_glyph_string_background_width (struct glyph_string *s, int start, int last_x)
26180 /* If the face of this glyph string has to be drawn to the end of
26181 the drawing area, set S->extends_to_end_of_line_p. */
26183 if (start == s->row->used[s->area]
26184 && ((s->row->fill_line_p
26185 && (s->hl == DRAW_NORMAL_TEXT
26186 || s->hl == DRAW_IMAGE_RAISED
26187 || s->hl == DRAW_IMAGE_SUNKEN))
26188 || s->hl == DRAW_MOUSE_FACE))
26189 s->extends_to_end_of_line_p = true;
26191 /* If S extends its face to the end of the line, set its
26192 background_width to the distance to the right edge of the drawing
26193 area. */
26194 if (s->extends_to_end_of_line_p)
26195 s->background_width = last_x - s->x + 1;
26196 else
26197 s->background_width = s->width;
26201 /* Return glyph string that shares background with glyph string S and
26202 whose `background_width' member has been set. */
26204 static struct glyph_string *
26205 glyph_string_containing_background_width (struct glyph_string *s)
26207 if (s->cmp)
26208 while (s->cmp_from)
26209 s = s->prev;
26211 return s;
26215 /* Compute overhangs and x-positions for glyph string S and its
26216 predecessors, or successors. X is the starting x-position for S.
26217 BACKWARD_P means process predecessors. */
26219 static void
26220 compute_overhangs_and_x (struct glyph_string *s, int x, bool backward_p)
26222 if (backward_p)
26224 while (s)
26226 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
26227 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
26228 if (!s->cmp || s->cmp_to == s->cmp->glyph_len)
26229 x -= s->width;
26230 s->x = x;
26231 s = s->prev;
26234 else
26236 while (s)
26238 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
26239 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
26240 s->x = x;
26241 if (!s->cmp || s->cmp_to == s->cmp->glyph_len)
26242 x += s->width;
26243 s = s->next;
26250 /* The following macros are only called from draw_glyphs below.
26251 They reference the following parameters of that function directly:
26252 `w', `row', `area', and `overlap_p'
26253 as well as the following local variables:
26254 `s', `f', and `hdc' (in W32) */
26256 #ifdef HAVE_NTGUI
26257 /* On W32, silently add local `hdc' variable to argument list of
26258 init_glyph_string. */
26259 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
26260 init_glyph_string (s, hdc, char2b, w, row, area, start, hl)
26261 #else
26262 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
26263 init_glyph_string (s, char2b, w, row, area, start, hl)
26264 #endif
26266 /* Add a glyph string for a stretch glyph to the list of strings
26267 between HEAD and TAIL. START is the index of the stretch glyph in
26268 row area AREA of glyph row ROW. END is the index of the last glyph
26269 in that glyph row area. X is the current output position assigned
26270 to the new glyph string constructed. HL overrides that face of the
26271 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
26272 is the right-most x-position of the drawing area. */
26274 /* SunOS 4 bundled cc, barfed on continuations in the arg lists here
26275 and below -- keep them on one line. */
26276 #define BUILD_STRETCH_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
26277 do \
26279 s = alloca (sizeof *s); \
26280 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
26281 START = fill_stretch_glyph_string (s, START, END); \
26282 append_glyph_string (&HEAD, &TAIL, s); \
26283 s->x = (X); \
26285 while (false)
26288 /* Add a glyph string for an image glyph to the list of strings
26289 between HEAD and TAIL. START is the index of the image glyph in
26290 row area AREA of glyph row ROW. END is the index of the last glyph
26291 in that glyph row area. X is the current output position assigned
26292 to the new glyph string constructed. HL overrides that face of the
26293 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
26294 is the right-most x-position of the drawing area. */
26296 #define BUILD_IMAGE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
26297 do \
26299 s = alloca (sizeof *s); \
26300 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
26301 fill_image_glyph_string (s); \
26302 append_glyph_string (&HEAD, &TAIL, s); \
26303 ++START; \
26304 s->x = (X); \
26306 while (false)
26308 #ifndef HAVE_XWIDGETS
26309 # define BUILD_XWIDGET_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
26310 eassume (false)
26311 #else
26312 # define BUILD_XWIDGET_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
26313 do \
26315 s = alloca (sizeof *s); \
26316 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
26317 fill_xwidget_glyph_string (s); \
26318 append_glyph_string (&(HEAD), &(TAIL), s); \
26319 ++(START); \
26320 s->x = (X); \
26322 while (false)
26323 #endif
26325 /* Add a glyph string for a sequence of character glyphs to the list
26326 of strings between HEAD and TAIL. START is the index of the first
26327 glyph in row area AREA of glyph row ROW that is part of the new
26328 glyph string. END is the index of the last glyph in that glyph row
26329 area. X is the current output position assigned to the new glyph
26330 string constructed. HL overrides that face of the glyph; e.g. it
26331 is DRAW_CURSOR if a cursor has to be drawn. LAST_X is the
26332 right-most x-position of the drawing area. */
26334 #define BUILD_CHAR_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
26335 do \
26337 int face_id; \
26338 XChar2b *char2b; \
26340 face_id = (row)->glyphs[area][START].face_id; \
26342 s = alloca (sizeof *s); \
26343 SAFE_NALLOCA (char2b, 1, (END) - (START)); \
26344 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
26345 append_glyph_string (&HEAD, &TAIL, s); \
26346 s->x = (X); \
26347 START = fill_glyph_string (s, face_id, START, END, overlaps); \
26349 while (false)
26352 /* Add a glyph string for a composite sequence to the list of strings
26353 between HEAD and TAIL. START is the index of the first glyph in
26354 row area AREA of glyph row ROW that is part of the new glyph
26355 string. END is the index of the last glyph in that glyph row area.
26356 X is the current output position assigned to the new glyph string
26357 constructed. HL overrides that face of the glyph; e.g. it is
26358 DRAW_CURSOR if a cursor has to be drawn. LAST_X is the right-most
26359 x-position of the drawing area. */
26361 #define BUILD_COMPOSITE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
26362 do { \
26363 int face_id = (row)->glyphs[area][START].face_id; \
26364 struct face *base_face = FACE_FROM_ID (f, face_id); \
26365 ptrdiff_t cmp_id = (row)->glyphs[area][START].u.cmp.id; \
26366 struct composition *cmp = composition_table[cmp_id]; \
26367 XChar2b *char2b; \
26368 struct glyph_string *first_s = NULL; \
26369 int n; \
26371 SAFE_NALLOCA (char2b, 1, cmp->glyph_len); \
26373 /* Make glyph_strings for each glyph sequence that is drawable by \
26374 the same face, and append them to HEAD/TAIL. */ \
26375 for (n = 0; n < cmp->glyph_len;) \
26377 s = alloca (sizeof *s); \
26378 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
26379 append_glyph_string (&(HEAD), &(TAIL), s); \
26380 s->cmp = cmp; \
26381 s->cmp_from = n; \
26382 s->x = (X); \
26383 if (n == 0) \
26384 first_s = s; \
26385 n = fill_composite_glyph_string (s, base_face, overlaps); \
26388 ++START; \
26389 s = first_s; \
26390 } while (false)
26393 /* Add a glyph string for a glyph-string sequence to the list of strings
26394 between HEAD and TAIL. */
26396 #define BUILD_GSTRING_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
26397 do { \
26398 int face_id; \
26399 XChar2b *char2b; \
26400 Lisp_Object gstring; \
26402 face_id = (row)->glyphs[area][START].face_id; \
26403 gstring = (composition_gstring_from_id \
26404 ((row)->glyphs[area][START].u.cmp.id)); \
26405 s = alloca (sizeof *s); \
26406 SAFE_NALLOCA (char2b, 1, LGSTRING_GLYPH_LEN (gstring)); \
26407 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
26408 append_glyph_string (&(HEAD), &(TAIL), s); \
26409 s->x = (X); \
26410 START = fill_gstring_glyph_string (s, face_id, START, END, overlaps); \
26411 } while (false)
26414 /* Add a glyph string for a sequence of glyphless character's glyphs
26415 to the list of strings between HEAD and TAIL. The meanings of
26416 arguments are the same as those of BUILD_CHAR_GLYPH_STRINGS. */
26418 #define BUILD_GLYPHLESS_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
26419 do \
26421 int face_id; \
26423 face_id = (row)->glyphs[area][START].face_id; \
26425 s = alloca (sizeof *s); \
26426 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
26427 append_glyph_string (&HEAD, &TAIL, s); \
26428 s->x = (X); \
26429 START = fill_glyphless_glyph_string (s, face_id, START, END, \
26430 overlaps); \
26432 while (false)
26435 /* Build a list of glyph strings between HEAD and TAIL for the glyphs
26436 of AREA of glyph row ROW on window W between indices START and END.
26437 HL overrides the face for drawing glyph strings, e.g. it is
26438 DRAW_CURSOR to draw a cursor. X and LAST_X are start and end
26439 x-positions of the drawing area.
26441 This is an ugly monster macro construct because we must use alloca
26442 to allocate glyph strings (because draw_glyphs can be called
26443 asynchronously). */
26445 #define BUILD_GLYPH_STRINGS_1(START, END, HEAD, TAIL, HL, X, LAST_X) \
26446 do \
26448 HEAD = TAIL = NULL; \
26449 while (START < END) \
26451 struct glyph *first_glyph = (row)->glyphs[area] + START; \
26452 switch (first_glyph->type) \
26454 case CHAR_GLYPH: \
26455 BUILD_CHAR_GLYPH_STRINGS (START, END, HEAD, TAIL, \
26456 HL, X, LAST_X); \
26457 break; \
26459 case COMPOSITE_GLYPH: \
26460 if (first_glyph->u.cmp.automatic) \
26461 BUILD_GSTRING_GLYPH_STRING (START, END, HEAD, TAIL, \
26462 HL, X, LAST_X); \
26463 else \
26464 BUILD_COMPOSITE_GLYPH_STRING (START, END, HEAD, TAIL, \
26465 HL, X, LAST_X); \
26466 break; \
26468 case STRETCH_GLYPH: \
26469 BUILD_STRETCH_GLYPH_STRING (START, END, HEAD, TAIL, \
26470 HL, X, LAST_X); \
26471 break; \
26473 case IMAGE_GLYPH: \
26474 BUILD_IMAGE_GLYPH_STRING (START, END, HEAD, TAIL, \
26475 HL, X, LAST_X); \
26476 break;
26478 #define BUILD_GLYPH_STRINGS_XW(START, END, HEAD, TAIL, HL, X, LAST_X) \
26479 case XWIDGET_GLYPH: \
26480 BUILD_XWIDGET_GLYPH_STRING (START, END, HEAD, TAIL, \
26481 HL, X, LAST_X); \
26482 break;
26484 #define BUILD_GLYPH_STRINGS_2(START, END, HEAD, TAIL, HL, X, LAST_X) \
26485 case GLYPHLESS_GLYPH: \
26486 BUILD_GLYPHLESS_GLYPH_STRING (START, END, HEAD, TAIL, \
26487 HL, X, LAST_X); \
26488 break; \
26490 default: \
26491 emacs_abort (); \
26494 if (s) \
26496 set_glyph_string_background_width (s, START, LAST_X); \
26497 (X) += s->width; \
26500 } while (false)
26503 #define BUILD_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
26504 BUILD_GLYPH_STRINGS_1(START, END, HEAD, TAIL, HL, X, LAST_X) \
26505 BUILD_GLYPH_STRINGS_XW(START, END, HEAD, TAIL, HL, X, LAST_X) \
26506 BUILD_GLYPH_STRINGS_2(START, END, HEAD, TAIL, HL, X, LAST_X)
26509 /* Draw glyphs between START and END in AREA of ROW on window W,
26510 starting at x-position X. X is relative to AREA in W. HL is a
26511 face-override with the following meaning:
26513 DRAW_NORMAL_TEXT draw normally
26514 DRAW_CURSOR draw in cursor face
26515 DRAW_MOUSE_FACE draw in mouse face.
26516 DRAW_INVERSE_VIDEO draw in mode line face
26517 DRAW_IMAGE_SUNKEN draw an image with a sunken relief around it
26518 DRAW_IMAGE_RAISED draw an image with a raised relief around it
26520 If OVERLAPS is non-zero, draw only the foreground of characters and
26521 clip to the physical height of ROW. Non-zero value also defines
26522 the overlapping part to be drawn:
26524 OVERLAPS_PRED overlap with preceding rows
26525 OVERLAPS_SUCC overlap with succeeding rows
26526 OVERLAPS_BOTH overlap with both preceding/succeeding rows
26527 OVERLAPS_ERASED_CURSOR overlap with erased cursor area
26529 Value is the x-position reached, relative to AREA of W. */
26531 static int
26532 draw_glyphs (struct window *w, int x, struct glyph_row *row,
26533 enum glyph_row_area area, ptrdiff_t start, ptrdiff_t end,
26534 enum draw_glyphs_face hl, int overlaps)
26536 struct glyph_string *head, *tail;
26537 struct glyph_string *s;
26538 struct glyph_string *clip_head = NULL, *clip_tail = NULL;
26539 int i, j, x_reached, last_x, area_left = 0;
26540 struct frame *f = XFRAME (WINDOW_FRAME (w));
26541 DECLARE_HDC (hdc);
26543 ALLOCATE_HDC (hdc, f);
26545 /* Let's rather be paranoid than getting a SEGV. */
26546 end = min (end, row->used[area]);
26547 start = clip_to_bounds (0, start, end);
26549 /* Translate X to frame coordinates. Set last_x to the right
26550 end of the drawing area. */
26551 if (row->full_width_p)
26553 /* X is relative to the left edge of W, without scroll bars
26554 or fringes. */
26555 area_left = WINDOW_LEFT_EDGE_X (w);
26556 last_x = (WINDOW_LEFT_EDGE_X (w) + WINDOW_PIXEL_WIDTH (w)
26557 - (row->mode_line_p ? WINDOW_RIGHT_DIVIDER_WIDTH (w) : 0));
26559 else
26561 area_left = window_box_left (w, area);
26562 last_x = area_left + window_box_width (w, area);
26564 x += area_left;
26566 /* Build a doubly-linked list of glyph_string structures between
26567 head and tail from what we have to draw. Note that the macro
26568 BUILD_GLYPH_STRINGS will modify its start parameter. That's
26569 the reason we use a separate variable `i'. */
26570 i = start;
26571 USE_SAFE_ALLOCA;
26572 BUILD_GLYPH_STRINGS (i, end, head, tail, hl, x, last_x);
26573 if (tail)
26575 s = glyph_string_containing_background_width (tail);
26576 x_reached = s->x + s->background_width;
26578 else
26579 x_reached = x;
26581 /* If there are any glyphs with lbearing < 0 or rbearing > width in
26582 the row, redraw some glyphs in front or following the glyph
26583 strings built above. */
26584 if (head && !overlaps && row->contains_overlapping_glyphs_p)
26586 struct glyph_string *h, *t;
26587 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
26588 int mouse_beg_col UNINIT, mouse_end_col UNINIT;
26589 bool check_mouse_face = false;
26590 int dummy_x = 0;
26592 /* If mouse highlighting is on, we may need to draw adjacent
26593 glyphs using mouse-face highlighting. */
26594 if (area == TEXT_AREA && row->mouse_face_p
26595 && hlinfo->mouse_face_beg_row >= 0
26596 && hlinfo->mouse_face_end_row >= 0)
26598 ptrdiff_t row_vpos = MATRIX_ROW_VPOS (row, w->current_matrix);
26600 if (row_vpos >= hlinfo->mouse_face_beg_row
26601 && row_vpos <= hlinfo->mouse_face_end_row)
26603 check_mouse_face = true;
26604 mouse_beg_col = (row_vpos == hlinfo->mouse_face_beg_row)
26605 ? hlinfo->mouse_face_beg_col : 0;
26606 mouse_end_col = (row_vpos == hlinfo->mouse_face_end_row)
26607 ? hlinfo->mouse_face_end_col
26608 : row->used[TEXT_AREA];
26612 /* Compute overhangs for all glyph strings. */
26613 if (FRAME_RIF (f)->compute_glyph_string_overhangs)
26614 for (s = head; s; s = s->next)
26615 FRAME_RIF (f)->compute_glyph_string_overhangs (s);
26617 /* Prepend glyph strings for glyphs in front of the first glyph
26618 string that are overwritten because of the first glyph
26619 string's left overhang. The background of all strings
26620 prepended must be drawn because the first glyph string
26621 draws over it. */
26622 i = left_overwritten (head);
26623 if (i >= 0)
26625 enum draw_glyphs_face overlap_hl;
26627 /* If this row contains mouse highlighting, attempt to draw
26628 the overlapped glyphs with the correct highlight. This
26629 code fails if the overlap encompasses more than one glyph
26630 and mouse-highlight spans only some of these glyphs.
26631 However, making it work perfectly involves a lot more
26632 code, and I don't know if the pathological case occurs in
26633 practice, so we'll stick to this for now. --- cyd */
26634 if (check_mouse_face
26635 && mouse_beg_col < start && mouse_end_col > i)
26636 overlap_hl = DRAW_MOUSE_FACE;
26637 else
26638 overlap_hl = DRAW_NORMAL_TEXT;
26640 if (hl != overlap_hl)
26641 clip_head = head;
26642 j = i;
26643 BUILD_GLYPH_STRINGS (j, start, h, t,
26644 overlap_hl, dummy_x, last_x);
26645 start = i;
26646 compute_overhangs_and_x (t, head->x, true);
26647 prepend_glyph_string_lists (&head, &tail, h, t);
26648 if (clip_head == NULL)
26649 clip_head = head;
26652 /* Prepend glyph strings for glyphs in front of the first glyph
26653 string that overwrite that glyph string because of their
26654 right overhang. For these strings, only the foreground must
26655 be drawn, because it draws over the glyph string at `head'.
26656 The background must not be drawn because this would overwrite
26657 right overhangs of preceding glyphs for which no glyph
26658 strings exist. */
26659 i = left_overwriting (head);
26660 if (i >= 0)
26662 enum draw_glyphs_face overlap_hl;
26664 if (check_mouse_face
26665 && mouse_beg_col < start && mouse_end_col > i)
26666 overlap_hl = DRAW_MOUSE_FACE;
26667 else
26668 overlap_hl = DRAW_NORMAL_TEXT;
26670 if (hl == overlap_hl || clip_head == NULL)
26671 clip_head = head;
26672 BUILD_GLYPH_STRINGS (i, start, h, t,
26673 overlap_hl, dummy_x, last_x);
26674 for (s = h; s; s = s->next)
26675 s->background_filled_p = true;
26676 compute_overhangs_and_x (t, head->x, true);
26677 prepend_glyph_string_lists (&head, &tail, h, t);
26680 /* Append glyphs strings for glyphs following the last glyph
26681 string tail that are overwritten by tail. The background of
26682 these strings has to be drawn because tail's foreground draws
26683 over it. */
26684 i = right_overwritten (tail);
26685 if (i >= 0)
26687 enum draw_glyphs_face overlap_hl;
26689 if (check_mouse_face
26690 && mouse_beg_col < i && mouse_end_col > end)
26691 overlap_hl = DRAW_MOUSE_FACE;
26692 else
26693 overlap_hl = DRAW_NORMAL_TEXT;
26695 if (hl != overlap_hl)
26696 clip_tail = tail;
26697 BUILD_GLYPH_STRINGS (end, i, h, t,
26698 overlap_hl, x, last_x);
26699 /* Because BUILD_GLYPH_STRINGS updates the first argument,
26700 we don't have `end = i;' here. */
26701 compute_overhangs_and_x (h, tail->x + tail->width, false);
26702 append_glyph_string_lists (&head, &tail, h, t);
26703 if (clip_tail == NULL)
26704 clip_tail = tail;
26707 /* Append glyph strings for glyphs following the last glyph
26708 string tail that overwrite tail. The foreground of such
26709 glyphs has to be drawn because it writes into the background
26710 of tail. The background must not be drawn because it could
26711 paint over the foreground of following glyphs. */
26712 i = right_overwriting (tail);
26713 if (i >= 0)
26715 enum draw_glyphs_face overlap_hl;
26716 if (check_mouse_face
26717 && mouse_beg_col < i && mouse_end_col > end)
26718 overlap_hl = DRAW_MOUSE_FACE;
26719 else
26720 overlap_hl = DRAW_NORMAL_TEXT;
26722 if (hl == overlap_hl || clip_tail == NULL)
26723 clip_tail = tail;
26724 i++; /* We must include the Ith glyph. */
26725 BUILD_GLYPH_STRINGS (end, i, h, t,
26726 overlap_hl, x, last_x);
26727 for (s = h; s; s = s->next)
26728 s->background_filled_p = true;
26729 compute_overhangs_and_x (h, tail->x + tail->width, false);
26730 append_glyph_string_lists (&head, &tail, h, t);
26732 tail = glyph_string_containing_background_width (tail);
26733 if (clip_tail)
26734 clip_tail = glyph_string_containing_background_width (clip_tail);
26735 if (clip_head || clip_tail)
26736 for (s = head; s; s = s->next)
26738 s->clip_head = clip_head;
26739 s->clip_tail = clip_tail;
26743 /* Draw all strings. */
26744 for (s = head; s; s = s->next)
26745 FRAME_RIF (f)->draw_glyph_string (s);
26747 #ifndef HAVE_NS
26748 /* When focus a sole frame and move horizontally, this clears on_p
26749 causing a failure to erase prev cursor position. */
26750 if (area == TEXT_AREA
26751 && !row->full_width_p
26752 /* When drawing overlapping rows, only the glyph strings'
26753 foreground is drawn, which doesn't erase a cursor
26754 completely. */
26755 && !overlaps)
26757 int x0 = clip_head ? clip_head->x : (head ? head->x : x);
26758 int x1 = (clip_tail ? clip_tail->x + clip_tail->background_width
26759 : (tail ? tail->x + tail->background_width : x));
26760 x0 -= area_left;
26761 x1 -= area_left;
26763 notice_overwritten_cursor (w, TEXT_AREA, x0, x1,
26764 row->y, MATRIX_ROW_BOTTOM_Y (row));
26766 #endif
26768 /* Value is the x-position up to which drawn, relative to AREA of W.
26769 This doesn't include parts drawn because of overhangs. */
26770 if (row->full_width_p)
26771 x_reached = FRAME_TO_WINDOW_PIXEL_X (w, x_reached);
26772 else
26773 x_reached -= area_left;
26775 RELEASE_HDC (hdc, f);
26777 SAFE_FREE ();
26778 return x_reached;
26781 /* Find the first glyph in the run of underlined glyphs preceding the
26782 beginning of glyph string S, and return its font (which could be
26783 NULL). This is needed because that font determines the underline
26784 position and thickness for the entire run of the underlined glyphs.
26785 This function is called from the draw_glyph_string method of GUI
26786 frame's redisplay interface (RIF) when it needs to draw in an
26787 underlined face. */
26788 struct font *
26789 font_for_underline_metrics (struct glyph_string *s)
26791 struct glyph *g0 = s->row->glyphs[s->area], *g;
26793 for (g = s->first_glyph - 1; g >= g0; g--)
26795 struct face *prev_face = FACE_FROM_ID (s->f, g->face_id);
26796 if (!(prev_face && prev_face->underline_p))
26797 break;
26800 /* If preceding glyphs are not underlined, use the font of S. */
26801 if (g == s->first_glyph - 1)
26802 return s->font;
26803 else
26805 /* Otherwise use the font of the last glyph we saw in the above
26806 loop whose face had the underline_p flag set. */
26807 return FACE_FROM_ID (s->f, g[1].face_id)->font;
26811 /* Expand row matrix if too narrow. Don't expand if area
26812 is not present. */
26814 #define IT_EXPAND_MATRIX_WIDTH(it, area) \
26816 if (!it->f->fonts_changed \
26817 && (it->glyph_row->glyphs[area] \
26818 < it->glyph_row->glyphs[area + 1])) \
26820 it->w->ncols_scale_factor++; \
26821 it->f->fonts_changed = true; \
26825 /* Store one glyph for IT->char_to_display in IT->glyph_row.
26826 Called from x_produce_glyphs when IT->glyph_row is non-null. */
26828 static void
26829 append_glyph (struct it *it)
26831 struct glyph *glyph;
26832 enum glyph_row_area area = it->area;
26834 eassert (it->glyph_row);
26835 eassert (it->char_to_display != '\n' && it->char_to_display != '\t');
26837 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
26838 if (glyph < it->glyph_row->glyphs[area + 1])
26840 /* If the glyph row is reversed, we need to prepend the glyph
26841 rather than append it. */
26842 if (it->glyph_row->reversed_p && area == TEXT_AREA)
26844 struct glyph *g;
26846 /* Make room for the additional glyph. */
26847 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
26848 g[1] = *g;
26849 glyph = it->glyph_row->glyphs[area];
26851 glyph->charpos = CHARPOS (it->position);
26852 glyph->object = it->object;
26853 if (it->pixel_width > 0)
26855 eassert (it->pixel_width <= SHRT_MAX);
26856 glyph->pixel_width = it->pixel_width;
26857 glyph->padding_p = false;
26859 else
26861 /* Assure at least 1-pixel width. Otherwise, cursor can't
26862 be displayed correctly. */
26863 glyph->pixel_width = 1;
26864 glyph->padding_p = true;
26866 glyph->ascent = it->ascent;
26867 glyph->descent = it->descent;
26868 glyph->voffset = it->voffset;
26869 glyph->type = CHAR_GLYPH;
26870 glyph->avoid_cursor_p = it->avoid_cursor_p;
26871 glyph->multibyte_p = it->multibyte_p;
26872 if (it->glyph_row->reversed_p && area == TEXT_AREA)
26874 /* In R2L rows, the left and the right box edges need to be
26875 drawn in reverse direction. */
26876 glyph->right_box_line_p = it->start_of_box_run_p;
26877 glyph->left_box_line_p = it->end_of_box_run_p;
26879 else
26881 glyph->left_box_line_p = it->start_of_box_run_p;
26882 glyph->right_box_line_p = it->end_of_box_run_p;
26884 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
26885 || it->phys_descent > it->descent);
26886 glyph->glyph_not_available_p = it->glyph_not_available_p;
26887 glyph->face_id = it->face_id;
26888 glyph->u.ch = it->char_to_display;
26889 glyph->slice.img = null_glyph_slice;
26890 glyph->font_type = FONT_TYPE_UNKNOWN;
26891 if (it->bidi_p)
26893 glyph->resolved_level = it->bidi_it.resolved_level;
26894 eassert ((it->bidi_it.type & 7) == it->bidi_it.type);
26895 glyph->bidi_type = it->bidi_it.type;
26897 else
26899 glyph->resolved_level = 0;
26900 glyph->bidi_type = UNKNOWN_BT;
26902 ++it->glyph_row->used[area];
26904 else
26905 IT_EXPAND_MATRIX_WIDTH (it, area);
26908 /* Store one glyph for the composition IT->cmp_it.id in
26909 IT->glyph_row. Called from x_produce_glyphs when IT->glyph_row is
26910 non-null. */
26912 static void
26913 append_composite_glyph (struct it *it)
26915 struct glyph *glyph;
26916 enum glyph_row_area area = it->area;
26918 eassert (it->glyph_row);
26920 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
26921 if (glyph < it->glyph_row->glyphs[area + 1])
26923 /* If the glyph row is reversed, we need to prepend the glyph
26924 rather than append it. */
26925 if (it->glyph_row->reversed_p && it->area == TEXT_AREA)
26927 struct glyph *g;
26929 /* Make room for the new glyph. */
26930 for (g = glyph - 1; g >= it->glyph_row->glyphs[it->area]; g--)
26931 g[1] = *g;
26932 glyph = it->glyph_row->glyphs[it->area];
26934 glyph->charpos = it->cmp_it.charpos;
26935 glyph->object = it->object;
26936 eassert (it->pixel_width <= SHRT_MAX);
26937 glyph->pixel_width = it->pixel_width;
26938 glyph->ascent = it->ascent;
26939 glyph->descent = it->descent;
26940 glyph->voffset = it->voffset;
26941 glyph->type = COMPOSITE_GLYPH;
26942 if (it->cmp_it.ch < 0)
26944 glyph->u.cmp.automatic = false;
26945 glyph->u.cmp.id = it->cmp_it.id;
26946 glyph->slice.cmp.from = glyph->slice.cmp.to = 0;
26948 else
26950 glyph->u.cmp.automatic = true;
26951 glyph->u.cmp.id = it->cmp_it.id;
26952 glyph->slice.cmp.from = it->cmp_it.from;
26953 glyph->slice.cmp.to = it->cmp_it.to - 1;
26955 glyph->avoid_cursor_p = it->avoid_cursor_p;
26956 glyph->multibyte_p = it->multibyte_p;
26957 if (it->glyph_row->reversed_p && area == TEXT_AREA)
26959 /* In R2L rows, the left and the right box edges need to be
26960 drawn in reverse direction. */
26961 glyph->right_box_line_p = it->start_of_box_run_p;
26962 glyph->left_box_line_p = it->end_of_box_run_p;
26964 else
26966 glyph->left_box_line_p = it->start_of_box_run_p;
26967 glyph->right_box_line_p = it->end_of_box_run_p;
26969 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
26970 || it->phys_descent > it->descent);
26971 glyph->padding_p = false;
26972 glyph->glyph_not_available_p = false;
26973 glyph->face_id = it->face_id;
26974 glyph->font_type = FONT_TYPE_UNKNOWN;
26975 if (it->bidi_p)
26977 glyph->resolved_level = it->bidi_it.resolved_level;
26978 eassert ((it->bidi_it.type & 7) == it->bidi_it.type);
26979 glyph->bidi_type = it->bidi_it.type;
26981 ++it->glyph_row->used[area];
26983 else
26984 IT_EXPAND_MATRIX_WIDTH (it, area);
26988 /* Change IT->ascent and IT->height according to the setting of
26989 IT->voffset. */
26991 static void
26992 take_vertical_position_into_account (struct it *it)
26994 if (it->voffset)
26996 if (it->voffset < 0)
26997 /* Increase the ascent so that we can display the text higher
26998 in the line. */
26999 it->ascent -= it->voffset;
27000 else
27001 /* Increase the descent so that we can display the text lower
27002 in the line. */
27003 it->descent += it->voffset;
27008 /* Produce glyphs/get display metrics for the image IT is loaded with.
27009 See the description of struct display_iterator in dispextern.h for
27010 an overview of struct display_iterator. */
27012 static void
27013 produce_image_glyph (struct it *it)
27015 struct image *img;
27016 struct face *face;
27017 int glyph_ascent, crop;
27018 struct glyph_slice slice;
27020 eassert (it->what == IT_IMAGE);
27022 face = FACE_FROM_ID (it->f, it->face_id);
27023 /* Make sure X resources of the face is loaded. */
27024 prepare_face_for_display (it->f, face);
27026 if (it->image_id < 0)
27028 /* Fringe bitmap. */
27029 it->ascent = it->phys_ascent = 0;
27030 it->descent = it->phys_descent = 0;
27031 it->pixel_width = 0;
27032 it->nglyphs = 0;
27033 return;
27036 img = IMAGE_FROM_ID (it->f, it->image_id);
27037 /* Make sure X resources of the image is loaded. */
27038 prepare_image_for_display (it->f, img);
27040 slice.x = slice.y = 0;
27041 slice.width = img->width;
27042 slice.height = img->height;
27044 if (INTEGERP (it->slice.x))
27045 slice.x = XINT (it->slice.x);
27046 else if (FLOATP (it->slice.x))
27047 slice.x = XFLOAT_DATA (it->slice.x) * img->width;
27049 if (INTEGERP (it->slice.y))
27050 slice.y = XINT (it->slice.y);
27051 else if (FLOATP (it->slice.y))
27052 slice.y = XFLOAT_DATA (it->slice.y) * img->height;
27054 if (INTEGERP (it->slice.width))
27055 slice.width = XINT (it->slice.width);
27056 else if (FLOATP (it->slice.width))
27057 slice.width = XFLOAT_DATA (it->slice.width) * img->width;
27059 if (INTEGERP (it->slice.height))
27060 slice.height = XINT (it->slice.height);
27061 else if (FLOATP (it->slice.height))
27062 slice.height = XFLOAT_DATA (it->slice.height) * img->height;
27064 if (slice.x >= img->width)
27065 slice.x = img->width;
27066 if (slice.y >= img->height)
27067 slice.y = img->height;
27068 if (slice.x + slice.width >= img->width)
27069 slice.width = img->width - slice.x;
27070 if (slice.y + slice.height > img->height)
27071 slice.height = img->height - slice.y;
27073 if (slice.width == 0 || slice.height == 0)
27074 return;
27076 it->ascent = it->phys_ascent = glyph_ascent = image_ascent (img, face, &slice);
27078 it->descent = slice.height - glyph_ascent;
27079 if (slice.y == 0)
27080 it->descent += img->vmargin;
27081 if (slice.y + slice.height == img->height)
27082 it->descent += img->vmargin;
27083 it->phys_descent = it->descent;
27085 it->pixel_width = slice.width;
27086 if (slice.x == 0)
27087 it->pixel_width += img->hmargin;
27088 if (slice.x + slice.width == img->width)
27089 it->pixel_width += img->hmargin;
27091 /* It's quite possible for images to have an ascent greater than
27092 their height, so don't get confused in that case. */
27093 if (it->descent < 0)
27094 it->descent = 0;
27096 it->nglyphs = 1;
27098 if (face->box != FACE_NO_BOX)
27100 if (face->box_line_width > 0)
27102 if (slice.y == 0)
27103 it->ascent += face->box_line_width;
27104 if (slice.y + slice.height == img->height)
27105 it->descent += face->box_line_width;
27108 if (it->start_of_box_run_p && slice.x == 0)
27109 it->pixel_width += eabs (face->box_line_width);
27110 if (it->end_of_box_run_p && slice.x + slice.width == img->width)
27111 it->pixel_width += eabs (face->box_line_width);
27114 take_vertical_position_into_account (it);
27116 /* Automatically crop wide image glyphs at right edge so we can
27117 draw the cursor on same display row. */
27118 if ((crop = it->pixel_width - (it->last_visible_x - it->current_x), crop > 0)
27119 && (it->hpos == 0 || it->pixel_width > it->last_visible_x / 4))
27121 it->pixel_width -= crop;
27122 slice.width -= crop;
27125 if (it->glyph_row)
27127 struct glyph *glyph;
27128 enum glyph_row_area area = it->area;
27130 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
27131 if (it->glyph_row->reversed_p)
27133 struct glyph *g;
27135 /* Make room for the new glyph. */
27136 for (g = glyph - 1; g >= it->glyph_row->glyphs[it->area]; g--)
27137 g[1] = *g;
27138 glyph = it->glyph_row->glyphs[it->area];
27140 if (glyph < it->glyph_row->glyphs[area + 1])
27142 glyph->charpos = CHARPOS (it->position);
27143 glyph->object = it->object;
27144 glyph->pixel_width = clip_to_bounds (-1, it->pixel_width, SHRT_MAX);
27145 glyph->ascent = glyph_ascent;
27146 glyph->descent = it->descent;
27147 glyph->voffset = it->voffset;
27148 glyph->type = IMAGE_GLYPH;
27149 glyph->avoid_cursor_p = it->avoid_cursor_p;
27150 glyph->multibyte_p = it->multibyte_p;
27151 if (it->glyph_row->reversed_p && area == TEXT_AREA)
27153 /* In R2L rows, the left and the right box edges need to be
27154 drawn in reverse direction. */
27155 glyph->right_box_line_p = it->start_of_box_run_p;
27156 glyph->left_box_line_p = it->end_of_box_run_p;
27158 else
27160 glyph->left_box_line_p = it->start_of_box_run_p;
27161 glyph->right_box_line_p = it->end_of_box_run_p;
27163 glyph->overlaps_vertically_p = false;
27164 glyph->padding_p = false;
27165 glyph->glyph_not_available_p = false;
27166 glyph->face_id = it->face_id;
27167 glyph->u.img_id = img->id;
27168 glyph->slice.img = slice;
27169 glyph->font_type = FONT_TYPE_UNKNOWN;
27170 if (it->bidi_p)
27172 glyph->resolved_level = it->bidi_it.resolved_level;
27173 eassert ((it->bidi_it.type & 7) == it->bidi_it.type);
27174 glyph->bidi_type = it->bidi_it.type;
27176 ++it->glyph_row->used[area];
27178 else
27179 IT_EXPAND_MATRIX_WIDTH (it, area);
27183 static void
27184 produce_xwidget_glyph (struct it *it)
27186 #ifdef HAVE_XWIDGETS
27187 struct xwidget *xw;
27188 int glyph_ascent, crop;
27189 eassert (it->what == IT_XWIDGET);
27191 struct face *face = FACE_FROM_ID (it->f, it->face_id);
27192 /* Make sure X resources of the face is loaded. */
27193 prepare_face_for_display (it->f, face);
27195 xw = it->xwidget;
27196 it->ascent = it->phys_ascent = glyph_ascent = xw->height/2;
27197 it->descent = xw->height/2;
27198 it->phys_descent = it->descent;
27199 it->pixel_width = xw->width;
27200 /* It's quite possible for images to have an ascent greater than
27201 their height, so don't get confused in that case. */
27202 if (it->descent < 0)
27203 it->descent = 0;
27205 it->nglyphs = 1;
27207 if (face->box != FACE_NO_BOX)
27209 if (face->box_line_width > 0)
27211 it->ascent += face->box_line_width;
27212 it->descent += face->box_line_width;
27215 if (it->start_of_box_run_p)
27216 it->pixel_width += eabs (face->box_line_width);
27217 it->pixel_width += eabs (face->box_line_width);
27220 take_vertical_position_into_account (it);
27222 /* Automatically crop wide image glyphs at right edge so we can
27223 draw the cursor on same display row. */
27224 crop = it->pixel_width - (it->last_visible_x - it->current_x);
27225 if (crop > 0 && (it->hpos == 0 || it->pixel_width > it->last_visible_x / 4))
27226 it->pixel_width -= crop;
27228 if (it->glyph_row)
27230 enum glyph_row_area area = it->area;
27231 struct glyph *glyph
27232 = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
27234 if (it->glyph_row->reversed_p)
27236 struct glyph *g;
27238 /* Make room for the new glyph. */
27239 for (g = glyph - 1; g >= it->glyph_row->glyphs[it->area]; g--)
27240 g[1] = *g;
27241 glyph = it->glyph_row->glyphs[it->area];
27243 if (glyph < it->glyph_row->glyphs[area + 1])
27245 glyph->charpos = CHARPOS (it->position);
27246 glyph->object = it->object;
27247 glyph->pixel_width = clip_to_bounds (-1, it->pixel_width, SHRT_MAX);
27248 glyph->ascent = glyph_ascent;
27249 glyph->descent = it->descent;
27250 glyph->voffset = it->voffset;
27251 glyph->type = XWIDGET_GLYPH;
27252 glyph->avoid_cursor_p = it->avoid_cursor_p;
27253 glyph->multibyte_p = it->multibyte_p;
27254 if (it->glyph_row->reversed_p && area == TEXT_AREA)
27256 /* In R2L rows, the left and the right box edges need to be
27257 drawn in reverse direction. */
27258 glyph->right_box_line_p = it->start_of_box_run_p;
27259 glyph->left_box_line_p = it->end_of_box_run_p;
27261 else
27263 glyph->left_box_line_p = it->start_of_box_run_p;
27264 glyph->right_box_line_p = it->end_of_box_run_p;
27266 glyph->overlaps_vertically_p = 0;
27267 glyph->padding_p = 0;
27268 glyph->glyph_not_available_p = 0;
27269 glyph->face_id = it->face_id;
27270 glyph->u.xwidget = it->xwidget;
27271 glyph->font_type = FONT_TYPE_UNKNOWN;
27272 if (it->bidi_p)
27274 glyph->resolved_level = it->bidi_it.resolved_level;
27275 eassert ((it->bidi_it.type & 7) == it->bidi_it.type);
27276 glyph->bidi_type = it->bidi_it.type;
27278 ++it->glyph_row->used[area];
27280 else
27281 IT_EXPAND_MATRIX_WIDTH (it, area);
27283 #endif
27286 /* Append a stretch glyph to IT->glyph_row. OBJECT is the source
27287 of the glyph, WIDTH and HEIGHT are the width and height of the
27288 stretch. ASCENT is the ascent of the glyph (0 <= ASCENT <= HEIGHT). */
27290 static void
27291 append_stretch_glyph (struct it *it, Lisp_Object object,
27292 int width, int height, int ascent)
27294 struct glyph *glyph;
27295 enum glyph_row_area area = it->area;
27297 eassert (ascent >= 0 && ascent <= height);
27299 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
27300 if (glyph < it->glyph_row->glyphs[area + 1])
27302 /* If the glyph row is reversed, we need to prepend the glyph
27303 rather than append it. */
27304 if (it->glyph_row->reversed_p && area == TEXT_AREA)
27306 struct glyph *g;
27308 /* Make room for the additional glyph. */
27309 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
27310 g[1] = *g;
27311 glyph = it->glyph_row->glyphs[area];
27313 /* Decrease the width of the first glyph of the row that
27314 begins before first_visible_x (e.g., due to hscroll).
27315 This is so the overall width of the row becomes smaller
27316 by the scroll amount, and the stretch glyph appended by
27317 extend_face_to_end_of_line will be wider, to shift the
27318 row glyphs to the right. (In L2R rows, the corresponding
27319 left-shift effect is accomplished by setting row->x to a
27320 negative value, which won't work with R2L rows.)
27322 This must leave us with a positive value of WIDTH, since
27323 otherwise the call to move_it_in_display_line_to at the
27324 beginning of display_line would have got past the entire
27325 first glyph, and then it->current_x would have been
27326 greater or equal to it->first_visible_x. */
27327 if (it->current_x < it->first_visible_x)
27328 width -= it->first_visible_x - it->current_x;
27329 eassert (width > 0);
27331 glyph->charpos = CHARPOS (it->position);
27332 glyph->object = object;
27333 /* FIXME: It would be better to use TYPE_MAX here, but
27334 __typeof__ is not portable enough... */
27335 glyph->pixel_width = clip_to_bounds (-1, width, SHRT_MAX);
27336 glyph->ascent = ascent;
27337 glyph->descent = height - ascent;
27338 glyph->voffset = it->voffset;
27339 glyph->type = STRETCH_GLYPH;
27340 glyph->avoid_cursor_p = it->avoid_cursor_p;
27341 glyph->multibyte_p = it->multibyte_p;
27342 if (it->glyph_row->reversed_p && area == TEXT_AREA)
27344 /* In R2L rows, the left and the right box edges need to be
27345 drawn in reverse direction. */
27346 glyph->right_box_line_p = it->start_of_box_run_p;
27347 glyph->left_box_line_p = it->end_of_box_run_p;
27349 else
27351 glyph->left_box_line_p = it->start_of_box_run_p;
27352 glyph->right_box_line_p = it->end_of_box_run_p;
27354 glyph->overlaps_vertically_p = false;
27355 glyph->padding_p = false;
27356 glyph->glyph_not_available_p = false;
27357 glyph->face_id = it->face_id;
27358 glyph->u.stretch.ascent = ascent;
27359 glyph->u.stretch.height = height;
27360 glyph->slice.img = null_glyph_slice;
27361 glyph->font_type = FONT_TYPE_UNKNOWN;
27362 if (it->bidi_p)
27364 glyph->resolved_level = it->bidi_it.resolved_level;
27365 eassert ((it->bidi_it.type & 7) == it->bidi_it.type);
27366 glyph->bidi_type = it->bidi_it.type;
27368 else
27370 glyph->resolved_level = 0;
27371 glyph->bidi_type = UNKNOWN_BT;
27373 ++it->glyph_row->used[area];
27375 else
27376 IT_EXPAND_MATRIX_WIDTH (it, area);
27379 #endif /* HAVE_WINDOW_SYSTEM */
27381 /* Produce a stretch glyph for iterator IT. IT->object is the value
27382 of the glyph property displayed. The value must be a list
27383 `(space KEYWORD VALUE ...)' with the following KEYWORD/VALUE pairs
27384 being recognized:
27386 1. `:width WIDTH' specifies that the space should be WIDTH *
27387 canonical char width wide. WIDTH may be an integer or floating
27388 point number.
27390 2. `:relative-width FACTOR' specifies that the width of the stretch
27391 should be computed from the width of the first character having the
27392 `glyph' property, and should be FACTOR times that width.
27394 3. `:align-to HPOS' specifies that the space should be wide enough
27395 to reach HPOS, a value in canonical character units.
27397 Exactly one of the above pairs must be present.
27399 4. `:height HEIGHT' specifies that the height of the stretch produced
27400 should be HEIGHT, measured in canonical character units.
27402 5. `:relative-height FACTOR' specifies that the height of the
27403 stretch should be FACTOR times the height of the characters having
27404 the glyph property.
27406 Either none or exactly one of 4 or 5 must be present.
27408 6. `:ascent ASCENT' specifies that ASCENT percent of the height
27409 of the stretch should be used for the ascent of the stretch.
27410 ASCENT must be in the range 0 <= ASCENT <= 100. */
27412 void
27413 produce_stretch_glyph (struct it *it)
27415 /* (space :width WIDTH :height HEIGHT ...) */
27416 Lisp_Object prop, plist;
27417 int width = 0, height = 0, align_to = -1;
27418 bool zero_width_ok_p = false;
27419 double tem;
27420 struct font *font = NULL;
27422 #ifdef HAVE_WINDOW_SYSTEM
27423 int ascent = 0;
27424 bool zero_height_ok_p = false;
27426 if (FRAME_WINDOW_P (it->f))
27428 struct face *face = FACE_FROM_ID (it->f, it->face_id);
27429 font = face->font ? face->font : FRAME_FONT (it->f);
27430 prepare_face_for_display (it->f, face);
27432 #endif
27434 /* List should start with `space'. */
27435 eassert (CONSP (it->object) && EQ (XCAR (it->object), Qspace));
27436 plist = XCDR (it->object);
27438 /* Compute the width of the stretch. */
27439 if ((prop = Fplist_get (plist, QCwidth), !NILP (prop))
27440 && calc_pixel_width_or_height (&tem, it, prop, font, true, 0))
27442 /* Absolute width `:width WIDTH' specified and valid. */
27443 zero_width_ok_p = true;
27444 width = (int)tem;
27446 else if (prop = Fplist_get (plist, QCrelative_width), NUMVAL (prop) > 0)
27448 /* Relative width `:relative-width FACTOR' specified and valid.
27449 Compute the width of the characters having the `glyph'
27450 property. */
27451 struct it it2;
27452 unsigned char *p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
27454 it2 = *it;
27455 if (it->multibyte_p)
27456 it2.c = it2.char_to_display = STRING_CHAR_AND_LENGTH (p, it2.len);
27457 else
27459 it2.c = it2.char_to_display = *p, it2.len = 1;
27460 if (! ASCII_CHAR_P (it2.c))
27461 it2.char_to_display = BYTE8_TO_CHAR (it2.c);
27464 it2.glyph_row = NULL;
27465 it2.what = IT_CHARACTER;
27466 PRODUCE_GLYPHS (&it2);
27467 width = NUMVAL (prop) * it2.pixel_width;
27469 else if ((prop = Fplist_get (plist, QCalign_to), !NILP (prop))
27470 && calc_pixel_width_or_height (&tem, it, prop, font, true,
27471 &align_to))
27473 if (it->glyph_row == NULL || !it->glyph_row->mode_line_p)
27474 align_to = (align_to < 0
27476 : align_to - window_box_left_offset (it->w, TEXT_AREA));
27477 else if (align_to < 0)
27478 align_to = window_box_left_offset (it->w, TEXT_AREA);
27479 width = max (0, (int)tem + align_to - it->current_x);
27480 zero_width_ok_p = true;
27482 else
27483 /* Nothing specified -> width defaults to canonical char width. */
27484 width = FRAME_COLUMN_WIDTH (it->f);
27486 if (width <= 0 && (width < 0 || !zero_width_ok_p))
27487 width = 1;
27489 #ifdef HAVE_WINDOW_SYSTEM
27490 /* Compute height. */
27491 if (FRAME_WINDOW_P (it->f))
27493 int default_height = normal_char_height (font, ' ');
27495 if ((prop = Fplist_get (plist, QCheight), !NILP (prop))
27496 && calc_pixel_width_or_height (&tem, it, prop, font, false, 0))
27498 height = (int)tem;
27499 zero_height_ok_p = true;
27501 else if (prop = Fplist_get (plist, QCrelative_height),
27502 NUMVAL (prop) > 0)
27503 height = default_height * NUMVAL (prop);
27504 else
27505 height = default_height;
27507 if (height <= 0 && (height < 0 || !zero_height_ok_p))
27508 height = 1;
27510 /* Compute percentage of height used for ascent. If
27511 `:ascent ASCENT' is present and valid, use that. Otherwise,
27512 derive the ascent from the font in use. */
27513 if (prop = Fplist_get (plist, QCascent),
27514 NUMVAL (prop) > 0 && NUMVAL (prop) <= 100)
27515 ascent = height * NUMVAL (prop) / 100.0;
27516 else if (!NILP (prop)
27517 && calc_pixel_width_or_height (&tem, it, prop, font, false, 0))
27518 ascent = min (max (0, (int)tem), height);
27519 else
27520 ascent = (height * FONT_BASE (font)) / FONT_HEIGHT (font);
27522 else
27523 #endif /* HAVE_WINDOW_SYSTEM */
27524 height = 1;
27526 if (width > 0 && it->line_wrap != TRUNCATE
27527 && it->current_x + width > it->last_visible_x)
27529 width = it->last_visible_x - it->current_x;
27530 #ifdef HAVE_WINDOW_SYSTEM
27531 /* Subtract one more pixel from the stretch width, but only on
27532 GUI frames, since on a TTY each glyph is one "pixel" wide. */
27533 width -= FRAME_WINDOW_P (it->f);
27534 #endif
27537 if (width > 0 && height > 0 && it->glyph_row)
27539 Lisp_Object o_object = it->object;
27540 Lisp_Object object = it->stack[it->sp - 1].string;
27541 int n = width;
27543 if (!STRINGP (object))
27544 object = it->w->contents;
27545 #ifdef HAVE_WINDOW_SYSTEM
27546 if (FRAME_WINDOW_P (it->f))
27547 append_stretch_glyph (it, object, width, height, ascent);
27548 else
27549 #endif
27551 it->object = object;
27552 it->char_to_display = ' ';
27553 it->pixel_width = it->len = 1;
27554 while (n--)
27555 tty_append_glyph (it);
27556 it->object = o_object;
27560 it->pixel_width = width;
27561 #ifdef HAVE_WINDOW_SYSTEM
27562 if (FRAME_WINDOW_P (it->f))
27564 it->ascent = it->phys_ascent = ascent;
27565 it->descent = it->phys_descent = height - it->ascent;
27566 it->nglyphs = width > 0 && height > 0;
27567 take_vertical_position_into_account (it);
27569 else
27570 #endif
27571 it->nglyphs = width;
27574 /* Get information about special display element WHAT in an
27575 environment described by IT. WHAT is one of IT_TRUNCATION or
27576 IT_CONTINUATION. Maybe produce glyphs for WHAT if IT has a
27577 non-null glyph_row member. This function ensures that fields like
27578 face_id, c, len of IT are left untouched. */
27580 static void
27581 produce_special_glyphs (struct it *it, enum display_element_type what)
27583 struct it temp_it;
27584 Lisp_Object gc;
27585 GLYPH glyph;
27587 temp_it = *it;
27588 temp_it.object = Qnil;
27589 memset (&temp_it.current, 0, sizeof temp_it.current);
27591 if (what == IT_CONTINUATION)
27593 /* Continuation glyph. For R2L lines, we mirror it by hand. */
27594 if (it->bidi_it.paragraph_dir == R2L)
27595 SET_GLYPH_FROM_CHAR (glyph, '/');
27596 else
27597 SET_GLYPH_FROM_CHAR (glyph, '\\');
27598 if (it->dp
27599 && (gc = DISP_CONTINUE_GLYPH (it->dp), GLYPH_CODE_P (gc)))
27601 /* FIXME: Should we mirror GC for R2L lines? */
27602 SET_GLYPH_FROM_GLYPH_CODE (glyph, gc);
27603 spec_glyph_lookup_face (XWINDOW (it->window), &glyph);
27606 else if (what == IT_TRUNCATION)
27608 /* Truncation glyph. */
27609 SET_GLYPH_FROM_CHAR (glyph, '$');
27610 if (it->dp
27611 && (gc = DISP_TRUNC_GLYPH (it->dp), GLYPH_CODE_P (gc)))
27613 /* FIXME: Should we mirror GC for R2L lines? */
27614 SET_GLYPH_FROM_GLYPH_CODE (glyph, gc);
27615 spec_glyph_lookup_face (XWINDOW (it->window), &glyph);
27618 else
27619 emacs_abort ();
27621 #ifdef HAVE_WINDOW_SYSTEM
27622 /* On a GUI frame, when the right fringe (left fringe for R2L rows)
27623 is turned off, we precede the truncation/continuation glyphs by a
27624 stretch glyph whose width is computed such that these special
27625 glyphs are aligned at the window margin, even when very different
27626 fonts are used in different glyph rows. */
27627 if (FRAME_WINDOW_P (temp_it.f)
27628 /* init_iterator calls this with it->glyph_row == NULL, and it
27629 wants only the pixel width of the truncation/continuation
27630 glyphs. */
27631 && temp_it.glyph_row
27632 /* insert_left_trunc_glyphs calls us at the beginning of the
27633 row, and it has its own calculation of the stretch glyph
27634 width. */
27635 && temp_it.glyph_row->used[TEXT_AREA] > 0
27636 && (temp_it.glyph_row->reversed_p
27637 ? WINDOW_LEFT_FRINGE_WIDTH (temp_it.w)
27638 : WINDOW_RIGHT_FRINGE_WIDTH (temp_it.w)) == 0)
27640 int stretch_width = temp_it.last_visible_x - temp_it.current_x;
27642 if (stretch_width > 0)
27644 struct face *face = FACE_FROM_ID (temp_it.f, temp_it.face_id);
27645 struct font *font =
27646 face->font ? face->font : FRAME_FONT (temp_it.f);
27647 int stretch_ascent =
27648 (((temp_it.ascent + temp_it.descent)
27649 * FONT_BASE (font)) / FONT_HEIGHT (font));
27651 append_stretch_glyph (&temp_it, Qnil, stretch_width,
27652 temp_it.ascent + temp_it.descent,
27653 stretch_ascent);
27656 #endif
27658 temp_it.dp = NULL;
27659 temp_it.what = IT_CHARACTER;
27660 temp_it.c = temp_it.char_to_display = GLYPH_CHAR (glyph);
27661 temp_it.face_id = GLYPH_FACE (glyph);
27662 temp_it.len = CHAR_BYTES (temp_it.c);
27664 PRODUCE_GLYPHS (&temp_it);
27665 it->pixel_width = temp_it.pixel_width;
27666 it->nglyphs = temp_it.nglyphs;
27669 #ifdef HAVE_WINDOW_SYSTEM
27671 /* Calculate line-height and line-spacing properties.
27672 An integer value specifies explicit pixel value.
27673 A float value specifies relative value to current face height.
27674 A cons (float . face-name) specifies relative value to
27675 height of specified face font.
27677 Returns height in pixels, or nil. */
27679 static Lisp_Object
27680 calc_line_height_property (struct it *it, Lisp_Object val, struct font *font,
27681 int boff, bool override)
27683 Lisp_Object face_name = Qnil;
27684 int ascent, descent, height;
27686 if (NILP (val) || INTEGERP (val) || (override && EQ (val, Qt)))
27687 return val;
27689 if (CONSP (val))
27691 face_name = XCAR (val);
27692 val = XCDR (val);
27693 if (!NUMBERP (val))
27694 val = make_number (1);
27695 if (NILP (face_name))
27697 height = it->ascent + it->descent;
27698 goto scale;
27702 if (NILP (face_name))
27704 font = FRAME_FONT (it->f);
27705 boff = FRAME_BASELINE_OFFSET (it->f);
27707 else if (EQ (face_name, Qt))
27709 override = false;
27711 else
27713 int face_id;
27714 struct face *face;
27716 face_id = lookup_named_face (it->f, face_name, false);
27717 face = FACE_FROM_ID_OR_NULL (it->f, face_id);
27718 if (face == NULL || ((font = face->font) == NULL))
27719 return make_number (-1);
27720 boff = font->baseline_offset;
27721 if (font->vertical_centering)
27722 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
27725 normal_char_ascent_descent (font, -1, &ascent, &descent);
27727 if (override)
27729 it->override_ascent = ascent;
27730 it->override_descent = descent;
27731 it->override_boff = boff;
27734 height = ascent + descent;
27736 scale:
27737 if (FLOATP (val))
27738 height = (int)(XFLOAT_DATA (val) * height);
27739 else if (INTEGERP (val))
27740 height *= XINT (val);
27742 return make_number (height);
27746 /* Append a glyph for a glyphless character to IT->glyph_row. FACE_ID
27747 is a face ID to be used for the glyph. FOR_NO_FONT is true if
27748 and only if this is for a character for which no font was found.
27750 If the display method (it->glyphless_method) is
27751 GLYPHLESS_DISPLAY_ACRONYM or GLYPHLESS_DISPLAY_HEX_CODE, LEN is a
27752 length of the acronym or the hexadecimal string, UPPER_XOFF and
27753 UPPER_YOFF are pixel offsets for the upper part of the string,
27754 LOWER_XOFF and LOWER_YOFF are for the lower part.
27756 For the other display methods, LEN through LOWER_YOFF are zero. */
27758 static void
27759 append_glyphless_glyph (struct it *it, int face_id, bool for_no_font, int len,
27760 short upper_xoff, short upper_yoff,
27761 short lower_xoff, short lower_yoff)
27763 struct glyph *glyph;
27764 enum glyph_row_area area = it->area;
27766 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
27767 if (glyph < it->glyph_row->glyphs[area + 1])
27769 /* If the glyph row is reversed, we need to prepend the glyph
27770 rather than append it. */
27771 if (it->glyph_row->reversed_p && area == TEXT_AREA)
27773 struct glyph *g;
27775 /* Make room for the additional glyph. */
27776 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
27777 g[1] = *g;
27778 glyph = it->glyph_row->glyphs[area];
27780 glyph->charpos = CHARPOS (it->position);
27781 glyph->object = it->object;
27782 eassert (it->pixel_width <= SHRT_MAX);
27783 glyph->pixel_width = it->pixel_width;
27784 glyph->ascent = it->ascent;
27785 glyph->descent = it->descent;
27786 glyph->voffset = it->voffset;
27787 glyph->type = GLYPHLESS_GLYPH;
27788 glyph->u.glyphless.method = it->glyphless_method;
27789 glyph->u.glyphless.for_no_font = for_no_font;
27790 glyph->u.glyphless.len = len;
27791 glyph->u.glyphless.ch = it->c;
27792 glyph->slice.glyphless.upper_xoff = upper_xoff;
27793 glyph->slice.glyphless.upper_yoff = upper_yoff;
27794 glyph->slice.glyphless.lower_xoff = lower_xoff;
27795 glyph->slice.glyphless.lower_yoff = lower_yoff;
27796 glyph->avoid_cursor_p = it->avoid_cursor_p;
27797 glyph->multibyte_p = it->multibyte_p;
27798 if (it->glyph_row->reversed_p && area == TEXT_AREA)
27800 /* In R2L rows, the left and the right box edges need to be
27801 drawn in reverse direction. */
27802 glyph->right_box_line_p = it->start_of_box_run_p;
27803 glyph->left_box_line_p = it->end_of_box_run_p;
27805 else
27807 glyph->left_box_line_p = it->start_of_box_run_p;
27808 glyph->right_box_line_p = it->end_of_box_run_p;
27810 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
27811 || it->phys_descent > it->descent);
27812 glyph->padding_p = false;
27813 glyph->glyph_not_available_p = false;
27814 glyph->face_id = face_id;
27815 glyph->font_type = FONT_TYPE_UNKNOWN;
27816 if (it->bidi_p)
27818 glyph->resolved_level = it->bidi_it.resolved_level;
27819 eassert ((it->bidi_it.type & 7) == it->bidi_it.type);
27820 glyph->bidi_type = it->bidi_it.type;
27822 ++it->glyph_row->used[area];
27824 else
27825 IT_EXPAND_MATRIX_WIDTH (it, area);
27829 /* Produce a glyph for a glyphless character for iterator IT.
27830 IT->glyphless_method specifies which method to use for displaying
27831 the character. See the description of enum
27832 glyphless_display_method in dispextern.h for the detail.
27834 FOR_NO_FONT is true if and only if this is for a character for
27835 which no font was found. ACRONYM, if non-nil, is an acronym string
27836 for the character. */
27838 static void
27839 produce_glyphless_glyph (struct it *it, bool for_no_font, Lisp_Object acronym)
27841 int face_id;
27842 struct face *face;
27843 struct font *font;
27844 int base_width, base_height, width, height;
27845 short upper_xoff, upper_yoff, lower_xoff, lower_yoff;
27846 int len;
27848 /* Get the metrics of the base font. We always refer to the current
27849 ASCII face. */
27850 face = FACE_FROM_ID (it->f, it->face_id)->ascii_face;
27851 font = face->font ? face->font : FRAME_FONT (it->f);
27852 normal_char_ascent_descent (font, -1, &it->ascent, &it->descent);
27853 it->ascent += font->baseline_offset;
27854 it->descent -= font->baseline_offset;
27855 base_height = it->ascent + it->descent;
27856 base_width = font->average_width;
27858 face_id = merge_glyphless_glyph_face (it);
27860 if (it->glyphless_method == GLYPHLESS_DISPLAY_THIN_SPACE)
27862 it->pixel_width = THIN_SPACE_WIDTH;
27863 len = 0;
27864 upper_xoff = upper_yoff = lower_xoff = lower_yoff = 0;
27866 else if (it->glyphless_method == GLYPHLESS_DISPLAY_EMPTY_BOX)
27868 width = CHARACTER_WIDTH (it->c);
27869 if (width == 0)
27870 width = 1;
27871 else if (width > 4)
27872 width = 4;
27873 it->pixel_width = base_width * width;
27874 len = 0;
27875 upper_xoff = upper_yoff = lower_xoff = lower_yoff = 0;
27877 else
27879 char buf[7];
27880 const char *str;
27881 unsigned int code[6];
27882 int upper_len;
27883 int ascent, descent;
27884 struct font_metrics metrics_upper, metrics_lower;
27886 face = FACE_FROM_ID (it->f, face_id);
27887 font = face->font ? face->font : FRAME_FONT (it->f);
27888 prepare_face_for_display (it->f, face);
27890 if (it->glyphless_method == GLYPHLESS_DISPLAY_ACRONYM)
27892 if (! STRINGP (acronym) && CHAR_TABLE_P (Vglyphless_char_display))
27893 acronym = CHAR_TABLE_REF (Vglyphless_char_display, it->c);
27894 if (CONSP (acronym))
27895 acronym = XCAR (acronym);
27896 str = STRINGP (acronym) ? SSDATA (acronym) : "";
27898 else
27900 eassert (it->glyphless_method == GLYPHLESS_DISPLAY_HEX_CODE);
27901 sprintf (buf, "%0*X", it->c < 0x10000 ? 4 : 6, it->c + 0u);
27902 str = buf;
27904 for (len = 0; str[len] && ASCII_CHAR_P (str[len]) && len < 6; len++)
27905 code[len] = font->driver->encode_char (font, str[len]);
27906 upper_len = (len + 1) / 2;
27907 font->driver->text_extents (font, code, upper_len,
27908 &metrics_upper);
27909 font->driver->text_extents (font, code + upper_len, len - upper_len,
27910 &metrics_lower);
27914 /* +4 is for vertical bars of a box plus 1-pixel spaces at both side. */
27915 width = max (metrics_upper.width, metrics_lower.width) + 4;
27916 upper_xoff = upper_yoff = 2; /* the typical case */
27917 if (base_width >= width)
27919 /* Align the upper to the left, the lower to the right. */
27920 it->pixel_width = base_width;
27921 lower_xoff = base_width - 2 - metrics_lower.width;
27923 else
27925 /* Center the shorter one. */
27926 it->pixel_width = width;
27927 if (metrics_upper.width >= metrics_lower.width)
27928 lower_xoff = (width - metrics_lower.width) / 2;
27929 else
27931 /* FIXME: This code doesn't look right. It formerly was
27932 missing the "lower_xoff = 0;", which couldn't have
27933 been right since it left lower_xoff uninitialized. */
27934 lower_xoff = 0;
27935 upper_xoff = (width - metrics_upper.width) / 2;
27939 /* +5 is for horizontal bars of a box plus 1-pixel spaces at
27940 top, bottom, and between upper and lower strings. */
27941 height = (metrics_upper.ascent + metrics_upper.descent
27942 + metrics_lower.ascent + metrics_lower.descent) + 5;
27943 /* Center vertically.
27944 H:base_height, D:base_descent
27945 h:height, ld:lower_descent, la:lower_ascent, ud:upper_descent
27947 ascent = - (D - H/2 - h/2 + 1); "+ 1" for rounding up
27948 descent = D - H/2 + h/2;
27949 lower_yoff = descent - 2 - ld;
27950 upper_yoff = lower_yoff - la - 1 - ud; */
27951 ascent = - (it->descent - (base_height + height + 1) / 2);
27952 descent = it->descent - (base_height - height) / 2;
27953 lower_yoff = descent - 2 - metrics_lower.descent;
27954 upper_yoff = (lower_yoff - metrics_lower.ascent - 1
27955 - metrics_upper.descent);
27956 /* Don't make the height shorter than the base height. */
27957 if (height > base_height)
27959 it->ascent = ascent;
27960 it->descent = descent;
27964 it->phys_ascent = it->ascent;
27965 it->phys_descent = it->descent;
27966 if (it->glyph_row)
27967 append_glyphless_glyph (it, face_id, for_no_font, len,
27968 upper_xoff, upper_yoff,
27969 lower_xoff, lower_yoff);
27970 it->nglyphs = 1;
27971 take_vertical_position_into_account (it);
27975 /* RIF:
27976 Produce glyphs/get display metrics for the display element IT is
27977 loaded with. See the description of struct it in dispextern.h
27978 for an overview of struct it. */
27980 void
27981 x_produce_glyphs (struct it *it)
27983 int extra_line_spacing = it->extra_line_spacing;
27985 it->glyph_not_available_p = false;
27987 if (it->what == IT_CHARACTER)
27989 XChar2b char2b;
27990 struct face *face = FACE_FROM_ID (it->f, it->face_id);
27991 struct font *font = face->font;
27992 struct font_metrics *pcm = NULL;
27993 int boff; /* Baseline offset. */
27995 if (font == NULL)
27997 /* When no suitable font is found, display this character by
27998 the method specified in the first extra slot of
27999 Vglyphless_char_display. */
28000 Lisp_Object acronym = lookup_glyphless_char_display (-1, it);
28002 eassert (it->what == IT_GLYPHLESS);
28003 produce_glyphless_glyph (it, true,
28004 STRINGP (acronym) ? acronym : Qnil);
28005 goto done;
28008 boff = font->baseline_offset;
28009 if (font->vertical_centering)
28010 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
28012 if (it->char_to_display != '\n' && it->char_to_display != '\t')
28014 it->nglyphs = 1;
28016 if (it->override_ascent >= 0)
28018 it->ascent = it->override_ascent;
28019 it->descent = it->override_descent;
28020 boff = it->override_boff;
28022 else
28024 it->ascent = FONT_BASE (font) + boff;
28025 it->descent = FONT_DESCENT (font) - boff;
28028 if (get_char_glyph_code (it->char_to_display, font, &char2b))
28030 pcm = get_per_char_metric (font, &char2b);
28031 if (pcm->width == 0
28032 && pcm->rbearing == 0 && pcm->lbearing == 0)
28033 pcm = NULL;
28036 if (pcm)
28038 it->phys_ascent = pcm->ascent + boff;
28039 it->phys_descent = pcm->descent - boff;
28040 it->pixel_width = pcm->width;
28041 /* Don't use font-global values for ascent and descent
28042 if they result in an exceedingly large line height. */
28043 if (it->override_ascent < 0)
28045 if (FONT_TOO_HIGH (font))
28047 it->ascent = it->phys_ascent;
28048 it->descent = it->phys_descent;
28049 /* These limitations are enforced by an
28050 assertion near the end of this function. */
28051 if (it->ascent < 0)
28052 it->ascent = 0;
28053 if (it->descent < 0)
28054 it->descent = 0;
28058 else
28060 it->glyph_not_available_p = true;
28061 it->phys_ascent = it->ascent;
28062 it->phys_descent = it->descent;
28063 it->pixel_width = font->space_width;
28066 if (it->constrain_row_ascent_descent_p)
28068 if (it->descent > it->max_descent)
28070 it->ascent += it->descent - it->max_descent;
28071 it->descent = it->max_descent;
28073 if (it->ascent > it->max_ascent)
28075 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
28076 it->ascent = it->max_ascent;
28078 it->phys_ascent = min (it->phys_ascent, it->ascent);
28079 it->phys_descent = min (it->phys_descent, it->descent);
28080 extra_line_spacing = 0;
28083 /* If this is a space inside a region of text with
28084 `space-width' property, change its width. */
28085 bool stretched_p
28086 = it->char_to_display == ' ' && !NILP (it->space_width);
28087 if (stretched_p)
28088 it->pixel_width *= XFLOATINT (it->space_width);
28090 /* If face has a box, add the box thickness to the character
28091 height. If character has a box line to the left and/or
28092 right, add the box line width to the character's width. */
28093 if (face->box != FACE_NO_BOX)
28095 int thick = face->box_line_width;
28097 if (thick > 0)
28099 it->ascent += thick;
28100 it->descent += thick;
28102 else
28103 thick = -thick;
28105 if (it->start_of_box_run_p)
28106 it->pixel_width += thick;
28107 if (it->end_of_box_run_p)
28108 it->pixel_width += thick;
28111 /* If face has an overline, add the height of the overline
28112 (1 pixel) and a 1 pixel margin to the character height. */
28113 if (face->overline_p)
28114 it->ascent += overline_margin;
28116 if (it->constrain_row_ascent_descent_p)
28118 if (it->ascent > it->max_ascent)
28119 it->ascent = it->max_ascent;
28120 if (it->descent > it->max_descent)
28121 it->descent = it->max_descent;
28124 take_vertical_position_into_account (it);
28126 /* If we have to actually produce glyphs, do it. */
28127 if (it->glyph_row)
28129 if (stretched_p)
28131 /* Translate a space with a `space-width' property
28132 into a stretch glyph. */
28133 int ascent = (((it->ascent + it->descent) * FONT_BASE (font))
28134 / FONT_HEIGHT (font));
28135 append_stretch_glyph (it, it->object, it->pixel_width,
28136 it->ascent + it->descent, ascent);
28138 else
28139 append_glyph (it);
28141 /* If characters with lbearing or rbearing are displayed
28142 in this line, record that fact in a flag of the
28143 glyph row. This is used to optimize X output code. */
28144 if (pcm && (pcm->lbearing < 0 || pcm->rbearing > pcm->width))
28145 it->glyph_row->contains_overlapping_glyphs_p = true;
28147 if (! stretched_p && it->pixel_width == 0)
28148 /* We assure that all visible glyphs have at least 1-pixel
28149 width. */
28150 it->pixel_width = 1;
28152 else if (it->char_to_display == '\n')
28154 /* A newline has no width, but we need the height of the
28155 line. But if previous part of the line sets a height,
28156 don't increase that height. */
28158 Lisp_Object height;
28159 Lisp_Object total_height = Qnil;
28161 it->override_ascent = -1;
28162 it->pixel_width = 0;
28163 it->nglyphs = 0;
28165 height = get_it_property (it, Qline_height);
28166 /* Split (line-height total-height) list. */
28167 if (CONSP (height)
28168 && CONSP (XCDR (height))
28169 && NILP (XCDR (XCDR (height))))
28171 total_height = XCAR (XCDR (height));
28172 height = XCAR (height);
28174 height = calc_line_height_property (it, height, font, boff, true);
28176 if (it->override_ascent >= 0)
28178 it->ascent = it->override_ascent;
28179 it->descent = it->override_descent;
28180 boff = it->override_boff;
28182 else
28184 if (FONT_TOO_HIGH (font))
28186 it->ascent = font->pixel_size + boff - 1;
28187 it->descent = -boff + 1;
28188 if (it->descent < 0)
28189 it->descent = 0;
28191 else
28193 it->ascent = FONT_BASE (font) + boff;
28194 it->descent = FONT_DESCENT (font) - boff;
28198 if (EQ (height, Qt))
28200 if (it->descent > it->max_descent)
28202 it->ascent += it->descent - it->max_descent;
28203 it->descent = it->max_descent;
28205 if (it->ascent > it->max_ascent)
28207 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
28208 it->ascent = it->max_ascent;
28210 it->phys_ascent = min (it->phys_ascent, it->ascent);
28211 it->phys_descent = min (it->phys_descent, it->descent);
28212 it->constrain_row_ascent_descent_p = true;
28213 extra_line_spacing = 0;
28215 else
28217 Lisp_Object spacing;
28219 it->phys_ascent = it->ascent;
28220 it->phys_descent = it->descent;
28222 if ((it->max_ascent > 0 || it->max_descent > 0)
28223 && face->box != FACE_NO_BOX
28224 && face->box_line_width > 0)
28226 it->ascent += face->box_line_width;
28227 it->descent += face->box_line_width;
28229 if (!NILP (height)
28230 && XINT (height) > it->ascent + it->descent)
28231 it->ascent = XINT (height) - it->descent;
28233 if (!NILP (total_height))
28234 spacing = calc_line_height_property (it, total_height, font,
28235 boff, false);
28236 else
28238 spacing = get_it_property (it, Qline_spacing);
28239 spacing = calc_line_height_property (it, spacing, font,
28240 boff, false);
28242 if (INTEGERP (spacing))
28244 extra_line_spacing = XINT (spacing);
28245 if (!NILP (total_height))
28246 extra_line_spacing -= (it->phys_ascent + it->phys_descent);
28250 else /* i.e. (it->char_to_display == '\t') */
28252 if (font->space_width > 0)
28254 int tab_width = it->tab_width * font->space_width;
28255 int x = it->current_x + it->continuation_lines_width;
28256 int x0 = x;
28257 /* Adjust for line numbers, if needed. */
28258 if (!NILP (Vdisplay_line_numbers) && x0 >= it->lnum_pixel_width)
28259 x -= it->lnum_pixel_width;
28260 int next_tab_x = ((1 + x + tab_width - 1) / tab_width) * tab_width;
28262 /* If the distance from the current position to the next tab
28263 stop is less than a space character width, use the
28264 tab stop after that. */
28265 if (next_tab_x - x < font->space_width)
28266 next_tab_x += tab_width;
28267 if (!NILP (Vdisplay_line_numbers) && x0 >= it->lnum_pixel_width)
28268 next_tab_x += (it->lnum_pixel_width
28269 - ((it->w->hscroll * font->space_width)
28270 % tab_width));
28272 it->pixel_width = next_tab_x - x0;
28273 it->nglyphs = 1;
28274 if (FONT_TOO_HIGH (font))
28276 if (get_char_glyph_code (' ', font, &char2b))
28278 pcm = get_per_char_metric (font, &char2b);
28279 if (pcm->width == 0
28280 && pcm->rbearing == 0 && pcm->lbearing == 0)
28281 pcm = NULL;
28284 if (pcm)
28286 it->ascent = pcm->ascent + boff;
28287 it->descent = pcm->descent - boff;
28289 else
28291 it->ascent = font->pixel_size + boff - 1;
28292 it->descent = -boff + 1;
28294 if (it->ascent < 0)
28295 it->ascent = 0;
28296 if (it->descent < 0)
28297 it->descent = 0;
28299 else
28301 it->ascent = FONT_BASE (font) + boff;
28302 it->descent = FONT_DESCENT (font) - boff;
28304 it->phys_ascent = it->ascent;
28305 it->phys_descent = it->descent;
28307 if (it->glyph_row)
28309 append_stretch_glyph (it, it->object, it->pixel_width,
28310 it->ascent + it->descent, it->ascent);
28313 else
28315 it->pixel_width = 0;
28316 it->nglyphs = 1;
28320 if (FONT_TOO_HIGH (font))
28322 int font_ascent, font_descent;
28324 /* For very large fonts, where we ignore the declared font
28325 dimensions, and go by per-character metrics instead,
28326 don't let the row ascent and descent values (and the row
28327 height computed from them) be smaller than the "normal"
28328 character metrics. This avoids unpleasant effects
28329 whereby lines on display would change their height
28330 depending on which characters are shown. */
28331 normal_char_ascent_descent (font, -1, &font_ascent, &font_descent);
28332 it->max_ascent = max (it->max_ascent, font_ascent);
28333 it->max_descent = max (it->max_descent, font_descent);
28336 else if (it->what == IT_COMPOSITION && it->cmp_it.ch < 0)
28338 /* A static composition.
28340 Note: A composition is represented as one glyph in the
28341 glyph matrix. There are no padding glyphs.
28343 Important note: pixel_width, ascent, and descent are the
28344 values of what is drawn by draw_glyphs (i.e. the values of
28345 the overall glyphs composed). */
28346 struct face *face = FACE_FROM_ID (it->f, it->face_id);
28347 int boff; /* baseline offset */
28348 struct composition *cmp = composition_table[it->cmp_it.id];
28349 int glyph_len = cmp->glyph_len;
28350 struct font *font = face->font;
28352 it->nglyphs = 1;
28354 /* If we have not yet calculated pixel size data of glyphs of
28355 the composition for the current face font, calculate them
28356 now. Theoretically, we have to check all fonts for the
28357 glyphs, but that requires much time and memory space. So,
28358 here we check only the font of the first glyph. This may
28359 lead to incorrect display, but it's very rare, and C-l
28360 (recenter-top-bottom) can correct the display anyway. */
28361 if (! cmp->font || cmp->font != font)
28363 /* Ascent and descent of the font of the first character
28364 of this composition (adjusted by baseline offset).
28365 Ascent and descent of overall glyphs should not be less
28366 than these, respectively. */
28367 int font_ascent, font_descent, font_height;
28368 /* Bounding box of the overall glyphs. */
28369 int leftmost, rightmost, lowest, highest;
28370 int lbearing, rbearing;
28371 int i, width, ascent, descent;
28372 int c;
28373 XChar2b char2b;
28374 struct font_metrics *pcm;
28375 ptrdiff_t pos;
28377 eassume (0 < glyph_len); /* See Bug#8512. */
28379 c = COMPOSITION_GLYPH (cmp, glyph_len - 1);
28380 while (c == '\t' && 0 < --glyph_len);
28382 bool right_padded = glyph_len < cmp->glyph_len;
28383 for (i = 0; i < glyph_len; i++)
28385 c = COMPOSITION_GLYPH (cmp, i);
28386 if (c != '\t')
28387 break;
28388 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
28390 bool left_padded = i > 0;
28392 pos = (STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
28393 : IT_CHARPOS (*it));
28394 /* If no suitable font is found, use the default font. */
28395 bool font_not_found_p = font == NULL;
28396 if (font_not_found_p)
28398 face = face->ascii_face;
28399 font = face->font;
28401 boff = font->baseline_offset;
28402 if (font->vertical_centering)
28403 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
28404 normal_char_ascent_descent (font, -1, &font_ascent, &font_descent);
28405 font_ascent += boff;
28406 font_descent -= boff;
28407 font_height = font_ascent + font_descent;
28409 cmp->font = font;
28411 pcm = NULL;
28412 if (! font_not_found_p)
28414 get_char_face_and_encoding (it->f, c, it->face_id,
28415 &char2b, false);
28416 pcm = get_per_char_metric (font, &char2b);
28419 /* Initialize the bounding box. */
28420 if (pcm)
28422 width = cmp->glyph_len > 0 ? pcm->width : 0;
28423 ascent = pcm->ascent;
28424 descent = pcm->descent;
28425 lbearing = pcm->lbearing;
28426 rbearing = pcm->rbearing;
28428 else
28430 width = cmp->glyph_len > 0 ? font->space_width : 0;
28431 ascent = FONT_BASE (font);
28432 descent = FONT_DESCENT (font);
28433 lbearing = 0;
28434 rbearing = width;
28437 rightmost = width;
28438 leftmost = 0;
28439 lowest = - descent + boff;
28440 highest = ascent + boff;
28442 if (! font_not_found_p
28443 && font->default_ascent
28444 && CHAR_TABLE_P (Vuse_default_ascent)
28445 && !NILP (Faref (Vuse_default_ascent,
28446 make_number (it->char_to_display))))
28447 highest = font->default_ascent + boff;
28449 /* Draw the first glyph at the normal position. It may be
28450 shifted to right later if some other glyphs are drawn
28451 at the left. */
28452 cmp->offsets[i * 2] = 0;
28453 cmp->offsets[i * 2 + 1] = boff;
28454 cmp->lbearing = lbearing;
28455 cmp->rbearing = rbearing;
28457 /* Set cmp->offsets for the remaining glyphs. */
28458 for (i++; i < glyph_len; i++)
28460 int left, right, btm, top;
28461 int ch = COMPOSITION_GLYPH (cmp, i);
28462 int face_id;
28463 struct face *this_face;
28465 if (ch == '\t')
28466 ch = ' ';
28467 face_id = FACE_FOR_CHAR (it->f, face, ch, pos, it->string);
28468 this_face = FACE_FROM_ID (it->f, face_id);
28469 font = this_face->font;
28471 if (font == NULL)
28472 pcm = NULL;
28473 else
28475 get_char_face_and_encoding (it->f, ch, face_id,
28476 &char2b, false);
28477 pcm = get_per_char_metric (font, &char2b);
28479 if (! pcm)
28480 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
28481 else
28483 width = pcm->width;
28484 ascent = pcm->ascent;
28485 descent = pcm->descent;
28486 lbearing = pcm->lbearing;
28487 rbearing = pcm->rbearing;
28488 if (cmp->method != COMPOSITION_WITH_RULE_ALTCHARS)
28490 /* Relative composition with or without
28491 alternate chars. */
28492 left = (leftmost + rightmost - width) / 2;
28493 btm = - descent + boff;
28494 if (font->relative_compose
28495 && (! CHAR_TABLE_P (Vignore_relative_composition)
28496 || NILP (Faref (Vignore_relative_composition,
28497 make_number (ch)))))
28500 if (- descent >= font->relative_compose)
28501 /* One extra pixel between two glyphs. */
28502 btm = highest + 1;
28503 else if (ascent <= 0)
28504 /* One extra pixel between two glyphs. */
28505 btm = lowest - 1 - ascent - descent;
28508 else
28510 /* A composition rule is specified by an integer
28511 value that encodes global and new reference
28512 points (GREF and NREF). GREF and NREF are
28513 specified by numbers as below:
28515 0---1---2 -- ascent
28519 9--10--11 -- center
28521 ---3---4---5--- baseline
28523 6---7---8 -- descent
28525 int rule = COMPOSITION_RULE (cmp, i);
28526 int gref, nref, grefx, grefy, nrefx, nrefy, xoff, yoff;
28528 COMPOSITION_DECODE_RULE (rule, gref, nref, xoff, yoff);
28529 grefx = gref % 3, nrefx = nref % 3;
28530 grefy = gref / 3, nrefy = nref / 3;
28531 if (xoff)
28532 xoff = font_height * (xoff - 128) / 256;
28533 if (yoff)
28534 yoff = font_height * (yoff - 128) / 256;
28536 left = (leftmost
28537 + grefx * (rightmost - leftmost) / 2
28538 - nrefx * width / 2
28539 + xoff);
28541 btm = ((grefy == 0 ? highest
28542 : grefy == 1 ? 0
28543 : grefy == 2 ? lowest
28544 : (highest + lowest) / 2)
28545 - (nrefy == 0 ? ascent + descent
28546 : nrefy == 1 ? descent - boff
28547 : nrefy == 2 ? 0
28548 : (ascent + descent) / 2)
28549 + yoff);
28552 cmp->offsets[i * 2] = left;
28553 cmp->offsets[i * 2 + 1] = btm + descent;
28555 /* Update the bounding box of the overall glyphs. */
28556 if (width > 0)
28558 right = left + width;
28559 if (left < leftmost)
28560 leftmost = left;
28561 if (right > rightmost)
28562 rightmost = right;
28564 top = btm + descent + ascent;
28565 if (top > highest)
28566 highest = top;
28567 if (btm < lowest)
28568 lowest = btm;
28570 if (cmp->lbearing > left + lbearing)
28571 cmp->lbearing = left + lbearing;
28572 if (cmp->rbearing < left + rbearing)
28573 cmp->rbearing = left + rbearing;
28577 /* If there are glyphs whose x-offsets are negative,
28578 shift all glyphs to the right and make all x-offsets
28579 non-negative. */
28580 if (leftmost < 0)
28582 for (i = 0; i < cmp->glyph_len; i++)
28583 cmp->offsets[i * 2] -= leftmost;
28584 rightmost -= leftmost;
28585 cmp->lbearing -= leftmost;
28586 cmp->rbearing -= leftmost;
28589 if (left_padded && cmp->lbearing < 0)
28591 for (i = 0; i < cmp->glyph_len; i++)
28592 cmp->offsets[i * 2] -= cmp->lbearing;
28593 rightmost -= cmp->lbearing;
28594 cmp->rbearing -= cmp->lbearing;
28595 cmp->lbearing = 0;
28597 if (right_padded && rightmost < cmp->rbearing)
28599 rightmost = cmp->rbearing;
28602 cmp->pixel_width = rightmost;
28603 cmp->ascent = highest;
28604 cmp->descent = - lowest;
28605 if (cmp->ascent < font_ascent)
28606 cmp->ascent = font_ascent;
28607 if (cmp->descent < font_descent)
28608 cmp->descent = font_descent;
28611 if (it->glyph_row
28612 && (cmp->lbearing < 0
28613 || cmp->rbearing > cmp->pixel_width))
28614 it->glyph_row->contains_overlapping_glyphs_p = true;
28616 it->pixel_width = cmp->pixel_width;
28617 it->ascent = it->phys_ascent = cmp->ascent;
28618 it->descent = it->phys_descent = cmp->descent;
28619 if (face->box != FACE_NO_BOX)
28621 int thick = face->box_line_width;
28623 if (thick > 0)
28625 it->ascent += thick;
28626 it->descent += thick;
28628 else
28629 thick = - thick;
28631 if (it->start_of_box_run_p)
28632 it->pixel_width += thick;
28633 if (it->end_of_box_run_p)
28634 it->pixel_width += thick;
28637 /* If face has an overline, add the height of the overline
28638 (1 pixel) and a 1 pixel margin to the character height. */
28639 if (face->overline_p)
28640 it->ascent += overline_margin;
28642 take_vertical_position_into_account (it);
28643 if (it->ascent < 0)
28644 it->ascent = 0;
28645 if (it->descent < 0)
28646 it->descent = 0;
28648 if (it->glyph_row && cmp->glyph_len > 0)
28649 append_composite_glyph (it);
28651 else if (it->what == IT_COMPOSITION)
28653 /* A dynamic (automatic) composition. */
28654 struct face *face = FACE_FROM_ID (it->f, it->face_id);
28655 Lisp_Object gstring;
28656 struct font_metrics metrics;
28658 it->nglyphs = 1;
28660 gstring = composition_gstring_from_id (it->cmp_it.id);
28661 it->pixel_width
28662 = composition_gstring_width (gstring, it->cmp_it.from, it->cmp_it.to,
28663 &metrics);
28664 if (it->glyph_row
28665 && (metrics.lbearing < 0 || metrics.rbearing > metrics.width))
28666 it->glyph_row->contains_overlapping_glyphs_p = true;
28667 it->ascent = it->phys_ascent = metrics.ascent;
28668 it->descent = it->phys_descent = metrics.descent;
28669 if (face->box != FACE_NO_BOX)
28671 int thick = face->box_line_width;
28673 if (thick > 0)
28675 it->ascent += thick;
28676 it->descent += thick;
28678 else
28679 thick = - thick;
28681 if (it->start_of_box_run_p)
28682 it->pixel_width += thick;
28683 if (it->end_of_box_run_p)
28684 it->pixel_width += thick;
28686 /* If face has an overline, add the height of the overline
28687 (1 pixel) and a 1 pixel margin to the character height. */
28688 if (face->overline_p)
28689 it->ascent += overline_margin;
28690 take_vertical_position_into_account (it);
28691 if (it->ascent < 0)
28692 it->ascent = 0;
28693 if (it->descent < 0)
28694 it->descent = 0;
28696 if (it->glyph_row)
28697 append_composite_glyph (it);
28699 else if (it->what == IT_GLYPHLESS)
28700 produce_glyphless_glyph (it, false, Qnil);
28701 else if (it->what == IT_IMAGE)
28702 produce_image_glyph (it);
28703 else if (it->what == IT_STRETCH)
28704 produce_stretch_glyph (it);
28705 else if (it->what == IT_XWIDGET)
28706 produce_xwidget_glyph (it);
28708 done:
28709 /* Accumulate dimensions. Note: can't assume that it->descent > 0
28710 because this isn't true for images with `:ascent 100'. */
28711 eassert (it->ascent >= 0 && it->descent >= 0);
28712 if (it->area == TEXT_AREA)
28713 it->current_x += it->pixel_width;
28715 if (extra_line_spacing > 0)
28717 it->descent += extra_line_spacing;
28718 if (extra_line_spacing > it->max_extra_line_spacing)
28719 it->max_extra_line_spacing = extra_line_spacing;
28722 it->max_ascent = max (it->max_ascent, it->ascent);
28723 it->max_descent = max (it->max_descent, it->descent);
28724 it->max_phys_ascent = max (it->max_phys_ascent, it->phys_ascent);
28725 it->max_phys_descent = max (it->max_phys_descent, it->phys_descent);
28728 /* EXPORT for RIF:
28729 Output LEN glyphs starting at START at the nominal cursor position.
28730 Advance the nominal cursor over the text. UPDATED_ROW is the glyph row
28731 being updated, and UPDATED_AREA is the area of that row being updated. */
28733 void
28734 x_write_glyphs (struct window *w, struct glyph_row *updated_row,
28735 struct glyph *start, enum glyph_row_area updated_area, int len)
28737 int x, hpos, chpos = w->phys_cursor.hpos;
28739 eassert (updated_row);
28740 /* When the window is hscrolled, cursor hpos can legitimately be out
28741 of bounds, but we draw the cursor at the corresponding window
28742 margin in that case. */
28743 if (!updated_row->reversed_p && chpos < 0)
28744 chpos = 0;
28745 if (updated_row->reversed_p && chpos >= updated_row->used[TEXT_AREA])
28746 chpos = updated_row->used[TEXT_AREA] - 1;
28748 block_input ();
28750 /* Write glyphs. */
28752 hpos = start - updated_row->glyphs[updated_area];
28753 x = draw_glyphs (w, w->output_cursor.x,
28754 updated_row, updated_area,
28755 hpos, hpos + len,
28756 DRAW_NORMAL_TEXT, 0);
28758 /* Invalidate old phys cursor if the glyph at its hpos is redrawn. */
28759 if (updated_area == TEXT_AREA
28760 && w->phys_cursor_on_p
28761 && w->phys_cursor.vpos == w->output_cursor.vpos
28762 && chpos >= hpos
28763 && chpos < hpos + len)
28764 w->phys_cursor_on_p = false;
28766 unblock_input ();
28768 /* Advance the output cursor. */
28769 w->output_cursor.hpos += len;
28770 w->output_cursor.x = x;
28774 /* EXPORT for RIF:
28775 Insert LEN glyphs from START at the nominal cursor position. */
28777 void
28778 x_insert_glyphs (struct window *w, struct glyph_row *updated_row,
28779 struct glyph *start, enum glyph_row_area updated_area, int len)
28781 struct frame *f;
28782 int line_height, shift_by_width, shifted_region_width;
28783 struct glyph_row *row;
28784 struct glyph *glyph;
28785 int frame_x, frame_y;
28786 ptrdiff_t hpos;
28788 eassert (updated_row);
28789 block_input ();
28790 f = XFRAME (WINDOW_FRAME (w));
28792 /* Get the height of the line we are in. */
28793 row = updated_row;
28794 line_height = row->height;
28796 /* Get the width of the glyphs to insert. */
28797 shift_by_width = 0;
28798 for (glyph = start; glyph < start + len; ++glyph)
28799 shift_by_width += glyph->pixel_width;
28801 /* Get the width of the region to shift right. */
28802 shifted_region_width = (window_box_width (w, updated_area)
28803 - w->output_cursor.x
28804 - shift_by_width);
28806 /* Shift right. */
28807 frame_x = window_box_left (w, updated_area) + w->output_cursor.x;
28808 frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, w->output_cursor.y);
28810 FRAME_RIF (f)->shift_glyphs_for_insert (f, frame_x, frame_y, shifted_region_width,
28811 line_height, shift_by_width);
28813 /* Write the glyphs. */
28814 hpos = start - row->glyphs[updated_area];
28815 draw_glyphs (w, w->output_cursor.x, row, updated_area,
28816 hpos, hpos + len,
28817 DRAW_NORMAL_TEXT, 0);
28819 /* Advance the output cursor. */
28820 w->output_cursor.hpos += len;
28821 w->output_cursor.x += shift_by_width;
28822 unblock_input ();
28826 /* EXPORT for RIF:
28827 Erase the current text line from the nominal cursor position
28828 (inclusive) to pixel column TO_X (exclusive). The idea is that
28829 everything from TO_X onward is already erased.
28831 TO_X is a pixel position relative to UPDATED_AREA of currently
28832 updated window W. TO_X == -1 means clear to the end of this area. */
28834 void
28835 x_clear_end_of_line (struct window *w, struct glyph_row *updated_row,
28836 enum glyph_row_area updated_area, int to_x)
28838 struct frame *f;
28839 int max_x, min_y, max_y;
28840 int from_x, from_y, to_y;
28842 eassert (updated_row);
28843 f = XFRAME (w->frame);
28845 if (updated_row->full_width_p)
28846 max_x = (WINDOW_PIXEL_WIDTH (w)
28847 - (updated_row->mode_line_p ? WINDOW_RIGHT_DIVIDER_WIDTH (w) : 0));
28848 else
28849 max_x = window_box_width (w, updated_area);
28850 max_y = window_text_bottom_y (w);
28852 /* TO_X == 0 means don't do anything. TO_X < 0 means clear to end
28853 of window. For TO_X > 0, truncate to end of drawing area. */
28854 if (to_x == 0)
28855 return;
28856 else if (to_x < 0)
28857 to_x = max_x;
28858 else
28859 to_x = min (to_x, max_x);
28861 to_y = min (max_y, w->output_cursor.y + updated_row->height);
28863 /* Notice if the cursor will be cleared by this operation. */
28864 if (!updated_row->full_width_p)
28865 notice_overwritten_cursor (w, updated_area,
28866 w->output_cursor.x, -1,
28867 updated_row->y,
28868 MATRIX_ROW_BOTTOM_Y (updated_row));
28870 from_x = w->output_cursor.x;
28872 /* Translate to frame coordinates. */
28873 if (updated_row->full_width_p)
28875 from_x = WINDOW_TO_FRAME_PIXEL_X (w, from_x);
28876 to_x = WINDOW_TO_FRAME_PIXEL_X (w, to_x);
28878 else
28880 int area_left = window_box_left (w, updated_area);
28881 from_x += area_left;
28882 to_x += area_left;
28885 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
28886 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, max (min_y, w->output_cursor.y));
28887 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, to_y);
28889 /* Prevent inadvertently clearing to end of the X window. */
28890 if (to_x > from_x && to_y > from_y)
28892 block_input ();
28893 FRAME_RIF (f)->clear_frame_area (f, from_x, from_y,
28894 to_x - from_x, to_y - from_y);
28895 unblock_input ();
28899 #endif /* HAVE_WINDOW_SYSTEM */
28903 /***********************************************************************
28904 Cursor types
28905 ***********************************************************************/
28907 /* Value is the internal representation of the specified cursor type
28908 ARG. If type is BAR_CURSOR, return in *WIDTH the specified width
28909 of the bar cursor. */
28911 static enum text_cursor_kinds
28912 get_specified_cursor_type (Lisp_Object arg, int *width)
28914 enum text_cursor_kinds type;
28916 if (NILP (arg))
28917 return NO_CURSOR;
28919 if (EQ (arg, Qbox))
28920 return FILLED_BOX_CURSOR;
28922 if (EQ (arg, Qhollow))
28923 return HOLLOW_BOX_CURSOR;
28925 if (EQ (arg, Qbar))
28927 *width = 2;
28928 return BAR_CURSOR;
28931 if (CONSP (arg)
28932 && EQ (XCAR (arg), Qbar)
28933 && RANGED_INTEGERP (0, XCDR (arg), INT_MAX))
28935 *width = XINT (XCDR (arg));
28936 return BAR_CURSOR;
28939 if (EQ (arg, Qhbar))
28941 *width = 2;
28942 return HBAR_CURSOR;
28945 if (CONSP (arg)
28946 && EQ (XCAR (arg), Qhbar)
28947 && RANGED_INTEGERP (0, XCDR (arg), INT_MAX))
28949 *width = XINT (XCDR (arg));
28950 return HBAR_CURSOR;
28953 /* Treat anything unknown as "hollow box cursor".
28954 It was bad to signal an error; people have trouble fixing
28955 .Xdefaults with Emacs, when it has something bad in it. */
28956 type = HOLLOW_BOX_CURSOR;
28958 return type;
28961 /* Set the default cursor types for specified frame. */
28962 void
28963 set_frame_cursor_types (struct frame *f, Lisp_Object arg)
28965 int width = 1;
28966 Lisp_Object tem;
28968 FRAME_DESIRED_CURSOR (f) = get_specified_cursor_type (arg, &width);
28969 FRAME_CURSOR_WIDTH (f) = width;
28971 /* By default, set up the blink-off state depending on the on-state. */
28973 tem = Fassoc (arg, Vblink_cursor_alist, Qnil);
28974 if (!NILP (tem))
28976 FRAME_BLINK_OFF_CURSOR (f)
28977 = get_specified_cursor_type (XCDR (tem), &width);
28978 FRAME_BLINK_OFF_CURSOR_WIDTH (f) = width;
28980 else
28981 FRAME_BLINK_OFF_CURSOR (f) = DEFAULT_CURSOR;
28983 /* Make sure the cursor gets redrawn. */
28984 f->cursor_type_changed = true;
28988 #ifdef HAVE_WINDOW_SYSTEM
28990 /* Return the cursor we want to be displayed in window W. Return
28991 width of bar/hbar cursor through WIDTH arg. Return with
28992 ACTIVE_CURSOR arg set to true if cursor in window W is `active'
28993 (i.e. if the `system caret' should track this cursor).
28995 In a mini-buffer window, we want the cursor only to appear if we
28996 are reading input from this window. For the selected window, we
28997 want the cursor type given by the frame parameter or buffer local
28998 setting of cursor-type. If explicitly marked off, draw no cursor.
28999 In all other cases, we want a hollow box cursor. */
29001 static enum text_cursor_kinds
29002 get_window_cursor_type (struct window *w, struct glyph *glyph, int *width,
29003 bool *active_cursor)
29005 struct frame *f = XFRAME (w->frame);
29006 struct buffer *b = XBUFFER (w->contents);
29007 int cursor_type = DEFAULT_CURSOR;
29008 Lisp_Object alt_cursor;
29009 bool non_selected = false;
29011 *active_cursor = true;
29013 /* Echo area */
29014 if (cursor_in_echo_area
29015 && FRAME_HAS_MINIBUF_P (f)
29016 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
29018 if (w == XWINDOW (echo_area_window))
29020 if (EQ (BVAR (b, cursor_type), Qt) || NILP (BVAR (b, cursor_type)))
29022 *width = FRAME_CURSOR_WIDTH (f);
29023 return FRAME_DESIRED_CURSOR (f);
29025 else
29026 return get_specified_cursor_type (BVAR (b, cursor_type), width);
29029 *active_cursor = false;
29030 non_selected = true;
29033 /* Detect a nonselected window or nonselected frame. */
29034 else if (w != XWINDOW (f->selected_window)
29035 || f != FRAME_DISPLAY_INFO (f)->x_highlight_frame)
29037 *active_cursor = false;
29039 if (MINI_WINDOW_P (w) && minibuf_level == 0)
29040 return NO_CURSOR;
29042 non_selected = true;
29045 /* Never display a cursor in a window in which cursor-type is nil. */
29046 if (NILP (BVAR (b, cursor_type)))
29047 return NO_CURSOR;
29049 /* Get the normal cursor type for this window. */
29050 if (EQ (BVAR (b, cursor_type), Qt))
29052 cursor_type = FRAME_DESIRED_CURSOR (f);
29053 *width = FRAME_CURSOR_WIDTH (f);
29055 else
29056 cursor_type = get_specified_cursor_type (BVAR (b, cursor_type), width);
29058 /* Use cursor-in-non-selected-windows instead
29059 for non-selected window or frame. */
29060 if (non_selected)
29062 alt_cursor = BVAR (b, cursor_in_non_selected_windows);
29063 if (!EQ (Qt, alt_cursor))
29064 return get_specified_cursor_type (alt_cursor, width);
29065 /* t means modify the normal cursor type. */
29066 if (cursor_type == FILLED_BOX_CURSOR)
29067 cursor_type = HOLLOW_BOX_CURSOR;
29068 else if (cursor_type == BAR_CURSOR && *width > 1)
29069 --*width;
29070 return cursor_type;
29073 /* Use normal cursor if not blinked off. */
29074 if (!w->cursor_off_p)
29076 if (glyph != NULL && glyph->type == XWIDGET_GLYPH)
29077 return NO_CURSOR;
29078 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
29080 if (cursor_type == FILLED_BOX_CURSOR)
29082 /* Using a block cursor on large images can be very annoying.
29083 So use a hollow cursor for "large" images.
29084 If image is not transparent (no mask), also use hollow cursor. */
29085 struct image *img = IMAGE_OPT_FROM_ID (f, glyph->u.img_id);
29086 if (img != NULL && IMAGEP (img->spec))
29088 /* Arbitrarily, interpret "Large" as >32x32 and >NxN
29089 where N = size of default frame font size.
29090 This should cover most of the "tiny" icons people may use. */
29091 if (!img->mask
29092 || img->width > max (32, WINDOW_FRAME_COLUMN_WIDTH (w))
29093 || img->height > max (32, WINDOW_FRAME_LINE_HEIGHT (w)))
29094 cursor_type = HOLLOW_BOX_CURSOR;
29097 else if (cursor_type != NO_CURSOR)
29099 /* Display current only supports BOX and HOLLOW cursors for images.
29100 So for now, unconditionally use a HOLLOW cursor when cursor is
29101 not a solid box cursor. */
29102 cursor_type = HOLLOW_BOX_CURSOR;
29105 return cursor_type;
29108 /* Cursor is blinked off, so determine how to "toggle" it. */
29110 /* First look for an entry matching the buffer's cursor-type in blink-cursor-alist. */
29111 if ((alt_cursor = Fassoc (BVAR (b, cursor_type), Vblink_cursor_alist, Qnil), !NILP (alt_cursor)))
29112 return get_specified_cursor_type (XCDR (alt_cursor), width);
29114 /* Then see if frame has specified a specific blink off cursor type. */
29115 if (FRAME_BLINK_OFF_CURSOR (f) != DEFAULT_CURSOR)
29117 *width = FRAME_BLINK_OFF_CURSOR_WIDTH (f);
29118 return FRAME_BLINK_OFF_CURSOR (f);
29121 #if false
29122 /* Some people liked having a permanently visible blinking cursor,
29123 while others had very strong opinions against it. So it was
29124 decided to remove it. KFS 2003-09-03 */
29126 /* Finally perform built-in cursor blinking:
29127 filled box <-> hollow box
29128 wide [h]bar <-> narrow [h]bar
29129 narrow [h]bar <-> no cursor
29130 other type <-> no cursor */
29132 if (cursor_type == FILLED_BOX_CURSOR)
29133 return HOLLOW_BOX_CURSOR;
29135 if ((cursor_type == BAR_CURSOR || cursor_type == HBAR_CURSOR) && *width > 1)
29137 *width = 1;
29138 return cursor_type;
29140 #endif
29142 return NO_CURSOR;
29146 /* Notice when the text cursor of window W has been completely
29147 overwritten by a drawing operation that outputs glyphs in AREA
29148 starting at X0 and ending at X1 in the line starting at Y0 and
29149 ending at Y1. X coordinates are area-relative. X1 < 0 means all
29150 the rest of the line after X0 has been written. Y coordinates
29151 are window-relative. */
29153 static void
29154 notice_overwritten_cursor (struct window *w, enum glyph_row_area area,
29155 int x0, int x1, int y0, int y1)
29157 int cx0, cx1, cy0, cy1;
29158 struct glyph_row *row;
29160 if (!w->phys_cursor_on_p)
29161 return;
29162 if (area != TEXT_AREA)
29163 return;
29165 if (w->phys_cursor.vpos < 0
29166 || w->phys_cursor.vpos >= w->current_matrix->nrows
29167 || (row = w->current_matrix->rows + w->phys_cursor.vpos,
29168 !(row->enabled_p && MATRIX_ROW_DISPLAYS_TEXT_P (row))))
29169 return;
29171 if (row->cursor_in_fringe_p)
29173 row->cursor_in_fringe_p = false;
29174 draw_fringe_bitmap (w, row, row->reversed_p);
29175 w->phys_cursor_on_p = false;
29176 return;
29179 cx0 = w->phys_cursor.x;
29180 cx1 = cx0 + w->phys_cursor_width;
29181 if (x0 > cx0 || (x1 >= 0 && x1 < cx1))
29182 return;
29184 /* The cursor image will be completely removed from the
29185 screen if the output area intersects the cursor area in
29186 y-direction. When we draw in [y0 y1[, and some part of
29187 the cursor is at y < y0, that part must have been drawn
29188 before. When scrolling, the cursor is erased before
29189 actually scrolling, so we don't come here. When not
29190 scrolling, the rows above the old cursor row must have
29191 changed, and in this case these rows must have written
29192 over the cursor image.
29194 Likewise if part of the cursor is below y1, with the
29195 exception of the cursor being in the first blank row at
29196 the buffer and window end because update_text_area
29197 doesn't draw that row. (Except when it does, but
29198 that's handled in update_text_area.) */
29200 cy0 = w->phys_cursor.y;
29201 cy1 = cy0 + w->phys_cursor_height;
29202 if ((y0 < cy0 || y0 >= cy1) && (y1 <= cy0 || y1 >= cy1))
29203 return;
29205 w->phys_cursor_on_p = false;
29208 #endif /* HAVE_WINDOW_SYSTEM */
29211 /************************************************************************
29212 Mouse Face
29213 ************************************************************************/
29215 #ifdef HAVE_WINDOW_SYSTEM
29217 /* EXPORT for RIF:
29218 Fix the display of area AREA of overlapping row ROW in window W
29219 with respect to the overlapping part OVERLAPS. */
29221 void
29222 x_fix_overlapping_area (struct window *w, struct glyph_row *row,
29223 enum glyph_row_area area, int overlaps)
29225 int i, x;
29227 block_input ();
29229 x = 0;
29230 for (i = 0; i < row->used[area];)
29232 if (row->glyphs[area][i].overlaps_vertically_p)
29234 int start = i, start_x = x;
29238 x += row->glyphs[area][i].pixel_width;
29239 ++i;
29241 while (i < row->used[area]
29242 && row->glyphs[area][i].overlaps_vertically_p);
29244 draw_glyphs (w, start_x, row, area,
29245 start, i,
29246 DRAW_NORMAL_TEXT, overlaps);
29248 else
29250 x += row->glyphs[area][i].pixel_width;
29251 ++i;
29255 unblock_input ();
29259 /* EXPORT:
29260 Draw the cursor glyph of window W in glyph row ROW. See the
29261 comment of draw_glyphs for the meaning of HL. */
29263 void
29264 draw_phys_cursor_glyph (struct window *w, struct glyph_row *row,
29265 enum draw_glyphs_face hl)
29267 /* If cursor hpos is out of bounds, don't draw garbage. This can
29268 happen in mini-buffer windows when switching between echo area
29269 glyphs and mini-buffer. */
29270 if ((row->reversed_p
29271 ? (w->phys_cursor.hpos >= 0)
29272 : (w->phys_cursor.hpos < row->used[TEXT_AREA])))
29274 bool on_p = w->phys_cursor_on_p;
29275 int x1;
29276 int hpos = w->phys_cursor.hpos;
29278 /* When the window is hscrolled, cursor hpos can legitimately be
29279 out of bounds, but we draw the cursor at the corresponding
29280 window margin in that case. */
29281 if (!row->reversed_p && hpos < 0)
29282 hpos = 0;
29283 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
29284 hpos = row->used[TEXT_AREA] - 1;
29286 x1 = draw_glyphs (w, w->phys_cursor.x, row, TEXT_AREA, hpos, hpos + 1,
29287 hl, 0);
29288 w->phys_cursor_on_p = on_p;
29290 if (hl == DRAW_CURSOR)
29291 w->phys_cursor_width = x1 - w->phys_cursor.x;
29292 /* When we erase the cursor, and ROW is overlapped by other
29293 rows, make sure that these overlapping parts of other rows
29294 are redrawn. */
29295 else if (hl == DRAW_NORMAL_TEXT && row->overlapped_p)
29297 w->phys_cursor_width = x1 - w->phys_cursor.x;
29299 if (row > w->current_matrix->rows
29300 && MATRIX_ROW_OVERLAPS_SUCC_P (row - 1))
29301 x_fix_overlapping_area (w, row - 1, TEXT_AREA,
29302 OVERLAPS_ERASED_CURSOR);
29304 if (MATRIX_ROW_BOTTOM_Y (row) < window_text_bottom_y (w)
29305 && MATRIX_ROW_OVERLAPS_PRED_P (row + 1))
29306 x_fix_overlapping_area (w, row + 1, TEXT_AREA,
29307 OVERLAPS_ERASED_CURSOR);
29313 /* Erase the image of a cursor of window W from the screen. */
29315 void
29316 erase_phys_cursor (struct window *w)
29318 struct frame *f = XFRAME (w->frame);
29319 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
29320 int hpos = w->phys_cursor.hpos;
29321 int vpos = w->phys_cursor.vpos;
29322 bool mouse_face_here_p = false;
29323 struct glyph_matrix *active_glyphs = w->current_matrix;
29324 struct glyph_row *cursor_row;
29325 struct glyph *cursor_glyph;
29326 enum draw_glyphs_face hl;
29328 /* No cursor displayed or row invalidated => nothing to do on the
29329 screen. */
29330 if (w->phys_cursor_type == NO_CURSOR)
29331 goto mark_cursor_off;
29333 /* VPOS >= active_glyphs->nrows means that window has been resized.
29334 Don't bother to erase the cursor. */
29335 if (vpos >= active_glyphs->nrows)
29336 goto mark_cursor_off;
29338 /* If row containing cursor is marked invalid, there is nothing we
29339 can do. */
29340 cursor_row = MATRIX_ROW (active_glyphs, vpos);
29341 if (!cursor_row->enabled_p)
29342 goto mark_cursor_off;
29344 /* If line spacing is > 0, old cursor may only be partially visible in
29345 window after split-window. So adjust visible height. */
29346 cursor_row->visible_height = min (cursor_row->visible_height,
29347 window_text_bottom_y (w) - cursor_row->y);
29349 /* If row is completely invisible, don't attempt to delete a cursor which
29350 isn't there. This can happen if cursor is at top of a window, and
29351 we switch to a buffer with a header line in that window. */
29352 if (cursor_row->visible_height <= 0)
29353 goto mark_cursor_off;
29355 /* If cursor is in the fringe, erase by drawing actual bitmap there. */
29356 if (cursor_row->cursor_in_fringe_p)
29358 cursor_row->cursor_in_fringe_p = false;
29359 draw_fringe_bitmap (w, cursor_row, cursor_row->reversed_p);
29360 goto mark_cursor_off;
29363 /* This can happen when the new row is shorter than the old one.
29364 In this case, either draw_glyphs or clear_end_of_line
29365 should have cleared the cursor. Note that we wouldn't be
29366 able to erase the cursor in this case because we don't have a
29367 cursor glyph at hand. */
29368 if ((cursor_row->reversed_p
29369 ? (w->phys_cursor.hpos < 0)
29370 : (w->phys_cursor.hpos >= cursor_row->used[TEXT_AREA])))
29371 goto mark_cursor_off;
29373 /* When the window is hscrolled, cursor hpos can legitimately be out
29374 of bounds, but we draw the cursor at the corresponding window
29375 margin in that case. */
29376 if (!cursor_row->reversed_p && hpos < 0)
29377 hpos = 0;
29378 if (cursor_row->reversed_p && hpos >= cursor_row->used[TEXT_AREA])
29379 hpos = cursor_row->used[TEXT_AREA] - 1;
29381 /* If the cursor is in the mouse face area, redisplay that when
29382 we clear the cursor. */
29383 if (! NILP (hlinfo->mouse_face_window)
29384 && coords_in_mouse_face_p (w, hpos, vpos)
29385 /* Don't redraw the cursor's spot in mouse face if it is at the
29386 end of a line (on a newline). The cursor appears there, but
29387 mouse highlighting does not. */
29388 && cursor_row->used[TEXT_AREA] > hpos && hpos >= 0)
29389 mouse_face_here_p = true;
29391 /* Maybe clear the display under the cursor. */
29392 if (w->phys_cursor_type == HOLLOW_BOX_CURSOR)
29394 int x, y;
29395 int header_line_height = WINDOW_HEADER_LINE_HEIGHT (w);
29396 int width;
29398 cursor_glyph = get_phys_cursor_glyph (w);
29399 if (cursor_glyph == NULL)
29400 goto mark_cursor_off;
29402 width = cursor_glyph->pixel_width;
29403 x = w->phys_cursor.x;
29404 if (x < 0)
29406 width += x;
29407 x = 0;
29409 width = min (width, window_box_width (w, TEXT_AREA) - x);
29410 y = WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height, cursor_row->y));
29411 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, x);
29413 if (width > 0)
29414 FRAME_RIF (f)->clear_frame_area (f, x, y, width, cursor_row->visible_height);
29417 /* Erase the cursor by redrawing the character underneath it. */
29418 if (mouse_face_here_p)
29419 hl = DRAW_MOUSE_FACE;
29420 else
29421 hl = DRAW_NORMAL_TEXT;
29422 draw_phys_cursor_glyph (w, cursor_row, hl);
29424 mark_cursor_off:
29425 w->phys_cursor_on_p = false;
29426 w->phys_cursor_type = NO_CURSOR;
29430 /* Display or clear cursor of window W. If !ON, clear the cursor.
29431 If ON, display the cursor; where to put the cursor is specified by
29432 HPOS, VPOS, X and Y. */
29434 void
29435 display_and_set_cursor (struct window *w, bool on,
29436 int hpos, int vpos, int x, int y)
29438 struct frame *f = XFRAME (w->frame);
29439 int new_cursor_type;
29440 int new_cursor_width;
29441 bool active_cursor;
29442 struct glyph_row *glyph_row;
29443 struct glyph *glyph;
29445 /* This is pointless on invisible frames, and dangerous on garbaged
29446 windows and frames; in the latter case, the frame or window may
29447 be in the midst of changing its size, and x and y may be off the
29448 window. */
29449 if (! FRAME_VISIBLE_P (f)
29450 || vpos >= w->current_matrix->nrows
29451 || hpos >= w->current_matrix->matrix_w)
29452 return;
29454 /* If cursor is off and we want it off, return quickly. */
29455 if (!on && !w->phys_cursor_on_p)
29456 return;
29458 glyph_row = MATRIX_ROW (w->current_matrix, vpos);
29459 /* If cursor row is not enabled, we don't really know where to
29460 display the cursor. */
29461 if (!glyph_row->enabled_p)
29463 w->phys_cursor_on_p = false;
29464 return;
29467 /* A frame might be marked garbaged even though its cursor position
29468 is correct, and will not change upon subsequent redisplay. This
29469 happens in some rare situations, like toggling the sort order in
29470 Dired windows. We've already established that VPOS is valid, so
29471 it shouldn't do any harm to record the cursor position, as we are
29472 going to return without acting on it anyway. Otherwise, expose
29473 events might come in and call update_window_cursor, which will
29474 blindly use outdated values in w->phys_cursor. */
29475 if (FRAME_GARBAGED_P (f))
29477 if (on)
29479 w->phys_cursor.x = x;
29480 w->phys_cursor.y = glyph_row->y;
29481 w->phys_cursor.hpos = hpos;
29482 w->phys_cursor.vpos = vpos;
29484 return;
29487 glyph = NULL;
29488 if (0 <= hpos && hpos < glyph_row->used[TEXT_AREA])
29489 glyph = glyph_row->glyphs[TEXT_AREA] + hpos;
29491 eassert (input_blocked_p ());
29493 /* Set new_cursor_type to the cursor we want to be displayed. */
29494 new_cursor_type = get_window_cursor_type (w, glyph,
29495 &new_cursor_width, &active_cursor);
29497 /* If cursor is currently being shown and we don't want it to be or
29498 it is in the wrong place, or the cursor type is not what we want,
29499 erase it. */
29500 if (w->phys_cursor_on_p
29501 && (!on
29502 || w->phys_cursor.x != x
29503 || w->phys_cursor.y != y
29504 /* HPOS can be negative in R2L rows whose
29505 exact_window_width_line_p flag is set (i.e. their newline
29506 would "overflow into the fringe"). */
29507 || hpos < 0
29508 || new_cursor_type != w->phys_cursor_type
29509 || ((new_cursor_type == BAR_CURSOR || new_cursor_type == HBAR_CURSOR)
29510 && new_cursor_width != w->phys_cursor_width)))
29511 erase_phys_cursor (w);
29513 /* Don't check phys_cursor_on_p here because that flag is only set
29514 to false in some cases where we know that the cursor has been
29515 completely erased, to avoid the extra work of erasing the cursor
29516 twice. In other words, phys_cursor_on_p can be true and the cursor
29517 still not be visible, or it has only been partly erased. */
29518 if (on)
29520 w->phys_cursor_ascent = glyph_row->ascent;
29521 w->phys_cursor_height = glyph_row->height;
29523 /* Set phys_cursor_.* before x_draw_.* is called because some
29524 of them may need the information. */
29525 w->phys_cursor.x = x;
29526 w->phys_cursor.y = glyph_row->y;
29527 w->phys_cursor.hpos = hpos;
29528 w->phys_cursor.vpos = vpos;
29531 FRAME_RIF (f)->draw_window_cursor (w, glyph_row, x, y,
29532 new_cursor_type, new_cursor_width,
29533 on, active_cursor);
29537 /* Switch the display of W's cursor on or off, according to the value
29538 of ON. */
29540 static void
29541 update_window_cursor (struct window *w, bool on)
29543 /* Don't update cursor in windows whose frame is in the process
29544 of being deleted. */
29545 if (w->current_matrix)
29547 int hpos = w->phys_cursor.hpos;
29548 int vpos = w->phys_cursor.vpos;
29549 struct glyph_row *row;
29551 if (vpos >= w->current_matrix->nrows
29552 || hpos >= w->current_matrix->matrix_w)
29553 return;
29555 row = MATRIX_ROW (w->current_matrix, vpos);
29557 /* When the window is hscrolled, cursor hpos can legitimately be
29558 out of bounds, but we draw the cursor at the corresponding
29559 window margin in that case. */
29560 if (!row->reversed_p && hpos < 0)
29561 hpos = 0;
29562 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
29563 hpos = row->used[TEXT_AREA] - 1;
29565 block_input ();
29566 display_and_set_cursor (w, on, hpos, vpos,
29567 w->phys_cursor.x, w->phys_cursor.y);
29568 unblock_input ();
29573 /* Call update_window_cursor with parameter ON_P on all leaf windows
29574 in the window tree rooted at W. */
29576 static void
29577 update_cursor_in_window_tree (struct window *w, bool on_p)
29579 while (w)
29581 if (WINDOWP (w->contents))
29582 update_cursor_in_window_tree (XWINDOW (w->contents), on_p);
29583 else
29584 update_window_cursor (w, on_p);
29586 w = NILP (w->next) ? 0 : XWINDOW (w->next);
29591 /* EXPORT:
29592 Display the cursor on window W, or clear it, according to ON_P.
29593 Don't change the cursor's position. */
29595 void
29596 x_update_cursor (struct frame *f, bool on_p)
29598 update_cursor_in_window_tree (XWINDOW (f->root_window), on_p);
29602 /* EXPORT:
29603 Clear the cursor of window W to background color, and mark the
29604 cursor as not shown. This is used when the text where the cursor
29605 is about to be rewritten. */
29607 void
29608 x_clear_cursor (struct window *w)
29610 if (FRAME_VISIBLE_P (XFRAME (w->frame)) && w->phys_cursor_on_p)
29611 update_window_cursor (w, false);
29614 #endif /* HAVE_WINDOW_SYSTEM */
29616 /* Implementation of draw_row_with_mouse_face for GUI sessions, GPM,
29617 and MSDOS. */
29618 static void
29619 draw_row_with_mouse_face (struct window *w, int start_x, struct glyph_row *row,
29620 int start_hpos, int end_hpos,
29621 enum draw_glyphs_face draw)
29623 #ifdef HAVE_WINDOW_SYSTEM
29624 if (FRAME_WINDOW_P (XFRAME (w->frame)))
29626 draw_glyphs (w, start_x, row, TEXT_AREA, start_hpos, end_hpos, draw, 0);
29627 return;
29629 #endif
29630 #if defined (HAVE_GPM) || defined (MSDOS) || defined (WINDOWSNT)
29631 tty_draw_row_with_mouse_face (w, row, start_hpos, end_hpos, draw);
29632 #endif
29635 /* Display the active region described by mouse_face_* according to DRAW. */
29637 static void
29638 show_mouse_face (Mouse_HLInfo *hlinfo, enum draw_glyphs_face draw)
29640 struct window *w = XWINDOW (hlinfo->mouse_face_window);
29641 struct frame *f = XFRAME (WINDOW_FRAME (w));
29643 if (/* If window is in the process of being destroyed, don't bother
29644 to do anything. */
29645 w->current_matrix != NULL
29646 /* Don't update mouse highlight if hidden. */
29647 && (draw != DRAW_MOUSE_FACE || !hlinfo->mouse_face_hidden)
29648 /* Recognize when we are called to operate on rows that don't exist
29649 anymore. This can happen when a window is split. */
29650 && hlinfo->mouse_face_end_row < w->current_matrix->nrows)
29652 bool phys_cursor_on_p = w->phys_cursor_on_p;
29653 struct glyph_row *row, *first, *last;
29655 first = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_beg_row);
29656 last = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_end_row);
29658 for (row = first; row <= last && row->enabled_p; ++row)
29660 int start_hpos, end_hpos, start_x;
29662 /* For all but the first row, the highlight starts at column 0. */
29663 if (row == first)
29665 /* R2L rows have BEG and END in reversed order, but the
29666 screen drawing geometry is always left to right. So
29667 we need to mirror the beginning and end of the
29668 highlighted area in R2L rows. */
29669 if (!row->reversed_p)
29671 start_hpos = hlinfo->mouse_face_beg_col;
29672 start_x = hlinfo->mouse_face_beg_x;
29674 else if (row == last)
29676 start_hpos = hlinfo->mouse_face_end_col;
29677 start_x = hlinfo->mouse_face_end_x;
29679 else
29681 start_hpos = 0;
29682 start_x = 0;
29685 else if (row->reversed_p && row == last)
29687 start_hpos = hlinfo->mouse_face_end_col;
29688 start_x = hlinfo->mouse_face_end_x;
29690 else
29692 start_hpos = 0;
29693 start_x = 0;
29696 if (row == last)
29698 if (!row->reversed_p)
29699 end_hpos = hlinfo->mouse_face_end_col;
29700 else if (row == first)
29701 end_hpos = hlinfo->mouse_face_beg_col;
29702 else
29704 end_hpos = row->used[TEXT_AREA];
29705 if (draw == DRAW_NORMAL_TEXT)
29706 row->fill_line_p = true; /* Clear to end of line. */
29709 else if (row->reversed_p && row == first)
29710 end_hpos = hlinfo->mouse_face_beg_col;
29711 else
29713 end_hpos = row->used[TEXT_AREA];
29714 if (draw == DRAW_NORMAL_TEXT)
29715 row->fill_line_p = true; /* Clear to end of line. */
29718 if (end_hpos > start_hpos)
29720 draw_row_with_mouse_face (w, start_x, row,
29721 start_hpos, end_hpos, draw);
29723 row->mouse_face_p
29724 = draw == DRAW_MOUSE_FACE || draw == DRAW_IMAGE_RAISED;
29728 /* When we've written over the cursor, arrange for it to
29729 be displayed again. */
29730 if (FRAME_WINDOW_P (f)
29731 && phys_cursor_on_p && !w->phys_cursor_on_p)
29733 #ifdef HAVE_WINDOW_SYSTEM
29734 int hpos = w->phys_cursor.hpos;
29736 /* When the window is hscrolled, cursor hpos can legitimately be
29737 out of bounds, but we draw the cursor at the corresponding
29738 window margin in that case. */
29739 if (!row->reversed_p && hpos < 0)
29740 hpos = 0;
29741 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
29742 hpos = row->used[TEXT_AREA] - 1;
29744 block_input ();
29745 display_and_set_cursor (w, true, hpos, w->phys_cursor.vpos,
29746 w->phys_cursor.x, w->phys_cursor.y);
29747 unblock_input ();
29748 #endif /* HAVE_WINDOW_SYSTEM */
29752 #ifdef HAVE_WINDOW_SYSTEM
29753 /* Change the mouse cursor. */
29754 if (FRAME_WINDOW_P (f) && NILP (do_mouse_tracking))
29756 #if ! defined (USE_GTK) && ! defined (HAVE_NS)
29757 if (draw == DRAW_NORMAL_TEXT
29758 && !EQ (hlinfo->mouse_face_window, f->tool_bar_window))
29759 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->text_cursor);
29760 else
29761 #endif
29762 if (draw == DRAW_MOUSE_FACE)
29763 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->hand_cursor);
29764 else
29765 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->nontext_cursor);
29767 #endif /* HAVE_WINDOW_SYSTEM */
29770 /* EXPORT:
29771 Clear out the mouse-highlighted active region.
29772 Redraw it un-highlighted first. Value is true if mouse
29773 face was actually drawn unhighlighted. */
29775 bool
29776 clear_mouse_face (Mouse_HLInfo *hlinfo)
29778 bool cleared
29779 = !hlinfo->mouse_face_hidden && !NILP (hlinfo->mouse_face_window);
29780 if (cleared)
29781 show_mouse_face (hlinfo, DRAW_NORMAL_TEXT);
29782 hlinfo->mouse_face_beg_row = hlinfo->mouse_face_beg_col = -1;
29783 hlinfo->mouse_face_end_row = hlinfo->mouse_face_end_col = -1;
29784 hlinfo->mouse_face_window = Qnil;
29785 hlinfo->mouse_face_overlay = Qnil;
29786 return cleared;
29789 /* Return true if the coordinates HPOS and VPOS on windows W are
29790 within the mouse face on that window. */
29791 static bool
29792 coords_in_mouse_face_p (struct window *w, int hpos, int vpos)
29794 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (XFRAME (w->frame));
29796 /* Quickly resolve the easy cases. */
29797 if (!(WINDOWP (hlinfo->mouse_face_window)
29798 && XWINDOW (hlinfo->mouse_face_window) == w))
29799 return false;
29800 if (vpos < hlinfo->mouse_face_beg_row
29801 || vpos > hlinfo->mouse_face_end_row)
29802 return false;
29803 if (vpos > hlinfo->mouse_face_beg_row
29804 && vpos < hlinfo->mouse_face_end_row)
29805 return true;
29807 if (!MATRIX_ROW (w->current_matrix, vpos)->reversed_p)
29809 if (hlinfo->mouse_face_beg_row == hlinfo->mouse_face_end_row)
29811 if (hlinfo->mouse_face_beg_col <= hpos && hpos < hlinfo->mouse_face_end_col)
29812 return true;
29814 else if ((vpos == hlinfo->mouse_face_beg_row
29815 && hpos >= hlinfo->mouse_face_beg_col)
29816 || (vpos == hlinfo->mouse_face_end_row
29817 && hpos < hlinfo->mouse_face_end_col))
29818 return true;
29820 else
29822 if (hlinfo->mouse_face_beg_row == hlinfo->mouse_face_end_row)
29824 if (hlinfo->mouse_face_end_col < hpos && hpos <= hlinfo->mouse_face_beg_col)
29825 return true;
29827 else if ((vpos == hlinfo->mouse_face_beg_row
29828 && hpos <= hlinfo->mouse_face_beg_col)
29829 || (vpos == hlinfo->mouse_face_end_row
29830 && hpos > hlinfo->mouse_face_end_col))
29831 return true;
29833 return false;
29837 /* EXPORT:
29838 True if physical cursor of window W is within mouse face. */
29840 bool
29841 cursor_in_mouse_face_p (struct window *w)
29843 int hpos = w->phys_cursor.hpos;
29844 int vpos = w->phys_cursor.vpos;
29845 struct glyph_row *row = MATRIX_ROW (w->current_matrix, vpos);
29847 /* When the window is hscrolled, cursor hpos can legitimately be out
29848 of bounds, but we draw the cursor at the corresponding window
29849 margin in that case. */
29850 if (!row->reversed_p && hpos < 0)
29851 hpos = 0;
29852 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
29853 hpos = row->used[TEXT_AREA] - 1;
29855 return coords_in_mouse_face_p (w, hpos, vpos);
29860 /* Find the glyph rows START_ROW and END_ROW of window W that display
29861 characters between buffer positions START_CHARPOS and END_CHARPOS
29862 (excluding END_CHARPOS). DISP_STRING is a display string that
29863 covers these buffer positions. This is similar to
29864 row_containing_pos, but is more accurate when bidi reordering makes
29865 buffer positions change non-linearly with glyph rows. */
29866 static void
29867 rows_from_pos_range (struct window *w,
29868 ptrdiff_t start_charpos, ptrdiff_t end_charpos,
29869 Lisp_Object disp_string,
29870 struct glyph_row **start, struct glyph_row **end)
29872 struct glyph_row *first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
29873 int last_y = window_text_bottom_y (w);
29874 struct glyph_row *row;
29876 *start = NULL;
29877 *end = NULL;
29879 while (!first->enabled_p
29880 && first < MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w))
29881 first++;
29883 /* Find the START row. */
29884 for (row = first;
29885 row->enabled_p && MATRIX_ROW_BOTTOM_Y (row) <= last_y;
29886 row++)
29888 /* A row can potentially be the START row if the range of the
29889 characters it displays intersects the range
29890 [START_CHARPOS..END_CHARPOS). */
29891 if (! ((start_charpos < MATRIX_ROW_START_CHARPOS (row)
29892 && end_charpos < MATRIX_ROW_START_CHARPOS (row))
29893 /* See the commentary in row_containing_pos, for the
29894 explanation of the complicated way to check whether
29895 some position is beyond the end of the characters
29896 displayed by a row. */
29897 || ((start_charpos > MATRIX_ROW_END_CHARPOS (row)
29898 || (start_charpos == MATRIX_ROW_END_CHARPOS (row)
29899 && !row->ends_at_zv_p
29900 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
29901 && (end_charpos > MATRIX_ROW_END_CHARPOS (row)
29902 || (end_charpos == MATRIX_ROW_END_CHARPOS (row)
29903 && !row->ends_at_zv_p
29904 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))))))
29906 /* Found a candidate row. Now make sure at least one of the
29907 glyphs it displays has a charpos from the range
29908 [START_CHARPOS..END_CHARPOS).
29910 This is not obvious because bidi reordering could make
29911 buffer positions of a row be 1,2,3,102,101,100, and if we
29912 want to highlight characters in [50..60), we don't want
29913 this row, even though [50..60) does intersect [1..103),
29914 the range of character positions given by the row's start
29915 and end positions. */
29916 struct glyph *g = row->glyphs[TEXT_AREA];
29917 struct glyph *e = g + row->used[TEXT_AREA];
29919 while (g < e)
29921 if (((BUFFERP (g->object) || NILP (g->object))
29922 && start_charpos <= g->charpos && g->charpos < end_charpos)
29923 /* A glyph that comes from DISP_STRING is by
29924 definition to be highlighted. */
29925 || EQ (g->object, disp_string))
29926 *start = row;
29927 g++;
29929 if (*start)
29930 break;
29934 /* Find the END row. */
29935 if (!*start
29936 /* If the last row is partially visible, start looking for END
29937 from that row, instead of starting from FIRST. */
29938 && !(row->enabled_p
29939 && row->y < last_y && MATRIX_ROW_BOTTOM_Y (row) > last_y))
29940 row = first;
29941 for ( ; row->enabled_p && MATRIX_ROW_BOTTOM_Y (row) <= last_y; row++)
29943 struct glyph_row *next = row + 1;
29944 ptrdiff_t next_start = MATRIX_ROW_START_CHARPOS (next);
29946 if (!next->enabled_p
29947 || next >= MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w)
29948 /* The first row >= START whose range of displayed characters
29949 does NOT intersect the range [START_CHARPOS..END_CHARPOS]
29950 is the row END + 1. */
29951 || (start_charpos < next_start
29952 && end_charpos < next_start)
29953 || ((start_charpos > MATRIX_ROW_END_CHARPOS (next)
29954 || (start_charpos == MATRIX_ROW_END_CHARPOS (next)
29955 && !next->ends_at_zv_p
29956 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (next)))
29957 && (end_charpos > MATRIX_ROW_END_CHARPOS (next)
29958 || (end_charpos == MATRIX_ROW_END_CHARPOS (next)
29959 && !next->ends_at_zv_p
29960 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (next)))))
29962 *end = row;
29963 break;
29965 else
29967 /* If the next row's edges intersect [START_CHARPOS..END_CHARPOS],
29968 but none of the characters it displays are in the range, it is
29969 also END + 1. */
29970 struct glyph *g = next->glyphs[TEXT_AREA];
29971 struct glyph *s = g;
29972 struct glyph *e = g + next->used[TEXT_AREA];
29974 while (g < e)
29976 if (((BUFFERP (g->object) || NILP (g->object))
29977 && ((start_charpos <= g->charpos && g->charpos < end_charpos)
29978 /* If the buffer position of the first glyph in
29979 the row is equal to END_CHARPOS, it means
29980 the last character to be highlighted is the
29981 newline of ROW, and we must consider NEXT as
29982 END, not END+1. */
29983 || (((!next->reversed_p && g == s)
29984 || (next->reversed_p && g == e - 1))
29985 && (g->charpos == end_charpos
29986 /* Special case for when NEXT is an
29987 empty line at ZV. */
29988 || (g->charpos == -1
29989 && !row->ends_at_zv_p
29990 && next_start == end_charpos)))))
29991 /* A glyph that comes from DISP_STRING is by
29992 definition to be highlighted. */
29993 || EQ (g->object, disp_string))
29994 break;
29995 g++;
29997 if (g == e)
29999 *end = row;
30000 break;
30002 /* The first row that ends at ZV must be the last to be
30003 highlighted. */
30004 else if (next->ends_at_zv_p)
30006 *end = next;
30007 break;
30013 /* This function sets the mouse_face_* elements of HLINFO, assuming
30014 the mouse cursor is on a glyph with buffer charpos MOUSE_CHARPOS in
30015 window WINDOW. START_CHARPOS and END_CHARPOS are buffer positions
30016 for the overlay or run of text properties specifying the mouse
30017 face. BEFORE_STRING and AFTER_STRING, if non-nil, are a
30018 before-string and after-string that must also be highlighted.
30019 DISP_STRING, if non-nil, is a display string that may cover some
30020 or all of the highlighted text. */
30022 static void
30023 mouse_face_from_buffer_pos (Lisp_Object window,
30024 Mouse_HLInfo *hlinfo,
30025 ptrdiff_t mouse_charpos,
30026 ptrdiff_t start_charpos,
30027 ptrdiff_t end_charpos,
30028 Lisp_Object before_string,
30029 Lisp_Object after_string,
30030 Lisp_Object disp_string)
30032 struct window *w = XWINDOW (window);
30033 struct glyph_row *first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
30034 struct glyph_row *r1, *r2;
30035 struct glyph *glyph, *end;
30036 ptrdiff_t ignore, pos;
30037 int x;
30039 eassert (NILP (disp_string) || STRINGP (disp_string));
30040 eassert (NILP (before_string) || STRINGP (before_string));
30041 eassert (NILP (after_string) || STRINGP (after_string));
30043 /* Find the rows corresponding to START_CHARPOS and END_CHARPOS. */
30044 rows_from_pos_range (w, start_charpos, end_charpos, disp_string, &r1, &r2);
30045 if (r1 == NULL)
30046 r1 = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
30047 /* If the before-string or display-string contains newlines,
30048 rows_from_pos_range skips to its last row. Move back. */
30049 if (!NILP (before_string) || !NILP (disp_string))
30051 struct glyph_row *prev;
30052 while ((prev = r1 - 1, prev >= first)
30053 && MATRIX_ROW_END_CHARPOS (prev) == start_charpos
30054 && prev->used[TEXT_AREA] > 0)
30056 struct glyph *beg = prev->glyphs[TEXT_AREA];
30057 glyph = beg + prev->used[TEXT_AREA];
30058 while (--glyph >= beg && NILP (glyph->object));
30059 if (glyph < beg
30060 || !(EQ (glyph->object, before_string)
30061 || EQ (glyph->object, disp_string)))
30062 break;
30063 r1 = prev;
30066 if (r2 == NULL)
30068 r2 = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
30069 hlinfo->mouse_face_past_end = true;
30071 else if (!NILP (after_string))
30073 /* If the after-string has newlines, advance to its last row. */
30074 struct glyph_row *next;
30075 struct glyph_row *last
30076 = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
30078 for (next = r2 + 1;
30079 next <= last
30080 && next->used[TEXT_AREA] > 0
30081 && EQ (next->glyphs[TEXT_AREA]->object, after_string);
30082 ++next)
30083 r2 = next;
30085 /* The rest of the display engine assumes that mouse_face_beg_row is
30086 either above mouse_face_end_row or identical to it. But with
30087 bidi-reordered continued lines, the row for START_CHARPOS could
30088 be below the row for END_CHARPOS. If so, swap the rows and store
30089 them in correct order. */
30090 if (r1->y > r2->y)
30092 struct glyph_row *tem = r2;
30094 r2 = r1;
30095 r1 = tem;
30098 hlinfo->mouse_face_beg_row = MATRIX_ROW_VPOS (r1, w->current_matrix);
30099 hlinfo->mouse_face_end_row = MATRIX_ROW_VPOS (r2, w->current_matrix);
30101 /* For a bidi-reordered row, the positions of BEFORE_STRING,
30102 AFTER_STRING, DISP_STRING, START_CHARPOS, and END_CHARPOS
30103 could be anywhere in the row and in any order. The strategy
30104 below is to find the leftmost and the rightmost glyph that
30105 belongs to either of these 3 strings, or whose position is
30106 between START_CHARPOS and END_CHARPOS, and highlight all the
30107 glyphs between those two. This may cover more than just the text
30108 between START_CHARPOS and END_CHARPOS if the range of characters
30109 strides the bidi level boundary, e.g. if the beginning is in R2L
30110 text while the end is in L2R text or vice versa. */
30111 if (!r1->reversed_p)
30113 /* This row is in a left to right paragraph. Scan it left to
30114 right. */
30115 glyph = r1->glyphs[TEXT_AREA];
30116 end = glyph + r1->used[TEXT_AREA];
30117 x = r1->x;
30119 /* Skip truncation glyphs at the start of the glyph row. */
30120 if (MATRIX_ROW_DISPLAYS_TEXT_P (r1))
30121 for (; glyph < end
30122 && NILP (glyph->object)
30123 && glyph->charpos < 0;
30124 ++glyph)
30125 x += glyph->pixel_width;
30127 /* Scan the glyph row, looking for BEFORE_STRING, AFTER_STRING,
30128 or DISP_STRING, and the first glyph from buffer whose
30129 position is between START_CHARPOS and END_CHARPOS. */
30130 for (; glyph < end
30131 && !NILP (glyph->object)
30132 && !EQ (glyph->object, disp_string)
30133 && !(BUFFERP (glyph->object)
30134 && (glyph->charpos >= start_charpos
30135 && glyph->charpos < end_charpos));
30136 ++glyph)
30138 /* BEFORE_STRING or AFTER_STRING are only relevant if they
30139 are present at buffer positions between START_CHARPOS and
30140 END_CHARPOS, or if they come from an overlay. */
30141 if (EQ (glyph->object, before_string))
30143 pos = string_buffer_position (before_string,
30144 start_charpos);
30145 /* If pos == 0, it means before_string came from an
30146 overlay, not from a buffer position. */
30147 if (!pos || (pos >= start_charpos && pos < end_charpos))
30148 break;
30150 else if (EQ (glyph->object, after_string))
30152 pos = string_buffer_position (after_string, end_charpos);
30153 if (!pos || (pos >= start_charpos && pos < end_charpos))
30154 break;
30156 x += glyph->pixel_width;
30158 hlinfo->mouse_face_beg_x = x;
30159 hlinfo->mouse_face_beg_col = glyph - r1->glyphs[TEXT_AREA];
30161 else
30163 /* This row is in a right to left paragraph. Scan it right to
30164 left. */
30165 struct glyph *g;
30167 end = r1->glyphs[TEXT_AREA] - 1;
30168 glyph = end + r1->used[TEXT_AREA];
30170 /* Skip truncation glyphs at the start of the glyph row. */
30171 if (MATRIX_ROW_DISPLAYS_TEXT_P (r1))
30172 for (; glyph > end
30173 && NILP (glyph->object)
30174 && glyph->charpos < 0;
30175 --glyph)
30178 /* Scan the glyph row, looking for BEFORE_STRING, AFTER_STRING,
30179 or DISP_STRING, and the first glyph from buffer whose
30180 position is between START_CHARPOS and END_CHARPOS. */
30181 for (; glyph > end
30182 && !NILP (glyph->object)
30183 && !EQ (glyph->object, disp_string)
30184 && !(BUFFERP (glyph->object)
30185 && (glyph->charpos >= start_charpos
30186 && glyph->charpos < end_charpos));
30187 --glyph)
30189 /* BEFORE_STRING or AFTER_STRING are only relevant if they
30190 are present at buffer positions between START_CHARPOS and
30191 END_CHARPOS, or if they come from an overlay. */
30192 if (EQ (glyph->object, before_string))
30194 pos = string_buffer_position (before_string, start_charpos);
30195 /* If pos == 0, it means before_string came from an
30196 overlay, not from a buffer position. */
30197 if (!pos || (pos >= start_charpos && pos < end_charpos))
30198 break;
30200 else if (EQ (glyph->object, after_string))
30202 pos = string_buffer_position (after_string, end_charpos);
30203 if (!pos || (pos >= start_charpos && pos < end_charpos))
30204 break;
30208 glyph++; /* first glyph to the right of the highlighted area */
30209 for (g = r1->glyphs[TEXT_AREA], x = r1->x; g < glyph; g++)
30210 x += g->pixel_width;
30211 hlinfo->mouse_face_beg_x = x;
30212 hlinfo->mouse_face_beg_col = glyph - r1->glyphs[TEXT_AREA];
30215 /* If the highlight ends in a different row, compute GLYPH and END
30216 for the end row. Otherwise, reuse the values computed above for
30217 the row where the highlight begins. */
30218 if (r2 != r1)
30220 if (!r2->reversed_p)
30222 glyph = r2->glyphs[TEXT_AREA];
30223 end = glyph + r2->used[TEXT_AREA];
30224 x = r2->x;
30226 else
30228 end = r2->glyphs[TEXT_AREA] - 1;
30229 glyph = end + r2->used[TEXT_AREA];
30233 if (!r2->reversed_p)
30235 /* Skip truncation and continuation glyphs near the end of the
30236 row, and also blanks and stretch glyphs inserted by
30237 extend_face_to_end_of_line. */
30238 while (end > glyph
30239 && NILP ((end - 1)->object))
30240 --end;
30241 /* Scan the rest of the glyph row from the end, looking for the
30242 first glyph that comes from BEFORE_STRING, AFTER_STRING, or
30243 DISP_STRING, or whose position is between START_CHARPOS
30244 and END_CHARPOS */
30245 for (--end;
30246 end > glyph
30247 && !NILP (end->object)
30248 && !EQ (end->object, disp_string)
30249 && !(BUFFERP (end->object)
30250 && (end->charpos >= start_charpos
30251 && end->charpos < end_charpos));
30252 --end)
30254 /* BEFORE_STRING or AFTER_STRING are only relevant if they
30255 are present at buffer positions between START_CHARPOS and
30256 END_CHARPOS, or if they come from an overlay. */
30257 if (EQ (end->object, before_string))
30259 pos = string_buffer_position (before_string, start_charpos);
30260 if (!pos || (pos >= start_charpos && pos < end_charpos))
30261 break;
30263 else if (EQ (end->object, after_string))
30265 pos = string_buffer_position (after_string, end_charpos);
30266 if (!pos || (pos >= start_charpos && pos < end_charpos))
30267 break;
30270 /* Find the X coordinate of the last glyph to be highlighted. */
30271 for (; glyph <= end; ++glyph)
30272 x += glyph->pixel_width;
30274 hlinfo->mouse_face_end_x = x;
30275 hlinfo->mouse_face_end_col = glyph - r2->glyphs[TEXT_AREA];
30277 else
30279 /* Skip truncation and continuation glyphs near the end of the
30280 row, and also blanks and stretch glyphs inserted by
30281 extend_face_to_end_of_line. */
30282 x = r2->x;
30283 end++;
30284 while (end < glyph
30285 && NILP (end->object))
30287 x += end->pixel_width;
30288 ++end;
30290 /* Scan the rest of the glyph row from the end, looking for the
30291 first glyph that comes from BEFORE_STRING, AFTER_STRING, or
30292 DISP_STRING, or whose position is between START_CHARPOS
30293 and END_CHARPOS */
30294 for ( ;
30295 end < glyph
30296 && !NILP (end->object)
30297 && !EQ (end->object, disp_string)
30298 && !(BUFFERP (end->object)
30299 && (end->charpos >= start_charpos
30300 && end->charpos < end_charpos));
30301 ++end)
30303 /* BEFORE_STRING or AFTER_STRING are only relevant if they
30304 are present at buffer positions between START_CHARPOS and
30305 END_CHARPOS, or if they come from an overlay. */
30306 if (EQ (end->object, before_string))
30308 pos = string_buffer_position (before_string, start_charpos);
30309 if (!pos || (pos >= start_charpos && pos < end_charpos))
30310 break;
30312 else if (EQ (end->object, after_string))
30314 pos = string_buffer_position (after_string, end_charpos);
30315 if (!pos || (pos >= start_charpos && pos < end_charpos))
30316 break;
30318 x += end->pixel_width;
30320 /* If we exited the above loop because we arrived at the last
30321 glyph of the row, and its buffer position is still not in
30322 range, it means the last character in range is the preceding
30323 newline. Bump the end column and x values to get past the
30324 last glyph. */
30325 if (end == glyph
30326 && BUFFERP (end->object)
30327 && (end->charpos < start_charpos
30328 || end->charpos >= end_charpos))
30330 x += end->pixel_width;
30331 ++end;
30333 hlinfo->mouse_face_end_x = x;
30334 hlinfo->mouse_face_end_col = end - r2->glyphs[TEXT_AREA];
30337 hlinfo->mouse_face_window = window;
30338 hlinfo->mouse_face_face_id
30339 = face_at_buffer_position (w, mouse_charpos, &ignore,
30340 mouse_charpos + 1,
30341 !hlinfo->mouse_face_hidden, -1);
30342 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
30345 /* The following function is not used anymore (replaced with
30346 mouse_face_from_string_pos), but I leave it here for the time
30347 being, in case someone would. */
30349 #if false /* not used */
30351 /* Find the position of the glyph for position POS in OBJECT in
30352 window W's current matrix, and return in *X, *Y the pixel
30353 coordinates, and return in *HPOS, *VPOS the column/row of the glyph.
30355 RIGHT_P means return the position of the right edge of the glyph.
30356 !RIGHT_P means return the left edge position.
30358 If no glyph for POS exists in the matrix, return the position of
30359 the glyph with the next smaller position that is in the matrix, if
30360 RIGHT_P is false. If RIGHT_P, and no glyph for POS
30361 exists in the matrix, return the position of the glyph with the
30362 next larger position in OBJECT.
30364 Value is true if a glyph was found. */
30366 static bool
30367 fast_find_string_pos (struct window *w, ptrdiff_t pos, Lisp_Object object,
30368 int *hpos, int *vpos, int *x, int *y, bool right_p)
30370 int yb = window_text_bottom_y (w);
30371 struct glyph_row *r;
30372 struct glyph *best_glyph = NULL;
30373 struct glyph_row *best_row = NULL;
30374 int best_x = 0;
30376 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
30377 r->enabled_p && r->y < yb;
30378 ++r)
30380 struct glyph *g = r->glyphs[TEXT_AREA];
30381 struct glyph *e = g + r->used[TEXT_AREA];
30382 int gx;
30384 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
30385 if (EQ (g->object, object))
30387 if (g->charpos == pos)
30389 best_glyph = g;
30390 best_x = gx;
30391 best_row = r;
30392 goto found;
30394 else if (best_glyph == NULL
30395 || ((eabs (g->charpos - pos)
30396 < eabs (best_glyph->charpos - pos))
30397 && (right_p
30398 ? g->charpos < pos
30399 : g->charpos > pos)))
30401 best_glyph = g;
30402 best_x = gx;
30403 best_row = r;
30408 found:
30410 if (best_glyph)
30412 *x = best_x;
30413 *hpos = best_glyph - best_row->glyphs[TEXT_AREA];
30415 if (right_p)
30417 *x += best_glyph->pixel_width;
30418 ++*hpos;
30421 *y = best_row->y;
30422 *vpos = MATRIX_ROW_VPOS (best_row, w->current_matrix);
30425 return best_glyph != NULL;
30427 #endif /* not used */
30429 /* Find the positions of the first and the last glyphs in window W's
30430 current matrix that occlude positions [STARTPOS..ENDPOS) in OBJECT
30431 (assumed to be a string), and return in HLINFO's mouse_face_*
30432 members the pixel and column/row coordinates of those glyphs. */
30434 static void
30435 mouse_face_from_string_pos (struct window *w, Mouse_HLInfo *hlinfo,
30436 Lisp_Object object,
30437 ptrdiff_t startpos, ptrdiff_t endpos)
30439 int yb = window_text_bottom_y (w);
30440 struct glyph_row *r;
30441 struct glyph *g, *e;
30442 int gx;
30443 bool found = false;
30445 /* Find the glyph row with at least one position in the range
30446 [STARTPOS..ENDPOS), and the first glyph in that row whose
30447 position belongs to that range. */
30448 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
30449 r->enabled_p && r->y < yb;
30450 ++r)
30452 if (!r->reversed_p)
30454 g = r->glyphs[TEXT_AREA];
30455 e = g + r->used[TEXT_AREA];
30456 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
30457 if (EQ (g->object, object)
30458 && startpos <= g->charpos && g->charpos < endpos)
30460 hlinfo->mouse_face_beg_row
30461 = MATRIX_ROW_VPOS (r, w->current_matrix);
30462 hlinfo->mouse_face_beg_col = g - r->glyphs[TEXT_AREA];
30463 hlinfo->mouse_face_beg_x = gx;
30464 found = true;
30465 break;
30468 else
30470 struct glyph *g1;
30472 e = r->glyphs[TEXT_AREA];
30473 g = e + r->used[TEXT_AREA];
30474 for ( ; g > e; --g)
30475 if (EQ ((g-1)->object, object)
30476 && startpos <= (g-1)->charpos && (g-1)->charpos < endpos)
30478 hlinfo->mouse_face_beg_row
30479 = MATRIX_ROW_VPOS (r, w->current_matrix);
30480 hlinfo->mouse_face_beg_col = g - r->glyphs[TEXT_AREA];
30481 for (gx = r->x, g1 = r->glyphs[TEXT_AREA]; g1 < g; ++g1)
30482 gx += g1->pixel_width;
30483 hlinfo->mouse_face_beg_x = gx;
30484 found = true;
30485 break;
30488 if (found)
30489 break;
30492 if (!found)
30493 return;
30495 /* Starting with the next row, look for the first row which does NOT
30496 include any glyphs whose positions are in the range. */
30497 for (++r; r->enabled_p && r->y < yb; ++r)
30499 g = r->glyphs[TEXT_AREA];
30500 e = g + r->used[TEXT_AREA];
30501 found = false;
30502 for ( ; g < e; ++g)
30503 if (EQ (g->object, object)
30504 && startpos <= g->charpos && g->charpos < endpos)
30506 found = true;
30507 break;
30509 if (!found)
30510 break;
30513 /* The highlighted region ends on the previous row. */
30514 r--;
30516 /* Set the end row. */
30517 hlinfo->mouse_face_end_row = MATRIX_ROW_VPOS (r, w->current_matrix);
30519 /* Compute and set the end column and the end column's horizontal
30520 pixel coordinate. */
30521 if (!r->reversed_p)
30523 g = r->glyphs[TEXT_AREA];
30524 e = g + r->used[TEXT_AREA];
30525 for ( ; e > g; --e)
30526 if (EQ ((e-1)->object, object)
30527 && startpos <= (e-1)->charpos && (e-1)->charpos < endpos)
30528 break;
30529 hlinfo->mouse_face_end_col = e - g;
30531 for (gx = r->x; g < e; ++g)
30532 gx += g->pixel_width;
30533 hlinfo->mouse_face_end_x = gx;
30535 else
30537 e = r->glyphs[TEXT_AREA];
30538 g = e + r->used[TEXT_AREA];
30539 for (gx = r->x ; e < g; ++e)
30541 if (EQ (e->object, object)
30542 && startpos <= e->charpos && e->charpos < endpos)
30543 break;
30544 gx += e->pixel_width;
30546 hlinfo->mouse_face_end_col = e - r->glyphs[TEXT_AREA];
30547 hlinfo->mouse_face_end_x = gx;
30551 #ifdef HAVE_WINDOW_SYSTEM
30553 /* See if position X, Y is within a hot-spot of an image. */
30555 static bool
30556 on_hot_spot_p (Lisp_Object hot_spot, int x, int y)
30558 if (!CONSP (hot_spot))
30559 return false;
30561 if (EQ (XCAR (hot_spot), Qrect))
30563 /* CDR is (Top-Left . Bottom-Right) = ((x0 . y0) . (x1 . y1)) */
30564 Lisp_Object rect = XCDR (hot_spot);
30565 Lisp_Object tem;
30566 if (!CONSP (rect))
30567 return false;
30568 if (!CONSP (XCAR (rect)))
30569 return false;
30570 if (!CONSP (XCDR (rect)))
30571 return false;
30572 if (!(tem = XCAR (XCAR (rect)), INTEGERP (tem) && x >= XINT (tem)))
30573 return false;
30574 if (!(tem = XCDR (XCAR (rect)), INTEGERP (tem) && y >= XINT (tem)))
30575 return false;
30576 if (!(tem = XCAR (XCDR (rect)), INTEGERP (tem) && x <= XINT (tem)))
30577 return false;
30578 if (!(tem = XCDR (XCDR (rect)), INTEGERP (tem) && y <= XINT (tem)))
30579 return false;
30580 return true;
30582 else if (EQ (XCAR (hot_spot), Qcircle))
30584 /* CDR is (Center . Radius) = ((x0 . y0) . r) */
30585 Lisp_Object circ = XCDR (hot_spot);
30586 Lisp_Object lr, lx0, ly0;
30587 if (CONSP (circ)
30588 && CONSP (XCAR (circ))
30589 && (lr = XCDR (circ), NUMBERP (lr))
30590 && (lx0 = XCAR (XCAR (circ)), INTEGERP (lx0))
30591 && (ly0 = XCDR (XCAR (circ)), INTEGERP (ly0)))
30593 double r = XFLOATINT (lr);
30594 double dx = XINT (lx0) - x;
30595 double dy = XINT (ly0) - y;
30596 return (dx * dx + dy * dy <= r * r);
30599 else if (EQ (XCAR (hot_spot), Qpoly))
30601 /* CDR is [x0 y0 x1 y1 x2 y2 ...x(n-1) y(n-1)] */
30602 if (VECTORP (XCDR (hot_spot)))
30604 struct Lisp_Vector *v = XVECTOR (XCDR (hot_spot));
30605 Lisp_Object *poly = v->contents;
30606 ptrdiff_t n = v->header.size;
30607 ptrdiff_t i;
30608 bool inside = false;
30609 Lisp_Object lx, ly;
30610 int x0, y0;
30612 /* Need an even number of coordinates, and at least 3 edges. */
30613 if (n < 6 || n & 1)
30614 return false;
30616 /* Count edge segments intersecting line from (X,Y) to (X,infinity).
30617 If count is odd, we are inside polygon. Pixels on edges
30618 may or may not be included depending on actual geometry of the
30619 polygon. */
30620 if ((lx = poly[n-2], !INTEGERP (lx))
30621 || (ly = poly[n-1], !INTEGERP (lx)))
30622 return false;
30623 x0 = XINT (lx), y0 = XINT (ly);
30624 for (i = 0; i < n; i += 2)
30626 int x1 = x0, y1 = y0;
30627 if ((lx = poly[i], !INTEGERP (lx))
30628 || (ly = poly[i+1], !INTEGERP (ly)))
30629 return false;
30630 x0 = XINT (lx), y0 = XINT (ly);
30632 /* Does this segment cross the X line? */
30633 if (x0 >= x)
30635 if (x1 >= x)
30636 continue;
30638 else if (x1 < x)
30639 continue;
30640 if (y > y0 && y > y1)
30641 continue;
30642 if (y < y0 + ((y1 - y0) * (x - x0)) / (x1 - x0))
30643 inside = !inside;
30645 return inside;
30648 return false;
30651 Lisp_Object
30652 find_hot_spot (Lisp_Object map, int x, int y)
30654 while (CONSP (map))
30656 if (CONSP (XCAR (map))
30657 && on_hot_spot_p (XCAR (XCAR (map)), x, y))
30658 return XCAR (map);
30659 map = XCDR (map);
30662 return Qnil;
30665 DEFUN ("lookup-image-map", Flookup_image_map, Slookup_image_map,
30666 3, 3, 0,
30667 doc: /* Lookup in image map MAP coordinates X and Y.
30668 An image map is an alist where each element has the format (AREA ID PLIST).
30669 An AREA is specified as either a rectangle, a circle, or a polygon:
30670 A rectangle is a cons (rect . ((x0 . y0) . (x1 . y1))) specifying the
30671 pixel coordinates of the upper left and bottom right corners.
30672 A circle is a cons (circle . ((x0 . y0) . r)) specifying the center
30673 and the radius of the circle; r may be a float or integer.
30674 A polygon is a cons (poly . [x0 y0 x1 y1 ...]) where each pair in the
30675 vector describes one corner in the polygon.
30676 Returns the alist element for the first matching AREA in MAP. */)
30677 (Lisp_Object map, Lisp_Object x, Lisp_Object y)
30679 if (NILP (map))
30680 return Qnil;
30682 CHECK_NUMBER (x);
30683 CHECK_NUMBER (y);
30685 return find_hot_spot (map,
30686 clip_to_bounds (INT_MIN, XINT (x), INT_MAX),
30687 clip_to_bounds (INT_MIN, XINT (y), INT_MAX));
30689 #endif /* HAVE_WINDOW_SYSTEM */
30692 /* Display frame CURSOR, optionally using shape defined by POINTER. */
30693 static void
30694 define_frame_cursor1 (struct frame *f, Cursor cursor, Lisp_Object pointer)
30696 #ifdef HAVE_WINDOW_SYSTEM
30697 if (!FRAME_WINDOW_P (f))
30698 return;
30700 /* Do not change cursor shape while dragging mouse. */
30701 if (EQ (do_mouse_tracking, Qdragging))
30702 return;
30704 if (!NILP (pointer))
30706 if (EQ (pointer, Qarrow))
30707 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
30708 else if (EQ (pointer, Qhand))
30709 cursor = FRAME_X_OUTPUT (f)->hand_cursor;
30710 else if (EQ (pointer, Qtext))
30711 cursor = FRAME_X_OUTPUT (f)->text_cursor;
30712 else if (EQ (pointer, intern ("hdrag")))
30713 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
30714 else if (EQ (pointer, intern ("nhdrag")))
30715 cursor = FRAME_X_OUTPUT (f)->vertical_drag_cursor;
30716 # ifdef HAVE_X_WINDOWS
30717 else if (EQ (pointer, intern ("vdrag")))
30718 cursor = FRAME_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
30719 # endif
30720 else if (EQ (pointer, intern ("hourglass")))
30721 cursor = FRAME_X_OUTPUT (f)->hourglass_cursor;
30722 else if (EQ (pointer, Qmodeline))
30723 cursor = FRAME_X_OUTPUT (f)->modeline_cursor;
30724 else
30725 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
30728 if (cursor != No_Cursor)
30729 FRAME_RIF (f)->define_frame_cursor (f, cursor);
30730 #endif
30733 /* Take proper action when mouse has moved to the mode or header line
30734 or marginal area AREA of window W, x-position X and y-position Y.
30735 X is relative to the start of the text display area of W, so the
30736 width of bitmap areas and scroll bars must be subtracted to get a
30737 position relative to the start of the mode line. */
30739 static void
30740 note_mode_line_or_margin_highlight (Lisp_Object window, int x, int y,
30741 enum window_part area)
30743 struct window *w = XWINDOW (window);
30744 struct frame *f = XFRAME (w->frame);
30745 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
30746 #ifdef HAVE_WINDOW_SYSTEM
30747 Display_Info *dpyinfo;
30748 #endif
30749 Cursor cursor = No_Cursor;
30750 Lisp_Object pointer = Qnil;
30751 int dx, dy, width, height;
30752 ptrdiff_t charpos;
30753 Lisp_Object string, object = Qnil;
30754 Lisp_Object pos UNINIT;
30755 Lisp_Object mouse_face;
30756 int original_x_pixel = x;
30757 struct glyph * glyph = NULL, * row_start_glyph = NULL;
30758 struct glyph_row *row UNINIT;
30760 if (area == ON_MODE_LINE || area == ON_HEADER_LINE)
30762 int x0;
30763 struct glyph *end;
30765 /* Kludge alert: mode_line_string takes X/Y in pixels, but
30766 returns them in row/column units! */
30767 string = mode_line_string (w, area, &x, &y, &charpos,
30768 &object, &dx, &dy, &width, &height);
30770 row = (area == ON_MODE_LINE
30771 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
30772 : MATRIX_HEADER_LINE_ROW (w->current_matrix));
30774 /* Find the glyph under the mouse pointer. */
30775 if (row->mode_line_p && row->enabled_p)
30777 glyph = row_start_glyph = row->glyphs[TEXT_AREA];
30778 end = glyph + row->used[TEXT_AREA];
30780 for (x0 = original_x_pixel;
30781 glyph < end && x0 >= glyph->pixel_width;
30782 ++glyph)
30783 x0 -= glyph->pixel_width;
30785 if (glyph >= end)
30786 glyph = NULL;
30789 else
30791 x -= WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
30792 /* Kludge alert: marginal_area_string takes X/Y in pixels, but
30793 returns them in row/column units! */
30794 string = marginal_area_string (w, area, &x, &y, &charpos,
30795 &object, &dx, &dy, &width, &height);
30798 Lisp_Object help = Qnil;
30800 #ifdef HAVE_WINDOW_SYSTEM
30801 if (IMAGEP (object))
30803 Lisp_Object image_map, hotspot;
30804 if ((image_map = Fplist_get (XCDR (object), QCmap),
30805 !NILP (image_map))
30806 && (hotspot = find_hot_spot (image_map, dx, dy),
30807 CONSP (hotspot))
30808 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
30810 Lisp_Object plist;
30812 /* Could check XCAR (hotspot) to see if we enter/leave this hot-spot.
30813 If so, we could look for mouse-enter, mouse-leave
30814 properties in PLIST (and do something...). */
30815 hotspot = XCDR (hotspot);
30816 if (CONSP (hotspot)
30817 && (plist = XCAR (hotspot), CONSP (plist)))
30819 pointer = Fplist_get (plist, Qpointer);
30820 if (NILP (pointer))
30821 pointer = Qhand;
30822 help = Fplist_get (plist, Qhelp_echo);
30823 if (!NILP (help))
30825 help_echo_string = help;
30826 XSETWINDOW (help_echo_window, w);
30827 help_echo_object = w->contents;
30828 help_echo_pos = charpos;
30832 if (NILP (pointer))
30833 pointer = Fplist_get (XCDR (object), QCpointer);
30835 #endif /* HAVE_WINDOW_SYSTEM */
30837 if (STRINGP (string))
30838 pos = make_number (charpos);
30840 /* Set the help text and mouse pointer. If the mouse is on a part
30841 of the mode line without any text (e.g. past the right edge of
30842 the mode line text), use the default help text and pointer. */
30843 if (STRINGP (string) || area == ON_MODE_LINE)
30845 /* Arrange to display the help by setting the global variables
30846 help_echo_string, help_echo_object, and help_echo_pos. */
30847 if (NILP (help))
30849 if (STRINGP (string))
30850 help = Fget_text_property (pos, Qhelp_echo, string);
30852 if (!NILP (help))
30854 help_echo_string = help;
30855 XSETWINDOW (help_echo_window, w);
30856 help_echo_object = string;
30857 help_echo_pos = charpos;
30859 else if (area == ON_MODE_LINE)
30861 Lisp_Object default_help
30862 = buffer_local_value (Qmode_line_default_help_echo,
30863 w->contents);
30865 if (STRINGP (default_help))
30867 help_echo_string = default_help;
30868 XSETWINDOW (help_echo_window, w);
30869 help_echo_object = Qnil;
30870 help_echo_pos = -1;
30875 #ifdef HAVE_WINDOW_SYSTEM
30876 /* Change the mouse pointer according to what is under it. */
30877 if (FRAME_WINDOW_P (f))
30879 bool draggable = (! WINDOW_BOTTOMMOST_P (w)
30880 || minibuf_level
30881 || NILP (Vresize_mini_windows));
30883 dpyinfo = FRAME_DISPLAY_INFO (f);
30884 if (STRINGP (string))
30886 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
30888 if (NILP (pointer))
30889 pointer = Fget_text_property (pos, Qpointer, string);
30891 /* Change the mouse pointer according to what is under X/Y. */
30892 if (NILP (pointer)
30893 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE)))
30895 Lisp_Object map;
30896 map = Fget_text_property (pos, Qlocal_map, string);
30897 if (!KEYMAPP (map))
30898 map = Fget_text_property (pos, Qkeymap, string);
30899 if (!KEYMAPP (map) && draggable)
30900 cursor = dpyinfo->vertical_scroll_bar_cursor;
30903 else if (draggable)
30904 /* Default mode-line pointer. */
30905 cursor = FRAME_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
30907 #endif
30910 /* Change the mouse face according to what is under X/Y. */
30911 bool mouse_face_shown = false;
30912 if (STRINGP (string))
30914 mouse_face = Fget_text_property (pos, Qmouse_face, string);
30915 if (!NILP (Vmouse_highlight) && !NILP (mouse_face)
30916 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
30917 && glyph)
30919 Lisp_Object b, e;
30921 struct glyph * tmp_glyph;
30923 int gpos;
30924 int gseq_length;
30925 int total_pixel_width;
30926 ptrdiff_t begpos, endpos, ignore;
30928 int vpos, hpos;
30930 b = Fprevious_single_property_change (make_number (charpos + 1),
30931 Qmouse_face, string, Qnil);
30932 if (NILP (b))
30933 begpos = 0;
30934 else
30935 begpos = XINT (b);
30937 e = Fnext_single_property_change (pos, Qmouse_face, string, Qnil);
30938 if (NILP (e))
30939 endpos = SCHARS (string);
30940 else
30941 endpos = XINT (e);
30943 /* Calculate the glyph position GPOS of GLYPH in the
30944 displayed string, relative to the beginning of the
30945 highlighted part of the string.
30947 Note: GPOS is different from CHARPOS. CHARPOS is the
30948 position of GLYPH in the internal string object. A mode
30949 line string format has structures which are converted to
30950 a flattened string by the Emacs Lisp interpreter. The
30951 internal string is an element of those structures. The
30952 displayed string is the flattened string. */
30953 tmp_glyph = row_start_glyph;
30954 while (tmp_glyph < glyph
30955 && (!(EQ (tmp_glyph->object, glyph->object)
30956 && begpos <= tmp_glyph->charpos
30957 && tmp_glyph->charpos < endpos)))
30958 tmp_glyph++;
30959 gpos = glyph - tmp_glyph;
30961 /* Calculate the length GSEQ_LENGTH of the glyph sequence of
30962 the highlighted part of the displayed string to which
30963 GLYPH belongs. Note: GSEQ_LENGTH is different from
30964 SCHARS (STRING), because the latter returns the length of
30965 the internal string. */
30966 for (tmp_glyph = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
30967 tmp_glyph > glyph
30968 && (!(EQ (tmp_glyph->object, glyph->object)
30969 && begpos <= tmp_glyph->charpos
30970 && tmp_glyph->charpos < endpos));
30971 tmp_glyph--)
30973 gseq_length = gpos + (tmp_glyph - glyph) + 1;
30975 /* Calculate the total pixel width of all the glyphs between
30976 the beginning of the highlighted area and GLYPH. */
30977 total_pixel_width = 0;
30978 for (tmp_glyph = glyph - gpos; tmp_glyph != glyph; tmp_glyph++)
30979 total_pixel_width += tmp_glyph->pixel_width;
30981 /* Pre calculation of re-rendering position. Note: X is in
30982 column units here, after the call to mode_line_string or
30983 marginal_area_string. */
30984 hpos = x - gpos;
30985 vpos = (area == ON_MODE_LINE
30986 ? (w->current_matrix)->nrows - 1
30987 : 0);
30989 /* If GLYPH's position is included in the region that is
30990 already drawn in mouse face, we have nothing to do. */
30991 if ( EQ (window, hlinfo->mouse_face_window)
30992 && (!row->reversed_p
30993 ? (hlinfo->mouse_face_beg_col <= hpos
30994 && hpos < hlinfo->mouse_face_end_col)
30995 /* In R2L rows we swap BEG and END, see below. */
30996 : (hlinfo->mouse_face_end_col <= hpos
30997 && hpos < hlinfo->mouse_face_beg_col))
30998 && hlinfo->mouse_face_beg_row == vpos )
30999 return;
31001 if (clear_mouse_face (hlinfo))
31002 cursor = No_Cursor;
31004 if (!row->reversed_p)
31006 hlinfo->mouse_face_beg_col = hpos;
31007 hlinfo->mouse_face_beg_x = original_x_pixel
31008 - (total_pixel_width + dx);
31009 hlinfo->mouse_face_end_col = hpos + gseq_length;
31010 hlinfo->mouse_face_end_x = 0;
31012 else
31014 /* In R2L rows, show_mouse_face expects BEG and END
31015 coordinates to be swapped. */
31016 hlinfo->mouse_face_end_col = hpos;
31017 hlinfo->mouse_face_end_x = original_x_pixel
31018 - (total_pixel_width + dx);
31019 hlinfo->mouse_face_beg_col = hpos + gseq_length;
31020 hlinfo->mouse_face_beg_x = 0;
31023 hlinfo->mouse_face_beg_row = vpos;
31024 hlinfo->mouse_face_end_row = hlinfo->mouse_face_beg_row;
31025 hlinfo->mouse_face_past_end = false;
31026 hlinfo->mouse_face_window = window;
31028 hlinfo->mouse_face_face_id = face_at_string_position (w, string,
31029 charpos,
31030 0, &ignore,
31031 glyph->face_id,
31032 true);
31033 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
31034 mouse_face_shown = true;
31036 if (NILP (pointer))
31037 pointer = Qhand;
31041 /* If mouse-face doesn't need to be shown, clear any existing
31042 mouse-face. */
31043 if ((area == ON_MODE_LINE || area == ON_HEADER_LINE) && !mouse_face_shown)
31044 clear_mouse_face (hlinfo);
31046 define_frame_cursor1 (f, cursor, pointer);
31050 /* EXPORT:
31051 Take proper action when the mouse has moved to position X, Y on
31052 frame F with regards to highlighting portions of display that have
31053 mouse-face properties. Also de-highlight portions of display where
31054 the mouse was before, set the mouse pointer shape as appropriate
31055 for the mouse coordinates, and activate help echo (tooltips).
31056 X and Y can be negative or out of range. */
31058 void
31059 note_mouse_highlight (struct frame *f, int x, int y)
31061 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
31062 enum window_part part = ON_NOTHING;
31063 Lisp_Object window;
31064 struct window *w;
31065 Cursor cursor = No_Cursor;
31066 Lisp_Object pointer = Qnil; /* Takes precedence over cursor! */
31067 struct buffer *b;
31069 /* When a menu is active, don't highlight because this looks odd. */
31070 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS) || defined (MSDOS)
31071 if (popup_activated ())
31072 return;
31073 #endif
31075 if (!f->glyphs_initialized_p
31076 || f->pointer_invisible)
31077 return;
31079 hlinfo->mouse_face_mouse_x = x;
31080 hlinfo->mouse_face_mouse_y = y;
31081 hlinfo->mouse_face_mouse_frame = f;
31083 if (hlinfo->mouse_face_defer)
31084 return;
31086 /* Which window is that in? */
31087 window = window_from_coordinates (f, x, y, &part, true);
31089 /* If displaying active text in another window, clear that. */
31090 if (! EQ (window, hlinfo->mouse_face_window)
31091 /* Also clear if we move out of text area in same window. */
31092 || (!NILP (hlinfo->mouse_face_window)
31093 && !NILP (window)
31094 && part != ON_TEXT
31095 && part != ON_MODE_LINE
31096 && part != ON_HEADER_LINE))
31097 clear_mouse_face (hlinfo);
31099 /* Reset help_echo_string. It will get recomputed below. */
31100 help_echo_string = Qnil;
31102 #ifdef HAVE_WINDOW_SYSTEM
31103 /* If the cursor is on the internal border of FRAME and FRAME's
31104 internal border is draggable, provide some visual feedback. */
31105 if (FRAME_INTERNAL_BORDER_WIDTH (f) > 0
31106 && !NILP (get_frame_param (f, Qdrag_internal_border)))
31108 enum internal_border_part part = frame_internal_border_part (f, x, y);
31110 switch (part)
31112 case INTERNAL_BORDER_NONE:
31113 if (cursor != FRAME_X_OUTPUT (f)->nontext_cursor)
31114 /* Reset cursor. */
31115 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
31116 break;
31117 case INTERNAL_BORDER_LEFT_EDGE:
31118 cursor = FRAME_X_OUTPUT (f)->left_edge_cursor;
31119 break;
31120 case INTERNAL_BORDER_TOP_LEFT_CORNER:
31121 cursor = FRAME_X_OUTPUT (f)->top_left_corner_cursor;
31122 break;
31123 case INTERNAL_BORDER_TOP_EDGE:
31124 cursor = FRAME_X_OUTPUT (f)->top_edge_cursor;
31125 break;
31126 case INTERNAL_BORDER_TOP_RIGHT_CORNER:
31127 cursor = FRAME_X_OUTPUT (f)->top_right_corner_cursor;
31128 break;
31129 case INTERNAL_BORDER_RIGHT_EDGE:
31130 cursor = FRAME_X_OUTPUT (f)->right_edge_cursor;
31131 break;
31132 case INTERNAL_BORDER_BOTTOM_RIGHT_CORNER:
31133 cursor = FRAME_X_OUTPUT (f)->bottom_right_corner_cursor;
31134 break;
31135 case INTERNAL_BORDER_BOTTOM_EDGE:
31136 cursor = FRAME_X_OUTPUT (f)->bottom_edge_cursor;
31137 break;
31138 case INTERNAL_BORDER_BOTTOM_LEFT_CORNER:
31139 cursor = FRAME_X_OUTPUT (f)->bottom_left_corner_cursor;
31140 break;
31141 default:
31142 /* This should not happen. */
31143 if (cursor != FRAME_X_OUTPUT (f)->nontext_cursor)
31144 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
31147 if (cursor != FRAME_X_OUTPUT (f)->nontext_cursor)
31149 /* Do we really want a help echo here? */
31150 help_echo_string = build_string ("drag-mouse-1: resize frame");
31151 goto set_cursor;
31154 #endif /* HAVE_WINDOW_SYSTEM */
31156 /* Not on a window -> return. */
31157 if (!WINDOWP (window))
31158 return;
31160 /* Convert to window-relative pixel coordinates. */
31161 w = XWINDOW (window);
31162 frame_to_window_pixel_xy (w, &x, &y);
31164 #if defined (HAVE_WINDOW_SYSTEM) && ! defined (USE_GTK) && ! defined (HAVE_NS)
31165 /* Handle tool-bar window differently since it doesn't display a
31166 buffer. */
31167 if (EQ (window, f->tool_bar_window))
31169 note_tool_bar_highlight (f, x, y);
31170 return;
31172 #endif
31174 /* Mouse is on the mode, header line or margin? */
31175 if (part == ON_MODE_LINE || part == ON_HEADER_LINE
31176 || part == ON_LEFT_MARGIN || part == ON_RIGHT_MARGIN)
31178 note_mode_line_or_margin_highlight (window, x, y, part);
31180 #ifdef HAVE_WINDOW_SYSTEM
31181 if (part == ON_LEFT_MARGIN || part == ON_RIGHT_MARGIN)
31183 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
31184 /* Show non-text cursor (Bug#16647). */
31185 goto set_cursor;
31187 else
31188 #endif
31189 return;
31192 #ifdef HAVE_WINDOW_SYSTEM
31193 if (part == ON_VERTICAL_BORDER)
31195 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
31196 help_echo_string = build_string ("drag-mouse-1: resize");
31197 goto set_cursor;
31199 else if (part == ON_RIGHT_DIVIDER)
31201 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
31202 help_echo_string = build_string ("drag-mouse-1: resize");
31203 goto set_cursor;
31205 else if (part == ON_BOTTOM_DIVIDER)
31206 if (! WINDOW_BOTTOMMOST_P (w)
31207 || minibuf_level
31208 || NILP (Vresize_mini_windows))
31210 cursor = FRAME_X_OUTPUT (f)->vertical_drag_cursor;
31211 help_echo_string = build_string ("drag-mouse-1: resize");
31212 goto set_cursor;
31214 else
31215 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
31216 else if (part == ON_LEFT_FRINGE || part == ON_RIGHT_FRINGE
31217 || part == ON_VERTICAL_SCROLL_BAR
31218 || part == ON_HORIZONTAL_SCROLL_BAR)
31219 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
31220 else
31221 cursor = FRAME_X_OUTPUT (f)->text_cursor;
31222 #endif
31224 /* Are we in a window whose display is up to date?
31225 And verify the buffer's text has not changed. */
31226 b = XBUFFER (w->contents);
31227 if (part == ON_TEXT && w->window_end_valid && !window_outdated (w))
31229 int hpos, vpos, dx, dy, area = LAST_AREA;
31230 ptrdiff_t pos;
31231 struct glyph *glyph;
31232 Lisp_Object object;
31233 Lisp_Object mouse_face = Qnil, position;
31234 Lisp_Object *overlay_vec = NULL;
31235 ptrdiff_t i, noverlays;
31236 struct buffer *obuf;
31237 ptrdiff_t obegv, ozv;
31238 bool same_region;
31240 /* Find the glyph under X/Y. */
31241 glyph = x_y_to_hpos_vpos (w, x, y, &hpos, &vpos, &dx, &dy, &area);
31243 #ifdef HAVE_WINDOW_SYSTEM
31244 /* Look for :pointer property on image. */
31245 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
31247 struct image *img = IMAGE_OPT_FROM_ID (f, glyph->u.img_id);
31248 if (img != NULL && IMAGEP (img->spec))
31250 Lisp_Object image_map, hotspot;
31251 if ((image_map = Fplist_get (XCDR (img->spec), QCmap),
31252 !NILP (image_map))
31253 && (hotspot = find_hot_spot (image_map,
31254 glyph->slice.img.x + dx,
31255 glyph->slice.img.y + dy),
31256 CONSP (hotspot))
31257 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
31259 Lisp_Object plist;
31261 /* Could check XCAR (hotspot) to see if we enter/leave
31262 this hot-spot.
31263 If so, we could look for mouse-enter, mouse-leave
31264 properties in PLIST (and do something...). */
31265 hotspot = XCDR (hotspot);
31266 if (CONSP (hotspot)
31267 && (plist = XCAR (hotspot), CONSP (plist)))
31269 pointer = Fplist_get (plist, Qpointer);
31270 if (NILP (pointer))
31271 pointer = Qhand;
31272 help_echo_string = Fplist_get (plist, Qhelp_echo);
31273 if (!NILP (help_echo_string))
31275 help_echo_window = window;
31276 help_echo_object = glyph->object;
31277 help_echo_pos = glyph->charpos;
31281 if (NILP (pointer))
31282 pointer = Fplist_get (XCDR (img->spec), QCpointer);
31285 #endif /* HAVE_WINDOW_SYSTEM */
31287 /* Clear mouse face if X/Y not over text. */
31288 if (glyph == NULL
31289 || area != TEXT_AREA
31290 || !MATRIX_ROW_DISPLAYS_TEXT_P (MATRIX_ROW (w->current_matrix, vpos))
31291 /* Glyph's OBJECT is nil for glyphs inserted by the
31292 display engine for its internal purposes, like truncation
31293 and continuation glyphs and blanks beyond the end of
31294 line's text on text terminals. If we are over such a
31295 glyph, we are not over any text. */
31296 || NILP (glyph->object)
31297 /* R2L rows have a stretch glyph at their front, which
31298 stands for no text, whereas L2R rows have no glyphs at
31299 all beyond the end of text. Treat such stretch glyphs
31300 like we do with NULL glyphs in L2R rows. */
31301 || (MATRIX_ROW (w->current_matrix, vpos)->reversed_p
31302 && glyph == MATRIX_ROW_GLYPH_START (w->current_matrix, vpos)
31303 && glyph->type == STRETCH_GLYPH
31304 && glyph->avoid_cursor_p))
31306 if (clear_mouse_face (hlinfo))
31307 cursor = No_Cursor;
31308 if (FRAME_WINDOW_P (f) && NILP (pointer))
31310 #ifdef HAVE_WINDOW_SYSTEM
31311 if (area != TEXT_AREA)
31312 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
31313 else
31314 pointer = Vvoid_text_area_pointer;
31315 #endif
31317 goto set_cursor;
31320 pos = glyph->charpos;
31321 object = glyph->object;
31322 if (!STRINGP (object) && !BUFFERP (object))
31323 goto set_cursor;
31325 /* If we get an out-of-range value, return now; avoid an error. */
31326 if (BUFFERP (object) && pos > BUF_Z (b))
31327 goto set_cursor;
31329 /* Make the window's buffer temporarily current for
31330 overlays_at and compute_char_face. */
31331 obuf = current_buffer;
31332 current_buffer = b;
31333 obegv = BEGV;
31334 ozv = ZV;
31335 BEGV = BEG;
31336 ZV = Z;
31338 /* Is this char mouse-active or does it have help-echo? */
31339 position = make_number (pos);
31341 USE_SAFE_ALLOCA;
31343 if (BUFFERP (object))
31345 /* Put all the overlays we want in a vector in overlay_vec. */
31346 GET_OVERLAYS_AT (pos, overlay_vec, noverlays, NULL, false);
31347 /* Sort overlays into increasing priority order. */
31348 noverlays = sort_overlays (overlay_vec, noverlays, w);
31350 else
31351 noverlays = 0;
31353 if (NILP (Vmouse_highlight))
31355 clear_mouse_face (hlinfo);
31356 goto check_help_echo;
31359 same_region = coords_in_mouse_face_p (w, hpos, vpos);
31361 if (same_region)
31362 cursor = No_Cursor;
31364 /* Check mouse-face highlighting. */
31365 if (! same_region
31366 /* If there exists an overlay with mouse-face overlapping
31367 the one we are currently highlighting, we have to check
31368 if we enter the overlapping overlay, and then highlight
31369 only that. Skip the check when mouse-face highlighting
31370 is currently hidden to avoid Bug#30519. */
31371 || (!hlinfo->mouse_face_hidden
31372 && OVERLAYP (hlinfo->mouse_face_overlay)
31373 && mouse_face_overlay_overlaps (hlinfo->mouse_face_overlay)))
31375 /* Find the highest priority overlay with a mouse-face. */
31376 Lisp_Object overlay = Qnil;
31377 for (i = noverlays - 1; i >= 0 && NILP (overlay); --i)
31379 mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
31380 if (!NILP (mouse_face))
31381 overlay = overlay_vec[i];
31384 /* If we're highlighting the same overlay as before, there's
31385 no need to do that again. */
31386 if (!NILP (overlay) && EQ (overlay, hlinfo->mouse_face_overlay))
31387 goto check_help_echo;
31389 /* Clear the display of the old active region, if any. */
31390 if (clear_mouse_face (hlinfo))
31391 cursor = No_Cursor;
31393 /* Record the overlay, if any, to be highlighted. */
31394 hlinfo->mouse_face_overlay = overlay;
31396 /* If no overlay applies, get a text property. */
31397 if (NILP (overlay))
31398 mouse_face = Fget_text_property (position, Qmouse_face, object);
31400 /* Next, compute the bounds of the mouse highlighting and
31401 display it. */
31402 if (!NILP (mouse_face) && STRINGP (object))
31404 /* The mouse-highlighting comes from a display string
31405 with a mouse-face. */
31406 Lisp_Object s, e;
31407 ptrdiff_t ignore;
31409 s = Fprevious_single_property_change
31410 (make_number (pos + 1), Qmouse_face, object, Qnil);
31411 e = Fnext_single_property_change
31412 (position, Qmouse_face, object, Qnil);
31413 if (NILP (s))
31414 s = make_number (0);
31415 if (NILP (e))
31416 e = make_number (SCHARS (object));
31417 mouse_face_from_string_pos (w, hlinfo, object,
31418 XINT (s), XINT (e));
31419 hlinfo->mouse_face_past_end = false;
31420 hlinfo->mouse_face_window = window;
31421 hlinfo->mouse_face_face_id
31422 = face_at_string_position (w, object, pos, 0, &ignore,
31423 glyph->face_id, true);
31424 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
31425 cursor = No_Cursor;
31427 else
31429 /* The mouse-highlighting, if any, comes from an overlay
31430 or text property in the buffer. */
31431 Lisp_Object buffer UNINIT;
31432 Lisp_Object disp_string UNINIT;
31434 if (STRINGP (object))
31436 /* If we are on a display string with no mouse-face,
31437 check if the text under it has one. */
31438 struct glyph_row *r = MATRIX_ROW (w->current_matrix, vpos);
31439 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
31440 pos = string_buffer_position (object, start);
31441 if (pos > 0)
31443 mouse_face = get_char_property_and_overlay
31444 (make_number (pos), Qmouse_face, w->contents, &overlay);
31445 buffer = w->contents;
31446 disp_string = object;
31449 else
31451 buffer = object;
31452 disp_string = Qnil;
31455 if (!NILP (mouse_face))
31457 Lisp_Object before, after;
31458 Lisp_Object before_string, after_string;
31459 /* To correctly find the limits of mouse highlight
31460 in a bidi-reordered buffer, we must not use the
31461 optimization of limiting the search in
31462 previous-single-property-change and
31463 next-single-property-change, because
31464 rows_from_pos_range needs the real start and end
31465 positions to DTRT in this case. That's because
31466 the first row visible in a window does not
31467 necessarily display the character whose position
31468 is the smallest. */
31469 Lisp_Object lim1
31470 = NILP (BVAR (XBUFFER (buffer), bidi_display_reordering))
31471 ? Fmarker_position (w->start)
31472 : Qnil;
31473 Lisp_Object lim2
31474 = NILP (BVAR (XBUFFER (buffer), bidi_display_reordering))
31475 ? make_number (BUF_Z (XBUFFER (buffer))
31476 - w->window_end_pos)
31477 : Qnil;
31479 if (NILP (overlay))
31481 /* Handle the text property case. */
31482 before = Fprevious_single_property_change
31483 (make_number (pos + 1), Qmouse_face, buffer, lim1);
31484 after = Fnext_single_property_change
31485 (make_number (pos), Qmouse_face, buffer, lim2);
31486 before_string = after_string = Qnil;
31488 else
31490 /* Handle the overlay case. */
31491 before = Foverlay_start (overlay);
31492 after = Foverlay_end (overlay);
31493 before_string = Foverlay_get (overlay, Qbefore_string);
31494 after_string = Foverlay_get (overlay, Qafter_string);
31496 if (!STRINGP (before_string)) before_string = Qnil;
31497 if (!STRINGP (after_string)) after_string = Qnil;
31500 mouse_face_from_buffer_pos (window, hlinfo, pos,
31501 NILP (before)
31503 : XFASTINT (before),
31504 NILP (after)
31505 ? BUF_Z (XBUFFER (buffer))
31506 : XFASTINT (after),
31507 before_string, after_string,
31508 disp_string);
31509 cursor = No_Cursor;
31514 check_help_echo:
31516 /* Look for a `help-echo' property. */
31517 if (NILP (help_echo_string)) {
31518 Lisp_Object help, overlay;
31520 /* Check overlays first. */
31521 help = overlay = Qnil;
31522 for (i = noverlays - 1; i >= 0 && NILP (help); --i)
31524 overlay = overlay_vec[i];
31525 help = Foverlay_get (overlay, Qhelp_echo);
31528 if (!NILP (help))
31530 help_echo_string = help;
31531 help_echo_window = window;
31532 help_echo_object = overlay;
31533 help_echo_pos = pos;
31535 else
31537 Lisp_Object obj = glyph->object;
31538 ptrdiff_t charpos = glyph->charpos;
31540 /* Try text properties. */
31541 if (STRINGP (obj)
31542 && charpos >= 0
31543 && charpos < SCHARS (obj))
31545 help = Fget_text_property (make_number (charpos),
31546 Qhelp_echo, obj);
31547 if (NILP (help))
31549 /* If the string itself doesn't specify a help-echo,
31550 see if the buffer text ``under'' it does. */
31551 struct glyph_row *r
31552 = MATRIX_ROW (w->current_matrix, vpos);
31553 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
31554 ptrdiff_t p = string_buffer_position (obj, start);
31555 if (p > 0)
31557 help = Fget_char_property (make_number (p),
31558 Qhelp_echo, w->contents);
31559 if (!NILP (help))
31561 charpos = p;
31562 obj = w->contents;
31567 else if (BUFFERP (obj)
31568 && charpos >= BEGV
31569 && charpos < ZV)
31570 help = Fget_text_property (make_number (charpos), Qhelp_echo,
31571 obj);
31573 if (!NILP (help))
31575 help_echo_string = help;
31576 help_echo_window = window;
31577 help_echo_object = obj;
31578 help_echo_pos = charpos;
31583 #ifdef HAVE_WINDOW_SYSTEM
31584 /* Look for a `pointer' property. */
31585 if (FRAME_WINDOW_P (f) && NILP (pointer))
31587 /* Check overlays first. */
31588 for (i = noverlays - 1; i >= 0 && NILP (pointer); --i)
31589 pointer = Foverlay_get (overlay_vec[i], Qpointer);
31591 if (NILP (pointer))
31593 Lisp_Object obj = glyph->object;
31594 ptrdiff_t charpos = glyph->charpos;
31596 /* Try text properties. */
31597 if (STRINGP (obj)
31598 && charpos >= 0
31599 && charpos < SCHARS (obj))
31601 pointer = Fget_text_property (make_number (charpos),
31602 Qpointer, obj);
31603 if (NILP (pointer))
31605 /* If the string itself doesn't specify a pointer,
31606 see if the buffer text ``under'' it does. */
31607 struct glyph_row *r
31608 = MATRIX_ROW (w->current_matrix, vpos);
31609 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
31610 ptrdiff_t p = string_buffer_position (obj, start);
31611 if (p > 0)
31612 pointer = Fget_char_property (make_number (p),
31613 Qpointer, w->contents);
31616 else if (BUFFERP (obj)
31617 && charpos >= BEGV
31618 && charpos < ZV)
31619 pointer = Fget_text_property (make_number (charpos),
31620 Qpointer, obj);
31623 #endif /* HAVE_WINDOW_SYSTEM */
31625 BEGV = obegv;
31626 ZV = ozv;
31627 current_buffer = obuf;
31628 SAFE_FREE ();
31631 set_cursor:
31632 define_frame_cursor1 (f, cursor, pointer);
31636 /* EXPORT for RIF:
31637 Clear any mouse-face on window W. This function is part of the
31638 redisplay interface, and is called from try_window_id and similar
31639 functions to ensure the mouse-highlight is off. */
31641 void
31642 x_clear_window_mouse_face (struct window *w)
31644 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (XFRAME (w->frame));
31645 Lisp_Object window;
31647 block_input ();
31648 XSETWINDOW (window, w);
31649 if (EQ (window, hlinfo->mouse_face_window))
31650 clear_mouse_face (hlinfo);
31651 unblock_input ();
31655 /* EXPORT:
31656 Just discard the mouse face information for frame F, if any.
31657 This is used when the size of F is changed. */
31659 void
31660 cancel_mouse_face (struct frame *f)
31662 Lisp_Object window;
31663 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
31665 window = hlinfo->mouse_face_window;
31666 if (! NILP (window) && XFRAME (XWINDOW (window)->frame) == f)
31667 reset_mouse_highlight (hlinfo);
31672 /***********************************************************************
31673 Exposure Events
31674 ***********************************************************************/
31676 #ifdef HAVE_WINDOW_SYSTEM
31678 /* Redraw the part of glyph row area AREA of glyph row ROW on window W
31679 which intersects rectangle R. R is in window-relative coordinates. */
31681 static void
31682 expose_area (struct window *w, struct glyph_row *row, XRectangle *r,
31683 enum glyph_row_area area)
31685 struct glyph *first = row->glyphs[area];
31686 struct glyph *end = row->glyphs[area] + row->used[area];
31687 struct glyph *last;
31688 int first_x, start_x, x;
31690 if (area == TEXT_AREA && row->fill_line_p)
31691 /* If row extends face to end of line write the whole line. */
31692 draw_glyphs (w, 0, row, area,
31693 0, row->used[area],
31694 DRAW_NORMAL_TEXT, 0);
31695 else
31697 /* Set START_X to the window-relative start position for drawing glyphs of
31698 AREA. The first glyph of the text area can be partially visible.
31699 The first glyphs of other areas cannot. */
31700 start_x = window_box_left_offset (w, area);
31701 x = start_x;
31702 if (area == TEXT_AREA)
31703 x += row->x;
31705 /* Find the first glyph that must be redrawn. */
31706 while (first < end
31707 && x + first->pixel_width < r->x)
31709 x += first->pixel_width;
31710 ++first;
31713 /* Find the last one. */
31714 last = first;
31715 first_x = x;
31716 /* Use a signed int intermediate value to avoid catastrophic
31717 failures due to comparison between signed and unsigned, when
31718 x is negative (can happen for wide images that are hscrolled). */
31719 int r_end = r->x + r->width;
31720 while (last < end && x < r_end)
31722 x += last->pixel_width;
31723 ++last;
31726 /* Repaint. */
31727 if (last > first)
31728 draw_glyphs (w, first_x - start_x, row, area,
31729 first - row->glyphs[area], last - row->glyphs[area],
31730 DRAW_NORMAL_TEXT, 0);
31735 /* Redraw the parts of the glyph row ROW on window W intersecting
31736 rectangle R. R is in window-relative coordinates. Value is
31737 true if mouse-face was overwritten. */
31739 static bool
31740 expose_line (struct window *w, struct glyph_row *row, XRectangle *r)
31742 eassert (row->enabled_p);
31744 if (row->mode_line_p || w->pseudo_window_p)
31745 draw_glyphs (w, 0, row, TEXT_AREA,
31746 0, row->used[TEXT_AREA],
31747 DRAW_NORMAL_TEXT, 0);
31748 else
31750 if (row->used[LEFT_MARGIN_AREA])
31751 expose_area (w, row, r, LEFT_MARGIN_AREA);
31752 if (row->used[TEXT_AREA])
31753 expose_area (w, row, r, TEXT_AREA);
31754 if (row->used[RIGHT_MARGIN_AREA])
31755 expose_area (w, row, r, RIGHT_MARGIN_AREA);
31756 draw_row_fringe_bitmaps (w, row);
31759 return row->mouse_face_p;
31763 /* Redraw those parts of glyphs rows during expose event handling that
31764 overlap other rows. Redrawing of an exposed line writes over parts
31765 of lines overlapping that exposed line; this function fixes that.
31767 W is the window being exposed. FIRST_OVERLAPPING_ROW is the first
31768 row in W's current matrix that is exposed and overlaps other rows.
31769 LAST_OVERLAPPING_ROW is the last such row. */
31771 static void
31772 expose_overlaps (struct window *w,
31773 struct glyph_row *first_overlapping_row,
31774 struct glyph_row *last_overlapping_row,
31775 XRectangle *r)
31777 struct glyph_row *row;
31779 for (row = first_overlapping_row; row <= last_overlapping_row; ++row)
31780 if (row->overlapping_p)
31782 eassert (row->enabled_p && !row->mode_line_p);
31784 row->clip = r;
31785 if (row->used[LEFT_MARGIN_AREA])
31786 x_fix_overlapping_area (w, row, LEFT_MARGIN_AREA, OVERLAPS_BOTH);
31788 if (row->used[TEXT_AREA])
31789 x_fix_overlapping_area (w, row, TEXT_AREA, OVERLAPS_BOTH);
31791 if (row->used[RIGHT_MARGIN_AREA])
31792 x_fix_overlapping_area (w, row, RIGHT_MARGIN_AREA, OVERLAPS_BOTH);
31793 row->clip = NULL;
31798 /* Return true if W's cursor intersects rectangle R. */
31800 static bool
31801 phys_cursor_in_rect_p (struct window *w, XRectangle *r)
31803 XRectangle cr, result;
31804 struct glyph *cursor_glyph;
31805 struct glyph_row *row;
31807 if (w->phys_cursor.vpos >= 0
31808 && w->phys_cursor.vpos < w->current_matrix->nrows
31809 && (row = MATRIX_ROW (w->current_matrix, w->phys_cursor.vpos),
31810 row->enabled_p)
31811 && row->cursor_in_fringe_p)
31813 /* Cursor is in the fringe. */
31814 cr.x = window_box_right_offset (w,
31815 (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
31816 ? RIGHT_MARGIN_AREA
31817 : TEXT_AREA));
31818 cr.y = row->y;
31819 cr.width = WINDOW_RIGHT_FRINGE_WIDTH (w);
31820 cr.height = row->height;
31821 return x_intersect_rectangles (&cr, r, &result);
31824 cursor_glyph = get_phys_cursor_glyph (w);
31825 if (cursor_glyph)
31827 /* r is relative to W's box, but w->phys_cursor.x is relative
31828 to left edge of W's TEXT area. Adjust it. */
31829 cr.x = window_box_left_offset (w, TEXT_AREA) + w->phys_cursor.x;
31830 cr.y = w->phys_cursor.y;
31831 cr.width = cursor_glyph->pixel_width;
31832 cr.height = w->phys_cursor_height;
31833 /* ++KFS: W32 version used W32-specific IntersectRect here, but
31834 I assume the effect is the same -- and this is portable. */
31835 return x_intersect_rectangles (&cr, r, &result);
31837 /* If we don't understand the format, pretend we're not in the hot-spot. */
31838 return false;
31842 /* EXPORT:
31843 Draw a vertical window border to the right of window W if W doesn't
31844 have vertical scroll bars. */
31846 void
31847 x_draw_vertical_border (struct window *w)
31849 struct frame *f = XFRAME (WINDOW_FRAME (w));
31851 /* We could do better, if we knew what type of scroll-bar the adjacent
31852 windows (on either side) have... But we don't :-(
31853 However, I think this works ok. ++KFS 2003-04-25 */
31855 /* Redraw borders between horizontally adjacent windows. Don't
31856 do it for frames with vertical scroll bars because either the
31857 right scroll bar of a window, or the left scroll bar of its
31858 neighbor will suffice as a border. */
31859 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f) || FRAME_RIGHT_DIVIDER_WIDTH (f))
31860 return;
31862 /* Note: It is necessary to redraw both the left and the right
31863 borders, for when only this single window W is being
31864 redisplayed. */
31865 if (!WINDOW_RIGHTMOST_P (w)
31866 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w))
31868 int x0, x1, y0, y1;
31870 window_box_edges (w, &x0, &y0, &x1, &y1);
31871 y1 -= 1;
31873 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
31874 x1 -= 1;
31876 FRAME_RIF (f)->draw_vertical_window_border (w, x1, y0, y1);
31879 if (!WINDOW_LEFTMOST_P (w)
31880 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w))
31882 int x0, x1, y0, y1;
31884 window_box_edges (w, &x0, &y0, &x1, &y1);
31885 y1 -= 1;
31887 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
31888 x0 -= 1;
31890 FRAME_RIF (f)->draw_vertical_window_border (w, x0, y0, y1);
31895 /* Draw window dividers for window W. */
31897 void
31898 x_draw_right_divider (struct window *w)
31900 struct frame *f = WINDOW_XFRAME (w);
31902 if (w->mini || w->pseudo_window_p)
31903 return;
31904 else if (WINDOW_RIGHT_DIVIDER_WIDTH (w))
31906 int x0 = WINDOW_RIGHT_EDGE_X (w) - WINDOW_RIGHT_DIVIDER_WIDTH (w);
31907 int x1 = WINDOW_RIGHT_EDGE_X (w);
31908 int y0 = WINDOW_TOP_EDGE_Y (w);
31909 int y1 = WINDOW_BOTTOM_EDGE_Y (w);
31911 /* If W is horizontally combined and has a right sibling, don't
31912 draw over any bottom divider. */
31913 if (WINDOW_BOTTOM_DIVIDER_WIDTH (w)
31914 && !NILP (w->parent)
31915 && WINDOW_HORIZONTAL_COMBINATION_P (XWINDOW (w->parent))
31916 && !NILP (w->next))
31917 y1 -= WINDOW_BOTTOM_DIVIDER_WIDTH (w);
31919 FRAME_RIF (f)->draw_window_divider (w, x0, x1, y0, y1);
31923 static void
31924 x_draw_bottom_divider (struct window *w)
31926 struct frame *f = XFRAME (WINDOW_FRAME (w));
31928 if (w->mini || w->pseudo_window_p)
31929 return;
31930 else if (WINDOW_BOTTOM_DIVIDER_WIDTH (w))
31932 int x0 = WINDOW_LEFT_EDGE_X (w);
31933 int x1 = WINDOW_RIGHT_EDGE_X (w);
31934 int y0 = WINDOW_BOTTOM_EDGE_Y (w) - WINDOW_BOTTOM_DIVIDER_WIDTH (w);
31935 int y1 = WINDOW_BOTTOM_EDGE_Y (w);
31936 struct window *p = !NILP (w->parent) ? XWINDOW (w->parent) : false;
31938 /* If W is vertically combined and has a sibling below, don't draw
31939 over any right divider. */
31940 if (WINDOW_RIGHT_DIVIDER_WIDTH (w)
31941 && p
31942 && ((WINDOW_VERTICAL_COMBINATION_P (p)
31943 && !NILP (w->next))
31944 || (WINDOW_HORIZONTAL_COMBINATION_P (p)
31945 && NILP (w->next)
31946 && !NILP (p->parent)
31947 && WINDOW_VERTICAL_COMBINATION_P (XWINDOW (p->parent))
31948 && !NILP (XWINDOW (p->parent)->next))))
31949 x1 -= WINDOW_RIGHT_DIVIDER_WIDTH (w);
31951 FRAME_RIF (f)->draw_window_divider (w, x0, x1, y0, y1);
31955 /* Redraw the part of window W intersection rectangle FR. Pixel
31956 coordinates in FR are frame-relative. Call this function with
31957 input blocked. Value is true if the exposure overwrites
31958 mouse-face. */
31960 static bool
31961 expose_window (struct window *w, XRectangle *fr)
31963 struct frame *f = XFRAME (w->frame);
31964 XRectangle wr, r;
31965 bool mouse_face_overwritten_p = false;
31967 /* If window is not yet fully initialized, do nothing. This can
31968 happen when toolkit scroll bars are used and a window is split.
31969 Reconfiguring the scroll bar will generate an expose for a newly
31970 created window. */
31971 if (w->current_matrix == NULL)
31972 return false;
31974 /* When we're currently updating the window, display and current
31975 matrix usually don't agree. Arrange for a thorough display
31976 later. */
31977 if (w->must_be_updated_p)
31979 SET_FRAME_GARBAGED (f);
31980 return false;
31983 /* Frame-relative pixel rectangle of W. */
31984 wr.x = WINDOW_LEFT_EDGE_X (w);
31985 wr.y = WINDOW_TOP_EDGE_Y (w);
31986 wr.width = WINDOW_PIXEL_WIDTH (w);
31987 wr.height = WINDOW_PIXEL_HEIGHT (w);
31989 if (x_intersect_rectangles (fr, &wr, &r))
31991 int yb = window_text_bottom_y (w);
31992 struct glyph_row *row;
31993 struct glyph_row *first_overlapping_row, *last_overlapping_row;
31995 TRACE ((stderr, "expose_window (%d, %d, %d, %d)\n",
31996 r.x, r.y, r.width, r.height));
31998 /* Convert to window coordinates. */
31999 r.x -= WINDOW_LEFT_EDGE_X (w);
32000 r.y -= WINDOW_TOP_EDGE_Y (w);
32002 /* Turn off the cursor. */
32003 bool cursor_cleared_p = (!w->pseudo_window_p
32004 && phys_cursor_in_rect_p (w, &r));
32005 if (cursor_cleared_p)
32006 x_clear_cursor (w);
32008 /* If the row containing the cursor extends face to end of line,
32009 then expose_area might overwrite the cursor outside the
32010 rectangle and thus notice_overwritten_cursor might clear
32011 w->phys_cursor_on_p. We remember the original value and
32012 check later if it is changed. */
32013 bool phys_cursor_on_p = w->phys_cursor_on_p;
32015 /* Use a signed int intermediate value to avoid catastrophic
32016 failures due to comparison between signed and unsigned, when
32017 y0 or y1 is negative (can happen for tall images). */
32018 int r_bottom = r.y + r.height;
32020 /* Update lines intersecting rectangle R. */
32021 first_overlapping_row = last_overlapping_row = NULL;
32022 for (row = w->current_matrix->rows;
32023 row->enabled_p;
32024 ++row)
32026 int y0 = row->y;
32027 int y1 = MATRIX_ROW_BOTTOM_Y (row);
32029 if ((y0 >= r.y && y0 < r_bottom)
32030 || (y1 > r.y && y1 < r_bottom)
32031 || (r.y >= y0 && r.y < y1)
32032 || (r_bottom > y0 && r_bottom < y1))
32034 /* A header line may be overlapping, but there is no need
32035 to fix overlapping areas for them. KFS 2005-02-12 */
32036 if (row->overlapping_p && !row->mode_line_p)
32038 if (first_overlapping_row == NULL)
32039 first_overlapping_row = row;
32040 last_overlapping_row = row;
32043 row->clip = fr;
32044 if (expose_line (w, row, &r))
32045 mouse_face_overwritten_p = true;
32046 row->clip = NULL;
32048 else if (row->overlapping_p)
32050 /* We must redraw a row overlapping the exposed area. */
32051 if (y0 < r.y
32052 ? y0 + row->phys_height > r.y
32053 : y0 + row->ascent - row->phys_ascent < r.y +r.height)
32055 if (first_overlapping_row == NULL)
32056 first_overlapping_row = row;
32057 last_overlapping_row = row;
32061 if (y1 >= yb)
32062 break;
32065 /* Display the mode line if there is one. */
32066 if (window_wants_mode_line (w)
32067 && (row = MATRIX_MODE_LINE_ROW (w->current_matrix),
32068 row->enabled_p)
32069 && row->y < r_bottom)
32071 if (expose_line (w, row, &r))
32072 mouse_face_overwritten_p = true;
32075 if (!w->pseudo_window_p)
32077 /* Fix the display of overlapping rows. */
32078 if (first_overlapping_row)
32079 expose_overlaps (w, first_overlapping_row, last_overlapping_row,
32080 fr);
32082 /* Draw border between windows. */
32083 if (WINDOW_RIGHT_DIVIDER_WIDTH (w))
32084 x_draw_right_divider (w);
32085 else
32086 x_draw_vertical_border (w);
32088 if (WINDOW_BOTTOM_DIVIDER_WIDTH (w))
32089 x_draw_bottom_divider (w);
32091 /* Turn the cursor on again. */
32092 if (cursor_cleared_p
32093 || (phys_cursor_on_p && !w->phys_cursor_on_p))
32094 update_window_cursor (w, true);
32098 return mouse_face_overwritten_p;
32103 /* Redraw (parts) of all windows in the window tree rooted at W that
32104 intersect R. R contains frame pixel coordinates. Value is
32105 true if the exposure overwrites mouse-face. */
32107 static bool
32108 expose_window_tree (struct window *w, XRectangle *r)
32110 struct frame *f = XFRAME (w->frame);
32111 bool mouse_face_overwritten_p = false;
32113 while (w && !FRAME_GARBAGED_P (f))
32115 mouse_face_overwritten_p
32116 |= (WINDOWP (w->contents)
32117 ? expose_window_tree (XWINDOW (w->contents), r)
32118 : expose_window (w, r));
32120 w = NILP (w->next) ? NULL : XWINDOW (w->next);
32123 return mouse_face_overwritten_p;
32127 /* EXPORT:
32128 Redisplay an exposed area of frame F. X and Y are the upper-left
32129 corner of the exposed rectangle. W and H are width and height of
32130 the exposed area. All are pixel values. W or H zero means redraw
32131 the entire frame. */
32133 void
32134 expose_frame (struct frame *f, int x, int y, int w, int h)
32136 XRectangle r;
32137 bool mouse_face_overwritten_p = false;
32139 TRACE ((stderr, "expose_frame "));
32141 /* No need to redraw if frame will be redrawn soon. */
32142 if (FRAME_GARBAGED_P (f))
32144 TRACE ((stderr, " garbaged\n"));
32145 return;
32148 /* If basic faces haven't been realized yet, there is no point in
32149 trying to redraw anything. This can happen when we get an expose
32150 event while Emacs is starting, e.g. by moving another window. */
32151 if (FRAME_FACE_CACHE (f) == NULL
32152 || FRAME_FACE_CACHE (f)->used < BASIC_FACE_ID_SENTINEL)
32154 TRACE ((stderr, " no faces\n"));
32155 return;
32158 if (w == 0 || h == 0)
32160 r.x = r.y = 0;
32161 r.width = FRAME_TEXT_WIDTH (f);
32162 r.height = FRAME_TEXT_HEIGHT (f);
32164 else
32166 r.x = x;
32167 r.y = y;
32168 r.width = w;
32169 r.height = h;
32172 TRACE ((stderr, "(%d, %d, %d, %d)\n", r.x, r.y, r.width, r.height));
32173 mouse_face_overwritten_p = expose_window_tree (XWINDOW (f->root_window), &r);
32175 #if ! defined (USE_GTK) && ! defined (HAVE_NS)
32176 if (WINDOWP (f->tool_bar_window))
32177 mouse_face_overwritten_p
32178 |= expose_window (XWINDOW (f->tool_bar_window), &r);
32179 #endif
32181 #ifdef HAVE_X_WINDOWS
32182 #ifndef MSDOS
32183 #if ! defined (USE_X_TOOLKIT) && ! defined (USE_GTK)
32184 if (WINDOWP (f->menu_bar_window))
32185 mouse_face_overwritten_p
32186 |= expose_window (XWINDOW (f->menu_bar_window), &r);
32187 #endif /* not USE_X_TOOLKIT and not USE_GTK */
32188 #endif
32189 #endif
32191 /* Some window managers support a focus-follows-mouse style with
32192 delayed raising of frames. Imagine a partially obscured frame,
32193 and moving the mouse into partially obscured mouse-face on that
32194 frame. The visible part of the mouse-face will be highlighted,
32195 then the WM raises the obscured frame. With at least one WM, KDE
32196 2.1, Emacs is not getting any event for the raising of the frame
32197 (even tried with SubstructureRedirectMask), only Expose events.
32198 These expose events will draw text normally, i.e. not
32199 highlighted. Which means we must redo the highlight here.
32200 Subsume it under ``we love X''. --gerd 2001-08-15 */
32201 /* Included in Windows version because Windows most likely does not
32202 do the right thing if any third party tool offers
32203 focus-follows-mouse with delayed raise. --jason 2001-10-12 */
32204 if (mouse_face_overwritten_p && !FRAME_GARBAGED_P (f))
32206 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
32207 if (f == hlinfo->mouse_face_mouse_frame)
32209 int mouse_x = hlinfo->mouse_face_mouse_x;
32210 int mouse_y = hlinfo->mouse_face_mouse_y;
32211 clear_mouse_face (hlinfo);
32212 note_mouse_highlight (f, mouse_x, mouse_y);
32218 /* EXPORT:
32219 Determine the intersection of two rectangles R1 and R2. Return
32220 the intersection in *RESULT. Value is true if RESULT is not
32221 empty. */
32223 bool
32224 x_intersect_rectangles (XRectangle *r1, XRectangle *r2, XRectangle *result)
32226 XRectangle *left, *right;
32227 XRectangle *upper, *lower;
32228 bool intersection_p = false;
32230 /* Rearrange so that R1 is the left-most rectangle. */
32231 if (r1->x < r2->x)
32232 left = r1, right = r2;
32233 else
32234 left = r2, right = r1;
32236 /* X0 of the intersection is right.x0, if this is inside R1,
32237 otherwise there is no intersection. */
32238 if (right->x <= left->x + left->width)
32240 result->x = right->x;
32242 /* The right end of the intersection is the minimum of
32243 the right ends of left and right. */
32244 result->width = (min (left->x + left->width, right->x + right->width)
32245 - result->x);
32247 /* Same game for Y. */
32248 if (r1->y < r2->y)
32249 upper = r1, lower = r2;
32250 else
32251 upper = r2, lower = r1;
32253 /* The upper end of the intersection is lower.y0, if this is inside
32254 of upper. Otherwise, there is no intersection. */
32255 if (lower->y <= upper->y + upper->height)
32257 result->y = lower->y;
32259 /* The lower end of the intersection is the minimum of the lower
32260 ends of upper and lower. */
32261 result->height = (min (lower->y + lower->height,
32262 upper->y + upper->height)
32263 - result->y);
32264 intersection_p = true;
32268 return intersection_p;
32271 #endif /* HAVE_WINDOW_SYSTEM */
32274 /***********************************************************************
32275 Initialization
32276 ***********************************************************************/
32278 void
32279 syms_of_xdisp (void)
32281 Vwith_echo_area_save_vector = Qnil;
32282 staticpro (&Vwith_echo_area_save_vector);
32284 Vmessage_stack = Qnil;
32285 staticpro (&Vmessage_stack);
32287 /* Non-nil means don't actually do any redisplay. */
32288 DEFSYM (Qinhibit_redisplay, "inhibit-redisplay");
32290 DEFSYM (Qredisplay_internal_xC_functionx, "redisplay_internal (C function)");
32292 DEFVAR_BOOL("inhibit-message", inhibit_message,
32293 doc: /* Non-nil means calls to `message' are not displayed.
32294 They are still logged to the *Messages* buffer. */);
32295 inhibit_message = 0;
32297 message_dolog_marker1 = Fmake_marker ();
32298 staticpro (&message_dolog_marker1);
32299 message_dolog_marker2 = Fmake_marker ();
32300 staticpro (&message_dolog_marker2);
32301 message_dolog_marker3 = Fmake_marker ();
32302 staticpro (&message_dolog_marker3);
32304 defsubr (&Sset_buffer_redisplay);
32305 #ifdef GLYPH_DEBUG
32306 defsubr (&Sdump_frame_glyph_matrix);
32307 defsubr (&Sdump_glyph_matrix);
32308 defsubr (&Sdump_glyph_row);
32309 defsubr (&Sdump_tool_bar_row);
32310 defsubr (&Strace_redisplay);
32311 defsubr (&Strace_to_stderr);
32312 #endif
32313 #ifdef HAVE_WINDOW_SYSTEM
32314 defsubr (&Stool_bar_height);
32315 defsubr (&Slookup_image_map);
32316 #endif
32317 defsubr (&Sline_pixel_height);
32318 defsubr (&Sformat_mode_line);
32319 defsubr (&Sinvisible_p);
32320 defsubr (&Scurrent_bidi_paragraph_direction);
32321 defsubr (&Swindow_text_pixel_size);
32322 defsubr (&Smove_point_visually);
32323 defsubr (&Sbidi_find_overridden_directionality);
32325 DEFSYM (Qmenu_bar_update_hook, "menu-bar-update-hook");
32326 DEFSYM (Qoverriding_terminal_local_map, "overriding-terminal-local-map");
32327 DEFSYM (Qoverriding_local_map, "overriding-local-map");
32328 DEFSYM (Qwindow_scroll_functions, "window-scroll-functions");
32329 DEFSYM (Qredisplay_end_trigger_functions, "redisplay-end-trigger-functions");
32330 DEFSYM (Qinhibit_point_motion_hooks, "inhibit-point-motion-hooks");
32331 DEFSYM (Qeval, "eval");
32332 DEFSYM (QCdata, ":data");
32334 /* Names of text properties relevant for redisplay. */
32335 DEFSYM (Qdisplay, "display");
32336 DEFSYM (Qspace_width, "space-width");
32337 DEFSYM (Qraise, "raise");
32338 DEFSYM (Qslice, "slice");
32339 DEFSYM (Qspace, "space");
32340 DEFSYM (Qmargin, "margin");
32341 DEFSYM (Qpointer, "pointer");
32342 DEFSYM (Qleft_margin, "left-margin");
32343 DEFSYM (Qright_margin, "right-margin");
32344 DEFSYM (Qcenter, "center");
32345 DEFSYM (Qline_height, "line-height");
32346 DEFSYM (QCalign_to, ":align-to");
32347 DEFSYM (QCrelative_width, ":relative-width");
32348 DEFSYM (QCrelative_height, ":relative-height");
32349 DEFSYM (QCeval, ":eval");
32350 DEFSYM (QCpropertize, ":propertize");
32351 DEFSYM (QCfile, ":file");
32352 DEFSYM (Qfontified, "fontified");
32353 DEFSYM (Qfontification_functions, "fontification-functions");
32355 /* Name of the symbol which disables Lisp evaluation in 'display'
32356 properties. This is used by enriched.el. */
32357 DEFSYM (Qdisable_eval, "disable-eval");
32359 /* Name of the face used to highlight trailing whitespace. */
32360 DEFSYM (Qtrailing_whitespace, "trailing-whitespace");
32362 /* Names of the faces used to display line numbers. */
32363 DEFSYM (Qline_number, "line-number");
32364 DEFSYM (Qline_number_current_line, "line-number-current-line");
32365 /* Name of a text property which disables line-number display. */
32366 DEFSYM (Qdisplay_line_numbers_disable, "display-line-numbers-disable");
32368 /* Name and number of the face used to highlight escape glyphs. */
32369 DEFSYM (Qescape_glyph, "escape-glyph");
32371 /* Name and number of the face used to highlight non-breaking
32372 spaces/hyphens. */
32373 DEFSYM (Qnobreak_space, "nobreak-space");
32374 DEFSYM (Qnobreak_hyphen, "nobreak-hyphen");
32376 /* The symbol 'image' which is the car of the lists used to represent
32377 images in Lisp. Also a tool bar style. */
32378 DEFSYM (Qimage, "image");
32380 /* Tool bar styles. */
32381 DEFSYM (Qtext, "text");
32382 DEFSYM (Qboth, "both");
32383 DEFSYM (Qboth_horiz, "both-horiz");
32384 DEFSYM (Qtext_image_horiz, "text-image-horiz");
32386 /* The image map types. */
32387 DEFSYM (QCmap, ":map");
32388 DEFSYM (QCpointer, ":pointer");
32389 DEFSYM (Qrect, "rect");
32390 DEFSYM (Qcircle, "circle");
32391 DEFSYM (Qpoly, "poly");
32393 DEFSYM (Qinhibit_menubar_update, "inhibit-menubar-update");
32395 DEFSYM (Qgrow_only, "grow-only");
32396 DEFSYM (Qinhibit_eval_during_redisplay, "inhibit-eval-during-redisplay");
32397 DEFSYM (Qposition, "position");
32398 DEFSYM (Qbuffer_position, "buffer-position");
32399 DEFSYM (Qobject, "object");
32401 /* Cursor shapes. */
32402 DEFSYM (Qbar, "bar");
32403 DEFSYM (Qhbar, "hbar");
32404 DEFSYM (Qbox, "box");
32405 DEFSYM (Qhollow, "hollow");
32407 /* Pointer shapes. */
32408 DEFSYM (Qhand, "hand");
32409 DEFSYM (Qarrow, "arrow");
32410 /* also Qtext */
32412 DEFSYM (Qdragging, "dragging");
32414 DEFSYM (Qinhibit_free_realized_faces, "inhibit-free-realized-faces");
32416 list_of_error = list1 (list2 (Qerror, Qvoid_variable));
32417 staticpro (&list_of_error);
32419 /* Values of those variables at last redisplay are stored as
32420 properties on 'overlay-arrow-position' symbol. However, if
32421 Voverlay_arrow_position is a marker, last-arrow-position is its
32422 numerical position. */
32423 DEFSYM (Qlast_arrow_position, "last-arrow-position");
32424 DEFSYM (Qlast_arrow_string, "last-arrow-string");
32426 /* Alternative overlay-arrow-string and overlay-arrow-bitmap
32427 properties on a symbol in overlay-arrow-variable-list. */
32428 DEFSYM (Qoverlay_arrow_string, "overlay-arrow-string");
32429 DEFSYM (Qoverlay_arrow_bitmap, "overlay-arrow-bitmap");
32431 echo_buffer[0] = echo_buffer[1] = Qnil;
32432 staticpro (&echo_buffer[0]);
32433 staticpro (&echo_buffer[1]);
32435 echo_area_buffer[0] = echo_area_buffer[1] = Qnil;
32436 staticpro (&echo_area_buffer[0]);
32437 staticpro (&echo_area_buffer[1]);
32439 Vmessages_buffer_name = build_pure_c_string ("*Messages*");
32440 staticpro (&Vmessages_buffer_name);
32442 mode_line_proptrans_alist = Qnil;
32443 staticpro (&mode_line_proptrans_alist);
32444 mode_line_string_list = Qnil;
32445 staticpro (&mode_line_string_list);
32446 mode_line_string_face = Qnil;
32447 staticpro (&mode_line_string_face);
32448 mode_line_string_face_prop = Qnil;
32449 staticpro (&mode_line_string_face_prop);
32450 Vmode_line_unwind_vector = Qnil;
32451 staticpro (&Vmode_line_unwind_vector);
32453 DEFSYM (Qmode_line_default_help_echo, "mode-line-default-help-echo");
32455 help_echo_string = Qnil;
32456 staticpro (&help_echo_string);
32457 help_echo_object = Qnil;
32458 staticpro (&help_echo_object);
32459 help_echo_window = Qnil;
32460 staticpro (&help_echo_window);
32461 previous_help_echo_string = Qnil;
32462 staticpro (&previous_help_echo_string);
32463 help_echo_pos = -1;
32465 DEFSYM (Qright_to_left, "right-to-left");
32466 DEFSYM (Qleft_to_right, "left-to-right");
32467 defsubr (&Sbidi_resolved_levels);
32469 #ifdef HAVE_WINDOW_SYSTEM
32470 DEFVAR_BOOL ("x-stretch-cursor", x_stretch_cursor_p,
32471 doc: /* Non-nil means draw block cursor as wide as the glyph under it.
32472 For example, if a block cursor is over a tab, it will be drawn as
32473 wide as that tab on the display. */);
32474 x_stretch_cursor_p = 0;
32475 #endif
32477 DEFVAR_LISP ("show-trailing-whitespace", Vshow_trailing_whitespace,
32478 doc: /* Non-nil means highlight trailing whitespace.
32479 The face used for trailing whitespace is `trailing-whitespace'. */);
32480 Vshow_trailing_whitespace = Qnil;
32482 DEFVAR_LISP ("nobreak-char-display", Vnobreak_char_display,
32483 doc: /* Control highlighting of non-ASCII space and hyphen chars.
32484 If the value is t, Emacs highlights non-ASCII chars which have the
32485 same appearance as an ASCII space or hyphen, using the `nobreak-space'
32486 or `nobreak-hyphen' face respectively.
32488 U+00A0 (no-break space), U+00AD (soft hyphen), U+2010 (hyphen), and
32489 U+2011 (non-breaking hyphen) are affected.
32491 Any other non-nil value means to display these characters as an escape
32492 glyph followed by an ordinary space or hyphen.
32494 A value of nil means no special handling of these characters. */);
32495 Vnobreak_char_display = Qt;
32497 DEFVAR_LISP ("void-text-area-pointer", Vvoid_text_area_pointer,
32498 doc: /* The pointer shape to show in void text areas.
32499 A value of nil means to show the text pointer. Other options are
32500 `arrow', `text', `hand', `vdrag', `hdrag', `nhdrag', `modeline', and
32501 `hourglass'. */);
32502 Vvoid_text_area_pointer = Qarrow;
32504 DEFVAR_LISP ("inhibit-redisplay", Vinhibit_redisplay,
32505 doc: /* Non-nil means don't actually do any redisplay.
32506 This is used for internal purposes. */);
32507 Vinhibit_redisplay = Qnil;
32509 DEFVAR_LISP ("global-mode-string", Vglobal_mode_string,
32510 doc: /* String (or mode line construct) included (normally) in `mode-line-format'. */);
32511 Vglobal_mode_string = Qnil;
32513 DEFVAR_LISP ("overlay-arrow-position", Voverlay_arrow_position,
32514 doc: /* Marker for where to display an arrow on top of the buffer text.
32515 This must be the beginning of a line in order to work.
32516 See also `overlay-arrow-string'. */);
32517 Voverlay_arrow_position = Qnil;
32519 DEFVAR_LISP ("overlay-arrow-string", Voverlay_arrow_string,
32520 doc: /* String to display as an arrow in non-window frames.
32521 See also `overlay-arrow-position'. */);
32522 Voverlay_arrow_string = build_pure_c_string ("=>");
32524 DEFVAR_LISP ("overlay-arrow-variable-list", Voverlay_arrow_variable_list,
32525 doc: /* List of variables (symbols) which hold markers for overlay arrows.
32526 The symbols on this list are examined during redisplay to determine
32527 where to display overlay arrows. */);
32528 Voverlay_arrow_variable_list
32529 = list1 (intern_c_string ("overlay-arrow-position"));
32531 DEFVAR_INT ("scroll-step", emacs_scroll_step,
32532 doc: /* The number of lines to try scrolling a window by when point moves out.
32533 If that fails to bring point back on frame, point is centered instead.
32534 If this is zero, point is always centered after it moves off frame.
32535 If you want scrolling to always be a line at a time, you should set
32536 `scroll-conservatively' to a large value rather than set this to 1. */);
32538 DEFVAR_INT ("scroll-conservatively", scroll_conservatively,
32539 doc: /* Scroll up to this many lines, to bring point back on screen.
32540 If point moves off-screen, redisplay will scroll by up to
32541 `scroll-conservatively' lines in order to bring point just barely
32542 onto the screen again. If that cannot be done, then redisplay
32543 recenters point as usual.
32545 If the value is greater than 100, redisplay will never recenter point,
32546 but will always scroll just enough text to bring point into view, even
32547 if you move far away.
32549 A value of zero means always recenter point if it moves off screen. */);
32550 scroll_conservatively = 0;
32552 DEFVAR_INT ("scroll-margin", scroll_margin,
32553 doc: /* Number of lines of margin at the top and bottom of a window.
32554 Trigger automatic scrolling whenever point gets within this many lines
32555 of the top or bottom of the window (see info node `Auto Scrolling'). */);
32556 scroll_margin = 0;
32558 DEFVAR_LISP ("maximum-scroll-margin", Vmaximum_scroll_margin,
32559 doc: /* Maximum effective value of `scroll-margin'.
32560 Given as a fraction of the current window's lines. The value should
32561 be a floating point number between 0.0 and 0.5. The effective maximum
32562 is limited to (/ (1- window-lines) 2). Non-float values for this
32563 variable are ignored and the default 0.25 is used instead. */);
32564 Vmaximum_scroll_margin = make_float (0.25);
32566 DEFVAR_LISP ("display-pixels-per-inch", Vdisplay_pixels_per_inch,
32567 doc: /* Pixels per inch value for non-window system displays.
32568 Value is a number or a cons (WIDTH-DPI . HEIGHT-DPI). */);
32569 Vdisplay_pixels_per_inch = make_float (72.0);
32571 #ifdef GLYPH_DEBUG
32572 DEFVAR_INT ("debug-end-pos", debug_end_pos, doc: /* Don't ask. */);
32573 #endif
32575 DEFVAR_LISP ("truncate-partial-width-windows",
32576 Vtruncate_partial_width_windows,
32577 doc: /* Non-nil means truncate lines in windows narrower than the frame.
32578 For an integer value, truncate lines in each window narrower than the
32579 full frame width, provided the total window width in column units is less
32580 than that integer; otherwise, respect the value of `truncate-lines'.
32581 The total width of the window is as returned by `window-total-width', it
32582 includes the fringes, the continuation and truncation glyphs, the
32583 display margins (if any), and the scroll bar
32585 For any other non-nil value, truncate lines in all windows that do
32586 not span the full frame width.
32588 A value of nil means to respect the value of `truncate-lines'.
32590 If `word-wrap' is enabled, you might want to reduce this. */);
32591 Vtruncate_partial_width_windows = make_number (50);
32593 DEFVAR_LISP ("line-number-display-limit", Vline_number_display_limit,
32594 doc: /* Maximum buffer size for which line number should be displayed.
32595 If the buffer is bigger than this, the line number does not appear
32596 in the mode line. A value of nil means no limit. */);
32597 Vline_number_display_limit = Qnil;
32599 DEFVAR_INT ("line-number-display-limit-width",
32600 line_number_display_limit_width,
32601 doc: /* Maximum line width (in characters) for line number display.
32602 If the average length of the lines near point is bigger than this, then the
32603 line number may be omitted from the mode line. */);
32604 line_number_display_limit_width = 200;
32606 DEFVAR_BOOL ("highlight-nonselected-windows", highlight_nonselected_windows,
32607 doc: /* Non-nil means highlight region even in nonselected windows. */);
32608 highlight_nonselected_windows = false;
32610 DEFVAR_BOOL ("multiple-frames", multiple_frames,
32611 doc: /* Non-nil if more than one frame is visible on this display.
32612 Minibuffer-only frames don't count, but iconified frames do.
32613 This variable is not guaranteed to be accurate except while processing
32614 `frame-title-format' and `icon-title-format'. */);
32616 DEFVAR_LISP ("frame-title-format", Vframe_title_format,
32617 doc: /* Template for displaying the title bar of visible frames.
32618 \(Assuming the window manager supports this feature.)
32620 This variable has the same structure as `mode-line-format', except that
32621 the %c, %C, and %l constructs are ignored. It is used only on frames for
32622 which no explicit name has been set (see `modify-frame-parameters'). */);
32624 DEFVAR_LISP ("icon-title-format", Vicon_title_format,
32625 doc: /* Template for displaying the title bar of an iconified frame.
32626 \(Assuming the window manager supports this feature.)
32627 This variable has the same structure as `mode-line-format' (which see),
32628 and is used only on frames for which no explicit name has been set
32629 \(see `modify-frame-parameters'). */);
32630 Vicon_title_format
32631 = Vframe_title_format
32632 = listn (CONSTYPE_PURE, 3,
32633 intern_c_string ("multiple-frames"),
32634 build_pure_c_string ("%b"),
32635 listn (CONSTYPE_PURE, 4,
32636 empty_unibyte_string,
32637 intern_c_string ("invocation-name"),
32638 build_pure_c_string ("@"),
32639 intern_c_string ("system-name")));
32641 DEFVAR_LISP ("message-log-max", Vmessage_log_max,
32642 doc: /* Maximum number of lines to keep in the message log buffer.
32643 If nil, disable message logging. If t, log messages but don't truncate
32644 the buffer when it becomes large. */);
32645 Vmessage_log_max = make_number (1000);
32647 DEFVAR_LISP ("window-scroll-functions", Vwindow_scroll_functions,
32648 doc: /* List of functions to call before redisplaying a window with scrolling.
32649 Each function is called with two arguments, the window and its new
32650 display-start position.
32651 These functions are called whenever the `window-start' marker is modified,
32652 either to point into another buffer (e.g. via `set-window-buffer') or another
32653 place in the same buffer.
32654 When each function is called, the `window-start' marker of its window
32655 argument has been already set to the new value, and the buffer which that
32656 window will display is set to be the current buffer.
32657 Note that the value of `window-end' is not valid when these functions are
32658 called.
32660 Warning: Do not use this feature to alter the way the window
32661 is scrolled. It is not designed for that, and such use probably won't
32662 work. */);
32663 Vwindow_scroll_functions = Qnil;
32665 DEFVAR_LISP ("redisplay-end-trigger-functions", Vredisplay_end_trigger_functions,
32666 doc: /* Functions called when redisplay of a window reaches the end trigger.
32667 Each function is called with two arguments, the window and the end trigger value.
32668 See `set-window-redisplay-end-trigger'. */);
32669 Vredisplay_end_trigger_functions = Qnil;
32671 DEFVAR_LISP ("mouse-autoselect-window", Vmouse_autoselect_window,
32672 doc: /* Non-nil means autoselect window with mouse pointer.
32673 If nil, do not autoselect windows.
32674 A positive number means delay autoselection by that many seconds: a
32675 window is autoselected only after the mouse has remained in that
32676 window for the duration of the delay.
32677 A negative number has a similar effect, but causes windows to be
32678 autoselected only after the mouse has stopped moving. (Because of
32679 the way Emacs compares mouse events, you will occasionally wait twice
32680 that time before the window gets selected.)
32681 Any other value means to autoselect window instantaneously when the
32682 mouse pointer enters it.
32684 Autoselection selects the minibuffer only if it is active, and never
32685 unselects the minibuffer if it is active.
32687 When customizing this variable make sure that the actual value of
32688 `focus-follows-mouse' matches the behavior of your window manager. */);
32689 Vmouse_autoselect_window = Qnil;
32691 DEFVAR_LISP ("auto-resize-tool-bars", Vauto_resize_tool_bars,
32692 doc: /* Non-nil means automatically resize tool-bars.
32693 This dynamically changes the tool-bar's height to the minimum height
32694 that is needed to make all tool-bar items visible.
32695 If value is `grow-only', the tool-bar's height is only increased
32696 automatically; to decrease the tool-bar height, use \\[recenter]. */);
32697 Vauto_resize_tool_bars = Qt;
32699 DEFVAR_BOOL ("auto-raise-tool-bar-buttons", auto_raise_tool_bar_buttons_p,
32700 doc: /* Non-nil means raise tool-bar buttons when the mouse moves over them. */);
32701 auto_raise_tool_bar_buttons_p = true;
32703 DEFVAR_BOOL ("make-cursor-line-fully-visible", make_cursor_line_fully_visible_p,
32704 doc: /* Non-nil means to scroll (recenter) cursor line if it is not fully visible. */);
32705 make_cursor_line_fully_visible_p = true;
32707 DEFVAR_LISP ("tool-bar-border", Vtool_bar_border,
32708 doc: /* Border below tool-bar in pixels.
32709 If an integer, use it as the height of the border.
32710 If it is one of `internal-border-width' or `border-width', use the
32711 value of the corresponding frame parameter.
32712 Otherwise, no border is added below the tool-bar. */);
32713 Vtool_bar_border = Qinternal_border_width;
32715 DEFVAR_LISP ("tool-bar-button-margin", Vtool_bar_button_margin,
32716 doc: /* Margin around tool-bar buttons in pixels.
32717 If an integer, use that for both horizontal and vertical margins.
32718 Otherwise, value should be a pair of integers `(HORZ . VERT)' with
32719 HORZ specifying the horizontal margin, and VERT specifying the
32720 vertical margin. */);
32721 Vtool_bar_button_margin = make_number (DEFAULT_TOOL_BAR_BUTTON_MARGIN);
32723 DEFVAR_INT ("tool-bar-button-relief", tool_bar_button_relief,
32724 doc: /* Relief thickness of tool-bar buttons. */);
32725 tool_bar_button_relief = DEFAULT_TOOL_BAR_BUTTON_RELIEF;
32727 DEFVAR_LISP ("tool-bar-style", Vtool_bar_style,
32728 doc: /* Tool bar style to use.
32729 It can be one of
32730 image - show images only
32731 text - show text only
32732 both - show both, text below image
32733 both-horiz - show text to the right of the image
32734 text-image-horiz - show text to the left of the image
32735 any other - use system default or image if no system default.
32737 This variable only affects the GTK+ toolkit version of Emacs. */);
32738 Vtool_bar_style = Qnil;
32740 DEFVAR_INT ("tool-bar-max-label-size", tool_bar_max_label_size,
32741 doc: /* Maximum number of characters a label can have to be shown.
32742 The tool bar style must also show labels for this to have any effect, see
32743 `tool-bar-style'. */);
32744 tool_bar_max_label_size = DEFAULT_TOOL_BAR_LABEL_SIZE;
32746 DEFVAR_LISP ("fontification-functions", Vfontification_functions,
32747 doc: /* List of functions to call to fontify regions of text.
32748 Each function is called with one argument POS. Functions must
32749 fontify a region starting at POS in the current buffer, and give
32750 fontified regions the property `fontified'. */);
32751 Vfontification_functions = Qnil;
32752 Fmake_variable_buffer_local (Qfontification_functions);
32754 DEFVAR_BOOL ("unibyte-display-via-language-environment",
32755 unibyte_display_via_language_environment,
32756 doc: /* Non-nil means display unibyte text according to language environment.
32757 Specifically, this means that raw bytes in the range 160-255 decimal
32758 are displayed by converting them to the equivalent multibyte characters
32759 according to the current language environment. As a result, they are
32760 displayed according to the current fontset.
32762 Note that this variable affects only how these bytes are displayed,
32763 but does not change the fact they are interpreted as raw bytes. */);
32764 unibyte_display_via_language_environment = false;
32766 DEFVAR_LISP ("max-mini-window-height", Vmax_mini_window_height,
32767 doc: /* Maximum height for resizing mini-windows (the minibuffer and the echo area).
32768 If a float, it specifies a fraction of the mini-window frame's height.
32769 If an integer, it specifies a number of lines. */);
32770 Vmax_mini_window_height = make_float (0.25);
32772 DEFVAR_LISP ("resize-mini-windows", Vresize_mini_windows,
32773 doc: /* How to resize mini-windows (the minibuffer and the echo area).
32774 A value of nil means don't automatically resize mini-windows.
32775 A value of t means resize them to fit the text displayed in them.
32776 A value of `grow-only', the default, means let mini-windows grow only;
32777 they return to their normal size when the minibuffer is closed, or the
32778 echo area becomes empty. */);
32779 /* Contrary to the doc string, we initialize this to nil, so that
32780 loading loadup.el won't try to resize windows before loading
32781 window.el, where some functions we need to call for this live.
32782 We assign the 'grow-only' value right after loading window.el
32783 during loadup. */
32784 Vresize_mini_windows = Qnil;
32786 DEFVAR_LISP ("blink-cursor-alist", Vblink_cursor_alist,
32787 doc: /* Alist specifying how to blink the cursor off.
32788 Each element has the form (ON-STATE . OFF-STATE). Whenever the
32789 `cursor-type' frame-parameter or variable equals ON-STATE,
32790 comparing using `equal', Emacs uses OFF-STATE to specify
32791 how to blink it off. ON-STATE and OFF-STATE are values for
32792 the `cursor-type' frame parameter.
32794 If a frame's ON-STATE has no entry in this list,
32795 the frame's other specifications determine how to blink the cursor off. */);
32796 Vblink_cursor_alist = Qnil;
32798 DEFVAR_LISP ("auto-hscroll-mode", automatic_hscrolling,
32799 doc: /* Allow or disallow automatic horizontal scrolling of windows.
32800 The value `current-line' means the line displaying point in each window
32801 is automatically scrolled horizontally to make point visible.
32802 Any other non-nil value means all the lines in a window are automatically
32803 scrolled horizontally to make point visible. */);
32804 automatic_hscrolling = Qt;
32805 DEFSYM (Qauto_hscroll_mode, "auto-hscroll-mode");
32806 DEFSYM (Qcurrent_line, "current-line");
32808 DEFVAR_INT ("hscroll-margin", hscroll_margin,
32809 doc: /* How many columns away from the window edge point is allowed to get
32810 before automatic hscrolling will horizontally scroll the window. */);
32811 hscroll_margin = 5;
32813 DEFVAR_LISP ("hscroll-step", Vhscroll_step,
32814 doc: /* How many columns to scroll the window when point gets too close to the edge.
32815 When point is less than `hscroll-margin' columns from the window
32816 edge, automatic hscrolling will scroll the window by the amount of columns
32817 determined by this variable. If its value is a positive integer, scroll that
32818 many columns. If it's a positive floating-point number, it specifies the
32819 fraction of the window's width to scroll. If it's nil or zero, point will be
32820 centered horizontally after the scroll. Any other value, including negative
32821 numbers, are treated as if the value were zero.
32823 Automatic hscrolling always moves point outside the scroll margin, so if
32824 point was more than scroll step columns inside the margin, the window will
32825 scroll more than the value given by the scroll step.
32827 Note that the lower bound for automatic hscrolling specified by `scroll-left'
32828 and `scroll-right' overrides this variable's effect. */);
32829 Vhscroll_step = make_number (0);
32831 DEFVAR_BOOL ("message-truncate-lines", message_truncate_lines,
32832 doc: /* If non-nil, messages are truncated instead of resizing the echo area.
32833 Bind this around calls to `message' to let it take effect. */);
32834 message_truncate_lines = false;
32836 DEFVAR_LISP ("menu-bar-update-hook", Vmenu_bar_update_hook,
32837 doc: /* Normal hook run to update the menu bar definitions.
32838 Redisplay runs this hook before it redisplays the menu bar.
32839 This is used to update menus such as Buffers, whose contents depend on
32840 various data. */);
32841 Vmenu_bar_update_hook = Qnil;
32843 DEFVAR_LISP ("menu-updating-frame", Vmenu_updating_frame,
32844 doc: /* Frame for which we are updating a menu.
32845 The enable predicate for a menu binding should check this variable. */);
32846 Vmenu_updating_frame = Qnil;
32848 DEFVAR_BOOL ("inhibit-menubar-update", inhibit_menubar_update,
32849 doc: /* Non-nil means don't update menu bars. Internal use only. */);
32850 inhibit_menubar_update = false;
32852 DEFVAR_LISP ("wrap-prefix", Vwrap_prefix,
32853 doc: /* Prefix prepended to all continuation lines at display time.
32854 The value may be a string, an image, or a stretch-glyph; it is
32855 interpreted in the same way as the value of a `display' text property.
32857 This variable is overridden by any `wrap-prefix' text or overlay
32858 property.
32860 To add a prefix to non-continuation lines, use `line-prefix'. */);
32861 Vwrap_prefix = Qnil;
32862 DEFSYM (Qwrap_prefix, "wrap-prefix");
32863 Fmake_variable_buffer_local (Qwrap_prefix);
32865 DEFVAR_LISP ("line-prefix", Vline_prefix,
32866 doc: /* Prefix prepended to all non-continuation lines at display time.
32867 The value may be a string, an image, or a stretch-glyph; it is
32868 interpreted in the same way as the value of a `display' text property.
32870 This variable is overridden by any `line-prefix' text or overlay
32871 property.
32873 To add a prefix to continuation lines, use `wrap-prefix'. */);
32874 Vline_prefix = Qnil;
32875 DEFSYM (Qline_prefix, "line-prefix");
32876 Fmake_variable_buffer_local (Qline_prefix);
32878 DEFVAR_LISP ("display-line-numbers", Vdisplay_line_numbers,
32879 doc: /* Non-nil means display line numbers.
32880 If the value is t, display the absolute number of each line of a buffer
32881 shown in a window. Absolute line numbers count from the beginning of
32882 the current narrowing, or from buffer beginning. If the value is
32883 `relative', display for each line not containing the window's point its
32884 relative number instead, i.e. the number of the line relative to the
32885 line showing the window's point.
32887 In either case, line numbers are displayed at the beginning of each
32888 non-continuation line that displays buffer text, i.e. after each newline
32889 character that comes from the buffer. The value `visual' is like
32890 `relative' but counts screen lines instead of buffer lines. In practice
32891 this means that continuation lines count as well when calculating the
32892 relative number of a line.
32894 Lisp programs can disable display of a line number of a particular
32895 buffer line by putting the `display-line-numbers-disable' text property
32896 or overlay property on the first visible character of that line. */);
32897 Vdisplay_line_numbers = Qnil;
32898 DEFSYM (Qdisplay_line_numbers, "display-line-numbers");
32899 Fmake_variable_buffer_local (Qdisplay_line_numbers);
32900 DEFSYM (Qrelative, "relative");
32901 DEFSYM (Qvisual, "visual");
32903 DEFVAR_LISP ("display-line-numbers-width", Vdisplay_line_numbers_width,
32904 doc: /* Minimum width of space reserved for line number display.
32905 A positive number means reserve that many columns for line numbers,
32906 even if the actual number needs less space.
32907 The default value of nil means compute the space dynamically.
32908 Any other value is treated as nil. */);
32909 Vdisplay_line_numbers_width = Qnil;
32910 DEFSYM (Qdisplay_line_numbers_width, "display-line-numbers-width");
32911 Fmake_variable_buffer_local (Qdisplay_line_numbers_width);
32913 DEFVAR_LISP ("display-line-numbers-current-absolute",
32914 Vdisplay_line_numbers_current_absolute,
32915 doc: /* Non-nil means display absolute number of current line.
32916 This variable has effect only when `display-line-numbers' is
32917 either `relative' or `visual'. */);
32918 Vdisplay_line_numbers_current_absolute = Qt;
32920 DEFVAR_BOOL ("display-line-numbers-widen", display_line_numbers_widen,
32921 doc: /* Non-nil means display line numbers disregarding any narrowing. */);
32922 display_line_numbers_widen = false;
32923 DEFSYM (Qdisplay_line_numbers_widen, "display-line-numbers-widen");
32924 Fmake_variable_buffer_local (Qdisplay_line_numbers_widen);
32926 DEFVAR_BOOL ("inhibit-eval-during-redisplay", inhibit_eval_during_redisplay,
32927 doc: /* Non-nil means don't eval Lisp during redisplay. */);
32928 inhibit_eval_during_redisplay = false;
32930 DEFVAR_BOOL ("inhibit-free-realized-faces", inhibit_free_realized_faces,
32931 doc: /* Non-nil means don't free realized faces. Internal use only. */);
32932 inhibit_free_realized_faces = false;
32934 DEFVAR_BOOL ("inhibit-bidi-mirroring", inhibit_bidi_mirroring,
32935 doc: /* Non-nil means don't mirror characters even when bidi context requires that.
32936 Intended for use during debugging and for testing bidi display;
32937 see biditest.el in the test suite. */);
32938 inhibit_bidi_mirroring = false;
32940 #ifdef GLYPH_DEBUG
32941 DEFVAR_BOOL ("inhibit-try-window-id", inhibit_try_window_id,
32942 doc: /* Inhibit try_window_id display optimization. */);
32943 inhibit_try_window_id = false;
32945 DEFVAR_BOOL ("inhibit-try-window-reusing", inhibit_try_window_reusing,
32946 doc: /* Inhibit try_window_reusing display optimization. */);
32947 inhibit_try_window_reusing = false;
32949 DEFVAR_BOOL ("inhibit-try-cursor-movement", inhibit_try_cursor_movement,
32950 doc: /* Inhibit try_cursor_movement display optimization. */);
32951 inhibit_try_cursor_movement = false;
32952 #endif /* GLYPH_DEBUG */
32954 DEFVAR_INT ("overline-margin", overline_margin,
32955 doc: /* Space between overline and text, in pixels.
32956 The default value is 2: the height of the overline (1 pixel) plus 1 pixel
32957 margin to the character height. */);
32958 overline_margin = 2;
32960 DEFVAR_INT ("underline-minimum-offset",
32961 underline_minimum_offset,
32962 doc: /* Minimum distance between baseline and underline.
32963 This can improve legibility of underlined text at small font sizes,
32964 particularly when using variable `x-use-underline-position-properties'
32965 with fonts that specify an UNDERLINE_POSITION relatively close to the
32966 baseline. The default value is 1. */);
32967 underline_minimum_offset = 1;
32969 DEFVAR_BOOL ("display-hourglass", display_hourglass_p,
32970 doc: /* Non-nil means show an hourglass pointer, when Emacs is busy.
32971 This feature only works when on a window system that can change
32972 cursor shapes. */);
32973 display_hourglass_p = true;
32975 DEFVAR_LISP ("hourglass-delay", Vhourglass_delay,
32976 doc: /* Seconds to wait before displaying an hourglass pointer when Emacs is busy. */);
32977 Vhourglass_delay = make_number (DEFAULT_HOURGLASS_DELAY);
32979 #ifdef HAVE_WINDOW_SYSTEM
32980 hourglass_atimer = NULL;
32981 hourglass_shown_p = false;
32982 #endif /* HAVE_WINDOW_SYSTEM */
32984 /* Name of the face used to display glyphless characters. */
32985 DEFSYM (Qglyphless_char, "glyphless-char");
32987 /* Method symbols for Vglyphless_char_display. */
32988 DEFSYM (Qhex_code, "hex-code");
32989 DEFSYM (Qempty_box, "empty-box");
32990 DEFSYM (Qthin_space, "thin-space");
32991 DEFSYM (Qzero_width, "zero-width");
32993 DEFVAR_LISP ("pre-redisplay-function", Vpre_redisplay_function,
32994 doc: /* Function run just before redisplay.
32995 It is called with one argument, which is the set of windows that are to
32996 be redisplayed. This set can be nil (meaning, only the selected window),
32997 or t (meaning all windows). */);
32998 Vpre_redisplay_function = intern ("ignore");
33000 /* Symbol for the purpose of Vglyphless_char_display. */
33001 DEFSYM (Qglyphless_char_display, "glyphless-char-display");
33002 Fput (Qglyphless_char_display, Qchar_table_extra_slots, make_number (1));
33004 DEFVAR_LISP ("glyphless-char-display", Vglyphless_char_display,
33005 doc: /* Char-table defining glyphless characters.
33006 Each element, if non-nil, should be one of the following:
33007 an ASCII acronym string: display this string in a box
33008 `hex-code': display the hexadecimal code of a character in a box
33009 `empty-box': display as an empty box
33010 `thin-space': display as 1-pixel width space
33011 `zero-width': don't display
33012 An element may also be a cons cell (GRAPHICAL . TEXT), which specifies the
33013 display method for graphical terminals and text terminals respectively.
33014 GRAPHICAL and TEXT should each have one of the values listed above.
33016 The char-table has one extra slot to control the display of a character for
33017 which no font is found. This slot only takes effect on graphical terminals.
33018 Its value should be an ASCII acronym string, `hex-code', `empty-box', or
33019 `thin-space'. The default is `empty-box'.
33021 If a character has a non-nil entry in an active display table, the
33022 display table takes effect; in this case, Emacs does not consult
33023 `glyphless-char-display' at all. */);
33024 Vglyphless_char_display = Fmake_char_table (Qglyphless_char_display, Qnil);
33025 Fset_char_table_extra_slot (Vglyphless_char_display, make_number (0),
33026 Qempty_box);
33028 DEFVAR_LISP ("debug-on-message", Vdebug_on_message,
33029 doc: /* If non-nil, debug if a message matching this regexp is displayed. */);
33030 Vdebug_on_message = Qnil;
33032 DEFVAR_LISP ("redisplay--all-windows-cause", Vredisplay__all_windows_cause,
33033 doc: /* */);
33034 Vredisplay__all_windows_cause = Fmake_hash_table (0, NULL);
33036 DEFVAR_LISP ("redisplay--mode-lines-cause", Vredisplay__mode_lines_cause,
33037 doc: /* */);
33038 Vredisplay__mode_lines_cause = Fmake_hash_table (0, NULL);
33040 DEFVAR_BOOL ("redisplay--inhibit-bidi", redisplay__inhibit_bidi,
33041 doc: /* Non-nil means it is not safe to attempt bidi reordering for display. */);
33042 /* Initialize to t, since we need to disable reordering until
33043 loadup.el successfully loads charprop.el. */
33044 redisplay__inhibit_bidi = true;
33046 DEFVAR_BOOL ("display-raw-bytes-as-hex", display_raw_bytes_as_hex,
33047 doc: /* Non-nil means display raw bytes in hexadecimal format.
33048 The default is to use octal format (\200) whereas hexadecimal (\x80)
33049 may be more familiar to users. */);
33050 display_raw_bytes_as_hex = false;
33055 /* Initialize this module when Emacs starts. */
33057 void
33058 init_xdisp (void)
33060 CHARPOS (this_line_start_pos) = 0;
33062 if (!noninteractive)
33064 struct window *m = XWINDOW (minibuf_window);
33065 Lisp_Object frame = m->frame;
33066 struct frame *f = XFRAME (frame);
33067 Lisp_Object root = FRAME_ROOT_WINDOW (f);
33068 struct window *r = XWINDOW (root);
33069 int i;
33071 echo_area_window = minibuf_window;
33073 r->top_line = FRAME_TOP_MARGIN (f);
33074 r->pixel_top = r->top_line * FRAME_LINE_HEIGHT (f);
33075 r->total_cols = FRAME_COLS (f);
33076 r->pixel_width = r->total_cols * FRAME_COLUMN_WIDTH (f);
33077 r->total_lines = FRAME_TOTAL_LINES (f) - 1 - FRAME_TOP_MARGIN (f);
33078 r->pixel_height = r->total_lines * FRAME_LINE_HEIGHT (f);
33080 m->top_line = FRAME_TOTAL_LINES (f) - 1;
33081 m->pixel_top = m->top_line * FRAME_LINE_HEIGHT (f);
33082 m->total_cols = FRAME_COLS (f);
33083 m->pixel_width = m->total_cols * FRAME_COLUMN_WIDTH (f);
33084 m->total_lines = 1;
33085 m->pixel_height = m->total_lines * FRAME_LINE_HEIGHT (f);
33087 scratch_glyph_row.glyphs[TEXT_AREA] = scratch_glyphs;
33088 scratch_glyph_row.glyphs[TEXT_AREA + 1]
33089 = scratch_glyphs + MAX_SCRATCH_GLYPHS;
33091 /* The default ellipsis glyphs `...'. */
33092 for (i = 0; i < 3; ++i)
33093 default_invis_vector[i] = make_number ('.');
33097 /* Allocate the buffer for frame titles.
33098 Also used for `format-mode-line'. */
33099 int size = 100;
33100 mode_line_noprop_buf = xmalloc (size);
33101 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
33102 mode_line_noprop_ptr = mode_line_noprop_buf;
33103 mode_line_target = MODE_LINE_DISPLAY;
33106 help_echo_showing_p = false;
33109 #ifdef HAVE_WINDOW_SYSTEM
33111 /* Platform-independent portion of hourglass implementation. */
33113 /* Timer function of hourglass_atimer. */
33115 static void
33116 show_hourglass (struct atimer *timer)
33118 /* The timer implementation will cancel this timer automatically
33119 after this function has run. Set hourglass_atimer to null
33120 so that we know the timer doesn't have to be canceled. */
33121 hourglass_atimer = NULL;
33123 if (!hourglass_shown_p)
33125 Lisp_Object tail, frame;
33127 block_input ();
33129 FOR_EACH_FRAME (tail, frame)
33131 struct frame *f = XFRAME (frame);
33133 if (FRAME_LIVE_P (f) && FRAME_WINDOW_P (f)
33134 && FRAME_RIF (f)->show_hourglass)
33135 FRAME_RIF (f)->show_hourglass (f);
33138 hourglass_shown_p = true;
33139 unblock_input ();
33143 /* Cancel a currently active hourglass timer, and start a new one. */
33145 void
33146 start_hourglass (void)
33148 struct timespec delay;
33150 cancel_hourglass ();
33152 if (INTEGERP (Vhourglass_delay)
33153 && XINT (Vhourglass_delay) > 0)
33154 delay = make_timespec (min (XINT (Vhourglass_delay),
33155 TYPE_MAXIMUM (time_t)),
33157 else if (FLOATP (Vhourglass_delay)
33158 && XFLOAT_DATA (Vhourglass_delay) > 0)
33159 delay = dtotimespec (XFLOAT_DATA (Vhourglass_delay));
33160 else
33161 delay = make_timespec (DEFAULT_HOURGLASS_DELAY, 0);
33163 hourglass_atimer = start_atimer (ATIMER_RELATIVE, delay,
33164 show_hourglass, NULL);
33167 /* Cancel the hourglass cursor timer if active, hide a busy cursor if
33168 shown. */
33170 void
33171 cancel_hourglass (void)
33173 if (hourglass_atimer)
33175 cancel_atimer (hourglass_atimer);
33176 hourglass_atimer = NULL;
33179 if (hourglass_shown_p)
33181 Lisp_Object tail, frame;
33183 block_input ();
33185 FOR_EACH_FRAME (tail, frame)
33187 struct frame *f = XFRAME (frame);
33189 if (FRAME_LIVE_P (f) && FRAME_WINDOW_P (f)
33190 && FRAME_RIF (f)->hide_hourglass)
33191 FRAME_RIF (f)->hide_hourglass (f);
33192 #ifdef HAVE_NTGUI
33193 /* No cursors on non GUI frames - restore to stock arrow cursor. */
33194 else if (!FRAME_W32_P (f))
33195 w32_arrow_cursor ();
33196 #endif
33199 hourglass_shown_p = false;
33200 unblock_input ();
33204 #endif /* HAVE_WINDOW_SYSTEM */