isearch.el (isearch-process-search-char): Add docstring.
[emacs.git] / src / xdisp.c
blob71871ec588538c3865c991b5adee398b1a7f57d0
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_count && !inhibit_free_realized_faces)
2729 face_change_count = 0;
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->vpos < it->w->window_end_vpos)
9268 it->continuation_lines_width += it->current_x;
9269 it->current_x = it->hpos = it->max_ascent = it->max_descent = 0;
9270 it->current_y += it->max_ascent + it->max_descent;
9271 ++it->vpos;
9272 last_height = it->max_ascent + it->max_descent;
9275 if (backup_data)
9276 bidi_unshelve_cache (backup_data, 1);
9278 TRACE_MOVE ((stderr, "move_it_to: reached %d\n", reached));
9280 return max_current_x;
9284 /* Move iterator IT backward by a specified y-distance DY, DY >= 0.
9286 If DY > 0, move IT backward at least that many pixels. DY = 0
9287 means move IT backward to the preceding line start or BEGV. This
9288 function may move over more than DY pixels if IT->current_y - DY
9289 ends up in the middle of a line; in this case IT->current_y will be
9290 set to the top of the line moved to. */
9292 void
9293 move_it_vertically_backward (struct it *it, int dy)
9295 int nlines, h;
9296 struct it it2, it3;
9297 void *it2data = NULL, *it3data = NULL;
9298 ptrdiff_t start_pos;
9299 int nchars_per_row
9300 = (it->last_visible_x - it->first_visible_x) / FRAME_COLUMN_WIDTH (it->f);
9301 ptrdiff_t pos_limit;
9303 move_further_back:
9304 eassert (dy >= 0);
9306 start_pos = IT_CHARPOS (*it);
9308 /* Estimate how many newlines we must move back. */
9309 nlines = max (1, dy / default_line_pixel_height (it->w));
9310 if (it->line_wrap == TRUNCATE || nchars_per_row == 0)
9311 pos_limit = BEGV;
9312 else
9313 pos_limit = max (start_pos - nlines * nchars_per_row, BEGV);
9315 /* Set the iterator's position that many lines back. But don't go
9316 back more than NLINES full screen lines -- this wins a day with
9317 buffers which have very long lines. */
9318 while (nlines-- && IT_CHARPOS (*it) > pos_limit)
9319 back_to_previous_visible_line_start (it);
9321 /* Reseat the iterator here. When moving backward, we don't want
9322 reseat to skip forward over invisible text, set up the iterator
9323 to deliver from overlay strings at the new position etc. So,
9324 use reseat_1 here. */
9325 reseat_1 (it, it->current.pos, 1);
9327 /* We are now surely at a line start. */
9328 it->current_x = it->hpos = 0; /* FIXME: this is incorrect when bidi
9329 reordering is in effect. */
9330 it->continuation_lines_width = 0;
9332 /* Move forward and see what y-distance we moved. First move to the
9333 start of the next line so that we get its height. We need this
9334 height to be able to tell whether we reached the specified
9335 y-distance. */
9336 SAVE_IT (it2, *it, it2data);
9337 it2.max_ascent = it2.max_descent = 0;
9340 move_it_to (&it2, start_pos, -1, -1, it2.vpos + 1,
9341 MOVE_TO_POS | MOVE_TO_VPOS);
9343 while (!(IT_POS_VALID_AFTER_MOVE_P (&it2)
9344 /* If we are in a display string which starts at START_POS,
9345 and that display string includes a newline, and we are
9346 right after that newline (i.e. at the beginning of a
9347 display line), exit the loop, because otherwise we will
9348 infloop, since move_it_to will see that it is already at
9349 START_POS and will not move. */
9350 || (it2.method == GET_FROM_STRING
9351 && IT_CHARPOS (it2) == start_pos
9352 && SREF (it2.string, IT_STRING_BYTEPOS (it2) - 1) == '\n')));
9353 eassert (IT_CHARPOS (*it) >= BEGV);
9354 SAVE_IT (it3, it2, it3data);
9356 move_it_to (&it2, start_pos, -1, -1, -1, MOVE_TO_POS);
9357 eassert (IT_CHARPOS (*it) >= BEGV);
9358 /* H is the actual vertical distance from the position in *IT
9359 and the starting position. */
9360 h = it2.current_y - it->current_y;
9361 /* NLINES is the distance in number of lines. */
9362 nlines = it2.vpos - it->vpos;
9364 /* Correct IT's y and vpos position
9365 so that they are relative to the starting point. */
9366 it->vpos -= nlines;
9367 it->current_y -= h;
9369 if (dy == 0)
9371 /* DY == 0 means move to the start of the screen line. The
9372 value of nlines is > 0 if continuation lines were involved,
9373 or if the original IT position was at start of a line. */
9374 RESTORE_IT (it, it, it2data);
9375 if (nlines > 0)
9376 move_it_by_lines (it, nlines);
9377 /* The above code moves us to some position NLINES down,
9378 usually to its first glyph (leftmost in an L2R line), but
9379 that's not necessarily the start of the line, under bidi
9380 reordering. We want to get to the character position
9381 that is immediately after the newline of the previous
9382 line. */
9383 if (it->bidi_p
9384 && !it->continuation_lines_width
9385 && !STRINGP (it->string)
9386 && IT_CHARPOS (*it) > BEGV
9387 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
9389 ptrdiff_t cp = IT_CHARPOS (*it), bp = IT_BYTEPOS (*it);
9391 DEC_BOTH (cp, bp);
9392 cp = find_newline_no_quit (cp, bp, -1, NULL);
9393 move_it_to (it, cp, -1, -1, -1, MOVE_TO_POS);
9395 bidi_unshelve_cache (it3data, 1);
9397 else
9399 /* The y-position we try to reach, relative to *IT.
9400 Note that H has been subtracted in front of the if-statement. */
9401 int target_y = it->current_y + h - dy;
9402 int y0 = it3.current_y;
9403 int y1;
9404 int line_height;
9406 RESTORE_IT (&it3, &it3, it3data);
9407 y1 = line_bottom_y (&it3);
9408 line_height = y1 - y0;
9409 RESTORE_IT (it, it, it2data);
9410 /* If we did not reach target_y, try to move further backward if
9411 we can. If we moved too far backward, try to move forward. */
9412 if (target_y < it->current_y
9413 /* This is heuristic. In a window that's 3 lines high, with
9414 a line height of 13 pixels each, recentering with point
9415 on the bottom line will try to move -39/2 = 19 pixels
9416 backward. Try to avoid moving into the first line. */
9417 && (it->current_y - target_y
9418 > min (window_box_height (it->w), line_height * 2 / 3))
9419 && IT_CHARPOS (*it) > BEGV)
9421 TRACE_MOVE ((stderr, " not far enough -> move_vert %d\n",
9422 target_y - it->current_y));
9423 dy = it->current_y - target_y;
9424 goto move_further_back;
9426 else if (target_y >= it->current_y + line_height
9427 && IT_CHARPOS (*it) < ZV)
9429 /* Should move forward by at least one line, maybe more.
9431 Note: Calling move_it_by_lines can be expensive on
9432 terminal frames, where compute_motion is used (via
9433 vmotion) to do the job, when there are very long lines
9434 and truncate-lines is nil. That's the reason for
9435 treating terminal frames specially here. */
9437 if (!FRAME_WINDOW_P (it->f))
9438 move_it_vertically (it, target_y - (it->current_y + line_height));
9439 else
9443 move_it_by_lines (it, 1);
9445 while (target_y >= line_bottom_y (it) && IT_CHARPOS (*it) < ZV);
9452 /* Move IT by a specified amount of pixel lines DY. DY negative means
9453 move backwards. DY = 0 means move to start of screen line. At the
9454 end, IT will be on the start of a screen line. */
9456 void
9457 move_it_vertically (struct it *it, int dy)
9459 if (dy <= 0)
9460 move_it_vertically_backward (it, -dy);
9461 else
9463 TRACE_MOVE ((stderr, "move_it_v: from %d, %d\n", IT_CHARPOS (*it), dy));
9464 move_it_to (it, ZV, -1, it->current_y + dy, -1,
9465 MOVE_TO_POS | MOVE_TO_Y);
9466 TRACE_MOVE ((stderr, "move_it_v: to %d\n", IT_CHARPOS (*it)));
9468 /* If buffer ends in ZV without a newline, move to the start of
9469 the line to satisfy the post-condition. */
9470 if (IT_CHARPOS (*it) == ZV
9471 && ZV > BEGV
9472 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
9473 move_it_by_lines (it, 0);
9478 /* Move iterator IT past the end of the text line it is in. */
9480 void
9481 move_it_past_eol (struct it *it)
9483 enum move_it_result rc;
9485 rc = move_it_in_display_line_to (it, Z, 0, MOVE_TO_POS);
9486 if (rc == MOVE_NEWLINE_OR_CR)
9487 set_iterator_to_next (it, 0);
9491 /* Move IT by a specified number DVPOS of screen lines down. DVPOS
9492 negative means move up. DVPOS == 0 means move to the start of the
9493 screen line.
9495 Optimization idea: If we would know that IT->f doesn't use
9496 a face with proportional font, we could be faster for
9497 truncate-lines nil. */
9499 void
9500 move_it_by_lines (struct it *it, ptrdiff_t dvpos)
9503 /* The commented-out optimization uses vmotion on terminals. This
9504 gives bad results, because elements like it->what, on which
9505 callers such as pos_visible_p rely, aren't updated. */
9506 /* struct position pos;
9507 if (!FRAME_WINDOW_P (it->f))
9509 struct text_pos textpos;
9511 pos = *vmotion (IT_CHARPOS (*it), dvpos, it->w);
9512 SET_TEXT_POS (textpos, pos.bufpos, pos.bytepos);
9513 reseat (it, textpos, 1);
9514 it->vpos += pos.vpos;
9515 it->current_y += pos.vpos;
9517 else */
9519 if (dvpos == 0)
9521 /* DVPOS == 0 means move to the start of the screen line. */
9522 move_it_vertically_backward (it, 0);
9523 /* Let next call to line_bottom_y calculate real line height. */
9524 last_height = 0;
9526 else if (dvpos > 0)
9528 move_it_to (it, -1, -1, -1, it->vpos + dvpos, MOVE_TO_VPOS);
9529 if (!IT_POS_VALID_AFTER_MOVE_P (it))
9531 /* Only move to the next buffer position if we ended up in a
9532 string from display property, not in an overlay string
9533 (before-string or after-string). That is because the
9534 latter don't conceal the underlying buffer position, so
9535 we can ask to move the iterator to the exact position we
9536 are interested in. Note that, even if we are already at
9537 IT_CHARPOS (*it), the call below is not a no-op, as it
9538 will detect that we are at the end of the string, pop the
9539 iterator, and compute it->current_x and it->hpos
9540 correctly. */
9541 move_it_to (it, IT_CHARPOS (*it) + it->string_from_display_prop_p,
9542 -1, -1, -1, MOVE_TO_POS);
9545 else
9547 struct it it2;
9548 void *it2data = NULL;
9549 ptrdiff_t start_charpos, i;
9550 int nchars_per_row
9551 = (it->last_visible_x - it->first_visible_x) / FRAME_COLUMN_WIDTH (it->f);
9552 bool hit_pos_limit = false;
9553 ptrdiff_t pos_limit;
9555 /* Start at the beginning of the screen line containing IT's
9556 position. This may actually move vertically backwards,
9557 in case of overlays, so adjust dvpos accordingly. */
9558 dvpos += it->vpos;
9559 move_it_vertically_backward (it, 0);
9560 dvpos -= it->vpos;
9562 /* Go back -DVPOS buffer lines, but no farther than -DVPOS full
9563 screen lines, and reseat the iterator there. */
9564 start_charpos = IT_CHARPOS (*it);
9565 if (it->line_wrap == TRUNCATE || nchars_per_row == 0)
9566 pos_limit = BEGV;
9567 else
9568 pos_limit = max (start_charpos + dvpos * nchars_per_row, BEGV);
9570 for (i = -dvpos; i > 0 && IT_CHARPOS (*it) > pos_limit; --i)
9571 back_to_previous_visible_line_start (it);
9572 if (i > 0 && IT_CHARPOS (*it) <= pos_limit)
9573 hit_pos_limit = true;
9574 reseat (it, it->current.pos, 1);
9576 /* Move further back if we end up in a string or an image. */
9577 while (!IT_POS_VALID_AFTER_MOVE_P (it))
9579 /* First try to move to start of display line. */
9580 dvpos += it->vpos;
9581 move_it_vertically_backward (it, 0);
9582 dvpos -= it->vpos;
9583 if (IT_POS_VALID_AFTER_MOVE_P (it))
9584 break;
9585 /* If start of line is still in string or image,
9586 move further back. */
9587 back_to_previous_visible_line_start (it);
9588 reseat (it, it->current.pos, 1);
9589 dvpos--;
9592 it->current_x = it->hpos = 0;
9594 /* Above call may have moved too far if continuation lines
9595 are involved. Scan forward and see if it did. */
9596 SAVE_IT (it2, *it, it2data);
9597 it2.vpos = it2.current_y = 0;
9598 move_it_to (&it2, start_charpos, -1, -1, -1, MOVE_TO_POS);
9599 it->vpos -= it2.vpos;
9600 it->current_y -= it2.current_y;
9601 it->current_x = it->hpos = 0;
9603 /* If we moved too far back, move IT some lines forward. */
9604 if (it2.vpos > -dvpos)
9606 int delta = it2.vpos + dvpos;
9608 RESTORE_IT (&it2, &it2, it2data);
9609 SAVE_IT (it2, *it, it2data);
9610 move_it_to (it, -1, -1, -1, it->vpos + delta, MOVE_TO_VPOS);
9611 /* Move back again if we got too far ahead. */
9612 if (IT_CHARPOS (*it) >= start_charpos)
9613 RESTORE_IT (it, &it2, it2data);
9614 else
9615 bidi_unshelve_cache (it2data, 1);
9617 else if (hit_pos_limit && pos_limit > BEGV
9618 && dvpos < 0 && it2.vpos < -dvpos)
9620 /* If we hit the limit, but still didn't make it far enough
9621 back, that means there's a display string with a newline
9622 covering a large chunk of text, and that caused
9623 back_to_previous_visible_line_start try to go too far.
9624 Punish those who commit such atrocities by going back
9625 until we've reached DVPOS, after lifting the limit, which
9626 could make it slow for very long lines. "If it hurts,
9627 don't do that!" */
9628 dvpos += it2.vpos;
9629 RESTORE_IT (it, it, it2data);
9630 for (i = -dvpos; i > 0; --i)
9632 back_to_previous_visible_line_start (it);
9633 it->vpos--;
9635 reseat_1 (it, it->current.pos, 1);
9637 else
9638 RESTORE_IT (it, it, it2data);
9642 /* Return true if IT points into the middle of a display vector. */
9644 bool
9645 in_display_vector_p (struct it *it)
9647 return (it->method == GET_FROM_DISPLAY_VECTOR
9648 && it->current.dpvec_index > 0
9649 && it->dpvec + it->current.dpvec_index != it->dpend);
9652 DEFUN ("window-text-pixel-size", Fwindow_text_pixel_size, Swindow_text_pixel_size, 0, 6, 0,
9653 doc: /* Return the size of the text of WINDOW's buffer in pixels.
9654 WINDOW must be a live window and defaults to the selected one. The
9655 return value is a cons of the maximum pixel-width of any text line and
9656 the maximum pixel-height of all text lines.
9658 The optional argument FROM, if non-nil, specifies the first text
9659 position and defaults to the minimum accessible position of the buffer.
9660 If FROM is t, use the minimum accessible position that is not a newline
9661 character. TO, if non-nil, specifies the last text position and
9662 defaults to the maximum accessible position of the buffer. If TO is t,
9663 use the maximum accessible position that is not a newline character.
9665 The optional argument X-LIMIT, if non-nil, specifies the maximum text
9666 width that can be returned. X-LIMIT nil or omitted, means to use the
9667 pixel-width of WINDOW's body; use this if you do not intend to change
9668 the width of WINDOW. Use the maximum width WINDOW may assume if you
9669 intend to change WINDOW's width. In any case, text whose x-coordinate
9670 is beyond X-LIMIT is ignored. Since calculating the width of long lines
9671 can take some time, it's always a good idea to make this argument as
9672 small as possible; in particular, if the buffer contains long lines that
9673 shall be truncated anyway.
9675 The optional argument Y-LIMIT, if non-nil, specifies the maximum text
9676 height that can be returned. Text lines whose y-coordinate is beyond
9677 Y-LIMIT are ignored. Since calculating the text height of a large
9678 buffer can take some time, it makes sense to specify this argument if
9679 the size of the buffer is unknown.
9681 Optional argument MODE-AND-HEADER-LINE nil or omitted means do not
9682 include the height of the mode- or header-line of WINDOW in the return
9683 value. If it is either the symbol `mode-line' or `header-line', include
9684 only the height of that line, if present, in the return value. If t,
9685 include the height of both, if present, in the return value. */)
9686 (Lisp_Object window, Lisp_Object from, Lisp_Object to, Lisp_Object x_limit, Lisp_Object y_limit,
9687 Lisp_Object mode_and_header_line)
9689 struct window *w = decode_live_window (window);
9690 Lisp_Object buf;
9691 struct buffer *b;
9692 struct it it;
9693 struct buffer *old_buffer = NULL;
9694 ptrdiff_t start, end, pos;
9695 struct text_pos startp;
9696 void *itdata = NULL;
9697 int c, max_y = -1, x = 0, y = 0;
9699 buf = w->contents;
9700 CHECK_BUFFER (buf);
9701 b = XBUFFER (buf);
9703 if (b != current_buffer)
9705 old_buffer = 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_buffer)
9783 set_buffer_internal (old_buffer);
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_count is non-zero, init_iterator will free all
13376 realized faces, which includes the faces referenced from current
13377 matrices. So, we can't reuse current matrices in this case. */
13378 if (face_change_count)
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_vpos < w->current_matrix->nrows);
15388 /* Handle case where text has not changed, only point, and it has
15389 not moved off the frame. */
15390 if (/* Point may be in this window. */
15391 PT >= CHARPOS (startp)
15392 /* Selective display hasn't changed. */
15393 && !current_buffer->clip_changed
15394 /* Function force-mode-line-update is used to force a thorough
15395 redisplay. It sets either windows_or_buffers_changed or
15396 update_mode_lines. So don't take a shortcut here for these
15397 cases. */
15398 && !update_mode_lines
15399 && !windows_or_buffers_changed
15400 && !f->cursor_type_changed
15401 && NILP (Vshow_trailing_whitespace)
15402 /* This code is not used for mini-buffer for the sake of the case
15403 of redisplaying to replace an echo area message; since in
15404 that case the mini-buffer contents per se are usually
15405 unchanged. This code is of no real use in the mini-buffer
15406 since the handling of this_line_start_pos, etc., in redisplay
15407 handles the same cases. */
15408 && !EQ (window, minibuf_window)
15409 && (FRAME_WINDOW_P (f)
15410 || !overlay_arrow_in_current_buffer_p ()))
15412 int this_scroll_margin, top_scroll_margin;
15413 struct glyph_row *row = NULL;
15414 int frame_line_height = default_line_pixel_height (w);
15415 int window_total_lines
15416 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
15418 #ifdef GLYPH_DEBUG
15419 debug_method_add (w, "cursor movement");
15420 #endif
15422 /* Scroll if point within this distance from the top or bottom
15423 of the window. This is a pixel value. */
15424 if (scroll_margin > 0)
15426 this_scroll_margin = min (scroll_margin, window_total_lines / 4);
15427 this_scroll_margin *= frame_line_height;
15429 else
15430 this_scroll_margin = 0;
15432 top_scroll_margin = this_scroll_margin;
15433 if (WINDOW_WANTS_HEADER_LINE_P (w))
15434 top_scroll_margin += CURRENT_HEADER_LINE_HEIGHT (w);
15436 /* Start with the row the cursor was displayed during the last
15437 not paused redisplay. Give up if that row is not valid. */
15438 if (w->last_cursor_vpos < 0
15439 || w->last_cursor_vpos >= w->current_matrix->nrows)
15440 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15441 else
15443 row = MATRIX_ROW (w->current_matrix, w->last_cursor_vpos);
15444 if (row->mode_line_p)
15445 ++row;
15446 if (!row->enabled_p)
15447 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15450 if (rc == CURSOR_MOVEMENT_CANNOT_BE_USED)
15452 int scroll_p = 0, must_scroll = 0;
15453 int last_y = window_text_bottom_y (w) - this_scroll_margin;
15455 if (PT > w->last_point)
15457 /* Point has moved forward. */
15458 while (MATRIX_ROW_END_CHARPOS (row) < PT
15459 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
15461 eassert (row->enabled_p);
15462 ++row;
15465 /* If the end position of a row equals the start
15466 position of the next row, and PT is at that position,
15467 we would rather display cursor in the next line. */
15468 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
15469 && MATRIX_ROW_END_CHARPOS (row) == PT
15470 && row < MATRIX_MODE_LINE_ROW (w->current_matrix)
15471 && MATRIX_ROW_START_CHARPOS (row+1) == PT
15472 && !cursor_row_p (row))
15473 ++row;
15475 /* If within the scroll margin, scroll. Note that
15476 MATRIX_ROW_BOTTOM_Y gives the pixel position at which
15477 the next line would be drawn, and that
15478 this_scroll_margin can be zero. */
15479 if (MATRIX_ROW_BOTTOM_Y (row) > last_y
15480 || PT > MATRIX_ROW_END_CHARPOS (row)
15481 /* Line is completely visible last line in window
15482 and PT is to be set in the next line. */
15483 || (MATRIX_ROW_BOTTOM_Y (row) == last_y
15484 && PT == MATRIX_ROW_END_CHARPOS (row)
15485 && !row->ends_at_zv_p
15486 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
15487 scroll_p = 1;
15489 else if (PT < w->last_point)
15491 /* Cursor has to be moved backward. Note that PT >=
15492 CHARPOS (startp) because of the outer if-statement. */
15493 while (!row->mode_line_p
15494 && (MATRIX_ROW_START_CHARPOS (row) > PT
15495 || (MATRIX_ROW_START_CHARPOS (row) == PT
15496 && (MATRIX_ROW_STARTS_IN_MIDDLE_OF_CHAR_P (row)
15497 || (/* STARTS_IN_MIDDLE_OF_STRING_P (row) */
15498 row > w->current_matrix->rows
15499 && (row-1)->ends_in_newline_from_string_p))))
15500 && (row->y > top_scroll_margin
15501 || CHARPOS (startp) == BEGV))
15503 eassert (row->enabled_p);
15504 --row;
15507 /* Consider the following case: Window starts at BEGV,
15508 there is invisible, intangible text at BEGV, so that
15509 display starts at some point START > BEGV. It can
15510 happen that we are called with PT somewhere between
15511 BEGV and START. Try to handle that case. */
15512 if (row < w->current_matrix->rows
15513 || row->mode_line_p)
15515 row = w->current_matrix->rows;
15516 if (row->mode_line_p)
15517 ++row;
15520 /* Due to newlines in overlay strings, we may have to
15521 skip forward over overlay strings. */
15522 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
15523 && MATRIX_ROW_END_CHARPOS (row) == PT
15524 && !cursor_row_p (row))
15525 ++row;
15527 /* If within the scroll margin, scroll. */
15528 if (row->y < top_scroll_margin
15529 && CHARPOS (startp) != BEGV)
15530 scroll_p = 1;
15532 else
15534 /* Cursor did not move. So don't scroll even if cursor line
15535 is partially visible, as it was so before. */
15536 rc = CURSOR_MOVEMENT_SUCCESS;
15539 if (PT < MATRIX_ROW_START_CHARPOS (row)
15540 || PT > MATRIX_ROW_END_CHARPOS (row))
15542 /* if PT is not in the glyph row, give up. */
15543 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15544 must_scroll = 1;
15546 else if (rc != CURSOR_MOVEMENT_SUCCESS
15547 && !NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering)))
15549 struct glyph_row *row1;
15551 /* If rows are bidi-reordered and point moved, back up
15552 until we find a row that does not belong to a
15553 continuation line. This is because we must consider
15554 all rows of a continued line as candidates for the
15555 new cursor positioning, since row start and end
15556 positions change non-linearly with vertical position
15557 in such rows. */
15558 /* FIXME: Revisit this when glyph ``spilling'' in
15559 continuation lines' rows is implemented for
15560 bidi-reordered rows. */
15561 for (row1 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
15562 MATRIX_ROW_CONTINUATION_LINE_P (row);
15563 --row)
15565 /* If we hit the beginning of the displayed portion
15566 without finding the first row of a continued
15567 line, give up. */
15568 if (row <= row1)
15570 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15571 break;
15573 eassert (row->enabled_p);
15576 if (must_scroll)
15578 else if (rc != CURSOR_MOVEMENT_SUCCESS
15579 && MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row)
15580 /* Make sure this isn't a header line by any chance, since
15581 then MATRIX_ROW_PARTIALLY_VISIBLE_P might yield non-zero. */
15582 && !row->mode_line_p
15583 && make_cursor_line_fully_visible_p)
15585 if (PT == MATRIX_ROW_END_CHARPOS (row)
15586 && !row->ends_at_zv_p
15587 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
15588 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15589 else if (row->height > window_box_height (w))
15591 /* If we end up in a partially visible line, let's
15592 make it fully visible, except when it's taller
15593 than the window, in which case we can't do much
15594 about it. */
15595 *scroll_step = 1;
15596 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15598 else
15600 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
15601 if (!cursor_row_fully_visible_p (w, 0, 1))
15602 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15603 else
15604 rc = CURSOR_MOVEMENT_SUCCESS;
15607 else if (scroll_p)
15608 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15609 else if (rc != CURSOR_MOVEMENT_SUCCESS
15610 && !NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering)))
15612 /* With bidi-reordered rows, there could be more than
15613 one candidate row whose start and end positions
15614 occlude point. We need to let set_cursor_from_row
15615 find the best candidate. */
15616 /* FIXME: Revisit this when glyph ``spilling'' in
15617 continuation lines' rows is implemented for
15618 bidi-reordered rows. */
15619 int rv = 0;
15623 int at_zv_p = 0, exact_match_p = 0;
15625 if (MATRIX_ROW_START_CHARPOS (row) <= PT
15626 && PT <= MATRIX_ROW_END_CHARPOS (row)
15627 && cursor_row_p (row))
15628 rv |= set_cursor_from_row (w, row, w->current_matrix,
15629 0, 0, 0, 0);
15630 /* As soon as we've found the exact match for point,
15631 or the first suitable row whose ends_at_zv_p flag
15632 is set, we are done. */
15633 if (rv)
15635 at_zv_p = MATRIX_ROW (w->current_matrix,
15636 w->cursor.vpos)->ends_at_zv_p;
15637 if (!at_zv_p
15638 && w->cursor.hpos >= 0
15639 && w->cursor.hpos < MATRIX_ROW_USED (w->current_matrix,
15640 w->cursor.vpos))
15642 struct glyph_row *candidate =
15643 MATRIX_ROW (w->current_matrix, w->cursor.vpos);
15644 struct glyph *g =
15645 candidate->glyphs[TEXT_AREA] + w->cursor.hpos;
15646 ptrdiff_t endpos = MATRIX_ROW_END_CHARPOS (candidate);
15648 exact_match_p =
15649 (BUFFERP (g->object) && g->charpos == PT)
15650 || (NILP (g->object)
15651 && (g->charpos == PT
15652 || (g->charpos == 0 && endpos - 1 == PT)));
15654 if (at_zv_p || exact_match_p)
15656 rc = CURSOR_MOVEMENT_SUCCESS;
15657 break;
15660 if (MATRIX_ROW_BOTTOM_Y (row) == last_y)
15661 break;
15662 ++row;
15664 while (((MATRIX_ROW_CONTINUATION_LINE_P (row)
15665 || row->continued_p)
15666 && MATRIX_ROW_BOTTOM_Y (row) <= last_y)
15667 || (MATRIX_ROW_START_CHARPOS (row) == PT
15668 && MATRIX_ROW_BOTTOM_Y (row) < last_y));
15669 /* If we didn't find any candidate rows, or exited the
15670 loop before all the candidates were examined, signal
15671 to the caller that this method failed. */
15672 if (rc != CURSOR_MOVEMENT_SUCCESS
15673 && !(rv
15674 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
15675 && !row->continued_p))
15676 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15677 else if (rv)
15678 rc = CURSOR_MOVEMENT_SUCCESS;
15680 else
15684 if (set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0))
15686 rc = CURSOR_MOVEMENT_SUCCESS;
15687 break;
15689 ++row;
15691 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
15692 && MATRIX_ROW_START_CHARPOS (row) == PT
15693 && cursor_row_p (row));
15698 return rc;
15702 void
15703 set_vertical_scroll_bar (struct window *w)
15705 ptrdiff_t start, end, whole;
15707 /* Calculate the start and end positions for the current window.
15708 At some point, it would be nice to choose between scrollbars
15709 which reflect the whole buffer size, with special markers
15710 indicating narrowing, and scrollbars which reflect only the
15711 visible region.
15713 Note that mini-buffers sometimes aren't displaying any text. */
15714 if (!MINI_WINDOW_P (w)
15715 || (w == XWINDOW (minibuf_window)
15716 && NILP (echo_area_buffer[0])))
15718 struct buffer *buf = XBUFFER (w->contents);
15719 whole = BUF_ZV (buf) - BUF_BEGV (buf);
15720 start = marker_position (w->start) - BUF_BEGV (buf);
15721 /* I don't think this is guaranteed to be right. For the
15722 moment, we'll pretend it is. */
15723 end = BUF_Z (buf) - w->window_end_pos - BUF_BEGV (buf);
15725 if (end < start)
15726 end = start;
15727 if (whole < (end - start))
15728 whole = end - start;
15730 else
15731 start = end = whole = 0;
15733 /* Indicate what this scroll bar ought to be displaying now. */
15734 if (FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
15735 (*FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
15736 (w, end - start, whole, start);
15740 void
15741 set_horizontal_scroll_bar (struct window *w)
15743 int start, end, whole, portion;
15745 if (!MINI_WINDOW_P (w)
15746 || (w == XWINDOW (minibuf_window)
15747 && NILP (echo_area_buffer[0])))
15749 struct buffer *b = XBUFFER (w->contents);
15750 struct buffer *old_buffer = NULL;
15751 struct it it;
15752 struct text_pos startp;
15754 if (b != current_buffer)
15756 old_buffer = current_buffer;
15757 set_buffer_internal (b);
15760 SET_TEXT_POS_FROM_MARKER (startp, w->start);
15761 start_display (&it, w, startp);
15762 it.last_visible_x = INT_MAX;
15763 whole = move_it_to (&it, -1, INT_MAX, window_box_height (w), -1,
15764 MOVE_TO_X | MOVE_TO_Y);
15765 /* whole = move_it_to (&it, w->window_end_pos, INT_MAX,
15766 window_box_height (w), -1,
15767 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y); */
15769 start = w->hscroll * FRAME_COLUMN_WIDTH (WINDOW_XFRAME (w));
15770 end = start + window_box_width (w, TEXT_AREA);
15771 portion = end - start;
15772 /* After enlarging a horizontally scrolled window such that it
15773 gets at least as wide as the text it contains, make sure that
15774 the thumb doesn't fill the entire scroll bar so we can still
15775 drag it back to see the entire text. */
15776 whole = max (whole, end);
15778 if (it.bidi_p)
15780 Lisp_Object pdir;
15782 pdir = Fcurrent_bidi_paragraph_direction (Qnil);
15783 if (EQ (pdir, Qright_to_left))
15785 start = whole - end;
15786 end = start + portion;
15790 if (old_buffer)
15791 set_buffer_internal (old_buffer);
15793 else
15794 start = end = whole = portion = 0;
15796 w->hscroll_whole = whole;
15798 /* Indicate what this scroll bar ought to be displaying now. */
15799 if (FRAME_TERMINAL (XFRAME (w->frame))->set_horizontal_scroll_bar_hook)
15800 (*FRAME_TERMINAL (XFRAME (w->frame))->set_horizontal_scroll_bar_hook)
15801 (w, portion, whole, start);
15805 /* Redisplay leaf window WINDOW. JUST_THIS_ONE_P non-zero means only
15806 selected_window is redisplayed.
15808 We can return without actually redisplaying the window if fonts has been
15809 changed on window's frame. In that case, redisplay_internal will retry.
15811 As one of the important parts of redisplaying a window, we need to
15812 decide whether the previous window-start position (stored in the
15813 window's w->start marker position) is still valid, and if it isn't,
15814 recompute it. Some details about that:
15816 . The previous window-start could be in a continuation line, in
15817 which case we need to recompute it when the window width
15818 changes. See compute_window_start_on_continuation_line and its
15819 call below.
15821 . The text that changed since last redisplay could include the
15822 previous window-start position. In that case, we try to salvage
15823 what we can from the current glyph matrix by calling
15824 try_scrolling, which see.
15826 . Some Emacs command could force us to use a specific window-start
15827 position by setting the window's force_start flag, or gently
15828 propose doing that by setting the window's optional_new_start
15829 flag. In these cases, we try using the specified start point if
15830 that succeeds (i.e. the window desired matrix is successfully
15831 recomputed, and point location is within the window). In case
15832 of optional_new_start, we first check if the specified start
15833 position is feasible, i.e. if it will allow point to be
15834 displayed in the window. If using the specified start point
15835 fails, e.g., if new fonts are needed to be loaded, we abort the
15836 redisplay cycle and leave it up to the next cycle to figure out
15837 things.
15839 . Note that the window's force_start flag is sometimes set by
15840 redisplay itself, when it decides that the previous window start
15841 point is fine and should be kept. Search for "goto force_start"
15842 below to see the details. Like the values of window-start
15843 specified outside of redisplay, these internally-deduced values
15844 are tested for feasibility, and ignored if found to be
15845 unfeasible.
15847 . Note that the function try_window, used to completely redisplay
15848 a window, accepts the window's start point as its argument.
15849 This is used several times in the redisplay code to control
15850 where the window start will be, according to user options such
15851 as scroll-conservatively, and also to ensure the screen line
15852 showing point will be fully (as opposed to partially) visible on
15853 display. */
15855 static void
15856 redisplay_window (Lisp_Object window, bool just_this_one_p)
15858 struct window *w = XWINDOW (window);
15859 struct frame *f = XFRAME (w->frame);
15860 struct buffer *buffer = XBUFFER (w->contents);
15861 struct buffer *old = current_buffer;
15862 struct text_pos lpoint, opoint, startp;
15863 int update_mode_line;
15864 int tem;
15865 struct it it;
15866 /* Record it now because it's overwritten. */
15867 bool current_matrix_up_to_date_p = false;
15868 bool used_current_matrix_p = false;
15869 /* This is less strict than current_matrix_up_to_date_p.
15870 It indicates that the buffer contents and narrowing are unchanged. */
15871 bool buffer_unchanged_p = false;
15872 int temp_scroll_step = 0;
15873 ptrdiff_t count = SPECPDL_INDEX ();
15874 int rc;
15875 int centering_position = -1;
15876 int last_line_misfit = 0;
15877 ptrdiff_t beg_unchanged, end_unchanged;
15878 int frame_line_height;
15880 SET_TEXT_POS (lpoint, PT, PT_BYTE);
15881 opoint = lpoint;
15883 #ifdef GLYPH_DEBUG
15884 *w->desired_matrix->method = 0;
15885 #endif
15887 if (!just_this_one_p
15888 && REDISPLAY_SOME_P ()
15889 && !w->redisplay
15890 && !f->redisplay
15891 && !buffer->text->redisplay
15892 && BUF_PT (buffer) == w->last_point)
15893 return;
15895 /* Make sure that both W's markers are valid. */
15896 eassert (XMARKER (w->start)->buffer == buffer);
15897 eassert (XMARKER (w->pointm)->buffer == buffer);
15899 /* We come here again if we need to run window-text-change-functions
15900 below. */
15901 restart:
15902 reconsider_clip_changes (w);
15903 frame_line_height = default_line_pixel_height (w);
15905 /* Has the mode line to be updated? */
15906 update_mode_line = (w->update_mode_line
15907 || update_mode_lines
15908 || buffer->clip_changed
15909 || buffer->prevent_redisplay_optimizations_p);
15911 if (!just_this_one_p)
15912 /* If `just_this_one_p' is set, we apparently set must_be_updated_p more
15913 cleverly elsewhere. */
15914 w->must_be_updated_p = true;
15916 if (MINI_WINDOW_P (w))
15918 if (w == XWINDOW (echo_area_window)
15919 && !NILP (echo_area_buffer[0]))
15921 if (update_mode_line)
15922 /* We may have to update a tty frame's menu bar or a
15923 tool-bar. Example `M-x C-h C-h C-g'. */
15924 goto finish_menu_bars;
15925 else
15926 /* We've already displayed the echo area glyphs in this window. */
15927 goto finish_scroll_bars;
15929 else if ((w != XWINDOW (minibuf_window)
15930 || minibuf_level == 0)
15931 /* When buffer is nonempty, redisplay window normally. */
15932 && BUF_Z (XBUFFER (w->contents)) == BUF_BEG (XBUFFER (w->contents))
15933 /* Quail displays non-mini buffers in minibuffer window.
15934 In that case, redisplay the window normally. */
15935 && !NILP (Fmemq (w->contents, Vminibuffer_list)))
15937 /* W is a mini-buffer window, but it's not active, so clear
15938 it. */
15939 int yb = window_text_bottom_y (w);
15940 struct glyph_row *row;
15941 int y;
15943 for (y = 0, row = w->desired_matrix->rows;
15944 y < yb;
15945 y += row->height, ++row)
15946 blank_row (w, row, y);
15947 goto finish_scroll_bars;
15950 clear_glyph_matrix (w->desired_matrix);
15953 /* Otherwise set up data on this window; select its buffer and point
15954 value. */
15955 /* Really select the buffer, for the sake of buffer-local
15956 variables. */
15957 set_buffer_internal_1 (XBUFFER (w->contents));
15959 current_matrix_up_to_date_p
15960 = (w->window_end_valid
15961 && !current_buffer->clip_changed
15962 && !current_buffer->prevent_redisplay_optimizations_p
15963 && !window_outdated (w));
15965 /* Run the window-text-change-functions
15966 if it is possible that the text on the screen has changed
15967 (either due to modification of the text, or any other reason). */
15968 if (!current_matrix_up_to_date_p
15969 && !NILP (Vwindow_text_change_functions))
15971 safe_run_hooks (Qwindow_text_change_functions);
15972 goto restart;
15975 beg_unchanged = BEG_UNCHANGED;
15976 end_unchanged = END_UNCHANGED;
15978 SET_TEXT_POS (opoint, PT, PT_BYTE);
15980 specbind (Qinhibit_point_motion_hooks, Qt);
15982 buffer_unchanged_p
15983 = (w->window_end_valid
15984 && !current_buffer->clip_changed
15985 && !window_outdated (w));
15987 /* When windows_or_buffers_changed is non-zero, we can't rely
15988 on the window end being valid, so set it to zero there. */
15989 if (windows_or_buffers_changed)
15991 /* If window starts on a continuation line, maybe adjust the
15992 window start in case the window's width changed. */
15993 if (XMARKER (w->start)->buffer == current_buffer)
15994 compute_window_start_on_continuation_line (w);
15996 w->window_end_valid = false;
15997 /* If so, we also can't rely on current matrix
15998 and should not fool try_cursor_movement below. */
15999 current_matrix_up_to_date_p = false;
16002 /* Some sanity checks. */
16003 CHECK_WINDOW_END (w);
16004 if (Z == Z_BYTE && CHARPOS (opoint) != BYTEPOS (opoint))
16005 emacs_abort ();
16006 if (BYTEPOS (opoint) < CHARPOS (opoint))
16007 emacs_abort ();
16009 if (mode_line_update_needed (w))
16010 update_mode_line = 1;
16012 /* Point refers normally to the selected window. For any other
16013 window, set up appropriate value. */
16014 if (!EQ (window, selected_window))
16016 ptrdiff_t new_pt = marker_position (w->pointm);
16017 ptrdiff_t new_pt_byte = marker_byte_position (w->pointm);
16019 if (new_pt < BEGV)
16021 new_pt = BEGV;
16022 new_pt_byte = BEGV_BYTE;
16023 set_marker_both (w->pointm, Qnil, BEGV, BEGV_BYTE);
16025 else if (new_pt > (ZV - 1))
16027 new_pt = ZV;
16028 new_pt_byte = ZV_BYTE;
16029 set_marker_both (w->pointm, Qnil, ZV, ZV_BYTE);
16032 /* We don't use SET_PT so that the point-motion hooks don't run. */
16033 TEMP_SET_PT_BOTH (new_pt, new_pt_byte);
16036 /* If any of the character widths specified in the display table
16037 have changed, invalidate the width run cache. It's true that
16038 this may be a bit late to catch such changes, but the rest of
16039 redisplay goes (non-fatally) haywire when the display table is
16040 changed, so why should we worry about doing any better? */
16041 if (current_buffer->width_run_cache
16042 || (current_buffer->base_buffer
16043 && current_buffer->base_buffer->width_run_cache))
16045 struct Lisp_Char_Table *disptab = buffer_display_table ();
16047 if (! disptab_matches_widthtab
16048 (disptab, XVECTOR (BVAR (current_buffer, width_table))))
16050 struct buffer *buf = current_buffer;
16052 if (buf->base_buffer)
16053 buf = buf->base_buffer;
16054 invalidate_region_cache (buf, buf->width_run_cache, BEG, Z);
16055 recompute_width_table (current_buffer, disptab);
16059 /* If window-start is screwed up, choose a new one. */
16060 if (XMARKER (w->start)->buffer != current_buffer)
16061 goto recenter;
16063 SET_TEXT_POS_FROM_MARKER (startp, w->start);
16065 /* If someone specified a new starting point but did not insist,
16066 check whether it can be used. */
16067 if ((w->optional_new_start || window_frozen_p (w))
16068 && CHARPOS (startp) >= BEGV
16069 && CHARPOS (startp) <= ZV)
16071 ptrdiff_t it_charpos;
16073 w->optional_new_start = 0;
16074 start_display (&it, w, startp);
16075 move_it_to (&it, PT, 0, it.last_visible_y, -1,
16076 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
16077 /* Record IT's position now, since line_bottom_y might change
16078 that. */
16079 it_charpos = IT_CHARPOS (it);
16080 /* Make sure we set the force_start flag only if the cursor row
16081 will be fully visible. Otherwise, the code under force_start
16082 label below will try to move point back into view, which is
16083 not what the code which sets optional_new_start wants. */
16084 if ((it.current_y == 0 || line_bottom_y (&it) < it.last_visible_y)
16085 && !w->force_start)
16087 if (it_charpos == PT)
16088 w->force_start = 1;
16089 /* IT may overshoot PT if text at PT is invisible. */
16090 else if (it_charpos > PT && CHARPOS (startp) <= PT)
16091 w->force_start = 1;
16092 #ifdef GLYPH_DEBUG
16093 if (w->force_start)
16095 if (window_frozen_p (w))
16096 debug_method_add (w, "set force_start from frozen window start");
16097 else
16098 debug_method_add (w, "set force_start from optional_new_start");
16100 #endif
16104 force_start:
16106 /* Handle case where place to start displaying has been specified,
16107 unless the specified location is outside the accessible range. */
16108 if (w->force_start)
16110 /* We set this later on if we have to adjust point. */
16111 int new_vpos = -1;
16113 w->force_start = 0;
16114 w->vscroll = 0;
16115 w->window_end_valid = 0;
16117 /* Forget any recorded base line for line number display. */
16118 if (!buffer_unchanged_p)
16119 w->base_line_number = 0;
16121 /* Redisplay the mode line. Select the buffer properly for that.
16122 Also, run the hook window-scroll-functions
16123 because we have scrolled. */
16124 /* Note, we do this after clearing force_start because
16125 if there's an error, it is better to forget about force_start
16126 than to get into an infinite loop calling the hook functions
16127 and having them get more errors. */
16128 if (!update_mode_line
16129 || ! NILP (Vwindow_scroll_functions))
16131 update_mode_line = 1;
16132 w->update_mode_line = 1;
16133 startp = run_window_scroll_functions (window, startp);
16136 if (CHARPOS (startp) < BEGV)
16137 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
16138 else if (CHARPOS (startp) > ZV)
16139 SET_TEXT_POS (startp, ZV, ZV_BYTE);
16141 /* Redisplay, then check if cursor has been set during the
16142 redisplay. Give up if new fonts were loaded. */
16143 /* We used to issue a CHECK_MARGINS argument to try_window here,
16144 but this causes scrolling to fail when point begins inside
16145 the scroll margin (bug#148) -- cyd */
16146 if (!try_window (window, startp, 0))
16148 w->force_start = 1;
16149 clear_glyph_matrix (w->desired_matrix);
16150 goto need_larger_matrices;
16153 if (w->cursor.vpos < 0)
16155 /* If point does not appear, try to move point so it does
16156 appear. The desired matrix has been built above, so we
16157 can use it here. */
16158 new_vpos = window_box_height (w) / 2;
16161 if (!cursor_row_fully_visible_p (w, 0, 0))
16163 /* Point does appear, but on a line partly visible at end of window.
16164 Move it back to a fully-visible line. */
16165 new_vpos = window_box_height (w);
16166 /* But if window_box_height suggests a Y coordinate that is
16167 not less than we already have, that line will clearly not
16168 be fully visible, so give up and scroll the display.
16169 This can happen when the default face uses a font whose
16170 dimensions are different from the frame's default
16171 font. */
16172 if (new_vpos >= w->cursor.y)
16174 w->cursor.vpos = -1;
16175 clear_glyph_matrix (w->desired_matrix);
16176 goto try_to_scroll;
16179 else if (w->cursor.vpos >= 0)
16181 /* Some people insist on not letting point enter the scroll
16182 margin, even though this part handles windows that didn't
16183 scroll at all. */
16184 int window_total_lines
16185 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
16186 int margin = min (scroll_margin, window_total_lines / 4);
16187 int pixel_margin = margin * frame_line_height;
16188 bool header_line = WINDOW_WANTS_HEADER_LINE_P (w);
16190 /* Note: We add an extra FRAME_LINE_HEIGHT, because the loop
16191 below, which finds the row to move point to, advances by
16192 the Y coordinate of the _next_ row, see the definition of
16193 MATRIX_ROW_BOTTOM_Y. */
16194 if (w->cursor.vpos < margin + header_line)
16196 w->cursor.vpos = -1;
16197 clear_glyph_matrix (w->desired_matrix);
16198 goto try_to_scroll;
16200 else
16202 int window_height = window_box_height (w);
16204 if (header_line)
16205 window_height += CURRENT_HEADER_LINE_HEIGHT (w);
16206 if (w->cursor.y >= window_height - pixel_margin)
16208 w->cursor.vpos = -1;
16209 clear_glyph_matrix (w->desired_matrix);
16210 goto try_to_scroll;
16215 /* If we need to move point for either of the above reasons,
16216 now actually do it. */
16217 if (new_vpos >= 0)
16219 struct glyph_row *row;
16221 row = MATRIX_FIRST_TEXT_ROW (w->desired_matrix);
16222 while (MATRIX_ROW_BOTTOM_Y (row) < new_vpos)
16223 ++row;
16225 TEMP_SET_PT_BOTH (MATRIX_ROW_START_CHARPOS (row),
16226 MATRIX_ROW_START_BYTEPOS (row));
16228 if (w != XWINDOW (selected_window))
16229 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
16230 else if (current_buffer == old)
16231 SET_TEXT_POS (lpoint, PT, PT_BYTE);
16233 set_cursor_from_row (w, row, w->desired_matrix, 0, 0, 0, 0);
16235 /* Re-run pre-redisplay-function so it can update the region
16236 according to the new position of point. */
16237 /* Other than the cursor, w's redisplay is done so we can set its
16238 redisplay to false. Also the buffer's redisplay can be set to
16239 false, since propagate_buffer_redisplay should have already
16240 propagated its info to `w' anyway. */
16241 w->redisplay = false;
16242 XBUFFER (w->contents)->text->redisplay = false;
16243 safe__call1 (true, Vpre_redisplay_function, Fcons (window, Qnil));
16245 if (w->redisplay || XBUFFER (w->contents)->text->redisplay)
16247 /* pre-redisplay-function made changes (e.g. move the region)
16248 that require another round of redisplay. */
16249 clear_glyph_matrix (w->desired_matrix);
16250 if (!try_window (window, startp, 0))
16251 goto need_larger_matrices;
16254 if (w->cursor.vpos < 0 || !cursor_row_fully_visible_p (w, 0, 0))
16256 clear_glyph_matrix (w->desired_matrix);
16257 goto try_to_scroll;
16260 #ifdef GLYPH_DEBUG
16261 debug_method_add (w, "forced window start");
16262 #endif
16263 goto done;
16266 /* Handle case where text has not changed, only point, and it has
16267 not moved off the frame, and we are not retrying after hscroll.
16268 (current_matrix_up_to_date_p is nonzero when retrying.) */
16269 if (current_matrix_up_to_date_p
16270 && (rc = try_cursor_movement (window, startp, &temp_scroll_step),
16271 rc != CURSOR_MOVEMENT_CANNOT_BE_USED))
16273 switch (rc)
16275 case CURSOR_MOVEMENT_SUCCESS:
16276 used_current_matrix_p = 1;
16277 goto done;
16279 case CURSOR_MOVEMENT_MUST_SCROLL:
16280 goto try_to_scroll;
16282 default:
16283 emacs_abort ();
16286 /* If current starting point was originally the beginning of a line
16287 but no longer is, find a new starting point. */
16288 else if (w->start_at_line_beg
16289 && !(CHARPOS (startp) <= BEGV
16290 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n'))
16292 #ifdef GLYPH_DEBUG
16293 debug_method_add (w, "recenter 1");
16294 #endif
16295 goto recenter;
16298 /* Try scrolling with try_window_id. Value is > 0 if update has
16299 been done, it is -1 if we know that the same window start will
16300 not work. It is 0 if unsuccessful for some other reason. */
16301 else if ((tem = try_window_id (w)) != 0)
16303 #ifdef GLYPH_DEBUG
16304 debug_method_add (w, "try_window_id %d", tem);
16305 #endif
16307 if (f->fonts_changed)
16308 goto need_larger_matrices;
16309 if (tem > 0)
16310 goto done;
16312 /* Otherwise try_window_id has returned -1 which means that we
16313 don't want the alternative below this comment to execute. */
16315 else if (CHARPOS (startp) >= BEGV
16316 && CHARPOS (startp) <= ZV
16317 && PT >= CHARPOS (startp)
16318 && (CHARPOS (startp) < ZV
16319 /* Avoid starting at end of buffer. */
16320 || CHARPOS (startp) == BEGV
16321 || !window_outdated (w)))
16323 int d1, d2, d5, d6;
16324 int rtop, rbot;
16326 /* If first window line is a continuation line, and window start
16327 is inside the modified region, but the first change is before
16328 current window start, we must select a new window start.
16330 However, if this is the result of a down-mouse event (e.g. by
16331 extending the mouse-drag-overlay), we don't want to select a
16332 new window start, since that would change the position under
16333 the mouse, resulting in an unwanted mouse-movement rather
16334 than a simple mouse-click. */
16335 if (!w->start_at_line_beg
16336 && NILP (do_mouse_tracking)
16337 && CHARPOS (startp) > BEGV
16338 && CHARPOS (startp) > BEG + beg_unchanged
16339 && CHARPOS (startp) <= Z - end_unchanged
16340 /* Even if w->start_at_line_beg is nil, a new window may
16341 start at a line_beg, since that's how set_buffer_window
16342 sets it. So, we need to check the return value of
16343 compute_window_start_on_continuation_line. (See also
16344 bug#197). */
16345 && XMARKER (w->start)->buffer == current_buffer
16346 && compute_window_start_on_continuation_line (w)
16347 /* It doesn't make sense to force the window start like we
16348 do at label force_start if it is already known that point
16349 will not be fully visible in the resulting window, because
16350 doing so will move point from its correct position
16351 instead of scrolling the window to bring point into view.
16352 See bug#9324. */
16353 && pos_visible_p (w, PT, &d1, &d2, &rtop, &rbot, &d5, &d6)
16354 /* A very tall row could need more than the window height,
16355 in which case we accept that it is partially visible. */
16356 && (rtop != 0) == (rbot != 0))
16358 w->force_start = 1;
16359 SET_TEXT_POS_FROM_MARKER (startp, w->start);
16360 #ifdef GLYPH_DEBUG
16361 debug_method_add (w, "recomputed window start in continuation line");
16362 #endif
16363 goto force_start;
16366 #ifdef GLYPH_DEBUG
16367 debug_method_add (w, "same window start");
16368 #endif
16370 /* Try to redisplay starting at same place as before.
16371 If point has not moved off frame, accept the results. */
16372 if (!current_matrix_up_to_date_p
16373 /* Don't use try_window_reusing_current_matrix in this case
16374 because a window scroll function can have changed the
16375 buffer. */
16376 || !NILP (Vwindow_scroll_functions)
16377 || MINI_WINDOW_P (w)
16378 || !(used_current_matrix_p
16379 = try_window_reusing_current_matrix (w)))
16381 IF_DEBUG (debug_method_add (w, "1"));
16382 if (try_window (window, startp, TRY_WINDOW_CHECK_MARGINS) < 0)
16383 /* -1 means we need to scroll.
16384 0 means we need new matrices, but fonts_changed
16385 is set in that case, so we will detect it below. */
16386 goto try_to_scroll;
16389 if (f->fonts_changed)
16390 goto need_larger_matrices;
16392 if (w->cursor.vpos >= 0)
16394 if (!just_this_one_p
16395 || current_buffer->clip_changed
16396 || BEG_UNCHANGED < CHARPOS (startp))
16397 /* Forget any recorded base line for line number display. */
16398 w->base_line_number = 0;
16400 if (!cursor_row_fully_visible_p (w, 1, 0))
16402 clear_glyph_matrix (w->desired_matrix);
16403 last_line_misfit = 1;
16405 /* Drop through and scroll. */
16406 else
16407 goto done;
16409 else
16410 clear_glyph_matrix (w->desired_matrix);
16413 try_to_scroll:
16415 /* Redisplay the mode line. Select the buffer properly for that. */
16416 if (!update_mode_line)
16418 update_mode_line = 1;
16419 w->update_mode_line = 1;
16422 /* Try to scroll by specified few lines. */
16423 if ((scroll_conservatively
16424 || emacs_scroll_step
16425 || temp_scroll_step
16426 || NUMBERP (BVAR (current_buffer, scroll_up_aggressively))
16427 || NUMBERP (BVAR (current_buffer, scroll_down_aggressively)))
16428 && CHARPOS (startp) >= BEGV
16429 && CHARPOS (startp) <= ZV)
16431 /* The function returns -1 if new fonts were loaded, 1 if
16432 successful, 0 if not successful. */
16433 int ss = try_scrolling (window, just_this_one_p,
16434 scroll_conservatively,
16435 emacs_scroll_step,
16436 temp_scroll_step, last_line_misfit);
16437 switch (ss)
16439 case SCROLLING_SUCCESS:
16440 goto done;
16442 case SCROLLING_NEED_LARGER_MATRICES:
16443 goto need_larger_matrices;
16445 case SCROLLING_FAILED:
16446 break;
16448 default:
16449 emacs_abort ();
16453 /* Finally, just choose a place to start which positions point
16454 according to user preferences. */
16456 recenter:
16458 #ifdef GLYPH_DEBUG
16459 debug_method_add (w, "recenter");
16460 #endif
16462 /* Forget any previously recorded base line for line number display. */
16463 if (!buffer_unchanged_p)
16464 w->base_line_number = 0;
16466 /* Determine the window start relative to point. */
16467 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
16468 it.current_y = it.last_visible_y;
16469 if (centering_position < 0)
16471 int window_total_lines
16472 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
16473 int margin
16474 = scroll_margin > 0
16475 ? min (scroll_margin, window_total_lines / 4)
16476 : 0;
16477 ptrdiff_t margin_pos = CHARPOS (startp);
16478 Lisp_Object aggressive;
16479 int scrolling_up;
16481 /* If there is a scroll margin at the top of the window, find
16482 its character position. */
16483 if (margin
16484 /* Cannot call start_display if startp is not in the
16485 accessible region of the buffer. This can happen when we
16486 have just switched to a different buffer and/or changed
16487 its restriction. In that case, startp is initialized to
16488 the character position 1 (BEGV) because we did not yet
16489 have chance to display the buffer even once. */
16490 && BEGV <= CHARPOS (startp) && CHARPOS (startp) <= ZV)
16492 struct it it1;
16493 void *it1data = NULL;
16495 SAVE_IT (it1, it, it1data);
16496 start_display (&it1, w, startp);
16497 move_it_vertically (&it1, margin * frame_line_height);
16498 margin_pos = IT_CHARPOS (it1);
16499 RESTORE_IT (&it, &it, it1data);
16501 scrolling_up = PT > margin_pos;
16502 aggressive =
16503 scrolling_up
16504 ? BVAR (current_buffer, scroll_up_aggressively)
16505 : BVAR (current_buffer, scroll_down_aggressively);
16507 if (!MINI_WINDOW_P (w)
16508 && (scroll_conservatively > SCROLL_LIMIT || NUMBERP (aggressive)))
16510 int pt_offset = 0;
16512 /* Setting scroll-conservatively overrides
16513 scroll-*-aggressively. */
16514 if (!scroll_conservatively && NUMBERP (aggressive))
16516 double float_amount = XFLOATINT (aggressive);
16518 pt_offset = float_amount * WINDOW_BOX_TEXT_HEIGHT (w);
16519 if (pt_offset == 0 && float_amount > 0)
16520 pt_offset = 1;
16521 if (pt_offset && margin > 0)
16522 margin -= 1;
16524 /* Compute how much to move the window start backward from
16525 point so that point will be displayed where the user
16526 wants it. */
16527 if (scrolling_up)
16529 centering_position = it.last_visible_y;
16530 if (pt_offset)
16531 centering_position -= pt_offset;
16532 centering_position -=
16533 frame_line_height * (1 + margin + (last_line_misfit != 0))
16534 + WINDOW_HEADER_LINE_HEIGHT (w);
16535 /* Don't let point enter the scroll margin near top of
16536 the window. */
16537 if (centering_position < margin * frame_line_height)
16538 centering_position = margin * frame_line_height;
16540 else
16541 centering_position = margin * frame_line_height + pt_offset;
16543 else
16544 /* Set the window start half the height of the window backward
16545 from point. */
16546 centering_position = window_box_height (w) / 2;
16548 move_it_vertically_backward (&it, centering_position);
16550 eassert (IT_CHARPOS (it) >= BEGV);
16552 /* The function move_it_vertically_backward may move over more
16553 than the specified y-distance. If it->w is small, e.g. a
16554 mini-buffer window, we may end up in front of the window's
16555 display area. Start displaying at the start of the line
16556 containing PT in this case. */
16557 if (it.current_y <= 0)
16559 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
16560 move_it_vertically_backward (&it, 0);
16561 it.current_y = 0;
16564 it.current_x = it.hpos = 0;
16566 /* Set the window start position here explicitly, to avoid an
16567 infinite loop in case the functions in window-scroll-functions
16568 get errors. */
16569 set_marker_both (w->start, Qnil, IT_CHARPOS (it), IT_BYTEPOS (it));
16571 /* Run scroll hooks. */
16572 startp = run_window_scroll_functions (window, it.current.pos);
16574 /* Redisplay the window. */
16575 if (!current_matrix_up_to_date_p
16576 || windows_or_buffers_changed
16577 || f->cursor_type_changed
16578 /* Don't use try_window_reusing_current_matrix in this case
16579 because it can have changed the buffer. */
16580 || !NILP (Vwindow_scroll_functions)
16581 || !just_this_one_p
16582 || MINI_WINDOW_P (w)
16583 || !(used_current_matrix_p
16584 = try_window_reusing_current_matrix (w)))
16585 try_window (window, startp, 0);
16587 /* If new fonts have been loaded (due to fontsets), give up. We
16588 have to start a new redisplay since we need to re-adjust glyph
16589 matrices. */
16590 if (f->fonts_changed)
16591 goto need_larger_matrices;
16593 /* If cursor did not appear assume that the middle of the window is
16594 in the first line of the window. Do it again with the next line.
16595 (Imagine a window of height 100, displaying two lines of height
16596 60. Moving back 50 from it->last_visible_y will end in the first
16597 line.) */
16598 if (w->cursor.vpos < 0)
16600 if (w->window_end_valid && PT >= Z - w->window_end_pos)
16602 clear_glyph_matrix (w->desired_matrix);
16603 move_it_by_lines (&it, 1);
16604 try_window (window, it.current.pos, 0);
16606 else if (PT < IT_CHARPOS (it))
16608 clear_glyph_matrix (w->desired_matrix);
16609 move_it_by_lines (&it, -1);
16610 try_window (window, it.current.pos, 0);
16612 else
16614 /* Not much we can do about it. */
16618 /* Consider the following case: Window starts at BEGV, there is
16619 invisible, intangible text at BEGV, so that display starts at
16620 some point START > BEGV. It can happen that we are called with
16621 PT somewhere between BEGV and START. Try to handle that case,
16622 and similar ones. */
16623 if (w->cursor.vpos < 0)
16625 /* First, try locating the proper glyph row for PT. */
16626 struct glyph_row *row =
16627 row_containing_pos (w, PT, w->current_matrix->rows, NULL, 0);
16629 /* Sometimes point is at the beginning of invisible text that is
16630 before the 1st character displayed in the row. In that case,
16631 row_containing_pos fails to find the row, because no glyphs
16632 with appropriate buffer positions are present in the row.
16633 Therefore, we next try to find the row which shows the 1st
16634 position after the invisible text. */
16635 if (!row)
16637 Lisp_Object val =
16638 get_char_property_and_overlay (make_number (PT), Qinvisible,
16639 Qnil, NULL);
16641 if (TEXT_PROP_MEANS_INVISIBLE (val))
16643 ptrdiff_t alt_pos;
16644 Lisp_Object invis_end =
16645 Fnext_single_char_property_change (make_number (PT), Qinvisible,
16646 Qnil, Qnil);
16648 if (NATNUMP (invis_end))
16649 alt_pos = XFASTINT (invis_end);
16650 else
16651 alt_pos = ZV;
16652 row = row_containing_pos (w, alt_pos, w->current_matrix->rows,
16653 NULL, 0);
16656 /* Finally, fall back on the first row of the window after the
16657 header line (if any). This is slightly better than not
16658 displaying the cursor at all. */
16659 if (!row)
16661 row = w->current_matrix->rows;
16662 if (row->mode_line_p)
16663 ++row;
16665 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
16668 if (!cursor_row_fully_visible_p (w, 0, 0))
16670 /* If vscroll is enabled, disable it and try again. */
16671 if (w->vscroll)
16673 w->vscroll = 0;
16674 clear_glyph_matrix (w->desired_matrix);
16675 goto recenter;
16678 /* Users who set scroll-conservatively to a large number want
16679 point just above/below the scroll margin. If we ended up
16680 with point's row partially visible, move the window start to
16681 make that row fully visible and out of the margin. */
16682 if (scroll_conservatively > SCROLL_LIMIT)
16684 int window_total_lines
16685 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) * frame_line_height;
16686 int margin =
16687 scroll_margin > 0
16688 ? min (scroll_margin, window_total_lines / 4)
16689 : 0;
16690 int move_down = w->cursor.vpos >= window_total_lines / 2;
16692 move_it_by_lines (&it, move_down ? margin + 1 : -(margin + 1));
16693 clear_glyph_matrix (w->desired_matrix);
16694 if (1 == try_window (window, it.current.pos,
16695 TRY_WINDOW_CHECK_MARGINS))
16696 goto done;
16699 /* If centering point failed to make the whole line visible,
16700 put point at the top instead. That has to make the whole line
16701 visible, if it can be done. */
16702 if (centering_position == 0)
16703 goto done;
16705 clear_glyph_matrix (w->desired_matrix);
16706 centering_position = 0;
16707 goto recenter;
16710 done:
16712 SET_TEXT_POS_FROM_MARKER (startp, w->start);
16713 w->start_at_line_beg = (CHARPOS (startp) == BEGV
16714 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n');
16716 /* Display the mode line, if we must. */
16717 if ((update_mode_line
16718 /* If window not full width, must redo its mode line
16719 if (a) the window to its side is being redone and
16720 (b) we do a frame-based redisplay. This is a consequence
16721 of how inverted lines are drawn in frame-based redisplay. */
16722 || (!just_this_one_p
16723 && !FRAME_WINDOW_P (f)
16724 && !WINDOW_FULL_WIDTH_P (w))
16725 /* Line number to display. */
16726 || w->base_line_pos > 0
16727 /* Column number is displayed and different from the one displayed. */
16728 || (w->column_number_displayed != -1
16729 && (w->column_number_displayed != current_column ())))
16730 /* This means that the window has a mode line. */
16731 && (WINDOW_WANTS_MODELINE_P (w)
16732 || WINDOW_WANTS_HEADER_LINE_P (w)))
16735 display_mode_lines (w);
16737 /* If mode line height has changed, arrange for a thorough
16738 immediate redisplay using the correct mode line height. */
16739 if (WINDOW_WANTS_MODELINE_P (w)
16740 && CURRENT_MODE_LINE_HEIGHT (w) != DESIRED_MODE_LINE_HEIGHT (w))
16742 f->fonts_changed = 1;
16743 w->mode_line_height = -1;
16744 MATRIX_MODE_LINE_ROW (w->current_matrix)->height
16745 = DESIRED_MODE_LINE_HEIGHT (w);
16748 /* If header line height has changed, arrange for a thorough
16749 immediate redisplay using the correct header line height. */
16750 if (WINDOW_WANTS_HEADER_LINE_P (w)
16751 && CURRENT_HEADER_LINE_HEIGHT (w) != DESIRED_HEADER_LINE_HEIGHT (w))
16753 f->fonts_changed = 1;
16754 w->header_line_height = -1;
16755 MATRIX_HEADER_LINE_ROW (w->current_matrix)->height
16756 = DESIRED_HEADER_LINE_HEIGHT (w);
16759 if (f->fonts_changed)
16760 goto need_larger_matrices;
16763 if (!line_number_displayed && w->base_line_pos != -1)
16765 w->base_line_pos = 0;
16766 w->base_line_number = 0;
16769 finish_menu_bars:
16771 /* When we reach a frame's selected window, redo the frame's menu bar. */
16772 if (update_mode_line
16773 && EQ (FRAME_SELECTED_WINDOW (f), window))
16775 int redisplay_menu_p = 0;
16777 if (FRAME_WINDOW_P (f))
16779 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
16780 || defined (HAVE_NS) || defined (USE_GTK)
16781 redisplay_menu_p = FRAME_EXTERNAL_MENU_BAR (f);
16782 #else
16783 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
16784 #endif
16786 else
16787 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
16789 if (redisplay_menu_p)
16790 display_menu_bar (w);
16792 #ifdef HAVE_WINDOW_SYSTEM
16793 if (FRAME_WINDOW_P (f))
16795 #if defined (USE_GTK) || defined (HAVE_NS)
16796 if (FRAME_EXTERNAL_TOOL_BAR (f))
16797 redisplay_tool_bar (f);
16798 #else
16799 if (WINDOWP (f->tool_bar_window)
16800 && (FRAME_TOOL_BAR_LINES (f) > 0
16801 || !NILP (Vauto_resize_tool_bars))
16802 && redisplay_tool_bar (f))
16803 ignore_mouse_drag_p = 1;
16804 #endif
16806 #endif
16809 #ifdef HAVE_WINDOW_SYSTEM
16810 if (FRAME_WINDOW_P (f)
16811 && update_window_fringes (w, (just_this_one_p
16812 || (!used_current_matrix_p && !overlay_arrow_seen)
16813 || w->pseudo_window_p)))
16815 update_begin (f);
16816 block_input ();
16817 if (draw_window_fringes (w, 1))
16819 if (WINDOW_RIGHT_DIVIDER_WIDTH (w))
16820 x_draw_right_divider (w);
16821 else
16822 x_draw_vertical_border (w);
16824 unblock_input ();
16825 update_end (f);
16828 if (WINDOW_BOTTOM_DIVIDER_WIDTH (w))
16829 x_draw_bottom_divider (w);
16830 #endif /* HAVE_WINDOW_SYSTEM */
16832 /* We go to this label, with fonts_changed set, if it is
16833 necessary to try again using larger glyph matrices.
16834 We have to redeem the scroll bar even in this case,
16835 because the loop in redisplay_internal expects that. */
16836 need_larger_matrices:
16838 finish_scroll_bars:
16840 if (WINDOW_HAS_VERTICAL_SCROLL_BAR (w) || WINDOW_HAS_HORIZONTAL_SCROLL_BAR (w))
16842 if (WINDOW_HAS_VERTICAL_SCROLL_BAR (w))
16843 /* Set the thumb's position and size. */
16844 set_vertical_scroll_bar (w);
16846 if (WINDOW_HAS_HORIZONTAL_SCROLL_BAR (w))
16847 /* Set the thumb's position and size. */
16848 set_horizontal_scroll_bar (w);
16850 /* Note that we actually used the scroll bar attached to this
16851 window, so it shouldn't be deleted at the end of redisplay. */
16852 if (FRAME_TERMINAL (f)->redeem_scroll_bar_hook)
16853 (*FRAME_TERMINAL (f)->redeem_scroll_bar_hook) (w);
16856 /* Restore current_buffer and value of point in it. The window
16857 update may have changed the buffer, so first make sure `opoint'
16858 is still valid (Bug#6177). */
16859 if (CHARPOS (opoint) < BEGV)
16860 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
16861 else if (CHARPOS (opoint) > ZV)
16862 TEMP_SET_PT_BOTH (Z, Z_BYTE);
16863 else
16864 TEMP_SET_PT_BOTH (CHARPOS (opoint), BYTEPOS (opoint));
16866 set_buffer_internal_1 (old);
16867 /* Avoid an abort in TEMP_SET_PT_BOTH if the buffer has become
16868 shorter. This can be caused by log truncation in *Messages*. */
16869 if (CHARPOS (lpoint) <= ZV)
16870 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
16872 unbind_to (count, Qnil);
16876 /* Build the complete desired matrix of WINDOW with a window start
16877 buffer position POS.
16879 Value is 1 if successful. It is zero if fonts were loaded during
16880 redisplay which makes re-adjusting glyph matrices necessary, and -1
16881 if point would appear in the scroll margins.
16882 (We check the former only if TRY_WINDOW_IGNORE_FONTS_CHANGE is
16883 unset in FLAGS, and the latter only if TRY_WINDOW_CHECK_MARGINS is
16884 set in FLAGS.) */
16887 try_window (Lisp_Object window, struct text_pos pos, int flags)
16889 struct window *w = XWINDOW (window);
16890 struct it it;
16891 struct glyph_row *last_text_row = NULL;
16892 struct frame *f = XFRAME (w->frame);
16893 int frame_line_height = default_line_pixel_height (w);
16895 /* Make POS the new window start. */
16896 set_marker_both (w->start, Qnil, CHARPOS (pos), BYTEPOS (pos));
16898 /* Mark cursor position as unknown. No overlay arrow seen. */
16899 w->cursor.vpos = -1;
16900 overlay_arrow_seen = 0;
16902 /* Initialize iterator and info to start at POS. */
16903 start_display (&it, w, pos);
16904 it.glyph_row->reversed_p = false;
16906 /* Display all lines of W. */
16907 while (it.current_y < it.last_visible_y)
16909 if (display_line (&it))
16910 last_text_row = it.glyph_row - 1;
16911 if (f->fonts_changed && !(flags & TRY_WINDOW_IGNORE_FONTS_CHANGE))
16912 return 0;
16915 /* Don't let the cursor end in the scroll margins. */
16916 if ((flags & TRY_WINDOW_CHECK_MARGINS)
16917 && !MINI_WINDOW_P (w))
16919 int this_scroll_margin;
16920 int window_total_lines
16921 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
16923 if (scroll_margin > 0)
16925 this_scroll_margin = min (scroll_margin, window_total_lines / 4);
16926 this_scroll_margin *= frame_line_height;
16928 else
16929 this_scroll_margin = 0;
16931 if ((w->cursor.y >= 0 /* not vscrolled */
16932 && w->cursor.y < this_scroll_margin
16933 && CHARPOS (pos) > BEGV
16934 && IT_CHARPOS (it) < ZV)
16935 /* rms: considering make_cursor_line_fully_visible_p here
16936 seems to give wrong results. We don't want to recenter
16937 when the last line is partly visible, we want to allow
16938 that case to be handled in the usual way. */
16939 || w->cursor.y > it.last_visible_y - this_scroll_margin - 1)
16941 w->cursor.vpos = -1;
16942 clear_glyph_matrix (w->desired_matrix);
16943 return -1;
16947 /* If bottom moved off end of frame, change mode line percentage. */
16948 if (w->window_end_pos <= 0 && Z != IT_CHARPOS (it))
16949 w->update_mode_line = 1;
16951 /* Set window_end_pos to the offset of the last character displayed
16952 on the window from the end of current_buffer. Set
16953 window_end_vpos to its row number. */
16954 if (last_text_row)
16956 eassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_text_row));
16957 adjust_window_ends (w, last_text_row, 0);
16958 eassert
16959 (MATRIX_ROW_DISPLAYS_TEXT_P (MATRIX_ROW (w->desired_matrix,
16960 w->window_end_vpos)));
16962 else
16964 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
16965 w->window_end_pos = Z - ZV;
16966 w->window_end_vpos = 0;
16969 /* But that is not valid info until redisplay finishes. */
16970 w->window_end_valid = 0;
16971 return 1;
16976 /************************************************************************
16977 Window redisplay reusing current matrix when buffer has not changed
16978 ************************************************************************/
16980 /* Try redisplay of window W showing an unchanged buffer with a
16981 different window start than the last time it was displayed by
16982 reusing its current matrix. Value is non-zero if successful.
16983 W->start is the new window start. */
16985 static int
16986 try_window_reusing_current_matrix (struct window *w)
16988 struct frame *f = XFRAME (w->frame);
16989 struct glyph_row *bottom_row;
16990 struct it it;
16991 struct run run;
16992 struct text_pos start, new_start;
16993 int nrows_scrolled, i;
16994 struct glyph_row *last_text_row;
16995 struct glyph_row *last_reused_text_row;
16996 struct glyph_row *start_row;
16997 int start_vpos, min_y, max_y;
16999 #ifdef GLYPH_DEBUG
17000 if (inhibit_try_window_reusing)
17001 return 0;
17002 #endif
17004 if (/* This function doesn't handle terminal frames. */
17005 !FRAME_WINDOW_P (f)
17006 /* Don't try to reuse the display if windows have been split
17007 or such. */
17008 || windows_or_buffers_changed
17009 || f->cursor_type_changed)
17010 return 0;
17012 /* Can't do this if showing trailing whitespace. */
17013 if (!NILP (Vshow_trailing_whitespace))
17014 return 0;
17016 /* If top-line visibility has changed, give up. */
17017 if (WINDOW_WANTS_HEADER_LINE_P (w)
17018 != MATRIX_HEADER_LINE_ROW (w->current_matrix)->mode_line_p)
17019 return 0;
17021 /* Give up if old or new display is scrolled vertically. We could
17022 make this function handle this, but right now it doesn't. */
17023 start_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
17024 if (w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row))
17025 return 0;
17027 /* The variable new_start now holds the new window start. The old
17028 start `start' can be determined from the current matrix. */
17029 SET_TEXT_POS_FROM_MARKER (new_start, w->start);
17030 start = start_row->minpos;
17031 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
17033 /* Clear the desired matrix for the display below. */
17034 clear_glyph_matrix (w->desired_matrix);
17036 if (CHARPOS (new_start) <= CHARPOS (start))
17038 /* Don't use this method if the display starts with an ellipsis
17039 displayed for invisible text. It's not easy to handle that case
17040 below, and it's certainly not worth the effort since this is
17041 not a frequent case. */
17042 if (in_ellipses_for_invisible_text_p (&start_row->start, w))
17043 return 0;
17045 IF_DEBUG (debug_method_add (w, "twu1"));
17047 /* Display up to a row that can be reused. The variable
17048 last_text_row is set to the last row displayed that displays
17049 text. Note that it.vpos == 0 if or if not there is a
17050 header-line; it's not the same as the MATRIX_ROW_VPOS! */
17051 start_display (&it, w, new_start);
17052 w->cursor.vpos = -1;
17053 last_text_row = last_reused_text_row = NULL;
17055 while (it.current_y < it.last_visible_y && !f->fonts_changed)
17057 /* If we have reached into the characters in the START row,
17058 that means the line boundaries have changed. So we
17059 can't start copying with the row START. Maybe it will
17060 work to start copying with the following row. */
17061 while (IT_CHARPOS (it) > CHARPOS (start))
17063 /* Advance to the next row as the "start". */
17064 start_row++;
17065 start = start_row->minpos;
17066 /* If there are no more rows to try, or just one, give up. */
17067 if (start_row == MATRIX_MODE_LINE_ROW (w->current_matrix) - 1
17068 || w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row)
17069 || CHARPOS (start) == ZV)
17071 clear_glyph_matrix (w->desired_matrix);
17072 return 0;
17075 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
17077 /* If we have reached alignment, we can copy the rest of the
17078 rows. */
17079 if (IT_CHARPOS (it) == CHARPOS (start)
17080 /* Don't accept "alignment" inside a display vector,
17081 since start_row could have started in the middle of
17082 that same display vector (thus their character
17083 positions match), and we have no way of telling if
17084 that is the case. */
17085 && it.current.dpvec_index < 0)
17086 break;
17088 it.glyph_row->reversed_p = false;
17089 if (display_line (&it))
17090 last_text_row = it.glyph_row - 1;
17094 /* A value of current_y < last_visible_y means that we stopped
17095 at the previous window start, which in turn means that we
17096 have at least one reusable row. */
17097 if (it.current_y < it.last_visible_y)
17099 struct glyph_row *row;
17101 /* IT.vpos always starts from 0; it counts text lines. */
17102 nrows_scrolled = it.vpos - (start_row - MATRIX_FIRST_TEXT_ROW (w->current_matrix));
17104 /* Find PT if not already found in the lines displayed. */
17105 if (w->cursor.vpos < 0)
17107 int dy = it.current_y - start_row->y;
17109 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
17110 row = row_containing_pos (w, PT, row, NULL, dy);
17111 if (row)
17112 set_cursor_from_row (w, row, w->current_matrix, 0, 0,
17113 dy, nrows_scrolled);
17114 else
17116 clear_glyph_matrix (w->desired_matrix);
17117 return 0;
17121 /* Scroll the display. Do it before the current matrix is
17122 changed. The problem here is that update has not yet
17123 run, i.e. part of the current matrix is not up to date.
17124 scroll_run_hook will clear the cursor, and use the
17125 current matrix to get the height of the row the cursor is
17126 in. */
17127 run.current_y = start_row->y;
17128 run.desired_y = it.current_y;
17129 run.height = it.last_visible_y - it.current_y;
17131 if (run.height > 0 && run.current_y != run.desired_y)
17133 update_begin (f);
17134 FRAME_RIF (f)->update_window_begin_hook (w);
17135 FRAME_RIF (f)->clear_window_mouse_face (w);
17136 FRAME_RIF (f)->scroll_run_hook (w, &run);
17137 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
17138 update_end (f);
17141 /* Shift current matrix down by nrows_scrolled lines. */
17142 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
17143 rotate_matrix (w->current_matrix,
17144 start_vpos,
17145 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
17146 nrows_scrolled);
17148 /* Disable lines that must be updated. */
17149 for (i = 0; i < nrows_scrolled; ++i)
17150 (start_row + i)->enabled_p = false;
17152 /* Re-compute Y positions. */
17153 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
17154 max_y = it.last_visible_y;
17155 for (row = start_row + nrows_scrolled;
17156 row < bottom_row;
17157 ++row)
17159 row->y = it.current_y;
17160 row->visible_height = row->height;
17162 if (row->y < min_y)
17163 row->visible_height -= min_y - row->y;
17164 if (row->y + row->height > max_y)
17165 row->visible_height -= row->y + row->height - max_y;
17166 if (row->fringe_bitmap_periodic_p)
17167 row->redraw_fringe_bitmaps_p = 1;
17169 it.current_y += row->height;
17171 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
17172 last_reused_text_row = row;
17173 if (MATRIX_ROW_BOTTOM_Y (row) >= it.last_visible_y)
17174 break;
17177 /* Disable lines in the current matrix which are now
17178 below the window. */
17179 for (++row; row < bottom_row; ++row)
17180 row->enabled_p = row->mode_line_p = 0;
17183 /* Update window_end_pos etc.; last_reused_text_row is the last
17184 reused row from the current matrix containing text, if any.
17185 The value of last_text_row is the last displayed line
17186 containing text. */
17187 if (last_reused_text_row)
17188 adjust_window_ends (w, last_reused_text_row, 1);
17189 else if (last_text_row)
17190 adjust_window_ends (w, last_text_row, 0);
17191 else
17193 /* This window must be completely empty. */
17194 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
17195 w->window_end_pos = Z - ZV;
17196 w->window_end_vpos = 0;
17198 w->window_end_valid = 0;
17200 /* Update hint: don't try scrolling again in update_window. */
17201 w->desired_matrix->no_scrolling_p = 1;
17203 #ifdef GLYPH_DEBUG
17204 debug_method_add (w, "try_window_reusing_current_matrix 1");
17205 #endif
17206 return 1;
17208 else if (CHARPOS (new_start) > CHARPOS (start))
17210 struct glyph_row *pt_row, *row;
17211 struct glyph_row *first_reusable_row;
17212 struct glyph_row *first_row_to_display;
17213 int dy;
17214 int yb = window_text_bottom_y (w);
17216 /* Find the row starting at new_start, if there is one. Don't
17217 reuse a partially visible line at the end. */
17218 first_reusable_row = start_row;
17219 while (first_reusable_row->enabled_p
17220 && MATRIX_ROW_BOTTOM_Y (first_reusable_row) < yb
17221 && (MATRIX_ROW_START_CHARPOS (first_reusable_row)
17222 < CHARPOS (new_start)))
17223 ++first_reusable_row;
17225 /* Give up if there is no row to reuse. */
17226 if (MATRIX_ROW_BOTTOM_Y (first_reusable_row) >= yb
17227 || !first_reusable_row->enabled_p
17228 || (MATRIX_ROW_START_CHARPOS (first_reusable_row)
17229 != CHARPOS (new_start)))
17230 return 0;
17232 /* We can reuse fully visible rows beginning with
17233 first_reusable_row to the end of the window. Set
17234 first_row_to_display to the first row that cannot be reused.
17235 Set pt_row to the row containing point, if there is any. */
17236 pt_row = NULL;
17237 for (first_row_to_display = first_reusable_row;
17238 MATRIX_ROW_BOTTOM_Y (first_row_to_display) < yb;
17239 ++first_row_to_display)
17241 if (PT >= MATRIX_ROW_START_CHARPOS (first_row_to_display)
17242 && (PT < MATRIX_ROW_END_CHARPOS (first_row_to_display)
17243 || (PT == MATRIX_ROW_END_CHARPOS (first_row_to_display)
17244 && first_row_to_display->ends_at_zv_p
17245 && pt_row == NULL)))
17246 pt_row = first_row_to_display;
17249 /* Start displaying at the start of first_row_to_display. */
17250 eassert (first_row_to_display->y < yb);
17251 init_to_row_start (&it, w, first_row_to_display);
17253 nrows_scrolled = (MATRIX_ROW_VPOS (first_reusable_row, w->current_matrix)
17254 - start_vpos);
17255 it.vpos = (MATRIX_ROW_VPOS (first_row_to_display, w->current_matrix)
17256 - nrows_scrolled);
17257 it.current_y = (first_row_to_display->y - first_reusable_row->y
17258 + WINDOW_HEADER_LINE_HEIGHT (w));
17260 /* Display lines beginning with first_row_to_display in the
17261 desired matrix. Set last_text_row to the last row displayed
17262 that displays text. */
17263 it.glyph_row = MATRIX_ROW (w->desired_matrix, it.vpos);
17264 if (pt_row == NULL)
17265 w->cursor.vpos = -1;
17266 last_text_row = NULL;
17267 while (it.current_y < it.last_visible_y && !f->fonts_changed)
17268 if (display_line (&it))
17269 last_text_row = it.glyph_row - 1;
17271 /* If point is in a reused row, adjust y and vpos of the cursor
17272 position. */
17273 if (pt_row)
17275 w->cursor.vpos -= nrows_scrolled;
17276 w->cursor.y -= first_reusable_row->y - start_row->y;
17279 /* Give up if point isn't in a row displayed or reused. (This
17280 also handles the case where w->cursor.vpos < nrows_scrolled
17281 after the calls to display_line, which can happen with scroll
17282 margins. See bug#1295.) */
17283 if (w->cursor.vpos < 0)
17285 clear_glyph_matrix (w->desired_matrix);
17286 return 0;
17289 /* Scroll the display. */
17290 run.current_y = first_reusable_row->y;
17291 run.desired_y = WINDOW_HEADER_LINE_HEIGHT (w);
17292 run.height = it.last_visible_y - run.current_y;
17293 dy = run.current_y - run.desired_y;
17295 if (run.height)
17297 update_begin (f);
17298 FRAME_RIF (f)->update_window_begin_hook (w);
17299 FRAME_RIF (f)->clear_window_mouse_face (w);
17300 FRAME_RIF (f)->scroll_run_hook (w, &run);
17301 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
17302 update_end (f);
17305 /* Adjust Y positions of reused rows. */
17306 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
17307 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
17308 max_y = it.last_visible_y;
17309 for (row = first_reusable_row; row < first_row_to_display; ++row)
17311 row->y -= dy;
17312 row->visible_height = row->height;
17313 if (row->y < min_y)
17314 row->visible_height -= min_y - row->y;
17315 if (row->y + row->height > max_y)
17316 row->visible_height -= row->y + row->height - max_y;
17317 if (row->fringe_bitmap_periodic_p)
17318 row->redraw_fringe_bitmaps_p = 1;
17321 /* Scroll the current matrix. */
17322 eassert (nrows_scrolled > 0);
17323 rotate_matrix (w->current_matrix,
17324 start_vpos,
17325 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
17326 -nrows_scrolled);
17328 /* Disable rows not reused. */
17329 for (row -= nrows_scrolled; row < bottom_row; ++row)
17330 row->enabled_p = false;
17332 /* Point may have moved to a different line, so we cannot assume that
17333 the previous cursor position is valid; locate the correct row. */
17334 if (pt_row)
17336 for (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
17337 row < bottom_row
17338 && PT >= MATRIX_ROW_END_CHARPOS (row)
17339 && !row->ends_at_zv_p;
17340 row++)
17342 w->cursor.vpos++;
17343 w->cursor.y = row->y;
17345 if (row < bottom_row)
17347 /* Can't simply scan the row for point with
17348 bidi-reordered glyph rows. Let set_cursor_from_row
17349 figure out where to put the cursor, and if it fails,
17350 give up. */
17351 if (!NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering)))
17353 if (!set_cursor_from_row (w, row, w->current_matrix,
17354 0, 0, 0, 0))
17356 clear_glyph_matrix (w->desired_matrix);
17357 return 0;
17360 else
17362 struct glyph *glyph = row->glyphs[TEXT_AREA] + w->cursor.hpos;
17363 struct glyph *end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
17365 for (; glyph < end
17366 && (!BUFFERP (glyph->object)
17367 || glyph->charpos < PT);
17368 glyph++)
17370 w->cursor.hpos++;
17371 w->cursor.x += glyph->pixel_width;
17377 /* Adjust window end. A null value of last_text_row means that
17378 the window end is in reused rows which in turn means that
17379 only its vpos can have changed. */
17380 if (last_text_row)
17381 adjust_window_ends (w, last_text_row, 0);
17382 else
17383 w->window_end_vpos -= nrows_scrolled;
17385 w->window_end_valid = 0;
17386 w->desired_matrix->no_scrolling_p = 1;
17388 #ifdef GLYPH_DEBUG
17389 debug_method_add (w, "try_window_reusing_current_matrix 2");
17390 #endif
17391 return 1;
17394 return 0;
17399 /************************************************************************
17400 Window redisplay reusing current matrix when buffer has changed
17401 ************************************************************************/
17403 static struct glyph_row *find_last_unchanged_at_beg_row (struct window *);
17404 static struct glyph_row *find_first_unchanged_at_end_row (struct window *,
17405 ptrdiff_t *, ptrdiff_t *);
17406 static struct glyph_row *
17407 find_last_row_displaying_text (struct glyph_matrix *, struct it *,
17408 struct glyph_row *);
17411 /* Return the last row in MATRIX displaying text. If row START is
17412 non-null, start searching with that row. IT gives the dimensions
17413 of the display. Value is null if matrix is empty; otherwise it is
17414 a pointer to the row found. */
17416 static struct glyph_row *
17417 find_last_row_displaying_text (struct glyph_matrix *matrix, struct it *it,
17418 struct glyph_row *start)
17420 struct glyph_row *row, *row_found;
17422 /* Set row_found to the last row in IT->w's current matrix
17423 displaying text. The loop looks funny but think of partially
17424 visible lines. */
17425 row_found = NULL;
17426 row = start ? start : MATRIX_FIRST_TEXT_ROW (matrix);
17427 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
17429 eassert (row->enabled_p);
17430 row_found = row;
17431 if (MATRIX_ROW_BOTTOM_Y (row) >= it->last_visible_y)
17432 break;
17433 ++row;
17436 return row_found;
17440 /* Return the last row in the current matrix of W that is not affected
17441 by changes at the start of current_buffer that occurred since W's
17442 current matrix was built. Value is null if no such row exists.
17444 BEG_UNCHANGED us the number of characters unchanged at the start of
17445 current_buffer. BEG + BEG_UNCHANGED is the buffer position of the
17446 first changed character in current_buffer. Characters at positions <
17447 BEG + BEG_UNCHANGED are at the same buffer positions as they were
17448 when the current matrix was built. */
17450 static struct glyph_row *
17451 find_last_unchanged_at_beg_row (struct window *w)
17453 ptrdiff_t first_changed_pos = BEG + BEG_UNCHANGED;
17454 struct glyph_row *row;
17455 struct glyph_row *row_found = NULL;
17456 int yb = window_text_bottom_y (w);
17458 /* Find the last row displaying unchanged text. */
17459 for (row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
17460 MATRIX_ROW_DISPLAYS_TEXT_P (row)
17461 && MATRIX_ROW_START_CHARPOS (row) < first_changed_pos;
17462 ++row)
17464 if (/* If row ends before first_changed_pos, it is unchanged,
17465 except in some case. */
17466 MATRIX_ROW_END_CHARPOS (row) <= first_changed_pos
17467 /* When row ends in ZV and we write at ZV it is not
17468 unchanged. */
17469 && !row->ends_at_zv_p
17470 /* When first_changed_pos is the end of a continued line,
17471 row is not unchanged because it may be no longer
17472 continued. */
17473 && !(MATRIX_ROW_END_CHARPOS (row) == first_changed_pos
17474 && (row->continued_p
17475 || row->exact_window_width_line_p))
17476 /* If ROW->end is beyond ZV, then ROW->end is outdated and
17477 needs to be recomputed, so don't consider this row as
17478 unchanged. This happens when the last line was
17479 bidi-reordered and was killed immediately before this
17480 redisplay cycle. In that case, ROW->end stores the
17481 buffer position of the first visual-order character of
17482 the killed text, which is now beyond ZV. */
17483 && CHARPOS (row->end.pos) <= ZV)
17484 row_found = row;
17486 /* Stop if last visible row. */
17487 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
17488 break;
17491 return row_found;
17495 /* Find the first glyph row in the current matrix of W that is not
17496 affected by changes at the end of current_buffer since the
17497 time W's current matrix was built.
17499 Return in *DELTA the number of chars by which buffer positions in
17500 unchanged text at the end of current_buffer must be adjusted.
17502 Return in *DELTA_BYTES the corresponding number of bytes.
17504 Value is null if no such row exists, i.e. all rows are affected by
17505 changes. */
17507 static struct glyph_row *
17508 find_first_unchanged_at_end_row (struct window *w,
17509 ptrdiff_t *delta, ptrdiff_t *delta_bytes)
17511 struct glyph_row *row;
17512 struct glyph_row *row_found = NULL;
17514 *delta = *delta_bytes = 0;
17516 /* Display must not have been paused, otherwise the current matrix
17517 is not up to date. */
17518 eassert (w->window_end_valid);
17520 /* A value of window_end_pos >= END_UNCHANGED means that the window
17521 end is in the range of changed text. If so, there is no
17522 unchanged row at the end of W's current matrix. */
17523 if (w->window_end_pos >= END_UNCHANGED)
17524 return NULL;
17526 /* Set row to the last row in W's current matrix displaying text. */
17527 row = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
17529 /* If matrix is entirely empty, no unchanged row exists. */
17530 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
17532 /* The value of row is the last glyph row in the matrix having a
17533 meaningful buffer position in it. The end position of row
17534 corresponds to window_end_pos. This allows us to translate
17535 buffer positions in the current matrix to current buffer
17536 positions for characters not in changed text. */
17537 ptrdiff_t Z_old =
17538 MATRIX_ROW_END_CHARPOS (row) + w->window_end_pos;
17539 ptrdiff_t Z_BYTE_old =
17540 MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
17541 ptrdiff_t last_unchanged_pos, last_unchanged_pos_old;
17542 struct glyph_row *first_text_row
17543 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
17545 *delta = Z - Z_old;
17546 *delta_bytes = Z_BYTE - Z_BYTE_old;
17548 /* Set last_unchanged_pos to the buffer position of the last
17549 character in the buffer that has not been changed. Z is the
17550 index + 1 of the last character in current_buffer, i.e. by
17551 subtracting END_UNCHANGED we get the index of the last
17552 unchanged character, and we have to add BEG to get its buffer
17553 position. */
17554 last_unchanged_pos = Z - END_UNCHANGED + BEG;
17555 last_unchanged_pos_old = last_unchanged_pos - *delta;
17557 /* Search backward from ROW for a row displaying a line that
17558 starts at a minimum position >= last_unchanged_pos_old. */
17559 for (; row > first_text_row; --row)
17561 /* This used to abort, but it can happen.
17562 It is ok to just stop the search instead here. KFS. */
17563 if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
17564 break;
17566 if (MATRIX_ROW_START_CHARPOS (row) >= last_unchanged_pos_old)
17567 row_found = row;
17571 eassert (!row_found || MATRIX_ROW_DISPLAYS_TEXT_P (row_found));
17573 return row_found;
17577 /* Make sure that glyph rows in the current matrix of window W
17578 reference the same glyph memory as corresponding rows in the
17579 frame's frame matrix. This function is called after scrolling W's
17580 current matrix on a terminal frame in try_window_id and
17581 try_window_reusing_current_matrix. */
17583 static void
17584 sync_frame_with_window_matrix_rows (struct window *w)
17586 struct frame *f = XFRAME (w->frame);
17587 struct glyph_row *window_row, *window_row_end, *frame_row;
17589 /* Preconditions: W must be a leaf window and full-width. Its frame
17590 must have a frame matrix. */
17591 eassert (BUFFERP (w->contents));
17592 eassert (WINDOW_FULL_WIDTH_P (w));
17593 eassert (!FRAME_WINDOW_P (f));
17595 /* If W is a full-width window, glyph pointers in W's current matrix
17596 have, by definition, to be the same as glyph pointers in the
17597 corresponding frame matrix. Note that frame matrices have no
17598 marginal areas (see build_frame_matrix). */
17599 window_row = w->current_matrix->rows;
17600 window_row_end = window_row + w->current_matrix->nrows;
17601 frame_row = f->current_matrix->rows + WINDOW_TOP_EDGE_LINE (w);
17602 while (window_row < window_row_end)
17604 struct glyph *start = window_row->glyphs[LEFT_MARGIN_AREA];
17605 struct glyph *end = window_row->glyphs[LAST_AREA];
17607 frame_row->glyphs[LEFT_MARGIN_AREA] = start;
17608 frame_row->glyphs[TEXT_AREA] = start;
17609 frame_row->glyphs[RIGHT_MARGIN_AREA] = end;
17610 frame_row->glyphs[LAST_AREA] = end;
17612 /* Disable frame rows whose corresponding window rows have
17613 been disabled in try_window_id. */
17614 if (!window_row->enabled_p)
17615 frame_row->enabled_p = false;
17617 ++window_row, ++frame_row;
17622 /* Find the glyph row in window W containing CHARPOS. Consider all
17623 rows between START and END (not inclusive). END null means search
17624 all rows to the end of the display area of W. Value is the row
17625 containing CHARPOS or null. */
17627 struct glyph_row *
17628 row_containing_pos (struct window *w, ptrdiff_t charpos,
17629 struct glyph_row *start, struct glyph_row *end, int dy)
17631 struct glyph_row *row = start;
17632 struct glyph_row *best_row = NULL;
17633 ptrdiff_t mindif = BUF_ZV (XBUFFER (w->contents)) + 1;
17634 int last_y;
17636 /* If we happen to start on a header-line, skip that. */
17637 if (row->mode_line_p)
17638 ++row;
17640 if ((end && row >= end) || !row->enabled_p)
17641 return NULL;
17643 last_y = window_text_bottom_y (w) - dy;
17645 while (1)
17647 /* Give up if we have gone too far. */
17648 if (end && row >= end)
17649 return NULL;
17650 /* This formerly returned if they were equal.
17651 I think that both quantities are of a "last plus one" type;
17652 if so, when they are equal, the row is within the screen. -- rms. */
17653 if (MATRIX_ROW_BOTTOM_Y (row) > last_y)
17654 return NULL;
17656 /* If it is in this row, return this row. */
17657 if (! (MATRIX_ROW_END_CHARPOS (row) < charpos
17658 || (MATRIX_ROW_END_CHARPOS (row) == charpos
17659 /* The end position of a row equals the start
17660 position of the next row. If CHARPOS is there, we
17661 would rather consider it displayed in the next
17662 line, except when this line ends in ZV. */
17663 && !row_for_charpos_p (row, charpos)))
17664 && charpos >= MATRIX_ROW_START_CHARPOS (row))
17666 struct glyph *g;
17668 if (NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering))
17669 || (!best_row && !row->continued_p))
17670 return row;
17671 /* In bidi-reordered rows, there could be several rows whose
17672 edges surround CHARPOS, all of these rows belonging to
17673 the same continued line. We need to find the row which
17674 fits CHARPOS the best. */
17675 for (g = row->glyphs[TEXT_AREA];
17676 g < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
17677 g++)
17679 if (!STRINGP (g->object))
17681 if (g->charpos > 0 && eabs (g->charpos - charpos) < mindif)
17683 mindif = eabs (g->charpos - charpos);
17684 best_row = row;
17685 /* Exact match always wins. */
17686 if (mindif == 0)
17687 return best_row;
17692 else if (best_row && !row->continued_p)
17693 return best_row;
17694 ++row;
17699 /* Try to redisplay window W by reusing its existing display. W's
17700 current matrix must be up to date when this function is called,
17701 i.e. window_end_valid must be nonzero.
17703 Value is
17705 >= 1 if successful, i.e. display has been updated
17706 specifically:
17707 1 means the changes were in front of a newline that precedes
17708 the window start, and the whole current matrix was reused
17709 2 means the changes were after the last position displayed
17710 in the window, and the whole current matrix was reused
17711 3 means portions of the current matrix were reused, while
17712 some of the screen lines were redrawn
17713 -1 if redisplay with same window start is known not to succeed
17714 0 if otherwise unsuccessful
17716 The following steps are performed:
17718 1. Find the last row in the current matrix of W that is not
17719 affected by changes at the start of current_buffer. If no such row
17720 is found, give up.
17722 2. Find the first row in W's current matrix that is not affected by
17723 changes at the end of current_buffer. Maybe there is no such row.
17725 3. Display lines beginning with the row + 1 found in step 1 to the
17726 row found in step 2 or, if step 2 didn't find a row, to the end of
17727 the window.
17729 4. If cursor is not known to appear on the window, give up.
17731 5. If display stopped at the row found in step 2, scroll the
17732 display and current matrix as needed.
17734 6. Maybe display some lines at the end of W, if we must. This can
17735 happen under various circumstances, like a partially visible line
17736 becoming fully visible, or because newly displayed lines are displayed
17737 in smaller font sizes.
17739 7. Update W's window end information. */
17741 static int
17742 try_window_id (struct window *w)
17744 struct frame *f = XFRAME (w->frame);
17745 struct glyph_matrix *current_matrix = w->current_matrix;
17746 struct glyph_matrix *desired_matrix = w->desired_matrix;
17747 struct glyph_row *last_unchanged_at_beg_row;
17748 struct glyph_row *first_unchanged_at_end_row;
17749 struct glyph_row *row;
17750 struct glyph_row *bottom_row;
17751 int bottom_vpos;
17752 struct it it;
17753 ptrdiff_t delta = 0, delta_bytes = 0, stop_pos;
17754 int dvpos, dy;
17755 struct text_pos start_pos;
17756 struct run run;
17757 int first_unchanged_at_end_vpos = 0;
17758 struct glyph_row *last_text_row, *last_text_row_at_end;
17759 struct text_pos start;
17760 ptrdiff_t first_changed_charpos, last_changed_charpos;
17762 #ifdef GLYPH_DEBUG
17763 if (inhibit_try_window_id)
17764 return 0;
17765 #endif
17767 /* This is handy for debugging. */
17768 #if 0
17769 #define GIVE_UP(X) \
17770 do { \
17771 fprintf (stderr, "try_window_id give up %d\n", (X)); \
17772 return 0; \
17773 } while (0)
17774 #else
17775 #define GIVE_UP(X) return 0
17776 #endif
17778 SET_TEXT_POS_FROM_MARKER (start, w->start);
17780 /* Don't use this for mini-windows because these can show
17781 messages and mini-buffers, and we don't handle that here. */
17782 if (MINI_WINDOW_P (w))
17783 GIVE_UP (1);
17785 /* This flag is used to prevent redisplay optimizations. */
17786 if (windows_or_buffers_changed || f->cursor_type_changed)
17787 GIVE_UP (2);
17789 /* This function's optimizations cannot be used if overlays have
17790 changed in the buffer displayed by the window, so give up if they
17791 have. */
17792 if (w->last_overlay_modified != OVERLAY_MODIFF)
17793 GIVE_UP (21);
17795 /* Verify that narrowing has not changed.
17796 Also verify that we were not told to prevent redisplay optimizations.
17797 It would be nice to further
17798 reduce the number of cases where this prevents try_window_id. */
17799 if (current_buffer->clip_changed
17800 || current_buffer->prevent_redisplay_optimizations_p)
17801 GIVE_UP (3);
17803 /* Window must either use window-based redisplay or be full width. */
17804 if (!FRAME_WINDOW_P (f)
17805 && (!FRAME_LINE_INS_DEL_OK (f)
17806 || !WINDOW_FULL_WIDTH_P (w)))
17807 GIVE_UP (4);
17809 /* Give up if point is known NOT to appear in W. */
17810 if (PT < CHARPOS (start))
17811 GIVE_UP (5);
17813 /* Another way to prevent redisplay optimizations. */
17814 if (w->last_modified == 0)
17815 GIVE_UP (6);
17817 /* Verify that window is not hscrolled. */
17818 if (w->hscroll != 0)
17819 GIVE_UP (7);
17821 /* Verify that display wasn't paused. */
17822 if (!w->window_end_valid)
17823 GIVE_UP (8);
17825 /* Likewise if highlighting trailing whitespace. */
17826 if (!NILP (Vshow_trailing_whitespace))
17827 GIVE_UP (11);
17829 /* Can't use this if overlay arrow position and/or string have
17830 changed. */
17831 if (overlay_arrows_changed_p ())
17832 GIVE_UP (12);
17834 /* When word-wrap is on, adding a space to the first word of a
17835 wrapped line can change the wrap position, altering the line
17836 above it. It might be worthwhile to handle this more
17837 intelligently, but for now just redisplay from scratch. */
17838 if (!NILP (BVAR (XBUFFER (w->contents), word_wrap)))
17839 GIVE_UP (21);
17841 /* Under bidi reordering, adding or deleting a character in the
17842 beginning of a paragraph, before the first strong directional
17843 character, can change the base direction of the paragraph (unless
17844 the buffer specifies a fixed paragraph direction), which will
17845 require to redisplay the whole paragraph. It might be worthwhile
17846 to find the paragraph limits and widen the range of redisplayed
17847 lines to that, but for now just give up this optimization and
17848 redisplay from scratch. */
17849 if (!NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering))
17850 && NILP (BVAR (XBUFFER (w->contents), bidi_paragraph_direction)))
17851 GIVE_UP (22);
17853 /* Make sure beg_unchanged and end_unchanged are up to date. Do it
17854 only if buffer has really changed. The reason is that the gap is
17855 initially at Z for freshly visited files. The code below would
17856 set end_unchanged to 0 in that case. */
17857 if (MODIFF > SAVE_MODIFF
17858 /* This seems to happen sometimes after saving a buffer. */
17859 || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
17861 if (GPT - BEG < BEG_UNCHANGED)
17862 BEG_UNCHANGED = GPT - BEG;
17863 if (Z - GPT < END_UNCHANGED)
17864 END_UNCHANGED = Z - GPT;
17867 /* The position of the first and last character that has been changed. */
17868 first_changed_charpos = BEG + BEG_UNCHANGED;
17869 last_changed_charpos = Z - END_UNCHANGED;
17871 /* If window starts after a line end, and the last change is in
17872 front of that newline, then changes don't affect the display.
17873 This case happens with stealth-fontification. Note that although
17874 the display is unchanged, glyph positions in the matrix have to
17875 be adjusted, of course. */
17876 row = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
17877 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
17878 && ((last_changed_charpos < CHARPOS (start)
17879 && CHARPOS (start) == BEGV)
17880 || (last_changed_charpos < CHARPOS (start) - 1
17881 && FETCH_BYTE (BYTEPOS (start) - 1) == '\n')))
17883 ptrdiff_t Z_old, Z_delta, Z_BYTE_old, Z_delta_bytes;
17884 struct glyph_row *r0;
17886 /* Compute how many chars/bytes have been added to or removed
17887 from the buffer. */
17888 Z_old = MATRIX_ROW_END_CHARPOS (row) + w->window_end_pos;
17889 Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
17890 Z_delta = Z - Z_old;
17891 Z_delta_bytes = Z_BYTE - Z_BYTE_old;
17893 /* Give up if PT is not in the window. Note that it already has
17894 been checked at the start of try_window_id that PT is not in
17895 front of the window start. */
17896 if (PT >= MATRIX_ROW_END_CHARPOS (row) + Z_delta)
17897 GIVE_UP (13);
17899 /* If window start is unchanged, we can reuse the whole matrix
17900 as is, after adjusting glyph positions. No need to compute
17901 the window end again, since its offset from Z hasn't changed. */
17902 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
17903 if (CHARPOS (start) == MATRIX_ROW_START_CHARPOS (r0) + Z_delta
17904 && BYTEPOS (start) == MATRIX_ROW_START_BYTEPOS (r0) + Z_delta_bytes
17905 /* PT must not be in a partially visible line. */
17906 && !(PT >= MATRIX_ROW_START_CHARPOS (row) + Z_delta
17907 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
17909 /* Adjust positions in the glyph matrix. */
17910 if (Z_delta || Z_delta_bytes)
17912 struct glyph_row *r1
17913 = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
17914 increment_matrix_positions (w->current_matrix,
17915 MATRIX_ROW_VPOS (r0, current_matrix),
17916 MATRIX_ROW_VPOS (r1, current_matrix),
17917 Z_delta, Z_delta_bytes);
17920 /* Set the cursor. */
17921 row = row_containing_pos (w, PT, r0, NULL, 0);
17922 if (row)
17923 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
17924 return 1;
17928 /* Handle the case that changes are all below what is displayed in
17929 the window, and that PT is in the window. This shortcut cannot
17930 be taken if ZV is visible in the window, and text has been added
17931 there that is visible in the window. */
17932 if (first_changed_charpos >= MATRIX_ROW_END_CHARPOS (row)
17933 /* ZV is not visible in the window, or there are no
17934 changes at ZV, actually. */
17935 && (current_matrix->zv > MATRIX_ROW_END_CHARPOS (row)
17936 || first_changed_charpos == last_changed_charpos))
17938 struct glyph_row *r0;
17940 /* Give up if PT is not in the window. Note that it already has
17941 been checked at the start of try_window_id that PT is not in
17942 front of the window start. */
17943 if (PT >= MATRIX_ROW_END_CHARPOS (row))
17944 GIVE_UP (14);
17946 /* If window start is unchanged, we can reuse the whole matrix
17947 as is, without changing glyph positions since no text has
17948 been added/removed in front of the window end. */
17949 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
17950 if (TEXT_POS_EQUAL_P (start, r0->minpos)
17951 /* PT must not be in a partially visible line. */
17952 && !(PT >= MATRIX_ROW_START_CHARPOS (row)
17953 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
17955 /* We have to compute the window end anew since text
17956 could have been added/removed after it. */
17957 w->window_end_pos = Z - MATRIX_ROW_END_CHARPOS (row);
17958 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
17960 /* Set the cursor. */
17961 row = row_containing_pos (w, PT, r0, NULL, 0);
17962 if (row)
17963 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
17964 return 2;
17968 /* Give up if window start is in the changed area.
17970 The condition used to read
17972 (BEG_UNCHANGED + END_UNCHANGED != Z - BEG && ...)
17974 but why that was tested escapes me at the moment. */
17975 if (CHARPOS (start) >= first_changed_charpos
17976 && CHARPOS (start) <= last_changed_charpos)
17977 GIVE_UP (15);
17979 /* Check that window start agrees with the start of the first glyph
17980 row in its current matrix. Check this after we know the window
17981 start is not in changed text, otherwise positions would not be
17982 comparable. */
17983 row = MATRIX_FIRST_TEXT_ROW (current_matrix);
17984 if (!TEXT_POS_EQUAL_P (start, row->minpos))
17985 GIVE_UP (16);
17987 /* Give up if the window ends in strings. Overlay strings
17988 at the end are difficult to handle, so don't try. */
17989 row = MATRIX_ROW (current_matrix, w->window_end_vpos);
17990 if (MATRIX_ROW_START_CHARPOS (row) == MATRIX_ROW_END_CHARPOS (row))
17991 GIVE_UP (20);
17993 /* Compute the position at which we have to start displaying new
17994 lines. Some of the lines at the top of the window might be
17995 reusable because they are not displaying changed text. Find the
17996 last row in W's current matrix not affected by changes at the
17997 start of current_buffer. Value is null if changes start in the
17998 first line of window. */
17999 last_unchanged_at_beg_row = find_last_unchanged_at_beg_row (w);
18000 if (last_unchanged_at_beg_row)
18002 /* Avoid starting to display in the middle of a character, a TAB
18003 for instance. This is easier than to set up the iterator
18004 exactly, and it's not a frequent case, so the additional
18005 effort wouldn't really pay off. */
18006 while ((MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row)
18007 || last_unchanged_at_beg_row->ends_in_newline_from_string_p)
18008 && last_unchanged_at_beg_row > w->current_matrix->rows)
18009 --last_unchanged_at_beg_row;
18011 if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row))
18012 GIVE_UP (17);
18014 if (init_to_row_end (&it, w, last_unchanged_at_beg_row) == 0)
18015 GIVE_UP (18);
18016 start_pos = it.current.pos;
18018 /* Start displaying new lines in the desired matrix at the same
18019 vpos we would use in the current matrix, i.e. below
18020 last_unchanged_at_beg_row. */
18021 it.vpos = 1 + MATRIX_ROW_VPOS (last_unchanged_at_beg_row,
18022 current_matrix);
18023 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
18024 it.current_y = MATRIX_ROW_BOTTOM_Y (last_unchanged_at_beg_row);
18026 eassert (it.hpos == 0 && it.current_x == 0);
18028 else
18030 /* There are no reusable lines at the start of the window.
18031 Start displaying in the first text line. */
18032 start_display (&it, w, start);
18033 it.vpos = it.first_vpos;
18034 start_pos = it.current.pos;
18037 /* Find the first row that is not affected by changes at the end of
18038 the buffer. Value will be null if there is no unchanged row, in
18039 which case we must redisplay to the end of the window. delta
18040 will be set to the value by which buffer positions beginning with
18041 first_unchanged_at_end_row have to be adjusted due to text
18042 changes. */
18043 first_unchanged_at_end_row
18044 = find_first_unchanged_at_end_row (w, &delta, &delta_bytes);
18045 IF_DEBUG (debug_delta = delta);
18046 IF_DEBUG (debug_delta_bytes = delta_bytes);
18048 /* Set stop_pos to the buffer position up to which we will have to
18049 display new lines. If first_unchanged_at_end_row != NULL, this
18050 is the buffer position of the start of the line displayed in that
18051 row. For first_unchanged_at_end_row == NULL, use 0 to indicate
18052 that we don't stop at a buffer position. */
18053 stop_pos = 0;
18054 if (first_unchanged_at_end_row)
18056 eassert (last_unchanged_at_beg_row == NULL
18057 || first_unchanged_at_end_row >= last_unchanged_at_beg_row);
18059 /* If this is a continuation line, move forward to the next one
18060 that isn't. Changes in lines above affect this line.
18061 Caution: this may move first_unchanged_at_end_row to a row
18062 not displaying text. */
18063 while (MATRIX_ROW_CONTINUATION_LINE_P (first_unchanged_at_end_row)
18064 && MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
18065 && (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
18066 < it.last_visible_y))
18067 ++first_unchanged_at_end_row;
18069 if (!MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
18070 || (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
18071 >= it.last_visible_y))
18072 first_unchanged_at_end_row = NULL;
18073 else
18075 stop_pos = (MATRIX_ROW_START_CHARPOS (first_unchanged_at_end_row)
18076 + delta);
18077 first_unchanged_at_end_vpos
18078 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, current_matrix);
18079 eassert (stop_pos >= Z - END_UNCHANGED);
18082 else if (last_unchanged_at_beg_row == NULL)
18083 GIVE_UP (19);
18086 #ifdef GLYPH_DEBUG
18088 /* Either there is no unchanged row at the end, or the one we have
18089 now displays text. This is a necessary condition for the window
18090 end pos calculation at the end of this function. */
18091 eassert (first_unchanged_at_end_row == NULL
18092 || MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
18094 debug_last_unchanged_at_beg_vpos
18095 = (last_unchanged_at_beg_row
18096 ? MATRIX_ROW_VPOS (last_unchanged_at_beg_row, current_matrix)
18097 : -1);
18098 debug_first_unchanged_at_end_vpos = first_unchanged_at_end_vpos;
18100 #endif /* GLYPH_DEBUG */
18103 /* Display new lines. Set last_text_row to the last new line
18104 displayed which has text on it, i.e. might end up as being the
18105 line where the window_end_vpos is. */
18106 w->cursor.vpos = -1;
18107 last_text_row = NULL;
18108 overlay_arrow_seen = 0;
18109 if (it.current_y < it.last_visible_y
18110 && !f->fonts_changed
18111 && (first_unchanged_at_end_row == NULL
18112 || IT_CHARPOS (it) < stop_pos))
18113 it.glyph_row->reversed_p = false;
18114 while (it.current_y < it.last_visible_y
18115 && !f->fonts_changed
18116 && (first_unchanged_at_end_row == NULL
18117 || IT_CHARPOS (it) < stop_pos))
18119 if (display_line (&it))
18120 last_text_row = it.glyph_row - 1;
18123 if (f->fonts_changed)
18124 return -1;
18127 /* Compute differences in buffer positions, y-positions etc. for
18128 lines reused at the bottom of the window. Compute what we can
18129 scroll. */
18130 if (first_unchanged_at_end_row
18131 /* No lines reused because we displayed everything up to the
18132 bottom of the window. */
18133 && it.current_y < it.last_visible_y)
18135 dvpos = (it.vpos
18136 - MATRIX_ROW_VPOS (first_unchanged_at_end_row,
18137 current_matrix));
18138 dy = it.current_y - first_unchanged_at_end_row->y;
18139 run.current_y = first_unchanged_at_end_row->y;
18140 run.desired_y = run.current_y + dy;
18141 run.height = it.last_visible_y - max (run.current_y, run.desired_y);
18143 else
18145 delta = delta_bytes = dvpos = dy
18146 = run.current_y = run.desired_y = run.height = 0;
18147 first_unchanged_at_end_row = NULL;
18149 IF_DEBUG ((debug_dvpos = dvpos, debug_dy = dy));
18152 /* Find the cursor if not already found. We have to decide whether
18153 PT will appear on this window (it sometimes doesn't, but this is
18154 not a very frequent case.) This decision has to be made before
18155 the current matrix is altered. A value of cursor.vpos < 0 means
18156 that PT is either in one of the lines beginning at
18157 first_unchanged_at_end_row or below the window. Don't care for
18158 lines that might be displayed later at the window end; as
18159 mentioned, this is not a frequent case. */
18160 if (w->cursor.vpos < 0)
18162 /* Cursor in unchanged rows at the top? */
18163 if (PT < CHARPOS (start_pos)
18164 && last_unchanged_at_beg_row)
18166 row = row_containing_pos (w, PT,
18167 MATRIX_FIRST_TEXT_ROW (w->current_matrix),
18168 last_unchanged_at_beg_row + 1, 0);
18169 if (row)
18170 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
18173 /* Start from first_unchanged_at_end_row looking for PT. */
18174 else if (first_unchanged_at_end_row)
18176 row = row_containing_pos (w, PT - delta,
18177 first_unchanged_at_end_row, NULL, 0);
18178 if (row)
18179 set_cursor_from_row (w, row, w->current_matrix, delta,
18180 delta_bytes, dy, dvpos);
18183 /* Give up if cursor was not found. */
18184 if (w->cursor.vpos < 0)
18186 clear_glyph_matrix (w->desired_matrix);
18187 return -1;
18191 /* Don't let the cursor end in the scroll margins. */
18193 int this_scroll_margin, cursor_height;
18194 int frame_line_height = default_line_pixel_height (w);
18195 int window_total_lines
18196 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (it.f) / frame_line_height;
18198 this_scroll_margin =
18199 max (0, min (scroll_margin, window_total_lines / 4));
18200 this_scroll_margin *= frame_line_height;
18201 cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
18203 if ((w->cursor.y < this_scroll_margin
18204 && CHARPOS (start) > BEGV)
18205 /* Old redisplay didn't take scroll margin into account at the bottom,
18206 but then global-hl-line-mode doesn't scroll. KFS 2004-06-14 */
18207 || (w->cursor.y + (make_cursor_line_fully_visible_p
18208 ? cursor_height + this_scroll_margin
18209 : 1)) > it.last_visible_y)
18211 w->cursor.vpos = -1;
18212 clear_glyph_matrix (w->desired_matrix);
18213 return -1;
18217 /* Scroll the display. Do it before changing the current matrix so
18218 that xterm.c doesn't get confused about where the cursor glyph is
18219 found. */
18220 if (dy && run.height)
18222 update_begin (f);
18224 if (FRAME_WINDOW_P (f))
18226 FRAME_RIF (f)->update_window_begin_hook (w);
18227 FRAME_RIF (f)->clear_window_mouse_face (w);
18228 FRAME_RIF (f)->scroll_run_hook (w, &run);
18229 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
18231 else
18233 /* Terminal frame. In this case, dvpos gives the number of
18234 lines to scroll by; dvpos < 0 means scroll up. */
18235 int from_vpos
18236 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, w->current_matrix);
18237 int from = WINDOW_TOP_EDGE_LINE (w) + from_vpos;
18238 int end = (WINDOW_TOP_EDGE_LINE (w)
18239 + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0)
18240 + window_internal_height (w));
18242 #if defined (HAVE_GPM) || defined (MSDOS)
18243 x_clear_window_mouse_face (w);
18244 #endif
18245 /* Perform the operation on the screen. */
18246 if (dvpos > 0)
18248 /* Scroll last_unchanged_at_beg_row to the end of the
18249 window down dvpos lines. */
18250 set_terminal_window (f, end);
18252 /* On dumb terminals delete dvpos lines at the end
18253 before inserting dvpos empty lines. */
18254 if (!FRAME_SCROLL_REGION_OK (f))
18255 ins_del_lines (f, end - dvpos, -dvpos);
18257 /* Insert dvpos empty lines in front of
18258 last_unchanged_at_beg_row. */
18259 ins_del_lines (f, from, dvpos);
18261 else if (dvpos < 0)
18263 /* Scroll up last_unchanged_at_beg_vpos to the end of
18264 the window to last_unchanged_at_beg_vpos - |dvpos|. */
18265 set_terminal_window (f, end);
18267 /* Delete dvpos lines in front of
18268 last_unchanged_at_beg_vpos. ins_del_lines will set
18269 the cursor to the given vpos and emit |dvpos| delete
18270 line sequences. */
18271 ins_del_lines (f, from + dvpos, dvpos);
18273 /* On a dumb terminal insert dvpos empty lines at the
18274 end. */
18275 if (!FRAME_SCROLL_REGION_OK (f))
18276 ins_del_lines (f, end + dvpos, -dvpos);
18279 set_terminal_window (f, 0);
18282 update_end (f);
18285 /* Shift reused rows of the current matrix to the right position.
18286 BOTTOM_ROW is the last + 1 row in the current matrix reserved for
18287 text. */
18288 bottom_row = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
18289 bottom_vpos = MATRIX_ROW_VPOS (bottom_row, current_matrix);
18290 if (dvpos < 0)
18292 rotate_matrix (current_matrix, first_unchanged_at_end_vpos + dvpos,
18293 bottom_vpos, dvpos);
18294 clear_glyph_matrix_rows (current_matrix, bottom_vpos + dvpos,
18295 bottom_vpos);
18297 else if (dvpos > 0)
18299 rotate_matrix (current_matrix, first_unchanged_at_end_vpos,
18300 bottom_vpos, dvpos);
18301 clear_glyph_matrix_rows (current_matrix, first_unchanged_at_end_vpos,
18302 first_unchanged_at_end_vpos + dvpos);
18305 /* For frame-based redisplay, make sure that current frame and window
18306 matrix are in sync with respect to glyph memory. */
18307 if (!FRAME_WINDOW_P (f))
18308 sync_frame_with_window_matrix_rows (w);
18310 /* Adjust buffer positions in reused rows. */
18311 if (delta || delta_bytes)
18312 increment_matrix_positions (current_matrix,
18313 first_unchanged_at_end_vpos + dvpos,
18314 bottom_vpos, delta, delta_bytes);
18316 /* Adjust Y positions. */
18317 if (dy)
18318 shift_glyph_matrix (w, current_matrix,
18319 first_unchanged_at_end_vpos + dvpos,
18320 bottom_vpos, dy);
18322 if (first_unchanged_at_end_row)
18324 first_unchanged_at_end_row += dvpos;
18325 if (first_unchanged_at_end_row->y >= it.last_visible_y
18326 || !MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row))
18327 first_unchanged_at_end_row = NULL;
18330 /* If scrolling up, there may be some lines to display at the end of
18331 the window. */
18332 last_text_row_at_end = NULL;
18333 if (dy < 0)
18335 /* Scrolling up can leave for example a partially visible line
18336 at the end of the window to be redisplayed. */
18337 /* Set last_row to the glyph row in the current matrix where the
18338 window end line is found. It has been moved up or down in
18339 the matrix by dvpos. */
18340 int last_vpos = w->window_end_vpos + dvpos;
18341 struct glyph_row *last_row = MATRIX_ROW (current_matrix, last_vpos);
18343 /* If last_row is the window end line, it should display text. */
18344 eassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_row));
18346 /* If window end line was partially visible before, begin
18347 displaying at that line. Otherwise begin displaying with the
18348 line following it. */
18349 if (MATRIX_ROW_BOTTOM_Y (last_row) - dy >= it.last_visible_y)
18351 init_to_row_start (&it, w, last_row);
18352 it.vpos = last_vpos;
18353 it.current_y = last_row->y;
18355 else
18357 init_to_row_end (&it, w, last_row);
18358 it.vpos = 1 + last_vpos;
18359 it.current_y = MATRIX_ROW_BOTTOM_Y (last_row);
18360 ++last_row;
18363 /* We may start in a continuation line. If so, we have to
18364 get the right continuation_lines_width and current_x. */
18365 it.continuation_lines_width = last_row->continuation_lines_width;
18366 it.hpos = it.current_x = 0;
18368 /* Display the rest of the lines at the window end. */
18369 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
18370 while (it.current_y < it.last_visible_y && !f->fonts_changed)
18372 /* Is it always sure that the display agrees with lines in
18373 the current matrix? I don't think so, so we mark rows
18374 displayed invalid in the current matrix by setting their
18375 enabled_p flag to zero. */
18376 SET_MATRIX_ROW_ENABLED_P (w->current_matrix, it.vpos, false);
18377 if (display_line (&it))
18378 last_text_row_at_end = it.glyph_row - 1;
18382 /* Update window_end_pos and window_end_vpos. */
18383 if (first_unchanged_at_end_row && !last_text_row_at_end)
18385 /* Window end line if one of the preserved rows from the current
18386 matrix. Set row to the last row displaying text in current
18387 matrix starting at first_unchanged_at_end_row, after
18388 scrolling. */
18389 eassert (MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
18390 row = find_last_row_displaying_text (w->current_matrix, &it,
18391 first_unchanged_at_end_row);
18392 eassert (row && MATRIX_ROW_DISPLAYS_TEXT_P (row));
18393 adjust_window_ends (w, row, 1);
18394 eassert (w->window_end_bytepos >= 0);
18395 IF_DEBUG (debug_method_add (w, "A"));
18397 else if (last_text_row_at_end)
18399 adjust_window_ends (w, last_text_row_at_end, 0);
18400 eassert (w->window_end_bytepos >= 0);
18401 IF_DEBUG (debug_method_add (w, "B"));
18403 else if (last_text_row)
18405 /* We have displayed either to the end of the window or at the
18406 end of the window, i.e. the last row with text is to be found
18407 in the desired matrix. */
18408 adjust_window_ends (w, last_text_row, 0);
18409 eassert (w->window_end_bytepos >= 0);
18411 else if (first_unchanged_at_end_row == NULL
18412 && last_text_row == NULL
18413 && last_text_row_at_end == NULL)
18415 /* Displayed to end of window, but no line containing text was
18416 displayed. Lines were deleted at the end of the window. */
18417 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
18418 int vpos = w->window_end_vpos;
18419 struct glyph_row *current_row = current_matrix->rows + vpos;
18420 struct glyph_row *desired_row = desired_matrix->rows + vpos;
18422 for (row = NULL;
18423 row == NULL && vpos >= first_vpos;
18424 --vpos, --current_row, --desired_row)
18426 if (desired_row->enabled_p)
18428 if (MATRIX_ROW_DISPLAYS_TEXT_P (desired_row))
18429 row = desired_row;
18431 else if (MATRIX_ROW_DISPLAYS_TEXT_P (current_row))
18432 row = current_row;
18435 eassert (row != NULL);
18436 w->window_end_vpos = vpos + 1;
18437 w->window_end_pos = Z - MATRIX_ROW_END_CHARPOS (row);
18438 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
18439 eassert (w->window_end_bytepos >= 0);
18440 IF_DEBUG (debug_method_add (w, "C"));
18442 else
18443 emacs_abort ();
18445 IF_DEBUG ((debug_end_pos = w->window_end_pos,
18446 debug_end_vpos = w->window_end_vpos));
18448 /* Record that display has not been completed. */
18449 w->window_end_valid = 0;
18450 w->desired_matrix->no_scrolling_p = 1;
18451 return 3;
18453 #undef GIVE_UP
18458 /***********************************************************************
18459 More debugging support
18460 ***********************************************************************/
18462 #ifdef GLYPH_DEBUG
18464 void dump_glyph_row (struct glyph_row *, int, int) EXTERNALLY_VISIBLE;
18465 void dump_glyph_matrix (struct glyph_matrix *, int) EXTERNALLY_VISIBLE;
18466 void dump_glyph (struct glyph_row *, struct glyph *, int) EXTERNALLY_VISIBLE;
18469 /* Dump the contents of glyph matrix MATRIX on stderr.
18471 GLYPHS 0 means don't show glyph contents.
18472 GLYPHS 1 means show glyphs in short form
18473 GLYPHS > 1 means show glyphs in long form. */
18475 void
18476 dump_glyph_matrix (struct glyph_matrix *matrix, int glyphs)
18478 int i;
18479 for (i = 0; i < matrix->nrows; ++i)
18480 dump_glyph_row (MATRIX_ROW (matrix, i), i, glyphs);
18484 /* Dump contents of glyph GLYPH to stderr. ROW and AREA are
18485 the glyph row and area where the glyph comes from. */
18487 void
18488 dump_glyph (struct glyph_row *row, struct glyph *glyph, int area)
18490 if (glyph->type == CHAR_GLYPH
18491 || glyph->type == GLYPHLESS_GLYPH)
18493 fprintf (stderr,
18494 " %5"pD"d %c %9"pI"d %c %3d 0x%06x %c %4d %1.1d%1.1d\n",
18495 glyph - row->glyphs[TEXT_AREA],
18496 (glyph->type == CHAR_GLYPH
18497 ? 'C'
18498 : 'G'),
18499 glyph->charpos,
18500 (BUFFERP (glyph->object)
18501 ? 'B'
18502 : (STRINGP (glyph->object)
18503 ? 'S'
18504 : (NILP (glyph->object)
18505 ? '0'
18506 : '-'))),
18507 glyph->pixel_width,
18508 glyph->u.ch,
18509 (glyph->u.ch < 0x80 && glyph->u.ch >= ' '
18510 ? glyph->u.ch
18511 : '.'),
18512 glyph->face_id,
18513 glyph->left_box_line_p,
18514 glyph->right_box_line_p);
18516 else if (glyph->type == STRETCH_GLYPH)
18518 fprintf (stderr,
18519 " %5"pD"d %c %9"pI"d %c %3d 0x%06x %c %4d %1.1d%1.1d\n",
18520 glyph - row->glyphs[TEXT_AREA],
18521 'S',
18522 glyph->charpos,
18523 (BUFFERP (glyph->object)
18524 ? 'B'
18525 : (STRINGP (glyph->object)
18526 ? 'S'
18527 : (NILP (glyph->object)
18528 ? '0'
18529 : '-'))),
18530 glyph->pixel_width,
18532 ' ',
18533 glyph->face_id,
18534 glyph->left_box_line_p,
18535 glyph->right_box_line_p);
18537 else if (glyph->type == IMAGE_GLYPH)
18539 fprintf (stderr,
18540 " %5"pD"d %c %9"pI"d %c %3d 0x%06x %c %4d %1.1d%1.1d\n",
18541 glyph - row->glyphs[TEXT_AREA],
18542 'I',
18543 glyph->charpos,
18544 (BUFFERP (glyph->object)
18545 ? 'B'
18546 : (STRINGP (glyph->object)
18547 ? 'S'
18548 : (NILP (glyph->object)
18549 ? '0'
18550 : '-'))),
18551 glyph->pixel_width,
18552 glyph->u.img_id,
18553 '.',
18554 glyph->face_id,
18555 glyph->left_box_line_p,
18556 glyph->right_box_line_p);
18558 else if (glyph->type == COMPOSITE_GLYPH)
18560 fprintf (stderr,
18561 " %5"pD"d %c %9"pI"d %c %3d 0x%06x",
18562 glyph - row->glyphs[TEXT_AREA],
18563 '+',
18564 glyph->charpos,
18565 (BUFFERP (glyph->object)
18566 ? 'B'
18567 : (STRINGP (glyph->object)
18568 ? 'S'
18569 : (NILP (glyph->object)
18570 ? '0'
18571 : '-'))),
18572 glyph->pixel_width,
18573 glyph->u.cmp.id);
18574 if (glyph->u.cmp.automatic)
18575 fprintf (stderr,
18576 "[%d-%d]",
18577 glyph->slice.cmp.from, glyph->slice.cmp.to);
18578 fprintf (stderr, " . %4d %1.1d%1.1d\n",
18579 glyph->face_id,
18580 glyph->left_box_line_p,
18581 glyph->right_box_line_p);
18586 /* Dump the contents of glyph row at VPOS in MATRIX to stderr.
18587 GLYPHS 0 means don't show glyph contents.
18588 GLYPHS 1 means show glyphs in short form
18589 GLYPHS > 1 means show glyphs in long form. */
18591 void
18592 dump_glyph_row (struct glyph_row *row, int vpos, int glyphs)
18594 if (glyphs != 1)
18596 fprintf (stderr, "Row Start End Used oE><\\CTZFesm X Y W H V A P\n");
18597 fprintf (stderr, "==============================================================================\n");
18599 fprintf (stderr, "%3d %9"pI"d %9"pI"d %4d %1.1d%1.1d%1.1d%1.1d\
18600 %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d %4d %4d %4d %4d %4d %4d %4d\n",
18601 vpos,
18602 MATRIX_ROW_START_CHARPOS (row),
18603 MATRIX_ROW_END_CHARPOS (row),
18604 row->used[TEXT_AREA],
18605 row->contains_overlapping_glyphs_p,
18606 row->enabled_p,
18607 row->truncated_on_left_p,
18608 row->truncated_on_right_p,
18609 row->continued_p,
18610 MATRIX_ROW_CONTINUATION_LINE_P (row),
18611 MATRIX_ROW_DISPLAYS_TEXT_P (row),
18612 row->ends_at_zv_p,
18613 row->fill_line_p,
18614 row->ends_in_middle_of_char_p,
18615 row->starts_in_middle_of_char_p,
18616 row->mouse_face_p,
18617 row->x,
18618 row->y,
18619 row->pixel_width,
18620 row->height,
18621 row->visible_height,
18622 row->ascent,
18623 row->phys_ascent);
18624 /* The next 3 lines should align to "Start" in the header. */
18625 fprintf (stderr, " %9"pD"d %9"pD"d\t%5d\n", row->start.overlay_string_index,
18626 row->end.overlay_string_index,
18627 row->continuation_lines_width);
18628 fprintf (stderr, " %9"pI"d %9"pI"d\n",
18629 CHARPOS (row->start.string_pos),
18630 CHARPOS (row->end.string_pos));
18631 fprintf (stderr, " %9d %9d\n", row->start.dpvec_index,
18632 row->end.dpvec_index);
18635 if (glyphs > 1)
18637 int area;
18639 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
18641 struct glyph *glyph = row->glyphs[area];
18642 struct glyph *glyph_end = glyph + row->used[area];
18644 /* Glyph for a line end in text. */
18645 if (area == TEXT_AREA && glyph == glyph_end && glyph->charpos > 0)
18646 ++glyph_end;
18648 if (glyph < glyph_end)
18649 fprintf (stderr, " Glyph# Type Pos O W Code C Face LR\n");
18651 for (; glyph < glyph_end; ++glyph)
18652 dump_glyph (row, glyph, area);
18655 else if (glyphs == 1)
18657 int area;
18658 char s[SHRT_MAX + 4];
18660 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
18662 int i;
18664 for (i = 0; i < row->used[area]; ++i)
18666 struct glyph *glyph = row->glyphs[area] + i;
18667 if (i == row->used[area] - 1
18668 && area == TEXT_AREA
18669 && NILP (glyph->object)
18670 && glyph->type == CHAR_GLYPH
18671 && glyph->u.ch == ' ')
18673 strcpy (&s[i], "[\\n]");
18674 i += 4;
18676 else if (glyph->type == CHAR_GLYPH
18677 && glyph->u.ch < 0x80
18678 && glyph->u.ch >= ' ')
18679 s[i] = glyph->u.ch;
18680 else
18681 s[i] = '.';
18684 s[i] = '\0';
18685 fprintf (stderr, "%3d: (%d) '%s'\n", vpos, row->enabled_p, s);
18691 DEFUN ("dump-glyph-matrix", Fdump_glyph_matrix,
18692 Sdump_glyph_matrix, 0, 1, "p",
18693 doc: /* Dump the current matrix of the selected window to stderr.
18694 Shows contents of glyph row structures. With non-nil
18695 parameter GLYPHS, dump glyphs as well. If GLYPHS is 1 show
18696 glyphs in short form, otherwise show glyphs in long form.
18698 Interactively, no argument means show glyphs in short form;
18699 with numeric argument, its value is passed as the GLYPHS flag. */)
18700 (Lisp_Object glyphs)
18702 struct window *w = XWINDOW (selected_window);
18703 struct buffer *buffer = XBUFFER (w->contents);
18705 fprintf (stderr, "PT = %"pI"d, BEGV = %"pI"d. ZV = %"pI"d\n",
18706 BUF_PT (buffer), BUF_BEGV (buffer), BUF_ZV (buffer));
18707 fprintf (stderr, "Cursor x = %d, y = %d, hpos = %d, vpos = %d\n",
18708 w->cursor.x, w->cursor.y, w->cursor.hpos, w->cursor.vpos);
18709 fprintf (stderr, "=============================================\n");
18710 dump_glyph_matrix (w->current_matrix,
18711 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 0);
18712 return Qnil;
18716 DEFUN ("dump-frame-glyph-matrix", Fdump_frame_glyph_matrix,
18717 Sdump_frame_glyph_matrix, 0, 0, "", doc: /* Dump the current glyph matrix of the selected frame to stderr.
18718 Only text-mode frames have frame glyph matrices. */)
18719 (void)
18721 struct frame *f = XFRAME (selected_frame);
18723 if (f->current_matrix)
18724 dump_glyph_matrix (f->current_matrix, 1);
18725 else
18726 fprintf (stderr, "*** This frame doesn't have a frame glyph matrix ***\n");
18727 return Qnil;
18731 DEFUN ("dump-glyph-row", Fdump_glyph_row, Sdump_glyph_row, 1, 2, "",
18732 doc: /* Dump glyph row ROW to stderr.
18733 GLYPH 0 means don't dump glyphs.
18734 GLYPH 1 means dump glyphs in short form.
18735 GLYPH > 1 or omitted means dump glyphs in long form. */)
18736 (Lisp_Object row, Lisp_Object glyphs)
18738 struct glyph_matrix *matrix;
18739 EMACS_INT vpos;
18741 CHECK_NUMBER (row);
18742 matrix = XWINDOW (selected_window)->current_matrix;
18743 vpos = XINT (row);
18744 if (vpos >= 0 && vpos < matrix->nrows)
18745 dump_glyph_row (MATRIX_ROW (matrix, vpos),
18746 vpos,
18747 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 2);
18748 return Qnil;
18752 DEFUN ("dump-tool-bar-row", Fdump_tool_bar_row, Sdump_tool_bar_row, 1, 2, "",
18753 doc: /* Dump glyph row ROW of the tool-bar of the current frame to stderr.
18754 GLYPH 0 means don't dump glyphs.
18755 GLYPH 1 means dump glyphs in short form.
18756 GLYPH > 1 or omitted means dump glyphs in long form.
18758 If there's no tool-bar, or if the tool-bar is not drawn by Emacs,
18759 do nothing. */)
18760 (Lisp_Object row, Lisp_Object glyphs)
18762 #if defined (HAVE_WINDOW_SYSTEM) && ! defined (USE_GTK) && ! defined (HAVE_NS)
18763 struct frame *sf = SELECTED_FRAME ();
18764 struct glyph_matrix *m = XWINDOW (sf->tool_bar_window)->current_matrix;
18765 EMACS_INT vpos;
18767 CHECK_NUMBER (row);
18768 vpos = XINT (row);
18769 if (vpos >= 0 && vpos < m->nrows)
18770 dump_glyph_row (MATRIX_ROW (m, vpos), vpos,
18771 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 2);
18772 #endif
18773 return Qnil;
18777 DEFUN ("trace-redisplay", Ftrace_redisplay, Strace_redisplay, 0, 1, "P",
18778 doc: /* Toggle tracing of redisplay.
18779 With ARG, turn tracing on if and only if ARG is positive. */)
18780 (Lisp_Object arg)
18782 if (NILP (arg))
18783 trace_redisplay_p = !trace_redisplay_p;
18784 else
18786 arg = Fprefix_numeric_value (arg);
18787 trace_redisplay_p = XINT (arg) > 0;
18790 return Qnil;
18794 DEFUN ("trace-to-stderr", Ftrace_to_stderr, Strace_to_stderr, 1, MANY, "",
18795 doc: /* Like `format', but print result to stderr.
18796 usage: (trace-to-stderr STRING &rest OBJECTS) */)
18797 (ptrdiff_t nargs, Lisp_Object *args)
18799 Lisp_Object s = Fformat (nargs, args);
18800 fwrite (SDATA (s), 1, SBYTES (s), stderr);
18801 return Qnil;
18804 #endif /* GLYPH_DEBUG */
18808 /***********************************************************************
18809 Building Desired Matrix Rows
18810 ***********************************************************************/
18812 /* Return a temporary glyph row holding the glyphs of an overlay arrow.
18813 Used for non-window-redisplay windows, and for windows w/o left fringe. */
18815 static struct glyph_row *
18816 get_overlay_arrow_glyph_row (struct window *w, Lisp_Object overlay_arrow_string)
18818 struct frame *f = XFRAME (WINDOW_FRAME (w));
18819 struct buffer *buffer = XBUFFER (w->contents);
18820 struct buffer *old = current_buffer;
18821 const unsigned char *arrow_string = SDATA (overlay_arrow_string);
18822 ptrdiff_t arrow_len = SCHARS (overlay_arrow_string);
18823 const unsigned char *arrow_end = arrow_string + arrow_len;
18824 const unsigned char *p;
18825 struct it it;
18826 bool multibyte_p;
18827 int n_glyphs_before;
18829 set_buffer_temp (buffer);
18830 init_iterator (&it, w, -1, -1, &scratch_glyph_row, DEFAULT_FACE_ID);
18831 scratch_glyph_row.reversed_p = false;
18832 it.glyph_row->used[TEXT_AREA] = 0;
18833 SET_TEXT_POS (it.position, 0, 0);
18835 multibyte_p = !NILP (BVAR (buffer, enable_multibyte_characters));
18836 p = arrow_string;
18837 while (p < arrow_end)
18839 Lisp_Object face, ilisp;
18841 /* Get the next character. */
18842 if (multibyte_p)
18843 it.c = it.char_to_display = string_char_and_length (p, &it.len);
18844 else
18846 it.c = it.char_to_display = *p, it.len = 1;
18847 if (! ASCII_CHAR_P (it.c))
18848 it.char_to_display = BYTE8_TO_CHAR (it.c);
18850 p += it.len;
18852 /* Get its face. */
18853 ilisp = make_number (p - arrow_string);
18854 face = Fget_text_property (ilisp, Qface, overlay_arrow_string);
18855 it.face_id = compute_char_face (f, it.char_to_display, face);
18857 /* Compute its width, get its glyphs. */
18858 n_glyphs_before = it.glyph_row->used[TEXT_AREA];
18859 SET_TEXT_POS (it.position, -1, -1);
18860 PRODUCE_GLYPHS (&it);
18862 /* If this character doesn't fit any more in the line, we have
18863 to remove some glyphs. */
18864 if (it.current_x > it.last_visible_x)
18866 it.glyph_row->used[TEXT_AREA] = n_glyphs_before;
18867 break;
18871 set_buffer_temp (old);
18872 return it.glyph_row;
18876 /* Insert truncation glyphs at the start of IT->glyph_row. Which
18877 glyphs to insert is determined by produce_special_glyphs. */
18879 static void
18880 insert_left_trunc_glyphs (struct it *it)
18882 struct it truncate_it;
18883 struct glyph *from, *end, *to, *toend;
18885 eassert (!FRAME_WINDOW_P (it->f)
18886 || (!it->glyph_row->reversed_p
18887 && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0)
18888 || (it->glyph_row->reversed_p
18889 && WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0));
18891 /* Get the truncation glyphs. */
18892 truncate_it = *it;
18893 truncate_it.current_x = 0;
18894 truncate_it.face_id = DEFAULT_FACE_ID;
18895 truncate_it.glyph_row = &scratch_glyph_row;
18896 truncate_it.area = TEXT_AREA;
18897 truncate_it.glyph_row->used[TEXT_AREA] = 0;
18898 CHARPOS (truncate_it.position) = BYTEPOS (truncate_it.position) = -1;
18899 truncate_it.object = Qnil;
18900 produce_special_glyphs (&truncate_it, IT_TRUNCATION);
18902 /* Overwrite glyphs from IT with truncation glyphs. */
18903 if (!it->glyph_row->reversed_p)
18905 short tused = truncate_it.glyph_row->used[TEXT_AREA];
18907 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
18908 end = from + tused;
18909 to = it->glyph_row->glyphs[TEXT_AREA];
18910 toend = to + it->glyph_row->used[TEXT_AREA];
18911 if (FRAME_WINDOW_P (it->f))
18913 /* On GUI frames, when variable-size fonts are displayed,
18914 the truncation glyphs may need more pixels than the row's
18915 glyphs they overwrite. We overwrite more glyphs to free
18916 enough screen real estate, and enlarge the stretch glyph
18917 on the right (see display_line), if there is one, to
18918 preserve the screen position of the truncation glyphs on
18919 the right. */
18920 int w = 0;
18921 struct glyph *g = to;
18922 short used;
18924 /* The first glyph could be partially visible, in which case
18925 it->glyph_row->x will be negative. But we want the left
18926 truncation glyphs to be aligned at the left margin of the
18927 window, so we override the x coordinate at which the row
18928 will begin. */
18929 it->glyph_row->x = 0;
18930 while (g < toend && w < it->truncation_pixel_width)
18932 w += g->pixel_width;
18933 ++g;
18935 if (g - to - tused > 0)
18937 memmove (to + tused, g, (toend - g) * sizeof(*g));
18938 it->glyph_row->used[TEXT_AREA] -= g - to - tused;
18940 used = it->glyph_row->used[TEXT_AREA];
18941 if (it->glyph_row->truncated_on_right_p
18942 && WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0
18943 && it->glyph_row->glyphs[TEXT_AREA][used - 2].type
18944 == STRETCH_GLYPH)
18946 int extra = w - it->truncation_pixel_width;
18948 it->glyph_row->glyphs[TEXT_AREA][used - 2].pixel_width += extra;
18952 while (from < end)
18953 *to++ = *from++;
18955 /* There may be padding glyphs left over. Overwrite them too. */
18956 if (!FRAME_WINDOW_P (it->f))
18958 while (to < toend && CHAR_GLYPH_PADDING_P (*to))
18960 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
18961 while (from < end)
18962 *to++ = *from++;
18966 if (to > toend)
18967 it->glyph_row->used[TEXT_AREA] = to - it->glyph_row->glyphs[TEXT_AREA];
18969 else
18971 short tused = truncate_it.glyph_row->used[TEXT_AREA];
18973 /* In R2L rows, overwrite the last (rightmost) glyphs, and do
18974 that back to front. */
18975 end = truncate_it.glyph_row->glyphs[TEXT_AREA];
18976 from = end + truncate_it.glyph_row->used[TEXT_AREA] - 1;
18977 toend = it->glyph_row->glyphs[TEXT_AREA];
18978 to = toend + it->glyph_row->used[TEXT_AREA] - 1;
18979 if (FRAME_WINDOW_P (it->f))
18981 int w = 0;
18982 struct glyph *g = to;
18984 while (g >= toend && w < it->truncation_pixel_width)
18986 w += g->pixel_width;
18987 --g;
18989 if (to - g - tused > 0)
18990 to = g + tused;
18991 if (it->glyph_row->truncated_on_right_p
18992 && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0
18993 && it->glyph_row->glyphs[TEXT_AREA][1].type == STRETCH_GLYPH)
18995 int extra = w - it->truncation_pixel_width;
18997 it->glyph_row->glyphs[TEXT_AREA][1].pixel_width += extra;
19001 while (from >= end && to >= toend)
19002 *to-- = *from--;
19003 if (!FRAME_WINDOW_P (it->f))
19005 while (to >= toend && CHAR_GLYPH_PADDING_P (*to))
19007 from =
19008 truncate_it.glyph_row->glyphs[TEXT_AREA]
19009 + truncate_it.glyph_row->used[TEXT_AREA] - 1;
19010 while (from >= end && to >= toend)
19011 *to-- = *from--;
19014 if (from >= end)
19016 /* Need to free some room before prepending additional
19017 glyphs. */
19018 int move_by = from - end + 1;
19019 struct glyph *g0 = it->glyph_row->glyphs[TEXT_AREA];
19020 struct glyph *g = g0 + it->glyph_row->used[TEXT_AREA] - 1;
19022 for ( ; g >= g0; g--)
19023 g[move_by] = *g;
19024 while (from >= end)
19025 *to-- = *from--;
19026 it->glyph_row->used[TEXT_AREA] += move_by;
19031 /* Compute the hash code for ROW. */
19032 unsigned
19033 row_hash (struct glyph_row *row)
19035 int area, k;
19036 unsigned hashval = 0;
19038 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
19039 for (k = 0; k < row->used[area]; ++k)
19040 hashval = ((((hashval << 4) + (hashval >> 24)) & 0x0fffffff)
19041 + row->glyphs[area][k].u.val
19042 + row->glyphs[area][k].face_id
19043 + row->glyphs[area][k].padding_p
19044 + (row->glyphs[area][k].type << 2));
19046 return hashval;
19049 /* Compute the pixel height and width of IT->glyph_row.
19051 Most of the time, ascent and height of a display line will be equal
19052 to the max_ascent and max_height values of the display iterator
19053 structure. This is not the case if
19055 1. We hit ZV without displaying anything. In this case, max_ascent
19056 and max_height will be zero.
19058 2. We have some glyphs that don't contribute to the line height.
19059 (The glyph row flag contributes_to_line_height_p is for future
19060 pixmap extensions).
19062 The first case is easily covered by using default values because in
19063 these cases, the line height does not really matter, except that it
19064 must not be zero. */
19066 static void
19067 compute_line_metrics (struct it *it)
19069 struct glyph_row *row = it->glyph_row;
19071 if (FRAME_WINDOW_P (it->f))
19073 int i, min_y, max_y;
19075 /* The line may consist of one space only, that was added to
19076 place the cursor on it. If so, the row's height hasn't been
19077 computed yet. */
19078 if (row->height == 0)
19080 if (it->max_ascent + it->max_descent == 0)
19081 it->max_descent = it->max_phys_descent = FRAME_LINE_HEIGHT (it->f);
19082 row->ascent = it->max_ascent;
19083 row->height = it->max_ascent + it->max_descent;
19084 row->phys_ascent = it->max_phys_ascent;
19085 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
19086 row->extra_line_spacing = it->max_extra_line_spacing;
19089 /* Compute the width of this line. */
19090 row->pixel_width = row->x;
19091 for (i = 0; i < row->used[TEXT_AREA]; ++i)
19092 row->pixel_width += row->glyphs[TEXT_AREA][i].pixel_width;
19094 eassert (row->pixel_width >= 0);
19095 eassert (row->ascent >= 0 && row->height > 0);
19097 row->overlapping_p = (MATRIX_ROW_OVERLAPS_SUCC_P (row)
19098 || MATRIX_ROW_OVERLAPS_PRED_P (row));
19100 /* If first line's physical ascent is larger than its logical
19101 ascent, use the physical ascent, and make the row taller.
19102 This makes accented characters fully visible. */
19103 if (row == MATRIX_FIRST_TEXT_ROW (it->w->desired_matrix)
19104 && row->phys_ascent > row->ascent)
19106 row->height += row->phys_ascent - row->ascent;
19107 row->ascent = row->phys_ascent;
19110 /* Compute how much of the line is visible. */
19111 row->visible_height = row->height;
19113 min_y = WINDOW_HEADER_LINE_HEIGHT (it->w);
19114 max_y = WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w);
19116 if (row->y < min_y)
19117 row->visible_height -= min_y - row->y;
19118 if (row->y + row->height > max_y)
19119 row->visible_height -= row->y + row->height - max_y;
19121 else
19123 row->pixel_width = row->used[TEXT_AREA];
19124 if (row->continued_p)
19125 row->pixel_width -= it->continuation_pixel_width;
19126 else if (row->truncated_on_right_p)
19127 row->pixel_width -= it->truncation_pixel_width;
19128 row->ascent = row->phys_ascent = 0;
19129 row->height = row->phys_height = row->visible_height = 1;
19130 row->extra_line_spacing = 0;
19133 /* Compute a hash code for this row. */
19134 row->hash = row_hash (row);
19136 it->max_ascent = it->max_descent = 0;
19137 it->max_phys_ascent = it->max_phys_descent = 0;
19141 /* Append one space to the glyph row of iterator IT if doing a
19142 window-based redisplay. The space has the same face as
19143 IT->face_id. Value is non-zero if a space was added.
19145 This function is called to make sure that there is always one glyph
19146 at the end of a glyph row that the cursor can be set on under
19147 window-systems. (If there weren't such a glyph we would not know
19148 how wide and tall a box cursor should be displayed).
19150 At the same time this space let's a nicely handle clearing to the
19151 end of the line if the row ends in italic text. */
19153 static int
19154 append_space_for_newline (struct it *it, int default_face_p)
19156 if (FRAME_WINDOW_P (it->f))
19158 int n = it->glyph_row->used[TEXT_AREA];
19160 if (it->glyph_row->glyphs[TEXT_AREA] + n
19161 < it->glyph_row->glyphs[1 + TEXT_AREA])
19163 /* Save some values that must not be changed.
19164 Must save IT->c and IT->len because otherwise
19165 ITERATOR_AT_END_P wouldn't work anymore after
19166 append_space_for_newline has been called. */
19167 enum display_element_type saved_what = it->what;
19168 int saved_c = it->c, saved_len = it->len;
19169 int saved_char_to_display = it->char_to_display;
19170 int saved_x = it->current_x;
19171 int saved_face_id = it->face_id;
19172 int saved_box_end = it->end_of_box_run_p;
19173 struct text_pos saved_pos;
19174 Lisp_Object saved_object;
19175 struct face *face;
19177 saved_object = it->object;
19178 saved_pos = it->position;
19180 it->what = IT_CHARACTER;
19181 memset (&it->position, 0, sizeof it->position);
19182 it->object = Qnil;
19183 it->c = it->char_to_display = ' ';
19184 it->len = 1;
19186 /* If the default face was remapped, be sure to use the
19187 remapped face for the appended newline. */
19188 if (default_face_p)
19189 it->face_id = lookup_basic_face (it->f, DEFAULT_FACE_ID);
19190 else if (it->face_before_selective_p)
19191 it->face_id = it->saved_face_id;
19192 face = FACE_FROM_ID (it->f, it->face_id);
19193 it->face_id = FACE_FOR_CHAR (it->f, face, 0, -1, Qnil);
19194 /* In R2L rows, we will prepend a stretch glyph that will
19195 have the end_of_box_run_p flag set for it, so there's no
19196 need for the appended newline glyph to have that flag
19197 set. */
19198 if (it->glyph_row->reversed_p
19199 /* But if the appended newline glyph goes all the way to
19200 the end of the row, there will be no stretch glyph,
19201 so leave the box flag set. */
19202 && saved_x + FRAME_COLUMN_WIDTH (it->f) < it->last_visible_x)
19203 it->end_of_box_run_p = 0;
19205 PRODUCE_GLYPHS (it);
19207 it->override_ascent = -1;
19208 it->constrain_row_ascent_descent_p = 0;
19209 it->current_x = saved_x;
19210 it->object = saved_object;
19211 it->position = saved_pos;
19212 it->what = saved_what;
19213 it->face_id = saved_face_id;
19214 it->len = saved_len;
19215 it->c = saved_c;
19216 it->char_to_display = saved_char_to_display;
19217 it->end_of_box_run_p = saved_box_end;
19218 return 1;
19222 return 0;
19226 /* Extend the face of the last glyph in the text area of IT->glyph_row
19227 to the end of the display line. Called from display_line. If the
19228 glyph row is empty, add a space glyph to it so that we know the
19229 face to draw. Set the glyph row flag fill_line_p. If the glyph
19230 row is R2L, prepend a stretch glyph to cover the empty space to the
19231 left of the leftmost glyph. */
19233 static void
19234 extend_face_to_end_of_line (struct it *it)
19236 struct face *face, *default_face;
19237 struct frame *f = it->f;
19239 /* If line is already filled, do nothing. Non window-system frames
19240 get a grace of one more ``pixel'' because their characters are
19241 1-``pixel'' wide, so they hit the equality too early. This grace
19242 is needed only for R2L rows that are not continued, to produce
19243 one extra blank where we could display the cursor. */
19244 if ((it->current_x >= it->last_visible_x
19245 + (!FRAME_WINDOW_P (f)
19246 && it->glyph_row->reversed_p
19247 && !it->glyph_row->continued_p))
19248 /* If the window has display margins, we will need to extend
19249 their face even if the text area is filled. */
19250 && !(WINDOW_LEFT_MARGIN_WIDTH (it->w) > 0
19251 || WINDOW_RIGHT_MARGIN_WIDTH (it->w) > 0))
19252 return;
19254 /* The default face, possibly remapped. */
19255 default_face = FACE_FROM_ID (f, lookup_basic_face (f, DEFAULT_FACE_ID));
19257 /* Face extension extends the background and box of IT->face_id
19258 to the end of the line. If the background equals the background
19259 of the frame, we don't have to do anything. */
19260 if (it->face_before_selective_p)
19261 face = FACE_FROM_ID (f, it->saved_face_id);
19262 else
19263 face = FACE_FROM_ID (f, it->face_id);
19265 if (FRAME_WINDOW_P (f)
19266 && MATRIX_ROW_DISPLAYS_TEXT_P (it->glyph_row)
19267 && face->box == FACE_NO_BOX
19268 && face->background == FRAME_BACKGROUND_PIXEL (f)
19269 #ifdef HAVE_WINDOW_SYSTEM
19270 && !face->stipple
19271 #endif
19272 && !it->glyph_row->reversed_p)
19273 return;
19275 /* Set the glyph row flag indicating that the face of the last glyph
19276 in the text area has to be drawn to the end of the text area. */
19277 it->glyph_row->fill_line_p = 1;
19279 /* If current character of IT is not ASCII, make sure we have the
19280 ASCII face. This will be automatically undone the next time
19281 get_next_display_element returns a multibyte character. Note
19282 that the character will always be single byte in unibyte
19283 text. */
19284 if (!ASCII_CHAR_P (it->c))
19286 it->face_id = FACE_FOR_CHAR (f, face, 0, -1, Qnil);
19289 if (FRAME_WINDOW_P (f))
19291 /* If the row is empty, add a space with the current face of IT,
19292 so that we know which face to draw. */
19293 if (it->glyph_row->used[TEXT_AREA] == 0)
19295 it->glyph_row->glyphs[TEXT_AREA][0] = space_glyph;
19296 it->glyph_row->glyphs[TEXT_AREA][0].face_id = face->id;
19297 it->glyph_row->used[TEXT_AREA] = 1;
19299 /* Mode line and the header line don't have margins, and
19300 likewise the frame's tool-bar window, if there is any. */
19301 if (!(it->glyph_row->mode_line_p
19302 #if defined (HAVE_WINDOW_SYSTEM) && ! defined (USE_GTK) && ! defined (HAVE_NS)
19303 || (WINDOWP (f->tool_bar_window)
19304 && it->w == XWINDOW (f->tool_bar_window))
19305 #endif
19308 if (WINDOW_LEFT_MARGIN_WIDTH (it->w) > 0
19309 && it->glyph_row->used[LEFT_MARGIN_AREA] == 0)
19311 it->glyph_row->glyphs[LEFT_MARGIN_AREA][0] = space_glyph;
19312 it->glyph_row->glyphs[LEFT_MARGIN_AREA][0].face_id =
19313 default_face->id;
19314 it->glyph_row->used[LEFT_MARGIN_AREA] = 1;
19316 if (WINDOW_RIGHT_MARGIN_WIDTH (it->w) > 0
19317 && it->glyph_row->used[RIGHT_MARGIN_AREA] == 0)
19319 it->glyph_row->glyphs[RIGHT_MARGIN_AREA][0] = space_glyph;
19320 it->glyph_row->glyphs[RIGHT_MARGIN_AREA][0].face_id =
19321 default_face->id;
19322 it->glyph_row->used[RIGHT_MARGIN_AREA] = 1;
19325 #ifdef HAVE_WINDOW_SYSTEM
19326 if (it->glyph_row->reversed_p)
19328 /* Prepend a stretch glyph to the row, such that the
19329 rightmost glyph will be drawn flushed all the way to the
19330 right margin of the window. The stretch glyph that will
19331 occupy the empty space, if any, to the left of the
19332 glyphs. */
19333 struct font *font = face->font ? face->font : FRAME_FONT (f);
19334 struct glyph *row_start = it->glyph_row->glyphs[TEXT_AREA];
19335 struct glyph *row_end = row_start + it->glyph_row->used[TEXT_AREA];
19336 struct glyph *g;
19337 int row_width, stretch_ascent, stretch_width;
19338 struct text_pos saved_pos;
19339 int saved_face_id, saved_avoid_cursor, saved_box_start;
19341 for (row_width = 0, g = row_start; g < row_end; g++)
19342 row_width += g->pixel_width;
19344 /* FIXME: There are various minor display glitches in R2L
19345 rows when only one of the fringes is missing. The
19346 strange condition below produces the least bad effect. */
19347 if ((WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0)
19348 == (WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0)
19349 || WINDOW_RIGHT_FRINGE_WIDTH (it->w) != 0)
19350 stretch_width = window_box_width (it->w, TEXT_AREA);
19351 else
19352 stretch_width = it->last_visible_x - it->first_visible_x;
19353 stretch_width -= row_width;
19355 if (stretch_width > 0)
19357 stretch_ascent =
19358 (((it->ascent + it->descent)
19359 * FONT_BASE (font)) / FONT_HEIGHT (font));
19360 saved_pos = it->position;
19361 memset (&it->position, 0, sizeof it->position);
19362 saved_avoid_cursor = it->avoid_cursor_p;
19363 it->avoid_cursor_p = 1;
19364 saved_face_id = it->face_id;
19365 saved_box_start = it->start_of_box_run_p;
19366 /* The last row's stretch glyph should get the default
19367 face, to avoid painting the rest of the window with
19368 the region face, if the region ends at ZV. */
19369 if (it->glyph_row->ends_at_zv_p)
19370 it->face_id = default_face->id;
19371 else
19372 it->face_id = face->id;
19373 it->start_of_box_run_p = 0;
19374 append_stretch_glyph (it, Qnil, stretch_width,
19375 it->ascent + it->descent, stretch_ascent);
19376 it->position = saved_pos;
19377 it->avoid_cursor_p = saved_avoid_cursor;
19378 it->face_id = saved_face_id;
19379 it->start_of_box_run_p = saved_box_start;
19381 /* If stretch_width comes out negative, it means that the
19382 last glyph is only partially visible. In R2L rows, we
19383 want the leftmost glyph to be partially visible, so we
19384 need to give the row the corresponding left offset. */
19385 if (stretch_width < 0)
19386 it->glyph_row->x = stretch_width;
19388 #endif /* HAVE_WINDOW_SYSTEM */
19390 else
19392 /* Save some values that must not be changed. */
19393 int saved_x = it->current_x;
19394 struct text_pos saved_pos;
19395 Lisp_Object saved_object;
19396 enum display_element_type saved_what = it->what;
19397 int saved_face_id = it->face_id;
19399 saved_object = it->object;
19400 saved_pos = it->position;
19402 it->what = IT_CHARACTER;
19403 memset (&it->position, 0, sizeof it->position);
19404 it->object = Qnil;
19405 it->c = it->char_to_display = ' ';
19406 it->len = 1;
19408 if (WINDOW_LEFT_MARGIN_WIDTH (it->w) > 0
19409 && (it->glyph_row->used[LEFT_MARGIN_AREA]
19410 < WINDOW_LEFT_MARGIN_WIDTH (it->w))
19411 && !it->glyph_row->mode_line_p
19412 && default_face->background != FRAME_BACKGROUND_PIXEL (f))
19414 struct glyph *g = it->glyph_row->glyphs[LEFT_MARGIN_AREA];
19415 struct glyph *e = g + it->glyph_row->used[LEFT_MARGIN_AREA];
19417 for (it->current_x = 0; g < e; g++)
19418 it->current_x += g->pixel_width;
19420 it->area = LEFT_MARGIN_AREA;
19421 it->face_id = default_face->id;
19422 while (it->glyph_row->used[LEFT_MARGIN_AREA]
19423 < WINDOW_LEFT_MARGIN_WIDTH (it->w))
19425 PRODUCE_GLYPHS (it);
19426 /* term.c:produce_glyphs advances it->current_x only for
19427 TEXT_AREA. */
19428 it->current_x += it->pixel_width;
19431 it->current_x = saved_x;
19432 it->area = TEXT_AREA;
19435 /* The last row's blank glyphs should get the default face, to
19436 avoid painting the rest of the window with the region face,
19437 if the region ends at ZV. */
19438 if (it->glyph_row->ends_at_zv_p)
19439 it->face_id = default_face->id;
19440 else
19441 it->face_id = face->id;
19442 PRODUCE_GLYPHS (it);
19444 while (it->current_x <= it->last_visible_x)
19445 PRODUCE_GLYPHS (it);
19447 if (WINDOW_RIGHT_MARGIN_WIDTH (it->w) > 0
19448 && (it->glyph_row->used[RIGHT_MARGIN_AREA]
19449 < WINDOW_RIGHT_MARGIN_WIDTH (it->w))
19450 && !it->glyph_row->mode_line_p
19451 && default_face->background != FRAME_BACKGROUND_PIXEL (f))
19453 struct glyph *g = it->glyph_row->glyphs[RIGHT_MARGIN_AREA];
19454 struct glyph *e = g + it->glyph_row->used[RIGHT_MARGIN_AREA];
19456 for ( ; g < e; g++)
19457 it->current_x += g->pixel_width;
19459 it->area = RIGHT_MARGIN_AREA;
19460 it->face_id = default_face->id;
19461 while (it->glyph_row->used[RIGHT_MARGIN_AREA]
19462 < WINDOW_RIGHT_MARGIN_WIDTH (it->w))
19464 PRODUCE_GLYPHS (it);
19465 it->current_x += it->pixel_width;
19468 it->area = TEXT_AREA;
19471 /* Don't count these blanks really. It would let us insert a left
19472 truncation glyph below and make us set the cursor on them, maybe. */
19473 it->current_x = saved_x;
19474 it->object = saved_object;
19475 it->position = saved_pos;
19476 it->what = saved_what;
19477 it->face_id = saved_face_id;
19482 /* Value is non-zero if text starting at CHARPOS in current_buffer is
19483 trailing whitespace. */
19485 static int
19486 trailing_whitespace_p (ptrdiff_t charpos)
19488 ptrdiff_t bytepos = CHAR_TO_BYTE (charpos);
19489 int c = 0;
19491 while (bytepos < ZV_BYTE
19492 && (c = FETCH_CHAR (bytepos),
19493 c == ' ' || c == '\t'))
19494 ++bytepos;
19496 if (bytepos >= ZV_BYTE || c == '\n' || c == '\r')
19498 if (bytepos != PT_BYTE)
19499 return 1;
19501 return 0;
19505 /* Highlight trailing whitespace, if any, in ROW. */
19507 static void
19508 highlight_trailing_whitespace (struct frame *f, struct glyph_row *row)
19510 int used = row->used[TEXT_AREA];
19512 if (used)
19514 struct glyph *start = row->glyphs[TEXT_AREA];
19515 struct glyph *glyph = start + used - 1;
19517 if (row->reversed_p)
19519 /* Right-to-left rows need to be processed in the opposite
19520 direction, so swap the edge pointers. */
19521 glyph = start;
19522 start = row->glyphs[TEXT_AREA] + used - 1;
19525 /* Skip over glyphs inserted to display the cursor at the
19526 end of a line, for extending the face of the last glyph
19527 to the end of the line on terminals, and for truncation
19528 and continuation glyphs. */
19529 if (!row->reversed_p)
19531 while (glyph >= start
19532 && glyph->type == CHAR_GLYPH
19533 && NILP (glyph->object))
19534 --glyph;
19536 else
19538 while (glyph <= start
19539 && glyph->type == CHAR_GLYPH
19540 && NILP (glyph->object))
19541 ++glyph;
19544 /* If last glyph is a space or stretch, and it's trailing
19545 whitespace, set the face of all trailing whitespace glyphs in
19546 IT->glyph_row to `trailing-whitespace'. */
19547 if ((row->reversed_p ? glyph <= start : glyph >= start)
19548 && BUFFERP (glyph->object)
19549 && (glyph->type == STRETCH_GLYPH
19550 || (glyph->type == CHAR_GLYPH
19551 && glyph->u.ch == ' '))
19552 && trailing_whitespace_p (glyph->charpos))
19554 int face_id = lookup_named_face (f, Qtrailing_whitespace, false);
19555 if (face_id < 0)
19556 return;
19558 if (!row->reversed_p)
19560 while (glyph >= start
19561 && BUFFERP (glyph->object)
19562 && (glyph->type == STRETCH_GLYPH
19563 || (glyph->type == CHAR_GLYPH
19564 && glyph->u.ch == ' ')))
19565 (glyph--)->face_id = face_id;
19567 else
19569 while (glyph <= start
19570 && BUFFERP (glyph->object)
19571 && (glyph->type == STRETCH_GLYPH
19572 || (glyph->type == CHAR_GLYPH
19573 && glyph->u.ch == ' ')))
19574 (glyph++)->face_id = face_id;
19581 /* Value is non-zero if glyph row ROW should be
19582 considered to hold the buffer position CHARPOS. */
19584 static int
19585 row_for_charpos_p (struct glyph_row *row, ptrdiff_t charpos)
19587 int result = 1;
19589 if (charpos == CHARPOS (row->end.pos)
19590 || charpos == MATRIX_ROW_END_CHARPOS (row))
19592 /* Suppose the row ends on a string.
19593 Unless the row is continued, that means it ends on a newline
19594 in the string. If it's anything other than a display string
19595 (e.g., a before-string from an overlay), we don't want the
19596 cursor there. (This heuristic seems to give the optimal
19597 behavior for the various types of multi-line strings.)
19598 One exception: if the string has `cursor' property on one of
19599 its characters, we _do_ want the cursor there. */
19600 if (CHARPOS (row->end.string_pos) >= 0)
19602 if (row->continued_p)
19603 result = 1;
19604 else
19606 /* Check for `display' property. */
19607 struct glyph *beg = row->glyphs[TEXT_AREA];
19608 struct glyph *end = beg + row->used[TEXT_AREA] - 1;
19609 struct glyph *glyph;
19611 result = 0;
19612 for (glyph = end; glyph >= beg; --glyph)
19613 if (STRINGP (glyph->object))
19615 Lisp_Object prop
19616 = Fget_char_property (make_number (charpos),
19617 Qdisplay, Qnil);
19618 result =
19619 (!NILP (prop)
19620 && display_prop_string_p (prop, glyph->object));
19621 /* If there's a `cursor' property on one of the
19622 string's characters, this row is a cursor row,
19623 even though this is not a display string. */
19624 if (!result)
19626 Lisp_Object s = glyph->object;
19628 for ( ; glyph >= beg && EQ (glyph->object, s); --glyph)
19630 ptrdiff_t gpos = glyph->charpos;
19632 if (!NILP (Fget_char_property (make_number (gpos),
19633 Qcursor, s)))
19635 result = 1;
19636 break;
19640 break;
19644 else if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
19646 /* If the row ends in middle of a real character,
19647 and the line is continued, we want the cursor here.
19648 That's because CHARPOS (ROW->end.pos) would equal
19649 PT if PT is before the character. */
19650 if (!row->ends_in_ellipsis_p)
19651 result = row->continued_p;
19652 else
19653 /* If the row ends in an ellipsis, then
19654 CHARPOS (ROW->end.pos) will equal point after the
19655 invisible text. We want that position to be displayed
19656 after the ellipsis. */
19657 result = 0;
19659 /* If the row ends at ZV, display the cursor at the end of that
19660 row instead of at the start of the row below. */
19661 else if (row->ends_at_zv_p)
19662 result = 1;
19663 else
19664 result = 0;
19667 return result;
19670 /* Value is non-zero if glyph row ROW should be
19671 used to hold the cursor. */
19673 static int
19674 cursor_row_p (struct glyph_row *row)
19676 return row_for_charpos_p (row, PT);
19681 /* Push the property PROP so that it will be rendered at the current
19682 position in IT. Return 1 if PROP was successfully pushed, 0
19683 otherwise. Called from handle_line_prefix to handle the
19684 `line-prefix' and `wrap-prefix' properties. */
19686 static int
19687 push_prefix_prop (struct it *it, Lisp_Object prop)
19689 struct text_pos pos =
19690 STRINGP (it->string) ? it->current.string_pos : it->current.pos;
19692 eassert (it->method == GET_FROM_BUFFER
19693 || it->method == GET_FROM_DISPLAY_VECTOR
19694 || it->method == GET_FROM_STRING);
19696 /* We need to save the current buffer/string position, so it will be
19697 restored by pop_it, because iterate_out_of_display_property
19698 depends on that being set correctly, but some situations leave
19699 it->position not yet set when this function is called. */
19700 push_it (it, &pos);
19702 if (STRINGP (prop))
19704 if (SCHARS (prop) == 0)
19706 pop_it (it);
19707 return 0;
19710 it->string = prop;
19711 it->string_from_prefix_prop_p = 1;
19712 it->multibyte_p = STRING_MULTIBYTE (it->string);
19713 it->current.overlay_string_index = -1;
19714 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
19715 it->end_charpos = it->string_nchars = SCHARS (it->string);
19716 it->method = GET_FROM_STRING;
19717 it->stop_charpos = 0;
19718 it->prev_stop = 0;
19719 it->base_level_stop = 0;
19721 /* Force paragraph direction to be that of the parent
19722 buffer/string. */
19723 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
19724 it->paragraph_embedding = it->bidi_it.paragraph_dir;
19725 else
19726 it->paragraph_embedding = L2R;
19728 /* Set up the bidi iterator for this display string. */
19729 if (it->bidi_p)
19731 it->bidi_it.string.lstring = it->string;
19732 it->bidi_it.string.s = NULL;
19733 it->bidi_it.string.schars = it->end_charpos;
19734 it->bidi_it.string.bufpos = IT_CHARPOS (*it);
19735 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
19736 it->bidi_it.string.unibyte = !it->multibyte_p;
19737 it->bidi_it.w = it->w;
19738 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
19741 else if (CONSP (prop) && EQ (XCAR (prop), Qspace))
19743 it->method = GET_FROM_STRETCH;
19744 it->object = prop;
19746 #ifdef HAVE_WINDOW_SYSTEM
19747 else if (IMAGEP (prop))
19749 it->what = IT_IMAGE;
19750 it->image_id = lookup_image (it->f, prop);
19751 it->method = GET_FROM_IMAGE;
19753 #endif /* HAVE_WINDOW_SYSTEM */
19754 else
19756 pop_it (it); /* bogus display property, give up */
19757 return 0;
19760 return 1;
19763 /* Return the character-property PROP at the current position in IT. */
19765 static Lisp_Object
19766 get_it_property (struct it *it, Lisp_Object prop)
19768 Lisp_Object position, object = it->object;
19770 if (STRINGP (object))
19771 position = make_number (IT_STRING_CHARPOS (*it));
19772 else if (BUFFERP (object))
19774 position = make_number (IT_CHARPOS (*it));
19775 object = it->window;
19777 else
19778 return Qnil;
19780 return Fget_char_property (position, prop, object);
19783 /* See if there's a line- or wrap-prefix, and if so, push it on IT. */
19785 static void
19786 handle_line_prefix (struct it *it)
19788 Lisp_Object prefix;
19790 if (it->continuation_lines_width > 0)
19792 prefix = get_it_property (it, Qwrap_prefix);
19793 if (NILP (prefix))
19794 prefix = Vwrap_prefix;
19796 else
19798 prefix = get_it_property (it, Qline_prefix);
19799 if (NILP (prefix))
19800 prefix = Vline_prefix;
19802 if (! NILP (prefix) && push_prefix_prop (it, prefix))
19804 /* If the prefix is wider than the window, and we try to wrap
19805 it, it would acquire its own wrap prefix, and so on till the
19806 iterator stack overflows. So, don't wrap the prefix. */
19807 it->line_wrap = TRUNCATE;
19808 it->avoid_cursor_p = 1;
19814 /* Remove N glyphs at the start of a reversed IT->glyph_row. Called
19815 only for R2L lines from display_line and display_string, when they
19816 decide that too many glyphs were produced by PRODUCE_GLYPHS, and
19817 the line/string needs to be continued on the next glyph row. */
19818 static void
19819 unproduce_glyphs (struct it *it, int n)
19821 struct glyph *glyph, *end;
19823 eassert (it->glyph_row);
19824 eassert (it->glyph_row->reversed_p);
19825 eassert (it->area == TEXT_AREA);
19826 eassert (n <= it->glyph_row->used[TEXT_AREA]);
19828 if (n > it->glyph_row->used[TEXT_AREA])
19829 n = it->glyph_row->used[TEXT_AREA];
19830 glyph = it->glyph_row->glyphs[TEXT_AREA] + n;
19831 end = it->glyph_row->glyphs[TEXT_AREA] + it->glyph_row->used[TEXT_AREA];
19832 for ( ; glyph < end; glyph++)
19833 glyph[-n] = *glyph;
19836 /* Find the positions in a bidi-reordered ROW to serve as ROW->minpos
19837 and ROW->maxpos. */
19838 static void
19839 find_row_edges (struct it *it, struct glyph_row *row,
19840 ptrdiff_t min_pos, ptrdiff_t min_bpos,
19841 ptrdiff_t max_pos, ptrdiff_t max_bpos)
19843 /* FIXME: Revisit this when glyph ``spilling'' in continuation
19844 lines' rows is implemented for bidi-reordered rows. */
19846 /* ROW->minpos is the value of min_pos, the minimal buffer position
19847 we have in ROW, or ROW->start.pos if that is smaller. */
19848 if (min_pos <= ZV && min_pos < row->start.pos.charpos)
19849 SET_TEXT_POS (row->minpos, min_pos, min_bpos);
19850 else
19851 /* We didn't find buffer positions smaller than ROW->start, or
19852 didn't find _any_ valid buffer positions in any of the glyphs,
19853 so we must trust the iterator's computed positions. */
19854 row->minpos = row->start.pos;
19855 if (max_pos <= 0)
19857 max_pos = CHARPOS (it->current.pos);
19858 max_bpos = BYTEPOS (it->current.pos);
19861 /* Here are the various use-cases for ending the row, and the
19862 corresponding values for ROW->maxpos:
19864 Line ends in a newline from buffer eol_pos + 1
19865 Line is continued from buffer max_pos + 1
19866 Line is truncated on right it->current.pos
19867 Line ends in a newline from string max_pos + 1(*)
19868 (*) + 1 only when line ends in a forward scan
19869 Line is continued from string max_pos
19870 Line is continued from display vector max_pos
19871 Line is entirely from a string min_pos == max_pos
19872 Line is entirely from a display vector min_pos == max_pos
19873 Line that ends at ZV ZV
19875 If you discover other use-cases, please add them here as
19876 appropriate. */
19877 if (row->ends_at_zv_p)
19878 row->maxpos = it->current.pos;
19879 else if (row->used[TEXT_AREA])
19881 int seen_this_string = 0;
19882 struct glyph_row *r1 = row - 1;
19884 /* Did we see the same display string on the previous row? */
19885 if (STRINGP (it->object)
19886 /* this is not the first row */
19887 && row > it->w->desired_matrix->rows
19888 /* previous row is not the header line */
19889 && !r1->mode_line_p
19890 /* previous row also ends in a newline from a string */
19891 && r1->ends_in_newline_from_string_p)
19893 struct glyph *start, *end;
19895 /* Search for the last glyph of the previous row that came
19896 from buffer or string. Depending on whether the row is
19897 L2R or R2L, we need to process it front to back or the
19898 other way round. */
19899 if (!r1->reversed_p)
19901 start = r1->glyphs[TEXT_AREA];
19902 end = start + r1->used[TEXT_AREA];
19903 /* Glyphs inserted by redisplay have nil as their object. */
19904 while (end > start
19905 && NILP ((end - 1)->object)
19906 && (end - 1)->charpos <= 0)
19907 --end;
19908 if (end > start)
19910 if (EQ ((end - 1)->object, it->object))
19911 seen_this_string = 1;
19913 else
19914 /* If all the glyphs of the previous row were inserted
19915 by redisplay, it means the previous row was
19916 produced from a single newline, which is only
19917 possible if that newline came from the same string
19918 as the one which produced this ROW. */
19919 seen_this_string = 1;
19921 else
19923 end = r1->glyphs[TEXT_AREA] - 1;
19924 start = end + r1->used[TEXT_AREA];
19925 while (end < start
19926 && NILP ((end + 1)->object)
19927 && (end + 1)->charpos <= 0)
19928 ++end;
19929 if (end < start)
19931 if (EQ ((end + 1)->object, it->object))
19932 seen_this_string = 1;
19934 else
19935 seen_this_string = 1;
19938 /* Take note of each display string that covers a newline only
19939 once, the first time we see it. This is for when a display
19940 string includes more than one newline in it. */
19941 if (row->ends_in_newline_from_string_p && !seen_this_string)
19943 /* If we were scanning the buffer forward when we displayed
19944 the string, we want to account for at least one buffer
19945 position that belongs to this row (position covered by
19946 the display string), so that cursor positioning will
19947 consider this row as a candidate when point is at the end
19948 of the visual line represented by this row. This is not
19949 required when scanning back, because max_pos will already
19950 have a much larger value. */
19951 if (CHARPOS (row->end.pos) > max_pos)
19952 INC_BOTH (max_pos, max_bpos);
19953 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
19955 else if (CHARPOS (it->eol_pos) > 0)
19956 SET_TEXT_POS (row->maxpos,
19957 CHARPOS (it->eol_pos) + 1, BYTEPOS (it->eol_pos) + 1);
19958 else if (row->continued_p)
19960 /* If max_pos is different from IT's current position, it
19961 means IT->method does not belong to the display element
19962 at max_pos. However, it also means that the display
19963 element at max_pos was displayed in its entirety on this
19964 line, which is equivalent to saying that the next line
19965 starts at the next buffer position. */
19966 if (IT_CHARPOS (*it) == max_pos && it->method != GET_FROM_BUFFER)
19967 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
19968 else
19970 INC_BOTH (max_pos, max_bpos);
19971 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
19974 else if (row->truncated_on_right_p)
19975 /* display_line already called reseat_at_next_visible_line_start,
19976 which puts the iterator at the beginning of the next line, in
19977 the logical order. */
19978 row->maxpos = it->current.pos;
19979 else if (max_pos == min_pos && it->method != GET_FROM_BUFFER)
19980 /* A line that is entirely from a string/image/stretch... */
19981 row->maxpos = row->minpos;
19982 else
19983 emacs_abort ();
19985 else
19986 row->maxpos = it->current.pos;
19989 /* Construct the glyph row IT->glyph_row in the desired matrix of
19990 IT->w from text at the current position of IT. See dispextern.h
19991 for an overview of struct it. Value is non-zero if
19992 IT->glyph_row displays text, as opposed to a line displaying ZV
19993 only. */
19995 static int
19996 display_line (struct it *it)
19998 struct glyph_row *row = it->glyph_row;
19999 Lisp_Object overlay_arrow_string;
20000 struct it wrap_it;
20001 void *wrap_data = NULL;
20002 int may_wrap = 0, wrap_x IF_LINT (= 0);
20003 int wrap_row_used = -1;
20004 int wrap_row_ascent IF_LINT (= 0), wrap_row_height IF_LINT (= 0);
20005 int wrap_row_phys_ascent IF_LINT (= 0), wrap_row_phys_height IF_LINT (= 0);
20006 int wrap_row_extra_line_spacing IF_LINT (= 0);
20007 ptrdiff_t wrap_row_min_pos IF_LINT (= 0), wrap_row_min_bpos IF_LINT (= 0);
20008 ptrdiff_t wrap_row_max_pos IF_LINT (= 0), wrap_row_max_bpos IF_LINT (= 0);
20009 int cvpos;
20010 ptrdiff_t min_pos = ZV + 1, max_pos = 0;
20011 ptrdiff_t min_bpos IF_LINT (= 0), max_bpos IF_LINT (= 0);
20012 bool pending_handle_line_prefix = false;
20014 /* We always start displaying at hpos zero even if hscrolled. */
20015 eassert (it->hpos == 0 && it->current_x == 0);
20017 if (MATRIX_ROW_VPOS (row, it->w->desired_matrix)
20018 >= it->w->desired_matrix->nrows)
20020 it->w->nrows_scale_factor++;
20021 it->f->fonts_changed = 1;
20022 return 0;
20025 /* Clear the result glyph row and enable it. */
20026 prepare_desired_row (it->w, row, false);
20028 row->y = it->current_y;
20029 row->start = it->start;
20030 row->continuation_lines_width = it->continuation_lines_width;
20031 row->displays_text_p = 1;
20032 row->starts_in_middle_of_char_p = it->starts_in_middle_of_char_p;
20033 it->starts_in_middle_of_char_p = 0;
20035 /* Arrange the overlays nicely for our purposes. Usually, we call
20036 display_line on only one line at a time, in which case this
20037 can't really hurt too much, or we call it on lines which appear
20038 one after another in the buffer, in which case all calls to
20039 recenter_overlay_lists but the first will be pretty cheap. */
20040 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
20042 /* Move over display elements that are not visible because we are
20043 hscrolled. This may stop at an x-position < IT->first_visible_x
20044 if the first glyph is partially visible or if we hit a line end. */
20045 if (it->current_x < it->first_visible_x)
20047 enum move_it_result move_result;
20049 this_line_min_pos = row->start.pos;
20050 move_result = move_it_in_display_line_to (it, ZV, it->first_visible_x,
20051 MOVE_TO_POS | MOVE_TO_X);
20052 /* If we are under a large hscroll, move_it_in_display_line_to
20053 could hit the end of the line without reaching
20054 it->first_visible_x. Pretend that we did reach it. This is
20055 especially important on a TTY, where we will call
20056 extend_face_to_end_of_line, which needs to know how many
20057 blank glyphs to produce. */
20058 if (it->current_x < it->first_visible_x
20059 && (move_result == MOVE_NEWLINE_OR_CR
20060 || move_result == MOVE_POS_MATCH_OR_ZV))
20061 it->current_x = it->first_visible_x;
20063 /* Record the smallest positions seen while we moved over
20064 display elements that are not visible. This is needed by
20065 redisplay_internal for optimizing the case where the cursor
20066 stays inside the same line. The rest of this function only
20067 considers positions that are actually displayed, so
20068 RECORD_MAX_MIN_POS will not otherwise record positions that
20069 are hscrolled to the left of the left edge of the window. */
20070 min_pos = CHARPOS (this_line_min_pos);
20071 min_bpos = BYTEPOS (this_line_min_pos);
20073 else if (it->area == TEXT_AREA)
20075 /* We only do this when not calling move_it_in_display_line_to
20076 above, because that function calls itself handle_line_prefix. */
20077 handle_line_prefix (it);
20079 else
20081 /* Line-prefix and wrap-prefix are always displayed in the text
20082 area. But if this is the first call to display_line after
20083 init_iterator, the iterator might have been set up to write
20084 into a marginal area, e.g. if the line begins with some
20085 display property that writes to the margins. So we need to
20086 wait with the call to handle_line_prefix until whatever
20087 writes to the margin has done its job. */
20088 pending_handle_line_prefix = true;
20091 /* Get the initial row height. This is either the height of the
20092 text hscrolled, if there is any, or zero. */
20093 row->ascent = it->max_ascent;
20094 row->height = it->max_ascent + it->max_descent;
20095 row->phys_ascent = it->max_phys_ascent;
20096 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
20097 row->extra_line_spacing = it->max_extra_line_spacing;
20099 /* Utility macro to record max and min buffer positions seen until now. */
20100 #define RECORD_MAX_MIN_POS(IT) \
20101 do \
20103 int composition_p = !STRINGP ((IT)->string) \
20104 && ((IT)->what == IT_COMPOSITION); \
20105 ptrdiff_t current_pos = \
20106 composition_p ? (IT)->cmp_it.charpos \
20107 : IT_CHARPOS (*(IT)); \
20108 ptrdiff_t current_bpos = \
20109 composition_p ? CHAR_TO_BYTE (current_pos) \
20110 : IT_BYTEPOS (*(IT)); \
20111 if (current_pos < min_pos) \
20113 min_pos = current_pos; \
20114 min_bpos = current_bpos; \
20116 if (IT_CHARPOS (*it) > max_pos) \
20118 max_pos = IT_CHARPOS (*it); \
20119 max_bpos = IT_BYTEPOS (*it); \
20122 while (0)
20124 /* Loop generating characters. The loop is left with IT on the next
20125 character to display. */
20126 while (1)
20128 int n_glyphs_before, hpos_before, x_before;
20129 int x, nglyphs;
20130 int ascent = 0, descent = 0, phys_ascent = 0, phys_descent = 0;
20132 /* Retrieve the next thing to display. Value is zero if end of
20133 buffer reached. */
20134 if (!get_next_display_element (it))
20136 /* Maybe add a space at the end of this line that is used to
20137 display the cursor there under X. Set the charpos of the
20138 first glyph of blank lines not corresponding to any text
20139 to -1. */
20140 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
20141 row->exact_window_width_line_p = 1;
20142 else if ((append_space_for_newline (it, 1) && row->used[TEXT_AREA] == 1)
20143 || row->used[TEXT_AREA] == 0)
20145 row->glyphs[TEXT_AREA]->charpos = -1;
20146 row->displays_text_p = 0;
20148 if (!NILP (BVAR (XBUFFER (it->w->contents), indicate_empty_lines))
20149 && (!MINI_WINDOW_P (it->w)
20150 || (minibuf_level && EQ (it->window, minibuf_window))))
20151 row->indicate_empty_line_p = 1;
20154 it->continuation_lines_width = 0;
20155 row->ends_at_zv_p = 1;
20156 /* A row that displays right-to-left text must always have
20157 its last face extended all the way to the end of line,
20158 even if this row ends in ZV, because we still write to
20159 the screen left to right. We also need to extend the
20160 last face if the default face is remapped to some
20161 different face, otherwise the functions that clear
20162 portions of the screen will clear with the default face's
20163 background color. */
20164 if (row->reversed_p
20165 || lookup_basic_face (it->f, DEFAULT_FACE_ID) != DEFAULT_FACE_ID)
20166 extend_face_to_end_of_line (it);
20167 break;
20170 /* Now, get the metrics of what we want to display. This also
20171 generates glyphs in `row' (which is IT->glyph_row). */
20172 n_glyphs_before = row->used[TEXT_AREA];
20173 x = it->current_x;
20175 /* Remember the line height so far in case the next element doesn't
20176 fit on the line. */
20177 if (it->line_wrap != TRUNCATE)
20179 ascent = it->max_ascent;
20180 descent = it->max_descent;
20181 phys_ascent = it->max_phys_ascent;
20182 phys_descent = it->max_phys_descent;
20184 if (it->line_wrap == WORD_WRAP && it->area == TEXT_AREA)
20186 if (IT_DISPLAYING_WHITESPACE (it))
20187 may_wrap = 1;
20188 else if (may_wrap)
20190 SAVE_IT (wrap_it, *it, wrap_data);
20191 wrap_x = x;
20192 wrap_row_used = row->used[TEXT_AREA];
20193 wrap_row_ascent = row->ascent;
20194 wrap_row_height = row->height;
20195 wrap_row_phys_ascent = row->phys_ascent;
20196 wrap_row_phys_height = row->phys_height;
20197 wrap_row_extra_line_spacing = row->extra_line_spacing;
20198 wrap_row_min_pos = min_pos;
20199 wrap_row_min_bpos = min_bpos;
20200 wrap_row_max_pos = max_pos;
20201 wrap_row_max_bpos = max_bpos;
20202 may_wrap = 0;
20207 PRODUCE_GLYPHS (it);
20209 /* If this display element was in marginal areas, continue with
20210 the next one. */
20211 if (it->area != TEXT_AREA)
20213 row->ascent = max (row->ascent, it->max_ascent);
20214 row->height = max (row->height, it->max_ascent + it->max_descent);
20215 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
20216 row->phys_height = max (row->phys_height,
20217 it->max_phys_ascent + it->max_phys_descent);
20218 row->extra_line_spacing = max (row->extra_line_spacing,
20219 it->max_extra_line_spacing);
20220 set_iterator_to_next (it, 1);
20221 /* If we didn't handle the line/wrap prefix above, and the
20222 call to set_iterator_to_next just switched to TEXT_AREA,
20223 process the prefix now. */
20224 if (it->area == TEXT_AREA && pending_handle_line_prefix)
20226 pending_handle_line_prefix = false;
20227 handle_line_prefix (it);
20229 continue;
20232 /* Does the display element fit on the line? If we truncate
20233 lines, we should draw past the right edge of the window. If
20234 we don't truncate, we want to stop so that we can display the
20235 continuation glyph before the right margin. If lines are
20236 continued, there are two possible strategies for characters
20237 resulting in more than 1 glyph (e.g. tabs): Display as many
20238 glyphs as possible in this line and leave the rest for the
20239 continuation line, or display the whole element in the next
20240 line. Original redisplay did the former, so we do it also. */
20241 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
20242 hpos_before = it->hpos;
20243 x_before = x;
20245 if (/* Not a newline. */
20246 nglyphs > 0
20247 /* Glyphs produced fit entirely in the line. */
20248 && it->current_x < it->last_visible_x)
20250 it->hpos += nglyphs;
20251 row->ascent = max (row->ascent, it->max_ascent);
20252 row->height = max (row->height, it->max_ascent + it->max_descent);
20253 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
20254 row->phys_height = max (row->phys_height,
20255 it->max_phys_ascent + it->max_phys_descent);
20256 row->extra_line_spacing = max (row->extra_line_spacing,
20257 it->max_extra_line_spacing);
20258 if (it->current_x - it->pixel_width < it->first_visible_x
20259 /* In R2L rows, we arrange in extend_face_to_end_of_line
20260 to add a right offset to the line, by a suitable
20261 change to the stretch glyph that is the leftmost
20262 glyph of the line. */
20263 && !row->reversed_p)
20264 row->x = x - it->first_visible_x;
20265 /* Record the maximum and minimum buffer positions seen so
20266 far in glyphs that will be displayed by this row. */
20267 if (it->bidi_p)
20268 RECORD_MAX_MIN_POS (it);
20270 else
20272 int i, new_x;
20273 struct glyph *glyph;
20275 for (i = 0; i < nglyphs; ++i, x = new_x)
20277 /* Identify the glyphs added by the last call to
20278 PRODUCE_GLYPHS. In R2L rows, they are prepended to
20279 the previous glyphs. */
20280 if (!row->reversed_p)
20281 glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
20282 else
20283 glyph = row->glyphs[TEXT_AREA] + nglyphs - 1 - i;
20284 new_x = x + glyph->pixel_width;
20286 if (/* Lines are continued. */
20287 it->line_wrap != TRUNCATE
20288 && (/* Glyph doesn't fit on the line. */
20289 new_x > it->last_visible_x
20290 /* Or it fits exactly on a window system frame. */
20291 || (new_x == it->last_visible_x
20292 && FRAME_WINDOW_P (it->f)
20293 && (row->reversed_p
20294 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
20295 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)))))
20297 /* End of a continued line. */
20299 if (it->hpos == 0
20300 || (new_x == it->last_visible_x
20301 && FRAME_WINDOW_P (it->f)
20302 && (row->reversed_p
20303 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
20304 : WINDOW_RIGHT_FRINGE_WIDTH (it->w))))
20306 /* Current glyph is the only one on the line or
20307 fits exactly on the line. We must continue
20308 the line because we can't draw the cursor
20309 after the glyph. */
20310 row->continued_p = 1;
20311 it->current_x = new_x;
20312 it->continuation_lines_width += new_x;
20313 ++it->hpos;
20314 if (i == nglyphs - 1)
20316 /* If line-wrap is on, check if a previous
20317 wrap point was found. */
20318 if (!IT_OVERFLOW_NEWLINE_INTO_FRINGE (it)
20319 && wrap_row_used > 0
20320 /* Even if there is a previous wrap
20321 point, continue the line here as
20322 usual, if (i) the previous character
20323 was a space or tab AND (ii) the
20324 current character is not. */
20325 && (!may_wrap
20326 || IT_DISPLAYING_WHITESPACE (it)))
20327 goto back_to_wrap;
20329 /* Record the maximum and minimum buffer
20330 positions seen so far in glyphs that will be
20331 displayed by this row. */
20332 if (it->bidi_p)
20333 RECORD_MAX_MIN_POS (it);
20334 set_iterator_to_next (it, 1);
20335 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
20337 if (!get_next_display_element (it))
20339 row->exact_window_width_line_p = 1;
20340 it->continuation_lines_width = 0;
20341 row->continued_p = 0;
20342 row->ends_at_zv_p = 1;
20344 else if (ITERATOR_AT_END_OF_LINE_P (it))
20346 row->continued_p = 0;
20347 row->exact_window_width_line_p = 1;
20349 /* If line-wrap is on, check if a
20350 previous wrap point was found. */
20351 else if (wrap_row_used > 0
20352 /* Even if there is a previous wrap
20353 point, continue the line here as
20354 usual, if (i) the previous character
20355 was a space or tab AND (ii) the
20356 current character is not. */
20357 && (!may_wrap
20358 || IT_DISPLAYING_WHITESPACE (it)))
20359 goto back_to_wrap;
20363 else if (it->bidi_p)
20364 RECORD_MAX_MIN_POS (it);
20365 if (WINDOW_LEFT_MARGIN_WIDTH (it->w) > 0
20366 || WINDOW_RIGHT_MARGIN_WIDTH (it->w) > 0)
20367 extend_face_to_end_of_line (it);
20369 else if (CHAR_GLYPH_PADDING_P (*glyph)
20370 && !FRAME_WINDOW_P (it->f))
20372 /* A padding glyph that doesn't fit on this line.
20373 This means the whole character doesn't fit
20374 on the line. */
20375 if (row->reversed_p)
20376 unproduce_glyphs (it, row->used[TEXT_AREA]
20377 - n_glyphs_before);
20378 row->used[TEXT_AREA] = n_glyphs_before;
20380 /* Fill the rest of the row with continuation
20381 glyphs like in 20.x. */
20382 while (row->glyphs[TEXT_AREA] + row->used[TEXT_AREA]
20383 < row->glyphs[1 + TEXT_AREA])
20384 produce_special_glyphs (it, IT_CONTINUATION);
20386 row->continued_p = 1;
20387 it->current_x = x_before;
20388 it->continuation_lines_width += x_before;
20390 /* Restore the height to what it was before the
20391 element not fitting on the line. */
20392 it->max_ascent = ascent;
20393 it->max_descent = descent;
20394 it->max_phys_ascent = phys_ascent;
20395 it->max_phys_descent = phys_descent;
20396 if (WINDOW_LEFT_MARGIN_WIDTH (it->w) > 0
20397 || WINDOW_RIGHT_MARGIN_WIDTH (it->w) > 0)
20398 extend_face_to_end_of_line (it);
20400 else if (wrap_row_used > 0)
20402 back_to_wrap:
20403 if (row->reversed_p)
20404 unproduce_glyphs (it,
20405 row->used[TEXT_AREA] - wrap_row_used);
20406 RESTORE_IT (it, &wrap_it, wrap_data);
20407 it->continuation_lines_width += wrap_x;
20408 row->used[TEXT_AREA] = wrap_row_used;
20409 row->ascent = wrap_row_ascent;
20410 row->height = wrap_row_height;
20411 row->phys_ascent = wrap_row_phys_ascent;
20412 row->phys_height = wrap_row_phys_height;
20413 row->extra_line_spacing = wrap_row_extra_line_spacing;
20414 min_pos = wrap_row_min_pos;
20415 min_bpos = wrap_row_min_bpos;
20416 max_pos = wrap_row_max_pos;
20417 max_bpos = wrap_row_max_bpos;
20418 row->continued_p = 1;
20419 row->ends_at_zv_p = 0;
20420 row->exact_window_width_line_p = 0;
20421 it->continuation_lines_width += x;
20423 /* Make sure that a non-default face is extended
20424 up to the right margin of the window. */
20425 extend_face_to_end_of_line (it);
20427 else if (it->c == '\t' && FRAME_WINDOW_P (it->f))
20429 /* A TAB that extends past the right edge of the
20430 window. This produces a single glyph on
20431 window system frames. We leave the glyph in
20432 this row and let it fill the row, but don't
20433 consume the TAB. */
20434 if ((row->reversed_p
20435 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
20436 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
20437 produce_special_glyphs (it, IT_CONTINUATION);
20438 it->continuation_lines_width += it->last_visible_x;
20439 row->ends_in_middle_of_char_p = 1;
20440 row->continued_p = 1;
20441 glyph->pixel_width = it->last_visible_x - x;
20442 it->starts_in_middle_of_char_p = 1;
20443 if (WINDOW_LEFT_MARGIN_WIDTH (it->w) > 0
20444 || WINDOW_RIGHT_MARGIN_WIDTH (it->w) > 0)
20445 extend_face_to_end_of_line (it);
20447 else
20449 /* Something other than a TAB that draws past
20450 the right edge of the window. Restore
20451 positions to values before the element. */
20452 if (row->reversed_p)
20453 unproduce_glyphs (it, row->used[TEXT_AREA]
20454 - (n_glyphs_before + i));
20455 row->used[TEXT_AREA] = n_glyphs_before + i;
20457 /* Display continuation glyphs. */
20458 it->current_x = x_before;
20459 it->continuation_lines_width += x;
20460 if (!FRAME_WINDOW_P (it->f)
20461 || (row->reversed_p
20462 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
20463 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
20464 produce_special_glyphs (it, IT_CONTINUATION);
20465 row->continued_p = 1;
20467 extend_face_to_end_of_line (it);
20469 if (nglyphs > 1 && i > 0)
20471 row->ends_in_middle_of_char_p = 1;
20472 it->starts_in_middle_of_char_p = 1;
20475 /* Restore the height to what it was before the
20476 element not fitting on the line. */
20477 it->max_ascent = ascent;
20478 it->max_descent = descent;
20479 it->max_phys_ascent = phys_ascent;
20480 it->max_phys_descent = phys_descent;
20483 break;
20485 else if (new_x > it->first_visible_x)
20487 /* Increment number of glyphs actually displayed. */
20488 ++it->hpos;
20490 /* Record the maximum and minimum buffer positions
20491 seen so far in glyphs that will be displayed by
20492 this row. */
20493 if (it->bidi_p)
20494 RECORD_MAX_MIN_POS (it);
20496 if (x < it->first_visible_x && !row->reversed_p)
20497 /* Glyph is partially visible, i.e. row starts at
20498 negative X position. Don't do that in R2L
20499 rows, where we arrange to add a right offset to
20500 the line in extend_face_to_end_of_line, by a
20501 suitable change to the stretch glyph that is
20502 the leftmost glyph of the line. */
20503 row->x = x - it->first_visible_x;
20504 /* When the last glyph of an R2L row only fits
20505 partially on the line, we need to set row->x to a
20506 negative offset, so that the leftmost glyph is
20507 the one that is partially visible. But if we are
20508 going to produce the truncation glyph, this will
20509 be taken care of in produce_special_glyphs. */
20510 if (row->reversed_p
20511 && new_x > it->last_visible_x
20512 && !(it->line_wrap == TRUNCATE
20513 && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0))
20515 eassert (FRAME_WINDOW_P (it->f));
20516 row->x = it->last_visible_x - new_x;
20519 else
20521 /* Glyph is completely off the left margin of the
20522 window. This should not happen because of the
20523 move_it_in_display_line at the start of this
20524 function, unless the text display area of the
20525 window is empty. */
20526 eassert (it->first_visible_x <= it->last_visible_x);
20529 /* Even if this display element produced no glyphs at all,
20530 we want to record its position. */
20531 if (it->bidi_p && nglyphs == 0)
20532 RECORD_MAX_MIN_POS (it);
20534 row->ascent = max (row->ascent, it->max_ascent);
20535 row->height = max (row->height, it->max_ascent + it->max_descent);
20536 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
20537 row->phys_height = max (row->phys_height,
20538 it->max_phys_ascent + it->max_phys_descent);
20539 row->extra_line_spacing = max (row->extra_line_spacing,
20540 it->max_extra_line_spacing);
20542 /* End of this display line if row is continued. */
20543 if (row->continued_p || row->ends_at_zv_p)
20544 break;
20547 at_end_of_line:
20548 /* Is this a line end? If yes, we're also done, after making
20549 sure that a non-default face is extended up to the right
20550 margin of the window. */
20551 if (ITERATOR_AT_END_OF_LINE_P (it))
20553 int used_before = row->used[TEXT_AREA];
20555 row->ends_in_newline_from_string_p = STRINGP (it->object);
20557 /* Add a space at the end of the line that is used to
20558 display the cursor there. */
20559 if (!IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
20560 append_space_for_newline (it, 0);
20562 /* Extend the face to the end of the line. */
20563 extend_face_to_end_of_line (it);
20565 /* Make sure we have the position. */
20566 if (used_before == 0)
20567 row->glyphs[TEXT_AREA]->charpos = CHARPOS (it->position);
20569 /* Record the position of the newline, for use in
20570 find_row_edges. */
20571 it->eol_pos = it->current.pos;
20573 /* Consume the line end. This skips over invisible lines. */
20574 set_iterator_to_next (it, 1);
20575 it->continuation_lines_width = 0;
20576 break;
20579 /* Proceed with next display element. Note that this skips
20580 over lines invisible because of selective display. */
20581 set_iterator_to_next (it, 1);
20583 /* If we truncate lines, we are done when the last displayed
20584 glyphs reach past the right margin of the window. */
20585 if (it->line_wrap == TRUNCATE
20586 && ((FRAME_WINDOW_P (it->f)
20587 /* Images are preprocessed in produce_image_glyph such
20588 that they are cropped at the right edge of the
20589 window, so an image glyph will always end exactly at
20590 last_visible_x, even if there's no right fringe. */
20591 && ((row->reversed_p
20592 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
20593 : WINDOW_RIGHT_FRINGE_WIDTH (it->w))
20594 || it->what == IT_IMAGE))
20595 ? (it->current_x >= it->last_visible_x)
20596 : (it->current_x > it->last_visible_x)))
20598 /* Maybe add truncation glyphs. */
20599 if (!FRAME_WINDOW_P (it->f)
20600 || (row->reversed_p
20601 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
20602 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
20604 int i, n;
20606 if (!row->reversed_p)
20608 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
20609 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
20610 break;
20612 else
20614 for (i = 0; i < row->used[TEXT_AREA]; i++)
20615 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
20616 break;
20617 /* Remove any padding glyphs at the front of ROW, to
20618 make room for the truncation glyphs we will be
20619 adding below. The loop below always inserts at
20620 least one truncation glyph, so also remove the
20621 last glyph added to ROW. */
20622 unproduce_glyphs (it, i + 1);
20623 /* Adjust i for the loop below. */
20624 i = row->used[TEXT_AREA] - (i + 1);
20627 /* produce_special_glyphs overwrites the last glyph, so
20628 we don't want that if we want to keep that last
20629 glyph, which means it's an image. */
20630 if (it->current_x > it->last_visible_x)
20632 it->current_x = x_before;
20633 if (!FRAME_WINDOW_P (it->f))
20635 for (n = row->used[TEXT_AREA]; i < n; ++i)
20637 row->used[TEXT_AREA] = i;
20638 produce_special_glyphs (it, IT_TRUNCATION);
20641 else
20643 row->used[TEXT_AREA] = i;
20644 produce_special_glyphs (it, IT_TRUNCATION);
20646 it->hpos = hpos_before;
20649 else if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
20651 /* Don't truncate if we can overflow newline into fringe. */
20652 if (!get_next_display_element (it))
20654 it->continuation_lines_width = 0;
20655 row->ends_at_zv_p = 1;
20656 row->exact_window_width_line_p = 1;
20657 break;
20659 if (ITERATOR_AT_END_OF_LINE_P (it))
20661 row->exact_window_width_line_p = 1;
20662 goto at_end_of_line;
20664 it->current_x = x_before;
20665 it->hpos = hpos_before;
20668 row->truncated_on_right_p = 1;
20669 it->continuation_lines_width = 0;
20670 reseat_at_next_visible_line_start (it, 0);
20671 /* We insist below that IT's position be at ZV because in
20672 bidi-reordered lines the character at visible line start
20673 might not be the character that follows the newline in
20674 the logical order. */
20675 if (IT_BYTEPOS (*it) > BEG_BYTE)
20676 row->ends_at_zv_p =
20677 IT_BYTEPOS (*it) >= ZV_BYTE && FETCH_BYTE (ZV_BYTE - 1) != '\n';
20678 else
20679 row->ends_at_zv_p = false;
20680 break;
20684 if (wrap_data)
20685 bidi_unshelve_cache (wrap_data, 1);
20687 /* If line is not empty and hscrolled, maybe insert truncation glyphs
20688 at the left window margin. */
20689 if (it->first_visible_x
20690 && IT_CHARPOS (*it) != CHARPOS (row->start.pos))
20692 if (!FRAME_WINDOW_P (it->f)
20693 || (((row->reversed_p
20694 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
20695 : WINDOW_LEFT_FRINGE_WIDTH (it->w)) == 0)
20696 /* Don't let insert_left_trunc_glyphs overwrite the
20697 first glyph of the row if it is an image. */
20698 && row->glyphs[TEXT_AREA]->type != IMAGE_GLYPH))
20699 insert_left_trunc_glyphs (it);
20700 row->truncated_on_left_p = 1;
20703 /* Remember the position at which this line ends.
20705 BIDI Note: any code that needs MATRIX_ROW_START/END_CHARPOS
20706 cannot be before the call to find_row_edges below, since that is
20707 where these positions are determined. */
20708 row->end = it->current;
20709 if (!it->bidi_p)
20711 row->minpos = row->start.pos;
20712 row->maxpos = row->end.pos;
20714 else
20716 /* ROW->minpos and ROW->maxpos must be the smallest and
20717 `1 + the largest' buffer positions in ROW. But if ROW was
20718 bidi-reordered, these two positions can be anywhere in the
20719 row, so we must determine them now. */
20720 find_row_edges (it, row, min_pos, min_bpos, max_pos, max_bpos);
20723 /* If the start of this line is the overlay arrow-position, then
20724 mark this glyph row as the one containing the overlay arrow.
20725 This is clearly a mess with variable size fonts. It would be
20726 better to let it be displayed like cursors under X. */
20727 if ((MATRIX_ROW_DISPLAYS_TEXT_P (row) || !overlay_arrow_seen)
20728 && (overlay_arrow_string = overlay_arrow_at_row (it, row),
20729 !NILP (overlay_arrow_string)))
20731 /* Overlay arrow in window redisplay is a fringe bitmap. */
20732 if (STRINGP (overlay_arrow_string))
20734 struct glyph_row *arrow_row
20735 = get_overlay_arrow_glyph_row (it->w, overlay_arrow_string);
20736 struct glyph *glyph = arrow_row->glyphs[TEXT_AREA];
20737 struct glyph *arrow_end = glyph + arrow_row->used[TEXT_AREA];
20738 struct glyph *p = row->glyphs[TEXT_AREA];
20739 struct glyph *p2, *end;
20741 /* Copy the arrow glyphs. */
20742 while (glyph < arrow_end)
20743 *p++ = *glyph++;
20745 /* Throw away padding glyphs. */
20746 p2 = p;
20747 end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
20748 while (p2 < end && CHAR_GLYPH_PADDING_P (*p2))
20749 ++p2;
20750 if (p2 > p)
20752 while (p2 < end)
20753 *p++ = *p2++;
20754 row->used[TEXT_AREA] = p2 - row->glyphs[TEXT_AREA];
20757 else
20759 eassert (INTEGERP (overlay_arrow_string));
20760 row->overlay_arrow_bitmap = XINT (overlay_arrow_string);
20762 overlay_arrow_seen = 1;
20765 /* Highlight trailing whitespace. */
20766 if (!NILP (Vshow_trailing_whitespace))
20767 highlight_trailing_whitespace (it->f, it->glyph_row);
20769 /* Compute pixel dimensions of this line. */
20770 compute_line_metrics (it);
20772 /* Implementation note: No changes in the glyphs of ROW or in their
20773 faces can be done past this point, because compute_line_metrics
20774 computes ROW's hash value and stores it within the glyph_row
20775 structure. */
20777 /* Record whether this row ends inside an ellipsis. */
20778 row->ends_in_ellipsis_p
20779 = (it->method == GET_FROM_DISPLAY_VECTOR
20780 && it->ellipsis_p);
20782 /* Save fringe bitmaps in this row. */
20783 row->left_user_fringe_bitmap = it->left_user_fringe_bitmap;
20784 row->left_user_fringe_face_id = it->left_user_fringe_face_id;
20785 row->right_user_fringe_bitmap = it->right_user_fringe_bitmap;
20786 row->right_user_fringe_face_id = it->right_user_fringe_face_id;
20788 it->left_user_fringe_bitmap = 0;
20789 it->left_user_fringe_face_id = 0;
20790 it->right_user_fringe_bitmap = 0;
20791 it->right_user_fringe_face_id = 0;
20793 /* Maybe set the cursor. */
20794 cvpos = it->w->cursor.vpos;
20795 if ((cvpos < 0
20796 /* In bidi-reordered rows, keep checking for proper cursor
20797 position even if one has been found already, because buffer
20798 positions in such rows change non-linearly with ROW->VPOS,
20799 when a line is continued. One exception: when we are at ZV,
20800 display cursor on the first suitable glyph row, since all
20801 the empty rows after that also have their position set to ZV. */
20802 /* FIXME: Revisit this when glyph ``spilling'' in continuation
20803 lines' rows is implemented for bidi-reordered rows. */
20804 || (it->bidi_p
20805 && !MATRIX_ROW (it->w->desired_matrix, cvpos)->ends_at_zv_p))
20806 && PT >= MATRIX_ROW_START_CHARPOS (row)
20807 && PT <= MATRIX_ROW_END_CHARPOS (row)
20808 && cursor_row_p (row))
20809 set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
20811 /* Prepare for the next line. This line starts horizontally at (X
20812 HPOS) = (0 0). Vertical positions are incremented. As a
20813 convenience for the caller, IT->glyph_row is set to the next
20814 row to be used. */
20815 it->current_x = it->hpos = 0;
20816 it->current_y += row->height;
20817 SET_TEXT_POS (it->eol_pos, 0, 0);
20818 ++it->vpos;
20819 ++it->glyph_row;
20820 /* The next row should by default use the same value of the
20821 reversed_p flag as this one. set_iterator_to_next decides when
20822 it's a new paragraph, and PRODUCE_GLYPHS recomputes the value of
20823 the flag accordingly. */
20824 if (it->glyph_row < MATRIX_BOTTOM_TEXT_ROW (it->w->desired_matrix, it->w))
20825 it->glyph_row->reversed_p = row->reversed_p;
20826 it->start = row->end;
20827 return MATRIX_ROW_DISPLAYS_TEXT_P (row);
20829 #undef RECORD_MAX_MIN_POS
20832 DEFUN ("current-bidi-paragraph-direction", Fcurrent_bidi_paragraph_direction,
20833 Scurrent_bidi_paragraph_direction, 0, 1, 0,
20834 doc: /* Return paragraph direction at point in BUFFER.
20835 Value is either `left-to-right' or `right-to-left'.
20836 If BUFFER is omitted or nil, it defaults to the current buffer.
20838 Paragraph direction determines how the text in the paragraph is displayed.
20839 In left-to-right paragraphs, text begins at the left margin of the window
20840 and the reading direction is generally left to right. In right-to-left
20841 paragraphs, text begins at the right margin and is read from right to left.
20843 See also `bidi-paragraph-direction'. */)
20844 (Lisp_Object buffer)
20846 struct buffer *buf = current_buffer;
20847 struct buffer *old = buf;
20849 if (! NILP (buffer))
20851 CHECK_BUFFER (buffer);
20852 buf = XBUFFER (buffer);
20855 if (NILP (BVAR (buf, bidi_display_reordering))
20856 || NILP (BVAR (buf, enable_multibyte_characters))
20857 /* When we are loading loadup.el, the character property tables
20858 needed for bidi iteration are not yet available. */
20859 || !NILP (Vpurify_flag))
20860 return Qleft_to_right;
20861 else if (!NILP (BVAR (buf, bidi_paragraph_direction)))
20862 return BVAR (buf, bidi_paragraph_direction);
20863 else
20865 /* Determine the direction from buffer text. We could try to
20866 use current_matrix if it is up to date, but this seems fast
20867 enough as it is. */
20868 struct bidi_it itb;
20869 ptrdiff_t pos = BUF_PT (buf);
20870 ptrdiff_t bytepos = BUF_PT_BYTE (buf);
20871 int c;
20872 void *itb_data = bidi_shelve_cache ();
20874 set_buffer_temp (buf);
20875 /* bidi_paragraph_init finds the base direction of the paragraph
20876 by searching forward from paragraph start. We need the base
20877 direction of the current or _previous_ paragraph, so we need
20878 to make sure we are within that paragraph. To that end, find
20879 the previous non-empty line. */
20880 if (pos >= ZV && pos > BEGV)
20881 DEC_BOTH (pos, bytepos);
20882 AUTO_STRING (trailing_white_space, "[\f\t ]*\n");
20883 if (fast_looking_at (trailing_white_space,
20884 pos, bytepos, ZV, ZV_BYTE, Qnil) > 0)
20886 while ((c = FETCH_BYTE (bytepos)) == '\n'
20887 || c == ' ' || c == '\t' || c == '\f')
20889 if (bytepos <= BEGV_BYTE)
20890 break;
20891 bytepos--;
20892 pos--;
20894 while (!CHAR_HEAD_P (FETCH_BYTE (bytepos)))
20895 bytepos--;
20897 bidi_init_it (pos, bytepos, FRAME_WINDOW_P (SELECTED_FRAME ()), &itb);
20898 itb.paragraph_dir = NEUTRAL_DIR;
20899 itb.string.s = NULL;
20900 itb.string.lstring = Qnil;
20901 itb.string.bufpos = 0;
20902 itb.string.from_disp_str = 0;
20903 itb.string.unibyte = 0;
20904 /* We have no window to use here for ignoring window-specific
20905 overlays. Using NULL for window pointer will cause
20906 compute_display_string_pos to use the current buffer. */
20907 itb.w = NULL;
20908 bidi_paragraph_init (NEUTRAL_DIR, &itb, 1);
20909 bidi_unshelve_cache (itb_data, 0);
20910 set_buffer_temp (old);
20911 switch (itb.paragraph_dir)
20913 case L2R:
20914 return Qleft_to_right;
20915 break;
20916 case R2L:
20917 return Qright_to_left;
20918 break;
20919 default:
20920 emacs_abort ();
20925 DEFUN ("bidi-find-overridden-directionality",
20926 Fbidi_find_overridden_directionality,
20927 Sbidi_find_overridden_directionality, 2, 3, 0,
20928 doc: /* Return position between FROM and TO where directionality was overridden.
20930 This function returns the first character position in the specified
20931 region of OBJECT where there is a character whose `bidi-class' property
20932 is `L', but which was forced to display as `R' by a directional
20933 override, and likewise with characters whose `bidi-class' is `R'
20934 or `AL' that were forced to display as `L'.
20936 If no such character is found, the function returns nil.
20938 OBJECT is a Lisp string or buffer to search for overridden
20939 directionality, and defaults to the current buffer if nil or omitted.
20940 OBJECT can also be a window, in which case the function will search
20941 the buffer displayed in that window. Passing the window instead of
20942 a buffer is preferable when the buffer is displayed in some window,
20943 because this function will then be able to correctly account for
20944 window-specific overlays, which can affect the results.
20946 Strong directional characters `L', `R', and `AL' can have their
20947 intrinsic directionality overridden by directional override
20948 control characters RLO \(u+202e) and LRO \(u+202d). See the
20949 function `get-char-code-property' for a way to inquire about
20950 the `bidi-class' property of a character. */)
20951 (Lisp_Object from, Lisp_Object to, Lisp_Object object)
20953 struct buffer *buf = current_buffer;
20954 struct buffer *old = buf;
20955 struct window *w = NULL;
20956 bool frame_window_p = FRAME_WINDOW_P (SELECTED_FRAME ());
20957 struct bidi_it itb;
20958 ptrdiff_t from_pos, to_pos, from_bpos;
20959 void *itb_data;
20961 if (!NILP (object))
20963 if (BUFFERP (object))
20964 buf = XBUFFER (object);
20965 else if (WINDOWP (object))
20967 w = decode_live_window (object);
20968 buf = XBUFFER (w->contents);
20969 frame_window_p = FRAME_WINDOW_P (XFRAME (w->frame));
20971 else
20972 CHECK_STRING (object);
20975 if (STRINGP (object))
20977 /* Characters in unibyte strings are always treated by bidi.c as
20978 strong LTR. */
20979 if (!STRING_MULTIBYTE (object)
20980 /* When we are loading loadup.el, the character property
20981 tables needed for bidi iteration are not yet
20982 available. */
20983 || !NILP (Vpurify_flag))
20984 return Qnil;
20986 validate_subarray (object, from, to, SCHARS (object), &from_pos, &to_pos);
20987 if (from_pos >= SCHARS (object))
20988 return Qnil;
20990 /* Set up the bidi iterator. */
20991 itb_data = bidi_shelve_cache ();
20992 itb.paragraph_dir = NEUTRAL_DIR;
20993 itb.string.lstring = object;
20994 itb.string.s = NULL;
20995 itb.string.schars = SCHARS (object);
20996 itb.string.bufpos = 0;
20997 itb.string.from_disp_str = 0;
20998 itb.string.unibyte = 0;
20999 itb.w = w;
21000 bidi_init_it (0, 0, frame_window_p, &itb);
21002 else
21004 /* Nothing this fancy can happen in unibyte buffers, or in a
21005 buffer that disabled reordering, or if FROM is at EOB. */
21006 if (NILP (BVAR (buf, bidi_display_reordering))
21007 || NILP (BVAR (buf, enable_multibyte_characters))
21008 /* When we are loading loadup.el, the character property
21009 tables needed for bidi iteration are not yet
21010 available. */
21011 || !NILP (Vpurify_flag))
21012 return Qnil;
21014 set_buffer_temp (buf);
21015 validate_region (&from, &to);
21016 from_pos = XINT (from);
21017 to_pos = XINT (to);
21018 if (from_pos >= ZV)
21019 return Qnil;
21021 /* Set up the bidi iterator. */
21022 itb_data = bidi_shelve_cache ();
21023 from_bpos = CHAR_TO_BYTE (from_pos);
21024 if (from_pos == BEGV)
21026 itb.charpos = BEGV;
21027 itb.bytepos = BEGV_BYTE;
21029 else if (FETCH_CHAR (from_bpos - 1) == '\n')
21031 itb.charpos = from_pos;
21032 itb.bytepos = from_bpos;
21034 else
21035 itb.charpos = find_newline_no_quit (from_pos, CHAR_TO_BYTE (from_pos),
21036 -1, &itb.bytepos);
21037 itb.paragraph_dir = NEUTRAL_DIR;
21038 itb.string.s = NULL;
21039 itb.string.lstring = Qnil;
21040 itb.string.bufpos = 0;
21041 itb.string.from_disp_str = 0;
21042 itb.string.unibyte = 0;
21043 itb.w = w;
21044 bidi_init_it (itb.charpos, itb.bytepos, frame_window_p, &itb);
21047 ptrdiff_t found;
21048 do {
21049 /* For the purposes of this function, the actual base direction of
21050 the paragraph doesn't matter, so just set it to L2R. */
21051 bidi_paragraph_init (L2R, &itb, 0);
21052 while ((found = bidi_find_first_overridden (&itb)) < from_pos)
21054 } while (found == ZV && itb.ch == '\n' && itb.charpos < to_pos);
21056 bidi_unshelve_cache (itb_data, 0);
21057 set_buffer_temp (old);
21059 return (from_pos <= found && found < to_pos) ? make_number (found) : Qnil;
21062 DEFUN ("move-point-visually", Fmove_point_visually,
21063 Smove_point_visually, 1, 1, 0,
21064 doc: /* Move point in the visual order in the specified DIRECTION.
21065 DIRECTION can be 1, meaning move to the right, or -1, which moves to the
21066 left.
21068 Value is the new character position of point. */)
21069 (Lisp_Object direction)
21071 struct window *w = XWINDOW (selected_window);
21072 struct buffer *b = XBUFFER (w->contents);
21073 struct glyph_row *row;
21074 int dir;
21075 Lisp_Object paragraph_dir;
21077 #define ROW_GLYPH_NEWLINE_P(ROW,GLYPH) \
21078 (!(ROW)->continued_p \
21079 && NILP ((GLYPH)->object) \
21080 && (GLYPH)->type == CHAR_GLYPH \
21081 && (GLYPH)->u.ch == ' ' \
21082 && (GLYPH)->charpos >= 0 \
21083 && !(GLYPH)->avoid_cursor_p)
21085 CHECK_NUMBER (direction);
21086 dir = XINT (direction);
21087 if (dir > 0)
21088 dir = 1;
21089 else
21090 dir = -1;
21092 /* If current matrix is up-to-date, we can use the information
21093 recorded in the glyphs, at least as long as the goal is on the
21094 screen. */
21095 if (w->window_end_valid
21096 && !windows_or_buffers_changed
21097 && b
21098 && !b->clip_changed
21099 && !b->prevent_redisplay_optimizations_p
21100 && !window_outdated (w)
21101 /* We rely below on the cursor coordinates to be up to date, but
21102 we cannot trust them if some command moved point since the
21103 last complete redisplay. */
21104 && w->last_point == BUF_PT (b)
21105 && w->cursor.vpos >= 0
21106 && w->cursor.vpos < w->current_matrix->nrows
21107 && (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos))->enabled_p)
21109 struct glyph *g = row->glyphs[TEXT_AREA];
21110 struct glyph *e = dir > 0 ? g + row->used[TEXT_AREA] : g - 1;
21111 struct glyph *gpt = g + w->cursor.hpos;
21113 for (g = gpt + dir; (dir > 0 ? g < e : g > e); g += dir)
21115 if (BUFFERP (g->object) && g->charpos != PT)
21117 SET_PT (g->charpos);
21118 w->cursor.vpos = -1;
21119 return make_number (PT);
21121 else if (!NILP (g->object) && !EQ (g->object, gpt->object))
21123 ptrdiff_t new_pos;
21125 if (BUFFERP (gpt->object))
21127 new_pos = PT;
21128 if ((gpt->resolved_level - row->reversed_p) % 2 == 0)
21129 new_pos += (row->reversed_p ? -dir : dir);
21130 else
21131 new_pos -= (row->reversed_p ? -dir : dir);
21133 else if (BUFFERP (g->object))
21134 new_pos = g->charpos;
21135 else
21136 break;
21137 SET_PT (new_pos);
21138 w->cursor.vpos = -1;
21139 return make_number (PT);
21141 else if (ROW_GLYPH_NEWLINE_P (row, g))
21143 /* Glyphs inserted at the end of a non-empty line for
21144 positioning the cursor have zero charpos, so we must
21145 deduce the value of point by other means. */
21146 if (g->charpos > 0)
21147 SET_PT (g->charpos);
21148 else if (row->ends_at_zv_p && PT != ZV)
21149 SET_PT (ZV);
21150 else if (PT != MATRIX_ROW_END_CHARPOS (row) - 1)
21151 SET_PT (MATRIX_ROW_END_CHARPOS (row) - 1);
21152 else
21153 break;
21154 w->cursor.vpos = -1;
21155 return make_number (PT);
21158 if (g == e || NILP (g->object))
21160 if (row->truncated_on_left_p || row->truncated_on_right_p)
21161 goto simulate_display;
21162 if (!row->reversed_p)
21163 row += dir;
21164 else
21165 row -= dir;
21166 if (row < MATRIX_FIRST_TEXT_ROW (w->current_matrix)
21167 || row > MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w))
21168 goto simulate_display;
21170 if (dir > 0)
21172 if (row->reversed_p && !row->continued_p)
21174 SET_PT (MATRIX_ROW_END_CHARPOS (row) - 1);
21175 w->cursor.vpos = -1;
21176 return make_number (PT);
21178 g = row->glyphs[TEXT_AREA];
21179 e = g + row->used[TEXT_AREA];
21180 for ( ; g < e; g++)
21182 if (BUFFERP (g->object)
21183 /* Empty lines have only one glyph, which stands
21184 for the newline, and whose charpos is the
21185 buffer position of the newline. */
21186 || ROW_GLYPH_NEWLINE_P (row, g)
21187 /* When the buffer ends in a newline, the line at
21188 EOB also has one glyph, but its charpos is -1. */
21189 || (row->ends_at_zv_p
21190 && !row->reversed_p
21191 && NILP (g->object)
21192 && g->type == CHAR_GLYPH
21193 && g->u.ch == ' '))
21195 if (g->charpos > 0)
21196 SET_PT (g->charpos);
21197 else if (!row->reversed_p
21198 && row->ends_at_zv_p
21199 && PT != ZV)
21200 SET_PT (ZV);
21201 else
21202 continue;
21203 w->cursor.vpos = -1;
21204 return make_number (PT);
21208 else
21210 if (!row->reversed_p && !row->continued_p)
21212 SET_PT (MATRIX_ROW_END_CHARPOS (row) - 1);
21213 w->cursor.vpos = -1;
21214 return make_number (PT);
21216 e = row->glyphs[TEXT_AREA];
21217 g = e + row->used[TEXT_AREA] - 1;
21218 for ( ; g >= e; g--)
21220 if (BUFFERP (g->object)
21221 || (ROW_GLYPH_NEWLINE_P (row, g)
21222 && g->charpos > 0)
21223 /* Empty R2L lines on GUI frames have the buffer
21224 position of the newline stored in the stretch
21225 glyph. */
21226 || g->type == STRETCH_GLYPH
21227 || (row->ends_at_zv_p
21228 && row->reversed_p
21229 && NILP (g->object)
21230 && g->type == CHAR_GLYPH
21231 && g->u.ch == ' '))
21233 if (g->charpos > 0)
21234 SET_PT (g->charpos);
21235 else if (row->reversed_p
21236 && row->ends_at_zv_p
21237 && PT != ZV)
21238 SET_PT (ZV);
21239 else
21240 continue;
21241 w->cursor.vpos = -1;
21242 return make_number (PT);
21249 simulate_display:
21251 /* If we wind up here, we failed to move by using the glyphs, so we
21252 need to simulate display instead. */
21254 if (b)
21255 paragraph_dir = Fcurrent_bidi_paragraph_direction (w->contents);
21256 else
21257 paragraph_dir = Qleft_to_right;
21258 if (EQ (paragraph_dir, Qright_to_left))
21259 dir = -dir;
21260 if (PT <= BEGV && dir < 0)
21261 xsignal0 (Qbeginning_of_buffer);
21262 else if (PT >= ZV && dir > 0)
21263 xsignal0 (Qend_of_buffer);
21264 else
21266 struct text_pos pt;
21267 struct it it;
21268 int pt_x, target_x, pixel_width, pt_vpos;
21269 bool at_eol_p;
21270 bool overshoot_expected = false;
21271 bool target_is_eol_p = false;
21273 /* Setup the arena. */
21274 SET_TEXT_POS (pt, PT, PT_BYTE);
21275 start_display (&it, w, pt);
21277 if (it.cmp_it.id < 0
21278 && it.method == GET_FROM_STRING
21279 && it.area == TEXT_AREA
21280 && it.string_from_display_prop_p
21281 && (it.sp > 0 && it.stack[it.sp - 1].method == GET_FROM_BUFFER))
21282 overshoot_expected = true;
21284 /* Find the X coordinate of point. We start from the beginning
21285 of this or previous line to make sure we are before point in
21286 the logical order (since the move_it_* functions can only
21287 move forward). */
21288 reseat:
21289 reseat_at_previous_visible_line_start (&it);
21290 it.current_x = it.hpos = it.current_y = it.vpos = 0;
21291 if (IT_CHARPOS (it) != PT)
21293 move_it_to (&it, overshoot_expected ? PT - 1 : PT,
21294 -1, -1, -1, MOVE_TO_POS);
21295 /* If we missed point because the character there is
21296 displayed out of a display vector that has more than one
21297 glyph, retry expecting overshoot. */
21298 if (it.method == GET_FROM_DISPLAY_VECTOR
21299 && it.current.dpvec_index > 0
21300 && !overshoot_expected)
21302 overshoot_expected = true;
21303 goto reseat;
21305 else if (IT_CHARPOS (it) != PT && !overshoot_expected)
21306 move_it_in_display_line (&it, PT, -1, MOVE_TO_POS);
21308 pt_x = it.current_x;
21309 pt_vpos = it.vpos;
21310 if (dir > 0 || overshoot_expected)
21312 struct glyph_row *row = it.glyph_row;
21314 /* When point is at beginning of line, we don't have
21315 information about the glyph there loaded into struct
21316 it. Calling get_next_display_element fixes that. */
21317 if (pt_x == 0)
21318 get_next_display_element (&it);
21319 at_eol_p = ITERATOR_AT_END_OF_LINE_P (&it);
21320 it.glyph_row = NULL;
21321 PRODUCE_GLYPHS (&it); /* compute it.pixel_width */
21322 it.glyph_row = row;
21323 /* PRODUCE_GLYPHS advances it.current_x, so we must restore
21324 it, lest it will become out of sync with it's buffer
21325 position. */
21326 it.current_x = pt_x;
21328 else
21329 at_eol_p = ITERATOR_AT_END_OF_LINE_P (&it);
21330 pixel_width = it.pixel_width;
21331 if (overshoot_expected && at_eol_p)
21332 pixel_width = 0;
21333 else if (pixel_width <= 0)
21334 pixel_width = 1;
21336 /* If there's a display string (or something similar) at point,
21337 we are actually at the glyph to the left of point, so we need
21338 to correct the X coordinate. */
21339 if (overshoot_expected)
21341 if (it.bidi_p)
21342 pt_x += pixel_width * it.bidi_it.scan_dir;
21343 else
21344 pt_x += pixel_width;
21347 /* Compute target X coordinate, either to the left or to the
21348 right of point. On TTY frames, all characters have the same
21349 pixel width of 1, so we can use that. On GUI frames we don't
21350 have an easy way of getting at the pixel width of the
21351 character to the left of point, so we use a different method
21352 of getting to that place. */
21353 if (dir > 0)
21354 target_x = pt_x + pixel_width;
21355 else
21356 target_x = pt_x - (!FRAME_WINDOW_P (it.f)) * pixel_width;
21358 /* Target X coordinate could be one line above or below the line
21359 of point, in which case we need to adjust the target X
21360 coordinate. Also, if moving to the left, we need to begin at
21361 the left edge of the point's screen line. */
21362 if (dir < 0)
21364 if (pt_x > 0)
21366 start_display (&it, w, pt);
21367 reseat_at_previous_visible_line_start (&it);
21368 it.current_x = it.current_y = it.hpos = 0;
21369 if (pt_vpos != 0)
21370 move_it_by_lines (&it, pt_vpos);
21372 else
21374 move_it_by_lines (&it, -1);
21375 target_x = it.last_visible_x - !FRAME_WINDOW_P (it.f);
21376 target_is_eol_p = true;
21377 /* Under word-wrap, we don't know the x coordinate of
21378 the last character displayed on the previous line,
21379 which immediately precedes the wrap point. To find
21380 out its x coordinate, we try moving to the right
21381 margin of the window, which will stop at the wrap
21382 point, and then reset target_x to point at the
21383 character that precedes the wrap point. This is not
21384 needed on GUI frames, because (see below) there we
21385 move from the left margin one grapheme cluster at a
21386 time, and stop when we hit the wrap point. */
21387 if (!FRAME_WINDOW_P (it.f) && it.line_wrap == WORD_WRAP)
21389 void *it_data = NULL;
21390 struct it it2;
21392 SAVE_IT (it2, it, it_data);
21393 move_it_in_display_line_to (&it, ZV, target_x,
21394 MOVE_TO_POS | MOVE_TO_X);
21395 /* If we arrived at target_x, that _is_ the last
21396 character on the previous line. */
21397 if (it.current_x != target_x)
21398 target_x = it.current_x - 1;
21399 RESTORE_IT (&it, &it2, it_data);
21403 else
21405 if (at_eol_p
21406 || (target_x >= it.last_visible_x
21407 && it.line_wrap != TRUNCATE))
21409 if (pt_x > 0)
21410 move_it_by_lines (&it, 0);
21411 move_it_by_lines (&it, 1);
21412 target_x = 0;
21416 /* Move to the target X coordinate. */
21417 #ifdef HAVE_WINDOW_SYSTEM
21418 /* On GUI frames, as we don't know the X coordinate of the
21419 character to the left of point, moving point to the left
21420 requires walking, one grapheme cluster at a time, until we
21421 find ourself at a place immediately to the left of the
21422 character at point. */
21423 if (FRAME_WINDOW_P (it.f) && dir < 0)
21425 struct text_pos new_pos;
21426 enum move_it_result rc = MOVE_X_REACHED;
21428 if (it.current_x == 0)
21429 get_next_display_element (&it);
21430 if (it.what == IT_COMPOSITION)
21432 new_pos.charpos = it.cmp_it.charpos;
21433 new_pos.bytepos = -1;
21435 else
21436 new_pos = it.current.pos;
21438 while (it.current_x + it.pixel_width <= target_x
21439 && (rc == MOVE_X_REACHED
21440 /* Under word-wrap, move_it_in_display_line_to
21441 stops at correct coordinates, but sometimes
21442 returns MOVE_POS_MATCH_OR_ZV. */
21443 || (it.line_wrap == WORD_WRAP
21444 && rc == MOVE_POS_MATCH_OR_ZV)))
21446 int new_x = it.current_x + it.pixel_width;
21448 /* For composed characters, we want the position of the
21449 first character in the grapheme cluster (usually, the
21450 composition's base character), whereas it.current
21451 might give us the position of the _last_ one, e.g. if
21452 the composition is rendered in reverse due to bidi
21453 reordering. */
21454 if (it.what == IT_COMPOSITION)
21456 new_pos.charpos = it.cmp_it.charpos;
21457 new_pos.bytepos = -1;
21459 else
21460 new_pos = it.current.pos;
21461 if (new_x == it.current_x)
21462 new_x++;
21463 rc = move_it_in_display_line_to (&it, ZV, new_x,
21464 MOVE_TO_POS | MOVE_TO_X);
21465 if (ITERATOR_AT_END_OF_LINE_P (&it) && !target_is_eol_p)
21466 break;
21468 /* The previous position we saw in the loop is the one we
21469 want. */
21470 if (new_pos.bytepos == -1)
21471 new_pos.bytepos = CHAR_TO_BYTE (new_pos.charpos);
21472 it.current.pos = new_pos;
21474 else
21475 #endif
21476 if (it.current_x != target_x)
21477 move_it_in_display_line_to (&it, ZV, target_x, MOVE_TO_POS | MOVE_TO_X);
21479 /* When lines are truncated, the above loop will stop at the
21480 window edge. But we want to get to the end of line, even if
21481 it is beyond the window edge; automatic hscroll will then
21482 scroll the window to show point as appropriate. */
21483 if (target_is_eol_p && it.line_wrap == TRUNCATE
21484 && get_next_display_element (&it))
21486 struct text_pos new_pos = it.current.pos;
21488 while (!ITERATOR_AT_END_OF_LINE_P (&it))
21490 set_iterator_to_next (&it, 0);
21491 if (it.method == GET_FROM_BUFFER)
21492 new_pos = it.current.pos;
21493 if (!get_next_display_element (&it))
21494 break;
21497 it.current.pos = new_pos;
21500 /* If we ended up in a display string that covers point, move to
21501 buffer position to the right in the visual order. */
21502 if (dir > 0)
21504 while (IT_CHARPOS (it) == PT)
21506 set_iterator_to_next (&it, 0);
21507 if (!get_next_display_element (&it))
21508 break;
21512 /* Move point to that position. */
21513 SET_PT_BOTH (IT_CHARPOS (it), IT_BYTEPOS (it));
21516 return make_number (PT);
21518 #undef ROW_GLYPH_NEWLINE_P
21521 DEFUN ("bidi-resolved-levels", Fbidi_resolved_levels,
21522 Sbidi_resolved_levels, 0, 1, 0,
21523 doc: /* Return the resolved bidirectional levels of characters at VPOS.
21525 The resolved levels are produced by the Emacs bidi reordering engine
21526 that implements the UBA, the Unicode Bidirectional Algorithm. Please
21527 read the Unicode Standard Annex 9 (UAX#9) for background information
21528 about these levels.
21530 VPOS is the zero-based number of the current window's screen line
21531 for which to produce the resolved levels. If VPOS is nil or omitted,
21532 it defaults to the screen line of point. If the window displays a
21533 header line, VPOS of zero will report on the header line, and first
21534 line of text in the window will have VPOS of 1.
21536 Value is an array of resolved levels, indexed by glyph number.
21537 Glyphs are numbered from zero starting from the beginning of the
21538 screen line, i.e. the left edge of the window for left-to-right lines
21539 and from the right edge for right-to-left lines. The resolved levels
21540 are produced only for the window's text area; text in display margins
21541 is not included.
21543 If the selected window's display is not up-to-date, or if the specified
21544 screen line does not display text, this function returns nil. It is
21545 highly recommended to bind this function to some simple key, like F8,
21546 in order to avoid these problems.
21548 This function exists mainly for testing the correctness of the
21549 Emacs UBA implementation, in particular with the test suite. */)
21550 (Lisp_Object vpos)
21552 struct window *w = XWINDOW (selected_window);
21553 struct buffer *b = XBUFFER (w->contents);
21554 int nrow;
21555 struct glyph_row *row;
21557 if (NILP (vpos))
21559 int d1, d2, d3, d4, d5;
21561 pos_visible_p (w, PT, &d1, &d2, &d3, &d4, &d5, &nrow);
21563 else
21565 CHECK_NUMBER_COERCE_MARKER (vpos);
21566 nrow = XINT (vpos);
21569 /* We require up-to-date glyph matrix for this window. */
21570 if (w->window_end_valid
21571 && !windows_or_buffers_changed
21572 && b
21573 && !b->clip_changed
21574 && !b->prevent_redisplay_optimizations_p
21575 && !window_outdated (w)
21576 && nrow >= 0
21577 && nrow < w->current_matrix->nrows
21578 && (row = MATRIX_ROW (w->current_matrix, nrow))->enabled_p
21579 && MATRIX_ROW_DISPLAYS_TEXT_P (row))
21581 struct glyph *g, *e, *g1;
21582 int nglyphs, i;
21583 Lisp_Object levels;
21585 if (!row->reversed_p) /* Left-to-right glyph row. */
21587 g = g1 = row->glyphs[TEXT_AREA];
21588 e = g + row->used[TEXT_AREA];
21590 /* Skip over glyphs at the start of the row that was
21591 generated by redisplay for its own needs. */
21592 while (g < e
21593 && NILP (g->object)
21594 && g->charpos < 0)
21595 g++;
21596 g1 = g;
21598 /* Count the "interesting" glyphs in this row. */
21599 for (nglyphs = 0; g < e && !NILP (g->object); g++)
21600 nglyphs++;
21602 /* Create and fill the array. */
21603 levels = make_uninit_vector (nglyphs);
21604 for (i = 0; g1 < g; i++, g1++)
21605 ASET (levels, i, make_number (g1->resolved_level));
21607 else /* Right-to-left glyph row. */
21609 g = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
21610 e = row->glyphs[TEXT_AREA] - 1;
21611 while (g > e
21612 && NILP (g->object)
21613 && g->charpos < 0)
21614 g--;
21615 g1 = g;
21616 for (nglyphs = 0; g > e && !NILP (g->object); g--)
21617 nglyphs++;
21618 levels = make_uninit_vector (nglyphs);
21619 for (i = 0; g1 > g; i++, g1--)
21620 ASET (levels, i, make_number (g1->resolved_level));
21622 return levels;
21624 else
21625 return Qnil;
21630 /***********************************************************************
21631 Menu Bar
21632 ***********************************************************************/
21634 /* Redisplay the menu bar in the frame for window W.
21636 The menu bar of X frames that don't have X toolkit support is
21637 displayed in a special window W->frame->menu_bar_window.
21639 The menu bar of terminal frames is treated specially as far as
21640 glyph matrices are concerned. Menu bar lines are not part of
21641 windows, so the update is done directly on the frame matrix rows
21642 for the menu bar. */
21644 static void
21645 display_menu_bar (struct window *w)
21647 struct frame *f = XFRAME (WINDOW_FRAME (w));
21648 struct it it;
21649 Lisp_Object items;
21650 int i;
21652 /* Don't do all this for graphical frames. */
21653 #ifdef HAVE_NTGUI
21654 if (FRAME_W32_P (f))
21655 return;
21656 #endif
21657 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
21658 if (FRAME_X_P (f))
21659 return;
21660 #endif
21662 #ifdef HAVE_NS
21663 if (FRAME_NS_P (f))
21664 return;
21665 #endif /* HAVE_NS */
21667 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
21668 eassert (!FRAME_WINDOW_P (f));
21669 init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MENU_FACE_ID);
21670 it.first_visible_x = 0;
21671 it.last_visible_x = FRAME_PIXEL_WIDTH (f);
21672 #elif defined (HAVE_X_WINDOWS) /* X without toolkit. */
21673 if (FRAME_WINDOW_P (f))
21675 /* Menu bar lines are displayed in the desired matrix of the
21676 dummy window menu_bar_window. */
21677 struct window *menu_w;
21678 menu_w = XWINDOW (f->menu_bar_window);
21679 init_iterator (&it, menu_w, -1, -1, menu_w->desired_matrix->rows,
21680 MENU_FACE_ID);
21681 it.first_visible_x = 0;
21682 it.last_visible_x = FRAME_PIXEL_WIDTH (f);
21684 else
21685 #endif /* not USE_X_TOOLKIT and not USE_GTK */
21687 /* This is a TTY frame, i.e. character hpos/vpos are used as
21688 pixel x/y. */
21689 init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
21690 MENU_FACE_ID);
21691 it.first_visible_x = 0;
21692 it.last_visible_x = FRAME_COLS (f);
21695 /* FIXME: This should be controlled by a user option. See the
21696 comments in redisplay_tool_bar and display_mode_line about
21697 this. */
21698 it.paragraph_embedding = L2R;
21700 /* Clear all rows of the menu bar. */
21701 for (i = 0; i < FRAME_MENU_BAR_LINES (f); ++i)
21703 struct glyph_row *row = it.glyph_row + i;
21704 clear_glyph_row (row);
21705 row->enabled_p = true;
21706 row->full_width_p = 1;
21707 row->reversed_p = false;
21710 /* Display all items of the menu bar. */
21711 items = FRAME_MENU_BAR_ITEMS (it.f);
21712 for (i = 0; i < ASIZE (items); i += 4)
21714 Lisp_Object string;
21716 /* Stop at nil string. */
21717 string = AREF (items, i + 1);
21718 if (NILP (string))
21719 break;
21721 /* Remember where item was displayed. */
21722 ASET (items, i + 3, make_number (it.hpos));
21724 /* Display the item, pad with one space. */
21725 if (it.current_x < it.last_visible_x)
21726 display_string (NULL, string, Qnil, 0, 0, &it,
21727 SCHARS (string) + 1, 0, 0, -1);
21730 /* Fill out the line with spaces. */
21731 if (it.current_x < it.last_visible_x)
21732 display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
21734 /* Compute the total height of the lines. */
21735 compute_line_metrics (&it);
21738 /* Deep copy of a glyph row, including the glyphs. */
21739 static void
21740 deep_copy_glyph_row (struct glyph_row *to, struct glyph_row *from)
21742 struct glyph *pointers[1 + LAST_AREA];
21743 int to_used = to->used[TEXT_AREA];
21745 /* Save glyph pointers of TO. */
21746 memcpy (pointers, to->glyphs, sizeof to->glyphs);
21748 /* Do a structure assignment. */
21749 *to = *from;
21751 /* Restore original glyph pointers of TO. */
21752 memcpy (to->glyphs, pointers, sizeof to->glyphs);
21754 /* Copy the glyphs. */
21755 memcpy (to->glyphs[TEXT_AREA], from->glyphs[TEXT_AREA],
21756 min (from->used[TEXT_AREA], to_used) * sizeof (struct glyph));
21758 /* If we filled only part of the TO row, fill the rest with
21759 space_glyph (which will display as empty space). */
21760 if (to_used > from->used[TEXT_AREA])
21761 fill_up_frame_row_with_spaces (to, to_used);
21764 /* Display one menu item on a TTY, by overwriting the glyphs in the
21765 frame F's desired glyph matrix with glyphs produced from the menu
21766 item text. Called from term.c to display TTY drop-down menus one
21767 item at a time.
21769 ITEM_TEXT is the menu item text as a C string.
21771 FACE_ID is the face ID to be used for this menu item. FACE_ID
21772 could specify one of 3 faces: a face for an enabled item, a face
21773 for a disabled item, or a face for a selected item.
21775 X and Y are coordinates of the first glyph in the frame's desired
21776 matrix to be overwritten by the menu item. Since this is a TTY, Y
21777 is the zero-based number of the glyph row and X is the zero-based
21778 glyph number in the row, starting from left, where to start
21779 displaying the item.
21781 SUBMENU non-zero means this menu item drops down a submenu, which
21782 should be indicated by displaying a proper visual cue after the
21783 item text. */
21785 void
21786 display_tty_menu_item (const char *item_text, int width, int face_id,
21787 int x, int y, int submenu)
21789 struct it it;
21790 struct frame *f = SELECTED_FRAME ();
21791 struct window *w = XWINDOW (f->selected_window);
21792 int saved_used, saved_truncated, saved_width, saved_reversed;
21793 struct glyph_row *row;
21794 size_t item_len = strlen (item_text);
21796 eassert (FRAME_TERMCAP_P (f));
21798 /* Don't write beyond the matrix's last row. This can happen for
21799 TTY screens that are not high enough to show the entire menu.
21800 (This is actually a bit of defensive programming, as
21801 tty_menu_display already limits the number of menu items to one
21802 less than the number of screen lines.) */
21803 if (y >= f->desired_matrix->nrows)
21804 return;
21806 init_iterator (&it, w, -1, -1, f->desired_matrix->rows + y, MENU_FACE_ID);
21807 it.first_visible_x = 0;
21808 it.last_visible_x = FRAME_COLS (f) - 1;
21809 row = it.glyph_row;
21810 /* Start with the row contents from the current matrix. */
21811 deep_copy_glyph_row (row, f->current_matrix->rows + y);
21812 saved_width = row->full_width_p;
21813 row->full_width_p = 1;
21814 saved_reversed = row->reversed_p;
21815 row->reversed_p = 0;
21816 row->enabled_p = true;
21818 /* Arrange for the menu item glyphs to start at (X,Y) and have the
21819 desired face. */
21820 eassert (x < f->desired_matrix->matrix_w);
21821 it.current_x = it.hpos = x;
21822 it.current_y = it.vpos = y;
21823 saved_used = row->used[TEXT_AREA];
21824 saved_truncated = row->truncated_on_right_p;
21825 row->used[TEXT_AREA] = x;
21826 it.face_id = face_id;
21827 it.line_wrap = TRUNCATE;
21829 /* FIXME: This should be controlled by a user option. See the
21830 comments in redisplay_tool_bar and display_mode_line about this.
21831 Also, if paragraph_embedding could ever be R2L, changes will be
21832 needed to avoid shifting to the right the row characters in
21833 term.c:append_glyph. */
21834 it.paragraph_embedding = L2R;
21836 /* Pad with a space on the left. */
21837 display_string (" ", Qnil, Qnil, 0, 0, &it, 1, 0, FRAME_COLS (f) - 1, -1);
21838 width--;
21839 /* Display the menu item, pad with spaces to WIDTH. */
21840 if (submenu)
21842 display_string (item_text, Qnil, Qnil, 0, 0, &it,
21843 item_len, 0, FRAME_COLS (f) - 1, -1);
21844 width -= item_len;
21845 /* Indicate with " >" that there's a submenu. */
21846 display_string (" >", Qnil, Qnil, 0, 0, &it, width, 0,
21847 FRAME_COLS (f) - 1, -1);
21849 else
21850 display_string (item_text, Qnil, Qnil, 0, 0, &it,
21851 width, 0, FRAME_COLS (f) - 1, -1);
21853 row->used[TEXT_AREA] = max (saved_used, row->used[TEXT_AREA]);
21854 row->truncated_on_right_p = saved_truncated;
21855 row->hash = row_hash (row);
21856 row->full_width_p = saved_width;
21857 row->reversed_p = saved_reversed;
21860 /***********************************************************************
21861 Mode Line
21862 ***********************************************************************/
21864 /* Redisplay mode lines in the window tree whose root is WINDOW. If
21865 FORCE is non-zero, redisplay mode lines unconditionally.
21866 Otherwise, redisplay only mode lines that are garbaged. Value is
21867 the number of windows whose mode lines were redisplayed. */
21869 static int
21870 redisplay_mode_lines (Lisp_Object window, bool force)
21872 int nwindows = 0;
21874 while (!NILP (window))
21876 struct window *w = XWINDOW (window);
21878 if (WINDOWP (w->contents))
21879 nwindows += redisplay_mode_lines (w->contents, force);
21880 else if (force
21881 || FRAME_GARBAGED_P (XFRAME (w->frame))
21882 || !MATRIX_MODE_LINE_ROW (w->current_matrix)->enabled_p)
21884 struct text_pos lpoint;
21885 struct buffer *old = current_buffer;
21887 /* Set the window's buffer for the mode line display. */
21888 SET_TEXT_POS (lpoint, PT, PT_BYTE);
21889 set_buffer_internal_1 (XBUFFER (w->contents));
21891 /* Point refers normally to the selected window. For any
21892 other window, set up appropriate value. */
21893 if (!EQ (window, selected_window))
21895 struct text_pos pt;
21897 CLIP_TEXT_POS_FROM_MARKER (pt, w->pointm);
21898 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
21901 /* Display mode lines. */
21902 clear_glyph_matrix (w->desired_matrix);
21903 if (display_mode_lines (w))
21904 ++nwindows;
21906 /* Restore old settings. */
21907 set_buffer_internal_1 (old);
21908 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
21911 window = w->next;
21914 return nwindows;
21918 /* Display the mode and/or header line of window W. Value is the
21919 sum number of mode lines and header lines displayed. */
21921 static int
21922 display_mode_lines (struct window *w)
21924 Lisp_Object old_selected_window = selected_window;
21925 Lisp_Object old_selected_frame = selected_frame;
21926 Lisp_Object new_frame = w->frame;
21927 Lisp_Object old_frame_selected_window = XFRAME (new_frame)->selected_window;
21928 int n = 0;
21930 selected_frame = new_frame;
21931 /* FIXME: If we were to allow the mode-line's computation changing the buffer
21932 or window's point, then we'd need select_window_1 here as well. */
21933 XSETWINDOW (selected_window, w);
21934 XFRAME (new_frame)->selected_window = selected_window;
21936 /* These will be set while the mode line specs are processed. */
21937 line_number_displayed = 0;
21938 w->column_number_displayed = -1;
21940 if (WINDOW_WANTS_MODELINE_P (w))
21942 struct window *sel_w = XWINDOW (old_selected_window);
21944 /* Select mode line face based on the real selected window. */
21945 display_mode_line (w, CURRENT_MODE_LINE_FACE_ID_3 (sel_w, sel_w, w),
21946 BVAR (current_buffer, mode_line_format));
21947 ++n;
21950 if (WINDOW_WANTS_HEADER_LINE_P (w))
21952 display_mode_line (w, HEADER_LINE_FACE_ID,
21953 BVAR (current_buffer, header_line_format));
21954 ++n;
21957 XFRAME (new_frame)->selected_window = old_frame_selected_window;
21958 selected_frame = old_selected_frame;
21959 selected_window = old_selected_window;
21960 if (n > 0)
21961 w->must_be_updated_p = true;
21962 return n;
21966 /* Display mode or header line of window W. FACE_ID specifies which
21967 line to display; it is either MODE_LINE_FACE_ID or
21968 HEADER_LINE_FACE_ID. FORMAT is the mode/header line format to
21969 display. Value is the pixel height of the mode/header line
21970 displayed. */
21972 static int
21973 display_mode_line (struct window *w, enum face_id face_id, Lisp_Object format)
21975 struct it it;
21976 struct face *face;
21977 ptrdiff_t count = SPECPDL_INDEX ();
21979 init_iterator (&it, w, -1, -1, NULL, face_id);
21980 /* Don't extend on a previously drawn mode-line.
21981 This may happen if called from pos_visible_p. */
21982 it.glyph_row->enabled_p = false;
21983 prepare_desired_row (w, it.glyph_row, true);
21985 it.glyph_row->mode_line_p = 1;
21987 /* FIXME: This should be controlled by a user option. But
21988 supporting such an option is not trivial, since the mode line is
21989 made up of many separate strings. */
21990 it.paragraph_embedding = L2R;
21992 record_unwind_protect (unwind_format_mode_line,
21993 format_mode_line_unwind_data (NULL, NULL, Qnil, 0));
21995 mode_line_target = MODE_LINE_DISPLAY;
21997 /* Temporarily make frame's keyboard the current kboard so that
21998 kboard-local variables in the mode_line_format will get the right
21999 values. */
22000 push_kboard (FRAME_KBOARD (it.f));
22001 record_unwind_save_match_data ();
22002 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
22003 pop_kboard ();
22005 unbind_to (count, Qnil);
22007 /* Fill up with spaces. */
22008 display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);
22010 compute_line_metrics (&it);
22011 it.glyph_row->full_width_p = 1;
22012 it.glyph_row->continued_p = 0;
22013 it.glyph_row->truncated_on_left_p = 0;
22014 it.glyph_row->truncated_on_right_p = 0;
22016 /* Make a 3D mode-line have a shadow at its right end. */
22017 face = FACE_FROM_ID (it.f, face_id);
22018 extend_face_to_end_of_line (&it);
22019 if (face->box != FACE_NO_BOX)
22021 struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
22022 + it.glyph_row->used[TEXT_AREA] - 1);
22023 last->right_box_line_p = 1;
22026 return it.glyph_row->height;
22029 /* Move element ELT in LIST to the front of LIST.
22030 Return the updated list. */
22032 static Lisp_Object
22033 move_elt_to_front (Lisp_Object elt, Lisp_Object list)
22035 register Lisp_Object tail, prev;
22036 register Lisp_Object tem;
22038 tail = list;
22039 prev = Qnil;
22040 while (CONSP (tail))
22042 tem = XCAR (tail);
22044 if (EQ (elt, tem))
22046 /* Splice out the link TAIL. */
22047 if (NILP (prev))
22048 list = XCDR (tail);
22049 else
22050 Fsetcdr (prev, XCDR (tail));
22052 /* Now make it the first. */
22053 Fsetcdr (tail, list);
22054 return tail;
22056 else
22057 prev = tail;
22058 tail = XCDR (tail);
22059 QUIT;
22062 /* Not found--return unchanged LIST. */
22063 return list;
22066 /* Contribute ELT to the mode line for window IT->w. How it
22067 translates into text depends on its data type.
22069 IT describes the display environment in which we display, as usual.
22071 DEPTH is the depth in recursion. It is used to prevent
22072 infinite recursion here.
22074 FIELD_WIDTH is the number of characters the display of ELT should
22075 occupy in the mode line, and PRECISION is the maximum number of
22076 characters to display from ELT's representation. See
22077 display_string for details.
22079 Returns the hpos of the end of the text generated by ELT.
22081 PROPS is a property list to add to any string we encounter.
22083 If RISKY is nonzero, remove (disregard) any properties in any string
22084 we encounter, and ignore :eval and :propertize.
22086 The global variable `mode_line_target' determines whether the
22087 output is passed to `store_mode_line_noprop',
22088 `store_mode_line_string', or `display_string'. */
22090 static int
22091 display_mode_element (struct it *it, int depth, int field_width, int precision,
22092 Lisp_Object elt, Lisp_Object props, int risky)
22094 int n = 0, field, prec;
22095 int literal = 0;
22097 tail_recurse:
22098 if (depth > 100)
22099 elt = build_string ("*too-deep*");
22101 depth++;
22103 switch (XTYPE (elt))
22105 case Lisp_String:
22107 /* A string: output it and check for %-constructs within it. */
22108 unsigned char c;
22109 ptrdiff_t offset = 0;
22111 if (SCHARS (elt) > 0
22112 && (!NILP (props) || risky))
22114 Lisp_Object oprops, aelt;
22115 oprops = Ftext_properties_at (make_number (0), elt);
22117 /* If the starting string's properties are not what
22118 we want, translate the string. Also, if the string
22119 is risky, do that anyway. */
22121 if (NILP (Fequal (props, oprops)) || risky)
22123 /* If the starting string has properties,
22124 merge the specified ones onto the existing ones. */
22125 if (! NILP (oprops) && !risky)
22127 Lisp_Object tem;
22129 oprops = Fcopy_sequence (oprops);
22130 tem = props;
22131 while (CONSP (tem))
22133 oprops = Fplist_put (oprops, XCAR (tem),
22134 XCAR (XCDR (tem)));
22135 tem = XCDR (XCDR (tem));
22137 props = oprops;
22140 aelt = Fassoc (elt, mode_line_proptrans_alist);
22141 if (! NILP (aelt) && !NILP (Fequal (props, XCDR (aelt))))
22143 /* AELT is what we want. Move it to the front
22144 without consing. */
22145 elt = XCAR (aelt);
22146 mode_line_proptrans_alist
22147 = move_elt_to_front (aelt, mode_line_proptrans_alist);
22149 else
22151 Lisp_Object tem;
22153 /* If AELT has the wrong props, it is useless.
22154 so get rid of it. */
22155 if (! NILP (aelt))
22156 mode_line_proptrans_alist
22157 = Fdelq (aelt, mode_line_proptrans_alist);
22159 elt = Fcopy_sequence (elt);
22160 Fset_text_properties (make_number (0), Flength (elt),
22161 props, elt);
22162 /* Add this item to mode_line_proptrans_alist. */
22163 mode_line_proptrans_alist
22164 = Fcons (Fcons (elt, props),
22165 mode_line_proptrans_alist);
22166 /* Truncate mode_line_proptrans_alist
22167 to at most 50 elements. */
22168 tem = Fnthcdr (make_number (50),
22169 mode_line_proptrans_alist);
22170 if (! NILP (tem))
22171 XSETCDR (tem, Qnil);
22176 offset = 0;
22178 if (literal)
22180 prec = precision - n;
22181 switch (mode_line_target)
22183 case MODE_LINE_NOPROP:
22184 case MODE_LINE_TITLE:
22185 n += store_mode_line_noprop (SSDATA (elt), -1, prec);
22186 break;
22187 case MODE_LINE_STRING:
22188 n += store_mode_line_string (NULL, elt, 1, 0, prec, Qnil);
22189 break;
22190 case MODE_LINE_DISPLAY:
22191 n += display_string (NULL, elt, Qnil, 0, 0, it,
22192 0, prec, 0, STRING_MULTIBYTE (elt));
22193 break;
22196 break;
22199 /* Handle the non-literal case. */
22201 while ((precision <= 0 || n < precision)
22202 && SREF (elt, offset) != 0
22203 && (mode_line_target != MODE_LINE_DISPLAY
22204 || it->current_x < it->last_visible_x))
22206 ptrdiff_t last_offset = offset;
22208 /* Advance to end of string or next format specifier. */
22209 while ((c = SREF (elt, offset++)) != '\0' && c != '%')
22212 if (offset - 1 != last_offset)
22214 ptrdiff_t nchars, nbytes;
22216 /* Output to end of string or up to '%'. Field width
22217 is length of string. Don't output more than
22218 PRECISION allows us. */
22219 offset--;
22221 prec = c_string_width (SDATA (elt) + last_offset,
22222 offset - last_offset, precision - n,
22223 &nchars, &nbytes);
22225 switch (mode_line_target)
22227 case MODE_LINE_NOPROP:
22228 case MODE_LINE_TITLE:
22229 n += store_mode_line_noprop (SSDATA (elt) + last_offset, 0, prec);
22230 break;
22231 case MODE_LINE_STRING:
22233 ptrdiff_t bytepos = last_offset;
22234 ptrdiff_t charpos = string_byte_to_char (elt, bytepos);
22235 ptrdiff_t endpos = (precision <= 0
22236 ? string_byte_to_char (elt, offset)
22237 : charpos + nchars);
22239 n += store_mode_line_string (NULL,
22240 Fsubstring (elt, make_number (charpos),
22241 make_number (endpos)),
22242 0, 0, 0, Qnil);
22244 break;
22245 case MODE_LINE_DISPLAY:
22247 ptrdiff_t bytepos = last_offset;
22248 ptrdiff_t charpos = string_byte_to_char (elt, bytepos);
22250 if (precision <= 0)
22251 nchars = string_byte_to_char (elt, offset) - charpos;
22252 n += display_string (NULL, elt, Qnil, 0, charpos,
22253 it, 0, nchars, 0,
22254 STRING_MULTIBYTE (elt));
22256 break;
22259 else /* c == '%' */
22261 ptrdiff_t percent_position = offset;
22263 /* Get the specified minimum width. Zero means
22264 don't pad. */
22265 field = 0;
22266 while ((c = SREF (elt, offset++)) >= '0' && c <= '9')
22267 field = field * 10 + c - '0';
22269 /* Don't pad beyond the total padding allowed. */
22270 if (field_width - n > 0 && field > field_width - n)
22271 field = field_width - n;
22273 /* Note that either PRECISION <= 0 or N < PRECISION. */
22274 prec = precision - n;
22276 if (c == 'M')
22277 n += display_mode_element (it, depth, field, prec,
22278 Vglobal_mode_string, props,
22279 risky);
22280 else if (c != 0)
22282 bool multibyte;
22283 ptrdiff_t bytepos, charpos;
22284 const char *spec;
22285 Lisp_Object string;
22287 bytepos = percent_position;
22288 charpos = (STRING_MULTIBYTE (elt)
22289 ? string_byte_to_char (elt, bytepos)
22290 : bytepos);
22291 spec = decode_mode_spec (it->w, c, field, &string);
22292 multibyte = STRINGP (string) && STRING_MULTIBYTE (string);
22294 switch (mode_line_target)
22296 case MODE_LINE_NOPROP:
22297 case MODE_LINE_TITLE:
22298 n += store_mode_line_noprop (spec, field, prec);
22299 break;
22300 case MODE_LINE_STRING:
22302 Lisp_Object tem = build_string (spec);
22303 props = Ftext_properties_at (make_number (charpos), elt);
22304 /* Should only keep face property in props */
22305 n += store_mode_line_string (NULL, tem, 0, field, prec, props);
22307 break;
22308 case MODE_LINE_DISPLAY:
22310 int nglyphs_before, nwritten;
22312 nglyphs_before = it->glyph_row->used[TEXT_AREA];
22313 nwritten = display_string (spec, string, elt,
22314 charpos, 0, it,
22315 field, prec, 0,
22316 multibyte);
22318 /* Assign to the glyphs written above the
22319 string where the `%x' came from, position
22320 of the `%'. */
22321 if (nwritten > 0)
22323 struct glyph *glyph
22324 = (it->glyph_row->glyphs[TEXT_AREA]
22325 + nglyphs_before);
22326 int i;
22328 for (i = 0; i < nwritten; ++i)
22330 glyph[i].object = elt;
22331 glyph[i].charpos = charpos;
22334 n += nwritten;
22337 break;
22340 else /* c == 0 */
22341 break;
22345 break;
22347 case Lisp_Symbol:
22348 /* A symbol: process the value of the symbol recursively
22349 as if it appeared here directly. Avoid error if symbol void.
22350 Special case: if value of symbol is a string, output the string
22351 literally. */
22353 register Lisp_Object tem;
22355 /* If the variable is not marked as risky to set
22356 then its contents are risky to use. */
22357 if (NILP (Fget (elt, Qrisky_local_variable)))
22358 risky = 1;
22360 tem = Fboundp (elt);
22361 if (!NILP (tem))
22363 tem = Fsymbol_value (elt);
22364 /* If value is a string, output that string literally:
22365 don't check for % within it. */
22366 if (STRINGP (tem))
22367 literal = 1;
22369 if (!EQ (tem, elt))
22371 /* Give up right away for nil or t. */
22372 elt = tem;
22373 goto tail_recurse;
22377 break;
22379 case Lisp_Cons:
22381 register Lisp_Object car, tem;
22383 /* A cons cell: five distinct cases.
22384 If first element is :eval or :propertize, do something special.
22385 If first element is a string or a cons, process all the elements
22386 and effectively concatenate them.
22387 If first element is a negative number, truncate displaying cdr to
22388 at most that many characters. If positive, pad (with spaces)
22389 to at least that many characters.
22390 If first element is a symbol, process the cadr or caddr recursively
22391 according to whether the symbol's value is non-nil or nil. */
22392 car = XCAR (elt);
22393 if (EQ (car, QCeval))
22395 /* An element of the form (:eval FORM) means evaluate FORM
22396 and use the result as mode line elements. */
22398 if (risky)
22399 break;
22401 if (CONSP (XCDR (elt)))
22403 Lisp_Object spec;
22404 spec = safe__eval (true, XCAR (XCDR (elt)));
22405 n += display_mode_element (it, depth, field_width - n,
22406 precision - n, spec, props,
22407 risky);
22410 else if (EQ (car, QCpropertize))
22412 /* An element of the form (:propertize ELT PROPS...)
22413 means display ELT but applying properties PROPS. */
22415 if (risky)
22416 break;
22418 if (CONSP (XCDR (elt)))
22419 n += display_mode_element (it, depth, field_width - n,
22420 precision - n, XCAR (XCDR (elt)),
22421 XCDR (XCDR (elt)), risky);
22423 else if (SYMBOLP (car))
22425 tem = Fboundp (car);
22426 elt = XCDR (elt);
22427 if (!CONSP (elt))
22428 goto invalid;
22429 /* elt is now the cdr, and we know it is a cons cell.
22430 Use its car if CAR has a non-nil value. */
22431 if (!NILP (tem))
22433 tem = Fsymbol_value (car);
22434 if (!NILP (tem))
22436 elt = XCAR (elt);
22437 goto tail_recurse;
22440 /* Symbol's value is nil (or symbol is unbound)
22441 Get the cddr of the original list
22442 and if possible find the caddr and use that. */
22443 elt = XCDR (elt);
22444 if (NILP (elt))
22445 break;
22446 else if (!CONSP (elt))
22447 goto invalid;
22448 elt = XCAR (elt);
22449 goto tail_recurse;
22451 else if (INTEGERP (car))
22453 register int lim = XINT (car);
22454 elt = XCDR (elt);
22455 if (lim < 0)
22457 /* Negative int means reduce maximum width. */
22458 if (precision <= 0)
22459 precision = -lim;
22460 else
22461 precision = min (precision, -lim);
22463 else if (lim > 0)
22465 /* Padding specified. Don't let it be more than
22466 current maximum. */
22467 if (precision > 0)
22468 lim = min (precision, lim);
22470 /* If that's more padding than already wanted, queue it.
22471 But don't reduce padding already specified even if
22472 that is beyond the current truncation point. */
22473 field_width = max (lim, field_width);
22475 goto tail_recurse;
22477 else if (STRINGP (car) || CONSP (car))
22479 Lisp_Object halftail = elt;
22480 int len = 0;
22482 while (CONSP (elt)
22483 && (precision <= 0 || n < precision))
22485 n += display_mode_element (it, depth,
22486 /* Do padding only after the last
22487 element in the list. */
22488 (! CONSP (XCDR (elt))
22489 ? field_width - n
22490 : 0),
22491 precision - n, XCAR (elt),
22492 props, risky);
22493 elt = XCDR (elt);
22494 len++;
22495 if ((len & 1) == 0)
22496 halftail = XCDR (halftail);
22497 /* Check for cycle. */
22498 if (EQ (halftail, elt))
22499 break;
22503 break;
22505 default:
22506 invalid:
22507 elt = build_string ("*invalid*");
22508 goto tail_recurse;
22511 /* Pad to FIELD_WIDTH. */
22512 if (field_width > 0 && n < field_width)
22514 switch (mode_line_target)
22516 case MODE_LINE_NOPROP:
22517 case MODE_LINE_TITLE:
22518 n += store_mode_line_noprop ("", field_width - n, 0);
22519 break;
22520 case MODE_LINE_STRING:
22521 n += store_mode_line_string ("", Qnil, 0, field_width - n, 0, Qnil);
22522 break;
22523 case MODE_LINE_DISPLAY:
22524 n += display_string ("", Qnil, Qnil, 0, 0, it, field_width - n,
22525 0, 0, 0);
22526 break;
22530 return n;
22533 /* Store a mode-line string element in mode_line_string_list.
22535 If STRING is non-null, display that C string. Otherwise, the Lisp
22536 string LISP_STRING is displayed.
22538 FIELD_WIDTH is the minimum number of output glyphs to produce.
22539 If STRING has fewer characters than FIELD_WIDTH, pad to the right
22540 with spaces. FIELD_WIDTH <= 0 means don't pad.
22542 PRECISION is the maximum number of characters to output from
22543 STRING. PRECISION <= 0 means don't truncate the string.
22545 If COPY_STRING is non-zero, make a copy of LISP_STRING before adding
22546 properties to the string.
22548 PROPS are the properties to add to the string.
22549 The mode_line_string_face face property is always added to the string.
22552 static int
22553 store_mode_line_string (const char *string, Lisp_Object lisp_string, int copy_string,
22554 int field_width, int precision, Lisp_Object props)
22556 ptrdiff_t len;
22557 int n = 0;
22559 if (string != NULL)
22561 len = strlen (string);
22562 if (precision > 0 && len > precision)
22563 len = precision;
22564 lisp_string = make_string (string, len);
22565 if (NILP (props))
22566 props = mode_line_string_face_prop;
22567 else if (!NILP (mode_line_string_face))
22569 Lisp_Object face = Fplist_get (props, Qface);
22570 props = Fcopy_sequence (props);
22571 if (NILP (face))
22572 face = mode_line_string_face;
22573 else
22574 face = list2 (face, mode_line_string_face);
22575 props = Fplist_put (props, Qface, face);
22577 Fadd_text_properties (make_number (0), make_number (len),
22578 props, lisp_string);
22580 else
22582 len = XFASTINT (Flength (lisp_string));
22583 if (precision > 0 && len > precision)
22585 len = precision;
22586 lisp_string = Fsubstring (lisp_string, make_number (0), make_number (len));
22587 precision = -1;
22589 if (!NILP (mode_line_string_face))
22591 Lisp_Object face;
22592 if (NILP (props))
22593 props = Ftext_properties_at (make_number (0), lisp_string);
22594 face = Fplist_get (props, Qface);
22595 if (NILP (face))
22596 face = mode_line_string_face;
22597 else
22598 face = list2 (face, mode_line_string_face);
22599 props = list2 (Qface, face);
22600 if (copy_string)
22601 lisp_string = Fcopy_sequence (lisp_string);
22603 if (!NILP (props))
22604 Fadd_text_properties (make_number (0), make_number (len),
22605 props, lisp_string);
22608 if (len > 0)
22610 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
22611 n += len;
22614 if (field_width > len)
22616 field_width -= len;
22617 lisp_string = Fmake_string (make_number (field_width), make_number (' '));
22618 if (!NILP (props))
22619 Fadd_text_properties (make_number (0), make_number (field_width),
22620 props, lisp_string);
22621 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
22622 n += field_width;
22625 return n;
22629 DEFUN ("format-mode-line", Fformat_mode_line, Sformat_mode_line,
22630 1, 4, 0,
22631 doc: /* Format a string out of a mode line format specification.
22632 First arg FORMAT specifies the mode line format (see `mode-line-format'
22633 for details) to use.
22635 By default, the format is evaluated for the currently selected window.
22637 Optional second arg FACE specifies the face property to put on all
22638 characters for which no face is specified. The value nil means the
22639 default face. The value t means whatever face the window's mode line
22640 currently uses (either `mode-line' or `mode-line-inactive',
22641 depending on whether the window is the selected window or not).
22642 An integer value means the value string has no text
22643 properties.
22645 Optional third and fourth args WINDOW and BUFFER specify the window
22646 and buffer to use as the context for the formatting (defaults
22647 are the selected window and the WINDOW's buffer). */)
22648 (Lisp_Object format, Lisp_Object face,
22649 Lisp_Object window, Lisp_Object buffer)
22651 struct it it;
22652 int len;
22653 struct window *w;
22654 struct buffer *old_buffer = NULL;
22655 int face_id;
22656 int no_props = INTEGERP (face);
22657 ptrdiff_t count = SPECPDL_INDEX ();
22658 Lisp_Object str;
22659 int string_start = 0;
22661 w = decode_any_window (window);
22662 XSETWINDOW (window, w);
22664 if (NILP (buffer))
22665 buffer = w->contents;
22666 CHECK_BUFFER (buffer);
22668 /* Make formatting the modeline a non-op when noninteractive, otherwise
22669 there will be problems later caused by a partially initialized frame. */
22670 if (NILP (format) || noninteractive)
22671 return empty_unibyte_string;
22673 if (no_props)
22674 face = Qnil;
22676 face_id = (NILP (face) || EQ (face, Qdefault)) ? DEFAULT_FACE_ID
22677 : EQ (face, Qt) ? (EQ (window, selected_window)
22678 ? MODE_LINE_FACE_ID : MODE_LINE_INACTIVE_FACE_ID)
22679 : EQ (face, Qmode_line) ? MODE_LINE_FACE_ID
22680 : EQ (face, Qmode_line_inactive) ? MODE_LINE_INACTIVE_FACE_ID
22681 : EQ (face, Qheader_line) ? HEADER_LINE_FACE_ID
22682 : EQ (face, Qtool_bar) ? TOOL_BAR_FACE_ID
22683 : DEFAULT_FACE_ID;
22685 old_buffer = current_buffer;
22687 /* Save things including mode_line_proptrans_alist,
22688 and set that to nil so that we don't alter the outer value. */
22689 record_unwind_protect (unwind_format_mode_line,
22690 format_mode_line_unwind_data
22691 (XFRAME (WINDOW_FRAME (w)),
22692 old_buffer, selected_window, 1));
22693 mode_line_proptrans_alist = Qnil;
22695 Fselect_window (window, Qt);
22696 set_buffer_internal_1 (XBUFFER (buffer));
22698 init_iterator (&it, w, -1, -1, NULL, face_id);
22700 if (no_props)
22702 mode_line_target = MODE_LINE_NOPROP;
22703 mode_line_string_face_prop = Qnil;
22704 mode_line_string_list = Qnil;
22705 string_start = MODE_LINE_NOPROP_LEN (0);
22707 else
22709 mode_line_target = MODE_LINE_STRING;
22710 mode_line_string_list = Qnil;
22711 mode_line_string_face = face;
22712 mode_line_string_face_prop
22713 = NILP (face) ? Qnil : list2 (Qface, face);
22716 push_kboard (FRAME_KBOARD (it.f));
22717 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
22718 pop_kboard ();
22720 if (no_props)
22722 len = MODE_LINE_NOPROP_LEN (string_start);
22723 str = make_string (mode_line_noprop_buf + string_start, len);
22725 else
22727 mode_line_string_list = Fnreverse (mode_line_string_list);
22728 str = Fmapconcat (intern ("identity"), mode_line_string_list,
22729 empty_unibyte_string);
22732 unbind_to (count, Qnil);
22733 return str;
22736 /* Write a null-terminated, right justified decimal representation of
22737 the positive integer D to BUF using a minimal field width WIDTH. */
22739 static void
22740 pint2str (register char *buf, register int width, register ptrdiff_t d)
22742 register char *p = buf;
22744 if (d <= 0)
22745 *p++ = '0';
22746 else
22748 while (d > 0)
22750 *p++ = d % 10 + '0';
22751 d /= 10;
22755 for (width -= (int) (p - buf); width > 0; --width)
22756 *p++ = ' ';
22757 *p-- = '\0';
22758 while (p > buf)
22760 d = *buf;
22761 *buf++ = *p;
22762 *p-- = d;
22766 /* Write a null-terminated, right justified decimal and "human
22767 readable" representation of the nonnegative integer D to BUF using
22768 a minimal field width WIDTH. D should be smaller than 999.5e24. */
22770 static const char power_letter[] =
22772 0, /* no letter */
22773 'k', /* kilo */
22774 'M', /* mega */
22775 'G', /* giga */
22776 'T', /* tera */
22777 'P', /* peta */
22778 'E', /* exa */
22779 'Z', /* zetta */
22780 'Y' /* yotta */
22783 static void
22784 pint2hrstr (char *buf, int width, ptrdiff_t d)
22786 /* We aim to represent the nonnegative integer D as
22787 QUOTIENT.TENTHS * 10 ^ (3 * EXPONENT). */
22788 ptrdiff_t quotient = d;
22789 int remainder = 0;
22790 /* -1 means: do not use TENTHS. */
22791 int tenths = -1;
22792 int exponent = 0;
22794 /* Length of QUOTIENT.TENTHS as a string. */
22795 int length;
22797 char * psuffix;
22798 char * p;
22800 if (quotient >= 1000)
22802 /* Scale to the appropriate EXPONENT. */
22805 remainder = quotient % 1000;
22806 quotient /= 1000;
22807 exponent++;
22809 while (quotient >= 1000);
22811 /* Round to nearest and decide whether to use TENTHS or not. */
22812 if (quotient <= 9)
22814 tenths = remainder / 100;
22815 if (remainder % 100 >= 50)
22817 if (tenths < 9)
22818 tenths++;
22819 else
22821 quotient++;
22822 if (quotient == 10)
22823 tenths = -1;
22824 else
22825 tenths = 0;
22829 else
22830 if (remainder >= 500)
22832 if (quotient < 999)
22833 quotient++;
22834 else
22836 quotient = 1;
22837 exponent++;
22838 tenths = 0;
22843 /* Calculate the LENGTH of QUOTIENT.TENTHS as a string. */
22844 if (tenths == -1 && quotient <= 99)
22845 if (quotient <= 9)
22846 length = 1;
22847 else
22848 length = 2;
22849 else
22850 length = 3;
22851 p = psuffix = buf + max (width, length);
22853 /* Print EXPONENT. */
22854 *psuffix++ = power_letter[exponent];
22855 *psuffix = '\0';
22857 /* Print TENTHS. */
22858 if (tenths >= 0)
22860 *--p = '0' + tenths;
22861 *--p = '.';
22864 /* Print QUOTIENT. */
22867 int digit = quotient % 10;
22868 *--p = '0' + digit;
22870 while ((quotient /= 10) != 0);
22872 /* Print leading spaces. */
22873 while (buf < p)
22874 *--p = ' ';
22877 /* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
22878 If EOL_FLAG is 1, set also a mnemonic character for end-of-line
22879 type of CODING_SYSTEM. Return updated pointer into BUF. */
22881 static unsigned char invalid_eol_type[] = "(*invalid*)";
22883 static char *
22884 decode_mode_spec_coding (Lisp_Object coding_system, register char *buf, int eol_flag)
22886 Lisp_Object val;
22887 bool multibyte = !NILP (BVAR (current_buffer, enable_multibyte_characters));
22888 const unsigned char *eol_str;
22889 int eol_str_len;
22890 /* The EOL conversion we are using. */
22891 Lisp_Object eoltype;
22893 val = CODING_SYSTEM_SPEC (coding_system);
22894 eoltype = Qnil;
22896 if (!VECTORP (val)) /* Not yet decided. */
22898 *buf++ = multibyte ? '-' : ' ';
22899 if (eol_flag)
22900 eoltype = eol_mnemonic_undecided;
22901 /* Don't mention EOL conversion if it isn't decided. */
22903 else
22905 Lisp_Object attrs;
22906 Lisp_Object eolvalue;
22908 attrs = AREF (val, 0);
22909 eolvalue = AREF (val, 2);
22911 *buf++ = multibyte
22912 ? XFASTINT (CODING_ATTR_MNEMONIC (attrs))
22913 : ' ';
22915 if (eol_flag)
22917 /* The EOL conversion that is normal on this system. */
22919 if (NILP (eolvalue)) /* Not yet decided. */
22920 eoltype = eol_mnemonic_undecided;
22921 else if (VECTORP (eolvalue)) /* Not yet decided. */
22922 eoltype = eol_mnemonic_undecided;
22923 else /* eolvalue is Qunix, Qdos, or Qmac. */
22924 eoltype = (EQ (eolvalue, Qunix)
22925 ? eol_mnemonic_unix
22926 : (EQ (eolvalue, Qdos) == 1
22927 ? eol_mnemonic_dos : eol_mnemonic_mac));
22931 if (eol_flag)
22933 /* Mention the EOL conversion if it is not the usual one. */
22934 if (STRINGP (eoltype))
22936 eol_str = SDATA (eoltype);
22937 eol_str_len = SBYTES (eoltype);
22939 else if (CHARACTERP (eoltype))
22941 int c = XFASTINT (eoltype);
22942 return buf + CHAR_STRING (c, (unsigned char *) buf);
22944 else
22946 eol_str = invalid_eol_type;
22947 eol_str_len = sizeof (invalid_eol_type) - 1;
22949 memcpy (buf, eol_str, eol_str_len);
22950 buf += eol_str_len;
22953 return buf;
22956 /* Return a string for the output of a mode line %-spec for window W,
22957 generated by character C. FIELD_WIDTH > 0 means pad the string
22958 returned with spaces to that value. Return a Lisp string in
22959 *STRING if the resulting string is taken from that Lisp string.
22961 Note we operate on the current buffer for most purposes. */
22963 static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
22965 static const char *
22966 decode_mode_spec (struct window *w, register int c, int field_width,
22967 Lisp_Object *string)
22969 Lisp_Object obj;
22970 struct frame *f = XFRAME (WINDOW_FRAME (w));
22971 char *decode_mode_spec_buf = f->decode_mode_spec_buffer;
22972 /* We are going to use f->decode_mode_spec_buffer as the buffer to
22973 produce strings from numerical values, so limit preposterously
22974 large values of FIELD_WIDTH to avoid overrunning the buffer's
22975 end. The size of the buffer is enough for FRAME_MESSAGE_BUF_SIZE
22976 bytes plus the terminating null. */
22977 int width = min (field_width, FRAME_MESSAGE_BUF_SIZE (f));
22978 struct buffer *b = current_buffer;
22980 obj = Qnil;
22981 *string = Qnil;
22983 switch (c)
22985 case '*':
22986 if (!NILP (BVAR (b, read_only)))
22987 return "%";
22988 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
22989 return "*";
22990 return "-";
22992 case '+':
22993 /* This differs from %* only for a modified read-only buffer. */
22994 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
22995 return "*";
22996 if (!NILP (BVAR (b, read_only)))
22997 return "%";
22998 return "-";
23000 case '&':
23001 /* This differs from %* in ignoring read-only-ness. */
23002 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
23003 return "*";
23004 return "-";
23006 case '%':
23007 return "%";
23009 case '[':
23011 int i;
23012 char *p;
23014 if (command_loop_level > 5)
23015 return "[[[... ";
23016 p = decode_mode_spec_buf;
23017 for (i = 0; i < command_loop_level; i++)
23018 *p++ = '[';
23019 *p = 0;
23020 return decode_mode_spec_buf;
23023 case ']':
23025 int i;
23026 char *p;
23028 if (command_loop_level > 5)
23029 return " ...]]]";
23030 p = decode_mode_spec_buf;
23031 for (i = 0; i < command_loop_level; i++)
23032 *p++ = ']';
23033 *p = 0;
23034 return decode_mode_spec_buf;
23037 case '-':
23039 register int i;
23041 /* Let lots_of_dashes be a string of infinite length. */
23042 if (mode_line_target == MODE_LINE_NOPROP
23043 || mode_line_target == MODE_LINE_STRING)
23044 return "--";
23045 if (field_width <= 0
23046 || field_width > sizeof (lots_of_dashes))
23048 for (i = 0; i < FRAME_MESSAGE_BUF_SIZE (f) - 1; ++i)
23049 decode_mode_spec_buf[i] = '-';
23050 decode_mode_spec_buf[i] = '\0';
23051 return decode_mode_spec_buf;
23053 else
23054 return lots_of_dashes;
23057 case 'b':
23058 obj = BVAR (b, name);
23059 break;
23061 case 'c':
23062 /* %c and %l are ignored in `frame-title-format'.
23063 (In redisplay_internal, the frame title is drawn _before_ the
23064 windows are updated, so the stuff which depends on actual
23065 window contents (such as %l) may fail to render properly, or
23066 even crash emacs.) */
23067 if (mode_line_target == MODE_LINE_TITLE)
23068 return "";
23069 else
23071 ptrdiff_t col = current_column ();
23072 w->column_number_displayed = col;
23073 pint2str (decode_mode_spec_buf, width, col);
23074 return decode_mode_spec_buf;
23077 case 'e':
23078 #if !defined SYSTEM_MALLOC && !defined HYBRID_MALLOC
23080 if (NILP (Vmemory_full))
23081 return "";
23082 else
23083 return "!MEM FULL! ";
23085 #else
23086 return "";
23087 #endif
23089 case 'F':
23090 /* %F displays the frame name. */
23091 if (!NILP (f->title))
23092 return SSDATA (f->title);
23093 if (f->explicit_name || ! FRAME_WINDOW_P (f))
23094 return SSDATA (f->name);
23095 return "Emacs";
23097 case 'f':
23098 obj = BVAR (b, filename);
23099 break;
23101 case 'i':
23103 ptrdiff_t size = ZV - BEGV;
23104 pint2str (decode_mode_spec_buf, width, size);
23105 return decode_mode_spec_buf;
23108 case 'I':
23110 ptrdiff_t size = ZV - BEGV;
23111 pint2hrstr (decode_mode_spec_buf, width, size);
23112 return decode_mode_spec_buf;
23115 case 'l':
23117 ptrdiff_t startpos, startpos_byte, line, linepos, linepos_byte;
23118 ptrdiff_t topline, nlines, height;
23119 ptrdiff_t junk;
23121 /* %c and %l are ignored in `frame-title-format'. */
23122 if (mode_line_target == MODE_LINE_TITLE)
23123 return "";
23125 startpos = marker_position (w->start);
23126 startpos_byte = marker_byte_position (w->start);
23127 height = WINDOW_TOTAL_LINES (w);
23129 /* If we decided that this buffer isn't suitable for line numbers,
23130 don't forget that too fast. */
23131 if (w->base_line_pos == -1)
23132 goto no_value;
23134 /* If the buffer is very big, don't waste time. */
23135 if (INTEGERP (Vline_number_display_limit)
23136 && BUF_ZV (b) - BUF_BEGV (b) > XINT (Vline_number_display_limit))
23138 w->base_line_pos = 0;
23139 w->base_line_number = 0;
23140 goto no_value;
23143 if (w->base_line_number > 0
23144 && w->base_line_pos > 0
23145 && w->base_line_pos <= startpos)
23147 line = w->base_line_number;
23148 linepos = w->base_line_pos;
23149 linepos_byte = buf_charpos_to_bytepos (b, linepos);
23151 else
23153 line = 1;
23154 linepos = BUF_BEGV (b);
23155 linepos_byte = BUF_BEGV_BYTE (b);
23158 /* Count lines from base line to window start position. */
23159 nlines = display_count_lines (linepos_byte,
23160 startpos_byte,
23161 startpos, &junk);
23163 topline = nlines + line;
23165 /* Determine a new base line, if the old one is too close
23166 or too far away, or if we did not have one.
23167 "Too close" means it's plausible a scroll-down would
23168 go back past it. */
23169 if (startpos == BUF_BEGV (b))
23171 w->base_line_number = topline;
23172 w->base_line_pos = BUF_BEGV (b);
23174 else if (nlines < height + 25 || nlines > height * 3 + 50
23175 || linepos == BUF_BEGV (b))
23177 ptrdiff_t limit = BUF_BEGV (b);
23178 ptrdiff_t limit_byte = BUF_BEGV_BYTE (b);
23179 ptrdiff_t position;
23180 ptrdiff_t distance =
23181 (height * 2 + 30) * line_number_display_limit_width;
23183 if (startpos - distance > limit)
23185 limit = startpos - distance;
23186 limit_byte = CHAR_TO_BYTE (limit);
23189 nlines = display_count_lines (startpos_byte,
23190 limit_byte,
23191 - (height * 2 + 30),
23192 &position);
23193 /* If we couldn't find the lines we wanted within
23194 line_number_display_limit_width chars per line,
23195 give up on line numbers for this window. */
23196 if (position == limit_byte && limit == startpos - distance)
23198 w->base_line_pos = -1;
23199 w->base_line_number = 0;
23200 goto no_value;
23203 w->base_line_number = topline - nlines;
23204 w->base_line_pos = BYTE_TO_CHAR (position);
23207 /* Now count lines from the start pos to point. */
23208 nlines = display_count_lines (startpos_byte,
23209 PT_BYTE, PT, &junk);
23211 /* Record that we did display the line number. */
23212 line_number_displayed = 1;
23214 /* Make the string to show. */
23215 pint2str (decode_mode_spec_buf, width, topline + nlines);
23216 return decode_mode_spec_buf;
23217 no_value:
23219 char *p = decode_mode_spec_buf;
23220 int pad = width - 2;
23221 while (pad-- > 0)
23222 *p++ = ' ';
23223 *p++ = '?';
23224 *p++ = '?';
23225 *p = '\0';
23226 return decode_mode_spec_buf;
23229 break;
23231 case 'm':
23232 obj = BVAR (b, mode_name);
23233 break;
23235 case 'n':
23236 if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
23237 return " Narrow";
23238 break;
23240 case 'p':
23242 ptrdiff_t pos = marker_position (w->start);
23243 ptrdiff_t total = BUF_ZV (b) - BUF_BEGV (b);
23245 if (w->window_end_pos <= BUF_Z (b) - BUF_ZV (b))
23247 if (pos <= BUF_BEGV (b))
23248 return "All";
23249 else
23250 return "Bottom";
23252 else if (pos <= BUF_BEGV (b))
23253 return "Top";
23254 else
23256 if (total > 1000000)
23257 /* Do it differently for a large value, to avoid overflow. */
23258 total = ((pos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
23259 else
23260 total = ((pos - BUF_BEGV (b)) * 100 + total - 1) / total;
23261 /* We can't normally display a 3-digit number,
23262 so get us a 2-digit number that is close. */
23263 if (total == 100)
23264 total = 99;
23265 sprintf (decode_mode_spec_buf, "%2"pD"d%%", total);
23266 return decode_mode_spec_buf;
23270 /* Display percentage of size above the bottom of the screen. */
23271 case 'P':
23273 ptrdiff_t toppos = marker_position (w->start);
23274 ptrdiff_t botpos = BUF_Z (b) - w->window_end_pos;
23275 ptrdiff_t total = BUF_ZV (b) - BUF_BEGV (b);
23277 if (botpos >= BUF_ZV (b))
23279 if (toppos <= BUF_BEGV (b))
23280 return "All";
23281 else
23282 return "Bottom";
23284 else
23286 if (total > 1000000)
23287 /* Do it differently for a large value, to avoid overflow. */
23288 total = ((botpos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
23289 else
23290 total = ((botpos - BUF_BEGV (b)) * 100 + total - 1) / total;
23291 /* We can't normally display a 3-digit number,
23292 so get us a 2-digit number that is close. */
23293 if (total == 100)
23294 total = 99;
23295 if (toppos <= BUF_BEGV (b))
23296 sprintf (decode_mode_spec_buf, "Top%2"pD"d%%", total);
23297 else
23298 sprintf (decode_mode_spec_buf, "%2"pD"d%%", total);
23299 return decode_mode_spec_buf;
23303 case 's':
23304 /* status of process */
23305 obj = Fget_buffer_process (Fcurrent_buffer ());
23306 if (NILP (obj))
23307 return "no process";
23308 #ifndef MSDOS
23309 obj = Fsymbol_name (Fprocess_status (obj));
23310 #endif
23311 break;
23313 case '@':
23315 ptrdiff_t count = inhibit_garbage_collection ();
23316 Lisp_Object curdir = BVAR (current_buffer, directory);
23317 Lisp_Object val = Qnil;
23319 if (STRINGP (curdir))
23320 val = call1 (intern ("file-remote-p"), curdir);
23322 unbind_to (count, Qnil);
23324 if (NILP (val))
23325 return "-";
23326 else
23327 return "@";
23330 case 'z':
23331 /* coding-system (not including end-of-line format) */
23332 case 'Z':
23333 /* coding-system (including end-of-line type) */
23335 int eol_flag = (c == 'Z');
23336 char *p = decode_mode_spec_buf;
23338 if (! FRAME_WINDOW_P (f))
23340 /* No need to mention EOL here--the terminal never needs
23341 to do EOL conversion. */
23342 p = decode_mode_spec_coding (CODING_ID_NAME
23343 (FRAME_KEYBOARD_CODING (f)->id),
23344 p, 0);
23345 p = decode_mode_spec_coding (CODING_ID_NAME
23346 (FRAME_TERMINAL_CODING (f)->id),
23347 p, 0);
23349 p = decode_mode_spec_coding (BVAR (b, buffer_file_coding_system),
23350 p, eol_flag);
23352 #if 0 /* This proves to be annoying; I think we can do without. -- rms. */
23353 #ifdef subprocesses
23354 obj = Fget_buffer_process (Fcurrent_buffer ());
23355 if (PROCESSP (obj))
23357 p = decode_mode_spec_coding
23358 (XPROCESS (obj)->decode_coding_system, p, eol_flag);
23359 p = decode_mode_spec_coding
23360 (XPROCESS (obj)->encode_coding_system, p, eol_flag);
23362 #endif /* subprocesses */
23363 #endif /* 0 */
23364 *p = 0;
23365 return decode_mode_spec_buf;
23369 if (STRINGP (obj))
23371 *string = obj;
23372 return SSDATA (obj);
23374 else
23375 return "";
23379 /* Count up to COUNT lines starting from START_BYTE. COUNT negative
23380 means count lines back from START_BYTE. But don't go beyond
23381 LIMIT_BYTE. Return the number of lines thus found (always
23382 nonnegative).
23384 Set *BYTE_POS_PTR to the byte position where we stopped. This is
23385 either the position COUNT lines after/before START_BYTE, if we
23386 found COUNT lines, or LIMIT_BYTE if we hit the limit before finding
23387 COUNT lines. */
23389 static ptrdiff_t
23390 display_count_lines (ptrdiff_t start_byte,
23391 ptrdiff_t limit_byte, ptrdiff_t count,
23392 ptrdiff_t *byte_pos_ptr)
23394 register unsigned char *cursor;
23395 unsigned char *base;
23397 register ptrdiff_t ceiling;
23398 register unsigned char *ceiling_addr;
23399 ptrdiff_t orig_count = count;
23401 /* If we are not in selective display mode,
23402 check only for newlines. */
23403 int selective_display = (!NILP (BVAR (current_buffer, selective_display))
23404 && !INTEGERP (BVAR (current_buffer, selective_display)));
23406 if (count > 0)
23408 while (start_byte < limit_byte)
23410 ceiling = BUFFER_CEILING_OF (start_byte);
23411 ceiling = min (limit_byte - 1, ceiling);
23412 ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
23413 base = (cursor = BYTE_POS_ADDR (start_byte));
23417 if (selective_display)
23419 while (*cursor != '\n' && *cursor != 015
23420 && ++cursor != ceiling_addr)
23421 continue;
23422 if (cursor == ceiling_addr)
23423 break;
23425 else
23427 cursor = memchr (cursor, '\n', ceiling_addr - cursor);
23428 if (! cursor)
23429 break;
23432 cursor++;
23434 if (--count == 0)
23436 start_byte += cursor - base;
23437 *byte_pos_ptr = start_byte;
23438 return orig_count;
23441 while (cursor < ceiling_addr);
23443 start_byte += ceiling_addr - base;
23446 else
23448 while (start_byte > limit_byte)
23450 ceiling = BUFFER_FLOOR_OF (start_byte - 1);
23451 ceiling = max (limit_byte, ceiling);
23452 ceiling_addr = BYTE_POS_ADDR (ceiling);
23453 base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
23454 while (1)
23456 if (selective_display)
23458 while (--cursor >= ceiling_addr
23459 && *cursor != '\n' && *cursor != 015)
23460 continue;
23461 if (cursor < ceiling_addr)
23462 break;
23464 else
23466 cursor = memrchr (ceiling_addr, '\n', cursor - ceiling_addr);
23467 if (! cursor)
23468 break;
23471 if (++count == 0)
23473 start_byte += cursor - base + 1;
23474 *byte_pos_ptr = start_byte;
23475 /* When scanning backwards, we should
23476 not count the newline posterior to which we stop. */
23477 return - orig_count - 1;
23480 start_byte += ceiling_addr - base;
23484 *byte_pos_ptr = limit_byte;
23486 if (count < 0)
23487 return - orig_count + count;
23488 return orig_count - count;
23494 /***********************************************************************
23495 Displaying strings
23496 ***********************************************************************/
23498 /* Display a NUL-terminated string, starting with index START.
23500 If STRING is non-null, display that C string. Otherwise, the Lisp
23501 string LISP_STRING is displayed. There's a case that STRING is
23502 non-null and LISP_STRING is not nil. It means STRING is a string
23503 data of LISP_STRING. In that case, we display LISP_STRING while
23504 ignoring its text properties.
23506 If FACE_STRING is not nil, FACE_STRING_POS is a position in
23507 FACE_STRING. Display STRING or LISP_STRING with the face at
23508 FACE_STRING_POS in FACE_STRING:
23510 Display the string in the environment given by IT, but use the
23511 standard display table, temporarily.
23513 FIELD_WIDTH is the minimum number of output glyphs to produce.
23514 If STRING has fewer characters than FIELD_WIDTH, pad to the right
23515 with spaces. If STRING has more characters, more than FIELD_WIDTH
23516 glyphs will be produced. FIELD_WIDTH <= 0 means don't pad.
23518 PRECISION is the maximum number of characters to output from
23519 STRING. PRECISION < 0 means don't truncate the string.
23521 This is roughly equivalent to printf format specifiers:
23523 FIELD_WIDTH PRECISION PRINTF
23524 ----------------------------------------
23525 -1 -1 %s
23526 -1 10 %.10s
23527 10 -1 %10s
23528 20 10 %20.10s
23530 MULTIBYTE zero means do not display multibyte chars, > 0 means do
23531 display them, and < 0 means obey the current buffer's value of
23532 enable_multibyte_characters.
23534 Value is the number of columns displayed. */
23536 static int
23537 display_string (const char *string, Lisp_Object lisp_string, Lisp_Object face_string,
23538 ptrdiff_t face_string_pos, ptrdiff_t start, struct it *it,
23539 int field_width, int precision, int max_x, int multibyte)
23541 int hpos_at_start = it->hpos;
23542 int saved_face_id = it->face_id;
23543 struct glyph_row *row = it->glyph_row;
23544 ptrdiff_t it_charpos;
23546 /* Initialize the iterator IT for iteration over STRING beginning
23547 with index START. */
23548 reseat_to_string (it, NILP (lisp_string) ? string : NULL, lisp_string, start,
23549 precision, field_width, multibyte);
23550 if (string && STRINGP (lisp_string))
23551 /* LISP_STRING is the one returned by decode_mode_spec. We should
23552 ignore its text properties. */
23553 it->stop_charpos = it->end_charpos;
23555 /* If displaying STRING, set up the face of the iterator from
23556 FACE_STRING, if that's given. */
23557 if (STRINGP (face_string))
23559 ptrdiff_t endptr;
23560 struct face *face;
23562 it->face_id
23563 = face_at_string_position (it->w, face_string, face_string_pos,
23564 0, &endptr, it->base_face_id, false);
23565 face = FACE_FROM_ID (it->f, it->face_id);
23566 it->face_box_p = face->box != FACE_NO_BOX;
23569 /* Set max_x to the maximum allowed X position. Don't let it go
23570 beyond the right edge of the window. */
23571 if (max_x <= 0)
23572 max_x = it->last_visible_x;
23573 else
23574 max_x = min (max_x, it->last_visible_x);
23576 /* Skip over display elements that are not visible. because IT->w is
23577 hscrolled. */
23578 if (it->current_x < it->first_visible_x)
23579 move_it_in_display_line_to (it, 100000, it->first_visible_x,
23580 MOVE_TO_POS | MOVE_TO_X);
23582 row->ascent = it->max_ascent;
23583 row->height = it->max_ascent + it->max_descent;
23584 row->phys_ascent = it->max_phys_ascent;
23585 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
23586 row->extra_line_spacing = it->max_extra_line_spacing;
23588 if (STRINGP (it->string))
23589 it_charpos = IT_STRING_CHARPOS (*it);
23590 else
23591 it_charpos = IT_CHARPOS (*it);
23593 /* This condition is for the case that we are called with current_x
23594 past last_visible_x. */
23595 while (it->current_x < max_x)
23597 int x_before, x, n_glyphs_before, i, nglyphs;
23599 /* Get the next display element. */
23600 if (!get_next_display_element (it))
23601 break;
23603 /* Produce glyphs. */
23604 x_before = it->current_x;
23605 n_glyphs_before = row->used[TEXT_AREA];
23606 PRODUCE_GLYPHS (it);
23608 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
23609 i = 0;
23610 x = x_before;
23611 while (i < nglyphs)
23613 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
23615 if (it->line_wrap != TRUNCATE
23616 && x + glyph->pixel_width > max_x)
23618 /* End of continued line or max_x reached. */
23619 if (CHAR_GLYPH_PADDING_P (*glyph))
23621 /* A wide character is unbreakable. */
23622 if (row->reversed_p)
23623 unproduce_glyphs (it, row->used[TEXT_AREA]
23624 - n_glyphs_before);
23625 row->used[TEXT_AREA] = n_glyphs_before;
23626 it->current_x = x_before;
23628 else
23630 if (row->reversed_p)
23631 unproduce_glyphs (it, row->used[TEXT_AREA]
23632 - (n_glyphs_before + i));
23633 row->used[TEXT_AREA] = n_glyphs_before + i;
23634 it->current_x = x;
23636 break;
23638 else if (x + glyph->pixel_width >= it->first_visible_x)
23640 /* Glyph is at least partially visible. */
23641 ++it->hpos;
23642 if (x < it->first_visible_x)
23643 row->x = x - it->first_visible_x;
23645 else
23647 /* Glyph is off the left margin of the display area.
23648 Should not happen. */
23649 emacs_abort ();
23652 row->ascent = max (row->ascent, it->max_ascent);
23653 row->height = max (row->height, it->max_ascent + it->max_descent);
23654 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
23655 row->phys_height = max (row->phys_height,
23656 it->max_phys_ascent + it->max_phys_descent);
23657 row->extra_line_spacing = max (row->extra_line_spacing,
23658 it->max_extra_line_spacing);
23659 x += glyph->pixel_width;
23660 ++i;
23663 /* Stop if max_x reached. */
23664 if (i < nglyphs)
23665 break;
23667 /* Stop at line ends. */
23668 if (ITERATOR_AT_END_OF_LINE_P (it))
23670 it->continuation_lines_width = 0;
23671 break;
23674 set_iterator_to_next (it, 1);
23675 if (STRINGP (it->string))
23676 it_charpos = IT_STRING_CHARPOS (*it);
23677 else
23678 it_charpos = IT_CHARPOS (*it);
23680 /* Stop if truncating at the right edge. */
23681 if (it->line_wrap == TRUNCATE
23682 && it->current_x >= it->last_visible_x)
23684 /* Add truncation mark, but don't do it if the line is
23685 truncated at a padding space. */
23686 if (it_charpos < it->string_nchars)
23688 if (!FRAME_WINDOW_P (it->f))
23690 int ii, n;
23692 if (it->current_x > it->last_visible_x)
23694 if (!row->reversed_p)
23696 for (ii = row->used[TEXT_AREA] - 1; ii > 0; --ii)
23697 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][ii]))
23698 break;
23700 else
23702 for (ii = 0; ii < row->used[TEXT_AREA]; ii++)
23703 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][ii]))
23704 break;
23705 unproduce_glyphs (it, ii + 1);
23706 ii = row->used[TEXT_AREA] - (ii + 1);
23708 for (n = row->used[TEXT_AREA]; ii < n; ++ii)
23710 row->used[TEXT_AREA] = ii;
23711 produce_special_glyphs (it, IT_TRUNCATION);
23714 produce_special_glyphs (it, IT_TRUNCATION);
23716 row->truncated_on_right_p = 1;
23718 break;
23722 /* Maybe insert a truncation at the left. */
23723 if (it->first_visible_x
23724 && it_charpos > 0)
23726 if (!FRAME_WINDOW_P (it->f)
23727 || (row->reversed_p
23728 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
23729 : WINDOW_LEFT_FRINGE_WIDTH (it->w)) == 0)
23730 insert_left_trunc_glyphs (it);
23731 row->truncated_on_left_p = 1;
23734 it->face_id = saved_face_id;
23736 /* Value is number of columns displayed. */
23737 return it->hpos - hpos_at_start;
23742 /* This is like a combination of memq and assq. Return 1/2 if PROPVAL
23743 appears as an element of LIST or as the car of an element of LIST.
23744 If PROPVAL is a list, compare each element against LIST in that
23745 way, and return 1/2 if any element of PROPVAL is found in LIST.
23746 Otherwise return 0. This function cannot quit.
23747 The return value is 2 if the text is invisible but with an ellipsis
23748 and 1 if it's invisible and without an ellipsis. */
23751 invisible_p (register Lisp_Object propval, Lisp_Object list)
23753 register Lisp_Object tail, proptail;
23755 for (tail = list; CONSP (tail); tail = XCDR (tail))
23757 register Lisp_Object tem;
23758 tem = XCAR (tail);
23759 if (EQ (propval, tem))
23760 return 1;
23761 if (CONSP (tem) && EQ (propval, XCAR (tem)))
23762 return NILP (XCDR (tem)) ? 1 : 2;
23765 if (CONSP (propval))
23767 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
23769 Lisp_Object propelt;
23770 propelt = XCAR (proptail);
23771 for (tail = list; CONSP (tail); tail = XCDR (tail))
23773 register Lisp_Object tem;
23774 tem = XCAR (tail);
23775 if (EQ (propelt, tem))
23776 return 1;
23777 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
23778 return NILP (XCDR (tem)) ? 1 : 2;
23783 return 0;
23786 DEFUN ("invisible-p", Finvisible_p, Sinvisible_p, 1, 1, 0,
23787 doc: /* Non-nil if the property makes the text invisible.
23788 POS-OR-PROP can be a marker or number, in which case it is taken to be
23789 a position in the current buffer and the value of the `invisible' property
23790 is checked; or it can be some other value, which is then presumed to be the
23791 value of the `invisible' property of the text of interest.
23792 The non-nil value returned can be t for truly invisible text or something
23793 else if the text is replaced by an ellipsis. */)
23794 (Lisp_Object pos_or_prop)
23796 Lisp_Object prop
23797 = (NATNUMP (pos_or_prop) || MARKERP (pos_or_prop)
23798 ? Fget_char_property (pos_or_prop, Qinvisible, Qnil)
23799 : pos_or_prop);
23800 int invis = TEXT_PROP_MEANS_INVISIBLE (prop);
23801 return (invis == 0 ? Qnil
23802 : invis == 1 ? Qt
23803 : make_number (invis));
23806 /* Calculate a width or height in pixels from a specification using
23807 the following elements:
23809 SPEC ::=
23810 NUM - a (fractional) multiple of the default font width/height
23811 (NUM) - specifies exactly NUM pixels
23812 UNIT - a fixed number of pixels, see below.
23813 ELEMENT - size of a display element in pixels, see below.
23814 (NUM . SPEC) - equals NUM * SPEC
23815 (+ SPEC SPEC ...) - add pixel values
23816 (- SPEC SPEC ...) - subtract pixel values
23817 (- SPEC) - negate pixel value
23819 NUM ::=
23820 INT or FLOAT - a number constant
23821 SYMBOL - use symbol's (buffer local) variable binding.
23823 UNIT ::=
23824 in - pixels per inch *)
23825 mm - pixels per 1/1000 meter *)
23826 cm - pixels per 1/100 meter *)
23827 width - width of current font in pixels.
23828 height - height of current font in pixels.
23830 *) using the ratio(s) defined in display-pixels-per-inch.
23832 ELEMENT ::=
23834 left-fringe - left fringe width in pixels
23835 right-fringe - right fringe width in pixels
23837 left-margin - left margin width in pixels
23838 right-margin - right margin width in pixels
23840 scroll-bar - scroll-bar area width in pixels
23842 Examples:
23844 Pixels corresponding to 5 inches:
23845 (5 . in)
23847 Total width of non-text areas on left side of window (if scroll-bar is on left):
23848 '(space :width (+ left-fringe left-margin scroll-bar))
23850 Align to first text column (in header line):
23851 '(space :align-to 0)
23853 Align to middle of text area minus half the width of variable `my-image'
23854 containing a loaded image:
23855 '(space :align-to (0.5 . (- text my-image)))
23857 Width of left margin minus width of 1 character in the default font:
23858 '(space :width (- left-margin 1))
23860 Width of left margin minus width of 2 characters in the current font:
23861 '(space :width (- left-margin (2 . width)))
23863 Center 1 character over left-margin (in header line):
23864 '(space :align-to (+ left-margin (0.5 . left-margin) -0.5))
23866 Different ways to express width of left fringe plus left margin minus one pixel:
23867 '(space :width (- (+ left-fringe left-margin) (1)))
23868 '(space :width (+ left-fringe left-margin (- (1))))
23869 '(space :width (+ left-fringe left-margin (-1)))
23873 static int
23874 calc_pixel_width_or_height (double *res, struct it *it, Lisp_Object prop,
23875 struct font *font, int width_p, int *align_to)
23877 double pixels;
23879 #define OK_PIXELS(val) ((*res = (double)(val)), 1)
23880 #define OK_ALIGN_TO(val) ((*align_to = (int)(val)), 1)
23882 if (NILP (prop))
23883 return OK_PIXELS (0);
23885 eassert (FRAME_LIVE_P (it->f));
23887 if (SYMBOLP (prop))
23889 if (SCHARS (SYMBOL_NAME (prop)) == 2)
23891 char *unit = SSDATA (SYMBOL_NAME (prop));
23893 if (unit[0] == 'i' && unit[1] == 'n')
23894 pixels = 1.0;
23895 else if (unit[0] == 'm' && unit[1] == 'm')
23896 pixels = 25.4;
23897 else if (unit[0] == 'c' && unit[1] == 'm')
23898 pixels = 2.54;
23899 else
23900 pixels = 0;
23901 if (pixels > 0)
23903 double ppi = (width_p ? FRAME_RES_X (it->f)
23904 : FRAME_RES_Y (it->f));
23906 if (ppi > 0)
23907 return OK_PIXELS (ppi / pixels);
23908 return 0;
23912 #ifdef HAVE_WINDOW_SYSTEM
23913 if (EQ (prop, Qheight))
23914 return OK_PIXELS (font ? FONT_HEIGHT (font) : FRAME_LINE_HEIGHT (it->f));
23915 if (EQ (prop, Qwidth))
23916 return OK_PIXELS (font ? FONT_WIDTH (font) : FRAME_COLUMN_WIDTH (it->f));
23917 #else
23918 if (EQ (prop, Qheight) || EQ (prop, Qwidth))
23919 return OK_PIXELS (1);
23920 #endif
23922 if (EQ (prop, Qtext))
23923 return OK_PIXELS (width_p
23924 ? window_box_width (it->w, TEXT_AREA)
23925 : WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w));
23927 if (align_to && *align_to < 0)
23929 *res = 0;
23930 if (EQ (prop, Qleft))
23931 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA));
23932 if (EQ (prop, Qright))
23933 return OK_ALIGN_TO (window_box_right_offset (it->w, TEXT_AREA));
23934 if (EQ (prop, Qcenter))
23935 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA)
23936 + window_box_width (it->w, TEXT_AREA) / 2);
23937 if (EQ (prop, Qleft_fringe))
23938 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
23939 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (it->w)
23940 : window_box_right_offset (it->w, LEFT_MARGIN_AREA));
23941 if (EQ (prop, Qright_fringe))
23942 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
23943 ? window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
23944 : window_box_right_offset (it->w, TEXT_AREA));
23945 if (EQ (prop, Qleft_margin))
23946 return OK_ALIGN_TO (window_box_left_offset (it->w, LEFT_MARGIN_AREA));
23947 if (EQ (prop, Qright_margin))
23948 return OK_ALIGN_TO (window_box_left_offset (it->w, RIGHT_MARGIN_AREA));
23949 if (EQ (prop, Qscroll_bar))
23950 return OK_ALIGN_TO (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (it->w)
23952 : (window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
23953 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
23954 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
23955 : 0)));
23957 else
23959 if (EQ (prop, Qleft_fringe))
23960 return OK_PIXELS (WINDOW_LEFT_FRINGE_WIDTH (it->w));
23961 if (EQ (prop, Qright_fringe))
23962 return OK_PIXELS (WINDOW_RIGHT_FRINGE_WIDTH (it->w));
23963 if (EQ (prop, Qleft_margin))
23964 return OK_PIXELS (WINDOW_LEFT_MARGIN_WIDTH (it->w));
23965 if (EQ (prop, Qright_margin))
23966 return OK_PIXELS (WINDOW_RIGHT_MARGIN_WIDTH (it->w));
23967 if (EQ (prop, Qscroll_bar))
23968 return OK_PIXELS (WINDOW_SCROLL_BAR_AREA_WIDTH (it->w));
23971 prop = buffer_local_value (prop, it->w->contents);
23972 if (EQ (prop, Qunbound))
23973 prop = Qnil;
23976 if (INTEGERP (prop) || FLOATP (prop))
23978 int base_unit = (width_p
23979 ? FRAME_COLUMN_WIDTH (it->f)
23980 : FRAME_LINE_HEIGHT (it->f));
23981 return OK_PIXELS (XFLOATINT (prop) * base_unit);
23984 if (CONSP (prop))
23986 Lisp_Object car = XCAR (prop);
23987 Lisp_Object cdr = XCDR (prop);
23989 if (SYMBOLP (car))
23991 #ifdef HAVE_WINDOW_SYSTEM
23992 if (FRAME_WINDOW_P (it->f)
23993 && valid_image_p (prop))
23995 ptrdiff_t id = lookup_image (it->f, prop);
23996 struct image *img = IMAGE_FROM_ID (it->f, id);
23998 return OK_PIXELS (width_p ? img->width : img->height);
24000 #endif
24001 if (EQ (car, Qplus) || EQ (car, Qminus))
24003 int first = 1;
24004 double px;
24006 pixels = 0;
24007 while (CONSP (cdr))
24009 if (!calc_pixel_width_or_height (&px, it, XCAR (cdr),
24010 font, width_p, align_to))
24011 return 0;
24012 if (first)
24013 pixels = (EQ (car, Qplus) ? px : -px), first = 0;
24014 else
24015 pixels += px;
24016 cdr = XCDR (cdr);
24018 if (EQ (car, Qminus))
24019 pixels = -pixels;
24020 return OK_PIXELS (pixels);
24023 car = buffer_local_value (car, it->w->contents);
24024 if (EQ (car, Qunbound))
24025 car = Qnil;
24028 if (INTEGERP (car) || FLOATP (car))
24030 double fact;
24031 pixels = XFLOATINT (car);
24032 if (NILP (cdr))
24033 return OK_PIXELS (pixels);
24034 if (calc_pixel_width_or_height (&fact, it, cdr,
24035 font, width_p, align_to))
24036 return OK_PIXELS (pixels * fact);
24037 return 0;
24040 return 0;
24043 return 0;
24047 /***********************************************************************
24048 Glyph Display
24049 ***********************************************************************/
24051 #ifdef HAVE_WINDOW_SYSTEM
24053 #ifdef GLYPH_DEBUG
24055 void
24056 dump_glyph_string (struct glyph_string *s)
24058 fprintf (stderr, "glyph string\n");
24059 fprintf (stderr, " x, y, w, h = %d, %d, %d, %d\n",
24060 s->x, s->y, s->width, s->height);
24061 fprintf (stderr, " ybase = %d\n", s->ybase);
24062 fprintf (stderr, " hl = %d\n", s->hl);
24063 fprintf (stderr, " left overhang = %d, right = %d\n",
24064 s->left_overhang, s->right_overhang);
24065 fprintf (stderr, " nchars = %d\n", s->nchars);
24066 fprintf (stderr, " extends to end of line = %d\n",
24067 s->extends_to_end_of_line_p);
24068 fprintf (stderr, " font height = %d\n", FONT_HEIGHT (s->font));
24069 fprintf (stderr, " bg width = %d\n", s->background_width);
24072 #endif /* GLYPH_DEBUG */
24074 /* Initialize glyph string S. CHAR2B is a suitably allocated vector
24075 of XChar2b structures for S; it can't be allocated in
24076 init_glyph_string because it must be allocated via `alloca'. W
24077 is the window on which S is drawn. ROW and AREA are the glyph row
24078 and area within the row from which S is constructed. START is the
24079 index of the first glyph structure covered by S. HL is a
24080 face-override for drawing S. */
24082 #ifdef HAVE_NTGUI
24083 #define OPTIONAL_HDC(hdc) HDC hdc,
24084 #define DECLARE_HDC(hdc) HDC hdc;
24085 #define ALLOCATE_HDC(hdc, f) hdc = get_frame_dc ((f))
24086 #define RELEASE_HDC(hdc, f) release_frame_dc ((f), (hdc))
24087 #endif
24089 #ifndef OPTIONAL_HDC
24090 #define OPTIONAL_HDC(hdc)
24091 #define DECLARE_HDC(hdc)
24092 #define ALLOCATE_HDC(hdc, f)
24093 #define RELEASE_HDC(hdc, f)
24094 #endif
24096 static void
24097 init_glyph_string (struct glyph_string *s,
24098 OPTIONAL_HDC (hdc)
24099 XChar2b *char2b, struct window *w, struct glyph_row *row,
24100 enum glyph_row_area area, int start, enum draw_glyphs_face hl)
24102 memset (s, 0, sizeof *s);
24103 s->w = w;
24104 s->f = XFRAME (w->frame);
24105 #ifdef HAVE_NTGUI
24106 s->hdc = hdc;
24107 #endif
24108 s->display = FRAME_X_DISPLAY (s->f);
24109 s->window = FRAME_X_WINDOW (s->f);
24110 s->char2b = char2b;
24111 s->hl = hl;
24112 s->row = row;
24113 s->area = area;
24114 s->first_glyph = row->glyphs[area] + start;
24115 s->height = row->height;
24116 s->y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
24117 s->ybase = s->y + row->ascent;
24121 /* Append the list of glyph strings with head H and tail T to the list
24122 with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the result. */
24124 static void
24125 append_glyph_string_lists (struct glyph_string **head, struct glyph_string **tail,
24126 struct glyph_string *h, struct glyph_string *t)
24128 if (h)
24130 if (*head)
24131 (*tail)->next = h;
24132 else
24133 *head = h;
24134 h->prev = *tail;
24135 *tail = t;
24140 /* Prepend the list of glyph strings with head H and tail T to the
24141 list with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the
24142 result. */
24144 static void
24145 prepend_glyph_string_lists (struct glyph_string **head, struct glyph_string **tail,
24146 struct glyph_string *h, struct glyph_string *t)
24148 if (h)
24150 if (*head)
24151 (*head)->prev = t;
24152 else
24153 *tail = t;
24154 t->next = *head;
24155 *head = h;
24160 /* Append glyph string S to the list with head *HEAD and tail *TAIL.
24161 Set *HEAD and *TAIL to the resulting list. */
24163 static void
24164 append_glyph_string (struct glyph_string **head, struct glyph_string **tail,
24165 struct glyph_string *s)
24167 s->next = s->prev = NULL;
24168 append_glyph_string_lists (head, tail, s, s);
24172 /* Get face and two-byte form of character C in face FACE_ID on frame F.
24173 The encoding of C is returned in *CHAR2B. DISPLAY_P non-zero means
24174 make sure that X resources for the face returned are allocated.
24175 Value is a pointer to a realized face that is ready for display if
24176 DISPLAY_P is non-zero. */
24178 static struct face *
24179 get_char_face_and_encoding (struct frame *f, int c, int face_id,
24180 XChar2b *char2b, int display_p)
24182 struct face *face = FACE_FROM_ID (f, face_id);
24183 unsigned code = 0;
24185 if (face->font)
24187 code = face->font->driver->encode_char (face->font, c);
24189 if (code == FONT_INVALID_CODE)
24190 code = 0;
24192 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
24194 /* Make sure X resources of the face are allocated. */
24195 #ifdef HAVE_X_WINDOWS
24196 if (display_p)
24197 #endif
24199 eassert (face != NULL);
24200 prepare_face_for_display (f, face);
24203 return face;
24207 /* Get face and two-byte form of character glyph GLYPH on frame F.
24208 The encoding of GLYPH->u.ch is returned in *CHAR2B. Value is
24209 a pointer to a realized face that is ready for display. */
24211 static struct face *
24212 get_glyph_face_and_encoding (struct frame *f, struct glyph *glyph,
24213 XChar2b *char2b, int *two_byte_p)
24215 struct face *face;
24216 unsigned code = 0;
24218 eassert (glyph->type == CHAR_GLYPH);
24219 face = FACE_FROM_ID (f, glyph->face_id);
24221 /* Make sure X resources of the face are allocated. */
24222 eassert (face != NULL);
24223 prepare_face_for_display (f, face);
24225 if (two_byte_p)
24226 *two_byte_p = 0;
24228 if (face->font)
24230 if (CHAR_BYTE8_P (glyph->u.ch))
24231 code = CHAR_TO_BYTE8 (glyph->u.ch);
24232 else
24233 code = face->font->driver->encode_char (face->font, glyph->u.ch);
24235 if (code == FONT_INVALID_CODE)
24236 code = 0;
24239 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
24240 return face;
24244 /* Get glyph code of character C in FONT in the two-byte form CHAR2B.
24245 Return 1 if FONT has a glyph for C, otherwise return 0. */
24247 static int
24248 get_char_glyph_code (int c, struct font *font, XChar2b *char2b)
24250 unsigned code;
24252 if (CHAR_BYTE8_P (c))
24253 code = CHAR_TO_BYTE8 (c);
24254 else
24255 code = font->driver->encode_char (font, c);
24257 if (code == FONT_INVALID_CODE)
24258 return 0;
24259 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
24260 return 1;
24264 /* Fill glyph string S with composition components specified by S->cmp.
24266 BASE_FACE is the base face of the composition.
24267 S->cmp_from is the index of the first component for S.
24269 OVERLAPS non-zero means S should draw the foreground only, and use
24270 its physical height for clipping. See also draw_glyphs.
24272 Value is the index of a component not in S. */
24274 static int
24275 fill_composite_glyph_string (struct glyph_string *s, struct face *base_face,
24276 int overlaps)
24278 int i;
24279 /* For all glyphs of this composition, starting at the offset
24280 S->cmp_from, until we reach the end of the definition or encounter a
24281 glyph that requires the different face, add it to S. */
24282 struct face *face;
24284 eassert (s);
24286 s->for_overlaps = overlaps;
24287 s->face = NULL;
24288 s->font = NULL;
24289 for (i = s->cmp_from; i < s->cmp->glyph_len; i++)
24291 int c = COMPOSITION_GLYPH (s->cmp, i);
24293 /* TAB in a composition means display glyphs with padding space
24294 on the left or right. */
24295 if (c != '\t')
24297 int face_id = FACE_FOR_CHAR (s->f, base_face->ascii_face, c,
24298 -1, Qnil);
24300 face = get_char_face_and_encoding (s->f, c, face_id,
24301 s->char2b + i, 1);
24302 if (face)
24304 if (! s->face)
24306 s->face = face;
24307 s->font = s->face->font;
24309 else if (s->face != face)
24310 break;
24313 ++s->nchars;
24315 s->cmp_to = i;
24317 if (s->face == NULL)
24319 s->face = base_face->ascii_face;
24320 s->font = s->face->font;
24323 /* All glyph strings for the same composition has the same width,
24324 i.e. the width set for the first component of the composition. */
24325 s->width = s->first_glyph->pixel_width;
24327 /* If the specified font could not be loaded, use the frame's
24328 default font, but record the fact that we couldn't load it in
24329 the glyph string so that we can draw rectangles for the
24330 characters of the glyph string. */
24331 if (s->font == NULL)
24333 s->font_not_found_p = 1;
24334 s->font = FRAME_FONT (s->f);
24337 /* Adjust base line for subscript/superscript text. */
24338 s->ybase += s->first_glyph->voffset;
24340 /* This glyph string must always be drawn with 16-bit functions. */
24341 s->two_byte_p = 1;
24343 return s->cmp_to;
24346 static int
24347 fill_gstring_glyph_string (struct glyph_string *s, int face_id,
24348 int start, int end, int overlaps)
24350 struct glyph *glyph, *last;
24351 Lisp_Object lgstring;
24352 int i;
24354 s->for_overlaps = overlaps;
24355 glyph = s->row->glyphs[s->area] + start;
24356 last = s->row->glyphs[s->area] + end;
24357 s->cmp_id = glyph->u.cmp.id;
24358 s->cmp_from = glyph->slice.cmp.from;
24359 s->cmp_to = glyph->slice.cmp.to + 1;
24360 s->face = FACE_FROM_ID (s->f, face_id);
24361 lgstring = composition_gstring_from_id (s->cmp_id);
24362 s->font = XFONT_OBJECT (LGSTRING_FONT (lgstring));
24363 glyph++;
24364 while (glyph < last
24365 && glyph->u.cmp.automatic
24366 && glyph->u.cmp.id == s->cmp_id
24367 && s->cmp_to == glyph->slice.cmp.from)
24368 s->cmp_to = (glyph++)->slice.cmp.to + 1;
24370 for (i = s->cmp_from; i < s->cmp_to; i++)
24372 Lisp_Object lglyph = LGSTRING_GLYPH (lgstring, i);
24373 unsigned code = LGLYPH_CODE (lglyph);
24375 STORE_XCHAR2B ((s->char2b + i), code >> 8, code & 0xFF);
24377 s->width = composition_gstring_width (lgstring, s->cmp_from, s->cmp_to, NULL);
24378 return glyph - s->row->glyphs[s->area];
24382 /* Fill glyph string S from a sequence glyphs for glyphless characters.
24383 See the comment of fill_glyph_string for arguments.
24384 Value is the index of the first glyph not in S. */
24387 static int
24388 fill_glyphless_glyph_string (struct glyph_string *s, int face_id,
24389 int start, int end, int overlaps)
24391 struct glyph *glyph, *last;
24392 int voffset;
24394 eassert (s->first_glyph->type == GLYPHLESS_GLYPH);
24395 s->for_overlaps = overlaps;
24396 glyph = s->row->glyphs[s->area] + start;
24397 last = s->row->glyphs[s->area] + end;
24398 voffset = glyph->voffset;
24399 s->face = FACE_FROM_ID (s->f, face_id);
24400 s->font = s->face->font ? s->face->font : FRAME_FONT (s->f);
24401 s->nchars = 1;
24402 s->width = glyph->pixel_width;
24403 glyph++;
24404 while (glyph < last
24405 && glyph->type == GLYPHLESS_GLYPH
24406 && glyph->voffset == voffset
24407 && glyph->face_id == face_id)
24409 s->nchars++;
24410 s->width += glyph->pixel_width;
24411 glyph++;
24413 s->ybase += voffset;
24414 return glyph - s->row->glyphs[s->area];
24418 /* Fill glyph string S from a sequence of character glyphs.
24420 FACE_ID is the face id of the string. START is the index of the
24421 first glyph to consider, END is the index of the last + 1.
24422 OVERLAPS non-zero means S should draw the foreground only, and use
24423 its physical height for clipping. See also draw_glyphs.
24425 Value is the index of the first glyph not in S. */
24427 static int
24428 fill_glyph_string (struct glyph_string *s, int face_id,
24429 int start, int end, int overlaps)
24431 struct glyph *glyph, *last;
24432 int voffset;
24433 int glyph_not_available_p;
24435 eassert (s->f == XFRAME (s->w->frame));
24436 eassert (s->nchars == 0);
24437 eassert (start >= 0 && end > start);
24439 s->for_overlaps = overlaps;
24440 glyph = s->row->glyphs[s->area] + start;
24441 last = s->row->glyphs[s->area] + end;
24442 voffset = glyph->voffset;
24443 s->padding_p = glyph->padding_p;
24444 glyph_not_available_p = glyph->glyph_not_available_p;
24446 while (glyph < last
24447 && glyph->type == CHAR_GLYPH
24448 && glyph->voffset == voffset
24449 /* Same face id implies same font, nowadays. */
24450 && glyph->face_id == face_id
24451 && glyph->glyph_not_available_p == glyph_not_available_p)
24453 int two_byte_p;
24455 s->face = get_glyph_face_and_encoding (s->f, glyph,
24456 s->char2b + s->nchars,
24457 &two_byte_p);
24458 s->two_byte_p = two_byte_p;
24459 ++s->nchars;
24460 eassert (s->nchars <= end - start);
24461 s->width += glyph->pixel_width;
24462 if (glyph++->padding_p != s->padding_p)
24463 break;
24466 s->font = s->face->font;
24468 /* If the specified font could not be loaded, use the frame's font,
24469 but record the fact that we couldn't load it in
24470 S->font_not_found_p so that we can draw rectangles for the
24471 characters of the glyph string. */
24472 if (s->font == NULL || glyph_not_available_p)
24474 s->font_not_found_p = 1;
24475 s->font = FRAME_FONT (s->f);
24478 /* Adjust base line for subscript/superscript text. */
24479 s->ybase += voffset;
24481 eassert (s->face && s->face->gc);
24482 return glyph - s->row->glyphs[s->area];
24486 /* Fill glyph string S from image glyph S->first_glyph. */
24488 static void
24489 fill_image_glyph_string (struct glyph_string *s)
24491 eassert (s->first_glyph->type == IMAGE_GLYPH);
24492 s->img = IMAGE_FROM_ID (s->f, s->first_glyph->u.img_id);
24493 eassert (s->img);
24494 s->slice = s->first_glyph->slice.img;
24495 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
24496 s->font = s->face->font;
24497 s->width = s->first_glyph->pixel_width;
24499 /* Adjust base line for subscript/superscript text. */
24500 s->ybase += s->first_glyph->voffset;
24504 /* Fill glyph string S from a sequence of stretch glyphs.
24506 START is the index of the first glyph to consider,
24507 END is the index of the last + 1.
24509 Value is the index of the first glyph not in S. */
24511 static int
24512 fill_stretch_glyph_string (struct glyph_string *s, int start, int end)
24514 struct glyph *glyph, *last;
24515 int voffset, face_id;
24517 eassert (s->first_glyph->type == STRETCH_GLYPH);
24519 glyph = s->row->glyphs[s->area] + start;
24520 last = s->row->glyphs[s->area] + end;
24521 face_id = glyph->face_id;
24522 s->face = FACE_FROM_ID (s->f, face_id);
24523 s->font = s->face->font;
24524 s->width = glyph->pixel_width;
24525 s->nchars = 1;
24526 voffset = glyph->voffset;
24528 for (++glyph;
24529 (glyph < last
24530 && glyph->type == STRETCH_GLYPH
24531 && glyph->voffset == voffset
24532 && glyph->face_id == face_id);
24533 ++glyph)
24534 s->width += glyph->pixel_width;
24536 /* Adjust base line for subscript/superscript text. */
24537 s->ybase += voffset;
24539 /* The case that face->gc == 0 is handled when drawing the glyph
24540 string by calling prepare_face_for_display. */
24541 eassert (s->face);
24542 return glyph - s->row->glyphs[s->area];
24545 static struct font_metrics *
24546 get_per_char_metric (struct font *font, XChar2b *char2b)
24548 static struct font_metrics metrics;
24549 unsigned code;
24551 if (! font)
24552 return NULL;
24553 code = (XCHAR2B_BYTE1 (char2b) << 8) | XCHAR2B_BYTE2 (char2b);
24554 if (code == FONT_INVALID_CODE)
24555 return NULL;
24556 font->driver->text_extents (font, &code, 1, &metrics);
24557 return &metrics;
24560 /* EXPORT for RIF:
24561 Set *LEFT and *RIGHT to the left and right overhang of GLYPH on
24562 frame F. Overhangs of glyphs other than type CHAR_GLYPH are
24563 assumed to be zero. */
24565 void
24566 x_get_glyph_overhangs (struct glyph *glyph, struct frame *f, int *left, int *right)
24568 *left = *right = 0;
24570 if (glyph->type == CHAR_GLYPH)
24572 struct face *face;
24573 XChar2b char2b;
24574 struct font_metrics *pcm;
24576 face = get_glyph_face_and_encoding (f, glyph, &char2b, NULL);
24577 if (face->font && (pcm = get_per_char_metric (face->font, &char2b)))
24579 if (pcm->rbearing > pcm->width)
24580 *right = pcm->rbearing - pcm->width;
24581 if (pcm->lbearing < 0)
24582 *left = -pcm->lbearing;
24585 else if (glyph->type == COMPOSITE_GLYPH)
24587 if (! glyph->u.cmp.automatic)
24589 struct composition *cmp = composition_table[glyph->u.cmp.id];
24591 if (cmp->rbearing > cmp->pixel_width)
24592 *right = cmp->rbearing - cmp->pixel_width;
24593 if (cmp->lbearing < 0)
24594 *left = - cmp->lbearing;
24596 else
24598 Lisp_Object gstring = composition_gstring_from_id (glyph->u.cmp.id);
24599 struct font_metrics metrics;
24601 composition_gstring_width (gstring, glyph->slice.cmp.from,
24602 glyph->slice.cmp.to + 1, &metrics);
24603 if (metrics.rbearing > metrics.width)
24604 *right = metrics.rbearing - metrics.width;
24605 if (metrics.lbearing < 0)
24606 *left = - metrics.lbearing;
24612 /* Return the index of the first glyph preceding glyph string S that
24613 is overwritten by S because of S's left overhang. Value is -1
24614 if no glyphs are overwritten. */
24616 static int
24617 left_overwritten (struct glyph_string *s)
24619 int k;
24621 if (s->left_overhang)
24623 int x = 0, i;
24624 struct glyph *glyphs = s->row->glyphs[s->area];
24625 int first = s->first_glyph - glyphs;
24627 for (i = first - 1; i >= 0 && x > -s->left_overhang; --i)
24628 x -= glyphs[i].pixel_width;
24630 k = i + 1;
24632 else
24633 k = -1;
24635 return k;
24639 /* Return the index of the first glyph preceding glyph string S that
24640 is overwriting S because of its right overhang. Value is -1 if no
24641 glyph in front of S overwrites S. */
24643 static int
24644 left_overwriting (struct glyph_string *s)
24646 int i, k, x;
24647 struct glyph *glyphs = s->row->glyphs[s->area];
24648 int first = s->first_glyph - glyphs;
24650 k = -1;
24651 x = 0;
24652 for (i = first - 1; i >= 0; --i)
24654 int left, right;
24655 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
24656 if (x + right > 0)
24657 k = i;
24658 x -= glyphs[i].pixel_width;
24661 return k;
24665 /* Return the index of the last glyph following glyph string S that is
24666 overwritten by S because of S's right overhang. Value is -1 if
24667 no such glyph is found. */
24669 static int
24670 right_overwritten (struct glyph_string *s)
24672 int k = -1;
24674 if (s->right_overhang)
24676 int x = 0, i;
24677 struct glyph *glyphs = s->row->glyphs[s->area];
24678 int first = (s->first_glyph - glyphs
24679 + (s->first_glyph->type == COMPOSITE_GLYPH ? 1 : s->nchars));
24680 int end = s->row->used[s->area];
24682 for (i = first; i < end && s->right_overhang > x; ++i)
24683 x += glyphs[i].pixel_width;
24685 k = i;
24688 return k;
24692 /* Return the index of the last glyph following glyph string S that
24693 overwrites S because of its left overhang. Value is negative
24694 if no such glyph is found. */
24696 static int
24697 right_overwriting (struct glyph_string *s)
24699 int i, k, x;
24700 int end = s->row->used[s->area];
24701 struct glyph *glyphs = s->row->glyphs[s->area];
24702 int first = (s->first_glyph - glyphs
24703 + (s->first_glyph->type == COMPOSITE_GLYPH ? 1 : s->nchars));
24705 k = -1;
24706 x = 0;
24707 for (i = first; i < end; ++i)
24709 int left, right;
24710 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
24711 if (x - left < 0)
24712 k = i;
24713 x += glyphs[i].pixel_width;
24716 return k;
24720 /* Set background width of glyph string S. START is the index of the
24721 first glyph following S. LAST_X is the right-most x-position + 1
24722 in the drawing area. */
24724 static void
24725 set_glyph_string_background_width (struct glyph_string *s, int start, int last_x)
24727 /* If the face of this glyph string has to be drawn to the end of
24728 the drawing area, set S->extends_to_end_of_line_p. */
24730 if (start == s->row->used[s->area]
24731 && ((s->row->fill_line_p
24732 && (s->hl == DRAW_NORMAL_TEXT
24733 || s->hl == DRAW_IMAGE_RAISED
24734 || s->hl == DRAW_IMAGE_SUNKEN))
24735 || s->hl == DRAW_MOUSE_FACE))
24736 s->extends_to_end_of_line_p = 1;
24738 /* If S extends its face to the end of the line, set its
24739 background_width to the distance to the right edge of the drawing
24740 area. */
24741 if (s->extends_to_end_of_line_p)
24742 s->background_width = last_x - s->x + 1;
24743 else
24744 s->background_width = s->width;
24748 /* Compute overhangs and x-positions for glyph string S and its
24749 predecessors, or successors. X is the starting x-position for S.
24750 BACKWARD_P non-zero means process predecessors. */
24752 static void
24753 compute_overhangs_and_x (struct glyph_string *s, int x, int backward_p)
24755 if (backward_p)
24757 while (s)
24759 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
24760 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
24761 x -= s->width;
24762 s->x = x;
24763 s = s->prev;
24766 else
24768 while (s)
24770 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
24771 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
24772 s->x = x;
24773 x += s->width;
24774 s = s->next;
24781 /* The following macros are only called from draw_glyphs below.
24782 They reference the following parameters of that function directly:
24783 `w', `row', `area', and `overlap_p'
24784 as well as the following local variables:
24785 `s', `f', and `hdc' (in W32) */
24787 #ifdef HAVE_NTGUI
24788 /* On W32, silently add local `hdc' variable to argument list of
24789 init_glyph_string. */
24790 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
24791 init_glyph_string (s, hdc, char2b, w, row, area, start, hl)
24792 #else
24793 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
24794 init_glyph_string (s, char2b, w, row, area, start, hl)
24795 #endif
24797 /* Add a glyph string for a stretch glyph to the list of strings
24798 between HEAD and TAIL. START is the index of the stretch glyph in
24799 row area AREA of glyph row ROW. END is the index of the last glyph
24800 in that glyph row area. X is the current output position assigned
24801 to the new glyph string constructed. HL overrides that face of the
24802 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
24803 is the right-most x-position of the drawing area. */
24805 /* SunOS 4 bundled cc, barfed on continuations in the arg lists here
24806 and below -- keep them on one line. */
24807 #define BUILD_STRETCH_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
24808 do \
24810 s = alloca (sizeof *s); \
24811 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
24812 START = fill_stretch_glyph_string (s, START, END); \
24813 append_glyph_string (&HEAD, &TAIL, s); \
24814 s->x = (X); \
24816 while (0)
24819 /* Add a glyph string for an image glyph to the list of strings
24820 between HEAD and TAIL. START is the index of the image glyph in
24821 row area AREA of glyph row ROW. END is the index of the last glyph
24822 in that glyph row area. X is the current output position assigned
24823 to the new glyph string constructed. HL overrides that face of the
24824 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
24825 is the right-most x-position of the drawing area. */
24827 #define BUILD_IMAGE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
24828 do \
24830 s = alloca (sizeof *s); \
24831 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
24832 fill_image_glyph_string (s); \
24833 append_glyph_string (&HEAD, &TAIL, s); \
24834 ++START; \
24835 s->x = (X); \
24837 while (0)
24840 /* Add a glyph string for a sequence of character glyphs to the list
24841 of strings between HEAD and TAIL. START is the index of the first
24842 glyph in row area AREA of glyph row ROW that is part of the new
24843 glyph string. END is the index of the last glyph in that glyph row
24844 area. X is the current output position assigned to the new glyph
24845 string constructed. HL overrides that face of the glyph; e.g. it
24846 is DRAW_CURSOR if a cursor has to be drawn. LAST_X is the
24847 right-most x-position of the drawing area. */
24849 #define BUILD_CHAR_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
24850 do \
24852 int face_id; \
24853 XChar2b *char2b; \
24855 face_id = (row)->glyphs[area][START].face_id; \
24857 s = alloca (sizeof *s); \
24858 SAFE_NALLOCA (char2b, 1, (END) - (START)); \
24859 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
24860 append_glyph_string (&HEAD, &TAIL, s); \
24861 s->x = (X); \
24862 START = fill_glyph_string (s, face_id, START, END, overlaps); \
24864 while (0)
24867 /* Add a glyph string for a composite sequence to the list of strings
24868 between HEAD and TAIL. START is the index of the first glyph in
24869 row area AREA of glyph row ROW that is part of the new glyph
24870 string. END is the index of the last glyph in that glyph row area.
24871 X is the current output position assigned to the new glyph string
24872 constructed. HL overrides that face of the glyph; e.g. it is
24873 DRAW_CURSOR if a cursor has to be drawn. LAST_X is the right-most
24874 x-position of the drawing area. */
24876 #define BUILD_COMPOSITE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
24877 do { \
24878 int face_id = (row)->glyphs[area][START].face_id; \
24879 struct face *base_face = FACE_FROM_ID (f, face_id); \
24880 ptrdiff_t cmp_id = (row)->glyphs[area][START].u.cmp.id; \
24881 struct composition *cmp = composition_table[cmp_id]; \
24882 XChar2b *char2b; \
24883 struct glyph_string *first_s = NULL; \
24884 int n; \
24886 SAFE_NALLOCA (char2b, 1, cmp->glyph_len); \
24888 /* Make glyph_strings for each glyph sequence that is drawable by \
24889 the same face, and append them to HEAD/TAIL. */ \
24890 for (n = 0; n < cmp->glyph_len;) \
24892 s = alloca (sizeof *s); \
24893 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
24894 append_glyph_string (&(HEAD), &(TAIL), s); \
24895 s->cmp = cmp; \
24896 s->cmp_from = n; \
24897 s->x = (X); \
24898 if (n == 0) \
24899 first_s = s; \
24900 n = fill_composite_glyph_string (s, base_face, overlaps); \
24903 ++START; \
24904 s = first_s; \
24905 } while (0)
24908 /* Add a glyph string for a glyph-string sequence to the list of strings
24909 between HEAD and TAIL. */
24911 #define BUILD_GSTRING_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
24912 do { \
24913 int face_id; \
24914 XChar2b *char2b; \
24915 Lisp_Object gstring; \
24917 face_id = (row)->glyphs[area][START].face_id; \
24918 gstring = (composition_gstring_from_id \
24919 ((row)->glyphs[area][START].u.cmp.id)); \
24920 s = alloca (sizeof *s); \
24921 SAFE_NALLOCA (char2b, 1, LGSTRING_GLYPH_LEN (gstring)); \
24922 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
24923 append_glyph_string (&(HEAD), &(TAIL), s); \
24924 s->x = (X); \
24925 START = fill_gstring_glyph_string (s, face_id, START, END, overlaps); \
24926 } while (0)
24929 /* Add a glyph string for a sequence of glyphless character's glyphs
24930 to the list of strings between HEAD and TAIL. The meanings of
24931 arguments are the same as those of BUILD_CHAR_GLYPH_STRINGS. */
24933 #define BUILD_GLYPHLESS_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
24934 do \
24936 int face_id; \
24938 face_id = (row)->glyphs[area][START].face_id; \
24940 s = alloca (sizeof *s); \
24941 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
24942 append_glyph_string (&HEAD, &TAIL, s); \
24943 s->x = (X); \
24944 START = fill_glyphless_glyph_string (s, face_id, START, END, \
24945 overlaps); \
24947 while (0)
24950 /* Build a list of glyph strings between HEAD and TAIL for the glyphs
24951 of AREA of glyph row ROW on window W between indices START and END.
24952 HL overrides the face for drawing glyph strings, e.g. it is
24953 DRAW_CURSOR to draw a cursor. X and LAST_X are start and end
24954 x-positions of the drawing area.
24956 This is an ugly monster macro construct because we must use alloca
24957 to allocate glyph strings (because draw_glyphs can be called
24958 asynchronously). */
24960 #define BUILD_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
24961 do \
24963 HEAD = TAIL = NULL; \
24964 while (START < END) \
24966 struct glyph *first_glyph = (row)->glyphs[area] + START; \
24967 switch (first_glyph->type) \
24969 case CHAR_GLYPH: \
24970 BUILD_CHAR_GLYPH_STRINGS (START, END, HEAD, TAIL, \
24971 HL, X, LAST_X); \
24972 break; \
24974 case COMPOSITE_GLYPH: \
24975 if (first_glyph->u.cmp.automatic) \
24976 BUILD_GSTRING_GLYPH_STRING (START, END, HEAD, TAIL, \
24977 HL, X, LAST_X); \
24978 else \
24979 BUILD_COMPOSITE_GLYPH_STRING (START, END, HEAD, TAIL, \
24980 HL, X, LAST_X); \
24981 break; \
24983 case STRETCH_GLYPH: \
24984 BUILD_STRETCH_GLYPH_STRING (START, END, HEAD, TAIL, \
24985 HL, X, LAST_X); \
24986 break; \
24988 case IMAGE_GLYPH: \
24989 BUILD_IMAGE_GLYPH_STRING (START, END, HEAD, TAIL, \
24990 HL, X, LAST_X); \
24991 break; \
24993 case GLYPHLESS_GLYPH: \
24994 BUILD_GLYPHLESS_GLYPH_STRING (START, END, HEAD, TAIL, \
24995 HL, X, LAST_X); \
24996 break; \
24998 default: \
24999 emacs_abort (); \
25002 if (s) \
25004 set_glyph_string_background_width (s, START, LAST_X); \
25005 (X) += s->width; \
25008 } while (0)
25011 /* Draw glyphs between START and END in AREA of ROW on window W,
25012 starting at x-position X. X is relative to AREA in W. HL is a
25013 face-override with the following meaning:
25015 DRAW_NORMAL_TEXT draw normally
25016 DRAW_CURSOR draw in cursor face
25017 DRAW_MOUSE_FACE draw in mouse face.
25018 DRAW_INVERSE_VIDEO draw in mode line face
25019 DRAW_IMAGE_SUNKEN draw an image with a sunken relief around it
25020 DRAW_IMAGE_RAISED draw an image with a raised relief around it
25022 If OVERLAPS is non-zero, draw only the foreground of characters and
25023 clip to the physical height of ROW. Non-zero value also defines
25024 the overlapping part to be drawn:
25026 OVERLAPS_PRED overlap with preceding rows
25027 OVERLAPS_SUCC overlap with succeeding rows
25028 OVERLAPS_BOTH overlap with both preceding/succeeding rows
25029 OVERLAPS_ERASED_CURSOR overlap with erased cursor area
25031 Value is the x-position reached, relative to AREA of W. */
25033 static int
25034 draw_glyphs (struct window *w, int x, struct glyph_row *row,
25035 enum glyph_row_area area, ptrdiff_t start, ptrdiff_t end,
25036 enum draw_glyphs_face hl, int overlaps)
25038 struct glyph_string *head, *tail;
25039 struct glyph_string *s;
25040 struct glyph_string *clip_head = NULL, *clip_tail = NULL;
25041 int i, j, x_reached, last_x, area_left = 0;
25042 struct frame *f = XFRAME (WINDOW_FRAME (w));
25043 DECLARE_HDC (hdc);
25045 ALLOCATE_HDC (hdc, f);
25047 /* Let's rather be paranoid than getting a SEGV. */
25048 end = min (end, row->used[area]);
25049 start = clip_to_bounds (0, start, end);
25051 /* Translate X to frame coordinates. Set last_x to the right
25052 end of the drawing area. */
25053 if (row->full_width_p)
25055 /* X is relative to the left edge of W, without scroll bars
25056 or fringes. */
25057 area_left = WINDOW_LEFT_EDGE_X (w);
25058 last_x = (WINDOW_LEFT_EDGE_X (w) + WINDOW_PIXEL_WIDTH (w)
25059 - (row->mode_line_p ? WINDOW_RIGHT_DIVIDER_WIDTH (w) : 0));
25061 else
25063 area_left = window_box_left (w, area);
25064 last_x = area_left + window_box_width (w, area);
25066 x += area_left;
25068 /* Build a doubly-linked list of glyph_string structures between
25069 head and tail from what we have to draw. Note that the macro
25070 BUILD_GLYPH_STRINGS will modify its start parameter. That's
25071 the reason we use a separate variable `i'. */
25072 i = start;
25073 USE_SAFE_ALLOCA;
25074 BUILD_GLYPH_STRINGS (i, end, head, tail, hl, x, last_x);
25075 if (tail)
25076 x_reached = tail->x + tail->background_width;
25077 else
25078 x_reached = x;
25080 /* If there are any glyphs with lbearing < 0 or rbearing > width in
25081 the row, redraw some glyphs in front or following the glyph
25082 strings built above. */
25083 if (head && !overlaps && row->contains_overlapping_glyphs_p)
25085 struct glyph_string *h, *t;
25086 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
25087 int mouse_beg_col IF_LINT (= 0), mouse_end_col IF_LINT (= 0);
25088 int check_mouse_face = 0;
25089 int dummy_x = 0;
25091 /* If mouse highlighting is on, we may need to draw adjacent
25092 glyphs using mouse-face highlighting. */
25093 if (area == TEXT_AREA && row->mouse_face_p
25094 && hlinfo->mouse_face_beg_row >= 0
25095 && hlinfo->mouse_face_end_row >= 0)
25097 ptrdiff_t row_vpos = MATRIX_ROW_VPOS (row, w->current_matrix);
25099 if (row_vpos >= hlinfo->mouse_face_beg_row
25100 && row_vpos <= hlinfo->mouse_face_end_row)
25102 check_mouse_face = 1;
25103 mouse_beg_col = (row_vpos == hlinfo->mouse_face_beg_row)
25104 ? hlinfo->mouse_face_beg_col : 0;
25105 mouse_end_col = (row_vpos == hlinfo->mouse_face_end_row)
25106 ? hlinfo->mouse_face_end_col
25107 : row->used[TEXT_AREA];
25111 /* Compute overhangs for all glyph strings. */
25112 if (FRAME_RIF (f)->compute_glyph_string_overhangs)
25113 for (s = head; s; s = s->next)
25114 FRAME_RIF (f)->compute_glyph_string_overhangs (s);
25116 /* Prepend glyph strings for glyphs in front of the first glyph
25117 string that are overwritten because of the first glyph
25118 string's left overhang. The background of all strings
25119 prepended must be drawn because the first glyph string
25120 draws over it. */
25121 i = left_overwritten (head);
25122 if (i >= 0)
25124 enum draw_glyphs_face overlap_hl;
25126 /* If this row contains mouse highlighting, attempt to draw
25127 the overlapped glyphs with the correct highlight. This
25128 code fails if the overlap encompasses more than one glyph
25129 and mouse-highlight spans only some of these glyphs.
25130 However, making it work perfectly involves a lot more
25131 code, and I don't know if the pathological case occurs in
25132 practice, so we'll stick to this for now. --- cyd */
25133 if (check_mouse_face
25134 && mouse_beg_col < start && mouse_end_col > i)
25135 overlap_hl = DRAW_MOUSE_FACE;
25136 else
25137 overlap_hl = DRAW_NORMAL_TEXT;
25139 if (hl != overlap_hl)
25140 clip_head = head;
25141 j = i;
25142 BUILD_GLYPH_STRINGS (j, start, h, t,
25143 overlap_hl, dummy_x, last_x);
25144 start = i;
25145 compute_overhangs_and_x (t, head->x, 1);
25146 prepend_glyph_string_lists (&head, &tail, h, t);
25147 if (clip_head == NULL)
25148 clip_head = head;
25151 /* Prepend glyph strings for glyphs in front of the first glyph
25152 string that overwrite that glyph string because of their
25153 right overhang. For these strings, only the foreground must
25154 be drawn, because it draws over the glyph string at `head'.
25155 The background must not be drawn because this would overwrite
25156 right overhangs of preceding glyphs for which no glyph
25157 strings exist. */
25158 i = left_overwriting (head);
25159 if (i >= 0)
25161 enum draw_glyphs_face overlap_hl;
25163 if (check_mouse_face
25164 && mouse_beg_col < start && mouse_end_col > i)
25165 overlap_hl = DRAW_MOUSE_FACE;
25166 else
25167 overlap_hl = DRAW_NORMAL_TEXT;
25169 if (hl == overlap_hl || clip_head == NULL)
25170 clip_head = head;
25171 BUILD_GLYPH_STRINGS (i, start, h, t,
25172 overlap_hl, dummy_x, last_x);
25173 for (s = h; s; s = s->next)
25174 s->background_filled_p = 1;
25175 compute_overhangs_and_x (t, head->x, 1);
25176 prepend_glyph_string_lists (&head, &tail, h, t);
25179 /* Append glyphs strings for glyphs following the last glyph
25180 string tail that are overwritten by tail. The background of
25181 these strings has to be drawn because tail's foreground draws
25182 over it. */
25183 i = right_overwritten (tail);
25184 if (i >= 0)
25186 enum draw_glyphs_face overlap_hl;
25188 if (check_mouse_face
25189 && mouse_beg_col < i && mouse_end_col > end)
25190 overlap_hl = DRAW_MOUSE_FACE;
25191 else
25192 overlap_hl = DRAW_NORMAL_TEXT;
25194 if (hl != overlap_hl)
25195 clip_tail = tail;
25196 BUILD_GLYPH_STRINGS (end, i, h, t,
25197 overlap_hl, x, last_x);
25198 /* Because BUILD_GLYPH_STRINGS updates the first argument,
25199 we don't have `end = i;' here. */
25200 compute_overhangs_and_x (h, tail->x + tail->width, 0);
25201 append_glyph_string_lists (&head, &tail, h, t);
25202 if (clip_tail == NULL)
25203 clip_tail = tail;
25206 /* Append glyph strings for glyphs following the last glyph
25207 string tail that overwrite tail. The foreground of such
25208 glyphs has to be drawn because it writes into the background
25209 of tail. The background must not be drawn because it could
25210 paint over the foreground of following glyphs. */
25211 i = right_overwriting (tail);
25212 if (i >= 0)
25214 enum draw_glyphs_face overlap_hl;
25215 if (check_mouse_face
25216 && mouse_beg_col < i && mouse_end_col > end)
25217 overlap_hl = DRAW_MOUSE_FACE;
25218 else
25219 overlap_hl = DRAW_NORMAL_TEXT;
25221 if (hl == overlap_hl || clip_tail == NULL)
25222 clip_tail = tail;
25223 i++; /* We must include the Ith glyph. */
25224 BUILD_GLYPH_STRINGS (end, i, h, t,
25225 overlap_hl, x, last_x);
25226 for (s = h; s; s = s->next)
25227 s->background_filled_p = 1;
25228 compute_overhangs_and_x (h, tail->x + tail->width, 0);
25229 append_glyph_string_lists (&head, &tail, h, t);
25231 if (clip_head || clip_tail)
25232 for (s = head; s; s = s->next)
25234 s->clip_head = clip_head;
25235 s->clip_tail = clip_tail;
25239 /* Draw all strings. */
25240 for (s = head; s; s = s->next)
25241 FRAME_RIF (f)->draw_glyph_string (s);
25243 #ifndef HAVE_NS
25244 /* When focus a sole frame and move horizontally, this sets on_p to 0
25245 causing a failure to erase prev cursor position. */
25246 if (area == TEXT_AREA
25247 && !row->full_width_p
25248 /* When drawing overlapping rows, only the glyph strings'
25249 foreground is drawn, which doesn't erase a cursor
25250 completely. */
25251 && !overlaps)
25253 int x0 = clip_head ? clip_head->x : (head ? head->x : x);
25254 int x1 = (clip_tail ? clip_tail->x + clip_tail->background_width
25255 : (tail ? tail->x + tail->background_width : x));
25256 x0 -= area_left;
25257 x1 -= area_left;
25259 notice_overwritten_cursor (w, TEXT_AREA, x0, x1,
25260 row->y, MATRIX_ROW_BOTTOM_Y (row));
25262 #endif
25264 /* Value is the x-position up to which drawn, relative to AREA of W.
25265 This doesn't include parts drawn because of overhangs. */
25266 if (row->full_width_p)
25267 x_reached = FRAME_TO_WINDOW_PIXEL_X (w, x_reached);
25268 else
25269 x_reached -= area_left;
25271 RELEASE_HDC (hdc, f);
25273 SAFE_FREE ();
25274 return x_reached;
25277 /* Expand row matrix if too narrow. Don't expand if area
25278 is not present. */
25280 #define IT_EXPAND_MATRIX_WIDTH(it, area) \
25282 if (!it->f->fonts_changed \
25283 && (it->glyph_row->glyphs[area] \
25284 < it->glyph_row->glyphs[area + 1])) \
25286 it->w->ncols_scale_factor++; \
25287 it->f->fonts_changed = 1; \
25291 /* Store one glyph for IT->char_to_display in IT->glyph_row.
25292 Called from x_produce_glyphs when IT->glyph_row is non-null. */
25294 static void
25295 append_glyph (struct it *it)
25297 struct glyph *glyph;
25298 enum glyph_row_area area = it->area;
25300 eassert (it->glyph_row);
25301 eassert (it->char_to_display != '\n' && it->char_to_display != '\t');
25303 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
25304 if (glyph < it->glyph_row->glyphs[area + 1])
25306 /* If the glyph row is reversed, we need to prepend the glyph
25307 rather than append it. */
25308 if (it->glyph_row->reversed_p && area == TEXT_AREA)
25310 struct glyph *g;
25312 /* Make room for the additional glyph. */
25313 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
25314 g[1] = *g;
25315 glyph = it->glyph_row->glyphs[area];
25317 glyph->charpos = CHARPOS (it->position);
25318 glyph->object = it->object;
25319 if (it->pixel_width > 0)
25321 glyph->pixel_width = it->pixel_width;
25322 glyph->padding_p = 0;
25324 else
25326 /* Assure at least 1-pixel width. Otherwise, cursor can't
25327 be displayed correctly. */
25328 glyph->pixel_width = 1;
25329 glyph->padding_p = 1;
25331 glyph->ascent = it->ascent;
25332 glyph->descent = it->descent;
25333 glyph->voffset = it->voffset;
25334 glyph->type = CHAR_GLYPH;
25335 glyph->avoid_cursor_p = it->avoid_cursor_p;
25336 glyph->multibyte_p = it->multibyte_p;
25337 if (it->glyph_row->reversed_p && area == TEXT_AREA)
25339 /* In R2L rows, the left and the right box edges need to be
25340 drawn in reverse direction. */
25341 glyph->right_box_line_p = it->start_of_box_run_p;
25342 glyph->left_box_line_p = it->end_of_box_run_p;
25344 else
25346 glyph->left_box_line_p = it->start_of_box_run_p;
25347 glyph->right_box_line_p = it->end_of_box_run_p;
25349 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
25350 || it->phys_descent > it->descent);
25351 glyph->glyph_not_available_p = it->glyph_not_available_p;
25352 glyph->face_id = it->face_id;
25353 glyph->u.ch = it->char_to_display;
25354 glyph->slice.img = null_glyph_slice;
25355 glyph->font_type = FONT_TYPE_UNKNOWN;
25356 if (it->bidi_p)
25358 glyph->resolved_level = it->bidi_it.resolved_level;
25359 eassert ((it->bidi_it.type & 7) == it->bidi_it.type);
25360 glyph->bidi_type = it->bidi_it.type;
25362 else
25364 glyph->resolved_level = 0;
25365 glyph->bidi_type = UNKNOWN_BT;
25367 ++it->glyph_row->used[area];
25369 else
25370 IT_EXPAND_MATRIX_WIDTH (it, area);
25373 /* Store one glyph for the composition IT->cmp_it.id in
25374 IT->glyph_row. Called from x_produce_glyphs when IT->glyph_row is
25375 non-null. */
25377 static void
25378 append_composite_glyph (struct it *it)
25380 struct glyph *glyph;
25381 enum glyph_row_area area = it->area;
25383 eassert (it->glyph_row);
25385 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
25386 if (glyph < it->glyph_row->glyphs[area + 1])
25388 /* If the glyph row is reversed, we need to prepend the glyph
25389 rather than append it. */
25390 if (it->glyph_row->reversed_p && it->area == TEXT_AREA)
25392 struct glyph *g;
25394 /* Make room for the new glyph. */
25395 for (g = glyph - 1; g >= it->glyph_row->glyphs[it->area]; g--)
25396 g[1] = *g;
25397 glyph = it->glyph_row->glyphs[it->area];
25399 glyph->charpos = it->cmp_it.charpos;
25400 glyph->object = it->object;
25401 glyph->pixel_width = it->pixel_width;
25402 glyph->ascent = it->ascent;
25403 glyph->descent = it->descent;
25404 glyph->voffset = it->voffset;
25405 glyph->type = COMPOSITE_GLYPH;
25406 if (it->cmp_it.ch < 0)
25408 glyph->u.cmp.automatic = 0;
25409 glyph->u.cmp.id = it->cmp_it.id;
25410 glyph->slice.cmp.from = glyph->slice.cmp.to = 0;
25412 else
25414 glyph->u.cmp.automatic = 1;
25415 glyph->u.cmp.id = it->cmp_it.id;
25416 glyph->slice.cmp.from = it->cmp_it.from;
25417 glyph->slice.cmp.to = it->cmp_it.to - 1;
25419 glyph->avoid_cursor_p = it->avoid_cursor_p;
25420 glyph->multibyte_p = it->multibyte_p;
25421 if (it->glyph_row->reversed_p && area == TEXT_AREA)
25423 /* In R2L rows, the left and the right box edges need to be
25424 drawn in reverse direction. */
25425 glyph->right_box_line_p = it->start_of_box_run_p;
25426 glyph->left_box_line_p = it->end_of_box_run_p;
25428 else
25430 glyph->left_box_line_p = it->start_of_box_run_p;
25431 glyph->right_box_line_p = it->end_of_box_run_p;
25433 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
25434 || it->phys_descent > it->descent);
25435 glyph->padding_p = 0;
25436 glyph->glyph_not_available_p = 0;
25437 glyph->face_id = it->face_id;
25438 glyph->font_type = FONT_TYPE_UNKNOWN;
25439 if (it->bidi_p)
25441 glyph->resolved_level = it->bidi_it.resolved_level;
25442 eassert ((it->bidi_it.type & 7) == it->bidi_it.type);
25443 glyph->bidi_type = it->bidi_it.type;
25445 ++it->glyph_row->used[area];
25447 else
25448 IT_EXPAND_MATRIX_WIDTH (it, area);
25452 /* Change IT->ascent and IT->height according to the setting of
25453 IT->voffset. */
25455 static void
25456 take_vertical_position_into_account (struct it *it)
25458 if (it->voffset)
25460 if (it->voffset < 0)
25461 /* Increase the ascent so that we can display the text higher
25462 in the line. */
25463 it->ascent -= it->voffset;
25464 else
25465 /* Increase the descent so that we can display the text lower
25466 in the line. */
25467 it->descent += it->voffset;
25472 /* Produce glyphs/get display metrics for the image IT is loaded with.
25473 See the description of struct display_iterator in dispextern.h for
25474 an overview of struct display_iterator. */
25476 static void
25477 produce_image_glyph (struct it *it)
25479 struct image *img;
25480 struct face *face;
25481 int glyph_ascent, crop;
25482 struct glyph_slice slice;
25484 eassert (it->what == IT_IMAGE);
25486 face = FACE_FROM_ID (it->f, it->face_id);
25487 eassert (face);
25488 /* Make sure X resources of the face is loaded. */
25489 prepare_face_for_display (it->f, face);
25491 if (it->image_id < 0)
25493 /* Fringe bitmap. */
25494 it->ascent = it->phys_ascent = 0;
25495 it->descent = it->phys_descent = 0;
25496 it->pixel_width = 0;
25497 it->nglyphs = 0;
25498 return;
25501 img = IMAGE_FROM_ID (it->f, it->image_id);
25502 eassert (img);
25503 /* Make sure X resources of the image is loaded. */
25504 prepare_image_for_display (it->f, img);
25506 slice.x = slice.y = 0;
25507 slice.width = img->width;
25508 slice.height = img->height;
25510 if (INTEGERP (it->slice.x))
25511 slice.x = XINT (it->slice.x);
25512 else if (FLOATP (it->slice.x))
25513 slice.x = XFLOAT_DATA (it->slice.x) * img->width;
25515 if (INTEGERP (it->slice.y))
25516 slice.y = XINT (it->slice.y);
25517 else if (FLOATP (it->slice.y))
25518 slice.y = XFLOAT_DATA (it->slice.y) * img->height;
25520 if (INTEGERP (it->slice.width))
25521 slice.width = XINT (it->slice.width);
25522 else if (FLOATP (it->slice.width))
25523 slice.width = XFLOAT_DATA (it->slice.width) * img->width;
25525 if (INTEGERP (it->slice.height))
25526 slice.height = XINT (it->slice.height);
25527 else if (FLOATP (it->slice.height))
25528 slice.height = XFLOAT_DATA (it->slice.height) * img->height;
25530 if (slice.x >= img->width)
25531 slice.x = img->width;
25532 if (slice.y >= img->height)
25533 slice.y = img->height;
25534 if (slice.x + slice.width >= img->width)
25535 slice.width = img->width - slice.x;
25536 if (slice.y + slice.height > img->height)
25537 slice.height = img->height - slice.y;
25539 if (slice.width == 0 || slice.height == 0)
25540 return;
25542 it->ascent = it->phys_ascent = glyph_ascent = image_ascent (img, face, &slice);
25544 it->descent = slice.height - glyph_ascent;
25545 if (slice.y == 0)
25546 it->descent += img->vmargin;
25547 if (slice.y + slice.height == img->height)
25548 it->descent += img->vmargin;
25549 it->phys_descent = it->descent;
25551 it->pixel_width = slice.width;
25552 if (slice.x == 0)
25553 it->pixel_width += img->hmargin;
25554 if (slice.x + slice.width == img->width)
25555 it->pixel_width += img->hmargin;
25557 /* It's quite possible for images to have an ascent greater than
25558 their height, so don't get confused in that case. */
25559 if (it->descent < 0)
25560 it->descent = 0;
25562 it->nglyphs = 1;
25564 if (face->box != FACE_NO_BOX)
25566 if (face->box_line_width > 0)
25568 if (slice.y == 0)
25569 it->ascent += face->box_line_width;
25570 if (slice.y + slice.height == img->height)
25571 it->descent += face->box_line_width;
25574 if (it->start_of_box_run_p && slice.x == 0)
25575 it->pixel_width += eabs (face->box_line_width);
25576 if (it->end_of_box_run_p && slice.x + slice.width == img->width)
25577 it->pixel_width += eabs (face->box_line_width);
25580 take_vertical_position_into_account (it);
25582 /* Automatically crop wide image glyphs at right edge so we can
25583 draw the cursor on same display row. */
25584 if ((crop = it->pixel_width - (it->last_visible_x - it->current_x), crop > 0)
25585 && (it->hpos == 0 || it->pixel_width > it->last_visible_x / 4))
25587 it->pixel_width -= crop;
25588 slice.width -= crop;
25591 if (it->glyph_row)
25593 struct glyph *glyph;
25594 enum glyph_row_area area = it->area;
25596 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
25597 if (glyph < it->glyph_row->glyphs[area + 1])
25599 glyph->charpos = CHARPOS (it->position);
25600 glyph->object = it->object;
25601 glyph->pixel_width = it->pixel_width;
25602 glyph->ascent = glyph_ascent;
25603 glyph->descent = it->descent;
25604 glyph->voffset = it->voffset;
25605 glyph->type = IMAGE_GLYPH;
25606 glyph->avoid_cursor_p = it->avoid_cursor_p;
25607 glyph->multibyte_p = it->multibyte_p;
25608 if (it->glyph_row->reversed_p && area == TEXT_AREA)
25610 /* In R2L rows, the left and the right box edges need to be
25611 drawn in reverse direction. */
25612 glyph->right_box_line_p = it->start_of_box_run_p;
25613 glyph->left_box_line_p = it->end_of_box_run_p;
25615 else
25617 glyph->left_box_line_p = it->start_of_box_run_p;
25618 glyph->right_box_line_p = it->end_of_box_run_p;
25620 glyph->overlaps_vertically_p = 0;
25621 glyph->padding_p = 0;
25622 glyph->glyph_not_available_p = 0;
25623 glyph->face_id = it->face_id;
25624 glyph->u.img_id = img->id;
25625 glyph->slice.img = slice;
25626 glyph->font_type = FONT_TYPE_UNKNOWN;
25627 if (it->bidi_p)
25629 glyph->resolved_level = it->bidi_it.resolved_level;
25630 eassert ((it->bidi_it.type & 7) == it->bidi_it.type);
25631 glyph->bidi_type = it->bidi_it.type;
25633 ++it->glyph_row->used[area];
25635 else
25636 IT_EXPAND_MATRIX_WIDTH (it, area);
25641 /* Append a stretch glyph to IT->glyph_row. OBJECT is the source
25642 of the glyph, WIDTH and HEIGHT are the width and height of the
25643 stretch. ASCENT is the ascent of the glyph (0 <= ASCENT <= HEIGHT). */
25645 static void
25646 append_stretch_glyph (struct it *it, Lisp_Object object,
25647 int width, int height, int ascent)
25649 struct glyph *glyph;
25650 enum glyph_row_area area = it->area;
25652 eassert (ascent >= 0 && ascent <= height);
25654 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
25655 if (glyph < it->glyph_row->glyphs[area + 1])
25657 /* If the glyph row is reversed, we need to prepend the glyph
25658 rather than append it. */
25659 if (it->glyph_row->reversed_p && area == TEXT_AREA)
25661 struct glyph *g;
25663 /* Make room for the additional glyph. */
25664 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
25665 g[1] = *g;
25666 glyph = it->glyph_row->glyphs[area];
25668 /* Decrease the width of the first glyph of the row that
25669 begins before first_visible_x (e.g., due to hscroll).
25670 This is so the overall width of the row becomes smaller
25671 by the scroll amount, and the stretch glyph appended by
25672 extend_face_to_end_of_line will be wider, to shift the
25673 row glyphs to the right. (In L2R rows, the corresponding
25674 left-shift effect is accomplished by setting row->x to a
25675 negative value, which won't work with R2L rows.)
25677 This must leave us with a positive value of WIDTH, since
25678 otherwise the call to move_it_in_display_line_to at the
25679 beginning of display_line would have got past the entire
25680 first glyph, and then it->current_x would have been
25681 greater or equal to it->first_visible_x. */
25682 if (it->current_x < it->first_visible_x)
25683 width -= it->first_visible_x - it->current_x;
25684 eassert (width > 0);
25686 glyph->charpos = CHARPOS (it->position);
25687 glyph->object = object;
25688 glyph->pixel_width = width;
25689 glyph->ascent = ascent;
25690 glyph->descent = height - ascent;
25691 glyph->voffset = it->voffset;
25692 glyph->type = STRETCH_GLYPH;
25693 glyph->avoid_cursor_p = it->avoid_cursor_p;
25694 glyph->multibyte_p = it->multibyte_p;
25695 if (it->glyph_row->reversed_p && area == TEXT_AREA)
25697 /* In R2L rows, the left and the right box edges need to be
25698 drawn in reverse direction. */
25699 glyph->right_box_line_p = it->start_of_box_run_p;
25700 glyph->left_box_line_p = it->end_of_box_run_p;
25702 else
25704 glyph->left_box_line_p = it->start_of_box_run_p;
25705 glyph->right_box_line_p = it->end_of_box_run_p;
25707 glyph->overlaps_vertically_p = 0;
25708 glyph->padding_p = 0;
25709 glyph->glyph_not_available_p = 0;
25710 glyph->face_id = it->face_id;
25711 glyph->u.stretch.ascent = ascent;
25712 glyph->u.stretch.height = height;
25713 glyph->slice.img = null_glyph_slice;
25714 glyph->font_type = FONT_TYPE_UNKNOWN;
25715 if (it->bidi_p)
25717 glyph->resolved_level = it->bidi_it.resolved_level;
25718 eassert ((it->bidi_it.type & 7) == it->bidi_it.type);
25719 glyph->bidi_type = it->bidi_it.type;
25721 else
25723 glyph->resolved_level = 0;
25724 glyph->bidi_type = UNKNOWN_BT;
25726 ++it->glyph_row->used[area];
25728 else
25729 IT_EXPAND_MATRIX_WIDTH (it, area);
25732 #endif /* HAVE_WINDOW_SYSTEM */
25734 /* Produce a stretch glyph for iterator IT. IT->object is the value
25735 of the glyph property displayed. The value must be a list
25736 `(space KEYWORD VALUE ...)' with the following KEYWORD/VALUE pairs
25737 being recognized:
25739 1. `:width WIDTH' specifies that the space should be WIDTH *
25740 canonical char width wide. WIDTH may be an integer or floating
25741 point number.
25743 2. `:relative-width FACTOR' specifies that the width of the stretch
25744 should be computed from the width of the first character having the
25745 `glyph' property, and should be FACTOR times that width.
25747 3. `:align-to HPOS' specifies that the space should be wide enough
25748 to reach HPOS, a value in canonical character units.
25750 Exactly one of the above pairs must be present.
25752 4. `:height HEIGHT' specifies that the height of the stretch produced
25753 should be HEIGHT, measured in canonical character units.
25755 5. `:relative-height FACTOR' specifies that the height of the
25756 stretch should be FACTOR times the height of the characters having
25757 the glyph property.
25759 Either none or exactly one of 4 or 5 must be present.
25761 6. `:ascent ASCENT' specifies that ASCENT percent of the height
25762 of the stretch should be used for the ascent of the stretch.
25763 ASCENT must be in the range 0 <= ASCENT <= 100. */
25765 void
25766 produce_stretch_glyph (struct it *it)
25768 /* (space :width WIDTH :height HEIGHT ...) */
25769 Lisp_Object prop, plist;
25770 int width = 0, height = 0, align_to = -1;
25771 int zero_width_ok_p = 0;
25772 double tem;
25773 struct font *font = NULL;
25775 #ifdef HAVE_WINDOW_SYSTEM
25776 int ascent = 0;
25777 int zero_height_ok_p = 0;
25779 if (FRAME_WINDOW_P (it->f))
25781 struct face *face = FACE_FROM_ID (it->f, it->face_id);
25782 font = face->font ? face->font : FRAME_FONT (it->f);
25783 prepare_face_for_display (it->f, face);
25785 #endif
25787 /* List should start with `space'. */
25788 eassert (CONSP (it->object) && EQ (XCAR (it->object), Qspace));
25789 plist = XCDR (it->object);
25791 /* Compute the width of the stretch. */
25792 if ((prop = Fplist_get (plist, QCwidth), !NILP (prop))
25793 && calc_pixel_width_or_height (&tem, it, prop, font, 1, 0))
25795 /* Absolute width `:width WIDTH' specified and valid. */
25796 zero_width_ok_p = 1;
25797 width = (int)tem;
25799 #ifdef HAVE_WINDOW_SYSTEM
25800 else if (FRAME_WINDOW_P (it->f)
25801 && (prop = Fplist_get (plist, QCrelative_width), NUMVAL (prop) > 0))
25803 /* Relative width `:relative-width FACTOR' specified and valid.
25804 Compute the width of the characters having the `glyph'
25805 property. */
25806 struct it it2;
25807 unsigned char *p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
25809 it2 = *it;
25810 if (it->multibyte_p)
25811 it2.c = it2.char_to_display = STRING_CHAR_AND_LENGTH (p, it2.len);
25812 else
25814 it2.c = it2.char_to_display = *p, it2.len = 1;
25815 if (! ASCII_CHAR_P (it2.c))
25816 it2.char_to_display = BYTE8_TO_CHAR (it2.c);
25819 it2.glyph_row = NULL;
25820 it2.what = IT_CHARACTER;
25821 x_produce_glyphs (&it2);
25822 width = NUMVAL (prop) * it2.pixel_width;
25824 #endif /* HAVE_WINDOW_SYSTEM */
25825 else if ((prop = Fplist_get (plist, QCalign_to), !NILP (prop))
25826 && calc_pixel_width_or_height (&tem, it, prop, font, 1, &align_to))
25828 if (it->glyph_row == NULL || !it->glyph_row->mode_line_p)
25829 align_to = (align_to < 0
25831 : align_to - window_box_left_offset (it->w, TEXT_AREA));
25832 else if (align_to < 0)
25833 align_to = window_box_left_offset (it->w, TEXT_AREA);
25834 width = max (0, (int)tem + align_to - it->current_x);
25835 zero_width_ok_p = 1;
25837 else
25838 /* Nothing specified -> width defaults to canonical char width. */
25839 width = FRAME_COLUMN_WIDTH (it->f);
25841 if (width <= 0 && (width < 0 || !zero_width_ok_p))
25842 width = 1;
25844 #ifdef HAVE_WINDOW_SYSTEM
25845 /* Compute height. */
25846 if (FRAME_WINDOW_P (it->f))
25848 if ((prop = Fplist_get (plist, QCheight), !NILP (prop))
25849 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
25851 height = (int)tem;
25852 zero_height_ok_p = 1;
25854 else if (prop = Fplist_get (plist, QCrelative_height),
25855 NUMVAL (prop) > 0)
25856 height = FONT_HEIGHT (font) * NUMVAL (prop);
25857 else
25858 height = FONT_HEIGHT (font);
25860 if (height <= 0 && (height < 0 || !zero_height_ok_p))
25861 height = 1;
25863 /* Compute percentage of height used for ascent. If
25864 `:ascent ASCENT' is present and valid, use that. Otherwise,
25865 derive the ascent from the font in use. */
25866 if (prop = Fplist_get (plist, QCascent),
25867 NUMVAL (prop) > 0 && NUMVAL (prop) <= 100)
25868 ascent = height * NUMVAL (prop) / 100.0;
25869 else if (!NILP (prop)
25870 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
25871 ascent = min (max (0, (int)tem), height);
25872 else
25873 ascent = (height * FONT_BASE (font)) / FONT_HEIGHT (font);
25875 else
25876 #endif /* HAVE_WINDOW_SYSTEM */
25877 height = 1;
25879 if (width > 0 && it->line_wrap != TRUNCATE
25880 && it->current_x + width > it->last_visible_x)
25882 width = it->last_visible_x - it->current_x;
25883 #ifdef HAVE_WINDOW_SYSTEM
25884 /* Subtract one more pixel from the stretch width, but only on
25885 GUI frames, since on a TTY each glyph is one "pixel" wide. */
25886 width -= FRAME_WINDOW_P (it->f);
25887 #endif
25890 if (width > 0 && height > 0 && it->glyph_row)
25892 Lisp_Object o_object = it->object;
25893 Lisp_Object object = it->stack[it->sp - 1].string;
25894 int n = width;
25896 if (!STRINGP (object))
25897 object = it->w->contents;
25898 #ifdef HAVE_WINDOW_SYSTEM
25899 if (FRAME_WINDOW_P (it->f))
25900 append_stretch_glyph (it, object, width, height, ascent);
25901 else
25902 #endif
25904 it->object = object;
25905 it->char_to_display = ' ';
25906 it->pixel_width = it->len = 1;
25907 while (n--)
25908 tty_append_glyph (it);
25909 it->object = o_object;
25913 it->pixel_width = width;
25914 #ifdef HAVE_WINDOW_SYSTEM
25915 if (FRAME_WINDOW_P (it->f))
25917 it->ascent = it->phys_ascent = ascent;
25918 it->descent = it->phys_descent = height - it->ascent;
25919 it->nglyphs = width > 0 && height > 0 ? 1 : 0;
25920 take_vertical_position_into_account (it);
25922 else
25923 #endif
25924 it->nglyphs = width;
25927 /* Get information about special display element WHAT in an
25928 environment described by IT. WHAT is one of IT_TRUNCATION or
25929 IT_CONTINUATION. Maybe produce glyphs for WHAT if IT has a
25930 non-null glyph_row member. This function ensures that fields like
25931 face_id, c, len of IT are left untouched. */
25933 static void
25934 produce_special_glyphs (struct it *it, enum display_element_type what)
25936 struct it temp_it;
25937 Lisp_Object gc;
25938 GLYPH glyph;
25940 temp_it = *it;
25941 temp_it.object = Qnil;
25942 memset (&temp_it.current, 0, sizeof temp_it.current);
25944 if (what == IT_CONTINUATION)
25946 /* Continuation glyph. For R2L lines, we mirror it by hand. */
25947 if (it->bidi_it.paragraph_dir == R2L)
25948 SET_GLYPH_FROM_CHAR (glyph, '/');
25949 else
25950 SET_GLYPH_FROM_CHAR (glyph, '\\');
25951 if (it->dp
25952 && (gc = DISP_CONTINUE_GLYPH (it->dp), GLYPH_CODE_P (gc)))
25954 /* FIXME: Should we mirror GC for R2L lines? */
25955 SET_GLYPH_FROM_GLYPH_CODE (glyph, gc);
25956 spec_glyph_lookup_face (XWINDOW (it->window), &glyph);
25959 else if (what == IT_TRUNCATION)
25961 /* Truncation glyph. */
25962 SET_GLYPH_FROM_CHAR (glyph, '$');
25963 if (it->dp
25964 && (gc = DISP_TRUNC_GLYPH (it->dp), GLYPH_CODE_P (gc)))
25966 /* FIXME: Should we mirror GC for R2L lines? */
25967 SET_GLYPH_FROM_GLYPH_CODE (glyph, gc);
25968 spec_glyph_lookup_face (XWINDOW (it->window), &glyph);
25971 else
25972 emacs_abort ();
25974 #ifdef HAVE_WINDOW_SYSTEM
25975 /* On a GUI frame, when the right fringe (left fringe for R2L rows)
25976 is turned off, we precede the truncation/continuation glyphs by a
25977 stretch glyph whose width is computed such that these special
25978 glyphs are aligned at the window margin, even when very different
25979 fonts are used in different glyph rows. */
25980 if (FRAME_WINDOW_P (temp_it.f)
25981 /* init_iterator calls this with it->glyph_row == NULL, and it
25982 wants only the pixel width of the truncation/continuation
25983 glyphs. */
25984 && temp_it.glyph_row
25985 /* insert_left_trunc_glyphs calls us at the beginning of the
25986 row, and it has its own calculation of the stretch glyph
25987 width. */
25988 && temp_it.glyph_row->used[TEXT_AREA] > 0
25989 && (temp_it.glyph_row->reversed_p
25990 ? WINDOW_LEFT_FRINGE_WIDTH (temp_it.w)
25991 : WINDOW_RIGHT_FRINGE_WIDTH (temp_it.w)) == 0)
25993 int stretch_width = temp_it.last_visible_x - temp_it.current_x;
25995 if (stretch_width > 0)
25997 struct face *face = FACE_FROM_ID (temp_it.f, temp_it.face_id);
25998 struct font *font =
25999 face->font ? face->font : FRAME_FONT (temp_it.f);
26000 int stretch_ascent =
26001 (((temp_it.ascent + temp_it.descent)
26002 * FONT_BASE (font)) / FONT_HEIGHT (font));
26004 append_stretch_glyph (&temp_it, Qnil, stretch_width,
26005 temp_it.ascent + temp_it.descent,
26006 stretch_ascent);
26009 #endif
26011 temp_it.dp = NULL;
26012 temp_it.what = IT_CHARACTER;
26013 temp_it.c = temp_it.char_to_display = GLYPH_CHAR (glyph);
26014 temp_it.face_id = GLYPH_FACE (glyph);
26015 temp_it.len = CHAR_BYTES (temp_it.c);
26017 PRODUCE_GLYPHS (&temp_it);
26018 it->pixel_width = temp_it.pixel_width;
26019 it->nglyphs = temp_it.nglyphs;
26022 #ifdef HAVE_WINDOW_SYSTEM
26024 /* Calculate line-height and line-spacing properties.
26025 An integer value specifies explicit pixel value.
26026 A float value specifies relative value to current face height.
26027 A cons (float . face-name) specifies relative value to
26028 height of specified face font.
26030 Returns height in pixels, or nil. */
26033 static Lisp_Object
26034 calc_line_height_property (struct it *it, Lisp_Object val, struct font *font,
26035 int boff, int override)
26037 Lisp_Object face_name = Qnil;
26038 int ascent, descent, height;
26040 if (NILP (val) || INTEGERP (val) || (override && EQ (val, Qt)))
26041 return val;
26043 if (CONSP (val))
26045 face_name = XCAR (val);
26046 val = XCDR (val);
26047 if (!NUMBERP (val))
26048 val = make_number (1);
26049 if (NILP (face_name))
26051 height = it->ascent + it->descent;
26052 goto scale;
26056 if (NILP (face_name))
26058 font = FRAME_FONT (it->f);
26059 boff = FRAME_BASELINE_OFFSET (it->f);
26061 else if (EQ (face_name, Qt))
26063 override = 0;
26065 else
26067 int face_id;
26068 struct face *face;
26070 face_id = lookup_named_face (it->f, face_name, false);
26071 if (face_id < 0)
26072 return make_number (-1);
26074 face = FACE_FROM_ID (it->f, face_id);
26075 font = face->font;
26076 if (font == NULL)
26077 return make_number (-1);
26078 boff = font->baseline_offset;
26079 if (font->vertical_centering)
26080 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
26083 ascent = FONT_BASE (font) + boff;
26084 descent = FONT_DESCENT (font) - boff;
26086 if (override)
26088 it->override_ascent = ascent;
26089 it->override_descent = descent;
26090 it->override_boff = boff;
26093 height = ascent + descent;
26095 scale:
26096 if (FLOATP (val))
26097 height = (int)(XFLOAT_DATA (val) * height);
26098 else if (INTEGERP (val))
26099 height *= XINT (val);
26101 return make_number (height);
26105 /* Append a glyph for a glyphless character to IT->glyph_row. FACE_ID
26106 is a face ID to be used for the glyph. FOR_NO_FONT is nonzero if
26107 and only if this is for a character for which no font was found.
26109 If the display method (it->glyphless_method) is
26110 GLYPHLESS_DISPLAY_ACRONYM or GLYPHLESS_DISPLAY_HEX_CODE, LEN is a
26111 length of the acronym or the hexadecimal string, UPPER_XOFF and
26112 UPPER_YOFF are pixel offsets for the upper part of the string,
26113 LOWER_XOFF and LOWER_YOFF are for the lower part.
26115 For the other display methods, LEN through LOWER_YOFF are zero. */
26117 static void
26118 append_glyphless_glyph (struct it *it, int face_id, int for_no_font, int len,
26119 short upper_xoff, short upper_yoff,
26120 short lower_xoff, short lower_yoff)
26122 struct glyph *glyph;
26123 enum glyph_row_area area = it->area;
26125 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
26126 if (glyph < it->glyph_row->glyphs[area + 1])
26128 /* If the glyph row is reversed, we need to prepend the glyph
26129 rather than append it. */
26130 if (it->glyph_row->reversed_p && area == TEXT_AREA)
26132 struct glyph *g;
26134 /* Make room for the additional glyph. */
26135 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
26136 g[1] = *g;
26137 glyph = it->glyph_row->glyphs[area];
26139 glyph->charpos = CHARPOS (it->position);
26140 glyph->object = it->object;
26141 glyph->pixel_width = it->pixel_width;
26142 glyph->ascent = it->ascent;
26143 glyph->descent = it->descent;
26144 glyph->voffset = it->voffset;
26145 glyph->type = GLYPHLESS_GLYPH;
26146 glyph->u.glyphless.method = it->glyphless_method;
26147 glyph->u.glyphless.for_no_font = for_no_font;
26148 glyph->u.glyphless.len = len;
26149 glyph->u.glyphless.ch = it->c;
26150 glyph->slice.glyphless.upper_xoff = upper_xoff;
26151 glyph->slice.glyphless.upper_yoff = upper_yoff;
26152 glyph->slice.glyphless.lower_xoff = lower_xoff;
26153 glyph->slice.glyphless.lower_yoff = lower_yoff;
26154 glyph->avoid_cursor_p = it->avoid_cursor_p;
26155 glyph->multibyte_p = it->multibyte_p;
26156 if (it->glyph_row->reversed_p && area == TEXT_AREA)
26158 /* In R2L rows, the left and the right box edges need to be
26159 drawn in reverse direction. */
26160 glyph->right_box_line_p = it->start_of_box_run_p;
26161 glyph->left_box_line_p = it->end_of_box_run_p;
26163 else
26165 glyph->left_box_line_p = it->start_of_box_run_p;
26166 glyph->right_box_line_p = it->end_of_box_run_p;
26168 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
26169 || it->phys_descent > it->descent);
26170 glyph->padding_p = 0;
26171 glyph->glyph_not_available_p = 0;
26172 glyph->face_id = face_id;
26173 glyph->font_type = FONT_TYPE_UNKNOWN;
26174 if (it->bidi_p)
26176 glyph->resolved_level = it->bidi_it.resolved_level;
26177 eassert ((it->bidi_it.type & 7) == it->bidi_it.type);
26178 glyph->bidi_type = it->bidi_it.type;
26180 ++it->glyph_row->used[area];
26182 else
26183 IT_EXPAND_MATRIX_WIDTH (it, area);
26187 /* Produce a glyph for a glyphless character for iterator IT.
26188 IT->glyphless_method specifies which method to use for displaying
26189 the character. See the description of enum
26190 glyphless_display_method in dispextern.h for the detail.
26192 FOR_NO_FONT is nonzero if and only if this is for a character for
26193 which no font was found. ACRONYM, if non-nil, is an acronym string
26194 for the character. */
26196 static void
26197 produce_glyphless_glyph (struct it *it, int for_no_font, Lisp_Object acronym)
26199 int face_id;
26200 struct face *face;
26201 struct font *font;
26202 int base_width, base_height, width, height;
26203 short upper_xoff, upper_yoff, lower_xoff, lower_yoff;
26204 int len;
26206 /* Get the metrics of the base font. We always refer to the current
26207 ASCII face. */
26208 face = FACE_FROM_ID (it->f, it->face_id)->ascii_face;
26209 font = face->font ? face->font : FRAME_FONT (it->f);
26210 it->ascent = FONT_BASE (font) + font->baseline_offset;
26211 it->descent = FONT_DESCENT (font) - font->baseline_offset;
26212 base_height = it->ascent + it->descent;
26213 base_width = font->average_width;
26215 face_id = merge_glyphless_glyph_face (it);
26217 if (it->glyphless_method == GLYPHLESS_DISPLAY_THIN_SPACE)
26219 it->pixel_width = THIN_SPACE_WIDTH;
26220 len = 0;
26221 upper_xoff = upper_yoff = lower_xoff = lower_yoff = 0;
26223 else if (it->glyphless_method == GLYPHLESS_DISPLAY_EMPTY_BOX)
26225 width = CHAR_WIDTH (it->c);
26226 if (width == 0)
26227 width = 1;
26228 else if (width > 4)
26229 width = 4;
26230 it->pixel_width = base_width * width;
26231 len = 0;
26232 upper_xoff = upper_yoff = lower_xoff = lower_yoff = 0;
26234 else
26236 char buf[7];
26237 const char *str;
26238 unsigned int code[6];
26239 int upper_len;
26240 int ascent, descent;
26241 struct font_metrics metrics_upper, metrics_lower;
26243 face = FACE_FROM_ID (it->f, face_id);
26244 font = face->font ? face->font : FRAME_FONT (it->f);
26245 prepare_face_for_display (it->f, face);
26247 if (it->glyphless_method == GLYPHLESS_DISPLAY_ACRONYM)
26249 if (! STRINGP (acronym) && CHAR_TABLE_P (Vglyphless_char_display))
26250 acronym = CHAR_TABLE_REF (Vglyphless_char_display, it->c);
26251 if (CONSP (acronym))
26252 acronym = XCAR (acronym);
26253 str = STRINGP (acronym) ? SSDATA (acronym) : "";
26255 else
26257 eassert (it->glyphless_method == GLYPHLESS_DISPLAY_HEX_CODE);
26258 sprintf (buf, "%0*X", it->c < 0x10000 ? 4 : 6, it->c);
26259 str = buf;
26261 for (len = 0; str[len] && ASCII_CHAR_P (str[len]) && len < 6; len++)
26262 code[len] = font->driver->encode_char (font, str[len]);
26263 upper_len = (len + 1) / 2;
26264 font->driver->text_extents (font, code, upper_len,
26265 &metrics_upper);
26266 font->driver->text_extents (font, code + upper_len, len - upper_len,
26267 &metrics_lower);
26271 /* +4 is for vertical bars of a box plus 1-pixel spaces at both side. */
26272 width = max (metrics_upper.width, metrics_lower.width) + 4;
26273 upper_xoff = upper_yoff = 2; /* the typical case */
26274 if (base_width >= width)
26276 /* Align the upper to the left, the lower to the right. */
26277 it->pixel_width = base_width;
26278 lower_xoff = base_width - 2 - metrics_lower.width;
26280 else
26282 /* Center the shorter one. */
26283 it->pixel_width = width;
26284 if (metrics_upper.width >= metrics_lower.width)
26285 lower_xoff = (width - metrics_lower.width) / 2;
26286 else
26288 /* FIXME: This code doesn't look right. It formerly was
26289 missing the "lower_xoff = 0;", which couldn't have
26290 been right since it left lower_xoff uninitialized. */
26291 lower_xoff = 0;
26292 upper_xoff = (width - metrics_upper.width) / 2;
26296 /* +5 is for horizontal bars of a box plus 1-pixel spaces at
26297 top, bottom, and between upper and lower strings. */
26298 height = (metrics_upper.ascent + metrics_upper.descent
26299 + metrics_lower.ascent + metrics_lower.descent) + 5;
26300 /* Center vertically.
26301 H:base_height, D:base_descent
26302 h:height, ld:lower_descent, la:lower_ascent, ud:upper_descent
26304 ascent = - (D - H/2 - h/2 + 1); "+ 1" for rounding up
26305 descent = D - H/2 + h/2;
26306 lower_yoff = descent - 2 - ld;
26307 upper_yoff = lower_yoff - la - 1 - ud; */
26308 ascent = - (it->descent - (base_height + height + 1) / 2);
26309 descent = it->descent - (base_height - height) / 2;
26310 lower_yoff = descent - 2 - metrics_lower.descent;
26311 upper_yoff = (lower_yoff - metrics_lower.ascent - 1
26312 - metrics_upper.descent);
26313 /* Don't make the height shorter than the base height. */
26314 if (height > base_height)
26316 it->ascent = ascent;
26317 it->descent = descent;
26321 it->phys_ascent = it->ascent;
26322 it->phys_descent = it->descent;
26323 if (it->glyph_row)
26324 append_glyphless_glyph (it, face_id, for_no_font, len,
26325 upper_xoff, upper_yoff,
26326 lower_xoff, lower_yoff);
26327 it->nglyphs = 1;
26328 take_vertical_position_into_account (it);
26332 /* RIF:
26333 Produce glyphs/get display metrics for the display element IT is
26334 loaded with. See the description of struct it in dispextern.h
26335 for an overview of struct it. */
26337 void
26338 x_produce_glyphs (struct it *it)
26340 int extra_line_spacing = it->extra_line_spacing;
26342 it->glyph_not_available_p = 0;
26344 if (it->what == IT_CHARACTER)
26346 XChar2b char2b;
26347 struct face *face = FACE_FROM_ID (it->f, it->face_id);
26348 struct font *font = face->font;
26349 struct font_metrics *pcm = NULL;
26350 int boff; /* Baseline offset. */
26352 if (font == NULL)
26354 /* When no suitable font is found, display this character by
26355 the method specified in the first extra slot of
26356 Vglyphless_char_display. */
26357 Lisp_Object acronym = lookup_glyphless_char_display (-1, it);
26359 eassert (it->what == IT_GLYPHLESS);
26360 produce_glyphless_glyph (it, 1, STRINGP (acronym) ? acronym : Qnil);
26361 goto done;
26364 boff = font->baseline_offset;
26365 if (font->vertical_centering)
26366 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
26368 if (it->char_to_display != '\n' && it->char_to_display != '\t')
26370 int stretched_p;
26372 it->nglyphs = 1;
26374 if (it->override_ascent >= 0)
26376 it->ascent = it->override_ascent;
26377 it->descent = it->override_descent;
26378 boff = it->override_boff;
26380 else
26382 it->ascent = FONT_BASE (font) + boff;
26383 it->descent = FONT_DESCENT (font) - boff;
26386 if (get_char_glyph_code (it->char_to_display, font, &char2b))
26388 pcm = get_per_char_metric (font, &char2b);
26389 if (pcm->width == 0
26390 && pcm->rbearing == 0 && pcm->lbearing == 0)
26391 pcm = NULL;
26394 if (pcm)
26396 it->phys_ascent = pcm->ascent + boff;
26397 it->phys_descent = pcm->descent - boff;
26398 it->pixel_width = pcm->width;
26400 else
26402 it->glyph_not_available_p = 1;
26403 it->phys_ascent = it->ascent;
26404 it->phys_descent = it->descent;
26405 it->pixel_width = font->space_width;
26408 if (it->constrain_row_ascent_descent_p)
26410 if (it->descent > it->max_descent)
26412 it->ascent += it->descent - it->max_descent;
26413 it->descent = it->max_descent;
26415 if (it->ascent > it->max_ascent)
26417 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
26418 it->ascent = it->max_ascent;
26420 it->phys_ascent = min (it->phys_ascent, it->ascent);
26421 it->phys_descent = min (it->phys_descent, it->descent);
26422 extra_line_spacing = 0;
26425 /* If this is a space inside a region of text with
26426 `space-width' property, change its width. */
26427 stretched_p = it->char_to_display == ' ' && !NILP (it->space_width);
26428 if (stretched_p)
26429 it->pixel_width *= XFLOATINT (it->space_width);
26431 /* If face has a box, add the box thickness to the character
26432 height. If character has a box line to the left and/or
26433 right, add the box line width to the character's width. */
26434 if (face->box != FACE_NO_BOX)
26436 int thick = face->box_line_width;
26438 if (thick > 0)
26440 it->ascent += thick;
26441 it->descent += thick;
26443 else
26444 thick = -thick;
26446 if (it->start_of_box_run_p)
26447 it->pixel_width += thick;
26448 if (it->end_of_box_run_p)
26449 it->pixel_width += thick;
26452 /* If face has an overline, add the height of the overline
26453 (1 pixel) and a 1 pixel margin to the character height. */
26454 if (face->overline_p)
26455 it->ascent += overline_margin;
26457 if (it->constrain_row_ascent_descent_p)
26459 if (it->ascent > it->max_ascent)
26460 it->ascent = it->max_ascent;
26461 if (it->descent > it->max_descent)
26462 it->descent = it->max_descent;
26465 take_vertical_position_into_account (it);
26467 /* If we have to actually produce glyphs, do it. */
26468 if (it->glyph_row)
26470 if (stretched_p)
26472 /* Translate a space with a `space-width' property
26473 into a stretch glyph. */
26474 int ascent = (((it->ascent + it->descent) * FONT_BASE (font))
26475 / FONT_HEIGHT (font));
26476 append_stretch_glyph (it, it->object, it->pixel_width,
26477 it->ascent + it->descent, ascent);
26479 else
26480 append_glyph (it);
26482 /* If characters with lbearing or rbearing are displayed
26483 in this line, record that fact in a flag of the
26484 glyph row. This is used to optimize X output code. */
26485 if (pcm && (pcm->lbearing < 0 || pcm->rbearing > pcm->width))
26486 it->glyph_row->contains_overlapping_glyphs_p = 1;
26488 if (! stretched_p && it->pixel_width == 0)
26489 /* We assure that all visible glyphs have at least 1-pixel
26490 width. */
26491 it->pixel_width = 1;
26493 else if (it->char_to_display == '\n')
26495 /* A newline has no width, but we need the height of the
26496 line. But if previous part of the line sets a height,
26497 don't increase that height. */
26499 Lisp_Object height;
26500 Lisp_Object total_height = Qnil;
26502 it->override_ascent = -1;
26503 it->pixel_width = 0;
26504 it->nglyphs = 0;
26506 height = get_it_property (it, Qline_height);
26507 /* Split (line-height total-height) list. */
26508 if (CONSP (height)
26509 && CONSP (XCDR (height))
26510 && NILP (XCDR (XCDR (height))))
26512 total_height = XCAR (XCDR (height));
26513 height = XCAR (height);
26515 height = calc_line_height_property (it, height, font, boff, 1);
26517 if (it->override_ascent >= 0)
26519 it->ascent = it->override_ascent;
26520 it->descent = it->override_descent;
26521 boff = it->override_boff;
26523 else
26525 it->ascent = FONT_BASE (font) + boff;
26526 it->descent = FONT_DESCENT (font) - boff;
26529 if (EQ (height, Qt))
26531 if (it->descent > it->max_descent)
26533 it->ascent += it->descent - it->max_descent;
26534 it->descent = it->max_descent;
26536 if (it->ascent > it->max_ascent)
26538 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
26539 it->ascent = it->max_ascent;
26541 it->phys_ascent = min (it->phys_ascent, it->ascent);
26542 it->phys_descent = min (it->phys_descent, it->descent);
26543 it->constrain_row_ascent_descent_p = 1;
26544 extra_line_spacing = 0;
26546 else
26548 Lisp_Object spacing;
26550 it->phys_ascent = it->ascent;
26551 it->phys_descent = it->descent;
26553 if ((it->max_ascent > 0 || it->max_descent > 0)
26554 && face->box != FACE_NO_BOX
26555 && face->box_line_width > 0)
26557 it->ascent += face->box_line_width;
26558 it->descent += face->box_line_width;
26560 if (!NILP (height)
26561 && XINT (height) > it->ascent + it->descent)
26562 it->ascent = XINT (height) - it->descent;
26564 if (!NILP (total_height))
26565 spacing = calc_line_height_property (it, total_height, font, boff, 0);
26566 else
26568 spacing = get_it_property (it, Qline_spacing);
26569 spacing = calc_line_height_property (it, spacing, font, boff, 0);
26571 if (INTEGERP (spacing))
26573 extra_line_spacing = XINT (spacing);
26574 if (!NILP (total_height))
26575 extra_line_spacing -= (it->phys_ascent + it->phys_descent);
26579 else /* i.e. (it->char_to_display == '\t') */
26581 if (font->space_width > 0)
26583 int tab_width = it->tab_width * font->space_width;
26584 int x = it->current_x + it->continuation_lines_width;
26585 int next_tab_x = ((1 + x + tab_width - 1) / tab_width) * tab_width;
26587 /* If the distance from the current position to the next tab
26588 stop is less than a space character width, use the
26589 tab stop after that. */
26590 if (next_tab_x - x < font->space_width)
26591 next_tab_x += tab_width;
26593 it->pixel_width = next_tab_x - x;
26594 it->nglyphs = 1;
26595 it->ascent = it->phys_ascent = FONT_BASE (font) + boff;
26596 it->descent = it->phys_descent = FONT_DESCENT (font) - boff;
26598 if (it->glyph_row)
26600 append_stretch_glyph (it, it->object, it->pixel_width,
26601 it->ascent + it->descent, it->ascent);
26604 else
26606 it->pixel_width = 0;
26607 it->nglyphs = 1;
26611 else if (it->what == IT_COMPOSITION && it->cmp_it.ch < 0)
26613 /* A static composition.
26615 Note: A composition is represented as one glyph in the
26616 glyph matrix. There are no padding glyphs.
26618 Important note: pixel_width, ascent, and descent are the
26619 values of what is drawn by draw_glyphs (i.e. the values of
26620 the overall glyphs composed). */
26621 struct face *face = FACE_FROM_ID (it->f, it->face_id);
26622 int boff; /* baseline offset */
26623 struct composition *cmp = composition_table[it->cmp_it.id];
26624 int glyph_len = cmp->glyph_len;
26625 struct font *font = face->font;
26627 it->nglyphs = 1;
26629 /* If we have not yet calculated pixel size data of glyphs of
26630 the composition for the current face font, calculate them
26631 now. Theoretically, we have to check all fonts for the
26632 glyphs, but that requires much time and memory space. So,
26633 here we check only the font of the first glyph. This may
26634 lead to incorrect display, but it's very rare, and C-l
26635 (recenter-top-bottom) can correct the display anyway. */
26636 if (! cmp->font || cmp->font != font)
26638 /* Ascent and descent of the font of the first character
26639 of this composition (adjusted by baseline offset).
26640 Ascent and descent of overall glyphs should not be less
26641 than these, respectively. */
26642 int font_ascent, font_descent, font_height;
26643 /* Bounding box of the overall glyphs. */
26644 int leftmost, rightmost, lowest, highest;
26645 int lbearing, rbearing;
26646 int i, width, ascent, descent;
26647 int left_padded = 0, right_padded = 0;
26648 int c IF_LINT (= 0); /* cmp->glyph_len can't be zero; see Bug#8512 */
26649 XChar2b char2b;
26650 struct font_metrics *pcm;
26651 int font_not_found_p;
26652 ptrdiff_t pos;
26654 for (glyph_len = cmp->glyph_len; glyph_len > 0; glyph_len--)
26655 if ((c = COMPOSITION_GLYPH (cmp, glyph_len - 1)) != '\t')
26656 break;
26657 if (glyph_len < cmp->glyph_len)
26658 right_padded = 1;
26659 for (i = 0; i < glyph_len; i++)
26661 if ((c = COMPOSITION_GLYPH (cmp, i)) != '\t')
26662 break;
26663 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
26665 if (i > 0)
26666 left_padded = 1;
26668 pos = (STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
26669 : IT_CHARPOS (*it));
26670 /* If no suitable font is found, use the default font. */
26671 font_not_found_p = font == NULL;
26672 if (font_not_found_p)
26674 face = face->ascii_face;
26675 font = face->font;
26677 boff = font->baseline_offset;
26678 if (font->vertical_centering)
26679 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
26680 font_ascent = FONT_BASE (font) + boff;
26681 font_descent = FONT_DESCENT (font) - boff;
26682 font_height = FONT_HEIGHT (font);
26684 cmp->font = font;
26686 pcm = NULL;
26687 if (! font_not_found_p)
26689 get_char_face_and_encoding (it->f, c, it->face_id,
26690 &char2b, 0);
26691 pcm = get_per_char_metric (font, &char2b);
26694 /* Initialize the bounding box. */
26695 if (pcm)
26697 width = cmp->glyph_len > 0 ? pcm->width : 0;
26698 ascent = pcm->ascent;
26699 descent = pcm->descent;
26700 lbearing = pcm->lbearing;
26701 rbearing = pcm->rbearing;
26703 else
26705 width = cmp->glyph_len > 0 ? font->space_width : 0;
26706 ascent = FONT_BASE (font);
26707 descent = FONT_DESCENT (font);
26708 lbearing = 0;
26709 rbearing = width;
26712 rightmost = width;
26713 leftmost = 0;
26714 lowest = - descent + boff;
26715 highest = ascent + boff;
26717 if (! font_not_found_p
26718 && font->default_ascent
26719 && CHAR_TABLE_P (Vuse_default_ascent)
26720 && !NILP (Faref (Vuse_default_ascent,
26721 make_number (it->char_to_display))))
26722 highest = font->default_ascent + boff;
26724 /* Draw the first glyph at the normal position. It may be
26725 shifted to right later if some other glyphs are drawn
26726 at the left. */
26727 cmp->offsets[i * 2] = 0;
26728 cmp->offsets[i * 2 + 1] = boff;
26729 cmp->lbearing = lbearing;
26730 cmp->rbearing = rbearing;
26732 /* Set cmp->offsets for the remaining glyphs. */
26733 for (i++; i < glyph_len; i++)
26735 int left, right, btm, top;
26736 int ch = COMPOSITION_GLYPH (cmp, i);
26737 int face_id;
26738 struct face *this_face;
26740 if (ch == '\t')
26741 ch = ' ';
26742 face_id = FACE_FOR_CHAR (it->f, face, ch, pos, it->string);
26743 this_face = FACE_FROM_ID (it->f, face_id);
26744 font = this_face->font;
26746 if (font == NULL)
26747 pcm = NULL;
26748 else
26750 get_char_face_and_encoding (it->f, ch, face_id,
26751 &char2b, 0);
26752 pcm = get_per_char_metric (font, &char2b);
26754 if (! pcm)
26755 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
26756 else
26758 width = pcm->width;
26759 ascent = pcm->ascent;
26760 descent = pcm->descent;
26761 lbearing = pcm->lbearing;
26762 rbearing = pcm->rbearing;
26763 if (cmp->method != COMPOSITION_WITH_RULE_ALTCHARS)
26765 /* Relative composition with or without
26766 alternate chars. */
26767 left = (leftmost + rightmost - width) / 2;
26768 btm = - descent + boff;
26769 if (font->relative_compose
26770 && (! CHAR_TABLE_P (Vignore_relative_composition)
26771 || NILP (Faref (Vignore_relative_composition,
26772 make_number (ch)))))
26775 if (- descent >= font->relative_compose)
26776 /* One extra pixel between two glyphs. */
26777 btm = highest + 1;
26778 else if (ascent <= 0)
26779 /* One extra pixel between two glyphs. */
26780 btm = lowest - 1 - ascent - descent;
26783 else
26785 /* A composition rule is specified by an integer
26786 value that encodes global and new reference
26787 points (GREF and NREF). GREF and NREF are
26788 specified by numbers as below:
26790 0---1---2 -- ascent
26794 9--10--11 -- center
26796 ---3---4---5--- baseline
26798 6---7---8 -- descent
26800 int rule = COMPOSITION_RULE (cmp, i);
26801 int gref, nref, grefx, grefy, nrefx, nrefy, xoff, yoff;
26803 COMPOSITION_DECODE_RULE (rule, gref, nref, xoff, yoff);
26804 grefx = gref % 3, nrefx = nref % 3;
26805 grefy = gref / 3, nrefy = nref / 3;
26806 if (xoff)
26807 xoff = font_height * (xoff - 128) / 256;
26808 if (yoff)
26809 yoff = font_height * (yoff - 128) / 256;
26811 left = (leftmost
26812 + grefx * (rightmost - leftmost) / 2
26813 - nrefx * width / 2
26814 + xoff);
26816 btm = ((grefy == 0 ? highest
26817 : grefy == 1 ? 0
26818 : grefy == 2 ? lowest
26819 : (highest + lowest) / 2)
26820 - (nrefy == 0 ? ascent + descent
26821 : nrefy == 1 ? descent - boff
26822 : nrefy == 2 ? 0
26823 : (ascent + descent) / 2)
26824 + yoff);
26827 cmp->offsets[i * 2] = left;
26828 cmp->offsets[i * 2 + 1] = btm + descent;
26830 /* Update the bounding box of the overall glyphs. */
26831 if (width > 0)
26833 right = left + width;
26834 if (left < leftmost)
26835 leftmost = left;
26836 if (right > rightmost)
26837 rightmost = right;
26839 top = btm + descent + ascent;
26840 if (top > highest)
26841 highest = top;
26842 if (btm < lowest)
26843 lowest = btm;
26845 if (cmp->lbearing > left + lbearing)
26846 cmp->lbearing = left + lbearing;
26847 if (cmp->rbearing < left + rbearing)
26848 cmp->rbearing = left + rbearing;
26852 /* If there are glyphs whose x-offsets are negative,
26853 shift all glyphs to the right and make all x-offsets
26854 non-negative. */
26855 if (leftmost < 0)
26857 for (i = 0; i < cmp->glyph_len; i++)
26858 cmp->offsets[i * 2] -= leftmost;
26859 rightmost -= leftmost;
26860 cmp->lbearing -= leftmost;
26861 cmp->rbearing -= leftmost;
26864 if (left_padded && cmp->lbearing < 0)
26866 for (i = 0; i < cmp->glyph_len; i++)
26867 cmp->offsets[i * 2] -= cmp->lbearing;
26868 rightmost -= cmp->lbearing;
26869 cmp->rbearing -= cmp->lbearing;
26870 cmp->lbearing = 0;
26872 if (right_padded && rightmost < cmp->rbearing)
26874 rightmost = cmp->rbearing;
26877 cmp->pixel_width = rightmost;
26878 cmp->ascent = highest;
26879 cmp->descent = - lowest;
26880 if (cmp->ascent < font_ascent)
26881 cmp->ascent = font_ascent;
26882 if (cmp->descent < font_descent)
26883 cmp->descent = font_descent;
26886 if (it->glyph_row
26887 && (cmp->lbearing < 0
26888 || cmp->rbearing > cmp->pixel_width))
26889 it->glyph_row->contains_overlapping_glyphs_p = 1;
26891 it->pixel_width = cmp->pixel_width;
26892 it->ascent = it->phys_ascent = cmp->ascent;
26893 it->descent = it->phys_descent = cmp->descent;
26894 if (face->box != FACE_NO_BOX)
26896 int thick = face->box_line_width;
26898 if (thick > 0)
26900 it->ascent += thick;
26901 it->descent += thick;
26903 else
26904 thick = - thick;
26906 if (it->start_of_box_run_p)
26907 it->pixel_width += thick;
26908 if (it->end_of_box_run_p)
26909 it->pixel_width += thick;
26912 /* If face has an overline, add the height of the overline
26913 (1 pixel) and a 1 pixel margin to the character height. */
26914 if (face->overline_p)
26915 it->ascent += overline_margin;
26917 take_vertical_position_into_account (it);
26918 if (it->ascent < 0)
26919 it->ascent = 0;
26920 if (it->descent < 0)
26921 it->descent = 0;
26923 if (it->glyph_row && cmp->glyph_len > 0)
26924 append_composite_glyph (it);
26926 else if (it->what == IT_COMPOSITION)
26928 /* A dynamic (automatic) composition. */
26929 struct face *face = FACE_FROM_ID (it->f, it->face_id);
26930 Lisp_Object gstring;
26931 struct font_metrics metrics;
26933 it->nglyphs = 1;
26935 gstring = composition_gstring_from_id (it->cmp_it.id);
26936 it->pixel_width
26937 = composition_gstring_width (gstring, it->cmp_it.from, it->cmp_it.to,
26938 &metrics);
26939 if (it->glyph_row
26940 && (metrics.lbearing < 0 || metrics.rbearing > metrics.width))
26941 it->glyph_row->contains_overlapping_glyphs_p = 1;
26942 it->ascent = it->phys_ascent = metrics.ascent;
26943 it->descent = it->phys_descent = metrics.descent;
26944 if (face->box != FACE_NO_BOX)
26946 int thick = face->box_line_width;
26948 if (thick > 0)
26950 it->ascent += thick;
26951 it->descent += thick;
26953 else
26954 thick = - thick;
26956 if (it->start_of_box_run_p)
26957 it->pixel_width += thick;
26958 if (it->end_of_box_run_p)
26959 it->pixel_width += thick;
26961 /* If face has an overline, add the height of the overline
26962 (1 pixel) and a 1 pixel margin to the character height. */
26963 if (face->overline_p)
26964 it->ascent += overline_margin;
26965 take_vertical_position_into_account (it);
26966 if (it->ascent < 0)
26967 it->ascent = 0;
26968 if (it->descent < 0)
26969 it->descent = 0;
26971 if (it->glyph_row)
26972 append_composite_glyph (it);
26974 else if (it->what == IT_GLYPHLESS)
26975 produce_glyphless_glyph (it, 0, Qnil);
26976 else if (it->what == IT_IMAGE)
26977 produce_image_glyph (it);
26978 else if (it->what == IT_STRETCH)
26979 produce_stretch_glyph (it);
26981 done:
26982 /* Accumulate dimensions. Note: can't assume that it->descent > 0
26983 because this isn't true for images with `:ascent 100'. */
26984 eassert (it->ascent >= 0 && it->descent >= 0);
26985 if (it->area == TEXT_AREA)
26986 it->current_x += it->pixel_width;
26988 if (extra_line_spacing > 0)
26990 it->descent += extra_line_spacing;
26991 if (extra_line_spacing > it->max_extra_line_spacing)
26992 it->max_extra_line_spacing = extra_line_spacing;
26995 it->max_ascent = max (it->max_ascent, it->ascent);
26996 it->max_descent = max (it->max_descent, it->descent);
26997 it->max_phys_ascent = max (it->max_phys_ascent, it->phys_ascent);
26998 it->max_phys_descent = max (it->max_phys_descent, it->phys_descent);
27001 /* EXPORT for RIF:
27002 Output LEN glyphs starting at START at the nominal cursor position.
27003 Advance the nominal cursor over the text. UPDATED_ROW is the glyph row
27004 being updated, and UPDATED_AREA is the area of that row being updated. */
27006 void
27007 x_write_glyphs (struct window *w, struct glyph_row *updated_row,
27008 struct glyph *start, enum glyph_row_area updated_area, int len)
27010 int x, hpos, chpos = w->phys_cursor.hpos;
27012 eassert (updated_row);
27013 /* When the window is hscrolled, cursor hpos can legitimately be out
27014 of bounds, but we draw the cursor at the corresponding window
27015 margin in that case. */
27016 if (!updated_row->reversed_p && chpos < 0)
27017 chpos = 0;
27018 if (updated_row->reversed_p && chpos >= updated_row->used[TEXT_AREA])
27019 chpos = updated_row->used[TEXT_AREA] - 1;
27021 block_input ();
27023 /* Write glyphs. */
27025 hpos = start - updated_row->glyphs[updated_area];
27026 x = draw_glyphs (w, w->output_cursor.x,
27027 updated_row, updated_area,
27028 hpos, hpos + len,
27029 DRAW_NORMAL_TEXT, 0);
27031 /* Invalidate old phys cursor if the glyph at its hpos is redrawn. */
27032 if (updated_area == TEXT_AREA
27033 && w->phys_cursor_on_p
27034 && w->phys_cursor.vpos == w->output_cursor.vpos
27035 && chpos >= hpos
27036 && chpos < hpos + len)
27037 w->phys_cursor_on_p = 0;
27039 unblock_input ();
27041 /* Advance the output cursor. */
27042 w->output_cursor.hpos += len;
27043 w->output_cursor.x = x;
27047 /* EXPORT for RIF:
27048 Insert LEN glyphs from START at the nominal cursor position. */
27050 void
27051 x_insert_glyphs (struct window *w, struct glyph_row *updated_row,
27052 struct glyph *start, enum glyph_row_area updated_area, int len)
27054 struct frame *f;
27055 int line_height, shift_by_width, shifted_region_width;
27056 struct glyph_row *row;
27057 struct glyph *glyph;
27058 int frame_x, frame_y;
27059 ptrdiff_t hpos;
27061 eassert (updated_row);
27062 block_input ();
27063 f = XFRAME (WINDOW_FRAME (w));
27065 /* Get the height of the line we are in. */
27066 row = updated_row;
27067 line_height = row->height;
27069 /* Get the width of the glyphs to insert. */
27070 shift_by_width = 0;
27071 for (glyph = start; glyph < start + len; ++glyph)
27072 shift_by_width += glyph->pixel_width;
27074 /* Get the width of the region to shift right. */
27075 shifted_region_width = (window_box_width (w, updated_area)
27076 - w->output_cursor.x
27077 - shift_by_width);
27079 /* Shift right. */
27080 frame_x = window_box_left (w, updated_area) + w->output_cursor.x;
27081 frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, w->output_cursor.y);
27083 FRAME_RIF (f)->shift_glyphs_for_insert (f, frame_x, frame_y, shifted_region_width,
27084 line_height, shift_by_width);
27086 /* Write the glyphs. */
27087 hpos = start - row->glyphs[updated_area];
27088 draw_glyphs (w, w->output_cursor.x, row, updated_area,
27089 hpos, hpos + len,
27090 DRAW_NORMAL_TEXT, 0);
27092 /* Advance the output cursor. */
27093 w->output_cursor.hpos += len;
27094 w->output_cursor.x += shift_by_width;
27095 unblock_input ();
27099 /* EXPORT for RIF:
27100 Erase the current text line from the nominal cursor position
27101 (inclusive) to pixel column TO_X (exclusive). The idea is that
27102 everything from TO_X onward is already erased.
27104 TO_X is a pixel position relative to UPDATED_AREA of currently
27105 updated window W. TO_X == -1 means clear to the end of this area. */
27107 void
27108 x_clear_end_of_line (struct window *w, struct glyph_row *updated_row,
27109 enum glyph_row_area updated_area, int to_x)
27111 struct frame *f;
27112 int max_x, min_y, max_y;
27113 int from_x, from_y, to_y;
27115 eassert (updated_row);
27116 f = XFRAME (w->frame);
27118 if (updated_row->full_width_p)
27119 max_x = (WINDOW_PIXEL_WIDTH (w)
27120 - (updated_row->mode_line_p ? WINDOW_RIGHT_DIVIDER_WIDTH (w) : 0));
27121 else
27122 max_x = window_box_width (w, updated_area);
27123 max_y = window_text_bottom_y (w);
27125 /* TO_X == 0 means don't do anything. TO_X < 0 means clear to end
27126 of window. For TO_X > 0, truncate to end of drawing area. */
27127 if (to_x == 0)
27128 return;
27129 else if (to_x < 0)
27130 to_x = max_x;
27131 else
27132 to_x = min (to_x, max_x);
27134 to_y = min (max_y, w->output_cursor.y + updated_row->height);
27136 /* Notice if the cursor will be cleared by this operation. */
27137 if (!updated_row->full_width_p)
27138 notice_overwritten_cursor (w, updated_area,
27139 w->output_cursor.x, -1,
27140 updated_row->y,
27141 MATRIX_ROW_BOTTOM_Y (updated_row));
27143 from_x = w->output_cursor.x;
27145 /* Translate to frame coordinates. */
27146 if (updated_row->full_width_p)
27148 from_x = WINDOW_TO_FRAME_PIXEL_X (w, from_x);
27149 to_x = WINDOW_TO_FRAME_PIXEL_X (w, to_x);
27151 else
27153 int area_left = window_box_left (w, updated_area);
27154 from_x += area_left;
27155 to_x += area_left;
27158 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
27159 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, max (min_y, w->output_cursor.y));
27160 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, to_y);
27162 /* Prevent inadvertently clearing to end of the X window. */
27163 if (to_x > from_x && to_y > from_y)
27165 block_input ();
27166 FRAME_RIF (f)->clear_frame_area (f, from_x, from_y,
27167 to_x - from_x, to_y - from_y);
27168 unblock_input ();
27172 #endif /* HAVE_WINDOW_SYSTEM */
27176 /***********************************************************************
27177 Cursor types
27178 ***********************************************************************/
27180 /* Value is the internal representation of the specified cursor type
27181 ARG. If type is BAR_CURSOR, return in *WIDTH the specified width
27182 of the bar cursor. */
27184 static enum text_cursor_kinds
27185 get_specified_cursor_type (Lisp_Object arg, int *width)
27187 enum text_cursor_kinds type;
27189 if (NILP (arg))
27190 return NO_CURSOR;
27192 if (EQ (arg, Qbox))
27193 return FILLED_BOX_CURSOR;
27195 if (EQ (arg, Qhollow))
27196 return HOLLOW_BOX_CURSOR;
27198 if (EQ (arg, Qbar))
27200 *width = 2;
27201 return BAR_CURSOR;
27204 if (CONSP (arg)
27205 && EQ (XCAR (arg), Qbar)
27206 && RANGED_INTEGERP (0, XCDR (arg), INT_MAX))
27208 *width = XINT (XCDR (arg));
27209 return BAR_CURSOR;
27212 if (EQ (arg, Qhbar))
27214 *width = 2;
27215 return HBAR_CURSOR;
27218 if (CONSP (arg)
27219 && EQ (XCAR (arg), Qhbar)
27220 && RANGED_INTEGERP (0, XCDR (arg), INT_MAX))
27222 *width = XINT (XCDR (arg));
27223 return HBAR_CURSOR;
27226 /* Treat anything unknown as "hollow box cursor".
27227 It was bad to signal an error; people have trouble fixing
27228 .Xdefaults with Emacs, when it has something bad in it. */
27229 type = HOLLOW_BOX_CURSOR;
27231 return type;
27234 /* Set the default cursor types for specified frame. */
27235 void
27236 set_frame_cursor_types (struct frame *f, Lisp_Object arg)
27238 int width = 1;
27239 Lisp_Object tem;
27241 FRAME_DESIRED_CURSOR (f) = get_specified_cursor_type (arg, &width);
27242 FRAME_CURSOR_WIDTH (f) = width;
27244 /* By default, set up the blink-off state depending on the on-state. */
27246 tem = Fassoc (arg, Vblink_cursor_alist);
27247 if (!NILP (tem))
27249 FRAME_BLINK_OFF_CURSOR (f)
27250 = get_specified_cursor_type (XCDR (tem), &width);
27251 FRAME_BLINK_OFF_CURSOR_WIDTH (f) = width;
27253 else
27254 FRAME_BLINK_OFF_CURSOR (f) = DEFAULT_CURSOR;
27256 /* Make sure the cursor gets redrawn. */
27257 f->cursor_type_changed = 1;
27261 #ifdef HAVE_WINDOW_SYSTEM
27263 /* Return the cursor we want to be displayed in window W. Return
27264 width of bar/hbar cursor through WIDTH arg. Return with
27265 ACTIVE_CURSOR arg set to 1 if cursor in window W is `active'
27266 (i.e. if the `system caret' should track this cursor).
27268 In a mini-buffer window, we want the cursor only to appear if we
27269 are reading input from this window. For the selected window, we
27270 want the cursor type given by the frame parameter or buffer local
27271 setting of cursor-type. If explicitly marked off, draw no cursor.
27272 In all other cases, we want a hollow box cursor. */
27274 static enum text_cursor_kinds
27275 get_window_cursor_type (struct window *w, struct glyph *glyph, int *width,
27276 int *active_cursor)
27278 struct frame *f = XFRAME (w->frame);
27279 struct buffer *b = XBUFFER (w->contents);
27280 int cursor_type = DEFAULT_CURSOR;
27281 Lisp_Object alt_cursor;
27282 int non_selected = 0;
27284 *active_cursor = 1;
27286 /* Echo area */
27287 if (cursor_in_echo_area
27288 && FRAME_HAS_MINIBUF_P (f)
27289 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
27291 if (w == XWINDOW (echo_area_window))
27293 if (EQ (BVAR (b, cursor_type), Qt) || NILP (BVAR (b, cursor_type)))
27295 *width = FRAME_CURSOR_WIDTH (f);
27296 return FRAME_DESIRED_CURSOR (f);
27298 else
27299 return get_specified_cursor_type (BVAR (b, cursor_type), width);
27302 *active_cursor = 0;
27303 non_selected = 1;
27306 /* Detect a nonselected window or nonselected frame. */
27307 else if (w != XWINDOW (f->selected_window)
27308 || f != FRAME_DISPLAY_INFO (f)->x_highlight_frame)
27310 *active_cursor = 0;
27312 if (MINI_WINDOW_P (w) && minibuf_level == 0)
27313 return NO_CURSOR;
27315 non_selected = 1;
27318 /* Never display a cursor in a window in which cursor-type is nil. */
27319 if (NILP (BVAR (b, cursor_type)))
27320 return NO_CURSOR;
27322 /* Get the normal cursor type for this window. */
27323 if (EQ (BVAR (b, cursor_type), Qt))
27325 cursor_type = FRAME_DESIRED_CURSOR (f);
27326 *width = FRAME_CURSOR_WIDTH (f);
27328 else
27329 cursor_type = get_specified_cursor_type (BVAR (b, cursor_type), width);
27331 /* Use cursor-in-non-selected-windows instead
27332 for non-selected window or frame. */
27333 if (non_selected)
27335 alt_cursor = BVAR (b, cursor_in_non_selected_windows);
27336 if (!EQ (Qt, alt_cursor))
27337 return get_specified_cursor_type (alt_cursor, width);
27338 /* t means modify the normal cursor type. */
27339 if (cursor_type == FILLED_BOX_CURSOR)
27340 cursor_type = HOLLOW_BOX_CURSOR;
27341 else if (cursor_type == BAR_CURSOR && *width > 1)
27342 --*width;
27343 return cursor_type;
27346 /* Use normal cursor if not blinked off. */
27347 if (!w->cursor_off_p)
27349 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
27351 if (cursor_type == FILLED_BOX_CURSOR)
27353 /* Using a block cursor on large images can be very annoying.
27354 So use a hollow cursor for "large" images.
27355 If image is not transparent (no mask), also use hollow cursor. */
27356 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
27357 if (img != NULL && IMAGEP (img->spec))
27359 /* Arbitrarily, interpret "Large" as >32x32 and >NxN
27360 where N = size of default frame font size.
27361 This should cover most of the "tiny" icons people may use. */
27362 if (!img->mask
27363 || img->width > max (32, WINDOW_FRAME_COLUMN_WIDTH (w))
27364 || img->height > max (32, WINDOW_FRAME_LINE_HEIGHT (w)))
27365 cursor_type = HOLLOW_BOX_CURSOR;
27368 else if (cursor_type != NO_CURSOR)
27370 /* Display current only supports BOX and HOLLOW cursors for images.
27371 So for now, unconditionally use a HOLLOW cursor when cursor is
27372 not a solid box cursor. */
27373 cursor_type = HOLLOW_BOX_CURSOR;
27376 return cursor_type;
27379 /* Cursor is blinked off, so determine how to "toggle" it. */
27381 /* First look for an entry matching the buffer's cursor-type in blink-cursor-alist. */
27382 if ((alt_cursor = Fassoc (BVAR (b, cursor_type), Vblink_cursor_alist), !NILP (alt_cursor)))
27383 return get_specified_cursor_type (XCDR (alt_cursor), width);
27385 /* Then see if frame has specified a specific blink off cursor type. */
27386 if (FRAME_BLINK_OFF_CURSOR (f) != DEFAULT_CURSOR)
27388 *width = FRAME_BLINK_OFF_CURSOR_WIDTH (f);
27389 return FRAME_BLINK_OFF_CURSOR (f);
27392 #if 0
27393 /* Some people liked having a permanently visible blinking cursor,
27394 while others had very strong opinions against it. So it was
27395 decided to remove it. KFS 2003-09-03 */
27397 /* Finally perform built-in cursor blinking:
27398 filled box <-> hollow box
27399 wide [h]bar <-> narrow [h]bar
27400 narrow [h]bar <-> no cursor
27401 other type <-> no cursor */
27403 if (cursor_type == FILLED_BOX_CURSOR)
27404 return HOLLOW_BOX_CURSOR;
27406 if ((cursor_type == BAR_CURSOR || cursor_type == HBAR_CURSOR) && *width > 1)
27408 *width = 1;
27409 return cursor_type;
27411 #endif
27413 return NO_CURSOR;
27417 /* Notice when the text cursor of window W has been completely
27418 overwritten by a drawing operation that outputs glyphs in AREA
27419 starting at X0 and ending at X1 in the line starting at Y0 and
27420 ending at Y1. X coordinates are area-relative. X1 < 0 means all
27421 the rest of the line after X0 has been written. Y coordinates
27422 are window-relative. */
27424 static void
27425 notice_overwritten_cursor (struct window *w, enum glyph_row_area area,
27426 int x0, int x1, int y0, int y1)
27428 int cx0, cx1, cy0, cy1;
27429 struct glyph_row *row;
27431 if (!w->phys_cursor_on_p)
27432 return;
27433 if (area != TEXT_AREA)
27434 return;
27436 if (w->phys_cursor.vpos < 0
27437 || w->phys_cursor.vpos >= w->current_matrix->nrows
27438 || (row = w->current_matrix->rows + w->phys_cursor.vpos,
27439 !(row->enabled_p && MATRIX_ROW_DISPLAYS_TEXT_P (row))))
27440 return;
27442 if (row->cursor_in_fringe_p)
27444 row->cursor_in_fringe_p = 0;
27445 draw_fringe_bitmap (w, row, row->reversed_p);
27446 w->phys_cursor_on_p = 0;
27447 return;
27450 cx0 = w->phys_cursor.x;
27451 cx1 = cx0 + w->phys_cursor_width;
27452 if (x0 > cx0 || (x1 >= 0 && x1 < cx1))
27453 return;
27455 /* The cursor image will be completely removed from the
27456 screen if the output area intersects the cursor area in
27457 y-direction. When we draw in [y0 y1[, and some part of
27458 the cursor is at y < y0, that part must have been drawn
27459 before. When scrolling, the cursor is erased before
27460 actually scrolling, so we don't come here. When not
27461 scrolling, the rows above the old cursor row must have
27462 changed, and in this case these rows must have written
27463 over the cursor image.
27465 Likewise if part of the cursor is below y1, with the
27466 exception of the cursor being in the first blank row at
27467 the buffer and window end because update_text_area
27468 doesn't draw that row. (Except when it does, but
27469 that's handled in update_text_area.) */
27471 cy0 = w->phys_cursor.y;
27472 cy1 = cy0 + w->phys_cursor_height;
27473 if ((y0 < cy0 || y0 >= cy1) && (y1 <= cy0 || y1 >= cy1))
27474 return;
27476 w->phys_cursor_on_p = 0;
27479 #endif /* HAVE_WINDOW_SYSTEM */
27482 /************************************************************************
27483 Mouse Face
27484 ************************************************************************/
27486 #ifdef HAVE_WINDOW_SYSTEM
27488 /* EXPORT for RIF:
27489 Fix the display of area AREA of overlapping row ROW in window W
27490 with respect to the overlapping part OVERLAPS. */
27492 void
27493 x_fix_overlapping_area (struct window *w, struct glyph_row *row,
27494 enum glyph_row_area area, int overlaps)
27496 int i, x;
27498 block_input ();
27500 x = 0;
27501 for (i = 0; i < row->used[area];)
27503 if (row->glyphs[area][i].overlaps_vertically_p)
27505 int start = i, start_x = x;
27509 x += row->glyphs[area][i].pixel_width;
27510 ++i;
27512 while (i < row->used[area]
27513 && row->glyphs[area][i].overlaps_vertically_p);
27515 draw_glyphs (w, start_x, row, area,
27516 start, i,
27517 DRAW_NORMAL_TEXT, overlaps);
27519 else
27521 x += row->glyphs[area][i].pixel_width;
27522 ++i;
27526 unblock_input ();
27530 /* EXPORT:
27531 Draw the cursor glyph of window W in glyph row ROW. See the
27532 comment of draw_glyphs for the meaning of HL. */
27534 void
27535 draw_phys_cursor_glyph (struct window *w, struct glyph_row *row,
27536 enum draw_glyphs_face hl)
27538 /* If cursor hpos is out of bounds, don't draw garbage. This can
27539 happen in mini-buffer windows when switching between echo area
27540 glyphs and mini-buffer. */
27541 if ((row->reversed_p
27542 ? (w->phys_cursor.hpos >= 0)
27543 : (w->phys_cursor.hpos < row->used[TEXT_AREA])))
27545 int on_p = w->phys_cursor_on_p;
27546 int x1;
27547 int hpos = w->phys_cursor.hpos;
27549 /* When the window is hscrolled, cursor hpos can legitimately be
27550 out of bounds, but we draw the cursor at the corresponding
27551 window margin in that case. */
27552 if (!row->reversed_p && hpos < 0)
27553 hpos = 0;
27554 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
27555 hpos = row->used[TEXT_AREA] - 1;
27557 x1 = draw_glyphs (w, w->phys_cursor.x, row, TEXT_AREA, hpos, hpos + 1,
27558 hl, 0);
27559 w->phys_cursor_on_p = on_p;
27561 if (hl == DRAW_CURSOR)
27562 w->phys_cursor_width = x1 - w->phys_cursor.x;
27563 /* When we erase the cursor, and ROW is overlapped by other
27564 rows, make sure that these overlapping parts of other rows
27565 are redrawn. */
27566 else if (hl == DRAW_NORMAL_TEXT && row->overlapped_p)
27568 w->phys_cursor_width = x1 - w->phys_cursor.x;
27570 if (row > w->current_matrix->rows
27571 && MATRIX_ROW_OVERLAPS_SUCC_P (row - 1))
27572 x_fix_overlapping_area (w, row - 1, TEXT_AREA,
27573 OVERLAPS_ERASED_CURSOR);
27575 if (MATRIX_ROW_BOTTOM_Y (row) < window_text_bottom_y (w)
27576 && MATRIX_ROW_OVERLAPS_PRED_P (row + 1))
27577 x_fix_overlapping_area (w, row + 1, TEXT_AREA,
27578 OVERLAPS_ERASED_CURSOR);
27584 /* Erase the image of a cursor of window W from the screen. */
27586 void
27587 erase_phys_cursor (struct window *w)
27589 struct frame *f = XFRAME (w->frame);
27590 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
27591 int hpos = w->phys_cursor.hpos;
27592 int vpos = w->phys_cursor.vpos;
27593 int mouse_face_here_p = 0;
27594 struct glyph_matrix *active_glyphs = w->current_matrix;
27595 struct glyph_row *cursor_row;
27596 struct glyph *cursor_glyph;
27597 enum draw_glyphs_face hl;
27599 /* No cursor displayed or row invalidated => nothing to do on the
27600 screen. */
27601 if (w->phys_cursor_type == NO_CURSOR)
27602 goto mark_cursor_off;
27604 /* VPOS >= active_glyphs->nrows means that window has been resized.
27605 Don't bother to erase the cursor. */
27606 if (vpos >= active_glyphs->nrows)
27607 goto mark_cursor_off;
27609 /* If row containing cursor is marked invalid, there is nothing we
27610 can do. */
27611 cursor_row = MATRIX_ROW (active_glyphs, vpos);
27612 if (!cursor_row->enabled_p)
27613 goto mark_cursor_off;
27615 /* If line spacing is > 0, old cursor may only be partially visible in
27616 window after split-window. So adjust visible height. */
27617 cursor_row->visible_height = min (cursor_row->visible_height,
27618 window_text_bottom_y (w) - cursor_row->y);
27620 /* If row is completely invisible, don't attempt to delete a cursor which
27621 isn't there. This can happen if cursor is at top of a window, and
27622 we switch to a buffer with a header line in that window. */
27623 if (cursor_row->visible_height <= 0)
27624 goto mark_cursor_off;
27626 /* If cursor is in the fringe, erase by drawing actual bitmap there. */
27627 if (cursor_row->cursor_in_fringe_p)
27629 cursor_row->cursor_in_fringe_p = 0;
27630 draw_fringe_bitmap (w, cursor_row, cursor_row->reversed_p);
27631 goto mark_cursor_off;
27634 /* This can happen when the new row is shorter than the old one.
27635 In this case, either draw_glyphs or clear_end_of_line
27636 should have cleared the cursor. Note that we wouldn't be
27637 able to erase the cursor in this case because we don't have a
27638 cursor glyph at hand. */
27639 if ((cursor_row->reversed_p
27640 ? (w->phys_cursor.hpos < 0)
27641 : (w->phys_cursor.hpos >= cursor_row->used[TEXT_AREA])))
27642 goto mark_cursor_off;
27644 /* When the window is hscrolled, cursor hpos can legitimately be out
27645 of bounds, but we draw the cursor at the corresponding window
27646 margin in that case. */
27647 if (!cursor_row->reversed_p && hpos < 0)
27648 hpos = 0;
27649 if (cursor_row->reversed_p && hpos >= cursor_row->used[TEXT_AREA])
27650 hpos = cursor_row->used[TEXT_AREA] - 1;
27652 /* If the cursor is in the mouse face area, redisplay that when
27653 we clear the cursor. */
27654 if (! NILP (hlinfo->mouse_face_window)
27655 && coords_in_mouse_face_p (w, hpos, vpos)
27656 /* Don't redraw the cursor's spot in mouse face if it is at the
27657 end of a line (on a newline). The cursor appears there, but
27658 mouse highlighting does not. */
27659 && cursor_row->used[TEXT_AREA] > hpos && hpos >= 0)
27660 mouse_face_here_p = 1;
27662 /* Maybe clear the display under the cursor. */
27663 if (w->phys_cursor_type == HOLLOW_BOX_CURSOR)
27665 int x, y;
27666 int header_line_height = WINDOW_HEADER_LINE_HEIGHT (w);
27667 int width;
27669 cursor_glyph = get_phys_cursor_glyph (w);
27670 if (cursor_glyph == NULL)
27671 goto mark_cursor_off;
27673 width = cursor_glyph->pixel_width;
27674 x = w->phys_cursor.x;
27675 if (x < 0)
27677 width += x;
27678 x = 0;
27680 width = min (width, window_box_width (w, TEXT_AREA) - x);
27681 y = WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height, cursor_row->y));
27682 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, x);
27684 if (width > 0)
27685 FRAME_RIF (f)->clear_frame_area (f, x, y, width, cursor_row->visible_height);
27688 /* Erase the cursor by redrawing the character underneath it. */
27689 if (mouse_face_here_p)
27690 hl = DRAW_MOUSE_FACE;
27691 else
27692 hl = DRAW_NORMAL_TEXT;
27693 draw_phys_cursor_glyph (w, cursor_row, hl);
27695 mark_cursor_off:
27696 w->phys_cursor_on_p = 0;
27697 w->phys_cursor_type = NO_CURSOR;
27701 /* EXPORT:
27702 Display or clear cursor of window W. If ON is zero, clear the
27703 cursor. If it is non-zero, display the cursor. If ON is nonzero,
27704 where to put the cursor is specified by HPOS, VPOS, X and Y. */
27706 void
27707 display_and_set_cursor (struct window *w, bool on,
27708 int hpos, int vpos, int x, int y)
27710 struct frame *f = XFRAME (w->frame);
27711 int new_cursor_type;
27712 int new_cursor_width;
27713 int active_cursor;
27714 struct glyph_row *glyph_row;
27715 struct glyph *glyph;
27717 /* This is pointless on invisible frames, and dangerous on garbaged
27718 windows and frames; in the latter case, the frame or window may
27719 be in the midst of changing its size, and x and y may be off the
27720 window. */
27721 if (! FRAME_VISIBLE_P (f)
27722 || FRAME_GARBAGED_P (f)
27723 || vpos >= w->current_matrix->nrows
27724 || hpos >= w->current_matrix->matrix_w)
27725 return;
27727 /* If cursor is off and we want it off, return quickly. */
27728 if (!on && !w->phys_cursor_on_p)
27729 return;
27731 glyph_row = MATRIX_ROW (w->current_matrix, vpos);
27732 /* If cursor row is not enabled, we don't really know where to
27733 display the cursor. */
27734 if (!glyph_row->enabled_p)
27736 w->phys_cursor_on_p = 0;
27737 return;
27740 glyph = NULL;
27741 if (!glyph_row->exact_window_width_line_p
27742 || (0 <= hpos && hpos < glyph_row->used[TEXT_AREA]))
27743 glyph = glyph_row->glyphs[TEXT_AREA] + hpos;
27745 eassert (input_blocked_p ());
27747 /* Set new_cursor_type to the cursor we want to be displayed. */
27748 new_cursor_type = get_window_cursor_type (w, glyph,
27749 &new_cursor_width, &active_cursor);
27751 /* If cursor is currently being shown and we don't want it to be or
27752 it is in the wrong place, or the cursor type is not what we want,
27753 erase it. */
27754 if (w->phys_cursor_on_p
27755 && (!on
27756 || w->phys_cursor.x != x
27757 || w->phys_cursor.y != y
27758 /* HPOS can be negative in R2L rows whose
27759 exact_window_width_line_p flag is set (i.e. their newline
27760 would "overflow into the fringe"). */
27761 || hpos < 0
27762 || new_cursor_type != w->phys_cursor_type
27763 || ((new_cursor_type == BAR_CURSOR || new_cursor_type == HBAR_CURSOR)
27764 && new_cursor_width != w->phys_cursor_width)))
27765 erase_phys_cursor (w);
27767 /* Don't check phys_cursor_on_p here because that flag is only set
27768 to zero in some cases where we know that the cursor has been
27769 completely erased, to avoid the extra work of erasing the cursor
27770 twice. In other words, phys_cursor_on_p can be 1 and the cursor
27771 still not be visible, or it has only been partly erased. */
27772 if (on)
27774 w->phys_cursor_ascent = glyph_row->ascent;
27775 w->phys_cursor_height = glyph_row->height;
27777 /* Set phys_cursor_.* before x_draw_.* is called because some
27778 of them may need the information. */
27779 w->phys_cursor.x = x;
27780 w->phys_cursor.y = glyph_row->y;
27781 w->phys_cursor.hpos = hpos;
27782 w->phys_cursor.vpos = vpos;
27785 FRAME_RIF (f)->draw_window_cursor (w, glyph_row, x, y,
27786 new_cursor_type, new_cursor_width,
27787 on, active_cursor);
27791 /* Switch the display of W's cursor on or off, according to the value
27792 of ON. */
27794 static void
27795 update_window_cursor (struct window *w, bool on)
27797 /* Don't update cursor in windows whose frame is in the process
27798 of being deleted. */
27799 if (w->current_matrix)
27801 int hpos = w->phys_cursor.hpos;
27802 int vpos = w->phys_cursor.vpos;
27803 struct glyph_row *row;
27805 if (vpos >= w->current_matrix->nrows
27806 || hpos >= w->current_matrix->matrix_w)
27807 return;
27809 row = MATRIX_ROW (w->current_matrix, vpos);
27811 /* When the window is hscrolled, cursor hpos can legitimately be
27812 out of bounds, but we draw the cursor at the corresponding
27813 window margin in that case. */
27814 if (!row->reversed_p && hpos < 0)
27815 hpos = 0;
27816 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
27817 hpos = row->used[TEXT_AREA] - 1;
27819 block_input ();
27820 display_and_set_cursor (w, on, hpos, vpos,
27821 w->phys_cursor.x, w->phys_cursor.y);
27822 unblock_input ();
27827 /* Call update_window_cursor with parameter ON_P on all leaf windows
27828 in the window tree rooted at W. */
27830 static void
27831 update_cursor_in_window_tree (struct window *w, bool on_p)
27833 while (w)
27835 if (WINDOWP (w->contents))
27836 update_cursor_in_window_tree (XWINDOW (w->contents), on_p);
27837 else
27838 update_window_cursor (w, on_p);
27840 w = NILP (w->next) ? 0 : XWINDOW (w->next);
27845 /* EXPORT:
27846 Display the cursor on window W, or clear it, according to ON_P.
27847 Don't change the cursor's position. */
27849 void
27850 x_update_cursor (struct frame *f, bool on_p)
27852 update_cursor_in_window_tree (XWINDOW (f->root_window), on_p);
27856 /* EXPORT:
27857 Clear the cursor of window W to background color, and mark the
27858 cursor as not shown. This is used when the text where the cursor
27859 is about to be rewritten. */
27861 void
27862 x_clear_cursor (struct window *w)
27864 if (FRAME_VISIBLE_P (XFRAME (w->frame)) && w->phys_cursor_on_p)
27865 update_window_cursor (w, 0);
27868 #endif /* HAVE_WINDOW_SYSTEM */
27870 /* Implementation of draw_row_with_mouse_face for GUI sessions, GPM,
27871 and MSDOS. */
27872 static void
27873 draw_row_with_mouse_face (struct window *w, int start_x, struct glyph_row *row,
27874 int start_hpos, int end_hpos,
27875 enum draw_glyphs_face draw)
27877 #ifdef HAVE_WINDOW_SYSTEM
27878 if (FRAME_WINDOW_P (XFRAME (w->frame)))
27880 draw_glyphs (w, start_x, row, TEXT_AREA, start_hpos, end_hpos, draw, 0);
27881 return;
27883 #endif
27884 #if defined (HAVE_GPM) || defined (MSDOS) || defined (WINDOWSNT)
27885 tty_draw_row_with_mouse_face (w, row, start_hpos, end_hpos, draw);
27886 #endif
27889 /* Display the active region described by mouse_face_* according to DRAW. */
27891 static void
27892 show_mouse_face (Mouse_HLInfo *hlinfo, enum draw_glyphs_face draw)
27894 struct window *w = XWINDOW (hlinfo->mouse_face_window);
27895 struct frame *f = XFRAME (WINDOW_FRAME (w));
27897 if (/* If window is in the process of being destroyed, don't bother
27898 to do anything. */
27899 w->current_matrix != NULL
27900 /* Don't update mouse highlight if hidden. */
27901 && (draw != DRAW_MOUSE_FACE || !hlinfo->mouse_face_hidden)
27902 /* Recognize when we are called to operate on rows that don't exist
27903 anymore. This can happen when a window is split. */
27904 && hlinfo->mouse_face_end_row < w->current_matrix->nrows)
27906 int phys_cursor_on_p = w->phys_cursor_on_p;
27907 struct glyph_row *row, *first, *last;
27909 first = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_beg_row);
27910 last = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_end_row);
27912 for (row = first; row <= last && row->enabled_p; ++row)
27914 int start_hpos, end_hpos, start_x;
27916 /* For all but the first row, the highlight starts at column 0. */
27917 if (row == first)
27919 /* R2L rows have BEG and END in reversed order, but the
27920 screen drawing geometry is always left to right. So
27921 we need to mirror the beginning and end of the
27922 highlighted area in R2L rows. */
27923 if (!row->reversed_p)
27925 start_hpos = hlinfo->mouse_face_beg_col;
27926 start_x = hlinfo->mouse_face_beg_x;
27928 else if (row == last)
27930 start_hpos = hlinfo->mouse_face_end_col;
27931 start_x = hlinfo->mouse_face_end_x;
27933 else
27935 start_hpos = 0;
27936 start_x = 0;
27939 else if (row->reversed_p && row == last)
27941 start_hpos = hlinfo->mouse_face_end_col;
27942 start_x = hlinfo->mouse_face_end_x;
27944 else
27946 start_hpos = 0;
27947 start_x = 0;
27950 if (row == last)
27952 if (!row->reversed_p)
27953 end_hpos = hlinfo->mouse_face_end_col;
27954 else if (row == first)
27955 end_hpos = hlinfo->mouse_face_beg_col;
27956 else
27958 end_hpos = row->used[TEXT_AREA];
27959 if (draw == DRAW_NORMAL_TEXT)
27960 row->fill_line_p = 1; /* Clear to end of line */
27963 else if (row->reversed_p && row == first)
27964 end_hpos = hlinfo->mouse_face_beg_col;
27965 else
27967 end_hpos = row->used[TEXT_AREA];
27968 if (draw == DRAW_NORMAL_TEXT)
27969 row->fill_line_p = 1; /* Clear to end of line */
27972 if (end_hpos > start_hpos)
27974 draw_row_with_mouse_face (w, start_x, row,
27975 start_hpos, end_hpos, draw);
27977 row->mouse_face_p
27978 = draw == DRAW_MOUSE_FACE || draw == DRAW_IMAGE_RAISED;
27982 #ifdef HAVE_WINDOW_SYSTEM
27983 /* When we've written over the cursor, arrange for it to
27984 be displayed again. */
27985 if (FRAME_WINDOW_P (f)
27986 && phys_cursor_on_p && !w->phys_cursor_on_p)
27988 int hpos = w->phys_cursor.hpos;
27990 /* When the window is hscrolled, cursor hpos can legitimately be
27991 out of bounds, but we draw the cursor at the corresponding
27992 window margin in that case. */
27993 if (!row->reversed_p && hpos < 0)
27994 hpos = 0;
27995 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
27996 hpos = row->used[TEXT_AREA] - 1;
27998 block_input ();
27999 display_and_set_cursor (w, 1, hpos, w->phys_cursor.vpos,
28000 w->phys_cursor.x, w->phys_cursor.y);
28001 unblock_input ();
28003 #endif /* HAVE_WINDOW_SYSTEM */
28006 #ifdef HAVE_WINDOW_SYSTEM
28007 /* Change the mouse cursor. */
28008 if (FRAME_WINDOW_P (f) && NILP (do_mouse_tracking))
28010 #if ! defined (USE_GTK) && ! defined (HAVE_NS)
28011 if (draw == DRAW_NORMAL_TEXT
28012 && !EQ (hlinfo->mouse_face_window, f->tool_bar_window))
28013 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->text_cursor);
28014 else
28015 #endif
28016 if (draw == DRAW_MOUSE_FACE)
28017 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->hand_cursor);
28018 else
28019 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->nontext_cursor);
28021 #endif /* HAVE_WINDOW_SYSTEM */
28024 /* EXPORT:
28025 Clear out the mouse-highlighted active region.
28026 Redraw it un-highlighted first. Value is non-zero if mouse
28027 face was actually drawn unhighlighted. */
28030 clear_mouse_face (Mouse_HLInfo *hlinfo)
28032 int cleared = 0;
28034 if (!hlinfo->mouse_face_hidden && !NILP (hlinfo->mouse_face_window))
28036 show_mouse_face (hlinfo, DRAW_NORMAL_TEXT);
28037 cleared = 1;
28040 hlinfo->mouse_face_beg_row = hlinfo->mouse_face_beg_col = -1;
28041 hlinfo->mouse_face_end_row = hlinfo->mouse_face_end_col = -1;
28042 hlinfo->mouse_face_window = Qnil;
28043 hlinfo->mouse_face_overlay = Qnil;
28044 return cleared;
28047 /* Return true if the coordinates HPOS and VPOS on windows W are
28048 within the mouse face on that window. */
28049 static bool
28050 coords_in_mouse_face_p (struct window *w, int hpos, int vpos)
28052 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (XFRAME (w->frame));
28054 /* Quickly resolve the easy cases. */
28055 if (!(WINDOWP (hlinfo->mouse_face_window)
28056 && XWINDOW (hlinfo->mouse_face_window) == w))
28057 return false;
28058 if (vpos < hlinfo->mouse_face_beg_row
28059 || vpos > hlinfo->mouse_face_end_row)
28060 return false;
28061 if (vpos > hlinfo->mouse_face_beg_row
28062 && vpos < hlinfo->mouse_face_end_row)
28063 return true;
28065 if (!MATRIX_ROW (w->current_matrix, vpos)->reversed_p)
28067 if (hlinfo->mouse_face_beg_row == hlinfo->mouse_face_end_row)
28069 if (hlinfo->mouse_face_beg_col <= hpos && hpos < hlinfo->mouse_face_end_col)
28070 return true;
28072 else if ((vpos == hlinfo->mouse_face_beg_row
28073 && hpos >= hlinfo->mouse_face_beg_col)
28074 || (vpos == hlinfo->mouse_face_end_row
28075 && hpos < hlinfo->mouse_face_end_col))
28076 return true;
28078 else
28080 if (hlinfo->mouse_face_beg_row == hlinfo->mouse_face_end_row)
28082 if (hlinfo->mouse_face_end_col < hpos && hpos <= hlinfo->mouse_face_beg_col)
28083 return true;
28085 else if ((vpos == hlinfo->mouse_face_beg_row
28086 && hpos <= hlinfo->mouse_face_beg_col)
28087 || (vpos == hlinfo->mouse_face_end_row
28088 && hpos > hlinfo->mouse_face_end_col))
28089 return true;
28091 return false;
28095 /* EXPORT:
28096 True if physical cursor of window W is within mouse face. */
28098 bool
28099 cursor_in_mouse_face_p (struct window *w)
28101 int hpos = w->phys_cursor.hpos;
28102 int vpos = w->phys_cursor.vpos;
28103 struct glyph_row *row = MATRIX_ROW (w->current_matrix, vpos);
28105 /* When the window is hscrolled, cursor hpos can legitimately be out
28106 of bounds, but we draw the cursor at the corresponding window
28107 margin in that case. */
28108 if (!row->reversed_p && hpos < 0)
28109 hpos = 0;
28110 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
28111 hpos = row->used[TEXT_AREA] - 1;
28113 return coords_in_mouse_face_p (w, hpos, vpos);
28118 /* Find the glyph rows START_ROW and END_ROW of window W that display
28119 characters between buffer positions START_CHARPOS and END_CHARPOS
28120 (excluding END_CHARPOS). DISP_STRING is a display string that
28121 covers these buffer positions. This is similar to
28122 row_containing_pos, but is more accurate when bidi reordering makes
28123 buffer positions change non-linearly with glyph rows. */
28124 static void
28125 rows_from_pos_range (struct window *w,
28126 ptrdiff_t start_charpos, ptrdiff_t end_charpos,
28127 Lisp_Object disp_string,
28128 struct glyph_row **start, struct glyph_row **end)
28130 struct glyph_row *first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
28131 int last_y = window_text_bottom_y (w);
28132 struct glyph_row *row;
28134 *start = NULL;
28135 *end = NULL;
28137 while (!first->enabled_p
28138 && first < MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w))
28139 first++;
28141 /* Find the START row. */
28142 for (row = first;
28143 row->enabled_p && MATRIX_ROW_BOTTOM_Y (row) <= last_y;
28144 row++)
28146 /* A row can potentially be the START row if the range of the
28147 characters it displays intersects the range
28148 [START_CHARPOS..END_CHARPOS). */
28149 if (! ((start_charpos < MATRIX_ROW_START_CHARPOS (row)
28150 && end_charpos < MATRIX_ROW_START_CHARPOS (row))
28151 /* See the commentary in row_containing_pos, for the
28152 explanation of the complicated way to check whether
28153 some position is beyond the end of the characters
28154 displayed by a row. */
28155 || ((start_charpos > MATRIX_ROW_END_CHARPOS (row)
28156 || (start_charpos == MATRIX_ROW_END_CHARPOS (row)
28157 && !row->ends_at_zv_p
28158 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
28159 && (end_charpos > MATRIX_ROW_END_CHARPOS (row)
28160 || (end_charpos == MATRIX_ROW_END_CHARPOS (row)
28161 && !row->ends_at_zv_p
28162 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))))))
28164 /* Found a candidate row. Now make sure at least one of the
28165 glyphs it displays has a charpos from the range
28166 [START_CHARPOS..END_CHARPOS).
28168 This is not obvious because bidi reordering could make
28169 buffer positions of a row be 1,2,3,102,101,100, and if we
28170 want to highlight characters in [50..60), we don't want
28171 this row, even though [50..60) does intersect [1..103),
28172 the range of character positions given by the row's start
28173 and end positions. */
28174 struct glyph *g = row->glyphs[TEXT_AREA];
28175 struct glyph *e = g + row->used[TEXT_AREA];
28177 while (g < e)
28179 if (((BUFFERP (g->object) || NILP (g->object))
28180 && start_charpos <= g->charpos && g->charpos < end_charpos)
28181 /* A glyph that comes from DISP_STRING is by
28182 definition to be highlighted. */
28183 || EQ (g->object, disp_string))
28184 *start = row;
28185 g++;
28187 if (*start)
28188 break;
28192 /* Find the END row. */
28193 if (!*start
28194 /* If the last row is partially visible, start looking for END
28195 from that row, instead of starting from FIRST. */
28196 && !(row->enabled_p
28197 && row->y < last_y && MATRIX_ROW_BOTTOM_Y (row) > last_y))
28198 row = first;
28199 for ( ; row->enabled_p && MATRIX_ROW_BOTTOM_Y (row) <= last_y; row++)
28201 struct glyph_row *next = row + 1;
28202 ptrdiff_t next_start = MATRIX_ROW_START_CHARPOS (next);
28204 if (!next->enabled_p
28205 || next >= MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w)
28206 /* The first row >= START whose range of displayed characters
28207 does NOT intersect the range [START_CHARPOS..END_CHARPOS]
28208 is the row END + 1. */
28209 || (start_charpos < next_start
28210 && end_charpos < next_start)
28211 || ((start_charpos > MATRIX_ROW_END_CHARPOS (next)
28212 || (start_charpos == MATRIX_ROW_END_CHARPOS (next)
28213 && !next->ends_at_zv_p
28214 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (next)))
28215 && (end_charpos > MATRIX_ROW_END_CHARPOS (next)
28216 || (end_charpos == MATRIX_ROW_END_CHARPOS (next)
28217 && !next->ends_at_zv_p
28218 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (next)))))
28220 *end = row;
28221 break;
28223 else
28225 /* If the next row's edges intersect [START_CHARPOS..END_CHARPOS],
28226 but none of the characters it displays are in the range, it is
28227 also END + 1. */
28228 struct glyph *g = next->glyphs[TEXT_AREA];
28229 struct glyph *s = g;
28230 struct glyph *e = g + next->used[TEXT_AREA];
28232 while (g < e)
28234 if (((BUFFERP (g->object) || NILP (g->object))
28235 && ((start_charpos <= g->charpos && g->charpos < end_charpos)
28236 /* If the buffer position of the first glyph in
28237 the row is equal to END_CHARPOS, it means
28238 the last character to be highlighted is the
28239 newline of ROW, and we must consider NEXT as
28240 END, not END+1. */
28241 || (((!next->reversed_p && g == s)
28242 || (next->reversed_p && g == e - 1))
28243 && (g->charpos == end_charpos
28244 /* Special case for when NEXT is an
28245 empty line at ZV. */
28246 || (g->charpos == -1
28247 && !row->ends_at_zv_p
28248 && next_start == end_charpos)))))
28249 /* A glyph that comes from DISP_STRING is by
28250 definition to be highlighted. */
28251 || EQ (g->object, disp_string))
28252 break;
28253 g++;
28255 if (g == e)
28257 *end = row;
28258 break;
28260 /* The first row that ends at ZV must be the last to be
28261 highlighted. */
28262 else if (next->ends_at_zv_p)
28264 *end = next;
28265 break;
28271 /* This function sets the mouse_face_* elements of HLINFO, assuming
28272 the mouse cursor is on a glyph with buffer charpos MOUSE_CHARPOS in
28273 window WINDOW. START_CHARPOS and END_CHARPOS are buffer positions
28274 for the overlay or run of text properties specifying the mouse
28275 face. BEFORE_STRING and AFTER_STRING, if non-nil, are a
28276 before-string and after-string that must also be highlighted.
28277 DISP_STRING, if non-nil, is a display string that may cover some
28278 or all of the highlighted text. */
28280 static void
28281 mouse_face_from_buffer_pos (Lisp_Object window,
28282 Mouse_HLInfo *hlinfo,
28283 ptrdiff_t mouse_charpos,
28284 ptrdiff_t start_charpos,
28285 ptrdiff_t end_charpos,
28286 Lisp_Object before_string,
28287 Lisp_Object after_string,
28288 Lisp_Object disp_string)
28290 struct window *w = XWINDOW (window);
28291 struct glyph_row *first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
28292 struct glyph_row *r1, *r2;
28293 struct glyph *glyph, *end;
28294 ptrdiff_t ignore, pos;
28295 int x;
28297 eassert (NILP (disp_string) || STRINGP (disp_string));
28298 eassert (NILP (before_string) || STRINGP (before_string));
28299 eassert (NILP (after_string) || STRINGP (after_string));
28301 /* Find the rows corresponding to START_CHARPOS and END_CHARPOS. */
28302 rows_from_pos_range (w, start_charpos, end_charpos, disp_string, &r1, &r2);
28303 if (r1 == NULL)
28304 r1 = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
28305 /* If the before-string or display-string contains newlines,
28306 rows_from_pos_range skips to its last row. Move back. */
28307 if (!NILP (before_string) || !NILP (disp_string))
28309 struct glyph_row *prev;
28310 while ((prev = r1 - 1, prev >= first)
28311 && MATRIX_ROW_END_CHARPOS (prev) == start_charpos
28312 && prev->used[TEXT_AREA] > 0)
28314 struct glyph *beg = prev->glyphs[TEXT_AREA];
28315 glyph = beg + prev->used[TEXT_AREA];
28316 while (--glyph >= beg && NILP (glyph->object));
28317 if (glyph < beg
28318 || !(EQ (glyph->object, before_string)
28319 || EQ (glyph->object, disp_string)))
28320 break;
28321 r1 = prev;
28324 if (r2 == NULL)
28326 r2 = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
28327 hlinfo->mouse_face_past_end = 1;
28329 else if (!NILP (after_string))
28331 /* If the after-string has newlines, advance to its last row. */
28332 struct glyph_row *next;
28333 struct glyph_row *last
28334 = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
28336 for (next = r2 + 1;
28337 next <= last
28338 && next->used[TEXT_AREA] > 0
28339 && EQ (next->glyphs[TEXT_AREA]->object, after_string);
28340 ++next)
28341 r2 = next;
28343 /* The rest of the display engine assumes that mouse_face_beg_row is
28344 either above mouse_face_end_row or identical to it. But with
28345 bidi-reordered continued lines, the row for START_CHARPOS could
28346 be below the row for END_CHARPOS. If so, swap the rows and store
28347 them in correct order. */
28348 if (r1->y > r2->y)
28350 struct glyph_row *tem = r2;
28352 r2 = r1;
28353 r1 = tem;
28356 hlinfo->mouse_face_beg_row = MATRIX_ROW_VPOS (r1, w->current_matrix);
28357 hlinfo->mouse_face_end_row = MATRIX_ROW_VPOS (r2, w->current_matrix);
28359 /* For a bidi-reordered row, the positions of BEFORE_STRING,
28360 AFTER_STRING, DISP_STRING, START_CHARPOS, and END_CHARPOS
28361 could be anywhere in the row and in any order. The strategy
28362 below is to find the leftmost and the rightmost glyph that
28363 belongs to either of these 3 strings, or whose position is
28364 between START_CHARPOS and END_CHARPOS, and highlight all the
28365 glyphs between those two. This may cover more than just the text
28366 between START_CHARPOS and END_CHARPOS if the range of characters
28367 strides the bidi level boundary, e.g. if the beginning is in R2L
28368 text while the end is in L2R text or vice versa. */
28369 if (!r1->reversed_p)
28371 /* This row is in a left to right paragraph. Scan it left to
28372 right. */
28373 glyph = r1->glyphs[TEXT_AREA];
28374 end = glyph + r1->used[TEXT_AREA];
28375 x = r1->x;
28377 /* Skip truncation glyphs at the start of the glyph row. */
28378 if (MATRIX_ROW_DISPLAYS_TEXT_P (r1))
28379 for (; glyph < end
28380 && NILP (glyph->object)
28381 && glyph->charpos < 0;
28382 ++glyph)
28383 x += glyph->pixel_width;
28385 /* Scan the glyph row, looking for BEFORE_STRING, AFTER_STRING,
28386 or DISP_STRING, and the first glyph from buffer whose
28387 position is between START_CHARPOS and END_CHARPOS. */
28388 for (; glyph < end
28389 && !NILP (glyph->object)
28390 && !EQ (glyph->object, disp_string)
28391 && !(BUFFERP (glyph->object)
28392 && (glyph->charpos >= start_charpos
28393 && glyph->charpos < end_charpos));
28394 ++glyph)
28396 /* BEFORE_STRING or AFTER_STRING are only relevant if they
28397 are present at buffer positions between START_CHARPOS and
28398 END_CHARPOS, or if they come from an overlay. */
28399 if (EQ (glyph->object, before_string))
28401 pos = string_buffer_position (before_string,
28402 start_charpos);
28403 /* If pos == 0, it means before_string came from an
28404 overlay, not from a buffer position. */
28405 if (!pos || (pos >= start_charpos && pos < end_charpos))
28406 break;
28408 else if (EQ (glyph->object, after_string))
28410 pos = string_buffer_position (after_string, end_charpos);
28411 if (!pos || (pos >= start_charpos && pos < end_charpos))
28412 break;
28414 x += glyph->pixel_width;
28416 hlinfo->mouse_face_beg_x = x;
28417 hlinfo->mouse_face_beg_col = glyph - r1->glyphs[TEXT_AREA];
28419 else
28421 /* This row is in a right to left paragraph. Scan it right to
28422 left. */
28423 struct glyph *g;
28425 end = r1->glyphs[TEXT_AREA] - 1;
28426 glyph = end + r1->used[TEXT_AREA];
28428 /* Skip truncation glyphs at the start of the glyph row. */
28429 if (MATRIX_ROW_DISPLAYS_TEXT_P (r1))
28430 for (; glyph > end
28431 && NILP (glyph->object)
28432 && glyph->charpos < 0;
28433 --glyph)
28436 /* Scan the glyph row, looking for BEFORE_STRING, AFTER_STRING,
28437 or DISP_STRING, and the first glyph from buffer whose
28438 position is between START_CHARPOS and END_CHARPOS. */
28439 for (; glyph > end
28440 && !NILP (glyph->object)
28441 && !EQ (glyph->object, disp_string)
28442 && !(BUFFERP (glyph->object)
28443 && (glyph->charpos >= start_charpos
28444 && glyph->charpos < end_charpos));
28445 --glyph)
28447 /* BEFORE_STRING or AFTER_STRING are only relevant if they
28448 are present at buffer positions between START_CHARPOS and
28449 END_CHARPOS, or if they come from an overlay. */
28450 if (EQ (glyph->object, before_string))
28452 pos = string_buffer_position (before_string, start_charpos);
28453 /* If pos == 0, it means before_string came from an
28454 overlay, not from a buffer position. */
28455 if (!pos || (pos >= start_charpos && pos < end_charpos))
28456 break;
28458 else if (EQ (glyph->object, after_string))
28460 pos = string_buffer_position (after_string, end_charpos);
28461 if (!pos || (pos >= start_charpos && pos < end_charpos))
28462 break;
28466 glyph++; /* first glyph to the right of the highlighted area */
28467 for (g = r1->glyphs[TEXT_AREA], x = r1->x; g < glyph; g++)
28468 x += g->pixel_width;
28469 hlinfo->mouse_face_beg_x = x;
28470 hlinfo->mouse_face_beg_col = glyph - r1->glyphs[TEXT_AREA];
28473 /* If the highlight ends in a different row, compute GLYPH and END
28474 for the end row. Otherwise, reuse the values computed above for
28475 the row where the highlight begins. */
28476 if (r2 != r1)
28478 if (!r2->reversed_p)
28480 glyph = r2->glyphs[TEXT_AREA];
28481 end = glyph + r2->used[TEXT_AREA];
28482 x = r2->x;
28484 else
28486 end = r2->glyphs[TEXT_AREA] - 1;
28487 glyph = end + r2->used[TEXT_AREA];
28491 if (!r2->reversed_p)
28493 /* Skip truncation and continuation glyphs near the end of the
28494 row, and also blanks and stretch glyphs inserted by
28495 extend_face_to_end_of_line. */
28496 while (end > glyph
28497 && NILP ((end - 1)->object))
28498 --end;
28499 /* Scan the rest of the glyph row from the end, looking for the
28500 first glyph that comes from BEFORE_STRING, AFTER_STRING, or
28501 DISP_STRING, or whose position is between START_CHARPOS
28502 and END_CHARPOS */
28503 for (--end;
28504 end > glyph
28505 && !NILP (end->object)
28506 && !EQ (end->object, disp_string)
28507 && !(BUFFERP (end->object)
28508 && (end->charpos >= start_charpos
28509 && end->charpos < end_charpos));
28510 --end)
28512 /* BEFORE_STRING or AFTER_STRING are only relevant if they
28513 are present at buffer positions between START_CHARPOS and
28514 END_CHARPOS, or if they come from an overlay. */
28515 if (EQ (end->object, before_string))
28517 pos = string_buffer_position (before_string, start_charpos);
28518 if (!pos || (pos >= start_charpos && pos < end_charpos))
28519 break;
28521 else if (EQ (end->object, after_string))
28523 pos = string_buffer_position (after_string, end_charpos);
28524 if (!pos || (pos >= start_charpos && pos < end_charpos))
28525 break;
28528 /* Find the X coordinate of the last glyph to be highlighted. */
28529 for (; glyph <= end; ++glyph)
28530 x += glyph->pixel_width;
28532 hlinfo->mouse_face_end_x = x;
28533 hlinfo->mouse_face_end_col = glyph - r2->glyphs[TEXT_AREA];
28535 else
28537 /* Skip truncation and continuation glyphs near the end of the
28538 row, and also blanks and stretch glyphs inserted by
28539 extend_face_to_end_of_line. */
28540 x = r2->x;
28541 end++;
28542 while (end < glyph
28543 && NILP (end->object))
28545 x += end->pixel_width;
28546 ++end;
28548 /* Scan the rest of the glyph row from the end, looking for the
28549 first glyph that comes from BEFORE_STRING, AFTER_STRING, or
28550 DISP_STRING, or whose position is between START_CHARPOS
28551 and END_CHARPOS */
28552 for ( ;
28553 end < glyph
28554 && !NILP (end->object)
28555 && !EQ (end->object, disp_string)
28556 && !(BUFFERP (end->object)
28557 && (end->charpos >= start_charpos
28558 && end->charpos < end_charpos));
28559 ++end)
28561 /* BEFORE_STRING or AFTER_STRING are only relevant if they
28562 are present at buffer positions between START_CHARPOS and
28563 END_CHARPOS, or if they come from an overlay. */
28564 if (EQ (end->object, before_string))
28566 pos = string_buffer_position (before_string, start_charpos);
28567 if (!pos || (pos >= start_charpos && pos < end_charpos))
28568 break;
28570 else if (EQ (end->object, after_string))
28572 pos = string_buffer_position (after_string, end_charpos);
28573 if (!pos || (pos >= start_charpos && pos < end_charpos))
28574 break;
28576 x += end->pixel_width;
28578 /* If we exited the above loop because we arrived at the last
28579 glyph of the row, and its buffer position is still not in
28580 range, it means the last character in range is the preceding
28581 newline. Bump the end column and x values to get past the
28582 last glyph. */
28583 if (end == glyph
28584 && BUFFERP (end->object)
28585 && (end->charpos < start_charpos
28586 || end->charpos >= end_charpos))
28588 x += end->pixel_width;
28589 ++end;
28591 hlinfo->mouse_face_end_x = x;
28592 hlinfo->mouse_face_end_col = end - r2->glyphs[TEXT_AREA];
28595 hlinfo->mouse_face_window = window;
28596 hlinfo->mouse_face_face_id
28597 = face_at_buffer_position (w, mouse_charpos, &ignore,
28598 mouse_charpos + 1,
28599 !hlinfo->mouse_face_hidden, -1);
28600 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
28603 /* The following function is not used anymore (replaced with
28604 mouse_face_from_string_pos), but I leave it here for the time
28605 being, in case someone would. */
28607 #if 0 /* not used */
28609 /* Find the position of the glyph for position POS in OBJECT in
28610 window W's current matrix, and return in *X, *Y the pixel
28611 coordinates, and return in *HPOS, *VPOS the column/row of the glyph.
28613 RIGHT_P non-zero means return the position of the right edge of the
28614 glyph, RIGHT_P zero means return the left edge position.
28616 If no glyph for POS exists in the matrix, return the position of
28617 the glyph with the next smaller position that is in the matrix, if
28618 RIGHT_P is zero. If RIGHT_P is non-zero, and no glyph for POS
28619 exists in the matrix, return the position of the glyph with the
28620 next larger position in OBJECT.
28622 Value is non-zero if a glyph was found. */
28624 static int
28625 fast_find_string_pos (struct window *w, ptrdiff_t pos, Lisp_Object object,
28626 int *hpos, int *vpos, int *x, int *y, int right_p)
28628 int yb = window_text_bottom_y (w);
28629 struct glyph_row *r;
28630 struct glyph *best_glyph = NULL;
28631 struct glyph_row *best_row = NULL;
28632 int best_x = 0;
28634 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
28635 r->enabled_p && r->y < yb;
28636 ++r)
28638 struct glyph *g = r->glyphs[TEXT_AREA];
28639 struct glyph *e = g + r->used[TEXT_AREA];
28640 int gx;
28642 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
28643 if (EQ (g->object, object))
28645 if (g->charpos == pos)
28647 best_glyph = g;
28648 best_x = gx;
28649 best_row = r;
28650 goto found;
28652 else if (best_glyph == NULL
28653 || ((eabs (g->charpos - pos)
28654 < eabs (best_glyph->charpos - pos))
28655 && (right_p
28656 ? g->charpos < pos
28657 : g->charpos > pos)))
28659 best_glyph = g;
28660 best_x = gx;
28661 best_row = r;
28666 found:
28668 if (best_glyph)
28670 *x = best_x;
28671 *hpos = best_glyph - best_row->glyphs[TEXT_AREA];
28673 if (right_p)
28675 *x += best_glyph->pixel_width;
28676 ++*hpos;
28679 *y = best_row->y;
28680 *vpos = MATRIX_ROW_VPOS (best_row, w->current_matrix);
28683 return best_glyph != NULL;
28685 #endif /* not used */
28687 /* Find the positions of the first and the last glyphs in window W's
28688 current matrix that occlude positions [STARTPOS..ENDPOS) in OBJECT
28689 (assumed to be a string), and return in HLINFO's mouse_face_*
28690 members the pixel and column/row coordinates of those glyphs. */
28692 static void
28693 mouse_face_from_string_pos (struct window *w, Mouse_HLInfo *hlinfo,
28694 Lisp_Object object,
28695 ptrdiff_t startpos, ptrdiff_t endpos)
28697 int yb = window_text_bottom_y (w);
28698 struct glyph_row *r;
28699 struct glyph *g, *e;
28700 int gx;
28701 int found = 0;
28703 /* Find the glyph row with at least one position in the range
28704 [STARTPOS..ENDPOS), and the first glyph in that row whose
28705 position belongs to that range. */
28706 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
28707 r->enabled_p && r->y < yb;
28708 ++r)
28710 if (!r->reversed_p)
28712 g = r->glyphs[TEXT_AREA];
28713 e = g + r->used[TEXT_AREA];
28714 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
28715 if (EQ (g->object, object)
28716 && startpos <= g->charpos && g->charpos < endpos)
28718 hlinfo->mouse_face_beg_row
28719 = MATRIX_ROW_VPOS (r, w->current_matrix);
28720 hlinfo->mouse_face_beg_col = g - r->glyphs[TEXT_AREA];
28721 hlinfo->mouse_face_beg_x = gx;
28722 found = 1;
28723 break;
28726 else
28728 struct glyph *g1;
28730 e = r->glyphs[TEXT_AREA];
28731 g = e + r->used[TEXT_AREA];
28732 for ( ; g > e; --g)
28733 if (EQ ((g-1)->object, object)
28734 && startpos <= (g-1)->charpos && (g-1)->charpos < endpos)
28736 hlinfo->mouse_face_beg_row
28737 = MATRIX_ROW_VPOS (r, w->current_matrix);
28738 hlinfo->mouse_face_beg_col = g - r->glyphs[TEXT_AREA];
28739 for (gx = r->x, g1 = r->glyphs[TEXT_AREA]; g1 < g; ++g1)
28740 gx += g1->pixel_width;
28741 hlinfo->mouse_face_beg_x = gx;
28742 found = 1;
28743 break;
28746 if (found)
28747 break;
28750 if (!found)
28751 return;
28753 /* Starting with the next row, look for the first row which does NOT
28754 include any glyphs whose positions are in the range. */
28755 for (++r; r->enabled_p && r->y < yb; ++r)
28757 g = r->glyphs[TEXT_AREA];
28758 e = g + r->used[TEXT_AREA];
28759 found = 0;
28760 for ( ; g < e; ++g)
28761 if (EQ (g->object, object)
28762 && startpos <= g->charpos && g->charpos < endpos)
28764 found = 1;
28765 break;
28767 if (!found)
28768 break;
28771 /* The highlighted region ends on the previous row. */
28772 r--;
28774 /* Set the end row. */
28775 hlinfo->mouse_face_end_row = MATRIX_ROW_VPOS (r, w->current_matrix);
28777 /* Compute and set the end column and the end column's horizontal
28778 pixel coordinate. */
28779 if (!r->reversed_p)
28781 g = r->glyphs[TEXT_AREA];
28782 e = g + r->used[TEXT_AREA];
28783 for ( ; e > g; --e)
28784 if (EQ ((e-1)->object, object)
28785 && startpos <= (e-1)->charpos && (e-1)->charpos < endpos)
28786 break;
28787 hlinfo->mouse_face_end_col = e - g;
28789 for (gx = r->x; g < e; ++g)
28790 gx += g->pixel_width;
28791 hlinfo->mouse_face_end_x = gx;
28793 else
28795 e = r->glyphs[TEXT_AREA];
28796 g = e + r->used[TEXT_AREA];
28797 for (gx = r->x ; e < g; ++e)
28799 if (EQ (e->object, object)
28800 && startpos <= e->charpos && e->charpos < endpos)
28801 break;
28802 gx += e->pixel_width;
28804 hlinfo->mouse_face_end_col = e - r->glyphs[TEXT_AREA];
28805 hlinfo->mouse_face_end_x = gx;
28809 #ifdef HAVE_WINDOW_SYSTEM
28811 /* See if position X, Y is within a hot-spot of an image. */
28813 static int
28814 on_hot_spot_p (Lisp_Object hot_spot, int x, int y)
28816 if (!CONSP (hot_spot))
28817 return 0;
28819 if (EQ (XCAR (hot_spot), Qrect))
28821 /* CDR is (Top-Left . Bottom-Right) = ((x0 . y0) . (x1 . y1)) */
28822 Lisp_Object rect = XCDR (hot_spot);
28823 Lisp_Object tem;
28824 if (!CONSP (rect))
28825 return 0;
28826 if (!CONSP (XCAR (rect)))
28827 return 0;
28828 if (!CONSP (XCDR (rect)))
28829 return 0;
28830 if (!(tem = XCAR (XCAR (rect)), INTEGERP (tem) && x >= XINT (tem)))
28831 return 0;
28832 if (!(tem = XCDR (XCAR (rect)), INTEGERP (tem) && y >= XINT (tem)))
28833 return 0;
28834 if (!(tem = XCAR (XCDR (rect)), INTEGERP (tem) && x <= XINT (tem)))
28835 return 0;
28836 if (!(tem = XCDR (XCDR (rect)), INTEGERP (tem) && y <= XINT (tem)))
28837 return 0;
28838 return 1;
28840 else if (EQ (XCAR (hot_spot), Qcircle))
28842 /* CDR is (Center . Radius) = ((x0 . y0) . r) */
28843 Lisp_Object circ = XCDR (hot_spot);
28844 Lisp_Object lr, lx0, ly0;
28845 if (CONSP (circ)
28846 && CONSP (XCAR (circ))
28847 && (lr = XCDR (circ), INTEGERP (lr) || FLOATP (lr))
28848 && (lx0 = XCAR (XCAR (circ)), INTEGERP (lx0))
28849 && (ly0 = XCDR (XCAR (circ)), INTEGERP (ly0)))
28851 double r = XFLOATINT (lr);
28852 double dx = XINT (lx0) - x;
28853 double dy = XINT (ly0) - y;
28854 return (dx * dx + dy * dy <= r * r);
28857 else if (EQ (XCAR (hot_spot), Qpoly))
28859 /* CDR is [x0 y0 x1 y1 x2 y2 ...x(n-1) y(n-1)] */
28860 if (VECTORP (XCDR (hot_spot)))
28862 struct Lisp_Vector *v = XVECTOR (XCDR (hot_spot));
28863 Lisp_Object *poly = v->contents;
28864 ptrdiff_t n = v->header.size;
28865 ptrdiff_t i;
28866 int inside = 0;
28867 Lisp_Object lx, ly;
28868 int x0, y0;
28870 /* Need an even number of coordinates, and at least 3 edges. */
28871 if (n < 6 || n & 1)
28872 return 0;
28874 /* Count edge segments intersecting line from (X,Y) to (X,infinity).
28875 If count is odd, we are inside polygon. Pixels on edges
28876 may or may not be included depending on actual geometry of the
28877 polygon. */
28878 if ((lx = poly[n-2], !INTEGERP (lx))
28879 || (ly = poly[n-1], !INTEGERP (lx)))
28880 return 0;
28881 x0 = XINT (lx), y0 = XINT (ly);
28882 for (i = 0; i < n; i += 2)
28884 int x1 = x0, y1 = y0;
28885 if ((lx = poly[i], !INTEGERP (lx))
28886 || (ly = poly[i+1], !INTEGERP (ly)))
28887 return 0;
28888 x0 = XINT (lx), y0 = XINT (ly);
28890 /* Does this segment cross the X line? */
28891 if (x0 >= x)
28893 if (x1 >= x)
28894 continue;
28896 else if (x1 < x)
28897 continue;
28898 if (y > y0 && y > y1)
28899 continue;
28900 if (y < y0 + ((y1 - y0) * (x - x0)) / (x1 - x0))
28901 inside = !inside;
28903 return inside;
28906 return 0;
28909 Lisp_Object
28910 find_hot_spot (Lisp_Object map, int x, int y)
28912 while (CONSP (map))
28914 if (CONSP (XCAR (map))
28915 && on_hot_spot_p (XCAR (XCAR (map)), x, y))
28916 return XCAR (map);
28917 map = XCDR (map);
28920 return Qnil;
28923 DEFUN ("lookup-image-map", Flookup_image_map, Slookup_image_map,
28924 3, 3, 0,
28925 doc: /* Lookup in image map MAP coordinates X and Y.
28926 An image map is an alist where each element has the format (AREA ID PLIST).
28927 An AREA is specified as either a rectangle, a circle, or a polygon:
28928 A rectangle is a cons (rect . ((x0 . y0) . (x1 . y1))) specifying the
28929 pixel coordinates of the upper left and bottom right corners.
28930 A circle is a cons (circle . ((x0 . y0) . r)) specifying the center
28931 and the radius of the circle; r may be a float or integer.
28932 A polygon is a cons (poly . [x0 y0 x1 y1 ...]) where each pair in the
28933 vector describes one corner in the polygon.
28934 Returns the alist element for the first matching AREA in MAP. */)
28935 (Lisp_Object map, Lisp_Object x, Lisp_Object y)
28937 if (NILP (map))
28938 return Qnil;
28940 CHECK_NUMBER (x);
28941 CHECK_NUMBER (y);
28943 return find_hot_spot (map,
28944 clip_to_bounds (INT_MIN, XINT (x), INT_MAX),
28945 clip_to_bounds (INT_MIN, XINT (y), INT_MAX));
28949 /* Display frame CURSOR, optionally using shape defined by POINTER. */
28950 static void
28951 define_frame_cursor1 (struct frame *f, Cursor cursor, Lisp_Object pointer)
28953 /* Do not change cursor shape while dragging mouse. */
28954 if (!NILP (do_mouse_tracking))
28955 return;
28957 if (!NILP (pointer))
28959 if (EQ (pointer, Qarrow))
28960 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
28961 else if (EQ (pointer, Qhand))
28962 cursor = FRAME_X_OUTPUT (f)->hand_cursor;
28963 else if (EQ (pointer, Qtext))
28964 cursor = FRAME_X_OUTPUT (f)->text_cursor;
28965 else if (EQ (pointer, intern ("hdrag")))
28966 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
28967 else if (EQ (pointer, intern ("nhdrag")))
28968 cursor = FRAME_X_OUTPUT (f)->vertical_drag_cursor;
28969 #ifdef HAVE_X_WINDOWS
28970 else if (EQ (pointer, intern ("vdrag")))
28971 cursor = FRAME_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
28972 #endif
28973 else if (EQ (pointer, intern ("hourglass")))
28974 cursor = FRAME_X_OUTPUT (f)->hourglass_cursor;
28975 else if (EQ (pointer, Qmodeline))
28976 cursor = FRAME_X_OUTPUT (f)->modeline_cursor;
28977 else
28978 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
28981 if (cursor != No_Cursor)
28982 FRAME_RIF (f)->define_frame_cursor (f, cursor);
28985 #endif /* HAVE_WINDOW_SYSTEM */
28987 /* Take proper action when mouse has moved to the mode or header line
28988 or marginal area AREA of window W, x-position X and y-position Y.
28989 X is relative to the start of the text display area of W, so the
28990 width of bitmap areas and scroll bars must be subtracted to get a
28991 position relative to the start of the mode line. */
28993 static void
28994 note_mode_line_or_margin_highlight (Lisp_Object window, int x, int y,
28995 enum window_part area)
28997 struct window *w = XWINDOW (window);
28998 struct frame *f = XFRAME (w->frame);
28999 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
29000 #ifdef HAVE_WINDOW_SYSTEM
29001 Display_Info *dpyinfo;
29002 #endif
29003 Cursor cursor = No_Cursor;
29004 Lisp_Object pointer = Qnil;
29005 int dx, dy, width, height;
29006 ptrdiff_t charpos;
29007 Lisp_Object string, object = Qnil;
29008 Lisp_Object pos IF_LINT (= Qnil), help;
29010 Lisp_Object mouse_face;
29011 int original_x_pixel = x;
29012 struct glyph * glyph = NULL, * row_start_glyph = NULL;
29013 struct glyph_row *row IF_LINT (= 0);
29015 if (area == ON_MODE_LINE || area == ON_HEADER_LINE)
29017 int x0;
29018 struct glyph *end;
29020 /* Kludge alert: mode_line_string takes X/Y in pixels, but
29021 returns them in row/column units! */
29022 string = mode_line_string (w, area, &x, &y, &charpos,
29023 &object, &dx, &dy, &width, &height);
29025 row = (area == ON_MODE_LINE
29026 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
29027 : MATRIX_HEADER_LINE_ROW (w->current_matrix));
29029 /* Find the glyph under the mouse pointer. */
29030 if (row->mode_line_p && row->enabled_p)
29032 glyph = row_start_glyph = row->glyphs[TEXT_AREA];
29033 end = glyph + row->used[TEXT_AREA];
29035 for (x0 = original_x_pixel;
29036 glyph < end && x0 >= glyph->pixel_width;
29037 ++glyph)
29038 x0 -= glyph->pixel_width;
29040 if (glyph >= end)
29041 glyph = NULL;
29044 else
29046 x -= WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
29047 /* Kludge alert: marginal_area_string takes X/Y in pixels, but
29048 returns them in row/column units! */
29049 string = marginal_area_string (w, area, &x, &y, &charpos,
29050 &object, &dx, &dy, &width, &height);
29053 help = Qnil;
29055 #ifdef HAVE_WINDOW_SYSTEM
29056 if (IMAGEP (object))
29058 Lisp_Object image_map, hotspot;
29059 if ((image_map = Fplist_get (XCDR (object), QCmap),
29060 !NILP (image_map))
29061 && (hotspot = find_hot_spot (image_map, dx, dy),
29062 CONSP (hotspot))
29063 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
29065 Lisp_Object plist;
29067 /* Could check XCAR (hotspot) to see if we enter/leave this hot-spot.
29068 If so, we could look for mouse-enter, mouse-leave
29069 properties in PLIST (and do something...). */
29070 hotspot = XCDR (hotspot);
29071 if (CONSP (hotspot)
29072 && (plist = XCAR (hotspot), CONSP (plist)))
29074 pointer = Fplist_get (plist, Qpointer);
29075 if (NILP (pointer))
29076 pointer = Qhand;
29077 help = Fplist_get (plist, Qhelp_echo);
29078 if (!NILP (help))
29080 help_echo_string = help;
29081 XSETWINDOW (help_echo_window, w);
29082 help_echo_object = w->contents;
29083 help_echo_pos = charpos;
29087 if (NILP (pointer))
29088 pointer = Fplist_get (XCDR (object), QCpointer);
29090 #endif /* HAVE_WINDOW_SYSTEM */
29092 if (STRINGP (string))
29093 pos = make_number (charpos);
29095 /* Set the help text and mouse pointer. If the mouse is on a part
29096 of the mode line without any text (e.g. past the right edge of
29097 the mode line text), use the default help text and pointer. */
29098 if (STRINGP (string) || area == ON_MODE_LINE)
29100 /* Arrange to display the help by setting the global variables
29101 help_echo_string, help_echo_object, and help_echo_pos. */
29102 if (NILP (help))
29104 if (STRINGP (string))
29105 help = Fget_text_property (pos, Qhelp_echo, string);
29107 if (!NILP (help))
29109 help_echo_string = help;
29110 XSETWINDOW (help_echo_window, w);
29111 help_echo_object = string;
29112 help_echo_pos = charpos;
29114 else if (area == ON_MODE_LINE)
29116 Lisp_Object default_help
29117 = buffer_local_value (Qmode_line_default_help_echo,
29118 w->contents);
29120 if (STRINGP (default_help))
29122 help_echo_string = default_help;
29123 XSETWINDOW (help_echo_window, w);
29124 help_echo_object = Qnil;
29125 help_echo_pos = -1;
29130 #ifdef HAVE_WINDOW_SYSTEM
29131 /* Change the mouse pointer according to what is under it. */
29132 if (FRAME_WINDOW_P (f))
29134 bool draggable = (! WINDOW_BOTTOMMOST_P (w)
29135 || minibuf_level
29136 || NILP (Vresize_mini_windows));
29138 dpyinfo = FRAME_DISPLAY_INFO (f);
29139 if (STRINGP (string))
29141 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
29143 if (NILP (pointer))
29144 pointer = Fget_text_property (pos, Qpointer, string);
29146 /* Change the mouse pointer according to what is under X/Y. */
29147 if (NILP (pointer)
29148 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE)))
29150 Lisp_Object map;
29151 map = Fget_text_property (pos, Qlocal_map, string);
29152 if (!KEYMAPP (map))
29153 map = Fget_text_property (pos, Qkeymap, string);
29154 if (!KEYMAPP (map) && draggable)
29155 cursor = dpyinfo->vertical_scroll_bar_cursor;
29158 else if (draggable)
29159 /* Default mode-line pointer. */
29160 cursor = FRAME_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
29162 #endif
29165 /* Change the mouse face according to what is under X/Y. */
29166 if (STRINGP (string))
29168 mouse_face = Fget_text_property (pos, Qmouse_face, string);
29169 if (!NILP (Vmouse_highlight) && !NILP (mouse_face)
29170 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
29171 && glyph)
29173 Lisp_Object b, e;
29175 struct glyph * tmp_glyph;
29177 int gpos;
29178 int gseq_length;
29179 int total_pixel_width;
29180 ptrdiff_t begpos, endpos, ignore;
29182 int vpos, hpos;
29184 b = Fprevious_single_property_change (make_number (charpos + 1),
29185 Qmouse_face, string, Qnil);
29186 if (NILP (b))
29187 begpos = 0;
29188 else
29189 begpos = XINT (b);
29191 e = Fnext_single_property_change (pos, Qmouse_face, string, Qnil);
29192 if (NILP (e))
29193 endpos = SCHARS (string);
29194 else
29195 endpos = XINT (e);
29197 /* Calculate the glyph position GPOS of GLYPH in the
29198 displayed string, relative to the beginning of the
29199 highlighted part of the string.
29201 Note: GPOS is different from CHARPOS. CHARPOS is the
29202 position of GLYPH in the internal string object. A mode
29203 line string format has structures which are converted to
29204 a flattened string by the Emacs Lisp interpreter. The
29205 internal string is an element of those structures. The
29206 displayed string is the flattened string. */
29207 tmp_glyph = row_start_glyph;
29208 while (tmp_glyph < glyph
29209 && (!(EQ (tmp_glyph->object, glyph->object)
29210 && begpos <= tmp_glyph->charpos
29211 && tmp_glyph->charpos < endpos)))
29212 tmp_glyph++;
29213 gpos = glyph - tmp_glyph;
29215 /* Calculate the length GSEQ_LENGTH of the glyph sequence of
29216 the highlighted part of the displayed string to which
29217 GLYPH belongs. Note: GSEQ_LENGTH is different from
29218 SCHARS (STRING), because the latter returns the length of
29219 the internal string. */
29220 for (tmp_glyph = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
29221 tmp_glyph > glyph
29222 && (!(EQ (tmp_glyph->object, glyph->object)
29223 && begpos <= tmp_glyph->charpos
29224 && tmp_glyph->charpos < endpos));
29225 tmp_glyph--)
29227 gseq_length = gpos + (tmp_glyph - glyph) + 1;
29229 /* Calculate the total pixel width of all the glyphs between
29230 the beginning of the highlighted area and GLYPH. */
29231 total_pixel_width = 0;
29232 for (tmp_glyph = glyph - gpos; tmp_glyph != glyph; tmp_glyph++)
29233 total_pixel_width += tmp_glyph->pixel_width;
29235 /* Pre calculation of re-rendering position. Note: X is in
29236 column units here, after the call to mode_line_string or
29237 marginal_area_string. */
29238 hpos = x - gpos;
29239 vpos = (area == ON_MODE_LINE
29240 ? (w->current_matrix)->nrows - 1
29241 : 0);
29243 /* If GLYPH's position is included in the region that is
29244 already drawn in mouse face, we have nothing to do. */
29245 if ( EQ (window, hlinfo->mouse_face_window)
29246 && (!row->reversed_p
29247 ? (hlinfo->mouse_face_beg_col <= hpos
29248 && hpos < hlinfo->mouse_face_end_col)
29249 /* In R2L rows we swap BEG and END, see below. */
29250 : (hlinfo->mouse_face_end_col <= hpos
29251 && hpos < hlinfo->mouse_face_beg_col))
29252 && hlinfo->mouse_face_beg_row == vpos )
29253 return;
29255 if (clear_mouse_face (hlinfo))
29256 cursor = No_Cursor;
29258 if (!row->reversed_p)
29260 hlinfo->mouse_face_beg_col = hpos;
29261 hlinfo->mouse_face_beg_x = original_x_pixel
29262 - (total_pixel_width + dx);
29263 hlinfo->mouse_face_end_col = hpos + gseq_length;
29264 hlinfo->mouse_face_end_x = 0;
29266 else
29268 /* In R2L rows, show_mouse_face expects BEG and END
29269 coordinates to be swapped. */
29270 hlinfo->mouse_face_end_col = hpos;
29271 hlinfo->mouse_face_end_x = original_x_pixel
29272 - (total_pixel_width + dx);
29273 hlinfo->mouse_face_beg_col = hpos + gseq_length;
29274 hlinfo->mouse_face_beg_x = 0;
29277 hlinfo->mouse_face_beg_row = vpos;
29278 hlinfo->mouse_face_end_row = hlinfo->mouse_face_beg_row;
29279 hlinfo->mouse_face_past_end = 0;
29280 hlinfo->mouse_face_window = window;
29282 hlinfo->mouse_face_face_id = face_at_string_position (w, string,
29283 charpos,
29284 0, &ignore,
29285 glyph->face_id,
29286 true);
29287 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
29289 if (NILP (pointer))
29290 pointer = Qhand;
29292 else if ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
29293 clear_mouse_face (hlinfo);
29295 #ifdef HAVE_WINDOW_SYSTEM
29296 if (FRAME_WINDOW_P (f))
29297 define_frame_cursor1 (f, cursor, pointer);
29298 #endif
29302 /* EXPORT:
29303 Take proper action when the mouse has moved to position X, Y on
29304 frame F with regards to highlighting portions of display that have
29305 mouse-face properties. Also de-highlight portions of display where
29306 the mouse was before, set the mouse pointer shape as appropriate
29307 for the mouse coordinates, and activate help echo (tooltips).
29308 X and Y can be negative or out of range. */
29310 void
29311 note_mouse_highlight (struct frame *f, int x, int y)
29313 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
29314 enum window_part part = ON_NOTHING;
29315 Lisp_Object window;
29316 struct window *w;
29317 Cursor cursor = No_Cursor;
29318 Lisp_Object pointer = Qnil; /* Takes precedence over cursor! */
29319 struct buffer *b;
29321 /* When a menu is active, don't highlight because this looks odd. */
29322 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS) || defined (MSDOS)
29323 if (popup_activated ())
29324 return;
29325 #endif
29327 if (!f->glyphs_initialized_p
29328 || f->pointer_invisible)
29329 return;
29331 hlinfo->mouse_face_mouse_x = x;
29332 hlinfo->mouse_face_mouse_y = y;
29333 hlinfo->mouse_face_mouse_frame = f;
29335 if (hlinfo->mouse_face_defer)
29336 return;
29338 /* Which window is that in? */
29339 window = window_from_coordinates (f, x, y, &part, 1);
29341 /* If displaying active text in another window, clear that. */
29342 if (! EQ (window, hlinfo->mouse_face_window)
29343 /* Also clear if we move out of text area in same window. */
29344 || (!NILP (hlinfo->mouse_face_window)
29345 && !NILP (window)
29346 && part != ON_TEXT
29347 && part != ON_MODE_LINE
29348 && part != ON_HEADER_LINE))
29349 clear_mouse_face (hlinfo);
29351 /* Not on a window -> return. */
29352 if (!WINDOWP (window))
29353 return;
29355 /* Reset help_echo_string. It will get recomputed below. */
29356 help_echo_string = Qnil;
29358 /* Convert to window-relative pixel coordinates. */
29359 w = XWINDOW (window);
29360 frame_to_window_pixel_xy (w, &x, &y);
29362 #if defined (HAVE_WINDOW_SYSTEM) && ! defined (USE_GTK) && ! defined (HAVE_NS)
29363 /* Handle tool-bar window differently since it doesn't display a
29364 buffer. */
29365 if (EQ (window, f->tool_bar_window))
29367 note_tool_bar_highlight (f, x, y);
29368 return;
29370 #endif
29372 /* Mouse is on the mode, header line or margin? */
29373 if (part == ON_MODE_LINE || part == ON_HEADER_LINE
29374 || part == ON_LEFT_MARGIN || part == ON_RIGHT_MARGIN)
29376 note_mode_line_or_margin_highlight (window, x, y, part);
29378 #ifdef HAVE_WINDOW_SYSTEM
29379 if (part == ON_LEFT_MARGIN || part == ON_RIGHT_MARGIN)
29381 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
29382 /* Show non-text cursor (Bug#16647). */
29383 goto set_cursor;
29385 else
29386 #endif
29387 return;
29390 #ifdef HAVE_WINDOW_SYSTEM
29391 if (part == ON_VERTICAL_BORDER)
29393 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
29394 help_echo_string = build_string ("drag-mouse-1: resize");
29396 else if (part == ON_RIGHT_DIVIDER)
29398 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
29399 help_echo_string = build_string ("drag-mouse-1: resize");
29401 else if (part == ON_BOTTOM_DIVIDER)
29402 if (! WINDOW_BOTTOMMOST_P (w)
29403 || minibuf_level
29404 || NILP (Vresize_mini_windows))
29406 cursor = FRAME_X_OUTPUT (f)->vertical_drag_cursor;
29407 help_echo_string = build_string ("drag-mouse-1: resize");
29409 else
29410 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
29411 else if (part == ON_LEFT_FRINGE || part == ON_RIGHT_FRINGE
29412 || part == ON_VERTICAL_SCROLL_BAR
29413 || part == ON_HORIZONTAL_SCROLL_BAR)
29414 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
29415 else
29416 cursor = FRAME_X_OUTPUT (f)->text_cursor;
29417 #endif
29419 /* Are we in a window whose display is up to date?
29420 And verify the buffer's text has not changed. */
29421 b = XBUFFER (w->contents);
29422 if (part == ON_TEXT && w->window_end_valid && !window_outdated (w))
29424 int hpos, vpos, dx, dy, area = LAST_AREA;
29425 ptrdiff_t pos;
29426 struct glyph *glyph;
29427 Lisp_Object object;
29428 Lisp_Object mouse_face = Qnil, position;
29429 Lisp_Object *overlay_vec = NULL;
29430 ptrdiff_t i, noverlays;
29431 struct buffer *obuf;
29432 ptrdiff_t obegv, ozv;
29433 int same_region;
29435 /* Find the glyph under X/Y. */
29436 glyph = x_y_to_hpos_vpos (w, x, y, &hpos, &vpos, &dx, &dy, &area);
29438 #ifdef HAVE_WINDOW_SYSTEM
29439 /* Look for :pointer property on image. */
29440 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
29442 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
29443 if (img != NULL && IMAGEP (img->spec))
29445 Lisp_Object image_map, hotspot;
29446 if ((image_map = Fplist_get (XCDR (img->spec), QCmap),
29447 !NILP (image_map))
29448 && (hotspot = find_hot_spot (image_map,
29449 glyph->slice.img.x + dx,
29450 glyph->slice.img.y + dy),
29451 CONSP (hotspot))
29452 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
29454 Lisp_Object plist;
29456 /* Could check XCAR (hotspot) to see if we enter/leave
29457 this hot-spot.
29458 If so, we could look for mouse-enter, mouse-leave
29459 properties in PLIST (and do something...). */
29460 hotspot = XCDR (hotspot);
29461 if (CONSP (hotspot)
29462 && (plist = XCAR (hotspot), CONSP (plist)))
29464 pointer = Fplist_get (plist, Qpointer);
29465 if (NILP (pointer))
29466 pointer = Qhand;
29467 help_echo_string = Fplist_get (plist, Qhelp_echo);
29468 if (!NILP (help_echo_string))
29470 help_echo_window = window;
29471 help_echo_object = glyph->object;
29472 help_echo_pos = glyph->charpos;
29476 if (NILP (pointer))
29477 pointer = Fplist_get (XCDR (img->spec), QCpointer);
29480 #endif /* HAVE_WINDOW_SYSTEM */
29482 /* Clear mouse face if X/Y not over text. */
29483 if (glyph == NULL
29484 || area != TEXT_AREA
29485 || !MATRIX_ROW_DISPLAYS_TEXT_P (MATRIX_ROW (w->current_matrix, vpos))
29486 /* Glyph's OBJECT is nil for glyphs inserted by the
29487 display engine for its internal purposes, like truncation
29488 and continuation glyphs and blanks beyond the end of
29489 line's text on text terminals. If we are over such a
29490 glyph, we are not over any text. */
29491 || NILP (glyph->object)
29492 /* R2L rows have a stretch glyph at their front, which
29493 stands for no text, whereas L2R rows have no glyphs at
29494 all beyond the end of text. Treat such stretch glyphs
29495 like we do with NULL glyphs in L2R rows. */
29496 || (MATRIX_ROW (w->current_matrix, vpos)->reversed_p
29497 && glyph == MATRIX_ROW_GLYPH_START (w->current_matrix, vpos)
29498 && glyph->type == STRETCH_GLYPH
29499 && glyph->avoid_cursor_p))
29501 if (clear_mouse_face (hlinfo))
29502 cursor = No_Cursor;
29503 #ifdef HAVE_WINDOW_SYSTEM
29504 if (FRAME_WINDOW_P (f) && NILP (pointer))
29506 if (area != TEXT_AREA)
29507 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
29508 else
29509 pointer = Vvoid_text_area_pointer;
29511 #endif
29512 goto set_cursor;
29515 pos = glyph->charpos;
29516 object = glyph->object;
29517 if (!STRINGP (object) && !BUFFERP (object))
29518 goto set_cursor;
29520 /* If we get an out-of-range value, return now; avoid an error. */
29521 if (BUFFERP (object) && pos > BUF_Z (b))
29522 goto set_cursor;
29524 /* Make the window's buffer temporarily current for
29525 overlays_at and compute_char_face. */
29526 obuf = current_buffer;
29527 current_buffer = b;
29528 obegv = BEGV;
29529 ozv = ZV;
29530 BEGV = BEG;
29531 ZV = Z;
29533 /* Is this char mouse-active or does it have help-echo? */
29534 position = make_number (pos);
29536 USE_SAFE_ALLOCA;
29538 if (BUFFERP (object))
29540 /* Put all the overlays we want in a vector in overlay_vec. */
29541 GET_OVERLAYS_AT (pos, overlay_vec, noverlays, NULL, 0);
29542 /* Sort overlays into increasing priority order. */
29543 noverlays = sort_overlays (overlay_vec, noverlays, w);
29545 else
29546 noverlays = 0;
29548 if (NILP (Vmouse_highlight))
29550 clear_mouse_face (hlinfo);
29551 goto check_help_echo;
29554 same_region = coords_in_mouse_face_p (w, hpos, vpos);
29556 if (same_region)
29557 cursor = No_Cursor;
29559 /* Check mouse-face highlighting. */
29560 if (! same_region
29561 /* If there exists an overlay with mouse-face overlapping
29562 the one we are currently highlighting, we have to
29563 check if we enter the overlapping overlay, and then
29564 highlight only that. */
29565 || (OVERLAYP (hlinfo->mouse_face_overlay)
29566 && mouse_face_overlay_overlaps (hlinfo->mouse_face_overlay)))
29568 /* Find the highest priority overlay with a mouse-face. */
29569 Lisp_Object overlay = Qnil;
29570 for (i = noverlays - 1; i >= 0 && NILP (overlay); --i)
29572 mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
29573 if (!NILP (mouse_face))
29574 overlay = overlay_vec[i];
29577 /* If we're highlighting the same overlay as before, there's
29578 no need to do that again. */
29579 if (!NILP (overlay) && EQ (overlay, hlinfo->mouse_face_overlay))
29580 goto check_help_echo;
29581 hlinfo->mouse_face_overlay = overlay;
29583 /* Clear the display of the old active region, if any. */
29584 if (clear_mouse_face (hlinfo))
29585 cursor = No_Cursor;
29587 /* If no overlay applies, get a text property. */
29588 if (NILP (overlay))
29589 mouse_face = Fget_text_property (position, Qmouse_face, object);
29591 /* Next, compute the bounds of the mouse highlighting and
29592 display it. */
29593 if (!NILP (mouse_face) && STRINGP (object))
29595 /* The mouse-highlighting comes from a display string
29596 with a mouse-face. */
29597 Lisp_Object s, e;
29598 ptrdiff_t ignore;
29600 s = Fprevious_single_property_change
29601 (make_number (pos + 1), Qmouse_face, object, Qnil);
29602 e = Fnext_single_property_change
29603 (position, Qmouse_face, object, Qnil);
29604 if (NILP (s))
29605 s = make_number (0);
29606 if (NILP (e))
29607 e = make_number (SCHARS (object));
29608 mouse_face_from_string_pos (w, hlinfo, object,
29609 XINT (s), XINT (e));
29610 hlinfo->mouse_face_past_end = 0;
29611 hlinfo->mouse_face_window = window;
29612 hlinfo->mouse_face_face_id
29613 = face_at_string_position (w, object, pos, 0, &ignore,
29614 glyph->face_id, true);
29615 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
29616 cursor = No_Cursor;
29618 else
29620 /* The mouse-highlighting, if any, comes from an overlay
29621 or text property in the buffer. */
29622 Lisp_Object buffer IF_LINT (= Qnil);
29623 Lisp_Object disp_string IF_LINT (= Qnil);
29625 if (STRINGP (object))
29627 /* If we are on a display string with no mouse-face,
29628 check if the text under it has one. */
29629 struct glyph_row *r = MATRIX_ROW (w->current_matrix, vpos);
29630 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
29631 pos = string_buffer_position (object, start);
29632 if (pos > 0)
29634 mouse_face = get_char_property_and_overlay
29635 (make_number (pos), Qmouse_face, w->contents, &overlay);
29636 buffer = w->contents;
29637 disp_string = object;
29640 else
29642 buffer = object;
29643 disp_string = Qnil;
29646 if (!NILP (mouse_face))
29648 Lisp_Object before, after;
29649 Lisp_Object before_string, after_string;
29650 /* To correctly find the limits of mouse highlight
29651 in a bidi-reordered buffer, we must not use the
29652 optimization of limiting the search in
29653 previous-single-property-change and
29654 next-single-property-change, because
29655 rows_from_pos_range needs the real start and end
29656 positions to DTRT in this case. That's because
29657 the first row visible in a window does not
29658 necessarily display the character whose position
29659 is the smallest. */
29660 Lisp_Object lim1
29661 = NILP (BVAR (XBUFFER (buffer), bidi_display_reordering))
29662 ? Fmarker_position (w->start)
29663 : Qnil;
29664 Lisp_Object lim2
29665 = NILP (BVAR (XBUFFER (buffer), bidi_display_reordering))
29666 ? make_number (BUF_Z (XBUFFER (buffer))
29667 - w->window_end_pos)
29668 : Qnil;
29670 if (NILP (overlay))
29672 /* Handle the text property case. */
29673 before = Fprevious_single_property_change
29674 (make_number (pos + 1), Qmouse_face, buffer, lim1);
29675 after = Fnext_single_property_change
29676 (make_number (pos), Qmouse_face, buffer, lim2);
29677 before_string = after_string = Qnil;
29679 else
29681 /* Handle the overlay case. */
29682 before = Foverlay_start (overlay);
29683 after = Foverlay_end (overlay);
29684 before_string = Foverlay_get (overlay, Qbefore_string);
29685 after_string = Foverlay_get (overlay, Qafter_string);
29687 if (!STRINGP (before_string)) before_string = Qnil;
29688 if (!STRINGP (after_string)) after_string = Qnil;
29691 mouse_face_from_buffer_pos (window, hlinfo, pos,
29692 NILP (before)
29694 : XFASTINT (before),
29695 NILP (after)
29696 ? BUF_Z (XBUFFER (buffer))
29697 : XFASTINT (after),
29698 before_string, after_string,
29699 disp_string);
29700 cursor = No_Cursor;
29705 check_help_echo:
29707 /* Look for a `help-echo' property. */
29708 if (NILP (help_echo_string)) {
29709 Lisp_Object help, overlay;
29711 /* Check overlays first. */
29712 help = overlay = Qnil;
29713 for (i = noverlays - 1; i >= 0 && NILP (help); --i)
29715 overlay = overlay_vec[i];
29716 help = Foverlay_get (overlay, Qhelp_echo);
29719 if (!NILP (help))
29721 help_echo_string = help;
29722 help_echo_window = window;
29723 help_echo_object = overlay;
29724 help_echo_pos = pos;
29726 else
29728 Lisp_Object obj = glyph->object;
29729 ptrdiff_t charpos = glyph->charpos;
29731 /* Try text properties. */
29732 if (STRINGP (obj)
29733 && charpos >= 0
29734 && charpos < SCHARS (obj))
29736 help = Fget_text_property (make_number (charpos),
29737 Qhelp_echo, obj);
29738 if (NILP (help))
29740 /* If the string itself doesn't specify a help-echo,
29741 see if the buffer text ``under'' it does. */
29742 struct glyph_row *r
29743 = MATRIX_ROW (w->current_matrix, vpos);
29744 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
29745 ptrdiff_t p = string_buffer_position (obj, start);
29746 if (p > 0)
29748 help = Fget_char_property (make_number (p),
29749 Qhelp_echo, w->contents);
29750 if (!NILP (help))
29752 charpos = p;
29753 obj = w->contents;
29758 else if (BUFFERP (obj)
29759 && charpos >= BEGV
29760 && charpos < ZV)
29761 help = Fget_text_property (make_number (charpos), Qhelp_echo,
29762 obj);
29764 if (!NILP (help))
29766 help_echo_string = help;
29767 help_echo_window = window;
29768 help_echo_object = obj;
29769 help_echo_pos = charpos;
29774 #ifdef HAVE_WINDOW_SYSTEM
29775 /* Look for a `pointer' property. */
29776 if (FRAME_WINDOW_P (f) && NILP (pointer))
29778 /* Check overlays first. */
29779 for (i = noverlays - 1; i >= 0 && NILP (pointer); --i)
29780 pointer = Foverlay_get (overlay_vec[i], Qpointer);
29782 if (NILP (pointer))
29784 Lisp_Object obj = glyph->object;
29785 ptrdiff_t charpos = glyph->charpos;
29787 /* Try text properties. */
29788 if (STRINGP (obj)
29789 && charpos >= 0
29790 && charpos < SCHARS (obj))
29792 pointer = Fget_text_property (make_number (charpos),
29793 Qpointer, obj);
29794 if (NILP (pointer))
29796 /* If the string itself doesn't specify a pointer,
29797 see if the buffer text ``under'' it does. */
29798 struct glyph_row *r
29799 = MATRIX_ROW (w->current_matrix, vpos);
29800 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
29801 ptrdiff_t p = string_buffer_position (obj, start);
29802 if (p > 0)
29803 pointer = Fget_char_property (make_number (p),
29804 Qpointer, w->contents);
29807 else if (BUFFERP (obj)
29808 && charpos >= BEGV
29809 && charpos < ZV)
29810 pointer = Fget_text_property (make_number (charpos),
29811 Qpointer, obj);
29814 #endif /* HAVE_WINDOW_SYSTEM */
29816 BEGV = obegv;
29817 ZV = ozv;
29818 current_buffer = obuf;
29819 SAFE_FREE ();
29822 set_cursor:
29824 #ifdef HAVE_WINDOW_SYSTEM
29825 if (FRAME_WINDOW_P (f))
29826 define_frame_cursor1 (f, cursor, pointer);
29827 #else
29828 /* This is here to prevent a compiler error, about "label at end of
29829 compound statement". */
29830 return;
29831 #endif
29835 /* EXPORT for RIF:
29836 Clear any mouse-face on window W. This function is part of the
29837 redisplay interface, and is called from try_window_id and similar
29838 functions to ensure the mouse-highlight is off. */
29840 void
29841 x_clear_window_mouse_face (struct window *w)
29843 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (XFRAME (w->frame));
29844 Lisp_Object window;
29846 block_input ();
29847 XSETWINDOW (window, w);
29848 if (EQ (window, hlinfo->mouse_face_window))
29849 clear_mouse_face (hlinfo);
29850 unblock_input ();
29854 /* EXPORT:
29855 Just discard the mouse face information for frame F, if any.
29856 This is used when the size of F is changed. */
29858 void
29859 cancel_mouse_face (struct frame *f)
29861 Lisp_Object window;
29862 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
29864 window = hlinfo->mouse_face_window;
29865 if (! NILP (window) && XFRAME (XWINDOW (window)->frame) == f)
29866 reset_mouse_highlight (hlinfo);
29871 /***********************************************************************
29872 Exposure Events
29873 ***********************************************************************/
29875 #ifdef HAVE_WINDOW_SYSTEM
29877 /* Redraw the part of glyph row area AREA of glyph row ROW on window W
29878 which intersects rectangle R. R is in window-relative coordinates. */
29880 static void
29881 expose_area (struct window *w, struct glyph_row *row, XRectangle *r,
29882 enum glyph_row_area area)
29884 struct glyph *first = row->glyphs[area];
29885 struct glyph *end = row->glyphs[area] + row->used[area];
29886 struct glyph *last;
29887 int first_x, start_x, x;
29889 if (area == TEXT_AREA && row->fill_line_p)
29890 /* If row extends face to end of line write the whole line. */
29891 draw_glyphs (w, 0, row, area,
29892 0, row->used[area],
29893 DRAW_NORMAL_TEXT, 0);
29894 else
29896 /* Set START_X to the window-relative start position for drawing glyphs of
29897 AREA. The first glyph of the text area can be partially visible.
29898 The first glyphs of other areas cannot. */
29899 start_x = window_box_left_offset (w, area);
29900 x = start_x;
29901 if (area == TEXT_AREA)
29902 x += row->x;
29904 /* Find the first glyph that must be redrawn. */
29905 while (first < end
29906 && x + first->pixel_width < r->x)
29908 x += first->pixel_width;
29909 ++first;
29912 /* Find the last one. */
29913 last = first;
29914 first_x = x;
29915 while (last < end
29916 && x < r->x + r->width)
29918 x += last->pixel_width;
29919 ++last;
29922 /* Repaint. */
29923 if (last > first)
29924 draw_glyphs (w, first_x - start_x, row, area,
29925 first - row->glyphs[area], last - row->glyphs[area],
29926 DRAW_NORMAL_TEXT, 0);
29931 /* Redraw the parts of the glyph row ROW on window W intersecting
29932 rectangle R. R is in window-relative coordinates. Value is
29933 non-zero if mouse-face was overwritten. */
29935 static int
29936 expose_line (struct window *w, struct glyph_row *row, XRectangle *r)
29938 eassert (row->enabled_p);
29940 if (row->mode_line_p || w->pseudo_window_p)
29941 draw_glyphs (w, 0, row, TEXT_AREA,
29942 0, row->used[TEXT_AREA],
29943 DRAW_NORMAL_TEXT, 0);
29944 else
29946 if (row->used[LEFT_MARGIN_AREA])
29947 expose_area (w, row, r, LEFT_MARGIN_AREA);
29948 if (row->used[TEXT_AREA])
29949 expose_area (w, row, r, TEXT_AREA);
29950 if (row->used[RIGHT_MARGIN_AREA])
29951 expose_area (w, row, r, RIGHT_MARGIN_AREA);
29952 draw_row_fringe_bitmaps (w, row);
29955 return row->mouse_face_p;
29959 /* Redraw those parts of glyphs rows during expose event handling that
29960 overlap other rows. Redrawing of an exposed line writes over parts
29961 of lines overlapping that exposed line; this function fixes that.
29963 W is the window being exposed. FIRST_OVERLAPPING_ROW is the first
29964 row in W's current matrix that is exposed and overlaps other rows.
29965 LAST_OVERLAPPING_ROW is the last such row. */
29967 static void
29968 expose_overlaps (struct window *w,
29969 struct glyph_row *first_overlapping_row,
29970 struct glyph_row *last_overlapping_row,
29971 XRectangle *r)
29973 struct glyph_row *row;
29975 for (row = first_overlapping_row; row <= last_overlapping_row; ++row)
29976 if (row->overlapping_p)
29978 eassert (row->enabled_p && !row->mode_line_p);
29980 row->clip = r;
29981 if (row->used[LEFT_MARGIN_AREA])
29982 x_fix_overlapping_area (w, row, LEFT_MARGIN_AREA, OVERLAPS_BOTH);
29984 if (row->used[TEXT_AREA])
29985 x_fix_overlapping_area (w, row, TEXT_AREA, OVERLAPS_BOTH);
29987 if (row->used[RIGHT_MARGIN_AREA])
29988 x_fix_overlapping_area (w, row, RIGHT_MARGIN_AREA, OVERLAPS_BOTH);
29989 row->clip = NULL;
29994 /* Return non-zero if W's cursor intersects rectangle R. */
29996 static int
29997 phys_cursor_in_rect_p (struct window *w, XRectangle *r)
29999 XRectangle cr, result;
30000 struct glyph *cursor_glyph;
30001 struct glyph_row *row;
30003 if (w->phys_cursor.vpos >= 0
30004 && w->phys_cursor.vpos < w->current_matrix->nrows
30005 && (row = MATRIX_ROW (w->current_matrix, w->phys_cursor.vpos),
30006 row->enabled_p)
30007 && row->cursor_in_fringe_p)
30009 /* Cursor is in the fringe. */
30010 cr.x = window_box_right_offset (w,
30011 (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
30012 ? RIGHT_MARGIN_AREA
30013 : TEXT_AREA));
30014 cr.y = row->y;
30015 cr.width = WINDOW_RIGHT_FRINGE_WIDTH (w);
30016 cr.height = row->height;
30017 return x_intersect_rectangles (&cr, r, &result);
30020 cursor_glyph = get_phys_cursor_glyph (w);
30021 if (cursor_glyph)
30023 /* r is relative to W's box, but w->phys_cursor.x is relative
30024 to left edge of W's TEXT area. Adjust it. */
30025 cr.x = window_box_left_offset (w, TEXT_AREA) + w->phys_cursor.x;
30026 cr.y = w->phys_cursor.y;
30027 cr.width = cursor_glyph->pixel_width;
30028 cr.height = w->phys_cursor_height;
30029 /* ++KFS: W32 version used W32-specific IntersectRect here, but
30030 I assume the effect is the same -- and this is portable. */
30031 return x_intersect_rectangles (&cr, r, &result);
30033 /* If we don't understand the format, pretend we're not in the hot-spot. */
30034 return 0;
30038 /* EXPORT:
30039 Draw a vertical window border to the right of window W if W doesn't
30040 have vertical scroll bars. */
30042 void
30043 x_draw_vertical_border (struct window *w)
30045 struct frame *f = XFRAME (WINDOW_FRAME (w));
30047 /* We could do better, if we knew what type of scroll-bar the adjacent
30048 windows (on either side) have... But we don't :-(
30049 However, I think this works ok. ++KFS 2003-04-25 */
30051 /* Redraw borders between horizontally adjacent windows. Don't
30052 do it for frames with vertical scroll bars because either the
30053 right scroll bar of a window, or the left scroll bar of its
30054 neighbor will suffice as a border. */
30055 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f) || FRAME_RIGHT_DIVIDER_WIDTH (f))
30056 return;
30058 /* Note: It is necessary to redraw both the left and the right
30059 borders, for when only this single window W is being
30060 redisplayed. */
30061 if (!WINDOW_RIGHTMOST_P (w)
30062 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w))
30064 int x0, x1, y0, y1;
30066 window_box_edges (w, &x0, &y0, &x1, &y1);
30067 y1 -= 1;
30069 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
30070 x1 -= 1;
30072 FRAME_RIF (f)->draw_vertical_window_border (w, x1, y0, y1);
30075 if (!WINDOW_LEFTMOST_P (w)
30076 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w))
30078 int x0, x1, y0, y1;
30080 window_box_edges (w, &x0, &y0, &x1, &y1);
30081 y1 -= 1;
30083 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
30084 x0 -= 1;
30086 FRAME_RIF (f)->draw_vertical_window_border (w, x0, y0, y1);
30091 /* Draw window dividers for window W. */
30093 void
30094 x_draw_right_divider (struct window *w)
30096 struct frame *f = WINDOW_XFRAME (w);
30098 if (w->mini || w->pseudo_window_p)
30099 return;
30100 else if (WINDOW_RIGHT_DIVIDER_WIDTH (w))
30102 int x0 = WINDOW_RIGHT_EDGE_X (w) - WINDOW_RIGHT_DIVIDER_WIDTH (w);
30103 int x1 = WINDOW_RIGHT_EDGE_X (w);
30104 int y0 = WINDOW_TOP_EDGE_Y (w);
30105 /* The bottom divider prevails. */
30106 int y1 = WINDOW_BOTTOM_EDGE_Y (w) - WINDOW_BOTTOM_DIVIDER_WIDTH (w);
30108 FRAME_RIF (f)->draw_window_divider (w, x0, x1, y0, y1);
30112 static void
30113 x_draw_bottom_divider (struct window *w)
30115 struct frame *f = XFRAME (WINDOW_FRAME (w));
30117 if (w->mini || w->pseudo_window_p)
30118 return;
30119 else if (WINDOW_BOTTOM_DIVIDER_WIDTH (w))
30121 int x0 = WINDOW_LEFT_EDGE_X (w);
30122 int x1 = WINDOW_RIGHT_EDGE_X (w);
30123 int y0 = WINDOW_BOTTOM_EDGE_Y (w) - WINDOW_BOTTOM_DIVIDER_WIDTH (w);
30124 int y1 = WINDOW_BOTTOM_EDGE_Y (w);
30126 FRAME_RIF (f)->draw_window_divider (w, x0, x1, y0, y1);
30130 /* Redraw the part of window W intersection rectangle FR. Pixel
30131 coordinates in FR are frame-relative. Call this function with
30132 input blocked. Value is non-zero if the exposure overwrites
30133 mouse-face. */
30135 static int
30136 expose_window (struct window *w, XRectangle *fr)
30138 struct frame *f = XFRAME (w->frame);
30139 XRectangle wr, r;
30140 int mouse_face_overwritten_p = 0;
30142 /* If window is not yet fully initialized, do nothing. This can
30143 happen when toolkit scroll bars are used and a window is split.
30144 Reconfiguring the scroll bar will generate an expose for a newly
30145 created window. */
30146 if (w->current_matrix == NULL)
30147 return 0;
30149 /* When we're currently updating the window, display and current
30150 matrix usually don't agree. Arrange for a thorough display
30151 later. */
30152 if (w->must_be_updated_p)
30154 SET_FRAME_GARBAGED (f);
30155 return 0;
30158 /* Frame-relative pixel rectangle of W. */
30159 wr.x = WINDOW_LEFT_EDGE_X (w);
30160 wr.y = WINDOW_TOP_EDGE_Y (w);
30161 wr.width = WINDOW_PIXEL_WIDTH (w);
30162 wr.height = WINDOW_PIXEL_HEIGHT (w);
30164 if (x_intersect_rectangles (fr, &wr, &r))
30166 int yb = window_text_bottom_y (w);
30167 struct glyph_row *row;
30168 int cursor_cleared_p, phys_cursor_on_p;
30169 struct glyph_row *first_overlapping_row, *last_overlapping_row;
30171 TRACE ((stderr, "expose_window (%d, %d, %d, %d)\n",
30172 r.x, r.y, r.width, r.height));
30174 /* Convert to window coordinates. */
30175 r.x -= WINDOW_LEFT_EDGE_X (w);
30176 r.y -= WINDOW_TOP_EDGE_Y (w);
30178 /* Turn off the cursor. */
30179 if (!w->pseudo_window_p
30180 && phys_cursor_in_rect_p (w, &r))
30182 x_clear_cursor (w);
30183 cursor_cleared_p = 1;
30185 else
30186 cursor_cleared_p = 0;
30188 /* If the row containing the cursor extends face to end of line,
30189 then expose_area might overwrite the cursor outside the
30190 rectangle and thus notice_overwritten_cursor might clear
30191 w->phys_cursor_on_p. We remember the original value and
30192 check later if it is changed. */
30193 phys_cursor_on_p = w->phys_cursor_on_p;
30195 /* Update lines intersecting rectangle R. */
30196 first_overlapping_row = last_overlapping_row = NULL;
30197 for (row = w->current_matrix->rows;
30198 row->enabled_p;
30199 ++row)
30201 int y0 = row->y;
30202 int y1 = MATRIX_ROW_BOTTOM_Y (row);
30204 if ((y0 >= r.y && y0 < r.y + r.height)
30205 || (y1 > r.y && y1 < r.y + r.height)
30206 || (r.y >= y0 && r.y < y1)
30207 || (r.y + r.height > y0 && r.y + r.height < y1))
30209 /* A header line may be overlapping, but there is no need
30210 to fix overlapping areas for them. KFS 2005-02-12 */
30211 if (row->overlapping_p && !row->mode_line_p)
30213 if (first_overlapping_row == NULL)
30214 first_overlapping_row = row;
30215 last_overlapping_row = row;
30218 row->clip = fr;
30219 if (expose_line (w, row, &r))
30220 mouse_face_overwritten_p = 1;
30221 row->clip = NULL;
30223 else if (row->overlapping_p)
30225 /* We must redraw a row overlapping the exposed area. */
30226 if (y0 < r.y
30227 ? y0 + row->phys_height > r.y
30228 : y0 + row->ascent - row->phys_ascent < r.y +r.height)
30230 if (first_overlapping_row == NULL)
30231 first_overlapping_row = row;
30232 last_overlapping_row = row;
30236 if (y1 >= yb)
30237 break;
30240 /* Display the mode line if there is one. */
30241 if (WINDOW_WANTS_MODELINE_P (w)
30242 && (row = MATRIX_MODE_LINE_ROW (w->current_matrix),
30243 row->enabled_p)
30244 && row->y < r.y + r.height)
30246 if (expose_line (w, row, &r))
30247 mouse_face_overwritten_p = 1;
30250 if (!w->pseudo_window_p)
30252 /* Fix the display of overlapping rows. */
30253 if (first_overlapping_row)
30254 expose_overlaps (w, first_overlapping_row, last_overlapping_row,
30255 fr);
30257 /* Draw border between windows. */
30258 if (WINDOW_RIGHT_DIVIDER_WIDTH (w))
30259 x_draw_right_divider (w);
30260 else
30261 x_draw_vertical_border (w);
30263 if (WINDOW_BOTTOM_DIVIDER_WIDTH (w))
30264 x_draw_bottom_divider (w);
30266 /* Turn the cursor on again. */
30267 if (cursor_cleared_p
30268 || (phys_cursor_on_p && !w->phys_cursor_on_p))
30269 update_window_cursor (w, 1);
30273 return mouse_face_overwritten_p;
30278 /* Redraw (parts) of all windows in the window tree rooted at W that
30279 intersect R. R contains frame pixel coordinates. Value is
30280 non-zero if the exposure overwrites mouse-face. */
30282 static int
30283 expose_window_tree (struct window *w, XRectangle *r)
30285 struct frame *f = XFRAME (w->frame);
30286 int mouse_face_overwritten_p = 0;
30288 while (w && !FRAME_GARBAGED_P (f))
30290 if (WINDOWP (w->contents))
30291 mouse_face_overwritten_p
30292 |= expose_window_tree (XWINDOW (w->contents), r);
30293 else
30294 mouse_face_overwritten_p |= expose_window (w, r);
30296 w = NILP (w->next) ? NULL : XWINDOW (w->next);
30299 return mouse_face_overwritten_p;
30303 /* EXPORT:
30304 Redisplay an exposed area of frame F. X and Y are the upper-left
30305 corner of the exposed rectangle. W and H are width and height of
30306 the exposed area. All are pixel values. W or H zero means redraw
30307 the entire frame. */
30309 void
30310 expose_frame (struct frame *f, int x, int y, int w, int h)
30312 XRectangle r;
30313 int mouse_face_overwritten_p = 0;
30315 TRACE ((stderr, "expose_frame "));
30317 /* No need to redraw if frame will be redrawn soon. */
30318 if (FRAME_GARBAGED_P (f))
30320 TRACE ((stderr, " garbaged\n"));
30321 return;
30324 /* If basic faces haven't been realized yet, there is no point in
30325 trying to redraw anything. This can happen when we get an expose
30326 event while Emacs is starting, e.g. by moving another window. */
30327 if (FRAME_FACE_CACHE (f) == NULL
30328 || FRAME_FACE_CACHE (f)->used < BASIC_FACE_ID_SENTINEL)
30330 TRACE ((stderr, " no faces\n"));
30331 return;
30334 if (w == 0 || h == 0)
30336 r.x = r.y = 0;
30337 r.width = FRAME_TEXT_WIDTH (f);
30338 r.height = FRAME_TEXT_HEIGHT (f);
30340 else
30342 r.x = x;
30343 r.y = y;
30344 r.width = w;
30345 r.height = h;
30348 TRACE ((stderr, "(%d, %d, %d, %d)\n", r.x, r.y, r.width, r.height));
30349 mouse_face_overwritten_p = expose_window_tree (XWINDOW (f->root_window), &r);
30351 #if ! defined (USE_GTK) && ! defined (HAVE_NS)
30352 if (WINDOWP (f->tool_bar_window))
30353 mouse_face_overwritten_p
30354 |= expose_window (XWINDOW (f->tool_bar_window), &r);
30355 #endif
30357 #ifdef HAVE_X_WINDOWS
30358 #ifndef MSDOS
30359 #if ! defined (USE_X_TOOLKIT) && ! defined (USE_GTK)
30360 if (WINDOWP (f->menu_bar_window))
30361 mouse_face_overwritten_p
30362 |= expose_window (XWINDOW (f->menu_bar_window), &r);
30363 #endif /* not USE_X_TOOLKIT and not USE_GTK */
30364 #endif
30365 #endif
30367 /* Some window managers support a focus-follows-mouse style with
30368 delayed raising of frames. Imagine a partially obscured frame,
30369 and moving the mouse into partially obscured mouse-face on that
30370 frame. The visible part of the mouse-face will be highlighted,
30371 then the WM raises the obscured frame. With at least one WM, KDE
30372 2.1, Emacs is not getting any event for the raising of the frame
30373 (even tried with SubstructureRedirectMask), only Expose events.
30374 These expose events will draw text normally, i.e. not
30375 highlighted. Which means we must redo the highlight here.
30376 Subsume it under ``we love X''. --gerd 2001-08-15 */
30377 /* Included in Windows version because Windows most likely does not
30378 do the right thing if any third party tool offers
30379 focus-follows-mouse with delayed raise. --jason 2001-10-12 */
30380 if (mouse_face_overwritten_p && !FRAME_GARBAGED_P (f))
30382 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
30383 if (f == hlinfo->mouse_face_mouse_frame)
30385 int mouse_x = hlinfo->mouse_face_mouse_x;
30386 int mouse_y = hlinfo->mouse_face_mouse_y;
30387 clear_mouse_face (hlinfo);
30388 note_mouse_highlight (f, mouse_x, mouse_y);
30394 /* EXPORT:
30395 Determine the intersection of two rectangles R1 and R2. Return
30396 the intersection in *RESULT. Value is non-zero if RESULT is not
30397 empty. */
30400 x_intersect_rectangles (XRectangle *r1, XRectangle *r2, XRectangle *result)
30402 XRectangle *left, *right;
30403 XRectangle *upper, *lower;
30404 int intersection_p = 0;
30406 /* Rearrange so that R1 is the left-most rectangle. */
30407 if (r1->x < r2->x)
30408 left = r1, right = r2;
30409 else
30410 left = r2, right = r1;
30412 /* X0 of the intersection is right.x0, if this is inside R1,
30413 otherwise there is no intersection. */
30414 if (right->x <= left->x + left->width)
30416 result->x = right->x;
30418 /* The right end of the intersection is the minimum of
30419 the right ends of left and right. */
30420 result->width = (min (left->x + left->width, right->x + right->width)
30421 - result->x);
30423 /* Same game for Y. */
30424 if (r1->y < r2->y)
30425 upper = r1, lower = r2;
30426 else
30427 upper = r2, lower = r1;
30429 /* The upper end of the intersection is lower.y0, if this is inside
30430 of upper. Otherwise, there is no intersection. */
30431 if (lower->y <= upper->y + upper->height)
30433 result->y = lower->y;
30435 /* The lower end of the intersection is the minimum of the lower
30436 ends of upper and lower. */
30437 result->height = (min (lower->y + lower->height,
30438 upper->y + upper->height)
30439 - result->y);
30440 intersection_p = 1;
30444 return intersection_p;
30447 #endif /* HAVE_WINDOW_SYSTEM */
30450 /***********************************************************************
30451 Initialization
30452 ***********************************************************************/
30454 void
30455 syms_of_xdisp (void)
30457 Vwith_echo_area_save_vector = Qnil;
30458 staticpro (&Vwith_echo_area_save_vector);
30460 Vmessage_stack = Qnil;
30461 staticpro (&Vmessage_stack);
30463 /* Non-nil means don't actually do any redisplay. */
30464 DEFSYM (Qinhibit_redisplay, "inhibit-redisplay");
30466 DEFSYM (Qredisplay_internal, "redisplay_internal (C function)");
30468 message_dolog_marker1 = Fmake_marker ();
30469 staticpro (&message_dolog_marker1);
30470 message_dolog_marker2 = Fmake_marker ();
30471 staticpro (&message_dolog_marker2);
30472 message_dolog_marker3 = Fmake_marker ();
30473 staticpro (&message_dolog_marker3);
30475 #ifdef GLYPH_DEBUG
30476 defsubr (&Sdump_frame_glyph_matrix);
30477 defsubr (&Sdump_glyph_matrix);
30478 defsubr (&Sdump_glyph_row);
30479 defsubr (&Sdump_tool_bar_row);
30480 defsubr (&Strace_redisplay);
30481 defsubr (&Strace_to_stderr);
30482 #endif
30483 #ifdef HAVE_WINDOW_SYSTEM
30484 defsubr (&Stool_bar_height);
30485 defsubr (&Slookup_image_map);
30486 #endif
30487 defsubr (&Sline_pixel_height);
30488 defsubr (&Sformat_mode_line);
30489 defsubr (&Sinvisible_p);
30490 defsubr (&Scurrent_bidi_paragraph_direction);
30491 defsubr (&Swindow_text_pixel_size);
30492 defsubr (&Smove_point_visually);
30493 defsubr (&Sbidi_find_overridden_directionality);
30495 DEFSYM (Qmenu_bar_update_hook, "menu-bar-update-hook");
30496 DEFSYM (Qoverriding_terminal_local_map, "overriding-terminal-local-map");
30497 DEFSYM (Qoverriding_local_map, "overriding-local-map");
30498 DEFSYM (Qwindow_scroll_functions, "window-scroll-functions");
30499 DEFSYM (Qwindow_text_change_functions, "window-text-change-functions");
30500 DEFSYM (Qredisplay_end_trigger_functions, "redisplay-end-trigger-functions");
30501 DEFSYM (Qinhibit_point_motion_hooks, "inhibit-point-motion-hooks");
30502 DEFSYM (Qeval, "eval");
30503 DEFSYM (QCdata, ":data");
30505 /* Names of text properties relevant for redisplay. */
30506 DEFSYM (Qdisplay, "display");
30507 DEFSYM (Qspace_width, "space-width");
30508 DEFSYM (Qraise, "raise");
30509 DEFSYM (Qslice, "slice");
30510 DEFSYM (Qspace, "space");
30511 DEFSYM (Qmargin, "margin");
30512 DEFSYM (Qpointer, "pointer");
30513 DEFSYM (Qleft_margin, "left-margin");
30514 DEFSYM (Qright_margin, "right-margin");
30515 DEFSYM (Qcenter, "center");
30516 DEFSYM (Qline_height, "line-height");
30517 DEFSYM (QCalign_to, ":align-to");
30518 DEFSYM (QCrelative_width, ":relative-width");
30519 DEFSYM (QCrelative_height, ":relative-height");
30520 DEFSYM (QCeval, ":eval");
30521 DEFSYM (QCpropertize, ":propertize");
30522 DEFSYM (QCfile, ":file");
30523 DEFSYM (Qfontified, "fontified");
30524 DEFSYM (Qfontification_functions, "fontification-functions");
30526 /* Name of the face used to highlight trailing whitespace. */
30527 DEFSYM (Qtrailing_whitespace, "trailing-whitespace");
30529 /* Name and number of the face used to highlight escape glyphs. */
30530 DEFSYM (Qescape_glyph, "escape-glyph");
30532 /* Name and number of the face used to highlight non-breaking spaces. */
30533 DEFSYM (Qnobreak_space, "nobreak-space");
30535 /* The symbol 'image' which is the car of the lists used to represent
30536 images in Lisp. Also a tool bar style. */
30537 DEFSYM (Qimage, "image");
30539 /* Tool bar styles. */
30540 DEFSYM (Qtext, "text");
30541 DEFSYM (Qboth, "both");
30542 DEFSYM (Qboth_horiz, "both-horiz");
30543 DEFSYM (Qtext_image_horiz, "text-image-horiz");
30545 /* The image map types. */
30546 DEFSYM (QCmap, ":map");
30547 DEFSYM (QCpointer, ":pointer");
30548 DEFSYM (Qrect, "rect");
30549 DEFSYM (Qcircle, "circle");
30550 DEFSYM (Qpoly, "poly");
30552 /* The symbol `inhibit-menubar-update' and its DEFVAR_BOOL variable. */
30553 DEFSYM (Qinhibit_menubar_update, "inhibit-menubar-update");
30554 DEFSYM (Qmessage_truncate_lines, "message-truncate-lines");
30556 DEFSYM (Qgrow_only, "grow-only");
30557 DEFSYM (Qinhibit_eval_during_redisplay, "inhibit-eval-during-redisplay");
30558 DEFSYM (Qposition, "position");
30559 DEFSYM (Qbuffer_position, "buffer-position");
30560 DEFSYM (Qobject, "object");
30562 /* Cursor shapes. */
30563 DEFSYM (Qbar, "bar");
30564 DEFSYM (Qhbar, "hbar");
30565 DEFSYM (Qbox, "box");
30566 DEFSYM (Qhollow, "hollow");
30568 /* Pointer shapes. */
30569 DEFSYM (Qhand, "hand");
30570 DEFSYM (Qarrow, "arrow");
30571 /* also Qtext */
30573 DEFSYM (Qinhibit_free_realized_faces, "inhibit-free-realized-faces");
30575 list_of_error = list1 (list2 (intern_c_string ("error"),
30576 intern_c_string ("void-variable")));
30577 staticpro (&list_of_error);
30579 /* Values of those variables at last redisplay are stored as
30580 properties on 'overlay-arrow-position' symbol. However, if
30581 Voverlay_arrow_position is a marker, last-arrow-position is its
30582 numerical position. */
30583 DEFSYM (Qlast_arrow_position, "last-arrow-position");
30584 DEFSYM (Qlast_arrow_string, "last-arrow-string");
30586 /* Alternative overlay-arrow-string and overlay-arrow-bitmap
30587 properties on a symbol in overlay-arrow-variable-list. */
30588 DEFSYM (Qoverlay_arrow_string, "overlay-arrow-string");
30589 DEFSYM (Qoverlay_arrow_bitmap, "overlay-arrow-bitmap");
30591 echo_buffer[0] = echo_buffer[1] = Qnil;
30592 staticpro (&echo_buffer[0]);
30593 staticpro (&echo_buffer[1]);
30595 echo_area_buffer[0] = echo_area_buffer[1] = Qnil;
30596 staticpro (&echo_area_buffer[0]);
30597 staticpro (&echo_area_buffer[1]);
30599 Vmessages_buffer_name = build_pure_c_string ("*Messages*");
30600 staticpro (&Vmessages_buffer_name);
30602 mode_line_proptrans_alist = Qnil;
30603 staticpro (&mode_line_proptrans_alist);
30604 mode_line_string_list = Qnil;
30605 staticpro (&mode_line_string_list);
30606 mode_line_string_face = Qnil;
30607 staticpro (&mode_line_string_face);
30608 mode_line_string_face_prop = Qnil;
30609 staticpro (&mode_line_string_face_prop);
30610 Vmode_line_unwind_vector = Qnil;
30611 staticpro (&Vmode_line_unwind_vector);
30613 DEFSYM (Qmode_line_default_help_echo, "mode-line-default-help-echo");
30615 help_echo_string = Qnil;
30616 staticpro (&help_echo_string);
30617 help_echo_object = Qnil;
30618 staticpro (&help_echo_object);
30619 help_echo_window = Qnil;
30620 staticpro (&help_echo_window);
30621 previous_help_echo_string = Qnil;
30622 staticpro (&previous_help_echo_string);
30623 help_echo_pos = -1;
30625 DEFSYM (Qright_to_left, "right-to-left");
30626 DEFSYM (Qleft_to_right, "left-to-right");
30627 defsubr (&Sbidi_resolved_levels);
30629 #ifdef HAVE_WINDOW_SYSTEM
30630 DEFVAR_BOOL ("x-stretch-cursor", x_stretch_cursor_p,
30631 doc: /* Non-nil means draw block cursor as wide as the glyph under it.
30632 For example, if a block cursor is over a tab, it will be drawn as
30633 wide as that tab on the display. */);
30634 x_stretch_cursor_p = 0;
30635 #endif
30637 DEFVAR_LISP ("show-trailing-whitespace", Vshow_trailing_whitespace,
30638 doc: /* Non-nil means highlight trailing whitespace.
30639 The face used for trailing whitespace is `trailing-whitespace'. */);
30640 Vshow_trailing_whitespace = Qnil;
30642 DEFVAR_LISP ("nobreak-char-display", Vnobreak_char_display,
30643 doc: /* Control highlighting of non-ASCII space and hyphen chars.
30644 If the value is t, Emacs highlights non-ASCII chars which have the
30645 same appearance as an ASCII space or hyphen, using the `nobreak-space'
30646 or `escape-glyph' face respectively.
30648 U+00A0 (no-break space), U+00AD (soft hyphen), U+2010 (hyphen), and
30649 U+2011 (non-breaking hyphen) are affected.
30651 Any other non-nil value means to display these characters as a escape
30652 glyph followed by an ordinary space or hyphen.
30654 A value of nil means no special handling of these characters. */);
30655 Vnobreak_char_display = Qt;
30657 DEFVAR_LISP ("void-text-area-pointer", Vvoid_text_area_pointer,
30658 doc: /* The pointer shape to show in void text areas.
30659 A value of nil means to show the text pointer. Other options are
30660 `arrow', `text', `hand', `vdrag', `hdrag', `nhdrag', `modeline', and
30661 `hourglass'. */);
30662 Vvoid_text_area_pointer = Qarrow;
30664 DEFVAR_LISP ("inhibit-redisplay", Vinhibit_redisplay,
30665 doc: /* Non-nil means don't actually do any redisplay.
30666 This is used for internal purposes. */);
30667 Vinhibit_redisplay = Qnil;
30669 DEFVAR_LISP ("global-mode-string", Vglobal_mode_string,
30670 doc: /* String (or mode line construct) included (normally) in `mode-line-format'. */);
30671 Vglobal_mode_string = Qnil;
30673 DEFVAR_LISP ("overlay-arrow-position", Voverlay_arrow_position,
30674 doc: /* Marker for where to display an arrow on top of the buffer text.
30675 This must be the beginning of a line in order to work.
30676 See also `overlay-arrow-string'. */);
30677 Voverlay_arrow_position = Qnil;
30679 DEFVAR_LISP ("overlay-arrow-string", Voverlay_arrow_string,
30680 doc: /* String to display as an arrow in non-window frames.
30681 See also `overlay-arrow-position'. */);
30682 Voverlay_arrow_string = build_pure_c_string ("=>");
30684 DEFVAR_LISP ("overlay-arrow-variable-list", Voverlay_arrow_variable_list,
30685 doc: /* List of variables (symbols) which hold markers for overlay arrows.
30686 The symbols on this list are examined during redisplay to determine
30687 where to display overlay arrows. */);
30688 Voverlay_arrow_variable_list
30689 = list1 (intern_c_string ("overlay-arrow-position"));
30691 DEFVAR_INT ("scroll-step", emacs_scroll_step,
30692 doc: /* The number of lines to try scrolling a window by when point moves out.
30693 If that fails to bring point back on frame, point is centered instead.
30694 If this is zero, point is always centered after it moves off frame.
30695 If you want scrolling to always be a line at a time, you should set
30696 `scroll-conservatively' to a large value rather than set this to 1. */);
30698 DEFVAR_INT ("scroll-conservatively", scroll_conservatively,
30699 doc: /* Scroll up to this many lines, to bring point back on screen.
30700 If point moves off-screen, redisplay will scroll by up to
30701 `scroll-conservatively' lines in order to bring point just barely
30702 onto the screen again. If that cannot be done, then redisplay
30703 recenters point as usual.
30705 If the value is greater than 100, redisplay will never recenter point,
30706 but will always scroll just enough text to bring point into view, even
30707 if you move far away.
30709 A value of zero means always recenter point if it moves off screen. */);
30710 scroll_conservatively = 0;
30712 DEFVAR_INT ("scroll-margin", scroll_margin,
30713 doc: /* Number of lines of margin at the top and bottom of a window.
30714 Recenter the window whenever point gets within this many lines
30715 of the top or bottom of the window. */);
30716 scroll_margin = 0;
30718 DEFVAR_LISP ("display-pixels-per-inch", Vdisplay_pixels_per_inch,
30719 doc: /* Pixels per inch value for non-window system displays.
30720 Value is a number or a cons (WIDTH-DPI . HEIGHT-DPI). */);
30721 Vdisplay_pixels_per_inch = make_float (72.0);
30723 #ifdef GLYPH_DEBUG
30724 DEFVAR_INT ("debug-end-pos", debug_end_pos, doc: /* Don't ask. */);
30725 #endif
30727 DEFVAR_LISP ("truncate-partial-width-windows",
30728 Vtruncate_partial_width_windows,
30729 doc: /* Non-nil means truncate lines in windows narrower than the frame.
30730 For an integer value, truncate lines in each window narrower than the
30731 full frame width, provided the window width is less than that integer;
30732 otherwise, respect the value of `truncate-lines'.
30734 For any other non-nil value, truncate lines in all windows that do
30735 not span the full frame width.
30737 A value of nil means to respect the value of `truncate-lines'.
30739 If `word-wrap' is enabled, you might want to reduce this. */);
30740 Vtruncate_partial_width_windows = make_number (50);
30742 DEFVAR_LISP ("line-number-display-limit", Vline_number_display_limit,
30743 doc: /* Maximum buffer size for which line number should be displayed.
30744 If the buffer is bigger than this, the line number does not appear
30745 in the mode line. A value of nil means no limit. */);
30746 Vline_number_display_limit = Qnil;
30748 DEFVAR_INT ("line-number-display-limit-width",
30749 line_number_display_limit_width,
30750 doc: /* Maximum line width (in characters) for line number display.
30751 If the average length of the lines near point is bigger than this, then the
30752 line number may be omitted from the mode line. */);
30753 line_number_display_limit_width = 200;
30755 DEFVAR_BOOL ("highlight-nonselected-windows", highlight_nonselected_windows,
30756 doc: /* Non-nil means highlight region even in nonselected windows. */);
30757 highlight_nonselected_windows = 0;
30759 DEFVAR_BOOL ("multiple-frames", multiple_frames,
30760 doc: /* Non-nil if more than one frame is visible on this display.
30761 Minibuffer-only frames don't count, but iconified frames do.
30762 This variable is not guaranteed to be accurate except while processing
30763 `frame-title-format' and `icon-title-format'. */);
30765 DEFVAR_LISP ("frame-title-format", Vframe_title_format,
30766 doc: /* Template for displaying the title bar of visible frames.
30767 \(Assuming the window manager supports this feature.)
30769 This variable has the same structure as `mode-line-format', except that
30770 the %c and %l constructs are ignored. It is used only on frames for
30771 which no explicit name has been set \(see `modify-frame-parameters'). */);
30773 DEFVAR_LISP ("icon-title-format", Vicon_title_format,
30774 doc: /* Template for displaying the title bar of an iconified frame.
30775 \(Assuming the window manager supports this feature.)
30776 This variable has the same structure as `mode-line-format' (which see),
30777 and is used only on frames for which no explicit name has been set
30778 \(see `modify-frame-parameters'). */);
30779 Vicon_title_format
30780 = Vframe_title_format
30781 = listn (CONSTYPE_PURE, 3,
30782 intern_c_string ("multiple-frames"),
30783 build_pure_c_string ("%b"),
30784 listn (CONSTYPE_PURE, 4,
30785 empty_unibyte_string,
30786 intern_c_string ("invocation-name"),
30787 build_pure_c_string ("@"),
30788 intern_c_string ("system-name")));
30790 DEFVAR_LISP ("message-log-max", Vmessage_log_max,
30791 doc: /* Maximum number of lines to keep in the message log buffer.
30792 If nil, disable message logging. If t, log messages but don't truncate
30793 the buffer when it becomes large. */);
30794 Vmessage_log_max = make_number (1000);
30796 DEFVAR_LISP ("window-size-change-functions", Vwindow_size_change_functions,
30797 doc: /* Functions called before redisplay, if window sizes have changed.
30798 The value should be a list of functions that take one argument.
30799 Just before redisplay, for each frame, if any of its windows have changed
30800 size since the last redisplay, or have been split or deleted,
30801 all the functions in the list are called, with the frame as argument. */);
30802 Vwindow_size_change_functions = Qnil;
30804 DEFVAR_LISP ("window-scroll-functions", Vwindow_scroll_functions,
30805 doc: /* List of functions to call before redisplaying a window with scrolling.
30806 Each function is called with two arguments, the window and its new
30807 display-start position.
30808 These functions are called whenever the `window-start' marker is modified,
30809 either to point into another buffer (e.g. via `set-window-buffer') or another
30810 place in the same buffer.
30811 Note that the value of `window-end' is not valid when these functions are
30812 called.
30814 Warning: Do not use this feature to alter the way the window
30815 is scrolled. It is not designed for that, and such use probably won't
30816 work. */);
30817 Vwindow_scroll_functions = Qnil;
30819 DEFVAR_LISP ("window-text-change-functions",
30820 Vwindow_text_change_functions,
30821 doc: /* Functions to call in redisplay when text in the window might change. */);
30822 Vwindow_text_change_functions = Qnil;
30824 DEFVAR_LISP ("redisplay-end-trigger-functions", Vredisplay_end_trigger_functions,
30825 doc: /* Functions called when redisplay of a window reaches the end trigger.
30826 Each function is called with two arguments, the window and the end trigger value.
30827 See `set-window-redisplay-end-trigger'. */);
30828 Vredisplay_end_trigger_functions = Qnil;
30830 DEFVAR_LISP ("mouse-autoselect-window", Vmouse_autoselect_window,
30831 doc: /* Non-nil means autoselect window with mouse pointer.
30832 If nil, do not autoselect windows.
30833 A positive number means delay autoselection by that many seconds: a
30834 window is autoselected only after the mouse has remained in that
30835 window for the duration of the delay.
30836 A negative number has a similar effect, but causes windows to be
30837 autoselected only after the mouse has stopped moving. \(Because of
30838 the way Emacs compares mouse events, you will occasionally wait twice
30839 that time before the window gets selected.\)
30840 Any other value means to autoselect window instantaneously when the
30841 mouse pointer enters it.
30843 Autoselection selects the minibuffer only if it is active, and never
30844 unselects the minibuffer if it is active.
30846 When customizing this variable make sure that the actual value of
30847 `focus-follows-mouse' matches the behavior of your window manager. */);
30848 Vmouse_autoselect_window = Qnil;
30850 DEFVAR_LISP ("auto-resize-tool-bars", Vauto_resize_tool_bars,
30851 doc: /* Non-nil means automatically resize tool-bars.
30852 This dynamically changes the tool-bar's height to the minimum height
30853 that is needed to make all tool-bar items visible.
30854 If value is `grow-only', the tool-bar's height is only increased
30855 automatically; to decrease the tool-bar height, use \\[recenter]. */);
30856 Vauto_resize_tool_bars = Qt;
30858 DEFVAR_BOOL ("auto-raise-tool-bar-buttons", auto_raise_tool_bar_buttons_p,
30859 doc: /* Non-nil means raise tool-bar buttons when the mouse moves over them. */);
30860 auto_raise_tool_bar_buttons_p = 1;
30862 DEFVAR_BOOL ("make-cursor-line-fully-visible", make_cursor_line_fully_visible_p,
30863 doc: /* Non-nil means to scroll (recenter) cursor line if it is not fully visible. */);
30864 make_cursor_line_fully_visible_p = 1;
30866 DEFVAR_LISP ("tool-bar-border", Vtool_bar_border,
30867 doc: /* Border below tool-bar in pixels.
30868 If an integer, use it as the height of the border.
30869 If it is one of `internal-border-width' or `border-width', use the
30870 value of the corresponding frame parameter.
30871 Otherwise, no border is added below the tool-bar. */);
30872 Vtool_bar_border = Qinternal_border_width;
30874 DEFVAR_LISP ("tool-bar-button-margin", Vtool_bar_button_margin,
30875 doc: /* Margin around tool-bar buttons in pixels.
30876 If an integer, use that for both horizontal and vertical margins.
30877 Otherwise, value should be a pair of integers `(HORZ . VERT)' with
30878 HORZ specifying the horizontal margin, and VERT specifying the
30879 vertical margin. */);
30880 Vtool_bar_button_margin = make_number (DEFAULT_TOOL_BAR_BUTTON_MARGIN);
30882 DEFVAR_INT ("tool-bar-button-relief", tool_bar_button_relief,
30883 doc: /* Relief thickness of tool-bar buttons. */);
30884 tool_bar_button_relief = DEFAULT_TOOL_BAR_BUTTON_RELIEF;
30886 DEFVAR_LISP ("tool-bar-style", Vtool_bar_style,
30887 doc: /* Tool bar style to use.
30888 It can be one of
30889 image - show images only
30890 text - show text only
30891 both - show both, text below image
30892 both-horiz - show text to the right of the image
30893 text-image-horiz - show text to the left of the image
30894 any other - use system default or image if no system default.
30896 This variable only affects the GTK+ toolkit version of Emacs. */);
30897 Vtool_bar_style = Qnil;
30899 DEFVAR_INT ("tool-bar-max-label-size", tool_bar_max_label_size,
30900 doc: /* Maximum number of characters a label can have to be shown.
30901 The tool bar style must also show labels for this to have any effect, see
30902 `tool-bar-style'. */);
30903 tool_bar_max_label_size = DEFAULT_TOOL_BAR_LABEL_SIZE;
30905 DEFVAR_LISP ("fontification-functions", Vfontification_functions,
30906 doc: /* List of functions to call to fontify regions of text.
30907 Each function is called with one argument POS. Functions must
30908 fontify a region starting at POS in the current buffer, and give
30909 fontified regions the property `fontified'. */);
30910 Vfontification_functions = Qnil;
30911 Fmake_variable_buffer_local (Qfontification_functions);
30913 DEFVAR_BOOL ("unibyte-display-via-language-environment",
30914 unibyte_display_via_language_environment,
30915 doc: /* Non-nil means display unibyte text according to language environment.
30916 Specifically, this means that raw bytes in the range 160-255 decimal
30917 are displayed by converting them to the equivalent multibyte characters
30918 according to the current language environment. As a result, they are
30919 displayed according to the current fontset.
30921 Note that this variable affects only how these bytes are displayed,
30922 but does not change the fact they are interpreted as raw bytes. */);
30923 unibyte_display_via_language_environment = 0;
30925 DEFVAR_LISP ("max-mini-window-height", Vmax_mini_window_height,
30926 doc: /* Maximum height for resizing mini-windows (the minibuffer and the echo area).
30927 If a float, it specifies a fraction of the mini-window frame's height.
30928 If an integer, it specifies a number of lines. */);
30929 Vmax_mini_window_height = make_float (0.25);
30931 DEFVAR_LISP ("resize-mini-windows", Vresize_mini_windows,
30932 doc: /* How to resize mini-windows (the minibuffer and the echo area).
30933 A value of nil means don't automatically resize mini-windows.
30934 A value of t means resize them to fit the text displayed in them.
30935 A value of `grow-only', the default, means let mini-windows grow only;
30936 they return to their normal size when the minibuffer is closed, or the
30937 echo area becomes empty. */);
30938 Vresize_mini_windows = Qgrow_only;
30940 DEFVAR_LISP ("blink-cursor-alist", Vblink_cursor_alist,
30941 doc: /* Alist specifying how to blink the cursor off.
30942 Each element has the form (ON-STATE . OFF-STATE). Whenever the
30943 `cursor-type' frame-parameter or variable equals ON-STATE,
30944 comparing using `equal', Emacs uses OFF-STATE to specify
30945 how to blink it off. ON-STATE and OFF-STATE are values for
30946 the `cursor-type' frame parameter.
30948 If a frame's ON-STATE has no entry in this list,
30949 the frame's other specifications determine how to blink the cursor off. */);
30950 Vblink_cursor_alist = Qnil;
30952 DEFVAR_BOOL ("auto-hscroll-mode", automatic_hscrolling_p,
30953 doc: /* Allow or disallow automatic horizontal scrolling of windows.
30954 If non-nil, windows are automatically scrolled horizontally to make
30955 point visible. */);
30956 automatic_hscrolling_p = 1;
30957 DEFSYM (Qauto_hscroll_mode, "auto-hscroll-mode");
30959 DEFVAR_INT ("hscroll-margin", hscroll_margin,
30960 doc: /* How many columns away from the window edge point is allowed to get
30961 before automatic hscrolling will horizontally scroll the window. */);
30962 hscroll_margin = 5;
30964 DEFVAR_LISP ("hscroll-step", Vhscroll_step,
30965 doc: /* How many columns to scroll the window when point gets too close to the edge.
30966 When point is less than `hscroll-margin' columns from the window
30967 edge, automatic hscrolling will scroll the window by the amount of columns
30968 determined by this variable. If its value is a positive integer, scroll that
30969 many columns. If it's a positive floating-point number, it specifies the
30970 fraction of the window's width to scroll. If it's nil or zero, point will be
30971 centered horizontally after the scroll. Any other value, including negative
30972 numbers, are treated as if the value were zero.
30974 Automatic hscrolling always moves point outside the scroll margin, so if
30975 point was more than scroll step columns inside the margin, the window will
30976 scroll more than the value given by the scroll step.
30978 Note that the lower bound for automatic hscrolling specified by `scroll-left'
30979 and `scroll-right' overrides this variable's effect. */);
30980 Vhscroll_step = make_number (0);
30982 DEFVAR_BOOL ("message-truncate-lines", message_truncate_lines,
30983 doc: /* If non-nil, messages are truncated instead of resizing the echo area.
30984 Bind this around calls to `message' to let it take effect. */);
30985 message_truncate_lines = 0;
30987 DEFVAR_LISP ("menu-bar-update-hook", Vmenu_bar_update_hook,
30988 doc: /* Normal hook run to update the menu bar definitions.
30989 Redisplay runs this hook before it redisplays the menu bar.
30990 This is used to update menus such as Buffers, whose contents depend on
30991 various data. */);
30992 Vmenu_bar_update_hook = Qnil;
30994 DEFVAR_LISP ("menu-updating-frame", Vmenu_updating_frame,
30995 doc: /* Frame for which we are updating a menu.
30996 The enable predicate for a menu binding should check this variable. */);
30997 Vmenu_updating_frame = Qnil;
30999 DEFVAR_BOOL ("inhibit-menubar-update", inhibit_menubar_update,
31000 doc: /* Non-nil means don't update menu bars. Internal use only. */);
31001 inhibit_menubar_update = 0;
31003 DEFVAR_LISP ("wrap-prefix", Vwrap_prefix,
31004 doc: /* Prefix prepended to all continuation lines at display time.
31005 The value may be a string, an image, or a stretch-glyph; it is
31006 interpreted in the same way as the value of a `display' text property.
31008 This variable is overridden by any `wrap-prefix' text or overlay
31009 property.
31011 To add a prefix to non-continuation lines, use `line-prefix'. */);
31012 Vwrap_prefix = Qnil;
31013 DEFSYM (Qwrap_prefix, "wrap-prefix");
31014 Fmake_variable_buffer_local (Qwrap_prefix);
31016 DEFVAR_LISP ("line-prefix", Vline_prefix,
31017 doc: /* Prefix prepended to all non-continuation lines at display time.
31018 The value may be a string, an image, or a stretch-glyph; it is
31019 interpreted in the same way as the value of a `display' text property.
31021 This variable is overridden by any `line-prefix' text or overlay
31022 property.
31024 To add a prefix to continuation lines, use `wrap-prefix'. */);
31025 Vline_prefix = Qnil;
31026 DEFSYM (Qline_prefix, "line-prefix");
31027 Fmake_variable_buffer_local (Qline_prefix);
31029 DEFVAR_BOOL ("inhibit-eval-during-redisplay", inhibit_eval_during_redisplay,
31030 doc: /* Non-nil means don't eval Lisp during redisplay. */);
31031 inhibit_eval_during_redisplay = 0;
31033 DEFVAR_BOOL ("inhibit-free-realized-faces", inhibit_free_realized_faces,
31034 doc: /* Non-nil means don't free realized faces. Internal use only. */);
31035 inhibit_free_realized_faces = 0;
31037 DEFVAR_BOOL ("inhibit-bidi-mirroring", inhibit_bidi_mirroring,
31038 doc: /* Non-nil means don't mirror characters even when bidi context requires that.
31039 Intended for use during debugging and for testing bidi display;
31040 see biditest.el in the test suite. */);
31041 inhibit_bidi_mirroring = 0;
31043 #ifdef GLYPH_DEBUG
31044 DEFVAR_BOOL ("inhibit-try-window-id", inhibit_try_window_id,
31045 doc: /* Inhibit try_window_id display optimization. */);
31046 inhibit_try_window_id = 0;
31048 DEFVAR_BOOL ("inhibit-try-window-reusing", inhibit_try_window_reusing,
31049 doc: /* Inhibit try_window_reusing display optimization. */);
31050 inhibit_try_window_reusing = 0;
31052 DEFVAR_BOOL ("inhibit-try-cursor-movement", inhibit_try_cursor_movement,
31053 doc: /* Inhibit try_cursor_movement display optimization. */);
31054 inhibit_try_cursor_movement = 0;
31055 #endif /* GLYPH_DEBUG */
31057 DEFVAR_INT ("overline-margin", overline_margin,
31058 doc: /* Space between overline and text, in pixels.
31059 The default value is 2: the height of the overline (1 pixel) plus 1 pixel
31060 margin to the character height. */);
31061 overline_margin = 2;
31063 DEFVAR_INT ("underline-minimum-offset",
31064 underline_minimum_offset,
31065 doc: /* Minimum distance between baseline and underline.
31066 This can improve legibility of underlined text at small font sizes,
31067 particularly when using variable `x-use-underline-position-properties'
31068 with fonts that specify an UNDERLINE_POSITION relatively close to the
31069 baseline. The default value is 1. */);
31070 underline_minimum_offset = 1;
31072 DEFVAR_BOOL ("display-hourglass", display_hourglass_p,
31073 doc: /* Non-nil means show an hourglass pointer, when Emacs is busy.
31074 This feature only works when on a window system that can change
31075 cursor shapes. */);
31076 display_hourglass_p = 1;
31078 DEFVAR_LISP ("hourglass-delay", Vhourglass_delay,
31079 doc: /* Seconds to wait before displaying an hourglass pointer when Emacs is busy. */);
31080 Vhourglass_delay = make_number (DEFAULT_HOURGLASS_DELAY);
31082 #ifdef HAVE_WINDOW_SYSTEM
31083 hourglass_atimer = NULL;
31084 hourglass_shown_p = 0;
31085 #endif /* HAVE_WINDOW_SYSTEM */
31087 /* Name of the face used to display glyphless characters. */
31088 DEFSYM (Qglyphless_char, "glyphless-char");
31090 /* Method symbols for Vglyphless_char_display. */
31091 DEFSYM (Qhex_code, "hex-code");
31092 DEFSYM (Qempty_box, "empty-box");
31093 DEFSYM (Qthin_space, "thin-space");
31094 DEFSYM (Qzero_width, "zero-width");
31096 DEFVAR_LISP ("pre-redisplay-function", Vpre_redisplay_function,
31097 doc: /* Function run just before redisplay.
31098 It is called with one argument, which is the set of windows that are to
31099 be redisplayed. This set can be nil (meaning, only the selected window),
31100 or t (meaning all windows). */);
31101 Vpre_redisplay_function = intern ("ignore");
31103 /* Symbol for the purpose of Vglyphless_char_display. */
31104 DEFSYM (Qglyphless_char_display, "glyphless-char-display");
31105 Fput (Qglyphless_char_display, Qchar_table_extra_slots, make_number (1));
31107 DEFVAR_LISP ("glyphless-char-display", Vglyphless_char_display,
31108 doc: /* Char-table defining glyphless characters.
31109 Each element, if non-nil, should be one of the following:
31110 an ASCII acronym string: display this string in a box
31111 `hex-code': display the hexadecimal code of a character in a box
31112 `empty-box': display as an empty box
31113 `thin-space': display as 1-pixel width space
31114 `zero-width': don't display
31115 An element may also be a cons cell (GRAPHICAL . TEXT), which specifies the
31116 display method for graphical terminals and text terminals respectively.
31117 GRAPHICAL and TEXT should each have one of the values listed above.
31119 The char-table has one extra slot to control the display of a character for
31120 which no font is found. This slot only takes effect on graphical terminals.
31121 Its value should be an ASCII acronym string, `hex-code', `empty-box', or
31122 `thin-space'. The default is `empty-box'.
31124 If a character has a non-nil entry in an active display table, the
31125 display table takes effect; in this case, Emacs does not consult
31126 `glyphless-char-display' at all. */);
31127 Vglyphless_char_display = Fmake_char_table (Qglyphless_char_display, Qnil);
31128 Fset_char_table_extra_slot (Vglyphless_char_display, make_number (0),
31129 Qempty_box);
31131 DEFVAR_LISP ("debug-on-message", Vdebug_on_message,
31132 doc: /* If non-nil, debug if a message matching this regexp is displayed. */);
31133 Vdebug_on_message = Qnil;
31135 DEFVAR_LISP ("redisplay--all-windows-cause", Vredisplay__all_windows_cause,
31136 doc: /* */);
31137 Vredisplay__all_windows_cause
31138 = Fmake_vector (make_number (100), make_number (0));
31140 DEFVAR_LISP ("redisplay--mode-lines-cause", Vredisplay__mode_lines_cause,
31141 doc: /* */);
31142 Vredisplay__mode_lines_cause
31143 = Fmake_vector (make_number (100), make_number (0));
31147 /* Initialize this module when Emacs starts. */
31149 void
31150 init_xdisp (void)
31152 CHARPOS (this_line_start_pos) = 0;
31154 if (!noninteractive)
31156 struct window *m = XWINDOW (minibuf_window);
31157 Lisp_Object frame = m->frame;
31158 struct frame *f = XFRAME (frame);
31159 Lisp_Object root = FRAME_ROOT_WINDOW (f);
31160 struct window *r = XWINDOW (root);
31161 int i;
31163 echo_area_window = minibuf_window;
31165 r->top_line = FRAME_TOP_MARGIN (f);
31166 r->pixel_top = r->top_line * FRAME_LINE_HEIGHT (f);
31167 r->total_cols = FRAME_COLS (f);
31168 r->pixel_width = r->total_cols * FRAME_COLUMN_WIDTH (f);
31169 r->total_lines = FRAME_TOTAL_LINES (f) - 1 - FRAME_TOP_MARGIN (f);
31170 r->pixel_height = r->total_lines * FRAME_LINE_HEIGHT (f);
31172 m->top_line = FRAME_TOTAL_LINES (f) - 1;
31173 m->pixel_top = m->top_line * FRAME_LINE_HEIGHT (f);
31174 m->total_cols = FRAME_COLS (f);
31175 m->pixel_width = m->total_cols * FRAME_COLUMN_WIDTH (f);
31176 m->total_lines = 1;
31177 m->pixel_height = m->total_lines * FRAME_LINE_HEIGHT (f);
31179 scratch_glyph_row.glyphs[TEXT_AREA] = scratch_glyphs;
31180 scratch_glyph_row.glyphs[TEXT_AREA + 1]
31181 = scratch_glyphs + MAX_SCRATCH_GLYPHS;
31183 /* The default ellipsis glyphs `...'. */
31184 for (i = 0; i < 3; ++i)
31185 default_invis_vector[i] = make_number ('.');
31189 /* Allocate the buffer for frame titles.
31190 Also used for `format-mode-line'. */
31191 int size = 100;
31192 mode_line_noprop_buf = xmalloc (size);
31193 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
31194 mode_line_noprop_ptr = mode_line_noprop_buf;
31195 mode_line_target = MODE_LINE_DISPLAY;
31198 help_echo_showing_p = 0;
31201 #ifdef HAVE_WINDOW_SYSTEM
31203 /* Platform-independent portion of hourglass implementation. */
31205 /* Timer function of hourglass_atimer. */
31207 static void
31208 show_hourglass (struct atimer *timer)
31210 /* The timer implementation will cancel this timer automatically
31211 after this function has run. Set hourglass_atimer to null
31212 so that we know the timer doesn't have to be canceled. */
31213 hourglass_atimer = NULL;
31215 if (!hourglass_shown_p)
31217 Lisp_Object tail, frame;
31219 block_input ();
31221 FOR_EACH_FRAME (tail, frame)
31223 struct frame *f = XFRAME (frame);
31225 if (FRAME_LIVE_P (f) && FRAME_WINDOW_P (f)
31226 && FRAME_RIF (f)->show_hourglass)
31227 FRAME_RIF (f)->show_hourglass (f);
31230 hourglass_shown_p = 1;
31231 unblock_input ();
31235 /* Cancel a currently active hourglass timer, and start a new one. */
31237 void
31238 start_hourglass (void)
31240 struct timespec delay;
31242 cancel_hourglass ();
31244 if (INTEGERP (Vhourglass_delay)
31245 && XINT (Vhourglass_delay) > 0)
31246 delay = make_timespec (min (XINT (Vhourglass_delay),
31247 TYPE_MAXIMUM (time_t)),
31249 else if (FLOATP (Vhourglass_delay)
31250 && XFLOAT_DATA (Vhourglass_delay) > 0)
31251 delay = dtotimespec (XFLOAT_DATA (Vhourglass_delay));
31252 else
31253 delay = make_timespec (DEFAULT_HOURGLASS_DELAY, 0);
31255 hourglass_atimer = start_atimer (ATIMER_RELATIVE, delay,
31256 show_hourglass, NULL);
31259 /* Cancel the hourglass cursor timer if active, hide a busy cursor if
31260 shown. */
31262 void
31263 cancel_hourglass (void)
31265 if (hourglass_atimer)
31267 cancel_atimer (hourglass_atimer);
31268 hourglass_atimer = NULL;
31271 if (hourglass_shown_p)
31273 Lisp_Object tail, frame;
31275 block_input ();
31277 FOR_EACH_FRAME (tail, frame)
31279 struct frame *f = XFRAME (frame);
31281 if (FRAME_LIVE_P (f) && FRAME_WINDOW_P (f)
31282 && FRAME_RIF (f)->hide_hourglass)
31283 FRAME_RIF (f)->hide_hourglass (f);
31284 #ifdef HAVE_NTGUI
31285 /* No cursors on non GUI frames - restore to stock arrow cursor. */
31286 else if (!FRAME_W32_P (f))
31287 w32_arrow_cursor ();
31288 #endif
31291 hourglass_shown_p = 0;
31292 unblock_input ();
31296 #endif /* HAVE_WINDOW_SYSTEM */