emacs-lisp/package.el (package-initialize): Populate `package-selected-packages'.
[emacs.git] / src / xdisp.c
blob8f6695ae2b0ed0cf208871e49c50dccbeaf1f888
1 /* Display generation from window structure and buffer text.
3 Copyright (C) 1985-1988, 1993-1995, 1997-2015 Free Software Foundation,
4 Inc.
6 This file is part of GNU Emacs.
8 GNU Emacs is free software: you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation, either version 3 of the License, or
11 (at your option) any later version.
13 GNU Emacs is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
21 /* New redisplay written by Gerd Moellmann <gerd@gnu.org>.
23 Redisplay.
25 Emacs separates the task of updating the display from code
26 modifying global state, e.g. buffer text. This way functions
27 operating on buffers don't also have to be concerned with updating
28 the display.
30 Updating the display is triggered by the Lisp interpreter when it
31 decides it's time to do it. This is done either automatically for
32 you as part of the interpreter's command loop or as the result of
33 calling Lisp functions like `sit-for'. The C function `redisplay'
34 in xdisp.c is the only entry into the inner redisplay code.
36 The following diagram shows how redisplay code is invoked. As you
37 can see, Lisp calls redisplay and vice versa. Under window systems
38 like X, some portions of the redisplay code are also called
39 asynchronously during mouse movement or expose events. It is very
40 important that these code parts do NOT use the C library (malloc,
41 free) because many C libraries under Unix are not reentrant. They
42 may also NOT call functions of the Lisp interpreter which could
43 change the interpreter's state. If you don't follow these rules,
44 you will encounter bugs which are very hard to explain.
46 +--------------+ redisplay +----------------+
47 | Lisp machine |---------------->| Redisplay code |<--+
48 +--------------+ (xdisp.c) +----------------+ |
49 ^ | |
50 +----------------------------------+ |
51 Don't use this path when called |
52 asynchronously! |
54 expose_window (asynchronous) |
56 X expose events -----+
58 What does redisplay do? Obviously, it has to figure out somehow what
59 has been changed since the last time the display has been updated,
60 and to make these changes visible. Preferably it would do that in
61 a moderately intelligent way, i.e. fast.
63 Changes in buffer text can be deduced from window and buffer
64 structures, and from some global variables like `beg_unchanged' and
65 `end_unchanged'. The contents of the display are additionally
66 recorded in a `glyph matrix', a two-dimensional matrix of glyph
67 structures. Each row in such a matrix corresponds to a line on the
68 display, and each glyph in a row corresponds to a column displaying
69 a character, an image, or what else. This matrix is called the
70 `current glyph matrix' or `current matrix' in redisplay
71 terminology.
73 For buffer parts that have been changed since the last update, a
74 second glyph matrix is constructed, the so called `desired glyph
75 matrix' or short `desired matrix'. Current and desired matrix are
76 then compared to find a cheap way to update the display, e.g. by
77 reusing part of the display by scrolling lines.
79 You will find a lot of redisplay optimizations when you start
80 looking at the innards of redisplay. The overall goal of all these
81 optimizations is to make redisplay fast because it is done
82 frequently. Some of these optimizations are implemented by the
83 following functions:
85 . try_cursor_movement
87 This function tries to update the display if the text in the
88 window did not change and did not scroll, only point moved, and
89 it did not move off the displayed portion of the text.
91 . try_window_reusing_current_matrix
93 This function reuses the current matrix of a window when text
94 has not changed, but the window start changed (e.g., due to
95 scrolling).
97 . try_window_id
99 This function attempts to redisplay a window by reusing parts of
100 its existing display. It finds and reuses the part that was not
101 changed, and redraws the rest. (The "id" part in the function's
102 name stands for "insert/delete", not for "identification" or
103 somesuch.)
105 . try_window
107 This function performs the full redisplay of a single window
108 assuming that its fonts were not changed and that the cursor
109 will not end up in the scroll margins. (Loading fonts requires
110 re-adjustment of dimensions of glyph matrices, which makes this
111 method impossible to use.)
113 These optimizations are tried in sequence (some can be skipped if
114 it is known that they are not applicable). If none of the
115 optimizations were successful, redisplay calls redisplay_windows,
116 which performs a full redisplay of all windows.
118 Note that there's one more important optimization up Emacs's
119 sleeve, but it is related to actually redrawing the potentially
120 changed portions of the window/frame, not to reproducing the
121 desired matrices of those potentially changed portions. Namely,
122 the function update_frame and its subroutines, which you will find
123 in dispnew.c, compare the desired matrices with the current
124 matrices, and only redraw the portions that changed. So it could
125 happen that the functions in this file for some reason decide that
126 the entire desired matrix needs to be regenerated from scratch, and
127 still only parts of the Emacs display, or even nothing at all, will
128 be actually delivered to the glass, because update_frame has found
129 that the new and the old screen contents are similar or identical.
131 Desired matrices.
133 Desired matrices are always built per Emacs window. The function
134 `display_line' is the central function to look at if you are
135 interested. It constructs one row in a desired matrix given an
136 iterator structure containing both a buffer position and a
137 description of the environment in which the text is to be
138 displayed. But this is too early, read on.
140 Characters and pixmaps displayed for a range of buffer text depend
141 on various settings of buffers and windows, on overlays and text
142 properties, on display tables, on selective display. The good news
143 is that all this hairy stuff is hidden behind a small set of
144 interface functions taking an iterator structure (struct it)
145 argument.
147 Iteration over things to be displayed is then simple. It is
148 started by initializing an iterator with a call to init_iterator,
149 passing it the buffer position where to start iteration. For
150 iteration over strings, pass -1 as the position to init_iterator,
151 and call reseat_to_string when the string is ready, to initialize
152 the iterator for that string. Thereafter, calls to
153 get_next_display_element fill the iterator structure with relevant
154 information about the next thing to display. Calls to
155 set_iterator_to_next move the iterator to the next thing.
157 Besides this, an iterator also contains information about the
158 display environment in which glyphs for display elements are to be
159 produced. It has fields for the width and height of the display,
160 the information whether long lines are truncated or continued, a
161 current X and Y position, and lots of other stuff you can better
162 see in dispextern.h.
164 Glyphs in a desired matrix are normally constructed in a loop
165 calling get_next_display_element and then PRODUCE_GLYPHS. The call
166 to PRODUCE_GLYPHS will fill the iterator structure with pixel
167 information about the element being displayed and at the same time
168 produce glyphs for it. If the display element fits on the line
169 being displayed, set_iterator_to_next is called next, otherwise the
170 glyphs produced are discarded. The function display_line is the
171 workhorse of filling glyph rows in the desired matrix with glyphs.
172 In addition to producing glyphs, it also handles line truncation
173 and continuation, word wrap, and cursor positioning (for the
174 latter, see also set_cursor_from_row).
176 Frame matrices.
178 That just couldn't be all, could it? What about terminal types not
179 supporting operations on sub-windows of the screen? To update the
180 display on such a terminal, window-based glyph matrices are not
181 well suited. To be able to reuse part of the display (scrolling
182 lines up and down), we must instead have a view of the whole
183 screen. This is what `frame matrices' are for. They are a trick.
185 Frames on terminals like above have a glyph pool. Windows on such
186 a frame sub-allocate their glyph memory from their frame's glyph
187 pool. The frame itself is given its own glyph matrices. By
188 coincidence---or maybe something else---rows in window glyph
189 matrices are slices of corresponding rows in frame matrices. Thus
190 writing to window matrices implicitly updates a frame matrix which
191 provides us with the view of the whole screen that we originally
192 wanted to have without having to move many bytes around. To be
193 honest, there is a little bit more done, but not much more. If you
194 plan to extend that code, take a look at dispnew.c. The function
195 build_frame_matrix is a good starting point.
197 Bidirectional display.
199 Bidirectional display adds quite some hair to this already complex
200 design. The good news are that a large portion of that hairy stuff
201 is hidden in bidi.c behind only 3 interfaces. bidi.c implements a
202 reordering engine which is called by set_iterator_to_next and
203 returns the next character to display in the visual order. See
204 commentary on bidi.c for more details. As far as redisplay is
205 concerned, the effect of calling bidi_move_to_visually_next, the
206 main interface of the reordering engine, is that the iterator gets
207 magically placed on the buffer or string position that is to be
208 displayed next. In other words, a linear iteration through the
209 buffer/string is replaced with a non-linear one. All the rest of
210 the redisplay is oblivious to the bidi reordering.
212 Well, almost oblivious---there are still complications, most of
213 them due to the fact that buffer and string positions no longer
214 change monotonously with glyph indices in a glyph row. Moreover,
215 for continued lines, the buffer positions may not even be
216 monotonously changing with vertical positions. Also, accounting
217 for face changes, overlays, etc. becomes more complex because
218 non-linear iteration could potentially skip many positions with
219 changes, and then cross them again on the way back...
221 One other prominent effect of bidirectional display is that some
222 paragraphs of text need to be displayed starting at the right
223 margin of the window---the so-called right-to-left, or R2L
224 paragraphs. R2L paragraphs are displayed with R2L glyph rows,
225 which have their reversed_p flag set. The bidi reordering engine
226 produces characters in such rows starting from the character which
227 should be the rightmost on display. PRODUCE_GLYPHS then reverses
228 the order, when it fills up the glyph row whose reversed_p flag is
229 set, by prepending each new glyph to what is already there, instead
230 of appending it. When the glyph row is complete, the function
231 extend_face_to_end_of_line fills the empty space to the left of the
232 leftmost character with special glyphs, which will display as,
233 well, empty. On text terminals, these special glyphs are simply
234 blank characters. On graphics terminals, there's a single stretch
235 glyph of a suitably computed width. Both the blanks and the
236 stretch glyph are given the face of the background of the line.
237 This way, the terminal-specific back-end can still draw the glyphs
238 left to right, even for R2L lines.
240 Bidirectional display and character compositions
242 Some scripts cannot be displayed by drawing each character
243 individually, because adjacent characters change each other's shape
244 on display. For example, Arabic and Indic scripts belong to this
245 category.
247 Emacs display supports this by providing "character compositions",
248 most of which is implemented in composite.c. During the buffer
249 scan that delivers characters to PRODUCE_GLYPHS, if the next
250 character to be delivered is a composed character, the iteration
251 calls composition_reseat_it and next_element_from_composition. If
252 they succeed to compose the character with one or more of the
253 following characters, the whole sequence of characters that where
254 composed is recorded in the `struct composition_it' object that is
255 part of the buffer iterator. The composed sequence could produce
256 one or more font glyphs (called "grapheme clusters") on the screen.
257 Each of these grapheme clusters is then delivered to PRODUCE_GLYPHS
258 in the direction corresponding to the current bidi scan direction
259 (recorded in the scan_dir member of the `struct bidi_it' object
260 that is part of the buffer iterator). In particular, if the bidi
261 iterator currently scans the buffer backwards, the grapheme
262 clusters are delivered back to front. This reorders the grapheme
263 clusters as appropriate for the current bidi context. Note that
264 this means that the grapheme clusters are always stored in the
265 LGSTRING object (see composite.c) in the logical order.
267 Moving an iterator in bidirectional text
268 without producing glyphs
270 Note one important detail mentioned above: that the bidi reordering
271 engine, driven by the iterator, produces characters in R2L rows
272 starting at the character that will be the rightmost on display.
273 As far as the iterator is concerned, the geometry of such rows is
274 still left to right, i.e. the iterator "thinks" the first character
275 is at the leftmost pixel position. The iterator does not know that
276 PRODUCE_GLYPHS reverses the order of the glyphs that the iterator
277 delivers. This is important when functions from the move_it_*
278 family are used to get to certain screen position or to match
279 screen coordinates with buffer coordinates: these functions use the
280 iterator geometry, which is left to right even in R2L paragraphs.
281 This works well with most callers of move_it_*, because they need
282 to get to a specific column, and columns are still numbered in the
283 reading order, i.e. the rightmost character in a R2L paragraph is
284 still column zero. But some callers do not get well with this; a
285 notable example is mouse clicks that need to find the character
286 that corresponds to certain pixel coordinates. See
287 buffer_posn_from_coords in dispnew.c for how this is handled. */
289 #include <config.h>
290 #include <stdio.h>
291 #include <limits.h>
293 #include "lisp.h"
294 #include "atimer.h"
295 #include "keyboard.h"
296 #include "frame.h"
297 #include "window.h"
298 #include "termchar.h"
299 #include "dispextern.h"
300 #include "character.h"
301 #include "buffer.h"
302 #include "charset.h"
303 #include "indent.h"
304 #include "commands.h"
305 #include "keymap.h"
306 #include "macros.h"
307 #include "disptab.h"
308 #include "termhooks.h"
309 #include "termopts.h"
310 #include "intervals.h"
311 #include "coding.h"
312 #include "process.h"
313 #include "region-cache.h"
314 #include "font.h"
315 #include "fontset.h"
316 #include "blockinput.h"
317 #ifdef HAVE_WINDOW_SYSTEM
318 #include TERM_HEADER
319 #endif /* HAVE_WINDOW_SYSTEM */
321 #ifndef FRAME_X_OUTPUT
322 #define FRAME_X_OUTPUT(f) ((f)->output_data.x)
323 #endif
325 #define INFINITY 10000000
327 /* Holds the list (error). */
328 static Lisp_Object list_of_error;
330 #ifdef HAVE_WINDOW_SYSTEM
332 /* Test if overflow newline into fringe. Called with iterator IT
333 at or past right window margin, and with IT->current_x set. */
335 #define IT_OVERFLOW_NEWLINE_INTO_FRINGE(IT) \
336 (!NILP (Voverflow_newline_into_fringe) \
337 && FRAME_WINDOW_P ((IT)->f) \
338 && ((IT)->bidi_it.paragraph_dir == R2L \
339 ? (WINDOW_LEFT_FRINGE_WIDTH ((IT)->w) > 0) \
340 : (WINDOW_RIGHT_FRINGE_WIDTH ((IT)->w) > 0)) \
341 && (IT)->current_x == (IT)->last_visible_x)
343 #else /* !HAVE_WINDOW_SYSTEM */
344 #define IT_OVERFLOW_NEWLINE_INTO_FRINGE(it) 0
345 #endif /* HAVE_WINDOW_SYSTEM */
347 /* Test if the display element loaded in IT, or the underlying buffer
348 or string character, is a space or a TAB character. This is used
349 to determine where word wrapping can occur. */
351 #define IT_DISPLAYING_WHITESPACE(it) \
352 ((it->what == IT_CHARACTER && (it->c == ' ' || it->c == '\t')) \
353 || ((STRINGP (it->string) \
354 && (SREF (it->string, IT_STRING_BYTEPOS (*it)) == ' ' \
355 || SREF (it->string, IT_STRING_BYTEPOS (*it)) == '\t')) \
356 || (it->s \
357 && (it->s[IT_BYTEPOS (*it)] == ' ' \
358 || it->s[IT_BYTEPOS (*it)] == '\t')) \
359 || (IT_BYTEPOS (*it) < ZV_BYTE \
360 && (*BYTE_POS_ADDR (IT_BYTEPOS (*it)) == ' ' \
361 || *BYTE_POS_ADDR (IT_BYTEPOS (*it)) == '\t')))) \
363 /* Non-zero means print newline to stdout before next mini-buffer
364 message. */
366 bool noninteractive_need_newline;
368 /* Non-zero means print newline to message log before next message. */
370 static bool message_log_need_newline;
372 /* Three markers that message_dolog uses.
373 It could allocate them itself, but that causes trouble
374 in handling memory-full errors. */
375 static Lisp_Object message_dolog_marker1;
376 static Lisp_Object message_dolog_marker2;
377 static Lisp_Object message_dolog_marker3;
379 /* The buffer position of the first character appearing entirely or
380 partially on the line of the selected window which contains the
381 cursor; <= 0 if not known. Set by set_cursor_from_row, used for
382 redisplay optimization in redisplay_internal. */
384 static struct text_pos this_line_start_pos;
386 /* Number of characters past the end of the line above, including the
387 terminating newline. */
389 static struct text_pos this_line_end_pos;
391 /* The vertical positions and the height of this line. */
393 static int this_line_vpos;
394 static int this_line_y;
395 static int this_line_pixel_height;
397 /* X position at which this display line starts. Usually zero;
398 negative if first character is partially visible. */
400 static int this_line_start_x;
402 /* The smallest character position seen by move_it_* functions as they
403 move across display lines. Used to set MATRIX_ROW_START_CHARPOS of
404 hscrolled lines, see display_line. */
406 static struct text_pos this_line_min_pos;
408 /* Buffer that this_line_.* variables are referring to. */
410 static struct buffer *this_line_buffer;
412 /* Nonzero if an overlay arrow has been displayed in this window. */
414 static bool overlay_arrow_seen;
416 /* Vector containing glyphs for an ellipsis `...'. */
418 static Lisp_Object default_invis_vector[3];
420 /* This is the window where the echo area message was displayed. It
421 is always a mini-buffer window, but it may not be the same window
422 currently active as a mini-buffer. */
424 Lisp_Object echo_area_window;
426 /* List of pairs (MESSAGE . MULTIBYTE). The function save_message
427 pushes the current message and the value of
428 message_enable_multibyte on the stack, the function restore_message
429 pops the stack and displays MESSAGE again. */
431 static Lisp_Object Vmessage_stack;
433 /* Nonzero means multibyte characters were enabled when the echo area
434 message was specified. */
436 static bool message_enable_multibyte;
438 /* Nonzero if we should redraw the mode lines on the next redisplay.
439 If it has value REDISPLAY_SOME, then only redisplay the mode lines where
440 the `redisplay' bit has been set. Otherwise, redisplay all mode lines
441 (the number used is then only used to track down the cause for this
442 full-redisplay). */
444 int update_mode_lines;
446 /* Nonzero if window sizes or contents other than selected-window have changed
447 since last redisplay that finished.
448 If it has value REDISPLAY_SOME, then only redisplay the windows where
449 the `redisplay' bit has been set. Otherwise, redisplay all windows
450 (the number used is then only used to track down the cause for this
451 full-redisplay). */
453 int windows_or_buffers_changed;
455 /* Nonzero after display_mode_line if %l was used and it displayed a
456 line number. */
458 static bool line_number_displayed;
460 /* The name of the *Messages* buffer, a string. */
462 static Lisp_Object Vmessages_buffer_name;
464 /* Current, index 0, and last displayed echo area message. Either
465 buffers from echo_buffers, or nil to indicate no message. */
467 Lisp_Object echo_area_buffer[2];
469 /* The buffers referenced from echo_area_buffer. */
471 static Lisp_Object echo_buffer[2];
473 /* A vector saved used in with_area_buffer to reduce consing. */
475 static Lisp_Object Vwith_echo_area_save_vector;
477 /* Non-zero means display_echo_area should display the last echo area
478 message again. Set by redisplay_preserve_echo_area. */
480 static bool display_last_displayed_message_p;
482 /* Nonzero if echo area is being used by print; zero if being used by
483 message. */
485 static bool message_buf_print;
487 /* Set to 1 in clear_message to make redisplay_internal aware
488 of an emptied echo area. */
490 static bool message_cleared_p;
492 /* A scratch glyph row with contents used for generating truncation
493 glyphs. Also used in direct_output_for_insert. */
495 #define MAX_SCRATCH_GLYPHS 100
496 static struct glyph_row scratch_glyph_row;
497 static struct glyph scratch_glyphs[MAX_SCRATCH_GLYPHS];
499 /* Ascent and height of the last line processed by move_it_to. */
501 static int last_height;
503 /* Non-zero if there's a help-echo in the echo area. */
505 bool help_echo_showing_p;
507 /* The maximum distance to look ahead for text properties. Values
508 that are too small let us call compute_char_face and similar
509 functions too often which is expensive. Values that are too large
510 let us call compute_char_face and alike too often because we
511 might not be interested in text properties that far away. */
513 #define TEXT_PROP_DISTANCE_LIMIT 100
515 /* SAVE_IT and RESTORE_IT are called when we save a snapshot of the
516 iterator state and later restore it. This is needed because the
517 bidi iterator on bidi.c keeps a stacked cache of its states, which
518 is really a singleton. When we use scratch iterator objects to
519 move around the buffer, we can cause the bidi cache to be pushed or
520 popped, and therefore we need to restore the cache state when we
521 return to the original iterator. */
522 #define SAVE_IT(ITCOPY,ITORIG,CACHE) \
523 do { \
524 if (CACHE) \
525 bidi_unshelve_cache (CACHE, 1); \
526 ITCOPY = ITORIG; \
527 CACHE = bidi_shelve_cache (); \
528 } while (0)
530 #define RESTORE_IT(pITORIG,pITCOPY,CACHE) \
531 do { \
532 if (pITORIG != pITCOPY) \
533 *(pITORIG) = *(pITCOPY); \
534 bidi_unshelve_cache (CACHE, 0); \
535 CACHE = NULL; \
536 } while (0)
538 /* Functions to mark elements as needing redisplay. */
539 enum { REDISPLAY_SOME = 2}; /* Arbitrary choice. */
541 void
542 redisplay_other_windows (void)
544 if (!windows_or_buffers_changed)
545 windows_or_buffers_changed = REDISPLAY_SOME;
548 void
549 wset_redisplay (struct window *w)
551 /* Beware: selected_window can be nil during early stages. */
552 if (!EQ (make_lisp_ptr (w, Lisp_Vectorlike), selected_window))
553 redisplay_other_windows ();
554 w->redisplay = true;
557 void
558 fset_redisplay (struct frame *f)
560 redisplay_other_windows ();
561 f->redisplay = true;
564 void
565 bset_redisplay (struct buffer *b)
567 int count = buffer_window_count (b);
568 if (count > 0)
570 /* ... it's visible in other window than selected, */
571 if (count > 1 || b != XBUFFER (XWINDOW (selected_window)->contents))
572 redisplay_other_windows ();
573 /* Even if we don't set windows_or_buffers_changed, do set `redisplay'
574 so that if we later set windows_or_buffers_changed, this buffer will
575 not be omitted. */
576 b->text->redisplay = true;
580 void
581 bset_update_mode_line (struct buffer *b)
583 if (!update_mode_lines)
584 update_mode_lines = REDISPLAY_SOME;
585 b->text->redisplay = true;
588 #ifdef GLYPH_DEBUG
590 /* Non-zero means print traces of redisplay if compiled with
591 GLYPH_DEBUG defined. */
593 bool trace_redisplay_p;
595 #endif /* GLYPH_DEBUG */
597 #ifdef DEBUG_TRACE_MOVE
598 /* Non-zero means trace with TRACE_MOVE to stderr. */
599 int trace_move;
601 #define TRACE_MOVE(x) if (trace_move) fprintf x; else (void) 0
602 #else
603 #define TRACE_MOVE(x) (void) 0
604 #endif
606 /* Buffer being redisplayed -- for redisplay_window_error. */
608 static struct buffer *displayed_buffer;
610 /* Value returned from text property handlers (see below). */
612 enum prop_handled
614 HANDLED_NORMALLY,
615 HANDLED_RECOMPUTE_PROPS,
616 HANDLED_OVERLAY_STRING_CONSUMED,
617 HANDLED_RETURN
620 /* A description of text properties that redisplay is interested
621 in. */
623 struct props
625 /* The symbol index of the name of the property. */
626 short name;
628 /* A unique index for the property. */
629 enum prop_idx idx;
631 /* A handler function called to set up iterator IT from the property
632 at IT's current position. Value is used to steer handle_stop. */
633 enum prop_handled (*handler) (struct it *it);
636 static enum prop_handled handle_face_prop (struct it *);
637 static enum prop_handled handle_invisible_prop (struct it *);
638 static enum prop_handled handle_display_prop (struct it *);
639 static enum prop_handled handle_composition_prop (struct it *);
640 static enum prop_handled handle_overlay_change (struct it *);
641 static enum prop_handled handle_fontified_prop (struct it *);
643 /* Properties handled by iterators. */
645 static struct props it_props[] =
647 {SYMBOL_INDEX (Qfontified), FONTIFIED_PROP_IDX, handle_fontified_prop},
648 /* Handle `face' before `display' because some sub-properties of
649 `display' need to know the face. */
650 {SYMBOL_INDEX (Qface), FACE_PROP_IDX, handle_face_prop},
651 {SYMBOL_INDEX (Qdisplay), DISPLAY_PROP_IDX, handle_display_prop},
652 {SYMBOL_INDEX (Qinvisible), INVISIBLE_PROP_IDX, handle_invisible_prop},
653 {SYMBOL_INDEX (Qcomposition), COMPOSITION_PROP_IDX, handle_composition_prop},
654 {0, 0, NULL}
657 /* Value is the position described by X. If X is a marker, value is
658 the marker_position of X. Otherwise, value is X. */
660 #define COERCE_MARKER(X) (MARKERP ((X)) ? Fmarker_position (X) : (X))
662 /* Enumeration returned by some move_it_.* functions internally. */
664 enum move_it_result
666 /* Not used. Undefined value. */
667 MOVE_UNDEFINED,
669 /* Move ended at the requested buffer position or ZV. */
670 MOVE_POS_MATCH_OR_ZV,
672 /* Move ended at the requested X pixel position. */
673 MOVE_X_REACHED,
675 /* Move within a line ended at the end of a line that must be
676 continued. */
677 MOVE_LINE_CONTINUED,
679 /* Move within a line ended at the end of a line that would
680 be displayed truncated. */
681 MOVE_LINE_TRUNCATED,
683 /* Move within a line ended at a line end. */
684 MOVE_NEWLINE_OR_CR
687 /* This counter is used to clear the face cache every once in a while
688 in redisplay_internal. It is incremented for each redisplay.
689 Every CLEAR_FACE_CACHE_COUNT full redisplays, the face cache is
690 cleared. */
692 #define CLEAR_FACE_CACHE_COUNT 500
693 static int clear_face_cache_count;
695 /* Similarly for the image cache. */
697 #ifdef HAVE_WINDOW_SYSTEM
698 #define CLEAR_IMAGE_CACHE_COUNT 101
699 static int clear_image_cache_count;
701 /* Null glyph slice */
702 static struct glyph_slice null_glyph_slice = { 0, 0, 0, 0 };
703 #endif
705 /* True while redisplay_internal is in progress. */
707 bool redisplaying_p;
709 /* If a string, XTread_socket generates an event to display that string.
710 (The display is done in read_char.) */
712 Lisp_Object help_echo_string;
713 Lisp_Object help_echo_window;
714 Lisp_Object help_echo_object;
715 ptrdiff_t help_echo_pos;
717 /* Temporary variable for XTread_socket. */
719 Lisp_Object previous_help_echo_string;
721 /* Platform-independent portion of hourglass implementation. */
723 #ifdef HAVE_WINDOW_SYSTEM
725 /* Non-zero means an hourglass cursor is currently shown. */
726 static bool hourglass_shown_p;
728 /* If non-null, an asynchronous timer that, when it expires, displays
729 an hourglass cursor on all frames. */
730 static struct atimer *hourglass_atimer;
732 #endif /* HAVE_WINDOW_SYSTEM */
734 /* Default number of seconds to wait before displaying an hourglass
735 cursor. */
736 #define DEFAULT_HOURGLASS_DELAY 1
738 #ifdef HAVE_WINDOW_SYSTEM
740 /* Default pixel width of `thin-space' display method. */
741 #define THIN_SPACE_WIDTH 1
743 #endif /* HAVE_WINDOW_SYSTEM */
745 /* Function prototypes. */
747 static void setup_for_ellipsis (struct it *, int);
748 static void set_iterator_to_next (struct it *, int);
749 static void mark_window_display_accurate_1 (struct window *, int);
750 static int single_display_spec_string_p (Lisp_Object, Lisp_Object);
751 static int display_prop_string_p (Lisp_Object, Lisp_Object);
752 static int row_for_charpos_p (struct glyph_row *, ptrdiff_t);
753 static int cursor_row_p (struct glyph_row *);
754 static int redisplay_mode_lines (Lisp_Object, bool);
755 static char *decode_mode_spec_coding (Lisp_Object, char *, int);
757 static Lisp_Object get_it_property (struct it *it, Lisp_Object prop);
759 static void handle_line_prefix (struct it *);
761 static void pint2str (char *, int, ptrdiff_t);
762 static void pint2hrstr (char *, int, ptrdiff_t);
763 static struct text_pos run_window_scroll_functions (Lisp_Object,
764 struct text_pos);
765 static int text_outside_line_unchanged_p (struct window *,
766 ptrdiff_t, ptrdiff_t);
767 static void store_mode_line_noprop_char (char);
768 static int store_mode_line_noprop (const char *, int, int);
769 static void handle_stop (struct it *);
770 static void handle_stop_backwards (struct it *, ptrdiff_t);
771 static void vmessage (const char *, va_list) ATTRIBUTE_FORMAT_PRINTF (1, 0);
772 static void ensure_echo_area_buffers (void);
773 static void unwind_with_echo_area_buffer (Lisp_Object);
774 static Lisp_Object with_echo_area_buffer_unwind_data (struct window *);
775 static int with_echo_area_buffer (struct window *, int,
776 int (*) (ptrdiff_t, Lisp_Object),
777 ptrdiff_t, Lisp_Object);
778 static void clear_garbaged_frames (void);
779 static int current_message_1 (ptrdiff_t, Lisp_Object);
780 static int truncate_message_1 (ptrdiff_t, Lisp_Object);
781 static void set_message (Lisp_Object);
782 static int set_message_1 (ptrdiff_t, Lisp_Object);
783 static int display_echo_area (struct window *);
784 static int display_echo_area_1 (ptrdiff_t, Lisp_Object);
785 static int resize_mini_window_1 (ptrdiff_t, Lisp_Object);
786 static void unwind_redisplay (void);
787 static int string_char_and_length (const unsigned char *, int *);
788 static struct text_pos display_prop_end (struct it *, Lisp_Object,
789 struct text_pos);
790 static int compute_window_start_on_continuation_line (struct window *);
791 static void insert_left_trunc_glyphs (struct it *);
792 static struct glyph_row *get_overlay_arrow_glyph_row (struct window *,
793 Lisp_Object);
794 static void extend_face_to_end_of_line (struct it *);
795 static int append_space_for_newline (struct it *, int);
796 static int cursor_row_fully_visible_p (struct window *, int, int);
797 static int try_scrolling (Lisp_Object, int, ptrdiff_t, ptrdiff_t, int, int);
798 static int try_cursor_movement (Lisp_Object, struct text_pos, int *);
799 static int trailing_whitespace_p (ptrdiff_t);
800 static intmax_t message_log_check_duplicate (ptrdiff_t, ptrdiff_t);
801 static void push_it (struct it *, struct text_pos *);
802 static void iterate_out_of_display_property (struct it *);
803 static void pop_it (struct it *);
804 static void sync_frame_with_window_matrix_rows (struct window *);
805 static void redisplay_internal (void);
806 static bool echo_area_display (bool);
807 static void redisplay_windows (Lisp_Object);
808 static void redisplay_window (Lisp_Object, bool);
809 static Lisp_Object redisplay_window_error (Lisp_Object);
810 static Lisp_Object redisplay_window_0 (Lisp_Object);
811 static Lisp_Object redisplay_window_1 (Lisp_Object);
812 static int set_cursor_from_row (struct window *, struct glyph_row *,
813 struct glyph_matrix *, ptrdiff_t, ptrdiff_t,
814 int, int);
815 static int update_menu_bar (struct frame *, int, int);
816 static int try_window_reusing_current_matrix (struct window *);
817 static int try_window_id (struct window *);
818 static int display_line (struct it *);
819 static int display_mode_lines (struct window *);
820 static int display_mode_line (struct window *, enum face_id, Lisp_Object);
821 static int display_mode_element (struct it *, int, int, int, Lisp_Object, Lisp_Object, int);
822 static int store_mode_line_string (const char *, Lisp_Object, int, int, int, Lisp_Object);
823 static const char *decode_mode_spec (struct window *, int, int, Lisp_Object *);
824 static void display_menu_bar (struct window *);
825 static ptrdiff_t display_count_lines (ptrdiff_t, ptrdiff_t, ptrdiff_t,
826 ptrdiff_t *);
827 static int display_string (const char *, Lisp_Object, Lisp_Object,
828 ptrdiff_t, ptrdiff_t, struct it *, int, int, int, int);
829 static void compute_line_metrics (struct it *);
830 static void run_redisplay_end_trigger_hook (struct it *);
831 static int get_overlay_strings (struct it *, ptrdiff_t);
832 static int get_overlay_strings_1 (struct it *, ptrdiff_t, int);
833 static void next_overlay_string (struct it *);
834 static void reseat (struct it *, struct text_pos, int);
835 static void reseat_1 (struct it *, struct text_pos, int);
836 static void back_to_previous_visible_line_start (struct it *);
837 static void reseat_at_next_visible_line_start (struct it *, int);
838 static int next_element_from_ellipsis (struct it *);
839 static int next_element_from_display_vector (struct it *);
840 static int next_element_from_string (struct it *);
841 static int next_element_from_c_string (struct it *);
842 static int next_element_from_buffer (struct it *);
843 static int next_element_from_composition (struct it *);
844 static int next_element_from_image (struct it *);
845 static int next_element_from_stretch (struct it *);
846 static void load_overlay_strings (struct it *, ptrdiff_t);
847 static int init_from_display_pos (struct it *, struct window *,
848 struct display_pos *);
849 static void reseat_to_string (struct it *, const char *,
850 Lisp_Object, ptrdiff_t, ptrdiff_t, int, int);
851 static int get_next_display_element (struct it *);
852 static enum move_it_result
853 move_it_in_display_line_to (struct it *, ptrdiff_t, int,
854 enum move_operation_enum);
855 static void get_visually_first_element (struct it *);
856 static void init_to_row_start (struct it *, struct window *,
857 struct glyph_row *);
858 static int init_to_row_end (struct it *, struct window *,
859 struct glyph_row *);
860 static void back_to_previous_line_start (struct it *);
861 static int forward_to_next_line_start (struct it *, int *, struct bidi_it *);
862 static struct text_pos string_pos_nchars_ahead (struct text_pos,
863 Lisp_Object, ptrdiff_t);
864 static struct text_pos string_pos (ptrdiff_t, Lisp_Object);
865 static struct text_pos c_string_pos (ptrdiff_t, const char *, bool);
866 static ptrdiff_t number_of_chars (const char *, bool);
867 static void compute_stop_pos (struct it *);
868 static void compute_string_pos (struct text_pos *, struct text_pos,
869 Lisp_Object);
870 static int face_before_or_after_it_pos (struct it *, int);
871 static ptrdiff_t next_overlay_change (ptrdiff_t);
872 static int handle_display_spec (struct it *, Lisp_Object, Lisp_Object,
873 Lisp_Object, struct text_pos *, ptrdiff_t, int);
874 static int handle_single_display_spec (struct it *, Lisp_Object,
875 Lisp_Object, Lisp_Object,
876 struct text_pos *, ptrdiff_t, int, int);
877 static int underlying_face_id (struct it *);
878 static int in_ellipses_for_invisible_text_p (struct display_pos *,
879 struct window *);
881 #define face_before_it_pos(IT) face_before_or_after_it_pos ((IT), 1)
882 #define face_after_it_pos(IT) face_before_or_after_it_pos ((IT), 0)
884 #ifdef HAVE_WINDOW_SYSTEM
886 static void x_consider_frame_title (Lisp_Object);
887 static void update_tool_bar (struct frame *, int);
888 static int redisplay_tool_bar (struct frame *);
889 static void x_draw_bottom_divider (struct window *w);
890 static void notice_overwritten_cursor (struct window *,
891 enum glyph_row_area,
892 int, int, int, int);
893 static void append_stretch_glyph (struct it *, Lisp_Object,
894 int, int, int);
897 #endif /* HAVE_WINDOW_SYSTEM */
899 static void produce_special_glyphs (struct it *, enum display_element_type);
900 static void show_mouse_face (Mouse_HLInfo *, enum draw_glyphs_face);
901 static bool coords_in_mouse_face_p (struct window *, int, int);
905 /***********************************************************************
906 Window display dimensions
907 ***********************************************************************/
909 /* Return the bottom boundary y-position for text lines in window W.
910 This is the first y position at which a line cannot start.
911 It is relative to the top of the window.
913 This is the height of W minus the height of a mode line, if any. */
916 window_text_bottom_y (struct window *w)
918 int height = WINDOW_PIXEL_HEIGHT (w);
920 height -= WINDOW_BOTTOM_DIVIDER_WIDTH (w);
922 if (WINDOW_WANTS_MODELINE_P (w))
923 height -= CURRENT_MODE_LINE_HEIGHT (w);
925 height -= WINDOW_SCROLL_BAR_AREA_HEIGHT (w);
927 return height;
930 /* Return the pixel width of display area AREA of window W.
931 ANY_AREA means return the total width of W, not including
932 fringes to the left and right of the window. */
935 window_box_width (struct window *w, enum glyph_row_area area)
937 int width = w->pixel_width;
939 if (!w->pseudo_window_p)
941 width -= WINDOW_SCROLL_BAR_AREA_WIDTH (w);
942 width -= WINDOW_RIGHT_DIVIDER_WIDTH (w);
944 if (area == TEXT_AREA)
945 width -= (WINDOW_MARGINS_WIDTH (w)
946 + WINDOW_FRINGES_WIDTH (w));
947 else if (area == LEFT_MARGIN_AREA)
948 width = WINDOW_LEFT_MARGIN_WIDTH (w);
949 else if (area == RIGHT_MARGIN_AREA)
950 width = WINDOW_RIGHT_MARGIN_WIDTH (w);
953 /* With wide margins, fringes, etc. we might end up with a negative
954 width, correct that here. */
955 return max (0, width);
959 /* Return the pixel height of the display area of window W, not
960 including mode lines of W, if any. */
963 window_box_height (struct window *w)
965 struct frame *f = XFRAME (w->frame);
966 int height = WINDOW_PIXEL_HEIGHT (w);
968 eassert (height >= 0);
970 height -= WINDOW_BOTTOM_DIVIDER_WIDTH (w);
971 height -= WINDOW_SCROLL_BAR_AREA_HEIGHT (w);
973 /* Note: the code below that determines the mode-line/header-line
974 height is essentially the same as that contained in the macro
975 CURRENT_{MODE,HEADER}_LINE_HEIGHT, except that it checks whether
976 the appropriate glyph row has its `mode_line_p' flag set,
977 and if it doesn't, uses estimate_mode_line_height instead. */
979 if (WINDOW_WANTS_MODELINE_P (w))
981 struct glyph_row *ml_row
982 = (w->current_matrix && w->current_matrix->rows
983 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
984 : 0);
985 if (ml_row && ml_row->mode_line_p)
986 height -= ml_row->height;
987 else
988 height -= estimate_mode_line_height (f, CURRENT_MODE_LINE_FACE_ID (w));
991 if (WINDOW_WANTS_HEADER_LINE_P (w))
993 struct glyph_row *hl_row
994 = (w->current_matrix && w->current_matrix->rows
995 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
996 : 0);
997 if (hl_row && hl_row->mode_line_p)
998 height -= hl_row->height;
999 else
1000 height -= estimate_mode_line_height (f, HEADER_LINE_FACE_ID);
1003 /* With a very small font and a mode-line that's taller than
1004 default, we might end up with a negative height. */
1005 return max (0, height);
1008 /* Return the window-relative coordinate of the left edge of display
1009 area AREA of window W. ANY_AREA means return the left edge of the
1010 whole window, to the right of the left fringe of W. */
1013 window_box_left_offset (struct window *w, enum glyph_row_area area)
1015 int x;
1017 if (w->pseudo_window_p)
1018 return 0;
1020 x = WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
1022 if (area == TEXT_AREA)
1023 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1024 + window_box_width (w, LEFT_MARGIN_AREA));
1025 else if (area == RIGHT_MARGIN_AREA)
1026 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1027 + window_box_width (w, LEFT_MARGIN_AREA)
1028 + window_box_width (w, TEXT_AREA)
1029 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
1031 : WINDOW_RIGHT_FRINGE_WIDTH (w)));
1032 else if (area == LEFT_MARGIN_AREA
1033 && WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w))
1034 x += WINDOW_LEFT_FRINGE_WIDTH (w);
1036 /* Don't return more than the window's pixel width. */
1037 return min (x, w->pixel_width);
1041 /* Return the window-relative coordinate of the right edge of display
1042 area AREA of window W. ANY_AREA means return the right edge of the
1043 whole window, to the left of the right fringe of W. */
1045 static int
1046 window_box_right_offset (struct window *w, enum glyph_row_area area)
1048 /* Don't return more than the window's pixel width. */
1049 return min (window_box_left_offset (w, area) + window_box_width (w, area),
1050 w->pixel_width);
1053 /* Return the frame-relative coordinate of the left edge of display
1054 area AREA of window W. ANY_AREA means return the left edge of the
1055 whole window, to the right of the left fringe of W. */
1058 window_box_left (struct window *w, enum glyph_row_area area)
1060 struct frame *f = XFRAME (w->frame);
1061 int x;
1063 if (w->pseudo_window_p)
1064 return FRAME_INTERNAL_BORDER_WIDTH (f);
1066 x = (WINDOW_LEFT_EDGE_X (w)
1067 + window_box_left_offset (w, area));
1069 return x;
1073 /* Return the frame-relative coordinate of the right edge of display
1074 area AREA of window W. ANY_AREA means return the right edge of the
1075 whole window, to the left of the right fringe of W. */
1078 window_box_right (struct window *w, enum glyph_row_area area)
1080 return window_box_left (w, area) + window_box_width (w, area);
1083 /* Get the bounding box of the display area AREA of window W, without
1084 mode lines, in frame-relative coordinates. ANY_AREA means the
1085 whole window, not including the left and right fringes of
1086 the window. Return in *BOX_X and *BOX_Y the frame-relative pixel
1087 coordinates of the upper-left corner of the box. Return in
1088 *BOX_WIDTH, and *BOX_HEIGHT the pixel width and height of the box. */
1090 void
1091 window_box (struct window *w, enum glyph_row_area area, int *box_x,
1092 int *box_y, int *box_width, int *box_height)
1094 if (box_width)
1095 *box_width = window_box_width (w, area);
1096 if (box_height)
1097 *box_height = window_box_height (w);
1098 if (box_x)
1099 *box_x = window_box_left (w, area);
1100 if (box_y)
1102 *box_y = WINDOW_TOP_EDGE_Y (w);
1103 if (WINDOW_WANTS_HEADER_LINE_P (w))
1104 *box_y += CURRENT_HEADER_LINE_HEIGHT (w);
1108 #ifdef HAVE_WINDOW_SYSTEM
1110 /* Get the bounding box of the display area AREA of window W, without
1111 mode lines and both fringes of the window. Return in *TOP_LEFT_X
1112 and TOP_LEFT_Y the frame-relative pixel coordinates of the
1113 upper-left corner of the box. Return in *BOTTOM_RIGHT_X, and
1114 *BOTTOM_RIGHT_Y the coordinates of the bottom-right corner of the
1115 box. */
1117 static void
1118 window_box_edges (struct window *w, int *top_left_x, int *top_left_y,
1119 int *bottom_right_x, int *bottom_right_y)
1121 window_box (w, ANY_AREA, top_left_x, top_left_y,
1122 bottom_right_x, bottom_right_y);
1123 *bottom_right_x += *top_left_x;
1124 *bottom_right_y += *top_left_y;
1127 #endif /* HAVE_WINDOW_SYSTEM */
1129 /***********************************************************************
1130 Utilities
1131 ***********************************************************************/
1133 /* Return the bottom y-position of the line the iterator IT is in.
1134 This can modify IT's settings. */
1137 line_bottom_y (struct it *it)
1139 int line_height = it->max_ascent + it->max_descent;
1140 int line_top_y = it->current_y;
1142 if (line_height == 0)
1144 if (last_height)
1145 line_height = last_height;
1146 else if (IT_CHARPOS (*it) < ZV)
1148 move_it_by_lines (it, 1);
1149 line_height = (it->max_ascent || it->max_descent
1150 ? it->max_ascent + it->max_descent
1151 : last_height);
1153 else
1155 struct glyph_row *row = it->glyph_row;
1157 /* Use the default character height. */
1158 it->glyph_row = NULL;
1159 it->what = IT_CHARACTER;
1160 it->c = ' ';
1161 it->len = 1;
1162 PRODUCE_GLYPHS (it);
1163 line_height = it->ascent + it->descent;
1164 it->glyph_row = row;
1168 return line_top_y + line_height;
1171 DEFUN ("line-pixel-height", Fline_pixel_height,
1172 Sline_pixel_height, 0, 0, 0,
1173 doc: /* Return height in pixels of text line in the selected window.
1175 Value is the height in pixels of the line at point. */)
1176 (void)
1178 struct it it;
1179 struct text_pos pt;
1180 struct window *w = XWINDOW (selected_window);
1181 struct buffer *old_buffer = NULL;
1182 Lisp_Object result;
1184 if (XBUFFER (w->contents) != current_buffer)
1186 old_buffer = current_buffer;
1187 set_buffer_internal_1 (XBUFFER (w->contents));
1189 SET_TEXT_POS (pt, PT, PT_BYTE);
1190 start_display (&it, w, pt);
1191 it.vpos = it.current_y = 0;
1192 last_height = 0;
1193 result = make_number (line_bottom_y (&it));
1194 if (old_buffer)
1195 set_buffer_internal_1 (old_buffer);
1197 return result;
1200 /* Return the default pixel height of text lines in window W. The
1201 value is the canonical height of the W frame's default font, plus
1202 any extra space required by the line-spacing variable or frame
1203 parameter.
1205 Implementation note: this ignores any line-spacing text properties
1206 put on the newline characters. This is because those properties
1207 only affect the _screen_ line ending in the newline (i.e., in a
1208 continued line, only the last screen line will be affected), which
1209 means only a small number of lines in a buffer can ever use this
1210 feature. Since this function is used to compute the default pixel
1211 equivalent of text lines in a window, we can safely ignore those
1212 few lines. For the same reasons, we ignore the line-height
1213 properties. */
1215 default_line_pixel_height (struct window *w)
1217 struct frame *f = WINDOW_XFRAME (w);
1218 int height = FRAME_LINE_HEIGHT (f);
1220 if (!FRAME_INITIAL_P (f) && BUFFERP (w->contents))
1222 struct buffer *b = XBUFFER (w->contents);
1223 Lisp_Object val = BVAR (b, extra_line_spacing);
1225 if (NILP (val))
1226 val = BVAR (&buffer_defaults, extra_line_spacing);
1227 if (!NILP (val))
1229 if (RANGED_INTEGERP (0, val, INT_MAX))
1230 height += XFASTINT (val);
1231 else if (FLOATP (val))
1233 int addon = XFLOAT_DATA (val) * height + 0.5;
1235 if (addon >= 0)
1236 height += addon;
1239 else
1240 height += f->extra_line_spacing;
1243 return height;
1246 /* Subroutine of pos_visible_p below. Extracts a display string, if
1247 any, from the display spec given as its argument. */
1248 static Lisp_Object
1249 string_from_display_spec (Lisp_Object spec)
1251 if (CONSP (spec))
1253 while (CONSP (spec))
1255 if (STRINGP (XCAR (spec)))
1256 return XCAR (spec);
1257 spec = XCDR (spec);
1260 else if (VECTORP (spec))
1262 ptrdiff_t i;
1264 for (i = 0; i < ASIZE (spec); i++)
1266 if (STRINGP (AREF (spec, i)))
1267 return AREF (spec, i);
1269 return Qnil;
1272 return spec;
1276 /* Limit insanely large values of W->hscroll on frame F to the largest
1277 value that will still prevent first_visible_x and last_visible_x of
1278 'struct it' from overflowing an int. */
1279 static int
1280 window_hscroll_limited (struct window *w, struct frame *f)
1282 ptrdiff_t window_hscroll = w->hscroll;
1283 int window_text_width = window_box_width (w, TEXT_AREA);
1284 int colwidth = FRAME_COLUMN_WIDTH (f);
1286 if (window_hscroll > (INT_MAX - window_text_width) / colwidth - 1)
1287 window_hscroll = (INT_MAX - window_text_width) / colwidth - 1;
1289 return window_hscroll;
1292 /* Return 1 if position CHARPOS is visible in window W.
1293 CHARPOS < 0 means return info about WINDOW_END position.
1294 If visible, set *X and *Y to pixel coordinates of top left corner.
1295 Set *RTOP and *RBOT to pixel height of an invisible area of glyph at POS.
1296 Set *ROWH and *VPOS to row's visible height and VPOS (row number). */
1299 pos_visible_p (struct window *w, ptrdiff_t charpos, int *x, int *y,
1300 int *rtop, int *rbot, int *rowh, int *vpos)
1302 struct it it;
1303 void *itdata = bidi_shelve_cache ();
1304 struct text_pos top;
1305 int visible_p = 0;
1306 struct buffer *old_buffer = NULL;
1307 bool r2l = false;
1309 if (FRAME_INITIAL_P (XFRAME (WINDOW_FRAME (w))))
1310 return visible_p;
1312 if (XBUFFER (w->contents) != current_buffer)
1314 old_buffer = current_buffer;
1315 set_buffer_internal_1 (XBUFFER (w->contents));
1318 SET_TEXT_POS_FROM_MARKER (top, w->start);
1319 /* Scrolling a minibuffer window via scroll bar when the echo area
1320 shows long text sometimes resets the minibuffer contents behind
1321 our backs. */
1322 if (CHARPOS (top) > ZV)
1323 SET_TEXT_POS (top, BEGV, BEGV_BYTE);
1325 /* Compute exact mode line heights. */
1326 if (WINDOW_WANTS_MODELINE_P (w))
1327 w->mode_line_height
1328 = display_mode_line (w, CURRENT_MODE_LINE_FACE_ID (w),
1329 BVAR (current_buffer, mode_line_format));
1331 if (WINDOW_WANTS_HEADER_LINE_P (w))
1332 w->header_line_height
1333 = display_mode_line (w, HEADER_LINE_FACE_ID,
1334 BVAR (current_buffer, header_line_format));
1336 start_display (&it, w, top);
1337 move_it_to (&it, charpos, -1, it.last_visible_y - 1, -1,
1338 (charpos >= 0 ? MOVE_TO_POS : 0) | MOVE_TO_Y);
1340 if (charpos >= 0
1341 && (((!it.bidi_p || it.bidi_it.scan_dir != -1)
1342 && IT_CHARPOS (it) >= charpos)
1343 /* When scanning backwards under bidi iteration, move_it_to
1344 stops at or _before_ CHARPOS, because it stops at or to
1345 the _right_ of the character at CHARPOS. */
1346 || (it.bidi_p && it.bidi_it.scan_dir == -1
1347 && IT_CHARPOS (it) <= charpos)))
1349 /* We have reached CHARPOS, or passed it. How the call to
1350 move_it_to can overshoot: (i) If CHARPOS is on invisible text
1351 or covered by a display property, move_it_to stops at the end
1352 of the invisible text, to the right of CHARPOS. (ii) If
1353 CHARPOS is in a display vector, move_it_to stops on its last
1354 glyph. */
1355 int top_x = it.current_x;
1356 int top_y = it.current_y;
1357 int window_top_y = WINDOW_HEADER_LINE_HEIGHT (w);
1358 int bottom_y;
1359 struct it save_it;
1360 void *save_it_data = NULL;
1362 /* Calling line_bottom_y may change it.method, it.position, etc. */
1363 SAVE_IT (save_it, it, save_it_data);
1364 last_height = 0;
1365 bottom_y = line_bottom_y (&it);
1366 if (top_y < window_top_y)
1367 visible_p = bottom_y > window_top_y;
1368 else if (top_y < it.last_visible_y)
1369 visible_p = 1;
1370 if (bottom_y >= it.last_visible_y
1371 && it.bidi_p && it.bidi_it.scan_dir == -1
1372 && IT_CHARPOS (it) < charpos)
1374 /* When the last line of the window is scanned backwards
1375 under bidi iteration, we could be duped into thinking
1376 that we have passed CHARPOS, when in fact move_it_to
1377 simply stopped short of CHARPOS because it reached
1378 last_visible_y. To see if that's what happened, we call
1379 move_it_to again with a slightly larger vertical limit,
1380 and see if it actually moved vertically; if it did, we
1381 didn't really reach CHARPOS, which is beyond window end. */
1382 /* Why 10? because we don't know how many canonical lines
1383 will the height of the next line(s) be. So we guess. */
1384 int ten_more_lines = 10 * default_line_pixel_height (w);
1386 move_it_to (&it, charpos, -1, bottom_y + ten_more_lines, -1,
1387 MOVE_TO_POS | MOVE_TO_Y);
1388 if (it.current_y > top_y)
1389 visible_p = 0;
1392 RESTORE_IT (&it, &save_it, save_it_data);
1393 if (visible_p)
1395 if (it.method == GET_FROM_DISPLAY_VECTOR)
1397 /* We stopped on the last glyph of a display vector.
1398 Try and recompute. Hack alert! */
1399 if (charpos < 2 || top.charpos >= charpos)
1400 top_x = it.glyph_row->x;
1401 else
1403 struct it it2, it2_prev;
1404 /* The idea is to get to the previous buffer
1405 position, consume the character there, and use
1406 the pixel coordinates we get after that. But if
1407 the previous buffer position is also displayed
1408 from a display vector, we need to consume all of
1409 the glyphs from that display vector. */
1410 start_display (&it2, w, top);
1411 move_it_to (&it2, charpos - 1, -1, -1, -1, MOVE_TO_POS);
1412 /* If we didn't get to CHARPOS - 1, there's some
1413 replacing display property at that position, and
1414 we stopped after it. That is exactly the place
1415 whose coordinates we want. */
1416 if (IT_CHARPOS (it2) != charpos - 1)
1417 it2_prev = it2;
1418 else
1420 /* Iterate until we get out of the display
1421 vector that displays the character at
1422 CHARPOS - 1. */
1423 do {
1424 get_next_display_element (&it2);
1425 PRODUCE_GLYPHS (&it2);
1426 it2_prev = it2;
1427 set_iterator_to_next (&it2, 1);
1428 } while (it2.method == GET_FROM_DISPLAY_VECTOR
1429 && IT_CHARPOS (it2) < charpos);
1431 if (ITERATOR_AT_END_OF_LINE_P (&it2_prev)
1432 || it2_prev.current_x > it2_prev.last_visible_x)
1433 top_x = it.glyph_row->x;
1434 else
1436 top_x = it2_prev.current_x;
1437 top_y = it2_prev.current_y;
1441 else if (IT_CHARPOS (it) != charpos)
1443 Lisp_Object cpos = make_number (charpos);
1444 Lisp_Object spec = Fget_char_property (cpos, Qdisplay, Qnil);
1445 Lisp_Object string = string_from_display_spec (spec);
1446 struct text_pos tpos;
1447 int replacing_spec_p;
1448 bool newline_in_string
1449 = (STRINGP (string)
1450 && memchr (SDATA (string), '\n', SBYTES (string)));
1452 SET_TEXT_POS (tpos, charpos, CHAR_TO_BYTE (charpos));
1453 replacing_spec_p
1454 = (!NILP (spec)
1455 && handle_display_spec (NULL, spec, Qnil, Qnil, &tpos,
1456 charpos, FRAME_WINDOW_P (it.f)));
1457 /* The tricky code below is needed because there's a
1458 discrepancy between move_it_to and how we set cursor
1459 when PT is at the beginning of a portion of text
1460 covered by a display property or an overlay with a
1461 display property, or the display line ends in a
1462 newline from a display string. move_it_to will stop
1463 _after_ such display strings, whereas
1464 set_cursor_from_row conspires with cursor_row_p to
1465 place the cursor on the first glyph produced from the
1466 display string. */
1468 /* We have overshoot PT because it is covered by a
1469 display property that replaces the text it covers.
1470 If the string includes embedded newlines, we are also
1471 in the wrong display line. Backtrack to the correct
1472 line, where the display property begins. */
1473 if (replacing_spec_p)
1475 Lisp_Object startpos, endpos;
1476 EMACS_INT start, end;
1477 struct it it3;
1478 int it3_moved;
1480 /* Find the first and the last buffer positions
1481 covered by the display string. */
1482 endpos =
1483 Fnext_single_char_property_change (cpos, Qdisplay,
1484 Qnil, Qnil);
1485 startpos =
1486 Fprevious_single_char_property_change (endpos, Qdisplay,
1487 Qnil, Qnil);
1488 start = XFASTINT (startpos);
1489 end = XFASTINT (endpos);
1490 /* Move to the last buffer position before the
1491 display property. */
1492 start_display (&it3, w, top);
1493 if (start > CHARPOS (top))
1494 move_it_to (&it3, start - 1, -1, -1, -1, MOVE_TO_POS);
1495 /* Move forward one more line if the position before
1496 the display string is a newline or if it is the
1497 rightmost character on a line that is
1498 continued or word-wrapped. */
1499 if (it3.method == GET_FROM_BUFFER
1500 && (it3.c == '\n'
1501 || FETCH_BYTE (IT_BYTEPOS (it3)) == '\n'))
1502 move_it_by_lines (&it3, 1);
1503 else if (move_it_in_display_line_to (&it3, -1,
1504 it3.current_x
1505 + it3.pixel_width,
1506 MOVE_TO_X)
1507 == MOVE_LINE_CONTINUED)
1509 move_it_by_lines (&it3, 1);
1510 /* When we are under word-wrap, the #$@%!
1511 move_it_by_lines moves 2 lines, so we need to
1512 fix that up. */
1513 if (it3.line_wrap == WORD_WRAP)
1514 move_it_by_lines (&it3, -1);
1517 /* Record the vertical coordinate of the display
1518 line where we wound up. */
1519 top_y = it3.current_y;
1520 if (it3.bidi_p)
1522 /* When characters are reordered for display,
1523 the character displayed to the left of the
1524 display string could be _after_ the display
1525 property in the logical order. Use the
1526 smallest vertical position of these two. */
1527 start_display (&it3, w, top);
1528 move_it_to (&it3, end + 1, -1, -1, -1, MOVE_TO_POS);
1529 if (it3.current_y < top_y)
1530 top_y = it3.current_y;
1532 /* Move from the top of the window to the beginning
1533 of the display line where the display string
1534 begins. */
1535 start_display (&it3, w, top);
1536 move_it_to (&it3, -1, 0, top_y, -1, MOVE_TO_X | MOVE_TO_Y);
1537 /* If it3_moved stays zero after the 'while' loop
1538 below, that means we already were at a newline
1539 before the loop (e.g., the display string begins
1540 with a newline), so we don't need to (and cannot)
1541 inspect the glyphs of it3.glyph_row, because
1542 PRODUCE_GLYPHS will not produce anything for a
1543 newline, and thus it3.glyph_row stays at its
1544 stale content it got at top of the window. */
1545 it3_moved = 0;
1546 /* Finally, advance the iterator until we hit the
1547 first display element whose character position is
1548 CHARPOS, or until the first newline from the
1549 display string, which signals the end of the
1550 display line. */
1551 while (get_next_display_element (&it3))
1553 PRODUCE_GLYPHS (&it3);
1554 if (IT_CHARPOS (it3) == charpos
1555 || ITERATOR_AT_END_OF_LINE_P (&it3))
1556 break;
1557 it3_moved = 1;
1558 set_iterator_to_next (&it3, 0);
1560 top_x = it3.current_x - it3.pixel_width;
1561 /* Normally, we would exit the above loop because we
1562 found the display element whose character
1563 position is CHARPOS. For the contingency that we
1564 didn't, and stopped at the first newline from the
1565 display string, move back over the glyphs
1566 produced from the string, until we find the
1567 rightmost glyph not from the string. */
1568 if (it3_moved
1569 && newline_in_string
1570 && IT_CHARPOS (it3) != charpos && EQ (it3.object, string))
1572 struct glyph *g = it3.glyph_row->glyphs[TEXT_AREA]
1573 + it3.glyph_row->used[TEXT_AREA];
1575 while (EQ ((g - 1)->object, string))
1577 --g;
1578 top_x -= g->pixel_width;
1580 eassert (g < it3.glyph_row->glyphs[TEXT_AREA]
1581 + it3.glyph_row->used[TEXT_AREA]);
1586 *x = top_x;
1587 *y = max (top_y + max (0, it.max_ascent - it.ascent), window_top_y);
1588 *rtop = max (0, window_top_y - top_y);
1589 *rbot = max (0, bottom_y - it.last_visible_y);
1590 *rowh = max (0, (min (bottom_y, it.last_visible_y)
1591 - max (top_y, window_top_y)));
1592 *vpos = it.vpos;
1593 if (it.bidi_it.paragraph_dir == R2L)
1594 r2l = true;
1597 else
1599 /* Either we were asked to provide info about WINDOW_END, or
1600 CHARPOS is in the partially visible glyph row at end of
1601 window. */
1602 struct it it2;
1603 void *it2data = NULL;
1605 SAVE_IT (it2, it, it2data);
1606 if (IT_CHARPOS (it) < ZV && FETCH_BYTE (IT_BYTEPOS (it)) != '\n')
1607 move_it_by_lines (&it, 1);
1608 if (charpos < IT_CHARPOS (it)
1609 || (it.what == IT_EOB && charpos == IT_CHARPOS (it)))
1611 visible_p = true;
1612 RESTORE_IT (&it2, &it2, it2data);
1613 move_it_to (&it2, charpos, -1, -1, -1, MOVE_TO_POS);
1614 *x = it2.current_x;
1615 *y = it2.current_y + it2.max_ascent - it2.ascent;
1616 *rtop = max (0, -it2.current_y);
1617 *rbot = max (0, ((it2.current_y + it2.max_ascent + it2.max_descent)
1618 - it.last_visible_y));
1619 *rowh = max (0, (min (it2.current_y + it2.max_ascent + it2.max_descent,
1620 it.last_visible_y)
1621 - max (it2.current_y,
1622 WINDOW_HEADER_LINE_HEIGHT (w))));
1623 *vpos = it2.vpos;
1624 if (it2.bidi_it.paragraph_dir == R2L)
1625 r2l = true;
1627 else
1628 bidi_unshelve_cache (it2data, 1);
1630 bidi_unshelve_cache (itdata, 0);
1632 if (old_buffer)
1633 set_buffer_internal_1 (old_buffer);
1635 if (visible_p)
1637 if (w->hscroll > 0)
1638 *x -=
1639 window_hscroll_limited (w, WINDOW_XFRAME (w))
1640 * WINDOW_FRAME_COLUMN_WIDTH (w);
1641 /* For lines in an R2L paragraph, we need to mirror the X pixel
1642 coordinate wrt the text area. For the reasons, see the
1643 commentary in buffer_posn_from_coords and the explanation of
1644 the geometry used by the move_it_* functions at the end of
1645 the large commentary near the beginning of this file. */
1646 if (r2l)
1647 *x = window_box_width (w, TEXT_AREA) - *x - 1;
1650 #if 0
1651 /* Debugging code. */
1652 if (visible_p)
1653 fprintf (stderr, "+pv pt=%d vs=%d --> x=%d y=%d rt=%d rb=%d rh=%d vp=%d\n",
1654 charpos, w->vscroll, *x, *y, *rtop, *rbot, *rowh, *vpos);
1655 else
1656 fprintf (stderr, "-pv pt=%d vs=%d\n", charpos, w->vscroll);
1657 #endif
1659 return visible_p;
1663 /* Return the next character from STR. Return in *LEN the length of
1664 the character. This is like STRING_CHAR_AND_LENGTH but never
1665 returns an invalid character. If we find one, we return a `?', but
1666 with the length of the invalid character. */
1668 static int
1669 string_char_and_length (const unsigned char *str, int *len)
1671 int c;
1673 c = STRING_CHAR_AND_LENGTH (str, *len);
1674 if (!CHAR_VALID_P (c))
1675 /* We may not change the length here because other places in Emacs
1676 don't use this function, i.e. they silently accept invalid
1677 characters. */
1678 c = '?';
1680 return c;
1685 /* Given a position POS containing a valid character and byte position
1686 in STRING, return the position NCHARS ahead (NCHARS >= 0). */
1688 static struct text_pos
1689 string_pos_nchars_ahead (struct text_pos pos, Lisp_Object string, ptrdiff_t nchars)
1691 eassert (STRINGP (string) && nchars >= 0);
1693 if (STRING_MULTIBYTE (string))
1695 const unsigned char *p = SDATA (string) + BYTEPOS (pos);
1696 int len;
1698 while (nchars--)
1700 string_char_and_length (p, &len);
1701 p += len;
1702 CHARPOS (pos) += 1;
1703 BYTEPOS (pos) += len;
1706 else
1707 SET_TEXT_POS (pos, CHARPOS (pos) + nchars, BYTEPOS (pos) + nchars);
1709 return pos;
1713 /* Value is the text position, i.e. character and byte position,
1714 for character position CHARPOS in STRING. */
1716 static struct text_pos
1717 string_pos (ptrdiff_t charpos, Lisp_Object string)
1719 struct text_pos pos;
1720 eassert (STRINGP (string));
1721 eassert (charpos >= 0);
1722 SET_TEXT_POS (pos, charpos, string_char_to_byte (string, charpos));
1723 return pos;
1727 /* Value is a text position, i.e. character and byte position, for
1728 character position CHARPOS in C string S. MULTIBYTE_P non-zero
1729 means recognize multibyte characters. */
1731 static struct text_pos
1732 c_string_pos (ptrdiff_t charpos, const char *s, bool multibyte_p)
1734 struct text_pos pos;
1736 eassert (s != NULL);
1737 eassert (charpos >= 0);
1739 if (multibyte_p)
1741 int len;
1743 SET_TEXT_POS (pos, 0, 0);
1744 while (charpos--)
1746 string_char_and_length ((const unsigned char *) s, &len);
1747 s += len;
1748 CHARPOS (pos) += 1;
1749 BYTEPOS (pos) += len;
1752 else
1753 SET_TEXT_POS (pos, charpos, charpos);
1755 return pos;
1759 /* Value is the number of characters in C string S. MULTIBYTE_P
1760 non-zero means recognize multibyte characters. */
1762 static ptrdiff_t
1763 number_of_chars (const char *s, bool multibyte_p)
1765 ptrdiff_t nchars;
1767 if (multibyte_p)
1769 ptrdiff_t rest = strlen (s);
1770 int len;
1771 const unsigned char *p = (const unsigned char *) s;
1773 for (nchars = 0; rest > 0; ++nchars)
1775 string_char_and_length (p, &len);
1776 rest -= len, p += len;
1779 else
1780 nchars = strlen (s);
1782 return nchars;
1786 /* Compute byte position NEWPOS->bytepos corresponding to
1787 NEWPOS->charpos. POS is a known position in string STRING.
1788 NEWPOS->charpos must be >= POS.charpos. */
1790 static void
1791 compute_string_pos (struct text_pos *newpos, struct text_pos pos, Lisp_Object string)
1793 eassert (STRINGP (string));
1794 eassert (CHARPOS (*newpos) >= CHARPOS (pos));
1796 if (STRING_MULTIBYTE (string))
1797 *newpos = string_pos_nchars_ahead (pos, string,
1798 CHARPOS (*newpos) - CHARPOS (pos));
1799 else
1800 BYTEPOS (*newpos) = CHARPOS (*newpos);
1803 /* EXPORT:
1804 Return an estimation of the pixel height of mode or header lines on
1805 frame F. FACE_ID specifies what line's height to estimate. */
1808 estimate_mode_line_height (struct frame *f, enum face_id face_id)
1810 #ifdef HAVE_WINDOW_SYSTEM
1811 if (FRAME_WINDOW_P (f))
1813 int height = FONT_HEIGHT (FRAME_FONT (f));
1815 /* This function is called so early when Emacs starts that the face
1816 cache and mode line face are not yet initialized. */
1817 if (FRAME_FACE_CACHE (f))
1819 struct face *face = FACE_FROM_ID (f, face_id);
1820 if (face)
1822 if (face->font)
1823 height = FONT_HEIGHT (face->font);
1824 if (face->box_line_width > 0)
1825 height += 2 * face->box_line_width;
1829 return height;
1831 #endif
1833 return 1;
1836 /* Given a pixel position (PIX_X, PIX_Y) on frame F, return glyph
1837 co-ordinates in (*X, *Y). Set *BOUNDS to the rectangle that the
1838 glyph at X, Y occupies, if BOUNDS != 0. If NOCLIP is non-zero, do
1839 not force the value into range. */
1841 void
1842 pixel_to_glyph_coords (struct frame *f, register int pix_x, register int pix_y,
1843 int *x, int *y, NativeRectangle *bounds, int noclip)
1846 #ifdef HAVE_WINDOW_SYSTEM
1847 if (FRAME_WINDOW_P (f))
1849 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to round down
1850 even for negative values. */
1851 if (pix_x < 0)
1852 pix_x -= FRAME_COLUMN_WIDTH (f) - 1;
1853 if (pix_y < 0)
1854 pix_y -= FRAME_LINE_HEIGHT (f) - 1;
1856 pix_x = FRAME_PIXEL_X_TO_COL (f, pix_x);
1857 pix_y = FRAME_PIXEL_Y_TO_LINE (f, pix_y);
1859 if (bounds)
1860 STORE_NATIVE_RECT (*bounds,
1861 FRAME_COL_TO_PIXEL_X (f, pix_x),
1862 FRAME_LINE_TO_PIXEL_Y (f, pix_y),
1863 FRAME_COLUMN_WIDTH (f) - 1,
1864 FRAME_LINE_HEIGHT (f) - 1);
1866 /* PXW: Should we clip pixels before converting to columns/lines? */
1867 if (!noclip)
1869 if (pix_x < 0)
1870 pix_x = 0;
1871 else if (pix_x > FRAME_TOTAL_COLS (f))
1872 pix_x = FRAME_TOTAL_COLS (f);
1874 if (pix_y < 0)
1875 pix_y = 0;
1876 else if (pix_y > FRAME_TOTAL_LINES (f))
1877 pix_y = FRAME_TOTAL_LINES (f);
1880 #endif
1882 *x = pix_x;
1883 *y = pix_y;
1887 /* Find the glyph under window-relative coordinates X/Y in window W.
1888 Consider only glyphs from buffer text, i.e. no glyphs from overlay
1889 strings. Return in *HPOS and *VPOS the row and column number of
1890 the glyph found. Return in *AREA the glyph area containing X.
1891 Value is a pointer to the glyph found or null if X/Y is not on
1892 text, or we can't tell because W's current matrix is not up to
1893 date. */
1895 static struct glyph *
1896 x_y_to_hpos_vpos (struct window *w, int x, int y, int *hpos, int *vpos,
1897 int *dx, int *dy, int *area)
1899 struct glyph *glyph, *end;
1900 struct glyph_row *row = NULL;
1901 int x0, i;
1903 /* Find row containing Y. Give up if some row is not enabled. */
1904 for (i = 0; i < w->current_matrix->nrows; ++i)
1906 row = MATRIX_ROW (w->current_matrix, i);
1907 if (!row->enabled_p)
1908 return NULL;
1909 if (y >= row->y && y < MATRIX_ROW_BOTTOM_Y (row))
1910 break;
1913 *vpos = i;
1914 *hpos = 0;
1916 /* Give up if Y is not in the window. */
1917 if (i == w->current_matrix->nrows)
1918 return NULL;
1920 /* Get the glyph area containing X. */
1921 if (w->pseudo_window_p)
1923 *area = TEXT_AREA;
1924 x0 = 0;
1926 else
1928 if (x < window_box_left_offset (w, TEXT_AREA))
1930 *area = LEFT_MARGIN_AREA;
1931 x0 = window_box_left_offset (w, LEFT_MARGIN_AREA);
1933 else if (x < window_box_right_offset (w, TEXT_AREA))
1935 *area = TEXT_AREA;
1936 x0 = window_box_left_offset (w, TEXT_AREA) + min (row->x, 0);
1938 else
1940 *area = RIGHT_MARGIN_AREA;
1941 x0 = window_box_left_offset (w, RIGHT_MARGIN_AREA);
1945 /* Find glyph containing X. */
1946 glyph = row->glyphs[*area];
1947 end = glyph + row->used[*area];
1948 x -= x0;
1949 while (glyph < end && x >= glyph->pixel_width)
1951 x -= glyph->pixel_width;
1952 ++glyph;
1955 if (glyph == end)
1956 return NULL;
1958 if (dx)
1960 *dx = x;
1961 *dy = y - (row->y + row->ascent - glyph->ascent);
1964 *hpos = glyph - row->glyphs[*area];
1965 return glyph;
1968 /* Convert frame-relative x/y to coordinates relative to window W.
1969 Takes pseudo-windows into account. */
1971 static void
1972 frame_to_window_pixel_xy (struct window *w, int *x, int *y)
1974 if (w->pseudo_window_p)
1976 /* A pseudo-window is always full-width, and starts at the
1977 left edge of the frame, plus a frame border. */
1978 struct frame *f = XFRAME (w->frame);
1979 *x -= FRAME_INTERNAL_BORDER_WIDTH (f);
1980 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1982 else
1984 *x -= WINDOW_LEFT_EDGE_X (w);
1985 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1989 #ifdef HAVE_WINDOW_SYSTEM
1991 /* EXPORT:
1992 Return in RECTS[] at most N clipping rectangles for glyph string S.
1993 Return the number of stored rectangles. */
1996 get_glyph_string_clip_rects (struct glyph_string *s, NativeRectangle *rects, int n)
1998 XRectangle r;
2000 if (n <= 0)
2001 return 0;
2003 if (s->row->full_width_p)
2005 /* Draw full-width. X coordinates are relative to S->w->left_col. */
2006 r.x = WINDOW_LEFT_EDGE_X (s->w);
2007 if (s->row->mode_line_p)
2008 r.width = WINDOW_PIXEL_WIDTH (s->w) - WINDOW_RIGHT_DIVIDER_WIDTH (s->w);
2009 else
2010 r.width = WINDOW_PIXEL_WIDTH (s->w);
2012 /* Unless displaying a mode or menu bar line, which are always
2013 fully visible, clip to the visible part of the row. */
2014 if (s->w->pseudo_window_p)
2015 r.height = s->row->visible_height;
2016 else
2017 r.height = s->height;
2019 else
2021 /* This is a text line that may be partially visible. */
2022 r.x = window_box_left (s->w, s->area);
2023 r.width = window_box_width (s->w, s->area);
2024 r.height = s->row->visible_height;
2027 if (s->clip_head)
2028 if (r.x < s->clip_head->x)
2030 if (r.width >= s->clip_head->x - r.x)
2031 r.width -= s->clip_head->x - r.x;
2032 else
2033 r.width = 0;
2034 r.x = s->clip_head->x;
2036 if (s->clip_tail)
2037 if (r.x + r.width > s->clip_tail->x + s->clip_tail->background_width)
2039 if (s->clip_tail->x + s->clip_tail->background_width >= r.x)
2040 r.width = s->clip_tail->x + s->clip_tail->background_width - r.x;
2041 else
2042 r.width = 0;
2045 /* If S draws overlapping rows, it's sufficient to use the top and
2046 bottom of the window for clipping because this glyph string
2047 intentionally draws over other lines. */
2048 if (s->for_overlaps)
2050 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
2051 r.height = window_text_bottom_y (s->w) - r.y;
2053 /* Alas, the above simple strategy does not work for the
2054 environments with anti-aliased text: if the same text is
2055 drawn onto the same place multiple times, it gets thicker.
2056 If the overlap we are processing is for the erased cursor, we
2057 take the intersection with the rectangle of the cursor. */
2058 if (s->for_overlaps & OVERLAPS_ERASED_CURSOR)
2060 XRectangle rc, r_save = r;
2062 rc.x = WINDOW_TEXT_TO_FRAME_PIXEL_X (s->w, s->w->phys_cursor.x);
2063 rc.y = s->w->phys_cursor.y;
2064 rc.width = s->w->phys_cursor_width;
2065 rc.height = s->w->phys_cursor_height;
2067 x_intersect_rectangles (&r_save, &rc, &r);
2070 else
2072 /* Don't use S->y for clipping because it doesn't take partially
2073 visible lines into account. For example, it can be negative for
2074 partially visible lines at the top of a window. */
2075 if (!s->row->full_width_p
2076 && MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (s->w, s->row))
2077 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
2078 else
2079 r.y = max (0, s->row->y);
2082 r.y = WINDOW_TO_FRAME_PIXEL_Y (s->w, r.y);
2084 /* If drawing the cursor, don't let glyph draw outside its
2085 advertised boundaries. Cleartype does this under some circumstances. */
2086 if (s->hl == DRAW_CURSOR)
2088 struct glyph *glyph = s->first_glyph;
2089 int height, max_y;
2091 if (s->x > r.x)
2093 if (r.width >= s->x - r.x)
2094 r.width -= s->x - r.x;
2095 else /* R2L hscrolled row with cursor outside text area */
2096 r.width = 0;
2097 r.x = s->x;
2099 r.width = min (r.width, glyph->pixel_width);
2101 /* If r.y is below window bottom, ensure that we still see a cursor. */
2102 height = min (glyph->ascent + glyph->descent,
2103 min (FRAME_LINE_HEIGHT (s->f), s->row->visible_height));
2104 max_y = window_text_bottom_y (s->w) - height;
2105 max_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, max_y);
2106 if (s->ybase - glyph->ascent > max_y)
2108 r.y = max_y;
2109 r.height = height;
2111 else
2113 /* Don't draw cursor glyph taller than our actual glyph. */
2114 height = max (FRAME_LINE_HEIGHT (s->f), glyph->ascent + glyph->descent);
2115 if (height < r.height)
2117 max_y = r.y + r.height;
2118 r.y = min (max_y, max (r.y, s->ybase + glyph->descent - height));
2119 r.height = min (max_y - r.y, height);
2124 if (s->row->clip)
2126 XRectangle r_save = r;
2128 if (! x_intersect_rectangles (&r_save, s->row->clip, &r))
2129 r.width = 0;
2132 if ((s->for_overlaps & OVERLAPS_BOTH) == 0
2133 || ((s->for_overlaps & OVERLAPS_BOTH) == OVERLAPS_BOTH && n == 1))
2135 #ifdef CONVERT_FROM_XRECT
2136 CONVERT_FROM_XRECT (r, *rects);
2137 #else
2138 *rects = r;
2139 #endif
2140 return 1;
2142 else
2144 /* If we are processing overlapping and allowed to return
2145 multiple clipping rectangles, we exclude the row of the glyph
2146 string from the clipping rectangle. This is to avoid drawing
2147 the same text on the environment with anti-aliasing. */
2148 #ifdef CONVERT_FROM_XRECT
2149 XRectangle rs[2];
2150 #else
2151 XRectangle *rs = rects;
2152 #endif
2153 int i = 0, row_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, s->row->y);
2155 if (s->for_overlaps & OVERLAPS_PRED)
2157 rs[i] = r;
2158 if (r.y + r.height > row_y)
2160 if (r.y < row_y)
2161 rs[i].height = row_y - r.y;
2162 else
2163 rs[i].height = 0;
2165 i++;
2167 if (s->for_overlaps & OVERLAPS_SUCC)
2169 rs[i] = r;
2170 if (r.y < row_y + s->row->visible_height)
2172 if (r.y + r.height > row_y + s->row->visible_height)
2174 rs[i].y = row_y + s->row->visible_height;
2175 rs[i].height = r.y + r.height - rs[i].y;
2177 else
2178 rs[i].height = 0;
2180 i++;
2183 n = i;
2184 #ifdef CONVERT_FROM_XRECT
2185 for (i = 0; i < n; i++)
2186 CONVERT_FROM_XRECT (rs[i], rects[i]);
2187 #endif
2188 return n;
2192 /* EXPORT:
2193 Return in *NR the clipping rectangle for glyph string S. */
2195 void
2196 get_glyph_string_clip_rect (struct glyph_string *s, NativeRectangle *nr)
2198 get_glyph_string_clip_rects (s, nr, 1);
2202 /* EXPORT:
2203 Return the position and height of the phys cursor in window W.
2204 Set w->phys_cursor_width to width of phys cursor.
2207 void
2208 get_phys_cursor_geometry (struct window *w, struct glyph_row *row,
2209 struct glyph *glyph, int *xp, int *yp, int *heightp)
2211 struct frame *f = XFRAME (WINDOW_FRAME (w));
2212 int x, y, wd, h, h0, y0;
2214 /* Compute the width of the rectangle to draw. If on a stretch
2215 glyph, and `x-stretch-block-cursor' is nil, don't draw a
2216 rectangle as wide as the glyph, but use a canonical character
2217 width instead. */
2218 wd = glyph->pixel_width;
2220 x = w->phys_cursor.x;
2221 if (x < 0)
2223 wd += x;
2224 x = 0;
2227 if (glyph->type == STRETCH_GLYPH
2228 && !x_stretch_cursor_p)
2229 wd = min (FRAME_COLUMN_WIDTH (f), wd);
2230 w->phys_cursor_width = wd;
2232 y = w->phys_cursor.y + row->ascent - glyph->ascent;
2234 /* If y is below window bottom, ensure that we still see a cursor. */
2235 h0 = min (FRAME_LINE_HEIGHT (f), row->visible_height);
2237 h = max (h0, glyph->ascent + glyph->descent);
2238 h0 = min (h0, glyph->ascent + glyph->descent);
2240 y0 = WINDOW_HEADER_LINE_HEIGHT (w);
2241 if (y < y0)
2243 h = max (h - (y0 - y) + 1, h0);
2244 y = y0 - 1;
2246 else
2248 y0 = window_text_bottom_y (w) - h0;
2249 if (y > y0)
2251 h += y - y0;
2252 y = y0;
2256 *xp = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, x);
2257 *yp = WINDOW_TO_FRAME_PIXEL_Y (w, y);
2258 *heightp = h;
2262 * Remember which glyph the mouse is over.
2265 void
2266 remember_mouse_glyph (struct frame *f, int gx, int gy, NativeRectangle *rect)
2268 Lisp_Object window;
2269 struct window *w;
2270 struct glyph_row *r, *gr, *end_row;
2271 enum window_part part;
2272 enum glyph_row_area area;
2273 int x, y, width, height;
2275 /* Try to determine frame pixel position and size of the glyph under
2276 frame pixel coordinates X/Y on frame F. */
2278 if (window_resize_pixelwise)
2280 width = height = 1;
2281 goto virtual_glyph;
2283 else if (!f->glyphs_initialized_p
2284 || (window = window_from_coordinates (f, gx, gy, &part, 0),
2285 NILP (window)))
2287 width = FRAME_SMALLEST_CHAR_WIDTH (f);
2288 height = FRAME_SMALLEST_FONT_HEIGHT (f);
2289 goto virtual_glyph;
2292 w = XWINDOW (window);
2293 width = WINDOW_FRAME_COLUMN_WIDTH (w);
2294 height = WINDOW_FRAME_LINE_HEIGHT (w);
2296 x = window_relative_x_coord (w, part, gx);
2297 y = gy - WINDOW_TOP_EDGE_Y (w);
2299 r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
2300 end_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
2302 if (w->pseudo_window_p)
2304 area = TEXT_AREA;
2305 part = ON_MODE_LINE; /* Don't adjust margin. */
2306 goto text_glyph;
2309 switch (part)
2311 case ON_LEFT_MARGIN:
2312 area = LEFT_MARGIN_AREA;
2313 goto text_glyph;
2315 case ON_RIGHT_MARGIN:
2316 area = RIGHT_MARGIN_AREA;
2317 goto text_glyph;
2319 case ON_HEADER_LINE:
2320 case ON_MODE_LINE:
2321 gr = (part == ON_HEADER_LINE
2322 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
2323 : MATRIX_MODE_LINE_ROW (w->current_matrix));
2324 gy = gr->y;
2325 area = TEXT_AREA;
2326 goto text_glyph_row_found;
2328 case ON_TEXT:
2329 area = TEXT_AREA;
2331 text_glyph:
2332 gr = 0; gy = 0;
2333 for (; r <= end_row && r->enabled_p; ++r)
2334 if (r->y + r->height > y)
2336 gr = r; gy = r->y;
2337 break;
2340 text_glyph_row_found:
2341 if (gr && gy <= y)
2343 struct glyph *g = gr->glyphs[area];
2344 struct glyph *end = g + gr->used[area];
2346 height = gr->height;
2347 for (gx = gr->x; g < end; gx += g->pixel_width, ++g)
2348 if (gx + g->pixel_width > x)
2349 break;
2351 if (g < end)
2353 if (g->type == IMAGE_GLYPH)
2355 /* Don't remember when mouse is over image, as
2356 image may have hot-spots. */
2357 STORE_NATIVE_RECT (*rect, 0, 0, 0, 0);
2358 return;
2360 width = g->pixel_width;
2362 else
2364 /* Use nominal char spacing at end of line. */
2365 x -= gx;
2366 gx += (x / width) * width;
2369 if (part != ON_MODE_LINE && part != ON_HEADER_LINE)
2371 gx += window_box_left_offset (w, area);
2372 /* Don't expand over the modeline to make sure the vertical
2373 drag cursor is shown early enough. */
2374 height = min (height,
2375 max (0, WINDOW_BOX_HEIGHT_NO_MODE_LINE (w) - gy));
2378 else
2380 /* Use nominal line height at end of window. */
2381 gx = (x / width) * width;
2382 y -= gy;
2383 gy += (y / height) * height;
2384 if (part != ON_MODE_LINE && part != ON_HEADER_LINE)
2385 /* See comment above. */
2386 height = min (height,
2387 max (0, WINDOW_BOX_HEIGHT_NO_MODE_LINE (w) - gy));
2389 break;
2391 case ON_LEFT_FRINGE:
2392 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2393 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w)
2394 : window_box_right_offset (w, LEFT_MARGIN_AREA));
2395 width = WINDOW_LEFT_FRINGE_WIDTH (w);
2396 goto row_glyph;
2398 case ON_RIGHT_FRINGE:
2399 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2400 ? window_box_right_offset (w, RIGHT_MARGIN_AREA)
2401 : window_box_right_offset (w, TEXT_AREA));
2402 if (WINDOW_RIGHT_DIVIDER_WIDTH (w) == 0
2403 && !WINDOW_HAS_VERTICAL_SCROLL_BAR (w)
2404 && !WINDOW_RIGHTMOST_P (w))
2405 if (gx < WINDOW_PIXEL_WIDTH (w) - width)
2406 /* Make sure the vertical border can get her own glyph to the
2407 right of the one we build here. */
2408 width = WINDOW_RIGHT_FRINGE_WIDTH (w) - width;
2409 else
2410 width = WINDOW_PIXEL_WIDTH (w) - gx;
2411 else
2412 width = WINDOW_RIGHT_FRINGE_WIDTH (w);
2414 goto row_glyph;
2416 case ON_VERTICAL_BORDER:
2417 gx = WINDOW_PIXEL_WIDTH (w) - width;
2418 goto row_glyph;
2420 case ON_VERTICAL_SCROLL_BAR:
2421 gx = (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w)
2423 : (window_box_right_offset (w, RIGHT_MARGIN_AREA)
2424 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2425 ? WINDOW_RIGHT_FRINGE_WIDTH (w)
2426 : 0)));
2427 width = WINDOW_SCROLL_BAR_AREA_WIDTH (w);
2429 row_glyph:
2430 gr = 0, gy = 0;
2431 for (; r <= end_row && r->enabled_p; ++r)
2432 if (r->y + r->height > y)
2434 gr = r; gy = r->y;
2435 break;
2438 if (gr && gy <= y)
2439 height = gr->height;
2440 else
2442 /* Use nominal line height at end of window. */
2443 y -= gy;
2444 gy += (y / height) * height;
2446 break;
2448 case ON_RIGHT_DIVIDER:
2449 gx = WINDOW_PIXEL_WIDTH (w) - WINDOW_RIGHT_DIVIDER_WIDTH (w);
2450 width = WINDOW_RIGHT_DIVIDER_WIDTH (w);
2451 gy = 0;
2452 /* The bottom divider prevails. */
2453 height = WINDOW_PIXEL_HEIGHT (w) - WINDOW_BOTTOM_DIVIDER_WIDTH (w);
2454 goto add_edge;
2456 case ON_BOTTOM_DIVIDER:
2457 gx = 0;
2458 width = WINDOW_PIXEL_WIDTH (w);
2459 gy = WINDOW_PIXEL_HEIGHT (w) - WINDOW_BOTTOM_DIVIDER_WIDTH (w);
2460 height = WINDOW_BOTTOM_DIVIDER_WIDTH (w);
2461 goto add_edge;
2463 default:
2465 virtual_glyph:
2466 /* If there is no glyph under the mouse, then we divide the screen
2467 into a grid of the smallest glyph in the frame, and use that
2468 as our "glyph". */
2470 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to
2471 round down even for negative values. */
2472 if (gx < 0)
2473 gx -= width - 1;
2474 if (gy < 0)
2475 gy -= height - 1;
2477 gx = (gx / width) * width;
2478 gy = (gy / height) * height;
2480 goto store_rect;
2483 add_edge:
2484 gx += WINDOW_LEFT_EDGE_X (w);
2485 gy += WINDOW_TOP_EDGE_Y (w);
2487 store_rect:
2488 STORE_NATIVE_RECT (*rect, gx, gy, width, height);
2490 /* Visible feedback for debugging. */
2491 #if 0
2492 #if HAVE_X_WINDOWS
2493 XDrawRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
2494 f->output_data.x->normal_gc,
2495 gx, gy, width, height);
2496 #endif
2497 #endif
2501 #endif /* HAVE_WINDOW_SYSTEM */
2503 static void
2504 adjust_window_ends (struct window *w, struct glyph_row *row, bool current)
2506 eassert (w);
2507 w->window_end_pos = Z - MATRIX_ROW_END_CHARPOS (row);
2508 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
2509 w->window_end_vpos
2510 = MATRIX_ROW_VPOS (row, current ? w->current_matrix : w->desired_matrix);
2513 /***********************************************************************
2514 Lisp form evaluation
2515 ***********************************************************************/
2517 /* Error handler for safe_eval and safe_call. */
2519 static Lisp_Object
2520 safe_eval_handler (Lisp_Object arg, ptrdiff_t nargs, Lisp_Object *args)
2522 add_to_log ("Error during redisplay: %S signaled %S",
2523 Flist (nargs, args), arg);
2524 return Qnil;
2527 /* Call function FUNC with the rest of NARGS - 1 arguments
2528 following. Return the result, or nil if something went
2529 wrong. Prevent redisplay during the evaluation. */
2531 static Lisp_Object
2532 safe__call (bool inhibit_quit, ptrdiff_t nargs, Lisp_Object func, va_list ap)
2534 Lisp_Object val;
2536 if (inhibit_eval_during_redisplay)
2537 val = Qnil;
2538 else
2540 ptrdiff_t i;
2541 ptrdiff_t count = SPECPDL_INDEX ();
2542 Lisp_Object *args;
2543 USE_SAFE_ALLOCA;
2544 SAFE_ALLOCA_LISP (args, nargs);
2546 args[0] = func;
2547 for (i = 1; i < nargs; i++)
2548 args[i] = va_arg (ap, Lisp_Object);
2550 specbind (Qinhibit_redisplay, Qt);
2551 if (inhibit_quit)
2552 specbind (Qinhibit_quit, Qt);
2553 /* Use Qt to ensure debugger does not run,
2554 so there is no possibility of wanting to redisplay. */
2555 val = internal_condition_case_n (Ffuncall, nargs, args, Qt,
2556 safe_eval_handler);
2557 SAFE_FREE ();
2558 val = unbind_to (count, val);
2561 return val;
2564 Lisp_Object
2565 safe_call (ptrdiff_t nargs, Lisp_Object func, ...)
2567 Lisp_Object retval;
2568 va_list ap;
2570 va_start (ap, func);
2571 retval = safe__call (false, nargs, func, ap);
2572 va_end (ap);
2573 return retval;
2576 /* Call function FN with one argument ARG.
2577 Return the result, or nil if something went wrong. */
2579 Lisp_Object
2580 safe_call1 (Lisp_Object fn, Lisp_Object arg)
2582 return safe_call (2, fn, arg);
2585 static Lisp_Object
2586 safe__call1 (bool inhibit_quit, Lisp_Object fn, ...)
2588 Lisp_Object retval;
2589 va_list ap;
2591 va_start (ap, fn);
2592 retval = safe__call (inhibit_quit, 2, fn, ap);
2593 va_end (ap);
2594 return retval;
2597 Lisp_Object
2598 safe_eval (Lisp_Object sexpr)
2600 return safe__call1 (false, Qeval, sexpr);
2603 static Lisp_Object
2604 safe__eval (bool inhibit_quit, Lisp_Object sexpr)
2606 return safe__call1 (inhibit_quit, Qeval, sexpr);
2609 /* Call function FN with two arguments ARG1 and ARG2.
2610 Return the result, or nil if something went wrong. */
2612 Lisp_Object
2613 safe_call2 (Lisp_Object fn, Lisp_Object arg1, Lisp_Object arg2)
2615 return safe_call (3, fn, arg1, arg2);
2620 /***********************************************************************
2621 Debugging
2622 ***********************************************************************/
2624 #if 0
2626 /* Define CHECK_IT to perform sanity checks on iterators.
2627 This is for debugging. It is too slow to do unconditionally. */
2629 static void
2630 check_it (struct it *it)
2632 if (it->method == GET_FROM_STRING)
2634 eassert (STRINGP (it->string));
2635 eassert (IT_STRING_CHARPOS (*it) >= 0);
2637 else
2639 eassert (IT_STRING_CHARPOS (*it) < 0);
2640 if (it->method == GET_FROM_BUFFER)
2642 /* Check that character and byte positions agree. */
2643 eassert (IT_CHARPOS (*it) == BYTE_TO_CHAR (IT_BYTEPOS (*it)));
2647 if (it->dpvec)
2648 eassert (it->current.dpvec_index >= 0);
2649 else
2650 eassert (it->current.dpvec_index < 0);
2653 #define CHECK_IT(IT) check_it ((IT))
2655 #else /* not 0 */
2657 #define CHECK_IT(IT) (void) 0
2659 #endif /* not 0 */
2662 #if defined GLYPH_DEBUG && defined ENABLE_CHECKING
2664 /* Check that the window end of window W is what we expect it
2665 to be---the last row in the current matrix displaying text. */
2667 static void
2668 check_window_end (struct window *w)
2670 if (!MINI_WINDOW_P (w) && w->window_end_valid)
2672 struct glyph_row *row;
2673 eassert ((row = MATRIX_ROW (w->current_matrix, w->window_end_vpos),
2674 !row->enabled_p
2675 || MATRIX_ROW_DISPLAYS_TEXT_P (row)
2676 || MATRIX_ROW_VPOS (row, w->current_matrix) == 0));
2680 #define CHECK_WINDOW_END(W) check_window_end ((W))
2682 #else
2684 #define CHECK_WINDOW_END(W) (void) 0
2686 #endif /* GLYPH_DEBUG and ENABLE_CHECKING */
2688 /***********************************************************************
2689 Iterator initialization
2690 ***********************************************************************/
2692 /* Initialize IT for displaying current_buffer in window W, starting
2693 at character position CHARPOS. CHARPOS < 0 means that no buffer
2694 position is specified which is useful when the iterator is assigned
2695 a position later. BYTEPOS is the byte position corresponding to
2696 CHARPOS.
2698 If ROW is not null, calls to produce_glyphs with IT as parameter
2699 will produce glyphs in that row.
2701 BASE_FACE_ID is the id of a base face to use. It must be one of
2702 DEFAULT_FACE_ID for normal text, MODE_LINE_FACE_ID,
2703 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID for displaying
2704 mode lines, or TOOL_BAR_FACE_ID for displaying the tool-bar.
2706 If ROW is null and BASE_FACE_ID is equal to MODE_LINE_FACE_ID,
2707 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID, the iterator
2708 will be initialized to use the corresponding mode line glyph row of
2709 the desired matrix of W. */
2711 void
2712 init_iterator (struct it *it, struct window *w,
2713 ptrdiff_t charpos, ptrdiff_t bytepos,
2714 struct glyph_row *row, enum face_id base_face_id)
2716 enum face_id remapped_base_face_id = base_face_id;
2718 /* Some precondition checks. */
2719 eassert (w != NULL && it != NULL);
2720 eassert (charpos < 0 || (charpos >= BUF_BEG (current_buffer)
2721 && charpos <= ZV));
2723 /* If face attributes have been changed since the last redisplay,
2724 free realized faces now because they depend on face definitions
2725 that might have changed. Don't free faces while there might be
2726 desired matrices pending which reference these faces. */
2727 if (face_change && !inhibit_free_realized_faces)
2729 face_change = false;
2730 free_all_realized_faces (Qnil);
2733 /* Perhaps remap BASE_FACE_ID to a user-specified alternative. */
2734 if (! NILP (Vface_remapping_alist))
2735 remapped_base_face_id
2736 = lookup_basic_face (XFRAME (w->frame), base_face_id);
2738 /* Use one of the mode line rows of W's desired matrix if
2739 appropriate. */
2740 if (row == NULL)
2742 if (base_face_id == MODE_LINE_FACE_ID
2743 || base_face_id == MODE_LINE_INACTIVE_FACE_ID)
2744 row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
2745 else if (base_face_id == HEADER_LINE_FACE_ID)
2746 row = MATRIX_HEADER_LINE_ROW (w->desired_matrix);
2749 /* Clear IT, and set it->object and other IT's Lisp objects to Qnil.
2750 Other parts of redisplay rely on that. */
2751 memclear (it, sizeof *it);
2752 it->current.overlay_string_index = -1;
2753 it->current.dpvec_index = -1;
2754 it->base_face_id = remapped_base_face_id;
2755 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
2756 it->paragraph_embedding = L2R;
2757 it->bidi_it.w = w;
2759 /* The window in which we iterate over current_buffer: */
2760 XSETWINDOW (it->window, w);
2761 it->w = w;
2762 it->f = XFRAME (w->frame);
2764 it->cmp_it.id = -1;
2766 /* Extra space between lines (on window systems only). */
2767 if (base_face_id == DEFAULT_FACE_ID
2768 && FRAME_WINDOW_P (it->f))
2770 if (NATNUMP (BVAR (current_buffer, extra_line_spacing)))
2771 it->extra_line_spacing = XFASTINT (BVAR (current_buffer, extra_line_spacing));
2772 else if (FLOATP (BVAR (current_buffer, extra_line_spacing)))
2773 it->extra_line_spacing = (XFLOAT_DATA (BVAR (current_buffer, extra_line_spacing))
2774 * FRAME_LINE_HEIGHT (it->f));
2775 else if (it->f->extra_line_spacing > 0)
2776 it->extra_line_spacing = it->f->extra_line_spacing;
2779 /* If realized faces have been removed, e.g. because of face
2780 attribute changes of named faces, recompute them. When running
2781 in batch mode, the face cache of the initial frame is null. If
2782 we happen to get called, make a dummy face cache. */
2783 if (FRAME_FACE_CACHE (it->f) == NULL)
2784 init_frame_faces (it->f);
2785 if (FRAME_FACE_CACHE (it->f)->used == 0)
2786 recompute_basic_faces (it->f);
2788 it->override_ascent = -1;
2790 /* Are control characters displayed as `^C'? */
2791 it->ctl_arrow_p = !NILP (BVAR (current_buffer, ctl_arrow));
2793 /* -1 means everything between a CR and the following line end
2794 is invisible. >0 means lines indented more than this value are
2795 invisible. */
2796 it->selective = (INTEGERP (BVAR (current_buffer, selective_display))
2797 ? (clip_to_bounds
2798 (-1, XINT (BVAR (current_buffer, selective_display)),
2799 PTRDIFF_MAX))
2800 : (!NILP (BVAR (current_buffer, selective_display))
2801 ? -1 : 0));
2802 it->selective_display_ellipsis_p
2803 = !NILP (BVAR (current_buffer, selective_display_ellipses));
2805 /* Display table to use. */
2806 it->dp = window_display_table (w);
2808 /* Are multibyte characters enabled in current_buffer? */
2809 it->multibyte_p = !NILP (BVAR (current_buffer, enable_multibyte_characters));
2811 /* Get the position at which the redisplay_end_trigger hook should
2812 be run, if it is to be run at all. */
2813 if (MARKERP (w->redisplay_end_trigger)
2814 && XMARKER (w->redisplay_end_trigger)->buffer != 0)
2815 it->redisplay_end_trigger_charpos
2816 = marker_position (w->redisplay_end_trigger);
2817 else if (INTEGERP (w->redisplay_end_trigger))
2818 it->redisplay_end_trigger_charpos
2819 = clip_to_bounds (PTRDIFF_MIN, XINT (w->redisplay_end_trigger),
2820 PTRDIFF_MAX);
2822 it->tab_width = SANE_TAB_WIDTH (current_buffer);
2824 /* Are lines in the display truncated? */
2825 if (TRUNCATE != 0)
2826 it->line_wrap = TRUNCATE;
2827 if (base_face_id == DEFAULT_FACE_ID
2828 && !it->w->hscroll
2829 && (WINDOW_FULL_WIDTH_P (it->w)
2830 || NILP (Vtruncate_partial_width_windows)
2831 || (INTEGERP (Vtruncate_partial_width_windows)
2832 /* PXW: Shall we do something about this? */
2833 && (XINT (Vtruncate_partial_width_windows)
2834 <= WINDOW_TOTAL_COLS (it->w))))
2835 && NILP (BVAR (current_buffer, truncate_lines)))
2836 it->line_wrap = NILP (BVAR (current_buffer, word_wrap))
2837 ? WINDOW_WRAP : WORD_WRAP;
2839 /* Get dimensions of truncation and continuation glyphs. These are
2840 displayed as fringe bitmaps under X, but we need them for such
2841 frames when the fringes are turned off. But leave the dimensions
2842 zero for tooltip frames, as these glyphs look ugly there and also
2843 sabotage calculations of tooltip dimensions in x-show-tip. */
2844 #ifdef HAVE_WINDOW_SYSTEM
2845 if (!(FRAME_WINDOW_P (it->f)
2846 && FRAMEP (tip_frame)
2847 && it->f == XFRAME (tip_frame)))
2848 #endif
2850 if (it->line_wrap == TRUNCATE)
2852 /* We will need the truncation glyph. */
2853 eassert (it->glyph_row == NULL);
2854 produce_special_glyphs (it, IT_TRUNCATION);
2855 it->truncation_pixel_width = it->pixel_width;
2857 else
2859 /* We will need the continuation glyph. */
2860 eassert (it->glyph_row == NULL);
2861 produce_special_glyphs (it, IT_CONTINUATION);
2862 it->continuation_pixel_width = it->pixel_width;
2866 /* Reset these values to zero because the produce_special_glyphs
2867 above has changed them. */
2868 it->pixel_width = it->ascent = it->descent = 0;
2869 it->phys_ascent = it->phys_descent = 0;
2871 /* Set this after getting the dimensions of truncation and
2872 continuation glyphs, so that we don't produce glyphs when calling
2873 produce_special_glyphs, above. */
2874 it->glyph_row = row;
2875 it->area = TEXT_AREA;
2877 /* Get the dimensions of the display area. The display area
2878 consists of the visible window area plus a horizontally scrolled
2879 part to the left of the window. All x-values are relative to the
2880 start of this total display area. */
2881 if (base_face_id != DEFAULT_FACE_ID)
2883 /* Mode lines, menu bar in terminal frames. */
2884 it->first_visible_x = 0;
2885 it->last_visible_x = WINDOW_PIXEL_WIDTH (w);
2887 else
2889 it->first_visible_x
2890 = window_hscroll_limited (it->w, it->f) * FRAME_COLUMN_WIDTH (it->f);
2891 it->last_visible_x = (it->first_visible_x
2892 + window_box_width (w, TEXT_AREA));
2894 /* If we truncate lines, leave room for the truncation glyph(s) at
2895 the right margin. Otherwise, leave room for the continuation
2896 glyph(s). Done only if the window has no right fringe. */
2897 if (WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0)
2899 if (it->line_wrap == TRUNCATE)
2900 it->last_visible_x -= it->truncation_pixel_width;
2901 else
2902 it->last_visible_x -= it->continuation_pixel_width;
2905 it->header_line_p = WINDOW_WANTS_HEADER_LINE_P (w);
2906 it->current_y = WINDOW_HEADER_LINE_HEIGHT (w) + w->vscroll;
2909 /* Leave room for a border glyph. */
2910 if (!FRAME_WINDOW_P (it->f)
2911 && !WINDOW_RIGHTMOST_P (it->w))
2912 it->last_visible_x -= 1;
2914 it->last_visible_y = window_text_bottom_y (w);
2916 /* For mode lines and alike, arrange for the first glyph having a
2917 left box line if the face specifies a box. */
2918 if (base_face_id != DEFAULT_FACE_ID)
2920 struct face *face;
2922 it->face_id = remapped_base_face_id;
2924 /* If we have a boxed mode line, make the first character appear
2925 with a left box line. */
2926 face = FACE_FROM_ID (it->f, remapped_base_face_id);
2927 if (face && face->box != FACE_NO_BOX)
2928 it->start_of_box_run_p = true;
2931 /* If a buffer position was specified, set the iterator there,
2932 getting overlays and face properties from that position. */
2933 if (charpos >= BUF_BEG (current_buffer))
2935 it->stop_charpos = charpos;
2936 it->end_charpos = ZV;
2937 eassert (charpos == BYTE_TO_CHAR (bytepos));
2938 IT_CHARPOS (*it) = charpos;
2939 IT_BYTEPOS (*it) = bytepos;
2941 /* We will rely on `reseat' to set this up properly, via
2942 handle_face_prop. */
2943 it->face_id = it->base_face_id;
2945 it->start = it->current;
2946 /* Do we need to reorder bidirectional text? Not if this is a
2947 unibyte buffer: by definition, none of the single-byte
2948 characters are strong R2L, so no reordering is needed. And
2949 bidi.c doesn't support unibyte buffers anyway. Also, don't
2950 reorder while we are loading loadup.el, since the tables of
2951 character properties needed for reordering are not yet
2952 available. */
2953 it->bidi_p =
2954 NILP (Vpurify_flag)
2955 && !NILP (BVAR (current_buffer, bidi_display_reordering))
2956 && it->multibyte_p;
2958 /* If we are to reorder bidirectional text, init the bidi
2959 iterator. */
2960 if (it->bidi_p)
2962 /* Since we don't know at this point whether there will be
2963 any R2L lines in the window, we reserve space for
2964 truncation/continuation glyphs even if only the left
2965 fringe is absent. */
2966 if (base_face_id == DEFAULT_FACE_ID
2967 && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0
2968 && WINDOW_RIGHT_FRINGE_WIDTH (it->w) != 0)
2970 if (it->line_wrap == TRUNCATE)
2971 it->last_visible_x -= it->truncation_pixel_width;
2972 else
2973 it->last_visible_x -= it->continuation_pixel_width;
2975 /* Note the paragraph direction that this buffer wants to
2976 use. */
2977 if (EQ (BVAR (current_buffer, bidi_paragraph_direction),
2978 Qleft_to_right))
2979 it->paragraph_embedding = L2R;
2980 else if (EQ (BVAR (current_buffer, bidi_paragraph_direction),
2981 Qright_to_left))
2982 it->paragraph_embedding = R2L;
2983 else
2984 it->paragraph_embedding = NEUTRAL_DIR;
2985 bidi_unshelve_cache (NULL, 0);
2986 bidi_init_it (charpos, IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
2987 &it->bidi_it);
2990 /* Compute faces etc. */
2991 reseat (it, it->current.pos, 1);
2994 CHECK_IT (it);
2998 /* Initialize IT for the display of window W with window start POS. */
3000 void
3001 start_display (struct it *it, struct window *w, struct text_pos pos)
3003 struct glyph_row *row;
3004 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
3006 row = w->desired_matrix->rows + first_vpos;
3007 init_iterator (it, w, CHARPOS (pos), BYTEPOS (pos), row, DEFAULT_FACE_ID);
3008 it->first_vpos = first_vpos;
3010 /* Don't reseat to previous visible line start if current start
3011 position is in a string or image. */
3012 if (it->method == GET_FROM_BUFFER && it->line_wrap != TRUNCATE)
3014 int start_at_line_beg_p;
3015 int first_y = it->current_y;
3017 /* If window start is not at a line start, skip forward to POS to
3018 get the correct continuation lines width. */
3019 start_at_line_beg_p = (CHARPOS (pos) == BEGV
3020 || FETCH_BYTE (BYTEPOS (pos) - 1) == '\n');
3021 if (!start_at_line_beg_p)
3023 int new_x;
3025 reseat_at_previous_visible_line_start (it);
3026 move_it_to (it, CHARPOS (pos), -1, -1, -1, MOVE_TO_POS);
3028 new_x = it->current_x + it->pixel_width;
3030 /* If lines are continued, this line may end in the middle
3031 of a multi-glyph character (e.g. a control character
3032 displayed as \003, or in the middle of an overlay
3033 string). In this case move_it_to above will not have
3034 taken us to the start of the continuation line but to the
3035 end of the continued line. */
3036 if (it->current_x > 0
3037 && it->line_wrap != TRUNCATE /* Lines are continued. */
3038 && (/* And glyph doesn't fit on the line. */
3039 new_x > it->last_visible_x
3040 /* Or it fits exactly and we're on a window
3041 system frame. */
3042 || (new_x == it->last_visible_x
3043 && FRAME_WINDOW_P (it->f)
3044 && ((it->bidi_p && it->bidi_it.paragraph_dir == R2L)
3045 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
3046 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)))))
3048 if ((it->current.dpvec_index >= 0
3049 || it->current.overlay_string_index >= 0)
3050 /* If we are on a newline from a display vector or
3051 overlay string, then we are already at the end of
3052 a screen line; no need to go to the next line in
3053 that case, as this line is not really continued.
3054 (If we do go to the next line, C-e will not DTRT.) */
3055 && it->c != '\n')
3057 set_iterator_to_next (it, 1);
3058 move_it_in_display_line_to (it, -1, -1, 0);
3061 it->continuation_lines_width += it->current_x;
3063 /* If the character at POS is displayed via a display
3064 vector, move_it_to above stops at the final glyph of
3065 IT->dpvec. To make the caller redisplay that character
3066 again (a.k.a. start at POS), we need to reset the
3067 dpvec_index to the beginning of IT->dpvec. */
3068 else if (it->current.dpvec_index >= 0)
3069 it->current.dpvec_index = 0;
3071 /* We're starting a new display line, not affected by the
3072 height of the continued line, so clear the appropriate
3073 fields in the iterator structure. */
3074 it->max_ascent = it->max_descent = 0;
3075 it->max_phys_ascent = it->max_phys_descent = 0;
3077 it->current_y = first_y;
3078 it->vpos = 0;
3079 it->current_x = it->hpos = 0;
3085 /* Return 1 if POS is a position in ellipses displayed for invisible
3086 text. W is the window we display, for text property lookup. */
3088 static int
3089 in_ellipses_for_invisible_text_p (struct display_pos *pos, struct window *w)
3091 Lisp_Object prop, window;
3092 int ellipses_p = 0;
3093 ptrdiff_t charpos = CHARPOS (pos->pos);
3095 /* If POS specifies a position in a display vector, this might
3096 be for an ellipsis displayed for invisible text. We won't
3097 get the iterator set up for delivering that ellipsis unless
3098 we make sure that it gets aware of the invisible text. */
3099 if (pos->dpvec_index >= 0
3100 && pos->overlay_string_index < 0
3101 && CHARPOS (pos->string_pos) < 0
3102 && charpos > BEGV
3103 && (XSETWINDOW (window, w),
3104 prop = Fget_char_property (make_number (charpos),
3105 Qinvisible, window),
3106 !TEXT_PROP_MEANS_INVISIBLE (prop)))
3108 prop = Fget_char_property (make_number (charpos - 1), Qinvisible,
3109 window);
3110 ellipses_p = 2 == TEXT_PROP_MEANS_INVISIBLE (prop);
3113 return ellipses_p;
3117 /* Initialize IT for stepping through current_buffer in window W,
3118 starting at position POS that includes overlay string and display
3119 vector/ control character translation position information. Value
3120 is zero if there are overlay strings with newlines at POS. */
3122 static int
3123 init_from_display_pos (struct it *it, struct window *w, struct display_pos *pos)
3125 ptrdiff_t charpos = CHARPOS (pos->pos), bytepos = BYTEPOS (pos->pos);
3126 int i, overlay_strings_with_newlines = 0;
3128 /* If POS specifies a position in a display vector, this might
3129 be for an ellipsis displayed for invisible text. We won't
3130 get the iterator set up for delivering that ellipsis unless
3131 we make sure that it gets aware of the invisible text. */
3132 if (in_ellipses_for_invisible_text_p (pos, w))
3134 --charpos;
3135 bytepos = 0;
3138 /* Keep in mind: the call to reseat in init_iterator skips invisible
3139 text, so we might end up at a position different from POS. This
3140 is only a problem when POS is a row start after a newline and an
3141 overlay starts there with an after-string, and the overlay has an
3142 invisible property. Since we don't skip invisible text in
3143 display_line and elsewhere immediately after consuming the
3144 newline before the row start, such a POS will not be in a string,
3145 but the call to init_iterator below will move us to the
3146 after-string. */
3147 init_iterator (it, w, charpos, bytepos, NULL, DEFAULT_FACE_ID);
3149 /* This only scans the current chunk -- it should scan all chunks.
3150 However, OVERLAY_STRING_CHUNK_SIZE has been increased from 3 in 21.1
3151 to 16 in 22.1 to make this a lesser problem. */
3152 for (i = 0; i < it->n_overlay_strings && i < OVERLAY_STRING_CHUNK_SIZE; ++i)
3154 const char *s = SSDATA (it->overlay_strings[i]);
3155 const char *e = s + SBYTES (it->overlay_strings[i]);
3157 while (s < e && *s != '\n')
3158 ++s;
3160 if (s < e)
3162 overlay_strings_with_newlines = 1;
3163 break;
3167 /* If position is within an overlay string, set up IT to the right
3168 overlay string. */
3169 if (pos->overlay_string_index >= 0)
3171 int relative_index;
3173 /* If the first overlay string happens to have a `display'
3174 property for an image, the iterator will be set up for that
3175 image, and we have to undo that setup first before we can
3176 correct the overlay string index. */
3177 if (it->method == GET_FROM_IMAGE)
3178 pop_it (it);
3180 /* We already have the first chunk of overlay strings in
3181 IT->overlay_strings. Load more until the one for
3182 pos->overlay_string_index is in IT->overlay_strings. */
3183 if (pos->overlay_string_index >= OVERLAY_STRING_CHUNK_SIZE)
3185 ptrdiff_t n = pos->overlay_string_index / OVERLAY_STRING_CHUNK_SIZE;
3186 it->current.overlay_string_index = 0;
3187 while (n--)
3189 load_overlay_strings (it, 0);
3190 it->current.overlay_string_index += OVERLAY_STRING_CHUNK_SIZE;
3194 it->current.overlay_string_index = pos->overlay_string_index;
3195 relative_index = (it->current.overlay_string_index
3196 % OVERLAY_STRING_CHUNK_SIZE);
3197 it->string = it->overlay_strings[relative_index];
3198 eassert (STRINGP (it->string));
3199 it->current.string_pos = pos->string_pos;
3200 it->method = GET_FROM_STRING;
3201 it->end_charpos = SCHARS (it->string);
3202 /* Set up the bidi iterator for this overlay string. */
3203 if (it->bidi_p)
3205 it->bidi_it.string.lstring = it->string;
3206 it->bidi_it.string.s = NULL;
3207 it->bidi_it.string.schars = SCHARS (it->string);
3208 it->bidi_it.string.bufpos = it->overlay_strings_charpos;
3209 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
3210 it->bidi_it.string.unibyte = !it->multibyte_p;
3211 it->bidi_it.w = it->w;
3212 bidi_init_it (IT_STRING_CHARPOS (*it), IT_STRING_BYTEPOS (*it),
3213 FRAME_WINDOW_P (it->f), &it->bidi_it);
3215 /* Synchronize the state of the bidi iterator with
3216 pos->string_pos. For any string position other than
3217 zero, this will be done automagically when we resume
3218 iteration over the string and get_visually_first_element
3219 is called. But if string_pos is zero, and the string is
3220 to be reordered for display, we need to resync manually,
3221 since it could be that the iteration state recorded in
3222 pos ended at string_pos of 0 moving backwards in string. */
3223 if (CHARPOS (pos->string_pos) == 0)
3225 get_visually_first_element (it);
3226 if (IT_STRING_CHARPOS (*it) != 0)
3227 do {
3228 /* Paranoia. */
3229 eassert (it->bidi_it.charpos < it->bidi_it.string.schars);
3230 bidi_move_to_visually_next (&it->bidi_it);
3231 } while (it->bidi_it.charpos != 0);
3233 eassert (IT_STRING_CHARPOS (*it) == it->bidi_it.charpos
3234 && IT_STRING_BYTEPOS (*it) == it->bidi_it.bytepos);
3238 if (CHARPOS (pos->string_pos) >= 0)
3240 /* Recorded position is not in an overlay string, but in another
3241 string. This can only be a string from a `display' property.
3242 IT should already be filled with that string. */
3243 it->current.string_pos = pos->string_pos;
3244 eassert (STRINGP (it->string));
3245 if (it->bidi_p)
3246 bidi_init_it (IT_STRING_CHARPOS (*it), IT_STRING_BYTEPOS (*it),
3247 FRAME_WINDOW_P (it->f), &it->bidi_it);
3250 /* Restore position in display vector translations, control
3251 character translations or ellipses. */
3252 if (pos->dpvec_index >= 0)
3254 if (it->dpvec == NULL)
3255 get_next_display_element (it);
3256 eassert (it->dpvec && it->current.dpvec_index == 0);
3257 it->current.dpvec_index = pos->dpvec_index;
3260 CHECK_IT (it);
3261 return !overlay_strings_with_newlines;
3265 /* Initialize IT for stepping through current_buffer in window W
3266 starting at ROW->start. */
3268 static void
3269 init_to_row_start (struct it *it, struct window *w, struct glyph_row *row)
3271 init_from_display_pos (it, w, &row->start);
3272 it->start = row->start;
3273 it->continuation_lines_width = row->continuation_lines_width;
3274 CHECK_IT (it);
3278 /* Initialize IT for stepping through current_buffer in window W
3279 starting in the line following ROW, i.e. starting at ROW->end.
3280 Value is zero if there are overlay strings with newlines at ROW's
3281 end position. */
3283 static int
3284 init_to_row_end (struct it *it, struct window *w, struct glyph_row *row)
3286 int success = 0;
3288 if (init_from_display_pos (it, w, &row->end))
3290 if (row->continued_p)
3291 it->continuation_lines_width
3292 = row->continuation_lines_width + row->pixel_width;
3293 CHECK_IT (it);
3294 success = 1;
3297 return success;
3303 /***********************************************************************
3304 Text properties
3305 ***********************************************************************/
3307 /* Called when IT reaches IT->stop_charpos. Handle text property and
3308 overlay changes. Set IT->stop_charpos to the next position where
3309 to stop. */
3311 static void
3312 handle_stop (struct it *it)
3314 enum prop_handled handled;
3315 int handle_overlay_change_p;
3316 struct props *p;
3318 it->dpvec = NULL;
3319 it->current.dpvec_index = -1;
3320 handle_overlay_change_p = !it->ignore_overlay_strings_at_pos_p;
3321 it->ignore_overlay_strings_at_pos_p = 0;
3322 it->ellipsis_p = 0;
3324 /* Use face of preceding text for ellipsis (if invisible) */
3325 if (it->selective_display_ellipsis_p)
3326 it->saved_face_id = it->face_id;
3328 /* Here's the description of the semantics of, and the logic behind,
3329 the various HANDLED_* statuses:
3331 HANDLED_NORMALLY means the handler did its job, and the loop
3332 should proceed to calling the next handler in order.
3334 HANDLED_RECOMPUTE_PROPS means the handler caused a significant
3335 change in the properties and overlays at current position, so the
3336 loop should be restarted, to re-invoke the handlers that were
3337 already called. This happens when fontification-functions were
3338 called by handle_fontified_prop, and actually fontified
3339 something. Another case where HANDLED_RECOMPUTE_PROPS is
3340 returned is when we discover overlay strings that need to be
3341 displayed right away. The loop below will continue for as long
3342 as the status is HANDLED_RECOMPUTE_PROPS.
3344 HANDLED_RETURN means return immediately to the caller, to
3345 continue iteration without calling any further handlers. This is
3346 used when we need to act on some property right away, for example
3347 when we need to display the ellipsis or a replacing display
3348 property, such as display string or image.
3350 HANDLED_OVERLAY_STRING_CONSUMED means an overlay string was just
3351 consumed, and the handler switched to the next overlay string.
3352 This signals the loop below to refrain from looking for more
3353 overlays before all the overlay strings of the current overlay
3354 are processed.
3356 Some of the handlers called by the loop push the iterator state
3357 onto the stack (see 'push_it'), and arrange for the iteration to
3358 continue with another object, such as an image, a display string,
3359 or an overlay string. In most such cases, it->stop_charpos is
3360 set to the first character of the string, so that when the
3361 iteration resumes, this function will immediately be called
3362 again, to examine the properties at the beginning of the string.
3364 When a display or overlay string is exhausted, the iterator state
3365 is popped (see 'pop_it'), and iteration continues with the
3366 previous object. Again, in many such cases this function is
3367 called again to find the next position where properties might
3368 change. */
3372 handled = HANDLED_NORMALLY;
3374 /* Call text property handlers. */
3375 for (p = it_props; p->handler; ++p)
3377 handled = p->handler (it);
3379 if (handled == HANDLED_RECOMPUTE_PROPS)
3380 break;
3381 else if (handled == HANDLED_RETURN)
3383 /* We still want to show before and after strings from
3384 overlays even if the actual buffer text is replaced. */
3385 if (!handle_overlay_change_p
3386 || it->sp > 1
3387 /* Don't call get_overlay_strings_1 if we already
3388 have overlay strings loaded, because doing so
3389 will load them again and push the iterator state
3390 onto the stack one more time, which is not
3391 expected by the rest of the code that processes
3392 overlay strings. */
3393 || (it->current.overlay_string_index < 0
3394 ? !get_overlay_strings_1 (it, 0, 0)
3395 : 0))
3397 if (it->ellipsis_p)
3398 setup_for_ellipsis (it, 0);
3399 /* When handling a display spec, we might load an
3400 empty string. In that case, discard it here. We
3401 used to discard it in handle_single_display_spec,
3402 but that causes get_overlay_strings_1, above, to
3403 ignore overlay strings that we must check. */
3404 if (STRINGP (it->string) && !SCHARS (it->string))
3405 pop_it (it);
3406 return;
3408 else if (STRINGP (it->string) && !SCHARS (it->string))
3409 pop_it (it);
3410 else
3412 it->ignore_overlay_strings_at_pos_p = true;
3413 it->string_from_display_prop_p = 0;
3414 it->from_disp_prop_p = 0;
3415 handle_overlay_change_p = 0;
3417 handled = HANDLED_RECOMPUTE_PROPS;
3418 break;
3420 else if (handled == HANDLED_OVERLAY_STRING_CONSUMED)
3421 handle_overlay_change_p = 0;
3424 if (handled != HANDLED_RECOMPUTE_PROPS)
3426 /* Don't check for overlay strings below when set to deliver
3427 characters from a display vector. */
3428 if (it->method == GET_FROM_DISPLAY_VECTOR)
3429 handle_overlay_change_p = 0;
3431 /* Handle overlay changes.
3432 This sets HANDLED to HANDLED_RECOMPUTE_PROPS
3433 if it finds overlays. */
3434 if (handle_overlay_change_p)
3435 handled = handle_overlay_change (it);
3438 if (it->ellipsis_p)
3440 setup_for_ellipsis (it, 0);
3441 break;
3444 while (handled == HANDLED_RECOMPUTE_PROPS);
3446 /* Determine where to stop next. */
3447 if (handled == HANDLED_NORMALLY)
3448 compute_stop_pos (it);
3452 /* Compute IT->stop_charpos from text property and overlay change
3453 information for IT's current position. */
3455 static void
3456 compute_stop_pos (struct it *it)
3458 register INTERVAL iv, next_iv;
3459 Lisp_Object object, limit, position;
3460 ptrdiff_t charpos, bytepos;
3462 if (STRINGP (it->string))
3464 /* Strings are usually short, so don't limit the search for
3465 properties. */
3466 it->stop_charpos = it->end_charpos;
3467 object = it->string;
3468 limit = Qnil;
3469 charpos = IT_STRING_CHARPOS (*it);
3470 bytepos = IT_STRING_BYTEPOS (*it);
3472 else
3474 ptrdiff_t pos;
3476 /* If end_charpos is out of range for some reason, such as a
3477 misbehaving display function, rationalize it (Bug#5984). */
3478 if (it->end_charpos > ZV)
3479 it->end_charpos = ZV;
3480 it->stop_charpos = it->end_charpos;
3482 /* If next overlay change is in front of the current stop pos
3483 (which is IT->end_charpos), stop there. Note: value of
3484 next_overlay_change is point-max if no overlay change
3485 follows. */
3486 charpos = IT_CHARPOS (*it);
3487 bytepos = IT_BYTEPOS (*it);
3488 pos = next_overlay_change (charpos);
3489 if (pos < it->stop_charpos)
3490 it->stop_charpos = pos;
3492 /* Set up variables for computing the stop position from text
3493 property changes. */
3494 XSETBUFFER (object, current_buffer);
3495 limit = make_number (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT);
3498 /* Get the interval containing IT's position. Value is a null
3499 interval if there isn't such an interval. */
3500 position = make_number (charpos);
3501 iv = validate_interval_range (object, &position, &position, 0);
3502 if (iv)
3504 Lisp_Object values_here[LAST_PROP_IDX];
3505 struct props *p;
3507 /* Get properties here. */
3508 for (p = it_props; p->handler; ++p)
3509 values_here[p->idx] = textget (iv->plist,
3510 builtin_lisp_symbol (p->name));
3512 /* Look for an interval following iv that has different
3513 properties. */
3514 for (next_iv = next_interval (iv);
3515 (next_iv
3516 && (NILP (limit)
3517 || XFASTINT (limit) > next_iv->position));
3518 next_iv = next_interval (next_iv))
3520 for (p = it_props; p->handler; ++p)
3522 Lisp_Object new_value = textget (next_iv->plist,
3523 builtin_lisp_symbol (p->name));
3524 if (!EQ (values_here[p->idx], new_value))
3525 break;
3528 if (p->handler)
3529 break;
3532 if (next_iv)
3534 if (INTEGERP (limit)
3535 && next_iv->position >= XFASTINT (limit))
3536 /* No text property change up to limit. */
3537 it->stop_charpos = min (XFASTINT (limit), it->stop_charpos);
3538 else
3539 /* Text properties change in next_iv. */
3540 it->stop_charpos = min (it->stop_charpos, next_iv->position);
3544 if (it->cmp_it.id < 0)
3546 ptrdiff_t stoppos = it->end_charpos;
3548 if (it->bidi_p && it->bidi_it.scan_dir < 0)
3549 stoppos = -1;
3550 composition_compute_stop_pos (&it->cmp_it, charpos, bytepos,
3551 stoppos, it->string);
3554 eassert (STRINGP (it->string)
3555 || (it->stop_charpos >= BEGV
3556 && it->stop_charpos >= IT_CHARPOS (*it)));
3560 /* Return the position of the next overlay change after POS in
3561 current_buffer. Value is point-max if no overlay change
3562 follows. This is like `next-overlay-change' but doesn't use
3563 xmalloc. */
3565 static ptrdiff_t
3566 next_overlay_change (ptrdiff_t pos)
3568 ptrdiff_t i, noverlays;
3569 ptrdiff_t endpos;
3570 Lisp_Object *overlays;
3571 USE_SAFE_ALLOCA;
3573 /* Get all overlays at the given position. */
3574 GET_OVERLAYS_AT (pos, overlays, noverlays, &endpos, 1);
3576 /* If any of these overlays ends before endpos,
3577 use its ending point instead. */
3578 for (i = 0; i < noverlays; ++i)
3580 Lisp_Object oend;
3581 ptrdiff_t oendpos;
3583 oend = OVERLAY_END (overlays[i]);
3584 oendpos = OVERLAY_POSITION (oend);
3585 endpos = min (endpos, oendpos);
3588 SAFE_FREE ();
3589 return endpos;
3592 /* How many characters forward to search for a display property or
3593 display string. Searching too far forward makes the bidi display
3594 sluggish, especially in small windows. */
3595 #define MAX_DISP_SCAN 250
3597 /* Return the character position of a display string at or after
3598 position specified by POSITION. If no display string exists at or
3599 after POSITION, return ZV. A display string is either an overlay
3600 with `display' property whose value is a string, or a `display'
3601 text property whose value is a string. STRING is data about the
3602 string to iterate; if STRING->lstring is nil, we are iterating a
3603 buffer. FRAME_WINDOW_P is non-zero when we are displaying a window
3604 on a GUI frame. DISP_PROP is set to zero if we searched
3605 MAX_DISP_SCAN characters forward without finding any display
3606 strings, non-zero otherwise. It is set to 2 if the display string
3607 uses any kind of `(space ...)' spec that will produce a stretch of
3608 white space in the text area. */
3609 ptrdiff_t
3610 compute_display_string_pos (struct text_pos *position,
3611 struct bidi_string_data *string,
3612 struct window *w,
3613 int frame_window_p, int *disp_prop)
3615 /* OBJECT = nil means current buffer. */
3616 Lisp_Object object, object1;
3617 Lisp_Object pos, spec, limpos;
3618 int string_p = (string && (STRINGP (string->lstring) || string->s));
3619 ptrdiff_t eob = string_p ? string->schars : ZV;
3620 ptrdiff_t begb = string_p ? 0 : BEGV;
3621 ptrdiff_t bufpos, charpos = CHARPOS (*position);
3622 ptrdiff_t lim =
3623 (charpos < eob - MAX_DISP_SCAN) ? charpos + MAX_DISP_SCAN : eob;
3624 struct text_pos tpos;
3625 int rv = 0;
3627 if (string && STRINGP (string->lstring))
3628 object1 = object = string->lstring;
3629 else if (w && !string_p)
3631 XSETWINDOW (object, w);
3632 object1 = Qnil;
3634 else
3635 object1 = object = Qnil;
3637 *disp_prop = 1;
3639 if (charpos >= eob
3640 /* We don't support display properties whose values are strings
3641 that have display string properties. */
3642 || string->from_disp_str
3643 /* C strings cannot have display properties. */
3644 || (string->s && !STRINGP (object)))
3646 *disp_prop = 0;
3647 return eob;
3650 /* If the character at CHARPOS is where the display string begins,
3651 return CHARPOS. */
3652 pos = make_number (charpos);
3653 if (STRINGP (object))
3654 bufpos = string->bufpos;
3655 else
3656 bufpos = charpos;
3657 tpos = *position;
3658 if (!NILP (spec = Fget_char_property (pos, Qdisplay, object))
3659 && (charpos <= begb
3660 || !EQ (Fget_char_property (make_number (charpos - 1), Qdisplay,
3661 object),
3662 spec))
3663 && (rv = handle_display_spec (NULL, spec, object, Qnil, &tpos, bufpos,
3664 frame_window_p)))
3666 if (rv == 2)
3667 *disp_prop = 2;
3668 return charpos;
3671 /* Look forward for the first character with a `display' property
3672 that will replace the underlying text when displayed. */
3673 limpos = make_number (lim);
3674 do {
3675 pos = Fnext_single_char_property_change (pos, Qdisplay, object1, limpos);
3676 CHARPOS (tpos) = XFASTINT (pos);
3677 if (CHARPOS (tpos) >= lim)
3679 *disp_prop = 0;
3680 break;
3682 if (STRINGP (object))
3683 BYTEPOS (tpos) = string_char_to_byte (object, CHARPOS (tpos));
3684 else
3685 BYTEPOS (tpos) = CHAR_TO_BYTE (CHARPOS (tpos));
3686 spec = Fget_char_property (pos, Qdisplay, object);
3687 if (!STRINGP (object))
3688 bufpos = CHARPOS (tpos);
3689 } while (NILP (spec)
3690 || !(rv = handle_display_spec (NULL, spec, object, Qnil, &tpos,
3691 bufpos, frame_window_p)));
3692 if (rv == 2)
3693 *disp_prop = 2;
3695 return CHARPOS (tpos);
3698 /* Return the character position of the end of the display string that
3699 started at CHARPOS. If there's no display string at CHARPOS,
3700 return -1. A display string is either an overlay with `display'
3701 property whose value is a string or a `display' text property whose
3702 value is a string. */
3703 ptrdiff_t
3704 compute_display_string_end (ptrdiff_t charpos, struct bidi_string_data *string)
3706 /* OBJECT = nil means current buffer. */
3707 Lisp_Object object =
3708 (string && STRINGP (string->lstring)) ? string->lstring : Qnil;
3709 Lisp_Object pos = make_number (charpos);
3710 ptrdiff_t eob =
3711 (STRINGP (object) || (string && string->s)) ? string->schars : ZV;
3713 if (charpos >= eob || (string->s && !STRINGP (object)))
3714 return eob;
3716 /* It could happen that the display property or overlay was removed
3717 since we found it in compute_display_string_pos above. One way
3718 this can happen is if JIT font-lock was called (through
3719 handle_fontified_prop), and jit-lock-functions remove text
3720 properties or overlays from the portion of buffer that includes
3721 CHARPOS. Muse mode is known to do that, for example. In this
3722 case, we return -1 to the caller, to signal that no display
3723 string is actually present at CHARPOS. See bidi_fetch_char for
3724 how this is handled.
3726 An alternative would be to never look for display properties past
3727 it->stop_charpos. But neither compute_display_string_pos nor
3728 bidi_fetch_char that calls it know or care where the next
3729 stop_charpos is. */
3730 if (NILP (Fget_char_property (pos, Qdisplay, object)))
3731 return -1;
3733 /* Look forward for the first character where the `display' property
3734 changes. */
3735 pos = Fnext_single_char_property_change (pos, Qdisplay, object, Qnil);
3737 return XFASTINT (pos);
3742 /***********************************************************************
3743 Fontification
3744 ***********************************************************************/
3746 /* Handle changes in the `fontified' property of the current buffer by
3747 calling hook functions from Qfontification_functions to fontify
3748 regions of text. */
3750 static enum prop_handled
3751 handle_fontified_prop (struct it *it)
3753 Lisp_Object prop, pos;
3754 enum prop_handled handled = HANDLED_NORMALLY;
3756 if (!NILP (Vmemory_full))
3757 return handled;
3759 /* Get the value of the `fontified' property at IT's current buffer
3760 position. (The `fontified' property doesn't have a special
3761 meaning in strings.) If the value is nil, call functions from
3762 Qfontification_functions. */
3763 if (!STRINGP (it->string)
3764 && it->s == NULL
3765 && !NILP (Vfontification_functions)
3766 && !NILP (Vrun_hooks)
3767 && (pos = make_number (IT_CHARPOS (*it)),
3768 prop = Fget_char_property (pos, Qfontified, Qnil),
3769 /* Ignore the special cased nil value always present at EOB since
3770 no amount of fontifying will be able to change it. */
3771 NILP (prop) && IT_CHARPOS (*it) < Z))
3773 ptrdiff_t count = SPECPDL_INDEX ();
3774 Lisp_Object val;
3775 struct buffer *obuf = current_buffer;
3776 ptrdiff_t begv = BEGV, zv = ZV;
3777 bool old_clip_changed = current_buffer->clip_changed;
3779 val = Vfontification_functions;
3780 specbind (Qfontification_functions, Qnil);
3782 eassert (it->end_charpos == ZV);
3784 if (!CONSP (val) || EQ (XCAR (val), Qlambda))
3785 safe_call1 (val, pos);
3786 else
3788 Lisp_Object fns, fn;
3789 struct gcpro gcpro1, gcpro2;
3791 fns = Qnil;
3792 GCPRO2 (val, fns);
3794 for (; CONSP (val); val = XCDR (val))
3796 fn = XCAR (val);
3798 if (EQ (fn, Qt))
3800 /* A value of t indicates this hook has a local
3801 binding; it means to run the global binding too.
3802 In a global value, t should not occur. If it
3803 does, we must ignore it to avoid an endless
3804 loop. */
3805 for (fns = Fdefault_value (Qfontification_functions);
3806 CONSP (fns);
3807 fns = XCDR (fns))
3809 fn = XCAR (fns);
3810 if (!EQ (fn, Qt))
3811 safe_call1 (fn, pos);
3814 else
3815 safe_call1 (fn, pos);
3818 UNGCPRO;
3821 unbind_to (count, Qnil);
3823 /* Fontification functions routinely call `save-restriction'.
3824 Normally, this tags clip_changed, which can confuse redisplay
3825 (see discussion in Bug#6671). Since we don't perform any
3826 special handling of fontification changes in the case where
3827 `save-restriction' isn't called, there's no point doing so in
3828 this case either. So, if the buffer's restrictions are
3829 actually left unchanged, reset clip_changed. */
3830 if (obuf == current_buffer)
3832 if (begv == BEGV && zv == ZV)
3833 current_buffer->clip_changed = old_clip_changed;
3835 /* There isn't much we can reasonably do to protect against
3836 misbehaving fontification, but here's a fig leaf. */
3837 else if (BUFFER_LIVE_P (obuf))
3838 set_buffer_internal_1 (obuf);
3840 /* The fontification code may have added/removed text.
3841 It could do even a lot worse, but let's at least protect against
3842 the most obvious case where only the text past `pos' gets changed',
3843 as is/was done in grep.el where some escapes sequences are turned
3844 into face properties (bug#7876). */
3845 it->end_charpos = ZV;
3847 /* Return HANDLED_RECOMPUTE_PROPS only if function fontified
3848 something. This avoids an endless loop if they failed to
3849 fontify the text for which reason ever. */
3850 if (!NILP (Fget_char_property (pos, Qfontified, Qnil)))
3851 handled = HANDLED_RECOMPUTE_PROPS;
3854 return handled;
3859 /***********************************************************************
3860 Faces
3861 ***********************************************************************/
3863 /* Set up iterator IT from face properties at its current position.
3864 Called from handle_stop. */
3866 static enum prop_handled
3867 handle_face_prop (struct it *it)
3869 int new_face_id;
3870 ptrdiff_t next_stop;
3872 if (!STRINGP (it->string))
3874 new_face_id
3875 = face_at_buffer_position (it->w,
3876 IT_CHARPOS (*it),
3877 &next_stop,
3878 (IT_CHARPOS (*it)
3879 + TEXT_PROP_DISTANCE_LIMIT),
3880 false, it->base_face_id);
3882 /* Is this a start of a run of characters with box face?
3883 Caveat: this can be called for a freshly initialized
3884 iterator; face_id is -1 in this case. We know that the new
3885 face will not change until limit, i.e. if the new face has a
3886 box, all characters up to limit will have one. But, as
3887 usual, we don't know whether limit is really the end. */
3888 if (new_face_id != it->face_id)
3890 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3891 /* If it->face_id is -1, old_face below will be NULL, see
3892 the definition of FACE_FROM_ID. This will happen if this
3893 is the initial call that gets the face. */
3894 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
3896 /* If the value of face_id of the iterator is -1, we have to
3897 look in front of IT's position and see whether there is a
3898 face there that's different from new_face_id. */
3899 if (!old_face && IT_CHARPOS (*it) > BEG)
3901 int prev_face_id = face_before_it_pos (it);
3903 old_face = FACE_FROM_ID (it->f, prev_face_id);
3906 /* If the new face has a box, but the old face does not,
3907 this is the start of a run of characters with box face,
3908 i.e. this character has a shadow on the left side. */
3909 it->start_of_box_run_p = (new_face->box != FACE_NO_BOX
3910 && (old_face == NULL || !old_face->box));
3911 it->face_box_p = new_face->box != FACE_NO_BOX;
3914 else
3916 int base_face_id;
3917 ptrdiff_t bufpos;
3918 int i;
3919 Lisp_Object from_overlay
3920 = (it->current.overlay_string_index >= 0
3921 ? it->string_overlays[it->current.overlay_string_index
3922 % OVERLAY_STRING_CHUNK_SIZE]
3923 : Qnil);
3925 /* See if we got to this string directly or indirectly from
3926 an overlay property. That includes the before-string or
3927 after-string of an overlay, strings in display properties
3928 provided by an overlay, their text properties, etc.
3930 FROM_OVERLAY is the overlay that brought us here, or nil if none. */
3931 if (! NILP (from_overlay))
3932 for (i = it->sp - 1; i >= 0; i--)
3934 if (it->stack[i].current.overlay_string_index >= 0)
3935 from_overlay
3936 = it->string_overlays[it->stack[i].current.overlay_string_index
3937 % OVERLAY_STRING_CHUNK_SIZE];
3938 else if (! NILP (it->stack[i].from_overlay))
3939 from_overlay = it->stack[i].from_overlay;
3941 if (!NILP (from_overlay))
3942 break;
3945 if (! NILP (from_overlay))
3947 bufpos = IT_CHARPOS (*it);
3948 /* For a string from an overlay, the base face depends
3949 only on text properties and ignores overlays. */
3950 base_face_id
3951 = face_for_overlay_string (it->w,
3952 IT_CHARPOS (*it),
3953 &next_stop,
3954 (IT_CHARPOS (*it)
3955 + TEXT_PROP_DISTANCE_LIMIT),
3956 false,
3957 from_overlay);
3959 else
3961 bufpos = 0;
3963 /* For strings from a `display' property, use the face at
3964 IT's current buffer position as the base face to merge
3965 with, so that overlay strings appear in the same face as
3966 surrounding text, unless they specify their own faces.
3967 For strings from wrap-prefix and line-prefix properties,
3968 use the default face, possibly remapped via
3969 Vface_remapping_alist. */
3970 /* Note that the fact that we use the face at _buffer_
3971 position means that a 'display' property on an overlay
3972 string will not inherit the face of that overlay string,
3973 but will instead revert to the face of buffer text
3974 covered by the overlay. This is visible, e.g., when the
3975 overlay specifies a box face, but neither the buffer nor
3976 the display string do. This sounds like a design bug,
3977 but Emacs always did that since v21.1, so changing that
3978 might be a big deal. */
3979 base_face_id = it->string_from_prefix_prop_p
3980 ? (!NILP (Vface_remapping_alist)
3981 ? lookup_basic_face (it->f, DEFAULT_FACE_ID)
3982 : DEFAULT_FACE_ID)
3983 : underlying_face_id (it);
3986 new_face_id = face_at_string_position (it->w,
3987 it->string,
3988 IT_STRING_CHARPOS (*it),
3989 bufpos,
3990 &next_stop,
3991 base_face_id, false);
3993 /* Is this a start of a run of characters with box? Caveat:
3994 this can be called for a freshly allocated iterator; face_id
3995 is -1 is this case. We know that the new face will not
3996 change until the next check pos, i.e. if the new face has a
3997 box, all characters up to that position will have a
3998 box. But, as usual, we don't know whether that position
3999 is really the end. */
4000 if (new_face_id != it->face_id)
4002 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
4003 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
4005 /* If new face has a box but old face hasn't, this is the
4006 start of a run of characters with box, i.e. it has a
4007 shadow on the left side. */
4008 it->start_of_box_run_p
4009 = new_face->box && (old_face == NULL || !old_face->box);
4010 it->face_box_p = new_face->box != FACE_NO_BOX;
4014 it->face_id = new_face_id;
4015 return HANDLED_NORMALLY;
4019 /* Return the ID of the face ``underlying'' IT's current position,
4020 which is in a string. If the iterator is associated with a
4021 buffer, return the face at IT's current buffer position.
4022 Otherwise, use the iterator's base_face_id. */
4024 static int
4025 underlying_face_id (struct it *it)
4027 int face_id = it->base_face_id, i;
4029 eassert (STRINGP (it->string));
4031 for (i = it->sp - 1; i >= 0; --i)
4032 if (NILP (it->stack[i].string))
4033 face_id = it->stack[i].face_id;
4035 return face_id;
4039 /* Compute the face one character before or after the current position
4040 of IT, in the visual order. BEFORE_P non-zero means get the face
4041 in front (to the left in L2R paragraphs, to the right in R2L
4042 paragraphs) of IT's screen position. Value is the ID of the face. */
4044 static int
4045 face_before_or_after_it_pos (struct it *it, int before_p)
4047 int face_id, limit;
4048 ptrdiff_t next_check_charpos;
4049 struct it it_copy;
4050 void *it_copy_data = NULL;
4052 eassert (it->s == NULL);
4054 if (STRINGP (it->string))
4056 ptrdiff_t bufpos, charpos;
4057 int base_face_id;
4059 /* No face change past the end of the string (for the case
4060 we are padding with spaces). No face change before the
4061 string start. */
4062 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string)
4063 || (IT_STRING_CHARPOS (*it) == 0 && before_p))
4064 return it->face_id;
4066 if (!it->bidi_p)
4068 /* Set charpos to the position before or after IT's current
4069 position, in the logical order, which in the non-bidi
4070 case is the same as the visual order. */
4071 if (before_p)
4072 charpos = IT_STRING_CHARPOS (*it) - 1;
4073 else if (it->what == IT_COMPOSITION)
4074 /* For composition, we must check the character after the
4075 composition. */
4076 charpos = IT_STRING_CHARPOS (*it) + it->cmp_it.nchars;
4077 else
4078 charpos = IT_STRING_CHARPOS (*it) + 1;
4080 else
4082 if (before_p)
4084 /* With bidi iteration, the character before the current
4085 in the visual order cannot be found by simple
4086 iteration, because "reverse" reordering is not
4087 supported. Instead, we need to use the move_it_*
4088 family of functions. */
4089 /* Ignore face changes before the first visible
4090 character on this display line. */
4091 if (it->current_x <= it->first_visible_x)
4092 return it->face_id;
4093 SAVE_IT (it_copy, *it, it_copy_data);
4094 /* Implementation note: Since move_it_in_display_line
4095 works in the iterator geometry, and thinks the first
4096 character is always the leftmost, even in R2L lines,
4097 we don't need to distinguish between the R2L and L2R
4098 cases here. */
4099 move_it_in_display_line (&it_copy, SCHARS (it_copy.string),
4100 it_copy.current_x - 1, MOVE_TO_X);
4101 charpos = IT_STRING_CHARPOS (it_copy);
4102 RESTORE_IT (it, it, it_copy_data);
4104 else
4106 /* Set charpos to the string position of the character
4107 that comes after IT's current position in the visual
4108 order. */
4109 int n = (it->what == IT_COMPOSITION ? it->cmp_it.nchars : 1);
4111 it_copy = *it;
4112 while (n--)
4113 bidi_move_to_visually_next (&it_copy.bidi_it);
4115 charpos = it_copy.bidi_it.charpos;
4118 eassert (0 <= charpos && charpos <= SCHARS (it->string));
4120 if (it->current.overlay_string_index >= 0)
4121 bufpos = IT_CHARPOS (*it);
4122 else
4123 bufpos = 0;
4125 base_face_id = underlying_face_id (it);
4127 /* Get the face for ASCII, or unibyte. */
4128 face_id = face_at_string_position (it->w,
4129 it->string,
4130 charpos,
4131 bufpos,
4132 &next_check_charpos,
4133 base_face_id, false);
4135 /* Correct the face for charsets different from ASCII. Do it
4136 for the multibyte case only. The face returned above is
4137 suitable for unibyte text if IT->string is unibyte. */
4138 if (STRING_MULTIBYTE (it->string))
4140 struct text_pos pos1 = string_pos (charpos, it->string);
4141 const unsigned char *p = SDATA (it->string) + BYTEPOS (pos1);
4142 int c, len;
4143 struct face *face = FACE_FROM_ID (it->f, face_id);
4145 c = string_char_and_length (p, &len);
4146 face_id = FACE_FOR_CHAR (it->f, face, c, charpos, it->string);
4149 else
4151 struct text_pos pos;
4153 if ((IT_CHARPOS (*it) >= ZV && !before_p)
4154 || (IT_CHARPOS (*it) <= BEGV && before_p))
4155 return it->face_id;
4157 limit = IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT;
4158 pos = it->current.pos;
4160 if (!it->bidi_p)
4162 if (before_p)
4163 DEC_TEXT_POS (pos, it->multibyte_p);
4164 else
4166 if (it->what == IT_COMPOSITION)
4168 /* For composition, we must check the position after
4169 the composition. */
4170 pos.charpos += it->cmp_it.nchars;
4171 pos.bytepos += it->len;
4173 else
4174 INC_TEXT_POS (pos, it->multibyte_p);
4177 else
4179 if (before_p)
4181 /* With bidi iteration, the character before the current
4182 in the visual order cannot be found by simple
4183 iteration, because "reverse" reordering is not
4184 supported. Instead, we need to use the move_it_*
4185 family of functions. */
4186 /* Ignore face changes before the first visible
4187 character on this display line. */
4188 if (it->current_x <= it->first_visible_x)
4189 return it->face_id;
4190 SAVE_IT (it_copy, *it, it_copy_data);
4191 /* Implementation note: Since move_it_in_display_line
4192 works in the iterator geometry, and thinks the first
4193 character is always the leftmost, even in R2L lines,
4194 we don't need to distinguish between the R2L and L2R
4195 cases here. */
4196 move_it_in_display_line (&it_copy, ZV,
4197 it_copy.current_x - 1, MOVE_TO_X);
4198 pos = it_copy.current.pos;
4199 RESTORE_IT (it, it, it_copy_data);
4201 else
4203 /* Set charpos to the buffer position of the character
4204 that comes after IT's current position in the visual
4205 order. */
4206 int n = (it->what == IT_COMPOSITION ? it->cmp_it.nchars : 1);
4208 it_copy = *it;
4209 while (n--)
4210 bidi_move_to_visually_next (&it_copy.bidi_it);
4212 SET_TEXT_POS (pos,
4213 it_copy.bidi_it.charpos, it_copy.bidi_it.bytepos);
4216 eassert (BEGV <= CHARPOS (pos) && CHARPOS (pos) <= ZV);
4218 /* Determine face for CHARSET_ASCII, or unibyte. */
4219 face_id = face_at_buffer_position (it->w,
4220 CHARPOS (pos),
4221 &next_check_charpos,
4222 limit, false, -1);
4224 /* Correct the face for charsets different from ASCII. Do it
4225 for the multibyte case only. The face returned above is
4226 suitable for unibyte text if current_buffer is unibyte. */
4227 if (it->multibyte_p)
4229 int c = FETCH_MULTIBYTE_CHAR (BYTEPOS (pos));
4230 struct face *face = FACE_FROM_ID (it->f, face_id);
4231 face_id = FACE_FOR_CHAR (it->f, face, c, CHARPOS (pos), Qnil);
4235 return face_id;
4240 /***********************************************************************
4241 Invisible text
4242 ***********************************************************************/
4244 /* Set up iterator IT from invisible properties at its current
4245 position. Called from handle_stop. */
4247 static enum prop_handled
4248 handle_invisible_prop (struct it *it)
4250 enum prop_handled handled = HANDLED_NORMALLY;
4251 int invis_p;
4252 Lisp_Object prop;
4254 if (STRINGP (it->string))
4256 Lisp_Object end_charpos, limit, charpos;
4258 /* Get the value of the invisible text property at the
4259 current position. Value will be nil if there is no such
4260 property. */
4261 charpos = make_number (IT_STRING_CHARPOS (*it));
4262 prop = Fget_text_property (charpos, Qinvisible, it->string);
4263 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
4265 if (invis_p && IT_STRING_CHARPOS (*it) < it->end_charpos)
4267 /* Record whether we have to display an ellipsis for the
4268 invisible text. */
4269 int display_ellipsis_p = (invis_p == 2);
4270 ptrdiff_t len, endpos;
4272 handled = HANDLED_RECOMPUTE_PROPS;
4274 /* Get the position at which the next visible text can be
4275 found in IT->string, if any. */
4276 endpos = len = SCHARS (it->string);
4277 XSETINT (limit, len);
4280 end_charpos = Fnext_single_property_change (charpos, Qinvisible,
4281 it->string, limit);
4282 if (INTEGERP (end_charpos))
4284 endpos = XFASTINT (end_charpos);
4285 prop = Fget_text_property (end_charpos, Qinvisible, it->string);
4286 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
4287 if (invis_p == 2)
4288 display_ellipsis_p = true;
4291 while (invis_p && endpos < len);
4293 if (display_ellipsis_p)
4294 it->ellipsis_p = true;
4296 if (endpos < len)
4298 /* Text at END_CHARPOS is visible. Move IT there. */
4299 struct text_pos old;
4300 ptrdiff_t oldpos;
4302 old = it->current.string_pos;
4303 oldpos = CHARPOS (old);
4304 if (it->bidi_p)
4306 if (it->bidi_it.first_elt
4307 && it->bidi_it.charpos < SCHARS (it->string))
4308 bidi_paragraph_init (it->paragraph_embedding,
4309 &it->bidi_it, 1);
4310 /* Bidi-iterate out of the invisible text. */
4313 bidi_move_to_visually_next (&it->bidi_it);
4315 while (oldpos <= it->bidi_it.charpos
4316 && it->bidi_it.charpos < endpos);
4318 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
4319 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
4320 if (IT_CHARPOS (*it) >= endpos)
4321 it->prev_stop = endpos;
4323 else
4325 IT_STRING_CHARPOS (*it) = XFASTINT (end_charpos);
4326 compute_string_pos (&it->current.string_pos, old, it->string);
4329 else
4331 /* The rest of the string is invisible. If this is an
4332 overlay string, proceed with the next overlay string
4333 or whatever comes and return a character from there. */
4334 if (it->current.overlay_string_index >= 0
4335 && !display_ellipsis_p)
4337 next_overlay_string (it);
4338 /* Don't check for overlay strings when we just
4339 finished processing them. */
4340 handled = HANDLED_OVERLAY_STRING_CONSUMED;
4342 else
4344 IT_STRING_CHARPOS (*it) = SCHARS (it->string);
4345 IT_STRING_BYTEPOS (*it) = SBYTES (it->string);
4350 else
4352 ptrdiff_t newpos, next_stop, start_charpos, tem;
4353 Lisp_Object pos, overlay;
4355 /* First of all, is there invisible text at this position? */
4356 tem = start_charpos = IT_CHARPOS (*it);
4357 pos = make_number (tem);
4358 prop = get_char_property_and_overlay (pos, Qinvisible, it->window,
4359 &overlay);
4360 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
4362 /* If we are on invisible text, skip over it. */
4363 if (invis_p && start_charpos < it->end_charpos)
4365 /* Record whether we have to display an ellipsis for the
4366 invisible text. */
4367 int display_ellipsis_p = invis_p == 2;
4369 handled = HANDLED_RECOMPUTE_PROPS;
4371 /* Loop skipping over invisible text. The loop is left at
4372 ZV or with IT on the first char being visible again. */
4375 /* Try to skip some invisible text. Return value is the
4376 position reached which can be equal to where we start
4377 if there is nothing invisible there. This skips both
4378 over invisible text properties and overlays with
4379 invisible property. */
4380 newpos = skip_invisible (tem, &next_stop, ZV, it->window);
4382 /* If we skipped nothing at all we weren't at invisible
4383 text in the first place. If everything to the end of
4384 the buffer was skipped, end the loop. */
4385 if (newpos == tem || newpos >= ZV)
4386 invis_p = 0;
4387 else
4389 /* We skipped some characters but not necessarily
4390 all there are. Check if we ended up on visible
4391 text. Fget_char_property returns the property of
4392 the char before the given position, i.e. if we
4393 get invis_p = 0, this means that the char at
4394 newpos is visible. */
4395 pos = make_number (newpos);
4396 prop = Fget_char_property (pos, Qinvisible, it->window);
4397 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
4400 /* If we ended up on invisible text, proceed to
4401 skip starting with next_stop. */
4402 if (invis_p)
4403 tem = next_stop;
4405 /* If there are adjacent invisible texts, don't lose the
4406 second one's ellipsis. */
4407 if (invis_p == 2)
4408 display_ellipsis_p = true;
4410 while (invis_p);
4412 /* The position newpos is now either ZV or on visible text. */
4413 if (it->bidi_p)
4415 ptrdiff_t bpos = CHAR_TO_BYTE (newpos);
4416 int on_newline
4417 = bpos == ZV_BYTE || FETCH_BYTE (bpos) == '\n';
4418 int after_newline
4419 = newpos <= BEGV || FETCH_BYTE (bpos - 1) == '\n';
4421 /* If the invisible text ends on a newline or on a
4422 character after a newline, we can avoid the costly,
4423 character by character, bidi iteration to NEWPOS, and
4424 instead simply reseat the iterator there. That's
4425 because all bidi reordering information is tossed at
4426 the newline. This is a big win for modes that hide
4427 complete lines, like Outline, Org, etc. */
4428 if (on_newline || after_newline)
4430 struct text_pos tpos;
4431 bidi_dir_t pdir = it->bidi_it.paragraph_dir;
4433 SET_TEXT_POS (tpos, newpos, bpos);
4434 reseat_1 (it, tpos, 0);
4435 /* If we reseat on a newline/ZV, we need to prep the
4436 bidi iterator for advancing to the next character
4437 after the newline/EOB, keeping the current paragraph
4438 direction (so that PRODUCE_GLYPHS does TRT wrt
4439 prepending/appending glyphs to a glyph row). */
4440 if (on_newline)
4442 it->bidi_it.first_elt = 0;
4443 it->bidi_it.paragraph_dir = pdir;
4444 it->bidi_it.ch = (bpos == ZV_BYTE) ? -1 : '\n';
4445 it->bidi_it.nchars = 1;
4446 it->bidi_it.ch_len = 1;
4449 else /* Must use the slow method. */
4451 /* With bidi iteration, the region of invisible text
4452 could start and/or end in the middle of a
4453 non-base embedding level. Therefore, we need to
4454 skip invisible text using the bidi iterator,
4455 starting at IT's current position, until we find
4456 ourselves outside of the invisible text.
4457 Skipping invisible text _after_ bidi iteration
4458 avoids affecting the visual order of the
4459 displayed text when invisible properties are
4460 added or removed. */
4461 if (it->bidi_it.first_elt && it->bidi_it.charpos < ZV)
4463 /* If we were `reseat'ed to a new paragraph,
4464 determine the paragraph base direction. We
4465 need to do it now because
4466 next_element_from_buffer may not have a
4467 chance to do it, if we are going to skip any
4468 text at the beginning, which resets the
4469 FIRST_ELT flag. */
4470 bidi_paragraph_init (it->paragraph_embedding,
4471 &it->bidi_it, 1);
4475 bidi_move_to_visually_next (&it->bidi_it);
4477 while (it->stop_charpos <= it->bidi_it.charpos
4478 && it->bidi_it.charpos < newpos);
4479 IT_CHARPOS (*it) = it->bidi_it.charpos;
4480 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
4481 /* If we overstepped NEWPOS, record its position in
4482 the iterator, so that we skip invisible text if
4483 later the bidi iteration lands us in the
4484 invisible region again. */
4485 if (IT_CHARPOS (*it) >= newpos)
4486 it->prev_stop = newpos;
4489 else
4491 IT_CHARPOS (*it) = newpos;
4492 IT_BYTEPOS (*it) = CHAR_TO_BYTE (newpos);
4495 /* If there are before-strings at the start of invisible
4496 text, and the text is invisible because of a text
4497 property, arrange to show before-strings because 20.x did
4498 it that way. (If the text is invisible because of an
4499 overlay property instead of a text property, this is
4500 already handled in the overlay code.) */
4501 if (NILP (overlay)
4502 && get_overlay_strings (it, it->stop_charpos))
4504 handled = HANDLED_RECOMPUTE_PROPS;
4505 if (it->sp > 0)
4507 it->stack[it->sp - 1].display_ellipsis_p = display_ellipsis_p;
4508 /* The call to get_overlay_strings above recomputes
4509 it->stop_charpos, but it only considers changes
4510 in properties and overlays beyond iterator's
4511 current position. This causes us to miss changes
4512 that happen exactly where the invisible property
4513 ended. So we play it safe here and force the
4514 iterator to check for potential stop positions
4515 immediately after the invisible text. Note that
4516 if get_overlay_strings returns non-zero, it
4517 normally also pushed the iterator stack, so we
4518 need to update the stop position in the slot
4519 below the current one. */
4520 it->stack[it->sp - 1].stop_charpos
4521 = CHARPOS (it->stack[it->sp - 1].current.pos);
4524 else if (display_ellipsis_p)
4526 /* Make sure that the glyphs of the ellipsis will get
4527 correct `charpos' values. If we would not update
4528 it->position here, the glyphs would belong to the
4529 last visible character _before_ the invisible
4530 text, which confuses `set_cursor_from_row'.
4532 We use the last invisible position instead of the
4533 first because this way the cursor is always drawn on
4534 the first "." of the ellipsis, whenever PT is inside
4535 the invisible text. Otherwise the cursor would be
4536 placed _after_ the ellipsis when the point is after the
4537 first invisible character. */
4538 if (!STRINGP (it->object))
4540 it->position.charpos = newpos - 1;
4541 it->position.bytepos = CHAR_TO_BYTE (it->position.charpos);
4543 it->ellipsis_p = true;
4544 /* Let the ellipsis display before
4545 considering any properties of the following char.
4546 Fixes jasonr@gnu.org 01 Oct 07 bug. */
4547 handled = HANDLED_RETURN;
4552 return handled;
4556 /* Make iterator IT return `...' next.
4557 Replaces LEN characters from buffer. */
4559 static void
4560 setup_for_ellipsis (struct it *it, int len)
4562 /* Use the display table definition for `...'. Invalid glyphs
4563 will be handled by the method returning elements from dpvec. */
4564 if (it->dp && VECTORP (DISP_INVIS_VECTOR (it->dp)))
4566 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
4567 it->dpvec = v->contents;
4568 it->dpend = v->contents + v->header.size;
4570 else
4572 /* Default `...'. */
4573 it->dpvec = default_invis_vector;
4574 it->dpend = default_invis_vector + 3;
4577 it->dpvec_char_len = len;
4578 it->current.dpvec_index = 0;
4579 it->dpvec_face_id = -1;
4581 /* Remember the current face id in case glyphs specify faces.
4582 IT's face is restored in set_iterator_to_next.
4583 saved_face_id was set to preceding char's face in handle_stop. */
4584 if (it->saved_face_id < 0 || it->saved_face_id != it->face_id)
4585 it->saved_face_id = it->face_id = DEFAULT_FACE_ID;
4587 it->method = GET_FROM_DISPLAY_VECTOR;
4588 it->ellipsis_p = true;
4593 /***********************************************************************
4594 'display' property
4595 ***********************************************************************/
4597 /* Set up iterator IT from `display' property at its current position.
4598 Called from handle_stop.
4599 We return HANDLED_RETURN if some part of the display property
4600 overrides the display of the buffer text itself.
4601 Otherwise we return HANDLED_NORMALLY. */
4603 static enum prop_handled
4604 handle_display_prop (struct it *it)
4606 Lisp_Object propval, object, overlay;
4607 struct text_pos *position;
4608 ptrdiff_t bufpos;
4609 /* Nonzero if some property replaces the display of the text itself. */
4610 int display_replaced_p = 0;
4612 if (STRINGP (it->string))
4614 object = it->string;
4615 position = &it->current.string_pos;
4616 bufpos = CHARPOS (it->current.pos);
4618 else
4620 XSETWINDOW (object, it->w);
4621 position = &it->current.pos;
4622 bufpos = CHARPOS (*position);
4625 /* Reset those iterator values set from display property values. */
4626 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
4627 it->space_width = Qnil;
4628 it->font_height = Qnil;
4629 it->voffset = 0;
4631 /* We don't support recursive `display' properties, i.e. string
4632 values that have a string `display' property, that have a string
4633 `display' property etc. */
4634 if (!it->string_from_display_prop_p)
4635 it->area = TEXT_AREA;
4637 propval = get_char_property_and_overlay (make_number (position->charpos),
4638 Qdisplay, object, &overlay);
4639 if (NILP (propval))
4640 return HANDLED_NORMALLY;
4641 /* Now OVERLAY is the overlay that gave us this property, or nil
4642 if it was a text property. */
4644 if (!STRINGP (it->string))
4645 object = it->w->contents;
4647 display_replaced_p = handle_display_spec (it, propval, object, overlay,
4648 position, bufpos,
4649 FRAME_WINDOW_P (it->f));
4651 return display_replaced_p ? HANDLED_RETURN : HANDLED_NORMALLY;
4654 /* Subroutine of handle_display_prop. Returns non-zero if the display
4655 specification in SPEC is a replacing specification, i.e. it would
4656 replace the text covered by `display' property with something else,
4657 such as an image or a display string. If SPEC includes any kind or
4658 `(space ...) specification, the value is 2; this is used by
4659 compute_display_string_pos, which see.
4661 See handle_single_display_spec for documentation of arguments.
4662 frame_window_p is non-zero if the window being redisplayed is on a
4663 GUI frame; this argument is used only if IT is NULL, see below.
4665 IT can be NULL, if this is called by the bidi reordering code
4666 through compute_display_string_pos, which see. In that case, this
4667 function only examines SPEC, but does not otherwise "handle" it, in
4668 the sense that it doesn't set up members of IT from the display
4669 spec. */
4670 static int
4671 handle_display_spec (struct it *it, Lisp_Object spec, Lisp_Object object,
4672 Lisp_Object overlay, struct text_pos *position,
4673 ptrdiff_t bufpos, int frame_window_p)
4675 int replacing_p = 0;
4676 int rv;
4678 if (CONSP (spec)
4679 /* Simple specifications. */
4680 && !EQ (XCAR (spec), Qimage)
4681 && !EQ (XCAR (spec), Qspace)
4682 && !EQ (XCAR (spec), Qwhen)
4683 && !EQ (XCAR (spec), Qslice)
4684 && !EQ (XCAR (spec), Qspace_width)
4685 && !EQ (XCAR (spec), Qheight)
4686 && !EQ (XCAR (spec), Qraise)
4687 /* Marginal area specifications. */
4688 && !(CONSP (XCAR (spec)) && EQ (XCAR (XCAR (spec)), Qmargin))
4689 && !EQ (XCAR (spec), Qleft_fringe)
4690 && !EQ (XCAR (spec), Qright_fringe)
4691 && !NILP (XCAR (spec)))
4693 for (; CONSP (spec); spec = XCDR (spec))
4695 if ((rv = handle_single_display_spec (it, XCAR (spec), object,
4696 overlay, position, bufpos,
4697 replacing_p, frame_window_p)))
4699 replacing_p = rv;
4700 /* If some text in a string is replaced, `position' no
4701 longer points to the position of `object'. */
4702 if (!it || STRINGP (object))
4703 break;
4707 else if (VECTORP (spec))
4709 ptrdiff_t i;
4710 for (i = 0; i < ASIZE (spec); ++i)
4711 if ((rv = handle_single_display_spec (it, AREF (spec, i), object,
4712 overlay, position, bufpos,
4713 replacing_p, frame_window_p)))
4715 replacing_p = rv;
4716 /* If some text in a string is replaced, `position' no
4717 longer points to the position of `object'. */
4718 if (!it || STRINGP (object))
4719 break;
4722 else
4724 if ((rv = handle_single_display_spec (it, spec, object, overlay,
4725 position, bufpos, 0,
4726 frame_window_p)))
4727 replacing_p = rv;
4730 return replacing_p;
4733 /* Value is the position of the end of the `display' property starting
4734 at START_POS in OBJECT. */
4736 static struct text_pos
4737 display_prop_end (struct it *it, Lisp_Object object, struct text_pos start_pos)
4739 Lisp_Object end;
4740 struct text_pos end_pos;
4742 end = Fnext_single_char_property_change (make_number (CHARPOS (start_pos)),
4743 Qdisplay, object, Qnil);
4744 CHARPOS (end_pos) = XFASTINT (end);
4745 if (STRINGP (object))
4746 compute_string_pos (&end_pos, start_pos, it->string);
4747 else
4748 BYTEPOS (end_pos) = CHAR_TO_BYTE (XFASTINT (end));
4750 return end_pos;
4754 /* Set up IT from a single `display' property specification SPEC. OBJECT
4755 is the object in which the `display' property was found. *POSITION
4756 is the position in OBJECT at which the `display' property was found.
4757 BUFPOS is the buffer position of OBJECT (different from POSITION if
4758 OBJECT is not a buffer). DISPLAY_REPLACED_P non-zero means that we
4759 previously saw a display specification which already replaced text
4760 display with something else, for example an image; we ignore such
4761 properties after the first one has been processed.
4763 OVERLAY is the overlay this `display' property came from,
4764 or nil if it was a text property.
4766 If SPEC is a `space' or `image' specification, and in some other
4767 cases too, set *POSITION to the position where the `display'
4768 property ends.
4770 If IT is NULL, only examine the property specification in SPEC, but
4771 don't set up IT. In that case, FRAME_WINDOW_P non-zero means SPEC
4772 is intended to be displayed in a window on a GUI frame.
4774 Value is non-zero if something was found which replaces the display
4775 of buffer or string text. */
4777 static int
4778 handle_single_display_spec (struct it *it, Lisp_Object spec, Lisp_Object object,
4779 Lisp_Object overlay, struct text_pos *position,
4780 ptrdiff_t bufpos, int display_replaced_p,
4781 int frame_window_p)
4783 Lisp_Object form;
4784 Lisp_Object location, value;
4785 struct text_pos start_pos = *position;
4786 int valid_p;
4788 /* If SPEC is a list of the form `(when FORM . VALUE)', evaluate FORM.
4789 If the result is non-nil, use VALUE instead of SPEC. */
4790 form = Qt;
4791 if (CONSP (spec) && EQ (XCAR (spec), Qwhen))
4793 spec = XCDR (spec);
4794 if (!CONSP (spec))
4795 return 0;
4796 form = XCAR (spec);
4797 spec = XCDR (spec);
4800 if (!NILP (form) && !EQ (form, Qt))
4802 ptrdiff_t count = SPECPDL_INDEX ();
4803 struct gcpro gcpro1;
4805 /* Bind `object' to the object having the `display' property, a
4806 buffer or string. Bind `position' to the position in the
4807 object where the property was found, and `buffer-position'
4808 to the current position in the buffer. */
4810 if (NILP (object))
4811 XSETBUFFER (object, current_buffer);
4812 specbind (Qobject, object);
4813 specbind (Qposition, make_number (CHARPOS (*position)));
4814 specbind (Qbuffer_position, make_number (bufpos));
4815 GCPRO1 (form);
4816 form = safe_eval (form);
4817 UNGCPRO;
4818 unbind_to (count, Qnil);
4821 if (NILP (form))
4822 return 0;
4824 /* Handle `(height HEIGHT)' specifications. */
4825 if (CONSP (spec)
4826 && EQ (XCAR (spec), Qheight)
4827 && CONSP (XCDR (spec)))
4829 if (it)
4831 if (!FRAME_WINDOW_P (it->f))
4832 return 0;
4834 it->font_height = XCAR (XCDR (spec));
4835 if (!NILP (it->font_height))
4837 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4838 int new_height = -1;
4840 if (CONSP (it->font_height)
4841 && (EQ (XCAR (it->font_height), Qplus)
4842 || EQ (XCAR (it->font_height), Qminus))
4843 && CONSP (XCDR (it->font_height))
4844 && RANGED_INTEGERP (0, XCAR (XCDR (it->font_height)), INT_MAX))
4846 /* `(+ N)' or `(- N)' where N is an integer. */
4847 int steps = XINT (XCAR (XCDR (it->font_height)));
4848 if (EQ (XCAR (it->font_height), Qplus))
4849 steps = - steps;
4850 it->face_id = smaller_face (it->f, it->face_id, steps);
4852 else if (FUNCTIONP (it->font_height))
4854 /* Call function with current height as argument.
4855 Value is the new height. */
4856 Lisp_Object height;
4857 height = safe_call1 (it->font_height,
4858 face->lface[LFACE_HEIGHT_INDEX]);
4859 if (NUMBERP (height))
4860 new_height = XFLOATINT (height);
4862 else if (NUMBERP (it->font_height))
4864 /* Value is a multiple of the canonical char height. */
4865 struct face *f;
4867 f = FACE_FROM_ID (it->f,
4868 lookup_basic_face (it->f, DEFAULT_FACE_ID));
4869 new_height = (XFLOATINT (it->font_height)
4870 * XINT (f->lface[LFACE_HEIGHT_INDEX]));
4872 else
4874 /* Evaluate IT->font_height with `height' bound to the
4875 current specified height to get the new height. */
4876 ptrdiff_t count = SPECPDL_INDEX ();
4878 specbind (Qheight, face->lface[LFACE_HEIGHT_INDEX]);
4879 value = safe_eval (it->font_height);
4880 unbind_to (count, Qnil);
4882 if (NUMBERP (value))
4883 new_height = XFLOATINT (value);
4886 if (new_height > 0)
4887 it->face_id = face_with_height (it->f, it->face_id, new_height);
4891 return 0;
4894 /* Handle `(space-width WIDTH)'. */
4895 if (CONSP (spec)
4896 && EQ (XCAR (spec), Qspace_width)
4897 && CONSP (XCDR (spec)))
4899 if (it)
4901 if (!FRAME_WINDOW_P (it->f))
4902 return 0;
4904 value = XCAR (XCDR (spec));
4905 if (NUMBERP (value) && XFLOATINT (value) > 0)
4906 it->space_width = value;
4909 return 0;
4912 /* Handle `(slice X Y WIDTH HEIGHT)'. */
4913 if (CONSP (spec)
4914 && EQ (XCAR (spec), Qslice))
4916 Lisp_Object tem;
4918 if (it)
4920 if (!FRAME_WINDOW_P (it->f))
4921 return 0;
4923 if (tem = XCDR (spec), CONSP (tem))
4925 it->slice.x = XCAR (tem);
4926 if (tem = XCDR (tem), CONSP (tem))
4928 it->slice.y = XCAR (tem);
4929 if (tem = XCDR (tem), CONSP (tem))
4931 it->slice.width = XCAR (tem);
4932 if (tem = XCDR (tem), CONSP (tem))
4933 it->slice.height = XCAR (tem);
4939 return 0;
4942 /* Handle `(raise FACTOR)'. */
4943 if (CONSP (spec)
4944 && EQ (XCAR (spec), Qraise)
4945 && CONSP (XCDR (spec)))
4947 if (it)
4949 if (!FRAME_WINDOW_P (it->f))
4950 return 0;
4952 #ifdef HAVE_WINDOW_SYSTEM
4953 value = XCAR (XCDR (spec));
4954 if (NUMBERP (value))
4956 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4957 it->voffset = - (XFLOATINT (value)
4958 * (FONT_HEIGHT (face->font)));
4960 #endif /* HAVE_WINDOW_SYSTEM */
4963 return 0;
4966 /* Don't handle the other kinds of display specifications
4967 inside a string that we got from a `display' property. */
4968 if (it && it->string_from_display_prop_p)
4969 return 0;
4971 /* Characters having this form of property are not displayed, so
4972 we have to find the end of the property. */
4973 if (it)
4975 start_pos = *position;
4976 *position = display_prop_end (it, object, start_pos);
4978 value = Qnil;
4980 /* Stop the scan at that end position--we assume that all
4981 text properties change there. */
4982 if (it)
4983 it->stop_charpos = position->charpos;
4985 /* Handle `(left-fringe BITMAP [FACE])'
4986 and `(right-fringe BITMAP [FACE])'. */
4987 if (CONSP (spec)
4988 && (EQ (XCAR (spec), Qleft_fringe)
4989 || EQ (XCAR (spec), Qright_fringe))
4990 && CONSP (XCDR (spec)))
4992 int fringe_bitmap;
4994 if (it)
4996 if (!FRAME_WINDOW_P (it->f))
4997 /* If we return here, POSITION has been advanced
4998 across the text with this property. */
5000 /* Synchronize the bidi iterator with POSITION. This is
5001 needed because we are not going to push the iterator
5002 on behalf of this display property, so there will be
5003 no pop_it call to do this synchronization for us. */
5004 if (it->bidi_p)
5006 it->position = *position;
5007 iterate_out_of_display_property (it);
5008 *position = it->position;
5010 /* If we were to display this fringe bitmap,
5011 next_element_from_image would have reset this flag.
5012 Do the same, to avoid affecting overlays that
5013 follow. */
5014 it->ignore_overlay_strings_at_pos_p = 0;
5015 return 1;
5018 else if (!frame_window_p)
5019 return 1;
5021 #ifdef HAVE_WINDOW_SYSTEM
5022 value = XCAR (XCDR (spec));
5023 if (!SYMBOLP (value)
5024 || !(fringe_bitmap = lookup_fringe_bitmap (value)))
5025 /* If we return here, POSITION has been advanced
5026 across the text with this property. */
5028 if (it && it->bidi_p)
5030 it->position = *position;
5031 iterate_out_of_display_property (it);
5032 *position = it->position;
5034 if (it)
5035 /* Reset this flag like next_element_from_image would. */
5036 it->ignore_overlay_strings_at_pos_p = 0;
5037 return 1;
5040 if (it)
5042 int face_id = lookup_basic_face (it->f, DEFAULT_FACE_ID);
5044 if (CONSP (XCDR (XCDR (spec))))
5046 Lisp_Object face_name = XCAR (XCDR (XCDR (spec)));
5047 int face_id2 = lookup_derived_face (it->f, face_name,
5048 FRINGE_FACE_ID, 0);
5049 if (face_id2 >= 0)
5050 face_id = face_id2;
5053 /* Save current settings of IT so that we can restore them
5054 when we are finished with the glyph property value. */
5055 push_it (it, position);
5057 it->area = TEXT_AREA;
5058 it->what = IT_IMAGE;
5059 it->image_id = -1; /* no image */
5060 it->position = start_pos;
5061 it->object = NILP (object) ? it->w->contents : object;
5062 it->method = GET_FROM_IMAGE;
5063 it->from_overlay = Qnil;
5064 it->face_id = face_id;
5065 it->from_disp_prop_p = true;
5067 /* Say that we haven't consumed the characters with
5068 `display' property yet. The call to pop_it in
5069 set_iterator_to_next will clean this up. */
5070 *position = start_pos;
5072 if (EQ (XCAR (spec), Qleft_fringe))
5074 it->left_user_fringe_bitmap = fringe_bitmap;
5075 it->left_user_fringe_face_id = face_id;
5077 else
5079 it->right_user_fringe_bitmap = fringe_bitmap;
5080 it->right_user_fringe_face_id = face_id;
5083 #endif /* HAVE_WINDOW_SYSTEM */
5084 return 1;
5087 /* Prepare to handle `((margin left-margin) ...)',
5088 `((margin right-margin) ...)' and `((margin nil) ...)'
5089 prefixes for display specifications. */
5090 location = Qunbound;
5091 if (CONSP (spec) && CONSP (XCAR (spec)))
5093 Lisp_Object tem;
5095 value = XCDR (spec);
5096 if (CONSP (value))
5097 value = XCAR (value);
5099 tem = XCAR (spec);
5100 if (EQ (XCAR (tem), Qmargin)
5101 && (tem = XCDR (tem),
5102 tem = CONSP (tem) ? XCAR (tem) : Qnil,
5103 (NILP (tem)
5104 || EQ (tem, Qleft_margin)
5105 || EQ (tem, Qright_margin))))
5106 location = tem;
5109 if (EQ (location, Qunbound))
5111 location = Qnil;
5112 value = spec;
5115 /* After this point, VALUE is the property after any
5116 margin prefix has been stripped. It must be a string,
5117 an image specification, or `(space ...)'.
5119 LOCATION specifies where to display: `left-margin',
5120 `right-margin' or nil. */
5122 valid_p = (STRINGP (value)
5123 #ifdef HAVE_WINDOW_SYSTEM
5124 || ((it ? FRAME_WINDOW_P (it->f) : frame_window_p)
5125 && valid_image_p (value))
5126 #endif /* not HAVE_WINDOW_SYSTEM */
5127 || (CONSP (value) && EQ (XCAR (value), Qspace)));
5129 if (valid_p && !display_replaced_p)
5131 int retval = 1;
5133 if (!it)
5135 /* Callers need to know whether the display spec is any kind
5136 of `(space ...)' spec that is about to affect text-area
5137 display. */
5138 if (CONSP (value) && EQ (XCAR (value), Qspace) && NILP (location))
5139 retval = 2;
5140 return retval;
5143 /* Save current settings of IT so that we can restore them
5144 when we are finished with the glyph property value. */
5145 push_it (it, position);
5146 it->from_overlay = overlay;
5147 it->from_disp_prop_p = true;
5149 if (NILP (location))
5150 it->area = TEXT_AREA;
5151 else if (EQ (location, Qleft_margin))
5152 it->area = LEFT_MARGIN_AREA;
5153 else
5154 it->area = RIGHT_MARGIN_AREA;
5156 if (STRINGP (value))
5158 it->string = value;
5159 it->multibyte_p = STRING_MULTIBYTE (it->string);
5160 it->current.overlay_string_index = -1;
5161 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
5162 it->end_charpos = it->string_nchars = SCHARS (it->string);
5163 it->method = GET_FROM_STRING;
5164 it->stop_charpos = 0;
5165 it->prev_stop = 0;
5166 it->base_level_stop = 0;
5167 it->string_from_display_prop_p = true;
5168 /* Say that we haven't consumed the characters with
5169 `display' property yet. The call to pop_it in
5170 set_iterator_to_next will clean this up. */
5171 if (BUFFERP (object))
5172 *position = start_pos;
5174 /* Force paragraph direction to be that of the parent
5175 object. If the parent object's paragraph direction is
5176 not yet determined, default to L2R. */
5177 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
5178 it->paragraph_embedding = it->bidi_it.paragraph_dir;
5179 else
5180 it->paragraph_embedding = L2R;
5182 /* Set up the bidi iterator for this display string. */
5183 if (it->bidi_p)
5185 it->bidi_it.string.lstring = it->string;
5186 it->bidi_it.string.s = NULL;
5187 it->bidi_it.string.schars = it->end_charpos;
5188 it->bidi_it.string.bufpos = bufpos;
5189 it->bidi_it.string.from_disp_str = 1;
5190 it->bidi_it.string.unibyte = !it->multibyte_p;
5191 it->bidi_it.w = it->w;
5192 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
5195 else if (CONSP (value) && EQ (XCAR (value), Qspace))
5197 it->method = GET_FROM_STRETCH;
5198 it->object = value;
5199 *position = it->position = start_pos;
5200 retval = 1 + (it->area == TEXT_AREA);
5202 #ifdef HAVE_WINDOW_SYSTEM
5203 else
5205 it->what = IT_IMAGE;
5206 it->image_id = lookup_image (it->f, value);
5207 it->position = start_pos;
5208 it->object = NILP (object) ? it->w->contents : object;
5209 it->method = GET_FROM_IMAGE;
5211 /* Say that we haven't consumed the characters with
5212 `display' property yet. The call to pop_it in
5213 set_iterator_to_next will clean this up. */
5214 *position = start_pos;
5216 #endif /* HAVE_WINDOW_SYSTEM */
5218 return retval;
5221 /* Invalid property or property not supported. Restore
5222 POSITION to what it was before. */
5223 *position = start_pos;
5224 return 0;
5227 /* Check if PROP is a display property value whose text should be
5228 treated as intangible. OVERLAY is the overlay from which PROP
5229 came, or nil if it came from a text property. CHARPOS and BYTEPOS
5230 specify the buffer position covered by PROP. */
5233 display_prop_intangible_p (Lisp_Object prop, Lisp_Object overlay,
5234 ptrdiff_t charpos, ptrdiff_t bytepos)
5236 int frame_window_p = FRAME_WINDOW_P (XFRAME (selected_frame));
5237 struct text_pos position;
5239 SET_TEXT_POS (position, charpos, bytepos);
5240 return handle_display_spec (NULL, prop, Qnil, overlay,
5241 &position, charpos, frame_window_p);
5245 /* Return 1 if PROP is a display sub-property value containing STRING.
5247 Implementation note: this and the following function are really
5248 special cases of handle_display_spec and
5249 handle_single_display_spec, and should ideally use the same code.
5250 Until they do, these two pairs must be consistent and must be
5251 modified in sync. */
5253 static int
5254 single_display_spec_string_p (Lisp_Object prop, Lisp_Object string)
5256 if (EQ (string, prop))
5257 return 1;
5259 /* Skip over `when FORM'. */
5260 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
5262 prop = XCDR (prop);
5263 if (!CONSP (prop))
5264 return 0;
5265 /* Actually, the condition following `when' should be eval'ed,
5266 like handle_single_display_spec does, and we should return
5267 zero if it evaluates to nil. However, this function is
5268 called only when the buffer was already displayed and some
5269 glyph in the glyph matrix was found to come from a display
5270 string. Therefore, the condition was already evaluated, and
5271 the result was non-nil, otherwise the display string wouldn't
5272 have been displayed and we would have never been called for
5273 this property. Thus, we can skip the evaluation and assume
5274 its result is non-nil. */
5275 prop = XCDR (prop);
5278 if (CONSP (prop))
5279 /* Skip over `margin LOCATION'. */
5280 if (EQ (XCAR (prop), Qmargin))
5282 prop = XCDR (prop);
5283 if (!CONSP (prop))
5284 return 0;
5286 prop = XCDR (prop);
5287 if (!CONSP (prop))
5288 return 0;
5291 return EQ (prop, string) || (CONSP (prop) && EQ (XCAR (prop), string));
5295 /* Return 1 if STRING appears in the `display' property PROP. */
5297 static int
5298 display_prop_string_p (Lisp_Object prop, Lisp_Object string)
5300 if (CONSP (prop)
5301 && !EQ (XCAR (prop), Qwhen)
5302 && !(CONSP (XCAR (prop)) && EQ (Qmargin, XCAR (XCAR (prop)))))
5304 /* A list of sub-properties. */
5305 while (CONSP (prop))
5307 if (single_display_spec_string_p (XCAR (prop), string))
5308 return 1;
5309 prop = XCDR (prop);
5312 else if (VECTORP (prop))
5314 /* A vector of sub-properties. */
5315 ptrdiff_t i;
5316 for (i = 0; i < ASIZE (prop); ++i)
5317 if (single_display_spec_string_p (AREF (prop, i), string))
5318 return 1;
5320 else
5321 return single_display_spec_string_p (prop, string);
5323 return 0;
5326 /* Look for STRING in overlays and text properties in the current
5327 buffer, between character positions FROM and TO (excluding TO).
5328 BACK_P non-zero means look back (in this case, TO is supposed to be
5329 less than FROM).
5330 Value is the first character position where STRING was found, or
5331 zero if it wasn't found before hitting TO.
5333 This function may only use code that doesn't eval because it is
5334 called asynchronously from note_mouse_highlight. */
5336 static ptrdiff_t
5337 string_buffer_position_lim (Lisp_Object string,
5338 ptrdiff_t from, ptrdiff_t to, int back_p)
5340 Lisp_Object limit, prop, pos;
5341 int found = 0;
5343 pos = make_number (max (from, BEGV));
5345 if (!back_p) /* looking forward */
5347 limit = make_number (min (to, ZV));
5348 while (!found && !EQ (pos, limit))
5350 prop = Fget_char_property (pos, Qdisplay, Qnil);
5351 if (!NILP (prop) && display_prop_string_p (prop, string))
5352 found = 1;
5353 else
5354 pos = Fnext_single_char_property_change (pos, Qdisplay, Qnil,
5355 limit);
5358 else /* looking back */
5360 limit = make_number (max (to, BEGV));
5361 while (!found && !EQ (pos, limit))
5363 prop = Fget_char_property (pos, Qdisplay, Qnil);
5364 if (!NILP (prop) && display_prop_string_p (prop, string))
5365 found = 1;
5366 else
5367 pos = Fprevious_single_char_property_change (pos, Qdisplay, Qnil,
5368 limit);
5372 return found ? XINT (pos) : 0;
5375 /* Determine which buffer position in current buffer STRING comes from.
5376 AROUND_CHARPOS is an approximate position where it could come from.
5377 Value is the buffer position or 0 if it couldn't be determined.
5379 This function is necessary because we don't record buffer positions
5380 in glyphs generated from strings (to keep struct glyph small).
5381 This function may only use code that doesn't eval because it is
5382 called asynchronously from note_mouse_highlight. */
5384 static ptrdiff_t
5385 string_buffer_position (Lisp_Object string, ptrdiff_t around_charpos)
5387 const int MAX_DISTANCE = 1000;
5388 ptrdiff_t found = string_buffer_position_lim (string, around_charpos,
5389 around_charpos + MAX_DISTANCE,
5392 if (!found)
5393 found = string_buffer_position_lim (string, around_charpos,
5394 around_charpos - MAX_DISTANCE, 1);
5395 return found;
5400 /***********************************************************************
5401 `composition' property
5402 ***********************************************************************/
5404 /* Set up iterator IT from `composition' property at its current
5405 position. Called from handle_stop. */
5407 static enum prop_handled
5408 handle_composition_prop (struct it *it)
5410 Lisp_Object prop, string;
5411 ptrdiff_t pos, pos_byte, start, end;
5413 if (STRINGP (it->string))
5415 unsigned char *s;
5417 pos = IT_STRING_CHARPOS (*it);
5418 pos_byte = IT_STRING_BYTEPOS (*it);
5419 string = it->string;
5420 s = SDATA (string) + pos_byte;
5421 it->c = STRING_CHAR (s);
5423 else
5425 pos = IT_CHARPOS (*it);
5426 pos_byte = IT_BYTEPOS (*it);
5427 string = Qnil;
5428 it->c = FETCH_CHAR (pos_byte);
5431 /* If there's a valid composition and point is not inside of the
5432 composition (in the case that the composition is from the current
5433 buffer), draw a glyph composed from the composition components. */
5434 if (find_composition (pos, -1, &start, &end, &prop, string)
5435 && composition_valid_p (start, end, prop)
5436 && (STRINGP (it->string) || (PT <= start || PT >= end)))
5438 if (start < pos)
5439 /* As we can't handle this situation (perhaps font-lock added
5440 a new composition), we just return here hoping that next
5441 redisplay will detect this composition much earlier. */
5442 return HANDLED_NORMALLY;
5443 if (start != pos)
5445 if (STRINGP (it->string))
5446 pos_byte = string_char_to_byte (it->string, start);
5447 else
5448 pos_byte = CHAR_TO_BYTE (start);
5450 it->cmp_it.id = get_composition_id (start, pos_byte, end - start,
5451 prop, string);
5453 if (it->cmp_it.id >= 0)
5455 it->cmp_it.ch = -1;
5456 it->cmp_it.nchars = COMPOSITION_LENGTH (prop);
5457 it->cmp_it.nglyphs = -1;
5461 return HANDLED_NORMALLY;
5466 /***********************************************************************
5467 Overlay strings
5468 ***********************************************************************/
5470 /* The following structure is used to record overlay strings for
5471 later sorting in load_overlay_strings. */
5473 struct overlay_entry
5475 Lisp_Object overlay;
5476 Lisp_Object string;
5477 EMACS_INT priority;
5478 int after_string_p;
5482 /* Set up iterator IT from overlay strings at its current position.
5483 Called from handle_stop. */
5485 static enum prop_handled
5486 handle_overlay_change (struct it *it)
5488 if (!STRINGP (it->string) && get_overlay_strings (it, 0))
5489 return HANDLED_RECOMPUTE_PROPS;
5490 else
5491 return HANDLED_NORMALLY;
5495 /* Set up the next overlay string for delivery by IT, if there is an
5496 overlay string to deliver. Called by set_iterator_to_next when the
5497 end of the current overlay string is reached. If there are more
5498 overlay strings to display, IT->string and
5499 IT->current.overlay_string_index are set appropriately here.
5500 Otherwise IT->string is set to nil. */
5502 static void
5503 next_overlay_string (struct it *it)
5505 ++it->current.overlay_string_index;
5506 if (it->current.overlay_string_index == it->n_overlay_strings)
5508 /* No more overlay strings. Restore IT's settings to what
5509 they were before overlay strings were processed, and
5510 continue to deliver from current_buffer. */
5512 it->ellipsis_p = (it->stack[it->sp - 1].display_ellipsis_p != 0);
5513 pop_it (it);
5514 eassert (it->sp > 0
5515 || (NILP (it->string)
5516 && it->method == GET_FROM_BUFFER
5517 && it->stop_charpos >= BEGV
5518 && it->stop_charpos <= it->end_charpos));
5519 it->current.overlay_string_index = -1;
5520 it->n_overlay_strings = 0;
5521 it->overlay_strings_charpos = -1;
5522 /* If there's an empty display string on the stack, pop the
5523 stack, to resync the bidi iterator with IT's position. Such
5524 empty strings are pushed onto the stack in
5525 get_overlay_strings_1. */
5526 if (it->sp > 0 && STRINGP (it->string) && !SCHARS (it->string))
5527 pop_it (it);
5529 /* If we're at the end of the buffer, record that we have
5530 processed the overlay strings there already, so that
5531 next_element_from_buffer doesn't try it again. */
5532 if (NILP (it->string) && IT_CHARPOS (*it) >= it->end_charpos)
5533 it->overlay_strings_at_end_processed_p = true;
5535 else
5537 /* There are more overlay strings to process. If
5538 IT->current.overlay_string_index has advanced to a position
5539 where we must load IT->overlay_strings with more strings, do
5540 it. We must load at the IT->overlay_strings_charpos where
5541 IT->n_overlay_strings was originally computed; when invisible
5542 text is present, this might not be IT_CHARPOS (Bug#7016). */
5543 int i = it->current.overlay_string_index % OVERLAY_STRING_CHUNK_SIZE;
5545 if (it->current.overlay_string_index && i == 0)
5546 load_overlay_strings (it, it->overlay_strings_charpos);
5548 /* Initialize IT to deliver display elements from the overlay
5549 string. */
5550 it->string = it->overlay_strings[i];
5551 it->multibyte_p = STRING_MULTIBYTE (it->string);
5552 SET_TEXT_POS (it->current.string_pos, 0, 0);
5553 it->method = GET_FROM_STRING;
5554 it->stop_charpos = 0;
5555 it->end_charpos = SCHARS (it->string);
5556 if (it->cmp_it.stop_pos >= 0)
5557 it->cmp_it.stop_pos = 0;
5558 it->prev_stop = 0;
5559 it->base_level_stop = 0;
5561 /* Set up the bidi iterator for this overlay string. */
5562 if (it->bidi_p)
5564 it->bidi_it.string.lstring = it->string;
5565 it->bidi_it.string.s = NULL;
5566 it->bidi_it.string.schars = SCHARS (it->string);
5567 it->bidi_it.string.bufpos = it->overlay_strings_charpos;
5568 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
5569 it->bidi_it.string.unibyte = !it->multibyte_p;
5570 it->bidi_it.w = it->w;
5571 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
5575 CHECK_IT (it);
5579 /* Compare two overlay_entry structures E1 and E2. Used as a
5580 comparison function for qsort in load_overlay_strings. Overlay
5581 strings for the same position are sorted so that
5583 1. All after-strings come in front of before-strings, except
5584 when they come from the same overlay.
5586 2. Within after-strings, strings are sorted so that overlay strings
5587 from overlays with higher priorities come first.
5589 2. Within before-strings, strings are sorted so that overlay
5590 strings from overlays with higher priorities come last.
5592 Value is analogous to strcmp. */
5595 static int
5596 compare_overlay_entries (const void *e1, const void *e2)
5598 struct overlay_entry const *entry1 = e1;
5599 struct overlay_entry const *entry2 = e2;
5600 int result;
5602 if (entry1->after_string_p != entry2->after_string_p)
5604 /* Let after-strings appear in front of before-strings if
5605 they come from different overlays. */
5606 if (EQ (entry1->overlay, entry2->overlay))
5607 result = entry1->after_string_p ? 1 : -1;
5608 else
5609 result = entry1->after_string_p ? -1 : 1;
5611 else if (entry1->priority != entry2->priority)
5613 if (entry1->after_string_p)
5614 /* After-strings sorted in order of decreasing priority. */
5615 result = entry2->priority < entry1->priority ? -1 : 1;
5616 else
5617 /* Before-strings sorted in order of increasing priority. */
5618 result = entry1->priority < entry2->priority ? -1 : 1;
5620 else
5621 result = 0;
5623 return result;
5627 /* Load the vector IT->overlay_strings with overlay strings from IT's
5628 current buffer position, or from CHARPOS if that is > 0. Set
5629 IT->n_overlays to the total number of overlay strings found.
5631 Overlay strings are processed OVERLAY_STRING_CHUNK_SIZE strings at
5632 a time. On entry into load_overlay_strings,
5633 IT->current.overlay_string_index gives the number of overlay
5634 strings that have already been loaded by previous calls to this
5635 function.
5637 IT->add_overlay_start contains an additional overlay start
5638 position to consider for taking overlay strings from, if non-zero.
5639 This position comes into play when the overlay has an `invisible'
5640 property, and both before and after-strings. When we've skipped to
5641 the end of the overlay, because of its `invisible' property, we
5642 nevertheless want its before-string to appear.
5643 IT->add_overlay_start will contain the overlay start position
5644 in this case.
5646 Overlay strings are sorted so that after-string strings come in
5647 front of before-string strings. Within before and after-strings,
5648 strings are sorted by overlay priority. See also function
5649 compare_overlay_entries. */
5651 static void
5652 load_overlay_strings (struct it *it, ptrdiff_t charpos)
5654 Lisp_Object overlay, window, str, invisible;
5655 struct Lisp_Overlay *ov;
5656 ptrdiff_t start, end;
5657 ptrdiff_t n = 0, i, j;
5658 int invis_p;
5659 struct overlay_entry entriesbuf[20];
5660 ptrdiff_t size = ARRAYELTS (entriesbuf);
5661 struct overlay_entry *entries = entriesbuf;
5662 USE_SAFE_ALLOCA;
5664 if (charpos <= 0)
5665 charpos = IT_CHARPOS (*it);
5667 /* Append the overlay string STRING of overlay OVERLAY to vector
5668 `entries' which has size `size' and currently contains `n'
5669 elements. AFTER_P non-zero means STRING is an after-string of
5670 OVERLAY. */
5671 #define RECORD_OVERLAY_STRING(OVERLAY, STRING, AFTER_P) \
5672 do \
5674 Lisp_Object priority; \
5676 if (n == size) \
5678 struct overlay_entry *old = entries; \
5679 SAFE_NALLOCA (entries, 2, size); \
5680 memcpy (entries, old, size * sizeof *entries); \
5681 size *= 2; \
5684 entries[n].string = (STRING); \
5685 entries[n].overlay = (OVERLAY); \
5686 priority = Foverlay_get ((OVERLAY), Qpriority); \
5687 entries[n].priority = INTEGERP (priority) ? XINT (priority) : 0; \
5688 entries[n].after_string_p = (AFTER_P); \
5689 ++n; \
5691 while (0)
5693 /* Process overlay before the overlay center. */
5694 for (ov = current_buffer->overlays_before; ov; ov = ov->next)
5696 XSETMISC (overlay, ov);
5697 eassert (OVERLAYP (overlay));
5698 start = OVERLAY_POSITION (OVERLAY_START (overlay));
5699 end = OVERLAY_POSITION (OVERLAY_END (overlay));
5701 if (end < charpos)
5702 break;
5704 /* Skip this overlay if it doesn't start or end at IT's current
5705 position. */
5706 if (end != charpos && start != charpos)
5707 continue;
5709 /* Skip this overlay if it doesn't apply to IT->w. */
5710 window = Foverlay_get (overlay, Qwindow);
5711 if (WINDOWP (window) && XWINDOW (window) != it->w)
5712 continue;
5714 /* If the text ``under'' the overlay is invisible, both before-
5715 and after-strings from this overlay are visible; start and
5716 end position are indistinguishable. */
5717 invisible = Foverlay_get (overlay, Qinvisible);
5718 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
5720 /* If overlay has a non-empty before-string, record it. */
5721 if ((start == charpos || (end == charpos && invis_p))
5722 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
5723 && SCHARS (str))
5724 RECORD_OVERLAY_STRING (overlay, str, 0);
5726 /* If overlay has a non-empty after-string, record it. */
5727 if ((end == charpos || (start == charpos && invis_p))
5728 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
5729 && SCHARS (str))
5730 RECORD_OVERLAY_STRING (overlay, str, 1);
5733 /* Process overlays after the overlay center. */
5734 for (ov = current_buffer->overlays_after; ov; ov = ov->next)
5736 XSETMISC (overlay, ov);
5737 eassert (OVERLAYP (overlay));
5738 start = OVERLAY_POSITION (OVERLAY_START (overlay));
5739 end = OVERLAY_POSITION (OVERLAY_END (overlay));
5741 if (start > charpos)
5742 break;
5744 /* Skip this overlay if it doesn't start or end at IT's current
5745 position. */
5746 if (end != charpos && start != charpos)
5747 continue;
5749 /* Skip this overlay if it doesn't apply to IT->w. */
5750 window = Foverlay_get (overlay, Qwindow);
5751 if (WINDOWP (window) && XWINDOW (window) != it->w)
5752 continue;
5754 /* If the text ``under'' the overlay is invisible, it has a zero
5755 dimension, and both before- and after-strings apply. */
5756 invisible = Foverlay_get (overlay, Qinvisible);
5757 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
5759 /* If overlay has a non-empty before-string, record it. */
5760 if ((start == charpos || (end == charpos && invis_p))
5761 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
5762 && SCHARS (str))
5763 RECORD_OVERLAY_STRING (overlay, str, 0);
5765 /* If overlay has a non-empty after-string, record it. */
5766 if ((end == charpos || (start == charpos && invis_p))
5767 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
5768 && SCHARS (str))
5769 RECORD_OVERLAY_STRING (overlay, str, 1);
5772 #undef RECORD_OVERLAY_STRING
5774 /* Sort entries. */
5775 if (n > 1)
5776 qsort (entries, n, sizeof *entries, compare_overlay_entries);
5778 /* Record number of overlay strings, and where we computed it. */
5779 it->n_overlay_strings = n;
5780 it->overlay_strings_charpos = charpos;
5782 /* IT->current.overlay_string_index is the number of overlay strings
5783 that have already been consumed by IT. Copy some of the
5784 remaining overlay strings to IT->overlay_strings. */
5785 i = 0;
5786 j = it->current.overlay_string_index;
5787 while (i < OVERLAY_STRING_CHUNK_SIZE && j < n)
5789 it->overlay_strings[i] = entries[j].string;
5790 it->string_overlays[i++] = entries[j++].overlay;
5793 CHECK_IT (it);
5794 SAFE_FREE ();
5798 /* Get the first chunk of overlay strings at IT's current buffer
5799 position, or at CHARPOS if that is > 0. Value is non-zero if at
5800 least one overlay string was found. */
5802 static int
5803 get_overlay_strings_1 (struct it *it, ptrdiff_t charpos, int compute_stop_p)
5805 /* Get the first OVERLAY_STRING_CHUNK_SIZE overlay strings to
5806 process. This fills IT->overlay_strings with strings, and sets
5807 IT->n_overlay_strings to the total number of strings to process.
5808 IT->pos.overlay_string_index has to be set temporarily to zero
5809 because load_overlay_strings needs this; it must be set to -1
5810 when no overlay strings are found because a zero value would
5811 indicate a position in the first overlay string. */
5812 it->current.overlay_string_index = 0;
5813 load_overlay_strings (it, charpos);
5815 /* If we found overlay strings, set up IT to deliver display
5816 elements from the first one. Otherwise set up IT to deliver
5817 from current_buffer. */
5818 if (it->n_overlay_strings)
5820 /* Make sure we know settings in current_buffer, so that we can
5821 restore meaningful values when we're done with the overlay
5822 strings. */
5823 if (compute_stop_p)
5824 compute_stop_pos (it);
5825 eassert (it->face_id >= 0);
5827 /* Save IT's settings. They are restored after all overlay
5828 strings have been processed. */
5829 eassert (!compute_stop_p || it->sp == 0);
5831 /* When called from handle_stop, there might be an empty display
5832 string loaded. In that case, don't bother saving it. But
5833 don't use this optimization with the bidi iterator, since we
5834 need the corresponding pop_it call to resync the bidi
5835 iterator's position with IT's position, after we are done
5836 with the overlay strings. (The corresponding call to pop_it
5837 in case of an empty display string is in
5838 next_overlay_string.) */
5839 if (!(!it->bidi_p
5840 && STRINGP (it->string) && !SCHARS (it->string)))
5841 push_it (it, NULL);
5843 /* Set up IT to deliver display elements from the first overlay
5844 string. */
5845 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
5846 it->string = it->overlay_strings[0];
5847 it->from_overlay = Qnil;
5848 it->stop_charpos = 0;
5849 eassert (STRINGP (it->string));
5850 it->end_charpos = SCHARS (it->string);
5851 it->prev_stop = 0;
5852 it->base_level_stop = 0;
5853 it->multibyte_p = STRING_MULTIBYTE (it->string);
5854 it->method = GET_FROM_STRING;
5855 it->from_disp_prop_p = 0;
5857 /* Force paragraph direction to be that of the parent
5858 buffer. */
5859 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
5860 it->paragraph_embedding = it->bidi_it.paragraph_dir;
5861 else
5862 it->paragraph_embedding = L2R;
5864 /* Set up the bidi iterator for this overlay string. */
5865 if (it->bidi_p)
5867 ptrdiff_t pos = (charpos > 0 ? charpos : IT_CHARPOS (*it));
5869 it->bidi_it.string.lstring = it->string;
5870 it->bidi_it.string.s = NULL;
5871 it->bidi_it.string.schars = SCHARS (it->string);
5872 it->bidi_it.string.bufpos = pos;
5873 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
5874 it->bidi_it.string.unibyte = !it->multibyte_p;
5875 it->bidi_it.w = it->w;
5876 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
5878 return 1;
5881 it->current.overlay_string_index = -1;
5882 return 0;
5885 static int
5886 get_overlay_strings (struct it *it, ptrdiff_t charpos)
5888 it->string = Qnil;
5889 it->method = GET_FROM_BUFFER;
5891 (void) get_overlay_strings_1 (it, charpos, 1);
5893 CHECK_IT (it);
5895 /* Value is non-zero if we found at least one overlay string. */
5896 return STRINGP (it->string);
5901 /***********************************************************************
5902 Saving and restoring state
5903 ***********************************************************************/
5905 /* Save current settings of IT on IT->stack. Called, for example,
5906 before setting up IT for an overlay string, to be able to restore
5907 IT's settings to what they were after the overlay string has been
5908 processed. If POSITION is non-NULL, it is the position to save on
5909 the stack instead of IT->position. */
5911 static void
5912 push_it (struct it *it, struct text_pos *position)
5914 struct iterator_stack_entry *p;
5916 eassert (it->sp < IT_STACK_SIZE);
5917 p = it->stack + it->sp;
5919 p->stop_charpos = it->stop_charpos;
5920 p->prev_stop = it->prev_stop;
5921 p->base_level_stop = it->base_level_stop;
5922 p->cmp_it = it->cmp_it;
5923 eassert (it->face_id >= 0);
5924 p->face_id = it->face_id;
5925 p->string = it->string;
5926 p->method = it->method;
5927 p->from_overlay = it->from_overlay;
5928 switch (p->method)
5930 case GET_FROM_IMAGE:
5931 p->u.image.object = it->object;
5932 p->u.image.image_id = it->image_id;
5933 p->u.image.slice = it->slice;
5934 break;
5935 case GET_FROM_STRETCH:
5936 p->u.stretch.object = it->object;
5937 break;
5939 p->position = position ? *position : it->position;
5940 p->current = it->current;
5941 p->end_charpos = it->end_charpos;
5942 p->string_nchars = it->string_nchars;
5943 p->area = it->area;
5944 p->multibyte_p = it->multibyte_p;
5945 p->avoid_cursor_p = it->avoid_cursor_p;
5946 p->space_width = it->space_width;
5947 p->font_height = it->font_height;
5948 p->voffset = it->voffset;
5949 p->string_from_display_prop_p = it->string_from_display_prop_p;
5950 p->string_from_prefix_prop_p = it->string_from_prefix_prop_p;
5951 p->display_ellipsis_p = 0;
5952 p->line_wrap = it->line_wrap;
5953 p->bidi_p = it->bidi_p;
5954 p->paragraph_embedding = it->paragraph_embedding;
5955 p->from_disp_prop_p = it->from_disp_prop_p;
5956 ++it->sp;
5958 /* Save the state of the bidi iterator as well. */
5959 if (it->bidi_p)
5960 bidi_push_it (&it->bidi_it);
5963 static void
5964 iterate_out_of_display_property (struct it *it)
5966 int buffer_p = !STRINGP (it->string);
5967 ptrdiff_t eob = (buffer_p ? ZV : it->end_charpos);
5968 ptrdiff_t bob = (buffer_p ? BEGV : 0);
5970 eassert (eob >= CHARPOS (it->position) && CHARPOS (it->position) >= bob);
5972 /* Maybe initialize paragraph direction. If we are at the beginning
5973 of a new paragraph, next_element_from_buffer may not have a
5974 chance to do that. */
5975 if (it->bidi_it.first_elt && it->bidi_it.charpos < eob)
5976 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
5977 /* prev_stop can be zero, so check against BEGV as well. */
5978 while (it->bidi_it.charpos >= bob
5979 && it->prev_stop <= it->bidi_it.charpos
5980 && it->bidi_it.charpos < CHARPOS (it->position)
5981 && it->bidi_it.charpos < eob)
5982 bidi_move_to_visually_next (&it->bidi_it);
5983 /* Record the stop_pos we just crossed, for when we cross it
5984 back, maybe. */
5985 if (it->bidi_it.charpos > CHARPOS (it->position))
5986 it->prev_stop = CHARPOS (it->position);
5987 /* If we ended up not where pop_it put us, resync IT's
5988 positional members with the bidi iterator. */
5989 if (it->bidi_it.charpos != CHARPOS (it->position))
5990 SET_TEXT_POS (it->position, it->bidi_it.charpos, it->bidi_it.bytepos);
5991 if (buffer_p)
5992 it->current.pos = it->position;
5993 else
5994 it->current.string_pos = it->position;
5997 /* Restore IT's settings from IT->stack. Called, for example, when no
5998 more overlay strings must be processed, and we return to delivering
5999 display elements from a buffer, or when the end of a string from a
6000 `display' property is reached and we return to delivering display
6001 elements from an overlay string, or from a buffer. */
6003 static void
6004 pop_it (struct it *it)
6006 struct iterator_stack_entry *p;
6007 int from_display_prop = it->from_disp_prop_p;
6009 eassert (it->sp > 0);
6010 --it->sp;
6011 p = it->stack + it->sp;
6012 it->stop_charpos = p->stop_charpos;
6013 it->prev_stop = p->prev_stop;
6014 it->base_level_stop = p->base_level_stop;
6015 it->cmp_it = p->cmp_it;
6016 it->face_id = p->face_id;
6017 it->current = p->current;
6018 it->position = p->position;
6019 it->string = p->string;
6020 it->from_overlay = p->from_overlay;
6021 if (NILP (it->string))
6022 SET_TEXT_POS (it->current.string_pos, -1, -1);
6023 it->method = p->method;
6024 switch (it->method)
6026 case GET_FROM_IMAGE:
6027 it->image_id = p->u.image.image_id;
6028 it->object = p->u.image.object;
6029 it->slice = p->u.image.slice;
6030 break;
6031 case GET_FROM_STRETCH:
6032 it->object = p->u.stretch.object;
6033 break;
6034 case GET_FROM_BUFFER:
6035 it->object = it->w->contents;
6036 break;
6037 case GET_FROM_STRING:
6039 struct face *face = FACE_FROM_ID (it->f, it->face_id);
6041 /* Restore the face_box_p flag, since it could have been
6042 overwritten by the face of the object that we just finished
6043 displaying. */
6044 if (face)
6045 it->face_box_p = face->box != FACE_NO_BOX;
6046 it->object = it->string;
6048 break;
6049 case GET_FROM_DISPLAY_VECTOR:
6050 if (it->s)
6051 it->method = GET_FROM_C_STRING;
6052 else if (STRINGP (it->string))
6053 it->method = GET_FROM_STRING;
6054 else
6056 it->method = GET_FROM_BUFFER;
6057 it->object = it->w->contents;
6060 it->end_charpos = p->end_charpos;
6061 it->string_nchars = p->string_nchars;
6062 it->area = p->area;
6063 it->multibyte_p = p->multibyte_p;
6064 it->avoid_cursor_p = p->avoid_cursor_p;
6065 it->space_width = p->space_width;
6066 it->font_height = p->font_height;
6067 it->voffset = p->voffset;
6068 it->string_from_display_prop_p = p->string_from_display_prop_p;
6069 it->string_from_prefix_prop_p = p->string_from_prefix_prop_p;
6070 it->line_wrap = p->line_wrap;
6071 it->bidi_p = p->bidi_p;
6072 it->paragraph_embedding = p->paragraph_embedding;
6073 it->from_disp_prop_p = p->from_disp_prop_p;
6074 if (it->bidi_p)
6076 bidi_pop_it (&it->bidi_it);
6077 /* Bidi-iterate until we get out of the portion of text, if any,
6078 covered by a `display' text property or by an overlay with
6079 `display' property. (We cannot just jump there, because the
6080 internal coherency of the bidi iterator state can not be
6081 preserved across such jumps.) We also must determine the
6082 paragraph base direction if the overlay we just processed is
6083 at the beginning of a new paragraph. */
6084 if (from_display_prop
6085 && (it->method == GET_FROM_BUFFER || it->method == GET_FROM_STRING))
6086 iterate_out_of_display_property (it);
6088 eassert ((BUFFERP (it->object)
6089 && IT_CHARPOS (*it) == it->bidi_it.charpos
6090 && IT_BYTEPOS (*it) == it->bidi_it.bytepos)
6091 || (STRINGP (it->object)
6092 && IT_STRING_CHARPOS (*it) == it->bidi_it.charpos
6093 && IT_STRING_BYTEPOS (*it) == it->bidi_it.bytepos)
6094 || (CONSP (it->object) && it->method == GET_FROM_STRETCH));
6100 /***********************************************************************
6101 Moving over lines
6102 ***********************************************************************/
6104 /* Set IT's current position to the previous line start. */
6106 static void
6107 back_to_previous_line_start (struct it *it)
6109 ptrdiff_t cp = IT_CHARPOS (*it), bp = IT_BYTEPOS (*it);
6111 DEC_BOTH (cp, bp);
6112 IT_CHARPOS (*it) = find_newline_no_quit (cp, bp, -1, &IT_BYTEPOS (*it));
6116 /* Move IT to the next line start.
6118 Value is non-zero if a newline was found. Set *SKIPPED_P to 1 if
6119 we skipped over part of the text (as opposed to moving the iterator
6120 continuously over the text). Otherwise, don't change the value
6121 of *SKIPPED_P.
6123 If BIDI_IT_PREV is non-NULL, store into it the state of the bidi
6124 iterator on the newline, if it was found.
6126 Newlines may come from buffer text, overlay strings, or strings
6127 displayed via the `display' property. That's the reason we can't
6128 simply use find_newline_no_quit.
6130 Note that this function may not skip over invisible text that is so
6131 because of text properties and immediately follows a newline. If
6132 it would, function reseat_at_next_visible_line_start, when called
6133 from set_iterator_to_next, would effectively make invisible
6134 characters following a newline part of the wrong glyph row, which
6135 leads to wrong cursor motion. */
6137 static int
6138 forward_to_next_line_start (struct it *it, int *skipped_p,
6139 struct bidi_it *bidi_it_prev)
6141 ptrdiff_t old_selective;
6142 int newline_found_p, n;
6143 const int MAX_NEWLINE_DISTANCE = 500;
6145 /* If already on a newline, just consume it to avoid unintended
6146 skipping over invisible text below. */
6147 if (it->what == IT_CHARACTER
6148 && it->c == '\n'
6149 && CHARPOS (it->position) == IT_CHARPOS (*it))
6151 if (it->bidi_p && bidi_it_prev)
6152 *bidi_it_prev = it->bidi_it;
6153 set_iterator_to_next (it, 0);
6154 it->c = 0;
6155 return 1;
6158 /* Don't handle selective display in the following. It's (a)
6159 unnecessary because it's done by the caller, and (b) leads to an
6160 infinite recursion because next_element_from_ellipsis indirectly
6161 calls this function. */
6162 old_selective = it->selective;
6163 it->selective = 0;
6165 /* Scan for a newline within MAX_NEWLINE_DISTANCE display elements
6166 from buffer text. */
6167 for (n = newline_found_p = 0;
6168 !newline_found_p && n < MAX_NEWLINE_DISTANCE;
6169 n += STRINGP (it->string) ? 0 : 1)
6171 if (!get_next_display_element (it))
6172 return 0;
6173 newline_found_p = it->what == IT_CHARACTER && it->c == '\n';
6174 if (newline_found_p && it->bidi_p && bidi_it_prev)
6175 *bidi_it_prev = it->bidi_it;
6176 set_iterator_to_next (it, 0);
6179 /* If we didn't find a newline near enough, see if we can use a
6180 short-cut. */
6181 if (!newline_found_p)
6183 ptrdiff_t bytepos, start = IT_CHARPOS (*it);
6184 ptrdiff_t limit = find_newline_no_quit (start, IT_BYTEPOS (*it),
6185 1, &bytepos);
6186 Lisp_Object pos;
6188 eassert (!STRINGP (it->string));
6190 /* If there isn't any `display' property in sight, and no
6191 overlays, we can just use the position of the newline in
6192 buffer text. */
6193 if (it->stop_charpos >= limit
6194 || ((pos = Fnext_single_property_change (make_number (start),
6195 Qdisplay, Qnil,
6196 make_number (limit)),
6197 NILP (pos))
6198 && next_overlay_change (start) == ZV))
6200 if (!it->bidi_p)
6202 IT_CHARPOS (*it) = limit;
6203 IT_BYTEPOS (*it) = bytepos;
6205 else
6207 struct bidi_it bprev;
6209 /* Help bidi.c avoid expensive searches for display
6210 properties and overlays, by telling it that there are
6211 none up to `limit'. */
6212 if (it->bidi_it.disp_pos < limit)
6214 it->bidi_it.disp_pos = limit;
6215 it->bidi_it.disp_prop = 0;
6217 do {
6218 bprev = it->bidi_it;
6219 bidi_move_to_visually_next (&it->bidi_it);
6220 } while (it->bidi_it.charpos != limit);
6221 IT_CHARPOS (*it) = limit;
6222 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6223 if (bidi_it_prev)
6224 *bidi_it_prev = bprev;
6226 *skipped_p = newline_found_p = true;
6228 else
6230 while (get_next_display_element (it)
6231 && !newline_found_p)
6233 newline_found_p = ITERATOR_AT_END_OF_LINE_P (it);
6234 if (newline_found_p && it->bidi_p && bidi_it_prev)
6235 *bidi_it_prev = it->bidi_it;
6236 set_iterator_to_next (it, 0);
6241 it->selective = old_selective;
6242 return newline_found_p;
6246 /* Set IT's current position to the previous visible line start. Skip
6247 invisible text that is so either due to text properties or due to
6248 selective display. Caution: this does not change IT->current_x and
6249 IT->hpos. */
6251 static void
6252 back_to_previous_visible_line_start (struct it *it)
6254 while (IT_CHARPOS (*it) > BEGV)
6256 back_to_previous_line_start (it);
6258 if (IT_CHARPOS (*it) <= BEGV)
6259 break;
6261 /* If selective > 0, then lines indented more than its value are
6262 invisible. */
6263 if (it->selective > 0
6264 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
6265 it->selective))
6266 continue;
6268 /* Check the newline before point for invisibility. */
6270 Lisp_Object prop;
6271 prop = Fget_char_property (make_number (IT_CHARPOS (*it) - 1),
6272 Qinvisible, it->window);
6273 if (TEXT_PROP_MEANS_INVISIBLE (prop))
6274 continue;
6277 if (IT_CHARPOS (*it) <= BEGV)
6278 break;
6281 struct it it2;
6282 void *it2data = NULL;
6283 ptrdiff_t pos;
6284 ptrdiff_t beg, end;
6285 Lisp_Object val, overlay;
6287 SAVE_IT (it2, *it, it2data);
6289 /* If newline is part of a composition, continue from start of composition */
6290 if (find_composition (IT_CHARPOS (*it), -1, &beg, &end, &val, Qnil)
6291 && beg < IT_CHARPOS (*it))
6292 goto replaced;
6294 /* If newline is replaced by a display property, find start of overlay
6295 or interval and continue search from that point. */
6296 pos = --IT_CHARPOS (it2);
6297 --IT_BYTEPOS (it2);
6298 it2.sp = 0;
6299 bidi_unshelve_cache (NULL, 0);
6300 it2.string_from_display_prop_p = 0;
6301 it2.from_disp_prop_p = 0;
6302 if (handle_display_prop (&it2) == HANDLED_RETURN
6303 && !NILP (val = get_char_property_and_overlay
6304 (make_number (pos), Qdisplay, Qnil, &overlay))
6305 && (OVERLAYP (overlay)
6306 ? (beg = OVERLAY_POSITION (OVERLAY_START (overlay)))
6307 : get_property_and_range (pos, Qdisplay, &val, &beg, &end, Qnil)))
6309 RESTORE_IT (it, it, it2data);
6310 goto replaced;
6313 /* Newline is not replaced by anything -- so we are done. */
6314 RESTORE_IT (it, it, it2data);
6315 break;
6317 replaced:
6318 if (beg < BEGV)
6319 beg = BEGV;
6320 IT_CHARPOS (*it) = beg;
6321 IT_BYTEPOS (*it) = buf_charpos_to_bytepos (current_buffer, beg);
6325 it->continuation_lines_width = 0;
6327 eassert (IT_CHARPOS (*it) >= BEGV);
6328 eassert (IT_CHARPOS (*it) == BEGV
6329 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
6330 CHECK_IT (it);
6334 /* Reseat iterator IT at the previous visible line start. Skip
6335 invisible text that is so either due to text properties or due to
6336 selective display. At the end, update IT's overlay information,
6337 face information etc. */
6339 void
6340 reseat_at_previous_visible_line_start (struct it *it)
6342 back_to_previous_visible_line_start (it);
6343 reseat (it, it->current.pos, 1);
6344 CHECK_IT (it);
6348 /* Reseat iterator IT on the next visible line start in the current
6349 buffer. ON_NEWLINE_P non-zero means position IT on the newline
6350 preceding the line start. Skip over invisible text that is so
6351 because of selective display. Compute faces, overlays etc at the
6352 new position. Note that this function does not skip over text that
6353 is invisible because of text properties. */
6355 static void
6356 reseat_at_next_visible_line_start (struct it *it, int on_newline_p)
6358 int newline_found_p, skipped_p = 0;
6359 struct bidi_it bidi_it_prev;
6361 newline_found_p = forward_to_next_line_start (it, &skipped_p, &bidi_it_prev);
6363 /* Skip over lines that are invisible because they are indented
6364 more than the value of IT->selective. */
6365 if (it->selective > 0)
6366 while (IT_CHARPOS (*it) < ZV
6367 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
6368 it->selective))
6370 eassert (IT_BYTEPOS (*it) == BEGV
6371 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
6372 newline_found_p =
6373 forward_to_next_line_start (it, &skipped_p, &bidi_it_prev);
6376 /* Position on the newline if that's what's requested. */
6377 if (on_newline_p && newline_found_p)
6379 if (STRINGP (it->string))
6381 if (IT_STRING_CHARPOS (*it) > 0)
6383 if (!it->bidi_p)
6385 --IT_STRING_CHARPOS (*it);
6386 --IT_STRING_BYTEPOS (*it);
6388 else
6390 /* We need to restore the bidi iterator to the state
6391 it had on the newline, and resync the IT's
6392 position with that. */
6393 it->bidi_it = bidi_it_prev;
6394 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
6395 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
6399 else if (IT_CHARPOS (*it) > BEGV)
6401 if (!it->bidi_p)
6403 --IT_CHARPOS (*it);
6404 --IT_BYTEPOS (*it);
6406 else
6408 /* We need to restore the bidi iterator to the state it
6409 had on the newline and resync IT with that. */
6410 it->bidi_it = bidi_it_prev;
6411 IT_CHARPOS (*it) = it->bidi_it.charpos;
6412 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6414 reseat (it, it->current.pos, 0);
6417 else if (skipped_p)
6418 reseat (it, it->current.pos, 0);
6420 CHECK_IT (it);
6425 /***********************************************************************
6426 Changing an iterator's position
6427 ***********************************************************************/
6429 /* Change IT's current position to POS in current_buffer. If FORCE_P
6430 is non-zero, always check for text properties at the new position.
6431 Otherwise, text properties are only looked up if POS >=
6432 IT->check_charpos of a property. */
6434 static void
6435 reseat (struct it *it, struct text_pos pos, int force_p)
6437 ptrdiff_t original_pos = IT_CHARPOS (*it);
6439 reseat_1 (it, pos, 0);
6441 /* Determine where to check text properties. Avoid doing it
6442 where possible because text property lookup is very expensive. */
6443 if (force_p
6444 || CHARPOS (pos) > it->stop_charpos
6445 || CHARPOS (pos) < original_pos)
6447 if (it->bidi_p)
6449 /* For bidi iteration, we need to prime prev_stop and
6450 base_level_stop with our best estimations. */
6451 /* Implementation note: Of course, POS is not necessarily a
6452 stop position, so assigning prev_pos to it is a lie; we
6453 should have called compute_stop_backwards. However, if
6454 the current buffer does not include any R2L characters,
6455 that call would be a waste of cycles, because the
6456 iterator will never move back, and thus never cross this
6457 "fake" stop position. So we delay that backward search
6458 until the time we really need it, in next_element_from_buffer. */
6459 if (CHARPOS (pos) != it->prev_stop)
6460 it->prev_stop = CHARPOS (pos);
6461 if (CHARPOS (pos) < it->base_level_stop)
6462 it->base_level_stop = 0; /* meaning it's unknown */
6463 handle_stop (it);
6465 else
6467 handle_stop (it);
6468 it->prev_stop = it->base_level_stop = 0;
6473 CHECK_IT (it);
6477 /* Change IT's buffer position to POS. SET_STOP_P non-zero means set
6478 IT->stop_pos to POS, also. */
6480 static void
6481 reseat_1 (struct it *it, struct text_pos pos, int set_stop_p)
6483 /* Don't call this function when scanning a C string. */
6484 eassert (it->s == NULL);
6486 /* POS must be a reasonable value. */
6487 eassert (CHARPOS (pos) >= BEGV && CHARPOS (pos) <= ZV);
6489 it->current.pos = it->position = pos;
6490 it->end_charpos = ZV;
6491 it->dpvec = NULL;
6492 it->current.dpvec_index = -1;
6493 it->current.overlay_string_index = -1;
6494 IT_STRING_CHARPOS (*it) = -1;
6495 IT_STRING_BYTEPOS (*it) = -1;
6496 it->string = Qnil;
6497 it->method = GET_FROM_BUFFER;
6498 it->object = it->w->contents;
6499 it->area = TEXT_AREA;
6500 it->multibyte_p = !NILP (BVAR (current_buffer, enable_multibyte_characters));
6501 it->sp = 0;
6502 it->string_from_display_prop_p = 0;
6503 it->string_from_prefix_prop_p = 0;
6505 it->from_disp_prop_p = 0;
6506 it->face_before_selective_p = 0;
6507 if (it->bidi_p)
6509 bidi_init_it (IT_CHARPOS (*it), IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
6510 &it->bidi_it);
6511 bidi_unshelve_cache (NULL, 0);
6512 it->bidi_it.paragraph_dir = NEUTRAL_DIR;
6513 it->bidi_it.string.s = NULL;
6514 it->bidi_it.string.lstring = Qnil;
6515 it->bidi_it.string.bufpos = 0;
6516 it->bidi_it.string.from_disp_str = 0;
6517 it->bidi_it.string.unibyte = 0;
6518 it->bidi_it.w = it->w;
6521 if (set_stop_p)
6523 it->stop_charpos = CHARPOS (pos);
6524 it->base_level_stop = CHARPOS (pos);
6526 /* This make the information stored in it->cmp_it invalidate. */
6527 it->cmp_it.id = -1;
6531 /* Set up IT for displaying a string, starting at CHARPOS in window W.
6532 If S is non-null, it is a C string to iterate over. Otherwise,
6533 STRING gives a Lisp string to iterate over.
6535 If PRECISION > 0, don't return more then PRECISION number of
6536 characters from the string.
6538 If FIELD_WIDTH > 0, return padding spaces until FIELD_WIDTH
6539 characters have been returned. FIELD_WIDTH < 0 means an infinite
6540 field width.
6542 MULTIBYTE = 0 means disable processing of multibyte characters,
6543 MULTIBYTE > 0 means enable it,
6544 MULTIBYTE < 0 means use IT->multibyte_p.
6546 IT must be initialized via a prior call to init_iterator before
6547 calling this function. */
6549 static void
6550 reseat_to_string (struct it *it, const char *s, Lisp_Object string,
6551 ptrdiff_t charpos, ptrdiff_t precision, int field_width,
6552 int multibyte)
6554 /* No text property checks performed by default, but see below. */
6555 it->stop_charpos = -1;
6557 /* Set iterator position and end position. */
6558 memset (&it->current, 0, sizeof it->current);
6559 it->current.overlay_string_index = -1;
6560 it->current.dpvec_index = -1;
6561 eassert (charpos >= 0);
6563 /* If STRING is specified, use its multibyteness, otherwise use the
6564 setting of MULTIBYTE, if specified. */
6565 if (multibyte >= 0)
6566 it->multibyte_p = multibyte > 0;
6568 /* Bidirectional reordering of strings is controlled by the default
6569 value of bidi-display-reordering. Don't try to reorder while
6570 loading loadup.el, as the necessary character property tables are
6571 not yet available. */
6572 it->bidi_p =
6573 NILP (Vpurify_flag)
6574 && !NILP (BVAR (&buffer_defaults, bidi_display_reordering));
6576 if (s == NULL)
6578 eassert (STRINGP (string));
6579 it->string = string;
6580 it->s = NULL;
6581 it->end_charpos = it->string_nchars = SCHARS (string);
6582 it->method = GET_FROM_STRING;
6583 it->current.string_pos = string_pos (charpos, string);
6585 if (it->bidi_p)
6587 it->bidi_it.string.lstring = string;
6588 it->bidi_it.string.s = NULL;
6589 it->bidi_it.string.schars = it->end_charpos;
6590 it->bidi_it.string.bufpos = 0;
6591 it->bidi_it.string.from_disp_str = 0;
6592 it->bidi_it.string.unibyte = !it->multibyte_p;
6593 it->bidi_it.w = it->w;
6594 bidi_init_it (charpos, IT_STRING_BYTEPOS (*it),
6595 FRAME_WINDOW_P (it->f), &it->bidi_it);
6598 else
6600 it->s = (const unsigned char *) s;
6601 it->string = Qnil;
6603 /* Note that we use IT->current.pos, not it->current.string_pos,
6604 for displaying C strings. */
6605 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
6606 if (it->multibyte_p)
6608 it->current.pos = c_string_pos (charpos, s, 1);
6609 it->end_charpos = it->string_nchars = number_of_chars (s, 1);
6611 else
6613 IT_CHARPOS (*it) = IT_BYTEPOS (*it) = charpos;
6614 it->end_charpos = it->string_nchars = strlen (s);
6617 if (it->bidi_p)
6619 it->bidi_it.string.lstring = Qnil;
6620 it->bidi_it.string.s = (const unsigned char *) s;
6621 it->bidi_it.string.schars = it->end_charpos;
6622 it->bidi_it.string.bufpos = 0;
6623 it->bidi_it.string.from_disp_str = 0;
6624 it->bidi_it.string.unibyte = !it->multibyte_p;
6625 it->bidi_it.w = it->w;
6626 bidi_init_it (charpos, IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
6627 &it->bidi_it);
6629 it->method = GET_FROM_C_STRING;
6632 /* PRECISION > 0 means don't return more than PRECISION characters
6633 from the string. */
6634 if (precision > 0 && it->end_charpos - charpos > precision)
6636 it->end_charpos = it->string_nchars = charpos + precision;
6637 if (it->bidi_p)
6638 it->bidi_it.string.schars = it->end_charpos;
6641 /* FIELD_WIDTH > 0 means pad with spaces until FIELD_WIDTH
6642 characters have been returned. FIELD_WIDTH == 0 means don't pad,
6643 FIELD_WIDTH < 0 means infinite field width. This is useful for
6644 padding with `-' at the end of a mode line. */
6645 if (field_width < 0)
6646 field_width = INFINITY;
6647 /* Implementation note: We deliberately don't enlarge
6648 it->bidi_it.string.schars here to fit it->end_charpos, because
6649 the bidi iterator cannot produce characters out of thin air. */
6650 if (field_width > it->end_charpos - charpos)
6651 it->end_charpos = charpos + field_width;
6653 /* Use the standard display table for displaying strings. */
6654 if (DISP_TABLE_P (Vstandard_display_table))
6655 it->dp = XCHAR_TABLE (Vstandard_display_table);
6657 it->stop_charpos = charpos;
6658 it->prev_stop = charpos;
6659 it->base_level_stop = 0;
6660 if (it->bidi_p)
6662 it->bidi_it.first_elt = 1;
6663 it->bidi_it.paragraph_dir = NEUTRAL_DIR;
6664 it->bidi_it.disp_pos = -1;
6666 if (s == NULL && it->multibyte_p)
6668 ptrdiff_t endpos = SCHARS (it->string);
6669 if (endpos > it->end_charpos)
6670 endpos = it->end_charpos;
6671 composition_compute_stop_pos (&it->cmp_it, charpos, -1, endpos,
6672 it->string);
6674 CHECK_IT (it);
6679 /***********************************************************************
6680 Iteration
6681 ***********************************************************************/
6683 /* Map enum it_method value to corresponding next_element_from_* function. */
6685 static int (* get_next_element[NUM_IT_METHODS]) (struct it *it) =
6687 next_element_from_buffer,
6688 next_element_from_display_vector,
6689 next_element_from_string,
6690 next_element_from_c_string,
6691 next_element_from_image,
6692 next_element_from_stretch
6695 #define GET_NEXT_DISPLAY_ELEMENT(it) (*get_next_element[(it)->method]) (it)
6698 /* Return 1 iff a character at CHARPOS (and BYTEPOS) is composed
6699 (possibly with the following characters). */
6701 #define CHAR_COMPOSED_P(IT,CHARPOS,BYTEPOS,END_CHARPOS) \
6702 ((IT)->cmp_it.id >= 0 \
6703 || ((IT)->cmp_it.stop_pos == (CHARPOS) \
6704 && composition_reseat_it (&(IT)->cmp_it, CHARPOS, BYTEPOS, \
6705 END_CHARPOS, (IT)->w, \
6706 FACE_FROM_ID ((IT)->f, (IT)->face_id), \
6707 (IT)->string)))
6710 /* Lookup the char-table Vglyphless_char_display for character C (-1
6711 if we want information for no-font case), and return the display
6712 method symbol. By side-effect, update it->what and
6713 it->glyphless_method. This function is called from
6714 get_next_display_element for each character element, and from
6715 x_produce_glyphs when no suitable font was found. */
6717 Lisp_Object
6718 lookup_glyphless_char_display (int c, struct it *it)
6720 Lisp_Object glyphless_method = Qnil;
6722 if (CHAR_TABLE_P (Vglyphless_char_display)
6723 && CHAR_TABLE_EXTRA_SLOTS (XCHAR_TABLE (Vglyphless_char_display)) >= 1)
6725 if (c >= 0)
6727 glyphless_method = CHAR_TABLE_REF (Vglyphless_char_display, c);
6728 if (CONSP (glyphless_method))
6729 glyphless_method = FRAME_WINDOW_P (it->f)
6730 ? XCAR (glyphless_method)
6731 : XCDR (glyphless_method);
6733 else
6734 glyphless_method = XCHAR_TABLE (Vglyphless_char_display)->extras[0];
6737 retry:
6738 if (NILP (glyphless_method))
6740 if (c >= 0)
6741 /* The default is to display the character by a proper font. */
6742 return Qnil;
6743 /* The default for the no-font case is to display an empty box. */
6744 glyphless_method = Qempty_box;
6746 if (EQ (glyphless_method, Qzero_width))
6748 if (c >= 0)
6749 return glyphless_method;
6750 /* This method can't be used for the no-font case. */
6751 glyphless_method = Qempty_box;
6753 if (EQ (glyphless_method, Qthin_space))
6754 it->glyphless_method = GLYPHLESS_DISPLAY_THIN_SPACE;
6755 else if (EQ (glyphless_method, Qempty_box))
6756 it->glyphless_method = GLYPHLESS_DISPLAY_EMPTY_BOX;
6757 else if (EQ (glyphless_method, Qhex_code))
6758 it->glyphless_method = GLYPHLESS_DISPLAY_HEX_CODE;
6759 else if (STRINGP (glyphless_method))
6760 it->glyphless_method = GLYPHLESS_DISPLAY_ACRONYM;
6761 else
6763 /* Invalid value. We use the default method. */
6764 glyphless_method = Qnil;
6765 goto retry;
6767 it->what = IT_GLYPHLESS;
6768 return glyphless_method;
6771 /* Merge escape glyph face and cache the result. */
6773 static struct frame *last_escape_glyph_frame = NULL;
6774 static int last_escape_glyph_face_id = (1 << FACE_ID_BITS);
6775 static int last_escape_glyph_merged_face_id = 0;
6777 static int
6778 merge_escape_glyph_face (struct it *it)
6780 int face_id;
6782 if (it->f == last_escape_glyph_frame
6783 && it->face_id == last_escape_glyph_face_id)
6784 face_id = last_escape_glyph_merged_face_id;
6785 else
6787 /* Merge the `escape-glyph' face into the current face. */
6788 face_id = merge_faces (it->f, Qescape_glyph, 0, it->face_id);
6789 last_escape_glyph_frame = it->f;
6790 last_escape_glyph_face_id = it->face_id;
6791 last_escape_glyph_merged_face_id = face_id;
6793 return face_id;
6796 /* Likewise for glyphless glyph face. */
6798 static struct frame *last_glyphless_glyph_frame = NULL;
6799 static int last_glyphless_glyph_face_id = (1 << FACE_ID_BITS);
6800 static int last_glyphless_glyph_merged_face_id = 0;
6803 merge_glyphless_glyph_face (struct it *it)
6805 int face_id;
6807 if (it->f == last_glyphless_glyph_frame
6808 && it->face_id == last_glyphless_glyph_face_id)
6809 face_id = last_glyphless_glyph_merged_face_id;
6810 else
6812 /* Merge the `glyphless-char' face into the current face. */
6813 face_id = merge_faces (it->f, Qglyphless_char, 0, it->face_id);
6814 last_glyphless_glyph_frame = it->f;
6815 last_glyphless_glyph_face_id = it->face_id;
6816 last_glyphless_glyph_merged_face_id = face_id;
6818 return face_id;
6821 /* Load IT's display element fields with information about the next
6822 display element from the current position of IT. Value is zero if
6823 end of buffer (or C string) is reached. */
6825 static int
6826 get_next_display_element (struct it *it)
6828 /* Non-zero means that we found a display element. Zero means that
6829 we hit the end of what we iterate over. Performance note: the
6830 function pointer `method' used here turns out to be faster than
6831 using a sequence of if-statements. */
6832 int success_p;
6834 get_next:
6835 success_p = GET_NEXT_DISPLAY_ELEMENT (it);
6837 if (it->what == IT_CHARACTER)
6839 /* UAX#9, L4: "A character is depicted by a mirrored glyph if
6840 and only if (a) the resolved directionality of that character
6841 is R..." */
6842 /* FIXME: Do we need an exception for characters from display
6843 tables? */
6844 if (it->bidi_p && it->bidi_it.type == STRONG_R
6845 && !inhibit_bidi_mirroring)
6846 it->c = bidi_mirror_char (it->c);
6847 /* Map via display table or translate control characters.
6848 IT->c, IT->len etc. have been set to the next character by
6849 the function call above. If we have a display table, and it
6850 contains an entry for IT->c, translate it. Don't do this if
6851 IT->c itself comes from a display table, otherwise we could
6852 end up in an infinite recursion. (An alternative could be to
6853 count the recursion depth of this function and signal an
6854 error when a certain maximum depth is reached.) Is it worth
6855 it? */
6856 if (success_p && it->dpvec == NULL)
6858 Lisp_Object dv;
6859 struct charset *unibyte = CHARSET_FROM_ID (charset_unibyte);
6860 int nonascii_space_p = 0;
6861 int nonascii_hyphen_p = 0;
6862 int c = it->c; /* This is the character to display. */
6864 if (! it->multibyte_p && ! ASCII_CHAR_P (c))
6866 eassert (SINGLE_BYTE_CHAR_P (c));
6867 if (unibyte_display_via_language_environment)
6869 c = DECODE_CHAR (unibyte, c);
6870 if (c < 0)
6871 c = BYTE8_TO_CHAR (it->c);
6873 else
6874 c = BYTE8_TO_CHAR (it->c);
6877 if (it->dp
6878 && (dv = DISP_CHAR_VECTOR (it->dp, c),
6879 VECTORP (dv)))
6881 struct Lisp_Vector *v = XVECTOR (dv);
6883 /* Return the first character from the display table
6884 entry, if not empty. If empty, don't display the
6885 current character. */
6886 if (v->header.size)
6888 it->dpvec_char_len = it->len;
6889 it->dpvec = v->contents;
6890 it->dpend = v->contents + v->header.size;
6891 it->current.dpvec_index = 0;
6892 it->dpvec_face_id = -1;
6893 it->saved_face_id = it->face_id;
6894 it->method = GET_FROM_DISPLAY_VECTOR;
6895 it->ellipsis_p = 0;
6897 else
6899 set_iterator_to_next (it, 0);
6901 goto get_next;
6904 if (! NILP (lookup_glyphless_char_display (c, it)))
6906 if (it->what == IT_GLYPHLESS)
6907 goto done;
6908 /* Don't display this character. */
6909 set_iterator_to_next (it, 0);
6910 goto get_next;
6913 /* If `nobreak-char-display' is non-nil, we display
6914 non-ASCII spaces and hyphens specially. */
6915 if (! ASCII_CHAR_P (c) && ! NILP (Vnobreak_char_display))
6917 if (c == 0xA0)
6918 nonascii_space_p = true;
6919 else if (c == 0xAD || c == 0x2010 || c == 0x2011)
6920 nonascii_hyphen_p = true;
6923 /* Translate control characters into `\003' or `^C' form.
6924 Control characters coming from a display table entry are
6925 currently not translated because we use IT->dpvec to hold
6926 the translation. This could easily be changed but I
6927 don't believe that it is worth doing.
6929 The characters handled by `nobreak-char-display' must be
6930 translated too.
6932 Non-printable characters and raw-byte characters are also
6933 translated to octal form. */
6934 if (((c < ' ' || c == 127) /* ASCII control chars. */
6935 ? (it->area != TEXT_AREA
6936 /* In mode line, treat \n, \t like other crl chars. */
6937 || (c != '\t'
6938 && it->glyph_row
6939 && (it->glyph_row->mode_line_p || it->avoid_cursor_p))
6940 || (c != '\n' && c != '\t'))
6941 : (nonascii_space_p
6942 || nonascii_hyphen_p
6943 || CHAR_BYTE8_P (c)
6944 || ! CHAR_PRINTABLE_P (c))))
6946 /* C is a control character, non-ASCII space/hyphen,
6947 raw-byte, or a non-printable character which must be
6948 displayed either as '\003' or as `^C' where the '\\'
6949 and '^' can be defined in the display table. Fill
6950 IT->ctl_chars with glyphs for what we have to
6951 display. Then, set IT->dpvec to these glyphs. */
6952 Lisp_Object gc;
6953 int ctl_len;
6954 int face_id;
6955 int lface_id = 0;
6956 int escape_glyph;
6958 /* Handle control characters with ^. */
6960 if (ASCII_CHAR_P (c) && it->ctl_arrow_p)
6962 int g;
6964 g = '^'; /* default glyph for Control */
6965 /* Set IT->ctl_chars[0] to the glyph for `^'. */
6966 if (it->dp
6967 && (gc = DISP_CTRL_GLYPH (it->dp), GLYPH_CODE_P (gc)))
6969 g = GLYPH_CODE_CHAR (gc);
6970 lface_id = GLYPH_CODE_FACE (gc);
6973 face_id = (lface_id
6974 ? merge_faces (it->f, Qt, lface_id, it->face_id)
6975 : merge_escape_glyph_face (it));
6977 XSETINT (it->ctl_chars[0], g);
6978 XSETINT (it->ctl_chars[1], c ^ 0100);
6979 ctl_len = 2;
6980 goto display_control;
6983 /* Handle non-ascii space in the mode where it only gets
6984 highlighting. */
6986 if (nonascii_space_p && EQ (Vnobreak_char_display, Qt))
6988 /* Merge `nobreak-space' into the current face. */
6989 face_id = merge_faces (it->f, Qnobreak_space, 0,
6990 it->face_id);
6991 XSETINT (it->ctl_chars[0], ' ');
6992 ctl_len = 1;
6993 goto display_control;
6996 /* Handle sequences that start with the "escape glyph". */
6998 /* the default escape glyph is \. */
6999 escape_glyph = '\\';
7001 if (it->dp
7002 && (gc = DISP_ESCAPE_GLYPH (it->dp), GLYPH_CODE_P (gc)))
7004 escape_glyph = GLYPH_CODE_CHAR (gc);
7005 lface_id = GLYPH_CODE_FACE (gc);
7008 face_id = (lface_id
7009 ? merge_faces (it->f, Qt, lface_id, it->face_id)
7010 : merge_escape_glyph_face (it));
7012 /* Draw non-ASCII hyphen with just highlighting: */
7014 if (nonascii_hyphen_p && EQ (Vnobreak_char_display, Qt))
7016 XSETINT (it->ctl_chars[0], '-');
7017 ctl_len = 1;
7018 goto display_control;
7021 /* Draw non-ASCII space/hyphen with escape glyph: */
7023 if (nonascii_space_p || nonascii_hyphen_p)
7025 XSETINT (it->ctl_chars[0], escape_glyph);
7026 XSETINT (it->ctl_chars[1], nonascii_space_p ? ' ' : '-');
7027 ctl_len = 2;
7028 goto display_control;
7032 char str[10];
7033 int len, i;
7035 if (CHAR_BYTE8_P (c))
7036 /* Display \200 instead of \17777600. */
7037 c = CHAR_TO_BYTE8 (c);
7038 len = sprintf (str, "%03o", c);
7040 XSETINT (it->ctl_chars[0], escape_glyph);
7041 for (i = 0; i < len; i++)
7042 XSETINT (it->ctl_chars[i + 1], str[i]);
7043 ctl_len = len + 1;
7046 display_control:
7047 /* Set up IT->dpvec and return first character from it. */
7048 it->dpvec_char_len = it->len;
7049 it->dpvec = it->ctl_chars;
7050 it->dpend = it->dpvec + ctl_len;
7051 it->current.dpvec_index = 0;
7052 it->dpvec_face_id = face_id;
7053 it->saved_face_id = it->face_id;
7054 it->method = GET_FROM_DISPLAY_VECTOR;
7055 it->ellipsis_p = 0;
7056 goto get_next;
7058 it->char_to_display = c;
7060 else if (success_p)
7062 it->char_to_display = it->c;
7066 #ifdef HAVE_WINDOW_SYSTEM
7067 /* Adjust face id for a multibyte character. There are no multibyte
7068 character in unibyte text. */
7069 if ((it->what == IT_CHARACTER || it->what == IT_COMPOSITION)
7070 && it->multibyte_p
7071 && success_p
7072 && FRAME_WINDOW_P (it->f))
7074 struct face *face = FACE_FROM_ID (it->f, it->face_id);
7076 if (it->what == IT_COMPOSITION && it->cmp_it.ch >= 0)
7078 /* Automatic composition with glyph-string. */
7079 Lisp_Object gstring = composition_gstring_from_id (it->cmp_it.id);
7081 it->face_id = face_for_font (it->f, LGSTRING_FONT (gstring), face);
7083 else
7085 ptrdiff_t pos = (it->s ? -1
7086 : STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
7087 : IT_CHARPOS (*it));
7088 int c;
7090 if (it->what == IT_CHARACTER)
7091 c = it->char_to_display;
7092 else
7094 struct composition *cmp = composition_table[it->cmp_it.id];
7095 int i;
7097 c = ' ';
7098 for (i = 0; i < cmp->glyph_len; i++)
7099 /* TAB in a composition means display glyphs with
7100 padding space on the left or right. */
7101 if ((c = COMPOSITION_GLYPH (cmp, i)) != '\t')
7102 break;
7104 it->face_id = FACE_FOR_CHAR (it->f, face, c, pos, it->string);
7107 #endif /* HAVE_WINDOW_SYSTEM */
7109 done:
7110 /* Is this character the last one of a run of characters with
7111 box? If yes, set IT->end_of_box_run_p to 1. */
7112 if (it->face_box_p
7113 && it->s == NULL)
7115 if (it->method == GET_FROM_STRING && it->sp)
7117 int face_id = underlying_face_id (it);
7118 struct face *face = FACE_FROM_ID (it->f, face_id);
7120 if (face)
7122 if (face->box == FACE_NO_BOX)
7124 /* If the box comes from face properties in a
7125 display string, check faces in that string. */
7126 int string_face_id = face_after_it_pos (it);
7127 it->end_of_box_run_p
7128 = (FACE_FROM_ID (it->f, string_face_id)->box
7129 == FACE_NO_BOX);
7131 /* Otherwise, the box comes from the underlying face.
7132 If this is the last string character displayed, check
7133 the next buffer location. */
7134 else if ((IT_STRING_CHARPOS (*it) >= SCHARS (it->string) - 1)
7135 /* n_overlay_strings is unreliable unless
7136 overlay_string_index is non-negative. */
7137 && ((it->current.overlay_string_index >= 0
7138 && (it->current.overlay_string_index
7139 == it->n_overlay_strings - 1))
7140 /* A string from display property. */
7141 || it->from_disp_prop_p))
7143 ptrdiff_t ignore;
7144 int next_face_id;
7145 struct text_pos pos = it->current.pos;
7147 /* For a string from a display property, the next
7148 buffer position is stored in the 'position'
7149 member of the iteration stack slot below the
7150 current one, see handle_single_display_spec. By
7151 contrast, it->current.pos was is not yet updated
7152 to point to that buffer position; that will
7153 happen in pop_it, after we finish displaying the
7154 current string. Note that we already checked
7155 above that it->sp is positive, so subtracting one
7156 from it is safe. */
7157 if (it->from_disp_prop_p)
7158 pos = (it->stack + it->sp - 1)->position;
7159 else
7160 INC_TEXT_POS (pos, it->multibyte_p);
7162 if (CHARPOS (pos) >= ZV)
7163 it->end_of_box_run_p = true;
7164 else
7166 next_face_id = face_at_buffer_position
7167 (it->w, CHARPOS (pos), &ignore,
7168 CHARPOS (pos) + TEXT_PROP_DISTANCE_LIMIT, false, -1);
7169 it->end_of_box_run_p
7170 = (FACE_FROM_ID (it->f, next_face_id)->box
7171 == FACE_NO_BOX);
7176 /* next_element_from_display_vector sets this flag according to
7177 faces of the display vector glyphs, see there. */
7178 else if (it->method != GET_FROM_DISPLAY_VECTOR)
7180 int face_id = face_after_it_pos (it);
7181 it->end_of_box_run_p
7182 = (face_id != it->face_id
7183 && FACE_FROM_ID (it->f, face_id)->box == FACE_NO_BOX);
7186 /* If we reached the end of the object we've been iterating (e.g., a
7187 display string or an overlay string), and there's something on
7188 IT->stack, proceed with what's on the stack. It doesn't make
7189 sense to return zero if there's unprocessed stuff on the stack,
7190 because otherwise that stuff will never be displayed. */
7191 if (!success_p && it->sp > 0)
7193 set_iterator_to_next (it, 0);
7194 success_p = get_next_display_element (it);
7197 /* Value is 0 if end of buffer or string reached. */
7198 return success_p;
7202 /* Move IT to the next display element.
7204 RESEAT_P non-zero means if called on a newline in buffer text,
7205 skip to the next visible line start.
7207 Functions get_next_display_element and set_iterator_to_next are
7208 separate because I find this arrangement easier to handle than a
7209 get_next_display_element function that also increments IT's
7210 position. The way it is we can first look at an iterator's current
7211 display element, decide whether it fits on a line, and if it does,
7212 increment the iterator position. The other way around we probably
7213 would either need a flag indicating whether the iterator has to be
7214 incremented the next time, or we would have to implement a
7215 decrement position function which would not be easy to write. */
7217 void
7218 set_iterator_to_next (struct it *it, int reseat_p)
7220 /* Reset flags indicating start and end of a sequence of characters
7221 with box. Reset them at the start of this function because
7222 moving the iterator to a new position might set them. */
7223 it->start_of_box_run_p = it->end_of_box_run_p = 0;
7225 switch (it->method)
7227 case GET_FROM_BUFFER:
7228 /* The current display element of IT is a character from
7229 current_buffer. Advance in the buffer, and maybe skip over
7230 invisible lines that are so because of selective display. */
7231 if (ITERATOR_AT_END_OF_LINE_P (it) && reseat_p)
7232 reseat_at_next_visible_line_start (it, 0);
7233 else if (it->cmp_it.id >= 0)
7235 /* We are currently getting glyphs from a composition. */
7236 if (! it->bidi_p)
7238 IT_CHARPOS (*it) += it->cmp_it.nchars;
7239 IT_BYTEPOS (*it) += it->cmp_it.nbytes;
7241 else
7243 int i;
7245 /* Update IT's char/byte positions to point to the first
7246 character of the next grapheme cluster, or to the
7247 character visually after the current composition. */
7248 for (i = 0; i < it->cmp_it.nchars; i++)
7249 bidi_move_to_visually_next (&it->bidi_it);
7250 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7251 IT_CHARPOS (*it) = it->bidi_it.charpos;
7254 if ((! it->bidi_p || ! it->cmp_it.reversed_p)
7255 && it->cmp_it.to < it->cmp_it.nglyphs)
7257 /* Composition created while scanning forward. Proceed
7258 to the next grapheme cluster. */
7259 it->cmp_it.from = it->cmp_it.to;
7261 else if ((it->bidi_p && it->cmp_it.reversed_p)
7262 && it->cmp_it.from > 0)
7264 /* Composition created while scanning backward. Proceed
7265 to the previous grapheme cluster. */
7266 it->cmp_it.to = it->cmp_it.from;
7268 else
7270 /* No more grapheme clusters in this composition.
7271 Find the next stop position. */
7272 ptrdiff_t stop = it->end_charpos;
7274 if (it->bidi_it.scan_dir < 0)
7275 /* Now we are scanning backward and don't know
7276 where to stop. */
7277 stop = -1;
7278 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
7279 IT_BYTEPOS (*it), stop, Qnil);
7282 else
7284 eassert (it->len != 0);
7286 if (!it->bidi_p)
7288 IT_BYTEPOS (*it) += it->len;
7289 IT_CHARPOS (*it) += 1;
7291 else
7293 int prev_scan_dir = it->bidi_it.scan_dir;
7294 /* If this is a new paragraph, determine its base
7295 direction (a.k.a. its base embedding level). */
7296 if (it->bidi_it.new_paragraph)
7297 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 0);
7298 bidi_move_to_visually_next (&it->bidi_it);
7299 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7300 IT_CHARPOS (*it) = it->bidi_it.charpos;
7301 if (prev_scan_dir != it->bidi_it.scan_dir)
7303 /* As the scan direction was changed, we must
7304 re-compute the stop position for composition. */
7305 ptrdiff_t stop = it->end_charpos;
7306 if (it->bidi_it.scan_dir < 0)
7307 stop = -1;
7308 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
7309 IT_BYTEPOS (*it), stop, Qnil);
7312 eassert (IT_BYTEPOS (*it) == CHAR_TO_BYTE (IT_CHARPOS (*it)));
7314 break;
7316 case GET_FROM_C_STRING:
7317 /* Current display element of IT is from a C string. */
7318 if (!it->bidi_p
7319 /* If the string position is beyond string's end, it means
7320 next_element_from_c_string is padding the string with
7321 blanks, in which case we bypass the bidi iterator,
7322 because it cannot deal with such virtual characters. */
7323 || IT_CHARPOS (*it) >= it->bidi_it.string.schars)
7325 IT_BYTEPOS (*it) += it->len;
7326 IT_CHARPOS (*it) += 1;
7328 else
7330 bidi_move_to_visually_next (&it->bidi_it);
7331 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7332 IT_CHARPOS (*it) = it->bidi_it.charpos;
7334 break;
7336 case GET_FROM_DISPLAY_VECTOR:
7337 /* Current display element of IT is from a display table entry.
7338 Advance in the display table definition. Reset it to null if
7339 end reached, and continue with characters from buffers/
7340 strings. */
7341 ++it->current.dpvec_index;
7343 /* Restore face of the iterator to what they were before the
7344 display vector entry (these entries may contain faces). */
7345 it->face_id = it->saved_face_id;
7347 if (it->dpvec + it->current.dpvec_index >= it->dpend)
7349 int recheck_faces = it->ellipsis_p;
7351 if (it->s)
7352 it->method = GET_FROM_C_STRING;
7353 else if (STRINGP (it->string))
7354 it->method = GET_FROM_STRING;
7355 else
7357 it->method = GET_FROM_BUFFER;
7358 it->object = it->w->contents;
7361 it->dpvec = NULL;
7362 it->current.dpvec_index = -1;
7364 /* Skip over characters which were displayed via IT->dpvec. */
7365 if (it->dpvec_char_len < 0)
7366 reseat_at_next_visible_line_start (it, 1);
7367 else if (it->dpvec_char_len > 0)
7369 if (it->method == GET_FROM_STRING
7370 && it->current.overlay_string_index >= 0
7371 && it->n_overlay_strings > 0)
7372 it->ignore_overlay_strings_at_pos_p = true;
7373 it->len = it->dpvec_char_len;
7374 set_iterator_to_next (it, reseat_p);
7377 /* Maybe recheck faces after display vector. */
7378 if (recheck_faces)
7379 it->stop_charpos = IT_CHARPOS (*it);
7381 break;
7383 case GET_FROM_STRING:
7384 /* Current display element is a character from a Lisp string. */
7385 eassert (it->s == NULL && STRINGP (it->string));
7386 /* Don't advance past string end. These conditions are true
7387 when set_iterator_to_next is called at the end of
7388 get_next_display_element, in which case the Lisp string is
7389 already exhausted, and all we want is pop the iterator
7390 stack. */
7391 if (it->current.overlay_string_index >= 0)
7393 /* This is an overlay string, so there's no padding with
7394 spaces, and the number of characters in the string is
7395 where the string ends. */
7396 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7397 goto consider_string_end;
7399 else
7401 /* Not an overlay string. There could be padding, so test
7402 against it->end_charpos. */
7403 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
7404 goto consider_string_end;
7406 if (it->cmp_it.id >= 0)
7408 /* We are delivering display elements from a composition.
7409 Update the string position past the grapheme cluster
7410 we've just processed. */
7411 if (! it->bidi_p)
7413 IT_STRING_CHARPOS (*it) += it->cmp_it.nchars;
7414 IT_STRING_BYTEPOS (*it) += it->cmp_it.nbytes;
7416 else
7418 int i;
7420 for (i = 0; i < it->cmp_it.nchars; i++)
7421 bidi_move_to_visually_next (&it->bidi_it);
7422 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7423 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7426 /* Did we exhaust all the grapheme clusters of this
7427 composition? */
7428 if ((! it->bidi_p || ! it->cmp_it.reversed_p)
7429 && (it->cmp_it.to < it->cmp_it.nglyphs))
7431 /* Not all the grapheme clusters were processed yet;
7432 advance to the next cluster. */
7433 it->cmp_it.from = it->cmp_it.to;
7435 else if ((it->bidi_p && it->cmp_it.reversed_p)
7436 && it->cmp_it.from > 0)
7438 /* Likewise: advance to the next cluster, but going in
7439 the reverse direction. */
7440 it->cmp_it.to = it->cmp_it.from;
7442 else
7444 /* This composition was fully processed; find the next
7445 candidate place for checking for composed
7446 characters. */
7447 /* Always limit string searches to the string length;
7448 any padding spaces are not part of the string, and
7449 there cannot be any compositions in that padding. */
7450 ptrdiff_t stop = SCHARS (it->string);
7452 if (it->bidi_p && it->bidi_it.scan_dir < 0)
7453 stop = -1;
7454 else if (it->end_charpos < stop)
7456 /* Cf. PRECISION in reseat_to_string: we might be
7457 limited in how many of the string characters we
7458 need to deliver. */
7459 stop = it->end_charpos;
7461 composition_compute_stop_pos (&it->cmp_it,
7462 IT_STRING_CHARPOS (*it),
7463 IT_STRING_BYTEPOS (*it), stop,
7464 it->string);
7467 else
7469 if (!it->bidi_p
7470 /* If the string position is beyond string's end, it
7471 means next_element_from_string is padding the string
7472 with blanks, in which case we bypass the bidi
7473 iterator, because it cannot deal with such virtual
7474 characters. */
7475 || IT_STRING_CHARPOS (*it) >= it->bidi_it.string.schars)
7477 IT_STRING_BYTEPOS (*it) += it->len;
7478 IT_STRING_CHARPOS (*it) += 1;
7480 else
7482 int prev_scan_dir = it->bidi_it.scan_dir;
7484 bidi_move_to_visually_next (&it->bidi_it);
7485 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7486 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7487 /* If the scan direction changes, we may need to update
7488 the place where to check for composed characters. */
7489 if (prev_scan_dir != it->bidi_it.scan_dir)
7491 ptrdiff_t stop = SCHARS (it->string);
7493 if (it->bidi_it.scan_dir < 0)
7494 stop = -1;
7495 else if (it->end_charpos < stop)
7496 stop = it->end_charpos;
7498 composition_compute_stop_pos (&it->cmp_it,
7499 IT_STRING_CHARPOS (*it),
7500 IT_STRING_BYTEPOS (*it), stop,
7501 it->string);
7506 consider_string_end:
7508 if (it->current.overlay_string_index >= 0)
7510 /* IT->string is an overlay string. Advance to the
7511 next, if there is one. */
7512 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7514 it->ellipsis_p = 0;
7515 next_overlay_string (it);
7516 if (it->ellipsis_p)
7517 setup_for_ellipsis (it, 0);
7520 else
7522 /* IT->string is not an overlay string. If we reached
7523 its end, and there is something on IT->stack, proceed
7524 with what is on the stack. This can be either another
7525 string, this time an overlay string, or a buffer. */
7526 if (IT_STRING_CHARPOS (*it) == SCHARS (it->string)
7527 && it->sp > 0)
7529 pop_it (it);
7530 if (it->method == GET_FROM_STRING)
7531 goto consider_string_end;
7534 break;
7536 case GET_FROM_IMAGE:
7537 case GET_FROM_STRETCH:
7538 /* The position etc with which we have to proceed are on
7539 the stack. The position may be at the end of a string,
7540 if the `display' property takes up the whole string. */
7541 eassert (it->sp > 0);
7542 pop_it (it);
7543 if (it->method == GET_FROM_STRING)
7544 goto consider_string_end;
7545 break;
7547 default:
7548 /* There are no other methods defined, so this should be a bug. */
7549 emacs_abort ();
7552 eassert (it->method != GET_FROM_STRING
7553 || (STRINGP (it->string)
7554 && IT_STRING_CHARPOS (*it) >= 0));
7557 /* Load IT's display element fields with information about the next
7558 display element which comes from a display table entry or from the
7559 result of translating a control character to one of the forms `^C'
7560 or `\003'.
7562 IT->dpvec holds the glyphs to return as characters.
7563 IT->saved_face_id holds the face id before the display vector--it
7564 is restored into IT->face_id in set_iterator_to_next. */
7566 static int
7567 next_element_from_display_vector (struct it *it)
7569 Lisp_Object gc;
7570 int prev_face_id = it->face_id;
7571 int next_face_id;
7573 /* Precondition. */
7574 eassert (it->dpvec && it->current.dpvec_index >= 0);
7576 it->face_id = it->saved_face_id;
7578 /* KFS: This code used to check ip->dpvec[0] instead of the current element.
7579 That seemed totally bogus - so I changed it... */
7580 gc = it->dpvec[it->current.dpvec_index];
7582 if (GLYPH_CODE_P (gc))
7584 struct face *this_face, *prev_face, *next_face;
7586 it->c = GLYPH_CODE_CHAR (gc);
7587 it->len = CHAR_BYTES (it->c);
7589 /* The entry may contain a face id to use. Such a face id is
7590 the id of a Lisp face, not a realized face. A face id of
7591 zero means no face is specified. */
7592 if (it->dpvec_face_id >= 0)
7593 it->face_id = it->dpvec_face_id;
7594 else
7596 int lface_id = GLYPH_CODE_FACE (gc);
7597 if (lface_id > 0)
7598 it->face_id = merge_faces (it->f, Qt, lface_id,
7599 it->saved_face_id);
7602 /* Glyphs in the display vector could have the box face, so we
7603 need to set the related flags in the iterator, as
7604 appropriate. */
7605 this_face = FACE_FROM_ID (it->f, it->face_id);
7606 prev_face = FACE_FROM_ID (it->f, prev_face_id);
7608 /* Is this character the first character of a box-face run? */
7609 it->start_of_box_run_p = (this_face && this_face->box != FACE_NO_BOX
7610 && (!prev_face
7611 || prev_face->box == FACE_NO_BOX));
7613 /* For the last character of the box-face run, we need to look
7614 either at the next glyph from the display vector, or at the
7615 face we saw before the display vector. */
7616 next_face_id = it->saved_face_id;
7617 if (it->current.dpvec_index < it->dpend - it->dpvec - 1)
7619 if (it->dpvec_face_id >= 0)
7620 next_face_id = it->dpvec_face_id;
7621 else
7623 int lface_id =
7624 GLYPH_CODE_FACE (it->dpvec[it->current.dpvec_index + 1]);
7626 if (lface_id > 0)
7627 next_face_id = merge_faces (it->f, Qt, lface_id,
7628 it->saved_face_id);
7631 next_face = FACE_FROM_ID (it->f, next_face_id);
7632 it->end_of_box_run_p = (this_face && this_face->box != FACE_NO_BOX
7633 && (!next_face
7634 || next_face->box == FACE_NO_BOX));
7635 it->face_box_p = this_face && this_face->box != FACE_NO_BOX;
7637 else
7638 /* Display table entry is invalid. Return a space. */
7639 it->c = ' ', it->len = 1;
7641 /* Don't change position and object of the iterator here. They are
7642 still the values of the character that had this display table
7643 entry or was translated, and that's what we want. */
7644 it->what = IT_CHARACTER;
7645 return 1;
7648 /* Get the first element of string/buffer in the visual order, after
7649 being reseated to a new position in a string or a buffer. */
7650 static void
7651 get_visually_first_element (struct it *it)
7653 int string_p = STRINGP (it->string) || it->s;
7654 ptrdiff_t eob = (string_p ? it->bidi_it.string.schars : ZV);
7655 ptrdiff_t bob = (string_p ? 0 : BEGV);
7657 if (STRINGP (it->string))
7659 it->bidi_it.charpos = IT_STRING_CHARPOS (*it);
7660 it->bidi_it.bytepos = IT_STRING_BYTEPOS (*it);
7662 else
7664 it->bidi_it.charpos = IT_CHARPOS (*it);
7665 it->bidi_it.bytepos = IT_BYTEPOS (*it);
7668 if (it->bidi_it.charpos == eob)
7670 /* Nothing to do, but reset the FIRST_ELT flag, like
7671 bidi_paragraph_init does, because we are not going to
7672 call it. */
7673 it->bidi_it.first_elt = 0;
7675 else if (it->bidi_it.charpos == bob
7676 || (!string_p
7677 && (FETCH_CHAR (it->bidi_it.bytepos - 1) == '\n'
7678 || FETCH_CHAR (it->bidi_it.bytepos) == '\n')))
7680 /* If we are at the beginning of a line/string, we can produce
7681 the next element right away. */
7682 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
7683 bidi_move_to_visually_next (&it->bidi_it);
7685 else
7687 ptrdiff_t orig_bytepos = it->bidi_it.bytepos;
7689 /* We need to prime the bidi iterator starting at the line's or
7690 string's beginning, before we will be able to produce the
7691 next element. */
7692 if (string_p)
7693 it->bidi_it.charpos = it->bidi_it.bytepos = 0;
7694 else
7695 it->bidi_it.charpos = find_newline_no_quit (IT_CHARPOS (*it),
7696 IT_BYTEPOS (*it), -1,
7697 &it->bidi_it.bytepos);
7698 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
7701 /* Now return to buffer/string position where we were asked
7702 to get the next display element, and produce that. */
7703 bidi_move_to_visually_next (&it->bidi_it);
7705 while (it->bidi_it.bytepos != orig_bytepos
7706 && it->bidi_it.charpos < eob);
7709 /* Adjust IT's position information to where we ended up. */
7710 if (STRINGP (it->string))
7712 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7713 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7715 else
7717 IT_CHARPOS (*it) = it->bidi_it.charpos;
7718 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7721 if (STRINGP (it->string) || !it->s)
7723 ptrdiff_t stop, charpos, bytepos;
7725 if (STRINGP (it->string))
7727 eassert (!it->s);
7728 stop = SCHARS (it->string);
7729 if (stop > it->end_charpos)
7730 stop = it->end_charpos;
7731 charpos = IT_STRING_CHARPOS (*it);
7732 bytepos = IT_STRING_BYTEPOS (*it);
7734 else
7736 stop = it->end_charpos;
7737 charpos = IT_CHARPOS (*it);
7738 bytepos = IT_BYTEPOS (*it);
7740 if (it->bidi_it.scan_dir < 0)
7741 stop = -1;
7742 composition_compute_stop_pos (&it->cmp_it, charpos, bytepos, stop,
7743 it->string);
7747 /* Load IT with the next display element from Lisp string IT->string.
7748 IT->current.string_pos is the current position within the string.
7749 If IT->current.overlay_string_index >= 0, the Lisp string is an
7750 overlay string. */
7752 static int
7753 next_element_from_string (struct it *it)
7755 struct text_pos position;
7757 eassert (STRINGP (it->string));
7758 eassert (!it->bidi_p || EQ (it->string, it->bidi_it.string.lstring));
7759 eassert (IT_STRING_CHARPOS (*it) >= 0);
7760 position = it->current.string_pos;
7762 /* With bidi reordering, the character to display might not be the
7763 character at IT_STRING_CHARPOS. BIDI_IT.FIRST_ELT non-zero means
7764 that we were reseat()ed to a new string, whose paragraph
7765 direction is not known. */
7766 if (it->bidi_p && it->bidi_it.first_elt)
7768 get_visually_first_element (it);
7769 SET_TEXT_POS (position, IT_STRING_CHARPOS (*it), IT_STRING_BYTEPOS (*it));
7772 /* Time to check for invisible text? */
7773 if (IT_STRING_CHARPOS (*it) < it->end_charpos)
7775 if (IT_STRING_CHARPOS (*it) >= it->stop_charpos)
7777 if (!(!it->bidi_p
7778 || BIDI_AT_BASE_LEVEL (it->bidi_it)
7779 || IT_STRING_CHARPOS (*it) == it->stop_charpos))
7781 /* With bidi non-linear iteration, we could find
7782 ourselves far beyond the last computed stop_charpos,
7783 with several other stop positions in between that we
7784 missed. Scan them all now, in buffer's logical
7785 order, until we find and handle the last stop_charpos
7786 that precedes our current position. */
7787 handle_stop_backwards (it, it->stop_charpos);
7788 return GET_NEXT_DISPLAY_ELEMENT (it);
7790 else
7792 if (it->bidi_p)
7794 /* Take note of the stop position we just moved
7795 across, for when we will move back across it. */
7796 it->prev_stop = it->stop_charpos;
7797 /* If we are at base paragraph embedding level, take
7798 note of the last stop position seen at this
7799 level. */
7800 if (BIDI_AT_BASE_LEVEL (it->bidi_it))
7801 it->base_level_stop = it->stop_charpos;
7803 handle_stop (it);
7805 /* Since a handler may have changed IT->method, we must
7806 recurse here. */
7807 return GET_NEXT_DISPLAY_ELEMENT (it);
7810 else if (it->bidi_p
7811 /* If we are before prev_stop, we may have overstepped
7812 on our way backwards a stop_pos, and if so, we need
7813 to handle that stop_pos. */
7814 && IT_STRING_CHARPOS (*it) < it->prev_stop
7815 /* We can sometimes back up for reasons that have nothing
7816 to do with bidi reordering. E.g., compositions. The
7817 code below is only needed when we are above the base
7818 embedding level, so test for that explicitly. */
7819 && !BIDI_AT_BASE_LEVEL (it->bidi_it))
7821 /* If we lost track of base_level_stop, we have no better
7822 place for handle_stop_backwards to start from than string
7823 beginning. This happens, e.g., when we were reseated to
7824 the previous screenful of text by vertical-motion. */
7825 if (it->base_level_stop <= 0
7826 || IT_STRING_CHARPOS (*it) < it->base_level_stop)
7827 it->base_level_stop = 0;
7828 handle_stop_backwards (it, it->base_level_stop);
7829 return GET_NEXT_DISPLAY_ELEMENT (it);
7833 if (it->current.overlay_string_index >= 0)
7835 /* Get the next character from an overlay string. In overlay
7836 strings, there is no field width or padding with spaces to
7837 do. */
7838 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7840 it->what = IT_EOB;
7841 return 0;
7843 else if (CHAR_COMPOSED_P (it, IT_STRING_CHARPOS (*it),
7844 IT_STRING_BYTEPOS (*it),
7845 it->bidi_it.scan_dir < 0
7846 ? -1
7847 : SCHARS (it->string))
7848 && next_element_from_composition (it))
7850 return 1;
7852 else if (STRING_MULTIBYTE (it->string))
7854 const unsigned char *s = (SDATA (it->string)
7855 + IT_STRING_BYTEPOS (*it));
7856 it->c = string_char_and_length (s, &it->len);
7858 else
7860 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
7861 it->len = 1;
7864 else
7866 /* Get the next character from a Lisp string that is not an
7867 overlay string. Such strings come from the mode line, for
7868 example. We may have to pad with spaces, or truncate the
7869 string. See also next_element_from_c_string. */
7870 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
7872 it->what = IT_EOB;
7873 return 0;
7875 else if (IT_STRING_CHARPOS (*it) >= it->string_nchars)
7877 /* Pad with spaces. */
7878 it->c = ' ', it->len = 1;
7879 CHARPOS (position) = BYTEPOS (position) = -1;
7881 else if (CHAR_COMPOSED_P (it, IT_STRING_CHARPOS (*it),
7882 IT_STRING_BYTEPOS (*it),
7883 it->bidi_it.scan_dir < 0
7884 ? -1
7885 : it->string_nchars)
7886 && next_element_from_composition (it))
7888 return 1;
7890 else if (STRING_MULTIBYTE (it->string))
7892 const unsigned char *s = (SDATA (it->string)
7893 + IT_STRING_BYTEPOS (*it));
7894 it->c = string_char_and_length (s, &it->len);
7896 else
7898 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
7899 it->len = 1;
7903 /* Record what we have and where it came from. */
7904 it->what = IT_CHARACTER;
7905 it->object = it->string;
7906 it->position = position;
7907 return 1;
7911 /* Load IT with next display element from C string IT->s.
7912 IT->string_nchars is the maximum number of characters to return
7913 from the string. IT->end_charpos may be greater than
7914 IT->string_nchars when this function is called, in which case we
7915 may have to return padding spaces. Value is zero if end of string
7916 reached, including padding spaces. */
7918 static int
7919 next_element_from_c_string (struct it *it)
7921 bool success_p = true;
7923 eassert (it->s);
7924 eassert (!it->bidi_p || it->s == it->bidi_it.string.s);
7925 it->what = IT_CHARACTER;
7926 BYTEPOS (it->position) = CHARPOS (it->position) = 0;
7927 it->object = make_number (0);
7929 /* With bidi reordering, the character to display might not be the
7930 character at IT_CHARPOS. BIDI_IT.FIRST_ELT non-zero means that
7931 we were reseated to a new string, whose paragraph direction is
7932 not known. */
7933 if (it->bidi_p && it->bidi_it.first_elt)
7934 get_visually_first_element (it);
7936 /* IT's position can be greater than IT->string_nchars in case a
7937 field width or precision has been specified when the iterator was
7938 initialized. */
7939 if (IT_CHARPOS (*it) >= it->end_charpos)
7941 /* End of the game. */
7942 it->what = IT_EOB;
7943 success_p = 0;
7945 else if (IT_CHARPOS (*it) >= it->string_nchars)
7947 /* Pad with spaces. */
7948 it->c = ' ', it->len = 1;
7949 BYTEPOS (it->position) = CHARPOS (it->position) = -1;
7951 else if (it->multibyte_p)
7952 it->c = string_char_and_length (it->s + IT_BYTEPOS (*it), &it->len);
7953 else
7954 it->c = it->s[IT_BYTEPOS (*it)], it->len = 1;
7956 return success_p;
7960 /* Set up IT to return characters from an ellipsis, if appropriate.
7961 The definition of the ellipsis glyphs may come from a display table
7962 entry. This function fills IT with the first glyph from the
7963 ellipsis if an ellipsis is to be displayed. */
7965 static int
7966 next_element_from_ellipsis (struct it *it)
7968 if (it->selective_display_ellipsis_p)
7969 setup_for_ellipsis (it, it->len);
7970 else
7972 /* The face at the current position may be different from the
7973 face we find after the invisible text. Remember what it
7974 was in IT->saved_face_id, and signal that it's there by
7975 setting face_before_selective_p. */
7976 it->saved_face_id = it->face_id;
7977 it->method = GET_FROM_BUFFER;
7978 it->object = it->w->contents;
7979 reseat_at_next_visible_line_start (it, 1);
7980 it->face_before_selective_p = true;
7983 return GET_NEXT_DISPLAY_ELEMENT (it);
7987 /* Deliver an image display element. The iterator IT is already
7988 filled with image information (done in handle_display_prop). Value
7989 is always 1. */
7992 static int
7993 next_element_from_image (struct it *it)
7995 it->what = IT_IMAGE;
7996 it->ignore_overlay_strings_at_pos_p = 0;
7997 return 1;
8001 /* Fill iterator IT with next display element from a stretch glyph
8002 property. IT->object is the value of the text property. Value is
8003 always 1. */
8005 static int
8006 next_element_from_stretch (struct it *it)
8008 it->what = IT_STRETCH;
8009 return 1;
8012 /* Scan backwards from IT's current position until we find a stop
8013 position, or until BEGV. This is called when we find ourself
8014 before both the last known prev_stop and base_level_stop while
8015 reordering bidirectional text. */
8017 static void
8018 compute_stop_pos_backwards (struct it *it)
8020 const int SCAN_BACK_LIMIT = 1000;
8021 struct text_pos pos;
8022 struct display_pos save_current = it->current;
8023 struct text_pos save_position = it->position;
8024 ptrdiff_t charpos = IT_CHARPOS (*it);
8025 ptrdiff_t where_we_are = charpos;
8026 ptrdiff_t save_stop_pos = it->stop_charpos;
8027 ptrdiff_t save_end_pos = it->end_charpos;
8029 eassert (NILP (it->string) && !it->s);
8030 eassert (it->bidi_p);
8031 it->bidi_p = 0;
8034 it->end_charpos = min (charpos + 1, ZV);
8035 charpos = max (charpos - SCAN_BACK_LIMIT, BEGV);
8036 SET_TEXT_POS (pos, charpos, CHAR_TO_BYTE (charpos));
8037 reseat_1 (it, pos, 0);
8038 compute_stop_pos (it);
8039 /* We must advance forward, right? */
8040 if (it->stop_charpos <= charpos)
8041 emacs_abort ();
8043 while (charpos > BEGV && it->stop_charpos >= it->end_charpos);
8045 if (it->stop_charpos <= where_we_are)
8046 it->prev_stop = it->stop_charpos;
8047 else
8048 it->prev_stop = BEGV;
8049 it->bidi_p = true;
8050 it->current = save_current;
8051 it->position = save_position;
8052 it->stop_charpos = save_stop_pos;
8053 it->end_charpos = save_end_pos;
8056 /* Scan forward from CHARPOS in the current buffer/string, until we
8057 find a stop position > current IT's position. Then handle the stop
8058 position before that. This is called when we bump into a stop
8059 position while reordering bidirectional text. CHARPOS should be
8060 the last previously processed stop_pos (or BEGV/0, if none were
8061 processed yet) whose position is less that IT's current
8062 position. */
8064 static void
8065 handle_stop_backwards (struct it *it, ptrdiff_t charpos)
8067 int bufp = !STRINGP (it->string);
8068 ptrdiff_t where_we_are = (bufp ? IT_CHARPOS (*it) : IT_STRING_CHARPOS (*it));
8069 struct display_pos save_current = it->current;
8070 struct text_pos save_position = it->position;
8071 struct text_pos pos1;
8072 ptrdiff_t next_stop;
8074 /* Scan in strict logical order. */
8075 eassert (it->bidi_p);
8076 it->bidi_p = 0;
8079 it->prev_stop = charpos;
8080 if (bufp)
8082 SET_TEXT_POS (pos1, charpos, CHAR_TO_BYTE (charpos));
8083 reseat_1 (it, pos1, 0);
8085 else
8086 it->current.string_pos = string_pos (charpos, it->string);
8087 compute_stop_pos (it);
8088 /* We must advance forward, right? */
8089 if (it->stop_charpos <= it->prev_stop)
8090 emacs_abort ();
8091 charpos = it->stop_charpos;
8093 while (charpos <= where_we_are);
8095 it->bidi_p = true;
8096 it->current = save_current;
8097 it->position = save_position;
8098 next_stop = it->stop_charpos;
8099 it->stop_charpos = it->prev_stop;
8100 handle_stop (it);
8101 it->stop_charpos = next_stop;
8104 /* Load IT with the next display element from current_buffer. Value
8105 is zero if end of buffer reached. IT->stop_charpos is the next
8106 position at which to stop and check for text properties or buffer
8107 end. */
8109 static int
8110 next_element_from_buffer (struct it *it)
8112 bool success_p = true;
8114 eassert (IT_CHARPOS (*it) >= BEGV);
8115 eassert (NILP (it->string) && !it->s);
8116 eassert (!it->bidi_p
8117 || (EQ (it->bidi_it.string.lstring, Qnil)
8118 && it->bidi_it.string.s == NULL));
8120 /* With bidi reordering, the character to display might not be the
8121 character at IT_CHARPOS. BIDI_IT.FIRST_ELT non-zero means that
8122 we were reseat()ed to a new buffer position, which is potentially
8123 a different paragraph. */
8124 if (it->bidi_p && it->bidi_it.first_elt)
8126 get_visually_first_element (it);
8127 SET_TEXT_POS (it->position, IT_CHARPOS (*it), IT_BYTEPOS (*it));
8130 if (IT_CHARPOS (*it) >= it->stop_charpos)
8132 if (IT_CHARPOS (*it) >= it->end_charpos)
8134 int overlay_strings_follow_p;
8136 /* End of the game, except when overlay strings follow that
8137 haven't been returned yet. */
8138 if (it->overlay_strings_at_end_processed_p)
8139 overlay_strings_follow_p = 0;
8140 else
8142 it->overlay_strings_at_end_processed_p = true;
8143 overlay_strings_follow_p = get_overlay_strings (it, 0);
8146 if (overlay_strings_follow_p)
8147 success_p = GET_NEXT_DISPLAY_ELEMENT (it);
8148 else
8150 it->what = IT_EOB;
8151 it->position = it->current.pos;
8152 success_p = 0;
8155 else if (!(!it->bidi_p
8156 || BIDI_AT_BASE_LEVEL (it->bidi_it)
8157 || IT_CHARPOS (*it) == it->stop_charpos))
8159 /* With bidi non-linear iteration, we could find ourselves
8160 far beyond the last computed stop_charpos, with several
8161 other stop positions in between that we missed. Scan
8162 them all now, in buffer's logical order, until we find
8163 and handle the last stop_charpos that precedes our
8164 current position. */
8165 handle_stop_backwards (it, it->stop_charpos);
8166 return GET_NEXT_DISPLAY_ELEMENT (it);
8168 else
8170 if (it->bidi_p)
8172 /* Take note of the stop position we just moved across,
8173 for when we will move back across it. */
8174 it->prev_stop = it->stop_charpos;
8175 /* If we are at base paragraph embedding level, take
8176 note of the last stop position seen at this
8177 level. */
8178 if (BIDI_AT_BASE_LEVEL (it->bidi_it))
8179 it->base_level_stop = it->stop_charpos;
8181 handle_stop (it);
8182 return GET_NEXT_DISPLAY_ELEMENT (it);
8185 else if (it->bidi_p
8186 /* If we are before prev_stop, we may have overstepped on
8187 our way backwards a stop_pos, and if so, we need to
8188 handle that stop_pos. */
8189 && IT_CHARPOS (*it) < it->prev_stop
8190 /* We can sometimes back up for reasons that have nothing
8191 to do with bidi reordering. E.g., compositions. The
8192 code below is only needed when we are above the base
8193 embedding level, so test for that explicitly. */
8194 && !BIDI_AT_BASE_LEVEL (it->bidi_it))
8196 if (it->base_level_stop <= 0
8197 || IT_CHARPOS (*it) < it->base_level_stop)
8199 /* If we lost track of base_level_stop, we need to find
8200 prev_stop by looking backwards. This happens, e.g., when
8201 we were reseated to the previous screenful of text by
8202 vertical-motion. */
8203 it->base_level_stop = BEGV;
8204 compute_stop_pos_backwards (it);
8205 handle_stop_backwards (it, it->prev_stop);
8207 else
8208 handle_stop_backwards (it, it->base_level_stop);
8209 return GET_NEXT_DISPLAY_ELEMENT (it);
8211 else
8213 /* No face changes, overlays etc. in sight, so just return a
8214 character from current_buffer. */
8215 unsigned char *p;
8216 ptrdiff_t stop;
8218 /* We moved to the next buffer position, so any info about
8219 previously seen overlays is no longer valid. */
8220 it->ignore_overlay_strings_at_pos_p = 0;
8222 /* Maybe run the redisplay end trigger hook. Performance note:
8223 This doesn't seem to cost measurable time. */
8224 if (it->redisplay_end_trigger_charpos
8225 && it->glyph_row
8226 && IT_CHARPOS (*it) >= it->redisplay_end_trigger_charpos)
8227 run_redisplay_end_trigger_hook (it);
8229 stop = it->bidi_it.scan_dir < 0 ? -1 : it->end_charpos;
8230 if (CHAR_COMPOSED_P (it, IT_CHARPOS (*it), IT_BYTEPOS (*it),
8231 stop)
8232 && next_element_from_composition (it))
8234 return 1;
8237 /* Get the next character, maybe multibyte. */
8238 p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
8239 if (it->multibyte_p && !ASCII_CHAR_P (*p))
8240 it->c = STRING_CHAR_AND_LENGTH (p, it->len);
8241 else
8242 it->c = *p, it->len = 1;
8244 /* Record what we have and where it came from. */
8245 it->what = IT_CHARACTER;
8246 it->object = it->w->contents;
8247 it->position = it->current.pos;
8249 /* Normally we return the character found above, except when we
8250 really want to return an ellipsis for selective display. */
8251 if (it->selective)
8253 if (it->c == '\n')
8255 /* A value of selective > 0 means hide lines indented more
8256 than that number of columns. */
8257 if (it->selective > 0
8258 && IT_CHARPOS (*it) + 1 < ZV
8259 && indented_beyond_p (IT_CHARPOS (*it) + 1,
8260 IT_BYTEPOS (*it) + 1,
8261 it->selective))
8263 success_p = next_element_from_ellipsis (it);
8264 it->dpvec_char_len = -1;
8267 else if (it->c == '\r' && it->selective == -1)
8269 /* A value of selective == -1 means that everything from the
8270 CR to the end of the line is invisible, with maybe an
8271 ellipsis displayed for it. */
8272 success_p = next_element_from_ellipsis (it);
8273 it->dpvec_char_len = -1;
8278 /* Value is zero if end of buffer reached. */
8279 eassert (!success_p || it->what != IT_CHARACTER || it->len > 0);
8280 return success_p;
8284 /* Run the redisplay end trigger hook for IT. */
8286 static void
8287 run_redisplay_end_trigger_hook (struct it *it)
8289 /* IT->glyph_row should be non-null, i.e. we should be actually
8290 displaying something, or otherwise we should not run the hook. */
8291 eassert (it->glyph_row);
8293 ptrdiff_t charpos = it->redisplay_end_trigger_charpos;
8294 it->redisplay_end_trigger_charpos = 0;
8296 /* Since we are *trying* to run these functions, don't try to run
8297 them again, even if they get an error. */
8298 wset_redisplay_end_trigger (it->w, Qnil);
8299 CALLN (Frun_hook_with_args, Qredisplay_end_trigger_functions, it->window,
8300 make_number (charpos));
8302 /* Notice if it changed the face of the character we are on. */
8303 handle_face_prop (it);
8307 /* Deliver a composition display element. Unlike the other
8308 next_element_from_XXX, this function is not registered in the array
8309 get_next_element[]. It is called from next_element_from_buffer and
8310 next_element_from_string when necessary. */
8312 static int
8313 next_element_from_composition (struct it *it)
8315 it->what = IT_COMPOSITION;
8316 it->len = it->cmp_it.nbytes;
8317 if (STRINGP (it->string))
8319 if (it->c < 0)
8321 IT_STRING_CHARPOS (*it) += it->cmp_it.nchars;
8322 IT_STRING_BYTEPOS (*it) += it->cmp_it.nbytes;
8323 return 0;
8325 it->position = it->current.string_pos;
8326 it->object = it->string;
8327 it->c = composition_update_it (&it->cmp_it, IT_STRING_CHARPOS (*it),
8328 IT_STRING_BYTEPOS (*it), it->string);
8330 else
8332 if (it->c < 0)
8334 IT_CHARPOS (*it) += it->cmp_it.nchars;
8335 IT_BYTEPOS (*it) += it->cmp_it.nbytes;
8336 if (it->bidi_p)
8338 if (it->bidi_it.new_paragraph)
8339 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 0);
8340 /* Resync the bidi iterator with IT's new position.
8341 FIXME: this doesn't support bidirectional text. */
8342 while (it->bidi_it.charpos < IT_CHARPOS (*it))
8343 bidi_move_to_visually_next (&it->bidi_it);
8345 return 0;
8347 it->position = it->current.pos;
8348 it->object = it->w->contents;
8349 it->c = composition_update_it (&it->cmp_it, IT_CHARPOS (*it),
8350 IT_BYTEPOS (*it), Qnil);
8352 return 1;
8357 /***********************************************************************
8358 Moving an iterator without producing glyphs
8359 ***********************************************************************/
8361 /* Check if iterator is at a position corresponding to a valid buffer
8362 position after some move_it_ call. */
8364 #define IT_POS_VALID_AFTER_MOVE_P(it) \
8365 ((it)->method == GET_FROM_STRING \
8366 ? IT_STRING_CHARPOS (*it) == 0 \
8367 : 1)
8370 /* Move iterator IT to a specified buffer or X position within one
8371 line on the display without producing glyphs.
8373 OP should be a bit mask including some or all of these bits:
8374 MOVE_TO_X: Stop upon reaching x-position TO_X.
8375 MOVE_TO_POS: Stop upon reaching buffer or string position TO_CHARPOS.
8376 Regardless of OP's value, stop upon reaching the end of the display line.
8378 TO_X is normally a value 0 <= TO_X <= IT->last_visible_x.
8379 This means, in particular, that TO_X includes window's horizontal
8380 scroll amount.
8382 The return value has several possible values that
8383 say what condition caused the scan to stop:
8385 MOVE_POS_MATCH_OR_ZV
8386 - when TO_POS or ZV was reached.
8388 MOVE_X_REACHED
8389 -when TO_X was reached before TO_POS or ZV were reached.
8391 MOVE_LINE_CONTINUED
8392 - when we reached the end of the display area and the line must
8393 be continued.
8395 MOVE_LINE_TRUNCATED
8396 - when we reached the end of the display area and the line is
8397 truncated.
8399 MOVE_NEWLINE_OR_CR
8400 - when we stopped at a line end, i.e. a newline or a CR and selective
8401 display is on. */
8403 static enum move_it_result
8404 move_it_in_display_line_to (struct it *it,
8405 ptrdiff_t to_charpos, int to_x,
8406 enum move_operation_enum op)
8408 enum move_it_result result = MOVE_UNDEFINED;
8409 struct glyph_row *saved_glyph_row;
8410 struct it wrap_it, atpos_it, atx_it, ppos_it;
8411 void *wrap_data = NULL, *atpos_data = NULL, *atx_data = NULL;
8412 void *ppos_data = NULL;
8413 int may_wrap = 0;
8414 enum it_method prev_method = it->method;
8415 ptrdiff_t closest_pos IF_LINT (= 0), prev_pos = IT_CHARPOS (*it);
8416 int saw_smaller_pos = prev_pos < to_charpos;
8418 /* Don't produce glyphs in produce_glyphs. */
8419 saved_glyph_row = it->glyph_row;
8420 it->glyph_row = NULL;
8422 /* Use wrap_it to save a copy of IT wherever a word wrap could
8423 occur. Use atpos_it to save a copy of IT at the desired buffer
8424 position, if found, so that we can scan ahead and check if the
8425 word later overshoots the window edge. Use atx_it similarly, for
8426 pixel positions. */
8427 wrap_it.sp = -1;
8428 atpos_it.sp = -1;
8429 atx_it.sp = -1;
8431 /* Use ppos_it under bidi reordering to save a copy of IT for the
8432 initial position. We restore that position in IT when we have
8433 scanned the entire display line without finding a match for
8434 TO_CHARPOS and all the character positions are greater than
8435 TO_CHARPOS. We then restart the scan from the initial position,
8436 and stop at CLOSEST_POS, which is a position > TO_CHARPOS that is
8437 the closest to TO_CHARPOS. */
8438 if (it->bidi_p)
8440 if ((op & MOVE_TO_POS) && IT_CHARPOS (*it) >= to_charpos)
8442 SAVE_IT (ppos_it, *it, ppos_data);
8443 closest_pos = IT_CHARPOS (*it);
8445 else
8446 closest_pos = ZV;
8449 #define BUFFER_POS_REACHED_P() \
8450 ((op & MOVE_TO_POS) != 0 \
8451 && BUFFERP (it->object) \
8452 && (IT_CHARPOS (*it) == to_charpos \
8453 || ((!it->bidi_p \
8454 || BIDI_AT_BASE_LEVEL (it->bidi_it)) \
8455 && IT_CHARPOS (*it) > to_charpos) \
8456 || (it->what == IT_COMPOSITION \
8457 && ((IT_CHARPOS (*it) > to_charpos \
8458 && to_charpos >= it->cmp_it.charpos) \
8459 || (IT_CHARPOS (*it) < to_charpos \
8460 && to_charpos <= it->cmp_it.charpos)))) \
8461 && (it->method == GET_FROM_BUFFER \
8462 || (it->method == GET_FROM_DISPLAY_VECTOR \
8463 && it->dpvec + it->current.dpvec_index + 1 >= it->dpend)))
8465 /* If there's a line-/wrap-prefix, handle it. */
8466 if (it->hpos == 0 && it->method == GET_FROM_BUFFER
8467 && it->current_y < it->last_visible_y)
8468 handle_line_prefix (it);
8470 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8471 SET_TEXT_POS (this_line_min_pos, IT_CHARPOS (*it), IT_BYTEPOS (*it));
8473 while (1)
8475 int x, i, ascent = 0, descent = 0;
8477 /* Utility macro to reset an iterator with x, ascent, and descent. */
8478 #define IT_RESET_X_ASCENT_DESCENT(IT) \
8479 ((IT)->current_x = x, (IT)->max_ascent = ascent, \
8480 (IT)->max_descent = descent)
8482 /* Stop if we move beyond TO_CHARPOS (after an image or a
8483 display string or stretch glyph). */
8484 if ((op & MOVE_TO_POS) != 0
8485 && BUFFERP (it->object)
8486 && it->method == GET_FROM_BUFFER
8487 && (((!it->bidi_p
8488 /* When the iterator is at base embedding level, we
8489 are guaranteed that characters are delivered for
8490 display in strictly increasing order of their
8491 buffer positions. */
8492 || BIDI_AT_BASE_LEVEL (it->bidi_it))
8493 && IT_CHARPOS (*it) > to_charpos)
8494 || (it->bidi_p
8495 && (prev_method == GET_FROM_IMAGE
8496 || prev_method == GET_FROM_STRETCH
8497 || prev_method == GET_FROM_STRING)
8498 /* Passed TO_CHARPOS from left to right. */
8499 && ((prev_pos < to_charpos
8500 && IT_CHARPOS (*it) > to_charpos)
8501 /* Passed TO_CHARPOS from right to left. */
8502 || (prev_pos > to_charpos
8503 && IT_CHARPOS (*it) < to_charpos)))))
8505 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8507 result = MOVE_POS_MATCH_OR_ZV;
8508 break;
8510 else if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
8511 /* If wrap_it is valid, the current position might be in a
8512 word that is wrapped. So, save the iterator in
8513 atpos_it and continue to see if wrapping happens. */
8514 SAVE_IT (atpos_it, *it, atpos_data);
8517 /* Stop when ZV reached.
8518 We used to stop here when TO_CHARPOS reached as well, but that is
8519 too soon if this glyph does not fit on this line. So we handle it
8520 explicitly below. */
8521 if (!get_next_display_element (it))
8523 result = MOVE_POS_MATCH_OR_ZV;
8524 break;
8527 if (it->line_wrap == TRUNCATE)
8529 if (BUFFER_POS_REACHED_P ())
8531 result = MOVE_POS_MATCH_OR_ZV;
8532 break;
8535 else
8537 if (it->line_wrap == WORD_WRAP && it->area == TEXT_AREA)
8539 if (IT_DISPLAYING_WHITESPACE (it))
8540 may_wrap = 1;
8541 else if (may_wrap)
8543 /* We have reached a glyph that follows one or more
8544 whitespace characters. If the position is
8545 already found, we are done. */
8546 if (atpos_it.sp >= 0)
8548 RESTORE_IT (it, &atpos_it, atpos_data);
8549 result = MOVE_POS_MATCH_OR_ZV;
8550 goto done;
8552 if (atx_it.sp >= 0)
8554 RESTORE_IT (it, &atx_it, atx_data);
8555 result = MOVE_X_REACHED;
8556 goto done;
8558 /* Otherwise, we can wrap here. */
8559 SAVE_IT (wrap_it, *it, wrap_data);
8560 may_wrap = 0;
8565 /* Remember the line height for the current line, in case
8566 the next element doesn't fit on the line. */
8567 ascent = it->max_ascent;
8568 descent = it->max_descent;
8570 /* The call to produce_glyphs will get the metrics of the
8571 display element IT is loaded with. Record the x-position
8572 before this display element, in case it doesn't fit on the
8573 line. */
8574 x = it->current_x;
8576 PRODUCE_GLYPHS (it);
8578 if (it->area != TEXT_AREA)
8580 prev_method = it->method;
8581 if (it->method == GET_FROM_BUFFER)
8582 prev_pos = IT_CHARPOS (*it);
8583 set_iterator_to_next (it, 1);
8584 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8585 SET_TEXT_POS (this_line_min_pos,
8586 IT_CHARPOS (*it), IT_BYTEPOS (*it));
8587 if (it->bidi_p
8588 && (op & MOVE_TO_POS)
8589 && IT_CHARPOS (*it) > to_charpos
8590 && IT_CHARPOS (*it) < closest_pos)
8591 closest_pos = IT_CHARPOS (*it);
8592 continue;
8595 /* The number of glyphs we get back in IT->nglyphs will normally
8596 be 1 except when IT->c is (i) a TAB, or (ii) a multi-glyph
8597 character on a terminal frame, or (iii) a line end. For the
8598 second case, IT->nglyphs - 1 padding glyphs will be present.
8599 (On X frames, there is only one glyph produced for a
8600 composite character.)
8602 The behavior implemented below means, for continuation lines,
8603 that as many spaces of a TAB as fit on the current line are
8604 displayed there. For terminal frames, as many glyphs of a
8605 multi-glyph character are displayed in the current line, too.
8606 This is what the old redisplay code did, and we keep it that
8607 way. Under X, the whole shape of a complex character must
8608 fit on the line or it will be completely displayed in the
8609 next line.
8611 Note that both for tabs and padding glyphs, all glyphs have
8612 the same width. */
8613 if (it->nglyphs)
8615 /* More than one glyph or glyph doesn't fit on line. All
8616 glyphs have the same width. */
8617 int single_glyph_width = it->pixel_width / it->nglyphs;
8618 int new_x;
8619 int x_before_this_char = x;
8620 int hpos_before_this_char = it->hpos;
8622 for (i = 0; i < it->nglyphs; ++i, x = new_x)
8624 new_x = x + single_glyph_width;
8626 /* We want to leave anything reaching TO_X to the caller. */
8627 if ((op & MOVE_TO_X) && new_x > to_x)
8629 if (BUFFER_POS_REACHED_P ())
8631 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8632 goto buffer_pos_reached;
8633 if (atpos_it.sp < 0)
8635 SAVE_IT (atpos_it, *it, atpos_data);
8636 IT_RESET_X_ASCENT_DESCENT (&atpos_it);
8639 else
8641 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8643 it->current_x = x;
8644 result = MOVE_X_REACHED;
8645 break;
8647 if (atx_it.sp < 0)
8649 SAVE_IT (atx_it, *it, atx_data);
8650 IT_RESET_X_ASCENT_DESCENT (&atx_it);
8655 if (/* Lines are continued. */
8656 it->line_wrap != TRUNCATE
8657 && (/* And glyph doesn't fit on the line. */
8658 new_x > it->last_visible_x
8659 /* Or it fits exactly and we're on a window
8660 system frame. */
8661 || (new_x == it->last_visible_x
8662 && FRAME_WINDOW_P (it->f)
8663 && ((it->bidi_p && it->bidi_it.paragraph_dir == R2L)
8664 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
8665 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)))))
8667 if (/* IT->hpos == 0 means the very first glyph
8668 doesn't fit on the line, e.g. a wide image. */
8669 it->hpos == 0
8670 || (new_x == it->last_visible_x
8671 && FRAME_WINDOW_P (it->f)))
8673 ++it->hpos;
8674 it->current_x = new_x;
8676 /* The character's last glyph just barely fits
8677 in this row. */
8678 if (i == it->nglyphs - 1)
8680 /* If this is the destination position,
8681 return a position *before* it in this row,
8682 now that we know it fits in this row. */
8683 if (BUFFER_POS_REACHED_P ())
8685 if (it->line_wrap != WORD_WRAP
8686 || wrap_it.sp < 0)
8688 it->hpos = hpos_before_this_char;
8689 it->current_x = x_before_this_char;
8690 result = MOVE_POS_MATCH_OR_ZV;
8691 break;
8693 if (it->line_wrap == WORD_WRAP
8694 && atpos_it.sp < 0)
8696 SAVE_IT (atpos_it, *it, atpos_data);
8697 atpos_it.current_x = x_before_this_char;
8698 atpos_it.hpos = hpos_before_this_char;
8702 prev_method = it->method;
8703 if (it->method == GET_FROM_BUFFER)
8704 prev_pos = IT_CHARPOS (*it);
8705 set_iterator_to_next (it, 1);
8706 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8707 SET_TEXT_POS (this_line_min_pos,
8708 IT_CHARPOS (*it), IT_BYTEPOS (*it));
8709 /* On graphical terminals, newlines may
8710 "overflow" into the fringe if
8711 overflow-newline-into-fringe is non-nil.
8712 On text terminals, and on graphical
8713 terminals with no right margin, newlines
8714 may overflow into the last glyph on the
8715 display line.*/
8716 if (!FRAME_WINDOW_P (it->f)
8717 || ((it->bidi_p
8718 && it->bidi_it.paragraph_dir == R2L)
8719 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
8720 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0
8721 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
8723 if (!get_next_display_element (it))
8725 result = MOVE_POS_MATCH_OR_ZV;
8726 break;
8728 if (BUFFER_POS_REACHED_P ())
8730 if (ITERATOR_AT_END_OF_LINE_P (it))
8731 result = MOVE_POS_MATCH_OR_ZV;
8732 else
8733 result = MOVE_LINE_CONTINUED;
8734 break;
8736 if (ITERATOR_AT_END_OF_LINE_P (it)
8737 && (it->line_wrap != WORD_WRAP
8738 || wrap_it.sp < 0
8739 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it)))
8741 result = MOVE_NEWLINE_OR_CR;
8742 break;
8747 else
8748 IT_RESET_X_ASCENT_DESCENT (it);
8750 if (wrap_it.sp >= 0)
8752 RESTORE_IT (it, &wrap_it, wrap_data);
8753 atpos_it.sp = -1;
8754 atx_it.sp = -1;
8757 TRACE_MOVE ((stderr, "move_it_in: continued at %d\n",
8758 IT_CHARPOS (*it)));
8759 result = MOVE_LINE_CONTINUED;
8760 break;
8763 if (BUFFER_POS_REACHED_P ())
8765 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8766 goto buffer_pos_reached;
8767 if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
8769 SAVE_IT (atpos_it, *it, atpos_data);
8770 IT_RESET_X_ASCENT_DESCENT (&atpos_it);
8774 if (new_x > it->first_visible_x)
8776 /* Glyph is visible. Increment number of glyphs that
8777 would be displayed. */
8778 ++it->hpos;
8782 if (result != MOVE_UNDEFINED)
8783 break;
8785 else if (BUFFER_POS_REACHED_P ())
8787 buffer_pos_reached:
8788 IT_RESET_X_ASCENT_DESCENT (it);
8789 result = MOVE_POS_MATCH_OR_ZV;
8790 break;
8792 else if ((op & MOVE_TO_X) && it->current_x >= to_x)
8794 /* Stop when TO_X specified and reached. This check is
8795 necessary here because of lines consisting of a line end,
8796 only. The line end will not produce any glyphs and we
8797 would never get MOVE_X_REACHED. */
8798 eassert (it->nglyphs == 0);
8799 result = MOVE_X_REACHED;
8800 break;
8803 /* Is this a line end? If yes, we're done. */
8804 if (ITERATOR_AT_END_OF_LINE_P (it))
8806 /* If we are past TO_CHARPOS, but never saw any character
8807 positions smaller than TO_CHARPOS, return
8808 MOVE_POS_MATCH_OR_ZV, like the unidirectional display
8809 did. */
8810 if (it->bidi_p && (op & MOVE_TO_POS) != 0)
8812 if (!saw_smaller_pos && IT_CHARPOS (*it) > to_charpos)
8814 if (closest_pos < ZV)
8816 RESTORE_IT (it, &ppos_it, ppos_data);
8817 /* Don't recurse if closest_pos is equal to
8818 to_charpos, since we have just tried that. */
8819 if (closest_pos != to_charpos)
8820 move_it_in_display_line_to (it, closest_pos, -1,
8821 MOVE_TO_POS);
8822 result = MOVE_POS_MATCH_OR_ZV;
8824 else
8825 goto buffer_pos_reached;
8827 else if (it->line_wrap == WORD_WRAP && atpos_it.sp >= 0
8828 && IT_CHARPOS (*it) > to_charpos)
8829 goto buffer_pos_reached;
8830 else
8831 result = MOVE_NEWLINE_OR_CR;
8833 else
8834 result = MOVE_NEWLINE_OR_CR;
8835 break;
8838 prev_method = it->method;
8839 if (it->method == GET_FROM_BUFFER)
8840 prev_pos = IT_CHARPOS (*it);
8841 /* The current display element has been consumed. Advance
8842 to the next. */
8843 set_iterator_to_next (it, 1);
8844 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8845 SET_TEXT_POS (this_line_min_pos, IT_CHARPOS (*it), IT_BYTEPOS (*it));
8846 if (IT_CHARPOS (*it) < to_charpos)
8847 saw_smaller_pos = 1;
8848 if (it->bidi_p
8849 && (op & MOVE_TO_POS)
8850 && IT_CHARPOS (*it) >= to_charpos
8851 && IT_CHARPOS (*it) < closest_pos)
8852 closest_pos = IT_CHARPOS (*it);
8854 /* Stop if lines are truncated and IT's current x-position is
8855 past the right edge of the window now. */
8856 if (it->line_wrap == TRUNCATE
8857 && it->current_x >= it->last_visible_x)
8859 if (!FRAME_WINDOW_P (it->f)
8860 || ((it->bidi_p && it->bidi_it.paragraph_dir == R2L)
8861 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
8862 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0
8863 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
8865 int at_eob_p = 0;
8867 if ((at_eob_p = !get_next_display_element (it))
8868 || BUFFER_POS_REACHED_P ()
8869 /* If we are past TO_CHARPOS, but never saw any
8870 character positions smaller than TO_CHARPOS,
8871 return MOVE_POS_MATCH_OR_ZV, like the
8872 unidirectional display did. */
8873 || (it->bidi_p && (op & MOVE_TO_POS) != 0
8874 && !saw_smaller_pos
8875 && IT_CHARPOS (*it) > to_charpos))
8877 if (it->bidi_p
8878 && !BUFFER_POS_REACHED_P ()
8879 && !at_eob_p && closest_pos < ZV)
8881 RESTORE_IT (it, &ppos_it, ppos_data);
8882 if (closest_pos != to_charpos)
8883 move_it_in_display_line_to (it, closest_pos, -1,
8884 MOVE_TO_POS);
8886 result = MOVE_POS_MATCH_OR_ZV;
8887 break;
8889 if (ITERATOR_AT_END_OF_LINE_P (it))
8891 result = MOVE_NEWLINE_OR_CR;
8892 break;
8895 else if (it->bidi_p && (op & MOVE_TO_POS) != 0
8896 && !saw_smaller_pos
8897 && IT_CHARPOS (*it) > to_charpos)
8899 if (closest_pos < ZV)
8901 RESTORE_IT (it, &ppos_it, ppos_data);
8902 if (closest_pos != to_charpos)
8903 move_it_in_display_line_to (it, closest_pos, -1,
8904 MOVE_TO_POS);
8906 result = MOVE_POS_MATCH_OR_ZV;
8907 break;
8909 result = MOVE_LINE_TRUNCATED;
8910 break;
8912 #undef IT_RESET_X_ASCENT_DESCENT
8915 #undef BUFFER_POS_REACHED_P
8917 /* If we scanned beyond to_pos and didn't find a point to wrap at,
8918 restore the saved iterator. */
8919 if (atpos_it.sp >= 0)
8920 RESTORE_IT (it, &atpos_it, atpos_data);
8921 else if (atx_it.sp >= 0)
8922 RESTORE_IT (it, &atx_it, atx_data);
8924 done:
8926 if (atpos_data)
8927 bidi_unshelve_cache (atpos_data, 1);
8928 if (atx_data)
8929 bidi_unshelve_cache (atx_data, 1);
8930 if (wrap_data)
8931 bidi_unshelve_cache (wrap_data, 1);
8932 if (ppos_data)
8933 bidi_unshelve_cache (ppos_data, 1);
8935 /* Restore the iterator settings altered at the beginning of this
8936 function. */
8937 it->glyph_row = saved_glyph_row;
8938 return result;
8941 /* For external use. */
8942 void
8943 move_it_in_display_line (struct it *it,
8944 ptrdiff_t to_charpos, int to_x,
8945 enum move_operation_enum op)
8947 if (it->line_wrap == WORD_WRAP
8948 && (op & MOVE_TO_X))
8950 struct it save_it;
8951 void *save_data = NULL;
8952 int skip;
8954 SAVE_IT (save_it, *it, save_data);
8955 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
8956 /* When word-wrap is on, TO_X may lie past the end
8957 of a wrapped line. Then it->current is the
8958 character on the next line, so backtrack to the
8959 space before the wrap point. */
8960 if (skip == MOVE_LINE_CONTINUED)
8962 int prev_x = max (it->current_x - 1, 0);
8963 RESTORE_IT (it, &save_it, save_data);
8964 move_it_in_display_line_to
8965 (it, -1, prev_x, MOVE_TO_X);
8967 else
8968 bidi_unshelve_cache (save_data, 1);
8970 else
8971 move_it_in_display_line_to (it, to_charpos, to_x, op);
8975 /* Move IT forward until it satisfies one or more of the criteria in
8976 TO_CHARPOS, TO_X, TO_Y, and TO_VPOS.
8978 OP is a bit-mask that specifies where to stop, and in particular,
8979 which of those four position arguments makes a difference. See the
8980 description of enum move_operation_enum.
8982 If TO_CHARPOS is in invisible text, e.g. a truncated part of a
8983 screen line, this function will set IT to the next position that is
8984 displayed to the right of TO_CHARPOS on the screen.
8986 Return the maximum pixel length of any line scanned but never more
8987 than it.last_visible_x. */
8990 move_it_to (struct it *it, ptrdiff_t to_charpos, int to_x, int to_y, int to_vpos, int op)
8992 enum move_it_result skip, skip2 = MOVE_X_REACHED;
8993 int line_height, line_start_x = 0, reached = 0;
8994 int max_current_x = 0;
8995 void *backup_data = NULL;
8997 for (;;)
8999 if (op & MOVE_TO_VPOS)
9001 /* If no TO_CHARPOS and no TO_X specified, stop at the
9002 start of the line TO_VPOS. */
9003 if ((op & (MOVE_TO_X | MOVE_TO_POS)) == 0)
9005 if (it->vpos == to_vpos)
9007 reached = 1;
9008 break;
9010 else
9011 skip = move_it_in_display_line_to (it, -1, -1, 0);
9013 else
9015 /* TO_VPOS >= 0 means stop at TO_X in the line at
9016 TO_VPOS, or at TO_POS, whichever comes first. */
9017 if (it->vpos == to_vpos)
9019 reached = 2;
9020 break;
9023 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
9025 if (skip == MOVE_POS_MATCH_OR_ZV || it->vpos == to_vpos)
9027 reached = 3;
9028 break;
9030 else if (skip == MOVE_X_REACHED && it->vpos != to_vpos)
9032 /* We have reached TO_X but not in the line we want. */
9033 skip = move_it_in_display_line_to (it, to_charpos,
9034 -1, MOVE_TO_POS);
9035 if (skip == MOVE_POS_MATCH_OR_ZV)
9037 reached = 4;
9038 break;
9043 else if (op & MOVE_TO_Y)
9045 struct it it_backup;
9047 if (it->line_wrap == WORD_WRAP)
9048 SAVE_IT (it_backup, *it, backup_data);
9050 /* TO_Y specified means stop at TO_X in the line containing
9051 TO_Y---or at TO_CHARPOS if this is reached first. The
9052 problem is that we can't really tell whether the line
9053 contains TO_Y before we have completely scanned it, and
9054 this may skip past TO_X. What we do is to first scan to
9055 TO_X.
9057 If TO_X is not specified, use a TO_X of zero. The reason
9058 is to make the outcome of this function more predictable.
9059 If we didn't use TO_X == 0, we would stop at the end of
9060 the line which is probably not what a caller would expect
9061 to happen. */
9062 skip = move_it_in_display_line_to
9063 (it, to_charpos, ((op & MOVE_TO_X) ? to_x : 0),
9064 (MOVE_TO_X | (op & MOVE_TO_POS)));
9066 /* If TO_CHARPOS is reached or ZV, we don't have to do more. */
9067 if (skip == MOVE_POS_MATCH_OR_ZV)
9068 reached = 5;
9069 else if (skip == MOVE_X_REACHED)
9071 /* If TO_X was reached, we want to know whether TO_Y is
9072 in the line. We know this is the case if the already
9073 scanned glyphs make the line tall enough. Otherwise,
9074 we must check by scanning the rest of the line. */
9075 line_height = it->max_ascent + it->max_descent;
9076 if (to_y >= it->current_y
9077 && to_y < it->current_y + line_height)
9079 reached = 6;
9080 break;
9082 SAVE_IT (it_backup, *it, backup_data);
9083 TRACE_MOVE ((stderr, "move_it: from %d\n", IT_CHARPOS (*it)));
9084 skip2 = move_it_in_display_line_to (it, to_charpos, -1,
9085 op & MOVE_TO_POS);
9086 TRACE_MOVE ((stderr, "move_it: to %d\n", IT_CHARPOS (*it)));
9087 line_height = it->max_ascent + it->max_descent;
9088 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
9090 if (to_y >= it->current_y
9091 && to_y < it->current_y + line_height)
9093 /* If TO_Y is in this line and TO_X was reached
9094 above, we scanned too far. We have to restore
9095 IT's settings to the ones before skipping. But
9096 keep the more accurate values of max_ascent and
9097 max_descent we've found while skipping the rest
9098 of the line, for the sake of callers, such as
9099 pos_visible_p, that need to know the line
9100 height. */
9101 int max_ascent = it->max_ascent;
9102 int max_descent = it->max_descent;
9104 RESTORE_IT (it, &it_backup, backup_data);
9105 it->max_ascent = max_ascent;
9106 it->max_descent = max_descent;
9107 reached = 6;
9109 else
9111 skip = skip2;
9112 if (skip == MOVE_POS_MATCH_OR_ZV)
9113 reached = 7;
9116 else
9118 /* Check whether TO_Y is in this line. */
9119 line_height = it->max_ascent + it->max_descent;
9120 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
9122 if (to_y >= it->current_y
9123 && to_y < it->current_y + line_height)
9125 if (to_y > it->current_y)
9126 max_current_x = max (it->current_x, max_current_x);
9128 /* When word-wrap is on, TO_X may lie past the end
9129 of a wrapped line. Then it->current is the
9130 character on the next line, so backtrack to the
9131 space before the wrap point. */
9132 if (skip == MOVE_LINE_CONTINUED
9133 && it->line_wrap == WORD_WRAP)
9135 int prev_x = max (it->current_x - 1, 0);
9136 RESTORE_IT (it, &it_backup, backup_data);
9137 skip = move_it_in_display_line_to
9138 (it, -1, prev_x, MOVE_TO_X);
9141 reached = 6;
9145 if (reached)
9147 max_current_x = max (it->current_x, max_current_x);
9148 break;
9151 else if (BUFFERP (it->object)
9152 && (it->method == GET_FROM_BUFFER
9153 || it->method == GET_FROM_STRETCH)
9154 && IT_CHARPOS (*it) >= to_charpos
9155 /* Under bidi iteration, a call to set_iterator_to_next
9156 can scan far beyond to_charpos if the initial
9157 portion of the next line needs to be reordered. In
9158 that case, give move_it_in_display_line_to another
9159 chance below. */
9160 && !(it->bidi_p
9161 && it->bidi_it.scan_dir == -1))
9162 skip = MOVE_POS_MATCH_OR_ZV;
9163 else
9164 skip = move_it_in_display_line_to (it, to_charpos, -1, MOVE_TO_POS);
9166 switch (skip)
9168 case MOVE_POS_MATCH_OR_ZV:
9169 max_current_x = max (it->current_x, max_current_x);
9170 reached = 8;
9171 goto out;
9173 case MOVE_NEWLINE_OR_CR:
9174 max_current_x = max (it->current_x, max_current_x);
9175 set_iterator_to_next (it, 1);
9176 it->continuation_lines_width = 0;
9177 break;
9179 case MOVE_LINE_TRUNCATED:
9180 max_current_x = it->last_visible_x;
9181 it->continuation_lines_width = 0;
9182 reseat_at_next_visible_line_start (it, 0);
9183 if ((op & MOVE_TO_POS) != 0
9184 && IT_CHARPOS (*it) > to_charpos)
9186 reached = 9;
9187 goto out;
9189 break;
9191 case MOVE_LINE_CONTINUED:
9192 max_current_x = it->last_visible_x;
9193 /* For continued lines ending in a tab, some of the glyphs
9194 associated with the tab are displayed on the current
9195 line. Since it->current_x does not include these glyphs,
9196 we use it->last_visible_x instead. */
9197 if (it->c == '\t')
9199 it->continuation_lines_width += it->last_visible_x;
9200 /* When moving by vpos, ensure that the iterator really
9201 advances to the next line (bug#847, bug#969). Fixme:
9202 do we need to do this in other circumstances? */
9203 if (it->current_x != it->last_visible_x
9204 && (op & MOVE_TO_VPOS)
9205 && !(op & (MOVE_TO_X | MOVE_TO_POS)))
9207 line_start_x = it->current_x + it->pixel_width
9208 - it->last_visible_x;
9209 if (FRAME_WINDOW_P (it->f))
9211 struct face *face = FACE_FROM_ID (it->f, it->face_id);
9212 struct font *face_font = face->font;
9214 /* When display_line produces a continued line
9215 that ends in a TAB, it skips a tab stop that
9216 is closer than the font's space character
9217 width (see x_produce_glyphs where it produces
9218 the stretch glyph which represents a TAB).
9219 We need to reproduce the same logic here. */
9220 eassert (face_font);
9221 if (face_font)
9223 if (line_start_x < face_font->space_width)
9224 line_start_x
9225 += it->tab_width * face_font->space_width;
9228 set_iterator_to_next (it, 0);
9231 else
9232 it->continuation_lines_width += it->current_x;
9233 break;
9235 default:
9236 emacs_abort ();
9239 /* Reset/increment for the next run. */
9240 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
9241 it->current_x = line_start_x;
9242 line_start_x = 0;
9243 it->hpos = 0;
9244 it->current_y += it->max_ascent + it->max_descent;
9245 ++it->vpos;
9246 last_height = it->max_ascent + it->max_descent;
9247 it->max_ascent = it->max_descent = 0;
9250 out:
9252 /* On text terminals, we may stop at the end of a line in the middle
9253 of a multi-character glyph. If the glyph itself is continued,
9254 i.e. it is actually displayed on the next line, don't treat this
9255 stopping point as valid; move to the next line instead (unless
9256 that brings us offscreen). */
9257 if (!FRAME_WINDOW_P (it->f)
9258 && op & MOVE_TO_POS
9259 && IT_CHARPOS (*it) == to_charpos
9260 && it->what == IT_CHARACTER
9261 && it->nglyphs > 1
9262 && it->line_wrap == WINDOW_WRAP
9263 && it->current_x == it->last_visible_x - 1
9264 && it->c != '\n'
9265 && it->c != '\t'
9266 && it->w->window_end_valid
9267 && it->vpos < it->w->window_end_vpos)
9269 it->continuation_lines_width += it->current_x;
9270 it->current_x = it->hpos = it->max_ascent = it->max_descent = 0;
9271 it->current_y += it->max_ascent + it->max_descent;
9272 ++it->vpos;
9273 last_height = it->max_ascent + it->max_descent;
9276 if (backup_data)
9277 bidi_unshelve_cache (backup_data, 1);
9279 TRACE_MOVE ((stderr, "move_it_to: reached %d\n", reached));
9281 return max_current_x;
9285 /* Move iterator IT backward by a specified y-distance DY, DY >= 0.
9287 If DY > 0, move IT backward at least that many pixels. DY = 0
9288 means move IT backward to the preceding line start or BEGV. This
9289 function may move over more than DY pixels if IT->current_y - DY
9290 ends up in the middle of a line; in this case IT->current_y will be
9291 set to the top of the line moved to. */
9293 void
9294 move_it_vertically_backward (struct it *it, int dy)
9296 int nlines, h;
9297 struct it it2, it3;
9298 void *it2data = NULL, *it3data = NULL;
9299 ptrdiff_t start_pos;
9300 int nchars_per_row
9301 = (it->last_visible_x - it->first_visible_x) / FRAME_COLUMN_WIDTH (it->f);
9302 ptrdiff_t pos_limit;
9304 move_further_back:
9305 eassert (dy >= 0);
9307 start_pos = IT_CHARPOS (*it);
9309 /* Estimate how many newlines we must move back. */
9310 nlines = max (1, dy / default_line_pixel_height (it->w));
9311 if (it->line_wrap == TRUNCATE || nchars_per_row == 0)
9312 pos_limit = BEGV;
9313 else
9314 pos_limit = max (start_pos - nlines * nchars_per_row, BEGV);
9316 /* Set the iterator's position that many lines back. But don't go
9317 back more than NLINES full screen lines -- this wins a day with
9318 buffers which have very long lines. */
9319 while (nlines-- && IT_CHARPOS (*it) > pos_limit)
9320 back_to_previous_visible_line_start (it);
9322 /* Reseat the iterator here. When moving backward, we don't want
9323 reseat to skip forward over invisible text, set up the iterator
9324 to deliver from overlay strings at the new position etc. So,
9325 use reseat_1 here. */
9326 reseat_1 (it, it->current.pos, 1);
9328 /* We are now surely at a line start. */
9329 it->current_x = it->hpos = 0; /* FIXME: this is incorrect when bidi
9330 reordering is in effect. */
9331 it->continuation_lines_width = 0;
9333 /* Move forward and see what y-distance we moved. First move to the
9334 start of the next line so that we get its height. We need this
9335 height to be able to tell whether we reached the specified
9336 y-distance. */
9337 SAVE_IT (it2, *it, it2data);
9338 it2.max_ascent = it2.max_descent = 0;
9341 move_it_to (&it2, start_pos, -1, -1, it2.vpos + 1,
9342 MOVE_TO_POS | MOVE_TO_VPOS);
9344 while (!(IT_POS_VALID_AFTER_MOVE_P (&it2)
9345 /* If we are in a display string which starts at START_POS,
9346 and that display string includes a newline, and we are
9347 right after that newline (i.e. at the beginning of a
9348 display line), exit the loop, because otherwise we will
9349 infloop, since move_it_to will see that it is already at
9350 START_POS and will not move. */
9351 || (it2.method == GET_FROM_STRING
9352 && IT_CHARPOS (it2) == start_pos
9353 && SREF (it2.string, IT_STRING_BYTEPOS (it2) - 1) == '\n')));
9354 eassert (IT_CHARPOS (*it) >= BEGV);
9355 SAVE_IT (it3, it2, it3data);
9357 move_it_to (&it2, start_pos, -1, -1, -1, MOVE_TO_POS);
9358 eassert (IT_CHARPOS (*it) >= BEGV);
9359 /* H is the actual vertical distance from the position in *IT
9360 and the starting position. */
9361 h = it2.current_y - it->current_y;
9362 /* NLINES is the distance in number of lines. */
9363 nlines = it2.vpos - it->vpos;
9365 /* Correct IT's y and vpos position
9366 so that they are relative to the starting point. */
9367 it->vpos -= nlines;
9368 it->current_y -= h;
9370 if (dy == 0)
9372 /* DY == 0 means move to the start of the screen line. The
9373 value of nlines is > 0 if continuation lines were involved,
9374 or if the original IT position was at start of a line. */
9375 RESTORE_IT (it, it, it2data);
9376 if (nlines > 0)
9377 move_it_by_lines (it, nlines);
9378 /* The above code moves us to some position NLINES down,
9379 usually to its first glyph (leftmost in an L2R line), but
9380 that's not necessarily the start of the line, under bidi
9381 reordering. We want to get to the character position
9382 that is immediately after the newline of the previous
9383 line. */
9384 if (it->bidi_p
9385 && !it->continuation_lines_width
9386 && !STRINGP (it->string)
9387 && IT_CHARPOS (*it) > BEGV
9388 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
9390 ptrdiff_t cp = IT_CHARPOS (*it), bp = IT_BYTEPOS (*it);
9392 DEC_BOTH (cp, bp);
9393 cp = find_newline_no_quit (cp, bp, -1, NULL);
9394 move_it_to (it, cp, -1, -1, -1, MOVE_TO_POS);
9396 bidi_unshelve_cache (it3data, 1);
9398 else
9400 /* The y-position we try to reach, relative to *IT.
9401 Note that H has been subtracted in front of the if-statement. */
9402 int target_y = it->current_y + h - dy;
9403 int y0 = it3.current_y;
9404 int y1;
9405 int line_height;
9407 RESTORE_IT (&it3, &it3, it3data);
9408 y1 = line_bottom_y (&it3);
9409 line_height = y1 - y0;
9410 RESTORE_IT (it, it, it2data);
9411 /* If we did not reach target_y, try to move further backward if
9412 we can. If we moved too far backward, try to move forward. */
9413 if (target_y < it->current_y
9414 /* This is heuristic. In a window that's 3 lines high, with
9415 a line height of 13 pixels each, recentering with point
9416 on the bottom line will try to move -39/2 = 19 pixels
9417 backward. Try to avoid moving into the first line. */
9418 && (it->current_y - target_y
9419 > min (window_box_height (it->w), line_height * 2 / 3))
9420 && IT_CHARPOS (*it) > BEGV)
9422 TRACE_MOVE ((stderr, " not far enough -> move_vert %d\n",
9423 target_y - it->current_y));
9424 dy = it->current_y - target_y;
9425 goto move_further_back;
9427 else if (target_y >= it->current_y + line_height
9428 && IT_CHARPOS (*it) < ZV)
9430 /* Should move forward by at least one line, maybe more.
9432 Note: Calling move_it_by_lines can be expensive on
9433 terminal frames, where compute_motion is used (via
9434 vmotion) to do the job, when there are very long lines
9435 and truncate-lines is nil. That's the reason for
9436 treating terminal frames specially here. */
9438 if (!FRAME_WINDOW_P (it->f))
9439 move_it_vertically (it, target_y - (it->current_y + line_height));
9440 else
9444 move_it_by_lines (it, 1);
9446 while (target_y >= line_bottom_y (it) && IT_CHARPOS (*it) < ZV);
9453 /* Move IT by a specified amount of pixel lines DY. DY negative means
9454 move backwards. DY = 0 means move to start of screen line. At the
9455 end, IT will be on the start of a screen line. */
9457 void
9458 move_it_vertically (struct it *it, int dy)
9460 if (dy <= 0)
9461 move_it_vertically_backward (it, -dy);
9462 else
9464 TRACE_MOVE ((stderr, "move_it_v: from %d, %d\n", IT_CHARPOS (*it), dy));
9465 move_it_to (it, ZV, -1, it->current_y + dy, -1,
9466 MOVE_TO_POS | MOVE_TO_Y);
9467 TRACE_MOVE ((stderr, "move_it_v: to %d\n", IT_CHARPOS (*it)));
9469 /* If buffer ends in ZV without a newline, move to the start of
9470 the line to satisfy the post-condition. */
9471 if (IT_CHARPOS (*it) == ZV
9472 && ZV > BEGV
9473 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
9474 move_it_by_lines (it, 0);
9479 /* Move iterator IT past the end of the text line it is in. */
9481 void
9482 move_it_past_eol (struct it *it)
9484 enum move_it_result rc;
9486 rc = move_it_in_display_line_to (it, Z, 0, MOVE_TO_POS);
9487 if (rc == MOVE_NEWLINE_OR_CR)
9488 set_iterator_to_next (it, 0);
9492 /* Move IT by a specified number DVPOS of screen lines down. DVPOS
9493 negative means move up. DVPOS == 0 means move to the start of the
9494 screen line.
9496 Optimization idea: If we would know that IT->f doesn't use
9497 a face with proportional font, we could be faster for
9498 truncate-lines nil. */
9500 void
9501 move_it_by_lines (struct it *it, ptrdiff_t dvpos)
9504 /* The commented-out optimization uses vmotion on terminals. This
9505 gives bad results, because elements like it->what, on which
9506 callers such as pos_visible_p rely, aren't updated. */
9507 /* struct position pos;
9508 if (!FRAME_WINDOW_P (it->f))
9510 struct text_pos textpos;
9512 pos = *vmotion (IT_CHARPOS (*it), dvpos, it->w);
9513 SET_TEXT_POS (textpos, pos.bufpos, pos.bytepos);
9514 reseat (it, textpos, 1);
9515 it->vpos += pos.vpos;
9516 it->current_y += pos.vpos;
9518 else */
9520 if (dvpos == 0)
9522 /* DVPOS == 0 means move to the start of the screen line. */
9523 move_it_vertically_backward (it, 0);
9524 /* Let next call to line_bottom_y calculate real line height. */
9525 last_height = 0;
9527 else if (dvpos > 0)
9529 move_it_to (it, -1, -1, -1, it->vpos + dvpos, MOVE_TO_VPOS);
9530 if (!IT_POS_VALID_AFTER_MOVE_P (it))
9532 /* Only move to the next buffer position if we ended up in a
9533 string from display property, not in an overlay string
9534 (before-string or after-string). That is because the
9535 latter don't conceal the underlying buffer position, so
9536 we can ask to move the iterator to the exact position we
9537 are interested in. Note that, even if we are already at
9538 IT_CHARPOS (*it), the call below is not a no-op, as it
9539 will detect that we are at the end of the string, pop the
9540 iterator, and compute it->current_x and it->hpos
9541 correctly. */
9542 move_it_to (it, IT_CHARPOS (*it) + it->string_from_display_prop_p,
9543 -1, -1, -1, MOVE_TO_POS);
9546 else
9548 struct it it2;
9549 void *it2data = NULL;
9550 ptrdiff_t start_charpos, i;
9551 int nchars_per_row
9552 = (it->last_visible_x - it->first_visible_x) / FRAME_COLUMN_WIDTH (it->f);
9553 bool hit_pos_limit = false;
9554 ptrdiff_t pos_limit;
9556 /* Start at the beginning of the screen line containing IT's
9557 position. This may actually move vertically backwards,
9558 in case of overlays, so adjust dvpos accordingly. */
9559 dvpos += it->vpos;
9560 move_it_vertically_backward (it, 0);
9561 dvpos -= it->vpos;
9563 /* Go back -DVPOS buffer lines, but no farther than -DVPOS full
9564 screen lines, and reseat the iterator there. */
9565 start_charpos = IT_CHARPOS (*it);
9566 if (it->line_wrap == TRUNCATE || nchars_per_row == 0)
9567 pos_limit = BEGV;
9568 else
9569 pos_limit = max (start_charpos + dvpos * nchars_per_row, BEGV);
9571 for (i = -dvpos; i > 0 && IT_CHARPOS (*it) > pos_limit; --i)
9572 back_to_previous_visible_line_start (it);
9573 if (i > 0 && IT_CHARPOS (*it) <= pos_limit)
9574 hit_pos_limit = true;
9575 reseat (it, it->current.pos, 1);
9577 /* Move further back if we end up in a string or an image. */
9578 while (!IT_POS_VALID_AFTER_MOVE_P (it))
9580 /* First try to move to start of display line. */
9581 dvpos += it->vpos;
9582 move_it_vertically_backward (it, 0);
9583 dvpos -= it->vpos;
9584 if (IT_POS_VALID_AFTER_MOVE_P (it))
9585 break;
9586 /* If start of line is still in string or image,
9587 move further back. */
9588 back_to_previous_visible_line_start (it);
9589 reseat (it, it->current.pos, 1);
9590 dvpos--;
9593 it->current_x = it->hpos = 0;
9595 /* Above call may have moved too far if continuation lines
9596 are involved. Scan forward and see if it did. */
9597 SAVE_IT (it2, *it, it2data);
9598 it2.vpos = it2.current_y = 0;
9599 move_it_to (&it2, start_charpos, -1, -1, -1, MOVE_TO_POS);
9600 it->vpos -= it2.vpos;
9601 it->current_y -= it2.current_y;
9602 it->current_x = it->hpos = 0;
9604 /* If we moved too far back, move IT some lines forward. */
9605 if (it2.vpos > -dvpos)
9607 int delta = it2.vpos + dvpos;
9609 RESTORE_IT (&it2, &it2, it2data);
9610 SAVE_IT (it2, *it, it2data);
9611 move_it_to (it, -1, -1, -1, it->vpos + delta, MOVE_TO_VPOS);
9612 /* Move back again if we got too far ahead. */
9613 if (IT_CHARPOS (*it) >= start_charpos)
9614 RESTORE_IT (it, &it2, it2data);
9615 else
9616 bidi_unshelve_cache (it2data, 1);
9618 else if (hit_pos_limit && pos_limit > BEGV
9619 && dvpos < 0 && it2.vpos < -dvpos)
9621 /* If we hit the limit, but still didn't make it far enough
9622 back, that means there's a display string with a newline
9623 covering a large chunk of text, and that caused
9624 back_to_previous_visible_line_start try to go too far.
9625 Punish those who commit such atrocities by going back
9626 until we've reached DVPOS, after lifting the limit, which
9627 could make it slow for very long lines. "If it hurts,
9628 don't do that!" */
9629 dvpos += it2.vpos;
9630 RESTORE_IT (it, it, it2data);
9631 for (i = -dvpos; i > 0; --i)
9633 back_to_previous_visible_line_start (it);
9634 it->vpos--;
9636 reseat_1 (it, it->current.pos, 1);
9638 else
9639 RESTORE_IT (it, it, it2data);
9643 /* Return true if IT points into the middle of a display vector. */
9645 bool
9646 in_display_vector_p (struct it *it)
9648 return (it->method == GET_FROM_DISPLAY_VECTOR
9649 && it->current.dpvec_index > 0
9650 && it->dpvec + it->current.dpvec_index != it->dpend);
9653 DEFUN ("window-text-pixel-size", Fwindow_text_pixel_size, Swindow_text_pixel_size, 0, 7, 0,
9654 doc: /* Return the size of the text of WINDOW's buffer in pixels.
9655 WINDOW must be a live window and defaults to the selected one. The
9656 return value is a cons of the maximum pixel-width of any text line and
9657 the maximum pixel-height of all text lines.
9659 The optional argument FROM, if non-nil, specifies the first text
9660 position and defaults to the minimum accessible position of the buffer.
9661 If FROM is t, use the minimum accessible position that is not a newline
9662 character. TO, if non-nil, specifies the last text position and
9663 defaults to the maximum accessible position of the buffer. If TO is t,
9664 use the maximum accessible position that is not a newline character.
9666 The optional argument X-LIMIT, if non-nil, specifies the maximum text
9667 width that can be returned. X-LIMIT nil or omitted, means to use the
9668 pixel-width of WINDOW's body; use this if you do not intend to change
9669 the width of WINDOW. Use the maximum width WINDOW may assume if you
9670 intend to change WINDOW's width. In any case, text whose x-coordinate
9671 is beyond X-LIMIT is ignored. Since calculating the width of long lines
9672 can take some time, it's always a good idea to make this argument as
9673 small as possible; in particular, if the buffer contains long lines that
9674 shall be truncated anyway.
9676 The optional argument Y-LIMIT, if non-nil, specifies the maximum text
9677 height that can be returned. Text lines whose y-coordinate is beyond
9678 Y-LIMIT are ignored. Since calculating the text height of a large
9679 buffer can take some time, it makes sense to specify this argument if
9680 the size of the buffer is unknown.
9682 Optional argument MODE-AND-HEADER-LINE nil or omitted means do not
9683 include the height of the mode- or header-line of WINDOW in the return
9684 value. If it is either the symbol `mode-line' or `header-line', include
9685 only the height of that line, if present, in the return value. If t,
9686 include the height of both, if present, in the return value.
9688 Optional argument BUFFER nil means to return the size of the text of
9689 WINDOW's buffer. BUFFER t means to return the size of the text of the
9690 current buffer as if it were displayed in WINDOW. Else BUFFER has to
9691 specify a live buffer and this function returns the size of the text of
9692 BUFFER as if it were displayed in WINDOW. */)
9693 (Lisp_Object window, Lisp_Object from, Lisp_Object to, Lisp_Object x_limit,
9694 Lisp_Object y_limit, Lisp_Object mode_and_header_line, Lisp_Object buffer)
9696 struct window *w = decode_live_window (window);
9697 struct buffer *b;
9698 struct it it;
9699 struct buffer *old_b = NULL;
9700 ptrdiff_t start, end, pos;
9701 struct text_pos startp;
9702 void *itdata = NULL;
9703 int c, max_y = -1, x = 0, y = 0;
9705 if (EQ (buffer, Qt))
9706 b = current_buffer;
9707 else
9709 if (NILP (buffer))
9710 buffer = w->contents;
9712 CHECK_BUFFER (buffer);
9713 if (!BUFFER_LIVE_P (XBUFFER (buffer)))
9714 error ("Not a live buffer");
9716 b = XBUFFER (buffer);
9717 if (b != current_buffer)
9719 old_b = current_buffer;
9720 set_buffer_internal (b);
9724 if (NILP (from))
9725 start = BEGV;
9726 else if (EQ (from, Qt))
9728 start = pos = BEGV;
9729 while ((pos++ < ZV) && (c = FETCH_CHAR (pos))
9730 && (c == ' ' || c == '\t' || c == '\n' || c == '\r'))
9731 start = pos;
9732 while ((pos-- > BEGV) && (c = FETCH_CHAR (pos)) && (c == ' ' || c == '\t'))
9733 start = pos;
9735 else
9737 CHECK_NUMBER_COERCE_MARKER (from);
9738 start = min (max (XINT (from), BEGV), ZV);
9741 if (NILP (to))
9742 end = ZV;
9743 else if (EQ (to, Qt))
9745 end = pos = ZV;
9746 while ((pos-- > BEGV) && (c = FETCH_CHAR (pos))
9747 && (c == ' ' || c == '\t' || c == '\n' || c == '\r'))
9748 end = pos;
9749 while ((pos++ < ZV) && (c = FETCH_CHAR (pos)) && (c == ' ' || c == '\t'))
9750 end = pos;
9752 else
9754 CHECK_NUMBER_COERCE_MARKER (to);
9755 end = max (start, min (XINT (to), ZV));
9758 if (!NILP (y_limit))
9760 CHECK_NUMBER (y_limit);
9761 max_y = min (XINT (y_limit), INT_MAX);
9764 itdata = bidi_shelve_cache ();
9765 SET_TEXT_POS (startp, start, CHAR_TO_BYTE (start));
9766 start_display (&it, w, startp);
9768 if (NILP (x_limit))
9769 x = move_it_to (&it, end, -1, max_y, -1, MOVE_TO_POS | MOVE_TO_Y);
9770 else
9772 CHECK_NUMBER (x_limit);
9773 it.last_visible_x = min (XINT (x_limit), INFINITY);
9774 /* Actually, we never want move_it_to stop at to_x. But to make
9775 sure that move_it_in_display_line_to always moves far enough,
9776 we set it to INT_MAX and specify MOVE_TO_X. */
9777 x = move_it_to (&it, end, INT_MAX, max_y, -1,
9778 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
9781 y = it.current_y + it.max_ascent + it.max_descent;
9783 if (!EQ (mode_and_header_line, Qheader_line)
9784 && !EQ (mode_and_header_line, Qt))
9785 /* Do not count the header-line which was counted automatically by
9786 start_display. */
9787 y = y - WINDOW_HEADER_LINE_HEIGHT (w);
9789 if (EQ (mode_and_header_line, Qmode_line)
9790 || EQ (mode_and_header_line, Qt))
9791 /* Do count the mode-line which is not included automatically by
9792 start_display. */
9793 y = y + WINDOW_MODE_LINE_HEIGHT (w);
9795 bidi_unshelve_cache (itdata, 0);
9797 if (old_b)
9798 set_buffer_internal (old_b);
9800 return Fcons (make_number (x), make_number (y));
9803 /***********************************************************************
9804 Messages
9805 ***********************************************************************/
9808 /* Add a message with format string FORMAT and arguments ARG1 and ARG2
9809 to *Messages*. */
9811 void
9812 add_to_log (const char *format, Lisp_Object arg1, Lisp_Object arg2)
9814 Lisp_Object msg, fmt;
9815 char *buffer;
9816 ptrdiff_t len;
9817 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
9818 USE_SAFE_ALLOCA;
9820 fmt = msg = Qnil;
9821 GCPRO4 (fmt, msg, arg1, arg2);
9823 fmt = build_string (format);
9824 msg = CALLN (Fformat, fmt, arg1, arg2);
9826 len = SBYTES (msg) + 1;
9827 buffer = SAFE_ALLOCA (len);
9828 memcpy (buffer, SDATA (msg), len);
9830 message_dolog (buffer, len - 1, 1, 0);
9831 SAFE_FREE ();
9833 UNGCPRO;
9837 /* Output a newline in the *Messages* buffer if "needs" one. */
9839 void
9840 message_log_maybe_newline (void)
9842 if (message_log_need_newline)
9843 message_dolog ("", 0, 1, 0);
9847 /* Add a string M of length NBYTES to the message log, optionally
9848 terminated with a newline when NLFLAG is true. MULTIBYTE, if
9849 true, means interpret the contents of M as multibyte. This
9850 function calls low-level routines in order to bypass text property
9851 hooks, etc. which might not be safe to run.
9853 This may GC (insert may run before/after change hooks),
9854 so the buffer M must NOT point to a Lisp string. */
9856 void
9857 message_dolog (const char *m, ptrdiff_t nbytes, bool nlflag, bool multibyte)
9859 const unsigned char *msg = (const unsigned char *) m;
9861 if (!NILP (Vmemory_full))
9862 return;
9864 if (!NILP (Vmessage_log_max))
9866 struct buffer *oldbuf;
9867 Lisp_Object oldpoint, oldbegv, oldzv;
9868 int old_windows_or_buffers_changed = windows_or_buffers_changed;
9869 ptrdiff_t point_at_end = 0;
9870 ptrdiff_t zv_at_end = 0;
9871 Lisp_Object old_deactivate_mark;
9872 struct gcpro gcpro1;
9874 old_deactivate_mark = Vdeactivate_mark;
9875 oldbuf = current_buffer;
9877 /* Ensure the Messages buffer exists, and switch to it.
9878 If we created it, set the major-mode. */
9880 int newbuffer = 0;
9881 if (NILP (Fget_buffer (Vmessages_buffer_name))) newbuffer = 1;
9883 Fset_buffer (Fget_buffer_create (Vmessages_buffer_name));
9885 if (newbuffer
9886 && !NILP (Ffboundp (intern ("messages-buffer-mode"))))
9887 call0 (intern ("messages-buffer-mode"));
9890 bset_undo_list (current_buffer, Qt);
9891 bset_cache_long_scans (current_buffer, Qnil);
9893 oldpoint = message_dolog_marker1;
9894 set_marker_restricted_both (oldpoint, Qnil, PT, PT_BYTE);
9895 oldbegv = message_dolog_marker2;
9896 set_marker_restricted_both (oldbegv, Qnil, BEGV, BEGV_BYTE);
9897 oldzv = message_dolog_marker3;
9898 set_marker_restricted_both (oldzv, Qnil, ZV, ZV_BYTE);
9899 GCPRO1 (old_deactivate_mark);
9901 if (PT == Z)
9902 point_at_end = 1;
9903 if (ZV == Z)
9904 zv_at_end = 1;
9906 BEGV = BEG;
9907 BEGV_BYTE = BEG_BYTE;
9908 ZV = Z;
9909 ZV_BYTE = Z_BYTE;
9910 TEMP_SET_PT_BOTH (Z, Z_BYTE);
9912 /* Insert the string--maybe converting multibyte to single byte
9913 or vice versa, so that all the text fits the buffer. */
9914 if (multibyte
9915 && NILP (BVAR (current_buffer, enable_multibyte_characters)))
9917 ptrdiff_t i;
9918 int c, char_bytes;
9919 char work[1];
9921 /* Convert a multibyte string to single-byte
9922 for the *Message* buffer. */
9923 for (i = 0; i < nbytes; i += char_bytes)
9925 c = string_char_and_length (msg + i, &char_bytes);
9926 work[0] = CHAR_TO_BYTE8 (c);
9927 insert_1_both (work, 1, 1, 1, 0, 0);
9930 else if (! multibyte
9931 && ! NILP (BVAR (current_buffer, enable_multibyte_characters)))
9933 ptrdiff_t i;
9934 int c, char_bytes;
9935 unsigned char str[MAX_MULTIBYTE_LENGTH];
9936 /* Convert a single-byte string to multibyte
9937 for the *Message* buffer. */
9938 for (i = 0; i < nbytes; i++)
9940 c = msg[i];
9941 MAKE_CHAR_MULTIBYTE (c);
9942 char_bytes = CHAR_STRING (c, str);
9943 insert_1_both ((char *) str, 1, char_bytes, 1, 0, 0);
9946 else if (nbytes)
9947 insert_1_both (m, chars_in_text (msg, nbytes), nbytes, 1, 0, 0);
9949 if (nlflag)
9951 ptrdiff_t this_bol, this_bol_byte, prev_bol, prev_bol_byte;
9952 printmax_t dups;
9954 insert_1_both ("\n", 1, 1, 1, 0, 0);
9956 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE, -2, 0);
9957 this_bol = PT;
9958 this_bol_byte = PT_BYTE;
9960 /* See if this line duplicates the previous one.
9961 If so, combine duplicates. */
9962 if (this_bol > BEG)
9964 scan_newline (PT, PT_BYTE, BEG, BEG_BYTE, -2, 0);
9965 prev_bol = PT;
9966 prev_bol_byte = PT_BYTE;
9968 dups = message_log_check_duplicate (prev_bol_byte,
9969 this_bol_byte);
9970 if (dups)
9972 del_range_both (prev_bol, prev_bol_byte,
9973 this_bol, this_bol_byte, 0);
9974 if (dups > 1)
9976 char dupstr[sizeof " [ times]"
9977 + INT_STRLEN_BOUND (printmax_t)];
9979 /* If you change this format, don't forget to also
9980 change message_log_check_duplicate. */
9981 int duplen = sprintf (dupstr, " [%"pMd" times]", dups);
9982 TEMP_SET_PT_BOTH (Z - 1, Z_BYTE - 1);
9983 insert_1_both (dupstr, duplen, duplen, 1, 0, 1);
9988 /* If we have more than the desired maximum number of lines
9989 in the *Messages* buffer now, delete the oldest ones.
9990 This is safe because we don't have undo in this buffer. */
9992 if (NATNUMP (Vmessage_log_max))
9994 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE,
9995 -XFASTINT (Vmessage_log_max) - 1, 0);
9996 del_range_both (BEG, BEG_BYTE, PT, PT_BYTE, 0);
9999 BEGV = marker_position (oldbegv);
10000 BEGV_BYTE = marker_byte_position (oldbegv);
10002 if (zv_at_end)
10004 ZV = Z;
10005 ZV_BYTE = Z_BYTE;
10007 else
10009 ZV = marker_position (oldzv);
10010 ZV_BYTE = marker_byte_position (oldzv);
10013 if (point_at_end)
10014 TEMP_SET_PT_BOTH (Z, Z_BYTE);
10015 else
10016 /* We can't do Fgoto_char (oldpoint) because it will run some
10017 Lisp code. */
10018 TEMP_SET_PT_BOTH (marker_position (oldpoint),
10019 marker_byte_position (oldpoint));
10021 UNGCPRO;
10022 unchain_marker (XMARKER (oldpoint));
10023 unchain_marker (XMARKER (oldbegv));
10024 unchain_marker (XMARKER (oldzv));
10026 /* We called insert_1_both above with its 5th argument (PREPARE)
10027 zero, which prevents insert_1_both from calling
10028 prepare_to_modify_buffer, which in turns prevents us from
10029 incrementing windows_or_buffers_changed even if *Messages* is
10030 shown in some window. So we must manually set
10031 windows_or_buffers_changed here to make up for that. */
10032 windows_or_buffers_changed = old_windows_or_buffers_changed;
10033 bset_redisplay (current_buffer);
10035 set_buffer_internal (oldbuf);
10037 message_log_need_newline = !nlflag;
10038 Vdeactivate_mark = old_deactivate_mark;
10043 /* We are at the end of the buffer after just having inserted a newline.
10044 (Note: We depend on the fact we won't be crossing the gap.)
10045 Check to see if the most recent message looks a lot like the previous one.
10046 Return 0 if different, 1 if the new one should just replace it, or a
10047 value N > 1 if we should also append " [N times]". */
10049 static intmax_t
10050 message_log_check_duplicate (ptrdiff_t prev_bol_byte, ptrdiff_t this_bol_byte)
10052 ptrdiff_t i;
10053 ptrdiff_t len = Z_BYTE - 1 - this_bol_byte;
10054 int seen_dots = 0;
10055 unsigned char *p1 = BUF_BYTE_ADDRESS (current_buffer, prev_bol_byte);
10056 unsigned char *p2 = BUF_BYTE_ADDRESS (current_buffer, this_bol_byte);
10058 for (i = 0; i < len; i++)
10060 if (i >= 3 && p1[i - 3] == '.' && p1[i - 2] == '.' && p1[i - 1] == '.')
10061 seen_dots = 1;
10062 if (p1[i] != p2[i])
10063 return seen_dots;
10065 p1 += len;
10066 if (*p1 == '\n')
10067 return 2;
10068 if (*p1++ == ' ' && *p1++ == '[')
10070 char *pend;
10071 intmax_t n = strtoimax ((char *) p1, &pend, 10);
10072 if (0 < n && n < INTMAX_MAX && strncmp (pend, " times]\n", 8) == 0)
10073 return n + 1;
10075 return 0;
10079 /* Display an echo area message M with a specified length of NBYTES
10080 bytes. The string may include null characters. If M is not a
10081 string, clear out any existing message, and let the mini-buffer
10082 text show through.
10084 This function cancels echoing. */
10086 void
10087 message3 (Lisp_Object m)
10089 struct gcpro gcpro1;
10091 GCPRO1 (m);
10092 clear_message (true, true);
10093 cancel_echoing ();
10095 /* First flush out any partial line written with print. */
10096 message_log_maybe_newline ();
10097 if (STRINGP (m))
10099 ptrdiff_t nbytes = SBYTES (m);
10100 bool multibyte = STRING_MULTIBYTE (m);
10101 char *buffer;
10102 USE_SAFE_ALLOCA;
10103 SAFE_ALLOCA_STRING (buffer, m);
10104 message_dolog (buffer, nbytes, 1, multibyte);
10105 SAFE_FREE ();
10107 message3_nolog (m);
10109 UNGCPRO;
10113 /* The non-logging version of message3.
10114 This does not cancel echoing, because it is used for echoing.
10115 Perhaps we need to make a separate function for echoing
10116 and make this cancel echoing. */
10118 void
10119 message3_nolog (Lisp_Object m)
10121 struct frame *sf = SELECTED_FRAME ();
10123 if (FRAME_INITIAL_P (sf))
10125 if (noninteractive_need_newline)
10126 putc ('\n', stderr);
10127 noninteractive_need_newline = 0;
10128 if (STRINGP (m))
10130 Lisp_Object s = ENCODE_SYSTEM (m);
10132 fwrite (SDATA (s), SBYTES (s), 1, stderr);
10134 if (cursor_in_echo_area == 0)
10135 fprintf (stderr, "\n");
10136 fflush (stderr);
10138 /* Error messages get reported properly by cmd_error, so this must be just an
10139 informative message; if the frame hasn't really been initialized yet, just
10140 toss it. */
10141 else if (INTERACTIVE && sf->glyphs_initialized_p)
10143 /* Get the frame containing the mini-buffer
10144 that the selected frame is using. */
10145 Lisp_Object mini_window = FRAME_MINIBUF_WINDOW (sf);
10146 Lisp_Object frame = XWINDOW (mini_window)->frame;
10147 struct frame *f = XFRAME (frame);
10149 if (FRAME_VISIBLE_P (sf) && !FRAME_VISIBLE_P (f))
10150 Fmake_frame_visible (frame);
10152 if (STRINGP (m) && SCHARS (m) > 0)
10154 set_message (m);
10155 if (minibuffer_auto_raise)
10156 Fraise_frame (frame);
10157 /* Assume we are not echoing.
10158 (If we are, echo_now will override this.) */
10159 echo_message_buffer = Qnil;
10161 else
10162 clear_message (true, true);
10164 do_pending_window_change (false);
10165 echo_area_display (true);
10166 do_pending_window_change (false);
10167 if (FRAME_TERMINAL (f)->frame_up_to_date_hook)
10168 (*FRAME_TERMINAL (f)->frame_up_to_date_hook) (f);
10173 /* Display a null-terminated echo area message M. If M is 0, clear
10174 out any existing message, and let the mini-buffer text show through.
10176 The buffer M must continue to exist until after the echo area gets
10177 cleared or some other message gets displayed there. Do not pass
10178 text that is stored in a Lisp string. Do not pass text in a buffer
10179 that was alloca'd. */
10181 void
10182 message1 (const char *m)
10184 message3 (m ? build_unibyte_string (m) : Qnil);
10188 /* The non-logging counterpart of message1. */
10190 void
10191 message1_nolog (const char *m)
10193 message3_nolog (m ? build_unibyte_string (m) : Qnil);
10196 /* Display a message M which contains a single %s
10197 which gets replaced with STRING. */
10199 void
10200 message_with_string (const char *m, Lisp_Object string, int log)
10202 CHECK_STRING (string);
10204 if (noninteractive)
10206 if (m)
10208 /* ENCODE_SYSTEM below can GC and/or relocate the
10209 Lisp data, so make sure we don't use it here. */
10210 eassert (relocatable_string_data_p (m) != 1);
10212 if (noninteractive_need_newline)
10213 putc ('\n', stderr);
10214 noninteractive_need_newline = 0;
10215 fprintf (stderr, m, SDATA (ENCODE_SYSTEM (string)));
10216 if (!cursor_in_echo_area)
10217 fprintf (stderr, "\n");
10218 fflush (stderr);
10221 else if (INTERACTIVE)
10223 /* The frame whose minibuffer we're going to display the message on.
10224 It may be larger than the selected frame, so we need
10225 to use its buffer, not the selected frame's buffer. */
10226 Lisp_Object mini_window;
10227 struct frame *f, *sf = SELECTED_FRAME ();
10229 /* Get the frame containing the minibuffer
10230 that the selected frame is using. */
10231 mini_window = FRAME_MINIBUF_WINDOW (sf);
10232 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
10234 /* Error messages get reported properly by cmd_error, so this must be
10235 just an informative message; if the frame hasn't really been
10236 initialized yet, just toss it. */
10237 if (f->glyphs_initialized_p)
10239 struct gcpro gcpro1, gcpro2;
10241 Lisp_Object fmt = build_string (m);
10242 Lisp_Object msg = string;
10243 GCPRO2 (fmt, msg);
10245 msg = CALLN (Fformat, fmt, msg);
10247 if (log)
10248 message3 (msg);
10249 else
10250 message3_nolog (msg);
10252 UNGCPRO;
10254 /* Print should start at the beginning of the message
10255 buffer next time. */
10256 message_buf_print = 0;
10262 /* Dump an informative message to the minibuf. If M is 0, clear out
10263 any existing message, and let the mini-buffer text show through. */
10265 static void
10266 vmessage (const char *m, va_list ap)
10268 if (noninteractive)
10270 if (m)
10272 if (noninteractive_need_newline)
10273 putc ('\n', stderr);
10274 noninteractive_need_newline = 0;
10275 vfprintf (stderr, m, ap);
10276 if (cursor_in_echo_area == 0)
10277 fprintf (stderr, "\n");
10278 fflush (stderr);
10281 else if (INTERACTIVE)
10283 /* The frame whose mini-buffer we're going to display the message
10284 on. It may be larger than the selected frame, so we need to
10285 use its buffer, not the selected frame's buffer. */
10286 Lisp_Object mini_window;
10287 struct frame *f, *sf = SELECTED_FRAME ();
10289 /* Get the frame containing the mini-buffer
10290 that the selected frame is using. */
10291 mini_window = FRAME_MINIBUF_WINDOW (sf);
10292 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
10294 /* Error messages get reported properly by cmd_error, so this must be
10295 just an informative message; if the frame hasn't really been
10296 initialized yet, just toss it. */
10297 if (f->glyphs_initialized_p)
10299 if (m)
10301 ptrdiff_t len;
10302 ptrdiff_t maxsize = FRAME_MESSAGE_BUF_SIZE (f);
10303 USE_SAFE_ALLOCA;
10304 char *message_buf = SAFE_ALLOCA (maxsize + 1);
10306 len = doprnt (message_buf, maxsize, m, 0, ap);
10308 message3 (make_string (message_buf, len));
10309 SAFE_FREE ();
10311 else
10312 message1 (0);
10314 /* Print should start at the beginning of the message
10315 buffer next time. */
10316 message_buf_print = 0;
10321 void
10322 message (const char *m, ...)
10324 va_list ap;
10325 va_start (ap, m);
10326 vmessage (m, ap);
10327 va_end (ap);
10331 #if 0
10332 /* The non-logging version of message. */
10334 void
10335 message_nolog (const char *m, ...)
10337 Lisp_Object old_log_max;
10338 va_list ap;
10339 va_start (ap, m);
10340 old_log_max = Vmessage_log_max;
10341 Vmessage_log_max = Qnil;
10342 vmessage (m, ap);
10343 Vmessage_log_max = old_log_max;
10344 va_end (ap);
10346 #endif
10349 /* Display the current message in the current mini-buffer. This is
10350 only called from error handlers in process.c, and is not time
10351 critical. */
10353 void
10354 update_echo_area (void)
10356 if (!NILP (echo_area_buffer[0]))
10358 Lisp_Object string;
10359 string = Fcurrent_message ();
10360 message3 (string);
10365 /* Make sure echo area buffers in `echo_buffers' are live.
10366 If they aren't, make new ones. */
10368 static void
10369 ensure_echo_area_buffers (void)
10371 int i;
10373 for (i = 0; i < 2; ++i)
10374 if (!BUFFERP (echo_buffer[i])
10375 || !BUFFER_LIVE_P (XBUFFER (echo_buffer[i])))
10377 char name[30];
10378 Lisp_Object old_buffer;
10379 int j;
10381 old_buffer = echo_buffer[i];
10382 echo_buffer[i] = Fget_buffer_create
10383 (make_formatted_string (name, " *Echo Area %d*", i));
10384 bset_truncate_lines (XBUFFER (echo_buffer[i]), Qnil);
10385 /* to force word wrap in echo area -
10386 it was decided to postpone this*/
10387 /* XBUFFER (echo_buffer[i])->word_wrap = Qt; */
10389 for (j = 0; j < 2; ++j)
10390 if (EQ (old_buffer, echo_area_buffer[j]))
10391 echo_area_buffer[j] = echo_buffer[i];
10396 /* Call FN with args A1..A2 with either the current or last displayed
10397 echo_area_buffer as current buffer.
10399 WHICH zero means use the current message buffer
10400 echo_area_buffer[0]. If that is nil, choose a suitable buffer
10401 from echo_buffer[] and clear it.
10403 WHICH > 0 means use echo_area_buffer[1]. If that is nil, choose a
10404 suitable buffer from echo_buffer[] and clear it.
10406 If WHICH < 0, set echo_area_buffer[1] to echo_area_buffer[0], so
10407 that the current message becomes the last displayed one, make
10408 choose a suitable buffer for echo_area_buffer[0], and clear it.
10410 Value is what FN returns. */
10412 static int
10413 with_echo_area_buffer (struct window *w, int which,
10414 int (*fn) (ptrdiff_t, Lisp_Object),
10415 ptrdiff_t a1, Lisp_Object a2)
10417 Lisp_Object buffer;
10418 int this_one, the_other, clear_buffer_p, rc;
10419 ptrdiff_t count = SPECPDL_INDEX ();
10421 /* If buffers aren't live, make new ones. */
10422 ensure_echo_area_buffers ();
10424 clear_buffer_p = 0;
10426 if (which == 0)
10427 this_one = 0, the_other = 1;
10428 else if (which > 0)
10429 this_one = 1, the_other = 0;
10430 else
10432 this_one = 0, the_other = 1;
10433 clear_buffer_p = true;
10435 /* We need a fresh one in case the current echo buffer equals
10436 the one containing the last displayed echo area message. */
10437 if (!NILP (echo_area_buffer[this_one])
10438 && EQ (echo_area_buffer[this_one], echo_area_buffer[the_other]))
10439 echo_area_buffer[this_one] = Qnil;
10442 /* Choose a suitable buffer from echo_buffer[] is we don't
10443 have one. */
10444 if (NILP (echo_area_buffer[this_one]))
10446 echo_area_buffer[this_one]
10447 = (EQ (echo_area_buffer[the_other], echo_buffer[this_one])
10448 ? echo_buffer[the_other]
10449 : echo_buffer[this_one]);
10450 clear_buffer_p = true;
10453 buffer = echo_area_buffer[this_one];
10455 /* Don't get confused by reusing the buffer used for echoing
10456 for a different purpose. */
10457 if (echo_kboard == NULL && EQ (buffer, echo_message_buffer))
10458 cancel_echoing ();
10460 record_unwind_protect (unwind_with_echo_area_buffer,
10461 with_echo_area_buffer_unwind_data (w));
10463 /* Make the echo area buffer current. Note that for display
10464 purposes, it is not necessary that the displayed window's buffer
10465 == current_buffer, except for text property lookup. So, let's
10466 only set that buffer temporarily here without doing a full
10467 Fset_window_buffer. We must also change w->pointm, though,
10468 because otherwise an assertions in unshow_buffer fails, and Emacs
10469 aborts. */
10470 set_buffer_internal_1 (XBUFFER (buffer));
10471 if (w)
10473 wset_buffer (w, buffer);
10474 set_marker_both (w->pointm, buffer, BEG, BEG_BYTE);
10475 set_marker_both (w->old_pointm, buffer, BEG, BEG_BYTE);
10478 bset_undo_list (current_buffer, Qt);
10479 bset_read_only (current_buffer, Qnil);
10480 specbind (Qinhibit_read_only, Qt);
10481 specbind (Qinhibit_modification_hooks, Qt);
10483 if (clear_buffer_p && Z > BEG)
10484 del_range (BEG, Z);
10486 eassert (BEGV >= BEG);
10487 eassert (ZV <= Z && ZV >= BEGV);
10489 rc = fn (a1, a2);
10491 eassert (BEGV >= BEG);
10492 eassert (ZV <= Z && ZV >= BEGV);
10494 unbind_to (count, Qnil);
10495 return rc;
10499 /* Save state that should be preserved around the call to the function
10500 FN called in with_echo_area_buffer. */
10502 static Lisp_Object
10503 with_echo_area_buffer_unwind_data (struct window *w)
10505 int i = 0;
10506 Lisp_Object vector, tmp;
10508 /* Reduce consing by keeping one vector in
10509 Vwith_echo_area_save_vector. */
10510 vector = Vwith_echo_area_save_vector;
10511 Vwith_echo_area_save_vector = Qnil;
10513 if (NILP (vector))
10514 vector = Fmake_vector (make_number (11), Qnil);
10516 XSETBUFFER (tmp, current_buffer); ASET (vector, i, tmp); ++i;
10517 ASET (vector, i, Vdeactivate_mark); ++i;
10518 ASET (vector, i, make_number (windows_or_buffers_changed)); ++i;
10520 if (w)
10522 XSETWINDOW (tmp, w); ASET (vector, i, tmp); ++i;
10523 ASET (vector, i, w->contents); ++i;
10524 ASET (vector, i, make_number (marker_position (w->pointm))); ++i;
10525 ASET (vector, i, make_number (marker_byte_position (w->pointm))); ++i;
10526 ASET (vector, i, make_number (marker_position (w->old_pointm))); ++i;
10527 ASET (vector, i, make_number (marker_byte_position (w->old_pointm))); ++i;
10528 ASET (vector, i, make_number (marker_position (w->start))); ++i;
10529 ASET (vector, i, make_number (marker_byte_position (w->start))); ++i;
10531 else
10533 int end = i + 8;
10534 for (; i < end; ++i)
10535 ASET (vector, i, Qnil);
10538 eassert (i == ASIZE (vector));
10539 return vector;
10543 /* Restore global state from VECTOR which was created by
10544 with_echo_area_buffer_unwind_data. */
10546 static void
10547 unwind_with_echo_area_buffer (Lisp_Object vector)
10549 set_buffer_internal_1 (XBUFFER (AREF (vector, 0)));
10550 Vdeactivate_mark = AREF (vector, 1);
10551 windows_or_buffers_changed = XFASTINT (AREF (vector, 2));
10553 if (WINDOWP (AREF (vector, 3)))
10555 struct window *w;
10556 Lisp_Object buffer;
10558 w = XWINDOW (AREF (vector, 3));
10559 buffer = AREF (vector, 4);
10561 wset_buffer (w, buffer);
10562 set_marker_both (w->pointm, buffer,
10563 XFASTINT (AREF (vector, 5)),
10564 XFASTINT (AREF (vector, 6)));
10565 set_marker_both (w->old_pointm, buffer,
10566 XFASTINT (AREF (vector, 7)),
10567 XFASTINT (AREF (vector, 8)));
10568 set_marker_both (w->start, buffer,
10569 XFASTINT (AREF (vector, 9)),
10570 XFASTINT (AREF (vector, 10)));
10573 Vwith_echo_area_save_vector = vector;
10577 /* Set up the echo area for use by print functions. MULTIBYTE_P
10578 non-zero means we will print multibyte. */
10580 void
10581 setup_echo_area_for_printing (int multibyte_p)
10583 /* If we can't find an echo area any more, exit. */
10584 if (! FRAME_LIVE_P (XFRAME (selected_frame)))
10585 Fkill_emacs (Qnil);
10587 ensure_echo_area_buffers ();
10589 if (!message_buf_print)
10591 /* A message has been output since the last time we printed.
10592 Choose a fresh echo area buffer. */
10593 if (EQ (echo_area_buffer[1], echo_buffer[0]))
10594 echo_area_buffer[0] = echo_buffer[1];
10595 else
10596 echo_area_buffer[0] = echo_buffer[0];
10598 /* Switch to that buffer and clear it. */
10599 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
10600 bset_truncate_lines (current_buffer, Qnil);
10602 if (Z > BEG)
10604 ptrdiff_t count = SPECPDL_INDEX ();
10605 specbind (Qinhibit_read_only, Qt);
10606 /* Note that undo recording is always disabled. */
10607 del_range (BEG, Z);
10608 unbind_to (count, Qnil);
10610 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
10612 /* Set up the buffer for the multibyteness we need. */
10613 if (multibyte_p
10614 != !NILP (BVAR (current_buffer, enable_multibyte_characters)))
10615 Fset_buffer_multibyte (multibyte_p ? Qt : Qnil);
10617 /* Raise the frame containing the echo area. */
10618 if (minibuffer_auto_raise)
10620 struct frame *sf = SELECTED_FRAME ();
10621 Lisp_Object mini_window;
10622 mini_window = FRAME_MINIBUF_WINDOW (sf);
10623 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
10626 message_log_maybe_newline ();
10627 message_buf_print = 1;
10629 else
10631 if (NILP (echo_area_buffer[0]))
10633 if (EQ (echo_area_buffer[1], echo_buffer[0]))
10634 echo_area_buffer[0] = echo_buffer[1];
10635 else
10636 echo_area_buffer[0] = echo_buffer[0];
10639 if (current_buffer != XBUFFER (echo_area_buffer[0]))
10641 /* Someone switched buffers between print requests. */
10642 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
10643 bset_truncate_lines (current_buffer, Qnil);
10649 /* Display an echo area message in window W. Value is non-zero if W's
10650 height is changed. If display_last_displayed_message_p is
10651 non-zero, display the message that was last displayed, otherwise
10652 display the current message. */
10654 static int
10655 display_echo_area (struct window *w)
10657 int i, no_message_p, window_height_changed_p;
10659 /* Temporarily disable garbage collections while displaying the echo
10660 area. This is done because a GC can print a message itself.
10661 That message would modify the echo area buffer's contents while a
10662 redisplay of the buffer is going on, and seriously confuse
10663 redisplay. */
10664 ptrdiff_t count = inhibit_garbage_collection ();
10666 /* If there is no message, we must call display_echo_area_1
10667 nevertheless because it resizes the window. But we will have to
10668 reset the echo_area_buffer in question to nil at the end because
10669 with_echo_area_buffer will sets it to an empty buffer. */
10670 i = display_last_displayed_message_p ? 1 : 0;
10671 no_message_p = NILP (echo_area_buffer[i]);
10673 window_height_changed_p
10674 = with_echo_area_buffer (w, display_last_displayed_message_p,
10675 display_echo_area_1,
10676 (intptr_t) w, Qnil);
10678 if (no_message_p)
10679 echo_area_buffer[i] = Qnil;
10681 unbind_to (count, Qnil);
10682 return window_height_changed_p;
10686 /* Helper for display_echo_area. Display the current buffer which
10687 contains the current echo area message in window W, a mini-window,
10688 a pointer to which is passed in A1. A2..A4 are currently not used.
10689 Change the height of W so that all of the message is displayed.
10690 Value is non-zero if height of W was changed. */
10692 static int
10693 display_echo_area_1 (ptrdiff_t a1, Lisp_Object a2)
10695 intptr_t i1 = a1;
10696 struct window *w = (struct window *) i1;
10697 Lisp_Object window;
10698 struct text_pos start;
10699 int window_height_changed_p = 0;
10701 /* Do this before displaying, so that we have a large enough glyph
10702 matrix for the display. If we can't get enough space for the
10703 whole text, display the last N lines. That works by setting w->start. */
10704 window_height_changed_p = resize_mini_window (w, 0);
10706 /* Use the starting position chosen by resize_mini_window. */
10707 SET_TEXT_POS_FROM_MARKER (start, w->start);
10709 /* Display. */
10710 clear_glyph_matrix (w->desired_matrix);
10711 XSETWINDOW (window, w);
10712 try_window (window, start, 0);
10714 return window_height_changed_p;
10718 /* Resize the echo area window to exactly the size needed for the
10719 currently displayed message, if there is one. If a mini-buffer
10720 is active, don't shrink it. */
10722 void
10723 resize_echo_area_exactly (void)
10725 if (BUFFERP (echo_area_buffer[0])
10726 && WINDOWP (echo_area_window))
10728 struct window *w = XWINDOW (echo_area_window);
10729 Lisp_Object resize_exactly = (minibuf_level == 0 ? Qt : Qnil);
10730 int resized_p = with_echo_area_buffer (w, 0, resize_mini_window_1,
10731 (intptr_t) w, resize_exactly);
10732 if (resized_p)
10734 windows_or_buffers_changed = 42;
10735 update_mode_lines = 30;
10736 redisplay_internal ();
10742 /* Callback function for with_echo_area_buffer, when used from
10743 resize_echo_area_exactly. A1 contains a pointer to the window to
10744 resize, EXACTLY non-nil means resize the mini-window exactly to the
10745 size of the text displayed. A3 and A4 are not used. Value is what
10746 resize_mini_window returns. */
10748 static int
10749 resize_mini_window_1 (ptrdiff_t a1, Lisp_Object exactly)
10751 intptr_t i1 = a1;
10752 return resize_mini_window ((struct window *) i1, !NILP (exactly));
10756 /* Resize mini-window W to fit the size of its contents. EXACT_P
10757 means size the window exactly to the size needed. Otherwise, it's
10758 only enlarged until W's buffer is empty.
10760 Set W->start to the right place to begin display. If the whole
10761 contents fit, start at the beginning. Otherwise, start so as
10762 to make the end of the contents appear. This is particularly
10763 important for y-or-n-p, but seems desirable generally.
10765 Value is non-zero if the window height has been changed. */
10768 resize_mini_window (struct window *w, int exact_p)
10770 struct frame *f = XFRAME (w->frame);
10771 int window_height_changed_p = 0;
10773 eassert (MINI_WINDOW_P (w));
10775 /* By default, start display at the beginning. */
10776 set_marker_both (w->start, w->contents,
10777 BUF_BEGV (XBUFFER (w->contents)),
10778 BUF_BEGV_BYTE (XBUFFER (w->contents)));
10780 /* Don't resize windows while redisplaying a window; it would
10781 confuse redisplay functions when the size of the window they are
10782 displaying changes from under them. Such a resizing can happen,
10783 for instance, when which-func prints a long message while
10784 we are running fontification-functions. We're running these
10785 functions with safe_call which binds inhibit-redisplay to t. */
10786 if (!NILP (Vinhibit_redisplay))
10787 return 0;
10789 /* Nil means don't try to resize. */
10790 if (NILP (Vresize_mini_windows)
10791 || (FRAME_X_P (f) && FRAME_X_OUTPUT (f) == NULL))
10792 return 0;
10794 if (!FRAME_MINIBUF_ONLY_P (f))
10796 struct it it;
10797 int total_height = (WINDOW_PIXEL_HEIGHT (XWINDOW (FRAME_ROOT_WINDOW (f)))
10798 + WINDOW_PIXEL_HEIGHT (w));
10799 int unit = FRAME_LINE_HEIGHT (f);
10800 int height, max_height;
10801 struct text_pos start;
10802 struct buffer *old_current_buffer = NULL;
10804 if (current_buffer != XBUFFER (w->contents))
10806 old_current_buffer = current_buffer;
10807 set_buffer_internal (XBUFFER (w->contents));
10810 init_iterator (&it, w, BEGV, BEGV_BYTE, NULL, DEFAULT_FACE_ID);
10812 /* Compute the max. number of lines specified by the user. */
10813 if (FLOATP (Vmax_mini_window_height))
10814 max_height = XFLOATINT (Vmax_mini_window_height) * total_height;
10815 else if (INTEGERP (Vmax_mini_window_height))
10816 max_height = XINT (Vmax_mini_window_height) * unit;
10817 else
10818 max_height = total_height / 4;
10820 /* Correct that max. height if it's bogus. */
10821 max_height = clip_to_bounds (unit, max_height, total_height);
10823 /* Find out the height of the text in the window. */
10824 if (it.line_wrap == TRUNCATE)
10825 height = unit;
10826 else
10828 last_height = 0;
10829 move_it_to (&it, ZV, -1, -1, -1, MOVE_TO_POS);
10830 if (it.max_ascent == 0 && it.max_descent == 0)
10831 height = it.current_y + last_height;
10832 else
10833 height = it.current_y + it.max_ascent + it.max_descent;
10834 height -= min (it.extra_line_spacing, it.max_extra_line_spacing);
10837 /* Compute a suitable window start. */
10838 if (height > max_height)
10840 height = (max_height / unit) * unit;
10841 init_iterator (&it, w, ZV, ZV_BYTE, NULL, DEFAULT_FACE_ID);
10842 move_it_vertically_backward (&it, height - unit);
10843 start = it.current.pos;
10845 else
10846 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
10847 SET_MARKER_FROM_TEXT_POS (w->start, start);
10849 if (EQ (Vresize_mini_windows, Qgrow_only))
10851 /* Let it grow only, until we display an empty message, in which
10852 case the window shrinks again. */
10853 if (height > WINDOW_PIXEL_HEIGHT (w))
10855 int old_height = WINDOW_PIXEL_HEIGHT (w);
10857 FRAME_WINDOWS_FROZEN (f) = 1;
10858 grow_mini_window (w, height - WINDOW_PIXEL_HEIGHT (w), 1);
10859 window_height_changed_p = WINDOW_PIXEL_HEIGHT (w) != old_height;
10861 else if (height < WINDOW_PIXEL_HEIGHT (w)
10862 && (exact_p || BEGV == ZV))
10864 int old_height = WINDOW_PIXEL_HEIGHT (w);
10866 FRAME_WINDOWS_FROZEN (f) = 0;
10867 shrink_mini_window (w, 1);
10868 window_height_changed_p = WINDOW_PIXEL_HEIGHT (w) != old_height;
10871 else
10873 /* Always resize to exact size needed. */
10874 if (height > WINDOW_PIXEL_HEIGHT (w))
10876 int old_height = WINDOW_PIXEL_HEIGHT (w);
10878 FRAME_WINDOWS_FROZEN (f) = 1;
10879 grow_mini_window (w, height - WINDOW_PIXEL_HEIGHT (w), 1);
10880 window_height_changed_p = WINDOW_PIXEL_HEIGHT (w) != old_height;
10882 else if (height < WINDOW_PIXEL_HEIGHT (w))
10884 int old_height = WINDOW_PIXEL_HEIGHT (w);
10886 FRAME_WINDOWS_FROZEN (f) = 0;
10887 shrink_mini_window (w, 1);
10889 if (height)
10891 FRAME_WINDOWS_FROZEN (f) = 1;
10892 grow_mini_window (w, height - WINDOW_PIXEL_HEIGHT (w), 1);
10895 window_height_changed_p = WINDOW_PIXEL_HEIGHT (w) != old_height;
10899 if (old_current_buffer)
10900 set_buffer_internal (old_current_buffer);
10903 return window_height_changed_p;
10907 /* Value is the current message, a string, or nil if there is no
10908 current message. */
10910 Lisp_Object
10911 current_message (void)
10913 Lisp_Object msg;
10915 if (!BUFFERP (echo_area_buffer[0]))
10916 msg = Qnil;
10917 else
10919 with_echo_area_buffer (0, 0, current_message_1,
10920 (intptr_t) &msg, Qnil);
10921 if (NILP (msg))
10922 echo_area_buffer[0] = Qnil;
10925 return msg;
10929 static int
10930 current_message_1 (ptrdiff_t a1, Lisp_Object a2)
10932 intptr_t i1 = a1;
10933 Lisp_Object *msg = (Lisp_Object *) i1;
10935 if (Z > BEG)
10936 *msg = make_buffer_string (BEG, Z, 1);
10937 else
10938 *msg = Qnil;
10939 return 0;
10943 /* Push the current message on Vmessage_stack for later restoration
10944 by restore_message. Value is non-zero if the current message isn't
10945 empty. This is a relatively infrequent operation, so it's not
10946 worth optimizing. */
10948 bool
10949 push_message (void)
10951 Lisp_Object msg = current_message ();
10952 Vmessage_stack = Fcons (msg, Vmessage_stack);
10953 return STRINGP (msg);
10957 /* Restore message display from the top of Vmessage_stack. */
10959 void
10960 restore_message (void)
10962 eassert (CONSP (Vmessage_stack));
10963 message3_nolog (XCAR (Vmessage_stack));
10967 /* Handler for unwind-protect calling pop_message. */
10969 void
10970 pop_message_unwind (void)
10972 /* Pop the top-most entry off Vmessage_stack. */
10973 eassert (CONSP (Vmessage_stack));
10974 Vmessage_stack = XCDR (Vmessage_stack);
10978 /* Check that Vmessage_stack is nil. Called from emacs.c when Emacs
10979 exits. If the stack is not empty, we have a missing pop_message
10980 somewhere. */
10982 void
10983 check_message_stack (void)
10985 if (!NILP (Vmessage_stack))
10986 emacs_abort ();
10990 /* Truncate to NCHARS what will be displayed in the echo area the next
10991 time we display it---but don't redisplay it now. */
10993 void
10994 truncate_echo_area (ptrdiff_t nchars)
10996 if (nchars == 0)
10997 echo_area_buffer[0] = Qnil;
10998 else if (!noninteractive
10999 && INTERACTIVE
11000 && !NILP (echo_area_buffer[0]))
11002 struct frame *sf = SELECTED_FRAME ();
11003 /* Error messages get reported properly by cmd_error, so this must be
11004 just an informative message; if the frame hasn't really been
11005 initialized yet, just toss it. */
11006 if (sf->glyphs_initialized_p)
11007 with_echo_area_buffer (0, 0, truncate_message_1, nchars, Qnil);
11012 /* Helper function for truncate_echo_area. Truncate the current
11013 message to at most NCHARS characters. */
11015 static int
11016 truncate_message_1 (ptrdiff_t nchars, Lisp_Object a2)
11018 if (BEG + nchars < Z)
11019 del_range (BEG + nchars, Z);
11020 if (Z == BEG)
11021 echo_area_buffer[0] = Qnil;
11022 return 0;
11025 /* Set the current message to STRING. */
11027 static void
11028 set_message (Lisp_Object string)
11030 eassert (STRINGP (string));
11032 message_enable_multibyte = STRING_MULTIBYTE (string);
11034 with_echo_area_buffer (0, -1, set_message_1, 0, string);
11035 message_buf_print = 0;
11036 help_echo_showing_p = 0;
11038 if (STRINGP (Vdebug_on_message)
11039 && STRINGP (string)
11040 && fast_string_match (Vdebug_on_message, string) >= 0)
11041 call_debugger (list2 (Qerror, string));
11045 /* Helper function for set_message. First argument is ignored and second
11046 argument has the same meaning as for set_message.
11047 This function is called with the echo area buffer being current. */
11049 static int
11050 set_message_1 (ptrdiff_t a1, Lisp_Object string)
11052 eassert (STRINGP (string));
11054 /* Change multibyteness of the echo buffer appropriately. */
11055 if (message_enable_multibyte
11056 != !NILP (BVAR (current_buffer, enable_multibyte_characters)))
11057 Fset_buffer_multibyte (message_enable_multibyte ? Qt : Qnil);
11059 bset_truncate_lines (current_buffer, message_truncate_lines ? Qt : Qnil);
11060 if (!NILP (BVAR (current_buffer, bidi_display_reordering)))
11061 bset_bidi_paragraph_direction (current_buffer, Qleft_to_right);
11063 /* Insert new message at BEG. */
11064 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
11066 /* This function takes care of single/multibyte conversion.
11067 We just have to ensure that the echo area buffer has the right
11068 setting of enable_multibyte_characters. */
11069 insert_from_string (string, 0, 0, SCHARS (string), SBYTES (string), 1);
11071 return 0;
11075 /* Clear messages. CURRENT_P non-zero means clear the current
11076 message. LAST_DISPLAYED_P non-zero means clear the message
11077 last displayed. */
11079 void
11080 clear_message (bool current_p, bool last_displayed_p)
11082 if (current_p)
11084 echo_area_buffer[0] = Qnil;
11085 message_cleared_p = true;
11088 if (last_displayed_p)
11089 echo_area_buffer[1] = Qnil;
11091 message_buf_print = 0;
11094 /* Clear garbaged frames.
11096 This function is used where the old redisplay called
11097 redraw_garbaged_frames which in turn called redraw_frame which in
11098 turn called clear_frame. The call to clear_frame was a source of
11099 flickering. I believe a clear_frame is not necessary. It should
11100 suffice in the new redisplay to invalidate all current matrices,
11101 and ensure a complete redisplay of all windows. */
11103 static void
11104 clear_garbaged_frames (void)
11106 if (frame_garbaged)
11108 Lisp_Object tail, frame;
11110 FOR_EACH_FRAME (tail, frame)
11112 struct frame *f = XFRAME (frame);
11114 if (FRAME_VISIBLE_P (f) && FRAME_GARBAGED_P (f))
11116 if (f->resized_p)
11117 redraw_frame (f);
11118 else
11119 clear_current_matrices (f);
11120 fset_redisplay (f);
11121 f->garbaged = false;
11122 f->resized_p = false;
11126 frame_garbaged = false;
11131 /* Redisplay the echo area of the selected frame. If UPDATE_FRAME_P
11132 is non-zero update selected_frame. Value is non-zero if the
11133 mini-windows height has been changed. */
11135 static bool
11136 echo_area_display (bool update_frame_p)
11138 Lisp_Object mini_window;
11139 struct window *w;
11140 struct frame *f;
11141 bool window_height_changed_p = false;
11142 struct frame *sf = SELECTED_FRAME ();
11144 mini_window = FRAME_MINIBUF_WINDOW (sf);
11145 w = XWINDOW (mini_window);
11146 f = XFRAME (WINDOW_FRAME (w));
11148 /* Don't display if frame is invisible or not yet initialized. */
11149 if (!FRAME_VISIBLE_P (f) || !f->glyphs_initialized_p)
11150 return 0;
11152 #ifdef HAVE_WINDOW_SYSTEM
11153 /* When Emacs starts, selected_frame may be the initial terminal
11154 frame. If we let this through, a message would be displayed on
11155 the terminal. */
11156 if (FRAME_INITIAL_P (XFRAME (selected_frame)))
11157 return 0;
11158 #endif /* HAVE_WINDOW_SYSTEM */
11160 /* Redraw garbaged frames. */
11161 clear_garbaged_frames ();
11163 if (!NILP (echo_area_buffer[0]) || minibuf_level == 0)
11165 echo_area_window = mini_window;
11166 window_height_changed_p = display_echo_area (w);
11167 w->must_be_updated_p = true;
11169 /* Update the display, unless called from redisplay_internal.
11170 Also don't update the screen during redisplay itself. The
11171 update will happen at the end of redisplay, and an update
11172 here could cause confusion. */
11173 if (update_frame_p && !redisplaying_p)
11175 int n = 0;
11177 /* If the display update has been interrupted by pending
11178 input, update mode lines in the frame. Due to the
11179 pending input, it might have been that redisplay hasn't
11180 been called, so that mode lines above the echo area are
11181 garbaged. This looks odd, so we prevent it here. */
11182 if (!display_completed)
11183 n = redisplay_mode_lines (FRAME_ROOT_WINDOW (f), false);
11185 if (window_height_changed_p
11186 /* Don't do this if Emacs is shutting down. Redisplay
11187 needs to run hooks. */
11188 && !NILP (Vrun_hooks))
11190 /* Must update other windows. Likewise as in other
11191 cases, don't let this update be interrupted by
11192 pending input. */
11193 ptrdiff_t count = SPECPDL_INDEX ();
11194 specbind (Qredisplay_dont_pause, Qt);
11195 windows_or_buffers_changed = 44;
11196 redisplay_internal ();
11197 unbind_to (count, Qnil);
11199 else if (FRAME_WINDOW_P (f) && n == 0)
11201 /* Window configuration is the same as before.
11202 Can do with a display update of the echo area,
11203 unless we displayed some mode lines. */
11204 update_single_window (w);
11205 flush_frame (f);
11207 else
11208 update_frame (f, true, true);
11210 /* If cursor is in the echo area, make sure that the next
11211 redisplay displays the minibuffer, so that the cursor will
11212 be replaced with what the minibuffer wants. */
11213 if (cursor_in_echo_area)
11214 wset_redisplay (XWINDOW (mini_window));
11217 else if (!EQ (mini_window, selected_window))
11218 wset_redisplay (XWINDOW (mini_window));
11220 /* Last displayed message is now the current message. */
11221 echo_area_buffer[1] = echo_area_buffer[0];
11222 /* Inform read_char that we're not echoing. */
11223 echo_message_buffer = Qnil;
11225 /* Prevent redisplay optimization in redisplay_internal by resetting
11226 this_line_start_pos. This is done because the mini-buffer now
11227 displays the message instead of its buffer text. */
11228 if (EQ (mini_window, selected_window))
11229 CHARPOS (this_line_start_pos) = 0;
11231 return window_height_changed_p;
11234 /* Nonzero if W's buffer was changed but not saved. */
11236 static int
11237 window_buffer_changed (struct window *w)
11239 struct buffer *b = XBUFFER (w->contents);
11241 eassert (BUFFER_LIVE_P (b));
11243 return (((BUF_SAVE_MODIFF (b) < BUF_MODIFF (b)) != w->last_had_star));
11246 /* Nonzero if W has %c in its mode line and mode line should be updated. */
11248 static int
11249 mode_line_update_needed (struct window *w)
11251 return (w->column_number_displayed != -1
11252 && !(PT == w->last_point && !window_outdated (w))
11253 && (w->column_number_displayed != current_column ()));
11256 /* Nonzero if window start of W is frozen and may not be changed during
11257 redisplay. */
11259 static bool
11260 window_frozen_p (struct window *w)
11262 if (FRAME_WINDOWS_FROZEN (XFRAME (WINDOW_FRAME (w))))
11264 Lisp_Object window;
11266 XSETWINDOW (window, w);
11267 if (MINI_WINDOW_P (w))
11268 return 0;
11269 else if (EQ (window, selected_window))
11270 return 0;
11271 else if (MINI_WINDOW_P (XWINDOW (selected_window))
11272 && EQ (window, Vminibuf_scroll_window))
11273 /* This special window can't be frozen too. */
11274 return 0;
11275 else
11276 return 1;
11278 return 0;
11281 /***********************************************************************
11282 Mode Lines and Frame Titles
11283 ***********************************************************************/
11285 /* A buffer for constructing non-propertized mode-line strings and
11286 frame titles in it; allocated from the heap in init_xdisp and
11287 resized as needed in store_mode_line_noprop_char. */
11289 static char *mode_line_noprop_buf;
11291 /* The buffer's end, and a current output position in it. */
11293 static char *mode_line_noprop_buf_end;
11294 static char *mode_line_noprop_ptr;
11296 #define MODE_LINE_NOPROP_LEN(start) \
11297 ((mode_line_noprop_ptr - mode_line_noprop_buf) - start)
11299 static enum {
11300 MODE_LINE_DISPLAY = 0,
11301 MODE_LINE_TITLE,
11302 MODE_LINE_NOPROP,
11303 MODE_LINE_STRING
11304 } mode_line_target;
11306 /* Alist that caches the results of :propertize.
11307 Each element is (PROPERTIZED-STRING . PROPERTY-LIST). */
11308 static Lisp_Object mode_line_proptrans_alist;
11310 /* List of strings making up the mode-line. */
11311 static Lisp_Object mode_line_string_list;
11313 /* Base face property when building propertized mode line string. */
11314 static Lisp_Object mode_line_string_face;
11315 static Lisp_Object mode_line_string_face_prop;
11318 /* Unwind data for mode line strings */
11320 static Lisp_Object Vmode_line_unwind_vector;
11322 static Lisp_Object
11323 format_mode_line_unwind_data (struct frame *target_frame,
11324 struct buffer *obuf,
11325 Lisp_Object owin,
11326 int save_proptrans)
11328 Lisp_Object vector, tmp;
11330 /* Reduce consing by keeping one vector in
11331 Vwith_echo_area_save_vector. */
11332 vector = Vmode_line_unwind_vector;
11333 Vmode_line_unwind_vector = Qnil;
11335 if (NILP (vector))
11336 vector = Fmake_vector (make_number (10), Qnil);
11338 ASET (vector, 0, make_number (mode_line_target));
11339 ASET (vector, 1, make_number (MODE_LINE_NOPROP_LEN (0)));
11340 ASET (vector, 2, mode_line_string_list);
11341 ASET (vector, 3, save_proptrans ? mode_line_proptrans_alist : Qt);
11342 ASET (vector, 4, mode_line_string_face);
11343 ASET (vector, 5, mode_line_string_face_prop);
11345 if (obuf)
11346 XSETBUFFER (tmp, obuf);
11347 else
11348 tmp = Qnil;
11349 ASET (vector, 6, tmp);
11350 ASET (vector, 7, owin);
11351 if (target_frame)
11353 /* Similarly to `with-selected-window', if the operation selects
11354 a window on another frame, we must restore that frame's
11355 selected window, and (for a tty) the top-frame. */
11356 ASET (vector, 8, target_frame->selected_window);
11357 if (FRAME_TERMCAP_P (target_frame))
11358 ASET (vector, 9, FRAME_TTY (target_frame)->top_frame);
11361 return vector;
11364 static void
11365 unwind_format_mode_line (Lisp_Object vector)
11367 Lisp_Object old_window = AREF (vector, 7);
11368 Lisp_Object target_frame_window = AREF (vector, 8);
11369 Lisp_Object old_top_frame = AREF (vector, 9);
11371 mode_line_target = XINT (AREF (vector, 0));
11372 mode_line_noprop_ptr = mode_line_noprop_buf + XINT (AREF (vector, 1));
11373 mode_line_string_list = AREF (vector, 2);
11374 if (! EQ (AREF (vector, 3), Qt))
11375 mode_line_proptrans_alist = AREF (vector, 3);
11376 mode_line_string_face = AREF (vector, 4);
11377 mode_line_string_face_prop = AREF (vector, 5);
11379 /* Select window before buffer, since it may change the buffer. */
11380 if (!NILP (old_window))
11382 /* If the operation that we are unwinding had selected a window
11383 on a different frame, reset its frame-selected-window. For a
11384 text terminal, reset its top-frame if necessary. */
11385 if (!NILP (target_frame_window))
11387 Lisp_Object frame
11388 = WINDOW_FRAME (XWINDOW (target_frame_window));
11390 if (!EQ (frame, WINDOW_FRAME (XWINDOW (old_window))))
11391 Fselect_window (target_frame_window, Qt);
11393 if (!NILP (old_top_frame) && !EQ (old_top_frame, frame))
11394 Fselect_frame (old_top_frame, Qt);
11397 Fselect_window (old_window, Qt);
11400 if (!NILP (AREF (vector, 6)))
11402 set_buffer_internal_1 (XBUFFER (AREF (vector, 6)));
11403 ASET (vector, 6, Qnil);
11406 Vmode_line_unwind_vector = vector;
11410 /* Store a single character C for the frame title in mode_line_noprop_buf.
11411 Re-allocate mode_line_noprop_buf if necessary. */
11413 static void
11414 store_mode_line_noprop_char (char c)
11416 /* If output position has reached the end of the allocated buffer,
11417 increase the buffer's size. */
11418 if (mode_line_noprop_ptr == mode_line_noprop_buf_end)
11420 ptrdiff_t len = MODE_LINE_NOPROP_LEN (0);
11421 ptrdiff_t size = len;
11422 mode_line_noprop_buf =
11423 xpalloc (mode_line_noprop_buf, &size, 1, STRING_BYTES_BOUND, 1);
11424 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
11425 mode_line_noprop_ptr = mode_line_noprop_buf + len;
11428 *mode_line_noprop_ptr++ = c;
11432 /* Store part of a frame title in mode_line_noprop_buf, beginning at
11433 mode_line_noprop_ptr. STRING is the string to store. Do not copy
11434 characters that yield more columns than PRECISION; PRECISION <= 0
11435 means copy the whole string. Pad with spaces until FIELD_WIDTH
11436 number of characters have been copied; FIELD_WIDTH <= 0 means don't
11437 pad. Called from display_mode_element when it is used to build a
11438 frame title. */
11440 static int
11441 store_mode_line_noprop (const char *string, int field_width, int precision)
11443 const unsigned char *str = (const unsigned char *) string;
11444 int n = 0;
11445 ptrdiff_t dummy, nbytes;
11447 /* Copy at most PRECISION chars from STR. */
11448 nbytes = strlen (string);
11449 n += c_string_width (str, nbytes, precision, &dummy, &nbytes);
11450 while (nbytes--)
11451 store_mode_line_noprop_char (*str++);
11453 /* Fill up with spaces until FIELD_WIDTH reached. */
11454 while (field_width > 0
11455 && n < field_width)
11457 store_mode_line_noprop_char (' ');
11458 ++n;
11461 return n;
11464 /***********************************************************************
11465 Frame Titles
11466 ***********************************************************************/
11468 #ifdef HAVE_WINDOW_SYSTEM
11470 /* Set the title of FRAME, if it has changed. The title format is
11471 Vicon_title_format if FRAME is iconified, otherwise it is
11472 frame_title_format. */
11474 static void
11475 x_consider_frame_title (Lisp_Object frame)
11477 struct frame *f = XFRAME (frame);
11479 if (FRAME_WINDOW_P (f)
11480 || FRAME_MINIBUF_ONLY_P (f)
11481 || f->explicit_name)
11483 /* Do we have more than one visible frame on this X display? */
11484 Lisp_Object tail, other_frame, fmt;
11485 ptrdiff_t title_start;
11486 char *title;
11487 ptrdiff_t len;
11488 struct it it;
11489 ptrdiff_t count = SPECPDL_INDEX ();
11491 FOR_EACH_FRAME (tail, other_frame)
11493 struct frame *tf = XFRAME (other_frame);
11495 if (tf != f
11496 && FRAME_KBOARD (tf) == FRAME_KBOARD (f)
11497 && !FRAME_MINIBUF_ONLY_P (tf)
11498 && !EQ (other_frame, tip_frame)
11499 && (FRAME_VISIBLE_P (tf) || FRAME_ICONIFIED_P (tf)))
11500 break;
11503 /* Set global variable indicating that multiple frames exist. */
11504 multiple_frames = CONSP (tail);
11506 /* Switch to the buffer of selected window of the frame. Set up
11507 mode_line_target so that display_mode_element will output into
11508 mode_line_noprop_buf; then display the title. */
11509 record_unwind_protect (unwind_format_mode_line,
11510 format_mode_line_unwind_data
11511 (f, current_buffer, selected_window, 0));
11513 Fselect_window (f->selected_window, Qt);
11514 set_buffer_internal_1
11515 (XBUFFER (XWINDOW (f->selected_window)->contents));
11516 fmt = FRAME_ICONIFIED_P (f) ? Vicon_title_format : Vframe_title_format;
11518 mode_line_target = MODE_LINE_TITLE;
11519 title_start = MODE_LINE_NOPROP_LEN (0);
11520 init_iterator (&it, XWINDOW (f->selected_window), -1, -1,
11521 NULL, DEFAULT_FACE_ID);
11522 display_mode_element (&it, 0, -1, -1, fmt, Qnil, 0);
11523 len = MODE_LINE_NOPROP_LEN (title_start);
11524 title = mode_line_noprop_buf + title_start;
11525 unbind_to (count, Qnil);
11527 /* Set the title only if it's changed. This avoids consing in
11528 the common case where it hasn't. (If it turns out that we've
11529 already wasted too much time by walking through the list with
11530 display_mode_element, then we might need to optimize at a
11531 higher level than this.) */
11532 if (! STRINGP (f->name)
11533 || SBYTES (f->name) != len
11534 || memcmp (title, SDATA (f->name), len) != 0)
11535 x_implicitly_set_name (f, make_string (title, len), Qnil);
11539 #endif /* not HAVE_WINDOW_SYSTEM */
11542 /***********************************************************************
11543 Menu Bars
11544 ***********************************************************************/
11546 /* Non-zero if we will not redisplay all visible windows. */
11547 #define REDISPLAY_SOME_P() \
11548 ((windows_or_buffers_changed == 0 \
11549 || windows_or_buffers_changed == REDISPLAY_SOME) \
11550 && (update_mode_lines == 0 \
11551 || update_mode_lines == REDISPLAY_SOME))
11553 /* Prepare for redisplay by updating menu-bar item lists when
11554 appropriate. This can call eval. */
11556 static void
11557 prepare_menu_bars (void)
11559 bool all_windows = windows_or_buffers_changed || update_mode_lines;
11560 bool some_windows = REDISPLAY_SOME_P ();
11561 struct gcpro gcpro1, gcpro2;
11562 Lisp_Object tooltip_frame;
11564 #ifdef HAVE_WINDOW_SYSTEM
11565 tooltip_frame = tip_frame;
11566 #else
11567 tooltip_frame = Qnil;
11568 #endif
11570 if (FUNCTIONP (Vpre_redisplay_function))
11572 Lisp_Object windows = all_windows ? Qt : Qnil;
11573 if (all_windows && some_windows)
11575 Lisp_Object ws = window_list ();
11576 for (windows = Qnil; CONSP (ws); ws = XCDR (ws))
11578 Lisp_Object this = XCAR (ws);
11579 struct window *w = XWINDOW (this);
11580 if (w->redisplay
11581 || XFRAME (w->frame)->redisplay
11582 || XBUFFER (w->contents)->text->redisplay)
11584 windows = Fcons (this, windows);
11588 safe__call1 (true, Vpre_redisplay_function, windows);
11591 /* Update all frame titles based on their buffer names, etc. We do
11592 this before the menu bars so that the buffer-menu will show the
11593 up-to-date frame titles. */
11594 #ifdef HAVE_WINDOW_SYSTEM
11595 if (all_windows)
11597 Lisp_Object tail, frame;
11599 FOR_EACH_FRAME (tail, frame)
11601 struct frame *f = XFRAME (frame);
11602 struct window *w = XWINDOW (FRAME_SELECTED_WINDOW (f));
11603 if (some_windows
11604 && !f->redisplay
11605 && !w->redisplay
11606 && !XBUFFER (w->contents)->text->redisplay)
11607 continue;
11609 if (!EQ (frame, tooltip_frame)
11610 && (FRAME_ICONIFIED_P (f)
11611 || FRAME_VISIBLE_P (f) == 1
11612 /* Exclude TTY frames that are obscured because they
11613 are not the top frame on their console. This is
11614 because x_consider_frame_title actually switches
11615 to the frame, which for TTY frames means it is
11616 marked as garbaged, and will be completely
11617 redrawn on the next redisplay cycle. This causes
11618 TTY frames to be completely redrawn, when there
11619 are more than one of them, even though nothing
11620 should be changed on display. */
11621 || (FRAME_VISIBLE_P (f) == 2 && FRAME_WINDOW_P (f))))
11622 x_consider_frame_title (frame);
11625 #endif /* HAVE_WINDOW_SYSTEM */
11627 /* Update the menu bar item lists, if appropriate. This has to be
11628 done before any actual redisplay or generation of display lines. */
11630 if (all_windows)
11632 Lisp_Object tail, frame;
11633 ptrdiff_t count = SPECPDL_INDEX ();
11634 /* 1 means that update_menu_bar has run its hooks
11635 so any further calls to update_menu_bar shouldn't do so again. */
11636 int menu_bar_hooks_run = 0;
11638 record_unwind_save_match_data ();
11640 FOR_EACH_FRAME (tail, frame)
11642 struct frame *f = XFRAME (frame);
11643 struct window *w = XWINDOW (FRAME_SELECTED_WINDOW (f));
11645 /* Ignore tooltip frame. */
11646 if (EQ (frame, tooltip_frame))
11647 continue;
11649 if (some_windows
11650 && !f->redisplay
11651 && !w->redisplay
11652 && !XBUFFER (w->contents)->text->redisplay)
11653 continue;
11655 /* If a window on this frame changed size, report that to
11656 the user and clear the size-change flag. */
11657 if (FRAME_WINDOW_SIZES_CHANGED (f))
11659 Lisp_Object functions;
11661 /* Clear flag first in case we get an error below. */
11662 FRAME_WINDOW_SIZES_CHANGED (f) = 0;
11663 functions = Vwindow_size_change_functions;
11664 GCPRO2 (tail, functions);
11666 while (CONSP (functions))
11668 if (!EQ (XCAR (functions), Qt))
11669 call1 (XCAR (functions), frame);
11670 functions = XCDR (functions);
11672 UNGCPRO;
11675 GCPRO1 (tail);
11676 menu_bar_hooks_run = update_menu_bar (f, 0, menu_bar_hooks_run);
11677 #ifdef HAVE_WINDOW_SYSTEM
11678 update_tool_bar (f, 0);
11679 #endif
11680 UNGCPRO;
11683 unbind_to (count, Qnil);
11685 else
11687 struct frame *sf = SELECTED_FRAME ();
11688 update_menu_bar (sf, 1, 0);
11689 #ifdef HAVE_WINDOW_SYSTEM
11690 update_tool_bar (sf, 1);
11691 #endif
11696 /* Update the menu bar item list for frame F. This has to be done
11697 before we start to fill in any display lines, because it can call
11698 eval.
11700 If SAVE_MATCH_DATA is non-zero, we must save and restore it here.
11702 If HOOKS_RUN is 1, that means a previous call to update_menu_bar
11703 already ran the menu bar hooks for this redisplay, so there
11704 is no need to run them again. The return value is the
11705 updated value of this flag, to pass to the next call. */
11707 static int
11708 update_menu_bar (struct frame *f, int save_match_data, int hooks_run)
11710 Lisp_Object window;
11711 register struct window *w;
11713 /* If called recursively during a menu update, do nothing. This can
11714 happen when, for instance, an activate-menubar-hook causes a
11715 redisplay. */
11716 if (inhibit_menubar_update)
11717 return hooks_run;
11719 window = FRAME_SELECTED_WINDOW (f);
11720 w = XWINDOW (window);
11722 if (FRAME_WINDOW_P (f)
11724 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
11725 || defined (HAVE_NS) || defined (USE_GTK)
11726 FRAME_EXTERNAL_MENU_BAR (f)
11727 #else
11728 FRAME_MENU_BAR_LINES (f) > 0
11729 #endif
11730 : FRAME_MENU_BAR_LINES (f) > 0)
11732 /* If the user has switched buffers or windows, we need to
11733 recompute to reflect the new bindings. But we'll
11734 recompute when update_mode_lines is set too; that means
11735 that people can use force-mode-line-update to request
11736 that the menu bar be recomputed. The adverse effect on
11737 the rest of the redisplay algorithm is about the same as
11738 windows_or_buffers_changed anyway. */
11739 if (windows_or_buffers_changed
11740 /* This used to test w->update_mode_line, but we believe
11741 there is no need to recompute the menu in that case. */
11742 || update_mode_lines
11743 || window_buffer_changed (w))
11745 struct buffer *prev = current_buffer;
11746 ptrdiff_t count = SPECPDL_INDEX ();
11748 specbind (Qinhibit_menubar_update, Qt);
11750 set_buffer_internal_1 (XBUFFER (w->contents));
11751 if (save_match_data)
11752 record_unwind_save_match_data ();
11753 if (NILP (Voverriding_local_map_menu_flag))
11755 specbind (Qoverriding_terminal_local_map, Qnil);
11756 specbind (Qoverriding_local_map, Qnil);
11759 if (!hooks_run)
11761 /* Run the Lucid hook. */
11762 safe_run_hooks (Qactivate_menubar_hook);
11764 /* If it has changed current-menubar from previous value,
11765 really recompute the menu-bar from the value. */
11766 if (! NILP (Vlucid_menu_bar_dirty_flag))
11767 call0 (Qrecompute_lucid_menubar);
11769 safe_run_hooks (Qmenu_bar_update_hook);
11771 hooks_run = 1;
11774 XSETFRAME (Vmenu_updating_frame, f);
11775 fset_menu_bar_items (f, menu_bar_items (FRAME_MENU_BAR_ITEMS (f)));
11777 /* Redisplay the menu bar in case we changed it. */
11778 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
11779 || defined (HAVE_NS) || defined (USE_GTK)
11780 if (FRAME_WINDOW_P (f))
11782 #if defined (HAVE_NS)
11783 /* All frames on Mac OS share the same menubar. So only
11784 the selected frame should be allowed to set it. */
11785 if (f == SELECTED_FRAME ())
11786 #endif
11787 set_frame_menubar (f, 0, 0);
11789 else
11790 /* On a terminal screen, the menu bar is an ordinary screen
11791 line, and this makes it get updated. */
11792 w->update_mode_line = 1;
11793 #else /* ! (USE_X_TOOLKIT || HAVE_NTGUI || HAVE_NS || USE_GTK) */
11794 /* In the non-toolkit version, the menu bar is an ordinary screen
11795 line, and this makes it get updated. */
11796 w->update_mode_line = 1;
11797 #endif /* ! (USE_X_TOOLKIT || HAVE_NTGUI || HAVE_NS || USE_GTK) */
11799 unbind_to (count, Qnil);
11800 set_buffer_internal_1 (prev);
11804 return hooks_run;
11807 /***********************************************************************
11808 Tool-bars
11809 ***********************************************************************/
11811 #ifdef HAVE_WINDOW_SYSTEM
11813 /* Select `frame' temporarily without running all the code in
11814 do_switch_frame.
11815 FIXME: Maybe do_switch_frame should be trimmed down similarly
11816 when `norecord' is set. */
11817 static void
11818 fast_set_selected_frame (Lisp_Object frame)
11820 if (!EQ (selected_frame, frame))
11822 selected_frame = frame;
11823 selected_window = XFRAME (frame)->selected_window;
11827 /* Update the tool-bar item list for frame F. This has to be done
11828 before we start to fill in any display lines. Called from
11829 prepare_menu_bars. If SAVE_MATCH_DATA is non-zero, we must save
11830 and restore it here. */
11832 static void
11833 update_tool_bar (struct frame *f, int save_match_data)
11835 #if defined (USE_GTK) || defined (HAVE_NS)
11836 int do_update = FRAME_EXTERNAL_TOOL_BAR (f);
11837 #else
11838 int do_update = (WINDOWP (f->tool_bar_window)
11839 && WINDOW_TOTAL_LINES (XWINDOW (f->tool_bar_window)) > 0);
11840 #endif
11842 if (do_update)
11844 Lisp_Object window;
11845 struct window *w;
11847 window = FRAME_SELECTED_WINDOW (f);
11848 w = XWINDOW (window);
11850 /* If the user has switched buffers or windows, we need to
11851 recompute to reflect the new bindings. But we'll
11852 recompute when update_mode_lines is set too; that means
11853 that people can use force-mode-line-update to request
11854 that the menu bar be recomputed. The adverse effect on
11855 the rest of the redisplay algorithm is about the same as
11856 windows_or_buffers_changed anyway. */
11857 if (windows_or_buffers_changed
11858 || w->update_mode_line
11859 || update_mode_lines
11860 || window_buffer_changed (w))
11862 struct buffer *prev = current_buffer;
11863 ptrdiff_t count = SPECPDL_INDEX ();
11864 Lisp_Object frame, new_tool_bar;
11865 int new_n_tool_bar;
11866 struct gcpro gcpro1;
11868 /* Set current_buffer to the buffer of the selected
11869 window of the frame, so that we get the right local
11870 keymaps. */
11871 set_buffer_internal_1 (XBUFFER (w->contents));
11873 /* Save match data, if we must. */
11874 if (save_match_data)
11875 record_unwind_save_match_data ();
11877 /* Make sure that we don't accidentally use bogus keymaps. */
11878 if (NILP (Voverriding_local_map_menu_flag))
11880 specbind (Qoverriding_terminal_local_map, Qnil);
11881 specbind (Qoverriding_local_map, Qnil);
11884 GCPRO1 (new_tool_bar);
11886 /* We must temporarily set the selected frame to this frame
11887 before calling tool_bar_items, because the calculation of
11888 the tool-bar keymap uses the selected frame (see
11889 `tool-bar-make-keymap' in tool-bar.el). */
11890 eassert (EQ (selected_window,
11891 /* Since we only explicitly preserve selected_frame,
11892 check that selected_window would be redundant. */
11893 XFRAME (selected_frame)->selected_window));
11894 record_unwind_protect (fast_set_selected_frame, selected_frame);
11895 XSETFRAME (frame, f);
11896 fast_set_selected_frame (frame);
11898 /* Build desired tool-bar items from keymaps. */
11899 new_tool_bar
11900 = tool_bar_items (Fcopy_sequence (f->tool_bar_items),
11901 &new_n_tool_bar);
11903 /* Redisplay the tool-bar if we changed it. */
11904 if (new_n_tool_bar != f->n_tool_bar_items
11905 || NILP (Fequal (new_tool_bar, f->tool_bar_items)))
11907 /* Redisplay that happens asynchronously due to an expose event
11908 may access f->tool_bar_items. Make sure we update both
11909 variables within BLOCK_INPUT so no such event interrupts. */
11910 block_input ();
11911 fset_tool_bar_items (f, new_tool_bar);
11912 f->n_tool_bar_items = new_n_tool_bar;
11913 w->update_mode_line = 1;
11914 unblock_input ();
11917 UNGCPRO;
11919 unbind_to (count, Qnil);
11920 set_buffer_internal_1 (prev);
11925 #if ! defined (USE_GTK) && ! defined (HAVE_NS)
11927 /* Set F->desired_tool_bar_string to a Lisp string representing frame
11928 F's desired tool-bar contents. F->tool_bar_items must have
11929 been set up previously by calling prepare_menu_bars. */
11931 static void
11932 build_desired_tool_bar_string (struct frame *f)
11934 int i, size, size_needed;
11935 struct gcpro gcpro1, gcpro2;
11936 Lisp_Object image, plist;
11938 image = plist = Qnil;
11939 GCPRO2 (image, plist);
11941 /* Prepare F->desired_tool_bar_string. If we can reuse it, do so.
11942 Otherwise, make a new string. */
11944 /* The size of the string we might be able to reuse. */
11945 size = (STRINGP (f->desired_tool_bar_string)
11946 ? SCHARS (f->desired_tool_bar_string)
11947 : 0);
11949 /* We need one space in the string for each image. */
11950 size_needed = f->n_tool_bar_items;
11952 /* Reuse f->desired_tool_bar_string, if possible. */
11953 if (size < size_needed || NILP (f->desired_tool_bar_string))
11954 fset_desired_tool_bar_string
11955 (f, Fmake_string (make_number (size_needed), make_number (' ')));
11956 else
11958 AUTO_LIST4 (props, Qdisplay, Qnil, Qmenu_item, Qnil);
11959 struct gcpro gcpro1;
11960 GCPRO1 (props);
11961 Fremove_text_properties (make_number (0), make_number (size),
11962 props, f->desired_tool_bar_string);
11963 UNGCPRO;
11966 /* Put a `display' property on the string for the images to display,
11967 put a `menu_item' property on tool-bar items with a value that
11968 is the index of the item in F's tool-bar item vector. */
11969 for (i = 0; i < f->n_tool_bar_items; ++i)
11971 #define PROP(IDX) \
11972 AREF (f->tool_bar_items, i * TOOL_BAR_ITEM_NSLOTS + (IDX))
11974 int enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
11975 int selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
11976 int hmargin, vmargin, relief, idx, end;
11978 /* If image is a vector, choose the image according to the
11979 button state. */
11980 image = PROP (TOOL_BAR_ITEM_IMAGES);
11981 if (VECTORP (image))
11983 if (enabled_p)
11984 idx = (selected_p
11985 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
11986 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
11987 else
11988 idx = (selected_p
11989 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
11990 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
11992 eassert (ASIZE (image) >= idx);
11993 image = AREF (image, idx);
11995 else
11996 idx = -1;
11998 /* Ignore invalid image specifications. */
11999 if (!valid_image_p (image))
12000 continue;
12002 /* Display the tool-bar button pressed, or depressed. */
12003 plist = Fcopy_sequence (XCDR (image));
12005 /* Compute margin and relief to draw. */
12006 relief = (tool_bar_button_relief >= 0
12007 ? tool_bar_button_relief
12008 : DEFAULT_TOOL_BAR_BUTTON_RELIEF);
12009 hmargin = vmargin = relief;
12011 if (RANGED_INTEGERP (1, Vtool_bar_button_margin,
12012 INT_MAX - max (hmargin, vmargin)))
12014 hmargin += XFASTINT (Vtool_bar_button_margin);
12015 vmargin += XFASTINT (Vtool_bar_button_margin);
12017 else if (CONSP (Vtool_bar_button_margin))
12019 if (RANGED_INTEGERP (1, XCAR (Vtool_bar_button_margin),
12020 INT_MAX - hmargin))
12021 hmargin += XFASTINT (XCAR (Vtool_bar_button_margin));
12023 if (RANGED_INTEGERP (1, XCDR (Vtool_bar_button_margin),
12024 INT_MAX - vmargin))
12025 vmargin += XFASTINT (XCDR (Vtool_bar_button_margin));
12028 if (auto_raise_tool_bar_buttons_p)
12030 /* Add a `:relief' property to the image spec if the item is
12031 selected. */
12032 if (selected_p)
12034 plist = Fplist_put (plist, QCrelief, make_number (-relief));
12035 hmargin -= relief;
12036 vmargin -= relief;
12039 else
12041 /* If image is selected, display it pressed, i.e. with a
12042 negative relief. If it's not selected, display it with a
12043 raised relief. */
12044 plist = Fplist_put (plist, QCrelief,
12045 (selected_p
12046 ? make_number (-relief)
12047 : make_number (relief)));
12048 hmargin -= relief;
12049 vmargin -= relief;
12052 /* Put a margin around the image. */
12053 if (hmargin || vmargin)
12055 if (hmargin == vmargin)
12056 plist = Fplist_put (plist, QCmargin, make_number (hmargin));
12057 else
12058 plist = Fplist_put (plist, QCmargin,
12059 Fcons (make_number (hmargin),
12060 make_number (vmargin)));
12063 /* If button is not enabled, and we don't have special images
12064 for the disabled state, make the image appear disabled by
12065 applying an appropriate algorithm to it. */
12066 if (!enabled_p && idx < 0)
12067 plist = Fplist_put (plist, QCconversion, Qdisabled);
12069 /* Put a `display' text property on the string for the image to
12070 display. Put a `menu-item' property on the string that gives
12071 the start of this item's properties in the tool-bar items
12072 vector. */
12073 image = Fcons (Qimage, plist);
12074 AUTO_LIST4 (props, Qdisplay, image, Qmenu_item,
12075 make_number (i * TOOL_BAR_ITEM_NSLOTS));
12076 struct gcpro gcpro1;
12077 GCPRO1 (props);
12079 /* Let the last image hide all remaining spaces in the tool bar
12080 string. The string can be longer than needed when we reuse a
12081 previous string. */
12082 if (i + 1 == f->n_tool_bar_items)
12083 end = SCHARS (f->desired_tool_bar_string);
12084 else
12085 end = i + 1;
12086 Fadd_text_properties (make_number (i), make_number (end),
12087 props, f->desired_tool_bar_string);
12088 UNGCPRO;
12089 #undef PROP
12092 UNGCPRO;
12096 /* Display one line of the tool-bar of frame IT->f.
12098 HEIGHT specifies the desired height of the tool-bar line.
12099 If the actual height of the glyph row is less than HEIGHT, the
12100 row's height is increased to HEIGHT, and the icons are centered
12101 vertically in the new height.
12103 If HEIGHT is -1, we are counting needed tool-bar lines, so don't
12104 count a final empty row in case the tool-bar width exactly matches
12105 the window width.
12108 static void
12109 display_tool_bar_line (struct it *it, int height)
12111 struct glyph_row *row = it->glyph_row;
12112 int max_x = it->last_visible_x;
12113 struct glyph *last;
12115 /* Don't extend on a previously drawn tool bar items (Bug#16058). */
12116 clear_glyph_row (row);
12117 row->enabled_p = true;
12118 row->y = it->current_y;
12120 /* Note that this isn't made use of if the face hasn't a box,
12121 so there's no need to check the face here. */
12122 it->start_of_box_run_p = 1;
12124 while (it->current_x < max_x)
12126 int x, n_glyphs_before, i, nglyphs;
12127 struct it it_before;
12129 /* Get the next display element. */
12130 if (!get_next_display_element (it))
12132 /* Don't count empty row if we are counting needed tool-bar lines. */
12133 if (height < 0 && !it->hpos)
12134 return;
12135 break;
12138 /* Produce glyphs. */
12139 n_glyphs_before = row->used[TEXT_AREA];
12140 it_before = *it;
12142 PRODUCE_GLYPHS (it);
12144 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
12145 i = 0;
12146 x = it_before.current_x;
12147 while (i < nglyphs)
12149 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
12151 if (x + glyph->pixel_width > max_x)
12153 /* Glyph doesn't fit on line. Backtrack. */
12154 row->used[TEXT_AREA] = n_glyphs_before;
12155 *it = it_before;
12156 /* If this is the only glyph on this line, it will never fit on the
12157 tool-bar, so skip it. But ensure there is at least one glyph,
12158 so we don't accidentally disable the tool-bar. */
12159 if (n_glyphs_before == 0
12160 && (it->vpos > 0 || IT_STRING_CHARPOS (*it) < it->end_charpos-1))
12161 break;
12162 goto out;
12165 ++it->hpos;
12166 x += glyph->pixel_width;
12167 ++i;
12170 /* Stop at line end. */
12171 if (ITERATOR_AT_END_OF_LINE_P (it))
12172 break;
12174 set_iterator_to_next (it, 1);
12177 out:;
12179 row->displays_text_p = row->used[TEXT_AREA] != 0;
12181 /* Use default face for the border below the tool bar.
12183 FIXME: When auto-resize-tool-bars is grow-only, there is
12184 no additional border below the possibly empty tool-bar lines.
12185 So to make the extra empty lines look "normal", we have to
12186 use the tool-bar face for the border too. */
12187 if (!MATRIX_ROW_DISPLAYS_TEXT_P (row)
12188 && !EQ (Vauto_resize_tool_bars, Qgrow_only))
12189 it->face_id = DEFAULT_FACE_ID;
12191 extend_face_to_end_of_line (it);
12192 last = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
12193 last->right_box_line_p = 1;
12194 if (last == row->glyphs[TEXT_AREA])
12195 last->left_box_line_p = 1;
12197 /* Make line the desired height and center it vertically. */
12198 if ((height -= it->max_ascent + it->max_descent) > 0)
12200 /* Don't add more than one line height. */
12201 height %= FRAME_LINE_HEIGHT (it->f);
12202 it->max_ascent += height / 2;
12203 it->max_descent += (height + 1) / 2;
12206 compute_line_metrics (it);
12208 /* If line is empty, make it occupy the rest of the tool-bar. */
12209 if (!MATRIX_ROW_DISPLAYS_TEXT_P (row))
12211 row->height = row->phys_height = it->last_visible_y - row->y;
12212 row->visible_height = row->height;
12213 row->ascent = row->phys_ascent = 0;
12214 row->extra_line_spacing = 0;
12217 row->full_width_p = 1;
12218 row->continued_p = 0;
12219 row->truncated_on_left_p = 0;
12220 row->truncated_on_right_p = 0;
12222 it->current_x = it->hpos = 0;
12223 it->current_y += row->height;
12224 ++it->vpos;
12225 ++it->glyph_row;
12229 /* Value is the number of pixels needed to make all tool-bar items of
12230 frame F visible. The actual number of glyph rows needed is
12231 returned in *N_ROWS if non-NULL. */
12232 static int
12233 tool_bar_height (struct frame *f, int *n_rows, bool pixelwise)
12235 struct window *w = XWINDOW (f->tool_bar_window);
12236 struct it it;
12237 /* tool_bar_height is called from redisplay_tool_bar after building
12238 the desired matrix, so use (unused) mode-line row as temporary row to
12239 avoid destroying the first tool-bar row. */
12240 struct glyph_row *temp_row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
12242 /* Initialize an iterator for iteration over
12243 F->desired_tool_bar_string in the tool-bar window of frame F. */
12244 init_iterator (&it, w, -1, -1, temp_row, TOOL_BAR_FACE_ID);
12245 temp_row->reversed_p = false;
12246 it.first_visible_x = 0;
12247 it.last_visible_x = WINDOW_PIXEL_WIDTH (w);
12248 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
12249 it.paragraph_embedding = L2R;
12251 while (!ITERATOR_AT_END_P (&it))
12253 clear_glyph_row (temp_row);
12254 it.glyph_row = temp_row;
12255 display_tool_bar_line (&it, -1);
12257 clear_glyph_row (temp_row);
12259 /* f->n_tool_bar_rows == 0 means "unknown"; -1 means no tool-bar. */
12260 if (n_rows)
12261 *n_rows = it.vpos > 0 ? it.vpos : -1;
12263 if (pixelwise)
12264 return it.current_y;
12265 else
12266 return (it.current_y + FRAME_LINE_HEIGHT (f) - 1) / FRAME_LINE_HEIGHT (f);
12269 #endif /* !USE_GTK && !HAVE_NS */
12271 DEFUN ("tool-bar-height", Ftool_bar_height, Stool_bar_height,
12272 0, 2, 0,
12273 doc: /* Return the number of lines occupied by the tool bar of FRAME.
12274 If FRAME is nil or omitted, use the selected frame. Optional argument
12275 PIXELWISE non-nil means return the height of the tool bar in pixels. */)
12276 (Lisp_Object frame, Lisp_Object pixelwise)
12278 int height = 0;
12280 #if ! defined (USE_GTK) && ! defined (HAVE_NS)
12281 struct frame *f = decode_any_frame (frame);
12283 if (WINDOWP (f->tool_bar_window)
12284 && WINDOW_PIXEL_HEIGHT (XWINDOW (f->tool_bar_window)) > 0)
12286 update_tool_bar (f, 1);
12287 if (f->n_tool_bar_items)
12289 build_desired_tool_bar_string (f);
12290 height = tool_bar_height (f, NULL, NILP (pixelwise) ? 0 : 1);
12293 #endif
12295 return make_number (height);
12299 /* Display the tool-bar of frame F. Value is non-zero if tool-bar's
12300 height should be changed. */
12301 static int
12302 redisplay_tool_bar (struct frame *f)
12304 #if defined (USE_GTK) || defined (HAVE_NS)
12306 if (FRAME_EXTERNAL_TOOL_BAR (f))
12307 update_frame_tool_bar (f);
12308 return 0;
12310 #else /* !USE_GTK && !HAVE_NS */
12312 struct window *w;
12313 struct it it;
12314 struct glyph_row *row;
12316 /* If frame hasn't a tool-bar window or if it is zero-height, don't
12317 do anything. This means you must start with tool-bar-lines
12318 non-zero to get the auto-sizing effect. Or in other words, you
12319 can turn off tool-bars by specifying tool-bar-lines zero. */
12320 if (!WINDOWP (f->tool_bar_window)
12321 || (w = XWINDOW (f->tool_bar_window),
12322 WINDOW_TOTAL_LINES (w) == 0))
12323 return 0;
12325 /* Set up an iterator for the tool-bar window. */
12326 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
12327 it.first_visible_x = 0;
12328 it.last_visible_x = WINDOW_PIXEL_WIDTH (w);
12329 row = it.glyph_row;
12330 row->reversed_p = false;
12332 /* Build a string that represents the contents of the tool-bar. */
12333 build_desired_tool_bar_string (f);
12334 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
12335 /* FIXME: This should be controlled by a user option. But it
12336 doesn't make sense to have an R2L tool bar if the menu bar cannot
12337 be drawn also R2L, and making the menu bar R2L is tricky due
12338 toolkit-specific code that implements it. If an R2L tool bar is
12339 ever supported, display_tool_bar_line should also be augmented to
12340 call unproduce_glyphs like display_line and display_string
12341 do. */
12342 it.paragraph_embedding = L2R;
12344 if (f->n_tool_bar_rows == 0)
12346 int new_height = tool_bar_height (f, &f->n_tool_bar_rows, 1);
12348 if (new_height != WINDOW_PIXEL_HEIGHT (w))
12350 x_change_tool_bar_height (f, new_height);
12351 frame_default_tool_bar_height = new_height;
12352 /* Always do that now. */
12353 clear_glyph_matrix (w->desired_matrix);
12354 f->fonts_changed = 1;
12355 return 1;
12359 /* Display as many lines as needed to display all tool-bar items. */
12361 if (f->n_tool_bar_rows > 0)
12363 int border, rows, height, extra;
12365 if (TYPE_RANGED_INTEGERP (int, Vtool_bar_border))
12366 border = XINT (Vtool_bar_border);
12367 else if (EQ (Vtool_bar_border, Qinternal_border_width))
12368 border = FRAME_INTERNAL_BORDER_WIDTH (f);
12369 else if (EQ (Vtool_bar_border, Qborder_width))
12370 border = f->border_width;
12371 else
12372 border = 0;
12373 if (border < 0)
12374 border = 0;
12376 rows = f->n_tool_bar_rows;
12377 height = max (1, (it.last_visible_y - border) / rows);
12378 extra = it.last_visible_y - border - height * rows;
12380 while (it.current_y < it.last_visible_y)
12382 int h = 0;
12383 if (extra > 0 && rows-- > 0)
12385 h = (extra + rows - 1) / rows;
12386 extra -= h;
12388 display_tool_bar_line (&it, height + h);
12391 else
12393 while (it.current_y < it.last_visible_y)
12394 display_tool_bar_line (&it, 0);
12397 /* It doesn't make much sense to try scrolling in the tool-bar
12398 window, so don't do it. */
12399 w->desired_matrix->no_scrolling_p = 1;
12400 w->must_be_updated_p = 1;
12402 if (!NILP (Vauto_resize_tool_bars))
12404 int change_height_p = 0;
12406 /* If we couldn't display everything, change the tool-bar's
12407 height if there is room for more. */
12408 if (IT_STRING_CHARPOS (it) < it.end_charpos)
12409 change_height_p = 1;
12411 /* We subtract 1 because display_tool_bar_line advances the
12412 glyph_row pointer before returning to its caller. We want to
12413 examine the last glyph row produced by
12414 display_tool_bar_line. */
12415 row = it.glyph_row - 1;
12417 /* If there are blank lines at the end, except for a partially
12418 visible blank line at the end that is smaller than
12419 FRAME_LINE_HEIGHT, change the tool-bar's height. */
12420 if (!MATRIX_ROW_DISPLAYS_TEXT_P (row)
12421 && row->height >= FRAME_LINE_HEIGHT (f))
12422 change_height_p = 1;
12424 /* If row displays tool-bar items, but is partially visible,
12425 change the tool-bar's height. */
12426 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
12427 && MATRIX_ROW_BOTTOM_Y (row) > it.last_visible_y)
12428 change_height_p = 1;
12430 /* Resize windows as needed by changing the `tool-bar-lines'
12431 frame parameter. */
12432 if (change_height_p)
12434 int nrows;
12435 int new_height = tool_bar_height (f, &nrows, 1);
12437 change_height_p = ((EQ (Vauto_resize_tool_bars, Qgrow_only)
12438 && !f->minimize_tool_bar_window_p)
12439 ? (new_height > WINDOW_PIXEL_HEIGHT (w))
12440 : (new_height != WINDOW_PIXEL_HEIGHT (w)));
12441 f->minimize_tool_bar_window_p = 0;
12443 if (change_height_p)
12445 x_change_tool_bar_height (f, new_height);
12446 frame_default_tool_bar_height = new_height;
12447 clear_glyph_matrix (w->desired_matrix);
12448 f->n_tool_bar_rows = nrows;
12449 f->fonts_changed = 1;
12451 return 1;
12456 f->minimize_tool_bar_window_p = 0;
12457 return 0;
12459 #endif /* USE_GTK || HAVE_NS */
12462 #if ! defined (USE_GTK) && ! defined (HAVE_NS)
12464 /* Get information about the tool-bar item which is displayed in GLYPH
12465 on frame F. Return in *PROP_IDX the index where tool-bar item
12466 properties start in F->tool_bar_items. Value is zero if
12467 GLYPH doesn't display a tool-bar item. */
12469 static int
12470 tool_bar_item_info (struct frame *f, struct glyph *glyph, int *prop_idx)
12472 Lisp_Object prop;
12473 int success_p;
12474 int charpos;
12476 /* This function can be called asynchronously, which means we must
12477 exclude any possibility that Fget_text_property signals an
12478 error. */
12479 charpos = min (SCHARS (f->current_tool_bar_string), glyph->charpos);
12480 charpos = max (0, charpos);
12482 /* Get the text property `menu-item' at pos. The value of that
12483 property is the start index of this item's properties in
12484 F->tool_bar_items. */
12485 prop = Fget_text_property (make_number (charpos),
12486 Qmenu_item, f->current_tool_bar_string);
12487 if (INTEGERP (prop))
12489 *prop_idx = XINT (prop);
12490 success_p = 1;
12492 else
12493 success_p = 0;
12495 return success_p;
12499 /* Get information about the tool-bar item at position X/Y on frame F.
12500 Return in *GLYPH a pointer to the glyph of the tool-bar item in
12501 the current matrix of the tool-bar window of F, or NULL if not
12502 on a tool-bar item. Return in *PROP_IDX the index of the tool-bar
12503 item in F->tool_bar_items. Value is
12505 -1 if X/Y is not on a tool-bar item
12506 0 if X/Y is on the same item that was highlighted before.
12507 1 otherwise. */
12509 static int
12510 get_tool_bar_item (struct frame *f, int x, int y, struct glyph **glyph,
12511 int *hpos, int *vpos, int *prop_idx)
12513 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12514 struct window *w = XWINDOW (f->tool_bar_window);
12515 int area;
12517 /* Find the glyph under X/Y. */
12518 *glyph = x_y_to_hpos_vpos (w, x, y, hpos, vpos, 0, 0, &area);
12519 if (*glyph == NULL)
12520 return -1;
12522 /* Get the start of this tool-bar item's properties in
12523 f->tool_bar_items. */
12524 if (!tool_bar_item_info (f, *glyph, prop_idx))
12525 return -1;
12527 /* Is mouse on the highlighted item? */
12528 if (EQ (f->tool_bar_window, hlinfo->mouse_face_window)
12529 && *vpos >= hlinfo->mouse_face_beg_row
12530 && *vpos <= hlinfo->mouse_face_end_row
12531 && (*vpos > hlinfo->mouse_face_beg_row
12532 || *hpos >= hlinfo->mouse_face_beg_col)
12533 && (*vpos < hlinfo->mouse_face_end_row
12534 || *hpos < hlinfo->mouse_face_end_col
12535 || hlinfo->mouse_face_past_end))
12536 return 0;
12538 return 1;
12542 /* EXPORT:
12543 Handle mouse button event on the tool-bar of frame F, at
12544 frame-relative coordinates X/Y. DOWN_P is 1 for a button press,
12545 0 for button release. MODIFIERS is event modifiers for button
12546 release. */
12548 void
12549 handle_tool_bar_click (struct frame *f, int x, int y, int down_p,
12550 int modifiers)
12552 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12553 struct window *w = XWINDOW (f->tool_bar_window);
12554 int hpos, vpos, prop_idx;
12555 struct glyph *glyph;
12556 Lisp_Object enabled_p;
12557 int ts;
12559 /* If not on the highlighted tool-bar item, and mouse-highlight is
12560 non-nil, return. This is so we generate the tool-bar button
12561 click only when the mouse button is released on the same item as
12562 where it was pressed. However, when mouse-highlight is disabled,
12563 generate the click when the button is released regardless of the
12564 highlight, since tool-bar items are not highlighted in that
12565 case. */
12566 frame_to_window_pixel_xy (w, &x, &y);
12567 ts = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
12568 if (ts == -1
12569 || (ts != 0 && !NILP (Vmouse_highlight)))
12570 return;
12572 /* When mouse-highlight is off, generate the click for the item
12573 where the button was pressed, disregarding where it was
12574 released. */
12575 if (NILP (Vmouse_highlight) && !down_p)
12576 prop_idx = f->last_tool_bar_item;
12578 /* If item is disabled, do nothing. */
12579 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
12580 if (NILP (enabled_p))
12581 return;
12583 if (down_p)
12585 /* Show item in pressed state. */
12586 if (!NILP (Vmouse_highlight))
12587 show_mouse_face (hlinfo, DRAW_IMAGE_SUNKEN);
12588 f->last_tool_bar_item = prop_idx;
12590 else
12592 Lisp_Object key, frame;
12593 struct input_event event;
12594 EVENT_INIT (event);
12596 /* Show item in released state. */
12597 if (!NILP (Vmouse_highlight))
12598 show_mouse_face (hlinfo, DRAW_IMAGE_RAISED);
12600 key = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_KEY);
12602 XSETFRAME (frame, f);
12603 event.kind = TOOL_BAR_EVENT;
12604 event.frame_or_window = frame;
12605 event.arg = frame;
12606 kbd_buffer_store_event (&event);
12608 event.kind = TOOL_BAR_EVENT;
12609 event.frame_or_window = frame;
12610 event.arg = key;
12611 event.modifiers = modifiers;
12612 kbd_buffer_store_event (&event);
12613 f->last_tool_bar_item = -1;
12618 /* Possibly highlight a tool-bar item on frame F when mouse moves to
12619 tool-bar window-relative coordinates X/Y. Called from
12620 note_mouse_highlight. */
12622 static void
12623 note_tool_bar_highlight (struct frame *f, int x, int y)
12625 Lisp_Object window = f->tool_bar_window;
12626 struct window *w = XWINDOW (window);
12627 Display_Info *dpyinfo = FRAME_DISPLAY_INFO (f);
12628 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12629 int hpos, vpos;
12630 struct glyph *glyph;
12631 struct glyph_row *row;
12632 int i;
12633 Lisp_Object enabled_p;
12634 int prop_idx;
12635 enum draw_glyphs_face draw = DRAW_IMAGE_RAISED;
12636 int mouse_down_p, rc;
12638 /* Function note_mouse_highlight is called with negative X/Y
12639 values when mouse moves outside of the frame. */
12640 if (x <= 0 || y <= 0)
12642 clear_mouse_face (hlinfo);
12643 return;
12646 rc = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
12647 if (rc < 0)
12649 /* Not on tool-bar item. */
12650 clear_mouse_face (hlinfo);
12651 return;
12653 else if (rc == 0)
12654 /* On same tool-bar item as before. */
12655 goto set_help_echo;
12657 clear_mouse_face (hlinfo);
12659 /* Mouse is down, but on different tool-bar item? */
12660 mouse_down_p = (x_mouse_grabbed (dpyinfo)
12661 && f == dpyinfo->last_mouse_frame);
12663 if (mouse_down_p && f->last_tool_bar_item != prop_idx)
12664 return;
12666 draw = mouse_down_p ? DRAW_IMAGE_SUNKEN : DRAW_IMAGE_RAISED;
12668 /* If tool-bar item is not enabled, don't highlight it. */
12669 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
12670 if (!NILP (enabled_p) && !NILP (Vmouse_highlight))
12672 /* Compute the x-position of the glyph. In front and past the
12673 image is a space. We include this in the highlighted area. */
12674 row = MATRIX_ROW (w->current_matrix, vpos);
12675 for (i = x = 0; i < hpos; ++i)
12676 x += row->glyphs[TEXT_AREA][i].pixel_width;
12678 /* Record this as the current active region. */
12679 hlinfo->mouse_face_beg_col = hpos;
12680 hlinfo->mouse_face_beg_row = vpos;
12681 hlinfo->mouse_face_beg_x = x;
12682 hlinfo->mouse_face_past_end = 0;
12684 hlinfo->mouse_face_end_col = hpos + 1;
12685 hlinfo->mouse_face_end_row = vpos;
12686 hlinfo->mouse_face_end_x = x + glyph->pixel_width;
12687 hlinfo->mouse_face_window = window;
12688 hlinfo->mouse_face_face_id = TOOL_BAR_FACE_ID;
12690 /* Display it as active. */
12691 show_mouse_face (hlinfo, draw);
12694 set_help_echo:
12696 /* Set help_echo_string to a help string to display for this tool-bar item.
12697 XTread_socket does the rest. */
12698 help_echo_object = help_echo_window = Qnil;
12699 help_echo_pos = -1;
12700 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_HELP);
12701 if (NILP (help_echo_string))
12702 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_CAPTION);
12705 #endif /* !USE_GTK && !HAVE_NS */
12707 #endif /* HAVE_WINDOW_SYSTEM */
12711 /************************************************************************
12712 Horizontal scrolling
12713 ************************************************************************/
12715 static int hscroll_window_tree (Lisp_Object);
12716 static int hscroll_windows (Lisp_Object);
12718 /* For all leaf windows in the window tree rooted at WINDOW, set their
12719 hscroll value so that PT is (i) visible in the window, and (ii) so
12720 that it is not within a certain margin at the window's left and
12721 right border. Value is non-zero if any window's hscroll has been
12722 changed. */
12724 static int
12725 hscroll_window_tree (Lisp_Object window)
12727 int hscrolled_p = 0;
12728 int hscroll_relative_p = FLOATP (Vhscroll_step);
12729 int hscroll_step_abs = 0;
12730 double hscroll_step_rel = 0;
12732 if (hscroll_relative_p)
12734 hscroll_step_rel = XFLOAT_DATA (Vhscroll_step);
12735 if (hscroll_step_rel < 0)
12737 hscroll_relative_p = 0;
12738 hscroll_step_abs = 0;
12741 else if (TYPE_RANGED_INTEGERP (int, Vhscroll_step))
12743 hscroll_step_abs = XINT (Vhscroll_step);
12744 if (hscroll_step_abs < 0)
12745 hscroll_step_abs = 0;
12747 else
12748 hscroll_step_abs = 0;
12750 while (WINDOWP (window))
12752 struct window *w = XWINDOW (window);
12754 if (WINDOWP (w->contents))
12755 hscrolled_p |= hscroll_window_tree (w->contents);
12756 else if (w->cursor.vpos >= 0)
12758 int h_margin;
12759 int text_area_width;
12760 struct glyph_row *cursor_row;
12761 struct glyph_row *bottom_row;
12762 int row_r2l_p;
12764 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->desired_matrix, w);
12765 if (w->cursor.vpos < bottom_row - w->desired_matrix->rows)
12766 cursor_row = MATRIX_ROW (w->desired_matrix, w->cursor.vpos);
12767 else
12768 cursor_row = bottom_row - 1;
12770 if (!cursor_row->enabled_p)
12772 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
12773 if (w->cursor.vpos < bottom_row - w->current_matrix->rows)
12774 cursor_row = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
12775 else
12776 cursor_row = bottom_row - 1;
12778 row_r2l_p = cursor_row->reversed_p;
12780 text_area_width = window_box_width (w, TEXT_AREA);
12782 /* Scroll when cursor is inside this scroll margin. */
12783 h_margin = hscroll_margin * WINDOW_FRAME_COLUMN_WIDTH (w);
12785 /* If the position of this window's point has explicitly
12786 changed, no more suspend auto hscrolling. */
12787 if (NILP (Fequal (Fwindow_point (window), Fwindow_old_point (window))))
12788 w->suspend_auto_hscroll = 0;
12790 /* Remember window point. */
12791 Fset_marker (w->old_pointm,
12792 ((w == XWINDOW (selected_window))
12793 ? make_number (BUF_PT (XBUFFER (w->contents)))
12794 : Fmarker_position (w->pointm)),
12795 w->contents);
12797 if (!NILP (Fbuffer_local_value (Qauto_hscroll_mode, w->contents))
12798 && w->suspend_auto_hscroll == 0
12799 /* In some pathological cases, like restoring a window
12800 configuration into a frame that is much smaller than
12801 the one from which the configuration was saved, we
12802 get glyph rows whose start and end have zero buffer
12803 positions, which we cannot handle below. Just skip
12804 such windows. */
12805 && CHARPOS (cursor_row->start.pos) >= BUF_BEG (w->contents)
12806 /* For left-to-right rows, hscroll when cursor is either
12807 (i) inside the right hscroll margin, or (ii) if it is
12808 inside the left margin and the window is already
12809 hscrolled. */
12810 && ((!row_r2l_p
12811 && ((w->hscroll && w->cursor.x <= h_margin)
12812 || (cursor_row->enabled_p
12813 && cursor_row->truncated_on_right_p
12814 && (w->cursor.x >= text_area_width - h_margin))))
12815 /* For right-to-left rows, the logic is similar,
12816 except that rules for scrolling to left and right
12817 are reversed. E.g., if cursor.x <= h_margin, we
12818 need to hscroll "to the right" unconditionally,
12819 and that will scroll the screen to the left so as
12820 to reveal the next portion of the row. */
12821 || (row_r2l_p
12822 && ((cursor_row->enabled_p
12823 /* FIXME: It is confusing to set the
12824 truncated_on_right_p flag when R2L rows
12825 are actually truncated on the left. */
12826 && cursor_row->truncated_on_right_p
12827 && w->cursor.x <= h_margin)
12828 || (w->hscroll
12829 && (w->cursor.x >= text_area_width - h_margin))))))
12831 struct it it;
12832 ptrdiff_t hscroll;
12833 struct buffer *saved_current_buffer;
12834 ptrdiff_t pt;
12835 int wanted_x;
12837 /* Find point in a display of infinite width. */
12838 saved_current_buffer = current_buffer;
12839 current_buffer = XBUFFER (w->contents);
12841 if (w == XWINDOW (selected_window))
12842 pt = PT;
12843 else
12844 pt = clip_to_bounds (BEGV, marker_position (w->pointm), ZV);
12846 /* Move iterator to pt starting at cursor_row->start in
12847 a line with infinite width. */
12848 init_to_row_start (&it, w, cursor_row);
12849 it.last_visible_x = INFINITY;
12850 move_it_in_display_line_to (&it, pt, -1, MOVE_TO_POS);
12851 current_buffer = saved_current_buffer;
12853 /* Position cursor in window. */
12854 if (!hscroll_relative_p && hscroll_step_abs == 0)
12855 hscroll = max (0, (it.current_x
12856 - (ITERATOR_AT_END_OF_LINE_P (&it)
12857 ? (text_area_width - 4 * FRAME_COLUMN_WIDTH (it.f))
12858 : (text_area_width / 2))))
12859 / FRAME_COLUMN_WIDTH (it.f);
12860 else if ((!row_r2l_p
12861 && w->cursor.x >= text_area_width - h_margin)
12862 || (row_r2l_p && w->cursor.x <= h_margin))
12864 if (hscroll_relative_p)
12865 wanted_x = text_area_width * (1 - hscroll_step_rel)
12866 - h_margin;
12867 else
12868 wanted_x = text_area_width
12869 - hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
12870 - h_margin;
12871 hscroll
12872 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
12874 else
12876 if (hscroll_relative_p)
12877 wanted_x = text_area_width * hscroll_step_rel
12878 + h_margin;
12879 else
12880 wanted_x = hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
12881 + h_margin;
12882 hscroll
12883 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
12885 hscroll = max (hscroll, w->min_hscroll);
12887 /* Don't prevent redisplay optimizations if hscroll
12888 hasn't changed, as it will unnecessarily slow down
12889 redisplay. */
12890 if (w->hscroll != hscroll)
12892 XBUFFER (w->contents)->prevent_redisplay_optimizations_p = 1;
12893 w->hscroll = hscroll;
12894 hscrolled_p = 1;
12899 window = w->next;
12902 /* Value is non-zero if hscroll of any leaf window has been changed. */
12903 return hscrolled_p;
12907 /* Set hscroll so that cursor is visible and not inside horizontal
12908 scroll margins for all windows in the tree rooted at WINDOW. See
12909 also hscroll_window_tree above. Value is non-zero if any window's
12910 hscroll has been changed. If it has, desired matrices on the frame
12911 of WINDOW are cleared. */
12913 static int
12914 hscroll_windows (Lisp_Object window)
12916 int hscrolled_p = hscroll_window_tree (window);
12917 if (hscrolled_p)
12918 clear_desired_matrices (XFRAME (WINDOW_FRAME (XWINDOW (window))));
12919 return hscrolled_p;
12924 /************************************************************************
12925 Redisplay
12926 ************************************************************************/
12928 /* Variables holding some state of redisplay if GLYPH_DEBUG is defined
12929 to a non-zero value. This is sometimes handy to have in a debugger
12930 session. */
12932 #ifdef GLYPH_DEBUG
12934 /* First and last unchanged row for try_window_id. */
12936 static int debug_first_unchanged_at_end_vpos;
12937 static int debug_last_unchanged_at_beg_vpos;
12939 /* Delta vpos and y. */
12941 static int debug_dvpos, debug_dy;
12943 /* Delta in characters and bytes for try_window_id. */
12945 static ptrdiff_t debug_delta, debug_delta_bytes;
12947 /* Values of window_end_pos and window_end_vpos at the end of
12948 try_window_id. */
12950 static ptrdiff_t debug_end_vpos;
12952 /* Append a string to W->desired_matrix->method. FMT is a printf
12953 format string. If trace_redisplay_p is true also printf the
12954 resulting string to stderr. */
12956 static void debug_method_add (struct window *, char const *, ...)
12957 ATTRIBUTE_FORMAT_PRINTF (2, 3);
12959 static void
12960 debug_method_add (struct window *w, char const *fmt, ...)
12962 void *ptr = w;
12963 char *method = w->desired_matrix->method;
12964 int len = strlen (method);
12965 int size = sizeof w->desired_matrix->method;
12966 int remaining = size - len - 1;
12967 va_list ap;
12969 if (len && remaining)
12971 method[len] = '|';
12972 --remaining, ++len;
12975 va_start (ap, fmt);
12976 vsnprintf (method + len, remaining + 1, fmt, ap);
12977 va_end (ap);
12979 if (trace_redisplay_p)
12980 fprintf (stderr, "%p (%s): %s\n",
12981 ptr,
12982 ((BUFFERP (w->contents)
12983 && STRINGP (BVAR (XBUFFER (w->contents), name)))
12984 ? SSDATA (BVAR (XBUFFER (w->contents), name))
12985 : "no buffer"),
12986 method + len);
12989 #endif /* GLYPH_DEBUG */
12992 /* Value is non-zero if all changes in window W, which displays
12993 current_buffer, are in the text between START and END. START is a
12994 buffer position, END is given as a distance from Z. Used in
12995 redisplay_internal for display optimization. */
12997 static int
12998 text_outside_line_unchanged_p (struct window *w,
12999 ptrdiff_t start, ptrdiff_t end)
13001 int unchanged_p = 1;
13003 /* If text or overlays have changed, see where. */
13004 if (window_outdated (w))
13006 /* Gap in the line? */
13007 if (GPT < start || Z - GPT < end)
13008 unchanged_p = 0;
13010 /* Changes start in front of the line, or end after it? */
13011 if (unchanged_p
13012 && (BEG_UNCHANGED < start - 1
13013 || END_UNCHANGED < end))
13014 unchanged_p = 0;
13016 /* If selective display, can't optimize if changes start at the
13017 beginning of the line. */
13018 if (unchanged_p
13019 && INTEGERP (BVAR (current_buffer, selective_display))
13020 && XINT (BVAR (current_buffer, selective_display)) > 0
13021 && (BEG_UNCHANGED < start || GPT <= start))
13022 unchanged_p = 0;
13024 /* If there are overlays at the start or end of the line, these
13025 may have overlay strings with newlines in them. A change at
13026 START, for instance, may actually concern the display of such
13027 overlay strings as well, and they are displayed on different
13028 lines. So, quickly rule out this case. (For the future, it
13029 might be desirable to implement something more telling than
13030 just BEG/END_UNCHANGED.) */
13031 if (unchanged_p)
13033 if (BEG + BEG_UNCHANGED == start
13034 && overlay_touches_p (start))
13035 unchanged_p = 0;
13036 if (END_UNCHANGED == end
13037 && overlay_touches_p (Z - end))
13038 unchanged_p = 0;
13041 /* Under bidi reordering, adding or deleting a character in the
13042 beginning of a paragraph, before the first strong directional
13043 character, can change the base direction of the paragraph (unless
13044 the buffer specifies a fixed paragraph direction), which will
13045 require to redisplay the whole paragraph. It might be worthwhile
13046 to find the paragraph limits and widen the range of redisplayed
13047 lines to that, but for now just give up this optimization. */
13048 if (!NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering))
13049 && NILP (BVAR (XBUFFER (w->contents), bidi_paragraph_direction)))
13050 unchanged_p = 0;
13053 return unchanged_p;
13057 /* Do a frame update, taking possible shortcuts into account. This is
13058 the main external entry point for redisplay.
13060 If the last redisplay displayed an echo area message and that message
13061 is no longer requested, we clear the echo area or bring back the
13062 mini-buffer if that is in use. */
13064 void
13065 redisplay (void)
13067 redisplay_internal ();
13071 static Lisp_Object
13072 overlay_arrow_string_or_property (Lisp_Object var)
13074 Lisp_Object val;
13076 if (val = Fget (var, Qoverlay_arrow_string), STRINGP (val))
13077 return val;
13079 return Voverlay_arrow_string;
13082 /* Return 1 if there are any overlay-arrows in current_buffer. */
13083 static int
13084 overlay_arrow_in_current_buffer_p (void)
13086 Lisp_Object vlist;
13088 for (vlist = Voverlay_arrow_variable_list;
13089 CONSP (vlist);
13090 vlist = XCDR (vlist))
13092 Lisp_Object var = XCAR (vlist);
13093 Lisp_Object val;
13095 if (!SYMBOLP (var))
13096 continue;
13097 val = find_symbol_value (var);
13098 if (MARKERP (val)
13099 && current_buffer == XMARKER (val)->buffer)
13100 return 1;
13102 return 0;
13106 /* Return 1 if any overlay_arrows have moved or overlay-arrow-string
13107 has changed. */
13109 static int
13110 overlay_arrows_changed_p (void)
13112 Lisp_Object vlist;
13114 for (vlist = Voverlay_arrow_variable_list;
13115 CONSP (vlist);
13116 vlist = XCDR (vlist))
13118 Lisp_Object var = XCAR (vlist);
13119 Lisp_Object val, pstr;
13121 if (!SYMBOLP (var))
13122 continue;
13123 val = find_symbol_value (var);
13124 if (!MARKERP (val))
13125 continue;
13126 if (! EQ (COERCE_MARKER (val),
13127 Fget (var, Qlast_arrow_position))
13128 || ! (pstr = overlay_arrow_string_or_property (var),
13129 EQ (pstr, Fget (var, Qlast_arrow_string))))
13130 return 1;
13132 return 0;
13135 /* Mark overlay arrows to be updated on next redisplay. */
13137 static void
13138 update_overlay_arrows (int up_to_date)
13140 Lisp_Object vlist;
13142 for (vlist = Voverlay_arrow_variable_list;
13143 CONSP (vlist);
13144 vlist = XCDR (vlist))
13146 Lisp_Object var = XCAR (vlist);
13148 if (!SYMBOLP (var))
13149 continue;
13151 if (up_to_date > 0)
13153 Lisp_Object val = find_symbol_value (var);
13154 Fput (var, Qlast_arrow_position,
13155 COERCE_MARKER (val));
13156 Fput (var, Qlast_arrow_string,
13157 overlay_arrow_string_or_property (var));
13159 else if (up_to_date < 0
13160 || !NILP (Fget (var, Qlast_arrow_position)))
13162 Fput (var, Qlast_arrow_position, Qt);
13163 Fput (var, Qlast_arrow_string, Qt);
13169 /* Return overlay arrow string to display at row.
13170 Return integer (bitmap number) for arrow bitmap in left fringe.
13171 Return nil if no overlay arrow. */
13173 static Lisp_Object
13174 overlay_arrow_at_row (struct it *it, struct glyph_row *row)
13176 Lisp_Object vlist;
13178 for (vlist = Voverlay_arrow_variable_list;
13179 CONSP (vlist);
13180 vlist = XCDR (vlist))
13182 Lisp_Object var = XCAR (vlist);
13183 Lisp_Object val;
13185 if (!SYMBOLP (var))
13186 continue;
13188 val = find_symbol_value (var);
13190 if (MARKERP (val)
13191 && current_buffer == XMARKER (val)->buffer
13192 && (MATRIX_ROW_START_CHARPOS (row) == marker_position (val)))
13194 if (FRAME_WINDOW_P (it->f)
13195 /* FIXME: if ROW->reversed_p is set, this should test
13196 the right fringe, not the left one. */
13197 && WINDOW_LEFT_FRINGE_WIDTH (it->w) > 0)
13199 #ifdef HAVE_WINDOW_SYSTEM
13200 if (val = Fget (var, Qoverlay_arrow_bitmap), SYMBOLP (val))
13202 int fringe_bitmap;
13203 if ((fringe_bitmap = lookup_fringe_bitmap (val)) != 0)
13204 return make_number (fringe_bitmap);
13206 #endif
13207 return make_number (-1); /* Use default arrow bitmap. */
13209 return overlay_arrow_string_or_property (var);
13213 return Qnil;
13216 /* Return 1 if point moved out of or into a composition. Otherwise
13217 return 0. PREV_BUF and PREV_PT are the last point buffer and
13218 position. BUF and PT are the current point buffer and position. */
13220 static int
13221 check_point_in_composition (struct buffer *prev_buf, ptrdiff_t prev_pt,
13222 struct buffer *buf, ptrdiff_t pt)
13224 ptrdiff_t start, end;
13225 Lisp_Object prop;
13226 Lisp_Object buffer;
13228 XSETBUFFER (buffer, buf);
13229 /* Check a composition at the last point if point moved within the
13230 same buffer. */
13231 if (prev_buf == buf)
13233 if (prev_pt == pt)
13234 /* Point didn't move. */
13235 return 0;
13237 if (prev_pt > BUF_BEGV (buf) && prev_pt < BUF_ZV (buf)
13238 && find_composition (prev_pt, -1, &start, &end, &prop, buffer)
13239 && composition_valid_p (start, end, prop)
13240 && start < prev_pt && end > prev_pt)
13241 /* The last point was within the composition. Return 1 iff
13242 point moved out of the composition. */
13243 return (pt <= start || pt >= end);
13246 /* Check a composition at the current point. */
13247 return (pt > BUF_BEGV (buf) && pt < BUF_ZV (buf)
13248 && find_composition (pt, -1, &start, &end, &prop, buffer)
13249 && composition_valid_p (start, end, prop)
13250 && start < pt && end > pt);
13253 /* Reconsider the clip changes of buffer which is displayed in W. */
13255 static void
13256 reconsider_clip_changes (struct window *w)
13258 struct buffer *b = XBUFFER (w->contents);
13260 if (b->clip_changed
13261 && w->window_end_valid
13262 && w->current_matrix->buffer == b
13263 && w->current_matrix->zv == BUF_ZV (b)
13264 && w->current_matrix->begv == BUF_BEGV (b))
13265 b->clip_changed = 0;
13267 /* If display wasn't paused, and W is not a tool bar window, see if
13268 point has been moved into or out of a composition. In that case,
13269 we set b->clip_changed to 1 to force updating the screen. If
13270 b->clip_changed has already been set to 1, we can skip this
13271 check. */
13272 if (!b->clip_changed && w->window_end_valid)
13274 ptrdiff_t pt = (w == XWINDOW (selected_window)
13275 ? PT : marker_position (w->pointm));
13277 if ((w->current_matrix->buffer != b || pt != w->last_point)
13278 && check_point_in_composition (w->current_matrix->buffer,
13279 w->last_point, b, pt))
13280 b->clip_changed = 1;
13284 static void
13285 propagate_buffer_redisplay (void)
13286 { /* Resetting b->text->redisplay is problematic!
13287 We can't just reset it in the case that some window that displays
13288 it has not been redisplayed; and such a window can stay
13289 unredisplayed for a long time if it's currently invisible.
13290 But we do want to reset it at the end of redisplay otherwise
13291 its displayed windows will keep being redisplayed over and over
13292 again.
13293 So we copy all b->text->redisplay flags up to their windows here,
13294 such that mark_window_display_accurate can safely reset
13295 b->text->redisplay. */
13296 Lisp_Object ws = window_list ();
13297 for (; CONSP (ws); ws = XCDR (ws))
13299 struct window *thisw = XWINDOW (XCAR (ws));
13300 struct buffer *thisb = XBUFFER (thisw->contents);
13301 if (thisb->text->redisplay)
13302 thisw->redisplay = true;
13306 #define STOP_POLLING \
13307 do { if (! polling_stopped_here) stop_polling (); \
13308 polling_stopped_here = 1; } while (0)
13310 #define RESUME_POLLING \
13311 do { if (polling_stopped_here) start_polling (); \
13312 polling_stopped_here = 0; } while (0)
13315 /* Perhaps in the future avoid recentering windows if it
13316 is not necessary; currently that causes some problems. */
13318 static void
13319 redisplay_internal (void)
13321 struct window *w = XWINDOW (selected_window);
13322 struct window *sw;
13323 struct frame *fr;
13324 bool pending;
13325 bool must_finish = 0, match_p;
13326 struct text_pos tlbufpos, tlendpos;
13327 int number_of_visible_frames;
13328 ptrdiff_t count;
13329 struct frame *sf;
13330 int polling_stopped_here = 0;
13331 Lisp_Object tail, frame;
13333 /* True means redisplay has to consider all windows on all
13334 frames. False, only selected_window is considered. */
13335 bool consider_all_windows_p;
13337 /* True means redisplay has to redisplay the miniwindow. */
13338 bool update_miniwindow_p = false;
13340 TRACE ((stderr, "redisplay_internal %d\n", redisplaying_p));
13342 /* No redisplay if running in batch mode or frame is not yet fully
13343 initialized, or redisplay is explicitly turned off by setting
13344 Vinhibit_redisplay. */
13345 if (FRAME_INITIAL_P (SELECTED_FRAME ())
13346 || !NILP (Vinhibit_redisplay))
13347 return;
13349 /* Don't examine these until after testing Vinhibit_redisplay.
13350 When Emacs is shutting down, perhaps because its connection to
13351 X has dropped, we should not look at them at all. */
13352 fr = XFRAME (w->frame);
13353 sf = SELECTED_FRAME ();
13355 if (!fr->glyphs_initialized_p)
13356 return;
13358 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS)
13359 if (popup_activated ())
13360 return;
13361 #endif
13363 /* I don't think this happens but let's be paranoid. */
13364 if (redisplaying_p)
13365 return;
13367 /* Record a function that clears redisplaying_p
13368 when we leave this function. */
13369 count = SPECPDL_INDEX ();
13370 record_unwind_protect_void (unwind_redisplay);
13371 redisplaying_p = 1;
13372 specbind (Qinhibit_free_realized_faces, Qnil);
13374 /* Record this function, so it appears on the profiler's backtraces. */
13375 record_in_backtrace (Qredisplay_internal, 0, 0);
13377 FOR_EACH_FRAME (tail, frame)
13378 XFRAME (frame)->already_hscrolled_p = 0;
13380 retry:
13381 /* Remember the currently selected window. */
13382 sw = w;
13384 pending = false;
13385 last_escape_glyph_frame = NULL;
13386 last_escape_glyph_face_id = (1 << FACE_ID_BITS);
13387 last_glyphless_glyph_frame = NULL;
13388 last_glyphless_glyph_face_id = (1 << FACE_ID_BITS);
13390 /* If face_change, init_iterator will free all realized faces, which
13391 includes the faces referenced from current matrices. So, we
13392 can't reuse current matrices in this case. */
13393 if (face_change)
13394 windows_or_buffers_changed = 47;
13396 if ((FRAME_TERMCAP_P (sf) || FRAME_MSDOS_P (sf))
13397 && FRAME_TTY (sf)->previous_frame != sf)
13399 /* Since frames on a single ASCII terminal share the same
13400 display area, displaying a different frame means redisplay
13401 the whole thing. */
13402 SET_FRAME_GARBAGED (sf);
13403 #ifndef DOS_NT
13404 set_tty_color_mode (FRAME_TTY (sf), sf);
13405 #endif
13406 FRAME_TTY (sf)->previous_frame = sf;
13409 /* Set the visible flags for all frames. Do this before checking for
13410 resized or garbaged frames; they want to know if their frames are
13411 visible. See the comment in frame.h for FRAME_SAMPLE_VISIBILITY. */
13412 number_of_visible_frames = 0;
13414 FOR_EACH_FRAME (tail, frame)
13416 struct frame *f = XFRAME (frame);
13418 if (FRAME_VISIBLE_P (f))
13420 ++number_of_visible_frames;
13421 /* Adjust matrices for visible frames only. */
13422 if (f->fonts_changed)
13424 adjust_frame_glyphs (f);
13425 f->fonts_changed = 0;
13427 /* If cursor type has been changed on the frame
13428 other than selected, consider all frames. */
13429 if (f != sf && f->cursor_type_changed)
13430 update_mode_lines = 31;
13432 clear_desired_matrices (f);
13435 /* Notice any pending interrupt request to change frame size. */
13436 do_pending_window_change (true);
13438 /* do_pending_window_change could change the selected_window due to
13439 frame resizing which makes the selected window too small. */
13440 if (WINDOWP (selected_window) && (w = XWINDOW (selected_window)) != sw)
13441 sw = w;
13443 /* Clear frames marked as garbaged. */
13444 clear_garbaged_frames ();
13446 /* Build menubar and tool-bar items. */
13447 if (NILP (Vmemory_full))
13448 prepare_menu_bars ();
13450 reconsider_clip_changes (w);
13452 /* In most cases selected window displays current buffer. */
13453 match_p = XBUFFER (w->contents) == current_buffer;
13454 if (match_p)
13456 /* Detect case that we need to write or remove a star in the mode line. */
13457 if ((SAVE_MODIFF < MODIFF) != w->last_had_star)
13458 w->update_mode_line = 1;
13460 if (mode_line_update_needed (w))
13461 w->update_mode_line = 1;
13463 /* If reconsider_clip_changes above decided that the narrowing
13464 in the current buffer changed, make sure all other windows
13465 showing that buffer will be redisplayed. */
13466 if (current_buffer->clip_changed)
13467 bset_update_mode_line (current_buffer);
13470 /* Normally the message* functions will have already displayed and
13471 updated the echo area, but the frame may have been trashed, or
13472 the update may have been preempted, so display the echo area
13473 again here. Checking message_cleared_p captures the case that
13474 the echo area should be cleared. */
13475 if ((!NILP (echo_area_buffer[0]) && !display_last_displayed_message_p)
13476 || (!NILP (echo_area_buffer[1]) && display_last_displayed_message_p)
13477 || (message_cleared_p
13478 && minibuf_level == 0
13479 /* If the mini-window is currently selected, this means the
13480 echo-area doesn't show through. */
13481 && !MINI_WINDOW_P (XWINDOW (selected_window))))
13483 int window_height_changed_p = echo_area_display (false);
13485 if (message_cleared_p)
13486 update_miniwindow_p = true;
13488 must_finish = 1;
13490 /* If we don't display the current message, don't clear the
13491 message_cleared_p flag, because, if we did, we wouldn't clear
13492 the echo area in the next redisplay which doesn't preserve
13493 the echo area. */
13494 if (!display_last_displayed_message_p)
13495 message_cleared_p = 0;
13497 if (window_height_changed_p)
13499 windows_or_buffers_changed = 50;
13501 /* If window configuration was changed, frames may have been
13502 marked garbaged. Clear them or we will experience
13503 surprises wrt scrolling. */
13504 clear_garbaged_frames ();
13507 else if (EQ (selected_window, minibuf_window)
13508 && (current_buffer->clip_changed || window_outdated (w))
13509 && resize_mini_window (w, 0))
13511 /* Resized active mini-window to fit the size of what it is
13512 showing if its contents might have changed. */
13513 must_finish = 1;
13515 /* If window configuration was changed, frames may have been
13516 marked garbaged. Clear them or we will experience
13517 surprises wrt scrolling. */
13518 clear_garbaged_frames ();
13521 if (windows_or_buffers_changed && !update_mode_lines)
13522 /* Code that sets windows_or_buffers_changed doesn't distinguish whether
13523 only the windows's contents needs to be refreshed, or whether the
13524 mode-lines also need a refresh. */
13525 update_mode_lines = (windows_or_buffers_changed == REDISPLAY_SOME
13526 ? REDISPLAY_SOME : 32);
13528 /* If specs for an arrow have changed, do thorough redisplay
13529 to ensure we remove any arrow that should no longer exist. */
13530 if (overlay_arrows_changed_p ())
13531 /* Apparently, this is the only case where we update other windows,
13532 without updating other mode-lines. */
13533 windows_or_buffers_changed = 49;
13535 consider_all_windows_p = (update_mode_lines
13536 || windows_or_buffers_changed);
13538 #define AINC(a,i) \
13539 if (VECTORP (a) && i >= 0 && i < ASIZE (a) && INTEGERP (AREF (a, i))) \
13540 ASET (a, i, make_number (1 + XINT (AREF (a, i))))
13542 AINC (Vredisplay__all_windows_cause, windows_or_buffers_changed);
13543 AINC (Vredisplay__mode_lines_cause, update_mode_lines);
13545 /* Optimize the case that only the line containing the cursor in the
13546 selected window has changed. Variables starting with this_ are
13547 set in display_line and record information about the line
13548 containing the cursor. */
13549 tlbufpos = this_line_start_pos;
13550 tlendpos = this_line_end_pos;
13551 if (!consider_all_windows_p
13552 && CHARPOS (tlbufpos) > 0
13553 && !w->update_mode_line
13554 && !current_buffer->clip_changed
13555 && !current_buffer->prevent_redisplay_optimizations_p
13556 && FRAME_VISIBLE_P (XFRAME (w->frame))
13557 && !FRAME_OBSCURED_P (XFRAME (w->frame))
13558 && !XFRAME (w->frame)->cursor_type_changed
13559 /* Make sure recorded data applies to current buffer, etc. */
13560 && this_line_buffer == current_buffer
13561 && match_p
13562 && !w->force_start
13563 && !w->optional_new_start
13564 /* Point must be on the line that we have info recorded about. */
13565 && PT >= CHARPOS (tlbufpos)
13566 && PT <= Z - CHARPOS (tlendpos)
13567 /* All text outside that line, including its final newline,
13568 must be unchanged. */
13569 && text_outside_line_unchanged_p (w, CHARPOS (tlbufpos),
13570 CHARPOS (tlendpos)))
13572 if (CHARPOS (tlbufpos) > BEGV
13573 && FETCH_BYTE (BYTEPOS (tlbufpos) - 1) != '\n'
13574 && (CHARPOS (tlbufpos) == ZV
13575 || FETCH_BYTE (BYTEPOS (tlbufpos)) == '\n'))
13576 /* Former continuation line has disappeared by becoming empty. */
13577 goto cancel;
13578 else if (window_outdated (w) || MINI_WINDOW_P (w))
13580 /* We have to handle the case of continuation around a
13581 wide-column character (see the comment in indent.c around
13582 line 1340).
13584 For instance, in the following case:
13586 -------- Insert --------
13587 K_A_N_\\ `a' K_A_N_a\ `X_' are wide-column chars.
13588 J_I_ ==> J_I_ `^^' are cursors.
13589 ^^ ^^
13590 -------- --------
13592 As we have to redraw the line above, we cannot use this
13593 optimization. */
13595 struct it it;
13596 int line_height_before = this_line_pixel_height;
13598 /* Note that start_display will handle the case that the
13599 line starting at tlbufpos is a continuation line. */
13600 start_display (&it, w, tlbufpos);
13602 /* Implementation note: It this still necessary? */
13603 if (it.current_x != this_line_start_x)
13604 goto cancel;
13606 TRACE ((stderr, "trying display optimization 1\n"));
13607 w->cursor.vpos = -1;
13608 overlay_arrow_seen = 0;
13609 it.vpos = this_line_vpos;
13610 it.current_y = this_line_y;
13611 it.glyph_row = MATRIX_ROW (w->desired_matrix, this_line_vpos);
13612 display_line (&it);
13614 /* If line contains point, is not continued,
13615 and ends at same distance from eob as before, we win. */
13616 if (w->cursor.vpos >= 0
13617 /* Line is not continued, otherwise this_line_start_pos
13618 would have been set to 0 in display_line. */
13619 && CHARPOS (this_line_start_pos)
13620 /* Line ends as before. */
13621 && CHARPOS (this_line_end_pos) == CHARPOS (tlendpos)
13622 /* Line has same height as before. Otherwise other lines
13623 would have to be shifted up or down. */
13624 && this_line_pixel_height == line_height_before)
13626 /* If this is not the window's last line, we must adjust
13627 the charstarts of the lines below. */
13628 if (it.current_y < it.last_visible_y)
13630 struct glyph_row *row
13631 = MATRIX_ROW (w->current_matrix, this_line_vpos + 1);
13632 ptrdiff_t delta, delta_bytes;
13634 /* We used to distinguish between two cases here,
13635 conditioned by Z - CHARPOS (tlendpos) == ZV, for
13636 when the line ends in a newline or the end of the
13637 buffer's accessible portion. But both cases did
13638 the same, so they were collapsed. */
13639 delta = (Z
13640 - CHARPOS (tlendpos)
13641 - MATRIX_ROW_START_CHARPOS (row));
13642 delta_bytes = (Z_BYTE
13643 - BYTEPOS (tlendpos)
13644 - MATRIX_ROW_START_BYTEPOS (row));
13646 increment_matrix_positions (w->current_matrix,
13647 this_line_vpos + 1,
13648 w->current_matrix->nrows,
13649 delta, delta_bytes);
13652 /* If this row displays text now but previously didn't,
13653 or vice versa, w->window_end_vpos may have to be
13654 adjusted. */
13655 if (MATRIX_ROW_DISPLAYS_TEXT_P (it.glyph_row - 1))
13657 if (w->window_end_vpos < this_line_vpos)
13658 w->window_end_vpos = this_line_vpos;
13660 else if (w->window_end_vpos == this_line_vpos
13661 && this_line_vpos > 0)
13662 w->window_end_vpos = this_line_vpos - 1;
13663 w->window_end_valid = 0;
13665 /* Update hint: No need to try to scroll in update_window. */
13666 w->desired_matrix->no_scrolling_p = 1;
13668 #ifdef GLYPH_DEBUG
13669 *w->desired_matrix->method = 0;
13670 debug_method_add (w, "optimization 1");
13671 #endif
13672 #ifdef HAVE_WINDOW_SYSTEM
13673 update_window_fringes (w, 0);
13674 #endif
13675 goto update;
13677 else
13678 goto cancel;
13680 else if (/* Cursor position hasn't changed. */
13681 PT == w->last_point
13682 /* Make sure the cursor was last displayed
13683 in this window. Otherwise we have to reposition it. */
13685 /* PXW: Must be converted to pixels, probably. */
13686 && 0 <= w->cursor.vpos
13687 && w->cursor.vpos < WINDOW_TOTAL_LINES (w))
13689 if (!must_finish)
13691 do_pending_window_change (true);
13692 /* If selected_window changed, redisplay again. */
13693 if (WINDOWP (selected_window)
13694 && (w = XWINDOW (selected_window)) != sw)
13695 goto retry;
13697 /* We used to always goto end_of_redisplay here, but this
13698 isn't enough if we have a blinking cursor. */
13699 if (w->cursor_off_p == w->last_cursor_off_p)
13700 goto end_of_redisplay;
13702 goto update;
13704 /* If highlighting the region, or if the cursor is in the echo area,
13705 then we can't just move the cursor. */
13706 else if (NILP (Vshow_trailing_whitespace)
13707 && !cursor_in_echo_area)
13709 struct it it;
13710 struct glyph_row *row;
13712 /* Skip from tlbufpos to PT and see where it is. Note that
13713 PT may be in invisible text. If so, we will end at the
13714 next visible position. */
13715 init_iterator (&it, w, CHARPOS (tlbufpos), BYTEPOS (tlbufpos),
13716 NULL, DEFAULT_FACE_ID);
13717 it.current_x = this_line_start_x;
13718 it.current_y = this_line_y;
13719 it.vpos = this_line_vpos;
13721 /* The call to move_it_to stops in front of PT, but
13722 moves over before-strings. */
13723 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
13725 if (it.vpos == this_line_vpos
13726 && (row = MATRIX_ROW (w->current_matrix, this_line_vpos),
13727 row->enabled_p))
13729 eassert (this_line_vpos == it.vpos);
13730 eassert (this_line_y == it.current_y);
13731 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
13732 #ifdef GLYPH_DEBUG
13733 *w->desired_matrix->method = 0;
13734 debug_method_add (w, "optimization 3");
13735 #endif
13736 goto update;
13738 else
13739 goto cancel;
13742 cancel:
13743 /* Text changed drastically or point moved off of line. */
13744 SET_MATRIX_ROW_ENABLED_P (w->desired_matrix, this_line_vpos, false);
13747 CHARPOS (this_line_start_pos) = 0;
13748 ++clear_face_cache_count;
13749 #ifdef HAVE_WINDOW_SYSTEM
13750 ++clear_image_cache_count;
13751 #endif
13753 /* Build desired matrices, and update the display. If
13754 consider_all_windows_p is non-zero, do it for all windows on all
13755 frames. Otherwise do it for selected_window, only. */
13757 if (consider_all_windows_p)
13759 FOR_EACH_FRAME (tail, frame)
13760 XFRAME (frame)->updated_p = 0;
13762 propagate_buffer_redisplay ();
13764 FOR_EACH_FRAME (tail, frame)
13766 struct frame *f = XFRAME (frame);
13768 /* We don't have to do anything for unselected terminal
13769 frames. */
13770 if ((FRAME_TERMCAP_P (f) || FRAME_MSDOS_P (f))
13771 && !EQ (FRAME_TTY (f)->top_frame, frame))
13772 continue;
13774 retry_frame:
13776 #if defined (HAVE_WINDOW_SYSTEM) && !defined (USE_GTK) && !defined (HAVE_NS)
13777 /* Redisplay internal tool bar if this is the first time so we
13778 can adjust the frame height right now, if necessary. */
13779 if (!f->tool_bar_redisplayed_once)
13781 if (redisplay_tool_bar (f))
13782 adjust_frame_glyphs (f);
13783 f->tool_bar_redisplayed_once = true;
13785 #endif
13787 if (FRAME_WINDOW_P (f) || FRAME_TERMCAP_P (f) || f == sf)
13789 bool gcscrollbars
13790 /* Only GC scrollbars when we redisplay the whole frame. */
13791 = f->redisplay || !REDISPLAY_SOME_P ();
13792 /* Mark all the scroll bars to be removed; we'll redeem
13793 the ones we want when we redisplay their windows. */
13794 if (gcscrollbars && FRAME_TERMINAL (f)->condemn_scroll_bars_hook)
13795 FRAME_TERMINAL (f)->condemn_scroll_bars_hook (f);
13797 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
13798 redisplay_windows (FRAME_ROOT_WINDOW (f));
13799 /* Remember that the invisible frames need to be redisplayed next
13800 time they're visible. */
13801 else if (!REDISPLAY_SOME_P ())
13802 f->redisplay = true;
13804 /* The X error handler may have deleted that frame. */
13805 if (!FRAME_LIVE_P (f))
13806 continue;
13808 /* Any scroll bars which redisplay_windows should have
13809 nuked should now go away. */
13810 if (gcscrollbars && FRAME_TERMINAL (f)->judge_scroll_bars_hook)
13811 FRAME_TERMINAL (f)->judge_scroll_bars_hook (f);
13813 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
13815 /* If fonts changed on visible frame, display again. */
13816 if (f->fonts_changed)
13818 adjust_frame_glyphs (f);
13819 f->fonts_changed = false;
13820 goto retry_frame;
13823 /* See if we have to hscroll. */
13824 if (!f->already_hscrolled_p)
13826 f->already_hscrolled_p = true;
13827 if (hscroll_windows (f->root_window))
13828 goto retry_frame;
13831 /* Prevent various kinds of signals during display
13832 update. stdio is not robust about handling
13833 signals, which can cause an apparent I/O error. */
13834 if (interrupt_input)
13835 unrequest_sigio ();
13836 STOP_POLLING;
13838 pending |= update_frame (f, false, false);
13839 f->cursor_type_changed = false;
13840 f->updated_p = true;
13845 eassert (EQ (XFRAME (selected_frame)->selected_window, selected_window));
13847 if (!pending)
13849 /* Do the mark_window_display_accurate after all windows have
13850 been redisplayed because this call resets flags in buffers
13851 which are needed for proper redisplay. */
13852 FOR_EACH_FRAME (tail, frame)
13854 struct frame *f = XFRAME (frame);
13855 if (f->updated_p)
13857 f->redisplay = false;
13858 mark_window_display_accurate (f->root_window, 1);
13859 if (FRAME_TERMINAL (f)->frame_up_to_date_hook)
13860 FRAME_TERMINAL (f)->frame_up_to_date_hook (f);
13865 else if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
13867 Lisp_Object mini_window = FRAME_MINIBUF_WINDOW (sf);
13868 struct frame *mini_frame;
13870 displayed_buffer = XBUFFER (XWINDOW (selected_window)->contents);
13871 /* Use list_of_error, not Qerror, so that
13872 we catch only errors and don't run the debugger. */
13873 internal_condition_case_1 (redisplay_window_1, selected_window,
13874 list_of_error,
13875 redisplay_window_error);
13876 if (update_miniwindow_p)
13877 internal_condition_case_1 (redisplay_window_1, mini_window,
13878 list_of_error,
13879 redisplay_window_error);
13881 /* Compare desired and current matrices, perform output. */
13883 update:
13884 /* If fonts changed, display again. */
13885 if (sf->fonts_changed)
13886 goto retry;
13888 /* Prevent various kinds of signals during display update.
13889 stdio is not robust about handling signals,
13890 which can cause an apparent I/O error. */
13891 if (interrupt_input)
13892 unrequest_sigio ();
13893 STOP_POLLING;
13895 if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
13897 if (hscroll_windows (selected_window))
13898 goto retry;
13900 XWINDOW (selected_window)->must_be_updated_p = true;
13901 pending = update_frame (sf, false, false);
13902 sf->cursor_type_changed = false;
13905 /* We may have called echo_area_display at the top of this
13906 function. If the echo area is on another frame, that may
13907 have put text on a frame other than the selected one, so the
13908 above call to update_frame would not have caught it. Catch
13909 it here. */
13910 mini_window = FRAME_MINIBUF_WINDOW (sf);
13911 mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
13913 if (mini_frame != sf && FRAME_WINDOW_P (mini_frame))
13915 XWINDOW (mini_window)->must_be_updated_p = true;
13916 pending |= update_frame (mini_frame, false, false);
13917 mini_frame->cursor_type_changed = false;
13918 if (!pending && hscroll_windows (mini_window))
13919 goto retry;
13923 /* If display was paused because of pending input, make sure we do a
13924 thorough update the next time. */
13925 if (pending)
13927 /* Prevent the optimization at the beginning of
13928 redisplay_internal that tries a single-line update of the
13929 line containing the cursor in the selected window. */
13930 CHARPOS (this_line_start_pos) = 0;
13932 /* Let the overlay arrow be updated the next time. */
13933 update_overlay_arrows (0);
13935 /* If we pause after scrolling, some rows in the current
13936 matrices of some windows are not valid. */
13937 if (!WINDOW_FULL_WIDTH_P (w)
13938 && !FRAME_WINDOW_P (XFRAME (w->frame)))
13939 update_mode_lines = 36;
13941 else
13943 if (!consider_all_windows_p)
13945 /* This has already been done above if
13946 consider_all_windows_p is set. */
13947 if (XBUFFER (w->contents)->text->redisplay
13948 && buffer_window_count (XBUFFER (w->contents)) > 1)
13949 /* This can happen if b->text->redisplay was set during
13950 jit-lock. */
13951 propagate_buffer_redisplay ();
13952 mark_window_display_accurate_1 (w, 1);
13954 /* Say overlay arrows are up to date. */
13955 update_overlay_arrows (1);
13957 if (FRAME_TERMINAL (sf)->frame_up_to_date_hook != 0)
13958 FRAME_TERMINAL (sf)->frame_up_to_date_hook (sf);
13961 update_mode_lines = 0;
13962 windows_or_buffers_changed = 0;
13965 /* Start SIGIO interrupts coming again. Having them off during the
13966 code above makes it less likely one will discard output, but not
13967 impossible, since there might be stuff in the system buffer here.
13968 But it is much hairier to try to do anything about that. */
13969 if (interrupt_input)
13970 request_sigio ();
13971 RESUME_POLLING;
13973 /* If a frame has become visible which was not before, redisplay
13974 again, so that we display it. Expose events for such a frame
13975 (which it gets when becoming visible) don't call the parts of
13976 redisplay constructing glyphs, so simply exposing a frame won't
13977 display anything in this case. So, we have to display these
13978 frames here explicitly. */
13979 if (!pending)
13981 int new_count = 0;
13983 FOR_EACH_FRAME (tail, frame)
13985 if (XFRAME (frame)->visible)
13986 new_count++;
13989 if (new_count != number_of_visible_frames)
13990 windows_or_buffers_changed = 52;
13993 /* Change frame size now if a change is pending. */
13994 do_pending_window_change (true);
13996 /* If we just did a pending size change, or have additional
13997 visible frames, or selected_window changed, redisplay again. */
13998 if ((windows_or_buffers_changed && !pending)
13999 || (WINDOWP (selected_window) && (w = XWINDOW (selected_window)) != sw))
14000 goto retry;
14002 /* Clear the face and image caches.
14004 We used to do this only if consider_all_windows_p. But the cache
14005 needs to be cleared if a timer creates images in the current
14006 buffer (e.g. the test case in Bug#6230). */
14008 if (clear_face_cache_count > CLEAR_FACE_CACHE_COUNT)
14010 clear_face_cache (false);
14011 clear_face_cache_count = 0;
14014 #ifdef HAVE_WINDOW_SYSTEM
14015 if (clear_image_cache_count > CLEAR_IMAGE_CACHE_COUNT)
14017 clear_image_caches (Qnil);
14018 clear_image_cache_count = 0;
14020 #endif /* HAVE_WINDOW_SYSTEM */
14022 end_of_redisplay:
14023 #ifdef HAVE_NS
14024 ns_set_doc_edited ();
14025 #endif
14026 if (interrupt_input && interrupts_deferred)
14027 request_sigio ();
14029 unbind_to (count, Qnil);
14030 RESUME_POLLING;
14034 /* Redisplay, but leave alone any recent echo area message unless
14035 another message has been requested in its place.
14037 This is useful in situations where you need to redisplay but no
14038 user action has occurred, making it inappropriate for the message
14039 area to be cleared. See tracking_off and
14040 wait_reading_process_output for examples of these situations.
14042 FROM_WHERE is an integer saying from where this function was
14043 called. This is useful for debugging. */
14045 void
14046 redisplay_preserve_echo_area (int from_where)
14048 TRACE ((stderr, "redisplay_preserve_echo_area (%d)\n", from_where));
14050 if (!NILP (echo_area_buffer[1]))
14052 /* We have a previously displayed message, but no current
14053 message. Redisplay the previous message. */
14054 display_last_displayed_message_p = true;
14055 redisplay_internal ();
14056 display_last_displayed_message_p = false;
14058 else
14059 redisplay_internal ();
14061 flush_frame (SELECTED_FRAME ());
14065 /* Function registered with record_unwind_protect in redisplay_internal. */
14067 static void
14068 unwind_redisplay (void)
14070 redisplaying_p = 0;
14074 /* Mark the display of leaf window W as accurate or inaccurate.
14075 If ACCURATE_P is non-zero mark display of W as accurate. If
14076 ACCURATE_P is zero, arrange for W to be redisplayed the next
14077 time redisplay_internal is called. */
14079 static void
14080 mark_window_display_accurate_1 (struct window *w, int accurate_p)
14082 struct buffer *b = XBUFFER (w->contents);
14084 w->last_modified = accurate_p ? BUF_MODIFF (b) : 0;
14085 w->last_overlay_modified = accurate_p ? BUF_OVERLAY_MODIFF (b) : 0;
14086 w->last_had_star = BUF_MODIFF (b) > BUF_SAVE_MODIFF (b);
14088 if (accurate_p)
14090 b->clip_changed = false;
14091 b->prevent_redisplay_optimizations_p = false;
14092 eassert (buffer_window_count (b) > 0);
14093 /* Resetting b->text->redisplay is problematic!
14094 In order to make it safer to do it here, redisplay_internal must
14095 have copied all b->text->redisplay to their respective windows. */
14096 b->text->redisplay = false;
14098 BUF_UNCHANGED_MODIFIED (b) = BUF_MODIFF (b);
14099 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = BUF_OVERLAY_MODIFF (b);
14100 BUF_BEG_UNCHANGED (b) = BUF_GPT (b) - BUF_BEG (b);
14101 BUF_END_UNCHANGED (b) = BUF_Z (b) - BUF_GPT (b);
14103 w->current_matrix->buffer = b;
14104 w->current_matrix->begv = BUF_BEGV (b);
14105 w->current_matrix->zv = BUF_ZV (b);
14107 w->last_cursor_vpos = w->cursor.vpos;
14108 w->last_cursor_off_p = w->cursor_off_p;
14110 if (w == XWINDOW (selected_window))
14111 w->last_point = BUF_PT (b);
14112 else
14113 w->last_point = marker_position (w->pointm);
14115 w->window_end_valid = true;
14116 w->update_mode_line = false;
14119 w->redisplay = !accurate_p;
14123 /* Mark the display of windows in the window tree rooted at WINDOW as
14124 accurate or inaccurate. If ACCURATE_P is non-zero mark display of
14125 windows as accurate. If ACCURATE_P is zero, arrange for windows to
14126 be redisplayed the next time redisplay_internal is called. */
14128 void
14129 mark_window_display_accurate (Lisp_Object window, int accurate_p)
14131 struct window *w;
14133 for (; !NILP (window); window = w->next)
14135 w = XWINDOW (window);
14136 if (WINDOWP (w->contents))
14137 mark_window_display_accurate (w->contents, accurate_p);
14138 else
14139 mark_window_display_accurate_1 (w, accurate_p);
14142 if (accurate_p)
14143 update_overlay_arrows (1);
14144 else
14145 /* Force a thorough redisplay the next time by setting
14146 last_arrow_position and last_arrow_string to t, which is
14147 unequal to any useful value of Voverlay_arrow_... */
14148 update_overlay_arrows (-1);
14152 /* Return value in display table DP (Lisp_Char_Table *) for character
14153 C. Since a display table doesn't have any parent, we don't have to
14154 follow parent. Do not call this function directly but use the
14155 macro DISP_CHAR_VECTOR. */
14157 Lisp_Object
14158 disp_char_vector (struct Lisp_Char_Table *dp, int c)
14160 Lisp_Object val;
14162 if (ASCII_CHAR_P (c))
14164 val = dp->ascii;
14165 if (SUB_CHAR_TABLE_P (val))
14166 val = XSUB_CHAR_TABLE (val)->contents[c];
14168 else
14170 Lisp_Object table;
14172 XSETCHAR_TABLE (table, dp);
14173 val = char_table_ref (table, c);
14175 if (NILP (val))
14176 val = dp->defalt;
14177 return val;
14182 /***********************************************************************
14183 Window Redisplay
14184 ***********************************************************************/
14186 /* Redisplay all leaf windows in the window tree rooted at WINDOW. */
14188 static void
14189 redisplay_windows (Lisp_Object window)
14191 while (!NILP (window))
14193 struct window *w = XWINDOW (window);
14195 if (WINDOWP (w->contents))
14196 redisplay_windows (w->contents);
14197 else if (BUFFERP (w->contents))
14199 displayed_buffer = XBUFFER (w->contents);
14200 /* Use list_of_error, not Qerror, so that
14201 we catch only errors and don't run the debugger. */
14202 internal_condition_case_1 (redisplay_window_0, window,
14203 list_of_error,
14204 redisplay_window_error);
14207 window = w->next;
14211 static Lisp_Object
14212 redisplay_window_error (Lisp_Object ignore)
14214 displayed_buffer->display_error_modiff = BUF_MODIFF (displayed_buffer);
14215 return Qnil;
14218 static Lisp_Object
14219 redisplay_window_0 (Lisp_Object window)
14221 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
14222 redisplay_window (window, false);
14223 return Qnil;
14226 static Lisp_Object
14227 redisplay_window_1 (Lisp_Object window)
14229 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
14230 redisplay_window (window, true);
14231 return Qnil;
14235 /* Set cursor position of W. PT is assumed to be displayed in ROW.
14236 DELTA and DELTA_BYTES are the numbers of characters and bytes by
14237 which positions recorded in ROW differ from current buffer
14238 positions.
14240 Return 0 if cursor is not on this row, 1 otherwise. */
14242 static int
14243 set_cursor_from_row (struct window *w, struct glyph_row *row,
14244 struct glyph_matrix *matrix,
14245 ptrdiff_t delta, ptrdiff_t delta_bytes,
14246 int dy, int dvpos)
14248 struct glyph *glyph = row->glyphs[TEXT_AREA];
14249 struct glyph *end = glyph + row->used[TEXT_AREA];
14250 struct glyph *cursor = NULL;
14251 /* The last known character position in row. */
14252 ptrdiff_t last_pos = MATRIX_ROW_START_CHARPOS (row) + delta;
14253 int x = row->x;
14254 ptrdiff_t pt_old = PT - delta;
14255 ptrdiff_t pos_before = MATRIX_ROW_START_CHARPOS (row) + delta;
14256 ptrdiff_t pos_after = MATRIX_ROW_END_CHARPOS (row) + delta;
14257 struct glyph *glyph_before = glyph - 1, *glyph_after = end;
14258 /* A glyph beyond the edge of TEXT_AREA which we should never
14259 touch. */
14260 struct glyph *glyphs_end = end;
14261 /* Non-zero means we've found a match for cursor position, but that
14262 glyph has the avoid_cursor_p flag set. */
14263 int match_with_avoid_cursor = 0;
14264 /* Non-zero means we've seen at least one glyph that came from a
14265 display string. */
14266 int string_seen = 0;
14267 /* Largest and smallest buffer positions seen so far during scan of
14268 glyph row. */
14269 ptrdiff_t bpos_max = pos_before;
14270 ptrdiff_t bpos_min = pos_after;
14271 /* Last buffer position covered by an overlay string with an integer
14272 `cursor' property. */
14273 ptrdiff_t bpos_covered = 0;
14274 /* Non-zero means the display string on which to display the cursor
14275 comes from a text property, not from an overlay. */
14276 int string_from_text_prop = 0;
14278 /* Don't even try doing anything if called for a mode-line or
14279 header-line row, since the rest of the code isn't prepared to
14280 deal with such calamities. */
14281 eassert (!row->mode_line_p);
14282 if (row->mode_line_p)
14283 return 0;
14285 /* Skip over glyphs not having an object at the start and the end of
14286 the row. These are special glyphs like truncation marks on
14287 terminal frames. */
14288 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
14290 if (!row->reversed_p)
14292 while (glyph < end
14293 && NILP (glyph->object)
14294 && glyph->charpos < 0)
14296 x += glyph->pixel_width;
14297 ++glyph;
14299 while (end > glyph
14300 && NILP ((end - 1)->object)
14301 /* CHARPOS is zero for blanks and stretch glyphs
14302 inserted by extend_face_to_end_of_line. */
14303 && (end - 1)->charpos <= 0)
14304 --end;
14305 glyph_before = glyph - 1;
14306 glyph_after = end;
14308 else
14310 struct glyph *g;
14312 /* If the glyph row is reversed, we need to process it from back
14313 to front, so swap the edge pointers. */
14314 glyphs_end = end = glyph - 1;
14315 glyph += row->used[TEXT_AREA] - 1;
14317 while (glyph > end + 1
14318 && NILP (glyph->object)
14319 && glyph->charpos < 0)
14321 --glyph;
14322 x -= glyph->pixel_width;
14324 if (NILP (glyph->object) && glyph->charpos < 0)
14325 --glyph;
14326 /* By default, in reversed rows we put the cursor on the
14327 rightmost (first in the reading order) glyph. */
14328 for (g = end + 1; g < glyph; g++)
14329 x += g->pixel_width;
14330 while (end < glyph
14331 && NILP ((end + 1)->object)
14332 && (end + 1)->charpos <= 0)
14333 ++end;
14334 glyph_before = glyph + 1;
14335 glyph_after = end;
14338 else if (row->reversed_p)
14340 /* In R2L rows that don't display text, put the cursor on the
14341 rightmost glyph. Case in point: an empty last line that is
14342 part of an R2L paragraph. */
14343 cursor = end - 1;
14344 /* Avoid placing the cursor on the last glyph of the row, where
14345 on terminal frames we hold the vertical border between
14346 adjacent windows. */
14347 if (!FRAME_WINDOW_P (WINDOW_XFRAME (w))
14348 && !WINDOW_RIGHTMOST_P (w)
14349 && cursor == row->glyphs[LAST_AREA] - 1)
14350 cursor--;
14351 x = -1; /* will be computed below, at label compute_x */
14354 /* Step 1: Try to find the glyph whose character position
14355 corresponds to point. If that's not possible, find 2 glyphs
14356 whose character positions are the closest to point, one before
14357 point, the other after it. */
14358 if (!row->reversed_p)
14359 while (/* not marched to end of glyph row */
14360 glyph < end
14361 /* glyph was not inserted by redisplay for internal purposes */
14362 && !NILP (glyph->object))
14364 if (BUFFERP (glyph->object))
14366 ptrdiff_t dpos = glyph->charpos - pt_old;
14368 if (glyph->charpos > bpos_max)
14369 bpos_max = glyph->charpos;
14370 if (glyph->charpos < bpos_min)
14371 bpos_min = glyph->charpos;
14372 if (!glyph->avoid_cursor_p)
14374 /* If we hit point, we've found the glyph on which to
14375 display the cursor. */
14376 if (dpos == 0)
14378 match_with_avoid_cursor = 0;
14379 break;
14381 /* See if we've found a better approximation to
14382 POS_BEFORE or to POS_AFTER. */
14383 if (0 > dpos && dpos > pos_before - pt_old)
14385 pos_before = glyph->charpos;
14386 glyph_before = glyph;
14388 else if (0 < dpos && dpos < pos_after - pt_old)
14390 pos_after = glyph->charpos;
14391 glyph_after = glyph;
14394 else if (dpos == 0)
14395 match_with_avoid_cursor = 1;
14397 else if (STRINGP (glyph->object))
14399 Lisp_Object chprop;
14400 ptrdiff_t glyph_pos = glyph->charpos;
14402 chprop = Fget_char_property (make_number (glyph_pos), Qcursor,
14403 glyph->object);
14404 if (!NILP (chprop))
14406 /* If the string came from a `display' text property,
14407 look up the buffer position of that property and
14408 use that position to update bpos_max, as if we
14409 actually saw such a position in one of the row's
14410 glyphs. This helps with supporting integer values
14411 of `cursor' property on the display string in
14412 situations where most or all of the row's buffer
14413 text is completely covered by display properties,
14414 so that no glyph with valid buffer positions is
14415 ever seen in the row. */
14416 ptrdiff_t prop_pos =
14417 string_buffer_position_lim (glyph->object, pos_before,
14418 pos_after, 0);
14420 if (prop_pos >= pos_before)
14421 bpos_max = prop_pos;
14423 if (INTEGERP (chprop))
14425 bpos_covered = bpos_max + XINT (chprop);
14426 /* If the `cursor' property covers buffer positions up
14427 to and including point, we should display cursor on
14428 this glyph. Note that, if a `cursor' property on one
14429 of the string's characters has an integer value, we
14430 will break out of the loop below _before_ we get to
14431 the position match above. IOW, integer values of
14432 the `cursor' property override the "exact match for
14433 point" strategy of positioning the cursor. */
14434 /* Implementation note: bpos_max == pt_old when, e.g.,
14435 we are in an empty line, where bpos_max is set to
14436 MATRIX_ROW_START_CHARPOS, see above. */
14437 if (bpos_max <= pt_old && bpos_covered >= pt_old)
14439 cursor = glyph;
14440 break;
14444 string_seen = 1;
14446 x += glyph->pixel_width;
14447 ++glyph;
14449 else if (glyph > end) /* row is reversed */
14450 while (!NILP (glyph->object))
14452 if (BUFFERP (glyph->object))
14454 ptrdiff_t dpos = glyph->charpos - pt_old;
14456 if (glyph->charpos > bpos_max)
14457 bpos_max = glyph->charpos;
14458 if (glyph->charpos < bpos_min)
14459 bpos_min = glyph->charpos;
14460 if (!glyph->avoid_cursor_p)
14462 if (dpos == 0)
14464 match_with_avoid_cursor = 0;
14465 break;
14467 if (0 > dpos && dpos > pos_before - pt_old)
14469 pos_before = glyph->charpos;
14470 glyph_before = glyph;
14472 else if (0 < dpos && dpos < pos_after - pt_old)
14474 pos_after = glyph->charpos;
14475 glyph_after = glyph;
14478 else if (dpos == 0)
14479 match_with_avoid_cursor = 1;
14481 else if (STRINGP (glyph->object))
14483 Lisp_Object chprop;
14484 ptrdiff_t glyph_pos = glyph->charpos;
14486 chprop = Fget_char_property (make_number (glyph_pos), Qcursor,
14487 glyph->object);
14488 if (!NILP (chprop))
14490 ptrdiff_t prop_pos =
14491 string_buffer_position_lim (glyph->object, pos_before,
14492 pos_after, 0);
14494 if (prop_pos >= pos_before)
14495 bpos_max = prop_pos;
14497 if (INTEGERP (chprop))
14499 bpos_covered = bpos_max + XINT (chprop);
14500 /* If the `cursor' property covers buffer positions up
14501 to and including point, we should display cursor on
14502 this glyph. */
14503 if (bpos_max <= pt_old && bpos_covered >= pt_old)
14505 cursor = glyph;
14506 break;
14509 string_seen = 1;
14511 --glyph;
14512 if (glyph == glyphs_end) /* don't dereference outside TEXT_AREA */
14514 x--; /* can't use any pixel_width */
14515 break;
14517 x -= glyph->pixel_width;
14520 /* Step 2: If we didn't find an exact match for point, we need to
14521 look for a proper place to put the cursor among glyphs between
14522 GLYPH_BEFORE and GLYPH_AFTER. */
14523 if (!((row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end)
14524 && BUFFERP (glyph->object) && glyph->charpos == pt_old)
14525 && !(bpos_max <= pt_old && pt_old <= bpos_covered))
14527 /* An empty line has a single glyph whose OBJECT is nil and
14528 whose CHARPOS is the position of a newline on that line.
14529 Note that on a TTY, there are more glyphs after that, which
14530 were produced by extend_face_to_end_of_line, but their
14531 CHARPOS is zero or negative. */
14532 int empty_line_p =
14533 (row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end)
14534 && NILP (glyph->object) && glyph->charpos > 0
14535 /* On a TTY, continued and truncated rows also have a glyph at
14536 their end whose OBJECT is nil and whose CHARPOS is
14537 positive (the continuation and truncation glyphs), but such
14538 rows are obviously not "empty". */
14539 && !(row->continued_p || row->truncated_on_right_p);
14541 if (row->ends_in_ellipsis_p && pos_after == last_pos)
14543 ptrdiff_t ellipsis_pos;
14545 /* Scan back over the ellipsis glyphs. */
14546 if (!row->reversed_p)
14548 ellipsis_pos = (glyph - 1)->charpos;
14549 while (glyph > row->glyphs[TEXT_AREA]
14550 && (glyph - 1)->charpos == ellipsis_pos)
14551 glyph--, x -= glyph->pixel_width;
14552 /* That loop always goes one position too far, including
14553 the glyph before the ellipsis. So scan forward over
14554 that one. */
14555 x += glyph->pixel_width;
14556 glyph++;
14558 else /* row is reversed */
14560 ellipsis_pos = (glyph + 1)->charpos;
14561 while (glyph < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
14562 && (glyph + 1)->charpos == ellipsis_pos)
14563 glyph++, x += glyph->pixel_width;
14564 x -= glyph->pixel_width;
14565 glyph--;
14568 else if (match_with_avoid_cursor)
14570 cursor = glyph_after;
14571 x = -1;
14573 else if (string_seen)
14575 int incr = row->reversed_p ? -1 : +1;
14577 /* Need to find the glyph that came out of a string which is
14578 present at point. That glyph is somewhere between
14579 GLYPH_BEFORE and GLYPH_AFTER, and it came from a string
14580 positioned between POS_BEFORE and POS_AFTER in the
14581 buffer. */
14582 struct glyph *start, *stop;
14583 ptrdiff_t pos = pos_before;
14585 x = -1;
14587 /* If the row ends in a newline from a display string,
14588 reordering could have moved the glyphs belonging to the
14589 string out of the [GLYPH_BEFORE..GLYPH_AFTER] range. So
14590 in this case we extend the search to the last glyph in
14591 the row that was not inserted by redisplay. */
14592 if (row->ends_in_newline_from_string_p)
14594 glyph_after = end;
14595 pos_after = MATRIX_ROW_END_CHARPOS (row) + delta;
14598 /* GLYPH_BEFORE and GLYPH_AFTER are the glyphs that
14599 correspond to POS_BEFORE and POS_AFTER, respectively. We
14600 need START and STOP in the order that corresponds to the
14601 row's direction as given by its reversed_p flag. If the
14602 directionality of characters between POS_BEFORE and
14603 POS_AFTER is the opposite of the row's base direction,
14604 these characters will have been reordered for display,
14605 and we need to reverse START and STOP. */
14606 if (!row->reversed_p)
14608 start = min (glyph_before, glyph_after);
14609 stop = max (glyph_before, glyph_after);
14611 else
14613 start = max (glyph_before, glyph_after);
14614 stop = min (glyph_before, glyph_after);
14616 for (glyph = start + incr;
14617 row->reversed_p ? glyph > stop : glyph < stop; )
14620 /* Any glyphs that come from the buffer are here because
14621 of bidi reordering. Skip them, and only pay
14622 attention to glyphs that came from some string. */
14623 if (STRINGP (glyph->object))
14625 Lisp_Object str;
14626 ptrdiff_t tem;
14627 /* If the display property covers the newline, we
14628 need to search for it one position farther. */
14629 ptrdiff_t lim = pos_after
14630 + (pos_after == MATRIX_ROW_END_CHARPOS (row) + delta);
14632 string_from_text_prop = 0;
14633 str = glyph->object;
14634 tem = string_buffer_position_lim (str, pos, lim, 0);
14635 if (tem == 0 /* from overlay */
14636 || pos <= tem)
14638 /* If the string from which this glyph came is
14639 found in the buffer at point, or at position
14640 that is closer to point than pos_after, then
14641 we've found the glyph we've been looking for.
14642 If it comes from an overlay (tem == 0), and
14643 it has the `cursor' property on one of its
14644 glyphs, record that glyph as a candidate for
14645 displaying the cursor. (As in the
14646 unidirectional version, we will display the
14647 cursor on the last candidate we find.) */
14648 if (tem == 0
14649 || tem == pt_old
14650 || (tem - pt_old > 0 && tem < pos_after))
14652 /* The glyphs from this string could have
14653 been reordered. Find the one with the
14654 smallest string position. Or there could
14655 be a character in the string with the
14656 `cursor' property, which means display
14657 cursor on that character's glyph. */
14658 ptrdiff_t strpos = glyph->charpos;
14660 if (tem)
14662 cursor = glyph;
14663 string_from_text_prop = 1;
14665 for ( ;
14666 (row->reversed_p ? glyph > stop : glyph < stop)
14667 && EQ (glyph->object, str);
14668 glyph += incr)
14670 Lisp_Object cprop;
14671 ptrdiff_t gpos = glyph->charpos;
14673 cprop = Fget_char_property (make_number (gpos),
14674 Qcursor,
14675 glyph->object);
14676 if (!NILP (cprop))
14678 cursor = glyph;
14679 break;
14681 if (tem && glyph->charpos < strpos)
14683 strpos = glyph->charpos;
14684 cursor = glyph;
14688 if (tem == pt_old
14689 || (tem - pt_old > 0 && tem < pos_after))
14690 goto compute_x;
14692 if (tem)
14693 pos = tem + 1; /* don't find previous instances */
14695 /* This string is not what we want; skip all of the
14696 glyphs that came from it. */
14697 while ((row->reversed_p ? glyph > stop : glyph < stop)
14698 && EQ (glyph->object, str))
14699 glyph += incr;
14701 else
14702 glyph += incr;
14705 /* If we reached the end of the line, and END was from a string,
14706 the cursor is not on this line. */
14707 if (cursor == NULL
14708 && (row->reversed_p ? glyph <= end : glyph >= end)
14709 && (row->reversed_p ? end > glyphs_end : end < glyphs_end)
14710 && STRINGP (end->object)
14711 && row->continued_p)
14712 return 0;
14714 /* A truncated row may not include PT among its character positions.
14715 Setting the cursor inside the scroll margin will trigger
14716 recalculation of hscroll in hscroll_window_tree. But if a
14717 display string covers point, defer to the string-handling
14718 code below to figure this out. */
14719 else if (row->truncated_on_left_p && pt_old < bpos_min)
14721 cursor = glyph_before;
14722 x = -1;
14724 else if ((row->truncated_on_right_p && pt_old > bpos_max)
14725 /* Zero-width characters produce no glyphs. */
14726 || (!empty_line_p
14727 && (row->reversed_p
14728 ? glyph_after > glyphs_end
14729 : glyph_after < glyphs_end)))
14731 cursor = glyph_after;
14732 x = -1;
14736 compute_x:
14737 if (cursor != NULL)
14738 glyph = cursor;
14739 else if (glyph == glyphs_end
14740 && pos_before == pos_after
14741 && STRINGP ((row->reversed_p
14742 ? row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
14743 : row->glyphs[TEXT_AREA])->object))
14745 /* If all the glyphs of this row came from strings, put the
14746 cursor on the first glyph of the row. This avoids having the
14747 cursor outside of the text area in this very rare and hard
14748 use case. */
14749 glyph =
14750 row->reversed_p
14751 ? row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
14752 : row->glyphs[TEXT_AREA];
14754 if (x < 0)
14756 struct glyph *g;
14758 /* Need to compute x that corresponds to GLYPH. */
14759 for (g = row->glyphs[TEXT_AREA], x = row->x; g < glyph; g++)
14761 if (g >= row->glyphs[TEXT_AREA] + row->used[TEXT_AREA])
14762 emacs_abort ();
14763 x += g->pixel_width;
14767 /* ROW could be part of a continued line, which, under bidi
14768 reordering, might have other rows whose start and end charpos
14769 occlude point. Only set w->cursor if we found a better
14770 approximation to the cursor position than we have from previously
14771 examined candidate rows belonging to the same continued line. */
14772 if (/* We already have a candidate row. */
14773 w->cursor.vpos >= 0
14774 /* That candidate is not the row we are processing. */
14775 && MATRIX_ROW (matrix, w->cursor.vpos) != row
14776 /* Make sure cursor.vpos specifies a row whose start and end
14777 charpos occlude point, and it is valid candidate for being a
14778 cursor-row. This is because some callers of this function
14779 leave cursor.vpos at the row where the cursor was displayed
14780 during the last redisplay cycle. */
14781 && MATRIX_ROW_START_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos)) <= pt_old
14782 && pt_old <= MATRIX_ROW_END_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
14783 && cursor_row_p (MATRIX_ROW (matrix, w->cursor.vpos)))
14785 struct glyph *g1
14786 = MATRIX_ROW_GLYPH_START (matrix, w->cursor.vpos) + w->cursor.hpos;
14788 /* Don't consider glyphs that are outside TEXT_AREA. */
14789 if (!(row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end))
14790 return 0;
14791 /* Keep the candidate whose buffer position is the closest to
14792 point or has the `cursor' property. */
14793 if (/* Previous candidate is a glyph in TEXT_AREA of that row. */
14794 w->cursor.hpos >= 0
14795 && w->cursor.hpos < MATRIX_ROW_USED (matrix, w->cursor.vpos)
14796 && ((BUFFERP (g1->object)
14797 && (g1->charpos == pt_old /* An exact match always wins. */
14798 || (BUFFERP (glyph->object)
14799 && eabs (g1->charpos - pt_old)
14800 < eabs (glyph->charpos - pt_old))))
14801 /* Previous candidate is a glyph from a string that has
14802 a non-nil `cursor' property. */
14803 || (STRINGP (g1->object)
14804 && (!NILP (Fget_char_property (make_number (g1->charpos),
14805 Qcursor, g1->object))
14806 /* Previous candidate is from the same display
14807 string as this one, and the display string
14808 came from a text property. */
14809 || (EQ (g1->object, glyph->object)
14810 && string_from_text_prop)
14811 /* this candidate is from newline and its
14812 position is not an exact match */
14813 || (NILP (glyph->object)
14814 && glyph->charpos != pt_old)))))
14815 return 0;
14816 /* If this candidate gives an exact match, use that. */
14817 if (!((BUFFERP (glyph->object) && glyph->charpos == pt_old)
14818 /* If this candidate is a glyph created for the
14819 terminating newline of a line, and point is on that
14820 newline, it wins because it's an exact match. */
14821 || (!row->continued_p
14822 && NILP (glyph->object)
14823 && glyph->charpos == 0
14824 && pt_old == MATRIX_ROW_END_CHARPOS (row) - 1))
14825 /* Otherwise, keep the candidate that comes from a row
14826 spanning less buffer positions. This may win when one or
14827 both candidate positions are on glyphs that came from
14828 display strings, for which we cannot compare buffer
14829 positions. */
14830 && MATRIX_ROW_END_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
14831 - MATRIX_ROW_START_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
14832 < MATRIX_ROW_END_CHARPOS (row) - MATRIX_ROW_START_CHARPOS (row))
14833 return 0;
14835 w->cursor.hpos = glyph - row->glyphs[TEXT_AREA];
14836 w->cursor.x = x;
14837 w->cursor.vpos = MATRIX_ROW_VPOS (row, matrix) + dvpos;
14838 w->cursor.y = row->y + dy;
14840 if (w == XWINDOW (selected_window))
14842 if (!row->continued_p
14843 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
14844 && row->x == 0)
14846 this_line_buffer = XBUFFER (w->contents);
14848 CHARPOS (this_line_start_pos)
14849 = MATRIX_ROW_START_CHARPOS (row) + delta;
14850 BYTEPOS (this_line_start_pos)
14851 = MATRIX_ROW_START_BYTEPOS (row) + delta_bytes;
14853 CHARPOS (this_line_end_pos)
14854 = Z - (MATRIX_ROW_END_CHARPOS (row) + delta);
14855 BYTEPOS (this_line_end_pos)
14856 = Z_BYTE - (MATRIX_ROW_END_BYTEPOS (row) + delta_bytes);
14858 this_line_y = w->cursor.y;
14859 this_line_pixel_height = row->height;
14860 this_line_vpos = w->cursor.vpos;
14861 this_line_start_x = row->x;
14863 else
14864 CHARPOS (this_line_start_pos) = 0;
14867 return 1;
14871 /* Run window scroll functions, if any, for WINDOW with new window
14872 start STARTP. Sets the window start of WINDOW to that position.
14874 We assume that the window's buffer is really current. */
14876 static struct text_pos
14877 run_window_scroll_functions (Lisp_Object window, struct text_pos startp)
14879 struct window *w = XWINDOW (window);
14880 SET_MARKER_FROM_TEXT_POS (w->start, startp);
14882 eassert (current_buffer == XBUFFER (w->contents));
14884 if (!NILP (Vwindow_scroll_functions))
14886 run_hook_with_args_2 (Qwindow_scroll_functions, window,
14887 make_number (CHARPOS (startp)));
14888 SET_TEXT_POS_FROM_MARKER (startp, w->start);
14889 /* In case the hook functions switch buffers. */
14890 set_buffer_internal (XBUFFER (w->contents));
14893 return startp;
14897 /* Make sure the line containing the cursor is fully visible.
14898 A value of 1 means there is nothing to be done.
14899 (Either the line is fully visible, or it cannot be made so,
14900 or we cannot tell.)
14902 If FORCE_P is non-zero, return 0 even if partial visible cursor row
14903 is higher than window.
14905 If CURRENT_MATRIX_P is non-zero, use the information from the
14906 window's current glyph matrix; otherwise use the desired glyph
14907 matrix.
14909 A value of 0 means the caller should do scrolling
14910 as if point had gone off the screen. */
14912 static int
14913 cursor_row_fully_visible_p (struct window *w, int force_p, int current_matrix_p)
14915 struct glyph_matrix *matrix;
14916 struct glyph_row *row;
14917 int window_height;
14919 if (!make_cursor_line_fully_visible_p)
14920 return 1;
14922 /* It's not always possible to find the cursor, e.g, when a window
14923 is full of overlay strings. Don't do anything in that case. */
14924 if (w->cursor.vpos < 0)
14925 return 1;
14927 matrix = current_matrix_p ? w->current_matrix : w->desired_matrix;
14928 row = MATRIX_ROW (matrix, w->cursor.vpos);
14930 /* If the cursor row is not partially visible, there's nothing to do. */
14931 if (!MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row))
14932 return 1;
14934 /* If the row the cursor is in is taller than the window's height,
14935 it's not clear what to do, so do nothing. */
14936 window_height = window_box_height (w);
14937 if (row->height >= window_height)
14939 if (!force_p || MINI_WINDOW_P (w)
14940 || w->vscroll || w->cursor.vpos == 0)
14941 return 1;
14943 return 0;
14947 /* Try scrolling PT into view in window WINDOW. JUST_THIS_ONE_P
14948 non-zero means only WINDOW is redisplayed in redisplay_internal.
14949 TEMP_SCROLL_STEP has the same meaning as emacs_scroll_step, and is used
14950 in redisplay_window to bring a partially visible line into view in
14951 the case that only the cursor has moved.
14953 LAST_LINE_MISFIT should be nonzero if we're scrolling because the
14954 last screen line's vertical height extends past the end of the screen.
14956 Value is
14958 1 if scrolling succeeded
14960 0 if scrolling didn't find point.
14962 -1 if new fonts have been loaded so that we must interrupt
14963 redisplay, adjust glyph matrices, and try again. */
14965 enum
14967 SCROLLING_SUCCESS,
14968 SCROLLING_FAILED,
14969 SCROLLING_NEED_LARGER_MATRICES
14972 /* If scroll-conservatively is more than this, never recenter.
14974 If you change this, don't forget to update the doc string of
14975 `scroll-conservatively' and the Emacs manual. */
14976 #define SCROLL_LIMIT 100
14978 static int
14979 try_scrolling (Lisp_Object window, int just_this_one_p,
14980 ptrdiff_t arg_scroll_conservatively, ptrdiff_t scroll_step,
14981 int temp_scroll_step, int last_line_misfit)
14983 struct window *w = XWINDOW (window);
14984 struct frame *f = XFRAME (w->frame);
14985 struct text_pos pos, startp;
14986 struct it it;
14987 int this_scroll_margin, scroll_max, rc, height;
14988 int dy = 0, amount_to_scroll = 0, scroll_down_p = 0;
14989 int extra_scroll_margin_lines = last_line_misfit ? 1 : 0;
14990 Lisp_Object aggressive;
14991 /* We will never try scrolling more than this number of lines. */
14992 int scroll_limit = SCROLL_LIMIT;
14993 int frame_line_height = default_line_pixel_height (w);
14994 int window_total_lines
14995 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
14997 #ifdef GLYPH_DEBUG
14998 debug_method_add (w, "try_scrolling");
14999 #endif
15001 SET_TEXT_POS_FROM_MARKER (startp, w->start);
15003 /* Compute scroll margin height in pixels. We scroll when point is
15004 within this distance from the top or bottom of the window. */
15005 if (scroll_margin > 0)
15006 this_scroll_margin = min (scroll_margin, window_total_lines / 4)
15007 * frame_line_height;
15008 else
15009 this_scroll_margin = 0;
15011 /* Force arg_scroll_conservatively to have a reasonable value, to
15012 avoid scrolling too far away with slow move_it_* functions. Note
15013 that the user can supply scroll-conservatively equal to
15014 `most-positive-fixnum', which can be larger than INT_MAX. */
15015 if (arg_scroll_conservatively > scroll_limit)
15017 arg_scroll_conservatively = scroll_limit + 1;
15018 scroll_max = scroll_limit * frame_line_height;
15020 else if (scroll_step || arg_scroll_conservatively || temp_scroll_step)
15021 /* Compute how much we should try to scroll maximally to bring
15022 point into view. */
15023 scroll_max = (max (scroll_step,
15024 max (arg_scroll_conservatively, temp_scroll_step))
15025 * frame_line_height);
15026 else if (NUMBERP (BVAR (current_buffer, scroll_down_aggressively))
15027 || NUMBERP (BVAR (current_buffer, scroll_up_aggressively)))
15028 /* We're trying to scroll because of aggressive scrolling but no
15029 scroll_step is set. Choose an arbitrary one. */
15030 scroll_max = 10 * frame_line_height;
15031 else
15032 scroll_max = 0;
15034 too_near_end:
15036 /* Decide whether to scroll down. */
15037 if (PT > CHARPOS (startp))
15039 int scroll_margin_y;
15041 /* Compute the pixel ypos of the scroll margin, then move IT to
15042 either that ypos or PT, whichever comes first. */
15043 start_display (&it, w, startp);
15044 scroll_margin_y = it.last_visible_y - this_scroll_margin
15045 - frame_line_height * extra_scroll_margin_lines;
15046 move_it_to (&it, PT, -1, scroll_margin_y - 1, -1,
15047 (MOVE_TO_POS | MOVE_TO_Y));
15049 if (PT > CHARPOS (it.current.pos))
15051 int y0 = line_bottom_y (&it);
15052 /* Compute how many pixels below window bottom to stop searching
15053 for PT. This avoids costly search for PT that is far away if
15054 the user limited scrolling by a small number of lines, but
15055 always finds PT if scroll_conservatively is set to a large
15056 number, such as most-positive-fixnum. */
15057 int slack = max (scroll_max, 10 * frame_line_height);
15058 int y_to_move = it.last_visible_y + slack;
15060 /* Compute the distance from the scroll margin to PT or to
15061 the scroll limit, whichever comes first. This should
15062 include the height of the cursor line, to make that line
15063 fully visible. */
15064 move_it_to (&it, PT, -1, y_to_move,
15065 -1, MOVE_TO_POS | MOVE_TO_Y);
15066 dy = line_bottom_y (&it) - y0;
15068 if (dy > scroll_max)
15069 return SCROLLING_FAILED;
15071 if (dy > 0)
15072 scroll_down_p = 1;
15076 if (scroll_down_p)
15078 /* Point is in or below the bottom scroll margin, so move the
15079 window start down. If scrolling conservatively, move it just
15080 enough down to make point visible. If scroll_step is set,
15081 move it down by scroll_step. */
15082 if (arg_scroll_conservatively)
15083 amount_to_scroll
15084 = min (max (dy, frame_line_height),
15085 frame_line_height * arg_scroll_conservatively);
15086 else if (scroll_step || temp_scroll_step)
15087 amount_to_scroll = scroll_max;
15088 else
15090 aggressive = BVAR (current_buffer, scroll_up_aggressively);
15091 height = WINDOW_BOX_TEXT_HEIGHT (w);
15092 if (NUMBERP (aggressive))
15094 double float_amount = XFLOATINT (aggressive) * height;
15095 int aggressive_scroll = float_amount;
15096 if (aggressive_scroll == 0 && float_amount > 0)
15097 aggressive_scroll = 1;
15098 /* Don't let point enter the scroll margin near top of
15099 the window. This could happen if the value of
15100 scroll_up_aggressively is too large and there are
15101 non-zero margins, because scroll_up_aggressively
15102 means put point that fraction of window height
15103 _from_the_bottom_margin_. */
15104 if (aggressive_scroll + 2 * this_scroll_margin > height)
15105 aggressive_scroll = height - 2 * this_scroll_margin;
15106 amount_to_scroll = dy + aggressive_scroll;
15110 if (amount_to_scroll <= 0)
15111 return SCROLLING_FAILED;
15113 start_display (&it, w, startp);
15114 if (arg_scroll_conservatively <= scroll_limit)
15115 move_it_vertically (&it, amount_to_scroll);
15116 else
15118 /* Extra precision for users who set scroll-conservatively
15119 to a large number: make sure the amount we scroll
15120 the window start is never less than amount_to_scroll,
15121 which was computed as distance from window bottom to
15122 point. This matters when lines at window top and lines
15123 below window bottom have different height. */
15124 struct it it1;
15125 void *it1data = NULL;
15126 /* We use a temporary it1 because line_bottom_y can modify
15127 its argument, if it moves one line down; see there. */
15128 int start_y;
15130 SAVE_IT (it1, it, it1data);
15131 start_y = line_bottom_y (&it1);
15132 do {
15133 RESTORE_IT (&it, &it, it1data);
15134 move_it_by_lines (&it, 1);
15135 SAVE_IT (it1, it, it1data);
15136 } while (line_bottom_y (&it1) - start_y < amount_to_scroll);
15139 /* If STARTP is unchanged, move it down another screen line. */
15140 if (CHARPOS (it.current.pos) == CHARPOS (startp))
15141 move_it_by_lines (&it, 1);
15142 startp = it.current.pos;
15144 else
15146 struct text_pos scroll_margin_pos = startp;
15147 int y_offset = 0;
15149 /* See if point is inside the scroll margin at the top of the
15150 window. */
15151 if (this_scroll_margin)
15153 int y_start;
15155 start_display (&it, w, startp);
15156 y_start = it.current_y;
15157 move_it_vertically (&it, this_scroll_margin);
15158 scroll_margin_pos = it.current.pos;
15159 /* If we didn't move enough before hitting ZV, request
15160 additional amount of scroll, to move point out of the
15161 scroll margin. */
15162 if (IT_CHARPOS (it) == ZV
15163 && it.current_y - y_start < this_scroll_margin)
15164 y_offset = this_scroll_margin - (it.current_y - y_start);
15167 if (PT < CHARPOS (scroll_margin_pos))
15169 /* Point is in the scroll margin at the top of the window or
15170 above what is displayed in the window. */
15171 int y0, y_to_move;
15173 /* Compute the vertical distance from PT to the scroll
15174 margin position. Move as far as scroll_max allows, or
15175 one screenful, or 10 screen lines, whichever is largest.
15176 Give up if distance is greater than scroll_max or if we
15177 didn't reach the scroll margin position. */
15178 SET_TEXT_POS (pos, PT, PT_BYTE);
15179 start_display (&it, w, pos);
15180 y0 = it.current_y;
15181 y_to_move = max (it.last_visible_y,
15182 max (scroll_max, 10 * frame_line_height));
15183 move_it_to (&it, CHARPOS (scroll_margin_pos), 0,
15184 y_to_move, -1,
15185 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
15186 dy = it.current_y - y0;
15187 if (dy > scroll_max
15188 || IT_CHARPOS (it) < CHARPOS (scroll_margin_pos))
15189 return SCROLLING_FAILED;
15191 /* Additional scroll for when ZV was too close to point. */
15192 dy += y_offset;
15194 /* Compute new window start. */
15195 start_display (&it, w, startp);
15197 if (arg_scroll_conservatively)
15198 amount_to_scroll = max (dy, frame_line_height
15199 * max (scroll_step, temp_scroll_step));
15200 else if (scroll_step || temp_scroll_step)
15201 amount_to_scroll = scroll_max;
15202 else
15204 aggressive = BVAR (current_buffer, scroll_down_aggressively);
15205 height = WINDOW_BOX_TEXT_HEIGHT (w);
15206 if (NUMBERP (aggressive))
15208 double float_amount = XFLOATINT (aggressive) * height;
15209 int aggressive_scroll = float_amount;
15210 if (aggressive_scroll == 0 && float_amount > 0)
15211 aggressive_scroll = 1;
15212 /* Don't let point enter the scroll margin near
15213 bottom of the window, if the value of
15214 scroll_down_aggressively happens to be too
15215 large. */
15216 if (aggressive_scroll + 2 * this_scroll_margin > height)
15217 aggressive_scroll = height - 2 * this_scroll_margin;
15218 amount_to_scroll = dy + aggressive_scroll;
15222 if (amount_to_scroll <= 0)
15223 return SCROLLING_FAILED;
15225 move_it_vertically_backward (&it, amount_to_scroll);
15226 startp = it.current.pos;
15230 /* Run window scroll functions. */
15231 startp = run_window_scroll_functions (window, startp);
15233 /* Display the window. Give up if new fonts are loaded, or if point
15234 doesn't appear. */
15235 if (!try_window (window, startp, 0))
15236 rc = SCROLLING_NEED_LARGER_MATRICES;
15237 else if (w->cursor.vpos < 0)
15239 clear_glyph_matrix (w->desired_matrix);
15240 rc = SCROLLING_FAILED;
15242 else
15244 /* Maybe forget recorded base line for line number display. */
15245 if (!just_this_one_p
15246 || current_buffer->clip_changed
15247 || BEG_UNCHANGED < CHARPOS (startp))
15248 w->base_line_number = 0;
15250 /* If cursor ends up on a partially visible line,
15251 treat that as being off the bottom of the screen. */
15252 if (! cursor_row_fully_visible_p (w, extra_scroll_margin_lines <= 1, 0)
15253 /* It's possible that the cursor is on the first line of the
15254 buffer, which is partially obscured due to a vscroll
15255 (Bug#7537). In that case, avoid looping forever. */
15256 && extra_scroll_margin_lines < w->desired_matrix->nrows - 1)
15258 clear_glyph_matrix (w->desired_matrix);
15259 ++extra_scroll_margin_lines;
15260 goto too_near_end;
15262 rc = SCROLLING_SUCCESS;
15265 return rc;
15269 /* Compute a suitable window start for window W if display of W starts
15270 on a continuation line. Value is non-zero if a new window start
15271 was computed.
15273 The new window start will be computed, based on W's width, starting
15274 from the start of the continued line. It is the start of the
15275 screen line with the minimum distance from the old start W->start. */
15277 static int
15278 compute_window_start_on_continuation_line (struct window *w)
15280 struct text_pos pos, start_pos;
15281 int window_start_changed_p = 0;
15283 SET_TEXT_POS_FROM_MARKER (start_pos, w->start);
15285 /* If window start is on a continuation line... Window start may be
15286 < BEGV in case there's invisible text at the start of the
15287 buffer (M-x rmail, for example). */
15288 if (CHARPOS (start_pos) > BEGV
15289 && FETCH_BYTE (BYTEPOS (start_pos) - 1) != '\n')
15291 struct it it;
15292 struct glyph_row *row;
15294 /* Handle the case that the window start is out of range. */
15295 if (CHARPOS (start_pos) < BEGV)
15296 SET_TEXT_POS (start_pos, BEGV, BEGV_BYTE);
15297 else if (CHARPOS (start_pos) > ZV)
15298 SET_TEXT_POS (start_pos, ZV, ZV_BYTE);
15300 /* Find the start of the continued line. This should be fast
15301 because find_newline is fast (newline cache). */
15302 row = w->desired_matrix->rows + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0);
15303 init_iterator (&it, w, CHARPOS (start_pos), BYTEPOS (start_pos),
15304 row, DEFAULT_FACE_ID);
15305 reseat_at_previous_visible_line_start (&it);
15307 /* If the line start is "too far" away from the window start,
15308 say it takes too much time to compute a new window start. */
15309 if (CHARPOS (start_pos) - IT_CHARPOS (it)
15310 /* PXW: Do we need upper bounds here? */
15311 < WINDOW_TOTAL_LINES (w) * WINDOW_TOTAL_COLS (w))
15313 int min_distance, distance;
15315 /* Move forward by display lines to find the new window
15316 start. If window width was enlarged, the new start can
15317 be expected to be > the old start. If window width was
15318 decreased, the new window start will be < the old start.
15319 So, we're looking for the display line start with the
15320 minimum distance from the old window start. */
15321 pos = it.current.pos;
15322 min_distance = INFINITY;
15323 while ((distance = eabs (CHARPOS (start_pos) - IT_CHARPOS (it))),
15324 distance < min_distance)
15326 min_distance = distance;
15327 pos = it.current.pos;
15328 if (it.line_wrap == WORD_WRAP)
15330 /* Under WORD_WRAP, move_it_by_lines is likely to
15331 overshoot and stop not at the first, but the
15332 second character from the left margin. So in
15333 that case, we need a more tight control on the X
15334 coordinate of the iterator than move_it_by_lines
15335 promises in its contract. The method is to first
15336 go to the last (rightmost) visible character of a
15337 line, then move to the leftmost character on the
15338 next line in a separate call. */
15339 move_it_to (&it, ZV, it.last_visible_x, it.current_y, -1,
15340 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
15341 move_it_to (&it, ZV, 0,
15342 it.current_y + it.max_ascent + it.max_descent, -1,
15343 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
15345 else
15346 move_it_by_lines (&it, 1);
15349 /* Set the window start there. */
15350 SET_MARKER_FROM_TEXT_POS (w->start, pos);
15351 window_start_changed_p = 1;
15355 return window_start_changed_p;
15359 /* Try cursor movement in case text has not changed in window WINDOW,
15360 with window start STARTP. Value is
15362 CURSOR_MOVEMENT_SUCCESS if successful
15364 CURSOR_MOVEMENT_CANNOT_BE_USED if this method cannot be used
15366 CURSOR_MOVEMENT_MUST_SCROLL if we know we have to scroll the
15367 display. *SCROLL_STEP is set to 1, under certain circumstances, if
15368 we want to scroll as if scroll-step were set to 1. See the code.
15370 CURSOR_MOVEMENT_NEED_LARGER_MATRICES if we need larger matrices, in
15371 which case we have to abort this redisplay, and adjust matrices
15372 first. */
15374 enum
15376 CURSOR_MOVEMENT_SUCCESS,
15377 CURSOR_MOVEMENT_CANNOT_BE_USED,
15378 CURSOR_MOVEMENT_MUST_SCROLL,
15379 CURSOR_MOVEMENT_NEED_LARGER_MATRICES
15382 static int
15383 try_cursor_movement (Lisp_Object window, struct text_pos startp, int *scroll_step)
15385 struct window *w = XWINDOW (window);
15386 struct frame *f = XFRAME (w->frame);
15387 int rc = CURSOR_MOVEMENT_CANNOT_BE_USED;
15389 #ifdef GLYPH_DEBUG
15390 if (inhibit_try_cursor_movement)
15391 return rc;
15392 #endif
15394 /* Previously, there was a check for Lisp integer in the
15395 if-statement below. Now, this field is converted to
15396 ptrdiff_t, thus zero means invalid position in a buffer. */
15397 eassert (w->last_point > 0);
15398 /* Likewise there was a check whether window_end_vpos is nil or larger
15399 than the window. Now window_end_vpos is int and so never nil, but
15400 let's leave eassert to check whether it fits in the window. */
15401 eassert (!w->window_end_valid
15402 || w->window_end_vpos < w->current_matrix->nrows);
15404 /* Handle case where text has not changed, only point, and it has
15405 not moved off the frame. */
15406 if (/* Point may be in this window. */
15407 PT >= CHARPOS (startp)
15408 /* Selective display hasn't changed. */
15409 && !current_buffer->clip_changed
15410 /* Function force-mode-line-update is used to force a thorough
15411 redisplay. It sets either windows_or_buffers_changed or
15412 update_mode_lines. So don't take a shortcut here for these
15413 cases. */
15414 && !update_mode_lines
15415 && !windows_or_buffers_changed
15416 && !f->cursor_type_changed
15417 && NILP (Vshow_trailing_whitespace)
15418 /* This code is not used for mini-buffer for the sake of the case
15419 of redisplaying to replace an echo area message; since in
15420 that case the mini-buffer contents per se are usually
15421 unchanged. This code is of no real use in the mini-buffer
15422 since the handling of this_line_start_pos, etc., in redisplay
15423 handles the same cases. */
15424 && !EQ (window, minibuf_window)
15425 && (FRAME_WINDOW_P (f)
15426 || !overlay_arrow_in_current_buffer_p ()))
15428 int this_scroll_margin, top_scroll_margin;
15429 struct glyph_row *row = NULL;
15430 int frame_line_height = default_line_pixel_height (w);
15431 int window_total_lines
15432 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
15434 #ifdef GLYPH_DEBUG
15435 debug_method_add (w, "cursor movement");
15436 #endif
15438 /* Scroll if point within this distance from the top or bottom
15439 of the window. This is a pixel value. */
15440 if (scroll_margin > 0)
15442 this_scroll_margin = min (scroll_margin, window_total_lines / 4);
15443 this_scroll_margin *= frame_line_height;
15445 else
15446 this_scroll_margin = 0;
15448 top_scroll_margin = this_scroll_margin;
15449 if (WINDOW_WANTS_HEADER_LINE_P (w))
15450 top_scroll_margin += CURRENT_HEADER_LINE_HEIGHT (w);
15452 /* Start with the row the cursor was displayed during the last
15453 not paused redisplay. Give up if that row is not valid. */
15454 if (w->last_cursor_vpos < 0
15455 || w->last_cursor_vpos >= w->current_matrix->nrows)
15456 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15457 else
15459 row = MATRIX_ROW (w->current_matrix, w->last_cursor_vpos);
15460 if (row->mode_line_p)
15461 ++row;
15462 if (!row->enabled_p)
15463 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15466 if (rc == CURSOR_MOVEMENT_CANNOT_BE_USED)
15468 int scroll_p = 0, must_scroll = 0;
15469 int last_y = window_text_bottom_y (w) - this_scroll_margin;
15471 if (PT > w->last_point)
15473 /* Point has moved forward. */
15474 while (MATRIX_ROW_END_CHARPOS (row) < PT
15475 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
15477 eassert (row->enabled_p);
15478 ++row;
15481 /* If the end position of a row equals the start
15482 position of the next row, and PT is at that position,
15483 we would rather display cursor in the next line. */
15484 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
15485 && MATRIX_ROW_END_CHARPOS (row) == PT
15486 && row < MATRIX_MODE_LINE_ROW (w->current_matrix)
15487 && MATRIX_ROW_START_CHARPOS (row+1) == PT
15488 && !cursor_row_p (row))
15489 ++row;
15491 /* If within the scroll margin, scroll. Note that
15492 MATRIX_ROW_BOTTOM_Y gives the pixel position at which
15493 the next line would be drawn, and that
15494 this_scroll_margin can be zero. */
15495 if (MATRIX_ROW_BOTTOM_Y (row) > last_y
15496 || PT > MATRIX_ROW_END_CHARPOS (row)
15497 /* Line is completely visible last line in window
15498 and PT is to be set in the next line. */
15499 || (MATRIX_ROW_BOTTOM_Y (row) == last_y
15500 && PT == MATRIX_ROW_END_CHARPOS (row)
15501 && !row->ends_at_zv_p
15502 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
15503 scroll_p = 1;
15505 else if (PT < w->last_point)
15507 /* Cursor has to be moved backward. Note that PT >=
15508 CHARPOS (startp) because of the outer if-statement. */
15509 while (!row->mode_line_p
15510 && (MATRIX_ROW_START_CHARPOS (row) > PT
15511 || (MATRIX_ROW_START_CHARPOS (row) == PT
15512 && (MATRIX_ROW_STARTS_IN_MIDDLE_OF_CHAR_P (row)
15513 || (/* STARTS_IN_MIDDLE_OF_STRING_P (row) */
15514 row > w->current_matrix->rows
15515 && (row-1)->ends_in_newline_from_string_p))))
15516 && (row->y > top_scroll_margin
15517 || CHARPOS (startp) == BEGV))
15519 eassert (row->enabled_p);
15520 --row;
15523 /* Consider the following case: Window starts at BEGV,
15524 there is invisible, intangible text at BEGV, so that
15525 display starts at some point START > BEGV. It can
15526 happen that we are called with PT somewhere between
15527 BEGV and START. Try to handle that case. */
15528 if (row < w->current_matrix->rows
15529 || row->mode_line_p)
15531 row = w->current_matrix->rows;
15532 if (row->mode_line_p)
15533 ++row;
15536 /* Due to newlines in overlay strings, we may have to
15537 skip forward over overlay strings. */
15538 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
15539 && MATRIX_ROW_END_CHARPOS (row) == PT
15540 && !cursor_row_p (row))
15541 ++row;
15543 /* If within the scroll margin, scroll. */
15544 if (row->y < top_scroll_margin
15545 && CHARPOS (startp) != BEGV)
15546 scroll_p = 1;
15548 else
15550 /* Cursor did not move. So don't scroll even if cursor line
15551 is partially visible, as it was so before. */
15552 rc = CURSOR_MOVEMENT_SUCCESS;
15555 if (PT < MATRIX_ROW_START_CHARPOS (row)
15556 || PT > MATRIX_ROW_END_CHARPOS (row))
15558 /* if PT is not in the glyph row, give up. */
15559 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15560 must_scroll = 1;
15562 else if (rc != CURSOR_MOVEMENT_SUCCESS
15563 && !NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering)))
15565 struct glyph_row *row1;
15567 /* If rows are bidi-reordered and point moved, back up
15568 until we find a row that does not belong to a
15569 continuation line. This is because we must consider
15570 all rows of a continued line as candidates for the
15571 new cursor positioning, since row start and end
15572 positions change non-linearly with vertical position
15573 in such rows. */
15574 /* FIXME: Revisit this when glyph ``spilling'' in
15575 continuation lines' rows is implemented for
15576 bidi-reordered rows. */
15577 for (row1 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
15578 MATRIX_ROW_CONTINUATION_LINE_P (row);
15579 --row)
15581 /* If we hit the beginning of the displayed portion
15582 without finding the first row of a continued
15583 line, give up. */
15584 if (row <= row1)
15586 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15587 break;
15589 eassert (row->enabled_p);
15592 if (must_scroll)
15594 else if (rc != CURSOR_MOVEMENT_SUCCESS
15595 && MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row)
15596 /* Make sure this isn't a header line by any chance, since
15597 then MATRIX_ROW_PARTIALLY_VISIBLE_P might yield non-zero. */
15598 && !row->mode_line_p
15599 && make_cursor_line_fully_visible_p)
15601 if (PT == MATRIX_ROW_END_CHARPOS (row)
15602 && !row->ends_at_zv_p
15603 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
15604 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15605 else if (row->height > window_box_height (w))
15607 /* If we end up in a partially visible line, let's
15608 make it fully visible, except when it's taller
15609 than the window, in which case we can't do much
15610 about it. */
15611 *scroll_step = 1;
15612 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15614 else
15616 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
15617 if (!cursor_row_fully_visible_p (w, 0, 1))
15618 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15619 else
15620 rc = CURSOR_MOVEMENT_SUCCESS;
15623 else if (scroll_p)
15624 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15625 else if (rc != CURSOR_MOVEMENT_SUCCESS
15626 && !NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering)))
15628 /* With bidi-reordered rows, there could be more than
15629 one candidate row whose start and end positions
15630 occlude point. We need to let set_cursor_from_row
15631 find the best candidate. */
15632 /* FIXME: Revisit this when glyph ``spilling'' in
15633 continuation lines' rows is implemented for
15634 bidi-reordered rows. */
15635 int rv = 0;
15639 int at_zv_p = 0, exact_match_p = 0;
15641 if (MATRIX_ROW_START_CHARPOS (row) <= PT
15642 && PT <= MATRIX_ROW_END_CHARPOS (row)
15643 && cursor_row_p (row))
15644 rv |= set_cursor_from_row (w, row, w->current_matrix,
15645 0, 0, 0, 0);
15646 /* As soon as we've found the exact match for point,
15647 or the first suitable row whose ends_at_zv_p flag
15648 is set, we are done. */
15649 if (rv)
15651 at_zv_p = MATRIX_ROW (w->current_matrix,
15652 w->cursor.vpos)->ends_at_zv_p;
15653 if (!at_zv_p
15654 && w->cursor.hpos >= 0
15655 && w->cursor.hpos < MATRIX_ROW_USED (w->current_matrix,
15656 w->cursor.vpos))
15658 struct glyph_row *candidate =
15659 MATRIX_ROW (w->current_matrix, w->cursor.vpos);
15660 struct glyph *g =
15661 candidate->glyphs[TEXT_AREA] + w->cursor.hpos;
15662 ptrdiff_t endpos = MATRIX_ROW_END_CHARPOS (candidate);
15664 exact_match_p =
15665 (BUFFERP (g->object) && g->charpos == PT)
15666 || (NILP (g->object)
15667 && (g->charpos == PT
15668 || (g->charpos == 0 && endpos - 1 == PT)));
15670 if (at_zv_p || exact_match_p)
15672 rc = CURSOR_MOVEMENT_SUCCESS;
15673 break;
15676 if (MATRIX_ROW_BOTTOM_Y (row) == last_y)
15677 break;
15678 ++row;
15680 while (((MATRIX_ROW_CONTINUATION_LINE_P (row)
15681 || row->continued_p)
15682 && MATRIX_ROW_BOTTOM_Y (row) <= last_y)
15683 || (MATRIX_ROW_START_CHARPOS (row) == PT
15684 && MATRIX_ROW_BOTTOM_Y (row) < last_y));
15685 /* If we didn't find any candidate rows, or exited the
15686 loop before all the candidates were examined, signal
15687 to the caller that this method failed. */
15688 if (rc != CURSOR_MOVEMENT_SUCCESS
15689 && !(rv
15690 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
15691 && !row->continued_p))
15692 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15693 else if (rv)
15694 rc = CURSOR_MOVEMENT_SUCCESS;
15696 else
15700 if (set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0))
15702 rc = CURSOR_MOVEMENT_SUCCESS;
15703 break;
15705 ++row;
15707 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
15708 && MATRIX_ROW_START_CHARPOS (row) == PT
15709 && cursor_row_p (row));
15714 return rc;
15718 void
15719 set_vertical_scroll_bar (struct window *w)
15721 ptrdiff_t start, end, whole;
15723 /* Calculate the start and end positions for the current window.
15724 At some point, it would be nice to choose between scrollbars
15725 which reflect the whole buffer size, with special markers
15726 indicating narrowing, and scrollbars which reflect only the
15727 visible region.
15729 Note that mini-buffers sometimes aren't displaying any text. */
15730 if (!MINI_WINDOW_P (w)
15731 || (w == XWINDOW (minibuf_window)
15732 && NILP (echo_area_buffer[0])))
15734 struct buffer *buf = XBUFFER (w->contents);
15735 whole = BUF_ZV (buf) - BUF_BEGV (buf);
15736 start = marker_position (w->start) - BUF_BEGV (buf);
15737 /* I don't think this is guaranteed to be right. For the
15738 moment, we'll pretend it is. */
15739 end = BUF_Z (buf) - w->window_end_pos - BUF_BEGV (buf);
15741 if (end < start)
15742 end = start;
15743 if (whole < (end - start))
15744 whole = end - start;
15746 else
15747 start = end = whole = 0;
15749 /* Indicate what this scroll bar ought to be displaying now. */
15750 if (FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
15751 (*FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
15752 (w, end - start, whole, start);
15756 void
15757 set_horizontal_scroll_bar (struct window *w)
15759 int start, end, whole, portion;
15761 if (!MINI_WINDOW_P (w)
15762 || (w == XWINDOW (minibuf_window)
15763 && NILP (echo_area_buffer[0])))
15765 struct buffer *b = XBUFFER (w->contents);
15766 struct buffer *old_buffer = NULL;
15767 struct it it;
15768 struct text_pos startp;
15770 if (b != current_buffer)
15772 old_buffer = current_buffer;
15773 set_buffer_internal (b);
15776 SET_TEXT_POS_FROM_MARKER (startp, w->start);
15777 start_display (&it, w, startp);
15778 it.last_visible_x = INT_MAX;
15779 whole = move_it_to (&it, -1, INT_MAX, window_box_height (w), -1,
15780 MOVE_TO_X | MOVE_TO_Y);
15781 /* whole = move_it_to (&it, w->window_end_pos, INT_MAX,
15782 window_box_height (w), -1,
15783 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y); */
15785 start = w->hscroll * FRAME_COLUMN_WIDTH (WINDOW_XFRAME (w));
15786 end = start + window_box_width (w, TEXT_AREA);
15787 portion = end - start;
15788 /* After enlarging a horizontally scrolled window such that it
15789 gets at least as wide as the text it contains, make sure that
15790 the thumb doesn't fill the entire scroll bar so we can still
15791 drag it back to see the entire text. */
15792 whole = max (whole, end);
15794 if (it.bidi_p)
15796 Lisp_Object pdir;
15798 pdir = Fcurrent_bidi_paragraph_direction (Qnil);
15799 if (EQ (pdir, Qright_to_left))
15801 start = whole - end;
15802 end = start + portion;
15806 if (old_buffer)
15807 set_buffer_internal (old_buffer);
15809 else
15810 start = end = whole = portion = 0;
15812 w->hscroll_whole = whole;
15814 /* Indicate what this scroll bar ought to be displaying now. */
15815 if (FRAME_TERMINAL (XFRAME (w->frame))->set_horizontal_scroll_bar_hook)
15816 (*FRAME_TERMINAL (XFRAME (w->frame))->set_horizontal_scroll_bar_hook)
15817 (w, portion, whole, start);
15821 /* Redisplay leaf window WINDOW. JUST_THIS_ONE_P non-zero means only
15822 selected_window is redisplayed.
15824 We can return without actually redisplaying the window if fonts has been
15825 changed on window's frame. In that case, redisplay_internal will retry.
15827 As one of the important parts of redisplaying a window, we need to
15828 decide whether the previous window-start position (stored in the
15829 window's w->start marker position) is still valid, and if it isn't,
15830 recompute it. Some details about that:
15832 . The previous window-start could be in a continuation line, in
15833 which case we need to recompute it when the window width
15834 changes. See compute_window_start_on_continuation_line and its
15835 call below.
15837 . The text that changed since last redisplay could include the
15838 previous window-start position. In that case, we try to salvage
15839 what we can from the current glyph matrix by calling
15840 try_scrolling, which see.
15842 . Some Emacs command could force us to use a specific window-start
15843 position by setting the window's force_start flag, or gently
15844 propose doing that by setting the window's optional_new_start
15845 flag. In these cases, we try using the specified start point if
15846 that succeeds (i.e. the window desired matrix is successfully
15847 recomputed, and point location is within the window). In case
15848 of optional_new_start, we first check if the specified start
15849 position is feasible, i.e. if it will allow point to be
15850 displayed in the window. If using the specified start point
15851 fails, e.g., if new fonts are needed to be loaded, we abort the
15852 redisplay cycle and leave it up to the next cycle to figure out
15853 things.
15855 . Note that the window's force_start flag is sometimes set by
15856 redisplay itself, when it decides that the previous window start
15857 point is fine and should be kept. Search for "goto force_start"
15858 below to see the details. Like the values of window-start
15859 specified outside of redisplay, these internally-deduced values
15860 are tested for feasibility, and ignored if found to be
15861 unfeasible.
15863 . Note that the function try_window, used to completely redisplay
15864 a window, accepts the window's start point as its argument.
15865 This is used several times in the redisplay code to control
15866 where the window start will be, according to user options such
15867 as scroll-conservatively, and also to ensure the screen line
15868 showing point will be fully (as opposed to partially) visible on
15869 display. */
15871 static void
15872 redisplay_window (Lisp_Object window, bool just_this_one_p)
15874 struct window *w = XWINDOW (window);
15875 struct frame *f = XFRAME (w->frame);
15876 struct buffer *buffer = XBUFFER (w->contents);
15877 struct buffer *old = current_buffer;
15878 struct text_pos lpoint, opoint, startp;
15879 int update_mode_line;
15880 int tem;
15881 struct it it;
15882 /* Record it now because it's overwritten. */
15883 bool current_matrix_up_to_date_p = false;
15884 bool used_current_matrix_p = false;
15885 /* This is less strict than current_matrix_up_to_date_p.
15886 It indicates that the buffer contents and narrowing are unchanged. */
15887 bool buffer_unchanged_p = false;
15888 int temp_scroll_step = 0;
15889 ptrdiff_t count = SPECPDL_INDEX ();
15890 int rc;
15891 int centering_position = -1;
15892 int last_line_misfit = 0;
15893 ptrdiff_t beg_unchanged, end_unchanged;
15894 int frame_line_height;
15896 SET_TEXT_POS (lpoint, PT, PT_BYTE);
15897 opoint = lpoint;
15899 #ifdef GLYPH_DEBUG
15900 *w->desired_matrix->method = 0;
15901 #endif
15903 if (!just_this_one_p
15904 && REDISPLAY_SOME_P ()
15905 && !w->redisplay
15906 && !f->redisplay
15907 && !buffer->text->redisplay
15908 && BUF_PT (buffer) == w->last_point)
15909 return;
15911 /* Make sure that both W's markers are valid. */
15912 eassert (XMARKER (w->start)->buffer == buffer);
15913 eassert (XMARKER (w->pointm)->buffer == buffer);
15915 /* We come here again if we need to run window-text-change-functions
15916 below. */
15917 restart:
15918 reconsider_clip_changes (w);
15919 frame_line_height = default_line_pixel_height (w);
15921 /* Has the mode line to be updated? */
15922 update_mode_line = (w->update_mode_line
15923 || update_mode_lines
15924 || buffer->clip_changed
15925 || buffer->prevent_redisplay_optimizations_p);
15927 if (!just_this_one_p)
15928 /* If `just_this_one_p' is set, we apparently set must_be_updated_p more
15929 cleverly elsewhere. */
15930 w->must_be_updated_p = true;
15932 if (MINI_WINDOW_P (w))
15934 if (w == XWINDOW (echo_area_window)
15935 && !NILP (echo_area_buffer[0]))
15937 if (update_mode_line)
15938 /* We may have to update a tty frame's menu bar or a
15939 tool-bar. Example `M-x C-h C-h C-g'. */
15940 goto finish_menu_bars;
15941 else
15942 /* We've already displayed the echo area glyphs in this window. */
15943 goto finish_scroll_bars;
15945 else if ((w != XWINDOW (minibuf_window)
15946 || minibuf_level == 0)
15947 /* When buffer is nonempty, redisplay window normally. */
15948 && BUF_Z (XBUFFER (w->contents)) == BUF_BEG (XBUFFER (w->contents))
15949 /* Quail displays non-mini buffers in minibuffer window.
15950 In that case, redisplay the window normally. */
15951 && !NILP (Fmemq (w->contents, Vminibuffer_list)))
15953 /* W is a mini-buffer window, but it's not active, so clear
15954 it. */
15955 int yb = window_text_bottom_y (w);
15956 struct glyph_row *row;
15957 int y;
15959 for (y = 0, row = w->desired_matrix->rows;
15960 y < yb;
15961 y += row->height, ++row)
15962 blank_row (w, row, y);
15963 goto finish_scroll_bars;
15966 clear_glyph_matrix (w->desired_matrix);
15969 /* Otherwise set up data on this window; select its buffer and point
15970 value. */
15971 /* Really select the buffer, for the sake of buffer-local
15972 variables. */
15973 set_buffer_internal_1 (XBUFFER (w->contents));
15975 current_matrix_up_to_date_p
15976 = (w->window_end_valid
15977 && !current_buffer->clip_changed
15978 && !current_buffer->prevent_redisplay_optimizations_p
15979 && !window_outdated (w));
15981 /* Run the window-text-change-functions
15982 if it is possible that the text on the screen has changed
15983 (either due to modification of the text, or any other reason). */
15984 if (!current_matrix_up_to_date_p
15985 && !NILP (Vwindow_text_change_functions))
15987 safe_run_hooks (Qwindow_text_change_functions);
15988 goto restart;
15991 beg_unchanged = BEG_UNCHANGED;
15992 end_unchanged = END_UNCHANGED;
15994 SET_TEXT_POS (opoint, PT, PT_BYTE);
15996 specbind (Qinhibit_point_motion_hooks, Qt);
15998 buffer_unchanged_p
15999 = (w->window_end_valid
16000 && !current_buffer->clip_changed
16001 && !window_outdated (w));
16003 /* When windows_or_buffers_changed is non-zero, we can't rely
16004 on the window end being valid, so set it to zero there. */
16005 if (windows_or_buffers_changed)
16007 /* If window starts on a continuation line, maybe adjust the
16008 window start in case the window's width changed. */
16009 if (XMARKER (w->start)->buffer == current_buffer)
16010 compute_window_start_on_continuation_line (w);
16012 w->window_end_valid = false;
16013 /* If so, we also can't rely on current matrix
16014 and should not fool try_cursor_movement below. */
16015 current_matrix_up_to_date_p = false;
16018 /* Some sanity checks. */
16019 CHECK_WINDOW_END (w);
16020 if (Z == Z_BYTE && CHARPOS (opoint) != BYTEPOS (opoint))
16021 emacs_abort ();
16022 if (BYTEPOS (opoint) < CHARPOS (opoint))
16023 emacs_abort ();
16025 if (mode_line_update_needed (w))
16026 update_mode_line = 1;
16028 /* Point refers normally to the selected window. For any other
16029 window, set up appropriate value. */
16030 if (!EQ (window, selected_window))
16032 ptrdiff_t new_pt = marker_position (w->pointm);
16033 ptrdiff_t new_pt_byte = marker_byte_position (w->pointm);
16035 if (new_pt < BEGV)
16037 new_pt = BEGV;
16038 new_pt_byte = BEGV_BYTE;
16039 set_marker_both (w->pointm, Qnil, BEGV, BEGV_BYTE);
16041 else if (new_pt > (ZV - 1))
16043 new_pt = ZV;
16044 new_pt_byte = ZV_BYTE;
16045 set_marker_both (w->pointm, Qnil, ZV, ZV_BYTE);
16048 /* We don't use SET_PT so that the point-motion hooks don't run. */
16049 TEMP_SET_PT_BOTH (new_pt, new_pt_byte);
16052 /* If any of the character widths specified in the display table
16053 have changed, invalidate the width run cache. It's true that
16054 this may be a bit late to catch such changes, but the rest of
16055 redisplay goes (non-fatally) haywire when the display table is
16056 changed, so why should we worry about doing any better? */
16057 if (current_buffer->width_run_cache
16058 || (current_buffer->base_buffer
16059 && current_buffer->base_buffer->width_run_cache))
16061 struct Lisp_Char_Table *disptab = buffer_display_table ();
16063 if (! disptab_matches_widthtab
16064 (disptab, XVECTOR (BVAR (current_buffer, width_table))))
16066 struct buffer *buf = current_buffer;
16068 if (buf->base_buffer)
16069 buf = buf->base_buffer;
16070 invalidate_region_cache (buf, buf->width_run_cache, BEG, Z);
16071 recompute_width_table (current_buffer, disptab);
16075 /* If window-start is screwed up, choose a new one. */
16076 if (XMARKER (w->start)->buffer != current_buffer)
16077 goto recenter;
16079 SET_TEXT_POS_FROM_MARKER (startp, w->start);
16081 /* If someone specified a new starting point but did not insist,
16082 check whether it can be used. */
16083 if ((w->optional_new_start || window_frozen_p (w))
16084 && CHARPOS (startp) >= BEGV
16085 && CHARPOS (startp) <= ZV)
16087 ptrdiff_t it_charpos;
16089 w->optional_new_start = 0;
16090 start_display (&it, w, startp);
16091 move_it_to (&it, PT, 0, it.last_visible_y, -1,
16092 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
16093 /* Record IT's position now, since line_bottom_y might change
16094 that. */
16095 it_charpos = IT_CHARPOS (it);
16096 /* Make sure we set the force_start flag only if the cursor row
16097 will be fully visible. Otherwise, the code under force_start
16098 label below will try to move point back into view, which is
16099 not what the code which sets optional_new_start wants. */
16100 if ((it.current_y == 0 || line_bottom_y (&it) < it.last_visible_y)
16101 && !w->force_start)
16103 if (it_charpos == PT)
16104 w->force_start = 1;
16105 /* IT may overshoot PT if text at PT is invisible. */
16106 else if (it_charpos > PT && CHARPOS (startp) <= PT)
16107 w->force_start = 1;
16108 #ifdef GLYPH_DEBUG
16109 if (w->force_start)
16111 if (window_frozen_p (w))
16112 debug_method_add (w, "set force_start from frozen window start");
16113 else
16114 debug_method_add (w, "set force_start from optional_new_start");
16116 #endif
16120 force_start:
16122 /* Handle case where place to start displaying has been specified,
16123 unless the specified location is outside the accessible range. */
16124 if (w->force_start)
16126 /* We set this later on if we have to adjust point. */
16127 int new_vpos = -1;
16129 w->force_start = 0;
16130 w->vscroll = 0;
16131 w->window_end_valid = 0;
16133 /* Forget any recorded base line for line number display. */
16134 if (!buffer_unchanged_p)
16135 w->base_line_number = 0;
16137 /* Redisplay the mode line. Select the buffer properly for that.
16138 Also, run the hook window-scroll-functions
16139 because we have scrolled. */
16140 /* Note, we do this after clearing force_start because
16141 if there's an error, it is better to forget about force_start
16142 than to get into an infinite loop calling the hook functions
16143 and having them get more errors. */
16144 if (!update_mode_line
16145 || ! NILP (Vwindow_scroll_functions))
16147 update_mode_line = 1;
16148 w->update_mode_line = 1;
16149 startp = run_window_scroll_functions (window, startp);
16152 if (CHARPOS (startp) < BEGV)
16153 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
16154 else if (CHARPOS (startp) > ZV)
16155 SET_TEXT_POS (startp, ZV, ZV_BYTE);
16157 /* Redisplay, then check if cursor has been set during the
16158 redisplay. Give up if new fonts were loaded. */
16159 /* We used to issue a CHECK_MARGINS argument to try_window here,
16160 but this causes scrolling to fail when point begins inside
16161 the scroll margin (bug#148) -- cyd */
16162 if (!try_window (window, startp, 0))
16164 w->force_start = 1;
16165 clear_glyph_matrix (w->desired_matrix);
16166 goto need_larger_matrices;
16169 if (w->cursor.vpos < 0)
16171 /* If point does not appear, try to move point so it does
16172 appear. The desired matrix has been built above, so we
16173 can use it here. */
16174 new_vpos = window_box_height (w) / 2;
16177 if (!cursor_row_fully_visible_p (w, 0, 0))
16179 /* Point does appear, but on a line partly visible at end of window.
16180 Move it back to a fully-visible line. */
16181 new_vpos = window_box_height (w);
16182 /* But if window_box_height suggests a Y coordinate that is
16183 not less than we already have, that line will clearly not
16184 be fully visible, so give up and scroll the display.
16185 This can happen when the default face uses a font whose
16186 dimensions are different from the frame's default
16187 font. */
16188 if (new_vpos >= w->cursor.y)
16190 w->cursor.vpos = -1;
16191 clear_glyph_matrix (w->desired_matrix);
16192 goto try_to_scroll;
16195 else if (w->cursor.vpos >= 0)
16197 /* Some people insist on not letting point enter the scroll
16198 margin, even though this part handles windows that didn't
16199 scroll at all. */
16200 int window_total_lines
16201 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
16202 int margin = min (scroll_margin, window_total_lines / 4);
16203 int pixel_margin = margin * frame_line_height;
16204 bool header_line = WINDOW_WANTS_HEADER_LINE_P (w);
16206 /* Note: We add an extra FRAME_LINE_HEIGHT, because the loop
16207 below, which finds the row to move point to, advances by
16208 the Y coordinate of the _next_ row, see the definition of
16209 MATRIX_ROW_BOTTOM_Y. */
16210 if (w->cursor.vpos < margin + header_line)
16212 w->cursor.vpos = -1;
16213 clear_glyph_matrix (w->desired_matrix);
16214 goto try_to_scroll;
16216 else
16218 int window_height = window_box_height (w);
16220 if (header_line)
16221 window_height += CURRENT_HEADER_LINE_HEIGHT (w);
16222 if (w->cursor.y >= window_height - pixel_margin)
16224 w->cursor.vpos = -1;
16225 clear_glyph_matrix (w->desired_matrix);
16226 goto try_to_scroll;
16231 /* If we need to move point for either of the above reasons,
16232 now actually do it. */
16233 if (new_vpos >= 0)
16235 struct glyph_row *row;
16237 row = MATRIX_FIRST_TEXT_ROW (w->desired_matrix);
16238 while (MATRIX_ROW_BOTTOM_Y (row) < new_vpos)
16239 ++row;
16241 TEMP_SET_PT_BOTH (MATRIX_ROW_START_CHARPOS (row),
16242 MATRIX_ROW_START_BYTEPOS (row));
16244 if (w != XWINDOW (selected_window))
16245 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
16246 else if (current_buffer == old)
16247 SET_TEXT_POS (lpoint, PT, PT_BYTE);
16249 set_cursor_from_row (w, row, w->desired_matrix, 0, 0, 0, 0);
16251 /* Re-run pre-redisplay-function so it can update the region
16252 according to the new position of point. */
16253 /* Other than the cursor, w's redisplay is done so we can set its
16254 redisplay to false. Also the buffer's redisplay can be set to
16255 false, since propagate_buffer_redisplay should have already
16256 propagated its info to `w' anyway. */
16257 w->redisplay = false;
16258 XBUFFER (w->contents)->text->redisplay = false;
16259 safe__call1 (true, Vpre_redisplay_function, Fcons (window, Qnil));
16261 if (w->redisplay || XBUFFER (w->contents)->text->redisplay)
16263 /* pre-redisplay-function made changes (e.g. move the region)
16264 that require another round of redisplay. */
16265 clear_glyph_matrix (w->desired_matrix);
16266 if (!try_window (window, startp, 0))
16267 goto need_larger_matrices;
16270 if (w->cursor.vpos < 0 || !cursor_row_fully_visible_p (w, 0, 0))
16272 clear_glyph_matrix (w->desired_matrix);
16273 goto try_to_scroll;
16276 #ifdef GLYPH_DEBUG
16277 debug_method_add (w, "forced window start");
16278 #endif
16279 goto done;
16282 /* Handle case where text has not changed, only point, and it has
16283 not moved off the frame, and we are not retrying after hscroll.
16284 (current_matrix_up_to_date_p is nonzero when retrying.) */
16285 if (current_matrix_up_to_date_p
16286 && (rc = try_cursor_movement (window, startp, &temp_scroll_step),
16287 rc != CURSOR_MOVEMENT_CANNOT_BE_USED))
16289 switch (rc)
16291 case CURSOR_MOVEMENT_SUCCESS:
16292 used_current_matrix_p = 1;
16293 goto done;
16295 case CURSOR_MOVEMENT_MUST_SCROLL:
16296 goto try_to_scroll;
16298 default:
16299 emacs_abort ();
16302 /* If current starting point was originally the beginning of a line
16303 but no longer is, find a new starting point. */
16304 else if (w->start_at_line_beg
16305 && !(CHARPOS (startp) <= BEGV
16306 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n'))
16308 #ifdef GLYPH_DEBUG
16309 debug_method_add (w, "recenter 1");
16310 #endif
16311 goto recenter;
16314 /* Try scrolling with try_window_id. Value is > 0 if update has
16315 been done, it is -1 if we know that the same window start will
16316 not work. It is 0 if unsuccessful for some other reason. */
16317 else if ((tem = try_window_id (w)) != 0)
16319 #ifdef GLYPH_DEBUG
16320 debug_method_add (w, "try_window_id %d", tem);
16321 #endif
16323 if (f->fonts_changed)
16324 goto need_larger_matrices;
16325 if (tem > 0)
16326 goto done;
16328 /* Otherwise try_window_id has returned -1 which means that we
16329 don't want the alternative below this comment to execute. */
16331 else if (CHARPOS (startp) >= BEGV
16332 && CHARPOS (startp) <= ZV
16333 && PT >= CHARPOS (startp)
16334 && (CHARPOS (startp) < ZV
16335 /* Avoid starting at end of buffer. */
16336 || CHARPOS (startp) == BEGV
16337 || !window_outdated (w)))
16339 int d1, d2, d5, d6;
16340 int rtop, rbot;
16342 /* If first window line is a continuation line, and window start
16343 is inside the modified region, but the first change is before
16344 current window start, we must select a new window start.
16346 However, if this is the result of a down-mouse event (e.g. by
16347 extending the mouse-drag-overlay), we don't want to select a
16348 new window start, since that would change the position under
16349 the mouse, resulting in an unwanted mouse-movement rather
16350 than a simple mouse-click. */
16351 if (!w->start_at_line_beg
16352 && NILP (do_mouse_tracking)
16353 && CHARPOS (startp) > BEGV
16354 && CHARPOS (startp) > BEG + beg_unchanged
16355 && CHARPOS (startp) <= Z - end_unchanged
16356 /* Even if w->start_at_line_beg is nil, a new window may
16357 start at a line_beg, since that's how set_buffer_window
16358 sets it. So, we need to check the return value of
16359 compute_window_start_on_continuation_line. (See also
16360 bug#197). */
16361 && XMARKER (w->start)->buffer == current_buffer
16362 && compute_window_start_on_continuation_line (w)
16363 /* It doesn't make sense to force the window start like we
16364 do at label force_start if it is already known that point
16365 will not be fully visible in the resulting window, because
16366 doing so will move point from its correct position
16367 instead of scrolling the window to bring point into view.
16368 See bug#9324. */
16369 && pos_visible_p (w, PT, &d1, &d2, &rtop, &rbot, &d5, &d6)
16370 /* A very tall row could need more than the window height,
16371 in which case we accept that it is partially visible. */
16372 && (rtop != 0) == (rbot != 0))
16374 w->force_start = 1;
16375 SET_TEXT_POS_FROM_MARKER (startp, w->start);
16376 #ifdef GLYPH_DEBUG
16377 debug_method_add (w, "recomputed window start in continuation line");
16378 #endif
16379 goto force_start;
16382 #ifdef GLYPH_DEBUG
16383 debug_method_add (w, "same window start");
16384 #endif
16386 /* Try to redisplay starting at same place as before.
16387 If point has not moved off frame, accept the results. */
16388 if (!current_matrix_up_to_date_p
16389 /* Don't use try_window_reusing_current_matrix in this case
16390 because a window scroll function can have changed the
16391 buffer. */
16392 || !NILP (Vwindow_scroll_functions)
16393 || MINI_WINDOW_P (w)
16394 || !(used_current_matrix_p
16395 = try_window_reusing_current_matrix (w)))
16397 IF_DEBUG (debug_method_add (w, "1"));
16398 if (try_window (window, startp, TRY_WINDOW_CHECK_MARGINS) < 0)
16399 /* -1 means we need to scroll.
16400 0 means we need new matrices, but fonts_changed
16401 is set in that case, so we will detect it below. */
16402 goto try_to_scroll;
16405 if (f->fonts_changed)
16406 goto need_larger_matrices;
16408 if (w->cursor.vpos >= 0)
16410 if (!just_this_one_p
16411 || current_buffer->clip_changed
16412 || BEG_UNCHANGED < CHARPOS (startp))
16413 /* Forget any recorded base line for line number display. */
16414 w->base_line_number = 0;
16416 if (!cursor_row_fully_visible_p (w, 1, 0))
16418 clear_glyph_matrix (w->desired_matrix);
16419 last_line_misfit = 1;
16421 /* Drop through and scroll. */
16422 else
16423 goto done;
16425 else
16426 clear_glyph_matrix (w->desired_matrix);
16429 try_to_scroll:
16431 /* Redisplay the mode line. Select the buffer properly for that. */
16432 if (!update_mode_line)
16434 update_mode_line = 1;
16435 w->update_mode_line = 1;
16438 /* Try to scroll by specified few lines. */
16439 if ((scroll_conservatively
16440 || emacs_scroll_step
16441 || temp_scroll_step
16442 || NUMBERP (BVAR (current_buffer, scroll_up_aggressively))
16443 || NUMBERP (BVAR (current_buffer, scroll_down_aggressively)))
16444 && CHARPOS (startp) >= BEGV
16445 && CHARPOS (startp) <= ZV)
16447 /* The function returns -1 if new fonts were loaded, 1 if
16448 successful, 0 if not successful. */
16449 int ss = try_scrolling (window, just_this_one_p,
16450 scroll_conservatively,
16451 emacs_scroll_step,
16452 temp_scroll_step, last_line_misfit);
16453 switch (ss)
16455 case SCROLLING_SUCCESS:
16456 goto done;
16458 case SCROLLING_NEED_LARGER_MATRICES:
16459 goto need_larger_matrices;
16461 case SCROLLING_FAILED:
16462 break;
16464 default:
16465 emacs_abort ();
16469 /* Finally, just choose a place to start which positions point
16470 according to user preferences. */
16472 recenter:
16474 #ifdef GLYPH_DEBUG
16475 debug_method_add (w, "recenter");
16476 #endif
16478 /* Forget any previously recorded base line for line number display. */
16479 if (!buffer_unchanged_p)
16480 w->base_line_number = 0;
16482 /* Determine the window start relative to point. */
16483 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
16484 it.current_y = it.last_visible_y;
16485 if (centering_position < 0)
16487 int window_total_lines
16488 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
16489 int margin
16490 = scroll_margin > 0
16491 ? min (scroll_margin, window_total_lines / 4)
16492 : 0;
16493 ptrdiff_t margin_pos = CHARPOS (startp);
16494 Lisp_Object aggressive;
16495 int scrolling_up;
16497 /* If there is a scroll margin at the top of the window, find
16498 its character position. */
16499 if (margin
16500 /* Cannot call start_display if startp is not in the
16501 accessible region of the buffer. This can happen when we
16502 have just switched to a different buffer and/or changed
16503 its restriction. In that case, startp is initialized to
16504 the character position 1 (BEGV) because we did not yet
16505 have chance to display the buffer even once. */
16506 && BEGV <= CHARPOS (startp) && CHARPOS (startp) <= ZV)
16508 struct it it1;
16509 void *it1data = NULL;
16511 SAVE_IT (it1, it, it1data);
16512 start_display (&it1, w, startp);
16513 move_it_vertically (&it1, margin * frame_line_height);
16514 margin_pos = IT_CHARPOS (it1);
16515 RESTORE_IT (&it, &it, it1data);
16517 scrolling_up = PT > margin_pos;
16518 aggressive =
16519 scrolling_up
16520 ? BVAR (current_buffer, scroll_up_aggressively)
16521 : BVAR (current_buffer, scroll_down_aggressively);
16523 if (!MINI_WINDOW_P (w)
16524 && (scroll_conservatively > SCROLL_LIMIT || NUMBERP (aggressive)))
16526 int pt_offset = 0;
16528 /* Setting scroll-conservatively overrides
16529 scroll-*-aggressively. */
16530 if (!scroll_conservatively && NUMBERP (aggressive))
16532 double float_amount = XFLOATINT (aggressive);
16534 pt_offset = float_amount * WINDOW_BOX_TEXT_HEIGHT (w);
16535 if (pt_offset == 0 && float_amount > 0)
16536 pt_offset = 1;
16537 if (pt_offset && margin > 0)
16538 margin -= 1;
16540 /* Compute how much to move the window start backward from
16541 point so that point will be displayed where the user
16542 wants it. */
16543 if (scrolling_up)
16545 centering_position = it.last_visible_y;
16546 if (pt_offset)
16547 centering_position -= pt_offset;
16548 centering_position -=
16549 frame_line_height * (1 + margin + (last_line_misfit != 0))
16550 + WINDOW_HEADER_LINE_HEIGHT (w);
16551 /* Don't let point enter the scroll margin near top of
16552 the window. */
16553 if (centering_position < margin * frame_line_height)
16554 centering_position = margin * frame_line_height;
16556 else
16557 centering_position = margin * frame_line_height + pt_offset;
16559 else
16560 /* Set the window start half the height of the window backward
16561 from point. */
16562 centering_position = window_box_height (w) / 2;
16564 move_it_vertically_backward (&it, centering_position);
16566 eassert (IT_CHARPOS (it) >= BEGV);
16568 /* The function move_it_vertically_backward may move over more
16569 than the specified y-distance. If it->w is small, e.g. a
16570 mini-buffer window, we may end up in front of the window's
16571 display area. Start displaying at the start of the line
16572 containing PT in this case. */
16573 if (it.current_y <= 0)
16575 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
16576 move_it_vertically_backward (&it, 0);
16577 it.current_y = 0;
16580 it.current_x = it.hpos = 0;
16582 /* Set the window start position here explicitly, to avoid an
16583 infinite loop in case the functions in window-scroll-functions
16584 get errors. */
16585 set_marker_both (w->start, Qnil, IT_CHARPOS (it), IT_BYTEPOS (it));
16587 /* Run scroll hooks. */
16588 startp = run_window_scroll_functions (window, it.current.pos);
16590 /* Redisplay the window. */
16591 if (!current_matrix_up_to_date_p
16592 || windows_or_buffers_changed
16593 || f->cursor_type_changed
16594 /* Don't use try_window_reusing_current_matrix in this case
16595 because it can have changed the buffer. */
16596 || !NILP (Vwindow_scroll_functions)
16597 || !just_this_one_p
16598 || MINI_WINDOW_P (w)
16599 || !(used_current_matrix_p
16600 = try_window_reusing_current_matrix (w)))
16601 try_window (window, startp, 0);
16603 /* If new fonts have been loaded (due to fontsets), give up. We
16604 have to start a new redisplay since we need to re-adjust glyph
16605 matrices. */
16606 if (f->fonts_changed)
16607 goto need_larger_matrices;
16609 /* If cursor did not appear assume that the middle of the window is
16610 in the first line of the window. Do it again with the next line.
16611 (Imagine a window of height 100, displaying two lines of height
16612 60. Moving back 50 from it->last_visible_y will end in the first
16613 line.) */
16614 if (w->cursor.vpos < 0)
16616 if (w->window_end_valid && PT >= Z - w->window_end_pos)
16618 clear_glyph_matrix (w->desired_matrix);
16619 move_it_by_lines (&it, 1);
16620 try_window (window, it.current.pos, 0);
16622 else if (PT < IT_CHARPOS (it))
16624 clear_glyph_matrix (w->desired_matrix);
16625 move_it_by_lines (&it, -1);
16626 try_window (window, it.current.pos, 0);
16628 else
16630 /* Not much we can do about it. */
16634 /* Consider the following case: Window starts at BEGV, there is
16635 invisible, intangible text at BEGV, so that display starts at
16636 some point START > BEGV. It can happen that we are called with
16637 PT somewhere between BEGV and START. Try to handle that case,
16638 and similar ones. */
16639 if (w->cursor.vpos < 0)
16641 /* First, try locating the proper glyph row for PT. */
16642 struct glyph_row *row =
16643 row_containing_pos (w, PT, w->current_matrix->rows, NULL, 0);
16645 /* Sometimes point is at the beginning of invisible text that is
16646 before the 1st character displayed in the row. In that case,
16647 row_containing_pos fails to find the row, because no glyphs
16648 with appropriate buffer positions are present in the row.
16649 Therefore, we next try to find the row which shows the 1st
16650 position after the invisible text. */
16651 if (!row)
16653 Lisp_Object val =
16654 get_char_property_and_overlay (make_number (PT), Qinvisible,
16655 Qnil, NULL);
16657 if (TEXT_PROP_MEANS_INVISIBLE (val))
16659 ptrdiff_t alt_pos;
16660 Lisp_Object invis_end =
16661 Fnext_single_char_property_change (make_number (PT), Qinvisible,
16662 Qnil, Qnil);
16664 if (NATNUMP (invis_end))
16665 alt_pos = XFASTINT (invis_end);
16666 else
16667 alt_pos = ZV;
16668 row = row_containing_pos (w, alt_pos, w->current_matrix->rows,
16669 NULL, 0);
16672 /* Finally, fall back on the first row of the window after the
16673 header line (if any). This is slightly better than not
16674 displaying the cursor at all. */
16675 if (!row)
16677 row = w->current_matrix->rows;
16678 if (row->mode_line_p)
16679 ++row;
16681 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
16684 if (!cursor_row_fully_visible_p (w, 0, 0))
16686 /* If vscroll is enabled, disable it and try again. */
16687 if (w->vscroll)
16689 w->vscroll = 0;
16690 clear_glyph_matrix (w->desired_matrix);
16691 goto recenter;
16694 /* Users who set scroll-conservatively to a large number want
16695 point just above/below the scroll margin. If we ended up
16696 with point's row partially visible, move the window start to
16697 make that row fully visible and out of the margin. */
16698 if (scroll_conservatively > SCROLL_LIMIT)
16700 int window_total_lines
16701 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) * frame_line_height;
16702 int margin =
16703 scroll_margin > 0
16704 ? min (scroll_margin, window_total_lines / 4)
16705 : 0;
16706 int move_down = w->cursor.vpos >= window_total_lines / 2;
16708 move_it_by_lines (&it, move_down ? margin + 1 : -(margin + 1));
16709 clear_glyph_matrix (w->desired_matrix);
16710 if (1 == try_window (window, it.current.pos,
16711 TRY_WINDOW_CHECK_MARGINS))
16712 goto done;
16715 /* If centering point failed to make the whole line visible,
16716 put point at the top instead. That has to make the whole line
16717 visible, if it can be done. */
16718 if (centering_position == 0)
16719 goto done;
16721 clear_glyph_matrix (w->desired_matrix);
16722 centering_position = 0;
16723 goto recenter;
16726 done:
16728 SET_TEXT_POS_FROM_MARKER (startp, w->start);
16729 w->start_at_line_beg = (CHARPOS (startp) == BEGV
16730 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n');
16732 /* Display the mode line, if we must. */
16733 if ((update_mode_line
16734 /* If window not full width, must redo its mode line
16735 if (a) the window to its side is being redone and
16736 (b) we do a frame-based redisplay. This is a consequence
16737 of how inverted lines are drawn in frame-based redisplay. */
16738 || (!just_this_one_p
16739 && !FRAME_WINDOW_P (f)
16740 && !WINDOW_FULL_WIDTH_P (w))
16741 /* Line number to display. */
16742 || w->base_line_pos > 0
16743 /* Column number is displayed and different from the one displayed. */
16744 || (w->column_number_displayed != -1
16745 && (w->column_number_displayed != current_column ())))
16746 /* This means that the window has a mode line. */
16747 && (WINDOW_WANTS_MODELINE_P (w)
16748 || WINDOW_WANTS_HEADER_LINE_P (w)))
16751 display_mode_lines (w);
16753 /* If mode line height has changed, arrange for a thorough
16754 immediate redisplay using the correct mode line height. */
16755 if (WINDOW_WANTS_MODELINE_P (w)
16756 && CURRENT_MODE_LINE_HEIGHT (w) != DESIRED_MODE_LINE_HEIGHT (w))
16758 f->fonts_changed = 1;
16759 w->mode_line_height = -1;
16760 MATRIX_MODE_LINE_ROW (w->current_matrix)->height
16761 = DESIRED_MODE_LINE_HEIGHT (w);
16764 /* If header line height has changed, arrange for a thorough
16765 immediate redisplay using the correct header line height. */
16766 if (WINDOW_WANTS_HEADER_LINE_P (w)
16767 && CURRENT_HEADER_LINE_HEIGHT (w) != DESIRED_HEADER_LINE_HEIGHT (w))
16769 f->fonts_changed = 1;
16770 w->header_line_height = -1;
16771 MATRIX_HEADER_LINE_ROW (w->current_matrix)->height
16772 = DESIRED_HEADER_LINE_HEIGHT (w);
16775 if (f->fonts_changed)
16776 goto need_larger_matrices;
16779 if (!line_number_displayed && w->base_line_pos != -1)
16781 w->base_line_pos = 0;
16782 w->base_line_number = 0;
16785 finish_menu_bars:
16787 /* When we reach a frame's selected window, redo the frame's menu bar. */
16788 if (update_mode_line
16789 && EQ (FRAME_SELECTED_WINDOW (f), window))
16791 int redisplay_menu_p = 0;
16793 if (FRAME_WINDOW_P (f))
16795 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
16796 || defined (HAVE_NS) || defined (USE_GTK)
16797 redisplay_menu_p = FRAME_EXTERNAL_MENU_BAR (f);
16798 #else
16799 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
16800 #endif
16802 else
16803 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
16805 if (redisplay_menu_p)
16806 display_menu_bar (w);
16808 #ifdef HAVE_WINDOW_SYSTEM
16809 if (FRAME_WINDOW_P (f))
16811 #if defined (USE_GTK) || defined (HAVE_NS)
16812 if (FRAME_EXTERNAL_TOOL_BAR (f))
16813 redisplay_tool_bar (f);
16814 #else
16815 if (WINDOWP (f->tool_bar_window)
16816 && (FRAME_TOOL_BAR_LINES (f) > 0
16817 || !NILP (Vauto_resize_tool_bars))
16818 && redisplay_tool_bar (f))
16819 ignore_mouse_drag_p = 1;
16820 #endif
16822 #endif
16825 #ifdef HAVE_WINDOW_SYSTEM
16826 if (FRAME_WINDOW_P (f)
16827 && update_window_fringes (w, (just_this_one_p
16828 || (!used_current_matrix_p && !overlay_arrow_seen)
16829 || w->pseudo_window_p)))
16831 update_begin (f);
16832 block_input ();
16833 if (draw_window_fringes (w, 1))
16835 if (WINDOW_RIGHT_DIVIDER_WIDTH (w))
16836 x_draw_right_divider (w);
16837 else
16838 x_draw_vertical_border (w);
16840 unblock_input ();
16841 update_end (f);
16844 if (WINDOW_BOTTOM_DIVIDER_WIDTH (w))
16845 x_draw_bottom_divider (w);
16846 #endif /* HAVE_WINDOW_SYSTEM */
16848 /* We go to this label, with fonts_changed set, if it is
16849 necessary to try again using larger glyph matrices.
16850 We have to redeem the scroll bar even in this case,
16851 because the loop in redisplay_internal expects that. */
16852 need_larger_matrices:
16854 finish_scroll_bars:
16856 if (WINDOW_HAS_VERTICAL_SCROLL_BAR (w) || WINDOW_HAS_HORIZONTAL_SCROLL_BAR (w))
16858 if (WINDOW_HAS_VERTICAL_SCROLL_BAR (w))
16859 /* Set the thumb's position and size. */
16860 set_vertical_scroll_bar (w);
16862 if (WINDOW_HAS_HORIZONTAL_SCROLL_BAR (w))
16863 /* Set the thumb's position and size. */
16864 set_horizontal_scroll_bar (w);
16866 /* Note that we actually used the scroll bar attached to this
16867 window, so it shouldn't be deleted at the end of redisplay. */
16868 if (FRAME_TERMINAL (f)->redeem_scroll_bar_hook)
16869 (*FRAME_TERMINAL (f)->redeem_scroll_bar_hook) (w);
16872 /* Restore current_buffer and value of point in it. The window
16873 update may have changed the buffer, so first make sure `opoint'
16874 is still valid (Bug#6177). */
16875 if (CHARPOS (opoint) < BEGV)
16876 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
16877 else if (CHARPOS (opoint) > ZV)
16878 TEMP_SET_PT_BOTH (Z, Z_BYTE);
16879 else
16880 TEMP_SET_PT_BOTH (CHARPOS (opoint), BYTEPOS (opoint));
16882 set_buffer_internal_1 (old);
16883 /* Avoid an abort in TEMP_SET_PT_BOTH if the buffer has become
16884 shorter. This can be caused by log truncation in *Messages*. */
16885 if (CHARPOS (lpoint) <= ZV)
16886 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
16888 unbind_to (count, Qnil);
16892 /* Build the complete desired matrix of WINDOW with a window start
16893 buffer position POS.
16895 Value is 1 if successful. It is zero if fonts were loaded during
16896 redisplay which makes re-adjusting glyph matrices necessary, and -1
16897 if point would appear in the scroll margins.
16898 (We check the former only if TRY_WINDOW_IGNORE_FONTS_CHANGE is
16899 unset in FLAGS, and the latter only if TRY_WINDOW_CHECK_MARGINS is
16900 set in FLAGS.) */
16903 try_window (Lisp_Object window, struct text_pos pos, int flags)
16905 struct window *w = XWINDOW (window);
16906 struct it it;
16907 struct glyph_row *last_text_row = NULL;
16908 struct frame *f = XFRAME (w->frame);
16909 int frame_line_height = default_line_pixel_height (w);
16911 /* Make POS the new window start. */
16912 set_marker_both (w->start, Qnil, CHARPOS (pos), BYTEPOS (pos));
16914 /* Mark cursor position as unknown. No overlay arrow seen. */
16915 w->cursor.vpos = -1;
16916 overlay_arrow_seen = 0;
16918 /* Initialize iterator and info to start at POS. */
16919 start_display (&it, w, pos);
16920 it.glyph_row->reversed_p = false;
16922 /* Display all lines of W. */
16923 while (it.current_y < it.last_visible_y)
16925 if (display_line (&it))
16926 last_text_row = it.glyph_row - 1;
16927 if (f->fonts_changed && !(flags & TRY_WINDOW_IGNORE_FONTS_CHANGE))
16928 return 0;
16931 /* Don't let the cursor end in the scroll margins. */
16932 if ((flags & TRY_WINDOW_CHECK_MARGINS)
16933 && !MINI_WINDOW_P (w))
16935 int this_scroll_margin;
16936 int window_total_lines
16937 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
16939 if (scroll_margin > 0)
16941 this_scroll_margin = min (scroll_margin, window_total_lines / 4);
16942 this_scroll_margin *= frame_line_height;
16944 else
16945 this_scroll_margin = 0;
16947 if ((w->cursor.y >= 0 /* not vscrolled */
16948 && w->cursor.y < this_scroll_margin
16949 && CHARPOS (pos) > BEGV
16950 && IT_CHARPOS (it) < ZV)
16951 /* rms: considering make_cursor_line_fully_visible_p here
16952 seems to give wrong results. We don't want to recenter
16953 when the last line is partly visible, we want to allow
16954 that case to be handled in the usual way. */
16955 || w->cursor.y > it.last_visible_y - this_scroll_margin - 1)
16957 w->cursor.vpos = -1;
16958 clear_glyph_matrix (w->desired_matrix);
16959 return -1;
16963 /* If bottom moved off end of frame, change mode line percentage. */
16964 if (w->window_end_pos <= 0 && Z != IT_CHARPOS (it))
16965 w->update_mode_line = 1;
16967 /* Set window_end_pos to the offset of the last character displayed
16968 on the window from the end of current_buffer. Set
16969 window_end_vpos to its row number. */
16970 if (last_text_row)
16972 eassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_text_row));
16973 adjust_window_ends (w, last_text_row, 0);
16974 eassert
16975 (MATRIX_ROW_DISPLAYS_TEXT_P (MATRIX_ROW (w->desired_matrix,
16976 w->window_end_vpos)));
16978 else
16980 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
16981 w->window_end_pos = Z - ZV;
16982 w->window_end_vpos = 0;
16985 /* But that is not valid info until redisplay finishes. */
16986 w->window_end_valid = 0;
16987 return 1;
16992 /************************************************************************
16993 Window redisplay reusing current matrix when buffer has not changed
16994 ************************************************************************/
16996 /* Try redisplay of window W showing an unchanged buffer with a
16997 different window start than the last time it was displayed by
16998 reusing its current matrix. Value is non-zero if successful.
16999 W->start is the new window start. */
17001 static int
17002 try_window_reusing_current_matrix (struct window *w)
17004 struct frame *f = XFRAME (w->frame);
17005 struct glyph_row *bottom_row;
17006 struct it it;
17007 struct run run;
17008 struct text_pos start, new_start;
17009 int nrows_scrolled, i;
17010 struct glyph_row *last_text_row;
17011 struct glyph_row *last_reused_text_row;
17012 struct glyph_row *start_row;
17013 int start_vpos, min_y, max_y;
17015 #ifdef GLYPH_DEBUG
17016 if (inhibit_try_window_reusing)
17017 return 0;
17018 #endif
17020 if (/* This function doesn't handle terminal frames. */
17021 !FRAME_WINDOW_P (f)
17022 /* Don't try to reuse the display if windows have been split
17023 or such. */
17024 || windows_or_buffers_changed
17025 || f->cursor_type_changed)
17026 return 0;
17028 /* Can't do this if showing trailing whitespace. */
17029 if (!NILP (Vshow_trailing_whitespace))
17030 return 0;
17032 /* If top-line visibility has changed, give up. */
17033 if (WINDOW_WANTS_HEADER_LINE_P (w)
17034 != MATRIX_HEADER_LINE_ROW (w->current_matrix)->mode_line_p)
17035 return 0;
17037 /* Give up if old or new display is scrolled vertically. We could
17038 make this function handle this, but right now it doesn't. */
17039 start_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
17040 if (w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row))
17041 return 0;
17043 /* The variable new_start now holds the new window start. The old
17044 start `start' can be determined from the current matrix. */
17045 SET_TEXT_POS_FROM_MARKER (new_start, w->start);
17046 start = start_row->minpos;
17047 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
17049 /* Clear the desired matrix for the display below. */
17050 clear_glyph_matrix (w->desired_matrix);
17052 if (CHARPOS (new_start) <= CHARPOS (start))
17054 /* Don't use this method if the display starts with an ellipsis
17055 displayed for invisible text. It's not easy to handle that case
17056 below, and it's certainly not worth the effort since this is
17057 not a frequent case. */
17058 if (in_ellipses_for_invisible_text_p (&start_row->start, w))
17059 return 0;
17061 IF_DEBUG (debug_method_add (w, "twu1"));
17063 /* Display up to a row that can be reused. The variable
17064 last_text_row is set to the last row displayed that displays
17065 text. Note that it.vpos == 0 if or if not there is a
17066 header-line; it's not the same as the MATRIX_ROW_VPOS! */
17067 start_display (&it, w, new_start);
17068 w->cursor.vpos = -1;
17069 last_text_row = last_reused_text_row = NULL;
17071 while (it.current_y < it.last_visible_y && !f->fonts_changed)
17073 /* If we have reached into the characters in the START row,
17074 that means the line boundaries have changed. So we
17075 can't start copying with the row START. Maybe it will
17076 work to start copying with the following row. */
17077 while (IT_CHARPOS (it) > CHARPOS (start))
17079 /* Advance to the next row as the "start". */
17080 start_row++;
17081 start = start_row->minpos;
17082 /* If there are no more rows to try, or just one, give up. */
17083 if (start_row == MATRIX_MODE_LINE_ROW (w->current_matrix) - 1
17084 || w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row)
17085 || CHARPOS (start) == ZV)
17087 clear_glyph_matrix (w->desired_matrix);
17088 return 0;
17091 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
17093 /* If we have reached alignment, we can copy the rest of the
17094 rows. */
17095 if (IT_CHARPOS (it) == CHARPOS (start)
17096 /* Don't accept "alignment" inside a display vector,
17097 since start_row could have started in the middle of
17098 that same display vector (thus their character
17099 positions match), and we have no way of telling if
17100 that is the case. */
17101 && it.current.dpvec_index < 0)
17102 break;
17104 it.glyph_row->reversed_p = false;
17105 if (display_line (&it))
17106 last_text_row = it.glyph_row - 1;
17110 /* A value of current_y < last_visible_y means that we stopped
17111 at the previous window start, which in turn means that we
17112 have at least one reusable row. */
17113 if (it.current_y < it.last_visible_y)
17115 struct glyph_row *row;
17117 /* IT.vpos always starts from 0; it counts text lines. */
17118 nrows_scrolled = it.vpos - (start_row - MATRIX_FIRST_TEXT_ROW (w->current_matrix));
17120 /* Find PT if not already found in the lines displayed. */
17121 if (w->cursor.vpos < 0)
17123 int dy = it.current_y - start_row->y;
17125 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
17126 row = row_containing_pos (w, PT, row, NULL, dy);
17127 if (row)
17128 set_cursor_from_row (w, row, w->current_matrix, 0, 0,
17129 dy, nrows_scrolled);
17130 else
17132 clear_glyph_matrix (w->desired_matrix);
17133 return 0;
17137 /* Scroll the display. Do it before the current matrix is
17138 changed. The problem here is that update has not yet
17139 run, i.e. part of the current matrix is not up to date.
17140 scroll_run_hook will clear the cursor, and use the
17141 current matrix to get the height of the row the cursor is
17142 in. */
17143 run.current_y = start_row->y;
17144 run.desired_y = it.current_y;
17145 run.height = it.last_visible_y - it.current_y;
17147 if (run.height > 0 && run.current_y != run.desired_y)
17149 update_begin (f);
17150 FRAME_RIF (f)->update_window_begin_hook (w);
17151 FRAME_RIF (f)->clear_window_mouse_face (w);
17152 FRAME_RIF (f)->scroll_run_hook (w, &run);
17153 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
17154 update_end (f);
17157 /* Shift current matrix down by nrows_scrolled lines. */
17158 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
17159 rotate_matrix (w->current_matrix,
17160 start_vpos,
17161 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
17162 nrows_scrolled);
17164 /* Disable lines that must be updated. */
17165 for (i = 0; i < nrows_scrolled; ++i)
17166 (start_row + i)->enabled_p = false;
17168 /* Re-compute Y positions. */
17169 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
17170 max_y = it.last_visible_y;
17171 for (row = start_row + nrows_scrolled;
17172 row < bottom_row;
17173 ++row)
17175 row->y = it.current_y;
17176 row->visible_height = row->height;
17178 if (row->y < min_y)
17179 row->visible_height -= min_y - row->y;
17180 if (row->y + row->height > max_y)
17181 row->visible_height -= row->y + row->height - max_y;
17182 if (row->fringe_bitmap_periodic_p)
17183 row->redraw_fringe_bitmaps_p = 1;
17185 it.current_y += row->height;
17187 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
17188 last_reused_text_row = row;
17189 if (MATRIX_ROW_BOTTOM_Y (row) >= it.last_visible_y)
17190 break;
17193 /* Disable lines in the current matrix which are now
17194 below the window. */
17195 for (++row; row < bottom_row; ++row)
17196 row->enabled_p = row->mode_line_p = 0;
17199 /* Update window_end_pos etc.; last_reused_text_row is the last
17200 reused row from the current matrix containing text, if any.
17201 The value of last_text_row is the last displayed line
17202 containing text. */
17203 if (last_reused_text_row)
17204 adjust_window_ends (w, last_reused_text_row, 1);
17205 else if (last_text_row)
17206 adjust_window_ends (w, last_text_row, 0);
17207 else
17209 /* This window must be completely empty. */
17210 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
17211 w->window_end_pos = Z - ZV;
17212 w->window_end_vpos = 0;
17214 w->window_end_valid = 0;
17216 /* Update hint: don't try scrolling again in update_window. */
17217 w->desired_matrix->no_scrolling_p = 1;
17219 #ifdef GLYPH_DEBUG
17220 debug_method_add (w, "try_window_reusing_current_matrix 1");
17221 #endif
17222 return 1;
17224 else if (CHARPOS (new_start) > CHARPOS (start))
17226 struct glyph_row *pt_row, *row;
17227 struct glyph_row *first_reusable_row;
17228 struct glyph_row *first_row_to_display;
17229 int dy;
17230 int yb = window_text_bottom_y (w);
17232 /* Find the row starting at new_start, if there is one. Don't
17233 reuse a partially visible line at the end. */
17234 first_reusable_row = start_row;
17235 while (first_reusable_row->enabled_p
17236 && MATRIX_ROW_BOTTOM_Y (first_reusable_row) < yb
17237 && (MATRIX_ROW_START_CHARPOS (first_reusable_row)
17238 < CHARPOS (new_start)))
17239 ++first_reusable_row;
17241 /* Give up if there is no row to reuse. */
17242 if (MATRIX_ROW_BOTTOM_Y (first_reusable_row) >= yb
17243 || !first_reusable_row->enabled_p
17244 || (MATRIX_ROW_START_CHARPOS (first_reusable_row)
17245 != CHARPOS (new_start)))
17246 return 0;
17248 /* We can reuse fully visible rows beginning with
17249 first_reusable_row to the end of the window. Set
17250 first_row_to_display to the first row that cannot be reused.
17251 Set pt_row to the row containing point, if there is any. */
17252 pt_row = NULL;
17253 for (first_row_to_display = first_reusable_row;
17254 MATRIX_ROW_BOTTOM_Y (first_row_to_display) < yb;
17255 ++first_row_to_display)
17257 if (PT >= MATRIX_ROW_START_CHARPOS (first_row_to_display)
17258 && (PT < MATRIX_ROW_END_CHARPOS (first_row_to_display)
17259 || (PT == MATRIX_ROW_END_CHARPOS (first_row_to_display)
17260 && first_row_to_display->ends_at_zv_p
17261 && pt_row == NULL)))
17262 pt_row = first_row_to_display;
17265 /* Start displaying at the start of first_row_to_display. */
17266 eassert (first_row_to_display->y < yb);
17267 init_to_row_start (&it, w, first_row_to_display);
17269 nrows_scrolled = (MATRIX_ROW_VPOS (first_reusable_row, w->current_matrix)
17270 - start_vpos);
17271 it.vpos = (MATRIX_ROW_VPOS (first_row_to_display, w->current_matrix)
17272 - nrows_scrolled);
17273 it.current_y = (first_row_to_display->y - first_reusable_row->y
17274 + WINDOW_HEADER_LINE_HEIGHT (w));
17276 /* Display lines beginning with first_row_to_display in the
17277 desired matrix. Set last_text_row to the last row displayed
17278 that displays text. */
17279 it.glyph_row = MATRIX_ROW (w->desired_matrix, it.vpos);
17280 if (pt_row == NULL)
17281 w->cursor.vpos = -1;
17282 last_text_row = NULL;
17283 while (it.current_y < it.last_visible_y && !f->fonts_changed)
17284 if (display_line (&it))
17285 last_text_row = it.glyph_row - 1;
17287 /* If point is in a reused row, adjust y and vpos of the cursor
17288 position. */
17289 if (pt_row)
17291 w->cursor.vpos -= nrows_scrolled;
17292 w->cursor.y -= first_reusable_row->y - start_row->y;
17295 /* Give up if point isn't in a row displayed or reused. (This
17296 also handles the case where w->cursor.vpos < nrows_scrolled
17297 after the calls to display_line, which can happen with scroll
17298 margins. See bug#1295.) */
17299 if (w->cursor.vpos < 0)
17301 clear_glyph_matrix (w->desired_matrix);
17302 return 0;
17305 /* Scroll the display. */
17306 run.current_y = first_reusable_row->y;
17307 run.desired_y = WINDOW_HEADER_LINE_HEIGHT (w);
17308 run.height = it.last_visible_y - run.current_y;
17309 dy = run.current_y - run.desired_y;
17311 if (run.height)
17313 update_begin (f);
17314 FRAME_RIF (f)->update_window_begin_hook (w);
17315 FRAME_RIF (f)->clear_window_mouse_face (w);
17316 FRAME_RIF (f)->scroll_run_hook (w, &run);
17317 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
17318 update_end (f);
17321 /* Adjust Y positions of reused rows. */
17322 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
17323 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
17324 max_y = it.last_visible_y;
17325 for (row = first_reusable_row; row < first_row_to_display; ++row)
17327 row->y -= dy;
17328 row->visible_height = row->height;
17329 if (row->y < min_y)
17330 row->visible_height -= min_y - row->y;
17331 if (row->y + row->height > max_y)
17332 row->visible_height -= row->y + row->height - max_y;
17333 if (row->fringe_bitmap_periodic_p)
17334 row->redraw_fringe_bitmaps_p = 1;
17337 /* Scroll the current matrix. */
17338 eassert (nrows_scrolled > 0);
17339 rotate_matrix (w->current_matrix,
17340 start_vpos,
17341 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
17342 -nrows_scrolled);
17344 /* Disable rows not reused. */
17345 for (row -= nrows_scrolled; row < bottom_row; ++row)
17346 row->enabled_p = false;
17348 /* Point may have moved to a different line, so we cannot assume that
17349 the previous cursor position is valid; locate the correct row. */
17350 if (pt_row)
17352 for (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
17353 row < bottom_row
17354 && PT >= MATRIX_ROW_END_CHARPOS (row)
17355 && !row->ends_at_zv_p;
17356 row++)
17358 w->cursor.vpos++;
17359 w->cursor.y = row->y;
17361 if (row < bottom_row)
17363 /* Can't simply scan the row for point with
17364 bidi-reordered glyph rows. Let set_cursor_from_row
17365 figure out where to put the cursor, and if it fails,
17366 give up. */
17367 if (!NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering)))
17369 if (!set_cursor_from_row (w, row, w->current_matrix,
17370 0, 0, 0, 0))
17372 clear_glyph_matrix (w->desired_matrix);
17373 return 0;
17376 else
17378 struct glyph *glyph = row->glyphs[TEXT_AREA] + w->cursor.hpos;
17379 struct glyph *end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
17381 for (; glyph < end
17382 && (!BUFFERP (glyph->object)
17383 || glyph->charpos < PT);
17384 glyph++)
17386 w->cursor.hpos++;
17387 w->cursor.x += glyph->pixel_width;
17393 /* Adjust window end. A null value of last_text_row means that
17394 the window end is in reused rows which in turn means that
17395 only its vpos can have changed. */
17396 if (last_text_row)
17397 adjust_window_ends (w, last_text_row, 0);
17398 else
17399 w->window_end_vpos -= nrows_scrolled;
17401 w->window_end_valid = 0;
17402 w->desired_matrix->no_scrolling_p = 1;
17404 #ifdef GLYPH_DEBUG
17405 debug_method_add (w, "try_window_reusing_current_matrix 2");
17406 #endif
17407 return 1;
17410 return 0;
17415 /************************************************************************
17416 Window redisplay reusing current matrix when buffer has changed
17417 ************************************************************************/
17419 static struct glyph_row *find_last_unchanged_at_beg_row (struct window *);
17420 static struct glyph_row *find_first_unchanged_at_end_row (struct window *,
17421 ptrdiff_t *, ptrdiff_t *);
17422 static struct glyph_row *
17423 find_last_row_displaying_text (struct glyph_matrix *, struct it *,
17424 struct glyph_row *);
17427 /* Return the last row in MATRIX displaying text. If row START is
17428 non-null, start searching with that row. IT gives the dimensions
17429 of the display. Value is null if matrix is empty; otherwise it is
17430 a pointer to the row found. */
17432 static struct glyph_row *
17433 find_last_row_displaying_text (struct glyph_matrix *matrix, struct it *it,
17434 struct glyph_row *start)
17436 struct glyph_row *row, *row_found;
17438 /* Set row_found to the last row in IT->w's current matrix
17439 displaying text. The loop looks funny but think of partially
17440 visible lines. */
17441 row_found = NULL;
17442 row = start ? start : MATRIX_FIRST_TEXT_ROW (matrix);
17443 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
17445 eassert (row->enabled_p);
17446 row_found = row;
17447 if (MATRIX_ROW_BOTTOM_Y (row) >= it->last_visible_y)
17448 break;
17449 ++row;
17452 return row_found;
17456 /* Return the last row in the current matrix of W that is not affected
17457 by changes at the start of current_buffer that occurred since W's
17458 current matrix was built. Value is null if no such row exists.
17460 BEG_UNCHANGED us the number of characters unchanged at the start of
17461 current_buffer. BEG + BEG_UNCHANGED is the buffer position of the
17462 first changed character in current_buffer. Characters at positions <
17463 BEG + BEG_UNCHANGED are at the same buffer positions as they were
17464 when the current matrix was built. */
17466 static struct glyph_row *
17467 find_last_unchanged_at_beg_row (struct window *w)
17469 ptrdiff_t first_changed_pos = BEG + BEG_UNCHANGED;
17470 struct glyph_row *row;
17471 struct glyph_row *row_found = NULL;
17472 int yb = window_text_bottom_y (w);
17474 /* Find the last row displaying unchanged text. */
17475 for (row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
17476 MATRIX_ROW_DISPLAYS_TEXT_P (row)
17477 && MATRIX_ROW_START_CHARPOS (row) < first_changed_pos;
17478 ++row)
17480 if (/* If row ends before first_changed_pos, it is unchanged,
17481 except in some case. */
17482 MATRIX_ROW_END_CHARPOS (row) <= first_changed_pos
17483 /* When row ends in ZV and we write at ZV it is not
17484 unchanged. */
17485 && !row->ends_at_zv_p
17486 /* When first_changed_pos is the end of a continued line,
17487 row is not unchanged because it may be no longer
17488 continued. */
17489 && !(MATRIX_ROW_END_CHARPOS (row) == first_changed_pos
17490 && (row->continued_p
17491 || row->exact_window_width_line_p))
17492 /* If ROW->end is beyond ZV, then ROW->end is outdated and
17493 needs to be recomputed, so don't consider this row as
17494 unchanged. This happens when the last line was
17495 bidi-reordered and was killed immediately before this
17496 redisplay cycle. In that case, ROW->end stores the
17497 buffer position of the first visual-order character of
17498 the killed text, which is now beyond ZV. */
17499 && CHARPOS (row->end.pos) <= ZV)
17500 row_found = row;
17502 /* Stop if last visible row. */
17503 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
17504 break;
17507 return row_found;
17511 /* Find the first glyph row in the current matrix of W that is not
17512 affected by changes at the end of current_buffer since the
17513 time W's current matrix was built.
17515 Return in *DELTA the number of chars by which buffer positions in
17516 unchanged text at the end of current_buffer must be adjusted.
17518 Return in *DELTA_BYTES the corresponding number of bytes.
17520 Value is null if no such row exists, i.e. all rows are affected by
17521 changes. */
17523 static struct glyph_row *
17524 find_first_unchanged_at_end_row (struct window *w,
17525 ptrdiff_t *delta, ptrdiff_t *delta_bytes)
17527 struct glyph_row *row;
17528 struct glyph_row *row_found = NULL;
17530 *delta = *delta_bytes = 0;
17532 /* Display must not have been paused, otherwise the current matrix
17533 is not up to date. */
17534 eassert (w->window_end_valid);
17536 /* A value of window_end_pos >= END_UNCHANGED means that the window
17537 end is in the range of changed text. If so, there is no
17538 unchanged row at the end of W's current matrix. */
17539 if (w->window_end_pos >= END_UNCHANGED)
17540 return NULL;
17542 /* Set row to the last row in W's current matrix displaying text. */
17543 row = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
17545 /* If matrix is entirely empty, no unchanged row exists. */
17546 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
17548 /* The value of row is the last glyph row in the matrix having a
17549 meaningful buffer position in it. The end position of row
17550 corresponds to window_end_pos. This allows us to translate
17551 buffer positions in the current matrix to current buffer
17552 positions for characters not in changed text. */
17553 ptrdiff_t Z_old =
17554 MATRIX_ROW_END_CHARPOS (row) + w->window_end_pos;
17555 ptrdiff_t Z_BYTE_old =
17556 MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
17557 ptrdiff_t last_unchanged_pos, last_unchanged_pos_old;
17558 struct glyph_row *first_text_row
17559 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
17561 *delta = Z - Z_old;
17562 *delta_bytes = Z_BYTE - Z_BYTE_old;
17564 /* Set last_unchanged_pos to the buffer position of the last
17565 character in the buffer that has not been changed. Z is the
17566 index + 1 of the last character in current_buffer, i.e. by
17567 subtracting END_UNCHANGED we get the index of the last
17568 unchanged character, and we have to add BEG to get its buffer
17569 position. */
17570 last_unchanged_pos = Z - END_UNCHANGED + BEG;
17571 last_unchanged_pos_old = last_unchanged_pos - *delta;
17573 /* Search backward from ROW for a row displaying a line that
17574 starts at a minimum position >= last_unchanged_pos_old. */
17575 for (; row > first_text_row; --row)
17577 /* This used to abort, but it can happen.
17578 It is ok to just stop the search instead here. KFS. */
17579 if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
17580 break;
17582 if (MATRIX_ROW_START_CHARPOS (row) >= last_unchanged_pos_old)
17583 row_found = row;
17587 eassert (!row_found || MATRIX_ROW_DISPLAYS_TEXT_P (row_found));
17589 return row_found;
17593 /* Make sure that glyph rows in the current matrix of window W
17594 reference the same glyph memory as corresponding rows in the
17595 frame's frame matrix. This function is called after scrolling W's
17596 current matrix on a terminal frame in try_window_id and
17597 try_window_reusing_current_matrix. */
17599 static void
17600 sync_frame_with_window_matrix_rows (struct window *w)
17602 struct frame *f = XFRAME (w->frame);
17603 struct glyph_row *window_row, *window_row_end, *frame_row;
17605 /* Preconditions: W must be a leaf window and full-width. Its frame
17606 must have a frame matrix. */
17607 eassert (BUFFERP (w->contents));
17608 eassert (WINDOW_FULL_WIDTH_P (w));
17609 eassert (!FRAME_WINDOW_P (f));
17611 /* If W is a full-width window, glyph pointers in W's current matrix
17612 have, by definition, to be the same as glyph pointers in the
17613 corresponding frame matrix. Note that frame matrices have no
17614 marginal areas (see build_frame_matrix). */
17615 window_row = w->current_matrix->rows;
17616 window_row_end = window_row + w->current_matrix->nrows;
17617 frame_row = f->current_matrix->rows + WINDOW_TOP_EDGE_LINE (w);
17618 while (window_row < window_row_end)
17620 struct glyph *start = window_row->glyphs[LEFT_MARGIN_AREA];
17621 struct glyph *end = window_row->glyphs[LAST_AREA];
17623 frame_row->glyphs[LEFT_MARGIN_AREA] = start;
17624 frame_row->glyphs[TEXT_AREA] = start;
17625 frame_row->glyphs[RIGHT_MARGIN_AREA] = end;
17626 frame_row->glyphs[LAST_AREA] = end;
17628 /* Disable frame rows whose corresponding window rows have
17629 been disabled in try_window_id. */
17630 if (!window_row->enabled_p)
17631 frame_row->enabled_p = false;
17633 ++window_row, ++frame_row;
17638 /* Find the glyph row in window W containing CHARPOS. Consider all
17639 rows between START and END (not inclusive). END null means search
17640 all rows to the end of the display area of W. Value is the row
17641 containing CHARPOS or null. */
17643 struct glyph_row *
17644 row_containing_pos (struct window *w, ptrdiff_t charpos,
17645 struct glyph_row *start, struct glyph_row *end, int dy)
17647 struct glyph_row *row = start;
17648 struct glyph_row *best_row = NULL;
17649 ptrdiff_t mindif = BUF_ZV (XBUFFER (w->contents)) + 1;
17650 int last_y;
17652 /* If we happen to start on a header-line, skip that. */
17653 if (row->mode_line_p)
17654 ++row;
17656 if ((end && row >= end) || !row->enabled_p)
17657 return NULL;
17659 last_y = window_text_bottom_y (w) - dy;
17661 while (1)
17663 /* Give up if we have gone too far. */
17664 if (end && row >= end)
17665 return NULL;
17666 /* This formerly returned if they were equal.
17667 I think that both quantities are of a "last plus one" type;
17668 if so, when they are equal, the row is within the screen. -- rms. */
17669 if (MATRIX_ROW_BOTTOM_Y (row) > last_y)
17670 return NULL;
17672 /* If it is in this row, return this row. */
17673 if (! (MATRIX_ROW_END_CHARPOS (row) < charpos
17674 || (MATRIX_ROW_END_CHARPOS (row) == charpos
17675 /* The end position of a row equals the start
17676 position of the next row. If CHARPOS is there, we
17677 would rather consider it displayed in the next
17678 line, except when this line ends in ZV. */
17679 && !row_for_charpos_p (row, charpos)))
17680 && charpos >= MATRIX_ROW_START_CHARPOS (row))
17682 struct glyph *g;
17684 if (NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering))
17685 || (!best_row && !row->continued_p))
17686 return row;
17687 /* In bidi-reordered rows, there could be several rows whose
17688 edges surround CHARPOS, all of these rows belonging to
17689 the same continued line. We need to find the row which
17690 fits CHARPOS the best. */
17691 for (g = row->glyphs[TEXT_AREA];
17692 g < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
17693 g++)
17695 if (!STRINGP (g->object))
17697 if (g->charpos > 0 && eabs (g->charpos - charpos) < mindif)
17699 mindif = eabs (g->charpos - charpos);
17700 best_row = row;
17701 /* Exact match always wins. */
17702 if (mindif == 0)
17703 return best_row;
17708 else if (best_row && !row->continued_p)
17709 return best_row;
17710 ++row;
17715 /* Try to redisplay window W by reusing its existing display. W's
17716 current matrix must be up to date when this function is called,
17717 i.e. window_end_valid must be nonzero.
17719 Value is
17721 >= 1 if successful, i.e. display has been updated
17722 specifically:
17723 1 means the changes were in front of a newline that precedes
17724 the window start, and the whole current matrix was reused
17725 2 means the changes were after the last position displayed
17726 in the window, and the whole current matrix was reused
17727 3 means portions of the current matrix were reused, while
17728 some of the screen lines were redrawn
17729 -1 if redisplay with same window start is known not to succeed
17730 0 if otherwise unsuccessful
17732 The following steps are performed:
17734 1. Find the last row in the current matrix of W that is not
17735 affected by changes at the start of current_buffer. If no such row
17736 is found, give up.
17738 2. Find the first row in W's current matrix that is not affected by
17739 changes at the end of current_buffer. Maybe there is no such row.
17741 3. Display lines beginning with the row + 1 found in step 1 to the
17742 row found in step 2 or, if step 2 didn't find a row, to the end of
17743 the window.
17745 4. If cursor is not known to appear on the window, give up.
17747 5. If display stopped at the row found in step 2, scroll the
17748 display and current matrix as needed.
17750 6. Maybe display some lines at the end of W, if we must. This can
17751 happen under various circumstances, like a partially visible line
17752 becoming fully visible, or because newly displayed lines are displayed
17753 in smaller font sizes.
17755 7. Update W's window end information. */
17757 static int
17758 try_window_id (struct window *w)
17760 struct frame *f = XFRAME (w->frame);
17761 struct glyph_matrix *current_matrix = w->current_matrix;
17762 struct glyph_matrix *desired_matrix = w->desired_matrix;
17763 struct glyph_row *last_unchanged_at_beg_row;
17764 struct glyph_row *first_unchanged_at_end_row;
17765 struct glyph_row *row;
17766 struct glyph_row *bottom_row;
17767 int bottom_vpos;
17768 struct it it;
17769 ptrdiff_t delta = 0, delta_bytes = 0, stop_pos;
17770 int dvpos, dy;
17771 struct text_pos start_pos;
17772 struct run run;
17773 int first_unchanged_at_end_vpos = 0;
17774 struct glyph_row *last_text_row, *last_text_row_at_end;
17775 struct text_pos start;
17776 ptrdiff_t first_changed_charpos, last_changed_charpos;
17778 #ifdef GLYPH_DEBUG
17779 if (inhibit_try_window_id)
17780 return 0;
17781 #endif
17783 /* This is handy for debugging. */
17784 #if 0
17785 #define GIVE_UP(X) \
17786 do { \
17787 fprintf (stderr, "try_window_id give up %d\n", (X)); \
17788 return 0; \
17789 } while (0)
17790 #else
17791 #define GIVE_UP(X) return 0
17792 #endif
17794 SET_TEXT_POS_FROM_MARKER (start, w->start);
17796 /* Don't use this for mini-windows because these can show
17797 messages and mini-buffers, and we don't handle that here. */
17798 if (MINI_WINDOW_P (w))
17799 GIVE_UP (1);
17801 /* This flag is used to prevent redisplay optimizations. */
17802 if (windows_or_buffers_changed || f->cursor_type_changed)
17803 GIVE_UP (2);
17805 /* This function's optimizations cannot be used if overlays have
17806 changed in the buffer displayed by the window, so give up if they
17807 have. */
17808 if (w->last_overlay_modified != OVERLAY_MODIFF)
17809 GIVE_UP (21);
17811 /* Verify that narrowing has not changed.
17812 Also verify that we were not told to prevent redisplay optimizations.
17813 It would be nice to further
17814 reduce the number of cases where this prevents try_window_id. */
17815 if (current_buffer->clip_changed
17816 || current_buffer->prevent_redisplay_optimizations_p)
17817 GIVE_UP (3);
17819 /* Window must either use window-based redisplay or be full width. */
17820 if (!FRAME_WINDOW_P (f)
17821 && (!FRAME_LINE_INS_DEL_OK (f)
17822 || !WINDOW_FULL_WIDTH_P (w)))
17823 GIVE_UP (4);
17825 /* Give up if point is known NOT to appear in W. */
17826 if (PT < CHARPOS (start))
17827 GIVE_UP (5);
17829 /* Another way to prevent redisplay optimizations. */
17830 if (w->last_modified == 0)
17831 GIVE_UP (6);
17833 /* Verify that window is not hscrolled. */
17834 if (w->hscroll != 0)
17835 GIVE_UP (7);
17837 /* Verify that display wasn't paused. */
17838 if (!w->window_end_valid)
17839 GIVE_UP (8);
17841 /* Likewise if highlighting trailing whitespace. */
17842 if (!NILP (Vshow_trailing_whitespace))
17843 GIVE_UP (11);
17845 /* Can't use this if overlay arrow position and/or string have
17846 changed. */
17847 if (overlay_arrows_changed_p ())
17848 GIVE_UP (12);
17850 /* When word-wrap is on, adding a space to the first word of a
17851 wrapped line can change the wrap position, altering the line
17852 above it. It might be worthwhile to handle this more
17853 intelligently, but for now just redisplay from scratch. */
17854 if (!NILP (BVAR (XBUFFER (w->contents), word_wrap)))
17855 GIVE_UP (21);
17857 /* Under bidi reordering, adding or deleting a character in the
17858 beginning of a paragraph, before the first strong directional
17859 character, can change the base direction of the paragraph (unless
17860 the buffer specifies a fixed paragraph direction), which will
17861 require to redisplay the whole paragraph. It might be worthwhile
17862 to find the paragraph limits and widen the range of redisplayed
17863 lines to that, but for now just give up this optimization and
17864 redisplay from scratch. */
17865 if (!NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering))
17866 && NILP (BVAR (XBUFFER (w->contents), bidi_paragraph_direction)))
17867 GIVE_UP (22);
17869 /* Make sure beg_unchanged and end_unchanged are up to date. Do it
17870 only if buffer has really changed. The reason is that the gap is
17871 initially at Z for freshly visited files. The code below would
17872 set end_unchanged to 0 in that case. */
17873 if (MODIFF > SAVE_MODIFF
17874 /* This seems to happen sometimes after saving a buffer. */
17875 || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
17877 if (GPT - BEG < BEG_UNCHANGED)
17878 BEG_UNCHANGED = GPT - BEG;
17879 if (Z - GPT < END_UNCHANGED)
17880 END_UNCHANGED = Z - GPT;
17883 /* The position of the first and last character that has been changed. */
17884 first_changed_charpos = BEG + BEG_UNCHANGED;
17885 last_changed_charpos = Z - END_UNCHANGED;
17887 /* If window starts after a line end, and the last change is in
17888 front of that newline, then changes don't affect the display.
17889 This case happens with stealth-fontification. Note that although
17890 the display is unchanged, glyph positions in the matrix have to
17891 be adjusted, of course. */
17892 row = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
17893 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
17894 && ((last_changed_charpos < CHARPOS (start)
17895 && CHARPOS (start) == BEGV)
17896 || (last_changed_charpos < CHARPOS (start) - 1
17897 && FETCH_BYTE (BYTEPOS (start) - 1) == '\n')))
17899 ptrdiff_t Z_old, Z_delta, Z_BYTE_old, Z_delta_bytes;
17900 struct glyph_row *r0;
17902 /* Compute how many chars/bytes have been added to or removed
17903 from the buffer. */
17904 Z_old = MATRIX_ROW_END_CHARPOS (row) + w->window_end_pos;
17905 Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
17906 Z_delta = Z - Z_old;
17907 Z_delta_bytes = Z_BYTE - Z_BYTE_old;
17909 /* Give up if PT is not in the window. Note that it already has
17910 been checked at the start of try_window_id that PT is not in
17911 front of the window start. */
17912 if (PT >= MATRIX_ROW_END_CHARPOS (row) + Z_delta)
17913 GIVE_UP (13);
17915 /* If window start is unchanged, we can reuse the whole matrix
17916 as is, after adjusting glyph positions. No need to compute
17917 the window end again, since its offset from Z hasn't changed. */
17918 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
17919 if (CHARPOS (start) == MATRIX_ROW_START_CHARPOS (r0) + Z_delta
17920 && BYTEPOS (start) == MATRIX_ROW_START_BYTEPOS (r0) + Z_delta_bytes
17921 /* PT must not be in a partially visible line. */
17922 && !(PT >= MATRIX_ROW_START_CHARPOS (row) + Z_delta
17923 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
17925 /* Adjust positions in the glyph matrix. */
17926 if (Z_delta || Z_delta_bytes)
17928 struct glyph_row *r1
17929 = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
17930 increment_matrix_positions (w->current_matrix,
17931 MATRIX_ROW_VPOS (r0, current_matrix),
17932 MATRIX_ROW_VPOS (r1, current_matrix),
17933 Z_delta, Z_delta_bytes);
17936 /* Set the cursor. */
17937 row = row_containing_pos (w, PT, r0, NULL, 0);
17938 if (row)
17939 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
17940 return 1;
17944 /* Handle the case that changes are all below what is displayed in
17945 the window, and that PT is in the window. This shortcut cannot
17946 be taken if ZV is visible in the window, and text has been added
17947 there that is visible in the window. */
17948 if (first_changed_charpos >= MATRIX_ROW_END_CHARPOS (row)
17949 /* ZV is not visible in the window, or there are no
17950 changes at ZV, actually. */
17951 && (current_matrix->zv > MATRIX_ROW_END_CHARPOS (row)
17952 || first_changed_charpos == last_changed_charpos))
17954 struct glyph_row *r0;
17956 /* Give up if PT is not in the window. Note that it already has
17957 been checked at the start of try_window_id that PT is not in
17958 front of the window start. */
17959 if (PT >= MATRIX_ROW_END_CHARPOS (row))
17960 GIVE_UP (14);
17962 /* If window start is unchanged, we can reuse the whole matrix
17963 as is, without changing glyph positions since no text has
17964 been added/removed in front of the window end. */
17965 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
17966 if (TEXT_POS_EQUAL_P (start, r0->minpos)
17967 /* PT must not be in a partially visible line. */
17968 && !(PT >= MATRIX_ROW_START_CHARPOS (row)
17969 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
17971 /* We have to compute the window end anew since text
17972 could have been added/removed after it. */
17973 w->window_end_pos = Z - MATRIX_ROW_END_CHARPOS (row);
17974 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
17976 /* Set the cursor. */
17977 row = row_containing_pos (w, PT, r0, NULL, 0);
17978 if (row)
17979 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
17980 return 2;
17984 /* Give up if window start is in the changed area.
17986 The condition used to read
17988 (BEG_UNCHANGED + END_UNCHANGED != Z - BEG && ...)
17990 but why that was tested escapes me at the moment. */
17991 if (CHARPOS (start) >= first_changed_charpos
17992 && CHARPOS (start) <= last_changed_charpos)
17993 GIVE_UP (15);
17995 /* Check that window start agrees with the start of the first glyph
17996 row in its current matrix. Check this after we know the window
17997 start is not in changed text, otherwise positions would not be
17998 comparable. */
17999 row = MATRIX_FIRST_TEXT_ROW (current_matrix);
18000 if (!TEXT_POS_EQUAL_P (start, row->minpos))
18001 GIVE_UP (16);
18003 /* Give up if the window ends in strings. Overlay strings
18004 at the end are difficult to handle, so don't try. */
18005 row = MATRIX_ROW (current_matrix, w->window_end_vpos);
18006 if (MATRIX_ROW_START_CHARPOS (row) == MATRIX_ROW_END_CHARPOS (row))
18007 GIVE_UP (20);
18009 /* Compute the position at which we have to start displaying new
18010 lines. Some of the lines at the top of the window might be
18011 reusable because they are not displaying changed text. Find the
18012 last row in W's current matrix not affected by changes at the
18013 start of current_buffer. Value is null if changes start in the
18014 first line of window. */
18015 last_unchanged_at_beg_row = find_last_unchanged_at_beg_row (w);
18016 if (last_unchanged_at_beg_row)
18018 /* Avoid starting to display in the middle of a character, a TAB
18019 for instance. This is easier than to set up the iterator
18020 exactly, and it's not a frequent case, so the additional
18021 effort wouldn't really pay off. */
18022 while ((MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row)
18023 || last_unchanged_at_beg_row->ends_in_newline_from_string_p)
18024 && last_unchanged_at_beg_row > w->current_matrix->rows)
18025 --last_unchanged_at_beg_row;
18027 if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row))
18028 GIVE_UP (17);
18030 if (init_to_row_end (&it, w, last_unchanged_at_beg_row) == 0)
18031 GIVE_UP (18);
18032 start_pos = it.current.pos;
18034 /* Start displaying new lines in the desired matrix at the same
18035 vpos we would use in the current matrix, i.e. below
18036 last_unchanged_at_beg_row. */
18037 it.vpos = 1 + MATRIX_ROW_VPOS (last_unchanged_at_beg_row,
18038 current_matrix);
18039 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
18040 it.current_y = MATRIX_ROW_BOTTOM_Y (last_unchanged_at_beg_row);
18042 eassert (it.hpos == 0 && it.current_x == 0);
18044 else
18046 /* There are no reusable lines at the start of the window.
18047 Start displaying in the first text line. */
18048 start_display (&it, w, start);
18049 it.vpos = it.first_vpos;
18050 start_pos = it.current.pos;
18053 /* Find the first row that is not affected by changes at the end of
18054 the buffer. Value will be null if there is no unchanged row, in
18055 which case we must redisplay to the end of the window. delta
18056 will be set to the value by which buffer positions beginning with
18057 first_unchanged_at_end_row have to be adjusted due to text
18058 changes. */
18059 first_unchanged_at_end_row
18060 = find_first_unchanged_at_end_row (w, &delta, &delta_bytes);
18061 IF_DEBUG (debug_delta = delta);
18062 IF_DEBUG (debug_delta_bytes = delta_bytes);
18064 /* Set stop_pos to the buffer position up to which we will have to
18065 display new lines. If first_unchanged_at_end_row != NULL, this
18066 is the buffer position of the start of the line displayed in that
18067 row. For first_unchanged_at_end_row == NULL, use 0 to indicate
18068 that we don't stop at a buffer position. */
18069 stop_pos = 0;
18070 if (first_unchanged_at_end_row)
18072 eassert (last_unchanged_at_beg_row == NULL
18073 || first_unchanged_at_end_row >= last_unchanged_at_beg_row);
18075 /* If this is a continuation line, move forward to the next one
18076 that isn't. Changes in lines above affect this line.
18077 Caution: this may move first_unchanged_at_end_row to a row
18078 not displaying text. */
18079 while (MATRIX_ROW_CONTINUATION_LINE_P (first_unchanged_at_end_row)
18080 && MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
18081 && (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
18082 < it.last_visible_y))
18083 ++first_unchanged_at_end_row;
18085 if (!MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
18086 || (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
18087 >= it.last_visible_y))
18088 first_unchanged_at_end_row = NULL;
18089 else
18091 stop_pos = (MATRIX_ROW_START_CHARPOS (first_unchanged_at_end_row)
18092 + delta);
18093 first_unchanged_at_end_vpos
18094 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, current_matrix);
18095 eassert (stop_pos >= Z - END_UNCHANGED);
18098 else if (last_unchanged_at_beg_row == NULL)
18099 GIVE_UP (19);
18102 #ifdef GLYPH_DEBUG
18104 /* Either there is no unchanged row at the end, or the one we have
18105 now displays text. This is a necessary condition for the window
18106 end pos calculation at the end of this function. */
18107 eassert (first_unchanged_at_end_row == NULL
18108 || MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
18110 debug_last_unchanged_at_beg_vpos
18111 = (last_unchanged_at_beg_row
18112 ? MATRIX_ROW_VPOS (last_unchanged_at_beg_row, current_matrix)
18113 : -1);
18114 debug_first_unchanged_at_end_vpos = first_unchanged_at_end_vpos;
18116 #endif /* GLYPH_DEBUG */
18119 /* Display new lines. Set last_text_row to the last new line
18120 displayed which has text on it, i.e. might end up as being the
18121 line where the window_end_vpos is. */
18122 w->cursor.vpos = -1;
18123 last_text_row = NULL;
18124 overlay_arrow_seen = 0;
18125 if (it.current_y < it.last_visible_y
18126 && !f->fonts_changed
18127 && (first_unchanged_at_end_row == NULL
18128 || IT_CHARPOS (it) < stop_pos))
18129 it.glyph_row->reversed_p = false;
18130 while (it.current_y < it.last_visible_y
18131 && !f->fonts_changed
18132 && (first_unchanged_at_end_row == NULL
18133 || IT_CHARPOS (it) < stop_pos))
18135 if (display_line (&it))
18136 last_text_row = it.glyph_row - 1;
18139 if (f->fonts_changed)
18140 return -1;
18142 /* The redisplay iterations in display_line above could have
18143 triggered font-lock, which could have done something that
18144 invalidates IT->w window's end-point information, on which we
18145 rely below. E.g., one package, which will remain unnamed, used
18146 to install a font-lock-fontify-region-function that called
18147 bury-buffer, whose side effect is to switch the buffer displayed
18148 by IT->w, and that predictably resets IT->w's window_end_valid
18149 flag, which we already tested at the entry to this function.
18150 Amply punish such packages/modes by giving up on this
18151 optimization in those cases. */
18152 if (!w->window_end_valid)
18154 clear_glyph_matrix (w->desired_matrix);
18155 return -1;
18158 /* Compute differences in buffer positions, y-positions etc. for
18159 lines reused at the bottom of the window. Compute what we can
18160 scroll. */
18161 if (first_unchanged_at_end_row
18162 /* No lines reused because we displayed everything up to the
18163 bottom of the window. */
18164 && it.current_y < it.last_visible_y)
18166 dvpos = (it.vpos
18167 - MATRIX_ROW_VPOS (first_unchanged_at_end_row,
18168 current_matrix));
18169 dy = it.current_y - first_unchanged_at_end_row->y;
18170 run.current_y = first_unchanged_at_end_row->y;
18171 run.desired_y = run.current_y + dy;
18172 run.height = it.last_visible_y - max (run.current_y, run.desired_y);
18174 else
18176 delta = delta_bytes = dvpos = dy
18177 = run.current_y = run.desired_y = run.height = 0;
18178 first_unchanged_at_end_row = NULL;
18180 IF_DEBUG ((debug_dvpos = dvpos, debug_dy = dy));
18183 /* Find the cursor if not already found. We have to decide whether
18184 PT will appear on this window (it sometimes doesn't, but this is
18185 not a very frequent case.) This decision has to be made before
18186 the current matrix is altered. A value of cursor.vpos < 0 means
18187 that PT is either in one of the lines beginning at
18188 first_unchanged_at_end_row or below the window. Don't care for
18189 lines that might be displayed later at the window end; as
18190 mentioned, this is not a frequent case. */
18191 if (w->cursor.vpos < 0)
18193 /* Cursor in unchanged rows at the top? */
18194 if (PT < CHARPOS (start_pos)
18195 && last_unchanged_at_beg_row)
18197 row = row_containing_pos (w, PT,
18198 MATRIX_FIRST_TEXT_ROW (w->current_matrix),
18199 last_unchanged_at_beg_row + 1, 0);
18200 if (row)
18201 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
18204 /* Start from first_unchanged_at_end_row looking for PT. */
18205 else if (first_unchanged_at_end_row)
18207 row = row_containing_pos (w, PT - delta,
18208 first_unchanged_at_end_row, NULL, 0);
18209 if (row)
18210 set_cursor_from_row (w, row, w->current_matrix, delta,
18211 delta_bytes, dy, dvpos);
18214 /* Give up if cursor was not found. */
18215 if (w->cursor.vpos < 0)
18217 clear_glyph_matrix (w->desired_matrix);
18218 return -1;
18222 /* Don't let the cursor end in the scroll margins. */
18224 int this_scroll_margin, cursor_height;
18225 int frame_line_height = default_line_pixel_height (w);
18226 int window_total_lines
18227 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (it.f) / frame_line_height;
18229 this_scroll_margin =
18230 max (0, min (scroll_margin, window_total_lines / 4));
18231 this_scroll_margin *= frame_line_height;
18232 cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
18234 if ((w->cursor.y < this_scroll_margin
18235 && CHARPOS (start) > BEGV)
18236 /* Old redisplay didn't take scroll margin into account at the bottom,
18237 but then global-hl-line-mode doesn't scroll. KFS 2004-06-14 */
18238 || (w->cursor.y + (make_cursor_line_fully_visible_p
18239 ? cursor_height + this_scroll_margin
18240 : 1)) > it.last_visible_y)
18242 w->cursor.vpos = -1;
18243 clear_glyph_matrix (w->desired_matrix);
18244 return -1;
18248 /* Scroll the display. Do it before changing the current matrix so
18249 that xterm.c doesn't get confused about where the cursor glyph is
18250 found. */
18251 if (dy && run.height)
18253 update_begin (f);
18255 if (FRAME_WINDOW_P (f))
18257 FRAME_RIF (f)->update_window_begin_hook (w);
18258 FRAME_RIF (f)->clear_window_mouse_face (w);
18259 FRAME_RIF (f)->scroll_run_hook (w, &run);
18260 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
18262 else
18264 /* Terminal frame. In this case, dvpos gives the number of
18265 lines to scroll by; dvpos < 0 means scroll up. */
18266 int from_vpos
18267 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, w->current_matrix);
18268 int from = WINDOW_TOP_EDGE_LINE (w) + from_vpos;
18269 int end = (WINDOW_TOP_EDGE_LINE (w)
18270 + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0)
18271 + window_internal_height (w));
18273 #if defined (HAVE_GPM) || defined (MSDOS)
18274 x_clear_window_mouse_face (w);
18275 #endif
18276 /* Perform the operation on the screen. */
18277 if (dvpos > 0)
18279 /* Scroll last_unchanged_at_beg_row to the end of the
18280 window down dvpos lines. */
18281 set_terminal_window (f, end);
18283 /* On dumb terminals delete dvpos lines at the end
18284 before inserting dvpos empty lines. */
18285 if (!FRAME_SCROLL_REGION_OK (f))
18286 ins_del_lines (f, end - dvpos, -dvpos);
18288 /* Insert dvpos empty lines in front of
18289 last_unchanged_at_beg_row. */
18290 ins_del_lines (f, from, dvpos);
18292 else if (dvpos < 0)
18294 /* Scroll up last_unchanged_at_beg_vpos to the end of
18295 the window to last_unchanged_at_beg_vpos - |dvpos|. */
18296 set_terminal_window (f, end);
18298 /* Delete dvpos lines in front of
18299 last_unchanged_at_beg_vpos. ins_del_lines will set
18300 the cursor to the given vpos and emit |dvpos| delete
18301 line sequences. */
18302 ins_del_lines (f, from + dvpos, dvpos);
18304 /* On a dumb terminal insert dvpos empty lines at the
18305 end. */
18306 if (!FRAME_SCROLL_REGION_OK (f))
18307 ins_del_lines (f, end + dvpos, -dvpos);
18310 set_terminal_window (f, 0);
18313 update_end (f);
18316 /* Shift reused rows of the current matrix to the right position.
18317 BOTTOM_ROW is the last + 1 row in the current matrix reserved for
18318 text. */
18319 bottom_row = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
18320 bottom_vpos = MATRIX_ROW_VPOS (bottom_row, current_matrix);
18321 if (dvpos < 0)
18323 rotate_matrix (current_matrix, first_unchanged_at_end_vpos + dvpos,
18324 bottom_vpos, dvpos);
18325 clear_glyph_matrix_rows (current_matrix, bottom_vpos + dvpos,
18326 bottom_vpos);
18328 else if (dvpos > 0)
18330 rotate_matrix (current_matrix, first_unchanged_at_end_vpos,
18331 bottom_vpos, dvpos);
18332 clear_glyph_matrix_rows (current_matrix, first_unchanged_at_end_vpos,
18333 first_unchanged_at_end_vpos + dvpos);
18336 /* For frame-based redisplay, make sure that current frame and window
18337 matrix are in sync with respect to glyph memory. */
18338 if (!FRAME_WINDOW_P (f))
18339 sync_frame_with_window_matrix_rows (w);
18341 /* Adjust buffer positions in reused rows. */
18342 if (delta || delta_bytes)
18343 increment_matrix_positions (current_matrix,
18344 first_unchanged_at_end_vpos + dvpos,
18345 bottom_vpos, delta, delta_bytes);
18347 /* Adjust Y positions. */
18348 if (dy)
18349 shift_glyph_matrix (w, current_matrix,
18350 first_unchanged_at_end_vpos + dvpos,
18351 bottom_vpos, dy);
18353 if (first_unchanged_at_end_row)
18355 first_unchanged_at_end_row += dvpos;
18356 if (first_unchanged_at_end_row->y >= it.last_visible_y
18357 || !MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row))
18358 first_unchanged_at_end_row = NULL;
18361 /* If scrolling up, there may be some lines to display at the end of
18362 the window. */
18363 last_text_row_at_end = NULL;
18364 if (dy < 0)
18366 /* Scrolling up can leave for example a partially visible line
18367 at the end of the window to be redisplayed. */
18368 /* Set last_row to the glyph row in the current matrix where the
18369 window end line is found. It has been moved up or down in
18370 the matrix by dvpos. */
18371 int last_vpos = w->window_end_vpos + dvpos;
18372 struct glyph_row *last_row = MATRIX_ROW (current_matrix, last_vpos);
18374 /* If last_row is the window end line, it should display text. */
18375 eassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_row));
18377 /* If window end line was partially visible before, begin
18378 displaying at that line. Otherwise begin displaying with the
18379 line following it. */
18380 if (MATRIX_ROW_BOTTOM_Y (last_row) - dy >= it.last_visible_y)
18382 init_to_row_start (&it, w, last_row);
18383 it.vpos = last_vpos;
18384 it.current_y = last_row->y;
18386 else
18388 init_to_row_end (&it, w, last_row);
18389 it.vpos = 1 + last_vpos;
18390 it.current_y = MATRIX_ROW_BOTTOM_Y (last_row);
18391 ++last_row;
18394 /* We may start in a continuation line. If so, we have to
18395 get the right continuation_lines_width and current_x. */
18396 it.continuation_lines_width = last_row->continuation_lines_width;
18397 it.hpos = it.current_x = 0;
18399 /* Display the rest of the lines at the window end. */
18400 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
18401 while (it.current_y < it.last_visible_y && !f->fonts_changed)
18403 /* Is it always sure that the display agrees with lines in
18404 the current matrix? I don't think so, so we mark rows
18405 displayed invalid in the current matrix by setting their
18406 enabled_p flag to zero. */
18407 SET_MATRIX_ROW_ENABLED_P (w->current_matrix, it.vpos, false);
18408 if (display_line (&it))
18409 last_text_row_at_end = it.glyph_row - 1;
18413 /* Update window_end_pos and window_end_vpos. */
18414 if (first_unchanged_at_end_row && !last_text_row_at_end)
18416 /* Window end line if one of the preserved rows from the current
18417 matrix. Set row to the last row displaying text in current
18418 matrix starting at first_unchanged_at_end_row, after
18419 scrolling. */
18420 eassert (MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
18421 row = find_last_row_displaying_text (w->current_matrix, &it,
18422 first_unchanged_at_end_row);
18423 eassert (row && MATRIX_ROW_DISPLAYS_TEXT_P (row));
18424 adjust_window_ends (w, row, 1);
18425 eassert (w->window_end_bytepos >= 0);
18426 IF_DEBUG (debug_method_add (w, "A"));
18428 else if (last_text_row_at_end)
18430 adjust_window_ends (w, last_text_row_at_end, 0);
18431 eassert (w->window_end_bytepos >= 0);
18432 IF_DEBUG (debug_method_add (w, "B"));
18434 else if (last_text_row)
18436 /* We have displayed either to the end of the window or at the
18437 end of the window, i.e. the last row with text is to be found
18438 in the desired matrix. */
18439 adjust_window_ends (w, last_text_row, 0);
18440 eassert (w->window_end_bytepos >= 0);
18442 else if (first_unchanged_at_end_row == NULL
18443 && last_text_row == NULL
18444 && last_text_row_at_end == NULL)
18446 /* Displayed to end of window, but no line containing text was
18447 displayed. Lines were deleted at the end of the window. */
18448 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
18449 int vpos = w->window_end_vpos;
18450 struct glyph_row *current_row = current_matrix->rows + vpos;
18451 struct glyph_row *desired_row = desired_matrix->rows + vpos;
18453 for (row = NULL;
18454 row == NULL && vpos >= first_vpos;
18455 --vpos, --current_row, --desired_row)
18457 if (desired_row->enabled_p)
18459 if (MATRIX_ROW_DISPLAYS_TEXT_P (desired_row))
18460 row = desired_row;
18462 else if (MATRIX_ROW_DISPLAYS_TEXT_P (current_row))
18463 row = current_row;
18466 eassert (row != NULL);
18467 w->window_end_vpos = vpos + 1;
18468 w->window_end_pos = Z - MATRIX_ROW_END_CHARPOS (row);
18469 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
18470 eassert (w->window_end_bytepos >= 0);
18471 IF_DEBUG (debug_method_add (w, "C"));
18473 else
18474 emacs_abort ();
18476 IF_DEBUG ((debug_end_pos = w->window_end_pos,
18477 debug_end_vpos = w->window_end_vpos));
18479 /* Record that display has not been completed. */
18480 w->window_end_valid = 0;
18481 w->desired_matrix->no_scrolling_p = 1;
18482 return 3;
18484 #undef GIVE_UP
18489 /***********************************************************************
18490 More debugging support
18491 ***********************************************************************/
18493 #ifdef GLYPH_DEBUG
18495 void dump_glyph_row (struct glyph_row *, int, int) EXTERNALLY_VISIBLE;
18496 void dump_glyph_matrix (struct glyph_matrix *, int) EXTERNALLY_VISIBLE;
18497 void dump_glyph (struct glyph_row *, struct glyph *, int) EXTERNALLY_VISIBLE;
18500 /* Dump the contents of glyph matrix MATRIX on stderr.
18502 GLYPHS 0 means don't show glyph contents.
18503 GLYPHS 1 means show glyphs in short form
18504 GLYPHS > 1 means show glyphs in long form. */
18506 void
18507 dump_glyph_matrix (struct glyph_matrix *matrix, int glyphs)
18509 int i;
18510 for (i = 0; i < matrix->nrows; ++i)
18511 dump_glyph_row (MATRIX_ROW (matrix, i), i, glyphs);
18515 /* Dump contents of glyph GLYPH to stderr. ROW and AREA are
18516 the glyph row and area where the glyph comes from. */
18518 void
18519 dump_glyph (struct glyph_row *row, struct glyph *glyph, int area)
18521 if (glyph->type == CHAR_GLYPH
18522 || glyph->type == GLYPHLESS_GLYPH)
18524 fprintf (stderr,
18525 " %5"pD"d %c %9"pI"d %c %3d 0x%06x %c %4d %1.1d%1.1d\n",
18526 glyph - row->glyphs[TEXT_AREA],
18527 (glyph->type == CHAR_GLYPH
18528 ? 'C'
18529 : 'G'),
18530 glyph->charpos,
18531 (BUFFERP (glyph->object)
18532 ? 'B'
18533 : (STRINGP (glyph->object)
18534 ? 'S'
18535 : (NILP (glyph->object)
18536 ? '0'
18537 : '-'))),
18538 glyph->pixel_width,
18539 glyph->u.ch,
18540 (glyph->u.ch < 0x80 && glyph->u.ch >= ' '
18541 ? glyph->u.ch
18542 : '.'),
18543 glyph->face_id,
18544 glyph->left_box_line_p,
18545 glyph->right_box_line_p);
18547 else if (glyph->type == STRETCH_GLYPH)
18549 fprintf (stderr,
18550 " %5"pD"d %c %9"pI"d %c %3d 0x%06x %c %4d %1.1d%1.1d\n",
18551 glyph - row->glyphs[TEXT_AREA],
18552 'S',
18553 glyph->charpos,
18554 (BUFFERP (glyph->object)
18555 ? 'B'
18556 : (STRINGP (glyph->object)
18557 ? 'S'
18558 : (NILP (glyph->object)
18559 ? '0'
18560 : '-'))),
18561 glyph->pixel_width,
18563 ' ',
18564 glyph->face_id,
18565 glyph->left_box_line_p,
18566 glyph->right_box_line_p);
18568 else if (glyph->type == IMAGE_GLYPH)
18570 fprintf (stderr,
18571 " %5"pD"d %c %9"pI"d %c %3d 0x%06x %c %4d %1.1d%1.1d\n",
18572 glyph - row->glyphs[TEXT_AREA],
18573 'I',
18574 glyph->charpos,
18575 (BUFFERP (glyph->object)
18576 ? 'B'
18577 : (STRINGP (glyph->object)
18578 ? 'S'
18579 : (NILP (glyph->object)
18580 ? '0'
18581 : '-'))),
18582 glyph->pixel_width,
18583 glyph->u.img_id,
18584 '.',
18585 glyph->face_id,
18586 glyph->left_box_line_p,
18587 glyph->right_box_line_p);
18589 else if (glyph->type == COMPOSITE_GLYPH)
18591 fprintf (stderr,
18592 " %5"pD"d %c %9"pI"d %c %3d 0x%06x",
18593 glyph - row->glyphs[TEXT_AREA],
18594 '+',
18595 glyph->charpos,
18596 (BUFFERP (glyph->object)
18597 ? 'B'
18598 : (STRINGP (glyph->object)
18599 ? 'S'
18600 : (NILP (glyph->object)
18601 ? '0'
18602 : '-'))),
18603 glyph->pixel_width,
18604 glyph->u.cmp.id);
18605 if (glyph->u.cmp.automatic)
18606 fprintf (stderr,
18607 "[%d-%d]",
18608 glyph->slice.cmp.from, glyph->slice.cmp.to);
18609 fprintf (stderr, " . %4d %1.1d%1.1d\n",
18610 glyph->face_id,
18611 glyph->left_box_line_p,
18612 glyph->right_box_line_p);
18617 /* Dump the contents of glyph row at VPOS in MATRIX to stderr.
18618 GLYPHS 0 means don't show glyph contents.
18619 GLYPHS 1 means show glyphs in short form
18620 GLYPHS > 1 means show glyphs in long form. */
18622 void
18623 dump_glyph_row (struct glyph_row *row, int vpos, int glyphs)
18625 if (glyphs != 1)
18627 fprintf (stderr, "Row Start End Used oE><\\CTZFesm X Y W H V A P\n");
18628 fprintf (stderr, "==============================================================================\n");
18630 fprintf (stderr, "%3d %9"pI"d %9"pI"d %4d %1.1d%1.1d%1.1d%1.1d\
18631 %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d %4d %4d %4d %4d %4d %4d %4d\n",
18632 vpos,
18633 MATRIX_ROW_START_CHARPOS (row),
18634 MATRIX_ROW_END_CHARPOS (row),
18635 row->used[TEXT_AREA],
18636 row->contains_overlapping_glyphs_p,
18637 row->enabled_p,
18638 row->truncated_on_left_p,
18639 row->truncated_on_right_p,
18640 row->continued_p,
18641 MATRIX_ROW_CONTINUATION_LINE_P (row),
18642 MATRIX_ROW_DISPLAYS_TEXT_P (row),
18643 row->ends_at_zv_p,
18644 row->fill_line_p,
18645 row->ends_in_middle_of_char_p,
18646 row->starts_in_middle_of_char_p,
18647 row->mouse_face_p,
18648 row->x,
18649 row->y,
18650 row->pixel_width,
18651 row->height,
18652 row->visible_height,
18653 row->ascent,
18654 row->phys_ascent);
18655 /* The next 3 lines should align to "Start" in the header. */
18656 fprintf (stderr, " %9"pD"d %9"pD"d\t%5d\n", row->start.overlay_string_index,
18657 row->end.overlay_string_index,
18658 row->continuation_lines_width);
18659 fprintf (stderr, " %9"pI"d %9"pI"d\n",
18660 CHARPOS (row->start.string_pos),
18661 CHARPOS (row->end.string_pos));
18662 fprintf (stderr, " %9d %9d\n", row->start.dpvec_index,
18663 row->end.dpvec_index);
18666 if (glyphs > 1)
18668 int area;
18670 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
18672 struct glyph *glyph = row->glyphs[area];
18673 struct glyph *glyph_end = glyph + row->used[area];
18675 /* Glyph for a line end in text. */
18676 if (area == TEXT_AREA && glyph == glyph_end && glyph->charpos > 0)
18677 ++glyph_end;
18679 if (glyph < glyph_end)
18680 fprintf (stderr, " Glyph# Type Pos O W Code C Face LR\n");
18682 for (; glyph < glyph_end; ++glyph)
18683 dump_glyph (row, glyph, area);
18686 else if (glyphs == 1)
18688 int area;
18689 char s[SHRT_MAX + 4];
18691 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
18693 int i;
18695 for (i = 0; i < row->used[area]; ++i)
18697 struct glyph *glyph = row->glyphs[area] + i;
18698 if (i == row->used[area] - 1
18699 && area == TEXT_AREA
18700 && NILP (glyph->object)
18701 && glyph->type == CHAR_GLYPH
18702 && glyph->u.ch == ' ')
18704 strcpy (&s[i], "[\\n]");
18705 i += 4;
18707 else if (glyph->type == CHAR_GLYPH
18708 && glyph->u.ch < 0x80
18709 && glyph->u.ch >= ' ')
18710 s[i] = glyph->u.ch;
18711 else
18712 s[i] = '.';
18715 s[i] = '\0';
18716 fprintf (stderr, "%3d: (%d) '%s'\n", vpos, row->enabled_p, s);
18722 DEFUN ("dump-glyph-matrix", Fdump_glyph_matrix,
18723 Sdump_glyph_matrix, 0, 1, "p",
18724 doc: /* Dump the current matrix of the selected window to stderr.
18725 Shows contents of glyph row structures. With non-nil
18726 parameter GLYPHS, dump glyphs as well. If GLYPHS is 1 show
18727 glyphs in short form, otherwise show glyphs in long form.
18729 Interactively, no argument means show glyphs in short form;
18730 with numeric argument, its value is passed as the GLYPHS flag. */)
18731 (Lisp_Object glyphs)
18733 struct window *w = XWINDOW (selected_window);
18734 struct buffer *buffer = XBUFFER (w->contents);
18736 fprintf (stderr, "PT = %"pI"d, BEGV = %"pI"d. ZV = %"pI"d\n",
18737 BUF_PT (buffer), BUF_BEGV (buffer), BUF_ZV (buffer));
18738 fprintf (stderr, "Cursor x = %d, y = %d, hpos = %d, vpos = %d\n",
18739 w->cursor.x, w->cursor.y, w->cursor.hpos, w->cursor.vpos);
18740 fprintf (stderr, "=============================================\n");
18741 dump_glyph_matrix (w->current_matrix,
18742 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 0);
18743 return Qnil;
18747 DEFUN ("dump-frame-glyph-matrix", Fdump_frame_glyph_matrix,
18748 Sdump_frame_glyph_matrix, 0, 0, "", doc: /* Dump the current glyph matrix of the selected frame to stderr.
18749 Only text-mode frames have frame glyph matrices. */)
18750 (void)
18752 struct frame *f = XFRAME (selected_frame);
18754 if (f->current_matrix)
18755 dump_glyph_matrix (f->current_matrix, 1);
18756 else
18757 fprintf (stderr, "*** This frame doesn't have a frame glyph matrix ***\n");
18758 return Qnil;
18762 DEFUN ("dump-glyph-row", Fdump_glyph_row, Sdump_glyph_row, 1, 2, "",
18763 doc: /* Dump glyph row ROW to stderr.
18764 GLYPH 0 means don't dump glyphs.
18765 GLYPH 1 means dump glyphs in short form.
18766 GLYPH > 1 or omitted means dump glyphs in long form. */)
18767 (Lisp_Object row, Lisp_Object glyphs)
18769 struct glyph_matrix *matrix;
18770 EMACS_INT vpos;
18772 CHECK_NUMBER (row);
18773 matrix = XWINDOW (selected_window)->current_matrix;
18774 vpos = XINT (row);
18775 if (vpos >= 0 && vpos < matrix->nrows)
18776 dump_glyph_row (MATRIX_ROW (matrix, vpos),
18777 vpos,
18778 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 2);
18779 return Qnil;
18783 DEFUN ("dump-tool-bar-row", Fdump_tool_bar_row, Sdump_tool_bar_row, 1, 2, "",
18784 doc: /* Dump glyph row ROW of the tool-bar of the current frame to stderr.
18785 GLYPH 0 means don't dump glyphs.
18786 GLYPH 1 means dump glyphs in short form.
18787 GLYPH > 1 or omitted means dump glyphs in long form.
18789 If there's no tool-bar, or if the tool-bar is not drawn by Emacs,
18790 do nothing. */)
18791 (Lisp_Object row, Lisp_Object glyphs)
18793 #if defined (HAVE_WINDOW_SYSTEM) && ! defined (USE_GTK) && ! defined (HAVE_NS)
18794 struct frame *sf = SELECTED_FRAME ();
18795 struct glyph_matrix *m = XWINDOW (sf->tool_bar_window)->current_matrix;
18796 EMACS_INT vpos;
18798 CHECK_NUMBER (row);
18799 vpos = XINT (row);
18800 if (vpos >= 0 && vpos < m->nrows)
18801 dump_glyph_row (MATRIX_ROW (m, vpos), vpos,
18802 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 2);
18803 #endif
18804 return Qnil;
18808 DEFUN ("trace-redisplay", Ftrace_redisplay, Strace_redisplay, 0, 1, "P",
18809 doc: /* Toggle tracing of redisplay.
18810 With ARG, turn tracing on if and only if ARG is positive. */)
18811 (Lisp_Object arg)
18813 if (NILP (arg))
18814 trace_redisplay_p = !trace_redisplay_p;
18815 else
18817 arg = Fprefix_numeric_value (arg);
18818 trace_redisplay_p = XINT (arg) > 0;
18821 return Qnil;
18825 DEFUN ("trace-to-stderr", Ftrace_to_stderr, Strace_to_stderr, 1, MANY, "",
18826 doc: /* Like `format', but print result to stderr.
18827 usage: (trace-to-stderr STRING &rest OBJECTS) */)
18828 (ptrdiff_t nargs, Lisp_Object *args)
18830 Lisp_Object s = Fformat (nargs, args);
18831 fwrite (SDATA (s), 1, SBYTES (s), stderr);
18832 return Qnil;
18835 #endif /* GLYPH_DEBUG */
18839 /***********************************************************************
18840 Building Desired Matrix Rows
18841 ***********************************************************************/
18843 /* Return a temporary glyph row holding the glyphs of an overlay arrow.
18844 Used for non-window-redisplay windows, and for windows w/o left fringe. */
18846 static struct glyph_row *
18847 get_overlay_arrow_glyph_row (struct window *w, Lisp_Object overlay_arrow_string)
18849 struct frame *f = XFRAME (WINDOW_FRAME (w));
18850 struct buffer *buffer = XBUFFER (w->contents);
18851 struct buffer *old = current_buffer;
18852 const unsigned char *arrow_string = SDATA (overlay_arrow_string);
18853 ptrdiff_t arrow_len = SCHARS (overlay_arrow_string);
18854 const unsigned char *arrow_end = arrow_string + arrow_len;
18855 const unsigned char *p;
18856 struct it it;
18857 bool multibyte_p;
18858 int n_glyphs_before;
18860 set_buffer_temp (buffer);
18861 init_iterator (&it, w, -1, -1, &scratch_glyph_row, DEFAULT_FACE_ID);
18862 scratch_glyph_row.reversed_p = false;
18863 it.glyph_row->used[TEXT_AREA] = 0;
18864 SET_TEXT_POS (it.position, 0, 0);
18866 multibyte_p = !NILP (BVAR (buffer, enable_multibyte_characters));
18867 p = arrow_string;
18868 while (p < arrow_end)
18870 Lisp_Object face, ilisp;
18872 /* Get the next character. */
18873 if (multibyte_p)
18874 it.c = it.char_to_display = string_char_and_length (p, &it.len);
18875 else
18877 it.c = it.char_to_display = *p, it.len = 1;
18878 if (! ASCII_CHAR_P (it.c))
18879 it.char_to_display = BYTE8_TO_CHAR (it.c);
18881 p += it.len;
18883 /* Get its face. */
18884 ilisp = make_number (p - arrow_string);
18885 face = Fget_text_property (ilisp, Qface, overlay_arrow_string);
18886 it.face_id = compute_char_face (f, it.char_to_display, face);
18888 /* Compute its width, get its glyphs. */
18889 n_glyphs_before = it.glyph_row->used[TEXT_AREA];
18890 SET_TEXT_POS (it.position, -1, -1);
18891 PRODUCE_GLYPHS (&it);
18893 /* If this character doesn't fit any more in the line, we have
18894 to remove some glyphs. */
18895 if (it.current_x > it.last_visible_x)
18897 it.glyph_row->used[TEXT_AREA] = n_glyphs_before;
18898 break;
18902 set_buffer_temp (old);
18903 return it.glyph_row;
18907 /* Insert truncation glyphs at the start of IT->glyph_row. Which
18908 glyphs to insert is determined by produce_special_glyphs. */
18910 static void
18911 insert_left_trunc_glyphs (struct it *it)
18913 struct it truncate_it;
18914 struct glyph *from, *end, *to, *toend;
18916 eassert (!FRAME_WINDOW_P (it->f)
18917 || (!it->glyph_row->reversed_p
18918 && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0)
18919 || (it->glyph_row->reversed_p
18920 && WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0));
18922 /* Get the truncation glyphs. */
18923 truncate_it = *it;
18924 truncate_it.current_x = 0;
18925 truncate_it.face_id = DEFAULT_FACE_ID;
18926 truncate_it.glyph_row = &scratch_glyph_row;
18927 truncate_it.area = TEXT_AREA;
18928 truncate_it.glyph_row->used[TEXT_AREA] = 0;
18929 CHARPOS (truncate_it.position) = BYTEPOS (truncate_it.position) = -1;
18930 truncate_it.object = Qnil;
18931 produce_special_glyphs (&truncate_it, IT_TRUNCATION);
18933 /* Overwrite glyphs from IT with truncation glyphs. */
18934 if (!it->glyph_row->reversed_p)
18936 short tused = truncate_it.glyph_row->used[TEXT_AREA];
18938 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
18939 end = from + tused;
18940 to = it->glyph_row->glyphs[TEXT_AREA];
18941 toend = to + it->glyph_row->used[TEXT_AREA];
18942 if (FRAME_WINDOW_P (it->f))
18944 /* On GUI frames, when variable-size fonts are displayed,
18945 the truncation glyphs may need more pixels than the row's
18946 glyphs they overwrite. We overwrite more glyphs to free
18947 enough screen real estate, and enlarge the stretch glyph
18948 on the right (see display_line), if there is one, to
18949 preserve the screen position of the truncation glyphs on
18950 the right. */
18951 int w = 0;
18952 struct glyph *g = to;
18953 short used;
18955 /* The first glyph could be partially visible, in which case
18956 it->glyph_row->x will be negative. But we want the left
18957 truncation glyphs to be aligned at the left margin of the
18958 window, so we override the x coordinate at which the row
18959 will begin. */
18960 it->glyph_row->x = 0;
18961 while (g < toend && w < it->truncation_pixel_width)
18963 w += g->pixel_width;
18964 ++g;
18966 if (g - to - tused > 0)
18968 memmove (to + tused, g, (toend - g) * sizeof(*g));
18969 it->glyph_row->used[TEXT_AREA] -= g - to - tused;
18971 used = it->glyph_row->used[TEXT_AREA];
18972 if (it->glyph_row->truncated_on_right_p
18973 && WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0
18974 && it->glyph_row->glyphs[TEXT_AREA][used - 2].type
18975 == STRETCH_GLYPH)
18977 int extra = w - it->truncation_pixel_width;
18979 it->glyph_row->glyphs[TEXT_AREA][used - 2].pixel_width += extra;
18983 while (from < end)
18984 *to++ = *from++;
18986 /* There may be padding glyphs left over. Overwrite them too. */
18987 if (!FRAME_WINDOW_P (it->f))
18989 while (to < toend && CHAR_GLYPH_PADDING_P (*to))
18991 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
18992 while (from < end)
18993 *to++ = *from++;
18997 if (to > toend)
18998 it->glyph_row->used[TEXT_AREA] = to - it->glyph_row->glyphs[TEXT_AREA];
19000 else
19002 short tused = truncate_it.glyph_row->used[TEXT_AREA];
19004 /* In R2L rows, overwrite the last (rightmost) glyphs, and do
19005 that back to front. */
19006 end = truncate_it.glyph_row->glyphs[TEXT_AREA];
19007 from = end + truncate_it.glyph_row->used[TEXT_AREA] - 1;
19008 toend = it->glyph_row->glyphs[TEXT_AREA];
19009 to = toend + it->glyph_row->used[TEXT_AREA] - 1;
19010 if (FRAME_WINDOW_P (it->f))
19012 int w = 0;
19013 struct glyph *g = to;
19015 while (g >= toend && w < it->truncation_pixel_width)
19017 w += g->pixel_width;
19018 --g;
19020 if (to - g - tused > 0)
19021 to = g + tused;
19022 if (it->glyph_row->truncated_on_right_p
19023 && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0
19024 && it->glyph_row->glyphs[TEXT_AREA][1].type == STRETCH_GLYPH)
19026 int extra = w - it->truncation_pixel_width;
19028 it->glyph_row->glyphs[TEXT_AREA][1].pixel_width += extra;
19032 while (from >= end && to >= toend)
19033 *to-- = *from--;
19034 if (!FRAME_WINDOW_P (it->f))
19036 while (to >= toend && CHAR_GLYPH_PADDING_P (*to))
19038 from =
19039 truncate_it.glyph_row->glyphs[TEXT_AREA]
19040 + truncate_it.glyph_row->used[TEXT_AREA] - 1;
19041 while (from >= end && to >= toend)
19042 *to-- = *from--;
19045 if (from >= end)
19047 /* Need to free some room before prepending additional
19048 glyphs. */
19049 int move_by = from - end + 1;
19050 struct glyph *g0 = it->glyph_row->glyphs[TEXT_AREA];
19051 struct glyph *g = g0 + it->glyph_row->used[TEXT_AREA] - 1;
19053 for ( ; g >= g0; g--)
19054 g[move_by] = *g;
19055 while (from >= end)
19056 *to-- = *from--;
19057 it->glyph_row->used[TEXT_AREA] += move_by;
19062 /* Compute the hash code for ROW. */
19063 unsigned
19064 row_hash (struct glyph_row *row)
19066 int area, k;
19067 unsigned hashval = 0;
19069 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
19070 for (k = 0; k < row->used[area]; ++k)
19071 hashval = ((((hashval << 4) + (hashval >> 24)) & 0x0fffffff)
19072 + row->glyphs[area][k].u.val
19073 + row->glyphs[area][k].face_id
19074 + row->glyphs[area][k].padding_p
19075 + (row->glyphs[area][k].type << 2));
19077 return hashval;
19080 /* Compute the pixel height and width of IT->glyph_row.
19082 Most of the time, ascent and height of a display line will be equal
19083 to the max_ascent and max_height values of the display iterator
19084 structure. This is not the case if
19086 1. We hit ZV without displaying anything. In this case, max_ascent
19087 and max_height will be zero.
19089 2. We have some glyphs that don't contribute to the line height.
19090 (The glyph row flag contributes_to_line_height_p is for future
19091 pixmap extensions).
19093 The first case is easily covered by using default values because in
19094 these cases, the line height does not really matter, except that it
19095 must not be zero. */
19097 static void
19098 compute_line_metrics (struct it *it)
19100 struct glyph_row *row = it->glyph_row;
19102 if (FRAME_WINDOW_P (it->f))
19104 int i, min_y, max_y;
19106 /* The line may consist of one space only, that was added to
19107 place the cursor on it. If so, the row's height hasn't been
19108 computed yet. */
19109 if (row->height == 0)
19111 if (it->max_ascent + it->max_descent == 0)
19112 it->max_descent = it->max_phys_descent = FRAME_LINE_HEIGHT (it->f);
19113 row->ascent = it->max_ascent;
19114 row->height = it->max_ascent + it->max_descent;
19115 row->phys_ascent = it->max_phys_ascent;
19116 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
19117 row->extra_line_spacing = it->max_extra_line_spacing;
19120 /* Compute the width of this line. */
19121 row->pixel_width = row->x;
19122 for (i = 0; i < row->used[TEXT_AREA]; ++i)
19123 row->pixel_width += row->glyphs[TEXT_AREA][i].pixel_width;
19125 eassert (row->pixel_width >= 0);
19126 eassert (row->ascent >= 0 && row->height > 0);
19128 row->overlapping_p = (MATRIX_ROW_OVERLAPS_SUCC_P (row)
19129 || MATRIX_ROW_OVERLAPS_PRED_P (row));
19131 /* If first line's physical ascent is larger than its logical
19132 ascent, use the physical ascent, and make the row taller.
19133 This makes accented characters fully visible. */
19134 if (row == MATRIX_FIRST_TEXT_ROW (it->w->desired_matrix)
19135 && row->phys_ascent > row->ascent)
19137 row->height += row->phys_ascent - row->ascent;
19138 row->ascent = row->phys_ascent;
19141 /* Compute how much of the line is visible. */
19142 row->visible_height = row->height;
19144 min_y = WINDOW_HEADER_LINE_HEIGHT (it->w);
19145 max_y = WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w);
19147 if (row->y < min_y)
19148 row->visible_height -= min_y - row->y;
19149 if (row->y + row->height > max_y)
19150 row->visible_height -= row->y + row->height - max_y;
19152 else
19154 row->pixel_width = row->used[TEXT_AREA];
19155 if (row->continued_p)
19156 row->pixel_width -= it->continuation_pixel_width;
19157 else if (row->truncated_on_right_p)
19158 row->pixel_width -= it->truncation_pixel_width;
19159 row->ascent = row->phys_ascent = 0;
19160 row->height = row->phys_height = row->visible_height = 1;
19161 row->extra_line_spacing = 0;
19164 /* Compute a hash code for this row. */
19165 row->hash = row_hash (row);
19167 it->max_ascent = it->max_descent = 0;
19168 it->max_phys_ascent = it->max_phys_descent = 0;
19172 /* Append one space to the glyph row of iterator IT if doing a
19173 window-based redisplay. The space has the same face as
19174 IT->face_id. Value is non-zero if a space was added.
19176 This function is called to make sure that there is always one glyph
19177 at the end of a glyph row that the cursor can be set on under
19178 window-systems. (If there weren't such a glyph we would not know
19179 how wide and tall a box cursor should be displayed).
19181 At the same time this space let's a nicely handle clearing to the
19182 end of the line if the row ends in italic text. */
19184 static int
19185 append_space_for_newline (struct it *it, int default_face_p)
19187 if (FRAME_WINDOW_P (it->f))
19189 int n = it->glyph_row->used[TEXT_AREA];
19191 if (it->glyph_row->glyphs[TEXT_AREA] + n
19192 < it->glyph_row->glyphs[1 + TEXT_AREA])
19194 /* Save some values that must not be changed.
19195 Must save IT->c and IT->len because otherwise
19196 ITERATOR_AT_END_P wouldn't work anymore after
19197 append_space_for_newline has been called. */
19198 enum display_element_type saved_what = it->what;
19199 int saved_c = it->c, saved_len = it->len;
19200 int saved_char_to_display = it->char_to_display;
19201 int saved_x = it->current_x;
19202 int saved_face_id = it->face_id;
19203 int saved_box_end = it->end_of_box_run_p;
19204 struct text_pos saved_pos;
19205 Lisp_Object saved_object;
19206 struct face *face;
19208 saved_object = it->object;
19209 saved_pos = it->position;
19211 it->what = IT_CHARACTER;
19212 memset (&it->position, 0, sizeof it->position);
19213 it->object = Qnil;
19214 it->c = it->char_to_display = ' ';
19215 it->len = 1;
19217 /* If the default face was remapped, be sure to use the
19218 remapped face for the appended newline. */
19219 if (default_face_p)
19220 it->face_id = lookup_basic_face (it->f, DEFAULT_FACE_ID);
19221 else if (it->face_before_selective_p)
19222 it->face_id = it->saved_face_id;
19223 face = FACE_FROM_ID (it->f, it->face_id);
19224 it->face_id = FACE_FOR_CHAR (it->f, face, 0, -1, Qnil);
19225 /* In R2L rows, we will prepend a stretch glyph that will
19226 have the end_of_box_run_p flag set for it, so there's no
19227 need for the appended newline glyph to have that flag
19228 set. */
19229 if (it->glyph_row->reversed_p
19230 /* But if the appended newline glyph goes all the way to
19231 the end of the row, there will be no stretch glyph,
19232 so leave the box flag set. */
19233 && saved_x + FRAME_COLUMN_WIDTH (it->f) < it->last_visible_x)
19234 it->end_of_box_run_p = 0;
19236 PRODUCE_GLYPHS (it);
19238 it->override_ascent = -1;
19239 it->constrain_row_ascent_descent_p = 0;
19240 it->current_x = saved_x;
19241 it->object = saved_object;
19242 it->position = saved_pos;
19243 it->what = saved_what;
19244 it->face_id = saved_face_id;
19245 it->len = saved_len;
19246 it->c = saved_c;
19247 it->char_to_display = saved_char_to_display;
19248 it->end_of_box_run_p = saved_box_end;
19249 return 1;
19253 return 0;
19257 /* Extend the face of the last glyph in the text area of IT->glyph_row
19258 to the end of the display line. Called from display_line. If the
19259 glyph row is empty, add a space glyph to it so that we know the
19260 face to draw. Set the glyph row flag fill_line_p. If the glyph
19261 row is R2L, prepend a stretch glyph to cover the empty space to the
19262 left of the leftmost glyph. */
19264 static void
19265 extend_face_to_end_of_line (struct it *it)
19267 struct face *face, *default_face;
19268 struct frame *f = it->f;
19270 /* If line is already filled, do nothing. Non window-system frames
19271 get a grace of one more ``pixel'' because their characters are
19272 1-``pixel'' wide, so they hit the equality too early. This grace
19273 is needed only for R2L rows that are not continued, to produce
19274 one extra blank where we could display the cursor. */
19275 if ((it->current_x >= it->last_visible_x
19276 + (!FRAME_WINDOW_P (f)
19277 && it->glyph_row->reversed_p
19278 && !it->glyph_row->continued_p))
19279 /* If the window has display margins, we will need to extend
19280 their face even if the text area is filled. */
19281 && !(WINDOW_LEFT_MARGIN_WIDTH (it->w) > 0
19282 || WINDOW_RIGHT_MARGIN_WIDTH (it->w) > 0))
19283 return;
19285 /* The default face, possibly remapped. */
19286 default_face = FACE_FROM_ID (f, lookup_basic_face (f, DEFAULT_FACE_ID));
19288 /* Face extension extends the background and box of IT->face_id
19289 to the end of the line. If the background equals the background
19290 of the frame, we don't have to do anything. */
19291 if (it->face_before_selective_p)
19292 face = FACE_FROM_ID (f, it->saved_face_id);
19293 else
19294 face = FACE_FROM_ID (f, it->face_id);
19296 if (FRAME_WINDOW_P (f)
19297 && MATRIX_ROW_DISPLAYS_TEXT_P (it->glyph_row)
19298 && face->box == FACE_NO_BOX
19299 && face->background == FRAME_BACKGROUND_PIXEL (f)
19300 #ifdef HAVE_WINDOW_SYSTEM
19301 && !face->stipple
19302 #endif
19303 && !it->glyph_row->reversed_p)
19304 return;
19306 /* Set the glyph row flag indicating that the face of the last glyph
19307 in the text area has to be drawn to the end of the text area. */
19308 it->glyph_row->fill_line_p = 1;
19310 /* If current character of IT is not ASCII, make sure we have the
19311 ASCII face. This will be automatically undone the next time
19312 get_next_display_element returns a multibyte character. Note
19313 that the character will always be single byte in unibyte
19314 text. */
19315 if (!ASCII_CHAR_P (it->c))
19317 it->face_id = FACE_FOR_CHAR (f, face, 0, -1, Qnil);
19320 if (FRAME_WINDOW_P (f))
19322 /* If the row is empty, add a space with the current face of IT,
19323 so that we know which face to draw. */
19324 if (it->glyph_row->used[TEXT_AREA] == 0)
19326 it->glyph_row->glyphs[TEXT_AREA][0] = space_glyph;
19327 it->glyph_row->glyphs[TEXT_AREA][0].face_id = face->id;
19328 it->glyph_row->used[TEXT_AREA] = 1;
19330 /* Mode line and the header line don't have margins, and
19331 likewise the frame's tool-bar window, if there is any. */
19332 if (!(it->glyph_row->mode_line_p
19333 #if defined (HAVE_WINDOW_SYSTEM) && ! defined (USE_GTK) && ! defined (HAVE_NS)
19334 || (WINDOWP (f->tool_bar_window)
19335 && it->w == XWINDOW (f->tool_bar_window))
19336 #endif
19339 if (WINDOW_LEFT_MARGIN_WIDTH (it->w) > 0
19340 && it->glyph_row->used[LEFT_MARGIN_AREA] == 0)
19342 it->glyph_row->glyphs[LEFT_MARGIN_AREA][0] = space_glyph;
19343 it->glyph_row->glyphs[LEFT_MARGIN_AREA][0].face_id =
19344 default_face->id;
19345 it->glyph_row->used[LEFT_MARGIN_AREA] = 1;
19347 if (WINDOW_RIGHT_MARGIN_WIDTH (it->w) > 0
19348 && it->glyph_row->used[RIGHT_MARGIN_AREA] == 0)
19350 it->glyph_row->glyphs[RIGHT_MARGIN_AREA][0] = space_glyph;
19351 it->glyph_row->glyphs[RIGHT_MARGIN_AREA][0].face_id =
19352 default_face->id;
19353 it->glyph_row->used[RIGHT_MARGIN_AREA] = 1;
19356 #ifdef HAVE_WINDOW_SYSTEM
19357 if (it->glyph_row->reversed_p)
19359 /* Prepend a stretch glyph to the row, such that the
19360 rightmost glyph will be drawn flushed all the way to the
19361 right margin of the window. The stretch glyph that will
19362 occupy the empty space, if any, to the left of the
19363 glyphs. */
19364 struct font *font = face->font ? face->font : FRAME_FONT (f);
19365 struct glyph *row_start = it->glyph_row->glyphs[TEXT_AREA];
19366 struct glyph *row_end = row_start + it->glyph_row->used[TEXT_AREA];
19367 struct glyph *g;
19368 int row_width, stretch_ascent, stretch_width;
19369 struct text_pos saved_pos;
19370 int saved_face_id, saved_avoid_cursor, saved_box_start;
19372 for (row_width = 0, g = row_start; g < row_end; g++)
19373 row_width += g->pixel_width;
19375 /* FIXME: There are various minor display glitches in R2L
19376 rows when only one of the fringes is missing. The
19377 strange condition below produces the least bad effect. */
19378 if ((WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0)
19379 == (WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0)
19380 || WINDOW_RIGHT_FRINGE_WIDTH (it->w) != 0)
19381 stretch_width = window_box_width (it->w, TEXT_AREA);
19382 else
19383 stretch_width = it->last_visible_x - it->first_visible_x;
19384 stretch_width -= row_width;
19386 if (stretch_width > 0)
19388 stretch_ascent =
19389 (((it->ascent + it->descent)
19390 * FONT_BASE (font)) / FONT_HEIGHT (font));
19391 saved_pos = it->position;
19392 memset (&it->position, 0, sizeof it->position);
19393 saved_avoid_cursor = it->avoid_cursor_p;
19394 it->avoid_cursor_p = 1;
19395 saved_face_id = it->face_id;
19396 saved_box_start = it->start_of_box_run_p;
19397 /* The last row's stretch glyph should get the default
19398 face, to avoid painting the rest of the window with
19399 the region face, if the region ends at ZV. */
19400 if (it->glyph_row->ends_at_zv_p)
19401 it->face_id = default_face->id;
19402 else
19403 it->face_id = face->id;
19404 it->start_of_box_run_p = 0;
19405 append_stretch_glyph (it, Qnil, stretch_width,
19406 it->ascent + it->descent, stretch_ascent);
19407 it->position = saved_pos;
19408 it->avoid_cursor_p = saved_avoid_cursor;
19409 it->face_id = saved_face_id;
19410 it->start_of_box_run_p = saved_box_start;
19412 /* If stretch_width comes out negative, it means that the
19413 last glyph is only partially visible. In R2L rows, we
19414 want the leftmost glyph to be partially visible, so we
19415 need to give the row the corresponding left offset. */
19416 if (stretch_width < 0)
19417 it->glyph_row->x = stretch_width;
19419 #endif /* HAVE_WINDOW_SYSTEM */
19421 else
19423 /* Save some values that must not be changed. */
19424 int saved_x = it->current_x;
19425 struct text_pos saved_pos;
19426 Lisp_Object saved_object;
19427 enum display_element_type saved_what = it->what;
19428 int saved_face_id = it->face_id;
19430 saved_object = it->object;
19431 saved_pos = it->position;
19433 it->what = IT_CHARACTER;
19434 memset (&it->position, 0, sizeof it->position);
19435 it->object = Qnil;
19436 it->c = it->char_to_display = ' ';
19437 it->len = 1;
19439 if (WINDOW_LEFT_MARGIN_WIDTH (it->w) > 0
19440 && (it->glyph_row->used[LEFT_MARGIN_AREA]
19441 < WINDOW_LEFT_MARGIN_WIDTH (it->w))
19442 && !it->glyph_row->mode_line_p
19443 && default_face->background != FRAME_BACKGROUND_PIXEL (f))
19445 struct glyph *g = it->glyph_row->glyphs[LEFT_MARGIN_AREA];
19446 struct glyph *e = g + it->glyph_row->used[LEFT_MARGIN_AREA];
19448 for (it->current_x = 0; g < e; g++)
19449 it->current_x += g->pixel_width;
19451 it->area = LEFT_MARGIN_AREA;
19452 it->face_id = default_face->id;
19453 while (it->glyph_row->used[LEFT_MARGIN_AREA]
19454 < WINDOW_LEFT_MARGIN_WIDTH (it->w))
19456 PRODUCE_GLYPHS (it);
19457 /* term.c:produce_glyphs advances it->current_x only for
19458 TEXT_AREA. */
19459 it->current_x += it->pixel_width;
19462 it->current_x = saved_x;
19463 it->area = TEXT_AREA;
19466 /* The last row's blank glyphs should get the default face, to
19467 avoid painting the rest of the window with the region face,
19468 if the region ends at ZV. */
19469 if (it->glyph_row->ends_at_zv_p)
19470 it->face_id = default_face->id;
19471 else
19472 it->face_id = face->id;
19473 PRODUCE_GLYPHS (it);
19475 while (it->current_x <= it->last_visible_x)
19476 PRODUCE_GLYPHS (it);
19478 if (WINDOW_RIGHT_MARGIN_WIDTH (it->w) > 0
19479 && (it->glyph_row->used[RIGHT_MARGIN_AREA]
19480 < WINDOW_RIGHT_MARGIN_WIDTH (it->w))
19481 && !it->glyph_row->mode_line_p
19482 && default_face->background != FRAME_BACKGROUND_PIXEL (f))
19484 struct glyph *g = it->glyph_row->glyphs[RIGHT_MARGIN_AREA];
19485 struct glyph *e = g + it->glyph_row->used[RIGHT_MARGIN_AREA];
19487 for ( ; g < e; g++)
19488 it->current_x += g->pixel_width;
19490 it->area = RIGHT_MARGIN_AREA;
19491 it->face_id = default_face->id;
19492 while (it->glyph_row->used[RIGHT_MARGIN_AREA]
19493 < WINDOW_RIGHT_MARGIN_WIDTH (it->w))
19495 PRODUCE_GLYPHS (it);
19496 it->current_x += it->pixel_width;
19499 it->area = TEXT_AREA;
19502 /* Don't count these blanks really. It would let us insert a left
19503 truncation glyph below and make us set the cursor on them, maybe. */
19504 it->current_x = saved_x;
19505 it->object = saved_object;
19506 it->position = saved_pos;
19507 it->what = saved_what;
19508 it->face_id = saved_face_id;
19513 /* Value is non-zero if text starting at CHARPOS in current_buffer is
19514 trailing whitespace. */
19516 static int
19517 trailing_whitespace_p (ptrdiff_t charpos)
19519 ptrdiff_t bytepos = CHAR_TO_BYTE (charpos);
19520 int c = 0;
19522 while (bytepos < ZV_BYTE
19523 && (c = FETCH_CHAR (bytepos),
19524 c == ' ' || c == '\t'))
19525 ++bytepos;
19527 if (bytepos >= ZV_BYTE || c == '\n' || c == '\r')
19529 if (bytepos != PT_BYTE)
19530 return 1;
19532 return 0;
19536 /* Highlight trailing whitespace, if any, in ROW. */
19538 static void
19539 highlight_trailing_whitespace (struct frame *f, struct glyph_row *row)
19541 int used = row->used[TEXT_AREA];
19543 if (used)
19545 struct glyph *start = row->glyphs[TEXT_AREA];
19546 struct glyph *glyph = start + used - 1;
19548 if (row->reversed_p)
19550 /* Right-to-left rows need to be processed in the opposite
19551 direction, so swap the edge pointers. */
19552 glyph = start;
19553 start = row->glyphs[TEXT_AREA] + used - 1;
19556 /* Skip over glyphs inserted to display the cursor at the
19557 end of a line, for extending the face of the last glyph
19558 to the end of the line on terminals, and for truncation
19559 and continuation glyphs. */
19560 if (!row->reversed_p)
19562 while (glyph >= start
19563 && glyph->type == CHAR_GLYPH
19564 && NILP (glyph->object))
19565 --glyph;
19567 else
19569 while (glyph <= start
19570 && glyph->type == CHAR_GLYPH
19571 && NILP (glyph->object))
19572 ++glyph;
19575 /* If last glyph is a space or stretch, and it's trailing
19576 whitespace, set the face of all trailing whitespace glyphs in
19577 IT->glyph_row to `trailing-whitespace'. */
19578 if ((row->reversed_p ? glyph <= start : glyph >= start)
19579 && BUFFERP (glyph->object)
19580 && (glyph->type == STRETCH_GLYPH
19581 || (glyph->type == CHAR_GLYPH
19582 && glyph->u.ch == ' '))
19583 && trailing_whitespace_p (glyph->charpos))
19585 int face_id = lookup_named_face (f, Qtrailing_whitespace, false);
19586 if (face_id < 0)
19587 return;
19589 if (!row->reversed_p)
19591 while (glyph >= start
19592 && BUFFERP (glyph->object)
19593 && (glyph->type == STRETCH_GLYPH
19594 || (glyph->type == CHAR_GLYPH
19595 && glyph->u.ch == ' ')))
19596 (glyph--)->face_id = face_id;
19598 else
19600 while (glyph <= start
19601 && BUFFERP (glyph->object)
19602 && (glyph->type == STRETCH_GLYPH
19603 || (glyph->type == CHAR_GLYPH
19604 && glyph->u.ch == ' ')))
19605 (glyph++)->face_id = face_id;
19612 /* Value is non-zero if glyph row ROW should be
19613 considered to hold the buffer position CHARPOS. */
19615 static int
19616 row_for_charpos_p (struct glyph_row *row, ptrdiff_t charpos)
19618 int result = 1;
19620 if (charpos == CHARPOS (row->end.pos)
19621 || charpos == MATRIX_ROW_END_CHARPOS (row))
19623 /* Suppose the row ends on a string.
19624 Unless the row is continued, that means it ends on a newline
19625 in the string. If it's anything other than a display string
19626 (e.g., a before-string from an overlay), we don't want the
19627 cursor there. (This heuristic seems to give the optimal
19628 behavior for the various types of multi-line strings.)
19629 One exception: if the string has `cursor' property on one of
19630 its characters, we _do_ want the cursor there. */
19631 if (CHARPOS (row->end.string_pos) >= 0)
19633 if (row->continued_p)
19634 result = 1;
19635 else
19637 /* Check for `display' property. */
19638 struct glyph *beg = row->glyphs[TEXT_AREA];
19639 struct glyph *end = beg + row->used[TEXT_AREA] - 1;
19640 struct glyph *glyph;
19642 result = 0;
19643 for (glyph = end; glyph >= beg; --glyph)
19644 if (STRINGP (glyph->object))
19646 Lisp_Object prop
19647 = Fget_char_property (make_number (charpos),
19648 Qdisplay, Qnil);
19649 result =
19650 (!NILP (prop)
19651 && display_prop_string_p (prop, glyph->object));
19652 /* If there's a `cursor' property on one of the
19653 string's characters, this row is a cursor row,
19654 even though this is not a display string. */
19655 if (!result)
19657 Lisp_Object s = glyph->object;
19659 for ( ; glyph >= beg && EQ (glyph->object, s); --glyph)
19661 ptrdiff_t gpos = glyph->charpos;
19663 if (!NILP (Fget_char_property (make_number (gpos),
19664 Qcursor, s)))
19666 result = 1;
19667 break;
19671 break;
19675 else if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
19677 /* If the row ends in middle of a real character,
19678 and the line is continued, we want the cursor here.
19679 That's because CHARPOS (ROW->end.pos) would equal
19680 PT if PT is before the character. */
19681 if (!row->ends_in_ellipsis_p)
19682 result = row->continued_p;
19683 else
19684 /* If the row ends in an ellipsis, then
19685 CHARPOS (ROW->end.pos) will equal point after the
19686 invisible text. We want that position to be displayed
19687 after the ellipsis. */
19688 result = 0;
19690 /* If the row ends at ZV, display the cursor at the end of that
19691 row instead of at the start of the row below. */
19692 else if (row->ends_at_zv_p)
19693 result = 1;
19694 else
19695 result = 0;
19698 return result;
19701 /* Value is non-zero if glyph row ROW should be
19702 used to hold the cursor. */
19704 static int
19705 cursor_row_p (struct glyph_row *row)
19707 return row_for_charpos_p (row, PT);
19712 /* Push the property PROP so that it will be rendered at the current
19713 position in IT. Return 1 if PROP was successfully pushed, 0
19714 otherwise. Called from handle_line_prefix to handle the
19715 `line-prefix' and `wrap-prefix' properties. */
19717 static int
19718 push_prefix_prop (struct it *it, Lisp_Object prop)
19720 struct text_pos pos =
19721 STRINGP (it->string) ? it->current.string_pos : it->current.pos;
19723 eassert (it->method == GET_FROM_BUFFER
19724 || it->method == GET_FROM_DISPLAY_VECTOR
19725 || it->method == GET_FROM_STRING);
19727 /* We need to save the current buffer/string position, so it will be
19728 restored by pop_it, because iterate_out_of_display_property
19729 depends on that being set correctly, but some situations leave
19730 it->position not yet set when this function is called. */
19731 push_it (it, &pos);
19733 if (STRINGP (prop))
19735 if (SCHARS (prop) == 0)
19737 pop_it (it);
19738 return 0;
19741 it->string = prop;
19742 it->string_from_prefix_prop_p = 1;
19743 it->multibyte_p = STRING_MULTIBYTE (it->string);
19744 it->current.overlay_string_index = -1;
19745 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
19746 it->end_charpos = it->string_nchars = SCHARS (it->string);
19747 it->method = GET_FROM_STRING;
19748 it->stop_charpos = 0;
19749 it->prev_stop = 0;
19750 it->base_level_stop = 0;
19752 /* Force paragraph direction to be that of the parent
19753 buffer/string. */
19754 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
19755 it->paragraph_embedding = it->bidi_it.paragraph_dir;
19756 else
19757 it->paragraph_embedding = L2R;
19759 /* Set up the bidi iterator for this display string. */
19760 if (it->bidi_p)
19762 it->bidi_it.string.lstring = it->string;
19763 it->bidi_it.string.s = NULL;
19764 it->bidi_it.string.schars = it->end_charpos;
19765 it->bidi_it.string.bufpos = IT_CHARPOS (*it);
19766 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
19767 it->bidi_it.string.unibyte = !it->multibyte_p;
19768 it->bidi_it.w = it->w;
19769 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
19772 else if (CONSP (prop) && EQ (XCAR (prop), Qspace))
19774 it->method = GET_FROM_STRETCH;
19775 it->object = prop;
19777 #ifdef HAVE_WINDOW_SYSTEM
19778 else if (IMAGEP (prop))
19780 it->what = IT_IMAGE;
19781 it->image_id = lookup_image (it->f, prop);
19782 it->method = GET_FROM_IMAGE;
19784 #endif /* HAVE_WINDOW_SYSTEM */
19785 else
19787 pop_it (it); /* bogus display property, give up */
19788 return 0;
19791 return 1;
19794 /* Return the character-property PROP at the current position in IT. */
19796 static Lisp_Object
19797 get_it_property (struct it *it, Lisp_Object prop)
19799 Lisp_Object position, object = it->object;
19801 if (STRINGP (object))
19802 position = make_number (IT_STRING_CHARPOS (*it));
19803 else if (BUFFERP (object))
19805 position = make_number (IT_CHARPOS (*it));
19806 object = it->window;
19808 else
19809 return Qnil;
19811 return Fget_char_property (position, prop, object);
19814 /* See if there's a line- or wrap-prefix, and if so, push it on IT. */
19816 static void
19817 handle_line_prefix (struct it *it)
19819 Lisp_Object prefix;
19821 if (it->continuation_lines_width > 0)
19823 prefix = get_it_property (it, Qwrap_prefix);
19824 if (NILP (prefix))
19825 prefix = Vwrap_prefix;
19827 else
19829 prefix = get_it_property (it, Qline_prefix);
19830 if (NILP (prefix))
19831 prefix = Vline_prefix;
19833 if (! NILP (prefix) && push_prefix_prop (it, prefix))
19835 /* If the prefix is wider than the window, and we try to wrap
19836 it, it would acquire its own wrap prefix, and so on till the
19837 iterator stack overflows. So, don't wrap the prefix. */
19838 it->line_wrap = TRUNCATE;
19839 it->avoid_cursor_p = 1;
19845 /* Remove N glyphs at the start of a reversed IT->glyph_row. Called
19846 only for R2L lines from display_line and display_string, when they
19847 decide that too many glyphs were produced by PRODUCE_GLYPHS, and
19848 the line/string needs to be continued on the next glyph row. */
19849 static void
19850 unproduce_glyphs (struct it *it, int n)
19852 struct glyph *glyph, *end;
19854 eassert (it->glyph_row);
19855 eassert (it->glyph_row->reversed_p);
19856 eassert (it->area == TEXT_AREA);
19857 eassert (n <= it->glyph_row->used[TEXT_AREA]);
19859 if (n > it->glyph_row->used[TEXT_AREA])
19860 n = it->glyph_row->used[TEXT_AREA];
19861 glyph = it->glyph_row->glyphs[TEXT_AREA] + n;
19862 end = it->glyph_row->glyphs[TEXT_AREA] + it->glyph_row->used[TEXT_AREA];
19863 for ( ; glyph < end; glyph++)
19864 glyph[-n] = *glyph;
19867 /* Find the positions in a bidi-reordered ROW to serve as ROW->minpos
19868 and ROW->maxpos. */
19869 static void
19870 find_row_edges (struct it *it, struct glyph_row *row,
19871 ptrdiff_t min_pos, ptrdiff_t min_bpos,
19872 ptrdiff_t max_pos, ptrdiff_t max_bpos)
19874 /* FIXME: Revisit this when glyph ``spilling'' in continuation
19875 lines' rows is implemented for bidi-reordered rows. */
19877 /* ROW->minpos is the value of min_pos, the minimal buffer position
19878 we have in ROW, or ROW->start.pos if that is smaller. */
19879 if (min_pos <= ZV && min_pos < row->start.pos.charpos)
19880 SET_TEXT_POS (row->minpos, min_pos, min_bpos);
19881 else
19882 /* We didn't find buffer positions smaller than ROW->start, or
19883 didn't find _any_ valid buffer positions in any of the glyphs,
19884 so we must trust the iterator's computed positions. */
19885 row->minpos = row->start.pos;
19886 if (max_pos <= 0)
19888 max_pos = CHARPOS (it->current.pos);
19889 max_bpos = BYTEPOS (it->current.pos);
19892 /* Here are the various use-cases for ending the row, and the
19893 corresponding values for ROW->maxpos:
19895 Line ends in a newline from buffer eol_pos + 1
19896 Line is continued from buffer max_pos + 1
19897 Line is truncated on right it->current.pos
19898 Line ends in a newline from string max_pos + 1(*)
19899 (*) + 1 only when line ends in a forward scan
19900 Line is continued from string max_pos
19901 Line is continued from display vector max_pos
19902 Line is entirely from a string min_pos == max_pos
19903 Line is entirely from a display vector min_pos == max_pos
19904 Line that ends at ZV ZV
19906 If you discover other use-cases, please add them here as
19907 appropriate. */
19908 if (row->ends_at_zv_p)
19909 row->maxpos = it->current.pos;
19910 else if (row->used[TEXT_AREA])
19912 int seen_this_string = 0;
19913 struct glyph_row *r1 = row - 1;
19915 /* Did we see the same display string on the previous row? */
19916 if (STRINGP (it->object)
19917 /* this is not the first row */
19918 && row > it->w->desired_matrix->rows
19919 /* previous row is not the header line */
19920 && !r1->mode_line_p
19921 /* previous row also ends in a newline from a string */
19922 && r1->ends_in_newline_from_string_p)
19924 struct glyph *start, *end;
19926 /* Search for the last glyph of the previous row that came
19927 from buffer or string. Depending on whether the row is
19928 L2R or R2L, we need to process it front to back or the
19929 other way round. */
19930 if (!r1->reversed_p)
19932 start = r1->glyphs[TEXT_AREA];
19933 end = start + r1->used[TEXT_AREA];
19934 /* Glyphs inserted by redisplay have nil as their object. */
19935 while (end > start
19936 && NILP ((end - 1)->object)
19937 && (end - 1)->charpos <= 0)
19938 --end;
19939 if (end > start)
19941 if (EQ ((end - 1)->object, it->object))
19942 seen_this_string = 1;
19944 else
19945 /* If all the glyphs of the previous row were inserted
19946 by redisplay, it means the previous row was
19947 produced from a single newline, which is only
19948 possible if that newline came from the same string
19949 as the one which produced this ROW. */
19950 seen_this_string = 1;
19952 else
19954 end = r1->glyphs[TEXT_AREA] - 1;
19955 start = end + r1->used[TEXT_AREA];
19956 while (end < start
19957 && NILP ((end + 1)->object)
19958 && (end + 1)->charpos <= 0)
19959 ++end;
19960 if (end < start)
19962 if (EQ ((end + 1)->object, it->object))
19963 seen_this_string = 1;
19965 else
19966 seen_this_string = 1;
19969 /* Take note of each display string that covers a newline only
19970 once, the first time we see it. This is for when a display
19971 string includes more than one newline in it. */
19972 if (row->ends_in_newline_from_string_p && !seen_this_string)
19974 /* If we were scanning the buffer forward when we displayed
19975 the string, we want to account for at least one buffer
19976 position that belongs to this row (position covered by
19977 the display string), so that cursor positioning will
19978 consider this row as a candidate when point is at the end
19979 of the visual line represented by this row. This is not
19980 required when scanning back, because max_pos will already
19981 have a much larger value. */
19982 if (CHARPOS (row->end.pos) > max_pos)
19983 INC_BOTH (max_pos, max_bpos);
19984 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
19986 else if (CHARPOS (it->eol_pos) > 0)
19987 SET_TEXT_POS (row->maxpos,
19988 CHARPOS (it->eol_pos) + 1, BYTEPOS (it->eol_pos) + 1);
19989 else if (row->continued_p)
19991 /* If max_pos is different from IT's current position, it
19992 means IT->method does not belong to the display element
19993 at max_pos. However, it also means that the display
19994 element at max_pos was displayed in its entirety on this
19995 line, which is equivalent to saying that the next line
19996 starts at the next buffer position. */
19997 if (IT_CHARPOS (*it) == max_pos && it->method != GET_FROM_BUFFER)
19998 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
19999 else
20001 INC_BOTH (max_pos, max_bpos);
20002 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
20005 else if (row->truncated_on_right_p)
20006 /* display_line already called reseat_at_next_visible_line_start,
20007 which puts the iterator at the beginning of the next line, in
20008 the logical order. */
20009 row->maxpos = it->current.pos;
20010 else if (max_pos == min_pos && it->method != GET_FROM_BUFFER)
20011 /* A line that is entirely from a string/image/stretch... */
20012 row->maxpos = row->minpos;
20013 else
20014 emacs_abort ();
20016 else
20017 row->maxpos = it->current.pos;
20020 /* Construct the glyph row IT->glyph_row in the desired matrix of
20021 IT->w from text at the current position of IT. See dispextern.h
20022 for an overview of struct it. Value is non-zero if
20023 IT->glyph_row displays text, as opposed to a line displaying ZV
20024 only. */
20026 static int
20027 display_line (struct it *it)
20029 struct glyph_row *row = it->glyph_row;
20030 Lisp_Object overlay_arrow_string;
20031 struct it wrap_it;
20032 void *wrap_data = NULL;
20033 int may_wrap = 0, wrap_x IF_LINT (= 0);
20034 int wrap_row_used = -1;
20035 int wrap_row_ascent IF_LINT (= 0), wrap_row_height IF_LINT (= 0);
20036 int wrap_row_phys_ascent IF_LINT (= 0), wrap_row_phys_height IF_LINT (= 0);
20037 int wrap_row_extra_line_spacing IF_LINT (= 0);
20038 ptrdiff_t wrap_row_min_pos IF_LINT (= 0), wrap_row_min_bpos IF_LINT (= 0);
20039 ptrdiff_t wrap_row_max_pos IF_LINT (= 0), wrap_row_max_bpos IF_LINT (= 0);
20040 int cvpos;
20041 ptrdiff_t min_pos = ZV + 1, max_pos = 0;
20042 ptrdiff_t min_bpos IF_LINT (= 0), max_bpos IF_LINT (= 0);
20043 bool pending_handle_line_prefix = false;
20045 /* We always start displaying at hpos zero even if hscrolled. */
20046 eassert (it->hpos == 0 && it->current_x == 0);
20048 if (MATRIX_ROW_VPOS (row, it->w->desired_matrix)
20049 >= it->w->desired_matrix->nrows)
20051 it->w->nrows_scale_factor++;
20052 it->f->fonts_changed = 1;
20053 return 0;
20056 /* Clear the result glyph row and enable it. */
20057 prepare_desired_row (it->w, row, false);
20059 row->y = it->current_y;
20060 row->start = it->start;
20061 row->continuation_lines_width = it->continuation_lines_width;
20062 row->displays_text_p = 1;
20063 row->starts_in_middle_of_char_p = it->starts_in_middle_of_char_p;
20064 it->starts_in_middle_of_char_p = 0;
20066 /* Arrange the overlays nicely for our purposes. Usually, we call
20067 display_line on only one line at a time, in which case this
20068 can't really hurt too much, or we call it on lines which appear
20069 one after another in the buffer, in which case all calls to
20070 recenter_overlay_lists but the first will be pretty cheap. */
20071 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
20073 /* Move over display elements that are not visible because we are
20074 hscrolled. This may stop at an x-position < IT->first_visible_x
20075 if the first glyph is partially visible or if we hit a line end. */
20076 if (it->current_x < it->first_visible_x)
20078 enum move_it_result move_result;
20080 this_line_min_pos = row->start.pos;
20081 move_result = move_it_in_display_line_to (it, ZV, it->first_visible_x,
20082 MOVE_TO_POS | MOVE_TO_X);
20083 /* If we are under a large hscroll, move_it_in_display_line_to
20084 could hit the end of the line without reaching
20085 it->first_visible_x. Pretend that we did reach it. This is
20086 especially important on a TTY, where we will call
20087 extend_face_to_end_of_line, which needs to know how many
20088 blank glyphs to produce. */
20089 if (it->current_x < it->first_visible_x
20090 && (move_result == MOVE_NEWLINE_OR_CR
20091 || move_result == MOVE_POS_MATCH_OR_ZV))
20092 it->current_x = it->first_visible_x;
20094 /* Record the smallest positions seen while we moved over
20095 display elements that are not visible. This is needed by
20096 redisplay_internal for optimizing the case where the cursor
20097 stays inside the same line. The rest of this function only
20098 considers positions that are actually displayed, so
20099 RECORD_MAX_MIN_POS will not otherwise record positions that
20100 are hscrolled to the left of the left edge of the window. */
20101 min_pos = CHARPOS (this_line_min_pos);
20102 min_bpos = BYTEPOS (this_line_min_pos);
20104 else if (it->area == TEXT_AREA)
20106 /* We only do this when not calling move_it_in_display_line_to
20107 above, because that function calls itself handle_line_prefix. */
20108 handle_line_prefix (it);
20110 else
20112 /* Line-prefix and wrap-prefix are always displayed in the text
20113 area. But if this is the first call to display_line after
20114 init_iterator, the iterator might have been set up to write
20115 into a marginal area, e.g. if the line begins with some
20116 display property that writes to the margins. So we need to
20117 wait with the call to handle_line_prefix until whatever
20118 writes to the margin has done its job. */
20119 pending_handle_line_prefix = true;
20122 /* Get the initial row height. This is either the height of the
20123 text hscrolled, if there is any, or zero. */
20124 row->ascent = it->max_ascent;
20125 row->height = it->max_ascent + it->max_descent;
20126 row->phys_ascent = it->max_phys_ascent;
20127 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
20128 row->extra_line_spacing = it->max_extra_line_spacing;
20130 /* Utility macro to record max and min buffer positions seen until now. */
20131 #define RECORD_MAX_MIN_POS(IT) \
20132 do \
20134 int composition_p = !STRINGP ((IT)->string) \
20135 && ((IT)->what == IT_COMPOSITION); \
20136 ptrdiff_t current_pos = \
20137 composition_p ? (IT)->cmp_it.charpos \
20138 : IT_CHARPOS (*(IT)); \
20139 ptrdiff_t current_bpos = \
20140 composition_p ? CHAR_TO_BYTE (current_pos) \
20141 : IT_BYTEPOS (*(IT)); \
20142 if (current_pos < min_pos) \
20144 min_pos = current_pos; \
20145 min_bpos = current_bpos; \
20147 if (IT_CHARPOS (*it) > max_pos) \
20149 max_pos = IT_CHARPOS (*it); \
20150 max_bpos = IT_BYTEPOS (*it); \
20153 while (0)
20155 /* Loop generating characters. The loop is left with IT on the next
20156 character to display. */
20157 while (1)
20159 int n_glyphs_before, hpos_before, x_before;
20160 int x, nglyphs;
20161 int ascent = 0, descent = 0, phys_ascent = 0, phys_descent = 0;
20163 /* Retrieve the next thing to display. Value is zero if end of
20164 buffer reached. */
20165 if (!get_next_display_element (it))
20167 /* Maybe add a space at the end of this line that is used to
20168 display the cursor there under X. Set the charpos of the
20169 first glyph of blank lines not corresponding to any text
20170 to -1. */
20171 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
20172 row->exact_window_width_line_p = 1;
20173 else if ((append_space_for_newline (it, 1) && row->used[TEXT_AREA] == 1)
20174 || row->used[TEXT_AREA] == 0)
20176 row->glyphs[TEXT_AREA]->charpos = -1;
20177 row->displays_text_p = 0;
20179 if (!NILP (BVAR (XBUFFER (it->w->contents), indicate_empty_lines))
20180 && (!MINI_WINDOW_P (it->w)
20181 || (minibuf_level && EQ (it->window, minibuf_window))))
20182 row->indicate_empty_line_p = 1;
20185 it->continuation_lines_width = 0;
20186 row->ends_at_zv_p = 1;
20187 /* A row that displays right-to-left text must always have
20188 its last face extended all the way to the end of line,
20189 even if this row ends in ZV, because we still write to
20190 the screen left to right. We also need to extend the
20191 last face if the default face is remapped to some
20192 different face, otherwise the functions that clear
20193 portions of the screen will clear with the default face's
20194 background color. */
20195 if (row->reversed_p
20196 || lookup_basic_face (it->f, DEFAULT_FACE_ID) != DEFAULT_FACE_ID)
20197 extend_face_to_end_of_line (it);
20198 break;
20201 /* Now, get the metrics of what we want to display. This also
20202 generates glyphs in `row' (which is IT->glyph_row). */
20203 n_glyphs_before = row->used[TEXT_AREA];
20204 x = it->current_x;
20206 /* Remember the line height so far in case the next element doesn't
20207 fit on the line. */
20208 if (it->line_wrap != TRUNCATE)
20210 ascent = it->max_ascent;
20211 descent = it->max_descent;
20212 phys_ascent = it->max_phys_ascent;
20213 phys_descent = it->max_phys_descent;
20215 if (it->line_wrap == WORD_WRAP && it->area == TEXT_AREA)
20217 if (IT_DISPLAYING_WHITESPACE (it))
20218 may_wrap = 1;
20219 else if (may_wrap)
20221 SAVE_IT (wrap_it, *it, wrap_data);
20222 wrap_x = x;
20223 wrap_row_used = row->used[TEXT_AREA];
20224 wrap_row_ascent = row->ascent;
20225 wrap_row_height = row->height;
20226 wrap_row_phys_ascent = row->phys_ascent;
20227 wrap_row_phys_height = row->phys_height;
20228 wrap_row_extra_line_spacing = row->extra_line_spacing;
20229 wrap_row_min_pos = min_pos;
20230 wrap_row_min_bpos = min_bpos;
20231 wrap_row_max_pos = max_pos;
20232 wrap_row_max_bpos = max_bpos;
20233 may_wrap = 0;
20238 PRODUCE_GLYPHS (it);
20240 /* If this display element was in marginal areas, continue with
20241 the next one. */
20242 if (it->area != TEXT_AREA)
20244 row->ascent = max (row->ascent, it->max_ascent);
20245 row->height = max (row->height, it->max_ascent + it->max_descent);
20246 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
20247 row->phys_height = max (row->phys_height,
20248 it->max_phys_ascent + it->max_phys_descent);
20249 row->extra_line_spacing = max (row->extra_line_spacing,
20250 it->max_extra_line_spacing);
20251 set_iterator_to_next (it, 1);
20252 /* If we didn't handle the line/wrap prefix above, and the
20253 call to set_iterator_to_next just switched to TEXT_AREA,
20254 process the prefix now. */
20255 if (it->area == TEXT_AREA && pending_handle_line_prefix)
20257 pending_handle_line_prefix = false;
20258 handle_line_prefix (it);
20260 continue;
20263 /* Does the display element fit on the line? If we truncate
20264 lines, we should draw past the right edge of the window. If
20265 we don't truncate, we want to stop so that we can display the
20266 continuation glyph before the right margin. If lines are
20267 continued, there are two possible strategies for characters
20268 resulting in more than 1 glyph (e.g. tabs): Display as many
20269 glyphs as possible in this line and leave the rest for the
20270 continuation line, or display the whole element in the next
20271 line. Original redisplay did the former, so we do it also. */
20272 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
20273 hpos_before = it->hpos;
20274 x_before = x;
20276 if (/* Not a newline. */
20277 nglyphs > 0
20278 /* Glyphs produced fit entirely in the line. */
20279 && it->current_x < it->last_visible_x)
20281 it->hpos += nglyphs;
20282 row->ascent = max (row->ascent, it->max_ascent);
20283 row->height = max (row->height, it->max_ascent + it->max_descent);
20284 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
20285 row->phys_height = max (row->phys_height,
20286 it->max_phys_ascent + it->max_phys_descent);
20287 row->extra_line_spacing = max (row->extra_line_spacing,
20288 it->max_extra_line_spacing);
20289 if (it->current_x - it->pixel_width < it->first_visible_x
20290 /* In R2L rows, we arrange in extend_face_to_end_of_line
20291 to add a right offset to the line, by a suitable
20292 change to the stretch glyph that is the leftmost
20293 glyph of the line. */
20294 && !row->reversed_p)
20295 row->x = x - it->first_visible_x;
20296 /* Record the maximum and minimum buffer positions seen so
20297 far in glyphs that will be displayed by this row. */
20298 if (it->bidi_p)
20299 RECORD_MAX_MIN_POS (it);
20301 else
20303 int i, new_x;
20304 struct glyph *glyph;
20306 for (i = 0; i < nglyphs; ++i, x = new_x)
20308 /* Identify the glyphs added by the last call to
20309 PRODUCE_GLYPHS. In R2L rows, they are prepended to
20310 the previous glyphs. */
20311 if (!row->reversed_p)
20312 glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
20313 else
20314 glyph = row->glyphs[TEXT_AREA] + nglyphs - 1 - i;
20315 new_x = x + glyph->pixel_width;
20317 if (/* Lines are continued. */
20318 it->line_wrap != TRUNCATE
20319 && (/* Glyph doesn't fit on the line. */
20320 new_x > it->last_visible_x
20321 /* Or it fits exactly on a window system frame. */
20322 || (new_x == it->last_visible_x
20323 && FRAME_WINDOW_P (it->f)
20324 && (row->reversed_p
20325 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
20326 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)))))
20328 /* End of a continued line. */
20330 if (it->hpos == 0
20331 || (new_x == it->last_visible_x
20332 && FRAME_WINDOW_P (it->f)
20333 && (row->reversed_p
20334 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
20335 : WINDOW_RIGHT_FRINGE_WIDTH (it->w))))
20337 /* Current glyph is the only one on the line or
20338 fits exactly on the line. We must continue
20339 the line because we can't draw the cursor
20340 after the glyph. */
20341 row->continued_p = 1;
20342 it->current_x = new_x;
20343 it->continuation_lines_width += new_x;
20344 ++it->hpos;
20345 if (i == nglyphs - 1)
20347 /* If line-wrap is on, check if a previous
20348 wrap point was found. */
20349 if (!IT_OVERFLOW_NEWLINE_INTO_FRINGE (it)
20350 && wrap_row_used > 0
20351 /* Even if there is a previous wrap
20352 point, continue the line here as
20353 usual, if (i) the previous character
20354 was a space or tab AND (ii) the
20355 current character is not. */
20356 && (!may_wrap
20357 || IT_DISPLAYING_WHITESPACE (it)))
20358 goto back_to_wrap;
20360 /* Record the maximum and minimum buffer
20361 positions seen so far in glyphs that will be
20362 displayed by this row. */
20363 if (it->bidi_p)
20364 RECORD_MAX_MIN_POS (it);
20365 set_iterator_to_next (it, 1);
20366 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
20368 if (!get_next_display_element (it))
20370 row->exact_window_width_line_p = 1;
20371 it->continuation_lines_width = 0;
20372 row->continued_p = 0;
20373 row->ends_at_zv_p = 1;
20375 else if (ITERATOR_AT_END_OF_LINE_P (it))
20377 row->continued_p = 0;
20378 row->exact_window_width_line_p = 1;
20380 /* If line-wrap is on, check if a
20381 previous wrap point was found. */
20382 else if (wrap_row_used > 0
20383 /* Even if there is a previous wrap
20384 point, continue the line here as
20385 usual, if (i) the previous character
20386 was a space or tab AND (ii) the
20387 current character is not. */
20388 && (!may_wrap
20389 || IT_DISPLAYING_WHITESPACE (it)))
20390 goto back_to_wrap;
20394 else if (it->bidi_p)
20395 RECORD_MAX_MIN_POS (it);
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 (CHAR_GLYPH_PADDING_P (*glyph)
20401 && !FRAME_WINDOW_P (it->f))
20403 /* A padding glyph that doesn't fit on this line.
20404 This means the whole character doesn't fit
20405 on the line. */
20406 if (row->reversed_p)
20407 unproduce_glyphs (it, row->used[TEXT_AREA]
20408 - n_glyphs_before);
20409 row->used[TEXT_AREA] = n_glyphs_before;
20411 /* Fill the rest of the row with continuation
20412 glyphs like in 20.x. */
20413 while (row->glyphs[TEXT_AREA] + row->used[TEXT_AREA]
20414 < row->glyphs[1 + TEXT_AREA])
20415 produce_special_glyphs (it, IT_CONTINUATION);
20417 row->continued_p = 1;
20418 it->current_x = x_before;
20419 it->continuation_lines_width += x_before;
20421 /* Restore the height to what it was before the
20422 element not fitting on the line. */
20423 it->max_ascent = ascent;
20424 it->max_descent = descent;
20425 it->max_phys_ascent = phys_ascent;
20426 it->max_phys_descent = phys_descent;
20427 if (WINDOW_LEFT_MARGIN_WIDTH (it->w) > 0
20428 || WINDOW_RIGHT_MARGIN_WIDTH (it->w) > 0)
20429 extend_face_to_end_of_line (it);
20431 else if (wrap_row_used > 0)
20433 back_to_wrap:
20434 if (row->reversed_p)
20435 unproduce_glyphs (it,
20436 row->used[TEXT_AREA] - wrap_row_used);
20437 RESTORE_IT (it, &wrap_it, wrap_data);
20438 it->continuation_lines_width += wrap_x;
20439 row->used[TEXT_AREA] = wrap_row_used;
20440 row->ascent = wrap_row_ascent;
20441 row->height = wrap_row_height;
20442 row->phys_ascent = wrap_row_phys_ascent;
20443 row->phys_height = wrap_row_phys_height;
20444 row->extra_line_spacing = wrap_row_extra_line_spacing;
20445 min_pos = wrap_row_min_pos;
20446 min_bpos = wrap_row_min_bpos;
20447 max_pos = wrap_row_max_pos;
20448 max_bpos = wrap_row_max_bpos;
20449 row->continued_p = 1;
20450 row->ends_at_zv_p = 0;
20451 row->exact_window_width_line_p = 0;
20452 it->continuation_lines_width += x;
20454 /* Make sure that a non-default face is extended
20455 up to the right margin of the window. */
20456 extend_face_to_end_of_line (it);
20458 else if (it->c == '\t' && FRAME_WINDOW_P (it->f))
20460 /* A TAB that extends past the right edge of the
20461 window. This produces a single glyph on
20462 window system frames. We leave the glyph in
20463 this row and let it fill the row, but don't
20464 consume the TAB. */
20465 if ((row->reversed_p
20466 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
20467 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
20468 produce_special_glyphs (it, IT_CONTINUATION);
20469 it->continuation_lines_width += it->last_visible_x;
20470 row->ends_in_middle_of_char_p = 1;
20471 row->continued_p = 1;
20472 glyph->pixel_width = it->last_visible_x - x;
20473 it->starts_in_middle_of_char_p = 1;
20474 if (WINDOW_LEFT_MARGIN_WIDTH (it->w) > 0
20475 || WINDOW_RIGHT_MARGIN_WIDTH (it->w) > 0)
20476 extend_face_to_end_of_line (it);
20478 else
20480 /* Something other than a TAB that draws past
20481 the right edge of the window. Restore
20482 positions to values before the element. */
20483 if (row->reversed_p)
20484 unproduce_glyphs (it, row->used[TEXT_AREA]
20485 - (n_glyphs_before + i));
20486 row->used[TEXT_AREA] = n_glyphs_before + i;
20488 /* Display continuation glyphs. */
20489 it->current_x = x_before;
20490 it->continuation_lines_width += x;
20491 if (!FRAME_WINDOW_P (it->f)
20492 || (row->reversed_p
20493 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
20494 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
20495 produce_special_glyphs (it, IT_CONTINUATION);
20496 row->continued_p = 1;
20498 extend_face_to_end_of_line (it);
20500 if (nglyphs > 1 && i > 0)
20502 row->ends_in_middle_of_char_p = 1;
20503 it->starts_in_middle_of_char_p = 1;
20506 /* Restore the height to what it was before the
20507 element not fitting on the line. */
20508 it->max_ascent = ascent;
20509 it->max_descent = descent;
20510 it->max_phys_ascent = phys_ascent;
20511 it->max_phys_descent = phys_descent;
20514 break;
20516 else if (new_x > it->first_visible_x)
20518 /* Increment number of glyphs actually displayed. */
20519 ++it->hpos;
20521 /* Record the maximum and minimum buffer positions
20522 seen so far in glyphs that will be displayed by
20523 this row. */
20524 if (it->bidi_p)
20525 RECORD_MAX_MIN_POS (it);
20527 if (x < it->first_visible_x && !row->reversed_p)
20528 /* Glyph is partially visible, i.e. row starts at
20529 negative X position. Don't do that in R2L
20530 rows, where we arrange to add a right offset to
20531 the line in extend_face_to_end_of_line, by a
20532 suitable change to the stretch glyph that is
20533 the leftmost glyph of the line. */
20534 row->x = x - it->first_visible_x;
20535 /* When the last glyph of an R2L row only fits
20536 partially on the line, we need to set row->x to a
20537 negative offset, so that the leftmost glyph is
20538 the one that is partially visible. But if we are
20539 going to produce the truncation glyph, this will
20540 be taken care of in produce_special_glyphs. */
20541 if (row->reversed_p
20542 && new_x > it->last_visible_x
20543 && !(it->line_wrap == TRUNCATE
20544 && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0))
20546 eassert (FRAME_WINDOW_P (it->f));
20547 row->x = it->last_visible_x - new_x;
20550 else
20552 /* Glyph is completely off the left margin of the
20553 window. This should not happen because of the
20554 move_it_in_display_line at the start of this
20555 function, unless the text display area of the
20556 window is empty. */
20557 eassert (it->first_visible_x <= it->last_visible_x);
20560 /* Even if this display element produced no glyphs at all,
20561 we want to record its position. */
20562 if (it->bidi_p && nglyphs == 0)
20563 RECORD_MAX_MIN_POS (it);
20565 row->ascent = max (row->ascent, it->max_ascent);
20566 row->height = max (row->height, it->max_ascent + it->max_descent);
20567 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
20568 row->phys_height = max (row->phys_height,
20569 it->max_phys_ascent + it->max_phys_descent);
20570 row->extra_line_spacing = max (row->extra_line_spacing,
20571 it->max_extra_line_spacing);
20573 /* End of this display line if row is continued. */
20574 if (row->continued_p || row->ends_at_zv_p)
20575 break;
20578 at_end_of_line:
20579 /* Is this a line end? If yes, we're also done, after making
20580 sure that a non-default face is extended up to the right
20581 margin of the window. */
20582 if (ITERATOR_AT_END_OF_LINE_P (it))
20584 int used_before = row->used[TEXT_AREA];
20586 row->ends_in_newline_from_string_p = STRINGP (it->object);
20588 /* Add a space at the end of the line that is used to
20589 display the cursor there. */
20590 if (!IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
20591 append_space_for_newline (it, 0);
20593 /* Extend the face to the end of the line. */
20594 extend_face_to_end_of_line (it);
20596 /* Make sure we have the position. */
20597 if (used_before == 0)
20598 row->glyphs[TEXT_AREA]->charpos = CHARPOS (it->position);
20600 /* Record the position of the newline, for use in
20601 find_row_edges. */
20602 it->eol_pos = it->current.pos;
20604 /* Consume the line end. This skips over invisible lines. */
20605 set_iterator_to_next (it, 1);
20606 it->continuation_lines_width = 0;
20607 break;
20610 /* Proceed with next display element. Note that this skips
20611 over lines invisible because of selective display. */
20612 set_iterator_to_next (it, 1);
20614 /* If we truncate lines, we are done when the last displayed
20615 glyphs reach past the right margin of the window. */
20616 if (it->line_wrap == TRUNCATE
20617 && ((FRAME_WINDOW_P (it->f)
20618 /* Images are preprocessed in produce_image_glyph such
20619 that they are cropped at the right edge of the
20620 window, so an image glyph will always end exactly at
20621 last_visible_x, even if there's no right fringe. */
20622 && ((row->reversed_p
20623 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
20624 : WINDOW_RIGHT_FRINGE_WIDTH (it->w))
20625 || it->what == IT_IMAGE))
20626 ? (it->current_x >= it->last_visible_x)
20627 : (it->current_x > it->last_visible_x)))
20629 /* Maybe add truncation glyphs. */
20630 if (!FRAME_WINDOW_P (it->f)
20631 || (row->reversed_p
20632 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
20633 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
20635 int i, n;
20637 if (!row->reversed_p)
20639 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
20640 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
20641 break;
20643 else
20645 for (i = 0; i < row->used[TEXT_AREA]; i++)
20646 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
20647 break;
20648 /* Remove any padding glyphs at the front of ROW, to
20649 make room for the truncation glyphs we will be
20650 adding below. The loop below always inserts at
20651 least one truncation glyph, so also remove the
20652 last glyph added to ROW. */
20653 unproduce_glyphs (it, i + 1);
20654 /* Adjust i for the loop below. */
20655 i = row->used[TEXT_AREA] - (i + 1);
20658 /* produce_special_glyphs overwrites the last glyph, so
20659 we don't want that if we want to keep that last
20660 glyph, which means it's an image. */
20661 if (it->current_x > it->last_visible_x)
20663 it->current_x = x_before;
20664 if (!FRAME_WINDOW_P (it->f))
20666 for (n = row->used[TEXT_AREA]; i < n; ++i)
20668 row->used[TEXT_AREA] = i;
20669 produce_special_glyphs (it, IT_TRUNCATION);
20672 else
20674 row->used[TEXT_AREA] = i;
20675 produce_special_glyphs (it, IT_TRUNCATION);
20677 it->hpos = hpos_before;
20680 else if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
20682 /* Don't truncate if we can overflow newline into fringe. */
20683 if (!get_next_display_element (it))
20685 it->continuation_lines_width = 0;
20686 row->ends_at_zv_p = 1;
20687 row->exact_window_width_line_p = 1;
20688 break;
20690 if (ITERATOR_AT_END_OF_LINE_P (it))
20692 row->exact_window_width_line_p = 1;
20693 goto at_end_of_line;
20695 it->current_x = x_before;
20696 it->hpos = hpos_before;
20699 row->truncated_on_right_p = 1;
20700 it->continuation_lines_width = 0;
20701 reseat_at_next_visible_line_start (it, 0);
20702 /* We insist below that IT's position be at ZV because in
20703 bidi-reordered lines the character at visible line start
20704 might not be the character that follows the newline in
20705 the logical order. */
20706 if (IT_BYTEPOS (*it) > BEG_BYTE)
20707 row->ends_at_zv_p =
20708 IT_BYTEPOS (*it) >= ZV_BYTE && FETCH_BYTE (ZV_BYTE - 1) != '\n';
20709 else
20710 row->ends_at_zv_p = false;
20711 break;
20715 if (wrap_data)
20716 bidi_unshelve_cache (wrap_data, 1);
20718 /* If line is not empty and hscrolled, maybe insert truncation glyphs
20719 at the left window margin. */
20720 if (it->first_visible_x
20721 && IT_CHARPOS (*it) != CHARPOS (row->start.pos))
20723 if (!FRAME_WINDOW_P (it->f)
20724 || (((row->reversed_p
20725 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
20726 : WINDOW_LEFT_FRINGE_WIDTH (it->w)) == 0)
20727 /* Don't let insert_left_trunc_glyphs overwrite the
20728 first glyph of the row if it is an image. */
20729 && row->glyphs[TEXT_AREA]->type != IMAGE_GLYPH))
20730 insert_left_trunc_glyphs (it);
20731 row->truncated_on_left_p = 1;
20734 /* Remember the position at which this line ends.
20736 BIDI Note: any code that needs MATRIX_ROW_START/END_CHARPOS
20737 cannot be before the call to find_row_edges below, since that is
20738 where these positions are determined. */
20739 row->end = it->current;
20740 if (!it->bidi_p)
20742 row->minpos = row->start.pos;
20743 row->maxpos = row->end.pos;
20745 else
20747 /* ROW->minpos and ROW->maxpos must be the smallest and
20748 `1 + the largest' buffer positions in ROW. But if ROW was
20749 bidi-reordered, these two positions can be anywhere in the
20750 row, so we must determine them now. */
20751 find_row_edges (it, row, min_pos, min_bpos, max_pos, max_bpos);
20754 /* If the start of this line is the overlay arrow-position, then
20755 mark this glyph row as the one containing the overlay arrow.
20756 This is clearly a mess with variable size fonts. It would be
20757 better to let it be displayed like cursors under X. */
20758 if ((MATRIX_ROW_DISPLAYS_TEXT_P (row) || !overlay_arrow_seen)
20759 && (overlay_arrow_string = overlay_arrow_at_row (it, row),
20760 !NILP (overlay_arrow_string)))
20762 /* Overlay arrow in window redisplay is a fringe bitmap. */
20763 if (STRINGP (overlay_arrow_string))
20765 struct glyph_row *arrow_row
20766 = get_overlay_arrow_glyph_row (it->w, overlay_arrow_string);
20767 struct glyph *glyph = arrow_row->glyphs[TEXT_AREA];
20768 struct glyph *arrow_end = glyph + arrow_row->used[TEXT_AREA];
20769 struct glyph *p = row->glyphs[TEXT_AREA];
20770 struct glyph *p2, *end;
20772 /* Copy the arrow glyphs. */
20773 while (glyph < arrow_end)
20774 *p++ = *glyph++;
20776 /* Throw away padding glyphs. */
20777 p2 = p;
20778 end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
20779 while (p2 < end && CHAR_GLYPH_PADDING_P (*p2))
20780 ++p2;
20781 if (p2 > p)
20783 while (p2 < end)
20784 *p++ = *p2++;
20785 row->used[TEXT_AREA] = p2 - row->glyphs[TEXT_AREA];
20788 else
20790 eassert (INTEGERP (overlay_arrow_string));
20791 row->overlay_arrow_bitmap = XINT (overlay_arrow_string);
20793 overlay_arrow_seen = 1;
20796 /* Highlight trailing whitespace. */
20797 if (!NILP (Vshow_trailing_whitespace))
20798 highlight_trailing_whitespace (it->f, it->glyph_row);
20800 /* Compute pixel dimensions of this line. */
20801 compute_line_metrics (it);
20803 /* Implementation note: No changes in the glyphs of ROW or in their
20804 faces can be done past this point, because compute_line_metrics
20805 computes ROW's hash value and stores it within the glyph_row
20806 structure. */
20808 /* Record whether this row ends inside an ellipsis. */
20809 row->ends_in_ellipsis_p
20810 = (it->method == GET_FROM_DISPLAY_VECTOR
20811 && it->ellipsis_p);
20813 /* Save fringe bitmaps in this row. */
20814 row->left_user_fringe_bitmap = it->left_user_fringe_bitmap;
20815 row->left_user_fringe_face_id = it->left_user_fringe_face_id;
20816 row->right_user_fringe_bitmap = it->right_user_fringe_bitmap;
20817 row->right_user_fringe_face_id = it->right_user_fringe_face_id;
20819 it->left_user_fringe_bitmap = 0;
20820 it->left_user_fringe_face_id = 0;
20821 it->right_user_fringe_bitmap = 0;
20822 it->right_user_fringe_face_id = 0;
20824 /* Maybe set the cursor. */
20825 cvpos = it->w->cursor.vpos;
20826 if ((cvpos < 0
20827 /* In bidi-reordered rows, keep checking for proper cursor
20828 position even if one has been found already, because buffer
20829 positions in such rows change non-linearly with ROW->VPOS,
20830 when a line is continued. One exception: when we are at ZV,
20831 display cursor on the first suitable glyph row, since all
20832 the empty rows after that also have their position set to ZV. */
20833 /* FIXME: Revisit this when glyph ``spilling'' in continuation
20834 lines' rows is implemented for bidi-reordered rows. */
20835 || (it->bidi_p
20836 && !MATRIX_ROW (it->w->desired_matrix, cvpos)->ends_at_zv_p))
20837 && PT >= MATRIX_ROW_START_CHARPOS (row)
20838 && PT <= MATRIX_ROW_END_CHARPOS (row)
20839 && cursor_row_p (row))
20840 set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
20842 /* Prepare for the next line. This line starts horizontally at (X
20843 HPOS) = (0 0). Vertical positions are incremented. As a
20844 convenience for the caller, IT->glyph_row is set to the next
20845 row to be used. */
20846 it->current_x = it->hpos = 0;
20847 it->current_y += row->height;
20848 SET_TEXT_POS (it->eol_pos, 0, 0);
20849 ++it->vpos;
20850 ++it->glyph_row;
20851 /* The next row should by default use the same value of the
20852 reversed_p flag as this one. set_iterator_to_next decides when
20853 it's a new paragraph, and PRODUCE_GLYPHS recomputes the value of
20854 the flag accordingly. */
20855 if (it->glyph_row < MATRIX_BOTTOM_TEXT_ROW (it->w->desired_matrix, it->w))
20856 it->glyph_row->reversed_p = row->reversed_p;
20857 it->start = row->end;
20858 return MATRIX_ROW_DISPLAYS_TEXT_P (row);
20860 #undef RECORD_MAX_MIN_POS
20863 DEFUN ("current-bidi-paragraph-direction", Fcurrent_bidi_paragraph_direction,
20864 Scurrent_bidi_paragraph_direction, 0, 1, 0,
20865 doc: /* Return paragraph direction at point in BUFFER.
20866 Value is either `left-to-right' or `right-to-left'.
20867 If BUFFER is omitted or nil, it defaults to the current buffer.
20869 Paragraph direction determines how the text in the paragraph is displayed.
20870 In left-to-right paragraphs, text begins at the left margin of the window
20871 and the reading direction is generally left to right. In right-to-left
20872 paragraphs, text begins at the right margin and is read from right to left.
20874 See also `bidi-paragraph-direction'. */)
20875 (Lisp_Object buffer)
20877 struct buffer *buf = current_buffer;
20878 struct buffer *old = buf;
20880 if (! NILP (buffer))
20882 CHECK_BUFFER (buffer);
20883 buf = XBUFFER (buffer);
20886 if (NILP (BVAR (buf, bidi_display_reordering))
20887 || NILP (BVAR (buf, enable_multibyte_characters))
20888 /* When we are loading loadup.el, the character property tables
20889 needed for bidi iteration are not yet available. */
20890 || !NILP (Vpurify_flag))
20891 return Qleft_to_right;
20892 else if (!NILP (BVAR (buf, bidi_paragraph_direction)))
20893 return BVAR (buf, bidi_paragraph_direction);
20894 else
20896 /* Determine the direction from buffer text. We could try to
20897 use current_matrix if it is up to date, but this seems fast
20898 enough as it is. */
20899 struct bidi_it itb;
20900 ptrdiff_t pos = BUF_PT (buf);
20901 ptrdiff_t bytepos = BUF_PT_BYTE (buf);
20902 int c;
20903 void *itb_data = bidi_shelve_cache ();
20905 set_buffer_temp (buf);
20906 /* bidi_paragraph_init finds the base direction of the paragraph
20907 by searching forward from paragraph start. We need the base
20908 direction of the current or _previous_ paragraph, so we need
20909 to make sure we are within that paragraph. To that end, find
20910 the previous non-empty line. */
20911 if (pos >= ZV && pos > BEGV)
20912 DEC_BOTH (pos, bytepos);
20913 AUTO_STRING (trailing_white_space, "[\f\t ]*\n");
20914 if (fast_looking_at (trailing_white_space,
20915 pos, bytepos, ZV, ZV_BYTE, Qnil) > 0)
20917 while ((c = FETCH_BYTE (bytepos)) == '\n'
20918 || c == ' ' || c == '\t' || c == '\f')
20920 if (bytepos <= BEGV_BYTE)
20921 break;
20922 bytepos--;
20923 pos--;
20925 while (!CHAR_HEAD_P (FETCH_BYTE (bytepos)))
20926 bytepos--;
20928 bidi_init_it (pos, bytepos, FRAME_WINDOW_P (SELECTED_FRAME ()), &itb);
20929 itb.paragraph_dir = NEUTRAL_DIR;
20930 itb.string.s = NULL;
20931 itb.string.lstring = Qnil;
20932 itb.string.bufpos = 0;
20933 itb.string.from_disp_str = 0;
20934 itb.string.unibyte = 0;
20935 /* We have no window to use here for ignoring window-specific
20936 overlays. Using NULL for window pointer will cause
20937 compute_display_string_pos to use the current buffer. */
20938 itb.w = NULL;
20939 bidi_paragraph_init (NEUTRAL_DIR, &itb, 1);
20940 bidi_unshelve_cache (itb_data, 0);
20941 set_buffer_temp (old);
20942 switch (itb.paragraph_dir)
20944 case L2R:
20945 return Qleft_to_right;
20946 break;
20947 case R2L:
20948 return Qright_to_left;
20949 break;
20950 default:
20951 emacs_abort ();
20956 DEFUN ("bidi-find-overridden-directionality",
20957 Fbidi_find_overridden_directionality,
20958 Sbidi_find_overridden_directionality, 2, 3, 0,
20959 doc: /* Return position between FROM and TO where directionality was overridden.
20961 This function returns the first character position in the specified
20962 region of OBJECT where there is a character whose `bidi-class' property
20963 is `L', but which was forced to display as `R' by a directional
20964 override, and likewise with characters whose `bidi-class' is `R'
20965 or `AL' that were forced to display as `L'.
20967 If no such character is found, the function returns nil.
20969 OBJECT is a Lisp string or buffer to search for overridden
20970 directionality, and defaults to the current buffer if nil or omitted.
20971 OBJECT can also be a window, in which case the function will search
20972 the buffer displayed in that window. Passing the window instead of
20973 a buffer is preferable when the buffer is displayed in some window,
20974 because this function will then be able to correctly account for
20975 window-specific overlays, which can affect the results.
20977 Strong directional characters `L', `R', and `AL' can have their
20978 intrinsic directionality overridden by directional override
20979 control characters RLO \(u+202e) and LRO \(u+202d). See the
20980 function `get-char-code-property' for a way to inquire about
20981 the `bidi-class' property of a character. */)
20982 (Lisp_Object from, Lisp_Object to, Lisp_Object object)
20984 struct buffer *buf = current_buffer;
20985 struct buffer *old = buf;
20986 struct window *w = NULL;
20987 bool frame_window_p = FRAME_WINDOW_P (SELECTED_FRAME ());
20988 struct bidi_it itb;
20989 ptrdiff_t from_pos, to_pos, from_bpos;
20990 void *itb_data;
20992 if (!NILP (object))
20994 if (BUFFERP (object))
20995 buf = XBUFFER (object);
20996 else if (WINDOWP (object))
20998 w = decode_live_window (object);
20999 buf = XBUFFER (w->contents);
21000 frame_window_p = FRAME_WINDOW_P (XFRAME (w->frame));
21002 else
21003 CHECK_STRING (object);
21006 if (STRINGP (object))
21008 /* Characters in unibyte strings are always treated by bidi.c as
21009 strong LTR. */
21010 if (!STRING_MULTIBYTE (object)
21011 /* When we are loading loadup.el, the character property
21012 tables needed for bidi iteration are not yet
21013 available. */
21014 || !NILP (Vpurify_flag))
21015 return Qnil;
21017 validate_subarray (object, from, to, SCHARS (object), &from_pos, &to_pos);
21018 if (from_pos >= SCHARS (object))
21019 return Qnil;
21021 /* Set up the bidi iterator. */
21022 itb_data = bidi_shelve_cache ();
21023 itb.paragraph_dir = NEUTRAL_DIR;
21024 itb.string.lstring = object;
21025 itb.string.s = NULL;
21026 itb.string.schars = SCHARS (object);
21027 itb.string.bufpos = 0;
21028 itb.string.from_disp_str = 0;
21029 itb.string.unibyte = 0;
21030 itb.w = w;
21031 bidi_init_it (0, 0, frame_window_p, &itb);
21033 else
21035 /* Nothing this fancy can happen in unibyte buffers, or in a
21036 buffer that disabled reordering, or if FROM is at EOB. */
21037 if (NILP (BVAR (buf, bidi_display_reordering))
21038 || NILP (BVAR (buf, enable_multibyte_characters))
21039 /* When we are loading loadup.el, the character property
21040 tables needed for bidi iteration are not yet
21041 available. */
21042 || !NILP (Vpurify_flag))
21043 return Qnil;
21045 set_buffer_temp (buf);
21046 validate_region (&from, &to);
21047 from_pos = XINT (from);
21048 to_pos = XINT (to);
21049 if (from_pos >= ZV)
21050 return Qnil;
21052 /* Set up the bidi iterator. */
21053 itb_data = bidi_shelve_cache ();
21054 from_bpos = CHAR_TO_BYTE (from_pos);
21055 if (from_pos == BEGV)
21057 itb.charpos = BEGV;
21058 itb.bytepos = BEGV_BYTE;
21060 else if (FETCH_CHAR (from_bpos - 1) == '\n')
21062 itb.charpos = from_pos;
21063 itb.bytepos = from_bpos;
21065 else
21066 itb.charpos = find_newline_no_quit (from_pos, CHAR_TO_BYTE (from_pos),
21067 -1, &itb.bytepos);
21068 itb.paragraph_dir = NEUTRAL_DIR;
21069 itb.string.s = NULL;
21070 itb.string.lstring = Qnil;
21071 itb.string.bufpos = 0;
21072 itb.string.from_disp_str = 0;
21073 itb.string.unibyte = 0;
21074 itb.w = w;
21075 bidi_init_it (itb.charpos, itb.bytepos, frame_window_p, &itb);
21078 ptrdiff_t found;
21079 do {
21080 /* For the purposes of this function, the actual base direction of
21081 the paragraph doesn't matter, so just set it to L2R. */
21082 bidi_paragraph_init (L2R, &itb, 0);
21083 while ((found = bidi_find_first_overridden (&itb)) < from_pos)
21085 } while (found == ZV && itb.ch == '\n' && itb.charpos < to_pos);
21087 bidi_unshelve_cache (itb_data, 0);
21088 set_buffer_temp (old);
21090 return (from_pos <= found && found < to_pos) ? make_number (found) : Qnil;
21093 DEFUN ("move-point-visually", Fmove_point_visually,
21094 Smove_point_visually, 1, 1, 0,
21095 doc: /* Move point in the visual order in the specified DIRECTION.
21096 DIRECTION can be 1, meaning move to the right, or -1, which moves to the
21097 left.
21099 Value is the new character position of point. */)
21100 (Lisp_Object direction)
21102 struct window *w = XWINDOW (selected_window);
21103 struct buffer *b = XBUFFER (w->contents);
21104 struct glyph_row *row;
21105 int dir;
21106 Lisp_Object paragraph_dir;
21108 #define ROW_GLYPH_NEWLINE_P(ROW,GLYPH) \
21109 (!(ROW)->continued_p \
21110 && NILP ((GLYPH)->object) \
21111 && (GLYPH)->type == CHAR_GLYPH \
21112 && (GLYPH)->u.ch == ' ' \
21113 && (GLYPH)->charpos >= 0 \
21114 && !(GLYPH)->avoid_cursor_p)
21116 CHECK_NUMBER (direction);
21117 dir = XINT (direction);
21118 if (dir > 0)
21119 dir = 1;
21120 else
21121 dir = -1;
21123 /* If current matrix is up-to-date, we can use the information
21124 recorded in the glyphs, at least as long as the goal is on the
21125 screen. */
21126 if (w->window_end_valid
21127 && !windows_or_buffers_changed
21128 && b
21129 && !b->clip_changed
21130 && !b->prevent_redisplay_optimizations_p
21131 && !window_outdated (w)
21132 /* We rely below on the cursor coordinates to be up to date, but
21133 we cannot trust them if some command moved point since the
21134 last complete redisplay. */
21135 && w->last_point == BUF_PT (b)
21136 && w->cursor.vpos >= 0
21137 && w->cursor.vpos < w->current_matrix->nrows
21138 && (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos))->enabled_p)
21140 struct glyph *g = row->glyphs[TEXT_AREA];
21141 struct glyph *e = dir > 0 ? g + row->used[TEXT_AREA] : g - 1;
21142 struct glyph *gpt = g + w->cursor.hpos;
21144 for (g = gpt + dir; (dir > 0 ? g < e : g > e); g += dir)
21146 if (BUFFERP (g->object) && g->charpos != PT)
21148 SET_PT (g->charpos);
21149 w->cursor.vpos = -1;
21150 return make_number (PT);
21152 else if (!NILP (g->object) && !EQ (g->object, gpt->object))
21154 ptrdiff_t new_pos;
21156 if (BUFFERP (gpt->object))
21158 new_pos = PT;
21159 if ((gpt->resolved_level - row->reversed_p) % 2 == 0)
21160 new_pos += (row->reversed_p ? -dir : dir);
21161 else
21162 new_pos -= (row->reversed_p ? -dir : dir);
21164 else if (BUFFERP (g->object))
21165 new_pos = g->charpos;
21166 else
21167 break;
21168 SET_PT (new_pos);
21169 w->cursor.vpos = -1;
21170 return make_number (PT);
21172 else if (ROW_GLYPH_NEWLINE_P (row, g))
21174 /* Glyphs inserted at the end of a non-empty line for
21175 positioning the cursor have zero charpos, so we must
21176 deduce the value of point by other means. */
21177 if (g->charpos > 0)
21178 SET_PT (g->charpos);
21179 else if (row->ends_at_zv_p && PT != ZV)
21180 SET_PT (ZV);
21181 else if (PT != MATRIX_ROW_END_CHARPOS (row) - 1)
21182 SET_PT (MATRIX_ROW_END_CHARPOS (row) - 1);
21183 else
21184 break;
21185 w->cursor.vpos = -1;
21186 return make_number (PT);
21189 if (g == e || NILP (g->object))
21191 if (row->truncated_on_left_p || row->truncated_on_right_p)
21192 goto simulate_display;
21193 if (!row->reversed_p)
21194 row += dir;
21195 else
21196 row -= dir;
21197 if (row < MATRIX_FIRST_TEXT_ROW (w->current_matrix)
21198 || row > MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w))
21199 goto simulate_display;
21201 if (dir > 0)
21203 if (row->reversed_p && !row->continued_p)
21205 SET_PT (MATRIX_ROW_END_CHARPOS (row) - 1);
21206 w->cursor.vpos = -1;
21207 return make_number (PT);
21209 g = row->glyphs[TEXT_AREA];
21210 e = g + row->used[TEXT_AREA];
21211 for ( ; g < e; g++)
21213 if (BUFFERP (g->object)
21214 /* Empty lines have only one glyph, which stands
21215 for the newline, and whose charpos is the
21216 buffer position of the newline. */
21217 || ROW_GLYPH_NEWLINE_P (row, g)
21218 /* When the buffer ends in a newline, the line at
21219 EOB also has one glyph, but its charpos is -1. */
21220 || (row->ends_at_zv_p
21221 && !row->reversed_p
21222 && NILP (g->object)
21223 && g->type == CHAR_GLYPH
21224 && g->u.ch == ' '))
21226 if (g->charpos > 0)
21227 SET_PT (g->charpos);
21228 else if (!row->reversed_p
21229 && row->ends_at_zv_p
21230 && PT != ZV)
21231 SET_PT (ZV);
21232 else
21233 continue;
21234 w->cursor.vpos = -1;
21235 return make_number (PT);
21239 else
21241 if (!row->reversed_p && !row->continued_p)
21243 SET_PT (MATRIX_ROW_END_CHARPOS (row) - 1);
21244 w->cursor.vpos = -1;
21245 return make_number (PT);
21247 e = row->glyphs[TEXT_AREA];
21248 g = e + row->used[TEXT_AREA] - 1;
21249 for ( ; g >= e; g--)
21251 if (BUFFERP (g->object)
21252 || (ROW_GLYPH_NEWLINE_P (row, g)
21253 && g->charpos > 0)
21254 /* Empty R2L lines on GUI frames have the buffer
21255 position of the newline stored in the stretch
21256 glyph. */
21257 || g->type == STRETCH_GLYPH
21258 || (row->ends_at_zv_p
21259 && row->reversed_p
21260 && NILP (g->object)
21261 && g->type == CHAR_GLYPH
21262 && g->u.ch == ' '))
21264 if (g->charpos > 0)
21265 SET_PT (g->charpos);
21266 else if (row->reversed_p
21267 && row->ends_at_zv_p
21268 && PT != ZV)
21269 SET_PT (ZV);
21270 else
21271 continue;
21272 w->cursor.vpos = -1;
21273 return make_number (PT);
21280 simulate_display:
21282 /* If we wind up here, we failed to move by using the glyphs, so we
21283 need to simulate display instead. */
21285 if (b)
21286 paragraph_dir = Fcurrent_bidi_paragraph_direction (w->contents);
21287 else
21288 paragraph_dir = Qleft_to_right;
21289 if (EQ (paragraph_dir, Qright_to_left))
21290 dir = -dir;
21291 if (PT <= BEGV && dir < 0)
21292 xsignal0 (Qbeginning_of_buffer);
21293 else if (PT >= ZV && dir > 0)
21294 xsignal0 (Qend_of_buffer);
21295 else
21297 struct text_pos pt;
21298 struct it it;
21299 int pt_x, target_x, pixel_width, pt_vpos;
21300 bool at_eol_p;
21301 bool overshoot_expected = false;
21302 bool target_is_eol_p = false;
21304 /* Setup the arena. */
21305 SET_TEXT_POS (pt, PT, PT_BYTE);
21306 start_display (&it, w, pt);
21308 if (it.cmp_it.id < 0
21309 && it.method == GET_FROM_STRING
21310 && it.area == TEXT_AREA
21311 && it.string_from_display_prop_p
21312 && (it.sp > 0 && it.stack[it.sp - 1].method == GET_FROM_BUFFER))
21313 overshoot_expected = true;
21315 /* Find the X coordinate of point. We start from the beginning
21316 of this or previous line to make sure we are before point in
21317 the logical order (since the move_it_* functions can only
21318 move forward). */
21319 reseat:
21320 reseat_at_previous_visible_line_start (&it);
21321 it.current_x = it.hpos = it.current_y = it.vpos = 0;
21322 if (IT_CHARPOS (it) != PT)
21324 move_it_to (&it, overshoot_expected ? PT - 1 : PT,
21325 -1, -1, -1, MOVE_TO_POS);
21326 /* If we missed point because the character there is
21327 displayed out of a display vector that has more than one
21328 glyph, retry expecting overshoot. */
21329 if (it.method == GET_FROM_DISPLAY_VECTOR
21330 && it.current.dpvec_index > 0
21331 && !overshoot_expected)
21333 overshoot_expected = true;
21334 goto reseat;
21336 else if (IT_CHARPOS (it) != PT && !overshoot_expected)
21337 move_it_in_display_line (&it, PT, -1, MOVE_TO_POS);
21339 pt_x = it.current_x;
21340 pt_vpos = it.vpos;
21341 if (dir > 0 || overshoot_expected)
21343 struct glyph_row *row = it.glyph_row;
21345 /* When point is at beginning of line, we don't have
21346 information about the glyph there loaded into struct
21347 it. Calling get_next_display_element fixes that. */
21348 if (pt_x == 0)
21349 get_next_display_element (&it);
21350 at_eol_p = ITERATOR_AT_END_OF_LINE_P (&it);
21351 it.glyph_row = NULL;
21352 PRODUCE_GLYPHS (&it); /* compute it.pixel_width */
21353 it.glyph_row = row;
21354 /* PRODUCE_GLYPHS advances it.current_x, so we must restore
21355 it, lest it will become out of sync with it's buffer
21356 position. */
21357 it.current_x = pt_x;
21359 else
21360 at_eol_p = ITERATOR_AT_END_OF_LINE_P (&it);
21361 pixel_width = it.pixel_width;
21362 if (overshoot_expected && at_eol_p)
21363 pixel_width = 0;
21364 else if (pixel_width <= 0)
21365 pixel_width = 1;
21367 /* If there's a display string (or something similar) at point,
21368 we are actually at the glyph to the left of point, so we need
21369 to correct the X coordinate. */
21370 if (overshoot_expected)
21372 if (it.bidi_p)
21373 pt_x += pixel_width * it.bidi_it.scan_dir;
21374 else
21375 pt_x += pixel_width;
21378 /* Compute target X coordinate, either to the left or to the
21379 right of point. On TTY frames, all characters have the same
21380 pixel width of 1, so we can use that. On GUI frames we don't
21381 have an easy way of getting at the pixel width of the
21382 character to the left of point, so we use a different method
21383 of getting to that place. */
21384 if (dir > 0)
21385 target_x = pt_x + pixel_width;
21386 else
21387 target_x = pt_x - (!FRAME_WINDOW_P (it.f)) * pixel_width;
21389 /* Target X coordinate could be one line above or below the line
21390 of point, in which case we need to adjust the target X
21391 coordinate. Also, if moving to the left, we need to begin at
21392 the left edge of the point's screen line. */
21393 if (dir < 0)
21395 if (pt_x > 0)
21397 start_display (&it, w, pt);
21398 reseat_at_previous_visible_line_start (&it);
21399 it.current_x = it.current_y = it.hpos = 0;
21400 if (pt_vpos != 0)
21401 move_it_by_lines (&it, pt_vpos);
21403 else
21405 move_it_by_lines (&it, -1);
21406 target_x = it.last_visible_x - !FRAME_WINDOW_P (it.f);
21407 target_is_eol_p = true;
21408 /* Under word-wrap, we don't know the x coordinate of
21409 the last character displayed on the previous line,
21410 which immediately precedes the wrap point. To find
21411 out its x coordinate, we try moving to the right
21412 margin of the window, which will stop at the wrap
21413 point, and then reset target_x to point at the
21414 character that precedes the wrap point. This is not
21415 needed on GUI frames, because (see below) there we
21416 move from the left margin one grapheme cluster at a
21417 time, and stop when we hit the wrap point. */
21418 if (!FRAME_WINDOW_P (it.f) && it.line_wrap == WORD_WRAP)
21420 void *it_data = NULL;
21421 struct it it2;
21423 SAVE_IT (it2, it, it_data);
21424 move_it_in_display_line_to (&it, ZV, target_x,
21425 MOVE_TO_POS | MOVE_TO_X);
21426 /* If we arrived at target_x, that _is_ the last
21427 character on the previous line. */
21428 if (it.current_x != target_x)
21429 target_x = it.current_x - 1;
21430 RESTORE_IT (&it, &it2, it_data);
21434 else
21436 if (at_eol_p
21437 || (target_x >= it.last_visible_x
21438 && it.line_wrap != TRUNCATE))
21440 if (pt_x > 0)
21441 move_it_by_lines (&it, 0);
21442 move_it_by_lines (&it, 1);
21443 target_x = 0;
21447 /* Move to the target X coordinate. */
21448 #ifdef HAVE_WINDOW_SYSTEM
21449 /* On GUI frames, as we don't know the X coordinate of the
21450 character to the left of point, moving point to the left
21451 requires walking, one grapheme cluster at a time, until we
21452 find ourself at a place immediately to the left of the
21453 character at point. */
21454 if (FRAME_WINDOW_P (it.f) && dir < 0)
21456 struct text_pos new_pos;
21457 enum move_it_result rc = MOVE_X_REACHED;
21459 if (it.current_x == 0)
21460 get_next_display_element (&it);
21461 if (it.what == IT_COMPOSITION)
21463 new_pos.charpos = it.cmp_it.charpos;
21464 new_pos.bytepos = -1;
21466 else
21467 new_pos = it.current.pos;
21469 while (it.current_x + it.pixel_width <= target_x
21470 && (rc == MOVE_X_REACHED
21471 /* Under word-wrap, move_it_in_display_line_to
21472 stops at correct coordinates, but sometimes
21473 returns MOVE_POS_MATCH_OR_ZV. */
21474 || (it.line_wrap == WORD_WRAP
21475 && rc == MOVE_POS_MATCH_OR_ZV)))
21477 int new_x = it.current_x + it.pixel_width;
21479 /* For composed characters, we want the position of the
21480 first character in the grapheme cluster (usually, the
21481 composition's base character), whereas it.current
21482 might give us the position of the _last_ one, e.g. if
21483 the composition is rendered in reverse due to bidi
21484 reordering. */
21485 if (it.what == IT_COMPOSITION)
21487 new_pos.charpos = it.cmp_it.charpos;
21488 new_pos.bytepos = -1;
21490 else
21491 new_pos = it.current.pos;
21492 if (new_x == it.current_x)
21493 new_x++;
21494 rc = move_it_in_display_line_to (&it, ZV, new_x,
21495 MOVE_TO_POS | MOVE_TO_X);
21496 if (ITERATOR_AT_END_OF_LINE_P (&it) && !target_is_eol_p)
21497 break;
21499 /* The previous position we saw in the loop is the one we
21500 want. */
21501 if (new_pos.bytepos == -1)
21502 new_pos.bytepos = CHAR_TO_BYTE (new_pos.charpos);
21503 it.current.pos = new_pos;
21505 else
21506 #endif
21507 if (it.current_x != target_x)
21508 move_it_in_display_line_to (&it, ZV, target_x, MOVE_TO_POS | MOVE_TO_X);
21510 /* When lines are truncated, the above loop will stop at the
21511 window edge. But we want to get to the end of line, even if
21512 it is beyond the window edge; automatic hscroll will then
21513 scroll the window to show point as appropriate. */
21514 if (target_is_eol_p && it.line_wrap == TRUNCATE
21515 && get_next_display_element (&it))
21517 struct text_pos new_pos = it.current.pos;
21519 while (!ITERATOR_AT_END_OF_LINE_P (&it))
21521 set_iterator_to_next (&it, 0);
21522 if (it.method == GET_FROM_BUFFER)
21523 new_pos = it.current.pos;
21524 if (!get_next_display_element (&it))
21525 break;
21528 it.current.pos = new_pos;
21531 /* If we ended up in a display string that covers point, move to
21532 buffer position to the right in the visual order. */
21533 if (dir > 0)
21535 while (IT_CHARPOS (it) == PT)
21537 set_iterator_to_next (&it, 0);
21538 if (!get_next_display_element (&it))
21539 break;
21543 /* Move point to that position. */
21544 SET_PT_BOTH (IT_CHARPOS (it), IT_BYTEPOS (it));
21547 return make_number (PT);
21549 #undef ROW_GLYPH_NEWLINE_P
21552 DEFUN ("bidi-resolved-levels", Fbidi_resolved_levels,
21553 Sbidi_resolved_levels, 0, 1, 0,
21554 doc: /* Return the resolved bidirectional levels of characters at VPOS.
21556 The resolved levels are produced by the Emacs bidi reordering engine
21557 that implements the UBA, the Unicode Bidirectional Algorithm. Please
21558 read the Unicode Standard Annex 9 (UAX#9) for background information
21559 about these levels.
21561 VPOS is the zero-based number of the current window's screen line
21562 for which to produce the resolved levels. If VPOS is nil or omitted,
21563 it defaults to the screen line of point. If the window displays a
21564 header line, VPOS of zero will report on the header line, and first
21565 line of text in the window will have VPOS of 1.
21567 Value is an array of resolved levels, indexed by glyph number.
21568 Glyphs are numbered from zero starting from the beginning of the
21569 screen line, i.e. the left edge of the window for left-to-right lines
21570 and from the right edge for right-to-left lines. The resolved levels
21571 are produced only for the window's text area; text in display margins
21572 is not included.
21574 If the selected window's display is not up-to-date, or if the specified
21575 screen line does not display text, this function returns nil. It is
21576 highly recommended to bind this function to some simple key, like F8,
21577 in order to avoid these problems.
21579 This function exists mainly for testing the correctness of the
21580 Emacs UBA implementation, in particular with the test suite. */)
21581 (Lisp_Object vpos)
21583 struct window *w = XWINDOW (selected_window);
21584 struct buffer *b = XBUFFER (w->contents);
21585 int nrow;
21586 struct glyph_row *row;
21588 if (NILP (vpos))
21590 int d1, d2, d3, d4, d5;
21592 pos_visible_p (w, PT, &d1, &d2, &d3, &d4, &d5, &nrow);
21594 else
21596 CHECK_NUMBER_COERCE_MARKER (vpos);
21597 nrow = XINT (vpos);
21600 /* We require up-to-date glyph matrix for this window. */
21601 if (w->window_end_valid
21602 && !windows_or_buffers_changed
21603 && b
21604 && !b->clip_changed
21605 && !b->prevent_redisplay_optimizations_p
21606 && !window_outdated (w)
21607 && nrow >= 0
21608 && nrow < w->current_matrix->nrows
21609 && (row = MATRIX_ROW (w->current_matrix, nrow))->enabled_p
21610 && MATRIX_ROW_DISPLAYS_TEXT_P (row))
21612 struct glyph *g, *e, *g1;
21613 int nglyphs, i;
21614 Lisp_Object levels;
21616 if (!row->reversed_p) /* Left-to-right glyph row. */
21618 g = g1 = row->glyphs[TEXT_AREA];
21619 e = g + row->used[TEXT_AREA];
21621 /* Skip over glyphs at the start of the row that was
21622 generated by redisplay for its own needs. */
21623 while (g < e
21624 && NILP (g->object)
21625 && g->charpos < 0)
21626 g++;
21627 g1 = g;
21629 /* Count the "interesting" glyphs in this row. */
21630 for (nglyphs = 0; g < e && !NILP (g->object); g++)
21631 nglyphs++;
21633 /* Create and fill the array. */
21634 levels = make_uninit_vector (nglyphs);
21635 for (i = 0; g1 < g; i++, g1++)
21636 ASET (levels, i, make_number (g1->resolved_level));
21638 else /* Right-to-left glyph row. */
21640 g = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
21641 e = row->glyphs[TEXT_AREA] - 1;
21642 while (g > e
21643 && NILP (g->object)
21644 && g->charpos < 0)
21645 g--;
21646 g1 = g;
21647 for (nglyphs = 0; g > e && !NILP (g->object); g--)
21648 nglyphs++;
21649 levels = make_uninit_vector (nglyphs);
21650 for (i = 0; g1 > g; i++, g1--)
21651 ASET (levels, i, make_number (g1->resolved_level));
21653 return levels;
21655 else
21656 return Qnil;
21661 /***********************************************************************
21662 Menu Bar
21663 ***********************************************************************/
21665 /* Redisplay the menu bar in the frame for window W.
21667 The menu bar of X frames that don't have X toolkit support is
21668 displayed in a special window W->frame->menu_bar_window.
21670 The menu bar of terminal frames is treated specially as far as
21671 glyph matrices are concerned. Menu bar lines are not part of
21672 windows, so the update is done directly on the frame matrix rows
21673 for the menu bar. */
21675 static void
21676 display_menu_bar (struct window *w)
21678 struct frame *f = XFRAME (WINDOW_FRAME (w));
21679 struct it it;
21680 Lisp_Object items;
21681 int i;
21683 /* Don't do all this for graphical frames. */
21684 #ifdef HAVE_NTGUI
21685 if (FRAME_W32_P (f))
21686 return;
21687 #endif
21688 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
21689 if (FRAME_X_P (f))
21690 return;
21691 #endif
21693 #ifdef HAVE_NS
21694 if (FRAME_NS_P (f))
21695 return;
21696 #endif /* HAVE_NS */
21698 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
21699 eassert (!FRAME_WINDOW_P (f));
21700 init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MENU_FACE_ID);
21701 it.first_visible_x = 0;
21702 it.last_visible_x = FRAME_PIXEL_WIDTH (f);
21703 #elif defined (HAVE_X_WINDOWS) /* X without toolkit. */
21704 if (FRAME_WINDOW_P (f))
21706 /* Menu bar lines are displayed in the desired matrix of the
21707 dummy window menu_bar_window. */
21708 struct window *menu_w;
21709 menu_w = XWINDOW (f->menu_bar_window);
21710 init_iterator (&it, menu_w, -1, -1, menu_w->desired_matrix->rows,
21711 MENU_FACE_ID);
21712 it.first_visible_x = 0;
21713 it.last_visible_x = FRAME_PIXEL_WIDTH (f);
21715 else
21716 #endif /* not USE_X_TOOLKIT and not USE_GTK */
21718 /* This is a TTY frame, i.e. character hpos/vpos are used as
21719 pixel x/y. */
21720 init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
21721 MENU_FACE_ID);
21722 it.first_visible_x = 0;
21723 it.last_visible_x = FRAME_COLS (f);
21726 /* FIXME: This should be controlled by a user option. See the
21727 comments in redisplay_tool_bar and display_mode_line about
21728 this. */
21729 it.paragraph_embedding = L2R;
21731 /* Clear all rows of the menu bar. */
21732 for (i = 0; i < FRAME_MENU_BAR_LINES (f); ++i)
21734 struct glyph_row *row = it.glyph_row + i;
21735 clear_glyph_row (row);
21736 row->enabled_p = true;
21737 row->full_width_p = 1;
21738 row->reversed_p = false;
21741 /* Display all items of the menu bar. */
21742 items = FRAME_MENU_BAR_ITEMS (it.f);
21743 for (i = 0; i < ASIZE (items); i += 4)
21745 Lisp_Object string;
21747 /* Stop at nil string. */
21748 string = AREF (items, i + 1);
21749 if (NILP (string))
21750 break;
21752 /* Remember where item was displayed. */
21753 ASET (items, i + 3, make_number (it.hpos));
21755 /* Display the item, pad with one space. */
21756 if (it.current_x < it.last_visible_x)
21757 display_string (NULL, string, Qnil, 0, 0, &it,
21758 SCHARS (string) + 1, 0, 0, -1);
21761 /* Fill out the line with spaces. */
21762 if (it.current_x < it.last_visible_x)
21763 display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
21765 /* Compute the total height of the lines. */
21766 compute_line_metrics (&it);
21769 /* Deep copy of a glyph row, including the glyphs. */
21770 static void
21771 deep_copy_glyph_row (struct glyph_row *to, struct glyph_row *from)
21773 struct glyph *pointers[1 + LAST_AREA];
21774 int to_used = to->used[TEXT_AREA];
21776 /* Save glyph pointers of TO. */
21777 memcpy (pointers, to->glyphs, sizeof to->glyphs);
21779 /* Do a structure assignment. */
21780 *to = *from;
21782 /* Restore original glyph pointers of TO. */
21783 memcpy (to->glyphs, pointers, sizeof to->glyphs);
21785 /* Copy the glyphs. */
21786 memcpy (to->glyphs[TEXT_AREA], from->glyphs[TEXT_AREA],
21787 min (from->used[TEXT_AREA], to_used) * sizeof (struct glyph));
21789 /* If we filled only part of the TO row, fill the rest with
21790 space_glyph (which will display as empty space). */
21791 if (to_used > from->used[TEXT_AREA])
21792 fill_up_frame_row_with_spaces (to, to_used);
21795 /* Display one menu item on a TTY, by overwriting the glyphs in the
21796 frame F's desired glyph matrix with glyphs produced from the menu
21797 item text. Called from term.c to display TTY drop-down menus one
21798 item at a time.
21800 ITEM_TEXT is the menu item text as a C string.
21802 FACE_ID is the face ID to be used for this menu item. FACE_ID
21803 could specify one of 3 faces: a face for an enabled item, a face
21804 for a disabled item, or a face for a selected item.
21806 X and Y are coordinates of the first glyph in the frame's desired
21807 matrix to be overwritten by the menu item. Since this is a TTY, Y
21808 is the zero-based number of the glyph row and X is the zero-based
21809 glyph number in the row, starting from left, where to start
21810 displaying the item.
21812 SUBMENU non-zero means this menu item drops down a submenu, which
21813 should be indicated by displaying a proper visual cue after the
21814 item text. */
21816 void
21817 display_tty_menu_item (const char *item_text, int width, int face_id,
21818 int x, int y, int submenu)
21820 struct it it;
21821 struct frame *f = SELECTED_FRAME ();
21822 struct window *w = XWINDOW (f->selected_window);
21823 int saved_used, saved_truncated, saved_width, saved_reversed;
21824 struct glyph_row *row;
21825 size_t item_len = strlen (item_text);
21827 eassert (FRAME_TERMCAP_P (f));
21829 /* Don't write beyond the matrix's last row. This can happen for
21830 TTY screens that are not high enough to show the entire menu.
21831 (This is actually a bit of defensive programming, as
21832 tty_menu_display already limits the number of menu items to one
21833 less than the number of screen lines.) */
21834 if (y >= f->desired_matrix->nrows)
21835 return;
21837 init_iterator (&it, w, -1, -1, f->desired_matrix->rows + y, MENU_FACE_ID);
21838 it.first_visible_x = 0;
21839 it.last_visible_x = FRAME_COLS (f) - 1;
21840 row = it.glyph_row;
21841 /* Start with the row contents from the current matrix. */
21842 deep_copy_glyph_row (row, f->current_matrix->rows + y);
21843 saved_width = row->full_width_p;
21844 row->full_width_p = 1;
21845 saved_reversed = row->reversed_p;
21846 row->reversed_p = 0;
21847 row->enabled_p = true;
21849 /* Arrange for the menu item glyphs to start at (X,Y) and have the
21850 desired face. */
21851 eassert (x < f->desired_matrix->matrix_w);
21852 it.current_x = it.hpos = x;
21853 it.current_y = it.vpos = y;
21854 saved_used = row->used[TEXT_AREA];
21855 saved_truncated = row->truncated_on_right_p;
21856 row->used[TEXT_AREA] = x;
21857 it.face_id = face_id;
21858 it.line_wrap = TRUNCATE;
21860 /* FIXME: This should be controlled by a user option. See the
21861 comments in redisplay_tool_bar and display_mode_line about this.
21862 Also, if paragraph_embedding could ever be R2L, changes will be
21863 needed to avoid shifting to the right the row characters in
21864 term.c:append_glyph. */
21865 it.paragraph_embedding = L2R;
21867 /* Pad with a space on the left. */
21868 display_string (" ", Qnil, Qnil, 0, 0, &it, 1, 0, FRAME_COLS (f) - 1, -1);
21869 width--;
21870 /* Display the menu item, pad with spaces to WIDTH. */
21871 if (submenu)
21873 display_string (item_text, Qnil, Qnil, 0, 0, &it,
21874 item_len, 0, FRAME_COLS (f) - 1, -1);
21875 width -= item_len;
21876 /* Indicate with " >" that there's a submenu. */
21877 display_string (" >", Qnil, Qnil, 0, 0, &it, width, 0,
21878 FRAME_COLS (f) - 1, -1);
21880 else
21881 display_string (item_text, Qnil, Qnil, 0, 0, &it,
21882 width, 0, FRAME_COLS (f) - 1, -1);
21884 row->used[TEXT_AREA] = max (saved_used, row->used[TEXT_AREA]);
21885 row->truncated_on_right_p = saved_truncated;
21886 row->hash = row_hash (row);
21887 row->full_width_p = saved_width;
21888 row->reversed_p = saved_reversed;
21891 /***********************************************************************
21892 Mode Line
21893 ***********************************************************************/
21895 /* Redisplay mode lines in the window tree whose root is WINDOW. If
21896 FORCE is non-zero, redisplay mode lines unconditionally.
21897 Otherwise, redisplay only mode lines that are garbaged. Value is
21898 the number of windows whose mode lines were redisplayed. */
21900 static int
21901 redisplay_mode_lines (Lisp_Object window, bool force)
21903 int nwindows = 0;
21905 while (!NILP (window))
21907 struct window *w = XWINDOW (window);
21909 if (WINDOWP (w->contents))
21910 nwindows += redisplay_mode_lines (w->contents, force);
21911 else if (force
21912 || FRAME_GARBAGED_P (XFRAME (w->frame))
21913 || !MATRIX_MODE_LINE_ROW (w->current_matrix)->enabled_p)
21915 struct text_pos lpoint;
21916 struct buffer *old = current_buffer;
21918 /* Set the window's buffer for the mode line display. */
21919 SET_TEXT_POS (lpoint, PT, PT_BYTE);
21920 set_buffer_internal_1 (XBUFFER (w->contents));
21922 /* Point refers normally to the selected window. For any
21923 other window, set up appropriate value. */
21924 if (!EQ (window, selected_window))
21926 struct text_pos pt;
21928 CLIP_TEXT_POS_FROM_MARKER (pt, w->pointm);
21929 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
21932 /* Display mode lines. */
21933 clear_glyph_matrix (w->desired_matrix);
21934 if (display_mode_lines (w))
21935 ++nwindows;
21937 /* Restore old settings. */
21938 set_buffer_internal_1 (old);
21939 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
21942 window = w->next;
21945 return nwindows;
21949 /* Display the mode and/or header line of window W. Value is the
21950 sum number of mode lines and header lines displayed. */
21952 static int
21953 display_mode_lines (struct window *w)
21955 Lisp_Object old_selected_window = selected_window;
21956 Lisp_Object old_selected_frame = selected_frame;
21957 Lisp_Object new_frame = w->frame;
21958 Lisp_Object old_frame_selected_window = XFRAME (new_frame)->selected_window;
21959 int n = 0;
21961 selected_frame = new_frame;
21962 /* FIXME: If we were to allow the mode-line's computation changing the buffer
21963 or window's point, then we'd need select_window_1 here as well. */
21964 XSETWINDOW (selected_window, w);
21965 XFRAME (new_frame)->selected_window = selected_window;
21967 /* These will be set while the mode line specs are processed. */
21968 line_number_displayed = 0;
21969 w->column_number_displayed = -1;
21971 if (WINDOW_WANTS_MODELINE_P (w))
21973 struct window *sel_w = XWINDOW (old_selected_window);
21975 /* Select mode line face based on the real selected window. */
21976 display_mode_line (w, CURRENT_MODE_LINE_FACE_ID_3 (sel_w, sel_w, w),
21977 BVAR (current_buffer, mode_line_format));
21978 ++n;
21981 if (WINDOW_WANTS_HEADER_LINE_P (w))
21983 display_mode_line (w, HEADER_LINE_FACE_ID,
21984 BVAR (current_buffer, header_line_format));
21985 ++n;
21988 XFRAME (new_frame)->selected_window = old_frame_selected_window;
21989 selected_frame = old_selected_frame;
21990 selected_window = old_selected_window;
21991 if (n > 0)
21992 w->must_be_updated_p = true;
21993 return n;
21997 /* Display mode or header line of window W. FACE_ID specifies which
21998 line to display; it is either MODE_LINE_FACE_ID or
21999 HEADER_LINE_FACE_ID. FORMAT is the mode/header line format to
22000 display. Value is the pixel height of the mode/header line
22001 displayed. */
22003 static int
22004 display_mode_line (struct window *w, enum face_id face_id, Lisp_Object format)
22006 struct it it;
22007 struct face *face;
22008 ptrdiff_t count = SPECPDL_INDEX ();
22010 init_iterator (&it, w, -1, -1, NULL, face_id);
22011 /* Don't extend on a previously drawn mode-line.
22012 This may happen if called from pos_visible_p. */
22013 it.glyph_row->enabled_p = false;
22014 prepare_desired_row (w, it.glyph_row, true);
22016 it.glyph_row->mode_line_p = 1;
22018 /* FIXME: This should be controlled by a user option. But
22019 supporting such an option is not trivial, since the mode line is
22020 made up of many separate strings. */
22021 it.paragraph_embedding = L2R;
22023 record_unwind_protect (unwind_format_mode_line,
22024 format_mode_line_unwind_data (NULL, NULL, Qnil, 0));
22026 mode_line_target = MODE_LINE_DISPLAY;
22028 /* Temporarily make frame's keyboard the current kboard so that
22029 kboard-local variables in the mode_line_format will get the right
22030 values. */
22031 push_kboard (FRAME_KBOARD (it.f));
22032 record_unwind_save_match_data ();
22033 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
22034 pop_kboard ();
22036 unbind_to (count, Qnil);
22038 /* Fill up with spaces. */
22039 display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);
22041 compute_line_metrics (&it);
22042 it.glyph_row->full_width_p = 1;
22043 it.glyph_row->continued_p = 0;
22044 it.glyph_row->truncated_on_left_p = 0;
22045 it.glyph_row->truncated_on_right_p = 0;
22047 /* Make a 3D mode-line have a shadow at its right end. */
22048 face = FACE_FROM_ID (it.f, face_id);
22049 extend_face_to_end_of_line (&it);
22050 if (face->box != FACE_NO_BOX)
22052 struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
22053 + it.glyph_row->used[TEXT_AREA] - 1);
22054 last->right_box_line_p = 1;
22057 return it.glyph_row->height;
22060 /* Move element ELT in LIST to the front of LIST.
22061 Return the updated list. */
22063 static Lisp_Object
22064 move_elt_to_front (Lisp_Object elt, Lisp_Object list)
22066 register Lisp_Object tail, prev;
22067 register Lisp_Object tem;
22069 tail = list;
22070 prev = Qnil;
22071 while (CONSP (tail))
22073 tem = XCAR (tail);
22075 if (EQ (elt, tem))
22077 /* Splice out the link TAIL. */
22078 if (NILP (prev))
22079 list = XCDR (tail);
22080 else
22081 Fsetcdr (prev, XCDR (tail));
22083 /* Now make it the first. */
22084 Fsetcdr (tail, list);
22085 return tail;
22087 else
22088 prev = tail;
22089 tail = XCDR (tail);
22090 QUIT;
22093 /* Not found--return unchanged LIST. */
22094 return list;
22097 /* Contribute ELT to the mode line for window IT->w. How it
22098 translates into text depends on its data type.
22100 IT describes the display environment in which we display, as usual.
22102 DEPTH is the depth in recursion. It is used to prevent
22103 infinite recursion here.
22105 FIELD_WIDTH is the number of characters the display of ELT should
22106 occupy in the mode line, and PRECISION is the maximum number of
22107 characters to display from ELT's representation. See
22108 display_string for details.
22110 Returns the hpos of the end of the text generated by ELT.
22112 PROPS is a property list to add to any string we encounter.
22114 If RISKY is nonzero, remove (disregard) any properties in any string
22115 we encounter, and ignore :eval and :propertize.
22117 The global variable `mode_line_target' determines whether the
22118 output is passed to `store_mode_line_noprop',
22119 `store_mode_line_string', or `display_string'. */
22121 static int
22122 display_mode_element (struct it *it, int depth, int field_width, int precision,
22123 Lisp_Object elt, Lisp_Object props, int risky)
22125 int n = 0, field, prec;
22126 int literal = 0;
22128 tail_recurse:
22129 if (depth > 100)
22130 elt = build_string ("*too-deep*");
22132 depth++;
22134 switch (XTYPE (elt))
22136 case Lisp_String:
22138 /* A string: output it and check for %-constructs within it. */
22139 unsigned char c;
22140 ptrdiff_t offset = 0;
22142 if (SCHARS (elt) > 0
22143 && (!NILP (props) || risky))
22145 Lisp_Object oprops, aelt;
22146 oprops = Ftext_properties_at (make_number (0), elt);
22148 /* If the starting string's properties are not what
22149 we want, translate the string. Also, if the string
22150 is risky, do that anyway. */
22152 if (NILP (Fequal (props, oprops)) || risky)
22154 /* If the starting string has properties,
22155 merge the specified ones onto the existing ones. */
22156 if (! NILP (oprops) && !risky)
22158 Lisp_Object tem;
22160 oprops = Fcopy_sequence (oprops);
22161 tem = props;
22162 while (CONSP (tem))
22164 oprops = Fplist_put (oprops, XCAR (tem),
22165 XCAR (XCDR (tem)));
22166 tem = XCDR (XCDR (tem));
22168 props = oprops;
22171 aelt = Fassoc (elt, mode_line_proptrans_alist);
22172 if (! NILP (aelt) && !NILP (Fequal (props, XCDR (aelt))))
22174 /* AELT is what we want. Move it to the front
22175 without consing. */
22176 elt = XCAR (aelt);
22177 mode_line_proptrans_alist
22178 = move_elt_to_front (aelt, mode_line_proptrans_alist);
22180 else
22182 Lisp_Object tem;
22184 /* If AELT has the wrong props, it is useless.
22185 so get rid of it. */
22186 if (! NILP (aelt))
22187 mode_line_proptrans_alist
22188 = Fdelq (aelt, mode_line_proptrans_alist);
22190 elt = Fcopy_sequence (elt);
22191 Fset_text_properties (make_number (0), Flength (elt),
22192 props, elt);
22193 /* Add this item to mode_line_proptrans_alist. */
22194 mode_line_proptrans_alist
22195 = Fcons (Fcons (elt, props),
22196 mode_line_proptrans_alist);
22197 /* Truncate mode_line_proptrans_alist
22198 to at most 50 elements. */
22199 tem = Fnthcdr (make_number (50),
22200 mode_line_proptrans_alist);
22201 if (! NILP (tem))
22202 XSETCDR (tem, Qnil);
22207 offset = 0;
22209 if (literal)
22211 prec = precision - n;
22212 switch (mode_line_target)
22214 case MODE_LINE_NOPROP:
22215 case MODE_LINE_TITLE:
22216 n += store_mode_line_noprop (SSDATA (elt), -1, prec);
22217 break;
22218 case MODE_LINE_STRING:
22219 n += store_mode_line_string (NULL, elt, 1, 0, prec, Qnil);
22220 break;
22221 case MODE_LINE_DISPLAY:
22222 n += display_string (NULL, elt, Qnil, 0, 0, it,
22223 0, prec, 0, STRING_MULTIBYTE (elt));
22224 break;
22227 break;
22230 /* Handle the non-literal case. */
22232 while ((precision <= 0 || n < precision)
22233 && SREF (elt, offset) != 0
22234 && (mode_line_target != MODE_LINE_DISPLAY
22235 || it->current_x < it->last_visible_x))
22237 ptrdiff_t last_offset = offset;
22239 /* Advance to end of string or next format specifier. */
22240 while ((c = SREF (elt, offset++)) != '\0' && c != '%')
22243 if (offset - 1 != last_offset)
22245 ptrdiff_t nchars, nbytes;
22247 /* Output to end of string or up to '%'. Field width
22248 is length of string. Don't output more than
22249 PRECISION allows us. */
22250 offset--;
22252 prec = c_string_width (SDATA (elt) + last_offset,
22253 offset - last_offset, precision - n,
22254 &nchars, &nbytes);
22256 switch (mode_line_target)
22258 case MODE_LINE_NOPROP:
22259 case MODE_LINE_TITLE:
22260 n += store_mode_line_noprop (SSDATA (elt) + last_offset, 0, prec);
22261 break;
22262 case MODE_LINE_STRING:
22264 ptrdiff_t bytepos = last_offset;
22265 ptrdiff_t charpos = string_byte_to_char (elt, bytepos);
22266 ptrdiff_t endpos = (precision <= 0
22267 ? string_byte_to_char (elt, offset)
22268 : charpos + nchars);
22270 n += store_mode_line_string (NULL,
22271 Fsubstring (elt, make_number (charpos),
22272 make_number (endpos)),
22273 0, 0, 0, Qnil);
22275 break;
22276 case MODE_LINE_DISPLAY:
22278 ptrdiff_t bytepos = last_offset;
22279 ptrdiff_t charpos = string_byte_to_char (elt, bytepos);
22281 if (precision <= 0)
22282 nchars = string_byte_to_char (elt, offset) - charpos;
22283 n += display_string (NULL, elt, Qnil, 0, charpos,
22284 it, 0, nchars, 0,
22285 STRING_MULTIBYTE (elt));
22287 break;
22290 else /* c == '%' */
22292 ptrdiff_t percent_position = offset;
22294 /* Get the specified minimum width. Zero means
22295 don't pad. */
22296 field = 0;
22297 while ((c = SREF (elt, offset++)) >= '0' && c <= '9')
22298 field = field * 10 + c - '0';
22300 /* Don't pad beyond the total padding allowed. */
22301 if (field_width - n > 0 && field > field_width - n)
22302 field = field_width - n;
22304 /* Note that either PRECISION <= 0 or N < PRECISION. */
22305 prec = precision - n;
22307 if (c == 'M')
22308 n += display_mode_element (it, depth, field, prec,
22309 Vglobal_mode_string, props,
22310 risky);
22311 else if (c != 0)
22313 bool multibyte;
22314 ptrdiff_t bytepos, charpos;
22315 const char *spec;
22316 Lisp_Object string;
22318 bytepos = percent_position;
22319 charpos = (STRING_MULTIBYTE (elt)
22320 ? string_byte_to_char (elt, bytepos)
22321 : bytepos);
22322 spec = decode_mode_spec (it->w, c, field, &string);
22323 multibyte = STRINGP (string) && STRING_MULTIBYTE (string);
22325 switch (mode_line_target)
22327 case MODE_LINE_NOPROP:
22328 case MODE_LINE_TITLE:
22329 n += store_mode_line_noprop (spec, field, prec);
22330 break;
22331 case MODE_LINE_STRING:
22333 Lisp_Object tem = build_string (spec);
22334 props = Ftext_properties_at (make_number (charpos), elt);
22335 /* Should only keep face property in props */
22336 n += store_mode_line_string (NULL, tem, 0, field, prec, props);
22338 break;
22339 case MODE_LINE_DISPLAY:
22341 int nglyphs_before, nwritten;
22343 nglyphs_before = it->glyph_row->used[TEXT_AREA];
22344 nwritten = display_string (spec, string, elt,
22345 charpos, 0, it,
22346 field, prec, 0,
22347 multibyte);
22349 /* Assign to the glyphs written above the
22350 string where the `%x' came from, position
22351 of the `%'. */
22352 if (nwritten > 0)
22354 struct glyph *glyph
22355 = (it->glyph_row->glyphs[TEXT_AREA]
22356 + nglyphs_before);
22357 int i;
22359 for (i = 0; i < nwritten; ++i)
22361 glyph[i].object = elt;
22362 glyph[i].charpos = charpos;
22365 n += nwritten;
22368 break;
22371 else /* c == 0 */
22372 break;
22376 break;
22378 case Lisp_Symbol:
22379 /* A symbol: process the value of the symbol recursively
22380 as if it appeared here directly. Avoid error if symbol void.
22381 Special case: if value of symbol is a string, output the string
22382 literally. */
22384 register Lisp_Object tem;
22386 /* If the variable is not marked as risky to set
22387 then its contents are risky to use. */
22388 if (NILP (Fget (elt, Qrisky_local_variable)))
22389 risky = 1;
22391 tem = Fboundp (elt);
22392 if (!NILP (tem))
22394 tem = Fsymbol_value (elt);
22395 /* If value is a string, output that string literally:
22396 don't check for % within it. */
22397 if (STRINGP (tem))
22398 literal = 1;
22400 if (!EQ (tem, elt))
22402 /* Give up right away for nil or t. */
22403 elt = tem;
22404 goto tail_recurse;
22408 break;
22410 case Lisp_Cons:
22412 register Lisp_Object car, tem;
22414 /* A cons cell: five distinct cases.
22415 If first element is :eval or :propertize, do something special.
22416 If first element is a string or a cons, process all the elements
22417 and effectively concatenate them.
22418 If first element is a negative number, truncate displaying cdr to
22419 at most that many characters. If positive, pad (with spaces)
22420 to at least that many characters.
22421 If first element is a symbol, process the cadr or caddr recursively
22422 according to whether the symbol's value is non-nil or nil. */
22423 car = XCAR (elt);
22424 if (EQ (car, QCeval))
22426 /* An element of the form (:eval FORM) means evaluate FORM
22427 and use the result as mode line elements. */
22429 if (risky)
22430 break;
22432 if (CONSP (XCDR (elt)))
22434 Lisp_Object spec;
22435 spec = safe__eval (true, XCAR (XCDR (elt)));
22436 n += display_mode_element (it, depth, field_width - n,
22437 precision - n, spec, props,
22438 risky);
22441 else if (EQ (car, QCpropertize))
22443 /* An element of the form (:propertize ELT PROPS...)
22444 means display ELT but applying properties PROPS. */
22446 if (risky)
22447 break;
22449 if (CONSP (XCDR (elt)))
22450 n += display_mode_element (it, depth, field_width - n,
22451 precision - n, XCAR (XCDR (elt)),
22452 XCDR (XCDR (elt)), risky);
22454 else if (SYMBOLP (car))
22456 tem = Fboundp (car);
22457 elt = XCDR (elt);
22458 if (!CONSP (elt))
22459 goto invalid;
22460 /* elt is now the cdr, and we know it is a cons cell.
22461 Use its car if CAR has a non-nil value. */
22462 if (!NILP (tem))
22464 tem = Fsymbol_value (car);
22465 if (!NILP (tem))
22467 elt = XCAR (elt);
22468 goto tail_recurse;
22471 /* Symbol's value is nil (or symbol is unbound)
22472 Get the cddr of the original list
22473 and if possible find the caddr and use that. */
22474 elt = XCDR (elt);
22475 if (NILP (elt))
22476 break;
22477 else if (!CONSP (elt))
22478 goto invalid;
22479 elt = XCAR (elt);
22480 goto tail_recurse;
22482 else if (INTEGERP (car))
22484 register int lim = XINT (car);
22485 elt = XCDR (elt);
22486 if (lim < 0)
22488 /* Negative int means reduce maximum width. */
22489 if (precision <= 0)
22490 precision = -lim;
22491 else
22492 precision = min (precision, -lim);
22494 else if (lim > 0)
22496 /* Padding specified. Don't let it be more than
22497 current maximum. */
22498 if (precision > 0)
22499 lim = min (precision, lim);
22501 /* If that's more padding than already wanted, queue it.
22502 But don't reduce padding already specified even if
22503 that is beyond the current truncation point. */
22504 field_width = max (lim, field_width);
22506 goto tail_recurse;
22508 else if (STRINGP (car) || CONSP (car))
22510 Lisp_Object halftail = elt;
22511 int len = 0;
22513 while (CONSP (elt)
22514 && (precision <= 0 || n < precision))
22516 n += display_mode_element (it, depth,
22517 /* Do padding only after the last
22518 element in the list. */
22519 (! CONSP (XCDR (elt))
22520 ? field_width - n
22521 : 0),
22522 precision - n, XCAR (elt),
22523 props, risky);
22524 elt = XCDR (elt);
22525 len++;
22526 if ((len & 1) == 0)
22527 halftail = XCDR (halftail);
22528 /* Check for cycle. */
22529 if (EQ (halftail, elt))
22530 break;
22534 break;
22536 default:
22537 invalid:
22538 elt = build_string ("*invalid*");
22539 goto tail_recurse;
22542 /* Pad to FIELD_WIDTH. */
22543 if (field_width > 0 && n < field_width)
22545 switch (mode_line_target)
22547 case MODE_LINE_NOPROP:
22548 case MODE_LINE_TITLE:
22549 n += store_mode_line_noprop ("", field_width - n, 0);
22550 break;
22551 case MODE_LINE_STRING:
22552 n += store_mode_line_string ("", Qnil, 0, field_width - n, 0, Qnil);
22553 break;
22554 case MODE_LINE_DISPLAY:
22555 n += display_string ("", Qnil, Qnil, 0, 0, it, field_width - n,
22556 0, 0, 0);
22557 break;
22561 return n;
22564 /* Store a mode-line string element in mode_line_string_list.
22566 If STRING is non-null, display that C string. Otherwise, the Lisp
22567 string LISP_STRING is displayed.
22569 FIELD_WIDTH is the minimum number of output glyphs to produce.
22570 If STRING has fewer characters than FIELD_WIDTH, pad to the right
22571 with spaces. FIELD_WIDTH <= 0 means don't pad.
22573 PRECISION is the maximum number of characters to output from
22574 STRING. PRECISION <= 0 means don't truncate the string.
22576 If COPY_STRING is non-zero, make a copy of LISP_STRING before adding
22577 properties to the string.
22579 PROPS are the properties to add to the string.
22580 The mode_line_string_face face property is always added to the string.
22583 static int
22584 store_mode_line_string (const char *string, Lisp_Object lisp_string, int copy_string,
22585 int field_width, int precision, Lisp_Object props)
22587 ptrdiff_t len;
22588 int n = 0;
22590 if (string != NULL)
22592 len = strlen (string);
22593 if (precision > 0 && len > precision)
22594 len = precision;
22595 lisp_string = make_string (string, len);
22596 if (NILP (props))
22597 props = mode_line_string_face_prop;
22598 else if (!NILP (mode_line_string_face))
22600 Lisp_Object face = Fplist_get (props, Qface);
22601 props = Fcopy_sequence (props);
22602 if (NILP (face))
22603 face = mode_line_string_face;
22604 else
22605 face = list2 (face, mode_line_string_face);
22606 props = Fplist_put (props, Qface, face);
22608 Fadd_text_properties (make_number (0), make_number (len),
22609 props, lisp_string);
22611 else
22613 len = XFASTINT (Flength (lisp_string));
22614 if (precision > 0 && len > precision)
22616 len = precision;
22617 lisp_string = Fsubstring (lisp_string, make_number (0), make_number (len));
22618 precision = -1;
22620 if (!NILP (mode_line_string_face))
22622 Lisp_Object face;
22623 if (NILP (props))
22624 props = Ftext_properties_at (make_number (0), lisp_string);
22625 face = Fplist_get (props, Qface);
22626 if (NILP (face))
22627 face = mode_line_string_face;
22628 else
22629 face = list2 (face, mode_line_string_face);
22630 props = list2 (Qface, face);
22631 if (copy_string)
22632 lisp_string = Fcopy_sequence (lisp_string);
22634 if (!NILP (props))
22635 Fadd_text_properties (make_number (0), make_number (len),
22636 props, lisp_string);
22639 if (len > 0)
22641 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
22642 n += len;
22645 if (field_width > len)
22647 field_width -= len;
22648 lisp_string = Fmake_string (make_number (field_width), make_number (' '));
22649 if (!NILP (props))
22650 Fadd_text_properties (make_number (0), make_number (field_width),
22651 props, lisp_string);
22652 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
22653 n += field_width;
22656 return n;
22660 DEFUN ("format-mode-line", Fformat_mode_line, Sformat_mode_line,
22661 1, 4, 0,
22662 doc: /* Format a string out of a mode line format specification.
22663 First arg FORMAT specifies the mode line format (see `mode-line-format'
22664 for details) to use.
22666 By default, the format is evaluated for the currently selected window.
22668 Optional second arg FACE specifies the face property to put on all
22669 characters for which no face is specified. The value nil means the
22670 default face. The value t means whatever face the window's mode line
22671 currently uses (either `mode-line' or `mode-line-inactive',
22672 depending on whether the window is the selected window or not).
22673 An integer value means the value string has no text
22674 properties.
22676 Optional third and fourth args WINDOW and BUFFER specify the window
22677 and buffer to use as the context for the formatting (defaults
22678 are the selected window and the WINDOW's buffer). */)
22679 (Lisp_Object format, Lisp_Object face,
22680 Lisp_Object window, Lisp_Object buffer)
22682 struct it it;
22683 int len;
22684 struct window *w;
22685 struct buffer *old_buffer = NULL;
22686 int face_id;
22687 int no_props = INTEGERP (face);
22688 ptrdiff_t count = SPECPDL_INDEX ();
22689 Lisp_Object str;
22690 int string_start = 0;
22692 w = decode_any_window (window);
22693 XSETWINDOW (window, w);
22695 if (NILP (buffer))
22696 buffer = w->contents;
22697 CHECK_BUFFER (buffer);
22699 /* Make formatting the modeline a non-op when noninteractive, otherwise
22700 there will be problems later caused by a partially initialized frame. */
22701 if (NILP (format) || noninteractive)
22702 return empty_unibyte_string;
22704 if (no_props)
22705 face = Qnil;
22707 face_id = (NILP (face) || EQ (face, Qdefault)) ? DEFAULT_FACE_ID
22708 : EQ (face, Qt) ? (EQ (window, selected_window)
22709 ? MODE_LINE_FACE_ID : MODE_LINE_INACTIVE_FACE_ID)
22710 : EQ (face, Qmode_line) ? MODE_LINE_FACE_ID
22711 : EQ (face, Qmode_line_inactive) ? MODE_LINE_INACTIVE_FACE_ID
22712 : EQ (face, Qheader_line) ? HEADER_LINE_FACE_ID
22713 : EQ (face, Qtool_bar) ? TOOL_BAR_FACE_ID
22714 : DEFAULT_FACE_ID;
22716 old_buffer = current_buffer;
22718 /* Save things including mode_line_proptrans_alist,
22719 and set that to nil so that we don't alter the outer value. */
22720 record_unwind_protect (unwind_format_mode_line,
22721 format_mode_line_unwind_data
22722 (XFRAME (WINDOW_FRAME (w)),
22723 old_buffer, selected_window, 1));
22724 mode_line_proptrans_alist = Qnil;
22726 Fselect_window (window, Qt);
22727 set_buffer_internal_1 (XBUFFER (buffer));
22729 init_iterator (&it, w, -1, -1, NULL, face_id);
22731 if (no_props)
22733 mode_line_target = MODE_LINE_NOPROP;
22734 mode_line_string_face_prop = Qnil;
22735 mode_line_string_list = Qnil;
22736 string_start = MODE_LINE_NOPROP_LEN (0);
22738 else
22740 mode_line_target = MODE_LINE_STRING;
22741 mode_line_string_list = Qnil;
22742 mode_line_string_face = face;
22743 mode_line_string_face_prop
22744 = NILP (face) ? Qnil : list2 (Qface, face);
22747 push_kboard (FRAME_KBOARD (it.f));
22748 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
22749 pop_kboard ();
22751 if (no_props)
22753 len = MODE_LINE_NOPROP_LEN (string_start);
22754 str = make_string (mode_line_noprop_buf + string_start, len);
22756 else
22758 mode_line_string_list = Fnreverse (mode_line_string_list);
22759 str = Fmapconcat (intern ("identity"), mode_line_string_list,
22760 empty_unibyte_string);
22763 unbind_to (count, Qnil);
22764 return str;
22767 /* Write a null-terminated, right justified decimal representation of
22768 the positive integer D to BUF using a minimal field width WIDTH. */
22770 static void
22771 pint2str (register char *buf, register int width, register ptrdiff_t d)
22773 register char *p = buf;
22775 if (d <= 0)
22776 *p++ = '0';
22777 else
22779 while (d > 0)
22781 *p++ = d % 10 + '0';
22782 d /= 10;
22786 for (width -= (int) (p - buf); width > 0; --width)
22787 *p++ = ' ';
22788 *p-- = '\0';
22789 while (p > buf)
22791 d = *buf;
22792 *buf++ = *p;
22793 *p-- = d;
22797 /* Write a null-terminated, right justified decimal and "human
22798 readable" representation of the nonnegative integer D to BUF using
22799 a minimal field width WIDTH. D should be smaller than 999.5e24. */
22801 static const char power_letter[] =
22803 0, /* no letter */
22804 'k', /* kilo */
22805 'M', /* mega */
22806 'G', /* giga */
22807 'T', /* tera */
22808 'P', /* peta */
22809 'E', /* exa */
22810 'Z', /* zetta */
22811 'Y' /* yotta */
22814 static void
22815 pint2hrstr (char *buf, int width, ptrdiff_t d)
22817 /* We aim to represent the nonnegative integer D as
22818 QUOTIENT.TENTHS * 10 ^ (3 * EXPONENT). */
22819 ptrdiff_t quotient = d;
22820 int remainder = 0;
22821 /* -1 means: do not use TENTHS. */
22822 int tenths = -1;
22823 int exponent = 0;
22825 /* Length of QUOTIENT.TENTHS as a string. */
22826 int length;
22828 char * psuffix;
22829 char * p;
22831 if (quotient >= 1000)
22833 /* Scale to the appropriate EXPONENT. */
22836 remainder = quotient % 1000;
22837 quotient /= 1000;
22838 exponent++;
22840 while (quotient >= 1000);
22842 /* Round to nearest and decide whether to use TENTHS or not. */
22843 if (quotient <= 9)
22845 tenths = remainder / 100;
22846 if (remainder % 100 >= 50)
22848 if (tenths < 9)
22849 tenths++;
22850 else
22852 quotient++;
22853 if (quotient == 10)
22854 tenths = -1;
22855 else
22856 tenths = 0;
22860 else
22861 if (remainder >= 500)
22863 if (quotient < 999)
22864 quotient++;
22865 else
22867 quotient = 1;
22868 exponent++;
22869 tenths = 0;
22874 /* Calculate the LENGTH of QUOTIENT.TENTHS as a string. */
22875 if (tenths == -1 && quotient <= 99)
22876 if (quotient <= 9)
22877 length = 1;
22878 else
22879 length = 2;
22880 else
22881 length = 3;
22882 p = psuffix = buf + max (width, length);
22884 /* Print EXPONENT. */
22885 *psuffix++ = power_letter[exponent];
22886 *psuffix = '\0';
22888 /* Print TENTHS. */
22889 if (tenths >= 0)
22891 *--p = '0' + tenths;
22892 *--p = '.';
22895 /* Print QUOTIENT. */
22898 int digit = quotient % 10;
22899 *--p = '0' + digit;
22901 while ((quotient /= 10) != 0);
22903 /* Print leading spaces. */
22904 while (buf < p)
22905 *--p = ' ';
22908 /* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
22909 If EOL_FLAG is 1, set also a mnemonic character for end-of-line
22910 type of CODING_SYSTEM. Return updated pointer into BUF. */
22912 static unsigned char invalid_eol_type[] = "(*invalid*)";
22914 static char *
22915 decode_mode_spec_coding (Lisp_Object coding_system, register char *buf, int eol_flag)
22917 Lisp_Object val;
22918 bool multibyte = !NILP (BVAR (current_buffer, enable_multibyte_characters));
22919 const unsigned char *eol_str;
22920 int eol_str_len;
22921 /* The EOL conversion we are using. */
22922 Lisp_Object eoltype;
22924 val = CODING_SYSTEM_SPEC (coding_system);
22925 eoltype = Qnil;
22927 if (!VECTORP (val)) /* Not yet decided. */
22929 *buf++ = multibyte ? '-' : ' ';
22930 if (eol_flag)
22931 eoltype = eol_mnemonic_undecided;
22932 /* Don't mention EOL conversion if it isn't decided. */
22934 else
22936 Lisp_Object attrs;
22937 Lisp_Object eolvalue;
22939 attrs = AREF (val, 0);
22940 eolvalue = AREF (val, 2);
22942 *buf++ = multibyte
22943 ? XFASTINT (CODING_ATTR_MNEMONIC (attrs))
22944 : ' ';
22946 if (eol_flag)
22948 /* The EOL conversion that is normal on this system. */
22950 if (NILP (eolvalue)) /* Not yet decided. */
22951 eoltype = eol_mnemonic_undecided;
22952 else if (VECTORP (eolvalue)) /* Not yet decided. */
22953 eoltype = eol_mnemonic_undecided;
22954 else /* eolvalue is Qunix, Qdos, or Qmac. */
22955 eoltype = (EQ (eolvalue, Qunix)
22956 ? eol_mnemonic_unix
22957 : (EQ (eolvalue, Qdos) == 1
22958 ? eol_mnemonic_dos : eol_mnemonic_mac));
22962 if (eol_flag)
22964 /* Mention the EOL conversion if it is not the usual one. */
22965 if (STRINGP (eoltype))
22967 eol_str = SDATA (eoltype);
22968 eol_str_len = SBYTES (eoltype);
22970 else if (CHARACTERP (eoltype))
22972 int c = XFASTINT (eoltype);
22973 return buf + CHAR_STRING (c, (unsigned char *) buf);
22975 else
22977 eol_str = invalid_eol_type;
22978 eol_str_len = sizeof (invalid_eol_type) - 1;
22980 memcpy (buf, eol_str, eol_str_len);
22981 buf += eol_str_len;
22984 return buf;
22987 /* Return a string for the output of a mode line %-spec for window W,
22988 generated by character C. FIELD_WIDTH > 0 means pad the string
22989 returned with spaces to that value. Return a Lisp string in
22990 *STRING if the resulting string is taken from that Lisp string.
22992 Note we operate on the current buffer for most purposes. */
22994 static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
22996 static const char *
22997 decode_mode_spec (struct window *w, register int c, int field_width,
22998 Lisp_Object *string)
23000 Lisp_Object obj;
23001 struct frame *f = XFRAME (WINDOW_FRAME (w));
23002 char *decode_mode_spec_buf = f->decode_mode_spec_buffer;
23003 /* We are going to use f->decode_mode_spec_buffer as the buffer to
23004 produce strings from numerical values, so limit preposterously
23005 large values of FIELD_WIDTH to avoid overrunning the buffer's
23006 end. The size of the buffer is enough for FRAME_MESSAGE_BUF_SIZE
23007 bytes plus the terminating null. */
23008 int width = min (field_width, FRAME_MESSAGE_BUF_SIZE (f));
23009 struct buffer *b = current_buffer;
23011 obj = Qnil;
23012 *string = Qnil;
23014 switch (c)
23016 case '*':
23017 if (!NILP (BVAR (b, read_only)))
23018 return "%";
23019 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
23020 return "*";
23021 return "-";
23023 case '+':
23024 /* This differs from %* only for a modified read-only buffer. */
23025 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
23026 return "*";
23027 if (!NILP (BVAR (b, read_only)))
23028 return "%";
23029 return "-";
23031 case '&':
23032 /* This differs from %* in ignoring read-only-ness. */
23033 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
23034 return "*";
23035 return "-";
23037 case '%':
23038 return "%";
23040 case '[':
23042 int i;
23043 char *p;
23045 if (command_loop_level > 5)
23046 return "[[[... ";
23047 p = decode_mode_spec_buf;
23048 for (i = 0; i < command_loop_level; i++)
23049 *p++ = '[';
23050 *p = 0;
23051 return decode_mode_spec_buf;
23054 case ']':
23056 int i;
23057 char *p;
23059 if (command_loop_level > 5)
23060 return " ...]]]";
23061 p = decode_mode_spec_buf;
23062 for (i = 0; i < command_loop_level; i++)
23063 *p++ = ']';
23064 *p = 0;
23065 return decode_mode_spec_buf;
23068 case '-':
23070 register int i;
23072 /* Let lots_of_dashes be a string of infinite length. */
23073 if (mode_line_target == MODE_LINE_NOPROP
23074 || mode_line_target == MODE_LINE_STRING)
23075 return "--";
23076 if (field_width <= 0
23077 || field_width > sizeof (lots_of_dashes))
23079 for (i = 0; i < FRAME_MESSAGE_BUF_SIZE (f) - 1; ++i)
23080 decode_mode_spec_buf[i] = '-';
23081 decode_mode_spec_buf[i] = '\0';
23082 return decode_mode_spec_buf;
23084 else
23085 return lots_of_dashes;
23088 case 'b':
23089 obj = BVAR (b, name);
23090 break;
23092 case 'c':
23093 /* %c and %l are ignored in `frame-title-format'.
23094 (In redisplay_internal, the frame title is drawn _before_ the
23095 windows are updated, so the stuff which depends on actual
23096 window contents (such as %l) may fail to render properly, or
23097 even crash emacs.) */
23098 if (mode_line_target == MODE_LINE_TITLE)
23099 return "";
23100 else
23102 ptrdiff_t col = current_column ();
23103 w->column_number_displayed = col;
23104 pint2str (decode_mode_spec_buf, width, col);
23105 return decode_mode_spec_buf;
23108 case 'e':
23109 #if !defined SYSTEM_MALLOC && !defined HYBRID_MALLOC
23111 if (NILP (Vmemory_full))
23112 return "";
23113 else
23114 return "!MEM FULL! ";
23116 #else
23117 return "";
23118 #endif
23120 case 'F':
23121 /* %F displays the frame name. */
23122 if (!NILP (f->title))
23123 return SSDATA (f->title);
23124 if (f->explicit_name || ! FRAME_WINDOW_P (f))
23125 return SSDATA (f->name);
23126 return "Emacs";
23128 case 'f':
23129 obj = BVAR (b, filename);
23130 break;
23132 case 'i':
23134 ptrdiff_t size = ZV - BEGV;
23135 pint2str (decode_mode_spec_buf, width, size);
23136 return decode_mode_spec_buf;
23139 case 'I':
23141 ptrdiff_t size = ZV - BEGV;
23142 pint2hrstr (decode_mode_spec_buf, width, size);
23143 return decode_mode_spec_buf;
23146 case 'l':
23148 ptrdiff_t startpos, startpos_byte, line, linepos, linepos_byte;
23149 ptrdiff_t topline, nlines, height;
23150 ptrdiff_t junk;
23152 /* %c and %l are ignored in `frame-title-format'. */
23153 if (mode_line_target == MODE_LINE_TITLE)
23154 return "";
23156 startpos = marker_position (w->start);
23157 startpos_byte = marker_byte_position (w->start);
23158 height = WINDOW_TOTAL_LINES (w);
23160 /* If we decided that this buffer isn't suitable for line numbers,
23161 don't forget that too fast. */
23162 if (w->base_line_pos == -1)
23163 goto no_value;
23165 /* If the buffer is very big, don't waste time. */
23166 if (INTEGERP (Vline_number_display_limit)
23167 && BUF_ZV (b) - BUF_BEGV (b) > XINT (Vline_number_display_limit))
23169 w->base_line_pos = 0;
23170 w->base_line_number = 0;
23171 goto no_value;
23174 if (w->base_line_number > 0
23175 && w->base_line_pos > 0
23176 && w->base_line_pos <= startpos)
23178 line = w->base_line_number;
23179 linepos = w->base_line_pos;
23180 linepos_byte = buf_charpos_to_bytepos (b, linepos);
23182 else
23184 line = 1;
23185 linepos = BUF_BEGV (b);
23186 linepos_byte = BUF_BEGV_BYTE (b);
23189 /* Count lines from base line to window start position. */
23190 nlines = display_count_lines (linepos_byte,
23191 startpos_byte,
23192 startpos, &junk);
23194 topline = nlines + line;
23196 /* Determine a new base line, if the old one is too close
23197 or too far away, or if we did not have one.
23198 "Too close" means it's plausible a scroll-down would
23199 go back past it. */
23200 if (startpos == BUF_BEGV (b))
23202 w->base_line_number = topline;
23203 w->base_line_pos = BUF_BEGV (b);
23205 else if (nlines < height + 25 || nlines > height * 3 + 50
23206 || linepos == BUF_BEGV (b))
23208 ptrdiff_t limit = BUF_BEGV (b);
23209 ptrdiff_t limit_byte = BUF_BEGV_BYTE (b);
23210 ptrdiff_t position;
23211 ptrdiff_t distance =
23212 (height * 2 + 30) * line_number_display_limit_width;
23214 if (startpos - distance > limit)
23216 limit = startpos - distance;
23217 limit_byte = CHAR_TO_BYTE (limit);
23220 nlines = display_count_lines (startpos_byte,
23221 limit_byte,
23222 - (height * 2 + 30),
23223 &position);
23224 /* If we couldn't find the lines we wanted within
23225 line_number_display_limit_width chars per line,
23226 give up on line numbers for this window. */
23227 if (position == limit_byte && limit == startpos - distance)
23229 w->base_line_pos = -1;
23230 w->base_line_number = 0;
23231 goto no_value;
23234 w->base_line_number = topline - nlines;
23235 w->base_line_pos = BYTE_TO_CHAR (position);
23238 /* Now count lines from the start pos to point. */
23239 nlines = display_count_lines (startpos_byte,
23240 PT_BYTE, PT, &junk);
23242 /* Record that we did display the line number. */
23243 line_number_displayed = 1;
23245 /* Make the string to show. */
23246 pint2str (decode_mode_spec_buf, width, topline + nlines);
23247 return decode_mode_spec_buf;
23248 no_value:
23250 char *p = decode_mode_spec_buf;
23251 int pad = width - 2;
23252 while (pad-- > 0)
23253 *p++ = ' ';
23254 *p++ = '?';
23255 *p++ = '?';
23256 *p = '\0';
23257 return decode_mode_spec_buf;
23260 break;
23262 case 'm':
23263 obj = BVAR (b, mode_name);
23264 break;
23266 case 'n':
23267 if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
23268 return " Narrow";
23269 break;
23271 case 'p':
23273 ptrdiff_t pos = marker_position (w->start);
23274 ptrdiff_t total = BUF_ZV (b) - BUF_BEGV (b);
23276 if (w->window_end_pos <= BUF_Z (b) - BUF_ZV (b))
23278 if (pos <= BUF_BEGV (b))
23279 return "All";
23280 else
23281 return "Bottom";
23283 else if (pos <= BUF_BEGV (b))
23284 return "Top";
23285 else
23287 if (total > 1000000)
23288 /* Do it differently for a large value, to avoid overflow. */
23289 total = ((pos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
23290 else
23291 total = ((pos - BUF_BEGV (b)) * 100 + total - 1) / total;
23292 /* We can't normally display a 3-digit number,
23293 so get us a 2-digit number that is close. */
23294 if (total == 100)
23295 total = 99;
23296 sprintf (decode_mode_spec_buf, "%2"pD"d%%", total);
23297 return decode_mode_spec_buf;
23301 /* Display percentage of size above the bottom of the screen. */
23302 case 'P':
23304 ptrdiff_t toppos = marker_position (w->start);
23305 ptrdiff_t botpos = BUF_Z (b) - w->window_end_pos;
23306 ptrdiff_t total = BUF_ZV (b) - BUF_BEGV (b);
23308 if (botpos >= BUF_ZV (b))
23310 if (toppos <= BUF_BEGV (b))
23311 return "All";
23312 else
23313 return "Bottom";
23315 else
23317 if (total > 1000000)
23318 /* Do it differently for a large value, to avoid overflow. */
23319 total = ((botpos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
23320 else
23321 total = ((botpos - BUF_BEGV (b)) * 100 + total - 1) / total;
23322 /* We can't normally display a 3-digit number,
23323 so get us a 2-digit number that is close. */
23324 if (total == 100)
23325 total = 99;
23326 if (toppos <= BUF_BEGV (b))
23327 sprintf (decode_mode_spec_buf, "Top%2"pD"d%%", total);
23328 else
23329 sprintf (decode_mode_spec_buf, "%2"pD"d%%", total);
23330 return decode_mode_spec_buf;
23334 case 's':
23335 /* status of process */
23336 obj = Fget_buffer_process (Fcurrent_buffer ());
23337 if (NILP (obj))
23338 return "no process";
23339 #ifndef MSDOS
23340 obj = Fsymbol_name (Fprocess_status (obj));
23341 #endif
23342 break;
23344 case '@':
23346 ptrdiff_t count = inhibit_garbage_collection ();
23347 Lisp_Object curdir = BVAR (current_buffer, directory);
23348 Lisp_Object val = Qnil;
23350 if (STRINGP (curdir))
23351 val = call1 (intern ("file-remote-p"), curdir);
23353 unbind_to (count, Qnil);
23355 if (NILP (val))
23356 return "-";
23357 else
23358 return "@";
23361 case 'z':
23362 /* coding-system (not including end-of-line format) */
23363 case 'Z':
23364 /* coding-system (including end-of-line type) */
23366 int eol_flag = (c == 'Z');
23367 char *p = decode_mode_spec_buf;
23369 if (! FRAME_WINDOW_P (f))
23371 /* No need to mention EOL here--the terminal never needs
23372 to do EOL conversion. */
23373 p = decode_mode_spec_coding (CODING_ID_NAME
23374 (FRAME_KEYBOARD_CODING (f)->id),
23375 p, 0);
23376 p = decode_mode_spec_coding (CODING_ID_NAME
23377 (FRAME_TERMINAL_CODING (f)->id),
23378 p, 0);
23380 p = decode_mode_spec_coding (BVAR (b, buffer_file_coding_system),
23381 p, eol_flag);
23383 #if 0 /* This proves to be annoying; I think we can do without. -- rms. */
23384 #ifdef subprocesses
23385 obj = Fget_buffer_process (Fcurrent_buffer ());
23386 if (PROCESSP (obj))
23388 p = decode_mode_spec_coding
23389 (XPROCESS (obj)->decode_coding_system, p, eol_flag);
23390 p = decode_mode_spec_coding
23391 (XPROCESS (obj)->encode_coding_system, p, eol_flag);
23393 #endif /* subprocesses */
23394 #endif /* 0 */
23395 *p = 0;
23396 return decode_mode_spec_buf;
23400 if (STRINGP (obj))
23402 *string = obj;
23403 return SSDATA (obj);
23405 else
23406 return "";
23410 /* Count up to COUNT lines starting from START_BYTE. COUNT negative
23411 means count lines back from START_BYTE. But don't go beyond
23412 LIMIT_BYTE. Return the number of lines thus found (always
23413 nonnegative).
23415 Set *BYTE_POS_PTR to the byte position where we stopped. This is
23416 either the position COUNT lines after/before START_BYTE, if we
23417 found COUNT lines, or LIMIT_BYTE if we hit the limit before finding
23418 COUNT lines. */
23420 static ptrdiff_t
23421 display_count_lines (ptrdiff_t start_byte,
23422 ptrdiff_t limit_byte, ptrdiff_t count,
23423 ptrdiff_t *byte_pos_ptr)
23425 register unsigned char *cursor;
23426 unsigned char *base;
23428 register ptrdiff_t ceiling;
23429 register unsigned char *ceiling_addr;
23430 ptrdiff_t orig_count = count;
23432 /* If we are not in selective display mode,
23433 check only for newlines. */
23434 int selective_display = (!NILP (BVAR (current_buffer, selective_display))
23435 && !INTEGERP (BVAR (current_buffer, selective_display)));
23437 if (count > 0)
23439 while (start_byte < limit_byte)
23441 ceiling = BUFFER_CEILING_OF (start_byte);
23442 ceiling = min (limit_byte - 1, ceiling);
23443 ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
23444 base = (cursor = BYTE_POS_ADDR (start_byte));
23448 if (selective_display)
23450 while (*cursor != '\n' && *cursor != 015
23451 && ++cursor != ceiling_addr)
23452 continue;
23453 if (cursor == ceiling_addr)
23454 break;
23456 else
23458 cursor = memchr (cursor, '\n', ceiling_addr - cursor);
23459 if (! cursor)
23460 break;
23463 cursor++;
23465 if (--count == 0)
23467 start_byte += cursor - base;
23468 *byte_pos_ptr = start_byte;
23469 return orig_count;
23472 while (cursor < ceiling_addr);
23474 start_byte += ceiling_addr - base;
23477 else
23479 while (start_byte > limit_byte)
23481 ceiling = BUFFER_FLOOR_OF (start_byte - 1);
23482 ceiling = max (limit_byte, ceiling);
23483 ceiling_addr = BYTE_POS_ADDR (ceiling);
23484 base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
23485 while (1)
23487 if (selective_display)
23489 while (--cursor >= ceiling_addr
23490 && *cursor != '\n' && *cursor != 015)
23491 continue;
23492 if (cursor < ceiling_addr)
23493 break;
23495 else
23497 cursor = memrchr (ceiling_addr, '\n', cursor - ceiling_addr);
23498 if (! cursor)
23499 break;
23502 if (++count == 0)
23504 start_byte += cursor - base + 1;
23505 *byte_pos_ptr = start_byte;
23506 /* When scanning backwards, we should
23507 not count the newline posterior to which we stop. */
23508 return - orig_count - 1;
23511 start_byte += ceiling_addr - base;
23515 *byte_pos_ptr = limit_byte;
23517 if (count < 0)
23518 return - orig_count + count;
23519 return orig_count - count;
23525 /***********************************************************************
23526 Displaying strings
23527 ***********************************************************************/
23529 /* Display a NUL-terminated string, starting with index START.
23531 If STRING is non-null, display that C string. Otherwise, the Lisp
23532 string LISP_STRING is displayed. There's a case that STRING is
23533 non-null and LISP_STRING is not nil. It means STRING is a string
23534 data of LISP_STRING. In that case, we display LISP_STRING while
23535 ignoring its text properties.
23537 If FACE_STRING is not nil, FACE_STRING_POS is a position in
23538 FACE_STRING. Display STRING or LISP_STRING with the face at
23539 FACE_STRING_POS in FACE_STRING:
23541 Display the string in the environment given by IT, but use the
23542 standard display table, temporarily.
23544 FIELD_WIDTH is the minimum number of output glyphs to produce.
23545 If STRING has fewer characters than FIELD_WIDTH, pad to the right
23546 with spaces. If STRING has more characters, more than FIELD_WIDTH
23547 glyphs will be produced. FIELD_WIDTH <= 0 means don't pad.
23549 PRECISION is the maximum number of characters to output from
23550 STRING. PRECISION < 0 means don't truncate the string.
23552 This is roughly equivalent to printf format specifiers:
23554 FIELD_WIDTH PRECISION PRINTF
23555 ----------------------------------------
23556 -1 -1 %s
23557 -1 10 %.10s
23558 10 -1 %10s
23559 20 10 %20.10s
23561 MULTIBYTE zero means do not display multibyte chars, > 0 means do
23562 display them, and < 0 means obey the current buffer's value of
23563 enable_multibyte_characters.
23565 Value is the number of columns displayed. */
23567 static int
23568 display_string (const char *string, Lisp_Object lisp_string, Lisp_Object face_string,
23569 ptrdiff_t face_string_pos, ptrdiff_t start, struct it *it,
23570 int field_width, int precision, int max_x, int multibyte)
23572 int hpos_at_start = it->hpos;
23573 int saved_face_id = it->face_id;
23574 struct glyph_row *row = it->glyph_row;
23575 ptrdiff_t it_charpos;
23577 /* Initialize the iterator IT for iteration over STRING beginning
23578 with index START. */
23579 reseat_to_string (it, NILP (lisp_string) ? string : NULL, lisp_string, start,
23580 precision, field_width, multibyte);
23581 if (string && STRINGP (lisp_string))
23582 /* LISP_STRING is the one returned by decode_mode_spec. We should
23583 ignore its text properties. */
23584 it->stop_charpos = it->end_charpos;
23586 /* If displaying STRING, set up the face of the iterator from
23587 FACE_STRING, if that's given. */
23588 if (STRINGP (face_string))
23590 ptrdiff_t endptr;
23591 struct face *face;
23593 it->face_id
23594 = face_at_string_position (it->w, face_string, face_string_pos,
23595 0, &endptr, it->base_face_id, false);
23596 face = FACE_FROM_ID (it->f, it->face_id);
23597 it->face_box_p = face->box != FACE_NO_BOX;
23600 /* Set max_x to the maximum allowed X position. Don't let it go
23601 beyond the right edge of the window. */
23602 if (max_x <= 0)
23603 max_x = it->last_visible_x;
23604 else
23605 max_x = min (max_x, it->last_visible_x);
23607 /* Skip over display elements that are not visible. because IT->w is
23608 hscrolled. */
23609 if (it->current_x < it->first_visible_x)
23610 move_it_in_display_line_to (it, 100000, it->first_visible_x,
23611 MOVE_TO_POS | MOVE_TO_X);
23613 row->ascent = it->max_ascent;
23614 row->height = it->max_ascent + it->max_descent;
23615 row->phys_ascent = it->max_phys_ascent;
23616 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
23617 row->extra_line_spacing = it->max_extra_line_spacing;
23619 if (STRINGP (it->string))
23620 it_charpos = IT_STRING_CHARPOS (*it);
23621 else
23622 it_charpos = IT_CHARPOS (*it);
23624 /* This condition is for the case that we are called with current_x
23625 past last_visible_x. */
23626 while (it->current_x < max_x)
23628 int x_before, x, n_glyphs_before, i, nglyphs;
23630 /* Get the next display element. */
23631 if (!get_next_display_element (it))
23632 break;
23634 /* Produce glyphs. */
23635 x_before = it->current_x;
23636 n_glyphs_before = row->used[TEXT_AREA];
23637 PRODUCE_GLYPHS (it);
23639 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
23640 i = 0;
23641 x = x_before;
23642 while (i < nglyphs)
23644 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
23646 if (it->line_wrap != TRUNCATE
23647 && x + glyph->pixel_width > max_x)
23649 /* End of continued line or max_x reached. */
23650 if (CHAR_GLYPH_PADDING_P (*glyph))
23652 /* A wide character is unbreakable. */
23653 if (row->reversed_p)
23654 unproduce_glyphs (it, row->used[TEXT_AREA]
23655 - n_glyphs_before);
23656 row->used[TEXT_AREA] = n_glyphs_before;
23657 it->current_x = x_before;
23659 else
23661 if (row->reversed_p)
23662 unproduce_glyphs (it, row->used[TEXT_AREA]
23663 - (n_glyphs_before + i));
23664 row->used[TEXT_AREA] = n_glyphs_before + i;
23665 it->current_x = x;
23667 break;
23669 else if (x + glyph->pixel_width >= it->first_visible_x)
23671 /* Glyph is at least partially visible. */
23672 ++it->hpos;
23673 if (x < it->first_visible_x)
23674 row->x = x - it->first_visible_x;
23676 else
23678 /* Glyph is off the left margin of the display area.
23679 Should not happen. */
23680 emacs_abort ();
23683 row->ascent = max (row->ascent, it->max_ascent);
23684 row->height = max (row->height, it->max_ascent + it->max_descent);
23685 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
23686 row->phys_height = max (row->phys_height,
23687 it->max_phys_ascent + it->max_phys_descent);
23688 row->extra_line_spacing = max (row->extra_line_spacing,
23689 it->max_extra_line_spacing);
23690 x += glyph->pixel_width;
23691 ++i;
23694 /* Stop if max_x reached. */
23695 if (i < nglyphs)
23696 break;
23698 /* Stop at line ends. */
23699 if (ITERATOR_AT_END_OF_LINE_P (it))
23701 it->continuation_lines_width = 0;
23702 break;
23705 set_iterator_to_next (it, 1);
23706 if (STRINGP (it->string))
23707 it_charpos = IT_STRING_CHARPOS (*it);
23708 else
23709 it_charpos = IT_CHARPOS (*it);
23711 /* Stop if truncating at the right edge. */
23712 if (it->line_wrap == TRUNCATE
23713 && it->current_x >= it->last_visible_x)
23715 /* Add truncation mark, but don't do it if the line is
23716 truncated at a padding space. */
23717 if (it_charpos < it->string_nchars)
23719 if (!FRAME_WINDOW_P (it->f))
23721 int ii, n;
23723 if (it->current_x > it->last_visible_x)
23725 if (!row->reversed_p)
23727 for (ii = row->used[TEXT_AREA] - 1; ii > 0; --ii)
23728 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][ii]))
23729 break;
23731 else
23733 for (ii = 0; ii < row->used[TEXT_AREA]; ii++)
23734 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][ii]))
23735 break;
23736 unproduce_glyphs (it, ii + 1);
23737 ii = row->used[TEXT_AREA] - (ii + 1);
23739 for (n = row->used[TEXT_AREA]; ii < n; ++ii)
23741 row->used[TEXT_AREA] = ii;
23742 produce_special_glyphs (it, IT_TRUNCATION);
23745 produce_special_glyphs (it, IT_TRUNCATION);
23747 row->truncated_on_right_p = 1;
23749 break;
23753 /* Maybe insert a truncation at the left. */
23754 if (it->first_visible_x
23755 && it_charpos > 0)
23757 if (!FRAME_WINDOW_P (it->f)
23758 || (row->reversed_p
23759 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
23760 : WINDOW_LEFT_FRINGE_WIDTH (it->w)) == 0)
23761 insert_left_trunc_glyphs (it);
23762 row->truncated_on_left_p = 1;
23765 it->face_id = saved_face_id;
23767 /* Value is number of columns displayed. */
23768 return it->hpos - hpos_at_start;
23773 /* This is like a combination of memq and assq. Return 1/2 if PROPVAL
23774 appears as an element of LIST or as the car of an element of LIST.
23775 If PROPVAL is a list, compare each element against LIST in that
23776 way, and return 1/2 if any element of PROPVAL is found in LIST.
23777 Otherwise return 0. This function cannot quit.
23778 The return value is 2 if the text is invisible but with an ellipsis
23779 and 1 if it's invisible and without an ellipsis. */
23782 invisible_p (register Lisp_Object propval, Lisp_Object list)
23784 register Lisp_Object tail, proptail;
23786 for (tail = list; CONSP (tail); tail = XCDR (tail))
23788 register Lisp_Object tem;
23789 tem = XCAR (tail);
23790 if (EQ (propval, tem))
23791 return 1;
23792 if (CONSP (tem) && EQ (propval, XCAR (tem)))
23793 return NILP (XCDR (tem)) ? 1 : 2;
23796 if (CONSP (propval))
23798 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
23800 Lisp_Object propelt;
23801 propelt = XCAR (proptail);
23802 for (tail = list; CONSP (tail); tail = XCDR (tail))
23804 register Lisp_Object tem;
23805 tem = XCAR (tail);
23806 if (EQ (propelt, tem))
23807 return 1;
23808 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
23809 return NILP (XCDR (tem)) ? 1 : 2;
23814 return 0;
23817 DEFUN ("invisible-p", Finvisible_p, Sinvisible_p, 1, 1, 0,
23818 doc: /* Non-nil if the property makes the text invisible.
23819 POS-OR-PROP can be a marker or number, in which case it is taken to be
23820 a position in the current buffer and the value of the `invisible' property
23821 is checked; or it can be some other value, which is then presumed to be the
23822 value of the `invisible' property of the text of interest.
23823 The non-nil value returned can be t for truly invisible text or something
23824 else if the text is replaced by an ellipsis. */)
23825 (Lisp_Object pos_or_prop)
23827 Lisp_Object prop
23828 = (NATNUMP (pos_or_prop) || MARKERP (pos_or_prop)
23829 ? Fget_char_property (pos_or_prop, Qinvisible, Qnil)
23830 : pos_or_prop);
23831 int invis = TEXT_PROP_MEANS_INVISIBLE (prop);
23832 return (invis == 0 ? Qnil
23833 : invis == 1 ? Qt
23834 : make_number (invis));
23837 /* Calculate a width or height in pixels from a specification using
23838 the following elements:
23840 SPEC ::=
23841 NUM - a (fractional) multiple of the default font width/height
23842 (NUM) - specifies exactly NUM pixels
23843 UNIT - a fixed number of pixels, see below.
23844 ELEMENT - size of a display element in pixels, see below.
23845 (NUM . SPEC) - equals NUM * SPEC
23846 (+ SPEC SPEC ...) - add pixel values
23847 (- SPEC SPEC ...) - subtract pixel values
23848 (- SPEC) - negate pixel value
23850 NUM ::=
23851 INT or FLOAT - a number constant
23852 SYMBOL - use symbol's (buffer local) variable binding.
23854 UNIT ::=
23855 in - pixels per inch *)
23856 mm - pixels per 1/1000 meter *)
23857 cm - pixels per 1/100 meter *)
23858 width - width of current font in pixels.
23859 height - height of current font in pixels.
23861 *) using the ratio(s) defined in display-pixels-per-inch.
23863 ELEMENT ::=
23865 left-fringe - left fringe width in pixels
23866 right-fringe - right fringe width in pixels
23868 left-margin - left margin width in pixels
23869 right-margin - right margin width in pixels
23871 scroll-bar - scroll-bar area width in pixels
23873 Examples:
23875 Pixels corresponding to 5 inches:
23876 (5 . in)
23878 Total width of non-text areas on left side of window (if scroll-bar is on left):
23879 '(space :width (+ left-fringe left-margin scroll-bar))
23881 Align to first text column (in header line):
23882 '(space :align-to 0)
23884 Align to middle of text area minus half the width of variable `my-image'
23885 containing a loaded image:
23886 '(space :align-to (0.5 . (- text my-image)))
23888 Width of left margin minus width of 1 character in the default font:
23889 '(space :width (- left-margin 1))
23891 Width of left margin minus width of 2 characters in the current font:
23892 '(space :width (- left-margin (2 . width)))
23894 Center 1 character over left-margin (in header line):
23895 '(space :align-to (+ left-margin (0.5 . left-margin) -0.5))
23897 Different ways to express width of left fringe plus left margin minus one pixel:
23898 '(space :width (- (+ left-fringe left-margin) (1)))
23899 '(space :width (+ left-fringe left-margin (- (1))))
23900 '(space :width (+ left-fringe left-margin (-1)))
23904 static int
23905 calc_pixel_width_or_height (double *res, struct it *it, Lisp_Object prop,
23906 struct font *font, int width_p, int *align_to)
23908 double pixels;
23910 #define OK_PIXELS(val) ((*res = (double)(val)), 1)
23911 #define OK_ALIGN_TO(val) ((*align_to = (int)(val)), 1)
23913 if (NILP (prop))
23914 return OK_PIXELS (0);
23916 eassert (FRAME_LIVE_P (it->f));
23918 if (SYMBOLP (prop))
23920 if (SCHARS (SYMBOL_NAME (prop)) == 2)
23922 char *unit = SSDATA (SYMBOL_NAME (prop));
23924 if (unit[0] == 'i' && unit[1] == 'n')
23925 pixels = 1.0;
23926 else if (unit[0] == 'm' && unit[1] == 'm')
23927 pixels = 25.4;
23928 else if (unit[0] == 'c' && unit[1] == 'm')
23929 pixels = 2.54;
23930 else
23931 pixels = 0;
23932 if (pixels > 0)
23934 double ppi = (width_p ? FRAME_RES_X (it->f)
23935 : FRAME_RES_Y (it->f));
23937 if (ppi > 0)
23938 return OK_PIXELS (ppi / pixels);
23939 return 0;
23943 #ifdef HAVE_WINDOW_SYSTEM
23944 if (EQ (prop, Qheight))
23945 return OK_PIXELS (font ? FONT_HEIGHT (font) : FRAME_LINE_HEIGHT (it->f));
23946 if (EQ (prop, Qwidth))
23947 return OK_PIXELS (font ? FONT_WIDTH (font) : FRAME_COLUMN_WIDTH (it->f));
23948 #else
23949 if (EQ (prop, Qheight) || EQ (prop, Qwidth))
23950 return OK_PIXELS (1);
23951 #endif
23953 if (EQ (prop, Qtext))
23954 return OK_PIXELS (width_p
23955 ? window_box_width (it->w, TEXT_AREA)
23956 : WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w));
23958 if (align_to && *align_to < 0)
23960 *res = 0;
23961 if (EQ (prop, Qleft))
23962 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA));
23963 if (EQ (prop, Qright))
23964 return OK_ALIGN_TO (window_box_right_offset (it->w, TEXT_AREA));
23965 if (EQ (prop, Qcenter))
23966 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA)
23967 + window_box_width (it->w, TEXT_AREA) / 2);
23968 if (EQ (prop, Qleft_fringe))
23969 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
23970 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (it->w)
23971 : window_box_right_offset (it->w, LEFT_MARGIN_AREA));
23972 if (EQ (prop, Qright_fringe))
23973 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
23974 ? window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
23975 : window_box_right_offset (it->w, TEXT_AREA));
23976 if (EQ (prop, Qleft_margin))
23977 return OK_ALIGN_TO (window_box_left_offset (it->w, LEFT_MARGIN_AREA));
23978 if (EQ (prop, Qright_margin))
23979 return OK_ALIGN_TO (window_box_left_offset (it->w, RIGHT_MARGIN_AREA));
23980 if (EQ (prop, Qscroll_bar))
23981 return OK_ALIGN_TO (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (it->w)
23983 : (window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
23984 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
23985 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
23986 : 0)));
23988 else
23990 if (EQ (prop, Qleft_fringe))
23991 return OK_PIXELS (WINDOW_LEFT_FRINGE_WIDTH (it->w));
23992 if (EQ (prop, Qright_fringe))
23993 return OK_PIXELS (WINDOW_RIGHT_FRINGE_WIDTH (it->w));
23994 if (EQ (prop, Qleft_margin))
23995 return OK_PIXELS (WINDOW_LEFT_MARGIN_WIDTH (it->w));
23996 if (EQ (prop, Qright_margin))
23997 return OK_PIXELS (WINDOW_RIGHT_MARGIN_WIDTH (it->w));
23998 if (EQ (prop, Qscroll_bar))
23999 return OK_PIXELS (WINDOW_SCROLL_BAR_AREA_WIDTH (it->w));
24002 prop = buffer_local_value (prop, it->w->contents);
24003 if (EQ (prop, Qunbound))
24004 prop = Qnil;
24007 if (INTEGERP (prop) || FLOATP (prop))
24009 int base_unit = (width_p
24010 ? FRAME_COLUMN_WIDTH (it->f)
24011 : FRAME_LINE_HEIGHT (it->f));
24012 return OK_PIXELS (XFLOATINT (prop) * base_unit);
24015 if (CONSP (prop))
24017 Lisp_Object car = XCAR (prop);
24018 Lisp_Object cdr = XCDR (prop);
24020 if (SYMBOLP (car))
24022 #ifdef HAVE_WINDOW_SYSTEM
24023 if (FRAME_WINDOW_P (it->f)
24024 && valid_image_p (prop))
24026 ptrdiff_t id = lookup_image (it->f, prop);
24027 struct image *img = IMAGE_FROM_ID (it->f, id);
24029 return OK_PIXELS (width_p ? img->width : img->height);
24031 #endif
24032 if (EQ (car, Qplus) || EQ (car, Qminus))
24034 int first = 1;
24035 double px;
24037 pixels = 0;
24038 while (CONSP (cdr))
24040 if (!calc_pixel_width_or_height (&px, it, XCAR (cdr),
24041 font, width_p, align_to))
24042 return 0;
24043 if (first)
24044 pixels = (EQ (car, Qplus) ? px : -px), first = 0;
24045 else
24046 pixels += px;
24047 cdr = XCDR (cdr);
24049 if (EQ (car, Qminus))
24050 pixels = -pixels;
24051 return OK_PIXELS (pixels);
24054 car = buffer_local_value (car, it->w->contents);
24055 if (EQ (car, Qunbound))
24056 car = Qnil;
24059 if (INTEGERP (car) || FLOATP (car))
24061 double fact;
24062 pixels = XFLOATINT (car);
24063 if (NILP (cdr))
24064 return OK_PIXELS (pixels);
24065 if (calc_pixel_width_or_height (&fact, it, cdr,
24066 font, width_p, align_to))
24067 return OK_PIXELS (pixels * fact);
24068 return 0;
24071 return 0;
24074 return 0;
24078 /***********************************************************************
24079 Glyph Display
24080 ***********************************************************************/
24082 #ifdef HAVE_WINDOW_SYSTEM
24084 #ifdef GLYPH_DEBUG
24086 void
24087 dump_glyph_string (struct glyph_string *s)
24089 fprintf (stderr, "glyph string\n");
24090 fprintf (stderr, " x, y, w, h = %d, %d, %d, %d\n",
24091 s->x, s->y, s->width, s->height);
24092 fprintf (stderr, " ybase = %d\n", s->ybase);
24093 fprintf (stderr, " hl = %d\n", s->hl);
24094 fprintf (stderr, " left overhang = %d, right = %d\n",
24095 s->left_overhang, s->right_overhang);
24096 fprintf (stderr, " nchars = %d\n", s->nchars);
24097 fprintf (stderr, " extends to end of line = %d\n",
24098 s->extends_to_end_of_line_p);
24099 fprintf (stderr, " font height = %d\n", FONT_HEIGHT (s->font));
24100 fprintf (stderr, " bg width = %d\n", s->background_width);
24103 #endif /* GLYPH_DEBUG */
24105 /* Initialize glyph string S. CHAR2B is a suitably allocated vector
24106 of XChar2b structures for S; it can't be allocated in
24107 init_glyph_string because it must be allocated via `alloca'. W
24108 is the window on which S is drawn. ROW and AREA are the glyph row
24109 and area within the row from which S is constructed. START is the
24110 index of the first glyph structure covered by S. HL is a
24111 face-override for drawing S. */
24113 #ifdef HAVE_NTGUI
24114 #define OPTIONAL_HDC(hdc) HDC hdc,
24115 #define DECLARE_HDC(hdc) HDC hdc;
24116 #define ALLOCATE_HDC(hdc, f) hdc = get_frame_dc ((f))
24117 #define RELEASE_HDC(hdc, f) release_frame_dc ((f), (hdc))
24118 #endif
24120 #ifndef OPTIONAL_HDC
24121 #define OPTIONAL_HDC(hdc)
24122 #define DECLARE_HDC(hdc)
24123 #define ALLOCATE_HDC(hdc, f)
24124 #define RELEASE_HDC(hdc, f)
24125 #endif
24127 static void
24128 init_glyph_string (struct glyph_string *s,
24129 OPTIONAL_HDC (hdc)
24130 XChar2b *char2b, struct window *w, struct glyph_row *row,
24131 enum glyph_row_area area, int start, enum draw_glyphs_face hl)
24133 memset (s, 0, sizeof *s);
24134 s->w = w;
24135 s->f = XFRAME (w->frame);
24136 #ifdef HAVE_NTGUI
24137 s->hdc = hdc;
24138 #endif
24139 s->display = FRAME_X_DISPLAY (s->f);
24140 s->window = FRAME_X_WINDOW (s->f);
24141 s->char2b = char2b;
24142 s->hl = hl;
24143 s->row = row;
24144 s->area = area;
24145 s->first_glyph = row->glyphs[area] + start;
24146 s->height = row->height;
24147 s->y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
24148 s->ybase = s->y + row->ascent;
24152 /* Append the list of glyph strings with head H and tail T to the list
24153 with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the result. */
24155 static void
24156 append_glyph_string_lists (struct glyph_string **head, struct glyph_string **tail,
24157 struct glyph_string *h, struct glyph_string *t)
24159 if (h)
24161 if (*head)
24162 (*tail)->next = h;
24163 else
24164 *head = h;
24165 h->prev = *tail;
24166 *tail = t;
24171 /* Prepend the list of glyph strings with head H and tail T to the
24172 list with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the
24173 result. */
24175 static void
24176 prepend_glyph_string_lists (struct glyph_string **head, struct glyph_string **tail,
24177 struct glyph_string *h, struct glyph_string *t)
24179 if (h)
24181 if (*head)
24182 (*head)->prev = t;
24183 else
24184 *tail = t;
24185 t->next = *head;
24186 *head = h;
24191 /* Append glyph string S to the list with head *HEAD and tail *TAIL.
24192 Set *HEAD and *TAIL to the resulting list. */
24194 static void
24195 append_glyph_string (struct glyph_string **head, struct glyph_string **tail,
24196 struct glyph_string *s)
24198 s->next = s->prev = NULL;
24199 append_glyph_string_lists (head, tail, s, s);
24203 /* Get face and two-byte form of character C in face FACE_ID on frame F.
24204 The encoding of C is returned in *CHAR2B. DISPLAY_P non-zero means
24205 make sure that X resources for the face returned are allocated.
24206 Value is a pointer to a realized face that is ready for display if
24207 DISPLAY_P is non-zero. */
24209 static struct face *
24210 get_char_face_and_encoding (struct frame *f, int c, int face_id,
24211 XChar2b *char2b, int display_p)
24213 struct face *face = FACE_FROM_ID (f, face_id);
24214 unsigned code = 0;
24216 if (face->font)
24218 code = face->font->driver->encode_char (face->font, c);
24220 if (code == FONT_INVALID_CODE)
24221 code = 0;
24223 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
24225 /* Make sure X resources of the face are allocated. */
24226 #ifdef HAVE_X_WINDOWS
24227 if (display_p)
24228 #endif
24230 eassert (face != NULL);
24231 prepare_face_for_display (f, face);
24234 return face;
24238 /* Get face and two-byte form of character glyph GLYPH on frame F.
24239 The encoding of GLYPH->u.ch is returned in *CHAR2B. Value is
24240 a pointer to a realized face that is ready for display. */
24242 static struct face *
24243 get_glyph_face_and_encoding (struct frame *f, struct glyph *glyph,
24244 XChar2b *char2b, int *two_byte_p)
24246 struct face *face;
24247 unsigned code = 0;
24249 eassert (glyph->type == CHAR_GLYPH);
24250 face = FACE_FROM_ID (f, glyph->face_id);
24252 /* Make sure X resources of the face are allocated. */
24253 eassert (face != NULL);
24254 prepare_face_for_display (f, face);
24256 if (two_byte_p)
24257 *two_byte_p = 0;
24259 if (face->font)
24261 if (CHAR_BYTE8_P (glyph->u.ch))
24262 code = CHAR_TO_BYTE8 (glyph->u.ch);
24263 else
24264 code = face->font->driver->encode_char (face->font, glyph->u.ch);
24266 if (code == FONT_INVALID_CODE)
24267 code = 0;
24270 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
24271 return face;
24275 /* Get glyph code of character C in FONT in the two-byte form CHAR2B.
24276 Return 1 if FONT has a glyph for C, otherwise return 0. */
24278 static int
24279 get_char_glyph_code (int c, struct font *font, XChar2b *char2b)
24281 unsigned code;
24283 if (CHAR_BYTE8_P (c))
24284 code = CHAR_TO_BYTE8 (c);
24285 else
24286 code = font->driver->encode_char (font, c);
24288 if (code == FONT_INVALID_CODE)
24289 return 0;
24290 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
24291 return 1;
24295 /* Fill glyph string S with composition components specified by S->cmp.
24297 BASE_FACE is the base face of the composition.
24298 S->cmp_from is the index of the first component for S.
24300 OVERLAPS non-zero means S should draw the foreground only, and use
24301 its physical height for clipping. See also draw_glyphs.
24303 Value is the index of a component not in S. */
24305 static int
24306 fill_composite_glyph_string (struct glyph_string *s, struct face *base_face,
24307 int overlaps)
24309 int i;
24310 /* For all glyphs of this composition, starting at the offset
24311 S->cmp_from, until we reach the end of the definition or encounter a
24312 glyph that requires the different face, add it to S. */
24313 struct face *face;
24315 eassert (s);
24317 s->for_overlaps = overlaps;
24318 s->face = NULL;
24319 s->font = NULL;
24320 for (i = s->cmp_from; i < s->cmp->glyph_len; i++)
24322 int c = COMPOSITION_GLYPH (s->cmp, i);
24324 /* TAB in a composition means display glyphs with padding space
24325 on the left or right. */
24326 if (c != '\t')
24328 int face_id = FACE_FOR_CHAR (s->f, base_face->ascii_face, c,
24329 -1, Qnil);
24331 face = get_char_face_and_encoding (s->f, c, face_id,
24332 s->char2b + i, 1);
24333 if (face)
24335 if (! s->face)
24337 s->face = face;
24338 s->font = s->face->font;
24340 else if (s->face != face)
24341 break;
24344 ++s->nchars;
24346 s->cmp_to = i;
24348 if (s->face == NULL)
24350 s->face = base_face->ascii_face;
24351 s->font = s->face->font;
24354 /* All glyph strings for the same composition has the same width,
24355 i.e. the width set for the first component of the composition. */
24356 s->width = s->first_glyph->pixel_width;
24358 /* If the specified font could not be loaded, use the frame's
24359 default font, but record the fact that we couldn't load it in
24360 the glyph string so that we can draw rectangles for the
24361 characters of the glyph string. */
24362 if (s->font == NULL)
24364 s->font_not_found_p = 1;
24365 s->font = FRAME_FONT (s->f);
24368 /* Adjust base line for subscript/superscript text. */
24369 s->ybase += s->first_glyph->voffset;
24371 /* This glyph string must always be drawn with 16-bit functions. */
24372 s->two_byte_p = 1;
24374 return s->cmp_to;
24377 static int
24378 fill_gstring_glyph_string (struct glyph_string *s, int face_id,
24379 int start, int end, int overlaps)
24381 struct glyph *glyph, *last;
24382 Lisp_Object lgstring;
24383 int i;
24385 s->for_overlaps = overlaps;
24386 glyph = s->row->glyphs[s->area] + start;
24387 last = s->row->glyphs[s->area] + end;
24388 s->cmp_id = glyph->u.cmp.id;
24389 s->cmp_from = glyph->slice.cmp.from;
24390 s->cmp_to = glyph->slice.cmp.to + 1;
24391 s->face = FACE_FROM_ID (s->f, face_id);
24392 lgstring = composition_gstring_from_id (s->cmp_id);
24393 s->font = XFONT_OBJECT (LGSTRING_FONT (lgstring));
24394 glyph++;
24395 while (glyph < last
24396 && glyph->u.cmp.automatic
24397 && glyph->u.cmp.id == s->cmp_id
24398 && s->cmp_to == glyph->slice.cmp.from)
24399 s->cmp_to = (glyph++)->slice.cmp.to + 1;
24401 for (i = s->cmp_from; i < s->cmp_to; i++)
24403 Lisp_Object lglyph = LGSTRING_GLYPH (lgstring, i);
24404 unsigned code = LGLYPH_CODE (lglyph);
24406 STORE_XCHAR2B ((s->char2b + i), code >> 8, code & 0xFF);
24408 s->width = composition_gstring_width (lgstring, s->cmp_from, s->cmp_to, NULL);
24409 return glyph - s->row->glyphs[s->area];
24413 /* Fill glyph string S from a sequence glyphs for glyphless characters.
24414 See the comment of fill_glyph_string for arguments.
24415 Value is the index of the first glyph not in S. */
24418 static int
24419 fill_glyphless_glyph_string (struct glyph_string *s, int face_id,
24420 int start, int end, int overlaps)
24422 struct glyph *glyph, *last;
24423 int voffset;
24425 eassert (s->first_glyph->type == GLYPHLESS_GLYPH);
24426 s->for_overlaps = overlaps;
24427 glyph = s->row->glyphs[s->area] + start;
24428 last = s->row->glyphs[s->area] + end;
24429 voffset = glyph->voffset;
24430 s->face = FACE_FROM_ID (s->f, face_id);
24431 s->font = s->face->font ? s->face->font : FRAME_FONT (s->f);
24432 s->nchars = 1;
24433 s->width = glyph->pixel_width;
24434 glyph++;
24435 while (glyph < last
24436 && glyph->type == GLYPHLESS_GLYPH
24437 && glyph->voffset == voffset
24438 && glyph->face_id == face_id)
24440 s->nchars++;
24441 s->width += glyph->pixel_width;
24442 glyph++;
24444 s->ybase += voffset;
24445 return glyph - s->row->glyphs[s->area];
24449 /* Fill glyph string S from a sequence of character glyphs.
24451 FACE_ID is the face id of the string. START is the index of the
24452 first glyph to consider, END is the index of the last + 1.
24453 OVERLAPS non-zero means S should draw the foreground only, and use
24454 its physical height for clipping. See also draw_glyphs.
24456 Value is the index of the first glyph not in S. */
24458 static int
24459 fill_glyph_string (struct glyph_string *s, int face_id,
24460 int start, int end, int overlaps)
24462 struct glyph *glyph, *last;
24463 int voffset;
24464 int glyph_not_available_p;
24466 eassert (s->f == XFRAME (s->w->frame));
24467 eassert (s->nchars == 0);
24468 eassert (start >= 0 && end > start);
24470 s->for_overlaps = overlaps;
24471 glyph = s->row->glyphs[s->area] + start;
24472 last = s->row->glyphs[s->area] + end;
24473 voffset = glyph->voffset;
24474 s->padding_p = glyph->padding_p;
24475 glyph_not_available_p = glyph->glyph_not_available_p;
24477 while (glyph < last
24478 && glyph->type == CHAR_GLYPH
24479 && glyph->voffset == voffset
24480 /* Same face id implies same font, nowadays. */
24481 && glyph->face_id == face_id
24482 && glyph->glyph_not_available_p == glyph_not_available_p)
24484 int two_byte_p;
24486 s->face = get_glyph_face_and_encoding (s->f, glyph,
24487 s->char2b + s->nchars,
24488 &two_byte_p);
24489 s->two_byte_p = two_byte_p;
24490 ++s->nchars;
24491 eassert (s->nchars <= end - start);
24492 s->width += glyph->pixel_width;
24493 if (glyph++->padding_p != s->padding_p)
24494 break;
24497 s->font = s->face->font;
24499 /* If the specified font could not be loaded, use the frame's font,
24500 but record the fact that we couldn't load it in
24501 S->font_not_found_p so that we can draw rectangles for the
24502 characters of the glyph string. */
24503 if (s->font == NULL || glyph_not_available_p)
24505 s->font_not_found_p = 1;
24506 s->font = FRAME_FONT (s->f);
24509 /* Adjust base line for subscript/superscript text. */
24510 s->ybase += voffset;
24512 eassert (s->face && s->face->gc);
24513 return glyph - s->row->glyphs[s->area];
24517 /* Fill glyph string S from image glyph S->first_glyph. */
24519 static void
24520 fill_image_glyph_string (struct glyph_string *s)
24522 eassert (s->first_glyph->type == IMAGE_GLYPH);
24523 s->img = IMAGE_FROM_ID (s->f, s->first_glyph->u.img_id);
24524 eassert (s->img);
24525 s->slice = s->first_glyph->slice.img;
24526 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
24527 s->font = s->face->font;
24528 s->width = s->first_glyph->pixel_width;
24530 /* Adjust base line for subscript/superscript text. */
24531 s->ybase += s->first_glyph->voffset;
24535 /* Fill glyph string S from a sequence of stretch glyphs.
24537 START is the index of the first glyph to consider,
24538 END is the index of the last + 1.
24540 Value is the index of the first glyph not in S. */
24542 static int
24543 fill_stretch_glyph_string (struct glyph_string *s, int start, int end)
24545 struct glyph *glyph, *last;
24546 int voffset, face_id;
24548 eassert (s->first_glyph->type == STRETCH_GLYPH);
24550 glyph = s->row->glyphs[s->area] + start;
24551 last = s->row->glyphs[s->area] + end;
24552 face_id = glyph->face_id;
24553 s->face = FACE_FROM_ID (s->f, face_id);
24554 s->font = s->face->font;
24555 s->width = glyph->pixel_width;
24556 s->nchars = 1;
24557 voffset = glyph->voffset;
24559 for (++glyph;
24560 (glyph < last
24561 && glyph->type == STRETCH_GLYPH
24562 && glyph->voffset == voffset
24563 && glyph->face_id == face_id);
24564 ++glyph)
24565 s->width += glyph->pixel_width;
24567 /* Adjust base line for subscript/superscript text. */
24568 s->ybase += voffset;
24570 /* The case that face->gc == 0 is handled when drawing the glyph
24571 string by calling prepare_face_for_display. */
24572 eassert (s->face);
24573 return glyph - s->row->glyphs[s->area];
24576 static struct font_metrics *
24577 get_per_char_metric (struct font *font, XChar2b *char2b)
24579 static struct font_metrics metrics;
24580 unsigned code;
24582 if (! font)
24583 return NULL;
24584 code = (XCHAR2B_BYTE1 (char2b) << 8) | XCHAR2B_BYTE2 (char2b);
24585 if (code == FONT_INVALID_CODE)
24586 return NULL;
24587 font->driver->text_extents (font, &code, 1, &metrics);
24588 return &metrics;
24591 /* EXPORT for RIF:
24592 Set *LEFT and *RIGHT to the left and right overhang of GLYPH on
24593 frame F. Overhangs of glyphs other than type CHAR_GLYPH are
24594 assumed to be zero. */
24596 void
24597 x_get_glyph_overhangs (struct glyph *glyph, struct frame *f, int *left, int *right)
24599 *left = *right = 0;
24601 if (glyph->type == CHAR_GLYPH)
24603 struct face *face;
24604 XChar2b char2b;
24605 struct font_metrics *pcm;
24607 face = get_glyph_face_and_encoding (f, glyph, &char2b, NULL);
24608 if (face->font && (pcm = get_per_char_metric (face->font, &char2b)))
24610 if (pcm->rbearing > pcm->width)
24611 *right = pcm->rbearing - pcm->width;
24612 if (pcm->lbearing < 0)
24613 *left = -pcm->lbearing;
24616 else if (glyph->type == COMPOSITE_GLYPH)
24618 if (! glyph->u.cmp.automatic)
24620 struct composition *cmp = composition_table[glyph->u.cmp.id];
24622 if (cmp->rbearing > cmp->pixel_width)
24623 *right = cmp->rbearing - cmp->pixel_width;
24624 if (cmp->lbearing < 0)
24625 *left = - cmp->lbearing;
24627 else
24629 Lisp_Object gstring = composition_gstring_from_id (glyph->u.cmp.id);
24630 struct font_metrics metrics;
24632 composition_gstring_width (gstring, glyph->slice.cmp.from,
24633 glyph->slice.cmp.to + 1, &metrics);
24634 if (metrics.rbearing > metrics.width)
24635 *right = metrics.rbearing - metrics.width;
24636 if (metrics.lbearing < 0)
24637 *left = - metrics.lbearing;
24643 /* Return the index of the first glyph preceding glyph string S that
24644 is overwritten by S because of S's left overhang. Value is -1
24645 if no glyphs are overwritten. */
24647 static int
24648 left_overwritten (struct glyph_string *s)
24650 int k;
24652 if (s->left_overhang)
24654 int x = 0, i;
24655 struct glyph *glyphs = s->row->glyphs[s->area];
24656 int first = s->first_glyph - glyphs;
24658 for (i = first - 1; i >= 0 && x > -s->left_overhang; --i)
24659 x -= glyphs[i].pixel_width;
24661 k = i + 1;
24663 else
24664 k = -1;
24666 return k;
24670 /* Return the index of the first glyph preceding glyph string S that
24671 is overwriting S because of its right overhang. Value is -1 if no
24672 glyph in front of S overwrites S. */
24674 static int
24675 left_overwriting (struct glyph_string *s)
24677 int i, k, x;
24678 struct glyph *glyphs = s->row->glyphs[s->area];
24679 int first = s->first_glyph - glyphs;
24681 k = -1;
24682 x = 0;
24683 for (i = first - 1; i >= 0; --i)
24685 int left, right;
24686 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
24687 if (x + right > 0)
24688 k = i;
24689 x -= glyphs[i].pixel_width;
24692 return k;
24696 /* Return the index of the last glyph following glyph string S that is
24697 overwritten by S because of S's right overhang. Value is -1 if
24698 no such glyph is found. */
24700 static int
24701 right_overwritten (struct glyph_string *s)
24703 int k = -1;
24705 if (s->right_overhang)
24707 int x = 0, i;
24708 struct glyph *glyphs = s->row->glyphs[s->area];
24709 int first = (s->first_glyph - glyphs
24710 + (s->first_glyph->type == COMPOSITE_GLYPH ? 1 : s->nchars));
24711 int end = s->row->used[s->area];
24713 for (i = first; i < end && s->right_overhang > x; ++i)
24714 x += glyphs[i].pixel_width;
24716 k = i;
24719 return k;
24723 /* Return the index of the last glyph following glyph string S that
24724 overwrites S because of its left overhang. Value is negative
24725 if no such glyph is found. */
24727 static int
24728 right_overwriting (struct glyph_string *s)
24730 int i, k, x;
24731 int end = s->row->used[s->area];
24732 struct glyph *glyphs = s->row->glyphs[s->area];
24733 int first = (s->first_glyph - glyphs
24734 + (s->first_glyph->type == COMPOSITE_GLYPH ? 1 : s->nchars));
24736 k = -1;
24737 x = 0;
24738 for (i = first; i < end; ++i)
24740 int left, right;
24741 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
24742 if (x - left < 0)
24743 k = i;
24744 x += glyphs[i].pixel_width;
24747 return k;
24751 /* Set background width of glyph string S. START is the index of the
24752 first glyph following S. LAST_X is the right-most x-position + 1
24753 in the drawing area. */
24755 static void
24756 set_glyph_string_background_width (struct glyph_string *s, int start, int last_x)
24758 /* If the face of this glyph string has to be drawn to the end of
24759 the drawing area, set S->extends_to_end_of_line_p. */
24761 if (start == s->row->used[s->area]
24762 && ((s->row->fill_line_p
24763 && (s->hl == DRAW_NORMAL_TEXT
24764 || s->hl == DRAW_IMAGE_RAISED
24765 || s->hl == DRAW_IMAGE_SUNKEN))
24766 || s->hl == DRAW_MOUSE_FACE))
24767 s->extends_to_end_of_line_p = 1;
24769 /* If S extends its face to the end of the line, set its
24770 background_width to the distance to the right edge of the drawing
24771 area. */
24772 if (s->extends_to_end_of_line_p)
24773 s->background_width = last_x - s->x + 1;
24774 else
24775 s->background_width = s->width;
24779 /* Compute overhangs and x-positions for glyph string S and its
24780 predecessors, or successors. X is the starting x-position for S.
24781 BACKWARD_P non-zero means process predecessors. */
24783 static void
24784 compute_overhangs_and_x (struct glyph_string *s, int x, int backward_p)
24786 if (backward_p)
24788 while (s)
24790 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
24791 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
24792 x -= s->width;
24793 s->x = x;
24794 s = s->prev;
24797 else
24799 while (s)
24801 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
24802 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
24803 s->x = x;
24804 x += s->width;
24805 s = s->next;
24812 /* The following macros are only called from draw_glyphs below.
24813 They reference the following parameters of that function directly:
24814 `w', `row', `area', and `overlap_p'
24815 as well as the following local variables:
24816 `s', `f', and `hdc' (in W32) */
24818 #ifdef HAVE_NTGUI
24819 /* On W32, silently add local `hdc' variable to argument list of
24820 init_glyph_string. */
24821 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
24822 init_glyph_string (s, hdc, char2b, w, row, area, start, hl)
24823 #else
24824 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
24825 init_glyph_string (s, char2b, w, row, area, start, hl)
24826 #endif
24828 /* Add a glyph string for a stretch glyph to the list of strings
24829 between HEAD and TAIL. START is the index of the stretch glyph in
24830 row area AREA of glyph row ROW. END is the index of the last glyph
24831 in that glyph row area. X is the current output position assigned
24832 to the new glyph string constructed. HL overrides that face of the
24833 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
24834 is the right-most x-position of the drawing area. */
24836 /* SunOS 4 bundled cc, barfed on continuations in the arg lists here
24837 and below -- keep them on one line. */
24838 #define BUILD_STRETCH_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
24839 do \
24841 s = alloca (sizeof *s); \
24842 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
24843 START = fill_stretch_glyph_string (s, START, END); \
24844 append_glyph_string (&HEAD, &TAIL, s); \
24845 s->x = (X); \
24847 while (0)
24850 /* Add a glyph string for an image glyph to the list of strings
24851 between HEAD and TAIL. START is the index of the image glyph in
24852 row area AREA of glyph row ROW. END is the index of the last glyph
24853 in that glyph row area. X is the current output position assigned
24854 to the new glyph string constructed. HL overrides that face of the
24855 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
24856 is the right-most x-position of the drawing area. */
24858 #define BUILD_IMAGE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
24859 do \
24861 s = alloca (sizeof *s); \
24862 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
24863 fill_image_glyph_string (s); \
24864 append_glyph_string (&HEAD, &TAIL, s); \
24865 ++START; \
24866 s->x = (X); \
24868 while (0)
24871 /* Add a glyph string for a sequence of character glyphs to the list
24872 of strings between HEAD and TAIL. START is the index of the first
24873 glyph in row area AREA of glyph row ROW that is part of the new
24874 glyph string. END is the index of the last glyph in that glyph row
24875 area. X is the current output position assigned to the new glyph
24876 string constructed. HL overrides that face of the glyph; e.g. it
24877 is DRAW_CURSOR if a cursor has to be drawn. LAST_X is the
24878 right-most x-position of the drawing area. */
24880 #define BUILD_CHAR_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
24881 do \
24883 int face_id; \
24884 XChar2b *char2b; \
24886 face_id = (row)->glyphs[area][START].face_id; \
24888 s = alloca (sizeof *s); \
24889 SAFE_NALLOCA (char2b, 1, (END) - (START)); \
24890 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
24891 append_glyph_string (&HEAD, &TAIL, s); \
24892 s->x = (X); \
24893 START = fill_glyph_string (s, face_id, START, END, overlaps); \
24895 while (0)
24898 /* Add a glyph string for a composite sequence to the list of strings
24899 between HEAD and TAIL. START is the index of the first glyph in
24900 row area AREA of glyph row ROW that is part of the new glyph
24901 string. END is the index of the last glyph in that glyph row area.
24902 X is the current output position assigned to the new glyph string
24903 constructed. HL overrides that face of the glyph; e.g. it is
24904 DRAW_CURSOR if a cursor has to be drawn. LAST_X is the right-most
24905 x-position of the drawing area. */
24907 #define BUILD_COMPOSITE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
24908 do { \
24909 int face_id = (row)->glyphs[area][START].face_id; \
24910 struct face *base_face = FACE_FROM_ID (f, face_id); \
24911 ptrdiff_t cmp_id = (row)->glyphs[area][START].u.cmp.id; \
24912 struct composition *cmp = composition_table[cmp_id]; \
24913 XChar2b *char2b; \
24914 struct glyph_string *first_s = NULL; \
24915 int n; \
24917 SAFE_NALLOCA (char2b, 1, cmp->glyph_len); \
24919 /* Make glyph_strings for each glyph sequence that is drawable by \
24920 the same face, and append them to HEAD/TAIL. */ \
24921 for (n = 0; n < cmp->glyph_len;) \
24923 s = alloca (sizeof *s); \
24924 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
24925 append_glyph_string (&(HEAD), &(TAIL), s); \
24926 s->cmp = cmp; \
24927 s->cmp_from = n; \
24928 s->x = (X); \
24929 if (n == 0) \
24930 first_s = s; \
24931 n = fill_composite_glyph_string (s, base_face, overlaps); \
24934 ++START; \
24935 s = first_s; \
24936 } while (0)
24939 /* Add a glyph string for a glyph-string sequence to the list of strings
24940 between HEAD and TAIL. */
24942 #define BUILD_GSTRING_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
24943 do { \
24944 int face_id; \
24945 XChar2b *char2b; \
24946 Lisp_Object gstring; \
24948 face_id = (row)->glyphs[area][START].face_id; \
24949 gstring = (composition_gstring_from_id \
24950 ((row)->glyphs[area][START].u.cmp.id)); \
24951 s = alloca (sizeof *s); \
24952 SAFE_NALLOCA (char2b, 1, LGSTRING_GLYPH_LEN (gstring)); \
24953 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
24954 append_glyph_string (&(HEAD), &(TAIL), s); \
24955 s->x = (X); \
24956 START = fill_gstring_glyph_string (s, face_id, START, END, overlaps); \
24957 } while (0)
24960 /* Add a glyph string for a sequence of glyphless character's glyphs
24961 to the list of strings between HEAD and TAIL. The meanings of
24962 arguments are the same as those of BUILD_CHAR_GLYPH_STRINGS. */
24964 #define BUILD_GLYPHLESS_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
24965 do \
24967 int face_id; \
24969 face_id = (row)->glyphs[area][START].face_id; \
24971 s = alloca (sizeof *s); \
24972 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
24973 append_glyph_string (&HEAD, &TAIL, s); \
24974 s->x = (X); \
24975 START = fill_glyphless_glyph_string (s, face_id, START, END, \
24976 overlaps); \
24978 while (0)
24981 /* Build a list of glyph strings between HEAD and TAIL for the glyphs
24982 of AREA of glyph row ROW on window W between indices START and END.
24983 HL overrides the face for drawing glyph strings, e.g. it is
24984 DRAW_CURSOR to draw a cursor. X and LAST_X are start and end
24985 x-positions of the drawing area.
24987 This is an ugly monster macro construct because we must use alloca
24988 to allocate glyph strings (because draw_glyphs can be called
24989 asynchronously). */
24991 #define BUILD_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
24992 do \
24994 HEAD = TAIL = NULL; \
24995 while (START < END) \
24997 struct glyph *first_glyph = (row)->glyphs[area] + START; \
24998 switch (first_glyph->type) \
25000 case CHAR_GLYPH: \
25001 BUILD_CHAR_GLYPH_STRINGS (START, END, HEAD, TAIL, \
25002 HL, X, LAST_X); \
25003 break; \
25005 case COMPOSITE_GLYPH: \
25006 if (first_glyph->u.cmp.automatic) \
25007 BUILD_GSTRING_GLYPH_STRING (START, END, HEAD, TAIL, \
25008 HL, X, LAST_X); \
25009 else \
25010 BUILD_COMPOSITE_GLYPH_STRING (START, END, HEAD, TAIL, \
25011 HL, X, LAST_X); \
25012 break; \
25014 case STRETCH_GLYPH: \
25015 BUILD_STRETCH_GLYPH_STRING (START, END, HEAD, TAIL, \
25016 HL, X, LAST_X); \
25017 break; \
25019 case IMAGE_GLYPH: \
25020 BUILD_IMAGE_GLYPH_STRING (START, END, HEAD, TAIL, \
25021 HL, X, LAST_X); \
25022 break; \
25024 case GLYPHLESS_GLYPH: \
25025 BUILD_GLYPHLESS_GLYPH_STRING (START, END, HEAD, TAIL, \
25026 HL, X, LAST_X); \
25027 break; \
25029 default: \
25030 emacs_abort (); \
25033 if (s) \
25035 set_glyph_string_background_width (s, START, LAST_X); \
25036 (X) += s->width; \
25039 } while (0)
25042 /* Draw glyphs between START and END in AREA of ROW on window W,
25043 starting at x-position X. X is relative to AREA in W. HL is a
25044 face-override with the following meaning:
25046 DRAW_NORMAL_TEXT draw normally
25047 DRAW_CURSOR draw in cursor face
25048 DRAW_MOUSE_FACE draw in mouse face.
25049 DRAW_INVERSE_VIDEO draw in mode line face
25050 DRAW_IMAGE_SUNKEN draw an image with a sunken relief around it
25051 DRAW_IMAGE_RAISED draw an image with a raised relief around it
25053 If OVERLAPS is non-zero, draw only the foreground of characters and
25054 clip to the physical height of ROW. Non-zero value also defines
25055 the overlapping part to be drawn:
25057 OVERLAPS_PRED overlap with preceding rows
25058 OVERLAPS_SUCC overlap with succeeding rows
25059 OVERLAPS_BOTH overlap with both preceding/succeeding rows
25060 OVERLAPS_ERASED_CURSOR overlap with erased cursor area
25062 Value is the x-position reached, relative to AREA of W. */
25064 static int
25065 draw_glyphs (struct window *w, int x, struct glyph_row *row,
25066 enum glyph_row_area area, ptrdiff_t start, ptrdiff_t end,
25067 enum draw_glyphs_face hl, int overlaps)
25069 struct glyph_string *head, *tail;
25070 struct glyph_string *s;
25071 struct glyph_string *clip_head = NULL, *clip_tail = NULL;
25072 int i, j, x_reached, last_x, area_left = 0;
25073 struct frame *f = XFRAME (WINDOW_FRAME (w));
25074 DECLARE_HDC (hdc);
25076 ALLOCATE_HDC (hdc, f);
25078 /* Let's rather be paranoid than getting a SEGV. */
25079 end = min (end, row->used[area]);
25080 start = clip_to_bounds (0, start, end);
25082 /* Translate X to frame coordinates. Set last_x to the right
25083 end of the drawing area. */
25084 if (row->full_width_p)
25086 /* X is relative to the left edge of W, without scroll bars
25087 or fringes. */
25088 area_left = WINDOW_LEFT_EDGE_X (w);
25089 last_x = (WINDOW_LEFT_EDGE_X (w) + WINDOW_PIXEL_WIDTH (w)
25090 - (row->mode_line_p ? WINDOW_RIGHT_DIVIDER_WIDTH (w) : 0));
25092 else
25094 area_left = window_box_left (w, area);
25095 last_x = area_left + window_box_width (w, area);
25097 x += area_left;
25099 /* Build a doubly-linked list of glyph_string structures between
25100 head and tail from what we have to draw. Note that the macro
25101 BUILD_GLYPH_STRINGS will modify its start parameter. That's
25102 the reason we use a separate variable `i'. */
25103 i = start;
25104 USE_SAFE_ALLOCA;
25105 BUILD_GLYPH_STRINGS (i, end, head, tail, hl, x, last_x);
25106 if (tail)
25107 x_reached = tail->x + tail->background_width;
25108 else
25109 x_reached = x;
25111 /* If there are any glyphs with lbearing < 0 or rbearing > width in
25112 the row, redraw some glyphs in front or following the glyph
25113 strings built above. */
25114 if (head && !overlaps && row->contains_overlapping_glyphs_p)
25116 struct glyph_string *h, *t;
25117 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
25118 int mouse_beg_col IF_LINT (= 0), mouse_end_col IF_LINT (= 0);
25119 int check_mouse_face = 0;
25120 int dummy_x = 0;
25122 /* If mouse highlighting is on, we may need to draw adjacent
25123 glyphs using mouse-face highlighting. */
25124 if (area == TEXT_AREA && row->mouse_face_p
25125 && hlinfo->mouse_face_beg_row >= 0
25126 && hlinfo->mouse_face_end_row >= 0)
25128 ptrdiff_t row_vpos = MATRIX_ROW_VPOS (row, w->current_matrix);
25130 if (row_vpos >= hlinfo->mouse_face_beg_row
25131 && row_vpos <= hlinfo->mouse_face_end_row)
25133 check_mouse_face = 1;
25134 mouse_beg_col = (row_vpos == hlinfo->mouse_face_beg_row)
25135 ? hlinfo->mouse_face_beg_col : 0;
25136 mouse_end_col = (row_vpos == hlinfo->mouse_face_end_row)
25137 ? hlinfo->mouse_face_end_col
25138 : row->used[TEXT_AREA];
25142 /* Compute overhangs for all glyph strings. */
25143 if (FRAME_RIF (f)->compute_glyph_string_overhangs)
25144 for (s = head; s; s = s->next)
25145 FRAME_RIF (f)->compute_glyph_string_overhangs (s);
25147 /* Prepend glyph strings for glyphs in front of the first glyph
25148 string that are overwritten because of the first glyph
25149 string's left overhang. The background of all strings
25150 prepended must be drawn because the first glyph string
25151 draws over it. */
25152 i = left_overwritten (head);
25153 if (i >= 0)
25155 enum draw_glyphs_face overlap_hl;
25157 /* If this row contains mouse highlighting, attempt to draw
25158 the overlapped glyphs with the correct highlight. This
25159 code fails if the overlap encompasses more than one glyph
25160 and mouse-highlight spans only some of these glyphs.
25161 However, making it work perfectly involves a lot more
25162 code, and I don't know if the pathological case occurs in
25163 practice, so we'll stick to this for now. --- cyd */
25164 if (check_mouse_face
25165 && mouse_beg_col < start && mouse_end_col > i)
25166 overlap_hl = DRAW_MOUSE_FACE;
25167 else
25168 overlap_hl = DRAW_NORMAL_TEXT;
25170 if (hl != overlap_hl)
25171 clip_head = head;
25172 j = i;
25173 BUILD_GLYPH_STRINGS (j, start, h, t,
25174 overlap_hl, dummy_x, last_x);
25175 start = i;
25176 compute_overhangs_and_x (t, head->x, 1);
25177 prepend_glyph_string_lists (&head, &tail, h, t);
25178 if (clip_head == NULL)
25179 clip_head = head;
25182 /* Prepend glyph strings for glyphs in front of the first glyph
25183 string that overwrite that glyph string because of their
25184 right overhang. For these strings, only the foreground must
25185 be drawn, because it draws over the glyph string at `head'.
25186 The background must not be drawn because this would overwrite
25187 right overhangs of preceding glyphs for which no glyph
25188 strings exist. */
25189 i = left_overwriting (head);
25190 if (i >= 0)
25192 enum draw_glyphs_face overlap_hl;
25194 if (check_mouse_face
25195 && mouse_beg_col < start && mouse_end_col > i)
25196 overlap_hl = DRAW_MOUSE_FACE;
25197 else
25198 overlap_hl = DRAW_NORMAL_TEXT;
25200 if (hl == overlap_hl || clip_head == NULL)
25201 clip_head = head;
25202 BUILD_GLYPH_STRINGS (i, start, h, t,
25203 overlap_hl, dummy_x, last_x);
25204 for (s = h; s; s = s->next)
25205 s->background_filled_p = 1;
25206 compute_overhangs_and_x (t, head->x, 1);
25207 prepend_glyph_string_lists (&head, &tail, h, t);
25210 /* Append glyphs strings for glyphs following the last glyph
25211 string tail that are overwritten by tail. The background of
25212 these strings has to be drawn because tail's foreground draws
25213 over it. */
25214 i = right_overwritten (tail);
25215 if (i >= 0)
25217 enum draw_glyphs_face overlap_hl;
25219 if (check_mouse_face
25220 && mouse_beg_col < i && mouse_end_col > end)
25221 overlap_hl = DRAW_MOUSE_FACE;
25222 else
25223 overlap_hl = DRAW_NORMAL_TEXT;
25225 if (hl != overlap_hl)
25226 clip_tail = tail;
25227 BUILD_GLYPH_STRINGS (end, i, h, t,
25228 overlap_hl, x, last_x);
25229 /* Because BUILD_GLYPH_STRINGS updates the first argument,
25230 we don't have `end = i;' here. */
25231 compute_overhangs_and_x (h, tail->x + tail->width, 0);
25232 append_glyph_string_lists (&head, &tail, h, t);
25233 if (clip_tail == NULL)
25234 clip_tail = tail;
25237 /* Append glyph strings for glyphs following the last glyph
25238 string tail that overwrite tail. The foreground of such
25239 glyphs has to be drawn because it writes into the background
25240 of tail. The background must not be drawn because it could
25241 paint over the foreground of following glyphs. */
25242 i = right_overwriting (tail);
25243 if (i >= 0)
25245 enum draw_glyphs_face overlap_hl;
25246 if (check_mouse_face
25247 && mouse_beg_col < i && mouse_end_col > end)
25248 overlap_hl = DRAW_MOUSE_FACE;
25249 else
25250 overlap_hl = DRAW_NORMAL_TEXT;
25252 if (hl == overlap_hl || clip_tail == NULL)
25253 clip_tail = tail;
25254 i++; /* We must include the Ith glyph. */
25255 BUILD_GLYPH_STRINGS (end, i, h, t,
25256 overlap_hl, x, last_x);
25257 for (s = h; s; s = s->next)
25258 s->background_filled_p = 1;
25259 compute_overhangs_and_x (h, tail->x + tail->width, 0);
25260 append_glyph_string_lists (&head, &tail, h, t);
25262 if (clip_head || clip_tail)
25263 for (s = head; s; s = s->next)
25265 s->clip_head = clip_head;
25266 s->clip_tail = clip_tail;
25270 /* Draw all strings. */
25271 for (s = head; s; s = s->next)
25272 FRAME_RIF (f)->draw_glyph_string (s);
25274 #ifndef HAVE_NS
25275 /* When focus a sole frame and move horizontally, this sets on_p to 0
25276 causing a failure to erase prev cursor position. */
25277 if (area == TEXT_AREA
25278 && !row->full_width_p
25279 /* When drawing overlapping rows, only the glyph strings'
25280 foreground is drawn, which doesn't erase a cursor
25281 completely. */
25282 && !overlaps)
25284 int x0 = clip_head ? clip_head->x : (head ? head->x : x);
25285 int x1 = (clip_tail ? clip_tail->x + clip_tail->background_width
25286 : (tail ? tail->x + tail->background_width : x));
25287 x0 -= area_left;
25288 x1 -= area_left;
25290 notice_overwritten_cursor (w, TEXT_AREA, x0, x1,
25291 row->y, MATRIX_ROW_BOTTOM_Y (row));
25293 #endif
25295 /* Value is the x-position up to which drawn, relative to AREA of W.
25296 This doesn't include parts drawn because of overhangs. */
25297 if (row->full_width_p)
25298 x_reached = FRAME_TO_WINDOW_PIXEL_X (w, x_reached);
25299 else
25300 x_reached -= area_left;
25302 RELEASE_HDC (hdc, f);
25304 SAFE_FREE ();
25305 return x_reached;
25308 /* Expand row matrix if too narrow. Don't expand if area
25309 is not present. */
25311 #define IT_EXPAND_MATRIX_WIDTH(it, area) \
25313 if (!it->f->fonts_changed \
25314 && (it->glyph_row->glyphs[area] \
25315 < it->glyph_row->glyphs[area + 1])) \
25317 it->w->ncols_scale_factor++; \
25318 it->f->fonts_changed = 1; \
25322 /* Store one glyph for IT->char_to_display in IT->glyph_row.
25323 Called from x_produce_glyphs when IT->glyph_row is non-null. */
25325 static void
25326 append_glyph (struct it *it)
25328 struct glyph *glyph;
25329 enum glyph_row_area area = it->area;
25331 eassert (it->glyph_row);
25332 eassert (it->char_to_display != '\n' && it->char_to_display != '\t');
25334 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
25335 if (glyph < it->glyph_row->glyphs[area + 1])
25337 /* If the glyph row is reversed, we need to prepend the glyph
25338 rather than append it. */
25339 if (it->glyph_row->reversed_p && area == TEXT_AREA)
25341 struct glyph *g;
25343 /* Make room for the additional glyph. */
25344 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
25345 g[1] = *g;
25346 glyph = it->glyph_row->glyphs[area];
25348 glyph->charpos = CHARPOS (it->position);
25349 glyph->object = it->object;
25350 if (it->pixel_width > 0)
25352 glyph->pixel_width = it->pixel_width;
25353 glyph->padding_p = 0;
25355 else
25357 /* Assure at least 1-pixel width. Otherwise, cursor can't
25358 be displayed correctly. */
25359 glyph->pixel_width = 1;
25360 glyph->padding_p = 1;
25362 glyph->ascent = it->ascent;
25363 glyph->descent = it->descent;
25364 glyph->voffset = it->voffset;
25365 glyph->type = CHAR_GLYPH;
25366 glyph->avoid_cursor_p = it->avoid_cursor_p;
25367 glyph->multibyte_p = it->multibyte_p;
25368 if (it->glyph_row->reversed_p && area == TEXT_AREA)
25370 /* In R2L rows, the left and the right box edges need to be
25371 drawn in reverse direction. */
25372 glyph->right_box_line_p = it->start_of_box_run_p;
25373 glyph->left_box_line_p = it->end_of_box_run_p;
25375 else
25377 glyph->left_box_line_p = it->start_of_box_run_p;
25378 glyph->right_box_line_p = it->end_of_box_run_p;
25380 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
25381 || it->phys_descent > it->descent);
25382 glyph->glyph_not_available_p = it->glyph_not_available_p;
25383 glyph->face_id = it->face_id;
25384 glyph->u.ch = it->char_to_display;
25385 glyph->slice.img = null_glyph_slice;
25386 glyph->font_type = FONT_TYPE_UNKNOWN;
25387 if (it->bidi_p)
25389 glyph->resolved_level = it->bidi_it.resolved_level;
25390 eassert ((it->bidi_it.type & 7) == it->bidi_it.type);
25391 glyph->bidi_type = it->bidi_it.type;
25393 else
25395 glyph->resolved_level = 0;
25396 glyph->bidi_type = UNKNOWN_BT;
25398 ++it->glyph_row->used[area];
25400 else
25401 IT_EXPAND_MATRIX_WIDTH (it, area);
25404 /* Store one glyph for the composition IT->cmp_it.id in
25405 IT->glyph_row. Called from x_produce_glyphs when IT->glyph_row is
25406 non-null. */
25408 static void
25409 append_composite_glyph (struct it *it)
25411 struct glyph *glyph;
25412 enum glyph_row_area area = it->area;
25414 eassert (it->glyph_row);
25416 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
25417 if (glyph < it->glyph_row->glyphs[area + 1])
25419 /* If the glyph row is reversed, we need to prepend the glyph
25420 rather than append it. */
25421 if (it->glyph_row->reversed_p && it->area == TEXT_AREA)
25423 struct glyph *g;
25425 /* Make room for the new glyph. */
25426 for (g = glyph - 1; g >= it->glyph_row->glyphs[it->area]; g--)
25427 g[1] = *g;
25428 glyph = it->glyph_row->glyphs[it->area];
25430 glyph->charpos = it->cmp_it.charpos;
25431 glyph->object = it->object;
25432 glyph->pixel_width = it->pixel_width;
25433 glyph->ascent = it->ascent;
25434 glyph->descent = it->descent;
25435 glyph->voffset = it->voffset;
25436 glyph->type = COMPOSITE_GLYPH;
25437 if (it->cmp_it.ch < 0)
25439 glyph->u.cmp.automatic = 0;
25440 glyph->u.cmp.id = it->cmp_it.id;
25441 glyph->slice.cmp.from = glyph->slice.cmp.to = 0;
25443 else
25445 glyph->u.cmp.automatic = 1;
25446 glyph->u.cmp.id = it->cmp_it.id;
25447 glyph->slice.cmp.from = it->cmp_it.from;
25448 glyph->slice.cmp.to = it->cmp_it.to - 1;
25450 glyph->avoid_cursor_p = it->avoid_cursor_p;
25451 glyph->multibyte_p = it->multibyte_p;
25452 if (it->glyph_row->reversed_p && area == TEXT_AREA)
25454 /* In R2L rows, the left and the right box edges need to be
25455 drawn in reverse direction. */
25456 glyph->right_box_line_p = it->start_of_box_run_p;
25457 glyph->left_box_line_p = it->end_of_box_run_p;
25459 else
25461 glyph->left_box_line_p = it->start_of_box_run_p;
25462 glyph->right_box_line_p = it->end_of_box_run_p;
25464 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
25465 || it->phys_descent > it->descent);
25466 glyph->padding_p = 0;
25467 glyph->glyph_not_available_p = 0;
25468 glyph->face_id = it->face_id;
25469 glyph->font_type = FONT_TYPE_UNKNOWN;
25470 if (it->bidi_p)
25472 glyph->resolved_level = it->bidi_it.resolved_level;
25473 eassert ((it->bidi_it.type & 7) == it->bidi_it.type);
25474 glyph->bidi_type = it->bidi_it.type;
25476 ++it->glyph_row->used[area];
25478 else
25479 IT_EXPAND_MATRIX_WIDTH (it, area);
25483 /* Change IT->ascent and IT->height according to the setting of
25484 IT->voffset. */
25486 static void
25487 take_vertical_position_into_account (struct it *it)
25489 if (it->voffset)
25491 if (it->voffset < 0)
25492 /* Increase the ascent so that we can display the text higher
25493 in the line. */
25494 it->ascent -= it->voffset;
25495 else
25496 /* Increase the descent so that we can display the text lower
25497 in the line. */
25498 it->descent += it->voffset;
25503 /* Produce glyphs/get display metrics for the image IT is loaded with.
25504 See the description of struct display_iterator in dispextern.h for
25505 an overview of struct display_iterator. */
25507 static void
25508 produce_image_glyph (struct it *it)
25510 struct image *img;
25511 struct face *face;
25512 int glyph_ascent, crop;
25513 struct glyph_slice slice;
25515 eassert (it->what == IT_IMAGE);
25517 face = FACE_FROM_ID (it->f, it->face_id);
25518 eassert (face);
25519 /* Make sure X resources of the face is loaded. */
25520 prepare_face_for_display (it->f, face);
25522 if (it->image_id < 0)
25524 /* Fringe bitmap. */
25525 it->ascent = it->phys_ascent = 0;
25526 it->descent = it->phys_descent = 0;
25527 it->pixel_width = 0;
25528 it->nglyphs = 0;
25529 return;
25532 img = IMAGE_FROM_ID (it->f, it->image_id);
25533 eassert (img);
25534 /* Make sure X resources of the image is loaded. */
25535 prepare_image_for_display (it->f, img);
25537 slice.x = slice.y = 0;
25538 slice.width = img->width;
25539 slice.height = img->height;
25541 if (INTEGERP (it->slice.x))
25542 slice.x = XINT (it->slice.x);
25543 else if (FLOATP (it->slice.x))
25544 slice.x = XFLOAT_DATA (it->slice.x) * img->width;
25546 if (INTEGERP (it->slice.y))
25547 slice.y = XINT (it->slice.y);
25548 else if (FLOATP (it->slice.y))
25549 slice.y = XFLOAT_DATA (it->slice.y) * img->height;
25551 if (INTEGERP (it->slice.width))
25552 slice.width = XINT (it->slice.width);
25553 else if (FLOATP (it->slice.width))
25554 slice.width = XFLOAT_DATA (it->slice.width) * img->width;
25556 if (INTEGERP (it->slice.height))
25557 slice.height = XINT (it->slice.height);
25558 else if (FLOATP (it->slice.height))
25559 slice.height = XFLOAT_DATA (it->slice.height) * img->height;
25561 if (slice.x >= img->width)
25562 slice.x = img->width;
25563 if (slice.y >= img->height)
25564 slice.y = img->height;
25565 if (slice.x + slice.width >= img->width)
25566 slice.width = img->width - slice.x;
25567 if (slice.y + slice.height > img->height)
25568 slice.height = img->height - slice.y;
25570 if (slice.width == 0 || slice.height == 0)
25571 return;
25573 it->ascent = it->phys_ascent = glyph_ascent = image_ascent (img, face, &slice);
25575 it->descent = slice.height - glyph_ascent;
25576 if (slice.y == 0)
25577 it->descent += img->vmargin;
25578 if (slice.y + slice.height == img->height)
25579 it->descent += img->vmargin;
25580 it->phys_descent = it->descent;
25582 it->pixel_width = slice.width;
25583 if (slice.x == 0)
25584 it->pixel_width += img->hmargin;
25585 if (slice.x + slice.width == img->width)
25586 it->pixel_width += img->hmargin;
25588 /* It's quite possible for images to have an ascent greater than
25589 their height, so don't get confused in that case. */
25590 if (it->descent < 0)
25591 it->descent = 0;
25593 it->nglyphs = 1;
25595 if (face->box != FACE_NO_BOX)
25597 if (face->box_line_width > 0)
25599 if (slice.y == 0)
25600 it->ascent += face->box_line_width;
25601 if (slice.y + slice.height == img->height)
25602 it->descent += face->box_line_width;
25605 if (it->start_of_box_run_p && slice.x == 0)
25606 it->pixel_width += eabs (face->box_line_width);
25607 if (it->end_of_box_run_p && slice.x + slice.width == img->width)
25608 it->pixel_width += eabs (face->box_line_width);
25611 take_vertical_position_into_account (it);
25613 /* Automatically crop wide image glyphs at right edge so we can
25614 draw the cursor on same display row. */
25615 if ((crop = it->pixel_width - (it->last_visible_x - it->current_x), crop > 0)
25616 && (it->hpos == 0 || it->pixel_width > it->last_visible_x / 4))
25618 it->pixel_width -= crop;
25619 slice.width -= crop;
25622 if (it->glyph_row)
25624 struct glyph *glyph;
25625 enum glyph_row_area area = it->area;
25627 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
25628 if (it->glyph_row->reversed_p)
25630 struct glyph *g;
25632 /* Make room for the new glyph. */
25633 for (g = glyph - 1; g >= it->glyph_row->glyphs[it->area]; g--)
25634 g[1] = *g;
25635 glyph = it->glyph_row->glyphs[it->area];
25637 if (glyph < it->glyph_row->glyphs[area + 1])
25639 glyph->charpos = CHARPOS (it->position);
25640 glyph->object = it->object;
25641 glyph->pixel_width = it->pixel_width;
25642 glyph->ascent = glyph_ascent;
25643 glyph->descent = it->descent;
25644 glyph->voffset = it->voffset;
25645 glyph->type = IMAGE_GLYPH;
25646 glyph->avoid_cursor_p = it->avoid_cursor_p;
25647 glyph->multibyte_p = it->multibyte_p;
25648 if (it->glyph_row->reversed_p && area == TEXT_AREA)
25650 /* In R2L rows, the left and the right box edges need to be
25651 drawn in reverse direction. */
25652 glyph->right_box_line_p = it->start_of_box_run_p;
25653 glyph->left_box_line_p = it->end_of_box_run_p;
25655 else
25657 glyph->left_box_line_p = it->start_of_box_run_p;
25658 glyph->right_box_line_p = it->end_of_box_run_p;
25660 glyph->overlaps_vertically_p = 0;
25661 glyph->padding_p = 0;
25662 glyph->glyph_not_available_p = 0;
25663 glyph->face_id = it->face_id;
25664 glyph->u.img_id = img->id;
25665 glyph->slice.img = slice;
25666 glyph->font_type = FONT_TYPE_UNKNOWN;
25667 if (it->bidi_p)
25669 glyph->resolved_level = it->bidi_it.resolved_level;
25670 eassert ((it->bidi_it.type & 7) == it->bidi_it.type);
25671 glyph->bidi_type = it->bidi_it.type;
25673 ++it->glyph_row->used[area];
25675 else
25676 IT_EXPAND_MATRIX_WIDTH (it, area);
25681 /* Append a stretch glyph to IT->glyph_row. OBJECT is the source
25682 of the glyph, WIDTH and HEIGHT are the width and height of the
25683 stretch. ASCENT is the ascent of the glyph (0 <= ASCENT <= HEIGHT). */
25685 static void
25686 append_stretch_glyph (struct it *it, Lisp_Object object,
25687 int width, int height, int ascent)
25689 struct glyph *glyph;
25690 enum glyph_row_area area = it->area;
25692 eassert (ascent >= 0 && ascent <= height);
25694 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
25695 if (glyph < it->glyph_row->glyphs[area + 1])
25697 /* If the glyph row is reversed, we need to prepend the glyph
25698 rather than append it. */
25699 if (it->glyph_row->reversed_p && area == TEXT_AREA)
25701 struct glyph *g;
25703 /* Make room for the additional glyph. */
25704 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
25705 g[1] = *g;
25706 glyph = it->glyph_row->glyphs[area];
25708 /* Decrease the width of the first glyph of the row that
25709 begins before first_visible_x (e.g., due to hscroll).
25710 This is so the overall width of the row becomes smaller
25711 by the scroll amount, and the stretch glyph appended by
25712 extend_face_to_end_of_line will be wider, to shift the
25713 row glyphs to the right. (In L2R rows, the corresponding
25714 left-shift effect is accomplished by setting row->x to a
25715 negative value, which won't work with R2L rows.)
25717 This must leave us with a positive value of WIDTH, since
25718 otherwise the call to move_it_in_display_line_to at the
25719 beginning of display_line would have got past the entire
25720 first glyph, and then it->current_x would have been
25721 greater or equal to it->first_visible_x. */
25722 if (it->current_x < it->first_visible_x)
25723 width -= it->first_visible_x - it->current_x;
25724 eassert (width > 0);
25726 glyph->charpos = CHARPOS (it->position);
25727 glyph->object = object;
25728 glyph->pixel_width = width;
25729 glyph->ascent = ascent;
25730 glyph->descent = height - ascent;
25731 glyph->voffset = it->voffset;
25732 glyph->type = STRETCH_GLYPH;
25733 glyph->avoid_cursor_p = it->avoid_cursor_p;
25734 glyph->multibyte_p = it->multibyte_p;
25735 if (it->glyph_row->reversed_p && area == TEXT_AREA)
25737 /* In R2L rows, the left and the right box edges need to be
25738 drawn in reverse direction. */
25739 glyph->right_box_line_p = it->start_of_box_run_p;
25740 glyph->left_box_line_p = it->end_of_box_run_p;
25742 else
25744 glyph->left_box_line_p = it->start_of_box_run_p;
25745 glyph->right_box_line_p = it->end_of_box_run_p;
25747 glyph->overlaps_vertically_p = 0;
25748 glyph->padding_p = 0;
25749 glyph->glyph_not_available_p = 0;
25750 glyph->face_id = it->face_id;
25751 glyph->u.stretch.ascent = ascent;
25752 glyph->u.stretch.height = height;
25753 glyph->slice.img = null_glyph_slice;
25754 glyph->font_type = FONT_TYPE_UNKNOWN;
25755 if (it->bidi_p)
25757 glyph->resolved_level = it->bidi_it.resolved_level;
25758 eassert ((it->bidi_it.type & 7) == it->bidi_it.type);
25759 glyph->bidi_type = it->bidi_it.type;
25761 else
25763 glyph->resolved_level = 0;
25764 glyph->bidi_type = UNKNOWN_BT;
25766 ++it->glyph_row->used[area];
25768 else
25769 IT_EXPAND_MATRIX_WIDTH (it, area);
25772 #endif /* HAVE_WINDOW_SYSTEM */
25774 /* Produce a stretch glyph for iterator IT. IT->object is the value
25775 of the glyph property displayed. The value must be a list
25776 `(space KEYWORD VALUE ...)' with the following KEYWORD/VALUE pairs
25777 being recognized:
25779 1. `:width WIDTH' specifies that the space should be WIDTH *
25780 canonical char width wide. WIDTH may be an integer or floating
25781 point number.
25783 2. `:relative-width FACTOR' specifies that the width of the stretch
25784 should be computed from the width of the first character having the
25785 `glyph' property, and should be FACTOR times that width.
25787 3. `:align-to HPOS' specifies that the space should be wide enough
25788 to reach HPOS, a value in canonical character units.
25790 Exactly one of the above pairs must be present.
25792 4. `:height HEIGHT' specifies that the height of the stretch produced
25793 should be HEIGHT, measured in canonical character units.
25795 5. `:relative-height FACTOR' specifies that the height of the
25796 stretch should be FACTOR times the height of the characters having
25797 the glyph property.
25799 Either none or exactly one of 4 or 5 must be present.
25801 6. `:ascent ASCENT' specifies that ASCENT percent of the height
25802 of the stretch should be used for the ascent of the stretch.
25803 ASCENT must be in the range 0 <= ASCENT <= 100. */
25805 void
25806 produce_stretch_glyph (struct it *it)
25808 /* (space :width WIDTH :height HEIGHT ...) */
25809 Lisp_Object prop, plist;
25810 int width = 0, height = 0, align_to = -1;
25811 int zero_width_ok_p = 0;
25812 double tem;
25813 struct font *font = NULL;
25815 #ifdef HAVE_WINDOW_SYSTEM
25816 int ascent = 0;
25817 int zero_height_ok_p = 0;
25819 if (FRAME_WINDOW_P (it->f))
25821 struct face *face = FACE_FROM_ID (it->f, it->face_id);
25822 font = face->font ? face->font : FRAME_FONT (it->f);
25823 prepare_face_for_display (it->f, face);
25825 #endif
25827 /* List should start with `space'. */
25828 eassert (CONSP (it->object) && EQ (XCAR (it->object), Qspace));
25829 plist = XCDR (it->object);
25831 /* Compute the width of the stretch. */
25832 if ((prop = Fplist_get (plist, QCwidth), !NILP (prop))
25833 && calc_pixel_width_or_height (&tem, it, prop, font, 1, 0))
25835 /* Absolute width `:width WIDTH' specified and valid. */
25836 zero_width_ok_p = 1;
25837 width = (int)tem;
25839 #ifdef HAVE_WINDOW_SYSTEM
25840 else if (FRAME_WINDOW_P (it->f)
25841 && (prop = Fplist_get (plist, QCrelative_width), NUMVAL (prop) > 0))
25843 /* Relative width `:relative-width FACTOR' specified and valid.
25844 Compute the width of the characters having the `glyph'
25845 property. */
25846 struct it it2;
25847 unsigned char *p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
25849 it2 = *it;
25850 if (it->multibyte_p)
25851 it2.c = it2.char_to_display = STRING_CHAR_AND_LENGTH (p, it2.len);
25852 else
25854 it2.c = it2.char_to_display = *p, it2.len = 1;
25855 if (! ASCII_CHAR_P (it2.c))
25856 it2.char_to_display = BYTE8_TO_CHAR (it2.c);
25859 it2.glyph_row = NULL;
25860 it2.what = IT_CHARACTER;
25861 x_produce_glyphs (&it2);
25862 width = NUMVAL (prop) * it2.pixel_width;
25864 #endif /* HAVE_WINDOW_SYSTEM */
25865 else if ((prop = Fplist_get (plist, QCalign_to), !NILP (prop))
25866 && calc_pixel_width_or_height (&tem, it, prop, font, 1, &align_to))
25868 if (it->glyph_row == NULL || !it->glyph_row->mode_line_p)
25869 align_to = (align_to < 0
25871 : align_to - window_box_left_offset (it->w, TEXT_AREA));
25872 else if (align_to < 0)
25873 align_to = window_box_left_offset (it->w, TEXT_AREA);
25874 width = max (0, (int)tem + align_to - it->current_x);
25875 zero_width_ok_p = 1;
25877 else
25878 /* Nothing specified -> width defaults to canonical char width. */
25879 width = FRAME_COLUMN_WIDTH (it->f);
25881 if (width <= 0 && (width < 0 || !zero_width_ok_p))
25882 width = 1;
25884 #ifdef HAVE_WINDOW_SYSTEM
25885 /* Compute height. */
25886 if (FRAME_WINDOW_P (it->f))
25888 if ((prop = Fplist_get (plist, QCheight), !NILP (prop))
25889 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
25891 height = (int)tem;
25892 zero_height_ok_p = 1;
25894 else if (prop = Fplist_get (plist, QCrelative_height),
25895 NUMVAL (prop) > 0)
25896 height = FONT_HEIGHT (font) * NUMVAL (prop);
25897 else
25898 height = FONT_HEIGHT (font);
25900 if (height <= 0 && (height < 0 || !zero_height_ok_p))
25901 height = 1;
25903 /* Compute percentage of height used for ascent. If
25904 `:ascent ASCENT' is present and valid, use that. Otherwise,
25905 derive the ascent from the font in use. */
25906 if (prop = Fplist_get (plist, QCascent),
25907 NUMVAL (prop) > 0 && NUMVAL (prop) <= 100)
25908 ascent = height * NUMVAL (prop) / 100.0;
25909 else if (!NILP (prop)
25910 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
25911 ascent = min (max (0, (int)tem), height);
25912 else
25913 ascent = (height * FONT_BASE (font)) / FONT_HEIGHT (font);
25915 else
25916 #endif /* HAVE_WINDOW_SYSTEM */
25917 height = 1;
25919 if (width > 0 && it->line_wrap != TRUNCATE
25920 && it->current_x + width > it->last_visible_x)
25922 width = it->last_visible_x - it->current_x;
25923 #ifdef HAVE_WINDOW_SYSTEM
25924 /* Subtract one more pixel from the stretch width, but only on
25925 GUI frames, since on a TTY each glyph is one "pixel" wide. */
25926 width -= FRAME_WINDOW_P (it->f);
25927 #endif
25930 if (width > 0 && height > 0 && it->glyph_row)
25932 Lisp_Object o_object = it->object;
25933 Lisp_Object object = it->stack[it->sp - 1].string;
25934 int n = width;
25936 if (!STRINGP (object))
25937 object = it->w->contents;
25938 #ifdef HAVE_WINDOW_SYSTEM
25939 if (FRAME_WINDOW_P (it->f))
25940 append_stretch_glyph (it, object, width, height, ascent);
25941 else
25942 #endif
25944 it->object = object;
25945 it->char_to_display = ' ';
25946 it->pixel_width = it->len = 1;
25947 while (n--)
25948 tty_append_glyph (it);
25949 it->object = o_object;
25953 it->pixel_width = width;
25954 #ifdef HAVE_WINDOW_SYSTEM
25955 if (FRAME_WINDOW_P (it->f))
25957 it->ascent = it->phys_ascent = ascent;
25958 it->descent = it->phys_descent = height - it->ascent;
25959 it->nglyphs = width > 0 && height > 0 ? 1 : 0;
25960 take_vertical_position_into_account (it);
25962 else
25963 #endif
25964 it->nglyphs = width;
25967 /* Get information about special display element WHAT in an
25968 environment described by IT. WHAT is one of IT_TRUNCATION or
25969 IT_CONTINUATION. Maybe produce glyphs for WHAT if IT has a
25970 non-null glyph_row member. This function ensures that fields like
25971 face_id, c, len of IT are left untouched. */
25973 static void
25974 produce_special_glyphs (struct it *it, enum display_element_type what)
25976 struct it temp_it;
25977 Lisp_Object gc;
25978 GLYPH glyph;
25980 temp_it = *it;
25981 temp_it.object = Qnil;
25982 memset (&temp_it.current, 0, sizeof temp_it.current);
25984 if (what == IT_CONTINUATION)
25986 /* Continuation glyph. For R2L lines, we mirror it by hand. */
25987 if (it->bidi_it.paragraph_dir == R2L)
25988 SET_GLYPH_FROM_CHAR (glyph, '/');
25989 else
25990 SET_GLYPH_FROM_CHAR (glyph, '\\');
25991 if (it->dp
25992 && (gc = DISP_CONTINUE_GLYPH (it->dp), GLYPH_CODE_P (gc)))
25994 /* FIXME: Should we mirror GC for R2L lines? */
25995 SET_GLYPH_FROM_GLYPH_CODE (glyph, gc);
25996 spec_glyph_lookup_face (XWINDOW (it->window), &glyph);
25999 else if (what == IT_TRUNCATION)
26001 /* Truncation glyph. */
26002 SET_GLYPH_FROM_CHAR (glyph, '$');
26003 if (it->dp
26004 && (gc = DISP_TRUNC_GLYPH (it->dp), GLYPH_CODE_P (gc)))
26006 /* FIXME: Should we mirror GC for R2L lines? */
26007 SET_GLYPH_FROM_GLYPH_CODE (glyph, gc);
26008 spec_glyph_lookup_face (XWINDOW (it->window), &glyph);
26011 else
26012 emacs_abort ();
26014 #ifdef HAVE_WINDOW_SYSTEM
26015 /* On a GUI frame, when the right fringe (left fringe for R2L rows)
26016 is turned off, we precede the truncation/continuation glyphs by a
26017 stretch glyph whose width is computed such that these special
26018 glyphs are aligned at the window margin, even when very different
26019 fonts are used in different glyph rows. */
26020 if (FRAME_WINDOW_P (temp_it.f)
26021 /* init_iterator calls this with it->glyph_row == NULL, and it
26022 wants only the pixel width of the truncation/continuation
26023 glyphs. */
26024 && temp_it.glyph_row
26025 /* insert_left_trunc_glyphs calls us at the beginning of the
26026 row, and it has its own calculation of the stretch glyph
26027 width. */
26028 && temp_it.glyph_row->used[TEXT_AREA] > 0
26029 && (temp_it.glyph_row->reversed_p
26030 ? WINDOW_LEFT_FRINGE_WIDTH (temp_it.w)
26031 : WINDOW_RIGHT_FRINGE_WIDTH (temp_it.w)) == 0)
26033 int stretch_width = temp_it.last_visible_x - temp_it.current_x;
26035 if (stretch_width > 0)
26037 struct face *face = FACE_FROM_ID (temp_it.f, temp_it.face_id);
26038 struct font *font =
26039 face->font ? face->font : FRAME_FONT (temp_it.f);
26040 int stretch_ascent =
26041 (((temp_it.ascent + temp_it.descent)
26042 * FONT_BASE (font)) / FONT_HEIGHT (font));
26044 append_stretch_glyph (&temp_it, Qnil, stretch_width,
26045 temp_it.ascent + temp_it.descent,
26046 stretch_ascent);
26049 #endif
26051 temp_it.dp = NULL;
26052 temp_it.what = IT_CHARACTER;
26053 temp_it.c = temp_it.char_to_display = GLYPH_CHAR (glyph);
26054 temp_it.face_id = GLYPH_FACE (glyph);
26055 temp_it.len = CHAR_BYTES (temp_it.c);
26057 PRODUCE_GLYPHS (&temp_it);
26058 it->pixel_width = temp_it.pixel_width;
26059 it->nglyphs = temp_it.nglyphs;
26062 #ifdef HAVE_WINDOW_SYSTEM
26064 /* Calculate line-height and line-spacing properties.
26065 An integer value specifies explicit pixel value.
26066 A float value specifies relative value to current face height.
26067 A cons (float . face-name) specifies relative value to
26068 height of specified face font.
26070 Returns height in pixels, or nil. */
26073 static Lisp_Object
26074 calc_line_height_property (struct it *it, Lisp_Object val, struct font *font,
26075 int boff, int override)
26077 Lisp_Object face_name = Qnil;
26078 int ascent, descent, height;
26080 if (NILP (val) || INTEGERP (val) || (override && EQ (val, Qt)))
26081 return val;
26083 if (CONSP (val))
26085 face_name = XCAR (val);
26086 val = XCDR (val);
26087 if (!NUMBERP (val))
26088 val = make_number (1);
26089 if (NILP (face_name))
26091 height = it->ascent + it->descent;
26092 goto scale;
26096 if (NILP (face_name))
26098 font = FRAME_FONT (it->f);
26099 boff = FRAME_BASELINE_OFFSET (it->f);
26101 else if (EQ (face_name, Qt))
26103 override = 0;
26105 else
26107 int face_id;
26108 struct face *face;
26110 face_id = lookup_named_face (it->f, face_name, false);
26111 if (face_id < 0)
26112 return make_number (-1);
26114 face = FACE_FROM_ID (it->f, face_id);
26115 font = face->font;
26116 if (font == NULL)
26117 return make_number (-1);
26118 boff = font->baseline_offset;
26119 if (font->vertical_centering)
26120 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
26123 ascent = FONT_BASE (font) + boff;
26124 descent = FONT_DESCENT (font) - boff;
26126 if (override)
26128 it->override_ascent = ascent;
26129 it->override_descent = descent;
26130 it->override_boff = boff;
26133 height = ascent + descent;
26135 scale:
26136 if (FLOATP (val))
26137 height = (int)(XFLOAT_DATA (val) * height);
26138 else if (INTEGERP (val))
26139 height *= XINT (val);
26141 return make_number (height);
26145 /* Append a glyph for a glyphless character to IT->glyph_row. FACE_ID
26146 is a face ID to be used for the glyph. FOR_NO_FONT is nonzero if
26147 and only if this is for a character for which no font was found.
26149 If the display method (it->glyphless_method) is
26150 GLYPHLESS_DISPLAY_ACRONYM or GLYPHLESS_DISPLAY_HEX_CODE, LEN is a
26151 length of the acronym or the hexadecimal string, UPPER_XOFF and
26152 UPPER_YOFF are pixel offsets for the upper part of the string,
26153 LOWER_XOFF and LOWER_YOFF are for the lower part.
26155 For the other display methods, LEN through LOWER_YOFF are zero. */
26157 static void
26158 append_glyphless_glyph (struct it *it, int face_id, int for_no_font, int len,
26159 short upper_xoff, short upper_yoff,
26160 short lower_xoff, short lower_yoff)
26162 struct glyph *glyph;
26163 enum glyph_row_area area = it->area;
26165 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
26166 if (glyph < it->glyph_row->glyphs[area + 1])
26168 /* If the glyph row is reversed, we need to prepend the glyph
26169 rather than append it. */
26170 if (it->glyph_row->reversed_p && area == TEXT_AREA)
26172 struct glyph *g;
26174 /* Make room for the additional glyph. */
26175 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
26176 g[1] = *g;
26177 glyph = it->glyph_row->glyphs[area];
26179 glyph->charpos = CHARPOS (it->position);
26180 glyph->object = it->object;
26181 glyph->pixel_width = it->pixel_width;
26182 glyph->ascent = it->ascent;
26183 glyph->descent = it->descent;
26184 glyph->voffset = it->voffset;
26185 glyph->type = GLYPHLESS_GLYPH;
26186 glyph->u.glyphless.method = it->glyphless_method;
26187 glyph->u.glyphless.for_no_font = for_no_font;
26188 glyph->u.glyphless.len = len;
26189 glyph->u.glyphless.ch = it->c;
26190 glyph->slice.glyphless.upper_xoff = upper_xoff;
26191 glyph->slice.glyphless.upper_yoff = upper_yoff;
26192 glyph->slice.glyphless.lower_xoff = lower_xoff;
26193 glyph->slice.glyphless.lower_yoff = lower_yoff;
26194 glyph->avoid_cursor_p = it->avoid_cursor_p;
26195 glyph->multibyte_p = it->multibyte_p;
26196 if (it->glyph_row->reversed_p && area == TEXT_AREA)
26198 /* In R2L rows, the left and the right box edges need to be
26199 drawn in reverse direction. */
26200 glyph->right_box_line_p = it->start_of_box_run_p;
26201 glyph->left_box_line_p = it->end_of_box_run_p;
26203 else
26205 glyph->left_box_line_p = it->start_of_box_run_p;
26206 glyph->right_box_line_p = it->end_of_box_run_p;
26208 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
26209 || it->phys_descent > it->descent);
26210 glyph->padding_p = 0;
26211 glyph->glyph_not_available_p = 0;
26212 glyph->face_id = face_id;
26213 glyph->font_type = FONT_TYPE_UNKNOWN;
26214 if (it->bidi_p)
26216 glyph->resolved_level = it->bidi_it.resolved_level;
26217 eassert ((it->bidi_it.type & 7) == it->bidi_it.type);
26218 glyph->bidi_type = it->bidi_it.type;
26220 ++it->glyph_row->used[area];
26222 else
26223 IT_EXPAND_MATRIX_WIDTH (it, area);
26227 /* Produce a glyph for a glyphless character for iterator IT.
26228 IT->glyphless_method specifies which method to use for displaying
26229 the character. See the description of enum
26230 glyphless_display_method in dispextern.h for the detail.
26232 FOR_NO_FONT is nonzero if and only if this is for a character for
26233 which no font was found. ACRONYM, if non-nil, is an acronym string
26234 for the character. */
26236 static void
26237 produce_glyphless_glyph (struct it *it, int for_no_font, Lisp_Object acronym)
26239 int face_id;
26240 struct face *face;
26241 struct font *font;
26242 int base_width, base_height, width, height;
26243 short upper_xoff, upper_yoff, lower_xoff, lower_yoff;
26244 int len;
26246 /* Get the metrics of the base font. We always refer to the current
26247 ASCII face. */
26248 face = FACE_FROM_ID (it->f, it->face_id)->ascii_face;
26249 font = face->font ? face->font : FRAME_FONT (it->f);
26250 it->ascent = FONT_BASE (font) + font->baseline_offset;
26251 it->descent = FONT_DESCENT (font) - font->baseline_offset;
26252 base_height = it->ascent + it->descent;
26253 base_width = font->average_width;
26255 face_id = merge_glyphless_glyph_face (it);
26257 if (it->glyphless_method == GLYPHLESS_DISPLAY_THIN_SPACE)
26259 it->pixel_width = THIN_SPACE_WIDTH;
26260 len = 0;
26261 upper_xoff = upper_yoff = lower_xoff = lower_yoff = 0;
26263 else if (it->glyphless_method == GLYPHLESS_DISPLAY_EMPTY_BOX)
26265 width = CHAR_WIDTH (it->c);
26266 if (width == 0)
26267 width = 1;
26268 else if (width > 4)
26269 width = 4;
26270 it->pixel_width = base_width * width;
26271 len = 0;
26272 upper_xoff = upper_yoff = lower_xoff = lower_yoff = 0;
26274 else
26276 char buf[7];
26277 const char *str;
26278 unsigned int code[6];
26279 int upper_len;
26280 int ascent, descent;
26281 struct font_metrics metrics_upper, metrics_lower;
26283 face = FACE_FROM_ID (it->f, face_id);
26284 font = face->font ? face->font : FRAME_FONT (it->f);
26285 prepare_face_for_display (it->f, face);
26287 if (it->glyphless_method == GLYPHLESS_DISPLAY_ACRONYM)
26289 if (! STRINGP (acronym) && CHAR_TABLE_P (Vglyphless_char_display))
26290 acronym = CHAR_TABLE_REF (Vglyphless_char_display, it->c);
26291 if (CONSP (acronym))
26292 acronym = XCAR (acronym);
26293 str = STRINGP (acronym) ? SSDATA (acronym) : "";
26295 else
26297 eassert (it->glyphless_method == GLYPHLESS_DISPLAY_HEX_CODE);
26298 sprintf (buf, "%0*X", it->c < 0x10000 ? 4 : 6, it->c);
26299 str = buf;
26301 for (len = 0; str[len] && ASCII_CHAR_P (str[len]) && len < 6; len++)
26302 code[len] = font->driver->encode_char (font, str[len]);
26303 upper_len = (len + 1) / 2;
26304 font->driver->text_extents (font, code, upper_len,
26305 &metrics_upper);
26306 font->driver->text_extents (font, code + upper_len, len - upper_len,
26307 &metrics_lower);
26311 /* +4 is for vertical bars of a box plus 1-pixel spaces at both side. */
26312 width = max (metrics_upper.width, metrics_lower.width) + 4;
26313 upper_xoff = upper_yoff = 2; /* the typical case */
26314 if (base_width >= width)
26316 /* Align the upper to the left, the lower to the right. */
26317 it->pixel_width = base_width;
26318 lower_xoff = base_width - 2 - metrics_lower.width;
26320 else
26322 /* Center the shorter one. */
26323 it->pixel_width = width;
26324 if (metrics_upper.width >= metrics_lower.width)
26325 lower_xoff = (width - metrics_lower.width) / 2;
26326 else
26328 /* FIXME: This code doesn't look right. It formerly was
26329 missing the "lower_xoff = 0;", which couldn't have
26330 been right since it left lower_xoff uninitialized. */
26331 lower_xoff = 0;
26332 upper_xoff = (width - metrics_upper.width) / 2;
26336 /* +5 is for horizontal bars of a box plus 1-pixel spaces at
26337 top, bottom, and between upper and lower strings. */
26338 height = (metrics_upper.ascent + metrics_upper.descent
26339 + metrics_lower.ascent + metrics_lower.descent) + 5;
26340 /* Center vertically.
26341 H:base_height, D:base_descent
26342 h:height, ld:lower_descent, la:lower_ascent, ud:upper_descent
26344 ascent = - (D - H/2 - h/2 + 1); "+ 1" for rounding up
26345 descent = D - H/2 + h/2;
26346 lower_yoff = descent - 2 - ld;
26347 upper_yoff = lower_yoff - la - 1 - ud; */
26348 ascent = - (it->descent - (base_height + height + 1) / 2);
26349 descent = it->descent - (base_height - height) / 2;
26350 lower_yoff = descent - 2 - metrics_lower.descent;
26351 upper_yoff = (lower_yoff - metrics_lower.ascent - 1
26352 - metrics_upper.descent);
26353 /* Don't make the height shorter than the base height. */
26354 if (height > base_height)
26356 it->ascent = ascent;
26357 it->descent = descent;
26361 it->phys_ascent = it->ascent;
26362 it->phys_descent = it->descent;
26363 if (it->glyph_row)
26364 append_glyphless_glyph (it, face_id, for_no_font, len,
26365 upper_xoff, upper_yoff,
26366 lower_xoff, lower_yoff);
26367 it->nglyphs = 1;
26368 take_vertical_position_into_account (it);
26372 /* RIF:
26373 Produce glyphs/get display metrics for the display element IT is
26374 loaded with. See the description of struct it in dispextern.h
26375 for an overview of struct it. */
26377 void
26378 x_produce_glyphs (struct it *it)
26380 int extra_line_spacing = it->extra_line_spacing;
26382 it->glyph_not_available_p = 0;
26384 if (it->what == IT_CHARACTER)
26386 XChar2b char2b;
26387 struct face *face = FACE_FROM_ID (it->f, it->face_id);
26388 struct font *font = face->font;
26389 struct font_metrics *pcm = NULL;
26390 int boff; /* Baseline offset. */
26392 if (font == NULL)
26394 /* When no suitable font is found, display this character by
26395 the method specified in the first extra slot of
26396 Vglyphless_char_display. */
26397 Lisp_Object acronym = lookup_glyphless_char_display (-1, it);
26399 eassert (it->what == IT_GLYPHLESS);
26400 produce_glyphless_glyph (it, 1, STRINGP (acronym) ? acronym : Qnil);
26401 goto done;
26404 boff = font->baseline_offset;
26405 if (font->vertical_centering)
26406 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
26408 if (it->char_to_display != '\n' && it->char_to_display != '\t')
26410 int stretched_p;
26412 it->nglyphs = 1;
26414 if (it->override_ascent >= 0)
26416 it->ascent = it->override_ascent;
26417 it->descent = it->override_descent;
26418 boff = it->override_boff;
26420 else
26422 it->ascent = FONT_BASE (font) + boff;
26423 it->descent = FONT_DESCENT (font) - boff;
26426 if (get_char_glyph_code (it->char_to_display, font, &char2b))
26428 pcm = get_per_char_metric (font, &char2b);
26429 if (pcm->width == 0
26430 && pcm->rbearing == 0 && pcm->lbearing == 0)
26431 pcm = NULL;
26434 if (pcm)
26436 it->phys_ascent = pcm->ascent + boff;
26437 it->phys_descent = pcm->descent - boff;
26438 it->pixel_width = pcm->width;
26440 else
26442 it->glyph_not_available_p = 1;
26443 it->phys_ascent = it->ascent;
26444 it->phys_descent = it->descent;
26445 it->pixel_width = font->space_width;
26448 if (it->constrain_row_ascent_descent_p)
26450 if (it->descent > it->max_descent)
26452 it->ascent += it->descent - it->max_descent;
26453 it->descent = it->max_descent;
26455 if (it->ascent > it->max_ascent)
26457 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
26458 it->ascent = it->max_ascent;
26460 it->phys_ascent = min (it->phys_ascent, it->ascent);
26461 it->phys_descent = min (it->phys_descent, it->descent);
26462 extra_line_spacing = 0;
26465 /* If this is a space inside a region of text with
26466 `space-width' property, change its width. */
26467 stretched_p = it->char_to_display == ' ' && !NILP (it->space_width);
26468 if (stretched_p)
26469 it->pixel_width *= XFLOATINT (it->space_width);
26471 /* If face has a box, add the box thickness to the character
26472 height. If character has a box line to the left and/or
26473 right, add the box line width to the character's width. */
26474 if (face->box != FACE_NO_BOX)
26476 int thick = face->box_line_width;
26478 if (thick > 0)
26480 it->ascent += thick;
26481 it->descent += thick;
26483 else
26484 thick = -thick;
26486 if (it->start_of_box_run_p)
26487 it->pixel_width += thick;
26488 if (it->end_of_box_run_p)
26489 it->pixel_width += thick;
26492 /* If face has an overline, add the height of the overline
26493 (1 pixel) and a 1 pixel margin to the character height. */
26494 if (face->overline_p)
26495 it->ascent += overline_margin;
26497 if (it->constrain_row_ascent_descent_p)
26499 if (it->ascent > it->max_ascent)
26500 it->ascent = it->max_ascent;
26501 if (it->descent > it->max_descent)
26502 it->descent = it->max_descent;
26505 take_vertical_position_into_account (it);
26507 /* If we have to actually produce glyphs, do it. */
26508 if (it->glyph_row)
26510 if (stretched_p)
26512 /* Translate a space with a `space-width' property
26513 into a stretch glyph. */
26514 int ascent = (((it->ascent + it->descent) * FONT_BASE (font))
26515 / FONT_HEIGHT (font));
26516 append_stretch_glyph (it, it->object, it->pixel_width,
26517 it->ascent + it->descent, ascent);
26519 else
26520 append_glyph (it);
26522 /* If characters with lbearing or rbearing are displayed
26523 in this line, record that fact in a flag of the
26524 glyph row. This is used to optimize X output code. */
26525 if (pcm && (pcm->lbearing < 0 || pcm->rbearing > pcm->width))
26526 it->glyph_row->contains_overlapping_glyphs_p = 1;
26528 if (! stretched_p && it->pixel_width == 0)
26529 /* We assure that all visible glyphs have at least 1-pixel
26530 width. */
26531 it->pixel_width = 1;
26533 else if (it->char_to_display == '\n')
26535 /* A newline has no width, but we need the height of the
26536 line. But if previous part of the line sets a height,
26537 don't increase that height. */
26539 Lisp_Object height;
26540 Lisp_Object total_height = Qnil;
26542 it->override_ascent = -1;
26543 it->pixel_width = 0;
26544 it->nglyphs = 0;
26546 height = get_it_property (it, Qline_height);
26547 /* Split (line-height total-height) list. */
26548 if (CONSP (height)
26549 && CONSP (XCDR (height))
26550 && NILP (XCDR (XCDR (height))))
26552 total_height = XCAR (XCDR (height));
26553 height = XCAR (height);
26555 height = calc_line_height_property (it, height, font, boff, 1);
26557 if (it->override_ascent >= 0)
26559 it->ascent = it->override_ascent;
26560 it->descent = it->override_descent;
26561 boff = it->override_boff;
26563 else
26565 it->ascent = FONT_BASE (font) + boff;
26566 it->descent = FONT_DESCENT (font) - boff;
26569 if (EQ (height, Qt))
26571 if (it->descent > it->max_descent)
26573 it->ascent += it->descent - it->max_descent;
26574 it->descent = it->max_descent;
26576 if (it->ascent > it->max_ascent)
26578 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
26579 it->ascent = it->max_ascent;
26581 it->phys_ascent = min (it->phys_ascent, it->ascent);
26582 it->phys_descent = min (it->phys_descent, it->descent);
26583 it->constrain_row_ascent_descent_p = 1;
26584 extra_line_spacing = 0;
26586 else
26588 Lisp_Object spacing;
26590 it->phys_ascent = it->ascent;
26591 it->phys_descent = it->descent;
26593 if ((it->max_ascent > 0 || it->max_descent > 0)
26594 && face->box != FACE_NO_BOX
26595 && face->box_line_width > 0)
26597 it->ascent += face->box_line_width;
26598 it->descent += face->box_line_width;
26600 if (!NILP (height)
26601 && XINT (height) > it->ascent + it->descent)
26602 it->ascent = XINT (height) - it->descent;
26604 if (!NILP (total_height))
26605 spacing = calc_line_height_property (it, total_height, font, boff, 0);
26606 else
26608 spacing = get_it_property (it, Qline_spacing);
26609 spacing = calc_line_height_property (it, spacing, font, boff, 0);
26611 if (INTEGERP (spacing))
26613 extra_line_spacing = XINT (spacing);
26614 if (!NILP (total_height))
26615 extra_line_spacing -= (it->phys_ascent + it->phys_descent);
26619 else /* i.e. (it->char_to_display == '\t') */
26621 if (font->space_width > 0)
26623 int tab_width = it->tab_width * font->space_width;
26624 int x = it->current_x + it->continuation_lines_width;
26625 int next_tab_x = ((1 + x + tab_width - 1) / tab_width) * tab_width;
26627 /* If the distance from the current position to the next tab
26628 stop is less than a space character width, use the
26629 tab stop after that. */
26630 if (next_tab_x - x < font->space_width)
26631 next_tab_x += tab_width;
26633 it->pixel_width = next_tab_x - x;
26634 it->nglyphs = 1;
26635 it->ascent = it->phys_ascent = FONT_BASE (font) + boff;
26636 it->descent = it->phys_descent = FONT_DESCENT (font) - boff;
26638 if (it->glyph_row)
26640 append_stretch_glyph (it, it->object, it->pixel_width,
26641 it->ascent + it->descent, it->ascent);
26644 else
26646 it->pixel_width = 0;
26647 it->nglyphs = 1;
26651 else if (it->what == IT_COMPOSITION && it->cmp_it.ch < 0)
26653 /* A static composition.
26655 Note: A composition is represented as one glyph in the
26656 glyph matrix. There are no padding glyphs.
26658 Important note: pixel_width, ascent, and descent are the
26659 values of what is drawn by draw_glyphs (i.e. the values of
26660 the overall glyphs composed). */
26661 struct face *face = FACE_FROM_ID (it->f, it->face_id);
26662 int boff; /* baseline offset */
26663 struct composition *cmp = composition_table[it->cmp_it.id];
26664 int glyph_len = cmp->glyph_len;
26665 struct font *font = face->font;
26667 it->nglyphs = 1;
26669 /* If we have not yet calculated pixel size data of glyphs of
26670 the composition for the current face font, calculate them
26671 now. Theoretically, we have to check all fonts for the
26672 glyphs, but that requires much time and memory space. So,
26673 here we check only the font of the first glyph. This may
26674 lead to incorrect display, but it's very rare, and C-l
26675 (recenter-top-bottom) can correct the display anyway. */
26676 if (! cmp->font || cmp->font != font)
26678 /* Ascent and descent of the font of the first character
26679 of this composition (adjusted by baseline offset).
26680 Ascent and descent of overall glyphs should not be less
26681 than these, respectively. */
26682 int font_ascent, font_descent, font_height;
26683 /* Bounding box of the overall glyphs. */
26684 int leftmost, rightmost, lowest, highest;
26685 int lbearing, rbearing;
26686 int i, width, ascent, descent;
26687 int left_padded = 0, right_padded = 0;
26688 int c IF_LINT (= 0); /* cmp->glyph_len can't be zero; see Bug#8512 */
26689 XChar2b char2b;
26690 struct font_metrics *pcm;
26691 int font_not_found_p;
26692 ptrdiff_t pos;
26694 for (glyph_len = cmp->glyph_len; glyph_len > 0; glyph_len--)
26695 if ((c = COMPOSITION_GLYPH (cmp, glyph_len - 1)) != '\t')
26696 break;
26697 if (glyph_len < cmp->glyph_len)
26698 right_padded = 1;
26699 for (i = 0; i < glyph_len; i++)
26701 if ((c = COMPOSITION_GLYPH (cmp, i)) != '\t')
26702 break;
26703 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
26705 if (i > 0)
26706 left_padded = 1;
26708 pos = (STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
26709 : IT_CHARPOS (*it));
26710 /* If no suitable font is found, use the default font. */
26711 font_not_found_p = font == NULL;
26712 if (font_not_found_p)
26714 face = face->ascii_face;
26715 font = face->font;
26717 boff = font->baseline_offset;
26718 if (font->vertical_centering)
26719 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
26720 font_ascent = FONT_BASE (font) + boff;
26721 font_descent = FONT_DESCENT (font) - boff;
26722 font_height = FONT_HEIGHT (font);
26724 cmp->font = font;
26726 pcm = NULL;
26727 if (! font_not_found_p)
26729 get_char_face_and_encoding (it->f, c, it->face_id,
26730 &char2b, 0);
26731 pcm = get_per_char_metric (font, &char2b);
26734 /* Initialize the bounding box. */
26735 if (pcm)
26737 width = cmp->glyph_len > 0 ? pcm->width : 0;
26738 ascent = pcm->ascent;
26739 descent = pcm->descent;
26740 lbearing = pcm->lbearing;
26741 rbearing = pcm->rbearing;
26743 else
26745 width = cmp->glyph_len > 0 ? font->space_width : 0;
26746 ascent = FONT_BASE (font);
26747 descent = FONT_DESCENT (font);
26748 lbearing = 0;
26749 rbearing = width;
26752 rightmost = width;
26753 leftmost = 0;
26754 lowest = - descent + boff;
26755 highest = ascent + boff;
26757 if (! font_not_found_p
26758 && font->default_ascent
26759 && CHAR_TABLE_P (Vuse_default_ascent)
26760 && !NILP (Faref (Vuse_default_ascent,
26761 make_number (it->char_to_display))))
26762 highest = font->default_ascent + boff;
26764 /* Draw the first glyph at the normal position. It may be
26765 shifted to right later if some other glyphs are drawn
26766 at the left. */
26767 cmp->offsets[i * 2] = 0;
26768 cmp->offsets[i * 2 + 1] = boff;
26769 cmp->lbearing = lbearing;
26770 cmp->rbearing = rbearing;
26772 /* Set cmp->offsets for the remaining glyphs. */
26773 for (i++; i < glyph_len; i++)
26775 int left, right, btm, top;
26776 int ch = COMPOSITION_GLYPH (cmp, i);
26777 int face_id;
26778 struct face *this_face;
26780 if (ch == '\t')
26781 ch = ' ';
26782 face_id = FACE_FOR_CHAR (it->f, face, ch, pos, it->string);
26783 this_face = FACE_FROM_ID (it->f, face_id);
26784 font = this_face->font;
26786 if (font == NULL)
26787 pcm = NULL;
26788 else
26790 get_char_face_and_encoding (it->f, ch, face_id,
26791 &char2b, 0);
26792 pcm = get_per_char_metric (font, &char2b);
26794 if (! pcm)
26795 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
26796 else
26798 width = pcm->width;
26799 ascent = pcm->ascent;
26800 descent = pcm->descent;
26801 lbearing = pcm->lbearing;
26802 rbearing = pcm->rbearing;
26803 if (cmp->method != COMPOSITION_WITH_RULE_ALTCHARS)
26805 /* Relative composition with or without
26806 alternate chars. */
26807 left = (leftmost + rightmost - width) / 2;
26808 btm = - descent + boff;
26809 if (font->relative_compose
26810 && (! CHAR_TABLE_P (Vignore_relative_composition)
26811 || NILP (Faref (Vignore_relative_composition,
26812 make_number (ch)))))
26815 if (- descent >= font->relative_compose)
26816 /* One extra pixel between two glyphs. */
26817 btm = highest + 1;
26818 else if (ascent <= 0)
26819 /* One extra pixel between two glyphs. */
26820 btm = lowest - 1 - ascent - descent;
26823 else
26825 /* A composition rule is specified by an integer
26826 value that encodes global and new reference
26827 points (GREF and NREF). GREF and NREF are
26828 specified by numbers as below:
26830 0---1---2 -- ascent
26834 9--10--11 -- center
26836 ---3---4---5--- baseline
26838 6---7---8 -- descent
26840 int rule = COMPOSITION_RULE (cmp, i);
26841 int gref, nref, grefx, grefy, nrefx, nrefy, xoff, yoff;
26843 COMPOSITION_DECODE_RULE (rule, gref, nref, xoff, yoff);
26844 grefx = gref % 3, nrefx = nref % 3;
26845 grefy = gref / 3, nrefy = nref / 3;
26846 if (xoff)
26847 xoff = font_height * (xoff - 128) / 256;
26848 if (yoff)
26849 yoff = font_height * (yoff - 128) / 256;
26851 left = (leftmost
26852 + grefx * (rightmost - leftmost) / 2
26853 - nrefx * width / 2
26854 + xoff);
26856 btm = ((grefy == 0 ? highest
26857 : grefy == 1 ? 0
26858 : grefy == 2 ? lowest
26859 : (highest + lowest) / 2)
26860 - (nrefy == 0 ? ascent + descent
26861 : nrefy == 1 ? descent - boff
26862 : nrefy == 2 ? 0
26863 : (ascent + descent) / 2)
26864 + yoff);
26867 cmp->offsets[i * 2] = left;
26868 cmp->offsets[i * 2 + 1] = btm + descent;
26870 /* Update the bounding box of the overall glyphs. */
26871 if (width > 0)
26873 right = left + width;
26874 if (left < leftmost)
26875 leftmost = left;
26876 if (right > rightmost)
26877 rightmost = right;
26879 top = btm + descent + ascent;
26880 if (top > highest)
26881 highest = top;
26882 if (btm < lowest)
26883 lowest = btm;
26885 if (cmp->lbearing > left + lbearing)
26886 cmp->lbearing = left + lbearing;
26887 if (cmp->rbearing < left + rbearing)
26888 cmp->rbearing = left + rbearing;
26892 /* If there are glyphs whose x-offsets are negative,
26893 shift all glyphs to the right and make all x-offsets
26894 non-negative. */
26895 if (leftmost < 0)
26897 for (i = 0; i < cmp->glyph_len; i++)
26898 cmp->offsets[i * 2] -= leftmost;
26899 rightmost -= leftmost;
26900 cmp->lbearing -= leftmost;
26901 cmp->rbearing -= leftmost;
26904 if (left_padded && cmp->lbearing < 0)
26906 for (i = 0; i < cmp->glyph_len; i++)
26907 cmp->offsets[i * 2] -= cmp->lbearing;
26908 rightmost -= cmp->lbearing;
26909 cmp->rbearing -= cmp->lbearing;
26910 cmp->lbearing = 0;
26912 if (right_padded && rightmost < cmp->rbearing)
26914 rightmost = cmp->rbearing;
26917 cmp->pixel_width = rightmost;
26918 cmp->ascent = highest;
26919 cmp->descent = - lowest;
26920 if (cmp->ascent < font_ascent)
26921 cmp->ascent = font_ascent;
26922 if (cmp->descent < font_descent)
26923 cmp->descent = font_descent;
26926 if (it->glyph_row
26927 && (cmp->lbearing < 0
26928 || cmp->rbearing > cmp->pixel_width))
26929 it->glyph_row->contains_overlapping_glyphs_p = 1;
26931 it->pixel_width = cmp->pixel_width;
26932 it->ascent = it->phys_ascent = cmp->ascent;
26933 it->descent = it->phys_descent = cmp->descent;
26934 if (face->box != FACE_NO_BOX)
26936 int thick = face->box_line_width;
26938 if (thick > 0)
26940 it->ascent += thick;
26941 it->descent += thick;
26943 else
26944 thick = - thick;
26946 if (it->start_of_box_run_p)
26947 it->pixel_width += thick;
26948 if (it->end_of_box_run_p)
26949 it->pixel_width += thick;
26952 /* If face has an overline, add the height of the overline
26953 (1 pixel) and a 1 pixel margin to the character height. */
26954 if (face->overline_p)
26955 it->ascent += overline_margin;
26957 take_vertical_position_into_account (it);
26958 if (it->ascent < 0)
26959 it->ascent = 0;
26960 if (it->descent < 0)
26961 it->descent = 0;
26963 if (it->glyph_row && cmp->glyph_len > 0)
26964 append_composite_glyph (it);
26966 else if (it->what == IT_COMPOSITION)
26968 /* A dynamic (automatic) composition. */
26969 struct face *face = FACE_FROM_ID (it->f, it->face_id);
26970 Lisp_Object gstring;
26971 struct font_metrics metrics;
26973 it->nglyphs = 1;
26975 gstring = composition_gstring_from_id (it->cmp_it.id);
26976 it->pixel_width
26977 = composition_gstring_width (gstring, it->cmp_it.from, it->cmp_it.to,
26978 &metrics);
26979 if (it->glyph_row
26980 && (metrics.lbearing < 0 || metrics.rbearing > metrics.width))
26981 it->glyph_row->contains_overlapping_glyphs_p = 1;
26982 it->ascent = it->phys_ascent = metrics.ascent;
26983 it->descent = it->phys_descent = metrics.descent;
26984 if (face->box != FACE_NO_BOX)
26986 int thick = face->box_line_width;
26988 if (thick > 0)
26990 it->ascent += thick;
26991 it->descent += thick;
26993 else
26994 thick = - thick;
26996 if (it->start_of_box_run_p)
26997 it->pixel_width += thick;
26998 if (it->end_of_box_run_p)
26999 it->pixel_width += thick;
27001 /* If face has an overline, add the height of the overline
27002 (1 pixel) and a 1 pixel margin to the character height. */
27003 if (face->overline_p)
27004 it->ascent += overline_margin;
27005 take_vertical_position_into_account (it);
27006 if (it->ascent < 0)
27007 it->ascent = 0;
27008 if (it->descent < 0)
27009 it->descent = 0;
27011 if (it->glyph_row)
27012 append_composite_glyph (it);
27014 else if (it->what == IT_GLYPHLESS)
27015 produce_glyphless_glyph (it, 0, Qnil);
27016 else if (it->what == IT_IMAGE)
27017 produce_image_glyph (it);
27018 else if (it->what == IT_STRETCH)
27019 produce_stretch_glyph (it);
27021 done:
27022 /* Accumulate dimensions. Note: can't assume that it->descent > 0
27023 because this isn't true for images with `:ascent 100'. */
27024 eassert (it->ascent >= 0 && it->descent >= 0);
27025 if (it->area == TEXT_AREA)
27026 it->current_x += it->pixel_width;
27028 if (extra_line_spacing > 0)
27030 it->descent += extra_line_spacing;
27031 if (extra_line_spacing > it->max_extra_line_spacing)
27032 it->max_extra_line_spacing = extra_line_spacing;
27035 it->max_ascent = max (it->max_ascent, it->ascent);
27036 it->max_descent = max (it->max_descent, it->descent);
27037 it->max_phys_ascent = max (it->max_phys_ascent, it->phys_ascent);
27038 it->max_phys_descent = max (it->max_phys_descent, it->phys_descent);
27041 /* EXPORT for RIF:
27042 Output LEN glyphs starting at START at the nominal cursor position.
27043 Advance the nominal cursor over the text. UPDATED_ROW is the glyph row
27044 being updated, and UPDATED_AREA is the area of that row being updated. */
27046 void
27047 x_write_glyphs (struct window *w, struct glyph_row *updated_row,
27048 struct glyph *start, enum glyph_row_area updated_area, int len)
27050 int x, hpos, chpos = w->phys_cursor.hpos;
27052 eassert (updated_row);
27053 /* When the window is hscrolled, cursor hpos can legitimately be out
27054 of bounds, but we draw the cursor at the corresponding window
27055 margin in that case. */
27056 if (!updated_row->reversed_p && chpos < 0)
27057 chpos = 0;
27058 if (updated_row->reversed_p && chpos >= updated_row->used[TEXT_AREA])
27059 chpos = updated_row->used[TEXT_AREA] - 1;
27061 block_input ();
27063 /* Write glyphs. */
27065 hpos = start - updated_row->glyphs[updated_area];
27066 x = draw_glyphs (w, w->output_cursor.x,
27067 updated_row, updated_area,
27068 hpos, hpos + len,
27069 DRAW_NORMAL_TEXT, 0);
27071 /* Invalidate old phys cursor if the glyph at its hpos is redrawn. */
27072 if (updated_area == TEXT_AREA
27073 && w->phys_cursor_on_p
27074 && w->phys_cursor.vpos == w->output_cursor.vpos
27075 && chpos >= hpos
27076 && chpos < hpos + len)
27077 w->phys_cursor_on_p = 0;
27079 unblock_input ();
27081 /* Advance the output cursor. */
27082 w->output_cursor.hpos += len;
27083 w->output_cursor.x = x;
27087 /* EXPORT for RIF:
27088 Insert LEN glyphs from START at the nominal cursor position. */
27090 void
27091 x_insert_glyphs (struct window *w, struct glyph_row *updated_row,
27092 struct glyph *start, enum glyph_row_area updated_area, int len)
27094 struct frame *f;
27095 int line_height, shift_by_width, shifted_region_width;
27096 struct glyph_row *row;
27097 struct glyph *glyph;
27098 int frame_x, frame_y;
27099 ptrdiff_t hpos;
27101 eassert (updated_row);
27102 block_input ();
27103 f = XFRAME (WINDOW_FRAME (w));
27105 /* Get the height of the line we are in. */
27106 row = updated_row;
27107 line_height = row->height;
27109 /* Get the width of the glyphs to insert. */
27110 shift_by_width = 0;
27111 for (glyph = start; glyph < start + len; ++glyph)
27112 shift_by_width += glyph->pixel_width;
27114 /* Get the width of the region to shift right. */
27115 shifted_region_width = (window_box_width (w, updated_area)
27116 - w->output_cursor.x
27117 - shift_by_width);
27119 /* Shift right. */
27120 frame_x = window_box_left (w, updated_area) + w->output_cursor.x;
27121 frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, w->output_cursor.y);
27123 FRAME_RIF (f)->shift_glyphs_for_insert (f, frame_x, frame_y, shifted_region_width,
27124 line_height, shift_by_width);
27126 /* Write the glyphs. */
27127 hpos = start - row->glyphs[updated_area];
27128 draw_glyphs (w, w->output_cursor.x, row, updated_area,
27129 hpos, hpos + len,
27130 DRAW_NORMAL_TEXT, 0);
27132 /* Advance the output cursor. */
27133 w->output_cursor.hpos += len;
27134 w->output_cursor.x += shift_by_width;
27135 unblock_input ();
27139 /* EXPORT for RIF:
27140 Erase the current text line from the nominal cursor position
27141 (inclusive) to pixel column TO_X (exclusive). The idea is that
27142 everything from TO_X onward is already erased.
27144 TO_X is a pixel position relative to UPDATED_AREA of currently
27145 updated window W. TO_X == -1 means clear to the end of this area. */
27147 void
27148 x_clear_end_of_line (struct window *w, struct glyph_row *updated_row,
27149 enum glyph_row_area updated_area, int to_x)
27151 struct frame *f;
27152 int max_x, min_y, max_y;
27153 int from_x, from_y, to_y;
27155 eassert (updated_row);
27156 f = XFRAME (w->frame);
27158 if (updated_row->full_width_p)
27159 max_x = (WINDOW_PIXEL_WIDTH (w)
27160 - (updated_row->mode_line_p ? WINDOW_RIGHT_DIVIDER_WIDTH (w) : 0));
27161 else
27162 max_x = window_box_width (w, updated_area);
27163 max_y = window_text_bottom_y (w);
27165 /* TO_X == 0 means don't do anything. TO_X < 0 means clear to end
27166 of window. For TO_X > 0, truncate to end of drawing area. */
27167 if (to_x == 0)
27168 return;
27169 else if (to_x < 0)
27170 to_x = max_x;
27171 else
27172 to_x = min (to_x, max_x);
27174 to_y = min (max_y, w->output_cursor.y + updated_row->height);
27176 /* Notice if the cursor will be cleared by this operation. */
27177 if (!updated_row->full_width_p)
27178 notice_overwritten_cursor (w, updated_area,
27179 w->output_cursor.x, -1,
27180 updated_row->y,
27181 MATRIX_ROW_BOTTOM_Y (updated_row));
27183 from_x = w->output_cursor.x;
27185 /* Translate to frame coordinates. */
27186 if (updated_row->full_width_p)
27188 from_x = WINDOW_TO_FRAME_PIXEL_X (w, from_x);
27189 to_x = WINDOW_TO_FRAME_PIXEL_X (w, to_x);
27191 else
27193 int area_left = window_box_left (w, updated_area);
27194 from_x += area_left;
27195 to_x += area_left;
27198 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
27199 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, max (min_y, w->output_cursor.y));
27200 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, to_y);
27202 /* Prevent inadvertently clearing to end of the X window. */
27203 if (to_x > from_x && to_y > from_y)
27205 block_input ();
27206 FRAME_RIF (f)->clear_frame_area (f, from_x, from_y,
27207 to_x - from_x, to_y - from_y);
27208 unblock_input ();
27212 #endif /* HAVE_WINDOW_SYSTEM */
27216 /***********************************************************************
27217 Cursor types
27218 ***********************************************************************/
27220 /* Value is the internal representation of the specified cursor type
27221 ARG. If type is BAR_CURSOR, return in *WIDTH the specified width
27222 of the bar cursor. */
27224 static enum text_cursor_kinds
27225 get_specified_cursor_type (Lisp_Object arg, int *width)
27227 enum text_cursor_kinds type;
27229 if (NILP (arg))
27230 return NO_CURSOR;
27232 if (EQ (arg, Qbox))
27233 return FILLED_BOX_CURSOR;
27235 if (EQ (arg, Qhollow))
27236 return HOLLOW_BOX_CURSOR;
27238 if (EQ (arg, Qbar))
27240 *width = 2;
27241 return BAR_CURSOR;
27244 if (CONSP (arg)
27245 && EQ (XCAR (arg), Qbar)
27246 && RANGED_INTEGERP (0, XCDR (arg), INT_MAX))
27248 *width = XINT (XCDR (arg));
27249 return BAR_CURSOR;
27252 if (EQ (arg, Qhbar))
27254 *width = 2;
27255 return HBAR_CURSOR;
27258 if (CONSP (arg)
27259 && EQ (XCAR (arg), Qhbar)
27260 && RANGED_INTEGERP (0, XCDR (arg), INT_MAX))
27262 *width = XINT (XCDR (arg));
27263 return HBAR_CURSOR;
27266 /* Treat anything unknown as "hollow box cursor".
27267 It was bad to signal an error; people have trouble fixing
27268 .Xdefaults with Emacs, when it has something bad in it. */
27269 type = HOLLOW_BOX_CURSOR;
27271 return type;
27274 /* Set the default cursor types for specified frame. */
27275 void
27276 set_frame_cursor_types (struct frame *f, Lisp_Object arg)
27278 int width = 1;
27279 Lisp_Object tem;
27281 FRAME_DESIRED_CURSOR (f) = get_specified_cursor_type (arg, &width);
27282 FRAME_CURSOR_WIDTH (f) = width;
27284 /* By default, set up the blink-off state depending on the on-state. */
27286 tem = Fassoc (arg, Vblink_cursor_alist);
27287 if (!NILP (tem))
27289 FRAME_BLINK_OFF_CURSOR (f)
27290 = get_specified_cursor_type (XCDR (tem), &width);
27291 FRAME_BLINK_OFF_CURSOR_WIDTH (f) = width;
27293 else
27294 FRAME_BLINK_OFF_CURSOR (f) = DEFAULT_CURSOR;
27296 /* Make sure the cursor gets redrawn. */
27297 f->cursor_type_changed = 1;
27301 #ifdef HAVE_WINDOW_SYSTEM
27303 /* Return the cursor we want to be displayed in window W. Return
27304 width of bar/hbar cursor through WIDTH arg. Return with
27305 ACTIVE_CURSOR arg set to 1 if cursor in window W is `active'
27306 (i.e. if the `system caret' should track this cursor).
27308 In a mini-buffer window, we want the cursor only to appear if we
27309 are reading input from this window. For the selected window, we
27310 want the cursor type given by the frame parameter or buffer local
27311 setting of cursor-type. If explicitly marked off, draw no cursor.
27312 In all other cases, we want a hollow box cursor. */
27314 static enum text_cursor_kinds
27315 get_window_cursor_type (struct window *w, struct glyph *glyph, int *width,
27316 int *active_cursor)
27318 struct frame *f = XFRAME (w->frame);
27319 struct buffer *b = XBUFFER (w->contents);
27320 int cursor_type = DEFAULT_CURSOR;
27321 Lisp_Object alt_cursor;
27322 int non_selected = 0;
27324 *active_cursor = 1;
27326 /* Echo area */
27327 if (cursor_in_echo_area
27328 && FRAME_HAS_MINIBUF_P (f)
27329 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
27331 if (w == XWINDOW (echo_area_window))
27333 if (EQ (BVAR (b, cursor_type), Qt) || NILP (BVAR (b, cursor_type)))
27335 *width = FRAME_CURSOR_WIDTH (f);
27336 return FRAME_DESIRED_CURSOR (f);
27338 else
27339 return get_specified_cursor_type (BVAR (b, cursor_type), width);
27342 *active_cursor = 0;
27343 non_selected = 1;
27346 /* Detect a nonselected window or nonselected frame. */
27347 else if (w != XWINDOW (f->selected_window)
27348 || f != FRAME_DISPLAY_INFO (f)->x_highlight_frame)
27350 *active_cursor = 0;
27352 if (MINI_WINDOW_P (w) && minibuf_level == 0)
27353 return NO_CURSOR;
27355 non_selected = 1;
27358 /* Never display a cursor in a window in which cursor-type is nil. */
27359 if (NILP (BVAR (b, cursor_type)))
27360 return NO_CURSOR;
27362 /* Get the normal cursor type for this window. */
27363 if (EQ (BVAR (b, cursor_type), Qt))
27365 cursor_type = FRAME_DESIRED_CURSOR (f);
27366 *width = FRAME_CURSOR_WIDTH (f);
27368 else
27369 cursor_type = get_specified_cursor_type (BVAR (b, cursor_type), width);
27371 /* Use cursor-in-non-selected-windows instead
27372 for non-selected window or frame. */
27373 if (non_selected)
27375 alt_cursor = BVAR (b, cursor_in_non_selected_windows);
27376 if (!EQ (Qt, alt_cursor))
27377 return get_specified_cursor_type (alt_cursor, width);
27378 /* t means modify the normal cursor type. */
27379 if (cursor_type == FILLED_BOX_CURSOR)
27380 cursor_type = HOLLOW_BOX_CURSOR;
27381 else if (cursor_type == BAR_CURSOR && *width > 1)
27382 --*width;
27383 return cursor_type;
27386 /* Use normal cursor if not blinked off. */
27387 if (!w->cursor_off_p)
27389 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
27391 if (cursor_type == FILLED_BOX_CURSOR)
27393 /* Using a block cursor on large images can be very annoying.
27394 So use a hollow cursor for "large" images.
27395 If image is not transparent (no mask), also use hollow cursor. */
27396 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
27397 if (img != NULL && IMAGEP (img->spec))
27399 /* Arbitrarily, interpret "Large" as >32x32 and >NxN
27400 where N = size of default frame font size.
27401 This should cover most of the "tiny" icons people may use. */
27402 if (!img->mask
27403 || img->width > max (32, WINDOW_FRAME_COLUMN_WIDTH (w))
27404 || img->height > max (32, WINDOW_FRAME_LINE_HEIGHT (w)))
27405 cursor_type = HOLLOW_BOX_CURSOR;
27408 else if (cursor_type != NO_CURSOR)
27410 /* Display current only supports BOX and HOLLOW cursors for images.
27411 So for now, unconditionally use a HOLLOW cursor when cursor is
27412 not a solid box cursor. */
27413 cursor_type = HOLLOW_BOX_CURSOR;
27416 return cursor_type;
27419 /* Cursor is blinked off, so determine how to "toggle" it. */
27421 /* First look for an entry matching the buffer's cursor-type in blink-cursor-alist. */
27422 if ((alt_cursor = Fassoc (BVAR (b, cursor_type), Vblink_cursor_alist), !NILP (alt_cursor)))
27423 return get_specified_cursor_type (XCDR (alt_cursor), width);
27425 /* Then see if frame has specified a specific blink off cursor type. */
27426 if (FRAME_BLINK_OFF_CURSOR (f) != DEFAULT_CURSOR)
27428 *width = FRAME_BLINK_OFF_CURSOR_WIDTH (f);
27429 return FRAME_BLINK_OFF_CURSOR (f);
27432 #if 0
27433 /* Some people liked having a permanently visible blinking cursor,
27434 while others had very strong opinions against it. So it was
27435 decided to remove it. KFS 2003-09-03 */
27437 /* Finally perform built-in cursor blinking:
27438 filled box <-> hollow box
27439 wide [h]bar <-> narrow [h]bar
27440 narrow [h]bar <-> no cursor
27441 other type <-> no cursor */
27443 if (cursor_type == FILLED_BOX_CURSOR)
27444 return HOLLOW_BOX_CURSOR;
27446 if ((cursor_type == BAR_CURSOR || cursor_type == HBAR_CURSOR) && *width > 1)
27448 *width = 1;
27449 return cursor_type;
27451 #endif
27453 return NO_CURSOR;
27457 /* Notice when the text cursor of window W has been completely
27458 overwritten by a drawing operation that outputs glyphs in AREA
27459 starting at X0 and ending at X1 in the line starting at Y0 and
27460 ending at Y1. X coordinates are area-relative. X1 < 0 means all
27461 the rest of the line after X0 has been written. Y coordinates
27462 are window-relative. */
27464 static void
27465 notice_overwritten_cursor (struct window *w, enum glyph_row_area area,
27466 int x0, int x1, int y0, int y1)
27468 int cx0, cx1, cy0, cy1;
27469 struct glyph_row *row;
27471 if (!w->phys_cursor_on_p)
27472 return;
27473 if (area != TEXT_AREA)
27474 return;
27476 if (w->phys_cursor.vpos < 0
27477 || w->phys_cursor.vpos >= w->current_matrix->nrows
27478 || (row = w->current_matrix->rows + w->phys_cursor.vpos,
27479 !(row->enabled_p && MATRIX_ROW_DISPLAYS_TEXT_P (row))))
27480 return;
27482 if (row->cursor_in_fringe_p)
27484 row->cursor_in_fringe_p = 0;
27485 draw_fringe_bitmap (w, row, row->reversed_p);
27486 w->phys_cursor_on_p = 0;
27487 return;
27490 cx0 = w->phys_cursor.x;
27491 cx1 = cx0 + w->phys_cursor_width;
27492 if (x0 > cx0 || (x1 >= 0 && x1 < cx1))
27493 return;
27495 /* The cursor image will be completely removed from the
27496 screen if the output area intersects the cursor area in
27497 y-direction. When we draw in [y0 y1[, and some part of
27498 the cursor is at y < y0, that part must have been drawn
27499 before. When scrolling, the cursor is erased before
27500 actually scrolling, so we don't come here. When not
27501 scrolling, the rows above the old cursor row must have
27502 changed, and in this case these rows must have written
27503 over the cursor image.
27505 Likewise if part of the cursor is below y1, with the
27506 exception of the cursor being in the first blank row at
27507 the buffer and window end because update_text_area
27508 doesn't draw that row. (Except when it does, but
27509 that's handled in update_text_area.) */
27511 cy0 = w->phys_cursor.y;
27512 cy1 = cy0 + w->phys_cursor_height;
27513 if ((y0 < cy0 || y0 >= cy1) && (y1 <= cy0 || y1 >= cy1))
27514 return;
27516 w->phys_cursor_on_p = 0;
27519 #endif /* HAVE_WINDOW_SYSTEM */
27522 /************************************************************************
27523 Mouse Face
27524 ************************************************************************/
27526 #ifdef HAVE_WINDOW_SYSTEM
27528 /* EXPORT for RIF:
27529 Fix the display of area AREA of overlapping row ROW in window W
27530 with respect to the overlapping part OVERLAPS. */
27532 void
27533 x_fix_overlapping_area (struct window *w, struct glyph_row *row,
27534 enum glyph_row_area area, int overlaps)
27536 int i, x;
27538 block_input ();
27540 x = 0;
27541 for (i = 0; i < row->used[area];)
27543 if (row->glyphs[area][i].overlaps_vertically_p)
27545 int start = i, start_x = x;
27549 x += row->glyphs[area][i].pixel_width;
27550 ++i;
27552 while (i < row->used[area]
27553 && row->glyphs[area][i].overlaps_vertically_p);
27555 draw_glyphs (w, start_x, row, area,
27556 start, i,
27557 DRAW_NORMAL_TEXT, overlaps);
27559 else
27561 x += row->glyphs[area][i].pixel_width;
27562 ++i;
27566 unblock_input ();
27570 /* EXPORT:
27571 Draw the cursor glyph of window W in glyph row ROW. See the
27572 comment of draw_glyphs for the meaning of HL. */
27574 void
27575 draw_phys_cursor_glyph (struct window *w, struct glyph_row *row,
27576 enum draw_glyphs_face hl)
27578 /* If cursor hpos is out of bounds, don't draw garbage. This can
27579 happen in mini-buffer windows when switching between echo area
27580 glyphs and mini-buffer. */
27581 if ((row->reversed_p
27582 ? (w->phys_cursor.hpos >= 0)
27583 : (w->phys_cursor.hpos < row->used[TEXT_AREA])))
27585 int on_p = w->phys_cursor_on_p;
27586 int x1;
27587 int hpos = w->phys_cursor.hpos;
27589 /* When the window is hscrolled, cursor hpos can legitimately be
27590 out of bounds, but we draw the cursor at the corresponding
27591 window margin in that case. */
27592 if (!row->reversed_p && hpos < 0)
27593 hpos = 0;
27594 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
27595 hpos = row->used[TEXT_AREA] - 1;
27597 x1 = draw_glyphs (w, w->phys_cursor.x, row, TEXT_AREA, hpos, hpos + 1,
27598 hl, 0);
27599 w->phys_cursor_on_p = on_p;
27601 if (hl == DRAW_CURSOR)
27602 w->phys_cursor_width = x1 - w->phys_cursor.x;
27603 /* When we erase the cursor, and ROW is overlapped by other
27604 rows, make sure that these overlapping parts of other rows
27605 are redrawn. */
27606 else if (hl == DRAW_NORMAL_TEXT && row->overlapped_p)
27608 w->phys_cursor_width = x1 - w->phys_cursor.x;
27610 if (row > w->current_matrix->rows
27611 && MATRIX_ROW_OVERLAPS_SUCC_P (row - 1))
27612 x_fix_overlapping_area (w, row - 1, TEXT_AREA,
27613 OVERLAPS_ERASED_CURSOR);
27615 if (MATRIX_ROW_BOTTOM_Y (row) < window_text_bottom_y (w)
27616 && MATRIX_ROW_OVERLAPS_PRED_P (row + 1))
27617 x_fix_overlapping_area (w, row + 1, TEXT_AREA,
27618 OVERLAPS_ERASED_CURSOR);
27624 /* Erase the image of a cursor of window W from the screen. */
27626 void
27627 erase_phys_cursor (struct window *w)
27629 struct frame *f = XFRAME (w->frame);
27630 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
27631 int hpos = w->phys_cursor.hpos;
27632 int vpos = w->phys_cursor.vpos;
27633 int mouse_face_here_p = 0;
27634 struct glyph_matrix *active_glyphs = w->current_matrix;
27635 struct glyph_row *cursor_row;
27636 struct glyph *cursor_glyph;
27637 enum draw_glyphs_face hl;
27639 /* No cursor displayed or row invalidated => nothing to do on the
27640 screen. */
27641 if (w->phys_cursor_type == NO_CURSOR)
27642 goto mark_cursor_off;
27644 /* VPOS >= active_glyphs->nrows means that window has been resized.
27645 Don't bother to erase the cursor. */
27646 if (vpos >= active_glyphs->nrows)
27647 goto mark_cursor_off;
27649 /* If row containing cursor is marked invalid, there is nothing we
27650 can do. */
27651 cursor_row = MATRIX_ROW (active_glyphs, vpos);
27652 if (!cursor_row->enabled_p)
27653 goto mark_cursor_off;
27655 /* If line spacing is > 0, old cursor may only be partially visible in
27656 window after split-window. So adjust visible height. */
27657 cursor_row->visible_height = min (cursor_row->visible_height,
27658 window_text_bottom_y (w) - cursor_row->y);
27660 /* If row is completely invisible, don't attempt to delete a cursor which
27661 isn't there. This can happen if cursor is at top of a window, and
27662 we switch to a buffer with a header line in that window. */
27663 if (cursor_row->visible_height <= 0)
27664 goto mark_cursor_off;
27666 /* If cursor is in the fringe, erase by drawing actual bitmap there. */
27667 if (cursor_row->cursor_in_fringe_p)
27669 cursor_row->cursor_in_fringe_p = 0;
27670 draw_fringe_bitmap (w, cursor_row, cursor_row->reversed_p);
27671 goto mark_cursor_off;
27674 /* This can happen when the new row is shorter than the old one.
27675 In this case, either draw_glyphs or clear_end_of_line
27676 should have cleared the cursor. Note that we wouldn't be
27677 able to erase the cursor in this case because we don't have a
27678 cursor glyph at hand. */
27679 if ((cursor_row->reversed_p
27680 ? (w->phys_cursor.hpos < 0)
27681 : (w->phys_cursor.hpos >= cursor_row->used[TEXT_AREA])))
27682 goto mark_cursor_off;
27684 /* When the window is hscrolled, cursor hpos can legitimately be out
27685 of bounds, but we draw the cursor at the corresponding window
27686 margin in that case. */
27687 if (!cursor_row->reversed_p && hpos < 0)
27688 hpos = 0;
27689 if (cursor_row->reversed_p && hpos >= cursor_row->used[TEXT_AREA])
27690 hpos = cursor_row->used[TEXT_AREA] - 1;
27692 /* If the cursor is in the mouse face area, redisplay that when
27693 we clear the cursor. */
27694 if (! NILP (hlinfo->mouse_face_window)
27695 && coords_in_mouse_face_p (w, hpos, vpos)
27696 /* Don't redraw the cursor's spot in mouse face if it is at the
27697 end of a line (on a newline). The cursor appears there, but
27698 mouse highlighting does not. */
27699 && cursor_row->used[TEXT_AREA] > hpos && hpos >= 0)
27700 mouse_face_here_p = 1;
27702 /* Maybe clear the display under the cursor. */
27703 if (w->phys_cursor_type == HOLLOW_BOX_CURSOR)
27705 int x, y;
27706 int header_line_height = WINDOW_HEADER_LINE_HEIGHT (w);
27707 int width;
27709 cursor_glyph = get_phys_cursor_glyph (w);
27710 if (cursor_glyph == NULL)
27711 goto mark_cursor_off;
27713 width = cursor_glyph->pixel_width;
27714 x = w->phys_cursor.x;
27715 if (x < 0)
27717 width += x;
27718 x = 0;
27720 width = min (width, window_box_width (w, TEXT_AREA) - x);
27721 y = WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height, cursor_row->y));
27722 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, x);
27724 if (width > 0)
27725 FRAME_RIF (f)->clear_frame_area (f, x, y, width, cursor_row->visible_height);
27728 /* Erase the cursor by redrawing the character underneath it. */
27729 if (mouse_face_here_p)
27730 hl = DRAW_MOUSE_FACE;
27731 else
27732 hl = DRAW_NORMAL_TEXT;
27733 draw_phys_cursor_glyph (w, cursor_row, hl);
27735 mark_cursor_off:
27736 w->phys_cursor_on_p = 0;
27737 w->phys_cursor_type = NO_CURSOR;
27741 /* EXPORT:
27742 Display or clear cursor of window W. If ON is zero, clear the
27743 cursor. If it is non-zero, display the cursor. If ON is nonzero,
27744 where to put the cursor is specified by HPOS, VPOS, X and Y. */
27746 void
27747 display_and_set_cursor (struct window *w, bool on,
27748 int hpos, int vpos, int x, int y)
27750 struct frame *f = XFRAME (w->frame);
27751 int new_cursor_type;
27752 int new_cursor_width;
27753 int active_cursor;
27754 struct glyph_row *glyph_row;
27755 struct glyph *glyph;
27757 /* This is pointless on invisible frames, and dangerous on garbaged
27758 windows and frames; in the latter case, the frame or window may
27759 be in the midst of changing its size, and x and y may be off the
27760 window. */
27761 if (! FRAME_VISIBLE_P (f)
27762 || FRAME_GARBAGED_P (f)
27763 || vpos >= w->current_matrix->nrows
27764 || hpos >= w->current_matrix->matrix_w)
27765 return;
27767 /* If cursor is off and we want it off, return quickly. */
27768 if (!on && !w->phys_cursor_on_p)
27769 return;
27771 glyph_row = MATRIX_ROW (w->current_matrix, vpos);
27772 /* If cursor row is not enabled, we don't really know where to
27773 display the cursor. */
27774 if (!glyph_row->enabled_p)
27776 w->phys_cursor_on_p = 0;
27777 return;
27780 glyph = NULL;
27781 if (!glyph_row->exact_window_width_line_p
27782 || (0 <= hpos && hpos < glyph_row->used[TEXT_AREA]))
27783 glyph = glyph_row->glyphs[TEXT_AREA] + hpos;
27785 eassert (input_blocked_p ());
27787 /* Set new_cursor_type to the cursor we want to be displayed. */
27788 new_cursor_type = get_window_cursor_type (w, glyph,
27789 &new_cursor_width, &active_cursor);
27791 /* If cursor is currently being shown and we don't want it to be or
27792 it is in the wrong place, or the cursor type is not what we want,
27793 erase it. */
27794 if (w->phys_cursor_on_p
27795 && (!on
27796 || w->phys_cursor.x != x
27797 || w->phys_cursor.y != y
27798 /* HPOS can be negative in R2L rows whose
27799 exact_window_width_line_p flag is set (i.e. their newline
27800 would "overflow into the fringe"). */
27801 || hpos < 0
27802 || new_cursor_type != w->phys_cursor_type
27803 || ((new_cursor_type == BAR_CURSOR || new_cursor_type == HBAR_CURSOR)
27804 && new_cursor_width != w->phys_cursor_width)))
27805 erase_phys_cursor (w);
27807 /* Don't check phys_cursor_on_p here because that flag is only set
27808 to zero in some cases where we know that the cursor has been
27809 completely erased, to avoid the extra work of erasing the cursor
27810 twice. In other words, phys_cursor_on_p can be 1 and the cursor
27811 still not be visible, or it has only been partly erased. */
27812 if (on)
27814 w->phys_cursor_ascent = glyph_row->ascent;
27815 w->phys_cursor_height = glyph_row->height;
27817 /* Set phys_cursor_.* before x_draw_.* is called because some
27818 of them may need the information. */
27819 w->phys_cursor.x = x;
27820 w->phys_cursor.y = glyph_row->y;
27821 w->phys_cursor.hpos = hpos;
27822 w->phys_cursor.vpos = vpos;
27825 FRAME_RIF (f)->draw_window_cursor (w, glyph_row, x, y,
27826 new_cursor_type, new_cursor_width,
27827 on, active_cursor);
27831 /* Switch the display of W's cursor on or off, according to the value
27832 of ON. */
27834 static void
27835 update_window_cursor (struct window *w, bool on)
27837 /* Don't update cursor in windows whose frame is in the process
27838 of being deleted. */
27839 if (w->current_matrix)
27841 int hpos = w->phys_cursor.hpos;
27842 int vpos = w->phys_cursor.vpos;
27843 struct glyph_row *row;
27845 if (vpos >= w->current_matrix->nrows
27846 || hpos >= w->current_matrix->matrix_w)
27847 return;
27849 row = MATRIX_ROW (w->current_matrix, vpos);
27851 /* When the window is hscrolled, cursor hpos can legitimately be
27852 out of bounds, but we draw the cursor at the corresponding
27853 window margin in that case. */
27854 if (!row->reversed_p && hpos < 0)
27855 hpos = 0;
27856 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
27857 hpos = row->used[TEXT_AREA] - 1;
27859 block_input ();
27860 display_and_set_cursor (w, on, hpos, vpos,
27861 w->phys_cursor.x, w->phys_cursor.y);
27862 unblock_input ();
27867 /* Call update_window_cursor with parameter ON_P on all leaf windows
27868 in the window tree rooted at W. */
27870 static void
27871 update_cursor_in_window_tree (struct window *w, bool on_p)
27873 while (w)
27875 if (WINDOWP (w->contents))
27876 update_cursor_in_window_tree (XWINDOW (w->contents), on_p);
27877 else
27878 update_window_cursor (w, on_p);
27880 w = NILP (w->next) ? 0 : XWINDOW (w->next);
27885 /* EXPORT:
27886 Display the cursor on window W, or clear it, according to ON_P.
27887 Don't change the cursor's position. */
27889 void
27890 x_update_cursor (struct frame *f, bool on_p)
27892 update_cursor_in_window_tree (XWINDOW (f->root_window), on_p);
27896 /* EXPORT:
27897 Clear the cursor of window W to background color, and mark the
27898 cursor as not shown. This is used when the text where the cursor
27899 is about to be rewritten. */
27901 void
27902 x_clear_cursor (struct window *w)
27904 if (FRAME_VISIBLE_P (XFRAME (w->frame)) && w->phys_cursor_on_p)
27905 update_window_cursor (w, 0);
27908 #endif /* HAVE_WINDOW_SYSTEM */
27910 /* Implementation of draw_row_with_mouse_face for GUI sessions, GPM,
27911 and MSDOS. */
27912 static void
27913 draw_row_with_mouse_face (struct window *w, int start_x, struct glyph_row *row,
27914 int start_hpos, int end_hpos,
27915 enum draw_glyphs_face draw)
27917 #ifdef HAVE_WINDOW_SYSTEM
27918 if (FRAME_WINDOW_P (XFRAME (w->frame)))
27920 draw_glyphs (w, start_x, row, TEXT_AREA, start_hpos, end_hpos, draw, 0);
27921 return;
27923 #endif
27924 #if defined (HAVE_GPM) || defined (MSDOS) || defined (WINDOWSNT)
27925 tty_draw_row_with_mouse_face (w, row, start_hpos, end_hpos, draw);
27926 #endif
27929 /* Display the active region described by mouse_face_* according to DRAW. */
27931 static void
27932 show_mouse_face (Mouse_HLInfo *hlinfo, enum draw_glyphs_face draw)
27934 struct window *w = XWINDOW (hlinfo->mouse_face_window);
27935 struct frame *f = XFRAME (WINDOW_FRAME (w));
27937 if (/* If window is in the process of being destroyed, don't bother
27938 to do anything. */
27939 w->current_matrix != NULL
27940 /* Don't update mouse highlight if hidden. */
27941 && (draw != DRAW_MOUSE_FACE || !hlinfo->mouse_face_hidden)
27942 /* Recognize when we are called to operate on rows that don't exist
27943 anymore. This can happen when a window is split. */
27944 && hlinfo->mouse_face_end_row < w->current_matrix->nrows)
27946 int phys_cursor_on_p = w->phys_cursor_on_p;
27947 struct glyph_row *row, *first, *last;
27949 first = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_beg_row);
27950 last = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_end_row);
27952 for (row = first; row <= last && row->enabled_p; ++row)
27954 int start_hpos, end_hpos, start_x;
27956 /* For all but the first row, the highlight starts at column 0. */
27957 if (row == first)
27959 /* R2L rows have BEG and END in reversed order, but the
27960 screen drawing geometry is always left to right. So
27961 we need to mirror the beginning and end of the
27962 highlighted area in R2L rows. */
27963 if (!row->reversed_p)
27965 start_hpos = hlinfo->mouse_face_beg_col;
27966 start_x = hlinfo->mouse_face_beg_x;
27968 else if (row == last)
27970 start_hpos = hlinfo->mouse_face_end_col;
27971 start_x = hlinfo->mouse_face_end_x;
27973 else
27975 start_hpos = 0;
27976 start_x = 0;
27979 else if (row->reversed_p && row == last)
27981 start_hpos = hlinfo->mouse_face_end_col;
27982 start_x = hlinfo->mouse_face_end_x;
27984 else
27986 start_hpos = 0;
27987 start_x = 0;
27990 if (row == last)
27992 if (!row->reversed_p)
27993 end_hpos = hlinfo->mouse_face_end_col;
27994 else if (row == first)
27995 end_hpos = hlinfo->mouse_face_beg_col;
27996 else
27998 end_hpos = row->used[TEXT_AREA];
27999 if (draw == DRAW_NORMAL_TEXT)
28000 row->fill_line_p = 1; /* Clear to end of line */
28003 else if (row->reversed_p && row == first)
28004 end_hpos = hlinfo->mouse_face_beg_col;
28005 else
28007 end_hpos = row->used[TEXT_AREA];
28008 if (draw == DRAW_NORMAL_TEXT)
28009 row->fill_line_p = 1; /* Clear to end of line */
28012 if (end_hpos > start_hpos)
28014 draw_row_with_mouse_face (w, start_x, row,
28015 start_hpos, end_hpos, draw);
28017 row->mouse_face_p
28018 = draw == DRAW_MOUSE_FACE || draw == DRAW_IMAGE_RAISED;
28022 #ifdef HAVE_WINDOW_SYSTEM
28023 /* When we've written over the cursor, arrange for it to
28024 be displayed again. */
28025 if (FRAME_WINDOW_P (f)
28026 && phys_cursor_on_p && !w->phys_cursor_on_p)
28028 int hpos = w->phys_cursor.hpos;
28030 /* When the window is hscrolled, cursor hpos can legitimately be
28031 out of bounds, but we draw the cursor at the corresponding
28032 window margin in that case. */
28033 if (!row->reversed_p && hpos < 0)
28034 hpos = 0;
28035 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
28036 hpos = row->used[TEXT_AREA] - 1;
28038 block_input ();
28039 display_and_set_cursor (w, 1, hpos, w->phys_cursor.vpos,
28040 w->phys_cursor.x, w->phys_cursor.y);
28041 unblock_input ();
28043 #endif /* HAVE_WINDOW_SYSTEM */
28046 #ifdef HAVE_WINDOW_SYSTEM
28047 /* Change the mouse cursor. */
28048 if (FRAME_WINDOW_P (f) && NILP (do_mouse_tracking))
28050 #if ! defined (USE_GTK) && ! defined (HAVE_NS)
28051 if (draw == DRAW_NORMAL_TEXT
28052 && !EQ (hlinfo->mouse_face_window, f->tool_bar_window))
28053 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->text_cursor);
28054 else
28055 #endif
28056 if (draw == DRAW_MOUSE_FACE)
28057 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->hand_cursor);
28058 else
28059 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->nontext_cursor);
28061 #endif /* HAVE_WINDOW_SYSTEM */
28064 /* EXPORT:
28065 Clear out the mouse-highlighted active region.
28066 Redraw it un-highlighted first. Value is non-zero if mouse
28067 face was actually drawn unhighlighted. */
28070 clear_mouse_face (Mouse_HLInfo *hlinfo)
28072 int cleared = 0;
28074 if (!hlinfo->mouse_face_hidden && !NILP (hlinfo->mouse_face_window))
28076 show_mouse_face (hlinfo, DRAW_NORMAL_TEXT);
28077 cleared = 1;
28080 hlinfo->mouse_face_beg_row = hlinfo->mouse_face_beg_col = -1;
28081 hlinfo->mouse_face_end_row = hlinfo->mouse_face_end_col = -1;
28082 hlinfo->mouse_face_window = Qnil;
28083 hlinfo->mouse_face_overlay = Qnil;
28084 return cleared;
28087 /* Return true if the coordinates HPOS and VPOS on windows W are
28088 within the mouse face on that window. */
28089 static bool
28090 coords_in_mouse_face_p (struct window *w, int hpos, int vpos)
28092 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (XFRAME (w->frame));
28094 /* Quickly resolve the easy cases. */
28095 if (!(WINDOWP (hlinfo->mouse_face_window)
28096 && XWINDOW (hlinfo->mouse_face_window) == w))
28097 return false;
28098 if (vpos < hlinfo->mouse_face_beg_row
28099 || vpos > hlinfo->mouse_face_end_row)
28100 return false;
28101 if (vpos > hlinfo->mouse_face_beg_row
28102 && vpos < hlinfo->mouse_face_end_row)
28103 return true;
28105 if (!MATRIX_ROW (w->current_matrix, vpos)->reversed_p)
28107 if (hlinfo->mouse_face_beg_row == hlinfo->mouse_face_end_row)
28109 if (hlinfo->mouse_face_beg_col <= hpos && hpos < hlinfo->mouse_face_end_col)
28110 return true;
28112 else if ((vpos == hlinfo->mouse_face_beg_row
28113 && hpos >= hlinfo->mouse_face_beg_col)
28114 || (vpos == hlinfo->mouse_face_end_row
28115 && hpos < hlinfo->mouse_face_end_col))
28116 return true;
28118 else
28120 if (hlinfo->mouse_face_beg_row == hlinfo->mouse_face_end_row)
28122 if (hlinfo->mouse_face_end_col < hpos && hpos <= hlinfo->mouse_face_beg_col)
28123 return true;
28125 else if ((vpos == hlinfo->mouse_face_beg_row
28126 && hpos <= hlinfo->mouse_face_beg_col)
28127 || (vpos == hlinfo->mouse_face_end_row
28128 && hpos > hlinfo->mouse_face_end_col))
28129 return true;
28131 return false;
28135 /* EXPORT:
28136 True if physical cursor of window W is within mouse face. */
28138 bool
28139 cursor_in_mouse_face_p (struct window *w)
28141 int hpos = w->phys_cursor.hpos;
28142 int vpos = w->phys_cursor.vpos;
28143 struct glyph_row *row = MATRIX_ROW (w->current_matrix, vpos);
28145 /* When the window is hscrolled, cursor hpos can legitimately be out
28146 of bounds, but we draw the cursor at the corresponding window
28147 margin in that case. */
28148 if (!row->reversed_p && hpos < 0)
28149 hpos = 0;
28150 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
28151 hpos = row->used[TEXT_AREA] - 1;
28153 return coords_in_mouse_face_p (w, hpos, vpos);
28158 /* Find the glyph rows START_ROW and END_ROW of window W that display
28159 characters between buffer positions START_CHARPOS and END_CHARPOS
28160 (excluding END_CHARPOS). DISP_STRING is a display string that
28161 covers these buffer positions. This is similar to
28162 row_containing_pos, but is more accurate when bidi reordering makes
28163 buffer positions change non-linearly with glyph rows. */
28164 static void
28165 rows_from_pos_range (struct window *w,
28166 ptrdiff_t start_charpos, ptrdiff_t end_charpos,
28167 Lisp_Object disp_string,
28168 struct glyph_row **start, struct glyph_row **end)
28170 struct glyph_row *first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
28171 int last_y = window_text_bottom_y (w);
28172 struct glyph_row *row;
28174 *start = NULL;
28175 *end = NULL;
28177 while (!first->enabled_p
28178 && first < MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w))
28179 first++;
28181 /* Find the START row. */
28182 for (row = first;
28183 row->enabled_p && MATRIX_ROW_BOTTOM_Y (row) <= last_y;
28184 row++)
28186 /* A row can potentially be the START row if the range of the
28187 characters it displays intersects the range
28188 [START_CHARPOS..END_CHARPOS). */
28189 if (! ((start_charpos < MATRIX_ROW_START_CHARPOS (row)
28190 && end_charpos < MATRIX_ROW_START_CHARPOS (row))
28191 /* See the commentary in row_containing_pos, for the
28192 explanation of the complicated way to check whether
28193 some position is beyond the end of the characters
28194 displayed by a row. */
28195 || ((start_charpos > MATRIX_ROW_END_CHARPOS (row)
28196 || (start_charpos == MATRIX_ROW_END_CHARPOS (row)
28197 && !row->ends_at_zv_p
28198 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
28199 && (end_charpos > MATRIX_ROW_END_CHARPOS (row)
28200 || (end_charpos == MATRIX_ROW_END_CHARPOS (row)
28201 && !row->ends_at_zv_p
28202 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))))))
28204 /* Found a candidate row. Now make sure at least one of the
28205 glyphs it displays has a charpos from the range
28206 [START_CHARPOS..END_CHARPOS).
28208 This is not obvious because bidi reordering could make
28209 buffer positions of a row be 1,2,3,102,101,100, and if we
28210 want to highlight characters in [50..60), we don't want
28211 this row, even though [50..60) does intersect [1..103),
28212 the range of character positions given by the row's start
28213 and end positions. */
28214 struct glyph *g = row->glyphs[TEXT_AREA];
28215 struct glyph *e = g + row->used[TEXT_AREA];
28217 while (g < e)
28219 if (((BUFFERP (g->object) || NILP (g->object))
28220 && start_charpos <= g->charpos && g->charpos < end_charpos)
28221 /* A glyph that comes from DISP_STRING is by
28222 definition to be highlighted. */
28223 || EQ (g->object, disp_string))
28224 *start = row;
28225 g++;
28227 if (*start)
28228 break;
28232 /* Find the END row. */
28233 if (!*start
28234 /* If the last row is partially visible, start looking for END
28235 from that row, instead of starting from FIRST. */
28236 && !(row->enabled_p
28237 && row->y < last_y && MATRIX_ROW_BOTTOM_Y (row) > last_y))
28238 row = first;
28239 for ( ; row->enabled_p && MATRIX_ROW_BOTTOM_Y (row) <= last_y; row++)
28241 struct glyph_row *next = row + 1;
28242 ptrdiff_t next_start = MATRIX_ROW_START_CHARPOS (next);
28244 if (!next->enabled_p
28245 || next >= MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w)
28246 /* The first row >= START whose range of displayed characters
28247 does NOT intersect the range [START_CHARPOS..END_CHARPOS]
28248 is the row END + 1. */
28249 || (start_charpos < next_start
28250 && end_charpos < next_start)
28251 || ((start_charpos > MATRIX_ROW_END_CHARPOS (next)
28252 || (start_charpos == MATRIX_ROW_END_CHARPOS (next)
28253 && !next->ends_at_zv_p
28254 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (next)))
28255 && (end_charpos > MATRIX_ROW_END_CHARPOS (next)
28256 || (end_charpos == MATRIX_ROW_END_CHARPOS (next)
28257 && !next->ends_at_zv_p
28258 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (next)))))
28260 *end = row;
28261 break;
28263 else
28265 /* If the next row's edges intersect [START_CHARPOS..END_CHARPOS],
28266 but none of the characters it displays are in the range, it is
28267 also END + 1. */
28268 struct glyph *g = next->glyphs[TEXT_AREA];
28269 struct glyph *s = g;
28270 struct glyph *e = g + next->used[TEXT_AREA];
28272 while (g < e)
28274 if (((BUFFERP (g->object) || NILP (g->object))
28275 && ((start_charpos <= g->charpos && g->charpos < end_charpos)
28276 /* If the buffer position of the first glyph in
28277 the row is equal to END_CHARPOS, it means
28278 the last character to be highlighted is the
28279 newline of ROW, and we must consider NEXT as
28280 END, not END+1. */
28281 || (((!next->reversed_p && g == s)
28282 || (next->reversed_p && g == e - 1))
28283 && (g->charpos == end_charpos
28284 /* Special case for when NEXT is an
28285 empty line at ZV. */
28286 || (g->charpos == -1
28287 && !row->ends_at_zv_p
28288 && next_start == end_charpos)))))
28289 /* A glyph that comes from DISP_STRING is by
28290 definition to be highlighted. */
28291 || EQ (g->object, disp_string))
28292 break;
28293 g++;
28295 if (g == e)
28297 *end = row;
28298 break;
28300 /* The first row that ends at ZV must be the last to be
28301 highlighted. */
28302 else if (next->ends_at_zv_p)
28304 *end = next;
28305 break;
28311 /* This function sets the mouse_face_* elements of HLINFO, assuming
28312 the mouse cursor is on a glyph with buffer charpos MOUSE_CHARPOS in
28313 window WINDOW. START_CHARPOS and END_CHARPOS are buffer positions
28314 for the overlay or run of text properties specifying the mouse
28315 face. BEFORE_STRING and AFTER_STRING, if non-nil, are a
28316 before-string and after-string that must also be highlighted.
28317 DISP_STRING, if non-nil, is a display string that may cover some
28318 or all of the highlighted text. */
28320 static void
28321 mouse_face_from_buffer_pos (Lisp_Object window,
28322 Mouse_HLInfo *hlinfo,
28323 ptrdiff_t mouse_charpos,
28324 ptrdiff_t start_charpos,
28325 ptrdiff_t end_charpos,
28326 Lisp_Object before_string,
28327 Lisp_Object after_string,
28328 Lisp_Object disp_string)
28330 struct window *w = XWINDOW (window);
28331 struct glyph_row *first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
28332 struct glyph_row *r1, *r2;
28333 struct glyph *glyph, *end;
28334 ptrdiff_t ignore, pos;
28335 int x;
28337 eassert (NILP (disp_string) || STRINGP (disp_string));
28338 eassert (NILP (before_string) || STRINGP (before_string));
28339 eassert (NILP (after_string) || STRINGP (after_string));
28341 /* Find the rows corresponding to START_CHARPOS and END_CHARPOS. */
28342 rows_from_pos_range (w, start_charpos, end_charpos, disp_string, &r1, &r2);
28343 if (r1 == NULL)
28344 r1 = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
28345 /* If the before-string or display-string contains newlines,
28346 rows_from_pos_range skips to its last row. Move back. */
28347 if (!NILP (before_string) || !NILP (disp_string))
28349 struct glyph_row *prev;
28350 while ((prev = r1 - 1, prev >= first)
28351 && MATRIX_ROW_END_CHARPOS (prev) == start_charpos
28352 && prev->used[TEXT_AREA] > 0)
28354 struct glyph *beg = prev->glyphs[TEXT_AREA];
28355 glyph = beg + prev->used[TEXT_AREA];
28356 while (--glyph >= beg && NILP (glyph->object));
28357 if (glyph < beg
28358 || !(EQ (glyph->object, before_string)
28359 || EQ (glyph->object, disp_string)))
28360 break;
28361 r1 = prev;
28364 if (r2 == NULL)
28366 r2 = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
28367 hlinfo->mouse_face_past_end = 1;
28369 else if (!NILP (after_string))
28371 /* If the after-string has newlines, advance to its last row. */
28372 struct glyph_row *next;
28373 struct glyph_row *last
28374 = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
28376 for (next = r2 + 1;
28377 next <= last
28378 && next->used[TEXT_AREA] > 0
28379 && EQ (next->glyphs[TEXT_AREA]->object, after_string);
28380 ++next)
28381 r2 = next;
28383 /* The rest of the display engine assumes that mouse_face_beg_row is
28384 either above mouse_face_end_row or identical to it. But with
28385 bidi-reordered continued lines, the row for START_CHARPOS could
28386 be below the row for END_CHARPOS. If so, swap the rows and store
28387 them in correct order. */
28388 if (r1->y > r2->y)
28390 struct glyph_row *tem = r2;
28392 r2 = r1;
28393 r1 = tem;
28396 hlinfo->mouse_face_beg_row = MATRIX_ROW_VPOS (r1, w->current_matrix);
28397 hlinfo->mouse_face_end_row = MATRIX_ROW_VPOS (r2, w->current_matrix);
28399 /* For a bidi-reordered row, the positions of BEFORE_STRING,
28400 AFTER_STRING, DISP_STRING, START_CHARPOS, and END_CHARPOS
28401 could be anywhere in the row and in any order. The strategy
28402 below is to find the leftmost and the rightmost glyph that
28403 belongs to either of these 3 strings, or whose position is
28404 between START_CHARPOS and END_CHARPOS, and highlight all the
28405 glyphs between those two. This may cover more than just the text
28406 between START_CHARPOS and END_CHARPOS if the range of characters
28407 strides the bidi level boundary, e.g. if the beginning is in R2L
28408 text while the end is in L2R text or vice versa. */
28409 if (!r1->reversed_p)
28411 /* This row is in a left to right paragraph. Scan it left to
28412 right. */
28413 glyph = r1->glyphs[TEXT_AREA];
28414 end = glyph + r1->used[TEXT_AREA];
28415 x = r1->x;
28417 /* Skip truncation glyphs at the start of the glyph row. */
28418 if (MATRIX_ROW_DISPLAYS_TEXT_P (r1))
28419 for (; glyph < end
28420 && NILP (glyph->object)
28421 && glyph->charpos < 0;
28422 ++glyph)
28423 x += glyph->pixel_width;
28425 /* Scan the glyph row, looking for BEFORE_STRING, AFTER_STRING,
28426 or DISP_STRING, and the first glyph from buffer whose
28427 position is between START_CHARPOS and END_CHARPOS. */
28428 for (; glyph < end
28429 && !NILP (glyph->object)
28430 && !EQ (glyph->object, disp_string)
28431 && !(BUFFERP (glyph->object)
28432 && (glyph->charpos >= start_charpos
28433 && glyph->charpos < end_charpos));
28434 ++glyph)
28436 /* BEFORE_STRING or AFTER_STRING are only relevant if they
28437 are present at buffer positions between START_CHARPOS and
28438 END_CHARPOS, or if they come from an overlay. */
28439 if (EQ (glyph->object, before_string))
28441 pos = string_buffer_position (before_string,
28442 start_charpos);
28443 /* If pos == 0, it means before_string came from an
28444 overlay, not from a buffer position. */
28445 if (!pos || (pos >= start_charpos && pos < end_charpos))
28446 break;
28448 else if (EQ (glyph->object, after_string))
28450 pos = string_buffer_position (after_string, end_charpos);
28451 if (!pos || (pos >= start_charpos && pos < end_charpos))
28452 break;
28454 x += glyph->pixel_width;
28456 hlinfo->mouse_face_beg_x = x;
28457 hlinfo->mouse_face_beg_col = glyph - r1->glyphs[TEXT_AREA];
28459 else
28461 /* This row is in a right to left paragraph. Scan it right to
28462 left. */
28463 struct glyph *g;
28465 end = r1->glyphs[TEXT_AREA] - 1;
28466 glyph = end + r1->used[TEXT_AREA];
28468 /* Skip truncation glyphs at the start of the glyph row. */
28469 if (MATRIX_ROW_DISPLAYS_TEXT_P (r1))
28470 for (; glyph > end
28471 && NILP (glyph->object)
28472 && glyph->charpos < 0;
28473 --glyph)
28476 /* Scan the glyph row, looking for BEFORE_STRING, AFTER_STRING,
28477 or DISP_STRING, and the first glyph from buffer whose
28478 position is between START_CHARPOS and END_CHARPOS. */
28479 for (; glyph > end
28480 && !NILP (glyph->object)
28481 && !EQ (glyph->object, disp_string)
28482 && !(BUFFERP (glyph->object)
28483 && (glyph->charpos >= start_charpos
28484 && glyph->charpos < end_charpos));
28485 --glyph)
28487 /* BEFORE_STRING or AFTER_STRING are only relevant if they
28488 are present at buffer positions between START_CHARPOS and
28489 END_CHARPOS, or if they come from an overlay. */
28490 if (EQ (glyph->object, before_string))
28492 pos = string_buffer_position (before_string, start_charpos);
28493 /* If pos == 0, it means before_string came from an
28494 overlay, not from a buffer position. */
28495 if (!pos || (pos >= start_charpos && pos < end_charpos))
28496 break;
28498 else if (EQ (glyph->object, after_string))
28500 pos = string_buffer_position (after_string, end_charpos);
28501 if (!pos || (pos >= start_charpos && pos < end_charpos))
28502 break;
28506 glyph++; /* first glyph to the right of the highlighted area */
28507 for (g = r1->glyphs[TEXT_AREA], x = r1->x; g < glyph; g++)
28508 x += g->pixel_width;
28509 hlinfo->mouse_face_beg_x = x;
28510 hlinfo->mouse_face_beg_col = glyph - r1->glyphs[TEXT_AREA];
28513 /* If the highlight ends in a different row, compute GLYPH and END
28514 for the end row. Otherwise, reuse the values computed above for
28515 the row where the highlight begins. */
28516 if (r2 != r1)
28518 if (!r2->reversed_p)
28520 glyph = r2->glyphs[TEXT_AREA];
28521 end = glyph + r2->used[TEXT_AREA];
28522 x = r2->x;
28524 else
28526 end = r2->glyphs[TEXT_AREA] - 1;
28527 glyph = end + r2->used[TEXT_AREA];
28531 if (!r2->reversed_p)
28533 /* Skip truncation and continuation glyphs near the end of the
28534 row, and also blanks and stretch glyphs inserted by
28535 extend_face_to_end_of_line. */
28536 while (end > glyph
28537 && NILP ((end - 1)->object))
28538 --end;
28539 /* Scan the rest of the glyph row from the end, looking for the
28540 first glyph that comes from BEFORE_STRING, AFTER_STRING, or
28541 DISP_STRING, or whose position is between START_CHARPOS
28542 and END_CHARPOS */
28543 for (--end;
28544 end > glyph
28545 && !NILP (end->object)
28546 && !EQ (end->object, disp_string)
28547 && !(BUFFERP (end->object)
28548 && (end->charpos >= start_charpos
28549 && end->charpos < end_charpos));
28550 --end)
28552 /* BEFORE_STRING or AFTER_STRING are only relevant if they
28553 are present at buffer positions between START_CHARPOS and
28554 END_CHARPOS, or if they come from an overlay. */
28555 if (EQ (end->object, before_string))
28557 pos = string_buffer_position (before_string, start_charpos);
28558 if (!pos || (pos >= start_charpos && pos < end_charpos))
28559 break;
28561 else if (EQ (end->object, after_string))
28563 pos = string_buffer_position (after_string, end_charpos);
28564 if (!pos || (pos >= start_charpos && pos < end_charpos))
28565 break;
28568 /* Find the X coordinate of the last glyph to be highlighted. */
28569 for (; glyph <= end; ++glyph)
28570 x += glyph->pixel_width;
28572 hlinfo->mouse_face_end_x = x;
28573 hlinfo->mouse_face_end_col = glyph - r2->glyphs[TEXT_AREA];
28575 else
28577 /* Skip truncation and continuation glyphs near the end of the
28578 row, and also blanks and stretch glyphs inserted by
28579 extend_face_to_end_of_line. */
28580 x = r2->x;
28581 end++;
28582 while (end < glyph
28583 && NILP (end->object))
28585 x += end->pixel_width;
28586 ++end;
28588 /* Scan the rest of the glyph row from the end, looking for the
28589 first glyph that comes from BEFORE_STRING, AFTER_STRING, or
28590 DISP_STRING, or whose position is between START_CHARPOS
28591 and END_CHARPOS */
28592 for ( ;
28593 end < glyph
28594 && !NILP (end->object)
28595 && !EQ (end->object, disp_string)
28596 && !(BUFFERP (end->object)
28597 && (end->charpos >= start_charpos
28598 && end->charpos < end_charpos));
28599 ++end)
28601 /* BEFORE_STRING or AFTER_STRING are only relevant if they
28602 are present at buffer positions between START_CHARPOS and
28603 END_CHARPOS, or if they come from an overlay. */
28604 if (EQ (end->object, before_string))
28606 pos = string_buffer_position (before_string, start_charpos);
28607 if (!pos || (pos >= start_charpos && pos < end_charpos))
28608 break;
28610 else if (EQ (end->object, after_string))
28612 pos = string_buffer_position (after_string, end_charpos);
28613 if (!pos || (pos >= start_charpos && pos < end_charpos))
28614 break;
28616 x += end->pixel_width;
28618 /* If we exited the above loop because we arrived at the last
28619 glyph of the row, and its buffer position is still not in
28620 range, it means the last character in range is the preceding
28621 newline. Bump the end column and x values to get past the
28622 last glyph. */
28623 if (end == glyph
28624 && BUFFERP (end->object)
28625 && (end->charpos < start_charpos
28626 || end->charpos >= end_charpos))
28628 x += end->pixel_width;
28629 ++end;
28631 hlinfo->mouse_face_end_x = x;
28632 hlinfo->mouse_face_end_col = end - r2->glyphs[TEXT_AREA];
28635 hlinfo->mouse_face_window = window;
28636 hlinfo->mouse_face_face_id
28637 = face_at_buffer_position (w, mouse_charpos, &ignore,
28638 mouse_charpos + 1,
28639 !hlinfo->mouse_face_hidden, -1);
28640 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
28643 /* The following function is not used anymore (replaced with
28644 mouse_face_from_string_pos), but I leave it here for the time
28645 being, in case someone would. */
28647 #if 0 /* not used */
28649 /* Find the position of the glyph for position POS in OBJECT in
28650 window W's current matrix, and return in *X, *Y the pixel
28651 coordinates, and return in *HPOS, *VPOS the column/row of the glyph.
28653 RIGHT_P non-zero means return the position of the right edge of the
28654 glyph, RIGHT_P zero means return the left edge position.
28656 If no glyph for POS exists in the matrix, return the position of
28657 the glyph with the next smaller position that is in the matrix, if
28658 RIGHT_P is zero. If RIGHT_P is non-zero, and no glyph for POS
28659 exists in the matrix, return the position of the glyph with the
28660 next larger position in OBJECT.
28662 Value is non-zero if a glyph was found. */
28664 static int
28665 fast_find_string_pos (struct window *w, ptrdiff_t pos, Lisp_Object object,
28666 int *hpos, int *vpos, int *x, int *y, int right_p)
28668 int yb = window_text_bottom_y (w);
28669 struct glyph_row *r;
28670 struct glyph *best_glyph = NULL;
28671 struct glyph_row *best_row = NULL;
28672 int best_x = 0;
28674 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
28675 r->enabled_p && r->y < yb;
28676 ++r)
28678 struct glyph *g = r->glyphs[TEXT_AREA];
28679 struct glyph *e = g + r->used[TEXT_AREA];
28680 int gx;
28682 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
28683 if (EQ (g->object, object))
28685 if (g->charpos == pos)
28687 best_glyph = g;
28688 best_x = gx;
28689 best_row = r;
28690 goto found;
28692 else if (best_glyph == NULL
28693 || ((eabs (g->charpos - pos)
28694 < eabs (best_glyph->charpos - pos))
28695 && (right_p
28696 ? g->charpos < pos
28697 : g->charpos > pos)))
28699 best_glyph = g;
28700 best_x = gx;
28701 best_row = r;
28706 found:
28708 if (best_glyph)
28710 *x = best_x;
28711 *hpos = best_glyph - best_row->glyphs[TEXT_AREA];
28713 if (right_p)
28715 *x += best_glyph->pixel_width;
28716 ++*hpos;
28719 *y = best_row->y;
28720 *vpos = MATRIX_ROW_VPOS (best_row, w->current_matrix);
28723 return best_glyph != NULL;
28725 #endif /* not used */
28727 /* Find the positions of the first and the last glyphs in window W's
28728 current matrix that occlude positions [STARTPOS..ENDPOS) in OBJECT
28729 (assumed to be a string), and return in HLINFO's mouse_face_*
28730 members the pixel and column/row coordinates of those glyphs. */
28732 static void
28733 mouse_face_from_string_pos (struct window *w, Mouse_HLInfo *hlinfo,
28734 Lisp_Object object,
28735 ptrdiff_t startpos, ptrdiff_t endpos)
28737 int yb = window_text_bottom_y (w);
28738 struct glyph_row *r;
28739 struct glyph *g, *e;
28740 int gx;
28741 int found = 0;
28743 /* Find the glyph row with at least one position in the range
28744 [STARTPOS..ENDPOS), and the first glyph in that row whose
28745 position belongs to that range. */
28746 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
28747 r->enabled_p && r->y < yb;
28748 ++r)
28750 if (!r->reversed_p)
28752 g = r->glyphs[TEXT_AREA];
28753 e = g + r->used[TEXT_AREA];
28754 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
28755 if (EQ (g->object, object)
28756 && startpos <= g->charpos && g->charpos < endpos)
28758 hlinfo->mouse_face_beg_row
28759 = MATRIX_ROW_VPOS (r, w->current_matrix);
28760 hlinfo->mouse_face_beg_col = g - r->glyphs[TEXT_AREA];
28761 hlinfo->mouse_face_beg_x = gx;
28762 found = 1;
28763 break;
28766 else
28768 struct glyph *g1;
28770 e = r->glyphs[TEXT_AREA];
28771 g = e + r->used[TEXT_AREA];
28772 for ( ; g > e; --g)
28773 if (EQ ((g-1)->object, object)
28774 && startpos <= (g-1)->charpos && (g-1)->charpos < endpos)
28776 hlinfo->mouse_face_beg_row
28777 = MATRIX_ROW_VPOS (r, w->current_matrix);
28778 hlinfo->mouse_face_beg_col = g - r->glyphs[TEXT_AREA];
28779 for (gx = r->x, g1 = r->glyphs[TEXT_AREA]; g1 < g; ++g1)
28780 gx += g1->pixel_width;
28781 hlinfo->mouse_face_beg_x = gx;
28782 found = 1;
28783 break;
28786 if (found)
28787 break;
28790 if (!found)
28791 return;
28793 /* Starting with the next row, look for the first row which does NOT
28794 include any glyphs whose positions are in the range. */
28795 for (++r; r->enabled_p && r->y < yb; ++r)
28797 g = r->glyphs[TEXT_AREA];
28798 e = g + r->used[TEXT_AREA];
28799 found = 0;
28800 for ( ; g < e; ++g)
28801 if (EQ (g->object, object)
28802 && startpos <= g->charpos && g->charpos < endpos)
28804 found = 1;
28805 break;
28807 if (!found)
28808 break;
28811 /* The highlighted region ends on the previous row. */
28812 r--;
28814 /* Set the end row. */
28815 hlinfo->mouse_face_end_row = MATRIX_ROW_VPOS (r, w->current_matrix);
28817 /* Compute and set the end column and the end column's horizontal
28818 pixel coordinate. */
28819 if (!r->reversed_p)
28821 g = r->glyphs[TEXT_AREA];
28822 e = g + r->used[TEXT_AREA];
28823 for ( ; e > g; --e)
28824 if (EQ ((e-1)->object, object)
28825 && startpos <= (e-1)->charpos && (e-1)->charpos < endpos)
28826 break;
28827 hlinfo->mouse_face_end_col = e - g;
28829 for (gx = r->x; g < e; ++g)
28830 gx += g->pixel_width;
28831 hlinfo->mouse_face_end_x = gx;
28833 else
28835 e = r->glyphs[TEXT_AREA];
28836 g = e + r->used[TEXT_AREA];
28837 for (gx = r->x ; e < g; ++e)
28839 if (EQ (e->object, object)
28840 && startpos <= e->charpos && e->charpos < endpos)
28841 break;
28842 gx += e->pixel_width;
28844 hlinfo->mouse_face_end_col = e - r->glyphs[TEXT_AREA];
28845 hlinfo->mouse_face_end_x = gx;
28849 #ifdef HAVE_WINDOW_SYSTEM
28851 /* See if position X, Y is within a hot-spot of an image. */
28853 static int
28854 on_hot_spot_p (Lisp_Object hot_spot, int x, int y)
28856 if (!CONSP (hot_spot))
28857 return 0;
28859 if (EQ (XCAR (hot_spot), Qrect))
28861 /* CDR is (Top-Left . Bottom-Right) = ((x0 . y0) . (x1 . y1)) */
28862 Lisp_Object rect = XCDR (hot_spot);
28863 Lisp_Object tem;
28864 if (!CONSP (rect))
28865 return 0;
28866 if (!CONSP (XCAR (rect)))
28867 return 0;
28868 if (!CONSP (XCDR (rect)))
28869 return 0;
28870 if (!(tem = XCAR (XCAR (rect)), INTEGERP (tem) && x >= XINT (tem)))
28871 return 0;
28872 if (!(tem = XCDR (XCAR (rect)), INTEGERP (tem) && y >= XINT (tem)))
28873 return 0;
28874 if (!(tem = XCAR (XCDR (rect)), INTEGERP (tem) && x <= XINT (tem)))
28875 return 0;
28876 if (!(tem = XCDR (XCDR (rect)), INTEGERP (tem) && y <= XINT (tem)))
28877 return 0;
28878 return 1;
28880 else if (EQ (XCAR (hot_spot), Qcircle))
28882 /* CDR is (Center . Radius) = ((x0 . y0) . r) */
28883 Lisp_Object circ = XCDR (hot_spot);
28884 Lisp_Object lr, lx0, ly0;
28885 if (CONSP (circ)
28886 && CONSP (XCAR (circ))
28887 && (lr = XCDR (circ), INTEGERP (lr) || FLOATP (lr))
28888 && (lx0 = XCAR (XCAR (circ)), INTEGERP (lx0))
28889 && (ly0 = XCDR (XCAR (circ)), INTEGERP (ly0)))
28891 double r = XFLOATINT (lr);
28892 double dx = XINT (lx0) - x;
28893 double dy = XINT (ly0) - y;
28894 return (dx * dx + dy * dy <= r * r);
28897 else if (EQ (XCAR (hot_spot), Qpoly))
28899 /* CDR is [x0 y0 x1 y1 x2 y2 ...x(n-1) y(n-1)] */
28900 if (VECTORP (XCDR (hot_spot)))
28902 struct Lisp_Vector *v = XVECTOR (XCDR (hot_spot));
28903 Lisp_Object *poly = v->contents;
28904 ptrdiff_t n = v->header.size;
28905 ptrdiff_t i;
28906 int inside = 0;
28907 Lisp_Object lx, ly;
28908 int x0, y0;
28910 /* Need an even number of coordinates, and at least 3 edges. */
28911 if (n < 6 || n & 1)
28912 return 0;
28914 /* Count edge segments intersecting line from (X,Y) to (X,infinity).
28915 If count is odd, we are inside polygon. Pixels on edges
28916 may or may not be included depending on actual geometry of the
28917 polygon. */
28918 if ((lx = poly[n-2], !INTEGERP (lx))
28919 || (ly = poly[n-1], !INTEGERP (lx)))
28920 return 0;
28921 x0 = XINT (lx), y0 = XINT (ly);
28922 for (i = 0; i < n; i += 2)
28924 int x1 = x0, y1 = y0;
28925 if ((lx = poly[i], !INTEGERP (lx))
28926 || (ly = poly[i+1], !INTEGERP (ly)))
28927 return 0;
28928 x0 = XINT (lx), y0 = XINT (ly);
28930 /* Does this segment cross the X line? */
28931 if (x0 >= x)
28933 if (x1 >= x)
28934 continue;
28936 else if (x1 < x)
28937 continue;
28938 if (y > y0 && y > y1)
28939 continue;
28940 if (y < y0 + ((y1 - y0) * (x - x0)) / (x1 - x0))
28941 inside = !inside;
28943 return inside;
28946 return 0;
28949 Lisp_Object
28950 find_hot_spot (Lisp_Object map, int x, int y)
28952 while (CONSP (map))
28954 if (CONSP (XCAR (map))
28955 && on_hot_spot_p (XCAR (XCAR (map)), x, y))
28956 return XCAR (map);
28957 map = XCDR (map);
28960 return Qnil;
28963 DEFUN ("lookup-image-map", Flookup_image_map, Slookup_image_map,
28964 3, 3, 0,
28965 doc: /* Lookup in image map MAP coordinates X and Y.
28966 An image map is an alist where each element has the format (AREA ID PLIST).
28967 An AREA is specified as either a rectangle, a circle, or a polygon:
28968 A rectangle is a cons (rect . ((x0 . y0) . (x1 . y1))) specifying the
28969 pixel coordinates of the upper left and bottom right corners.
28970 A circle is a cons (circle . ((x0 . y0) . r)) specifying the center
28971 and the radius of the circle; r may be a float or integer.
28972 A polygon is a cons (poly . [x0 y0 x1 y1 ...]) where each pair in the
28973 vector describes one corner in the polygon.
28974 Returns the alist element for the first matching AREA in MAP. */)
28975 (Lisp_Object map, Lisp_Object x, Lisp_Object y)
28977 if (NILP (map))
28978 return Qnil;
28980 CHECK_NUMBER (x);
28981 CHECK_NUMBER (y);
28983 return find_hot_spot (map,
28984 clip_to_bounds (INT_MIN, XINT (x), INT_MAX),
28985 clip_to_bounds (INT_MIN, XINT (y), INT_MAX));
28989 /* Display frame CURSOR, optionally using shape defined by POINTER. */
28990 static void
28991 define_frame_cursor1 (struct frame *f, Cursor cursor, Lisp_Object pointer)
28993 /* Do not change cursor shape while dragging mouse. */
28994 if (!NILP (do_mouse_tracking))
28995 return;
28997 if (!NILP (pointer))
28999 if (EQ (pointer, Qarrow))
29000 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
29001 else if (EQ (pointer, Qhand))
29002 cursor = FRAME_X_OUTPUT (f)->hand_cursor;
29003 else if (EQ (pointer, Qtext))
29004 cursor = FRAME_X_OUTPUT (f)->text_cursor;
29005 else if (EQ (pointer, intern ("hdrag")))
29006 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
29007 else if (EQ (pointer, intern ("nhdrag")))
29008 cursor = FRAME_X_OUTPUT (f)->vertical_drag_cursor;
29009 #ifdef HAVE_X_WINDOWS
29010 else if (EQ (pointer, intern ("vdrag")))
29011 cursor = FRAME_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
29012 #endif
29013 else if (EQ (pointer, intern ("hourglass")))
29014 cursor = FRAME_X_OUTPUT (f)->hourglass_cursor;
29015 else if (EQ (pointer, Qmodeline))
29016 cursor = FRAME_X_OUTPUT (f)->modeline_cursor;
29017 else
29018 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
29021 if (cursor != No_Cursor)
29022 FRAME_RIF (f)->define_frame_cursor (f, cursor);
29025 #endif /* HAVE_WINDOW_SYSTEM */
29027 /* Take proper action when mouse has moved to the mode or header line
29028 or marginal area AREA of window W, x-position X and y-position Y.
29029 X is relative to the start of the text display area of W, so the
29030 width of bitmap areas and scroll bars must be subtracted to get a
29031 position relative to the start of the mode line. */
29033 static void
29034 note_mode_line_or_margin_highlight (Lisp_Object window, int x, int y,
29035 enum window_part area)
29037 struct window *w = XWINDOW (window);
29038 struct frame *f = XFRAME (w->frame);
29039 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
29040 #ifdef HAVE_WINDOW_SYSTEM
29041 Display_Info *dpyinfo;
29042 #endif
29043 Cursor cursor = No_Cursor;
29044 Lisp_Object pointer = Qnil;
29045 int dx, dy, width, height;
29046 ptrdiff_t charpos;
29047 Lisp_Object string, object = Qnil;
29048 Lisp_Object pos IF_LINT (= Qnil), help;
29050 Lisp_Object mouse_face;
29051 int original_x_pixel = x;
29052 struct glyph * glyph = NULL, * row_start_glyph = NULL;
29053 struct glyph_row *row IF_LINT (= 0);
29055 if (area == ON_MODE_LINE || area == ON_HEADER_LINE)
29057 int x0;
29058 struct glyph *end;
29060 /* Kludge alert: mode_line_string takes X/Y in pixels, but
29061 returns them in row/column units! */
29062 string = mode_line_string (w, area, &x, &y, &charpos,
29063 &object, &dx, &dy, &width, &height);
29065 row = (area == ON_MODE_LINE
29066 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
29067 : MATRIX_HEADER_LINE_ROW (w->current_matrix));
29069 /* Find the glyph under the mouse pointer. */
29070 if (row->mode_line_p && row->enabled_p)
29072 glyph = row_start_glyph = row->glyphs[TEXT_AREA];
29073 end = glyph + row->used[TEXT_AREA];
29075 for (x0 = original_x_pixel;
29076 glyph < end && x0 >= glyph->pixel_width;
29077 ++glyph)
29078 x0 -= glyph->pixel_width;
29080 if (glyph >= end)
29081 glyph = NULL;
29084 else
29086 x -= WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
29087 /* Kludge alert: marginal_area_string takes X/Y in pixels, but
29088 returns them in row/column units! */
29089 string = marginal_area_string (w, area, &x, &y, &charpos,
29090 &object, &dx, &dy, &width, &height);
29093 help = Qnil;
29095 #ifdef HAVE_WINDOW_SYSTEM
29096 if (IMAGEP (object))
29098 Lisp_Object image_map, hotspot;
29099 if ((image_map = Fplist_get (XCDR (object), QCmap),
29100 !NILP (image_map))
29101 && (hotspot = find_hot_spot (image_map, dx, dy),
29102 CONSP (hotspot))
29103 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
29105 Lisp_Object plist;
29107 /* Could check XCAR (hotspot) to see if we enter/leave this hot-spot.
29108 If so, we could look for mouse-enter, mouse-leave
29109 properties in PLIST (and do something...). */
29110 hotspot = XCDR (hotspot);
29111 if (CONSP (hotspot)
29112 && (plist = XCAR (hotspot), CONSP (plist)))
29114 pointer = Fplist_get (plist, Qpointer);
29115 if (NILP (pointer))
29116 pointer = Qhand;
29117 help = Fplist_get (plist, Qhelp_echo);
29118 if (!NILP (help))
29120 help_echo_string = help;
29121 XSETWINDOW (help_echo_window, w);
29122 help_echo_object = w->contents;
29123 help_echo_pos = charpos;
29127 if (NILP (pointer))
29128 pointer = Fplist_get (XCDR (object), QCpointer);
29130 #endif /* HAVE_WINDOW_SYSTEM */
29132 if (STRINGP (string))
29133 pos = make_number (charpos);
29135 /* Set the help text and mouse pointer. If the mouse is on a part
29136 of the mode line without any text (e.g. past the right edge of
29137 the mode line text), use the default help text and pointer. */
29138 if (STRINGP (string) || area == ON_MODE_LINE)
29140 /* Arrange to display the help by setting the global variables
29141 help_echo_string, help_echo_object, and help_echo_pos. */
29142 if (NILP (help))
29144 if (STRINGP (string))
29145 help = Fget_text_property (pos, Qhelp_echo, string);
29147 if (!NILP (help))
29149 help_echo_string = help;
29150 XSETWINDOW (help_echo_window, w);
29151 help_echo_object = string;
29152 help_echo_pos = charpos;
29154 else if (area == ON_MODE_LINE)
29156 Lisp_Object default_help
29157 = buffer_local_value (Qmode_line_default_help_echo,
29158 w->contents);
29160 if (STRINGP (default_help))
29162 help_echo_string = default_help;
29163 XSETWINDOW (help_echo_window, w);
29164 help_echo_object = Qnil;
29165 help_echo_pos = -1;
29170 #ifdef HAVE_WINDOW_SYSTEM
29171 /* Change the mouse pointer according to what is under it. */
29172 if (FRAME_WINDOW_P (f))
29174 bool draggable = (! WINDOW_BOTTOMMOST_P (w)
29175 || minibuf_level
29176 || NILP (Vresize_mini_windows));
29178 dpyinfo = FRAME_DISPLAY_INFO (f);
29179 if (STRINGP (string))
29181 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
29183 if (NILP (pointer))
29184 pointer = Fget_text_property (pos, Qpointer, string);
29186 /* Change the mouse pointer according to what is under X/Y. */
29187 if (NILP (pointer)
29188 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE)))
29190 Lisp_Object map;
29191 map = Fget_text_property (pos, Qlocal_map, string);
29192 if (!KEYMAPP (map))
29193 map = Fget_text_property (pos, Qkeymap, string);
29194 if (!KEYMAPP (map) && draggable)
29195 cursor = dpyinfo->vertical_scroll_bar_cursor;
29198 else if (draggable)
29199 /* Default mode-line pointer. */
29200 cursor = FRAME_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
29202 #endif
29205 /* Change the mouse face according to what is under X/Y. */
29206 if (STRINGP (string))
29208 mouse_face = Fget_text_property (pos, Qmouse_face, string);
29209 if (!NILP (Vmouse_highlight) && !NILP (mouse_face)
29210 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
29211 && glyph)
29213 Lisp_Object b, e;
29215 struct glyph * tmp_glyph;
29217 int gpos;
29218 int gseq_length;
29219 int total_pixel_width;
29220 ptrdiff_t begpos, endpos, ignore;
29222 int vpos, hpos;
29224 b = Fprevious_single_property_change (make_number (charpos + 1),
29225 Qmouse_face, string, Qnil);
29226 if (NILP (b))
29227 begpos = 0;
29228 else
29229 begpos = XINT (b);
29231 e = Fnext_single_property_change (pos, Qmouse_face, string, Qnil);
29232 if (NILP (e))
29233 endpos = SCHARS (string);
29234 else
29235 endpos = XINT (e);
29237 /* Calculate the glyph position GPOS of GLYPH in the
29238 displayed string, relative to the beginning of the
29239 highlighted part of the string.
29241 Note: GPOS is different from CHARPOS. CHARPOS is the
29242 position of GLYPH in the internal string object. A mode
29243 line string format has structures which are converted to
29244 a flattened string by the Emacs Lisp interpreter. The
29245 internal string is an element of those structures. The
29246 displayed string is the flattened string. */
29247 tmp_glyph = row_start_glyph;
29248 while (tmp_glyph < glyph
29249 && (!(EQ (tmp_glyph->object, glyph->object)
29250 && begpos <= tmp_glyph->charpos
29251 && tmp_glyph->charpos < endpos)))
29252 tmp_glyph++;
29253 gpos = glyph - tmp_glyph;
29255 /* Calculate the length GSEQ_LENGTH of the glyph sequence of
29256 the highlighted part of the displayed string to which
29257 GLYPH belongs. Note: GSEQ_LENGTH is different from
29258 SCHARS (STRING), because the latter returns the length of
29259 the internal string. */
29260 for (tmp_glyph = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
29261 tmp_glyph > glyph
29262 && (!(EQ (tmp_glyph->object, glyph->object)
29263 && begpos <= tmp_glyph->charpos
29264 && tmp_glyph->charpos < endpos));
29265 tmp_glyph--)
29267 gseq_length = gpos + (tmp_glyph - glyph) + 1;
29269 /* Calculate the total pixel width of all the glyphs between
29270 the beginning of the highlighted area and GLYPH. */
29271 total_pixel_width = 0;
29272 for (tmp_glyph = glyph - gpos; tmp_glyph != glyph; tmp_glyph++)
29273 total_pixel_width += tmp_glyph->pixel_width;
29275 /* Pre calculation of re-rendering position. Note: X is in
29276 column units here, after the call to mode_line_string or
29277 marginal_area_string. */
29278 hpos = x - gpos;
29279 vpos = (area == ON_MODE_LINE
29280 ? (w->current_matrix)->nrows - 1
29281 : 0);
29283 /* If GLYPH's position is included in the region that is
29284 already drawn in mouse face, we have nothing to do. */
29285 if ( EQ (window, hlinfo->mouse_face_window)
29286 && (!row->reversed_p
29287 ? (hlinfo->mouse_face_beg_col <= hpos
29288 && hpos < hlinfo->mouse_face_end_col)
29289 /* In R2L rows we swap BEG and END, see below. */
29290 : (hlinfo->mouse_face_end_col <= hpos
29291 && hpos < hlinfo->mouse_face_beg_col))
29292 && hlinfo->mouse_face_beg_row == vpos )
29293 return;
29295 if (clear_mouse_face (hlinfo))
29296 cursor = No_Cursor;
29298 if (!row->reversed_p)
29300 hlinfo->mouse_face_beg_col = hpos;
29301 hlinfo->mouse_face_beg_x = original_x_pixel
29302 - (total_pixel_width + dx);
29303 hlinfo->mouse_face_end_col = hpos + gseq_length;
29304 hlinfo->mouse_face_end_x = 0;
29306 else
29308 /* In R2L rows, show_mouse_face expects BEG and END
29309 coordinates to be swapped. */
29310 hlinfo->mouse_face_end_col = hpos;
29311 hlinfo->mouse_face_end_x = original_x_pixel
29312 - (total_pixel_width + dx);
29313 hlinfo->mouse_face_beg_col = hpos + gseq_length;
29314 hlinfo->mouse_face_beg_x = 0;
29317 hlinfo->mouse_face_beg_row = vpos;
29318 hlinfo->mouse_face_end_row = hlinfo->mouse_face_beg_row;
29319 hlinfo->mouse_face_past_end = 0;
29320 hlinfo->mouse_face_window = window;
29322 hlinfo->mouse_face_face_id = face_at_string_position (w, string,
29323 charpos,
29324 0, &ignore,
29325 glyph->face_id,
29326 true);
29327 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
29329 if (NILP (pointer))
29330 pointer = Qhand;
29332 else if ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
29333 clear_mouse_face (hlinfo);
29335 #ifdef HAVE_WINDOW_SYSTEM
29336 if (FRAME_WINDOW_P (f))
29337 define_frame_cursor1 (f, cursor, pointer);
29338 #endif
29342 /* EXPORT:
29343 Take proper action when the mouse has moved to position X, Y on
29344 frame F with regards to highlighting portions of display that have
29345 mouse-face properties. Also de-highlight portions of display where
29346 the mouse was before, set the mouse pointer shape as appropriate
29347 for the mouse coordinates, and activate help echo (tooltips).
29348 X and Y can be negative or out of range. */
29350 void
29351 note_mouse_highlight (struct frame *f, int x, int y)
29353 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
29354 enum window_part part = ON_NOTHING;
29355 Lisp_Object window;
29356 struct window *w;
29357 Cursor cursor = No_Cursor;
29358 Lisp_Object pointer = Qnil; /* Takes precedence over cursor! */
29359 struct buffer *b;
29361 /* When a menu is active, don't highlight because this looks odd. */
29362 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS) || defined (MSDOS)
29363 if (popup_activated ())
29364 return;
29365 #endif
29367 if (!f->glyphs_initialized_p
29368 || f->pointer_invisible)
29369 return;
29371 hlinfo->mouse_face_mouse_x = x;
29372 hlinfo->mouse_face_mouse_y = y;
29373 hlinfo->mouse_face_mouse_frame = f;
29375 if (hlinfo->mouse_face_defer)
29376 return;
29378 /* Which window is that in? */
29379 window = window_from_coordinates (f, x, y, &part, 1);
29381 /* If displaying active text in another window, clear that. */
29382 if (! EQ (window, hlinfo->mouse_face_window)
29383 /* Also clear if we move out of text area in same window. */
29384 || (!NILP (hlinfo->mouse_face_window)
29385 && !NILP (window)
29386 && part != ON_TEXT
29387 && part != ON_MODE_LINE
29388 && part != ON_HEADER_LINE))
29389 clear_mouse_face (hlinfo);
29391 /* Not on a window -> return. */
29392 if (!WINDOWP (window))
29393 return;
29395 /* Reset help_echo_string. It will get recomputed below. */
29396 help_echo_string = Qnil;
29398 /* Convert to window-relative pixel coordinates. */
29399 w = XWINDOW (window);
29400 frame_to_window_pixel_xy (w, &x, &y);
29402 #if defined (HAVE_WINDOW_SYSTEM) && ! defined (USE_GTK) && ! defined (HAVE_NS)
29403 /* Handle tool-bar window differently since it doesn't display a
29404 buffer. */
29405 if (EQ (window, f->tool_bar_window))
29407 note_tool_bar_highlight (f, x, y);
29408 return;
29410 #endif
29412 /* Mouse is on the mode, header line or margin? */
29413 if (part == ON_MODE_LINE || part == ON_HEADER_LINE
29414 || part == ON_LEFT_MARGIN || part == ON_RIGHT_MARGIN)
29416 note_mode_line_or_margin_highlight (window, x, y, part);
29418 #ifdef HAVE_WINDOW_SYSTEM
29419 if (part == ON_LEFT_MARGIN || part == ON_RIGHT_MARGIN)
29421 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
29422 /* Show non-text cursor (Bug#16647). */
29423 goto set_cursor;
29425 else
29426 #endif
29427 return;
29430 #ifdef HAVE_WINDOW_SYSTEM
29431 if (part == ON_VERTICAL_BORDER)
29433 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
29434 help_echo_string = build_string ("drag-mouse-1: resize");
29436 else if (part == ON_RIGHT_DIVIDER)
29438 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
29439 help_echo_string = build_string ("drag-mouse-1: resize");
29441 else if (part == ON_BOTTOM_DIVIDER)
29442 if (! WINDOW_BOTTOMMOST_P (w)
29443 || minibuf_level
29444 || NILP (Vresize_mini_windows))
29446 cursor = FRAME_X_OUTPUT (f)->vertical_drag_cursor;
29447 help_echo_string = build_string ("drag-mouse-1: resize");
29449 else
29450 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
29451 else if (part == ON_LEFT_FRINGE || part == ON_RIGHT_FRINGE
29452 || part == ON_VERTICAL_SCROLL_BAR
29453 || part == ON_HORIZONTAL_SCROLL_BAR)
29454 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
29455 else
29456 cursor = FRAME_X_OUTPUT (f)->text_cursor;
29457 #endif
29459 /* Are we in a window whose display is up to date?
29460 And verify the buffer's text has not changed. */
29461 b = XBUFFER (w->contents);
29462 if (part == ON_TEXT && w->window_end_valid && !window_outdated (w))
29464 int hpos, vpos, dx, dy, area = LAST_AREA;
29465 ptrdiff_t pos;
29466 struct glyph *glyph;
29467 Lisp_Object object;
29468 Lisp_Object mouse_face = Qnil, position;
29469 Lisp_Object *overlay_vec = NULL;
29470 ptrdiff_t i, noverlays;
29471 struct buffer *obuf;
29472 ptrdiff_t obegv, ozv;
29473 int same_region;
29475 /* Find the glyph under X/Y. */
29476 glyph = x_y_to_hpos_vpos (w, x, y, &hpos, &vpos, &dx, &dy, &area);
29478 #ifdef HAVE_WINDOW_SYSTEM
29479 /* Look for :pointer property on image. */
29480 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
29482 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
29483 if (img != NULL && IMAGEP (img->spec))
29485 Lisp_Object image_map, hotspot;
29486 if ((image_map = Fplist_get (XCDR (img->spec), QCmap),
29487 !NILP (image_map))
29488 && (hotspot = find_hot_spot (image_map,
29489 glyph->slice.img.x + dx,
29490 glyph->slice.img.y + dy),
29491 CONSP (hotspot))
29492 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
29494 Lisp_Object plist;
29496 /* Could check XCAR (hotspot) to see if we enter/leave
29497 this hot-spot.
29498 If so, we could look for mouse-enter, mouse-leave
29499 properties in PLIST (and do something...). */
29500 hotspot = XCDR (hotspot);
29501 if (CONSP (hotspot)
29502 && (plist = XCAR (hotspot), CONSP (plist)))
29504 pointer = Fplist_get (plist, Qpointer);
29505 if (NILP (pointer))
29506 pointer = Qhand;
29507 help_echo_string = Fplist_get (plist, Qhelp_echo);
29508 if (!NILP (help_echo_string))
29510 help_echo_window = window;
29511 help_echo_object = glyph->object;
29512 help_echo_pos = glyph->charpos;
29516 if (NILP (pointer))
29517 pointer = Fplist_get (XCDR (img->spec), QCpointer);
29520 #endif /* HAVE_WINDOW_SYSTEM */
29522 /* Clear mouse face if X/Y not over text. */
29523 if (glyph == NULL
29524 || area != TEXT_AREA
29525 || !MATRIX_ROW_DISPLAYS_TEXT_P (MATRIX_ROW (w->current_matrix, vpos))
29526 /* Glyph's OBJECT is nil for glyphs inserted by the
29527 display engine for its internal purposes, like truncation
29528 and continuation glyphs and blanks beyond the end of
29529 line's text on text terminals. If we are over such a
29530 glyph, we are not over any text. */
29531 || NILP (glyph->object)
29532 /* R2L rows have a stretch glyph at their front, which
29533 stands for no text, whereas L2R rows have no glyphs at
29534 all beyond the end of text. Treat such stretch glyphs
29535 like we do with NULL glyphs in L2R rows. */
29536 || (MATRIX_ROW (w->current_matrix, vpos)->reversed_p
29537 && glyph == MATRIX_ROW_GLYPH_START (w->current_matrix, vpos)
29538 && glyph->type == STRETCH_GLYPH
29539 && glyph->avoid_cursor_p))
29541 if (clear_mouse_face (hlinfo))
29542 cursor = No_Cursor;
29543 #ifdef HAVE_WINDOW_SYSTEM
29544 if (FRAME_WINDOW_P (f) && NILP (pointer))
29546 if (area != TEXT_AREA)
29547 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
29548 else
29549 pointer = Vvoid_text_area_pointer;
29551 #endif
29552 goto set_cursor;
29555 pos = glyph->charpos;
29556 object = glyph->object;
29557 if (!STRINGP (object) && !BUFFERP (object))
29558 goto set_cursor;
29560 /* If we get an out-of-range value, return now; avoid an error. */
29561 if (BUFFERP (object) && pos > BUF_Z (b))
29562 goto set_cursor;
29564 /* Make the window's buffer temporarily current for
29565 overlays_at and compute_char_face. */
29566 obuf = current_buffer;
29567 current_buffer = b;
29568 obegv = BEGV;
29569 ozv = ZV;
29570 BEGV = BEG;
29571 ZV = Z;
29573 /* Is this char mouse-active or does it have help-echo? */
29574 position = make_number (pos);
29576 USE_SAFE_ALLOCA;
29578 if (BUFFERP (object))
29580 /* Put all the overlays we want in a vector in overlay_vec. */
29581 GET_OVERLAYS_AT (pos, overlay_vec, noverlays, NULL, 0);
29582 /* Sort overlays into increasing priority order. */
29583 noverlays = sort_overlays (overlay_vec, noverlays, w);
29585 else
29586 noverlays = 0;
29588 if (NILP (Vmouse_highlight))
29590 clear_mouse_face (hlinfo);
29591 goto check_help_echo;
29594 same_region = coords_in_mouse_face_p (w, hpos, vpos);
29596 if (same_region)
29597 cursor = No_Cursor;
29599 /* Check mouse-face highlighting. */
29600 if (! same_region
29601 /* If there exists an overlay with mouse-face overlapping
29602 the one we are currently highlighting, we have to
29603 check if we enter the overlapping overlay, and then
29604 highlight only that. */
29605 || (OVERLAYP (hlinfo->mouse_face_overlay)
29606 && mouse_face_overlay_overlaps (hlinfo->mouse_face_overlay)))
29608 /* Find the highest priority overlay with a mouse-face. */
29609 Lisp_Object overlay = Qnil;
29610 for (i = noverlays - 1; i >= 0 && NILP (overlay); --i)
29612 mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
29613 if (!NILP (mouse_face))
29614 overlay = overlay_vec[i];
29617 /* If we're highlighting the same overlay as before, there's
29618 no need to do that again. */
29619 if (!NILP (overlay) && EQ (overlay, hlinfo->mouse_face_overlay))
29620 goto check_help_echo;
29621 hlinfo->mouse_face_overlay = overlay;
29623 /* Clear the display of the old active region, if any. */
29624 if (clear_mouse_face (hlinfo))
29625 cursor = No_Cursor;
29627 /* If no overlay applies, get a text property. */
29628 if (NILP (overlay))
29629 mouse_face = Fget_text_property (position, Qmouse_face, object);
29631 /* Next, compute the bounds of the mouse highlighting and
29632 display it. */
29633 if (!NILP (mouse_face) && STRINGP (object))
29635 /* The mouse-highlighting comes from a display string
29636 with a mouse-face. */
29637 Lisp_Object s, e;
29638 ptrdiff_t ignore;
29640 s = Fprevious_single_property_change
29641 (make_number (pos + 1), Qmouse_face, object, Qnil);
29642 e = Fnext_single_property_change
29643 (position, Qmouse_face, object, Qnil);
29644 if (NILP (s))
29645 s = make_number (0);
29646 if (NILP (e))
29647 e = make_number (SCHARS (object));
29648 mouse_face_from_string_pos (w, hlinfo, object,
29649 XINT (s), XINT (e));
29650 hlinfo->mouse_face_past_end = 0;
29651 hlinfo->mouse_face_window = window;
29652 hlinfo->mouse_face_face_id
29653 = face_at_string_position (w, object, pos, 0, &ignore,
29654 glyph->face_id, true);
29655 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
29656 cursor = No_Cursor;
29658 else
29660 /* The mouse-highlighting, if any, comes from an overlay
29661 or text property in the buffer. */
29662 Lisp_Object buffer IF_LINT (= Qnil);
29663 Lisp_Object disp_string IF_LINT (= Qnil);
29665 if (STRINGP (object))
29667 /* If we are on a display string with no mouse-face,
29668 check if the text under it has one. */
29669 struct glyph_row *r = MATRIX_ROW (w->current_matrix, vpos);
29670 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
29671 pos = string_buffer_position (object, start);
29672 if (pos > 0)
29674 mouse_face = get_char_property_and_overlay
29675 (make_number (pos), Qmouse_face, w->contents, &overlay);
29676 buffer = w->contents;
29677 disp_string = object;
29680 else
29682 buffer = object;
29683 disp_string = Qnil;
29686 if (!NILP (mouse_face))
29688 Lisp_Object before, after;
29689 Lisp_Object before_string, after_string;
29690 /* To correctly find the limits of mouse highlight
29691 in a bidi-reordered buffer, we must not use the
29692 optimization of limiting the search in
29693 previous-single-property-change and
29694 next-single-property-change, because
29695 rows_from_pos_range needs the real start and end
29696 positions to DTRT in this case. That's because
29697 the first row visible in a window does not
29698 necessarily display the character whose position
29699 is the smallest. */
29700 Lisp_Object lim1
29701 = NILP (BVAR (XBUFFER (buffer), bidi_display_reordering))
29702 ? Fmarker_position (w->start)
29703 : Qnil;
29704 Lisp_Object lim2
29705 = NILP (BVAR (XBUFFER (buffer), bidi_display_reordering))
29706 ? make_number (BUF_Z (XBUFFER (buffer))
29707 - w->window_end_pos)
29708 : Qnil;
29710 if (NILP (overlay))
29712 /* Handle the text property case. */
29713 before = Fprevious_single_property_change
29714 (make_number (pos + 1), Qmouse_face, buffer, lim1);
29715 after = Fnext_single_property_change
29716 (make_number (pos), Qmouse_face, buffer, lim2);
29717 before_string = after_string = Qnil;
29719 else
29721 /* Handle the overlay case. */
29722 before = Foverlay_start (overlay);
29723 after = Foverlay_end (overlay);
29724 before_string = Foverlay_get (overlay, Qbefore_string);
29725 after_string = Foverlay_get (overlay, Qafter_string);
29727 if (!STRINGP (before_string)) before_string = Qnil;
29728 if (!STRINGP (after_string)) after_string = Qnil;
29731 mouse_face_from_buffer_pos (window, hlinfo, pos,
29732 NILP (before)
29734 : XFASTINT (before),
29735 NILP (after)
29736 ? BUF_Z (XBUFFER (buffer))
29737 : XFASTINT (after),
29738 before_string, after_string,
29739 disp_string);
29740 cursor = No_Cursor;
29745 check_help_echo:
29747 /* Look for a `help-echo' property. */
29748 if (NILP (help_echo_string)) {
29749 Lisp_Object help, overlay;
29751 /* Check overlays first. */
29752 help = overlay = Qnil;
29753 for (i = noverlays - 1; i >= 0 && NILP (help); --i)
29755 overlay = overlay_vec[i];
29756 help = Foverlay_get (overlay, Qhelp_echo);
29759 if (!NILP (help))
29761 help_echo_string = help;
29762 help_echo_window = window;
29763 help_echo_object = overlay;
29764 help_echo_pos = pos;
29766 else
29768 Lisp_Object obj = glyph->object;
29769 ptrdiff_t charpos = glyph->charpos;
29771 /* Try text properties. */
29772 if (STRINGP (obj)
29773 && charpos >= 0
29774 && charpos < SCHARS (obj))
29776 help = Fget_text_property (make_number (charpos),
29777 Qhelp_echo, obj);
29778 if (NILP (help))
29780 /* If the string itself doesn't specify a help-echo,
29781 see if the buffer text ``under'' it does. */
29782 struct glyph_row *r
29783 = MATRIX_ROW (w->current_matrix, vpos);
29784 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
29785 ptrdiff_t p = string_buffer_position (obj, start);
29786 if (p > 0)
29788 help = Fget_char_property (make_number (p),
29789 Qhelp_echo, w->contents);
29790 if (!NILP (help))
29792 charpos = p;
29793 obj = w->contents;
29798 else if (BUFFERP (obj)
29799 && charpos >= BEGV
29800 && charpos < ZV)
29801 help = Fget_text_property (make_number (charpos), Qhelp_echo,
29802 obj);
29804 if (!NILP (help))
29806 help_echo_string = help;
29807 help_echo_window = window;
29808 help_echo_object = obj;
29809 help_echo_pos = charpos;
29814 #ifdef HAVE_WINDOW_SYSTEM
29815 /* Look for a `pointer' property. */
29816 if (FRAME_WINDOW_P (f) && NILP (pointer))
29818 /* Check overlays first. */
29819 for (i = noverlays - 1; i >= 0 && NILP (pointer); --i)
29820 pointer = Foverlay_get (overlay_vec[i], Qpointer);
29822 if (NILP (pointer))
29824 Lisp_Object obj = glyph->object;
29825 ptrdiff_t charpos = glyph->charpos;
29827 /* Try text properties. */
29828 if (STRINGP (obj)
29829 && charpos >= 0
29830 && charpos < SCHARS (obj))
29832 pointer = Fget_text_property (make_number (charpos),
29833 Qpointer, obj);
29834 if (NILP (pointer))
29836 /* If the string itself doesn't specify a pointer,
29837 see if the buffer text ``under'' it does. */
29838 struct glyph_row *r
29839 = MATRIX_ROW (w->current_matrix, vpos);
29840 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
29841 ptrdiff_t p = string_buffer_position (obj, start);
29842 if (p > 0)
29843 pointer = Fget_char_property (make_number (p),
29844 Qpointer, w->contents);
29847 else if (BUFFERP (obj)
29848 && charpos >= BEGV
29849 && charpos < ZV)
29850 pointer = Fget_text_property (make_number (charpos),
29851 Qpointer, obj);
29854 #endif /* HAVE_WINDOW_SYSTEM */
29856 BEGV = obegv;
29857 ZV = ozv;
29858 current_buffer = obuf;
29859 SAFE_FREE ();
29862 set_cursor:
29864 #ifdef HAVE_WINDOW_SYSTEM
29865 if (FRAME_WINDOW_P (f))
29866 define_frame_cursor1 (f, cursor, pointer);
29867 #else
29868 /* This is here to prevent a compiler error, about "label at end of
29869 compound statement". */
29870 return;
29871 #endif
29875 /* EXPORT for RIF:
29876 Clear any mouse-face on window W. This function is part of the
29877 redisplay interface, and is called from try_window_id and similar
29878 functions to ensure the mouse-highlight is off. */
29880 void
29881 x_clear_window_mouse_face (struct window *w)
29883 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (XFRAME (w->frame));
29884 Lisp_Object window;
29886 block_input ();
29887 XSETWINDOW (window, w);
29888 if (EQ (window, hlinfo->mouse_face_window))
29889 clear_mouse_face (hlinfo);
29890 unblock_input ();
29894 /* EXPORT:
29895 Just discard the mouse face information for frame F, if any.
29896 This is used when the size of F is changed. */
29898 void
29899 cancel_mouse_face (struct frame *f)
29901 Lisp_Object window;
29902 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
29904 window = hlinfo->mouse_face_window;
29905 if (! NILP (window) && XFRAME (XWINDOW (window)->frame) == f)
29906 reset_mouse_highlight (hlinfo);
29911 /***********************************************************************
29912 Exposure Events
29913 ***********************************************************************/
29915 #ifdef HAVE_WINDOW_SYSTEM
29917 /* Redraw the part of glyph row area AREA of glyph row ROW on window W
29918 which intersects rectangle R. R is in window-relative coordinates. */
29920 static void
29921 expose_area (struct window *w, struct glyph_row *row, XRectangle *r,
29922 enum glyph_row_area area)
29924 struct glyph *first = row->glyphs[area];
29925 struct glyph *end = row->glyphs[area] + row->used[area];
29926 struct glyph *last;
29927 int first_x, start_x, x;
29929 if (area == TEXT_AREA && row->fill_line_p)
29930 /* If row extends face to end of line write the whole line. */
29931 draw_glyphs (w, 0, row, area,
29932 0, row->used[area],
29933 DRAW_NORMAL_TEXT, 0);
29934 else
29936 /* Set START_X to the window-relative start position for drawing glyphs of
29937 AREA. The first glyph of the text area can be partially visible.
29938 The first glyphs of other areas cannot. */
29939 start_x = window_box_left_offset (w, area);
29940 x = start_x;
29941 if (area == TEXT_AREA)
29942 x += row->x;
29944 /* Find the first glyph that must be redrawn. */
29945 while (first < end
29946 && x + first->pixel_width < r->x)
29948 x += first->pixel_width;
29949 ++first;
29952 /* Find the last one. */
29953 last = first;
29954 first_x = x;
29955 while (last < end
29956 && x < r->x + r->width)
29958 x += last->pixel_width;
29959 ++last;
29962 /* Repaint. */
29963 if (last > first)
29964 draw_glyphs (w, first_x - start_x, row, area,
29965 first - row->glyphs[area], last - row->glyphs[area],
29966 DRAW_NORMAL_TEXT, 0);
29971 /* Redraw the parts of the glyph row ROW on window W intersecting
29972 rectangle R. R is in window-relative coordinates. Value is
29973 non-zero if mouse-face was overwritten. */
29975 static int
29976 expose_line (struct window *w, struct glyph_row *row, XRectangle *r)
29978 eassert (row->enabled_p);
29980 if (row->mode_line_p || w->pseudo_window_p)
29981 draw_glyphs (w, 0, row, TEXT_AREA,
29982 0, row->used[TEXT_AREA],
29983 DRAW_NORMAL_TEXT, 0);
29984 else
29986 if (row->used[LEFT_MARGIN_AREA])
29987 expose_area (w, row, r, LEFT_MARGIN_AREA);
29988 if (row->used[TEXT_AREA])
29989 expose_area (w, row, r, TEXT_AREA);
29990 if (row->used[RIGHT_MARGIN_AREA])
29991 expose_area (w, row, r, RIGHT_MARGIN_AREA);
29992 draw_row_fringe_bitmaps (w, row);
29995 return row->mouse_face_p;
29999 /* Redraw those parts of glyphs rows during expose event handling that
30000 overlap other rows. Redrawing of an exposed line writes over parts
30001 of lines overlapping that exposed line; this function fixes that.
30003 W is the window being exposed. FIRST_OVERLAPPING_ROW is the first
30004 row in W's current matrix that is exposed and overlaps other rows.
30005 LAST_OVERLAPPING_ROW is the last such row. */
30007 static void
30008 expose_overlaps (struct window *w,
30009 struct glyph_row *first_overlapping_row,
30010 struct glyph_row *last_overlapping_row,
30011 XRectangle *r)
30013 struct glyph_row *row;
30015 for (row = first_overlapping_row; row <= last_overlapping_row; ++row)
30016 if (row->overlapping_p)
30018 eassert (row->enabled_p && !row->mode_line_p);
30020 row->clip = r;
30021 if (row->used[LEFT_MARGIN_AREA])
30022 x_fix_overlapping_area (w, row, LEFT_MARGIN_AREA, OVERLAPS_BOTH);
30024 if (row->used[TEXT_AREA])
30025 x_fix_overlapping_area (w, row, TEXT_AREA, OVERLAPS_BOTH);
30027 if (row->used[RIGHT_MARGIN_AREA])
30028 x_fix_overlapping_area (w, row, RIGHT_MARGIN_AREA, OVERLAPS_BOTH);
30029 row->clip = NULL;
30034 /* Return non-zero if W's cursor intersects rectangle R. */
30036 static int
30037 phys_cursor_in_rect_p (struct window *w, XRectangle *r)
30039 XRectangle cr, result;
30040 struct glyph *cursor_glyph;
30041 struct glyph_row *row;
30043 if (w->phys_cursor.vpos >= 0
30044 && w->phys_cursor.vpos < w->current_matrix->nrows
30045 && (row = MATRIX_ROW (w->current_matrix, w->phys_cursor.vpos),
30046 row->enabled_p)
30047 && row->cursor_in_fringe_p)
30049 /* Cursor is in the fringe. */
30050 cr.x = window_box_right_offset (w,
30051 (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
30052 ? RIGHT_MARGIN_AREA
30053 : TEXT_AREA));
30054 cr.y = row->y;
30055 cr.width = WINDOW_RIGHT_FRINGE_WIDTH (w);
30056 cr.height = row->height;
30057 return x_intersect_rectangles (&cr, r, &result);
30060 cursor_glyph = get_phys_cursor_glyph (w);
30061 if (cursor_glyph)
30063 /* r is relative to W's box, but w->phys_cursor.x is relative
30064 to left edge of W's TEXT area. Adjust it. */
30065 cr.x = window_box_left_offset (w, TEXT_AREA) + w->phys_cursor.x;
30066 cr.y = w->phys_cursor.y;
30067 cr.width = cursor_glyph->pixel_width;
30068 cr.height = w->phys_cursor_height;
30069 /* ++KFS: W32 version used W32-specific IntersectRect here, but
30070 I assume the effect is the same -- and this is portable. */
30071 return x_intersect_rectangles (&cr, r, &result);
30073 /* If we don't understand the format, pretend we're not in the hot-spot. */
30074 return 0;
30078 /* EXPORT:
30079 Draw a vertical window border to the right of window W if W doesn't
30080 have vertical scroll bars. */
30082 void
30083 x_draw_vertical_border (struct window *w)
30085 struct frame *f = XFRAME (WINDOW_FRAME (w));
30087 /* We could do better, if we knew what type of scroll-bar the adjacent
30088 windows (on either side) have... But we don't :-(
30089 However, I think this works ok. ++KFS 2003-04-25 */
30091 /* Redraw borders between horizontally adjacent windows. Don't
30092 do it for frames with vertical scroll bars because either the
30093 right scroll bar of a window, or the left scroll bar of its
30094 neighbor will suffice as a border. */
30095 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f) || FRAME_RIGHT_DIVIDER_WIDTH (f))
30096 return;
30098 /* Note: It is necessary to redraw both the left and the right
30099 borders, for when only this single window W is being
30100 redisplayed. */
30101 if (!WINDOW_RIGHTMOST_P (w)
30102 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w))
30104 int x0, x1, y0, y1;
30106 window_box_edges (w, &x0, &y0, &x1, &y1);
30107 y1 -= 1;
30109 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
30110 x1 -= 1;
30112 FRAME_RIF (f)->draw_vertical_window_border (w, x1, y0, y1);
30115 if (!WINDOW_LEFTMOST_P (w)
30116 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w))
30118 int x0, x1, y0, y1;
30120 window_box_edges (w, &x0, &y0, &x1, &y1);
30121 y1 -= 1;
30123 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
30124 x0 -= 1;
30126 FRAME_RIF (f)->draw_vertical_window_border (w, x0, y0, y1);
30131 /* Draw window dividers for window W. */
30133 void
30134 x_draw_right_divider (struct window *w)
30136 struct frame *f = WINDOW_XFRAME (w);
30138 if (w->mini || w->pseudo_window_p)
30139 return;
30140 else if (WINDOW_RIGHT_DIVIDER_WIDTH (w))
30142 int x0 = WINDOW_RIGHT_EDGE_X (w) - WINDOW_RIGHT_DIVIDER_WIDTH (w);
30143 int x1 = WINDOW_RIGHT_EDGE_X (w);
30144 int y0 = WINDOW_TOP_EDGE_Y (w);
30145 /* The bottom divider prevails. */
30146 int y1 = WINDOW_BOTTOM_EDGE_Y (w) - WINDOW_BOTTOM_DIVIDER_WIDTH (w);
30148 FRAME_RIF (f)->draw_window_divider (w, x0, x1, y0, y1);
30152 static void
30153 x_draw_bottom_divider (struct window *w)
30155 struct frame *f = XFRAME (WINDOW_FRAME (w));
30157 if (w->mini || w->pseudo_window_p)
30158 return;
30159 else if (WINDOW_BOTTOM_DIVIDER_WIDTH (w))
30161 int x0 = WINDOW_LEFT_EDGE_X (w);
30162 int x1 = WINDOW_RIGHT_EDGE_X (w);
30163 int y0 = WINDOW_BOTTOM_EDGE_Y (w) - WINDOW_BOTTOM_DIVIDER_WIDTH (w);
30164 int y1 = WINDOW_BOTTOM_EDGE_Y (w);
30166 FRAME_RIF (f)->draw_window_divider (w, x0, x1, y0, y1);
30170 /* Redraw the part of window W intersection rectangle FR. Pixel
30171 coordinates in FR are frame-relative. Call this function with
30172 input blocked. Value is non-zero if the exposure overwrites
30173 mouse-face. */
30175 static int
30176 expose_window (struct window *w, XRectangle *fr)
30178 struct frame *f = XFRAME (w->frame);
30179 XRectangle wr, r;
30180 int mouse_face_overwritten_p = 0;
30182 /* If window is not yet fully initialized, do nothing. This can
30183 happen when toolkit scroll bars are used and a window is split.
30184 Reconfiguring the scroll bar will generate an expose for a newly
30185 created window. */
30186 if (w->current_matrix == NULL)
30187 return 0;
30189 /* When we're currently updating the window, display and current
30190 matrix usually don't agree. Arrange for a thorough display
30191 later. */
30192 if (w->must_be_updated_p)
30194 SET_FRAME_GARBAGED (f);
30195 return 0;
30198 /* Frame-relative pixel rectangle of W. */
30199 wr.x = WINDOW_LEFT_EDGE_X (w);
30200 wr.y = WINDOW_TOP_EDGE_Y (w);
30201 wr.width = WINDOW_PIXEL_WIDTH (w);
30202 wr.height = WINDOW_PIXEL_HEIGHT (w);
30204 if (x_intersect_rectangles (fr, &wr, &r))
30206 int yb = window_text_bottom_y (w);
30207 struct glyph_row *row;
30208 int cursor_cleared_p, phys_cursor_on_p;
30209 struct glyph_row *first_overlapping_row, *last_overlapping_row;
30211 TRACE ((stderr, "expose_window (%d, %d, %d, %d)\n",
30212 r.x, r.y, r.width, r.height));
30214 /* Convert to window coordinates. */
30215 r.x -= WINDOW_LEFT_EDGE_X (w);
30216 r.y -= WINDOW_TOP_EDGE_Y (w);
30218 /* Turn off the cursor. */
30219 if (!w->pseudo_window_p
30220 && phys_cursor_in_rect_p (w, &r))
30222 x_clear_cursor (w);
30223 cursor_cleared_p = 1;
30225 else
30226 cursor_cleared_p = 0;
30228 /* If the row containing the cursor extends face to end of line,
30229 then expose_area might overwrite the cursor outside the
30230 rectangle and thus notice_overwritten_cursor might clear
30231 w->phys_cursor_on_p. We remember the original value and
30232 check later if it is changed. */
30233 phys_cursor_on_p = w->phys_cursor_on_p;
30235 /* Update lines intersecting rectangle R. */
30236 first_overlapping_row = last_overlapping_row = NULL;
30237 for (row = w->current_matrix->rows;
30238 row->enabled_p;
30239 ++row)
30241 int y0 = row->y;
30242 int y1 = MATRIX_ROW_BOTTOM_Y (row);
30244 if ((y0 >= r.y && y0 < r.y + r.height)
30245 || (y1 > r.y && y1 < r.y + r.height)
30246 || (r.y >= y0 && r.y < y1)
30247 || (r.y + r.height > y0 && r.y + r.height < y1))
30249 /* A header line may be overlapping, but there is no need
30250 to fix overlapping areas for them. KFS 2005-02-12 */
30251 if (row->overlapping_p && !row->mode_line_p)
30253 if (first_overlapping_row == NULL)
30254 first_overlapping_row = row;
30255 last_overlapping_row = row;
30258 row->clip = fr;
30259 if (expose_line (w, row, &r))
30260 mouse_face_overwritten_p = 1;
30261 row->clip = NULL;
30263 else if (row->overlapping_p)
30265 /* We must redraw a row overlapping the exposed area. */
30266 if (y0 < r.y
30267 ? y0 + row->phys_height > r.y
30268 : y0 + row->ascent - row->phys_ascent < r.y +r.height)
30270 if (first_overlapping_row == NULL)
30271 first_overlapping_row = row;
30272 last_overlapping_row = row;
30276 if (y1 >= yb)
30277 break;
30280 /* Display the mode line if there is one. */
30281 if (WINDOW_WANTS_MODELINE_P (w)
30282 && (row = MATRIX_MODE_LINE_ROW (w->current_matrix),
30283 row->enabled_p)
30284 && row->y < r.y + r.height)
30286 if (expose_line (w, row, &r))
30287 mouse_face_overwritten_p = 1;
30290 if (!w->pseudo_window_p)
30292 /* Fix the display of overlapping rows. */
30293 if (first_overlapping_row)
30294 expose_overlaps (w, first_overlapping_row, last_overlapping_row,
30295 fr);
30297 /* Draw border between windows. */
30298 if (WINDOW_RIGHT_DIVIDER_WIDTH (w))
30299 x_draw_right_divider (w);
30300 else
30301 x_draw_vertical_border (w);
30303 if (WINDOW_BOTTOM_DIVIDER_WIDTH (w))
30304 x_draw_bottom_divider (w);
30306 /* Turn the cursor on again. */
30307 if (cursor_cleared_p
30308 || (phys_cursor_on_p && !w->phys_cursor_on_p))
30309 update_window_cursor (w, 1);
30313 return mouse_face_overwritten_p;
30318 /* Redraw (parts) of all windows in the window tree rooted at W that
30319 intersect R. R contains frame pixel coordinates. Value is
30320 non-zero if the exposure overwrites mouse-face. */
30322 static int
30323 expose_window_tree (struct window *w, XRectangle *r)
30325 struct frame *f = XFRAME (w->frame);
30326 int mouse_face_overwritten_p = 0;
30328 while (w && !FRAME_GARBAGED_P (f))
30330 if (WINDOWP (w->contents))
30331 mouse_face_overwritten_p
30332 |= expose_window_tree (XWINDOW (w->contents), r);
30333 else
30334 mouse_face_overwritten_p |= expose_window (w, r);
30336 w = NILP (w->next) ? NULL : XWINDOW (w->next);
30339 return mouse_face_overwritten_p;
30343 /* EXPORT:
30344 Redisplay an exposed area of frame F. X and Y are the upper-left
30345 corner of the exposed rectangle. W and H are width and height of
30346 the exposed area. All are pixel values. W or H zero means redraw
30347 the entire frame. */
30349 void
30350 expose_frame (struct frame *f, int x, int y, int w, int h)
30352 XRectangle r;
30353 int mouse_face_overwritten_p = 0;
30355 TRACE ((stderr, "expose_frame "));
30357 /* No need to redraw if frame will be redrawn soon. */
30358 if (FRAME_GARBAGED_P (f))
30360 TRACE ((stderr, " garbaged\n"));
30361 return;
30364 /* If basic faces haven't been realized yet, there is no point in
30365 trying to redraw anything. This can happen when we get an expose
30366 event while Emacs is starting, e.g. by moving another window. */
30367 if (FRAME_FACE_CACHE (f) == NULL
30368 || FRAME_FACE_CACHE (f)->used < BASIC_FACE_ID_SENTINEL)
30370 TRACE ((stderr, " no faces\n"));
30371 return;
30374 if (w == 0 || h == 0)
30376 r.x = r.y = 0;
30377 r.width = FRAME_TEXT_WIDTH (f);
30378 r.height = FRAME_TEXT_HEIGHT (f);
30380 else
30382 r.x = x;
30383 r.y = y;
30384 r.width = w;
30385 r.height = h;
30388 TRACE ((stderr, "(%d, %d, %d, %d)\n", r.x, r.y, r.width, r.height));
30389 mouse_face_overwritten_p = expose_window_tree (XWINDOW (f->root_window), &r);
30391 #if ! defined (USE_GTK) && ! defined (HAVE_NS)
30392 if (WINDOWP (f->tool_bar_window))
30393 mouse_face_overwritten_p
30394 |= expose_window (XWINDOW (f->tool_bar_window), &r);
30395 #endif
30397 #ifdef HAVE_X_WINDOWS
30398 #ifndef MSDOS
30399 #if ! defined (USE_X_TOOLKIT) && ! defined (USE_GTK)
30400 if (WINDOWP (f->menu_bar_window))
30401 mouse_face_overwritten_p
30402 |= expose_window (XWINDOW (f->menu_bar_window), &r);
30403 #endif /* not USE_X_TOOLKIT and not USE_GTK */
30404 #endif
30405 #endif
30407 /* Some window managers support a focus-follows-mouse style with
30408 delayed raising of frames. Imagine a partially obscured frame,
30409 and moving the mouse into partially obscured mouse-face on that
30410 frame. The visible part of the mouse-face will be highlighted,
30411 then the WM raises the obscured frame. With at least one WM, KDE
30412 2.1, Emacs is not getting any event for the raising of the frame
30413 (even tried with SubstructureRedirectMask), only Expose events.
30414 These expose events will draw text normally, i.e. not
30415 highlighted. Which means we must redo the highlight here.
30416 Subsume it under ``we love X''. --gerd 2001-08-15 */
30417 /* Included in Windows version because Windows most likely does not
30418 do the right thing if any third party tool offers
30419 focus-follows-mouse with delayed raise. --jason 2001-10-12 */
30420 if (mouse_face_overwritten_p && !FRAME_GARBAGED_P (f))
30422 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
30423 if (f == hlinfo->mouse_face_mouse_frame)
30425 int mouse_x = hlinfo->mouse_face_mouse_x;
30426 int mouse_y = hlinfo->mouse_face_mouse_y;
30427 clear_mouse_face (hlinfo);
30428 note_mouse_highlight (f, mouse_x, mouse_y);
30434 /* EXPORT:
30435 Determine the intersection of two rectangles R1 and R2. Return
30436 the intersection in *RESULT. Value is non-zero if RESULT is not
30437 empty. */
30440 x_intersect_rectangles (XRectangle *r1, XRectangle *r2, XRectangle *result)
30442 XRectangle *left, *right;
30443 XRectangle *upper, *lower;
30444 int intersection_p = 0;
30446 /* Rearrange so that R1 is the left-most rectangle. */
30447 if (r1->x < r2->x)
30448 left = r1, right = r2;
30449 else
30450 left = r2, right = r1;
30452 /* X0 of the intersection is right.x0, if this is inside R1,
30453 otherwise there is no intersection. */
30454 if (right->x <= left->x + left->width)
30456 result->x = right->x;
30458 /* The right end of the intersection is the minimum of
30459 the right ends of left and right. */
30460 result->width = (min (left->x + left->width, right->x + right->width)
30461 - result->x);
30463 /* Same game for Y. */
30464 if (r1->y < r2->y)
30465 upper = r1, lower = r2;
30466 else
30467 upper = r2, lower = r1;
30469 /* The upper end of the intersection is lower.y0, if this is inside
30470 of upper. Otherwise, there is no intersection. */
30471 if (lower->y <= upper->y + upper->height)
30473 result->y = lower->y;
30475 /* The lower end of the intersection is the minimum of the lower
30476 ends of upper and lower. */
30477 result->height = (min (lower->y + lower->height,
30478 upper->y + upper->height)
30479 - result->y);
30480 intersection_p = 1;
30484 return intersection_p;
30487 #endif /* HAVE_WINDOW_SYSTEM */
30490 /***********************************************************************
30491 Initialization
30492 ***********************************************************************/
30494 void
30495 syms_of_xdisp (void)
30497 Vwith_echo_area_save_vector = Qnil;
30498 staticpro (&Vwith_echo_area_save_vector);
30500 Vmessage_stack = Qnil;
30501 staticpro (&Vmessage_stack);
30503 /* Non-nil means don't actually do any redisplay. */
30504 DEFSYM (Qinhibit_redisplay, "inhibit-redisplay");
30506 DEFSYM (Qredisplay_internal, "redisplay_internal (C function)");
30508 message_dolog_marker1 = Fmake_marker ();
30509 staticpro (&message_dolog_marker1);
30510 message_dolog_marker2 = Fmake_marker ();
30511 staticpro (&message_dolog_marker2);
30512 message_dolog_marker3 = Fmake_marker ();
30513 staticpro (&message_dolog_marker3);
30515 #ifdef GLYPH_DEBUG
30516 defsubr (&Sdump_frame_glyph_matrix);
30517 defsubr (&Sdump_glyph_matrix);
30518 defsubr (&Sdump_glyph_row);
30519 defsubr (&Sdump_tool_bar_row);
30520 defsubr (&Strace_redisplay);
30521 defsubr (&Strace_to_stderr);
30522 #endif
30523 #ifdef HAVE_WINDOW_SYSTEM
30524 defsubr (&Stool_bar_height);
30525 defsubr (&Slookup_image_map);
30526 #endif
30527 defsubr (&Sline_pixel_height);
30528 defsubr (&Sformat_mode_line);
30529 defsubr (&Sinvisible_p);
30530 defsubr (&Scurrent_bidi_paragraph_direction);
30531 defsubr (&Swindow_text_pixel_size);
30532 defsubr (&Smove_point_visually);
30533 defsubr (&Sbidi_find_overridden_directionality);
30535 DEFSYM (Qmenu_bar_update_hook, "menu-bar-update-hook");
30536 DEFSYM (Qoverriding_terminal_local_map, "overriding-terminal-local-map");
30537 DEFSYM (Qoverriding_local_map, "overriding-local-map");
30538 DEFSYM (Qwindow_scroll_functions, "window-scroll-functions");
30539 DEFSYM (Qwindow_text_change_functions, "window-text-change-functions");
30540 DEFSYM (Qredisplay_end_trigger_functions, "redisplay-end-trigger-functions");
30541 DEFSYM (Qinhibit_point_motion_hooks, "inhibit-point-motion-hooks");
30542 DEFSYM (Qeval, "eval");
30543 DEFSYM (QCdata, ":data");
30545 /* Names of text properties relevant for redisplay. */
30546 DEFSYM (Qdisplay, "display");
30547 DEFSYM (Qspace_width, "space-width");
30548 DEFSYM (Qraise, "raise");
30549 DEFSYM (Qslice, "slice");
30550 DEFSYM (Qspace, "space");
30551 DEFSYM (Qmargin, "margin");
30552 DEFSYM (Qpointer, "pointer");
30553 DEFSYM (Qleft_margin, "left-margin");
30554 DEFSYM (Qright_margin, "right-margin");
30555 DEFSYM (Qcenter, "center");
30556 DEFSYM (Qline_height, "line-height");
30557 DEFSYM (QCalign_to, ":align-to");
30558 DEFSYM (QCrelative_width, ":relative-width");
30559 DEFSYM (QCrelative_height, ":relative-height");
30560 DEFSYM (QCeval, ":eval");
30561 DEFSYM (QCpropertize, ":propertize");
30562 DEFSYM (QCfile, ":file");
30563 DEFSYM (Qfontified, "fontified");
30564 DEFSYM (Qfontification_functions, "fontification-functions");
30566 /* Name of the face used to highlight trailing whitespace. */
30567 DEFSYM (Qtrailing_whitespace, "trailing-whitespace");
30569 /* Name and number of the face used to highlight escape glyphs. */
30570 DEFSYM (Qescape_glyph, "escape-glyph");
30572 /* Name and number of the face used to highlight non-breaking spaces. */
30573 DEFSYM (Qnobreak_space, "nobreak-space");
30575 /* The symbol 'image' which is the car of the lists used to represent
30576 images in Lisp. Also a tool bar style. */
30577 DEFSYM (Qimage, "image");
30579 /* Tool bar styles. */
30580 DEFSYM (Qtext, "text");
30581 DEFSYM (Qboth, "both");
30582 DEFSYM (Qboth_horiz, "both-horiz");
30583 DEFSYM (Qtext_image_horiz, "text-image-horiz");
30585 /* The image map types. */
30586 DEFSYM (QCmap, ":map");
30587 DEFSYM (QCpointer, ":pointer");
30588 DEFSYM (Qrect, "rect");
30589 DEFSYM (Qcircle, "circle");
30590 DEFSYM (Qpoly, "poly");
30592 /* The symbol `inhibit-menubar-update' and its DEFVAR_BOOL variable. */
30593 DEFSYM (Qinhibit_menubar_update, "inhibit-menubar-update");
30594 DEFSYM (Qmessage_truncate_lines, "message-truncate-lines");
30596 DEFSYM (Qgrow_only, "grow-only");
30597 DEFSYM (Qinhibit_eval_during_redisplay, "inhibit-eval-during-redisplay");
30598 DEFSYM (Qposition, "position");
30599 DEFSYM (Qbuffer_position, "buffer-position");
30600 DEFSYM (Qobject, "object");
30602 /* Cursor shapes. */
30603 DEFSYM (Qbar, "bar");
30604 DEFSYM (Qhbar, "hbar");
30605 DEFSYM (Qbox, "box");
30606 DEFSYM (Qhollow, "hollow");
30608 /* Pointer shapes. */
30609 DEFSYM (Qhand, "hand");
30610 DEFSYM (Qarrow, "arrow");
30611 /* also Qtext */
30613 DEFSYM (Qinhibit_free_realized_faces, "inhibit-free-realized-faces");
30615 list_of_error = list1 (list2 (intern_c_string ("error"),
30616 intern_c_string ("void-variable")));
30617 staticpro (&list_of_error);
30619 /* Values of those variables at last redisplay are stored as
30620 properties on 'overlay-arrow-position' symbol. However, if
30621 Voverlay_arrow_position is a marker, last-arrow-position is its
30622 numerical position. */
30623 DEFSYM (Qlast_arrow_position, "last-arrow-position");
30624 DEFSYM (Qlast_arrow_string, "last-arrow-string");
30626 /* Alternative overlay-arrow-string and overlay-arrow-bitmap
30627 properties on a symbol in overlay-arrow-variable-list. */
30628 DEFSYM (Qoverlay_arrow_string, "overlay-arrow-string");
30629 DEFSYM (Qoverlay_arrow_bitmap, "overlay-arrow-bitmap");
30631 echo_buffer[0] = echo_buffer[1] = Qnil;
30632 staticpro (&echo_buffer[0]);
30633 staticpro (&echo_buffer[1]);
30635 echo_area_buffer[0] = echo_area_buffer[1] = Qnil;
30636 staticpro (&echo_area_buffer[0]);
30637 staticpro (&echo_area_buffer[1]);
30639 Vmessages_buffer_name = build_pure_c_string ("*Messages*");
30640 staticpro (&Vmessages_buffer_name);
30642 mode_line_proptrans_alist = Qnil;
30643 staticpro (&mode_line_proptrans_alist);
30644 mode_line_string_list = Qnil;
30645 staticpro (&mode_line_string_list);
30646 mode_line_string_face = Qnil;
30647 staticpro (&mode_line_string_face);
30648 mode_line_string_face_prop = Qnil;
30649 staticpro (&mode_line_string_face_prop);
30650 Vmode_line_unwind_vector = Qnil;
30651 staticpro (&Vmode_line_unwind_vector);
30653 DEFSYM (Qmode_line_default_help_echo, "mode-line-default-help-echo");
30655 help_echo_string = Qnil;
30656 staticpro (&help_echo_string);
30657 help_echo_object = Qnil;
30658 staticpro (&help_echo_object);
30659 help_echo_window = Qnil;
30660 staticpro (&help_echo_window);
30661 previous_help_echo_string = Qnil;
30662 staticpro (&previous_help_echo_string);
30663 help_echo_pos = -1;
30665 DEFSYM (Qright_to_left, "right-to-left");
30666 DEFSYM (Qleft_to_right, "left-to-right");
30667 defsubr (&Sbidi_resolved_levels);
30669 #ifdef HAVE_WINDOW_SYSTEM
30670 DEFVAR_BOOL ("x-stretch-cursor", x_stretch_cursor_p,
30671 doc: /* Non-nil means draw block cursor as wide as the glyph under it.
30672 For example, if a block cursor is over a tab, it will be drawn as
30673 wide as that tab on the display. */);
30674 x_stretch_cursor_p = 0;
30675 #endif
30677 DEFVAR_LISP ("show-trailing-whitespace", Vshow_trailing_whitespace,
30678 doc: /* Non-nil means highlight trailing whitespace.
30679 The face used for trailing whitespace is `trailing-whitespace'. */);
30680 Vshow_trailing_whitespace = Qnil;
30682 DEFVAR_LISP ("nobreak-char-display", Vnobreak_char_display,
30683 doc: /* Control highlighting of non-ASCII space and hyphen chars.
30684 If the value is t, Emacs highlights non-ASCII chars which have the
30685 same appearance as an ASCII space or hyphen, using the `nobreak-space'
30686 or `escape-glyph' face respectively.
30688 U+00A0 (no-break space), U+00AD (soft hyphen), U+2010 (hyphen), and
30689 U+2011 (non-breaking hyphen) are affected.
30691 Any other non-nil value means to display these characters as a escape
30692 glyph followed by an ordinary space or hyphen.
30694 A value of nil means no special handling of these characters. */);
30695 Vnobreak_char_display = Qt;
30697 DEFVAR_LISP ("void-text-area-pointer", Vvoid_text_area_pointer,
30698 doc: /* The pointer shape to show in void text areas.
30699 A value of nil means to show the text pointer. Other options are
30700 `arrow', `text', `hand', `vdrag', `hdrag', `nhdrag', `modeline', and
30701 `hourglass'. */);
30702 Vvoid_text_area_pointer = Qarrow;
30704 DEFVAR_LISP ("inhibit-redisplay", Vinhibit_redisplay,
30705 doc: /* Non-nil means don't actually do any redisplay.
30706 This is used for internal purposes. */);
30707 Vinhibit_redisplay = Qnil;
30709 DEFVAR_LISP ("global-mode-string", Vglobal_mode_string,
30710 doc: /* String (or mode line construct) included (normally) in `mode-line-format'. */);
30711 Vglobal_mode_string = Qnil;
30713 DEFVAR_LISP ("overlay-arrow-position", Voverlay_arrow_position,
30714 doc: /* Marker for where to display an arrow on top of the buffer text.
30715 This must be the beginning of a line in order to work.
30716 See also `overlay-arrow-string'. */);
30717 Voverlay_arrow_position = Qnil;
30719 DEFVAR_LISP ("overlay-arrow-string", Voverlay_arrow_string,
30720 doc: /* String to display as an arrow in non-window frames.
30721 See also `overlay-arrow-position'. */);
30722 Voverlay_arrow_string = build_pure_c_string ("=>");
30724 DEFVAR_LISP ("overlay-arrow-variable-list", Voverlay_arrow_variable_list,
30725 doc: /* List of variables (symbols) which hold markers for overlay arrows.
30726 The symbols on this list are examined during redisplay to determine
30727 where to display overlay arrows. */);
30728 Voverlay_arrow_variable_list
30729 = list1 (intern_c_string ("overlay-arrow-position"));
30731 DEFVAR_INT ("scroll-step", emacs_scroll_step,
30732 doc: /* The number of lines to try scrolling a window by when point moves out.
30733 If that fails to bring point back on frame, point is centered instead.
30734 If this is zero, point is always centered after it moves off frame.
30735 If you want scrolling to always be a line at a time, you should set
30736 `scroll-conservatively' to a large value rather than set this to 1. */);
30738 DEFVAR_INT ("scroll-conservatively", scroll_conservatively,
30739 doc: /* Scroll up to this many lines, to bring point back on screen.
30740 If point moves off-screen, redisplay will scroll by up to
30741 `scroll-conservatively' lines in order to bring point just barely
30742 onto the screen again. If that cannot be done, then redisplay
30743 recenters point as usual.
30745 If the value is greater than 100, redisplay will never recenter point,
30746 but will always scroll just enough text to bring point into view, even
30747 if you move far away.
30749 A value of zero means always recenter point if it moves off screen. */);
30750 scroll_conservatively = 0;
30752 DEFVAR_INT ("scroll-margin", scroll_margin,
30753 doc: /* Number of lines of margin at the top and bottom of a window.
30754 Recenter the window whenever point gets within this many lines
30755 of the top or bottom of the window. */);
30756 scroll_margin = 0;
30758 DEFVAR_LISP ("display-pixels-per-inch", Vdisplay_pixels_per_inch,
30759 doc: /* Pixels per inch value for non-window system displays.
30760 Value is a number or a cons (WIDTH-DPI . HEIGHT-DPI). */);
30761 Vdisplay_pixels_per_inch = make_float (72.0);
30763 #ifdef GLYPH_DEBUG
30764 DEFVAR_INT ("debug-end-pos", debug_end_pos, doc: /* Don't ask. */);
30765 #endif
30767 DEFVAR_LISP ("truncate-partial-width-windows",
30768 Vtruncate_partial_width_windows,
30769 doc: /* Non-nil means truncate lines in windows narrower than the frame.
30770 For an integer value, truncate lines in each window narrower than the
30771 full frame width, provided the window width is less than that integer;
30772 otherwise, respect the value of `truncate-lines'.
30774 For any other non-nil value, truncate lines in all windows that do
30775 not span the full frame width.
30777 A value of nil means to respect the value of `truncate-lines'.
30779 If `word-wrap' is enabled, you might want to reduce this. */);
30780 Vtruncate_partial_width_windows = make_number (50);
30782 DEFVAR_LISP ("line-number-display-limit", Vline_number_display_limit,
30783 doc: /* Maximum buffer size for which line number should be displayed.
30784 If the buffer is bigger than this, the line number does not appear
30785 in the mode line. A value of nil means no limit. */);
30786 Vline_number_display_limit = Qnil;
30788 DEFVAR_INT ("line-number-display-limit-width",
30789 line_number_display_limit_width,
30790 doc: /* Maximum line width (in characters) for line number display.
30791 If the average length of the lines near point is bigger than this, then the
30792 line number may be omitted from the mode line. */);
30793 line_number_display_limit_width = 200;
30795 DEFVAR_BOOL ("highlight-nonselected-windows", highlight_nonselected_windows,
30796 doc: /* Non-nil means highlight region even in nonselected windows. */);
30797 highlight_nonselected_windows = 0;
30799 DEFVAR_BOOL ("multiple-frames", multiple_frames,
30800 doc: /* Non-nil if more than one frame is visible on this display.
30801 Minibuffer-only frames don't count, but iconified frames do.
30802 This variable is not guaranteed to be accurate except while processing
30803 `frame-title-format' and `icon-title-format'. */);
30805 DEFVAR_LISP ("frame-title-format", Vframe_title_format,
30806 doc: /* Template for displaying the title bar of visible frames.
30807 \(Assuming the window manager supports this feature.)
30809 This variable has the same structure as `mode-line-format', except that
30810 the %c and %l constructs are ignored. It is used only on frames for
30811 which no explicit name has been set \(see `modify-frame-parameters'). */);
30813 DEFVAR_LISP ("icon-title-format", Vicon_title_format,
30814 doc: /* Template for displaying the title bar of an iconified frame.
30815 \(Assuming the window manager supports this feature.)
30816 This variable has the same structure as `mode-line-format' (which see),
30817 and is used only on frames for which no explicit name has been set
30818 \(see `modify-frame-parameters'). */);
30819 Vicon_title_format
30820 = Vframe_title_format
30821 = listn (CONSTYPE_PURE, 3,
30822 intern_c_string ("multiple-frames"),
30823 build_pure_c_string ("%b"),
30824 listn (CONSTYPE_PURE, 4,
30825 empty_unibyte_string,
30826 intern_c_string ("invocation-name"),
30827 build_pure_c_string ("@"),
30828 intern_c_string ("system-name")));
30830 DEFVAR_LISP ("message-log-max", Vmessage_log_max,
30831 doc: /* Maximum number of lines to keep in the message log buffer.
30832 If nil, disable message logging. If t, log messages but don't truncate
30833 the buffer when it becomes large. */);
30834 Vmessage_log_max = make_number (1000);
30836 DEFVAR_LISP ("window-size-change-functions", Vwindow_size_change_functions,
30837 doc: /* Functions called before redisplay, if window sizes have changed.
30838 The value should be a list of functions that take one argument.
30839 Just before redisplay, for each frame, if any of its windows have changed
30840 size since the last redisplay, or have been split or deleted,
30841 all the functions in the list are called, with the frame as argument. */);
30842 Vwindow_size_change_functions = Qnil;
30844 DEFVAR_LISP ("window-scroll-functions", Vwindow_scroll_functions,
30845 doc: /* List of functions to call before redisplaying a window with scrolling.
30846 Each function is called with two arguments, the window and its new
30847 display-start position.
30848 These functions are called whenever the `window-start' marker is modified,
30849 either to point into another buffer (e.g. via `set-window-buffer') or another
30850 place in the same buffer.
30851 Note that the value of `window-end' is not valid when these functions are
30852 called.
30854 Warning: Do not use this feature to alter the way the window
30855 is scrolled. It is not designed for that, and such use probably won't
30856 work. */);
30857 Vwindow_scroll_functions = Qnil;
30859 DEFVAR_LISP ("window-text-change-functions",
30860 Vwindow_text_change_functions,
30861 doc: /* Functions to call in redisplay when text in the window might change. */);
30862 Vwindow_text_change_functions = Qnil;
30864 DEFVAR_LISP ("redisplay-end-trigger-functions", Vredisplay_end_trigger_functions,
30865 doc: /* Functions called when redisplay of a window reaches the end trigger.
30866 Each function is called with two arguments, the window and the end trigger value.
30867 See `set-window-redisplay-end-trigger'. */);
30868 Vredisplay_end_trigger_functions = Qnil;
30870 DEFVAR_LISP ("mouse-autoselect-window", Vmouse_autoselect_window,
30871 doc: /* Non-nil means autoselect window with mouse pointer.
30872 If nil, do not autoselect windows.
30873 A positive number means delay autoselection by that many seconds: a
30874 window is autoselected only after the mouse has remained in that
30875 window for the duration of the delay.
30876 A negative number has a similar effect, but causes windows to be
30877 autoselected only after the mouse has stopped moving. \(Because of
30878 the way Emacs compares mouse events, you will occasionally wait twice
30879 that time before the window gets selected.\)
30880 Any other value means to autoselect window instantaneously when the
30881 mouse pointer enters it.
30883 Autoselection selects the minibuffer only if it is active, and never
30884 unselects the minibuffer if it is active.
30886 When customizing this variable make sure that the actual value of
30887 `focus-follows-mouse' matches the behavior of your window manager. */);
30888 Vmouse_autoselect_window = Qnil;
30890 DEFVAR_LISP ("auto-resize-tool-bars", Vauto_resize_tool_bars,
30891 doc: /* Non-nil means automatically resize tool-bars.
30892 This dynamically changes the tool-bar's height to the minimum height
30893 that is needed to make all tool-bar items visible.
30894 If value is `grow-only', the tool-bar's height is only increased
30895 automatically; to decrease the tool-bar height, use \\[recenter]. */);
30896 Vauto_resize_tool_bars = Qt;
30898 DEFVAR_BOOL ("auto-raise-tool-bar-buttons", auto_raise_tool_bar_buttons_p,
30899 doc: /* Non-nil means raise tool-bar buttons when the mouse moves over them. */);
30900 auto_raise_tool_bar_buttons_p = 1;
30902 DEFVAR_BOOL ("make-cursor-line-fully-visible", make_cursor_line_fully_visible_p,
30903 doc: /* Non-nil means to scroll (recenter) cursor line if it is not fully visible. */);
30904 make_cursor_line_fully_visible_p = 1;
30906 DEFVAR_LISP ("tool-bar-border", Vtool_bar_border,
30907 doc: /* Border below tool-bar in pixels.
30908 If an integer, use it as the height of the border.
30909 If it is one of `internal-border-width' or `border-width', use the
30910 value of the corresponding frame parameter.
30911 Otherwise, no border is added below the tool-bar. */);
30912 Vtool_bar_border = Qinternal_border_width;
30914 DEFVAR_LISP ("tool-bar-button-margin", Vtool_bar_button_margin,
30915 doc: /* Margin around tool-bar buttons in pixels.
30916 If an integer, use that for both horizontal and vertical margins.
30917 Otherwise, value should be a pair of integers `(HORZ . VERT)' with
30918 HORZ specifying the horizontal margin, and VERT specifying the
30919 vertical margin. */);
30920 Vtool_bar_button_margin = make_number (DEFAULT_TOOL_BAR_BUTTON_MARGIN);
30922 DEFVAR_INT ("tool-bar-button-relief", tool_bar_button_relief,
30923 doc: /* Relief thickness of tool-bar buttons. */);
30924 tool_bar_button_relief = DEFAULT_TOOL_BAR_BUTTON_RELIEF;
30926 DEFVAR_LISP ("tool-bar-style", Vtool_bar_style,
30927 doc: /* Tool bar style to use.
30928 It can be one of
30929 image - show images only
30930 text - show text only
30931 both - show both, text below image
30932 both-horiz - show text to the right of the image
30933 text-image-horiz - show text to the left of the image
30934 any other - use system default or image if no system default.
30936 This variable only affects the GTK+ toolkit version of Emacs. */);
30937 Vtool_bar_style = Qnil;
30939 DEFVAR_INT ("tool-bar-max-label-size", tool_bar_max_label_size,
30940 doc: /* Maximum number of characters a label can have to be shown.
30941 The tool bar style must also show labels for this to have any effect, see
30942 `tool-bar-style'. */);
30943 tool_bar_max_label_size = DEFAULT_TOOL_BAR_LABEL_SIZE;
30945 DEFVAR_LISP ("fontification-functions", Vfontification_functions,
30946 doc: /* List of functions to call to fontify regions of text.
30947 Each function is called with one argument POS. Functions must
30948 fontify a region starting at POS in the current buffer, and give
30949 fontified regions the property `fontified'. */);
30950 Vfontification_functions = Qnil;
30951 Fmake_variable_buffer_local (Qfontification_functions);
30953 DEFVAR_BOOL ("unibyte-display-via-language-environment",
30954 unibyte_display_via_language_environment,
30955 doc: /* Non-nil means display unibyte text according to language environment.
30956 Specifically, this means that raw bytes in the range 160-255 decimal
30957 are displayed by converting them to the equivalent multibyte characters
30958 according to the current language environment. As a result, they are
30959 displayed according to the current fontset.
30961 Note that this variable affects only how these bytes are displayed,
30962 but does not change the fact they are interpreted as raw bytes. */);
30963 unibyte_display_via_language_environment = 0;
30965 DEFVAR_LISP ("max-mini-window-height", Vmax_mini_window_height,
30966 doc: /* Maximum height for resizing mini-windows (the minibuffer and the echo area).
30967 If a float, it specifies a fraction of the mini-window frame's height.
30968 If an integer, it specifies a number of lines. */);
30969 Vmax_mini_window_height = make_float (0.25);
30971 DEFVAR_LISP ("resize-mini-windows", Vresize_mini_windows,
30972 doc: /* How to resize mini-windows (the minibuffer and the echo area).
30973 A value of nil means don't automatically resize mini-windows.
30974 A value of t means resize them to fit the text displayed in them.
30975 A value of `grow-only', the default, means let mini-windows grow only;
30976 they return to their normal size when the minibuffer is closed, or the
30977 echo area becomes empty. */);
30978 Vresize_mini_windows = Qgrow_only;
30980 DEFVAR_LISP ("blink-cursor-alist", Vblink_cursor_alist,
30981 doc: /* Alist specifying how to blink the cursor off.
30982 Each element has the form (ON-STATE . OFF-STATE). Whenever the
30983 `cursor-type' frame-parameter or variable equals ON-STATE,
30984 comparing using `equal', Emacs uses OFF-STATE to specify
30985 how to blink it off. ON-STATE and OFF-STATE are values for
30986 the `cursor-type' frame parameter.
30988 If a frame's ON-STATE has no entry in this list,
30989 the frame's other specifications determine how to blink the cursor off. */);
30990 Vblink_cursor_alist = Qnil;
30992 DEFVAR_BOOL ("auto-hscroll-mode", automatic_hscrolling_p,
30993 doc: /* Allow or disallow automatic horizontal scrolling of windows.
30994 If non-nil, windows are automatically scrolled horizontally to make
30995 point visible. */);
30996 automatic_hscrolling_p = 1;
30997 DEFSYM (Qauto_hscroll_mode, "auto-hscroll-mode");
30999 DEFVAR_INT ("hscroll-margin", hscroll_margin,
31000 doc: /* How many columns away from the window edge point is allowed to get
31001 before automatic hscrolling will horizontally scroll the window. */);
31002 hscroll_margin = 5;
31004 DEFVAR_LISP ("hscroll-step", Vhscroll_step,
31005 doc: /* How many columns to scroll the window when point gets too close to the edge.
31006 When point is less than `hscroll-margin' columns from the window
31007 edge, automatic hscrolling will scroll the window by the amount of columns
31008 determined by this variable. If its value is a positive integer, scroll that
31009 many columns. If it's a positive floating-point number, it specifies the
31010 fraction of the window's width to scroll. If it's nil or zero, point will be
31011 centered horizontally after the scroll. Any other value, including negative
31012 numbers, are treated as if the value were zero.
31014 Automatic hscrolling always moves point outside the scroll margin, so if
31015 point was more than scroll step columns inside the margin, the window will
31016 scroll more than the value given by the scroll step.
31018 Note that the lower bound for automatic hscrolling specified by `scroll-left'
31019 and `scroll-right' overrides this variable's effect. */);
31020 Vhscroll_step = make_number (0);
31022 DEFVAR_BOOL ("message-truncate-lines", message_truncate_lines,
31023 doc: /* If non-nil, messages are truncated instead of resizing the echo area.
31024 Bind this around calls to `message' to let it take effect. */);
31025 message_truncate_lines = 0;
31027 DEFVAR_LISP ("menu-bar-update-hook", Vmenu_bar_update_hook,
31028 doc: /* Normal hook run to update the menu bar definitions.
31029 Redisplay runs this hook before it redisplays the menu bar.
31030 This is used to update menus such as Buffers, whose contents depend on
31031 various data. */);
31032 Vmenu_bar_update_hook = Qnil;
31034 DEFVAR_LISP ("menu-updating-frame", Vmenu_updating_frame,
31035 doc: /* Frame for which we are updating a menu.
31036 The enable predicate for a menu binding should check this variable. */);
31037 Vmenu_updating_frame = Qnil;
31039 DEFVAR_BOOL ("inhibit-menubar-update", inhibit_menubar_update,
31040 doc: /* Non-nil means don't update menu bars. Internal use only. */);
31041 inhibit_menubar_update = 0;
31043 DEFVAR_LISP ("wrap-prefix", Vwrap_prefix,
31044 doc: /* Prefix prepended to all continuation lines at display time.
31045 The value may be a string, an image, or a stretch-glyph; it is
31046 interpreted in the same way as the value of a `display' text property.
31048 This variable is overridden by any `wrap-prefix' text or overlay
31049 property.
31051 To add a prefix to non-continuation lines, use `line-prefix'. */);
31052 Vwrap_prefix = Qnil;
31053 DEFSYM (Qwrap_prefix, "wrap-prefix");
31054 Fmake_variable_buffer_local (Qwrap_prefix);
31056 DEFVAR_LISP ("line-prefix", Vline_prefix,
31057 doc: /* Prefix prepended to all non-continuation lines at display time.
31058 The value may be a string, an image, or a stretch-glyph; it is
31059 interpreted in the same way as the value of a `display' text property.
31061 This variable is overridden by any `line-prefix' text or overlay
31062 property.
31064 To add a prefix to continuation lines, use `wrap-prefix'. */);
31065 Vline_prefix = Qnil;
31066 DEFSYM (Qline_prefix, "line-prefix");
31067 Fmake_variable_buffer_local (Qline_prefix);
31069 DEFVAR_BOOL ("inhibit-eval-during-redisplay", inhibit_eval_during_redisplay,
31070 doc: /* Non-nil means don't eval Lisp during redisplay. */);
31071 inhibit_eval_during_redisplay = 0;
31073 DEFVAR_BOOL ("inhibit-free-realized-faces", inhibit_free_realized_faces,
31074 doc: /* Non-nil means don't free realized faces. Internal use only. */);
31075 inhibit_free_realized_faces = 0;
31077 DEFVAR_BOOL ("inhibit-bidi-mirroring", inhibit_bidi_mirroring,
31078 doc: /* Non-nil means don't mirror characters even when bidi context requires that.
31079 Intended for use during debugging and for testing bidi display;
31080 see biditest.el in the test suite. */);
31081 inhibit_bidi_mirroring = 0;
31083 #ifdef GLYPH_DEBUG
31084 DEFVAR_BOOL ("inhibit-try-window-id", inhibit_try_window_id,
31085 doc: /* Inhibit try_window_id display optimization. */);
31086 inhibit_try_window_id = 0;
31088 DEFVAR_BOOL ("inhibit-try-window-reusing", inhibit_try_window_reusing,
31089 doc: /* Inhibit try_window_reusing display optimization. */);
31090 inhibit_try_window_reusing = 0;
31092 DEFVAR_BOOL ("inhibit-try-cursor-movement", inhibit_try_cursor_movement,
31093 doc: /* Inhibit try_cursor_movement display optimization. */);
31094 inhibit_try_cursor_movement = 0;
31095 #endif /* GLYPH_DEBUG */
31097 DEFVAR_INT ("overline-margin", overline_margin,
31098 doc: /* Space between overline and text, in pixels.
31099 The default value is 2: the height of the overline (1 pixel) plus 1 pixel
31100 margin to the character height. */);
31101 overline_margin = 2;
31103 DEFVAR_INT ("underline-minimum-offset",
31104 underline_minimum_offset,
31105 doc: /* Minimum distance between baseline and underline.
31106 This can improve legibility of underlined text at small font sizes,
31107 particularly when using variable `x-use-underline-position-properties'
31108 with fonts that specify an UNDERLINE_POSITION relatively close to the
31109 baseline. The default value is 1. */);
31110 underline_minimum_offset = 1;
31112 DEFVAR_BOOL ("display-hourglass", display_hourglass_p,
31113 doc: /* Non-nil means show an hourglass pointer, when Emacs is busy.
31114 This feature only works when on a window system that can change
31115 cursor shapes. */);
31116 display_hourglass_p = 1;
31118 DEFVAR_LISP ("hourglass-delay", Vhourglass_delay,
31119 doc: /* Seconds to wait before displaying an hourglass pointer when Emacs is busy. */);
31120 Vhourglass_delay = make_number (DEFAULT_HOURGLASS_DELAY);
31122 #ifdef HAVE_WINDOW_SYSTEM
31123 hourglass_atimer = NULL;
31124 hourglass_shown_p = 0;
31125 #endif /* HAVE_WINDOW_SYSTEM */
31127 /* Name of the face used to display glyphless characters. */
31128 DEFSYM (Qglyphless_char, "glyphless-char");
31130 /* Method symbols for Vglyphless_char_display. */
31131 DEFSYM (Qhex_code, "hex-code");
31132 DEFSYM (Qempty_box, "empty-box");
31133 DEFSYM (Qthin_space, "thin-space");
31134 DEFSYM (Qzero_width, "zero-width");
31136 DEFVAR_LISP ("pre-redisplay-function", Vpre_redisplay_function,
31137 doc: /* Function run just before redisplay.
31138 It is called with one argument, which is the set of windows that are to
31139 be redisplayed. This set can be nil (meaning, only the selected window),
31140 or t (meaning all windows). */);
31141 Vpre_redisplay_function = intern ("ignore");
31143 /* Symbol for the purpose of Vglyphless_char_display. */
31144 DEFSYM (Qglyphless_char_display, "glyphless-char-display");
31145 Fput (Qglyphless_char_display, Qchar_table_extra_slots, make_number (1));
31147 DEFVAR_LISP ("glyphless-char-display", Vglyphless_char_display,
31148 doc: /* Char-table defining glyphless characters.
31149 Each element, if non-nil, should be one of the following:
31150 an ASCII acronym string: display this string in a box
31151 `hex-code': display the hexadecimal code of a character in a box
31152 `empty-box': display as an empty box
31153 `thin-space': display as 1-pixel width space
31154 `zero-width': don't display
31155 An element may also be a cons cell (GRAPHICAL . TEXT), which specifies the
31156 display method for graphical terminals and text terminals respectively.
31157 GRAPHICAL and TEXT should each have one of the values listed above.
31159 The char-table has one extra slot to control the display of a character for
31160 which no font is found. This slot only takes effect on graphical terminals.
31161 Its value should be an ASCII acronym string, `hex-code', `empty-box', or
31162 `thin-space'. The default is `empty-box'.
31164 If a character has a non-nil entry in an active display table, the
31165 display table takes effect; in this case, Emacs does not consult
31166 `glyphless-char-display' at all. */);
31167 Vglyphless_char_display = Fmake_char_table (Qglyphless_char_display, Qnil);
31168 Fset_char_table_extra_slot (Vglyphless_char_display, make_number (0),
31169 Qempty_box);
31171 DEFVAR_LISP ("debug-on-message", Vdebug_on_message,
31172 doc: /* If non-nil, debug if a message matching this regexp is displayed. */);
31173 Vdebug_on_message = Qnil;
31175 DEFVAR_LISP ("redisplay--all-windows-cause", Vredisplay__all_windows_cause,
31176 doc: /* */);
31177 Vredisplay__all_windows_cause
31178 = Fmake_vector (make_number (100), make_number (0));
31180 DEFVAR_LISP ("redisplay--mode-lines-cause", Vredisplay__mode_lines_cause,
31181 doc: /* */);
31182 Vredisplay__mode_lines_cause
31183 = Fmake_vector (make_number (100), make_number (0));
31187 /* Initialize this module when Emacs starts. */
31189 void
31190 init_xdisp (void)
31192 CHARPOS (this_line_start_pos) = 0;
31194 if (!noninteractive)
31196 struct window *m = XWINDOW (minibuf_window);
31197 Lisp_Object frame = m->frame;
31198 struct frame *f = XFRAME (frame);
31199 Lisp_Object root = FRAME_ROOT_WINDOW (f);
31200 struct window *r = XWINDOW (root);
31201 int i;
31203 echo_area_window = minibuf_window;
31205 r->top_line = FRAME_TOP_MARGIN (f);
31206 r->pixel_top = r->top_line * FRAME_LINE_HEIGHT (f);
31207 r->total_cols = FRAME_COLS (f);
31208 r->pixel_width = r->total_cols * FRAME_COLUMN_WIDTH (f);
31209 r->total_lines = FRAME_TOTAL_LINES (f) - 1 - FRAME_TOP_MARGIN (f);
31210 r->pixel_height = r->total_lines * FRAME_LINE_HEIGHT (f);
31212 m->top_line = FRAME_TOTAL_LINES (f) - 1;
31213 m->pixel_top = m->top_line * FRAME_LINE_HEIGHT (f);
31214 m->total_cols = FRAME_COLS (f);
31215 m->pixel_width = m->total_cols * FRAME_COLUMN_WIDTH (f);
31216 m->total_lines = 1;
31217 m->pixel_height = m->total_lines * FRAME_LINE_HEIGHT (f);
31219 scratch_glyph_row.glyphs[TEXT_AREA] = scratch_glyphs;
31220 scratch_glyph_row.glyphs[TEXT_AREA + 1]
31221 = scratch_glyphs + MAX_SCRATCH_GLYPHS;
31223 /* The default ellipsis glyphs `...'. */
31224 for (i = 0; i < 3; ++i)
31225 default_invis_vector[i] = make_number ('.');
31229 /* Allocate the buffer for frame titles.
31230 Also used for `format-mode-line'. */
31231 int size = 100;
31232 mode_line_noprop_buf = xmalloc (size);
31233 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
31234 mode_line_noprop_ptr = mode_line_noprop_buf;
31235 mode_line_target = MODE_LINE_DISPLAY;
31238 help_echo_showing_p = 0;
31241 #ifdef HAVE_WINDOW_SYSTEM
31243 /* Platform-independent portion of hourglass implementation. */
31245 /* Timer function of hourglass_atimer. */
31247 static void
31248 show_hourglass (struct atimer *timer)
31250 /* The timer implementation will cancel this timer automatically
31251 after this function has run. Set hourglass_atimer to null
31252 so that we know the timer doesn't have to be canceled. */
31253 hourglass_atimer = NULL;
31255 if (!hourglass_shown_p)
31257 Lisp_Object tail, frame;
31259 block_input ();
31261 FOR_EACH_FRAME (tail, frame)
31263 struct frame *f = XFRAME (frame);
31265 if (FRAME_LIVE_P (f) && FRAME_WINDOW_P (f)
31266 && FRAME_RIF (f)->show_hourglass)
31267 FRAME_RIF (f)->show_hourglass (f);
31270 hourglass_shown_p = 1;
31271 unblock_input ();
31275 /* Cancel a currently active hourglass timer, and start a new one. */
31277 void
31278 start_hourglass (void)
31280 struct timespec delay;
31282 cancel_hourglass ();
31284 if (INTEGERP (Vhourglass_delay)
31285 && XINT (Vhourglass_delay) > 0)
31286 delay = make_timespec (min (XINT (Vhourglass_delay),
31287 TYPE_MAXIMUM (time_t)),
31289 else if (FLOATP (Vhourglass_delay)
31290 && XFLOAT_DATA (Vhourglass_delay) > 0)
31291 delay = dtotimespec (XFLOAT_DATA (Vhourglass_delay));
31292 else
31293 delay = make_timespec (DEFAULT_HOURGLASS_DELAY, 0);
31295 hourglass_atimer = start_atimer (ATIMER_RELATIVE, delay,
31296 show_hourglass, NULL);
31299 /* Cancel the hourglass cursor timer if active, hide a busy cursor if
31300 shown. */
31302 void
31303 cancel_hourglass (void)
31305 if (hourglass_atimer)
31307 cancel_atimer (hourglass_atimer);
31308 hourglass_atimer = NULL;
31311 if (hourglass_shown_p)
31313 Lisp_Object tail, frame;
31315 block_input ();
31317 FOR_EACH_FRAME (tail, frame)
31319 struct frame *f = XFRAME (frame);
31321 if (FRAME_LIVE_P (f) && FRAME_WINDOW_P (f)
31322 && FRAME_RIF (f)->hide_hourglass)
31323 FRAME_RIF (f)->hide_hourglass (f);
31324 #ifdef HAVE_NTGUI
31325 /* No cursors on non GUI frames - restore to stock arrow cursor. */
31326 else if (!FRAME_W32_P (f))
31327 w32_arrow_cursor ();
31328 #endif
31331 hourglass_shown_p = 0;
31332 unblock_input ();
31336 #endif /* HAVE_WINDOW_SYSTEM */