emacs-lisp/package.el (package-menu-execute): Delete packages in order.
[emacs.git] / src / xdisp.c
blob5c3e641fdfe211d67e4da5c5a490479c7c08eefc
1 /* Display generation from window structure and buffer text.
3 Copyright (C) 1985-1988, 1993-1995, 1997-2015 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
11 (at your option) any later version.
13 GNU Emacs is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
21 /* New redisplay written by Gerd Moellmann <gerd@gnu.org>.
23 Redisplay.
25 Emacs separates the task of updating the display from code
26 modifying global state, e.g. buffer text. This way functions
27 operating on buffers don't also have to be concerned with updating
28 the display.
30 Updating the display is triggered by the Lisp interpreter when it
31 decides it's time to do it. This is done either automatically for
32 you as part of the interpreter's command loop or as the result of
33 calling Lisp functions like `sit-for'. The C function `redisplay'
34 in xdisp.c is the only entry into the inner redisplay code.
36 The following diagram shows how redisplay code is invoked. As you
37 can see, Lisp calls redisplay and vice versa. Under window systems
38 like X, some portions of the redisplay code are also called
39 asynchronously during mouse movement or expose events. It is very
40 important that these code parts do NOT use the C library (malloc,
41 free) because many C libraries under Unix are not reentrant. They
42 may also NOT call functions of the Lisp interpreter which could
43 change the interpreter's state. If you don't follow these rules,
44 you will encounter bugs which are very hard to explain.
46 +--------------+ redisplay +----------------+
47 | Lisp machine |---------------->| Redisplay code |<--+
48 +--------------+ (xdisp.c) +----------------+ |
49 ^ | |
50 +----------------------------------+ |
51 Don't use this path when called |
52 asynchronously! |
54 expose_window (asynchronous) |
56 X expose events -----+
58 What does redisplay do? Obviously, it has to figure out somehow what
59 has been changed since the last time the display has been updated,
60 and to make these changes visible. Preferably it would do that in
61 a moderately intelligent way, i.e. fast.
63 Changes in buffer text can be deduced from window and buffer
64 structures, and from some global variables like `beg_unchanged' and
65 `end_unchanged'. The contents of the display are additionally
66 recorded in a `glyph matrix', a two-dimensional matrix of glyph
67 structures. Each row in such a matrix corresponds to a line on the
68 display, and each glyph in a row corresponds to a column displaying
69 a character, an image, or what else. This matrix is called the
70 `current glyph matrix' or `current matrix' in redisplay
71 terminology.
73 For buffer parts that have been changed since the last update, a
74 second glyph matrix is constructed, the so called `desired glyph
75 matrix' or short `desired matrix'. Current and desired matrix are
76 then compared to find a cheap way to update the display, e.g. by
77 reusing part of the display by scrolling lines.
79 You will find a lot of redisplay optimizations when you start
80 looking at the innards of redisplay. The overall goal of all these
81 optimizations is to make redisplay fast because it is done
82 frequently. Some of these optimizations are implemented by the
83 following functions:
85 . try_cursor_movement
87 This function tries to update the display if the text in the
88 window did not change and did not scroll, only point moved, and
89 it did not move off the displayed portion of the text.
91 . try_window_reusing_current_matrix
93 This function reuses the current matrix of a window when text
94 has not changed, but the window start changed (e.g., due to
95 scrolling).
97 . try_window_id
99 This function attempts to redisplay a window by reusing parts of
100 its existing display. It finds and reuses the part that was not
101 changed, and redraws the rest. (The "id" part in the function's
102 name stands for "insert/delete", not for "identification" or
103 somesuch.)
105 . try_window
107 This function performs the full redisplay of a single window
108 assuming that its fonts were not changed and that the cursor
109 will not end up in the scroll margins. (Loading fonts requires
110 re-adjustment of dimensions of glyph matrices, which makes this
111 method impossible to use.)
113 These optimizations are tried in sequence (some can be skipped if
114 it is known that they are not applicable). If none of the
115 optimizations were successful, redisplay calls redisplay_windows,
116 which performs a full redisplay of all windows.
118 Note that there's one more important optimization up Emacs's
119 sleeve, but it is related to actually redrawing the potentially
120 changed portions of the window/frame, not to reproducing the
121 desired matrices of those potentially changed portions. Namely,
122 the function update_frame and its subroutines, which you will find
123 in dispnew.c, compare the desired matrices with the current
124 matrices, and only redraw the portions that changed. So it could
125 happen that the functions in this file for some reason decide that
126 the entire desired matrix needs to be regenerated from scratch, and
127 still only parts of the Emacs display, or even nothing at all, will
128 be actually delivered to the glass, because update_frame has found
129 that the new and the old screen contents are similar or identical.
131 Desired matrices.
133 Desired matrices are always built per Emacs window. The function
134 `display_line' is the central function to look at if you are
135 interested. It constructs one row in a desired matrix given an
136 iterator structure containing both a buffer position and a
137 description of the environment in which the text is to be
138 displayed. But this is too early, read on.
140 Characters and pixmaps displayed for a range of buffer text depend
141 on various settings of buffers and windows, on overlays and text
142 properties, on display tables, on selective display. The good news
143 is that all this hairy stuff is hidden behind a small set of
144 interface functions taking an iterator structure (struct it)
145 argument.
147 Iteration over things to be displayed is then simple. It is
148 started by initializing an iterator with a call to init_iterator,
149 passing it the buffer position where to start iteration. For
150 iteration over strings, pass -1 as the position to init_iterator,
151 and call reseat_to_string when the string is ready, to initialize
152 the iterator for that string. Thereafter, calls to
153 get_next_display_element fill the iterator structure with relevant
154 information about the next thing to display. Calls to
155 set_iterator_to_next move the iterator to the next thing.
157 Besides this, an iterator also contains information about the
158 display environment in which glyphs for display elements are to be
159 produced. It has fields for the width and height of the display,
160 the information whether long lines are truncated or continued, a
161 current X and Y position, and lots of other stuff you can better
162 see in dispextern.h.
164 Glyphs in a desired matrix are normally constructed in a loop
165 calling get_next_display_element and then PRODUCE_GLYPHS. The call
166 to PRODUCE_GLYPHS will fill the iterator structure with pixel
167 information about the element being displayed and at the same time
168 produce glyphs for it. If the display element fits on the line
169 being displayed, set_iterator_to_next is called next, otherwise the
170 glyphs produced are discarded. The function display_line is the
171 workhorse of filling glyph rows in the desired matrix with glyphs.
172 In addition to producing glyphs, it also handles line truncation
173 and continuation, word wrap, and cursor positioning (for the
174 latter, see also set_cursor_from_row).
176 Frame matrices.
178 That just couldn't be all, could it? What about terminal types not
179 supporting operations on sub-windows of the screen? To update the
180 display on such a terminal, window-based glyph matrices are not
181 well suited. To be able to reuse part of the display (scrolling
182 lines up and down), we must instead have a view of the whole
183 screen. This is what `frame matrices' are for. They are a trick.
185 Frames on terminals like above have a glyph pool. Windows on such
186 a frame sub-allocate their glyph memory from their frame's glyph
187 pool. The frame itself is given its own glyph matrices. By
188 coincidence---or maybe something else---rows in window glyph
189 matrices are slices of corresponding rows in frame matrices. Thus
190 writing to window matrices implicitly updates a frame matrix which
191 provides us with the view of the whole screen that we originally
192 wanted to have without having to move many bytes around. To be
193 honest, there is a little bit more done, but not much more. If you
194 plan to extend that code, take a look at dispnew.c. The function
195 build_frame_matrix is a good starting point.
197 Bidirectional display.
199 Bidirectional display adds quite some hair to this already complex
200 design. The good news are that a large portion of that hairy stuff
201 is hidden in bidi.c behind only 3 interfaces. bidi.c implements a
202 reordering engine which is called by set_iterator_to_next and
203 returns the next character to display in the visual order. See
204 commentary on bidi.c for more details. As far as redisplay is
205 concerned, the effect of calling bidi_move_to_visually_next, the
206 main interface of the reordering engine, is that the iterator gets
207 magically placed on the buffer or string position that is to be
208 displayed next. In other words, a linear iteration through the
209 buffer/string is replaced with a non-linear one. All the rest of
210 the redisplay is oblivious to the bidi reordering.
212 Well, almost oblivious---there are still complications, most of
213 them due to the fact that buffer and string positions no longer
214 change monotonously with glyph indices in a glyph row. Moreover,
215 for continued lines, the buffer positions may not even be
216 monotonously changing with vertical positions. Also, accounting
217 for face changes, overlays, etc. becomes more complex because
218 non-linear iteration could potentially skip many positions with
219 changes, and then cross them again on the way back...
221 One other prominent effect of bidirectional display is that some
222 paragraphs of text need to be displayed starting at the right
223 margin of the window---the so-called right-to-left, or R2L
224 paragraphs. R2L paragraphs are displayed with R2L glyph rows,
225 which have their reversed_p flag set. The bidi reordering engine
226 produces characters in such rows starting from the character which
227 should be the rightmost on display. PRODUCE_GLYPHS then reverses
228 the order, when it fills up the glyph row whose reversed_p flag is
229 set, by prepending each new glyph to what is already there, instead
230 of appending it. When the glyph row is complete, the function
231 extend_face_to_end_of_line fills the empty space to the left of the
232 leftmost character with special glyphs, which will display as,
233 well, empty. On text terminals, these special glyphs are simply
234 blank characters. On graphics terminals, there's a single stretch
235 glyph of a suitably computed width. Both the blanks and the
236 stretch glyph are given the face of the background of the line.
237 This way, the terminal-specific back-end can still draw the glyphs
238 left to right, even for R2L lines.
240 Bidirectional display and character compositions
242 Some scripts cannot be displayed by drawing each character
243 individually, because adjacent characters change each other's shape
244 on display. For example, Arabic and Indic scripts belong to this
245 category.
247 Emacs display supports this by providing "character compositions",
248 most of which is implemented in composite.c. During the buffer
249 scan that delivers characters to PRODUCE_GLYPHS, if the next
250 character to be delivered is a composed character, the iteration
251 calls composition_reseat_it and next_element_from_composition. If
252 they succeed to compose the character with one or more of the
253 following characters, the whole sequence of characters that where
254 composed is recorded in the `struct composition_it' object that is
255 part of the buffer iterator. The composed sequence could produce
256 one or more font glyphs (called "grapheme clusters") on the screen.
257 Each of these grapheme clusters is then delivered to PRODUCE_GLYPHS
258 in the direction corresponding to the current bidi scan direction
259 (recorded in the scan_dir member of the `struct bidi_it' object
260 that is part of the buffer iterator). In particular, if the bidi
261 iterator currently scans the buffer backwards, the grapheme
262 clusters are delivered back to front. This reorders the grapheme
263 clusters as appropriate for the current bidi context. Note that
264 this means that the grapheme clusters are always stored in the
265 LGSTRING object (see composite.c) in the logical order.
267 Moving an iterator in bidirectional text
268 without producing glyphs
270 Note one important detail mentioned above: that the bidi reordering
271 engine, driven by the iterator, produces characters in R2L rows
272 starting at the character that will be the rightmost on display.
273 As far as the iterator is concerned, the geometry of such rows is
274 still left to right, i.e. the iterator "thinks" the first character
275 is at the leftmost pixel position. The iterator does not know that
276 PRODUCE_GLYPHS reverses the order of the glyphs that the iterator
277 delivers. This is important when functions from the move_it_*
278 family are used to get to certain screen position or to match
279 screen coordinates with buffer coordinates: these functions use the
280 iterator geometry, which is left to right even in R2L paragraphs.
281 This works well with most callers of move_it_*, because they need
282 to get to a specific column, and columns are still numbered in the
283 reading order, i.e. the rightmost character in a R2L paragraph is
284 still column zero. But some callers do not get well with this; a
285 notable example is mouse clicks that need to find the character
286 that corresponds to certain pixel coordinates. See
287 buffer_posn_from_coords in dispnew.c for how this is handled. */
289 #include <config.h>
290 #include <stdio.h>
291 #include <limits.h>
293 #include "lisp.h"
294 #include "atimer.h"
295 #include "keyboard.h"
296 #include "frame.h"
297 #include "window.h"
298 #include "termchar.h"
299 #include "dispextern.h"
300 #include "character.h"
301 #include "buffer.h"
302 #include "charset.h"
303 #include "indent.h"
304 #include "commands.h"
305 #include "keymap.h"
306 #include "macros.h"
307 #include "disptab.h"
308 #include "termhooks.h"
309 #include "termopts.h"
310 #include "intervals.h"
311 #include "coding.h"
312 #include "process.h"
313 #include "region-cache.h"
314 #include "font.h"
315 #include "fontset.h"
316 #include "blockinput.h"
317 #ifdef HAVE_WINDOW_SYSTEM
318 #include TERM_HEADER
319 #endif /* HAVE_WINDOW_SYSTEM */
321 #ifndef FRAME_X_OUTPUT
322 #define FRAME_X_OUTPUT(f) ((f)->output_data.x)
323 #endif
325 #define INFINITY 10000000
327 /* Holds the list (error). */
328 static Lisp_Object list_of_error;
330 #ifdef HAVE_WINDOW_SYSTEM
332 /* Test if overflow newline into fringe. Called with iterator IT
333 at or past right window margin, and with IT->current_x set. */
335 #define IT_OVERFLOW_NEWLINE_INTO_FRINGE(IT) \
336 (!NILP (Voverflow_newline_into_fringe) \
337 && FRAME_WINDOW_P ((IT)->f) \
338 && ((IT)->bidi_it.paragraph_dir == R2L \
339 ? (WINDOW_LEFT_FRINGE_WIDTH ((IT)->w) > 0) \
340 : (WINDOW_RIGHT_FRINGE_WIDTH ((IT)->w) > 0)) \
341 && (IT)->current_x == (IT)->last_visible_x)
343 #else /* !HAVE_WINDOW_SYSTEM */
344 #define IT_OVERFLOW_NEWLINE_INTO_FRINGE(it) 0
345 #endif /* HAVE_WINDOW_SYSTEM */
347 /* Test if the display element loaded in IT, or the underlying buffer
348 or string character, is a space or a TAB character. This is used
349 to determine where word wrapping can occur. */
351 #define IT_DISPLAYING_WHITESPACE(it) \
352 ((it->what == IT_CHARACTER && (it->c == ' ' || it->c == '\t')) \
353 || ((STRINGP (it->string) \
354 && (SREF (it->string, IT_STRING_BYTEPOS (*it)) == ' ' \
355 || SREF (it->string, IT_STRING_BYTEPOS (*it)) == '\t')) \
356 || (it->s \
357 && (it->s[IT_BYTEPOS (*it)] == ' ' \
358 || it->s[IT_BYTEPOS (*it)] == '\t')) \
359 || (IT_BYTEPOS (*it) < ZV_BYTE \
360 && (*BYTE_POS_ADDR (IT_BYTEPOS (*it)) == ' ' \
361 || *BYTE_POS_ADDR (IT_BYTEPOS (*it)) == '\t')))) \
363 /* Non-zero means print newline to stdout before next mini-buffer
364 message. */
366 bool noninteractive_need_newline;
368 /* Non-zero means print newline to message log before next message. */
370 static bool message_log_need_newline;
372 /* Three markers that message_dolog uses.
373 It could allocate them itself, but that causes trouble
374 in handling memory-full errors. */
375 static Lisp_Object message_dolog_marker1;
376 static Lisp_Object message_dolog_marker2;
377 static Lisp_Object message_dolog_marker3;
379 /* The buffer position of the first character appearing entirely or
380 partially on the line of the selected window which contains the
381 cursor; <= 0 if not known. Set by set_cursor_from_row, used for
382 redisplay optimization in redisplay_internal. */
384 static struct text_pos this_line_start_pos;
386 /* Number of characters past the end of the line above, including the
387 terminating newline. */
389 static struct text_pos this_line_end_pos;
391 /* The vertical positions and the height of this line. */
393 static int this_line_vpos;
394 static int this_line_y;
395 static int this_line_pixel_height;
397 /* X position at which this display line starts. Usually zero;
398 negative if first character is partially visible. */
400 static int this_line_start_x;
402 /* The smallest character position seen by move_it_* functions as they
403 move across display lines. Used to set MATRIX_ROW_START_CHARPOS of
404 hscrolled lines, see display_line. */
406 static struct text_pos this_line_min_pos;
408 /* Buffer that this_line_.* variables are referring to. */
410 static struct buffer *this_line_buffer;
412 /* Nonzero if an overlay arrow has been displayed in this window. */
414 static bool overlay_arrow_seen;
416 /* Vector containing glyphs for an ellipsis `...'. */
418 static Lisp_Object default_invis_vector[3];
420 /* This is the window where the echo area message was displayed. It
421 is always a mini-buffer window, but it may not be the same window
422 currently active as a mini-buffer. */
424 Lisp_Object echo_area_window;
426 /* List of pairs (MESSAGE . MULTIBYTE). The function save_message
427 pushes the current message and the value of
428 message_enable_multibyte on the stack, the function restore_message
429 pops the stack and displays MESSAGE again. */
431 static Lisp_Object Vmessage_stack;
433 /* Nonzero means multibyte characters were enabled when the echo area
434 message was specified. */
436 static bool message_enable_multibyte;
438 /* Nonzero if we should redraw the mode lines on the next redisplay.
439 If it has value REDISPLAY_SOME, then only redisplay the mode lines where
440 the `redisplay' bit has been set. Otherwise, redisplay all mode lines
441 (the number used is then only used to track down the cause for this
442 full-redisplay). */
444 int update_mode_lines;
446 /* Nonzero if window sizes or contents other than selected-window have changed
447 since last redisplay that finished.
448 If it has value REDISPLAY_SOME, then only redisplay the windows where
449 the `redisplay' bit has been set. Otherwise, redisplay all windows
450 (the number used is then only used to track down the cause for this
451 full-redisplay). */
453 int windows_or_buffers_changed;
455 /* Nonzero after display_mode_line if %l was used and it displayed a
456 line number. */
458 static bool line_number_displayed;
460 /* The name of the *Messages* buffer, a string. */
462 static Lisp_Object Vmessages_buffer_name;
464 /* Current, index 0, and last displayed echo area message. Either
465 buffers from echo_buffers, or nil to indicate no message. */
467 Lisp_Object echo_area_buffer[2];
469 /* The buffers referenced from echo_area_buffer. */
471 static Lisp_Object echo_buffer[2];
473 /* A vector saved used in with_area_buffer to reduce consing. */
475 static Lisp_Object Vwith_echo_area_save_vector;
477 /* Non-zero means display_echo_area should display the last echo area
478 message again. Set by redisplay_preserve_echo_area. */
480 static bool display_last_displayed_message_p;
482 /* Nonzero if echo area is being used by print; zero if being used by
483 message. */
485 static bool message_buf_print;
487 /* Set to 1 in clear_message to make redisplay_internal aware
488 of an emptied echo area. */
490 static bool message_cleared_p;
492 /* A scratch glyph row with contents used for generating truncation
493 glyphs. Also used in direct_output_for_insert. */
495 #define MAX_SCRATCH_GLYPHS 100
496 static struct glyph_row scratch_glyph_row;
497 static struct glyph scratch_glyphs[MAX_SCRATCH_GLYPHS];
499 /* Ascent and height of the last line processed by move_it_to. */
501 static int last_height;
503 /* Non-zero if there's a help-echo in the echo area. */
505 bool help_echo_showing_p;
507 /* The maximum distance to look ahead for text properties. Values
508 that are too small let us call compute_char_face and similar
509 functions too often which is expensive. Values that are too large
510 let us call compute_char_face and alike too often because we
511 might not be interested in text properties that far away. */
513 #define TEXT_PROP_DISTANCE_LIMIT 100
515 /* SAVE_IT and RESTORE_IT are called when we save a snapshot of the
516 iterator state and later restore it. This is needed because the
517 bidi iterator on bidi.c keeps a stacked cache of its states, which
518 is really a singleton. When we use scratch iterator objects to
519 move around the buffer, we can cause the bidi cache to be pushed or
520 popped, and therefore we need to restore the cache state when we
521 return to the original iterator. */
522 #define SAVE_IT(ITCOPY,ITORIG,CACHE) \
523 do { \
524 if (CACHE) \
525 bidi_unshelve_cache (CACHE, 1); \
526 ITCOPY = ITORIG; \
527 CACHE = bidi_shelve_cache (); \
528 } while (0)
530 #define RESTORE_IT(pITORIG,pITCOPY,CACHE) \
531 do { \
532 if (pITORIG != pITCOPY) \
533 *(pITORIG) = *(pITCOPY); \
534 bidi_unshelve_cache (CACHE, 0); \
535 CACHE = NULL; \
536 } while (0)
538 /* Functions to mark elements as needing redisplay. */
539 enum { REDISPLAY_SOME = 2}; /* Arbitrary choice. */
541 void
542 redisplay_other_windows (void)
544 if (!windows_or_buffers_changed)
545 windows_or_buffers_changed = REDISPLAY_SOME;
548 void
549 wset_redisplay (struct window *w)
551 /* Beware: selected_window can be nil during early stages. */
552 if (!EQ (make_lisp_ptr (w, Lisp_Vectorlike), selected_window))
553 redisplay_other_windows ();
554 w->redisplay = true;
557 void
558 fset_redisplay (struct frame *f)
560 redisplay_other_windows ();
561 f->redisplay = true;
564 void
565 bset_redisplay (struct buffer *b)
567 int count = buffer_window_count (b);
568 if (count > 0)
570 /* ... it's visible in other window than selected, */
571 if (count > 1 || b != XBUFFER (XWINDOW (selected_window)->contents))
572 redisplay_other_windows ();
573 /* Even if we don't set windows_or_buffers_changed, do set `redisplay'
574 so that if we later set windows_or_buffers_changed, this buffer will
575 not be omitted. */
576 b->text->redisplay = true;
580 void
581 bset_update_mode_line (struct buffer *b)
583 if (!update_mode_lines)
584 update_mode_lines = REDISPLAY_SOME;
585 b->text->redisplay = true;
588 #ifdef GLYPH_DEBUG
590 /* Non-zero means print traces of redisplay if compiled with
591 GLYPH_DEBUG defined. */
593 bool trace_redisplay_p;
595 #endif /* GLYPH_DEBUG */
597 #ifdef DEBUG_TRACE_MOVE
598 /* Non-zero means trace with TRACE_MOVE to stderr. */
599 int trace_move;
601 #define TRACE_MOVE(x) if (trace_move) fprintf x; else (void) 0
602 #else
603 #define TRACE_MOVE(x) (void) 0
604 #endif
606 /* Buffer being redisplayed -- for redisplay_window_error. */
608 static struct buffer *displayed_buffer;
610 /* Value returned from text property handlers (see below). */
612 enum prop_handled
614 HANDLED_NORMALLY,
615 HANDLED_RECOMPUTE_PROPS,
616 HANDLED_OVERLAY_STRING_CONSUMED,
617 HANDLED_RETURN
620 /* A description of text properties that redisplay is interested
621 in. */
623 struct props
625 /* The symbol index of the name of the property. */
626 short name;
628 /* A unique index for the property. */
629 enum prop_idx idx;
631 /* A handler function called to set up iterator IT from the property
632 at IT's current position. Value is used to steer handle_stop. */
633 enum prop_handled (*handler) (struct it *it);
636 static enum prop_handled handle_face_prop (struct it *);
637 static enum prop_handled handle_invisible_prop (struct it *);
638 static enum prop_handled handle_display_prop (struct it *);
639 static enum prop_handled handle_composition_prop (struct it *);
640 static enum prop_handled handle_overlay_change (struct it *);
641 static enum prop_handled handle_fontified_prop (struct it *);
643 /* Properties handled by iterators. */
645 static struct props it_props[] =
647 {SYMBOL_INDEX (Qfontified), FONTIFIED_PROP_IDX, handle_fontified_prop},
648 /* Handle `face' before `display' because some sub-properties of
649 `display' need to know the face. */
650 {SYMBOL_INDEX (Qface), FACE_PROP_IDX, handle_face_prop},
651 {SYMBOL_INDEX (Qdisplay), DISPLAY_PROP_IDX, handle_display_prop},
652 {SYMBOL_INDEX (Qinvisible), INVISIBLE_PROP_IDX, handle_invisible_prop},
653 {SYMBOL_INDEX (Qcomposition), COMPOSITION_PROP_IDX, handle_composition_prop},
654 {0, 0, NULL}
657 /* Value is the position described by X. If X is a marker, value is
658 the marker_position of X. Otherwise, value is X. */
660 #define COERCE_MARKER(X) (MARKERP ((X)) ? Fmarker_position (X) : (X))
662 /* Enumeration returned by some move_it_.* functions internally. */
664 enum move_it_result
666 /* Not used. Undefined value. */
667 MOVE_UNDEFINED,
669 /* Move ended at the requested buffer position or ZV. */
670 MOVE_POS_MATCH_OR_ZV,
672 /* Move ended at the requested X pixel position. */
673 MOVE_X_REACHED,
675 /* Move within a line ended at the end of a line that must be
676 continued. */
677 MOVE_LINE_CONTINUED,
679 /* Move within a line ended at the end of a line that would
680 be displayed truncated. */
681 MOVE_LINE_TRUNCATED,
683 /* Move within a line ended at a line end. */
684 MOVE_NEWLINE_OR_CR
687 /* This counter is used to clear the face cache every once in a while
688 in redisplay_internal. It is incremented for each redisplay.
689 Every CLEAR_FACE_CACHE_COUNT full redisplays, the face cache is
690 cleared. */
692 #define CLEAR_FACE_CACHE_COUNT 500
693 static int clear_face_cache_count;
695 /* Similarly for the image cache. */
697 #ifdef HAVE_WINDOW_SYSTEM
698 #define CLEAR_IMAGE_CACHE_COUNT 101
699 static int clear_image_cache_count;
701 /* Null glyph slice */
702 static struct glyph_slice null_glyph_slice = { 0, 0, 0, 0 };
703 #endif
705 /* True while redisplay_internal is in progress. */
707 bool redisplaying_p;
709 /* If a string, XTread_socket generates an event to display that string.
710 (The display is done in read_char.) */
712 Lisp_Object help_echo_string;
713 Lisp_Object help_echo_window;
714 Lisp_Object help_echo_object;
715 ptrdiff_t help_echo_pos;
717 /* Temporary variable for XTread_socket. */
719 Lisp_Object previous_help_echo_string;
721 /* Platform-independent portion of hourglass implementation. */
723 #ifdef HAVE_WINDOW_SYSTEM
725 /* Non-zero means an hourglass cursor is currently shown. */
726 static bool hourglass_shown_p;
728 /* If non-null, an asynchronous timer that, when it expires, displays
729 an hourglass cursor on all frames. */
730 static struct atimer *hourglass_atimer;
732 #endif /* HAVE_WINDOW_SYSTEM */
734 /* Default number of seconds to wait before displaying an hourglass
735 cursor. */
736 #define DEFAULT_HOURGLASS_DELAY 1
738 #ifdef HAVE_WINDOW_SYSTEM
740 /* Default pixel width of `thin-space' display method. */
741 #define THIN_SPACE_WIDTH 1
743 #endif /* HAVE_WINDOW_SYSTEM */
745 /* Function prototypes. */
747 static void setup_for_ellipsis (struct it *, int);
748 static void set_iterator_to_next (struct it *, int);
749 static void mark_window_display_accurate_1 (struct window *, int);
750 static int single_display_spec_string_p (Lisp_Object, Lisp_Object);
751 static int display_prop_string_p (Lisp_Object, Lisp_Object);
752 static int row_for_charpos_p (struct glyph_row *, ptrdiff_t);
753 static int cursor_row_p (struct glyph_row *);
754 static int redisplay_mode_lines (Lisp_Object, bool);
755 static char *decode_mode_spec_coding (Lisp_Object, char *, int);
757 static Lisp_Object get_it_property (struct it *it, Lisp_Object prop);
759 static void handle_line_prefix (struct it *);
761 static void pint2str (char *, int, ptrdiff_t);
762 static void pint2hrstr (char *, int, ptrdiff_t);
763 static struct text_pos run_window_scroll_functions (Lisp_Object,
764 struct text_pos);
765 static int text_outside_line_unchanged_p (struct window *,
766 ptrdiff_t, ptrdiff_t);
767 static void store_mode_line_noprop_char (char);
768 static int store_mode_line_noprop (const char *, int, int);
769 static void handle_stop (struct it *);
770 static void handle_stop_backwards (struct it *, ptrdiff_t);
771 static void vmessage (const char *, va_list) ATTRIBUTE_FORMAT_PRINTF (1, 0);
772 static void ensure_echo_area_buffers (void);
773 static void unwind_with_echo_area_buffer (Lisp_Object);
774 static Lisp_Object with_echo_area_buffer_unwind_data (struct window *);
775 static int with_echo_area_buffer (struct window *, int,
776 int (*) (ptrdiff_t, Lisp_Object),
777 ptrdiff_t, Lisp_Object);
778 static void clear_garbaged_frames (void);
779 static int current_message_1 (ptrdiff_t, Lisp_Object);
780 static int truncate_message_1 (ptrdiff_t, Lisp_Object);
781 static void set_message (Lisp_Object);
782 static int set_message_1 (ptrdiff_t, Lisp_Object);
783 static int display_echo_area (struct window *);
784 static int display_echo_area_1 (ptrdiff_t, Lisp_Object);
785 static int resize_mini_window_1 (ptrdiff_t, Lisp_Object);
786 static void unwind_redisplay (void);
787 static int string_char_and_length (const unsigned char *, int *);
788 static struct text_pos display_prop_end (struct it *, Lisp_Object,
789 struct text_pos);
790 static int compute_window_start_on_continuation_line (struct window *);
791 static void insert_left_trunc_glyphs (struct it *);
792 static struct glyph_row *get_overlay_arrow_glyph_row (struct window *,
793 Lisp_Object);
794 static void extend_face_to_end_of_line (struct it *);
795 static int append_space_for_newline (struct it *, int);
796 static int cursor_row_fully_visible_p (struct window *, int, int);
797 static int try_scrolling (Lisp_Object, int, ptrdiff_t, ptrdiff_t, int, int);
798 static int try_cursor_movement (Lisp_Object, struct text_pos, int *);
799 static int trailing_whitespace_p (ptrdiff_t);
800 static intmax_t message_log_check_duplicate (ptrdiff_t, ptrdiff_t);
801 static void push_it (struct it *, struct text_pos *);
802 static void iterate_out_of_display_property (struct it *);
803 static void pop_it (struct it *);
804 static void sync_frame_with_window_matrix_rows (struct window *);
805 static void redisplay_internal (void);
806 static bool echo_area_display (bool);
807 static void redisplay_windows (Lisp_Object);
808 static void redisplay_window (Lisp_Object, bool);
809 static Lisp_Object redisplay_window_error (Lisp_Object);
810 static Lisp_Object redisplay_window_0 (Lisp_Object);
811 static Lisp_Object redisplay_window_1 (Lisp_Object);
812 static int set_cursor_from_row (struct window *, struct glyph_row *,
813 struct glyph_matrix *, ptrdiff_t, ptrdiff_t,
814 int, int);
815 static int update_menu_bar (struct frame *, int, int);
816 static int try_window_reusing_current_matrix (struct window *);
817 static int try_window_id (struct window *);
818 static int display_line (struct it *);
819 static int display_mode_lines (struct window *);
820 static int display_mode_line (struct window *, enum face_id, Lisp_Object);
821 static int display_mode_element (struct it *, int, int, int, Lisp_Object, Lisp_Object, int);
822 static int store_mode_line_string (const char *, Lisp_Object, int, int, int, Lisp_Object);
823 static const char *decode_mode_spec (struct window *, int, int, Lisp_Object *);
824 static void display_menu_bar (struct window *);
825 static ptrdiff_t display_count_lines (ptrdiff_t, ptrdiff_t, ptrdiff_t,
826 ptrdiff_t *);
827 static int display_string (const char *, Lisp_Object, Lisp_Object,
828 ptrdiff_t, ptrdiff_t, struct it *, int, int, int, int);
829 static void compute_line_metrics (struct it *);
830 static void run_redisplay_end_trigger_hook (struct it *);
831 static int get_overlay_strings (struct it *, ptrdiff_t);
832 static int get_overlay_strings_1 (struct it *, ptrdiff_t, int);
833 static void next_overlay_string (struct it *);
834 static void reseat (struct it *, struct text_pos, int);
835 static void reseat_1 (struct it *, struct text_pos, int);
836 static void back_to_previous_visible_line_start (struct it *);
837 static void reseat_at_next_visible_line_start (struct it *, int);
838 static int next_element_from_ellipsis (struct it *);
839 static int next_element_from_display_vector (struct it *);
840 static int next_element_from_string (struct it *);
841 static int next_element_from_c_string (struct it *);
842 static int next_element_from_buffer (struct it *);
843 static int next_element_from_composition (struct it *);
844 static int next_element_from_image (struct it *);
845 static int next_element_from_stretch (struct it *);
846 static void load_overlay_strings (struct it *, ptrdiff_t);
847 static int init_from_display_pos (struct it *, struct window *,
848 struct display_pos *);
849 static void reseat_to_string (struct it *, const char *,
850 Lisp_Object, ptrdiff_t, ptrdiff_t, int, int);
851 static int get_next_display_element (struct it *);
852 static enum move_it_result
853 move_it_in_display_line_to (struct it *, ptrdiff_t, int,
854 enum move_operation_enum);
855 static void get_visually_first_element (struct it *);
856 static void init_to_row_start (struct it *, struct window *,
857 struct glyph_row *);
858 static int init_to_row_end (struct it *, struct window *,
859 struct glyph_row *);
860 static void back_to_previous_line_start (struct it *);
861 static int forward_to_next_line_start (struct it *, int *, struct bidi_it *);
862 static struct text_pos string_pos_nchars_ahead (struct text_pos,
863 Lisp_Object, ptrdiff_t);
864 static struct text_pos string_pos (ptrdiff_t, Lisp_Object);
865 static struct text_pos c_string_pos (ptrdiff_t, const char *, bool);
866 static ptrdiff_t number_of_chars (const char *, bool);
867 static void compute_stop_pos (struct it *);
868 static void compute_string_pos (struct text_pos *, struct text_pos,
869 Lisp_Object);
870 static int face_before_or_after_it_pos (struct it *, int);
871 static ptrdiff_t next_overlay_change (ptrdiff_t);
872 static int handle_display_spec (struct it *, Lisp_Object, Lisp_Object,
873 Lisp_Object, struct text_pos *, ptrdiff_t, int);
874 static int handle_single_display_spec (struct it *, Lisp_Object,
875 Lisp_Object, Lisp_Object,
876 struct text_pos *, ptrdiff_t, int, int);
877 static int underlying_face_id (struct it *);
878 static int in_ellipses_for_invisible_text_p (struct display_pos *,
879 struct window *);
881 #define face_before_it_pos(IT) face_before_or_after_it_pos ((IT), 1)
882 #define face_after_it_pos(IT) face_before_or_after_it_pos ((IT), 0)
884 #ifdef HAVE_WINDOW_SYSTEM
886 static void x_consider_frame_title (Lisp_Object);
887 static void update_tool_bar (struct frame *, int);
888 static int redisplay_tool_bar (struct frame *);
889 static void x_draw_bottom_divider (struct window *w);
890 static void notice_overwritten_cursor (struct window *,
891 enum glyph_row_area,
892 int, int, int, int);
893 static void append_stretch_glyph (struct it *, Lisp_Object,
894 int, int, int);
897 #endif /* HAVE_WINDOW_SYSTEM */
899 static void produce_special_glyphs (struct it *, enum display_element_type);
900 static void show_mouse_face (Mouse_HLInfo *, enum draw_glyphs_face);
901 static bool coords_in_mouse_face_p (struct window *, int, int);
905 /***********************************************************************
906 Window display dimensions
907 ***********************************************************************/
909 /* Return the bottom boundary y-position for text lines in window W.
910 This is the first y position at which a line cannot start.
911 It is relative to the top of the window.
913 This is the height of W minus the height of a mode line, if any. */
916 window_text_bottom_y (struct window *w)
918 int height = WINDOW_PIXEL_HEIGHT (w);
920 height -= WINDOW_BOTTOM_DIVIDER_WIDTH (w);
922 if (WINDOW_WANTS_MODELINE_P (w))
923 height -= CURRENT_MODE_LINE_HEIGHT (w);
925 height -= WINDOW_SCROLL_BAR_AREA_HEIGHT (w);
927 return height;
930 /* Return the pixel width of display area AREA of window W.
931 ANY_AREA means return the total width of W, not including
932 fringes to the left and right of the window. */
935 window_box_width (struct window *w, enum glyph_row_area area)
937 int width = w->pixel_width;
939 if (!w->pseudo_window_p)
941 width -= WINDOW_SCROLL_BAR_AREA_WIDTH (w);
942 width -= WINDOW_RIGHT_DIVIDER_WIDTH (w);
944 if (area == TEXT_AREA)
945 width -= (WINDOW_MARGINS_WIDTH (w)
946 + WINDOW_FRINGES_WIDTH (w));
947 else if (area == LEFT_MARGIN_AREA)
948 width = WINDOW_LEFT_MARGIN_WIDTH (w);
949 else if (area == RIGHT_MARGIN_AREA)
950 width = WINDOW_RIGHT_MARGIN_WIDTH (w);
953 /* With wide margins, fringes, etc. we might end up with a negative
954 width, correct that here. */
955 return max (0, width);
959 /* Return the pixel height of the display area of window W, not
960 including mode lines of W, if any. */
963 window_box_height (struct window *w)
965 struct frame *f = XFRAME (w->frame);
966 int height = WINDOW_PIXEL_HEIGHT (w);
968 eassert (height >= 0);
970 height -= WINDOW_BOTTOM_DIVIDER_WIDTH (w);
971 height -= WINDOW_SCROLL_BAR_AREA_HEIGHT (w);
973 /* Note: the code below that determines the mode-line/header-line
974 height is essentially the same as that contained in the macro
975 CURRENT_{MODE,HEADER}_LINE_HEIGHT, except that it checks whether
976 the appropriate glyph row has its `mode_line_p' flag set,
977 and if it doesn't, uses estimate_mode_line_height instead. */
979 if (WINDOW_WANTS_MODELINE_P (w))
981 struct glyph_row *ml_row
982 = (w->current_matrix && w->current_matrix->rows
983 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
984 : 0);
985 if (ml_row && ml_row->mode_line_p)
986 height -= ml_row->height;
987 else
988 height -= estimate_mode_line_height (f, CURRENT_MODE_LINE_FACE_ID (w));
991 if (WINDOW_WANTS_HEADER_LINE_P (w))
993 struct glyph_row *hl_row
994 = (w->current_matrix && w->current_matrix->rows
995 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
996 : 0);
997 if (hl_row && hl_row->mode_line_p)
998 height -= hl_row->height;
999 else
1000 height -= estimate_mode_line_height (f, HEADER_LINE_FACE_ID);
1003 /* With a very small font and a mode-line that's taller than
1004 default, we might end up with a negative height. */
1005 return max (0, height);
1008 /* Return the window-relative coordinate of the left edge of display
1009 area AREA of window W. ANY_AREA means return the left edge of the
1010 whole window, to the right of the left fringe of W. */
1013 window_box_left_offset (struct window *w, enum glyph_row_area area)
1015 int x;
1017 if (w->pseudo_window_p)
1018 return 0;
1020 x = WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
1022 if (area == TEXT_AREA)
1023 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1024 + window_box_width (w, LEFT_MARGIN_AREA));
1025 else if (area == RIGHT_MARGIN_AREA)
1026 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1027 + window_box_width (w, LEFT_MARGIN_AREA)
1028 + window_box_width (w, TEXT_AREA)
1029 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
1031 : WINDOW_RIGHT_FRINGE_WIDTH (w)));
1032 else if (area == LEFT_MARGIN_AREA
1033 && WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w))
1034 x += WINDOW_LEFT_FRINGE_WIDTH (w);
1036 /* Don't return more than the window's pixel width. */
1037 return min (x, w->pixel_width);
1041 /* Return the window-relative coordinate of the right edge of display
1042 area AREA of window W. ANY_AREA means return the right edge of the
1043 whole window, to the left of the right fringe of W. */
1045 static int
1046 window_box_right_offset (struct window *w, enum glyph_row_area area)
1048 /* Don't return more than the window's pixel width. */
1049 return min (window_box_left_offset (w, area) + window_box_width (w, area),
1050 w->pixel_width);
1053 /* Return the frame-relative coordinate of the left edge of display
1054 area AREA of window W. ANY_AREA means return the left edge of the
1055 whole window, to the right of the left fringe of W. */
1058 window_box_left (struct window *w, enum glyph_row_area area)
1060 struct frame *f = XFRAME (w->frame);
1061 int x;
1063 if (w->pseudo_window_p)
1064 return FRAME_INTERNAL_BORDER_WIDTH (f);
1066 x = (WINDOW_LEFT_EDGE_X (w)
1067 + window_box_left_offset (w, area));
1069 return x;
1073 /* Return the frame-relative coordinate of the right edge of display
1074 area AREA of window W. ANY_AREA means return the right edge of the
1075 whole window, to the left of the right fringe of W. */
1078 window_box_right (struct window *w, enum glyph_row_area area)
1080 return window_box_left (w, area) + window_box_width (w, area);
1083 /* Get the bounding box of the display area AREA of window W, without
1084 mode lines, in frame-relative coordinates. ANY_AREA means the
1085 whole window, not including the left and right fringes of
1086 the window. Return in *BOX_X and *BOX_Y the frame-relative pixel
1087 coordinates of the upper-left corner of the box. Return in
1088 *BOX_WIDTH, and *BOX_HEIGHT the pixel width and height of the box. */
1090 void
1091 window_box (struct window *w, enum glyph_row_area area, int *box_x,
1092 int *box_y, int *box_width, int *box_height)
1094 if (box_width)
1095 *box_width = window_box_width (w, area);
1096 if (box_height)
1097 *box_height = window_box_height (w);
1098 if (box_x)
1099 *box_x = window_box_left (w, area);
1100 if (box_y)
1102 *box_y = WINDOW_TOP_EDGE_Y (w);
1103 if (WINDOW_WANTS_HEADER_LINE_P (w))
1104 *box_y += CURRENT_HEADER_LINE_HEIGHT (w);
1108 #ifdef HAVE_WINDOW_SYSTEM
1110 /* Get the bounding box of the display area AREA of window W, without
1111 mode lines and both fringes of the window. Return in *TOP_LEFT_X
1112 and TOP_LEFT_Y the frame-relative pixel coordinates of the
1113 upper-left corner of the box. Return in *BOTTOM_RIGHT_X, and
1114 *BOTTOM_RIGHT_Y the coordinates of the bottom-right corner of the
1115 box. */
1117 static void
1118 window_box_edges (struct window *w, int *top_left_x, int *top_left_y,
1119 int *bottom_right_x, int *bottom_right_y)
1121 window_box (w, ANY_AREA, top_left_x, top_left_y,
1122 bottom_right_x, bottom_right_y);
1123 *bottom_right_x += *top_left_x;
1124 *bottom_right_y += *top_left_y;
1127 #endif /* HAVE_WINDOW_SYSTEM */
1129 /***********************************************************************
1130 Utilities
1131 ***********************************************************************/
1133 /* Return the bottom y-position of the line the iterator IT is in.
1134 This can modify IT's settings. */
1137 line_bottom_y (struct it *it)
1139 int line_height = it->max_ascent + it->max_descent;
1140 int line_top_y = it->current_y;
1142 if (line_height == 0)
1144 if (last_height)
1145 line_height = last_height;
1146 else if (IT_CHARPOS (*it) < ZV)
1148 move_it_by_lines (it, 1);
1149 line_height = (it->max_ascent || it->max_descent
1150 ? it->max_ascent + it->max_descent
1151 : last_height);
1153 else
1155 struct glyph_row *row = it->glyph_row;
1157 /* Use the default character height. */
1158 it->glyph_row = NULL;
1159 it->what = IT_CHARACTER;
1160 it->c = ' ';
1161 it->len = 1;
1162 PRODUCE_GLYPHS (it);
1163 line_height = it->ascent + it->descent;
1164 it->glyph_row = row;
1168 return line_top_y + line_height;
1171 DEFUN ("line-pixel-height", Fline_pixel_height,
1172 Sline_pixel_height, 0, 0, 0,
1173 doc: /* Return height in pixels of text line in the selected window.
1175 Value is the height in pixels of the line at point. */)
1176 (void)
1178 struct it it;
1179 struct text_pos pt;
1180 struct window *w = XWINDOW (selected_window);
1181 struct buffer *old_buffer = NULL;
1182 Lisp_Object result;
1184 if (XBUFFER (w->contents) != current_buffer)
1186 old_buffer = current_buffer;
1187 set_buffer_internal_1 (XBUFFER (w->contents));
1189 SET_TEXT_POS (pt, PT, PT_BYTE);
1190 start_display (&it, w, pt);
1191 it.vpos = it.current_y = 0;
1192 last_height = 0;
1193 result = make_number (line_bottom_y (&it));
1194 if (old_buffer)
1195 set_buffer_internal_1 (old_buffer);
1197 return result;
1200 /* Return the default pixel height of text lines in window W. The
1201 value is the canonical height of the W frame's default font, plus
1202 any extra space required by the line-spacing variable or frame
1203 parameter.
1205 Implementation note: this ignores any line-spacing text properties
1206 put on the newline characters. This is because those properties
1207 only affect the _screen_ line ending in the newline (i.e., in a
1208 continued line, only the last screen line will be affected), which
1209 means only a small number of lines in a buffer can ever use this
1210 feature. Since this function is used to compute the default pixel
1211 equivalent of text lines in a window, we can safely ignore those
1212 few lines. For the same reasons, we ignore the line-height
1213 properties. */
1215 default_line_pixel_height (struct window *w)
1217 struct frame *f = WINDOW_XFRAME (w);
1218 int height = FRAME_LINE_HEIGHT (f);
1220 if (!FRAME_INITIAL_P (f) && BUFFERP (w->contents))
1222 struct buffer *b = XBUFFER (w->contents);
1223 Lisp_Object val = BVAR (b, extra_line_spacing);
1225 if (NILP (val))
1226 val = BVAR (&buffer_defaults, extra_line_spacing);
1227 if (!NILP (val))
1229 if (RANGED_INTEGERP (0, val, INT_MAX))
1230 height += XFASTINT (val);
1231 else if (FLOATP (val))
1233 int addon = XFLOAT_DATA (val) * height + 0.5;
1235 if (addon >= 0)
1236 height += addon;
1239 else
1240 height += f->extra_line_spacing;
1243 return height;
1246 /* Subroutine of pos_visible_p below. Extracts a display string, if
1247 any, from the display spec given as its argument. */
1248 static Lisp_Object
1249 string_from_display_spec (Lisp_Object spec)
1251 if (CONSP (spec))
1253 while (CONSP (spec))
1255 if (STRINGP (XCAR (spec)))
1256 return XCAR (spec);
1257 spec = XCDR (spec);
1260 else if (VECTORP (spec))
1262 ptrdiff_t i;
1264 for (i = 0; i < ASIZE (spec); i++)
1266 if (STRINGP (AREF (spec, i)))
1267 return AREF (spec, i);
1269 return Qnil;
1272 return spec;
1276 /* Limit insanely large values of W->hscroll on frame F to the largest
1277 value that will still prevent first_visible_x and last_visible_x of
1278 'struct it' from overflowing an int. */
1279 static int
1280 window_hscroll_limited (struct window *w, struct frame *f)
1282 ptrdiff_t window_hscroll = w->hscroll;
1283 int window_text_width = window_box_width (w, TEXT_AREA);
1284 int colwidth = FRAME_COLUMN_WIDTH (f);
1286 if (window_hscroll > (INT_MAX - window_text_width) / colwidth - 1)
1287 window_hscroll = (INT_MAX - window_text_width) / colwidth - 1;
1289 return window_hscroll;
1292 /* Return 1 if position CHARPOS is visible in window W.
1293 CHARPOS < 0 means return info about WINDOW_END position.
1294 If visible, set *X and *Y to pixel coordinates of top left corner.
1295 Set *RTOP and *RBOT to pixel height of an invisible area of glyph at POS.
1296 Set *ROWH and *VPOS to row's visible height and VPOS (row number). */
1299 pos_visible_p (struct window *w, ptrdiff_t charpos, int *x, int *y,
1300 int *rtop, int *rbot, int *rowh, int *vpos)
1302 struct it it;
1303 void *itdata = bidi_shelve_cache ();
1304 struct text_pos top;
1305 int visible_p = 0;
1306 struct buffer *old_buffer = NULL;
1307 bool r2l = false;
1309 if (FRAME_INITIAL_P (XFRAME (WINDOW_FRAME (w))))
1310 return visible_p;
1312 if (XBUFFER (w->contents) != current_buffer)
1314 old_buffer = current_buffer;
1315 set_buffer_internal_1 (XBUFFER (w->contents));
1318 SET_TEXT_POS_FROM_MARKER (top, w->start);
1319 /* Scrolling a minibuffer window via scroll bar when the echo area
1320 shows long text sometimes resets the minibuffer contents behind
1321 our backs. */
1322 if (CHARPOS (top) > ZV)
1323 SET_TEXT_POS (top, BEGV, BEGV_BYTE);
1325 /* Compute exact mode line heights. */
1326 if (WINDOW_WANTS_MODELINE_P (w))
1327 w->mode_line_height
1328 = display_mode_line (w, CURRENT_MODE_LINE_FACE_ID (w),
1329 BVAR (current_buffer, mode_line_format));
1331 if (WINDOW_WANTS_HEADER_LINE_P (w))
1332 w->header_line_height
1333 = display_mode_line (w, HEADER_LINE_FACE_ID,
1334 BVAR (current_buffer, header_line_format));
1336 start_display (&it, w, top);
1337 move_it_to (&it, charpos, -1, it.last_visible_y - 1, -1,
1338 (charpos >= 0 ? MOVE_TO_POS : 0) | MOVE_TO_Y);
1340 if (charpos >= 0
1341 && (((!it.bidi_p || it.bidi_it.scan_dir != -1)
1342 && IT_CHARPOS (it) >= charpos)
1343 /* When scanning backwards under bidi iteration, move_it_to
1344 stops at or _before_ CHARPOS, because it stops at or to
1345 the _right_ of the character at CHARPOS. */
1346 || (it.bidi_p && it.bidi_it.scan_dir == -1
1347 && IT_CHARPOS (it) <= charpos)))
1349 /* We have reached CHARPOS, or passed it. How the call to
1350 move_it_to can overshoot: (i) If CHARPOS is on invisible text
1351 or covered by a display property, move_it_to stops at the end
1352 of the invisible text, to the right of CHARPOS. (ii) If
1353 CHARPOS is in a display vector, move_it_to stops on its last
1354 glyph. */
1355 int top_x = it.current_x;
1356 int top_y = it.current_y;
1357 int window_top_y = WINDOW_HEADER_LINE_HEIGHT (w);
1358 int bottom_y;
1359 struct it save_it;
1360 void *save_it_data = NULL;
1362 /* Calling line_bottom_y may change it.method, it.position, etc. */
1363 SAVE_IT (save_it, it, save_it_data);
1364 last_height = 0;
1365 bottom_y = line_bottom_y (&it);
1366 if (top_y < window_top_y)
1367 visible_p = bottom_y > window_top_y;
1368 else if (top_y < it.last_visible_y)
1369 visible_p = 1;
1370 if (bottom_y >= it.last_visible_y
1371 && it.bidi_p && it.bidi_it.scan_dir == -1
1372 && IT_CHARPOS (it) < charpos)
1374 /* When the last line of the window is scanned backwards
1375 under bidi iteration, we could be duped into thinking
1376 that we have passed CHARPOS, when in fact move_it_to
1377 simply stopped short of CHARPOS because it reached
1378 last_visible_y. To see if that's what happened, we call
1379 move_it_to again with a slightly larger vertical limit,
1380 and see if it actually moved vertically; if it did, we
1381 didn't really reach CHARPOS, which is beyond window end. */
1382 /* Why 10? because we don't know how many canonical lines
1383 will the height of the next line(s) be. So we guess. */
1384 int ten_more_lines = 10 * default_line_pixel_height (w);
1386 move_it_to (&it, charpos, -1, bottom_y + ten_more_lines, -1,
1387 MOVE_TO_POS | MOVE_TO_Y);
1388 if (it.current_y > top_y)
1389 visible_p = 0;
1392 RESTORE_IT (&it, &save_it, save_it_data);
1393 if (visible_p)
1395 if (it.method == GET_FROM_DISPLAY_VECTOR)
1397 /* We stopped on the last glyph of a display vector.
1398 Try and recompute. Hack alert! */
1399 if (charpos < 2 || top.charpos >= charpos)
1400 top_x = it.glyph_row->x;
1401 else
1403 struct it it2, it2_prev;
1404 /* The idea is to get to the previous buffer
1405 position, consume the character there, and use
1406 the pixel coordinates we get after that. But if
1407 the previous buffer position is also displayed
1408 from a display vector, we need to consume all of
1409 the glyphs from that display vector. */
1410 start_display (&it2, w, top);
1411 move_it_to (&it2, charpos - 1, -1, -1, -1, MOVE_TO_POS);
1412 /* If we didn't get to CHARPOS - 1, there's some
1413 replacing display property at that position, and
1414 we stopped after it. That is exactly the place
1415 whose coordinates we want. */
1416 if (IT_CHARPOS (it2) != charpos - 1)
1417 it2_prev = it2;
1418 else
1420 /* Iterate until we get out of the display
1421 vector that displays the character at
1422 CHARPOS - 1. */
1423 do {
1424 get_next_display_element (&it2);
1425 PRODUCE_GLYPHS (&it2);
1426 it2_prev = it2;
1427 set_iterator_to_next (&it2, 1);
1428 } while (it2.method == GET_FROM_DISPLAY_VECTOR
1429 && IT_CHARPOS (it2) < charpos);
1431 if (ITERATOR_AT_END_OF_LINE_P (&it2_prev)
1432 || it2_prev.current_x > it2_prev.last_visible_x)
1433 top_x = it.glyph_row->x;
1434 else
1436 top_x = it2_prev.current_x;
1437 top_y = it2_prev.current_y;
1441 else if (IT_CHARPOS (it) != charpos)
1443 Lisp_Object cpos = make_number (charpos);
1444 Lisp_Object spec = Fget_char_property (cpos, Qdisplay, Qnil);
1445 Lisp_Object string = string_from_display_spec (spec);
1446 struct text_pos tpos;
1447 int replacing_spec_p;
1448 bool newline_in_string
1449 = (STRINGP (string)
1450 && memchr (SDATA (string), '\n', SBYTES (string)));
1452 SET_TEXT_POS (tpos, charpos, CHAR_TO_BYTE (charpos));
1453 replacing_spec_p
1454 = (!NILP (spec)
1455 && handle_display_spec (NULL, spec, Qnil, Qnil, &tpos,
1456 charpos, FRAME_WINDOW_P (it.f)));
1457 /* The tricky code below is needed because there's a
1458 discrepancy between move_it_to and how we set cursor
1459 when PT is at the beginning of a portion of text
1460 covered by a display property or an overlay with a
1461 display property, or the display line ends in a
1462 newline from a display string. move_it_to will stop
1463 _after_ such display strings, whereas
1464 set_cursor_from_row conspires with cursor_row_p to
1465 place the cursor on the first glyph produced from the
1466 display string. */
1468 /* We have overshoot PT because it is covered by a
1469 display property that replaces the text it covers.
1470 If the string includes embedded newlines, we are also
1471 in the wrong display line. Backtrack to the correct
1472 line, where the display property begins. */
1473 if (replacing_spec_p)
1475 Lisp_Object startpos, endpos;
1476 EMACS_INT start, end;
1477 struct it it3;
1478 int it3_moved;
1480 /* Find the first and the last buffer positions
1481 covered by the display string. */
1482 endpos =
1483 Fnext_single_char_property_change (cpos, Qdisplay,
1484 Qnil, Qnil);
1485 startpos =
1486 Fprevious_single_char_property_change (endpos, Qdisplay,
1487 Qnil, Qnil);
1488 start = XFASTINT (startpos);
1489 end = XFASTINT (endpos);
1490 /* Move to the last buffer position before the
1491 display property. */
1492 start_display (&it3, w, top);
1493 if (start > CHARPOS (top))
1494 move_it_to (&it3, start - 1, -1, -1, -1, MOVE_TO_POS);
1495 /* Move forward one more line if the position before
1496 the display string is a newline or if it is the
1497 rightmost character on a line that is
1498 continued or word-wrapped. */
1499 if (it3.method == GET_FROM_BUFFER
1500 && (it3.c == '\n'
1501 || FETCH_BYTE (IT_BYTEPOS (it3)) == '\n'))
1502 move_it_by_lines (&it3, 1);
1503 else if (move_it_in_display_line_to (&it3, -1,
1504 it3.current_x
1505 + it3.pixel_width,
1506 MOVE_TO_X)
1507 == MOVE_LINE_CONTINUED)
1509 move_it_by_lines (&it3, 1);
1510 /* When we are under word-wrap, the #$@%!
1511 move_it_by_lines moves 2 lines, so we need to
1512 fix that up. */
1513 if (it3.line_wrap == WORD_WRAP)
1514 move_it_by_lines (&it3, -1);
1517 /* Record the vertical coordinate of the display
1518 line where we wound up. */
1519 top_y = it3.current_y;
1520 if (it3.bidi_p)
1522 /* When characters are reordered for display,
1523 the character displayed to the left of the
1524 display string could be _after_ the display
1525 property in the logical order. Use the
1526 smallest vertical position of these two. */
1527 start_display (&it3, w, top);
1528 move_it_to (&it3, end + 1, -1, -1, -1, MOVE_TO_POS);
1529 if (it3.current_y < top_y)
1530 top_y = it3.current_y;
1532 /* Move from the top of the window to the beginning
1533 of the display line where the display string
1534 begins. */
1535 start_display (&it3, w, top);
1536 move_it_to (&it3, -1, 0, top_y, -1, MOVE_TO_X | MOVE_TO_Y);
1537 /* If it3_moved stays zero after the 'while' loop
1538 below, that means we already were at a newline
1539 before the loop (e.g., the display string begins
1540 with a newline), so we don't need to (and cannot)
1541 inspect the glyphs of it3.glyph_row, because
1542 PRODUCE_GLYPHS will not produce anything for a
1543 newline, and thus it3.glyph_row stays at its
1544 stale content it got at top of the window. */
1545 it3_moved = 0;
1546 /* Finally, advance the iterator until we hit the
1547 first display element whose character position is
1548 CHARPOS, or until the first newline from the
1549 display string, which signals the end of the
1550 display line. */
1551 while (get_next_display_element (&it3))
1553 PRODUCE_GLYPHS (&it3);
1554 if (IT_CHARPOS (it3) == charpos
1555 || ITERATOR_AT_END_OF_LINE_P (&it3))
1556 break;
1557 it3_moved = 1;
1558 set_iterator_to_next (&it3, 0);
1560 top_x = it3.current_x - it3.pixel_width;
1561 /* Normally, we would exit the above loop because we
1562 found the display element whose character
1563 position is CHARPOS. For the contingency that we
1564 didn't, and stopped at the first newline from the
1565 display string, move back over the glyphs
1566 produced from the string, until we find the
1567 rightmost glyph not from the string. */
1568 if (it3_moved
1569 && newline_in_string
1570 && IT_CHARPOS (it3) != charpos && EQ (it3.object, string))
1572 struct glyph *g = it3.glyph_row->glyphs[TEXT_AREA]
1573 + it3.glyph_row->used[TEXT_AREA];
1575 while (EQ ((g - 1)->object, string))
1577 --g;
1578 top_x -= g->pixel_width;
1580 eassert (g < it3.glyph_row->glyphs[TEXT_AREA]
1581 + it3.glyph_row->used[TEXT_AREA]);
1586 *x = top_x;
1587 *y = max (top_y + max (0, it.max_ascent - it.ascent), window_top_y);
1588 *rtop = max (0, window_top_y - top_y);
1589 *rbot = max (0, bottom_y - it.last_visible_y);
1590 *rowh = max (0, (min (bottom_y, it.last_visible_y)
1591 - max (top_y, window_top_y)));
1592 *vpos = it.vpos;
1593 if (it.bidi_it.paragraph_dir == R2L)
1594 r2l = true;
1597 else
1599 /* Either we were asked to provide info about WINDOW_END, or
1600 CHARPOS is in the partially visible glyph row at end of
1601 window. */
1602 struct it it2;
1603 void *it2data = NULL;
1605 SAVE_IT (it2, it, it2data);
1606 if (IT_CHARPOS (it) < ZV && FETCH_BYTE (IT_BYTEPOS (it)) != '\n')
1607 move_it_by_lines (&it, 1);
1608 if (charpos < IT_CHARPOS (it)
1609 || (it.what == IT_EOB && charpos == IT_CHARPOS (it)))
1611 visible_p = true;
1612 RESTORE_IT (&it2, &it2, it2data);
1613 move_it_to (&it2, charpos, -1, -1, -1, MOVE_TO_POS);
1614 *x = it2.current_x;
1615 *y = it2.current_y + it2.max_ascent - it2.ascent;
1616 *rtop = max (0, -it2.current_y);
1617 *rbot = max (0, ((it2.current_y + it2.max_ascent + it2.max_descent)
1618 - it.last_visible_y));
1619 *rowh = max (0, (min (it2.current_y + it2.max_ascent + it2.max_descent,
1620 it.last_visible_y)
1621 - max (it2.current_y,
1622 WINDOW_HEADER_LINE_HEIGHT (w))));
1623 *vpos = it2.vpos;
1624 if (it2.bidi_it.paragraph_dir == R2L)
1625 r2l = true;
1627 else
1628 bidi_unshelve_cache (it2data, 1);
1630 bidi_unshelve_cache (itdata, 0);
1632 if (old_buffer)
1633 set_buffer_internal_1 (old_buffer);
1635 if (visible_p)
1637 if (w->hscroll > 0)
1638 *x -=
1639 window_hscroll_limited (w, WINDOW_XFRAME (w))
1640 * WINDOW_FRAME_COLUMN_WIDTH (w);
1641 /* For lines in an R2L paragraph, we need to mirror the X pixel
1642 coordinate wrt the text area. For the reasons, see the
1643 commentary in buffer_posn_from_coords and the explanation of
1644 the geometry used by the move_it_* functions at the end of
1645 the large commentary near the beginning of this file. */
1646 if (r2l)
1647 *x = window_box_width (w, TEXT_AREA) - *x - 1;
1650 #if 0
1651 /* Debugging code. */
1652 if (visible_p)
1653 fprintf (stderr, "+pv pt=%d vs=%d --> x=%d y=%d rt=%d rb=%d rh=%d vp=%d\n",
1654 charpos, w->vscroll, *x, *y, *rtop, *rbot, *rowh, *vpos);
1655 else
1656 fprintf (stderr, "-pv pt=%d vs=%d\n", charpos, w->vscroll);
1657 #endif
1659 return visible_p;
1663 /* Return the next character from STR. Return in *LEN the length of
1664 the character. This is like STRING_CHAR_AND_LENGTH but never
1665 returns an invalid character. If we find one, we return a `?', but
1666 with the length of the invalid character. */
1668 static int
1669 string_char_and_length (const unsigned char *str, int *len)
1671 int c;
1673 c = STRING_CHAR_AND_LENGTH (str, *len);
1674 if (!CHAR_VALID_P (c))
1675 /* We may not change the length here because other places in Emacs
1676 don't use this function, i.e. they silently accept invalid
1677 characters. */
1678 c = '?';
1680 return c;
1685 /* Given a position POS containing a valid character and byte position
1686 in STRING, return the position NCHARS ahead (NCHARS >= 0). */
1688 static struct text_pos
1689 string_pos_nchars_ahead (struct text_pos pos, Lisp_Object string, ptrdiff_t nchars)
1691 eassert (STRINGP (string) && nchars >= 0);
1693 if (STRING_MULTIBYTE (string))
1695 const unsigned char *p = SDATA (string) + BYTEPOS (pos);
1696 int len;
1698 while (nchars--)
1700 string_char_and_length (p, &len);
1701 p += len;
1702 CHARPOS (pos) += 1;
1703 BYTEPOS (pos) += len;
1706 else
1707 SET_TEXT_POS (pos, CHARPOS (pos) + nchars, BYTEPOS (pos) + nchars);
1709 return pos;
1713 /* Value is the text position, i.e. character and byte position,
1714 for character position CHARPOS in STRING. */
1716 static struct text_pos
1717 string_pos (ptrdiff_t charpos, Lisp_Object string)
1719 struct text_pos pos;
1720 eassert (STRINGP (string));
1721 eassert (charpos >= 0);
1722 SET_TEXT_POS (pos, charpos, string_char_to_byte (string, charpos));
1723 return pos;
1727 /* Value is a text position, i.e. character and byte position, for
1728 character position CHARPOS in C string S. MULTIBYTE_P non-zero
1729 means recognize multibyte characters. */
1731 static struct text_pos
1732 c_string_pos (ptrdiff_t charpos, const char *s, bool multibyte_p)
1734 struct text_pos pos;
1736 eassert (s != NULL);
1737 eassert (charpos >= 0);
1739 if (multibyte_p)
1741 int len;
1743 SET_TEXT_POS (pos, 0, 0);
1744 while (charpos--)
1746 string_char_and_length ((const unsigned char *) s, &len);
1747 s += len;
1748 CHARPOS (pos) += 1;
1749 BYTEPOS (pos) += len;
1752 else
1753 SET_TEXT_POS (pos, charpos, charpos);
1755 return pos;
1759 /* Value is the number of characters in C string S. MULTIBYTE_P
1760 non-zero means recognize multibyte characters. */
1762 static ptrdiff_t
1763 number_of_chars (const char *s, bool multibyte_p)
1765 ptrdiff_t nchars;
1767 if (multibyte_p)
1769 ptrdiff_t rest = strlen (s);
1770 int len;
1771 const unsigned char *p = (const unsigned char *) s;
1773 for (nchars = 0; rest > 0; ++nchars)
1775 string_char_and_length (p, &len);
1776 rest -= len, p += len;
1779 else
1780 nchars = strlen (s);
1782 return nchars;
1786 /* Compute byte position NEWPOS->bytepos corresponding to
1787 NEWPOS->charpos. POS is a known position in string STRING.
1788 NEWPOS->charpos must be >= POS.charpos. */
1790 static void
1791 compute_string_pos (struct text_pos *newpos, struct text_pos pos, Lisp_Object string)
1793 eassert (STRINGP (string));
1794 eassert (CHARPOS (*newpos) >= CHARPOS (pos));
1796 if (STRING_MULTIBYTE (string))
1797 *newpos = string_pos_nchars_ahead (pos, string,
1798 CHARPOS (*newpos) - CHARPOS (pos));
1799 else
1800 BYTEPOS (*newpos) = CHARPOS (*newpos);
1803 /* EXPORT:
1804 Return an estimation of the pixel height of mode or header lines on
1805 frame F. FACE_ID specifies what line's height to estimate. */
1808 estimate_mode_line_height (struct frame *f, enum face_id face_id)
1810 #ifdef HAVE_WINDOW_SYSTEM
1811 if (FRAME_WINDOW_P (f))
1813 int height = FONT_HEIGHT (FRAME_FONT (f));
1815 /* This function is called so early when Emacs starts that the face
1816 cache and mode line face are not yet initialized. */
1817 if (FRAME_FACE_CACHE (f))
1819 struct face *face = FACE_FROM_ID (f, face_id);
1820 if (face)
1822 if (face->font)
1823 height = FONT_HEIGHT (face->font);
1824 if (face->box_line_width > 0)
1825 height += 2 * face->box_line_width;
1829 return height;
1831 #endif
1833 return 1;
1836 /* Given a pixel position (PIX_X, PIX_Y) on frame F, return glyph
1837 co-ordinates in (*X, *Y). Set *BOUNDS to the rectangle that the
1838 glyph at X, Y occupies, if BOUNDS != 0. If NOCLIP is non-zero, do
1839 not force the value into range. */
1841 void
1842 pixel_to_glyph_coords (struct frame *f, register int pix_x, register int pix_y,
1843 int *x, int *y, NativeRectangle *bounds, int noclip)
1846 #ifdef HAVE_WINDOW_SYSTEM
1847 if (FRAME_WINDOW_P (f))
1849 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to round down
1850 even for negative values. */
1851 if (pix_x < 0)
1852 pix_x -= FRAME_COLUMN_WIDTH (f) - 1;
1853 if (pix_y < 0)
1854 pix_y -= FRAME_LINE_HEIGHT (f) - 1;
1856 pix_x = FRAME_PIXEL_X_TO_COL (f, pix_x);
1857 pix_y = FRAME_PIXEL_Y_TO_LINE (f, pix_y);
1859 if (bounds)
1860 STORE_NATIVE_RECT (*bounds,
1861 FRAME_COL_TO_PIXEL_X (f, pix_x),
1862 FRAME_LINE_TO_PIXEL_Y (f, pix_y),
1863 FRAME_COLUMN_WIDTH (f) - 1,
1864 FRAME_LINE_HEIGHT (f) - 1);
1866 /* PXW: Should we clip pixels before converting to columns/lines? */
1867 if (!noclip)
1869 if (pix_x < 0)
1870 pix_x = 0;
1871 else if (pix_x > FRAME_TOTAL_COLS (f))
1872 pix_x = FRAME_TOTAL_COLS (f);
1874 if (pix_y < 0)
1875 pix_y = 0;
1876 else if (pix_y > FRAME_TOTAL_LINES (f))
1877 pix_y = FRAME_TOTAL_LINES (f);
1880 #endif
1882 *x = pix_x;
1883 *y = pix_y;
1887 /* Find the glyph under window-relative coordinates X/Y in window W.
1888 Consider only glyphs from buffer text, i.e. no glyphs from overlay
1889 strings. Return in *HPOS and *VPOS the row and column number of
1890 the glyph found. Return in *AREA the glyph area containing X.
1891 Value is a pointer to the glyph found or null if X/Y is not on
1892 text, or we can't tell because W's current matrix is not up to
1893 date. */
1895 static struct glyph *
1896 x_y_to_hpos_vpos (struct window *w, int x, int y, int *hpos, int *vpos,
1897 int *dx, int *dy, int *area)
1899 struct glyph *glyph, *end;
1900 struct glyph_row *row = NULL;
1901 int x0, i;
1903 /* Find row containing Y. Give up if some row is not enabled. */
1904 for (i = 0; i < w->current_matrix->nrows; ++i)
1906 row = MATRIX_ROW (w->current_matrix, i);
1907 if (!row->enabled_p)
1908 return NULL;
1909 if (y >= row->y && y < MATRIX_ROW_BOTTOM_Y (row))
1910 break;
1913 *vpos = i;
1914 *hpos = 0;
1916 /* Give up if Y is not in the window. */
1917 if (i == w->current_matrix->nrows)
1918 return NULL;
1920 /* Get the glyph area containing X. */
1921 if (w->pseudo_window_p)
1923 *area = TEXT_AREA;
1924 x0 = 0;
1926 else
1928 if (x < window_box_left_offset (w, TEXT_AREA))
1930 *area = LEFT_MARGIN_AREA;
1931 x0 = window_box_left_offset (w, LEFT_MARGIN_AREA);
1933 else if (x < window_box_right_offset (w, TEXT_AREA))
1935 *area = TEXT_AREA;
1936 x0 = window_box_left_offset (w, TEXT_AREA) + min (row->x, 0);
1938 else
1940 *area = RIGHT_MARGIN_AREA;
1941 x0 = window_box_left_offset (w, RIGHT_MARGIN_AREA);
1945 /* Find glyph containing X. */
1946 glyph = row->glyphs[*area];
1947 end = glyph + row->used[*area];
1948 x -= x0;
1949 while (glyph < end && x >= glyph->pixel_width)
1951 x -= glyph->pixel_width;
1952 ++glyph;
1955 if (glyph == end)
1956 return NULL;
1958 if (dx)
1960 *dx = x;
1961 *dy = y - (row->y + row->ascent - glyph->ascent);
1964 *hpos = glyph - row->glyphs[*area];
1965 return glyph;
1968 /* Convert frame-relative x/y to coordinates relative to window W.
1969 Takes pseudo-windows into account. */
1971 static void
1972 frame_to_window_pixel_xy (struct window *w, int *x, int *y)
1974 if (w->pseudo_window_p)
1976 /* A pseudo-window is always full-width, and starts at the
1977 left edge of the frame, plus a frame border. */
1978 struct frame *f = XFRAME (w->frame);
1979 *x -= FRAME_INTERNAL_BORDER_WIDTH (f);
1980 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1982 else
1984 *x -= WINDOW_LEFT_EDGE_X (w);
1985 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1989 #ifdef HAVE_WINDOW_SYSTEM
1991 /* EXPORT:
1992 Return in RECTS[] at most N clipping rectangles for glyph string S.
1993 Return the number of stored rectangles. */
1996 get_glyph_string_clip_rects (struct glyph_string *s, NativeRectangle *rects, int n)
1998 XRectangle r;
2000 if (n <= 0)
2001 return 0;
2003 if (s->row->full_width_p)
2005 /* Draw full-width. X coordinates are relative to S->w->left_col. */
2006 r.x = WINDOW_LEFT_EDGE_X (s->w);
2007 if (s->row->mode_line_p)
2008 r.width = WINDOW_PIXEL_WIDTH (s->w) - WINDOW_RIGHT_DIVIDER_WIDTH (s->w);
2009 else
2010 r.width = WINDOW_PIXEL_WIDTH (s->w);
2012 /* Unless displaying a mode or menu bar line, which are always
2013 fully visible, clip to the visible part of the row. */
2014 if (s->w->pseudo_window_p)
2015 r.height = s->row->visible_height;
2016 else
2017 r.height = s->height;
2019 else
2021 /* This is a text line that may be partially visible. */
2022 r.x = window_box_left (s->w, s->area);
2023 r.width = window_box_width (s->w, s->area);
2024 r.height = s->row->visible_height;
2027 if (s->clip_head)
2028 if (r.x < s->clip_head->x)
2030 if (r.width >= s->clip_head->x - r.x)
2031 r.width -= s->clip_head->x - r.x;
2032 else
2033 r.width = 0;
2034 r.x = s->clip_head->x;
2036 if (s->clip_tail)
2037 if (r.x + r.width > s->clip_tail->x + s->clip_tail->background_width)
2039 if (s->clip_tail->x + s->clip_tail->background_width >= r.x)
2040 r.width = s->clip_tail->x + s->clip_tail->background_width - r.x;
2041 else
2042 r.width = 0;
2045 /* If S draws overlapping rows, it's sufficient to use the top and
2046 bottom of the window for clipping because this glyph string
2047 intentionally draws over other lines. */
2048 if (s->for_overlaps)
2050 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
2051 r.height = window_text_bottom_y (s->w) - r.y;
2053 /* Alas, the above simple strategy does not work for the
2054 environments with anti-aliased text: if the same text is
2055 drawn onto the same place multiple times, it gets thicker.
2056 If the overlap we are processing is for the erased cursor, we
2057 take the intersection with the rectangle of the cursor. */
2058 if (s->for_overlaps & OVERLAPS_ERASED_CURSOR)
2060 XRectangle rc, r_save = r;
2062 rc.x = WINDOW_TEXT_TO_FRAME_PIXEL_X (s->w, s->w->phys_cursor.x);
2063 rc.y = s->w->phys_cursor.y;
2064 rc.width = s->w->phys_cursor_width;
2065 rc.height = s->w->phys_cursor_height;
2067 x_intersect_rectangles (&r_save, &rc, &r);
2070 else
2072 /* Don't use S->y for clipping because it doesn't take partially
2073 visible lines into account. For example, it can be negative for
2074 partially visible lines at the top of a window. */
2075 if (!s->row->full_width_p
2076 && MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (s->w, s->row))
2077 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
2078 else
2079 r.y = max (0, s->row->y);
2082 r.y = WINDOW_TO_FRAME_PIXEL_Y (s->w, r.y);
2084 /* If drawing the cursor, don't let glyph draw outside its
2085 advertised boundaries. Cleartype does this under some circumstances. */
2086 if (s->hl == DRAW_CURSOR)
2088 struct glyph *glyph = s->first_glyph;
2089 int height, max_y;
2091 if (s->x > r.x)
2093 if (r.width >= s->x - r.x)
2094 r.width -= s->x - r.x;
2095 else /* R2L hscrolled row with cursor outside text area */
2096 r.width = 0;
2097 r.x = s->x;
2099 r.width = min (r.width, glyph->pixel_width);
2101 /* If r.y is below window bottom, ensure that we still see a cursor. */
2102 height = min (glyph->ascent + glyph->descent,
2103 min (FRAME_LINE_HEIGHT (s->f), s->row->visible_height));
2104 max_y = window_text_bottom_y (s->w) - height;
2105 max_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, max_y);
2106 if (s->ybase - glyph->ascent > max_y)
2108 r.y = max_y;
2109 r.height = height;
2111 else
2113 /* Don't draw cursor glyph taller than our actual glyph. */
2114 height = max (FRAME_LINE_HEIGHT (s->f), glyph->ascent + glyph->descent);
2115 if (height < r.height)
2117 max_y = r.y + r.height;
2118 r.y = min (max_y, max (r.y, s->ybase + glyph->descent - height));
2119 r.height = min (max_y - r.y, height);
2124 if (s->row->clip)
2126 XRectangle r_save = r;
2128 if (! x_intersect_rectangles (&r_save, s->row->clip, &r))
2129 r.width = 0;
2132 if ((s->for_overlaps & OVERLAPS_BOTH) == 0
2133 || ((s->for_overlaps & OVERLAPS_BOTH) == OVERLAPS_BOTH && n == 1))
2135 #ifdef CONVERT_FROM_XRECT
2136 CONVERT_FROM_XRECT (r, *rects);
2137 #else
2138 *rects = r;
2139 #endif
2140 return 1;
2142 else
2144 /* If we are processing overlapping and allowed to return
2145 multiple clipping rectangles, we exclude the row of the glyph
2146 string from the clipping rectangle. This is to avoid drawing
2147 the same text on the environment with anti-aliasing. */
2148 #ifdef CONVERT_FROM_XRECT
2149 XRectangle rs[2];
2150 #else
2151 XRectangle *rs = rects;
2152 #endif
2153 int i = 0, row_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, s->row->y);
2155 if (s->for_overlaps & OVERLAPS_PRED)
2157 rs[i] = r;
2158 if (r.y + r.height > row_y)
2160 if (r.y < row_y)
2161 rs[i].height = row_y - r.y;
2162 else
2163 rs[i].height = 0;
2165 i++;
2167 if (s->for_overlaps & OVERLAPS_SUCC)
2169 rs[i] = r;
2170 if (r.y < row_y + s->row->visible_height)
2172 if (r.y + r.height > row_y + s->row->visible_height)
2174 rs[i].y = row_y + s->row->visible_height;
2175 rs[i].height = r.y + r.height - rs[i].y;
2177 else
2178 rs[i].height = 0;
2180 i++;
2183 n = i;
2184 #ifdef CONVERT_FROM_XRECT
2185 for (i = 0; i < n; i++)
2186 CONVERT_FROM_XRECT (rs[i], rects[i]);
2187 #endif
2188 return n;
2192 /* EXPORT:
2193 Return in *NR the clipping rectangle for glyph string S. */
2195 void
2196 get_glyph_string_clip_rect (struct glyph_string *s, NativeRectangle *nr)
2198 get_glyph_string_clip_rects (s, nr, 1);
2202 /* EXPORT:
2203 Return the position and height of the phys cursor in window W.
2204 Set w->phys_cursor_width to width of phys cursor.
2207 void
2208 get_phys_cursor_geometry (struct window *w, struct glyph_row *row,
2209 struct glyph *glyph, int *xp, int *yp, int *heightp)
2211 struct frame *f = XFRAME (WINDOW_FRAME (w));
2212 int x, y, wd, h, h0, y0;
2214 /* Compute the width of the rectangle to draw. If on a stretch
2215 glyph, and `x-stretch-block-cursor' is nil, don't draw a
2216 rectangle as wide as the glyph, but use a canonical character
2217 width instead. */
2218 wd = glyph->pixel_width;
2220 x = w->phys_cursor.x;
2221 if (x < 0)
2223 wd += x;
2224 x = 0;
2227 if (glyph->type == STRETCH_GLYPH
2228 && !x_stretch_cursor_p)
2229 wd = min (FRAME_COLUMN_WIDTH (f), wd);
2230 w->phys_cursor_width = wd;
2232 y = w->phys_cursor.y + row->ascent - glyph->ascent;
2234 /* If y is below window bottom, ensure that we still see a cursor. */
2235 h0 = min (FRAME_LINE_HEIGHT (f), row->visible_height);
2237 h = max (h0, glyph->ascent + glyph->descent);
2238 h0 = min (h0, glyph->ascent + glyph->descent);
2240 y0 = WINDOW_HEADER_LINE_HEIGHT (w);
2241 if (y < y0)
2243 h = max (h - (y0 - y) + 1, h0);
2244 y = y0 - 1;
2246 else
2248 y0 = window_text_bottom_y (w) - h0;
2249 if (y > y0)
2251 h += y - y0;
2252 y = y0;
2256 *xp = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, x);
2257 *yp = WINDOW_TO_FRAME_PIXEL_Y (w, y);
2258 *heightp = h;
2262 * Remember which glyph the mouse is over.
2265 void
2266 remember_mouse_glyph (struct frame *f, int gx, int gy, NativeRectangle *rect)
2268 Lisp_Object window;
2269 struct window *w;
2270 struct glyph_row *r, *gr, *end_row;
2271 enum window_part part;
2272 enum glyph_row_area area;
2273 int x, y, width, height;
2275 /* Try to determine frame pixel position and size of the glyph under
2276 frame pixel coordinates X/Y on frame F. */
2278 if (window_resize_pixelwise)
2280 width = height = 1;
2281 goto virtual_glyph;
2283 else if (!f->glyphs_initialized_p
2284 || (window = window_from_coordinates (f, gx, gy, &part, 0),
2285 NILP (window)))
2287 width = FRAME_SMALLEST_CHAR_WIDTH (f);
2288 height = FRAME_SMALLEST_FONT_HEIGHT (f);
2289 goto virtual_glyph;
2292 w = XWINDOW (window);
2293 width = WINDOW_FRAME_COLUMN_WIDTH (w);
2294 height = WINDOW_FRAME_LINE_HEIGHT (w);
2296 x = window_relative_x_coord (w, part, gx);
2297 y = gy - WINDOW_TOP_EDGE_Y (w);
2299 r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
2300 end_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
2302 if (w->pseudo_window_p)
2304 area = TEXT_AREA;
2305 part = ON_MODE_LINE; /* Don't adjust margin. */
2306 goto text_glyph;
2309 switch (part)
2311 case ON_LEFT_MARGIN:
2312 area = LEFT_MARGIN_AREA;
2313 goto text_glyph;
2315 case ON_RIGHT_MARGIN:
2316 area = RIGHT_MARGIN_AREA;
2317 goto text_glyph;
2319 case ON_HEADER_LINE:
2320 case ON_MODE_LINE:
2321 gr = (part == ON_HEADER_LINE
2322 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
2323 : MATRIX_MODE_LINE_ROW (w->current_matrix));
2324 gy = gr->y;
2325 area = TEXT_AREA;
2326 goto text_glyph_row_found;
2328 case ON_TEXT:
2329 area = TEXT_AREA;
2331 text_glyph:
2332 gr = 0; gy = 0;
2333 for (; r <= end_row && r->enabled_p; ++r)
2334 if (r->y + r->height > y)
2336 gr = r; gy = r->y;
2337 break;
2340 text_glyph_row_found:
2341 if (gr && gy <= y)
2343 struct glyph *g = gr->glyphs[area];
2344 struct glyph *end = g + gr->used[area];
2346 height = gr->height;
2347 for (gx = gr->x; g < end; gx += g->pixel_width, ++g)
2348 if (gx + g->pixel_width > x)
2349 break;
2351 if (g < end)
2353 if (g->type == IMAGE_GLYPH)
2355 /* Don't remember when mouse is over image, as
2356 image may have hot-spots. */
2357 STORE_NATIVE_RECT (*rect, 0, 0, 0, 0);
2358 return;
2360 width = g->pixel_width;
2362 else
2364 /* Use nominal char spacing at end of line. */
2365 x -= gx;
2366 gx += (x / width) * width;
2369 if (part != ON_MODE_LINE && part != ON_HEADER_LINE)
2371 gx += window_box_left_offset (w, area);
2372 /* Don't expand over the modeline to make sure the vertical
2373 drag cursor is shown early enough. */
2374 height = min (height,
2375 max (0, WINDOW_BOX_HEIGHT_NO_MODE_LINE (w) - gy));
2378 else
2380 /* Use nominal line height at end of window. */
2381 gx = (x / width) * width;
2382 y -= gy;
2383 gy += (y / height) * height;
2384 if (part != ON_MODE_LINE && part != ON_HEADER_LINE)
2385 /* See comment above. */
2386 height = min (height,
2387 max (0, WINDOW_BOX_HEIGHT_NO_MODE_LINE (w) - gy));
2389 break;
2391 case ON_LEFT_FRINGE:
2392 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2393 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w)
2394 : window_box_right_offset (w, LEFT_MARGIN_AREA));
2395 width = WINDOW_LEFT_FRINGE_WIDTH (w);
2396 goto row_glyph;
2398 case ON_RIGHT_FRINGE:
2399 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2400 ? window_box_right_offset (w, RIGHT_MARGIN_AREA)
2401 : window_box_right_offset (w, TEXT_AREA));
2402 if (WINDOW_RIGHT_DIVIDER_WIDTH (w) == 0
2403 && !WINDOW_HAS_VERTICAL_SCROLL_BAR (w)
2404 && !WINDOW_RIGHTMOST_P (w))
2405 if (gx < WINDOW_PIXEL_WIDTH (w) - width)
2406 /* Make sure the vertical border can get her own glyph to the
2407 right of the one we build here. */
2408 width = WINDOW_RIGHT_FRINGE_WIDTH (w) - width;
2409 else
2410 width = WINDOW_PIXEL_WIDTH (w) - gx;
2411 else
2412 width = WINDOW_RIGHT_FRINGE_WIDTH (w);
2414 goto row_glyph;
2416 case ON_VERTICAL_BORDER:
2417 gx = WINDOW_PIXEL_WIDTH (w) - width;
2418 goto row_glyph;
2420 case ON_VERTICAL_SCROLL_BAR:
2421 gx = (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w)
2423 : (window_box_right_offset (w, RIGHT_MARGIN_AREA)
2424 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2425 ? WINDOW_RIGHT_FRINGE_WIDTH (w)
2426 : 0)));
2427 width = WINDOW_SCROLL_BAR_AREA_WIDTH (w);
2429 row_glyph:
2430 gr = 0, gy = 0;
2431 for (; r <= end_row && r->enabled_p; ++r)
2432 if (r->y + r->height > y)
2434 gr = r; gy = r->y;
2435 break;
2438 if (gr && gy <= y)
2439 height = gr->height;
2440 else
2442 /* Use nominal line height at end of window. */
2443 y -= gy;
2444 gy += (y / height) * height;
2446 break;
2448 case ON_RIGHT_DIVIDER:
2449 gx = WINDOW_PIXEL_WIDTH (w) - WINDOW_RIGHT_DIVIDER_WIDTH (w);
2450 width = WINDOW_RIGHT_DIVIDER_WIDTH (w);
2451 gy = 0;
2452 /* The bottom divider prevails. */
2453 height = WINDOW_PIXEL_HEIGHT (w) - WINDOW_BOTTOM_DIVIDER_WIDTH (w);
2454 goto add_edge;
2456 case ON_BOTTOM_DIVIDER:
2457 gx = 0;
2458 width = WINDOW_PIXEL_WIDTH (w);
2459 gy = WINDOW_PIXEL_HEIGHT (w) - WINDOW_BOTTOM_DIVIDER_WIDTH (w);
2460 height = WINDOW_BOTTOM_DIVIDER_WIDTH (w);
2461 goto add_edge;
2463 default:
2465 virtual_glyph:
2466 /* If there is no glyph under the mouse, then we divide the screen
2467 into a grid of the smallest glyph in the frame, and use that
2468 as our "glyph". */
2470 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to
2471 round down even for negative values. */
2472 if (gx < 0)
2473 gx -= width - 1;
2474 if (gy < 0)
2475 gy -= height - 1;
2477 gx = (gx / width) * width;
2478 gy = (gy / height) * height;
2480 goto store_rect;
2483 add_edge:
2484 gx += WINDOW_LEFT_EDGE_X (w);
2485 gy += WINDOW_TOP_EDGE_Y (w);
2487 store_rect:
2488 STORE_NATIVE_RECT (*rect, gx, gy, width, height);
2490 /* Visible feedback for debugging. */
2491 #if 0
2492 #if HAVE_X_WINDOWS
2493 XDrawRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
2494 f->output_data.x->normal_gc,
2495 gx, gy, width, height);
2496 #endif
2497 #endif
2501 #endif /* HAVE_WINDOW_SYSTEM */
2503 static void
2504 adjust_window_ends (struct window *w, struct glyph_row *row, bool current)
2506 eassert (w);
2507 w->window_end_pos = Z - MATRIX_ROW_END_CHARPOS (row);
2508 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
2509 w->window_end_vpos
2510 = MATRIX_ROW_VPOS (row, current ? w->current_matrix : w->desired_matrix);
2513 /***********************************************************************
2514 Lisp form evaluation
2515 ***********************************************************************/
2517 /* Error handler for safe_eval and safe_call. */
2519 static Lisp_Object
2520 safe_eval_handler (Lisp_Object arg, ptrdiff_t nargs, Lisp_Object *args)
2522 add_to_log ("Error during redisplay: %S signaled %S",
2523 Flist (nargs, args), arg);
2524 return Qnil;
2527 /* Call function FUNC with the rest of NARGS - 1 arguments
2528 following. Return the result, or nil if something went
2529 wrong. Prevent redisplay during the evaluation. */
2531 static Lisp_Object
2532 safe__call (bool inhibit_quit, ptrdiff_t nargs, Lisp_Object func, va_list ap)
2534 Lisp_Object val;
2536 if (inhibit_eval_during_redisplay)
2537 val = Qnil;
2538 else
2540 ptrdiff_t i;
2541 ptrdiff_t count = SPECPDL_INDEX ();
2542 Lisp_Object *args;
2543 USE_SAFE_ALLOCA;
2544 SAFE_ALLOCA_LISP (args, nargs);
2546 args[0] = func;
2547 for (i = 1; i < nargs; i++)
2548 args[i] = va_arg (ap, Lisp_Object);
2550 specbind (Qinhibit_redisplay, Qt);
2551 if (inhibit_quit)
2552 specbind (Qinhibit_quit, Qt);
2553 /* Use Qt to ensure debugger does not run,
2554 so there is no possibility of wanting to redisplay. */
2555 val = internal_condition_case_n (Ffuncall, nargs, args, Qt,
2556 safe_eval_handler);
2557 SAFE_FREE ();
2558 val = unbind_to (count, val);
2561 return val;
2564 Lisp_Object
2565 safe_call (ptrdiff_t nargs, Lisp_Object func, ...)
2567 Lisp_Object retval;
2568 va_list ap;
2570 va_start (ap, func);
2571 retval = safe__call (false, nargs, func, ap);
2572 va_end (ap);
2573 return retval;
2576 /* Call function FN with one argument ARG.
2577 Return the result, or nil if something went wrong. */
2579 Lisp_Object
2580 safe_call1 (Lisp_Object fn, Lisp_Object arg)
2582 return safe_call (2, fn, arg);
2585 static Lisp_Object
2586 safe__call1 (bool inhibit_quit, Lisp_Object fn, ...)
2588 Lisp_Object retval;
2589 va_list ap;
2591 va_start (ap, fn);
2592 retval = safe__call (inhibit_quit, 2, fn, ap);
2593 va_end (ap);
2594 return retval;
2597 Lisp_Object
2598 safe_eval (Lisp_Object sexpr)
2600 return safe__call1 (false, Qeval, sexpr);
2603 static Lisp_Object
2604 safe__eval (bool inhibit_quit, Lisp_Object sexpr)
2606 return safe__call1 (inhibit_quit, Qeval, sexpr);
2609 /* Call function FN with two arguments ARG1 and ARG2.
2610 Return the result, or nil if something went wrong. */
2612 Lisp_Object
2613 safe_call2 (Lisp_Object fn, Lisp_Object arg1, Lisp_Object arg2)
2615 return safe_call (3, fn, arg1, arg2);
2620 /***********************************************************************
2621 Debugging
2622 ***********************************************************************/
2624 #if 0
2626 /* Define CHECK_IT to perform sanity checks on iterators.
2627 This is for debugging. It is too slow to do unconditionally. */
2629 static void
2630 check_it (struct it *it)
2632 if (it->method == GET_FROM_STRING)
2634 eassert (STRINGP (it->string));
2635 eassert (IT_STRING_CHARPOS (*it) >= 0);
2637 else
2639 eassert (IT_STRING_CHARPOS (*it) < 0);
2640 if (it->method == GET_FROM_BUFFER)
2642 /* Check that character and byte positions agree. */
2643 eassert (IT_CHARPOS (*it) == BYTE_TO_CHAR (IT_BYTEPOS (*it)));
2647 if (it->dpvec)
2648 eassert (it->current.dpvec_index >= 0);
2649 else
2650 eassert (it->current.dpvec_index < 0);
2653 #define CHECK_IT(IT) check_it ((IT))
2655 #else /* not 0 */
2657 #define CHECK_IT(IT) (void) 0
2659 #endif /* not 0 */
2662 #if defined GLYPH_DEBUG && defined ENABLE_CHECKING
2664 /* Check that the window end of window W is what we expect it
2665 to be---the last row in the current matrix displaying text. */
2667 static void
2668 check_window_end (struct window *w)
2670 if (!MINI_WINDOW_P (w) && w->window_end_valid)
2672 struct glyph_row *row;
2673 eassert ((row = MATRIX_ROW (w->current_matrix, w->window_end_vpos),
2674 !row->enabled_p
2675 || MATRIX_ROW_DISPLAYS_TEXT_P (row)
2676 || MATRIX_ROW_VPOS (row, w->current_matrix) == 0));
2680 #define CHECK_WINDOW_END(W) check_window_end ((W))
2682 #else
2684 #define CHECK_WINDOW_END(W) (void) 0
2686 #endif /* GLYPH_DEBUG and ENABLE_CHECKING */
2688 /***********************************************************************
2689 Iterator initialization
2690 ***********************************************************************/
2692 /* Initialize IT for displaying current_buffer in window W, starting
2693 at character position CHARPOS. CHARPOS < 0 means that no buffer
2694 position is specified which is useful when the iterator is assigned
2695 a position later. BYTEPOS is the byte position corresponding to
2696 CHARPOS.
2698 If ROW is not null, calls to produce_glyphs with IT as parameter
2699 will produce glyphs in that row.
2701 BASE_FACE_ID is the id of a base face to use. It must be one of
2702 DEFAULT_FACE_ID for normal text, MODE_LINE_FACE_ID,
2703 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID for displaying
2704 mode lines, or TOOL_BAR_FACE_ID for displaying the tool-bar.
2706 If ROW is null and BASE_FACE_ID is equal to MODE_LINE_FACE_ID,
2707 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID, the iterator
2708 will be initialized to use the corresponding mode line glyph row of
2709 the desired matrix of W. */
2711 void
2712 init_iterator (struct it *it, struct window *w,
2713 ptrdiff_t charpos, ptrdiff_t bytepos,
2714 struct glyph_row *row, enum face_id base_face_id)
2716 enum face_id remapped_base_face_id = base_face_id;
2718 /* Some precondition checks. */
2719 eassert (w != NULL && it != NULL);
2720 eassert (charpos < 0 || (charpos >= BUF_BEG (current_buffer)
2721 && charpos <= ZV));
2723 /* If face attributes have been changed since the last redisplay,
2724 free realized faces now because they depend on face definitions
2725 that might have changed. Don't free faces while there might be
2726 desired matrices pending which reference these faces. */
2727 if (face_change && !inhibit_free_realized_faces)
2729 face_change = false;
2730 free_all_realized_faces (Qnil);
2733 /* Perhaps remap BASE_FACE_ID to a user-specified alternative. */
2734 if (! NILP (Vface_remapping_alist))
2735 remapped_base_face_id
2736 = lookup_basic_face (XFRAME (w->frame), base_face_id);
2738 /* Use one of the mode line rows of W's desired matrix if
2739 appropriate. */
2740 if (row == NULL)
2742 if (base_face_id == MODE_LINE_FACE_ID
2743 || base_face_id == MODE_LINE_INACTIVE_FACE_ID)
2744 row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
2745 else if (base_face_id == HEADER_LINE_FACE_ID)
2746 row = MATRIX_HEADER_LINE_ROW (w->desired_matrix);
2749 /* Clear IT, and set it->object and other IT's Lisp objects to Qnil.
2750 Other parts of redisplay rely on that. */
2751 memclear (it, sizeof *it);
2752 it->current.overlay_string_index = -1;
2753 it->current.dpvec_index = -1;
2754 it->base_face_id = remapped_base_face_id;
2755 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
2756 it->paragraph_embedding = L2R;
2757 it->bidi_it.w = w;
2759 /* The window in which we iterate over current_buffer: */
2760 XSETWINDOW (it->window, w);
2761 it->w = w;
2762 it->f = XFRAME (w->frame);
2764 it->cmp_it.id = -1;
2766 /* Extra space between lines (on window systems only). */
2767 if (base_face_id == DEFAULT_FACE_ID
2768 && FRAME_WINDOW_P (it->f))
2770 if (NATNUMP (BVAR (current_buffer, extra_line_spacing)))
2771 it->extra_line_spacing = XFASTINT (BVAR (current_buffer, extra_line_spacing));
2772 else if (FLOATP (BVAR (current_buffer, extra_line_spacing)))
2773 it->extra_line_spacing = (XFLOAT_DATA (BVAR (current_buffer, extra_line_spacing))
2774 * FRAME_LINE_HEIGHT (it->f));
2775 else if (it->f->extra_line_spacing > 0)
2776 it->extra_line_spacing = it->f->extra_line_spacing;
2779 /* If realized faces have been removed, e.g. because of face
2780 attribute changes of named faces, recompute them. When running
2781 in batch mode, the face cache of the initial frame is null. If
2782 we happen to get called, make a dummy face cache. */
2783 if (FRAME_FACE_CACHE (it->f) == NULL)
2784 init_frame_faces (it->f);
2785 if (FRAME_FACE_CACHE (it->f)->used == 0)
2786 recompute_basic_faces (it->f);
2788 it->override_ascent = -1;
2790 /* Are control characters displayed as `^C'? */
2791 it->ctl_arrow_p = !NILP (BVAR (current_buffer, ctl_arrow));
2793 /* -1 means everything between a CR and the following line end
2794 is invisible. >0 means lines indented more than this value are
2795 invisible. */
2796 it->selective = (INTEGERP (BVAR (current_buffer, selective_display))
2797 ? (clip_to_bounds
2798 (-1, XINT (BVAR (current_buffer, selective_display)),
2799 PTRDIFF_MAX))
2800 : (!NILP (BVAR (current_buffer, selective_display))
2801 ? -1 : 0));
2802 it->selective_display_ellipsis_p
2803 = !NILP (BVAR (current_buffer, selective_display_ellipses));
2805 /* Display table to use. */
2806 it->dp = window_display_table (w);
2808 /* Are multibyte characters enabled in current_buffer? */
2809 it->multibyte_p = !NILP (BVAR (current_buffer, enable_multibyte_characters));
2811 /* Get the position at which the redisplay_end_trigger hook should
2812 be run, if it is to be run at all. */
2813 if (MARKERP (w->redisplay_end_trigger)
2814 && XMARKER (w->redisplay_end_trigger)->buffer != 0)
2815 it->redisplay_end_trigger_charpos
2816 = marker_position (w->redisplay_end_trigger);
2817 else if (INTEGERP (w->redisplay_end_trigger))
2818 it->redisplay_end_trigger_charpos
2819 = clip_to_bounds (PTRDIFF_MIN, XINT (w->redisplay_end_trigger),
2820 PTRDIFF_MAX);
2822 it->tab_width = SANE_TAB_WIDTH (current_buffer);
2824 /* Are lines in the display truncated? */
2825 if (TRUNCATE != 0)
2826 it->line_wrap = TRUNCATE;
2827 if (base_face_id == DEFAULT_FACE_ID
2828 && !it->w->hscroll
2829 && (WINDOW_FULL_WIDTH_P (it->w)
2830 || NILP (Vtruncate_partial_width_windows)
2831 || (INTEGERP (Vtruncate_partial_width_windows)
2832 /* PXW: Shall we do something about this? */
2833 && (XINT (Vtruncate_partial_width_windows)
2834 <= WINDOW_TOTAL_COLS (it->w))))
2835 && NILP (BVAR (current_buffer, truncate_lines)))
2836 it->line_wrap = NILP (BVAR (current_buffer, word_wrap))
2837 ? WINDOW_WRAP : WORD_WRAP;
2839 /* Get dimensions of truncation and continuation glyphs. These are
2840 displayed as fringe bitmaps under X, but we need them for such
2841 frames when the fringes are turned off. But leave the dimensions
2842 zero for tooltip frames, as these glyphs look ugly there and also
2843 sabotage calculations of tooltip dimensions in x-show-tip. */
2844 #ifdef HAVE_WINDOW_SYSTEM
2845 if (!(FRAME_WINDOW_P (it->f)
2846 && FRAMEP (tip_frame)
2847 && it->f == XFRAME (tip_frame)))
2848 #endif
2850 if (it->line_wrap == TRUNCATE)
2852 /* We will need the truncation glyph. */
2853 eassert (it->glyph_row == NULL);
2854 produce_special_glyphs (it, IT_TRUNCATION);
2855 it->truncation_pixel_width = it->pixel_width;
2857 else
2859 /* We will need the continuation glyph. */
2860 eassert (it->glyph_row == NULL);
2861 produce_special_glyphs (it, IT_CONTINUATION);
2862 it->continuation_pixel_width = it->pixel_width;
2866 /* Reset these values to zero because the produce_special_glyphs
2867 above has changed them. */
2868 it->pixel_width = it->ascent = it->descent = 0;
2869 it->phys_ascent = it->phys_descent = 0;
2871 /* Set this after getting the dimensions of truncation and
2872 continuation glyphs, so that we don't produce glyphs when calling
2873 produce_special_glyphs, above. */
2874 it->glyph_row = row;
2875 it->area = TEXT_AREA;
2877 /* Get the dimensions of the display area. The display area
2878 consists of the visible window area plus a horizontally scrolled
2879 part to the left of the window. All x-values are relative to the
2880 start of this total display area. */
2881 if (base_face_id != DEFAULT_FACE_ID)
2883 /* Mode lines, menu bar in terminal frames. */
2884 it->first_visible_x = 0;
2885 it->last_visible_x = WINDOW_PIXEL_WIDTH (w);
2887 else
2889 it->first_visible_x
2890 = window_hscroll_limited (it->w, it->f) * FRAME_COLUMN_WIDTH (it->f);
2891 it->last_visible_x = (it->first_visible_x
2892 + window_box_width (w, TEXT_AREA));
2894 /* If we truncate lines, leave room for the truncation glyph(s) at
2895 the right margin. Otherwise, leave room for the continuation
2896 glyph(s). Done only if the window has no right fringe. */
2897 if (WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0)
2899 if (it->line_wrap == TRUNCATE)
2900 it->last_visible_x -= it->truncation_pixel_width;
2901 else
2902 it->last_visible_x -= it->continuation_pixel_width;
2905 it->header_line_p = WINDOW_WANTS_HEADER_LINE_P (w);
2906 it->current_y = WINDOW_HEADER_LINE_HEIGHT (w) + w->vscroll;
2909 /* Leave room for a border glyph. */
2910 if (!FRAME_WINDOW_P (it->f)
2911 && !WINDOW_RIGHTMOST_P (it->w))
2912 it->last_visible_x -= 1;
2914 it->last_visible_y = window_text_bottom_y (w);
2916 /* For mode lines and alike, arrange for the first glyph having a
2917 left box line if the face specifies a box. */
2918 if (base_face_id != DEFAULT_FACE_ID)
2920 struct face *face;
2922 it->face_id = remapped_base_face_id;
2924 /* If we have a boxed mode line, make the first character appear
2925 with a left box line. */
2926 face = FACE_FROM_ID (it->f, remapped_base_face_id);
2927 if (face && face->box != FACE_NO_BOX)
2928 it->start_of_box_run_p = true;
2931 /* If a buffer position was specified, set the iterator there,
2932 getting overlays and face properties from that position. */
2933 if (charpos >= BUF_BEG (current_buffer))
2935 it->stop_charpos = charpos;
2936 it->end_charpos = ZV;
2937 eassert (charpos == BYTE_TO_CHAR (bytepos));
2938 IT_CHARPOS (*it) = charpos;
2939 IT_BYTEPOS (*it) = bytepos;
2941 /* We will rely on `reseat' to set this up properly, via
2942 handle_face_prop. */
2943 it->face_id = it->base_face_id;
2945 it->start = it->current;
2946 /* Do we need to reorder bidirectional text? Not if this is a
2947 unibyte buffer: by definition, none of the single-byte
2948 characters are strong R2L, so no reordering is needed. And
2949 bidi.c doesn't support unibyte buffers anyway. Also, don't
2950 reorder while we are loading loadup.el, since the tables of
2951 character properties needed for reordering are not yet
2952 available. */
2953 it->bidi_p =
2954 NILP (Vpurify_flag)
2955 && !NILP (BVAR (current_buffer, bidi_display_reordering))
2956 && it->multibyte_p;
2958 /* If we are to reorder bidirectional text, init the bidi
2959 iterator. */
2960 if (it->bidi_p)
2962 /* Since we don't know at this point whether there will be
2963 any R2L lines in the window, we reserve space for
2964 truncation/continuation glyphs even if only the left
2965 fringe is absent. */
2966 if (base_face_id == DEFAULT_FACE_ID
2967 && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0
2968 && WINDOW_RIGHT_FRINGE_WIDTH (it->w) != 0)
2970 if (it->line_wrap == TRUNCATE)
2971 it->last_visible_x -= it->truncation_pixel_width;
2972 else
2973 it->last_visible_x -= it->continuation_pixel_width;
2975 /* Note the paragraph direction that this buffer wants to
2976 use. */
2977 if (EQ (BVAR (current_buffer, bidi_paragraph_direction),
2978 Qleft_to_right))
2979 it->paragraph_embedding = L2R;
2980 else if (EQ (BVAR (current_buffer, bidi_paragraph_direction),
2981 Qright_to_left))
2982 it->paragraph_embedding = R2L;
2983 else
2984 it->paragraph_embedding = NEUTRAL_DIR;
2985 bidi_unshelve_cache (NULL, 0);
2986 bidi_init_it (charpos, IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
2987 &it->bidi_it);
2990 /* Compute faces etc. */
2991 reseat (it, it->current.pos, 1);
2994 CHECK_IT (it);
2998 /* Initialize IT for the display of window W with window start POS. */
3000 void
3001 start_display (struct it *it, struct window *w, struct text_pos pos)
3003 struct glyph_row *row;
3004 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
3006 row = w->desired_matrix->rows + first_vpos;
3007 init_iterator (it, w, CHARPOS (pos), BYTEPOS (pos), row, DEFAULT_FACE_ID);
3008 it->first_vpos = first_vpos;
3010 /* Don't reseat to previous visible line start if current start
3011 position is in a string or image. */
3012 if (it->method == GET_FROM_BUFFER && it->line_wrap != TRUNCATE)
3014 int start_at_line_beg_p;
3015 int first_y = it->current_y;
3017 /* If window start is not at a line start, skip forward to POS to
3018 get the correct continuation lines width. */
3019 start_at_line_beg_p = (CHARPOS (pos) == BEGV
3020 || FETCH_BYTE (BYTEPOS (pos) - 1) == '\n');
3021 if (!start_at_line_beg_p)
3023 int new_x;
3025 reseat_at_previous_visible_line_start (it);
3026 move_it_to (it, CHARPOS (pos), -1, -1, -1, MOVE_TO_POS);
3028 new_x = it->current_x + it->pixel_width;
3030 /* If lines are continued, this line may end in the middle
3031 of a multi-glyph character (e.g. a control character
3032 displayed as \003, or in the middle of an overlay
3033 string). In this case move_it_to above will not have
3034 taken us to the start of the continuation line but to the
3035 end of the continued line. */
3036 if (it->current_x > 0
3037 && it->line_wrap != TRUNCATE /* Lines are continued. */
3038 && (/* And glyph doesn't fit on the line. */
3039 new_x > it->last_visible_x
3040 /* Or it fits exactly and we're on a window
3041 system frame. */
3042 || (new_x == it->last_visible_x
3043 && FRAME_WINDOW_P (it->f)
3044 && ((it->bidi_p && it->bidi_it.paragraph_dir == R2L)
3045 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
3046 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)))))
3048 if ((it->current.dpvec_index >= 0
3049 || it->current.overlay_string_index >= 0)
3050 /* If we are on a newline from a display vector or
3051 overlay string, then we are already at the end of
3052 a screen line; no need to go to the next line in
3053 that case, as this line is not really continued.
3054 (If we do go to the next line, C-e will not DTRT.) */
3055 && it->c != '\n')
3057 set_iterator_to_next (it, 1);
3058 move_it_in_display_line_to (it, -1, -1, 0);
3061 it->continuation_lines_width += it->current_x;
3063 /* If the character at POS is displayed via a display
3064 vector, move_it_to above stops at the final glyph of
3065 IT->dpvec. To make the caller redisplay that character
3066 again (a.k.a. start at POS), we need to reset the
3067 dpvec_index to the beginning of IT->dpvec. */
3068 else if (it->current.dpvec_index >= 0)
3069 it->current.dpvec_index = 0;
3071 /* We're starting a new display line, not affected by the
3072 height of the continued line, so clear the appropriate
3073 fields in the iterator structure. */
3074 it->max_ascent = it->max_descent = 0;
3075 it->max_phys_ascent = it->max_phys_descent = 0;
3077 it->current_y = first_y;
3078 it->vpos = 0;
3079 it->current_x = it->hpos = 0;
3085 /* Return 1 if POS is a position in ellipses displayed for invisible
3086 text. W is the window we display, for text property lookup. */
3088 static int
3089 in_ellipses_for_invisible_text_p (struct display_pos *pos, struct window *w)
3091 Lisp_Object prop, window;
3092 int ellipses_p = 0;
3093 ptrdiff_t charpos = CHARPOS (pos->pos);
3095 /* If POS specifies a position in a display vector, this might
3096 be for an ellipsis displayed for invisible text. We won't
3097 get the iterator set up for delivering that ellipsis unless
3098 we make sure that it gets aware of the invisible text. */
3099 if (pos->dpvec_index >= 0
3100 && pos->overlay_string_index < 0
3101 && CHARPOS (pos->string_pos) < 0
3102 && charpos > BEGV
3103 && (XSETWINDOW (window, w),
3104 prop = Fget_char_property (make_number (charpos),
3105 Qinvisible, window),
3106 !TEXT_PROP_MEANS_INVISIBLE (prop)))
3108 prop = Fget_char_property (make_number (charpos - 1), Qinvisible,
3109 window);
3110 ellipses_p = 2 == TEXT_PROP_MEANS_INVISIBLE (prop);
3113 return ellipses_p;
3117 /* Initialize IT for stepping through current_buffer in window W,
3118 starting at position POS that includes overlay string and display
3119 vector/ control character translation position information. Value
3120 is zero if there are overlay strings with newlines at POS. */
3122 static int
3123 init_from_display_pos (struct it *it, struct window *w, struct display_pos *pos)
3125 ptrdiff_t charpos = CHARPOS (pos->pos), bytepos = BYTEPOS (pos->pos);
3126 int i, overlay_strings_with_newlines = 0;
3128 /* If POS specifies a position in a display vector, this might
3129 be for an ellipsis displayed for invisible text. We won't
3130 get the iterator set up for delivering that ellipsis unless
3131 we make sure that it gets aware of the invisible text. */
3132 if (in_ellipses_for_invisible_text_p (pos, w))
3134 --charpos;
3135 bytepos = 0;
3138 /* Keep in mind: the call to reseat in init_iterator skips invisible
3139 text, so we might end up at a position different from POS. This
3140 is only a problem when POS is a row start after a newline and an
3141 overlay starts there with an after-string, and the overlay has an
3142 invisible property. Since we don't skip invisible text in
3143 display_line and elsewhere immediately after consuming the
3144 newline before the row start, such a POS will not be in a string,
3145 but the call to init_iterator below will move us to the
3146 after-string. */
3147 init_iterator (it, w, charpos, bytepos, NULL, DEFAULT_FACE_ID);
3149 /* This only scans the current chunk -- it should scan all chunks.
3150 However, OVERLAY_STRING_CHUNK_SIZE has been increased from 3 in 21.1
3151 to 16 in 22.1 to make this a lesser problem. */
3152 for (i = 0; i < it->n_overlay_strings && i < OVERLAY_STRING_CHUNK_SIZE; ++i)
3154 const char *s = SSDATA (it->overlay_strings[i]);
3155 const char *e = s + SBYTES (it->overlay_strings[i]);
3157 while (s < e && *s != '\n')
3158 ++s;
3160 if (s < e)
3162 overlay_strings_with_newlines = 1;
3163 break;
3167 /* If position is within an overlay string, set up IT to the right
3168 overlay string. */
3169 if (pos->overlay_string_index >= 0)
3171 int relative_index;
3173 /* If the first overlay string happens to have a `display'
3174 property for an image, the iterator will be set up for that
3175 image, and we have to undo that setup first before we can
3176 correct the overlay string index. */
3177 if (it->method == GET_FROM_IMAGE)
3178 pop_it (it);
3180 /* We already have the first chunk of overlay strings in
3181 IT->overlay_strings. Load more until the one for
3182 pos->overlay_string_index is in IT->overlay_strings. */
3183 if (pos->overlay_string_index >= OVERLAY_STRING_CHUNK_SIZE)
3185 ptrdiff_t n = pos->overlay_string_index / OVERLAY_STRING_CHUNK_SIZE;
3186 it->current.overlay_string_index = 0;
3187 while (n--)
3189 load_overlay_strings (it, 0);
3190 it->current.overlay_string_index += OVERLAY_STRING_CHUNK_SIZE;
3194 it->current.overlay_string_index = pos->overlay_string_index;
3195 relative_index = (it->current.overlay_string_index
3196 % OVERLAY_STRING_CHUNK_SIZE);
3197 it->string = it->overlay_strings[relative_index];
3198 eassert (STRINGP (it->string));
3199 it->current.string_pos = pos->string_pos;
3200 it->method = GET_FROM_STRING;
3201 it->end_charpos = SCHARS (it->string);
3202 /* Set up the bidi iterator for this overlay string. */
3203 if (it->bidi_p)
3205 it->bidi_it.string.lstring = it->string;
3206 it->bidi_it.string.s = NULL;
3207 it->bidi_it.string.schars = SCHARS (it->string);
3208 it->bidi_it.string.bufpos = it->overlay_strings_charpos;
3209 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
3210 it->bidi_it.string.unibyte = !it->multibyte_p;
3211 it->bidi_it.w = it->w;
3212 bidi_init_it (IT_STRING_CHARPOS (*it), IT_STRING_BYTEPOS (*it),
3213 FRAME_WINDOW_P (it->f), &it->bidi_it);
3215 /* Synchronize the state of the bidi iterator with
3216 pos->string_pos. For any string position other than
3217 zero, this will be done automagically when we resume
3218 iteration over the string and get_visually_first_element
3219 is called. But if string_pos is zero, and the string is
3220 to be reordered for display, we need to resync manually,
3221 since it could be that the iteration state recorded in
3222 pos ended at string_pos of 0 moving backwards in string. */
3223 if (CHARPOS (pos->string_pos) == 0)
3225 get_visually_first_element (it);
3226 if (IT_STRING_CHARPOS (*it) != 0)
3227 do {
3228 /* Paranoia. */
3229 eassert (it->bidi_it.charpos < it->bidi_it.string.schars);
3230 bidi_move_to_visually_next (&it->bidi_it);
3231 } while (it->bidi_it.charpos != 0);
3233 eassert (IT_STRING_CHARPOS (*it) == it->bidi_it.charpos
3234 && IT_STRING_BYTEPOS (*it) == it->bidi_it.bytepos);
3238 if (CHARPOS (pos->string_pos) >= 0)
3240 /* Recorded position is not in an overlay string, but in another
3241 string. This can only be a string from a `display' property.
3242 IT should already be filled with that string. */
3243 it->current.string_pos = pos->string_pos;
3244 eassert (STRINGP (it->string));
3245 if (it->bidi_p)
3246 bidi_init_it (IT_STRING_CHARPOS (*it), IT_STRING_BYTEPOS (*it),
3247 FRAME_WINDOW_P (it->f), &it->bidi_it);
3250 /* Restore position in display vector translations, control
3251 character translations or ellipses. */
3252 if (pos->dpvec_index >= 0)
3254 if (it->dpvec == NULL)
3255 get_next_display_element (it);
3256 eassert (it->dpvec && it->current.dpvec_index == 0);
3257 it->current.dpvec_index = pos->dpvec_index;
3260 CHECK_IT (it);
3261 return !overlay_strings_with_newlines;
3265 /* Initialize IT for stepping through current_buffer in window W
3266 starting at ROW->start. */
3268 static void
3269 init_to_row_start (struct it *it, struct window *w, struct glyph_row *row)
3271 init_from_display_pos (it, w, &row->start);
3272 it->start = row->start;
3273 it->continuation_lines_width = row->continuation_lines_width;
3274 CHECK_IT (it);
3278 /* Initialize IT for stepping through current_buffer in window W
3279 starting in the line following ROW, i.e. starting at ROW->end.
3280 Value is zero if there are overlay strings with newlines at ROW's
3281 end position. */
3283 static int
3284 init_to_row_end (struct it *it, struct window *w, struct glyph_row *row)
3286 int success = 0;
3288 if (init_from_display_pos (it, w, &row->end))
3290 if (row->continued_p)
3291 it->continuation_lines_width
3292 = row->continuation_lines_width + row->pixel_width;
3293 CHECK_IT (it);
3294 success = 1;
3297 return success;
3303 /***********************************************************************
3304 Text properties
3305 ***********************************************************************/
3307 /* Called when IT reaches IT->stop_charpos. Handle text property and
3308 overlay changes. Set IT->stop_charpos to the next position where
3309 to stop. */
3311 static void
3312 handle_stop (struct it *it)
3314 enum prop_handled handled;
3315 int handle_overlay_change_p;
3316 struct props *p;
3318 it->dpvec = NULL;
3319 it->current.dpvec_index = -1;
3320 handle_overlay_change_p = !it->ignore_overlay_strings_at_pos_p;
3321 it->ignore_overlay_strings_at_pos_p = 0;
3322 it->ellipsis_p = 0;
3324 /* Use face of preceding text for ellipsis (if invisible) */
3325 if (it->selective_display_ellipsis_p)
3326 it->saved_face_id = it->face_id;
3328 /* Here's the description of the semantics of, and the logic behind,
3329 the various HANDLED_* statuses:
3331 HANDLED_NORMALLY means the handler did its job, and the loop
3332 should proceed to calling the next handler in order.
3334 HANDLED_RECOMPUTE_PROPS means the handler caused a significant
3335 change in the properties and overlays at current position, so the
3336 loop should be restarted, to re-invoke the handlers that were
3337 already called. This happens when fontification-functions were
3338 called by handle_fontified_prop, and actually fontified
3339 something. Another case where HANDLED_RECOMPUTE_PROPS is
3340 returned is when we discover overlay strings that need to be
3341 displayed right away. The loop below will continue for as long
3342 as the status is HANDLED_RECOMPUTE_PROPS.
3344 HANDLED_RETURN means return immediately to the caller, to
3345 continue iteration without calling any further handlers. This is
3346 used when we need to act on some property right away, for example
3347 when we need to display the ellipsis or a replacing display
3348 property, such as display string or image.
3350 HANDLED_OVERLAY_STRING_CONSUMED means an overlay string was just
3351 consumed, and the handler switched to the next overlay string.
3352 This signals the loop below to refrain from looking for more
3353 overlays before all the overlay strings of the current overlay
3354 are processed.
3356 Some of the handlers called by the loop push the iterator state
3357 onto the stack (see 'push_it'), and arrange for the iteration to
3358 continue with another object, such as an image, a display string,
3359 or an overlay string. In most such cases, it->stop_charpos is
3360 set to the first character of the string, so that when the
3361 iteration resumes, this function will immediately be called
3362 again, to examine the properties at the beginning of the string.
3364 When a display or overlay string is exhausted, the iterator state
3365 is popped (see 'pop_it'), and iteration continues with the
3366 previous object. Again, in many such cases this function is
3367 called again to find the next position where properties might
3368 change. */
3372 handled = HANDLED_NORMALLY;
3374 /* Call text property handlers. */
3375 for (p = it_props; p->handler; ++p)
3377 handled = p->handler (it);
3379 if (handled == HANDLED_RECOMPUTE_PROPS)
3380 break;
3381 else if (handled == HANDLED_RETURN)
3383 /* We still want to show before and after strings from
3384 overlays even if the actual buffer text is replaced. */
3385 if (!handle_overlay_change_p
3386 || it->sp > 1
3387 /* Don't call get_overlay_strings_1 if we already
3388 have overlay strings loaded, because doing so
3389 will load them again and push the iterator state
3390 onto the stack one more time, which is not
3391 expected by the rest of the code that processes
3392 overlay strings. */
3393 || (it->current.overlay_string_index < 0
3394 ? !get_overlay_strings_1 (it, 0, 0)
3395 : 0))
3397 if (it->ellipsis_p)
3398 setup_for_ellipsis (it, 0);
3399 /* When handling a display spec, we might load an
3400 empty string. In that case, discard it here. We
3401 used to discard it in handle_single_display_spec,
3402 but that causes get_overlay_strings_1, above, to
3403 ignore overlay strings that we must check. */
3404 if (STRINGP (it->string) && !SCHARS (it->string))
3405 pop_it (it);
3406 return;
3408 else if (STRINGP (it->string) && !SCHARS (it->string))
3409 pop_it (it);
3410 else
3412 it->ignore_overlay_strings_at_pos_p = true;
3413 it->string_from_display_prop_p = 0;
3414 it->from_disp_prop_p = 0;
3415 handle_overlay_change_p = 0;
3417 handled = HANDLED_RECOMPUTE_PROPS;
3418 break;
3420 else if (handled == HANDLED_OVERLAY_STRING_CONSUMED)
3421 handle_overlay_change_p = 0;
3424 if (handled != HANDLED_RECOMPUTE_PROPS)
3426 /* Don't check for overlay strings below when set to deliver
3427 characters from a display vector. */
3428 if (it->method == GET_FROM_DISPLAY_VECTOR)
3429 handle_overlay_change_p = 0;
3431 /* Handle overlay changes.
3432 This sets HANDLED to HANDLED_RECOMPUTE_PROPS
3433 if it finds overlays. */
3434 if (handle_overlay_change_p)
3435 handled = handle_overlay_change (it);
3438 if (it->ellipsis_p)
3440 setup_for_ellipsis (it, 0);
3441 break;
3444 while (handled == HANDLED_RECOMPUTE_PROPS);
3446 /* Determine where to stop next. */
3447 if (handled == HANDLED_NORMALLY)
3448 compute_stop_pos (it);
3452 /* Compute IT->stop_charpos from text property and overlay change
3453 information for IT's current position. */
3455 static void
3456 compute_stop_pos (struct it *it)
3458 register INTERVAL iv, next_iv;
3459 Lisp_Object object, limit, position;
3460 ptrdiff_t charpos, bytepos;
3462 if (STRINGP (it->string))
3464 /* Strings are usually short, so don't limit the search for
3465 properties. */
3466 it->stop_charpos = it->end_charpos;
3467 object = it->string;
3468 limit = Qnil;
3469 charpos = IT_STRING_CHARPOS (*it);
3470 bytepos = IT_STRING_BYTEPOS (*it);
3472 else
3474 ptrdiff_t pos;
3476 /* If end_charpos is out of range for some reason, such as a
3477 misbehaving display function, rationalize it (Bug#5984). */
3478 if (it->end_charpos > ZV)
3479 it->end_charpos = ZV;
3480 it->stop_charpos = it->end_charpos;
3482 /* If next overlay change is in front of the current stop pos
3483 (which is IT->end_charpos), stop there. Note: value of
3484 next_overlay_change is point-max if no overlay change
3485 follows. */
3486 charpos = IT_CHARPOS (*it);
3487 bytepos = IT_BYTEPOS (*it);
3488 pos = next_overlay_change (charpos);
3489 if (pos < it->stop_charpos)
3490 it->stop_charpos = pos;
3492 /* Set up variables for computing the stop position from text
3493 property changes. */
3494 XSETBUFFER (object, current_buffer);
3495 limit = make_number (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT);
3498 /* Get the interval containing IT's position. Value is a null
3499 interval if there isn't such an interval. */
3500 position = make_number (charpos);
3501 iv = validate_interval_range (object, &position, &position, 0);
3502 if (iv)
3504 Lisp_Object values_here[LAST_PROP_IDX];
3505 struct props *p;
3507 /* Get properties here. */
3508 for (p = it_props; p->handler; ++p)
3509 values_here[p->idx] = textget (iv->plist,
3510 builtin_lisp_symbol (p->name));
3512 /* Look for an interval following iv that has different
3513 properties. */
3514 for (next_iv = next_interval (iv);
3515 (next_iv
3516 && (NILP (limit)
3517 || XFASTINT (limit) > next_iv->position));
3518 next_iv = next_interval (next_iv))
3520 for (p = it_props; p->handler; ++p)
3522 Lisp_Object new_value = textget (next_iv->plist,
3523 builtin_lisp_symbol (p->name));
3524 if (!EQ (values_here[p->idx], new_value))
3525 break;
3528 if (p->handler)
3529 break;
3532 if (next_iv)
3534 if (INTEGERP (limit)
3535 && next_iv->position >= XFASTINT (limit))
3536 /* No text property change up to limit. */
3537 it->stop_charpos = min (XFASTINT (limit), it->stop_charpos);
3538 else
3539 /* Text properties change in next_iv. */
3540 it->stop_charpos = min (it->stop_charpos, next_iv->position);
3544 if (it->cmp_it.id < 0)
3546 ptrdiff_t stoppos = it->end_charpos;
3548 if (it->bidi_p && it->bidi_it.scan_dir < 0)
3549 stoppos = -1;
3550 composition_compute_stop_pos (&it->cmp_it, charpos, bytepos,
3551 stoppos, it->string);
3554 eassert (STRINGP (it->string)
3555 || (it->stop_charpos >= BEGV
3556 && it->stop_charpos >= IT_CHARPOS (*it)));
3560 /* Return the position of the next overlay change after POS in
3561 current_buffer. Value is point-max if no overlay change
3562 follows. This is like `next-overlay-change' but doesn't use
3563 xmalloc. */
3565 static ptrdiff_t
3566 next_overlay_change (ptrdiff_t pos)
3568 ptrdiff_t i, noverlays;
3569 ptrdiff_t endpos;
3570 Lisp_Object *overlays;
3571 USE_SAFE_ALLOCA;
3573 /* Get all overlays at the given position. */
3574 GET_OVERLAYS_AT (pos, overlays, noverlays, &endpos, 1);
3576 /* If any of these overlays ends before endpos,
3577 use its ending point instead. */
3578 for (i = 0; i < noverlays; ++i)
3580 Lisp_Object oend;
3581 ptrdiff_t oendpos;
3583 oend = OVERLAY_END (overlays[i]);
3584 oendpos = OVERLAY_POSITION (oend);
3585 endpos = min (endpos, oendpos);
3588 SAFE_FREE ();
3589 return endpos;
3592 /* How many characters forward to search for a display property or
3593 display string. Searching too far forward makes the bidi display
3594 sluggish, especially in small windows. */
3595 #define MAX_DISP_SCAN 250
3597 /* Return the character position of a display string at or after
3598 position specified by POSITION. If no display string exists at or
3599 after POSITION, return ZV. A display string is either an overlay
3600 with `display' property whose value is a string, or a `display'
3601 text property whose value is a string. STRING is data about the
3602 string to iterate; if STRING->lstring is nil, we are iterating a
3603 buffer. FRAME_WINDOW_P is non-zero when we are displaying a window
3604 on a GUI frame. DISP_PROP is set to zero if we searched
3605 MAX_DISP_SCAN characters forward without finding any display
3606 strings, non-zero otherwise. It is set to 2 if the display string
3607 uses any kind of `(space ...)' spec that will produce a stretch of
3608 white space in the text area. */
3609 ptrdiff_t
3610 compute_display_string_pos (struct text_pos *position,
3611 struct bidi_string_data *string,
3612 struct window *w,
3613 int frame_window_p, int *disp_prop)
3615 /* OBJECT = nil means current buffer. */
3616 Lisp_Object object, object1;
3617 Lisp_Object pos, spec, limpos;
3618 int string_p = (string && (STRINGP (string->lstring) || string->s));
3619 ptrdiff_t eob = string_p ? string->schars : ZV;
3620 ptrdiff_t begb = string_p ? 0 : BEGV;
3621 ptrdiff_t bufpos, charpos = CHARPOS (*position);
3622 ptrdiff_t lim =
3623 (charpos < eob - MAX_DISP_SCAN) ? charpos + MAX_DISP_SCAN : eob;
3624 struct text_pos tpos;
3625 int rv = 0;
3627 if (string && STRINGP (string->lstring))
3628 object1 = object = string->lstring;
3629 else if (w && !string_p)
3631 XSETWINDOW (object, w);
3632 object1 = Qnil;
3634 else
3635 object1 = object = Qnil;
3637 *disp_prop = 1;
3639 if (charpos >= eob
3640 /* We don't support display properties whose values are strings
3641 that have display string properties. */
3642 || string->from_disp_str
3643 /* C strings cannot have display properties. */
3644 || (string->s && !STRINGP (object)))
3646 *disp_prop = 0;
3647 return eob;
3650 /* If the character at CHARPOS is where the display string begins,
3651 return CHARPOS. */
3652 pos = make_number (charpos);
3653 if (STRINGP (object))
3654 bufpos = string->bufpos;
3655 else
3656 bufpos = charpos;
3657 tpos = *position;
3658 if (!NILP (spec = Fget_char_property (pos, Qdisplay, object))
3659 && (charpos <= begb
3660 || !EQ (Fget_char_property (make_number (charpos - 1), Qdisplay,
3661 object),
3662 spec))
3663 && (rv = handle_display_spec (NULL, spec, object, Qnil, &tpos, bufpos,
3664 frame_window_p)))
3666 if (rv == 2)
3667 *disp_prop = 2;
3668 return charpos;
3671 /* Look forward for the first character with a `display' property
3672 that will replace the underlying text when displayed. */
3673 limpos = make_number (lim);
3674 do {
3675 pos = Fnext_single_char_property_change (pos, Qdisplay, object1, limpos);
3676 CHARPOS (tpos) = XFASTINT (pos);
3677 if (CHARPOS (tpos) >= lim)
3679 *disp_prop = 0;
3680 break;
3682 if (STRINGP (object))
3683 BYTEPOS (tpos) = string_char_to_byte (object, CHARPOS (tpos));
3684 else
3685 BYTEPOS (tpos) = CHAR_TO_BYTE (CHARPOS (tpos));
3686 spec = Fget_char_property (pos, Qdisplay, object);
3687 if (!STRINGP (object))
3688 bufpos = CHARPOS (tpos);
3689 } while (NILP (spec)
3690 || !(rv = handle_display_spec (NULL, spec, object, Qnil, &tpos,
3691 bufpos, frame_window_p)));
3692 if (rv == 2)
3693 *disp_prop = 2;
3695 return CHARPOS (tpos);
3698 /* Return the character position of the end of the display string that
3699 started at CHARPOS. If there's no display string at CHARPOS,
3700 return -1. A display string is either an overlay with `display'
3701 property whose value is a string or a `display' text property whose
3702 value is a string. */
3703 ptrdiff_t
3704 compute_display_string_end (ptrdiff_t charpos, struct bidi_string_data *string)
3706 /* OBJECT = nil means current buffer. */
3707 Lisp_Object object =
3708 (string && STRINGP (string->lstring)) ? string->lstring : Qnil;
3709 Lisp_Object pos = make_number (charpos);
3710 ptrdiff_t eob =
3711 (STRINGP (object) || (string && string->s)) ? string->schars : ZV;
3713 if (charpos >= eob || (string->s && !STRINGP (object)))
3714 return eob;
3716 /* It could happen that the display property or overlay was removed
3717 since we found it in compute_display_string_pos above. One way
3718 this can happen is if JIT font-lock was called (through
3719 handle_fontified_prop), and jit-lock-functions remove text
3720 properties or overlays from the portion of buffer that includes
3721 CHARPOS. Muse mode is known to do that, for example. In this
3722 case, we return -1 to the caller, to signal that no display
3723 string is actually present at CHARPOS. See bidi_fetch_char for
3724 how this is handled.
3726 An alternative would be to never look for display properties past
3727 it->stop_charpos. But neither compute_display_string_pos nor
3728 bidi_fetch_char that calls it know or care where the next
3729 stop_charpos is. */
3730 if (NILP (Fget_char_property (pos, Qdisplay, object)))
3731 return -1;
3733 /* Look forward for the first character where the `display' property
3734 changes. */
3735 pos = Fnext_single_char_property_change (pos, Qdisplay, object, Qnil);
3737 return XFASTINT (pos);
3742 /***********************************************************************
3743 Fontification
3744 ***********************************************************************/
3746 /* Handle changes in the `fontified' property of the current buffer by
3747 calling hook functions from Qfontification_functions to fontify
3748 regions of text. */
3750 static enum prop_handled
3751 handle_fontified_prop (struct it *it)
3753 Lisp_Object prop, pos;
3754 enum prop_handled handled = HANDLED_NORMALLY;
3756 if (!NILP (Vmemory_full))
3757 return handled;
3759 /* Get the value of the `fontified' property at IT's current buffer
3760 position. (The `fontified' property doesn't have a special
3761 meaning in strings.) If the value is nil, call functions from
3762 Qfontification_functions. */
3763 if (!STRINGP (it->string)
3764 && it->s == NULL
3765 && !NILP (Vfontification_functions)
3766 && !NILP (Vrun_hooks)
3767 && (pos = make_number (IT_CHARPOS (*it)),
3768 prop = Fget_char_property (pos, Qfontified, Qnil),
3769 /* Ignore the special cased nil value always present at EOB since
3770 no amount of fontifying will be able to change it. */
3771 NILP (prop) && IT_CHARPOS (*it) < Z))
3773 ptrdiff_t count = SPECPDL_INDEX ();
3774 Lisp_Object val;
3775 struct buffer *obuf = current_buffer;
3776 ptrdiff_t begv = BEGV, zv = ZV;
3777 bool old_clip_changed = current_buffer->clip_changed;
3779 val = Vfontification_functions;
3780 specbind (Qfontification_functions, Qnil);
3782 eassert (it->end_charpos == ZV);
3784 if (!CONSP (val) || EQ (XCAR (val), Qlambda))
3785 safe_call1 (val, pos);
3786 else
3788 Lisp_Object fns, fn;
3789 struct gcpro gcpro1, gcpro2;
3791 fns = Qnil;
3792 GCPRO2 (val, fns);
3794 for (; CONSP (val); val = XCDR (val))
3796 fn = XCAR (val);
3798 if (EQ (fn, Qt))
3800 /* A value of t indicates this hook has a local
3801 binding; it means to run the global binding too.
3802 In a global value, t should not occur. If it
3803 does, we must ignore it to avoid an endless
3804 loop. */
3805 for (fns = Fdefault_value (Qfontification_functions);
3806 CONSP (fns);
3807 fns = XCDR (fns))
3809 fn = XCAR (fns);
3810 if (!EQ (fn, Qt))
3811 safe_call1 (fn, pos);
3814 else
3815 safe_call1 (fn, pos);
3818 UNGCPRO;
3821 unbind_to (count, Qnil);
3823 /* Fontification functions routinely call `save-restriction'.
3824 Normally, this tags clip_changed, which can confuse redisplay
3825 (see discussion in Bug#6671). Since we don't perform any
3826 special handling of fontification changes in the case where
3827 `save-restriction' isn't called, there's no point doing so in
3828 this case either. So, if the buffer's restrictions are
3829 actually left unchanged, reset clip_changed. */
3830 if (obuf == current_buffer)
3832 if (begv == BEGV && zv == ZV)
3833 current_buffer->clip_changed = old_clip_changed;
3835 /* There isn't much we can reasonably do to protect against
3836 misbehaving fontification, but here's a fig leaf. */
3837 else if (BUFFER_LIVE_P (obuf))
3838 set_buffer_internal_1 (obuf);
3840 /* The fontification code may have added/removed text.
3841 It could do even a lot worse, but let's at least protect against
3842 the most obvious case where only the text past `pos' gets changed',
3843 as is/was done in grep.el where some escapes sequences are turned
3844 into face properties (bug#7876). */
3845 it->end_charpos = ZV;
3847 /* Return HANDLED_RECOMPUTE_PROPS only if function fontified
3848 something. This avoids an endless loop if they failed to
3849 fontify the text for which reason ever. */
3850 if (!NILP (Fget_char_property (pos, Qfontified, Qnil)))
3851 handled = HANDLED_RECOMPUTE_PROPS;
3854 return handled;
3859 /***********************************************************************
3860 Faces
3861 ***********************************************************************/
3863 /* Set up iterator IT from face properties at its current position.
3864 Called from handle_stop. */
3866 static enum prop_handled
3867 handle_face_prop (struct it *it)
3869 int new_face_id;
3870 ptrdiff_t next_stop;
3872 if (!STRINGP (it->string))
3874 new_face_id
3875 = face_at_buffer_position (it->w,
3876 IT_CHARPOS (*it),
3877 &next_stop,
3878 (IT_CHARPOS (*it)
3879 + TEXT_PROP_DISTANCE_LIMIT),
3880 false, it->base_face_id);
3882 /* Is this a start of a run of characters with box face?
3883 Caveat: this can be called for a freshly initialized
3884 iterator; face_id is -1 in this case. We know that the new
3885 face will not change until limit, i.e. if the new face has a
3886 box, all characters up to limit will have one. But, as
3887 usual, we don't know whether limit is really the end. */
3888 if (new_face_id != it->face_id)
3890 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3891 /* If it->face_id is -1, old_face below will be NULL, see
3892 the definition of FACE_FROM_ID. This will happen if this
3893 is the initial call that gets the face. */
3894 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
3896 /* If the value of face_id of the iterator is -1, we have to
3897 look in front of IT's position and see whether there is a
3898 face there that's different from new_face_id. */
3899 if (!old_face && IT_CHARPOS (*it) > BEG)
3901 int prev_face_id = face_before_it_pos (it);
3903 old_face = FACE_FROM_ID (it->f, prev_face_id);
3906 /* If the new face has a box, but the old face does not,
3907 this is the start of a run of characters with box face,
3908 i.e. this character has a shadow on the left side. */
3909 it->start_of_box_run_p = (new_face->box != FACE_NO_BOX
3910 && (old_face == NULL || !old_face->box));
3911 it->face_box_p = new_face->box != FACE_NO_BOX;
3914 else
3916 int base_face_id;
3917 ptrdiff_t bufpos;
3918 int i;
3919 Lisp_Object from_overlay
3920 = (it->current.overlay_string_index >= 0
3921 ? it->string_overlays[it->current.overlay_string_index
3922 % OVERLAY_STRING_CHUNK_SIZE]
3923 : Qnil);
3925 /* See if we got to this string directly or indirectly from
3926 an overlay property. That includes the before-string or
3927 after-string of an overlay, strings in display properties
3928 provided by an overlay, their text properties, etc.
3930 FROM_OVERLAY is the overlay that brought us here, or nil if none. */
3931 if (! NILP (from_overlay))
3932 for (i = it->sp - 1; i >= 0; i--)
3934 if (it->stack[i].current.overlay_string_index >= 0)
3935 from_overlay
3936 = it->string_overlays[it->stack[i].current.overlay_string_index
3937 % OVERLAY_STRING_CHUNK_SIZE];
3938 else if (! NILP (it->stack[i].from_overlay))
3939 from_overlay = it->stack[i].from_overlay;
3941 if (!NILP (from_overlay))
3942 break;
3945 if (! NILP (from_overlay))
3947 bufpos = IT_CHARPOS (*it);
3948 /* For a string from an overlay, the base face depends
3949 only on text properties and ignores overlays. */
3950 base_face_id
3951 = face_for_overlay_string (it->w,
3952 IT_CHARPOS (*it),
3953 &next_stop,
3954 (IT_CHARPOS (*it)
3955 + TEXT_PROP_DISTANCE_LIMIT),
3956 false,
3957 from_overlay);
3959 else
3961 bufpos = 0;
3963 /* For strings from a `display' property, use the face at
3964 IT's current buffer position as the base face to merge
3965 with, so that overlay strings appear in the same face as
3966 surrounding text, unless they specify their own faces.
3967 For strings from wrap-prefix and line-prefix properties,
3968 use the default face, possibly remapped via
3969 Vface_remapping_alist. */
3970 /* Note that the fact that we use the face at _buffer_
3971 position means that a 'display' property on an overlay
3972 string will not inherit the face of that overlay string,
3973 but will instead revert to the face of buffer text
3974 covered by the overlay. This is visible, e.g., when the
3975 overlay specifies a box face, but neither the buffer nor
3976 the display string do. This sounds like a design bug,
3977 but Emacs always did that since v21.1, so changing that
3978 might be a big deal. */
3979 base_face_id = it->string_from_prefix_prop_p
3980 ? (!NILP (Vface_remapping_alist)
3981 ? lookup_basic_face (it->f, DEFAULT_FACE_ID)
3982 : DEFAULT_FACE_ID)
3983 : underlying_face_id (it);
3986 new_face_id = face_at_string_position (it->w,
3987 it->string,
3988 IT_STRING_CHARPOS (*it),
3989 bufpos,
3990 &next_stop,
3991 base_face_id, false);
3993 /* Is this a start of a run of characters with box? Caveat:
3994 this can be called for a freshly allocated iterator; face_id
3995 is -1 is this case. We know that the new face will not
3996 change until the next check pos, i.e. if the new face has a
3997 box, all characters up to that position will have a
3998 box. But, as usual, we don't know whether that position
3999 is really the end. */
4000 if (new_face_id != it->face_id)
4002 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
4003 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
4005 /* If new face has a box but old face hasn't, this is the
4006 start of a run of characters with box, i.e. it has a
4007 shadow on the left side. */
4008 it->start_of_box_run_p
4009 = new_face->box && (old_face == NULL || !old_face->box);
4010 it->face_box_p = new_face->box != FACE_NO_BOX;
4014 it->face_id = new_face_id;
4015 return HANDLED_NORMALLY;
4019 /* Return the ID of the face ``underlying'' IT's current position,
4020 which is in a string. If the iterator is associated with a
4021 buffer, return the face at IT's current buffer position.
4022 Otherwise, use the iterator's base_face_id. */
4024 static int
4025 underlying_face_id (struct it *it)
4027 int face_id = it->base_face_id, i;
4029 eassert (STRINGP (it->string));
4031 for (i = it->sp - 1; i >= 0; --i)
4032 if (NILP (it->stack[i].string))
4033 face_id = it->stack[i].face_id;
4035 return face_id;
4039 /* Compute the face one character before or after the current position
4040 of IT, in the visual order. BEFORE_P non-zero means get the face
4041 in front (to the left in L2R paragraphs, to the right in R2L
4042 paragraphs) of IT's screen position. Value is the ID of the face. */
4044 static int
4045 face_before_or_after_it_pos (struct it *it, int before_p)
4047 int face_id, limit;
4048 ptrdiff_t next_check_charpos;
4049 struct it it_copy;
4050 void *it_copy_data = NULL;
4052 eassert (it->s == NULL);
4054 if (STRINGP (it->string))
4056 ptrdiff_t bufpos, charpos;
4057 int base_face_id;
4059 /* No face change past the end of the string (for the case
4060 we are padding with spaces). No face change before the
4061 string start. */
4062 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string)
4063 || (IT_STRING_CHARPOS (*it) == 0 && before_p))
4064 return it->face_id;
4066 if (!it->bidi_p)
4068 /* Set charpos to the position before or after IT's current
4069 position, in the logical order, which in the non-bidi
4070 case is the same as the visual order. */
4071 if (before_p)
4072 charpos = IT_STRING_CHARPOS (*it) - 1;
4073 else if (it->what == IT_COMPOSITION)
4074 /* For composition, we must check the character after the
4075 composition. */
4076 charpos = IT_STRING_CHARPOS (*it) + it->cmp_it.nchars;
4077 else
4078 charpos = IT_STRING_CHARPOS (*it) + 1;
4080 else
4082 if (before_p)
4084 /* With bidi iteration, the character before the current
4085 in the visual order cannot be found by simple
4086 iteration, because "reverse" reordering is not
4087 supported. Instead, we need to use the move_it_*
4088 family of functions. */
4089 /* Ignore face changes before the first visible
4090 character on this display line. */
4091 if (it->current_x <= it->first_visible_x)
4092 return it->face_id;
4093 SAVE_IT (it_copy, *it, it_copy_data);
4094 /* Implementation note: Since move_it_in_display_line
4095 works in the iterator geometry, and thinks the first
4096 character is always the leftmost, even in R2L lines,
4097 we don't need to distinguish between the R2L and L2R
4098 cases here. */
4099 move_it_in_display_line (&it_copy, SCHARS (it_copy.string),
4100 it_copy.current_x - 1, MOVE_TO_X);
4101 charpos = IT_STRING_CHARPOS (it_copy);
4102 RESTORE_IT (it, it, it_copy_data);
4104 else
4106 /* Set charpos to the string position of the character
4107 that comes after IT's current position in the visual
4108 order. */
4109 int n = (it->what == IT_COMPOSITION ? it->cmp_it.nchars : 1);
4111 it_copy = *it;
4112 while (n--)
4113 bidi_move_to_visually_next (&it_copy.bidi_it);
4115 charpos = it_copy.bidi_it.charpos;
4118 eassert (0 <= charpos && charpos <= SCHARS (it->string));
4120 if (it->current.overlay_string_index >= 0)
4121 bufpos = IT_CHARPOS (*it);
4122 else
4123 bufpos = 0;
4125 base_face_id = underlying_face_id (it);
4127 /* Get the face for ASCII, or unibyte. */
4128 face_id = face_at_string_position (it->w,
4129 it->string,
4130 charpos,
4131 bufpos,
4132 &next_check_charpos,
4133 base_face_id, false);
4135 /* Correct the face for charsets different from ASCII. Do it
4136 for the multibyte case only. The face returned above is
4137 suitable for unibyte text if IT->string is unibyte. */
4138 if (STRING_MULTIBYTE (it->string))
4140 struct text_pos pos1 = string_pos (charpos, it->string);
4141 const unsigned char *p = SDATA (it->string) + BYTEPOS (pos1);
4142 int c, len;
4143 struct face *face = FACE_FROM_ID (it->f, face_id);
4145 c = string_char_and_length (p, &len);
4146 face_id = FACE_FOR_CHAR (it->f, face, c, charpos, it->string);
4149 else
4151 struct text_pos pos;
4153 if ((IT_CHARPOS (*it) >= ZV && !before_p)
4154 || (IT_CHARPOS (*it) <= BEGV && before_p))
4155 return it->face_id;
4157 limit = IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT;
4158 pos = it->current.pos;
4160 if (!it->bidi_p)
4162 if (before_p)
4163 DEC_TEXT_POS (pos, it->multibyte_p);
4164 else
4166 if (it->what == IT_COMPOSITION)
4168 /* For composition, we must check the position after
4169 the composition. */
4170 pos.charpos += it->cmp_it.nchars;
4171 pos.bytepos += it->len;
4173 else
4174 INC_TEXT_POS (pos, it->multibyte_p);
4177 else
4179 if (before_p)
4181 /* With bidi iteration, the character before the current
4182 in the visual order cannot be found by simple
4183 iteration, because "reverse" reordering is not
4184 supported. Instead, we need to use the move_it_*
4185 family of functions. */
4186 /* Ignore face changes before the first visible
4187 character on this display line. */
4188 if (it->current_x <= it->first_visible_x)
4189 return it->face_id;
4190 SAVE_IT (it_copy, *it, it_copy_data);
4191 /* Implementation note: Since move_it_in_display_line
4192 works in the iterator geometry, and thinks the first
4193 character is always the leftmost, even in R2L lines,
4194 we don't need to distinguish between the R2L and L2R
4195 cases here. */
4196 move_it_in_display_line (&it_copy, ZV,
4197 it_copy.current_x - 1, MOVE_TO_X);
4198 pos = it_copy.current.pos;
4199 RESTORE_IT (it, it, it_copy_data);
4201 else
4203 /* Set charpos to the buffer position of the character
4204 that comes after IT's current position in the visual
4205 order. */
4206 int n = (it->what == IT_COMPOSITION ? it->cmp_it.nchars : 1);
4208 it_copy = *it;
4209 while (n--)
4210 bidi_move_to_visually_next (&it_copy.bidi_it);
4212 SET_TEXT_POS (pos,
4213 it_copy.bidi_it.charpos, it_copy.bidi_it.bytepos);
4216 eassert (BEGV <= CHARPOS (pos) && CHARPOS (pos) <= ZV);
4218 /* Determine face for CHARSET_ASCII, or unibyte. */
4219 face_id = face_at_buffer_position (it->w,
4220 CHARPOS (pos),
4221 &next_check_charpos,
4222 limit, false, -1);
4224 /* Correct the face for charsets different from ASCII. Do it
4225 for the multibyte case only. The face returned above is
4226 suitable for unibyte text if current_buffer is unibyte. */
4227 if (it->multibyte_p)
4229 int c = FETCH_MULTIBYTE_CHAR (BYTEPOS (pos));
4230 struct face *face = FACE_FROM_ID (it->f, face_id);
4231 face_id = FACE_FOR_CHAR (it->f, face, c, CHARPOS (pos), Qnil);
4235 return face_id;
4240 /***********************************************************************
4241 Invisible text
4242 ***********************************************************************/
4244 /* Set up iterator IT from invisible properties at its current
4245 position. Called from handle_stop. */
4247 static enum prop_handled
4248 handle_invisible_prop (struct it *it)
4250 enum prop_handled handled = HANDLED_NORMALLY;
4251 int invis_p;
4252 Lisp_Object prop;
4254 if (STRINGP (it->string))
4256 Lisp_Object end_charpos, limit, charpos;
4258 /* Get the value of the invisible text property at the
4259 current position. Value will be nil if there is no such
4260 property. */
4261 charpos = make_number (IT_STRING_CHARPOS (*it));
4262 prop = Fget_text_property (charpos, Qinvisible, it->string);
4263 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
4265 if (invis_p && IT_STRING_CHARPOS (*it) < it->end_charpos)
4267 /* Record whether we have to display an ellipsis for the
4268 invisible text. */
4269 int display_ellipsis_p = (invis_p == 2);
4270 ptrdiff_t len, endpos;
4272 handled = HANDLED_RECOMPUTE_PROPS;
4274 /* Get the position at which the next visible text can be
4275 found in IT->string, if any. */
4276 endpos = len = SCHARS (it->string);
4277 XSETINT (limit, len);
4280 end_charpos = Fnext_single_property_change (charpos, Qinvisible,
4281 it->string, limit);
4282 if (INTEGERP (end_charpos))
4284 endpos = XFASTINT (end_charpos);
4285 prop = Fget_text_property (end_charpos, Qinvisible, it->string);
4286 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
4287 if (invis_p == 2)
4288 display_ellipsis_p = true;
4291 while (invis_p && endpos < len);
4293 if (display_ellipsis_p)
4294 it->ellipsis_p = true;
4296 if (endpos < len)
4298 /* Text at END_CHARPOS is visible. Move IT there. */
4299 struct text_pos old;
4300 ptrdiff_t oldpos;
4302 old = it->current.string_pos;
4303 oldpos = CHARPOS (old);
4304 if (it->bidi_p)
4306 if (it->bidi_it.first_elt
4307 && it->bidi_it.charpos < SCHARS (it->string))
4308 bidi_paragraph_init (it->paragraph_embedding,
4309 &it->bidi_it, 1);
4310 /* Bidi-iterate out of the invisible text. */
4313 bidi_move_to_visually_next (&it->bidi_it);
4315 while (oldpos <= it->bidi_it.charpos
4316 && it->bidi_it.charpos < endpos);
4318 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
4319 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
4320 if (IT_CHARPOS (*it) >= endpos)
4321 it->prev_stop = endpos;
4323 else
4325 IT_STRING_CHARPOS (*it) = XFASTINT (end_charpos);
4326 compute_string_pos (&it->current.string_pos, old, it->string);
4329 else
4331 /* The rest of the string is invisible. If this is an
4332 overlay string, proceed with the next overlay string
4333 or whatever comes and return a character from there. */
4334 if (it->current.overlay_string_index >= 0
4335 && !display_ellipsis_p)
4337 next_overlay_string (it);
4338 /* Don't check for overlay strings when we just
4339 finished processing them. */
4340 handled = HANDLED_OVERLAY_STRING_CONSUMED;
4342 else
4344 IT_STRING_CHARPOS (*it) = SCHARS (it->string);
4345 IT_STRING_BYTEPOS (*it) = SBYTES (it->string);
4350 else
4352 ptrdiff_t newpos, next_stop, start_charpos, tem;
4353 Lisp_Object pos, overlay;
4355 /* First of all, is there invisible text at this position? */
4356 tem = start_charpos = IT_CHARPOS (*it);
4357 pos = make_number (tem);
4358 prop = get_char_property_and_overlay (pos, Qinvisible, it->window,
4359 &overlay);
4360 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
4362 /* If we are on invisible text, skip over it. */
4363 if (invis_p && start_charpos < it->end_charpos)
4365 /* Record whether we have to display an ellipsis for the
4366 invisible text. */
4367 int display_ellipsis_p = invis_p == 2;
4369 handled = HANDLED_RECOMPUTE_PROPS;
4371 /* Loop skipping over invisible text. The loop is left at
4372 ZV or with IT on the first char being visible again. */
4375 /* Try to skip some invisible text. Return value is the
4376 position reached which can be equal to where we start
4377 if there is nothing invisible there. This skips both
4378 over invisible text properties and overlays with
4379 invisible property. */
4380 newpos = skip_invisible (tem, &next_stop, ZV, it->window);
4382 /* If we skipped nothing at all we weren't at invisible
4383 text in the first place. If everything to the end of
4384 the buffer was skipped, end the loop. */
4385 if (newpos == tem || newpos >= ZV)
4386 invis_p = 0;
4387 else
4389 /* We skipped some characters but not necessarily
4390 all there are. Check if we ended up on visible
4391 text. Fget_char_property returns the property of
4392 the char before the given position, i.e. if we
4393 get invis_p = 0, this means that the char at
4394 newpos is visible. */
4395 pos = make_number (newpos);
4396 prop = Fget_char_property (pos, Qinvisible, it->window);
4397 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
4400 /* If we ended up on invisible text, proceed to
4401 skip starting with next_stop. */
4402 if (invis_p)
4403 tem = next_stop;
4405 /* If there are adjacent invisible texts, don't lose the
4406 second one's ellipsis. */
4407 if (invis_p == 2)
4408 display_ellipsis_p = true;
4410 while (invis_p);
4412 /* The position newpos is now either ZV or on visible text. */
4413 if (it->bidi_p)
4415 ptrdiff_t bpos = CHAR_TO_BYTE (newpos);
4416 int on_newline
4417 = bpos == ZV_BYTE || FETCH_BYTE (bpos) == '\n';
4418 int after_newline
4419 = newpos <= BEGV || FETCH_BYTE (bpos - 1) == '\n';
4421 /* If the invisible text ends on a newline or on a
4422 character after a newline, we can avoid the costly,
4423 character by character, bidi iteration to NEWPOS, and
4424 instead simply reseat the iterator there. That's
4425 because all bidi reordering information is tossed at
4426 the newline. This is a big win for modes that hide
4427 complete lines, like Outline, Org, etc. */
4428 if (on_newline || after_newline)
4430 struct text_pos tpos;
4431 bidi_dir_t pdir = it->bidi_it.paragraph_dir;
4433 SET_TEXT_POS (tpos, newpos, bpos);
4434 reseat_1 (it, tpos, 0);
4435 /* If we reseat on a newline/ZV, we need to prep the
4436 bidi iterator for advancing to the next character
4437 after the newline/EOB, keeping the current paragraph
4438 direction (so that PRODUCE_GLYPHS does TRT wrt
4439 prepending/appending glyphs to a glyph row). */
4440 if (on_newline)
4442 it->bidi_it.first_elt = 0;
4443 it->bidi_it.paragraph_dir = pdir;
4444 it->bidi_it.ch = (bpos == ZV_BYTE) ? -1 : '\n';
4445 it->bidi_it.nchars = 1;
4446 it->bidi_it.ch_len = 1;
4449 else /* Must use the slow method. */
4451 /* With bidi iteration, the region of invisible text
4452 could start and/or end in the middle of a
4453 non-base embedding level. Therefore, we need to
4454 skip invisible text using the bidi iterator,
4455 starting at IT's current position, until we find
4456 ourselves outside of the invisible text.
4457 Skipping invisible text _after_ bidi iteration
4458 avoids affecting the visual order of the
4459 displayed text when invisible properties are
4460 added or removed. */
4461 if (it->bidi_it.first_elt && it->bidi_it.charpos < ZV)
4463 /* If we were `reseat'ed to a new paragraph,
4464 determine the paragraph base direction. We
4465 need to do it now because
4466 next_element_from_buffer may not have a
4467 chance to do it, if we are going to skip any
4468 text at the beginning, which resets the
4469 FIRST_ELT flag. */
4470 bidi_paragraph_init (it->paragraph_embedding,
4471 &it->bidi_it, 1);
4475 bidi_move_to_visually_next (&it->bidi_it);
4477 while (it->stop_charpos <= it->bidi_it.charpos
4478 && it->bidi_it.charpos < newpos);
4479 IT_CHARPOS (*it) = it->bidi_it.charpos;
4480 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
4481 /* If we overstepped NEWPOS, record its position in
4482 the iterator, so that we skip invisible text if
4483 later the bidi iteration lands us in the
4484 invisible region again. */
4485 if (IT_CHARPOS (*it) >= newpos)
4486 it->prev_stop = newpos;
4489 else
4491 IT_CHARPOS (*it) = newpos;
4492 IT_BYTEPOS (*it) = CHAR_TO_BYTE (newpos);
4495 /* If there are before-strings at the start of invisible
4496 text, and the text is invisible because of a text
4497 property, arrange to show before-strings because 20.x did
4498 it that way. (If the text is invisible because of an
4499 overlay property instead of a text property, this is
4500 already handled in the overlay code.) */
4501 if (NILP (overlay)
4502 && get_overlay_strings (it, it->stop_charpos))
4504 handled = HANDLED_RECOMPUTE_PROPS;
4505 if (it->sp > 0)
4507 it->stack[it->sp - 1].display_ellipsis_p = display_ellipsis_p;
4508 /* The call to get_overlay_strings above recomputes
4509 it->stop_charpos, but it only considers changes
4510 in properties and overlays beyond iterator's
4511 current position. This causes us to miss changes
4512 that happen exactly where the invisible property
4513 ended. So we play it safe here and force the
4514 iterator to check for potential stop positions
4515 immediately after the invisible text. Note that
4516 if get_overlay_strings returns non-zero, it
4517 normally also pushed the iterator stack, so we
4518 need to update the stop position in the slot
4519 below the current one. */
4520 it->stack[it->sp - 1].stop_charpos
4521 = CHARPOS (it->stack[it->sp - 1].current.pos);
4524 else if (display_ellipsis_p)
4526 /* Make sure that the glyphs of the ellipsis will get
4527 correct `charpos' values. If we would not update
4528 it->position here, the glyphs would belong to the
4529 last visible character _before_ the invisible
4530 text, which confuses `set_cursor_from_row'.
4532 We use the last invisible position instead of the
4533 first because this way the cursor is always drawn on
4534 the first "." of the ellipsis, whenever PT is inside
4535 the invisible text. Otherwise the cursor would be
4536 placed _after_ the ellipsis when the point is after the
4537 first invisible character. */
4538 if (!STRINGP (it->object))
4540 it->position.charpos = newpos - 1;
4541 it->position.bytepos = CHAR_TO_BYTE (it->position.charpos);
4543 it->ellipsis_p = true;
4544 /* Let the ellipsis display before
4545 considering any properties of the following char.
4546 Fixes jasonr@gnu.org 01 Oct 07 bug. */
4547 handled = HANDLED_RETURN;
4552 return handled;
4556 /* Make iterator IT return `...' next.
4557 Replaces LEN characters from buffer. */
4559 static void
4560 setup_for_ellipsis (struct it *it, int len)
4562 /* Use the display table definition for `...'. Invalid glyphs
4563 will be handled by the method returning elements from dpvec. */
4564 if (it->dp && VECTORP (DISP_INVIS_VECTOR (it->dp)))
4566 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
4567 it->dpvec = v->contents;
4568 it->dpend = v->contents + v->header.size;
4570 else
4572 /* Default `...'. */
4573 it->dpvec = default_invis_vector;
4574 it->dpend = default_invis_vector + 3;
4577 it->dpvec_char_len = len;
4578 it->current.dpvec_index = 0;
4579 it->dpvec_face_id = -1;
4581 /* Remember the current face id in case glyphs specify faces.
4582 IT's face is restored in set_iterator_to_next.
4583 saved_face_id was set to preceding char's face in handle_stop. */
4584 if (it->saved_face_id < 0 || it->saved_face_id != it->face_id)
4585 it->saved_face_id = it->face_id = DEFAULT_FACE_ID;
4587 it->method = GET_FROM_DISPLAY_VECTOR;
4588 it->ellipsis_p = true;
4593 /***********************************************************************
4594 'display' property
4595 ***********************************************************************/
4597 /* Set up iterator IT from `display' property at its current position.
4598 Called from handle_stop.
4599 We return HANDLED_RETURN if some part of the display property
4600 overrides the display of the buffer text itself.
4601 Otherwise we return HANDLED_NORMALLY. */
4603 static enum prop_handled
4604 handle_display_prop (struct it *it)
4606 Lisp_Object propval, object, overlay;
4607 struct text_pos *position;
4608 ptrdiff_t bufpos;
4609 /* Nonzero if some property replaces the display of the text itself. */
4610 int display_replaced_p = 0;
4612 if (STRINGP (it->string))
4614 object = it->string;
4615 position = &it->current.string_pos;
4616 bufpos = CHARPOS (it->current.pos);
4618 else
4620 XSETWINDOW (object, it->w);
4621 position = &it->current.pos;
4622 bufpos = CHARPOS (*position);
4625 /* Reset those iterator values set from display property values. */
4626 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
4627 it->space_width = Qnil;
4628 it->font_height = Qnil;
4629 it->voffset = 0;
4631 /* We don't support recursive `display' properties, i.e. string
4632 values that have a string `display' property, that have a string
4633 `display' property etc. */
4634 if (!it->string_from_display_prop_p)
4635 it->area = TEXT_AREA;
4637 propval = get_char_property_and_overlay (make_number (position->charpos),
4638 Qdisplay, object, &overlay);
4639 if (NILP (propval))
4640 return HANDLED_NORMALLY;
4641 /* Now OVERLAY is the overlay that gave us this property, or nil
4642 if it was a text property. */
4644 if (!STRINGP (it->string))
4645 object = it->w->contents;
4647 display_replaced_p = handle_display_spec (it, propval, object, overlay,
4648 position, bufpos,
4649 FRAME_WINDOW_P (it->f));
4651 return display_replaced_p ? HANDLED_RETURN : HANDLED_NORMALLY;
4654 /* Subroutine of handle_display_prop. Returns non-zero if the display
4655 specification in SPEC is a replacing specification, i.e. it would
4656 replace the text covered by `display' property with something else,
4657 such as an image or a display string. If SPEC includes any kind or
4658 `(space ...) specification, the value is 2; this is used by
4659 compute_display_string_pos, which see.
4661 See handle_single_display_spec for documentation of arguments.
4662 frame_window_p is non-zero if the window being redisplayed is on a
4663 GUI frame; this argument is used only if IT is NULL, see below.
4665 IT can be NULL, if this is called by the bidi reordering code
4666 through compute_display_string_pos, which see. In that case, this
4667 function only examines SPEC, but does not otherwise "handle" it, in
4668 the sense that it doesn't set up members of IT from the display
4669 spec. */
4670 static int
4671 handle_display_spec (struct it *it, Lisp_Object spec, Lisp_Object object,
4672 Lisp_Object overlay, struct text_pos *position,
4673 ptrdiff_t bufpos, int frame_window_p)
4675 int replacing_p = 0;
4676 int rv;
4678 if (CONSP (spec)
4679 /* Simple specifications. */
4680 && !EQ (XCAR (spec), Qimage)
4681 && !EQ (XCAR (spec), Qspace)
4682 && !EQ (XCAR (spec), Qwhen)
4683 && !EQ (XCAR (spec), Qslice)
4684 && !EQ (XCAR (spec), Qspace_width)
4685 && !EQ (XCAR (spec), Qheight)
4686 && !EQ (XCAR (spec), Qraise)
4687 /* Marginal area specifications. */
4688 && !(CONSP (XCAR (spec)) && EQ (XCAR (XCAR (spec)), Qmargin))
4689 && !EQ (XCAR (spec), Qleft_fringe)
4690 && !EQ (XCAR (spec), Qright_fringe)
4691 && !NILP (XCAR (spec)))
4693 for (; CONSP (spec); spec = XCDR (spec))
4695 if ((rv = handle_single_display_spec (it, XCAR (spec), object,
4696 overlay, position, bufpos,
4697 replacing_p, frame_window_p)))
4699 replacing_p = rv;
4700 /* If some text in a string is replaced, `position' no
4701 longer points to the position of `object'. */
4702 if (!it || STRINGP (object))
4703 break;
4707 else if (VECTORP (spec))
4709 ptrdiff_t i;
4710 for (i = 0; i < ASIZE (spec); ++i)
4711 if ((rv = handle_single_display_spec (it, AREF (spec, i), object,
4712 overlay, position, bufpos,
4713 replacing_p, frame_window_p)))
4715 replacing_p = rv;
4716 /* If some text in a string is replaced, `position' no
4717 longer points to the position of `object'. */
4718 if (!it || STRINGP (object))
4719 break;
4722 else
4724 if ((rv = handle_single_display_spec (it, spec, object, overlay,
4725 position, bufpos, 0,
4726 frame_window_p)))
4727 replacing_p = rv;
4730 return replacing_p;
4733 /* Value is the position of the end of the `display' property starting
4734 at START_POS in OBJECT. */
4736 static struct text_pos
4737 display_prop_end (struct it *it, Lisp_Object object, struct text_pos start_pos)
4739 Lisp_Object end;
4740 struct text_pos end_pos;
4742 end = Fnext_single_char_property_change (make_number (CHARPOS (start_pos)),
4743 Qdisplay, object, Qnil);
4744 CHARPOS (end_pos) = XFASTINT (end);
4745 if (STRINGP (object))
4746 compute_string_pos (&end_pos, start_pos, it->string);
4747 else
4748 BYTEPOS (end_pos) = CHAR_TO_BYTE (XFASTINT (end));
4750 return end_pos;
4754 /* Set up IT from a single `display' property specification SPEC. OBJECT
4755 is the object in which the `display' property was found. *POSITION
4756 is the position in OBJECT at which the `display' property was found.
4757 BUFPOS is the buffer position of OBJECT (different from POSITION if
4758 OBJECT is not a buffer). DISPLAY_REPLACED_P non-zero means that we
4759 previously saw a display specification which already replaced text
4760 display with something else, for example an image; we ignore such
4761 properties after the first one has been processed.
4763 OVERLAY is the overlay this `display' property came from,
4764 or nil if it was a text property.
4766 If SPEC is a `space' or `image' specification, and in some other
4767 cases too, set *POSITION to the position where the `display'
4768 property ends.
4770 If IT is NULL, only examine the property specification in SPEC, but
4771 don't set up IT. In that case, FRAME_WINDOW_P non-zero means SPEC
4772 is intended to be displayed in a window on a GUI frame.
4774 Value is non-zero if something was found which replaces the display
4775 of buffer or string text. */
4777 static int
4778 handle_single_display_spec (struct it *it, Lisp_Object spec, Lisp_Object object,
4779 Lisp_Object overlay, struct text_pos *position,
4780 ptrdiff_t bufpos, int display_replaced_p,
4781 int frame_window_p)
4783 Lisp_Object form;
4784 Lisp_Object location, value;
4785 struct text_pos start_pos = *position;
4786 int valid_p;
4788 /* If SPEC is a list of the form `(when FORM . VALUE)', evaluate FORM.
4789 If the result is non-nil, use VALUE instead of SPEC. */
4790 form = Qt;
4791 if (CONSP (spec) && EQ (XCAR (spec), Qwhen))
4793 spec = XCDR (spec);
4794 if (!CONSP (spec))
4795 return 0;
4796 form = XCAR (spec);
4797 spec = XCDR (spec);
4800 if (!NILP (form) && !EQ (form, Qt))
4802 ptrdiff_t count = SPECPDL_INDEX ();
4803 struct gcpro gcpro1;
4805 /* Bind `object' to the object having the `display' property, a
4806 buffer or string. Bind `position' to the position in the
4807 object where the property was found, and `buffer-position'
4808 to the current position in the buffer. */
4810 if (NILP (object))
4811 XSETBUFFER (object, current_buffer);
4812 specbind (Qobject, object);
4813 specbind (Qposition, make_number (CHARPOS (*position)));
4814 specbind (Qbuffer_position, make_number (bufpos));
4815 GCPRO1 (form);
4816 form = safe_eval (form);
4817 UNGCPRO;
4818 unbind_to (count, Qnil);
4821 if (NILP (form))
4822 return 0;
4824 /* Handle `(height HEIGHT)' specifications. */
4825 if (CONSP (spec)
4826 && EQ (XCAR (spec), Qheight)
4827 && CONSP (XCDR (spec)))
4829 if (it)
4831 if (!FRAME_WINDOW_P (it->f))
4832 return 0;
4834 it->font_height = XCAR (XCDR (spec));
4835 if (!NILP (it->font_height))
4837 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4838 int new_height = -1;
4840 if (CONSP (it->font_height)
4841 && (EQ (XCAR (it->font_height), Qplus)
4842 || EQ (XCAR (it->font_height), Qminus))
4843 && CONSP (XCDR (it->font_height))
4844 && RANGED_INTEGERP (0, XCAR (XCDR (it->font_height)), INT_MAX))
4846 /* `(+ N)' or `(- N)' where N is an integer. */
4847 int steps = XINT (XCAR (XCDR (it->font_height)));
4848 if (EQ (XCAR (it->font_height), Qplus))
4849 steps = - steps;
4850 it->face_id = smaller_face (it->f, it->face_id, steps);
4852 else if (FUNCTIONP (it->font_height))
4854 /* Call function with current height as argument.
4855 Value is the new height. */
4856 Lisp_Object height;
4857 height = safe_call1 (it->font_height,
4858 face->lface[LFACE_HEIGHT_INDEX]);
4859 if (NUMBERP (height))
4860 new_height = XFLOATINT (height);
4862 else if (NUMBERP (it->font_height))
4864 /* Value is a multiple of the canonical char height. */
4865 struct face *f;
4867 f = FACE_FROM_ID (it->f,
4868 lookup_basic_face (it->f, DEFAULT_FACE_ID));
4869 new_height = (XFLOATINT (it->font_height)
4870 * XINT (f->lface[LFACE_HEIGHT_INDEX]));
4872 else
4874 /* Evaluate IT->font_height with `height' bound to the
4875 current specified height to get the new height. */
4876 ptrdiff_t count = SPECPDL_INDEX ();
4878 specbind (Qheight, face->lface[LFACE_HEIGHT_INDEX]);
4879 value = safe_eval (it->font_height);
4880 unbind_to (count, Qnil);
4882 if (NUMBERP (value))
4883 new_height = XFLOATINT (value);
4886 if (new_height > 0)
4887 it->face_id = face_with_height (it->f, it->face_id, new_height);
4891 return 0;
4894 /* Handle `(space-width WIDTH)'. */
4895 if (CONSP (spec)
4896 && EQ (XCAR (spec), Qspace_width)
4897 && CONSP (XCDR (spec)))
4899 if (it)
4901 if (!FRAME_WINDOW_P (it->f))
4902 return 0;
4904 value = XCAR (XCDR (spec));
4905 if (NUMBERP (value) && XFLOATINT (value) > 0)
4906 it->space_width = value;
4909 return 0;
4912 /* Handle `(slice X Y WIDTH HEIGHT)'. */
4913 if (CONSP (spec)
4914 && EQ (XCAR (spec), Qslice))
4916 Lisp_Object tem;
4918 if (it)
4920 if (!FRAME_WINDOW_P (it->f))
4921 return 0;
4923 if (tem = XCDR (spec), CONSP (tem))
4925 it->slice.x = XCAR (tem);
4926 if (tem = XCDR (tem), CONSP (tem))
4928 it->slice.y = XCAR (tem);
4929 if (tem = XCDR (tem), CONSP (tem))
4931 it->slice.width = XCAR (tem);
4932 if (tem = XCDR (tem), CONSP (tem))
4933 it->slice.height = XCAR (tem);
4939 return 0;
4942 /* Handle `(raise FACTOR)'. */
4943 if (CONSP (spec)
4944 && EQ (XCAR (spec), Qraise)
4945 && CONSP (XCDR (spec)))
4947 if (it)
4949 if (!FRAME_WINDOW_P (it->f))
4950 return 0;
4952 #ifdef HAVE_WINDOW_SYSTEM
4953 value = XCAR (XCDR (spec));
4954 if (NUMBERP (value))
4956 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4957 it->voffset = - (XFLOATINT (value)
4958 * (FONT_HEIGHT (face->font)));
4960 #endif /* HAVE_WINDOW_SYSTEM */
4963 return 0;
4966 /* Don't handle the other kinds of display specifications
4967 inside a string that we got from a `display' property. */
4968 if (it && it->string_from_display_prop_p)
4969 return 0;
4971 /* Characters having this form of property are not displayed, so
4972 we have to find the end of the property. */
4973 if (it)
4975 start_pos = *position;
4976 *position = display_prop_end (it, object, start_pos);
4978 value = Qnil;
4980 /* Stop the scan at that end position--we assume that all
4981 text properties change there. */
4982 if (it)
4983 it->stop_charpos = position->charpos;
4985 /* Handle `(left-fringe BITMAP [FACE])'
4986 and `(right-fringe BITMAP [FACE])'. */
4987 if (CONSP (spec)
4988 && (EQ (XCAR (spec), Qleft_fringe)
4989 || EQ (XCAR (spec), Qright_fringe))
4990 && CONSP (XCDR (spec)))
4992 int fringe_bitmap;
4994 if (it)
4996 if (!FRAME_WINDOW_P (it->f))
4997 /* If we return here, POSITION has been advanced
4998 across the text with this property. */
5000 /* Synchronize the bidi iterator with POSITION. This is
5001 needed because we are not going to push the iterator
5002 on behalf of this display property, so there will be
5003 no pop_it call to do this synchronization for us. */
5004 if (it->bidi_p)
5006 it->position = *position;
5007 iterate_out_of_display_property (it);
5008 *position = it->position;
5010 /* If we were to display this fringe bitmap,
5011 next_element_from_image would have reset this flag.
5012 Do the same, to avoid affecting overlays that
5013 follow. */
5014 it->ignore_overlay_strings_at_pos_p = 0;
5015 return 1;
5018 else if (!frame_window_p)
5019 return 1;
5021 #ifdef HAVE_WINDOW_SYSTEM
5022 value = XCAR (XCDR (spec));
5023 if (!SYMBOLP (value)
5024 || !(fringe_bitmap = lookup_fringe_bitmap (value)))
5025 /* If we return here, POSITION has been advanced
5026 across the text with this property. */
5028 if (it && it->bidi_p)
5030 it->position = *position;
5031 iterate_out_of_display_property (it);
5032 *position = it->position;
5034 if (it)
5035 /* Reset this flag like next_element_from_image would. */
5036 it->ignore_overlay_strings_at_pos_p = 0;
5037 return 1;
5040 if (it)
5042 int face_id = lookup_basic_face (it->f, DEFAULT_FACE_ID);
5044 if (CONSP (XCDR (XCDR (spec))))
5046 Lisp_Object face_name = XCAR (XCDR (XCDR (spec)));
5047 int face_id2 = lookup_derived_face (it->f, face_name,
5048 FRINGE_FACE_ID, 0);
5049 if (face_id2 >= 0)
5050 face_id = face_id2;
5053 /* Save current settings of IT so that we can restore them
5054 when we are finished with the glyph property value. */
5055 push_it (it, position);
5057 it->area = TEXT_AREA;
5058 it->what = IT_IMAGE;
5059 it->image_id = -1; /* no image */
5060 it->position = start_pos;
5061 it->object = NILP (object) ? it->w->contents : object;
5062 it->method = GET_FROM_IMAGE;
5063 it->from_overlay = Qnil;
5064 it->face_id = face_id;
5065 it->from_disp_prop_p = true;
5067 /* Say that we haven't consumed the characters with
5068 `display' property yet. The call to pop_it in
5069 set_iterator_to_next will clean this up. */
5070 *position = start_pos;
5072 if (EQ (XCAR (spec), Qleft_fringe))
5074 it->left_user_fringe_bitmap = fringe_bitmap;
5075 it->left_user_fringe_face_id = face_id;
5077 else
5079 it->right_user_fringe_bitmap = fringe_bitmap;
5080 it->right_user_fringe_face_id = face_id;
5083 #endif /* HAVE_WINDOW_SYSTEM */
5084 return 1;
5087 /* Prepare to handle `((margin left-margin) ...)',
5088 `((margin right-margin) ...)' and `((margin nil) ...)'
5089 prefixes for display specifications. */
5090 location = Qunbound;
5091 if (CONSP (spec) && CONSP (XCAR (spec)))
5093 Lisp_Object tem;
5095 value = XCDR (spec);
5096 if (CONSP (value))
5097 value = XCAR (value);
5099 tem = XCAR (spec);
5100 if (EQ (XCAR (tem), Qmargin)
5101 && (tem = XCDR (tem),
5102 tem = CONSP (tem) ? XCAR (tem) : Qnil,
5103 (NILP (tem)
5104 || EQ (tem, Qleft_margin)
5105 || EQ (tem, Qright_margin))))
5106 location = tem;
5109 if (EQ (location, Qunbound))
5111 location = Qnil;
5112 value = spec;
5115 /* After this point, VALUE is the property after any
5116 margin prefix has been stripped. It must be a string,
5117 an image specification, or `(space ...)'.
5119 LOCATION specifies where to display: `left-margin',
5120 `right-margin' or nil. */
5122 valid_p = (STRINGP (value)
5123 #ifdef HAVE_WINDOW_SYSTEM
5124 || ((it ? FRAME_WINDOW_P (it->f) : frame_window_p)
5125 && valid_image_p (value))
5126 #endif /* not HAVE_WINDOW_SYSTEM */
5127 || (CONSP (value) && EQ (XCAR (value), Qspace)));
5129 if (valid_p && !display_replaced_p)
5131 int retval = 1;
5133 if (!it)
5135 /* Callers need to know whether the display spec is any kind
5136 of `(space ...)' spec that is about to affect text-area
5137 display. */
5138 if (CONSP (value) && EQ (XCAR (value), Qspace) && NILP (location))
5139 retval = 2;
5140 return retval;
5143 /* Save current settings of IT so that we can restore them
5144 when we are finished with the glyph property value. */
5145 push_it (it, position);
5146 it->from_overlay = overlay;
5147 it->from_disp_prop_p = true;
5149 if (NILP (location))
5150 it->area = TEXT_AREA;
5151 else if (EQ (location, Qleft_margin))
5152 it->area = LEFT_MARGIN_AREA;
5153 else
5154 it->area = RIGHT_MARGIN_AREA;
5156 if (STRINGP (value))
5158 it->string = value;
5159 it->multibyte_p = STRING_MULTIBYTE (it->string);
5160 it->current.overlay_string_index = -1;
5161 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
5162 it->end_charpos = it->string_nchars = SCHARS (it->string);
5163 it->method = GET_FROM_STRING;
5164 it->stop_charpos = 0;
5165 it->prev_stop = 0;
5166 it->base_level_stop = 0;
5167 it->string_from_display_prop_p = true;
5168 /* Say that we haven't consumed the characters with
5169 `display' property yet. The call to pop_it in
5170 set_iterator_to_next will clean this up. */
5171 if (BUFFERP (object))
5172 *position = start_pos;
5174 /* Force paragraph direction to be that of the parent
5175 object. If the parent object's paragraph direction is
5176 not yet determined, default to L2R. */
5177 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
5178 it->paragraph_embedding = it->bidi_it.paragraph_dir;
5179 else
5180 it->paragraph_embedding = L2R;
5182 /* Set up the bidi iterator for this display string. */
5183 if (it->bidi_p)
5185 it->bidi_it.string.lstring = it->string;
5186 it->bidi_it.string.s = NULL;
5187 it->bidi_it.string.schars = it->end_charpos;
5188 it->bidi_it.string.bufpos = bufpos;
5189 it->bidi_it.string.from_disp_str = 1;
5190 it->bidi_it.string.unibyte = !it->multibyte_p;
5191 it->bidi_it.w = it->w;
5192 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
5195 else if (CONSP (value) && EQ (XCAR (value), Qspace))
5197 it->method = GET_FROM_STRETCH;
5198 it->object = value;
5199 *position = it->position = start_pos;
5200 retval = 1 + (it->area == TEXT_AREA);
5202 #ifdef HAVE_WINDOW_SYSTEM
5203 else
5205 it->what = IT_IMAGE;
5206 it->image_id = lookup_image (it->f, value);
5207 it->position = start_pos;
5208 it->object = NILP (object) ? it->w->contents : object;
5209 it->method = GET_FROM_IMAGE;
5211 /* Say that we haven't consumed the characters with
5212 `display' property yet. The call to pop_it in
5213 set_iterator_to_next will clean this up. */
5214 *position = start_pos;
5216 #endif /* HAVE_WINDOW_SYSTEM */
5218 return retval;
5221 /* Invalid property or property not supported. Restore
5222 POSITION to what it was before. */
5223 *position = start_pos;
5224 return 0;
5227 /* Check if PROP is a display property value whose text should be
5228 treated as intangible. OVERLAY is the overlay from which PROP
5229 came, or nil if it came from a text property. CHARPOS and BYTEPOS
5230 specify the buffer position covered by PROP. */
5233 display_prop_intangible_p (Lisp_Object prop, Lisp_Object overlay,
5234 ptrdiff_t charpos, ptrdiff_t bytepos)
5236 int frame_window_p = FRAME_WINDOW_P (XFRAME (selected_frame));
5237 struct text_pos position;
5239 SET_TEXT_POS (position, charpos, bytepos);
5240 return handle_display_spec (NULL, prop, Qnil, overlay,
5241 &position, charpos, frame_window_p);
5245 /* Return 1 if PROP is a display sub-property value containing STRING.
5247 Implementation note: this and the following function are really
5248 special cases of handle_display_spec and
5249 handle_single_display_spec, and should ideally use the same code.
5250 Until they do, these two pairs must be consistent and must be
5251 modified in sync. */
5253 static int
5254 single_display_spec_string_p (Lisp_Object prop, Lisp_Object string)
5256 if (EQ (string, prop))
5257 return 1;
5259 /* Skip over `when FORM'. */
5260 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
5262 prop = XCDR (prop);
5263 if (!CONSP (prop))
5264 return 0;
5265 /* Actually, the condition following `when' should be eval'ed,
5266 like handle_single_display_spec does, and we should return
5267 zero if it evaluates to nil. However, this function is
5268 called only when the buffer was already displayed and some
5269 glyph in the glyph matrix was found to come from a display
5270 string. Therefore, the condition was already evaluated, and
5271 the result was non-nil, otherwise the display string wouldn't
5272 have been displayed and we would have never been called for
5273 this property. Thus, we can skip the evaluation and assume
5274 its result is non-nil. */
5275 prop = XCDR (prop);
5278 if (CONSP (prop))
5279 /* Skip over `margin LOCATION'. */
5280 if (EQ (XCAR (prop), Qmargin))
5282 prop = XCDR (prop);
5283 if (!CONSP (prop))
5284 return 0;
5286 prop = XCDR (prop);
5287 if (!CONSP (prop))
5288 return 0;
5291 return EQ (prop, string) || (CONSP (prop) && EQ (XCAR (prop), string));
5295 /* Return 1 if STRING appears in the `display' property PROP. */
5297 static int
5298 display_prop_string_p (Lisp_Object prop, Lisp_Object string)
5300 if (CONSP (prop)
5301 && !EQ (XCAR (prop), Qwhen)
5302 && !(CONSP (XCAR (prop)) && EQ (Qmargin, XCAR (XCAR (prop)))))
5304 /* A list of sub-properties. */
5305 while (CONSP (prop))
5307 if (single_display_spec_string_p (XCAR (prop), string))
5308 return 1;
5309 prop = XCDR (prop);
5312 else if (VECTORP (prop))
5314 /* A vector of sub-properties. */
5315 ptrdiff_t i;
5316 for (i = 0; i < ASIZE (prop); ++i)
5317 if (single_display_spec_string_p (AREF (prop, i), string))
5318 return 1;
5320 else
5321 return single_display_spec_string_p (prop, string);
5323 return 0;
5326 /* Look for STRING in overlays and text properties in the current
5327 buffer, between character positions FROM and TO (excluding TO).
5328 BACK_P non-zero means look back (in this case, TO is supposed to be
5329 less than FROM).
5330 Value is the first character position where STRING was found, or
5331 zero if it wasn't found before hitting TO.
5333 This function may only use code that doesn't eval because it is
5334 called asynchronously from note_mouse_highlight. */
5336 static ptrdiff_t
5337 string_buffer_position_lim (Lisp_Object string,
5338 ptrdiff_t from, ptrdiff_t to, int back_p)
5340 Lisp_Object limit, prop, pos;
5341 int found = 0;
5343 pos = make_number (max (from, BEGV));
5345 if (!back_p) /* looking forward */
5347 limit = make_number (min (to, ZV));
5348 while (!found && !EQ (pos, limit))
5350 prop = Fget_char_property (pos, Qdisplay, Qnil);
5351 if (!NILP (prop) && display_prop_string_p (prop, string))
5352 found = 1;
5353 else
5354 pos = Fnext_single_char_property_change (pos, Qdisplay, Qnil,
5355 limit);
5358 else /* looking back */
5360 limit = make_number (max (to, BEGV));
5361 while (!found && !EQ (pos, limit))
5363 prop = Fget_char_property (pos, Qdisplay, Qnil);
5364 if (!NILP (prop) && display_prop_string_p (prop, string))
5365 found = 1;
5366 else
5367 pos = Fprevious_single_char_property_change (pos, Qdisplay, Qnil,
5368 limit);
5372 return found ? XINT (pos) : 0;
5375 /* Determine which buffer position in current buffer STRING comes from.
5376 AROUND_CHARPOS is an approximate position where it could come from.
5377 Value is the buffer position or 0 if it couldn't be determined.
5379 This function is necessary because we don't record buffer positions
5380 in glyphs generated from strings (to keep struct glyph small).
5381 This function may only use code that doesn't eval because it is
5382 called asynchronously from note_mouse_highlight. */
5384 static ptrdiff_t
5385 string_buffer_position (Lisp_Object string, ptrdiff_t around_charpos)
5387 const int MAX_DISTANCE = 1000;
5388 ptrdiff_t found = string_buffer_position_lim (string, around_charpos,
5389 around_charpos + MAX_DISTANCE,
5392 if (!found)
5393 found = string_buffer_position_lim (string, around_charpos,
5394 around_charpos - MAX_DISTANCE, 1);
5395 return found;
5400 /***********************************************************************
5401 `composition' property
5402 ***********************************************************************/
5404 /* Set up iterator IT from `composition' property at its current
5405 position. Called from handle_stop. */
5407 static enum prop_handled
5408 handle_composition_prop (struct it *it)
5410 Lisp_Object prop, string;
5411 ptrdiff_t pos, pos_byte, start, end;
5413 if (STRINGP (it->string))
5415 unsigned char *s;
5417 pos = IT_STRING_CHARPOS (*it);
5418 pos_byte = IT_STRING_BYTEPOS (*it);
5419 string = it->string;
5420 s = SDATA (string) + pos_byte;
5421 it->c = STRING_CHAR (s);
5423 else
5425 pos = IT_CHARPOS (*it);
5426 pos_byte = IT_BYTEPOS (*it);
5427 string = Qnil;
5428 it->c = FETCH_CHAR (pos_byte);
5431 /* If there's a valid composition and point is not inside of the
5432 composition (in the case that the composition is from the current
5433 buffer), draw a glyph composed from the composition components. */
5434 if (find_composition (pos, -1, &start, &end, &prop, string)
5435 && composition_valid_p (start, end, prop)
5436 && (STRINGP (it->string) || (PT <= start || PT >= end)))
5438 if (start < pos)
5439 /* As we can't handle this situation (perhaps font-lock added
5440 a new composition), we just return here hoping that next
5441 redisplay will detect this composition much earlier. */
5442 return HANDLED_NORMALLY;
5443 if (start != pos)
5445 if (STRINGP (it->string))
5446 pos_byte = string_char_to_byte (it->string, start);
5447 else
5448 pos_byte = CHAR_TO_BYTE (start);
5450 it->cmp_it.id = get_composition_id (start, pos_byte, end - start,
5451 prop, string);
5453 if (it->cmp_it.id >= 0)
5455 it->cmp_it.ch = -1;
5456 it->cmp_it.nchars = COMPOSITION_LENGTH (prop);
5457 it->cmp_it.nglyphs = -1;
5461 return HANDLED_NORMALLY;
5466 /***********************************************************************
5467 Overlay strings
5468 ***********************************************************************/
5470 /* The following structure is used to record overlay strings for
5471 later sorting in load_overlay_strings. */
5473 struct overlay_entry
5475 Lisp_Object overlay;
5476 Lisp_Object string;
5477 EMACS_INT priority;
5478 int after_string_p;
5482 /* Set up iterator IT from overlay strings at its current position.
5483 Called from handle_stop. */
5485 static enum prop_handled
5486 handle_overlay_change (struct it *it)
5488 if (!STRINGP (it->string) && get_overlay_strings (it, 0))
5489 return HANDLED_RECOMPUTE_PROPS;
5490 else
5491 return HANDLED_NORMALLY;
5495 /* Set up the next overlay string for delivery by IT, if there is an
5496 overlay string to deliver. Called by set_iterator_to_next when the
5497 end of the current overlay string is reached. If there are more
5498 overlay strings to display, IT->string and
5499 IT->current.overlay_string_index are set appropriately here.
5500 Otherwise IT->string is set to nil. */
5502 static void
5503 next_overlay_string (struct it *it)
5505 ++it->current.overlay_string_index;
5506 if (it->current.overlay_string_index == it->n_overlay_strings)
5508 /* No more overlay strings. Restore IT's settings to what
5509 they were before overlay strings were processed, and
5510 continue to deliver from current_buffer. */
5512 it->ellipsis_p = (it->stack[it->sp - 1].display_ellipsis_p != 0);
5513 pop_it (it);
5514 eassert (it->sp > 0
5515 || (NILP (it->string)
5516 && it->method == GET_FROM_BUFFER
5517 && it->stop_charpos >= BEGV
5518 && it->stop_charpos <= it->end_charpos));
5519 it->current.overlay_string_index = -1;
5520 it->n_overlay_strings = 0;
5521 it->overlay_strings_charpos = -1;
5522 /* If there's an empty display string on the stack, pop the
5523 stack, to resync the bidi iterator with IT's position. Such
5524 empty strings are pushed onto the stack in
5525 get_overlay_strings_1. */
5526 if (it->sp > 0 && STRINGP (it->string) && !SCHARS (it->string))
5527 pop_it (it);
5529 /* If we're at the end of the buffer, record that we have
5530 processed the overlay strings there already, so that
5531 next_element_from_buffer doesn't try it again. */
5532 if (NILP (it->string) && IT_CHARPOS (*it) >= it->end_charpos)
5533 it->overlay_strings_at_end_processed_p = true;
5535 else
5537 /* There are more overlay strings to process. If
5538 IT->current.overlay_string_index has advanced to a position
5539 where we must load IT->overlay_strings with more strings, do
5540 it. We must load at the IT->overlay_strings_charpos where
5541 IT->n_overlay_strings was originally computed; when invisible
5542 text is present, this might not be IT_CHARPOS (Bug#7016). */
5543 int i = it->current.overlay_string_index % OVERLAY_STRING_CHUNK_SIZE;
5545 if (it->current.overlay_string_index && i == 0)
5546 load_overlay_strings (it, it->overlay_strings_charpos);
5548 /* Initialize IT to deliver display elements from the overlay
5549 string. */
5550 it->string = it->overlay_strings[i];
5551 it->multibyte_p = STRING_MULTIBYTE (it->string);
5552 SET_TEXT_POS (it->current.string_pos, 0, 0);
5553 it->method = GET_FROM_STRING;
5554 it->stop_charpos = 0;
5555 it->end_charpos = SCHARS (it->string);
5556 if (it->cmp_it.stop_pos >= 0)
5557 it->cmp_it.stop_pos = 0;
5558 it->prev_stop = 0;
5559 it->base_level_stop = 0;
5561 /* Set up the bidi iterator for this overlay string. */
5562 if (it->bidi_p)
5564 it->bidi_it.string.lstring = it->string;
5565 it->bidi_it.string.s = NULL;
5566 it->bidi_it.string.schars = SCHARS (it->string);
5567 it->bidi_it.string.bufpos = it->overlay_strings_charpos;
5568 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
5569 it->bidi_it.string.unibyte = !it->multibyte_p;
5570 it->bidi_it.w = it->w;
5571 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
5575 CHECK_IT (it);
5579 /* Compare two overlay_entry structures E1 and E2. Used as a
5580 comparison function for qsort in load_overlay_strings. Overlay
5581 strings for the same position are sorted so that
5583 1. All after-strings come in front of before-strings, except
5584 when they come from the same overlay.
5586 2. Within after-strings, strings are sorted so that overlay strings
5587 from overlays with higher priorities come first.
5589 2. Within before-strings, strings are sorted so that overlay
5590 strings from overlays with higher priorities come last.
5592 Value is analogous to strcmp. */
5595 static int
5596 compare_overlay_entries (const void *e1, const void *e2)
5598 struct overlay_entry const *entry1 = e1;
5599 struct overlay_entry const *entry2 = e2;
5600 int result;
5602 if (entry1->after_string_p != entry2->after_string_p)
5604 /* Let after-strings appear in front of before-strings if
5605 they come from different overlays. */
5606 if (EQ (entry1->overlay, entry2->overlay))
5607 result = entry1->after_string_p ? 1 : -1;
5608 else
5609 result = entry1->after_string_p ? -1 : 1;
5611 else if (entry1->priority != entry2->priority)
5613 if (entry1->after_string_p)
5614 /* After-strings sorted in order of decreasing priority. */
5615 result = entry2->priority < entry1->priority ? -1 : 1;
5616 else
5617 /* Before-strings sorted in order of increasing priority. */
5618 result = entry1->priority < entry2->priority ? -1 : 1;
5620 else
5621 result = 0;
5623 return result;
5627 /* Load the vector IT->overlay_strings with overlay strings from IT's
5628 current buffer position, or from CHARPOS if that is > 0. Set
5629 IT->n_overlays to the total number of overlay strings found.
5631 Overlay strings are processed OVERLAY_STRING_CHUNK_SIZE strings at
5632 a time. On entry into load_overlay_strings,
5633 IT->current.overlay_string_index gives the number of overlay
5634 strings that have already been loaded by previous calls to this
5635 function.
5637 IT->add_overlay_start contains an additional overlay start
5638 position to consider for taking overlay strings from, if non-zero.
5639 This position comes into play when the overlay has an `invisible'
5640 property, and both before and after-strings. When we've skipped to
5641 the end of the overlay, because of its `invisible' property, we
5642 nevertheless want its before-string to appear.
5643 IT->add_overlay_start will contain the overlay start position
5644 in this case.
5646 Overlay strings are sorted so that after-string strings come in
5647 front of before-string strings. Within before and after-strings,
5648 strings are sorted by overlay priority. See also function
5649 compare_overlay_entries. */
5651 static void
5652 load_overlay_strings (struct it *it, ptrdiff_t charpos)
5654 Lisp_Object overlay, window, str, invisible;
5655 struct Lisp_Overlay *ov;
5656 ptrdiff_t start, end;
5657 ptrdiff_t n = 0, i, j;
5658 int invis_p;
5659 struct overlay_entry entriesbuf[20];
5660 ptrdiff_t size = ARRAYELTS (entriesbuf);
5661 struct overlay_entry *entries = entriesbuf;
5662 USE_SAFE_ALLOCA;
5664 if (charpos <= 0)
5665 charpos = IT_CHARPOS (*it);
5667 /* Append the overlay string STRING of overlay OVERLAY to vector
5668 `entries' which has size `size' and currently contains `n'
5669 elements. AFTER_P non-zero means STRING is an after-string of
5670 OVERLAY. */
5671 #define RECORD_OVERLAY_STRING(OVERLAY, STRING, AFTER_P) \
5672 do \
5674 Lisp_Object priority; \
5676 if (n == size) \
5678 struct overlay_entry *old = entries; \
5679 SAFE_NALLOCA (entries, 2, size); \
5680 memcpy (entries, old, size * sizeof *entries); \
5681 size *= 2; \
5684 entries[n].string = (STRING); \
5685 entries[n].overlay = (OVERLAY); \
5686 priority = Foverlay_get ((OVERLAY), Qpriority); \
5687 entries[n].priority = INTEGERP (priority) ? XINT (priority) : 0; \
5688 entries[n].after_string_p = (AFTER_P); \
5689 ++n; \
5691 while (0)
5693 /* Process overlay before the overlay center. */
5694 for (ov = current_buffer->overlays_before; ov; ov = ov->next)
5696 XSETMISC (overlay, ov);
5697 eassert (OVERLAYP (overlay));
5698 start = OVERLAY_POSITION (OVERLAY_START (overlay));
5699 end = OVERLAY_POSITION (OVERLAY_END (overlay));
5701 if (end < charpos)
5702 break;
5704 /* Skip this overlay if it doesn't start or end at IT's current
5705 position. */
5706 if (end != charpos && start != charpos)
5707 continue;
5709 /* Skip this overlay if it doesn't apply to IT->w. */
5710 window = Foverlay_get (overlay, Qwindow);
5711 if (WINDOWP (window) && XWINDOW (window) != it->w)
5712 continue;
5714 /* If the text ``under'' the overlay is invisible, both before-
5715 and after-strings from this overlay are visible; start and
5716 end position are indistinguishable. */
5717 invisible = Foverlay_get (overlay, Qinvisible);
5718 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
5720 /* If overlay has a non-empty before-string, record it. */
5721 if ((start == charpos || (end == charpos && invis_p))
5722 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
5723 && SCHARS (str))
5724 RECORD_OVERLAY_STRING (overlay, str, 0);
5726 /* If overlay has a non-empty after-string, record it. */
5727 if ((end == charpos || (start == charpos && invis_p))
5728 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
5729 && SCHARS (str))
5730 RECORD_OVERLAY_STRING (overlay, str, 1);
5733 /* Process overlays after the overlay center. */
5734 for (ov = current_buffer->overlays_after; ov; ov = ov->next)
5736 XSETMISC (overlay, ov);
5737 eassert (OVERLAYP (overlay));
5738 start = OVERLAY_POSITION (OVERLAY_START (overlay));
5739 end = OVERLAY_POSITION (OVERLAY_END (overlay));
5741 if (start > charpos)
5742 break;
5744 /* Skip this overlay if it doesn't start or end at IT's current
5745 position. */
5746 if (end != charpos && start != charpos)
5747 continue;
5749 /* Skip this overlay if it doesn't apply to IT->w. */
5750 window = Foverlay_get (overlay, Qwindow);
5751 if (WINDOWP (window) && XWINDOW (window) != it->w)
5752 continue;
5754 /* If the text ``under'' the overlay is invisible, it has a zero
5755 dimension, and both before- and after-strings apply. */
5756 invisible = Foverlay_get (overlay, Qinvisible);
5757 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
5759 /* If overlay has a non-empty before-string, record it. */
5760 if ((start == charpos || (end == charpos && invis_p))
5761 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
5762 && SCHARS (str))
5763 RECORD_OVERLAY_STRING (overlay, str, 0);
5765 /* If overlay has a non-empty after-string, record it. */
5766 if ((end == charpos || (start == charpos && invis_p))
5767 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
5768 && SCHARS (str))
5769 RECORD_OVERLAY_STRING (overlay, str, 1);
5772 #undef RECORD_OVERLAY_STRING
5774 /* Sort entries. */
5775 if (n > 1)
5776 qsort (entries, n, sizeof *entries, compare_overlay_entries);
5778 /* Record number of overlay strings, and where we computed it. */
5779 it->n_overlay_strings = n;
5780 it->overlay_strings_charpos = charpos;
5782 /* IT->current.overlay_string_index is the number of overlay strings
5783 that have already been consumed by IT. Copy some of the
5784 remaining overlay strings to IT->overlay_strings. */
5785 i = 0;
5786 j = it->current.overlay_string_index;
5787 while (i < OVERLAY_STRING_CHUNK_SIZE && j < n)
5789 it->overlay_strings[i] = entries[j].string;
5790 it->string_overlays[i++] = entries[j++].overlay;
5793 CHECK_IT (it);
5794 SAFE_FREE ();
5798 /* Get the first chunk of overlay strings at IT's current buffer
5799 position, or at CHARPOS if that is > 0. Value is non-zero if at
5800 least one overlay string was found. */
5802 static int
5803 get_overlay_strings_1 (struct it *it, ptrdiff_t charpos, int compute_stop_p)
5805 /* Get the first OVERLAY_STRING_CHUNK_SIZE overlay strings to
5806 process. This fills IT->overlay_strings with strings, and sets
5807 IT->n_overlay_strings to the total number of strings to process.
5808 IT->pos.overlay_string_index has to be set temporarily to zero
5809 because load_overlay_strings needs this; it must be set to -1
5810 when no overlay strings are found because a zero value would
5811 indicate a position in the first overlay string. */
5812 it->current.overlay_string_index = 0;
5813 load_overlay_strings (it, charpos);
5815 /* If we found overlay strings, set up IT to deliver display
5816 elements from the first one. Otherwise set up IT to deliver
5817 from current_buffer. */
5818 if (it->n_overlay_strings)
5820 /* Make sure we know settings in current_buffer, so that we can
5821 restore meaningful values when we're done with the overlay
5822 strings. */
5823 if (compute_stop_p)
5824 compute_stop_pos (it);
5825 eassert (it->face_id >= 0);
5827 /* Save IT's settings. They are restored after all overlay
5828 strings have been processed. */
5829 eassert (!compute_stop_p || it->sp == 0);
5831 /* When called from handle_stop, there might be an empty display
5832 string loaded. In that case, don't bother saving it. But
5833 don't use this optimization with the bidi iterator, since we
5834 need the corresponding pop_it call to resync the bidi
5835 iterator's position with IT's position, after we are done
5836 with the overlay strings. (The corresponding call to pop_it
5837 in case of an empty display string is in
5838 next_overlay_string.) */
5839 if (!(!it->bidi_p
5840 && STRINGP (it->string) && !SCHARS (it->string)))
5841 push_it (it, NULL);
5843 /* Set up IT to deliver display elements from the first overlay
5844 string. */
5845 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
5846 it->string = it->overlay_strings[0];
5847 it->from_overlay = Qnil;
5848 it->stop_charpos = 0;
5849 eassert (STRINGP (it->string));
5850 it->end_charpos = SCHARS (it->string);
5851 it->prev_stop = 0;
5852 it->base_level_stop = 0;
5853 it->multibyte_p = STRING_MULTIBYTE (it->string);
5854 it->method = GET_FROM_STRING;
5855 it->from_disp_prop_p = 0;
5857 /* Force paragraph direction to be that of the parent
5858 buffer. */
5859 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
5860 it->paragraph_embedding = it->bidi_it.paragraph_dir;
5861 else
5862 it->paragraph_embedding = L2R;
5864 /* Set up the bidi iterator for this overlay string. */
5865 if (it->bidi_p)
5867 ptrdiff_t pos = (charpos > 0 ? charpos : IT_CHARPOS (*it));
5869 it->bidi_it.string.lstring = it->string;
5870 it->bidi_it.string.s = NULL;
5871 it->bidi_it.string.schars = SCHARS (it->string);
5872 it->bidi_it.string.bufpos = pos;
5873 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
5874 it->bidi_it.string.unibyte = !it->multibyte_p;
5875 it->bidi_it.w = it->w;
5876 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
5878 return 1;
5881 it->current.overlay_string_index = -1;
5882 return 0;
5885 static int
5886 get_overlay_strings (struct it *it, ptrdiff_t charpos)
5888 it->string = Qnil;
5889 it->method = GET_FROM_BUFFER;
5891 (void) get_overlay_strings_1 (it, charpos, 1);
5893 CHECK_IT (it);
5895 /* Value is non-zero if we found at least one overlay string. */
5896 return STRINGP (it->string);
5901 /***********************************************************************
5902 Saving and restoring state
5903 ***********************************************************************/
5905 /* Save current settings of IT on IT->stack. Called, for example,
5906 before setting up IT for an overlay string, to be able to restore
5907 IT's settings to what they were after the overlay string has been
5908 processed. If POSITION is non-NULL, it is the position to save on
5909 the stack instead of IT->position. */
5911 static void
5912 push_it (struct it *it, struct text_pos *position)
5914 struct iterator_stack_entry *p;
5916 eassert (it->sp < IT_STACK_SIZE);
5917 p = it->stack + it->sp;
5919 p->stop_charpos = it->stop_charpos;
5920 p->prev_stop = it->prev_stop;
5921 p->base_level_stop = it->base_level_stop;
5922 p->cmp_it = it->cmp_it;
5923 eassert (it->face_id >= 0);
5924 p->face_id = it->face_id;
5925 p->string = it->string;
5926 p->method = it->method;
5927 p->from_overlay = it->from_overlay;
5928 switch (p->method)
5930 case GET_FROM_IMAGE:
5931 p->u.image.object = it->object;
5932 p->u.image.image_id = it->image_id;
5933 p->u.image.slice = it->slice;
5934 break;
5935 case GET_FROM_STRETCH:
5936 p->u.stretch.object = it->object;
5937 break;
5939 p->position = position ? *position : it->position;
5940 p->current = it->current;
5941 p->end_charpos = it->end_charpos;
5942 p->string_nchars = it->string_nchars;
5943 p->area = it->area;
5944 p->multibyte_p = it->multibyte_p;
5945 p->avoid_cursor_p = it->avoid_cursor_p;
5946 p->space_width = it->space_width;
5947 p->font_height = it->font_height;
5948 p->voffset = it->voffset;
5949 p->string_from_display_prop_p = it->string_from_display_prop_p;
5950 p->string_from_prefix_prop_p = it->string_from_prefix_prop_p;
5951 p->display_ellipsis_p = 0;
5952 p->line_wrap = it->line_wrap;
5953 p->bidi_p = it->bidi_p;
5954 p->paragraph_embedding = it->paragraph_embedding;
5955 p->from_disp_prop_p = it->from_disp_prop_p;
5956 ++it->sp;
5958 /* Save the state of the bidi iterator as well. */
5959 if (it->bidi_p)
5960 bidi_push_it (&it->bidi_it);
5963 static void
5964 iterate_out_of_display_property (struct it *it)
5966 int buffer_p = !STRINGP (it->string);
5967 ptrdiff_t eob = (buffer_p ? ZV : it->end_charpos);
5968 ptrdiff_t bob = (buffer_p ? BEGV : 0);
5970 eassert (eob >= CHARPOS (it->position) && CHARPOS (it->position) >= bob);
5972 /* Maybe initialize paragraph direction. If we are at the beginning
5973 of a new paragraph, next_element_from_buffer may not have a
5974 chance to do that. */
5975 if (it->bidi_it.first_elt && it->bidi_it.charpos < eob)
5976 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
5977 /* prev_stop can be zero, so check against BEGV as well. */
5978 while (it->bidi_it.charpos >= bob
5979 && it->prev_stop <= it->bidi_it.charpos
5980 && it->bidi_it.charpos < CHARPOS (it->position)
5981 && it->bidi_it.charpos < eob)
5982 bidi_move_to_visually_next (&it->bidi_it);
5983 /* Record the stop_pos we just crossed, for when we cross it
5984 back, maybe. */
5985 if (it->bidi_it.charpos > CHARPOS (it->position))
5986 it->prev_stop = CHARPOS (it->position);
5987 /* If we ended up not where pop_it put us, resync IT's
5988 positional members with the bidi iterator. */
5989 if (it->bidi_it.charpos != CHARPOS (it->position))
5990 SET_TEXT_POS (it->position, it->bidi_it.charpos, it->bidi_it.bytepos);
5991 if (buffer_p)
5992 it->current.pos = it->position;
5993 else
5994 it->current.string_pos = it->position;
5997 /* Restore IT's settings from IT->stack. Called, for example, when no
5998 more overlay strings must be processed, and we return to delivering
5999 display elements from a buffer, or when the end of a string from a
6000 `display' property is reached and we return to delivering display
6001 elements from an overlay string, or from a buffer. */
6003 static void
6004 pop_it (struct it *it)
6006 struct iterator_stack_entry *p;
6007 int from_display_prop = it->from_disp_prop_p;
6009 eassert (it->sp > 0);
6010 --it->sp;
6011 p = it->stack + it->sp;
6012 it->stop_charpos = p->stop_charpos;
6013 it->prev_stop = p->prev_stop;
6014 it->base_level_stop = p->base_level_stop;
6015 it->cmp_it = p->cmp_it;
6016 it->face_id = p->face_id;
6017 it->current = p->current;
6018 it->position = p->position;
6019 it->string = p->string;
6020 it->from_overlay = p->from_overlay;
6021 if (NILP (it->string))
6022 SET_TEXT_POS (it->current.string_pos, -1, -1);
6023 it->method = p->method;
6024 switch (it->method)
6026 case GET_FROM_IMAGE:
6027 it->image_id = p->u.image.image_id;
6028 it->object = p->u.image.object;
6029 it->slice = p->u.image.slice;
6030 break;
6031 case GET_FROM_STRETCH:
6032 it->object = p->u.stretch.object;
6033 break;
6034 case GET_FROM_BUFFER:
6035 it->object = it->w->contents;
6036 break;
6037 case GET_FROM_STRING:
6039 struct face *face = FACE_FROM_ID (it->f, it->face_id);
6041 /* Restore the face_box_p flag, since it could have been
6042 overwritten by the face of the object that we just finished
6043 displaying. */
6044 if (face)
6045 it->face_box_p = face->box != FACE_NO_BOX;
6046 it->object = it->string;
6048 break;
6049 case GET_FROM_DISPLAY_VECTOR:
6050 if (it->s)
6051 it->method = GET_FROM_C_STRING;
6052 else if (STRINGP (it->string))
6053 it->method = GET_FROM_STRING;
6054 else
6056 it->method = GET_FROM_BUFFER;
6057 it->object = it->w->contents;
6060 it->end_charpos = p->end_charpos;
6061 it->string_nchars = p->string_nchars;
6062 it->area = p->area;
6063 it->multibyte_p = p->multibyte_p;
6064 it->avoid_cursor_p = p->avoid_cursor_p;
6065 it->space_width = p->space_width;
6066 it->font_height = p->font_height;
6067 it->voffset = p->voffset;
6068 it->string_from_display_prop_p = p->string_from_display_prop_p;
6069 it->string_from_prefix_prop_p = p->string_from_prefix_prop_p;
6070 it->line_wrap = p->line_wrap;
6071 it->bidi_p = p->bidi_p;
6072 it->paragraph_embedding = p->paragraph_embedding;
6073 it->from_disp_prop_p = p->from_disp_prop_p;
6074 if (it->bidi_p)
6076 bidi_pop_it (&it->bidi_it);
6077 /* Bidi-iterate until we get out of the portion of text, if any,
6078 covered by a `display' text property or by an overlay with
6079 `display' property. (We cannot just jump there, because the
6080 internal coherency of the bidi iterator state can not be
6081 preserved across such jumps.) We also must determine the
6082 paragraph base direction if the overlay we just processed is
6083 at the beginning of a new paragraph. */
6084 if (from_display_prop
6085 && (it->method == GET_FROM_BUFFER || it->method == GET_FROM_STRING))
6086 iterate_out_of_display_property (it);
6088 eassert ((BUFFERP (it->object)
6089 && IT_CHARPOS (*it) == it->bidi_it.charpos
6090 && IT_BYTEPOS (*it) == it->bidi_it.bytepos)
6091 || (STRINGP (it->object)
6092 && IT_STRING_CHARPOS (*it) == it->bidi_it.charpos
6093 && IT_STRING_BYTEPOS (*it) == it->bidi_it.bytepos)
6094 || (CONSP (it->object) && it->method == GET_FROM_STRETCH));
6100 /***********************************************************************
6101 Moving over lines
6102 ***********************************************************************/
6104 /* Set IT's current position to the previous line start. */
6106 static void
6107 back_to_previous_line_start (struct it *it)
6109 ptrdiff_t cp = IT_CHARPOS (*it), bp = IT_BYTEPOS (*it);
6111 DEC_BOTH (cp, bp);
6112 IT_CHARPOS (*it) = find_newline_no_quit (cp, bp, -1, &IT_BYTEPOS (*it));
6116 /* Move IT to the next line start.
6118 Value is non-zero if a newline was found. Set *SKIPPED_P to 1 if
6119 we skipped over part of the text (as opposed to moving the iterator
6120 continuously over the text). Otherwise, don't change the value
6121 of *SKIPPED_P.
6123 If BIDI_IT_PREV is non-NULL, store into it the state of the bidi
6124 iterator on the newline, if it was found.
6126 Newlines may come from buffer text, overlay strings, or strings
6127 displayed via the `display' property. That's the reason we can't
6128 simply use find_newline_no_quit.
6130 Note that this function may not skip over invisible text that is so
6131 because of text properties and immediately follows a newline. If
6132 it would, function reseat_at_next_visible_line_start, when called
6133 from set_iterator_to_next, would effectively make invisible
6134 characters following a newline part of the wrong glyph row, which
6135 leads to wrong cursor motion. */
6137 static int
6138 forward_to_next_line_start (struct it *it, int *skipped_p,
6139 struct bidi_it *bidi_it_prev)
6141 ptrdiff_t old_selective;
6142 int newline_found_p, n;
6143 const int MAX_NEWLINE_DISTANCE = 500;
6145 /* If already on a newline, just consume it to avoid unintended
6146 skipping over invisible text below. */
6147 if (it->what == IT_CHARACTER
6148 && it->c == '\n'
6149 && CHARPOS (it->position) == IT_CHARPOS (*it))
6151 if (it->bidi_p && bidi_it_prev)
6152 *bidi_it_prev = it->bidi_it;
6153 set_iterator_to_next (it, 0);
6154 it->c = 0;
6155 return 1;
6158 /* Don't handle selective display in the following. It's (a)
6159 unnecessary because it's done by the caller, and (b) leads to an
6160 infinite recursion because next_element_from_ellipsis indirectly
6161 calls this function. */
6162 old_selective = it->selective;
6163 it->selective = 0;
6165 /* Scan for a newline within MAX_NEWLINE_DISTANCE display elements
6166 from buffer text. */
6167 for (n = newline_found_p = 0;
6168 !newline_found_p && n < MAX_NEWLINE_DISTANCE;
6169 n += STRINGP (it->string) ? 0 : 1)
6171 if (!get_next_display_element (it))
6172 return 0;
6173 newline_found_p = it->what == IT_CHARACTER && it->c == '\n';
6174 if (newline_found_p && it->bidi_p && bidi_it_prev)
6175 *bidi_it_prev = it->bidi_it;
6176 set_iterator_to_next (it, 0);
6179 /* If we didn't find a newline near enough, see if we can use a
6180 short-cut. */
6181 if (!newline_found_p)
6183 ptrdiff_t bytepos, start = IT_CHARPOS (*it);
6184 ptrdiff_t limit = find_newline_no_quit (start, IT_BYTEPOS (*it),
6185 1, &bytepos);
6186 Lisp_Object pos;
6188 eassert (!STRINGP (it->string));
6190 /* If there isn't any `display' property in sight, and no
6191 overlays, we can just use the position of the newline in
6192 buffer text. */
6193 if (it->stop_charpos >= limit
6194 || ((pos = Fnext_single_property_change (make_number (start),
6195 Qdisplay, Qnil,
6196 make_number (limit)),
6197 NILP (pos))
6198 && next_overlay_change (start) == ZV))
6200 if (!it->bidi_p)
6202 IT_CHARPOS (*it) = limit;
6203 IT_BYTEPOS (*it) = bytepos;
6205 else
6207 struct bidi_it bprev;
6209 /* Help bidi.c avoid expensive searches for display
6210 properties and overlays, by telling it that there are
6211 none up to `limit'. */
6212 if (it->bidi_it.disp_pos < limit)
6214 it->bidi_it.disp_pos = limit;
6215 it->bidi_it.disp_prop = 0;
6217 do {
6218 bprev = it->bidi_it;
6219 bidi_move_to_visually_next (&it->bidi_it);
6220 } while (it->bidi_it.charpos != limit);
6221 IT_CHARPOS (*it) = limit;
6222 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6223 if (bidi_it_prev)
6224 *bidi_it_prev = bprev;
6226 *skipped_p = newline_found_p = true;
6228 else
6230 while (get_next_display_element (it)
6231 && !newline_found_p)
6233 newline_found_p = ITERATOR_AT_END_OF_LINE_P (it);
6234 if (newline_found_p && it->bidi_p && bidi_it_prev)
6235 *bidi_it_prev = it->bidi_it;
6236 set_iterator_to_next (it, 0);
6241 it->selective = old_selective;
6242 return newline_found_p;
6246 /* Set IT's current position to the previous visible line start. Skip
6247 invisible text that is so either due to text properties or due to
6248 selective display. Caution: this does not change IT->current_x and
6249 IT->hpos. */
6251 static void
6252 back_to_previous_visible_line_start (struct it *it)
6254 while (IT_CHARPOS (*it) > BEGV)
6256 back_to_previous_line_start (it);
6258 if (IT_CHARPOS (*it) <= BEGV)
6259 break;
6261 /* If selective > 0, then lines indented more than its value are
6262 invisible. */
6263 if (it->selective > 0
6264 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
6265 it->selective))
6266 continue;
6268 /* Check the newline before point for invisibility. */
6270 Lisp_Object prop;
6271 prop = Fget_char_property (make_number (IT_CHARPOS (*it) - 1),
6272 Qinvisible, it->window);
6273 if (TEXT_PROP_MEANS_INVISIBLE (prop))
6274 continue;
6277 if (IT_CHARPOS (*it) <= BEGV)
6278 break;
6281 struct it it2;
6282 void *it2data = NULL;
6283 ptrdiff_t pos;
6284 ptrdiff_t beg, end;
6285 Lisp_Object val, overlay;
6287 SAVE_IT (it2, *it, it2data);
6289 /* If newline is part of a composition, continue from start of composition */
6290 if (find_composition (IT_CHARPOS (*it), -1, &beg, &end, &val, Qnil)
6291 && beg < IT_CHARPOS (*it))
6292 goto replaced;
6294 /* If newline is replaced by a display property, find start of overlay
6295 or interval and continue search from that point. */
6296 pos = --IT_CHARPOS (it2);
6297 --IT_BYTEPOS (it2);
6298 it2.sp = 0;
6299 bidi_unshelve_cache (NULL, 0);
6300 it2.string_from_display_prop_p = 0;
6301 it2.from_disp_prop_p = 0;
6302 if (handle_display_prop (&it2) == HANDLED_RETURN
6303 && !NILP (val = get_char_property_and_overlay
6304 (make_number (pos), Qdisplay, Qnil, &overlay))
6305 && (OVERLAYP (overlay)
6306 ? (beg = OVERLAY_POSITION (OVERLAY_START (overlay)))
6307 : get_property_and_range (pos, Qdisplay, &val, &beg, &end, Qnil)))
6309 RESTORE_IT (it, it, it2data);
6310 goto replaced;
6313 /* Newline is not replaced by anything -- so we are done. */
6314 RESTORE_IT (it, it, it2data);
6315 break;
6317 replaced:
6318 if (beg < BEGV)
6319 beg = BEGV;
6320 IT_CHARPOS (*it) = beg;
6321 IT_BYTEPOS (*it) = buf_charpos_to_bytepos (current_buffer, beg);
6325 it->continuation_lines_width = 0;
6327 eassert (IT_CHARPOS (*it) >= BEGV);
6328 eassert (IT_CHARPOS (*it) == BEGV
6329 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
6330 CHECK_IT (it);
6334 /* Reseat iterator IT at the previous visible line start. Skip
6335 invisible text that is so either due to text properties or due to
6336 selective display. At the end, update IT's overlay information,
6337 face information etc. */
6339 void
6340 reseat_at_previous_visible_line_start (struct it *it)
6342 back_to_previous_visible_line_start (it);
6343 reseat (it, it->current.pos, 1);
6344 CHECK_IT (it);
6348 /* Reseat iterator IT on the next visible line start in the current
6349 buffer. ON_NEWLINE_P non-zero means position IT on the newline
6350 preceding the line start. Skip over invisible text that is so
6351 because of selective display. Compute faces, overlays etc at the
6352 new position. Note that this function does not skip over text that
6353 is invisible because of text properties. */
6355 static void
6356 reseat_at_next_visible_line_start (struct it *it, int on_newline_p)
6358 int newline_found_p, skipped_p = 0;
6359 struct bidi_it bidi_it_prev;
6361 newline_found_p = forward_to_next_line_start (it, &skipped_p, &bidi_it_prev);
6363 /* Skip over lines that are invisible because they are indented
6364 more than the value of IT->selective. */
6365 if (it->selective > 0)
6366 while (IT_CHARPOS (*it) < ZV
6367 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
6368 it->selective))
6370 eassert (IT_BYTEPOS (*it) == BEGV
6371 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
6372 newline_found_p =
6373 forward_to_next_line_start (it, &skipped_p, &bidi_it_prev);
6376 /* Position on the newline if that's what's requested. */
6377 if (on_newline_p && newline_found_p)
6379 if (STRINGP (it->string))
6381 if (IT_STRING_CHARPOS (*it) > 0)
6383 if (!it->bidi_p)
6385 --IT_STRING_CHARPOS (*it);
6386 --IT_STRING_BYTEPOS (*it);
6388 else
6390 /* We need to restore the bidi iterator to the state
6391 it had on the newline, and resync the IT's
6392 position with that. */
6393 it->bidi_it = bidi_it_prev;
6394 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
6395 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
6399 else if (IT_CHARPOS (*it) > BEGV)
6401 if (!it->bidi_p)
6403 --IT_CHARPOS (*it);
6404 --IT_BYTEPOS (*it);
6406 else
6408 /* We need to restore the bidi iterator to the state it
6409 had on the newline and resync IT with that. */
6410 it->bidi_it = bidi_it_prev;
6411 IT_CHARPOS (*it) = it->bidi_it.charpos;
6412 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6414 reseat (it, it->current.pos, 0);
6417 else if (skipped_p)
6418 reseat (it, it->current.pos, 0);
6420 CHECK_IT (it);
6425 /***********************************************************************
6426 Changing an iterator's position
6427 ***********************************************************************/
6429 /* Change IT's current position to POS in current_buffer. If FORCE_P
6430 is non-zero, always check for text properties at the new position.
6431 Otherwise, text properties are only looked up if POS >=
6432 IT->check_charpos of a property. */
6434 static void
6435 reseat (struct it *it, struct text_pos pos, int force_p)
6437 ptrdiff_t original_pos = IT_CHARPOS (*it);
6439 reseat_1 (it, pos, 0);
6441 /* Determine where to check text properties. Avoid doing it
6442 where possible because text property lookup is very expensive. */
6443 if (force_p
6444 || CHARPOS (pos) > it->stop_charpos
6445 || CHARPOS (pos) < original_pos)
6447 if (it->bidi_p)
6449 /* For bidi iteration, we need to prime prev_stop and
6450 base_level_stop with our best estimations. */
6451 /* Implementation note: Of course, POS is not necessarily a
6452 stop position, so assigning prev_pos to it is a lie; we
6453 should have called compute_stop_backwards. However, if
6454 the current buffer does not include any R2L characters,
6455 that call would be a waste of cycles, because the
6456 iterator will never move back, and thus never cross this
6457 "fake" stop position. So we delay that backward search
6458 until the time we really need it, in next_element_from_buffer. */
6459 if (CHARPOS (pos) != it->prev_stop)
6460 it->prev_stop = CHARPOS (pos);
6461 if (CHARPOS (pos) < it->base_level_stop)
6462 it->base_level_stop = 0; /* meaning it's unknown */
6463 handle_stop (it);
6465 else
6467 handle_stop (it);
6468 it->prev_stop = it->base_level_stop = 0;
6473 CHECK_IT (it);
6477 /* Change IT's buffer position to POS. SET_STOP_P non-zero means set
6478 IT->stop_pos to POS, also. */
6480 static void
6481 reseat_1 (struct it *it, struct text_pos pos, int set_stop_p)
6483 /* Don't call this function when scanning a C string. */
6484 eassert (it->s == NULL);
6486 /* POS must be a reasonable value. */
6487 eassert (CHARPOS (pos) >= BEGV && CHARPOS (pos) <= ZV);
6489 it->current.pos = it->position = pos;
6490 it->end_charpos = ZV;
6491 it->dpvec = NULL;
6492 it->current.dpvec_index = -1;
6493 it->current.overlay_string_index = -1;
6494 IT_STRING_CHARPOS (*it) = -1;
6495 IT_STRING_BYTEPOS (*it) = -1;
6496 it->string = Qnil;
6497 it->method = GET_FROM_BUFFER;
6498 it->object = it->w->contents;
6499 it->area = TEXT_AREA;
6500 it->multibyte_p = !NILP (BVAR (current_buffer, enable_multibyte_characters));
6501 it->sp = 0;
6502 it->string_from_display_prop_p = 0;
6503 it->string_from_prefix_prop_p = 0;
6505 it->from_disp_prop_p = 0;
6506 it->face_before_selective_p = 0;
6507 if (it->bidi_p)
6509 bidi_init_it (IT_CHARPOS (*it), IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
6510 &it->bidi_it);
6511 bidi_unshelve_cache (NULL, 0);
6512 it->bidi_it.paragraph_dir = NEUTRAL_DIR;
6513 it->bidi_it.string.s = NULL;
6514 it->bidi_it.string.lstring = Qnil;
6515 it->bidi_it.string.bufpos = 0;
6516 it->bidi_it.string.from_disp_str = 0;
6517 it->bidi_it.string.unibyte = 0;
6518 it->bidi_it.w = it->w;
6521 if (set_stop_p)
6523 it->stop_charpos = CHARPOS (pos);
6524 it->base_level_stop = CHARPOS (pos);
6526 /* This make the information stored in it->cmp_it invalidate. */
6527 it->cmp_it.id = -1;
6531 /* Set up IT for displaying a string, starting at CHARPOS in window W.
6532 If S is non-null, it is a C string to iterate over. Otherwise,
6533 STRING gives a Lisp string to iterate over.
6535 If PRECISION > 0, don't return more then PRECISION number of
6536 characters from the string.
6538 If FIELD_WIDTH > 0, return padding spaces until FIELD_WIDTH
6539 characters have been returned. FIELD_WIDTH < 0 means an infinite
6540 field width.
6542 MULTIBYTE = 0 means disable processing of multibyte characters,
6543 MULTIBYTE > 0 means enable it,
6544 MULTIBYTE < 0 means use IT->multibyte_p.
6546 IT must be initialized via a prior call to init_iterator before
6547 calling this function. */
6549 static void
6550 reseat_to_string (struct it *it, const char *s, Lisp_Object string,
6551 ptrdiff_t charpos, ptrdiff_t precision, int field_width,
6552 int multibyte)
6554 /* No text property checks performed by default, but see below. */
6555 it->stop_charpos = -1;
6557 /* Set iterator position and end position. */
6558 memset (&it->current, 0, sizeof it->current);
6559 it->current.overlay_string_index = -1;
6560 it->current.dpvec_index = -1;
6561 eassert (charpos >= 0);
6563 /* If STRING is specified, use its multibyteness, otherwise use the
6564 setting of MULTIBYTE, if specified. */
6565 if (multibyte >= 0)
6566 it->multibyte_p = multibyte > 0;
6568 /* Bidirectional reordering of strings is controlled by the default
6569 value of bidi-display-reordering. Don't try to reorder while
6570 loading loadup.el, as the necessary character property tables are
6571 not yet available. */
6572 it->bidi_p =
6573 NILP (Vpurify_flag)
6574 && !NILP (BVAR (&buffer_defaults, bidi_display_reordering));
6576 if (s == NULL)
6578 eassert (STRINGP (string));
6579 it->string = string;
6580 it->s = NULL;
6581 it->end_charpos = it->string_nchars = SCHARS (string);
6582 it->method = GET_FROM_STRING;
6583 it->current.string_pos = string_pos (charpos, string);
6585 if (it->bidi_p)
6587 it->bidi_it.string.lstring = string;
6588 it->bidi_it.string.s = NULL;
6589 it->bidi_it.string.schars = it->end_charpos;
6590 it->bidi_it.string.bufpos = 0;
6591 it->bidi_it.string.from_disp_str = 0;
6592 it->bidi_it.string.unibyte = !it->multibyte_p;
6593 it->bidi_it.w = it->w;
6594 bidi_init_it (charpos, IT_STRING_BYTEPOS (*it),
6595 FRAME_WINDOW_P (it->f), &it->bidi_it);
6598 else
6600 it->s = (const unsigned char *) s;
6601 it->string = Qnil;
6603 /* Note that we use IT->current.pos, not it->current.string_pos,
6604 for displaying C strings. */
6605 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
6606 if (it->multibyte_p)
6608 it->current.pos = c_string_pos (charpos, s, 1);
6609 it->end_charpos = it->string_nchars = number_of_chars (s, 1);
6611 else
6613 IT_CHARPOS (*it) = IT_BYTEPOS (*it) = charpos;
6614 it->end_charpos = it->string_nchars = strlen (s);
6617 if (it->bidi_p)
6619 it->bidi_it.string.lstring = Qnil;
6620 it->bidi_it.string.s = (const unsigned char *) s;
6621 it->bidi_it.string.schars = it->end_charpos;
6622 it->bidi_it.string.bufpos = 0;
6623 it->bidi_it.string.from_disp_str = 0;
6624 it->bidi_it.string.unibyte = !it->multibyte_p;
6625 it->bidi_it.w = it->w;
6626 bidi_init_it (charpos, IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
6627 &it->bidi_it);
6629 it->method = GET_FROM_C_STRING;
6632 /* PRECISION > 0 means don't return more than PRECISION characters
6633 from the string. */
6634 if (precision > 0 && it->end_charpos - charpos > precision)
6636 it->end_charpos = it->string_nchars = charpos + precision;
6637 if (it->bidi_p)
6638 it->bidi_it.string.schars = it->end_charpos;
6641 /* FIELD_WIDTH > 0 means pad with spaces until FIELD_WIDTH
6642 characters have been returned. FIELD_WIDTH == 0 means don't pad,
6643 FIELD_WIDTH < 0 means infinite field width. This is useful for
6644 padding with `-' at the end of a mode line. */
6645 if (field_width < 0)
6646 field_width = INFINITY;
6647 /* Implementation note: We deliberately don't enlarge
6648 it->bidi_it.string.schars here to fit it->end_charpos, because
6649 the bidi iterator cannot produce characters out of thin air. */
6650 if (field_width > it->end_charpos - charpos)
6651 it->end_charpos = charpos + field_width;
6653 /* Use the standard display table for displaying strings. */
6654 if (DISP_TABLE_P (Vstandard_display_table))
6655 it->dp = XCHAR_TABLE (Vstandard_display_table);
6657 it->stop_charpos = charpos;
6658 it->prev_stop = charpos;
6659 it->base_level_stop = 0;
6660 if (it->bidi_p)
6662 it->bidi_it.first_elt = 1;
6663 it->bidi_it.paragraph_dir = NEUTRAL_DIR;
6664 it->bidi_it.disp_pos = -1;
6666 if (s == NULL && it->multibyte_p)
6668 ptrdiff_t endpos = SCHARS (it->string);
6669 if (endpos > it->end_charpos)
6670 endpos = it->end_charpos;
6671 composition_compute_stop_pos (&it->cmp_it, charpos, -1, endpos,
6672 it->string);
6674 CHECK_IT (it);
6679 /***********************************************************************
6680 Iteration
6681 ***********************************************************************/
6683 /* Map enum it_method value to corresponding next_element_from_* function. */
6685 static int (* get_next_element[NUM_IT_METHODS]) (struct it *it) =
6687 next_element_from_buffer,
6688 next_element_from_display_vector,
6689 next_element_from_string,
6690 next_element_from_c_string,
6691 next_element_from_image,
6692 next_element_from_stretch
6695 #define GET_NEXT_DISPLAY_ELEMENT(it) (*get_next_element[(it)->method]) (it)
6698 /* Return 1 iff a character at CHARPOS (and BYTEPOS) is composed
6699 (possibly with the following characters). */
6701 #define CHAR_COMPOSED_P(IT,CHARPOS,BYTEPOS,END_CHARPOS) \
6702 ((IT)->cmp_it.id >= 0 \
6703 || ((IT)->cmp_it.stop_pos == (CHARPOS) \
6704 && composition_reseat_it (&(IT)->cmp_it, CHARPOS, BYTEPOS, \
6705 END_CHARPOS, (IT)->w, \
6706 FACE_FROM_ID ((IT)->f, (IT)->face_id), \
6707 (IT)->string)))
6710 /* Lookup the char-table Vglyphless_char_display for character C (-1
6711 if we want information for no-font case), and return the display
6712 method symbol. By side-effect, update it->what and
6713 it->glyphless_method. This function is called from
6714 get_next_display_element for each character element, and from
6715 x_produce_glyphs when no suitable font was found. */
6717 Lisp_Object
6718 lookup_glyphless_char_display (int c, struct it *it)
6720 Lisp_Object glyphless_method = Qnil;
6722 if (CHAR_TABLE_P (Vglyphless_char_display)
6723 && CHAR_TABLE_EXTRA_SLOTS (XCHAR_TABLE (Vglyphless_char_display)) >= 1)
6725 if (c >= 0)
6727 glyphless_method = CHAR_TABLE_REF (Vglyphless_char_display, c);
6728 if (CONSP (glyphless_method))
6729 glyphless_method = FRAME_WINDOW_P (it->f)
6730 ? XCAR (glyphless_method)
6731 : XCDR (glyphless_method);
6733 else
6734 glyphless_method = XCHAR_TABLE (Vglyphless_char_display)->extras[0];
6737 retry:
6738 if (NILP (glyphless_method))
6740 if (c >= 0)
6741 /* The default is to display the character by a proper font. */
6742 return Qnil;
6743 /* The default for the no-font case is to display an empty box. */
6744 glyphless_method = Qempty_box;
6746 if (EQ (glyphless_method, Qzero_width))
6748 if (c >= 0)
6749 return glyphless_method;
6750 /* This method can't be used for the no-font case. */
6751 glyphless_method = Qempty_box;
6753 if (EQ (glyphless_method, Qthin_space))
6754 it->glyphless_method = GLYPHLESS_DISPLAY_THIN_SPACE;
6755 else if (EQ (glyphless_method, Qempty_box))
6756 it->glyphless_method = GLYPHLESS_DISPLAY_EMPTY_BOX;
6757 else if (EQ (glyphless_method, Qhex_code))
6758 it->glyphless_method = GLYPHLESS_DISPLAY_HEX_CODE;
6759 else if (STRINGP (glyphless_method))
6760 it->glyphless_method = GLYPHLESS_DISPLAY_ACRONYM;
6761 else
6763 /* Invalid value. We use the default method. */
6764 glyphless_method = Qnil;
6765 goto retry;
6767 it->what = IT_GLYPHLESS;
6768 return glyphless_method;
6771 /* Merge escape glyph face and cache the result. */
6773 static struct frame *last_escape_glyph_frame = NULL;
6774 static int last_escape_glyph_face_id = (1 << FACE_ID_BITS);
6775 static int last_escape_glyph_merged_face_id = 0;
6777 static int
6778 merge_escape_glyph_face (struct it *it)
6780 int face_id;
6782 if (it->f == last_escape_glyph_frame
6783 && it->face_id == last_escape_glyph_face_id)
6784 face_id = last_escape_glyph_merged_face_id;
6785 else
6787 /* Merge the `escape-glyph' face into the current face. */
6788 face_id = merge_faces (it->f, Qescape_glyph, 0, it->face_id);
6789 last_escape_glyph_frame = it->f;
6790 last_escape_glyph_face_id = it->face_id;
6791 last_escape_glyph_merged_face_id = face_id;
6793 return face_id;
6796 /* Likewise for glyphless glyph face. */
6798 static struct frame *last_glyphless_glyph_frame = NULL;
6799 static int last_glyphless_glyph_face_id = (1 << FACE_ID_BITS);
6800 static int last_glyphless_glyph_merged_face_id = 0;
6803 merge_glyphless_glyph_face (struct it *it)
6805 int face_id;
6807 if (it->f == last_glyphless_glyph_frame
6808 && it->face_id == last_glyphless_glyph_face_id)
6809 face_id = last_glyphless_glyph_merged_face_id;
6810 else
6812 /* Merge the `glyphless-char' face into the current face. */
6813 face_id = merge_faces (it->f, Qglyphless_char, 0, it->face_id);
6814 last_glyphless_glyph_frame = it->f;
6815 last_glyphless_glyph_face_id = it->face_id;
6816 last_glyphless_glyph_merged_face_id = face_id;
6818 return face_id;
6821 /* Load IT's display element fields with information about the next
6822 display element from the current position of IT. Value is zero if
6823 end of buffer (or C string) is reached. */
6825 static int
6826 get_next_display_element (struct it *it)
6828 /* Non-zero means that we found a display element. Zero means that
6829 we hit the end of what we iterate over. Performance note: the
6830 function pointer `method' used here turns out to be faster than
6831 using a sequence of if-statements. */
6832 int success_p;
6834 get_next:
6835 success_p = GET_NEXT_DISPLAY_ELEMENT (it);
6837 if (it->what == IT_CHARACTER)
6839 /* UAX#9, L4: "A character is depicted by a mirrored glyph if
6840 and only if (a) the resolved directionality of that character
6841 is R..." */
6842 /* FIXME: Do we need an exception for characters from display
6843 tables? */
6844 if (it->bidi_p && it->bidi_it.type == STRONG_R
6845 && !inhibit_bidi_mirroring)
6846 it->c = bidi_mirror_char (it->c);
6847 /* Map via display table or translate control characters.
6848 IT->c, IT->len etc. have been set to the next character by
6849 the function call above. If we have a display table, and it
6850 contains an entry for IT->c, translate it. Don't do this if
6851 IT->c itself comes from a display table, otherwise we could
6852 end up in an infinite recursion. (An alternative could be to
6853 count the recursion depth of this function and signal an
6854 error when a certain maximum depth is reached.) Is it worth
6855 it? */
6856 if (success_p && it->dpvec == NULL)
6858 Lisp_Object dv;
6859 struct charset *unibyte = CHARSET_FROM_ID (charset_unibyte);
6860 int nonascii_space_p = 0;
6861 int nonascii_hyphen_p = 0;
6862 int c = it->c; /* This is the character to display. */
6864 if (! it->multibyte_p && ! ASCII_CHAR_P (c))
6866 eassert (SINGLE_BYTE_CHAR_P (c));
6867 if (unibyte_display_via_language_environment)
6869 c = DECODE_CHAR (unibyte, c);
6870 if (c < 0)
6871 c = BYTE8_TO_CHAR (it->c);
6873 else
6874 c = BYTE8_TO_CHAR (it->c);
6877 if (it->dp
6878 && (dv = DISP_CHAR_VECTOR (it->dp, c),
6879 VECTORP (dv)))
6881 struct Lisp_Vector *v = XVECTOR (dv);
6883 /* Return the first character from the display table
6884 entry, if not empty. If empty, don't display the
6885 current character. */
6886 if (v->header.size)
6888 it->dpvec_char_len = it->len;
6889 it->dpvec = v->contents;
6890 it->dpend = v->contents + v->header.size;
6891 it->current.dpvec_index = 0;
6892 it->dpvec_face_id = -1;
6893 it->saved_face_id = it->face_id;
6894 it->method = GET_FROM_DISPLAY_VECTOR;
6895 it->ellipsis_p = 0;
6897 else
6899 set_iterator_to_next (it, 0);
6901 goto get_next;
6904 if (! NILP (lookup_glyphless_char_display (c, it)))
6906 if (it->what == IT_GLYPHLESS)
6907 goto done;
6908 /* Don't display this character. */
6909 set_iterator_to_next (it, 0);
6910 goto get_next;
6913 /* If `nobreak-char-display' is non-nil, we display
6914 non-ASCII spaces and hyphens specially. */
6915 if (! ASCII_CHAR_P (c) && ! NILP (Vnobreak_char_display))
6917 if (c == 0xA0)
6918 nonascii_space_p = true;
6919 else if (c == 0xAD || c == 0x2010 || c == 0x2011)
6920 nonascii_hyphen_p = true;
6923 /* Translate control characters into `\003' or `^C' form.
6924 Control characters coming from a display table entry are
6925 currently not translated because we use IT->dpvec to hold
6926 the translation. This could easily be changed but I
6927 don't believe that it is worth doing.
6929 The characters handled by `nobreak-char-display' must be
6930 translated too.
6932 Non-printable characters and raw-byte characters are also
6933 translated to octal form. */
6934 if (((c < ' ' || c == 127) /* ASCII control chars. */
6935 ? (it->area != TEXT_AREA
6936 /* In mode line, treat \n, \t like other crl chars. */
6937 || (c != '\t'
6938 && it->glyph_row
6939 && (it->glyph_row->mode_line_p || it->avoid_cursor_p))
6940 || (c != '\n' && c != '\t'))
6941 : (nonascii_space_p
6942 || nonascii_hyphen_p
6943 || CHAR_BYTE8_P (c)
6944 || ! CHAR_PRINTABLE_P (c))))
6946 /* C is a control character, non-ASCII space/hyphen,
6947 raw-byte, or a non-printable character which must be
6948 displayed either as '\003' or as `^C' where the '\\'
6949 and '^' can be defined in the display table. Fill
6950 IT->ctl_chars with glyphs for what we have to
6951 display. Then, set IT->dpvec to these glyphs. */
6952 Lisp_Object gc;
6953 int ctl_len;
6954 int face_id;
6955 int lface_id = 0;
6956 int escape_glyph;
6958 /* Handle control characters with ^. */
6960 if (ASCII_CHAR_P (c) && it->ctl_arrow_p)
6962 int g;
6964 g = '^'; /* default glyph for Control */
6965 /* Set IT->ctl_chars[0] to the glyph for `^'. */
6966 if (it->dp
6967 && (gc = DISP_CTRL_GLYPH (it->dp), GLYPH_CODE_P (gc)))
6969 g = GLYPH_CODE_CHAR (gc);
6970 lface_id = GLYPH_CODE_FACE (gc);
6973 face_id = (lface_id
6974 ? merge_faces (it->f, Qt, lface_id, it->face_id)
6975 : merge_escape_glyph_face (it));
6977 XSETINT (it->ctl_chars[0], g);
6978 XSETINT (it->ctl_chars[1], c ^ 0100);
6979 ctl_len = 2;
6980 goto display_control;
6983 /* Handle non-ascii space in the mode where it only gets
6984 highlighting. */
6986 if (nonascii_space_p && EQ (Vnobreak_char_display, Qt))
6988 /* Merge `nobreak-space' into the current face. */
6989 face_id = merge_faces (it->f, Qnobreak_space, 0,
6990 it->face_id);
6991 XSETINT (it->ctl_chars[0], ' ');
6992 ctl_len = 1;
6993 goto display_control;
6996 /* Handle sequences that start with the "escape glyph". */
6998 /* the default escape glyph is \. */
6999 escape_glyph = '\\';
7001 if (it->dp
7002 && (gc = DISP_ESCAPE_GLYPH (it->dp), GLYPH_CODE_P (gc)))
7004 escape_glyph = GLYPH_CODE_CHAR (gc);
7005 lface_id = GLYPH_CODE_FACE (gc);
7008 face_id = (lface_id
7009 ? merge_faces (it->f, Qt, lface_id, it->face_id)
7010 : merge_escape_glyph_face (it));
7012 /* Draw non-ASCII hyphen with just highlighting: */
7014 if (nonascii_hyphen_p && EQ (Vnobreak_char_display, Qt))
7016 XSETINT (it->ctl_chars[0], '-');
7017 ctl_len = 1;
7018 goto display_control;
7021 /* Draw non-ASCII space/hyphen with escape glyph: */
7023 if (nonascii_space_p || nonascii_hyphen_p)
7025 XSETINT (it->ctl_chars[0], escape_glyph);
7026 XSETINT (it->ctl_chars[1], nonascii_space_p ? ' ' : '-');
7027 ctl_len = 2;
7028 goto display_control;
7032 char str[10];
7033 int len, i;
7035 if (CHAR_BYTE8_P (c))
7036 /* Display \200 instead of \17777600. */
7037 c = CHAR_TO_BYTE8 (c);
7038 len = sprintf (str, "%03o", c);
7040 XSETINT (it->ctl_chars[0], escape_glyph);
7041 for (i = 0; i < len; i++)
7042 XSETINT (it->ctl_chars[i + 1], str[i]);
7043 ctl_len = len + 1;
7046 display_control:
7047 /* Set up IT->dpvec and return first character from it. */
7048 it->dpvec_char_len = it->len;
7049 it->dpvec = it->ctl_chars;
7050 it->dpend = it->dpvec + ctl_len;
7051 it->current.dpvec_index = 0;
7052 it->dpvec_face_id = face_id;
7053 it->saved_face_id = it->face_id;
7054 it->method = GET_FROM_DISPLAY_VECTOR;
7055 it->ellipsis_p = 0;
7056 goto get_next;
7058 it->char_to_display = c;
7060 else if (success_p)
7062 it->char_to_display = it->c;
7066 #ifdef HAVE_WINDOW_SYSTEM
7067 /* Adjust face id for a multibyte character. There are no multibyte
7068 character in unibyte text. */
7069 if ((it->what == IT_CHARACTER || it->what == IT_COMPOSITION)
7070 && it->multibyte_p
7071 && success_p
7072 && FRAME_WINDOW_P (it->f))
7074 struct face *face = FACE_FROM_ID (it->f, it->face_id);
7076 if (it->what == IT_COMPOSITION && it->cmp_it.ch >= 0)
7078 /* Automatic composition with glyph-string. */
7079 Lisp_Object gstring = composition_gstring_from_id (it->cmp_it.id);
7081 it->face_id = face_for_font (it->f, LGSTRING_FONT (gstring), face);
7083 else
7085 ptrdiff_t pos = (it->s ? -1
7086 : STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
7087 : IT_CHARPOS (*it));
7088 int c;
7090 if (it->what == IT_CHARACTER)
7091 c = it->char_to_display;
7092 else
7094 struct composition *cmp = composition_table[it->cmp_it.id];
7095 int i;
7097 c = ' ';
7098 for (i = 0; i < cmp->glyph_len; i++)
7099 /* TAB in a composition means display glyphs with
7100 padding space on the left or right. */
7101 if ((c = COMPOSITION_GLYPH (cmp, i)) != '\t')
7102 break;
7104 it->face_id = FACE_FOR_CHAR (it->f, face, c, pos, it->string);
7107 #endif /* HAVE_WINDOW_SYSTEM */
7109 done:
7110 /* Is this character the last one of a run of characters with
7111 box? If yes, set IT->end_of_box_run_p to 1. */
7112 if (it->face_box_p
7113 && it->s == NULL)
7115 if (it->method == GET_FROM_STRING && it->sp)
7117 int face_id = underlying_face_id (it);
7118 struct face *face = FACE_FROM_ID (it->f, face_id);
7120 if (face)
7122 if (face->box == FACE_NO_BOX)
7124 /* If the box comes from face properties in a
7125 display string, check faces in that string. */
7126 int string_face_id = face_after_it_pos (it);
7127 it->end_of_box_run_p
7128 = (FACE_FROM_ID (it->f, string_face_id)->box
7129 == FACE_NO_BOX);
7131 /* Otherwise, the box comes from the underlying face.
7132 If this is the last string character displayed, check
7133 the next buffer location. */
7134 else if ((IT_STRING_CHARPOS (*it) >= SCHARS (it->string) - 1)
7135 /* n_overlay_strings is unreliable unless
7136 overlay_string_index is non-negative. */
7137 && ((it->current.overlay_string_index >= 0
7138 && (it->current.overlay_string_index
7139 == it->n_overlay_strings - 1))
7140 /* A string from display property. */
7141 || it->from_disp_prop_p))
7143 ptrdiff_t ignore;
7144 int next_face_id;
7145 struct text_pos pos = it->current.pos;
7147 /* For a string from a display property, the next
7148 buffer position is stored in the 'position'
7149 member of the iteration stack slot below the
7150 current one, see handle_single_display_spec. By
7151 contrast, it->current.pos was is not yet updated
7152 to point to that buffer position; that will
7153 happen in pop_it, after we finish displaying the
7154 current string. Note that we already checked
7155 above that it->sp is positive, so subtracting one
7156 from it is safe. */
7157 if (it->from_disp_prop_p)
7158 pos = (it->stack + it->sp - 1)->position;
7159 else
7160 INC_TEXT_POS (pos, it->multibyte_p);
7162 if (CHARPOS (pos) >= ZV)
7163 it->end_of_box_run_p = true;
7164 else
7166 next_face_id = face_at_buffer_position
7167 (it->w, CHARPOS (pos), &ignore,
7168 CHARPOS (pos) + TEXT_PROP_DISTANCE_LIMIT, false, -1);
7169 it->end_of_box_run_p
7170 = (FACE_FROM_ID (it->f, next_face_id)->box
7171 == FACE_NO_BOX);
7176 /* next_element_from_display_vector sets this flag according to
7177 faces of the display vector glyphs, see there. */
7178 else if (it->method != GET_FROM_DISPLAY_VECTOR)
7180 int face_id = face_after_it_pos (it);
7181 it->end_of_box_run_p
7182 = (face_id != it->face_id
7183 && FACE_FROM_ID (it->f, face_id)->box == FACE_NO_BOX);
7186 /* If we reached the end of the object we've been iterating (e.g., a
7187 display string or an overlay string), and there's something on
7188 IT->stack, proceed with what's on the stack. It doesn't make
7189 sense to return zero if there's unprocessed stuff on the stack,
7190 because otherwise that stuff will never be displayed. */
7191 if (!success_p && it->sp > 0)
7193 set_iterator_to_next (it, 0);
7194 success_p = get_next_display_element (it);
7197 /* Value is 0 if end of buffer or string reached. */
7198 return success_p;
7202 /* Move IT to the next display element.
7204 RESEAT_P non-zero means if called on a newline in buffer text,
7205 skip to the next visible line start.
7207 Functions get_next_display_element and set_iterator_to_next are
7208 separate because I find this arrangement easier to handle than a
7209 get_next_display_element function that also increments IT's
7210 position. The way it is we can first look at an iterator's current
7211 display element, decide whether it fits on a line, and if it does,
7212 increment the iterator position. The other way around we probably
7213 would either need a flag indicating whether the iterator has to be
7214 incremented the next time, or we would have to implement a
7215 decrement position function which would not be easy to write. */
7217 void
7218 set_iterator_to_next (struct it *it, int reseat_p)
7220 /* Reset flags indicating start and end of a sequence of characters
7221 with box. Reset them at the start of this function because
7222 moving the iterator to a new position might set them. */
7223 it->start_of_box_run_p = it->end_of_box_run_p = 0;
7225 switch (it->method)
7227 case GET_FROM_BUFFER:
7228 /* The current display element of IT is a character from
7229 current_buffer. Advance in the buffer, and maybe skip over
7230 invisible lines that are so because of selective display. */
7231 if (ITERATOR_AT_END_OF_LINE_P (it) && reseat_p)
7232 reseat_at_next_visible_line_start (it, 0);
7233 else if (it->cmp_it.id >= 0)
7235 /* We are currently getting glyphs from a composition. */
7236 if (! it->bidi_p)
7238 IT_CHARPOS (*it) += it->cmp_it.nchars;
7239 IT_BYTEPOS (*it) += it->cmp_it.nbytes;
7241 else
7243 int i;
7245 /* Update IT's char/byte positions to point to the first
7246 character of the next grapheme cluster, or to the
7247 character visually after the current composition. */
7248 for (i = 0; i < it->cmp_it.nchars; i++)
7249 bidi_move_to_visually_next (&it->bidi_it);
7250 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7251 IT_CHARPOS (*it) = it->bidi_it.charpos;
7254 if ((! it->bidi_p || ! it->cmp_it.reversed_p)
7255 && it->cmp_it.to < it->cmp_it.nglyphs)
7257 /* Composition created while scanning forward. Proceed
7258 to the next grapheme cluster. */
7259 it->cmp_it.from = it->cmp_it.to;
7261 else if ((it->bidi_p && it->cmp_it.reversed_p)
7262 && it->cmp_it.from > 0)
7264 /* Composition created while scanning backward. Proceed
7265 to the previous grapheme cluster. */
7266 it->cmp_it.to = it->cmp_it.from;
7268 else
7270 /* No more grapheme clusters in this composition.
7271 Find the next stop position. */
7272 ptrdiff_t stop = it->end_charpos;
7274 if (it->bidi_it.scan_dir < 0)
7275 /* Now we are scanning backward and don't know
7276 where to stop. */
7277 stop = -1;
7278 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
7279 IT_BYTEPOS (*it), stop, Qnil);
7282 else
7284 eassert (it->len != 0);
7286 if (!it->bidi_p)
7288 IT_BYTEPOS (*it) += it->len;
7289 IT_CHARPOS (*it) += 1;
7291 else
7293 int prev_scan_dir = it->bidi_it.scan_dir;
7294 /* If this is a new paragraph, determine its base
7295 direction (a.k.a. its base embedding level). */
7296 if (it->bidi_it.new_paragraph)
7297 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 0);
7298 bidi_move_to_visually_next (&it->bidi_it);
7299 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7300 IT_CHARPOS (*it) = it->bidi_it.charpos;
7301 if (prev_scan_dir != it->bidi_it.scan_dir)
7303 /* As the scan direction was changed, we must
7304 re-compute the stop position for composition. */
7305 ptrdiff_t stop = it->end_charpos;
7306 if (it->bidi_it.scan_dir < 0)
7307 stop = -1;
7308 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
7309 IT_BYTEPOS (*it), stop, Qnil);
7312 eassert (IT_BYTEPOS (*it) == CHAR_TO_BYTE (IT_CHARPOS (*it)));
7314 break;
7316 case GET_FROM_C_STRING:
7317 /* Current display element of IT is from a C string. */
7318 if (!it->bidi_p
7319 /* If the string position is beyond string's end, it means
7320 next_element_from_c_string is padding the string with
7321 blanks, in which case we bypass the bidi iterator,
7322 because it cannot deal with such virtual characters. */
7323 || IT_CHARPOS (*it) >= it->bidi_it.string.schars)
7325 IT_BYTEPOS (*it) += it->len;
7326 IT_CHARPOS (*it) += 1;
7328 else
7330 bidi_move_to_visually_next (&it->bidi_it);
7331 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7332 IT_CHARPOS (*it) = it->bidi_it.charpos;
7334 break;
7336 case GET_FROM_DISPLAY_VECTOR:
7337 /* Current display element of IT is from a display table entry.
7338 Advance in the display table definition. Reset it to null if
7339 end reached, and continue with characters from buffers/
7340 strings. */
7341 ++it->current.dpvec_index;
7343 /* Restore face of the iterator to what they were before the
7344 display vector entry (these entries may contain faces). */
7345 it->face_id = it->saved_face_id;
7347 if (it->dpvec + it->current.dpvec_index >= it->dpend)
7349 int recheck_faces = it->ellipsis_p;
7351 if (it->s)
7352 it->method = GET_FROM_C_STRING;
7353 else if (STRINGP (it->string))
7354 it->method = GET_FROM_STRING;
7355 else
7357 it->method = GET_FROM_BUFFER;
7358 it->object = it->w->contents;
7361 it->dpvec = NULL;
7362 it->current.dpvec_index = -1;
7364 /* Skip over characters which were displayed via IT->dpvec. */
7365 if (it->dpvec_char_len < 0)
7366 reseat_at_next_visible_line_start (it, 1);
7367 else if (it->dpvec_char_len > 0)
7369 if (it->method == GET_FROM_STRING
7370 && it->current.overlay_string_index >= 0
7371 && it->n_overlay_strings > 0)
7372 it->ignore_overlay_strings_at_pos_p = true;
7373 it->len = it->dpvec_char_len;
7374 set_iterator_to_next (it, reseat_p);
7377 /* Maybe recheck faces after display vector. */
7378 if (recheck_faces)
7379 it->stop_charpos = IT_CHARPOS (*it);
7381 break;
7383 case GET_FROM_STRING:
7384 /* Current display element is a character from a Lisp string. */
7385 eassert (it->s == NULL && STRINGP (it->string));
7386 /* Don't advance past string end. These conditions are true
7387 when set_iterator_to_next is called at the end of
7388 get_next_display_element, in which case the Lisp string is
7389 already exhausted, and all we want is pop the iterator
7390 stack. */
7391 if (it->current.overlay_string_index >= 0)
7393 /* This is an overlay string, so there's no padding with
7394 spaces, and the number of characters in the string is
7395 where the string ends. */
7396 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7397 goto consider_string_end;
7399 else
7401 /* Not an overlay string. There could be padding, so test
7402 against it->end_charpos. */
7403 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
7404 goto consider_string_end;
7406 if (it->cmp_it.id >= 0)
7408 /* We are delivering display elements from a composition.
7409 Update the string position past the grapheme cluster
7410 we've just processed. */
7411 if (! it->bidi_p)
7413 IT_STRING_CHARPOS (*it) += it->cmp_it.nchars;
7414 IT_STRING_BYTEPOS (*it) += it->cmp_it.nbytes;
7416 else
7418 int i;
7420 for (i = 0; i < it->cmp_it.nchars; i++)
7421 bidi_move_to_visually_next (&it->bidi_it);
7422 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7423 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7426 /* Did we exhaust all the grapheme clusters of this
7427 composition? */
7428 if ((! it->bidi_p || ! it->cmp_it.reversed_p)
7429 && (it->cmp_it.to < it->cmp_it.nglyphs))
7431 /* Not all the grapheme clusters were processed yet;
7432 advance to the next cluster. */
7433 it->cmp_it.from = it->cmp_it.to;
7435 else if ((it->bidi_p && it->cmp_it.reversed_p)
7436 && it->cmp_it.from > 0)
7438 /* Likewise: advance to the next cluster, but going in
7439 the reverse direction. */
7440 it->cmp_it.to = it->cmp_it.from;
7442 else
7444 /* This composition was fully processed; find the next
7445 candidate place for checking for composed
7446 characters. */
7447 /* Always limit string searches to the string length;
7448 any padding spaces are not part of the string, and
7449 there cannot be any compositions in that padding. */
7450 ptrdiff_t stop = SCHARS (it->string);
7452 if (it->bidi_p && it->bidi_it.scan_dir < 0)
7453 stop = -1;
7454 else if (it->end_charpos < stop)
7456 /* Cf. PRECISION in reseat_to_string: we might be
7457 limited in how many of the string characters we
7458 need to deliver. */
7459 stop = it->end_charpos;
7461 composition_compute_stop_pos (&it->cmp_it,
7462 IT_STRING_CHARPOS (*it),
7463 IT_STRING_BYTEPOS (*it), stop,
7464 it->string);
7467 else
7469 if (!it->bidi_p
7470 /* If the string position is beyond string's end, it
7471 means next_element_from_string is padding the string
7472 with blanks, in which case we bypass the bidi
7473 iterator, because it cannot deal with such virtual
7474 characters. */
7475 || IT_STRING_CHARPOS (*it) >= it->bidi_it.string.schars)
7477 IT_STRING_BYTEPOS (*it) += it->len;
7478 IT_STRING_CHARPOS (*it) += 1;
7480 else
7482 int prev_scan_dir = it->bidi_it.scan_dir;
7484 bidi_move_to_visually_next (&it->bidi_it);
7485 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7486 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7487 /* If the scan direction changes, we may need to update
7488 the place where to check for composed characters. */
7489 if (prev_scan_dir != it->bidi_it.scan_dir)
7491 ptrdiff_t stop = SCHARS (it->string);
7493 if (it->bidi_it.scan_dir < 0)
7494 stop = -1;
7495 else if (it->end_charpos < stop)
7496 stop = it->end_charpos;
7498 composition_compute_stop_pos (&it->cmp_it,
7499 IT_STRING_CHARPOS (*it),
7500 IT_STRING_BYTEPOS (*it), stop,
7501 it->string);
7506 consider_string_end:
7508 if (it->current.overlay_string_index >= 0)
7510 /* IT->string is an overlay string. Advance to the
7511 next, if there is one. */
7512 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7514 it->ellipsis_p = 0;
7515 next_overlay_string (it);
7516 if (it->ellipsis_p)
7517 setup_for_ellipsis (it, 0);
7520 else
7522 /* IT->string is not an overlay string. If we reached
7523 its end, and there is something on IT->stack, proceed
7524 with what is on the stack. This can be either another
7525 string, this time an overlay string, or a buffer. */
7526 if (IT_STRING_CHARPOS (*it) == SCHARS (it->string)
7527 && it->sp > 0)
7529 pop_it (it);
7530 if (it->method == GET_FROM_STRING)
7531 goto consider_string_end;
7534 break;
7536 case GET_FROM_IMAGE:
7537 case GET_FROM_STRETCH:
7538 /* The position etc with which we have to proceed are on
7539 the stack. The position may be at the end of a string,
7540 if the `display' property takes up the whole string. */
7541 eassert (it->sp > 0);
7542 pop_it (it);
7543 if (it->method == GET_FROM_STRING)
7544 goto consider_string_end;
7545 break;
7547 default:
7548 /* There are no other methods defined, so this should be a bug. */
7549 emacs_abort ();
7552 eassert (it->method != GET_FROM_STRING
7553 || (STRINGP (it->string)
7554 && IT_STRING_CHARPOS (*it) >= 0));
7557 /* Load IT's display element fields with information about the next
7558 display element which comes from a display table entry or from the
7559 result of translating a control character to one of the forms `^C'
7560 or `\003'.
7562 IT->dpvec holds the glyphs to return as characters.
7563 IT->saved_face_id holds the face id before the display vector--it
7564 is restored into IT->face_id in set_iterator_to_next. */
7566 static int
7567 next_element_from_display_vector (struct it *it)
7569 Lisp_Object gc;
7570 int prev_face_id = it->face_id;
7571 int next_face_id;
7573 /* Precondition. */
7574 eassert (it->dpvec && it->current.dpvec_index >= 0);
7576 it->face_id = it->saved_face_id;
7578 /* KFS: This code used to check ip->dpvec[0] instead of the current element.
7579 That seemed totally bogus - so I changed it... */
7580 gc = it->dpvec[it->current.dpvec_index];
7582 if (GLYPH_CODE_P (gc))
7584 struct face *this_face, *prev_face, *next_face;
7586 it->c = GLYPH_CODE_CHAR (gc);
7587 it->len = CHAR_BYTES (it->c);
7589 /* The entry may contain a face id to use. Such a face id is
7590 the id of a Lisp face, not a realized face. A face id of
7591 zero means no face is specified. */
7592 if (it->dpvec_face_id >= 0)
7593 it->face_id = it->dpvec_face_id;
7594 else
7596 int lface_id = GLYPH_CODE_FACE (gc);
7597 if (lface_id > 0)
7598 it->face_id = merge_faces (it->f, Qt, lface_id,
7599 it->saved_face_id);
7602 /* Glyphs in the display vector could have the box face, so we
7603 need to set the related flags in the iterator, as
7604 appropriate. */
7605 this_face = FACE_FROM_ID (it->f, it->face_id);
7606 prev_face = FACE_FROM_ID (it->f, prev_face_id);
7608 /* Is this character the first character of a box-face run? */
7609 it->start_of_box_run_p = (this_face && this_face->box != FACE_NO_BOX
7610 && (!prev_face
7611 || prev_face->box == FACE_NO_BOX));
7613 /* For the last character of the box-face run, we need to look
7614 either at the next glyph from the display vector, or at the
7615 face we saw before the display vector. */
7616 next_face_id = it->saved_face_id;
7617 if (it->current.dpvec_index < it->dpend - it->dpvec - 1)
7619 if (it->dpvec_face_id >= 0)
7620 next_face_id = it->dpvec_face_id;
7621 else
7623 int lface_id =
7624 GLYPH_CODE_FACE (it->dpvec[it->current.dpvec_index + 1]);
7626 if (lface_id > 0)
7627 next_face_id = merge_faces (it->f, Qt, lface_id,
7628 it->saved_face_id);
7631 next_face = FACE_FROM_ID (it->f, next_face_id);
7632 it->end_of_box_run_p = (this_face && this_face->box != FACE_NO_BOX
7633 && (!next_face
7634 || next_face->box == FACE_NO_BOX));
7635 it->face_box_p = this_face && this_face->box != FACE_NO_BOX;
7637 else
7638 /* Display table entry is invalid. Return a space. */
7639 it->c = ' ', it->len = 1;
7641 /* Don't change position and object of the iterator here. They are
7642 still the values of the character that had this display table
7643 entry or was translated, and that's what we want. */
7644 it->what = IT_CHARACTER;
7645 return 1;
7648 /* Get the first element of string/buffer in the visual order, after
7649 being reseated to a new position in a string or a buffer. */
7650 static void
7651 get_visually_first_element (struct it *it)
7653 int string_p = STRINGP (it->string) || it->s;
7654 ptrdiff_t eob = (string_p ? it->bidi_it.string.schars : ZV);
7655 ptrdiff_t bob = (string_p ? 0 : BEGV);
7657 if (STRINGP (it->string))
7659 it->bidi_it.charpos = IT_STRING_CHARPOS (*it);
7660 it->bidi_it.bytepos = IT_STRING_BYTEPOS (*it);
7662 else
7664 it->bidi_it.charpos = IT_CHARPOS (*it);
7665 it->bidi_it.bytepos = IT_BYTEPOS (*it);
7668 if (it->bidi_it.charpos == eob)
7670 /* Nothing to do, but reset the FIRST_ELT flag, like
7671 bidi_paragraph_init does, because we are not going to
7672 call it. */
7673 it->bidi_it.first_elt = 0;
7675 else if (it->bidi_it.charpos == bob
7676 || (!string_p
7677 && (FETCH_CHAR (it->bidi_it.bytepos - 1) == '\n'
7678 || FETCH_CHAR (it->bidi_it.bytepos) == '\n')))
7680 /* If we are at the beginning of a line/string, we can produce
7681 the next element right away. */
7682 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
7683 bidi_move_to_visually_next (&it->bidi_it);
7685 else
7687 ptrdiff_t orig_bytepos = it->bidi_it.bytepos;
7689 /* We need to prime the bidi iterator starting at the line's or
7690 string's beginning, before we will be able to produce the
7691 next element. */
7692 if (string_p)
7693 it->bidi_it.charpos = it->bidi_it.bytepos = 0;
7694 else
7695 it->bidi_it.charpos = find_newline_no_quit (IT_CHARPOS (*it),
7696 IT_BYTEPOS (*it), -1,
7697 &it->bidi_it.bytepos);
7698 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
7701 /* Now return to buffer/string position where we were asked
7702 to get the next display element, and produce that. */
7703 bidi_move_to_visually_next (&it->bidi_it);
7705 while (it->bidi_it.bytepos != orig_bytepos
7706 && it->bidi_it.charpos < eob);
7709 /* Adjust IT's position information to where we ended up. */
7710 if (STRINGP (it->string))
7712 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7713 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7715 else
7717 IT_CHARPOS (*it) = it->bidi_it.charpos;
7718 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7721 if (STRINGP (it->string) || !it->s)
7723 ptrdiff_t stop, charpos, bytepos;
7725 if (STRINGP (it->string))
7727 eassert (!it->s);
7728 stop = SCHARS (it->string);
7729 if (stop > it->end_charpos)
7730 stop = it->end_charpos;
7731 charpos = IT_STRING_CHARPOS (*it);
7732 bytepos = IT_STRING_BYTEPOS (*it);
7734 else
7736 stop = it->end_charpos;
7737 charpos = IT_CHARPOS (*it);
7738 bytepos = IT_BYTEPOS (*it);
7740 if (it->bidi_it.scan_dir < 0)
7741 stop = -1;
7742 composition_compute_stop_pos (&it->cmp_it, charpos, bytepos, stop,
7743 it->string);
7747 /* Load IT with the next display element from Lisp string IT->string.
7748 IT->current.string_pos is the current position within the string.
7749 If IT->current.overlay_string_index >= 0, the Lisp string is an
7750 overlay string. */
7752 static int
7753 next_element_from_string (struct it *it)
7755 struct text_pos position;
7757 eassert (STRINGP (it->string));
7758 eassert (!it->bidi_p || EQ (it->string, it->bidi_it.string.lstring));
7759 eassert (IT_STRING_CHARPOS (*it) >= 0);
7760 position = it->current.string_pos;
7762 /* With bidi reordering, the character to display might not be the
7763 character at IT_STRING_CHARPOS. BIDI_IT.FIRST_ELT non-zero means
7764 that we were reseat()ed to a new string, whose paragraph
7765 direction is not known. */
7766 if (it->bidi_p && it->bidi_it.first_elt)
7768 get_visually_first_element (it);
7769 SET_TEXT_POS (position, IT_STRING_CHARPOS (*it), IT_STRING_BYTEPOS (*it));
7772 /* Time to check for invisible text? */
7773 if (IT_STRING_CHARPOS (*it) < it->end_charpos)
7775 if (IT_STRING_CHARPOS (*it) >= it->stop_charpos)
7777 if (!(!it->bidi_p
7778 || BIDI_AT_BASE_LEVEL (it->bidi_it)
7779 || IT_STRING_CHARPOS (*it) == it->stop_charpos))
7781 /* With bidi non-linear iteration, we could find
7782 ourselves far beyond the last computed stop_charpos,
7783 with several other stop positions in between that we
7784 missed. Scan them all now, in buffer's logical
7785 order, until we find and handle the last stop_charpos
7786 that precedes our current position. */
7787 handle_stop_backwards (it, it->stop_charpos);
7788 return GET_NEXT_DISPLAY_ELEMENT (it);
7790 else
7792 if (it->bidi_p)
7794 /* Take note of the stop position we just moved
7795 across, for when we will move back across it. */
7796 it->prev_stop = it->stop_charpos;
7797 /* If we are at base paragraph embedding level, take
7798 note of the last stop position seen at this
7799 level. */
7800 if (BIDI_AT_BASE_LEVEL (it->bidi_it))
7801 it->base_level_stop = it->stop_charpos;
7803 handle_stop (it);
7805 /* Since a handler may have changed IT->method, we must
7806 recurse here. */
7807 return GET_NEXT_DISPLAY_ELEMENT (it);
7810 else if (it->bidi_p
7811 /* If we are before prev_stop, we may have overstepped
7812 on our way backwards a stop_pos, and if so, we need
7813 to handle that stop_pos. */
7814 && IT_STRING_CHARPOS (*it) < it->prev_stop
7815 /* We can sometimes back up for reasons that have nothing
7816 to do with bidi reordering. E.g., compositions. The
7817 code below is only needed when we are above the base
7818 embedding level, so test for that explicitly. */
7819 && !BIDI_AT_BASE_LEVEL (it->bidi_it))
7821 /* If we lost track of base_level_stop, we have no better
7822 place for handle_stop_backwards to start from than string
7823 beginning. This happens, e.g., when we were reseated to
7824 the previous screenful of text by vertical-motion. */
7825 if (it->base_level_stop <= 0
7826 || IT_STRING_CHARPOS (*it) < it->base_level_stop)
7827 it->base_level_stop = 0;
7828 handle_stop_backwards (it, it->base_level_stop);
7829 return GET_NEXT_DISPLAY_ELEMENT (it);
7833 if (it->current.overlay_string_index >= 0)
7835 /* Get the next character from an overlay string. In overlay
7836 strings, there is no field width or padding with spaces to
7837 do. */
7838 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7840 it->what = IT_EOB;
7841 return 0;
7843 else if (CHAR_COMPOSED_P (it, IT_STRING_CHARPOS (*it),
7844 IT_STRING_BYTEPOS (*it),
7845 it->bidi_it.scan_dir < 0
7846 ? -1
7847 : SCHARS (it->string))
7848 && next_element_from_composition (it))
7850 return 1;
7852 else if (STRING_MULTIBYTE (it->string))
7854 const unsigned char *s = (SDATA (it->string)
7855 + IT_STRING_BYTEPOS (*it));
7856 it->c = string_char_and_length (s, &it->len);
7858 else
7860 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
7861 it->len = 1;
7864 else
7866 /* Get the next character from a Lisp string that is not an
7867 overlay string. Such strings come from the mode line, for
7868 example. We may have to pad with spaces, or truncate the
7869 string. See also next_element_from_c_string. */
7870 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
7872 it->what = IT_EOB;
7873 return 0;
7875 else if (IT_STRING_CHARPOS (*it) >= it->string_nchars)
7877 /* Pad with spaces. */
7878 it->c = ' ', it->len = 1;
7879 CHARPOS (position) = BYTEPOS (position) = -1;
7881 else if (CHAR_COMPOSED_P (it, IT_STRING_CHARPOS (*it),
7882 IT_STRING_BYTEPOS (*it),
7883 it->bidi_it.scan_dir < 0
7884 ? -1
7885 : it->string_nchars)
7886 && next_element_from_composition (it))
7888 return 1;
7890 else if (STRING_MULTIBYTE (it->string))
7892 const unsigned char *s = (SDATA (it->string)
7893 + IT_STRING_BYTEPOS (*it));
7894 it->c = string_char_and_length (s, &it->len);
7896 else
7898 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
7899 it->len = 1;
7903 /* Record what we have and where it came from. */
7904 it->what = IT_CHARACTER;
7905 it->object = it->string;
7906 it->position = position;
7907 return 1;
7911 /* Load IT with next display element from C string IT->s.
7912 IT->string_nchars is the maximum number of characters to return
7913 from the string. IT->end_charpos may be greater than
7914 IT->string_nchars when this function is called, in which case we
7915 may have to return padding spaces. Value is zero if end of string
7916 reached, including padding spaces. */
7918 static int
7919 next_element_from_c_string (struct it *it)
7921 bool success_p = true;
7923 eassert (it->s);
7924 eassert (!it->bidi_p || it->s == it->bidi_it.string.s);
7925 it->what = IT_CHARACTER;
7926 BYTEPOS (it->position) = CHARPOS (it->position) = 0;
7927 it->object = make_number (0);
7929 /* With bidi reordering, the character to display might not be the
7930 character at IT_CHARPOS. BIDI_IT.FIRST_ELT non-zero means that
7931 we were reseated to a new string, whose paragraph direction is
7932 not known. */
7933 if (it->bidi_p && it->bidi_it.first_elt)
7934 get_visually_first_element (it);
7936 /* IT's position can be greater than IT->string_nchars in case a
7937 field width or precision has been specified when the iterator was
7938 initialized. */
7939 if (IT_CHARPOS (*it) >= it->end_charpos)
7941 /* End of the game. */
7942 it->what = IT_EOB;
7943 success_p = 0;
7945 else if (IT_CHARPOS (*it) >= it->string_nchars)
7947 /* Pad with spaces. */
7948 it->c = ' ', it->len = 1;
7949 BYTEPOS (it->position) = CHARPOS (it->position) = -1;
7951 else if (it->multibyte_p)
7952 it->c = string_char_and_length (it->s + IT_BYTEPOS (*it), &it->len);
7953 else
7954 it->c = it->s[IT_BYTEPOS (*it)], it->len = 1;
7956 return success_p;
7960 /* Set up IT to return characters from an ellipsis, if appropriate.
7961 The definition of the ellipsis glyphs may come from a display table
7962 entry. This function fills IT with the first glyph from the
7963 ellipsis if an ellipsis is to be displayed. */
7965 static int
7966 next_element_from_ellipsis (struct it *it)
7968 if (it->selective_display_ellipsis_p)
7969 setup_for_ellipsis (it, it->len);
7970 else
7972 /* The face at the current position may be different from the
7973 face we find after the invisible text. Remember what it
7974 was in IT->saved_face_id, and signal that it's there by
7975 setting face_before_selective_p. */
7976 it->saved_face_id = it->face_id;
7977 it->method = GET_FROM_BUFFER;
7978 it->object = it->w->contents;
7979 reseat_at_next_visible_line_start (it, 1);
7980 it->face_before_selective_p = true;
7983 return GET_NEXT_DISPLAY_ELEMENT (it);
7987 /* Deliver an image display element. The iterator IT is already
7988 filled with image information (done in handle_display_prop). Value
7989 is always 1. */
7992 static int
7993 next_element_from_image (struct it *it)
7995 it->what = IT_IMAGE;
7996 it->ignore_overlay_strings_at_pos_p = 0;
7997 return 1;
8001 /* Fill iterator IT with next display element from a stretch glyph
8002 property. IT->object is the value of the text property. Value is
8003 always 1. */
8005 static int
8006 next_element_from_stretch (struct it *it)
8008 it->what = IT_STRETCH;
8009 return 1;
8012 /* Scan backwards from IT's current position until we find a stop
8013 position, or until BEGV. This is called when we find ourself
8014 before both the last known prev_stop and base_level_stop while
8015 reordering bidirectional text. */
8017 static void
8018 compute_stop_pos_backwards (struct it *it)
8020 const int SCAN_BACK_LIMIT = 1000;
8021 struct text_pos pos;
8022 struct display_pos save_current = it->current;
8023 struct text_pos save_position = it->position;
8024 ptrdiff_t charpos = IT_CHARPOS (*it);
8025 ptrdiff_t where_we_are = charpos;
8026 ptrdiff_t save_stop_pos = it->stop_charpos;
8027 ptrdiff_t save_end_pos = it->end_charpos;
8029 eassert (NILP (it->string) && !it->s);
8030 eassert (it->bidi_p);
8031 it->bidi_p = 0;
8034 it->end_charpos = min (charpos + 1, ZV);
8035 charpos = max (charpos - SCAN_BACK_LIMIT, BEGV);
8036 SET_TEXT_POS (pos, charpos, CHAR_TO_BYTE (charpos));
8037 reseat_1 (it, pos, 0);
8038 compute_stop_pos (it);
8039 /* We must advance forward, right? */
8040 if (it->stop_charpos <= charpos)
8041 emacs_abort ();
8043 while (charpos > BEGV && it->stop_charpos >= it->end_charpos);
8045 if (it->stop_charpos <= where_we_are)
8046 it->prev_stop = it->stop_charpos;
8047 else
8048 it->prev_stop = BEGV;
8049 it->bidi_p = true;
8050 it->current = save_current;
8051 it->position = save_position;
8052 it->stop_charpos = save_stop_pos;
8053 it->end_charpos = save_end_pos;
8056 /* Scan forward from CHARPOS in the current buffer/string, until we
8057 find a stop position > current IT's position. Then handle the stop
8058 position before that. This is called when we bump into a stop
8059 position while reordering bidirectional text. CHARPOS should be
8060 the last previously processed stop_pos (or BEGV/0, if none were
8061 processed yet) whose position is less that IT's current
8062 position. */
8064 static void
8065 handle_stop_backwards (struct it *it, ptrdiff_t charpos)
8067 int bufp = !STRINGP (it->string);
8068 ptrdiff_t where_we_are = (bufp ? IT_CHARPOS (*it) : IT_STRING_CHARPOS (*it));
8069 struct display_pos save_current = it->current;
8070 struct text_pos save_position = it->position;
8071 struct text_pos pos1;
8072 ptrdiff_t next_stop;
8074 /* Scan in strict logical order. */
8075 eassert (it->bidi_p);
8076 it->bidi_p = 0;
8079 it->prev_stop = charpos;
8080 if (bufp)
8082 SET_TEXT_POS (pos1, charpos, CHAR_TO_BYTE (charpos));
8083 reseat_1 (it, pos1, 0);
8085 else
8086 it->current.string_pos = string_pos (charpos, it->string);
8087 compute_stop_pos (it);
8088 /* We must advance forward, right? */
8089 if (it->stop_charpos <= it->prev_stop)
8090 emacs_abort ();
8091 charpos = it->stop_charpos;
8093 while (charpos <= where_we_are);
8095 it->bidi_p = true;
8096 it->current = save_current;
8097 it->position = save_position;
8098 next_stop = it->stop_charpos;
8099 it->stop_charpos = it->prev_stop;
8100 handle_stop (it);
8101 it->stop_charpos = next_stop;
8104 /* Load IT with the next display element from current_buffer. Value
8105 is zero if end of buffer reached. IT->stop_charpos is the next
8106 position at which to stop and check for text properties or buffer
8107 end. */
8109 static int
8110 next_element_from_buffer (struct it *it)
8112 bool success_p = true;
8114 eassert (IT_CHARPOS (*it) >= BEGV);
8115 eassert (NILP (it->string) && !it->s);
8116 eassert (!it->bidi_p
8117 || (EQ (it->bidi_it.string.lstring, Qnil)
8118 && it->bidi_it.string.s == NULL));
8120 /* With bidi reordering, the character to display might not be the
8121 character at IT_CHARPOS. BIDI_IT.FIRST_ELT non-zero means that
8122 we were reseat()ed to a new buffer position, which is potentially
8123 a different paragraph. */
8124 if (it->bidi_p && it->bidi_it.first_elt)
8126 get_visually_first_element (it);
8127 SET_TEXT_POS (it->position, IT_CHARPOS (*it), IT_BYTEPOS (*it));
8130 if (IT_CHARPOS (*it) >= it->stop_charpos)
8132 if (IT_CHARPOS (*it) >= it->end_charpos)
8134 int overlay_strings_follow_p;
8136 /* End of the game, except when overlay strings follow that
8137 haven't been returned yet. */
8138 if (it->overlay_strings_at_end_processed_p)
8139 overlay_strings_follow_p = 0;
8140 else
8142 it->overlay_strings_at_end_processed_p = true;
8143 overlay_strings_follow_p = get_overlay_strings (it, 0);
8146 if (overlay_strings_follow_p)
8147 success_p = GET_NEXT_DISPLAY_ELEMENT (it);
8148 else
8150 it->what = IT_EOB;
8151 it->position = it->current.pos;
8152 success_p = 0;
8155 else if (!(!it->bidi_p
8156 || BIDI_AT_BASE_LEVEL (it->bidi_it)
8157 || IT_CHARPOS (*it) == it->stop_charpos))
8159 /* With bidi non-linear iteration, we could find ourselves
8160 far beyond the last computed stop_charpos, with several
8161 other stop positions in between that we missed. Scan
8162 them all now, in buffer's logical order, until we find
8163 and handle the last stop_charpos that precedes our
8164 current position. */
8165 handle_stop_backwards (it, it->stop_charpos);
8166 return GET_NEXT_DISPLAY_ELEMENT (it);
8168 else
8170 if (it->bidi_p)
8172 /* Take note of the stop position we just moved across,
8173 for when we will move back across it. */
8174 it->prev_stop = it->stop_charpos;
8175 /* If we are at base paragraph embedding level, take
8176 note of the last stop position seen at this
8177 level. */
8178 if (BIDI_AT_BASE_LEVEL (it->bidi_it))
8179 it->base_level_stop = it->stop_charpos;
8181 handle_stop (it);
8182 return GET_NEXT_DISPLAY_ELEMENT (it);
8185 else if (it->bidi_p
8186 /* If we are before prev_stop, we may have overstepped on
8187 our way backwards a stop_pos, and if so, we need to
8188 handle that stop_pos. */
8189 && IT_CHARPOS (*it) < it->prev_stop
8190 /* We can sometimes back up for reasons that have nothing
8191 to do with bidi reordering. E.g., compositions. The
8192 code below is only needed when we are above the base
8193 embedding level, so test for that explicitly. */
8194 && !BIDI_AT_BASE_LEVEL (it->bidi_it))
8196 if (it->base_level_stop <= 0
8197 || IT_CHARPOS (*it) < it->base_level_stop)
8199 /* If we lost track of base_level_stop, we need to find
8200 prev_stop by looking backwards. This happens, e.g., when
8201 we were reseated to the previous screenful of text by
8202 vertical-motion. */
8203 it->base_level_stop = BEGV;
8204 compute_stop_pos_backwards (it);
8205 handle_stop_backwards (it, it->prev_stop);
8207 else
8208 handle_stop_backwards (it, it->base_level_stop);
8209 return GET_NEXT_DISPLAY_ELEMENT (it);
8211 else
8213 /* No face changes, overlays etc. in sight, so just return a
8214 character from current_buffer. */
8215 unsigned char *p;
8216 ptrdiff_t stop;
8218 /* We moved to the next buffer position, so any info about
8219 previously seen overlays is no longer valid. */
8220 it->ignore_overlay_strings_at_pos_p = 0;
8222 /* Maybe run the redisplay end trigger hook. Performance note:
8223 This doesn't seem to cost measurable time. */
8224 if (it->redisplay_end_trigger_charpos
8225 && it->glyph_row
8226 && IT_CHARPOS (*it) >= it->redisplay_end_trigger_charpos)
8227 run_redisplay_end_trigger_hook (it);
8229 stop = it->bidi_it.scan_dir < 0 ? -1 : it->end_charpos;
8230 if (CHAR_COMPOSED_P (it, IT_CHARPOS (*it), IT_BYTEPOS (*it),
8231 stop)
8232 && next_element_from_composition (it))
8234 return 1;
8237 /* Get the next character, maybe multibyte. */
8238 p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
8239 if (it->multibyte_p && !ASCII_CHAR_P (*p))
8240 it->c = STRING_CHAR_AND_LENGTH (p, it->len);
8241 else
8242 it->c = *p, it->len = 1;
8244 /* Record what we have and where it came from. */
8245 it->what = IT_CHARACTER;
8246 it->object = it->w->contents;
8247 it->position = it->current.pos;
8249 /* Normally we return the character found above, except when we
8250 really want to return an ellipsis for selective display. */
8251 if (it->selective)
8253 if (it->c == '\n')
8255 /* A value of selective > 0 means hide lines indented more
8256 than that number of columns. */
8257 if (it->selective > 0
8258 && IT_CHARPOS (*it) + 1 < ZV
8259 && indented_beyond_p (IT_CHARPOS (*it) + 1,
8260 IT_BYTEPOS (*it) + 1,
8261 it->selective))
8263 success_p = next_element_from_ellipsis (it);
8264 it->dpvec_char_len = -1;
8267 else if (it->c == '\r' && it->selective == -1)
8269 /* A value of selective == -1 means that everything from the
8270 CR to the end of the line is invisible, with maybe an
8271 ellipsis displayed for it. */
8272 success_p = next_element_from_ellipsis (it);
8273 it->dpvec_char_len = -1;
8278 /* Value is zero if end of buffer reached. */
8279 eassert (!success_p || it->what != IT_CHARACTER || it->len > 0);
8280 return success_p;
8284 /* Run the redisplay end trigger hook for IT. */
8286 static void
8287 run_redisplay_end_trigger_hook (struct it *it)
8289 /* IT->glyph_row should be non-null, i.e. we should be actually
8290 displaying something, or otherwise we should not run the hook. */
8291 eassert (it->glyph_row);
8293 ptrdiff_t charpos = it->redisplay_end_trigger_charpos;
8294 it->redisplay_end_trigger_charpos = 0;
8296 /* Since we are *trying* to run these functions, don't try to run
8297 them again, even if they get an error. */
8298 wset_redisplay_end_trigger (it->w, Qnil);
8299 CALLN (Frun_hook_with_args, Qredisplay_end_trigger_functions, it->window,
8300 make_number (charpos));
8302 /* Notice if it changed the face of the character we are on. */
8303 handle_face_prop (it);
8307 /* Deliver a composition display element. Unlike the other
8308 next_element_from_XXX, this function is not registered in the array
8309 get_next_element[]. It is called from next_element_from_buffer and
8310 next_element_from_string when necessary. */
8312 static int
8313 next_element_from_composition (struct it *it)
8315 it->what = IT_COMPOSITION;
8316 it->len = it->cmp_it.nbytes;
8317 if (STRINGP (it->string))
8319 if (it->c < 0)
8321 IT_STRING_CHARPOS (*it) += it->cmp_it.nchars;
8322 IT_STRING_BYTEPOS (*it) += it->cmp_it.nbytes;
8323 return 0;
8325 it->position = it->current.string_pos;
8326 it->object = it->string;
8327 it->c = composition_update_it (&it->cmp_it, IT_STRING_CHARPOS (*it),
8328 IT_STRING_BYTEPOS (*it), it->string);
8330 else
8332 if (it->c < 0)
8334 IT_CHARPOS (*it) += it->cmp_it.nchars;
8335 IT_BYTEPOS (*it) += it->cmp_it.nbytes;
8336 if (it->bidi_p)
8338 if (it->bidi_it.new_paragraph)
8339 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 0);
8340 /* Resync the bidi iterator with IT's new position.
8341 FIXME: this doesn't support bidirectional text. */
8342 while (it->bidi_it.charpos < IT_CHARPOS (*it))
8343 bidi_move_to_visually_next (&it->bidi_it);
8345 return 0;
8347 it->position = it->current.pos;
8348 it->object = it->w->contents;
8349 it->c = composition_update_it (&it->cmp_it, IT_CHARPOS (*it),
8350 IT_BYTEPOS (*it), Qnil);
8352 return 1;
8357 /***********************************************************************
8358 Moving an iterator without producing glyphs
8359 ***********************************************************************/
8361 /* Check if iterator is at a position corresponding to a valid buffer
8362 position after some move_it_ call. */
8364 #define IT_POS_VALID_AFTER_MOVE_P(it) \
8365 ((it)->method == GET_FROM_STRING \
8366 ? IT_STRING_CHARPOS (*it) == 0 \
8367 : 1)
8370 /* Move iterator IT to a specified buffer or X position within one
8371 line on the display without producing glyphs.
8373 OP should be a bit mask including some or all of these bits:
8374 MOVE_TO_X: Stop upon reaching x-position TO_X.
8375 MOVE_TO_POS: Stop upon reaching buffer or string position TO_CHARPOS.
8376 Regardless of OP's value, stop upon reaching the end of the display line.
8378 TO_X is normally a value 0 <= TO_X <= IT->last_visible_x.
8379 This means, in particular, that TO_X includes window's horizontal
8380 scroll amount.
8382 The return value has several possible values that
8383 say what condition caused the scan to stop:
8385 MOVE_POS_MATCH_OR_ZV
8386 - when TO_POS or ZV was reached.
8388 MOVE_X_REACHED
8389 -when TO_X was reached before TO_POS or ZV were reached.
8391 MOVE_LINE_CONTINUED
8392 - when we reached the end of the display area and the line must
8393 be continued.
8395 MOVE_LINE_TRUNCATED
8396 - when we reached the end of the display area and the line is
8397 truncated.
8399 MOVE_NEWLINE_OR_CR
8400 - when we stopped at a line end, i.e. a newline or a CR and selective
8401 display is on. */
8403 static enum move_it_result
8404 move_it_in_display_line_to (struct it *it,
8405 ptrdiff_t to_charpos, int to_x,
8406 enum move_operation_enum op)
8408 enum move_it_result result = MOVE_UNDEFINED;
8409 struct glyph_row *saved_glyph_row;
8410 struct it wrap_it, atpos_it, atx_it, ppos_it;
8411 void *wrap_data = NULL, *atpos_data = NULL, *atx_data = NULL;
8412 void *ppos_data = NULL;
8413 int may_wrap = 0;
8414 enum it_method prev_method = it->method;
8415 ptrdiff_t closest_pos IF_LINT (= 0), prev_pos = IT_CHARPOS (*it);
8416 int saw_smaller_pos = prev_pos < to_charpos;
8418 /* Don't produce glyphs in produce_glyphs. */
8419 saved_glyph_row = it->glyph_row;
8420 it->glyph_row = NULL;
8422 /* Use wrap_it to save a copy of IT wherever a word wrap could
8423 occur. Use atpos_it to save a copy of IT at the desired buffer
8424 position, if found, so that we can scan ahead and check if the
8425 word later overshoots the window edge. Use atx_it similarly, for
8426 pixel positions. */
8427 wrap_it.sp = -1;
8428 atpos_it.sp = -1;
8429 atx_it.sp = -1;
8431 /* Use ppos_it under bidi reordering to save a copy of IT for the
8432 initial position. We restore that position in IT when we have
8433 scanned the entire display line without finding a match for
8434 TO_CHARPOS and all the character positions are greater than
8435 TO_CHARPOS. We then restart the scan from the initial position,
8436 and stop at CLOSEST_POS, which is a position > TO_CHARPOS that is
8437 the closest to TO_CHARPOS. */
8438 if (it->bidi_p)
8440 if ((op & MOVE_TO_POS) && IT_CHARPOS (*it) >= to_charpos)
8442 SAVE_IT (ppos_it, *it, ppos_data);
8443 closest_pos = IT_CHARPOS (*it);
8445 else
8446 closest_pos = ZV;
8449 #define BUFFER_POS_REACHED_P() \
8450 ((op & MOVE_TO_POS) != 0 \
8451 && BUFFERP (it->object) \
8452 && (IT_CHARPOS (*it) == to_charpos \
8453 || ((!it->bidi_p \
8454 || BIDI_AT_BASE_LEVEL (it->bidi_it)) \
8455 && IT_CHARPOS (*it) > to_charpos) \
8456 || (it->what == IT_COMPOSITION \
8457 && ((IT_CHARPOS (*it) > to_charpos \
8458 && to_charpos >= it->cmp_it.charpos) \
8459 || (IT_CHARPOS (*it) < to_charpos \
8460 && to_charpos <= it->cmp_it.charpos)))) \
8461 && (it->method == GET_FROM_BUFFER \
8462 || (it->method == GET_FROM_DISPLAY_VECTOR \
8463 && it->dpvec + it->current.dpvec_index + 1 >= it->dpend)))
8465 /* If there's a line-/wrap-prefix, handle it. */
8466 if (it->hpos == 0 && it->method == GET_FROM_BUFFER
8467 && it->current_y < it->last_visible_y)
8468 handle_line_prefix (it);
8470 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8471 SET_TEXT_POS (this_line_min_pos, IT_CHARPOS (*it), IT_BYTEPOS (*it));
8473 while (1)
8475 int x, i, ascent = 0, descent = 0;
8477 /* Utility macro to reset an iterator with x, ascent, and descent. */
8478 #define IT_RESET_X_ASCENT_DESCENT(IT) \
8479 ((IT)->current_x = x, (IT)->max_ascent = ascent, \
8480 (IT)->max_descent = descent)
8482 /* Stop if we move beyond TO_CHARPOS (after an image or a
8483 display string or stretch glyph). */
8484 if ((op & MOVE_TO_POS) != 0
8485 && BUFFERP (it->object)
8486 && it->method == GET_FROM_BUFFER
8487 && (((!it->bidi_p
8488 /* When the iterator is at base embedding level, we
8489 are guaranteed that characters are delivered for
8490 display in strictly increasing order of their
8491 buffer positions. */
8492 || BIDI_AT_BASE_LEVEL (it->bidi_it))
8493 && IT_CHARPOS (*it) > to_charpos)
8494 || (it->bidi_p
8495 && (prev_method == GET_FROM_IMAGE
8496 || prev_method == GET_FROM_STRETCH
8497 || prev_method == GET_FROM_STRING)
8498 /* Passed TO_CHARPOS from left to right. */
8499 && ((prev_pos < to_charpos
8500 && IT_CHARPOS (*it) > to_charpos)
8501 /* Passed TO_CHARPOS from right to left. */
8502 || (prev_pos > to_charpos
8503 && IT_CHARPOS (*it) < to_charpos)))))
8505 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8507 result = MOVE_POS_MATCH_OR_ZV;
8508 break;
8510 else if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
8511 /* If wrap_it is valid, the current position might be in a
8512 word that is wrapped. So, save the iterator in
8513 atpos_it and continue to see if wrapping happens. */
8514 SAVE_IT (atpos_it, *it, atpos_data);
8517 /* Stop when ZV reached.
8518 We used to stop here when TO_CHARPOS reached as well, but that is
8519 too soon if this glyph does not fit on this line. So we handle it
8520 explicitly below. */
8521 if (!get_next_display_element (it))
8523 result = MOVE_POS_MATCH_OR_ZV;
8524 break;
8527 if (it->line_wrap == TRUNCATE)
8529 if (BUFFER_POS_REACHED_P ())
8531 result = MOVE_POS_MATCH_OR_ZV;
8532 break;
8535 else
8537 if (it->line_wrap == WORD_WRAP && it->area == TEXT_AREA)
8539 if (IT_DISPLAYING_WHITESPACE (it))
8540 may_wrap = 1;
8541 else if (may_wrap)
8543 /* We have reached a glyph that follows one or more
8544 whitespace characters. If the position is
8545 already found, we are done. */
8546 if (atpos_it.sp >= 0)
8548 RESTORE_IT (it, &atpos_it, atpos_data);
8549 result = MOVE_POS_MATCH_OR_ZV;
8550 goto done;
8552 if (atx_it.sp >= 0)
8554 RESTORE_IT (it, &atx_it, atx_data);
8555 result = MOVE_X_REACHED;
8556 goto done;
8558 /* Otherwise, we can wrap here. */
8559 SAVE_IT (wrap_it, *it, wrap_data);
8560 may_wrap = 0;
8565 /* Remember the line height for the current line, in case
8566 the next element doesn't fit on the line. */
8567 ascent = it->max_ascent;
8568 descent = it->max_descent;
8570 /* The call to produce_glyphs will get the metrics of the
8571 display element IT is loaded with. Record the x-position
8572 before this display element, in case it doesn't fit on the
8573 line. */
8574 x = it->current_x;
8576 PRODUCE_GLYPHS (it);
8578 if (it->area != TEXT_AREA)
8580 prev_method = it->method;
8581 if (it->method == GET_FROM_BUFFER)
8582 prev_pos = IT_CHARPOS (*it);
8583 set_iterator_to_next (it, 1);
8584 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8585 SET_TEXT_POS (this_line_min_pos,
8586 IT_CHARPOS (*it), IT_BYTEPOS (*it));
8587 if (it->bidi_p
8588 && (op & MOVE_TO_POS)
8589 && IT_CHARPOS (*it) > to_charpos
8590 && IT_CHARPOS (*it) < closest_pos)
8591 closest_pos = IT_CHARPOS (*it);
8592 continue;
8595 /* The number of glyphs we get back in IT->nglyphs will normally
8596 be 1 except when IT->c is (i) a TAB, or (ii) a multi-glyph
8597 character on a terminal frame, or (iii) a line end. For the
8598 second case, IT->nglyphs - 1 padding glyphs will be present.
8599 (On X frames, there is only one glyph produced for a
8600 composite character.)
8602 The behavior implemented below means, for continuation lines,
8603 that as many spaces of a TAB as fit on the current line are
8604 displayed there. For terminal frames, as many glyphs of a
8605 multi-glyph character are displayed in the current line, too.
8606 This is what the old redisplay code did, and we keep it that
8607 way. Under X, the whole shape of a complex character must
8608 fit on the line or it will be completely displayed in the
8609 next line.
8611 Note that both for tabs and padding glyphs, all glyphs have
8612 the same width. */
8613 if (it->nglyphs)
8615 /* More than one glyph or glyph doesn't fit on line. All
8616 glyphs have the same width. */
8617 int single_glyph_width = it->pixel_width / it->nglyphs;
8618 int new_x;
8619 int x_before_this_char = x;
8620 int hpos_before_this_char = it->hpos;
8622 for (i = 0; i < it->nglyphs; ++i, x = new_x)
8624 new_x = x + single_glyph_width;
8626 /* We want to leave anything reaching TO_X to the caller. */
8627 if ((op & MOVE_TO_X) && new_x > to_x)
8629 if (BUFFER_POS_REACHED_P ())
8631 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8632 goto buffer_pos_reached;
8633 if (atpos_it.sp < 0)
8635 SAVE_IT (atpos_it, *it, atpos_data);
8636 IT_RESET_X_ASCENT_DESCENT (&atpos_it);
8639 else
8641 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8643 it->current_x = x;
8644 result = MOVE_X_REACHED;
8645 break;
8647 if (atx_it.sp < 0)
8649 SAVE_IT (atx_it, *it, atx_data);
8650 IT_RESET_X_ASCENT_DESCENT (&atx_it);
8655 if (/* Lines are continued. */
8656 it->line_wrap != TRUNCATE
8657 && (/* And glyph doesn't fit on the line. */
8658 new_x > it->last_visible_x
8659 /* Or it fits exactly and we're on a window
8660 system frame. */
8661 || (new_x == it->last_visible_x
8662 && FRAME_WINDOW_P (it->f)
8663 && ((it->bidi_p && it->bidi_it.paragraph_dir == R2L)
8664 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
8665 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)))))
8667 if (/* IT->hpos == 0 means the very first glyph
8668 doesn't fit on the line, e.g. a wide image. */
8669 it->hpos == 0
8670 || (new_x == it->last_visible_x
8671 && FRAME_WINDOW_P (it->f)))
8673 ++it->hpos;
8674 it->current_x = new_x;
8676 /* The character's last glyph just barely fits
8677 in this row. */
8678 if (i == it->nglyphs - 1)
8680 /* If this is the destination position,
8681 return a position *before* it in this row,
8682 now that we know it fits in this row. */
8683 if (BUFFER_POS_REACHED_P ())
8685 if (it->line_wrap != WORD_WRAP
8686 || wrap_it.sp < 0)
8688 it->hpos = hpos_before_this_char;
8689 it->current_x = x_before_this_char;
8690 result = MOVE_POS_MATCH_OR_ZV;
8691 break;
8693 if (it->line_wrap == WORD_WRAP
8694 && atpos_it.sp < 0)
8696 SAVE_IT (atpos_it, *it, atpos_data);
8697 atpos_it.current_x = x_before_this_char;
8698 atpos_it.hpos = hpos_before_this_char;
8702 prev_method = it->method;
8703 if (it->method == GET_FROM_BUFFER)
8704 prev_pos = IT_CHARPOS (*it);
8705 set_iterator_to_next (it, 1);
8706 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8707 SET_TEXT_POS (this_line_min_pos,
8708 IT_CHARPOS (*it), IT_BYTEPOS (*it));
8709 /* On graphical terminals, newlines may
8710 "overflow" into the fringe if
8711 overflow-newline-into-fringe is non-nil.
8712 On text terminals, and on graphical
8713 terminals with no right margin, newlines
8714 may overflow into the last glyph on the
8715 display line.*/
8716 if (!FRAME_WINDOW_P (it->f)
8717 || ((it->bidi_p
8718 && it->bidi_it.paragraph_dir == R2L)
8719 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
8720 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0
8721 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
8723 if (!get_next_display_element (it))
8725 result = MOVE_POS_MATCH_OR_ZV;
8726 break;
8728 if (BUFFER_POS_REACHED_P ())
8730 if (ITERATOR_AT_END_OF_LINE_P (it))
8731 result = MOVE_POS_MATCH_OR_ZV;
8732 else
8733 result = MOVE_LINE_CONTINUED;
8734 break;
8736 if (ITERATOR_AT_END_OF_LINE_P (it)
8737 && (it->line_wrap != WORD_WRAP
8738 || wrap_it.sp < 0
8739 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it)))
8741 result = MOVE_NEWLINE_OR_CR;
8742 break;
8747 else
8748 IT_RESET_X_ASCENT_DESCENT (it);
8750 if (wrap_it.sp >= 0)
8752 RESTORE_IT (it, &wrap_it, wrap_data);
8753 atpos_it.sp = -1;
8754 atx_it.sp = -1;
8757 TRACE_MOVE ((stderr, "move_it_in: continued at %d\n",
8758 IT_CHARPOS (*it)));
8759 result = MOVE_LINE_CONTINUED;
8760 break;
8763 if (BUFFER_POS_REACHED_P ())
8765 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8766 goto buffer_pos_reached;
8767 if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
8769 SAVE_IT (atpos_it, *it, atpos_data);
8770 IT_RESET_X_ASCENT_DESCENT (&atpos_it);
8774 if (new_x > it->first_visible_x)
8776 /* Glyph is visible. Increment number of glyphs that
8777 would be displayed. */
8778 ++it->hpos;
8782 if (result != MOVE_UNDEFINED)
8783 break;
8785 else if (BUFFER_POS_REACHED_P ())
8787 buffer_pos_reached:
8788 IT_RESET_X_ASCENT_DESCENT (it);
8789 result = MOVE_POS_MATCH_OR_ZV;
8790 break;
8792 else if ((op & MOVE_TO_X) && it->current_x >= to_x)
8794 /* Stop when TO_X specified and reached. This check is
8795 necessary here because of lines consisting of a line end,
8796 only. The line end will not produce any glyphs and we
8797 would never get MOVE_X_REACHED. */
8798 eassert (it->nglyphs == 0);
8799 result = MOVE_X_REACHED;
8800 break;
8803 /* Is this a line end? If yes, we're done. */
8804 if (ITERATOR_AT_END_OF_LINE_P (it))
8806 /* If we are past TO_CHARPOS, but never saw any character
8807 positions smaller than TO_CHARPOS, return
8808 MOVE_POS_MATCH_OR_ZV, like the unidirectional display
8809 did. */
8810 if (it->bidi_p && (op & MOVE_TO_POS) != 0)
8812 if (!saw_smaller_pos && IT_CHARPOS (*it) > to_charpos)
8814 if (closest_pos < ZV)
8816 RESTORE_IT (it, &ppos_it, ppos_data);
8817 /* Don't recurse if closest_pos is equal to
8818 to_charpos, since we have just tried that. */
8819 if (closest_pos != to_charpos)
8820 move_it_in_display_line_to (it, closest_pos, -1,
8821 MOVE_TO_POS);
8822 result = MOVE_POS_MATCH_OR_ZV;
8824 else
8825 goto buffer_pos_reached;
8827 else if (it->line_wrap == WORD_WRAP && atpos_it.sp >= 0
8828 && IT_CHARPOS (*it) > to_charpos)
8829 goto buffer_pos_reached;
8830 else
8831 result = MOVE_NEWLINE_OR_CR;
8833 else
8834 result = MOVE_NEWLINE_OR_CR;
8835 break;
8838 prev_method = it->method;
8839 if (it->method == GET_FROM_BUFFER)
8840 prev_pos = IT_CHARPOS (*it);
8841 /* The current display element has been consumed. Advance
8842 to the next. */
8843 set_iterator_to_next (it, 1);
8844 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8845 SET_TEXT_POS (this_line_min_pos, IT_CHARPOS (*it), IT_BYTEPOS (*it));
8846 if (IT_CHARPOS (*it) < to_charpos)
8847 saw_smaller_pos = 1;
8848 if (it->bidi_p
8849 && (op & MOVE_TO_POS)
8850 && IT_CHARPOS (*it) >= to_charpos
8851 && IT_CHARPOS (*it) < closest_pos)
8852 closest_pos = IT_CHARPOS (*it);
8854 /* Stop if lines are truncated and IT's current x-position is
8855 past the right edge of the window now. */
8856 if (it->line_wrap == TRUNCATE
8857 && it->current_x >= it->last_visible_x)
8859 if (!FRAME_WINDOW_P (it->f)
8860 || ((it->bidi_p && it->bidi_it.paragraph_dir == R2L)
8861 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
8862 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0
8863 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
8865 int at_eob_p = 0;
8867 if ((at_eob_p = !get_next_display_element (it))
8868 || BUFFER_POS_REACHED_P ()
8869 /* If we are past TO_CHARPOS, but never saw any
8870 character positions smaller than TO_CHARPOS,
8871 return MOVE_POS_MATCH_OR_ZV, like the
8872 unidirectional display did. */
8873 || (it->bidi_p && (op & MOVE_TO_POS) != 0
8874 && !saw_smaller_pos
8875 && IT_CHARPOS (*it) > to_charpos))
8877 if (it->bidi_p
8878 && !BUFFER_POS_REACHED_P ()
8879 && !at_eob_p && closest_pos < ZV)
8881 RESTORE_IT (it, &ppos_it, ppos_data);
8882 if (closest_pos != to_charpos)
8883 move_it_in_display_line_to (it, closest_pos, -1,
8884 MOVE_TO_POS);
8886 result = MOVE_POS_MATCH_OR_ZV;
8887 break;
8889 if (ITERATOR_AT_END_OF_LINE_P (it))
8891 result = MOVE_NEWLINE_OR_CR;
8892 break;
8895 else if (it->bidi_p && (op & MOVE_TO_POS) != 0
8896 && !saw_smaller_pos
8897 && IT_CHARPOS (*it) > to_charpos)
8899 if (closest_pos < ZV)
8901 RESTORE_IT (it, &ppos_it, ppos_data);
8902 if (closest_pos != to_charpos)
8903 move_it_in_display_line_to (it, closest_pos, -1,
8904 MOVE_TO_POS);
8906 result = MOVE_POS_MATCH_OR_ZV;
8907 break;
8909 result = MOVE_LINE_TRUNCATED;
8910 break;
8912 #undef IT_RESET_X_ASCENT_DESCENT
8915 #undef BUFFER_POS_REACHED_P
8917 /* If we scanned beyond to_pos and didn't find a point to wrap at,
8918 restore the saved iterator. */
8919 if (atpos_it.sp >= 0)
8920 RESTORE_IT (it, &atpos_it, atpos_data);
8921 else if (atx_it.sp >= 0)
8922 RESTORE_IT (it, &atx_it, atx_data);
8924 done:
8926 if (atpos_data)
8927 bidi_unshelve_cache (atpos_data, 1);
8928 if (atx_data)
8929 bidi_unshelve_cache (atx_data, 1);
8930 if (wrap_data)
8931 bidi_unshelve_cache (wrap_data, 1);
8932 if (ppos_data)
8933 bidi_unshelve_cache (ppos_data, 1);
8935 /* Restore the iterator settings altered at the beginning of this
8936 function. */
8937 it->glyph_row = saved_glyph_row;
8938 return result;
8941 /* For external use. */
8942 void
8943 move_it_in_display_line (struct it *it,
8944 ptrdiff_t to_charpos, int to_x,
8945 enum move_operation_enum op)
8947 if (it->line_wrap == WORD_WRAP
8948 && (op & MOVE_TO_X))
8950 struct it save_it;
8951 void *save_data = NULL;
8952 int skip;
8954 SAVE_IT (save_it, *it, save_data);
8955 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
8956 /* When word-wrap is on, TO_X may lie past the end
8957 of a wrapped line. Then it->current is the
8958 character on the next line, so backtrack to the
8959 space before the wrap point. */
8960 if (skip == MOVE_LINE_CONTINUED)
8962 int prev_x = max (it->current_x - 1, 0);
8963 RESTORE_IT (it, &save_it, save_data);
8964 move_it_in_display_line_to
8965 (it, -1, prev_x, MOVE_TO_X);
8967 else
8968 bidi_unshelve_cache (save_data, 1);
8970 else
8971 move_it_in_display_line_to (it, to_charpos, to_x, op);
8975 /* Move IT forward until it satisfies one or more of the criteria in
8976 TO_CHARPOS, TO_X, TO_Y, and TO_VPOS.
8978 OP is a bit-mask that specifies where to stop, and in particular,
8979 which of those four position arguments makes a difference. See the
8980 description of enum move_operation_enum.
8982 If TO_CHARPOS is in invisible text, e.g. a truncated part of a
8983 screen line, this function will set IT to the next position that is
8984 displayed to the right of TO_CHARPOS on the screen.
8986 Return the maximum pixel length of any line scanned but never more
8987 than it.last_visible_x. */
8990 move_it_to (struct it *it, ptrdiff_t to_charpos, int to_x, int to_y, int to_vpos, int op)
8992 enum move_it_result skip, skip2 = MOVE_X_REACHED;
8993 int line_height, line_start_x = 0, reached = 0;
8994 int max_current_x = 0;
8995 void *backup_data = NULL;
8997 for (;;)
8999 if (op & MOVE_TO_VPOS)
9001 /* If no TO_CHARPOS and no TO_X specified, stop at the
9002 start of the line TO_VPOS. */
9003 if ((op & (MOVE_TO_X | MOVE_TO_POS)) == 0)
9005 if (it->vpos == to_vpos)
9007 reached = 1;
9008 break;
9010 else
9011 skip = move_it_in_display_line_to (it, -1, -1, 0);
9013 else
9015 /* TO_VPOS >= 0 means stop at TO_X in the line at
9016 TO_VPOS, or at TO_POS, whichever comes first. */
9017 if (it->vpos == to_vpos)
9019 reached = 2;
9020 break;
9023 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
9025 if (skip == MOVE_POS_MATCH_OR_ZV || it->vpos == to_vpos)
9027 reached = 3;
9028 break;
9030 else if (skip == MOVE_X_REACHED && it->vpos != to_vpos)
9032 /* We have reached TO_X but not in the line we want. */
9033 skip = move_it_in_display_line_to (it, to_charpos,
9034 -1, MOVE_TO_POS);
9035 if (skip == MOVE_POS_MATCH_OR_ZV)
9037 reached = 4;
9038 break;
9043 else if (op & MOVE_TO_Y)
9045 struct it it_backup;
9047 if (it->line_wrap == WORD_WRAP)
9048 SAVE_IT (it_backup, *it, backup_data);
9050 /* TO_Y specified means stop at TO_X in the line containing
9051 TO_Y---or at TO_CHARPOS if this is reached first. The
9052 problem is that we can't really tell whether the line
9053 contains TO_Y before we have completely scanned it, and
9054 this may skip past TO_X. What we do is to first scan to
9055 TO_X.
9057 If TO_X is not specified, use a TO_X of zero. The reason
9058 is to make the outcome of this function more predictable.
9059 If we didn't use TO_X == 0, we would stop at the end of
9060 the line which is probably not what a caller would expect
9061 to happen. */
9062 skip = move_it_in_display_line_to
9063 (it, to_charpos, ((op & MOVE_TO_X) ? to_x : 0),
9064 (MOVE_TO_X | (op & MOVE_TO_POS)));
9066 /* If TO_CHARPOS is reached or ZV, we don't have to do more. */
9067 if (skip == MOVE_POS_MATCH_OR_ZV)
9068 reached = 5;
9069 else if (skip == MOVE_X_REACHED)
9071 /* If TO_X was reached, we want to know whether TO_Y is
9072 in the line. We know this is the case if the already
9073 scanned glyphs make the line tall enough. Otherwise,
9074 we must check by scanning the rest of the line. */
9075 line_height = it->max_ascent + it->max_descent;
9076 if (to_y >= it->current_y
9077 && to_y < it->current_y + line_height)
9079 reached = 6;
9080 break;
9082 SAVE_IT (it_backup, *it, backup_data);
9083 TRACE_MOVE ((stderr, "move_it: from %d\n", IT_CHARPOS (*it)));
9084 skip2 = move_it_in_display_line_to (it, to_charpos, -1,
9085 op & MOVE_TO_POS);
9086 TRACE_MOVE ((stderr, "move_it: to %d\n", IT_CHARPOS (*it)));
9087 line_height = it->max_ascent + it->max_descent;
9088 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
9090 if (to_y >= it->current_y
9091 && to_y < it->current_y + line_height)
9093 /* If TO_Y is in this line and TO_X was reached
9094 above, we scanned too far. We have to restore
9095 IT's settings to the ones before skipping. But
9096 keep the more accurate values of max_ascent and
9097 max_descent we've found while skipping the rest
9098 of the line, for the sake of callers, such as
9099 pos_visible_p, that need to know the line
9100 height. */
9101 int max_ascent = it->max_ascent;
9102 int max_descent = it->max_descent;
9104 RESTORE_IT (it, &it_backup, backup_data);
9105 it->max_ascent = max_ascent;
9106 it->max_descent = max_descent;
9107 reached = 6;
9109 else
9111 skip = skip2;
9112 if (skip == MOVE_POS_MATCH_OR_ZV)
9113 reached = 7;
9116 else
9118 /* Check whether TO_Y is in this line. */
9119 line_height = it->max_ascent + it->max_descent;
9120 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
9122 if (to_y >= it->current_y
9123 && to_y < it->current_y + line_height)
9125 if (to_y > it->current_y)
9126 max_current_x = max (it->current_x, max_current_x);
9128 /* When word-wrap is on, TO_X may lie past the end
9129 of a wrapped line. Then it->current is the
9130 character on the next line, so backtrack to the
9131 space before the wrap point. */
9132 if (skip == MOVE_LINE_CONTINUED
9133 && it->line_wrap == WORD_WRAP)
9135 int prev_x = max (it->current_x - 1, 0);
9136 RESTORE_IT (it, &it_backup, backup_data);
9137 skip = move_it_in_display_line_to
9138 (it, -1, prev_x, MOVE_TO_X);
9141 reached = 6;
9145 if (reached)
9147 max_current_x = max (it->current_x, max_current_x);
9148 break;
9151 else if (BUFFERP (it->object)
9152 && (it->method == GET_FROM_BUFFER
9153 || it->method == GET_FROM_STRETCH)
9154 && IT_CHARPOS (*it) >= to_charpos
9155 /* Under bidi iteration, a call to set_iterator_to_next
9156 can scan far beyond to_charpos if the initial
9157 portion of the next line needs to be reordered. In
9158 that case, give move_it_in_display_line_to another
9159 chance below. */
9160 && !(it->bidi_p
9161 && it->bidi_it.scan_dir == -1))
9162 skip = MOVE_POS_MATCH_OR_ZV;
9163 else
9164 skip = move_it_in_display_line_to (it, to_charpos, -1, MOVE_TO_POS);
9166 switch (skip)
9168 case MOVE_POS_MATCH_OR_ZV:
9169 max_current_x = max (it->current_x, max_current_x);
9170 reached = 8;
9171 goto out;
9173 case MOVE_NEWLINE_OR_CR:
9174 max_current_x = max (it->current_x, max_current_x);
9175 set_iterator_to_next (it, 1);
9176 it->continuation_lines_width = 0;
9177 break;
9179 case MOVE_LINE_TRUNCATED:
9180 max_current_x = it->last_visible_x;
9181 it->continuation_lines_width = 0;
9182 reseat_at_next_visible_line_start (it, 0);
9183 if ((op & MOVE_TO_POS) != 0
9184 && IT_CHARPOS (*it) > to_charpos)
9186 reached = 9;
9187 goto out;
9189 break;
9191 case MOVE_LINE_CONTINUED:
9192 max_current_x = it->last_visible_x;
9193 /* For continued lines ending in a tab, some of the glyphs
9194 associated with the tab are displayed on the current
9195 line. Since it->current_x does not include these glyphs,
9196 we use it->last_visible_x instead. */
9197 if (it->c == '\t')
9199 it->continuation_lines_width += it->last_visible_x;
9200 /* When moving by vpos, ensure that the iterator really
9201 advances to the next line (bug#847, bug#969). Fixme:
9202 do we need to do this in other circumstances? */
9203 if (it->current_x != it->last_visible_x
9204 && (op & MOVE_TO_VPOS)
9205 && !(op & (MOVE_TO_X | MOVE_TO_POS)))
9207 line_start_x = it->current_x + it->pixel_width
9208 - it->last_visible_x;
9209 if (FRAME_WINDOW_P (it->f))
9211 struct face *face = FACE_FROM_ID (it->f, it->face_id);
9212 struct font *face_font = face->font;
9214 /* When display_line produces a continued line
9215 that ends in a TAB, it skips a tab stop that
9216 is closer than the font's space character
9217 width (see x_produce_glyphs where it produces
9218 the stretch glyph which represents a TAB).
9219 We need to reproduce the same logic here. */
9220 eassert (face_font);
9221 if (face_font)
9223 if (line_start_x < face_font->space_width)
9224 line_start_x
9225 += it->tab_width * face_font->space_width;
9228 set_iterator_to_next (it, 0);
9231 else
9232 it->continuation_lines_width += it->current_x;
9233 break;
9235 default:
9236 emacs_abort ();
9239 /* Reset/increment for the next run. */
9240 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
9241 it->current_x = line_start_x;
9242 line_start_x = 0;
9243 it->hpos = 0;
9244 it->current_y += it->max_ascent + it->max_descent;
9245 ++it->vpos;
9246 last_height = it->max_ascent + it->max_descent;
9247 it->max_ascent = it->max_descent = 0;
9250 out:
9252 /* On text terminals, we may stop at the end of a line in the middle
9253 of a multi-character glyph. If the glyph itself is continued,
9254 i.e. it is actually displayed on the next line, don't treat this
9255 stopping point as valid; move to the next line instead (unless
9256 that brings us offscreen). */
9257 if (!FRAME_WINDOW_P (it->f)
9258 && op & MOVE_TO_POS
9259 && IT_CHARPOS (*it) == to_charpos
9260 && it->what == IT_CHARACTER
9261 && it->nglyphs > 1
9262 && it->line_wrap == WINDOW_WRAP
9263 && it->current_x == it->last_visible_x - 1
9264 && it->c != '\n'
9265 && it->c != '\t'
9266 && it->w->window_end_valid
9267 && it->vpos < it->w->window_end_vpos)
9269 it->continuation_lines_width += it->current_x;
9270 it->current_x = it->hpos = it->max_ascent = it->max_descent = 0;
9271 it->current_y += it->max_ascent + it->max_descent;
9272 ++it->vpos;
9273 last_height = it->max_ascent + it->max_descent;
9276 if (backup_data)
9277 bidi_unshelve_cache (backup_data, 1);
9279 TRACE_MOVE ((stderr, "move_it_to: reached %d\n", reached));
9281 return max_current_x;
9285 /* Move iterator IT backward by a specified y-distance DY, DY >= 0.
9287 If DY > 0, move IT backward at least that many pixels. DY = 0
9288 means move IT backward to the preceding line start or BEGV. This
9289 function may move over more than DY pixels if IT->current_y - DY
9290 ends up in the middle of a line; in this case IT->current_y will be
9291 set to the top of the line moved to. */
9293 void
9294 move_it_vertically_backward (struct it *it, int dy)
9296 int nlines, h;
9297 struct it it2, it3;
9298 void *it2data = NULL, *it3data = NULL;
9299 ptrdiff_t start_pos;
9300 int nchars_per_row
9301 = (it->last_visible_x - it->first_visible_x) / FRAME_COLUMN_WIDTH (it->f);
9302 ptrdiff_t pos_limit;
9304 move_further_back:
9305 eassert (dy >= 0);
9307 start_pos = IT_CHARPOS (*it);
9309 /* Estimate how many newlines we must move back. */
9310 nlines = max (1, dy / default_line_pixel_height (it->w));
9311 if (it->line_wrap == TRUNCATE || nchars_per_row == 0)
9312 pos_limit = BEGV;
9313 else
9314 pos_limit = max (start_pos - nlines * nchars_per_row, BEGV);
9316 /* Set the iterator's position that many lines back. But don't go
9317 back more than NLINES full screen lines -- this wins a day with
9318 buffers which have very long lines. */
9319 while (nlines-- && IT_CHARPOS (*it) > pos_limit)
9320 back_to_previous_visible_line_start (it);
9322 /* Reseat the iterator here. When moving backward, we don't want
9323 reseat to skip forward over invisible text, set up the iterator
9324 to deliver from overlay strings at the new position etc. So,
9325 use reseat_1 here. */
9326 reseat_1 (it, it->current.pos, 1);
9328 /* We are now surely at a line start. */
9329 it->current_x = it->hpos = 0; /* FIXME: this is incorrect when bidi
9330 reordering is in effect. */
9331 it->continuation_lines_width = 0;
9333 /* Move forward and see what y-distance we moved. First move to the
9334 start of the next line so that we get its height. We need this
9335 height to be able to tell whether we reached the specified
9336 y-distance. */
9337 SAVE_IT (it2, *it, it2data);
9338 it2.max_ascent = it2.max_descent = 0;
9341 move_it_to (&it2, start_pos, -1, -1, it2.vpos + 1,
9342 MOVE_TO_POS | MOVE_TO_VPOS);
9344 while (!(IT_POS_VALID_AFTER_MOVE_P (&it2)
9345 /* If we are in a display string which starts at START_POS,
9346 and that display string includes a newline, and we are
9347 right after that newline (i.e. at the beginning of a
9348 display line), exit the loop, because otherwise we will
9349 infloop, since move_it_to will see that it is already at
9350 START_POS and will not move. */
9351 || (it2.method == GET_FROM_STRING
9352 && IT_CHARPOS (it2) == start_pos
9353 && SREF (it2.string, IT_STRING_BYTEPOS (it2) - 1) == '\n')));
9354 eassert (IT_CHARPOS (*it) >= BEGV);
9355 SAVE_IT (it3, it2, it3data);
9357 move_it_to (&it2, start_pos, -1, -1, -1, MOVE_TO_POS);
9358 eassert (IT_CHARPOS (*it) >= BEGV);
9359 /* H is the actual vertical distance from the position in *IT
9360 and the starting position. */
9361 h = it2.current_y - it->current_y;
9362 /* NLINES is the distance in number of lines. */
9363 nlines = it2.vpos - it->vpos;
9365 /* Correct IT's y and vpos position
9366 so that they are relative to the starting point. */
9367 it->vpos -= nlines;
9368 it->current_y -= h;
9370 if (dy == 0)
9372 /* DY == 0 means move to the start of the screen line. The
9373 value of nlines is > 0 if continuation lines were involved,
9374 or if the original IT position was at start of a line. */
9375 RESTORE_IT (it, it, it2data);
9376 if (nlines > 0)
9377 move_it_by_lines (it, nlines);
9378 /* The above code moves us to some position NLINES down,
9379 usually to its first glyph (leftmost in an L2R line), but
9380 that's not necessarily the start of the line, under bidi
9381 reordering. We want to get to the character position
9382 that is immediately after the newline of the previous
9383 line. */
9384 if (it->bidi_p
9385 && !it->continuation_lines_width
9386 && !STRINGP (it->string)
9387 && IT_CHARPOS (*it) > BEGV
9388 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
9390 ptrdiff_t cp = IT_CHARPOS (*it), bp = IT_BYTEPOS (*it);
9392 DEC_BOTH (cp, bp);
9393 cp = find_newline_no_quit (cp, bp, -1, NULL);
9394 move_it_to (it, cp, -1, -1, -1, MOVE_TO_POS);
9396 bidi_unshelve_cache (it3data, 1);
9398 else
9400 /* The y-position we try to reach, relative to *IT.
9401 Note that H has been subtracted in front of the if-statement. */
9402 int target_y = it->current_y + h - dy;
9403 int y0 = it3.current_y;
9404 int y1;
9405 int line_height;
9407 RESTORE_IT (&it3, &it3, it3data);
9408 y1 = line_bottom_y (&it3);
9409 line_height = y1 - y0;
9410 RESTORE_IT (it, it, it2data);
9411 /* If we did not reach target_y, try to move further backward if
9412 we can. If we moved too far backward, try to move forward. */
9413 if (target_y < it->current_y
9414 /* This is heuristic. In a window that's 3 lines high, with
9415 a line height of 13 pixels each, recentering with point
9416 on the bottom line will try to move -39/2 = 19 pixels
9417 backward. Try to avoid moving into the first line. */
9418 && (it->current_y - target_y
9419 > min (window_box_height (it->w), line_height * 2 / 3))
9420 && IT_CHARPOS (*it) > BEGV)
9422 TRACE_MOVE ((stderr, " not far enough -> move_vert %d\n",
9423 target_y - it->current_y));
9424 dy = it->current_y - target_y;
9425 goto move_further_back;
9427 else if (target_y >= it->current_y + line_height
9428 && IT_CHARPOS (*it) < ZV)
9430 /* Should move forward by at least one line, maybe more.
9432 Note: Calling move_it_by_lines can be expensive on
9433 terminal frames, where compute_motion is used (via
9434 vmotion) to do the job, when there are very long lines
9435 and truncate-lines is nil. That's the reason for
9436 treating terminal frames specially here. */
9438 if (!FRAME_WINDOW_P (it->f))
9439 move_it_vertically (it, target_y - (it->current_y + line_height));
9440 else
9444 move_it_by_lines (it, 1);
9446 while (target_y >= line_bottom_y (it) && IT_CHARPOS (*it) < ZV);
9453 /* Move IT by a specified amount of pixel lines DY. DY negative means
9454 move backwards. DY = 0 means move to start of screen line. At the
9455 end, IT will be on the start of a screen line. */
9457 void
9458 move_it_vertically (struct it *it, int dy)
9460 if (dy <= 0)
9461 move_it_vertically_backward (it, -dy);
9462 else
9464 TRACE_MOVE ((stderr, "move_it_v: from %d, %d\n", IT_CHARPOS (*it), dy));
9465 move_it_to (it, ZV, -1, it->current_y + dy, -1,
9466 MOVE_TO_POS | MOVE_TO_Y);
9467 TRACE_MOVE ((stderr, "move_it_v: to %d\n", IT_CHARPOS (*it)));
9469 /* If buffer ends in ZV without a newline, move to the start of
9470 the line to satisfy the post-condition. */
9471 if (IT_CHARPOS (*it) == ZV
9472 && ZV > BEGV
9473 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
9474 move_it_by_lines (it, 0);
9479 /* Move iterator IT past the end of the text line it is in. */
9481 void
9482 move_it_past_eol (struct it *it)
9484 enum move_it_result rc;
9486 rc = move_it_in_display_line_to (it, Z, 0, MOVE_TO_POS);
9487 if (rc == MOVE_NEWLINE_OR_CR)
9488 set_iterator_to_next (it, 0);
9492 /* Move IT by a specified number DVPOS of screen lines down. DVPOS
9493 negative means move up. DVPOS == 0 means move to the start of the
9494 screen line.
9496 Optimization idea: If we would know that IT->f doesn't use
9497 a face with proportional font, we could be faster for
9498 truncate-lines nil. */
9500 void
9501 move_it_by_lines (struct it *it, ptrdiff_t dvpos)
9504 /* The commented-out optimization uses vmotion on terminals. This
9505 gives bad results, because elements like it->what, on which
9506 callers such as pos_visible_p rely, aren't updated. */
9507 /* struct position pos;
9508 if (!FRAME_WINDOW_P (it->f))
9510 struct text_pos textpos;
9512 pos = *vmotion (IT_CHARPOS (*it), dvpos, it->w);
9513 SET_TEXT_POS (textpos, pos.bufpos, pos.bytepos);
9514 reseat (it, textpos, 1);
9515 it->vpos += pos.vpos;
9516 it->current_y += pos.vpos;
9518 else */
9520 if (dvpos == 0)
9522 /* DVPOS == 0 means move to the start of the screen line. */
9523 move_it_vertically_backward (it, 0);
9524 /* Let next call to line_bottom_y calculate real line height. */
9525 last_height = 0;
9527 else if (dvpos > 0)
9529 move_it_to (it, -1, -1, -1, it->vpos + dvpos, MOVE_TO_VPOS);
9530 if (!IT_POS_VALID_AFTER_MOVE_P (it))
9532 /* Only move to the next buffer position if we ended up in a
9533 string from display property, not in an overlay string
9534 (before-string or after-string). That is because the
9535 latter don't conceal the underlying buffer position, so
9536 we can ask to move the iterator to the exact position we
9537 are interested in. Note that, even if we are already at
9538 IT_CHARPOS (*it), the call below is not a no-op, as it
9539 will detect that we are at the end of the string, pop the
9540 iterator, and compute it->current_x and it->hpos
9541 correctly. */
9542 move_it_to (it, IT_CHARPOS (*it) + it->string_from_display_prop_p,
9543 -1, -1, -1, MOVE_TO_POS);
9546 else
9548 struct it it2;
9549 void *it2data = NULL;
9550 ptrdiff_t start_charpos, i;
9551 int nchars_per_row
9552 = (it->last_visible_x - it->first_visible_x) / FRAME_COLUMN_WIDTH (it->f);
9553 bool hit_pos_limit = false;
9554 ptrdiff_t pos_limit;
9556 /* Start at the beginning of the screen line containing IT's
9557 position. This may actually move vertically backwards,
9558 in case of overlays, so adjust dvpos accordingly. */
9559 dvpos += it->vpos;
9560 move_it_vertically_backward (it, 0);
9561 dvpos -= it->vpos;
9563 /* Go back -DVPOS buffer lines, but no farther than -DVPOS full
9564 screen lines, and reseat the iterator there. */
9565 start_charpos = IT_CHARPOS (*it);
9566 if (it->line_wrap == TRUNCATE || nchars_per_row == 0)
9567 pos_limit = BEGV;
9568 else
9569 pos_limit = max (start_charpos + dvpos * nchars_per_row, BEGV);
9571 for (i = -dvpos; i > 0 && IT_CHARPOS (*it) > pos_limit; --i)
9572 back_to_previous_visible_line_start (it);
9573 if (i > 0 && IT_CHARPOS (*it) <= pos_limit)
9574 hit_pos_limit = true;
9575 reseat (it, it->current.pos, 1);
9577 /* Move further back if we end up in a string or an image. */
9578 while (!IT_POS_VALID_AFTER_MOVE_P (it))
9580 /* First try to move to start of display line. */
9581 dvpos += it->vpos;
9582 move_it_vertically_backward (it, 0);
9583 dvpos -= it->vpos;
9584 if (IT_POS_VALID_AFTER_MOVE_P (it))
9585 break;
9586 /* If start of line is still in string or image,
9587 move further back. */
9588 back_to_previous_visible_line_start (it);
9589 reseat (it, it->current.pos, 1);
9590 dvpos--;
9593 it->current_x = it->hpos = 0;
9595 /* Above call may have moved too far if continuation lines
9596 are involved. Scan forward and see if it did. */
9597 SAVE_IT (it2, *it, it2data);
9598 it2.vpos = it2.current_y = 0;
9599 move_it_to (&it2, start_charpos, -1, -1, -1, MOVE_TO_POS);
9600 it->vpos -= it2.vpos;
9601 it->current_y -= it2.current_y;
9602 it->current_x = it->hpos = 0;
9604 /* If we moved too far back, move IT some lines forward. */
9605 if (it2.vpos > -dvpos)
9607 int delta = it2.vpos + dvpos;
9609 RESTORE_IT (&it2, &it2, it2data);
9610 SAVE_IT (it2, *it, it2data);
9611 move_it_to (it, -1, -1, -1, it->vpos + delta, MOVE_TO_VPOS);
9612 /* Move back again if we got too far ahead. */
9613 if (IT_CHARPOS (*it) >= start_charpos)
9614 RESTORE_IT (it, &it2, it2data);
9615 else
9616 bidi_unshelve_cache (it2data, 1);
9618 else if (hit_pos_limit && pos_limit > BEGV
9619 && dvpos < 0 && it2.vpos < -dvpos)
9621 /* If we hit the limit, but still didn't make it far enough
9622 back, that means there's a display string with a newline
9623 covering a large chunk of text, and that caused
9624 back_to_previous_visible_line_start try to go too far.
9625 Punish those who commit such atrocities by going back
9626 until we've reached DVPOS, after lifting the limit, which
9627 could make it slow for very long lines. "If it hurts,
9628 don't do that!" */
9629 dvpos += it2.vpos;
9630 RESTORE_IT (it, it, it2data);
9631 for (i = -dvpos; i > 0; --i)
9633 back_to_previous_visible_line_start (it);
9634 it->vpos--;
9636 reseat_1 (it, it->current.pos, 1);
9638 else
9639 RESTORE_IT (it, it, it2data);
9643 /* Return true if IT points into the middle of a display vector. */
9645 bool
9646 in_display_vector_p (struct it *it)
9648 return (it->method == GET_FROM_DISPLAY_VECTOR
9649 && it->current.dpvec_index > 0
9650 && it->dpvec + it->current.dpvec_index != it->dpend);
9653 DEFUN ("window-text-pixel-size", Fwindow_text_pixel_size, Swindow_text_pixel_size, 0, 6, 0,
9654 doc: /* Return the size of the text of WINDOW's buffer in pixels.
9655 WINDOW must be a live window and defaults to the selected one. The
9656 return value is a cons of the maximum pixel-width of any text line and
9657 the maximum pixel-height of all text lines.
9659 The optional argument FROM, if non-nil, specifies the first text
9660 position and defaults to the minimum accessible position of the buffer.
9661 If FROM is t, use the minimum accessible position that is not a newline
9662 character. TO, if non-nil, specifies the last text position and
9663 defaults to the maximum accessible position of the buffer. If TO is t,
9664 use the maximum accessible position that is not a newline character.
9666 The optional argument X-LIMIT, if non-nil, specifies the maximum text
9667 width that can be returned. X-LIMIT nil or omitted, means to use the
9668 pixel-width of WINDOW's body; use this if you do not intend to change
9669 the width of WINDOW. Use the maximum width WINDOW may assume if you
9670 intend to change WINDOW's width. In any case, text whose x-coordinate
9671 is beyond X-LIMIT is ignored. Since calculating the width of long lines
9672 can take some time, it's always a good idea to make this argument as
9673 small as possible; in particular, if the buffer contains long lines that
9674 shall be truncated anyway.
9676 The optional argument Y-LIMIT, if non-nil, specifies the maximum text
9677 height that can be returned. Text lines whose y-coordinate is beyond
9678 Y-LIMIT are ignored. Since calculating the text height of a large
9679 buffer can take some time, it makes sense to specify this argument if
9680 the size of the buffer is unknown.
9682 Optional argument MODE-AND-HEADER-LINE nil or omitted means do not
9683 include the height of the mode- or header-line of WINDOW in the return
9684 value. If it is either the symbol `mode-line' or `header-line', include
9685 only the height of that line, if present, in the return value. If t,
9686 include the height of both, if present, in the return value. */)
9687 (Lisp_Object window, Lisp_Object from, Lisp_Object to, Lisp_Object x_limit,
9688 Lisp_Object y_limit, Lisp_Object mode_and_header_line)
9690 struct window *w = decode_live_window (window);
9691 Lisp_Object buffer = w->contents;
9692 struct buffer *b;
9693 struct it it;
9694 struct buffer *old_b = NULL;
9695 ptrdiff_t start, end, pos;
9696 struct text_pos startp;
9697 void *itdata = NULL;
9698 int c, max_y = -1, x = 0, y = 0;
9700 CHECK_BUFFER (buffer);
9701 b = XBUFFER (buffer);
9703 if (b != current_buffer)
9705 old_b = current_buffer;
9706 set_buffer_internal (b);
9709 if (NILP (from))
9710 start = BEGV;
9711 else if (EQ (from, Qt))
9713 start = pos = BEGV;
9714 while ((pos++ < ZV) && (c = FETCH_CHAR (pos))
9715 && (c == ' ' || c == '\t' || c == '\n' || c == '\r'))
9716 start = pos;
9717 while ((pos-- > BEGV) && (c = FETCH_CHAR (pos)) && (c == ' ' || c == '\t'))
9718 start = pos;
9720 else
9722 CHECK_NUMBER_COERCE_MARKER (from);
9723 start = min (max (XINT (from), BEGV), ZV);
9726 if (NILP (to))
9727 end = ZV;
9728 else if (EQ (to, Qt))
9730 end = pos = ZV;
9731 while ((pos-- > BEGV) && (c = FETCH_CHAR (pos))
9732 && (c == ' ' || c == '\t' || c == '\n' || c == '\r'))
9733 end = pos;
9734 while ((pos++ < ZV) && (c = FETCH_CHAR (pos)) && (c == ' ' || c == '\t'))
9735 end = pos;
9737 else
9739 CHECK_NUMBER_COERCE_MARKER (to);
9740 end = max (start, min (XINT (to), ZV));
9743 if (!NILP (y_limit))
9745 CHECK_NUMBER (y_limit);
9746 max_y = min (XINT (y_limit), INT_MAX);
9749 itdata = bidi_shelve_cache ();
9750 SET_TEXT_POS (startp, start, CHAR_TO_BYTE (start));
9751 start_display (&it, w, startp);
9753 if (NILP (x_limit))
9754 x = move_it_to (&it, end, -1, max_y, -1, MOVE_TO_POS | MOVE_TO_Y);
9755 else
9757 CHECK_NUMBER (x_limit);
9758 it.last_visible_x = min (XINT (x_limit), INFINITY);
9759 /* Actually, we never want move_it_to stop at to_x. But to make
9760 sure that move_it_in_display_line_to always moves far enough,
9761 we set it to INT_MAX and specify MOVE_TO_X. */
9762 x = move_it_to (&it, end, INT_MAX, max_y, -1,
9763 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
9766 y = it.current_y + it.max_ascent + it.max_descent;
9768 if (!EQ (mode_and_header_line, Qheader_line)
9769 && !EQ (mode_and_header_line, Qt))
9770 /* Do not count the header-line which was counted automatically by
9771 start_display. */
9772 y = y - WINDOW_HEADER_LINE_HEIGHT (w);
9774 if (EQ (mode_and_header_line, Qmode_line)
9775 || EQ (mode_and_header_line, Qt))
9776 /* Do count the mode-line which is not included automatically by
9777 start_display. */
9778 y = y + WINDOW_MODE_LINE_HEIGHT (w);
9780 bidi_unshelve_cache (itdata, 0);
9782 if (old_b)
9783 set_buffer_internal (old_b);
9785 return Fcons (make_number (x), make_number (y));
9788 /***********************************************************************
9789 Messages
9790 ***********************************************************************/
9793 /* Add a message with format string FORMAT and arguments ARG1 and ARG2
9794 to *Messages*. */
9796 void
9797 add_to_log (const char *format, Lisp_Object arg1, Lisp_Object arg2)
9799 Lisp_Object msg, fmt;
9800 char *buffer;
9801 ptrdiff_t len;
9802 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
9803 USE_SAFE_ALLOCA;
9805 fmt = msg = Qnil;
9806 GCPRO4 (fmt, msg, arg1, arg2);
9808 fmt = build_string (format);
9809 msg = CALLN (Fformat, fmt, arg1, arg2);
9811 len = SBYTES (msg) + 1;
9812 buffer = SAFE_ALLOCA (len);
9813 memcpy (buffer, SDATA (msg), len);
9815 message_dolog (buffer, len - 1, 1, 0);
9816 SAFE_FREE ();
9818 UNGCPRO;
9822 /* Output a newline in the *Messages* buffer if "needs" one. */
9824 void
9825 message_log_maybe_newline (void)
9827 if (message_log_need_newline)
9828 message_dolog ("", 0, 1, 0);
9832 /* Add a string M of length NBYTES to the message log, optionally
9833 terminated with a newline when NLFLAG is true. MULTIBYTE, if
9834 true, means interpret the contents of M as multibyte. This
9835 function calls low-level routines in order to bypass text property
9836 hooks, etc. which might not be safe to run.
9838 This may GC (insert may run before/after change hooks),
9839 so the buffer M must NOT point to a Lisp string. */
9841 void
9842 message_dolog (const char *m, ptrdiff_t nbytes, bool nlflag, bool multibyte)
9844 const unsigned char *msg = (const unsigned char *) m;
9846 if (!NILP (Vmemory_full))
9847 return;
9849 if (!NILP (Vmessage_log_max))
9851 struct buffer *oldbuf;
9852 Lisp_Object oldpoint, oldbegv, oldzv;
9853 int old_windows_or_buffers_changed = windows_or_buffers_changed;
9854 ptrdiff_t point_at_end = 0;
9855 ptrdiff_t zv_at_end = 0;
9856 Lisp_Object old_deactivate_mark;
9857 struct gcpro gcpro1;
9859 old_deactivate_mark = Vdeactivate_mark;
9860 oldbuf = current_buffer;
9862 /* Ensure the Messages buffer exists, and switch to it.
9863 If we created it, set the major-mode. */
9865 int newbuffer = 0;
9866 if (NILP (Fget_buffer (Vmessages_buffer_name))) newbuffer = 1;
9868 Fset_buffer (Fget_buffer_create (Vmessages_buffer_name));
9870 if (newbuffer
9871 && !NILP (Ffboundp (intern ("messages-buffer-mode"))))
9872 call0 (intern ("messages-buffer-mode"));
9875 bset_undo_list (current_buffer, Qt);
9876 bset_cache_long_scans (current_buffer, Qnil);
9878 oldpoint = message_dolog_marker1;
9879 set_marker_restricted_both (oldpoint, Qnil, PT, PT_BYTE);
9880 oldbegv = message_dolog_marker2;
9881 set_marker_restricted_both (oldbegv, Qnil, BEGV, BEGV_BYTE);
9882 oldzv = message_dolog_marker3;
9883 set_marker_restricted_both (oldzv, Qnil, ZV, ZV_BYTE);
9884 GCPRO1 (old_deactivate_mark);
9886 if (PT == Z)
9887 point_at_end = 1;
9888 if (ZV == Z)
9889 zv_at_end = 1;
9891 BEGV = BEG;
9892 BEGV_BYTE = BEG_BYTE;
9893 ZV = Z;
9894 ZV_BYTE = Z_BYTE;
9895 TEMP_SET_PT_BOTH (Z, Z_BYTE);
9897 /* Insert the string--maybe converting multibyte to single byte
9898 or vice versa, so that all the text fits the buffer. */
9899 if (multibyte
9900 && NILP (BVAR (current_buffer, enable_multibyte_characters)))
9902 ptrdiff_t i;
9903 int c, char_bytes;
9904 char work[1];
9906 /* Convert a multibyte string to single-byte
9907 for the *Message* buffer. */
9908 for (i = 0; i < nbytes; i += char_bytes)
9910 c = string_char_and_length (msg + i, &char_bytes);
9911 work[0] = CHAR_TO_BYTE8 (c);
9912 insert_1_both (work, 1, 1, 1, 0, 0);
9915 else if (! multibyte
9916 && ! NILP (BVAR (current_buffer, enable_multibyte_characters)))
9918 ptrdiff_t i;
9919 int c, char_bytes;
9920 unsigned char str[MAX_MULTIBYTE_LENGTH];
9921 /* Convert a single-byte string to multibyte
9922 for the *Message* buffer. */
9923 for (i = 0; i < nbytes; i++)
9925 c = msg[i];
9926 MAKE_CHAR_MULTIBYTE (c);
9927 char_bytes = CHAR_STRING (c, str);
9928 insert_1_both ((char *) str, 1, char_bytes, 1, 0, 0);
9931 else if (nbytes)
9932 insert_1_both (m, chars_in_text (msg, nbytes), nbytes, 1, 0, 0);
9934 if (nlflag)
9936 ptrdiff_t this_bol, this_bol_byte, prev_bol, prev_bol_byte;
9937 printmax_t dups;
9939 insert_1_both ("\n", 1, 1, 1, 0, 0);
9941 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE, -2, 0);
9942 this_bol = PT;
9943 this_bol_byte = PT_BYTE;
9945 /* See if this line duplicates the previous one.
9946 If so, combine duplicates. */
9947 if (this_bol > BEG)
9949 scan_newline (PT, PT_BYTE, BEG, BEG_BYTE, -2, 0);
9950 prev_bol = PT;
9951 prev_bol_byte = PT_BYTE;
9953 dups = message_log_check_duplicate (prev_bol_byte,
9954 this_bol_byte);
9955 if (dups)
9957 del_range_both (prev_bol, prev_bol_byte,
9958 this_bol, this_bol_byte, 0);
9959 if (dups > 1)
9961 char dupstr[sizeof " [ times]"
9962 + INT_STRLEN_BOUND (printmax_t)];
9964 /* If you change this format, don't forget to also
9965 change message_log_check_duplicate. */
9966 int duplen = sprintf (dupstr, " [%"pMd" times]", dups);
9967 TEMP_SET_PT_BOTH (Z - 1, Z_BYTE - 1);
9968 insert_1_both (dupstr, duplen, duplen, 1, 0, 1);
9973 /* If we have more than the desired maximum number of lines
9974 in the *Messages* buffer now, delete the oldest ones.
9975 This is safe because we don't have undo in this buffer. */
9977 if (NATNUMP (Vmessage_log_max))
9979 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE,
9980 -XFASTINT (Vmessage_log_max) - 1, 0);
9981 del_range_both (BEG, BEG_BYTE, PT, PT_BYTE, 0);
9984 BEGV = marker_position (oldbegv);
9985 BEGV_BYTE = marker_byte_position (oldbegv);
9987 if (zv_at_end)
9989 ZV = Z;
9990 ZV_BYTE = Z_BYTE;
9992 else
9994 ZV = marker_position (oldzv);
9995 ZV_BYTE = marker_byte_position (oldzv);
9998 if (point_at_end)
9999 TEMP_SET_PT_BOTH (Z, Z_BYTE);
10000 else
10001 /* We can't do Fgoto_char (oldpoint) because it will run some
10002 Lisp code. */
10003 TEMP_SET_PT_BOTH (marker_position (oldpoint),
10004 marker_byte_position (oldpoint));
10006 UNGCPRO;
10007 unchain_marker (XMARKER (oldpoint));
10008 unchain_marker (XMARKER (oldbegv));
10009 unchain_marker (XMARKER (oldzv));
10011 /* We called insert_1_both above with its 5th argument (PREPARE)
10012 zero, which prevents insert_1_both from calling
10013 prepare_to_modify_buffer, which in turns prevents us from
10014 incrementing windows_or_buffers_changed even if *Messages* is
10015 shown in some window. So we must manually set
10016 windows_or_buffers_changed here to make up for that. */
10017 windows_or_buffers_changed = old_windows_or_buffers_changed;
10018 bset_redisplay (current_buffer);
10020 set_buffer_internal (oldbuf);
10022 message_log_need_newline = !nlflag;
10023 Vdeactivate_mark = old_deactivate_mark;
10028 /* We are at the end of the buffer after just having inserted a newline.
10029 (Note: We depend on the fact we won't be crossing the gap.)
10030 Check to see if the most recent message looks a lot like the previous one.
10031 Return 0 if different, 1 if the new one should just replace it, or a
10032 value N > 1 if we should also append " [N times]". */
10034 static intmax_t
10035 message_log_check_duplicate (ptrdiff_t prev_bol_byte, ptrdiff_t this_bol_byte)
10037 ptrdiff_t i;
10038 ptrdiff_t len = Z_BYTE - 1 - this_bol_byte;
10039 int seen_dots = 0;
10040 unsigned char *p1 = BUF_BYTE_ADDRESS (current_buffer, prev_bol_byte);
10041 unsigned char *p2 = BUF_BYTE_ADDRESS (current_buffer, this_bol_byte);
10043 for (i = 0; i < len; i++)
10045 if (i >= 3 && p1[i - 3] == '.' && p1[i - 2] == '.' && p1[i - 1] == '.')
10046 seen_dots = 1;
10047 if (p1[i] != p2[i])
10048 return seen_dots;
10050 p1 += len;
10051 if (*p1 == '\n')
10052 return 2;
10053 if (*p1++ == ' ' && *p1++ == '[')
10055 char *pend;
10056 intmax_t n = strtoimax ((char *) p1, &pend, 10);
10057 if (0 < n && n < INTMAX_MAX && strncmp (pend, " times]\n", 8) == 0)
10058 return n + 1;
10060 return 0;
10064 /* Display an echo area message M with a specified length of NBYTES
10065 bytes. The string may include null characters. If M is not a
10066 string, clear out any existing message, and let the mini-buffer
10067 text show through.
10069 This function cancels echoing. */
10071 void
10072 message3 (Lisp_Object m)
10074 struct gcpro gcpro1;
10076 GCPRO1 (m);
10077 clear_message (true, true);
10078 cancel_echoing ();
10080 /* First flush out any partial line written with print. */
10081 message_log_maybe_newline ();
10082 if (STRINGP (m))
10084 ptrdiff_t nbytes = SBYTES (m);
10085 bool multibyte = STRING_MULTIBYTE (m);
10086 char *buffer;
10087 USE_SAFE_ALLOCA;
10088 SAFE_ALLOCA_STRING (buffer, m);
10089 message_dolog (buffer, nbytes, 1, multibyte);
10090 SAFE_FREE ();
10092 message3_nolog (m);
10094 UNGCPRO;
10098 /* The non-logging version of message3.
10099 This does not cancel echoing, because it is used for echoing.
10100 Perhaps we need to make a separate function for echoing
10101 and make this cancel echoing. */
10103 void
10104 message3_nolog (Lisp_Object m)
10106 struct frame *sf = SELECTED_FRAME ();
10108 if (FRAME_INITIAL_P (sf))
10110 if (noninteractive_need_newline)
10111 putc ('\n', stderr);
10112 noninteractive_need_newline = 0;
10113 if (STRINGP (m))
10115 Lisp_Object s = ENCODE_SYSTEM (m);
10117 fwrite (SDATA (s), SBYTES (s), 1, stderr);
10119 if (cursor_in_echo_area == 0)
10120 fprintf (stderr, "\n");
10121 fflush (stderr);
10123 /* Error messages get reported properly by cmd_error, so this must be just an
10124 informative message; if the frame hasn't really been initialized yet, just
10125 toss it. */
10126 else if (INTERACTIVE && sf->glyphs_initialized_p)
10128 /* Get the frame containing the mini-buffer
10129 that the selected frame is using. */
10130 Lisp_Object mini_window = FRAME_MINIBUF_WINDOW (sf);
10131 Lisp_Object frame = XWINDOW (mini_window)->frame;
10132 struct frame *f = XFRAME (frame);
10134 if (FRAME_VISIBLE_P (sf) && !FRAME_VISIBLE_P (f))
10135 Fmake_frame_visible (frame);
10137 if (STRINGP (m) && SCHARS (m) > 0)
10139 set_message (m);
10140 if (minibuffer_auto_raise)
10141 Fraise_frame (frame);
10142 /* Assume we are not echoing.
10143 (If we are, echo_now will override this.) */
10144 echo_message_buffer = Qnil;
10146 else
10147 clear_message (true, true);
10149 do_pending_window_change (false);
10150 echo_area_display (true);
10151 do_pending_window_change (false);
10152 if (FRAME_TERMINAL (f)->frame_up_to_date_hook)
10153 (*FRAME_TERMINAL (f)->frame_up_to_date_hook) (f);
10158 /* Display a null-terminated echo area message M. If M is 0, clear
10159 out any existing message, and let the mini-buffer text show through.
10161 The buffer M must continue to exist until after the echo area gets
10162 cleared or some other message gets displayed there. Do not pass
10163 text that is stored in a Lisp string. Do not pass text in a buffer
10164 that was alloca'd. */
10166 void
10167 message1 (const char *m)
10169 message3 (m ? build_unibyte_string (m) : Qnil);
10173 /* The non-logging counterpart of message1. */
10175 void
10176 message1_nolog (const char *m)
10178 message3_nolog (m ? build_unibyte_string (m) : Qnil);
10181 /* Display a message M which contains a single %s
10182 which gets replaced with STRING. */
10184 void
10185 message_with_string (const char *m, Lisp_Object string, int log)
10187 CHECK_STRING (string);
10189 if (noninteractive)
10191 if (m)
10193 /* ENCODE_SYSTEM below can GC and/or relocate the
10194 Lisp data, so make sure we don't use it here. */
10195 eassert (relocatable_string_data_p (m) != 1);
10197 if (noninteractive_need_newline)
10198 putc ('\n', stderr);
10199 noninteractive_need_newline = 0;
10200 fprintf (stderr, m, SDATA (ENCODE_SYSTEM (string)));
10201 if (!cursor_in_echo_area)
10202 fprintf (stderr, "\n");
10203 fflush (stderr);
10206 else if (INTERACTIVE)
10208 /* The frame whose minibuffer we're going to display the message on.
10209 It may be larger than the selected frame, so we need
10210 to use its buffer, not the selected frame's buffer. */
10211 Lisp_Object mini_window;
10212 struct frame *f, *sf = SELECTED_FRAME ();
10214 /* Get the frame containing the minibuffer
10215 that the selected frame is using. */
10216 mini_window = FRAME_MINIBUF_WINDOW (sf);
10217 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
10219 /* Error messages get reported properly by cmd_error, so this must be
10220 just an informative message; if the frame hasn't really been
10221 initialized yet, just toss it. */
10222 if (f->glyphs_initialized_p)
10224 struct gcpro gcpro1, gcpro2;
10226 Lisp_Object fmt = build_string (m);
10227 Lisp_Object msg = string;
10228 GCPRO2 (fmt, msg);
10230 msg = CALLN (Fformat, fmt, msg);
10232 if (log)
10233 message3 (msg);
10234 else
10235 message3_nolog (msg);
10237 UNGCPRO;
10239 /* Print should start at the beginning of the message
10240 buffer next time. */
10241 message_buf_print = 0;
10247 /* Dump an informative message to the minibuf. If M is 0, clear out
10248 any existing message, and let the mini-buffer text show through. */
10250 static void
10251 vmessage (const char *m, va_list ap)
10253 if (noninteractive)
10255 if (m)
10257 if (noninteractive_need_newline)
10258 putc ('\n', stderr);
10259 noninteractive_need_newline = 0;
10260 vfprintf (stderr, m, ap);
10261 if (cursor_in_echo_area == 0)
10262 fprintf (stderr, "\n");
10263 fflush (stderr);
10266 else if (INTERACTIVE)
10268 /* The frame whose mini-buffer we're going to display the message
10269 on. It may be larger than the selected frame, so we need to
10270 use its buffer, not the selected frame's buffer. */
10271 Lisp_Object mini_window;
10272 struct frame *f, *sf = SELECTED_FRAME ();
10274 /* Get the frame containing the mini-buffer
10275 that the selected frame is using. */
10276 mini_window = FRAME_MINIBUF_WINDOW (sf);
10277 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
10279 /* Error messages get reported properly by cmd_error, so this must be
10280 just an informative message; if the frame hasn't really been
10281 initialized yet, just toss it. */
10282 if (f->glyphs_initialized_p)
10284 if (m)
10286 ptrdiff_t len;
10287 ptrdiff_t maxsize = FRAME_MESSAGE_BUF_SIZE (f);
10288 USE_SAFE_ALLOCA;
10289 char *message_buf = SAFE_ALLOCA (maxsize + 1);
10291 len = doprnt (message_buf, maxsize, m, 0, ap);
10293 message3 (make_string (message_buf, len));
10294 SAFE_FREE ();
10296 else
10297 message1 (0);
10299 /* Print should start at the beginning of the message
10300 buffer next time. */
10301 message_buf_print = 0;
10306 void
10307 message (const char *m, ...)
10309 va_list ap;
10310 va_start (ap, m);
10311 vmessage (m, ap);
10312 va_end (ap);
10316 #if 0
10317 /* The non-logging version of message. */
10319 void
10320 message_nolog (const char *m, ...)
10322 Lisp_Object old_log_max;
10323 va_list ap;
10324 va_start (ap, m);
10325 old_log_max = Vmessage_log_max;
10326 Vmessage_log_max = Qnil;
10327 vmessage (m, ap);
10328 Vmessage_log_max = old_log_max;
10329 va_end (ap);
10331 #endif
10334 /* Display the current message in the current mini-buffer. This is
10335 only called from error handlers in process.c, and is not time
10336 critical. */
10338 void
10339 update_echo_area (void)
10341 if (!NILP (echo_area_buffer[0]))
10343 Lisp_Object string;
10344 string = Fcurrent_message ();
10345 message3 (string);
10350 /* Make sure echo area buffers in `echo_buffers' are live.
10351 If they aren't, make new ones. */
10353 static void
10354 ensure_echo_area_buffers (void)
10356 int i;
10358 for (i = 0; i < 2; ++i)
10359 if (!BUFFERP (echo_buffer[i])
10360 || !BUFFER_LIVE_P (XBUFFER (echo_buffer[i])))
10362 char name[30];
10363 Lisp_Object old_buffer;
10364 int j;
10366 old_buffer = echo_buffer[i];
10367 echo_buffer[i] = Fget_buffer_create
10368 (make_formatted_string (name, " *Echo Area %d*", i));
10369 bset_truncate_lines (XBUFFER (echo_buffer[i]), Qnil);
10370 /* to force word wrap in echo area -
10371 it was decided to postpone this*/
10372 /* XBUFFER (echo_buffer[i])->word_wrap = Qt; */
10374 for (j = 0; j < 2; ++j)
10375 if (EQ (old_buffer, echo_area_buffer[j]))
10376 echo_area_buffer[j] = echo_buffer[i];
10381 /* Call FN with args A1..A2 with either the current or last displayed
10382 echo_area_buffer as current buffer.
10384 WHICH zero means use the current message buffer
10385 echo_area_buffer[0]. If that is nil, choose a suitable buffer
10386 from echo_buffer[] and clear it.
10388 WHICH > 0 means use echo_area_buffer[1]. If that is nil, choose a
10389 suitable buffer from echo_buffer[] and clear it.
10391 If WHICH < 0, set echo_area_buffer[1] to echo_area_buffer[0], so
10392 that the current message becomes the last displayed one, make
10393 choose a suitable buffer for echo_area_buffer[0], and clear it.
10395 Value is what FN returns. */
10397 static int
10398 with_echo_area_buffer (struct window *w, int which,
10399 int (*fn) (ptrdiff_t, Lisp_Object),
10400 ptrdiff_t a1, Lisp_Object a2)
10402 Lisp_Object buffer;
10403 int this_one, the_other, clear_buffer_p, rc;
10404 ptrdiff_t count = SPECPDL_INDEX ();
10406 /* If buffers aren't live, make new ones. */
10407 ensure_echo_area_buffers ();
10409 clear_buffer_p = 0;
10411 if (which == 0)
10412 this_one = 0, the_other = 1;
10413 else if (which > 0)
10414 this_one = 1, the_other = 0;
10415 else
10417 this_one = 0, the_other = 1;
10418 clear_buffer_p = true;
10420 /* We need a fresh one in case the current echo buffer equals
10421 the one containing the last displayed echo area message. */
10422 if (!NILP (echo_area_buffer[this_one])
10423 && EQ (echo_area_buffer[this_one], echo_area_buffer[the_other]))
10424 echo_area_buffer[this_one] = Qnil;
10427 /* Choose a suitable buffer from echo_buffer[] is we don't
10428 have one. */
10429 if (NILP (echo_area_buffer[this_one]))
10431 echo_area_buffer[this_one]
10432 = (EQ (echo_area_buffer[the_other], echo_buffer[this_one])
10433 ? echo_buffer[the_other]
10434 : echo_buffer[this_one]);
10435 clear_buffer_p = true;
10438 buffer = echo_area_buffer[this_one];
10440 /* Don't get confused by reusing the buffer used for echoing
10441 for a different purpose. */
10442 if (echo_kboard == NULL && EQ (buffer, echo_message_buffer))
10443 cancel_echoing ();
10445 record_unwind_protect (unwind_with_echo_area_buffer,
10446 with_echo_area_buffer_unwind_data (w));
10448 /* Make the echo area buffer current. Note that for display
10449 purposes, it is not necessary that the displayed window's buffer
10450 == current_buffer, except for text property lookup. So, let's
10451 only set that buffer temporarily here without doing a full
10452 Fset_window_buffer. We must also change w->pointm, though,
10453 because otherwise an assertions in unshow_buffer fails, and Emacs
10454 aborts. */
10455 set_buffer_internal_1 (XBUFFER (buffer));
10456 if (w)
10458 wset_buffer (w, buffer);
10459 set_marker_both (w->pointm, buffer, BEG, BEG_BYTE);
10460 set_marker_both (w->old_pointm, buffer, BEG, BEG_BYTE);
10463 bset_undo_list (current_buffer, Qt);
10464 bset_read_only (current_buffer, Qnil);
10465 specbind (Qinhibit_read_only, Qt);
10466 specbind (Qinhibit_modification_hooks, Qt);
10468 if (clear_buffer_p && Z > BEG)
10469 del_range (BEG, Z);
10471 eassert (BEGV >= BEG);
10472 eassert (ZV <= Z && ZV >= BEGV);
10474 rc = fn (a1, a2);
10476 eassert (BEGV >= BEG);
10477 eassert (ZV <= Z && ZV >= BEGV);
10479 unbind_to (count, Qnil);
10480 return rc;
10484 /* Save state that should be preserved around the call to the function
10485 FN called in with_echo_area_buffer. */
10487 static Lisp_Object
10488 with_echo_area_buffer_unwind_data (struct window *w)
10490 int i = 0;
10491 Lisp_Object vector, tmp;
10493 /* Reduce consing by keeping one vector in
10494 Vwith_echo_area_save_vector. */
10495 vector = Vwith_echo_area_save_vector;
10496 Vwith_echo_area_save_vector = Qnil;
10498 if (NILP (vector))
10499 vector = Fmake_vector (make_number (11), Qnil);
10501 XSETBUFFER (tmp, current_buffer); ASET (vector, i, tmp); ++i;
10502 ASET (vector, i, Vdeactivate_mark); ++i;
10503 ASET (vector, i, make_number (windows_or_buffers_changed)); ++i;
10505 if (w)
10507 XSETWINDOW (tmp, w); ASET (vector, i, tmp); ++i;
10508 ASET (vector, i, w->contents); ++i;
10509 ASET (vector, i, make_number (marker_position (w->pointm))); ++i;
10510 ASET (vector, i, make_number (marker_byte_position (w->pointm))); ++i;
10511 ASET (vector, i, make_number (marker_position (w->old_pointm))); ++i;
10512 ASET (vector, i, make_number (marker_byte_position (w->old_pointm))); ++i;
10513 ASET (vector, i, make_number (marker_position (w->start))); ++i;
10514 ASET (vector, i, make_number (marker_byte_position (w->start))); ++i;
10516 else
10518 int end = i + 8;
10519 for (; i < end; ++i)
10520 ASET (vector, i, Qnil);
10523 eassert (i == ASIZE (vector));
10524 return vector;
10528 /* Restore global state from VECTOR which was created by
10529 with_echo_area_buffer_unwind_data. */
10531 static void
10532 unwind_with_echo_area_buffer (Lisp_Object vector)
10534 set_buffer_internal_1 (XBUFFER (AREF (vector, 0)));
10535 Vdeactivate_mark = AREF (vector, 1);
10536 windows_or_buffers_changed = XFASTINT (AREF (vector, 2));
10538 if (WINDOWP (AREF (vector, 3)))
10540 struct window *w;
10541 Lisp_Object buffer;
10543 w = XWINDOW (AREF (vector, 3));
10544 buffer = AREF (vector, 4);
10546 wset_buffer (w, buffer);
10547 set_marker_both (w->pointm, buffer,
10548 XFASTINT (AREF (vector, 5)),
10549 XFASTINT (AREF (vector, 6)));
10550 set_marker_both (w->old_pointm, buffer,
10551 XFASTINT (AREF (vector, 7)),
10552 XFASTINT (AREF (vector, 8)));
10553 set_marker_both (w->start, buffer,
10554 XFASTINT (AREF (vector, 9)),
10555 XFASTINT (AREF (vector, 10)));
10558 Vwith_echo_area_save_vector = vector;
10562 /* Set up the echo area for use by print functions. MULTIBYTE_P
10563 non-zero means we will print multibyte. */
10565 void
10566 setup_echo_area_for_printing (int multibyte_p)
10568 /* If we can't find an echo area any more, exit. */
10569 if (! FRAME_LIVE_P (XFRAME (selected_frame)))
10570 Fkill_emacs (Qnil);
10572 ensure_echo_area_buffers ();
10574 if (!message_buf_print)
10576 /* A message has been output since the last time we printed.
10577 Choose a fresh echo area buffer. */
10578 if (EQ (echo_area_buffer[1], echo_buffer[0]))
10579 echo_area_buffer[0] = echo_buffer[1];
10580 else
10581 echo_area_buffer[0] = echo_buffer[0];
10583 /* Switch to that buffer and clear it. */
10584 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
10585 bset_truncate_lines (current_buffer, Qnil);
10587 if (Z > BEG)
10589 ptrdiff_t count = SPECPDL_INDEX ();
10590 specbind (Qinhibit_read_only, Qt);
10591 /* Note that undo recording is always disabled. */
10592 del_range (BEG, Z);
10593 unbind_to (count, Qnil);
10595 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
10597 /* Set up the buffer for the multibyteness we need. */
10598 if (multibyte_p
10599 != !NILP (BVAR (current_buffer, enable_multibyte_characters)))
10600 Fset_buffer_multibyte (multibyte_p ? Qt : Qnil);
10602 /* Raise the frame containing the echo area. */
10603 if (minibuffer_auto_raise)
10605 struct frame *sf = SELECTED_FRAME ();
10606 Lisp_Object mini_window;
10607 mini_window = FRAME_MINIBUF_WINDOW (sf);
10608 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
10611 message_log_maybe_newline ();
10612 message_buf_print = 1;
10614 else
10616 if (NILP (echo_area_buffer[0]))
10618 if (EQ (echo_area_buffer[1], echo_buffer[0]))
10619 echo_area_buffer[0] = echo_buffer[1];
10620 else
10621 echo_area_buffer[0] = echo_buffer[0];
10624 if (current_buffer != XBUFFER (echo_area_buffer[0]))
10626 /* Someone switched buffers between print requests. */
10627 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
10628 bset_truncate_lines (current_buffer, Qnil);
10634 /* Display an echo area message in window W. Value is non-zero if W's
10635 height is changed. If display_last_displayed_message_p is
10636 non-zero, display the message that was last displayed, otherwise
10637 display the current message. */
10639 static int
10640 display_echo_area (struct window *w)
10642 int i, no_message_p, window_height_changed_p;
10644 /* Temporarily disable garbage collections while displaying the echo
10645 area. This is done because a GC can print a message itself.
10646 That message would modify the echo area buffer's contents while a
10647 redisplay of the buffer is going on, and seriously confuse
10648 redisplay. */
10649 ptrdiff_t count = inhibit_garbage_collection ();
10651 /* If there is no message, we must call display_echo_area_1
10652 nevertheless because it resizes the window. But we will have to
10653 reset the echo_area_buffer in question to nil at the end because
10654 with_echo_area_buffer will sets it to an empty buffer. */
10655 i = display_last_displayed_message_p ? 1 : 0;
10656 no_message_p = NILP (echo_area_buffer[i]);
10658 window_height_changed_p
10659 = with_echo_area_buffer (w, display_last_displayed_message_p,
10660 display_echo_area_1,
10661 (intptr_t) w, Qnil);
10663 if (no_message_p)
10664 echo_area_buffer[i] = Qnil;
10666 unbind_to (count, Qnil);
10667 return window_height_changed_p;
10671 /* Helper for display_echo_area. Display the current buffer which
10672 contains the current echo area message in window W, a mini-window,
10673 a pointer to which is passed in A1. A2..A4 are currently not used.
10674 Change the height of W so that all of the message is displayed.
10675 Value is non-zero if height of W was changed. */
10677 static int
10678 display_echo_area_1 (ptrdiff_t a1, Lisp_Object a2)
10680 intptr_t i1 = a1;
10681 struct window *w = (struct window *) i1;
10682 Lisp_Object window;
10683 struct text_pos start;
10684 int window_height_changed_p = 0;
10686 /* Do this before displaying, so that we have a large enough glyph
10687 matrix for the display. If we can't get enough space for the
10688 whole text, display the last N lines. That works by setting w->start. */
10689 window_height_changed_p = resize_mini_window (w, 0);
10691 /* Use the starting position chosen by resize_mini_window. */
10692 SET_TEXT_POS_FROM_MARKER (start, w->start);
10694 /* Display. */
10695 clear_glyph_matrix (w->desired_matrix);
10696 XSETWINDOW (window, w);
10697 try_window (window, start, 0);
10699 return window_height_changed_p;
10703 /* Resize the echo area window to exactly the size needed for the
10704 currently displayed message, if there is one. If a mini-buffer
10705 is active, don't shrink it. */
10707 void
10708 resize_echo_area_exactly (void)
10710 if (BUFFERP (echo_area_buffer[0])
10711 && WINDOWP (echo_area_window))
10713 struct window *w = XWINDOW (echo_area_window);
10714 Lisp_Object resize_exactly = (minibuf_level == 0 ? Qt : Qnil);
10715 int resized_p = with_echo_area_buffer (w, 0, resize_mini_window_1,
10716 (intptr_t) w, resize_exactly);
10717 if (resized_p)
10719 windows_or_buffers_changed = 42;
10720 update_mode_lines = 30;
10721 redisplay_internal ();
10727 /* Callback function for with_echo_area_buffer, when used from
10728 resize_echo_area_exactly. A1 contains a pointer to the window to
10729 resize, EXACTLY non-nil means resize the mini-window exactly to the
10730 size of the text displayed. A3 and A4 are not used. Value is what
10731 resize_mini_window returns. */
10733 static int
10734 resize_mini_window_1 (ptrdiff_t a1, Lisp_Object exactly)
10736 intptr_t i1 = a1;
10737 return resize_mini_window ((struct window *) i1, !NILP (exactly));
10741 /* Resize mini-window W to fit the size of its contents. EXACT_P
10742 means size the window exactly to the size needed. Otherwise, it's
10743 only enlarged until W's buffer is empty.
10745 Set W->start to the right place to begin display. If the whole
10746 contents fit, start at the beginning. Otherwise, start so as
10747 to make the end of the contents appear. This is particularly
10748 important for y-or-n-p, but seems desirable generally.
10750 Value is non-zero if the window height has been changed. */
10753 resize_mini_window (struct window *w, int exact_p)
10755 struct frame *f = XFRAME (w->frame);
10756 int window_height_changed_p = 0;
10758 eassert (MINI_WINDOW_P (w));
10760 /* By default, start display at the beginning. */
10761 set_marker_both (w->start, w->contents,
10762 BUF_BEGV (XBUFFER (w->contents)),
10763 BUF_BEGV_BYTE (XBUFFER (w->contents)));
10765 /* Don't resize windows while redisplaying a window; it would
10766 confuse redisplay functions when the size of the window they are
10767 displaying changes from under them. Such a resizing can happen,
10768 for instance, when which-func prints a long message while
10769 we are running fontification-functions. We're running these
10770 functions with safe_call which binds inhibit-redisplay to t. */
10771 if (!NILP (Vinhibit_redisplay))
10772 return 0;
10774 /* Nil means don't try to resize. */
10775 if (NILP (Vresize_mini_windows)
10776 || (FRAME_X_P (f) && FRAME_X_OUTPUT (f) == NULL))
10777 return 0;
10779 if (!FRAME_MINIBUF_ONLY_P (f))
10781 struct it it;
10782 int total_height = (WINDOW_PIXEL_HEIGHT (XWINDOW (FRAME_ROOT_WINDOW (f)))
10783 + WINDOW_PIXEL_HEIGHT (w));
10784 int unit = FRAME_LINE_HEIGHT (f);
10785 int height, max_height;
10786 struct text_pos start;
10787 struct buffer *old_current_buffer = NULL;
10789 if (current_buffer != XBUFFER (w->contents))
10791 old_current_buffer = current_buffer;
10792 set_buffer_internal (XBUFFER (w->contents));
10795 init_iterator (&it, w, BEGV, BEGV_BYTE, NULL, DEFAULT_FACE_ID);
10797 /* Compute the max. number of lines specified by the user. */
10798 if (FLOATP (Vmax_mini_window_height))
10799 max_height = XFLOATINT (Vmax_mini_window_height) * total_height;
10800 else if (INTEGERP (Vmax_mini_window_height))
10801 max_height = XINT (Vmax_mini_window_height) * unit;
10802 else
10803 max_height = total_height / 4;
10805 /* Correct that max. height if it's bogus. */
10806 max_height = clip_to_bounds (unit, max_height, total_height);
10808 /* Find out the height of the text in the window. */
10809 if (it.line_wrap == TRUNCATE)
10810 height = unit;
10811 else
10813 last_height = 0;
10814 move_it_to (&it, ZV, -1, -1, -1, MOVE_TO_POS);
10815 if (it.max_ascent == 0 && it.max_descent == 0)
10816 height = it.current_y + last_height;
10817 else
10818 height = it.current_y + it.max_ascent + it.max_descent;
10819 height -= min (it.extra_line_spacing, it.max_extra_line_spacing);
10822 /* Compute a suitable window start. */
10823 if (height > max_height)
10825 height = (max_height / unit) * unit;
10826 init_iterator (&it, w, ZV, ZV_BYTE, NULL, DEFAULT_FACE_ID);
10827 move_it_vertically_backward (&it, height - unit);
10828 start = it.current.pos;
10830 else
10831 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
10832 SET_MARKER_FROM_TEXT_POS (w->start, start);
10834 if (EQ (Vresize_mini_windows, Qgrow_only))
10836 /* Let it grow only, until we display an empty message, in which
10837 case the window shrinks again. */
10838 if (height > WINDOW_PIXEL_HEIGHT (w))
10840 int old_height = WINDOW_PIXEL_HEIGHT (w);
10842 FRAME_WINDOWS_FROZEN (f) = 1;
10843 grow_mini_window (w, height - WINDOW_PIXEL_HEIGHT (w), 1);
10844 window_height_changed_p = WINDOW_PIXEL_HEIGHT (w) != old_height;
10846 else if (height < WINDOW_PIXEL_HEIGHT (w)
10847 && (exact_p || BEGV == ZV))
10849 int old_height = WINDOW_PIXEL_HEIGHT (w);
10851 FRAME_WINDOWS_FROZEN (f) = 0;
10852 shrink_mini_window (w, 1);
10853 window_height_changed_p = WINDOW_PIXEL_HEIGHT (w) != old_height;
10856 else
10858 /* Always resize to exact size needed. */
10859 if (height > WINDOW_PIXEL_HEIGHT (w))
10861 int old_height = WINDOW_PIXEL_HEIGHT (w);
10863 FRAME_WINDOWS_FROZEN (f) = 1;
10864 grow_mini_window (w, height - WINDOW_PIXEL_HEIGHT (w), 1);
10865 window_height_changed_p = WINDOW_PIXEL_HEIGHT (w) != old_height;
10867 else if (height < WINDOW_PIXEL_HEIGHT (w))
10869 int old_height = WINDOW_PIXEL_HEIGHT (w);
10871 FRAME_WINDOWS_FROZEN (f) = 0;
10872 shrink_mini_window (w, 1);
10874 if (height)
10876 FRAME_WINDOWS_FROZEN (f) = 1;
10877 grow_mini_window (w, height - WINDOW_PIXEL_HEIGHT (w), 1);
10880 window_height_changed_p = WINDOW_PIXEL_HEIGHT (w) != old_height;
10884 if (old_current_buffer)
10885 set_buffer_internal (old_current_buffer);
10888 return window_height_changed_p;
10892 /* Value is the current message, a string, or nil if there is no
10893 current message. */
10895 Lisp_Object
10896 current_message (void)
10898 Lisp_Object msg;
10900 if (!BUFFERP (echo_area_buffer[0]))
10901 msg = Qnil;
10902 else
10904 with_echo_area_buffer (0, 0, current_message_1,
10905 (intptr_t) &msg, Qnil);
10906 if (NILP (msg))
10907 echo_area_buffer[0] = Qnil;
10910 return msg;
10914 static int
10915 current_message_1 (ptrdiff_t a1, Lisp_Object a2)
10917 intptr_t i1 = a1;
10918 Lisp_Object *msg = (Lisp_Object *) i1;
10920 if (Z > BEG)
10921 *msg = make_buffer_string (BEG, Z, 1);
10922 else
10923 *msg = Qnil;
10924 return 0;
10928 /* Push the current message on Vmessage_stack for later restoration
10929 by restore_message. Value is non-zero if the current message isn't
10930 empty. This is a relatively infrequent operation, so it's not
10931 worth optimizing. */
10933 bool
10934 push_message (void)
10936 Lisp_Object msg = current_message ();
10937 Vmessage_stack = Fcons (msg, Vmessage_stack);
10938 return STRINGP (msg);
10942 /* Restore message display from the top of Vmessage_stack. */
10944 void
10945 restore_message (void)
10947 eassert (CONSP (Vmessage_stack));
10948 message3_nolog (XCAR (Vmessage_stack));
10952 /* Handler for unwind-protect calling pop_message. */
10954 void
10955 pop_message_unwind (void)
10957 /* Pop the top-most entry off Vmessage_stack. */
10958 eassert (CONSP (Vmessage_stack));
10959 Vmessage_stack = XCDR (Vmessage_stack);
10963 /* Check that Vmessage_stack is nil. Called from emacs.c when Emacs
10964 exits. If the stack is not empty, we have a missing pop_message
10965 somewhere. */
10967 void
10968 check_message_stack (void)
10970 if (!NILP (Vmessage_stack))
10971 emacs_abort ();
10975 /* Truncate to NCHARS what will be displayed in the echo area the next
10976 time we display it---but don't redisplay it now. */
10978 void
10979 truncate_echo_area (ptrdiff_t nchars)
10981 if (nchars == 0)
10982 echo_area_buffer[0] = Qnil;
10983 else if (!noninteractive
10984 && INTERACTIVE
10985 && !NILP (echo_area_buffer[0]))
10987 struct frame *sf = SELECTED_FRAME ();
10988 /* Error messages get reported properly by cmd_error, so this must be
10989 just an informative message; if the frame hasn't really been
10990 initialized yet, just toss it. */
10991 if (sf->glyphs_initialized_p)
10992 with_echo_area_buffer (0, 0, truncate_message_1, nchars, Qnil);
10997 /* Helper function for truncate_echo_area. Truncate the current
10998 message to at most NCHARS characters. */
11000 static int
11001 truncate_message_1 (ptrdiff_t nchars, Lisp_Object a2)
11003 if (BEG + nchars < Z)
11004 del_range (BEG + nchars, Z);
11005 if (Z == BEG)
11006 echo_area_buffer[0] = Qnil;
11007 return 0;
11010 /* Set the current message to STRING. */
11012 static void
11013 set_message (Lisp_Object string)
11015 eassert (STRINGP (string));
11017 message_enable_multibyte = STRING_MULTIBYTE (string);
11019 with_echo_area_buffer (0, -1, set_message_1, 0, string);
11020 message_buf_print = 0;
11021 help_echo_showing_p = 0;
11023 if (STRINGP (Vdebug_on_message)
11024 && STRINGP (string)
11025 && fast_string_match (Vdebug_on_message, string) >= 0)
11026 call_debugger (list2 (Qerror, string));
11030 /* Helper function for set_message. First argument is ignored and second
11031 argument has the same meaning as for set_message.
11032 This function is called with the echo area buffer being current. */
11034 static int
11035 set_message_1 (ptrdiff_t a1, Lisp_Object string)
11037 eassert (STRINGP (string));
11039 /* Change multibyteness of the echo buffer appropriately. */
11040 if (message_enable_multibyte
11041 != !NILP (BVAR (current_buffer, enable_multibyte_characters)))
11042 Fset_buffer_multibyte (message_enable_multibyte ? Qt : Qnil);
11044 bset_truncate_lines (current_buffer, message_truncate_lines ? Qt : Qnil);
11045 if (!NILP (BVAR (current_buffer, bidi_display_reordering)))
11046 bset_bidi_paragraph_direction (current_buffer, Qleft_to_right);
11048 /* Insert new message at BEG. */
11049 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
11051 /* This function takes care of single/multibyte conversion.
11052 We just have to ensure that the echo area buffer has the right
11053 setting of enable_multibyte_characters. */
11054 insert_from_string (string, 0, 0, SCHARS (string), SBYTES (string), 1);
11056 return 0;
11060 /* Clear messages. CURRENT_P non-zero means clear the current
11061 message. LAST_DISPLAYED_P non-zero means clear the message
11062 last displayed. */
11064 void
11065 clear_message (bool current_p, bool last_displayed_p)
11067 if (current_p)
11069 echo_area_buffer[0] = Qnil;
11070 message_cleared_p = true;
11073 if (last_displayed_p)
11074 echo_area_buffer[1] = Qnil;
11076 message_buf_print = 0;
11079 /* Clear garbaged frames.
11081 This function is used where the old redisplay called
11082 redraw_garbaged_frames which in turn called redraw_frame which in
11083 turn called clear_frame. The call to clear_frame was a source of
11084 flickering. I believe a clear_frame is not necessary. It should
11085 suffice in the new redisplay to invalidate all current matrices,
11086 and ensure a complete redisplay of all windows. */
11088 static void
11089 clear_garbaged_frames (void)
11091 if (frame_garbaged)
11093 Lisp_Object tail, frame;
11095 FOR_EACH_FRAME (tail, frame)
11097 struct frame *f = XFRAME (frame);
11099 if (FRAME_VISIBLE_P (f) && FRAME_GARBAGED_P (f))
11101 if (f->resized_p)
11102 redraw_frame (f);
11103 else
11104 clear_current_matrices (f);
11105 fset_redisplay (f);
11106 f->garbaged = false;
11107 f->resized_p = false;
11111 frame_garbaged = false;
11116 /* Redisplay the echo area of the selected frame. If UPDATE_FRAME_P
11117 is non-zero update selected_frame. Value is non-zero if the
11118 mini-windows height has been changed. */
11120 static bool
11121 echo_area_display (bool update_frame_p)
11123 Lisp_Object mini_window;
11124 struct window *w;
11125 struct frame *f;
11126 bool window_height_changed_p = false;
11127 struct frame *sf = SELECTED_FRAME ();
11129 mini_window = FRAME_MINIBUF_WINDOW (sf);
11130 w = XWINDOW (mini_window);
11131 f = XFRAME (WINDOW_FRAME (w));
11133 /* Don't display if frame is invisible or not yet initialized. */
11134 if (!FRAME_VISIBLE_P (f) || !f->glyphs_initialized_p)
11135 return 0;
11137 #ifdef HAVE_WINDOW_SYSTEM
11138 /* When Emacs starts, selected_frame may be the initial terminal
11139 frame. If we let this through, a message would be displayed on
11140 the terminal. */
11141 if (FRAME_INITIAL_P (XFRAME (selected_frame)))
11142 return 0;
11143 #endif /* HAVE_WINDOW_SYSTEM */
11145 /* Redraw garbaged frames. */
11146 clear_garbaged_frames ();
11148 if (!NILP (echo_area_buffer[0]) || minibuf_level == 0)
11150 echo_area_window = mini_window;
11151 window_height_changed_p = display_echo_area (w);
11152 w->must_be_updated_p = true;
11154 /* Update the display, unless called from redisplay_internal.
11155 Also don't update the screen during redisplay itself. The
11156 update will happen at the end of redisplay, and an update
11157 here could cause confusion. */
11158 if (update_frame_p && !redisplaying_p)
11160 int n = 0;
11162 /* If the display update has been interrupted by pending
11163 input, update mode lines in the frame. Due to the
11164 pending input, it might have been that redisplay hasn't
11165 been called, so that mode lines above the echo area are
11166 garbaged. This looks odd, so we prevent it here. */
11167 if (!display_completed)
11168 n = redisplay_mode_lines (FRAME_ROOT_WINDOW (f), false);
11170 if (window_height_changed_p
11171 /* Don't do this if Emacs is shutting down. Redisplay
11172 needs to run hooks. */
11173 && !NILP (Vrun_hooks))
11175 /* Must update other windows. Likewise as in other
11176 cases, don't let this update be interrupted by
11177 pending input. */
11178 ptrdiff_t count = SPECPDL_INDEX ();
11179 specbind (Qredisplay_dont_pause, Qt);
11180 windows_or_buffers_changed = 44;
11181 redisplay_internal ();
11182 unbind_to (count, Qnil);
11184 else if (FRAME_WINDOW_P (f) && n == 0)
11186 /* Window configuration is the same as before.
11187 Can do with a display update of the echo area,
11188 unless we displayed some mode lines. */
11189 update_single_window (w);
11190 flush_frame (f);
11192 else
11193 update_frame (f, true, true);
11195 /* If cursor is in the echo area, make sure that the next
11196 redisplay displays the minibuffer, so that the cursor will
11197 be replaced with what the minibuffer wants. */
11198 if (cursor_in_echo_area)
11199 wset_redisplay (XWINDOW (mini_window));
11202 else if (!EQ (mini_window, selected_window))
11203 wset_redisplay (XWINDOW (mini_window));
11205 /* Last displayed message is now the current message. */
11206 echo_area_buffer[1] = echo_area_buffer[0];
11207 /* Inform read_char that we're not echoing. */
11208 echo_message_buffer = Qnil;
11210 /* Prevent redisplay optimization in redisplay_internal by resetting
11211 this_line_start_pos. This is done because the mini-buffer now
11212 displays the message instead of its buffer text. */
11213 if (EQ (mini_window, selected_window))
11214 CHARPOS (this_line_start_pos) = 0;
11216 return window_height_changed_p;
11219 /* Nonzero if W's buffer was changed but not saved. */
11221 static int
11222 window_buffer_changed (struct window *w)
11224 struct buffer *b = XBUFFER (w->contents);
11226 eassert (BUFFER_LIVE_P (b));
11228 return (((BUF_SAVE_MODIFF (b) < BUF_MODIFF (b)) != w->last_had_star));
11231 /* Nonzero if W has %c in its mode line and mode line should be updated. */
11233 static int
11234 mode_line_update_needed (struct window *w)
11236 return (w->column_number_displayed != -1
11237 && !(PT == w->last_point && !window_outdated (w))
11238 && (w->column_number_displayed != current_column ()));
11241 /* Nonzero if window start of W is frozen and may not be changed during
11242 redisplay. */
11244 static bool
11245 window_frozen_p (struct window *w)
11247 if (FRAME_WINDOWS_FROZEN (XFRAME (WINDOW_FRAME (w))))
11249 Lisp_Object window;
11251 XSETWINDOW (window, w);
11252 if (MINI_WINDOW_P (w))
11253 return 0;
11254 else if (EQ (window, selected_window))
11255 return 0;
11256 else if (MINI_WINDOW_P (XWINDOW (selected_window))
11257 && EQ (window, Vminibuf_scroll_window))
11258 /* This special window can't be frozen too. */
11259 return 0;
11260 else
11261 return 1;
11263 return 0;
11266 /***********************************************************************
11267 Mode Lines and Frame Titles
11268 ***********************************************************************/
11270 /* A buffer for constructing non-propertized mode-line strings and
11271 frame titles in it; allocated from the heap in init_xdisp and
11272 resized as needed in store_mode_line_noprop_char. */
11274 static char *mode_line_noprop_buf;
11276 /* The buffer's end, and a current output position in it. */
11278 static char *mode_line_noprop_buf_end;
11279 static char *mode_line_noprop_ptr;
11281 #define MODE_LINE_NOPROP_LEN(start) \
11282 ((mode_line_noprop_ptr - mode_line_noprop_buf) - start)
11284 static enum {
11285 MODE_LINE_DISPLAY = 0,
11286 MODE_LINE_TITLE,
11287 MODE_LINE_NOPROP,
11288 MODE_LINE_STRING
11289 } mode_line_target;
11291 /* Alist that caches the results of :propertize.
11292 Each element is (PROPERTIZED-STRING . PROPERTY-LIST). */
11293 static Lisp_Object mode_line_proptrans_alist;
11295 /* List of strings making up the mode-line. */
11296 static Lisp_Object mode_line_string_list;
11298 /* Base face property when building propertized mode line string. */
11299 static Lisp_Object mode_line_string_face;
11300 static Lisp_Object mode_line_string_face_prop;
11303 /* Unwind data for mode line strings */
11305 static Lisp_Object Vmode_line_unwind_vector;
11307 static Lisp_Object
11308 format_mode_line_unwind_data (struct frame *target_frame,
11309 struct buffer *obuf,
11310 Lisp_Object owin,
11311 int save_proptrans)
11313 Lisp_Object vector, tmp;
11315 /* Reduce consing by keeping one vector in
11316 Vwith_echo_area_save_vector. */
11317 vector = Vmode_line_unwind_vector;
11318 Vmode_line_unwind_vector = Qnil;
11320 if (NILP (vector))
11321 vector = Fmake_vector (make_number (10), Qnil);
11323 ASET (vector, 0, make_number (mode_line_target));
11324 ASET (vector, 1, make_number (MODE_LINE_NOPROP_LEN (0)));
11325 ASET (vector, 2, mode_line_string_list);
11326 ASET (vector, 3, save_proptrans ? mode_line_proptrans_alist : Qt);
11327 ASET (vector, 4, mode_line_string_face);
11328 ASET (vector, 5, mode_line_string_face_prop);
11330 if (obuf)
11331 XSETBUFFER (tmp, obuf);
11332 else
11333 tmp = Qnil;
11334 ASET (vector, 6, tmp);
11335 ASET (vector, 7, owin);
11336 if (target_frame)
11338 /* Similarly to `with-selected-window', if the operation selects
11339 a window on another frame, we must restore that frame's
11340 selected window, and (for a tty) the top-frame. */
11341 ASET (vector, 8, target_frame->selected_window);
11342 if (FRAME_TERMCAP_P (target_frame))
11343 ASET (vector, 9, FRAME_TTY (target_frame)->top_frame);
11346 return vector;
11349 static void
11350 unwind_format_mode_line (Lisp_Object vector)
11352 Lisp_Object old_window = AREF (vector, 7);
11353 Lisp_Object target_frame_window = AREF (vector, 8);
11354 Lisp_Object old_top_frame = AREF (vector, 9);
11356 mode_line_target = XINT (AREF (vector, 0));
11357 mode_line_noprop_ptr = mode_line_noprop_buf + XINT (AREF (vector, 1));
11358 mode_line_string_list = AREF (vector, 2);
11359 if (! EQ (AREF (vector, 3), Qt))
11360 mode_line_proptrans_alist = AREF (vector, 3);
11361 mode_line_string_face = AREF (vector, 4);
11362 mode_line_string_face_prop = AREF (vector, 5);
11364 /* Select window before buffer, since it may change the buffer. */
11365 if (!NILP (old_window))
11367 /* If the operation that we are unwinding had selected a window
11368 on a different frame, reset its frame-selected-window. For a
11369 text terminal, reset its top-frame if necessary. */
11370 if (!NILP (target_frame_window))
11372 Lisp_Object frame
11373 = WINDOW_FRAME (XWINDOW (target_frame_window));
11375 if (!EQ (frame, WINDOW_FRAME (XWINDOW (old_window))))
11376 Fselect_window (target_frame_window, Qt);
11378 if (!NILP (old_top_frame) && !EQ (old_top_frame, frame))
11379 Fselect_frame (old_top_frame, Qt);
11382 Fselect_window (old_window, Qt);
11385 if (!NILP (AREF (vector, 6)))
11387 set_buffer_internal_1 (XBUFFER (AREF (vector, 6)));
11388 ASET (vector, 6, Qnil);
11391 Vmode_line_unwind_vector = vector;
11395 /* Store a single character C for the frame title in mode_line_noprop_buf.
11396 Re-allocate mode_line_noprop_buf if necessary. */
11398 static void
11399 store_mode_line_noprop_char (char c)
11401 /* If output position has reached the end of the allocated buffer,
11402 increase the buffer's size. */
11403 if (mode_line_noprop_ptr == mode_line_noprop_buf_end)
11405 ptrdiff_t len = MODE_LINE_NOPROP_LEN (0);
11406 ptrdiff_t size = len;
11407 mode_line_noprop_buf =
11408 xpalloc (mode_line_noprop_buf, &size, 1, STRING_BYTES_BOUND, 1);
11409 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
11410 mode_line_noprop_ptr = mode_line_noprop_buf + len;
11413 *mode_line_noprop_ptr++ = c;
11417 /* Store part of a frame title in mode_line_noprop_buf, beginning at
11418 mode_line_noprop_ptr. STRING is the string to store. Do not copy
11419 characters that yield more columns than PRECISION; PRECISION <= 0
11420 means copy the whole string. Pad with spaces until FIELD_WIDTH
11421 number of characters have been copied; FIELD_WIDTH <= 0 means don't
11422 pad. Called from display_mode_element when it is used to build a
11423 frame title. */
11425 static int
11426 store_mode_line_noprop (const char *string, int field_width, int precision)
11428 const unsigned char *str = (const unsigned char *) string;
11429 int n = 0;
11430 ptrdiff_t dummy, nbytes;
11432 /* Copy at most PRECISION chars from STR. */
11433 nbytes = strlen (string);
11434 n += c_string_width (str, nbytes, precision, &dummy, &nbytes);
11435 while (nbytes--)
11436 store_mode_line_noprop_char (*str++);
11438 /* Fill up with spaces until FIELD_WIDTH reached. */
11439 while (field_width > 0
11440 && n < field_width)
11442 store_mode_line_noprop_char (' ');
11443 ++n;
11446 return n;
11449 /***********************************************************************
11450 Frame Titles
11451 ***********************************************************************/
11453 #ifdef HAVE_WINDOW_SYSTEM
11455 /* Set the title of FRAME, if it has changed. The title format is
11456 Vicon_title_format if FRAME is iconified, otherwise it is
11457 frame_title_format. */
11459 static void
11460 x_consider_frame_title (Lisp_Object frame)
11462 struct frame *f = XFRAME (frame);
11464 if (FRAME_WINDOW_P (f)
11465 || FRAME_MINIBUF_ONLY_P (f)
11466 || f->explicit_name)
11468 /* Do we have more than one visible frame on this X display? */
11469 Lisp_Object tail, other_frame, fmt;
11470 ptrdiff_t title_start;
11471 char *title;
11472 ptrdiff_t len;
11473 struct it it;
11474 ptrdiff_t count = SPECPDL_INDEX ();
11476 FOR_EACH_FRAME (tail, other_frame)
11478 struct frame *tf = XFRAME (other_frame);
11480 if (tf != f
11481 && FRAME_KBOARD (tf) == FRAME_KBOARD (f)
11482 && !FRAME_MINIBUF_ONLY_P (tf)
11483 && !EQ (other_frame, tip_frame)
11484 && (FRAME_VISIBLE_P (tf) || FRAME_ICONIFIED_P (tf)))
11485 break;
11488 /* Set global variable indicating that multiple frames exist. */
11489 multiple_frames = CONSP (tail);
11491 /* Switch to the buffer of selected window of the frame. Set up
11492 mode_line_target so that display_mode_element will output into
11493 mode_line_noprop_buf; then display the title. */
11494 record_unwind_protect (unwind_format_mode_line,
11495 format_mode_line_unwind_data
11496 (f, current_buffer, selected_window, 0));
11498 Fselect_window (f->selected_window, Qt);
11499 set_buffer_internal_1
11500 (XBUFFER (XWINDOW (f->selected_window)->contents));
11501 fmt = FRAME_ICONIFIED_P (f) ? Vicon_title_format : Vframe_title_format;
11503 mode_line_target = MODE_LINE_TITLE;
11504 title_start = MODE_LINE_NOPROP_LEN (0);
11505 init_iterator (&it, XWINDOW (f->selected_window), -1, -1,
11506 NULL, DEFAULT_FACE_ID);
11507 display_mode_element (&it, 0, -1, -1, fmt, Qnil, 0);
11508 len = MODE_LINE_NOPROP_LEN (title_start);
11509 title = mode_line_noprop_buf + title_start;
11510 unbind_to (count, Qnil);
11512 /* Set the title only if it's changed. This avoids consing in
11513 the common case where it hasn't. (If it turns out that we've
11514 already wasted too much time by walking through the list with
11515 display_mode_element, then we might need to optimize at a
11516 higher level than this.) */
11517 if (! STRINGP (f->name)
11518 || SBYTES (f->name) != len
11519 || memcmp (title, SDATA (f->name), len) != 0)
11520 x_implicitly_set_name (f, make_string (title, len), Qnil);
11524 #endif /* not HAVE_WINDOW_SYSTEM */
11527 /***********************************************************************
11528 Menu Bars
11529 ***********************************************************************/
11531 /* Non-zero if we will not redisplay all visible windows. */
11532 #define REDISPLAY_SOME_P() \
11533 ((windows_or_buffers_changed == 0 \
11534 || windows_or_buffers_changed == REDISPLAY_SOME) \
11535 && (update_mode_lines == 0 \
11536 || update_mode_lines == REDISPLAY_SOME))
11538 /* Prepare for redisplay by updating menu-bar item lists when
11539 appropriate. This can call eval. */
11541 static void
11542 prepare_menu_bars (void)
11544 bool all_windows = windows_or_buffers_changed || update_mode_lines;
11545 bool some_windows = REDISPLAY_SOME_P ();
11546 struct gcpro gcpro1, gcpro2;
11547 Lisp_Object tooltip_frame;
11549 #ifdef HAVE_WINDOW_SYSTEM
11550 tooltip_frame = tip_frame;
11551 #else
11552 tooltip_frame = Qnil;
11553 #endif
11555 if (FUNCTIONP (Vpre_redisplay_function))
11557 Lisp_Object windows = all_windows ? Qt : Qnil;
11558 if (all_windows && some_windows)
11560 Lisp_Object ws = window_list ();
11561 for (windows = Qnil; CONSP (ws); ws = XCDR (ws))
11563 Lisp_Object this = XCAR (ws);
11564 struct window *w = XWINDOW (this);
11565 if (w->redisplay
11566 || XFRAME (w->frame)->redisplay
11567 || XBUFFER (w->contents)->text->redisplay)
11569 windows = Fcons (this, windows);
11573 safe__call1 (true, Vpre_redisplay_function, windows);
11576 /* Update all frame titles based on their buffer names, etc. We do
11577 this before the menu bars so that the buffer-menu will show the
11578 up-to-date frame titles. */
11579 #ifdef HAVE_WINDOW_SYSTEM
11580 if (all_windows)
11582 Lisp_Object tail, frame;
11584 FOR_EACH_FRAME (tail, frame)
11586 struct frame *f = XFRAME (frame);
11587 struct window *w = XWINDOW (FRAME_SELECTED_WINDOW (f));
11588 if (some_windows
11589 && !f->redisplay
11590 && !w->redisplay
11591 && !XBUFFER (w->contents)->text->redisplay)
11592 continue;
11594 if (!EQ (frame, tooltip_frame)
11595 && (FRAME_ICONIFIED_P (f)
11596 || FRAME_VISIBLE_P (f) == 1
11597 /* Exclude TTY frames that are obscured because they
11598 are not the top frame on their console. This is
11599 because x_consider_frame_title actually switches
11600 to the frame, which for TTY frames means it is
11601 marked as garbaged, and will be completely
11602 redrawn on the next redisplay cycle. This causes
11603 TTY frames to be completely redrawn, when there
11604 are more than one of them, even though nothing
11605 should be changed on display. */
11606 || (FRAME_VISIBLE_P (f) == 2 && FRAME_WINDOW_P (f))))
11607 x_consider_frame_title (frame);
11610 #endif /* HAVE_WINDOW_SYSTEM */
11612 /* Update the menu bar item lists, if appropriate. This has to be
11613 done before any actual redisplay or generation of display lines. */
11615 if (all_windows)
11617 Lisp_Object tail, frame;
11618 ptrdiff_t count = SPECPDL_INDEX ();
11619 /* 1 means that update_menu_bar has run its hooks
11620 so any further calls to update_menu_bar shouldn't do so again. */
11621 int menu_bar_hooks_run = 0;
11623 record_unwind_save_match_data ();
11625 FOR_EACH_FRAME (tail, frame)
11627 struct frame *f = XFRAME (frame);
11628 struct window *w = XWINDOW (FRAME_SELECTED_WINDOW (f));
11630 /* Ignore tooltip frame. */
11631 if (EQ (frame, tooltip_frame))
11632 continue;
11634 if (some_windows
11635 && !f->redisplay
11636 && !w->redisplay
11637 && !XBUFFER (w->contents)->text->redisplay)
11638 continue;
11640 /* If a window on this frame changed size, report that to
11641 the user and clear the size-change flag. */
11642 if (FRAME_WINDOW_SIZES_CHANGED (f))
11644 Lisp_Object functions;
11646 /* Clear flag first in case we get an error below. */
11647 FRAME_WINDOW_SIZES_CHANGED (f) = 0;
11648 functions = Vwindow_size_change_functions;
11649 GCPRO2 (tail, functions);
11651 while (CONSP (functions))
11653 if (!EQ (XCAR (functions), Qt))
11654 call1 (XCAR (functions), frame);
11655 functions = XCDR (functions);
11657 UNGCPRO;
11660 GCPRO1 (tail);
11661 menu_bar_hooks_run = update_menu_bar (f, 0, menu_bar_hooks_run);
11662 #ifdef HAVE_WINDOW_SYSTEM
11663 update_tool_bar (f, 0);
11664 #endif
11665 UNGCPRO;
11668 unbind_to (count, Qnil);
11670 else
11672 struct frame *sf = SELECTED_FRAME ();
11673 update_menu_bar (sf, 1, 0);
11674 #ifdef HAVE_WINDOW_SYSTEM
11675 update_tool_bar (sf, 1);
11676 #endif
11681 /* Update the menu bar item list for frame F. This has to be done
11682 before we start to fill in any display lines, because it can call
11683 eval.
11685 If SAVE_MATCH_DATA is non-zero, we must save and restore it here.
11687 If HOOKS_RUN is 1, that means a previous call to update_menu_bar
11688 already ran the menu bar hooks for this redisplay, so there
11689 is no need to run them again. The return value is the
11690 updated value of this flag, to pass to the next call. */
11692 static int
11693 update_menu_bar (struct frame *f, int save_match_data, int hooks_run)
11695 Lisp_Object window;
11696 register struct window *w;
11698 /* If called recursively during a menu update, do nothing. This can
11699 happen when, for instance, an activate-menubar-hook causes a
11700 redisplay. */
11701 if (inhibit_menubar_update)
11702 return hooks_run;
11704 window = FRAME_SELECTED_WINDOW (f);
11705 w = XWINDOW (window);
11707 if (FRAME_WINDOW_P (f)
11709 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
11710 || defined (HAVE_NS) || defined (USE_GTK)
11711 FRAME_EXTERNAL_MENU_BAR (f)
11712 #else
11713 FRAME_MENU_BAR_LINES (f) > 0
11714 #endif
11715 : FRAME_MENU_BAR_LINES (f) > 0)
11717 /* If the user has switched buffers or windows, we need to
11718 recompute to reflect the new bindings. But we'll
11719 recompute when update_mode_lines is set too; that means
11720 that people can use force-mode-line-update to request
11721 that the menu bar be recomputed. The adverse effect on
11722 the rest of the redisplay algorithm is about the same as
11723 windows_or_buffers_changed anyway. */
11724 if (windows_or_buffers_changed
11725 /* This used to test w->update_mode_line, but we believe
11726 there is no need to recompute the menu in that case. */
11727 || update_mode_lines
11728 || window_buffer_changed (w))
11730 struct buffer *prev = current_buffer;
11731 ptrdiff_t count = SPECPDL_INDEX ();
11733 specbind (Qinhibit_menubar_update, Qt);
11735 set_buffer_internal_1 (XBUFFER (w->contents));
11736 if (save_match_data)
11737 record_unwind_save_match_data ();
11738 if (NILP (Voverriding_local_map_menu_flag))
11740 specbind (Qoverriding_terminal_local_map, Qnil);
11741 specbind (Qoverriding_local_map, Qnil);
11744 if (!hooks_run)
11746 /* Run the Lucid hook. */
11747 safe_run_hooks (Qactivate_menubar_hook);
11749 /* If it has changed current-menubar from previous value,
11750 really recompute the menu-bar from the value. */
11751 if (! NILP (Vlucid_menu_bar_dirty_flag))
11752 call0 (Qrecompute_lucid_menubar);
11754 safe_run_hooks (Qmenu_bar_update_hook);
11756 hooks_run = 1;
11759 XSETFRAME (Vmenu_updating_frame, f);
11760 fset_menu_bar_items (f, menu_bar_items (FRAME_MENU_BAR_ITEMS (f)));
11762 /* Redisplay the menu bar in case we changed it. */
11763 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
11764 || defined (HAVE_NS) || defined (USE_GTK)
11765 if (FRAME_WINDOW_P (f))
11767 #if defined (HAVE_NS)
11768 /* All frames on Mac OS share the same menubar. So only
11769 the selected frame should be allowed to set it. */
11770 if (f == SELECTED_FRAME ())
11771 #endif
11772 set_frame_menubar (f, 0, 0);
11774 else
11775 /* On a terminal screen, the menu bar is an ordinary screen
11776 line, and this makes it get updated. */
11777 w->update_mode_line = 1;
11778 #else /* ! (USE_X_TOOLKIT || HAVE_NTGUI || HAVE_NS || USE_GTK) */
11779 /* In the non-toolkit version, the menu bar is an ordinary screen
11780 line, and this makes it get updated. */
11781 w->update_mode_line = 1;
11782 #endif /* ! (USE_X_TOOLKIT || HAVE_NTGUI || HAVE_NS || USE_GTK) */
11784 unbind_to (count, Qnil);
11785 set_buffer_internal_1 (prev);
11789 return hooks_run;
11792 /***********************************************************************
11793 Tool-bars
11794 ***********************************************************************/
11796 #ifdef HAVE_WINDOW_SYSTEM
11798 /* Select `frame' temporarily without running all the code in
11799 do_switch_frame.
11800 FIXME: Maybe do_switch_frame should be trimmed down similarly
11801 when `norecord' is set. */
11802 static void
11803 fast_set_selected_frame (Lisp_Object frame)
11805 if (!EQ (selected_frame, frame))
11807 selected_frame = frame;
11808 selected_window = XFRAME (frame)->selected_window;
11812 /* Update the tool-bar item list for frame F. This has to be done
11813 before we start to fill in any display lines. Called from
11814 prepare_menu_bars. If SAVE_MATCH_DATA is non-zero, we must save
11815 and restore it here. */
11817 static void
11818 update_tool_bar (struct frame *f, int save_match_data)
11820 #if defined (USE_GTK) || defined (HAVE_NS)
11821 int do_update = FRAME_EXTERNAL_TOOL_BAR (f);
11822 #else
11823 int do_update = (WINDOWP (f->tool_bar_window)
11824 && WINDOW_TOTAL_LINES (XWINDOW (f->tool_bar_window)) > 0);
11825 #endif
11827 if (do_update)
11829 Lisp_Object window;
11830 struct window *w;
11832 window = FRAME_SELECTED_WINDOW (f);
11833 w = XWINDOW (window);
11835 /* If the user has switched buffers or windows, we need to
11836 recompute to reflect the new bindings. But we'll
11837 recompute when update_mode_lines is set too; that means
11838 that people can use force-mode-line-update to request
11839 that the menu bar be recomputed. The adverse effect on
11840 the rest of the redisplay algorithm is about the same as
11841 windows_or_buffers_changed anyway. */
11842 if (windows_or_buffers_changed
11843 || w->update_mode_line
11844 || update_mode_lines
11845 || window_buffer_changed (w))
11847 struct buffer *prev = current_buffer;
11848 ptrdiff_t count = SPECPDL_INDEX ();
11849 Lisp_Object frame, new_tool_bar;
11850 int new_n_tool_bar;
11851 struct gcpro gcpro1;
11853 /* Set current_buffer to the buffer of the selected
11854 window of the frame, so that we get the right local
11855 keymaps. */
11856 set_buffer_internal_1 (XBUFFER (w->contents));
11858 /* Save match data, if we must. */
11859 if (save_match_data)
11860 record_unwind_save_match_data ();
11862 /* Make sure that we don't accidentally use bogus keymaps. */
11863 if (NILP (Voverriding_local_map_menu_flag))
11865 specbind (Qoverriding_terminal_local_map, Qnil);
11866 specbind (Qoverriding_local_map, Qnil);
11869 GCPRO1 (new_tool_bar);
11871 /* We must temporarily set the selected frame to this frame
11872 before calling tool_bar_items, because the calculation of
11873 the tool-bar keymap uses the selected frame (see
11874 `tool-bar-make-keymap' in tool-bar.el). */
11875 eassert (EQ (selected_window,
11876 /* Since we only explicitly preserve selected_frame,
11877 check that selected_window would be redundant. */
11878 XFRAME (selected_frame)->selected_window));
11879 record_unwind_protect (fast_set_selected_frame, selected_frame);
11880 XSETFRAME (frame, f);
11881 fast_set_selected_frame (frame);
11883 /* Build desired tool-bar items from keymaps. */
11884 new_tool_bar
11885 = tool_bar_items (Fcopy_sequence (f->tool_bar_items),
11886 &new_n_tool_bar);
11888 /* Redisplay the tool-bar if we changed it. */
11889 if (new_n_tool_bar != f->n_tool_bar_items
11890 || NILP (Fequal (new_tool_bar, f->tool_bar_items)))
11892 /* Redisplay that happens asynchronously due to an expose event
11893 may access f->tool_bar_items. Make sure we update both
11894 variables within BLOCK_INPUT so no such event interrupts. */
11895 block_input ();
11896 fset_tool_bar_items (f, new_tool_bar);
11897 f->n_tool_bar_items = new_n_tool_bar;
11898 w->update_mode_line = 1;
11899 unblock_input ();
11902 UNGCPRO;
11904 unbind_to (count, Qnil);
11905 set_buffer_internal_1 (prev);
11910 #if ! defined (USE_GTK) && ! defined (HAVE_NS)
11912 /* Set F->desired_tool_bar_string to a Lisp string representing frame
11913 F's desired tool-bar contents. F->tool_bar_items must have
11914 been set up previously by calling prepare_menu_bars. */
11916 static void
11917 build_desired_tool_bar_string (struct frame *f)
11919 int i, size, size_needed;
11920 struct gcpro gcpro1, gcpro2;
11921 Lisp_Object image, plist;
11923 image = plist = Qnil;
11924 GCPRO2 (image, plist);
11926 /* Prepare F->desired_tool_bar_string. If we can reuse it, do so.
11927 Otherwise, make a new string. */
11929 /* The size of the string we might be able to reuse. */
11930 size = (STRINGP (f->desired_tool_bar_string)
11931 ? SCHARS (f->desired_tool_bar_string)
11932 : 0);
11934 /* We need one space in the string for each image. */
11935 size_needed = f->n_tool_bar_items;
11937 /* Reuse f->desired_tool_bar_string, if possible. */
11938 if (size < size_needed || NILP (f->desired_tool_bar_string))
11939 fset_desired_tool_bar_string
11940 (f, Fmake_string (make_number (size_needed), make_number (' ')));
11941 else
11943 AUTO_LIST4 (props, Qdisplay, Qnil, Qmenu_item, Qnil);
11944 struct gcpro gcpro1;
11945 GCPRO1 (props);
11946 Fremove_text_properties (make_number (0), make_number (size),
11947 props, f->desired_tool_bar_string);
11948 UNGCPRO;
11951 /* Put a `display' property on the string for the images to display,
11952 put a `menu_item' property on tool-bar items with a value that
11953 is the index of the item in F's tool-bar item vector. */
11954 for (i = 0; i < f->n_tool_bar_items; ++i)
11956 #define PROP(IDX) \
11957 AREF (f->tool_bar_items, i * TOOL_BAR_ITEM_NSLOTS + (IDX))
11959 int enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
11960 int selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
11961 int hmargin, vmargin, relief, idx, end;
11963 /* If image is a vector, choose the image according to the
11964 button state. */
11965 image = PROP (TOOL_BAR_ITEM_IMAGES);
11966 if (VECTORP (image))
11968 if (enabled_p)
11969 idx = (selected_p
11970 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
11971 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
11972 else
11973 idx = (selected_p
11974 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
11975 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
11977 eassert (ASIZE (image) >= idx);
11978 image = AREF (image, idx);
11980 else
11981 idx = -1;
11983 /* Ignore invalid image specifications. */
11984 if (!valid_image_p (image))
11985 continue;
11987 /* Display the tool-bar button pressed, or depressed. */
11988 plist = Fcopy_sequence (XCDR (image));
11990 /* Compute margin and relief to draw. */
11991 relief = (tool_bar_button_relief >= 0
11992 ? tool_bar_button_relief
11993 : DEFAULT_TOOL_BAR_BUTTON_RELIEF);
11994 hmargin = vmargin = relief;
11996 if (RANGED_INTEGERP (1, Vtool_bar_button_margin,
11997 INT_MAX - max (hmargin, vmargin)))
11999 hmargin += XFASTINT (Vtool_bar_button_margin);
12000 vmargin += XFASTINT (Vtool_bar_button_margin);
12002 else if (CONSP (Vtool_bar_button_margin))
12004 if (RANGED_INTEGERP (1, XCAR (Vtool_bar_button_margin),
12005 INT_MAX - hmargin))
12006 hmargin += XFASTINT (XCAR (Vtool_bar_button_margin));
12008 if (RANGED_INTEGERP (1, XCDR (Vtool_bar_button_margin),
12009 INT_MAX - vmargin))
12010 vmargin += XFASTINT (XCDR (Vtool_bar_button_margin));
12013 if (auto_raise_tool_bar_buttons_p)
12015 /* Add a `:relief' property to the image spec if the item is
12016 selected. */
12017 if (selected_p)
12019 plist = Fplist_put (plist, QCrelief, make_number (-relief));
12020 hmargin -= relief;
12021 vmargin -= relief;
12024 else
12026 /* If image is selected, display it pressed, i.e. with a
12027 negative relief. If it's not selected, display it with a
12028 raised relief. */
12029 plist = Fplist_put (plist, QCrelief,
12030 (selected_p
12031 ? make_number (-relief)
12032 : make_number (relief)));
12033 hmargin -= relief;
12034 vmargin -= relief;
12037 /* Put a margin around the image. */
12038 if (hmargin || vmargin)
12040 if (hmargin == vmargin)
12041 plist = Fplist_put (plist, QCmargin, make_number (hmargin));
12042 else
12043 plist = Fplist_put (plist, QCmargin,
12044 Fcons (make_number (hmargin),
12045 make_number (vmargin)));
12048 /* If button is not enabled, and we don't have special images
12049 for the disabled state, make the image appear disabled by
12050 applying an appropriate algorithm to it. */
12051 if (!enabled_p && idx < 0)
12052 plist = Fplist_put (plist, QCconversion, Qdisabled);
12054 /* Put a `display' text property on the string for the image to
12055 display. Put a `menu-item' property on the string that gives
12056 the start of this item's properties in the tool-bar items
12057 vector. */
12058 image = Fcons (Qimage, plist);
12059 AUTO_LIST4 (props, Qdisplay, image, Qmenu_item,
12060 make_number (i * TOOL_BAR_ITEM_NSLOTS));
12061 struct gcpro gcpro1;
12062 GCPRO1 (props);
12064 /* Let the last image hide all remaining spaces in the tool bar
12065 string. The string can be longer than needed when we reuse a
12066 previous string. */
12067 if (i + 1 == f->n_tool_bar_items)
12068 end = SCHARS (f->desired_tool_bar_string);
12069 else
12070 end = i + 1;
12071 Fadd_text_properties (make_number (i), make_number (end),
12072 props, f->desired_tool_bar_string);
12073 UNGCPRO;
12074 #undef PROP
12077 UNGCPRO;
12081 /* Display one line of the tool-bar of frame IT->f.
12083 HEIGHT specifies the desired height of the tool-bar line.
12084 If the actual height of the glyph row is less than HEIGHT, the
12085 row's height is increased to HEIGHT, and the icons are centered
12086 vertically in the new height.
12088 If HEIGHT is -1, we are counting needed tool-bar lines, so don't
12089 count a final empty row in case the tool-bar width exactly matches
12090 the window width.
12093 static void
12094 display_tool_bar_line (struct it *it, int height)
12096 struct glyph_row *row = it->glyph_row;
12097 int max_x = it->last_visible_x;
12098 struct glyph *last;
12100 /* Don't extend on a previously drawn tool bar items (Bug#16058). */
12101 clear_glyph_row (row);
12102 row->enabled_p = true;
12103 row->y = it->current_y;
12105 /* Note that this isn't made use of if the face hasn't a box,
12106 so there's no need to check the face here. */
12107 it->start_of_box_run_p = 1;
12109 while (it->current_x < max_x)
12111 int x, n_glyphs_before, i, nglyphs;
12112 struct it it_before;
12114 /* Get the next display element. */
12115 if (!get_next_display_element (it))
12117 /* Don't count empty row if we are counting needed tool-bar lines. */
12118 if (height < 0 && !it->hpos)
12119 return;
12120 break;
12123 /* Produce glyphs. */
12124 n_glyphs_before = row->used[TEXT_AREA];
12125 it_before = *it;
12127 PRODUCE_GLYPHS (it);
12129 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
12130 i = 0;
12131 x = it_before.current_x;
12132 while (i < nglyphs)
12134 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
12136 if (x + glyph->pixel_width > max_x)
12138 /* Glyph doesn't fit on line. Backtrack. */
12139 row->used[TEXT_AREA] = n_glyphs_before;
12140 *it = it_before;
12141 /* If this is the only glyph on this line, it will never fit on the
12142 tool-bar, so skip it. But ensure there is at least one glyph,
12143 so we don't accidentally disable the tool-bar. */
12144 if (n_glyphs_before == 0
12145 && (it->vpos > 0 || IT_STRING_CHARPOS (*it) < it->end_charpos-1))
12146 break;
12147 goto out;
12150 ++it->hpos;
12151 x += glyph->pixel_width;
12152 ++i;
12155 /* Stop at line end. */
12156 if (ITERATOR_AT_END_OF_LINE_P (it))
12157 break;
12159 set_iterator_to_next (it, 1);
12162 out:;
12164 row->displays_text_p = row->used[TEXT_AREA] != 0;
12166 /* Use default face for the border below the tool bar.
12168 FIXME: When auto-resize-tool-bars is grow-only, there is
12169 no additional border below the possibly empty tool-bar lines.
12170 So to make the extra empty lines look "normal", we have to
12171 use the tool-bar face for the border too. */
12172 if (!MATRIX_ROW_DISPLAYS_TEXT_P (row)
12173 && !EQ (Vauto_resize_tool_bars, Qgrow_only))
12174 it->face_id = DEFAULT_FACE_ID;
12176 extend_face_to_end_of_line (it);
12177 last = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
12178 last->right_box_line_p = 1;
12179 if (last == row->glyphs[TEXT_AREA])
12180 last->left_box_line_p = 1;
12182 /* Make line the desired height and center it vertically. */
12183 if ((height -= it->max_ascent + it->max_descent) > 0)
12185 /* Don't add more than one line height. */
12186 height %= FRAME_LINE_HEIGHT (it->f);
12187 it->max_ascent += height / 2;
12188 it->max_descent += (height + 1) / 2;
12191 compute_line_metrics (it);
12193 /* If line is empty, make it occupy the rest of the tool-bar. */
12194 if (!MATRIX_ROW_DISPLAYS_TEXT_P (row))
12196 row->height = row->phys_height = it->last_visible_y - row->y;
12197 row->visible_height = row->height;
12198 row->ascent = row->phys_ascent = 0;
12199 row->extra_line_spacing = 0;
12202 row->full_width_p = 1;
12203 row->continued_p = 0;
12204 row->truncated_on_left_p = 0;
12205 row->truncated_on_right_p = 0;
12207 it->current_x = it->hpos = 0;
12208 it->current_y += row->height;
12209 ++it->vpos;
12210 ++it->glyph_row;
12214 /* Value is the number of pixels needed to make all tool-bar items of
12215 frame F visible. The actual number of glyph rows needed is
12216 returned in *N_ROWS if non-NULL. */
12217 static int
12218 tool_bar_height (struct frame *f, int *n_rows, bool pixelwise)
12220 struct window *w = XWINDOW (f->tool_bar_window);
12221 struct it it;
12222 /* tool_bar_height is called from redisplay_tool_bar after building
12223 the desired matrix, so use (unused) mode-line row as temporary row to
12224 avoid destroying the first tool-bar row. */
12225 struct glyph_row *temp_row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
12227 /* Initialize an iterator for iteration over
12228 F->desired_tool_bar_string in the tool-bar window of frame F. */
12229 init_iterator (&it, w, -1, -1, temp_row, TOOL_BAR_FACE_ID);
12230 temp_row->reversed_p = false;
12231 it.first_visible_x = 0;
12232 it.last_visible_x = WINDOW_PIXEL_WIDTH (w);
12233 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
12234 it.paragraph_embedding = L2R;
12236 while (!ITERATOR_AT_END_P (&it))
12238 clear_glyph_row (temp_row);
12239 it.glyph_row = temp_row;
12240 display_tool_bar_line (&it, -1);
12242 clear_glyph_row (temp_row);
12244 /* f->n_tool_bar_rows == 0 means "unknown"; -1 means no tool-bar. */
12245 if (n_rows)
12246 *n_rows = it.vpos > 0 ? it.vpos : -1;
12248 if (pixelwise)
12249 return it.current_y;
12250 else
12251 return (it.current_y + FRAME_LINE_HEIGHT (f) - 1) / FRAME_LINE_HEIGHT (f);
12254 #endif /* !USE_GTK && !HAVE_NS */
12256 DEFUN ("tool-bar-height", Ftool_bar_height, Stool_bar_height,
12257 0, 2, 0,
12258 doc: /* Return the number of lines occupied by the tool bar of FRAME.
12259 If FRAME is nil or omitted, use the selected frame. Optional argument
12260 PIXELWISE non-nil means return the height of the tool bar in pixels. */)
12261 (Lisp_Object frame, Lisp_Object pixelwise)
12263 int height = 0;
12265 #if ! defined (USE_GTK) && ! defined (HAVE_NS)
12266 struct frame *f = decode_any_frame (frame);
12268 if (WINDOWP (f->tool_bar_window)
12269 && WINDOW_PIXEL_HEIGHT (XWINDOW (f->tool_bar_window)) > 0)
12271 update_tool_bar (f, 1);
12272 if (f->n_tool_bar_items)
12274 build_desired_tool_bar_string (f);
12275 height = tool_bar_height (f, NULL, NILP (pixelwise) ? 0 : 1);
12278 #endif
12280 return make_number (height);
12284 /* Display the tool-bar of frame F. Value is non-zero if tool-bar's
12285 height should be changed. */
12286 static int
12287 redisplay_tool_bar (struct frame *f)
12289 #if defined (USE_GTK) || defined (HAVE_NS)
12291 if (FRAME_EXTERNAL_TOOL_BAR (f))
12292 update_frame_tool_bar (f);
12293 return 0;
12295 #else /* !USE_GTK && !HAVE_NS */
12297 struct window *w;
12298 struct it it;
12299 struct glyph_row *row;
12301 /* If frame hasn't a tool-bar window or if it is zero-height, don't
12302 do anything. This means you must start with tool-bar-lines
12303 non-zero to get the auto-sizing effect. Or in other words, you
12304 can turn off tool-bars by specifying tool-bar-lines zero. */
12305 if (!WINDOWP (f->tool_bar_window)
12306 || (w = XWINDOW (f->tool_bar_window),
12307 WINDOW_TOTAL_LINES (w) == 0))
12308 return 0;
12310 /* Set up an iterator for the tool-bar window. */
12311 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
12312 it.first_visible_x = 0;
12313 it.last_visible_x = WINDOW_PIXEL_WIDTH (w);
12314 row = it.glyph_row;
12315 row->reversed_p = false;
12317 /* Build a string that represents the contents of the tool-bar. */
12318 build_desired_tool_bar_string (f);
12319 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
12320 /* FIXME: This should be controlled by a user option. But it
12321 doesn't make sense to have an R2L tool bar if the menu bar cannot
12322 be drawn also R2L, and making the menu bar R2L is tricky due
12323 toolkit-specific code that implements it. If an R2L tool bar is
12324 ever supported, display_tool_bar_line should also be augmented to
12325 call unproduce_glyphs like display_line and display_string
12326 do. */
12327 it.paragraph_embedding = L2R;
12329 if (f->n_tool_bar_rows == 0)
12331 int new_height = tool_bar_height (f, &f->n_tool_bar_rows, 1);
12333 if (new_height != WINDOW_PIXEL_HEIGHT (w))
12335 x_change_tool_bar_height (f, new_height);
12336 frame_default_tool_bar_height = new_height;
12337 /* Always do that now. */
12338 clear_glyph_matrix (w->desired_matrix);
12339 f->fonts_changed = 1;
12340 return 1;
12344 /* Display as many lines as needed to display all tool-bar items. */
12346 if (f->n_tool_bar_rows > 0)
12348 int border, rows, height, extra;
12350 if (TYPE_RANGED_INTEGERP (int, Vtool_bar_border))
12351 border = XINT (Vtool_bar_border);
12352 else if (EQ (Vtool_bar_border, Qinternal_border_width))
12353 border = FRAME_INTERNAL_BORDER_WIDTH (f);
12354 else if (EQ (Vtool_bar_border, Qborder_width))
12355 border = f->border_width;
12356 else
12357 border = 0;
12358 if (border < 0)
12359 border = 0;
12361 rows = f->n_tool_bar_rows;
12362 height = max (1, (it.last_visible_y - border) / rows);
12363 extra = it.last_visible_y - border - height * rows;
12365 while (it.current_y < it.last_visible_y)
12367 int h = 0;
12368 if (extra > 0 && rows-- > 0)
12370 h = (extra + rows - 1) / rows;
12371 extra -= h;
12373 display_tool_bar_line (&it, height + h);
12376 else
12378 while (it.current_y < it.last_visible_y)
12379 display_tool_bar_line (&it, 0);
12382 /* It doesn't make much sense to try scrolling in the tool-bar
12383 window, so don't do it. */
12384 w->desired_matrix->no_scrolling_p = 1;
12385 w->must_be_updated_p = 1;
12387 if (!NILP (Vauto_resize_tool_bars))
12389 int change_height_p = 0;
12391 /* If we couldn't display everything, change the tool-bar's
12392 height if there is room for more. */
12393 if (IT_STRING_CHARPOS (it) < it.end_charpos)
12394 change_height_p = 1;
12396 /* We subtract 1 because display_tool_bar_line advances the
12397 glyph_row pointer before returning to its caller. We want to
12398 examine the last glyph row produced by
12399 display_tool_bar_line. */
12400 row = it.glyph_row - 1;
12402 /* If there are blank lines at the end, except for a partially
12403 visible blank line at the end that is smaller than
12404 FRAME_LINE_HEIGHT, change the tool-bar's height. */
12405 if (!MATRIX_ROW_DISPLAYS_TEXT_P (row)
12406 && row->height >= FRAME_LINE_HEIGHT (f))
12407 change_height_p = 1;
12409 /* If row displays tool-bar items, but is partially visible,
12410 change the tool-bar's height. */
12411 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
12412 && MATRIX_ROW_BOTTOM_Y (row) > it.last_visible_y)
12413 change_height_p = 1;
12415 /* Resize windows as needed by changing the `tool-bar-lines'
12416 frame parameter. */
12417 if (change_height_p)
12419 int nrows;
12420 int new_height = tool_bar_height (f, &nrows, 1);
12422 change_height_p = ((EQ (Vauto_resize_tool_bars, Qgrow_only)
12423 && !f->minimize_tool_bar_window_p)
12424 ? (new_height > WINDOW_PIXEL_HEIGHT (w))
12425 : (new_height != WINDOW_PIXEL_HEIGHT (w)));
12426 f->minimize_tool_bar_window_p = 0;
12428 if (change_height_p)
12430 x_change_tool_bar_height (f, new_height);
12431 frame_default_tool_bar_height = new_height;
12432 clear_glyph_matrix (w->desired_matrix);
12433 f->n_tool_bar_rows = nrows;
12434 f->fonts_changed = 1;
12436 return 1;
12441 f->minimize_tool_bar_window_p = 0;
12442 return 0;
12444 #endif /* USE_GTK || HAVE_NS */
12447 #if ! defined (USE_GTK) && ! defined (HAVE_NS)
12449 /* Get information about the tool-bar item which is displayed in GLYPH
12450 on frame F. Return in *PROP_IDX the index where tool-bar item
12451 properties start in F->tool_bar_items. Value is zero if
12452 GLYPH doesn't display a tool-bar item. */
12454 static int
12455 tool_bar_item_info (struct frame *f, struct glyph *glyph, int *prop_idx)
12457 Lisp_Object prop;
12458 int success_p;
12459 int charpos;
12461 /* This function can be called asynchronously, which means we must
12462 exclude any possibility that Fget_text_property signals an
12463 error. */
12464 charpos = min (SCHARS (f->current_tool_bar_string), glyph->charpos);
12465 charpos = max (0, charpos);
12467 /* Get the text property `menu-item' at pos. The value of that
12468 property is the start index of this item's properties in
12469 F->tool_bar_items. */
12470 prop = Fget_text_property (make_number (charpos),
12471 Qmenu_item, f->current_tool_bar_string);
12472 if (INTEGERP (prop))
12474 *prop_idx = XINT (prop);
12475 success_p = 1;
12477 else
12478 success_p = 0;
12480 return success_p;
12484 /* Get information about the tool-bar item at position X/Y on frame F.
12485 Return in *GLYPH a pointer to the glyph of the tool-bar item in
12486 the current matrix of the tool-bar window of F, or NULL if not
12487 on a tool-bar item. Return in *PROP_IDX the index of the tool-bar
12488 item in F->tool_bar_items. Value is
12490 -1 if X/Y is not on a tool-bar item
12491 0 if X/Y is on the same item that was highlighted before.
12492 1 otherwise. */
12494 static int
12495 get_tool_bar_item (struct frame *f, int x, int y, struct glyph **glyph,
12496 int *hpos, int *vpos, int *prop_idx)
12498 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12499 struct window *w = XWINDOW (f->tool_bar_window);
12500 int area;
12502 /* Find the glyph under X/Y. */
12503 *glyph = x_y_to_hpos_vpos (w, x, y, hpos, vpos, 0, 0, &area);
12504 if (*glyph == NULL)
12505 return -1;
12507 /* Get the start of this tool-bar item's properties in
12508 f->tool_bar_items. */
12509 if (!tool_bar_item_info (f, *glyph, prop_idx))
12510 return -1;
12512 /* Is mouse on the highlighted item? */
12513 if (EQ (f->tool_bar_window, hlinfo->mouse_face_window)
12514 && *vpos >= hlinfo->mouse_face_beg_row
12515 && *vpos <= hlinfo->mouse_face_end_row
12516 && (*vpos > hlinfo->mouse_face_beg_row
12517 || *hpos >= hlinfo->mouse_face_beg_col)
12518 && (*vpos < hlinfo->mouse_face_end_row
12519 || *hpos < hlinfo->mouse_face_end_col
12520 || hlinfo->mouse_face_past_end))
12521 return 0;
12523 return 1;
12527 /* EXPORT:
12528 Handle mouse button event on the tool-bar of frame F, at
12529 frame-relative coordinates X/Y. DOWN_P is 1 for a button press,
12530 0 for button release. MODIFIERS is event modifiers for button
12531 release. */
12533 void
12534 handle_tool_bar_click (struct frame *f, int x, int y, int down_p,
12535 int modifiers)
12537 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12538 struct window *w = XWINDOW (f->tool_bar_window);
12539 int hpos, vpos, prop_idx;
12540 struct glyph *glyph;
12541 Lisp_Object enabled_p;
12542 int ts;
12544 /* If not on the highlighted tool-bar item, and mouse-highlight is
12545 non-nil, return. This is so we generate the tool-bar button
12546 click only when the mouse button is released on the same item as
12547 where it was pressed. However, when mouse-highlight is disabled,
12548 generate the click when the button is released regardless of the
12549 highlight, since tool-bar items are not highlighted in that
12550 case. */
12551 frame_to_window_pixel_xy (w, &x, &y);
12552 ts = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
12553 if (ts == -1
12554 || (ts != 0 && !NILP (Vmouse_highlight)))
12555 return;
12557 /* When mouse-highlight is off, generate the click for the item
12558 where the button was pressed, disregarding where it was
12559 released. */
12560 if (NILP (Vmouse_highlight) && !down_p)
12561 prop_idx = f->last_tool_bar_item;
12563 /* If item is disabled, do nothing. */
12564 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
12565 if (NILP (enabled_p))
12566 return;
12568 if (down_p)
12570 /* Show item in pressed state. */
12571 if (!NILP (Vmouse_highlight))
12572 show_mouse_face (hlinfo, DRAW_IMAGE_SUNKEN);
12573 f->last_tool_bar_item = prop_idx;
12575 else
12577 Lisp_Object key, frame;
12578 struct input_event event;
12579 EVENT_INIT (event);
12581 /* Show item in released state. */
12582 if (!NILP (Vmouse_highlight))
12583 show_mouse_face (hlinfo, DRAW_IMAGE_RAISED);
12585 key = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_KEY);
12587 XSETFRAME (frame, f);
12588 event.kind = TOOL_BAR_EVENT;
12589 event.frame_or_window = frame;
12590 event.arg = frame;
12591 kbd_buffer_store_event (&event);
12593 event.kind = TOOL_BAR_EVENT;
12594 event.frame_or_window = frame;
12595 event.arg = key;
12596 event.modifiers = modifiers;
12597 kbd_buffer_store_event (&event);
12598 f->last_tool_bar_item = -1;
12603 /* Possibly highlight a tool-bar item on frame F when mouse moves to
12604 tool-bar window-relative coordinates X/Y. Called from
12605 note_mouse_highlight. */
12607 static void
12608 note_tool_bar_highlight (struct frame *f, int x, int y)
12610 Lisp_Object window = f->tool_bar_window;
12611 struct window *w = XWINDOW (window);
12612 Display_Info *dpyinfo = FRAME_DISPLAY_INFO (f);
12613 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12614 int hpos, vpos;
12615 struct glyph *glyph;
12616 struct glyph_row *row;
12617 int i;
12618 Lisp_Object enabled_p;
12619 int prop_idx;
12620 enum draw_glyphs_face draw = DRAW_IMAGE_RAISED;
12621 int mouse_down_p, rc;
12623 /* Function note_mouse_highlight is called with negative X/Y
12624 values when mouse moves outside of the frame. */
12625 if (x <= 0 || y <= 0)
12627 clear_mouse_face (hlinfo);
12628 return;
12631 rc = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
12632 if (rc < 0)
12634 /* Not on tool-bar item. */
12635 clear_mouse_face (hlinfo);
12636 return;
12638 else if (rc == 0)
12639 /* On same tool-bar item as before. */
12640 goto set_help_echo;
12642 clear_mouse_face (hlinfo);
12644 /* Mouse is down, but on different tool-bar item? */
12645 mouse_down_p = (x_mouse_grabbed (dpyinfo)
12646 && f == dpyinfo->last_mouse_frame);
12648 if (mouse_down_p && f->last_tool_bar_item != prop_idx)
12649 return;
12651 draw = mouse_down_p ? DRAW_IMAGE_SUNKEN : DRAW_IMAGE_RAISED;
12653 /* If tool-bar item is not enabled, don't highlight it. */
12654 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
12655 if (!NILP (enabled_p) && !NILP (Vmouse_highlight))
12657 /* Compute the x-position of the glyph. In front and past the
12658 image is a space. We include this in the highlighted area. */
12659 row = MATRIX_ROW (w->current_matrix, vpos);
12660 for (i = x = 0; i < hpos; ++i)
12661 x += row->glyphs[TEXT_AREA][i].pixel_width;
12663 /* Record this as the current active region. */
12664 hlinfo->mouse_face_beg_col = hpos;
12665 hlinfo->mouse_face_beg_row = vpos;
12666 hlinfo->mouse_face_beg_x = x;
12667 hlinfo->mouse_face_past_end = 0;
12669 hlinfo->mouse_face_end_col = hpos + 1;
12670 hlinfo->mouse_face_end_row = vpos;
12671 hlinfo->mouse_face_end_x = x + glyph->pixel_width;
12672 hlinfo->mouse_face_window = window;
12673 hlinfo->mouse_face_face_id = TOOL_BAR_FACE_ID;
12675 /* Display it as active. */
12676 show_mouse_face (hlinfo, draw);
12679 set_help_echo:
12681 /* Set help_echo_string to a help string to display for this tool-bar item.
12682 XTread_socket does the rest. */
12683 help_echo_object = help_echo_window = Qnil;
12684 help_echo_pos = -1;
12685 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_HELP);
12686 if (NILP (help_echo_string))
12687 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_CAPTION);
12690 #endif /* !USE_GTK && !HAVE_NS */
12692 #endif /* HAVE_WINDOW_SYSTEM */
12696 /************************************************************************
12697 Horizontal scrolling
12698 ************************************************************************/
12700 static int hscroll_window_tree (Lisp_Object);
12701 static int hscroll_windows (Lisp_Object);
12703 /* For all leaf windows in the window tree rooted at WINDOW, set their
12704 hscroll value so that PT is (i) visible in the window, and (ii) so
12705 that it is not within a certain margin at the window's left and
12706 right border. Value is non-zero if any window's hscroll has been
12707 changed. */
12709 static int
12710 hscroll_window_tree (Lisp_Object window)
12712 int hscrolled_p = 0;
12713 int hscroll_relative_p = FLOATP (Vhscroll_step);
12714 int hscroll_step_abs = 0;
12715 double hscroll_step_rel = 0;
12717 if (hscroll_relative_p)
12719 hscroll_step_rel = XFLOAT_DATA (Vhscroll_step);
12720 if (hscroll_step_rel < 0)
12722 hscroll_relative_p = 0;
12723 hscroll_step_abs = 0;
12726 else if (TYPE_RANGED_INTEGERP (int, Vhscroll_step))
12728 hscroll_step_abs = XINT (Vhscroll_step);
12729 if (hscroll_step_abs < 0)
12730 hscroll_step_abs = 0;
12732 else
12733 hscroll_step_abs = 0;
12735 while (WINDOWP (window))
12737 struct window *w = XWINDOW (window);
12739 if (WINDOWP (w->contents))
12740 hscrolled_p |= hscroll_window_tree (w->contents);
12741 else if (w->cursor.vpos >= 0)
12743 int h_margin;
12744 int text_area_width;
12745 struct glyph_row *cursor_row;
12746 struct glyph_row *bottom_row;
12747 int row_r2l_p;
12749 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->desired_matrix, w);
12750 if (w->cursor.vpos < bottom_row - w->desired_matrix->rows)
12751 cursor_row = MATRIX_ROW (w->desired_matrix, w->cursor.vpos);
12752 else
12753 cursor_row = bottom_row - 1;
12755 if (!cursor_row->enabled_p)
12757 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
12758 if (w->cursor.vpos < bottom_row - w->current_matrix->rows)
12759 cursor_row = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
12760 else
12761 cursor_row = bottom_row - 1;
12763 row_r2l_p = cursor_row->reversed_p;
12765 text_area_width = window_box_width (w, TEXT_AREA);
12767 /* Scroll when cursor is inside this scroll margin. */
12768 h_margin = hscroll_margin * WINDOW_FRAME_COLUMN_WIDTH (w);
12770 /* If the position of this window's point has explicitly
12771 changed, no more suspend auto hscrolling. */
12772 if (NILP (Fequal (Fwindow_point (window), Fwindow_old_point (window))))
12773 w->suspend_auto_hscroll = 0;
12775 /* Remember window point. */
12776 Fset_marker (w->old_pointm,
12777 ((w == XWINDOW (selected_window))
12778 ? make_number (BUF_PT (XBUFFER (w->contents)))
12779 : Fmarker_position (w->pointm)),
12780 w->contents);
12782 if (!NILP (Fbuffer_local_value (Qauto_hscroll_mode, w->contents))
12783 && w->suspend_auto_hscroll == 0
12784 /* In some pathological cases, like restoring a window
12785 configuration into a frame that is much smaller than
12786 the one from which the configuration was saved, we
12787 get glyph rows whose start and end have zero buffer
12788 positions, which we cannot handle below. Just skip
12789 such windows. */
12790 && CHARPOS (cursor_row->start.pos) >= BUF_BEG (w->contents)
12791 /* For left-to-right rows, hscroll when cursor is either
12792 (i) inside the right hscroll margin, or (ii) if it is
12793 inside the left margin and the window is already
12794 hscrolled. */
12795 && ((!row_r2l_p
12796 && ((w->hscroll && w->cursor.x <= h_margin)
12797 || (cursor_row->enabled_p
12798 && cursor_row->truncated_on_right_p
12799 && (w->cursor.x >= text_area_width - h_margin))))
12800 /* For right-to-left rows, the logic is similar,
12801 except that rules for scrolling to left and right
12802 are reversed. E.g., if cursor.x <= h_margin, we
12803 need to hscroll "to the right" unconditionally,
12804 and that will scroll the screen to the left so as
12805 to reveal the next portion of the row. */
12806 || (row_r2l_p
12807 && ((cursor_row->enabled_p
12808 /* FIXME: It is confusing to set the
12809 truncated_on_right_p flag when R2L rows
12810 are actually truncated on the left. */
12811 && cursor_row->truncated_on_right_p
12812 && w->cursor.x <= h_margin)
12813 || (w->hscroll
12814 && (w->cursor.x >= text_area_width - h_margin))))))
12816 struct it it;
12817 ptrdiff_t hscroll;
12818 struct buffer *saved_current_buffer;
12819 ptrdiff_t pt;
12820 int wanted_x;
12822 /* Find point in a display of infinite width. */
12823 saved_current_buffer = current_buffer;
12824 current_buffer = XBUFFER (w->contents);
12826 if (w == XWINDOW (selected_window))
12827 pt = PT;
12828 else
12829 pt = clip_to_bounds (BEGV, marker_position (w->pointm), ZV);
12831 /* Move iterator to pt starting at cursor_row->start in
12832 a line with infinite width. */
12833 init_to_row_start (&it, w, cursor_row);
12834 it.last_visible_x = INFINITY;
12835 move_it_in_display_line_to (&it, pt, -1, MOVE_TO_POS);
12836 current_buffer = saved_current_buffer;
12838 /* Position cursor in window. */
12839 if (!hscroll_relative_p && hscroll_step_abs == 0)
12840 hscroll = max (0, (it.current_x
12841 - (ITERATOR_AT_END_OF_LINE_P (&it)
12842 ? (text_area_width - 4 * FRAME_COLUMN_WIDTH (it.f))
12843 : (text_area_width / 2))))
12844 / FRAME_COLUMN_WIDTH (it.f);
12845 else if ((!row_r2l_p
12846 && w->cursor.x >= text_area_width - h_margin)
12847 || (row_r2l_p && w->cursor.x <= h_margin))
12849 if (hscroll_relative_p)
12850 wanted_x = text_area_width * (1 - hscroll_step_rel)
12851 - h_margin;
12852 else
12853 wanted_x = text_area_width
12854 - hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
12855 - h_margin;
12856 hscroll
12857 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
12859 else
12861 if (hscroll_relative_p)
12862 wanted_x = text_area_width * hscroll_step_rel
12863 + h_margin;
12864 else
12865 wanted_x = hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
12866 + h_margin;
12867 hscroll
12868 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
12870 hscroll = max (hscroll, w->min_hscroll);
12872 /* Don't prevent redisplay optimizations if hscroll
12873 hasn't changed, as it will unnecessarily slow down
12874 redisplay. */
12875 if (w->hscroll != hscroll)
12877 XBUFFER (w->contents)->prevent_redisplay_optimizations_p = 1;
12878 w->hscroll = hscroll;
12879 hscrolled_p = 1;
12884 window = w->next;
12887 /* Value is non-zero if hscroll of any leaf window has been changed. */
12888 return hscrolled_p;
12892 /* Set hscroll so that cursor is visible and not inside horizontal
12893 scroll margins for all windows in the tree rooted at WINDOW. See
12894 also hscroll_window_tree above. Value is non-zero if any window's
12895 hscroll has been changed. If it has, desired matrices on the frame
12896 of WINDOW are cleared. */
12898 static int
12899 hscroll_windows (Lisp_Object window)
12901 int hscrolled_p = hscroll_window_tree (window);
12902 if (hscrolled_p)
12903 clear_desired_matrices (XFRAME (WINDOW_FRAME (XWINDOW (window))));
12904 return hscrolled_p;
12909 /************************************************************************
12910 Redisplay
12911 ************************************************************************/
12913 /* Variables holding some state of redisplay if GLYPH_DEBUG is defined
12914 to a non-zero value. This is sometimes handy to have in a debugger
12915 session. */
12917 #ifdef GLYPH_DEBUG
12919 /* First and last unchanged row for try_window_id. */
12921 static int debug_first_unchanged_at_end_vpos;
12922 static int debug_last_unchanged_at_beg_vpos;
12924 /* Delta vpos and y. */
12926 static int debug_dvpos, debug_dy;
12928 /* Delta in characters and bytes for try_window_id. */
12930 static ptrdiff_t debug_delta, debug_delta_bytes;
12932 /* Values of window_end_pos and window_end_vpos at the end of
12933 try_window_id. */
12935 static ptrdiff_t debug_end_vpos;
12937 /* Append a string to W->desired_matrix->method. FMT is a printf
12938 format string. If trace_redisplay_p is true also printf the
12939 resulting string to stderr. */
12941 static void debug_method_add (struct window *, char const *, ...)
12942 ATTRIBUTE_FORMAT_PRINTF (2, 3);
12944 static void
12945 debug_method_add (struct window *w, char const *fmt, ...)
12947 void *ptr = w;
12948 char *method = w->desired_matrix->method;
12949 int len = strlen (method);
12950 int size = sizeof w->desired_matrix->method;
12951 int remaining = size - len - 1;
12952 va_list ap;
12954 if (len && remaining)
12956 method[len] = '|';
12957 --remaining, ++len;
12960 va_start (ap, fmt);
12961 vsnprintf (method + len, remaining + 1, fmt, ap);
12962 va_end (ap);
12964 if (trace_redisplay_p)
12965 fprintf (stderr, "%p (%s): %s\n",
12966 ptr,
12967 ((BUFFERP (w->contents)
12968 && STRINGP (BVAR (XBUFFER (w->contents), name)))
12969 ? SSDATA (BVAR (XBUFFER (w->contents), name))
12970 : "no buffer"),
12971 method + len);
12974 #endif /* GLYPH_DEBUG */
12977 /* Value is non-zero if all changes in window W, which displays
12978 current_buffer, are in the text between START and END. START is a
12979 buffer position, END is given as a distance from Z. Used in
12980 redisplay_internal for display optimization. */
12982 static int
12983 text_outside_line_unchanged_p (struct window *w,
12984 ptrdiff_t start, ptrdiff_t end)
12986 int unchanged_p = 1;
12988 /* If text or overlays have changed, see where. */
12989 if (window_outdated (w))
12991 /* Gap in the line? */
12992 if (GPT < start || Z - GPT < end)
12993 unchanged_p = 0;
12995 /* Changes start in front of the line, or end after it? */
12996 if (unchanged_p
12997 && (BEG_UNCHANGED < start - 1
12998 || END_UNCHANGED < end))
12999 unchanged_p = 0;
13001 /* If selective display, can't optimize if changes start at the
13002 beginning of the line. */
13003 if (unchanged_p
13004 && INTEGERP (BVAR (current_buffer, selective_display))
13005 && XINT (BVAR (current_buffer, selective_display)) > 0
13006 && (BEG_UNCHANGED < start || GPT <= start))
13007 unchanged_p = 0;
13009 /* If there are overlays at the start or end of the line, these
13010 may have overlay strings with newlines in them. A change at
13011 START, for instance, may actually concern the display of such
13012 overlay strings as well, and they are displayed on different
13013 lines. So, quickly rule out this case. (For the future, it
13014 might be desirable to implement something more telling than
13015 just BEG/END_UNCHANGED.) */
13016 if (unchanged_p)
13018 if (BEG + BEG_UNCHANGED == start
13019 && overlay_touches_p (start))
13020 unchanged_p = 0;
13021 if (END_UNCHANGED == end
13022 && overlay_touches_p (Z - end))
13023 unchanged_p = 0;
13026 /* Under bidi reordering, adding or deleting a character in the
13027 beginning of a paragraph, before the first strong directional
13028 character, can change the base direction of the paragraph (unless
13029 the buffer specifies a fixed paragraph direction), which will
13030 require to redisplay the whole paragraph. It might be worthwhile
13031 to find the paragraph limits and widen the range of redisplayed
13032 lines to that, but for now just give up this optimization. */
13033 if (!NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering))
13034 && NILP (BVAR (XBUFFER (w->contents), bidi_paragraph_direction)))
13035 unchanged_p = 0;
13038 return unchanged_p;
13042 /* Do a frame update, taking possible shortcuts into account. This is
13043 the main external entry point for redisplay.
13045 If the last redisplay displayed an echo area message and that message
13046 is no longer requested, we clear the echo area or bring back the
13047 mini-buffer if that is in use. */
13049 void
13050 redisplay (void)
13052 redisplay_internal ();
13056 static Lisp_Object
13057 overlay_arrow_string_or_property (Lisp_Object var)
13059 Lisp_Object val;
13061 if (val = Fget (var, Qoverlay_arrow_string), STRINGP (val))
13062 return val;
13064 return Voverlay_arrow_string;
13067 /* Return 1 if there are any overlay-arrows in current_buffer. */
13068 static int
13069 overlay_arrow_in_current_buffer_p (void)
13071 Lisp_Object vlist;
13073 for (vlist = Voverlay_arrow_variable_list;
13074 CONSP (vlist);
13075 vlist = XCDR (vlist))
13077 Lisp_Object var = XCAR (vlist);
13078 Lisp_Object val;
13080 if (!SYMBOLP (var))
13081 continue;
13082 val = find_symbol_value (var);
13083 if (MARKERP (val)
13084 && current_buffer == XMARKER (val)->buffer)
13085 return 1;
13087 return 0;
13091 /* Return 1 if any overlay_arrows have moved or overlay-arrow-string
13092 has changed. */
13094 static int
13095 overlay_arrows_changed_p (void)
13097 Lisp_Object vlist;
13099 for (vlist = Voverlay_arrow_variable_list;
13100 CONSP (vlist);
13101 vlist = XCDR (vlist))
13103 Lisp_Object var = XCAR (vlist);
13104 Lisp_Object val, pstr;
13106 if (!SYMBOLP (var))
13107 continue;
13108 val = find_symbol_value (var);
13109 if (!MARKERP (val))
13110 continue;
13111 if (! EQ (COERCE_MARKER (val),
13112 Fget (var, Qlast_arrow_position))
13113 || ! (pstr = overlay_arrow_string_or_property (var),
13114 EQ (pstr, Fget (var, Qlast_arrow_string))))
13115 return 1;
13117 return 0;
13120 /* Mark overlay arrows to be updated on next redisplay. */
13122 static void
13123 update_overlay_arrows (int up_to_date)
13125 Lisp_Object vlist;
13127 for (vlist = Voverlay_arrow_variable_list;
13128 CONSP (vlist);
13129 vlist = XCDR (vlist))
13131 Lisp_Object var = XCAR (vlist);
13133 if (!SYMBOLP (var))
13134 continue;
13136 if (up_to_date > 0)
13138 Lisp_Object val = find_symbol_value (var);
13139 Fput (var, Qlast_arrow_position,
13140 COERCE_MARKER (val));
13141 Fput (var, Qlast_arrow_string,
13142 overlay_arrow_string_or_property (var));
13144 else if (up_to_date < 0
13145 || !NILP (Fget (var, Qlast_arrow_position)))
13147 Fput (var, Qlast_arrow_position, Qt);
13148 Fput (var, Qlast_arrow_string, Qt);
13154 /* Return overlay arrow string to display at row.
13155 Return integer (bitmap number) for arrow bitmap in left fringe.
13156 Return nil if no overlay arrow. */
13158 static Lisp_Object
13159 overlay_arrow_at_row (struct it *it, struct glyph_row *row)
13161 Lisp_Object vlist;
13163 for (vlist = Voverlay_arrow_variable_list;
13164 CONSP (vlist);
13165 vlist = XCDR (vlist))
13167 Lisp_Object var = XCAR (vlist);
13168 Lisp_Object val;
13170 if (!SYMBOLP (var))
13171 continue;
13173 val = find_symbol_value (var);
13175 if (MARKERP (val)
13176 && current_buffer == XMARKER (val)->buffer
13177 && (MATRIX_ROW_START_CHARPOS (row) == marker_position (val)))
13179 if (FRAME_WINDOW_P (it->f)
13180 /* FIXME: if ROW->reversed_p is set, this should test
13181 the right fringe, not the left one. */
13182 && WINDOW_LEFT_FRINGE_WIDTH (it->w) > 0)
13184 #ifdef HAVE_WINDOW_SYSTEM
13185 if (val = Fget (var, Qoverlay_arrow_bitmap), SYMBOLP (val))
13187 int fringe_bitmap;
13188 if ((fringe_bitmap = lookup_fringe_bitmap (val)) != 0)
13189 return make_number (fringe_bitmap);
13191 #endif
13192 return make_number (-1); /* Use default arrow bitmap. */
13194 return overlay_arrow_string_or_property (var);
13198 return Qnil;
13201 /* Return 1 if point moved out of or into a composition. Otherwise
13202 return 0. PREV_BUF and PREV_PT are the last point buffer and
13203 position. BUF and PT are the current point buffer and position. */
13205 static int
13206 check_point_in_composition (struct buffer *prev_buf, ptrdiff_t prev_pt,
13207 struct buffer *buf, ptrdiff_t pt)
13209 ptrdiff_t start, end;
13210 Lisp_Object prop;
13211 Lisp_Object buffer;
13213 XSETBUFFER (buffer, buf);
13214 /* Check a composition at the last point if point moved within the
13215 same buffer. */
13216 if (prev_buf == buf)
13218 if (prev_pt == pt)
13219 /* Point didn't move. */
13220 return 0;
13222 if (prev_pt > BUF_BEGV (buf) && prev_pt < BUF_ZV (buf)
13223 && find_composition (prev_pt, -1, &start, &end, &prop, buffer)
13224 && composition_valid_p (start, end, prop)
13225 && start < prev_pt && end > prev_pt)
13226 /* The last point was within the composition. Return 1 iff
13227 point moved out of the composition. */
13228 return (pt <= start || pt >= end);
13231 /* Check a composition at the current point. */
13232 return (pt > BUF_BEGV (buf) && pt < BUF_ZV (buf)
13233 && find_composition (pt, -1, &start, &end, &prop, buffer)
13234 && composition_valid_p (start, end, prop)
13235 && start < pt && end > pt);
13238 /* Reconsider the clip changes of buffer which is displayed in W. */
13240 static void
13241 reconsider_clip_changes (struct window *w)
13243 struct buffer *b = XBUFFER (w->contents);
13245 if (b->clip_changed
13246 && w->window_end_valid
13247 && w->current_matrix->buffer == b
13248 && w->current_matrix->zv == BUF_ZV (b)
13249 && w->current_matrix->begv == BUF_BEGV (b))
13250 b->clip_changed = 0;
13252 /* If display wasn't paused, and W is not a tool bar window, see if
13253 point has been moved into or out of a composition. In that case,
13254 we set b->clip_changed to 1 to force updating the screen. If
13255 b->clip_changed has already been set to 1, we can skip this
13256 check. */
13257 if (!b->clip_changed && w->window_end_valid)
13259 ptrdiff_t pt = (w == XWINDOW (selected_window)
13260 ? PT : marker_position (w->pointm));
13262 if ((w->current_matrix->buffer != b || pt != w->last_point)
13263 && check_point_in_composition (w->current_matrix->buffer,
13264 w->last_point, b, pt))
13265 b->clip_changed = 1;
13269 static void
13270 propagate_buffer_redisplay (void)
13271 { /* Resetting b->text->redisplay is problematic!
13272 We can't just reset it in the case that some window that displays
13273 it has not been redisplayed; and such a window can stay
13274 unredisplayed for a long time if it's currently invisible.
13275 But we do want to reset it at the end of redisplay otherwise
13276 its displayed windows will keep being redisplayed over and over
13277 again.
13278 So we copy all b->text->redisplay flags up to their windows here,
13279 such that mark_window_display_accurate can safely reset
13280 b->text->redisplay. */
13281 Lisp_Object ws = window_list ();
13282 for (; CONSP (ws); ws = XCDR (ws))
13284 struct window *thisw = XWINDOW (XCAR (ws));
13285 struct buffer *thisb = XBUFFER (thisw->contents);
13286 if (thisb->text->redisplay)
13287 thisw->redisplay = true;
13291 #define STOP_POLLING \
13292 do { if (! polling_stopped_here) stop_polling (); \
13293 polling_stopped_here = 1; } while (0)
13295 #define RESUME_POLLING \
13296 do { if (polling_stopped_here) start_polling (); \
13297 polling_stopped_here = 0; } while (0)
13300 /* Perhaps in the future avoid recentering windows if it
13301 is not necessary; currently that causes some problems. */
13303 static void
13304 redisplay_internal (void)
13306 struct window *w = XWINDOW (selected_window);
13307 struct window *sw;
13308 struct frame *fr;
13309 bool pending;
13310 bool must_finish = 0, match_p;
13311 struct text_pos tlbufpos, tlendpos;
13312 int number_of_visible_frames;
13313 ptrdiff_t count;
13314 struct frame *sf;
13315 int polling_stopped_here = 0;
13316 Lisp_Object tail, frame;
13318 /* True means redisplay has to consider all windows on all
13319 frames. False, only selected_window is considered. */
13320 bool consider_all_windows_p;
13322 /* True means redisplay has to redisplay the miniwindow. */
13323 bool update_miniwindow_p = false;
13325 TRACE ((stderr, "redisplay_internal %d\n", redisplaying_p));
13327 /* No redisplay if running in batch mode or frame is not yet fully
13328 initialized, or redisplay is explicitly turned off by setting
13329 Vinhibit_redisplay. */
13330 if (FRAME_INITIAL_P (SELECTED_FRAME ())
13331 || !NILP (Vinhibit_redisplay))
13332 return;
13334 /* Don't examine these until after testing Vinhibit_redisplay.
13335 When Emacs is shutting down, perhaps because its connection to
13336 X has dropped, we should not look at them at all. */
13337 fr = XFRAME (w->frame);
13338 sf = SELECTED_FRAME ();
13340 if (!fr->glyphs_initialized_p)
13341 return;
13343 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS)
13344 if (popup_activated ())
13345 return;
13346 #endif
13348 /* I don't think this happens but let's be paranoid. */
13349 if (redisplaying_p)
13350 return;
13352 /* Record a function that clears redisplaying_p
13353 when we leave this function. */
13354 count = SPECPDL_INDEX ();
13355 record_unwind_protect_void (unwind_redisplay);
13356 redisplaying_p = 1;
13357 specbind (Qinhibit_free_realized_faces, Qnil);
13359 /* Record this function, so it appears on the profiler's backtraces. */
13360 record_in_backtrace (Qredisplay_internal, 0, 0);
13362 FOR_EACH_FRAME (tail, frame)
13363 XFRAME (frame)->already_hscrolled_p = 0;
13365 retry:
13366 /* Remember the currently selected window. */
13367 sw = w;
13369 pending = false;
13370 last_escape_glyph_frame = NULL;
13371 last_escape_glyph_face_id = (1 << FACE_ID_BITS);
13372 last_glyphless_glyph_frame = NULL;
13373 last_glyphless_glyph_face_id = (1 << FACE_ID_BITS);
13375 /* If face_change, init_iterator will free all realized faces, which
13376 includes the faces referenced from current matrices. So, we
13377 can't reuse current matrices in this case. */
13378 if (face_change)
13379 windows_or_buffers_changed = 47;
13381 if ((FRAME_TERMCAP_P (sf) || FRAME_MSDOS_P (sf))
13382 && FRAME_TTY (sf)->previous_frame != sf)
13384 /* Since frames on a single ASCII terminal share the same
13385 display area, displaying a different frame means redisplay
13386 the whole thing. */
13387 SET_FRAME_GARBAGED (sf);
13388 #ifndef DOS_NT
13389 set_tty_color_mode (FRAME_TTY (sf), sf);
13390 #endif
13391 FRAME_TTY (sf)->previous_frame = sf;
13394 /* Set the visible flags for all frames. Do this before checking for
13395 resized or garbaged frames; they want to know if their frames are
13396 visible. See the comment in frame.h for FRAME_SAMPLE_VISIBILITY. */
13397 number_of_visible_frames = 0;
13399 FOR_EACH_FRAME (tail, frame)
13401 struct frame *f = XFRAME (frame);
13403 if (FRAME_VISIBLE_P (f))
13405 ++number_of_visible_frames;
13406 /* Adjust matrices for visible frames only. */
13407 if (f->fonts_changed)
13409 adjust_frame_glyphs (f);
13410 f->fonts_changed = 0;
13412 /* If cursor type has been changed on the frame
13413 other than selected, consider all frames. */
13414 if (f != sf && f->cursor_type_changed)
13415 update_mode_lines = 31;
13417 clear_desired_matrices (f);
13420 /* Notice any pending interrupt request to change frame size. */
13421 do_pending_window_change (true);
13423 /* do_pending_window_change could change the selected_window due to
13424 frame resizing which makes the selected window too small. */
13425 if (WINDOWP (selected_window) && (w = XWINDOW (selected_window)) != sw)
13426 sw = w;
13428 /* Clear frames marked as garbaged. */
13429 clear_garbaged_frames ();
13431 /* Build menubar and tool-bar items. */
13432 if (NILP (Vmemory_full))
13433 prepare_menu_bars ();
13435 reconsider_clip_changes (w);
13437 /* In most cases selected window displays current buffer. */
13438 match_p = XBUFFER (w->contents) == current_buffer;
13439 if (match_p)
13441 /* Detect case that we need to write or remove a star in the mode line. */
13442 if ((SAVE_MODIFF < MODIFF) != w->last_had_star)
13443 w->update_mode_line = 1;
13445 if (mode_line_update_needed (w))
13446 w->update_mode_line = 1;
13448 /* If reconsider_clip_changes above decided that the narrowing
13449 in the current buffer changed, make sure all other windows
13450 showing that buffer will be redisplayed. */
13451 if (current_buffer->clip_changed)
13452 bset_update_mode_line (current_buffer);
13455 /* Normally the message* functions will have already displayed and
13456 updated the echo area, but the frame may have been trashed, or
13457 the update may have been preempted, so display the echo area
13458 again here. Checking message_cleared_p captures the case that
13459 the echo area should be cleared. */
13460 if ((!NILP (echo_area_buffer[0]) && !display_last_displayed_message_p)
13461 || (!NILP (echo_area_buffer[1]) && display_last_displayed_message_p)
13462 || (message_cleared_p
13463 && minibuf_level == 0
13464 /* If the mini-window is currently selected, this means the
13465 echo-area doesn't show through. */
13466 && !MINI_WINDOW_P (XWINDOW (selected_window))))
13468 int window_height_changed_p = echo_area_display (false);
13470 if (message_cleared_p)
13471 update_miniwindow_p = true;
13473 must_finish = 1;
13475 /* If we don't display the current message, don't clear the
13476 message_cleared_p flag, because, if we did, we wouldn't clear
13477 the echo area in the next redisplay which doesn't preserve
13478 the echo area. */
13479 if (!display_last_displayed_message_p)
13480 message_cleared_p = 0;
13482 if (window_height_changed_p)
13484 windows_or_buffers_changed = 50;
13486 /* If window configuration was changed, frames may have been
13487 marked garbaged. Clear them or we will experience
13488 surprises wrt scrolling. */
13489 clear_garbaged_frames ();
13492 else if (EQ (selected_window, minibuf_window)
13493 && (current_buffer->clip_changed || window_outdated (w))
13494 && resize_mini_window (w, 0))
13496 /* Resized active mini-window to fit the size of what it is
13497 showing if its contents might have changed. */
13498 must_finish = 1;
13500 /* If window configuration was changed, frames may have been
13501 marked garbaged. Clear them or we will experience
13502 surprises wrt scrolling. */
13503 clear_garbaged_frames ();
13506 if (windows_or_buffers_changed && !update_mode_lines)
13507 /* Code that sets windows_or_buffers_changed doesn't distinguish whether
13508 only the windows's contents needs to be refreshed, or whether the
13509 mode-lines also need a refresh. */
13510 update_mode_lines = (windows_or_buffers_changed == REDISPLAY_SOME
13511 ? REDISPLAY_SOME : 32);
13513 /* If specs for an arrow have changed, do thorough redisplay
13514 to ensure we remove any arrow that should no longer exist. */
13515 if (overlay_arrows_changed_p ())
13516 /* Apparently, this is the only case where we update other windows,
13517 without updating other mode-lines. */
13518 windows_or_buffers_changed = 49;
13520 consider_all_windows_p = (update_mode_lines
13521 || windows_or_buffers_changed);
13523 #define AINC(a,i) \
13524 if (VECTORP (a) && i >= 0 && i < ASIZE (a) && INTEGERP (AREF (a, i))) \
13525 ASET (a, i, make_number (1 + XINT (AREF (a, i))))
13527 AINC (Vredisplay__all_windows_cause, windows_or_buffers_changed);
13528 AINC (Vredisplay__mode_lines_cause, update_mode_lines);
13530 /* Optimize the case that only the line containing the cursor in the
13531 selected window has changed. Variables starting with this_ are
13532 set in display_line and record information about the line
13533 containing the cursor. */
13534 tlbufpos = this_line_start_pos;
13535 tlendpos = this_line_end_pos;
13536 if (!consider_all_windows_p
13537 && CHARPOS (tlbufpos) > 0
13538 && !w->update_mode_line
13539 && !current_buffer->clip_changed
13540 && !current_buffer->prevent_redisplay_optimizations_p
13541 && FRAME_VISIBLE_P (XFRAME (w->frame))
13542 && !FRAME_OBSCURED_P (XFRAME (w->frame))
13543 && !XFRAME (w->frame)->cursor_type_changed
13544 /* Make sure recorded data applies to current buffer, etc. */
13545 && this_line_buffer == current_buffer
13546 && match_p
13547 && !w->force_start
13548 && !w->optional_new_start
13549 /* Point must be on the line that we have info recorded about. */
13550 && PT >= CHARPOS (tlbufpos)
13551 && PT <= Z - CHARPOS (tlendpos)
13552 /* All text outside that line, including its final newline,
13553 must be unchanged. */
13554 && text_outside_line_unchanged_p (w, CHARPOS (tlbufpos),
13555 CHARPOS (tlendpos)))
13557 if (CHARPOS (tlbufpos) > BEGV
13558 && FETCH_BYTE (BYTEPOS (tlbufpos) - 1) != '\n'
13559 && (CHARPOS (tlbufpos) == ZV
13560 || FETCH_BYTE (BYTEPOS (tlbufpos)) == '\n'))
13561 /* Former continuation line has disappeared by becoming empty. */
13562 goto cancel;
13563 else if (window_outdated (w) || MINI_WINDOW_P (w))
13565 /* We have to handle the case of continuation around a
13566 wide-column character (see the comment in indent.c around
13567 line 1340).
13569 For instance, in the following case:
13571 -------- Insert --------
13572 K_A_N_\\ `a' K_A_N_a\ `X_' are wide-column chars.
13573 J_I_ ==> J_I_ `^^' are cursors.
13574 ^^ ^^
13575 -------- --------
13577 As we have to redraw the line above, we cannot use this
13578 optimization. */
13580 struct it it;
13581 int line_height_before = this_line_pixel_height;
13583 /* Note that start_display will handle the case that the
13584 line starting at tlbufpos is a continuation line. */
13585 start_display (&it, w, tlbufpos);
13587 /* Implementation note: It this still necessary? */
13588 if (it.current_x != this_line_start_x)
13589 goto cancel;
13591 TRACE ((stderr, "trying display optimization 1\n"));
13592 w->cursor.vpos = -1;
13593 overlay_arrow_seen = 0;
13594 it.vpos = this_line_vpos;
13595 it.current_y = this_line_y;
13596 it.glyph_row = MATRIX_ROW (w->desired_matrix, this_line_vpos);
13597 display_line (&it);
13599 /* If line contains point, is not continued,
13600 and ends at same distance from eob as before, we win. */
13601 if (w->cursor.vpos >= 0
13602 /* Line is not continued, otherwise this_line_start_pos
13603 would have been set to 0 in display_line. */
13604 && CHARPOS (this_line_start_pos)
13605 /* Line ends as before. */
13606 && CHARPOS (this_line_end_pos) == CHARPOS (tlendpos)
13607 /* Line has same height as before. Otherwise other lines
13608 would have to be shifted up or down. */
13609 && this_line_pixel_height == line_height_before)
13611 /* If this is not the window's last line, we must adjust
13612 the charstarts of the lines below. */
13613 if (it.current_y < it.last_visible_y)
13615 struct glyph_row *row
13616 = MATRIX_ROW (w->current_matrix, this_line_vpos + 1);
13617 ptrdiff_t delta, delta_bytes;
13619 /* We used to distinguish between two cases here,
13620 conditioned by Z - CHARPOS (tlendpos) == ZV, for
13621 when the line ends in a newline or the end of the
13622 buffer's accessible portion. But both cases did
13623 the same, so they were collapsed. */
13624 delta = (Z
13625 - CHARPOS (tlendpos)
13626 - MATRIX_ROW_START_CHARPOS (row));
13627 delta_bytes = (Z_BYTE
13628 - BYTEPOS (tlendpos)
13629 - MATRIX_ROW_START_BYTEPOS (row));
13631 increment_matrix_positions (w->current_matrix,
13632 this_line_vpos + 1,
13633 w->current_matrix->nrows,
13634 delta, delta_bytes);
13637 /* If this row displays text now but previously didn't,
13638 or vice versa, w->window_end_vpos may have to be
13639 adjusted. */
13640 if (MATRIX_ROW_DISPLAYS_TEXT_P (it.glyph_row - 1))
13642 if (w->window_end_vpos < this_line_vpos)
13643 w->window_end_vpos = this_line_vpos;
13645 else if (w->window_end_vpos == this_line_vpos
13646 && this_line_vpos > 0)
13647 w->window_end_vpos = this_line_vpos - 1;
13648 w->window_end_valid = 0;
13650 /* Update hint: No need to try to scroll in update_window. */
13651 w->desired_matrix->no_scrolling_p = 1;
13653 #ifdef GLYPH_DEBUG
13654 *w->desired_matrix->method = 0;
13655 debug_method_add (w, "optimization 1");
13656 #endif
13657 #ifdef HAVE_WINDOW_SYSTEM
13658 update_window_fringes (w, 0);
13659 #endif
13660 goto update;
13662 else
13663 goto cancel;
13665 else if (/* Cursor position hasn't changed. */
13666 PT == w->last_point
13667 /* Make sure the cursor was last displayed
13668 in this window. Otherwise we have to reposition it. */
13670 /* PXW: Must be converted to pixels, probably. */
13671 && 0 <= w->cursor.vpos
13672 && w->cursor.vpos < WINDOW_TOTAL_LINES (w))
13674 if (!must_finish)
13676 do_pending_window_change (true);
13677 /* If selected_window changed, redisplay again. */
13678 if (WINDOWP (selected_window)
13679 && (w = XWINDOW (selected_window)) != sw)
13680 goto retry;
13682 /* We used to always goto end_of_redisplay here, but this
13683 isn't enough if we have a blinking cursor. */
13684 if (w->cursor_off_p == w->last_cursor_off_p)
13685 goto end_of_redisplay;
13687 goto update;
13689 /* If highlighting the region, or if the cursor is in the echo area,
13690 then we can't just move the cursor. */
13691 else if (NILP (Vshow_trailing_whitespace)
13692 && !cursor_in_echo_area)
13694 struct it it;
13695 struct glyph_row *row;
13697 /* Skip from tlbufpos to PT and see where it is. Note that
13698 PT may be in invisible text. If so, we will end at the
13699 next visible position. */
13700 init_iterator (&it, w, CHARPOS (tlbufpos), BYTEPOS (tlbufpos),
13701 NULL, DEFAULT_FACE_ID);
13702 it.current_x = this_line_start_x;
13703 it.current_y = this_line_y;
13704 it.vpos = this_line_vpos;
13706 /* The call to move_it_to stops in front of PT, but
13707 moves over before-strings. */
13708 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
13710 if (it.vpos == this_line_vpos
13711 && (row = MATRIX_ROW (w->current_matrix, this_line_vpos),
13712 row->enabled_p))
13714 eassert (this_line_vpos == it.vpos);
13715 eassert (this_line_y == it.current_y);
13716 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
13717 #ifdef GLYPH_DEBUG
13718 *w->desired_matrix->method = 0;
13719 debug_method_add (w, "optimization 3");
13720 #endif
13721 goto update;
13723 else
13724 goto cancel;
13727 cancel:
13728 /* Text changed drastically or point moved off of line. */
13729 SET_MATRIX_ROW_ENABLED_P (w->desired_matrix, this_line_vpos, false);
13732 CHARPOS (this_line_start_pos) = 0;
13733 ++clear_face_cache_count;
13734 #ifdef HAVE_WINDOW_SYSTEM
13735 ++clear_image_cache_count;
13736 #endif
13738 /* Build desired matrices, and update the display. If
13739 consider_all_windows_p is non-zero, do it for all windows on all
13740 frames. Otherwise do it for selected_window, only. */
13742 if (consider_all_windows_p)
13744 FOR_EACH_FRAME (tail, frame)
13745 XFRAME (frame)->updated_p = 0;
13747 propagate_buffer_redisplay ();
13749 FOR_EACH_FRAME (tail, frame)
13751 struct frame *f = XFRAME (frame);
13753 /* We don't have to do anything for unselected terminal
13754 frames. */
13755 if ((FRAME_TERMCAP_P (f) || FRAME_MSDOS_P (f))
13756 && !EQ (FRAME_TTY (f)->top_frame, frame))
13757 continue;
13759 retry_frame:
13761 #if defined (HAVE_WINDOW_SYSTEM) && !defined (USE_GTK) && !defined (HAVE_NS)
13762 /* Redisplay internal tool bar if this is the first time so we
13763 can adjust the frame height right now, if necessary. */
13764 if (!f->tool_bar_redisplayed_once)
13766 if (redisplay_tool_bar (f))
13767 adjust_frame_glyphs (f);
13768 f->tool_bar_redisplayed_once = true;
13770 #endif
13772 if (FRAME_WINDOW_P (f) || FRAME_TERMCAP_P (f) || f == sf)
13774 bool gcscrollbars
13775 /* Only GC scrollbars when we redisplay the whole frame. */
13776 = f->redisplay || !REDISPLAY_SOME_P ();
13777 /* Mark all the scroll bars to be removed; we'll redeem
13778 the ones we want when we redisplay their windows. */
13779 if (gcscrollbars && FRAME_TERMINAL (f)->condemn_scroll_bars_hook)
13780 FRAME_TERMINAL (f)->condemn_scroll_bars_hook (f);
13782 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
13783 redisplay_windows (FRAME_ROOT_WINDOW (f));
13784 /* Remember that the invisible frames need to be redisplayed next
13785 time they're visible. */
13786 else if (!REDISPLAY_SOME_P ())
13787 f->redisplay = true;
13789 /* The X error handler may have deleted that frame. */
13790 if (!FRAME_LIVE_P (f))
13791 continue;
13793 /* Any scroll bars which redisplay_windows should have
13794 nuked should now go away. */
13795 if (gcscrollbars && FRAME_TERMINAL (f)->judge_scroll_bars_hook)
13796 FRAME_TERMINAL (f)->judge_scroll_bars_hook (f);
13798 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
13800 /* If fonts changed on visible frame, display again. */
13801 if (f->fonts_changed)
13803 adjust_frame_glyphs (f);
13804 f->fonts_changed = false;
13805 goto retry_frame;
13808 /* See if we have to hscroll. */
13809 if (!f->already_hscrolled_p)
13811 f->already_hscrolled_p = true;
13812 if (hscroll_windows (f->root_window))
13813 goto retry_frame;
13816 /* Prevent various kinds of signals during display
13817 update. stdio is not robust about handling
13818 signals, which can cause an apparent I/O error. */
13819 if (interrupt_input)
13820 unrequest_sigio ();
13821 STOP_POLLING;
13823 pending |= update_frame (f, false, false);
13824 f->cursor_type_changed = false;
13825 f->updated_p = true;
13830 eassert (EQ (XFRAME (selected_frame)->selected_window, selected_window));
13832 if (!pending)
13834 /* Do the mark_window_display_accurate after all windows have
13835 been redisplayed because this call resets flags in buffers
13836 which are needed for proper redisplay. */
13837 FOR_EACH_FRAME (tail, frame)
13839 struct frame *f = XFRAME (frame);
13840 if (f->updated_p)
13842 f->redisplay = false;
13843 mark_window_display_accurate (f->root_window, 1);
13844 if (FRAME_TERMINAL (f)->frame_up_to_date_hook)
13845 FRAME_TERMINAL (f)->frame_up_to_date_hook (f);
13850 else if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
13852 Lisp_Object mini_window = FRAME_MINIBUF_WINDOW (sf);
13853 struct frame *mini_frame;
13855 displayed_buffer = XBUFFER (XWINDOW (selected_window)->contents);
13856 /* Use list_of_error, not Qerror, so that
13857 we catch only errors and don't run the debugger. */
13858 internal_condition_case_1 (redisplay_window_1, selected_window,
13859 list_of_error,
13860 redisplay_window_error);
13861 if (update_miniwindow_p)
13862 internal_condition_case_1 (redisplay_window_1, mini_window,
13863 list_of_error,
13864 redisplay_window_error);
13866 /* Compare desired and current matrices, perform output. */
13868 update:
13869 /* If fonts changed, display again. */
13870 if (sf->fonts_changed)
13871 goto retry;
13873 /* Prevent various kinds of signals during display update.
13874 stdio is not robust about handling signals,
13875 which can cause an apparent I/O error. */
13876 if (interrupt_input)
13877 unrequest_sigio ();
13878 STOP_POLLING;
13880 if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
13882 if (hscroll_windows (selected_window))
13883 goto retry;
13885 XWINDOW (selected_window)->must_be_updated_p = true;
13886 pending = update_frame (sf, false, false);
13887 sf->cursor_type_changed = false;
13890 /* We may have called echo_area_display at the top of this
13891 function. If the echo area is on another frame, that may
13892 have put text on a frame other than the selected one, so the
13893 above call to update_frame would not have caught it. Catch
13894 it here. */
13895 mini_window = FRAME_MINIBUF_WINDOW (sf);
13896 mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
13898 if (mini_frame != sf && FRAME_WINDOW_P (mini_frame))
13900 XWINDOW (mini_window)->must_be_updated_p = true;
13901 pending |= update_frame (mini_frame, false, false);
13902 mini_frame->cursor_type_changed = false;
13903 if (!pending && hscroll_windows (mini_window))
13904 goto retry;
13908 /* If display was paused because of pending input, make sure we do a
13909 thorough update the next time. */
13910 if (pending)
13912 /* Prevent the optimization at the beginning of
13913 redisplay_internal that tries a single-line update of the
13914 line containing the cursor in the selected window. */
13915 CHARPOS (this_line_start_pos) = 0;
13917 /* Let the overlay arrow be updated the next time. */
13918 update_overlay_arrows (0);
13920 /* If we pause after scrolling, some rows in the current
13921 matrices of some windows are not valid. */
13922 if (!WINDOW_FULL_WIDTH_P (w)
13923 && !FRAME_WINDOW_P (XFRAME (w->frame)))
13924 update_mode_lines = 36;
13926 else
13928 if (!consider_all_windows_p)
13930 /* This has already been done above if
13931 consider_all_windows_p is set. */
13932 if (XBUFFER (w->contents)->text->redisplay
13933 && buffer_window_count (XBUFFER (w->contents)) > 1)
13934 /* This can happen if b->text->redisplay was set during
13935 jit-lock. */
13936 propagate_buffer_redisplay ();
13937 mark_window_display_accurate_1 (w, 1);
13939 /* Say overlay arrows are up to date. */
13940 update_overlay_arrows (1);
13942 if (FRAME_TERMINAL (sf)->frame_up_to_date_hook != 0)
13943 FRAME_TERMINAL (sf)->frame_up_to_date_hook (sf);
13946 update_mode_lines = 0;
13947 windows_or_buffers_changed = 0;
13950 /* Start SIGIO interrupts coming again. Having them off during the
13951 code above makes it less likely one will discard output, but not
13952 impossible, since there might be stuff in the system buffer here.
13953 But it is much hairier to try to do anything about that. */
13954 if (interrupt_input)
13955 request_sigio ();
13956 RESUME_POLLING;
13958 /* If a frame has become visible which was not before, redisplay
13959 again, so that we display it. Expose events for such a frame
13960 (which it gets when becoming visible) don't call the parts of
13961 redisplay constructing glyphs, so simply exposing a frame won't
13962 display anything in this case. So, we have to display these
13963 frames here explicitly. */
13964 if (!pending)
13966 int new_count = 0;
13968 FOR_EACH_FRAME (tail, frame)
13970 if (XFRAME (frame)->visible)
13971 new_count++;
13974 if (new_count != number_of_visible_frames)
13975 windows_or_buffers_changed = 52;
13978 /* Change frame size now if a change is pending. */
13979 do_pending_window_change (true);
13981 /* If we just did a pending size change, or have additional
13982 visible frames, or selected_window changed, redisplay again. */
13983 if ((windows_or_buffers_changed && !pending)
13984 || (WINDOWP (selected_window) && (w = XWINDOW (selected_window)) != sw))
13985 goto retry;
13987 /* Clear the face and image caches.
13989 We used to do this only if consider_all_windows_p. But the cache
13990 needs to be cleared if a timer creates images in the current
13991 buffer (e.g. the test case in Bug#6230). */
13993 if (clear_face_cache_count > CLEAR_FACE_CACHE_COUNT)
13995 clear_face_cache (false);
13996 clear_face_cache_count = 0;
13999 #ifdef HAVE_WINDOW_SYSTEM
14000 if (clear_image_cache_count > CLEAR_IMAGE_CACHE_COUNT)
14002 clear_image_caches (Qnil);
14003 clear_image_cache_count = 0;
14005 #endif /* HAVE_WINDOW_SYSTEM */
14007 end_of_redisplay:
14008 #ifdef HAVE_NS
14009 ns_set_doc_edited ();
14010 #endif
14011 if (interrupt_input && interrupts_deferred)
14012 request_sigio ();
14014 unbind_to (count, Qnil);
14015 RESUME_POLLING;
14019 /* Redisplay, but leave alone any recent echo area message unless
14020 another message has been requested in its place.
14022 This is useful in situations where you need to redisplay but no
14023 user action has occurred, making it inappropriate for the message
14024 area to be cleared. See tracking_off and
14025 wait_reading_process_output for examples of these situations.
14027 FROM_WHERE is an integer saying from where this function was
14028 called. This is useful for debugging. */
14030 void
14031 redisplay_preserve_echo_area (int from_where)
14033 TRACE ((stderr, "redisplay_preserve_echo_area (%d)\n", from_where));
14035 if (!NILP (echo_area_buffer[1]))
14037 /* We have a previously displayed message, but no current
14038 message. Redisplay the previous message. */
14039 display_last_displayed_message_p = true;
14040 redisplay_internal ();
14041 display_last_displayed_message_p = false;
14043 else
14044 redisplay_internal ();
14046 flush_frame (SELECTED_FRAME ());
14050 /* Function registered with record_unwind_protect in redisplay_internal. */
14052 static void
14053 unwind_redisplay (void)
14055 redisplaying_p = 0;
14059 /* Mark the display of leaf window W as accurate or inaccurate.
14060 If ACCURATE_P is non-zero mark display of W as accurate. If
14061 ACCURATE_P is zero, arrange for W to be redisplayed the next
14062 time redisplay_internal is called. */
14064 static void
14065 mark_window_display_accurate_1 (struct window *w, int accurate_p)
14067 struct buffer *b = XBUFFER (w->contents);
14069 w->last_modified = accurate_p ? BUF_MODIFF (b) : 0;
14070 w->last_overlay_modified = accurate_p ? BUF_OVERLAY_MODIFF (b) : 0;
14071 w->last_had_star = BUF_MODIFF (b) > BUF_SAVE_MODIFF (b);
14073 if (accurate_p)
14075 b->clip_changed = false;
14076 b->prevent_redisplay_optimizations_p = false;
14077 eassert (buffer_window_count (b) > 0);
14078 /* Resetting b->text->redisplay is problematic!
14079 In order to make it safer to do it here, redisplay_internal must
14080 have copied all b->text->redisplay to their respective windows. */
14081 b->text->redisplay = false;
14083 BUF_UNCHANGED_MODIFIED (b) = BUF_MODIFF (b);
14084 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = BUF_OVERLAY_MODIFF (b);
14085 BUF_BEG_UNCHANGED (b) = BUF_GPT (b) - BUF_BEG (b);
14086 BUF_END_UNCHANGED (b) = BUF_Z (b) - BUF_GPT (b);
14088 w->current_matrix->buffer = b;
14089 w->current_matrix->begv = BUF_BEGV (b);
14090 w->current_matrix->zv = BUF_ZV (b);
14092 w->last_cursor_vpos = w->cursor.vpos;
14093 w->last_cursor_off_p = w->cursor_off_p;
14095 if (w == XWINDOW (selected_window))
14096 w->last_point = BUF_PT (b);
14097 else
14098 w->last_point = marker_position (w->pointm);
14100 w->window_end_valid = true;
14101 w->update_mode_line = false;
14104 w->redisplay = !accurate_p;
14108 /* Mark the display of windows in the window tree rooted at WINDOW as
14109 accurate or inaccurate. If ACCURATE_P is non-zero mark display of
14110 windows as accurate. If ACCURATE_P is zero, arrange for windows to
14111 be redisplayed the next time redisplay_internal is called. */
14113 void
14114 mark_window_display_accurate (Lisp_Object window, int accurate_p)
14116 struct window *w;
14118 for (; !NILP (window); window = w->next)
14120 w = XWINDOW (window);
14121 if (WINDOWP (w->contents))
14122 mark_window_display_accurate (w->contents, accurate_p);
14123 else
14124 mark_window_display_accurate_1 (w, accurate_p);
14127 if (accurate_p)
14128 update_overlay_arrows (1);
14129 else
14130 /* Force a thorough redisplay the next time by setting
14131 last_arrow_position and last_arrow_string to t, which is
14132 unequal to any useful value of Voverlay_arrow_... */
14133 update_overlay_arrows (-1);
14137 /* Return value in display table DP (Lisp_Char_Table *) for character
14138 C. Since a display table doesn't have any parent, we don't have to
14139 follow parent. Do not call this function directly but use the
14140 macro DISP_CHAR_VECTOR. */
14142 Lisp_Object
14143 disp_char_vector (struct Lisp_Char_Table *dp, int c)
14145 Lisp_Object val;
14147 if (ASCII_CHAR_P (c))
14149 val = dp->ascii;
14150 if (SUB_CHAR_TABLE_P (val))
14151 val = XSUB_CHAR_TABLE (val)->contents[c];
14153 else
14155 Lisp_Object table;
14157 XSETCHAR_TABLE (table, dp);
14158 val = char_table_ref (table, c);
14160 if (NILP (val))
14161 val = dp->defalt;
14162 return val;
14167 /***********************************************************************
14168 Window Redisplay
14169 ***********************************************************************/
14171 /* Redisplay all leaf windows in the window tree rooted at WINDOW. */
14173 static void
14174 redisplay_windows (Lisp_Object window)
14176 while (!NILP (window))
14178 struct window *w = XWINDOW (window);
14180 if (WINDOWP (w->contents))
14181 redisplay_windows (w->contents);
14182 else if (BUFFERP (w->contents))
14184 displayed_buffer = XBUFFER (w->contents);
14185 /* Use list_of_error, not Qerror, so that
14186 we catch only errors and don't run the debugger. */
14187 internal_condition_case_1 (redisplay_window_0, window,
14188 list_of_error,
14189 redisplay_window_error);
14192 window = w->next;
14196 static Lisp_Object
14197 redisplay_window_error (Lisp_Object ignore)
14199 displayed_buffer->display_error_modiff = BUF_MODIFF (displayed_buffer);
14200 return Qnil;
14203 static Lisp_Object
14204 redisplay_window_0 (Lisp_Object window)
14206 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
14207 redisplay_window (window, false);
14208 return Qnil;
14211 static Lisp_Object
14212 redisplay_window_1 (Lisp_Object window)
14214 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
14215 redisplay_window (window, true);
14216 return Qnil;
14220 /* Set cursor position of W. PT is assumed to be displayed in ROW.
14221 DELTA and DELTA_BYTES are the numbers of characters and bytes by
14222 which positions recorded in ROW differ from current buffer
14223 positions.
14225 Return 0 if cursor is not on this row, 1 otherwise. */
14227 static int
14228 set_cursor_from_row (struct window *w, struct glyph_row *row,
14229 struct glyph_matrix *matrix,
14230 ptrdiff_t delta, ptrdiff_t delta_bytes,
14231 int dy, int dvpos)
14233 struct glyph *glyph = row->glyphs[TEXT_AREA];
14234 struct glyph *end = glyph + row->used[TEXT_AREA];
14235 struct glyph *cursor = NULL;
14236 /* The last known character position in row. */
14237 ptrdiff_t last_pos = MATRIX_ROW_START_CHARPOS (row) + delta;
14238 int x = row->x;
14239 ptrdiff_t pt_old = PT - delta;
14240 ptrdiff_t pos_before = MATRIX_ROW_START_CHARPOS (row) + delta;
14241 ptrdiff_t pos_after = MATRIX_ROW_END_CHARPOS (row) + delta;
14242 struct glyph *glyph_before = glyph - 1, *glyph_after = end;
14243 /* A glyph beyond the edge of TEXT_AREA which we should never
14244 touch. */
14245 struct glyph *glyphs_end = end;
14246 /* Non-zero means we've found a match for cursor position, but that
14247 glyph has the avoid_cursor_p flag set. */
14248 int match_with_avoid_cursor = 0;
14249 /* Non-zero means we've seen at least one glyph that came from a
14250 display string. */
14251 int string_seen = 0;
14252 /* Largest and smallest buffer positions seen so far during scan of
14253 glyph row. */
14254 ptrdiff_t bpos_max = pos_before;
14255 ptrdiff_t bpos_min = pos_after;
14256 /* Last buffer position covered by an overlay string with an integer
14257 `cursor' property. */
14258 ptrdiff_t bpos_covered = 0;
14259 /* Non-zero means the display string on which to display the cursor
14260 comes from a text property, not from an overlay. */
14261 int string_from_text_prop = 0;
14263 /* Don't even try doing anything if called for a mode-line or
14264 header-line row, since the rest of the code isn't prepared to
14265 deal with such calamities. */
14266 eassert (!row->mode_line_p);
14267 if (row->mode_line_p)
14268 return 0;
14270 /* Skip over glyphs not having an object at the start and the end of
14271 the row. These are special glyphs like truncation marks on
14272 terminal frames. */
14273 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
14275 if (!row->reversed_p)
14277 while (glyph < end
14278 && NILP (glyph->object)
14279 && glyph->charpos < 0)
14281 x += glyph->pixel_width;
14282 ++glyph;
14284 while (end > glyph
14285 && NILP ((end - 1)->object)
14286 /* CHARPOS is zero for blanks and stretch glyphs
14287 inserted by extend_face_to_end_of_line. */
14288 && (end - 1)->charpos <= 0)
14289 --end;
14290 glyph_before = glyph - 1;
14291 glyph_after = end;
14293 else
14295 struct glyph *g;
14297 /* If the glyph row is reversed, we need to process it from back
14298 to front, so swap the edge pointers. */
14299 glyphs_end = end = glyph - 1;
14300 glyph += row->used[TEXT_AREA] - 1;
14302 while (glyph > end + 1
14303 && NILP (glyph->object)
14304 && glyph->charpos < 0)
14306 --glyph;
14307 x -= glyph->pixel_width;
14309 if (NILP (glyph->object) && glyph->charpos < 0)
14310 --glyph;
14311 /* By default, in reversed rows we put the cursor on the
14312 rightmost (first in the reading order) glyph. */
14313 for (g = end + 1; g < glyph; g++)
14314 x += g->pixel_width;
14315 while (end < glyph
14316 && NILP ((end + 1)->object)
14317 && (end + 1)->charpos <= 0)
14318 ++end;
14319 glyph_before = glyph + 1;
14320 glyph_after = end;
14323 else if (row->reversed_p)
14325 /* In R2L rows that don't display text, put the cursor on the
14326 rightmost glyph. Case in point: an empty last line that is
14327 part of an R2L paragraph. */
14328 cursor = end - 1;
14329 /* Avoid placing the cursor on the last glyph of the row, where
14330 on terminal frames we hold the vertical border between
14331 adjacent windows. */
14332 if (!FRAME_WINDOW_P (WINDOW_XFRAME (w))
14333 && !WINDOW_RIGHTMOST_P (w)
14334 && cursor == row->glyphs[LAST_AREA] - 1)
14335 cursor--;
14336 x = -1; /* will be computed below, at label compute_x */
14339 /* Step 1: Try to find the glyph whose character position
14340 corresponds to point. If that's not possible, find 2 glyphs
14341 whose character positions are the closest to point, one before
14342 point, the other after it. */
14343 if (!row->reversed_p)
14344 while (/* not marched to end of glyph row */
14345 glyph < end
14346 /* glyph was not inserted by redisplay for internal purposes */
14347 && !NILP (glyph->object))
14349 if (BUFFERP (glyph->object))
14351 ptrdiff_t dpos = glyph->charpos - pt_old;
14353 if (glyph->charpos > bpos_max)
14354 bpos_max = glyph->charpos;
14355 if (glyph->charpos < bpos_min)
14356 bpos_min = glyph->charpos;
14357 if (!glyph->avoid_cursor_p)
14359 /* If we hit point, we've found the glyph on which to
14360 display the cursor. */
14361 if (dpos == 0)
14363 match_with_avoid_cursor = 0;
14364 break;
14366 /* See if we've found a better approximation to
14367 POS_BEFORE or to POS_AFTER. */
14368 if (0 > dpos && dpos > pos_before - pt_old)
14370 pos_before = glyph->charpos;
14371 glyph_before = glyph;
14373 else if (0 < dpos && dpos < pos_after - pt_old)
14375 pos_after = glyph->charpos;
14376 glyph_after = glyph;
14379 else if (dpos == 0)
14380 match_with_avoid_cursor = 1;
14382 else if (STRINGP (glyph->object))
14384 Lisp_Object chprop;
14385 ptrdiff_t glyph_pos = glyph->charpos;
14387 chprop = Fget_char_property (make_number (glyph_pos), Qcursor,
14388 glyph->object);
14389 if (!NILP (chprop))
14391 /* If the string came from a `display' text property,
14392 look up the buffer position of that property and
14393 use that position to update bpos_max, as if we
14394 actually saw such a position in one of the row's
14395 glyphs. This helps with supporting integer values
14396 of `cursor' property on the display string in
14397 situations where most or all of the row's buffer
14398 text is completely covered by display properties,
14399 so that no glyph with valid buffer positions is
14400 ever seen in the row. */
14401 ptrdiff_t prop_pos =
14402 string_buffer_position_lim (glyph->object, pos_before,
14403 pos_after, 0);
14405 if (prop_pos >= pos_before)
14406 bpos_max = prop_pos;
14408 if (INTEGERP (chprop))
14410 bpos_covered = bpos_max + XINT (chprop);
14411 /* If the `cursor' property covers buffer positions up
14412 to and including point, we should display cursor on
14413 this glyph. Note that, if a `cursor' property on one
14414 of the string's characters has an integer value, we
14415 will break out of the loop below _before_ we get to
14416 the position match above. IOW, integer values of
14417 the `cursor' property override the "exact match for
14418 point" strategy of positioning the cursor. */
14419 /* Implementation note: bpos_max == pt_old when, e.g.,
14420 we are in an empty line, where bpos_max is set to
14421 MATRIX_ROW_START_CHARPOS, see above. */
14422 if (bpos_max <= pt_old && bpos_covered >= pt_old)
14424 cursor = glyph;
14425 break;
14429 string_seen = 1;
14431 x += glyph->pixel_width;
14432 ++glyph;
14434 else if (glyph > end) /* row is reversed */
14435 while (!NILP (glyph->object))
14437 if (BUFFERP (glyph->object))
14439 ptrdiff_t dpos = glyph->charpos - pt_old;
14441 if (glyph->charpos > bpos_max)
14442 bpos_max = glyph->charpos;
14443 if (glyph->charpos < bpos_min)
14444 bpos_min = glyph->charpos;
14445 if (!glyph->avoid_cursor_p)
14447 if (dpos == 0)
14449 match_with_avoid_cursor = 0;
14450 break;
14452 if (0 > dpos && dpos > pos_before - pt_old)
14454 pos_before = glyph->charpos;
14455 glyph_before = glyph;
14457 else if (0 < dpos && dpos < pos_after - pt_old)
14459 pos_after = glyph->charpos;
14460 glyph_after = glyph;
14463 else if (dpos == 0)
14464 match_with_avoid_cursor = 1;
14466 else if (STRINGP (glyph->object))
14468 Lisp_Object chprop;
14469 ptrdiff_t glyph_pos = glyph->charpos;
14471 chprop = Fget_char_property (make_number (glyph_pos), Qcursor,
14472 glyph->object);
14473 if (!NILP (chprop))
14475 ptrdiff_t prop_pos =
14476 string_buffer_position_lim (glyph->object, pos_before,
14477 pos_after, 0);
14479 if (prop_pos >= pos_before)
14480 bpos_max = prop_pos;
14482 if (INTEGERP (chprop))
14484 bpos_covered = bpos_max + XINT (chprop);
14485 /* If the `cursor' property covers buffer positions up
14486 to and including point, we should display cursor on
14487 this glyph. */
14488 if (bpos_max <= pt_old && bpos_covered >= pt_old)
14490 cursor = glyph;
14491 break;
14494 string_seen = 1;
14496 --glyph;
14497 if (glyph == glyphs_end) /* don't dereference outside TEXT_AREA */
14499 x--; /* can't use any pixel_width */
14500 break;
14502 x -= glyph->pixel_width;
14505 /* Step 2: If we didn't find an exact match for point, we need to
14506 look for a proper place to put the cursor among glyphs between
14507 GLYPH_BEFORE and GLYPH_AFTER. */
14508 if (!((row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end)
14509 && BUFFERP (glyph->object) && glyph->charpos == pt_old)
14510 && !(bpos_max <= pt_old && pt_old <= bpos_covered))
14512 /* An empty line has a single glyph whose OBJECT is nil and
14513 whose CHARPOS is the position of a newline on that line.
14514 Note that on a TTY, there are more glyphs after that, which
14515 were produced by extend_face_to_end_of_line, but their
14516 CHARPOS is zero or negative. */
14517 int empty_line_p =
14518 (row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end)
14519 && NILP (glyph->object) && glyph->charpos > 0
14520 /* On a TTY, continued and truncated rows also have a glyph at
14521 their end whose OBJECT is nil and whose CHARPOS is
14522 positive (the continuation and truncation glyphs), but such
14523 rows are obviously not "empty". */
14524 && !(row->continued_p || row->truncated_on_right_p);
14526 if (row->ends_in_ellipsis_p && pos_after == last_pos)
14528 ptrdiff_t ellipsis_pos;
14530 /* Scan back over the ellipsis glyphs. */
14531 if (!row->reversed_p)
14533 ellipsis_pos = (glyph - 1)->charpos;
14534 while (glyph > row->glyphs[TEXT_AREA]
14535 && (glyph - 1)->charpos == ellipsis_pos)
14536 glyph--, x -= glyph->pixel_width;
14537 /* That loop always goes one position too far, including
14538 the glyph before the ellipsis. So scan forward over
14539 that one. */
14540 x += glyph->pixel_width;
14541 glyph++;
14543 else /* row is reversed */
14545 ellipsis_pos = (glyph + 1)->charpos;
14546 while (glyph < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
14547 && (glyph + 1)->charpos == ellipsis_pos)
14548 glyph++, x += glyph->pixel_width;
14549 x -= glyph->pixel_width;
14550 glyph--;
14553 else if (match_with_avoid_cursor)
14555 cursor = glyph_after;
14556 x = -1;
14558 else if (string_seen)
14560 int incr = row->reversed_p ? -1 : +1;
14562 /* Need to find the glyph that came out of a string which is
14563 present at point. That glyph is somewhere between
14564 GLYPH_BEFORE and GLYPH_AFTER, and it came from a string
14565 positioned between POS_BEFORE and POS_AFTER in the
14566 buffer. */
14567 struct glyph *start, *stop;
14568 ptrdiff_t pos = pos_before;
14570 x = -1;
14572 /* If the row ends in a newline from a display string,
14573 reordering could have moved the glyphs belonging to the
14574 string out of the [GLYPH_BEFORE..GLYPH_AFTER] range. So
14575 in this case we extend the search to the last glyph in
14576 the row that was not inserted by redisplay. */
14577 if (row->ends_in_newline_from_string_p)
14579 glyph_after = end;
14580 pos_after = MATRIX_ROW_END_CHARPOS (row) + delta;
14583 /* GLYPH_BEFORE and GLYPH_AFTER are the glyphs that
14584 correspond to POS_BEFORE and POS_AFTER, respectively. We
14585 need START and STOP in the order that corresponds to the
14586 row's direction as given by its reversed_p flag. If the
14587 directionality of characters between POS_BEFORE and
14588 POS_AFTER is the opposite of the row's base direction,
14589 these characters will have been reordered for display,
14590 and we need to reverse START and STOP. */
14591 if (!row->reversed_p)
14593 start = min (glyph_before, glyph_after);
14594 stop = max (glyph_before, glyph_after);
14596 else
14598 start = max (glyph_before, glyph_after);
14599 stop = min (glyph_before, glyph_after);
14601 for (glyph = start + incr;
14602 row->reversed_p ? glyph > stop : glyph < stop; )
14605 /* Any glyphs that come from the buffer are here because
14606 of bidi reordering. Skip them, and only pay
14607 attention to glyphs that came from some string. */
14608 if (STRINGP (glyph->object))
14610 Lisp_Object str;
14611 ptrdiff_t tem;
14612 /* If the display property covers the newline, we
14613 need to search for it one position farther. */
14614 ptrdiff_t lim = pos_after
14615 + (pos_after == MATRIX_ROW_END_CHARPOS (row) + delta);
14617 string_from_text_prop = 0;
14618 str = glyph->object;
14619 tem = string_buffer_position_lim (str, pos, lim, 0);
14620 if (tem == 0 /* from overlay */
14621 || pos <= tem)
14623 /* If the string from which this glyph came is
14624 found in the buffer at point, or at position
14625 that is closer to point than pos_after, then
14626 we've found the glyph we've been looking for.
14627 If it comes from an overlay (tem == 0), and
14628 it has the `cursor' property on one of its
14629 glyphs, record that glyph as a candidate for
14630 displaying the cursor. (As in the
14631 unidirectional version, we will display the
14632 cursor on the last candidate we find.) */
14633 if (tem == 0
14634 || tem == pt_old
14635 || (tem - pt_old > 0 && tem < pos_after))
14637 /* The glyphs from this string could have
14638 been reordered. Find the one with the
14639 smallest string position. Or there could
14640 be a character in the string with the
14641 `cursor' property, which means display
14642 cursor on that character's glyph. */
14643 ptrdiff_t strpos = glyph->charpos;
14645 if (tem)
14647 cursor = glyph;
14648 string_from_text_prop = 1;
14650 for ( ;
14651 (row->reversed_p ? glyph > stop : glyph < stop)
14652 && EQ (glyph->object, str);
14653 glyph += incr)
14655 Lisp_Object cprop;
14656 ptrdiff_t gpos = glyph->charpos;
14658 cprop = Fget_char_property (make_number (gpos),
14659 Qcursor,
14660 glyph->object);
14661 if (!NILP (cprop))
14663 cursor = glyph;
14664 break;
14666 if (tem && glyph->charpos < strpos)
14668 strpos = glyph->charpos;
14669 cursor = glyph;
14673 if (tem == pt_old
14674 || (tem - pt_old > 0 && tem < pos_after))
14675 goto compute_x;
14677 if (tem)
14678 pos = tem + 1; /* don't find previous instances */
14680 /* This string is not what we want; skip all of the
14681 glyphs that came from it. */
14682 while ((row->reversed_p ? glyph > stop : glyph < stop)
14683 && EQ (glyph->object, str))
14684 glyph += incr;
14686 else
14687 glyph += incr;
14690 /* If we reached the end of the line, and END was from a string,
14691 the cursor is not on this line. */
14692 if (cursor == NULL
14693 && (row->reversed_p ? glyph <= end : glyph >= end)
14694 && (row->reversed_p ? end > glyphs_end : end < glyphs_end)
14695 && STRINGP (end->object)
14696 && row->continued_p)
14697 return 0;
14699 /* A truncated row may not include PT among its character positions.
14700 Setting the cursor inside the scroll margin will trigger
14701 recalculation of hscroll in hscroll_window_tree. But if a
14702 display string covers point, defer to the string-handling
14703 code below to figure this out. */
14704 else if (row->truncated_on_left_p && pt_old < bpos_min)
14706 cursor = glyph_before;
14707 x = -1;
14709 else if ((row->truncated_on_right_p && pt_old > bpos_max)
14710 /* Zero-width characters produce no glyphs. */
14711 || (!empty_line_p
14712 && (row->reversed_p
14713 ? glyph_after > glyphs_end
14714 : glyph_after < glyphs_end)))
14716 cursor = glyph_after;
14717 x = -1;
14721 compute_x:
14722 if (cursor != NULL)
14723 glyph = cursor;
14724 else if (glyph == glyphs_end
14725 && pos_before == pos_after
14726 && STRINGP ((row->reversed_p
14727 ? row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
14728 : row->glyphs[TEXT_AREA])->object))
14730 /* If all the glyphs of this row came from strings, put the
14731 cursor on the first glyph of the row. This avoids having the
14732 cursor outside of the text area in this very rare and hard
14733 use case. */
14734 glyph =
14735 row->reversed_p
14736 ? row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
14737 : row->glyphs[TEXT_AREA];
14739 if (x < 0)
14741 struct glyph *g;
14743 /* Need to compute x that corresponds to GLYPH. */
14744 for (g = row->glyphs[TEXT_AREA], x = row->x; g < glyph; g++)
14746 if (g >= row->glyphs[TEXT_AREA] + row->used[TEXT_AREA])
14747 emacs_abort ();
14748 x += g->pixel_width;
14752 /* ROW could be part of a continued line, which, under bidi
14753 reordering, might have other rows whose start and end charpos
14754 occlude point. Only set w->cursor if we found a better
14755 approximation to the cursor position than we have from previously
14756 examined candidate rows belonging to the same continued line. */
14757 if (/* We already have a candidate row. */
14758 w->cursor.vpos >= 0
14759 /* That candidate is not the row we are processing. */
14760 && MATRIX_ROW (matrix, w->cursor.vpos) != row
14761 /* Make sure cursor.vpos specifies a row whose start and end
14762 charpos occlude point, and it is valid candidate for being a
14763 cursor-row. This is because some callers of this function
14764 leave cursor.vpos at the row where the cursor was displayed
14765 during the last redisplay cycle. */
14766 && MATRIX_ROW_START_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos)) <= pt_old
14767 && pt_old <= MATRIX_ROW_END_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
14768 && cursor_row_p (MATRIX_ROW (matrix, w->cursor.vpos)))
14770 struct glyph *g1
14771 = MATRIX_ROW_GLYPH_START (matrix, w->cursor.vpos) + w->cursor.hpos;
14773 /* Don't consider glyphs that are outside TEXT_AREA. */
14774 if (!(row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end))
14775 return 0;
14776 /* Keep the candidate whose buffer position is the closest to
14777 point or has the `cursor' property. */
14778 if (/* Previous candidate is a glyph in TEXT_AREA of that row. */
14779 w->cursor.hpos >= 0
14780 && w->cursor.hpos < MATRIX_ROW_USED (matrix, w->cursor.vpos)
14781 && ((BUFFERP (g1->object)
14782 && (g1->charpos == pt_old /* An exact match always wins. */
14783 || (BUFFERP (glyph->object)
14784 && eabs (g1->charpos - pt_old)
14785 < eabs (glyph->charpos - pt_old))))
14786 /* Previous candidate is a glyph from a string that has
14787 a non-nil `cursor' property. */
14788 || (STRINGP (g1->object)
14789 && (!NILP (Fget_char_property (make_number (g1->charpos),
14790 Qcursor, g1->object))
14791 /* Previous candidate is from the same display
14792 string as this one, and the display string
14793 came from a text property. */
14794 || (EQ (g1->object, glyph->object)
14795 && string_from_text_prop)
14796 /* this candidate is from newline and its
14797 position is not an exact match */
14798 || (NILP (glyph->object)
14799 && glyph->charpos != pt_old)))))
14800 return 0;
14801 /* If this candidate gives an exact match, use that. */
14802 if (!((BUFFERP (glyph->object) && glyph->charpos == pt_old)
14803 /* If this candidate is a glyph created for the
14804 terminating newline of a line, and point is on that
14805 newline, it wins because it's an exact match. */
14806 || (!row->continued_p
14807 && NILP (glyph->object)
14808 && glyph->charpos == 0
14809 && pt_old == MATRIX_ROW_END_CHARPOS (row) - 1))
14810 /* Otherwise, keep the candidate that comes from a row
14811 spanning less buffer positions. This may win when one or
14812 both candidate positions are on glyphs that came from
14813 display strings, for which we cannot compare buffer
14814 positions. */
14815 && MATRIX_ROW_END_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
14816 - MATRIX_ROW_START_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
14817 < MATRIX_ROW_END_CHARPOS (row) - MATRIX_ROW_START_CHARPOS (row))
14818 return 0;
14820 w->cursor.hpos = glyph - row->glyphs[TEXT_AREA];
14821 w->cursor.x = x;
14822 w->cursor.vpos = MATRIX_ROW_VPOS (row, matrix) + dvpos;
14823 w->cursor.y = row->y + dy;
14825 if (w == XWINDOW (selected_window))
14827 if (!row->continued_p
14828 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
14829 && row->x == 0)
14831 this_line_buffer = XBUFFER (w->contents);
14833 CHARPOS (this_line_start_pos)
14834 = MATRIX_ROW_START_CHARPOS (row) + delta;
14835 BYTEPOS (this_line_start_pos)
14836 = MATRIX_ROW_START_BYTEPOS (row) + delta_bytes;
14838 CHARPOS (this_line_end_pos)
14839 = Z - (MATRIX_ROW_END_CHARPOS (row) + delta);
14840 BYTEPOS (this_line_end_pos)
14841 = Z_BYTE - (MATRIX_ROW_END_BYTEPOS (row) + delta_bytes);
14843 this_line_y = w->cursor.y;
14844 this_line_pixel_height = row->height;
14845 this_line_vpos = w->cursor.vpos;
14846 this_line_start_x = row->x;
14848 else
14849 CHARPOS (this_line_start_pos) = 0;
14852 return 1;
14856 /* Run window scroll functions, if any, for WINDOW with new window
14857 start STARTP. Sets the window start of WINDOW to that position.
14859 We assume that the window's buffer is really current. */
14861 static struct text_pos
14862 run_window_scroll_functions (Lisp_Object window, struct text_pos startp)
14864 struct window *w = XWINDOW (window);
14865 SET_MARKER_FROM_TEXT_POS (w->start, startp);
14867 eassert (current_buffer == XBUFFER (w->contents));
14869 if (!NILP (Vwindow_scroll_functions))
14871 run_hook_with_args_2 (Qwindow_scroll_functions, window,
14872 make_number (CHARPOS (startp)));
14873 SET_TEXT_POS_FROM_MARKER (startp, w->start);
14874 /* In case the hook functions switch buffers. */
14875 set_buffer_internal (XBUFFER (w->contents));
14878 return startp;
14882 /* Make sure the line containing the cursor is fully visible.
14883 A value of 1 means there is nothing to be done.
14884 (Either the line is fully visible, or it cannot be made so,
14885 or we cannot tell.)
14887 If FORCE_P is non-zero, return 0 even if partial visible cursor row
14888 is higher than window.
14890 If CURRENT_MATRIX_P is non-zero, use the information from the
14891 window's current glyph matrix; otherwise use the desired glyph
14892 matrix.
14894 A value of 0 means the caller should do scrolling
14895 as if point had gone off the screen. */
14897 static int
14898 cursor_row_fully_visible_p (struct window *w, int force_p, int current_matrix_p)
14900 struct glyph_matrix *matrix;
14901 struct glyph_row *row;
14902 int window_height;
14904 if (!make_cursor_line_fully_visible_p)
14905 return 1;
14907 /* It's not always possible to find the cursor, e.g, when a window
14908 is full of overlay strings. Don't do anything in that case. */
14909 if (w->cursor.vpos < 0)
14910 return 1;
14912 matrix = current_matrix_p ? w->current_matrix : w->desired_matrix;
14913 row = MATRIX_ROW (matrix, w->cursor.vpos);
14915 /* If the cursor row is not partially visible, there's nothing to do. */
14916 if (!MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row))
14917 return 1;
14919 /* If the row the cursor is in is taller than the window's height,
14920 it's not clear what to do, so do nothing. */
14921 window_height = window_box_height (w);
14922 if (row->height >= window_height)
14924 if (!force_p || MINI_WINDOW_P (w)
14925 || w->vscroll || w->cursor.vpos == 0)
14926 return 1;
14928 return 0;
14932 /* Try scrolling PT into view in window WINDOW. JUST_THIS_ONE_P
14933 non-zero means only WINDOW is redisplayed in redisplay_internal.
14934 TEMP_SCROLL_STEP has the same meaning as emacs_scroll_step, and is used
14935 in redisplay_window to bring a partially visible line into view in
14936 the case that only the cursor has moved.
14938 LAST_LINE_MISFIT should be nonzero if we're scrolling because the
14939 last screen line's vertical height extends past the end of the screen.
14941 Value is
14943 1 if scrolling succeeded
14945 0 if scrolling didn't find point.
14947 -1 if new fonts have been loaded so that we must interrupt
14948 redisplay, adjust glyph matrices, and try again. */
14950 enum
14952 SCROLLING_SUCCESS,
14953 SCROLLING_FAILED,
14954 SCROLLING_NEED_LARGER_MATRICES
14957 /* If scroll-conservatively is more than this, never recenter.
14959 If you change this, don't forget to update the doc string of
14960 `scroll-conservatively' and the Emacs manual. */
14961 #define SCROLL_LIMIT 100
14963 static int
14964 try_scrolling (Lisp_Object window, int just_this_one_p,
14965 ptrdiff_t arg_scroll_conservatively, ptrdiff_t scroll_step,
14966 int temp_scroll_step, int last_line_misfit)
14968 struct window *w = XWINDOW (window);
14969 struct frame *f = XFRAME (w->frame);
14970 struct text_pos pos, startp;
14971 struct it it;
14972 int this_scroll_margin, scroll_max, rc, height;
14973 int dy = 0, amount_to_scroll = 0, scroll_down_p = 0;
14974 int extra_scroll_margin_lines = last_line_misfit ? 1 : 0;
14975 Lisp_Object aggressive;
14976 /* We will never try scrolling more than this number of lines. */
14977 int scroll_limit = SCROLL_LIMIT;
14978 int frame_line_height = default_line_pixel_height (w);
14979 int window_total_lines
14980 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
14982 #ifdef GLYPH_DEBUG
14983 debug_method_add (w, "try_scrolling");
14984 #endif
14986 SET_TEXT_POS_FROM_MARKER (startp, w->start);
14988 /* Compute scroll margin height in pixels. We scroll when point is
14989 within this distance from the top or bottom of the window. */
14990 if (scroll_margin > 0)
14991 this_scroll_margin = min (scroll_margin, window_total_lines / 4)
14992 * frame_line_height;
14993 else
14994 this_scroll_margin = 0;
14996 /* Force arg_scroll_conservatively to have a reasonable value, to
14997 avoid scrolling too far away with slow move_it_* functions. Note
14998 that the user can supply scroll-conservatively equal to
14999 `most-positive-fixnum', which can be larger than INT_MAX. */
15000 if (arg_scroll_conservatively > scroll_limit)
15002 arg_scroll_conservatively = scroll_limit + 1;
15003 scroll_max = scroll_limit * frame_line_height;
15005 else if (scroll_step || arg_scroll_conservatively || temp_scroll_step)
15006 /* Compute how much we should try to scroll maximally to bring
15007 point into view. */
15008 scroll_max = (max (scroll_step,
15009 max (arg_scroll_conservatively, temp_scroll_step))
15010 * frame_line_height);
15011 else if (NUMBERP (BVAR (current_buffer, scroll_down_aggressively))
15012 || NUMBERP (BVAR (current_buffer, scroll_up_aggressively)))
15013 /* We're trying to scroll because of aggressive scrolling but no
15014 scroll_step is set. Choose an arbitrary one. */
15015 scroll_max = 10 * frame_line_height;
15016 else
15017 scroll_max = 0;
15019 too_near_end:
15021 /* Decide whether to scroll down. */
15022 if (PT > CHARPOS (startp))
15024 int scroll_margin_y;
15026 /* Compute the pixel ypos of the scroll margin, then move IT to
15027 either that ypos or PT, whichever comes first. */
15028 start_display (&it, w, startp);
15029 scroll_margin_y = it.last_visible_y - this_scroll_margin
15030 - frame_line_height * extra_scroll_margin_lines;
15031 move_it_to (&it, PT, -1, scroll_margin_y - 1, -1,
15032 (MOVE_TO_POS | MOVE_TO_Y));
15034 if (PT > CHARPOS (it.current.pos))
15036 int y0 = line_bottom_y (&it);
15037 /* Compute how many pixels below window bottom to stop searching
15038 for PT. This avoids costly search for PT that is far away if
15039 the user limited scrolling by a small number of lines, but
15040 always finds PT if scroll_conservatively is set to a large
15041 number, such as most-positive-fixnum. */
15042 int slack = max (scroll_max, 10 * frame_line_height);
15043 int y_to_move = it.last_visible_y + slack;
15045 /* Compute the distance from the scroll margin to PT or to
15046 the scroll limit, whichever comes first. This should
15047 include the height of the cursor line, to make that line
15048 fully visible. */
15049 move_it_to (&it, PT, -1, y_to_move,
15050 -1, MOVE_TO_POS | MOVE_TO_Y);
15051 dy = line_bottom_y (&it) - y0;
15053 if (dy > scroll_max)
15054 return SCROLLING_FAILED;
15056 if (dy > 0)
15057 scroll_down_p = 1;
15061 if (scroll_down_p)
15063 /* Point is in or below the bottom scroll margin, so move the
15064 window start down. If scrolling conservatively, move it just
15065 enough down to make point visible. If scroll_step is set,
15066 move it down by scroll_step. */
15067 if (arg_scroll_conservatively)
15068 amount_to_scroll
15069 = min (max (dy, frame_line_height),
15070 frame_line_height * arg_scroll_conservatively);
15071 else if (scroll_step || temp_scroll_step)
15072 amount_to_scroll = scroll_max;
15073 else
15075 aggressive = BVAR (current_buffer, scroll_up_aggressively);
15076 height = WINDOW_BOX_TEXT_HEIGHT (w);
15077 if (NUMBERP (aggressive))
15079 double float_amount = XFLOATINT (aggressive) * height;
15080 int aggressive_scroll = float_amount;
15081 if (aggressive_scroll == 0 && float_amount > 0)
15082 aggressive_scroll = 1;
15083 /* Don't let point enter the scroll margin near top of
15084 the window. This could happen if the value of
15085 scroll_up_aggressively is too large and there are
15086 non-zero margins, because scroll_up_aggressively
15087 means put point that fraction of window height
15088 _from_the_bottom_margin_. */
15089 if (aggressive_scroll + 2 * this_scroll_margin > height)
15090 aggressive_scroll = height - 2 * this_scroll_margin;
15091 amount_to_scroll = dy + aggressive_scroll;
15095 if (amount_to_scroll <= 0)
15096 return SCROLLING_FAILED;
15098 start_display (&it, w, startp);
15099 if (arg_scroll_conservatively <= scroll_limit)
15100 move_it_vertically (&it, amount_to_scroll);
15101 else
15103 /* Extra precision for users who set scroll-conservatively
15104 to a large number: make sure the amount we scroll
15105 the window start is never less than amount_to_scroll,
15106 which was computed as distance from window bottom to
15107 point. This matters when lines at window top and lines
15108 below window bottom have different height. */
15109 struct it it1;
15110 void *it1data = NULL;
15111 /* We use a temporary it1 because line_bottom_y can modify
15112 its argument, if it moves one line down; see there. */
15113 int start_y;
15115 SAVE_IT (it1, it, it1data);
15116 start_y = line_bottom_y (&it1);
15117 do {
15118 RESTORE_IT (&it, &it, it1data);
15119 move_it_by_lines (&it, 1);
15120 SAVE_IT (it1, it, it1data);
15121 } while (line_bottom_y (&it1) - start_y < amount_to_scroll);
15124 /* If STARTP is unchanged, move it down another screen line. */
15125 if (CHARPOS (it.current.pos) == CHARPOS (startp))
15126 move_it_by_lines (&it, 1);
15127 startp = it.current.pos;
15129 else
15131 struct text_pos scroll_margin_pos = startp;
15132 int y_offset = 0;
15134 /* See if point is inside the scroll margin at the top of the
15135 window. */
15136 if (this_scroll_margin)
15138 int y_start;
15140 start_display (&it, w, startp);
15141 y_start = it.current_y;
15142 move_it_vertically (&it, this_scroll_margin);
15143 scroll_margin_pos = it.current.pos;
15144 /* If we didn't move enough before hitting ZV, request
15145 additional amount of scroll, to move point out of the
15146 scroll margin. */
15147 if (IT_CHARPOS (it) == ZV
15148 && it.current_y - y_start < this_scroll_margin)
15149 y_offset = this_scroll_margin - (it.current_y - y_start);
15152 if (PT < CHARPOS (scroll_margin_pos))
15154 /* Point is in the scroll margin at the top of the window or
15155 above what is displayed in the window. */
15156 int y0, y_to_move;
15158 /* Compute the vertical distance from PT to the scroll
15159 margin position. Move as far as scroll_max allows, or
15160 one screenful, or 10 screen lines, whichever is largest.
15161 Give up if distance is greater than scroll_max or if we
15162 didn't reach the scroll margin position. */
15163 SET_TEXT_POS (pos, PT, PT_BYTE);
15164 start_display (&it, w, pos);
15165 y0 = it.current_y;
15166 y_to_move = max (it.last_visible_y,
15167 max (scroll_max, 10 * frame_line_height));
15168 move_it_to (&it, CHARPOS (scroll_margin_pos), 0,
15169 y_to_move, -1,
15170 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
15171 dy = it.current_y - y0;
15172 if (dy > scroll_max
15173 || IT_CHARPOS (it) < CHARPOS (scroll_margin_pos))
15174 return SCROLLING_FAILED;
15176 /* Additional scroll for when ZV was too close to point. */
15177 dy += y_offset;
15179 /* Compute new window start. */
15180 start_display (&it, w, startp);
15182 if (arg_scroll_conservatively)
15183 amount_to_scroll = max (dy, frame_line_height
15184 * max (scroll_step, temp_scroll_step));
15185 else if (scroll_step || temp_scroll_step)
15186 amount_to_scroll = scroll_max;
15187 else
15189 aggressive = BVAR (current_buffer, scroll_down_aggressively);
15190 height = WINDOW_BOX_TEXT_HEIGHT (w);
15191 if (NUMBERP (aggressive))
15193 double float_amount = XFLOATINT (aggressive) * height;
15194 int aggressive_scroll = float_amount;
15195 if (aggressive_scroll == 0 && float_amount > 0)
15196 aggressive_scroll = 1;
15197 /* Don't let point enter the scroll margin near
15198 bottom of the window, if the value of
15199 scroll_down_aggressively happens to be too
15200 large. */
15201 if (aggressive_scroll + 2 * this_scroll_margin > height)
15202 aggressive_scroll = height - 2 * this_scroll_margin;
15203 amount_to_scroll = dy + aggressive_scroll;
15207 if (amount_to_scroll <= 0)
15208 return SCROLLING_FAILED;
15210 move_it_vertically_backward (&it, amount_to_scroll);
15211 startp = it.current.pos;
15215 /* Run window scroll functions. */
15216 startp = run_window_scroll_functions (window, startp);
15218 /* Display the window. Give up if new fonts are loaded, or if point
15219 doesn't appear. */
15220 if (!try_window (window, startp, 0))
15221 rc = SCROLLING_NEED_LARGER_MATRICES;
15222 else if (w->cursor.vpos < 0)
15224 clear_glyph_matrix (w->desired_matrix);
15225 rc = SCROLLING_FAILED;
15227 else
15229 /* Maybe forget recorded base line for line number display. */
15230 if (!just_this_one_p
15231 || current_buffer->clip_changed
15232 || BEG_UNCHANGED < CHARPOS (startp))
15233 w->base_line_number = 0;
15235 /* If cursor ends up on a partially visible line,
15236 treat that as being off the bottom of the screen. */
15237 if (! cursor_row_fully_visible_p (w, extra_scroll_margin_lines <= 1, 0)
15238 /* It's possible that the cursor is on the first line of the
15239 buffer, which is partially obscured due to a vscroll
15240 (Bug#7537). In that case, avoid looping forever. */
15241 && extra_scroll_margin_lines < w->desired_matrix->nrows - 1)
15243 clear_glyph_matrix (w->desired_matrix);
15244 ++extra_scroll_margin_lines;
15245 goto too_near_end;
15247 rc = SCROLLING_SUCCESS;
15250 return rc;
15254 /* Compute a suitable window start for window W if display of W starts
15255 on a continuation line. Value is non-zero if a new window start
15256 was computed.
15258 The new window start will be computed, based on W's width, starting
15259 from the start of the continued line. It is the start of the
15260 screen line with the minimum distance from the old start W->start. */
15262 static int
15263 compute_window_start_on_continuation_line (struct window *w)
15265 struct text_pos pos, start_pos;
15266 int window_start_changed_p = 0;
15268 SET_TEXT_POS_FROM_MARKER (start_pos, w->start);
15270 /* If window start is on a continuation line... Window start may be
15271 < BEGV in case there's invisible text at the start of the
15272 buffer (M-x rmail, for example). */
15273 if (CHARPOS (start_pos) > BEGV
15274 && FETCH_BYTE (BYTEPOS (start_pos) - 1) != '\n')
15276 struct it it;
15277 struct glyph_row *row;
15279 /* Handle the case that the window start is out of range. */
15280 if (CHARPOS (start_pos) < BEGV)
15281 SET_TEXT_POS (start_pos, BEGV, BEGV_BYTE);
15282 else if (CHARPOS (start_pos) > ZV)
15283 SET_TEXT_POS (start_pos, ZV, ZV_BYTE);
15285 /* Find the start of the continued line. This should be fast
15286 because find_newline is fast (newline cache). */
15287 row = w->desired_matrix->rows + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0);
15288 init_iterator (&it, w, CHARPOS (start_pos), BYTEPOS (start_pos),
15289 row, DEFAULT_FACE_ID);
15290 reseat_at_previous_visible_line_start (&it);
15292 /* If the line start is "too far" away from the window start,
15293 say it takes too much time to compute a new window start. */
15294 if (CHARPOS (start_pos) - IT_CHARPOS (it)
15295 /* PXW: Do we need upper bounds here? */
15296 < WINDOW_TOTAL_LINES (w) * WINDOW_TOTAL_COLS (w))
15298 int min_distance, distance;
15300 /* Move forward by display lines to find the new window
15301 start. If window width was enlarged, the new start can
15302 be expected to be > the old start. If window width was
15303 decreased, the new window start will be < the old start.
15304 So, we're looking for the display line start with the
15305 minimum distance from the old window start. */
15306 pos = it.current.pos;
15307 min_distance = INFINITY;
15308 while ((distance = eabs (CHARPOS (start_pos) - IT_CHARPOS (it))),
15309 distance < min_distance)
15311 min_distance = distance;
15312 pos = it.current.pos;
15313 if (it.line_wrap == WORD_WRAP)
15315 /* Under WORD_WRAP, move_it_by_lines is likely to
15316 overshoot and stop not at the first, but the
15317 second character from the left margin. So in
15318 that case, we need a more tight control on the X
15319 coordinate of the iterator than move_it_by_lines
15320 promises in its contract. The method is to first
15321 go to the last (rightmost) visible character of a
15322 line, then move to the leftmost character on the
15323 next line in a separate call. */
15324 move_it_to (&it, ZV, it.last_visible_x, it.current_y, -1,
15325 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
15326 move_it_to (&it, ZV, 0,
15327 it.current_y + it.max_ascent + it.max_descent, -1,
15328 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
15330 else
15331 move_it_by_lines (&it, 1);
15334 /* Set the window start there. */
15335 SET_MARKER_FROM_TEXT_POS (w->start, pos);
15336 window_start_changed_p = 1;
15340 return window_start_changed_p;
15344 /* Try cursor movement in case text has not changed in window WINDOW,
15345 with window start STARTP. Value is
15347 CURSOR_MOVEMENT_SUCCESS if successful
15349 CURSOR_MOVEMENT_CANNOT_BE_USED if this method cannot be used
15351 CURSOR_MOVEMENT_MUST_SCROLL if we know we have to scroll the
15352 display. *SCROLL_STEP is set to 1, under certain circumstances, if
15353 we want to scroll as if scroll-step were set to 1. See the code.
15355 CURSOR_MOVEMENT_NEED_LARGER_MATRICES if we need larger matrices, in
15356 which case we have to abort this redisplay, and adjust matrices
15357 first. */
15359 enum
15361 CURSOR_MOVEMENT_SUCCESS,
15362 CURSOR_MOVEMENT_CANNOT_BE_USED,
15363 CURSOR_MOVEMENT_MUST_SCROLL,
15364 CURSOR_MOVEMENT_NEED_LARGER_MATRICES
15367 static int
15368 try_cursor_movement (Lisp_Object window, struct text_pos startp, int *scroll_step)
15370 struct window *w = XWINDOW (window);
15371 struct frame *f = XFRAME (w->frame);
15372 int rc = CURSOR_MOVEMENT_CANNOT_BE_USED;
15374 #ifdef GLYPH_DEBUG
15375 if (inhibit_try_cursor_movement)
15376 return rc;
15377 #endif
15379 /* Previously, there was a check for Lisp integer in the
15380 if-statement below. Now, this field is converted to
15381 ptrdiff_t, thus zero means invalid position in a buffer. */
15382 eassert (w->last_point > 0);
15383 /* Likewise there was a check whether window_end_vpos is nil or larger
15384 than the window. Now window_end_vpos is int and so never nil, but
15385 let's leave eassert to check whether it fits in the window. */
15386 eassert (!w->window_end_valid
15387 || w->window_end_vpos < w->current_matrix->nrows);
15389 /* Handle case where text has not changed, only point, and it has
15390 not moved off the frame. */
15391 if (/* Point may be in this window. */
15392 PT >= CHARPOS (startp)
15393 /* Selective display hasn't changed. */
15394 && !current_buffer->clip_changed
15395 /* Function force-mode-line-update is used to force a thorough
15396 redisplay. It sets either windows_or_buffers_changed or
15397 update_mode_lines. So don't take a shortcut here for these
15398 cases. */
15399 && !update_mode_lines
15400 && !windows_or_buffers_changed
15401 && !f->cursor_type_changed
15402 && NILP (Vshow_trailing_whitespace)
15403 /* This code is not used for mini-buffer for the sake of the case
15404 of redisplaying to replace an echo area message; since in
15405 that case the mini-buffer contents per se are usually
15406 unchanged. This code is of no real use in the mini-buffer
15407 since the handling of this_line_start_pos, etc., in redisplay
15408 handles the same cases. */
15409 && !EQ (window, minibuf_window)
15410 && (FRAME_WINDOW_P (f)
15411 || !overlay_arrow_in_current_buffer_p ()))
15413 int this_scroll_margin, top_scroll_margin;
15414 struct glyph_row *row = NULL;
15415 int frame_line_height = default_line_pixel_height (w);
15416 int window_total_lines
15417 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
15419 #ifdef GLYPH_DEBUG
15420 debug_method_add (w, "cursor movement");
15421 #endif
15423 /* Scroll if point within this distance from the top or bottom
15424 of the window. This is a pixel value. */
15425 if (scroll_margin > 0)
15427 this_scroll_margin = min (scroll_margin, window_total_lines / 4);
15428 this_scroll_margin *= frame_line_height;
15430 else
15431 this_scroll_margin = 0;
15433 top_scroll_margin = this_scroll_margin;
15434 if (WINDOW_WANTS_HEADER_LINE_P (w))
15435 top_scroll_margin += CURRENT_HEADER_LINE_HEIGHT (w);
15437 /* Start with the row the cursor was displayed during the last
15438 not paused redisplay. Give up if that row is not valid. */
15439 if (w->last_cursor_vpos < 0
15440 || w->last_cursor_vpos >= w->current_matrix->nrows)
15441 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15442 else
15444 row = MATRIX_ROW (w->current_matrix, w->last_cursor_vpos);
15445 if (row->mode_line_p)
15446 ++row;
15447 if (!row->enabled_p)
15448 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15451 if (rc == CURSOR_MOVEMENT_CANNOT_BE_USED)
15453 int scroll_p = 0, must_scroll = 0;
15454 int last_y = window_text_bottom_y (w) - this_scroll_margin;
15456 if (PT > w->last_point)
15458 /* Point has moved forward. */
15459 while (MATRIX_ROW_END_CHARPOS (row) < PT
15460 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
15462 eassert (row->enabled_p);
15463 ++row;
15466 /* If the end position of a row equals the start
15467 position of the next row, and PT is at that position,
15468 we would rather display cursor in the next line. */
15469 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
15470 && MATRIX_ROW_END_CHARPOS (row) == PT
15471 && row < MATRIX_MODE_LINE_ROW (w->current_matrix)
15472 && MATRIX_ROW_START_CHARPOS (row+1) == PT
15473 && !cursor_row_p (row))
15474 ++row;
15476 /* If within the scroll margin, scroll. Note that
15477 MATRIX_ROW_BOTTOM_Y gives the pixel position at which
15478 the next line would be drawn, and that
15479 this_scroll_margin can be zero. */
15480 if (MATRIX_ROW_BOTTOM_Y (row) > last_y
15481 || PT > MATRIX_ROW_END_CHARPOS (row)
15482 /* Line is completely visible last line in window
15483 and PT is to be set in the next line. */
15484 || (MATRIX_ROW_BOTTOM_Y (row) == last_y
15485 && PT == MATRIX_ROW_END_CHARPOS (row)
15486 && !row->ends_at_zv_p
15487 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
15488 scroll_p = 1;
15490 else if (PT < w->last_point)
15492 /* Cursor has to be moved backward. Note that PT >=
15493 CHARPOS (startp) because of the outer if-statement. */
15494 while (!row->mode_line_p
15495 && (MATRIX_ROW_START_CHARPOS (row) > PT
15496 || (MATRIX_ROW_START_CHARPOS (row) == PT
15497 && (MATRIX_ROW_STARTS_IN_MIDDLE_OF_CHAR_P (row)
15498 || (/* STARTS_IN_MIDDLE_OF_STRING_P (row) */
15499 row > w->current_matrix->rows
15500 && (row-1)->ends_in_newline_from_string_p))))
15501 && (row->y > top_scroll_margin
15502 || CHARPOS (startp) == BEGV))
15504 eassert (row->enabled_p);
15505 --row;
15508 /* Consider the following case: Window starts at BEGV,
15509 there is invisible, intangible text at BEGV, so that
15510 display starts at some point START > BEGV. It can
15511 happen that we are called with PT somewhere between
15512 BEGV and START. Try to handle that case. */
15513 if (row < w->current_matrix->rows
15514 || row->mode_line_p)
15516 row = w->current_matrix->rows;
15517 if (row->mode_line_p)
15518 ++row;
15521 /* Due to newlines in overlay strings, we may have to
15522 skip forward over overlay strings. */
15523 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
15524 && MATRIX_ROW_END_CHARPOS (row) == PT
15525 && !cursor_row_p (row))
15526 ++row;
15528 /* If within the scroll margin, scroll. */
15529 if (row->y < top_scroll_margin
15530 && CHARPOS (startp) != BEGV)
15531 scroll_p = 1;
15533 else
15535 /* Cursor did not move. So don't scroll even if cursor line
15536 is partially visible, as it was so before. */
15537 rc = CURSOR_MOVEMENT_SUCCESS;
15540 if (PT < MATRIX_ROW_START_CHARPOS (row)
15541 || PT > MATRIX_ROW_END_CHARPOS (row))
15543 /* if PT is not in the glyph row, give up. */
15544 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15545 must_scroll = 1;
15547 else if (rc != CURSOR_MOVEMENT_SUCCESS
15548 && !NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering)))
15550 struct glyph_row *row1;
15552 /* If rows are bidi-reordered and point moved, back up
15553 until we find a row that does not belong to a
15554 continuation line. This is because we must consider
15555 all rows of a continued line as candidates for the
15556 new cursor positioning, since row start and end
15557 positions change non-linearly with vertical position
15558 in such rows. */
15559 /* FIXME: Revisit this when glyph ``spilling'' in
15560 continuation lines' rows is implemented for
15561 bidi-reordered rows. */
15562 for (row1 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
15563 MATRIX_ROW_CONTINUATION_LINE_P (row);
15564 --row)
15566 /* If we hit the beginning of the displayed portion
15567 without finding the first row of a continued
15568 line, give up. */
15569 if (row <= row1)
15571 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15572 break;
15574 eassert (row->enabled_p);
15577 if (must_scroll)
15579 else if (rc != CURSOR_MOVEMENT_SUCCESS
15580 && MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row)
15581 /* Make sure this isn't a header line by any chance, since
15582 then MATRIX_ROW_PARTIALLY_VISIBLE_P might yield non-zero. */
15583 && !row->mode_line_p
15584 && make_cursor_line_fully_visible_p)
15586 if (PT == MATRIX_ROW_END_CHARPOS (row)
15587 && !row->ends_at_zv_p
15588 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
15589 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15590 else if (row->height > window_box_height (w))
15592 /* If we end up in a partially visible line, let's
15593 make it fully visible, except when it's taller
15594 than the window, in which case we can't do much
15595 about it. */
15596 *scroll_step = 1;
15597 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15599 else
15601 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
15602 if (!cursor_row_fully_visible_p (w, 0, 1))
15603 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15604 else
15605 rc = CURSOR_MOVEMENT_SUCCESS;
15608 else if (scroll_p)
15609 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15610 else if (rc != CURSOR_MOVEMENT_SUCCESS
15611 && !NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering)))
15613 /* With bidi-reordered rows, there could be more than
15614 one candidate row whose start and end positions
15615 occlude point. We need to let set_cursor_from_row
15616 find the best candidate. */
15617 /* FIXME: Revisit this when glyph ``spilling'' in
15618 continuation lines' rows is implemented for
15619 bidi-reordered rows. */
15620 int rv = 0;
15624 int at_zv_p = 0, exact_match_p = 0;
15626 if (MATRIX_ROW_START_CHARPOS (row) <= PT
15627 && PT <= MATRIX_ROW_END_CHARPOS (row)
15628 && cursor_row_p (row))
15629 rv |= set_cursor_from_row (w, row, w->current_matrix,
15630 0, 0, 0, 0);
15631 /* As soon as we've found the exact match for point,
15632 or the first suitable row whose ends_at_zv_p flag
15633 is set, we are done. */
15634 if (rv)
15636 at_zv_p = MATRIX_ROW (w->current_matrix,
15637 w->cursor.vpos)->ends_at_zv_p;
15638 if (!at_zv_p
15639 && w->cursor.hpos >= 0
15640 && w->cursor.hpos < MATRIX_ROW_USED (w->current_matrix,
15641 w->cursor.vpos))
15643 struct glyph_row *candidate =
15644 MATRIX_ROW (w->current_matrix, w->cursor.vpos);
15645 struct glyph *g =
15646 candidate->glyphs[TEXT_AREA] + w->cursor.hpos;
15647 ptrdiff_t endpos = MATRIX_ROW_END_CHARPOS (candidate);
15649 exact_match_p =
15650 (BUFFERP (g->object) && g->charpos == PT)
15651 || (NILP (g->object)
15652 && (g->charpos == PT
15653 || (g->charpos == 0 && endpos - 1 == PT)));
15655 if (at_zv_p || exact_match_p)
15657 rc = CURSOR_MOVEMENT_SUCCESS;
15658 break;
15661 if (MATRIX_ROW_BOTTOM_Y (row) == last_y)
15662 break;
15663 ++row;
15665 while (((MATRIX_ROW_CONTINUATION_LINE_P (row)
15666 || row->continued_p)
15667 && MATRIX_ROW_BOTTOM_Y (row) <= last_y)
15668 || (MATRIX_ROW_START_CHARPOS (row) == PT
15669 && MATRIX_ROW_BOTTOM_Y (row) < last_y));
15670 /* If we didn't find any candidate rows, or exited the
15671 loop before all the candidates were examined, signal
15672 to the caller that this method failed. */
15673 if (rc != CURSOR_MOVEMENT_SUCCESS
15674 && !(rv
15675 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
15676 && !row->continued_p))
15677 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15678 else if (rv)
15679 rc = CURSOR_MOVEMENT_SUCCESS;
15681 else
15685 if (set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0))
15687 rc = CURSOR_MOVEMENT_SUCCESS;
15688 break;
15690 ++row;
15692 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
15693 && MATRIX_ROW_START_CHARPOS (row) == PT
15694 && cursor_row_p (row));
15699 return rc;
15703 void
15704 set_vertical_scroll_bar (struct window *w)
15706 ptrdiff_t start, end, whole;
15708 /* Calculate the start and end positions for the current window.
15709 At some point, it would be nice to choose between scrollbars
15710 which reflect the whole buffer size, with special markers
15711 indicating narrowing, and scrollbars which reflect only the
15712 visible region.
15714 Note that mini-buffers sometimes aren't displaying any text. */
15715 if (!MINI_WINDOW_P (w)
15716 || (w == XWINDOW (minibuf_window)
15717 && NILP (echo_area_buffer[0])))
15719 struct buffer *buf = XBUFFER (w->contents);
15720 whole = BUF_ZV (buf) - BUF_BEGV (buf);
15721 start = marker_position (w->start) - BUF_BEGV (buf);
15722 /* I don't think this is guaranteed to be right. For the
15723 moment, we'll pretend it is. */
15724 end = BUF_Z (buf) - w->window_end_pos - BUF_BEGV (buf);
15726 if (end < start)
15727 end = start;
15728 if (whole < (end - start))
15729 whole = end - start;
15731 else
15732 start = end = whole = 0;
15734 /* Indicate what this scroll bar ought to be displaying now. */
15735 if (FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
15736 (*FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
15737 (w, end - start, whole, start);
15741 void
15742 set_horizontal_scroll_bar (struct window *w)
15744 int start, end, whole, portion;
15746 if (!MINI_WINDOW_P (w)
15747 || (w == XWINDOW (minibuf_window)
15748 && NILP (echo_area_buffer[0])))
15750 struct buffer *b = XBUFFER (w->contents);
15751 struct buffer *old_buffer = NULL;
15752 struct it it;
15753 struct text_pos startp;
15755 if (b != current_buffer)
15757 old_buffer = current_buffer;
15758 set_buffer_internal (b);
15761 SET_TEXT_POS_FROM_MARKER (startp, w->start);
15762 start_display (&it, w, startp);
15763 it.last_visible_x = INT_MAX;
15764 whole = move_it_to (&it, -1, INT_MAX, window_box_height (w), -1,
15765 MOVE_TO_X | MOVE_TO_Y);
15766 /* whole = move_it_to (&it, w->window_end_pos, INT_MAX,
15767 window_box_height (w), -1,
15768 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y); */
15770 start = w->hscroll * FRAME_COLUMN_WIDTH (WINDOW_XFRAME (w));
15771 end = start + window_box_width (w, TEXT_AREA);
15772 portion = end - start;
15773 /* After enlarging a horizontally scrolled window such that it
15774 gets at least as wide as the text it contains, make sure that
15775 the thumb doesn't fill the entire scroll bar so we can still
15776 drag it back to see the entire text. */
15777 whole = max (whole, end);
15779 if (it.bidi_p)
15781 Lisp_Object pdir;
15783 pdir = Fcurrent_bidi_paragraph_direction (Qnil);
15784 if (EQ (pdir, Qright_to_left))
15786 start = whole - end;
15787 end = start + portion;
15791 if (old_buffer)
15792 set_buffer_internal (old_buffer);
15794 else
15795 start = end = whole = portion = 0;
15797 w->hscroll_whole = whole;
15799 /* Indicate what this scroll bar ought to be displaying now. */
15800 if (FRAME_TERMINAL (XFRAME (w->frame))->set_horizontal_scroll_bar_hook)
15801 (*FRAME_TERMINAL (XFRAME (w->frame))->set_horizontal_scroll_bar_hook)
15802 (w, portion, whole, start);
15806 /* Redisplay leaf window WINDOW. JUST_THIS_ONE_P non-zero means only
15807 selected_window is redisplayed.
15809 We can return without actually redisplaying the window if fonts has been
15810 changed on window's frame. In that case, redisplay_internal will retry.
15812 As one of the important parts of redisplaying a window, we need to
15813 decide whether the previous window-start position (stored in the
15814 window's w->start marker position) is still valid, and if it isn't,
15815 recompute it. Some details about that:
15817 . The previous window-start could be in a continuation line, in
15818 which case we need to recompute it when the window width
15819 changes. See compute_window_start_on_continuation_line and its
15820 call below.
15822 . The text that changed since last redisplay could include the
15823 previous window-start position. In that case, we try to salvage
15824 what we can from the current glyph matrix by calling
15825 try_scrolling, which see.
15827 . Some Emacs command could force us to use a specific window-start
15828 position by setting the window's force_start flag, or gently
15829 propose doing that by setting the window's optional_new_start
15830 flag. In these cases, we try using the specified start point if
15831 that succeeds (i.e. the window desired matrix is successfully
15832 recomputed, and point location is within the window). In case
15833 of optional_new_start, we first check if the specified start
15834 position is feasible, i.e. if it will allow point to be
15835 displayed in the window. If using the specified start point
15836 fails, e.g., if new fonts are needed to be loaded, we abort the
15837 redisplay cycle and leave it up to the next cycle to figure out
15838 things.
15840 . Note that the window's force_start flag is sometimes set by
15841 redisplay itself, when it decides that the previous window start
15842 point is fine and should be kept. Search for "goto force_start"
15843 below to see the details. Like the values of window-start
15844 specified outside of redisplay, these internally-deduced values
15845 are tested for feasibility, and ignored if found to be
15846 unfeasible.
15848 . Note that the function try_window, used to completely redisplay
15849 a window, accepts the window's start point as its argument.
15850 This is used several times in the redisplay code to control
15851 where the window start will be, according to user options such
15852 as scroll-conservatively, and also to ensure the screen line
15853 showing point will be fully (as opposed to partially) visible on
15854 display. */
15856 static void
15857 redisplay_window (Lisp_Object window, bool just_this_one_p)
15859 struct window *w = XWINDOW (window);
15860 struct frame *f = XFRAME (w->frame);
15861 struct buffer *buffer = XBUFFER (w->contents);
15862 struct buffer *old = current_buffer;
15863 struct text_pos lpoint, opoint, startp;
15864 int update_mode_line;
15865 int tem;
15866 struct it it;
15867 /* Record it now because it's overwritten. */
15868 bool current_matrix_up_to_date_p = false;
15869 bool used_current_matrix_p = false;
15870 /* This is less strict than current_matrix_up_to_date_p.
15871 It indicates that the buffer contents and narrowing are unchanged. */
15872 bool buffer_unchanged_p = false;
15873 int temp_scroll_step = 0;
15874 ptrdiff_t count = SPECPDL_INDEX ();
15875 int rc;
15876 int centering_position = -1;
15877 int last_line_misfit = 0;
15878 ptrdiff_t beg_unchanged, end_unchanged;
15879 int frame_line_height;
15881 SET_TEXT_POS (lpoint, PT, PT_BYTE);
15882 opoint = lpoint;
15884 #ifdef GLYPH_DEBUG
15885 *w->desired_matrix->method = 0;
15886 #endif
15888 if (!just_this_one_p
15889 && REDISPLAY_SOME_P ()
15890 && !w->redisplay
15891 && !f->redisplay
15892 && !buffer->text->redisplay
15893 && BUF_PT (buffer) == w->last_point)
15894 return;
15896 /* Make sure that both W's markers are valid. */
15897 eassert (XMARKER (w->start)->buffer == buffer);
15898 eassert (XMARKER (w->pointm)->buffer == buffer);
15900 /* We come here again if we need to run window-text-change-functions
15901 below. */
15902 restart:
15903 reconsider_clip_changes (w);
15904 frame_line_height = default_line_pixel_height (w);
15906 /* Has the mode line to be updated? */
15907 update_mode_line = (w->update_mode_line
15908 || update_mode_lines
15909 || buffer->clip_changed
15910 || buffer->prevent_redisplay_optimizations_p);
15912 if (!just_this_one_p)
15913 /* If `just_this_one_p' is set, we apparently set must_be_updated_p more
15914 cleverly elsewhere. */
15915 w->must_be_updated_p = true;
15917 if (MINI_WINDOW_P (w))
15919 if (w == XWINDOW (echo_area_window)
15920 && !NILP (echo_area_buffer[0]))
15922 if (update_mode_line)
15923 /* We may have to update a tty frame's menu bar or a
15924 tool-bar. Example `M-x C-h C-h C-g'. */
15925 goto finish_menu_bars;
15926 else
15927 /* We've already displayed the echo area glyphs in this window. */
15928 goto finish_scroll_bars;
15930 else if ((w != XWINDOW (minibuf_window)
15931 || minibuf_level == 0)
15932 /* When buffer is nonempty, redisplay window normally. */
15933 && BUF_Z (XBUFFER (w->contents)) == BUF_BEG (XBUFFER (w->contents))
15934 /* Quail displays non-mini buffers in minibuffer window.
15935 In that case, redisplay the window normally. */
15936 && !NILP (Fmemq (w->contents, Vminibuffer_list)))
15938 /* W is a mini-buffer window, but it's not active, so clear
15939 it. */
15940 int yb = window_text_bottom_y (w);
15941 struct glyph_row *row;
15942 int y;
15944 for (y = 0, row = w->desired_matrix->rows;
15945 y < yb;
15946 y += row->height, ++row)
15947 blank_row (w, row, y);
15948 goto finish_scroll_bars;
15951 clear_glyph_matrix (w->desired_matrix);
15954 /* Otherwise set up data on this window; select its buffer and point
15955 value. */
15956 /* Really select the buffer, for the sake of buffer-local
15957 variables. */
15958 set_buffer_internal_1 (XBUFFER (w->contents));
15960 current_matrix_up_to_date_p
15961 = (w->window_end_valid
15962 && !current_buffer->clip_changed
15963 && !current_buffer->prevent_redisplay_optimizations_p
15964 && !window_outdated (w));
15966 /* Run the window-text-change-functions
15967 if it is possible that the text on the screen has changed
15968 (either due to modification of the text, or any other reason). */
15969 if (!current_matrix_up_to_date_p
15970 && !NILP (Vwindow_text_change_functions))
15972 safe_run_hooks (Qwindow_text_change_functions);
15973 goto restart;
15976 beg_unchanged = BEG_UNCHANGED;
15977 end_unchanged = END_UNCHANGED;
15979 SET_TEXT_POS (opoint, PT, PT_BYTE);
15981 specbind (Qinhibit_point_motion_hooks, Qt);
15983 buffer_unchanged_p
15984 = (w->window_end_valid
15985 && !current_buffer->clip_changed
15986 && !window_outdated (w));
15988 /* When windows_or_buffers_changed is non-zero, we can't rely
15989 on the window end being valid, so set it to zero there. */
15990 if (windows_or_buffers_changed)
15992 /* If window starts on a continuation line, maybe adjust the
15993 window start in case the window's width changed. */
15994 if (XMARKER (w->start)->buffer == current_buffer)
15995 compute_window_start_on_continuation_line (w);
15997 w->window_end_valid = false;
15998 /* If so, we also can't rely on current matrix
15999 and should not fool try_cursor_movement below. */
16000 current_matrix_up_to_date_p = false;
16003 /* Some sanity checks. */
16004 CHECK_WINDOW_END (w);
16005 if (Z == Z_BYTE && CHARPOS (opoint) != BYTEPOS (opoint))
16006 emacs_abort ();
16007 if (BYTEPOS (opoint) < CHARPOS (opoint))
16008 emacs_abort ();
16010 if (mode_line_update_needed (w))
16011 update_mode_line = 1;
16013 /* Point refers normally to the selected window. For any other
16014 window, set up appropriate value. */
16015 if (!EQ (window, selected_window))
16017 ptrdiff_t new_pt = marker_position (w->pointm);
16018 ptrdiff_t new_pt_byte = marker_byte_position (w->pointm);
16020 if (new_pt < BEGV)
16022 new_pt = BEGV;
16023 new_pt_byte = BEGV_BYTE;
16024 set_marker_both (w->pointm, Qnil, BEGV, BEGV_BYTE);
16026 else if (new_pt > (ZV - 1))
16028 new_pt = ZV;
16029 new_pt_byte = ZV_BYTE;
16030 set_marker_both (w->pointm, Qnil, ZV, ZV_BYTE);
16033 /* We don't use SET_PT so that the point-motion hooks don't run. */
16034 TEMP_SET_PT_BOTH (new_pt, new_pt_byte);
16037 /* If any of the character widths specified in the display table
16038 have changed, invalidate the width run cache. It's true that
16039 this may be a bit late to catch such changes, but the rest of
16040 redisplay goes (non-fatally) haywire when the display table is
16041 changed, so why should we worry about doing any better? */
16042 if (current_buffer->width_run_cache
16043 || (current_buffer->base_buffer
16044 && current_buffer->base_buffer->width_run_cache))
16046 struct Lisp_Char_Table *disptab = buffer_display_table ();
16048 if (! disptab_matches_widthtab
16049 (disptab, XVECTOR (BVAR (current_buffer, width_table))))
16051 struct buffer *buf = current_buffer;
16053 if (buf->base_buffer)
16054 buf = buf->base_buffer;
16055 invalidate_region_cache (buf, buf->width_run_cache, BEG, Z);
16056 recompute_width_table (current_buffer, disptab);
16060 /* If window-start is screwed up, choose a new one. */
16061 if (XMARKER (w->start)->buffer != current_buffer)
16062 goto recenter;
16064 SET_TEXT_POS_FROM_MARKER (startp, w->start);
16066 /* If someone specified a new starting point but did not insist,
16067 check whether it can be used. */
16068 if ((w->optional_new_start || window_frozen_p (w))
16069 && CHARPOS (startp) >= BEGV
16070 && CHARPOS (startp) <= ZV)
16072 ptrdiff_t it_charpos;
16074 w->optional_new_start = 0;
16075 start_display (&it, w, startp);
16076 move_it_to (&it, PT, 0, it.last_visible_y, -1,
16077 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
16078 /* Record IT's position now, since line_bottom_y might change
16079 that. */
16080 it_charpos = IT_CHARPOS (it);
16081 /* Make sure we set the force_start flag only if the cursor row
16082 will be fully visible. Otherwise, the code under force_start
16083 label below will try to move point back into view, which is
16084 not what the code which sets optional_new_start wants. */
16085 if ((it.current_y == 0 || line_bottom_y (&it) < it.last_visible_y)
16086 && !w->force_start)
16088 if (it_charpos == PT)
16089 w->force_start = 1;
16090 /* IT may overshoot PT if text at PT is invisible. */
16091 else if (it_charpos > PT && CHARPOS (startp) <= PT)
16092 w->force_start = 1;
16093 #ifdef GLYPH_DEBUG
16094 if (w->force_start)
16096 if (window_frozen_p (w))
16097 debug_method_add (w, "set force_start from frozen window start");
16098 else
16099 debug_method_add (w, "set force_start from optional_new_start");
16101 #endif
16105 force_start:
16107 /* Handle case where place to start displaying has been specified,
16108 unless the specified location is outside the accessible range. */
16109 if (w->force_start)
16111 /* We set this later on if we have to adjust point. */
16112 int new_vpos = -1;
16114 w->force_start = 0;
16115 w->vscroll = 0;
16116 w->window_end_valid = 0;
16118 /* Forget any recorded base line for line number display. */
16119 if (!buffer_unchanged_p)
16120 w->base_line_number = 0;
16122 /* Redisplay the mode line. Select the buffer properly for that.
16123 Also, run the hook window-scroll-functions
16124 because we have scrolled. */
16125 /* Note, we do this after clearing force_start because
16126 if there's an error, it is better to forget about force_start
16127 than to get into an infinite loop calling the hook functions
16128 and having them get more errors. */
16129 if (!update_mode_line
16130 || ! NILP (Vwindow_scroll_functions))
16132 update_mode_line = 1;
16133 w->update_mode_line = 1;
16134 startp = run_window_scroll_functions (window, startp);
16137 if (CHARPOS (startp) < BEGV)
16138 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
16139 else if (CHARPOS (startp) > ZV)
16140 SET_TEXT_POS (startp, ZV, ZV_BYTE);
16142 /* Redisplay, then check if cursor has been set during the
16143 redisplay. Give up if new fonts were loaded. */
16144 /* We used to issue a CHECK_MARGINS argument to try_window here,
16145 but this causes scrolling to fail when point begins inside
16146 the scroll margin (bug#148) -- cyd */
16147 if (!try_window (window, startp, 0))
16149 w->force_start = 1;
16150 clear_glyph_matrix (w->desired_matrix);
16151 goto need_larger_matrices;
16154 if (w->cursor.vpos < 0)
16156 /* If point does not appear, try to move point so it does
16157 appear. The desired matrix has been built above, so we
16158 can use it here. */
16159 new_vpos = window_box_height (w) / 2;
16162 if (!cursor_row_fully_visible_p (w, 0, 0))
16164 /* Point does appear, but on a line partly visible at end of window.
16165 Move it back to a fully-visible line. */
16166 new_vpos = window_box_height (w);
16167 /* But if window_box_height suggests a Y coordinate that is
16168 not less than we already have, that line will clearly not
16169 be fully visible, so give up and scroll the display.
16170 This can happen when the default face uses a font whose
16171 dimensions are different from the frame's default
16172 font. */
16173 if (new_vpos >= w->cursor.y)
16175 w->cursor.vpos = -1;
16176 clear_glyph_matrix (w->desired_matrix);
16177 goto try_to_scroll;
16180 else if (w->cursor.vpos >= 0)
16182 /* Some people insist on not letting point enter the scroll
16183 margin, even though this part handles windows that didn't
16184 scroll at all. */
16185 int window_total_lines
16186 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
16187 int margin = min (scroll_margin, window_total_lines / 4);
16188 int pixel_margin = margin * frame_line_height;
16189 bool header_line = WINDOW_WANTS_HEADER_LINE_P (w);
16191 /* Note: We add an extra FRAME_LINE_HEIGHT, because the loop
16192 below, which finds the row to move point to, advances by
16193 the Y coordinate of the _next_ row, see the definition of
16194 MATRIX_ROW_BOTTOM_Y. */
16195 if (w->cursor.vpos < margin + header_line)
16197 w->cursor.vpos = -1;
16198 clear_glyph_matrix (w->desired_matrix);
16199 goto try_to_scroll;
16201 else
16203 int window_height = window_box_height (w);
16205 if (header_line)
16206 window_height += CURRENT_HEADER_LINE_HEIGHT (w);
16207 if (w->cursor.y >= window_height - pixel_margin)
16209 w->cursor.vpos = -1;
16210 clear_glyph_matrix (w->desired_matrix);
16211 goto try_to_scroll;
16216 /* If we need to move point for either of the above reasons,
16217 now actually do it. */
16218 if (new_vpos >= 0)
16220 struct glyph_row *row;
16222 row = MATRIX_FIRST_TEXT_ROW (w->desired_matrix);
16223 while (MATRIX_ROW_BOTTOM_Y (row) < new_vpos)
16224 ++row;
16226 TEMP_SET_PT_BOTH (MATRIX_ROW_START_CHARPOS (row),
16227 MATRIX_ROW_START_BYTEPOS (row));
16229 if (w != XWINDOW (selected_window))
16230 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
16231 else if (current_buffer == old)
16232 SET_TEXT_POS (lpoint, PT, PT_BYTE);
16234 set_cursor_from_row (w, row, w->desired_matrix, 0, 0, 0, 0);
16236 /* Re-run pre-redisplay-function so it can update the region
16237 according to the new position of point. */
16238 /* Other than the cursor, w's redisplay is done so we can set its
16239 redisplay to false. Also the buffer's redisplay can be set to
16240 false, since propagate_buffer_redisplay should have already
16241 propagated its info to `w' anyway. */
16242 w->redisplay = false;
16243 XBUFFER (w->contents)->text->redisplay = false;
16244 safe__call1 (true, Vpre_redisplay_function, Fcons (window, Qnil));
16246 if (w->redisplay || XBUFFER (w->contents)->text->redisplay)
16248 /* pre-redisplay-function made changes (e.g. move the region)
16249 that require another round of redisplay. */
16250 clear_glyph_matrix (w->desired_matrix);
16251 if (!try_window (window, startp, 0))
16252 goto need_larger_matrices;
16255 if (w->cursor.vpos < 0 || !cursor_row_fully_visible_p (w, 0, 0))
16257 clear_glyph_matrix (w->desired_matrix);
16258 goto try_to_scroll;
16261 #ifdef GLYPH_DEBUG
16262 debug_method_add (w, "forced window start");
16263 #endif
16264 goto done;
16267 /* Handle case where text has not changed, only point, and it has
16268 not moved off the frame, and we are not retrying after hscroll.
16269 (current_matrix_up_to_date_p is nonzero when retrying.) */
16270 if (current_matrix_up_to_date_p
16271 && (rc = try_cursor_movement (window, startp, &temp_scroll_step),
16272 rc != CURSOR_MOVEMENT_CANNOT_BE_USED))
16274 switch (rc)
16276 case CURSOR_MOVEMENT_SUCCESS:
16277 used_current_matrix_p = 1;
16278 goto done;
16280 case CURSOR_MOVEMENT_MUST_SCROLL:
16281 goto try_to_scroll;
16283 default:
16284 emacs_abort ();
16287 /* If current starting point was originally the beginning of a line
16288 but no longer is, find a new starting point. */
16289 else if (w->start_at_line_beg
16290 && !(CHARPOS (startp) <= BEGV
16291 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n'))
16293 #ifdef GLYPH_DEBUG
16294 debug_method_add (w, "recenter 1");
16295 #endif
16296 goto recenter;
16299 /* Try scrolling with try_window_id. Value is > 0 if update has
16300 been done, it is -1 if we know that the same window start will
16301 not work. It is 0 if unsuccessful for some other reason. */
16302 else if ((tem = try_window_id (w)) != 0)
16304 #ifdef GLYPH_DEBUG
16305 debug_method_add (w, "try_window_id %d", tem);
16306 #endif
16308 if (f->fonts_changed)
16309 goto need_larger_matrices;
16310 if (tem > 0)
16311 goto done;
16313 /* Otherwise try_window_id has returned -1 which means that we
16314 don't want the alternative below this comment to execute. */
16316 else if (CHARPOS (startp) >= BEGV
16317 && CHARPOS (startp) <= ZV
16318 && PT >= CHARPOS (startp)
16319 && (CHARPOS (startp) < ZV
16320 /* Avoid starting at end of buffer. */
16321 || CHARPOS (startp) == BEGV
16322 || !window_outdated (w)))
16324 int d1, d2, d5, d6;
16325 int rtop, rbot;
16327 /* If first window line is a continuation line, and window start
16328 is inside the modified region, but the first change is before
16329 current window start, we must select a new window start.
16331 However, if this is the result of a down-mouse event (e.g. by
16332 extending the mouse-drag-overlay), we don't want to select a
16333 new window start, since that would change the position under
16334 the mouse, resulting in an unwanted mouse-movement rather
16335 than a simple mouse-click. */
16336 if (!w->start_at_line_beg
16337 && NILP (do_mouse_tracking)
16338 && CHARPOS (startp) > BEGV
16339 && CHARPOS (startp) > BEG + beg_unchanged
16340 && CHARPOS (startp) <= Z - end_unchanged
16341 /* Even if w->start_at_line_beg is nil, a new window may
16342 start at a line_beg, since that's how set_buffer_window
16343 sets it. So, we need to check the return value of
16344 compute_window_start_on_continuation_line. (See also
16345 bug#197). */
16346 && XMARKER (w->start)->buffer == current_buffer
16347 && compute_window_start_on_continuation_line (w)
16348 /* It doesn't make sense to force the window start like we
16349 do at label force_start if it is already known that point
16350 will not be fully visible in the resulting window, because
16351 doing so will move point from its correct position
16352 instead of scrolling the window to bring point into view.
16353 See bug#9324. */
16354 && pos_visible_p (w, PT, &d1, &d2, &rtop, &rbot, &d5, &d6)
16355 /* A very tall row could need more than the window height,
16356 in which case we accept that it is partially visible. */
16357 && (rtop != 0) == (rbot != 0))
16359 w->force_start = 1;
16360 SET_TEXT_POS_FROM_MARKER (startp, w->start);
16361 #ifdef GLYPH_DEBUG
16362 debug_method_add (w, "recomputed window start in continuation line");
16363 #endif
16364 goto force_start;
16367 #ifdef GLYPH_DEBUG
16368 debug_method_add (w, "same window start");
16369 #endif
16371 /* Try to redisplay starting at same place as before.
16372 If point has not moved off frame, accept the results. */
16373 if (!current_matrix_up_to_date_p
16374 /* Don't use try_window_reusing_current_matrix in this case
16375 because a window scroll function can have changed the
16376 buffer. */
16377 || !NILP (Vwindow_scroll_functions)
16378 || MINI_WINDOW_P (w)
16379 || !(used_current_matrix_p
16380 = try_window_reusing_current_matrix (w)))
16382 IF_DEBUG (debug_method_add (w, "1"));
16383 if (try_window (window, startp, TRY_WINDOW_CHECK_MARGINS) < 0)
16384 /* -1 means we need to scroll.
16385 0 means we need new matrices, but fonts_changed
16386 is set in that case, so we will detect it below. */
16387 goto try_to_scroll;
16390 if (f->fonts_changed)
16391 goto need_larger_matrices;
16393 if (w->cursor.vpos >= 0)
16395 if (!just_this_one_p
16396 || current_buffer->clip_changed
16397 || BEG_UNCHANGED < CHARPOS (startp))
16398 /* Forget any recorded base line for line number display. */
16399 w->base_line_number = 0;
16401 if (!cursor_row_fully_visible_p (w, 1, 0))
16403 clear_glyph_matrix (w->desired_matrix);
16404 last_line_misfit = 1;
16406 /* Drop through and scroll. */
16407 else
16408 goto done;
16410 else
16411 clear_glyph_matrix (w->desired_matrix);
16414 try_to_scroll:
16416 /* Redisplay the mode line. Select the buffer properly for that. */
16417 if (!update_mode_line)
16419 update_mode_line = 1;
16420 w->update_mode_line = 1;
16423 /* Try to scroll by specified few lines. */
16424 if ((scroll_conservatively
16425 || emacs_scroll_step
16426 || temp_scroll_step
16427 || NUMBERP (BVAR (current_buffer, scroll_up_aggressively))
16428 || NUMBERP (BVAR (current_buffer, scroll_down_aggressively)))
16429 && CHARPOS (startp) >= BEGV
16430 && CHARPOS (startp) <= ZV)
16432 /* The function returns -1 if new fonts were loaded, 1 if
16433 successful, 0 if not successful. */
16434 int ss = try_scrolling (window, just_this_one_p,
16435 scroll_conservatively,
16436 emacs_scroll_step,
16437 temp_scroll_step, last_line_misfit);
16438 switch (ss)
16440 case SCROLLING_SUCCESS:
16441 goto done;
16443 case SCROLLING_NEED_LARGER_MATRICES:
16444 goto need_larger_matrices;
16446 case SCROLLING_FAILED:
16447 break;
16449 default:
16450 emacs_abort ();
16454 /* Finally, just choose a place to start which positions point
16455 according to user preferences. */
16457 recenter:
16459 #ifdef GLYPH_DEBUG
16460 debug_method_add (w, "recenter");
16461 #endif
16463 /* Forget any previously recorded base line for line number display. */
16464 if (!buffer_unchanged_p)
16465 w->base_line_number = 0;
16467 /* Determine the window start relative to point. */
16468 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
16469 it.current_y = it.last_visible_y;
16470 if (centering_position < 0)
16472 int window_total_lines
16473 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
16474 int margin
16475 = scroll_margin > 0
16476 ? min (scroll_margin, window_total_lines / 4)
16477 : 0;
16478 ptrdiff_t margin_pos = CHARPOS (startp);
16479 Lisp_Object aggressive;
16480 int scrolling_up;
16482 /* If there is a scroll margin at the top of the window, find
16483 its character position. */
16484 if (margin
16485 /* Cannot call start_display if startp is not in the
16486 accessible region of the buffer. This can happen when we
16487 have just switched to a different buffer and/or changed
16488 its restriction. In that case, startp is initialized to
16489 the character position 1 (BEGV) because we did not yet
16490 have chance to display the buffer even once. */
16491 && BEGV <= CHARPOS (startp) && CHARPOS (startp) <= ZV)
16493 struct it it1;
16494 void *it1data = NULL;
16496 SAVE_IT (it1, it, it1data);
16497 start_display (&it1, w, startp);
16498 move_it_vertically (&it1, margin * frame_line_height);
16499 margin_pos = IT_CHARPOS (it1);
16500 RESTORE_IT (&it, &it, it1data);
16502 scrolling_up = PT > margin_pos;
16503 aggressive =
16504 scrolling_up
16505 ? BVAR (current_buffer, scroll_up_aggressively)
16506 : BVAR (current_buffer, scroll_down_aggressively);
16508 if (!MINI_WINDOW_P (w)
16509 && (scroll_conservatively > SCROLL_LIMIT || NUMBERP (aggressive)))
16511 int pt_offset = 0;
16513 /* Setting scroll-conservatively overrides
16514 scroll-*-aggressively. */
16515 if (!scroll_conservatively && NUMBERP (aggressive))
16517 double float_amount = XFLOATINT (aggressive);
16519 pt_offset = float_amount * WINDOW_BOX_TEXT_HEIGHT (w);
16520 if (pt_offset == 0 && float_amount > 0)
16521 pt_offset = 1;
16522 if (pt_offset && margin > 0)
16523 margin -= 1;
16525 /* Compute how much to move the window start backward from
16526 point so that point will be displayed where the user
16527 wants it. */
16528 if (scrolling_up)
16530 centering_position = it.last_visible_y;
16531 if (pt_offset)
16532 centering_position -= pt_offset;
16533 centering_position -=
16534 frame_line_height * (1 + margin + (last_line_misfit != 0))
16535 + WINDOW_HEADER_LINE_HEIGHT (w);
16536 /* Don't let point enter the scroll margin near top of
16537 the window. */
16538 if (centering_position < margin * frame_line_height)
16539 centering_position = margin * frame_line_height;
16541 else
16542 centering_position = margin * frame_line_height + pt_offset;
16544 else
16545 /* Set the window start half the height of the window backward
16546 from point. */
16547 centering_position = window_box_height (w) / 2;
16549 move_it_vertically_backward (&it, centering_position);
16551 eassert (IT_CHARPOS (it) >= BEGV);
16553 /* The function move_it_vertically_backward may move over more
16554 than the specified y-distance. If it->w is small, e.g. a
16555 mini-buffer window, we may end up in front of the window's
16556 display area. Start displaying at the start of the line
16557 containing PT in this case. */
16558 if (it.current_y <= 0)
16560 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
16561 move_it_vertically_backward (&it, 0);
16562 it.current_y = 0;
16565 it.current_x = it.hpos = 0;
16567 /* Set the window start position here explicitly, to avoid an
16568 infinite loop in case the functions in window-scroll-functions
16569 get errors. */
16570 set_marker_both (w->start, Qnil, IT_CHARPOS (it), IT_BYTEPOS (it));
16572 /* Run scroll hooks. */
16573 startp = run_window_scroll_functions (window, it.current.pos);
16575 /* Redisplay the window. */
16576 if (!current_matrix_up_to_date_p
16577 || windows_or_buffers_changed
16578 || f->cursor_type_changed
16579 /* Don't use try_window_reusing_current_matrix in this case
16580 because it can have changed the buffer. */
16581 || !NILP (Vwindow_scroll_functions)
16582 || !just_this_one_p
16583 || MINI_WINDOW_P (w)
16584 || !(used_current_matrix_p
16585 = try_window_reusing_current_matrix (w)))
16586 try_window (window, startp, 0);
16588 /* If new fonts have been loaded (due to fontsets), give up. We
16589 have to start a new redisplay since we need to re-adjust glyph
16590 matrices. */
16591 if (f->fonts_changed)
16592 goto need_larger_matrices;
16594 /* If cursor did not appear assume that the middle of the window is
16595 in the first line of the window. Do it again with the next line.
16596 (Imagine a window of height 100, displaying two lines of height
16597 60. Moving back 50 from it->last_visible_y will end in the first
16598 line.) */
16599 if (w->cursor.vpos < 0)
16601 if (w->window_end_valid && PT >= Z - w->window_end_pos)
16603 clear_glyph_matrix (w->desired_matrix);
16604 move_it_by_lines (&it, 1);
16605 try_window (window, it.current.pos, 0);
16607 else if (PT < IT_CHARPOS (it))
16609 clear_glyph_matrix (w->desired_matrix);
16610 move_it_by_lines (&it, -1);
16611 try_window (window, it.current.pos, 0);
16613 else
16615 /* Not much we can do about it. */
16619 /* Consider the following case: Window starts at BEGV, there is
16620 invisible, intangible text at BEGV, so that display starts at
16621 some point START > BEGV. It can happen that we are called with
16622 PT somewhere between BEGV and START. Try to handle that case,
16623 and similar ones. */
16624 if (w->cursor.vpos < 0)
16626 /* First, try locating the proper glyph row for PT. */
16627 struct glyph_row *row =
16628 row_containing_pos (w, PT, w->current_matrix->rows, NULL, 0);
16630 /* Sometimes point is at the beginning of invisible text that is
16631 before the 1st character displayed in the row. In that case,
16632 row_containing_pos fails to find the row, because no glyphs
16633 with appropriate buffer positions are present in the row.
16634 Therefore, we next try to find the row which shows the 1st
16635 position after the invisible text. */
16636 if (!row)
16638 Lisp_Object val =
16639 get_char_property_and_overlay (make_number (PT), Qinvisible,
16640 Qnil, NULL);
16642 if (TEXT_PROP_MEANS_INVISIBLE (val))
16644 ptrdiff_t alt_pos;
16645 Lisp_Object invis_end =
16646 Fnext_single_char_property_change (make_number (PT), Qinvisible,
16647 Qnil, Qnil);
16649 if (NATNUMP (invis_end))
16650 alt_pos = XFASTINT (invis_end);
16651 else
16652 alt_pos = ZV;
16653 row = row_containing_pos (w, alt_pos, w->current_matrix->rows,
16654 NULL, 0);
16657 /* Finally, fall back on the first row of the window after the
16658 header line (if any). This is slightly better than not
16659 displaying the cursor at all. */
16660 if (!row)
16662 row = w->current_matrix->rows;
16663 if (row->mode_line_p)
16664 ++row;
16666 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
16669 if (!cursor_row_fully_visible_p (w, 0, 0))
16671 /* If vscroll is enabled, disable it and try again. */
16672 if (w->vscroll)
16674 w->vscroll = 0;
16675 clear_glyph_matrix (w->desired_matrix);
16676 goto recenter;
16679 /* Users who set scroll-conservatively to a large number want
16680 point just above/below the scroll margin. If we ended up
16681 with point's row partially visible, move the window start to
16682 make that row fully visible and out of the margin. */
16683 if (scroll_conservatively > SCROLL_LIMIT)
16685 int window_total_lines
16686 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) * frame_line_height;
16687 int margin =
16688 scroll_margin > 0
16689 ? min (scroll_margin, window_total_lines / 4)
16690 : 0;
16691 int move_down = w->cursor.vpos >= window_total_lines / 2;
16693 move_it_by_lines (&it, move_down ? margin + 1 : -(margin + 1));
16694 clear_glyph_matrix (w->desired_matrix);
16695 if (1 == try_window (window, it.current.pos,
16696 TRY_WINDOW_CHECK_MARGINS))
16697 goto done;
16700 /* If centering point failed to make the whole line visible,
16701 put point at the top instead. That has to make the whole line
16702 visible, if it can be done. */
16703 if (centering_position == 0)
16704 goto done;
16706 clear_glyph_matrix (w->desired_matrix);
16707 centering_position = 0;
16708 goto recenter;
16711 done:
16713 SET_TEXT_POS_FROM_MARKER (startp, w->start);
16714 w->start_at_line_beg = (CHARPOS (startp) == BEGV
16715 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n');
16717 /* Display the mode line, if we must. */
16718 if ((update_mode_line
16719 /* If window not full width, must redo its mode line
16720 if (a) the window to its side is being redone and
16721 (b) we do a frame-based redisplay. This is a consequence
16722 of how inverted lines are drawn in frame-based redisplay. */
16723 || (!just_this_one_p
16724 && !FRAME_WINDOW_P (f)
16725 && !WINDOW_FULL_WIDTH_P (w))
16726 /* Line number to display. */
16727 || w->base_line_pos > 0
16728 /* Column number is displayed and different from the one displayed. */
16729 || (w->column_number_displayed != -1
16730 && (w->column_number_displayed != current_column ())))
16731 /* This means that the window has a mode line. */
16732 && (WINDOW_WANTS_MODELINE_P (w)
16733 || WINDOW_WANTS_HEADER_LINE_P (w)))
16736 display_mode_lines (w);
16738 /* If mode line height has changed, arrange for a thorough
16739 immediate redisplay using the correct mode line height. */
16740 if (WINDOW_WANTS_MODELINE_P (w)
16741 && CURRENT_MODE_LINE_HEIGHT (w) != DESIRED_MODE_LINE_HEIGHT (w))
16743 f->fonts_changed = 1;
16744 w->mode_line_height = -1;
16745 MATRIX_MODE_LINE_ROW (w->current_matrix)->height
16746 = DESIRED_MODE_LINE_HEIGHT (w);
16749 /* If header line height has changed, arrange for a thorough
16750 immediate redisplay using the correct header line height. */
16751 if (WINDOW_WANTS_HEADER_LINE_P (w)
16752 && CURRENT_HEADER_LINE_HEIGHT (w) != DESIRED_HEADER_LINE_HEIGHT (w))
16754 f->fonts_changed = 1;
16755 w->header_line_height = -1;
16756 MATRIX_HEADER_LINE_ROW (w->current_matrix)->height
16757 = DESIRED_HEADER_LINE_HEIGHT (w);
16760 if (f->fonts_changed)
16761 goto need_larger_matrices;
16764 if (!line_number_displayed && w->base_line_pos != -1)
16766 w->base_line_pos = 0;
16767 w->base_line_number = 0;
16770 finish_menu_bars:
16772 /* When we reach a frame's selected window, redo the frame's menu bar. */
16773 if (update_mode_line
16774 && EQ (FRAME_SELECTED_WINDOW (f), window))
16776 int redisplay_menu_p = 0;
16778 if (FRAME_WINDOW_P (f))
16780 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
16781 || defined (HAVE_NS) || defined (USE_GTK)
16782 redisplay_menu_p = FRAME_EXTERNAL_MENU_BAR (f);
16783 #else
16784 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
16785 #endif
16787 else
16788 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
16790 if (redisplay_menu_p)
16791 display_menu_bar (w);
16793 #ifdef HAVE_WINDOW_SYSTEM
16794 if (FRAME_WINDOW_P (f))
16796 #if defined (USE_GTK) || defined (HAVE_NS)
16797 if (FRAME_EXTERNAL_TOOL_BAR (f))
16798 redisplay_tool_bar (f);
16799 #else
16800 if (WINDOWP (f->tool_bar_window)
16801 && (FRAME_TOOL_BAR_LINES (f) > 0
16802 || !NILP (Vauto_resize_tool_bars))
16803 && redisplay_tool_bar (f))
16804 ignore_mouse_drag_p = 1;
16805 #endif
16807 #endif
16810 #ifdef HAVE_WINDOW_SYSTEM
16811 if (FRAME_WINDOW_P (f)
16812 && update_window_fringes (w, (just_this_one_p
16813 || (!used_current_matrix_p && !overlay_arrow_seen)
16814 || w->pseudo_window_p)))
16816 update_begin (f);
16817 block_input ();
16818 if (draw_window_fringes (w, 1))
16820 if (WINDOW_RIGHT_DIVIDER_WIDTH (w))
16821 x_draw_right_divider (w);
16822 else
16823 x_draw_vertical_border (w);
16825 unblock_input ();
16826 update_end (f);
16829 if (WINDOW_BOTTOM_DIVIDER_WIDTH (w))
16830 x_draw_bottom_divider (w);
16831 #endif /* HAVE_WINDOW_SYSTEM */
16833 /* We go to this label, with fonts_changed set, if it is
16834 necessary to try again using larger glyph matrices.
16835 We have to redeem the scroll bar even in this case,
16836 because the loop in redisplay_internal expects that. */
16837 need_larger_matrices:
16839 finish_scroll_bars:
16841 if (WINDOW_HAS_VERTICAL_SCROLL_BAR (w) || WINDOW_HAS_HORIZONTAL_SCROLL_BAR (w))
16843 if (WINDOW_HAS_VERTICAL_SCROLL_BAR (w))
16844 /* Set the thumb's position and size. */
16845 set_vertical_scroll_bar (w);
16847 if (WINDOW_HAS_HORIZONTAL_SCROLL_BAR (w))
16848 /* Set the thumb's position and size. */
16849 set_horizontal_scroll_bar (w);
16851 /* Note that we actually used the scroll bar attached to this
16852 window, so it shouldn't be deleted at the end of redisplay. */
16853 if (FRAME_TERMINAL (f)->redeem_scroll_bar_hook)
16854 (*FRAME_TERMINAL (f)->redeem_scroll_bar_hook) (w);
16857 /* Restore current_buffer and value of point in it. The window
16858 update may have changed the buffer, so first make sure `opoint'
16859 is still valid (Bug#6177). */
16860 if (CHARPOS (opoint) < BEGV)
16861 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
16862 else if (CHARPOS (opoint) > ZV)
16863 TEMP_SET_PT_BOTH (Z, Z_BYTE);
16864 else
16865 TEMP_SET_PT_BOTH (CHARPOS (opoint), BYTEPOS (opoint));
16867 set_buffer_internal_1 (old);
16868 /* Avoid an abort in TEMP_SET_PT_BOTH if the buffer has become
16869 shorter. This can be caused by log truncation in *Messages*. */
16870 if (CHARPOS (lpoint) <= ZV)
16871 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
16873 unbind_to (count, Qnil);
16877 /* Build the complete desired matrix of WINDOW with a window start
16878 buffer position POS.
16880 Value is 1 if successful. It is zero if fonts were loaded during
16881 redisplay which makes re-adjusting glyph matrices necessary, and -1
16882 if point would appear in the scroll margins.
16883 (We check the former only if TRY_WINDOW_IGNORE_FONTS_CHANGE is
16884 unset in FLAGS, and the latter only if TRY_WINDOW_CHECK_MARGINS is
16885 set in FLAGS.) */
16888 try_window (Lisp_Object window, struct text_pos pos, int flags)
16890 struct window *w = XWINDOW (window);
16891 struct it it;
16892 struct glyph_row *last_text_row = NULL;
16893 struct frame *f = XFRAME (w->frame);
16894 int frame_line_height = default_line_pixel_height (w);
16896 /* Make POS the new window start. */
16897 set_marker_both (w->start, Qnil, CHARPOS (pos), BYTEPOS (pos));
16899 /* Mark cursor position as unknown. No overlay arrow seen. */
16900 w->cursor.vpos = -1;
16901 overlay_arrow_seen = 0;
16903 /* Initialize iterator and info to start at POS. */
16904 start_display (&it, w, pos);
16905 it.glyph_row->reversed_p = false;
16907 /* Display all lines of W. */
16908 while (it.current_y < it.last_visible_y)
16910 if (display_line (&it))
16911 last_text_row = it.glyph_row - 1;
16912 if (f->fonts_changed && !(flags & TRY_WINDOW_IGNORE_FONTS_CHANGE))
16913 return 0;
16916 /* Don't let the cursor end in the scroll margins. */
16917 if ((flags & TRY_WINDOW_CHECK_MARGINS)
16918 && !MINI_WINDOW_P (w))
16920 int this_scroll_margin;
16921 int window_total_lines
16922 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
16924 if (scroll_margin > 0)
16926 this_scroll_margin = min (scroll_margin, window_total_lines / 4);
16927 this_scroll_margin *= frame_line_height;
16929 else
16930 this_scroll_margin = 0;
16932 if ((w->cursor.y >= 0 /* not vscrolled */
16933 && w->cursor.y < this_scroll_margin
16934 && CHARPOS (pos) > BEGV
16935 && IT_CHARPOS (it) < ZV)
16936 /* rms: considering make_cursor_line_fully_visible_p here
16937 seems to give wrong results. We don't want to recenter
16938 when the last line is partly visible, we want to allow
16939 that case to be handled in the usual way. */
16940 || w->cursor.y > it.last_visible_y - this_scroll_margin - 1)
16942 w->cursor.vpos = -1;
16943 clear_glyph_matrix (w->desired_matrix);
16944 return -1;
16948 /* If bottom moved off end of frame, change mode line percentage. */
16949 if (w->window_end_pos <= 0 && Z != IT_CHARPOS (it))
16950 w->update_mode_line = 1;
16952 /* Set window_end_pos to the offset of the last character displayed
16953 on the window from the end of current_buffer. Set
16954 window_end_vpos to its row number. */
16955 if (last_text_row)
16957 eassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_text_row));
16958 adjust_window_ends (w, last_text_row, 0);
16959 eassert
16960 (MATRIX_ROW_DISPLAYS_TEXT_P (MATRIX_ROW (w->desired_matrix,
16961 w->window_end_vpos)));
16963 else
16965 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
16966 w->window_end_pos = Z - ZV;
16967 w->window_end_vpos = 0;
16970 /* But that is not valid info until redisplay finishes. */
16971 w->window_end_valid = 0;
16972 return 1;
16977 /************************************************************************
16978 Window redisplay reusing current matrix when buffer has not changed
16979 ************************************************************************/
16981 /* Try redisplay of window W showing an unchanged buffer with a
16982 different window start than the last time it was displayed by
16983 reusing its current matrix. Value is non-zero if successful.
16984 W->start is the new window start. */
16986 static int
16987 try_window_reusing_current_matrix (struct window *w)
16989 struct frame *f = XFRAME (w->frame);
16990 struct glyph_row *bottom_row;
16991 struct it it;
16992 struct run run;
16993 struct text_pos start, new_start;
16994 int nrows_scrolled, i;
16995 struct glyph_row *last_text_row;
16996 struct glyph_row *last_reused_text_row;
16997 struct glyph_row *start_row;
16998 int start_vpos, min_y, max_y;
17000 #ifdef GLYPH_DEBUG
17001 if (inhibit_try_window_reusing)
17002 return 0;
17003 #endif
17005 if (/* This function doesn't handle terminal frames. */
17006 !FRAME_WINDOW_P (f)
17007 /* Don't try to reuse the display if windows have been split
17008 or such. */
17009 || windows_or_buffers_changed
17010 || f->cursor_type_changed)
17011 return 0;
17013 /* Can't do this if showing trailing whitespace. */
17014 if (!NILP (Vshow_trailing_whitespace))
17015 return 0;
17017 /* If top-line visibility has changed, give up. */
17018 if (WINDOW_WANTS_HEADER_LINE_P (w)
17019 != MATRIX_HEADER_LINE_ROW (w->current_matrix)->mode_line_p)
17020 return 0;
17022 /* Give up if old or new display is scrolled vertically. We could
17023 make this function handle this, but right now it doesn't. */
17024 start_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
17025 if (w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row))
17026 return 0;
17028 /* The variable new_start now holds the new window start. The old
17029 start `start' can be determined from the current matrix. */
17030 SET_TEXT_POS_FROM_MARKER (new_start, w->start);
17031 start = start_row->minpos;
17032 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
17034 /* Clear the desired matrix for the display below. */
17035 clear_glyph_matrix (w->desired_matrix);
17037 if (CHARPOS (new_start) <= CHARPOS (start))
17039 /* Don't use this method if the display starts with an ellipsis
17040 displayed for invisible text. It's not easy to handle that case
17041 below, and it's certainly not worth the effort since this is
17042 not a frequent case. */
17043 if (in_ellipses_for_invisible_text_p (&start_row->start, w))
17044 return 0;
17046 IF_DEBUG (debug_method_add (w, "twu1"));
17048 /* Display up to a row that can be reused. The variable
17049 last_text_row is set to the last row displayed that displays
17050 text. Note that it.vpos == 0 if or if not there is a
17051 header-line; it's not the same as the MATRIX_ROW_VPOS! */
17052 start_display (&it, w, new_start);
17053 w->cursor.vpos = -1;
17054 last_text_row = last_reused_text_row = NULL;
17056 while (it.current_y < it.last_visible_y && !f->fonts_changed)
17058 /* If we have reached into the characters in the START row,
17059 that means the line boundaries have changed. So we
17060 can't start copying with the row START. Maybe it will
17061 work to start copying with the following row. */
17062 while (IT_CHARPOS (it) > CHARPOS (start))
17064 /* Advance to the next row as the "start". */
17065 start_row++;
17066 start = start_row->minpos;
17067 /* If there are no more rows to try, or just one, give up. */
17068 if (start_row == MATRIX_MODE_LINE_ROW (w->current_matrix) - 1
17069 || w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row)
17070 || CHARPOS (start) == ZV)
17072 clear_glyph_matrix (w->desired_matrix);
17073 return 0;
17076 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
17078 /* If we have reached alignment, we can copy the rest of the
17079 rows. */
17080 if (IT_CHARPOS (it) == CHARPOS (start)
17081 /* Don't accept "alignment" inside a display vector,
17082 since start_row could have started in the middle of
17083 that same display vector (thus their character
17084 positions match), and we have no way of telling if
17085 that is the case. */
17086 && it.current.dpvec_index < 0)
17087 break;
17089 it.glyph_row->reversed_p = false;
17090 if (display_line (&it))
17091 last_text_row = it.glyph_row - 1;
17095 /* A value of current_y < last_visible_y means that we stopped
17096 at the previous window start, which in turn means that we
17097 have at least one reusable row. */
17098 if (it.current_y < it.last_visible_y)
17100 struct glyph_row *row;
17102 /* IT.vpos always starts from 0; it counts text lines. */
17103 nrows_scrolled = it.vpos - (start_row - MATRIX_FIRST_TEXT_ROW (w->current_matrix));
17105 /* Find PT if not already found in the lines displayed. */
17106 if (w->cursor.vpos < 0)
17108 int dy = it.current_y - start_row->y;
17110 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
17111 row = row_containing_pos (w, PT, row, NULL, dy);
17112 if (row)
17113 set_cursor_from_row (w, row, w->current_matrix, 0, 0,
17114 dy, nrows_scrolled);
17115 else
17117 clear_glyph_matrix (w->desired_matrix);
17118 return 0;
17122 /* Scroll the display. Do it before the current matrix is
17123 changed. The problem here is that update has not yet
17124 run, i.e. part of the current matrix is not up to date.
17125 scroll_run_hook will clear the cursor, and use the
17126 current matrix to get the height of the row the cursor is
17127 in. */
17128 run.current_y = start_row->y;
17129 run.desired_y = it.current_y;
17130 run.height = it.last_visible_y - it.current_y;
17132 if (run.height > 0 && run.current_y != run.desired_y)
17134 update_begin (f);
17135 FRAME_RIF (f)->update_window_begin_hook (w);
17136 FRAME_RIF (f)->clear_window_mouse_face (w);
17137 FRAME_RIF (f)->scroll_run_hook (w, &run);
17138 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
17139 update_end (f);
17142 /* Shift current matrix down by nrows_scrolled lines. */
17143 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
17144 rotate_matrix (w->current_matrix,
17145 start_vpos,
17146 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
17147 nrows_scrolled);
17149 /* Disable lines that must be updated. */
17150 for (i = 0; i < nrows_scrolled; ++i)
17151 (start_row + i)->enabled_p = false;
17153 /* Re-compute Y positions. */
17154 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
17155 max_y = it.last_visible_y;
17156 for (row = start_row + nrows_scrolled;
17157 row < bottom_row;
17158 ++row)
17160 row->y = it.current_y;
17161 row->visible_height = row->height;
17163 if (row->y < min_y)
17164 row->visible_height -= min_y - row->y;
17165 if (row->y + row->height > max_y)
17166 row->visible_height -= row->y + row->height - max_y;
17167 if (row->fringe_bitmap_periodic_p)
17168 row->redraw_fringe_bitmaps_p = 1;
17170 it.current_y += row->height;
17172 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
17173 last_reused_text_row = row;
17174 if (MATRIX_ROW_BOTTOM_Y (row) >= it.last_visible_y)
17175 break;
17178 /* Disable lines in the current matrix which are now
17179 below the window. */
17180 for (++row; row < bottom_row; ++row)
17181 row->enabled_p = row->mode_line_p = 0;
17184 /* Update window_end_pos etc.; last_reused_text_row is the last
17185 reused row from the current matrix containing text, if any.
17186 The value of last_text_row is the last displayed line
17187 containing text. */
17188 if (last_reused_text_row)
17189 adjust_window_ends (w, last_reused_text_row, 1);
17190 else if (last_text_row)
17191 adjust_window_ends (w, last_text_row, 0);
17192 else
17194 /* This window must be completely empty. */
17195 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
17196 w->window_end_pos = Z - ZV;
17197 w->window_end_vpos = 0;
17199 w->window_end_valid = 0;
17201 /* Update hint: don't try scrolling again in update_window. */
17202 w->desired_matrix->no_scrolling_p = 1;
17204 #ifdef GLYPH_DEBUG
17205 debug_method_add (w, "try_window_reusing_current_matrix 1");
17206 #endif
17207 return 1;
17209 else if (CHARPOS (new_start) > CHARPOS (start))
17211 struct glyph_row *pt_row, *row;
17212 struct glyph_row *first_reusable_row;
17213 struct glyph_row *first_row_to_display;
17214 int dy;
17215 int yb = window_text_bottom_y (w);
17217 /* Find the row starting at new_start, if there is one. Don't
17218 reuse a partially visible line at the end. */
17219 first_reusable_row = start_row;
17220 while (first_reusable_row->enabled_p
17221 && MATRIX_ROW_BOTTOM_Y (first_reusable_row) < yb
17222 && (MATRIX_ROW_START_CHARPOS (first_reusable_row)
17223 < CHARPOS (new_start)))
17224 ++first_reusable_row;
17226 /* Give up if there is no row to reuse. */
17227 if (MATRIX_ROW_BOTTOM_Y (first_reusable_row) >= yb
17228 || !first_reusable_row->enabled_p
17229 || (MATRIX_ROW_START_CHARPOS (first_reusable_row)
17230 != CHARPOS (new_start)))
17231 return 0;
17233 /* We can reuse fully visible rows beginning with
17234 first_reusable_row to the end of the window. Set
17235 first_row_to_display to the first row that cannot be reused.
17236 Set pt_row to the row containing point, if there is any. */
17237 pt_row = NULL;
17238 for (first_row_to_display = first_reusable_row;
17239 MATRIX_ROW_BOTTOM_Y (first_row_to_display) < yb;
17240 ++first_row_to_display)
17242 if (PT >= MATRIX_ROW_START_CHARPOS (first_row_to_display)
17243 && (PT < MATRIX_ROW_END_CHARPOS (first_row_to_display)
17244 || (PT == MATRIX_ROW_END_CHARPOS (first_row_to_display)
17245 && first_row_to_display->ends_at_zv_p
17246 && pt_row == NULL)))
17247 pt_row = first_row_to_display;
17250 /* Start displaying at the start of first_row_to_display. */
17251 eassert (first_row_to_display->y < yb);
17252 init_to_row_start (&it, w, first_row_to_display);
17254 nrows_scrolled = (MATRIX_ROW_VPOS (first_reusable_row, w->current_matrix)
17255 - start_vpos);
17256 it.vpos = (MATRIX_ROW_VPOS (first_row_to_display, w->current_matrix)
17257 - nrows_scrolled);
17258 it.current_y = (first_row_to_display->y - first_reusable_row->y
17259 + WINDOW_HEADER_LINE_HEIGHT (w));
17261 /* Display lines beginning with first_row_to_display in the
17262 desired matrix. Set last_text_row to the last row displayed
17263 that displays text. */
17264 it.glyph_row = MATRIX_ROW (w->desired_matrix, it.vpos);
17265 if (pt_row == NULL)
17266 w->cursor.vpos = -1;
17267 last_text_row = NULL;
17268 while (it.current_y < it.last_visible_y && !f->fonts_changed)
17269 if (display_line (&it))
17270 last_text_row = it.glyph_row - 1;
17272 /* If point is in a reused row, adjust y and vpos of the cursor
17273 position. */
17274 if (pt_row)
17276 w->cursor.vpos -= nrows_scrolled;
17277 w->cursor.y -= first_reusable_row->y - start_row->y;
17280 /* Give up if point isn't in a row displayed or reused. (This
17281 also handles the case where w->cursor.vpos < nrows_scrolled
17282 after the calls to display_line, which can happen with scroll
17283 margins. See bug#1295.) */
17284 if (w->cursor.vpos < 0)
17286 clear_glyph_matrix (w->desired_matrix);
17287 return 0;
17290 /* Scroll the display. */
17291 run.current_y = first_reusable_row->y;
17292 run.desired_y = WINDOW_HEADER_LINE_HEIGHT (w);
17293 run.height = it.last_visible_y - run.current_y;
17294 dy = run.current_y - run.desired_y;
17296 if (run.height)
17298 update_begin (f);
17299 FRAME_RIF (f)->update_window_begin_hook (w);
17300 FRAME_RIF (f)->clear_window_mouse_face (w);
17301 FRAME_RIF (f)->scroll_run_hook (w, &run);
17302 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
17303 update_end (f);
17306 /* Adjust Y positions of reused rows. */
17307 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
17308 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
17309 max_y = it.last_visible_y;
17310 for (row = first_reusable_row; row < first_row_to_display; ++row)
17312 row->y -= dy;
17313 row->visible_height = row->height;
17314 if (row->y < min_y)
17315 row->visible_height -= min_y - row->y;
17316 if (row->y + row->height > max_y)
17317 row->visible_height -= row->y + row->height - max_y;
17318 if (row->fringe_bitmap_periodic_p)
17319 row->redraw_fringe_bitmaps_p = 1;
17322 /* Scroll the current matrix. */
17323 eassert (nrows_scrolled > 0);
17324 rotate_matrix (w->current_matrix,
17325 start_vpos,
17326 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
17327 -nrows_scrolled);
17329 /* Disable rows not reused. */
17330 for (row -= nrows_scrolled; row < bottom_row; ++row)
17331 row->enabled_p = false;
17333 /* Point may have moved to a different line, so we cannot assume that
17334 the previous cursor position is valid; locate the correct row. */
17335 if (pt_row)
17337 for (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
17338 row < bottom_row
17339 && PT >= MATRIX_ROW_END_CHARPOS (row)
17340 && !row->ends_at_zv_p;
17341 row++)
17343 w->cursor.vpos++;
17344 w->cursor.y = row->y;
17346 if (row < bottom_row)
17348 /* Can't simply scan the row for point with
17349 bidi-reordered glyph rows. Let set_cursor_from_row
17350 figure out where to put the cursor, and if it fails,
17351 give up. */
17352 if (!NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering)))
17354 if (!set_cursor_from_row (w, row, w->current_matrix,
17355 0, 0, 0, 0))
17357 clear_glyph_matrix (w->desired_matrix);
17358 return 0;
17361 else
17363 struct glyph *glyph = row->glyphs[TEXT_AREA] + w->cursor.hpos;
17364 struct glyph *end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
17366 for (; glyph < end
17367 && (!BUFFERP (glyph->object)
17368 || glyph->charpos < PT);
17369 glyph++)
17371 w->cursor.hpos++;
17372 w->cursor.x += glyph->pixel_width;
17378 /* Adjust window end. A null value of last_text_row means that
17379 the window end is in reused rows which in turn means that
17380 only its vpos can have changed. */
17381 if (last_text_row)
17382 adjust_window_ends (w, last_text_row, 0);
17383 else
17384 w->window_end_vpos -= nrows_scrolled;
17386 w->window_end_valid = 0;
17387 w->desired_matrix->no_scrolling_p = 1;
17389 #ifdef GLYPH_DEBUG
17390 debug_method_add (w, "try_window_reusing_current_matrix 2");
17391 #endif
17392 return 1;
17395 return 0;
17400 /************************************************************************
17401 Window redisplay reusing current matrix when buffer has changed
17402 ************************************************************************/
17404 static struct glyph_row *find_last_unchanged_at_beg_row (struct window *);
17405 static struct glyph_row *find_first_unchanged_at_end_row (struct window *,
17406 ptrdiff_t *, ptrdiff_t *);
17407 static struct glyph_row *
17408 find_last_row_displaying_text (struct glyph_matrix *, struct it *,
17409 struct glyph_row *);
17412 /* Return the last row in MATRIX displaying text. If row START is
17413 non-null, start searching with that row. IT gives the dimensions
17414 of the display. Value is null if matrix is empty; otherwise it is
17415 a pointer to the row found. */
17417 static struct glyph_row *
17418 find_last_row_displaying_text (struct glyph_matrix *matrix, struct it *it,
17419 struct glyph_row *start)
17421 struct glyph_row *row, *row_found;
17423 /* Set row_found to the last row in IT->w's current matrix
17424 displaying text. The loop looks funny but think of partially
17425 visible lines. */
17426 row_found = NULL;
17427 row = start ? start : MATRIX_FIRST_TEXT_ROW (matrix);
17428 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
17430 eassert (row->enabled_p);
17431 row_found = row;
17432 if (MATRIX_ROW_BOTTOM_Y (row) >= it->last_visible_y)
17433 break;
17434 ++row;
17437 return row_found;
17441 /* Return the last row in the current matrix of W that is not affected
17442 by changes at the start of current_buffer that occurred since W's
17443 current matrix was built. Value is null if no such row exists.
17445 BEG_UNCHANGED us the number of characters unchanged at the start of
17446 current_buffer. BEG + BEG_UNCHANGED is the buffer position of the
17447 first changed character in current_buffer. Characters at positions <
17448 BEG + BEG_UNCHANGED are at the same buffer positions as they were
17449 when the current matrix was built. */
17451 static struct glyph_row *
17452 find_last_unchanged_at_beg_row (struct window *w)
17454 ptrdiff_t first_changed_pos = BEG + BEG_UNCHANGED;
17455 struct glyph_row *row;
17456 struct glyph_row *row_found = NULL;
17457 int yb = window_text_bottom_y (w);
17459 /* Find the last row displaying unchanged text. */
17460 for (row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
17461 MATRIX_ROW_DISPLAYS_TEXT_P (row)
17462 && MATRIX_ROW_START_CHARPOS (row) < first_changed_pos;
17463 ++row)
17465 if (/* If row ends before first_changed_pos, it is unchanged,
17466 except in some case. */
17467 MATRIX_ROW_END_CHARPOS (row) <= first_changed_pos
17468 /* When row ends in ZV and we write at ZV it is not
17469 unchanged. */
17470 && !row->ends_at_zv_p
17471 /* When first_changed_pos is the end of a continued line,
17472 row is not unchanged because it may be no longer
17473 continued. */
17474 && !(MATRIX_ROW_END_CHARPOS (row) == first_changed_pos
17475 && (row->continued_p
17476 || row->exact_window_width_line_p))
17477 /* If ROW->end is beyond ZV, then ROW->end is outdated and
17478 needs to be recomputed, so don't consider this row as
17479 unchanged. This happens when the last line was
17480 bidi-reordered and was killed immediately before this
17481 redisplay cycle. In that case, ROW->end stores the
17482 buffer position of the first visual-order character of
17483 the killed text, which is now beyond ZV. */
17484 && CHARPOS (row->end.pos) <= ZV)
17485 row_found = row;
17487 /* Stop if last visible row. */
17488 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
17489 break;
17492 return row_found;
17496 /* Find the first glyph row in the current matrix of W that is not
17497 affected by changes at the end of current_buffer since the
17498 time W's current matrix was built.
17500 Return in *DELTA the number of chars by which buffer positions in
17501 unchanged text at the end of current_buffer must be adjusted.
17503 Return in *DELTA_BYTES the corresponding number of bytes.
17505 Value is null if no such row exists, i.e. all rows are affected by
17506 changes. */
17508 static struct glyph_row *
17509 find_first_unchanged_at_end_row (struct window *w,
17510 ptrdiff_t *delta, ptrdiff_t *delta_bytes)
17512 struct glyph_row *row;
17513 struct glyph_row *row_found = NULL;
17515 *delta = *delta_bytes = 0;
17517 /* Display must not have been paused, otherwise the current matrix
17518 is not up to date. */
17519 eassert (w->window_end_valid);
17521 /* A value of window_end_pos >= END_UNCHANGED means that the window
17522 end is in the range of changed text. If so, there is no
17523 unchanged row at the end of W's current matrix. */
17524 if (w->window_end_pos >= END_UNCHANGED)
17525 return NULL;
17527 /* Set row to the last row in W's current matrix displaying text. */
17528 row = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
17530 /* If matrix is entirely empty, no unchanged row exists. */
17531 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
17533 /* The value of row is the last glyph row in the matrix having a
17534 meaningful buffer position in it. The end position of row
17535 corresponds to window_end_pos. This allows us to translate
17536 buffer positions in the current matrix to current buffer
17537 positions for characters not in changed text. */
17538 ptrdiff_t Z_old =
17539 MATRIX_ROW_END_CHARPOS (row) + w->window_end_pos;
17540 ptrdiff_t Z_BYTE_old =
17541 MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
17542 ptrdiff_t last_unchanged_pos, last_unchanged_pos_old;
17543 struct glyph_row *first_text_row
17544 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
17546 *delta = Z - Z_old;
17547 *delta_bytes = Z_BYTE - Z_BYTE_old;
17549 /* Set last_unchanged_pos to the buffer position of the last
17550 character in the buffer that has not been changed. Z is the
17551 index + 1 of the last character in current_buffer, i.e. by
17552 subtracting END_UNCHANGED we get the index of the last
17553 unchanged character, and we have to add BEG to get its buffer
17554 position. */
17555 last_unchanged_pos = Z - END_UNCHANGED + BEG;
17556 last_unchanged_pos_old = last_unchanged_pos - *delta;
17558 /* Search backward from ROW for a row displaying a line that
17559 starts at a minimum position >= last_unchanged_pos_old. */
17560 for (; row > first_text_row; --row)
17562 /* This used to abort, but it can happen.
17563 It is ok to just stop the search instead here. KFS. */
17564 if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
17565 break;
17567 if (MATRIX_ROW_START_CHARPOS (row) >= last_unchanged_pos_old)
17568 row_found = row;
17572 eassert (!row_found || MATRIX_ROW_DISPLAYS_TEXT_P (row_found));
17574 return row_found;
17578 /* Make sure that glyph rows in the current matrix of window W
17579 reference the same glyph memory as corresponding rows in the
17580 frame's frame matrix. This function is called after scrolling W's
17581 current matrix on a terminal frame in try_window_id and
17582 try_window_reusing_current_matrix. */
17584 static void
17585 sync_frame_with_window_matrix_rows (struct window *w)
17587 struct frame *f = XFRAME (w->frame);
17588 struct glyph_row *window_row, *window_row_end, *frame_row;
17590 /* Preconditions: W must be a leaf window and full-width. Its frame
17591 must have a frame matrix. */
17592 eassert (BUFFERP (w->contents));
17593 eassert (WINDOW_FULL_WIDTH_P (w));
17594 eassert (!FRAME_WINDOW_P (f));
17596 /* If W is a full-width window, glyph pointers in W's current matrix
17597 have, by definition, to be the same as glyph pointers in the
17598 corresponding frame matrix. Note that frame matrices have no
17599 marginal areas (see build_frame_matrix). */
17600 window_row = w->current_matrix->rows;
17601 window_row_end = window_row + w->current_matrix->nrows;
17602 frame_row = f->current_matrix->rows + WINDOW_TOP_EDGE_LINE (w);
17603 while (window_row < window_row_end)
17605 struct glyph *start = window_row->glyphs[LEFT_MARGIN_AREA];
17606 struct glyph *end = window_row->glyphs[LAST_AREA];
17608 frame_row->glyphs[LEFT_MARGIN_AREA] = start;
17609 frame_row->glyphs[TEXT_AREA] = start;
17610 frame_row->glyphs[RIGHT_MARGIN_AREA] = end;
17611 frame_row->glyphs[LAST_AREA] = end;
17613 /* Disable frame rows whose corresponding window rows have
17614 been disabled in try_window_id. */
17615 if (!window_row->enabled_p)
17616 frame_row->enabled_p = false;
17618 ++window_row, ++frame_row;
17623 /* Find the glyph row in window W containing CHARPOS. Consider all
17624 rows between START and END (not inclusive). END null means search
17625 all rows to the end of the display area of W. Value is the row
17626 containing CHARPOS or null. */
17628 struct glyph_row *
17629 row_containing_pos (struct window *w, ptrdiff_t charpos,
17630 struct glyph_row *start, struct glyph_row *end, int dy)
17632 struct glyph_row *row = start;
17633 struct glyph_row *best_row = NULL;
17634 ptrdiff_t mindif = BUF_ZV (XBUFFER (w->contents)) + 1;
17635 int last_y;
17637 /* If we happen to start on a header-line, skip that. */
17638 if (row->mode_line_p)
17639 ++row;
17641 if ((end && row >= end) || !row->enabled_p)
17642 return NULL;
17644 last_y = window_text_bottom_y (w) - dy;
17646 while (1)
17648 /* Give up if we have gone too far. */
17649 if (end && row >= end)
17650 return NULL;
17651 /* This formerly returned if they were equal.
17652 I think that both quantities are of a "last plus one" type;
17653 if so, when they are equal, the row is within the screen. -- rms. */
17654 if (MATRIX_ROW_BOTTOM_Y (row) > last_y)
17655 return NULL;
17657 /* If it is in this row, return this row. */
17658 if (! (MATRIX_ROW_END_CHARPOS (row) < charpos
17659 || (MATRIX_ROW_END_CHARPOS (row) == charpos
17660 /* The end position of a row equals the start
17661 position of the next row. If CHARPOS is there, we
17662 would rather consider it displayed in the next
17663 line, except when this line ends in ZV. */
17664 && !row_for_charpos_p (row, charpos)))
17665 && charpos >= MATRIX_ROW_START_CHARPOS (row))
17667 struct glyph *g;
17669 if (NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering))
17670 || (!best_row && !row->continued_p))
17671 return row;
17672 /* In bidi-reordered rows, there could be several rows whose
17673 edges surround CHARPOS, all of these rows belonging to
17674 the same continued line. We need to find the row which
17675 fits CHARPOS the best. */
17676 for (g = row->glyphs[TEXT_AREA];
17677 g < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
17678 g++)
17680 if (!STRINGP (g->object))
17682 if (g->charpos > 0 && eabs (g->charpos - charpos) < mindif)
17684 mindif = eabs (g->charpos - charpos);
17685 best_row = row;
17686 /* Exact match always wins. */
17687 if (mindif == 0)
17688 return best_row;
17693 else if (best_row && !row->continued_p)
17694 return best_row;
17695 ++row;
17700 /* Try to redisplay window W by reusing its existing display. W's
17701 current matrix must be up to date when this function is called,
17702 i.e. window_end_valid must be nonzero.
17704 Value is
17706 >= 1 if successful, i.e. display has been updated
17707 specifically:
17708 1 means the changes were in front of a newline that precedes
17709 the window start, and the whole current matrix was reused
17710 2 means the changes were after the last position displayed
17711 in the window, and the whole current matrix was reused
17712 3 means portions of the current matrix were reused, while
17713 some of the screen lines were redrawn
17714 -1 if redisplay with same window start is known not to succeed
17715 0 if otherwise unsuccessful
17717 The following steps are performed:
17719 1. Find the last row in the current matrix of W that is not
17720 affected by changes at the start of current_buffer. If no such row
17721 is found, give up.
17723 2. Find the first row in W's current matrix that is not affected by
17724 changes at the end of current_buffer. Maybe there is no such row.
17726 3. Display lines beginning with the row + 1 found in step 1 to the
17727 row found in step 2 or, if step 2 didn't find a row, to the end of
17728 the window.
17730 4. If cursor is not known to appear on the window, give up.
17732 5. If display stopped at the row found in step 2, scroll the
17733 display and current matrix as needed.
17735 6. Maybe display some lines at the end of W, if we must. This can
17736 happen under various circumstances, like a partially visible line
17737 becoming fully visible, or because newly displayed lines are displayed
17738 in smaller font sizes.
17740 7. Update W's window end information. */
17742 static int
17743 try_window_id (struct window *w)
17745 struct frame *f = XFRAME (w->frame);
17746 struct glyph_matrix *current_matrix = w->current_matrix;
17747 struct glyph_matrix *desired_matrix = w->desired_matrix;
17748 struct glyph_row *last_unchanged_at_beg_row;
17749 struct glyph_row *first_unchanged_at_end_row;
17750 struct glyph_row *row;
17751 struct glyph_row *bottom_row;
17752 int bottom_vpos;
17753 struct it it;
17754 ptrdiff_t delta = 0, delta_bytes = 0, stop_pos;
17755 int dvpos, dy;
17756 struct text_pos start_pos;
17757 struct run run;
17758 int first_unchanged_at_end_vpos = 0;
17759 struct glyph_row *last_text_row, *last_text_row_at_end;
17760 struct text_pos start;
17761 ptrdiff_t first_changed_charpos, last_changed_charpos;
17763 #ifdef GLYPH_DEBUG
17764 if (inhibit_try_window_id)
17765 return 0;
17766 #endif
17768 /* This is handy for debugging. */
17769 #if 0
17770 #define GIVE_UP(X) \
17771 do { \
17772 fprintf (stderr, "try_window_id give up %d\n", (X)); \
17773 return 0; \
17774 } while (0)
17775 #else
17776 #define GIVE_UP(X) return 0
17777 #endif
17779 SET_TEXT_POS_FROM_MARKER (start, w->start);
17781 /* Don't use this for mini-windows because these can show
17782 messages and mini-buffers, and we don't handle that here. */
17783 if (MINI_WINDOW_P (w))
17784 GIVE_UP (1);
17786 /* This flag is used to prevent redisplay optimizations. */
17787 if (windows_or_buffers_changed || f->cursor_type_changed)
17788 GIVE_UP (2);
17790 /* This function's optimizations cannot be used if overlays have
17791 changed in the buffer displayed by the window, so give up if they
17792 have. */
17793 if (w->last_overlay_modified != OVERLAY_MODIFF)
17794 GIVE_UP (21);
17796 /* Verify that narrowing has not changed.
17797 Also verify that we were not told to prevent redisplay optimizations.
17798 It would be nice to further
17799 reduce the number of cases where this prevents try_window_id. */
17800 if (current_buffer->clip_changed
17801 || current_buffer->prevent_redisplay_optimizations_p)
17802 GIVE_UP (3);
17804 /* Window must either use window-based redisplay or be full width. */
17805 if (!FRAME_WINDOW_P (f)
17806 && (!FRAME_LINE_INS_DEL_OK (f)
17807 || !WINDOW_FULL_WIDTH_P (w)))
17808 GIVE_UP (4);
17810 /* Give up if point is known NOT to appear in W. */
17811 if (PT < CHARPOS (start))
17812 GIVE_UP (5);
17814 /* Another way to prevent redisplay optimizations. */
17815 if (w->last_modified == 0)
17816 GIVE_UP (6);
17818 /* Verify that window is not hscrolled. */
17819 if (w->hscroll != 0)
17820 GIVE_UP (7);
17822 /* Verify that display wasn't paused. */
17823 if (!w->window_end_valid)
17824 GIVE_UP (8);
17826 /* Likewise if highlighting trailing whitespace. */
17827 if (!NILP (Vshow_trailing_whitespace))
17828 GIVE_UP (11);
17830 /* Can't use this if overlay arrow position and/or string have
17831 changed. */
17832 if (overlay_arrows_changed_p ())
17833 GIVE_UP (12);
17835 /* When word-wrap is on, adding a space to the first word of a
17836 wrapped line can change the wrap position, altering the line
17837 above it. It might be worthwhile to handle this more
17838 intelligently, but for now just redisplay from scratch. */
17839 if (!NILP (BVAR (XBUFFER (w->contents), word_wrap)))
17840 GIVE_UP (21);
17842 /* Under bidi reordering, adding or deleting a character in the
17843 beginning of a paragraph, before the first strong directional
17844 character, can change the base direction of the paragraph (unless
17845 the buffer specifies a fixed paragraph direction), which will
17846 require to redisplay the whole paragraph. It might be worthwhile
17847 to find the paragraph limits and widen the range of redisplayed
17848 lines to that, but for now just give up this optimization and
17849 redisplay from scratch. */
17850 if (!NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering))
17851 && NILP (BVAR (XBUFFER (w->contents), bidi_paragraph_direction)))
17852 GIVE_UP (22);
17854 /* Make sure beg_unchanged and end_unchanged are up to date. Do it
17855 only if buffer has really changed. The reason is that the gap is
17856 initially at Z for freshly visited files. The code below would
17857 set end_unchanged to 0 in that case. */
17858 if (MODIFF > SAVE_MODIFF
17859 /* This seems to happen sometimes after saving a buffer. */
17860 || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
17862 if (GPT - BEG < BEG_UNCHANGED)
17863 BEG_UNCHANGED = GPT - BEG;
17864 if (Z - GPT < END_UNCHANGED)
17865 END_UNCHANGED = Z - GPT;
17868 /* The position of the first and last character that has been changed. */
17869 first_changed_charpos = BEG + BEG_UNCHANGED;
17870 last_changed_charpos = Z - END_UNCHANGED;
17872 /* If window starts after a line end, and the last change is in
17873 front of that newline, then changes don't affect the display.
17874 This case happens with stealth-fontification. Note that although
17875 the display is unchanged, glyph positions in the matrix have to
17876 be adjusted, of course. */
17877 row = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
17878 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
17879 && ((last_changed_charpos < CHARPOS (start)
17880 && CHARPOS (start) == BEGV)
17881 || (last_changed_charpos < CHARPOS (start) - 1
17882 && FETCH_BYTE (BYTEPOS (start) - 1) == '\n')))
17884 ptrdiff_t Z_old, Z_delta, Z_BYTE_old, Z_delta_bytes;
17885 struct glyph_row *r0;
17887 /* Compute how many chars/bytes have been added to or removed
17888 from the buffer. */
17889 Z_old = MATRIX_ROW_END_CHARPOS (row) + w->window_end_pos;
17890 Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
17891 Z_delta = Z - Z_old;
17892 Z_delta_bytes = Z_BYTE - Z_BYTE_old;
17894 /* Give up if PT is not in the window. Note that it already has
17895 been checked at the start of try_window_id that PT is not in
17896 front of the window start. */
17897 if (PT >= MATRIX_ROW_END_CHARPOS (row) + Z_delta)
17898 GIVE_UP (13);
17900 /* If window start is unchanged, we can reuse the whole matrix
17901 as is, after adjusting glyph positions. No need to compute
17902 the window end again, since its offset from Z hasn't changed. */
17903 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
17904 if (CHARPOS (start) == MATRIX_ROW_START_CHARPOS (r0) + Z_delta
17905 && BYTEPOS (start) == MATRIX_ROW_START_BYTEPOS (r0) + Z_delta_bytes
17906 /* PT must not be in a partially visible line. */
17907 && !(PT >= MATRIX_ROW_START_CHARPOS (row) + Z_delta
17908 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
17910 /* Adjust positions in the glyph matrix. */
17911 if (Z_delta || Z_delta_bytes)
17913 struct glyph_row *r1
17914 = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
17915 increment_matrix_positions (w->current_matrix,
17916 MATRIX_ROW_VPOS (r0, current_matrix),
17917 MATRIX_ROW_VPOS (r1, current_matrix),
17918 Z_delta, Z_delta_bytes);
17921 /* Set the cursor. */
17922 row = row_containing_pos (w, PT, r0, NULL, 0);
17923 if (row)
17924 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
17925 return 1;
17929 /* Handle the case that changes are all below what is displayed in
17930 the window, and that PT is in the window. This shortcut cannot
17931 be taken if ZV is visible in the window, and text has been added
17932 there that is visible in the window. */
17933 if (first_changed_charpos >= MATRIX_ROW_END_CHARPOS (row)
17934 /* ZV is not visible in the window, or there are no
17935 changes at ZV, actually. */
17936 && (current_matrix->zv > MATRIX_ROW_END_CHARPOS (row)
17937 || first_changed_charpos == last_changed_charpos))
17939 struct glyph_row *r0;
17941 /* Give up if PT is not in the window. Note that it already has
17942 been checked at the start of try_window_id that PT is not in
17943 front of the window start. */
17944 if (PT >= MATRIX_ROW_END_CHARPOS (row))
17945 GIVE_UP (14);
17947 /* If window start is unchanged, we can reuse the whole matrix
17948 as is, without changing glyph positions since no text has
17949 been added/removed in front of the window end. */
17950 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
17951 if (TEXT_POS_EQUAL_P (start, r0->minpos)
17952 /* PT must not be in a partially visible line. */
17953 && !(PT >= MATRIX_ROW_START_CHARPOS (row)
17954 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
17956 /* We have to compute the window end anew since text
17957 could have been added/removed after it. */
17958 w->window_end_pos = Z - MATRIX_ROW_END_CHARPOS (row);
17959 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
17961 /* Set the cursor. */
17962 row = row_containing_pos (w, PT, r0, NULL, 0);
17963 if (row)
17964 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
17965 return 2;
17969 /* Give up if window start is in the changed area.
17971 The condition used to read
17973 (BEG_UNCHANGED + END_UNCHANGED != Z - BEG && ...)
17975 but why that was tested escapes me at the moment. */
17976 if (CHARPOS (start) >= first_changed_charpos
17977 && CHARPOS (start) <= last_changed_charpos)
17978 GIVE_UP (15);
17980 /* Check that window start agrees with the start of the first glyph
17981 row in its current matrix. Check this after we know the window
17982 start is not in changed text, otherwise positions would not be
17983 comparable. */
17984 row = MATRIX_FIRST_TEXT_ROW (current_matrix);
17985 if (!TEXT_POS_EQUAL_P (start, row->minpos))
17986 GIVE_UP (16);
17988 /* Give up if the window ends in strings. Overlay strings
17989 at the end are difficult to handle, so don't try. */
17990 row = MATRIX_ROW (current_matrix, w->window_end_vpos);
17991 if (MATRIX_ROW_START_CHARPOS (row) == MATRIX_ROW_END_CHARPOS (row))
17992 GIVE_UP (20);
17994 /* Compute the position at which we have to start displaying new
17995 lines. Some of the lines at the top of the window might be
17996 reusable because they are not displaying changed text. Find the
17997 last row in W's current matrix not affected by changes at the
17998 start of current_buffer. Value is null if changes start in the
17999 first line of window. */
18000 last_unchanged_at_beg_row = find_last_unchanged_at_beg_row (w);
18001 if (last_unchanged_at_beg_row)
18003 /* Avoid starting to display in the middle of a character, a TAB
18004 for instance. This is easier than to set up the iterator
18005 exactly, and it's not a frequent case, so the additional
18006 effort wouldn't really pay off. */
18007 while ((MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row)
18008 || last_unchanged_at_beg_row->ends_in_newline_from_string_p)
18009 && last_unchanged_at_beg_row > w->current_matrix->rows)
18010 --last_unchanged_at_beg_row;
18012 if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row))
18013 GIVE_UP (17);
18015 if (init_to_row_end (&it, w, last_unchanged_at_beg_row) == 0)
18016 GIVE_UP (18);
18017 start_pos = it.current.pos;
18019 /* Start displaying new lines in the desired matrix at the same
18020 vpos we would use in the current matrix, i.e. below
18021 last_unchanged_at_beg_row. */
18022 it.vpos = 1 + MATRIX_ROW_VPOS (last_unchanged_at_beg_row,
18023 current_matrix);
18024 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
18025 it.current_y = MATRIX_ROW_BOTTOM_Y (last_unchanged_at_beg_row);
18027 eassert (it.hpos == 0 && it.current_x == 0);
18029 else
18031 /* There are no reusable lines at the start of the window.
18032 Start displaying in the first text line. */
18033 start_display (&it, w, start);
18034 it.vpos = it.first_vpos;
18035 start_pos = it.current.pos;
18038 /* Find the first row that is not affected by changes at the end of
18039 the buffer. Value will be null if there is no unchanged row, in
18040 which case we must redisplay to the end of the window. delta
18041 will be set to the value by which buffer positions beginning with
18042 first_unchanged_at_end_row have to be adjusted due to text
18043 changes. */
18044 first_unchanged_at_end_row
18045 = find_first_unchanged_at_end_row (w, &delta, &delta_bytes);
18046 IF_DEBUG (debug_delta = delta);
18047 IF_DEBUG (debug_delta_bytes = delta_bytes);
18049 /* Set stop_pos to the buffer position up to which we will have to
18050 display new lines. If first_unchanged_at_end_row != NULL, this
18051 is the buffer position of the start of the line displayed in that
18052 row. For first_unchanged_at_end_row == NULL, use 0 to indicate
18053 that we don't stop at a buffer position. */
18054 stop_pos = 0;
18055 if (first_unchanged_at_end_row)
18057 eassert (last_unchanged_at_beg_row == NULL
18058 || first_unchanged_at_end_row >= last_unchanged_at_beg_row);
18060 /* If this is a continuation line, move forward to the next one
18061 that isn't. Changes in lines above affect this line.
18062 Caution: this may move first_unchanged_at_end_row to a row
18063 not displaying text. */
18064 while (MATRIX_ROW_CONTINUATION_LINE_P (first_unchanged_at_end_row)
18065 && MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
18066 && (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
18067 < it.last_visible_y))
18068 ++first_unchanged_at_end_row;
18070 if (!MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
18071 || (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
18072 >= it.last_visible_y))
18073 first_unchanged_at_end_row = NULL;
18074 else
18076 stop_pos = (MATRIX_ROW_START_CHARPOS (first_unchanged_at_end_row)
18077 + delta);
18078 first_unchanged_at_end_vpos
18079 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, current_matrix);
18080 eassert (stop_pos >= Z - END_UNCHANGED);
18083 else if (last_unchanged_at_beg_row == NULL)
18084 GIVE_UP (19);
18087 #ifdef GLYPH_DEBUG
18089 /* Either there is no unchanged row at the end, or the one we have
18090 now displays text. This is a necessary condition for the window
18091 end pos calculation at the end of this function. */
18092 eassert (first_unchanged_at_end_row == NULL
18093 || MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
18095 debug_last_unchanged_at_beg_vpos
18096 = (last_unchanged_at_beg_row
18097 ? MATRIX_ROW_VPOS (last_unchanged_at_beg_row, current_matrix)
18098 : -1);
18099 debug_first_unchanged_at_end_vpos = first_unchanged_at_end_vpos;
18101 #endif /* GLYPH_DEBUG */
18104 /* Display new lines. Set last_text_row to the last new line
18105 displayed which has text on it, i.e. might end up as being the
18106 line where the window_end_vpos is. */
18107 w->cursor.vpos = -1;
18108 last_text_row = NULL;
18109 overlay_arrow_seen = 0;
18110 if (it.current_y < it.last_visible_y
18111 && !f->fonts_changed
18112 && (first_unchanged_at_end_row == NULL
18113 || IT_CHARPOS (it) < stop_pos))
18114 it.glyph_row->reversed_p = false;
18115 while (it.current_y < it.last_visible_y
18116 && !f->fonts_changed
18117 && (first_unchanged_at_end_row == NULL
18118 || IT_CHARPOS (it) < stop_pos))
18120 if (display_line (&it))
18121 last_text_row = it.glyph_row - 1;
18124 if (f->fonts_changed)
18125 return -1;
18127 /* The redisplay iterations in display_line above could have
18128 triggered font-lock, which could have done something that
18129 invalidates IT->w window's end-point information, on which we
18130 rely below. E.g., one package, which will remain unnamed, used
18131 to install a font-lock-fontify-region-function that called
18132 bury-buffer, whose side effect is to switch the buffer displayed
18133 by IT->w, and that predictably resets IT->w's window_end_valid
18134 flag, which we already tested at the entry to this function.
18135 Amply punish such packages/modes by giving up on this
18136 optimization in those cases. */
18137 if (!w->window_end_valid)
18139 clear_glyph_matrix (w->desired_matrix);
18140 return -1;
18143 /* Compute differences in buffer positions, y-positions etc. for
18144 lines reused at the bottom of the window. Compute what we can
18145 scroll. */
18146 if (first_unchanged_at_end_row
18147 /* No lines reused because we displayed everything up to the
18148 bottom of the window. */
18149 && it.current_y < it.last_visible_y)
18151 dvpos = (it.vpos
18152 - MATRIX_ROW_VPOS (first_unchanged_at_end_row,
18153 current_matrix));
18154 dy = it.current_y - first_unchanged_at_end_row->y;
18155 run.current_y = first_unchanged_at_end_row->y;
18156 run.desired_y = run.current_y + dy;
18157 run.height = it.last_visible_y - max (run.current_y, run.desired_y);
18159 else
18161 delta = delta_bytes = dvpos = dy
18162 = run.current_y = run.desired_y = run.height = 0;
18163 first_unchanged_at_end_row = NULL;
18165 IF_DEBUG ((debug_dvpos = dvpos, debug_dy = dy));
18168 /* Find the cursor if not already found. We have to decide whether
18169 PT will appear on this window (it sometimes doesn't, but this is
18170 not a very frequent case.) This decision has to be made before
18171 the current matrix is altered. A value of cursor.vpos < 0 means
18172 that PT is either in one of the lines beginning at
18173 first_unchanged_at_end_row or below the window. Don't care for
18174 lines that might be displayed later at the window end; as
18175 mentioned, this is not a frequent case. */
18176 if (w->cursor.vpos < 0)
18178 /* Cursor in unchanged rows at the top? */
18179 if (PT < CHARPOS (start_pos)
18180 && last_unchanged_at_beg_row)
18182 row = row_containing_pos (w, PT,
18183 MATRIX_FIRST_TEXT_ROW (w->current_matrix),
18184 last_unchanged_at_beg_row + 1, 0);
18185 if (row)
18186 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
18189 /* Start from first_unchanged_at_end_row looking for PT. */
18190 else if (first_unchanged_at_end_row)
18192 row = row_containing_pos (w, PT - delta,
18193 first_unchanged_at_end_row, NULL, 0);
18194 if (row)
18195 set_cursor_from_row (w, row, w->current_matrix, delta,
18196 delta_bytes, dy, dvpos);
18199 /* Give up if cursor was not found. */
18200 if (w->cursor.vpos < 0)
18202 clear_glyph_matrix (w->desired_matrix);
18203 return -1;
18207 /* Don't let the cursor end in the scroll margins. */
18209 int this_scroll_margin, cursor_height;
18210 int frame_line_height = default_line_pixel_height (w);
18211 int window_total_lines
18212 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (it.f) / frame_line_height;
18214 this_scroll_margin =
18215 max (0, min (scroll_margin, window_total_lines / 4));
18216 this_scroll_margin *= frame_line_height;
18217 cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
18219 if ((w->cursor.y < this_scroll_margin
18220 && CHARPOS (start) > BEGV)
18221 /* Old redisplay didn't take scroll margin into account at the bottom,
18222 but then global-hl-line-mode doesn't scroll. KFS 2004-06-14 */
18223 || (w->cursor.y + (make_cursor_line_fully_visible_p
18224 ? cursor_height + this_scroll_margin
18225 : 1)) > it.last_visible_y)
18227 w->cursor.vpos = -1;
18228 clear_glyph_matrix (w->desired_matrix);
18229 return -1;
18233 /* Scroll the display. Do it before changing the current matrix so
18234 that xterm.c doesn't get confused about where the cursor glyph is
18235 found. */
18236 if (dy && run.height)
18238 update_begin (f);
18240 if (FRAME_WINDOW_P (f))
18242 FRAME_RIF (f)->update_window_begin_hook (w);
18243 FRAME_RIF (f)->clear_window_mouse_face (w);
18244 FRAME_RIF (f)->scroll_run_hook (w, &run);
18245 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
18247 else
18249 /* Terminal frame. In this case, dvpos gives the number of
18250 lines to scroll by; dvpos < 0 means scroll up. */
18251 int from_vpos
18252 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, w->current_matrix);
18253 int from = WINDOW_TOP_EDGE_LINE (w) + from_vpos;
18254 int end = (WINDOW_TOP_EDGE_LINE (w)
18255 + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0)
18256 + window_internal_height (w));
18258 #if defined (HAVE_GPM) || defined (MSDOS)
18259 x_clear_window_mouse_face (w);
18260 #endif
18261 /* Perform the operation on the screen. */
18262 if (dvpos > 0)
18264 /* Scroll last_unchanged_at_beg_row to the end of the
18265 window down dvpos lines. */
18266 set_terminal_window (f, end);
18268 /* On dumb terminals delete dvpos lines at the end
18269 before inserting dvpos empty lines. */
18270 if (!FRAME_SCROLL_REGION_OK (f))
18271 ins_del_lines (f, end - dvpos, -dvpos);
18273 /* Insert dvpos empty lines in front of
18274 last_unchanged_at_beg_row. */
18275 ins_del_lines (f, from, dvpos);
18277 else if (dvpos < 0)
18279 /* Scroll up last_unchanged_at_beg_vpos to the end of
18280 the window to last_unchanged_at_beg_vpos - |dvpos|. */
18281 set_terminal_window (f, end);
18283 /* Delete dvpos lines in front of
18284 last_unchanged_at_beg_vpos. ins_del_lines will set
18285 the cursor to the given vpos and emit |dvpos| delete
18286 line sequences. */
18287 ins_del_lines (f, from + dvpos, dvpos);
18289 /* On a dumb terminal insert dvpos empty lines at the
18290 end. */
18291 if (!FRAME_SCROLL_REGION_OK (f))
18292 ins_del_lines (f, end + dvpos, -dvpos);
18295 set_terminal_window (f, 0);
18298 update_end (f);
18301 /* Shift reused rows of the current matrix to the right position.
18302 BOTTOM_ROW is the last + 1 row in the current matrix reserved for
18303 text. */
18304 bottom_row = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
18305 bottom_vpos = MATRIX_ROW_VPOS (bottom_row, current_matrix);
18306 if (dvpos < 0)
18308 rotate_matrix (current_matrix, first_unchanged_at_end_vpos + dvpos,
18309 bottom_vpos, dvpos);
18310 clear_glyph_matrix_rows (current_matrix, bottom_vpos + dvpos,
18311 bottom_vpos);
18313 else if (dvpos > 0)
18315 rotate_matrix (current_matrix, first_unchanged_at_end_vpos,
18316 bottom_vpos, dvpos);
18317 clear_glyph_matrix_rows (current_matrix, first_unchanged_at_end_vpos,
18318 first_unchanged_at_end_vpos + dvpos);
18321 /* For frame-based redisplay, make sure that current frame and window
18322 matrix are in sync with respect to glyph memory. */
18323 if (!FRAME_WINDOW_P (f))
18324 sync_frame_with_window_matrix_rows (w);
18326 /* Adjust buffer positions in reused rows. */
18327 if (delta || delta_bytes)
18328 increment_matrix_positions (current_matrix,
18329 first_unchanged_at_end_vpos + dvpos,
18330 bottom_vpos, delta, delta_bytes);
18332 /* Adjust Y positions. */
18333 if (dy)
18334 shift_glyph_matrix (w, current_matrix,
18335 first_unchanged_at_end_vpos + dvpos,
18336 bottom_vpos, dy);
18338 if (first_unchanged_at_end_row)
18340 first_unchanged_at_end_row += dvpos;
18341 if (first_unchanged_at_end_row->y >= it.last_visible_y
18342 || !MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row))
18343 first_unchanged_at_end_row = NULL;
18346 /* If scrolling up, there may be some lines to display at the end of
18347 the window. */
18348 last_text_row_at_end = NULL;
18349 if (dy < 0)
18351 /* Scrolling up can leave for example a partially visible line
18352 at the end of the window to be redisplayed. */
18353 /* Set last_row to the glyph row in the current matrix where the
18354 window end line is found. It has been moved up or down in
18355 the matrix by dvpos. */
18356 int last_vpos = w->window_end_vpos + dvpos;
18357 struct glyph_row *last_row = MATRIX_ROW (current_matrix, last_vpos);
18359 /* If last_row is the window end line, it should display text. */
18360 eassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_row));
18362 /* If window end line was partially visible before, begin
18363 displaying at that line. Otherwise begin displaying with the
18364 line following it. */
18365 if (MATRIX_ROW_BOTTOM_Y (last_row) - dy >= it.last_visible_y)
18367 init_to_row_start (&it, w, last_row);
18368 it.vpos = last_vpos;
18369 it.current_y = last_row->y;
18371 else
18373 init_to_row_end (&it, w, last_row);
18374 it.vpos = 1 + last_vpos;
18375 it.current_y = MATRIX_ROW_BOTTOM_Y (last_row);
18376 ++last_row;
18379 /* We may start in a continuation line. If so, we have to
18380 get the right continuation_lines_width and current_x. */
18381 it.continuation_lines_width = last_row->continuation_lines_width;
18382 it.hpos = it.current_x = 0;
18384 /* Display the rest of the lines at the window end. */
18385 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
18386 while (it.current_y < it.last_visible_y && !f->fonts_changed)
18388 /* Is it always sure that the display agrees with lines in
18389 the current matrix? I don't think so, so we mark rows
18390 displayed invalid in the current matrix by setting their
18391 enabled_p flag to zero. */
18392 SET_MATRIX_ROW_ENABLED_P (w->current_matrix, it.vpos, false);
18393 if (display_line (&it))
18394 last_text_row_at_end = it.glyph_row - 1;
18398 /* Update window_end_pos and window_end_vpos. */
18399 if (first_unchanged_at_end_row && !last_text_row_at_end)
18401 /* Window end line if one of the preserved rows from the current
18402 matrix. Set row to the last row displaying text in current
18403 matrix starting at first_unchanged_at_end_row, after
18404 scrolling. */
18405 eassert (MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
18406 row = find_last_row_displaying_text (w->current_matrix, &it,
18407 first_unchanged_at_end_row);
18408 eassert (row && MATRIX_ROW_DISPLAYS_TEXT_P (row));
18409 adjust_window_ends (w, row, 1);
18410 eassert (w->window_end_bytepos >= 0);
18411 IF_DEBUG (debug_method_add (w, "A"));
18413 else if (last_text_row_at_end)
18415 adjust_window_ends (w, last_text_row_at_end, 0);
18416 eassert (w->window_end_bytepos >= 0);
18417 IF_DEBUG (debug_method_add (w, "B"));
18419 else if (last_text_row)
18421 /* We have displayed either to the end of the window or at the
18422 end of the window, i.e. the last row with text is to be found
18423 in the desired matrix. */
18424 adjust_window_ends (w, last_text_row, 0);
18425 eassert (w->window_end_bytepos >= 0);
18427 else if (first_unchanged_at_end_row == NULL
18428 && last_text_row == NULL
18429 && last_text_row_at_end == NULL)
18431 /* Displayed to end of window, but no line containing text was
18432 displayed. Lines were deleted at the end of the window. */
18433 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
18434 int vpos = w->window_end_vpos;
18435 struct glyph_row *current_row = current_matrix->rows + vpos;
18436 struct glyph_row *desired_row = desired_matrix->rows + vpos;
18438 for (row = NULL;
18439 row == NULL && vpos >= first_vpos;
18440 --vpos, --current_row, --desired_row)
18442 if (desired_row->enabled_p)
18444 if (MATRIX_ROW_DISPLAYS_TEXT_P (desired_row))
18445 row = desired_row;
18447 else if (MATRIX_ROW_DISPLAYS_TEXT_P (current_row))
18448 row = current_row;
18451 eassert (row != NULL);
18452 w->window_end_vpos = vpos + 1;
18453 w->window_end_pos = Z - MATRIX_ROW_END_CHARPOS (row);
18454 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
18455 eassert (w->window_end_bytepos >= 0);
18456 IF_DEBUG (debug_method_add (w, "C"));
18458 else
18459 emacs_abort ();
18461 IF_DEBUG ((debug_end_pos = w->window_end_pos,
18462 debug_end_vpos = w->window_end_vpos));
18464 /* Record that display has not been completed. */
18465 w->window_end_valid = 0;
18466 w->desired_matrix->no_scrolling_p = 1;
18467 return 3;
18469 #undef GIVE_UP
18474 /***********************************************************************
18475 More debugging support
18476 ***********************************************************************/
18478 #ifdef GLYPH_DEBUG
18480 void dump_glyph_row (struct glyph_row *, int, int) EXTERNALLY_VISIBLE;
18481 void dump_glyph_matrix (struct glyph_matrix *, int) EXTERNALLY_VISIBLE;
18482 void dump_glyph (struct glyph_row *, struct glyph *, int) EXTERNALLY_VISIBLE;
18485 /* Dump the contents of glyph matrix MATRIX on stderr.
18487 GLYPHS 0 means don't show glyph contents.
18488 GLYPHS 1 means show glyphs in short form
18489 GLYPHS > 1 means show glyphs in long form. */
18491 void
18492 dump_glyph_matrix (struct glyph_matrix *matrix, int glyphs)
18494 int i;
18495 for (i = 0; i < matrix->nrows; ++i)
18496 dump_glyph_row (MATRIX_ROW (matrix, i), i, glyphs);
18500 /* Dump contents of glyph GLYPH to stderr. ROW and AREA are
18501 the glyph row and area where the glyph comes from. */
18503 void
18504 dump_glyph (struct glyph_row *row, struct glyph *glyph, int area)
18506 if (glyph->type == CHAR_GLYPH
18507 || glyph->type == GLYPHLESS_GLYPH)
18509 fprintf (stderr,
18510 " %5"pD"d %c %9"pI"d %c %3d 0x%06x %c %4d %1.1d%1.1d\n",
18511 glyph - row->glyphs[TEXT_AREA],
18512 (glyph->type == CHAR_GLYPH
18513 ? 'C'
18514 : 'G'),
18515 glyph->charpos,
18516 (BUFFERP (glyph->object)
18517 ? 'B'
18518 : (STRINGP (glyph->object)
18519 ? 'S'
18520 : (NILP (glyph->object)
18521 ? '0'
18522 : '-'))),
18523 glyph->pixel_width,
18524 glyph->u.ch,
18525 (glyph->u.ch < 0x80 && glyph->u.ch >= ' '
18526 ? glyph->u.ch
18527 : '.'),
18528 glyph->face_id,
18529 glyph->left_box_line_p,
18530 glyph->right_box_line_p);
18532 else if (glyph->type == STRETCH_GLYPH)
18534 fprintf (stderr,
18535 " %5"pD"d %c %9"pI"d %c %3d 0x%06x %c %4d %1.1d%1.1d\n",
18536 glyph - row->glyphs[TEXT_AREA],
18537 'S',
18538 glyph->charpos,
18539 (BUFFERP (glyph->object)
18540 ? 'B'
18541 : (STRINGP (glyph->object)
18542 ? 'S'
18543 : (NILP (glyph->object)
18544 ? '0'
18545 : '-'))),
18546 glyph->pixel_width,
18548 ' ',
18549 glyph->face_id,
18550 glyph->left_box_line_p,
18551 glyph->right_box_line_p);
18553 else if (glyph->type == IMAGE_GLYPH)
18555 fprintf (stderr,
18556 " %5"pD"d %c %9"pI"d %c %3d 0x%06x %c %4d %1.1d%1.1d\n",
18557 glyph - row->glyphs[TEXT_AREA],
18558 'I',
18559 glyph->charpos,
18560 (BUFFERP (glyph->object)
18561 ? 'B'
18562 : (STRINGP (glyph->object)
18563 ? 'S'
18564 : (NILP (glyph->object)
18565 ? '0'
18566 : '-'))),
18567 glyph->pixel_width,
18568 glyph->u.img_id,
18569 '.',
18570 glyph->face_id,
18571 glyph->left_box_line_p,
18572 glyph->right_box_line_p);
18574 else if (glyph->type == COMPOSITE_GLYPH)
18576 fprintf (stderr,
18577 " %5"pD"d %c %9"pI"d %c %3d 0x%06x",
18578 glyph - row->glyphs[TEXT_AREA],
18579 '+',
18580 glyph->charpos,
18581 (BUFFERP (glyph->object)
18582 ? 'B'
18583 : (STRINGP (glyph->object)
18584 ? 'S'
18585 : (NILP (glyph->object)
18586 ? '0'
18587 : '-'))),
18588 glyph->pixel_width,
18589 glyph->u.cmp.id);
18590 if (glyph->u.cmp.automatic)
18591 fprintf (stderr,
18592 "[%d-%d]",
18593 glyph->slice.cmp.from, glyph->slice.cmp.to);
18594 fprintf (stderr, " . %4d %1.1d%1.1d\n",
18595 glyph->face_id,
18596 glyph->left_box_line_p,
18597 glyph->right_box_line_p);
18602 /* Dump the contents of glyph row at VPOS in MATRIX to stderr.
18603 GLYPHS 0 means don't show glyph contents.
18604 GLYPHS 1 means show glyphs in short form
18605 GLYPHS > 1 means show glyphs in long form. */
18607 void
18608 dump_glyph_row (struct glyph_row *row, int vpos, int glyphs)
18610 if (glyphs != 1)
18612 fprintf (stderr, "Row Start End Used oE><\\CTZFesm X Y W H V A P\n");
18613 fprintf (stderr, "==============================================================================\n");
18615 fprintf (stderr, "%3d %9"pI"d %9"pI"d %4d %1.1d%1.1d%1.1d%1.1d\
18616 %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d %4d %4d %4d %4d %4d %4d %4d\n",
18617 vpos,
18618 MATRIX_ROW_START_CHARPOS (row),
18619 MATRIX_ROW_END_CHARPOS (row),
18620 row->used[TEXT_AREA],
18621 row->contains_overlapping_glyphs_p,
18622 row->enabled_p,
18623 row->truncated_on_left_p,
18624 row->truncated_on_right_p,
18625 row->continued_p,
18626 MATRIX_ROW_CONTINUATION_LINE_P (row),
18627 MATRIX_ROW_DISPLAYS_TEXT_P (row),
18628 row->ends_at_zv_p,
18629 row->fill_line_p,
18630 row->ends_in_middle_of_char_p,
18631 row->starts_in_middle_of_char_p,
18632 row->mouse_face_p,
18633 row->x,
18634 row->y,
18635 row->pixel_width,
18636 row->height,
18637 row->visible_height,
18638 row->ascent,
18639 row->phys_ascent);
18640 /* The next 3 lines should align to "Start" in the header. */
18641 fprintf (stderr, " %9"pD"d %9"pD"d\t%5d\n", row->start.overlay_string_index,
18642 row->end.overlay_string_index,
18643 row->continuation_lines_width);
18644 fprintf (stderr, " %9"pI"d %9"pI"d\n",
18645 CHARPOS (row->start.string_pos),
18646 CHARPOS (row->end.string_pos));
18647 fprintf (stderr, " %9d %9d\n", row->start.dpvec_index,
18648 row->end.dpvec_index);
18651 if (glyphs > 1)
18653 int area;
18655 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
18657 struct glyph *glyph = row->glyphs[area];
18658 struct glyph *glyph_end = glyph + row->used[area];
18660 /* Glyph for a line end in text. */
18661 if (area == TEXT_AREA && glyph == glyph_end && glyph->charpos > 0)
18662 ++glyph_end;
18664 if (glyph < glyph_end)
18665 fprintf (stderr, " Glyph# Type Pos O W Code C Face LR\n");
18667 for (; glyph < glyph_end; ++glyph)
18668 dump_glyph (row, glyph, area);
18671 else if (glyphs == 1)
18673 int area;
18674 char s[SHRT_MAX + 4];
18676 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
18678 int i;
18680 for (i = 0; i < row->used[area]; ++i)
18682 struct glyph *glyph = row->glyphs[area] + i;
18683 if (i == row->used[area] - 1
18684 && area == TEXT_AREA
18685 && NILP (glyph->object)
18686 && glyph->type == CHAR_GLYPH
18687 && glyph->u.ch == ' ')
18689 strcpy (&s[i], "[\\n]");
18690 i += 4;
18692 else if (glyph->type == CHAR_GLYPH
18693 && glyph->u.ch < 0x80
18694 && glyph->u.ch >= ' ')
18695 s[i] = glyph->u.ch;
18696 else
18697 s[i] = '.';
18700 s[i] = '\0';
18701 fprintf (stderr, "%3d: (%d) '%s'\n", vpos, row->enabled_p, s);
18707 DEFUN ("dump-glyph-matrix", Fdump_glyph_matrix,
18708 Sdump_glyph_matrix, 0, 1, "p",
18709 doc: /* Dump the current matrix of the selected window to stderr.
18710 Shows contents of glyph row structures. With non-nil
18711 parameter GLYPHS, dump glyphs as well. If GLYPHS is 1 show
18712 glyphs in short form, otherwise show glyphs in long form.
18714 Interactively, no argument means show glyphs in short form;
18715 with numeric argument, its value is passed as the GLYPHS flag. */)
18716 (Lisp_Object glyphs)
18718 struct window *w = XWINDOW (selected_window);
18719 struct buffer *buffer = XBUFFER (w->contents);
18721 fprintf (stderr, "PT = %"pI"d, BEGV = %"pI"d. ZV = %"pI"d\n",
18722 BUF_PT (buffer), BUF_BEGV (buffer), BUF_ZV (buffer));
18723 fprintf (stderr, "Cursor x = %d, y = %d, hpos = %d, vpos = %d\n",
18724 w->cursor.x, w->cursor.y, w->cursor.hpos, w->cursor.vpos);
18725 fprintf (stderr, "=============================================\n");
18726 dump_glyph_matrix (w->current_matrix,
18727 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 0);
18728 return Qnil;
18732 DEFUN ("dump-frame-glyph-matrix", Fdump_frame_glyph_matrix,
18733 Sdump_frame_glyph_matrix, 0, 0, "", doc: /* Dump the current glyph matrix of the selected frame to stderr.
18734 Only text-mode frames have frame glyph matrices. */)
18735 (void)
18737 struct frame *f = XFRAME (selected_frame);
18739 if (f->current_matrix)
18740 dump_glyph_matrix (f->current_matrix, 1);
18741 else
18742 fprintf (stderr, "*** This frame doesn't have a frame glyph matrix ***\n");
18743 return Qnil;
18747 DEFUN ("dump-glyph-row", Fdump_glyph_row, Sdump_glyph_row, 1, 2, "",
18748 doc: /* Dump glyph row ROW to stderr.
18749 GLYPH 0 means don't dump glyphs.
18750 GLYPH 1 means dump glyphs in short form.
18751 GLYPH > 1 or omitted means dump glyphs in long form. */)
18752 (Lisp_Object row, Lisp_Object glyphs)
18754 struct glyph_matrix *matrix;
18755 EMACS_INT vpos;
18757 CHECK_NUMBER (row);
18758 matrix = XWINDOW (selected_window)->current_matrix;
18759 vpos = XINT (row);
18760 if (vpos >= 0 && vpos < matrix->nrows)
18761 dump_glyph_row (MATRIX_ROW (matrix, vpos),
18762 vpos,
18763 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 2);
18764 return Qnil;
18768 DEFUN ("dump-tool-bar-row", Fdump_tool_bar_row, Sdump_tool_bar_row, 1, 2, "",
18769 doc: /* Dump glyph row ROW of the tool-bar of the current frame to stderr.
18770 GLYPH 0 means don't dump glyphs.
18771 GLYPH 1 means dump glyphs in short form.
18772 GLYPH > 1 or omitted means dump glyphs in long form.
18774 If there's no tool-bar, or if the tool-bar is not drawn by Emacs,
18775 do nothing. */)
18776 (Lisp_Object row, Lisp_Object glyphs)
18778 #if defined (HAVE_WINDOW_SYSTEM) && ! defined (USE_GTK) && ! defined (HAVE_NS)
18779 struct frame *sf = SELECTED_FRAME ();
18780 struct glyph_matrix *m = XWINDOW (sf->tool_bar_window)->current_matrix;
18781 EMACS_INT vpos;
18783 CHECK_NUMBER (row);
18784 vpos = XINT (row);
18785 if (vpos >= 0 && vpos < m->nrows)
18786 dump_glyph_row (MATRIX_ROW (m, vpos), vpos,
18787 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 2);
18788 #endif
18789 return Qnil;
18793 DEFUN ("trace-redisplay", Ftrace_redisplay, Strace_redisplay, 0, 1, "P",
18794 doc: /* Toggle tracing of redisplay.
18795 With ARG, turn tracing on if and only if ARG is positive. */)
18796 (Lisp_Object arg)
18798 if (NILP (arg))
18799 trace_redisplay_p = !trace_redisplay_p;
18800 else
18802 arg = Fprefix_numeric_value (arg);
18803 trace_redisplay_p = XINT (arg) > 0;
18806 return Qnil;
18810 DEFUN ("trace-to-stderr", Ftrace_to_stderr, Strace_to_stderr, 1, MANY, "",
18811 doc: /* Like `format', but print result to stderr.
18812 usage: (trace-to-stderr STRING &rest OBJECTS) */)
18813 (ptrdiff_t nargs, Lisp_Object *args)
18815 Lisp_Object s = Fformat (nargs, args);
18816 fwrite (SDATA (s), 1, SBYTES (s), stderr);
18817 return Qnil;
18820 #endif /* GLYPH_DEBUG */
18824 /***********************************************************************
18825 Building Desired Matrix Rows
18826 ***********************************************************************/
18828 /* Return a temporary glyph row holding the glyphs of an overlay arrow.
18829 Used for non-window-redisplay windows, and for windows w/o left fringe. */
18831 static struct glyph_row *
18832 get_overlay_arrow_glyph_row (struct window *w, Lisp_Object overlay_arrow_string)
18834 struct frame *f = XFRAME (WINDOW_FRAME (w));
18835 struct buffer *buffer = XBUFFER (w->contents);
18836 struct buffer *old = current_buffer;
18837 const unsigned char *arrow_string = SDATA (overlay_arrow_string);
18838 ptrdiff_t arrow_len = SCHARS (overlay_arrow_string);
18839 const unsigned char *arrow_end = arrow_string + arrow_len;
18840 const unsigned char *p;
18841 struct it it;
18842 bool multibyte_p;
18843 int n_glyphs_before;
18845 set_buffer_temp (buffer);
18846 init_iterator (&it, w, -1, -1, &scratch_glyph_row, DEFAULT_FACE_ID);
18847 scratch_glyph_row.reversed_p = false;
18848 it.glyph_row->used[TEXT_AREA] = 0;
18849 SET_TEXT_POS (it.position, 0, 0);
18851 multibyte_p = !NILP (BVAR (buffer, enable_multibyte_characters));
18852 p = arrow_string;
18853 while (p < arrow_end)
18855 Lisp_Object face, ilisp;
18857 /* Get the next character. */
18858 if (multibyte_p)
18859 it.c = it.char_to_display = string_char_and_length (p, &it.len);
18860 else
18862 it.c = it.char_to_display = *p, it.len = 1;
18863 if (! ASCII_CHAR_P (it.c))
18864 it.char_to_display = BYTE8_TO_CHAR (it.c);
18866 p += it.len;
18868 /* Get its face. */
18869 ilisp = make_number (p - arrow_string);
18870 face = Fget_text_property (ilisp, Qface, overlay_arrow_string);
18871 it.face_id = compute_char_face (f, it.char_to_display, face);
18873 /* Compute its width, get its glyphs. */
18874 n_glyphs_before = it.glyph_row->used[TEXT_AREA];
18875 SET_TEXT_POS (it.position, -1, -1);
18876 PRODUCE_GLYPHS (&it);
18878 /* If this character doesn't fit any more in the line, we have
18879 to remove some glyphs. */
18880 if (it.current_x > it.last_visible_x)
18882 it.glyph_row->used[TEXT_AREA] = n_glyphs_before;
18883 break;
18887 set_buffer_temp (old);
18888 return it.glyph_row;
18892 /* Insert truncation glyphs at the start of IT->glyph_row. Which
18893 glyphs to insert is determined by produce_special_glyphs. */
18895 static void
18896 insert_left_trunc_glyphs (struct it *it)
18898 struct it truncate_it;
18899 struct glyph *from, *end, *to, *toend;
18901 eassert (!FRAME_WINDOW_P (it->f)
18902 || (!it->glyph_row->reversed_p
18903 && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0)
18904 || (it->glyph_row->reversed_p
18905 && WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0));
18907 /* Get the truncation glyphs. */
18908 truncate_it = *it;
18909 truncate_it.current_x = 0;
18910 truncate_it.face_id = DEFAULT_FACE_ID;
18911 truncate_it.glyph_row = &scratch_glyph_row;
18912 truncate_it.area = TEXT_AREA;
18913 truncate_it.glyph_row->used[TEXT_AREA] = 0;
18914 CHARPOS (truncate_it.position) = BYTEPOS (truncate_it.position) = -1;
18915 truncate_it.object = Qnil;
18916 produce_special_glyphs (&truncate_it, IT_TRUNCATION);
18918 /* Overwrite glyphs from IT with truncation glyphs. */
18919 if (!it->glyph_row->reversed_p)
18921 short tused = truncate_it.glyph_row->used[TEXT_AREA];
18923 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
18924 end = from + tused;
18925 to = it->glyph_row->glyphs[TEXT_AREA];
18926 toend = to + it->glyph_row->used[TEXT_AREA];
18927 if (FRAME_WINDOW_P (it->f))
18929 /* On GUI frames, when variable-size fonts are displayed,
18930 the truncation glyphs may need more pixels than the row's
18931 glyphs they overwrite. We overwrite more glyphs to free
18932 enough screen real estate, and enlarge the stretch glyph
18933 on the right (see display_line), if there is one, to
18934 preserve the screen position of the truncation glyphs on
18935 the right. */
18936 int w = 0;
18937 struct glyph *g = to;
18938 short used;
18940 /* The first glyph could be partially visible, in which case
18941 it->glyph_row->x will be negative. But we want the left
18942 truncation glyphs to be aligned at the left margin of the
18943 window, so we override the x coordinate at which the row
18944 will begin. */
18945 it->glyph_row->x = 0;
18946 while (g < toend && w < it->truncation_pixel_width)
18948 w += g->pixel_width;
18949 ++g;
18951 if (g - to - tused > 0)
18953 memmove (to + tused, g, (toend - g) * sizeof(*g));
18954 it->glyph_row->used[TEXT_AREA] -= g - to - tused;
18956 used = it->glyph_row->used[TEXT_AREA];
18957 if (it->glyph_row->truncated_on_right_p
18958 && WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0
18959 && it->glyph_row->glyphs[TEXT_AREA][used - 2].type
18960 == STRETCH_GLYPH)
18962 int extra = w - it->truncation_pixel_width;
18964 it->glyph_row->glyphs[TEXT_AREA][used - 2].pixel_width += extra;
18968 while (from < end)
18969 *to++ = *from++;
18971 /* There may be padding glyphs left over. Overwrite them too. */
18972 if (!FRAME_WINDOW_P (it->f))
18974 while (to < toend && CHAR_GLYPH_PADDING_P (*to))
18976 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
18977 while (from < end)
18978 *to++ = *from++;
18982 if (to > toend)
18983 it->glyph_row->used[TEXT_AREA] = to - it->glyph_row->glyphs[TEXT_AREA];
18985 else
18987 short tused = truncate_it.glyph_row->used[TEXT_AREA];
18989 /* In R2L rows, overwrite the last (rightmost) glyphs, and do
18990 that back to front. */
18991 end = truncate_it.glyph_row->glyphs[TEXT_AREA];
18992 from = end + truncate_it.glyph_row->used[TEXT_AREA] - 1;
18993 toend = it->glyph_row->glyphs[TEXT_AREA];
18994 to = toend + it->glyph_row->used[TEXT_AREA] - 1;
18995 if (FRAME_WINDOW_P (it->f))
18997 int w = 0;
18998 struct glyph *g = to;
19000 while (g >= toend && w < it->truncation_pixel_width)
19002 w += g->pixel_width;
19003 --g;
19005 if (to - g - tused > 0)
19006 to = g + tused;
19007 if (it->glyph_row->truncated_on_right_p
19008 && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0
19009 && it->glyph_row->glyphs[TEXT_AREA][1].type == STRETCH_GLYPH)
19011 int extra = w - it->truncation_pixel_width;
19013 it->glyph_row->glyphs[TEXT_AREA][1].pixel_width += extra;
19017 while (from >= end && to >= toend)
19018 *to-- = *from--;
19019 if (!FRAME_WINDOW_P (it->f))
19021 while (to >= toend && CHAR_GLYPH_PADDING_P (*to))
19023 from =
19024 truncate_it.glyph_row->glyphs[TEXT_AREA]
19025 + truncate_it.glyph_row->used[TEXT_AREA] - 1;
19026 while (from >= end && to >= toend)
19027 *to-- = *from--;
19030 if (from >= end)
19032 /* Need to free some room before prepending additional
19033 glyphs. */
19034 int move_by = from - end + 1;
19035 struct glyph *g0 = it->glyph_row->glyphs[TEXT_AREA];
19036 struct glyph *g = g0 + it->glyph_row->used[TEXT_AREA] - 1;
19038 for ( ; g >= g0; g--)
19039 g[move_by] = *g;
19040 while (from >= end)
19041 *to-- = *from--;
19042 it->glyph_row->used[TEXT_AREA] += move_by;
19047 /* Compute the hash code for ROW. */
19048 unsigned
19049 row_hash (struct glyph_row *row)
19051 int area, k;
19052 unsigned hashval = 0;
19054 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
19055 for (k = 0; k < row->used[area]; ++k)
19056 hashval = ((((hashval << 4) + (hashval >> 24)) & 0x0fffffff)
19057 + row->glyphs[area][k].u.val
19058 + row->glyphs[area][k].face_id
19059 + row->glyphs[area][k].padding_p
19060 + (row->glyphs[area][k].type << 2));
19062 return hashval;
19065 /* Compute the pixel height and width of IT->glyph_row.
19067 Most of the time, ascent and height of a display line will be equal
19068 to the max_ascent and max_height values of the display iterator
19069 structure. This is not the case if
19071 1. We hit ZV without displaying anything. In this case, max_ascent
19072 and max_height will be zero.
19074 2. We have some glyphs that don't contribute to the line height.
19075 (The glyph row flag contributes_to_line_height_p is for future
19076 pixmap extensions).
19078 The first case is easily covered by using default values because in
19079 these cases, the line height does not really matter, except that it
19080 must not be zero. */
19082 static void
19083 compute_line_metrics (struct it *it)
19085 struct glyph_row *row = it->glyph_row;
19087 if (FRAME_WINDOW_P (it->f))
19089 int i, min_y, max_y;
19091 /* The line may consist of one space only, that was added to
19092 place the cursor on it. If so, the row's height hasn't been
19093 computed yet. */
19094 if (row->height == 0)
19096 if (it->max_ascent + it->max_descent == 0)
19097 it->max_descent = it->max_phys_descent = FRAME_LINE_HEIGHT (it->f);
19098 row->ascent = it->max_ascent;
19099 row->height = it->max_ascent + it->max_descent;
19100 row->phys_ascent = it->max_phys_ascent;
19101 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
19102 row->extra_line_spacing = it->max_extra_line_spacing;
19105 /* Compute the width of this line. */
19106 row->pixel_width = row->x;
19107 for (i = 0; i < row->used[TEXT_AREA]; ++i)
19108 row->pixel_width += row->glyphs[TEXT_AREA][i].pixel_width;
19110 eassert (row->pixel_width >= 0);
19111 eassert (row->ascent >= 0 && row->height > 0);
19113 row->overlapping_p = (MATRIX_ROW_OVERLAPS_SUCC_P (row)
19114 || MATRIX_ROW_OVERLAPS_PRED_P (row));
19116 /* If first line's physical ascent is larger than its logical
19117 ascent, use the physical ascent, and make the row taller.
19118 This makes accented characters fully visible. */
19119 if (row == MATRIX_FIRST_TEXT_ROW (it->w->desired_matrix)
19120 && row->phys_ascent > row->ascent)
19122 row->height += row->phys_ascent - row->ascent;
19123 row->ascent = row->phys_ascent;
19126 /* Compute how much of the line is visible. */
19127 row->visible_height = row->height;
19129 min_y = WINDOW_HEADER_LINE_HEIGHT (it->w);
19130 max_y = WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w);
19132 if (row->y < min_y)
19133 row->visible_height -= min_y - row->y;
19134 if (row->y + row->height > max_y)
19135 row->visible_height -= row->y + row->height - max_y;
19137 else
19139 row->pixel_width = row->used[TEXT_AREA];
19140 if (row->continued_p)
19141 row->pixel_width -= it->continuation_pixel_width;
19142 else if (row->truncated_on_right_p)
19143 row->pixel_width -= it->truncation_pixel_width;
19144 row->ascent = row->phys_ascent = 0;
19145 row->height = row->phys_height = row->visible_height = 1;
19146 row->extra_line_spacing = 0;
19149 /* Compute a hash code for this row. */
19150 row->hash = row_hash (row);
19152 it->max_ascent = it->max_descent = 0;
19153 it->max_phys_ascent = it->max_phys_descent = 0;
19157 /* Append one space to the glyph row of iterator IT if doing a
19158 window-based redisplay. The space has the same face as
19159 IT->face_id. Value is non-zero if a space was added.
19161 This function is called to make sure that there is always one glyph
19162 at the end of a glyph row that the cursor can be set on under
19163 window-systems. (If there weren't such a glyph we would not know
19164 how wide and tall a box cursor should be displayed).
19166 At the same time this space let's a nicely handle clearing to the
19167 end of the line if the row ends in italic text. */
19169 static int
19170 append_space_for_newline (struct it *it, int default_face_p)
19172 if (FRAME_WINDOW_P (it->f))
19174 int n = it->glyph_row->used[TEXT_AREA];
19176 if (it->glyph_row->glyphs[TEXT_AREA] + n
19177 < it->glyph_row->glyphs[1 + TEXT_AREA])
19179 /* Save some values that must not be changed.
19180 Must save IT->c and IT->len because otherwise
19181 ITERATOR_AT_END_P wouldn't work anymore after
19182 append_space_for_newline has been called. */
19183 enum display_element_type saved_what = it->what;
19184 int saved_c = it->c, saved_len = it->len;
19185 int saved_char_to_display = it->char_to_display;
19186 int saved_x = it->current_x;
19187 int saved_face_id = it->face_id;
19188 int saved_box_end = it->end_of_box_run_p;
19189 struct text_pos saved_pos;
19190 Lisp_Object saved_object;
19191 struct face *face;
19193 saved_object = it->object;
19194 saved_pos = it->position;
19196 it->what = IT_CHARACTER;
19197 memset (&it->position, 0, sizeof it->position);
19198 it->object = Qnil;
19199 it->c = it->char_to_display = ' ';
19200 it->len = 1;
19202 /* If the default face was remapped, be sure to use the
19203 remapped face for the appended newline. */
19204 if (default_face_p)
19205 it->face_id = lookup_basic_face (it->f, DEFAULT_FACE_ID);
19206 else if (it->face_before_selective_p)
19207 it->face_id = it->saved_face_id;
19208 face = FACE_FROM_ID (it->f, it->face_id);
19209 it->face_id = FACE_FOR_CHAR (it->f, face, 0, -1, Qnil);
19210 /* In R2L rows, we will prepend a stretch glyph that will
19211 have the end_of_box_run_p flag set for it, so there's no
19212 need for the appended newline glyph to have that flag
19213 set. */
19214 if (it->glyph_row->reversed_p
19215 /* But if the appended newline glyph goes all the way to
19216 the end of the row, there will be no stretch glyph,
19217 so leave the box flag set. */
19218 && saved_x + FRAME_COLUMN_WIDTH (it->f) < it->last_visible_x)
19219 it->end_of_box_run_p = 0;
19221 PRODUCE_GLYPHS (it);
19223 it->override_ascent = -1;
19224 it->constrain_row_ascent_descent_p = 0;
19225 it->current_x = saved_x;
19226 it->object = saved_object;
19227 it->position = saved_pos;
19228 it->what = saved_what;
19229 it->face_id = saved_face_id;
19230 it->len = saved_len;
19231 it->c = saved_c;
19232 it->char_to_display = saved_char_to_display;
19233 it->end_of_box_run_p = saved_box_end;
19234 return 1;
19238 return 0;
19242 /* Extend the face of the last glyph in the text area of IT->glyph_row
19243 to the end of the display line. Called from display_line. If the
19244 glyph row is empty, add a space glyph to it so that we know the
19245 face to draw. Set the glyph row flag fill_line_p. If the glyph
19246 row is R2L, prepend a stretch glyph to cover the empty space to the
19247 left of the leftmost glyph. */
19249 static void
19250 extend_face_to_end_of_line (struct it *it)
19252 struct face *face, *default_face;
19253 struct frame *f = it->f;
19255 /* If line is already filled, do nothing. Non window-system frames
19256 get a grace of one more ``pixel'' because their characters are
19257 1-``pixel'' wide, so they hit the equality too early. This grace
19258 is needed only for R2L rows that are not continued, to produce
19259 one extra blank where we could display the cursor. */
19260 if ((it->current_x >= it->last_visible_x
19261 + (!FRAME_WINDOW_P (f)
19262 && it->glyph_row->reversed_p
19263 && !it->glyph_row->continued_p))
19264 /* If the window has display margins, we will need to extend
19265 their face even if the text area is filled. */
19266 && !(WINDOW_LEFT_MARGIN_WIDTH (it->w) > 0
19267 || WINDOW_RIGHT_MARGIN_WIDTH (it->w) > 0))
19268 return;
19270 /* The default face, possibly remapped. */
19271 default_face = FACE_FROM_ID (f, lookup_basic_face (f, DEFAULT_FACE_ID));
19273 /* Face extension extends the background and box of IT->face_id
19274 to the end of the line. If the background equals the background
19275 of the frame, we don't have to do anything. */
19276 if (it->face_before_selective_p)
19277 face = FACE_FROM_ID (f, it->saved_face_id);
19278 else
19279 face = FACE_FROM_ID (f, it->face_id);
19281 if (FRAME_WINDOW_P (f)
19282 && MATRIX_ROW_DISPLAYS_TEXT_P (it->glyph_row)
19283 && face->box == FACE_NO_BOX
19284 && face->background == FRAME_BACKGROUND_PIXEL (f)
19285 #ifdef HAVE_WINDOW_SYSTEM
19286 && !face->stipple
19287 #endif
19288 && !it->glyph_row->reversed_p)
19289 return;
19291 /* Set the glyph row flag indicating that the face of the last glyph
19292 in the text area has to be drawn to the end of the text area. */
19293 it->glyph_row->fill_line_p = 1;
19295 /* If current character of IT is not ASCII, make sure we have the
19296 ASCII face. This will be automatically undone the next time
19297 get_next_display_element returns a multibyte character. Note
19298 that the character will always be single byte in unibyte
19299 text. */
19300 if (!ASCII_CHAR_P (it->c))
19302 it->face_id = FACE_FOR_CHAR (f, face, 0, -1, Qnil);
19305 if (FRAME_WINDOW_P (f))
19307 /* If the row is empty, add a space with the current face of IT,
19308 so that we know which face to draw. */
19309 if (it->glyph_row->used[TEXT_AREA] == 0)
19311 it->glyph_row->glyphs[TEXT_AREA][0] = space_glyph;
19312 it->glyph_row->glyphs[TEXT_AREA][0].face_id = face->id;
19313 it->glyph_row->used[TEXT_AREA] = 1;
19315 /* Mode line and the header line don't have margins, and
19316 likewise the frame's tool-bar window, if there is any. */
19317 if (!(it->glyph_row->mode_line_p
19318 #if defined (HAVE_WINDOW_SYSTEM) && ! defined (USE_GTK) && ! defined (HAVE_NS)
19319 || (WINDOWP (f->tool_bar_window)
19320 && it->w == XWINDOW (f->tool_bar_window))
19321 #endif
19324 if (WINDOW_LEFT_MARGIN_WIDTH (it->w) > 0
19325 && it->glyph_row->used[LEFT_MARGIN_AREA] == 0)
19327 it->glyph_row->glyphs[LEFT_MARGIN_AREA][0] = space_glyph;
19328 it->glyph_row->glyphs[LEFT_MARGIN_AREA][0].face_id =
19329 default_face->id;
19330 it->glyph_row->used[LEFT_MARGIN_AREA] = 1;
19332 if (WINDOW_RIGHT_MARGIN_WIDTH (it->w) > 0
19333 && it->glyph_row->used[RIGHT_MARGIN_AREA] == 0)
19335 it->glyph_row->glyphs[RIGHT_MARGIN_AREA][0] = space_glyph;
19336 it->glyph_row->glyphs[RIGHT_MARGIN_AREA][0].face_id =
19337 default_face->id;
19338 it->glyph_row->used[RIGHT_MARGIN_AREA] = 1;
19341 #ifdef HAVE_WINDOW_SYSTEM
19342 if (it->glyph_row->reversed_p)
19344 /* Prepend a stretch glyph to the row, such that the
19345 rightmost glyph will be drawn flushed all the way to the
19346 right margin of the window. The stretch glyph that will
19347 occupy the empty space, if any, to the left of the
19348 glyphs. */
19349 struct font *font = face->font ? face->font : FRAME_FONT (f);
19350 struct glyph *row_start = it->glyph_row->glyphs[TEXT_AREA];
19351 struct glyph *row_end = row_start + it->glyph_row->used[TEXT_AREA];
19352 struct glyph *g;
19353 int row_width, stretch_ascent, stretch_width;
19354 struct text_pos saved_pos;
19355 int saved_face_id, saved_avoid_cursor, saved_box_start;
19357 for (row_width = 0, g = row_start; g < row_end; g++)
19358 row_width += g->pixel_width;
19360 /* FIXME: There are various minor display glitches in R2L
19361 rows when only one of the fringes is missing. The
19362 strange condition below produces the least bad effect. */
19363 if ((WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0)
19364 == (WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0)
19365 || WINDOW_RIGHT_FRINGE_WIDTH (it->w) != 0)
19366 stretch_width = window_box_width (it->w, TEXT_AREA);
19367 else
19368 stretch_width = it->last_visible_x - it->first_visible_x;
19369 stretch_width -= row_width;
19371 if (stretch_width > 0)
19373 stretch_ascent =
19374 (((it->ascent + it->descent)
19375 * FONT_BASE (font)) / FONT_HEIGHT (font));
19376 saved_pos = it->position;
19377 memset (&it->position, 0, sizeof it->position);
19378 saved_avoid_cursor = it->avoid_cursor_p;
19379 it->avoid_cursor_p = 1;
19380 saved_face_id = it->face_id;
19381 saved_box_start = it->start_of_box_run_p;
19382 /* The last row's stretch glyph should get the default
19383 face, to avoid painting the rest of the window with
19384 the region face, if the region ends at ZV. */
19385 if (it->glyph_row->ends_at_zv_p)
19386 it->face_id = default_face->id;
19387 else
19388 it->face_id = face->id;
19389 it->start_of_box_run_p = 0;
19390 append_stretch_glyph (it, Qnil, stretch_width,
19391 it->ascent + it->descent, stretch_ascent);
19392 it->position = saved_pos;
19393 it->avoid_cursor_p = saved_avoid_cursor;
19394 it->face_id = saved_face_id;
19395 it->start_of_box_run_p = saved_box_start;
19397 /* If stretch_width comes out negative, it means that the
19398 last glyph is only partially visible. In R2L rows, we
19399 want the leftmost glyph to be partially visible, so we
19400 need to give the row the corresponding left offset. */
19401 if (stretch_width < 0)
19402 it->glyph_row->x = stretch_width;
19404 #endif /* HAVE_WINDOW_SYSTEM */
19406 else
19408 /* Save some values that must not be changed. */
19409 int saved_x = it->current_x;
19410 struct text_pos saved_pos;
19411 Lisp_Object saved_object;
19412 enum display_element_type saved_what = it->what;
19413 int saved_face_id = it->face_id;
19415 saved_object = it->object;
19416 saved_pos = it->position;
19418 it->what = IT_CHARACTER;
19419 memset (&it->position, 0, sizeof it->position);
19420 it->object = Qnil;
19421 it->c = it->char_to_display = ' ';
19422 it->len = 1;
19424 if (WINDOW_LEFT_MARGIN_WIDTH (it->w) > 0
19425 && (it->glyph_row->used[LEFT_MARGIN_AREA]
19426 < WINDOW_LEFT_MARGIN_WIDTH (it->w))
19427 && !it->glyph_row->mode_line_p
19428 && default_face->background != FRAME_BACKGROUND_PIXEL (f))
19430 struct glyph *g = it->glyph_row->glyphs[LEFT_MARGIN_AREA];
19431 struct glyph *e = g + it->glyph_row->used[LEFT_MARGIN_AREA];
19433 for (it->current_x = 0; g < e; g++)
19434 it->current_x += g->pixel_width;
19436 it->area = LEFT_MARGIN_AREA;
19437 it->face_id = default_face->id;
19438 while (it->glyph_row->used[LEFT_MARGIN_AREA]
19439 < WINDOW_LEFT_MARGIN_WIDTH (it->w))
19441 PRODUCE_GLYPHS (it);
19442 /* term.c:produce_glyphs advances it->current_x only for
19443 TEXT_AREA. */
19444 it->current_x += it->pixel_width;
19447 it->current_x = saved_x;
19448 it->area = TEXT_AREA;
19451 /* The last row's blank glyphs should get the default face, to
19452 avoid painting the rest of the window with the region face,
19453 if the region ends at ZV. */
19454 if (it->glyph_row->ends_at_zv_p)
19455 it->face_id = default_face->id;
19456 else
19457 it->face_id = face->id;
19458 PRODUCE_GLYPHS (it);
19460 while (it->current_x <= it->last_visible_x)
19461 PRODUCE_GLYPHS (it);
19463 if (WINDOW_RIGHT_MARGIN_WIDTH (it->w) > 0
19464 && (it->glyph_row->used[RIGHT_MARGIN_AREA]
19465 < WINDOW_RIGHT_MARGIN_WIDTH (it->w))
19466 && !it->glyph_row->mode_line_p
19467 && default_face->background != FRAME_BACKGROUND_PIXEL (f))
19469 struct glyph *g = it->glyph_row->glyphs[RIGHT_MARGIN_AREA];
19470 struct glyph *e = g + it->glyph_row->used[RIGHT_MARGIN_AREA];
19472 for ( ; g < e; g++)
19473 it->current_x += g->pixel_width;
19475 it->area = RIGHT_MARGIN_AREA;
19476 it->face_id = default_face->id;
19477 while (it->glyph_row->used[RIGHT_MARGIN_AREA]
19478 < WINDOW_RIGHT_MARGIN_WIDTH (it->w))
19480 PRODUCE_GLYPHS (it);
19481 it->current_x += it->pixel_width;
19484 it->area = TEXT_AREA;
19487 /* Don't count these blanks really. It would let us insert a left
19488 truncation glyph below and make us set the cursor on them, maybe. */
19489 it->current_x = saved_x;
19490 it->object = saved_object;
19491 it->position = saved_pos;
19492 it->what = saved_what;
19493 it->face_id = saved_face_id;
19498 /* Value is non-zero if text starting at CHARPOS in current_buffer is
19499 trailing whitespace. */
19501 static int
19502 trailing_whitespace_p (ptrdiff_t charpos)
19504 ptrdiff_t bytepos = CHAR_TO_BYTE (charpos);
19505 int c = 0;
19507 while (bytepos < ZV_BYTE
19508 && (c = FETCH_CHAR (bytepos),
19509 c == ' ' || c == '\t'))
19510 ++bytepos;
19512 if (bytepos >= ZV_BYTE || c == '\n' || c == '\r')
19514 if (bytepos != PT_BYTE)
19515 return 1;
19517 return 0;
19521 /* Highlight trailing whitespace, if any, in ROW. */
19523 static void
19524 highlight_trailing_whitespace (struct frame *f, struct glyph_row *row)
19526 int used = row->used[TEXT_AREA];
19528 if (used)
19530 struct glyph *start = row->glyphs[TEXT_AREA];
19531 struct glyph *glyph = start + used - 1;
19533 if (row->reversed_p)
19535 /* Right-to-left rows need to be processed in the opposite
19536 direction, so swap the edge pointers. */
19537 glyph = start;
19538 start = row->glyphs[TEXT_AREA] + used - 1;
19541 /* Skip over glyphs inserted to display the cursor at the
19542 end of a line, for extending the face of the last glyph
19543 to the end of the line on terminals, and for truncation
19544 and continuation glyphs. */
19545 if (!row->reversed_p)
19547 while (glyph >= start
19548 && glyph->type == CHAR_GLYPH
19549 && NILP (glyph->object))
19550 --glyph;
19552 else
19554 while (glyph <= start
19555 && glyph->type == CHAR_GLYPH
19556 && NILP (glyph->object))
19557 ++glyph;
19560 /* If last glyph is a space or stretch, and it's trailing
19561 whitespace, set the face of all trailing whitespace glyphs in
19562 IT->glyph_row to `trailing-whitespace'. */
19563 if ((row->reversed_p ? glyph <= start : glyph >= start)
19564 && BUFFERP (glyph->object)
19565 && (glyph->type == STRETCH_GLYPH
19566 || (glyph->type == CHAR_GLYPH
19567 && glyph->u.ch == ' '))
19568 && trailing_whitespace_p (glyph->charpos))
19570 int face_id = lookup_named_face (f, Qtrailing_whitespace, false);
19571 if (face_id < 0)
19572 return;
19574 if (!row->reversed_p)
19576 while (glyph >= start
19577 && BUFFERP (glyph->object)
19578 && (glyph->type == STRETCH_GLYPH
19579 || (glyph->type == CHAR_GLYPH
19580 && glyph->u.ch == ' ')))
19581 (glyph--)->face_id = face_id;
19583 else
19585 while (glyph <= start
19586 && BUFFERP (glyph->object)
19587 && (glyph->type == STRETCH_GLYPH
19588 || (glyph->type == CHAR_GLYPH
19589 && glyph->u.ch == ' ')))
19590 (glyph++)->face_id = face_id;
19597 /* Value is non-zero if glyph row ROW should be
19598 considered to hold the buffer position CHARPOS. */
19600 static int
19601 row_for_charpos_p (struct glyph_row *row, ptrdiff_t charpos)
19603 int result = 1;
19605 if (charpos == CHARPOS (row->end.pos)
19606 || charpos == MATRIX_ROW_END_CHARPOS (row))
19608 /* Suppose the row ends on a string.
19609 Unless the row is continued, that means it ends on a newline
19610 in the string. If it's anything other than a display string
19611 (e.g., a before-string from an overlay), we don't want the
19612 cursor there. (This heuristic seems to give the optimal
19613 behavior for the various types of multi-line strings.)
19614 One exception: if the string has `cursor' property on one of
19615 its characters, we _do_ want the cursor there. */
19616 if (CHARPOS (row->end.string_pos) >= 0)
19618 if (row->continued_p)
19619 result = 1;
19620 else
19622 /* Check for `display' property. */
19623 struct glyph *beg = row->glyphs[TEXT_AREA];
19624 struct glyph *end = beg + row->used[TEXT_AREA] - 1;
19625 struct glyph *glyph;
19627 result = 0;
19628 for (glyph = end; glyph >= beg; --glyph)
19629 if (STRINGP (glyph->object))
19631 Lisp_Object prop
19632 = Fget_char_property (make_number (charpos),
19633 Qdisplay, Qnil);
19634 result =
19635 (!NILP (prop)
19636 && display_prop_string_p (prop, glyph->object));
19637 /* If there's a `cursor' property on one of the
19638 string's characters, this row is a cursor row,
19639 even though this is not a display string. */
19640 if (!result)
19642 Lisp_Object s = glyph->object;
19644 for ( ; glyph >= beg && EQ (glyph->object, s); --glyph)
19646 ptrdiff_t gpos = glyph->charpos;
19648 if (!NILP (Fget_char_property (make_number (gpos),
19649 Qcursor, s)))
19651 result = 1;
19652 break;
19656 break;
19660 else if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
19662 /* If the row ends in middle of a real character,
19663 and the line is continued, we want the cursor here.
19664 That's because CHARPOS (ROW->end.pos) would equal
19665 PT if PT is before the character. */
19666 if (!row->ends_in_ellipsis_p)
19667 result = row->continued_p;
19668 else
19669 /* If the row ends in an ellipsis, then
19670 CHARPOS (ROW->end.pos) will equal point after the
19671 invisible text. We want that position to be displayed
19672 after the ellipsis. */
19673 result = 0;
19675 /* If the row ends at ZV, display the cursor at the end of that
19676 row instead of at the start of the row below. */
19677 else if (row->ends_at_zv_p)
19678 result = 1;
19679 else
19680 result = 0;
19683 return result;
19686 /* Value is non-zero if glyph row ROW should be
19687 used to hold the cursor. */
19689 static int
19690 cursor_row_p (struct glyph_row *row)
19692 return row_for_charpos_p (row, PT);
19697 /* Push the property PROP so that it will be rendered at the current
19698 position in IT. Return 1 if PROP was successfully pushed, 0
19699 otherwise. Called from handle_line_prefix to handle the
19700 `line-prefix' and `wrap-prefix' properties. */
19702 static int
19703 push_prefix_prop (struct it *it, Lisp_Object prop)
19705 struct text_pos pos =
19706 STRINGP (it->string) ? it->current.string_pos : it->current.pos;
19708 eassert (it->method == GET_FROM_BUFFER
19709 || it->method == GET_FROM_DISPLAY_VECTOR
19710 || it->method == GET_FROM_STRING);
19712 /* We need to save the current buffer/string position, so it will be
19713 restored by pop_it, because iterate_out_of_display_property
19714 depends on that being set correctly, but some situations leave
19715 it->position not yet set when this function is called. */
19716 push_it (it, &pos);
19718 if (STRINGP (prop))
19720 if (SCHARS (prop) == 0)
19722 pop_it (it);
19723 return 0;
19726 it->string = prop;
19727 it->string_from_prefix_prop_p = 1;
19728 it->multibyte_p = STRING_MULTIBYTE (it->string);
19729 it->current.overlay_string_index = -1;
19730 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
19731 it->end_charpos = it->string_nchars = SCHARS (it->string);
19732 it->method = GET_FROM_STRING;
19733 it->stop_charpos = 0;
19734 it->prev_stop = 0;
19735 it->base_level_stop = 0;
19737 /* Force paragraph direction to be that of the parent
19738 buffer/string. */
19739 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
19740 it->paragraph_embedding = it->bidi_it.paragraph_dir;
19741 else
19742 it->paragraph_embedding = L2R;
19744 /* Set up the bidi iterator for this display string. */
19745 if (it->bidi_p)
19747 it->bidi_it.string.lstring = it->string;
19748 it->bidi_it.string.s = NULL;
19749 it->bidi_it.string.schars = it->end_charpos;
19750 it->bidi_it.string.bufpos = IT_CHARPOS (*it);
19751 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
19752 it->bidi_it.string.unibyte = !it->multibyte_p;
19753 it->bidi_it.w = it->w;
19754 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
19757 else if (CONSP (prop) && EQ (XCAR (prop), Qspace))
19759 it->method = GET_FROM_STRETCH;
19760 it->object = prop;
19762 #ifdef HAVE_WINDOW_SYSTEM
19763 else if (IMAGEP (prop))
19765 it->what = IT_IMAGE;
19766 it->image_id = lookup_image (it->f, prop);
19767 it->method = GET_FROM_IMAGE;
19769 #endif /* HAVE_WINDOW_SYSTEM */
19770 else
19772 pop_it (it); /* bogus display property, give up */
19773 return 0;
19776 return 1;
19779 /* Return the character-property PROP at the current position in IT. */
19781 static Lisp_Object
19782 get_it_property (struct it *it, Lisp_Object prop)
19784 Lisp_Object position, object = it->object;
19786 if (STRINGP (object))
19787 position = make_number (IT_STRING_CHARPOS (*it));
19788 else if (BUFFERP (object))
19790 position = make_number (IT_CHARPOS (*it));
19791 object = it->window;
19793 else
19794 return Qnil;
19796 return Fget_char_property (position, prop, object);
19799 /* See if there's a line- or wrap-prefix, and if so, push it on IT. */
19801 static void
19802 handle_line_prefix (struct it *it)
19804 Lisp_Object prefix;
19806 if (it->continuation_lines_width > 0)
19808 prefix = get_it_property (it, Qwrap_prefix);
19809 if (NILP (prefix))
19810 prefix = Vwrap_prefix;
19812 else
19814 prefix = get_it_property (it, Qline_prefix);
19815 if (NILP (prefix))
19816 prefix = Vline_prefix;
19818 if (! NILP (prefix) && push_prefix_prop (it, prefix))
19820 /* If the prefix is wider than the window, and we try to wrap
19821 it, it would acquire its own wrap prefix, and so on till the
19822 iterator stack overflows. So, don't wrap the prefix. */
19823 it->line_wrap = TRUNCATE;
19824 it->avoid_cursor_p = 1;
19830 /* Remove N glyphs at the start of a reversed IT->glyph_row. Called
19831 only for R2L lines from display_line and display_string, when they
19832 decide that too many glyphs were produced by PRODUCE_GLYPHS, and
19833 the line/string needs to be continued on the next glyph row. */
19834 static void
19835 unproduce_glyphs (struct it *it, int n)
19837 struct glyph *glyph, *end;
19839 eassert (it->glyph_row);
19840 eassert (it->glyph_row->reversed_p);
19841 eassert (it->area == TEXT_AREA);
19842 eassert (n <= it->glyph_row->used[TEXT_AREA]);
19844 if (n > it->glyph_row->used[TEXT_AREA])
19845 n = it->glyph_row->used[TEXT_AREA];
19846 glyph = it->glyph_row->glyphs[TEXT_AREA] + n;
19847 end = it->glyph_row->glyphs[TEXT_AREA] + it->glyph_row->used[TEXT_AREA];
19848 for ( ; glyph < end; glyph++)
19849 glyph[-n] = *glyph;
19852 /* Find the positions in a bidi-reordered ROW to serve as ROW->minpos
19853 and ROW->maxpos. */
19854 static void
19855 find_row_edges (struct it *it, struct glyph_row *row,
19856 ptrdiff_t min_pos, ptrdiff_t min_bpos,
19857 ptrdiff_t max_pos, ptrdiff_t max_bpos)
19859 /* FIXME: Revisit this when glyph ``spilling'' in continuation
19860 lines' rows is implemented for bidi-reordered rows. */
19862 /* ROW->minpos is the value of min_pos, the minimal buffer position
19863 we have in ROW, or ROW->start.pos if that is smaller. */
19864 if (min_pos <= ZV && min_pos < row->start.pos.charpos)
19865 SET_TEXT_POS (row->minpos, min_pos, min_bpos);
19866 else
19867 /* We didn't find buffer positions smaller than ROW->start, or
19868 didn't find _any_ valid buffer positions in any of the glyphs,
19869 so we must trust the iterator's computed positions. */
19870 row->minpos = row->start.pos;
19871 if (max_pos <= 0)
19873 max_pos = CHARPOS (it->current.pos);
19874 max_bpos = BYTEPOS (it->current.pos);
19877 /* Here are the various use-cases for ending the row, and the
19878 corresponding values for ROW->maxpos:
19880 Line ends in a newline from buffer eol_pos + 1
19881 Line is continued from buffer max_pos + 1
19882 Line is truncated on right it->current.pos
19883 Line ends in a newline from string max_pos + 1(*)
19884 (*) + 1 only when line ends in a forward scan
19885 Line is continued from string max_pos
19886 Line is continued from display vector max_pos
19887 Line is entirely from a string min_pos == max_pos
19888 Line is entirely from a display vector min_pos == max_pos
19889 Line that ends at ZV ZV
19891 If you discover other use-cases, please add them here as
19892 appropriate. */
19893 if (row->ends_at_zv_p)
19894 row->maxpos = it->current.pos;
19895 else if (row->used[TEXT_AREA])
19897 int seen_this_string = 0;
19898 struct glyph_row *r1 = row - 1;
19900 /* Did we see the same display string on the previous row? */
19901 if (STRINGP (it->object)
19902 /* this is not the first row */
19903 && row > it->w->desired_matrix->rows
19904 /* previous row is not the header line */
19905 && !r1->mode_line_p
19906 /* previous row also ends in a newline from a string */
19907 && r1->ends_in_newline_from_string_p)
19909 struct glyph *start, *end;
19911 /* Search for the last glyph of the previous row that came
19912 from buffer or string. Depending on whether the row is
19913 L2R or R2L, we need to process it front to back or the
19914 other way round. */
19915 if (!r1->reversed_p)
19917 start = r1->glyphs[TEXT_AREA];
19918 end = start + r1->used[TEXT_AREA];
19919 /* Glyphs inserted by redisplay have nil as their object. */
19920 while (end > start
19921 && NILP ((end - 1)->object)
19922 && (end - 1)->charpos <= 0)
19923 --end;
19924 if (end > start)
19926 if (EQ ((end - 1)->object, it->object))
19927 seen_this_string = 1;
19929 else
19930 /* If all the glyphs of the previous row were inserted
19931 by redisplay, it means the previous row was
19932 produced from a single newline, which is only
19933 possible if that newline came from the same string
19934 as the one which produced this ROW. */
19935 seen_this_string = 1;
19937 else
19939 end = r1->glyphs[TEXT_AREA] - 1;
19940 start = end + r1->used[TEXT_AREA];
19941 while (end < start
19942 && NILP ((end + 1)->object)
19943 && (end + 1)->charpos <= 0)
19944 ++end;
19945 if (end < start)
19947 if (EQ ((end + 1)->object, it->object))
19948 seen_this_string = 1;
19950 else
19951 seen_this_string = 1;
19954 /* Take note of each display string that covers a newline only
19955 once, the first time we see it. This is for when a display
19956 string includes more than one newline in it. */
19957 if (row->ends_in_newline_from_string_p && !seen_this_string)
19959 /* If we were scanning the buffer forward when we displayed
19960 the string, we want to account for at least one buffer
19961 position that belongs to this row (position covered by
19962 the display string), so that cursor positioning will
19963 consider this row as a candidate when point is at the end
19964 of the visual line represented by this row. This is not
19965 required when scanning back, because max_pos will already
19966 have a much larger value. */
19967 if (CHARPOS (row->end.pos) > max_pos)
19968 INC_BOTH (max_pos, max_bpos);
19969 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
19971 else if (CHARPOS (it->eol_pos) > 0)
19972 SET_TEXT_POS (row->maxpos,
19973 CHARPOS (it->eol_pos) + 1, BYTEPOS (it->eol_pos) + 1);
19974 else if (row->continued_p)
19976 /* If max_pos is different from IT's current position, it
19977 means IT->method does not belong to the display element
19978 at max_pos. However, it also means that the display
19979 element at max_pos was displayed in its entirety on this
19980 line, which is equivalent to saying that the next line
19981 starts at the next buffer position. */
19982 if (IT_CHARPOS (*it) == max_pos && it->method != GET_FROM_BUFFER)
19983 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
19984 else
19986 INC_BOTH (max_pos, max_bpos);
19987 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
19990 else if (row->truncated_on_right_p)
19991 /* display_line already called reseat_at_next_visible_line_start,
19992 which puts the iterator at the beginning of the next line, in
19993 the logical order. */
19994 row->maxpos = it->current.pos;
19995 else if (max_pos == min_pos && it->method != GET_FROM_BUFFER)
19996 /* A line that is entirely from a string/image/stretch... */
19997 row->maxpos = row->minpos;
19998 else
19999 emacs_abort ();
20001 else
20002 row->maxpos = it->current.pos;
20005 /* Construct the glyph row IT->glyph_row in the desired matrix of
20006 IT->w from text at the current position of IT. See dispextern.h
20007 for an overview of struct it. Value is non-zero if
20008 IT->glyph_row displays text, as opposed to a line displaying ZV
20009 only. */
20011 static int
20012 display_line (struct it *it)
20014 struct glyph_row *row = it->glyph_row;
20015 Lisp_Object overlay_arrow_string;
20016 struct it wrap_it;
20017 void *wrap_data = NULL;
20018 int may_wrap = 0, wrap_x IF_LINT (= 0);
20019 int wrap_row_used = -1;
20020 int wrap_row_ascent IF_LINT (= 0), wrap_row_height IF_LINT (= 0);
20021 int wrap_row_phys_ascent IF_LINT (= 0), wrap_row_phys_height IF_LINT (= 0);
20022 int wrap_row_extra_line_spacing IF_LINT (= 0);
20023 ptrdiff_t wrap_row_min_pos IF_LINT (= 0), wrap_row_min_bpos IF_LINT (= 0);
20024 ptrdiff_t wrap_row_max_pos IF_LINT (= 0), wrap_row_max_bpos IF_LINT (= 0);
20025 int cvpos;
20026 ptrdiff_t min_pos = ZV + 1, max_pos = 0;
20027 ptrdiff_t min_bpos IF_LINT (= 0), max_bpos IF_LINT (= 0);
20028 bool pending_handle_line_prefix = false;
20030 /* We always start displaying at hpos zero even if hscrolled. */
20031 eassert (it->hpos == 0 && it->current_x == 0);
20033 if (MATRIX_ROW_VPOS (row, it->w->desired_matrix)
20034 >= it->w->desired_matrix->nrows)
20036 it->w->nrows_scale_factor++;
20037 it->f->fonts_changed = 1;
20038 return 0;
20041 /* Clear the result glyph row and enable it. */
20042 prepare_desired_row (it->w, row, false);
20044 row->y = it->current_y;
20045 row->start = it->start;
20046 row->continuation_lines_width = it->continuation_lines_width;
20047 row->displays_text_p = 1;
20048 row->starts_in_middle_of_char_p = it->starts_in_middle_of_char_p;
20049 it->starts_in_middle_of_char_p = 0;
20051 /* Arrange the overlays nicely for our purposes. Usually, we call
20052 display_line on only one line at a time, in which case this
20053 can't really hurt too much, or we call it on lines which appear
20054 one after another in the buffer, in which case all calls to
20055 recenter_overlay_lists but the first will be pretty cheap. */
20056 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
20058 /* Move over display elements that are not visible because we are
20059 hscrolled. This may stop at an x-position < IT->first_visible_x
20060 if the first glyph is partially visible or if we hit a line end. */
20061 if (it->current_x < it->first_visible_x)
20063 enum move_it_result move_result;
20065 this_line_min_pos = row->start.pos;
20066 move_result = move_it_in_display_line_to (it, ZV, it->first_visible_x,
20067 MOVE_TO_POS | MOVE_TO_X);
20068 /* If we are under a large hscroll, move_it_in_display_line_to
20069 could hit the end of the line without reaching
20070 it->first_visible_x. Pretend that we did reach it. This is
20071 especially important on a TTY, where we will call
20072 extend_face_to_end_of_line, which needs to know how many
20073 blank glyphs to produce. */
20074 if (it->current_x < it->first_visible_x
20075 && (move_result == MOVE_NEWLINE_OR_CR
20076 || move_result == MOVE_POS_MATCH_OR_ZV))
20077 it->current_x = it->first_visible_x;
20079 /* Record the smallest positions seen while we moved over
20080 display elements that are not visible. This is needed by
20081 redisplay_internal for optimizing the case where the cursor
20082 stays inside the same line. The rest of this function only
20083 considers positions that are actually displayed, so
20084 RECORD_MAX_MIN_POS will not otherwise record positions that
20085 are hscrolled to the left of the left edge of the window. */
20086 min_pos = CHARPOS (this_line_min_pos);
20087 min_bpos = BYTEPOS (this_line_min_pos);
20089 else if (it->area == TEXT_AREA)
20091 /* We only do this when not calling move_it_in_display_line_to
20092 above, because that function calls itself handle_line_prefix. */
20093 handle_line_prefix (it);
20095 else
20097 /* Line-prefix and wrap-prefix are always displayed in the text
20098 area. But if this is the first call to display_line after
20099 init_iterator, the iterator might have been set up to write
20100 into a marginal area, e.g. if the line begins with some
20101 display property that writes to the margins. So we need to
20102 wait with the call to handle_line_prefix until whatever
20103 writes to the margin has done its job. */
20104 pending_handle_line_prefix = true;
20107 /* Get the initial row height. This is either the height of the
20108 text hscrolled, if there is any, or zero. */
20109 row->ascent = it->max_ascent;
20110 row->height = it->max_ascent + it->max_descent;
20111 row->phys_ascent = it->max_phys_ascent;
20112 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
20113 row->extra_line_spacing = it->max_extra_line_spacing;
20115 /* Utility macro to record max and min buffer positions seen until now. */
20116 #define RECORD_MAX_MIN_POS(IT) \
20117 do \
20119 int composition_p = !STRINGP ((IT)->string) \
20120 && ((IT)->what == IT_COMPOSITION); \
20121 ptrdiff_t current_pos = \
20122 composition_p ? (IT)->cmp_it.charpos \
20123 : IT_CHARPOS (*(IT)); \
20124 ptrdiff_t current_bpos = \
20125 composition_p ? CHAR_TO_BYTE (current_pos) \
20126 : IT_BYTEPOS (*(IT)); \
20127 if (current_pos < min_pos) \
20129 min_pos = current_pos; \
20130 min_bpos = current_bpos; \
20132 if (IT_CHARPOS (*it) > max_pos) \
20134 max_pos = IT_CHARPOS (*it); \
20135 max_bpos = IT_BYTEPOS (*it); \
20138 while (0)
20140 /* Loop generating characters. The loop is left with IT on the next
20141 character to display. */
20142 while (1)
20144 int n_glyphs_before, hpos_before, x_before;
20145 int x, nglyphs;
20146 int ascent = 0, descent = 0, phys_ascent = 0, phys_descent = 0;
20148 /* Retrieve the next thing to display. Value is zero if end of
20149 buffer reached. */
20150 if (!get_next_display_element (it))
20152 /* Maybe add a space at the end of this line that is used to
20153 display the cursor there under X. Set the charpos of the
20154 first glyph of blank lines not corresponding to any text
20155 to -1. */
20156 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
20157 row->exact_window_width_line_p = 1;
20158 else if ((append_space_for_newline (it, 1) && row->used[TEXT_AREA] == 1)
20159 || row->used[TEXT_AREA] == 0)
20161 row->glyphs[TEXT_AREA]->charpos = -1;
20162 row->displays_text_p = 0;
20164 if (!NILP (BVAR (XBUFFER (it->w->contents), indicate_empty_lines))
20165 && (!MINI_WINDOW_P (it->w)
20166 || (minibuf_level && EQ (it->window, minibuf_window))))
20167 row->indicate_empty_line_p = 1;
20170 it->continuation_lines_width = 0;
20171 row->ends_at_zv_p = 1;
20172 /* A row that displays right-to-left text must always have
20173 its last face extended all the way to the end of line,
20174 even if this row ends in ZV, because we still write to
20175 the screen left to right. We also need to extend the
20176 last face if the default face is remapped to some
20177 different face, otherwise the functions that clear
20178 portions of the screen will clear with the default face's
20179 background color. */
20180 if (row->reversed_p
20181 || lookup_basic_face (it->f, DEFAULT_FACE_ID) != DEFAULT_FACE_ID)
20182 extend_face_to_end_of_line (it);
20183 break;
20186 /* Now, get the metrics of what we want to display. This also
20187 generates glyphs in `row' (which is IT->glyph_row). */
20188 n_glyphs_before = row->used[TEXT_AREA];
20189 x = it->current_x;
20191 /* Remember the line height so far in case the next element doesn't
20192 fit on the line. */
20193 if (it->line_wrap != TRUNCATE)
20195 ascent = it->max_ascent;
20196 descent = it->max_descent;
20197 phys_ascent = it->max_phys_ascent;
20198 phys_descent = it->max_phys_descent;
20200 if (it->line_wrap == WORD_WRAP && it->area == TEXT_AREA)
20202 if (IT_DISPLAYING_WHITESPACE (it))
20203 may_wrap = 1;
20204 else if (may_wrap)
20206 SAVE_IT (wrap_it, *it, wrap_data);
20207 wrap_x = x;
20208 wrap_row_used = row->used[TEXT_AREA];
20209 wrap_row_ascent = row->ascent;
20210 wrap_row_height = row->height;
20211 wrap_row_phys_ascent = row->phys_ascent;
20212 wrap_row_phys_height = row->phys_height;
20213 wrap_row_extra_line_spacing = row->extra_line_spacing;
20214 wrap_row_min_pos = min_pos;
20215 wrap_row_min_bpos = min_bpos;
20216 wrap_row_max_pos = max_pos;
20217 wrap_row_max_bpos = max_bpos;
20218 may_wrap = 0;
20223 PRODUCE_GLYPHS (it);
20225 /* If this display element was in marginal areas, continue with
20226 the next one. */
20227 if (it->area != TEXT_AREA)
20229 row->ascent = max (row->ascent, it->max_ascent);
20230 row->height = max (row->height, it->max_ascent + it->max_descent);
20231 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
20232 row->phys_height = max (row->phys_height,
20233 it->max_phys_ascent + it->max_phys_descent);
20234 row->extra_line_spacing = max (row->extra_line_spacing,
20235 it->max_extra_line_spacing);
20236 set_iterator_to_next (it, 1);
20237 /* If we didn't handle the line/wrap prefix above, and the
20238 call to set_iterator_to_next just switched to TEXT_AREA,
20239 process the prefix now. */
20240 if (it->area == TEXT_AREA && pending_handle_line_prefix)
20242 pending_handle_line_prefix = false;
20243 handle_line_prefix (it);
20245 continue;
20248 /* Does the display element fit on the line? If we truncate
20249 lines, we should draw past the right edge of the window. If
20250 we don't truncate, we want to stop so that we can display the
20251 continuation glyph before the right margin. If lines are
20252 continued, there are two possible strategies for characters
20253 resulting in more than 1 glyph (e.g. tabs): Display as many
20254 glyphs as possible in this line and leave the rest for the
20255 continuation line, or display the whole element in the next
20256 line. Original redisplay did the former, so we do it also. */
20257 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
20258 hpos_before = it->hpos;
20259 x_before = x;
20261 if (/* Not a newline. */
20262 nglyphs > 0
20263 /* Glyphs produced fit entirely in the line. */
20264 && it->current_x < it->last_visible_x)
20266 it->hpos += nglyphs;
20267 row->ascent = max (row->ascent, it->max_ascent);
20268 row->height = max (row->height, it->max_ascent + it->max_descent);
20269 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
20270 row->phys_height = max (row->phys_height,
20271 it->max_phys_ascent + it->max_phys_descent);
20272 row->extra_line_spacing = max (row->extra_line_spacing,
20273 it->max_extra_line_spacing);
20274 if (it->current_x - it->pixel_width < it->first_visible_x
20275 /* In R2L rows, we arrange in extend_face_to_end_of_line
20276 to add a right offset to the line, by a suitable
20277 change to the stretch glyph that is the leftmost
20278 glyph of the line. */
20279 && !row->reversed_p)
20280 row->x = x - it->first_visible_x;
20281 /* Record the maximum and minimum buffer positions seen so
20282 far in glyphs that will be displayed by this row. */
20283 if (it->bidi_p)
20284 RECORD_MAX_MIN_POS (it);
20286 else
20288 int i, new_x;
20289 struct glyph *glyph;
20291 for (i = 0; i < nglyphs; ++i, x = new_x)
20293 /* Identify the glyphs added by the last call to
20294 PRODUCE_GLYPHS. In R2L rows, they are prepended to
20295 the previous glyphs. */
20296 if (!row->reversed_p)
20297 glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
20298 else
20299 glyph = row->glyphs[TEXT_AREA] + nglyphs - 1 - i;
20300 new_x = x + glyph->pixel_width;
20302 if (/* Lines are continued. */
20303 it->line_wrap != TRUNCATE
20304 && (/* Glyph doesn't fit on the line. */
20305 new_x > it->last_visible_x
20306 /* Or it fits exactly on a window system frame. */
20307 || (new_x == it->last_visible_x
20308 && FRAME_WINDOW_P (it->f)
20309 && (row->reversed_p
20310 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
20311 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)))))
20313 /* End of a continued line. */
20315 if (it->hpos == 0
20316 || (new_x == it->last_visible_x
20317 && FRAME_WINDOW_P (it->f)
20318 && (row->reversed_p
20319 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
20320 : WINDOW_RIGHT_FRINGE_WIDTH (it->w))))
20322 /* Current glyph is the only one on the line or
20323 fits exactly on the line. We must continue
20324 the line because we can't draw the cursor
20325 after the glyph. */
20326 row->continued_p = 1;
20327 it->current_x = new_x;
20328 it->continuation_lines_width += new_x;
20329 ++it->hpos;
20330 if (i == nglyphs - 1)
20332 /* If line-wrap is on, check if a previous
20333 wrap point was found. */
20334 if (!IT_OVERFLOW_NEWLINE_INTO_FRINGE (it)
20335 && wrap_row_used > 0
20336 /* Even if there is a previous wrap
20337 point, continue the line here as
20338 usual, if (i) the previous character
20339 was a space or tab AND (ii) the
20340 current character is not. */
20341 && (!may_wrap
20342 || IT_DISPLAYING_WHITESPACE (it)))
20343 goto back_to_wrap;
20345 /* Record the maximum and minimum buffer
20346 positions seen so far in glyphs that will be
20347 displayed by this row. */
20348 if (it->bidi_p)
20349 RECORD_MAX_MIN_POS (it);
20350 set_iterator_to_next (it, 1);
20351 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
20353 if (!get_next_display_element (it))
20355 row->exact_window_width_line_p = 1;
20356 it->continuation_lines_width = 0;
20357 row->continued_p = 0;
20358 row->ends_at_zv_p = 1;
20360 else if (ITERATOR_AT_END_OF_LINE_P (it))
20362 row->continued_p = 0;
20363 row->exact_window_width_line_p = 1;
20365 /* If line-wrap is on, check if a
20366 previous wrap point was found. */
20367 else if (wrap_row_used > 0
20368 /* Even if there is a previous wrap
20369 point, continue the line here as
20370 usual, if (i) the previous character
20371 was a space or tab AND (ii) the
20372 current character is not. */
20373 && (!may_wrap
20374 || IT_DISPLAYING_WHITESPACE (it)))
20375 goto back_to_wrap;
20379 else if (it->bidi_p)
20380 RECORD_MAX_MIN_POS (it);
20381 if (WINDOW_LEFT_MARGIN_WIDTH (it->w) > 0
20382 || WINDOW_RIGHT_MARGIN_WIDTH (it->w) > 0)
20383 extend_face_to_end_of_line (it);
20385 else if (CHAR_GLYPH_PADDING_P (*glyph)
20386 && !FRAME_WINDOW_P (it->f))
20388 /* A padding glyph that doesn't fit on this line.
20389 This means the whole character doesn't fit
20390 on the line. */
20391 if (row->reversed_p)
20392 unproduce_glyphs (it, row->used[TEXT_AREA]
20393 - n_glyphs_before);
20394 row->used[TEXT_AREA] = n_glyphs_before;
20396 /* Fill the rest of the row with continuation
20397 glyphs like in 20.x. */
20398 while (row->glyphs[TEXT_AREA] + row->used[TEXT_AREA]
20399 < row->glyphs[1 + TEXT_AREA])
20400 produce_special_glyphs (it, IT_CONTINUATION);
20402 row->continued_p = 1;
20403 it->current_x = x_before;
20404 it->continuation_lines_width += x_before;
20406 /* Restore the height to what it was before the
20407 element not fitting on the line. */
20408 it->max_ascent = ascent;
20409 it->max_descent = descent;
20410 it->max_phys_ascent = phys_ascent;
20411 it->max_phys_descent = phys_descent;
20412 if (WINDOW_LEFT_MARGIN_WIDTH (it->w) > 0
20413 || WINDOW_RIGHT_MARGIN_WIDTH (it->w) > 0)
20414 extend_face_to_end_of_line (it);
20416 else if (wrap_row_used > 0)
20418 back_to_wrap:
20419 if (row->reversed_p)
20420 unproduce_glyphs (it,
20421 row->used[TEXT_AREA] - wrap_row_used);
20422 RESTORE_IT (it, &wrap_it, wrap_data);
20423 it->continuation_lines_width += wrap_x;
20424 row->used[TEXT_AREA] = wrap_row_used;
20425 row->ascent = wrap_row_ascent;
20426 row->height = wrap_row_height;
20427 row->phys_ascent = wrap_row_phys_ascent;
20428 row->phys_height = wrap_row_phys_height;
20429 row->extra_line_spacing = wrap_row_extra_line_spacing;
20430 min_pos = wrap_row_min_pos;
20431 min_bpos = wrap_row_min_bpos;
20432 max_pos = wrap_row_max_pos;
20433 max_bpos = wrap_row_max_bpos;
20434 row->continued_p = 1;
20435 row->ends_at_zv_p = 0;
20436 row->exact_window_width_line_p = 0;
20437 it->continuation_lines_width += x;
20439 /* Make sure that a non-default face is extended
20440 up to the right margin of the window. */
20441 extend_face_to_end_of_line (it);
20443 else if (it->c == '\t' && FRAME_WINDOW_P (it->f))
20445 /* A TAB that extends past the right edge of the
20446 window. This produces a single glyph on
20447 window system frames. We leave the glyph in
20448 this row and let it fill the row, but don't
20449 consume the TAB. */
20450 if ((row->reversed_p
20451 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
20452 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
20453 produce_special_glyphs (it, IT_CONTINUATION);
20454 it->continuation_lines_width += it->last_visible_x;
20455 row->ends_in_middle_of_char_p = 1;
20456 row->continued_p = 1;
20457 glyph->pixel_width = it->last_visible_x - x;
20458 it->starts_in_middle_of_char_p = 1;
20459 if (WINDOW_LEFT_MARGIN_WIDTH (it->w) > 0
20460 || WINDOW_RIGHT_MARGIN_WIDTH (it->w) > 0)
20461 extend_face_to_end_of_line (it);
20463 else
20465 /* Something other than a TAB that draws past
20466 the right edge of the window. Restore
20467 positions to values before the element. */
20468 if (row->reversed_p)
20469 unproduce_glyphs (it, row->used[TEXT_AREA]
20470 - (n_glyphs_before + i));
20471 row->used[TEXT_AREA] = n_glyphs_before + i;
20473 /* Display continuation glyphs. */
20474 it->current_x = x_before;
20475 it->continuation_lines_width += x;
20476 if (!FRAME_WINDOW_P (it->f)
20477 || (row->reversed_p
20478 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
20479 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
20480 produce_special_glyphs (it, IT_CONTINUATION);
20481 row->continued_p = 1;
20483 extend_face_to_end_of_line (it);
20485 if (nglyphs > 1 && i > 0)
20487 row->ends_in_middle_of_char_p = 1;
20488 it->starts_in_middle_of_char_p = 1;
20491 /* Restore the height to what it was before the
20492 element not fitting on the line. */
20493 it->max_ascent = ascent;
20494 it->max_descent = descent;
20495 it->max_phys_ascent = phys_ascent;
20496 it->max_phys_descent = phys_descent;
20499 break;
20501 else if (new_x > it->first_visible_x)
20503 /* Increment number of glyphs actually displayed. */
20504 ++it->hpos;
20506 /* Record the maximum and minimum buffer positions
20507 seen so far in glyphs that will be displayed by
20508 this row. */
20509 if (it->bidi_p)
20510 RECORD_MAX_MIN_POS (it);
20512 if (x < it->first_visible_x && !row->reversed_p)
20513 /* Glyph is partially visible, i.e. row starts at
20514 negative X position. Don't do that in R2L
20515 rows, where we arrange to add a right offset to
20516 the line in extend_face_to_end_of_line, by a
20517 suitable change to the stretch glyph that is
20518 the leftmost glyph of the line. */
20519 row->x = x - it->first_visible_x;
20520 /* When the last glyph of an R2L row only fits
20521 partially on the line, we need to set row->x to a
20522 negative offset, so that the leftmost glyph is
20523 the one that is partially visible. But if we are
20524 going to produce the truncation glyph, this will
20525 be taken care of in produce_special_glyphs. */
20526 if (row->reversed_p
20527 && new_x > it->last_visible_x
20528 && !(it->line_wrap == TRUNCATE
20529 && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0))
20531 eassert (FRAME_WINDOW_P (it->f));
20532 row->x = it->last_visible_x - new_x;
20535 else
20537 /* Glyph is completely off the left margin of the
20538 window. This should not happen because of the
20539 move_it_in_display_line at the start of this
20540 function, unless the text display area of the
20541 window is empty. */
20542 eassert (it->first_visible_x <= it->last_visible_x);
20545 /* Even if this display element produced no glyphs at all,
20546 we want to record its position. */
20547 if (it->bidi_p && nglyphs == 0)
20548 RECORD_MAX_MIN_POS (it);
20550 row->ascent = max (row->ascent, it->max_ascent);
20551 row->height = max (row->height, it->max_ascent + it->max_descent);
20552 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
20553 row->phys_height = max (row->phys_height,
20554 it->max_phys_ascent + it->max_phys_descent);
20555 row->extra_line_spacing = max (row->extra_line_spacing,
20556 it->max_extra_line_spacing);
20558 /* End of this display line if row is continued. */
20559 if (row->continued_p || row->ends_at_zv_p)
20560 break;
20563 at_end_of_line:
20564 /* Is this a line end? If yes, we're also done, after making
20565 sure that a non-default face is extended up to the right
20566 margin of the window. */
20567 if (ITERATOR_AT_END_OF_LINE_P (it))
20569 int used_before = row->used[TEXT_AREA];
20571 row->ends_in_newline_from_string_p = STRINGP (it->object);
20573 /* Add a space at the end of the line that is used to
20574 display the cursor there. */
20575 if (!IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
20576 append_space_for_newline (it, 0);
20578 /* Extend the face to the end of the line. */
20579 extend_face_to_end_of_line (it);
20581 /* Make sure we have the position. */
20582 if (used_before == 0)
20583 row->glyphs[TEXT_AREA]->charpos = CHARPOS (it->position);
20585 /* Record the position of the newline, for use in
20586 find_row_edges. */
20587 it->eol_pos = it->current.pos;
20589 /* Consume the line end. This skips over invisible lines. */
20590 set_iterator_to_next (it, 1);
20591 it->continuation_lines_width = 0;
20592 break;
20595 /* Proceed with next display element. Note that this skips
20596 over lines invisible because of selective display. */
20597 set_iterator_to_next (it, 1);
20599 /* If we truncate lines, we are done when the last displayed
20600 glyphs reach past the right margin of the window. */
20601 if (it->line_wrap == TRUNCATE
20602 && ((FRAME_WINDOW_P (it->f)
20603 /* Images are preprocessed in produce_image_glyph such
20604 that they are cropped at the right edge of the
20605 window, so an image glyph will always end exactly at
20606 last_visible_x, even if there's no right fringe. */
20607 && ((row->reversed_p
20608 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
20609 : WINDOW_RIGHT_FRINGE_WIDTH (it->w))
20610 || it->what == IT_IMAGE))
20611 ? (it->current_x >= it->last_visible_x)
20612 : (it->current_x > it->last_visible_x)))
20614 /* Maybe add truncation glyphs. */
20615 if (!FRAME_WINDOW_P (it->f)
20616 || (row->reversed_p
20617 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
20618 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
20620 int i, n;
20622 if (!row->reversed_p)
20624 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
20625 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
20626 break;
20628 else
20630 for (i = 0; i < row->used[TEXT_AREA]; i++)
20631 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
20632 break;
20633 /* Remove any padding glyphs at the front of ROW, to
20634 make room for the truncation glyphs we will be
20635 adding below. The loop below always inserts at
20636 least one truncation glyph, so also remove the
20637 last glyph added to ROW. */
20638 unproduce_glyphs (it, i + 1);
20639 /* Adjust i for the loop below. */
20640 i = row->used[TEXT_AREA] - (i + 1);
20643 /* produce_special_glyphs overwrites the last glyph, so
20644 we don't want that if we want to keep that last
20645 glyph, which means it's an image. */
20646 if (it->current_x > it->last_visible_x)
20648 it->current_x = x_before;
20649 if (!FRAME_WINDOW_P (it->f))
20651 for (n = row->used[TEXT_AREA]; i < n; ++i)
20653 row->used[TEXT_AREA] = i;
20654 produce_special_glyphs (it, IT_TRUNCATION);
20657 else
20659 row->used[TEXT_AREA] = i;
20660 produce_special_glyphs (it, IT_TRUNCATION);
20662 it->hpos = hpos_before;
20665 else if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
20667 /* Don't truncate if we can overflow newline into fringe. */
20668 if (!get_next_display_element (it))
20670 it->continuation_lines_width = 0;
20671 row->ends_at_zv_p = 1;
20672 row->exact_window_width_line_p = 1;
20673 break;
20675 if (ITERATOR_AT_END_OF_LINE_P (it))
20677 row->exact_window_width_line_p = 1;
20678 goto at_end_of_line;
20680 it->current_x = x_before;
20681 it->hpos = hpos_before;
20684 row->truncated_on_right_p = 1;
20685 it->continuation_lines_width = 0;
20686 reseat_at_next_visible_line_start (it, 0);
20687 /* We insist below that IT's position be at ZV because in
20688 bidi-reordered lines the character at visible line start
20689 might not be the character that follows the newline in
20690 the logical order. */
20691 if (IT_BYTEPOS (*it) > BEG_BYTE)
20692 row->ends_at_zv_p =
20693 IT_BYTEPOS (*it) >= ZV_BYTE && FETCH_BYTE (ZV_BYTE - 1) != '\n';
20694 else
20695 row->ends_at_zv_p = false;
20696 break;
20700 if (wrap_data)
20701 bidi_unshelve_cache (wrap_data, 1);
20703 /* If line is not empty and hscrolled, maybe insert truncation glyphs
20704 at the left window margin. */
20705 if (it->first_visible_x
20706 && IT_CHARPOS (*it) != CHARPOS (row->start.pos))
20708 if (!FRAME_WINDOW_P (it->f)
20709 || (((row->reversed_p
20710 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
20711 : WINDOW_LEFT_FRINGE_WIDTH (it->w)) == 0)
20712 /* Don't let insert_left_trunc_glyphs overwrite the
20713 first glyph of the row if it is an image. */
20714 && row->glyphs[TEXT_AREA]->type != IMAGE_GLYPH))
20715 insert_left_trunc_glyphs (it);
20716 row->truncated_on_left_p = 1;
20719 /* Remember the position at which this line ends.
20721 BIDI Note: any code that needs MATRIX_ROW_START/END_CHARPOS
20722 cannot be before the call to find_row_edges below, since that is
20723 where these positions are determined. */
20724 row->end = it->current;
20725 if (!it->bidi_p)
20727 row->minpos = row->start.pos;
20728 row->maxpos = row->end.pos;
20730 else
20732 /* ROW->minpos and ROW->maxpos must be the smallest and
20733 `1 + the largest' buffer positions in ROW. But if ROW was
20734 bidi-reordered, these two positions can be anywhere in the
20735 row, so we must determine them now. */
20736 find_row_edges (it, row, min_pos, min_bpos, max_pos, max_bpos);
20739 /* If the start of this line is the overlay arrow-position, then
20740 mark this glyph row as the one containing the overlay arrow.
20741 This is clearly a mess with variable size fonts. It would be
20742 better to let it be displayed like cursors under X. */
20743 if ((MATRIX_ROW_DISPLAYS_TEXT_P (row) || !overlay_arrow_seen)
20744 && (overlay_arrow_string = overlay_arrow_at_row (it, row),
20745 !NILP (overlay_arrow_string)))
20747 /* Overlay arrow in window redisplay is a fringe bitmap. */
20748 if (STRINGP (overlay_arrow_string))
20750 struct glyph_row *arrow_row
20751 = get_overlay_arrow_glyph_row (it->w, overlay_arrow_string);
20752 struct glyph *glyph = arrow_row->glyphs[TEXT_AREA];
20753 struct glyph *arrow_end = glyph + arrow_row->used[TEXT_AREA];
20754 struct glyph *p = row->glyphs[TEXT_AREA];
20755 struct glyph *p2, *end;
20757 /* Copy the arrow glyphs. */
20758 while (glyph < arrow_end)
20759 *p++ = *glyph++;
20761 /* Throw away padding glyphs. */
20762 p2 = p;
20763 end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
20764 while (p2 < end && CHAR_GLYPH_PADDING_P (*p2))
20765 ++p2;
20766 if (p2 > p)
20768 while (p2 < end)
20769 *p++ = *p2++;
20770 row->used[TEXT_AREA] = p2 - row->glyphs[TEXT_AREA];
20773 else
20775 eassert (INTEGERP (overlay_arrow_string));
20776 row->overlay_arrow_bitmap = XINT (overlay_arrow_string);
20778 overlay_arrow_seen = 1;
20781 /* Highlight trailing whitespace. */
20782 if (!NILP (Vshow_trailing_whitespace))
20783 highlight_trailing_whitespace (it->f, it->glyph_row);
20785 /* Compute pixel dimensions of this line. */
20786 compute_line_metrics (it);
20788 /* Implementation note: No changes in the glyphs of ROW or in their
20789 faces can be done past this point, because compute_line_metrics
20790 computes ROW's hash value and stores it within the glyph_row
20791 structure. */
20793 /* Record whether this row ends inside an ellipsis. */
20794 row->ends_in_ellipsis_p
20795 = (it->method == GET_FROM_DISPLAY_VECTOR
20796 && it->ellipsis_p);
20798 /* Save fringe bitmaps in this row. */
20799 row->left_user_fringe_bitmap = it->left_user_fringe_bitmap;
20800 row->left_user_fringe_face_id = it->left_user_fringe_face_id;
20801 row->right_user_fringe_bitmap = it->right_user_fringe_bitmap;
20802 row->right_user_fringe_face_id = it->right_user_fringe_face_id;
20804 it->left_user_fringe_bitmap = 0;
20805 it->left_user_fringe_face_id = 0;
20806 it->right_user_fringe_bitmap = 0;
20807 it->right_user_fringe_face_id = 0;
20809 /* Maybe set the cursor. */
20810 cvpos = it->w->cursor.vpos;
20811 if ((cvpos < 0
20812 /* In bidi-reordered rows, keep checking for proper cursor
20813 position even if one has been found already, because buffer
20814 positions in such rows change non-linearly with ROW->VPOS,
20815 when a line is continued. One exception: when we are at ZV,
20816 display cursor on the first suitable glyph row, since all
20817 the empty rows after that also have their position set to ZV. */
20818 /* FIXME: Revisit this when glyph ``spilling'' in continuation
20819 lines' rows is implemented for bidi-reordered rows. */
20820 || (it->bidi_p
20821 && !MATRIX_ROW (it->w->desired_matrix, cvpos)->ends_at_zv_p))
20822 && PT >= MATRIX_ROW_START_CHARPOS (row)
20823 && PT <= MATRIX_ROW_END_CHARPOS (row)
20824 && cursor_row_p (row))
20825 set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
20827 /* Prepare for the next line. This line starts horizontally at (X
20828 HPOS) = (0 0). Vertical positions are incremented. As a
20829 convenience for the caller, IT->glyph_row is set to the next
20830 row to be used. */
20831 it->current_x = it->hpos = 0;
20832 it->current_y += row->height;
20833 SET_TEXT_POS (it->eol_pos, 0, 0);
20834 ++it->vpos;
20835 ++it->glyph_row;
20836 /* The next row should by default use the same value of the
20837 reversed_p flag as this one. set_iterator_to_next decides when
20838 it's a new paragraph, and PRODUCE_GLYPHS recomputes the value of
20839 the flag accordingly. */
20840 if (it->glyph_row < MATRIX_BOTTOM_TEXT_ROW (it->w->desired_matrix, it->w))
20841 it->glyph_row->reversed_p = row->reversed_p;
20842 it->start = row->end;
20843 return MATRIX_ROW_DISPLAYS_TEXT_P (row);
20845 #undef RECORD_MAX_MIN_POS
20848 DEFUN ("current-bidi-paragraph-direction", Fcurrent_bidi_paragraph_direction,
20849 Scurrent_bidi_paragraph_direction, 0, 1, 0,
20850 doc: /* Return paragraph direction at point in BUFFER.
20851 Value is either `left-to-right' or `right-to-left'.
20852 If BUFFER is omitted or nil, it defaults to the current buffer.
20854 Paragraph direction determines how the text in the paragraph is displayed.
20855 In left-to-right paragraphs, text begins at the left margin of the window
20856 and the reading direction is generally left to right. In right-to-left
20857 paragraphs, text begins at the right margin and is read from right to left.
20859 See also `bidi-paragraph-direction'. */)
20860 (Lisp_Object buffer)
20862 struct buffer *buf = current_buffer;
20863 struct buffer *old = buf;
20865 if (! NILP (buffer))
20867 CHECK_BUFFER (buffer);
20868 buf = XBUFFER (buffer);
20871 if (NILP (BVAR (buf, bidi_display_reordering))
20872 || NILP (BVAR (buf, enable_multibyte_characters))
20873 /* When we are loading loadup.el, the character property tables
20874 needed for bidi iteration are not yet available. */
20875 || !NILP (Vpurify_flag))
20876 return Qleft_to_right;
20877 else if (!NILP (BVAR (buf, bidi_paragraph_direction)))
20878 return BVAR (buf, bidi_paragraph_direction);
20879 else
20881 /* Determine the direction from buffer text. We could try to
20882 use current_matrix if it is up to date, but this seems fast
20883 enough as it is. */
20884 struct bidi_it itb;
20885 ptrdiff_t pos = BUF_PT (buf);
20886 ptrdiff_t bytepos = BUF_PT_BYTE (buf);
20887 int c;
20888 void *itb_data = bidi_shelve_cache ();
20890 set_buffer_temp (buf);
20891 /* bidi_paragraph_init finds the base direction of the paragraph
20892 by searching forward from paragraph start. We need the base
20893 direction of the current or _previous_ paragraph, so we need
20894 to make sure we are within that paragraph. To that end, find
20895 the previous non-empty line. */
20896 if (pos >= ZV && pos > BEGV)
20897 DEC_BOTH (pos, bytepos);
20898 AUTO_STRING (trailing_white_space, "[\f\t ]*\n");
20899 if (fast_looking_at (trailing_white_space,
20900 pos, bytepos, ZV, ZV_BYTE, Qnil) > 0)
20902 while ((c = FETCH_BYTE (bytepos)) == '\n'
20903 || c == ' ' || c == '\t' || c == '\f')
20905 if (bytepos <= BEGV_BYTE)
20906 break;
20907 bytepos--;
20908 pos--;
20910 while (!CHAR_HEAD_P (FETCH_BYTE (bytepos)))
20911 bytepos--;
20913 bidi_init_it (pos, bytepos, FRAME_WINDOW_P (SELECTED_FRAME ()), &itb);
20914 itb.paragraph_dir = NEUTRAL_DIR;
20915 itb.string.s = NULL;
20916 itb.string.lstring = Qnil;
20917 itb.string.bufpos = 0;
20918 itb.string.from_disp_str = 0;
20919 itb.string.unibyte = 0;
20920 /* We have no window to use here for ignoring window-specific
20921 overlays. Using NULL for window pointer will cause
20922 compute_display_string_pos to use the current buffer. */
20923 itb.w = NULL;
20924 bidi_paragraph_init (NEUTRAL_DIR, &itb, 1);
20925 bidi_unshelve_cache (itb_data, 0);
20926 set_buffer_temp (old);
20927 switch (itb.paragraph_dir)
20929 case L2R:
20930 return Qleft_to_right;
20931 break;
20932 case R2L:
20933 return Qright_to_left;
20934 break;
20935 default:
20936 emacs_abort ();
20941 DEFUN ("bidi-find-overridden-directionality",
20942 Fbidi_find_overridden_directionality,
20943 Sbidi_find_overridden_directionality, 2, 3, 0,
20944 doc: /* Return position between FROM and TO where directionality was overridden.
20946 This function returns the first character position in the specified
20947 region of OBJECT where there is a character whose `bidi-class' property
20948 is `L', but which was forced to display as `R' by a directional
20949 override, and likewise with characters whose `bidi-class' is `R'
20950 or `AL' that were forced to display as `L'.
20952 If no such character is found, the function returns nil.
20954 OBJECT is a Lisp string or buffer to search for overridden
20955 directionality, and defaults to the current buffer if nil or omitted.
20956 OBJECT can also be a window, in which case the function will search
20957 the buffer displayed in that window. Passing the window instead of
20958 a buffer is preferable when the buffer is displayed in some window,
20959 because this function will then be able to correctly account for
20960 window-specific overlays, which can affect the results.
20962 Strong directional characters `L', `R', and `AL' can have their
20963 intrinsic directionality overridden by directional override
20964 control characters RLO \(u+202e) and LRO \(u+202d). See the
20965 function `get-char-code-property' for a way to inquire about
20966 the `bidi-class' property of a character. */)
20967 (Lisp_Object from, Lisp_Object to, Lisp_Object object)
20969 struct buffer *buf = current_buffer;
20970 struct buffer *old = buf;
20971 struct window *w = NULL;
20972 bool frame_window_p = FRAME_WINDOW_P (SELECTED_FRAME ());
20973 struct bidi_it itb;
20974 ptrdiff_t from_pos, to_pos, from_bpos;
20975 void *itb_data;
20977 if (!NILP (object))
20979 if (BUFFERP (object))
20980 buf = XBUFFER (object);
20981 else if (WINDOWP (object))
20983 w = decode_live_window (object);
20984 buf = XBUFFER (w->contents);
20985 frame_window_p = FRAME_WINDOW_P (XFRAME (w->frame));
20987 else
20988 CHECK_STRING (object);
20991 if (STRINGP (object))
20993 /* Characters in unibyte strings are always treated by bidi.c as
20994 strong LTR. */
20995 if (!STRING_MULTIBYTE (object)
20996 /* When we are loading loadup.el, the character property
20997 tables needed for bidi iteration are not yet
20998 available. */
20999 || !NILP (Vpurify_flag))
21000 return Qnil;
21002 validate_subarray (object, from, to, SCHARS (object), &from_pos, &to_pos);
21003 if (from_pos >= SCHARS (object))
21004 return Qnil;
21006 /* Set up the bidi iterator. */
21007 itb_data = bidi_shelve_cache ();
21008 itb.paragraph_dir = NEUTRAL_DIR;
21009 itb.string.lstring = object;
21010 itb.string.s = NULL;
21011 itb.string.schars = SCHARS (object);
21012 itb.string.bufpos = 0;
21013 itb.string.from_disp_str = 0;
21014 itb.string.unibyte = 0;
21015 itb.w = w;
21016 bidi_init_it (0, 0, frame_window_p, &itb);
21018 else
21020 /* Nothing this fancy can happen in unibyte buffers, or in a
21021 buffer that disabled reordering, or if FROM is at EOB. */
21022 if (NILP (BVAR (buf, bidi_display_reordering))
21023 || NILP (BVAR (buf, enable_multibyte_characters))
21024 /* When we are loading loadup.el, the character property
21025 tables needed for bidi iteration are not yet
21026 available. */
21027 || !NILP (Vpurify_flag))
21028 return Qnil;
21030 set_buffer_temp (buf);
21031 validate_region (&from, &to);
21032 from_pos = XINT (from);
21033 to_pos = XINT (to);
21034 if (from_pos >= ZV)
21035 return Qnil;
21037 /* Set up the bidi iterator. */
21038 itb_data = bidi_shelve_cache ();
21039 from_bpos = CHAR_TO_BYTE (from_pos);
21040 if (from_pos == BEGV)
21042 itb.charpos = BEGV;
21043 itb.bytepos = BEGV_BYTE;
21045 else if (FETCH_CHAR (from_bpos - 1) == '\n')
21047 itb.charpos = from_pos;
21048 itb.bytepos = from_bpos;
21050 else
21051 itb.charpos = find_newline_no_quit (from_pos, CHAR_TO_BYTE (from_pos),
21052 -1, &itb.bytepos);
21053 itb.paragraph_dir = NEUTRAL_DIR;
21054 itb.string.s = NULL;
21055 itb.string.lstring = Qnil;
21056 itb.string.bufpos = 0;
21057 itb.string.from_disp_str = 0;
21058 itb.string.unibyte = 0;
21059 itb.w = w;
21060 bidi_init_it (itb.charpos, itb.bytepos, frame_window_p, &itb);
21063 ptrdiff_t found;
21064 do {
21065 /* For the purposes of this function, the actual base direction of
21066 the paragraph doesn't matter, so just set it to L2R. */
21067 bidi_paragraph_init (L2R, &itb, 0);
21068 while ((found = bidi_find_first_overridden (&itb)) < from_pos)
21070 } while (found == ZV && itb.ch == '\n' && itb.charpos < to_pos);
21072 bidi_unshelve_cache (itb_data, 0);
21073 set_buffer_temp (old);
21075 return (from_pos <= found && found < to_pos) ? make_number (found) : Qnil;
21078 DEFUN ("move-point-visually", Fmove_point_visually,
21079 Smove_point_visually, 1, 1, 0,
21080 doc: /* Move point in the visual order in the specified DIRECTION.
21081 DIRECTION can be 1, meaning move to the right, or -1, which moves to the
21082 left.
21084 Value is the new character position of point. */)
21085 (Lisp_Object direction)
21087 struct window *w = XWINDOW (selected_window);
21088 struct buffer *b = XBUFFER (w->contents);
21089 struct glyph_row *row;
21090 int dir;
21091 Lisp_Object paragraph_dir;
21093 #define ROW_GLYPH_NEWLINE_P(ROW,GLYPH) \
21094 (!(ROW)->continued_p \
21095 && NILP ((GLYPH)->object) \
21096 && (GLYPH)->type == CHAR_GLYPH \
21097 && (GLYPH)->u.ch == ' ' \
21098 && (GLYPH)->charpos >= 0 \
21099 && !(GLYPH)->avoid_cursor_p)
21101 CHECK_NUMBER (direction);
21102 dir = XINT (direction);
21103 if (dir > 0)
21104 dir = 1;
21105 else
21106 dir = -1;
21108 /* If current matrix is up-to-date, we can use the information
21109 recorded in the glyphs, at least as long as the goal is on the
21110 screen. */
21111 if (w->window_end_valid
21112 && !windows_or_buffers_changed
21113 && b
21114 && !b->clip_changed
21115 && !b->prevent_redisplay_optimizations_p
21116 && !window_outdated (w)
21117 /* We rely below on the cursor coordinates to be up to date, but
21118 we cannot trust them if some command moved point since the
21119 last complete redisplay. */
21120 && w->last_point == BUF_PT (b)
21121 && w->cursor.vpos >= 0
21122 && w->cursor.vpos < w->current_matrix->nrows
21123 && (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos))->enabled_p)
21125 struct glyph *g = row->glyphs[TEXT_AREA];
21126 struct glyph *e = dir > 0 ? g + row->used[TEXT_AREA] : g - 1;
21127 struct glyph *gpt = g + w->cursor.hpos;
21129 for (g = gpt + dir; (dir > 0 ? g < e : g > e); g += dir)
21131 if (BUFFERP (g->object) && g->charpos != PT)
21133 SET_PT (g->charpos);
21134 w->cursor.vpos = -1;
21135 return make_number (PT);
21137 else if (!NILP (g->object) && !EQ (g->object, gpt->object))
21139 ptrdiff_t new_pos;
21141 if (BUFFERP (gpt->object))
21143 new_pos = PT;
21144 if ((gpt->resolved_level - row->reversed_p) % 2 == 0)
21145 new_pos += (row->reversed_p ? -dir : dir);
21146 else
21147 new_pos -= (row->reversed_p ? -dir : dir);
21149 else if (BUFFERP (g->object))
21150 new_pos = g->charpos;
21151 else
21152 break;
21153 SET_PT (new_pos);
21154 w->cursor.vpos = -1;
21155 return make_number (PT);
21157 else if (ROW_GLYPH_NEWLINE_P (row, g))
21159 /* Glyphs inserted at the end of a non-empty line for
21160 positioning the cursor have zero charpos, so we must
21161 deduce the value of point by other means. */
21162 if (g->charpos > 0)
21163 SET_PT (g->charpos);
21164 else if (row->ends_at_zv_p && PT != ZV)
21165 SET_PT (ZV);
21166 else if (PT != MATRIX_ROW_END_CHARPOS (row) - 1)
21167 SET_PT (MATRIX_ROW_END_CHARPOS (row) - 1);
21168 else
21169 break;
21170 w->cursor.vpos = -1;
21171 return make_number (PT);
21174 if (g == e || NILP (g->object))
21176 if (row->truncated_on_left_p || row->truncated_on_right_p)
21177 goto simulate_display;
21178 if (!row->reversed_p)
21179 row += dir;
21180 else
21181 row -= dir;
21182 if (row < MATRIX_FIRST_TEXT_ROW (w->current_matrix)
21183 || row > MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w))
21184 goto simulate_display;
21186 if (dir > 0)
21188 if (row->reversed_p && !row->continued_p)
21190 SET_PT (MATRIX_ROW_END_CHARPOS (row) - 1);
21191 w->cursor.vpos = -1;
21192 return make_number (PT);
21194 g = row->glyphs[TEXT_AREA];
21195 e = g + row->used[TEXT_AREA];
21196 for ( ; g < e; g++)
21198 if (BUFFERP (g->object)
21199 /* Empty lines have only one glyph, which stands
21200 for the newline, and whose charpos is the
21201 buffer position of the newline. */
21202 || ROW_GLYPH_NEWLINE_P (row, g)
21203 /* When the buffer ends in a newline, the line at
21204 EOB also has one glyph, but its charpos is -1. */
21205 || (row->ends_at_zv_p
21206 && !row->reversed_p
21207 && NILP (g->object)
21208 && g->type == CHAR_GLYPH
21209 && g->u.ch == ' '))
21211 if (g->charpos > 0)
21212 SET_PT (g->charpos);
21213 else if (!row->reversed_p
21214 && row->ends_at_zv_p
21215 && PT != ZV)
21216 SET_PT (ZV);
21217 else
21218 continue;
21219 w->cursor.vpos = -1;
21220 return make_number (PT);
21224 else
21226 if (!row->reversed_p && !row->continued_p)
21228 SET_PT (MATRIX_ROW_END_CHARPOS (row) - 1);
21229 w->cursor.vpos = -1;
21230 return make_number (PT);
21232 e = row->glyphs[TEXT_AREA];
21233 g = e + row->used[TEXT_AREA] - 1;
21234 for ( ; g >= e; g--)
21236 if (BUFFERP (g->object)
21237 || (ROW_GLYPH_NEWLINE_P (row, g)
21238 && g->charpos > 0)
21239 /* Empty R2L lines on GUI frames have the buffer
21240 position of the newline stored in the stretch
21241 glyph. */
21242 || g->type == STRETCH_GLYPH
21243 || (row->ends_at_zv_p
21244 && row->reversed_p
21245 && NILP (g->object)
21246 && g->type == CHAR_GLYPH
21247 && g->u.ch == ' '))
21249 if (g->charpos > 0)
21250 SET_PT (g->charpos);
21251 else if (row->reversed_p
21252 && row->ends_at_zv_p
21253 && PT != ZV)
21254 SET_PT (ZV);
21255 else
21256 continue;
21257 w->cursor.vpos = -1;
21258 return make_number (PT);
21265 simulate_display:
21267 /* If we wind up here, we failed to move by using the glyphs, so we
21268 need to simulate display instead. */
21270 if (b)
21271 paragraph_dir = Fcurrent_bidi_paragraph_direction (w->contents);
21272 else
21273 paragraph_dir = Qleft_to_right;
21274 if (EQ (paragraph_dir, Qright_to_left))
21275 dir = -dir;
21276 if (PT <= BEGV && dir < 0)
21277 xsignal0 (Qbeginning_of_buffer);
21278 else if (PT >= ZV && dir > 0)
21279 xsignal0 (Qend_of_buffer);
21280 else
21282 struct text_pos pt;
21283 struct it it;
21284 int pt_x, target_x, pixel_width, pt_vpos;
21285 bool at_eol_p;
21286 bool overshoot_expected = false;
21287 bool target_is_eol_p = false;
21289 /* Setup the arena. */
21290 SET_TEXT_POS (pt, PT, PT_BYTE);
21291 start_display (&it, w, pt);
21293 if (it.cmp_it.id < 0
21294 && it.method == GET_FROM_STRING
21295 && it.area == TEXT_AREA
21296 && it.string_from_display_prop_p
21297 && (it.sp > 0 && it.stack[it.sp - 1].method == GET_FROM_BUFFER))
21298 overshoot_expected = true;
21300 /* Find the X coordinate of point. We start from the beginning
21301 of this or previous line to make sure we are before point in
21302 the logical order (since the move_it_* functions can only
21303 move forward). */
21304 reseat:
21305 reseat_at_previous_visible_line_start (&it);
21306 it.current_x = it.hpos = it.current_y = it.vpos = 0;
21307 if (IT_CHARPOS (it) != PT)
21309 move_it_to (&it, overshoot_expected ? PT - 1 : PT,
21310 -1, -1, -1, MOVE_TO_POS);
21311 /* If we missed point because the character there is
21312 displayed out of a display vector that has more than one
21313 glyph, retry expecting overshoot. */
21314 if (it.method == GET_FROM_DISPLAY_VECTOR
21315 && it.current.dpvec_index > 0
21316 && !overshoot_expected)
21318 overshoot_expected = true;
21319 goto reseat;
21321 else if (IT_CHARPOS (it) != PT && !overshoot_expected)
21322 move_it_in_display_line (&it, PT, -1, MOVE_TO_POS);
21324 pt_x = it.current_x;
21325 pt_vpos = it.vpos;
21326 if (dir > 0 || overshoot_expected)
21328 struct glyph_row *row = it.glyph_row;
21330 /* When point is at beginning of line, we don't have
21331 information about the glyph there loaded into struct
21332 it. Calling get_next_display_element fixes that. */
21333 if (pt_x == 0)
21334 get_next_display_element (&it);
21335 at_eol_p = ITERATOR_AT_END_OF_LINE_P (&it);
21336 it.glyph_row = NULL;
21337 PRODUCE_GLYPHS (&it); /* compute it.pixel_width */
21338 it.glyph_row = row;
21339 /* PRODUCE_GLYPHS advances it.current_x, so we must restore
21340 it, lest it will become out of sync with it's buffer
21341 position. */
21342 it.current_x = pt_x;
21344 else
21345 at_eol_p = ITERATOR_AT_END_OF_LINE_P (&it);
21346 pixel_width = it.pixel_width;
21347 if (overshoot_expected && at_eol_p)
21348 pixel_width = 0;
21349 else if (pixel_width <= 0)
21350 pixel_width = 1;
21352 /* If there's a display string (or something similar) at point,
21353 we are actually at the glyph to the left of point, so we need
21354 to correct the X coordinate. */
21355 if (overshoot_expected)
21357 if (it.bidi_p)
21358 pt_x += pixel_width * it.bidi_it.scan_dir;
21359 else
21360 pt_x += pixel_width;
21363 /* Compute target X coordinate, either to the left or to the
21364 right of point. On TTY frames, all characters have the same
21365 pixel width of 1, so we can use that. On GUI frames we don't
21366 have an easy way of getting at the pixel width of the
21367 character to the left of point, so we use a different method
21368 of getting to that place. */
21369 if (dir > 0)
21370 target_x = pt_x + pixel_width;
21371 else
21372 target_x = pt_x - (!FRAME_WINDOW_P (it.f)) * pixel_width;
21374 /* Target X coordinate could be one line above or below the line
21375 of point, in which case we need to adjust the target X
21376 coordinate. Also, if moving to the left, we need to begin at
21377 the left edge of the point's screen line. */
21378 if (dir < 0)
21380 if (pt_x > 0)
21382 start_display (&it, w, pt);
21383 reseat_at_previous_visible_line_start (&it);
21384 it.current_x = it.current_y = it.hpos = 0;
21385 if (pt_vpos != 0)
21386 move_it_by_lines (&it, pt_vpos);
21388 else
21390 move_it_by_lines (&it, -1);
21391 target_x = it.last_visible_x - !FRAME_WINDOW_P (it.f);
21392 target_is_eol_p = true;
21393 /* Under word-wrap, we don't know the x coordinate of
21394 the last character displayed on the previous line,
21395 which immediately precedes the wrap point. To find
21396 out its x coordinate, we try moving to the right
21397 margin of the window, which will stop at the wrap
21398 point, and then reset target_x to point at the
21399 character that precedes the wrap point. This is not
21400 needed on GUI frames, because (see below) there we
21401 move from the left margin one grapheme cluster at a
21402 time, and stop when we hit the wrap point. */
21403 if (!FRAME_WINDOW_P (it.f) && it.line_wrap == WORD_WRAP)
21405 void *it_data = NULL;
21406 struct it it2;
21408 SAVE_IT (it2, it, it_data);
21409 move_it_in_display_line_to (&it, ZV, target_x,
21410 MOVE_TO_POS | MOVE_TO_X);
21411 /* If we arrived at target_x, that _is_ the last
21412 character on the previous line. */
21413 if (it.current_x != target_x)
21414 target_x = it.current_x - 1;
21415 RESTORE_IT (&it, &it2, it_data);
21419 else
21421 if (at_eol_p
21422 || (target_x >= it.last_visible_x
21423 && it.line_wrap != TRUNCATE))
21425 if (pt_x > 0)
21426 move_it_by_lines (&it, 0);
21427 move_it_by_lines (&it, 1);
21428 target_x = 0;
21432 /* Move to the target X coordinate. */
21433 #ifdef HAVE_WINDOW_SYSTEM
21434 /* On GUI frames, as we don't know the X coordinate of the
21435 character to the left of point, moving point to the left
21436 requires walking, one grapheme cluster at a time, until we
21437 find ourself at a place immediately to the left of the
21438 character at point. */
21439 if (FRAME_WINDOW_P (it.f) && dir < 0)
21441 struct text_pos new_pos;
21442 enum move_it_result rc = MOVE_X_REACHED;
21444 if (it.current_x == 0)
21445 get_next_display_element (&it);
21446 if (it.what == IT_COMPOSITION)
21448 new_pos.charpos = it.cmp_it.charpos;
21449 new_pos.bytepos = -1;
21451 else
21452 new_pos = it.current.pos;
21454 while (it.current_x + it.pixel_width <= target_x
21455 && (rc == MOVE_X_REACHED
21456 /* Under word-wrap, move_it_in_display_line_to
21457 stops at correct coordinates, but sometimes
21458 returns MOVE_POS_MATCH_OR_ZV. */
21459 || (it.line_wrap == WORD_WRAP
21460 && rc == MOVE_POS_MATCH_OR_ZV)))
21462 int new_x = it.current_x + it.pixel_width;
21464 /* For composed characters, we want the position of the
21465 first character in the grapheme cluster (usually, the
21466 composition's base character), whereas it.current
21467 might give us the position of the _last_ one, e.g. if
21468 the composition is rendered in reverse due to bidi
21469 reordering. */
21470 if (it.what == IT_COMPOSITION)
21472 new_pos.charpos = it.cmp_it.charpos;
21473 new_pos.bytepos = -1;
21475 else
21476 new_pos = it.current.pos;
21477 if (new_x == it.current_x)
21478 new_x++;
21479 rc = move_it_in_display_line_to (&it, ZV, new_x,
21480 MOVE_TO_POS | MOVE_TO_X);
21481 if (ITERATOR_AT_END_OF_LINE_P (&it) && !target_is_eol_p)
21482 break;
21484 /* The previous position we saw in the loop is the one we
21485 want. */
21486 if (new_pos.bytepos == -1)
21487 new_pos.bytepos = CHAR_TO_BYTE (new_pos.charpos);
21488 it.current.pos = new_pos;
21490 else
21491 #endif
21492 if (it.current_x != target_x)
21493 move_it_in_display_line_to (&it, ZV, target_x, MOVE_TO_POS | MOVE_TO_X);
21495 /* When lines are truncated, the above loop will stop at the
21496 window edge. But we want to get to the end of line, even if
21497 it is beyond the window edge; automatic hscroll will then
21498 scroll the window to show point as appropriate. */
21499 if (target_is_eol_p && it.line_wrap == TRUNCATE
21500 && get_next_display_element (&it))
21502 struct text_pos new_pos = it.current.pos;
21504 while (!ITERATOR_AT_END_OF_LINE_P (&it))
21506 set_iterator_to_next (&it, 0);
21507 if (it.method == GET_FROM_BUFFER)
21508 new_pos = it.current.pos;
21509 if (!get_next_display_element (&it))
21510 break;
21513 it.current.pos = new_pos;
21516 /* If we ended up in a display string that covers point, move to
21517 buffer position to the right in the visual order. */
21518 if (dir > 0)
21520 while (IT_CHARPOS (it) == PT)
21522 set_iterator_to_next (&it, 0);
21523 if (!get_next_display_element (&it))
21524 break;
21528 /* Move point to that position. */
21529 SET_PT_BOTH (IT_CHARPOS (it), IT_BYTEPOS (it));
21532 return make_number (PT);
21534 #undef ROW_GLYPH_NEWLINE_P
21537 DEFUN ("bidi-resolved-levels", Fbidi_resolved_levels,
21538 Sbidi_resolved_levels, 0, 1, 0,
21539 doc: /* Return the resolved bidirectional levels of characters at VPOS.
21541 The resolved levels are produced by the Emacs bidi reordering engine
21542 that implements the UBA, the Unicode Bidirectional Algorithm. Please
21543 read the Unicode Standard Annex 9 (UAX#9) for background information
21544 about these levels.
21546 VPOS is the zero-based number of the current window's screen line
21547 for which to produce the resolved levels. If VPOS is nil or omitted,
21548 it defaults to the screen line of point. If the window displays a
21549 header line, VPOS of zero will report on the header line, and first
21550 line of text in the window will have VPOS of 1.
21552 Value is an array of resolved levels, indexed by glyph number.
21553 Glyphs are numbered from zero starting from the beginning of the
21554 screen line, i.e. the left edge of the window for left-to-right lines
21555 and from the right edge for right-to-left lines. The resolved levels
21556 are produced only for the window's text area; text in display margins
21557 is not included.
21559 If the selected window's display is not up-to-date, or if the specified
21560 screen line does not display text, this function returns nil. It is
21561 highly recommended to bind this function to some simple key, like F8,
21562 in order to avoid these problems.
21564 This function exists mainly for testing the correctness of the
21565 Emacs UBA implementation, in particular with the test suite. */)
21566 (Lisp_Object vpos)
21568 struct window *w = XWINDOW (selected_window);
21569 struct buffer *b = XBUFFER (w->contents);
21570 int nrow;
21571 struct glyph_row *row;
21573 if (NILP (vpos))
21575 int d1, d2, d3, d4, d5;
21577 pos_visible_p (w, PT, &d1, &d2, &d3, &d4, &d5, &nrow);
21579 else
21581 CHECK_NUMBER_COERCE_MARKER (vpos);
21582 nrow = XINT (vpos);
21585 /* We require up-to-date glyph matrix for this window. */
21586 if (w->window_end_valid
21587 && !windows_or_buffers_changed
21588 && b
21589 && !b->clip_changed
21590 && !b->prevent_redisplay_optimizations_p
21591 && !window_outdated (w)
21592 && nrow >= 0
21593 && nrow < w->current_matrix->nrows
21594 && (row = MATRIX_ROW (w->current_matrix, nrow))->enabled_p
21595 && MATRIX_ROW_DISPLAYS_TEXT_P (row))
21597 struct glyph *g, *e, *g1;
21598 int nglyphs, i;
21599 Lisp_Object levels;
21601 if (!row->reversed_p) /* Left-to-right glyph row. */
21603 g = g1 = row->glyphs[TEXT_AREA];
21604 e = g + row->used[TEXT_AREA];
21606 /* Skip over glyphs at the start of the row that was
21607 generated by redisplay for its own needs. */
21608 while (g < e
21609 && NILP (g->object)
21610 && g->charpos < 0)
21611 g++;
21612 g1 = g;
21614 /* Count the "interesting" glyphs in this row. */
21615 for (nglyphs = 0; g < e && !NILP (g->object); g++)
21616 nglyphs++;
21618 /* Create and fill the array. */
21619 levels = make_uninit_vector (nglyphs);
21620 for (i = 0; g1 < g; i++, g1++)
21621 ASET (levels, i, make_number (g1->resolved_level));
21623 else /* Right-to-left glyph row. */
21625 g = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
21626 e = row->glyphs[TEXT_AREA] - 1;
21627 while (g > e
21628 && NILP (g->object)
21629 && g->charpos < 0)
21630 g--;
21631 g1 = g;
21632 for (nglyphs = 0; g > e && !NILP (g->object); g--)
21633 nglyphs++;
21634 levels = make_uninit_vector (nglyphs);
21635 for (i = 0; g1 > g; i++, g1--)
21636 ASET (levels, i, make_number (g1->resolved_level));
21638 return levels;
21640 else
21641 return Qnil;
21646 /***********************************************************************
21647 Menu Bar
21648 ***********************************************************************/
21650 /* Redisplay the menu bar in the frame for window W.
21652 The menu bar of X frames that don't have X toolkit support is
21653 displayed in a special window W->frame->menu_bar_window.
21655 The menu bar of terminal frames is treated specially as far as
21656 glyph matrices are concerned. Menu bar lines are not part of
21657 windows, so the update is done directly on the frame matrix rows
21658 for the menu bar. */
21660 static void
21661 display_menu_bar (struct window *w)
21663 struct frame *f = XFRAME (WINDOW_FRAME (w));
21664 struct it it;
21665 Lisp_Object items;
21666 int i;
21668 /* Don't do all this for graphical frames. */
21669 #ifdef HAVE_NTGUI
21670 if (FRAME_W32_P (f))
21671 return;
21672 #endif
21673 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
21674 if (FRAME_X_P (f))
21675 return;
21676 #endif
21678 #ifdef HAVE_NS
21679 if (FRAME_NS_P (f))
21680 return;
21681 #endif /* HAVE_NS */
21683 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
21684 eassert (!FRAME_WINDOW_P (f));
21685 init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MENU_FACE_ID);
21686 it.first_visible_x = 0;
21687 it.last_visible_x = FRAME_PIXEL_WIDTH (f);
21688 #elif defined (HAVE_X_WINDOWS) /* X without toolkit. */
21689 if (FRAME_WINDOW_P (f))
21691 /* Menu bar lines are displayed in the desired matrix of the
21692 dummy window menu_bar_window. */
21693 struct window *menu_w;
21694 menu_w = XWINDOW (f->menu_bar_window);
21695 init_iterator (&it, menu_w, -1, -1, menu_w->desired_matrix->rows,
21696 MENU_FACE_ID);
21697 it.first_visible_x = 0;
21698 it.last_visible_x = FRAME_PIXEL_WIDTH (f);
21700 else
21701 #endif /* not USE_X_TOOLKIT and not USE_GTK */
21703 /* This is a TTY frame, i.e. character hpos/vpos are used as
21704 pixel x/y. */
21705 init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
21706 MENU_FACE_ID);
21707 it.first_visible_x = 0;
21708 it.last_visible_x = FRAME_COLS (f);
21711 /* FIXME: This should be controlled by a user option. See the
21712 comments in redisplay_tool_bar and display_mode_line about
21713 this. */
21714 it.paragraph_embedding = L2R;
21716 /* Clear all rows of the menu bar. */
21717 for (i = 0; i < FRAME_MENU_BAR_LINES (f); ++i)
21719 struct glyph_row *row = it.glyph_row + i;
21720 clear_glyph_row (row);
21721 row->enabled_p = true;
21722 row->full_width_p = 1;
21723 row->reversed_p = false;
21726 /* Display all items of the menu bar. */
21727 items = FRAME_MENU_BAR_ITEMS (it.f);
21728 for (i = 0; i < ASIZE (items); i += 4)
21730 Lisp_Object string;
21732 /* Stop at nil string. */
21733 string = AREF (items, i + 1);
21734 if (NILP (string))
21735 break;
21737 /* Remember where item was displayed. */
21738 ASET (items, i + 3, make_number (it.hpos));
21740 /* Display the item, pad with one space. */
21741 if (it.current_x < it.last_visible_x)
21742 display_string (NULL, string, Qnil, 0, 0, &it,
21743 SCHARS (string) + 1, 0, 0, -1);
21746 /* Fill out the line with spaces. */
21747 if (it.current_x < it.last_visible_x)
21748 display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
21750 /* Compute the total height of the lines. */
21751 compute_line_metrics (&it);
21754 /* Deep copy of a glyph row, including the glyphs. */
21755 static void
21756 deep_copy_glyph_row (struct glyph_row *to, struct glyph_row *from)
21758 struct glyph *pointers[1 + LAST_AREA];
21759 int to_used = to->used[TEXT_AREA];
21761 /* Save glyph pointers of TO. */
21762 memcpy (pointers, to->glyphs, sizeof to->glyphs);
21764 /* Do a structure assignment. */
21765 *to = *from;
21767 /* Restore original glyph pointers of TO. */
21768 memcpy (to->glyphs, pointers, sizeof to->glyphs);
21770 /* Copy the glyphs. */
21771 memcpy (to->glyphs[TEXT_AREA], from->glyphs[TEXT_AREA],
21772 min (from->used[TEXT_AREA], to_used) * sizeof (struct glyph));
21774 /* If we filled only part of the TO row, fill the rest with
21775 space_glyph (which will display as empty space). */
21776 if (to_used > from->used[TEXT_AREA])
21777 fill_up_frame_row_with_spaces (to, to_used);
21780 /* Display one menu item on a TTY, by overwriting the glyphs in the
21781 frame F's desired glyph matrix with glyphs produced from the menu
21782 item text. Called from term.c to display TTY drop-down menus one
21783 item at a time.
21785 ITEM_TEXT is the menu item text as a C string.
21787 FACE_ID is the face ID to be used for this menu item. FACE_ID
21788 could specify one of 3 faces: a face for an enabled item, a face
21789 for a disabled item, or a face for a selected item.
21791 X and Y are coordinates of the first glyph in the frame's desired
21792 matrix to be overwritten by the menu item. Since this is a TTY, Y
21793 is the zero-based number of the glyph row and X is the zero-based
21794 glyph number in the row, starting from left, where to start
21795 displaying the item.
21797 SUBMENU non-zero means this menu item drops down a submenu, which
21798 should be indicated by displaying a proper visual cue after the
21799 item text. */
21801 void
21802 display_tty_menu_item (const char *item_text, int width, int face_id,
21803 int x, int y, int submenu)
21805 struct it it;
21806 struct frame *f = SELECTED_FRAME ();
21807 struct window *w = XWINDOW (f->selected_window);
21808 int saved_used, saved_truncated, saved_width, saved_reversed;
21809 struct glyph_row *row;
21810 size_t item_len = strlen (item_text);
21812 eassert (FRAME_TERMCAP_P (f));
21814 /* Don't write beyond the matrix's last row. This can happen for
21815 TTY screens that are not high enough to show the entire menu.
21816 (This is actually a bit of defensive programming, as
21817 tty_menu_display already limits the number of menu items to one
21818 less than the number of screen lines.) */
21819 if (y >= f->desired_matrix->nrows)
21820 return;
21822 init_iterator (&it, w, -1, -1, f->desired_matrix->rows + y, MENU_FACE_ID);
21823 it.first_visible_x = 0;
21824 it.last_visible_x = FRAME_COLS (f) - 1;
21825 row = it.glyph_row;
21826 /* Start with the row contents from the current matrix. */
21827 deep_copy_glyph_row (row, f->current_matrix->rows + y);
21828 saved_width = row->full_width_p;
21829 row->full_width_p = 1;
21830 saved_reversed = row->reversed_p;
21831 row->reversed_p = 0;
21832 row->enabled_p = true;
21834 /* Arrange for the menu item glyphs to start at (X,Y) and have the
21835 desired face. */
21836 eassert (x < f->desired_matrix->matrix_w);
21837 it.current_x = it.hpos = x;
21838 it.current_y = it.vpos = y;
21839 saved_used = row->used[TEXT_AREA];
21840 saved_truncated = row->truncated_on_right_p;
21841 row->used[TEXT_AREA] = x;
21842 it.face_id = face_id;
21843 it.line_wrap = TRUNCATE;
21845 /* FIXME: This should be controlled by a user option. See the
21846 comments in redisplay_tool_bar and display_mode_line about this.
21847 Also, if paragraph_embedding could ever be R2L, changes will be
21848 needed to avoid shifting to the right the row characters in
21849 term.c:append_glyph. */
21850 it.paragraph_embedding = L2R;
21852 /* Pad with a space on the left. */
21853 display_string (" ", Qnil, Qnil, 0, 0, &it, 1, 0, FRAME_COLS (f) - 1, -1);
21854 width--;
21855 /* Display the menu item, pad with spaces to WIDTH. */
21856 if (submenu)
21858 display_string (item_text, Qnil, Qnil, 0, 0, &it,
21859 item_len, 0, FRAME_COLS (f) - 1, -1);
21860 width -= item_len;
21861 /* Indicate with " >" that there's a submenu. */
21862 display_string (" >", Qnil, Qnil, 0, 0, &it, width, 0,
21863 FRAME_COLS (f) - 1, -1);
21865 else
21866 display_string (item_text, Qnil, Qnil, 0, 0, &it,
21867 width, 0, FRAME_COLS (f) - 1, -1);
21869 row->used[TEXT_AREA] = max (saved_used, row->used[TEXT_AREA]);
21870 row->truncated_on_right_p = saved_truncated;
21871 row->hash = row_hash (row);
21872 row->full_width_p = saved_width;
21873 row->reversed_p = saved_reversed;
21876 /***********************************************************************
21877 Mode Line
21878 ***********************************************************************/
21880 /* Redisplay mode lines in the window tree whose root is WINDOW. If
21881 FORCE is non-zero, redisplay mode lines unconditionally.
21882 Otherwise, redisplay only mode lines that are garbaged. Value is
21883 the number of windows whose mode lines were redisplayed. */
21885 static int
21886 redisplay_mode_lines (Lisp_Object window, bool force)
21888 int nwindows = 0;
21890 while (!NILP (window))
21892 struct window *w = XWINDOW (window);
21894 if (WINDOWP (w->contents))
21895 nwindows += redisplay_mode_lines (w->contents, force);
21896 else if (force
21897 || FRAME_GARBAGED_P (XFRAME (w->frame))
21898 || !MATRIX_MODE_LINE_ROW (w->current_matrix)->enabled_p)
21900 struct text_pos lpoint;
21901 struct buffer *old = current_buffer;
21903 /* Set the window's buffer for the mode line display. */
21904 SET_TEXT_POS (lpoint, PT, PT_BYTE);
21905 set_buffer_internal_1 (XBUFFER (w->contents));
21907 /* Point refers normally to the selected window. For any
21908 other window, set up appropriate value. */
21909 if (!EQ (window, selected_window))
21911 struct text_pos pt;
21913 CLIP_TEXT_POS_FROM_MARKER (pt, w->pointm);
21914 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
21917 /* Display mode lines. */
21918 clear_glyph_matrix (w->desired_matrix);
21919 if (display_mode_lines (w))
21920 ++nwindows;
21922 /* Restore old settings. */
21923 set_buffer_internal_1 (old);
21924 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
21927 window = w->next;
21930 return nwindows;
21934 /* Display the mode and/or header line of window W. Value is the
21935 sum number of mode lines and header lines displayed. */
21937 static int
21938 display_mode_lines (struct window *w)
21940 Lisp_Object old_selected_window = selected_window;
21941 Lisp_Object old_selected_frame = selected_frame;
21942 Lisp_Object new_frame = w->frame;
21943 Lisp_Object old_frame_selected_window = XFRAME (new_frame)->selected_window;
21944 int n = 0;
21946 selected_frame = new_frame;
21947 /* FIXME: If we were to allow the mode-line's computation changing the buffer
21948 or window's point, then we'd need select_window_1 here as well. */
21949 XSETWINDOW (selected_window, w);
21950 XFRAME (new_frame)->selected_window = selected_window;
21952 /* These will be set while the mode line specs are processed. */
21953 line_number_displayed = 0;
21954 w->column_number_displayed = -1;
21956 if (WINDOW_WANTS_MODELINE_P (w))
21958 struct window *sel_w = XWINDOW (old_selected_window);
21960 /* Select mode line face based on the real selected window. */
21961 display_mode_line (w, CURRENT_MODE_LINE_FACE_ID_3 (sel_w, sel_w, w),
21962 BVAR (current_buffer, mode_line_format));
21963 ++n;
21966 if (WINDOW_WANTS_HEADER_LINE_P (w))
21968 display_mode_line (w, HEADER_LINE_FACE_ID,
21969 BVAR (current_buffer, header_line_format));
21970 ++n;
21973 XFRAME (new_frame)->selected_window = old_frame_selected_window;
21974 selected_frame = old_selected_frame;
21975 selected_window = old_selected_window;
21976 if (n > 0)
21977 w->must_be_updated_p = true;
21978 return n;
21982 /* Display mode or header line of window W. FACE_ID specifies which
21983 line to display; it is either MODE_LINE_FACE_ID or
21984 HEADER_LINE_FACE_ID. FORMAT is the mode/header line format to
21985 display. Value is the pixel height of the mode/header line
21986 displayed. */
21988 static int
21989 display_mode_line (struct window *w, enum face_id face_id, Lisp_Object format)
21991 struct it it;
21992 struct face *face;
21993 ptrdiff_t count = SPECPDL_INDEX ();
21995 init_iterator (&it, w, -1, -1, NULL, face_id);
21996 /* Don't extend on a previously drawn mode-line.
21997 This may happen if called from pos_visible_p. */
21998 it.glyph_row->enabled_p = false;
21999 prepare_desired_row (w, it.glyph_row, true);
22001 it.glyph_row->mode_line_p = 1;
22003 /* FIXME: This should be controlled by a user option. But
22004 supporting such an option is not trivial, since the mode line is
22005 made up of many separate strings. */
22006 it.paragraph_embedding = L2R;
22008 record_unwind_protect (unwind_format_mode_line,
22009 format_mode_line_unwind_data (NULL, NULL, Qnil, 0));
22011 mode_line_target = MODE_LINE_DISPLAY;
22013 /* Temporarily make frame's keyboard the current kboard so that
22014 kboard-local variables in the mode_line_format will get the right
22015 values. */
22016 push_kboard (FRAME_KBOARD (it.f));
22017 record_unwind_save_match_data ();
22018 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
22019 pop_kboard ();
22021 unbind_to (count, Qnil);
22023 /* Fill up with spaces. */
22024 display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);
22026 compute_line_metrics (&it);
22027 it.glyph_row->full_width_p = 1;
22028 it.glyph_row->continued_p = 0;
22029 it.glyph_row->truncated_on_left_p = 0;
22030 it.glyph_row->truncated_on_right_p = 0;
22032 /* Make a 3D mode-line have a shadow at its right end. */
22033 face = FACE_FROM_ID (it.f, face_id);
22034 extend_face_to_end_of_line (&it);
22035 if (face->box != FACE_NO_BOX)
22037 struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
22038 + it.glyph_row->used[TEXT_AREA] - 1);
22039 last->right_box_line_p = 1;
22042 return it.glyph_row->height;
22045 /* Move element ELT in LIST to the front of LIST.
22046 Return the updated list. */
22048 static Lisp_Object
22049 move_elt_to_front (Lisp_Object elt, Lisp_Object list)
22051 register Lisp_Object tail, prev;
22052 register Lisp_Object tem;
22054 tail = list;
22055 prev = Qnil;
22056 while (CONSP (tail))
22058 tem = XCAR (tail);
22060 if (EQ (elt, tem))
22062 /* Splice out the link TAIL. */
22063 if (NILP (prev))
22064 list = XCDR (tail);
22065 else
22066 Fsetcdr (prev, XCDR (tail));
22068 /* Now make it the first. */
22069 Fsetcdr (tail, list);
22070 return tail;
22072 else
22073 prev = tail;
22074 tail = XCDR (tail);
22075 QUIT;
22078 /* Not found--return unchanged LIST. */
22079 return list;
22082 /* Contribute ELT to the mode line for window IT->w. How it
22083 translates into text depends on its data type.
22085 IT describes the display environment in which we display, as usual.
22087 DEPTH is the depth in recursion. It is used to prevent
22088 infinite recursion here.
22090 FIELD_WIDTH is the number of characters the display of ELT should
22091 occupy in the mode line, and PRECISION is the maximum number of
22092 characters to display from ELT's representation. See
22093 display_string for details.
22095 Returns the hpos of the end of the text generated by ELT.
22097 PROPS is a property list to add to any string we encounter.
22099 If RISKY is nonzero, remove (disregard) any properties in any string
22100 we encounter, and ignore :eval and :propertize.
22102 The global variable `mode_line_target' determines whether the
22103 output is passed to `store_mode_line_noprop',
22104 `store_mode_line_string', or `display_string'. */
22106 static int
22107 display_mode_element (struct it *it, int depth, int field_width, int precision,
22108 Lisp_Object elt, Lisp_Object props, int risky)
22110 int n = 0, field, prec;
22111 int literal = 0;
22113 tail_recurse:
22114 if (depth > 100)
22115 elt = build_string ("*too-deep*");
22117 depth++;
22119 switch (XTYPE (elt))
22121 case Lisp_String:
22123 /* A string: output it and check for %-constructs within it. */
22124 unsigned char c;
22125 ptrdiff_t offset = 0;
22127 if (SCHARS (elt) > 0
22128 && (!NILP (props) || risky))
22130 Lisp_Object oprops, aelt;
22131 oprops = Ftext_properties_at (make_number (0), elt);
22133 /* If the starting string's properties are not what
22134 we want, translate the string. Also, if the string
22135 is risky, do that anyway. */
22137 if (NILP (Fequal (props, oprops)) || risky)
22139 /* If the starting string has properties,
22140 merge the specified ones onto the existing ones. */
22141 if (! NILP (oprops) && !risky)
22143 Lisp_Object tem;
22145 oprops = Fcopy_sequence (oprops);
22146 tem = props;
22147 while (CONSP (tem))
22149 oprops = Fplist_put (oprops, XCAR (tem),
22150 XCAR (XCDR (tem)));
22151 tem = XCDR (XCDR (tem));
22153 props = oprops;
22156 aelt = Fassoc (elt, mode_line_proptrans_alist);
22157 if (! NILP (aelt) && !NILP (Fequal (props, XCDR (aelt))))
22159 /* AELT is what we want. Move it to the front
22160 without consing. */
22161 elt = XCAR (aelt);
22162 mode_line_proptrans_alist
22163 = move_elt_to_front (aelt, mode_line_proptrans_alist);
22165 else
22167 Lisp_Object tem;
22169 /* If AELT has the wrong props, it is useless.
22170 so get rid of it. */
22171 if (! NILP (aelt))
22172 mode_line_proptrans_alist
22173 = Fdelq (aelt, mode_line_proptrans_alist);
22175 elt = Fcopy_sequence (elt);
22176 Fset_text_properties (make_number (0), Flength (elt),
22177 props, elt);
22178 /* Add this item to mode_line_proptrans_alist. */
22179 mode_line_proptrans_alist
22180 = Fcons (Fcons (elt, props),
22181 mode_line_proptrans_alist);
22182 /* Truncate mode_line_proptrans_alist
22183 to at most 50 elements. */
22184 tem = Fnthcdr (make_number (50),
22185 mode_line_proptrans_alist);
22186 if (! NILP (tem))
22187 XSETCDR (tem, Qnil);
22192 offset = 0;
22194 if (literal)
22196 prec = precision - n;
22197 switch (mode_line_target)
22199 case MODE_LINE_NOPROP:
22200 case MODE_LINE_TITLE:
22201 n += store_mode_line_noprop (SSDATA (elt), -1, prec);
22202 break;
22203 case MODE_LINE_STRING:
22204 n += store_mode_line_string (NULL, elt, 1, 0, prec, Qnil);
22205 break;
22206 case MODE_LINE_DISPLAY:
22207 n += display_string (NULL, elt, Qnil, 0, 0, it,
22208 0, prec, 0, STRING_MULTIBYTE (elt));
22209 break;
22212 break;
22215 /* Handle the non-literal case. */
22217 while ((precision <= 0 || n < precision)
22218 && SREF (elt, offset) != 0
22219 && (mode_line_target != MODE_LINE_DISPLAY
22220 || it->current_x < it->last_visible_x))
22222 ptrdiff_t last_offset = offset;
22224 /* Advance to end of string or next format specifier. */
22225 while ((c = SREF (elt, offset++)) != '\0' && c != '%')
22228 if (offset - 1 != last_offset)
22230 ptrdiff_t nchars, nbytes;
22232 /* Output to end of string or up to '%'. Field width
22233 is length of string. Don't output more than
22234 PRECISION allows us. */
22235 offset--;
22237 prec = c_string_width (SDATA (elt) + last_offset,
22238 offset - last_offset, precision - n,
22239 &nchars, &nbytes);
22241 switch (mode_line_target)
22243 case MODE_LINE_NOPROP:
22244 case MODE_LINE_TITLE:
22245 n += store_mode_line_noprop (SSDATA (elt) + last_offset, 0, prec);
22246 break;
22247 case MODE_LINE_STRING:
22249 ptrdiff_t bytepos = last_offset;
22250 ptrdiff_t charpos = string_byte_to_char (elt, bytepos);
22251 ptrdiff_t endpos = (precision <= 0
22252 ? string_byte_to_char (elt, offset)
22253 : charpos + nchars);
22255 n += store_mode_line_string (NULL,
22256 Fsubstring (elt, make_number (charpos),
22257 make_number (endpos)),
22258 0, 0, 0, Qnil);
22260 break;
22261 case MODE_LINE_DISPLAY:
22263 ptrdiff_t bytepos = last_offset;
22264 ptrdiff_t charpos = string_byte_to_char (elt, bytepos);
22266 if (precision <= 0)
22267 nchars = string_byte_to_char (elt, offset) - charpos;
22268 n += display_string (NULL, elt, Qnil, 0, charpos,
22269 it, 0, nchars, 0,
22270 STRING_MULTIBYTE (elt));
22272 break;
22275 else /* c == '%' */
22277 ptrdiff_t percent_position = offset;
22279 /* Get the specified minimum width. Zero means
22280 don't pad. */
22281 field = 0;
22282 while ((c = SREF (elt, offset++)) >= '0' && c <= '9')
22283 field = field * 10 + c - '0';
22285 /* Don't pad beyond the total padding allowed. */
22286 if (field_width - n > 0 && field > field_width - n)
22287 field = field_width - n;
22289 /* Note that either PRECISION <= 0 or N < PRECISION. */
22290 prec = precision - n;
22292 if (c == 'M')
22293 n += display_mode_element (it, depth, field, prec,
22294 Vglobal_mode_string, props,
22295 risky);
22296 else if (c != 0)
22298 bool multibyte;
22299 ptrdiff_t bytepos, charpos;
22300 const char *spec;
22301 Lisp_Object string;
22303 bytepos = percent_position;
22304 charpos = (STRING_MULTIBYTE (elt)
22305 ? string_byte_to_char (elt, bytepos)
22306 : bytepos);
22307 spec = decode_mode_spec (it->w, c, field, &string);
22308 multibyte = STRINGP (string) && STRING_MULTIBYTE (string);
22310 switch (mode_line_target)
22312 case MODE_LINE_NOPROP:
22313 case MODE_LINE_TITLE:
22314 n += store_mode_line_noprop (spec, field, prec);
22315 break;
22316 case MODE_LINE_STRING:
22318 Lisp_Object tem = build_string (spec);
22319 props = Ftext_properties_at (make_number (charpos), elt);
22320 /* Should only keep face property in props */
22321 n += store_mode_line_string (NULL, tem, 0, field, prec, props);
22323 break;
22324 case MODE_LINE_DISPLAY:
22326 int nglyphs_before, nwritten;
22328 nglyphs_before = it->glyph_row->used[TEXT_AREA];
22329 nwritten = display_string (spec, string, elt,
22330 charpos, 0, it,
22331 field, prec, 0,
22332 multibyte);
22334 /* Assign to the glyphs written above the
22335 string where the `%x' came from, position
22336 of the `%'. */
22337 if (nwritten > 0)
22339 struct glyph *glyph
22340 = (it->glyph_row->glyphs[TEXT_AREA]
22341 + nglyphs_before);
22342 int i;
22344 for (i = 0; i < nwritten; ++i)
22346 glyph[i].object = elt;
22347 glyph[i].charpos = charpos;
22350 n += nwritten;
22353 break;
22356 else /* c == 0 */
22357 break;
22361 break;
22363 case Lisp_Symbol:
22364 /* A symbol: process the value of the symbol recursively
22365 as if it appeared here directly. Avoid error if symbol void.
22366 Special case: if value of symbol is a string, output the string
22367 literally. */
22369 register Lisp_Object tem;
22371 /* If the variable is not marked as risky to set
22372 then its contents are risky to use. */
22373 if (NILP (Fget (elt, Qrisky_local_variable)))
22374 risky = 1;
22376 tem = Fboundp (elt);
22377 if (!NILP (tem))
22379 tem = Fsymbol_value (elt);
22380 /* If value is a string, output that string literally:
22381 don't check for % within it. */
22382 if (STRINGP (tem))
22383 literal = 1;
22385 if (!EQ (tem, elt))
22387 /* Give up right away for nil or t. */
22388 elt = tem;
22389 goto tail_recurse;
22393 break;
22395 case Lisp_Cons:
22397 register Lisp_Object car, tem;
22399 /* A cons cell: five distinct cases.
22400 If first element is :eval or :propertize, do something special.
22401 If first element is a string or a cons, process all the elements
22402 and effectively concatenate them.
22403 If first element is a negative number, truncate displaying cdr to
22404 at most that many characters. If positive, pad (with spaces)
22405 to at least that many characters.
22406 If first element is a symbol, process the cadr or caddr recursively
22407 according to whether the symbol's value is non-nil or nil. */
22408 car = XCAR (elt);
22409 if (EQ (car, QCeval))
22411 /* An element of the form (:eval FORM) means evaluate FORM
22412 and use the result as mode line elements. */
22414 if (risky)
22415 break;
22417 if (CONSP (XCDR (elt)))
22419 Lisp_Object spec;
22420 spec = safe__eval (true, XCAR (XCDR (elt)));
22421 n += display_mode_element (it, depth, field_width - n,
22422 precision - n, spec, props,
22423 risky);
22426 else if (EQ (car, QCpropertize))
22428 /* An element of the form (:propertize ELT PROPS...)
22429 means display ELT but applying properties PROPS. */
22431 if (risky)
22432 break;
22434 if (CONSP (XCDR (elt)))
22435 n += display_mode_element (it, depth, field_width - n,
22436 precision - n, XCAR (XCDR (elt)),
22437 XCDR (XCDR (elt)), risky);
22439 else if (SYMBOLP (car))
22441 tem = Fboundp (car);
22442 elt = XCDR (elt);
22443 if (!CONSP (elt))
22444 goto invalid;
22445 /* elt is now the cdr, and we know it is a cons cell.
22446 Use its car if CAR has a non-nil value. */
22447 if (!NILP (tem))
22449 tem = Fsymbol_value (car);
22450 if (!NILP (tem))
22452 elt = XCAR (elt);
22453 goto tail_recurse;
22456 /* Symbol's value is nil (or symbol is unbound)
22457 Get the cddr of the original list
22458 and if possible find the caddr and use that. */
22459 elt = XCDR (elt);
22460 if (NILP (elt))
22461 break;
22462 else if (!CONSP (elt))
22463 goto invalid;
22464 elt = XCAR (elt);
22465 goto tail_recurse;
22467 else if (INTEGERP (car))
22469 register int lim = XINT (car);
22470 elt = XCDR (elt);
22471 if (lim < 0)
22473 /* Negative int means reduce maximum width. */
22474 if (precision <= 0)
22475 precision = -lim;
22476 else
22477 precision = min (precision, -lim);
22479 else if (lim > 0)
22481 /* Padding specified. Don't let it be more than
22482 current maximum. */
22483 if (precision > 0)
22484 lim = min (precision, lim);
22486 /* If that's more padding than already wanted, queue it.
22487 But don't reduce padding already specified even if
22488 that is beyond the current truncation point. */
22489 field_width = max (lim, field_width);
22491 goto tail_recurse;
22493 else if (STRINGP (car) || CONSP (car))
22495 Lisp_Object halftail = elt;
22496 int len = 0;
22498 while (CONSP (elt)
22499 && (precision <= 0 || n < precision))
22501 n += display_mode_element (it, depth,
22502 /* Do padding only after the last
22503 element in the list. */
22504 (! CONSP (XCDR (elt))
22505 ? field_width - n
22506 : 0),
22507 precision - n, XCAR (elt),
22508 props, risky);
22509 elt = XCDR (elt);
22510 len++;
22511 if ((len & 1) == 0)
22512 halftail = XCDR (halftail);
22513 /* Check for cycle. */
22514 if (EQ (halftail, elt))
22515 break;
22519 break;
22521 default:
22522 invalid:
22523 elt = build_string ("*invalid*");
22524 goto tail_recurse;
22527 /* Pad to FIELD_WIDTH. */
22528 if (field_width > 0 && n < field_width)
22530 switch (mode_line_target)
22532 case MODE_LINE_NOPROP:
22533 case MODE_LINE_TITLE:
22534 n += store_mode_line_noprop ("", field_width - n, 0);
22535 break;
22536 case MODE_LINE_STRING:
22537 n += store_mode_line_string ("", Qnil, 0, field_width - n, 0, Qnil);
22538 break;
22539 case MODE_LINE_DISPLAY:
22540 n += display_string ("", Qnil, Qnil, 0, 0, it, field_width - n,
22541 0, 0, 0);
22542 break;
22546 return n;
22549 /* Store a mode-line string element in mode_line_string_list.
22551 If STRING is non-null, display that C string. Otherwise, the Lisp
22552 string LISP_STRING is displayed.
22554 FIELD_WIDTH is the minimum number of output glyphs to produce.
22555 If STRING has fewer characters than FIELD_WIDTH, pad to the right
22556 with spaces. FIELD_WIDTH <= 0 means don't pad.
22558 PRECISION is the maximum number of characters to output from
22559 STRING. PRECISION <= 0 means don't truncate the string.
22561 If COPY_STRING is non-zero, make a copy of LISP_STRING before adding
22562 properties to the string.
22564 PROPS are the properties to add to the string.
22565 The mode_line_string_face face property is always added to the string.
22568 static int
22569 store_mode_line_string (const char *string, Lisp_Object lisp_string, int copy_string,
22570 int field_width, int precision, Lisp_Object props)
22572 ptrdiff_t len;
22573 int n = 0;
22575 if (string != NULL)
22577 len = strlen (string);
22578 if (precision > 0 && len > precision)
22579 len = precision;
22580 lisp_string = make_string (string, len);
22581 if (NILP (props))
22582 props = mode_line_string_face_prop;
22583 else if (!NILP (mode_line_string_face))
22585 Lisp_Object face = Fplist_get (props, Qface);
22586 props = Fcopy_sequence (props);
22587 if (NILP (face))
22588 face = mode_line_string_face;
22589 else
22590 face = list2 (face, mode_line_string_face);
22591 props = Fplist_put (props, Qface, face);
22593 Fadd_text_properties (make_number (0), make_number (len),
22594 props, lisp_string);
22596 else
22598 len = XFASTINT (Flength (lisp_string));
22599 if (precision > 0 && len > precision)
22601 len = precision;
22602 lisp_string = Fsubstring (lisp_string, make_number (0), make_number (len));
22603 precision = -1;
22605 if (!NILP (mode_line_string_face))
22607 Lisp_Object face;
22608 if (NILP (props))
22609 props = Ftext_properties_at (make_number (0), lisp_string);
22610 face = Fplist_get (props, Qface);
22611 if (NILP (face))
22612 face = mode_line_string_face;
22613 else
22614 face = list2 (face, mode_line_string_face);
22615 props = list2 (Qface, face);
22616 if (copy_string)
22617 lisp_string = Fcopy_sequence (lisp_string);
22619 if (!NILP (props))
22620 Fadd_text_properties (make_number (0), make_number (len),
22621 props, lisp_string);
22624 if (len > 0)
22626 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
22627 n += len;
22630 if (field_width > len)
22632 field_width -= len;
22633 lisp_string = Fmake_string (make_number (field_width), make_number (' '));
22634 if (!NILP (props))
22635 Fadd_text_properties (make_number (0), make_number (field_width),
22636 props, lisp_string);
22637 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
22638 n += field_width;
22641 return n;
22645 DEFUN ("format-mode-line", Fformat_mode_line, Sformat_mode_line,
22646 1, 4, 0,
22647 doc: /* Format a string out of a mode line format specification.
22648 First arg FORMAT specifies the mode line format (see `mode-line-format'
22649 for details) to use.
22651 By default, the format is evaluated for the currently selected window.
22653 Optional second arg FACE specifies the face property to put on all
22654 characters for which no face is specified. The value nil means the
22655 default face. The value t means whatever face the window's mode line
22656 currently uses (either `mode-line' or `mode-line-inactive',
22657 depending on whether the window is the selected window or not).
22658 An integer value means the value string has no text
22659 properties.
22661 Optional third and fourth args WINDOW and BUFFER specify the window
22662 and buffer to use as the context for the formatting (defaults
22663 are the selected window and the WINDOW's buffer). */)
22664 (Lisp_Object format, Lisp_Object face,
22665 Lisp_Object window, Lisp_Object buffer)
22667 struct it it;
22668 int len;
22669 struct window *w;
22670 struct buffer *old_buffer = NULL;
22671 int face_id;
22672 int no_props = INTEGERP (face);
22673 ptrdiff_t count = SPECPDL_INDEX ();
22674 Lisp_Object str;
22675 int string_start = 0;
22677 w = decode_any_window (window);
22678 XSETWINDOW (window, w);
22680 if (NILP (buffer))
22681 buffer = w->contents;
22682 CHECK_BUFFER (buffer);
22684 /* Make formatting the modeline a non-op when noninteractive, otherwise
22685 there will be problems later caused by a partially initialized frame. */
22686 if (NILP (format) || noninteractive)
22687 return empty_unibyte_string;
22689 if (no_props)
22690 face = Qnil;
22692 face_id = (NILP (face) || EQ (face, Qdefault)) ? DEFAULT_FACE_ID
22693 : EQ (face, Qt) ? (EQ (window, selected_window)
22694 ? MODE_LINE_FACE_ID : MODE_LINE_INACTIVE_FACE_ID)
22695 : EQ (face, Qmode_line) ? MODE_LINE_FACE_ID
22696 : EQ (face, Qmode_line_inactive) ? MODE_LINE_INACTIVE_FACE_ID
22697 : EQ (face, Qheader_line) ? HEADER_LINE_FACE_ID
22698 : EQ (face, Qtool_bar) ? TOOL_BAR_FACE_ID
22699 : DEFAULT_FACE_ID;
22701 old_buffer = current_buffer;
22703 /* Save things including mode_line_proptrans_alist,
22704 and set that to nil so that we don't alter the outer value. */
22705 record_unwind_protect (unwind_format_mode_line,
22706 format_mode_line_unwind_data
22707 (XFRAME (WINDOW_FRAME (w)),
22708 old_buffer, selected_window, 1));
22709 mode_line_proptrans_alist = Qnil;
22711 Fselect_window (window, Qt);
22712 set_buffer_internal_1 (XBUFFER (buffer));
22714 init_iterator (&it, w, -1, -1, NULL, face_id);
22716 if (no_props)
22718 mode_line_target = MODE_LINE_NOPROP;
22719 mode_line_string_face_prop = Qnil;
22720 mode_line_string_list = Qnil;
22721 string_start = MODE_LINE_NOPROP_LEN (0);
22723 else
22725 mode_line_target = MODE_LINE_STRING;
22726 mode_line_string_list = Qnil;
22727 mode_line_string_face = face;
22728 mode_line_string_face_prop
22729 = NILP (face) ? Qnil : list2 (Qface, face);
22732 push_kboard (FRAME_KBOARD (it.f));
22733 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
22734 pop_kboard ();
22736 if (no_props)
22738 len = MODE_LINE_NOPROP_LEN (string_start);
22739 str = make_string (mode_line_noprop_buf + string_start, len);
22741 else
22743 mode_line_string_list = Fnreverse (mode_line_string_list);
22744 str = Fmapconcat (intern ("identity"), mode_line_string_list,
22745 empty_unibyte_string);
22748 unbind_to (count, Qnil);
22749 return str;
22752 /* Write a null-terminated, right justified decimal representation of
22753 the positive integer D to BUF using a minimal field width WIDTH. */
22755 static void
22756 pint2str (register char *buf, register int width, register ptrdiff_t d)
22758 register char *p = buf;
22760 if (d <= 0)
22761 *p++ = '0';
22762 else
22764 while (d > 0)
22766 *p++ = d % 10 + '0';
22767 d /= 10;
22771 for (width -= (int) (p - buf); width > 0; --width)
22772 *p++ = ' ';
22773 *p-- = '\0';
22774 while (p > buf)
22776 d = *buf;
22777 *buf++ = *p;
22778 *p-- = d;
22782 /* Write a null-terminated, right justified decimal and "human
22783 readable" representation of the nonnegative integer D to BUF using
22784 a minimal field width WIDTH. D should be smaller than 999.5e24. */
22786 static const char power_letter[] =
22788 0, /* no letter */
22789 'k', /* kilo */
22790 'M', /* mega */
22791 'G', /* giga */
22792 'T', /* tera */
22793 'P', /* peta */
22794 'E', /* exa */
22795 'Z', /* zetta */
22796 'Y' /* yotta */
22799 static void
22800 pint2hrstr (char *buf, int width, ptrdiff_t d)
22802 /* We aim to represent the nonnegative integer D as
22803 QUOTIENT.TENTHS * 10 ^ (3 * EXPONENT). */
22804 ptrdiff_t quotient = d;
22805 int remainder = 0;
22806 /* -1 means: do not use TENTHS. */
22807 int tenths = -1;
22808 int exponent = 0;
22810 /* Length of QUOTIENT.TENTHS as a string. */
22811 int length;
22813 char * psuffix;
22814 char * p;
22816 if (quotient >= 1000)
22818 /* Scale to the appropriate EXPONENT. */
22821 remainder = quotient % 1000;
22822 quotient /= 1000;
22823 exponent++;
22825 while (quotient >= 1000);
22827 /* Round to nearest and decide whether to use TENTHS or not. */
22828 if (quotient <= 9)
22830 tenths = remainder / 100;
22831 if (remainder % 100 >= 50)
22833 if (tenths < 9)
22834 tenths++;
22835 else
22837 quotient++;
22838 if (quotient == 10)
22839 tenths = -1;
22840 else
22841 tenths = 0;
22845 else
22846 if (remainder >= 500)
22848 if (quotient < 999)
22849 quotient++;
22850 else
22852 quotient = 1;
22853 exponent++;
22854 tenths = 0;
22859 /* Calculate the LENGTH of QUOTIENT.TENTHS as a string. */
22860 if (tenths == -1 && quotient <= 99)
22861 if (quotient <= 9)
22862 length = 1;
22863 else
22864 length = 2;
22865 else
22866 length = 3;
22867 p = psuffix = buf + max (width, length);
22869 /* Print EXPONENT. */
22870 *psuffix++ = power_letter[exponent];
22871 *psuffix = '\0';
22873 /* Print TENTHS. */
22874 if (tenths >= 0)
22876 *--p = '0' + tenths;
22877 *--p = '.';
22880 /* Print QUOTIENT. */
22883 int digit = quotient % 10;
22884 *--p = '0' + digit;
22886 while ((quotient /= 10) != 0);
22888 /* Print leading spaces. */
22889 while (buf < p)
22890 *--p = ' ';
22893 /* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
22894 If EOL_FLAG is 1, set also a mnemonic character for end-of-line
22895 type of CODING_SYSTEM. Return updated pointer into BUF. */
22897 static unsigned char invalid_eol_type[] = "(*invalid*)";
22899 static char *
22900 decode_mode_spec_coding (Lisp_Object coding_system, register char *buf, int eol_flag)
22902 Lisp_Object val;
22903 bool multibyte = !NILP (BVAR (current_buffer, enable_multibyte_characters));
22904 const unsigned char *eol_str;
22905 int eol_str_len;
22906 /* The EOL conversion we are using. */
22907 Lisp_Object eoltype;
22909 val = CODING_SYSTEM_SPEC (coding_system);
22910 eoltype = Qnil;
22912 if (!VECTORP (val)) /* Not yet decided. */
22914 *buf++ = multibyte ? '-' : ' ';
22915 if (eol_flag)
22916 eoltype = eol_mnemonic_undecided;
22917 /* Don't mention EOL conversion if it isn't decided. */
22919 else
22921 Lisp_Object attrs;
22922 Lisp_Object eolvalue;
22924 attrs = AREF (val, 0);
22925 eolvalue = AREF (val, 2);
22927 *buf++ = multibyte
22928 ? XFASTINT (CODING_ATTR_MNEMONIC (attrs))
22929 : ' ';
22931 if (eol_flag)
22933 /* The EOL conversion that is normal on this system. */
22935 if (NILP (eolvalue)) /* Not yet decided. */
22936 eoltype = eol_mnemonic_undecided;
22937 else if (VECTORP (eolvalue)) /* Not yet decided. */
22938 eoltype = eol_mnemonic_undecided;
22939 else /* eolvalue is Qunix, Qdos, or Qmac. */
22940 eoltype = (EQ (eolvalue, Qunix)
22941 ? eol_mnemonic_unix
22942 : (EQ (eolvalue, Qdos) == 1
22943 ? eol_mnemonic_dos : eol_mnemonic_mac));
22947 if (eol_flag)
22949 /* Mention the EOL conversion if it is not the usual one. */
22950 if (STRINGP (eoltype))
22952 eol_str = SDATA (eoltype);
22953 eol_str_len = SBYTES (eoltype);
22955 else if (CHARACTERP (eoltype))
22957 int c = XFASTINT (eoltype);
22958 return buf + CHAR_STRING (c, (unsigned char *) buf);
22960 else
22962 eol_str = invalid_eol_type;
22963 eol_str_len = sizeof (invalid_eol_type) - 1;
22965 memcpy (buf, eol_str, eol_str_len);
22966 buf += eol_str_len;
22969 return buf;
22972 /* Return a string for the output of a mode line %-spec for window W,
22973 generated by character C. FIELD_WIDTH > 0 means pad the string
22974 returned with spaces to that value. Return a Lisp string in
22975 *STRING if the resulting string is taken from that Lisp string.
22977 Note we operate on the current buffer for most purposes. */
22979 static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
22981 static const char *
22982 decode_mode_spec (struct window *w, register int c, int field_width,
22983 Lisp_Object *string)
22985 Lisp_Object obj;
22986 struct frame *f = XFRAME (WINDOW_FRAME (w));
22987 char *decode_mode_spec_buf = f->decode_mode_spec_buffer;
22988 /* We are going to use f->decode_mode_spec_buffer as the buffer to
22989 produce strings from numerical values, so limit preposterously
22990 large values of FIELD_WIDTH to avoid overrunning the buffer's
22991 end. The size of the buffer is enough for FRAME_MESSAGE_BUF_SIZE
22992 bytes plus the terminating null. */
22993 int width = min (field_width, FRAME_MESSAGE_BUF_SIZE (f));
22994 struct buffer *b = current_buffer;
22996 obj = Qnil;
22997 *string = Qnil;
22999 switch (c)
23001 case '*':
23002 if (!NILP (BVAR (b, read_only)))
23003 return "%";
23004 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
23005 return "*";
23006 return "-";
23008 case '+':
23009 /* This differs from %* only for a modified read-only buffer. */
23010 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
23011 return "*";
23012 if (!NILP (BVAR (b, read_only)))
23013 return "%";
23014 return "-";
23016 case '&':
23017 /* This differs from %* in ignoring read-only-ness. */
23018 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
23019 return "*";
23020 return "-";
23022 case '%':
23023 return "%";
23025 case '[':
23027 int i;
23028 char *p;
23030 if (command_loop_level > 5)
23031 return "[[[... ";
23032 p = decode_mode_spec_buf;
23033 for (i = 0; i < command_loop_level; i++)
23034 *p++ = '[';
23035 *p = 0;
23036 return decode_mode_spec_buf;
23039 case ']':
23041 int i;
23042 char *p;
23044 if (command_loop_level > 5)
23045 return " ...]]]";
23046 p = decode_mode_spec_buf;
23047 for (i = 0; i < command_loop_level; i++)
23048 *p++ = ']';
23049 *p = 0;
23050 return decode_mode_spec_buf;
23053 case '-':
23055 register int i;
23057 /* Let lots_of_dashes be a string of infinite length. */
23058 if (mode_line_target == MODE_LINE_NOPROP
23059 || mode_line_target == MODE_LINE_STRING)
23060 return "--";
23061 if (field_width <= 0
23062 || field_width > sizeof (lots_of_dashes))
23064 for (i = 0; i < FRAME_MESSAGE_BUF_SIZE (f) - 1; ++i)
23065 decode_mode_spec_buf[i] = '-';
23066 decode_mode_spec_buf[i] = '\0';
23067 return decode_mode_spec_buf;
23069 else
23070 return lots_of_dashes;
23073 case 'b':
23074 obj = BVAR (b, name);
23075 break;
23077 case 'c':
23078 /* %c and %l are ignored in `frame-title-format'.
23079 (In redisplay_internal, the frame title is drawn _before_ the
23080 windows are updated, so the stuff which depends on actual
23081 window contents (such as %l) may fail to render properly, or
23082 even crash emacs.) */
23083 if (mode_line_target == MODE_LINE_TITLE)
23084 return "";
23085 else
23087 ptrdiff_t col = current_column ();
23088 w->column_number_displayed = col;
23089 pint2str (decode_mode_spec_buf, width, col);
23090 return decode_mode_spec_buf;
23093 case 'e':
23094 #if !defined SYSTEM_MALLOC && !defined HYBRID_MALLOC
23096 if (NILP (Vmemory_full))
23097 return "";
23098 else
23099 return "!MEM FULL! ";
23101 #else
23102 return "";
23103 #endif
23105 case 'F':
23106 /* %F displays the frame name. */
23107 if (!NILP (f->title))
23108 return SSDATA (f->title);
23109 if (f->explicit_name || ! FRAME_WINDOW_P (f))
23110 return SSDATA (f->name);
23111 return "Emacs";
23113 case 'f':
23114 obj = BVAR (b, filename);
23115 break;
23117 case 'i':
23119 ptrdiff_t size = ZV - BEGV;
23120 pint2str (decode_mode_spec_buf, width, size);
23121 return decode_mode_spec_buf;
23124 case 'I':
23126 ptrdiff_t size = ZV - BEGV;
23127 pint2hrstr (decode_mode_spec_buf, width, size);
23128 return decode_mode_spec_buf;
23131 case 'l':
23133 ptrdiff_t startpos, startpos_byte, line, linepos, linepos_byte;
23134 ptrdiff_t topline, nlines, height;
23135 ptrdiff_t junk;
23137 /* %c and %l are ignored in `frame-title-format'. */
23138 if (mode_line_target == MODE_LINE_TITLE)
23139 return "";
23141 startpos = marker_position (w->start);
23142 startpos_byte = marker_byte_position (w->start);
23143 height = WINDOW_TOTAL_LINES (w);
23145 /* If we decided that this buffer isn't suitable for line numbers,
23146 don't forget that too fast. */
23147 if (w->base_line_pos == -1)
23148 goto no_value;
23150 /* If the buffer is very big, don't waste time. */
23151 if (INTEGERP (Vline_number_display_limit)
23152 && BUF_ZV (b) - BUF_BEGV (b) > XINT (Vline_number_display_limit))
23154 w->base_line_pos = 0;
23155 w->base_line_number = 0;
23156 goto no_value;
23159 if (w->base_line_number > 0
23160 && w->base_line_pos > 0
23161 && w->base_line_pos <= startpos)
23163 line = w->base_line_number;
23164 linepos = w->base_line_pos;
23165 linepos_byte = buf_charpos_to_bytepos (b, linepos);
23167 else
23169 line = 1;
23170 linepos = BUF_BEGV (b);
23171 linepos_byte = BUF_BEGV_BYTE (b);
23174 /* Count lines from base line to window start position. */
23175 nlines = display_count_lines (linepos_byte,
23176 startpos_byte,
23177 startpos, &junk);
23179 topline = nlines + line;
23181 /* Determine a new base line, if the old one is too close
23182 or too far away, or if we did not have one.
23183 "Too close" means it's plausible a scroll-down would
23184 go back past it. */
23185 if (startpos == BUF_BEGV (b))
23187 w->base_line_number = topline;
23188 w->base_line_pos = BUF_BEGV (b);
23190 else if (nlines < height + 25 || nlines > height * 3 + 50
23191 || linepos == BUF_BEGV (b))
23193 ptrdiff_t limit = BUF_BEGV (b);
23194 ptrdiff_t limit_byte = BUF_BEGV_BYTE (b);
23195 ptrdiff_t position;
23196 ptrdiff_t distance =
23197 (height * 2 + 30) * line_number_display_limit_width;
23199 if (startpos - distance > limit)
23201 limit = startpos - distance;
23202 limit_byte = CHAR_TO_BYTE (limit);
23205 nlines = display_count_lines (startpos_byte,
23206 limit_byte,
23207 - (height * 2 + 30),
23208 &position);
23209 /* If we couldn't find the lines we wanted within
23210 line_number_display_limit_width chars per line,
23211 give up on line numbers for this window. */
23212 if (position == limit_byte && limit == startpos - distance)
23214 w->base_line_pos = -1;
23215 w->base_line_number = 0;
23216 goto no_value;
23219 w->base_line_number = topline - nlines;
23220 w->base_line_pos = BYTE_TO_CHAR (position);
23223 /* Now count lines from the start pos to point. */
23224 nlines = display_count_lines (startpos_byte,
23225 PT_BYTE, PT, &junk);
23227 /* Record that we did display the line number. */
23228 line_number_displayed = 1;
23230 /* Make the string to show. */
23231 pint2str (decode_mode_spec_buf, width, topline + nlines);
23232 return decode_mode_spec_buf;
23233 no_value:
23235 char *p = decode_mode_spec_buf;
23236 int pad = width - 2;
23237 while (pad-- > 0)
23238 *p++ = ' ';
23239 *p++ = '?';
23240 *p++ = '?';
23241 *p = '\0';
23242 return decode_mode_spec_buf;
23245 break;
23247 case 'm':
23248 obj = BVAR (b, mode_name);
23249 break;
23251 case 'n':
23252 if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
23253 return " Narrow";
23254 break;
23256 case 'p':
23258 ptrdiff_t pos = marker_position (w->start);
23259 ptrdiff_t total = BUF_ZV (b) - BUF_BEGV (b);
23261 if (w->window_end_pos <= BUF_Z (b) - BUF_ZV (b))
23263 if (pos <= BUF_BEGV (b))
23264 return "All";
23265 else
23266 return "Bottom";
23268 else if (pos <= BUF_BEGV (b))
23269 return "Top";
23270 else
23272 if (total > 1000000)
23273 /* Do it differently for a large value, to avoid overflow. */
23274 total = ((pos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
23275 else
23276 total = ((pos - BUF_BEGV (b)) * 100 + total - 1) / total;
23277 /* We can't normally display a 3-digit number,
23278 so get us a 2-digit number that is close. */
23279 if (total == 100)
23280 total = 99;
23281 sprintf (decode_mode_spec_buf, "%2"pD"d%%", total);
23282 return decode_mode_spec_buf;
23286 /* Display percentage of size above the bottom of the screen. */
23287 case 'P':
23289 ptrdiff_t toppos = marker_position (w->start);
23290 ptrdiff_t botpos = BUF_Z (b) - w->window_end_pos;
23291 ptrdiff_t total = BUF_ZV (b) - BUF_BEGV (b);
23293 if (botpos >= BUF_ZV (b))
23295 if (toppos <= BUF_BEGV (b))
23296 return "All";
23297 else
23298 return "Bottom";
23300 else
23302 if (total > 1000000)
23303 /* Do it differently for a large value, to avoid overflow. */
23304 total = ((botpos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
23305 else
23306 total = ((botpos - BUF_BEGV (b)) * 100 + total - 1) / total;
23307 /* We can't normally display a 3-digit number,
23308 so get us a 2-digit number that is close. */
23309 if (total == 100)
23310 total = 99;
23311 if (toppos <= BUF_BEGV (b))
23312 sprintf (decode_mode_spec_buf, "Top%2"pD"d%%", total);
23313 else
23314 sprintf (decode_mode_spec_buf, "%2"pD"d%%", total);
23315 return decode_mode_spec_buf;
23319 case 's':
23320 /* status of process */
23321 obj = Fget_buffer_process (Fcurrent_buffer ());
23322 if (NILP (obj))
23323 return "no process";
23324 #ifndef MSDOS
23325 obj = Fsymbol_name (Fprocess_status (obj));
23326 #endif
23327 break;
23329 case '@':
23331 ptrdiff_t count = inhibit_garbage_collection ();
23332 Lisp_Object curdir = BVAR (current_buffer, directory);
23333 Lisp_Object val = Qnil;
23335 if (STRINGP (curdir))
23336 val = call1 (intern ("file-remote-p"), curdir);
23338 unbind_to (count, Qnil);
23340 if (NILP (val))
23341 return "-";
23342 else
23343 return "@";
23346 case 'z':
23347 /* coding-system (not including end-of-line format) */
23348 case 'Z':
23349 /* coding-system (including end-of-line type) */
23351 int eol_flag = (c == 'Z');
23352 char *p = decode_mode_spec_buf;
23354 if (! FRAME_WINDOW_P (f))
23356 /* No need to mention EOL here--the terminal never needs
23357 to do EOL conversion. */
23358 p = decode_mode_spec_coding (CODING_ID_NAME
23359 (FRAME_KEYBOARD_CODING (f)->id),
23360 p, 0);
23361 p = decode_mode_spec_coding (CODING_ID_NAME
23362 (FRAME_TERMINAL_CODING (f)->id),
23363 p, 0);
23365 p = decode_mode_spec_coding (BVAR (b, buffer_file_coding_system),
23366 p, eol_flag);
23368 #if 0 /* This proves to be annoying; I think we can do without. -- rms. */
23369 #ifdef subprocesses
23370 obj = Fget_buffer_process (Fcurrent_buffer ());
23371 if (PROCESSP (obj))
23373 p = decode_mode_spec_coding
23374 (XPROCESS (obj)->decode_coding_system, p, eol_flag);
23375 p = decode_mode_spec_coding
23376 (XPROCESS (obj)->encode_coding_system, p, eol_flag);
23378 #endif /* subprocesses */
23379 #endif /* 0 */
23380 *p = 0;
23381 return decode_mode_spec_buf;
23385 if (STRINGP (obj))
23387 *string = obj;
23388 return SSDATA (obj);
23390 else
23391 return "";
23395 /* Count up to COUNT lines starting from START_BYTE. COUNT negative
23396 means count lines back from START_BYTE. But don't go beyond
23397 LIMIT_BYTE. Return the number of lines thus found (always
23398 nonnegative).
23400 Set *BYTE_POS_PTR to the byte position where we stopped. This is
23401 either the position COUNT lines after/before START_BYTE, if we
23402 found COUNT lines, or LIMIT_BYTE if we hit the limit before finding
23403 COUNT lines. */
23405 static ptrdiff_t
23406 display_count_lines (ptrdiff_t start_byte,
23407 ptrdiff_t limit_byte, ptrdiff_t count,
23408 ptrdiff_t *byte_pos_ptr)
23410 register unsigned char *cursor;
23411 unsigned char *base;
23413 register ptrdiff_t ceiling;
23414 register unsigned char *ceiling_addr;
23415 ptrdiff_t orig_count = count;
23417 /* If we are not in selective display mode,
23418 check only for newlines. */
23419 int selective_display = (!NILP (BVAR (current_buffer, selective_display))
23420 && !INTEGERP (BVAR (current_buffer, selective_display)));
23422 if (count > 0)
23424 while (start_byte < limit_byte)
23426 ceiling = BUFFER_CEILING_OF (start_byte);
23427 ceiling = min (limit_byte - 1, ceiling);
23428 ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
23429 base = (cursor = BYTE_POS_ADDR (start_byte));
23433 if (selective_display)
23435 while (*cursor != '\n' && *cursor != 015
23436 && ++cursor != ceiling_addr)
23437 continue;
23438 if (cursor == ceiling_addr)
23439 break;
23441 else
23443 cursor = memchr (cursor, '\n', ceiling_addr - cursor);
23444 if (! cursor)
23445 break;
23448 cursor++;
23450 if (--count == 0)
23452 start_byte += cursor - base;
23453 *byte_pos_ptr = start_byte;
23454 return orig_count;
23457 while (cursor < ceiling_addr);
23459 start_byte += ceiling_addr - base;
23462 else
23464 while (start_byte > limit_byte)
23466 ceiling = BUFFER_FLOOR_OF (start_byte - 1);
23467 ceiling = max (limit_byte, ceiling);
23468 ceiling_addr = BYTE_POS_ADDR (ceiling);
23469 base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
23470 while (1)
23472 if (selective_display)
23474 while (--cursor >= ceiling_addr
23475 && *cursor != '\n' && *cursor != 015)
23476 continue;
23477 if (cursor < ceiling_addr)
23478 break;
23480 else
23482 cursor = memrchr (ceiling_addr, '\n', cursor - ceiling_addr);
23483 if (! cursor)
23484 break;
23487 if (++count == 0)
23489 start_byte += cursor - base + 1;
23490 *byte_pos_ptr = start_byte;
23491 /* When scanning backwards, we should
23492 not count the newline posterior to which we stop. */
23493 return - orig_count - 1;
23496 start_byte += ceiling_addr - base;
23500 *byte_pos_ptr = limit_byte;
23502 if (count < 0)
23503 return - orig_count + count;
23504 return orig_count - count;
23510 /***********************************************************************
23511 Displaying strings
23512 ***********************************************************************/
23514 /* Display a NUL-terminated string, starting with index START.
23516 If STRING is non-null, display that C string. Otherwise, the Lisp
23517 string LISP_STRING is displayed. There's a case that STRING is
23518 non-null and LISP_STRING is not nil. It means STRING is a string
23519 data of LISP_STRING. In that case, we display LISP_STRING while
23520 ignoring its text properties.
23522 If FACE_STRING is not nil, FACE_STRING_POS is a position in
23523 FACE_STRING. Display STRING or LISP_STRING with the face at
23524 FACE_STRING_POS in FACE_STRING:
23526 Display the string in the environment given by IT, but use the
23527 standard display table, temporarily.
23529 FIELD_WIDTH is the minimum number of output glyphs to produce.
23530 If STRING has fewer characters than FIELD_WIDTH, pad to the right
23531 with spaces. If STRING has more characters, more than FIELD_WIDTH
23532 glyphs will be produced. FIELD_WIDTH <= 0 means don't pad.
23534 PRECISION is the maximum number of characters to output from
23535 STRING. PRECISION < 0 means don't truncate the string.
23537 This is roughly equivalent to printf format specifiers:
23539 FIELD_WIDTH PRECISION PRINTF
23540 ----------------------------------------
23541 -1 -1 %s
23542 -1 10 %.10s
23543 10 -1 %10s
23544 20 10 %20.10s
23546 MULTIBYTE zero means do not display multibyte chars, > 0 means do
23547 display them, and < 0 means obey the current buffer's value of
23548 enable_multibyte_characters.
23550 Value is the number of columns displayed. */
23552 static int
23553 display_string (const char *string, Lisp_Object lisp_string, Lisp_Object face_string,
23554 ptrdiff_t face_string_pos, ptrdiff_t start, struct it *it,
23555 int field_width, int precision, int max_x, int multibyte)
23557 int hpos_at_start = it->hpos;
23558 int saved_face_id = it->face_id;
23559 struct glyph_row *row = it->glyph_row;
23560 ptrdiff_t it_charpos;
23562 /* Initialize the iterator IT for iteration over STRING beginning
23563 with index START. */
23564 reseat_to_string (it, NILP (lisp_string) ? string : NULL, lisp_string, start,
23565 precision, field_width, multibyte);
23566 if (string && STRINGP (lisp_string))
23567 /* LISP_STRING is the one returned by decode_mode_spec. We should
23568 ignore its text properties. */
23569 it->stop_charpos = it->end_charpos;
23571 /* If displaying STRING, set up the face of the iterator from
23572 FACE_STRING, if that's given. */
23573 if (STRINGP (face_string))
23575 ptrdiff_t endptr;
23576 struct face *face;
23578 it->face_id
23579 = face_at_string_position (it->w, face_string, face_string_pos,
23580 0, &endptr, it->base_face_id, false);
23581 face = FACE_FROM_ID (it->f, it->face_id);
23582 it->face_box_p = face->box != FACE_NO_BOX;
23585 /* Set max_x to the maximum allowed X position. Don't let it go
23586 beyond the right edge of the window. */
23587 if (max_x <= 0)
23588 max_x = it->last_visible_x;
23589 else
23590 max_x = min (max_x, it->last_visible_x);
23592 /* Skip over display elements that are not visible. because IT->w is
23593 hscrolled. */
23594 if (it->current_x < it->first_visible_x)
23595 move_it_in_display_line_to (it, 100000, it->first_visible_x,
23596 MOVE_TO_POS | MOVE_TO_X);
23598 row->ascent = it->max_ascent;
23599 row->height = it->max_ascent + it->max_descent;
23600 row->phys_ascent = it->max_phys_ascent;
23601 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
23602 row->extra_line_spacing = it->max_extra_line_spacing;
23604 if (STRINGP (it->string))
23605 it_charpos = IT_STRING_CHARPOS (*it);
23606 else
23607 it_charpos = IT_CHARPOS (*it);
23609 /* This condition is for the case that we are called with current_x
23610 past last_visible_x. */
23611 while (it->current_x < max_x)
23613 int x_before, x, n_glyphs_before, i, nglyphs;
23615 /* Get the next display element. */
23616 if (!get_next_display_element (it))
23617 break;
23619 /* Produce glyphs. */
23620 x_before = it->current_x;
23621 n_glyphs_before = row->used[TEXT_AREA];
23622 PRODUCE_GLYPHS (it);
23624 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
23625 i = 0;
23626 x = x_before;
23627 while (i < nglyphs)
23629 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
23631 if (it->line_wrap != TRUNCATE
23632 && x + glyph->pixel_width > max_x)
23634 /* End of continued line or max_x reached. */
23635 if (CHAR_GLYPH_PADDING_P (*glyph))
23637 /* A wide character is unbreakable. */
23638 if (row->reversed_p)
23639 unproduce_glyphs (it, row->used[TEXT_AREA]
23640 - n_glyphs_before);
23641 row->used[TEXT_AREA] = n_glyphs_before;
23642 it->current_x = x_before;
23644 else
23646 if (row->reversed_p)
23647 unproduce_glyphs (it, row->used[TEXT_AREA]
23648 - (n_glyphs_before + i));
23649 row->used[TEXT_AREA] = n_glyphs_before + i;
23650 it->current_x = x;
23652 break;
23654 else if (x + glyph->pixel_width >= it->first_visible_x)
23656 /* Glyph is at least partially visible. */
23657 ++it->hpos;
23658 if (x < it->first_visible_x)
23659 row->x = x - it->first_visible_x;
23661 else
23663 /* Glyph is off the left margin of the display area.
23664 Should not happen. */
23665 emacs_abort ();
23668 row->ascent = max (row->ascent, it->max_ascent);
23669 row->height = max (row->height, it->max_ascent + it->max_descent);
23670 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
23671 row->phys_height = max (row->phys_height,
23672 it->max_phys_ascent + it->max_phys_descent);
23673 row->extra_line_spacing = max (row->extra_line_spacing,
23674 it->max_extra_line_spacing);
23675 x += glyph->pixel_width;
23676 ++i;
23679 /* Stop if max_x reached. */
23680 if (i < nglyphs)
23681 break;
23683 /* Stop at line ends. */
23684 if (ITERATOR_AT_END_OF_LINE_P (it))
23686 it->continuation_lines_width = 0;
23687 break;
23690 set_iterator_to_next (it, 1);
23691 if (STRINGP (it->string))
23692 it_charpos = IT_STRING_CHARPOS (*it);
23693 else
23694 it_charpos = IT_CHARPOS (*it);
23696 /* Stop if truncating at the right edge. */
23697 if (it->line_wrap == TRUNCATE
23698 && it->current_x >= it->last_visible_x)
23700 /* Add truncation mark, but don't do it if the line is
23701 truncated at a padding space. */
23702 if (it_charpos < it->string_nchars)
23704 if (!FRAME_WINDOW_P (it->f))
23706 int ii, n;
23708 if (it->current_x > it->last_visible_x)
23710 if (!row->reversed_p)
23712 for (ii = row->used[TEXT_AREA] - 1; ii > 0; --ii)
23713 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][ii]))
23714 break;
23716 else
23718 for (ii = 0; ii < row->used[TEXT_AREA]; ii++)
23719 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][ii]))
23720 break;
23721 unproduce_glyphs (it, ii + 1);
23722 ii = row->used[TEXT_AREA] - (ii + 1);
23724 for (n = row->used[TEXT_AREA]; ii < n; ++ii)
23726 row->used[TEXT_AREA] = ii;
23727 produce_special_glyphs (it, IT_TRUNCATION);
23730 produce_special_glyphs (it, IT_TRUNCATION);
23732 row->truncated_on_right_p = 1;
23734 break;
23738 /* Maybe insert a truncation at the left. */
23739 if (it->first_visible_x
23740 && it_charpos > 0)
23742 if (!FRAME_WINDOW_P (it->f)
23743 || (row->reversed_p
23744 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
23745 : WINDOW_LEFT_FRINGE_WIDTH (it->w)) == 0)
23746 insert_left_trunc_glyphs (it);
23747 row->truncated_on_left_p = 1;
23750 it->face_id = saved_face_id;
23752 /* Value is number of columns displayed. */
23753 return it->hpos - hpos_at_start;
23758 /* This is like a combination of memq and assq. Return 1/2 if PROPVAL
23759 appears as an element of LIST or as the car of an element of LIST.
23760 If PROPVAL is a list, compare each element against LIST in that
23761 way, and return 1/2 if any element of PROPVAL is found in LIST.
23762 Otherwise return 0. This function cannot quit.
23763 The return value is 2 if the text is invisible but with an ellipsis
23764 and 1 if it's invisible and without an ellipsis. */
23767 invisible_p (register Lisp_Object propval, Lisp_Object list)
23769 register Lisp_Object tail, proptail;
23771 for (tail = list; CONSP (tail); tail = XCDR (tail))
23773 register Lisp_Object tem;
23774 tem = XCAR (tail);
23775 if (EQ (propval, tem))
23776 return 1;
23777 if (CONSP (tem) && EQ (propval, XCAR (tem)))
23778 return NILP (XCDR (tem)) ? 1 : 2;
23781 if (CONSP (propval))
23783 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
23785 Lisp_Object propelt;
23786 propelt = XCAR (proptail);
23787 for (tail = list; CONSP (tail); tail = XCDR (tail))
23789 register Lisp_Object tem;
23790 tem = XCAR (tail);
23791 if (EQ (propelt, tem))
23792 return 1;
23793 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
23794 return NILP (XCDR (tem)) ? 1 : 2;
23799 return 0;
23802 DEFUN ("invisible-p", Finvisible_p, Sinvisible_p, 1, 1, 0,
23803 doc: /* Non-nil if the property makes the text invisible.
23804 POS-OR-PROP can be a marker or number, in which case it is taken to be
23805 a position in the current buffer and the value of the `invisible' property
23806 is checked; or it can be some other value, which is then presumed to be the
23807 value of the `invisible' property of the text of interest.
23808 The non-nil value returned can be t for truly invisible text or something
23809 else if the text is replaced by an ellipsis. */)
23810 (Lisp_Object pos_or_prop)
23812 Lisp_Object prop
23813 = (NATNUMP (pos_or_prop) || MARKERP (pos_or_prop)
23814 ? Fget_char_property (pos_or_prop, Qinvisible, Qnil)
23815 : pos_or_prop);
23816 int invis = TEXT_PROP_MEANS_INVISIBLE (prop);
23817 return (invis == 0 ? Qnil
23818 : invis == 1 ? Qt
23819 : make_number (invis));
23822 /* Calculate a width or height in pixels from a specification using
23823 the following elements:
23825 SPEC ::=
23826 NUM - a (fractional) multiple of the default font width/height
23827 (NUM) - specifies exactly NUM pixels
23828 UNIT - a fixed number of pixels, see below.
23829 ELEMENT - size of a display element in pixels, see below.
23830 (NUM . SPEC) - equals NUM * SPEC
23831 (+ SPEC SPEC ...) - add pixel values
23832 (- SPEC SPEC ...) - subtract pixel values
23833 (- SPEC) - negate pixel value
23835 NUM ::=
23836 INT or FLOAT - a number constant
23837 SYMBOL - use symbol's (buffer local) variable binding.
23839 UNIT ::=
23840 in - pixels per inch *)
23841 mm - pixels per 1/1000 meter *)
23842 cm - pixels per 1/100 meter *)
23843 width - width of current font in pixels.
23844 height - height of current font in pixels.
23846 *) using the ratio(s) defined in display-pixels-per-inch.
23848 ELEMENT ::=
23850 left-fringe - left fringe width in pixels
23851 right-fringe - right fringe width in pixels
23853 left-margin - left margin width in pixels
23854 right-margin - right margin width in pixels
23856 scroll-bar - scroll-bar area width in pixels
23858 Examples:
23860 Pixels corresponding to 5 inches:
23861 (5 . in)
23863 Total width of non-text areas on left side of window (if scroll-bar is on left):
23864 '(space :width (+ left-fringe left-margin scroll-bar))
23866 Align to first text column (in header line):
23867 '(space :align-to 0)
23869 Align to middle of text area minus half the width of variable `my-image'
23870 containing a loaded image:
23871 '(space :align-to (0.5 . (- text my-image)))
23873 Width of left margin minus width of 1 character in the default font:
23874 '(space :width (- left-margin 1))
23876 Width of left margin minus width of 2 characters in the current font:
23877 '(space :width (- left-margin (2 . width)))
23879 Center 1 character over left-margin (in header line):
23880 '(space :align-to (+ left-margin (0.5 . left-margin) -0.5))
23882 Different ways to express width of left fringe plus left margin minus one pixel:
23883 '(space :width (- (+ left-fringe left-margin) (1)))
23884 '(space :width (+ left-fringe left-margin (- (1))))
23885 '(space :width (+ left-fringe left-margin (-1)))
23889 static int
23890 calc_pixel_width_or_height (double *res, struct it *it, Lisp_Object prop,
23891 struct font *font, int width_p, int *align_to)
23893 double pixels;
23895 #define OK_PIXELS(val) ((*res = (double)(val)), 1)
23896 #define OK_ALIGN_TO(val) ((*align_to = (int)(val)), 1)
23898 if (NILP (prop))
23899 return OK_PIXELS (0);
23901 eassert (FRAME_LIVE_P (it->f));
23903 if (SYMBOLP (prop))
23905 if (SCHARS (SYMBOL_NAME (prop)) == 2)
23907 char *unit = SSDATA (SYMBOL_NAME (prop));
23909 if (unit[0] == 'i' && unit[1] == 'n')
23910 pixels = 1.0;
23911 else if (unit[0] == 'm' && unit[1] == 'm')
23912 pixels = 25.4;
23913 else if (unit[0] == 'c' && unit[1] == 'm')
23914 pixels = 2.54;
23915 else
23916 pixels = 0;
23917 if (pixels > 0)
23919 double ppi = (width_p ? FRAME_RES_X (it->f)
23920 : FRAME_RES_Y (it->f));
23922 if (ppi > 0)
23923 return OK_PIXELS (ppi / pixels);
23924 return 0;
23928 #ifdef HAVE_WINDOW_SYSTEM
23929 if (EQ (prop, Qheight))
23930 return OK_PIXELS (font ? FONT_HEIGHT (font) : FRAME_LINE_HEIGHT (it->f));
23931 if (EQ (prop, Qwidth))
23932 return OK_PIXELS (font ? FONT_WIDTH (font) : FRAME_COLUMN_WIDTH (it->f));
23933 #else
23934 if (EQ (prop, Qheight) || EQ (prop, Qwidth))
23935 return OK_PIXELS (1);
23936 #endif
23938 if (EQ (prop, Qtext))
23939 return OK_PIXELS (width_p
23940 ? window_box_width (it->w, TEXT_AREA)
23941 : WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w));
23943 if (align_to && *align_to < 0)
23945 *res = 0;
23946 if (EQ (prop, Qleft))
23947 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA));
23948 if (EQ (prop, Qright))
23949 return OK_ALIGN_TO (window_box_right_offset (it->w, TEXT_AREA));
23950 if (EQ (prop, Qcenter))
23951 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA)
23952 + window_box_width (it->w, TEXT_AREA) / 2);
23953 if (EQ (prop, Qleft_fringe))
23954 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
23955 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (it->w)
23956 : window_box_right_offset (it->w, LEFT_MARGIN_AREA));
23957 if (EQ (prop, Qright_fringe))
23958 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
23959 ? window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
23960 : window_box_right_offset (it->w, TEXT_AREA));
23961 if (EQ (prop, Qleft_margin))
23962 return OK_ALIGN_TO (window_box_left_offset (it->w, LEFT_MARGIN_AREA));
23963 if (EQ (prop, Qright_margin))
23964 return OK_ALIGN_TO (window_box_left_offset (it->w, RIGHT_MARGIN_AREA));
23965 if (EQ (prop, Qscroll_bar))
23966 return OK_ALIGN_TO (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (it->w)
23968 : (window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
23969 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
23970 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
23971 : 0)));
23973 else
23975 if (EQ (prop, Qleft_fringe))
23976 return OK_PIXELS (WINDOW_LEFT_FRINGE_WIDTH (it->w));
23977 if (EQ (prop, Qright_fringe))
23978 return OK_PIXELS (WINDOW_RIGHT_FRINGE_WIDTH (it->w));
23979 if (EQ (prop, Qleft_margin))
23980 return OK_PIXELS (WINDOW_LEFT_MARGIN_WIDTH (it->w));
23981 if (EQ (prop, Qright_margin))
23982 return OK_PIXELS (WINDOW_RIGHT_MARGIN_WIDTH (it->w));
23983 if (EQ (prop, Qscroll_bar))
23984 return OK_PIXELS (WINDOW_SCROLL_BAR_AREA_WIDTH (it->w));
23987 prop = buffer_local_value (prop, it->w->contents);
23988 if (EQ (prop, Qunbound))
23989 prop = Qnil;
23992 if (INTEGERP (prop) || FLOATP (prop))
23994 int base_unit = (width_p
23995 ? FRAME_COLUMN_WIDTH (it->f)
23996 : FRAME_LINE_HEIGHT (it->f));
23997 return OK_PIXELS (XFLOATINT (prop) * base_unit);
24000 if (CONSP (prop))
24002 Lisp_Object car = XCAR (prop);
24003 Lisp_Object cdr = XCDR (prop);
24005 if (SYMBOLP (car))
24007 #ifdef HAVE_WINDOW_SYSTEM
24008 if (FRAME_WINDOW_P (it->f)
24009 && valid_image_p (prop))
24011 ptrdiff_t id = lookup_image (it->f, prop);
24012 struct image *img = IMAGE_FROM_ID (it->f, id);
24014 return OK_PIXELS (width_p ? img->width : img->height);
24016 #endif
24017 if (EQ (car, Qplus) || EQ (car, Qminus))
24019 int first = 1;
24020 double px;
24022 pixels = 0;
24023 while (CONSP (cdr))
24025 if (!calc_pixel_width_or_height (&px, it, XCAR (cdr),
24026 font, width_p, align_to))
24027 return 0;
24028 if (first)
24029 pixels = (EQ (car, Qplus) ? px : -px), first = 0;
24030 else
24031 pixels += px;
24032 cdr = XCDR (cdr);
24034 if (EQ (car, Qminus))
24035 pixels = -pixels;
24036 return OK_PIXELS (pixels);
24039 car = buffer_local_value (car, it->w->contents);
24040 if (EQ (car, Qunbound))
24041 car = Qnil;
24044 if (INTEGERP (car) || FLOATP (car))
24046 double fact;
24047 pixels = XFLOATINT (car);
24048 if (NILP (cdr))
24049 return OK_PIXELS (pixels);
24050 if (calc_pixel_width_or_height (&fact, it, cdr,
24051 font, width_p, align_to))
24052 return OK_PIXELS (pixels * fact);
24053 return 0;
24056 return 0;
24059 return 0;
24063 /***********************************************************************
24064 Glyph Display
24065 ***********************************************************************/
24067 #ifdef HAVE_WINDOW_SYSTEM
24069 #ifdef GLYPH_DEBUG
24071 void
24072 dump_glyph_string (struct glyph_string *s)
24074 fprintf (stderr, "glyph string\n");
24075 fprintf (stderr, " x, y, w, h = %d, %d, %d, %d\n",
24076 s->x, s->y, s->width, s->height);
24077 fprintf (stderr, " ybase = %d\n", s->ybase);
24078 fprintf (stderr, " hl = %d\n", s->hl);
24079 fprintf (stderr, " left overhang = %d, right = %d\n",
24080 s->left_overhang, s->right_overhang);
24081 fprintf (stderr, " nchars = %d\n", s->nchars);
24082 fprintf (stderr, " extends to end of line = %d\n",
24083 s->extends_to_end_of_line_p);
24084 fprintf (stderr, " font height = %d\n", FONT_HEIGHT (s->font));
24085 fprintf (stderr, " bg width = %d\n", s->background_width);
24088 #endif /* GLYPH_DEBUG */
24090 /* Initialize glyph string S. CHAR2B is a suitably allocated vector
24091 of XChar2b structures for S; it can't be allocated in
24092 init_glyph_string because it must be allocated via `alloca'. W
24093 is the window on which S is drawn. ROW and AREA are the glyph row
24094 and area within the row from which S is constructed. START is the
24095 index of the first glyph structure covered by S. HL is a
24096 face-override for drawing S. */
24098 #ifdef HAVE_NTGUI
24099 #define OPTIONAL_HDC(hdc) HDC hdc,
24100 #define DECLARE_HDC(hdc) HDC hdc;
24101 #define ALLOCATE_HDC(hdc, f) hdc = get_frame_dc ((f))
24102 #define RELEASE_HDC(hdc, f) release_frame_dc ((f), (hdc))
24103 #endif
24105 #ifndef OPTIONAL_HDC
24106 #define OPTIONAL_HDC(hdc)
24107 #define DECLARE_HDC(hdc)
24108 #define ALLOCATE_HDC(hdc, f)
24109 #define RELEASE_HDC(hdc, f)
24110 #endif
24112 static void
24113 init_glyph_string (struct glyph_string *s,
24114 OPTIONAL_HDC (hdc)
24115 XChar2b *char2b, struct window *w, struct glyph_row *row,
24116 enum glyph_row_area area, int start, enum draw_glyphs_face hl)
24118 memset (s, 0, sizeof *s);
24119 s->w = w;
24120 s->f = XFRAME (w->frame);
24121 #ifdef HAVE_NTGUI
24122 s->hdc = hdc;
24123 #endif
24124 s->display = FRAME_X_DISPLAY (s->f);
24125 s->window = FRAME_X_WINDOW (s->f);
24126 s->char2b = char2b;
24127 s->hl = hl;
24128 s->row = row;
24129 s->area = area;
24130 s->first_glyph = row->glyphs[area] + start;
24131 s->height = row->height;
24132 s->y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
24133 s->ybase = s->y + row->ascent;
24137 /* Append the list of glyph strings with head H and tail T to the list
24138 with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the result. */
24140 static void
24141 append_glyph_string_lists (struct glyph_string **head, struct glyph_string **tail,
24142 struct glyph_string *h, struct glyph_string *t)
24144 if (h)
24146 if (*head)
24147 (*tail)->next = h;
24148 else
24149 *head = h;
24150 h->prev = *tail;
24151 *tail = t;
24156 /* Prepend the list of glyph strings with head H and tail T to the
24157 list with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the
24158 result. */
24160 static void
24161 prepend_glyph_string_lists (struct glyph_string **head, struct glyph_string **tail,
24162 struct glyph_string *h, struct glyph_string *t)
24164 if (h)
24166 if (*head)
24167 (*head)->prev = t;
24168 else
24169 *tail = t;
24170 t->next = *head;
24171 *head = h;
24176 /* Append glyph string S to the list with head *HEAD and tail *TAIL.
24177 Set *HEAD and *TAIL to the resulting list. */
24179 static void
24180 append_glyph_string (struct glyph_string **head, struct glyph_string **tail,
24181 struct glyph_string *s)
24183 s->next = s->prev = NULL;
24184 append_glyph_string_lists (head, tail, s, s);
24188 /* Get face and two-byte form of character C in face FACE_ID on frame F.
24189 The encoding of C is returned in *CHAR2B. DISPLAY_P non-zero means
24190 make sure that X resources for the face returned are allocated.
24191 Value is a pointer to a realized face that is ready for display if
24192 DISPLAY_P is non-zero. */
24194 static struct face *
24195 get_char_face_and_encoding (struct frame *f, int c, int face_id,
24196 XChar2b *char2b, int display_p)
24198 struct face *face = FACE_FROM_ID (f, face_id);
24199 unsigned code = 0;
24201 if (face->font)
24203 code = face->font->driver->encode_char (face->font, c);
24205 if (code == FONT_INVALID_CODE)
24206 code = 0;
24208 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
24210 /* Make sure X resources of the face are allocated. */
24211 #ifdef HAVE_X_WINDOWS
24212 if (display_p)
24213 #endif
24215 eassert (face != NULL);
24216 prepare_face_for_display (f, face);
24219 return face;
24223 /* Get face and two-byte form of character glyph GLYPH on frame F.
24224 The encoding of GLYPH->u.ch is returned in *CHAR2B. Value is
24225 a pointer to a realized face that is ready for display. */
24227 static struct face *
24228 get_glyph_face_and_encoding (struct frame *f, struct glyph *glyph,
24229 XChar2b *char2b)
24231 struct face *face;
24232 unsigned code = 0;
24234 eassert (glyph->type == CHAR_GLYPH);
24235 face = FACE_FROM_ID (f, glyph->face_id);
24237 /* Make sure X resources of the face are allocated. */
24238 eassert (face != NULL);
24239 prepare_face_for_display (f, face);
24241 if (face->font)
24243 if (CHAR_BYTE8_P (glyph->u.ch))
24244 code = CHAR_TO_BYTE8 (glyph->u.ch);
24245 else
24246 code = face->font->driver->encode_char (face->font, glyph->u.ch);
24248 if (code == FONT_INVALID_CODE)
24249 code = 0;
24252 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
24253 return face;
24257 /* Get glyph code of character C in FONT in the two-byte form CHAR2B.
24258 Return 1 if FONT has a glyph for C, otherwise return 0. */
24260 static int
24261 get_char_glyph_code (int c, struct font *font, XChar2b *char2b)
24263 unsigned code;
24265 if (CHAR_BYTE8_P (c))
24266 code = CHAR_TO_BYTE8 (c);
24267 else
24268 code = font->driver->encode_char (font, c);
24270 if (code == FONT_INVALID_CODE)
24271 return 0;
24272 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
24273 return 1;
24277 /* Fill glyph string S with composition components specified by S->cmp.
24279 BASE_FACE is the base face of the composition.
24280 S->cmp_from is the index of the first component for S.
24282 OVERLAPS non-zero means S should draw the foreground only, and use
24283 its physical height for clipping. See also draw_glyphs.
24285 Value is the index of a component not in S. */
24287 static int
24288 fill_composite_glyph_string (struct glyph_string *s, struct face *base_face,
24289 int overlaps)
24291 int i;
24292 /* For all glyphs of this composition, starting at the offset
24293 S->cmp_from, until we reach the end of the definition or encounter a
24294 glyph that requires the different face, add it to S. */
24295 struct face *face;
24297 eassert (s);
24299 s->for_overlaps = overlaps;
24300 s->face = NULL;
24301 s->font = NULL;
24302 for (i = s->cmp_from; i < s->cmp->glyph_len; i++)
24304 int c = COMPOSITION_GLYPH (s->cmp, i);
24306 /* TAB in a composition means display glyphs with padding space
24307 on the left or right. */
24308 if (c != '\t')
24310 int face_id = FACE_FOR_CHAR (s->f, base_face->ascii_face, c,
24311 -1, Qnil);
24313 face = get_char_face_and_encoding (s->f, c, face_id,
24314 s->char2b + i, 1);
24315 if (face)
24317 if (! s->face)
24319 s->face = face;
24320 s->font = s->face->font;
24322 else if (s->face != face)
24323 break;
24326 ++s->nchars;
24328 s->cmp_to = i;
24330 if (s->face == NULL)
24332 s->face = base_face->ascii_face;
24333 s->font = s->face->font;
24336 /* All glyph strings for the same composition has the same width,
24337 i.e. the width set for the first component of the composition. */
24338 s->width = s->first_glyph->pixel_width;
24340 /* If the specified font could not be loaded, use the frame's
24341 default font, but record the fact that we couldn't load it in
24342 the glyph string so that we can draw rectangles for the
24343 characters of the glyph string. */
24344 if (s->font == NULL)
24346 s->font_not_found_p = 1;
24347 s->font = FRAME_FONT (s->f);
24350 /* Adjust base line for subscript/superscript text. */
24351 s->ybase += s->first_glyph->voffset;
24353 return s->cmp_to;
24356 static int
24357 fill_gstring_glyph_string (struct glyph_string *s, int face_id,
24358 int start, int end, int overlaps)
24360 struct glyph *glyph, *last;
24361 Lisp_Object lgstring;
24362 int i;
24364 s->for_overlaps = overlaps;
24365 glyph = s->row->glyphs[s->area] + start;
24366 last = s->row->glyphs[s->area] + end;
24367 s->cmp_id = glyph->u.cmp.id;
24368 s->cmp_from = glyph->slice.cmp.from;
24369 s->cmp_to = glyph->slice.cmp.to + 1;
24370 s->face = FACE_FROM_ID (s->f, face_id);
24371 lgstring = composition_gstring_from_id (s->cmp_id);
24372 s->font = XFONT_OBJECT (LGSTRING_FONT (lgstring));
24373 glyph++;
24374 while (glyph < last
24375 && glyph->u.cmp.automatic
24376 && glyph->u.cmp.id == s->cmp_id
24377 && s->cmp_to == glyph->slice.cmp.from)
24378 s->cmp_to = (glyph++)->slice.cmp.to + 1;
24380 for (i = s->cmp_from; i < s->cmp_to; i++)
24382 Lisp_Object lglyph = LGSTRING_GLYPH (lgstring, i);
24383 unsigned code = LGLYPH_CODE (lglyph);
24385 STORE_XCHAR2B ((s->char2b + i), code >> 8, code & 0xFF);
24387 s->width = composition_gstring_width (lgstring, s->cmp_from, s->cmp_to, NULL);
24388 return glyph - s->row->glyphs[s->area];
24392 /* Fill glyph string S from a sequence glyphs for glyphless characters.
24393 See the comment of fill_glyph_string for arguments.
24394 Value is the index of the first glyph not in S. */
24397 static int
24398 fill_glyphless_glyph_string (struct glyph_string *s, int face_id,
24399 int start, int end, int overlaps)
24401 struct glyph *glyph, *last;
24402 int voffset;
24404 eassert (s->first_glyph->type == GLYPHLESS_GLYPH);
24405 s->for_overlaps = overlaps;
24406 glyph = s->row->glyphs[s->area] + start;
24407 last = s->row->glyphs[s->area] + end;
24408 voffset = glyph->voffset;
24409 s->face = FACE_FROM_ID (s->f, face_id);
24410 s->font = s->face->font ? s->face->font : FRAME_FONT (s->f);
24411 s->nchars = 1;
24412 s->width = glyph->pixel_width;
24413 glyph++;
24414 while (glyph < last
24415 && glyph->type == GLYPHLESS_GLYPH
24416 && glyph->voffset == voffset
24417 && glyph->face_id == face_id)
24419 s->nchars++;
24420 s->width += glyph->pixel_width;
24421 glyph++;
24423 s->ybase += voffset;
24424 return glyph - s->row->glyphs[s->area];
24428 /* Fill glyph string S from a sequence of character glyphs.
24430 FACE_ID is the face id of the string. START is the index of the
24431 first glyph to consider, END is the index of the last + 1.
24432 OVERLAPS non-zero means S should draw the foreground only, and use
24433 its physical height for clipping. See also draw_glyphs.
24435 Value is the index of the first glyph not in S. */
24437 static int
24438 fill_glyph_string (struct glyph_string *s, int face_id,
24439 int start, int end, int overlaps)
24441 struct glyph *glyph, *last;
24442 int voffset;
24443 int glyph_not_available_p;
24445 eassert (s->f == XFRAME (s->w->frame));
24446 eassert (s->nchars == 0);
24447 eassert (start >= 0 && end > start);
24449 s->for_overlaps = overlaps;
24450 glyph = s->row->glyphs[s->area] + start;
24451 last = s->row->glyphs[s->area] + end;
24452 voffset = glyph->voffset;
24453 s->padding_p = glyph->padding_p;
24454 glyph_not_available_p = glyph->glyph_not_available_p;
24456 while (glyph < last
24457 && glyph->type == CHAR_GLYPH
24458 && glyph->voffset == voffset
24459 /* Same face id implies same font, nowadays. */
24460 && glyph->face_id == face_id
24461 && glyph->glyph_not_available_p == glyph_not_available_p)
24463 s->face = get_glyph_face_and_encoding (s->f, glyph,
24464 s->char2b + s->nchars);
24465 ++s->nchars;
24466 eassert (s->nchars <= end - start);
24467 s->width += glyph->pixel_width;
24468 if (glyph++->padding_p != s->padding_p)
24469 break;
24472 s->font = s->face->font;
24474 /* If the specified font could not be loaded, use the frame's font,
24475 but record the fact that we couldn't load it in
24476 S->font_not_found_p so that we can draw rectangles for the
24477 characters of the glyph string. */
24478 if (s->font == NULL || glyph_not_available_p)
24480 s->font_not_found_p = 1;
24481 s->font = FRAME_FONT (s->f);
24484 /* Adjust base line for subscript/superscript text. */
24485 s->ybase += voffset;
24487 eassert (s->face && s->face->gc);
24488 return glyph - s->row->glyphs[s->area];
24492 /* Fill glyph string S from image glyph S->first_glyph. */
24494 static void
24495 fill_image_glyph_string (struct glyph_string *s)
24497 eassert (s->first_glyph->type == IMAGE_GLYPH);
24498 s->img = IMAGE_FROM_ID (s->f, s->first_glyph->u.img_id);
24499 eassert (s->img);
24500 s->slice = s->first_glyph->slice.img;
24501 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
24502 s->font = s->face->font;
24503 s->width = s->first_glyph->pixel_width;
24505 /* Adjust base line for subscript/superscript text. */
24506 s->ybase += s->first_glyph->voffset;
24510 /* Fill glyph string S from a sequence of stretch glyphs.
24512 START is the index of the first glyph to consider,
24513 END is the index of the last + 1.
24515 Value is the index of the first glyph not in S. */
24517 static int
24518 fill_stretch_glyph_string (struct glyph_string *s, int start, int end)
24520 struct glyph *glyph, *last;
24521 int voffset, face_id;
24523 eassert (s->first_glyph->type == STRETCH_GLYPH);
24525 glyph = s->row->glyphs[s->area] + start;
24526 last = s->row->glyphs[s->area] + end;
24527 face_id = glyph->face_id;
24528 s->face = FACE_FROM_ID (s->f, face_id);
24529 s->font = s->face->font;
24530 s->width = glyph->pixel_width;
24531 s->nchars = 1;
24532 voffset = glyph->voffset;
24534 for (++glyph;
24535 (glyph < last
24536 && glyph->type == STRETCH_GLYPH
24537 && glyph->voffset == voffset
24538 && glyph->face_id == face_id);
24539 ++glyph)
24540 s->width += glyph->pixel_width;
24542 /* Adjust base line for subscript/superscript text. */
24543 s->ybase += voffset;
24545 /* The case that face->gc == 0 is handled when drawing the glyph
24546 string by calling prepare_face_for_display. */
24547 eassert (s->face);
24548 return glyph - s->row->glyphs[s->area];
24551 static struct font_metrics *
24552 get_per_char_metric (struct font *font, XChar2b *char2b)
24554 static struct font_metrics metrics;
24555 unsigned code;
24557 if (! font)
24558 return NULL;
24559 code = (XCHAR2B_BYTE1 (char2b) << 8) | XCHAR2B_BYTE2 (char2b);
24560 if (code == FONT_INVALID_CODE)
24561 return NULL;
24562 font->driver->text_extents (font, &code, 1, &metrics);
24563 return &metrics;
24566 /* EXPORT for RIF:
24567 Set *LEFT and *RIGHT to the left and right overhang of GLYPH on
24568 frame F. Overhangs of glyphs other than type CHAR_GLYPH are
24569 assumed to be zero. */
24571 void
24572 x_get_glyph_overhangs (struct glyph *glyph, struct frame *f, int *left, int *right)
24574 *left = *right = 0;
24576 if (glyph->type == CHAR_GLYPH)
24578 XChar2b char2b;
24579 struct face *face = get_glyph_face_and_encoding (f, glyph, &char2b);
24580 if (face->font)
24582 struct font_metrics *pcm = get_per_char_metric (face->font, &char2b);
24583 if (pcm)
24585 if (pcm->rbearing > pcm->width)
24586 *right = pcm->rbearing - pcm->width;
24587 if (pcm->lbearing < 0)
24588 *left = -pcm->lbearing;
24592 else if (glyph->type == COMPOSITE_GLYPH)
24594 if (! glyph->u.cmp.automatic)
24596 struct composition *cmp = composition_table[glyph->u.cmp.id];
24598 if (cmp->rbearing > cmp->pixel_width)
24599 *right = cmp->rbearing - cmp->pixel_width;
24600 if (cmp->lbearing < 0)
24601 *left = - cmp->lbearing;
24603 else
24605 Lisp_Object gstring = composition_gstring_from_id (glyph->u.cmp.id);
24606 struct font_metrics metrics;
24608 composition_gstring_width (gstring, glyph->slice.cmp.from,
24609 glyph->slice.cmp.to + 1, &metrics);
24610 if (metrics.rbearing > metrics.width)
24611 *right = metrics.rbearing - metrics.width;
24612 if (metrics.lbearing < 0)
24613 *left = - metrics.lbearing;
24619 /* Return the index of the first glyph preceding glyph string S that
24620 is overwritten by S because of S's left overhang. Value is -1
24621 if no glyphs are overwritten. */
24623 static int
24624 left_overwritten (struct glyph_string *s)
24626 int k;
24628 if (s->left_overhang)
24630 int x = 0, i;
24631 struct glyph *glyphs = s->row->glyphs[s->area];
24632 int first = s->first_glyph - glyphs;
24634 for (i = first - 1; i >= 0 && x > -s->left_overhang; --i)
24635 x -= glyphs[i].pixel_width;
24637 k = i + 1;
24639 else
24640 k = -1;
24642 return k;
24646 /* Return the index of the first glyph preceding glyph string S that
24647 is overwriting S because of its right overhang. Value is -1 if no
24648 glyph in front of S overwrites S. */
24650 static int
24651 left_overwriting (struct glyph_string *s)
24653 int i, k, x;
24654 struct glyph *glyphs = s->row->glyphs[s->area];
24655 int first = s->first_glyph - glyphs;
24657 k = -1;
24658 x = 0;
24659 for (i = first - 1; i >= 0; --i)
24661 int left, right;
24662 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
24663 if (x + right > 0)
24664 k = i;
24665 x -= glyphs[i].pixel_width;
24668 return k;
24672 /* Return the index of the last glyph following glyph string S that is
24673 overwritten by S because of S's right overhang. Value is -1 if
24674 no such glyph is found. */
24676 static int
24677 right_overwritten (struct glyph_string *s)
24679 int k = -1;
24681 if (s->right_overhang)
24683 int x = 0, i;
24684 struct glyph *glyphs = s->row->glyphs[s->area];
24685 int first = (s->first_glyph - glyphs
24686 + (s->first_glyph->type == COMPOSITE_GLYPH ? 1 : s->nchars));
24687 int end = s->row->used[s->area];
24689 for (i = first; i < end && s->right_overhang > x; ++i)
24690 x += glyphs[i].pixel_width;
24692 k = i;
24695 return k;
24699 /* Return the index of the last glyph following glyph string S that
24700 overwrites S because of its left overhang. Value is negative
24701 if no such glyph is found. */
24703 static int
24704 right_overwriting (struct glyph_string *s)
24706 int i, k, x;
24707 int end = s->row->used[s->area];
24708 struct glyph *glyphs = s->row->glyphs[s->area];
24709 int first = (s->first_glyph - glyphs
24710 + (s->first_glyph->type == COMPOSITE_GLYPH ? 1 : s->nchars));
24712 k = -1;
24713 x = 0;
24714 for (i = first; i < end; ++i)
24716 int left, right;
24717 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
24718 if (x - left < 0)
24719 k = i;
24720 x += glyphs[i].pixel_width;
24723 return k;
24727 /* Set background width of glyph string S. START is the index of the
24728 first glyph following S. LAST_X is the right-most x-position + 1
24729 in the drawing area. */
24731 static void
24732 set_glyph_string_background_width (struct glyph_string *s, int start, int last_x)
24734 /* If the face of this glyph string has to be drawn to the end of
24735 the drawing area, set S->extends_to_end_of_line_p. */
24737 if (start == s->row->used[s->area]
24738 && ((s->row->fill_line_p
24739 && (s->hl == DRAW_NORMAL_TEXT
24740 || s->hl == DRAW_IMAGE_RAISED
24741 || s->hl == DRAW_IMAGE_SUNKEN))
24742 || s->hl == DRAW_MOUSE_FACE))
24743 s->extends_to_end_of_line_p = 1;
24745 /* If S extends its face to the end of the line, set its
24746 background_width to the distance to the right edge of the drawing
24747 area. */
24748 if (s->extends_to_end_of_line_p)
24749 s->background_width = last_x - s->x + 1;
24750 else
24751 s->background_width = s->width;
24755 /* Compute overhangs and x-positions for glyph string S and its
24756 predecessors, or successors. X is the starting x-position for S.
24757 BACKWARD_P non-zero means process predecessors. */
24759 static void
24760 compute_overhangs_and_x (struct glyph_string *s, int x, int backward_p)
24762 if (backward_p)
24764 while (s)
24766 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
24767 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
24768 x -= s->width;
24769 s->x = x;
24770 s = s->prev;
24773 else
24775 while (s)
24777 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
24778 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
24779 s->x = x;
24780 x += s->width;
24781 s = s->next;
24788 /* The following macros are only called from draw_glyphs below.
24789 They reference the following parameters of that function directly:
24790 `w', `row', `area', and `overlap_p'
24791 as well as the following local variables:
24792 `s', `f', and `hdc' (in W32) */
24794 #ifdef HAVE_NTGUI
24795 /* On W32, silently add local `hdc' variable to argument list of
24796 init_glyph_string. */
24797 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
24798 init_glyph_string (s, hdc, char2b, w, row, area, start, hl)
24799 #else
24800 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
24801 init_glyph_string (s, char2b, w, row, area, start, hl)
24802 #endif
24804 /* Add a glyph string for a stretch glyph to the list of strings
24805 between HEAD and TAIL. START is the index of the stretch glyph in
24806 row area AREA of glyph row ROW. END is the index of the last glyph
24807 in that glyph row area. X is the current output position assigned
24808 to the new glyph string constructed. HL overrides that face of the
24809 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
24810 is the right-most x-position of the drawing area. */
24812 /* SunOS 4 bundled cc, barfed on continuations in the arg lists here
24813 and below -- keep them on one line. */
24814 #define BUILD_STRETCH_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
24815 do \
24817 s = alloca (sizeof *s); \
24818 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
24819 START = fill_stretch_glyph_string (s, START, END); \
24820 append_glyph_string (&HEAD, &TAIL, s); \
24821 s->x = (X); \
24823 while (0)
24826 /* Add a glyph string for an image glyph to the list of strings
24827 between HEAD and TAIL. START is the index of the image glyph in
24828 row area AREA of glyph row ROW. END is the index of the last glyph
24829 in that glyph row area. X is the current output position assigned
24830 to the new glyph string constructed. HL overrides that face of the
24831 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
24832 is the right-most x-position of the drawing area. */
24834 #define BUILD_IMAGE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
24835 do \
24837 s = alloca (sizeof *s); \
24838 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
24839 fill_image_glyph_string (s); \
24840 append_glyph_string (&HEAD, &TAIL, s); \
24841 ++START; \
24842 s->x = (X); \
24844 while (0)
24847 /* Add a glyph string for a sequence of character glyphs to the list
24848 of strings between HEAD and TAIL. START is the index of the first
24849 glyph in row area AREA of glyph row ROW that is part of the new
24850 glyph string. END is the index of the last glyph in that glyph row
24851 area. X is the current output position assigned to the new glyph
24852 string constructed. HL overrides that face of the glyph; e.g. it
24853 is DRAW_CURSOR if a cursor has to be drawn. LAST_X is the
24854 right-most x-position of the drawing area. */
24856 #define BUILD_CHAR_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
24857 do \
24859 int face_id; \
24860 XChar2b *char2b; \
24862 face_id = (row)->glyphs[area][START].face_id; \
24864 s = alloca (sizeof *s); \
24865 SAFE_NALLOCA (char2b, 1, (END) - (START)); \
24866 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
24867 append_glyph_string (&HEAD, &TAIL, s); \
24868 s->x = (X); \
24869 START = fill_glyph_string (s, face_id, START, END, overlaps); \
24871 while (0)
24874 /* Add a glyph string for a composite sequence to the list of strings
24875 between HEAD and TAIL. START is the index of the first glyph in
24876 row area AREA of glyph row ROW that is part of the new glyph
24877 string. END is the index of the last glyph in that glyph row area.
24878 X is the current output position assigned to the new glyph string
24879 constructed. HL overrides that face of the glyph; e.g. it is
24880 DRAW_CURSOR if a cursor has to be drawn. LAST_X is the right-most
24881 x-position of the drawing area. */
24883 #define BUILD_COMPOSITE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
24884 do { \
24885 int face_id = (row)->glyphs[area][START].face_id; \
24886 struct face *base_face = FACE_FROM_ID (f, face_id); \
24887 ptrdiff_t cmp_id = (row)->glyphs[area][START].u.cmp.id; \
24888 struct composition *cmp = composition_table[cmp_id]; \
24889 XChar2b *char2b; \
24890 struct glyph_string *first_s = NULL; \
24891 int n; \
24893 SAFE_NALLOCA (char2b, 1, cmp->glyph_len); \
24895 /* Make glyph_strings for each glyph sequence that is drawable by \
24896 the same face, and append them to HEAD/TAIL. */ \
24897 for (n = 0; n < cmp->glyph_len;) \
24899 s = alloca (sizeof *s); \
24900 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
24901 append_glyph_string (&(HEAD), &(TAIL), s); \
24902 s->cmp = cmp; \
24903 s->cmp_from = n; \
24904 s->x = (X); \
24905 if (n == 0) \
24906 first_s = s; \
24907 n = fill_composite_glyph_string (s, base_face, overlaps); \
24910 ++START; \
24911 s = first_s; \
24912 } while (0)
24915 /* Add a glyph string for a glyph-string sequence to the list of strings
24916 between HEAD and TAIL. */
24918 #define BUILD_GSTRING_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
24919 do { \
24920 int face_id; \
24921 XChar2b *char2b; \
24922 Lisp_Object gstring; \
24924 face_id = (row)->glyphs[area][START].face_id; \
24925 gstring = (composition_gstring_from_id \
24926 ((row)->glyphs[area][START].u.cmp.id)); \
24927 s = alloca (sizeof *s); \
24928 SAFE_NALLOCA (char2b, 1, LGSTRING_GLYPH_LEN (gstring)); \
24929 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
24930 append_glyph_string (&(HEAD), &(TAIL), s); \
24931 s->x = (X); \
24932 START = fill_gstring_glyph_string (s, face_id, START, END, overlaps); \
24933 } while (0)
24936 /* Add a glyph string for a sequence of glyphless character's glyphs
24937 to the list of strings between HEAD and TAIL. The meanings of
24938 arguments are the same as those of BUILD_CHAR_GLYPH_STRINGS. */
24940 #define BUILD_GLYPHLESS_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
24941 do \
24943 int face_id; \
24945 face_id = (row)->glyphs[area][START].face_id; \
24947 s = alloca (sizeof *s); \
24948 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
24949 append_glyph_string (&HEAD, &TAIL, s); \
24950 s->x = (X); \
24951 START = fill_glyphless_glyph_string (s, face_id, START, END, \
24952 overlaps); \
24954 while (0)
24957 /* Build a list of glyph strings between HEAD and TAIL for the glyphs
24958 of AREA of glyph row ROW on window W between indices START and END.
24959 HL overrides the face for drawing glyph strings, e.g. it is
24960 DRAW_CURSOR to draw a cursor. X and LAST_X are start and end
24961 x-positions of the drawing area.
24963 This is an ugly monster macro construct because we must use alloca
24964 to allocate glyph strings (because draw_glyphs can be called
24965 asynchronously). */
24967 #define BUILD_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
24968 do \
24970 HEAD = TAIL = NULL; \
24971 while (START < END) \
24973 struct glyph *first_glyph = (row)->glyphs[area] + START; \
24974 switch (first_glyph->type) \
24976 case CHAR_GLYPH: \
24977 BUILD_CHAR_GLYPH_STRINGS (START, END, HEAD, TAIL, \
24978 HL, X, LAST_X); \
24979 break; \
24981 case COMPOSITE_GLYPH: \
24982 if (first_glyph->u.cmp.automatic) \
24983 BUILD_GSTRING_GLYPH_STRING (START, END, HEAD, TAIL, \
24984 HL, X, LAST_X); \
24985 else \
24986 BUILD_COMPOSITE_GLYPH_STRING (START, END, HEAD, TAIL, \
24987 HL, X, LAST_X); \
24988 break; \
24990 case STRETCH_GLYPH: \
24991 BUILD_STRETCH_GLYPH_STRING (START, END, HEAD, TAIL, \
24992 HL, X, LAST_X); \
24993 break; \
24995 case IMAGE_GLYPH: \
24996 BUILD_IMAGE_GLYPH_STRING (START, END, HEAD, TAIL, \
24997 HL, X, LAST_X); \
24998 break; \
25000 case GLYPHLESS_GLYPH: \
25001 BUILD_GLYPHLESS_GLYPH_STRING (START, END, HEAD, TAIL, \
25002 HL, X, LAST_X); \
25003 break; \
25005 default: \
25006 emacs_abort (); \
25009 if (s) \
25011 set_glyph_string_background_width (s, START, LAST_X); \
25012 (X) += s->width; \
25015 } while (0)
25018 /* Draw glyphs between START and END in AREA of ROW on window W,
25019 starting at x-position X. X is relative to AREA in W. HL is a
25020 face-override with the following meaning:
25022 DRAW_NORMAL_TEXT draw normally
25023 DRAW_CURSOR draw in cursor face
25024 DRAW_MOUSE_FACE draw in mouse face.
25025 DRAW_INVERSE_VIDEO draw in mode line face
25026 DRAW_IMAGE_SUNKEN draw an image with a sunken relief around it
25027 DRAW_IMAGE_RAISED draw an image with a raised relief around it
25029 If OVERLAPS is non-zero, draw only the foreground of characters and
25030 clip to the physical height of ROW. Non-zero value also defines
25031 the overlapping part to be drawn:
25033 OVERLAPS_PRED overlap with preceding rows
25034 OVERLAPS_SUCC overlap with succeeding rows
25035 OVERLAPS_BOTH overlap with both preceding/succeeding rows
25036 OVERLAPS_ERASED_CURSOR overlap with erased cursor area
25038 Value is the x-position reached, relative to AREA of W. */
25040 static int
25041 draw_glyphs (struct window *w, int x, struct glyph_row *row,
25042 enum glyph_row_area area, ptrdiff_t start, ptrdiff_t end,
25043 enum draw_glyphs_face hl, int overlaps)
25045 struct glyph_string *head, *tail;
25046 struct glyph_string *s;
25047 struct glyph_string *clip_head = NULL, *clip_tail = NULL;
25048 int i, j, x_reached, last_x, area_left = 0;
25049 struct frame *f = XFRAME (WINDOW_FRAME (w));
25050 DECLARE_HDC (hdc);
25052 ALLOCATE_HDC (hdc, f);
25054 /* Let's rather be paranoid than getting a SEGV. */
25055 end = min (end, row->used[area]);
25056 start = clip_to_bounds (0, start, end);
25058 /* Translate X to frame coordinates. Set last_x to the right
25059 end of the drawing area. */
25060 if (row->full_width_p)
25062 /* X is relative to the left edge of W, without scroll bars
25063 or fringes. */
25064 area_left = WINDOW_LEFT_EDGE_X (w);
25065 last_x = (WINDOW_LEFT_EDGE_X (w) + WINDOW_PIXEL_WIDTH (w)
25066 - (row->mode_line_p ? WINDOW_RIGHT_DIVIDER_WIDTH (w) : 0));
25068 else
25070 area_left = window_box_left (w, area);
25071 last_x = area_left + window_box_width (w, area);
25073 x += area_left;
25075 /* Build a doubly-linked list of glyph_string structures between
25076 head and tail from what we have to draw. Note that the macro
25077 BUILD_GLYPH_STRINGS will modify its start parameter. That's
25078 the reason we use a separate variable `i'. */
25079 i = start;
25080 USE_SAFE_ALLOCA;
25081 BUILD_GLYPH_STRINGS (i, end, head, tail, hl, x, last_x);
25082 if (tail)
25083 x_reached = tail->x + tail->background_width;
25084 else
25085 x_reached = x;
25087 /* If there are any glyphs with lbearing < 0 or rbearing > width in
25088 the row, redraw some glyphs in front or following the glyph
25089 strings built above. */
25090 if (head && !overlaps && row->contains_overlapping_glyphs_p)
25092 struct glyph_string *h, *t;
25093 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
25094 int mouse_beg_col IF_LINT (= 0), mouse_end_col IF_LINT (= 0);
25095 int check_mouse_face = 0;
25096 int dummy_x = 0;
25098 /* If mouse highlighting is on, we may need to draw adjacent
25099 glyphs using mouse-face highlighting. */
25100 if (area == TEXT_AREA && row->mouse_face_p
25101 && hlinfo->mouse_face_beg_row >= 0
25102 && hlinfo->mouse_face_end_row >= 0)
25104 ptrdiff_t row_vpos = MATRIX_ROW_VPOS (row, w->current_matrix);
25106 if (row_vpos >= hlinfo->mouse_face_beg_row
25107 && row_vpos <= hlinfo->mouse_face_end_row)
25109 check_mouse_face = 1;
25110 mouse_beg_col = (row_vpos == hlinfo->mouse_face_beg_row)
25111 ? hlinfo->mouse_face_beg_col : 0;
25112 mouse_end_col = (row_vpos == hlinfo->mouse_face_end_row)
25113 ? hlinfo->mouse_face_end_col
25114 : row->used[TEXT_AREA];
25118 /* Compute overhangs for all glyph strings. */
25119 if (FRAME_RIF (f)->compute_glyph_string_overhangs)
25120 for (s = head; s; s = s->next)
25121 FRAME_RIF (f)->compute_glyph_string_overhangs (s);
25123 /* Prepend glyph strings for glyphs in front of the first glyph
25124 string that are overwritten because of the first glyph
25125 string's left overhang. The background of all strings
25126 prepended must be drawn because the first glyph string
25127 draws over it. */
25128 i = left_overwritten (head);
25129 if (i >= 0)
25131 enum draw_glyphs_face overlap_hl;
25133 /* If this row contains mouse highlighting, attempt to draw
25134 the overlapped glyphs with the correct highlight. This
25135 code fails if the overlap encompasses more than one glyph
25136 and mouse-highlight spans only some of these glyphs.
25137 However, making it work perfectly involves a lot more
25138 code, and I don't know if the pathological case occurs in
25139 practice, so we'll stick to this for now. --- cyd */
25140 if (check_mouse_face
25141 && mouse_beg_col < start && mouse_end_col > i)
25142 overlap_hl = DRAW_MOUSE_FACE;
25143 else
25144 overlap_hl = DRAW_NORMAL_TEXT;
25146 if (hl != overlap_hl)
25147 clip_head = head;
25148 j = i;
25149 BUILD_GLYPH_STRINGS (j, start, h, t,
25150 overlap_hl, dummy_x, last_x);
25151 start = i;
25152 compute_overhangs_and_x (t, head->x, 1);
25153 prepend_glyph_string_lists (&head, &tail, h, t);
25154 if (clip_head == NULL)
25155 clip_head = head;
25158 /* Prepend glyph strings for glyphs in front of the first glyph
25159 string that overwrite that glyph string because of their
25160 right overhang. For these strings, only the foreground must
25161 be drawn, because it draws over the glyph string at `head'.
25162 The background must not be drawn because this would overwrite
25163 right overhangs of preceding glyphs for which no glyph
25164 strings exist. */
25165 i = left_overwriting (head);
25166 if (i >= 0)
25168 enum draw_glyphs_face overlap_hl;
25170 if (check_mouse_face
25171 && mouse_beg_col < start && mouse_end_col > i)
25172 overlap_hl = DRAW_MOUSE_FACE;
25173 else
25174 overlap_hl = DRAW_NORMAL_TEXT;
25176 if (hl == overlap_hl || clip_head == NULL)
25177 clip_head = head;
25178 BUILD_GLYPH_STRINGS (i, start, h, t,
25179 overlap_hl, dummy_x, last_x);
25180 for (s = h; s; s = s->next)
25181 s->background_filled_p = 1;
25182 compute_overhangs_and_x (t, head->x, 1);
25183 prepend_glyph_string_lists (&head, &tail, h, t);
25186 /* Append glyphs strings for glyphs following the last glyph
25187 string tail that are overwritten by tail. The background of
25188 these strings has to be drawn because tail's foreground draws
25189 over it. */
25190 i = right_overwritten (tail);
25191 if (i >= 0)
25193 enum draw_glyphs_face overlap_hl;
25195 if (check_mouse_face
25196 && mouse_beg_col < i && mouse_end_col > end)
25197 overlap_hl = DRAW_MOUSE_FACE;
25198 else
25199 overlap_hl = DRAW_NORMAL_TEXT;
25201 if (hl != overlap_hl)
25202 clip_tail = tail;
25203 BUILD_GLYPH_STRINGS (end, i, h, t,
25204 overlap_hl, x, last_x);
25205 /* Because BUILD_GLYPH_STRINGS updates the first argument,
25206 we don't have `end = i;' here. */
25207 compute_overhangs_and_x (h, tail->x + tail->width, 0);
25208 append_glyph_string_lists (&head, &tail, h, t);
25209 if (clip_tail == NULL)
25210 clip_tail = tail;
25213 /* Append glyph strings for glyphs following the last glyph
25214 string tail that overwrite tail. The foreground of such
25215 glyphs has to be drawn because it writes into the background
25216 of tail. The background must not be drawn because it could
25217 paint over the foreground of following glyphs. */
25218 i = right_overwriting (tail);
25219 if (i >= 0)
25221 enum draw_glyphs_face overlap_hl;
25222 if (check_mouse_face
25223 && mouse_beg_col < i && mouse_end_col > end)
25224 overlap_hl = DRAW_MOUSE_FACE;
25225 else
25226 overlap_hl = DRAW_NORMAL_TEXT;
25228 if (hl == overlap_hl || clip_tail == NULL)
25229 clip_tail = tail;
25230 i++; /* We must include the Ith glyph. */
25231 BUILD_GLYPH_STRINGS (end, i, h, t,
25232 overlap_hl, x, last_x);
25233 for (s = h; s; s = s->next)
25234 s->background_filled_p = 1;
25235 compute_overhangs_and_x (h, tail->x + tail->width, 0);
25236 append_glyph_string_lists (&head, &tail, h, t);
25238 if (clip_head || clip_tail)
25239 for (s = head; s; s = s->next)
25241 s->clip_head = clip_head;
25242 s->clip_tail = clip_tail;
25246 /* Draw all strings. */
25247 for (s = head; s; s = s->next)
25248 FRAME_RIF (f)->draw_glyph_string (s);
25250 #ifndef HAVE_NS
25251 /* When focus a sole frame and move horizontally, this sets on_p to 0
25252 causing a failure to erase prev cursor position. */
25253 if (area == TEXT_AREA
25254 && !row->full_width_p
25255 /* When drawing overlapping rows, only the glyph strings'
25256 foreground is drawn, which doesn't erase a cursor
25257 completely. */
25258 && !overlaps)
25260 int x0 = clip_head ? clip_head->x : (head ? head->x : x);
25261 int x1 = (clip_tail ? clip_tail->x + clip_tail->background_width
25262 : (tail ? tail->x + tail->background_width : x));
25263 x0 -= area_left;
25264 x1 -= area_left;
25266 notice_overwritten_cursor (w, TEXT_AREA, x0, x1,
25267 row->y, MATRIX_ROW_BOTTOM_Y (row));
25269 #endif
25271 /* Value is the x-position up to which drawn, relative to AREA of W.
25272 This doesn't include parts drawn because of overhangs. */
25273 if (row->full_width_p)
25274 x_reached = FRAME_TO_WINDOW_PIXEL_X (w, x_reached);
25275 else
25276 x_reached -= area_left;
25278 RELEASE_HDC (hdc, f);
25280 SAFE_FREE ();
25281 return x_reached;
25284 /* Expand row matrix if too narrow. Don't expand if area
25285 is not present. */
25287 #define IT_EXPAND_MATRIX_WIDTH(it, area) \
25289 if (!it->f->fonts_changed \
25290 && (it->glyph_row->glyphs[area] \
25291 < it->glyph_row->glyphs[area + 1])) \
25293 it->w->ncols_scale_factor++; \
25294 it->f->fonts_changed = 1; \
25298 /* Store one glyph for IT->char_to_display in IT->glyph_row.
25299 Called from x_produce_glyphs when IT->glyph_row is non-null. */
25301 static void
25302 append_glyph (struct it *it)
25304 struct glyph *glyph;
25305 enum glyph_row_area area = it->area;
25307 eassert (it->glyph_row);
25308 eassert (it->char_to_display != '\n' && it->char_to_display != '\t');
25310 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
25311 if (glyph < it->glyph_row->glyphs[area + 1])
25313 /* If the glyph row is reversed, we need to prepend the glyph
25314 rather than append it. */
25315 if (it->glyph_row->reversed_p && area == TEXT_AREA)
25317 struct glyph *g;
25319 /* Make room for the additional glyph. */
25320 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
25321 g[1] = *g;
25322 glyph = it->glyph_row->glyphs[area];
25324 glyph->charpos = CHARPOS (it->position);
25325 glyph->object = it->object;
25326 if (it->pixel_width > 0)
25328 glyph->pixel_width = it->pixel_width;
25329 glyph->padding_p = 0;
25331 else
25333 /* Assure at least 1-pixel width. Otherwise, cursor can't
25334 be displayed correctly. */
25335 glyph->pixel_width = 1;
25336 glyph->padding_p = 1;
25338 glyph->ascent = it->ascent;
25339 glyph->descent = it->descent;
25340 glyph->voffset = it->voffset;
25341 glyph->type = CHAR_GLYPH;
25342 glyph->avoid_cursor_p = it->avoid_cursor_p;
25343 glyph->multibyte_p = it->multibyte_p;
25344 if (it->glyph_row->reversed_p && area == TEXT_AREA)
25346 /* In R2L rows, the left and the right box edges need to be
25347 drawn in reverse direction. */
25348 glyph->right_box_line_p = it->start_of_box_run_p;
25349 glyph->left_box_line_p = it->end_of_box_run_p;
25351 else
25353 glyph->left_box_line_p = it->start_of_box_run_p;
25354 glyph->right_box_line_p = it->end_of_box_run_p;
25356 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
25357 || it->phys_descent > it->descent);
25358 glyph->glyph_not_available_p = it->glyph_not_available_p;
25359 glyph->face_id = it->face_id;
25360 glyph->u.ch = it->char_to_display;
25361 glyph->slice.img = null_glyph_slice;
25362 glyph->font_type = FONT_TYPE_UNKNOWN;
25363 if (it->bidi_p)
25365 glyph->resolved_level = it->bidi_it.resolved_level;
25366 eassert ((it->bidi_it.type & 7) == it->bidi_it.type);
25367 glyph->bidi_type = it->bidi_it.type;
25369 else
25371 glyph->resolved_level = 0;
25372 glyph->bidi_type = UNKNOWN_BT;
25374 ++it->glyph_row->used[area];
25376 else
25377 IT_EXPAND_MATRIX_WIDTH (it, area);
25380 /* Store one glyph for the composition IT->cmp_it.id in
25381 IT->glyph_row. Called from x_produce_glyphs when IT->glyph_row is
25382 non-null. */
25384 static void
25385 append_composite_glyph (struct it *it)
25387 struct glyph *glyph;
25388 enum glyph_row_area area = it->area;
25390 eassert (it->glyph_row);
25392 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
25393 if (glyph < it->glyph_row->glyphs[area + 1])
25395 /* If the glyph row is reversed, we need to prepend the glyph
25396 rather than append it. */
25397 if (it->glyph_row->reversed_p && it->area == TEXT_AREA)
25399 struct glyph *g;
25401 /* Make room for the new glyph. */
25402 for (g = glyph - 1; g >= it->glyph_row->glyphs[it->area]; g--)
25403 g[1] = *g;
25404 glyph = it->glyph_row->glyphs[it->area];
25406 glyph->charpos = it->cmp_it.charpos;
25407 glyph->object = it->object;
25408 glyph->pixel_width = it->pixel_width;
25409 glyph->ascent = it->ascent;
25410 glyph->descent = it->descent;
25411 glyph->voffset = it->voffset;
25412 glyph->type = COMPOSITE_GLYPH;
25413 if (it->cmp_it.ch < 0)
25415 glyph->u.cmp.automatic = 0;
25416 glyph->u.cmp.id = it->cmp_it.id;
25417 glyph->slice.cmp.from = glyph->slice.cmp.to = 0;
25419 else
25421 glyph->u.cmp.automatic = 1;
25422 glyph->u.cmp.id = it->cmp_it.id;
25423 glyph->slice.cmp.from = it->cmp_it.from;
25424 glyph->slice.cmp.to = it->cmp_it.to - 1;
25426 glyph->avoid_cursor_p = it->avoid_cursor_p;
25427 glyph->multibyte_p = it->multibyte_p;
25428 if (it->glyph_row->reversed_p && area == TEXT_AREA)
25430 /* In R2L rows, the left and the right box edges need to be
25431 drawn in reverse direction. */
25432 glyph->right_box_line_p = it->start_of_box_run_p;
25433 glyph->left_box_line_p = it->end_of_box_run_p;
25435 else
25437 glyph->left_box_line_p = it->start_of_box_run_p;
25438 glyph->right_box_line_p = it->end_of_box_run_p;
25440 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
25441 || it->phys_descent > it->descent);
25442 glyph->padding_p = 0;
25443 glyph->glyph_not_available_p = 0;
25444 glyph->face_id = it->face_id;
25445 glyph->font_type = FONT_TYPE_UNKNOWN;
25446 if (it->bidi_p)
25448 glyph->resolved_level = it->bidi_it.resolved_level;
25449 eassert ((it->bidi_it.type & 7) == it->bidi_it.type);
25450 glyph->bidi_type = it->bidi_it.type;
25452 ++it->glyph_row->used[area];
25454 else
25455 IT_EXPAND_MATRIX_WIDTH (it, area);
25459 /* Change IT->ascent and IT->height according to the setting of
25460 IT->voffset. */
25462 static void
25463 take_vertical_position_into_account (struct it *it)
25465 if (it->voffset)
25467 if (it->voffset < 0)
25468 /* Increase the ascent so that we can display the text higher
25469 in the line. */
25470 it->ascent -= it->voffset;
25471 else
25472 /* Increase the descent so that we can display the text lower
25473 in the line. */
25474 it->descent += it->voffset;
25479 /* Produce glyphs/get display metrics for the image IT is loaded with.
25480 See the description of struct display_iterator in dispextern.h for
25481 an overview of struct display_iterator. */
25483 static void
25484 produce_image_glyph (struct it *it)
25486 struct image *img;
25487 struct face *face;
25488 int glyph_ascent, crop;
25489 struct glyph_slice slice;
25491 eassert (it->what == IT_IMAGE);
25493 face = FACE_FROM_ID (it->f, it->face_id);
25494 eassert (face);
25495 /* Make sure X resources of the face is loaded. */
25496 prepare_face_for_display (it->f, face);
25498 if (it->image_id < 0)
25500 /* Fringe bitmap. */
25501 it->ascent = it->phys_ascent = 0;
25502 it->descent = it->phys_descent = 0;
25503 it->pixel_width = 0;
25504 it->nglyphs = 0;
25505 return;
25508 img = IMAGE_FROM_ID (it->f, it->image_id);
25509 eassert (img);
25510 /* Make sure X resources of the image is loaded. */
25511 prepare_image_for_display (it->f, img);
25513 slice.x = slice.y = 0;
25514 slice.width = img->width;
25515 slice.height = img->height;
25517 if (INTEGERP (it->slice.x))
25518 slice.x = XINT (it->slice.x);
25519 else if (FLOATP (it->slice.x))
25520 slice.x = XFLOAT_DATA (it->slice.x) * img->width;
25522 if (INTEGERP (it->slice.y))
25523 slice.y = XINT (it->slice.y);
25524 else if (FLOATP (it->slice.y))
25525 slice.y = XFLOAT_DATA (it->slice.y) * img->height;
25527 if (INTEGERP (it->slice.width))
25528 slice.width = XINT (it->slice.width);
25529 else if (FLOATP (it->slice.width))
25530 slice.width = XFLOAT_DATA (it->slice.width) * img->width;
25532 if (INTEGERP (it->slice.height))
25533 slice.height = XINT (it->slice.height);
25534 else if (FLOATP (it->slice.height))
25535 slice.height = XFLOAT_DATA (it->slice.height) * img->height;
25537 if (slice.x >= img->width)
25538 slice.x = img->width;
25539 if (slice.y >= img->height)
25540 slice.y = img->height;
25541 if (slice.x + slice.width >= img->width)
25542 slice.width = img->width - slice.x;
25543 if (slice.y + slice.height > img->height)
25544 slice.height = img->height - slice.y;
25546 if (slice.width == 0 || slice.height == 0)
25547 return;
25549 it->ascent = it->phys_ascent = glyph_ascent = image_ascent (img, face, &slice);
25551 it->descent = slice.height - glyph_ascent;
25552 if (slice.y == 0)
25553 it->descent += img->vmargin;
25554 if (slice.y + slice.height == img->height)
25555 it->descent += img->vmargin;
25556 it->phys_descent = it->descent;
25558 it->pixel_width = slice.width;
25559 if (slice.x == 0)
25560 it->pixel_width += img->hmargin;
25561 if (slice.x + slice.width == img->width)
25562 it->pixel_width += img->hmargin;
25564 /* It's quite possible for images to have an ascent greater than
25565 their height, so don't get confused in that case. */
25566 if (it->descent < 0)
25567 it->descent = 0;
25569 it->nglyphs = 1;
25571 if (face->box != FACE_NO_BOX)
25573 if (face->box_line_width > 0)
25575 if (slice.y == 0)
25576 it->ascent += face->box_line_width;
25577 if (slice.y + slice.height == img->height)
25578 it->descent += face->box_line_width;
25581 if (it->start_of_box_run_p && slice.x == 0)
25582 it->pixel_width += eabs (face->box_line_width);
25583 if (it->end_of_box_run_p && slice.x + slice.width == img->width)
25584 it->pixel_width += eabs (face->box_line_width);
25587 take_vertical_position_into_account (it);
25589 /* Automatically crop wide image glyphs at right edge so we can
25590 draw the cursor on same display row. */
25591 if ((crop = it->pixel_width - (it->last_visible_x - it->current_x), crop > 0)
25592 && (it->hpos == 0 || it->pixel_width > it->last_visible_x / 4))
25594 it->pixel_width -= crop;
25595 slice.width -= crop;
25598 if (it->glyph_row)
25600 struct glyph *glyph;
25601 enum glyph_row_area area = it->area;
25603 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
25604 if (it->glyph_row->reversed_p)
25606 struct glyph *g;
25608 /* Make room for the new glyph. */
25609 for (g = glyph - 1; g >= it->glyph_row->glyphs[it->area]; g--)
25610 g[1] = *g;
25611 glyph = it->glyph_row->glyphs[it->area];
25613 if (glyph < it->glyph_row->glyphs[area + 1])
25615 glyph->charpos = CHARPOS (it->position);
25616 glyph->object = it->object;
25617 glyph->pixel_width = it->pixel_width;
25618 glyph->ascent = glyph_ascent;
25619 glyph->descent = it->descent;
25620 glyph->voffset = it->voffset;
25621 glyph->type = IMAGE_GLYPH;
25622 glyph->avoid_cursor_p = it->avoid_cursor_p;
25623 glyph->multibyte_p = it->multibyte_p;
25624 if (it->glyph_row->reversed_p && area == TEXT_AREA)
25626 /* In R2L rows, the left and the right box edges need to be
25627 drawn in reverse direction. */
25628 glyph->right_box_line_p = it->start_of_box_run_p;
25629 glyph->left_box_line_p = it->end_of_box_run_p;
25631 else
25633 glyph->left_box_line_p = it->start_of_box_run_p;
25634 glyph->right_box_line_p = it->end_of_box_run_p;
25636 glyph->overlaps_vertically_p = 0;
25637 glyph->padding_p = 0;
25638 glyph->glyph_not_available_p = 0;
25639 glyph->face_id = it->face_id;
25640 glyph->u.img_id = img->id;
25641 glyph->slice.img = slice;
25642 glyph->font_type = FONT_TYPE_UNKNOWN;
25643 if (it->bidi_p)
25645 glyph->resolved_level = it->bidi_it.resolved_level;
25646 eassert ((it->bidi_it.type & 7) == it->bidi_it.type);
25647 glyph->bidi_type = it->bidi_it.type;
25649 ++it->glyph_row->used[area];
25651 else
25652 IT_EXPAND_MATRIX_WIDTH (it, area);
25657 /* Append a stretch glyph to IT->glyph_row. OBJECT is the source
25658 of the glyph, WIDTH and HEIGHT are the width and height of the
25659 stretch. ASCENT is the ascent of the glyph (0 <= ASCENT <= HEIGHT). */
25661 static void
25662 append_stretch_glyph (struct it *it, Lisp_Object object,
25663 int width, int height, int ascent)
25665 struct glyph *glyph;
25666 enum glyph_row_area area = it->area;
25668 eassert (ascent >= 0 && ascent <= height);
25670 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
25671 if (glyph < it->glyph_row->glyphs[area + 1])
25673 /* If the glyph row is reversed, we need to prepend the glyph
25674 rather than append it. */
25675 if (it->glyph_row->reversed_p && area == TEXT_AREA)
25677 struct glyph *g;
25679 /* Make room for the additional glyph. */
25680 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
25681 g[1] = *g;
25682 glyph = it->glyph_row->glyphs[area];
25684 /* Decrease the width of the first glyph of the row that
25685 begins before first_visible_x (e.g., due to hscroll).
25686 This is so the overall width of the row becomes smaller
25687 by the scroll amount, and the stretch glyph appended by
25688 extend_face_to_end_of_line will be wider, to shift the
25689 row glyphs to the right. (In L2R rows, the corresponding
25690 left-shift effect is accomplished by setting row->x to a
25691 negative value, which won't work with R2L rows.)
25693 This must leave us with a positive value of WIDTH, since
25694 otherwise the call to move_it_in_display_line_to at the
25695 beginning of display_line would have got past the entire
25696 first glyph, and then it->current_x would have been
25697 greater or equal to it->first_visible_x. */
25698 if (it->current_x < it->first_visible_x)
25699 width -= it->first_visible_x - it->current_x;
25700 eassert (width > 0);
25702 glyph->charpos = CHARPOS (it->position);
25703 glyph->object = object;
25704 glyph->pixel_width = width;
25705 glyph->ascent = ascent;
25706 glyph->descent = height - ascent;
25707 glyph->voffset = it->voffset;
25708 glyph->type = STRETCH_GLYPH;
25709 glyph->avoid_cursor_p = it->avoid_cursor_p;
25710 glyph->multibyte_p = it->multibyte_p;
25711 if (it->glyph_row->reversed_p && area == TEXT_AREA)
25713 /* In R2L rows, the left and the right box edges need to be
25714 drawn in reverse direction. */
25715 glyph->right_box_line_p = it->start_of_box_run_p;
25716 glyph->left_box_line_p = it->end_of_box_run_p;
25718 else
25720 glyph->left_box_line_p = it->start_of_box_run_p;
25721 glyph->right_box_line_p = it->end_of_box_run_p;
25723 glyph->overlaps_vertically_p = 0;
25724 glyph->padding_p = 0;
25725 glyph->glyph_not_available_p = 0;
25726 glyph->face_id = it->face_id;
25727 glyph->u.stretch.ascent = ascent;
25728 glyph->u.stretch.height = height;
25729 glyph->slice.img = null_glyph_slice;
25730 glyph->font_type = FONT_TYPE_UNKNOWN;
25731 if (it->bidi_p)
25733 glyph->resolved_level = it->bidi_it.resolved_level;
25734 eassert ((it->bidi_it.type & 7) == it->bidi_it.type);
25735 glyph->bidi_type = it->bidi_it.type;
25737 else
25739 glyph->resolved_level = 0;
25740 glyph->bidi_type = UNKNOWN_BT;
25742 ++it->glyph_row->used[area];
25744 else
25745 IT_EXPAND_MATRIX_WIDTH (it, area);
25748 #endif /* HAVE_WINDOW_SYSTEM */
25750 /* Produce a stretch glyph for iterator IT. IT->object is the value
25751 of the glyph property displayed. The value must be a list
25752 `(space KEYWORD VALUE ...)' with the following KEYWORD/VALUE pairs
25753 being recognized:
25755 1. `:width WIDTH' specifies that the space should be WIDTH *
25756 canonical char width wide. WIDTH may be an integer or floating
25757 point number.
25759 2. `:relative-width FACTOR' specifies that the width of the stretch
25760 should be computed from the width of the first character having the
25761 `glyph' property, and should be FACTOR times that width.
25763 3. `:align-to HPOS' specifies that the space should be wide enough
25764 to reach HPOS, a value in canonical character units.
25766 Exactly one of the above pairs must be present.
25768 4. `:height HEIGHT' specifies that the height of the stretch produced
25769 should be HEIGHT, measured in canonical character units.
25771 5. `:relative-height FACTOR' specifies that the height of the
25772 stretch should be FACTOR times the height of the characters having
25773 the glyph property.
25775 Either none or exactly one of 4 or 5 must be present.
25777 6. `:ascent ASCENT' specifies that ASCENT percent of the height
25778 of the stretch should be used for the ascent of the stretch.
25779 ASCENT must be in the range 0 <= ASCENT <= 100. */
25781 void
25782 produce_stretch_glyph (struct it *it)
25784 /* (space :width WIDTH :height HEIGHT ...) */
25785 Lisp_Object prop, plist;
25786 int width = 0, height = 0, align_to = -1;
25787 int zero_width_ok_p = 0;
25788 double tem;
25789 struct font *font = NULL;
25791 #ifdef HAVE_WINDOW_SYSTEM
25792 int ascent = 0;
25793 int zero_height_ok_p = 0;
25795 if (FRAME_WINDOW_P (it->f))
25797 struct face *face = FACE_FROM_ID (it->f, it->face_id);
25798 font = face->font ? face->font : FRAME_FONT (it->f);
25799 prepare_face_for_display (it->f, face);
25801 #endif
25803 /* List should start with `space'. */
25804 eassert (CONSP (it->object) && EQ (XCAR (it->object), Qspace));
25805 plist = XCDR (it->object);
25807 /* Compute the width of the stretch. */
25808 if ((prop = Fplist_get (plist, QCwidth), !NILP (prop))
25809 && calc_pixel_width_or_height (&tem, it, prop, font, 1, 0))
25811 /* Absolute width `:width WIDTH' specified and valid. */
25812 zero_width_ok_p = 1;
25813 width = (int)tem;
25815 #ifdef HAVE_WINDOW_SYSTEM
25816 else if (FRAME_WINDOW_P (it->f)
25817 && (prop = Fplist_get (plist, QCrelative_width), NUMVAL (prop) > 0))
25819 /* Relative width `:relative-width FACTOR' specified and valid.
25820 Compute the width of the characters having the `glyph'
25821 property. */
25822 struct it it2;
25823 unsigned char *p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
25825 it2 = *it;
25826 if (it->multibyte_p)
25827 it2.c = it2.char_to_display = STRING_CHAR_AND_LENGTH (p, it2.len);
25828 else
25830 it2.c = it2.char_to_display = *p, it2.len = 1;
25831 if (! ASCII_CHAR_P (it2.c))
25832 it2.char_to_display = BYTE8_TO_CHAR (it2.c);
25835 it2.glyph_row = NULL;
25836 it2.what = IT_CHARACTER;
25837 x_produce_glyphs (&it2);
25838 width = NUMVAL (prop) * it2.pixel_width;
25840 #endif /* HAVE_WINDOW_SYSTEM */
25841 else if ((prop = Fplist_get (plist, QCalign_to), !NILP (prop))
25842 && calc_pixel_width_or_height (&tem, it, prop, font, 1, &align_to))
25844 if (it->glyph_row == NULL || !it->glyph_row->mode_line_p)
25845 align_to = (align_to < 0
25847 : align_to - window_box_left_offset (it->w, TEXT_AREA));
25848 else if (align_to < 0)
25849 align_to = window_box_left_offset (it->w, TEXT_AREA);
25850 width = max (0, (int)tem + align_to - it->current_x);
25851 zero_width_ok_p = 1;
25853 else
25854 /* Nothing specified -> width defaults to canonical char width. */
25855 width = FRAME_COLUMN_WIDTH (it->f);
25857 if (width <= 0 && (width < 0 || !zero_width_ok_p))
25858 width = 1;
25860 #ifdef HAVE_WINDOW_SYSTEM
25861 /* Compute height. */
25862 if (FRAME_WINDOW_P (it->f))
25864 if ((prop = Fplist_get (plist, QCheight), !NILP (prop))
25865 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
25867 height = (int)tem;
25868 zero_height_ok_p = 1;
25870 else if (prop = Fplist_get (plist, QCrelative_height),
25871 NUMVAL (prop) > 0)
25872 height = FONT_HEIGHT (font) * NUMVAL (prop);
25873 else
25874 height = FONT_HEIGHT (font);
25876 if (height <= 0 && (height < 0 || !zero_height_ok_p))
25877 height = 1;
25879 /* Compute percentage of height used for ascent. If
25880 `:ascent ASCENT' is present and valid, use that. Otherwise,
25881 derive the ascent from the font in use. */
25882 if (prop = Fplist_get (plist, QCascent),
25883 NUMVAL (prop) > 0 && NUMVAL (prop) <= 100)
25884 ascent = height * NUMVAL (prop) / 100.0;
25885 else if (!NILP (prop)
25886 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
25887 ascent = min (max (0, (int)tem), height);
25888 else
25889 ascent = (height * FONT_BASE (font)) / FONT_HEIGHT (font);
25891 else
25892 #endif /* HAVE_WINDOW_SYSTEM */
25893 height = 1;
25895 if (width > 0 && it->line_wrap != TRUNCATE
25896 && it->current_x + width > it->last_visible_x)
25898 width = it->last_visible_x - it->current_x;
25899 #ifdef HAVE_WINDOW_SYSTEM
25900 /* Subtract one more pixel from the stretch width, but only on
25901 GUI frames, since on a TTY each glyph is one "pixel" wide. */
25902 width -= FRAME_WINDOW_P (it->f);
25903 #endif
25906 if (width > 0 && height > 0 && it->glyph_row)
25908 Lisp_Object o_object = it->object;
25909 Lisp_Object object = it->stack[it->sp - 1].string;
25910 int n = width;
25912 if (!STRINGP (object))
25913 object = it->w->contents;
25914 #ifdef HAVE_WINDOW_SYSTEM
25915 if (FRAME_WINDOW_P (it->f))
25916 append_stretch_glyph (it, object, width, height, ascent);
25917 else
25918 #endif
25920 it->object = object;
25921 it->char_to_display = ' ';
25922 it->pixel_width = it->len = 1;
25923 while (n--)
25924 tty_append_glyph (it);
25925 it->object = o_object;
25929 it->pixel_width = width;
25930 #ifdef HAVE_WINDOW_SYSTEM
25931 if (FRAME_WINDOW_P (it->f))
25933 it->ascent = it->phys_ascent = ascent;
25934 it->descent = it->phys_descent = height - it->ascent;
25935 it->nglyphs = width > 0 && height > 0 ? 1 : 0;
25936 take_vertical_position_into_account (it);
25938 else
25939 #endif
25940 it->nglyphs = width;
25943 /* Get information about special display element WHAT in an
25944 environment described by IT. WHAT is one of IT_TRUNCATION or
25945 IT_CONTINUATION. Maybe produce glyphs for WHAT if IT has a
25946 non-null glyph_row member. This function ensures that fields like
25947 face_id, c, len of IT are left untouched. */
25949 static void
25950 produce_special_glyphs (struct it *it, enum display_element_type what)
25952 struct it temp_it;
25953 Lisp_Object gc;
25954 GLYPH glyph;
25956 temp_it = *it;
25957 temp_it.object = Qnil;
25958 memset (&temp_it.current, 0, sizeof temp_it.current);
25960 if (what == IT_CONTINUATION)
25962 /* Continuation glyph. For R2L lines, we mirror it by hand. */
25963 if (it->bidi_it.paragraph_dir == R2L)
25964 SET_GLYPH_FROM_CHAR (glyph, '/');
25965 else
25966 SET_GLYPH_FROM_CHAR (glyph, '\\');
25967 if (it->dp
25968 && (gc = DISP_CONTINUE_GLYPH (it->dp), GLYPH_CODE_P (gc)))
25970 /* FIXME: Should we mirror GC for R2L lines? */
25971 SET_GLYPH_FROM_GLYPH_CODE (glyph, gc);
25972 spec_glyph_lookup_face (XWINDOW (it->window), &glyph);
25975 else if (what == IT_TRUNCATION)
25977 /* Truncation glyph. */
25978 SET_GLYPH_FROM_CHAR (glyph, '$');
25979 if (it->dp
25980 && (gc = DISP_TRUNC_GLYPH (it->dp), GLYPH_CODE_P (gc)))
25982 /* FIXME: Should we mirror GC for R2L lines? */
25983 SET_GLYPH_FROM_GLYPH_CODE (glyph, gc);
25984 spec_glyph_lookup_face (XWINDOW (it->window), &glyph);
25987 else
25988 emacs_abort ();
25990 #ifdef HAVE_WINDOW_SYSTEM
25991 /* On a GUI frame, when the right fringe (left fringe for R2L rows)
25992 is turned off, we precede the truncation/continuation glyphs by a
25993 stretch glyph whose width is computed such that these special
25994 glyphs are aligned at the window margin, even when very different
25995 fonts are used in different glyph rows. */
25996 if (FRAME_WINDOW_P (temp_it.f)
25997 /* init_iterator calls this with it->glyph_row == NULL, and it
25998 wants only the pixel width of the truncation/continuation
25999 glyphs. */
26000 && temp_it.glyph_row
26001 /* insert_left_trunc_glyphs calls us at the beginning of the
26002 row, and it has its own calculation of the stretch glyph
26003 width. */
26004 && temp_it.glyph_row->used[TEXT_AREA] > 0
26005 && (temp_it.glyph_row->reversed_p
26006 ? WINDOW_LEFT_FRINGE_WIDTH (temp_it.w)
26007 : WINDOW_RIGHT_FRINGE_WIDTH (temp_it.w)) == 0)
26009 int stretch_width = temp_it.last_visible_x - temp_it.current_x;
26011 if (stretch_width > 0)
26013 struct face *face = FACE_FROM_ID (temp_it.f, temp_it.face_id);
26014 struct font *font =
26015 face->font ? face->font : FRAME_FONT (temp_it.f);
26016 int stretch_ascent =
26017 (((temp_it.ascent + temp_it.descent)
26018 * FONT_BASE (font)) / FONT_HEIGHT (font));
26020 append_stretch_glyph (&temp_it, Qnil, stretch_width,
26021 temp_it.ascent + temp_it.descent,
26022 stretch_ascent);
26025 #endif
26027 temp_it.dp = NULL;
26028 temp_it.what = IT_CHARACTER;
26029 temp_it.c = temp_it.char_to_display = GLYPH_CHAR (glyph);
26030 temp_it.face_id = GLYPH_FACE (glyph);
26031 temp_it.len = CHAR_BYTES (temp_it.c);
26033 PRODUCE_GLYPHS (&temp_it);
26034 it->pixel_width = temp_it.pixel_width;
26035 it->nglyphs = temp_it.nglyphs;
26038 #ifdef HAVE_WINDOW_SYSTEM
26040 /* Calculate line-height and line-spacing properties.
26041 An integer value specifies explicit pixel value.
26042 A float value specifies relative value to current face height.
26043 A cons (float . face-name) specifies relative value to
26044 height of specified face font.
26046 Returns height in pixels, or nil. */
26049 static Lisp_Object
26050 calc_line_height_property (struct it *it, Lisp_Object val, struct font *font,
26051 int boff, int override)
26053 Lisp_Object face_name = Qnil;
26054 int ascent, descent, height;
26056 if (NILP (val) || INTEGERP (val) || (override && EQ (val, Qt)))
26057 return val;
26059 if (CONSP (val))
26061 face_name = XCAR (val);
26062 val = XCDR (val);
26063 if (!NUMBERP (val))
26064 val = make_number (1);
26065 if (NILP (face_name))
26067 height = it->ascent + it->descent;
26068 goto scale;
26072 if (NILP (face_name))
26074 font = FRAME_FONT (it->f);
26075 boff = FRAME_BASELINE_OFFSET (it->f);
26077 else if (EQ (face_name, Qt))
26079 override = 0;
26081 else
26083 int face_id;
26084 struct face *face;
26086 face_id = lookup_named_face (it->f, face_name, false);
26087 if (face_id < 0)
26088 return make_number (-1);
26090 face = FACE_FROM_ID (it->f, face_id);
26091 font = face->font;
26092 if (font == NULL)
26093 return make_number (-1);
26094 boff = font->baseline_offset;
26095 if (font->vertical_centering)
26096 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
26099 ascent = FONT_BASE (font) + boff;
26100 descent = FONT_DESCENT (font) - boff;
26102 if (override)
26104 it->override_ascent = ascent;
26105 it->override_descent = descent;
26106 it->override_boff = boff;
26109 height = ascent + descent;
26111 scale:
26112 if (FLOATP (val))
26113 height = (int)(XFLOAT_DATA (val) * height);
26114 else if (INTEGERP (val))
26115 height *= XINT (val);
26117 return make_number (height);
26121 /* Append a glyph for a glyphless character to IT->glyph_row. FACE_ID
26122 is a face ID to be used for the glyph. FOR_NO_FONT is nonzero if
26123 and only if this is for a character for which no font was found.
26125 If the display method (it->glyphless_method) is
26126 GLYPHLESS_DISPLAY_ACRONYM or GLYPHLESS_DISPLAY_HEX_CODE, LEN is a
26127 length of the acronym or the hexadecimal string, UPPER_XOFF and
26128 UPPER_YOFF are pixel offsets for the upper part of the string,
26129 LOWER_XOFF and LOWER_YOFF are for the lower part.
26131 For the other display methods, LEN through LOWER_YOFF are zero. */
26133 static void
26134 append_glyphless_glyph (struct it *it, int face_id, int for_no_font, int len,
26135 short upper_xoff, short upper_yoff,
26136 short lower_xoff, short lower_yoff)
26138 struct glyph *glyph;
26139 enum glyph_row_area area = it->area;
26141 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
26142 if (glyph < it->glyph_row->glyphs[area + 1])
26144 /* If the glyph row is reversed, we need to prepend the glyph
26145 rather than append it. */
26146 if (it->glyph_row->reversed_p && area == TEXT_AREA)
26148 struct glyph *g;
26150 /* Make room for the additional glyph. */
26151 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
26152 g[1] = *g;
26153 glyph = it->glyph_row->glyphs[area];
26155 glyph->charpos = CHARPOS (it->position);
26156 glyph->object = it->object;
26157 glyph->pixel_width = it->pixel_width;
26158 glyph->ascent = it->ascent;
26159 glyph->descent = it->descent;
26160 glyph->voffset = it->voffset;
26161 glyph->type = GLYPHLESS_GLYPH;
26162 glyph->u.glyphless.method = it->glyphless_method;
26163 glyph->u.glyphless.for_no_font = for_no_font;
26164 glyph->u.glyphless.len = len;
26165 glyph->u.glyphless.ch = it->c;
26166 glyph->slice.glyphless.upper_xoff = upper_xoff;
26167 glyph->slice.glyphless.upper_yoff = upper_yoff;
26168 glyph->slice.glyphless.lower_xoff = lower_xoff;
26169 glyph->slice.glyphless.lower_yoff = lower_yoff;
26170 glyph->avoid_cursor_p = it->avoid_cursor_p;
26171 glyph->multibyte_p = it->multibyte_p;
26172 if (it->glyph_row->reversed_p && area == TEXT_AREA)
26174 /* In R2L rows, the left and the right box edges need to be
26175 drawn in reverse direction. */
26176 glyph->right_box_line_p = it->start_of_box_run_p;
26177 glyph->left_box_line_p = it->end_of_box_run_p;
26179 else
26181 glyph->left_box_line_p = it->start_of_box_run_p;
26182 glyph->right_box_line_p = it->end_of_box_run_p;
26184 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
26185 || it->phys_descent > it->descent);
26186 glyph->padding_p = 0;
26187 glyph->glyph_not_available_p = 0;
26188 glyph->face_id = face_id;
26189 glyph->font_type = FONT_TYPE_UNKNOWN;
26190 if (it->bidi_p)
26192 glyph->resolved_level = it->bidi_it.resolved_level;
26193 eassert ((it->bidi_it.type & 7) == it->bidi_it.type);
26194 glyph->bidi_type = it->bidi_it.type;
26196 ++it->glyph_row->used[area];
26198 else
26199 IT_EXPAND_MATRIX_WIDTH (it, area);
26203 /* Produce a glyph for a glyphless character for iterator IT.
26204 IT->glyphless_method specifies which method to use for displaying
26205 the character. See the description of enum
26206 glyphless_display_method in dispextern.h for the detail.
26208 FOR_NO_FONT is nonzero if and only if this is for a character for
26209 which no font was found. ACRONYM, if non-nil, is an acronym string
26210 for the character. */
26212 static void
26213 produce_glyphless_glyph (struct it *it, int for_no_font, Lisp_Object acronym)
26215 int face_id;
26216 struct face *face;
26217 struct font *font;
26218 int base_width, base_height, width, height;
26219 short upper_xoff, upper_yoff, lower_xoff, lower_yoff;
26220 int len;
26222 /* Get the metrics of the base font. We always refer to the current
26223 ASCII face. */
26224 face = FACE_FROM_ID (it->f, it->face_id)->ascii_face;
26225 font = face->font ? face->font : FRAME_FONT (it->f);
26226 it->ascent = FONT_BASE (font) + font->baseline_offset;
26227 it->descent = FONT_DESCENT (font) - font->baseline_offset;
26228 base_height = it->ascent + it->descent;
26229 base_width = font->average_width;
26231 face_id = merge_glyphless_glyph_face (it);
26233 if (it->glyphless_method == GLYPHLESS_DISPLAY_THIN_SPACE)
26235 it->pixel_width = THIN_SPACE_WIDTH;
26236 len = 0;
26237 upper_xoff = upper_yoff = lower_xoff = lower_yoff = 0;
26239 else if (it->glyphless_method == GLYPHLESS_DISPLAY_EMPTY_BOX)
26241 width = CHAR_WIDTH (it->c);
26242 if (width == 0)
26243 width = 1;
26244 else if (width > 4)
26245 width = 4;
26246 it->pixel_width = base_width * width;
26247 len = 0;
26248 upper_xoff = upper_yoff = lower_xoff = lower_yoff = 0;
26250 else
26252 char buf[7];
26253 const char *str;
26254 unsigned int code[6];
26255 int upper_len;
26256 int ascent, descent;
26257 struct font_metrics metrics_upper, metrics_lower;
26259 face = FACE_FROM_ID (it->f, face_id);
26260 font = face->font ? face->font : FRAME_FONT (it->f);
26261 prepare_face_for_display (it->f, face);
26263 if (it->glyphless_method == GLYPHLESS_DISPLAY_ACRONYM)
26265 if (! STRINGP (acronym) && CHAR_TABLE_P (Vglyphless_char_display))
26266 acronym = CHAR_TABLE_REF (Vglyphless_char_display, it->c);
26267 if (CONSP (acronym))
26268 acronym = XCAR (acronym);
26269 str = STRINGP (acronym) ? SSDATA (acronym) : "";
26271 else
26273 eassert (it->glyphless_method == GLYPHLESS_DISPLAY_HEX_CODE);
26274 sprintf (buf, "%0*X", it->c < 0x10000 ? 4 : 6, it->c);
26275 str = buf;
26277 for (len = 0; str[len] && ASCII_CHAR_P (str[len]) && len < 6; len++)
26278 code[len] = font->driver->encode_char (font, str[len]);
26279 upper_len = (len + 1) / 2;
26280 font->driver->text_extents (font, code, upper_len,
26281 &metrics_upper);
26282 font->driver->text_extents (font, code + upper_len, len - upper_len,
26283 &metrics_lower);
26287 /* +4 is for vertical bars of a box plus 1-pixel spaces at both side. */
26288 width = max (metrics_upper.width, metrics_lower.width) + 4;
26289 upper_xoff = upper_yoff = 2; /* the typical case */
26290 if (base_width >= width)
26292 /* Align the upper to the left, the lower to the right. */
26293 it->pixel_width = base_width;
26294 lower_xoff = base_width - 2 - metrics_lower.width;
26296 else
26298 /* Center the shorter one. */
26299 it->pixel_width = width;
26300 if (metrics_upper.width >= metrics_lower.width)
26301 lower_xoff = (width - metrics_lower.width) / 2;
26302 else
26304 /* FIXME: This code doesn't look right. It formerly was
26305 missing the "lower_xoff = 0;", which couldn't have
26306 been right since it left lower_xoff uninitialized. */
26307 lower_xoff = 0;
26308 upper_xoff = (width - metrics_upper.width) / 2;
26312 /* +5 is for horizontal bars of a box plus 1-pixel spaces at
26313 top, bottom, and between upper and lower strings. */
26314 height = (metrics_upper.ascent + metrics_upper.descent
26315 + metrics_lower.ascent + metrics_lower.descent) + 5;
26316 /* Center vertically.
26317 H:base_height, D:base_descent
26318 h:height, ld:lower_descent, la:lower_ascent, ud:upper_descent
26320 ascent = - (D - H/2 - h/2 + 1); "+ 1" for rounding up
26321 descent = D - H/2 + h/2;
26322 lower_yoff = descent - 2 - ld;
26323 upper_yoff = lower_yoff - la - 1 - ud; */
26324 ascent = - (it->descent - (base_height + height + 1) / 2);
26325 descent = it->descent - (base_height - height) / 2;
26326 lower_yoff = descent - 2 - metrics_lower.descent;
26327 upper_yoff = (lower_yoff - metrics_lower.ascent - 1
26328 - metrics_upper.descent);
26329 /* Don't make the height shorter than the base height. */
26330 if (height > base_height)
26332 it->ascent = ascent;
26333 it->descent = descent;
26337 it->phys_ascent = it->ascent;
26338 it->phys_descent = it->descent;
26339 if (it->glyph_row)
26340 append_glyphless_glyph (it, face_id, for_no_font, len,
26341 upper_xoff, upper_yoff,
26342 lower_xoff, lower_yoff);
26343 it->nglyphs = 1;
26344 take_vertical_position_into_account (it);
26348 /* RIF:
26349 Produce glyphs/get display metrics for the display element IT is
26350 loaded with. See the description of struct it in dispextern.h
26351 for an overview of struct it. */
26353 void
26354 x_produce_glyphs (struct it *it)
26356 int extra_line_spacing = it->extra_line_spacing;
26358 it->glyph_not_available_p = 0;
26360 if (it->what == IT_CHARACTER)
26362 XChar2b char2b;
26363 struct face *face = FACE_FROM_ID (it->f, it->face_id);
26364 struct font *font = face->font;
26365 struct font_metrics *pcm = NULL;
26366 int boff; /* Baseline offset. */
26368 if (font == NULL)
26370 /* When no suitable font is found, display this character by
26371 the method specified in the first extra slot of
26372 Vglyphless_char_display. */
26373 Lisp_Object acronym = lookup_glyphless_char_display (-1, it);
26375 eassert (it->what == IT_GLYPHLESS);
26376 produce_glyphless_glyph (it, 1, STRINGP (acronym) ? acronym : Qnil);
26377 goto done;
26380 boff = font->baseline_offset;
26381 if (font->vertical_centering)
26382 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
26384 if (it->char_to_display != '\n' && it->char_to_display != '\t')
26386 int stretched_p;
26388 it->nglyphs = 1;
26390 if (it->override_ascent >= 0)
26392 it->ascent = it->override_ascent;
26393 it->descent = it->override_descent;
26394 boff = it->override_boff;
26396 else
26398 it->ascent = FONT_BASE (font) + boff;
26399 it->descent = FONT_DESCENT (font) - boff;
26402 if (get_char_glyph_code (it->char_to_display, font, &char2b))
26404 pcm = get_per_char_metric (font, &char2b);
26405 if (pcm->width == 0
26406 && pcm->rbearing == 0 && pcm->lbearing == 0)
26407 pcm = NULL;
26410 if (pcm)
26412 it->phys_ascent = pcm->ascent + boff;
26413 it->phys_descent = pcm->descent - boff;
26414 it->pixel_width = pcm->width;
26416 else
26418 it->glyph_not_available_p = 1;
26419 it->phys_ascent = it->ascent;
26420 it->phys_descent = it->descent;
26421 it->pixel_width = font->space_width;
26424 if (it->constrain_row_ascent_descent_p)
26426 if (it->descent > it->max_descent)
26428 it->ascent += it->descent - it->max_descent;
26429 it->descent = it->max_descent;
26431 if (it->ascent > it->max_ascent)
26433 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
26434 it->ascent = it->max_ascent;
26436 it->phys_ascent = min (it->phys_ascent, it->ascent);
26437 it->phys_descent = min (it->phys_descent, it->descent);
26438 extra_line_spacing = 0;
26441 /* If this is a space inside a region of text with
26442 `space-width' property, change its width. */
26443 stretched_p = it->char_to_display == ' ' && !NILP (it->space_width);
26444 if (stretched_p)
26445 it->pixel_width *= XFLOATINT (it->space_width);
26447 /* If face has a box, add the box thickness to the character
26448 height. If character has a box line to the left and/or
26449 right, add the box line width to the character's width. */
26450 if (face->box != FACE_NO_BOX)
26452 int thick = face->box_line_width;
26454 if (thick > 0)
26456 it->ascent += thick;
26457 it->descent += thick;
26459 else
26460 thick = -thick;
26462 if (it->start_of_box_run_p)
26463 it->pixel_width += thick;
26464 if (it->end_of_box_run_p)
26465 it->pixel_width += thick;
26468 /* If face has an overline, add the height of the overline
26469 (1 pixel) and a 1 pixel margin to the character height. */
26470 if (face->overline_p)
26471 it->ascent += overline_margin;
26473 if (it->constrain_row_ascent_descent_p)
26475 if (it->ascent > it->max_ascent)
26476 it->ascent = it->max_ascent;
26477 if (it->descent > it->max_descent)
26478 it->descent = it->max_descent;
26481 take_vertical_position_into_account (it);
26483 /* If we have to actually produce glyphs, do it. */
26484 if (it->glyph_row)
26486 if (stretched_p)
26488 /* Translate a space with a `space-width' property
26489 into a stretch glyph. */
26490 int ascent = (((it->ascent + it->descent) * FONT_BASE (font))
26491 / FONT_HEIGHT (font));
26492 append_stretch_glyph (it, it->object, it->pixel_width,
26493 it->ascent + it->descent, ascent);
26495 else
26496 append_glyph (it);
26498 /* If characters with lbearing or rbearing are displayed
26499 in this line, record that fact in a flag of the
26500 glyph row. This is used to optimize X output code. */
26501 if (pcm && (pcm->lbearing < 0 || pcm->rbearing > pcm->width))
26502 it->glyph_row->contains_overlapping_glyphs_p = 1;
26504 if (! stretched_p && it->pixel_width == 0)
26505 /* We assure that all visible glyphs have at least 1-pixel
26506 width. */
26507 it->pixel_width = 1;
26509 else if (it->char_to_display == '\n')
26511 /* A newline has no width, but we need the height of the
26512 line. But if previous part of the line sets a height,
26513 don't increase that height. */
26515 Lisp_Object height;
26516 Lisp_Object total_height = Qnil;
26518 it->override_ascent = -1;
26519 it->pixel_width = 0;
26520 it->nglyphs = 0;
26522 height = get_it_property (it, Qline_height);
26523 /* Split (line-height total-height) list. */
26524 if (CONSP (height)
26525 && CONSP (XCDR (height))
26526 && NILP (XCDR (XCDR (height))))
26528 total_height = XCAR (XCDR (height));
26529 height = XCAR (height);
26531 height = calc_line_height_property (it, height, font, boff, 1);
26533 if (it->override_ascent >= 0)
26535 it->ascent = it->override_ascent;
26536 it->descent = it->override_descent;
26537 boff = it->override_boff;
26539 else
26541 it->ascent = FONT_BASE (font) + boff;
26542 it->descent = FONT_DESCENT (font) - boff;
26545 if (EQ (height, Qt))
26547 if (it->descent > it->max_descent)
26549 it->ascent += it->descent - it->max_descent;
26550 it->descent = it->max_descent;
26552 if (it->ascent > it->max_ascent)
26554 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
26555 it->ascent = it->max_ascent;
26557 it->phys_ascent = min (it->phys_ascent, it->ascent);
26558 it->phys_descent = min (it->phys_descent, it->descent);
26559 it->constrain_row_ascent_descent_p = 1;
26560 extra_line_spacing = 0;
26562 else
26564 Lisp_Object spacing;
26566 it->phys_ascent = it->ascent;
26567 it->phys_descent = it->descent;
26569 if ((it->max_ascent > 0 || it->max_descent > 0)
26570 && face->box != FACE_NO_BOX
26571 && face->box_line_width > 0)
26573 it->ascent += face->box_line_width;
26574 it->descent += face->box_line_width;
26576 if (!NILP (height)
26577 && XINT (height) > it->ascent + it->descent)
26578 it->ascent = XINT (height) - it->descent;
26580 if (!NILP (total_height))
26581 spacing = calc_line_height_property (it, total_height, font, boff, 0);
26582 else
26584 spacing = get_it_property (it, Qline_spacing);
26585 spacing = calc_line_height_property (it, spacing, font, boff, 0);
26587 if (INTEGERP (spacing))
26589 extra_line_spacing = XINT (spacing);
26590 if (!NILP (total_height))
26591 extra_line_spacing -= (it->phys_ascent + it->phys_descent);
26595 else /* i.e. (it->char_to_display == '\t') */
26597 if (font->space_width > 0)
26599 int tab_width = it->tab_width * font->space_width;
26600 int x = it->current_x + it->continuation_lines_width;
26601 int next_tab_x = ((1 + x + tab_width - 1) / tab_width) * tab_width;
26603 /* If the distance from the current position to the next tab
26604 stop is less than a space character width, use the
26605 tab stop after that. */
26606 if (next_tab_x - x < font->space_width)
26607 next_tab_x += tab_width;
26609 it->pixel_width = next_tab_x - x;
26610 it->nglyphs = 1;
26611 it->ascent = it->phys_ascent = FONT_BASE (font) + boff;
26612 it->descent = it->phys_descent = FONT_DESCENT (font) - boff;
26614 if (it->glyph_row)
26616 append_stretch_glyph (it, it->object, it->pixel_width,
26617 it->ascent + it->descent, it->ascent);
26620 else
26622 it->pixel_width = 0;
26623 it->nglyphs = 1;
26627 else if (it->what == IT_COMPOSITION && it->cmp_it.ch < 0)
26629 /* A static composition.
26631 Note: A composition is represented as one glyph in the
26632 glyph matrix. There are no padding glyphs.
26634 Important note: pixel_width, ascent, and descent are the
26635 values of what is drawn by draw_glyphs (i.e. the values of
26636 the overall glyphs composed). */
26637 struct face *face = FACE_FROM_ID (it->f, it->face_id);
26638 int boff; /* baseline offset */
26639 struct composition *cmp = composition_table[it->cmp_it.id];
26640 int glyph_len = cmp->glyph_len;
26641 struct font *font = face->font;
26643 it->nglyphs = 1;
26645 /* If we have not yet calculated pixel size data of glyphs of
26646 the composition for the current face font, calculate them
26647 now. Theoretically, we have to check all fonts for the
26648 glyphs, but that requires much time and memory space. So,
26649 here we check only the font of the first glyph. This may
26650 lead to incorrect display, but it's very rare, and C-l
26651 (recenter-top-bottom) can correct the display anyway. */
26652 if (! cmp->font || cmp->font != font)
26654 /* Ascent and descent of the font of the first character
26655 of this composition (adjusted by baseline offset).
26656 Ascent and descent of overall glyphs should not be less
26657 than these, respectively. */
26658 int font_ascent, font_descent, font_height;
26659 /* Bounding box of the overall glyphs. */
26660 int leftmost, rightmost, lowest, highest;
26661 int lbearing, rbearing;
26662 int i, width, ascent, descent;
26663 int left_padded = 0, right_padded = 0;
26664 int c IF_LINT (= 0); /* cmp->glyph_len can't be zero; see Bug#8512 */
26665 XChar2b char2b;
26666 struct font_metrics *pcm;
26667 int font_not_found_p;
26668 ptrdiff_t pos;
26670 for (glyph_len = cmp->glyph_len; glyph_len > 0; glyph_len--)
26671 if ((c = COMPOSITION_GLYPH (cmp, glyph_len - 1)) != '\t')
26672 break;
26673 if (glyph_len < cmp->glyph_len)
26674 right_padded = 1;
26675 for (i = 0; i < glyph_len; i++)
26677 if ((c = COMPOSITION_GLYPH (cmp, i)) != '\t')
26678 break;
26679 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
26681 if (i > 0)
26682 left_padded = 1;
26684 pos = (STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
26685 : IT_CHARPOS (*it));
26686 /* If no suitable font is found, use the default font. */
26687 font_not_found_p = font == NULL;
26688 if (font_not_found_p)
26690 face = face->ascii_face;
26691 font = face->font;
26693 boff = font->baseline_offset;
26694 if (font->vertical_centering)
26695 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
26696 font_ascent = FONT_BASE (font) + boff;
26697 font_descent = FONT_DESCENT (font) - boff;
26698 font_height = FONT_HEIGHT (font);
26700 cmp->font = font;
26702 pcm = NULL;
26703 if (! font_not_found_p)
26705 get_char_face_and_encoding (it->f, c, it->face_id,
26706 &char2b, 0);
26707 pcm = get_per_char_metric (font, &char2b);
26710 /* Initialize the bounding box. */
26711 if (pcm)
26713 width = cmp->glyph_len > 0 ? pcm->width : 0;
26714 ascent = pcm->ascent;
26715 descent = pcm->descent;
26716 lbearing = pcm->lbearing;
26717 rbearing = pcm->rbearing;
26719 else
26721 width = cmp->glyph_len > 0 ? font->space_width : 0;
26722 ascent = FONT_BASE (font);
26723 descent = FONT_DESCENT (font);
26724 lbearing = 0;
26725 rbearing = width;
26728 rightmost = width;
26729 leftmost = 0;
26730 lowest = - descent + boff;
26731 highest = ascent + boff;
26733 if (! font_not_found_p
26734 && font->default_ascent
26735 && CHAR_TABLE_P (Vuse_default_ascent)
26736 && !NILP (Faref (Vuse_default_ascent,
26737 make_number (it->char_to_display))))
26738 highest = font->default_ascent + boff;
26740 /* Draw the first glyph at the normal position. It may be
26741 shifted to right later if some other glyphs are drawn
26742 at the left. */
26743 cmp->offsets[i * 2] = 0;
26744 cmp->offsets[i * 2 + 1] = boff;
26745 cmp->lbearing = lbearing;
26746 cmp->rbearing = rbearing;
26748 /* Set cmp->offsets for the remaining glyphs. */
26749 for (i++; i < glyph_len; i++)
26751 int left, right, btm, top;
26752 int ch = COMPOSITION_GLYPH (cmp, i);
26753 int face_id;
26754 struct face *this_face;
26756 if (ch == '\t')
26757 ch = ' ';
26758 face_id = FACE_FOR_CHAR (it->f, face, ch, pos, it->string);
26759 this_face = FACE_FROM_ID (it->f, face_id);
26760 font = this_face->font;
26762 if (font == NULL)
26763 pcm = NULL;
26764 else
26766 get_char_face_and_encoding (it->f, ch, face_id,
26767 &char2b, 0);
26768 pcm = get_per_char_metric (font, &char2b);
26770 if (! pcm)
26771 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
26772 else
26774 width = pcm->width;
26775 ascent = pcm->ascent;
26776 descent = pcm->descent;
26777 lbearing = pcm->lbearing;
26778 rbearing = pcm->rbearing;
26779 if (cmp->method != COMPOSITION_WITH_RULE_ALTCHARS)
26781 /* Relative composition with or without
26782 alternate chars. */
26783 left = (leftmost + rightmost - width) / 2;
26784 btm = - descent + boff;
26785 if (font->relative_compose
26786 && (! CHAR_TABLE_P (Vignore_relative_composition)
26787 || NILP (Faref (Vignore_relative_composition,
26788 make_number (ch)))))
26791 if (- descent >= font->relative_compose)
26792 /* One extra pixel between two glyphs. */
26793 btm = highest + 1;
26794 else if (ascent <= 0)
26795 /* One extra pixel between two glyphs. */
26796 btm = lowest - 1 - ascent - descent;
26799 else
26801 /* A composition rule is specified by an integer
26802 value that encodes global and new reference
26803 points (GREF and NREF). GREF and NREF are
26804 specified by numbers as below:
26806 0---1---2 -- ascent
26810 9--10--11 -- center
26812 ---3---4---5--- baseline
26814 6---7---8 -- descent
26816 int rule = COMPOSITION_RULE (cmp, i);
26817 int gref, nref, grefx, grefy, nrefx, nrefy, xoff, yoff;
26819 COMPOSITION_DECODE_RULE (rule, gref, nref, xoff, yoff);
26820 grefx = gref % 3, nrefx = nref % 3;
26821 grefy = gref / 3, nrefy = nref / 3;
26822 if (xoff)
26823 xoff = font_height * (xoff - 128) / 256;
26824 if (yoff)
26825 yoff = font_height * (yoff - 128) / 256;
26827 left = (leftmost
26828 + grefx * (rightmost - leftmost) / 2
26829 - nrefx * width / 2
26830 + xoff);
26832 btm = ((grefy == 0 ? highest
26833 : grefy == 1 ? 0
26834 : grefy == 2 ? lowest
26835 : (highest + lowest) / 2)
26836 - (nrefy == 0 ? ascent + descent
26837 : nrefy == 1 ? descent - boff
26838 : nrefy == 2 ? 0
26839 : (ascent + descent) / 2)
26840 + yoff);
26843 cmp->offsets[i * 2] = left;
26844 cmp->offsets[i * 2 + 1] = btm + descent;
26846 /* Update the bounding box of the overall glyphs. */
26847 if (width > 0)
26849 right = left + width;
26850 if (left < leftmost)
26851 leftmost = left;
26852 if (right > rightmost)
26853 rightmost = right;
26855 top = btm + descent + ascent;
26856 if (top > highest)
26857 highest = top;
26858 if (btm < lowest)
26859 lowest = btm;
26861 if (cmp->lbearing > left + lbearing)
26862 cmp->lbearing = left + lbearing;
26863 if (cmp->rbearing < left + rbearing)
26864 cmp->rbearing = left + rbearing;
26868 /* If there are glyphs whose x-offsets are negative,
26869 shift all glyphs to the right and make all x-offsets
26870 non-negative. */
26871 if (leftmost < 0)
26873 for (i = 0; i < cmp->glyph_len; i++)
26874 cmp->offsets[i * 2] -= leftmost;
26875 rightmost -= leftmost;
26876 cmp->lbearing -= leftmost;
26877 cmp->rbearing -= leftmost;
26880 if (left_padded && cmp->lbearing < 0)
26882 for (i = 0; i < cmp->glyph_len; i++)
26883 cmp->offsets[i * 2] -= cmp->lbearing;
26884 rightmost -= cmp->lbearing;
26885 cmp->rbearing -= cmp->lbearing;
26886 cmp->lbearing = 0;
26888 if (right_padded && rightmost < cmp->rbearing)
26890 rightmost = cmp->rbearing;
26893 cmp->pixel_width = rightmost;
26894 cmp->ascent = highest;
26895 cmp->descent = - lowest;
26896 if (cmp->ascent < font_ascent)
26897 cmp->ascent = font_ascent;
26898 if (cmp->descent < font_descent)
26899 cmp->descent = font_descent;
26902 if (it->glyph_row
26903 && (cmp->lbearing < 0
26904 || cmp->rbearing > cmp->pixel_width))
26905 it->glyph_row->contains_overlapping_glyphs_p = 1;
26907 it->pixel_width = cmp->pixel_width;
26908 it->ascent = it->phys_ascent = cmp->ascent;
26909 it->descent = it->phys_descent = cmp->descent;
26910 if (face->box != FACE_NO_BOX)
26912 int thick = face->box_line_width;
26914 if (thick > 0)
26916 it->ascent += thick;
26917 it->descent += thick;
26919 else
26920 thick = - thick;
26922 if (it->start_of_box_run_p)
26923 it->pixel_width += thick;
26924 if (it->end_of_box_run_p)
26925 it->pixel_width += thick;
26928 /* If face has an overline, add the height of the overline
26929 (1 pixel) and a 1 pixel margin to the character height. */
26930 if (face->overline_p)
26931 it->ascent += overline_margin;
26933 take_vertical_position_into_account (it);
26934 if (it->ascent < 0)
26935 it->ascent = 0;
26936 if (it->descent < 0)
26937 it->descent = 0;
26939 if (it->glyph_row && cmp->glyph_len > 0)
26940 append_composite_glyph (it);
26942 else if (it->what == IT_COMPOSITION)
26944 /* A dynamic (automatic) composition. */
26945 struct face *face = FACE_FROM_ID (it->f, it->face_id);
26946 Lisp_Object gstring;
26947 struct font_metrics metrics;
26949 it->nglyphs = 1;
26951 gstring = composition_gstring_from_id (it->cmp_it.id);
26952 it->pixel_width
26953 = composition_gstring_width (gstring, it->cmp_it.from, it->cmp_it.to,
26954 &metrics);
26955 if (it->glyph_row
26956 && (metrics.lbearing < 0 || metrics.rbearing > metrics.width))
26957 it->glyph_row->contains_overlapping_glyphs_p = 1;
26958 it->ascent = it->phys_ascent = metrics.ascent;
26959 it->descent = it->phys_descent = metrics.descent;
26960 if (face->box != FACE_NO_BOX)
26962 int thick = face->box_line_width;
26964 if (thick > 0)
26966 it->ascent += thick;
26967 it->descent += thick;
26969 else
26970 thick = - thick;
26972 if (it->start_of_box_run_p)
26973 it->pixel_width += thick;
26974 if (it->end_of_box_run_p)
26975 it->pixel_width += thick;
26977 /* If face has an overline, add the height of the overline
26978 (1 pixel) and a 1 pixel margin to the character height. */
26979 if (face->overline_p)
26980 it->ascent += overline_margin;
26981 take_vertical_position_into_account (it);
26982 if (it->ascent < 0)
26983 it->ascent = 0;
26984 if (it->descent < 0)
26985 it->descent = 0;
26987 if (it->glyph_row)
26988 append_composite_glyph (it);
26990 else if (it->what == IT_GLYPHLESS)
26991 produce_glyphless_glyph (it, 0, Qnil);
26992 else if (it->what == IT_IMAGE)
26993 produce_image_glyph (it);
26994 else if (it->what == IT_STRETCH)
26995 produce_stretch_glyph (it);
26997 done:
26998 /* Accumulate dimensions. Note: can't assume that it->descent > 0
26999 because this isn't true for images with `:ascent 100'. */
27000 eassert (it->ascent >= 0 && it->descent >= 0);
27001 if (it->area == TEXT_AREA)
27002 it->current_x += it->pixel_width;
27004 if (extra_line_spacing > 0)
27006 it->descent += extra_line_spacing;
27007 if (extra_line_spacing > it->max_extra_line_spacing)
27008 it->max_extra_line_spacing = extra_line_spacing;
27011 it->max_ascent = max (it->max_ascent, it->ascent);
27012 it->max_descent = max (it->max_descent, it->descent);
27013 it->max_phys_ascent = max (it->max_phys_ascent, it->phys_ascent);
27014 it->max_phys_descent = max (it->max_phys_descent, it->phys_descent);
27017 /* EXPORT for RIF:
27018 Output LEN glyphs starting at START at the nominal cursor position.
27019 Advance the nominal cursor over the text. UPDATED_ROW is the glyph row
27020 being updated, and UPDATED_AREA is the area of that row being updated. */
27022 void
27023 x_write_glyphs (struct window *w, struct glyph_row *updated_row,
27024 struct glyph *start, enum glyph_row_area updated_area, int len)
27026 int x, hpos, chpos = w->phys_cursor.hpos;
27028 eassert (updated_row);
27029 /* When the window is hscrolled, cursor hpos can legitimately be out
27030 of bounds, but we draw the cursor at the corresponding window
27031 margin in that case. */
27032 if (!updated_row->reversed_p && chpos < 0)
27033 chpos = 0;
27034 if (updated_row->reversed_p && chpos >= updated_row->used[TEXT_AREA])
27035 chpos = updated_row->used[TEXT_AREA] - 1;
27037 block_input ();
27039 /* Write glyphs. */
27041 hpos = start - updated_row->glyphs[updated_area];
27042 x = draw_glyphs (w, w->output_cursor.x,
27043 updated_row, updated_area,
27044 hpos, hpos + len,
27045 DRAW_NORMAL_TEXT, 0);
27047 /* Invalidate old phys cursor if the glyph at its hpos is redrawn. */
27048 if (updated_area == TEXT_AREA
27049 && w->phys_cursor_on_p
27050 && w->phys_cursor.vpos == w->output_cursor.vpos
27051 && chpos >= hpos
27052 && chpos < hpos + len)
27053 w->phys_cursor_on_p = 0;
27055 unblock_input ();
27057 /* Advance the output cursor. */
27058 w->output_cursor.hpos += len;
27059 w->output_cursor.x = x;
27063 /* EXPORT for RIF:
27064 Insert LEN glyphs from START at the nominal cursor position. */
27066 void
27067 x_insert_glyphs (struct window *w, struct glyph_row *updated_row,
27068 struct glyph *start, enum glyph_row_area updated_area, int len)
27070 struct frame *f;
27071 int line_height, shift_by_width, shifted_region_width;
27072 struct glyph_row *row;
27073 struct glyph *glyph;
27074 int frame_x, frame_y;
27075 ptrdiff_t hpos;
27077 eassert (updated_row);
27078 block_input ();
27079 f = XFRAME (WINDOW_FRAME (w));
27081 /* Get the height of the line we are in. */
27082 row = updated_row;
27083 line_height = row->height;
27085 /* Get the width of the glyphs to insert. */
27086 shift_by_width = 0;
27087 for (glyph = start; glyph < start + len; ++glyph)
27088 shift_by_width += glyph->pixel_width;
27090 /* Get the width of the region to shift right. */
27091 shifted_region_width = (window_box_width (w, updated_area)
27092 - w->output_cursor.x
27093 - shift_by_width);
27095 /* Shift right. */
27096 frame_x = window_box_left (w, updated_area) + w->output_cursor.x;
27097 frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, w->output_cursor.y);
27099 FRAME_RIF (f)->shift_glyphs_for_insert (f, frame_x, frame_y, shifted_region_width,
27100 line_height, shift_by_width);
27102 /* Write the glyphs. */
27103 hpos = start - row->glyphs[updated_area];
27104 draw_glyphs (w, w->output_cursor.x, row, updated_area,
27105 hpos, hpos + len,
27106 DRAW_NORMAL_TEXT, 0);
27108 /* Advance the output cursor. */
27109 w->output_cursor.hpos += len;
27110 w->output_cursor.x += shift_by_width;
27111 unblock_input ();
27115 /* EXPORT for RIF:
27116 Erase the current text line from the nominal cursor position
27117 (inclusive) to pixel column TO_X (exclusive). The idea is that
27118 everything from TO_X onward is already erased.
27120 TO_X is a pixel position relative to UPDATED_AREA of currently
27121 updated window W. TO_X == -1 means clear to the end of this area. */
27123 void
27124 x_clear_end_of_line (struct window *w, struct glyph_row *updated_row,
27125 enum glyph_row_area updated_area, int to_x)
27127 struct frame *f;
27128 int max_x, min_y, max_y;
27129 int from_x, from_y, to_y;
27131 eassert (updated_row);
27132 f = XFRAME (w->frame);
27134 if (updated_row->full_width_p)
27135 max_x = (WINDOW_PIXEL_WIDTH (w)
27136 - (updated_row->mode_line_p ? WINDOW_RIGHT_DIVIDER_WIDTH (w) : 0));
27137 else
27138 max_x = window_box_width (w, updated_area);
27139 max_y = window_text_bottom_y (w);
27141 /* TO_X == 0 means don't do anything. TO_X < 0 means clear to end
27142 of window. For TO_X > 0, truncate to end of drawing area. */
27143 if (to_x == 0)
27144 return;
27145 else if (to_x < 0)
27146 to_x = max_x;
27147 else
27148 to_x = min (to_x, max_x);
27150 to_y = min (max_y, w->output_cursor.y + updated_row->height);
27152 /* Notice if the cursor will be cleared by this operation. */
27153 if (!updated_row->full_width_p)
27154 notice_overwritten_cursor (w, updated_area,
27155 w->output_cursor.x, -1,
27156 updated_row->y,
27157 MATRIX_ROW_BOTTOM_Y (updated_row));
27159 from_x = w->output_cursor.x;
27161 /* Translate to frame coordinates. */
27162 if (updated_row->full_width_p)
27164 from_x = WINDOW_TO_FRAME_PIXEL_X (w, from_x);
27165 to_x = WINDOW_TO_FRAME_PIXEL_X (w, to_x);
27167 else
27169 int area_left = window_box_left (w, updated_area);
27170 from_x += area_left;
27171 to_x += area_left;
27174 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
27175 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, max (min_y, w->output_cursor.y));
27176 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, to_y);
27178 /* Prevent inadvertently clearing to end of the X window. */
27179 if (to_x > from_x && to_y > from_y)
27181 block_input ();
27182 FRAME_RIF (f)->clear_frame_area (f, from_x, from_y,
27183 to_x - from_x, to_y - from_y);
27184 unblock_input ();
27188 #endif /* HAVE_WINDOW_SYSTEM */
27192 /***********************************************************************
27193 Cursor types
27194 ***********************************************************************/
27196 /* Value is the internal representation of the specified cursor type
27197 ARG. If type is BAR_CURSOR, return in *WIDTH the specified width
27198 of the bar cursor. */
27200 static enum text_cursor_kinds
27201 get_specified_cursor_type (Lisp_Object arg, int *width)
27203 enum text_cursor_kinds type;
27205 if (NILP (arg))
27206 return NO_CURSOR;
27208 if (EQ (arg, Qbox))
27209 return FILLED_BOX_CURSOR;
27211 if (EQ (arg, Qhollow))
27212 return HOLLOW_BOX_CURSOR;
27214 if (EQ (arg, Qbar))
27216 *width = 2;
27217 return BAR_CURSOR;
27220 if (CONSP (arg)
27221 && EQ (XCAR (arg), Qbar)
27222 && RANGED_INTEGERP (0, XCDR (arg), INT_MAX))
27224 *width = XINT (XCDR (arg));
27225 return BAR_CURSOR;
27228 if (EQ (arg, Qhbar))
27230 *width = 2;
27231 return HBAR_CURSOR;
27234 if (CONSP (arg)
27235 && EQ (XCAR (arg), Qhbar)
27236 && RANGED_INTEGERP (0, XCDR (arg), INT_MAX))
27238 *width = XINT (XCDR (arg));
27239 return HBAR_CURSOR;
27242 /* Treat anything unknown as "hollow box cursor".
27243 It was bad to signal an error; people have trouble fixing
27244 .Xdefaults with Emacs, when it has something bad in it. */
27245 type = HOLLOW_BOX_CURSOR;
27247 return type;
27250 /* Set the default cursor types for specified frame. */
27251 void
27252 set_frame_cursor_types (struct frame *f, Lisp_Object arg)
27254 int width = 1;
27255 Lisp_Object tem;
27257 FRAME_DESIRED_CURSOR (f) = get_specified_cursor_type (arg, &width);
27258 FRAME_CURSOR_WIDTH (f) = width;
27260 /* By default, set up the blink-off state depending on the on-state. */
27262 tem = Fassoc (arg, Vblink_cursor_alist);
27263 if (!NILP (tem))
27265 FRAME_BLINK_OFF_CURSOR (f)
27266 = get_specified_cursor_type (XCDR (tem), &width);
27267 FRAME_BLINK_OFF_CURSOR_WIDTH (f) = width;
27269 else
27270 FRAME_BLINK_OFF_CURSOR (f) = DEFAULT_CURSOR;
27272 /* Make sure the cursor gets redrawn. */
27273 f->cursor_type_changed = 1;
27277 #ifdef HAVE_WINDOW_SYSTEM
27279 /* Return the cursor we want to be displayed in window W. Return
27280 width of bar/hbar cursor through WIDTH arg. Return with
27281 ACTIVE_CURSOR arg set to 1 if cursor in window W is `active'
27282 (i.e. if the `system caret' should track this cursor).
27284 In a mini-buffer window, we want the cursor only to appear if we
27285 are reading input from this window. For the selected window, we
27286 want the cursor type given by the frame parameter or buffer local
27287 setting of cursor-type. If explicitly marked off, draw no cursor.
27288 In all other cases, we want a hollow box cursor. */
27290 static enum text_cursor_kinds
27291 get_window_cursor_type (struct window *w, struct glyph *glyph, int *width,
27292 int *active_cursor)
27294 struct frame *f = XFRAME (w->frame);
27295 struct buffer *b = XBUFFER (w->contents);
27296 int cursor_type = DEFAULT_CURSOR;
27297 Lisp_Object alt_cursor;
27298 int non_selected = 0;
27300 *active_cursor = 1;
27302 /* Echo area */
27303 if (cursor_in_echo_area
27304 && FRAME_HAS_MINIBUF_P (f)
27305 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
27307 if (w == XWINDOW (echo_area_window))
27309 if (EQ (BVAR (b, cursor_type), Qt) || NILP (BVAR (b, cursor_type)))
27311 *width = FRAME_CURSOR_WIDTH (f);
27312 return FRAME_DESIRED_CURSOR (f);
27314 else
27315 return get_specified_cursor_type (BVAR (b, cursor_type), width);
27318 *active_cursor = 0;
27319 non_selected = 1;
27322 /* Detect a nonselected window or nonselected frame. */
27323 else if (w != XWINDOW (f->selected_window)
27324 || f != FRAME_DISPLAY_INFO (f)->x_highlight_frame)
27326 *active_cursor = 0;
27328 if (MINI_WINDOW_P (w) && minibuf_level == 0)
27329 return NO_CURSOR;
27331 non_selected = 1;
27334 /* Never display a cursor in a window in which cursor-type is nil. */
27335 if (NILP (BVAR (b, cursor_type)))
27336 return NO_CURSOR;
27338 /* Get the normal cursor type for this window. */
27339 if (EQ (BVAR (b, cursor_type), Qt))
27341 cursor_type = FRAME_DESIRED_CURSOR (f);
27342 *width = FRAME_CURSOR_WIDTH (f);
27344 else
27345 cursor_type = get_specified_cursor_type (BVAR (b, cursor_type), width);
27347 /* Use cursor-in-non-selected-windows instead
27348 for non-selected window or frame. */
27349 if (non_selected)
27351 alt_cursor = BVAR (b, cursor_in_non_selected_windows);
27352 if (!EQ (Qt, alt_cursor))
27353 return get_specified_cursor_type (alt_cursor, width);
27354 /* t means modify the normal cursor type. */
27355 if (cursor_type == FILLED_BOX_CURSOR)
27356 cursor_type = HOLLOW_BOX_CURSOR;
27357 else if (cursor_type == BAR_CURSOR && *width > 1)
27358 --*width;
27359 return cursor_type;
27362 /* Use normal cursor if not blinked off. */
27363 if (!w->cursor_off_p)
27365 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
27367 if (cursor_type == FILLED_BOX_CURSOR)
27369 /* Using a block cursor on large images can be very annoying.
27370 So use a hollow cursor for "large" images.
27371 If image is not transparent (no mask), also use hollow cursor. */
27372 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
27373 if (img != NULL && IMAGEP (img->spec))
27375 /* Arbitrarily, interpret "Large" as >32x32 and >NxN
27376 where N = size of default frame font size.
27377 This should cover most of the "tiny" icons people may use. */
27378 if (!img->mask
27379 || img->width > max (32, WINDOW_FRAME_COLUMN_WIDTH (w))
27380 || img->height > max (32, WINDOW_FRAME_LINE_HEIGHT (w)))
27381 cursor_type = HOLLOW_BOX_CURSOR;
27384 else if (cursor_type != NO_CURSOR)
27386 /* Display current only supports BOX and HOLLOW cursors for images.
27387 So for now, unconditionally use a HOLLOW cursor when cursor is
27388 not a solid box cursor. */
27389 cursor_type = HOLLOW_BOX_CURSOR;
27392 return cursor_type;
27395 /* Cursor is blinked off, so determine how to "toggle" it. */
27397 /* First look for an entry matching the buffer's cursor-type in blink-cursor-alist. */
27398 if ((alt_cursor = Fassoc (BVAR (b, cursor_type), Vblink_cursor_alist), !NILP (alt_cursor)))
27399 return get_specified_cursor_type (XCDR (alt_cursor), width);
27401 /* Then see if frame has specified a specific blink off cursor type. */
27402 if (FRAME_BLINK_OFF_CURSOR (f) != DEFAULT_CURSOR)
27404 *width = FRAME_BLINK_OFF_CURSOR_WIDTH (f);
27405 return FRAME_BLINK_OFF_CURSOR (f);
27408 #if 0
27409 /* Some people liked having a permanently visible blinking cursor,
27410 while others had very strong opinions against it. So it was
27411 decided to remove it. KFS 2003-09-03 */
27413 /* Finally perform built-in cursor blinking:
27414 filled box <-> hollow box
27415 wide [h]bar <-> narrow [h]bar
27416 narrow [h]bar <-> no cursor
27417 other type <-> no cursor */
27419 if (cursor_type == FILLED_BOX_CURSOR)
27420 return HOLLOW_BOX_CURSOR;
27422 if ((cursor_type == BAR_CURSOR || cursor_type == HBAR_CURSOR) && *width > 1)
27424 *width = 1;
27425 return cursor_type;
27427 #endif
27429 return NO_CURSOR;
27433 /* Notice when the text cursor of window W has been completely
27434 overwritten by a drawing operation that outputs glyphs in AREA
27435 starting at X0 and ending at X1 in the line starting at Y0 and
27436 ending at Y1. X coordinates are area-relative. X1 < 0 means all
27437 the rest of the line after X0 has been written. Y coordinates
27438 are window-relative. */
27440 static void
27441 notice_overwritten_cursor (struct window *w, enum glyph_row_area area,
27442 int x0, int x1, int y0, int y1)
27444 int cx0, cx1, cy0, cy1;
27445 struct glyph_row *row;
27447 if (!w->phys_cursor_on_p)
27448 return;
27449 if (area != TEXT_AREA)
27450 return;
27452 if (w->phys_cursor.vpos < 0
27453 || w->phys_cursor.vpos >= w->current_matrix->nrows
27454 || (row = w->current_matrix->rows + w->phys_cursor.vpos,
27455 !(row->enabled_p && MATRIX_ROW_DISPLAYS_TEXT_P (row))))
27456 return;
27458 if (row->cursor_in_fringe_p)
27460 row->cursor_in_fringe_p = 0;
27461 draw_fringe_bitmap (w, row, row->reversed_p);
27462 w->phys_cursor_on_p = 0;
27463 return;
27466 cx0 = w->phys_cursor.x;
27467 cx1 = cx0 + w->phys_cursor_width;
27468 if (x0 > cx0 || (x1 >= 0 && x1 < cx1))
27469 return;
27471 /* The cursor image will be completely removed from the
27472 screen if the output area intersects the cursor area in
27473 y-direction. When we draw in [y0 y1[, and some part of
27474 the cursor is at y < y0, that part must have been drawn
27475 before. When scrolling, the cursor is erased before
27476 actually scrolling, so we don't come here. When not
27477 scrolling, the rows above the old cursor row must have
27478 changed, and in this case these rows must have written
27479 over the cursor image.
27481 Likewise if part of the cursor is below y1, with the
27482 exception of the cursor being in the first blank row at
27483 the buffer and window end because update_text_area
27484 doesn't draw that row. (Except when it does, but
27485 that's handled in update_text_area.) */
27487 cy0 = w->phys_cursor.y;
27488 cy1 = cy0 + w->phys_cursor_height;
27489 if ((y0 < cy0 || y0 >= cy1) && (y1 <= cy0 || y1 >= cy1))
27490 return;
27492 w->phys_cursor_on_p = 0;
27495 #endif /* HAVE_WINDOW_SYSTEM */
27498 /************************************************************************
27499 Mouse Face
27500 ************************************************************************/
27502 #ifdef HAVE_WINDOW_SYSTEM
27504 /* EXPORT for RIF:
27505 Fix the display of area AREA of overlapping row ROW in window W
27506 with respect to the overlapping part OVERLAPS. */
27508 void
27509 x_fix_overlapping_area (struct window *w, struct glyph_row *row,
27510 enum glyph_row_area area, int overlaps)
27512 int i, x;
27514 block_input ();
27516 x = 0;
27517 for (i = 0; i < row->used[area];)
27519 if (row->glyphs[area][i].overlaps_vertically_p)
27521 int start = i, start_x = x;
27525 x += row->glyphs[area][i].pixel_width;
27526 ++i;
27528 while (i < row->used[area]
27529 && row->glyphs[area][i].overlaps_vertically_p);
27531 draw_glyphs (w, start_x, row, area,
27532 start, i,
27533 DRAW_NORMAL_TEXT, overlaps);
27535 else
27537 x += row->glyphs[area][i].pixel_width;
27538 ++i;
27542 unblock_input ();
27546 /* EXPORT:
27547 Draw the cursor glyph of window W in glyph row ROW. See the
27548 comment of draw_glyphs for the meaning of HL. */
27550 void
27551 draw_phys_cursor_glyph (struct window *w, struct glyph_row *row,
27552 enum draw_glyphs_face hl)
27554 /* If cursor hpos is out of bounds, don't draw garbage. This can
27555 happen in mini-buffer windows when switching between echo area
27556 glyphs and mini-buffer. */
27557 if ((row->reversed_p
27558 ? (w->phys_cursor.hpos >= 0)
27559 : (w->phys_cursor.hpos < row->used[TEXT_AREA])))
27561 int on_p = w->phys_cursor_on_p;
27562 int x1;
27563 int hpos = w->phys_cursor.hpos;
27565 /* When the window is hscrolled, cursor hpos can legitimately be
27566 out of bounds, but we draw the cursor at the corresponding
27567 window margin in that case. */
27568 if (!row->reversed_p && hpos < 0)
27569 hpos = 0;
27570 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
27571 hpos = row->used[TEXT_AREA] - 1;
27573 x1 = draw_glyphs (w, w->phys_cursor.x, row, TEXT_AREA, hpos, hpos + 1,
27574 hl, 0);
27575 w->phys_cursor_on_p = on_p;
27577 if (hl == DRAW_CURSOR)
27578 w->phys_cursor_width = x1 - w->phys_cursor.x;
27579 /* When we erase the cursor, and ROW is overlapped by other
27580 rows, make sure that these overlapping parts of other rows
27581 are redrawn. */
27582 else if (hl == DRAW_NORMAL_TEXT && row->overlapped_p)
27584 w->phys_cursor_width = x1 - w->phys_cursor.x;
27586 if (row > w->current_matrix->rows
27587 && MATRIX_ROW_OVERLAPS_SUCC_P (row - 1))
27588 x_fix_overlapping_area (w, row - 1, TEXT_AREA,
27589 OVERLAPS_ERASED_CURSOR);
27591 if (MATRIX_ROW_BOTTOM_Y (row) < window_text_bottom_y (w)
27592 && MATRIX_ROW_OVERLAPS_PRED_P (row + 1))
27593 x_fix_overlapping_area (w, row + 1, TEXT_AREA,
27594 OVERLAPS_ERASED_CURSOR);
27600 /* Erase the image of a cursor of window W from the screen. */
27602 void
27603 erase_phys_cursor (struct window *w)
27605 struct frame *f = XFRAME (w->frame);
27606 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
27607 int hpos = w->phys_cursor.hpos;
27608 int vpos = w->phys_cursor.vpos;
27609 int mouse_face_here_p = 0;
27610 struct glyph_matrix *active_glyphs = w->current_matrix;
27611 struct glyph_row *cursor_row;
27612 struct glyph *cursor_glyph;
27613 enum draw_glyphs_face hl;
27615 /* No cursor displayed or row invalidated => nothing to do on the
27616 screen. */
27617 if (w->phys_cursor_type == NO_CURSOR)
27618 goto mark_cursor_off;
27620 /* VPOS >= active_glyphs->nrows means that window has been resized.
27621 Don't bother to erase the cursor. */
27622 if (vpos >= active_glyphs->nrows)
27623 goto mark_cursor_off;
27625 /* If row containing cursor is marked invalid, there is nothing we
27626 can do. */
27627 cursor_row = MATRIX_ROW (active_glyphs, vpos);
27628 if (!cursor_row->enabled_p)
27629 goto mark_cursor_off;
27631 /* If line spacing is > 0, old cursor may only be partially visible in
27632 window after split-window. So adjust visible height. */
27633 cursor_row->visible_height = min (cursor_row->visible_height,
27634 window_text_bottom_y (w) - cursor_row->y);
27636 /* If row is completely invisible, don't attempt to delete a cursor which
27637 isn't there. This can happen if cursor is at top of a window, and
27638 we switch to a buffer with a header line in that window. */
27639 if (cursor_row->visible_height <= 0)
27640 goto mark_cursor_off;
27642 /* If cursor is in the fringe, erase by drawing actual bitmap there. */
27643 if (cursor_row->cursor_in_fringe_p)
27645 cursor_row->cursor_in_fringe_p = 0;
27646 draw_fringe_bitmap (w, cursor_row, cursor_row->reversed_p);
27647 goto mark_cursor_off;
27650 /* This can happen when the new row is shorter than the old one.
27651 In this case, either draw_glyphs or clear_end_of_line
27652 should have cleared the cursor. Note that we wouldn't be
27653 able to erase the cursor in this case because we don't have a
27654 cursor glyph at hand. */
27655 if ((cursor_row->reversed_p
27656 ? (w->phys_cursor.hpos < 0)
27657 : (w->phys_cursor.hpos >= cursor_row->used[TEXT_AREA])))
27658 goto mark_cursor_off;
27660 /* When the window is hscrolled, cursor hpos can legitimately be out
27661 of bounds, but we draw the cursor at the corresponding window
27662 margin in that case. */
27663 if (!cursor_row->reversed_p && hpos < 0)
27664 hpos = 0;
27665 if (cursor_row->reversed_p && hpos >= cursor_row->used[TEXT_AREA])
27666 hpos = cursor_row->used[TEXT_AREA] - 1;
27668 /* If the cursor is in the mouse face area, redisplay that when
27669 we clear the cursor. */
27670 if (! NILP (hlinfo->mouse_face_window)
27671 && coords_in_mouse_face_p (w, hpos, vpos)
27672 /* Don't redraw the cursor's spot in mouse face if it is at the
27673 end of a line (on a newline). The cursor appears there, but
27674 mouse highlighting does not. */
27675 && cursor_row->used[TEXT_AREA] > hpos && hpos >= 0)
27676 mouse_face_here_p = 1;
27678 /* Maybe clear the display under the cursor. */
27679 if (w->phys_cursor_type == HOLLOW_BOX_CURSOR)
27681 int x, y;
27682 int header_line_height = WINDOW_HEADER_LINE_HEIGHT (w);
27683 int width;
27685 cursor_glyph = get_phys_cursor_glyph (w);
27686 if (cursor_glyph == NULL)
27687 goto mark_cursor_off;
27689 width = cursor_glyph->pixel_width;
27690 x = w->phys_cursor.x;
27691 if (x < 0)
27693 width += x;
27694 x = 0;
27696 width = min (width, window_box_width (w, TEXT_AREA) - x);
27697 y = WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height, cursor_row->y));
27698 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, x);
27700 if (width > 0)
27701 FRAME_RIF (f)->clear_frame_area (f, x, y, width, cursor_row->visible_height);
27704 /* Erase the cursor by redrawing the character underneath it. */
27705 if (mouse_face_here_p)
27706 hl = DRAW_MOUSE_FACE;
27707 else
27708 hl = DRAW_NORMAL_TEXT;
27709 draw_phys_cursor_glyph (w, cursor_row, hl);
27711 mark_cursor_off:
27712 w->phys_cursor_on_p = 0;
27713 w->phys_cursor_type = NO_CURSOR;
27717 /* EXPORT:
27718 Display or clear cursor of window W. If ON is zero, clear the
27719 cursor. If it is non-zero, display the cursor. If ON is nonzero,
27720 where to put the cursor is specified by HPOS, VPOS, X and Y. */
27722 void
27723 display_and_set_cursor (struct window *w, bool on,
27724 int hpos, int vpos, int x, int y)
27726 struct frame *f = XFRAME (w->frame);
27727 int new_cursor_type;
27728 int new_cursor_width;
27729 int active_cursor;
27730 struct glyph_row *glyph_row;
27731 struct glyph *glyph;
27733 /* This is pointless on invisible frames, and dangerous on garbaged
27734 windows and frames; in the latter case, the frame or window may
27735 be in the midst of changing its size, and x and y may be off the
27736 window. */
27737 if (! FRAME_VISIBLE_P (f)
27738 || FRAME_GARBAGED_P (f)
27739 || vpos >= w->current_matrix->nrows
27740 || hpos >= w->current_matrix->matrix_w)
27741 return;
27743 /* If cursor is off and we want it off, return quickly. */
27744 if (!on && !w->phys_cursor_on_p)
27745 return;
27747 glyph_row = MATRIX_ROW (w->current_matrix, vpos);
27748 /* If cursor row is not enabled, we don't really know where to
27749 display the cursor. */
27750 if (!glyph_row->enabled_p)
27752 w->phys_cursor_on_p = 0;
27753 return;
27756 glyph = NULL;
27757 if (!glyph_row->exact_window_width_line_p
27758 || (0 <= hpos && hpos < glyph_row->used[TEXT_AREA]))
27759 glyph = glyph_row->glyphs[TEXT_AREA] + hpos;
27761 eassert (input_blocked_p ());
27763 /* Set new_cursor_type to the cursor we want to be displayed. */
27764 new_cursor_type = get_window_cursor_type (w, glyph,
27765 &new_cursor_width, &active_cursor);
27767 /* If cursor is currently being shown and we don't want it to be or
27768 it is in the wrong place, or the cursor type is not what we want,
27769 erase it. */
27770 if (w->phys_cursor_on_p
27771 && (!on
27772 || w->phys_cursor.x != x
27773 || w->phys_cursor.y != y
27774 /* HPOS can be negative in R2L rows whose
27775 exact_window_width_line_p flag is set (i.e. their newline
27776 would "overflow into the fringe"). */
27777 || hpos < 0
27778 || new_cursor_type != w->phys_cursor_type
27779 || ((new_cursor_type == BAR_CURSOR || new_cursor_type == HBAR_CURSOR)
27780 && new_cursor_width != w->phys_cursor_width)))
27781 erase_phys_cursor (w);
27783 /* Don't check phys_cursor_on_p here because that flag is only set
27784 to zero in some cases where we know that the cursor has been
27785 completely erased, to avoid the extra work of erasing the cursor
27786 twice. In other words, phys_cursor_on_p can be 1 and the cursor
27787 still not be visible, or it has only been partly erased. */
27788 if (on)
27790 w->phys_cursor_ascent = glyph_row->ascent;
27791 w->phys_cursor_height = glyph_row->height;
27793 /* Set phys_cursor_.* before x_draw_.* is called because some
27794 of them may need the information. */
27795 w->phys_cursor.x = x;
27796 w->phys_cursor.y = glyph_row->y;
27797 w->phys_cursor.hpos = hpos;
27798 w->phys_cursor.vpos = vpos;
27801 FRAME_RIF (f)->draw_window_cursor (w, glyph_row, x, y,
27802 new_cursor_type, new_cursor_width,
27803 on, active_cursor);
27807 /* Switch the display of W's cursor on or off, according to the value
27808 of ON. */
27810 static void
27811 update_window_cursor (struct window *w, bool on)
27813 /* Don't update cursor in windows whose frame is in the process
27814 of being deleted. */
27815 if (w->current_matrix)
27817 int hpos = w->phys_cursor.hpos;
27818 int vpos = w->phys_cursor.vpos;
27819 struct glyph_row *row;
27821 if (vpos >= w->current_matrix->nrows
27822 || hpos >= w->current_matrix->matrix_w)
27823 return;
27825 row = MATRIX_ROW (w->current_matrix, vpos);
27827 /* When the window is hscrolled, cursor hpos can legitimately be
27828 out of bounds, but we draw the cursor at the corresponding
27829 window margin in that case. */
27830 if (!row->reversed_p && hpos < 0)
27831 hpos = 0;
27832 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
27833 hpos = row->used[TEXT_AREA] - 1;
27835 block_input ();
27836 display_and_set_cursor (w, on, hpos, vpos,
27837 w->phys_cursor.x, w->phys_cursor.y);
27838 unblock_input ();
27843 /* Call update_window_cursor with parameter ON_P on all leaf windows
27844 in the window tree rooted at W. */
27846 static void
27847 update_cursor_in_window_tree (struct window *w, bool on_p)
27849 while (w)
27851 if (WINDOWP (w->contents))
27852 update_cursor_in_window_tree (XWINDOW (w->contents), on_p);
27853 else
27854 update_window_cursor (w, on_p);
27856 w = NILP (w->next) ? 0 : XWINDOW (w->next);
27861 /* EXPORT:
27862 Display the cursor on window W, or clear it, according to ON_P.
27863 Don't change the cursor's position. */
27865 void
27866 x_update_cursor (struct frame *f, bool on_p)
27868 update_cursor_in_window_tree (XWINDOW (f->root_window), on_p);
27872 /* EXPORT:
27873 Clear the cursor of window W to background color, and mark the
27874 cursor as not shown. This is used when the text where the cursor
27875 is about to be rewritten. */
27877 void
27878 x_clear_cursor (struct window *w)
27880 if (FRAME_VISIBLE_P (XFRAME (w->frame)) && w->phys_cursor_on_p)
27881 update_window_cursor (w, 0);
27884 #endif /* HAVE_WINDOW_SYSTEM */
27886 /* Implementation of draw_row_with_mouse_face for GUI sessions, GPM,
27887 and MSDOS. */
27888 static void
27889 draw_row_with_mouse_face (struct window *w, int start_x, struct glyph_row *row,
27890 int start_hpos, int end_hpos,
27891 enum draw_glyphs_face draw)
27893 #ifdef HAVE_WINDOW_SYSTEM
27894 if (FRAME_WINDOW_P (XFRAME (w->frame)))
27896 draw_glyphs (w, start_x, row, TEXT_AREA, start_hpos, end_hpos, draw, 0);
27897 return;
27899 #endif
27900 #if defined (HAVE_GPM) || defined (MSDOS) || defined (WINDOWSNT)
27901 tty_draw_row_with_mouse_face (w, row, start_hpos, end_hpos, draw);
27902 #endif
27905 /* Display the active region described by mouse_face_* according to DRAW. */
27907 static void
27908 show_mouse_face (Mouse_HLInfo *hlinfo, enum draw_glyphs_face draw)
27910 struct window *w = XWINDOW (hlinfo->mouse_face_window);
27911 struct frame *f = XFRAME (WINDOW_FRAME (w));
27913 if (/* If window is in the process of being destroyed, don't bother
27914 to do anything. */
27915 w->current_matrix != NULL
27916 /* Don't update mouse highlight if hidden. */
27917 && (draw != DRAW_MOUSE_FACE || !hlinfo->mouse_face_hidden)
27918 /* Recognize when we are called to operate on rows that don't exist
27919 anymore. This can happen when a window is split. */
27920 && hlinfo->mouse_face_end_row < w->current_matrix->nrows)
27922 int phys_cursor_on_p = w->phys_cursor_on_p;
27923 struct glyph_row *row, *first, *last;
27925 first = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_beg_row);
27926 last = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_end_row);
27928 for (row = first; row <= last && row->enabled_p; ++row)
27930 int start_hpos, end_hpos, start_x;
27932 /* For all but the first row, the highlight starts at column 0. */
27933 if (row == first)
27935 /* R2L rows have BEG and END in reversed order, but the
27936 screen drawing geometry is always left to right. So
27937 we need to mirror the beginning and end of the
27938 highlighted area in R2L rows. */
27939 if (!row->reversed_p)
27941 start_hpos = hlinfo->mouse_face_beg_col;
27942 start_x = hlinfo->mouse_face_beg_x;
27944 else if (row == last)
27946 start_hpos = hlinfo->mouse_face_end_col;
27947 start_x = hlinfo->mouse_face_end_x;
27949 else
27951 start_hpos = 0;
27952 start_x = 0;
27955 else if (row->reversed_p && row == last)
27957 start_hpos = hlinfo->mouse_face_end_col;
27958 start_x = hlinfo->mouse_face_end_x;
27960 else
27962 start_hpos = 0;
27963 start_x = 0;
27966 if (row == last)
27968 if (!row->reversed_p)
27969 end_hpos = hlinfo->mouse_face_end_col;
27970 else if (row == first)
27971 end_hpos = hlinfo->mouse_face_beg_col;
27972 else
27974 end_hpos = row->used[TEXT_AREA];
27975 if (draw == DRAW_NORMAL_TEXT)
27976 row->fill_line_p = 1; /* Clear to end of line */
27979 else if (row->reversed_p && row == first)
27980 end_hpos = hlinfo->mouse_face_beg_col;
27981 else
27983 end_hpos = row->used[TEXT_AREA];
27984 if (draw == DRAW_NORMAL_TEXT)
27985 row->fill_line_p = 1; /* Clear to end of line */
27988 if (end_hpos > start_hpos)
27990 draw_row_with_mouse_face (w, start_x, row,
27991 start_hpos, end_hpos, draw);
27993 row->mouse_face_p
27994 = draw == DRAW_MOUSE_FACE || draw == DRAW_IMAGE_RAISED;
27998 #ifdef HAVE_WINDOW_SYSTEM
27999 /* When we've written over the cursor, arrange for it to
28000 be displayed again. */
28001 if (FRAME_WINDOW_P (f)
28002 && phys_cursor_on_p && !w->phys_cursor_on_p)
28004 int hpos = w->phys_cursor.hpos;
28006 /* When the window is hscrolled, cursor hpos can legitimately be
28007 out of bounds, but we draw the cursor at the corresponding
28008 window margin in that case. */
28009 if (!row->reversed_p && hpos < 0)
28010 hpos = 0;
28011 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
28012 hpos = row->used[TEXT_AREA] - 1;
28014 block_input ();
28015 display_and_set_cursor (w, 1, hpos, w->phys_cursor.vpos,
28016 w->phys_cursor.x, w->phys_cursor.y);
28017 unblock_input ();
28019 #endif /* HAVE_WINDOW_SYSTEM */
28022 #ifdef HAVE_WINDOW_SYSTEM
28023 /* Change the mouse cursor. */
28024 if (FRAME_WINDOW_P (f) && NILP (do_mouse_tracking))
28026 #if ! defined (USE_GTK) && ! defined (HAVE_NS)
28027 if (draw == DRAW_NORMAL_TEXT
28028 && !EQ (hlinfo->mouse_face_window, f->tool_bar_window))
28029 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->text_cursor);
28030 else
28031 #endif
28032 if (draw == DRAW_MOUSE_FACE)
28033 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->hand_cursor);
28034 else
28035 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->nontext_cursor);
28037 #endif /* HAVE_WINDOW_SYSTEM */
28040 /* EXPORT:
28041 Clear out the mouse-highlighted active region.
28042 Redraw it un-highlighted first. Value is non-zero if mouse
28043 face was actually drawn unhighlighted. */
28046 clear_mouse_face (Mouse_HLInfo *hlinfo)
28048 int cleared = 0;
28050 if (!hlinfo->mouse_face_hidden && !NILP (hlinfo->mouse_face_window))
28052 show_mouse_face (hlinfo, DRAW_NORMAL_TEXT);
28053 cleared = 1;
28056 hlinfo->mouse_face_beg_row = hlinfo->mouse_face_beg_col = -1;
28057 hlinfo->mouse_face_end_row = hlinfo->mouse_face_end_col = -1;
28058 hlinfo->mouse_face_window = Qnil;
28059 hlinfo->mouse_face_overlay = Qnil;
28060 return cleared;
28063 /* Return true if the coordinates HPOS and VPOS on windows W are
28064 within the mouse face on that window. */
28065 static bool
28066 coords_in_mouse_face_p (struct window *w, int hpos, int vpos)
28068 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (XFRAME (w->frame));
28070 /* Quickly resolve the easy cases. */
28071 if (!(WINDOWP (hlinfo->mouse_face_window)
28072 && XWINDOW (hlinfo->mouse_face_window) == w))
28073 return false;
28074 if (vpos < hlinfo->mouse_face_beg_row
28075 || vpos > hlinfo->mouse_face_end_row)
28076 return false;
28077 if (vpos > hlinfo->mouse_face_beg_row
28078 && vpos < hlinfo->mouse_face_end_row)
28079 return true;
28081 if (!MATRIX_ROW (w->current_matrix, vpos)->reversed_p)
28083 if (hlinfo->mouse_face_beg_row == hlinfo->mouse_face_end_row)
28085 if (hlinfo->mouse_face_beg_col <= hpos && hpos < hlinfo->mouse_face_end_col)
28086 return true;
28088 else if ((vpos == hlinfo->mouse_face_beg_row
28089 && hpos >= hlinfo->mouse_face_beg_col)
28090 || (vpos == hlinfo->mouse_face_end_row
28091 && hpos < hlinfo->mouse_face_end_col))
28092 return true;
28094 else
28096 if (hlinfo->mouse_face_beg_row == hlinfo->mouse_face_end_row)
28098 if (hlinfo->mouse_face_end_col < hpos && hpos <= hlinfo->mouse_face_beg_col)
28099 return true;
28101 else if ((vpos == hlinfo->mouse_face_beg_row
28102 && hpos <= hlinfo->mouse_face_beg_col)
28103 || (vpos == hlinfo->mouse_face_end_row
28104 && hpos > hlinfo->mouse_face_end_col))
28105 return true;
28107 return false;
28111 /* EXPORT:
28112 True if physical cursor of window W is within mouse face. */
28114 bool
28115 cursor_in_mouse_face_p (struct window *w)
28117 int hpos = w->phys_cursor.hpos;
28118 int vpos = w->phys_cursor.vpos;
28119 struct glyph_row *row = MATRIX_ROW (w->current_matrix, vpos);
28121 /* When the window is hscrolled, cursor hpos can legitimately be out
28122 of bounds, but we draw the cursor at the corresponding window
28123 margin in that case. */
28124 if (!row->reversed_p && hpos < 0)
28125 hpos = 0;
28126 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
28127 hpos = row->used[TEXT_AREA] - 1;
28129 return coords_in_mouse_face_p (w, hpos, vpos);
28134 /* Find the glyph rows START_ROW and END_ROW of window W that display
28135 characters between buffer positions START_CHARPOS and END_CHARPOS
28136 (excluding END_CHARPOS). DISP_STRING is a display string that
28137 covers these buffer positions. This is similar to
28138 row_containing_pos, but is more accurate when bidi reordering makes
28139 buffer positions change non-linearly with glyph rows. */
28140 static void
28141 rows_from_pos_range (struct window *w,
28142 ptrdiff_t start_charpos, ptrdiff_t end_charpos,
28143 Lisp_Object disp_string,
28144 struct glyph_row **start, struct glyph_row **end)
28146 struct glyph_row *first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
28147 int last_y = window_text_bottom_y (w);
28148 struct glyph_row *row;
28150 *start = NULL;
28151 *end = NULL;
28153 while (!first->enabled_p
28154 && first < MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w))
28155 first++;
28157 /* Find the START row. */
28158 for (row = first;
28159 row->enabled_p && MATRIX_ROW_BOTTOM_Y (row) <= last_y;
28160 row++)
28162 /* A row can potentially be the START row if the range of the
28163 characters it displays intersects the range
28164 [START_CHARPOS..END_CHARPOS). */
28165 if (! ((start_charpos < MATRIX_ROW_START_CHARPOS (row)
28166 && end_charpos < MATRIX_ROW_START_CHARPOS (row))
28167 /* See the commentary in row_containing_pos, for the
28168 explanation of the complicated way to check whether
28169 some position is beyond the end of the characters
28170 displayed by a row. */
28171 || ((start_charpos > MATRIX_ROW_END_CHARPOS (row)
28172 || (start_charpos == MATRIX_ROW_END_CHARPOS (row)
28173 && !row->ends_at_zv_p
28174 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
28175 && (end_charpos > MATRIX_ROW_END_CHARPOS (row)
28176 || (end_charpos == MATRIX_ROW_END_CHARPOS (row)
28177 && !row->ends_at_zv_p
28178 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))))))
28180 /* Found a candidate row. Now make sure at least one of the
28181 glyphs it displays has a charpos from the range
28182 [START_CHARPOS..END_CHARPOS).
28184 This is not obvious because bidi reordering could make
28185 buffer positions of a row be 1,2,3,102,101,100, and if we
28186 want to highlight characters in [50..60), we don't want
28187 this row, even though [50..60) does intersect [1..103),
28188 the range of character positions given by the row's start
28189 and end positions. */
28190 struct glyph *g = row->glyphs[TEXT_AREA];
28191 struct glyph *e = g + row->used[TEXT_AREA];
28193 while (g < e)
28195 if (((BUFFERP (g->object) || NILP (g->object))
28196 && start_charpos <= g->charpos && g->charpos < end_charpos)
28197 /* A glyph that comes from DISP_STRING is by
28198 definition to be highlighted. */
28199 || EQ (g->object, disp_string))
28200 *start = row;
28201 g++;
28203 if (*start)
28204 break;
28208 /* Find the END row. */
28209 if (!*start
28210 /* If the last row is partially visible, start looking for END
28211 from that row, instead of starting from FIRST. */
28212 && !(row->enabled_p
28213 && row->y < last_y && MATRIX_ROW_BOTTOM_Y (row) > last_y))
28214 row = first;
28215 for ( ; row->enabled_p && MATRIX_ROW_BOTTOM_Y (row) <= last_y; row++)
28217 struct glyph_row *next = row + 1;
28218 ptrdiff_t next_start = MATRIX_ROW_START_CHARPOS (next);
28220 if (!next->enabled_p
28221 || next >= MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w)
28222 /* The first row >= START whose range of displayed characters
28223 does NOT intersect the range [START_CHARPOS..END_CHARPOS]
28224 is the row END + 1. */
28225 || (start_charpos < next_start
28226 && end_charpos < next_start)
28227 || ((start_charpos > MATRIX_ROW_END_CHARPOS (next)
28228 || (start_charpos == MATRIX_ROW_END_CHARPOS (next)
28229 && !next->ends_at_zv_p
28230 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (next)))
28231 && (end_charpos > MATRIX_ROW_END_CHARPOS (next)
28232 || (end_charpos == MATRIX_ROW_END_CHARPOS (next)
28233 && !next->ends_at_zv_p
28234 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (next)))))
28236 *end = row;
28237 break;
28239 else
28241 /* If the next row's edges intersect [START_CHARPOS..END_CHARPOS],
28242 but none of the characters it displays are in the range, it is
28243 also END + 1. */
28244 struct glyph *g = next->glyphs[TEXT_AREA];
28245 struct glyph *s = g;
28246 struct glyph *e = g + next->used[TEXT_AREA];
28248 while (g < e)
28250 if (((BUFFERP (g->object) || NILP (g->object))
28251 && ((start_charpos <= g->charpos && g->charpos < end_charpos)
28252 /* If the buffer position of the first glyph in
28253 the row is equal to END_CHARPOS, it means
28254 the last character to be highlighted is the
28255 newline of ROW, and we must consider NEXT as
28256 END, not END+1. */
28257 || (((!next->reversed_p && g == s)
28258 || (next->reversed_p && g == e - 1))
28259 && (g->charpos == end_charpos
28260 /* Special case for when NEXT is an
28261 empty line at ZV. */
28262 || (g->charpos == -1
28263 && !row->ends_at_zv_p
28264 && next_start == end_charpos)))))
28265 /* A glyph that comes from DISP_STRING is by
28266 definition to be highlighted. */
28267 || EQ (g->object, disp_string))
28268 break;
28269 g++;
28271 if (g == e)
28273 *end = row;
28274 break;
28276 /* The first row that ends at ZV must be the last to be
28277 highlighted. */
28278 else if (next->ends_at_zv_p)
28280 *end = next;
28281 break;
28287 /* This function sets the mouse_face_* elements of HLINFO, assuming
28288 the mouse cursor is on a glyph with buffer charpos MOUSE_CHARPOS in
28289 window WINDOW. START_CHARPOS and END_CHARPOS are buffer positions
28290 for the overlay or run of text properties specifying the mouse
28291 face. BEFORE_STRING and AFTER_STRING, if non-nil, are a
28292 before-string and after-string that must also be highlighted.
28293 DISP_STRING, if non-nil, is a display string that may cover some
28294 or all of the highlighted text. */
28296 static void
28297 mouse_face_from_buffer_pos (Lisp_Object window,
28298 Mouse_HLInfo *hlinfo,
28299 ptrdiff_t mouse_charpos,
28300 ptrdiff_t start_charpos,
28301 ptrdiff_t end_charpos,
28302 Lisp_Object before_string,
28303 Lisp_Object after_string,
28304 Lisp_Object disp_string)
28306 struct window *w = XWINDOW (window);
28307 struct glyph_row *first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
28308 struct glyph_row *r1, *r2;
28309 struct glyph *glyph, *end;
28310 ptrdiff_t ignore, pos;
28311 int x;
28313 eassert (NILP (disp_string) || STRINGP (disp_string));
28314 eassert (NILP (before_string) || STRINGP (before_string));
28315 eassert (NILP (after_string) || STRINGP (after_string));
28317 /* Find the rows corresponding to START_CHARPOS and END_CHARPOS. */
28318 rows_from_pos_range (w, start_charpos, end_charpos, disp_string, &r1, &r2);
28319 if (r1 == NULL)
28320 r1 = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
28321 /* If the before-string or display-string contains newlines,
28322 rows_from_pos_range skips to its last row. Move back. */
28323 if (!NILP (before_string) || !NILP (disp_string))
28325 struct glyph_row *prev;
28326 while ((prev = r1 - 1, prev >= first)
28327 && MATRIX_ROW_END_CHARPOS (prev) == start_charpos
28328 && prev->used[TEXT_AREA] > 0)
28330 struct glyph *beg = prev->glyphs[TEXT_AREA];
28331 glyph = beg + prev->used[TEXT_AREA];
28332 while (--glyph >= beg && NILP (glyph->object));
28333 if (glyph < beg
28334 || !(EQ (glyph->object, before_string)
28335 || EQ (glyph->object, disp_string)))
28336 break;
28337 r1 = prev;
28340 if (r2 == NULL)
28342 r2 = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
28343 hlinfo->mouse_face_past_end = 1;
28345 else if (!NILP (after_string))
28347 /* If the after-string has newlines, advance to its last row. */
28348 struct glyph_row *next;
28349 struct glyph_row *last
28350 = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
28352 for (next = r2 + 1;
28353 next <= last
28354 && next->used[TEXT_AREA] > 0
28355 && EQ (next->glyphs[TEXT_AREA]->object, after_string);
28356 ++next)
28357 r2 = next;
28359 /* The rest of the display engine assumes that mouse_face_beg_row is
28360 either above mouse_face_end_row or identical to it. But with
28361 bidi-reordered continued lines, the row for START_CHARPOS could
28362 be below the row for END_CHARPOS. If so, swap the rows and store
28363 them in correct order. */
28364 if (r1->y > r2->y)
28366 struct glyph_row *tem = r2;
28368 r2 = r1;
28369 r1 = tem;
28372 hlinfo->mouse_face_beg_row = MATRIX_ROW_VPOS (r1, w->current_matrix);
28373 hlinfo->mouse_face_end_row = MATRIX_ROW_VPOS (r2, w->current_matrix);
28375 /* For a bidi-reordered row, the positions of BEFORE_STRING,
28376 AFTER_STRING, DISP_STRING, START_CHARPOS, and END_CHARPOS
28377 could be anywhere in the row and in any order. The strategy
28378 below is to find the leftmost and the rightmost glyph that
28379 belongs to either of these 3 strings, or whose position is
28380 between START_CHARPOS and END_CHARPOS, and highlight all the
28381 glyphs between those two. This may cover more than just the text
28382 between START_CHARPOS and END_CHARPOS if the range of characters
28383 strides the bidi level boundary, e.g. if the beginning is in R2L
28384 text while the end is in L2R text or vice versa. */
28385 if (!r1->reversed_p)
28387 /* This row is in a left to right paragraph. Scan it left to
28388 right. */
28389 glyph = r1->glyphs[TEXT_AREA];
28390 end = glyph + r1->used[TEXT_AREA];
28391 x = r1->x;
28393 /* Skip truncation glyphs at the start of the glyph row. */
28394 if (MATRIX_ROW_DISPLAYS_TEXT_P (r1))
28395 for (; glyph < end
28396 && NILP (glyph->object)
28397 && glyph->charpos < 0;
28398 ++glyph)
28399 x += glyph->pixel_width;
28401 /* Scan the glyph row, looking for BEFORE_STRING, AFTER_STRING,
28402 or DISP_STRING, and the first glyph from buffer whose
28403 position is between START_CHARPOS and END_CHARPOS. */
28404 for (; glyph < end
28405 && !NILP (glyph->object)
28406 && !EQ (glyph->object, disp_string)
28407 && !(BUFFERP (glyph->object)
28408 && (glyph->charpos >= start_charpos
28409 && glyph->charpos < end_charpos));
28410 ++glyph)
28412 /* BEFORE_STRING or AFTER_STRING are only relevant if they
28413 are present at buffer positions between START_CHARPOS and
28414 END_CHARPOS, or if they come from an overlay. */
28415 if (EQ (glyph->object, before_string))
28417 pos = string_buffer_position (before_string,
28418 start_charpos);
28419 /* If pos == 0, it means before_string came from an
28420 overlay, not from a buffer position. */
28421 if (!pos || (pos >= start_charpos && pos < end_charpos))
28422 break;
28424 else if (EQ (glyph->object, after_string))
28426 pos = string_buffer_position (after_string, end_charpos);
28427 if (!pos || (pos >= start_charpos && pos < end_charpos))
28428 break;
28430 x += glyph->pixel_width;
28432 hlinfo->mouse_face_beg_x = x;
28433 hlinfo->mouse_face_beg_col = glyph - r1->glyphs[TEXT_AREA];
28435 else
28437 /* This row is in a right to left paragraph. Scan it right to
28438 left. */
28439 struct glyph *g;
28441 end = r1->glyphs[TEXT_AREA] - 1;
28442 glyph = end + r1->used[TEXT_AREA];
28444 /* Skip truncation glyphs at the start of the glyph row. */
28445 if (MATRIX_ROW_DISPLAYS_TEXT_P (r1))
28446 for (; glyph > end
28447 && NILP (glyph->object)
28448 && glyph->charpos < 0;
28449 --glyph)
28452 /* Scan the glyph row, looking for BEFORE_STRING, AFTER_STRING,
28453 or DISP_STRING, and the first glyph from buffer whose
28454 position is between START_CHARPOS and END_CHARPOS. */
28455 for (; glyph > end
28456 && !NILP (glyph->object)
28457 && !EQ (glyph->object, disp_string)
28458 && !(BUFFERP (glyph->object)
28459 && (glyph->charpos >= start_charpos
28460 && glyph->charpos < end_charpos));
28461 --glyph)
28463 /* BEFORE_STRING or AFTER_STRING are only relevant if they
28464 are present at buffer positions between START_CHARPOS and
28465 END_CHARPOS, or if they come from an overlay. */
28466 if (EQ (glyph->object, before_string))
28468 pos = string_buffer_position (before_string, start_charpos);
28469 /* If pos == 0, it means before_string came from an
28470 overlay, not from a buffer position. */
28471 if (!pos || (pos >= start_charpos && pos < end_charpos))
28472 break;
28474 else if (EQ (glyph->object, after_string))
28476 pos = string_buffer_position (after_string, end_charpos);
28477 if (!pos || (pos >= start_charpos && pos < end_charpos))
28478 break;
28482 glyph++; /* first glyph to the right of the highlighted area */
28483 for (g = r1->glyphs[TEXT_AREA], x = r1->x; g < glyph; g++)
28484 x += g->pixel_width;
28485 hlinfo->mouse_face_beg_x = x;
28486 hlinfo->mouse_face_beg_col = glyph - r1->glyphs[TEXT_AREA];
28489 /* If the highlight ends in a different row, compute GLYPH and END
28490 for the end row. Otherwise, reuse the values computed above for
28491 the row where the highlight begins. */
28492 if (r2 != r1)
28494 if (!r2->reversed_p)
28496 glyph = r2->glyphs[TEXT_AREA];
28497 end = glyph + r2->used[TEXT_AREA];
28498 x = r2->x;
28500 else
28502 end = r2->glyphs[TEXT_AREA] - 1;
28503 glyph = end + r2->used[TEXT_AREA];
28507 if (!r2->reversed_p)
28509 /* Skip truncation and continuation glyphs near the end of the
28510 row, and also blanks and stretch glyphs inserted by
28511 extend_face_to_end_of_line. */
28512 while (end > glyph
28513 && NILP ((end - 1)->object))
28514 --end;
28515 /* Scan the rest of the glyph row from the end, looking for the
28516 first glyph that comes from BEFORE_STRING, AFTER_STRING, or
28517 DISP_STRING, or whose position is between START_CHARPOS
28518 and END_CHARPOS */
28519 for (--end;
28520 end > glyph
28521 && !NILP (end->object)
28522 && !EQ (end->object, disp_string)
28523 && !(BUFFERP (end->object)
28524 && (end->charpos >= start_charpos
28525 && end->charpos < end_charpos));
28526 --end)
28528 /* BEFORE_STRING or AFTER_STRING are only relevant if they
28529 are present at buffer positions between START_CHARPOS and
28530 END_CHARPOS, or if they come from an overlay. */
28531 if (EQ (end->object, before_string))
28533 pos = string_buffer_position (before_string, start_charpos);
28534 if (!pos || (pos >= start_charpos && pos < end_charpos))
28535 break;
28537 else if (EQ (end->object, after_string))
28539 pos = string_buffer_position (after_string, end_charpos);
28540 if (!pos || (pos >= start_charpos && pos < end_charpos))
28541 break;
28544 /* Find the X coordinate of the last glyph to be highlighted. */
28545 for (; glyph <= end; ++glyph)
28546 x += glyph->pixel_width;
28548 hlinfo->mouse_face_end_x = x;
28549 hlinfo->mouse_face_end_col = glyph - r2->glyphs[TEXT_AREA];
28551 else
28553 /* Skip truncation and continuation glyphs near the end of the
28554 row, and also blanks and stretch glyphs inserted by
28555 extend_face_to_end_of_line. */
28556 x = r2->x;
28557 end++;
28558 while (end < glyph
28559 && NILP (end->object))
28561 x += end->pixel_width;
28562 ++end;
28564 /* Scan the rest of the glyph row from the end, looking for the
28565 first glyph that comes from BEFORE_STRING, AFTER_STRING, or
28566 DISP_STRING, or whose position is between START_CHARPOS
28567 and END_CHARPOS */
28568 for ( ;
28569 end < glyph
28570 && !NILP (end->object)
28571 && !EQ (end->object, disp_string)
28572 && !(BUFFERP (end->object)
28573 && (end->charpos >= start_charpos
28574 && end->charpos < end_charpos));
28575 ++end)
28577 /* BEFORE_STRING or AFTER_STRING are only relevant if they
28578 are present at buffer positions between START_CHARPOS and
28579 END_CHARPOS, or if they come from an overlay. */
28580 if (EQ (end->object, before_string))
28582 pos = string_buffer_position (before_string, start_charpos);
28583 if (!pos || (pos >= start_charpos && pos < end_charpos))
28584 break;
28586 else if (EQ (end->object, after_string))
28588 pos = string_buffer_position (after_string, end_charpos);
28589 if (!pos || (pos >= start_charpos && pos < end_charpos))
28590 break;
28592 x += end->pixel_width;
28594 /* If we exited the above loop because we arrived at the last
28595 glyph of the row, and its buffer position is still not in
28596 range, it means the last character in range is the preceding
28597 newline. Bump the end column and x values to get past the
28598 last glyph. */
28599 if (end == glyph
28600 && BUFFERP (end->object)
28601 && (end->charpos < start_charpos
28602 || end->charpos >= end_charpos))
28604 x += end->pixel_width;
28605 ++end;
28607 hlinfo->mouse_face_end_x = x;
28608 hlinfo->mouse_face_end_col = end - r2->glyphs[TEXT_AREA];
28611 hlinfo->mouse_face_window = window;
28612 hlinfo->mouse_face_face_id
28613 = face_at_buffer_position (w, mouse_charpos, &ignore,
28614 mouse_charpos + 1,
28615 !hlinfo->mouse_face_hidden, -1);
28616 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
28619 /* The following function is not used anymore (replaced with
28620 mouse_face_from_string_pos), but I leave it here for the time
28621 being, in case someone would. */
28623 #if 0 /* not used */
28625 /* Find the position of the glyph for position POS in OBJECT in
28626 window W's current matrix, and return in *X, *Y the pixel
28627 coordinates, and return in *HPOS, *VPOS the column/row of the glyph.
28629 RIGHT_P non-zero means return the position of the right edge of the
28630 glyph, RIGHT_P zero means return the left edge position.
28632 If no glyph for POS exists in the matrix, return the position of
28633 the glyph with the next smaller position that is in the matrix, if
28634 RIGHT_P is zero. If RIGHT_P is non-zero, and no glyph for POS
28635 exists in the matrix, return the position of the glyph with the
28636 next larger position in OBJECT.
28638 Value is non-zero if a glyph was found. */
28640 static int
28641 fast_find_string_pos (struct window *w, ptrdiff_t pos, Lisp_Object object,
28642 int *hpos, int *vpos, int *x, int *y, int right_p)
28644 int yb = window_text_bottom_y (w);
28645 struct glyph_row *r;
28646 struct glyph *best_glyph = NULL;
28647 struct glyph_row *best_row = NULL;
28648 int best_x = 0;
28650 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
28651 r->enabled_p && r->y < yb;
28652 ++r)
28654 struct glyph *g = r->glyphs[TEXT_AREA];
28655 struct glyph *e = g + r->used[TEXT_AREA];
28656 int gx;
28658 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
28659 if (EQ (g->object, object))
28661 if (g->charpos == pos)
28663 best_glyph = g;
28664 best_x = gx;
28665 best_row = r;
28666 goto found;
28668 else if (best_glyph == NULL
28669 || ((eabs (g->charpos - pos)
28670 < eabs (best_glyph->charpos - pos))
28671 && (right_p
28672 ? g->charpos < pos
28673 : g->charpos > pos)))
28675 best_glyph = g;
28676 best_x = gx;
28677 best_row = r;
28682 found:
28684 if (best_glyph)
28686 *x = best_x;
28687 *hpos = best_glyph - best_row->glyphs[TEXT_AREA];
28689 if (right_p)
28691 *x += best_glyph->pixel_width;
28692 ++*hpos;
28695 *y = best_row->y;
28696 *vpos = MATRIX_ROW_VPOS (best_row, w->current_matrix);
28699 return best_glyph != NULL;
28701 #endif /* not used */
28703 /* Find the positions of the first and the last glyphs in window W's
28704 current matrix that occlude positions [STARTPOS..ENDPOS) in OBJECT
28705 (assumed to be a string), and return in HLINFO's mouse_face_*
28706 members the pixel and column/row coordinates of those glyphs. */
28708 static void
28709 mouse_face_from_string_pos (struct window *w, Mouse_HLInfo *hlinfo,
28710 Lisp_Object object,
28711 ptrdiff_t startpos, ptrdiff_t endpos)
28713 int yb = window_text_bottom_y (w);
28714 struct glyph_row *r;
28715 struct glyph *g, *e;
28716 int gx;
28717 int found = 0;
28719 /* Find the glyph row with at least one position in the range
28720 [STARTPOS..ENDPOS), and the first glyph in that row whose
28721 position belongs to that range. */
28722 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
28723 r->enabled_p && r->y < yb;
28724 ++r)
28726 if (!r->reversed_p)
28728 g = r->glyphs[TEXT_AREA];
28729 e = g + r->used[TEXT_AREA];
28730 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
28731 if (EQ (g->object, object)
28732 && startpos <= g->charpos && g->charpos < endpos)
28734 hlinfo->mouse_face_beg_row
28735 = MATRIX_ROW_VPOS (r, w->current_matrix);
28736 hlinfo->mouse_face_beg_col = g - r->glyphs[TEXT_AREA];
28737 hlinfo->mouse_face_beg_x = gx;
28738 found = 1;
28739 break;
28742 else
28744 struct glyph *g1;
28746 e = r->glyphs[TEXT_AREA];
28747 g = e + r->used[TEXT_AREA];
28748 for ( ; g > e; --g)
28749 if (EQ ((g-1)->object, object)
28750 && startpos <= (g-1)->charpos && (g-1)->charpos < endpos)
28752 hlinfo->mouse_face_beg_row
28753 = MATRIX_ROW_VPOS (r, w->current_matrix);
28754 hlinfo->mouse_face_beg_col = g - r->glyphs[TEXT_AREA];
28755 for (gx = r->x, g1 = r->glyphs[TEXT_AREA]; g1 < g; ++g1)
28756 gx += g1->pixel_width;
28757 hlinfo->mouse_face_beg_x = gx;
28758 found = 1;
28759 break;
28762 if (found)
28763 break;
28766 if (!found)
28767 return;
28769 /* Starting with the next row, look for the first row which does NOT
28770 include any glyphs whose positions are in the range. */
28771 for (++r; r->enabled_p && r->y < yb; ++r)
28773 g = r->glyphs[TEXT_AREA];
28774 e = g + r->used[TEXT_AREA];
28775 found = 0;
28776 for ( ; g < e; ++g)
28777 if (EQ (g->object, object)
28778 && startpos <= g->charpos && g->charpos < endpos)
28780 found = 1;
28781 break;
28783 if (!found)
28784 break;
28787 /* The highlighted region ends on the previous row. */
28788 r--;
28790 /* Set the end row. */
28791 hlinfo->mouse_face_end_row = MATRIX_ROW_VPOS (r, w->current_matrix);
28793 /* Compute and set the end column and the end column's horizontal
28794 pixel coordinate. */
28795 if (!r->reversed_p)
28797 g = r->glyphs[TEXT_AREA];
28798 e = g + r->used[TEXT_AREA];
28799 for ( ; e > g; --e)
28800 if (EQ ((e-1)->object, object)
28801 && startpos <= (e-1)->charpos && (e-1)->charpos < endpos)
28802 break;
28803 hlinfo->mouse_face_end_col = e - g;
28805 for (gx = r->x; g < e; ++g)
28806 gx += g->pixel_width;
28807 hlinfo->mouse_face_end_x = gx;
28809 else
28811 e = r->glyphs[TEXT_AREA];
28812 g = e + r->used[TEXT_AREA];
28813 for (gx = r->x ; e < g; ++e)
28815 if (EQ (e->object, object)
28816 && startpos <= e->charpos && e->charpos < endpos)
28817 break;
28818 gx += e->pixel_width;
28820 hlinfo->mouse_face_end_col = e - r->glyphs[TEXT_AREA];
28821 hlinfo->mouse_face_end_x = gx;
28825 #ifdef HAVE_WINDOW_SYSTEM
28827 /* See if position X, Y is within a hot-spot of an image. */
28829 static int
28830 on_hot_spot_p (Lisp_Object hot_spot, int x, int y)
28832 if (!CONSP (hot_spot))
28833 return 0;
28835 if (EQ (XCAR (hot_spot), Qrect))
28837 /* CDR is (Top-Left . Bottom-Right) = ((x0 . y0) . (x1 . y1)) */
28838 Lisp_Object rect = XCDR (hot_spot);
28839 Lisp_Object tem;
28840 if (!CONSP (rect))
28841 return 0;
28842 if (!CONSP (XCAR (rect)))
28843 return 0;
28844 if (!CONSP (XCDR (rect)))
28845 return 0;
28846 if (!(tem = XCAR (XCAR (rect)), INTEGERP (tem) && x >= XINT (tem)))
28847 return 0;
28848 if (!(tem = XCDR (XCAR (rect)), INTEGERP (tem) && y >= XINT (tem)))
28849 return 0;
28850 if (!(tem = XCAR (XCDR (rect)), INTEGERP (tem) && x <= XINT (tem)))
28851 return 0;
28852 if (!(tem = XCDR (XCDR (rect)), INTEGERP (tem) && y <= XINT (tem)))
28853 return 0;
28854 return 1;
28856 else if (EQ (XCAR (hot_spot), Qcircle))
28858 /* CDR is (Center . Radius) = ((x0 . y0) . r) */
28859 Lisp_Object circ = XCDR (hot_spot);
28860 Lisp_Object lr, lx0, ly0;
28861 if (CONSP (circ)
28862 && CONSP (XCAR (circ))
28863 && (lr = XCDR (circ), INTEGERP (lr) || FLOATP (lr))
28864 && (lx0 = XCAR (XCAR (circ)), INTEGERP (lx0))
28865 && (ly0 = XCDR (XCAR (circ)), INTEGERP (ly0)))
28867 double r = XFLOATINT (lr);
28868 double dx = XINT (lx0) - x;
28869 double dy = XINT (ly0) - y;
28870 return (dx * dx + dy * dy <= r * r);
28873 else if (EQ (XCAR (hot_spot), Qpoly))
28875 /* CDR is [x0 y0 x1 y1 x2 y2 ...x(n-1) y(n-1)] */
28876 if (VECTORP (XCDR (hot_spot)))
28878 struct Lisp_Vector *v = XVECTOR (XCDR (hot_spot));
28879 Lisp_Object *poly = v->contents;
28880 ptrdiff_t n = v->header.size;
28881 ptrdiff_t i;
28882 int inside = 0;
28883 Lisp_Object lx, ly;
28884 int x0, y0;
28886 /* Need an even number of coordinates, and at least 3 edges. */
28887 if (n < 6 || n & 1)
28888 return 0;
28890 /* Count edge segments intersecting line from (X,Y) to (X,infinity).
28891 If count is odd, we are inside polygon. Pixels on edges
28892 may or may not be included depending on actual geometry of the
28893 polygon. */
28894 if ((lx = poly[n-2], !INTEGERP (lx))
28895 || (ly = poly[n-1], !INTEGERP (lx)))
28896 return 0;
28897 x0 = XINT (lx), y0 = XINT (ly);
28898 for (i = 0; i < n; i += 2)
28900 int x1 = x0, y1 = y0;
28901 if ((lx = poly[i], !INTEGERP (lx))
28902 || (ly = poly[i+1], !INTEGERP (ly)))
28903 return 0;
28904 x0 = XINT (lx), y0 = XINT (ly);
28906 /* Does this segment cross the X line? */
28907 if (x0 >= x)
28909 if (x1 >= x)
28910 continue;
28912 else if (x1 < x)
28913 continue;
28914 if (y > y0 && y > y1)
28915 continue;
28916 if (y < y0 + ((y1 - y0) * (x - x0)) / (x1 - x0))
28917 inside = !inside;
28919 return inside;
28922 return 0;
28925 Lisp_Object
28926 find_hot_spot (Lisp_Object map, int x, int y)
28928 while (CONSP (map))
28930 if (CONSP (XCAR (map))
28931 && on_hot_spot_p (XCAR (XCAR (map)), x, y))
28932 return XCAR (map);
28933 map = XCDR (map);
28936 return Qnil;
28939 DEFUN ("lookup-image-map", Flookup_image_map, Slookup_image_map,
28940 3, 3, 0,
28941 doc: /* Lookup in image map MAP coordinates X and Y.
28942 An image map is an alist where each element has the format (AREA ID PLIST).
28943 An AREA is specified as either a rectangle, a circle, or a polygon:
28944 A rectangle is a cons (rect . ((x0 . y0) . (x1 . y1))) specifying the
28945 pixel coordinates of the upper left and bottom right corners.
28946 A circle is a cons (circle . ((x0 . y0) . r)) specifying the center
28947 and the radius of the circle; r may be a float or integer.
28948 A polygon is a cons (poly . [x0 y0 x1 y1 ...]) where each pair in the
28949 vector describes one corner in the polygon.
28950 Returns the alist element for the first matching AREA in MAP. */)
28951 (Lisp_Object map, Lisp_Object x, Lisp_Object y)
28953 if (NILP (map))
28954 return Qnil;
28956 CHECK_NUMBER (x);
28957 CHECK_NUMBER (y);
28959 return find_hot_spot (map,
28960 clip_to_bounds (INT_MIN, XINT (x), INT_MAX),
28961 clip_to_bounds (INT_MIN, XINT (y), INT_MAX));
28965 /* Display frame CURSOR, optionally using shape defined by POINTER. */
28966 static void
28967 define_frame_cursor1 (struct frame *f, Cursor cursor, Lisp_Object pointer)
28969 /* Do not change cursor shape while dragging mouse. */
28970 if (!NILP (do_mouse_tracking))
28971 return;
28973 if (!NILP (pointer))
28975 if (EQ (pointer, Qarrow))
28976 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
28977 else if (EQ (pointer, Qhand))
28978 cursor = FRAME_X_OUTPUT (f)->hand_cursor;
28979 else if (EQ (pointer, Qtext))
28980 cursor = FRAME_X_OUTPUT (f)->text_cursor;
28981 else if (EQ (pointer, intern ("hdrag")))
28982 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
28983 else if (EQ (pointer, intern ("nhdrag")))
28984 cursor = FRAME_X_OUTPUT (f)->vertical_drag_cursor;
28985 #ifdef HAVE_X_WINDOWS
28986 else if (EQ (pointer, intern ("vdrag")))
28987 cursor = FRAME_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
28988 #endif
28989 else if (EQ (pointer, intern ("hourglass")))
28990 cursor = FRAME_X_OUTPUT (f)->hourglass_cursor;
28991 else if (EQ (pointer, Qmodeline))
28992 cursor = FRAME_X_OUTPUT (f)->modeline_cursor;
28993 else
28994 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
28997 if (cursor != No_Cursor)
28998 FRAME_RIF (f)->define_frame_cursor (f, cursor);
29001 #endif /* HAVE_WINDOW_SYSTEM */
29003 /* Take proper action when mouse has moved to the mode or header line
29004 or marginal area AREA of window W, x-position X and y-position Y.
29005 X is relative to the start of the text display area of W, so the
29006 width of bitmap areas and scroll bars must be subtracted to get a
29007 position relative to the start of the mode line. */
29009 static void
29010 note_mode_line_or_margin_highlight (Lisp_Object window, int x, int y,
29011 enum window_part area)
29013 struct window *w = XWINDOW (window);
29014 struct frame *f = XFRAME (w->frame);
29015 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
29016 #ifdef HAVE_WINDOW_SYSTEM
29017 Display_Info *dpyinfo;
29018 #endif
29019 Cursor cursor = No_Cursor;
29020 Lisp_Object pointer = Qnil;
29021 int dx, dy, width, height;
29022 ptrdiff_t charpos;
29023 Lisp_Object string, object = Qnil;
29024 Lisp_Object pos IF_LINT (= Qnil), help;
29026 Lisp_Object mouse_face;
29027 int original_x_pixel = x;
29028 struct glyph * glyph = NULL, * row_start_glyph = NULL;
29029 struct glyph_row *row IF_LINT (= 0);
29031 if (area == ON_MODE_LINE || area == ON_HEADER_LINE)
29033 int x0;
29034 struct glyph *end;
29036 /* Kludge alert: mode_line_string takes X/Y in pixels, but
29037 returns them in row/column units! */
29038 string = mode_line_string (w, area, &x, &y, &charpos,
29039 &object, &dx, &dy, &width, &height);
29041 row = (area == ON_MODE_LINE
29042 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
29043 : MATRIX_HEADER_LINE_ROW (w->current_matrix));
29045 /* Find the glyph under the mouse pointer. */
29046 if (row->mode_line_p && row->enabled_p)
29048 glyph = row_start_glyph = row->glyphs[TEXT_AREA];
29049 end = glyph + row->used[TEXT_AREA];
29051 for (x0 = original_x_pixel;
29052 glyph < end && x0 >= glyph->pixel_width;
29053 ++glyph)
29054 x0 -= glyph->pixel_width;
29056 if (glyph >= end)
29057 glyph = NULL;
29060 else
29062 x -= WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
29063 /* Kludge alert: marginal_area_string takes X/Y in pixels, but
29064 returns them in row/column units! */
29065 string = marginal_area_string (w, area, &x, &y, &charpos,
29066 &object, &dx, &dy, &width, &height);
29069 help = Qnil;
29071 #ifdef HAVE_WINDOW_SYSTEM
29072 if (IMAGEP (object))
29074 Lisp_Object image_map, hotspot;
29075 if ((image_map = Fplist_get (XCDR (object), QCmap),
29076 !NILP (image_map))
29077 && (hotspot = find_hot_spot (image_map, dx, dy),
29078 CONSP (hotspot))
29079 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
29081 Lisp_Object plist;
29083 /* Could check XCAR (hotspot) to see if we enter/leave this hot-spot.
29084 If so, we could look for mouse-enter, mouse-leave
29085 properties in PLIST (and do something...). */
29086 hotspot = XCDR (hotspot);
29087 if (CONSP (hotspot)
29088 && (plist = XCAR (hotspot), CONSP (plist)))
29090 pointer = Fplist_get (plist, Qpointer);
29091 if (NILP (pointer))
29092 pointer = Qhand;
29093 help = Fplist_get (plist, Qhelp_echo);
29094 if (!NILP (help))
29096 help_echo_string = help;
29097 XSETWINDOW (help_echo_window, w);
29098 help_echo_object = w->contents;
29099 help_echo_pos = charpos;
29103 if (NILP (pointer))
29104 pointer = Fplist_get (XCDR (object), QCpointer);
29106 #endif /* HAVE_WINDOW_SYSTEM */
29108 if (STRINGP (string))
29109 pos = make_number (charpos);
29111 /* Set the help text and mouse pointer. If the mouse is on a part
29112 of the mode line without any text (e.g. past the right edge of
29113 the mode line text), use the default help text and pointer. */
29114 if (STRINGP (string) || area == ON_MODE_LINE)
29116 /* Arrange to display the help by setting the global variables
29117 help_echo_string, help_echo_object, and help_echo_pos. */
29118 if (NILP (help))
29120 if (STRINGP (string))
29121 help = Fget_text_property (pos, Qhelp_echo, string);
29123 if (!NILP (help))
29125 help_echo_string = help;
29126 XSETWINDOW (help_echo_window, w);
29127 help_echo_object = string;
29128 help_echo_pos = charpos;
29130 else if (area == ON_MODE_LINE)
29132 Lisp_Object default_help
29133 = buffer_local_value (Qmode_line_default_help_echo,
29134 w->contents);
29136 if (STRINGP (default_help))
29138 help_echo_string = default_help;
29139 XSETWINDOW (help_echo_window, w);
29140 help_echo_object = Qnil;
29141 help_echo_pos = -1;
29146 #ifdef HAVE_WINDOW_SYSTEM
29147 /* Change the mouse pointer according to what is under it. */
29148 if (FRAME_WINDOW_P (f))
29150 bool draggable = (! WINDOW_BOTTOMMOST_P (w)
29151 || minibuf_level
29152 || NILP (Vresize_mini_windows));
29154 dpyinfo = FRAME_DISPLAY_INFO (f);
29155 if (STRINGP (string))
29157 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
29159 if (NILP (pointer))
29160 pointer = Fget_text_property (pos, Qpointer, string);
29162 /* Change the mouse pointer according to what is under X/Y. */
29163 if (NILP (pointer)
29164 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE)))
29166 Lisp_Object map;
29167 map = Fget_text_property (pos, Qlocal_map, string);
29168 if (!KEYMAPP (map))
29169 map = Fget_text_property (pos, Qkeymap, string);
29170 if (!KEYMAPP (map) && draggable)
29171 cursor = dpyinfo->vertical_scroll_bar_cursor;
29174 else if (draggable)
29175 /* Default mode-line pointer. */
29176 cursor = FRAME_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
29178 #endif
29181 /* Change the mouse face according to what is under X/Y. */
29182 if (STRINGP (string))
29184 mouse_face = Fget_text_property (pos, Qmouse_face, string);
29185 if (!NILP (Vmouse_highlight) && !NILP (mouse_face)
29186 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
29187 && glyph)
29189 Lisp_Object b, e;
29191 struct glyph * tmp_glyph;
29193 int gpos;
29194 int gseq_length;
29195 int total_pixel_width;
29196 ptrdiff_t begpos, endpos, ignore;
29198 int vpos, hpos;
29200 b = Fprevious_single_property_change (make_number (charpos + 1),
29201 Qmouse_face, string, Qnil);
29202 if (NILP (b))
29203 begpos = 0;
29204 else
29205 begpos = XINT (b);
29207 e = Fnext_single_property_change (pos, Qmouse_face, string, Qnil);
29208 if (NILP (e))
29209 endpos = SCHARS (string);
29210 else
29211 endpos = XINT (e);
29213 /* Calculate the glyph position GPOS of GLYPH in the
29214 displayed string, relative to the beginning of the
29215 highlighted part of the string.
29217 Note: GPOS is different from CHARPOS. CHARPOS is the
29218 position of GLYPH in the internal string object. A mode
29219 line string format has structures which are converted to
29220 a flattened string by the Emacs Lisp interpreter. The
29221 internal string is an element of those structures. The
29222 displayed string is the flattened string. */
29223 tmp_glyph = row_start_glyph;
29224 while (tmp_glyph < glyph
29225 && (!(EQ (tmp_glyph->object, glyph->object)
29226 && begpos <= tmp_glyph->charpos
29227 && tmp_glyph->charpos < endpos)))
29228 tmp_glyph++;
29229 gpos = glyph - tmp_glyph;
29231 /* Calculate the length GSEQ_LENGTH of the glyph sequence of
29232 the highlighted part of the displayed string to which
29233 GLYPH belongs. Note: GSEQ_LENGTH is different from
29234 SCHARS (STRING), because the latter returns the length of
29235 the internal string. */
29236 for (tmp_glyph = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
29237 tmp_glyph > glyph
29238 && (!(EQ (tmp_glyph->object, glyph->object)
29239 && begpos <= tmp_glyph->charpos
29240 && tmp_glyph->charpos < endpos));
29241 tmp_glyph--)
29243 gseq_length = gpos + (tmp_glyph - glyph) + 1;
29245 /* Calculate the total pixel width of all the glyphs between
29246 the beginning of the highlighted area and GLYPH. */
29247 total_pixel_width = 0;
29248 for (tmp_glyph = glyph - gpos; tmp_glyph != glyph; tmp_glyph++)
29249 total_pixel_width += tmp_glyph->pixel_width;
29251 /* Pre calculation of re-rendering position. Note: X is in
29252 column units here, after the call to mode_line_string or
29253 marginal_area_string. */
29254 hpos = x - gpos;
29255 vpos = (area == ON_MODE_LINE
29256 ? (w->current_matrix)->nrows - 1
29257 : 0);
29259 /* If GLYPH's position is included in the region that is
29260 already drawn in mouse face, we have nothing to do. */
29261 if ( EQ (window, hlinfo->mouse_face_window)
29262 && (!row->reversed_p
29263 ? (hlinfo->mouse_face_beg_col <= hpos
29264 && hpos < hlinfo->mouse_face_end_col)
29265 /* In R2L rows we swap BEG and END, see below. */
29266 : (hlinfo->mouse_face_end_col <= hpos
29267 && hpos < hlinfo->mouse_face_beg_col))
29268 && hlinfo->mouse_face_beg_row == vpos )
29269 return;
29271 if (clear_mouse_face (hlinfo))
29272 cursor = No_Cursor;
29274 if (!row->reversed_p)
29276 hlinfo->mouse_face_beg_col = hpos;
29277 hlinfo->mouse_face_beg_x = original_x_pixel
29278 - (total_pixel_width + dx);
29279 hlinfo->mouse_face_end_col = hpos + gseq_length;
29280 hlinfo->mouse_face_end_x = 0;
29282 else
29284 /* In R2L rows, show_mouse_face expects BEG and END
29285 coordinates to be swapped. */
29286 hlinfo->mouse_face_end_col = hpos;
29287 hlinfo->mouse_face_end_x = original_x_pixel
29288 - (total_pixel_width + dx);
29289 hlinfo->mouse_face_beg_col = hpos + gseq_length;
29290 hlinfo->mouse_face_beg_x = 0;
29293 hlinfo->mouse_face_beg_row = vpos;
29294 hlinfo->mouse_face_end_row = hlinfo->mouse_face_beg_row;
29295 hlinfo->mouse_face_past_end = 0;
29296 hlinfo->mouse_face_window = window;
29298 hlinfo->mouse_face_face_id = face_at_string_position (w, string,
29299 charpos,
29300 0, &ignore,
29301 glyph->face_id,
29302 true);
29303 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
29305 if (NILP (pointer))
29306 pointer = Qhand;
29308 else if ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
29309 clear_mouse_face (hlinfo);
29311 #ifdef HAVE_WINDOW_SYSTEM
29312 if (FRAME_WINDOW_P (f))
29313 define_frame_cursor1 (f, cursor, pointer);
29314 #endif
29318 /* EXPORT:
29319 Take proper action when the mouse has moved to position X, Y on
29320 frame F with regards to highlighting portions of display that have
29321 mouse-face properties. Also de-highlight portions of display where
29322 the mouse was before, set the mouse pointer shape as appropriate
29323 for the mouse coordinates, and activate help echo (tooltips).
29324 X and Y can be negative or out of range. */
29326 void
29327 note_mouse_highlight (struct frame *f, int x, int y)
29329 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
29330 enum window_part part = ON_NOTHING;
29331 Lisp_Object window;
29332 struct window *w;
29333 Cursor cursor = No_Cursor;
29334 Lisp_Object pointer = Qnil; /* Takes precedence over cursor! */
29335 struct buffer *b;
29337 /* When a menu is active, don't highlight because this looks odd. */
29338 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS) || defined (MSDOS)
29339 if (popup_activated ())
29340 return;
29341 #endif
29343 if (!f->glyphs_initialized_p
29344 || f->pointer_invisible)
29345 return;
29347 hlinfo->mouse_face_mouse_x = x;
29348 hlinfo->mouse_face_mouse_y = y;
29349 hlinfo->mouse_face_mouse_frame = f;
29351 if (hlinfo->mouse_face_defer)
29352 return;
29354 /* Which window is that in? */
29355 window = window_from_coordinates (f, x, y, &part, 1);
29357 /* If displaying active text in another window, clear that. */
29358 if (! EQ (window, hlinfo->mouse_face_window)
29359 /* Also clear if we move out of text area in same window. */
29360 || (!NILP (hlinfo->mouse_face_window)
29361 && !NILP (window)
29362 && part != ON_TEXT
29363 && part != ON_MODE_LINE
29364 && part != ON_HEADER_LINE))
29365 clear_mouse_face (hlinfo);
29367 /* Not on a window -> return. */
29368 if (!WINDOWP (window))
29369 return;
29371 /* Reset help_echo_string. It will get recomputed below. */
29372 help_echo_string = Qnil;
29374 /* Convert to window-relative pixel coordinates. */
29375 w = XWINDOW (window);
29376 frame_to_window_pixel_xy (w, &x, &y);
29378 #if defined (HAVE_WINDOW_SYSTEM) && ! defined (USE_GTK) && ! defined (HAVE_NS)
29379 /* Handle tool-bar window differently since it doesn't display a
29380 buffer. */
29381 if (EQ (window, f->tool_bar_window))
29383 note_tool_bar_highlight (f, x, y);
29384 return;
29386 #endif
29388 /* Mouse is on the mode, header line or margin? */
29389 if (part == ON_MODE_LINE || part == ON_HEADER_LINE
29390 || part == ON_LEFT_MARGIN || part == ON_RIGHT_MARGIN)
29392 note_mode_line_or_margin_highlight (window, x, y, part);
29394 #ifdef HAVE_WINDOW_SYSTEM
29395 if (part == ON_LEFT_MARGIN || part == ON_RIGHT_MARGIN)
29397 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
29398 /* Show non-text cursor (Bug#16647). */
29399 goto set_cursor;
29401 else
29402 #endif
29403 return;
29406 #ifdef HAVE_WINDOW_SYSTEM
29407 if (part == ON_VERTICAL_BORDER)
29409 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
29410 help_echo_string = build_string ("drag-mouse-1: resize");
29412 else if (part == ON_RIGHT_DIVIDER)
29414 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
29415 help_echo_string = build_string ("drag-mouse-1: resize");
29417 else if (part == ON_BOTTOM_DIVIDER)
29418 if (! WINDOW_BOTTOMMOST_P (w)
29419 || minibuf_level
29420 || NILP (Vresize_mini_windows))
29422 cursor = FRAME_X_OUTPUT (f)->vertical_drag_cursor;
29423 help_echo_string = build_string ("drag-mouse-1: resize");
29425 else
29426 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
29427 else if (part == ON_LEFT_FRINGE || part == ON_RIGHT_FRINGE
29428 || part == ON_VERTICAL_SCROLL_BAR
29429 || part == ON_HORIZONTAL_SCROLL_BAR)
29430 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
29431 else
29432 cursor = FRAME_X_OUTPUT (f)->text_cursor;
29433 #endif
29435 /* Are we in a window whose display is up to date?
29436 And verify the buffer's text has not changed. */
29437 b = XBUFFER (w->contents);
29438 if (part == ON_TEXT && w->window_end_valid && !window_outdated (w))
29440 int hpos, vpos, dx, dy, area = LAST_AREA;
29441 ptrdiff_t pos;
29442 struct glyph *glyph;
29443 Lisp_Object object;
29444 Lisp_Object mouse_face = Qnil, position;
29445 Lisp_Object *overlay_vec = NULL;
29446 ptrdiff_t i, noverlays;
29447 struct buffer *obuf;
29448 ptrdiff_t obegv, ozv;
29449 int same_region;
29451 /* Find the glyph under X/Y. */
29452 glyph = x_y_to_hpos_vpos (w, x, y, &hpos, &vpos, &dx, &dy, &area);
29454 #ifdef HAVE_WINDOW_SYSTEM
29455 /* Look for :pointer property on image. */
29456 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
29458 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
29459 if (img != NULL && IMAGEP (img->spec))
29461 Lisp_Object image_map, hotspot;
29462 if ((image_map = Fplist_get (XCDR (img->spec), QCmap),
29463 !NILP (image_map))
29464 && (hotspot = find_hot_spot (image_map,
29465 glyph->slice.img.x + dx,
29466 glyph->slice.img.y + dy),
29467 CONSP (hotspot))
29468 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
29470 Lisp_Object plist;
29472 /* Could check XCAR (hotspot) to see if we enter/leave
29473 this hot-spot.
29474 If so, we could look for mouse-enter, mouse-leave
29475 properties in PLIST (and do something...). */
29476 hotspot = XCDR (hotspot);
29477 if (CONSP (hotspot)
29478 && (plist = XCAR (hotspot), CONSP (plist)))
29480 pointer = Fplist_get (plist, Qpointer);
29481 if (NILP (pointer))
29482 pointer = Qhand;
29483 help_echo_string = Fplist_get (plist, Qhelp_echo);
29484 if (!NILP (help_echo_string))
29486 help_echo_window = window;
29487 help_echo_object = glyph->object;
29488 help_echo_pos = glyph->charpos;
29492 if (NILP (pointer))
29493 pointer = Fplist_get (XCDR (img->spec), QCpointer);
29496 #endif /* HAVE_WINDOW_SYSTEM */
29498 /* Clear mouse face if X/Y not over text. */
29499 if (glyph == NULL
29500 || area != TEXT_AREA
29501 || !MATRIX_ROW_DISPLAYS_TEXT_P (MATRIX_ROW (w->current_matrix, vpos))
29502 /* Glyph's OBJECT is nil for glyphs inserted by the
29503 display engine for its internal purposes, like truncation
29504 and continuation glyphs and blanks beyond the end of
29505 line's text on text terminals. If we are over such a
29506 glyph, we are not over any text. */
29507 || NILP (glyph->object)
29508 /* R2L rows have a stretch glyph at their front, which
29509 stands for no text, whereas L2R rows have no glyphs at
29510 all beyond the end of text. Treat such stretch glyphs
29511 like we do with NULL glyphs in L2R rows. */
29512 || (MATRIX_ROW (w->current_matrix, vpos)->reversed_p
29513 && glyph == MATRIX_ROW_GLYPH_START (w->current_matrix, vpos)
29514 && glyph->type == STRETCH_GLYPH
29515 && glyph->avoid_cursor_p))
29517 if (clear_mouse_face (hlinfo))
29518 cursor = No_Cursor;
29519 #ifdef HAVE_WINDOW_SYSTEM
29520 if (FRAME_WINDOW_P (f) && NILP (pointer))
29522 if (area != TEXT_AREA)
29523 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
29524 else
29525 pointer = Vvoid_text_area_pointer;
29527 #endif
29528 goto set_cursor;
29531 pos = glyph->charpos;
29532 object = glyph->object;
29533 if (!STRINGP (object) && !BUFFERP (object))
29534 goto set_cursor;
29536 /* If we get an out-of-range value, return now; avoid an error. */
29537 if (BUFFERP (object) && pos > BUF_Z (b))
29538 goto set_cursor;
29540 /* Make the window's buffer temporarily current for
29541 overlays_at and compute_char_face. */
29542 obuf = current_buffer;
29543 current_buffer = b;
29544 obegv = BEGV;
29545 ozv = ZV;
29546 BEGV = BEG;
29547 ZV = Z;
29549 /* Is this char mouse-active or does it have help-echo? */
29550 position = make_number (pos);
29552 USE_SAFE_ALLOCA;
29554 if (BUFFERP (object))
29556 /* Put all the overlays we want in a vector in overlay_vec. */
29557 GET_OVERLAYS_AT (pos, overlay_vec, noverlays, NULL, 0);
29558 /* Sort overlays into increasing priority order. */
29559 noverlays = sort_overlays (overlay_vec, noverlays, w);
29561 else
29562 noverlays = 0;
29564 if (NILP (Vmouse_highlight))
29566 clear_mouse_face (hlinfo);
29567 goto check_help_echo;
29570 same_region = coords_in_mouse_face_p (w, hpos, vpos);
29572 if (same_region)
29573 cursor = No_Cursor;
29575 /* Check mouse-face highlighting. */
29576 if (! same_region
29577 /* If there exists an overlay with mouse-face overlapping
29578 the one we are currently highlighting, we have to
29579 check if we enter the overlapping overlay, and then
29580 highlight only that. */
29581 || (OVERLAYP (hlinfo->mouse_face_overlay)
29582 && mouse_face_overlay_overlaps (hlinfo->mouse_face_overlay)))
29584 /* Find the highest priority overlay with a mouse-face. */
29585 Lisp_Object overlay = Qnil;
29586 for (i = noverlays - 1; i >= 0 && NILP (overlay); --i)
29588 mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
29589 if (!NILP (mouse_face))
29590 overlay = overlay_vec[i];
29593 /* If we're highlighting the same overlay as before, there's
29594 no need to do that again. */
29595 if (!NILP (overlay) && EQ (overlay, hlinfo->mouse_face_overlay))
29596 goto check_help_echo;
29597 hlinfo->mouse_face_overlay = overlay;
29599 /* Clear the display of the old active region, if any. */
29600 if (clear_mouse_face (hlinfo))
29601 cursor = No_Cursor;
29603 /* If no overlay applies, get a text property. */
29604 if (NILP (overlay))
29605 mouse_face = Fget_text_property (position, Qmouse_face, object);
29607 /* Next, compute the bounds of the mouse highlighting and
29608 display it. */
29609 if (!NILP (mouse_face) && STRINGP (object))
29611 /* The mouse-highlighting comes from a display string
29612 with a mouse-face. */
29613 Lisp_Object s, e;
29614 ptrdiff_t ignore;
29616 s = Fprevious_single_property_change
29617 (make_number (pos + 1), Qmouse_face, object, Qnil);
29618 e = Fnext_single_property_change
29619 (position, Qmouse_face, object, Qnil);
29620 if (NILP (s))
29621 s = make_number (0);
29622 if (NILP (e))
29623 e = make_number (SCHARS (object));
29624 mouse_face_from_string_pos (w, hlinfo, object,
29625 XINT (s), XINT (e));
29626 hlinfo->mouse_face_past_end = 0;
29627 hlinfo->mouse_face_window = window;
29628 hlinfo->mouse_face_face_id
29629 = face_at_string_position (w, object, pos, 0, &ignore,
29630 glyph->face_id, true);
29631 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
29632 cursor = No_Cursor;
29634 else
29636 /* The mouse-highlighting, if any, comes from an overlay
29637 or text property in the buffer. */
29638 Lisp_Object buffer IF_LINT (= Qnil);
29639 Lisp_Object disp_string IF_LINT (= Qnil);
29641 if (STRINGP (object))
29643 /* If we are on a display string with no mouse-face,
29644 check if the text under it has one. */
29645 struct glyph_row *r = MATRIX_ROW (w->current_matrix, vpos);
29646 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
29647 pos = string_buffer_position (object, start);
29648 if (pos > 0)
29650 mouse_face = get_char_property_and_overlay
29651 (make_number (pos), Qmouse_face, w->contents, &overlay);
29652 buffer = w->contents;
29653 disp_string = object;
29656 else
29658 buffer = object;
29659 disp_string = Qnil;
29662 if (!NILP (mouse_face))
29664 Lisp_Object before, after;
29665 Lisp_Object before_string, after_string;
29666 /* To correctly find the limits of mouse highlight
29667 in a bidi-reordered buffer, we must not use the
29668 optimization of limiting the search in
29669 previous-single-property-change and
29670 next-single-property-change, because
29671 rows_from_pos_range needs the real start and end
29672 positions to DTRT in this case. That's because
29673 the first row visible in a window does not
29674 necessarily display the character whose position
29675 is the smallest. */
29676 Lisp_Object lim1
29677 = NILP (BVAR (XBUFFER (buffer), bidi_display_reordering))
29678 ? Fmarker_position (w->start)
29679 : Qnil;
29680 Lisp_Object lim2
29681 = NILP (BVAR (XBUFFER (buffer), bidi_display_reordering))
29682 ? make_number (BUF_Z (XBUFFER (buffer))
29683 - w->window_end_pos)
29684 : Qnil;
29686 if (NILP (overlay))
29688 /* Handle the text property case. */
29689 before = Fprevious_single_property_change
29690 (make_number (pos + 1), Qmouse_face, buffer, lim1);
29691 after = Fnext_single_property_change
29692 (make_number (pos), Qmouse_face, buffer, lim2);
29693 before_string = after_string = Qnil;
29695 else
29697 /* Handle the overlay case. */
29698 before = Foverlay_start (overlay);
29699 after = Foverlay_end (overlay);
29700 before_string = Foverlay_get (overlay, Qbefore_string);
29701 after_string = Foverlay_get (overlay, Qafter_string);
29703 if (!STRINGP (before_string)) before_string = Qnil;
29704 if (!STRINGP (after_string)) after_string = Qnil;
29707 mouse_face_from_buffer_pos (window, hlinfo, pos,
29708 NILP (before)
29710 : XFASTINT (before),
29711 NILP (after)
29712 ? BUF_Z (XBUFFER (buffer))
29713 : XFASTINT (after),
29714 before_string, after_string,
29715 disp_string);
29716 cursor = No_Cursor;
29721 check_help_echo:
29723 /* Look for a `help-echo' property. */
29724 if (NILP (help_echo_string)) {
29725 Lisp_Object help, overlay;
29727 /* Check overlays first. */
29728 help = overlay = Qnil;
29729 for (i = noverlays - 1; i >= 0 && NILP (help); --i)
29731 overlay = overlay_vec[i];
29732 help = Foverlay_get (overlay, Qhelp_echo);
29735 if (!NILP (help))
29737 help_echo_string = help;
29738 help_echo_window = window;
29739 help_echo_object = overlay;
29740 help_echo_pos = pos;
29742 else
29744 Lisp_Object obj = glyph->object;
29745 ptrdiff_t charpos = glyph->charpos;
29747 /* Try text properties. */
29748 if (STRINGP (obj)
29749 && charpos >= 0
29750 && charpos < SCHARS (obj))
29752 help = Fget_text_property (make_number (charpos),
29753 Qhelp_echo, obj);
29754 if (NILP (help))
29756 /* If the string itself doesn't specify a help-echo,
29757 see if the buffer text ``under'' it does. */
29758 struct glyph_row *r
29759 = MATRIX_ROW (w->current_matrix, vpos);
29760 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
29761 ptrdiff_t p = string_buffer_position (obj, start);
29762 if (p > 0)
29764 help = Fget_char_property (make_number (p),
29765 Qhelp_echo, w->contents);
29766 if (!NILP (help))
29768 charpos = p;
29769 obj = w->contents;
29774 else if (BUFFERP (obj)
29775 && charpos >= BEGV
29776 && charpos < ZV)
29777 help = Fget_text_property (make_number (charpos), Qhelp_echo,
29778 obj);
29780 if (!NILP (help))
29782 help_echo_string = help;
29783 help_echo_window = window;
29784 help_echo_object = obj;
29785 help_echo_pos = charpos;
29790 #ifdef HAVE_WINDOW_SYSTEM
29791 /* Look for a `pointer' property. */
29792 if (FRAME_WINDOW_P (f) && NILP (pointer))
29794 /* Check overlays first. */
29795 for (i = noverlays - 1; i >= 0 && NILP (pointer); --i)
29796 pointer = Foverlay_get (overlay_vec[i], Qpointer);
29798 if (NILP (pointer))
29800 Lisp_Object obj = glyph->object;
29801 ptrdiff_t charpos = glyph->charpos;
29803 /* Try text properties. */
29804 if (STRINGP (obj)
29805 && charpos >= 0
29806 && charpos < SCHARS (obj))
29808 pointer = Fget_text_property (make_number (charpos),
29809 Qpointer, obj);
29810 if (NILP (pointer))
29812 /* If the string itself doesn't specify a pointer,
29813 see if the buffer text ``under'' it does. */
29814 struct glyph_row *r
29815 = MATRIX_ROW (w->current_matrix, vpos);
29816 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
29817 ptrdiff_t p = string_buffer_position (obj, start);
29818 if (p > 0)
29819 pointer = Fget_char_property (make_number (p),
29820 Qpointer, w->contents);
29823 else if (BUFFERP (obj)
29824 && charpos >= BEGV
29825 && charpos < ZV)
29826 pointer = Fget_text_property (make_number (charpos),
29827 Qpointer, obj);
29830 #endif /* HAVE_WINDOW_SYSTEM */
29832 BEGV = obegv;
29833 ZV = ozv;
29834 current_buffer = obuf;
29835 SAFE_FREE ();
29838 set_cursor:
29840 #ifdef HAVE_WINDOW_SYSTEM
29841 if (FRAME_WINDOW_P (f))
29842 define_frame_cursor1 (f, cursor, pointer);
29843 #else
29844 /* This is here to prevent a compiler error, about "label at end of
29845 compound statement". */
29846 return;
29847 #endif
29851 /* EXPORT for RIF:
29852 Clear any mouse-face on window W. This function is part of the
29853 redisplay interface, and is called from try_window_id and similar
29854 functions to ensure the mouse-highlight is off. */
29856 void
29857 x_clear_window_mouse_face (struct window *w)
29859 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (XFRAME (w->frame));
29860 Lisp_Object window;
29862 block_input ();
29863 XSETWINDOW (window, w);
29864 if (EQ (window, hlinfo->mouse_face_window))
29865 clear_mouse_face (hlinfo);
29866 unblock_input ();
29870 /* EXPORT:
29871 Just discard the mouse face information for frame F, if any.
29872 This is used when the size of F is changed. */
29874 void
29875 cancel_mouse_face (struct frame *f)
29877 Lisp_Object window;
29878 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
29880 window = hlinfo->mouse_face_window;
29881 if (! NILP (window) && XFRAME (XWINDOW (window)->frame) == f)
29882 reset_mouse_highlight (hlinfo);
29887 /***********************************************************************
29888 Exposure Events
29889 ***********************************************************************/
29891 #ifdef HAVE_WINDOW_SYSTEM
29893 /* Redraw the part of glyph row area AREA of glyph row ROW on window W
29894 which intersects rectangle R. R is in window-relative coordinates. */
29896 static void
29897 expose_area (struct window *w, struct glyph_row *row, XRectangle *r,
29898 enum glyph_row_area area)
29900 struct glyph *first = row->glyphs[area];
29901 struct glyph *end = row->glyphs[area] + row->used[area];
29902 struct glyph *last;
29903 int first_x, start_x, x;
29905 if (area == TEXT_AREA && row->fill_line_p)
29906 /* If row extends face to end of line write the whole line. */
29907 draw_glyphs (w, 0, row, area,
29908 0, row->used[area],
29909 DRAW_NORMAL_TEXT, 0);
29910 else
29912 /* Set START_X to the window-relative start position for drawing glyphs of
29913 AREA. The first glyph of the text area can be partially visible.
29914 The first glyphs of other areas cannot. */
29915 start_x = window_box_left_offset (w, area);
29916 x = start_x;
29917 if (area == TEXT_AREA)
29918 x += row->x;
29920 /* Find the first glyph that must be redrawn. */
29921 while (first < end
29922 && x + first->pixel_width < r->x)
29924 x += first->pixel_width;
29925 ++first;
29928 /* Find the last one. */
29929 last = first;
29930 first_x = x;
29931 while (last < end
29932 && x < r->x + r->width)
29934 x += last->pixel_width;
29935 ++last;
29938 /* Repaint. */
29939 if (last > first)
29940 draw_glyphs (w, first_x - start_x, row, area,
29941 first - row->glyphs[area], last - row->glyphs[area],
29942 DRAW_NORMAL_TEXT, 0);
29947 /* Redraw the parts of the glyph row ROW on window W intersecting
29948 rectangle R. R is in window-relative coordinates. Value is
29949 non-zero if mouse-face was overwritten. */
29951 static int
29952 expose_line (struct window *w, struct glyph_row *row, XRectangle *r)
29954 eassert (row->enabled_p);
29956 if (row->mode_line_p || w->pseudo_window_p)
29957 draw_glyphs (w, 0, row, TEXT_AREA,
29958 0, row->used[TEXT_AREA],
29959 DRAW_NORMAL_TEXT, 0);
29960 else
29962 if (row->used[LEFT_MARGIN_AREA])
29963 expose_area (w, row, r, LEFT_MARGIN_AREA);
29964 if (row->used[TEXT_AREA])
29965 expose_area (w, row, r, TEXT_AREA);
29966 if (row->used[RIGHT_MARGIN_AREA])
29967 expose_area (w, row, r, RIGHT_MARGIN_AREA);
29968 draw_row_fringe_bitmaps (w, row);
29971 return row->mouse_face_p;
29975 /* Redraw those parts of glyphs rows during expose event handling that
29976 overlap other rows. Redrawing of an exposed line writes over parts
29977 of lines overlapping that exposed line; this function fixes that.
29979 W is the window being exposed. FIRST_OVERLAPPING_ROW is the first
29980 row in W's current matrix that is exposed and overlaps other rows.
29981 LAST_OVERLAPPING_ROW is the last such row. */
29983 static void
29984 expose_overlaps (struct window *w,
29985 struct glyph_row *first_overlapping_row,
29986 struct glyph_row *last_overlapping_row,
29987 XRectangle *r)
29989 struct glyph_row *row;
29991 for (row = first_overlapping_row; row <= last_overlapping_row; ++row)
29992 if (row->overlapping_p)
29994 eassert (row->enabled_p && !row->mode_line_p);
29996 row->clip = r;
29997 if (row->used[LEFT_MARGIN_AREA])
29998 x_fix_overlapping_area (w, row, LEFT_MARGIN_AREA, OVERLAPS_BOTH);
30000 if (row->used[TEXT_AREA])
30001 x_fix_overlapping_area (w, row, TEXT_AREA, OVERLAPS_BOTH);
30003 if (row->used[RIGHT_MARGIN_AREA])
30004 x_fix_overlapping_area (w, row, RIGHT_MARGIN_AREA, OVERLAPS_BOTH);
30005 row->clip = NULL;
30010 /* Return non-zero if W's cursor intersects rectangle R. */
30012 static int
30013 phys_cursor_in_rect_p (struct window *w, XRectangle *r)
30015 XRectangle cr, result;
30016 struct glyph *cursor_glyph;
30017 struct glyph_row *row;
30019 if (w->phys_cursor.vpos >= 0
30020 && w->phys_cursor.vpos < w->current_matrix->nrows
30021 && (row = MATRIX_ROW (w->current_matrix, w->phys_cursor.vpos),
30022 row->enabled_p)
30023 && row->cursor_in_fringe_p)
30025 /* Cursor is in the fringe. */
30026 cr.x = window_box_right_offset (w,
30027 (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
30028 ? RIGHT_MARGIN_AREA
30029 : TEXT_AREA));
30030 cr.y = row->y;
30031 cr.width = WINDOW_RIGHT_FRINGE_WIDTH (w);
30032 cr.height = row->height;
30033 return x_intersect_rectangles (&cr, r, &result);
30036 cursor_glyph = get_phys_cursor_glyph (w);
30037 if (cursor_glyph)
30039 /* r is relative to W's box, but w->phys_cursor.x is relative
30040 to left edge of W's TEXT area. Adjust it. */
30041 cr.x = window_box_left_offset (w, TEXT_AREA) + w->phys_cursor.x;
30042 cr.y = w->phys_cursor.y;
30043 cr.width = cursor_glyph->pixel_width;
30044 cr.height = w->phys_cursor_height;
30045 /* ++KFS: W32 version used W32-specific IntersectRect here, but
30046 I assume the effect is the same -- and this is portable. */
30047 return x_intersect_rectangles (&cr, r, &result);
30049 /* If we don't understand the format, pretend we're not in the hot-spot. */
30050 return 0;
30054 /* EXPORT:
30055 Draw a vertical window border to the right of window W if W doesn't
30056 have vertical scroll bars. */
30058 void
30059 x_draw_vertical_border (struct window *w)
30061 struct frame *f = XFRAME (WINDOW_FRAME (w));
30063 /* We could do better, if we knew what type of scroll-bar the adjacent
30064 windows (on either side) have... But we don't :-(
30065 However, I think this works ok. ++KFS 2003-04-25 */
30067 /* Redraw borders between horizontally adjacent windows. Don't
30068 do it for frames with vertical scroll bars because either the
30069 right scroll bar of a window, or the left scroll bar of its
30070 neighbor will suffice as a border. */
30071 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f) || FRAME_RIGHT_DIVIDER_WIDTH (f))
30072 return;
30074 /* Note: It is necessary to redraw both the left and the right
30075 borders, for when only this single window W is being
30076 redisplayed. */
30077 if (!WINDOW_RIGHTMOST_P (w)
30078 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w))
30080 int x0, x1, y0, y1;
30082 window_box_edges (w, &x0, &y0, &x1, &y1);
30083 y1 -= 1;
30085 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
30086 x1 -= 1;
30088 FRAME_RIF (f)->draw_vertical_window_border (w, x1, y0, y1);
30091 if (!WINDOW_LEFTMOST_P (w)
30092 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w))
30094 int x0, x1, y0, y1;
30096 window_box_edges (w, &x0, &y0, &x1, &y1);
30097 y1 -= 1;
30099 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
30100 x0 -= 1;
30102 FRAME_RIF (f)->draw_vertical_window_border (w, x0, y0, y1);
30107 /* Draw window dividers for window W. */
30109 void
30110 x_draw_right_divider (struct window *w)
30112 struct frame *f = WINDOW_XFRAME (w);
30114 if (w->mini || w->pseudo_window_p)
30115 return;
30116 else if (WINDOW_RIGHT_DIVIDER_WIDTH (w))
30118 int x0 = WINDOW_RIGHT_EDGE_X (w) - WINDOW_RIGHT_DIVIDER_WIDTH (w);
30119 int x1 = WINDOW_RIGHT_EDGE_X (w);
30120 int y0 = WINDOW_TOP_EDGE_Y (w);
30121 /* The bottom divider prevails. */
30122 int y1 = WINDOW_BOTTOM_EDGE_Y (w) - WINDOW_BOTTOM_DIVIDER_WIDTH (w);
30124 FRAME_RIF (f)->draw_window_divider (w, x0, x1, y0, y1);
30128 static void
30129 x_draw_bottom_divider (struct window *w)
30131 struct frame *f = XFRAME (WINDOW_FRAME (w));
30133 if (w->mini || w->pseudo_window_p)
30134 return;
30135 else if (WINDOW_BOTTOM_DIVIDER_WIDTH (w))
30137 int x0 = WINDOW_LEFT_EDGE_X (w);
30138 int x1 = WINDOW_RIGHT_EDGE_X (w);
30139 int y0 = WINDOW_BOTTOM_EDGE_Y (w) - WINDOW_BOTTOM_DIVIDER_WIDTH (w);
30140 int y1 = WINDOW_BOTTOM_EDGE_Y (w);
30142 FRAME_RIF (f)->draw_window_divider (w, x0, x1, y0, y1);
30146 /* Redraw the part of window W intersection rectangle FR. Pixel
30147 coordinates in FR are frame-relative. Call this function with
30148 input blocked. Value is non-zero if the exposure overwrites
30149 mouse-face. */
30151 static int
30152 expose_window (struct window *w, XRectangle *fr)
30154 struct frame *f = XFRAME (w->frame);
30155 XRectangle wr, r;
30156 int mouse_face_overwritten_p = 0;
30158 /* If window is not yet fully initialized, do nothing. This can
30159 happen when toolkit scroll bars are used and a window is split.
30160 Reconfiguring the scroll bar will generate an expose for a newly
30161 created window. */
30162 if (w->current_matrix == NULL)
30163 return 0;
30165 /* When we're currently updating the window, display and current
30166 matrix usually don't agree. Arrange for a thorough display
30167 later. */
30168 if (w->must_be_updated_p)
30170 SET_FRAME_GARBAGED (f);
30171 return 0;
30174 /* Frame-relative pixel rectangle of W. */
30175 wr.x = WINDOW_LEFT_EDGE_X (w);
30176 wr.y = WINDOW_TOP_EDGE_Y (w);
30177 wr.width = WINDOW_PIXEL_WIDTH (w);
30178 wr.height = WINDOW_PIXEL_HEIGHT (w);
30180 if (x_intersect_rectangles (fr, &wr, &r))
30182 int yb = window_text_bottom_y (w);
30183 struct glyph_row *row;
30184 int cursor_cleared_p, phys_cursor_on_p;
30185 struct glyph_row *first_overlapping_row, *last_overlapping_row;
30187 TRACE ((stderr, "expose_window (%d, %d, %d, %d)\n",
30188 r.x, r.y, r.width, r.height));
30190 /* Convert to window coordinates. */
30191 r.x -= WINDOW_LEFT_EDGE_X (w);
30192 r.y -= WINDOW_TOP_EDGE_Y (w);
30194 /* Turn off the cursor. */
30195 if (!w->pseudo_window_p
30196 && phys_cursor_in_rect_p (w, &r))
30198 x_clear_cursor (w);
30199 cursor_cleared_p = 1;
30201 else
30202 cursor_cleared_p = 0;
30204 /* If the row containing the cursor extends face to end of line,
30205 then expose_area might overwrite the cursor outside the
30206 rectangle and thus notice_overwritten_cursor might clear
30207 w->phys_cursor_on_p. We remember the original value and
30208 check later if it is changed. */
30209 phys_cursor_on_p = w->phys_cursor_on_p;
30211 /* Update lines intersecting rectangle R. */
30212 first_overlapping_row = last_overlapping_row = NULL;
30213 for (row = w->current_matrix->rows;
30214 row->enabled_p;
30215 ++row)
30217 int y0 = row->y;
30218 int y1 = MATRIX_ROW_BOTTOM_Y (row);
30220 if ((y0 >= r.y && y0 < r.y + r.height)
30221 || (y1 > r.y && y1 < r.y + r.height)
30222 || (r.y >= y0 && r.y < y1)
30223 || (r.y + r.height > y0 && r.y + r.height < y1))
30225 /* A header line may be overlapping, but there is no need
30226 to fix overlapping areas for them. KFS 2005-02-12 */
30227 if (row->overlapping_p && !row->mode_line_p)
30229 if (first_overlapping_row == NULL)
30230 first_overlapping_row = row;
30231 last_overlapping_row = row;
30234 row->clip = fr;
30235 if (expose_line (w, row, &r))
30236 mouse_face_overwritten_p = 1;
30237 row->clip = NULL;
30239 else if (row->overlapping_p)
30241 /* We must redraw a row overlapping the exposed area. */
30242 if (y0 < r.y
30243 ? y0 + row->phys_height > r.y
30244 : y0 + row->ascent - row->phys_ascent < r.y +r.height)
30246 if (first_overlapping_row == NULL)
30247 first_overlapping_row = row;
30248 last_overlapping_row = row;
30252 if (y1 >= yb)
30253 break;
30256 /* Display the mode line if there is one. */
30257 if (WINDOW_WANTS_MODELINE_P (w)
30258 && (row = MATRIX_MODE_LINE_ROW (w->current_matrix),
30259 row->enabled_p)
30260 && row->y < r.y + r.height)
30262 if (expose_line (w, row, &r))
30263 mouse_face_overwritten_p = 1;
30266 if (!w->pseudo_window_p)
30268 /* Fix the display of overlapping rows. */
30269 if (first_overlapping_row)
30270 expose_overlaps (w, first_overlapping_row, last_overlapping_row,
30271 fr);
30273 /* Draw border between windows. */
30274 if (WINDOW_RIGHT_DIVIDER_WIDTH (w))
30275 x_draw_right_divider (w);
30276 else
30277 x_draw_vertical_border (w);
30279 if (WINDOW_BOTTOM_DIVIDER_WIDTH (w))
30280 x_draw_bottom_divider (w);
30282 /* Turn the cursor on again. */
30283 if (cursor_cleared_p
30284 || (phys_cursor_on_p && !w->phys_cursor_on_p))
30285 update_window_cursor (w, 1);
30289 return mouse_face_overwritten_p;
30294 /* Redraw (parts) of all windows in the window tree rooted at W that
30295 intersect R. R contains frame pixel coordinates. Value is
30296 non-zero if the exposure overwrites mouse-face. */
30298 static int
30299 expose_window_tree (struct window *w, XRectangle *r)
30301 struct frame *f = XFRAME (w->frame);
30302 int mouse_face_overwritten_p = 0;
30304 while (w && !FRAME_GARBAGED_P (f))
30306 if (WINDOWP (w->contents))
30307 mouse_face_overwritten_p
30308 |= expose_window_tree (XWINDOW (w->contents), r);
30309 else
30310 mouse_face_overwritten_p |= expose_window (w, r);
30312 w = NILP (w->next) ? NULL : XWINDOW (w->next);
30315 return mouse_face_overwritten_p;
30319 /* EXPORT:
30320 Redisplay an exposed area of frame F. X and Y are the upper-left
30321 corner of the exposed rectangle. W and H are width and height of
30322 the exposed area. All are pixel values. W or H zero means redraw
30323 the entire frame. */
30325 void
30326 expose_frame (struct frame *f, int x, int y, int w, int h)
30328 XRectangle r;
30329 int mouse_face_overwritten_p = 0;
30331 TRACE ((stderr, "expose_frame "));
30333 /* No need to redraw if frame will be redrawn soon. */
30334 if (FRAME_GARBAGED_P (f))
30336 TRACE ((stderr, " garbaged\n"));
30337 return;
30340 /* If basic faces haven't been realized yet, there is no point in
30341 trying to redraw anything. This can happen when we get an expose
30342 event while Emacs is starting, e.g. by moving another window. */
30343 if (FRAME_FACE_CACHE (f) == NULL
30344 || FRAME_FACE_CACHE (f)->used < BASIC_FACE_ID_SENTINEL)
30346 TRACE ((stderr, " no faces\n"));
30347 return;
30350 if (w == 0 || h == 0)
30352 r.x = r.y = 0;
30353 r.width = FRAME_TEXT_WIDTH (f);
30354 r.height = FRAME_TEXT_HEIGHT (f);
30356 else
30358 r.x = x;
30359 r.y = y;
30360 r.width = w;
30361 r.height = h;
30364 TRACE ((stderr, "(%d, %d, %d, %d)\n", r.x, r.y, r.width, r.height));
30365 mouse_face_overwritten_p = expose_window_tree (XWINDOW (f->root_window), &r);
30367 #if ! defined (USE_GTK) && ! defined (HAVE_NS)
30368 if (WINDOWP (f->tool_bar_window))
30369 mouse_face_overwritten_p
30370 |= expose_window (XWINDOW (f->tool_bar_window), &r);
30371 #endif
30373 #ifdef HAVE_X_WINDOWS
30374 #ifndef MSDOS
30375 #if ! defined (USE_X_TOOLKIT) && ! defined (USE_GTK)
30376 if (WINDOWP (f->menu_bar_window))
30377 mouse_face_overwritten_p
30378 |= expose_window (XWINDOW (f->menu_bar_window), &r);
30379 #endif /* not USE_X_TOOLKIT and not USE_GTK */
30380 #endif
30381 #endif
30383 /* Some window managers support a focus-follows-mouse style with
30384 delayed raising of frames. Imagine a partially obscured frame,
30385 and moving the mouse into partially obscured mouse-face on that
30386 frame. The visible part of the mouse-face will be highlighted,
30387 then the WM raises the obscured frame. With at least one WM, KDE
30388 2.1, Emacs is not getting any event for the raising of the frame
30389 (even tried with SubstructureRedirectMask), only Expose events.
30390 These expose events will draw text normally, i.e. not
30391 highlighted. Which means we must redo the highlight here.
30392 Subsume it under ``we love X''. --gerd 2001-08-15 */
30393 /* Included in Windows version because Windows most likely does not
30394 do the right thing if any third party tool offers
30395 focus-follows-mouse with delayed raise. --jason 2001-10-12 */
30396 if (mouse_face_overwritten_p && !FRAME_GARBAGED_P (f))
30398 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
30399 if (f == hlinfo->mouse_face_mouse_frame)
30401 int mouse_x = hlinfo->mouse_face_mouse_x;
30402 int mouse_y = hlinfo->mouse_face_mouse_y;
30403 clear_mouse_face (hlinfo);
30404 note_mouse_highlight (f, mouse_x, mouse_y);
30410 /* EXPORT:
30411 Determine the intersection of two rectangles R1 and R2. Return
30412 the intersection in *RESULT. Value is non-zero if RESULT is not
30413 empty. */
30416 x_intersect_rectangles (XRectangle *r1, XRectangle *r2, XRectangle *result)
30418 XRectangle *left, *right;
30419 XRectangle *upper, *lower;
30420 int intersection_p = 0;
30422 /* Rearrange so that R1 is the left-most rectangle. */
30423 if (r1->x < r2->x)
30424 left = r1, right = r2;
30425 else
30426 left = r2, right = r1;
30428 /* X0 of the intersection is right.x0, if this is inside R1,
30429 otherwise there is no intersection. */
30430 if (right->x <= left->x + left->width)
30432 result->x = right->x;
30434 /* The right end of the intersection is the minimum of
30435 the right ends of left and right. */
30436 result->width = (min (left->x + left->width, right->x + right->width)
30437 - result->x);
30439 /* Same game for Y. */
30440 if (r1->y < r2->y)
30441 upper = r1, lower = r2;
30442 else
30443 upper = r2, lower = r1;
30445 /* The upper end of the intersection is lower.y0, if this is inside
30446 of upper. Otherwise, there is no intersection. */
30447 if (lower->y <= upper->y + upper->height)
30449 result->y = lower->y;
30451 /* The lower end of the intersection is the minimum of the lower
30452 ends of upper and lower. */
30453 result->height = (min (lower->y + lower->height,
30454 upper->y + upper->height)
30455 - result->y);
30456 intersection_p = 1;
30460 return intersection_p;
30463 #endif /* HAVE_WINDOW_SYSTEM */
30466 /***********************************************************************
30467 Initialization
30468 ***********************************************************************/
30470 void
30471 syms_of_xdisp (void)
30473 Vwith_echo_area_save_vector = Qnil;
30474 staticpro (&Vwith_echo_area_save_vector);
30476 Vmessage_stack = Qnil;
30477 staticpro (&Vmessage_stack);
30479 /* Non-nil means don't actually do any redisplay. */
30480 DEFSYM (Qinhibit_redisplay, "inhibit-redisplay");
30482 DEFSYM (Qredisplay_internal, "redisplay_internal (C function)");
30484 message_dolog_marker1 = Fmake_marker ();
30485 staticpro (&message_dolog_marker1);
30486 message_dolog_marker2 = Fmake_marker ();
30487 staticpro (&message_dolog_marker2);
30488 message_dolog_marker3 = Fmake_marker ();
30489 staticpro (&message_dolog_marker3);
30491 #ifdef GLYPH_DEBUG
30492 defsubr (&Sdump_frame_glyph_matrix);
30493 defsubr (&Sdump_glyph_matrix);
30494 defsubr (&Sdump_glyph_row);
30495 defsubr (&Sdump_tool_bar_row);
30496 defsubr (&Strace_redisplay);
30497 defsubr (&Strace_to_stderr);
30498 #endif
30499 #ifdef HAVE_WINDOW_SYSTEM
30500 defsubr (&Stool_bar_height);
30501 defsubr (&Slookup_image_map);
30502 #endif
30503 defsubr (&Sline_pixel_height);
30504 defsubr (&Sformat_mode_line);
30505 defsubr (&Sinvisible_p);
30506 defsubr (&Scurrent_bidi_paragraph_direction);
30507 defsubr (&Swindow_text_pixel_size);
30508 defsubr (&Smove_point_visually);
30509 defsubr (&Sbidi_find_overridden_directionality);
30511 DEFSYM (Qmenu_bar_update_hook, "menu-bar-update-hook");
30512 DEFSYM (Qoverriding_terminal_local_map, "overriding-terminal-local-map");
30513 DEFSYM (Qoverriding_local_map, "overriding-local-map");
30514 DEFSYM (Qwindow_scroll_functions, "window-scroll-functions");
30515 DEFSYM (Qwindow_text_change_functions, "window-text-change-functions");
30516 DEFSYM (Qredisplay_end_trigger_functions, "redisplay-end-trigger-functions");
30517 DEFSYM (Qinhibit_point_motion_hooks, "inhibit-point-motion-hooks");
30518 DEFSYM (Qeval, "eval");
30519 DEFSYM (QCdata, ":data");
30521 /* Names of text properties relevant for redisplay. */
30522 DEFSYM (Qdisplay, "display");
30523 DEFSYM (Qspace_width, "space-width");
30524 DEFSYM (Qraise, "raise");
30525 DEFSYM (Qslice, "slice");
30526 DEFSYM (Qspace, "space");
30527 DEFSYM (Qmargin, "margin");
30528 DEFSYM (Qpointer, "pointer");
30529 DEFSYM (Qleft_margin, "left-margin");
30530 DEFSYM (Qright_margin, "right-margin");
30531 DEFSYM (Qcenter, "center");
30532 DEFSYM (Qline_height, "line-height");
30533 DEFSYM (QCalign_to, ":align-to");
30534 DEFSYM (QCrelative_width, ":relative-width");
30535 DEFSYM (QCrelative_height, ":relative-height");
30536 DEFSYM (QCeval, ":eval");
30537 DEFSYM (QCpropertize, ":propertize");
30538 DEFSYM (QCfile, ":file");
30539 DEFSYM (Qfontified, "fontified");
30540 DEFSYM (Qfontification_functions, "fontification-functions");
30542 /* Name of the face used to highlight trailing whitespace. */
30543 DEFSYM (Qtrailing_whitespace, "trailing-whitespace");
30545 /* Name and number of the face used to highlight escape glyphs. */
30546 DEFSYM (Qescape_glyph, "escape-glyph");
30548 /* Name and number of the face used to highlight non-breaking spaces. */
30549 DEFSYM (Qnobreak_space, "nobreak-space");
30551 /* The symbol 'image' which is the car of the lists used to represent
30552 images in Lisp. Also a tool bar style. */
30553 DEFSYM (Qimage, "image");
30555 /* Tool bar styles. */
30556 DEFSYM (Qtext, "text");
30557 DEFSYM (Qboth, "both");
30558 DEFSYM (Qboth_horiz, "both-horiz");
30559 DEFSYM (Qtext_image_horiz, "text-image-horiz");
30561 /* The image map types. */
30562 DEFSYM (QCmap, ":map");
30563 DEFSYM (QCpointer, ":pointer");
30564 DEFSYM (Qrect, "rect");
30565 DEFSYM (Qcircle, "circle");
30566 DEFSYM (Qpoly, "poly");
30568 /* The symbol `inhibit-menubar-update' and its DEFVAR_BOOL variable. */
30569 DEFSYM (Qinhibit_menubar_update, "inhibit-menubar-update");
30570 DEFSYM (Qmessage_truncate_lines, "message-truncate-lines");
30572 DEFSYM (Qgrow_only, "grow-only");
30573 DEFSYM (Qinhibit_eval_during_redisplay, "inhibit-eval-during-redisplay");
30574 DEFSYM (Qposition, "position");
30575 DEFSYM (Qbuffer_position, "buffer-position");
30576 DEFSYM (Qobject, "object");
30578 /* Cursor shapes. */
30579 DEFSYM (Qbar, "bar");
30580 DEFSYM (Qhbar, "hbar");
30581 DEFSYM (Qbox, "box");
30582 DEFSYM (Qhollow, "hollow");
30584 /* Pointer shapes. */
30585 DEFSYM (Qhand, "hand");
30586 DEFSYM (Qarrow, "arrow");
30587 /* also Qtext */
30589 DEFSYM (Qinhibit_free_realized_faces, "inhibit-free-realized-faces");
30591 list_of_error = list1 (list2 (intern_c_string ("error"),
30592 intern_c_string ("void-variable")));
30593 staticpro (&list_of_error);
30595 /* Values of those variables at last redisplay are stored as
30596 properties on 'overlay-arrow-position' symbol. However, if
30597 Voverlay_arrow_position is a marker, last-arrow-position is its
30598 numerical position. */
30599 DEFSYM (Qlast_arrow_position, "last-arrow-position");
30600 DEFSYM (Qlast_arrow_string, "last-arrow-string");
30602 /* Alternative overlay-arrow-string and overlay-arrow-bitmap
30603 properties on a symbol in overlay-arrow-variable-list. */
30604 DEFSYM (Qoverlay_arrow_string, "overlay-arrow-string");
30605 DEFSYM (Qoverlay_arrow_bitmap, "overlay-arrow-bitmap");
30607 echo_buffer[0] = echo_buffer[1] = Qnil;
30608 staticpro (&echo_buffer[0]);
30609 staticpro (&echo_buffer[1]);
30611 echo_area_buffer[0] = echo_area_buffer[1] = Qnil;
30612 staticpro (&echo_area_buffer[0]);
30613 staticpro (&echo_area_buffer[1]);
30615 Vmessages_buffer_name = build_pure_c_string ("*Messages*");
30616 staticpro (&Vmessages_buffer_name);
30618 mode_line_proptrans_alist = Qnil;
30619 staticpro (&mode_line_proptrans_alist);
30620 mode_line_string_list = Qnil;
30621 staticpro (&mode_line_string_list);
30622 mode_line_string_face = Qnil;
30623 staticpro (&mode_line_string_face);
30624 mode_line_string_face_prop = Qnil;
30625 staticpro (&mode_line_string_face_prop);
30626 Vmode_line_unwind_vector = Qnil;
30627 staticpro (&Vmode_line_unwind_vector);
30629 DEFSYM (Qmode_line_default_help_echo, "mode-line-default-help-echo");
30631 help_echo_string = Qnil;
30632 staticpro (&help_echo_string);
30633 help_echo_object = Qnil;
30634 staticpro (&help_echo_object);
30635 help_echo_window = Qnil;
30636 staticpro (&help_echo_window);
30637 previous_help_echo_string = Qnil;
30638 staticpro (&previous_help_echo_string);
30639 help_echo_pos = -1;
30641 DEFSYM (Qright_to_left, "right-to-left");
30642 DEFSYM (Qleft_to_right, "left-to-right");
30643 defsubr (&Sbidi_resolved_levels);
30645 #ifdef HAVE_WINDOW_SYSTEM
30646 DEFVAR_BOOL ("x-stretch-cursor", x_stretch_cursor_p,
30647 doc: /* Non-nil means draw block cursor as wide as the glyph under it.
30648 For example, if a block cursor is over a tab, it will be drawn as
30649 wide as that tab on the display. */);
30650 x_stretch_cursor_p = 0;
30651 #endif
30653 DEFVAR_LISP ("show-trailing-whitespace", Vshow_trailing_whitespace,
30654 doc: /* Non-nil means highlight trailing whitespace.
30655 The face used for trailing whitespace is `trailing-whitespace'. */);
30656 Vshow_trailing_whitespace = Qnil;
30658 DEFVAR_LISP ("nobreak-char-display", Vnobreak_char_display,
30659 doc: /* Control highlighting of non-ASCII space and hyphen chars.
30660 If the value is t, Emacs highlights non-ASCII chars which have the
30661 same appearance as an ASCII space or hyphen, using the `nobreak-space'
30662 or `escape-glyph' face respectively.
30664 U+00A0 (no-break space), U+00AD (soft hyphen), U+2010 (hyphen), and
30665 U+2011 (non-breaking hyphen) are affected.
30667 Any other non-nil value means to display these characters as a escape
30668 glyph followed by an ordinary space or hyphen.
30670 A value of nil means no special handling of these characters. */);
30671 Vnobreak_char_display = Qt;
30673 DEFVAR_LISP ("void-text-area-pointer", Vvoid_text_area_pointer,
30674 doc: /* The pointer shape to show in void text areas.
30675 A value of nil means to show the text pointer. Other options are
30676 `arrow', `text', `hand', `vdrag', `hdrag', `nhdrag', `modeline', and
30677 `hourglass'. */);
30678 Vvoid_text_area_pointer = Qarrow;
30680 DEFVAR_LISP ("inhibit-redisplay", Vinhibit_redisplay,
30681 doc: /* Non-nil means don't actually do any redisplay.
30682 This is used for internal purposes. */);
30683 Vinhibit_redisplay = Qnil;
30685 DEFVAR_LISP ("global-mode-string", Vglobal_mode_string,
30686 doc: /* String (or mode line construct) included (normally) in `mode-line-format'. */);
30687 Vglobal_mode_string = Qnil;
30689 DEFVAR_LISP ("overlay-arrow-position", Voverlay_arrow_position,
30690 doc: /* Marker for where to display an arrow on top of the buffer text.
30691 This must be the beginning of a line in order to work.
30692 See also `overlay-arrow-string'. */);
30693 Voverlay_arrow_position = Qnil;
30695 DEFVAR_LISP ("overlay-arrow-string", Voverlay_arrow_string,
30696 doc: /* String to display as an arrow in non-window frames.
30697 See also `overlay-arrow-position'. */);
30698 Voverlay_arrow_string = build_pure_c_string ("=>");
30700 DEFVAR_LISP ("overlay-arrow-variable-list", Voverlay_arrow_variable_list,
30701 doc: /* List of variables (symbols) which hold markers for overlay arrows.
30702 The symbols on this list are examined during redisplay to determine
30703 where to display overlay arrows. */);
30704 Voverlay_arrow_variable_list
30705 = list1 (intern_c_string ("overlay-arrow-position"));
30707 DEFVAR_INT ("scroll-step", emacs_scroll_step,
30708 doc: /* The number of lines to try scrolling a window by when point moves out.
30709 If that fails to bring point back on frame, point is centered instead.
30710 If this is zero, point is always centered after it moves off frame.
30711 If you want scrolling to always be a line at a time, you should set
30712 `scroll-conservatively' to a large value rather than set this to 1. */);
30714 DEFVAR_INT ("scroll-conservatively", scroll_conservatively,
30715 doc: /* Scroll up to this many lines, to bring point back on screen.
30716 If point moves off-screen, redisplay will scroll by up to
30717 `scroll-conservatively' lines in order to bring point just barely
30718 onto the screen again. If that cannot be done, then redisplay
30719 recenters point as usual.
30721 If the value is greater than 100, redisplay will never recenter point,
30722 but will always scroll just enough text to bring point into view, even
30723 if you move far away.
30725 A value of zero means always recenter point if it moves off screen. */);
30726 scroll_conservatively = 0;
30728 DEFVAR_INT ("scroll-margin", scroll_margin,
30729 doc: /* Number of lines of margin at the top and bottom of a window.
30730 Recenter the window whenever point gets within this many lines
30731 of the top or bottom of the window. */);
30732 scroll_margin = 0;
30734 DEFVAR_LISP ("display-pixels-per-inch", Vdisplay_pixels_per_inch,
30735 doc: /* Pixels per inch value for non-window system displays.
30736 Value is a number or a cons (WIDTH-DPI . HEIGHT-DPI). */);
30737 Vdisplay_pixels_per_inch = make_float (72.0);
30739 #ifdef GLYPH_DEBUG
30740 DEFVAR_INT ("debug-end-pos", debug_end_pos, doc: /* Don't ask. */);
30741 #endif
30743 DEFVAR_LISP ("truncate-partial-width-windows",
30744 Vtruncate_partial_width_windows,
30745 doc: /* Non-nil means truncate lines in windows narrower than the frame.
30746 For an integer value, truncate lines in each window narrower than the
30747 full frame width, provided the window width is less than that integer;
30748 otherwise, respect the value of `truncate-lines'.
30750 For any other non-nil value, truncate lines in all windows that do
30751 not span the full frame width.
30753 A value of nil means to respect the value of `truncate-lines'.
30755 If `word-wrap' is enabled, you might want to reduce this. */);
30756 Vtruncate_partial_width_windows = make_number (50);
30758 DEFVAR_LISP ("line-number-display-limit", Vline_number_display_limit,
30759 doc: /* Maximum buffer size for which line number should be displayed.
30760 If the buffer is bigger than this, the line number does not appear
30761 in the mode line. A value of nil means no limit. */);
30762 Vline_number_display_limit = Qnil;
30764 DEFVAR_INT ("line-number-display-limit-width",
30765 line_number_display_limit_width,
30766 doc: /* Maximum line width (in characters) for line number display.
30767 If the average length of the lines near point is bigger than this, then the
30768 line number may be omitted from the mode line. */);
30769 line_number_display_limit_width = 200;
30771 DEFVAR_BOOL ("highlight-nonselected-windows", highlight_nonselected_windows,
30772 doc: /* Non-nil means highlight region even in nonselected windows. */);
30773 highlight_nonselected_windows = 0;
30775 DEFVAR_BOOL ("multiple-frames", multiple_frames,
30776 doc: /* Non-nil if more than one frame is visible on this display.
30777 Minibuffer-only frames don't count, but iconified frames do.
30778 This variable is not guaranteed to be accurate except while processing
30779 `frame-title-format' and `icon-title-format'. */);
30781 DEFVAR_LISP ("frame-title-format", Vframe_title_format,
30782 doc: /* Template for displaying the title bar of visible frames.
30783 \(Assuming the window manager supports this feature.)
30785 This variable has the same structure as `mode-line-format', except that
30786 the %c and %l constructs are ignored. It is used only on frames for
30787 which no explicit name has been set \(see `modify-frame-parameters'). */);
30789 DEFVAR_LISP ("icon-title-format", Vicon_title_format,
30790 doc: /* Template for displaying the title bar of an iconified frame.
30791 \(Assuming the window manager supports this feature.)
30792 This variable has the same structure as `mode-line-format' (which see),
30793 and is used only on frames for which no explicit name has been set
30794 \(see `modify-frame-parameters'). */);
30795 Vicon_title_format
30796 = Vframe_title_format
30797 = listn (CONSTYPE_PURE, 3,
30798 intern_c_string ("multiple-frames"),
30799 build_pure_c_string ("%b"),
30800 listn (CONSTYPE_PURE, 4,
30801 empty_unibyte_string,
30802 intern_c_string ("invocation-name"),
30803 build_pure_c_string ("@"),
30804 intern_c_string ("system-name")));
30806 DEFVAR_LISP ("message-log-max", Vmessage_log_max,
30807 doc: /* Maximum number of lines to keep in the message log buffer.
30808 If nil, disable message logging. If t, log messages but don't truncate
30809 the buffer when it becomes large. */);
30810 Vmessage_log_max = make_number (1000);
30812 DEFVAR_LISP ("window-size-change-functions", Vwindow_size_change_functions,
30813 doc: /* Functions called before redisplay, if window sizes have changed.
30814 The value should be a list of functions that take one argument.
30815 Just before redisplay, for each frame, if any of its windows have changed
30816 size since the last redisplay, or have been split or deleted,
30817 all the functions in the list are called, with the frame as argument. */);
30818 Vwindow_size_change_functions = Qnil;
30820 DEFVAR_LISP ("window-scroll-functions", Vwindow_scroll_functions,
30821 doc: /* List of functions to call before redisplaying a window with scrolling.
30822 Each function is called with two arguments, the window and its new
30823 display-start position.
30824 These functions are called whenever the `window-start' marker is modified,
30825 either to point into another buffer (e.g. via `set-window-buffer') or another
30826 place in the same buffer.
30827 Note that the value of `window-end' is not valid when these functions are
30828 called.
30830 Warning: Do not use this feature to alter the way the window
30831 is scrolled. It is not designed for that, and such use probably won't
30832 work. */);
30833 Vwindow_scroll_functions = Qnil;
30835 DEFVAR_LISP ("window-text-change-functions",
30836 Vwindow_text_change_functions,
30837 doc: /* Functions to call in redisplay when text in the window might change. */);
30838 Vwindow_text_change_functions = Qnil;
30840 DEFVAR_LISP ("redisplay-end-trigger-functions", Vredisplay_end_trigger_functions,
30841 doc: /* Functions called when redisplay of a window reaches the end trigger.
30842 Each function is called with two arguments, the window and the end trigger value.
30843 See `set-window-redisplay-end-trigger'. */);
30844 Vredisplay_end_trigger_functions = Qnil;
30846 DEFVAR_LISP ("mouse-autoselect-window", Vmouse_autoselect_window,
30847 doc: /* Non-nil means autoselect window with mouse pointer.
30848 If nil, do not autoselect windows.
30849 A positive number means delay autoselection by that many seconds: a
30850 window is autoselected only after the mouse has remained in that
30851 window for the duration of the delay.
30852 A negative number has a similar effect, but causes windows to be
30853 autoselected only after the mouse has stopped moving. \(Because of
30854 the way Emacs compares mouse events, you will occasionally wait twice
30855 that time before the window gets selected.\)
30856 Any other value means to autoselect window instantaneously when the
30857 mouse pointer enters it.
30859 Autoselection selects the minibuffer only if it is active, and never
30860 unselects the minibuffer if it is active.
30862 When customizing this variable make sure that the actual value of
30863 `focus-follows-mouse' matches the behavior of your window manager. */);
30864 Vmouse_autoselect_window = Qnil;
30866 DEFVAR_LISP ("auto-resize-tool-bars", Vauto_resize_tool_bars,
30867 doc: /* Non-nil means automatically resize tool-bars.
30868 This dynamically changes the tool-bar's height to the minimum height
30869 that is needed to make all tool-bar items visible.
30870 If value is `grow-only', the tool-bar's height is only increased
30871 automatically; to decrease the tool-bar height, use \\[recenter]. */);
30872 Vauto_resize_tool_bars = Qt;
30874 DEFVAR_BOOL ("auto-raise-tool-bar-buttons", auto_raise_tool_bar_buttons_p,
30875 doc: /* Non-nil means raise tool-bar buttons when the mouse moves over them. */);
30876 auto_raise_tool_bar_buttons_p = 1;
30878 DEFVAR_BOOL ("make-cursor-line-fully-visible", make_cursor_line_fully_visible_p,
30879 doc: /* Non-nil means to scroll (recenter) cursor line if it is not fully visible. */);
30880 make_cursor_line_fully_visible_p = 1;
30882 DEFVAR_LISP ("tool-bar-border", Vtool_bar_border,
30883 doc: /* Border below tool-bar in pixels.
30884 If an integer, use it as the height of the border.
30885 If it is one of `internal-border-width' or `border-width', use the
30886 value of the corresponding frame parameter.
30887 Otherwise, no border is added below the tool-bar. */);
30888 Vtool_bar_border = Qinternal_border_width;
30890 DEFVAR_LISP ("tool-bar-button-margin", Vtool_bar_button_margin,
30891 doc: /* Margin around tool-bar buttons in pixels.
30892 If an integer, use that for both horizontal and vertical margins.
30893 Otherwise, value should be a pair of integers `(HORZ . VERT)' with
30894 HORZ specifying the horizontal margin, and VERT specifying the
30895 vertical margin. */);
30896 Vtool_bar_button_margin = make_number (DEFAULT_TOOL_BAR_BUTTON_MARGIN);
30898 DEFVAR_INT ("tool-bar-button-relief", tool_bar_button_relief,
30899 doc: /* Relief thickness of tool-bar buttons. */);
30900 tool_bar_button_relief = DEFAULT_TOOL_BAR_BUTTON_RELIEF;
30902 DEFVAR_LISP ("tool-bar-style", Vtool_bar_style,
30903 doc: /* Tool bar style to use.
30904 It can be one of
30905 image - show images only
30906 text - show text only
30907 both - show both, text below image
30908 both-horiz - show text to the right of the image
30909 text-image-horiz - show text to the left of the image
30910 any other - use system default or image if no system default.
30912 This variable only affects the GTK+ toolkit version of Emacs. */);
30913 Vtool_bar_style = Qnil;
30915 DEFVAR_INT ("tool-bar-max-label-size", tool_bar_max_label_size,
30916 doc: /* Maximum number of characters a label can have to be shown.
30917 The tool bar style must also show labels for this to have any effect, see
30918 `tool-bar-style'. */);
30919 tool_bar_max_label_size = DEFAULT_TOOL_BAR_LABEL_SIZE;
30921 DEFVAR_LISP ("fontification-functions", Vfontification_functions,
30922 doc: /* List of functions to call to fontify regions of text.
30923 Each function is called with one argument POS. Functions must
30924 fontify a region starting at POS in the current buffer, and give
30925 fontified regions the property `fontified'. */);
30926 Vfontification_functions = Qnil;
30927 Fmake_variable_buffer_local (Qfontification_functions);
30929 DEFVAR_BOOL ("unibyte-display-via-language-environment",
30930 unibyte_display_via_language_environment,
30931 doc: /* Non-nil means display unibyte text according to language environment.
30932 Specifically, this means that raw bytes in the range 160-255 decimal
30933 are displayed by converting them to the equivalent multibyte characters
30934 according to the current language environment. As a result, they are
30935 displayed according to the current fontset.
30937 Note that this variable affects only how these bytes are displayed,
30938 but does not change the fact they are interpreted as raw bytes. */);
30939 unibyte_display_via_language_environment = 0;
30941 DEFVAR_LISP ("max-mini-window-height", Vmax_mini_window_height,
30942 doc: /* Maximum height for resizing mini-windows (the minibuffer and the echo area).
30943 If a float, it specifies a fraction of the mini-window frame's height.
30944 If an integer, it specifies a number of lines. */);
30945 Vmax_mini_window_height = make_float (0.25);
30947 DEFVAR_LISP ("resize-mini-windows", Vresize_mini_windows,
30948 doc: /* How to resize mini-windows (the minibuffer and the echo area).
30949 A value of nil means don't automatically resize mini-windows.
30950 A value of t means resize them to fit the text displayed in them.
30951 A value of `grow-only', the default, means let mini-windows grow only;
30952 they return to their normal size when the minibuffer is closed, or the
30953 echo area becomes empty. */);
30954 Vresize_mini_windows = Qgrow_only;
30956 DEFVAR_LISP ("blink-cursor-alist", Vblink_cursor_alist,
30957 doc: /* Alist specifying how to blink the cursor off.
30958 Each element has the form (ON-STATE . OFF-STATE). Whenever the
30959 `cursor-type' frame-parameter or variable equals ON-STATE,
30960 comparing using `equal', Emacs uses OFF-STATE to specify
30961 how to blink it off. ON-STATE and OFF-STATE are values for
30962 the `cursor-type' frame parameter.
30964 If a frame's ON-STATE has no entry in this list,
30965 the frame's other specifications determine how to blink the cursor off. */);
30966 Vblink_cursor_alist = Qnil;
30968 DEFVAR_BOOL ("auto-hscroll-mode", automatic_hscrolling_p,
30969 doc: /* Allow or disallow automatic horizontal scrolling of windows.
30970 If non-nil, windows are automatically scrolled horizontally to make
30971 point visible. */);
30972 automatic_hscrolling_p = 1;
30973 DEFSYM (Qauto_hscroll_mode, "auto-hscroll-mode");
30975 DEFVAR_INT ("hscroll-margin", hscroll_margin,
30976 doc: /* How many columns away from the window edge point is allowed to get
30977 before automatic hscrolling will horizontally scroll the window. */);
30978 hscroll_margin = 5;
30980 DEFVAR_LISP ("hscroll-step", Vhscroll_step,
30981 doc: /* How many columns to scroll the window when point gets too close to the edge.
30982 When point is less than `hscroll-margin' columns from the window
30983 edge, automatic hscrolling will scroll the window by the amount of columns
30984 determined by this variable. If its value is a positive integer, scroll that
30985 many columns. If it's a positive floating-point number, it specifies the
30986 fraction of the window's width to scroll. If it's nil or zero, point will be
30987 centered horizontally after the scroll. Any other value, including negative
30988 numbers, are treated as if the value were zero.
30990 Automatic hscrolling always moves point outside the scroll margin, so if
30991 point was more than scroll step columns inside the margin, the window will
30992 scroll more than the value given by the scroll step.
30994 Note that the lower bound for automatic hscrolling specified by `scroll-left'
30995 and `scroll-right' overrides this variable's effect. */);
30996 Vhscroll_step = make_number (0);
30998 DEFVAR_BOOL ("message-truncate-lines", message_truncate_lines,
30999 doc: /* If non-nil, messages are truncated instead of resizing the echo area.
31000 Bind this around calls to `message' to let it take effect. */);
31001 message_truncate_lines = 0;
31003 DEFVAR_LISP ("menu-bar-update-hook", Vmenu_bar_update_hook,
31004 doc: /* Normal hook run to update the menu bar definitions.
31005 Redisplay runs this hook before it redisplays the menu bar.
31006 This is used to update menus such as Buffers, whose contents depend on
31007 various data. */);
31008 Vmenu_bar_update_hook = Qnil;
31010 DEFVAR_LISP ("menu-updating-frame", Vmenu_updating_frame,
31011 doc: /* Frame for which we are updating a menu.
31012 The enable predicate for a menu binding should check this variable. */);
31013 Vmenu_updating_frame = Qnil;
31015 DEFVAR_BOOL ("inhibit-menubar-update", inhibit_menubar_update,
31016 doc: /* Non-nil means don't update menu bars. Internal use only. */);
31017 inhibit_menubar_update = 0;
31019 DEFVAR_LISP ("wrap-prefix", Vwrap_prefix,
31020 doc: /* Prefix prepended to all continuation lines at display time.
31021 The value may be a string, an image, or a stretch-glyph; it is
31022 interpreted in the same way as the value of a `display' text property.
31024 This variable is overridden by any `wrap-prefix' text or overlay
31025 property.
31027 To add a prefix to non-continuation lines, use `line-prefix'. */);
31028 Vwrap_prefix = Qnil;
31029 DEFSYM (Qwrap_prefix, "wrap-prefix");
31030 Fmake_variable_buffer_local (Qwrap_prefix);
31032 DEFVAR_LISP ("line-prefix", Vline_prefix,
31033 doc: /* Prefix prepended to all non-continuation lines at display time.
31034 The value may be a string, an image, or a stretch-glyph; it is
31035 interpreted in the same way as the value of a `display' text property.
31037 This variable is overridden by any `line-prefix' text or overlay
31038 property.
31040 To add a prefix to continuation lines, use `wrap-prefix'. */);
31041 Vline_prefix = Qnil;
31042 DEFSYM (Qline_prefix, "line-prefix");
31043 Fmake_variable_buffer_local (Qline_prefix);
31045 DEFVAR_BOOL ("inhibit-eval-during-redisplay", inhibit_eval_during_redisplay,
31046 doc: /* Non-nil means don't eval Lisp during redisplay. */);
31047 inhibit_eval_during_redisplay = 0;
31049 DEFVAR_BOOL ("inhibit-free-realized-faces", inhibit_free_realized_faces,
31050 doc: /* Non-nil means don't free realized faces. Internal use only. */);
31051 inhibit_free_realized_faces = 0;
31053 DEFVAR_BOOL ("inhibit-bidi-mirroring", inhibit_bidi_mirroring,
31054 doc: /* Non-nil means don't mirror characters even when bidi context requires that.
31055 Intended for use during debugging and for testing bidi display;
31056 see biditest.el in the test suite. */);
31057 inhibit_bidi_mirroring = 0;
31059 #ifdef GLYPH_DEBUG
31060 DEFVAR_BOOL ("inhibit-try-window-id", inhibit_try_window_id,
31061 doc: /* Inhibit try_window_id display optimization. */);
31062 inhibit_try_window_id = 0;
31064 DEFVAR_BOOL ("inhibit-try-window-reusing", inhibit_try_window_reusing,
31065 doc: /* Inhibit try_window_reusing display optimization. */);
31066 inhibit_try_window_reusing = 0;
31068 DEFVAR_BOOL ("inhibit-try-cursor-movement", inhibit_try_cursor_movement,
31069 doc: /* Inhibit try_cursor_movement display optimization. */);
31070 inhibit_try_cursor_movement = 0;
31071 #endif /* GLYPH_DEBUG */
31073 DEFVAR_INT ("overline-margin", overline_margin,
31074 doc: /* Space between overline and text, in pixels.
31075 The default value is 2: the height of the overline (1 pixel) plus 1 pixel
31076 margin to the character height. */);
31077 overline_margin = 2;
31079 DEFVAR_INT ("underline-minimum-offset",
31080 underline_minimum_offset,
31081 doc: /* Minimum distance between baseline and underline.
31082 This can improve legibility of underlined text at small font sizes,
31083 particularly when using variable `x-use-underline-position-properties'
31084 with fonts that specify an UNDERLINE_POSITION relatively close to the
31085 baseline. The default value is 1. */);
31086 underline_minimum_offset = 1;
31088 DEFVAR_BOOL ("display-hourglass", display_hourglass_p,
31089 doc: /* Non-nil means show an hourglass pointer, when Emacs is busy.
31090 This feature only works when on a window system that can change
31091 cursor shapes. */);
31092 display_hourglass_p = 1;
31094 DEFVAR_LISP ("hourglass-delay", Vhourglass_delay,
31095 doc: /* Seconds to wait before displaying an hourglass pointer when Emacs is busy. */);
31096 Vhourglass_delay = make_number (DEFAULT_HOURGLASS_DELAY);
31098 #ifdef HAVE_WINDOW_SYSTEM
31099 hourglass_atimer = NULL;
31100 hourglass_shown_p = 0;
31101 #endif /* HAVE_WINDOW_SYSTEM */
31103 /* Name of the face used to display glyphless characters. */
31104 DEFSYM (Qglyphless_char, "glyphless-char");
31106 /* Method symbols for Vglyphless_char_display. */
31107 DEFSYM (Qhex_code, "hex-code");
31108 DEFSYM (Qempty_box, "empty-box");
31109 DEFSYM (Qthin_space, "thin-space");
31110 DEFSYM (Qzero_width, "zero-width");
31112 DEFVAR_LISP ("pre-redisplay-function", Vpre_redisplay_function,
31113 doc: /* Function run just before redisplay.
31114 It is called with one argument, which is the set of windows that are to
31115 be redisplayed. This set can be nil (meaning, only the selected window),
31116 or t (meaning all windows). */);
31117 Vpre_redisplay_function = intern ("ignore");
31119 /* Symbol for the purpose of Vglyphless_char_display. */
31120 DEFSYM (Qglyphless_char_display, "glyphless-char-display");
31121 Fput (Qglyphless_char_display, Qchar_table_extra_slots, make_number (1));
31123 DEFVAR_LISP ("glyphless-char-display", Vglyphless_char_display,
31124 doc: /* Char-table defining glyphless characters.
31125 Each element, if non-nil, should be one of the following:
31126 an ASCII acronym string: display this string in a box
31127 `hex-code': display the hexadecimal code of a character in a box
31128 `empty-box': display as an empty box
31129 `thin-space': display as 1-pixel width space
31130 `zero-width': don't display
31131 An element may also be a cons cell (GRAPHICAL . TEXT), which specifies the
31132 display method for graphical terminals and text terminals respectively.
31133 GRAPHICAL and TEXT should each have one of the values listed above.
31135 The char-table has one extra slot to control the display of a character for
31136 which no font is found. This slot only takes effect on graphical terminals.
31137 Its value should be an ASCII acronym string, `hex-code', `empty-box', or
31138 `thin-space'. The default is `empty-box'.
31140 If a character has a non-nil entry in an active display table, the
31141 display table takes effect; in this case, Emacs does not consult
31142 `glyphless-char-display' at all. */);
31143 Vglyphless_char_display = Fmake_char_table (Qglyphless_char_display, Qnil);
31144 Fset_char_table_extra_slot (Vglyphless_char_display, make_number (0),
31145 Qempty_box);
31147 DEFVAR_LISP ("debug-on-message", Vdebug_on_message,
31148 doc: /* If non-nil, debug if a message matching this regexp is displayed. */);
31149 Vdebug_on_message = Qnil;
31151 DEFVAR_LISP ("redisplay--all-windows-cause", Vredisplay__all_windows_cause,
31152 doc: /* */);
31153 Vredisplay__all_windows_cause
31154 = Fmake_vector (make_number (100), make_number (0));
31156 DEFVAR_LISP ("redisplay--mode-lines-cause", Vredisplay__mode_lines_cause,
31157 doc: /* */);
31158 Vredisplay__mode_lines_cause
31159 = Fmake_vector (make_number (100), make_number (0));
31163 /* Initialize this module when Emacs starts. */
31165 void
31166 init_xdisp (void)
31168 CHARPOS (this_line_start_pos) = 0;
31170 if (!noninteractive)
31172 struct window *m = XWINDOW (minibuf_window);
31173 Lisp_Object frame = m->frame;
31174 struct frame *f = XFRAME (frame);
31175 Lisp_Object root = FRAME_ROOT_WINDOW (f);
31176 struct window *r = XWINDOW (root);
31177 int i;
31179 echo_area_window = minibuf_window;
31181 r->top_line = FRAME_TOP_MARGIN (f);
31182 r->pixel_top = r->top_line * FRAME_LINE_HEIGHT (f);
31183 r->total_cols = FRAME_COLS (f);
31184 r->pixel_width = r->total_cols * FRAME_COLUMN_WIDTH (f);
31185 r->total_lines = FRAME_TOTAL_LINES (f) - 1 - FRAME_TOP_MARGIN (f);
31186 r->pixel_height = r->total_lines * FRAME_LINE_HEIGHT (f);
31188 m->top_line = FRAME_TOTAL_LINES (f) - 1;
31189 m->pixel_top = m->top_line * FRAME_LINE_HEIGHT (f);
31190 m->total_cols = FRAME_COLS (f);
31191 m->pixel_width = m->total_cols * FRAME_COLUMN_WIDTH (f);
31192 m->total_lines = 1;
31193 m->pixel_height = m->total_lines * FRAME_LINE_HEIGHT (f);
31195 scratch_glyph_row.glyphs[TEXT_AREA] = scratch_glyphs;
31196 scratch_glyph_row.glyphs[TEXT_AREA + 1]
31197 = scratch_glyphs + MAX_SCRATCH_GLYPHS;
31199 /* The default ellipsis glyphs `...'. */
31200 for (i = 0; i < 3; ++i)
31201 default_invis_vector[i] = make_number ('.');
31205 /* Allocate the buffer for frame titles.
31206 Also used for `format-mode-line'. */
31207 int size = 100;
31208 mode_line_noprop_buf = xmalloc (size);
31209 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
31210 mode_line_noprop_ptr = mode_line_noprop_buf;
31211 mode_line_target = MODE_LINE_DISPLAY;
31214 help_echo_showing_p = 0;
31217 #ifdef HAVE_WINDOW_SYSTEM
31219 /* Platform-independent portion of hourglass implementation. */
31221 /* Timer function of hourglass_atimer. */
31223 static void
31224 show_hourglass (struct atimer *timer)
31226 /* The timer implementation will cancel this timer automatically
31227 after this function has run. Set hourglass_atimer to null
31228 so that we know the timer doesn't have to be canceled. */
31229 hourglass_atimer = NULL;
31231 if (!hourglass_shown_p)
31233 Lisp_Object tail, frame;
31235 block_input ();
31237 FOR_EACH_FRAME (tail, frame)
31239 struct frame *f = XFRAME (frame);
31241 if (FRAME_LIVE_P (f) && FRAME_WINDOW_P (f)
31242 && FRAME_RIF (f)->show_hourglass)
31243 FRAME_RIF (f)->show_hourglass (f);
31246 hourglass_shown_p = 1;
31247 unblock_input ();
31251 /* Cancel a currently active hourglass timer, and start a new one. */
31253 void
31254 start_hourglass (void)
31256 struct timespec delay;
31258 cancel_hourglass ();
31260 if (INTEGERP (Vhourglass_delay)
31261 && XINT (Vhourglass_delay) > 0)
31262 delay = make_timespec (min (XINT (Vhourglass_delay),
31263 TYPE_MAXIMUM (time_t)),
31265 else if (FLOATP (Vhourglass_delay)
31266 && XFLOAT_DATA (Vhourglass_delay) > 0)
31267 delay = dtotimespec (XFLOAT_DATA (Vhourglass_delay));
31268 else
31269 delay = make_timespec (DEFAULT_HOURGLASS_DELAY, 0);
31271 hourglass_atimer = start_atimer (ATIMER_RELATIVE, delay,
31272 show_hourglass, NULL);
31275 /* Cancel the hourglass cursor timer if active, hide a busy cursor if
31276 shown. */
31278 void
31279 cancel_hourglass (void)
31281 if (hourglass_atimer)
31283 cancel_atimer (hourglass_atimer);
31284 hourglass_atimer = NULL;
31287 if (hourglass_shown_p)
31289 Lisp_Object tail, frame;
31291 block_input ();
31293 FOR_EACH_FRAME (tail, frame)
31295 struct frame *f = XFRAME (frame);
31297 if (FRAME_LIVE_P (f) && FRAME_WINDOW_P (f)
31298 && FRAME_RIF (f)->hide_hourglass)
31299 FRAME_RIF (f)->hide_hourglass (f);
31300 #ifdef HAVE_NTGUI
31301 /* No cursors on non GUI frames - restore to stock arrow cursor. */
31302 else if (!FRAME_W32_P (f))
31303 w32_arrow_cursor ();
31304 #endif
31307 hourglass_shown_p = 0;
31308 unblock_input ();
31312 #endif /* HAVE_WINDOW_SYSTEM */