(eww): Don't interpret "org/foo" as an URL.
[emacs.git] / src / xdisp.c
blob36babfa74dbfe97f4b6b93be83d2d8f56fec8ec2
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 name of the property. */
626 struct Lisp_Symbol *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 {XSYMBOL_INIT (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 {XSYMBOL_INIT (Qface), FACE_PROP_IDX, handle_face_prop},
651 {XSYMBOL_INIT (Qdisplay), DISPLAY_PROP_IDX, handle_display_prop},
652 {XSYMBOL_INIT (Qinvisible), INVISIBLE_PROP_IDX, handle_invisible_prop},
653 {XSYMBOL_INIT (Qcomposition), COMPOSITION_PROP_IDX, handle_composition_prop},
654 {NULL, 0, NULL}
657 /* Value is the position described by X. If X is a marker, value is
658 the marker_position of X. Otherwise, value is X. */
660 #define COERCE_MARKER(X) (MARKERP ((X)) ? Fmarker_position (X) : (X))
662 /* Enumeration returned by some move_it_.* functions internally. */
664 enum move_it_result
666 /* Not used. Undefined value. */
667 MOVE_UNDEFINED,
669 /* Move ended at the requested buffer position or ZV. */
670 MOVE_POS_MATCH_OR_ZV,
672 /* Move ended at the requested X pixel position. */
673 MOVE_X_REACHED,
675 /* Move within a line ended at the end of a line that must be
676 continued. */
677 MOVE_LINE_CONTINUED,
679 /* Move within a line ended at the end of a line that would
680 be displayed truncated. */
681 MOVE_LINE_TRUNCATED,
683 /* Move within a line ended at a line end. */
684 MOVE_NEWLINE_OR_CR
687 /* This counter is used to clear the face cache every once in a while
688 in redisplay_internal. It is incremented for each redisplay.
689 Every CLEAR_FACE_CACHE_COUNT full redisplays, the face cache is
690 cleared. */
692 #define CLEAR_FACE_CACHE_COUNT 500
693 static int clear_face_cache_count;
695 /* Similarly for the image cache. */
697 #ifdef HAVE_WINDOW_SYSTEM
698 #define CLEAR_IMAGE_CACHE_COUNT 101
699 static int clear_image_cache_count;
701 /* Null glyph slice */
702 static struct glyph_slice null_glyph_slice = { 0, 0, 0, 0 };
703 #endif
705 /* True while redisplay_internal is in progress. */
707 bool redisplaying_p;
709 /* If a string, XTread_socket generates an event to display that string.
710 (The display is done in read_char.) */
712 Lisp_Object help_echo_string;
713 Lisp_Object help_echo_window;
714 Lisp_Object help_echo_object;
715 ptrdiff_t help_echo_pos;
717 /* Temporary variable for XTread_socket. */
719 Lisp_Object previous_help_echo_string;
721 /* Platform-independent portion of hourglass implementation. */
723 #ifdef HAVE_WINDOW_SYSTEM
725 /* Non-zero means an hourglass cursor is currently shown. */
726 static bool hourglass_shown_p;
728 /* If non-null, an asynchronous timer that, when it expires, displays
729 an hourglass cursor on all frames. */
730 static struct atimer *hourglass_atimer;
732 #endif /* HAVE_WINDOW_SYSTEM */
734 /* Default number of seconds to wait before displaying an hourglass
735 cursor. */
736 #define DEFAULT_HOURGLASS_DELAY 1
738 #ifdef HAVE_WINDOW_SYSTEM
740 /* Default pixel width of `thin-space' display method. */
741 #define THIN_SPACE_WIDTH 1
743 #endif /* HAVE_WINDOW_SYSTEM */
745 /* Function prototypes. */
747 static void setup_for_ellipsis (struct it *, int);
748 static void set_iterator_to_next (struct it *, int);
749 static void mark_window_display_accurate_1 (struct window *, int);
750 static int single_display_spec_string_p (Lisp_Object, Lisp_Object);
751 static int display_prop_string_p (Lisp_Object, Lisp_Object);
752 static int row_for_charpos_p (struct glyph_row *, ptrdiff_t);
753 static int cursor_row_p (struct glyph_row *);
754 static int redisplay_mode_lines (Lisp_Object, bool);
755 static char *decode_mode_spec_coding (Lisp_Object, char *, int);
757 static Lisp_Object get_it_property (struct it *it, Lisp_Object prop);
759 static void handle_line_prefix (struct it *);
761 static void pint2str (char *, int, ptrdiff_t);
762 static void pint2hrstr (char *, int, ptrdiff_t);
763 static struct text_pos run_window_scroll_functions (Lisp_Object,
764 struct text_pos);
765 static int text_outside_line_unchanged_p (struct window *,
766 ptrdiff_t, ptrdiff_t);
767 static void store_mode_line_noprop_char (char);
768 static int store_mode_line_noprop (const char *, int, int);
769 static void handle_stop (struct it *);
770 static void handle_stop_backwards (struct it *, ptrdiff_t);
771 static void vmessage (const char *, va_list) ATTRIBUTE_FORMAT_PRINTF (1, 0);
772 static void ensure_echo_area_buffers (void);
773 static void unwind_with_echo_area_buffer (Lisp_Object);
774 static Lisp_Object with_echo_area_buffer_unwind_data (struct window *);
775 static int with_echo_area_buffer (struct window *, int,
776 int (*) (ptrdiff_t, Lisp_Object),
777 ptrdiff_t, Lisp_Object);
778 static void clear_garbaged_frames (void);
779 static int current_message_1 (ptrdiff_t, Lisp_Object);
780 static int truncate_message_1 (ptrdiff_t, Lisp_Object);
781 static void set_message (Lisp_Object);
782 static int set_message_1 (ptrdiff_t, Lisp_Object);
783 static int display_echo_area (struct window *);
784 static int display_echo_area_1 (ptrdiff_t, Lisp_Object);
785 static int resize_mini_window_1 (ptrdiff_t, Lisp_Object);
786 static void unwind_redisplay (void);
787 static int string_char_and_length (const unsigned char *, int *);
788 static struct text_pos display_prop_end (struct it *, Lisp_Object,
789 struct text_pos);
790 static int compute_window_start_on_continuation_line (struct window *);
791 static void insert_left_trunc_glyphs (struct it *);
792 static struct glyph_row *get_overlay_arrow_glyph_row (struct window *,
793 Lisp_Object);
794 static void extend_face_to_end_of_line (struct it *);
795 static int append_space_for_newline (struct it *, int);
796 static int cursor_row_fully_visible_p (struct window *, int, int);
797 static int try_scrolling (Lisp_Object, int, ptrdiff_t, ptrdiff_t, int, int);
798 static int try_cursor_movement (Lisp_Object, struct text_pos, int *);
799 static int trailing_whitespace_p (ptrdiff_t);
800 static intmax_t message_log_check_duplicate (ptrdiff_t, ptrdiff_t);
801 static void push_it (struct it *, struct text_pos *);
802 static void iterate_out_of_display_property (struct it *);
803 static void pop_it (struct it *);
804 static void sync_frame_with_window_matrix_rows (struct window *);
805 static void redisplay_internal (void);
806 static bool echo_area_display (bool);
807 static void redisplay_windows (Lisp_Object);
808 static void redisplay_window (Lisp_Object, bool);
809 static Lisp_Object redisplay_window_error (Lisp_Object);
810 static Lisp_Object redisplay_window_0 (Lisp_Object);
811 static Lisp_Object redisplay_window_1 (Lisp_Object);
812 static int set_cursor_from_row (struct window *, struct glyph_row *,
813 struct glyph_matrix *, ptrdiff_t, ptrdiff_t,
814 int, int);
815 static int update_menu_bar (struct frame *, int, int);
816 static int try_window_reusing_current_matrix (struct window *);
817 static int try_window_id (struct window *);
818 static int display_line (struct it *);
819 static int display_mode_lines (struct window *);
820 static int display_mode_line (struct window *, enum face_id, Lisp_Object);
821 static int display_mode_element (struct it *, int, int, int, Lisp_Object, Lisp_Object, int);
822 static int store_mode_line_string (const char *, Lisp_Object, int, int, int, Lisp_Object);
823 static const char *decode_mode_spec (struct window *, int, int, Lisp_Object *);
824 static void display_menu_bar (struct window *);
825 static ptrdiff_t display_count_lines (ptrdiff_t, ptrdiff_t, ptrdiff_t,
826 ptrdiff_t *);
827 static int display_string (const char *, Lisp_Object, Lisp_Object,
828 ptrdiff_t, ptrdiff_t, struct it *, int, int, int, int);
829 static void compute_line_metrics (struct it *);
830 static void run_redisplay_end_trigger_hook (struct it *);
831 static int get_overlay_strings (struct it *, ptrdiff_t);
832 static int get_overlay_strings_1 (struct it *, ptrdiff_t, int);
833 static void next_overlay_string (struct it *);
834 static void reseat (struct it *, struct text_pos, int);
835 static void reseat_1 (struct it *, struct text_pos, int);
836 static void back_to_previous_visible_line_start (struct it *);
837 static void reseat_at_next_visible_line_start (struct it *, int);
838 static int next_element_from_ellipsis (struct it *);
839 static int next_element_from_display_vector (struct it *);
840 static int next_element_from_string (struct it *);
841 static int next_element_from_c_string (struct it *);
842 static int next_element_from_buffer (struct it *);
843 static int next_element_from_composition (struct it *);
844 static int next_element_from_image (struct it *);
845 static int next_element_from_stretch (struct it *);
846 static void load_overlay_strings (struct it *, ptrdiff_t);
847 static int init_from_display_pos (struct it *, struct window *,
848 struct display_pos *);
849 static void reseat_to_string (struct it *, const char *,
850 Lisp_Object, ptrdiff_t, ptrdiff_t, int, int);
851 static int get_next_display_element (struct it *);
852 static enum move_it_result
853 move_it_in_display_line_to (struct it *, ptrdiff_t, int,
854 enum move_operation_enum);
855 static void get_visually_first_element (struct it *);
856 static void init_to_row_start (struct it *, struct window *,
857 struct glyph_row *);
858 static int init_to_row_end (struct it *, struct window *,
859 struct glyph_row *);
860 static void back_to_previous_line_start (struct it *);
861 static int forward_to_next_line_start (struct it *, int *, struct bidi_it *);
862 static struct text_pos string_pos_nchars_ahead (struct text_pos,
863 Lisp_Object, ptrdiff_t);
864 static struct text_pos string_pos (ptrdiff_t, Lisp_Object);
865 static struct text_pos c_string_pos (ptrdiff_t, const char *, bool);
866 static ptrdiff_t number_of_chars (const char *, bool);
867 static void compute_stop_pos (struct it *);
868 static void compute_string_pos (struct text_pos *, struct text_pos,
869 Lisp_Object);
870 static int face_before_or_after_it_pos (struct it *, int);
871 static ptrdiff_t next_overlay_change (ptrdiff_t);
872 static int handle_display_spec (struct it *, Lisp_Object, Lisp_Object,
873 Lisp_Object, struct text_pos *, ptrdiff_t, int);
874 static int handle_single_display_spec (struct it *, Lisp_Object,
875 Lisp_Object, Lisp_Object,
876 struct text_pos *, ptrdiff_t, int, int);
877 static int underlying_face_id (struct it *);
878 static int in_ellipses_for_invisible_text_p (struct display_pos *,
879 struct window *);
881 #define face_before_it_pos(IT) face_before_or_after_it_pos ((IT), 1)
882 #define face_after_it_pos(IT) face_before_or_after_it_pos ((IT), 0)
884 #ifdef HAVE_WINDOW_SYSTEM
886 static void x_consider_frame_title (Lisp_Object);
887 static void update_tool_bar (struct frame *, int);
888 static int redisplay_tool_bar (struct frame *);
889 static void x_draw_bottom_divider (struct window *w);
890 static void notice_overwritten_cursor (struct window *,
891 enum glyph_row_area,
892 int, int, int, int);
893 static void append_stretch_glyph (struct it *, Lisp_Object,
894 int, int, int);
897 #endif /* HAVE_WINDOW_SYSTEM */
899 static void produce_special_glyphs (struct it *, enum display_element_type);
900 static void show_mouse_face (Mouse_HLInfo *, enum draw_glyphs_face);
901 static bool coords_in_mouse_face_p (struct window *, int, int);
905 /***********************************************************************
906 Window display dimensions
907 ***********************************************************************/
909 /* Return the bottom boundary y-position for text lines in window W.
910 This is the first y position at which a line cannot start.
911 It is relative to the top of the window.
913 This is the height of W minus the height of a mode line, if any. */
916 window_text_bottom_y (struct window *w)
918 int height = WINDOW_PIXEL_HEIGHT (w);
920 height -= WINDOW_BOTTOM_DIVIDER_WIDTH (w);
922 if (WINDOW_WANTS_MODELINE_P (w))
923 height -= CURRENT_MODE_LINE_HEIGHT (w);
925 height -= WINDOW_SCROLL_BAR_AREA_HEIGHT (w);
927 return height;
930 /* Return the pixel width of display area AREA of window W.
931 ANY_AREA means return the total width of W, not including
932 fringes to the left and right of the window. */
935 window_box_width (struct window *w, enum glyph_row_area area)
937 int width = w->pixel_width;
939 if (!w->pseudo_window_p)
941 width -= WINDOW_SCROLL_BAR_AREA_WIDTH (w);
942 width -= WINDOW_RIGHT_DIVIDER_WIDTH (w);
944 if (area == TEXT_AREA)
945 width -= (WINDOW_MARGINS_WIDTH (w)
946 + WINDOW_FRINGES_WIDTH (w));
947 else if (area == LEFT_MARGIN_AREA)
948 width = WINDOW_LEFT_MARGIN_WIDTH (w);
949 else if (area == RIGHT_MARGIN_AREA)
950 width = WINDOW_RIGHT_MARGIN_WIDTH (w);
953 /* With wide margins, fringes, etc. we might end up with a negative
954 width, correct that here. */
955 return max (0, width);
959 /* Return the pixel height of the display area of window W, not
960 including mode lines of W, if any. */
963 window_box_height (struct window *w)
965 struct frame *f = XFRAME (w->frame);
966 int height = WINDOW_PIXEL_HEIGHT (w);
968 eassert (height >= 0);
970 height -= WINDOW_BOTTOM_DIVIDER_WIDTH (w);
971 height -= WINDOW_SCROLL_BAR_AREA_HEIGHT (w);
973 /* Note: the code below that determines the mode-line/header-line
974 height is essentially the same as that contained in the macro
975 CURRENT_{MODE,HEADER}_LINE_HEIGHT, except that it checks whether
976 the appropriate glyph row has its `mode_line_p' flag set,
977 and if it doesn't, uses estimate_mode_line_height instead. */
979 if (WINDOW_WANTS_MODELINE_P (w))
981 struct glyph_row *ml_row
982 = (w->current_matrix && w->current_matrix->rows
983 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
984 : 0);
985 if (ml_row && ml_row->mode_line_p)
986 height -= ml_row->height;
987 else
988 height -= estimate_mode_line_height (f, CURRENT_MODE_LINE_FACE_ID (w));
991 if (WINDOW_WANTS_HEADER_LINE_P (w))
993 struct glyph_row *hl_row
994 = (w->current_matrix && w->current_matrix->rows
995 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
996 : 0);
997 if (hl_row && hl_row->mode_line_p)
998 height -= hl_row->height;
999 else
1000 height -= estimate_mode_line_height (f, HEADER_LINE_FACE_ID);
1003 /* With a very small font and a mode-line that's taller than
1004 default, we might end up with a negative height. */
1005 return max (0, height);
1008 /* Return the window-relative coordinate of the left edge of display
1009 area AREA of window W. ANY_AREA means return the left edge of the
1010 whole window, to the right of the left fringe of W. */
1013 window_box_left_offset (struct window *w, enum glyph_row_area area)
1015 int x;
1017 if (w->pseudo_window_p)
1018 return 0;
1020 x = WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
1022 if (area == TEXT_AREA)
1023 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1024 + window_box_width (w, LEFT_MARGIN_AREA));
1025 else if (area == RIGHT_MARGIN_AREA)
1026 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1027 + window_box_width (w, LEFT_MARGIN_AREA)
1028 + window_box_width (w, TEXT_AREA)
1029 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
1031 : WINDOW_RIGHT_FRINGE_WIDTH (w)));
1032 else if (area == LEFT_MARGIN_AREA
1033 && WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w))
1034 x += WINDOW_LEFT_FRINGE_WIDTH (w);
1036 /* Don't return more than the window's pixel width. */
1037 return min (x, w->pixel_width);
1041 /* Return the window-relative coordinate of the right edge of display
1042 area AREA of window W. ANY_AREA means return the right edge of the
1043 whole window, to the left of the right fringe of W. */
1045 static int
1046 window_box_right_offset (struct window *w, enum glyph_row_area area)
1048 /* Don't return more than the window's pixel width. */
1049 return min (window_box_left_offset (w, area) + window_box_width (w, area),
1050 w->pixel_width);
1053 /* Return the frame-relative coordinate of the left edge of display
1054 area AREA of window W. ANY_AREA means return the left edge of the
1055 whole window, to the right of the left fringe of W. */
1058 window_box_left (struct window *w, enum glyph_row_area area)
1060 struct frame *f = XFRAME (w->frame);
1061 int x;
1063 if (w->pseudo_window_p)
1064 return FRAME_INTERNAL_BORDER_WIDTH (f);
1066 x = (WINDOW_LEFT_EDGE_X (w)
1067 + window_box_left_offset (w, area));
1069 return x;
1073 /* Return the frame-relative coordinate of the right edge of display
1074 area AREA of window W. ANY_AREA means return the right edge of the
1075 whole window, to the left of the right fringe of W. */
1078 window_box_right (struct window *w, enum glyph_row_area area)
1080 return window_box_left (w, area) + window_box_width (w, area);
1083 /* Get the bounding box of the display area AREA of window W, without
1084 mode lines, in frame-relative coordinates. ANY_AREA means the
1085 whole window, not including the left and right fringes of
1086 the window. Return in *BOX_X and *BOX_Y the frame-relative pixel
1087 coordinates of the upper-left corner of the box. Return in
1088 *BOX_WIDTH, and *BOX_HEIGHT the pixel width and height of the box. */
1090 void
1091 window_box (struct window *w, enum glyph_row_area area, int *box_x,
1092 int *box_y, int *box_width, int *box_height)
1094 if (box_width)
1095 *box_width = window_box_width (w, area);
1096 if (box_height)
1097 *box_height = window_box_height (w);
1098 if (box_x)
1099 *box_x = window_box_left (w, area);
1100 if (box_y)
1102 *box_y = WINDOW_TOP_EDGE_Y (w);
1103 if (WINDOW_WANTS_HEADER_LINE_P (w))
1104 *box_y += CURRENT_HEADER_LINE_HEIGHT (w);
1108 #ifdef HAVE_WINDOW_SYSTEM
1110 /* Get the bounding box of the display area AREA of window W, without
1111 mode lines and both fringes of the window. Return in *TOP_LEFT_X
1112 and TOP_LEFT_Y the frame-relative pixel coordinates of the
1113 upper-left corner of the box. Return in *BOTTOM_RIGHT_X, and
1114 *BOTTOM_RIGHT_Y the coordinates of the bottom-right corner of the
1115 box. */
1117 static void
1118 window_box_edges (struct window *w, int *top_left_x, int *top_left_y,
1119 int *bottom_right_x, int *bottom_right_y)
1121 window_box (w, ANY_AREA, top_left_x, top_left_y,
1122 bottom_right_x, bottom_right_y);
1123 *bottom_right_x += *top_left_x;
1124 *bottom_right_y += *top_left_y;
1127 #endif /* HAVE_WINDOW_SYSTEM */
1129 /***********************************************************************
1130 Utilities
1131 ***********************************************************************/
1133 /* Return the bottom y-position of the line the iterator IT is in.
1134 This can modify IT's settings. */
1137 line_bottom_y (struct it *it)
1139 int line_height = it->max_ascent + it->max_descent;
1140 int line_top_y = it->current_y;
1142 if (line_height == 0)
1144 if (last_height)
1145 line_height = last_height;
1146 else if (IT_CHARPOS (*it) < ZV)
1148 move_it_by_lines (it, 1);
1149 line_height = (it->max_ascent || it->max_descent
1150 ? it->max_ascent + it->max_descent
1151 : last_height);
1153 else
1155 struct glyph_row *row = it->glyph_row;
1157 /* Use the default character height. */
1158 it->glyph_row = NULL;
1159 it->what = IT_CHARACTER;
1160 it->c = ' ';
1161 it->len = 1;
1162 PRODUCE_GLYPHS (it);
1163 line_height = it->ascent + it->descent;
1164 it->glyph_row = row;
1168 return line_top_y + line_height;
1171 DEFUN ("line-pixel-height", Fline_pixel_height,
1172 Sline_pixel_height, 0, 0, 0,
1173 doc: /* Return height in pixels of text line in the selected window.
1175 Value is the height in pixels of the line at point. */)
1176 (void)
1178 struct it it;
1179 struct text_pos pt;
1180 struct window *w = XWINDOW (selected_window);
1181 struct buffer *old_buffer = NULL;
1182 Lisp_Object result;
1184 if (XBUFFER (w->contents) != current_buffer)
1186 old_buffer = current_buffer;
1187 set_buffer_internal_1 (XBUFFER (w->contents));
1189 SET_TEXT_POS (pt, PT, PT_BYTE);
1190 start_display (&it, w, pt);
1191 it.vpos = it.current_y = 0;
1192 last_height = 0;
1193 result = make_number (line_bottom_y (&it));
1194 if (old_buffer)
1195 set_buffer_internal_1 (old_buffer);
1197 return result;
1200 /* Return the default pixel height of text lines in window W. The
1201 value is the canonical height of the W frame's default font, plus
1202 any extra space required by the line-spacing variable or frame
1203 parameter.
1205 Implementation note: this ignores any line-spacing text properties
1206 put on the newline characters. This is because those properties
1207 only affect the _screen_ line ending in the newline (i.e., in a
1208 continued line, only the last screen line will be affected), which
1209 means only a small number of lines in a buffer can ever use this
1210 feature. Since this function is used to compute the default pixel
1211 equivalent of text lines in a window, we can safely ignore those
1212 few lines. For the same reasons, we ignore the line-height
1213 properties. */
1215 default_line_pixel_height (struct window *w)
1217 struct frame *f = WINDOW_XFRAME (w);
1218 int height = FRAME_LINE_HEIGHT (f);
1220 if (!FRAME_INITIAL_P (f) && BUFFERP (w->contents))
1222 struct buffer *b = XBUFFER (w->contents);
1223 Lisp_Object val = BVAR (b, extra_line_spacing);
1225 if (NILP (val))
1226 val = BVAR (&buffer_defaults, extra_line_spacing);
1227 if (!NILP (val))
1229 if (RANGED_INTEGERP (0, val, INT_MAX))
1230 height += XFASTINT (val);
1231 else if (FLOATP (val))
1233 int addon = XFLOAT_DATA (val) * height + 0.5;
1235 if (addon >= 0)
1236 height += addon;
1239 else
1240 height += f->extra_line_spacing;
1243 return height;
1246 /* Subroutine of pos_visible_p below. Extracts a display string, if
1247 any, from the display spec given as its argument. */
1248 static Lisp_Object
1249 string_from_display_spec (Lisp_Object spec)
1251 if (CONSP (spec))
1253 while (CONSP (spec))
1255 if (STRINGP (XCAR (spec)))
1256 return XCAR (spec);
1257 spec = XCDR (spec);
1260 else if (VECTORP (spec))
1262 ptrdiff_t i;
1264 for (i = 0; i < ASIZE (spec); i++)
1266 if (STRINGP (AREF (spec, i)))
1267 return AREF (spec, i);
1269 return Qnil;
1272 return spec;
1276 /* Limit insanely large values of W->hscroll on frame F to the largest
1277 value that will still prevent first_visible_x and last_visible_x of
1278 'struct it' from overflowing an int. */
1279 static int
1280 window_hscroll_limited (struct window *w, struct frame *f)
1282 ptrdiff_t window_hscroll = w->hscroll;
1283 int window_text_width = window_box_width (w, TEXT_AREA);
1284 int colwidth = FRAME_COLUMN_WIDTH (f);
1286 if (window_hscroll > (INT_MAX - window_text_width) / colwidth - 1)
1287 window_hscroll = (INT_MAX - window_text_width) / colwidth - 1;
1289 return window_hscroll;
1292 /* Return 1 if position CHARPOS is visible in window W.
1293 CHARPOS < 0 means return info about WINDOW_END position.
1294 If visible, set *X and *Y to pixel coordinates of top left corner.
1295 Set *RTOP and *RBOT to pixel height of an invisible area of glyph at POS.
1296 Set *ROWH and *VPOS to row's visible height and VPOS (row number). */
1299 pos_visible_p (struct window *w, ptrdiff_t charpos, int *x, int *y,
1300 int *rtop, int *rbot, int *rowh, int *vpos)
1302 struct it it;
1303 void *itdata = bidi_shelve_cache ();
1304 struct text_pos top;
1305 int visible_p = 0;
1306 struct buffer *old_buffer = NULL;
1307 bool r2l = false;
1309 if (FRAME_INITIAL_P (XFRAME (WINDOW_FRAME (w))))
1310 return visible_p;
1312 if (XBUFFER (w->contents) != current_buffer)
1314 old_buffer = current_buffer;
1315 set_buffer_internal_1 (XBUFFER (w->contents));
1318 SET_TEXT_POS_FROM_MARKER (top, w->start);
1319 /* Scrolling a minibuffer window via scroll bar when the echo area
1320 shows long text sometimes resets the minibuffer contents behind
1321 our backs. */
1322 if (CHARPOS (top) > ZV)
1323 SET_TEXT_POS (top, BEGV, BEGV_BYTE);
1325 /* Compute exact mode line heights. */
1326 if (WINDOW_WANTS_MODELINE_P (w))
1327 w->mode_line_height
1328 = display_mode_line (w, CURRENT_MODE_LINE_FACE_ID (w),
1329 BVAR (current_buffer, mode_line_format));
1331 if (WINDOW_WANTS_HEADER_LINE_P (w))
1332 w->header_line_height
1333 = display_mode_line (w, HEADER_LINE_FACE_ID,
1334 BVAR (current_buffer, header_line_format));
1336 start_display (&it, w, top);
1337 move_it_to (&it, charpos, -1, it.last_visible_y - 1, -1,
1338 (charpos >= 0 ? MOVE_TO_POS : 0) | MOVE_TO_Y);
1340 if (charpos >= 0
1341 && (((!it.bidi_p || it.bidi_it.scan_dir != -1)
1342 && IT_CHARPOS (it) >= charpos)
1343 /* When scanning backwards under bidi iteration, move_it_to
1344 stops at or _before_ CHARPOS, because it stops at or to
1345 the _right_ of the character at CHARPOS. */
1346 || (it.bidi_p && it.bidi_it.scan_dir == -1
1347 && IT_CHARPOS (it) <= charpos)))
1349 /* We have reached CHARPOS, or passed it. How the call to
1350 move_it_to can overshoot: (i) If CHARPOS is on invisible text
1351 or covered by a display property, move_it_to stops at the end
1352 of the invisible text, to the right of CHARPOS. (ii) If
1353 CHARPOS is in a display vector, move_it_to stops on its last
1354 glyph. */
1355 int top_x = it.current_x;
1356 int top_y = it.current_y;
1357 int window_top_y = WINDOW_HEADER_LINE_HEIGHT (w);
1358 int bottom_y;
1359 struct it save_it;
1360 void *save_it_data = NULL;
1362 /* Calling line_bottom_y may change it.method, it.position, etc. */
1363 SAVE_IT (save_it, it, save_it_data);
1364 last_height = 0;
1365 bottom_y = line_bottom_y (&it);
1366 if (top_y < window_top_y)
1367 visible_p = bottom_y > window_top_y;
1368 else if (top_y < it.last_visible_y)
1369 visible_p = 1;
1370 if (bottom_y >= it.last_visible_y
1371 && it.bidi_p && it.bidi_it.scan_dir == -1
1372 && IT_CHARPOS (it) < charpos)
1374 /* When the last line of the window is scanned backwards
1375 under bidi iteration, we could be duped into thinking
1376 that we have passed CHARPOS, when in fact move_it_to
1377 simply stopped short of CHARPOS because it reached
1378 last_visible_y. To see if that's what happened, we call
1379 move_it_to again with a slightly larger vertical limit,
1380 and see if it actually moved vertically; if it did, we
1381 didn't really reach CHARPOS, which is beyond window end. */
1382 /* Why 10? because we don't know how many canonical lines
1383 will the height of the next line(s) be. So we guess. */
1384 int ten_more_lines = 10 * default_line_pixel_height (w);
1386 move_it_to (&it, charpos, -1, bottom_y + ten_more_lines, -1,
1387 MOVE_TO_POS | MOVE_TO_Y);
1388 if (it.current_y > top_y)
1389 visible_p = 0;
1392 RESTORE_IT (&it, &save_it, save_it_data);
1393 if (visible_p)
1395 if (it.method == GET_FROM_DISPLAY_VECTOR)
1397 /* We stopped on the last glyph of a display vector.
1398 Try and recompute. Hack alert! */
1399 if (charpos < 2 || top.charpos >= charpos)
1400 top_x = it.glyph_row->x;
1401 else
1403 struct it it2, it2_prev;
1404 /* The idea is to get to the previous buffer
1405 position, consume the character there, and use
1406 the pixel coordinates we get after that. But if
1407 the previous buffer position is also displayed
1408 from a display vector, we need to consume all of
1409 the glyphs from that display vector. */
1410 start_display (&it2, w, top);
1411 move_it_to (&it2, charpos - 1, -1, -1, -1, MOVE_TO_POS);
1412 /* If we didn't get to CHARPOS - 1, there's some
1413 replacing display property at that position, and
1414 we stopped after it. That is exactly the place
1415 whose coordinates we want. */
1416 if (IT_CHARPOS (it2) != charpos - 1)
1417 it2_prev = it2;
1418 else
1420 /* Iterate until we get out of the display
1421 vector that displays the character at
1422 CHARPOS - 1. */
1423 do {
1424 get_next_display_element (&it2);
1425 PRODUCE_GLYPHS (&it2);
1426 it2_prev = it2;
1427 set_iterator_to_next (&it2, 1);
1428 } while (it2.method == GET_FROM_DISPLAY_VECTOR
1429 && IT_CHARPOS (it2) < charpos);
1431 if (ITERATOR_AT_END_OF_LINE_P (&it2_prev)
1432 || it2_prev.current_x > it2_prev.last_visible_x)
1433 top_x = it.glyph_row->x;
1434 else
1436 top_x = it2_prev.current_x;
1437 top_y = it2_prev.current_y;
1441 else if (IT_CHARPOS (it) != charpos)
1443 Lisp_Object cpos = make_number (charpos);
1444 Lisp_Object spec = Fget_char_property (cpos, Qdisplay, Qnil);
1445 Lisp_Object string = string_from_display_spec (spec);
1446 struct text_pos tpos;
1447 int replacing_spec_p;
1448 bool newline_in_string
1449 = (STRINGP (string)
1450 && memchr (SDATA (string), '\n', SBYTES (string)));
1452 SET_TEXT_POS (tpos, charpos, CHAR_TO_BYTE (charpos));
1453 replacing_spec_p
1454 = (!NILP (spec)
1455 && handle_display_spec (NULL, spec, Qnil, Qnil, &tpos,
1456 charpos, FRAME_WINDOW_P (it.f)));
1457 /* The tricky code below is needed because there's a
1458 discrepancy between move_it_to and how we set cursor
1459 when PT is at the beginning of a portion of text
1460 covered by a display property or an overlay with a
1461 display property, or the display line ends in a
1462 newline from a display string. move_it_to will stop
1463 _after_ such display strings, whereas
1464 set_cursor_from_row conspires with cursor_row_p to
1465 place the cursor on the first glyph produced from the
1466 display string. */
1468 /* We have overshoot PT because it is covered by a
1469 display property that replaces the text it covers.
1470 If the string includes embedded newlines, we are also
1471 in the wrong display line. Backtrack to the correct
1472 line, where the display property begins. */
1473 if (replacing_spec_p)
1475 Lisp_Object startpos, endpos;
1476 EMACS_INT start, end;
1477 struct it it3;
1478 int it3_moved;
1480 /* Find the first and the last buffer positions
1481 covered by the display string. */
1482 endpos =
1483 Fnext_single_char_property_change (cpos, Qdisplay,
1484 Qnil, Qnil);
1485 startpos =
1486 Fprevious_single_char_property_change (endpos, Qdisplay,
1487 Qnil, Qnil);
1488 start = XFASTINT (startpos);
1489 end = XFASTINT (endpos);
1490 /* Move to the last buffer position before the
1491 display property. */
1492 start_display (&it3, w, top);
1493 if (start > CHARPOS (top))
1494 move_it_to (&it3, start - 1, -1, -1, -1, MOVE_TO_POS);
1495 /* Move forward one more line if the position before
1496 the display string is a newline or if it is the
1497 rightmost character on a line that is
1498 continued or word-wrapped. */
1499 if (it3.method == GET_FROM_BUFFER
1500 && (it3.c == '\n'
1501 || FETCH_BYTE (IT_BYTEPOS (it3)) == '\n'))
1502 move_it_by_lines (&it3, 1);
1503 else if (move_it_in_display_line_to (&it3, -1,
1504 it3.current_x
1505 + it3.pixel_width,
1506 MOVE_TO_X)
1507 == MOVE_LINE_CONTINUED)
1509 move_it_by_lines (&it3, 1);
1510 /* When we are under word-wrap, the #$@%!
1511 move_it_by_lines moves 2 lines, so we need to
1512 fix that up. */
1513 if (it3.line_wrap == WORD_WRAP)
1514 move_it_by_lines (&it3, -1);
1517 /* Record the vertical coordinate of the display
1518 line where we wound up. */
1519 top_y = it3.current_y;
1520 if (it3.bidi_p)
1522 /* When characters are reordered for display,
1523 the character displayed to the left of the
1524 display string could be _after_ the display
1525 property in the logical order. Use the
1526 smallest vertical position of these two. */
1527 start_display (&it3, w, top);
1528 move_it_to (&it3, end + 1, -1, -1, -1, MOVE_TO_POS);
1529 if (it3.current_y < top_y)
1530 top_y = it3.current_y;
1532 /* Move from the top of the window to the beginning
1533 of the display line where the display string
1534 begins. */
1535 start_display (&it3, w, top);
1536 move_it_to (&it3, -1, 0, top_y, -1, MOVE_TO_X | MOVE_TO_Y);
1537 /* If it3_moved stays zero after the 'while' loop
1538 below, that means we already were at a newline
1539 before the loop (e.g., the display string begins
1540 with a newline), so we don't need to (and cannot)
1541 inspect the glyphs of it3.glyph_row, because
1542 PRODUCE_GLYPHS will not produce anything for a
1543 newline, and thus it3.glyph_row stays at its
1544 stale content it got at top of the window. */
1545 it3_moved = 0;
1546 /* Finally, advance the iterator until we hit the
1547 first display element whose character position is
1548 CHARPOS, or until the first newline from the
1549 display string, which signals the end of the
1550 display line. */
1551 while (get_next_display_element (&it3))
1553 PRODUCE_GLYPHS (&it3);
1554 if (IT_CHARPOS (it3) == charpos
1555 || ITERATOR_AT_END_OF_LINE_P (&it3))
1556 break;
1557 it3_moved = 1;
1558 set_iterator_to_next (&it3, 0);
1560 top_x = it3.current_x - it3.pixel_width;
1561 /* Normally, we would exit the above loop because we
1562 found the display element whose character
1563 position is CHARPOS. For the contingency that we
1564 didn't, and stopped at the first newline from the
1565 display string, move back over the glyphs
1566 produced from the string, until we find the
1567 rightmost glyph not from the string. */
1568 if (it3_moved
1569 && newline_in_string
1570 && IT_CHARPOS (it3) != charpos && EQ (it3.object, string))
1572 struct glyph *g = it3.glyph_row->glyphs[TEXT_AREA]
1573 + it3.glyph_row->used[TEXT_AREA];
1575 while (EQ ((g - 1)->object, string))
1577 --g;
1578 top_x -= g->pixel_width;
1580 eassert (g < it3.glyph_row->glyphs[TEXT_AREA]
1581 + it3.glyph_row->used[TEXT_AREA]);
1586 *x = top_x;
1587 *y = max (top_y + max (0, it.max_ascent - it.ascent), window_top_y);
1588 *rtop = max (0, window_top_y - top_y);
1589 *rbot = max (0, bottom_y - it.last_visible_y);
1590 *rowh = max (0, (min (bottom_y, it.last_visible_y)
1591 - max (top_y, window_top_y)));
1592 *vpos = it.vpos;
1593 if (it.bidi_it.paragraph_dir == R2L)
1594 r2l = true;
1597 else
1599 /* Either we were asked to provide info about WINDOW_END, or
1600 CHARPOS is in the partially visible glyph row at end of
1601 window. */
1602 struct it it2;
1603 void *it2data = NULL;
1605 SAVE_IT (it2, it, it2data);
1606 if (IT_CHARPOS (it) < ZV && FETCH_BYTE (IT_BYTEPOS (it)) != '\n')
1607 move_it_by_lines (&it, 1);
1608 if (charpos < IT_CHARPOS (it)
1609 || (it.what == IT_EOB && charpos == IT_CHARPOS (it)))
1611 visible_p = true;
1612 RESTORE_IT (&it2, &it2, it2data);
1613 move_it_to (&it2, charpos, -1, -1, -1, MOVE_TO_POS);
1614 *x = it2.current_x;
1615 *y = it2.current_y + it2.max_ascent - it2.ascent;
1616 *rtop = max (0, -it2.current_y);
1617 *rbot = max (0, ((it2.current_y + it2.max_ascent + it2.max_descent)
1618 - it.last_visible_y));
1619 *rowh = max (0, (min (it2.current_y + it2.max_ascent + it2.max_descent,
1620 it.last_visible_y)
1621 - max (it2.current_y,
1622 WINDOW_HEADER_LINE_HEIGHT (w))));
1623 *vpos = it2.vpos;
1624 if (it2.bidi_it.paragraph_dir == R2L)
1625 r2l = true;
1627 else
1628 bidi_unshelve_cache (it2data, 1);
1630 bidi_unshelve_cache (itdata, 0);
1632 if (old_buffer)
1633 set_buffer_internal_1 (old_buffer);
1635 if (visible_p)
1637 if (w->hscroll > 0)
1638 *x -=
1639 window_hscroll_limited (w, WINDOW_XFRAME (w))
1640 * WINDOW_FRAME_COLUMN_WIDTH (w);
1641 /* For lines in an R2L paragraph, we need to mirror the X pixel
1642 coordinate wrt the text area. For the reasons, see the
1643 commentary in buffer_posn_from_coords and the explanation of
1644 the geometry used by the move_it_* functions at the end of
1645 the large commentary near the beginning of this file. */
1646 if (r2l)
1647 *x = window_box_width (w, TEXT_AREA) - *x - 1;
1650 #if 0
1651 /* Debugging code. */
1652 if (visible_p)
1653 fprintf (stderr, "+pv pt=%d vs=%d --> x=%d y=%d rt=%d rb=%d rh=%d vp=%d\n",
1654 charpos, w->vscroll, *x, *y, *rtop, *rbot, *rowh, *vpos);
1655 else
1656 fprintf (stderr, "-pv pt=%d vs=%d\n", charpos, w->vscroll);
1657 #endif
1659 return visible_p;
1663 /* Return the next character from STR. Return in *LEN the length of
1664 the character. This is like STRING_CHAR_AND_LENGTH but never
1665 returns an invalid character. If we find one, we return a `?', but
1666 with the length of the invalid character. */
1668 static int
1669 string_char_and_length (const unsigned char *str, int *len)
1671 int c;
1673 c = STRING_CHAR_AND_LENGTH (str, *len);
1674 if (!CHAR_VALID_P (c))
1675 /* We may not change the length here because other places in Emacs
1676 don't use this function, i.e. they silently accept invalid
1677 characters. */
1678 c = '?';
1680 return c;
1685 /* Given a position POS containing a valid character and byte position
1686 in STRING, return the position NCHARS ahead (NCHARS >= 0). */
1688 static struct text_pos
1689 string_pos_nchars_ahead (struct text_pos pos, Lisp_Object string, ptrdiff_t nchars)
1691 eassert (STRINGP (string) && nchars >= 0);
1693 if (STRING_MULTIBYTE (string))
1695 const unsigned char *p = SDATA (string) + BYTEPOS (pos);
1696 int len;
1698 while (nchars--)
1700 string_char_and_length (p, &len);
1701 p += len;
1702 CHARPOS (pos) += 1;
1703 BYTEPOS (pos) += len;
1706 else
1707 SET_TEXT_POS (pos, CHARPOS (pos) + nchars, BYTEPOS (pos) + nchars);
1709 return pos;
1713 /* Value is the text position, i.e. character and byte position,
1714 for character position CHARPOS in STRING. */
1716 static struct text_pos
1717 string_pos (ptrdiff_t charpos, Lisp_Object string)
1719 struct text_pos pos;
1720 eassert (STRINGP (string));
1721 eassert (charpos >= 0);
1722 SET_TEXT_POS (pos, charpos, string_char_to_byte (string, charpos));
1723 return pos;
1727 /* Value is a text position, i.e. character and byte position, for
1728 character position CHARPOS in C string S. MULTIBYTE_P non-zero
1729 means recognize multibyte characters. */
1731 static struct text_pos
1732 c_string_pos (ptrdiff_t charpos, const char *s, bool multibyte_p)
1734 struct text_pos pos;
1736 eassert (s != NULL);
1737 eassert (charpos >= 0);
1739 if (multibyte_p)
1741 int len;
1743 SET_TEXT_POS (pos, 0, 0);
1744 while (charpos--)
1746 string_char_and_length ((const unsigned char *) s, &len);
1747 s += len;
1748 CHARPOS (pos) += 1;
1749 BYTEPOS (pos) += len;
1752 else
1753 SET_TEXT_POS (pos, charpos, charpos);
1755 return pos;
1759 /* Value is the number of characters in C string S. MULTIBYTE_P
1760 non-zero means recognize multibyte characters. */
1762 static ptrdiff_t
1763 number_of_chars (const char *s, bool multibyte_p)
1765 ptrdiff_t nchars;
1767 if (multibyte_p)
1769 ptrdiff_t rest = strlen (s);
1770 int len;
1771 const unsigned char *p = (const unsigned char *) s;
1773 for (nchars = 0; rest > 0; ++nchars)
1775 string_char_and_length (p, &len);
1776 rest -= len, p += len;
1779 else
1780 nchars = strlen (s);
1782 return nchars;
1786 /* Compute byte position NEWPOS->bytepos corresponding to
1787 NEWPOS->charpos. POS is a known position in string STRING.
1788 NEWPOS->charpos must be >= POS.charpos. */
1790 static void
1791 compute_string_pos (struct text_pos *newpos, struct text_pos pos, Lisp_Object string)
1793 eassert (STRINGP (string));
1794 eassert (CHARPOS (*newpos) >= CHARPOS (pos));
1796 if (STRING_MULTIBYTE (string))
1797 *newpos = string_pos_nchars_ahead (pos, string,
1798 CHARPOS (*newpos) - CHARPOS (pos));
1799 else
1800 BYTEPOS (*newpos) = CHARPOS (*newpos);
1803 /* EXPORT:
1804 Return an estimation of the pixel height of mode or header lines on
1805 frame F. FACE_ID specifies what line's height to estimate. */
1808 estimate_mode_line_height (struct frame *f, enum face_id face_id)
1810 #ifdef HAVE_WINDOW_SYSTEM
1811 if (FRAME_WINDOW_P (f))
1813 int height = FONT_HEIGHT (FRAME_FONT (f));
1815 /* This function is called so early when Emacs starts that the face
1816 cache and mode line face are not yet initialized. */
1817 if (FRAME_FACE_CACHE (f))
1819 struct face *face = FACE_FROM_ID (f, face_id);
1820 if (face)
1822 if (face->font)
1823 height = FONT_HEIGHT (face->font);
1824 if (face->box_line_width > 0)
1825 height += 2 * face->box_line_width;
1829 return height;
1831 #endif
1833 return 1;
1836 /* Given a pixel position (PIX_X, PIX_Y) on frame F, return glyph
1837 co-ordinates in (*X, *Y). Set *BOUNDS to the rectangle that the
1838 glyph at X, Y occupies, if BOUNDS != 0. If NOCLIP is non-zero, do
1839 not force the value into range. */
1841 void
1842 pixel_to_glyph_coords (struct frame *f, register int pix_x, register int pix_y,
1843 int *x, int *y, NativeRectangle *bounds, int noclip)
1846 #ifdef HAVE_WINDOW_SYSTEM
1847 if (FRAME_WINDOW_P (f))
1849 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to round down
1850 even for negative values. */
1851 if (pix_x < 0)
1852 pix_x -= FRAME_COLUMN_WIDTH (f) - 1;
1853 if (pix_y < 0)
1854 pix_y -= FRAME_LINE_HEIGHT (f) - 1;
1856 pix_x = FRAME_PIXEL_X_TO_COL (f, pix_x);
1857 pix_y = FRAME_PIXEL_Y_TO_LINE (f, pix_y);
1859 if (bounds)
1860 STORE_NATIVE_RECT (*bounds,
1861 FRAME_COL_TO_PIXEL_X (f, pix_x),
1862 FRAME_LINE_TO_PIXEL_Y (f, pix_y),
1863 FRAME_COLUMN_WIDTH (f) - 1,
1864 FRAME_LINE_HEIGHT (f) - 1);
1866 /* PXW: Should we clip pixels before converting to columns/lines? */
1867 if (!noclip)
1869 if (pix_x < 0)
1870 pix_x = 0;
1871 else if (pix_x > FRAME_TOTAL_COLS (f))
1872 pix_x = FRAME_TOTAL_COLS (f);
1874 if (pix_y < 0)
1875 pix_y = 0;
1876 else if (pix_y > FRAME_TOTAL_LINES (f))
1877 pix_y = FRAME_TOTAL_LINES (f);
1880 #endif
1882 *x = pix_x;
1883 *y = pix_y;
1887 /* Find the glyph under window-relative coordinates X/Y in window W.
1888 Consider only glyphs from buffer text, i.e. no glyphs from overlay
1889 strings. Return in *HPOS and *VPOS the row and column number of
1890 the glyph found. Return in *AREA the glyph area containing X.
1891 Value is a pointer to the glyph found or null if X/Y is not on
1892 text, or we can't tell because W's current matrix is not up to
1893 date. */
1895 static struct glyph *
1896 x_y_to_hpos_vpos (struct window *w, int x, int y, int *hpos, int *vpos,
1897 int *dx, int *dy, int *area)
1899 struct glyph *glyph, *end;
1900 struct glyph_row *row = NULL;
1901 int x0, i;
1903 /* Find row containing Y. Give up if some row is not enabled. */
1904 for (i = 0; i < w->current_matrix->nrows; ++i)
1906 row = MATRIX_ROW (w->current_matrix, i);
1907 if (!row->enabled_p)
1908 return NULL;
1909 if (y >= row->y && y < MATRIX_ROW_BOTTOM_Y (row))
1910 break;
1913 *vpos = i;
1914 *hpos = 0;
1916 /* Give up if Y is not in the window. */
1917 if (i == w->current_matrix->nrows)
1918 return NULL;
1920 /* Get the glyph area containing X. */
1921 if (w->pseudo_window_p)
1923 *area = TEXT_AREA;
1924 x0 = 0;
1926 else
1928 if (x < window_box_left_offset (w, TEXT_AREA))
1930 *area = LEFT_MARGIN_AREA;
1931 x0 = window_box_left_offset (w, LEFT_MARGIN_AREA);
1933 else if (x < window_box_right_offset (w, TEXT_AREA))
1935 *area = TEXT_AREA;
1936 x0 = window_box_left_offset (w, TEXT_AREA) + min (row->x, 0);
1938 else
1940 *area = RIGHT_MARGIN_AREA;
1941 x0 = window_box_left_offset (w, RIGHT_MARGIN_AREA);
1945 /* Find glyph containing X. */
1946 glyph = row->glyphs[*area];
1947 end = glyph + row->used[*area];
1948 x -= x0;
1949 while (glyph < end && x >= glyph->pixel_width)
1951 x -= glyph->pixel_width;
1952 ++glyph;
1955 if (glyph == end)
1956 return NULL;
1958 if (dx)
1960 *dx = x;
1961 *dy = y - (row->y + row->ascent - glyph->ascent);
1964 *hpos = glyph - row->glyphs[*area];
1965 return glyph;
1968 /* Convert frame-relative x/y to coordinates relative to window W.
1969 Takes pseudo-windows into account. */
1971 static void
1972 frame_to_window_pixel_xy (struct window *w, int *x, int *y)
1974 if (w->pseudo_window_p)
1976 /* A pseudo-window is always full-width, and starts at the
1977 left edge of the frame, plus a frame border. */
1978 struct frame *f = XFRAME (w->frame);
1979 *x -= FRAME_INTERNAL_BORDER_WIDTH (f);
1980 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1982 else
1984 *x -= WINDOW_LEFT_EDGE_X (w);
1985 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1989 #ifdef HAVE_WINDOW_SYSTEM
1991 /* EXPORT:
1992 Return in RECTS[] at most N clipping rectangles for glyph string S.
1993 Return the number of stored rectangles. */
1996 get_glyph_string_clip_rects (struct glyph_string *s, NativeRectangle *rects, int n)
1998 XRectangle r;
2000 if (n <= 0)
2001 return 0;
2003 if (s->row->full_width_p)
2005 /* Draw full-width. X coordinates are relative to S->w->left_col. */
2006 r.x = WINDOW_LEFT_EDGE_X (s->w);
2007 if (s->row->mode_line_p)
2008 r.width = WINDOW_PIXEL_WIDTH (s->w) - WINDOW_RIGHT_DIVIDER_WIDTH (s->w);
2009 else
2010 r.width = WINDOW_PIXEL_WIDTH (s->w);
2012 /* Unless displaying a mode or menu bar line, which are always
2013 fully visible, clip to the visible part of the row. */
2014 if (s->w->pseudo_window_p)
2015 r.height = s->row->visible_height;
2016 else
2017 r.height = s->height;
2019 else
2021 /* This is a text line that may be partially visible. */
2022 r.x = window_box_left (s->w, s->area);
2023 r.width = window_box_width (s->w, s->area);
2024 r.height = s->row->visible_height;
2027 if (s->clip_head)
2028 if (r.x < s->clip_head->x)
2030 if (r.width >= s->clip_head->x - r.x)
2031 r.width -= s->clip_head->x - r.x;
2032 else
2033 r.width = 0;
2034 r.x = s->clip_head->x;
2036 if (s->clip_tail)
2037 if (r.x + r.width > s->clip_tail->x + s->clip_tail->background_width)
2039 if (s->clip_tail->x + s->clip_tail->background_width >= r.x)
2040 r.width = s->clip_tail->x + s->clip_tail->background_width - r.x;
2041 else
2042 r.width = 0;
2045 /* If S draws overlapping rows, it's sufficient to use the top and
2046 bottom of the window for clipping because this glyph string
2047 intentionally draws over other lines. */
2048 if (s->for_overlaps)
2050 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
2051 r.height = window_text_bottom_y (s->w) - r.y;
2053 /* Alas, the above simple strategy does not work for the
2054 environments with anti-aliased text: if the same text is
2055 drawn onto the same place multiple times, it gets thicker.
2056 If the overlap we are processing is for the erased cursor, we
2057 take the intersection with the rectangle of the cursor. */
2058 if (s->for_overlaps & OVERLAPS_ERASED_CURSOR)
2060 XRectangle rc, r_save = r;
2062 rc.x = WINDOW_TEXT_TO_FRAME_PIXEL_X (s->w, s->w->phys_cursor.x);
2063 rc.y = s->w->phys_cursor.y;
2064 rc.width = s->w->phys_cursor_width;
2065 rc.height = s->w->phys_cursor_height;
2067 x_intersect_rectangles (&r_save, &rc, &r);
2070 else
2072 /* Don't use S->y for clipping because it doesn't take partially
2073 visible lines into account. For example, it can be negative for
2074 partially visible lines at the top of a window. */
2075 if (!s->row->full_width_p
2076 && MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (s->w, s->row))
2077 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
2078 else
2079 r.y = max (0, s->row->y);
2082 r.y = WINDOW_TO_FRAME_PIXEL_Y (s->w, r.y);
2084 /* If drawing the cursor, don't let glyph draw outside its
2085 advertised boundaries. Cleartype does this under some circumstances. */
2086 if (s->hl == DRAW_CURSOR)
2088 struct glyph *glyph = s->first_glyph;
2089 int height, max_y;
2091 if (s->x > r.x)
2093 if (r.width >= s->x - r.x)
2094 r.width -= s->x - r.x;
2095 else /* R2L hscrolled row with cursor outside text area */
2096 r.width = 0;
2097 r.x = s->x;
2099 r.width = min (r.width, glyph->pixel_width);
2101 /* If r.y is below window bottom, ensure that we still see a cursor. */
2102 height = min (glyph->ascent + glyph->descent,
2103 min (FRAME_LINE_HEIGHT (s->f), s->row->visible_height));
2104 max_y = window_text_bottom_y (s->w) - height;
2105 max_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, max_y);
2106 if (s->ybase - glyph->ascent > max_y)
2108 r.y = max_y;
2109 r.height = height;
2111 else
2113 /* Don't draw cursor glyph taller than our actual glyph. */
2114 height = max (FRAME_LINE_HEIGHT (s->f), glyph->ascent + glyph->descent);
2115 if (height < r.height)
2117 max_y = r.y + r.height;
2118 r.y = min (max_y, max (r.y, s->ybase + glyph->descent - height));
2119 r.height = min (max_y - r.y, height);
2124 if (s->row->clip)
2126 XRectangle r_save = r;
2128 if (! x_intersect_rectangles (&r_save, s->row->clip, &r))
2129 r.width = 0;
2132 if ((s->for_overlaps & OVERLAPS_BOTH) == 0
2133 || ((s->for_overlaps & OVERLAPS_BOTH) == OVERLAPS_BOTH && n == 1))
2135 #ifdef CONVERT_FROM_XRECT
2136 CONVERT_FROM_XRECT (r, *rects);
2137 #else
2138 *rects = r;
2139 #endif
2140 return 1;
2142 else
2144 /* If we are processing overlapping and allowed to return
2145 multiple clipping rectangles, we exclude the row of the glyph
2146 string from the clipping rectangle. This is to avoid drawing
2147 the same text on the environment with anti-aliasing. */
2148 #ifdef CONVERT_FROM_XRECT
2149 XRectangle rs[2];
2150 #else
2151 XRectangle *rs = rects;
2152 #endif
2153 int i = 0, row_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, s->row->y);
2155 if (s->for_overlaps & OVERLAPS_PRED)
2157 rs[i] = r;
2158 if (r.y + r.height > row_y)
2160 if (r.y < row_y)
2161 rs[i].height = row_y - r.y;
2162 else
2163 rs[i].height = 0;
2165 i++;
2167 if (s->for_overlaps & OVERLAPS_SUCC)
2169 rs[i] = r;
2170 if (r.y < row_y + s->row->visible_height)
2172 if (r.y + r.height > row_y + s->row->visible_height)
2174 rs[i].y = row_y + s->row->visible_height;
2175 rs[i].height = r.y + r.height - rs[i].y;
2177 else
2178 rs[i].height = 0;
2180 i++;
2183 n = i;
2184 #ifdef CONVERT_FROM_XRECT
2185 for (i = 0; i < n; i++)
2186 CONVERT_FROM_XRECT (rs[i], rects[i]);
2187 #endif
2188 return n;
2192 /* EXPORT:
2193 Return in *NR the clipping rectangle for glyph string S. */
2195 void
2196 get_glyph_string_clip_rect (struct glyph_string *s, NativeRectangle *nr)
2198 get_glyph_string_clip_rects (s, nr, 1);
2202 /* EXPORT:
2203 Return the position and height of the phys cursor in window W.
2204 Set w->phys_cursor_width to width of phys cursor.
2207 void
2208 get_phys_cursor_geometry (struct window *w, struct glyph_row *row,
2209 struct glyph *glyph, int *xp, int *yp, int *heightp)
2211 struct frame *f = XFRAME (WINDOW_FRAME (w));
2212 int x, y, wd, h, h0, y0;
2214 /* Compute the width of the rectangle to draw. If on a stretch
2215 glyph, and `x-stretch-block-cursor' is nil, don't draw a
2216 rectangle as wide as the glyph, but use a canonical character
2217 width instead. */
2218 wd = glyph->pixel_width;
2220 x = w->phys_cursor.x;
2221 if (x < 0)
2223 wd += x;
2224 x = 0;
2227 if (glyph->type == STRETCH_GLYPH
2228 && !x_stretch_cursor_p)
2229 wd = min (FRAME_COLUMN_WIDTH (f), wd);
2230 w->phys_cursor_width = wd;
2232 y = w->phys_cursor.y + row->ascent - glyph->ascent;
2234 /* If y is below window bottom, ensure that we still see a cursor. */
2235 h0 = min (FRAME_LINE_HEIGHT (f), row->visible_height);
2237 h = max (h0, glyph->ascent + glyph->descent);
2238 h0 = min (h0, glyph->ascent + glyph->descent);
2240 y0 = WINDOW_HEADER_LINE_HEIGHT (w);
2241 if (y < y0)
2243 h = max (h - (y0 - y) + 1, h0);
2244 y = y0 - 1;
2246 else
2248 y0 = window_text_bottom_y (w) - h0;
2249 if (y > y0)
2251 h += y - y0;
2252 y = y0;
2256 *xp = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, x);
2257 *yp = WINDOW_TO_FRAME_PIXEL_Y (w, y);
2258 *heightp = h;
2262 * Remember which glyph the mouse is over.
2265 void
2266 remember_mouse_glyph (struct frame *f, int gx, int gy, NativeRectangle *rect)
2268 Lisp_Object window;
2269 struct window *w;
2270 struct glyph_row *r, *gr, *end_row;
2271 enum window_part part;
2272 enum glyph_row_area area;
2273 int x, y, width, height;
2275 /* Try to determine frame pixel position and size of the glyph under
2276 frame pixel coordinates X/Y on frame F. */
2278 if (window_resize_pixelwise)
2280 width = height = 1;
2281 goto virtual_glyph;
2283 else if (!f->glyphs_initialized_p
2284 || (window = window_from_coordinates (f, gx, gy, &part, 0),
2285 NILP (window)))
2287 width = FRAME_SMALLEST_CHAR_WIDTH (f);
2288 height = FRAME_SMALLEST_FONT_HEIGHT (f);
2289 goto virtual_glyph;
2292 w = XWINDOW (window);
2293 width = WINDOW_FRAME_COLUMN_WIDTH (w);
2294 height = WINDOW_FRAME_LINE_HEIGHT (w);
2296 x = window_relative_x_coord (w, part, gx);
2297 y = gy - WINDOW_TOP_EDGE_Y (w);
2299 r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
2300 end_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
2302 if (w->pseudo_window_p)
2304 area = TEXT_AREA;
2305 part = ON_MODE_LINE; /* Don't adjust margin. */
2306 goto text_glyph;
2309 switch (part)
2311 case ON_LEFT_MARGIN:
2312 area = LEFT_MARGIN_AREA;
2313 goto text_glyph;
2315 case ON_RIGHT_MARGIN:
2316 area = RIGHT_MARGIN_AREA;
2317 goto text_glyph;
2319 case ON_HEADER_LINE:
2320 case ON_MODE_LINE:
2321 gr = (part == ON_HEADER_LINE
2322 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
2323 : MATRIX_MODE_LINE_ROW (w->current_matrix));
2324 gy = gr->y;
2325 area = TEXT_AREA;
2326 goto text_glyph_row_found;
2328 case ON_TEXT:
2329 area = TEXT_AREA;
2331 text_glyph:
2332 gr = 0; gy = 0;
2333 for (; r <= end_row && r->enabled_p; ++r)
2334 if (r->y + r->height > y)
2336 gr = r; gy = r->y;
2337 break;
2340 text_glyph_row_found:
2341 if (gr && gy <= y)
2343 struct glyph *g = gr->glyphs[area];
2344 struct glyph *end = g + gr->used[area];
2346 height = gr->height;
2347 for (gx = gr->x; g < end; gx += g->pixel_width, ++g)
2348 if (gx + g->pixel_width > x)
2349 break;
2351 if (g < end)
2353 if (g->type == IMAGE_GLYPH)
2355 /* Don't remember when mouse is over image, as
2356 image may have hot-spots. */
2357 STORE_NATIVE_RECT (*rect, 0, 0, 0, 0);
2358 return;
2360 width = g->pixel_width;
2362 else
2364 /* Use nominal char spacing at end of line. */
2365 x -= gx;
2366 gx += (x / width) * width;
2369 if (part != ON_MODE_LINE && part != ON_HEADER_LINE)
2371 gx += window_box_left_offset (w, area);
2372 /* Don't expand over the modeline to make sure the vertical
2373 drag cursor is shown early enough. */
2374 height = min (height,
2375 max (0, WINDOW_BOX_HEIGHT_NO_MODE_LINE (w) - gy));
2378 else
2380 /* Use nominal line height at end of window. */
2381 gx = (x / width) * width;
2382 y -= gy;
2383 gy += (y / height) * height;
2384 if (part != ON_MODE_LINE && part != ON_HEADER_LINE)
2385 /* See comment above. */
2386 height = min (height,
2387 max (0, WINDOW_BOX_HEIGHT_NO_MODE_LINE (w) - gy));
2389 break;
2391 case ON_LEFT_FRINGE:
2392 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2393 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w)
2394 : window_box_right_offset (w, LEFT_MARGIN_AREA));
2395 width = WINDOW_LEFT_FRINGE_WIDTH (w);
2396 goto row_glyph;
2398 case ON_RIGHT_FRINGE:
2399 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2400 ? window_box_right_offset (w, RIGHT_MARGIN_AREA)
2401 : window_box_right_offset (w, TEXT_AREA));
2402 if (WINDOW_RIGHT_DIVIDER_WIDTH (w) == 0
2403 && !WINDOW_HAS_VERTICAL_SCROLL_BAR (w)
2404 && !WINDOW_RIGHTMOST_P (w))
2405 if (gx < WINDOW_PIXEL_WIDTH (w) - width)
2406 /* Make sure the vertical border can get her own glyph to the
2407 right of the one we build here. */
2408 width = WINDOW_RIGHT_FRINGE_WIDTH (w) - width;
2409 else
2410 width = WINDOW_PIXEL_WIDTH (w) - gx;
2411 else
2412 width = WINDOW_RIGHT_FRINGE_WIDTH (w);
2414 goto row_glyph;
2416 case ON_VERTICAL_BORDER:
2417 gx = WINDOW_PIXEL_WIDTH (w) - width;
2418 goto row_glyph;
2420 case ON_VERTICAL_SCROLL_BAR:
2421 gx = (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w)
2423 : (window_box_right_offset (w, RIGHT_MARGIN_AREA)
2424 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2425 ? WINDOW_RIGHT_FRINGE_WIDTH (w)
2426 : 0)));
2427 width = WINDOW_SCROLL_BAR_AREA_WIDTH (w);
2429 row_glyph:
2430 gr = 0, gy = 0;
2431 for (; r <= end_row && r->enabled_p; ++r)
2432 if (r->y + r->height > y)
2434 gr = r; gy = r->y;
2435 break;
2438 if (gr && gy <= y)
2439 height = gr->height;
2440 else
2442 /* Use nominal line height at end of window. */
2443 y -= gy;
2444 gy += (y / height) * height;
2446 break;
2448 case ON_RIGHT_DIVIDER:
2449 gx = WINDOW_PIXEL_WIDTH (w) - WINDOW_RIGHT_DIVIDER_WIDTH (w);
2450 width = WINDOW_RIGHT_DIVIDER_WIDTH (w);
2451 gy = 0;
2452 /* The bottom divider prevails. */
2453 height = WINDOW_PIXEL_HEIGHT (w) - WINDOW_BOTTOM_DIVIDER_WIDTH (w);
2454 goto add_edge;
2456 case ON_BOTTOM_DIVIDER:
2457 gx = 0;
2458 width = WINDOW_PIXEL_WIDTH (w);
2459 gy = WINDOW_PIXEL_HEIGHT (w) - WINDOW_BOTTOM_DIVIDER_WIDTH (w);
2460 height = WINDOW_BOTTOM_DIVIDER_WIDTH (w);
2461 goto add_edge;
2463 default:
2465 virtual_glyph:
2466 /* If there is no glyph under the mouse, then we divide the screen
2467 into a grid of the smallest glyph in the frame, and use that
2468 as our "glyph". */
2470 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to
2471 round down even for negative values. */
2472 if (gx < 0)
2473 gx -= width - 1;
2474 if (gy < 0)
2475 gy -= height - 1;
2477 gx = (gx / width) * width;
2478 gy = (gy / height) * height;
2480 goto store_rect;
2483 add_edge:
2484 gx += WINDOW_LEFT_EDGE_X (w);
2485 gy += WINDOW_TOP_EDGE_Y (w);
2487 store_rect:
2488 STORE_NATIVE_RECT (*rect, gx, gy, width, height);
2490 /* Visible feedback for debugging. */
2491 #if 0
2492 #if HAVE_X_WINDOWS
2493 XDrawRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
2494 f->output_data.x->normal_gc,
2495 gx, gy, width, height);
2496 #endif
2497 #endif
2501 #endif /* HAVE_WINDOW_SYSTEM */
2503 static void
2504 adjust_window_ends (struct window *w, struct glyph_row *row, bool current)
2506 eassert (w);
2507 w->window_end_pos = Z - MATRIX_ROW_END_CHARPOS (row);
2508 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
2509 w->window_end_vpos
2510 = MATRIX_ROW_VPOS (row, current ? w->current_matrix : w->desired_matrix);
2513 /***********************************************************************
2514 Lisp form evaluation
2515 ***********************************************************************/
2517 /* Error handler for safe_eval and safe_call. */
2519 static Lisp_Object
2520 safe_eval_handler (Lisp_Object arg, ptrdiff_t nargs, Lisp_Object *args)
2522 add_to_log ("Error during redisplay: %S signaled %S",
2523 Flist (nargs, args), arg);
2524 return Qnil;
2527 /* Call function FUNC with the rest of NARGS - 1 arguments
2528 following. Return the result, or nil if something went
2529 wrong. Prevent redisplay during the evaluation. */
2531 static Lisp_Object
2532 safe__call (bool inhibit_quit, ptrdiff_t nargs, Lisp_Object func, va_list ap)
2534 Lisp_Object val;
2536 if (inhibit_eval_during_redisplay)
2537 val = Qnil;
2538 else
2540 ptrdiff_t i;
2541 ptrdiff_t count = SPECPDL_INDEX ();
2542 Lisp_Object *args;
2543 USE_SAFE_ALLOCA;
2544 SAFE_ALLOCA_LISP (args, nargs);
2546 args[0] = func;
2547 for (i = 1; i < nargs; i++)
2548 args[i] = va_arg (ap, Lisp_Object);
2550 specbind (Qinhibit_redisplay, Qt);
2551 if (inhibit_quit)
2552 specbind (Qinhibit_quit, Qt);
2553 /* Use Qt to ensure debugger does not run,
2554 so there is no possibility of wanting to redisplay. */
2555 val = internal_condition_case_n (Ffuncall, nargs, args, Qt,
2556 safe_eval_handler);
2557 SAFE_FREE ();
2558 val = unbind_to (count, val);
2561 return val;
2564 Lisp_Object
2565 safe_call (ptrdiff_t nargs, Lisp_Object func, ...)
2567 Lisp_Object retval;
2568 va_list ap;
2570 va_start (ap, func);
2571 retval = safe__call (false, nargs, func, ap);
2572 va_end (ap);
2573 return retval;
2576 /* Call function FN with one argument ARG.
2577 Return the result, or nil if something went wrong. */
2579 Lisp_Object
2580 safe_call1 (Lisp_Object fn, Lisp_Object arg)
2582 return safe_call (2, fn, arg);
2585 static Lisp_Object
2586 safe__call1 (bool inhibit_quit, Lisp_Object fn, ...)
2588 Lisp_Object retval;
2589 va_list ap;
2591 va_start (ap, fn);
2592 retval = safe__call (inhibit_quit, 2, fn, ap);
2593 va_end (ap);
2594 return retval;
2597 Lisp_Object
2598 safe_eval (Lisp_Object sexpr)
2600 return safe__call1 (false, Qeval, sexpr);
2603 static Lisp_Object
2604 safe__eval (bool inhibit_quit, Lisp_Object sexpr)
2606 return safe__call1 (inhibit_quit, Qeval, sexpr);
2609 /* Call function FN with two arguments ARG1 and ARG2.
2610 Return the result, or nil if something went wrong. */
2612 Lisp_Object
2613 safe_call2 (Lisp_Object fn, Lisp_Object arg1, Lisp_Object arg2)
2615 return safe_call (3, fn, arg1, arg2);
2620 /***********************************************************************
2621 Debugging
2622 ***********************************************************************/
2624 #if 0
2626 /* Define CHECK_IT to perform sanity checks on iterators.
2627 This is for debugging. It is too slow to do unconditionally. */
2629 static void
2630 check_it (struct it *it)
2632 if (it->method == GET_FROM_STRING)
2634 eassert (STRINGP (it->string));
2635 eassert (IT_STRING_CHARPOS (*it) >= 0);
2637 else
2639 eassert (IT_STRING_CHARPOS (*it) < 0);
2640 if (it->method == GET_FROM_BUFFER)
2642 /* Check that character and byte positions agree. */
2643 eassert (IT_CHARPOS (*it) == BYTE_TO_CHAR (IT_BYTEPOS (*it)));
2647 if (it->dpvec)
2648 eassert (it->current.dpvec_index >= 0);
2649 else
2650 eassert (it->current.dpvec_index < 0);
2653 #define CHECK_IT(IT) check_it ((IT))
2655 #else /* not 0 */
2657 #define CHECK_IT(IT) (void) 0
2659 #endif /* not 0 */
2662 #if defined GLYPH_DEBUG && defined ENABLE_CHECKING
2664 /* Check that the window end of window W is what we expect it
2665 to be---the last row in the current matrix displaying text. */
2667 static void
2668 check_window_end (struct window *w)
2670 if (!MINI_WINDOW_P (w) && w->window_end_valid)
2672 struct glyph_row *row;
2673 eassert ((row = MATRIX_ROW (w->current_matrix, w->window_end_vpos),
2674 !row->enabled_p
2675 || MATRIX_ROW_DISPLAYS_TEXT_P (row)
2676 || MATRIX_ROW_VPOS (row, w->current_matrix) == 0));
2680 #define CHECK_WINDOW_END(W) check_window_end ((W))
2682 #else
2684 #define CHECK_WINDOW_END(W) (void) 0
2686 #endif /* GLYPH_DEBUG and ENABLE_CHECKING */
2688 /***********************************************************************
2689 Iterator initialization
2690 ***********************************************************************/
2692 /* Initialize IT for displaying current_buffer in window W, starting
2693 at character position CHARPOS. CHARPOS < 0 means that no buffer
2694 position is specified which is useful when the iterator is assigned
2695 a position later. BYTEPOS is the byte position corresponding to
2696 CHARPOS.
2698 If ROW is not null, calls to produce_glyphs with IT as parameter
2699 will produce glyphs in that row.
2701 BASE_FACE_ID is the id of a base face to use. It must be one of
2702 DEFAULT_FACE_ID for normal text, MODE_LINE_FACE_ID,
2703 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID for displaying
2704 mode lines, or TOOL_BAR_FACE_ID for displaying the tool-bar.
2706 If ROW is null and BASE_FACE_ID is equal to MODE_LINE_FACE_ID,
2707 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID, the iterator
2708 will be initialized to use the corresponding mode line glyph row of
2709 the desired matrix of W. */
2711 void
2712 init_iterator (struct it *it, struct window *w,
2713 ptrdiff_t charpos, ptrdiff_t bytepos,
2714 struct glyph_row *row, enum face_id base_face_id)
2716 enum face_id remapped_base_face_id = base_face_id;
2718 /* Some precondition checks. */
2719 eassert (w != NULL && it != NULL);
2720 eassert (charpos < 0 || (charpos >= BUF_BEG (current_buffer)
2721 && charpos <= ZV));
2723 /* If face attributes have been changed since the last redisplay,
2724 free realized faces now because they depend on face definitions
2725 that might have changed. Don't free faces while there might be
2726 desired matrices pending which reference these faces. */
2727 if (face_change_count && !inhibit_free_realized_faces)
2729 face_change_count = 0;
2730 free_all_realized_faces (Qnil);
2733 /* Perhaps remap BASE_FACE_ID to a user-specified alternative. */
2734 if (! NILP (Vface_remapping_alist))
2735 remapped_base_face_id
2736 = lookup_basic_face (XFRAME (w->frame), base_face_id);
2738 /* Use one of the mode line rows of W's desired matrix if
2739 appropriate. */
2740 if (row == NULL)
2742 if (base_face_id == MODE_LINE_FACE_ID
2743 || base_face_id == MODE_LINE_INACTIVE_FACE_ID)
2744 row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
2745 else if (base_face_id == HEADER_LINE_FACE_ID)
2746 row = MATRIX_HEADER_LINE_ROW (w->desired_matrix);
2749 /* Clear IT. */
2750 memset (it, 0, sizeof *it);
2751 it->current.overlay_string_index = -1;
2752 it->current.dpvec_index = -1;
2753 it->base_face_id = remapped_base_face_id;
2754 it->string = Qnil;
2755 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
2756 it->paragraph_embedding = L2R;
2757 it->bidi_it.string.lstring = Qnil;
2758 it->bidi_it.string.s = NULL;
2759 it->bidi_it.string.bufpos = 0;
2760 it->bidi_it.w = w;
2762 /* The window in which we iterate over current_buffer: */
2763 XSETWINDOW (it->window, w);
2764 it->w = w;
2765 it->f = XFRAME (w->frame);
2767 it->cmp_it.id = -1;
2769 /* Extra space between lines (on window systems only). */
2770 if (base_face_id == DEFAULT_FACE_ID
2771 && FRAME_WINDOW_P (it->f))
2773 if (NATNUMP (BVAR (current_buffer, extra_line_spacing)))
2774 it->extra_line_spacing = XFASTINT (BVAR (current_buffer, extra_line_spacing));
2775 else if (FLOATP (BVAR (current_buffer, extra_line_spacing)))
2776 it->extra_line_spacing = (XFLOAT_DATA (BVAR (current_buffer, extra_line_spacing))
2777 * FRAME_LINE_HEIGHT (it->f));
2778 else if (it->f->extra_line_spacing > 0)
2779 it->extra_line_spacing = it->f->extra_line_spacing;
2780 it->max_extra_line_spacing = 0;
2783 /* If realized faces have been removed, e.g. because of face
2784 attribute changes of named faces, recompute them. When running
2785 in batch mode, the face cache of the initial frame is null. If
2786 we happen to get called, make a dummy face cache. */
2787 if (FRAME_FACE_CACHE (it->f) == NULL)
2788 init_frame_faces (it->f);
2789 if (FRAME_FACE_CACHE (it->f)->used == 0)
2790 recompute_basic_faces (it->f);
2792 /* Current value of the `slice', `space-width', and 'height' properties. */
2793 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
2794 it->space_width = Qnil;
2795 it->font_height = Qnil;
2796 it->override_ascent = -1;
2798 /* Are control characters displayed as `^C'? */
2799 it->ctl_arrow_p = !NILP (BVAR (current_buffer, ctl_arrow));
2801 /* -1 means everything between a CR and the following line end
2802 is invisible. >0 means lines indented more than this value are
2803 invisible. */
2804 it->selective = (INTEGERP (BVAR (current_buffer, selective_display))
2805 ? (clip_to_bounds
2806 (-1, XINT (BVAR (current_buffer, selective_display)),
2807 PTRDIFF_MAX))
2808 : (!NILP (BVAR (current_buffer, selective_display))
2809 ? -1 : 0));
2810 it->selective_display_ellipsis_p
2811 = !NILP (BVAR (current_buffer, selective_display_ellipses));
2813 /* Display table to use. */
2814 it->dp = window_display_table (w);
2816 /* Are multibyte characters enabled in current_buffer? */
2817 it->multibyte_p = !NILP (BVAR (current_buffer, enable_multibyte_characters));
2819 /* Get the position at which the redisplay_end_trigger hook should
2820 be run, if it is to be run at all. */
2821 if (MARKERP (w->redisplay_end_trigger)
2822 && XMARKER (w->redisplay_end_trigger)->buffer != 0)
2823 it->redisplay_end_trigger_charpos
2824 = marker_position (w->redisplay_end_trigger);
2825 else if (INTEGERP (w->redisplay_end_trigger))
2826 it->redisplay_end_trigger_charpos
2827 = clip_to_bounds (PTRDIFF_MIN, XINT (w->redisplay_end_trigger),
2828 PTRDIFF_MAX);
2830 it->tab_width = SANE_TAB_WIDTH (current_buffer);
2832 /* Are lines in the display truncated? */
2833 if (base_face_id != DEFAULT_FACE_ID
2834 || it->w->hscroll
2835 || (! WINDOW_FULL_WIDTH_P (it->w)
2836 && ((!NILP (Vtruncate_partial_width_windows)
2837 && !INTEGERP (Vtruncate_partial_width_windows))
2838 || (INTEGERP (Vtruncate_partial_width_windows)
2839 /* PXW: Shall we do something about this? */
2840 && (WINDOW_TOTAL_COLS (it->w)
2841 < XINT (Vtruncate_partial_width_windows))))))
2842 it->line_wrap = TRUNCATE;
2843 else if (NILP (BVAR (current_buffer, truncate_lines)))
2844 it->line_wrap = NILP (BVAR (current_buffer, word_wrap))
2845 ? WINDOW_WRAP : WORD_WRAP;
2846 else
2847 it->line_wrap = TRUNCATE;
2849 /* Get dimensions of truncation and continuation glyphs. These are
2850 displayed as fringe bitmaps under X, but we need them for such
2851 frames when the fringes are turned off. But leave the dimensions
2852 zero for tooltip frames, as these glyphs look ugly there and also
2853 sabotage calculations of tooltip dimensions in x-show-tip. */
2854 #ifdef HAVE_WINDOW_SYSTEM
2855 if (!(FRAME_WINDOW_P (it->f)
2856 && FRAMEP (tip_frame)
2857 && it->f == XFRAME (tip_frame)))
2858 #endif
2860 if (it->line_wrap == TRUNCATE)
2862 /* We will need the truncation glyph. */
2863 eassert (it->glyph_row == NULL);
2864 produce_special_glyphs (it, IT_TRUNCATION);
2865 it->truncation_pixel_width = it->pixel_width;
2867 else
2869 /* We will need the continuation glyph. */
2870 eassert (it->glyph_row == NULL);
2871 produce_special_glyphs (it, IT_CONTINUATION);
2872 it->continuation_pixel_width = it->pixel_width;
2876 /* Reset these values to zero because the produce_special_glyphs
2877 above has changed them. */
2878 it->pixel_width = it->ascent = it->descent = 0;
2879 it->phys_ascent = it->phys_descent = 0;
2881 /* Set this after getting the dimensions of truncation and
2882 continuation glyphs, so that we don't produce glyphs when calling
2883 produce_special_glyphs, above. */
2884 it->glyph_row = row;
2885 it->area = TEXT_AREA;
2887 /* Get the dimensions of the display area. The display area
2888 consists of the visible window area plus a horizontally scrolled
2889 part to the left of the window. All x-values are relative to the
2890 start of this total display area. */
2891 if (base_face_id != DEFAULT_FACE_ID)
2893 /* Mode lines, menu bar in terminal frames. */
2894 it->first_visible_x = 0;
2895 it->last_visible_x = WINDOW_PIXEL_WIDTH (w);
2897 else
2899 it->first_visible_x
2900 = window_hscroll_limited (it->w, it->f) * FRAME_COLUMN_WIDTH (it->f);
2901 it->last_visible_x = (it->first_visible_x
2902 + window_box_width (w, TEXT_AREA));
2904 /* If we truncate lines, leave room for the truncation glyph(s) at
2905 the right margin. Otherwise, leave room for the continuation
2906 glyph(s). Done only if the window has no right fringe. */
2907 if (WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0)
2909 if (it->line_wrap == TRUNCATE)
2910 it->last_visible_x -= it->truncation_pixel_width;
2911 else
2912 it->last_visible_x -= it->continuation_pixel_width;
2915 it->header_line_p = WINDOW_WANTS_HEADER_LINE_P (w);
2916 it->current_y = WINDOW_HEADER_LINE_HEIGHT (w) + w->vscroll;
2919 /* Leave room for a border glyph. */
2920 if (!FRAME_WINDOW_P (it->f)
2921 && !WINDOW_RIGHTMOST_P (it->w))
2922 it->last_visible_x -= 1;
2924 it->last_visible_y = window_text_bottom_y (w);
2926 /* For mode lines and alike, arrange for the first glyph having a
2927 left box line if the face specifies a box. */
2928 if (base_face_id != DEFAULT_FACE_ID)
2930 struct face *face;
2932 it->face_id = remapped_base_face_id;
2934 /* If we have a boxed mode line, make the first character appear
2935 with a left box line. */
2936 face = FACE_FROM_ID (it->f, remapped_base_face_id);
2937 if (face && face->box != FACE_NO_BOX)
2938 it->start_of_box_run_p = true;
2941 /* If a buffer position was specified, set the iterator there,
2942 getting overlays and face properties from that position. */
2943 if (charpos >= BUF_BEG (current_buffer))
2945 it->stop_charpos = charpos;
2946 it->end_charpos = ZV;
2947 eassert (charpos == BYTE_TO_CHAR (bytepos));
2948 IT_CHARPOS (*it) = charpos;
2949 IT_BYTEPOS (*it) = bytepos;
2951 /* We will rely on `reseat' to set this up properly, via
2952 handle_face_prop. */
2953 it->face_id = it->base_face_id;
2955 it->start = it->current;
2956 /* Do we need to reorder bidirectional text? Not if this is a
2957 unibyte buffer: by definition, none of the single-byte
2958 characters are strong R2L, so no reordering is needed. And
2959 bidi.c doesn't support unibyte buffers anyway. Also, don't
2960 reorder while we are loading loadup.el, since the tables of
2961 character properties needed for reordering are not yet
2962 available. */
2963 it->bidi_p =
2964 NILP (Vpurify_flag)
2965 && !NILP (BVAR (current_buffer, bidi_display_reordering))
2966 && it->multibyte_p;
2968 /* If we are to reorder bidirectional text, init the bidi
2969 iterator. */
2970 if (it->bidi_p)
2972 /* Since we don't know at this point whether there will be
2973 any R2L lines in the window, we reserve space for
2974 truncation/continuation glyphs even if only the left
2975 fringe is absent. */
2976 if (base_face_id == DEFAULT_FACE_ID
2977 && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0
2978 && WINDOW_RIGHT_FRINGE_WIDTH (it->w) != 0)
2980 if (it->line_wrap == TRUNCATE)
2981 it->last_visible_x -= it->truncation_pixel_width;
2982 else
2983 it->last_visible_x -= it->continuation_pixel_width;
2985 /* Note the paragraph direction that this buffer wants to
2986 use. */
2987 if (EQ (BVAR (current_buffer, bidi_paragraph_direction),
2988 Qleft_to_right))
2989 it->paragraph_embedding = L2R;
2990 else if (EQ (BVAR (current_buffer, bidi_paragraph_direction),
2991 Qright_to_left))
2992 it->paragraph_embedding = R2L;
2993 else
2994 it->paragraph_embedding = NEUTRAL_DIR;
2995 bidi_unshelve_cache (NULL, 0);
2996 bidi_init_it (charpos, IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
2997 &it->bidi_it);
3000 /* Compute faces etc. */
3001 reseat (it, it->current.pos, 1);
3004 CHECK_IT (it);
3008 /* Initialize IT for the display of window W with window start POS. */
3010 void
3011 start_display (struct it *it, struct window *w, struct text_pos pos)
3013 struct glyph_row *row;
3014 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
3016 row = w->desired_matrix->rows + first_vpos;
3017 init_iterator (it, w, CHARPOS (pos), BYTEPOS (pos), row, DEFAULT_FACE_ID);
3018 it->first_vpos = first_vpos;
3020 /* Don't reseat to previous visible line start if current start
3021 position is in a string or image. */
3022 if (it->method == GET_FROM_BUFFER && it->line_wrap != TRUNCATE)
3024 int start_at_line_beg_p;
3025 int first_y = it->current_y;
3027 /* If window start is not at a line start, skip forward to POS to
3028 get the correct continuation lines width. */
3029 start_at_line_beg_p = (CHARPOS (pos) == BEGV
3030 || FETCH_BYTE (BYTEPOS (pos) - 1) == '\n');
3031 if (!start_at_line_beg_p)
3033 int new_x;
3035 reseat_at_previous_visible_line_start (it);
3036 move_it_to (it, CHARPOS (pos), -1, -1, -1, MOVE_TO_POS);
3038 new_x = it->current_x + it->pixel_width;
3040 /* If lines are continued, this line may end in the middle
3041 of a multi-glyph character (e.g. a control character
3042 displayed as \003, or in the middle of an overlay
3043 string). In this case move_it_to above will not have
3044 taken us to the start of the continuation line but to the
3045 end of the continued line. */
3046 if (it->current_x > 0
3047 && it->line_wrap != TRUNCATE /* Lines are continued. */
3048 && (/* And glyph doesn't fit on the line. */
3049 new_x > it->last_visible_x
3050 /* Or it fits exactly and we're on a window
3051 system frame. */
3052 || (new_x == it->last_visible_x
3053 && FRAME_WINDOW_P (it->f)
3054 && ((it->bidi_p && it->bidi_it.paragraph_dir == R2L)
3055 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
3056 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)))))
3058 if ((it->current.dpvec_index >= 0
3059 || it->current.overlay_string_index >= 0)
3060 /* If we are on a newline from a display vector or
3061 overlay string, then we are already at the end of
3062 a screen line; no need to go to the next line in
3063 that case, as this line is not really continued.
3064 (If we do go to the next line, C-e will not DTRT.) */
3065 && it->c != '\n')
3067 set_iterator_to_next (it, 1);
3068 move_it_in_display_line_to (it, -1, -1, 0);
3071 it->continuation_lines_width += it->current_x;
3073 /* If the character at POS is displayed via a display
3074 vector, move_it_to above stops at the final glyph of
3075 IT->dpvec. To make the caller redisplay that character
3076 again (a.k.a. start at POS), we need to reset the
3077 dpvec_index to the beginning of IT->dpvec. */
3078 else if (it->current.dpvec_index >= 0)
3079 it->current.dpvec_index = 0;
3081 /* We're starting a new display line, not affected by the
3082 height of the continued line, so clear the appropriate
3083 fields in the iterator structure. */
3084 it->max_ascent = it->max_descent = 0;
3085 it->max_phys_ascent = it->max_phys_descent = 0;
3087 it->current_y = first_y;
3088 it->vpos = 0;
3089 it->current_x = it->hpos = 0;
3095 /* Return 1 if POS is a position in ellipses displayed for invisible
3096 text. W is the window we display, for text property lookup. */
3098 static int
3099 in_ellipses_for_invisible_text_p (struct display_pos *pos, struct window *w)
3101 Lisp_Object prop, window;
3102 int ellipses_p = 0;
3103 ptrdiff_t charpos = CHARPOS (pos->pos);
3105 /* If POS specifies a position in a display vector, this might
3106 be for an ellipsis displayed for invisible text. We won't
3107 get the iterator set up for delivering that ellipsis unless
3108 we make sure that it gets aware of the invisible text. */
3109 if (pos->dpvec_index >= 0
3110 && pos->overlay_string_index < 0
3111 && CHARPOS (pos->string_pos) < 0
3112 && charpos > BEGV
3113 && (XSETWINDOW (window, w),
3114 prop = Fget_char_property (make_number (charpos),
3115 Qinvisible, window),
3116 !TEXT_PROP_MEANS_INVISIBLE (prop)))
3118 prop = Fget_char_property (make_number (charpos - 1), Qinvisible,
3119 window);
3120 ellipses_p = 2 == TEXT_PROP_MEANS_INVISIBLE (prop);
3123 return ellipses_p;
3127 /* Initialize IT for stepping through current_buffer in window W,
3128 starting at position POS that includes overlay string and display
3129 vector/ control character translation position information. Value
3130 is zero if there are overlay strings with newlines at POS. */
3132 static int
3133 init_from_display_pos (struct it *it, struct window *w, struct display_pos *pos)
3135 ptrdiff_t charpos = CHARPOS (pos->pos), bytepos = BYTEPOS (pos->pos);
3136 int i, overlay_strings_with_newlines = 0;
3138 /* If POS specifies a position in a display vector, this might
3139 be for an ellipsis displayed for invisible text. We won't
3140 get the iterator set up for delivering that ellipsis unless
3141 we make sure that it gets aware of the invisible text. */
3142 if (in_ellipses_for_invisible_text_p (pos, w))
3144 --charpos;
3145 bytepos = 0;
3148 /* Keep in mind: the call to reseat in init_iterator skips invisible
3149 text, so we might end up at a position different from POS. This
3150 is only a problem when POS is a row start after a newline and an
3151 overlay starts there with an after-string, and the overlay has an
3152 invisible property. Since we don't skip invisible text in
3153 display_line and elsewhere immediately after consuming the
3154 newline before the row start, such a POS will not be in a string,
3155 but the call to init_iterator below will move us to the
3156 after-string. */
3157 init_iterator (it, w, charpos, bytepos, NULL, DEFAULT_FACE_ID);
3159 /* This only scans the current chunk -- it should scan all chunks.
3160 However, OVERLAY_STRING_CHUNK_SIZE has been increased from 3 in 21.1
3161 to 16 in 22.1 to make this a lesser problem. */
3162 for (i = 0; i < it->n_overlay_strings && i < OVERLAY_STRING_CHUNK_SIZE; ++i)
3164 const char *s = SSDATA (it->overlay_strings[i]);
3165 const char *e = s + SBYTES (it->overlay_strings[i]);
3167 while (s < e && *s != '\n')
3168 ++s;
3170 if (s < e)
3172 overlay_strings_with_newlines = 1;
3173 break;
3177 /* If position is within an overlay string, set up IT to the right
3178 overlay string. */
3179 if (pos->overlay_string_index >= 0)
3181 int relative_index;
3183 /* If the first overlay string happens to have a `display'
3184 property for an image, the iterator will be set up for that
3185 image, and we have to undo that setup first before we can
3186 correct the overlay string index. */
3187 if (it->method == GET_FROM_IMAGE)
3188 pop_it (it);
3190 /* We already have the first chunk of overlay strings in
3191 IT->overlay_strings. Load more until the one for
3192 pos->overlay_string_index is in IT->overlay_strings. */
3193 if (pos->overlay_string_index >= OVERLAY_STRING_CHUNK_SIZE)
3195 ptrdiff_t n = pos->overlay_string_index / OVERLAY_STRING_CHUNK_SIZE;
3196 it->current.overlay_string_index = 0;
3197 while (n--)
3199 load_overlay_strings (it, 0);
3200 it->current.overlay_string_index += OVERLAY_STRING_CHUNK_SIZE;
3204 it->current.overlay_string_index = pos->overlay_string_index;
3205 relative_index = (it->current.overlay_string_index
3206 % OVERLAY_STRING_CHUNK_SIZE);
3207 it->string = it->overlay_strings[relative_index];
3208 eassert (STRINGP (it->string));
3209 it->current.string_pos = pos->string_pos;
3210 it->method = GET_FROM_STRING;
3211 it->end_charpos = SCHARS (it->string);
3212 /* Set up the bidi iterator for this overlay string. */
3213 if (it->bidi_p)
3215 it->bidi_it.string.lstring = it->string;
3216 it->bidi_it.string.s = NULL;
3217 it->bidi_it.string.schars = SCHARS (it->string);
3218 it->bidi_it.string.bufpos = it->overlay_strings_charpos;
3219 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
3220 it->bidi_it.string.unibyte = !it->multibyte_p;
3221 it->bidi_it.w = it->w;
3222 bidi_init_it (IT_STRING_CHARPOS (*it), IT_STRING_BYTEPOS (*it),
3223 FRAME_WINDOW_P (it->f), &it->bidi_it);
3225 /* Synchronize the state of the bidi iterator with
3226 pos->string_pos. For any string position other than
3227 zero, this will be done automagically when we resume
3228 iteration over the string and get_visually_first_element
3229 is called. But if string_pos is zero, and the string is
3230 to be reordered for display, we need to resync manually,
3231 since it could be that the iteration state recorded in
3232 pos ended at string_pos of 0 moving backwards in string. */
3233 if (CHARPOS (pos->string_pos) == 0)
3235 get_visually_first_element (it);
3236 if (IT_STRING_CHARPOS (*it) != 0)
3237 do {
3238 /* Paranoia. */
3239 eassert (it->bidi_it.charpos < it->bidi_it.string.schars);
3240 bidi_move_to_visually_next (&it->bidi_it);
3241 } while (it->bidi_it.charpos != 0);
3243 eassert (IT_STRING_CHARPOS (*it) == it->bidi_it.charpos
3244 && IT_STRING_BYTEPOS (*it) == it->bidi_it.bytepos);
3248 if (CHARPOS (pos->string_pos) >= 0)
3250 /* Recorded position is not in an overlay string, but in another
3251 string. This can only be a string from a `display' property.
3252 IT should already be filled with that string. */
3253 it->current.string_pos = pos->string_pos;
3254 eassert (STRINGP (it->string));
3255 if (it->bidi_p)
3256 bidi_init_it (IT_STRING_CHARPOS (*it), IT_STRING_BYTEPOS (*it),
3257 FRAME_WINDOW_P (it->f), &it->bidi_it);
3260 /* Restore position in display vector translations, control
3261 character translations or ellipses. */
3262 if (pos->dpvec_index >= 0)
3264 if (it->dpvec == NULL)
3265 get_next_display_element (it);
3266 eassert (it->dpvec && it->current.dpvec_index == 0);
3267 it->current.dpvec_index = pos->dpvec_index;
3270 CHECK_IT (it);
3271 return !overlay_strings_with_newlines;
3275 /* Initialize IT for stepping through current_buffer in window W
3276 starting at ROW->start. */
3278 static void
3279 init_to_row_start (struct it *it, struct window *w, struct glyph_row *row)
3281 init_from_display_pos (it, w, &row->start);
3282 it->start = row->start;
3283 it->continuation_lines_width = row->continuation_lines_width;
3284 CHECK_IT (it);
3288 /* Initialize IT for stepping through current_buffer in window W
3289 starting in the line following ROW, i.e. starting at ROW->end.
3290 Value is zero if there are overlay strings with newlines at ROW's
3291 end position. */
3293 static int
3294 init_to_row_end (struct it *it, struct window *w, struct glyph_row *row)
3296 int success = 0;
3298 if (init_from_display_pos (it, w, &row->end))
3300 if (row->continued_p)
3301 it->continuation_lines_width
3302 = row->continuation_lines_width + row->pixel_width;
3303 CHECK_IT (it);
3304 success = 1;
3307 return success;
3313 /***********************************************************************
3314 Text properties
3315 ***********************************************************************/
3317 /* Called when IT reaches IT->stop_charpos. Handle text property and
3318 overlay changes. Set IT->stop_charpos to the next position where
3319 to stop. */
3321 static void
3322 handle_stop (struct it *it)
3324 enum prop_handled handled;
3325 int handle_overlay_change_p;
3326 struct props *p;
3328 it->dpvec = NULL;
3329 it->current.dpvec_index = -1;
3330 handle_overlay_change_p = !it->ignore_overlay_strings_at_pos_p;
3331 it->ignore_overlay_strings_at_pos_p = 0;
3332 it->ellipsis_p = 0;
3334 /* Use face of preceding text for ellipsis (if invisible) */
3335 if (it->selective_display_ellipsis_p)
3336 it->saved_face_id = it->face_id;
3338 /* Here's the description of the semantics of, and the logic behind,
3339 the various HANDLED_* statuses:
3341 HANDLED_NORMALLY means the handler did its job, and the loop
3342 should proceed to calling the next handler in order.
3344 HANDLED_RECOMPUTE_PROPS means the handler caused a significant
3345 change in the properties and overlays at current position, so the
3346 loop should be restarted, to re-invoke the handlers that were
3347 already called. This happens when fontification-functions were
3348 called by handle_fontified_prop, and actually fontified
3349 something. Another case where HANDLED_RECOMPUTE_PROPS is
3350 returned is when we discover overlay strings that need to be
3351 displayed right away. The loop below will continue for as long
3352 as the status is HANDLED_RECOMPUTE_PROPS.
3354 HANDLED_RETURN means return immediately to the caller, to
3355 continue iteration without calling any further handlers. This is
3356 used when we need to act on some property right away, for example
3357 when we need to display the ellipsis or a replacing display
3358 property, such as display string or image.
3360 HANDLED_OVERLAY_STRING_CONSUMED means an overlay string was just
3361 consumed, and the handler switched to the next overlay string.
3362 This signals the loop below to refrain from looking for more
3363 overlays before all the overlay strings of the current overlay
3364 are processed.
3366 Some of the handlers called by the loop push the iterator state
3367 onto the stack (see 'push_it'), and arrange for the iteration to
3368 continue with another object, such as an image, a display string,
3369 or an overlay string. In most such cases, it->stop_charpos is
3370 set to the first character of the string, so that when the
3371 iteration resumes, this function will immediately be called
3372 again, to examine the properties at the beginning of the string.
3374 When a display or overlay string is exhausted, the iterator state
3375 is popped (see 'pop_it'), and iteration continues with the
3376 previous object. Again, in many such cases this function is
3377 called again to find the next position where properties might
3378 change. */
3382 handled = HANDLED_NORMALLY;
3384 /* Call text property handlers. */
3385 for (p = it_props; p->handler; ++p)
3387 handled = p->handler (it);
3389 if (handled == HANDLED_RECOMPUTE_PROPS)
3390 break;
3391 else if (handled == HANDLED_RETURN)
3393 /* We still want to show before and after strings from
3394 overlays even if the actual buffer text is replaced. */
3395 if (!handle_overlay_change_p
3396 || it->sp > 1
3397 /* Don't call get_overlay_strings_1 if we already
3398 have overlay strings loaded, because doing so
3399 will load them again and push the iterator state
3400 onto the stack one more time, which is not
3401 expected by the rest of the code that processes
3402 overlay strings. */
3403 || (it->current.overlay_string_index < 0
3404 ? !get_overlay_strings_1 (it, 0, 0)
3405 : 0))
3407 if (it->ellipsis_p)
3408 setup_for_ellipsis (it, 0);
3409 /* When handling a display spec, we might load an
3410 empty string. In that case, discard it here. We
3411 used to discard it in handle_single_display_spec,
3412 but that causes get_overlay_strings_1, above, to
3413 ignore overlay strings that we must check. */
3414 if (STRINGP (it->string) && !SCHARS (it->string))
3415 pop_it (it);
3416 return;
3418 else if (STRINGP (it->string) && !SCHARS (it->string))
3419 pop_it (it);
3420 else
3422 it->ignore_overlay_strings_at_pos_p = true;
3423 it->string_from_display_prop_p = 0;
3424 it->from_disp_prop_p = 0;
3425 handle_overlay_change_p = 0;
3427 handled = HANDLED_RECOMPUTE_PROPS;
3428 break;
3430 else if (handled == HANDLED_OVERLAY_STRING_CONSUMED)
3431 handle_overlay_change_p = 0;
3434 if (handled != HANDLED_RECOMPUTE_PROPS)
3436 /* Don't check for overlay strings below when set to deliver
3437 characters from a display vector. */
3438 if (it->method == GET_FROM_DISPLAY_VECTOR)
3439 handle_overlay_change_p = 0;
3441 /* Handle overlay changes.
3442 This sets HANDLED to HANDLED_RECOMPUTE_PROPS
3443 if it finds overlays. */
3444 if (handle_overlay_change_p)
3445 handled = handle_overlay_change (it);
3448 if (it->ellipsis_p)
3450 setup_for_ellipsis (it, 0);
3451 break;
3454 while (handled == HANDLED_RECOMPUTE_PROPS);
3456 /* Determine where to stop next. */
3457 if (handled == HANDLED_NORMALLY)
3458 compute_stop_pos (it);
3462 /* Compute IT->stop_charpos from text property and overlay change
3463 information for IT's current position. */
3465 static void
3466 compute_stop_pos (struct it *it)
3468 register INTERVAL iv, next_iv;
3469 Lisp_Object object, limit, position;
3470 ptrdiff_t charpos, bytepos;
3472 if (STRINGP (it->string))
3474 /* Strings are usually short, so don't limit the search for
3475 properties. */
3476 it->stop_charpos = it->end_charpos;
3477 object = it->string;
3478 limit = Qnil;
3479 charpos = IT_STRING_CHARPOS (*it);
3480 bytepos = IT_STRING_BYTEPOS (*it);
3482 else
3484 ptrdiff_t pos;
3486 /* If end_charpos is out of range for some reason, such as a
3487 misbehaving display function, rationalize it (Bug#5984). */
3488 if (it->end_charpos > ZV)
3489 it->end_charpos = ZV;
3490 it->stop_charpos = it->end_charpos;
3492 /* If next overlay change is in front of the current stop pos
3493 (which is IT->end_charpos), stop there. Note: value of
3494 next_overlay_change is point-max if no overlay change
3495 follows. */
3496 charpos = IT_CHARPOS (*it);
3497 bytepos = IT_BYTEPOS (*it);
3498 pos = next_overlay_change (charpos);
3499 if (pos < it->stop_charpos)
3500 it->stop_charpos = pos;
3502 /* Set up variables for computing the stop position from text
3503 property changes. */
3504 XSETBUFFER (object, current_buffer);
3505 limit = make_number (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT);
3508 /* Get the interval containing IT's position. Value is a null
3509 interval if there isn't such an interval. */
3510 position = make_number (charpos);
3511 iv = validate_interval_range (object, &position, &position, 0);
3512 if (iv)
3514 Lisp_Object values_here[LAST_PROP_IDX];
3515 struct props *p;
3517 /* Get properties here. */
3518 for (p = it_props; p->handler; ++p)
3519 values_here[p->idx] = textget (iv->plist, make_lisp_symbol (p->name));
3521 /* Look for an interval following iv that has different
3522 properties. */
3523 for (next_iv = next_interval (iv);
3524 (next_iv
3525 && (NILP (limit)
3526 || XFASTINT (limit) > next_iv->position));
3527 next_iv = next_interval (next_iv))
3529 for (p = it_props; p->handler; ++p)
3531 Lisp_Object new_value = textget (next_iv->plist,
3532 make_lisp_symbol (p->name));
3533 if (!EQ (values_here[p->idx], new_value))
3534 break;
3537 if (p->handler)
3538 break;
3541 if (next_iv)
3543 if (INTEGERP (limit)
3544 && next_iv->position >= XFASTINT (limit))
3545 /* No text property change up to limit. */
3546 it->stop_charpos = min (XFASTINT (limit), it->stop_charpos);
3547 else
3548 /* Text properties change in next_iv. */
3549 it->stop_charpos = min (it->stop_charpos, next_iv->position);
3553 if (it->cmp_it.id < 0)
3555 ptrdiff_t stoppos = it->end_charpos;
3557 if (it->bidi_p && it->bidi_it.scan_dir < 0)
3558 stoppos = -1;
3559 composition_compute_stop_pos (&it->cmp_it, charpos, bytepos,
3560 stoppos, it->string);
3563 eassert (STRINGP (it->string)
3564 || (it->stop_charpos >= BEGV
3565 && it->stop_charpos >= IT_CHARPOS (*it)));
3569 /* Return the position of the next overlay change after POS in
3570 current_buffer. Value is point-max if no overlay change
3571 follows. This is like `next-overlay-change' but doesn't use
3572 xmalloc. */
3574 static ptrdiff_t
3575 next_overlay_change (ptrdiff_t pos)
3577 ptrdiff_t i, noverlays;
3578 ptrdiff_t endpos;
3579 Lisp_Object *overlays;
3580 USE_SAFE_ALLOCA;
3582 /* Get all overlays at the given position. */
3583 GET_OVERLAYS_AT (pos, overlays, noverlays, &endpos, 1);
3585 /* If any of these overlays ends before endpos,
3586 use its ending point instead. */
3587 for (i = 0; i < noverlays; ++i)
3589 Lisp_Object oend;
3590 ptrdiff_t oendpos;
3592 oend = OVERLAY_END (overlays[i]);
3593 oendpos = OVERLAY_POSITION (oend);
3594 endpos = min (endpos, oendpos);
3597 SAFE_FREE ();
3598 return endpos;
3601 /* How many characters forward to search for a display property or
3602 display string. Searching too far forward makes the bidi display
3603 sluggish, especially in small windows. */
3604 #define MAX_DISP_SCAN 250
3606 /* Return the character position of a display string at or after
3607 position specified by POSITION. If no display string exists at or
3608 after POSITION, return ZV. A display string is either an overlay
3609 with `display' property whose value is a string, or a `display'
3610 text property whose value is a string. STRING is data about the
3611 string to iterate; if STRING->lstring is nil, we are iterating a
3612 buffer. FRAME_WINDOW_P is non-zero when we are displaying a window
3613 on a GUI frame. DISP_PROP is set to zero if we searched
3614 MAX_DISP_SCAN characters forward without finding any display
3615 strings, non-zero otherwise. It is set to 2 if the display string
3616 uses any kind of `(space ...)' spec that will produce a stretch of
3617 white space in the text area. */
3618 ptrdiff_t
3619 compute_display_string_pos (struct text_pos *position,
3620 struct bidi_string_data *string,
3621 struct window *w,
3622 int frame_window_p, int *disp_prop)
3624 /* OBJECT = nil means current buffer. */
3625 Lisp_Object object, object1;
3626 Lisp_Object pos, spec, limpos;
3627 int string_p = (string && (STRINGP (string->lstring) || string->s));
3628 ptrdiff_t eob = string_p ? string->schars : ZV;
3629 ptrdiff_t begb = string_p ? 0 : BEGV;
3630 ptrdiff_t bufpos, charpos = CHARPOS (*position);
3631 ptrdiff_t lim =
3632 (charpos < eob - MAX_DISP_SCAN) ? charpos + MAX_DISP_SCAN : eob;
3633 struct text_pos tpos;
3634 int rv = 0;
3636 if (string && STRINGP (string->lstring))
3637 object1 = object = string->lstring;
3638 else if (w && !string_p)
3640 XSETWINDOW (object, w);
3641 object1 = Qnil;
3643 else
3644 object1 = object = Qnil;
3646 *disp_prop = 1;
3648 if (charpos >= eob
3649 /* We don't support display properties whose values are strings
3650 that have display string properties. */
3651 || string->from_disp_str
3652 /* C strings cannot have display properties. */
3653 || (string->s && !STRINGP (object)))
3655 *disp_prop = 0;
3656 return eob;
3659 /* If the character at CHARPOS is where the display string begins,
3660 return CHARPOS. */
3661 pos = make_number (charpos);
3662 if (STRINGP (object))
3663 bufpos = string->bufpos;
3664 else
3665 bufpos = charpos;
3666 tpos = *position;
3667 if (!NILP (spec = Fget_char_property (pos, Qdisplay, object))
3668 && (charpos <= begb
3669 || !EQ (Fget_char_property (make_number (charpos - 1), Qdisplay,
3670 object),
3671 spec))
3672 && (rv = handle_display_spec (NULL, spec, object, Qnil, &tpos, bufpos,
3673 frame_window_p)))
3675 if (rv == 2)
3676 *disp_prop = 2;
3677 return charpos;
3680 /* Look forward for the first character with a `display' property
3681 that will replace the underlying text when displayed. */
3682 limpos = make_number (lim);
3683 do {
3684 pos = Fnext_single_char_property_change (pos, Qdisplay, object1, limpos);
3685 CHARPOS (tpos) = XFASTINT (pos);
3686 if (CHARPOS (tpos) >= lim)
3688 *disp_prop = 0;
3689 break;
3691 if (STRINGP (object))
3692 BYTEPOS (tpos) = string_char_to_byte (object, CHARPOS (tpos));
3693 else
3694 BYTEPOS (tpos) = CHAR_TO_BYTE (CHARPOS (tpos));
3695 spec = Fget_char_property (pos, Qdisplay, object);
3696 if (!STRINGP (object))
3697 bufpos = CHARPOS (tpos);
3698 } while (NILP (spec)
3699 || !(rv = handle_display_spec (NULL, spec, object, Qnil, &tpos,
3700 bufpos, frame_window_p)));
3701 if (rv == 2)
3702 *disp_prop = 2;
3704 return CHARPOS (tpos);
3707 /* Return the character position of the end of the display string that
3708 started at CHARPOS. If there's no display string at CHARPOS,
3709 return -1. A display string is either an overlay with `display'
3710 property whose value is a string or a `display' text property whose
3711 value is a string. */
3712 ptrdiff_t
3713 compute_display_string_end (ptrdiff_t charpos, struct bidi_string_data *string)
3715 /* OBJECT = nil means current buffer. */
3716 Lisp_Object object =
3717 (string && STRINGP (string->lstring)) ? string->lstring : Qnil;
3718 Lisp_Object pos = make_number (charpos);
3719 ptrdiff_t eob =
3720 (STRINGP (object) || (string && string->s)) ? string->schars : ZV;
3722 if (charpos >= eob || (string->s && !STRINGP (object)))
3723 return eob;
3725 /* It could happen that the display property or overlay was removed
3726 since we found it in compute_display_string_pos above. One way
3727 this can happen is if JIT font-lock was called (through
3728 handle_fontified_prop), and jit-lock-functions remove text
3729 properties or overlays from the portion of buffer that includes
3730 CHARPOS. Muse mode is known to do that, for example. In this
3731 case, we return -1 to the caller, to signal that no display
3732 string is actually present at CHARPOS. See bidi_fetch_char for
3733 how this is handled.
3735 An alternative would be to never look for display properties past
3736 it->stop_charpos. But neither compute_display_string_pos nor
3737 bidi_fetch_char that calls it know or care where the next
3738 stop_charpos is. */
3739 if (NILP (Fget_char_property (pos, Qdisplay, object)))
3740 return -1;
3742 /* Look forward for the first character where the `display' property
3743 changes. */
3744 pos = Fnext_single_char_property_change (pos, Qdisplay, object, Qnil);
3746 return XFASTINT (pos);
3751 /***********************************************************************
3752 Fontification
3753 ***********************************************************************/
3755 /* Handle changes in the `fontified' property of the current buffer by
3756 calling hook functions from Qfontification_functions to fontify
3757 regions of text. */
3759 static enum prop_handled
3760 handle_fontified_prop (struct it *it)
3762 Lisp_Object prop, pos;
3763 enum prop_handled handled = HANDLED_NORMALLY;
3765 if (!NILP (Vmemory_full))
3766 return handled;
3768 /* Get the value of the `fontified' property at IT's current buffer
3769 position. (The `fontified' property doesn't have a special
3770 meaning in strings.) If the value is nil, call functions from
3771 Qfontification_functions. */
3772 if (!STRINGP (it->string)
3773 && it->s == NULL
3774 && !NILP (Vfontification_functions)
3775 && !NILP (Vrun_hooks)
3776 && (pos = make_number (IT_CHARPOS (*it)),
3777 prop = Fget_char_property (pos, Qfontified, Qnil),
3778 /* Ignore the special cased nil value always present at EOB since
3779 no amount of fontifying will be able to change it. */
3780 NILP (prop) && IT_CHARPOS (*it) < Z))
3782 ptrdiff_t count = SPECPDL_INDEX ();
3783 Lisp_Object val;
3784 struct buffer *obuf = current_buffer;
3785 ptrdiff_t begv = BEGV, zv = ZV;
3786 bool old_clip_changed = current_buffer->clip_changed;
3788 val = Vfontification_functions;
3789 specbind (Qfontification_functions, Qnil);
3791 eassert (it->end_charpos == ZV);
3793 if (!CONSP (val) || EQ (XCAR (val), Qlambda))
3794 safe_call1 (val, pos);
3795 else
3797 Lisp_Object fns, fn;
3798 struct gcpro gcpro1, gcpro2;
3800 fns = Qnil;
3801 GCPRO2 (val, fns);
3803 for (; CONSP (val); val = XCDR (val))
3805 fn = XCAR (val);
3807 if (EQ (fn, Qt))
3809 /* A value of t indicates this hook has a local
3810 binding; it means to run the global binding too.
3811 In a global value, t should not occur. If it
3812 does, we must ignore it to avoid an endless
3813 loop. */
3814 for (fns = Fdefault_value (Qfontification_functions);
3815 CONSP (fns);
3816 fns = XCDR (fns))
3818 fn = XCAR (fns);
3819 if (!EQ (fn, Qt))
3820 safe_call1 (fn, pos);
3823 else
3824 safe_call1 (fn, pos);
3827 UNGCPRO;
3830 unbind_to (count, Qnil);
3832 /* Fontification functions routinely call `save-restriction'.
3833 Normally, this tags clip_changed, which can confuse redisplay
3834 (see discussion in Bug#6671). Since we don't perform any
3835 special handling of fontification changes in the case where
3836 `save-restriction' isn't called, there's no point doing so in
3837 this case either. So, if the buffer's restrictions are
3838 actually left unchanged, reset clip_changed. */
3839 if (obuf == current_buffer)
3841 if (begv == BEGV && zv == ZV)
3842 current_buffer->clip_changed = old_clip_changed;
3844 /* There isn't much we can reasonably do to protect against
3845 misbehaving fontification, but here's a fig leaf. */
3846 else if (BUFFER_LIVE_P (obuf))
3847 set_buffer_internal_1 (obuf);
3849 /* The fontification code may have added/removed text.
3850 It could do even a lot worse, but let's at least protect against
3851 the most obvious case where only the text past `pos' gets changed',
3852 as is/was done in grep.el where some escapes sequences are turned
3853 into face properties (bug#7876). */
3854 it->end_charpos = ZV;
3856 /* Return HANDLED_RECOMPUTE_PROPS only if function fontified
3857 something. This avoids an endless loop if they failed to
3858 fontify the text for which reason ever. */
3859 if (!NILP (Fget_char_property (pos, Qfontified, Qnil)))
3860 handled = HANDLED_RECOMPUTE_PROPS;
3863 return handled;
3868 /***********************************************************************
3869 Faces
3870 ***********************************************************************/
3872 /* Set up iterator IT from face properties at its current position.
3873 Called from handle_stop. */
3875 static enum prop_handled
3876 handle_face_prop (struct it *it)
3878 int new_face_id;
3879 ptrdiff_t next_stop;
3881 if (!STRINGP (it->string))
3883 new_face_id
3884 = face_at_buffer_position (it->w,
3885 IT_CHARPOS (*it),
3886 &next_stop,
3887 (IT_CHARPOS (*it)
3888 + TEXT_PROP_DISTANCE_LIMIT),
3889 0, it->base_face_id);
3891 /* Is this a start of a run of characters with box face?
3892 Caveat: this can be called for a freshly initialized
3893 iterator; face_id is -1 in this case. We know that the new
3894 face will not change until limit, i.e. if the new face has a
3895 box, all characters up to limit will have one. But, as
3896 usual, we don't know whether limit is really the end. */
3897 if (new_face_id != it->face_id)
3899 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3900 /* If it->face_id is -1, old_face below will be NULL, see
3901 the definition of FACE_FROM_ID. This will happen if this
3902 is the initial call that gets the face. */
3903 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
3905 /* If the value of face_id of the iterator is -1, we have to
3906 look in front of IT's position and see whether there is a
3907 face there that's different from new_face_id. */
3908 if (!old_face && IT_CHARPOS (*it) > BEG)
3910 int prev_face_id = face_before_it_pos (it);
3912 old_face = FACE_FROM_ID (it->f, prev_face_id);
3915 /* If the new face has a box, but the old face does not,
3916 this is the start of a run of characters with box face,
3917 i.e. this character has a shadow on the left side. */
3918 it->start_of_box_run_p = (new_face->box != FACE_NO_BOX
3919 && (old_face == NULL || !old_face->box));
3920 it->face_box_p = new_face->box != FACE_NO_BOX;
3923 else
3925 int base_face_id;
3926 ptrdiff_t bufpos;
3927 int i;
3928 Lisp_Object from_overlay
3929 = (it->current.overlay_string_index >= 0
3930 ? it->string_overlays[it->current.overlay_string_index
3931 % OVERLAY_STRING_CHUNK_SIZE]
3932 : Qnil);
3934 /* See if we got to this string directly or indirectly from
3935 an overlay property. That includes the before-string or
3936 after-string of an overlay, strings in display properties
3937 provided by an overlay, their text properties, etc.
3939 FROM_OVERLAY is the overlay that brought us here, or nil if none. */
3940 if (! NILP (from_overlay))
3941 for (i = it->sp - 1; i >= 0; i--)
3943 if (it->stack[i].current.overlay_string_index >= 0)
3944 from_overlay
3945 = it->string_overlays[it->stack[i].current.overlay_string_index
3946 % OVERLAY_STRING_CHUNK_SIZE];
3947 else if (! NILP (it->stack[i].from_overlay))
3948 from_overlay = it->stack[i].from_overlay;
3950 if (!NILP (from_overlay))
3951 break;
3954 if (! NILP (from_overlay))
3956 bufpos = IT_CHARPOS (*it);
3957 /* For a string from an overlay, the base face depends
3958 only on text properties and ignores overlays. */
3959 base_face_id
3960 = face_for_overlay_string (it->w,
3961 IT_CHARPOS (*it),
3962 &next_stop,
3963 (IT_CHARPOS (*it)
3964 + TEXT_PROP_DISTANCE_LIMIT),
3966 from_overlay);
3968 else
3970 bufpos = 0;
3972 /* For strings from a `display' property, use the face at
3973 IT's current buffer position as the base face to merge
3974 with, so that overlay strings appear in the same face as
3975 surrounding text, unless they specify their own faces.
3976 For strings from wrap-prefix and line-prefix properties,
3977 use the default face, possibly remapped via
3978 Vface_remapping_alist. */
3979 /* Note that the fact that we use the face at _buffer_
3980 position means that a 'display' property on an overlay
3981 string will not inherit the face of that overlay string,
3982 but will instead revert to the face of buffer text
3983 covered by the overlay. This is visible, e.g., when the
3984 overlay specifies a box face, but neither the buffer nor
3985 the display string do. This sounds like a design bug,
3986 but Emacs always did that since v21.1, so changing that
3987 might be a big deal. */
3988 base_face_id = it->string_from_prefix_prop_p
3989 ? (!NILP (Vface_remapping_alist)
3990 ? lookup_basic_face (it->f, DEFAULT_FACE_ID)
3991 : DEFAULT_FACE_ID)
3992 : underlying_face_id (it);
3995 new_face_id = face_at_string_position (it->w,
3996 it->string,
3997 IT_STRING_CHARPOS (*it),
3998 bufpos,
3999 &next_stop,
4000 base_face_id, 0);
4002 /* Is this a start of a run of characters with box? Caveat:
4003 this can be called for a freshly allocated iterator; face_id
4004 is -1 is this case. We know that the new face will not
4005 change until the next check pos, i.e. if the new face has a
4006 box, all characters up to that position will have a
4007 box. But, as usual, we don't know whether that position
4008 is really the end. */
4009 if (new_face_id != it->face_id)
4011 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
4012 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
4014 /* If new face has a box but old face hasn't, this is the
4015 start of a run of characters with box, i.e. it has a
4016 shadow on the left side. */
4017 it->start_of_box_run_p
4018 = new_face->box && (old_face == NULL || !old_face->box);
4019 it->face_box_p = new_face->box != FACE_NO_BOX;
4023 it->face_id = new_face_id;
4024 return HANDLED_NORMALLY;
4028 /* Return the ID of the face ``underlying'' IT's current position,
4029 which is in a string. If the iterator is associated with a
4030 buffer, return the face at IT's current buffer position.
4031 Otherwise, use the iterator's base_face_id. */
4033 static int
4034 underlying_face_id (struct it *it)
4036 int face_id = it->base_face_id, i;
4038 eassert (STRINGP (it->string));
4040 for (i = it->sp - 1; i >= 0; --i)
4041 if (NILP (it->stack[i].string))
4042 face_id = it->stack[i].face_id;
4044 return face_id;
4048 /* Compute the face one character before or after the current position
4049 of IT, in the visual order. BEFORE_P non-zero means get the face
4050 in front (to the left in L2R paragraphs, to the right in R2L
4051 paragraphs) of IT's screen position. Value is the ID of the face. */
4053 static int
4054 face_before_or_after_it_pos (struct it *it, int before_p)
4056 int face_id, limit;
4057 ptrdiff_t next_check_charpos;
4058 struct it it_copy;
4059 void *it_copy_data = NULL;
4061 eassert (it->s == NULL);
4063 if (STRINGP (it->string))
4065 ptrdiff_t bufpos, charpos;
4066 int base_face_id;
4068 /* No face change past the end of the string (for the case
4069 we are padding with spaces). No face change before the
4070 string start. */
4071 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string)
4072 || (IT_STRING_CHARPOS (*it) == 0 && before_p))
4073 return it->face_id;
4075 if (!it->bidi_p)
4077 /* Set charpos to the position before or after IT's current
4078 position, in the logical order, which in the non-bidi
4079 case is the same as the visual order. */
4080 if (before_p)
4081 charpos = IT_STRING_CHARPOS (*it) - 1;
4082 else if (it->what == IT_COMPOSITION)
4083 /* For composition, we must check the character after the
4084 composition. */
4085 charpos = IT_STRING_CHARPOS (*it) + it->cmp_it.nchars;
4086 else
4087 charpos = IT_STRING_CHARPOS (*it) + 1;
4089 else
4091 if (before_p)
4093 /* With bidi iteration, the character before the current
4094 in the visual order cannot be found by simple
4095 iteration, because "reverse" reordering is not
4096 supported. Instead, we need to use the move_it_*
4097 family of functions. */
4098 /* Ignore face changes before the first visible
4099 character on this display line. */
4100 if (it->current_x <= it->first_visible_x)
4101 return it->face_id;
4102 SAVE_IT (it_copy, *it, it_copy_data);
4103 /* Implementation note: Since move_it_in_display_line
4104 works in the iterator geometry, and thinks the first
4105 character is always the leftmost, even in R2L lines,
4106 we don't need to distinguish between the R2L and L2R
4107 cases here. */
4108 move_it_in_display_line (&it_copy, SCHARS (it_copy.string),
4109 it_copy.current_x - 1, MOVE_TO_X);
4110 charpos = IT_STRING_CHARPOS (it_copy);
4111 RESTORE_IT (it, it, it_copy_data);
4113 else
4115 /* Set charpos to the string position of the character
4116 that comes after IT's current position in the visual
4117 order. */
4118 int n = (it->what == IT_COMPOSITION ? it->cmp_it.nchars : 1);
4120 it_copy = *it;
4121 while (n--)
4122 bidi_move_to_visually_next (&it_copy.bidi_it);
4124 charpos = it_copy.bidi_it.charpos;
4127 eassert (0 <= charpos && charpos <= SCHARS (it->string));
4129 if (it->current.overlay_string_index >= 0)
4130 bufpos = IT_CHARPOS (*it);
4131 else
4132 bufpos = 0;
4134 base_face_id = underlying_face_id (it);
4136 /* Get the face for ASCII, or unibyte. */
4137 face_id = face_at_string_position (it->w,
4138 it->string,
4139 charpos,
4140 bufpos,
4141 &next_check_charpos,
4142 base_face_id, 0);
4144 /* Correct the face for charsets different from ASCII. Do it
4145 for the multibyte case only. The face returned above is
4146 suitable for unibyte text if IT->string is unibyte. */
4147 if (STRING_MULTIBYTE (it->string))
4149 struct text_pos pos1 = string_pos (charpos, it->string);
4150 const unsigned char *p = SDATA (it->string) + BYTEPOS (pos1);
4151 int c, len;
4152 struct face *face = FACE_FROM_ID (it->f, face_id);
4154 c = string_char_and_length (p, &len);
4155 face_id = FACE_FOR_CHAR (it->f, face, c, charpos, it->string);
4158 else
4160 struct text_pos pos;
4162 if ((IT_CHARPOS (*it) >= ZV && !before_p)
4163 || (IT_CHARPOS (*it) <= BEGV && before_p))
4164 return it->face_id;
4166 limit = IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT;
4167 pos = it->current.pos;
4169 if (!it->bidi_p)
4171 if (before_p)
4172 DEC_TEXT_POS (pos, it->multibyte_p);
4173 else
4175 if (it->what == IT_COMPOSITION)
4177 /* For composition, we must check the position after
4178 the composition. */
4179 pos.charpos += it->cmp_it.nchars;
4180 pos.bytepos += it->len;
4182 else
4183 INC_TEXT_POS (pos, it->multibyte_p);
4186 else
4188 if (before_p)
4190 /* With bidi iteration, the character before the current
4191 in the visual order cannot be found by simple
4192 iteration, because "reverse" reordering is not
4193 supported. Instead, we need to use the move_it_*
4194 family of functions. */
4195 /* Ignore face changes before the first visible
4196 character on this display line. */
4197 if (it->current_x <= it->first_visible_x)
4198 return it->face_id;
4199 SAVE_IT (it_copy, *it, it_copy_data);
4200 /* Implementation note: Since move_it_in_display_line
4201 works in the iterator geometry, and thinks the first
4202 character is always the leftmost, even in R2L lines,
4203 we don't need to distinguish between the R2L and L2R
4204 cases here. */
4205 move_it_in_display_line (&it_copy, ZV,
4206 it_copy.current_x - 1, MOVE_TO_X);
4207 pos = it_copy.current.pos;
4208 RESTORE_IT (it, it, it_copy_data);
4210 else
4212 /* Set charpos to the buffer position of the character
4213 that comes after IT's current position in the visual
4214 order. */
4215 int n = (it->what == IT_COMPOSITION ? it->cmp_it.nchars : 1);
4217 it_copy = *it;
4218 while (n--)
4219 bidi_move_to_visually_next (&it_copy.bidi_it);
4221 SET_TEXT_POS (pos,
4222 it_copy.bidi_it.charpos, it_copy.bidi_it.bytepos);
4225 eassert (BEGV <= CHARPOS (pos) && CHARPOS (pos) <= ZV);
4227 /* Determine face for CHARSET_ASCII, or unibyte. */
4228 face_id = face_at_buffer_position (it->w,
4229 CHARPOS (pos),
4230 &next_check_charpos,
4231 limit, 0, -1);
4233 /* Correct the face for charsets different from ASCII. Do it
4234 for the multibyte case only. The face returned above is
4235 suitable for unibyte text if current_buffer is unibyte. */
4236 if (it->multibyte_p)
4238 int c = FETCH_MULTIBYTE_CHAR (BYTEPOS (pos));
4239 struct face *face = FACE_FROM_ID (it->f, face_id);
4240 face_id = FACE_FOR_CHAR (it->f, face, c, CHARPOS (pos), Qnil);
4244 return face_id;
4249 /***********************************************************************
4250 Invisible text
4251 ***********************************************************************/
4253 /* Set up iterator IT from invisible properties at its current
4254 position. Called from handle_stop. */
4256 static enum prop_handled
4257 handle_invisible_prop (struct it *it)
4259 enum prop_handled handled = HANDLED_NORMALLY;
4260 int invis_p;
4261 Lisp_Object prop;
4263 if (STRINGP (it->string))
4265 Lisp_Object end_charpos, limit, charpos;
4267 /* Get the value of the invisible text property at the
4268 current position. Value will be nil if there is no such
4269 property. */
4270 charpos = make_number (IT_STRING_CHARPOS (*it));
4271 prop = Fget_text_property (charpos, Qinvisible, it->string);
4272 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
4274 if (invis_p && IT_STRING_CHARPOS (*it) < it->end_charpos)
4276 /* Record whether we have to display an ellipsis for the
4277 invisible text. */
4278 int display_ellipsis_p = (invis_p == 2);
4279 ptrdiff_t len, endpos;
4281 handled = HANDLED_RECOMPUTE_PROPS;
4283 /* Get the position at which the next visible text can be
4284 found in IT->string, if any. */
4285 endpos = len = SCHARS (it->string);
4286 XSETINT (limit, len);
4289 end_charpos = Fnext_single_property_change (charpos, Qinvisible,
4290 it->string, limit);
4291 if (INTEGERP (end_charpos))
4293 endpos = XFASTINT (end_charpos);
4294 prop = Fget_text_property (end_charpos, Qinvisible, it->string);
4295 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
4296 if (invis_p == 2)
4297 display_ellipsis_p = true;
4300 while (invis_p && endpos < len);
4302 if (display_ellipsis_p)
4303 it->ellipsis_p = true;
4305 if (endpos < len)
4307 /* Text at END_CHARPOS is visible. Move IT there. */
4308 struct text_pos old;
4309 ptrdiff_t oldpos;
4311 old = it->current.string_pos;
4312 oldpos = CHARPOS (old);
4313 if (it->bidi_p)
4315 if (it->bidi_it.first_elt
4316 && it->bidi_it.charpos < SCHARS (it->string))
4317 bidi_paragraph_init (it->paragraph_embedding,
4318 &it->bidi_it, 1);
4319 /* Bidi-iterate out of the invisible text. */
4322 bidi_move_to_visually_next (&it->bidi_it);
4324 while (oldpos <= it->bidi_it.charpos
4325 && it->bidi_it.charpos < endpos);
4327 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
4328 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
4329 if (IT_CHARPOS (*it) >= endpos)
4330 it->prev_stop = endpos;
4332 else
4334 IT_STRING_CHARPOS (*it) = XFASTINT (end_charpos);
4335 compute_string_pos (&it->current.string_pos, old, it->string);
4338 else
4340 /* The rest of the string is invisible. If this is an
4341 overlay string, proceed with the next overlay string
4342 or whatever comes and return a character from there. */
4343 if (it->current.overlay_string_index >= 0
4344 && !display_ellipsis_p)
4346 next_overlay_string (it);
4347 /* Don't check for overlay strings when we just
4348 finished processing them. */
4349 handled = HANDLED_OVERLAY_STRING_CONSUMED;
4351 else
4353 IT_STRING_CHARPOS (*it) = SCHARS (it->string);
4354 IT_STRING_BYTEPOS (*it) = SBYTES (it->string);
4359 else
4361 ptrdiff_t newpos, next_stop, start_charpos, tem;
4362 Lisp_Object pos, overlay;
4364 /* First of all, is there invisible text at this position? */
4365 tem = start_charpos = IT_CHARPOS (*it);
4366 pos = make_number (tem);
4367 prop = get_char_property_and_overlay (pos, Qinvisible, it->window,
4368 &overlay);
4369 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
4371 /* If we are on invisible text, skip over it. */
4372 if (invis_p && start_charpos < it->end_charpos)
4374 /* Record whether we have to display an ellipsis for the
4375 invisible text. */
4376 int display_ellipsis_p = invis_p == 2;
4378 handled = HANDLED_RECOMPUTE_PROPS;
4380 /* Loop skipping over invisible text. The loop is left at
4381 ZV or with IT on the first char being visible again. */
4384 /* Try to skip some invisible text. Return value is the
4385 position reached which can be equal to where we start
4386 if there is nothing invisible there. This skips both
4387 over invisible text properties and overlays with
4388 invisible property. */
4389 newpos = skip_invisible (tem, &next_stop, ZV, it->window);
4391 /* If we skipped nothing at all we weren't at invisible
4392 text in the first place. If everything to the end of
4393 the buffer was skipped, end the loop. */
4394 if (newpos == tem || newpos >= ZV)
4395 invis_p = 0;
4396 else
4398 /* We skipped some characters but not necessarily
4399 all there are. Check if we ended up on visible
4400 text. Fget_char_property returns the property of
4401 the char before the given position, i.e. if we
4402 get invis_p = 0, this means that the char at
4403 newpos is visible. */
4404 pos = make_number (newpos);
4405 prop = Fget_char_property (pos, Qinvisible, it->window);
4406 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
4409 /* If we ended up on invisible text, proceed to
4410 skip starting with next_stop. */
4411 if (invis_p)
4412 tem = next_stop;
4414 /* If there are adjacent invisible texts, don't lose the
4415 second one's ellipsis. */
4416 if (invis_p == 2)
4417 display_ellipsis_p = true;
4419 while (invis_p);
4421 /* The position newpos is now either ZV or on visible text. */
4422 if (it->bidi_p)
4424 ptrdiff_t bpos = CHAR_TO_BYTE (newpos);
4425 int on_newline
4426 = bpos == ZV_BYTE || FETCH_BYTE (bpos) == '\n';
4427 int after_newline
4428 = newpos <= BEGV || FETCH_BYTE (bpos - 1) == '\n';
4430 /* If the invisible text ends on a newline or on a
4431 character after a newline, we can avoid the costly,
4432 character by character, bidi iteration to NEWPOS, and
4433 instead simply reseat the iterator there. That's
4434 because all bidi reordering information is tossed at
4435 the newline. This is a big win for modes that hide
4436 complete lines, like Outline, Org, etc. */
4437 if (on_newline || after_newline)
4439 struct text_pos tpos;
4440 bidi_dir_t pdir = it->bidi_it.paragraph_dir;
4442 SET_TEXT_POS (tpos, newpos, bpos);
4443 reseat_1 (it, tpos, 0);
4444 /* If we reseat on a newline/ZV, we need to prep the
4445 bidi iterator for advancing to the next character
4446 after the newline/EOB, keeping the current paragraph
4447 direction (so that PRODUCE_GLYPHS does TRT wrt
4448 prepending/appending glyphs to a glyph row). */
4449 if (on_newline)
4451 it->bidi_it.first_elt = 0;
4452 it->bidi_it.paragraph_dir = pdir;
4453 it->bidi_it.ch = (bpos == ZV_BYTE) ? -1 : '\n';
4454 it->bidi_it.nchars = 1;
4455 it->bidi_it.ch_len = 1;
4458 else /* Must use the slow method. */
4460 /* With bidi iteration, the region of invisible text
4461 could start and/or end in the middle of a
4462 non-base embedding level. Therefore, we need to
4463 skip invisible text using the bidi iterator,
4464 starting at IT's current position, until we find
4465 ourselves outside of the invisible text.
4466 Skipping invisible text _after_ bidi iteration
4467 avoids affecting the visual order of the
4468 displayed text when invisible properties are
4469 added or removed. */
4470 if (it->bidi_it.first_elt && it->bidi_it.charpos < ZV)
4472 /* If we were `reseat'ed to a new paragraph,
4473 determine the paragraph base direction. We
4474 need to do it now because
4475 next_element_from_buffer may not have a
4476 chance to do it, if we are going to skip any
4477 text at the beginning, which resets the
4478 FIRST_ELT flag. */
4479 bidi_paragraph_init (it->paragraph_embedding,
4480 &it->bidi_it, 1);
4484 bidi_move_to_visually_next (&it->bidi_it);
4486 while (it->stop_charpos <= it->bidi_it.charpos
4487 && it->bidi_it.charpos < newpos);
4488 IT_CHARPOS (*it) = it->bidi_it.charpos;
4489 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
4490 /* If we overstepped NEWPOS, record its position in
4491 the iterator, so that we skip invisible text if
4492 later the bidi iteration lands us in the
4493 invisible region again. */
4494 if (IT_CHARPOS (*it) >= newpos)
4495 it->prev_stop = newpos;
4498 else
4500 IT_CHARPOS (*it) = newpos;
4501 IT_BYTEPOS (*it) = CHAR_TO_BYTE (newpos);
4504 /* If there are before-strings at the start of invisible
4505 text, and the text is invisible because of a text
4506 property, arrange to show before-strings because 20.x did
4507 it that way. (If the text is invisible because of an
4508 overlay property instead of a text property, this is
4509 already handled in the overlay code.) */
4510 if (NILP (overlay)
4511 && get_overlay_strings (it, it->stop_charpos))
4513 handled = HANDLED_RECOMPUTE_PROPS;
4514 if (it->sp > 0)
4516 it->stack[it->sp - 1].display_ellipsis_p = display_ellipsis_p;
4517 /* The call to get_overlay_strings above recomputes
4518 it->stop_charpos, but it only considers changes
4519 in properties and overlays beyond iterator's
4520 current position. This causes us to miss changes
4521 that happen exactly where the invisible property
4522 ended. So we play it safe here and force the
4523 iterator to check for potential stop positions
4524 immediately after the invisible text. Note that
4525 if get_overlay_strings returns non-zero, it
4526 normally also pushed the iterator stack, so we
4527 need to update the stop position in the slot
4528 below the current one. */
4529 it->stack[it->sp - 1].stop_charpos
4530 = CHARPOS (it->stack[it->sp - 1].current.pos);
4533 else if (display_ellipsis_p)
4535 /* Make sure that the glyphs of the ellipsis will get
4536 correct `charpos' values. If we would not update
4537 it->position here, the glyphs would belong to the
4538 last visible character _before_ the invisible
4539 text, which confuses `set_cursor_from_row'.
4541 We use the last invisible position instead of the
4542 first because this way the cursor is always drawn on
4543 the first "." of the ellipsis, whenever PT is inside
4544 the invisible text. Otherwise the cursor would be
4545 placed _after_ the ellipsis when the point is after the
4546 first invisible character. */
4547 if (!STRINGP (it->object))
4549 it->position.charpos = newpos - 1;
4550 it->position.bytepos = CHAR_TO_BYTE (it->position.charpos);
4552 it->ellipsis_p = true;
4553 /* Let the ellipsis display before
4554 considering any properties of the following char.
4555 Fixes jasonr@gnu.org 01 Oct 07 bug. */
4556 handled = HANDLED_RETURN;
4561 return handled;
4565 /* Make iterator IT return `...' next.
4566 Replaces LEN characters from buffer. */
4568 static void
4569 setup_for_ellipsis (struct it *it, int len)
4571 /* Use the display table definition for `...'. Invalid glyphs
4572 will be handled by the method returning elements from dpvec. */
4573 if (it->dp && VECTORP (DISP_INVIS_VECTOR (it->dp)))
4575 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
4576 it->dpvec = v->contents;
4577 it->dpend = v->contents + v->header.size;
4579 else
4581 /* Default `...'. */
4582 it->dpvec = default_invis_vector;
4583 it->dpend = default_invis_vector + 3;
4586 it->dpvec_char_len = len;
4587 it->current.dpvec_index = 0;
4588 it->dpvec_face_id = -1;
4590 /* Remember the current face id in case glyphs specify faces.
4591 IT's face is restored in set_iterator_to_next.
4592 saved_face_id was set to preceding char's face in handle_stop. */
4593 if (it->saved_face_id < 0 || it->saved_face_id != it->face_id)
4594 it->saved_face_id = it->face_id = DEFAULT_FACE_ID;
4596 it->method = GET_FROM_DISPLAY_VECTOR;
4597 it->ellipsis_p = true;
4602 /***********************************************************************
4603 'display' property
4604 ***********************************************************************/
4606 /* Set up iterator IT from `display' property at its current position.
4607 Called from handle_stop.
4608 We return HANDLED_RETURN if some part of the display property
4609 overrides the display of the buffer text itself.
4610 Otherwise we return HANDLED_NORMALLY. */
4612 static enum prop_handled
4613 handle_display_prop (struct it *it)
4615 Lisp_Object propval, object, overlay;
4616 struct text_pos *position;
4617 ptrdiff_t bufpos;
4618 /* Nonzero if some property replaces the display of the text itself. */
4619 int display_replaced_p = 0;
4621 if (STRINGP (it->string))
4623 object = it->string;
4624 position = &it->current.string_pos;
4625 bufpos = CHARPOS (it->current.pos);
4627 else
4629 XSETWINDOW (object, it->w);
4630 position = &it->current.pos;
4631 bufpos = CHARPOS (*position);
4634 /* Reset those iterator values set from display property values. */
4635 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
4636 it->space_width = Qnil;
4637 it->font_height = Qnil;
4638 it->voffset = 0;
4640 /* We don't support recursive `display' properties, i.e. string
4641 values that have a string `display' property, that have a string
4642 `display' property etc. */
4643 if (!it->string_from_display_prop_p)
4644 it->area = TEXT_AREA;
4646 propval = get_char_property_and_overlay (make_number (position->charpos),
4647 Qdisplay, object, &overlay);
4648 if (NILP (propval))
4649 return HANDLED_NORMALLY;
4650 /* Now OVERLAY is the overlay that gave us this property, or nil
4651 if it was a text property. */
4653 if (!STRINGP (it->string))
4654 object = it->w->contents;
4656 display_replaced_p = handle_display_spec (it, propval, object, overlay,
4657 position, bufpos,
4658 FRAME_WINDOW_P (it->f));
4660 return display_replaced_p ? HANDLED_RETURN : HANDLED_NORMALLY;
4663 /* Subroutine of handle_display_prop. Returns non-zero if the display
4664 specification in SPEC is a replacing specification, i.e. it would
4665 replace the text covered by `display' property with something else,
4666 such as an image or a display string. If SPEC includes any kind or
4667 `(space ...) specification, the value is 2; this is used by
4668 compute_display_string_pos, which see.
4670 See handle_single_display_spec for documentation of arguments.
4671 frame_window_p is non-zero if the window being redisplayed is on a
4672 GUI frame; this argument is used only if IT is NULL, see below.
4674 IT can be NULL, if this is called by the bidi reordering code
4675 through compute_display_string_pos, which see. In that case, this
4676 function only examines SPEC, but does not otherwise "handle" it, in
4677 the sense that it doesn't set up members of IT from the display
4678 spec. */
4679 static int
4680 handle_display_spec (struct it *it, Lisp_Object spec, Lisp_Object object,
4681 Lisp_Object overlay, struct text_pos *position,
4682 ptrdiff_t bufpos, int frame_window_p)
4684 int replacing_p = 0;
4685 int rv;
4687 if (CONSP (spec)
4688 /* Simple specifications. */
4689 && !EQ (XCAR (spec), Qimage)
4690 && !EQ (XCAR (spec), Qspace)
4691 && !EQ (XCAR (spec), Qwhen)
4692 && !EQ (XCAR (spec), Qslice)
4693 && !EQ (XCAR (spec), Qspace_width)
4694 && !EQ (XCAR (spec), Qheight)
4695 && !EQ (XCAR (spec), Qraise)
4696 /* Marginal area specifications. */
4697 && !(CONSP (XCAR (spec)) && EQ (XCAR (XCAR (spec)), Qmargin))
4698 && !EQ (XCAR (spec), Qleft_fringe)
4699 && !EQ (XCAR (spec), Qright_fringe)
4700 && !NILP (XCAR (spec)))
4702 for (; CONSP (spec); spec = XCDR (spec))
4704 if ((rv = handle_single_display_spec (it, XCAR (spec), object,
4705 overlay, position, bufpos,
4706 replacing_p, frame_window_p)))
4708 replacing_p = rv;
4709 /* If some text in a string is replaced, `position' no
4710 longer points to the position of `object'. */
4711 if (!it || STRINGP (object))
4712 break;
4716 else if (VECTORP (spec))
4718 ptrdiff_t i;
4719 for (i = 0; i < ASIZE (spec); ++i)
4720 if ((rv = handle_single_display_spec (it, AREF (spec, i), object,
4721 overlay, position, bufpos,
4722 replacing_p, frame_window_p)))
4724 replacing_p = rv;
4725 /* If some text in a string is replaced, `position' no
4726 longer points to the position of `object'. */
4727 if (!it || STRINGP (object))
4728 break;
4731 else
4733 if ((rv = handle_single_display_spec (it, spec, object, overlay,
4734 position, bufpos, 0,
4735 frame_window_p)))
4736 replacing_p = rv;
4739 return replacing_p;
4742 /* Value is the position of the end of the `display' property starting
4743 at START_POS in OBJECT. */
4745 static struct text_pos
4746 display_prop_end (struct it *it, Lisp_Object object, struct text_pos start_pos)
4748 Lisp_Object end;
4749 struct text_pos end_pos;
4751 end = Fnext_single_char_property_change (make_number (CHARPOS (start_pos)),
4752 Qdisplay, object, Qnil);
4753 CHARPOS (end_pos) = XFASTINT (end);
4754 if (STRINGP (object))
4755 compute_string_pos (&end_pos, start_pos, it->string);
4756 else
4757 BYTEPOS (end_pos) = CHAR_TO_BYTE (XFASTINT (end));
4759 return end_pos;
4763 /* Set up IT from a single `display' property specification SPEC. OBJECT
4764 is the object in which the `display' property was found. *POSITION
4765 is the position in OBJECT at which the `display' property was found.
4766 BUFPOS is the buffer position of OBJECT (different from POSITION if
4767 OBJECT is not a buffer). DISPLAY_REPLACED_P non-zero means that we
4768 previously saw a display specification which already replaced text
4769 display with something else, for example an image; we ignore such
4770 properties after the first one has been processed.
4772 OVERLAY is the overlay this `display' property came from,
4773 or nil if it was a text property.
4775 If SPEC is a `space' or `image' specification, and in some other
4776 cases too, set *POSITION to the position where the `display'
4777 property ends.
4779 If IT is NULL, only examine the property specification in SPEC, but
4780 don't set up IT. In that case, FRAME_WINDOW_P non-zero means SPEC
4781 is intended to be displayed in a window on a GUI frame.
4783 Value is non-zero if something was found which replaces the display
4784 of buffer or string text. */
4786 static int
4787 handle_single_display_spec (struct it *it, Lisp_Object spec, Lisp_Object object,
4788 Lisp_Object overlay, struct text_pos *position,
4789 ptrdiff_t bufpos, int display_replaced_p,
4790 int frame_window_p)
4792 Lisp_Object form;
4793 Lisp_Object location, value;
4794 struct text_pos start_pos = *position;
4795 int valid_p;
4797 /* If SPEC is a list of the form `(when FORM . VALUE)', evaluate FORM.
4798 If the result is non-nil, use VALUE instead of SPEC. */
4799 form = Qt;
4800 if (CONSP (spec) && EQ (XCAR (spec), Qwhen))
4802 spec = XCDR (spec);
4803 if (!CONSP (spec))
4804 return 0;
4805 form = XCAR (spec);
4806 spec = XCDR (spec);
4809 if (!NILP (form) && !EQ (form, Qt))
4811 ptrdiff_t count = SPECPDL_INDEX ();
4812 struct gcpro gcpro1;
4814 /* Bind `object' to the object having the `display' property, a
4815 buffer or string. Bind `position' to the position in the
4816 object where the property was found, and `buffer-position'
4817 to the current position in the buffer. */
4819 if (NILP (object))
4820 XSETBUFFER (object, current_buffer);
4821 specbind (Qobject, object);
4822 specbind (Qposition, make_number (CHARPOS (*position)));
4823 specbind (Qbuffer_position, make_number (bufpos));
4824 GCPRO1 (form);
4825 form = safe_eval (form);
4826 UNGCPRO;
4827 unbind_to (count, Qnil);
4830 if (NILP (form))
4831 return 0;
4833 /* Handle `(height HEIGHT)' specifications. */
4834 if (CONSP (spec)
4835 && EQ (XCAR (spec), Qheight)
4836 && CONSP (XCDR (spec)))
4838 if (it)
4840 if (!FRAME_WINDOW_P (it->f))
4841 return 0;
4843 it->font_height = XCAR (XCDR (spec));
4844 if (!NILP (it->font_height))
4846 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4847 int new_height = -1;
4849 if (CONSP (it->font_height)
4850 && (EQ (XCAR (it->font_height), Qplus)
4851 || EQ (XCAR (it->font_height), Qminus))
4852 && CONSP (XCDR (it->font_height))
4853 && RANGED_INTEGERP (0, XCAR (XCDR (it->font_height)), INT_MAX))
4855 /* `(+ N)' or `(- N)' where N is an integer. */
4856 int steps = XINT (XCAR (XCDR (it->font_height)));
4857 if (EQ (XCAR (it->font_height), Qplus))
4858 steps = - steps;
4859 it->face_id = smaller_face (it->f, it->face_id, steps);
4861 else if (FUNCTIONP (it->font_height))
4863 /* Call function with current height as argument.
4864 Value is the new height. */
4865 Lisp_Object height;
4866 height = safe_call1 (it->font_height,
4867 face->lface[LFACE_HEIGHT_INDEX]);
4868 if (NUMBERP (height))
4869 new_height = XFLOATINT (height);
4871 else if (NUMBERP (it->font_height))
4873 /* Value is a multiple of the canonical char height. */
4874 struct face *f;
4876 f = FACE_FROM_ID (it->f,
4877 lookup_basic_face (it->f, DEFAULT_FACE_ID));
4878 new_height = (XFLOATINT (it->font_height)
4879 * XINT (f->lface[LFACE_HEIGHT_INDEX]));
4881 else
4883 /* Evaluate IT->font_height with `height' bound to the
4884 current specified height to get the new height. */
4885 ptrdiff_t count = SPECPDL_INDEX ();
4887 specbind (Qheight, face->lface[LFACE_HEIGHT_INDEX]);
4888 value = safe_eval (it->font_height);
4889 unbind_to (count, Qnil);
4891 if (NUMBERP (value))
4892 new_height = XFLOATINT (value);
4895 if (new_height > 0)
4896 it->face_id = face_with_height (it->f, it->face_id, new_height);
4900 return 0;
4903 /* Handle `(space-width WIDTH)'. */
4904 if (CONSP (spec)
4905 && EQ (XCAR (spec), Qspace_width)
4906 && CONSP (XCDR (spec)))
4908 if (it)
4910 if (!FRAME_WINDOW_P (it->f))
4911 return 0;
4913 value = XCAR (XCDR (spec));
4914 if (NUMBERP (value) && XFLOATINT (value) > 0)
4915 it->space_width = value;
4918 return 0;
4921 /* Handle `(slice X Y WIDTH HEIGHT)'. */
4922 if (CONSP (spec)
4923 && EQ (XCAR (spec), Qslice))
4925 Lisp_Object tem;
4927 if (it)
4929 if (!FRAME_WINDOW_P (it->f))
4930 return 0;
4932 if (tem = XCDR (spec), CONSP (tem))
4934 it->slice.x = XCAR (tem);
4935 if (tem = XCDR (tem), CONSP (tem))
4937 it->slice.y = XCAR (tem);
4938 if (tem = XCDR (tem), CONSP (tem))
4940 it->slice.width = XCAR (tem);
4941 if (tem = XCDR (tem), CONSP (tem))
4942 it->slice.height = XCAR (tem);
4948 return 0;
4951 /* Handle `(raise FACTOR)'. */
4952 if (CONSP (spec)
4953 && EQ (XCAR (spec), Qraise)
4954 && CONSP (XCDR (spec)))
4956 if (it)
4958 if (!FRAME_WINDOW_P (it->f))
4959 return 0;
4961 #ifdef HAVE_WINDOW_SYSTEM
4962 value = XCAR (XCDR (spec));
4963 if (NUMBERP (value))
4965 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4966 it->voffset = - (XFLOATINT (value)
4967 * (FONT_HEIGHT (face->font)));
4969 #endif /* HAVE_WINDOW_SYSTEM */
4972 return 0;
4975 /* Don't handle the other kinds of display specifications
4976 inside a string that we got from a `display' property. */
4977 if (it && it->string_from_display_prop_p)
4978 return 0;
4980 /* Characters having this form of property are not displayed, so
4981 we have to find the end of the property. */
4982 if (it)
4984 start_pos = *position;
4985 *position = display_prop_end (it, object, start_pos);
4987 value = Qnil;
4989 /* Stop the scan at that end position--we assume that all
4990 text properties change there. */
4991 if (it)
4992 it->stop_charpos = position->charpos;
4994 /* Handle `(left-fringe BITMAP [FACE])'
4995 and `(right-fringe BITMAP [FACE])'. */
4996 if (CONSP (spec)
4997 && (EQ (XCAR (spec), Qleft_fringe)
4998 || EQ (XCAR (spec), Qright_fringe))
4999 && CONSP (XCDR (spec)))
5001 int fringe_bitmap;
5003 if (it)
5005 if (!FRAME_WINDOW_P (it->f))
5006 /* If we return here, POSITION has been advanced
5007 across the text with this property. */
5009 /* Synchronize the bidi iterator with POSITION. This is
5010 needed because we are not going to push the iterator
5011 on behalf of this display property, so there will be
5012 no pop_it call to do this synchronization for us. */
5013 if (it->bidi_p)
5015 it->position = *position;
5016 iterate_out_of_display_property (it);
5017 *position = it->position;
5019 /* If we were to display this fringe bitmap,
5020 next_element_from_image would have reset this flag.
5021 Do the same, to avoid affecting overlays that
5022 follow. */
5023 it->ignore_overlay_strings_at_pos_p = 0;
5024 return 1;
5027 else if (!frame_window_p)
5028 return 1;
5030 #ifdef HAVE_WINDOW_SYSTEM
5031 value = XCAR (XCDR (spec));
5032 if (!SYMBOLP (value)
5033 || !(fringe_bitmap = lookup_fringe_bitmap (value)))
5034 /* If we return here, POSITION has been advanced
5035 across the text with this property. */
5037 if (it && it->bidi_p)
5039 it->position = *position;
5040 iterate_out_of_display_property (it);
5041 *position = it->position;
5043 if (it)
5044 /* Reset this flag like next_element_from_image would. */
5045 it->ignore_overlay_strings_at_pos_p = 0;
5046 return 1;
5049 if (it)
5051 int face_id = lookup_basic_face (it->f, DEFAULT_FACE_ID);
5053 if (CONSP (XCDR (XCDR (spec))))
5055 Lisp_Object face_name = XCAR (XCDR (XCDR (spec)));
5056 int face_id2 = lookup_derived_face (it->f, face_name,
5057 FRINGE_FACE_ID, 0);
5058 if (face_id2 >= 0)
5059 face_id = face_id2;
5062 /* Save current settings of IT so that we can restore them
5063 when we are finished with the glyph property value. */
5064 push_it (it, position);
5066 it->area = TEXT_AREA;
5067 it->what = IT_IMAGE;
5068 it->image_id = -1; /* no image */
5069 it->position = start_pos;
5070 it->object = NILP (object) ? it->w->contents : object;
5071 it->method = GET_FROM_IMAGE;
5072 it->from_overlay = Qnil;
5073 it->face_id = face_id;
5074 it->from_disp_prop_p = true;
5076 /* Say that we haven't consumed the characters with
5077 `display' property yet. The call to pop_it in
5078 set_iterator_to_next will clean this up. */
5079 *position = start_pos;
5081 if (EQ (XCAR (spec), Qleft_fringe))
5083 it->left_user_fringe_bitmap = fringe_bitmap;
5084 it->left_user_fringe_face_id = face_id;
5086 else
5088 it->right_user_fringe_bitmap = fringe_bitmap;
5089 it->right_user_fringe_face_id = face_id;
5092 #endif /* HAVE_WINDOW_SYSTEM */
5093 return 1;
5096 /* Prepare to handle `((margin left-margin) ...)',
5097 `((margin right-margin) ...)' and `((margin nil) ...)'
5098 prefixes for display specifications. */
5099 location = Qunbound;
5100 if (CONSP (spec) && CONSP (XCAR (spec)))
5102 Lisp_Object tem;
5104 value = XCDR (spec);
5105 if (CONSP (value))
5106 value = XCAR (value);
5108 tem = XCAR (spec);
5109 if (EQ (XCAR (tem), Qmargin)
5110 && (tem = XCDR (tem),
5111 tem = CONSP (tem) ? XCAR (tem) : Qnil,
5112 (NILP (tem)
5113 || EQ (tem, Qleft_margin)
5114 || EQ (tem, Qright_margin))))
5115 location = tem;
5118 if (EQ (location, Qunbound))
5120 location = Qnil;
5121 value = spec;
5124 /* After this point, VALUE is the property after any
5125 margin prefix has been stripped. It must be a string,
5126 an image specification, or `(space ...)'.
5128 LOCATION specifies where to display: `left-margin',
5129 `right-margin' or nil. */
5131 valid_p = (STRINGP (value)
5132 #ifdef HAVE_WINDOW_SYSTEM
5133 || ((it ? FRAME_WINDOW_P (it->f) : frame_window_p)
5134 && valid_image_p (value))
5135 #endif /* not HAVE_WINDOW_SYSTEM */
5136 || (CONSP (value) && EQ (XCAR (value), Qspace)));
5138 if (valid_p && !display_replaced_p)
5140 int retval = 1;
5142 if (!it)
5144 /* Callers need to know whether the display spec is any kind
5145 of `(space ...)' spec that is about to affect text-area
5146 display. */
5147 if (CONSP (value) && EQ (XCAR (value), Qspace) && NILP (location))
5148 retval = 2;
5149 return retval;
5152 /* Save current settings of IT so that we can restore them
5153 when we are finished with the glyph property value. */
5154 push_it (it, position);
5155 it->from_overlay = overlay;
5156 it->from_disp_prop_p = true;
5158 if (NILP (location))
5159 it->area = TEXT_AREA;
5160 else if (EQ (location, Qleft_margin))
5161 it->area = LEFT_MARGIN_AREA;
5162 else
5163 it->area = RIGHT_MARGIN_AREA;
5165 if (STRINGP (value))
5167 it->string = value;
5168 it->multibyte_p = STRING_MULTIBYTE (it->string);
5169 it->current.overlay_string_index = -1;
5170 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
5171 it->end_charpos = it->string_nchars = SCHARS (it->string);
5172 it->method = GET_FROM_STRING;
5173 it->stop_charpos = 0;
5174 it->prev_stop = 0;
5175 it->base_level_stop = 0;
5176 it->string_from_display_prop_p = true;
5177 /* Say that we haven't consumed the characters with
5178 `display' property yet. The call to pop_it in
5179 set_iterator_to_next will clean this up. */
5180 if (BUFFERP (object))
5181 *position = start_pos;
5183 /* Force paragraph direction to be that of the parent
5184 object. If the parent object's paragraph direction is
5185 not yet determined, default to L2R. */
5186 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
5187 it->paragraph_embedding = it->bidi_it.paragraph_dir;
5188 else
5189 it->paragraph_embedding = L2R;
5191 /* Set up the bidi iterator for this display string. */
5192 if (it->bidi_p)
5194 it->bidi_it.string.lstring = it->string;
5195 it->bidi_it.string.s = NULL;
5196 it->bidi_it.string.schars = it->end_charpos;
5197 it->bidi_it.string.bufpos = bufpos;
5198 it->bidi_it.string.from_disp_str = 1;
5199 it->bidi_it.string.unibyte = !it->multibyte_p;
5200 it->bidi_it.w = it->w;
5201 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
5204 else if (CONSP (value) && EQ (XCAR (value), Qspace))
5206 it->method = GET_FROM_STRETCH;
5207 it->object = value;
5208 *position = it->position = start_pos;
5209 retval = 1 + (it->area == TEXT_AREA);
5211 #ifdef HAVE_WINDOW_SYSTEM
5212 else
5214 it->what = IT_IMAGE;
5215 it->image_id = lookup_image (it->f, value);
5216 it->position = start_pos;
5217 it->object = NILP (object) ? it->w->contents : object;
5218 it->method = GET_FROM_IMAGE;
5220 /* Say that we haven't consumed the characters with
5221 `display' property yet. The call to pop_it in
5222 set_iterator_to_next will clean this up. */
5223 *position = start_pos;
5225 #endif /* HAVE_WINDOW_SYSTEM */
5227 return retval;
5230 /* Invalid property or property not supported. Restore
5231 POSITION to what it was before. */
5232 *position = start_pos;
5233 return 0;
5236 /* Check if PROP is a display property value whose text should be
5237 treated as intangible. OVERLAY is the overlay from which PROP
5238 came, or nil if it came from a text property. CHARPOS and BYTEPOS
5239 specify the buffer position covered by PROP. */
5242 display_prop_intangible_p (Lisp_Object prop, Lisp_Object overlay,
5243 ptrdiff_t charpos, ptrdiff_t bytepos)
5245 int frame_window_p = FRAME_WINDOW_P (XFRAME (selected_frame));
5246 struct text_pos position;
5248 SET_TEXT_POS (position, charpos, bytepos);
5249 return handle_display_spec (NULL, prop, Qnil, overlay,
5250 &position, charpos, frame_window_p);
5254 /* Return 1 if PROP is a display sub-property value containing STRING.
5256 Implementation note: this and the following function are really
5257 special cases of handle_display_spec and
5258 handle_single_display_spec, and should ideally use the same code.
5259 Until they do, these two pairs must be consistent and must be
5260 modified in sync. */
5262 static int
5263 single_display_spec_string_p (Lisp_Object prop, Lisp_Object string)
5265 if (EQ (string, prop))
5266 return 1;
5268 /* Skip over `when FORM'. */
5269 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
5271 prop = XCDR (prop);
5272 if (!CONSP (prop))
5273 return 0;
5274 /* Actually, the condition following `when' should be eval'ed,
5275 like handle_single_display_spec does, and we should return
5276 zero if it evaluates to nil. However, this function is
5277 called only when the buffer was already displayed and some
5278 glyph in the glyph matrix was found to come from a display
5279 string. Therefore, the condition was already evaluated, and
5280 the result was non-nil, otherwise the display string wouldn't
5281 have been displayed and we would have never been called for
5282 this property. Thus, we can skip the evaluation and assume
5283 its result is non-nil. */
5284 prop = XCDR (prop);
5287 if (CONSP (prop))
5288 /* Skip over `margin LOCATION'. */
5289 if (EQ (XCAR (prop), Qmargin))
5291 prop = XCDR (prop);
5292 if (!CONSP (prop))
5293 return 0;
5295 prop = XCDR (prop);
5296 if (!CONSP (prop))
5297 return 0;
5300 return EQ (prop, string) || (CONSP (prop) && EQ (XCAR (prop), string));
5304 /* Return 1 if STRING appears in the `display' property PROP. */
5306 static int
5307 display_prop_string_p (Lisp_Object prop, Lisp_Object string)
5309 if (CONSP (prop)
5310 && !EQ (XCAR (prop), Qwhen)
5311 && !(CONSP (XCAR (prop)) && EQ (Qmargin, XCAR (XCAR (prop)))))
5313 /* A list of sub-properties. */
5314 while (CONSP (prop))
5316 if (single_display_spec_string_p (XCAR (prop), string))
5317 return 1;
5318 prop = XCDR (prop);
5321 else if (VECTORP (prop))
5323 /* A vector of sub-properties. */
5324 ptrdiff_t i;
5325 for (i = 0; i < ASIZE (prop); ++i)
5326 if (single_display_spec_string_p (AREF (prop, i), string))
5327 return 1;
5329 else
5330 return single_display_spec_string_p (prop, string);
5332 return 0;
5335 /* Look for STRING in overlays and text properties in the current
5336 buffer, between character positions FROM and TO (excluding TO).
5337 BACK_P non-zero means look back (in this case, TO is supposed to be
5338 less than FROM).
5339 Value is the first character position where STRING was found, or
5340 zero if it wasn't found before hitting TO.
5342 This function may only use code that doesn't eval because it is
5343 called asynchronously from note_mouse_highlight. */
5345 static ptrdiff_t
5346 string_buffer_position_lim (Lisp_Object string,
5347 ptrdiff_t from, ptrdiff_t to, int back_p)
5349 Lisp_Object limit, prop, pos;
5350 int found = 0;
5352 pos = make_number (max (from, BEGV));
5354 if (!back_p) /* looking forward */
5356 limit = make_number (min (to, ZV));
5357 while (!found && !EQ (pos, limit))
5359 prop = Fget_char_property (pos, Qdisplay, Qnil);
5360 if (!NILP (prop) && display_prop_string_p (prop, string))
5361 found = 1;
5362 else
5363 pos = Fnext_single_char_property_change (pos, Qdisplay, Qnil,
5364 limit);
5367 else /* looking back */
5369 limit = make_number (max (to, BEGV));
5370 while (!found && !EQ (pos, limit))
5372 prop = Fget_char_property (pos, Qdisplay, Qnil);
5373 if (!NILP (prop) && display_prop_string_p (prop, string))
5374 found = 1;
5375 else
5376 pos = Fprevious_single_char_property_change (pos, Qdisplay, Qnil,
5377 limit);
5381 return found ? XINT (pos) : 0;
5384 /* Determine which buffer position in current buffer STRING comes from.
5385 AROUND_CHARPOS is an approximate position where it could come from.
5386 Value is the buffer position or 0 if it couldn't be determined.
5388 This function is necessary because we don't record buffer positions
5389 in glyphs generated from strings (to keep struct glyph small).
5390 This function may only use code that doesn't eval because it is
5391 called asynchronously from note_mouse_highlight. */
5393 static ptrdiff_t
5394 string_buffer_position (Lisp_Object string, ptrdiff_t around_charpos)
5396 const int MAX_DISTANCE = 1000;
5397 ptrdiff_t found = string_buffer_position_lim (string, around_charpos,
5398 around_charpos + MAX_DISTANCE,
5401 if (!found)
5402 found = string_buffer_position_lim (string, around_charpos,
5403 around_charpos - MAX_DISTANCE, 1);
5404 return found;
5409 /***********************************************************************
5410 `composition' property
5411 ***********************************************************************/
5413 /* Set up iterator IT from `composition' property at its current
5414 position. Called from handle_stop. */
5416 static enum prop_handled
5417 handle_composition_prop (struct it *it)
5419 Lisp_Object prop, string;
5420 ptrdiff_t pos, pos_byte, start, end;
5422 if (STRINGP (it->string))
5424 unsigned char *s;
5426 pos = IT_STRING_CHARPOS (*it);
5427 pos_byte = IT_STRING_BYTEPOS (*it);
5428 string = it->string;
5429 s = SDATA (string) + pos_byte;
5430 it->c = STRING_CHAR (s);
5432 else
5434 pos = IT_CHARPOS (*it);
5435 pos_byte = IT_BYTEPOS (*it);
5436 string = Qnil;
5437 it->c = FETCH_CHAR (pos_byte);
5440 /* If there's a valid composition and point is not inside of the
5441 composition (in the case that the composition is from the current
5442 buffer), draw a glyph composed from the composition components. */
5443 if (find_composition (pos, -1, &start, &end, &prop, string)
5444 && composition_valid_p (start, end, prop)
5445 && (STRINGP (it->string) || (PT <= start || PT >= end)))
5447 if (start < pos)
5448 /* As we can't handle this situation (perhaps font-lock added
5449 a new composition), we just return here hoping that next
5450 redisplay will detect this composition much earlier. */
5451 return HANDLED_NORMALLY;
5452 if (start != pos)
5454 if (STRINGP (it->string))
5455 pos_byte = string_char_to_byte (it->string, start);
5456 else
5457 pos_byte = CHAR_TO_BYTE (start);
5459 it->cmp_it.id = get_composition_id (start, pos_byte, end - start,
5460 prop, string);
5462 if (it->cmp_it.id >= 0)
5464 it->cmp_it.ch = -1;
5465 it->cmp_it.nchars = COMPOSITION_LENGTH (prop);
5466 it->cmp_it.nglyphs = -1;
5470 return HANDLED_NORMALLY;
5475 /***********************************************************************
5476 Overlay strings
5477 ***********************************************************************/
5479 /* The following structure is used to record overlay strings for
5480 later sorting in load_overlay_strings. */
5482 struct overlay_entry
5484 Lisp_Object overlay;
5485 Lisp_Object string;
5486 EMACS_INT priority;
5487 int after_string_p;
5491 /* Set up iterator IT from overlay strings at its current position.
5492 Called from handle_stop. */
5494 static enum prop_handled
5495 handle_overlay_change (struct it *it)
5497 if (!STRINGP (it->string) && get_overlay_strings (it, 0))
5498 return HANDLED_RECOMPUTE_PROPS;
5499 else
5500 return HANDLED_NORMALLY;
5504 /* Set up the next overlay string for delivery by IT, if there is an
5505 overlay string to deliver. Called by set_iterator_to_next when the
5506 end of the current overlay string is reached. If there are more
5507 overlay strings to display, IT->string and
5508 IT->current.overlay_string_index are set appropriately here.
5509 Otherwise IT->string is set to nil. */
5511 static void
5512 next_overlay_string (struct it *it)
5514 ++it->current.overlay_string_index;
5515 if (it->current.overlay_string_index == it->n_overlay_strings)
5517 /* No more overlay strings. Restore IT's settings to what
5518 they were before overlay strings were processed, and
5519 continue to deliver from current_buffer. */
5521 it->ellipsis_p = (it->stack[it->sp - 1].display_ellipsis_p != 0);
5522 pop_it (it);
5523 eassert (it->sp > 0
5524 || (NILP (it->string)
5525 && it->method == GET_FROM_BUFFER
5526 && it->stop_charpos >= BEGV
5527 && it->stop_charpos <= it->end_charpos));
5528 it->current.overlay_string_index = -1;
5529 it->n_overlay_strings = 0;
5530 it->overlay_strings_charpos = -1;
5531 /* If there's an empty display string on the stack, pop the
5532 stack, to resync the bidi iterator with IT's position. Such
5533 empty strings are pushed onto the stack in
5534 get_overlay_strings_1. */
5535 if (it->sp > 0 && STRINGP (it->string) && !SCHARS (it->string))
5536 pop_it (it);
5538 /* If we're at the end of the buffer, record that we have
5539 processed the overlay strings there already, so that
5540 next_element_from_buffer doesn't try it again. */
5541 if (NILP (it->string) && IT_CHARPOS (*it) >= it->end_charpos)
5542 it->overlay_strings_at_end_processed_p = true;
5544 else
5546 /* There are more overlay strings to process. If
5547 IT->current.overlay_string_index has advanced to a position
5548 where we must load IT->overlay_strings with more strings, do
5549 it. We must load at the IT->overlay_strings_charpos where
5550 IT->n_overlay_strings was originally computed; when invisible
5551 text is present, this might not be IT_CHARPOS (Bug#7016). */
5552 int i = it->current.overlay_string_index % OVERLAY_STRING_CHUNK_SIZE;
5554 if (it->current.overlay_string_index && i == 0)
5555 load_overlay_strings (it, it->overlay_strings_charpos);
5557 /* Initialize IT to deliver display elements from the overlay
5558 string. */
5559 it->string = it->overlay_strings[i];
5560 it->multibyte_p = STRING_MULTIBYTE (it->string);
5561 SET_TEXT_POS (it->current.string_pos, 0, 0);
5562 it->method = GET_FROM_STRING;
5563 it->stop_charpos = 0;
5564 it->end_charpos = SCHARS (it->string);
5565 if (it->cmp_it.stop_pos >= 0)
5566 it->cmp_it.stop_pos = 0;
5567 it->prev_stop = 0;
5568 it->base_level_stop = 0;
5570 /* Set up the bidi iterator for this overlay string. */
5571 if (it->bidi_p)
5573 it->bidi_it.string.lstring = it->string;
5574 it->bidi_it.string.s = NULL;
5575 it->bidi_it.string.schars = SCHARS (it->string);
5576 it->bidi_it.string.bufpos = it->overlay_strings_charpos;
5577 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
5578 it->bidi_it.string.unibyte = !it->multibyte_p;
5579 it->bidi_it.w = it->w;
5580 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
5584 CHECK_IT (it);
5588 /* Compare two overlay_entry structures E1 and E2. Used as a
5589 comparison function for qsort in load_overlay_strings. Overlay
5590 strings for the same position are sorted so that
5592 1. All after-strings come in front of before-strings, except
5593 when they come from the same overlay.
5595 2. Within after-strings, strings are sorted so that overlay strings
5596 from overlays with higher priorities come first.
5598 2. Within before-strings, strings are sorted so that overlay
5599 strings from overlays with higher priorities come last.
5601 Value is analogous to strcmp. */
5604 static int
5605 compare_overlay_entries (const void *e1, const void *e2)
5607 struct overlay_entry const *entry1 = e1;
5608 struct overlay_entry const *entry2 = e2;
5609 int result;
5611 if (entry1->after_string_p != entry2->after_string_p)
5613 /* Let after-strings appear in front of before-strings if
5614 they come from different overlays. */
5615 if (EQ (entry1->overlay, entry2->overlay))
5616 result = entry1->after_string_p ? 1 : -1;
5617 else
5618 result = entry1->after_string_p ? -1 : 1;
5620 else if (entry1->priority != entry2->priority)
5622 if (entry1->after_string_p)
5623 /* After-strings sorted in order of decreasing priority. */
5624 result = entry2->priority < entry1->priority ? -1 : 1;
5625 else
5626 /* Before-strings sorted in order of increasing priority. */
5627 result = entry1->priority < entry2->priority ? -1 : 1;
5629 else
5630 result = 0;
5632 return result;
5636 /* Load the vector IT->overlay_strings with overlay strings from IT's
5637 current buffer position, or from CHARPOS if that is > 0. Set
5638 IT->n_overlays to the total number of overlay strings found.
5640 Overlay strings are processed OVERLAY_STRING_CHUNK_SIZE strings at
5641 a time. On entry into load_overlay_strings,
5642 IT->current.overlay_string_index gives the number of overlay
5643 strings that have already been loaded by previous calls to this
5644 function.
5646 IT->add_overlay_start contains an additional overlay start
5647 position to consider for taking overlay strings from, if non-zero.
5648 This position comes into play when the overlay has an `invisible'
5649 property, and both before and after-strings. When we've skipped to
5650 the end of the overlay, because of its `invisible' property, we
5651 nevertheless want its before-string to appear.
5652 IT->add_overlay_start will contain the overlay start position
5653 in this case.
5655 Overlay strings are sorted so that after-string strings come in
5656 front of before-string strings. Within before and after-strings,
5657 strings are sorted by overlay priority. See also function
5658 compare_overlay_entries. */
5660 static void
5661 load_overlay_strings (struct it *it, ptrdiff_t charpos)
5663 Lisp_Object overlay, window, str, invisible;
5664 struct Lisp_Overlay *ov;
5665 ptrdiff_t start, end;
5666 ptrdiff_t n = 0, i, j;
5667 int invis_p;
5668 struct overlay_entry entriesbuf[20];
5669 ptrdiff_t size = ARRAYELTS (entriesbuf);
5670 struct overlay_entry *entries = entriesbuf;
5671 USE_SAFE_ALLOCA;
5673 if (charpos <= 0)
5674 charpos = IT_CHARPOS (*it);
5676 /* Append the overlay string STRING of overlay OVERLAY to vector
5677 `entries' which has size `size' and currently contains `n'
5678 elements. AFTER_P non-zero means STRING is an after-string of
5679 OVERLAY. */
5680 #define RECORD_OVERLAY_STRING(OVERLAY, STRING, AFTER_P) \
5681 do \
5683 Lisp_Object priority; \
5685 if (n == size) \
5687 struct overlay_entry *old = entries; \
5688 SAFE_NALLOCA (entries, 2, size); \
5689 memcpy (entries, old, size * sizeof *entries); \
5690 size *= 2; \
5693 entries[n].string = (STRING); \
5694 entries[n].overlay = (OVERLAY); \
5695 priority = Foverlay_get ((OVERLAY), Qpriority); \
5696 entries[n].priority = INTEGERP (priority) ? XINT (priority) : 0; \
5697 entries[n].after_string_p = (AFTER_P); \
5698 ++n; \
5700 while (0)
5702 /* Process overlay before the overlay center. */
5703 for (ov = current_buffer->overlays_before; ov; ov = ov->next)
5705 XSETMISC (overlay, ov);
5706 eassert (OVERLAYP (overlay));
5707 start = OVERLAY_POSITION (OVERLAY_START (overlay));
5708 end = OVERLAY_POSITION (OVERLAY_END (overlay));
5710 if (end < charpos)
5711 break;
5713 /* Skip this overlay if it doesn't start or end at IT's current
5714 position. */
5715 if (end != charpos && start != charpos)
5716 continue;
5718 /* Skip this overlay if it doesn't apply to IT->w. */
5719 window = Foverlay_get (overlay, Qwindow);
5720 if (WINDOWP (window) && XWINDOW (window) != it->w)
5721 continue;
5723 /* If the text ``under'' the overlay is invisible, both before-
5724 and after-strings from this overlay are visible; start and
5725 end position are indistinguishable. */
5726 invisible = Foverlay_get (overlay, Qinvisible);
5727 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
5729 /* If overlay has a non-empty before-string, record it. */
5730 if ((start == charpos || (end == charpos && invis_p))
5731 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
5732 && SCHARS (str))
5733 RECORD_OVERLAY_STRING (overlay, str, 0);
5735 /* If overlay has a non-empty after-string, record it. */
5736 if ((end == charpos || (start == charpos && invis_p))
5737 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
5738 && SCHARS (str))
5739 RECORD_OVERLAY_STRING (overlay, str, 1);
5742 /* Process overlays after the overlay center. */
5743 for (ov = current_buffer->overlays_after; ov; ov = ov->next)
5745 XSETMISC (overlay, ov);
5746 eassert (OVERLAYP (overlay));
5747 start = OVERLAY_POSITION (OVERLAY_START (overlay));
5748 end = OVERLAY_POSITION (OVERLAY_END (overlay));
5750 if (start > charpos)
5751 break;
5753 /* Skip this overlay if it doesn't start or end at IT's current
5754 position. */
5755 if (end != charpos && start != charpos)
5756 continue;
5758 /* Skip this overlay if it doesn't apply to IT->w. */
5759 window = Foverlay_get (overlay, Qwindow);
5760 if (WINDOWP (window) && XWINDOW (window) != it->w)
5761 continue;
5763 /* If the text ``under'' the overlay is invisible, it has a zero
5764 dimension, and both before- and after-strings apply. */
5765 invisible = Foverlay_get (overlay, Qinvisible);
5766 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
5768 /* If overlay has a non-empty before-string, record it. */
5769 if ((start == charpos || (end == charpos && invis_p))
5770 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
5771 && SCHARS (str))
5772 RECORD_OVERLAY_STRING (overlay, str, 0);
5774 /* If overlay has a non-empty after-string, record it. */
5775 if ((end == charpos || (start == charpos && invis_p))
5776 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
5777 && SCHARS (str))
5778 RECORD_OVERLAY_STRING (overlay, str, 1);
5781 #undef RECORD_OVERLAY_STRING
5783 /* Sort entries. */
5784 if (n > 1)
5785 qsort (entries, n, sizeof *entries, compare_overlay_entries);
5787 /* Record number of overlay strings, and where we computed it. */
5788 it->n_overlay_strings = n;
5789 it->overlay_strings_charpos = charpos;
5791 /* IT->current.overlay_string_index is the number of overlay strings
5792 that have already been consumed by IT. Copy some of the
5793 remaining overlay strings to IT->overlay_strings. */
5794 i = 0;
5795 j = it->current.overlay_string_index;
5796 while (i < OVERLAY_STRING_CHUNK_SIZE && j < n)
5798 it->overlay_strings[i] = entries[j].string;
5799 it->string_overlays[i++] = entries[j++].overlay;
5802 CHECK_IT (it);
5803 SAFE_FREE ();
5807 /* Get the first chunk of overlay strings at IT's current buffer
5808 position, or at CHARPOS if that is > 0. Value is non-zero if at
5809 least one overlay string was found. */
5811 static int
5812 get_overlay_strings_1 (struct it *it, ptrdiff_t charpos, int compute_stop_p)
5814 /* Get the first OVERLAY_STRING_CHUNK_SIZE overlay strings to
5815 process. This fills IT->overlay_strings with strings, and sets
5816 IT->n_overlay_strings to the total number of strings to process.
5817 IT->pos.overlay_string_index has to be set temporarily to zero
5818 because load_overlay_strings needs this; it must be set to -1
5819 when no overlay strings are found because a zero value would
5820 indicate a position in the first overlay string. */
5821 it->current.overlay_string_index = 0;
5822 load_overlay_strings (it, charpos);
5824 /* If we found overlay strings, set up IT to deliver display
5825 elements from the first one. Otherwise set up IT to deliver
5826 from current_buffer. */
5827 if (it->n_overlay_strings)
5829 /* Make sure we know settings in current_buffer, so that we can
5830 restore meaningful values when we're done with the overlay
5831 strings. */
5832 if (compute_stop_p)
5833 compute_stop_pos (it);
5834 eassert (it->face_id >= 0);
5836 /* Save IT's settings. They are restored after all overlay
5837 strings have been processed. */
5838 eassert (!compute_stop_p || it->sp == 0);
5840 /* When called from handle_stop, there might be an empty display
5841 string loaded. In that case, don't bother saving it. But
5842 don't use this optimization with the bidi iterator, since we
5843 need the corresponding pop_it call to resync the bidi
5844 iterator's position with IT's position, after we are done
5845 with the overlay strings. (The corresponding call to pop_it
5846 in case of an empty display string is in
5847 next_overlay_string.) */
5848 if (!(!it->bidi_p
5849 && STRINGP (it->string) && !SCHARS (it->string)))
5850 push_it (it, NULL);
5852 /* Set up IT to deliver display elements from the first overlay
5853 string. */
5854 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
5855 it->string = it->overlay_strings[0];
5856 it->from_overlay = Qnil;
5857 it->stop_charpos = 0;
5858 eassert (STRINGP (it->string));
5859 it->end_charpos = SCHARS (it->string);
5860 it->prev_stop = 0;
5861 it->base_level_stop = 0;
5862 it->multibyte_p = STRING_MULTIBYTE (it->string);
5863 it->method = GET_FROM_STRING;
5864 it->from_disp_prop_p = 0;
5866 /* Force paragraph direction to be that of the parent
5867 buffer. */
5868 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
5869 it->paragraph_embedding = it->bidi_it.paragraph_dir;
5870 else
5871 it->paragraph_embedding = L2R;
5873 /* Set up the bidi iterator for this overlay string. */
5874 if (it->bidi_p)
5876 ptrdiff_t pos = (charpos > 0 ? charpos : IT_CHARPOS (*it));
5878 it->bidi_it.string.lstring = it->string;
5879 it->bidi_it.string.s = NULL;
5880 it->bidi_it.string.schars = SCHARS (it->string);
5881 it->bidi_it.string.bufpos = pos;
5882 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
5883 it->bidi_it.string.unibyte = !it->multibyte_p;
5884 it->bidi_it.w = it->w;
5885 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
5887 return 1;
5890 it->current.overlay_string_index = -1;
5891 return 0;
5894 static int
5895 get_overlay_strings (struct it *it, ptrdiff_t charpos)
5897 it->string = Qnil;
5898 it->method = GET_FROM_BUFFER;
5900 (void) get_overlay_strings_1 (it, charpos, 1);
5902 CHECK_IT (it);
5904 /* Value is non-zero if we found at least one overlay string. */
5905 return STRINGP (it->string);
5910 /***********************************************************************
5911 Saving and restoring state
5912 ***********************************************************************/
5914 /* Save current settings of IT on IT->stack. Called, for example,
5915 before setting up IT for an overlay string, to be able to restore
5916 IT's settings to what they were after the overlay string has been
5917 processed. If POSITION is non-NULL, it is the position to save on
5918 the stack instead of IT->position. */
5920 static void
5921 push_it (struct it *it, struct text_pos *position)
5923 struct iterator_stack_entry *p;
5925 eassert (it->sp < IT_STACK_SIZE);
5926 p = it->stack + it->sp;
5928 p->stop_charpos = it->stop_charpos;
5929 p->prev_stop = it->prev_stop;
5930 p->base_level_stop = it->base_level_stop;
5931 p->cmp_it = it->cmp_it;
5932 eassert (it->face_id >= 0);
5933 p->face_id = it->face_id;
5934 p->string = it->string;
5935 p->method = it->method;
5936 p->from_overlay = it->from_overlay;
5937 switch (p->method)
5939 case GET_FROM_IMAGE:
5940 p->u.image.object = it->object;
5941 p->u.image.image_id = it->image_id;
5942 p->u.image.slice = it->slice;
5943 break;
5944 case GET_FROM_STRETCH:
5945 p->u.stretch.object = it->object;
5946 break;
5948 p->position = position ? *position : it->position;
5949 p->current = it->current;
5950 p->end_charpos = it->end_charpos;
5951 p->string_nchars = it->string_nchars;
5952 p->area = it->area;
5953 p->multibyte_p = it->multibyte_p;
5954 p->avoid_cursor_p = it->avoid_cursor_p;
5955 p->space_width = it->space_width;
5956 p->font_height = it->font_height;
5957 p->voffset = it->voffset;
5958 p->string_from_display_prop_p = it->string_from_display_prop_p;
5959 p->string_from_prefix_prop_p = it->string_from_prefix_prop_p;
5960 p->display_ellipsis_p = 0;
5961 p->line_wrap = it->line_wrap;
5962 p->bidi_p = it->bidi_p;
5963 p->paragraph_embedding = it->paragraph_embedding;
5964 p->from_disp_prop_p = it->from_disp_prop_p;
5965 ++it->sp;
5967 /* Save the state of the bidi iterator as well. */
5968 if (it->bidi_p)
5969 bidi_push_it (&it->bidi_it);
5972 static void
5973 iterate_out_of_display_property (struct it *it)
5975 int buffer_p = !STRINGP (it->string);
5976 ptrdiff_t eob = (buffer_p ? ZV : it->end_charpos);
5977 ptrdiff_t bob = (buffer_p ? BEGV : 0);
5979 eassert (eob >= CHARPOS (it->position) && CHARPOS (it->position) >= bob);
5981 /* Maybe initialize paragraph direction. If we are at the beginning
5982 of a new paragraph, next_element_from_buffer may not have a
5983 chance to do that. */
5984 if (it->bidi_it.first_elt && it->bidi_it.charpos < eob)
5985 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
5986 /* prev_stop can be zero, so check against BEGV as well. */
5987 while (it->bidi_it.charpos >= bob
5988 && it->prev_stop <= it->bidi_it.charpos
5989 && it->bidi_it.charpos < CHARPOS (it->position)
5990 && it->bidi_it.charpos < eob)
5991 bidi_move_to_visually_next (&it->bidi_it);
5992 /* Record the stop_pos we just crossed, for when we cross it
5993 back, maybe. */
5994 if (it->bidi_it.charpos > CHARPOS (it->position))
5995 it->prev_stop = CHARPOS (it->position);
5996 /* If we ended up not where pop_it put us, resync IT's
5997 positional members with the bidi iterator. */
5998 if (it->bidi_it.charpos != CHARPOS (it->position))
5999 SET_TEXT_POS (it->position, it->bidi_it.charpos, it->bidi_it.bytepos);
6000 if (buffer_p)
6001 it->current.pos = it->position;
6002 else
6003 it->current.string_pos = it->position;
6006 /* Restore IT's settings from IT->stack. Called, for example, when no
6007 more overlay strings must be processed, and we return to delivering
6008 display elements from a buffer, or when the end of a string from a
6009 `display' property is reached and we return to delivering display
6010 elements from an overlay string, or from a buffer. */
6012 static void
6013 pop_it (struct it *it)
6015 struct iterator_stack_entry *p;
6016 int from_display_prop = it->from_disp_prop_p;
6018 eassert (it->sp > 0);
6019 --it->sp;
6020 p = it->stack + it->sp;
6021 it->stop_charpos = p->stop_charpos;
6022 it->prev_stop = p->prev_stop;
6023 it->base_level_stop = p->base_level_stop;
6024 it->cmp_it = p->cmp_it;
6025 it->face_id = p->face_id;
6026 it->current = p->current;
6027 it->position = p->position;
6028 it->string = p->string;
6029 it->from_overlay = p->from_overlay;
6030 if (NILP (it->string))
6031 SET_TEXT_POS (it->current.string_pos, -1, -1);
6032 it->method = p->method;
6033 switch (it->method)
6035 case GET_FROM_IMAGE:
6036 it->image_id = p->u.image.image_id;
6037 it->object = p->u.image.object;
6038 it->slice = p->u.image.slice;
6039 break;
6040 case GET_FROM_STRETCH:
6041 it->object = p->u.stretch.object;
6042 break;
6043 case GET_FROM_BUFFER:
6044 it->object = it->w->contents;
6045 break;
6046 case GET_FROM_STRING:
6048 struct face *face = FACE_FROM_ID (it->f, it->face_id);
6050 /* Restore the face_box_p flag, since it could have been
6051 overwritten by the face of the object that we just finished
6052 displaying. */
6053 if (face)
6054 it->face_box_p = face->box != FACE_NO_BOX;
6055 it->object = it->string;
6057 break;
6058 case GET_FROM_DISPLAY_VECTOR:
6059 if (it->s)
6060 it->method = GET_FROM_C_STRING;
6061 else if (STRINGP (it->string))
6062 it->method = GET_FROM_STRING;
6063 else
6065 it->method = GET_FROM_BUFFER;
6066 it->object = it->w->contents;
6069 it->end_charpos = p->end_charpos;
6070 it->string_nchars = p->string_nchars;
6071 it->area = p->area;
6072 it->multibyte_p = p->multibyte_p;
6073 it->avoid_cursor_p = p->avoid_cursor_p;
6074 it->space_width = p->space_width;
6075 it->font_height = p->font_height;
6076 it->voffset = p->voffset;
6077 it->string_from_display_prop_p = p->string_from_display_prop_p;
6078 it->string_from_prefix_prop_p = p->string_from_prefix_prop_p;
6079 it->line_wrap = p->line_wrap;
6080 it->bidi_p = p->bidi_p;
6081 it->paragraph_embedding = p->paragraph_embedding;
6082 it->from_disp_prop_p = p->from_disp_prop_p;
6083 if (it->bidi_p)
6085 bidi_pop_it (&it->bidi_it);
6086 /* Bidi-iterate until we get out of the portion of text, if any,
6087 covered by a `display' text property or by an overlay with
6088 `display' property. (We cannot just jump there, because the
6089 internal coherency of the bidi iterator state can not be
6090 preserved across such jumps.) We also must determine the
6091 paragraph base direction if the overlay we just processed is
6092 at the beginning of a new paragraph. */
6093 if (from_display_prop
6094 && (it->method == GET_FROM_BUFFER || it->method == GET_FROM_STRING))
6095 iterate_out_of_display_property (it);
6097 eassert ((BUFFERP (it->object)
6098 && IT_CHARPOS (*it) == it->bidi_it.charpos
6099 && IT_BYTEPOS (*it) == it->bidi_it.bytepos)
6100 || (STRINGP (it->object)
6101 && IT_STRING_CHARPOS (*it) == it->bidi_it.charpos
6102 && IT_STRING_BYTEPOS (*it) == it->bidi_it.bytepos)
6103 || (CONSP (it->object) && it->method == GET_FROM_STRETCH));
6109 /***********************************************************************
6110 Moving over lines
6111 ***********************************************************************/
6113 /* Set IT's current position to the previous line start. */
6115 static void
6116 back_to_previous_line_start (struct it *it)
6118 ptrdiff_t cp = IT_CHARPOS (*it), bp = IT_BYTEPOS (*it);
6120 DEC_BOTH (cp, bp);
6121 IT_CHARPOS (*it) = find_newline_no_quit (cp, bp, -1, &IT_BYTEPOS (*it));
6125 /* Move IT to the next line start.
6127 Value is non-zero if a newline was found. Set *SKIPPED_P to 1 if
6128 we skipped over part of the text (as opposed to moving the iterator
6129 continuously over the text). Otherwise, don't change the value
6130 of *SKIPPED_P.
6132 If BIDI_IT_PREV is non-NULL, store into it the state of the bidi
6133 iterator on the newline, if it was found.
6135 Newlines may come from buffer text, overlay strings, or strings
6136 displayed via the `display' property. That's the reason we can't
6137 simply use find_newline_no_quit.
6139 Note that this function may not skip over invisible text that is so
6140 because of text properties and immediately follows a newline. If
6141 it would, function reseat_at_next_visible_line_start, when called
6142 from set_iterator_to_next, would effectively make invisible
6143 characters following a newline part of the wrong glyph row, which
6144 leads to wrong cursor motion. */
6146 static int
6147 forward_to_next_line_start (struct it *it, int *skipped_p,
6148 struct bidi_it *bidi_it_prev)
6150 ptrdiff_t old_selective;
6151 int newline_found_p, n;
6152 const int MAX_NEWLINE_DISTANCE = 500;
6154 /* If already on a newline, just consume it to avoid unintended
6155 skipping over invisible text below. */
6156 if (it->what == IT_CHARACTER
6157 && it->c == '\n'
6158 && CHARPOS (it->position) == IT_CHARPOS (*it))
6160 if (it->bidi_p && bidi_it_prev)
6161 *bidi_it_prev = it->bidi_it;
6162 set_iterator_to_next (it, 0);
6163 it->c = 0;
6164 return 1;
6167 /* Don't handle selective display in the following. It's (a)
6168 unnecessary because it's done by the caller, and (b) leads to an
6169 infinite recursion because next_element_from_ellipsis indirectly
6170 calls this function. */
6171 old_selective = it->selective;
6172 it->selective = 0;
6174 /* Scan for a newline within MAX_NEWLINE_DISTANCE display elements
6175 from buffer text. */
6176 for (n = newline_found_p = 0;
6177 !newline_found_p && n < MAX_NEWLINE_DISTANCE;
6178 n += STRINGP (it->string) ? 0 : 1)
6180 if (!get_next_display_element (it))
6181 return 0;
6182 newline_found_p = it->what == IT_CHARACTER && it->c == '\n';
6183 if (newline_found_p && it->bidi_p && bidi_it_prev)
6184 *bidi_it_prev = it->bidi_it;
6185 set_iterator_to_next (it, 0);
6188 /* If we didn't find a newline near enough, see if we can use a
6189 short-cut. */
6190 if (!newline_found_p)
6192 ptrdiff_t bytepos, start = IT_CHARPOS (*it);
6193 ptrdiff_t limit = find_newline_no_quit (start, IT_BYTEPOS (*it),
6194 1, &bytepos);
6195 Lisp_Object pos;
6197 eassert (!STRINGP (it->string));
6199 /* If there isn't any `display' property in sight, and no
6200 overlays, we can just use the position of the newline in
6201 buffer text. */
6202 if (it->stop_charpos >= limit
6203 || ((pos = Fnext_single_property_change (make_number (start),
6204 Qdisplay, Qnil,
6205 make_number (limit)),
6206 NILP (pos))
6207 && next_overlay_change (start) == ZV))
6209 if (!it->bidi_p)
6211 IT_CHARPOS (*it) = limit;
6212 IT_BYTEPOS (*it) = bytepos;
6214 else
6216 struct bidi_it bprev;
6218 /* Help bidi.c avoid expensive searches for display
6219 properties and overlays, by telling it that there are
6220 none up to `limit'. */
6221 if (it->bidi_it.disp_pos < limit)
6223 it->bidi_it.disp_pos = limit;
6224 it->bidi_it.disp_prop = 0;
6226 do {
6227 bprev = it->bidi_it;
6228 bidi_move_to_visually_next (&it->bidi_it);
6229 } while (it->bidi_it.charpos != limit);
6230 IT_CHARPOS (*it) = limit;
6231 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6232 if (bidi_it_prev)
6233 *bidi_it_prev = bprev;
6235 *skipped_p = newline_found_p = true;
6237 else
6239 while (get_next_display_element (it)
6240 && !newline_found_p)
6242 newline_found_p = ITERATOR_AT_END_OF_LINE_P (it);
6243 if (newline_found_p && it->bidi_p && bidi_it_prev)
6244 *bidi_it_prev = it->bidi_it;
6245 set_iterator_to_next (it, 0);
6250 it->selective = old_selective;
6251 return newline_found_p;
6255 /* Set IT's current position to the previous visible line start. Skip
6256 invisible text that is so either due to text properties or due to
6257 selective display. Caution: this does not change IT->current_x and
6258 IT->hpos. */
6260 static void
6261 back_to_previous_visible_line_start (struct it *it)
6263 while (IT_CHARPOS (*it) > BEGV)
6265 back_to_previous_line_start (it);
6267 if (IT_CHARPOS (*it) <= BEGV)
6268 break;
6270 /* If selective > 0, then lines indented more than its value are
6271 invisible. */
6272 if (it->selective > 0
6273 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
6274 it->selective))
6275 continue;
6277 /* Check the newline before point for invisibility. */
6279 Lisp_Object prop;
6280 prop = Fget_char_property (make_number (IT_CHARPOS (*it) - 1),
6281 Qinvisible, it->window);
6282 if (TEXT_PROP_MEANS_INVISIBLE (prop))
6283 continue;
6286 if (IT_CHARPOS (*it) <= BEGV)
6287 break;
6290 struct it it2;
6291 void *it2data = NULL;
6292 ptrdiff_t pos;
6293 ptrdiff_t beg, end;
6294 Lisp_Object val, overlay;
6296 SAVE_IT (it2, *it, it2data);
6298 /* If newline is part of a composition, continue from start of composition */
6299 if (find_composition (IT_CHARPOS (*it), -1, &beg, &end, &val, Qnil)
6300 && beg < IT_CHARPOS (*it))
6301 goto replaced;
6303 /* If newline is replaced by a display property, find start of overlay
6304 or interval and continue search from that point. */
6305 pos = --IT_CHARPOS (it2);
6306 --IT_BYTEPOS (it2);
6307 it2.sp = 0;
6308 bidi_unshelve_cache (NULL, 0);
6309 it2.string_from_display_prop_p = 0;
6310 it2.from_disp_prop_p = 0;
6311 if (handle_display_prop (&it2) == HANDLED_RETURN
6312 && !NILP (val = get_char_property_and_overlay
6313 (make_number (pos), Qdisplay, Qnil, &overlay))
6314 && (OVERLAYP (overlay)
6315 ? (beg = OVERLAY_POSITION (OVERLAY_START (overlay)))
6316 : get_property_and_range (pos, Qdisplay, &val, &beg, &end, Qnil)))
6318 RESTORE_IT (it, it, it2data);
6319 goto replaced;
6322 /* Newline is not replaced by anything -- so we are done. */
6323 RESTORE_IT (it, it, it2data);
6324 break;
6326 replaced:
6327 if (beg < BEGV)
6328 beg = BEGV;
6329 IT_CHARPOS (*it) = beg;
6330 IT_BYTEPOS (*it) = buf_charpos_to_bytepos (current_buffer, beg);
6334 it->continuation_lines_width = 0;
6336 eassert (IT_CHARPOS (*it) >= BEGV);
6337 eassert (IT_CHARPOS (*it) == BEGV
6338 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
6339 CHECK_IT (it);
6343 /* Reseat iterator IT at the previous visible line start. Skip
6344 invisible text that is so either due to text properties or due to
6345 selective display. At the end, update IT's overlay information,
6346 face information etc. */
6348 void
6349 reseat_at_previous_visible_line_start (struct it *it)
6351 back_to_previous_visible_line_start (it);
6352 reseat (it, it->current.pos, 1);
6353 CHECK_IT (it);
6357 /* Reseat iterator IT on the next visible line start in the current
6358 buffer. ON_NEWLINE_P non-zero means position IT on the newline
6359 preceding the line start. Skip over invisible text that is so
6360 because of selective display. Compute faces, overlays etc at the
6361 new position. Note that this function does not skip over text that
6362 is invisible because of text properties. */
6364 static void
6365 reseat_at_next_visible_line_start (struct it *it, int on_newline_p)
6367 int newline_found_p, skipped_p = 0;
6368 struct bidi_it bidi_it_prev;
6370 newline_found_p = forward_to_next_line_start (it, &skipped_p, &bidi_it_prev);
6372 /* Skip over lines that are invisible because they are indented
6373 more than the value of IT->selective. */
6374 if (it->selective > 0)
6375 while (IT_CHARPOS (*it) < ZV
6376 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
6377 it->selective))
6379 eassert (IT_BYTEPOS (*it) == BEGV
6380 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
6381 newline_found_p =
6382 forward_to_next_line_start (it, &skipped_p, &bidi_it_prev);
6385 /* Position on the newline if that's what's requested. */
6386 if (on_newline_p && newline_found_p)
6388 if (STRINGP (it->string))
6390 if (IT_STRING_CHARPOS (*it) > 0)
6392 if (!it->bidi_p)
6394 --IT_STRING_CHARPOS (*it);
6395 --IT_STRING_BYTEPOS (*it);
6397 else
6399 /* We need to restore the bidi iterator to the state
6400 it had on the newline, and resync the IT's
6401 position with that. */
6402 it->bidi_it = bidi_it_prev;
6403 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
6404 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
6408 else if (IT_CHARPOS (*it) > BEGV)
6410 if (!it->bidi_p)
6412 --IT_CHARPOS (*it);
6413 --IT_BYTEPOS (*it);
6415 else
6417 /* We need to restore the bidi iterator to the state it
6418 had on the newline and resync IT with that. */
6419 it->bidi_it = bidi_it_prev;
6420 IT_CHARPOS (*it) = it->bidi_it.charpos;
6421 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6423 reseat (it, it->current.pos, 0);
6426 else if (skipped_p)
6427 reseat (it, it->current.pos, 0);
6429 CHECK_IT (it);
6434 /***********************************************************************
6435 Changing an iterator's position
6436 ***********************************************************************/
6438 /* Change IT's current position to POS in current_buffer. If FORCE_P
6439 is non-zero, always check for text properties at the new position.
6440 Otherwise, text properties are only looked up if POS >=
6441 IT->check_charpos of a property. */
6443 static void
6444 reseat (struct it *it, struct text_pos pos, int force_p)
6446 ptrdiff_t original_pos = IT_CHARPOS (*it);
6448 reseat_1 (it, pos, 0);
6450 /* Determine where to check text properties. Avoid doing it
6451 where possible because text property lookup is very expensive. */
6452 if (force_p
6453 || CHARPOS (pos) > it->stop_charpos
6454 || CHARPOS (pos) < original_pos)
6456 if (it->bidi_p)
6458 /* For bidi iteration, we need to prime prev_stop and
6459 base_level_stop with our best estimations. */
6460 /* Implementation note: Of course, POS is not necessarily a
6461 stop position, so assigning prev_pos to it is a lie; we
6462 should have called compute_stop_backwards. However, if
6463 the current buffer does not include any R2L characters,
6464 that call would be a waste of cycles, because the
6465 iterator will never move back, and thus never cross this
6466 "fake" stop position. So we delay that backward search
6467 until the time we really need it, in next_element_from_buffer. */
6468 if (CHARPOS (pos) != it->prev_stop)
6469 it->prev_stop = CHARPOS (pos);
6470 if (CHARPOS (pos) < it->base_level_stop)
6471 it->base_level_stop = 0; /* meaning it's unknown */
6472 handle_stop (it);
6474 else
6476 handle_stop (it);
6477 it->prev_stop = it->base_level_stop = 0;
6482 CHECK_IT (it);
6486 /* Change IT's buffer position to POS. SET_STOP_P non-zero means set
6487 IT->stop_pos to POS, also. */
6489 static void
6490 reseat_1 (struct it *it, struct text_pos pos, int set_stop_p)
6492 /* Don't call this function when scanning a C string. */
6493 eassert (it->s == NULL);
6495 /* POS must be a reasonable value. */
6496 eassert (CHARPOS (pos) >= BEGV && CHARPOS (pos) <= ZV);
6498 it->current.pos = it->position = pos;
6499 it->end_charpos = ZV;
6500 it->dpvec = NULL;
6501 it->current.dpvec_index = -1;
6502 it->current.overlay_string_index = -1;
6503 IT_STRING_CHARPOS (*it) = -1;
6504 IT_STRING_BYTEPOS (*it) = -1;
6505 it->string = Qnil;
6506 it->method = GET_FROM_BUFFER;
6507 it->object = it->w->contents;
6508 it->area = TEXT_AREA;
6509 it->multibyte_p = !NILP (BVAR (current_buffer, enable_multibyte_characters));
6510 it->sp = 0;
6511 it->string_from_display_prop_p = 0;
6512 it->string_from_prefix_prop_p = 0;
6514 it->from_disp_prop_p = 0;
6515 it->face_before_selective_p = 0;
6516 if (it->bidi_p)
6518 bidi_init_it (IT_CHARPOS (*it), IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
6519 &it->bidi_it);
6520 bidi_unshelve_cache (NULL, 0);
6521 it->bidi_it.paragraph_dir = NEUTRAL_DIR;
6522 it->bidi_it.string.s = NULL;
6523 it->bidi_it.string.lstring = Qnil;
6524 it->bidi_it.string.bufpos = 0;
6525 it->bidi_it.string.from_disp_str = 0;
6526 it->bidi_it.string.unibyte = 0;
6527 it->bidi_it.w = it->w;
6530 if (set_stop_p)
6532 it->stop_charpos = CHARPOS (pos);
6533 it->base_level_stop = CHARPOS (pos);
6535 /* This make the information stored in it->cmp_it invalidate. */
6536 it->cmp_it.id = -1;
6540 /* Set up IT for displaying a string, starting at CHARPOS in window W.
6541 If S is non-null, it is a C string to iterate over. Otherwise,
6542 STRING gives a Lisp string to iterate over.
6544 If PRECISION > 0, don't return more then PRECISION number of
6545 characters from the string.
6547 If FIELD_WIDTH > 0, return padding spaces until FIELD_WIDTH
6548 characters have been returned. FIELD_WIDTH < 0 means an infinite
6549 field width.
6551 MULTIBYTE = 0 means disable processing of multibyte characters,
6552 MULTIBYTE > 0 means enable it,
6553 MULTIBYTE < 0 means use IT->multibyte_p.
6555 IT must be initialized via a prior call to init_iterator before
6556 calling this function. */
6558 static void
6559 reseat_to_string (struct it *it, const char *s, Lisp_Object string,
6560 ptrdiff_t charpos, ptrdiff_t precision, int field_width,
6561 int multibyte)
6563 /* No text property checks performed by default, but see below. */
6564 it->stop_charpos = -1;
6566 /* Set iterator position and end position. */
6567 memset (&it->current, 0, sizeof it->current);
6568 it->current.overlay_string_index = -1;
6569 it->current.dpvec_index = -1;
6570 eassert (charpos >= 0);
6572 /* If STRING is specified, use its multibyteness, otherwise use the
6573 setting of MULTIBYTE, if specified. */
6574 if (multibyte >= 0)
6575 it->multibyte_p = multibyte > 0;
6577 /* Bidirectional reordering of strings is controlled by the default
6578 value of bidi-display-reordering. Don't try to reorder while
6579 loading loadup.el, as the necessary character property tables are
6580 not yet available. */
6581 it->bidi_p =
6582 NILP (Vpurify_flag)
6583 && !NILP (BVAR (&buffer_defaults, bidi_display_reordering));
6585 if (s == NULL)
6587 eassert (STRINGP (string));
6588 it->string = string;
6589 it->s = NULL;
6590 it->end_charpos = it->string_nchars = SCHARS (string);
6591 it->method = GET_FROM_STRING;
6592 it->current.string_pos = string_pos (charpos, string);
6594 if (it->bidi_p)
6596 it->bidi_it.string.lstring = string;
6597 it->bidi_it.string.s = NULL;
6598 it->bidi_it.string.schars = it->end_charpos;
6599 it->bidi_it.string.bufpos = 0;
6600 it->bidi_it.string.from_disp_str = 0;
6601 it->bidi_it.string.unibyte = !it->multibyte_p;
6602 it->bidi_it.w = it->w;
6603 bidi_init_it (charpos, IT_STRING_BYTEPOS (*it),
6604 FRAME_WINDOW_P (it->f), &it->bidi_it);
6607 else
6609 it->s = (const unsigned char *) s;
6610 it->string = Qnil;
6612 /* Note that we use IT->current.pos, not it->current.string_pos,
6613 for displaying C strings. */
6614 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
6615 if (it->multibyte_p)
6617 it->current.pos = c_string_pos (charpos, s, 1);
6618 it->end_charpos = it->string_nchars = number_of_chars (s, 1);
6620 else
6622 IT_CHARPOS (*it) = IT_BYTEPOS (*it) = charpos;
6623 it->end_charpos = it->string_nchars = strlen (s);
6626 if (it->bidi_p)
6628 it->bidi_it.string.lstring = Qnil;
6629 it->bidi_it.string.s = (const unsigned char *) s;
6630 it->bidi_it.string.schars = it->end_charpos;
6631 it->bidi_it.string.bufpos = 0;
6632 it->bidi_it.string.from_disp_str = 0;
6633 it->bidi_it.string.unibyte = !it->multibyte_p;
6634 it->bidi_it.w = it->w;
6635 bidi_init_it (charpos, IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
6636 &it->bidi_it);
6638 it->method = GET_FROM_C_STRING;
6641 /* PRECISION > 0 means don't return more than PRECISION characters
6642 from the string. */
6643 if (precision > 0 && it->end_charpos - charpos > precision)
6645 it->end_charpos = it->string_nchars = charpos + precision;
6646 if (it->bidi_p)
6647 it->bidi_it.string.schars = it->end_charpos;
6650 /* FIELD_WIDTH > 0 means pad with spaces until FIELD_WIDTH
6651 characters have been returned. FIELD_WIDTH == 0 means don't pad,
6652 FIELD_WIDTH < 0 means infinite field width. This is useful for
6653 padding with `-' at the end of a mode line. */
6654 if (field_width < 0)
6655 field_width = INFINITY;
6656 /* Implementation note: We deliberately don't enlarge
6657 it->bidi_it.string.schars here to fit it->end_charpos, because
6658 the bidi iterator cannot produce characters out of thin air. */
6659 if (field_width > it->end_charpos - charpos)
6660 it->end_charpos = charpos + field_width;
6662 /* Use the standard display table for displaying strings. */
6663 if (DISP_TABLE_P (Vstandard_display_table))
6664 it->dp = XCHAR_TABLE (Vstandard_display_table);
6666 it->stop_charpos = charpos;
6667 it->prev_stop = charpos;
6668 it->base_level_stop = 0;
6669 if (it->bidi_p)
6671 it->bidi_it.first_elt = 1;
6672 it->bidi_it.paragraph_dir = NEUTRAL_DIR;
6673 it->bidi_it.disp_pos = -1;
6675 if (s == NULL && it->multibyte_p)
6677 ptrdiff_t endpos = SCHARS (it->string);
6678 if (endpos > it->end_charpos)
6679 endpos = it->end_charpos;
6680 composition_compute_stop_pos (&it->cmp_it, charpos, -1, endpos,
6681 it->string);
6683 CHECK_IT (it);
6688 /***********************************************************************
6689 Iteration
6690 ***********************************************************************/
6692 /* Map enum it_method value to corresponding next_element_from_* function. */
6694 static int (* get_next_element[NUM_IT_METHODS]) (struct it *it) =
6696 next_element_from_buffer,
6697 next_element_from_display_vector,
6698 next_element_from_string,
6699 next_element_from_c_string,
6700 next_element_from_image,
6701 next_element_from_stretch
6704 #define GET_NEXT_DISPLAY_ELEMENT(it) (*get_next_element[(it)->method]) (it)
6707 /* Return 1 iff a character at CHARPOS (and BYTEPOS) is composed
6708 (possibly with the following characters). */
6710 #define CHAR_COMPOSED_P(IT,CHARPOS,BYTEPOS,END_CHARPOS) \
6711 ((IT)->cmp_it.id >= 0 \
6712 || ((IT)->cmp_it.stop_pos == (CHARPOS) \
6713 && composition_reseat_it (&(IT)->cmp_it, CHARPOS, BYTEPOS, \
6714 END_CHARPOS, (IT)->w, \
6715 FACE_FROM_ID ((IT)->f, (IT)->face_id), \
6716 (IT)->string)))
6719 /* Lookup the char-table Vglyphless_char_display for character C (-1
6720 if we want information for no-font case), and return the display
6721 method symbol. By side-effect, update it->what and
6722 it->glyphless_method. This function is called from
6723 get_next_display_element for each character element, and from
6724 x_produce_glyphs when no suitable font was found. */
6726 Lisp_Object
6727 lookup_glyphless_char_display (int c, struct it *it)
6729 Lisp_Object glyphless_method = Qnil;
6731 if (CHAR_TABLE_P (Vglyphless_char_display)
6732 && CHAR_TABLE_EXTRA_SLOTS (XCHAR_TABLE (Vglyphless_char_display)) >= 1)
6734 if (c >= 0)
6736 glyphless_method = CHAR_TABLE_REF (Vglyphless_char_display, c);
6737 if (CONSP (glyphless_method))
6738 glyphless_method = FRAME_WINDOW_P (it->f)
6739 ? XCAR (glyphless_method)
6740 : XCDR (glyphless_method);
6742 else
6743 glyphless_method = XCHAR_TABLE (Vglyphless_char_display)->extras[0];
6746 retry:
6747 if (NILP (glyphless_method))
6749 if (c >= 0)
6750 /* The default is to display the character by a proper font. */
6751 return Qnil;
6752 /* The default for the no-font case is to display an empty box. */
6753 glyphless_method = Qempty_box;
6755 if (EQ (glyphless_method, Qzero_width))
6757 if (c >= 0)
6758 return glyphless_method;
6759 /* This method can't be used for the no-font case. */
6760 glyphless_method = Qempty_box;
6762 if (EQ (glyphless_method, Qthin_space))
6763 it->glyphless_method = GLYPHLESS_DISPLAY_THIN_SPACE;
6764 else if (EQ (glyphless_method, Qempty_box))
6765 it->glyphless_method = GLYPHLESS_DISPLAY_EMPTY_BOX;
6766 else if (EQ (glyphless_method, Qhex_code))
6767 it->glyphless_method = GLYPHLESS_DISPLAY_HEX_CODE;
6768 else if (STRINGP (glyphless_method))
6769 it->glyphless_method = GLYPHLESS_DISPLAY_ACRONYM;
6770 else
6772 /* Invalid value. We use the default method. */
6773 glyphless_method = Qnil;
6774 goto retry;
6776 it->what = IT_GLYPHLESS;
6777 return glyphless_method;
6780 /* Merge escape glyph face and cache the result. */
6782 static struct frame *last_escape_glyph_frame = NULL;
6783 static int last_escape_glyph_face_id = (1 << FACE_ID_BITS);
6784 static int last_escape_glyph_merged_face_id = 0;
6786 static int
6787 merge_escape_glyph_face (struct it *it)
6789 int face_id;
6791 if (it->f == last_escape_glyph_frame
6792 && it->face_id == last_escape_glyph_face_id)
6793 face_id = last_escape_glyph_merged_face_id;
6794 else
6796 /* Merge the `escape-glyph' face into the current face. */
6797 face_id = merge_faces (it->f, Qescape_glyph, 0, it->face_id);
6798 last_escape_glyph_frame = it->f;
6799 last_escape_glyph_face_id = it->face_id;
6800 last_escape_glyph_merged_face_id = face_id;
6802 return face_id;
6805 /* Likewise for glyphless glyph face. */
6807 static struct frame *last_glyphless_glyph_frame = NULL;
6808 static int last_glyphless_glyph_face_id = (1 << FACE_ID_BITS);
6809 static int last_glyphless_glyph_merged_face_id = 0;
6812 merge_glyphless_glyph_face (struct it *it)
6814 int face_id;
6816 if (it->f == last_glyphless_glyph_frame
6817 && it->face_id == last_glyphless_glyph_face_id)
6818 face_id = last_glyphless_glyph_merged_face_id;
6819 else
6821 /* Merge the `glyphless-char' face into the current face. */
6822 face_id = merge_faces (it->f, Qglyphless_char, 0, it->face_id);
6823 last_glyphless_glyph_frame = it->f;
6824 last_glyphless_glyph_face_id = it->face_id;
6825 last_glyphless_glyph_merged_face_id = face_id;
6827 return face_id;
6830 /* Load IT's display element fields with information about the next
6831 display element from the current position of IT. Value is zero if
6832 end of buffer (or C string) is reached. */
6834 static int
6835 get_next_display_element (struct it *it)
6837 /* Non-zero means that we found a display element. Zero means that
6838 we hit the end of what we iterate over. Performance note: the
6839 function pointer `method' used here turns out to be faster than
6840 using a sequence of if-statements. */
6841 int success_p;
6843 get_next:
6844 success_p = GET_NEXT_DISPLAY_ELEMENT (it);
6846 if (it->what == IT_CHARACTER)
6848 /* UAX#9, L4: "A character is depicted by a mirrored glyph if
6849 and only if (a) the resolved directionality of that character
6850 is R..." */
6851 /* FIXME: Do we need an exception for characters from display
6852 tables? */
6853 if (it->bidi_p && it->bidi_it.type == STRONG_R
6854 && !inhibit_bidi_mirroring)
6855 it->c = bidi_mirror_char (it->c);
6856 /* Map via display table or translate control characters.
6857 IT->c, IT->len etc. have been set to the next character by
6858 the function call above. If we have a display table, and it
6859 contains an entry for IT->c, translate it. Don't do this if
6860 IT->c itself comes from a display table, otherwise we could
6861 end up in an infinite recursion. (An alternative could be to
6862 count the recursion depth of this function and signal an
6863 error when a certain maximum depth is reached.) Is it worth
6864 it? */
6865 if (success_p && it->dpvec == NULL)
6867 Lisp_Object dv;
6868 struct charset *unibyte = CHARSET_FROM_ID (charset_unibyte);
6869 int nonascii_space_p = 0;
6870 int nonascii_hyphen_p = 0;
6871 int c = it->c; /* This is the character to display. */
6873 if (! it->multibyte_p && ! ASCII_CHAR_P (c))
6875 eassert (SINGLE_BYTE_CHAR_P (c));
6876 if (unibyte_display_via_language_environment)
6878 c = DECODE_CHAR (unibyte, c);
6879 if (c < 0)
6880 c = BYTE8_TO_CHAR (it->c);
6882 else
6883 c = BYTE8_TO_CHAR (it->c);
6886 if (it->dp
6887 && (dv = DISP_CHAR_VECTOR (it->dp, c),
6888 VECTORP (dv)))
6890 struct Lisp_Vector *v = XVECTOR (dv);
6892 /* Return the first character from the display table
6893 entry, if not empty. If empty, don't display the
6894 current character. */
6895 if (v->header.size)
6897 it->dpvec_char_len = it->len;
6898 it->dpvec = v->contents;
6899 it->dpend = v->contents + v->header.size;
6900 it->current.dpvec_index = 0;
6901 it->dpvec_face_id = -1;
6902 it->saved_face_id = it->face_id;
6903 it->method = GET_FROM_DISPLAY_VECTOR;
6904 it->ellipsis_p = 0;
6906 else
6908 set_iterator_to_next (it, 0);
6910 goto get_next;
6913 if (! NILP (lookup_glyphless_char_display (c, it)))
6915 if (it->what == IT_GLYPHLESS)
6916 goto done;
6917 /* Don't display this character. */
6918 set_iterator_to_next (it, 0);
6919 goto get_next;
6922 /* If `nobreak-char-display' is non-nil, we display
6923 non-ASCII spaces and hyphens specially. */
6924 if (! ASCII_CHAR_P (c) && ! NILP (Vnobreak_char_display))
6926 if (c == 0xA0)
6927 nonascii_space_p = true;
6928 else if (c == 0xAD || c == 0x2010 || c == 0x2011)
6929 nonascii_hyphen_p = true;
6932 /* Translate control characters into `\003' or `^C' form.
6933 Control characters coming from a display table entry are
6934 currently not translated because we use IT->dpvec to hold
6935 the translation. This could easily be changed but I
6936 don't believe that it is worth doing.
6938 The characters handled by `nobreak-char-display' must be
6939 translated too.
6941 Non-printable characters and raw-byte characters are also
6942 translated to octal form. */
6943 if (((c < ' ' || c == 127) /* ASCII control chars. */
6944 ? (it->area != TEXT_AREA
6945 /* In mode line, treat \n, \t like other crl chars. */
6946 || (c != '\t'
6947 && it->glyph_row
6948 && (it->glyph_row->mode_line_p || it->avoid_cursor_p))
6949 || (c != '\n' && c != '\t'))
6950 : (nonascii_space_p
6951 || nonascii_hyphen_p
6952 || CHAR_BYTE8_P (c)
6953 || ! CHAR_PRINTABLE_P (c))))
6955 /* C is a control character, non-ASCII space/hyphen,
6956 raw-byte, or a non-printable character which must be
6957 displayed either as '\003' or as `^C' where the '\\'
6958 and '^' can be defined in the display table. Fill
6959 IT->ctl_chars with glyphs for what we have to
6960 display. Then, set IT->dpvec to these glyphs. */
6961 Lisp_Object gc;
6962 int ctl_len;
6963 int face_id;
6964 int lface_id = 0;
6965 int escape_glyph;
6967 /* Handle control characters with ^. */
6969 if (ASCII_CHAR_P (c) && it->ctl_arrow_p)
6971 int g;
6973 g = '^'; /* default glyph for Control */
6974 /* Set IT->ctl_chars[0] to the glyph for `^'. */
6975 if (it->dp
6976 && (gc = DISP_CTRL_GLYPH (it->dp), GLYPH_CODE_P (gc)))
6978 g = GLYPH_CODE_CHAR (gc);
6979 lface_id = GLYPH_CODE_FACE (gc);
6982 face_id = (lface_id
6983 ? merge_faces (it->f, Qt, lface_id, it->face_id)
6984 : merge_escape_glyph_face (it));
6986 XSETINT (it->ctl_chars[0], g);
6987 XSETINT (it->ctl_chars[1], c ^ 0100);
6988 ctl_len = 2;
6989 goto display_control;
6992 /* Handle non-ascii space in the mode where it only gets
6993 highlighting. */
6995 if (nonascii_space_p && EQ (Vnobreak_char_display, Qt))
6997 /* Merge `nobreak-space' into the current face. */
6998 face_id = merge_faces (it->f, Qnobreak_space, 0,
6999 it->face_id);
7000 XSETINT (it->ctl_chars[0], ' ');
7001 ctl_len = 1;
7002 goto display_control;
7005 /* Handle sequences that start with the "escape glyph". */
7007 /* the default escape glyph is \. */
7008 escape_glyph = '\\';
7010 if (it->dp
7011 && (gc = DISP_ESCAPE_GLYPH (it->dp), GLYPH_CODE_P (gc)))
7013 escape_glyph = GLYPH_CODE_CHAR (gc);
7014 lface_id = GLYPH_CODE_FACE (gc);
7017 face_id = (lface_id
7018 ? merge_faces (it->f, Qt, lface_id, it->face_id)
7019 : merge_escape_glyph_face (it));
7021 /* Draw non-ASCII hyphen with just highlighting: */
7023 if (nonascii_hyphen_p && EQ (Vnobreak_char_display, Qt))
7025 XSETINT (it->ctl_chars[0], '-');
7026 ctl_len = 1;
7027 goto display_control;
7030 /* Draw non-ASCII space/hyphen with escape glyph: */
7032 if (nonascii_space_p || nonascii_hyphen_p)
7034 XSETINT (it->ctl_chars[0], escape_glyph);
7035 XSETINT (it->ctl_chars[1], nonascii_space_p ? ' ' : '-');
7036 ctl_len = 2;
7037 goto display_control;
7041 char str[10];
7042 int len, i;
7044 if (CHAR_BYTE8_P (c))
7045 /* Display \200 instead of \17777600. */
7046 c = CHAR_TO_BYTE8 (c);
7047 len = sprintf (str, "%03o", c);
7049 XSETINT (it->ctl_chars[0], escape_glyph);
7050 for (i = 0; i < len; i++)
7051 XSETINT (it->ctl_chars[i + 1], str[i]);
7052 ctl_len = len + 1;
7055 display_control:
7056 /* Set up IT->dpvec and return first character from it. */
7057 it->dpvec_char_len = it->len;
7058 it->dpvec = it->ctl_chars;
7059 it->dpend = it->dpvec + ctl_len;
7060 it->current.dpvec_index = 0;
7061 it->dpvec_face_id = face_id;
7062 it->saved_face_id = it->face_id;
7063 it->method = GET_FROM_DISPLAY_VECTOR;
7064 it->ellipsis_p = 0;
7065 goto get_next;
7067 it->char_to_display = c;
7069 else if (success_p)
7071 it->char_to_display = it->c;
7075 #ifdef HAVE_WINDOW_SYSTEM
7076 /* Adjust face id for a multibyte character. There are no multibyte
7077 character in unibyte text. */
7078 if ((it->what == IT_CHARACTER || it->what == IT_COMPOSITION)
7079 && it->multibyte_p
7080 && success_p
7081 && FRAME_WINDOW_P (it->f))
7083 struct face *face = FACE_FROM_ID (it->f, it->face_id);
7085 if (it->what == IT_COMPOSITION && it->cmp_it.ch >= 0)
7087 /* Automatic composition with glyph-string. */
7088 Lisp_Object gstring = composition_gstring_from_id (it->cmp_it.id);
7090 it->face_id = face_for_font (it->f, LGSTRING_FONT (gstring), face);
7092 else
7094 ptrdiff_t pos = (it->s ? -1
7095 : STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
7096 : IT_CHARPOS (*it));
7097 int c;
7099 if (it->what == IT_CHARACTER)
7100 c = it->char_to_display;
7101 else
7103 struct composition *cmp = composition_table[it->cmp_it.id];
7104 int i;
7106 c = ' ';
7107 for (i = 0; i < cmp->glyph_len; i++)
7108 /* TAB in a composition means display glyphs with
7109 padding space on the left or right. */
7110 if ((c = COMPOSITION_GLYPH (cmp, i)) != '\t')
7111 break;
7113 it->face_id = FACE_FOR_CHAR (it->f, face, c, pos, it->string);
7116 #endif /* HAVE_WINDOW_SYSTEM */
7118 done:
7119 /* Is this character the last one of a run of characters with
7120 box? If yes, set IT->end_of_box_run_p to 1. */
7121 if (it->face_box_p
7122 && it->s == NULL)
7124 if (it->method == GET_FROM_STRING && it->sp)
7126 int face_id = underlying_face_id (it);
7127 struct face *face = FACE_FROM_ID (it->f, face_id);
7129 if (face)
7131 if (face->box == FACE_NO_BOX)
7133 /* If the box comes from face properties in a
7134 display string, check faces in that string. */
7135 int string_face_id = face_after_it_pos (it);
7136 it->end_of_box_run_p
7137 = (FACE_FROM_ID (it->f, string_face_id)->box
7138 == FACE_NO_BOX);
7140 /* Otherwise, the box comes from the underlying face.
7141 If this is the last string character displayed, check
7142 the next buffer location. */
7143 else if ((IT_STRING_CHARPOS (*it) >= SCHARS (it->string) - 1)
7144 /* n_overlay_strings is unreliable unless
7145 overlay_string_index is non-negative. */
7146 && ((it->current.overlay_string_index >= 0
7147 && (it->current.overlay_string_index
7148 == it->n_overlay_strings - 1))
7149 /* A string from display property. */
7150 || it->from_disp_prop_p))
7152 ptrdiff_t ignore;
7153 int next_face_id;
7154 struct text_pos pos = it->current.pos;
7156 /* For a string from a display property, the next
7157 buffer position is stored in the 'position'
7158 member of the iteration stack slot below the
7159 current one, see handle_single_display_spec. By
7160 contrast, it->current.pos was is not yet updated
7161 to point to that buffer position; that will
7162 happen in pop_it, after we finish displaying the
7163 current string. Note that we already checked
7164 above that it->sp is positive, so subtracting one
7165 from it is safe. */
7166 if (it->from_disp_prop_p)
7167 pos = (it->stack + it->sp - 1)->position;
7168 else
7169 INC_TEXT_POS (pos, it->multibyte_p);
7171 if (CHARPOS (pos) >= ZV)
7172 it->end_of_box_run_p = true;
7173 else
7175 next_face_id = face_at_buffer_position
7176 (it->w, CHARPOS (pos), &ignore,
7177 CHARPOS (pos) + TEXT_PROP_DISTANCE_LIMIT, 0, -1);
7178 it->end_of_box_run_p
7179 = (FACE_FROM_ID (it->f, next_face_id)->box
7180 == FACE_NO_BOX);
7185 /* next_element_from_display_vector sets this flag according to
7186 faces of the display vector glyphs, see there. */
7187 else if (it->method != GET_FROM_DISPLAY_VECTOR)
7189 int face_id = face_after_it_pos (it);
7190 it->end_of_box_run_p
7191 = (face_id != it->face_id
7192 && FACE_FROM_ID (it->f, face_id)->box == FACE_NO_BOX);
7195 /* If we reached the end of the object we've been iterating (e.g., a
7196 display string or an overlay string), and there's something on
7197 IT->stack, proceed with what's on the stack. It doesn't make
7198 sense to return zero if there's unprocessed stuff on the stack,
7199 because otherwise that stuff will never be displayed. */
7200 if (!success_p && it->sp > 0)
7202 set_iterator_to_next (it, 0);
7203 success_p = get_next_display_element (it);
7206 /* Value is 0 if end of buffer or string reached. */
7207 return success_p;
7211 /* Move IT to the next display element.
7213 RESEAT_P non-zero means if called on a newline in buffer text,
7214 skip to the next visible line start.
7216 Functions get_next_display_element and set_iterator_to_next are
7217 separate because I find this arrangement easier to handle than a
7218 get_next_display_element function that also increments IT's
7219 position. The way it is we can first look at an iterator's current
7220 display element, decide whether it fits on a line, and if it does,
7221 increment the iterator position. The other way around we probably
7222 would either need a flag indicating whether the iterator has to be
7223 incremented the next time, or we would have to implement a
7224 decrement position function which would not be easy to write. */
7226 void
7227 set_iterator_to_next (struct it *it, int reseat_p)
7229 /* Reset flags indicating start and end of a sequence of characters
7230 with box. Reset them at the start of this function because
7231 moving the iterator to a new position might set them. */
7232 it->start_of_box_run_p = it->end_of_box_run_p = 0;
7234 switch (it->method)
7236 case GET_FROM_BUFFER:
7237 /* The current display element of IT is a character from
7238 current_buffer. Advance in the buffer, and maybe skip over
7239 invisible lines that are so because of selective display. */
7240 if (ITERATOR_AT_END_OF_LINE_P (it) && reseat_p)
7241 reseat_at_next_visible_line_start (it, 0);
7242 else if (it->cmp_it.id >= 0)
7244 /* We are currently getting glyphs from a composition. */
7245 if (! it->bidi_p)
7247 IT_CHARPOS (*it) += it->cmp_it.nchars;
7248 IT_BYTEPOS (*it) += it->cmp_it.nbytes;
7250 else
7252 int i;
7254 /* Update IT's char/byte positions to point to the first
7255 character of the next grapheme cluster, or to the
7256 character visually after the current composition. */
7257 for (i = 0; i < it->cmp_it.nchars; i++)
7258 bidi_move_to_visually_next (&it->bidi_it);
7259 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7260 IT_CHARPOS (*it) = it->bidi_it.charpos;
7263 if ((! it->bidi_p || ! it->cmp_it.reversed_p)
7264 && it->cmp_it.to < it->cmp_it.nglyphs)
7266 /* Composition created while scanning forward. Proceed
7267 to the next grapheme cluster. */
7268 it->cmp_it.from = it->cmp_it.to;
7270 else if ((it->bidi_p && it->cmp_it.reversed_p)
7271 && it->cmp_it.from > 0)
7273 /* Composition created while scanning backward. Proceed
7274 to the previous grapheme cluster. */
7275 it->cmp_it.to = it->cmp_it.from;
7277 else
7279 /* No more grapheme clusters in this composition.
7280 Find the next stop position. */
7281 ptrdiff_t stop = it->end_charpos;
7283 if (it->bidi_it.scan_dir < 0)
7284 /* Now we are scanning backward and don't know
7285 where to stop. */
7286 stop = -1;
7287 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
7288 IT_BYTEPOS (*it), stop, Qnil);
7291 else
7293 eassert (it->len != 0);
7295 if (!it->bidi_p)
7297 IT_BYTEPOS (*it) += it->len;
7298 IT_CHARPOS (*it) += 1;
7300 else
7302 int prev_scan_dir = it->bidi_it.scan_dir;
7303 /* If this is a new paragraph, determine its base
7304 direction (a.k.a. its base embedding level). */
7305 if (it->bidi_it.new_paragraph)
7306 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 0);
7307 bidi_move_to_visually_next (&it->bidi_it);
7308 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7309 IT_CHARPOS (*it) = it->bidi_it.charpos;
7310 if (prev_scan_dir != it->bidi_it.scan_dir)
7312 /* As the scan direction was changed, we must
7313 re-compute the stop position for composition. */
7314 ptrdiff_t stop = it->end_charpos;
7315 if (it->bidi_it.scan_dir < 0)
7316 stop = -1;
7317 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
7318 IT_BYTEPOS (*it), stop, Qnil);
7321 eassert (IT_BYTEPOS (*it) == CHAR_TO_BYTE (IT_CHARPOS (*it)));
7323 break;
7325 case GET_FROM_C_STRING:
7326 /* Current display element of IT is from a C string. */
7327 if (!it->bidi_p
7328 /* If the string position is beyond string's end, it means
7329 next_element_from_c_string is padding the string with
7330 blanks, in which case we bypass the bidi iterator,
7331 because it cannot deal with such virtual characters. */
7332 || IT_CHARPOS (*it) >= it->bidi_it.string.schars)
7334 IT_BYTEPOS (*it) += it->len;
7335 IT_CHARPOS (*it) += 1;
7337 else
7339 bidi_move_to_visually_next (&it->bidi_it);
7340 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7341 IT_CHARPOS (*it) = it->bidi_it.charpos;
7343 break;
7345 case GET_FROM_DISPLAY_VECTOR:
7346 /* Current display element of IT is from a display table entry.
7347 Advance in the display table definition. Reset it to null if
7348 end reached, and continue with characters from buffers/
7349 strings. */
7350 ++it->current.dpvec_index;
7352 /* Restore face of the iterator to what they were before the
7353 display vector entry (these entries may contain faces). */
7354 it->face_id = it->saved_face_id;
7356 if (it->dpvec + it->current.dpvec_index >= it->dpend)
7358 int recheck_faces = it->ellipsis_p;
7360 if (it->s)
7361 it->method = GET_FROM_C_STRING;
7362 else if (STRINGP (it->string))
7363 it->method = GET_FROM_STRING;
7364 else
7366 it->method = GET_FROM_BUFFER;
7367 it->object = it->w->contents;
7370 it->dpvec = NULL;
7371 it->current.dpvec_index = -1;
7373 /* Skip over characters which were displayed via IT->dpvec. */
7374 if (it->dpvec_char_len < 0)
7375 reseat_at_next_visible_line_start (it, 1);
7376 else if (it->dpvec_char_len > 0)
7378 if (it->method == GET_FROM_STRING
7379 && it->current.overlay_string_index >= 0
7380 && it->n_overlay_strings > 0)
7381 it->ignore_overlay_strings_at_pos_p = true;
7382 it->len = it->dpvec_char_len;
7383 set_iterator_to_next (it, reseat_p);
7386 /* Maybe recheck faces after display vector. */
7387 if (recheck_faces)
7388 it->stop_charpos = IT_CHARPOS (*it);
7390 break;
7392 case GET_FROM_STRING:
7393 /* Current display element is a character from a Lisp string. */
7394 eassert (it->s == NULL && STRINGP (it->string));
7395 /* Don't advance past string end. These conditions are true
7396 when set_iterator_to_next is called at the end of
7397 get_next_display_element, in which case the Lisp string is
7398 already exhausted, and all we want is pop the iterator
7399 stack. */
7400 if (it->current.overlay_string_index >= 0)
7402 /* This is an overlay string, so there's no padding with
7403 spaces, and the number of characters in the string is
7404 where the string ends. */
7405 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7406 goto consider_string_end;
7408 else
7410 /* Not an overlay string. There could be padding, so test
7411 against it->end_charpos. */
7412 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
7413 goto consider_string_end;
7415 if (it->cmp_it.id >= 0)
7417 /* We are delivering display elements from a composition.
7418 Update the string position past the grapheme cluster
7419 we've just processed. */
7420 if (! it->bidi_p)
7422 IT_STRING_CHARPOS (*it) += it->cmp_it.nchars;
7423 IT_STRING_BYTEPOS (*it) += it->cmp_it.nbytes;
7425 else
7427 int i;
7429 for (i = 0; i < it->cmp_it.nchars; i++)
7430 bidi_move_to_visually_next (&it->bidi_it);
7431 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7432 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7435 /* Did we exhaust all the grapheme clusters of this
7436 composition? */
7437 if ((! it->bidi_p || ! it->cmp_it.reversed_p)
7438 && (it->cmp_it.to < it->cmp_it.nglyphs))
7440 /* Not all the grapheme clusters were processed yet;
7441 advance to the next cluster. */
7442 it->cmp_it.from = it->cmp_it.to;
7444 else if ((it->bidi_p && it->cmp_it.reversed_p)
7445 && it->cmp_it.from > 0)
7447 /* Likewise: advance to the next cluster, but going in
7448 the reverse direction. */
7449 it->cmp_it.to = it->cmp_it.from;
7451 else
7453 /* This composition was fully processed; find the next
7454 candidate place for checking for composed
7455 characters. */
7456 /* Always limit string searches to the string length;
7457 any padding spaces are not part of the string, and
7458 there cannot be any compositions in that padding. */
7459 ptrdiff_t stop = SCHARS (it->string);
7461 if (it->bidi_p && it->bidi_it.scan_dir < 0)
7462 stop = -1;
7463 else if (it->end_charpos < stop)
7465 /* Cf. PRECISION in reseat_to_string: we might be
7466 limited in how many of the string characters we
7467 need to deliver. */
7468 stop = it->end_charpos;
7470 composition_compute_stop_pos (&it->cmp_it,
7471 IT_STRING_CHARPOS (*it),
7472 IT_STRING_BYTEPOS (*it), stop,
7473 it->string);
7476 else
7478 if (!it->bidi_p
7479 /* If the string position is beyond string's end, it
7480 means next_element_from_string is padding the string
7481 with blanks, in which case we bypass the bidi
7482 iterator, because it cannot deal with such virtual
7483 characters. */
7484 || IT_STRING_CHARPOS (*it) >= it->bidi_it.string.schars)
7486 IT_STRING_BYTEPOS (*it) += it->len;
7487 IT_STRING_CHARPOS (*it) += 1;
7489 else
7491 int prev_scan_dir = it->bidi_it.scan_dir;
7493 bidi_move_to_visually_next (&it->bidi_it);
7494 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7495 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7496 /* If the scan direction changes, we may need to update
7497 the place where to check for composed characters. */
7498 if (prev_scan_dir != it->bidi_it.scan_dir)
7500 ptrdiff_t stop = SCHARS (it->string);
7502 if (it->bidi_it.scan_dir < 0)
7503 stop = -1;
7504 else if (it->end_charpos < stop)
7505 stop = it->end_charpos;
7507 composition_compute_stop_pos (&it->cmp_it,
7508 IT_STRING_CHARPOS (*it),
7509 IT_STRING_BYTEPOS (*it), stop,
7510 it->string);
7515 consider_string_end:
7517 if (it->current.overlay_string_index >= 0)
7519 /* IT->string is an overlay string. Advance to the
7520 next, if there is one. */
7521 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7523 it->ellipsis_p = 0;
7524 next_overlay_string (it);
7525 if (it->ellipsis_p)
7526 setup_for_ellipsis (it, 0);
7529 else
7531 /* IT->string is not an overlay string. If we reached
7532 its end, and there is something on IT->stack, proceed
7533 with what is on the stack. This can be either another
7534 string, this time an overlay string, or a buffer. */
7535 if (IT_STRING_CHARPOS (*it) == SCHARS (it->string)
7536 && it->sp > 0)
7538 pop_it (it);
7539 if (it->method == GET_FROM_STRING)
7540 goto consider_string_end;
7543 break;
7545 case GET_FROM_IMAGE:
7546 case GET_FROM_STRETCH:
7547 /* The position etc with which we have to proceed are on
7548 the stack. The position may be at the end of a string,
7549 if the `display' property takes up the whole string. */
7550 eassert (it->sp > 0);
7551 pop_it (it);
7552 if (it->method == GET_FROM_STRING)
7553 goto consider_string_end;
7554 break;
7556 default:
7557 /* There are no other methods defined, so this should be a bug. */
7558 emacs_abort ();
7561 eassert (it->method != GET_FROM_STRING
7562 || (STRINGP (it->string)
7563 && IT_STRING_CHARPOS (*it) >= 0));
7566 /* Load IT's display element fields with information about the next
7567 display element which comes from a display table entry or from the
7568 result of translating a control character to one of the forms `^C'
7569 or `\003'.
7571 IT->dpvec holds the glyphs to return as characters.
7572 IT->saved_face_id holds the face id before the display vector--it
7573 is restored into IT->face_id in set_iterator_to_next. */
7575 static int
7576 next_element_from_display_vector (struct it *it)
7578 Lisp_Object gc;
7579 int prev_face_id = it->face_id;
7580 int next_face_id;
7582 /* Precondition. */
7583 eassert (it->dpvec && it->current.dpvec_index >= 0);
7585 it->face_id = it->saved_face_id;
7587 /* KFS: This code used to check ip->dpvec[0] instead of the current element.
7588 That seemed totally bogus - so I changed it... */
7589 gc = it->dpvec[it->current.dpvec_index];
7591 if (GLYPH_CODE_P (gc))
7593 struct face *this_face, *prev_face, *next_face;
7595 it->c = GLYPH_CODE_CHAR (gc);
7596 it->len = CHAR_BYTES (it->c);
7598 /* The entry may contain a face id to use. Such a face id is
7599 the id of a Lisp face, not a realized face. A face id of
7600 zero means no face is specified. */
7601 if (it->dpvec_face_id >= 0)
7602 it->face_id = it->dpvec_face_id;
7603 else
7605 int lface_id = GLYPH_CODE_FACE (gc);
7606 if (lface_id > 0)
7607 it->face_id = merge_faces (it->f, Qt, lface_id,
7608 it->saved_face_id);
7611 /* Glyphs in the display vector could have the box face, so we
7612 need to set the related flags in the iterator, as
7613 appropriate. */
7614 this_face = FACE_FROM_ID (it->f, it->face_id);
7615 prev_face = FACE_FROM_ID (it->f, prev_face_id);
7617 /* Is this character the first character of a box-face run? */
7618 it->start_of_box_run_p = (this_face && this_face->box != FACE_NO_BOX
7619 && (!prev_face
7620 || prev_face->box == FACE_NO_BOX));
7622 /* For the last character of the box-face run, we need to look
7623 either at the next glyph from the display vector, or at the
7624 face we saw before the display vector. */
7625 next_face_id = it->saved_face_id;
7626 if (it->current.dpvec_index < it->dpend - it->dpvec - 1)
7628 if (it->dpvec_face_id >= 0)
7629 next_face_id = it->dpvec_face_id;
7630 else
7632 int lface_id =
7633 GLYPH_CODE_FACE (it->dpvec[it->current.dpvec_index + 1]);
7635 if (lface_id > 0)
7636 next_face_id = merge_faces (it->f, Qt, lface_id,
7637 it->saved_face_id);
7640 next_face = FACE_FROM_ID (it->f, next_face_id);
7641 it->end_of_box_run_p = (this_face && this_face->box != FACE_NO_BOX
7642 && (!next_face
7643 || next_face->box == FACE_NO_BOX));
7644 it->face_box_p = this_face && this_face->box != FACE_NO_BOX;
7646 else
7647 /* Display table entry is invalid. Return a space. */
7648 it->c = ' ', it->len = 1;
7650 /* Don't change position and object of the iterator here. They are
7651 still the values of the character that had this display table
7652 entry or was translated, and that's what we want. */
7653 it->what = IT_CHARACTER;
7654 return 1;
7657 /* Get the first element of string/buffer in the visual order, after
7658 being reseated to a new position in a string or a buffer. */
7659 static void
7660 get_visually_first_element (struct it *it)
7662 int string_p = STRINGP (it->string) || it->s;
7663 ptrdiff_t eob = (string_p ? it->bidi_it.string.schars : ZV);
7664 ptrdiff_t bob = (string_p ? 0 : BEGV);
7666 if (STRINGP (it->string))
7668 it->bidi_it.charpos = IT_STRING_CHARPOS (*it);
7669 it->bidi_it.bytepos = IT_STRING_BYTEPOS (*it);
7671 else
7673 it->bidi_it.charpos = IT_CHARPOS (*it);
7674 it->bidi_it.bytepos = IT_BYTEPOS (*it);
7677 if (it->bidi_it.charpos == eob)
7679 /* Nothing to do, but reset the FIRST_ELT flag, like
7680 bidi_paragraph_init does, because we are not going to
7681 call it. */
7682 it->bidi_it.first_elt = 0;
7684 else if (it->bidi_it.charpos == bob
7685 || (!string_p
7686 && (FETCH_CHAR (it->bidi_it.bytepos - 1) == '\n'
7687 || FETCH_CHAR (it->bidi_it.bytepos) == '\n')))
7689 /* If we are at the beginning of a line/string, we can produce
7690 the next element right away. */
7691 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
7692 bidi_move_to_visually_next (&it->bidi_it);
7694 else
7696 ptrdiff_t orig_bytepos = it->bidi_it.bytepos;
7698 /* We need to prime the bidi iterator starting at the line's or
7699 string's beginning, before we will be able to produce the
7700 next element. */
7701 if (string_p)
7702 it->bidi_it.charpos = it->bidi_it.bytepos = 0;
7703 else
7704 it->bidi_it.charpos = find_newline_no_quit (IT_CHARPOS (*it),
7705 IT_BYTEPOS (*it), -1,
7706 &it->bidi_it.bytepos);
7707 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
7710 /* Now return to buffer/string position where we were asked
7711 to get the next display element, and produce that. */
7712 bidi_move_to_visually_next (&it->bidi_it);
7714 while (it->bidi_it.bytepos != orig_bytepos
7715 && it->bidi_it.charpos < eob);
7718 /* Adjust IT's position information to where we ended up. */
7719 if (STRINGP (it->string))
7721 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7722 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7724 else
7726 IT_CHARPOS (*it) = it->bidi_it.charpos;
7727 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7730 if (STRINGP (it->string) || !it->s)
7732 ptrdiff_t stop, charpos, bytepos;
7734 if (STRINGP (it->string))
7736 eassert (!it->s);
7737 stop = SCHARS (it->string);
7738 if (stop > it->end_charpos)
7739 stop = it->end_charpos;
7740 charpos = IT_STRING_CHARPOS (*it);
7741 bytepos = IT_STRING_BYTEPOS (*it);
7743 else
7745 stop = it->end_charpos;
7746 charpos = IT_CHARPOS (*it);
7747 bytepos = IT_BYTEPOS (*it);
7749 if (it->bidi_it.scan_dir < 0)
7750 stop = -1;
7751 composition_compute_stop_pos (&it->cmp_it, charpos, bytepos, stop,
7752 it->string);
7756 /* Load IT with the next display element from Lisp string IT->string.
7757 IT->current.string_pos is the current position within the string.
7758 If IT->current.overlay_string_index >= 0, the Lisp string is an
7759 overlay string. */
7761 static int
7762 next_element_from_string (struct it *it)
7764 struct text_pos position;
7766 eassert (STRINGP (it->string));
7767 eassert (!it->bidi_p || EQ (it->string, it->bidi_it.string.lstring));
7768 eassert (IT_STRING_CHARPOS (*it) >= 0);
7769 position = it->current.string_pos;
7771 /* With bidi reordering, the character to display might not be the
7772 character at IT_STRING_CHARPOS. BIDI_IT.FIRST_ELT non-zero means
7773 that we were reseat()ed to a new string, whose paragraph
7774 direction is not known. */
7775 if (it->bidi_p && it->bidi_it.first_elt)
7777 get_visually_first_element (it);
7778 SET_TEXT_POS (position, IT_STRING_CHARPOS (*it), IT_STRING_BYTEPOS (*it));
7781 /* Time to check for invisible text? */
7782 if (IT_STRING_CHARPOS (*it) < it->end_charpos)
7784 if (IT_STRING_CHARPOS (*it) >= it->stop_charpos)
7786 if (!(!it->bidi_p
7787 || BIDI_AT_BASE_LEVEL (it->bidi_it)
7788 || IT_STRING_CHARPOS (*it) == it->stop_charpos))
7790 /* With bidi non-linear iteration, we could find
7791 ourselves far beyond the last computed stop_charpos,
7792 with several other stop positions in between that we
7793 missed. Scan them all now, in buffer's logical
7794 order, until we find and handle the last stop_charpos
7795 that precedes our current position. */
7796 handle_stop_backwards (it, it->stop_charpos);
7797 return GET_NEXT_DISPLAY_ELEMENT (it);
7799 else
7801 if (it->bidi_p)
7803 /* Take note of the stop position we just moved
7804 across, for when we will move back across it. */
7805 it->prev_stop = it->stop_charpos;
7806 /* If we are at base paragraph embedding level, take
7807 note of the last stop position seen at this
7808 level. */
7809 if (BIDI_AT_BASE_LEVEL (it->bidi_it))
7810 it->base_level_stop = it->stop_charpos;
7812 handle_stop (it);
7814 /* Since a handler may have changed IT->method, we must
7815 recurse here. */
7816 return GET_NEXT_DISPLAY_ELEMENT (it);
7819 else if (it->bidi_p
7820 /* If we are before prev_stop, we may have overstepped
7821 on our way backwards a stop_pos, and if so, we need
7822 to handle that stop_pos. */
7823 && IT_STRING_CHARPOS (*it) < it->prev_stop
7824 /* We can sometimes back up for reasons that have nothing
7825 to do with bidi reordering. E.g., compositions. The
7826 code below is only needed when we are above the base
7827 embedding level, so test for that explicitly. */
7828 && !BIDI_AT_BASE_LEVEL (it->bidi_it))
7830 /* If we lost track of base_level_stop, we have no better
7831 place for handle_stop_backwards to start from than string
7832 beginning. This happens, e.g., when we were reseated to
7833 the previous screenful of text by vertical-motion. */
7834 if (it->base_level_stop <= 0
7835 || IT_STRING_CHARPOS (*it) < it->base_level_stop)
7836 it->base_level_stop = 0;
7837 handle_stop_backwards (it, it->base_level_stop);
7838 return GET_NEXT_DISPLAY_ELEMENT (it);
7842 if (it->current.overlay_string_index >= 0)
7844 /* Get the next character from an overlay string. In overlay
7845 strings, there is no field width or padding with spaces to
7846 do. */
7847 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7849 it->what = IT_EOB;
7850 return 0;
7852 else if (CHAR_COMPOSED_P (it, IT_STRING_CHARPOS (*it),
7853 IT_STRING_BYTEPOS (*it),
7854 it->bidi_it.scan_dir < 0
7855 ? -1
7856 : SCHARS (it->string))
7857 && next_element_from_composition (it))
7859 return 1;
7861 else if (STRING_MULTIBYTE (it->string))
7863 const unsigned char *s = (SDATA (it->string)
7864 + IT_STRING_BYTEPOS (*it));
7865 it->c = string_char_and_length (s, &it->len);
7867 else
7869 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
7870 it->len = 1;
7873 else
7875 /* Get the next character from a Lisp string that is not an
7876 overlay string. Such strings come from the mode line, for
7877 example. We may have to pad with spaces, or truncate the
7878 string. See also next_element_from_c_string. */
7879 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
7881 it->what = IT_EOB;
7882 return 0;
7884 else if (IT_STRING_CHARPOS (*it) >= it->string_nchars)
7886 /* Pad with spaces. */
7887 it->c = ' ', it->len = 1;
7888 CHARPOS (position) = BYTEPOS (position) = -1;
7890 else if (CHAR_COMPOSED_P (it, IT_STRING_CHARPOS (*it),
7891 IT_STRING_BYTEPOS (*it),
7892 it->bidi_it.scan_dir < 0
7893 ? -1
7894 : it->string_nchars)
7895 && next_element_from_composition (it))
7897 return 1;
7899 else if (STRING_MULTIBYTE (it->string))
7901 const unsigned char *s = (SDATA (it->string)
7902 + IT_STRING_BYTEPOS (*it));
7903 it->c = string_char_and_length (s, &it->len);
7905 else
7907 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
7908 it->len = 1;
7912 /* Record what we have and where it came from. */
7913 it->what = IT_CHARACTER;
7914 it->object = it->string;
7915 it->position = position;
7916 return 1;
7920 /* Load IT with next display element from C string IT->s.
7921 IT->string_nchars is the maximum number of characters to return
7922 from the string. IT->end_charpos may be greater than
7923 IT->string_nchars when this function is called, in which case we
7924 may have to return padding spaces. Value is zero if end of string
7925 reached, including padding spaces. */
7927 static int
7928 next_element_from_c_string (struct it *it)
7930 bool success_p = true;
7932 eassert (it->s);
7933 eassert (!it->bidi_p || it->s == it->bidi_it.string.s);
7934 it->what = IT_CHARACTER;
7935 BYTEPOS (it->position) = CHARPOS (it->position) = 0;
7936 it->object = make_number (0);
7938 /* With bidi reordering, the character to display might not be the
7939 character at IT_CHARPOS. BIDI_IT.FIRST_ELT non-zero means that
7940 we were reseated to a new string, whose paragraph direction is
7941 not known. */
7942 if (it->bidi_p && it->bidi_it.first_elt)
7943 get_visually_first_element (it);
7945 /* IT's position can be greater than IT->string_nchars in case a
7946 field width or precision has been specified when the iterator was
7947 initialized. */
7948 if (IT_CHARPOS (*it) >= it->end_charpos)
7950 /* End of the game. */
7951 it->what = IT_EOB;
7952 success_p = 0;
7954 else if (IT_CHARPOS (*it) >= it->string_nchars)
7956 /* Pad with spaces. */
7957 it->c = ' ', it->len = 1;
7958 BYTEPOS (it->position) = CHARPOS (it->position) = -1;
7960 else if (it->multibyte_p)
7961 it->c = string_char_and_length (it->s + IT_BYTEPOS (*it), &it->len);
7962 else
7963 it->c = it->s[IT_BYTEPOS (*it)], it->len = 1;
7965 return success_p;
7969 /* Set up IT to return characters from an ellipsis, if appropriate.
7970 The definition of the ellipsis glyphs may come from a display table
7971 entry. This function fills IT with the first glyph from the
7972 ellipsis if an ellipsis is to be displayed. */
7974 static int
7975 next_element_from_ellipsis (struct it *it)
7977 if (it->selective_display_ellipsis_p)
7978 setup_for_ellipsis (it, it->len);
7979 else
7981 /* The face at the current position may be different from the
7982 face we find after the invisible text. Remember what it
7983 was in IT->saved_face_id, and signal that it's there by
7984 setting face_before_selective_p. */
7985 it->saved_face_id = it->face_id;
7986 it->method = GET_FROM_BUFFER;
7987 it->object = it->w->contents;
7988 reseat_at_next_visible_line_start (it, 1);
7989 it->face_before_selective_p = true;
7992 return GET_NEXT_DISPLAY_ELEMENT (it);
7996 /* Deliver an image display element. The iterator IT is already
7997 filled with image information (done in handle_display_prop). Value
7998 is always 1. */
8001 static int
8002 next_element_from_image (struct it *it)
8004 it->what = IT_IMAGE;
8005 it->ignore_overlay_strings_at_pos_p = 0;
8006 return 1;
8010 /* Fill iterator IT with next display element from a stretch glyph
8011 property. IT->object is the value of the text property. Value is
8012 always 1. */
8014 static int
8015 next_element_from_stretch (struct it *it)
8017 it->what = IT_STRETCH;
8018 return 1;
8021 /* Scan backwards from IT's current position until we find a stop
8022 position, or until BEGV. This is called when we find ourself
8023 before both the last known prev_stop and base_level_stop while
8024 reordering bidirectional text. */
8026 static void
8027 compute_stop_pos_backwards (struct it *it)
8029 const int SCAN_BACK_LIMIT = 1000;
8030 struct text_pos pos;
8031 struct display_pos save_current = it->current;
8032 struct text_pos save_position = it->position;
8033 ptrdiff_t charpos = IT_CHARPOS (*it);
8034 ptrdiff_t where_we_are = charpos;
8035 ptrdiff_t save_stop_pos = it->stop_charpos;
8036 ptrdiff_t save_end_pos = it->end_charpos;
8038 eassert (NILP (it->string) && !it->s);
8039 eassert (it->bidi_p);
8040 it->bidi_p = 0;
8043 it->end_charpos = min (charpos + 1, ZV);
8044 charpos = max (charpos - SCAN_BACK_LIMIT, BEGV);
8045 SET_TEXT_POS (pos, charpos, CHAR_TO_BYTE (charpos));
8046 reseat_1 (it, pos, 0);
8047 compute_stop_pos (it);
8048 /* We must advance forward, right? */
8049 if (it->stop_charpos <= charpos)
8050 emacs_abort ();
8052 while (charpos > BEGV && it->stop_charpos >= it->end_charpos);
8054 if (it->stop_charpos <= where_we_are)
8055 it->prev_stop = it->stop_charpos;
8056 else
8057 it->prev_stop = BEGV;
8058 it->bidi_p = true;
8059 it->current = save_current;
8060 it->position = save_position;
8061 it->stop_charpos = save_stop_pos;
8062 it->end_charpos = save_end_pos;
8065 /* Scan forward from CHARPOS in the current buffer/string, until we
8066 find a stop position > current IT's position. Then handle the stop
8067 position before that. This is called when we bump into a stop
8068 position while reordering bidirectional text. CHARPOS should be
8069 the last previously processed stop_pos (or BEGV/0, if none were
8070 processed yet) whose position is less that IT's current
8071 position. */
8073 static void
8074 handle_stop_backwards (struct it *it, ptrdiff_t charpos)
8076 int bufp = !STRINGP (it->string);
8077 ptrdiff_t where_we_are = (bufp ? IT_CHARPOS (*it) : IT_STRING_CHARPOS (*it));
8078 struct display_pos save_current = it->current;
8079 struct text_pos save_position = it->position;
8080 struct text_pos pos1;
8081 ptrdiff_t next_stop;
8083 /* Scan in strict logical order. */
8084 eassert (it->bidi_p);
8085 it->bidi_p = 0;
8088 it->prev_stop = charpos;
8089 if (bufp)
8091 SET_TEXT_POS (pos1, charpos, CHAR_TO_BYTE (charpos));
8092 reseat_1 (it, pos1, 0);
8094 else
8095 it->current.string_pos = string_pos (charpos, it->string);
8096 compute_stop_pos (it);
8097 /* We must advance forward, right? */
8098 if (it->stop_charpos <= it->prev_stop)
8099 emacs_abort ();
8100 charpos = it->stop_charpos;
8102 while (charpos <= where_we_are);
8104 it->bidi_p = true;
8105 it->current = save_current;
8106 it->position = save_position;
8107 next_stop = it->stop_charpos;
8108 it->stop_charpos = it->prev_stop;
8109 handle_stop (it);
8110 it->stop_charpos = next_stop;
8113 /* Load IT with the next display element from current_buffer. Value
8114 is zero if end of buffer reached. IT->stop_charpos is the next
8115 position at which to stop and check for text properties or buffer
8116 end. */
8118 static int
8119 next_element_from_buffer (struct it *it)
8121 bool success_p = true;
8123 eassert (IT_CHARPOS (*it) >= BEGV);
8124 eassert (NILP (it->string) && !it->s);
8125 eassert (!it->bidi_p
8126 || (EQ (it->bidi_it.string.lstring, Qnil)
8127 && it->bidi_it.string.s == NULL));
8129 /* With bidi reordering, the character to display might not be the
8130 character at IT_CHARPOS. BIDI_IT.FIRST_ELT non-zero means that
8131 we were reseat()ed to a new buffer position, which is potentially
8132 a different paragraph. */
8133 if (it->bidi_p && it->bidi_it.first_elt)
8135 get_visually_first_element (it);
8136 SET_TEXT_POS (it->position, IT_CHARPOS (*it), IT_BYTEPOS (*it));
8139 if (IT_CHARPOS (*it) >= it->stop_charpos)
8141 if (IT_CHARPOS (*it) >= it->end_charpos)
8143 int overlay_strings_follow_p;
8145 /* End of the game, except when overlay strings follow that
8146 haven't been returned yet. */
8147 if (it->overlay_strings_at_end_processed_p)
8148 overlay_strings_follow_p = 0;
8149 else
8151 it->overlay_strings_at_end_processed_p = true;
8152 overlay_strings_follow_p = get_overlay_strings (it, 0);
8155 if (overlay_strings_follow_p)
8156 success_p = GET_NEXT_DISPLAY_ELEMENT (it);
8157 else
8159 it->what = IT_EOB;
8160 it->position = it->current.pos;
8161 success_p = 0;
8164 else if (!(!it->bidi_p
8165 || BIDI_AT_BASE_LEVEL (it->bidi_it)
8166 || IT_CHARPOS (*it) == it->stop_charpos))
8168 /* With bidi non-linear iteration, we could find ourselves
8169 far beyond the last computed stop_charpos, with several
8170 other stop positions in between that we missed. Scan
8171 them all now, in buffer's logical order, until we find
8172 and handle the last stop_charpos that precedes our
8173 current position. */
8174 handle_stop_backwards (it, it->stop_charpos);
8175 return GET_NEXT_DISPLAY_ELEMENT (it);
8177 else
8179 if (it->bidi_p)
8181 /* Take note of the stop position we just moved across,
8182 for when we will move back across it. */
8183 it->prev_stop = it->stop_charpos;
8184 /* If we are at base paragraph embedding level, take
8185 note of the last stop position seen at this
8186 level. */
8187 if (BIDI_AT_BASE_LEVEL (it->bidi_it))
8188 it->base_level_stop = it->stop_charpos;
8190 handle_stop (it);
8191 return GET_NEXT_DISPLAY_ELEMENT (it);
8194 else if (it->bidi_p
8195 /* If we are before prev_stop, we may have overstepped on
8196 our way backwards a stop_pos, and if so, we need to
8197 handle that stop_pos. */
8198 && IT_CHARPOS (*it) < it->prev_stop
8199 /* We can sometimes back up for reasons that have nothing
8200 to do with bidi reordering. E.g., compositions. The
8201 code below is only needed when we are above the base
8202 embedding level, so test for that explicitly. */
8203 && !BIDI_AT_BASE_LEVEL (it->bidi_it))
8205 if (it->base_level_stop <= 0
8206 || IT_CHARPOS (*it) < it->base_level_stop)
8208 /* If we lost track of base_level_stop, we need to find
8209 prev_stop by looking backwards. This happens, e.g., when
8210 we were reseated to the previous screenful of text by
8211 vertical-motion. */
8212 it->base_level_stop = BEGV;
8213 compute_stop_pos_backwards (it);
8214 handle_stop_backwards (it, it->prev_stop);
8216 else
8217 handle_stop_backwards (it, it->base_level_stop);
8218 return GET_NEXT_DISPLAY_ELEMENT (it);
8220 else
8222 /* No face changes, overlays etc. in sight, so just return a
8223 character from current_buffer. */
8224 unsigned char *p;
8225 ptrdiff_t stop;
8227 /* We moved to the next buffer position, so any info about
8228 previously seen overlays is no longer valid. */
8229 it->ignore_overlay_strings_at_pos_p = 0;
8231 /* Maybe run the redisplay end trigger hook. Performance note:
8232 This doesn't seem to cost measurable time. */
8233 if (it->redisplay_end_trigger_charpos
8234 && it->glyph_row
8235 && IT_CHARPOS (*it) >= it->redisplay_end_trigger_charpos)
8236 run_redisplay_end_trigger_hook (it);
8238 stop = it->bidi_it.scan_dir < 0 ? -1 : it->end_charpos;
8239 if (CHAR_COMPOSED_P (it, IT_CHARPOS (*it), IT_BYTEPOS (*it),
8240 stop)
8241 && next_element_from_composition (it))
8243 return 1;
8246 /* Get the next character, maybe multibyte. */
8247 p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
8248 if (it->multibyte_p && !ASCII_CHAR_P (*p))
8249 it->c = STRING_CHAR_AND_LENGTH (p, it->len);
8250 else
8251 it->c = *p, it->len = 1;
8253 /* Record what we have and where it came from. */
8254 it->what = IT_CHARACTER;
8255 it->object = it->w->contents;
8256 it->position = it->current.pos;
8258 /* Normally we return the character found above, except when we
8259 really want to return an ellipsis for selective display. */
8260 if (it->selective)
8262 if (it->c == '\n')
8264 /* A value of selective > 0 means hide lines indented more
8265 than that number of columns. */
8266 if (it->selective > 0
8267 && IT_CHARPOS (*it) + 1 < ZV
8268 && indented_beyond_p (IT_CHARPOS (*it) + 1,
8269 IT_BYTEPOS (*it) + 1,
8270 it->selective))
8272 success_p = next_element_from_ellipsis (it);
8273 it->dpvec_char_len = -1;
8276 else if (it->c == '\r' && it->selective == -1)
8278 /* A value of selective == -1 means that everything from the
8279 CR to the end of the line is invisible, with maybe an
8280 ellipsis displayed for it. */
8281 success_p = next_element_from_ellipsis (it);
8282 it->dpvec_char_len = -1;
8287 /* Value is zero if end of buffer reached. */
8288 eassert (!success_p || it->what != IT_CHARACTER || it->len > 0);
8289 return success_p;
8293 /* Run the redisplay end trigger hook for IT. */
8295 static void
8296 run_redisplay_end_trigger_hook (struct it *it)
8298 Lisp_Object args[3];
8300 /* IT->glyph_row should be non-null, i.e. we should be actually
8301 displaying something, or otherwise we should not run the hook. */
8302 eassert (it->glyph_row);
8304 /* Set up hook arguments. */
8305 args[0] = Qredisplay_end_trigger_functions;
8306 args[1] = it->window;
8307 XSETINT (args[2], it->redisplay_end_trigger_charpos);
8308 it->redisplay_end_trigger_charpos = 0;
8310 /* Since we are *trying* to run these functions, don't try to run
8311 them again, even if they get an error. */
8312 wset_redisplay_end_trigger (it->w, Qnil);
8313 Frun_hook_with_args (3, args);
8315 /* Notice if it changed the face of the character we are on. */
8316 handle_face_prop (it);
8320 /* Deliver a composition display element. Unlike the other
8321 next_element_from_XXX, this function is not registered in the array
8322 get_next_element[]. It is called from next_element_from_buffer and
8323 next_element_from_string when necessary. */
8325 static int
8326 next_element_from_composition (struct it *it)
8328 it->what = IT_COMPOSITION;
8329 it->len = it->cmp_it.nbytes;
8330 if (STRINGP (it->string))
8332 if (it->c < 0)
8334 IT_STRING_CHARPOS (*it) += it->cmp_it.nchars;
8335 IT_STRING_BYTEPOS (*it) += it->cmp_it.nbytes;
8336 return 0;
8338 it->position = it->current.string_pos;
8339 it->object = it->string;
8340 it->c = composition_update_it (&it->cmp_it, IT_STRING_CHARPOS (*it),
8341 IT_STRING_BYTEPOS (*it), it->string);
8343 else
8345 if (it->c < 0)
8347 IT_CHARPOS (*it) += it->cmp_it.nchars;
8348 IT_BYTEPOS (*it) += it->cmp_it.nbytes;
8349 if (it->bidi_p)
8351 if (it->bidi_it.new_paragraph)
8352 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 0);
8353 /* Resync the bidi iterator with IT's new position.
8354 FIXME: this doesn't support bidirectional text. */
8355 while (it->bidi_it.charpos < IT_CHARPOS (*it))
8356 bidi_move_to_visually_next (&it->bidi_it);
8358 return 0;
8360 it->position = it->current.pos;
8361 it->object = it->w->contents;
8362 it->c = composition_update_it (&it->cmp_it, IT_CHARPOS (*it),
8363 IT_BYTEPOS (*it), Qnil);
8365 return 1;
8370 /***********************************************************************
8371 Moving an iterator without producing glyphs
8372 ***********************************************************************/
8374 /* Check if iterator is at a position corresponding to a valid buffer
8375 position after some move_it_ call. */
8377 #define IT_POS_VALID_AFTER_MOVE_P(it) \
8378 ((it)->method == GET_FROM_STRING \
8379 ? IT_STRING_CHARPOS (*it) == 0 \
8380 : 1)
8383 /* Move iterator IT to a specified buffer or X position within one
8384 line on the display without producing glyphs.
8386 OP should be a bit mask including some or all of these bits:
8387 MOVE_TO_X: Stop upon reaching x-position TO_X.
8388 MOVE_TO_POS: Stop upon reaching buffer or string position TO_CHARPOS.
8389 Regardless of OP's value, stop upon reaching the end of the display line.
8391 TO_X is normally a value 0 <= TO_X <= IT->last_visible_x.
8392 This means, in particular, that TO_X includes window's horizontal
8393 scroll amount.
8395 The return value has several possible values that
8396 say what condition caused the scan to stop:
8398 MOVE_POS_MATCH_OR_ZV
8399 - when TO_POS or ZV was reached.
8401 MOVE_X_REACHED
8402 -when TO_X was reached before TO_POS or ZV were reached.
8404 MOVE_LINE_CONTINUED
8405 - when we reached the end of the display area and the line must
8406 be continued.
8408 MOVE_LINE_TRUNCATED
8409 - when we reached the end of the display area and the line is
8410 truncated.
8412 MOVE_NEWLINE_OR_CR
8413 - when we stopped at a line end, i.e. a newline or a CR and selective
8414 display is on. */
8416 static enum move_it_result
8417 move_it_in_display_line_to (struct it *it,
8418 ptrdiff_t to_charpos, int to_x,
8419 enum move_operation_enum op)
8421 enum move_it_result result = MOVE_UNDEFINED;
8422 struct glyph_row *saved_glyph_row;
8423 struct it wrap_it, atpos_it, atx_it, ppos_it;
8424 void *wrap_data = NULL, *atpos_data = NULL, *atx_data = NULL;
8425 void *ppos_data = NULL;
8426 int may_wrap = 0;
8427 enum it_method prev_method = it->method;
8428 ptrdiff_t closest_pos IF_LINT (= 0), prev_pos = IT_CHARPOS (*it);
8429 int saw_smaller_pos = prev_pos < to_charpos;
8431 /* Don't produce glyphs in produce_glyphs. */
8432 saved_glyph_row = it->glyph_row;
8433 it->glyph_row = NULL;
8435 /* Use wrap_it to save a copy of IT wherever a word wrap could
8436 occur. Use atpos_it to save a copy of IT at the desired buffer
8437 position, if found, so that we can scan ahead and check if the
8438 word later overshoots the window edge. Use atx_it similarly, for
8439 pixel positions. */
8440 wrap_it.sp = -1;
8441 atpos_it.sp = -1;
8442 atx_it.sp = -1;
8444 /* Use ppos_it under bidi reordering to save a copy of IT for the
8445 initial position. We restore that position in IT when we have
8446 scanned the entire display line without finding a match for
8447 TO_CHARPOS and all the character positions are greater than
8448 TO_CHARPOS. We then restart the scan from the initial position,
8449 and stop at CLOSEST_POS, which is a position > TO_CHARPOS that is
8450 the closest to TO_CHARPOS. */
8451 if (it->bidi_p)
8453 if ((op & MOVE_TO_POS) && IT_CHARPOS (*it) >= to_charpos)
8455 SAVE_IT (ppos_it, *it, ppos_data);
8456 closest_pos = IT_CHARPOS (*it);
8458 else
8459 closest_pos = ZV;
8462 #define BUFFER_POS_REACHED_P() \
8463 ((op & MOVE_TO_POS) != 0 \
8464 && BUFFERP (it->object) \
8465 && (IT_CHARPOS (*it) == to_charpos \
8466 || ((!it->bidi_p \
8467 || BIDI_AT_BASE_LEVEL (it->bidi_it)) \
8468 && IT_CHARPOS (*it) > to_charpos) \
8469 || (it->what == IT_COMPOSITION \
8470 && ((IT_CHARPOS (*it) > to_charpos \
8471 && to_charpos >= it->cmp_it.charpos) \
8472 || (IT_CHARPOS (*it) < to_charpos \
8473 && to_charpos <= it->cmp_it.charpos)))) \
8474 && (it->method == GET_FROM_BUFFER \
8475 || (it->method == GET_FROM_DISPLAY_VECTOR \
8476 && it->dpvec + it->current.dpvec_index + 1 >= it->dpend)))
8478 /* If there's a line-/wrap-prefix, handle it. */
8479 if (it->hpos == 0 && it->method == GET_FROM_BUFFER
8480 && it->current_y < it->last_visible_y)
8481 handle_line_prefix (it);
8483 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8484 SET_TEXT_POS (this_line_min_pos, IT_CHARPOS (*it), IT_BYTEPOS (*it));
8486 while (1)
8488 int x, i, ascent = 0, descent = 0;
8490 /* Utility macro to reset an iterator with x, ascent, and descent. */
8491 #define IT_RESET_X_ASCENT_DESCENT(IT) \
8492 ((IT)->current_x = x, (IT)->max_ascent = ascent, \
8493 (IT)->max_descent = descent)
8495 /* Stop if we move beyond TO_CHARPOS (after an image or a
8496 display string or stretch glyph). */
8497 if ((op & MOVE_TO_POS) != 0
8498 && BUFFERP (it->object)
8499 && it->method == GET_FROM_BUFFER
8500 && (((!it->bidi_p
8501 /* When the iterator is at base embedding level, we
8502 are guaranteed that characters are delivered for
8503 display in strictly increasing order of their
8504 buffer positions. */
8505 || BIDI_AT_BASE_LEVEL (it->bidi_it))
8506 && IT_CHARPOS (*it) > to_charpos)
8507 || (it->bidi_p
8508 && (prev_method == GET_FROM_IMAGE
8509 || prev_method == GET_FROM_STRETCH
8510 || prev_method == GET_FROM_STRING)
8511 /* Passed TO_CHARPOS from left to right. */
8512 && ((prev_pos < to_charpos
8513 && IT_CHARPOS (*it) > to_charpos)
8514 /* Passed TO_CHARPOS from right to left. */
8515 || (prev_pos > to_charpos
8516 && IT_CHARPOS (*it) < to_charpos)))))
8518 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8520 result = MOVE_POS_MATCH_OR_ZV;
8521 break;
8523 else if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
8524 /* If wrap_it is valid, the current position might be in a
8525 word that is wrapped. So, save the iterator in
8526 atpos_it and continue to see if wrapping happens. */
8527 SAVE_IT (atpos_it, *it, atpos_data);
8530 /* Stop when ZV reached.
8531 We used to stop here when TO_CHARPOS reached as well, but that is
8532 too soon if this glyph does not fit on this line. So we handle it
8533 explicitly below. */
8534 if (!get_next_display_element (it))
8536 result = MOVE_POS_MATCH_OR_ZV;
8537 break;
8540 if (it->line_wrap == TRUNCATE)
8542 if (BUFFER_POS_REACHED_P ())
8544 result = MOVE_POS_MATCH_OR_ZV;
8545 break;
8548 else
8550 if (it->line_wrap == WORD_WRAP && it->area == TEXT_AREA)
8552 if (IT_DISPLAYING_WHITESPACE (it))
8553 may_wrap = 1;
8554 else if (may_wrap)
8556 /* We have reached a glyph that follows one or more
8557 whitespace characters. If the position is
8558 already found, we are done. */
8559 if (atpos_it.sp >= 0)
8561 RESTORE_IT (it, &atpos_it, atpos_data);
8562 result = MOVE_POS_MATCH_OR_ZV;
8563 goto done;
8565 if (atx_it.sp >= 0)
8567 RESTORE_IT (it, &atx_it, atx_data);
8568 result = MOVE_X_REACHED;
8569 goto done;
8571 /* Otherwise, we can wrap here. */
8572 SAVE_IT (wrap_it, *it, wrap_data);
8573 may_wrap = 0;
8578 /* Remember the line height for the current line, in case
8579 the next element doesn't fit on the line. */
8580 ascent = it->max_ascent;
8581 descent = it->max_descent;
8583 /* The call to produce_glyphs will get the metrics of the
8584 display element IT is loaded with. Record the x-position
8585 before this display element, in case it doesn't fit on the
8586 line. */
8587 x = it->current_x;
8589 PRODUCE_GLYPHS (it);
8591 if (it->area != TEXT_AREA)
8593 prev_method = it->method;
8594 if (it->method == GET_FROM_BUFFER)
8595 prev_pos = IT_CHARPOS (*it);
8596 set_iterator_to_next (it, 1);
8597 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8598 SET_TEXT_POS (this_line_min_pos,
8599 IT_CHARPOS (*it), IT_BYTEPOS (*it));
8600 if (it->bidi_p
8601 && (op & MOVE_TO_POS)
8602 && IT_CHARPOS (*it) > to_charpos
8603 && IT_CHARPOS (*it) < closest_pos)
8604 closest_pos = IT_CHARPOS (*it);
8605 continue;
8608 /* The number of glyphs we get back in IT->nglyphs will normally
8609 be 1 except when IT->c is (i) a TAB, or (ii) a multi-glyph
8610 character on a terminal frame, or (iii) a line end. For the
8611 second case, IT->nglyphs - 1 padding glyphs will be present.
8612 (On X frames, there is only one glyph produced for a
8613 composite character.)
8615 The behavior implemented below means, for continuation lines,
8616 that as many spaces of a TAB as fit on the current line are
8617 displayed there. For terminal frames, as many glyphs of a
8618 multi-glyph character are displayed in the current line, too.
8619 This is what the old redisplay code did, and we keep it that
8620 way. Under X, the whole shape of a complex character must
8621 fit on the line or it will be completely displayed in the
8622 next line.
8624 Note that both for tabs and padding glyphs, all glyphs have
8625 the same width. */
8626 if (it->nglyphs)
8628 /* More than one glyph or glyph doesn't fit on line. All
8629 glyphs have the same width. */
8630 int single_glyph_width = it->pixel_width / it->nglyphs;
8631 int new_x;
8632 int x_before_this_char = x;
8633 int hpos_before_this_char = it->hpos;
8635 for (i = 0; i < it->nglyphs; ++i, x = new_x)
8637 new_x = x + single_glyph_width;
8639 /* We want to leave anything reaching TO_X to the caller. */
8640 if ((op & MOVE_TO_X) && new_x > to_x)
8642 if (BUFFER_POS_REACHED_P ())
8644 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8645 goto buffer_pos_reached;
8646 if (atpos_it.sp < 0)
8648 SAVE_IT (atpos_it, *it, atpos_data);
8649 IT_RESET_X_ASCENT_DESCENT (&atpos_it);
8652 else
8654 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8656 it->current_x = x;
8657 result = MOVE_X_REACHED;
8658 break;
8660 if (atx_it.sp < 0)
8662 SAVE_IT (atx_it, *it, atx_data);
8663 IT_RESET_X_ASCENT_DESCENT (&atx_it);
8668 if (/* Lines are continued. */
8669 it->line_wrap != TRUNCATE
8670 && (/* And glyph doesn't fit on the line. */
8671 new_x > it->last_visible_x
8672 /* Or it fits exactly and we're on a window
8673 system frame. */
8674 || (new_x == it->last_visible_x
8675 && FRAME_WINDOW_P (it->f)
8676 && ((it->bidi_p && it->bidi_it.paragraph_dir == R2L)
8677 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
8678 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)))))
8680 if (/* IT->hpos == 0 means the very first glyph
8681 doesn't fit on the line, e.g. a wide image. */
8682 it->hpos == 0
8683 || (new_x == it->last_visible_x
8684 && FRAME_WINDOW_P (it->f)))
8686 ++it->hpos;
8687 it->current_x = new_x;
8689 /* The character's last glyph just barely fits
8690 in this row. */
8691 if (i == it->nglyphs - 1)
8693 /* If this is the destination position,
8694 return a position *before* it in this row,
8695 now that we know it fits in this row. */
8696 if (BUFFER_POS_REACHED_P ())
8698 if (it->line_wrap != WORD_WRAP
8699 || wrap_it.sp < 0)
8701 it->hpos = hpos_before_this_char;
8702 it->current_x = x_before_this_char;
8703 result = MOVE_POS_MATCH_OR_ZV;
8704 break;
8706 if (it->line_wrap == WORD_WRAP
8707 && atpos_it.sp < 0)
8709 SAVE_IT (atpos_it, *it, atpos_data);
8710 atpos_it.current_x = x_before_this_char;
8711 atpos_it.hpos = hpos_before_this_char;
8715 prev_method = it->method;
8716 if (it->method == GET_FROM_BUFFER)
8717 prev_pos = IT_CHARPOS (*it);
8718 set_iterator_to_next (it, 1);
8719 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8720 SET_TEXT_POS (this_line_min_pos,
8721 IT_CHARPOS (*it), IT_BYTEPOS (*it));
8722 /* On graphical terminals, newlines may
8723 "overflow" into the fringe if
8724 overflow-newline-into-fringe is non-nil.
8725 On text terminals, and on graphical
8726 terminals with no right margin, newlines
8727 may overflow into the last glyph on the
8728 display line.*/
8729 if (!FRAME_WINDOW_P (it->f)
8730 || ((it->bidi_p
8731 && it->bidi_it.paragraph_dir == R2L)
8732 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
8733 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0
8734 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
8736 if (!get_next_display_element (it))
8738 result = MOVE_POS_MATCH_OR_ZV;
8739 break;
8741 if (BUFFER_POS_REACHED_P ())
8743 if (ITERATOR_AT_END_OF_LINE_P (it))
8744 result = MOVE_POS_MATCH_OR_ZV;
8745 else
8746 result = MOVE_LINE_CONTINUED;
8747 break;
8749 if (ITERATOR_AT_END_OF_LINE_P (it)
8750 && (it->line_wrap != WORD_WRAP
8751 || wrap_it.sp < 0
8752 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it)))
8754 result = MOVE_NEWLINE_OR_CR;
8755 break;
8760 else
8761 IT_RESET_X_ASCENT_DESCENT (it);
8763 if (wrap_it.sp >= 0)
8765 RESTORE_IT (it, &wrap_it, wrap_data);
8766 atpos_it.sp = -1;
8767 atx_it.sp = -1;
8770 TRACE_MOVE ((stderr, "move_it_in: continued at %d\n",
8771 IT_CHARPOS (*it)));
8772 result = MOVE_LINE_CONTINUED;
8773 break;
8776 if (BUFFER_POS_REACHED_P ())
8778 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8779 goto buffer_pos_reached;
8780 if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
8782 SAVE_IT (atpos_it, *it, atpos_data);
8783 IT_RESET_X_ASCENT_DESCENT (&atpos_it);
8787 if (new_x > it->first_visible_x)
8789 /* Glyph is visible. Increment number of glyphs that
8790 would be displayed. */
8791 ++it->hpos;
8795 if (result != MOVE_UNDEFINED)
8796 break;
8798 else if (BUFFER_POS_REACHED_P ())
8800 buffer_pos_reached:
8801 IT_RESET_X_ASCENT_DESCENT (it);
8802 result = MOVE_POS_MATCH_OR_ZV;
8803 break;
8805 else if ((op & MOVE_TO_X) && it->current_x >= to_x)
8807 /* Stop when TO_X specified and reached. This check is
8808 necessary here because of lines consisting of a line end,
8809 only. The line end will not produce any glyphs and we
8810 would never get MOVE_X_REACHED. */
8811 eassert (it->nglyphs == 0);
8812 result = MOVE_X_REACHED;
8813 break;
8816 /* Is this a line end? If yes, we're done. */
8817 if (ITERATOR_AT_END_OF_LINE_P (it))
8819 /* If we are past TO_CHARPOS, but never saw any character
8820 positions smaller than TO_CHARPOS, return
8821 MOVE_POS_MATCH_OR_ZV, like the unidirectional display
8822 did. */
8823 if (it->bidi_p && (op & MOVE_TO_POS) != 0)
8825 if (!saw_smaller_pos && IT_CHARPOS (*it) > to_charpos)
8827 if (closest_pos < ZV)
8829 RESTORE_IT (it, &ppos_it, ppos_data);
8830 /* Don't recurse if closest_pos is equal to
8831 to_charpos, since we have just tried that. */
8832 if (closest_pos != to_charpos)
8833 move_it_in_display_line_to (it, closest_pos, -1,
8834 MOVE_TO_POS);
8835 result = MOVE_POS_MATCH_OR_ZV;
8837 else
8838 goto buffer_pos_reached;
8840 else if (it->line_wrap == WORD_WRAP && atpos_it.sp >= 0
8841 && IT_CHARPOS (*it) > to_charpos)
8842 goto buffer_pos_reached;
8843 else
8844 result = MOVE_NEWLINE_OR_CR;
8846 else
8847 result = MOVE_NEWLINE_OR_CR;
8848 break;
8851 prev_method = it->method;
8852 if (it->method == GET_FROM_BUFFER)
8853 prev_pos = IT_CHARPOS (*it);
8854 /* The current display element has been consumed. Advance
8855 to the next. */
8856 set_iterator_to_next (it, 1);
8857 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8858 SET_TEXT_POS (this_line_min_pos, IT_CHARPOS (*it), IT_BYTEPOS (*it));
8859 if (IT_CHARPOS (*it) < to_charpos)
8860 saw_smaller_pos = 1;
8861 if (it->bidi_p
8862 && (op & MOVE_TO_POS)
8863 && IT_CHARPOS (*it) >= to_charpos
8864 && IT_CHARPOS (*it) < closest_pos)
8865 closest_pos = IT_CHARPOS (*it);
8867 /* Stop if lines are truncated and IT's current x-position is
8868 past the right edge of the window now. */
8869 if (it->line_wrap == TRUNCATE
8870 && it->current_x >= it->last_visible_x)
8872 if (!FRAME_WINDOW_P (it->f)
8873 || ((it->bidi_p && it->bidi_it.paragraph_dir == R2L)
8874 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
8875 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0
8876 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
8878 int at_eob_p = 0;
8880 if ((at_eob_p = !get_next_display_element (it))
8881 || BUFFER_POS_REACHED_P ()
8882 /* If we are past TO_CHARPOS, but never saw any
8883 character positions smaller than TO_CHARPOS,
8884 return MOVE_POS_MATCH_OR_ZV, like the
8885 unidirectional display did. */
8886 || (it->bidi_p && (op & MOVE_TO_POS) != 0
8887 && !saw_smaller_pos
8888 && IT_CHARPOS (*it) > to_charpos))
8890 if (it->bidi_p
8891 && !BUFFER_POS_REACHED_P ()
8892 && !at_eob_p && closest_pos < ZV)
8894 RESTORE_IT (it, &ppos_it, ppos_data);
8895 if (closest_pos != to_charpos)
8896 move_it_in_display_line_to (it, closest_pos, -1,
8897 MOVE_TO_POS);
8899 result = MOVE_POS_MATCH_OR_ZV;
8900 break;
8902 if (ITERATOR_AT_END_OF_LINE_P (it))
8904 result = MOVE_NEWLINE_OR_CR;
8905 break;
8908 else if (it->bidi_p && (op & MOVE_TO_POS) != 0
8909 && !saw_smaller_pos
8910 && IT_CHARPOS (*it) > to_charpos)
8912 if (closest_pos < ZV)
8914 RESTORE_IT (it, &ppos_it, ppos_data);
8915 if (closest_pos != to_charpos)
8916 move_it_in_display_line_to (it, closest_pos, -1,
8917 MOVE_TO_POS);
8919 result = MOVE_POS_MATCH_OR_ZV;
8920 break;
8922 result = MOVE_LINE_TRUNCATED;
8923 break;
8925 #undef IT_RESET_X_ASCENT_DESCENT
8928 #undef BUFFER_POS_REACHED_P
8930 /* If we scanned beyond to_pos and didn't find a point to wrap at,
8931 restore the saved iterator. */
8932 if (atpos_it.sp >= 0)
8933 RESTORE_IT (it, &atpos_it, atpos_data);
8934 else if (atx_it.sp >= 0)
8935 RESTORE_IT (it, &atx_it, atx_data);
8937 done:
8939 if (atpos_data)
8940 bidi_unshelve_cache (atpos_data, 1);
8941 if (atx_data)
8942 bidi_unshelve_cache (atx_data, 1);
8943 if (wrap_data)
8944 bidi_unshelve_cache (wrap_data, 1);
8945 if (ppos_data)
8946 bidi_unshelve_cache (ppos_data, 1);
8948 /* Restore the iterator settings altered at the beginning of this
8949 function. */
8950 it->glyph_row = saved_glyph_row;
8951 return result;
8954 /* For external use. */
8955 void
8956 move_it_in_display_line (struct it *it,
8957 ptrdiff_t to_charpos, int to_x,
8958 enum move_operation_enum op)
8960 if (it->line_wrap == WORD_WRAP
8961 && (op & MOVE_TO_X))
8963 struct it save_it;
8964 void *save_data = NULL;
8965 int skip;
8967 SAVE_IT (save_it, *it, save_data);
8968 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
8969 /* When word-wrap is on, TO_X may lie past the end
8970 of a wrapped line. Then it->current is the
8971 character on the next line, so backtrack to the
8972 space before the wrap point. */
8973 if (skip == MOVE_LINE_CONTINUED)
8975 int prev_x = max (it->current_x - 1, 0);
8976 RESTORE_IT (it, &save_it, save_data);
8977 move_it_in_display_line_to
8978 (it, -1, prev_x, MOVE_TO_X);
8980 else
8981 bidi_unshelve_cache (save_data, 1);
8983 else
8984 move_it_in_display_line_to (it, to_charpos, to_x, op);
8988 /* Move IT forward until it satisfies one or more of the criteria in
8989 TO_CHARPOS, TO_X, TO_Y, and TO_VPOS.
8991 OP is a bit-mask that specifies where to stop, and in particular,
8992 which of those four position arguments makes a difference. See the
8993 description of enum move_operation_enum.
8995 If TO_CHARPOS is in invisible text, e.g. a truncated part of a
8996 screen line, this function will set IT to the next position that is
8997 displayed to the right of TO_CHARPOS on the screen.
8999 Return the maximum pixel length of any line scanned but never more
9000 than it.last_visible_x. */
9003 move_it_to (struct it *it, ptrdiff_t to_charpos, int to_x, int to_y, int to_vpos, int op)
9005 enum move_it_result skip, skip2 = MOVE_X_REACHED;
9006 int line_height, line_start_x = 0, reached = 0;
9007 int max_current_x = 0;
9008 void *backup_data = NULL;
9010 for (;;)
9012 if (op & MOVE_TO_VPOS)
9014 /* If no TO_CHARPOS and no TO_X specified, stop at the
9015 start of the line TO_VPOS. */
9016 if ((op & (MOVE_TO_X | MOVE_TO_POS)) == 0)
9018 if (it->vpos == to_vpos)
9020 reached = 1;
9021 break;
9023 else
9024 skip = move_it_in_display_line_to (it, -1, -1, 0);
9026 else
9028 /* TO_VPOS >= 0 means stop at TO_X in the line at
9029 TO_VPOS, or at TO_POS, whichever comes first. */
9030 if (it->vpos == to_vpos)
9032 reached = 2;
9033 break;
9036 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
9038 if (skip == MOVE_POS_MATCH_OR_ZV || it->vpos == to_vpos)
9040 reached = 3;
9041 break;
9043 else if (skip == MOVE_X_REACHED && it->vpos != to_vpos)
9045 /* We have reached TO_X but not in the line we want. */
9046 skip = move_it_in_display_line_to (it, to_charpos,
9047 -1, MOVE_TO_POS);
9048 if (skip == MOVE_POS_MATCH_OR_ZV)
9050 reached = 4;
9051 break;
9056 else if (op & MOVE_TO_Y)
9058 struct it it_backup;
9060 if (it->line_wrap == WORD_WRAP)
9061 SAVE_IT (it_backup, *it, backup_data);
9063 /* TO_Y specified means stop at TO_X in the line containing
9064 TO_Y---or at TO_CHARPOS if this is reached first. The
9065 problem is that we can't really tell whether the line
9066 contains TO_Y before we have completely scanned it, and
9067 this may skip past TO_X. What we do is to first scan to
9068 TO_X.
9070 If TO_X is not specified, use a TO_X of zero. The reason
9071 is to make the outcome of this function more predictable.
9072 If we didn't use TO_X == 0, we would stop at the end of
9073 the line which is probably not what a caller would expect
9074 to happen. */
9075 skip = move_it_in_display_line_to
9076 (it, to_charpos, ((op & MOVE_TO_X) ? to_x : 0),
9077 (MOVE_TO_X | (op & MOVE_TO_POS)));
9079 /* If TO_CHARPOS is reached or ZV, we don't have to do more. */
9080 if (skip == MOVE_POS_MATCH_OR_ZV)
9081 reached = 5;
9082 else if (skip == MOVE_X_REACHED)
9084 /* If TO_X was reached, we want to know whether TO_Y is
9085 in the line. We know this is the case if the already
9086 scanned glyphs make the line tall enough. Otherwise,
9087 we must check by scanning the rest of the line. */
9088 line_height = it->max_ascent + it->max_descent;
9089 if (to_y >= it->current_y
9090 && to_y < it->current_y + line_height)
9092 reached = 6;
9093 break;
9095 SAVE_IT (it_backup, *it, backup_data);
9096 TRACE_MOVE ((stderr, "move_it: from %d\n", IT_CHARPOS (*it)));
9097 skip2 = move_it_in_display_line_to (it, to_charpos, -1,
9098 op & MOVE_TO_POS);
9099 TRACE_MOVE ((stderr, "move_it: to %d\n", IT_CHARPOS (*it)));
9100 line_height = it->max_ascent + it->max_descent;
9101 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
9103 if (to_y >= it->current_y
9104 && to_y < it->current_y + line_height)
9106 /* If TO_Y is in this line and TO_X was reached
9107 above, we scanned too far. We have to restore
9108 IT's settings to the ones before skipping. But
9109 keep the more accurate values of max_ascent and
9110 max_descent we've found while skipping the rest
9111 of the line, for the sake of callers, such as
9112 pos_visible_p, that need to know the line
9113 height. */
9114 int max_ascent = it->max_ascent;
9115 int max_descent = it->max_descent;
9117 RESTORE_IT (it, &it_backup, backup_data);
9118 it->max_ascent = max_ascent;
9119 it->max_descent = max_descent;
9120 reached = 6;
9122 else
9124 skip = skip2;
9125 if (skip == MOVE_POS_MATCH_OR_ZV)
9126 reached = 7;
9129 else
9131 /* Check whether TO_Y is in this line. */
9132 line_height = it->max_ascent + it->max_descent;
9133 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
9135 if (to_y >= it->current_y
9136 && to_y < it->current_y + line_height)
9138 if (to_y > it->current_y)
9139 max_current_x = max (it->current_x, max_current_x);
9141 /* When word-wrap is on, TO_X may lie past the end
9142 of a wrapped line. Then it->current is the
9143 character on the next line, so backtrack to the
9144 space before the wrap point. */
9145 if (skip == MOVE_LINE_CONTINUED
9146 && it->line_wrap == WORD_WRAP)
9148 int prev_x = max (it->current_x - 1, 0);
9149 RESTORE_IT (it, &it_backup, backup_data);
9150 skip = move_it_in_display_line_to
9151 (it, -1, prev_x, MOVE_TO_X);
9154 reached = 6;
9158 if (reached)
9160 max_current_x = max (it->current_x, max_current_x);
9161 break;
9164 else if (BUFFERP (it->object)
9165 && (it->method == GET_FROM_BUFFER
9166 || it->method == GET_FROM_STRETCH)
9167 && IT_CHARPOS (*it) >= to_charpos
9168 /* Under bidi iteration, a call to set_iterator_to_next
9169 can scan far beyond to_charpos if the initial
9170 portion of the next line needs to be reordered. In
9171 that case, give move_it_in_display_line_to another
9172 chance below. */
9173 && !(it->bidi_p
9174 && it->bidi_it.scan_dir == -1))
9175 skip = MOVE_POS_MATCH_OR_ZV;
9176 else
9177 skip = move_it_in_display_line_to (it, to_charpos, -1, MOVE_TO_POS);
9179 switch (skip)
9181 case MOVE_POS_MATCH_OR_ZV:
9182 max_current_x = max (it->current_x, max_current_x);
9183 reached = 8;
9184 goto out;
9186 case MOVE_NEWLINE_OR_CR:
9187 max_current_x = max (it->current_x, max_current_x);
9188 set_iterator_to_next (it, 1);
9189 it->continuation_lines_width = 0;
9190 break;
9192 case MOVE_LINE_TRUNCATED:
9193 max_current_x = it->last_visible_x;
9194 it->continuation_lines_width = 0;
9195 reseat_at_next_visible_line_start (it, 0);
9196 if ((op & MOVE_TO_POS) != 0
9197 && IT_CHARPOS (*it) > to_charpos)
9199 reached = 9;
9200 goto out;
9202 break;
9204 case MOVE_LINE_CONTINUED:
9205 max_current_x = it->last_visible_x;
9206 /* For continued lines ending in a tab, some of the glyphs
9207 associated with the tab are displayed on the current
9208 line. Since it->current_x does not include these glyphs,
9209 we use it->last_visible_x instead. */
9210 if (it->c == '\t')
9212 it->continuation_lines_width += it->last_visible_x;
9213 /* When moving by vpos, ensure that the iterator really
9214 advances to the next line (bug#847, bug#969). Fixme:
9215 do we need to do this in other circumstances? */
9216 if (it->current_x != it->last_visible_x
9217 && (op & MOVE_TO_VPOS)
9218 && !(op & (MOVE_TO_X | MOVE_TO_POS)))
9220 line_start_x = it->current_x + it->pixel_width
9221 - it->last_visible_x;
9222 if (FRAME_WINDOW_P (it->f))
9224 struct face *face = FACE_FROM_ID (it->f, it->face_id);
9225 struct font *face_font = face->font;
9227 /* When display_line produces a continued line
9228 that ends in a TAB, it skips a tab stop that
9229 is closer than the font's space character
9230 width (see x_produce_glyphs where it produces
9231 the stretch glyph which represents a TAB).
9232 We need to reproduce the same logic here. */
9233 eassert (face_font);
9234 if (face_font)
9236 if (line_start_x < face_font->space_width)
9237 line_start_x
9238 += it->tab_width * face_font->space_width;
9241 set_iterator_to_next (it, 0);
9244 else
9245 it->continuation_lines_width += it->current_x;
9246 break;
9248 default:
9249 emacs_abort ();
9252 /* Reset/increment for the next run. */
9253 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
9254 it->current_x = line_start_x;
9255 line_start_x = 0;
9256 it->hpos = 0;
9257 it->current_y += it->max_ascent + it->max_descent;
9258 ++it->vpos;
9259 last_height = it->max_ascent + it->max_descent;
9260 it->max_ascent = it->max_descent = 0;
9263 out:
9265 /* On text terminals, we may stop at the end of a line in the middle
9266 of a multi-character glyph. If the glyph itself is continued,
9267 i.e. it is actually displayed on the next line, don't treat this
9268 stopping point as valid; move to the next line instead (unless
9269 that brings us offscreen). */
9270 if (!FRAME_WINDOW_P (it->f)
9271 && op & MOVE_TO_POS
9272 && IT_CHARPOS (*it) == to_charpos
9273 && it->what == IT_CHARACTER
9274 && it->nglyphs > 1
9275 && it->line_wrap == WINDOW_WRAP
9276 && it->current_x == it->last_visible_x - 1
9277 && it->c != '\n'
9278 && it->c != '\t'
9279 && it->vpos < it->w->window_end_vpos)
9281 it->continuation_lines_width += it->current_x;
9282 it->current_x = it->hpos = it->max_ascent = it->max_descent = 0;
9283 it->current_y += it->max_ascent + it->max_descent;
9284 ++it->vpos;
9285 last_height = it->max_ascent + it->max_descent;
9288 if (backup_data)
9289 bidi_unshelve_cache (backup_data, 1);
9291 TRACE_MOVE ((stderr, "move_it_to: reached %d\n", reached));
9293 return max_current_x;
9297 /* Move iterator IT backward by a specified y-distance DY, DY >= 0.
9299 If DY > 0, move IT backward at least that many pixels. DY = 0
9300 means move IT backward to the preceding line start or BEGV. This
9301 function may move over more than DY pixels if IT->current_y - DY
9302 ends up in the middle of a line; in this case IT->current_y will be
9303 set to the top of the line moved to. */
9305 void
9306 move_it_vertically_backward (struct it *it, int dy)
9308 int nlines, h;
9309 struct it it2, it3;
9310 void *it2data = NULL, *it3data = NULL;
9311 ptrdiff_t start_pos;
9312 int nchars_per_row
9313 = (it->last_visible_x - it->first_visible_x) / FRAME_COLUMN_WIDTH (it->f);
9314 ptrdiff_t pos_limit;
9316 move_further_back:
9317 eassert (dy >= 0);
9319 start_pos = IT_CHARPOS (*it);
9321 /* Estimate how many newlines we must move back. */
9322 nlines = max (1, dy / default_line_pixel_height (it->w));
9323 if (it->line_wrap == TRUNCATE || nchars_per_row == 0)
9324 pos_limit = BEGV;
9325 else
9326 pos_limit = max (start_pos - nlines * nchars_per_row, BEGV);
9328 /* Set the iterator's position that many lines back. But don't go
9329 back more than NLINES full screen lines -- this wins a day with
9330 buffers which have very long lines. */
9331 while (nlines-- && IT_CHARPOS (*it) > pos_limit)
9332 back_to_previous_visible_line_start (it);
9334 /* Reseat the iterator here. When moving backward, we don't want
9335 reseat to skip forward over invisible text, set up the iterator
9336 to deliver from overlay strings at the new position etc. So,
9337 use reseat_1 here. */
9338 reseat_1 (it, it->current.pos, 1);
9340 /* We are now surely at a line start. */
9341 it->current_x = it->hpos = 0; /* FIXME: this is incorrect when bidi
9342 reordering is in effect. */
9343 it->continuation_lines_width = 0;
9345 /* Move forward and see what y-distance we moved. First move to the
9346 start of the next line so that we get its height. We need this
9347 height to be able to tell whether we reached the specified
9348 y-distance. */
9349 SAVE_IT (it2, *it, it2data);
9350 it2.max_ascent = it2.max_descent = 0;
9353 move_it_to (&it2, start_pos, -1, -1, it2.vpos + 1,
9354 MOVE_TO_POS | MOVE_TO_VPOS);
9356 while (!(IT_POS_VALID_AFTER_MOVE_P (&it2)
9357 /* If we are in a display string which starts at START_POS,
9358 and that display string includes a newline, and we are
9359 right after that newline (i.e. at the beginning of a
9360 display line), exit the loop, because otherwise we will
9361 infloop, since move_it_to will see that it is already at
9362 START_POS and will not move. */
9363 || (it2.method == GET_FROM_STRING
9364 && IT_CHARPOS (it2) == start_pos
9365 && SREF (it2.string, IT_STRING_BYTEPOS (it2) - 1) == '\n')));
9366 eassert (IT_CHARPOS (*it) >= BEGV);
9367 SAVE_IT (it3, it2, it3data);
9369 move_it_to (&it2, start_pos, -1, -1, -1, MOVE_TO_POS);
9370 eassert (IT_CHARPOS (*it) >= BEGV);
9371 /* H is the actual vertical distance from the position in *IT
9372 and the starting position. */
9373 h = it2.current_y - it->current_y;
9374 /* NLINES is the distance in number of lines. */
9375 nlines = it2.vpos - it->vpos;
9377 /* Correct IT's y and vpos position
9378 so that they are relative to the starting point. */
9379 it->vpos -= nlines;
9380 it->current_y -= h;
9382 if (dy == 0)
9384 /* DY == 0 means move to the start of the screen line. The
9385 value of nlines is > 0 if continuation lines were involved,
9386 or if the original IT position was at start of a line. */
9387 RESTORE_IT (it, it, it2data);
9388 if (nlines > 0)
9389 move_it_by_lines (it, nlines);
9390 /* The above code moves us to some position NLINES down,
9391 usually to its first glyph (leftmost in an L2R line), but
9392 that's not necessarily the start of the line, under bidi
9393 reordering. We want to get to the character position
9394 that is immediately after the newline of the previous
9395 line. */
9396 if (it->bidi_p
9397 && !it->continuation_lines_width
9398 && !STRINGP (it->string)
9399 && IT_CHARPOS (*it) > BEGV
9400 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
9402 ptrdiff_t cp = IT_CHARPOS (*it), bp = IT_BYTEPOS (*it);
9404 DEC_BOTH (cp, bp);
9405 cp = find_newline_no_quit (cp, bp, -1, NULL);
9406 move_it_to (it, cp, -1, -1, -1, MOVE_TO_POS);
9408 bidi_unshelve_cache (it3data, 1);
9410 else
9412 /* The y-position we try to reach, relative to *IT.
9413 Note that H has been subtracted in front of the if-statement. */
9414 int target_y = it->current_y + h - dy;
9415 int y0 = it3.current_y;
9416 int y1;
9417 int line_height;
9419 RESTORE_IT (&it3, &it3, it3data);
9420 y1 = line_bottom_y (&it3);
9421 line_height = y1 - y0;
9422 RESTORE_IT (it, it, it2data);
9423 /* If we did not reach target_y, try to move further backward if
9424 we can. If we moved too far backward, try to move forward. */
9425 if (target_y < it->current_y
9426 /* This is heuristic. In a window that's 3 lines high, with
9427 a line height of 13 pixels each, recentering with point
9428 on the bottom line will try to move -39/2 = 19 pixels
9429 backward. Try to avoid moving into the first line. */
9430 && (it->current_y - target_y
9431 > min (window_box_height (it->w), line_height * 2 / 3))
9432 && IT_CHARPOS (*it) > BEGV)
9434 TRACE_MOVE ((stderr, " not far enough -> move_vert %d\n",
9435 target_y - it->current_y));
9436 dy = it->current_y - target_y;
9437 goto move_further_back;
9439 else if (target_y >= it->current_y + line_height
9440 && IT_CHARPOS (*it) < ZV)
9442 /* Should move forward by at least one line, maybe more.
9444 Note: Calling move_it_by_lines can be expensive on
9445 terminal frames, where compute_motion is used (via
9446 vmotion) to do the job, when there are very long lines
9447 and truncate-lines is nil. That's the reason for
9448 treating terminal frames specially here. */
9450 if (!FRAME_WINDOW_P (it->f))
9451 move_it_vertically (it, target_y - (it->current_y + line_height));
9452 else
9456 move_it_by_lines (it, 1);
9458 while (target_y >= line_bottom_y (it) && IT_CHARPOS (*it) < ZV);
9465 /* Move IT by a specified amount of pixel lines DY. DY negative means
9466 move backwards. DY = 0 means move to start of screen line. At the
9467 end, IT will be on the start of a screen line. */
9469 void
9470 move_it_vertically (struct it *it, int dy)
9472 if (dy <= 0)
9473 move_it_vertically_backward (it, -dy);
9474 else
9476 TRACE_MOVE ((stderr, "move_it_v: from %d, %d\n", IT_CHARPOS (*it), dy));
9477 move_it_to (it, ZV, -1, it->current_y + dy, -1,
9478 MOVE_TO_POS | MOVE_TO_Y);
9479 TRACE_MOVE ((stderr, "move_it_v: to %d\n", IT_CHARPOS (*it)));
9481 /* If buffer ends in ZV without a newline, move to the start of
9482 the line to satisfy the post-condition. */
9483 if (IT_CHARPOS (*it) == ZV
9484 && ZV > BEGV
9485 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
9486 move_it_by_lines (it, 0);
9491 /* Move iterator IT past the end of the text line it is in. */
9493 void
9494 move_it_past_eol (struct it *it)
9496 enum move_it_result rc;
9498 rc = move_it_in_display_line_to (it, Z, 0, MOVE_TO_POS);
9499 if (rc == MOVE_NEWLINE_OR_CR)
9500 set_iterator_to_next (it, 0);
9504 /* Move IT by a specified number DVPOS of screen lines down. DVPOS
9505 negative means move up. DVPOS == 0 means move to the start of the
9506 screen line.
9508 Optimization idea: If we would know that IT->f doesn't use
9509 a face with proportional font, we could be faster for
9510 truncate-lines nil. */
9512 void
9513 move_it_by_lines (struct it *it, ptrdiff_t dvpos)
9516 /* The commented-out optimization uses vmotion on terminals. This
9517 gives bad results, because elements like it->what, on which
9518 callers such as pos_visible_p rely, aren't updated. */
9519 /* struct position pos;
9520 if (!FRAME_WINDOW_P (it->f))
9522 struct text_pos textpos;
9524 pos = *vmotion (IT_CHARPOS (*it), dvpos, it->w);
9525 SET_TEXT_POS (textpos, pos.bufpos, pos.bytepos);
9526 reseat (it, textpos, 1);
9527 it->vpos += pos.vpos;
9528 it->current_y += pos.vpos;
9530 else */
9532 if (dvpos == 0)
9534 /* DVPOS == 0 means move to the start of the screen line. */
9535 move_it_vertically_backward (it, 0);
9536 /* Let next call to line_bottom_y calculate real line height. */
9537 last_height = 0;
9539 else if (dvpos > 0)
9541 move_it_to (it, -1, -1, -1, it->vpos + dvpos, MOVE_TO_VPOS);
9542 if (!IT_POS_VALID_AFTER_MOVE_P (it))
9544 /* Only move to the next buffer position if we ended up in a
9545 string from display property, not in an overlay string
9546 (before-string or after-string). That is because the
9547 latter don't conceal the underlying buffer position, so
9548 we can ask to move the iterator to the exact position we
9549 are interested in. Note that, even if we are already at
9550 IT_CHARPOS (*it), the call below is not a no-op, as it
9551 will detect that we are at the end of the string, pop the
9552 iterator, and compute it->current_x and it->hpos
9553 correctly. */
9554 move_it_to (it, IT_CHARPOS (*it) + it->string_from_display_prop_p,
9555 -1, -1, -1, MOVE_TO_POS);
9558 else
9560 struct it it2;
9561 void *it2data = NULL;
9562 ptrdiff_t start_charpos, i;
9563 int nchars_per_row
9564 = (it->last_visible_x - it->first_visible_x) / FRAME_COLUMN_WIDTH (it->f);
9565 bool hit_pos_limit = false;
9566 ptrdiff_t pos_limit;
9568 /* Start at the beginning of the screen line containing IT's
9569 position. This may actually move vertically backwards,
9570 in case of overlays, so adjust dvpos accordingly. */
9571 dvpos += it->vpos;
9572 move_it_vertically_backward (it, 0);
9573 dvpos -= it->vpos;
9575 /* Go back -DVPOS buffer lines, but no farther than -DVPOS full
9576 screen lines, and reseat the iterator there. */
9577 start_charpos = IT_CHARPOS (*it);
9578 if (it->line_wrap == TRUNCATE || nchars_per_row == 0)
9579 pos_limit = BEGV;
9580 else
9581 pos_limit = max (start_charpos + dvpos * nchars_per_row, BEGV);
9583 for (i = -dvpos; i > 0 && IT_CHARPOS (*it) > pos_limit; --i)
9584 back_to_previous_visible_line_start (it);
9585 if (i > 0 && IT_CHARPOS (*it) <= pos_limit)
9586 hit_pos_limit = true;
9587 reseat (it, it->current.pos, 1);
9589 /* Move further back if we end up in a string or an image. */
9590 while (!IT_POS_VALID_AFTER_MOVE_P (it))
9592 /* First try to move to start of display line. */
9593 dvpos += it->vpos;
9594 move_it_vertically_backward (it, 0);
9595 dvpos -= it->vpos;
9596 if (IT_POS_VALID_AFTER_MOVE_P (it))
9597 break;
9598 /* If start of line is still in string or image,
9599 move further back. */
9600 back_to_previous_visible_line_start (it);
9601 reseat (it, it->current.pos, 1);
9602 dvpos--;
9605 it->current_x = it->hpos = 0;
9607 /* Above call may have moved too far if continuation lines
9608 are involved. Scan forward and see if it did. */
9609 SAVE_IT (it2, *it, it2data);
9610 it2.vpos = it2.current_y = 0;
9611 move_it_to (&it2, start_charpos, -1, -1, -1, MOVE_TO_POS);
9612 it->vpos -= it2.vpos;
9613 it->current_y -= it2.current_y;
9614 it->current_x = it->hpos = 0;
9616 /* If we moved too far back, move IT some lines forward. */
9617 if (it2.vpos > -dvpos)
9619 int delta = it2.vpos + dvpos;
9621 RESTORE_IT (&it2, &it2, it2data);
9622 SAVE_IT (it2, *it, it2data);
9623 move_it_to (it, -1, -1, -1, it->vpos + delta, MOVE_TO_VPOS);
9624 /* Move back again if we got too far ahead. */
9625 if (IT_CHARPOS (*it) >= start_charpos)
9626 RESTORE_IT (it, &it2, it2data);
9627 else
9628 bidi_unshelve_cache (it2data, 1);
9630 else if (hit_pos_limit && pos_limit > BEGV
9631 && dvpos < 0 && it2.vpos < -dvpos)
9633 /* If we hit the limit, but still didn't make it far enough
9634 back, that means there's a display string with a newline
9635 covering a large chunk of text, and that caused
9636 back_to_previous_visible_line_start try to go too far.
9637 Punish those who commit such atrocities by going back
9638 until we've reached DVPOS, after lifting the limit, which
9639 could make it slow for very long lines. "If it hurts,
9640 don't do that!" */
9641 dvpos += it2.vpos;
9642 RESTORE_IT (it, it, it2data);
9643 for (i = -dvpos; i > 0; --i)
9645 back_to_previous_visible_line_start (it);
9646 it->vpos--;
9648 reseat_1 (it, it->current.pos, 1);
9650 else
9651 RESTORE_IT (it, it, it2data);
9655 /* Return true if IT points into the middle of a display vector. */
9657 bool
9658 in_display_vector_p (struct it *it)
9660 return (it->method == GET_FROM_DISPLAY_VECTOR
9661 && it->current.dpvec_index > 0
9662 && it->dpvec + it->current.dpvec_index != it->dpend);
9665 DEFUN ("window-text-pixel-size", Fwindow_text_pixel_size, Swindow_text_pixel_size, 0, 6, 0,
9666 doc: /* Return the size of the text of WINDOW's buffer in pixels.
9667 WINDOW must be a live window and defaults to the selected one. The
9668 return value is a cons of the maximum pixel-width of any text line and
9669 the maximum pixel-height of all text lines.
9671 The optional argument FROM, if non-nil, specifies the first text
9672 position and defaults to the minimum accessible position of the buffer.
9673 If FROM is t, use the minimum accessible position that is not a newline
9674 character. TO, if non-nil, specifies the last text position and
9675 defaults to the maximum accessible position of the buffer. If TO is t,
9676 use the maximum accessible position that is not a newline character.
9678 The optional argument X-LIMIT, if non-nil, specifies the maximum text
9679 width that can be returned. X-LIMIT nil or omitted, means to use the
9680 pixel-width of WINDOW's body; use this if you do not intend to change
9681 the width of WINDOW. Use the maximum width WINDOW may assume if you
9682 intend to change WINDOW's width. In any case, text whose x-coordinate
9683 is beyond X-LIMIT is ignored. Since calculating the width of long lines
9684 can take some time, it's always a good idea to make this argument as
9685 small as possible; in particular, if the buffer contains long lines that
9686 shall be truncated anyway.
9688 The optional argument Y-LIMIT, if non-nil, specifies the maximum text
9689 height that can be returned. Text lines whose y-coordinate is beyond
9690 Y-LIMIT are ignored. Since calculating the text height of a large
9691 buffer can take some time, it makes sense to specify this argument if
9692 the size of the buffer is unknown.
9694 Optional argument MODE-AND-HEADER-LINE nil or omitted means do not
9695 include the height of the mode- or header-line of WINDOW in the return
9696 value. If it is either the symbol `mode-line' or `header-line', include
9697 only the height of that line, if present, in the return value. If t,
9698 include the height of both, if present, in the return value. */)
9699 (Lisp_Object window, Lisp_Object from, Lisp_Object to, Lisp_Object x_limit, Lisp_Object y_limit,
9700 Lisp_Object mode_and_header_line)
9702 struct window *w = decode_live_window (window);
9703 Lisp_Object buf;
9704 struct buffer *b;
9705 struct it it;
9706 struct buffer *old_buffer = NULL;
9707 ptrdiff_t start, end, pos;
9708 struct text_pos startp;
9709 void *itdata = NULL;
9710 int c, max_y = -1, x = 0, y = 0;
9712 buf = w->contents;
9713 CHECK_BUFFER (buf);
9714 b = XBUFFER (buf);
9716 if (b != current_buffer)
9718 old_buffer = current_buffer;
9719 set_buffer_internal (b);
9722 if (NILP (from))
9723 start = BEGV;
9724 else if (EQ (from, Qt))
9726 start = pos = BEGV;
9727 while ((pos++ < ZV) && (c = FETCH_CHAR (pos))
9728 && (c == ' ' || c == '\t' || c == '\n' || c == '\r'))
9729 start = pos;
9730 while ((pos-- > BEGV) && (c = FETCH_CHAR (pos)) && (c == ' ' || c == '\t'))
9731 start = pos;
9733 else
9735 CHECK_NUMBER_COERCE_MARKER (from);
9736 start = min (max (XINT (from), BEGV), ZV);
9739 if (NILP (to))
9740 end = ZV;
9741 else if (EQ (to, Qt))
9743 end = pos = ZV;
9744 while ((pos-- > BEGV) && (c = FETCH_CHAR (pos))
9745 && (c == ' ' || c == '\t' || c == '\n' || c == '\r'))
9746 end = pos;
9747 while ((pos++ < ZV) && (c = FETCH_CHAR (pos)) && (c == ' ' || c == '\t'))
9748 end = pos;
9750 else
9752 CHECK_NUMBER_COERCE_MARKER (to);
9753 end = max (start, min (XINT (to), ZV));
9756 if (!NILP (y_limit))
9758 CHECK_NUMBER (y_limit);
9759 max_y = min (XINT (y_limit), INT_MAX);
9762 itdata = bidi_shelve_cache ();
9763 SET_TEXT_POS (startp, start, CHAR_TO_BYTE (start));
9764 start_display (&it, w, startp);
9766 if (NILP (x_limit))
9767 x = move_it_to (&it, end, -1, max_y, -1, MOVE_TO_POS | MOVE_TO_Y);
9768 else
9770 CHECK_NUMBER (x_limit);
9771 it.last_visible_x = min (XINT (x_limit), INFINITY);
9772 /* Actually, we never want move_it_to stop at to_x. But to make
9773 sure that move_it_in_display_line_to always moves far enough,
9774 we set it to INT_MAX and specify MOVE_TO_X. */
9775 x = move_it_to (&it, end, INT_MAX, max_y, -1,
9776 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
9779 y = it.current_y + it.max_ascent + it.max_descent;
9781 if (!EQ (mode_and_header_line, Qheader_line)
9782 && !EQ (mode_and_header_line, Qt))
9783 /* Do not count the header-line which was counted automatically by
9784 start_display. */
9785 y = y - WINDOW_HEADER_LINE_HEIGHT (w);
9787 if (EQ (mode_and_header_line, Qmode_line)
9788 || EQ (mode_and_header_line, Qt))
9789 /* Do count the mode-line which is not included automatically by
9790 start_display. */
9791 y = y + WINDOW_MODE_LINE_HEIGHT (w);
9793 bidi_unshelve_cache (itdata, 0);
9795 if (old_buffer)
9796 set_buffer_internal (old_buffer);
9798 return Fcons (make_number (x), make_number (y));
9801 /***********************************************************************
9802 Messages
9803 ***********************************************************************/
9806 /* Add a message with format string FORMAT and arguments ARG1 and ARG2
9807 to *Messages*. */
9809 void
9810 add_to_log (const char *format, Lisp_Object arg1, Lisp_Object arg2)
9812 Lisp_Object args[3];
9813 Lisp_Object msg, fmt;
9814 char *buffer;
9815 ptrdiff_t len;
9816 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
9817 USE_SAFE_ALLOCA;
9819 fmt = msg = Qnil;
9820 GCPRO4 (fmt, msg, arg1, arg2);
9822 args[0] = fmt = build_string (format);
9823 args[1] = arg1;
9824 args[2] = arg2;
9825 msg = Fformat (3, args);
9827 len = SBYTES (msg) + 1;
9828 buffer = SAFE_ALLOCA (len);
9829 memcpy (buffer, SDATA (msg), len);
9831 message_dolog (buffer, len - 1, 1, 0);
9832 SAFE_FREE ();
9834 UNGCPRO;
9838 /* Output a newline in the *Messages* buffer if "needs" one. */
9840 void
9841 message_log_maybe_newline (void)
9843 if (message_log_need_newline)
9844 message_dolog ("", 0, 1, 0);
9848 /* Add a string M of length NBYTES to the message log, optionally
9849 terminated with a newline when NLFLAG is true. MULTIBYTE, if
9850 true, means interpret the contents of M as multibyte. This
9851 function calls low-level routines in order to bypass text property
9852 hooks, etc. which might not be safe to run.
9854 This may GC (insert may run before/after change hooks),
9855 so the buffer M must NOT point to a Lisp string. */
9857 void
9858 message_dolog (const char *m, ptrdiff_t nbytes, bool nlflag, bool multibyte)
9860 const unsigned char *msg = (const unsigned char *) m;
9862 if (!NILP (Vmemory_full))
9863 return;
9865 if (!NILP (Vmessage_log_max))
9867 struct buffer *oldbuf;
9868 Lisp_Object oldpoint, oldbegv, oldzv;
9869 int old_windows_or_buffers_changed = windows_or_buffers_changed;
9870 ptrdiff_t point_at_end = 0;
9871 ptrdiff_t zv_at_end = 0;
9872 Lisp_Object old_deactivate_mark;
9873 struct gcpro gcpro1;
9875 old_deactivate_mark = Vdeactivate_mark;
9876 oldbuf = current_buffer;
9878 /* Ensure the Messages buffer exists, and switch to it.
9879 If we created it, set the major-mode. */
9881 int newbuffer = 0;
9882 if (NILP (Fget_buffer (Vmessages_buffer_name))) newbuffer = 1;
9884 Fset_buffer (Fget_buffer_create (Vmessages_buffer_name));
9886 if (newbuffer
9887 && !NILP (Ffboundp (intern ("messages-buffer-mode"))))
9888 call0 (intern ("messages-buffer-mode"));
9891 bset_undo_list (current_buffer, Qt);
9892 bset_cache_long_scans (current_buffer, Qnil);
9894 oldpoint = message_dolog_marker1;
9895 set_marker_restricted_both (oldpoint, Qnil, PT, PT_BYTE);
9896 oldbegv = message_dolog_marker2;
9897 set_marker_restricted_both (oldbegv, Qnil, BEGV, BEGV_BYTE);
9898 oldzv = message_dolog_marker3;
9899 set_marker_restricted_both (oldzv, Qnil, ZV, ZV_BYTE);
9900 GCPRO1 (old_deactivate_mark);
9902 if (PT == Z)
9903 point_at_end = 1;
9904 if (ZV == Z)
9905 zv_at_end = 1;
9907 BEGV = BEG;
9908 BEGV_BYTE = BEG_BYTE;
9909 ZV = Z;
9910 ZV_BYTE = Z_BYTE;
9911 TEMP_SET_PT_BOTH (Z, Z_BYTE);
9913 /* Insert the string--maybe converting multibyte to single byte
9914 or vice versa, so that all the text fits the buffer. */
9915 if (multibyte
9916 && NILP (BVAR (current_buffer, enable_multibyte_characters)))
9918 ptrdiff_t i;
9919 int c, char_bytes;
9920 char work[1];
9922 /* Convert a multibyte string to single-byte
9923 for the *Message* buffer. */
9924 for (i = 0; i < nbytes; i += char_bytes)
9926 c = string_char_and_length (msg + i, &char_bytes);
9927 work[0] = CHAR_TO_BYTE8 (c);
9928 insert_1_both (work, 1, 1, 1, 0, 0);
9931 else if (! multibyte
9932 && ! NILP (BVAR (current_buffer, enable_multibyte_characters)))
9934 ptrdiff_t i;
9935 int c, char_bytes;
9936 unsigned char str[MAX_MULTIBYTE_LENGTH];
9937 /* Convert a single-byte string to multibyte
9938 for the *Message* buffer. */
9939 for (i = 0; i < nbytes; i++)
9941 c = msg[i];
9942 MAKE_CHAR_MULTIBYTE (c);
9943 char_bytes = CHAR_STRING (c, str);
9944 insert_1_both ((char *) str, 1, char_bytes, 1, 0, 0);
9947 else if (nbytes)
9948 insert_1_both (m, chars_in_text (msg, nbytes), nbytes, 1, 0, 0);
9950 if (nlflag)
9952 ptrdiff_t this_bol, this_bol_byte, prev_bol, prev_bol_byte;
9953 printmax_t dups;
9955 insert_1_both ("\n", 1, 1, 1, 0, 0);
9957 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE, -2, 0);
9958 this_bol = PT;
9959 this_bol_byte = PT_BYTE;
9961 /* See if this line duplicates the previous one.
9962 If so, combine duplicates. */
9963 if (this_bol > BEG)
9965 scan_newline (PT, PT_BYTE, BEG, BEG_BYTE, -2, 0);
9966 prev_bol = PT;
9967 prev_bol_byte = PT_BYTE;
9969 dups = message_log_check_duplicate (prev_bol_byte,
9970 this_bol_byte);
9971 if (dups)
9973 del_range_both (prev_bol, prev_bol_byte,
9974 this_bol, this_bol_byte, 0);
9975 if (dups > 1)
9977 char dupstr[sizeof " [ times]"
9978 + INT_STRLEN_BOUND (printmax_t)];
9980 /* If you change this format, don't forget to also
9981 change message_log_check_duplicate. */
9982 int duplen = sprintf (dupstr, " [%"pMd" times]", dups);
9983 TEMP_SET_PT_BOTH (Z - 1, Z_BYTE - 1);
9984 insert_1_both (dupstr, duplen, duplen, 1, 0, 1);
9989 /* If we have more than the desired maximum number of lines
9990 in the *Messages* buffer now, delete the oldest ones.
9991 This is safe because we don't have undo in this buffer. */
9993 if (NATNUMP (Vmessage_log_max))
9995 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE,
9996 -XFASTINT (Vmessage_log_max) - 1, 0);
9997 del_range_both (BEG, BEG_BYTE, PT, PT_BYTE, 0);
10000 BEGV = marker_position (oldbegv);
10001 BEGV_BYTE = marker_byte_position (oldbegv);
10003 if (zv_at_end)
10005 ZV = Z;
10006 ZV_BYTE = Z_BYTE;
10008 else
10010 ZV = marker_position (oldzv);
10011 ZV_BYTE = marker_byte_position (oldzv);
10014 if (point_at_end)
10015 TEMP_SET_PT_BOTH (Z, Z_BYTE);
10016 else
10017 /* We can't do Fgoto_char (oldpoint) because it will run some
10018 Lisp code. */
10019 TEMP_SET_PT_BOTH (marker_position (oldpoint),
10020 marker_byte_position (oldpoint));
10022 UNGCPRO;
10023 unchain_marker (XMARKER (oldpoint));
10024 unchain_marker (XMARKER (oldbegv));
10025 unchain_marker (XMARKER (oldzv));
10027 /* We called insert_1_both above with its 5th argument (PREPARE)
10028 zero, which prevents insert_1_both from calling
10029 prepare_to_modify_buffer, which in turns prevents us from
10030 incrementing windows_or_buffers_changed even if *Messages* is
10031 shown in some window. So we must manually set
10032 windows_or_buffers_changed here to make up for that. */
10033 windows_or_buffers_changed = old_windows_or_buffers_changed;
10034 bset_redisplay (current_buffer);
10036 set_buffer_internal (oldbuf);
10038 message_log_need_newline = !nlflag;
10039 Vdeactivate_mark = old_deactivate_mark;
10044 /* We are at the end of the buffer after just having inserted a newline.
10045 (Note: We depend on the fact we won't be crossing the gap.)
10046 Check to see if the most recent message looks a lot like the previous one.
10047 Return 0 if different, 1 if the new one should just replace it, or a
10048 value N > 1 if we should also append " [N times]". */
10050 static intmax_t
10051 message_log_check_duplicate (ptrdiff_t prev_bol_byte, ptrdiff_t this_bol_byte)
10053 ptrdiff_t i;
10054 ptrdiff_t len = Z_BYTE - 1 - this_bol_byte;
10055 int seen_dots = 0;
10056 unsigned char *p1 = BUF_BYTE_ADDRESS (current_buffer, prev_bol_byte);
10057 unsigned char *p2 = BUF_BYTE_ADDRESS (current_buffer, this_bol_byte);
10059 for (i = 0; i < len; i++)
10061 if (i >= 3 && p1[i - 3] == '.' && p1[i - 2] == '.' && p1[i - 1] == '.')
10062 seen_dots = 1;
10063 if (p1[i] != p2[i])
10064 return seen_dots;
10066 p1 += len;
10067 if (*p1 == '\n')
10068 return 2;
10069 if (*p1++ == ' ' && *p1++ == '[')
10071 char *pend;
10072 intmax_t n = strtoimax ((char *) p1, &pend, 10);
10073 if (0 < n && n < INTMAX_MAX && strncmp (pend, " times]\n", 8) == 0)
10074 return n + 1;
10076 return 0;
10080 /* Display an echo area message M with a specified length of NBYTES
10081 bytes. The string may include null characters. If M is not a
10082 string, clear out any existing message, and let the mini-buffer
10083 text show through.
10085 This function cancels echoing. */
10087 void
10088 message3 (Lisp_Object m)
10090 struct gcpro gcpro1;
10092 GCPRO1 (m);
10093 clear_message (true, true);
10094 cancel_echoing ();
10096 /* First flush out any partial line written with print. */
10097 message_log_maybe_newline ();
10098 if (STRINGP (m))
10100 ptrdiff_t nbytes = SBYTES (m);
10101 bool multibyte = STRING_MULTIBYTE (m);
10102 char *buffer;
10103 USE_SAFE_ALLOCA;
10104 SAFE_ALLOCA_STRING (buffer, m);
10105 message_dolog (buffer, nbytes, 1, multibyte);
10106 SAFE_FREE ();
10108 message3_nolog (m);
10110 UNGCPRO;
10114 /* The non-logging version of message3.
10115 This does not cancel echoing, because it is used for echoing.
10116 Perhaps we need to make a separate function for echoing
10117 and make this cancel echoing. */
10119 void
10120 message3_nolog (Lisp_Object m)
10122 struct frame *sf = SELECTED_FRAME ();
10124 if (FRAME_INITIAL_P (sf))
10126 if (noninteractive_need_newline)
10127 putc ('\n', stderr);
10128 noninteractive_need_newline = 0;
10129 if (STRINGP (m))
10131 Lisp_Object s = ENCODE_SYSTEM (m);
10133 fwrite (SDATA (s), SBYTES (s), 1, stderr);
10135 if (cursor_in_echo_area == 0)
10136 fprintf (stderr, "\n");
10137 fflush (stderr);
10139 /* Error messages get reported properly by cmd_error, so this must be just an
10140 informative message; if the frame hasn't really been initialized yet, just
10141 toss it. */
10142 else if (INTERACTIVE && sf->glyphs_initialized_p)
10144 /* Get the frame containing the mini-buffer
10145 that the selected frame is using. */
10146 Lisp_Object mini_window = FRAME_MINIBUF_WINDOW (sf);
10147 Lisp_Object frame = XWINDOW (mini_window)->frame;
10148 struct frame *f = XFRAME (frame);
10150 if (FRAME_VISIBLE_P (sf) && !FRAME_VISIBLE_P (f))
10151 Fmake_frame_visible (frame);
10153 if (STRINGP (m) && SCHARS (m) > 0)
10155 set_message (m);
10156 if (minibuffer_auto_raise)
10157 Fraise_frame (frame);
10158 /* Assume we are not echoing.
10159 (If we are, echo_now will override this.) */
10160 echo_message_buffer = Qnil;
10162 else
10163 clear_message (true, true);
10165 do_pending_window_change (false);
10166 echo_area_display (true);
10167 do_pending_window_change (false);
10168 if (FRAME_TERMINAL (f)->frame_up_to_date_hook)
10169 (*FRAME_TERMINAL (f)->frame_up_to_date_hook) (f);
10174 /* Display a null-terminated echo area message M. If M is 0, clear
10175 out any existing message, and let the mini-buffer text show through.
10177 The buffer M must continue to exist until after the echo area gets
10178 cleared or some other message gets displayed there. Do not pass
10179 text that is stored in a Lisp string. Do not pass text in a buffer
10180 that was alloca'd. */
10182 void
10183 message1 (const char *m)
10185 message3 (m ? build_unibyte_string (m) : Qnil);
10189 /* The non-logging counterpart of message1. */
10191 void
10192 message1_nolog (const char *m)
10194 message3_nolog (m ? build_unibyte_string (m) : Qnil);
10197 /* Display a message M which contains a single %s
10198 which gets replaced with STRING. */
10200 void
10201 message_with_string (const char *m, Lisp_Object string, int log)
10203 CHECK_STRING (string);
10205 if (noninteractive)
10207 if (m)
10209 /* ENCODE_SYSTEM below can GC and/or relocate the
10210 Lisp data, so make sure we don't use it here. */
10211 eassert (relocatable_string_data_p (m) != 1);
10213 if (noninteractive_need_newline)
10214 putc ('\n', stderr);
10215 noninteractive_need_newline = 0;
10216 fprintf (stderr, m, SDATA (ENCODE_SYSTEM (string)));
10217 if (!cursor_in_echo_area)
10218 fprintf (stderr, "\n");
10219 fflush (stderr);
10222 else if (INTERACTIVE)
10224 /* The frame whose minibuffer we're going to display the message on.
10225 It may be larger than the selected frame, so we need
10226 to use its buffer, not the selected frame's buffer. */
10227 Lisp_Object mini_window;
10228 struct frame *f, *sf = SELECTED_FRAME ();
10230 /* Get the frame containing the minibuffer
10231 that the selected frame is using. */
10232 mini_window = FRAME_MINIBUF_WINDOW (sf);
10233 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
10235 /* Error messages get reported properly by cmd_error, so this must be
10236 just an informative message; if the frame hasn't really been
10237 initialized yet, just toss it. */
10238 if (f->glyphs_initialized_p)
10240 Lisp_Object args[2], msg;
10241 struct gcpro gcpro1, gcpro2;
10243 args[0] = build_string (m);
10244 args[1] = msg = string;
10245 GCPRO2 (args[0], msg);
10246 gcpro1.nvars = 2;
10248 msg = Fformat (2, args);
10250 if (log)
10251 message3 (msg);
10252 else
10253 message3_nolog (msg);
10255 UNGCPRO;
10257 /* Print should start at the beginning of the message
10258 buffer next time. */
10259 message_buf_print = 0;
10265 /* Dump an informative message to the minibuf. If M is 0, clear out
10266 any existing message, and let the mini-buffer text show through. */
10268 static void
10269 vmessage (const char *m, va_list ap)
10271 if (noninteractive)
10273 if (m)
10275 if (noninteractive_need_newline)
10276 putc ('\n', stderr);
10277 noninteractive_need_newline = 0;
10278 vfprintf (stderr, m, ap);
10279 if (cursor_in_echo_area == 0)
10280 fprintf (stderr, "\n");
10281 fflush (stderr);
10284 else if (INTERACTIVE)
10286 /* The frame whose mini-buffer we're going to display the message
10287 on. It may be larger than the selected frame, so we need to
10288 use its buffer, not the selected frame's buffer. */
10289 Lisp_Object mini_window;
10290 struct frame *f, *sf = SELECTED_FRAME ();
10292 /* Get the frame containing the mini-buffer
10293 that the selected frame is using. */
10294 mini_window = FRAME_MINIBUF_WINDOW (sf);
10295 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
10297 /* Error messages get reported properly by cmd_error, so this must be
10298 just an informative message; if the frame hasn't really been
10299 initialized yet, just toss it. */
10300 if (f->glyphs_initialized_p)
10302 if (m)
10304 ptrdiff_t len;
10305 ptrdiff_t maxsize = FRAME_MESSAGE_BUF_SIZE (f);
10306 USE_SAFE_ALLOCA;
10307 char *message_buf = SAFE_ALLOCA (maxsize + 1);
10309 len = doprnt (message_buf, maxsize, m, 0, ap);
10311 message3 (make_string (message_buf, len));
10312 SAFE_FREE ();
10314 else
10315 message1 (0);
10317 /* Print should start at the beginning of the message
10318 buffer next time. */
10319 message_buf_print = 0;
10324 void
10325 message (const char *m, ...)
10327 va_list ap;
10328 va_start (ap, m);
10329 vmessage (m, ap);
10330 va_end (ap);
10334 #if 0
10335 /* The non-logging version of message. */
10337 void
10338 message_nolog (const char *m, ...)
10340 Lisp_Object old_log_max;
10341 va_list ap;
10342 va_start (ap, m);
10343 old_log_max = Vmessage_log_max;
10344 Vmessage_log_max = Qnil;
10345 vmessage (m, ap);
10346 Vmessage_log_max = old_log_max;
10347 va_end (ap);
10349 #endif
10352 /* Display the current message in the current mini-buffer. This is
10353 only called from error handlers in process.c, and is not time
10354 critical. */
10356 void
10357 update_echo_area (void)
10359 if (!NILP (echo_area_buffer[0]))
10361 Lisp_Object string;
10362 string = Fcurrent_message ();
10363 message3 (string);
10368 /* Make sure echo area buffers in `echo_buffers' are live.
10369 If they aren't, make new ones. */
10371 static void
10372 ensure_echo_area_buffers (void)
10374 int i;
10376 for (i = 0; i < 2; ++i)
10377 if (!BUFFERP (echo_buffer[i])
10378 || !BUFFER_LIVE_P (XBUFFER (echo_buffer[i])))
10380 char name[30];
10381 Lisp_Object old_buffer;
10382 int j;
10384 old_buffer = echo_buffer[i];
10385 echo_buffer[i] = Fget_buffer_create
10386 (make_formatted_string (name, " *Echo Area %d*", i));
10387 bset_truncate_lines (XBUFFER (echo_buffer[i]), Qnil);
10388 /* to force word wrap in echo area -
10389 it was decided to postpone this*/
10390 /* XBUFFER (echo_buffer[i])->word_wrap = Qt; */
10392 for (j = 0; j < 2; ++j)
10393 if (EQ (old_buffer, echo_area_buffer[j]))
10394 echo_area_buffer[j] = echo_buffer[i];
10399 /* Call FN with args A1..A2 with either the current or last displayed
10400 echo_area_buffer as current buffer.
10402 WHICH zero means use the current message buffer
10403 echo_area_buffer[0]. If that is nil, choose a suitable buffer
10404 from echo_buffer[] and clear it.
10406 WHICH > 0 means use echo_area_buffer[1]. If that is nil, choose a
10407 suitable buffer from echo_buffer[] and clear it.
10409 If WHICH < 0, set echo_area_buffer[1] to echo_area_buffer[0], so
10410 that the current message becomes the last displayed one, make
10411 choose a suitable buffer for echo_area_buffer[0], and clear it.
10413 Value is what FN returns. */
10415 static int
10416 with_echo_area_buffer (struct window *w, int which,
10417 int (*fn) (ptrdiff_t, Lisp_Object),
10418 ptrdiff_t a1, Lisp_Object a2)
10420 Lisp_Object buffer;
10421 int this_one, the_other, clear_buffer_p, rc;
10422 ptrdiff_t count = SPECPDL_INDEX ();
10424 /* If buffers aren't live, make new ones. */
10425 ensure_echo_area_buffers ();
10427 clear_buffer_p = 0;
10429 if (which == 0)
10430 this_one = 0, the_other = 1;
10431 else if (which > 0)
10432 this_one = 1, the_other = 0;
10433 else
10435 this_one = 0, the_other = 1;
10436 clear_buffer_p = true;
10438 /* We need a fresh one in case the current echo buffer equals
10439 the one containing the last displayed echo area message. */
10440 if (!NILP (echo_area_buffer[this_one])
10441 && EQ (echo_area_buffer[this_one], echo_area_buffer[the_other]))
10442 echo_area_buffer[this_one] = Qnil;
10445 /* Choose a suitable buffer from echo_buffer[] is we don't
10446 have one. */
10447 if (NILP (echo_area_buffer[this_one]))
10449 echo_area_buffer[this_one]
10450 = (EQ (echo_area_buffer[the_other], echo_buffer[this_one])
10451 ? echo_buffer[the_other]
10452 : echo_buffer[this_one]);
10453 clear_buffer_p = true;
10456 buffer = echo_area_buffer[this_one];
10458 /* Don't get confused by reusing the buffer used for echoing
10459 for a different purpose. */
10460 if (echo_kboard == NULL && EQ (buffer, echo_message_buffer))
10461 cancel_echoing ();
10463 record_unwind_protect (unwind_with_echo_area_buffer,
10464 with_echo_area_buffer_unwind_data (w));
10466 /* Make the echo area buffer current. Note that for display
10467 purposes, it is not necessary that the displayed window's buffer
10468 == current_buffer, except for text property lookup. So, let's
10469 only set that buffer temporarily here without doing a full
10470 Fset_window_buffer. We must also change w->pointm, though,
10471 because otherwise an assertions in unshow_buffer fails, and Emacs
10472 aborts. */
10473 set_buffer_internal_1 (XBUFFER (buffer));
10474 if (w)
10476 wset_buffer (w, buffer);
10477 set_marker_both (w->pointm, buffer, BEG, BEG_BYTE);
10478 set_marker_both (w->old_pointm, buffer, BEG, BEG_BYTE);
10481 bset_undo_list (current_buffer, Qt);
10482 bset_read_only (current_buffer, Qnil);
10483 specbind (Qinhibit_read_only, Qt);
10484 specbind (Qinhibit_modification_hooks, Qt);
10486 if (clear_buffer_p && Z > BEG)
10487 del_range (BEG, Z);
10489 eassert (BEGV >= BEG);
10490 eassert (ZV <= Z && ZV >= BEGV);
10492 rc = fn (a1, a2);
10494 eassert (BEGV >= BEG);
10495 eassert (ZV <= Z && ZV >= BEGV);
10497 unbind_to (count, Qnil);
10498 return rc;
10502 /* Save state that should be preserved around the call to the function
10503 FN called in with_echo_area_buffer. */
10505 static Lisp_Object
10506 with_echo_area_buffer_unwind_data (struct window *w)
10508 int i = 0;
10509 Lisp_Object vector, tmp;
10511 /* Reduce consing by keeping one vector in
10512 Vwith_echo_area_save_vector. */
10513 vector = Vwith_echo_area_save_vector;
10514 Vwith_echo_area_save_vector = Qnil;
10516 if (NILP (vector))
10517 vector = Fmake_vector (make_number (11), Qnil);
10519 XSETBUFFER (tmp, current_buffer); ASET (vector, i, tmp); ++i;
10520 ASET (vector, i, Vdeactivate_mark); ++i;
10521 ASET (vector, i, make_number (windows_or_buffers_changed)); ++i;
10523 if (w)
10525 XSETWINDOW (tmp, w); ASET (vector, i, tmp); ++i;
10526 ASET (vector, i, w->contents); ++i;
10527 ASET (vector, i, make_number (marker_position (w->pointm))); ++i;
10528 ASET (vector, i, make_number (marker_byte_position (w->pointm))); ++i;
10529 ASET (vector, i, make_number (marker_position (w->old_pointm))); ++i;
10530 ASET (vector, i, make_number (marker_byte_position (w->old_pointm))); ++i;
10531 ASET (vector, i, make_number (marker_position (w->start))); ++i;
10532 ASET (vector, i, make_number (marker_byte_position (w->start))); ++i;
10534 else
10536 int end = i + 8;
10537 for (; i < end; ++i)
10538 ASET (vector, i, Qnil);
10541 eassert (i == ASIZE (vector));
10542 return vector;
10546 /* Restore global state from VECTOR which was created by
10547 with_echo_area_buffer_unwind_data. */
10549 static void
10550 unwind_with_echo_area_buffer (Lisp_Object vector)
10552 set_buffer_internal_1 (XBUFFER (AREF (vector, 0)));
10553 Vdeactivate_mark = AREF (vector, 1);
10554 windows_or_buffers_changed = XFASTINT (AREF (vector, 2));
10556 if (WINDOWP (AREF (vector, 3)))
10558 struct window *w;
10559 Lisp_Object buffer;
10561 w = XWINDOW (AREF (vector, 3));
10562 buffer = AREF (vector, 4);
10564 wset_buffer (w, buffer);
10565 set_marker_both (w->pointm, buffer,
10566 XFASTINT (AREF (vector, 5)),
10567 XFASTINT (AREF (vector, 6)));
10568 set_marker_both (w->old_pointm, buffer,
10569 XFASTINT (AREF (vector, 7)),
10570 XFASTINT (AREF (vector, 8)));
10571 set_marker_both (w->start, buffer,
10572 XFASTINT (AREF (vector, 9)),
10573 XFASTINT (AREF (vector, 10)));
10576 Vwith_echo_area_save_vector = vector;
10580 /* Set up the echo area for use by print functions. MULTIBYTE_P
10581 non-zero means we will print multibyte. */
10583 void
10584 setup_echo_area_for_printing (int multibyte_p)
10586 /* If we can't find an echo area any more, exit. */
10587 if (! FRAME_LIVE_P (XFRAME (selected_frame)))
10588 Fkill_emacs (Qnil);
10590 ensure_echo_area_buffers ();
10592 if (!message_buf_print)
10594 /* A message has been output since the last time we printed.
10595 Choose a fresh echo area buffer. */
10596 if (EQ (echo_area_buffer[1], echo_buffer[0]))
10597 echo_area_buffer[0] = echo_buffer[1];
10598 else
10599 echo_area_buffer[0] = echo_buffer[0];
10601 /* Switch to that buffer and clear it. */
10602 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
10603 bset_truncate_lines (current_buffer, Qnil);
10605 if (Z > BEG)
10607 ptrdiff_t count = SPECPDL_INDEX ();
10608 specbind (Qinhibit_read_only, Qt);
10609 /* Note that undo recording is always disabled. */
10610 del_range (BEG, Z);
10611 unbind_to (count, Qnil);
10613 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
10615 /* Set up the buffer for the multibyteness we need. */
10616 if (multibyte_p
10617 != !NILP (BVAR (current_buffer, enable_multibyte_characters)))
10618 Fset_buffer_multibyte (multibyte_p ? Qt : Qnil);
10620 /* Raise the frame containing the echo area. */
10621 if (minibuffer_auto_raise)
10623 struct frame *sf = SELECTED_FRAME ();
10624 Lisp_Object mini_window;
10625 mini_window = FRAME_MINIBUF_WINDOW (sf);
10626 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
10629 message_log_maybe_newline ();
10630 message_buf_print = 1;
10632 else
10634 if (NILP (echo_area_buffer[0]))
10636 if (EQ (echo_area_buffer[1], echo_buffer[0]))
10637 echo_area_buffer[0] = echo_buffer[1];
10638 else
10639 echo_area_buffer[0] = echo_buffer[0];
10642 if (current_buffer != XBUFFER (echo_area_buffer[0]))
10644 /* Someone switched buffers between print requests. */
10645 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
10646 bset_truncate_lines (current_buffer, Qnil);
10652 /* Display an echo area message in window W. Value is non-zero if W's
10653 height is changed. If display_last_displayed_message_p is
10654 non-zero, display the message that was last displayed, otherwise
10655 display the current message. */
10657 static int
10658 display_echo_area (struct window *w)
10660 int i, no_message_p, window_height_changed_p;
10662 /* Temporarily disable garbage collections while displaying the echo
10663 area. This is done because a GC can print a message itself.
10664 That message would modify the echo area buffer's contents while a
10665 redisplay of the buffer is going on, and seriously confuse
10666 redisplay. */
10667 ptrdiff_t count = inhibit_garbage_collection ();
10669 /* If there is no message, we must call display_echo_area_1
10670 nevertheless because it resizes the window. But we will have to
10671 reset the echo_area_buffer in question to nil at the end because
10672 with_echo_area_buffer will sets it to an empty buffer. */
10673 i = display_last_displayed_message_p ? 1 : 0;
10674 no_message_p = NILP (echo_area_buffer[i]);
10676 window_height_changed_p
10677 = with_echo_area_buffer (w, display_last_displayed_message_p,
10678 display_echo_area_1,
10679 (intptr_t) w, Qnil);
10681 if (no_message_p)
10682 echo_area_buffer[i] = Qnil;
10684 unbind_to (count, Qnil);
10685 return window_height_changed_p;
10689 /* Helper for display_echo_area. Display the current buffer which
10690 contains the current echo area message in window W, a mini-window,
10691 a pointer to which is passed in A1. A2..A4 are currently not used.
10692 Change the height of W so that all of the message is displayed.
10693 Value is non-zero if height of W was changed. */
10695 static int
10696 display_echo_area_1 (ptrdiff_t a1, Lisp_Object a2)
10698 intptr_t i1 = a1;
10699 struct window *w = (struct window *) i1;
10700 Lisp_Object window;
10701 struct text_pos start;
10702 int window_height_changed_p = 0;
10704 /* Do this before displaying, so that we have a large enough glyph
10705 matrix for the display. If we can't get enough space for the
10706 whole text, display the last N lines. That works by setting w->start. */
10707 window_height_changed_p = resize_mini_window (w, 0);
10709 /* Use the starting position chosen by resize_mini_window. */
10710 SET_TEXT_POS_FROM_MARKER (start, w->start);
10712 /* Display. */
10713 clear_glyph_matrix (w->desired_matrix);
10714 XSETWINDOW (window, w);
10715 try_window (window, start, 0);
10717 return window_height_changed_p;
10721 /* Resize the echo area window to exactly the size needed for the
10722 currently displayed message, if there is one. If a mini-buffer
10723 is active, don't shrink it. */
10725 void
10726 resize_echo_area_exactly (void)
10728 if (BUFFERP (echo_area_buffer[0])
10729 && WINDOWP (echo_area_window))
10731 struct window *w = XWINDOW (echo_area_window);
10732 Lisp_Object resize_exactly = (minibuf_level == 0 ? Qt : Qnil);
10733 int resized_p = with_echo_area_buffer (w, 0, resize_mini_window_1,
10734 (intptr_t) w, resize_exactly);
10735 if (resized_p)
10737 windows_or_buffers_changed = 42;
10738 update_mode_lines = 30;
10739 redisplay_internal ();
10745 /* Callback function for with_echo_area_buffer, when used from
10746 resize_echo_area_exactly. A1 contains a pointer to the window to
10747 resize, EXACTLY non-nil means resize the mini-window exactly to the
10748 size of the text displayed. A3 and A4 are not used. Value is what
10749 resize_mini_window returns. */
10751 static int
10752 resize_mini_window_1 (ptrdiff_t a1, Lisp_Object exactly)
10754 intptr_t i1 = a1;
10755 return resize_mini_window ((struct window *) i1, !NILP (exactly));
10759 /* Resize mini-window W to fit the size of its contents. EXACT_P
10760 means size the window exactly to the size needed. Otherwise, it's
10761 only enlarged until W's buffer is empty.
10763 Set W->start to the right place to begin display. If the whole
10764 contents fit, start at the beginning. Otherwise, start so as
10765 to make the end of the contents appear. This is particularly
10766 important for y-or-n-p, but seems desirable generally.
10768 Value is non-zero if the window height has been changed. */
10771 resize_mini_window (struct window *w, int exact_p)
10773 struct frame *f = XFRAME (w->frame);
10774 int window_height_changed_p = 0;
10776 eassert (MINI_WINDOW_P (w));
10778 /* By default, start display at the beginning. */
10779 set_marker_both (w->start, w->contents,
10780 BUF_BEGV (XBUFFER (w->contents)),
10781 BUF_BEGV_BYTE (XBUFFER (w->contents)));
10783 /* Don't resize windows while redisplaying a window; it would
10784 confuse redisplay functions when the size of the window they are
10785 displaying changes from under them. Such a resizing can happen,
10786 for instance, when which-func prints a long message while
10787 we are running fontification-functions. We're running these
10788 functions with safe_call which binds inhibit-redisplay to t. */
10789 if (!NILP (Vinhibit_redisplay))
10790 return 0;
10792 /* Nil means don't try to resize. */
10793 if (NILP (Vresize_mini_windows)
10794 || (FRAME_X_P (f) && FRAME_X_OUTPUT (f) == NULL))
10795 return 0;
10797 if (!FRAME_MINIBUF_ONLY_P (f))
10799 struct it it;
10800 int total_height = (WINDOW_PIXEL_HEIGHT (XWINDOW (FRAME_ROOT_WINDOW (f)))
10801 + WINDOW_PIXEL_HEIGHT (w));
10802 int unit = FRAME_LINE_HEIGHT (f);
10803 int height, max_height;
10804 struct text_pos start;
10805 struct buffer *old_current_buffer = NULL;
10807 if (current_buffer != XBUFFER (w->contents))
10809 old_current_buffer = current_buffer;
10810 set_buffer_internal (XBUFFER (w->contents));
10813 init_iterator (&it, w, BEGV, BEGV_BYTE, NULL, DEFAULT_FACE_ID);
10815 /* Compute the max. number of lines specified by the user. */
10816 if (FLOATP (Vmax_mini_window_height))
10817 max_height = XFLOATINT (Vmax_mini_window_height) * total_height;
10818 else if (INTEGERP (Vmax_mini_window_height))
10819 max_height = XINT (Vmax_mini_window_height) * unit;
10820 else
10821 max_height = total_height / 4;
10823 /* Correct that max. height if it's bogus. */
10824 max_height = clip_to_bounds (unit, max_height, total_height);
10826 /* Find out the height of the text in the window. */
10827 if (it.line_wrap == TRUNCATE)
10828 height = unit;
10829 else
10831 last_height = 0;
10832 move_it_to (&it, ZV, -1, -1, -1, MOVE_TO_POS);
10833 if (it.max_ascent == 0 && it.max_descent == 0)
10834 height = it.current_y + last_height;
10835 else
10836 height = it.current_y + it.max_ascent + it.max_descent;
10837 height -= min (it.extra_line_spacing, it.max_extra_line_spacing);
10840 /* Compute a suitable window start. */
10841 if (height > max_height)
10843 height = (max_height / unit) * unit;
10844 init_iterator (&it, w, ZV, ZV_BYTE, NULL, DEFAULT_FACE_ID);
10845 move_it_vertically_backward (&it, height - unit);
10846 start = it.current.pos;
10848 else
10849 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
10850 SET_MARKER_FROM_TEXT_POS (w->start, start);
10852 if (EQ (Vresize_mini_windows, Qgrow_only))
10854 /* Let it grow only, until we display an empty message, in which
10855 case the window shrinks again. */
10856 if (height > WINDOW_PIXEL_HEIGHT (w))
10858 int old_height = WINDOW_PIXEL_HEIGHT (w);
10860 FRAME_WINDOWS_FROZEN (f) = 1;
10861 grow_mini_window (w, height - WINDOW_PIXEL_HEIGHT (w), 1);
10862 window_height_changed_p = WINDOW_PIXEL_HEIGHT (w) != old_height;
10864 else if (height < WINDOW_PIXEL_HEIGHT (w)
10865 && (exact_p || BEGV == ZV))
10867 int old_height = WINDOW_PIXEL_HEIGHT (w);
10869 FRAME_WINDOWS_FROZEN (f) = 0;
10870 shrink_mini_window (w, 1);
10871 window_height_changed_p = WINDOW_PIXEL_HEIGHT (w) != old_height;
10874 else
10876 /* Always resize to exact size needed. */
10877 if (height > WINDOW_PIXEL_HEIGHT (w))
10879 int old_height = WINDOW_PIXEL_HEIGHT (w);
10881 FRAME_WINDOWS_FROZEN (f) = 1;
10882 grow_mini_window (w, height - WINDOW_PIXEL_HEIGHT (w), 1);
10883 window_height_changed_p = WINDOW_PIXEL_HEIGHT (w) != old_height;
10885 else if (height < WINDOW_PIXEL_HEIGHT (w))
10887 int old_height = WINDOW_PIXEL_HEIGHT (w);
10889 FRAME_WINDOWS_FROZEN (f) = 0;
10890 shrink_mini_window (w, 1);
10892 if (height)
10894 FRAME_WINDOWS_FROZEN (f) = 1;
10895 grow_mini_window (w, height - WINDOW_PIXEL_HEIGHT (w), 1);
10898 window_height_changed_p = WINDOW_PIXEL_HEIGHT (w) != old_height;
10902 if (old_current_buffer)
10903 set_buffer_internal (old_current_buffer);
10906 return window_height_changed_p;
10910 /* Value is the current message, a string, or nil if there is no
10911 current message. */
10913 Lisp_Object
10914 current_message (void)
10916 Lisp_Object msg;
10918 if (!BUFFERP (echo_area_buffer[0]))
10919 msg = Qnil;
10920 else
10922 with_echo_area_buffer (0, 0, current_message_1,
10923 (intptr_t) &msg, Qnil);
10924 if (NILP (msg))
10925 echo_area_buffer[0] = Qnil;
10928 return msg;
10932 static int
10933 current_message_1 (ptrdiff_t a1, Lisp_Object a2)
10935 intptr_t i1 = a1;
10936 Lisp_Object *msg = (Lisp_Object *) i1;
10938 if (Z > BEG)
10939 *msg = make_buffer_string (BEG, Z, 1);
10940 else
10941 *msg = Qnil;
10942 return 0;
10946 /* Push the current message on Vmessage_stack for later restoration
10947 by restore_message. Value is non-zero if the current message isn't
10948 empty. This is a relatively infrequent operation, so it's not
10949 worth optimizing. */
10951 bool
10952 push_message (void)
10954 Lisp_Object msg = current_message ();
10955 Vmessage_stack = Fcons (msg, Vmessage_stack);
10956 return STRINGP (msg);
10960 /* Restore message display from the top of Vmessage_stack. */
10962 void
10963 restore_message (void)
10965 eassert (CONSP (Vmessage_stack));
10966 message3_nolog (XCAR (Vmessage_stack));
10970 /* Handler for unwind-protect calling pop_message. */
10972 void
10973 pop_message_unwind (void)
10975 /* Pop the top-most entry off Vmessage_stack. */
10976 eassert (CONSP (Vmessage_stack));
10977 Vmessage_stack = XCDR (Vmessage_stack);
10981 /* Check that Vmessage_stack is nil. Called from emacs.c when Emacs
10982 exits. If the stack is not empty, we have a missing pop_message
10983 somewhere. */
10985 void
10986 check_message_stack (void)
10988 if (!NILP (Vmessage_stack))
10989 emacs_abort ();
10993 /* Truncate to NCHARS what will be displayed in the echo area the next
10994 time we display it---but don't redisplay it now. */
10996 void
10997 truncate_echo_area (ptrdiff_t nchars)
10999 if (nchars == 0)
11000 echo_area_buffer[0] = Qnil;
11001 else if (!noninteractive
11002 && INTERACTIVE
11003 && !NILP (echo_area_buffer[0]))
11005 struct frame *sf = SELECTED_FRAME ();
11006 /* Error messages get reported properly by cmd_error, so this must be
11007 just an informative message; if the frame hasn't really been
11008 initialized yet, just toss it. */
11009 if (sf->glyphs_initialized_p)
11010 with_echo_area_buffer (0, 0, truncate_message_1, nchars, Qnil);
11015 /* Helper function for truncate_echo_area. Truncate the current
11016 message to at most NCHARS characters. */
11018 static int
11019 truncate_message_1 (ptrdiff_t nchars, Lisp_Object a2)
11021 if (BEG + nchars < Z)
11022 del_range (BEG + nchars, Z);
11023 if (Z == BEG)
11024 echo_area_buffer[0] = Qnil;
11025 return 0;
11028 /* Set the current message to STRING. */
11030 static void
11031 set_message (Lisp_Object string)
11033 eassert (STRINGP (string));
11035 message_enable_multibyte = STRING_MULTIBYTE (string);
11037 with_echo_area_buffer (0, -1, set_message_1, 0, string);
11038 message_buf_print = 0;
11039 help_echo_showing_p = 0;
11041 if (STRINGP (Vdebug_on_message)
11042 && STRINGP (string)
11043 && fast_string_match (Vdebug_on_message, string) >= 0)
11044 call_debugger (list2 (Qerror, string));
11048 /* Helper function for set_message. First argument is ignored and second
11049 argument has the same meaning as for set_message.
11050 This function is called with the echo area buffer being current. */
11052 static int
11053 set_message_1 (ptrdiff_t a1, Lisp_Object string)
11055 eassert (STRINGP (string));
11057 /* Change multibyteness of the echo buffer appropriately. */
11058 if (message_enable_multibyte
11059 != !NILP (BVAR (current_buffer, enable_multibyte_characters)))
11060 Fset_buffer_multibyte (message_enable_multibyte ? Qt : Qnil);
11062 bset_truncate_lines (current_buffer, message_truncate_lines ? Qt : Qnil);
11063 if (!NILP (BVAR (current_buffer, bidi_display_reordering)))
11064 bset_bidi_paragraph_direction (current_buffer, Qleft_to_right);
11066 /* Insert new message at BEG. */
11067 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
11069 /* This function takes care of single/multibyte conversion.
11070 We just have to ensure that the echo area buffer has the right
11071 setting of enable_multibyte_characters. */
11072 insert_from_string (string, 0, 0, SCHARS (string), SBYTES (string), 1);
11074 return 0;
11078 /* Clear messages. CURRENT_P non-zero means clear the current
11079 message. LAST_DISPLAYED_P non-zero means clear the message
11080 last displayed. */
11082 void
11083 clear_message (bool current_p, bool last_displayed_p)
11085 if (current_p)
11087 echo_area_buffer[0] = Qnil;
11088 message_cleared_p = true;
11091 if (last_displayed_p)
11092 echo_area_buffer[1] = Qnil;
11094 message_buf_print = 0;
11097 /* Clear garbaged frames.
11099 This function is used where the old redisplay called
11100 redraw_garbaged_frames which in turn called redraw_frame which in
11101 turn called clear_frame. The call to clear_frame was a source of
11102 flickering. I believe a clear_frame is not necessary. It should
11103 suffice in the new redisplay to invalidate all current matrices,
11104 and ensure a complete redisplay of all windows. */
11106 static void
11107 clear_garbaged_frames (void)
11109 if (frame_garbaged)
11111 Lisp_Object tail, frame;
11113 FOR_EACH_FRAME (tail, frame)
11115 struct frame *f = XFRAME (frame);
11117 if (FRAME_VISIBLE_P (f) && FRAME_GARBAGED_P (f))
11119 if (f->resized_p)
11120 redraw_frame (f);
11121 else
11122 clear_current_matrices (f);
11123 fset_redisplay (f);
11124 f->garbaged = false;
11125 f->resized_p = false;
11129 frame_garbaged = false;
11134 /* Redisplay the echo area of the selected frame. If UPDATE_FRAME_P
11135 is non-zero update selected_frame. Value is non-zero if the
11136 mini-windows height has been changed. */
11138 static bool
11139 echo_area_display (bool update_frame_p)
11141 Lisp_Object mini_window;
11142 struct window *w;
11143 struct frame *f;
11144 bool window_height_changed_p = false;
11145 struct frame *sf = SELECTED_FRAME ();
11147 mini_window = FRAME_MINIBUF_WINDOW (sf);
11148 w = XWINDOW (mini_window);
11149 f = XFRAME (WINDOW_FRAME (w));
11151 /* Don't display if frame is invisible or not yet initialized. */
11152 if (!FRAME_VISIBLE_P (f) || !f->glyphs_initialized_p)
11153 return 0;
11155 #ifdef HAVE_WINDOW_SYSTEM
11156 /* When Emacs starts, selected_frame may be the initial terminal
11157 frame. If we let this through, a message would be displayed on
11158 the terminal. */
11159 if (FRAME_INITIAL_P (XFRAME (selected_frame)))
11160 return 0;
11161 #endif /* HAVE_WINDOW_SYSTEM */
11163 /* Redraw garbaged frames. */
11164 clear_garbaged_frames ();
11166 if (!NILP (echo_area_buffer[0]) || minibuf_level == 0)
11168 echo_area_window = mini_window;
11169 window_height_changed_p = display_echo_area (w);
11170 w->must_be_updated_p = true;
11172 /* Update the display, unless called from redisplay_internal.
11173 Also don't update the screen during redisplay itself. The
11174 update will happen at the end of redisplay, and an update
11175 here could cause confusion. */
11176 if (update_frame_p && !redisplaying_p)
11178 int n = 0;
11180 /* If the display update has been interrupted by pending
11181 input, update mode lines in the frame. Due to the
11182 pending input, it might have been that redisplay hasn't
11183 been called, so that mode lines above the echo area are
11184 garbaged. This looks odd, so we prevent it here. */
11185 if (!display_completed)
11186 n = redisplay_mode_lines (FRAME_ROOT_WINDOW (f), false);
11188 if (window_height_changed_p
11189 /* Don't do this if Emacs is shutting down. Redisplay
11190 needs to run hooks. */
11191 && !NILP (Vrun_hooks))
11193 /* Must update other windows. Likewise as in other
11194 cases, don't let this update be interrupted by
11195 pending input. */
11196 ptrdiff_t count = SPECPDL_INDEX ();
11197 specbind (Qredisplay_dont_pause, Qt);
11198 windows_or_buffers_changed = 44;
11199 redisplay_internal ();
11200 unbind_to (count, Qnil);
11202 else if (FRAME_WINDOW_P (f) && n == 0)
11204 /* Window configuration is the same as before.
11205 Can do with a display update of the echo area,
11206 unless we displayed some mode lines. */
11207 update_single_window (w);
11208 flush_frame (f);
11210 else
11211 update_frame (f, true, true);
11213 /* If cursor is in the echo area, make sure that the next
11214 redisplay displays the minibuffer, so that the cursor will
11215 be replaced with what the minibuffer wants. */
11216 if (cursor_in_echo_area)
11217 wset_redisplay (XWINDOW (mini_window));
11220 else if (!EQ (mini_window, selected_window))
11221 wset_redisplay (XWINDOW (mini_window));
11223 /* Last displayed message is now the current message. */
11224 echo_area_buffer[1] = echo_area_buffer[0];
11225 /* Inform read_char that we're not echoing. */
11226 echo_message_buffer = Qnil;
11228 /* Prevent redisplay optimization in redisplay_internal by resetting
11229 this_line_start_pos. This is done because the mini-buffer now
11230 displays the message instead of its buffer text. */
11231 if (EQ (mini_window, selected_window))
11232 CHARPOS (this_line_start_pos) = 0;
11234 return window_height_changed_p;
11237 /* Nonzero if W's buffer was changed but not saved. */
11239 static int
11240 window_buffer_changed (struct window *w)
11242 struct buffer *b = XBUFFER (w->contents);
11244 eassert (BUFFER_LIVE_P (b));
11246 return (((BUF_SAVE_MODIFF (b) < BUF_MODIFF (b)) != w->last_had_star));
11249 /* Nonzero if W has %c in its mode line and mode line should be updated. */
11251 static int
11252 mode_line_update_needed (struct window *w)
11254 return (w->column_number_displayed != -1
11255 && !(PT == w->last_point && !window_outdated (w))
11256 && (w->column_number_displayed != current_column ()));
11259 /* Nonzero if window start of W is frozen and may not be changed during
11260 redisplay. */
11262 static bool
11263 window_frozen_p (struct window *w)
11265 if (FRAME_WINDOWS_FROZEN (XFRAME (WINDOW_FRAME (w))))
11267 Lisp_Object window;
11269 XSETWINDOW (window, w);
11270 if (MINI_WINDOW_P (w))
11271 return 0;
11272 else if (EQ (window, selected_window))
11273 return 0;
11274 else if (MINI_WINDOW_P (XWINDOW (selected_window))
11275 && EQ (window, Vminibuf_scroll_window))
11276 /* This special window can't be frozen too. */
11277 return 0;
11278 else
11279 return 1;
11281 return 0;
11284 /***********************************************************************
11285 Mode Lines and Frame Titles
11286 ***********************************************************************/
11288 /* A buffer for constructing non-propertized mode-line strings and
11289 frame titles in it; allocated from the heap in init_xdisp and
11290 resized as needed in store_mode_line_noprop_char. */
11292 static char *mode_line_noprop_buf;
11294 /* The buffer's end, and a current output position in it. */
11296 static char *mode_line_noprop_buf_end;
11297 static char *mode_line_noprop_ptr;
11299 #define MODE_LINE_NOPROP_LEN(start) \
11300 ((mode_line_noprop_ptr - mode_line_noprop_buf) - start)
11302 static enum {
11303 MODE_LINE_DISPLAY = 0,
11304 MODE_LINE_TITLE,
11305 MODE_LINE_NOPROP,
11306 MODE_LINE_STRING
11307 } mode_line_target;
11309 /* Alist that caches the results of :propertize.
11310 Each element is (PROPERTIZED-STRING . PROPERTY-LIST). */
11311 static Lisp_Object mode_line_proptrans_alist;
11313 /* List of strings making up the mode-line. */
11314 static Lisp_Object mode_line_string_list;
11316 /* Base face property when building propertized mode line string. */
11317 static Lisp_Object mode_line_string_face;
11318 static Lisp_Object mode_line_string_face_prop;
11321 /* Unwind data for mode line strings */
11323 static Lisp_Object Vmode_line_unwind_vector;
11325 static Lisp_Object
11326 format_mode_line_unwind_data (struct frame *target_frame,
11327 struct buffer *obuf,
11328 Lisp_Object owin,
11329 int save_proptrans)
11331 Lisp_Object vector, tmp;
11333 /* Reduce consing by keeping one vector in
11334 Vwith_echo_area_save_vector. */
11335 vector = Vmode_line_unwind_vector;
11336 Vmode_line_unwind_vector = Qnil;
11338 if (NILP (vector))
11339 vector = Fmake_vector (make_number (10), Qnil);
11341 ASET (vector, 0, make_number (mode_line_target));
11342 ASET (vector, 1, make_number (MODE_LINE_NOPROP_LEN (0)));
11343 ASET (vector, 2, mode_line_string_list);
11344 ASET (vector, 3, save_proptrans ? mode_line_proptrans_alist : Qt);
11345 ASET (vector, 4, mode_line_string_face);
11346 ASET (vector, 5, mode_line_string_face_prop);
11348 if (obuf)
11349 XSETBUFFER (tmp, obuf);
11350 else
11351 tmp = Qnil;
11352 ASET (vector, 6, tmp);
11353 ASET (vector, 7, owin);
11354 if (target_frame)
11356 /* Similarly to `with-selected-window', if the operation selects
11357 a window on another frame, we must restore that frame's
11358 selected window, and (for a tty) the top-frame. */
11359 ASET (vector, 8, target_frame->selected_window);
11360 if (FRAME_TERMCAP_P (target_frame))
11361 ASET (vector, 9, FRAME_TTY (target_frame)->top_frame);
11364 return vector;
11367 static void
11368 unwind_format_mode_line (Lisp_Object vector)
11370 Lisp_Object old_window = AREF (vector, 7);
11371 Lisp_Object target_frame_window = AREF (vector, 8);
11372 Lisp_Object old_top_frame = AREF (vector, 9);
11374 mode_line_target = XINT (AREF (vector, 0));
11375 mode_line_noprop_ptr = mode_line_noprop_buf + XINT (AREF (vector, 1));
11376 mode_line_string_list = AREF (vector, 2);
11377 if (! EQ (AREF (vector, 3), Qt))
11378 mode_line_proptrans_alist = AREF (vector, 3);
11379 mode_line_string_face = AREF (vector, 4);
11380 mode_line_string_face_prop = AREF (vector, 5);
11382 /* Select window before buffer, since it may change the buffer. */
11383 if (!NILP (old_window))
11385 /* If the operation that we are unwinding had selected a window
11386 on a different frame, reset its frame-selected-window. For a
11387 text terminal, reset its top-frame if necessary. */
11388 if (!NILP (target_frame_window))
11390 Lisp_Object frame
11391 = WINDOW_FRAME (XWINDOW (target_frame_window));
11393 if (!EQ (frame, WINDOW_FRAME (XWINDOW (old_window))))
11394 Fselect_window (target_frame_window, Qt);
11396 if (!NILP (old_top_frame) && !EQ (old_top_frame, frame))
11397 Fselect_frame (old_top_frame, Qt);
11400 Fselect_window (old_window, Qt);
11403 if (!NILP (AREF (vector, 6)))
11405 set_buffer_internal_1 (XBUFFER (AREF (vector, 6)));
11406 ASET (vector, 6, Qnil);
11409 Vmode_line_unwind_vector = vector;
11413 /* Store a single character C for the frame title in mode_line_noprop_buf.
11414 Re-allocate mode_line_noprop_buf if necessary. */
11416 static void
11417 store_mode_line_noprop_char (char c)
11419 /* If output position has reached the end of the allocated buffer,
11420 increase the buffer's size. */
11421 if (mode_line_noprop_ptr == mode_line_noprop_buf_end)
11423 ptrdiff_t len = MODE_LINE_NOPROP_LEN (0);
11424 ptrdiff_t size = len;
11425 mode_line_noprop_buf =
11426 xpalloc (mode_line_noprop_buf, &size, 1, STRING_BYTES_BOUND, 1);
11427 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
11428 mode_line_noprop_ptr = mode_line_noprop_buf + len;
11431 *mode_line_noprop_ptr++ = c;
11435 /* Store part of a frame title in mode_line_noprop_buf, beginning at
11436 mode_line_noprop_ptr. STRING is the string to store. Do not copy
11437 characters that yield more columns than PRECISION; PRECISION <= 0
11438 means copy the whole string. Pad with spaces until FIELD_WIDTH
11439 number of characters have been copied; FIELD_WIDTH <= 0 means don't
11440 pad. Called from display_mode_element when it is used to build a
11441 frame title. */
11443 static int
11444 store_mode_line_noprop (const char *string, int field_width, int precision)
11446 const unsigned char *str = (const unsigned char *) string;
11447 int n = 0;
11448 ptrdiff_t dummy, nbytes;
11450 /* Copy at most PRECISION chars from STR. */
11451 nbytes = strlen (string);
11452 n += c_string_width (str, nbytes, precision, &dummy, &nbytes);
11453 while (nbytes--)
11454 store_mode_line_noprop_char (*str++);
11456 /* Fill up with spaces until FIELD_WIDTH reached. */
11457 while (field_width > 0
11458 && n < field_width)
11460 store_mode_line_noprop_char (' ');
11461 ++n;
11464 return n;
11467 /***********************************************************************
11468 Frame Titles
11469 ***********************************************************************/
11471 #ifdef HAVE_WINDOW_SYSTEM
11473 /* Set the title of FRAME, if it has changed. The title format is
11474 Vicon_title_format if FRAME is iconified, otherwise it is
11475 frame_title_format. */
11477 static void
11478 x_consider_frame_title (Lisp_Object frame)
11480 struct frame *f = XFRAME (frame);
11482 if (FRAME_WINDOW_P (f)
11483 || FRAME_MINIBUF_ONLY_P (f)
11484 || f->explicit_name)
11486 /* Do we have more than one visible frame on this X display? */
11487 Lisp_Object tail, other_frame, fmt;
11488 ptrdiff_t title_start;
11489 char *title;
11490 ptrdiff_t len;
11491 struct it it;
11492 ptrdiff_t count = SPECPDL_INDEX ();
11494 FOR_EACH_FRAME (tail, other_frame)
11496 struct frame *tf = XFRAME (other_frame);
11498 if (tf != f
11499 && FRAME_KBOARD (tf) == FRAME_KBOARD (f)
11500 && !FRAME_MINIBUF_ONLY_P (tf)
11501 && !EQ (other_frame, tip_frame)
11502 && (FRAME_VISIBLE_P (tf) || FRAME_ICONIFIED_P (tf)))
11503 break;
11506 /* Set global variable indicating that multiple frames exist. */
11507 multiple_frames = CONSP (tail);
11509 /* Switch to the buffer of selected window of the frame. Set up
11510 mode_line_target so that display_mode_element will output into
11511 mode_line_noprop_buf; then display the title. */
11512 record_unwind_protect (unwind_format_mode_line,
11513 format_mode_line_unwind_data
11514 (f, current_buffer, selected_window, 0));
11516 Fselect_window (f->selected_window, Qt);
11517 set_buffer_internal_1
11518 (XBUFFER (XWINDOW (f->selected_window)->contents));
11519 fmt = FRAME_ICONIFIED_P (f) ? Vicon_title_format : Vframe_title_format;
11521 mode_line_target = MODE_LINE_TITLE;
11522 title_start = MODE_LINE_NOPROP_LEN (0);
11523 init_iterator (&it, XWINDOW (f->selected_window), -1, -1,
11524 NULL, DEFAULT_FACE_ID);
11525 display_mode_element (&it, 0, -1, -1, fmt, Qnil, 0);
11526 len = MODE_LINE_NOPROP_LEN (title_start);
11527 title = mode_line_noprop_buf + title_start;
11528 unbind_to (count, Qnil);
11530 /* Set the title only if it's changed. This avoids consing in
11531 the common case where it hasn't. (If it turns out that we've
11532 already wasted too much time by walking through the list with
11533 display_mode_element, then we might need to optimize at a
11534 higher level than this.) */
11535 if (! STRINGP (f->name)
11536 || SBYTES (f->name) != len
11537 || memcmp (title, SDATA (f->name), len) != 0)
11538 x_implicitly_set_name (f, make_string (title, len), Qnil);
11542 #endif /* not HAVE_WINDOW_SYSTEM */
11545 /***********************************************************************
11546 Menu Bars
11547 ***********************************************************************/
11549 /* Non-zero if we will not redisplay all visible windows. */
11550 #define REDISPLAY_SOME_P() \
11551 ((windows_or_buffers_changed == 0 \
11552 || windows_or_buffers_changed == REDISPLAY_SOME) \
11553 && (update_mode_lines == 0 \
11554 || update_mode_lines == REDISPLAY_SOME))
11556 /* Prepare for redisplay by updating menu-bar item lists when
11557 appropriate. This can call eval. */
11559 static void
11560 prepare_menu_bars (void)
11562 bool all_windows = windows_or_buffers_changed || update_mode_lines;
11563 bool some_windows = REDISPLAY_SOME_P ();
11564 struct gcpro gcpro1, gcpro2;
11565 Lisp_Object tooltip_frame;
11567 #ifdef HAVE_WINDOW_SYSTEM
11568 tooltip_frame = tip_frame;
11569 #else
11570 tooltip_frame = Qnil;
11571 #endif
11573 if (FUNCTIONP (Vpre_redisplay_function))
11575 Lisp_Object windows = all_windows ? Qt : Qnil;
11576 if (all_windows && some_windows)
11578 Lisp_Object ws = window_list ();
11579 for (windows = Qnil; CONSP (ws); ws = XCDR (ws))
11581 Lisp_Object this = XCAR (ws);
11582 struct window *w = XWINDOW (this);
11583 if (w->redisplay
11584 || XFRAME (w->frame)->redisplay
11585 || XBUFFER (w->contents)->text->redisplay)
11587 windows = Fcons (this, windows);
11591 safe__call1 (true, Vpre_redisplay_function, windows);
11594 /* Update all frame titles based on their buffer names, etc. We do
11595 this before the menu bars so that the buffer-menu will show the
11596 up-to-date frame titles. */
11597 #ifdef HAVE_WINDOW_SYSTEM
11598 if (all_windows)
11600 Lisp_Object tail, frame;
11602 FOR_EACH_FRAME (tail, frame)
11604 struct frame *f = XFRAME (frame);
11605 struct window *w = XWINDOW (FRAME_SELECTED_WINDOW (f));
11606 if (some_windows
11607 && !f->redisplay
11608 && !w->redisplay
11609 && !XBUFFER (w->contents)->text->redisplay)
11610 continue;
11612 if (!EQ (frame, tooltip_frame)
11613 && (FRAME_ICONIFIED_P (f)
11614 || FRAME_VISIBLE_P (f) == 1
11615 /* Exclude TTY frames that are obscured because they
11616 are not the top frame on their console. This is
11617 because x_consider_frame_title actually switches
11618 to the frame, which for TTY frames means it is
11619 marked as garbaged, and will be completely
11620 redrawn on the next redisplay cycle. This causes
11621 TTY frames to be completely redrawn, when there
11622 are more than one of them, even though nothing
11623 should be changed on display. */
11624 || (FRAME_VISIBLE_P (f) == 2 && FRAME_WINDOW_P (f))))
11625 x_consider_frame_title (frame);
11628 #endif /* HAVE_WINDOW_SYSTEM */
11630 /* Update the menu bar item lists, if appropriate. This has to be
11631 done before any actual redisplay or generation of display lines. */
11633 if (all_windows)
11635 Lisp_Object tail, frame;
11636 ptrdiff_t count = SPECPDL_INDEX ();
11637 /* 1 means that update_menu_bar has run its hooks
11638 so any further calls to update_menu_bar shouldn't do so again. */
11639 int menu_bar_hooks_run = 0;
11641 record_unwind_save_match_data ();
11643 FOR_EACH_FRAME (tail, frame)
11645 struct frame *f = XFRAME (frame);
11646 struct window *w = XWINDOW (FRAME_SELECTED_WINDOW (f));
11648 /* Ignore tooltip frame. */
11649 if (EQ (frame, tooltip_frame))
11650 continue;
11652 if (some_windows
11653 && !f->redisplay
11654 && !w->redisplay
11655 && !XBUFFER (w->contents)->text->redisplay)
11656 continue;
11658 /* If a window on this frame changed size, report that to
11659 the user and clear the size-change flag. */
11660 if (FRAME_WINDOW_SIZES_CHANGED (f))
11662 Lisp_Object functions;
11664 /* Clear flag first in case we get an error below. */
11665 FRAME_WINDOW_SIZES_CHANGED (f) = 0;
11666 functions = Vwindow_size_change_functions;
11667 GCPRO2 (tail, functions);
11669 while (CONSP (functions))
11671 if (!EQ (XCAR (functions), Qt))
11672 call1 (XCAR (functions), frame);
11673 functions = XCDR (functions);
11675 UNGCPRO;
11678 GCPRO1 (tail);
11679 menu_bar_hooks_run = update_menu_bar (f, 0, menu_bar_hooks_run);
11680 #ifdef HAVE_WINDOW_SYSTEM
11681 update_tool_bar (f, 0);
11682 #endif
11683 UNGCPRO;
11686 unbind_to (count, Qnil);
11688 else
11690 struct frame *sf = SELECTED_FRAME ();
11691 update_menu_bar (sf, 1, 0);
11692 #ifdef HAVE_WINDOW_SYSTEM
11693 update_tool_bar (sf, 1);
11694 #endif
11699 /* Update the menu bar item list for frame F. This has to be done
11700 before we start to fill in any display lines, because it can call
11701 eval.
11703 If SAVE_MATCH_DATA is non-zero, we must save and restore it here.
11705 If HOOKS_RUN is 1, that means a previous call to update_menu_bar
11706 already ran the menu bar hooks for this redisplay, so there
11707 is no need to run them again. The return value is the
11708 updated value of this flag, to pass to the next call. */
11710 static int
11711 update_menu_bar (struct frame *f, int save_match_data, int hooks_run)
11713 Lisp_Object window;
11714 register struct window *w;
11716 /* If called recursively during a menu update, do nothing. This can
11717 happen when, for instance, an activate-menubar-hook causes a
11718 redisplay. */
11719 if (inhibit_menubar_update)
11720 return hooks_run;
11722 window = FRAME_SELECTED_WINDOW (f);
11723 w = XWINDOW (window);
11725 if (FRAME_WINDOW_P (f)
11727 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
11728 || defined (HAVE_NS) || defined (USE_GTK)
11729 FRAME_EXTERNAL_MENU_BAR (f)
11730 #else
11731 FRAME_MENU_BAR_LINES (f) > 0
11732 #endif
11733 : FRAME_MENU_BAR_LINES (f) > 0)
11735 /* If the user has switched buffers or windows, we need to
11736 recompute to reflect the new bindings. But we'll
11737 recompute when update_mode_lines is set too; that means
11738 that people can use force-mode-line-update to request
11739 that the menu bar be recomputed. The adverse effect on
11740 the rest of the redisplay algorithm is about the same as
11741 windows_or_buffers_changed anyway. */
11742 if (windows_or_buffers_changed
11743 /* This used to test w->update_mode_line, but we believe
11744 there is no need to recompute the menu in that case. */
11745 || update_mode_lines
11746 || window_buffer_changed (w))
11748 struct buffer *prev = current_buffer;
11749 ptrdiff_t count = SPECPDL_INDEX ();
11751 specbind (Qinhibit_menubar_update, Qt);
11753 set_buffer_internal_1 (XBUFFER (w->contents));
11754 if (save_match_data)
11755 record_unwind_save_match_data ();
11756 if (NILP (Voverriding_local_map_menu_flag))
11758 specbind (Qoverriding_terminal_local_map, Qnil);
11759 specbind (Qoverriding_local_map, Qnil);
11762 if (!hooks_run)
11764 /* Run the Lucid hook. */
11765 safe_run_hooks (Qactivate_menubar_hook);
11767 /* If it has changed current-menubar from previous value,
11768 really recompute the menu-bar from the value. */
11769 if (! NILP (Vlucid_menu_bar_dirty_flag))
11770 call0 (Qrecompute_lucid_menubar);
11772 safe_run_hooks (Qmenu_bar_update_hook);
11774 hooks_run = 1;
11777 XSETFRAME (Vmenu_updating_frame, f);
11778 fset_menu_bar_items (f, menu_bar_items (FRAME_MENU_BAR_ITEMS (f)));
11780 /* Redisplay the menu bar in case we changed it. */
11781 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
11782 || defined (HAVE_NS) || defined (USE_GTK)
11783 if (FRAME_WINDOW_P (f))
11785 #if defined (HAVE_NS)
11786 /* All frames on Mac OS share the same menubar. So only
11787 the selected frame should be allowed to set it. */
11788 if (f == SELECTED_FRAME ())
11789 #endif
11790 set_frame_menubar (f, 0, 0);
11792 else
11793 /* On a terminal screen, the menu bar is an ordinary screen
11794 line, and this makes it get updated. */
11795 w->update_mode_line = 1;
11796 #else /* ! (USE_X_TOOLKIT || HAVE_NTGUI || HAVE_NS || USE_GTK) */
11797 /* In the non-toolkit version, the menu bar is an ordinary screen
11798 line, and this makes it get updated. */
11799 w->update_mode_line = 1;
11800 #endif /* ! (USE_X_TOOLKIT || HAVE_NTGUI || HAVE_NS || USE_GTK) */
11802 unbind_to (count, Qnil);
11803 set_buffer_internal_1 (prev);
11807 return hooks_run;
11810 /***********************************************************************
11811 Tool-bars
11812 ***********************************************************************/
11814 #ifdef HAVE_WINDOW_SYSTEM
11816 /* Select `frame' temporarily without running all the code in
11817 do_switch_frame.
11818 FIXME: Maybe do_switch_frame should be trimmed down similarly
11819 when `norecord' is set. */
11820 static void
11821 fast_set_selected_frame (Lisp_Object frame)
11823 if (!EQ (selected_frame, frame))
11825 selected_frame = frame;
11826 selected_window = XFRAME (frame)->selected_window;
11830 /* Update the tool-bar item list for frame F. This has to be done
11831 before we start to fill in any display lines. Called from
11832 prepare_menu_bars. If SAVE_MATCH_DATA is non-zero, we must save
11833 and restore it here. */
11835 static void
11836 update_tool_bar (struct frame *f, int save_match_data)
11838 #if defined (USE_GTK) || defined (HAVE_NS)
11839 int do_update = FRAME_EXTERNAL_TOOL_BAR (f);
11840 #else
11841 int do_update = (WINDOWP (f->tool_bar_window)
11842 && WINDOW_TOTAL_LINES (XWINDOW (f->tool_bar_window)) > 0);
11843 #endif
11845 if (do_update)
11847 Lisp_Object window;
11848 struct window *w;
11850 window = FRAME_SELECTED_WINDOW (f);
11851 w = XWINDOW (window);
11853 /* If the user has switched buffers or windows, we need to
11854 recompute to reflect the new bindings. But we'll
11855 recompute when update_mode_lines is set too; that means
11856 that people can use force-mode-line-update to request
11857 that the menu bar be recomputed. The adverse effect on
11858 the rest of the redisplay algorithm is about the same as
11859 windows_or_buffers_changed anyway. */
11860 if (windows_or_buffers_changed
11861 || w->update_mode_line
11862 || update_mode_lines
11863 || window_buffer_changed (w))
11865 struct buffer *prev = current_buffer;
11866 ptrdiff_t count = SPECPDL_INDEX ();
11867 Lisp_Object frame, new_tool_bar;
11868 int new_n_tool_bar;
11869 struct gcpro gcpro1;
11871 /* Set current_buffer to the buffer of the selected
11872 window of the frame, so that we get the right local
11873 keymaps. */
11874 set_buffer_internal_1 (XBUFFER (w->contents));
11876 /* Save match data, if we must. */
11877 if (save_match_data)
11878 record_unwind_save_match_data ();
11880 /* Make sure that we don't accidentally use bogus keymaps. */
11881 if (NILP (Voverriding_local_map_menu_flag))
11883 specbind (Qoverriding_terminal_local_map, Qnil);
11884 specbind (Qoverriding_local_map, Qnil);
11887 GCPRO1 (new_tool_bar);
11889 /* We must temporarily set the selected frame to this frame
11890 before calling tool_bar_items, because the calculation of
11891 the tool-bar keymap uses the selected frame (see
11892 `tool-bar-make-keymap' in tool-bar.el). */
11893 eassert (EQ (selected_window,
11894 /* Since we only explicitly preserve selected_frame,
11895 check that selected_window would be redundant. */
11896 XFRAME (selected_frame)->selected_window));
11897 record_unwind_protect (fast_set_selected_frame, selected_frame);
11898 XSETFRAME (frame, f);
11899 fast_set_selected_frame (frame);
11901 /* Build desired tool-bar items from keymaps. */
11902 new_tool_bar
11903 = tool_bar_items (Fcopy_sequence (f->tool_bar_items),
11904 &new_n_tool_bar);
11906 /* Redisplay the tool-bar if we changed it. */
11907 if (new_n_tool_bar != f->n_tool_bar_items
11908 || NILP (Fequal (new_tool_bar, f->tool_bar_items)))
11910 /* Redisplay that happens asynchronously due to an expose event
11911 may access f->tool_bar_items. Make sure we update both
11912 variables within BLOCK_INPUT so no such event interrupts. */
11913 block_input ();
11914 fset_tool_bar_items (f, new_tool_bar);
11915 f->n_tool_bar_items = new_n_tool_bar;
11916 w->update_mode_line = 1;
11917 unblock_input ();
11920 UNGCPRO;
11922 unbind_to (count, Qnil);
11923 set_buffer_internal_1 (prev);
11928 #if ! defined (USE_GTK) && ! defined (HAVE_NS)
11930 /* Set F->desired_tool_bar_string to a Lisp string representing frame
11931 F's desired tool-bar contents. F->tool_bar_items must have
11932 been set up previously by calling prepare_menu_bars. */
11934 static void
11935 build_desired_tool_bar_string (struct frame *f)
11937 int i, size, size_needed;
11938 struct gcpro gcpro1, gcpro2;
11939 Lisp_Object image, plist;
11941 image = plist = Qnil;
11942 GCPRO2 (image, plist);
11944 /* Prepare F->desired_tool_bar_string. If we can reuse it, do so.
11945 Otherwise, make a new string. */
11947 /* The size of the string we might be able to reuse. */
11948 size = (STRINGP (f->desired_tool_bar_string)
11949 ? SCHARS (f->desired_tool_bar_string)
11950 : 0);
11952 /* We need one space in the string for each image. */
11953 size_needed = f->n_tool_bar_items;
11955 /* Reuse f->desired_tool_bar_string, if possible. */
11956 if (size < size_needed || NILP (f->desired_tool_bar_string))
11957 fset_desired_tool_bar_string
11958 (f, Fmake_string (make_number (size_needed), make_number (' ')));
11959 else
11961 AUTO_LIST4 (props, Qdisplay, Qnil, Qmenu_item, Qnil);
11962 struct gcpro gcpro1;
11963 GCPRO1 (props);
11964 Fremove_text_properties (make_number (0), make_number (size),
11965 props, f->desired_tool_bar_string);
11966 UNGCPRO;
11969 /* Put a `display' property on the string for the images to display,
11970 put a `menu_item' property on tool-bar items with a value that
11971 is the index of the item in F's tool-bar item vector. */
11972 for (i = 0; i < f->n_tool_bar_items; ++i)
11974 #define PROP(IDX) \
11975 AREF (f->tool_bar_items, i * TOOL_BAR_ITEM_NSLOTS + (IDX))
11977 int enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
11978 int selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
11979 int hmargin, vmargin, relief, idx, end;
11981 /* If image is a vector, choose the image according to the
11982 button state. */
11983 image = PROP (TOOL_BAR_ITEM_IMAGES);
11984 if (VECTORP (image))
11986 if (enabled_p)
11987 idx = (selected_p
11988 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
11989 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
11990 else
11991 idx = (selected_p
11992 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
11993 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
11995 eassert (ASIZE (image) >= idx);
11996 image = AREF (image, idx);
11998 else
11999 idx = -1;
12001 /* Ignore invalid image specifications. */
12002 if (!valid_image_p (image))
12003 continue;
12005 /* Display the tool-bar button pressed, or depressed. */
12006 plist = Fcopy_sequence (XCDR (image));
12008 /* Compute margin and relief to draw. */
12009 relief = (tool_bar_button_relief >= 0
12010 ? tool_bar_button_relief
12011 : DEFAULT_TOOL_BAR_BUTTON_RELIEF);
12012 hmargin = vmargin = relief;
12014 if (RANGED_INTEGERP (1, Vtool_bar_button_margin,
12015 INT_MAX - max (hmargin, vmargin)))
12017 hmargin += XFASTINT (Vtool_bar_button_margin);
12018 vmargin += XFASTINT (Vtool_bar_button_margin);
12020 else if (CONSP (Vtool_bar_button_margin))
12022 if (RANGED_INTEGERP (1, XCAR (Vtool_bar_button_margin),
12023 INT_MAX - hmargin))
12024 hmargin += XFASTINT (XCAR (Vtool_bar_button_margin));
12026 if (RANGED_INTEGERP (1, XCDR (Vtool_bar_button_margin),
12027 INT_MAX - vmargin))
12028 vmargin += XFASTINT (XCDR (Vtool_bar_button_margin));
12031 if (auto_raise_tool_bar_buttons_p)
12033 /* Add a `:relief' property to the image spec if the item is
12034 selected. */
12035 if (selected_p)
12037 plist = Fplist_put (plist, QCrelief, make_number (-relief));
12038 hmargin -= relief;
12039 vmargin -= relief;
12042 else
12044 /* If image is selected, display it pressed, i.e. with a
12045 negative relief. If it's not selected, display it with a
12046 raised relief. */
12047 plist = Fplist_put (plist, QCrelief,
12048 (selected_p
12049 ? make_number (-relief)
12050 : make_number (relief)));
12051 hmargin -= relief;
12052 vmargin -= relief;
12055 /* Put a margin around the image. */
12056 if (hmargin || vmargin)
12058 if (hmargin == vmargin)
12059 plist = Fplist_put (plist, QCmargin, make_number (hmargin));
12060 else
12061 plist = Fplist_put (plist, QCmargin,
12062 Fcons (make_number (hmargin),
12063 make_number (vmargin)));
12066 /* If button is not enabled, and we don't have special images
12067 for the disabled state, make the image appear disabled by
12068 applying an appropriate algorithm to it. */
12069 if (!enabled_p && idx < 0)
12070 plist = Fplist_put (plist, QCconversion, Qdisabled);
12072 /* Put a `display' text property on the string for the image to
12073 display. Put a `menu-item' property on the string that gives
12074 the start of this item's properties in the tool-bar items
12075 vector. */
12076 image = Fcons (Qimage, plist);
12077 AUTO_LIST4 (props, Qdisplay, image, Qmenu_item,
12078 make_number (i * TOOL_BAR_ITEM_NSLOTS));
12079 struct gcpro gcpro1;
12080 GCPRO1 (props);
12082 /* Let the last image hide all remaining spaces in the tool bar
12083 string. The string can be longer than needed when we reuse a
12084 previous string. */
12085 if (i + 1 == f->n_tool_bar_items)
12086 end = SCHARS (f->desired_tool_bar_string);
12087 else
12088 end = i + 1;
12089 Fadd_text_properties (make_number (i), make_number (end),
12090 props, f->desired_tool_bar_string);
12091 UNGCPRO;
12092 #undef PROP
12095 UNGCPRO;
12099 /* Display one line of the tool-bar of frame IT->f.
12101 HEIGHT specifies the desired height of the tool-bar line.
12102 If the actual height of the glyph row is less than HEIGHT, the
12103 row's height is increased to HEIGHT, and the icons are centered
12104 vertically in the new height.
12106 If HEIGHT is -1, we are counting needed tool-bar lines, so don't
12107 count a final empty row in case the tool-bar width exactly matches
12108 the window width.
12111 static void
12112 display_tool_bar_line (struct it *it, int height)
12114 struct glyph_row *row = it->glyph_row;
12115 int max_x = it->last_visible_x;
12116 struct glyph *last;
12118 /* Don't extend on a previously drawn tool bar items (Bug#16058). */
12119 clear_glyph_row (row);
12120 row->enabled_p = true;
12121 row->y = it->current_y;
12123 /* Note that this isn't made use of if the face hasn't a box,
12124 so there's no need to check the face here. */
12125 it->start_of_box_run_p = 1;
12127 while (it->current_x < max_x)
12129 int x, n_glyphs_before, i, nglyphs;
12130 struct it it_before;
12132 /* Get the next display element. */
12133 if (!get_next_display_element (it))
12135 /* Don't count empty row if we are counting needed tool-bar lines. */
12136 if (height < 0 && !it->hpos)
12137 return;
12138 break;
12141 /* Produce glyphs. */
12142 n_glyphs_before = row->used[TEXT_AREA];
12143 it_before = *it;
12145 PRODUCE_GLYPHS (it);
12147 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
12148 i = 0;
12149 x = it_before.current_x;
12150 while (i < nglyphs)
12152 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
12154 if (x + glyph->pixel_width > max_x)
12156 /* Glyph doesn't fit on line. Backtrack. */
12157 row->used[TEXT_AREA] = n_glyphs_before;
12158 *it = it_before;
12159 /* If this is the only glyph on this line, it will never fit on the
12160 tool-bar, so skip it. But ensure there is at least one glyph,
12161 so we don't accidentally disable the tool-bar. */
12162 if (n_glyphs_before == 0
12163 && (it->vpos > 0 || IT_STRING_CHARPOS (*it) < it->end_charpos-1))
12164 break;
12165 goto out;
12168 ++it->hpos;
12169 x += glyph->pixel_width;
12170 ++i;
12173 /* Stop at line end. */
12174 if (ITERATOR_AT_END_OF_LINE_P (it))
12175 break;
12177 set_iterator_to_next (it, 1);
12180 out:;
12182 row->displays_text_p = row->used[TEXT_AREA] != 0;
12184 /* Use default face for the border below the tool bar.
12186 FIXME: When auto-resize-tool-bars is grow-only, there is
12187 no additional border below the possibly empty tool-bar lines.
12188 So to make the extra empty lines look "normal", we have to
12189 use the tool-bar face for the border too. */
12190 if (!MATRIX_ROW_DISPLAYS_TEXT_P (row)
12191 && !EQ (Vauto_resize_tool_bars, Qgrow_only))
12192 it->face_id = DEFAULT_FACE_ID;
12194 extend_face_to_end_of_line (it);
12195 last = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
12196 last->right_box_line_p = 1;
12197 if (last == row->glyphs[TEXT_AREA])
12198 last->left_box_line_p = 1;
12200 /* Make line the desired height and center it vertically. */
12201 if ((height -= it->max_ascent + it->max_descent) > 0)
12203 /* Don't add more than one line height. */
12204 height %= FRAME_LINE_HEIGHT (it->f);
12205 it->max_ascent += height / 2;
12206 it->max_descent += (height + 1) / 2;
12209 compute_line_metrics (it);
12211 /* If line is empty, make it occupy the rest of the tool-bar. */
12212 if (!MATRIX_ROW_DISPLAYS_TEXT_P (row))
12214 row->height = row->phys_height = it->last_visible_y - row->y;
12215 row->visible_height = row->height;
12216 row->ascent = row->phys_ascent = 0;
12217 row->extra_line_spacing = 0;
12220 row->full_width_p = 1;
12221 row->continued_p = 0;
12222 row->truncated_on_left_p = 0;
12223 row->truncated_on_right_p = 0;
12225 it->current_x = it->hpos = 0;
12226 it->current_y += row->height;
12227 ++it->vpos;
12228 ++it->glyph_row;
12232 /* Value is the number of pixels needed to make all tool-bar items of
12233 frame F visible. The actual number of glyph rows needed is
12234 returned in *N_ROWS if non-NULL. */
12235 static int
12236 tool_bar_height (struct frame *f, int *n_rows, bool pixelwise)
12238 struct window *w = XWINDOW (f->tool_bar_window);
12239 struct it it;
12240 /* tool_bar_height is called from redisplay_tool_bar after building
12241 the desired matrix, so use (unused) mode-line row as temporary row to
12242 avoid destroying the first tool-bar row. */
12243 struct glyph_row *temp_row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
12245 /* Initialize an iterator for iteration over
12246 F->desired_tool_bar_string in the tool-bar window of frame F. */
12247 init_iterator (&it, w, -1, -1, temp_row, TOOL_BAR_FACE_ID);
12248 temp_row->reversed_p = false;
12249 it.first_visible_x = 0;
12250 it.last_visible_x = WINDOW_PIXEL_WIDTH (w);
12251 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
12252 it.paragraph_embedding = L2R;
12254 while (!ITERATOR_AT_END_P (&it))
12256 clear_glyph_row (temp_row);
12257 it.glyph_row = temp_row;
12258 display_tool_bar_line (&it, -1);
12260 clear_glyph_row (temp_row);
12262 /* f->n_tool_bar_rows == 0 means "unknown"; -1 means no tool-bar. */
12263 if (n_rows)
12264 *n_rows = it.vpos > 0 ? it.vpos : -1;
12266 if (pixelwise)
12267 return it.current_y;
12268 else
12269 return (it.current_y + FRAME_LINE_HEIGHT (f) - 1) / FRAME_LINE_HEIGHT (f);
12272 #endif /* !USE_GTK && !HAVE_NS */
12274 DEFUN ("tool-bar-height", Ftool_bar_height, Stool_bar_height,
12275 0, 2, 0,
12276 doc: /* Return the number of lines occupied by the tool bar of FRAME.
12277 If FRAME is nil or omitted, use the selected frame. Optional argument
12278 PIXELWISE non-nil means return the height of the tool bar in pixels. */)
12279 (Lisp_Object frame, Lisp_Object pixelwise)
12281 int height = 0;
12283 #if ! defined (USE_GTK) && ! defined (HAVE_NS)
12284 struct frame *f = decode_any_frame (frame);
12286 if (WINDOWP (f->tool_bar_window)
12287 && WINDOW_PIXEL_HEIGHT (XWINDOW (f->tool_bar_window)) > 0)
12289 update_tool_bar (f, 1);
12290 if (f->n_tool_bar_items)
12292 build_desired_tool_bar_string (f);
12293 height = tool_bar_height (f, NULL, NILP (pixelwise) ? 0 : 1);
12296 #endif
12298 return make_number (height);
12302 /* Display the tool-bar of frame F. Value is non-zero if tool-bar's
12303 height should be changed. */
12304 static int
12305 redisplay_tool_bar (struct frame *f)
12307 #if defined (USE_GTK) || defined (HAVE_NS)
12309 if (FRAME_EXTERNAL_TOOL_BAR (f))
12310 update_frame_tool_bar (f);
12311 return 0;
12313 #else /* !USE_GTK && !HAVE_NS */
12315 struct window *w;
12316 struct it it;
12317 struct glyph_row *row;
12319 /* If frame hasn't a tool-bar window or if it is zero-height, don't
12320 do anything. This means you must start with tool-bar-lines
12321 non-zero to get the auto-sizing effect. Or in other words, you
12322 can turn off tool-bars by specifying tool-bar-lines zero. */
12323 if (!WINDOWP (f->tool_bar_window)
12324 || (w = XWINDOW (f->tool_bar_window),
12325 WINDOW_TOTAL_LINES (w) == 0))
12326 return 0;
12328 /* Set up an iterator for the tool-bar window. */
12329 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
12330 it.first_visible_x = 0;
12331 it.last_visible_x = WINDOW_PIXEL_WIDTH (w);
12332 row = it.glyph_row;
12333 row->reversed_p = false;
12335 /* Build a string that represents the contents of the tool-bar. */
12336 build_desired_tool_bar_string (f);
12337 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
12338 /* FIXME: This should be controlled by a user option. But it
12339 doesn't make sense to have an R2L tool bar if the menu bar cannot
12340 be drawn also R2L, and making the menu bar R2L is tricky due
12341 toolkit-specific code that implements it. If an R2L tool bar is
12342 ever supported, display_tool_bar_line should also be augmented to
12343 call unproduce_glyphs like display_line and display_string
12344 do. */
12345 it.paragraph_embedding = L2R;
12347 if (f->n_tool_bar_rows == 0)
12349 int new_height = tool_bar_height (f, &f->n_tool_bar_rows, 1);
12351 if (new_height != WINDOW_PIXEL_HEIGHT (w))
12353 x_change_tool_bar_height (f, new_height);
12354 /* Always do that now. */
12355 clear_glyph_matrix (w->desired_matrix);
12356 f->fonts_changed = 1;
12357 return 1;
12361 /* Display as many lines as needed to display all tool-bar items. */
12363 if (f->n_tool_bar_rows > 0)
12365 int border, rows, height, extra;
12367 if (TYPE_RANGED_INTEGERP (int, Vtool_bar_border))
12368 border = XINT (Vtool_bar_border);
12369 else if (EQ (Vtool_bar_border, Qinternal_border_width))
12370 border = FRAME_INTERNAL_BORDER_WIDTH (f);
12371 else if (EQ (Vtool_bar_border, Qborder_width))
12372 border = f->border_width;
12373 else
12374 border = 0;
12375 if (border < 0)
12376 border = 0;
12378 rows = f->n_tool_bar_rows;
12379 height = max (1, (it.last_visible_y - border) / rows);
12380 extra = it.last_visible_y - border - height * rows;
12382 while (it.current_y < it.last_visible_y)
12384 int h = 0;
12385 if (extra > 0 && rows-- > 0)
12387 h = (extra + rows - 1) / rows;
12388 extra -= h;
12390 display_tool_bar_line (&it, height + h);
12393 else
12395 while (it.current_y < it.last_visible_y)
12396 display_tool_bar_line (&it, 0);
12399 /* It doesn't make much sense to try scrolling in the tool-bar
12400 window, so don't do it. */
12401 w->desired_matrix->no_scrolling_p = 1;
12402 w->must_be_updated_p = 1;
12404 if (!NILP (Vauto_resize_tool_bars))
12406 int change_height_p = 0;
12408 /* If we couldn't display everything, change the tool-bar's
12409 height if there is room for more. */
12410 if (IT_STRING_CHARPOS (it) < it.end_charpos)
12411 change_height_p = 1;
12413 /* We subtract 1 because display_tool_bar_line advances the
12414 glyph_row pointer before returning to its caller. We want to
12415 examine the last glyph row produced by
12416 display_tool_bar_line. */
12417 row = it.glyph_row - 1;
12419 /* If there are blank lines at the end, except for a partially
12420 visible blank line at the end that is smaller than
12421 FRAME_LINE_HEIGHT, change the tool-bar's height. */
12422 if (!MATRIX_ROW_DISPLAYS_TEXT_P (row)
12423 && row->height >= FRAME_LINE_HEIGHT (f))
12424 change_height_p = 1;
12426 /* If row displays tool-bar items, but is partially visible,
12427 change the tool-bar's height. */
12428 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
12429 && MATRIX_ROW_BOTTOM_Y (row) > it.last_visible_y)
12430 change_height_p = 1;
12432 /* Resize windows as needed by changing the `tool-bar-lines'
12433 frame parameter. */
12434 if (change_height_p)
12436 int nrows;
12437 int new_height = tool_bar_height (f, &nrows, 1);
12439 change_height_p = ((EQ (Vauto_resize_tool_bars, Qgrow_only)
12440 && !f->minimize_tool_bar_window_p)
12441 ? (new_height > WINDOW_PIXEL_HEIGHT (w))
12442 : (new_height != WINDOW_PIXEL_HEIGHT (w)));
12443 f->minimize_tool_bar_window_p = 0;
12445 if (change_height_p)
12447 x_change_tool_bar_height (f, new_height);
12448 clear_glyph_matrix (w->desired_matrix);
12449 f->n_tool_bar_rows = nrows;
12450 f->fonts_changed = 1;
12452 return 1;
12457 f->minimize_tool_bar_window_p = 0;
12458 return 0;
12460 #endif /* USE_GTK || HAVE_NS */
12463 #if ! defined (USE_GTK) && ! defined (HAVE_NS)
12465 /* Get information about the tool-bar item which is displayed in GLYPH
12466 on frame F. Return in *PROP_IDX the index where tool-bar item
12467 properties start in F->tool_bar_items. Value is zero if
12468 GLYPH doesn't display a tool-bar item. */
12470 static int
12471 tool_bar_item_info (struct frame *f, struct glyph *glyph, int *prop_idx)
12473 Lisp_Object prop;
12474 int success_p;
12475 int charpos;
12477 /* This function can be called asynchronously, which means we must
12478 exclude any possibility that Fget_text_property signals an
12479 error. */
12480 charpos = min (SCHARS (f->current_tool_bar_string), glyph->charpos);
12481 charpos = max (0, charpos);
12483 /* Get the text property `menu-item' at pos. The value of that
12484 property is the start index of this item's properties in
12485 F->tool_bar_items. */
12486 prop = Fget_text_property (make_number (charpos),
12487 Qmenu_item, f->current_tool_bar_string);
12488 if (INTEGERP (prop))
12490 *prop_idx = XINT (prop);
12491 success_p = 1;
12493 else
12494 success_p = 0;
12496 return success_p;
12500 /* Get information about the tool-bar item at position X/Y on frame F.
12501 Return in *GLYPH a pointer to the glyph of the tool-bar item in
12502 the current matrix of the tool-bar window of F, or NULL if not
12503 on a tool-bar item. Return in *PROP_IDX the index of the tool-bar
12504 item in F->tool_bar_items. Value is
12506 -1 if X/Y is not on a tool-bar item
12507 0 if X/Y is on the same item that was highlighted before.
12508 1 otherwise. */
12510 static int
12511 get_tool_bar_item (struct frame *f, int x, int y, struct glyph **glyph,
12512 int *hpos, int *vpos, int *prop_idx)
12514 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12515 struct window *w = XWINDOW (f->tool_bar_window);
12516 int area;
12518 /* Find the glyph under X/Y. */
12519 *glyph = x_y_to_hpos_vpos (w, x, y, hpos, vpos, 0, 0, &area);
12520 if (*glyph == NULL)
12521 return -1;
12523 /* Get the start of this tool-bar item's properties in
12524 f->tool_bar_items. */
12525 if (!tool_bar_item_info (f, *glyph, prop_idx))
12526 return -1;
12528 /* Is mouse on the highlighted item? */
12529 if (EQ (f->tool_bar_window, hlinfo->mouse_face_window)
12530 && *vpos >= hlinfo->mouse_face_beg_row
12531 && *vpos <= hlinfo->mouse_face_end_row
12532 && (*vpos > hlinfo->mouse_face_beg_row
12533 || *hpos >= hlinfo->mouse_face_beg_col)
12534 && (*vpos < hlinfo->mouse_face_end_row
12535 || *hpos < hlinfo->mouse_face_end_col
12536 || hlinfo->mouse_face_past_end))
12537 return 0;
12539 return 1;
12543 /* EXPORT:
12544 Handle mouse button event on the tool-bar of frame F, at
12545 frame-relative coordinates X/Y. DOWN_P is 1 for a button press,
12546 0 for button release. MODIFIERS is event modifiers for button
12547 release. */
12549 void
12550 handle_tool_bar_click (struct frame *f, int x, int y, int down_p,
12551 int modifiers)
12553 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12554 struct window *w = XWINDOW (f->tool_bar_window);
12555 int hpos, vpos, prop_idx;
12556 struct glyph *glyph;
12557 Lisp_Object enabled_p;
12558 int ts;
12560 /* If not on the highlighted tool-bar item, and mouse-highlight is
12561 non-nil, return. This is so we generate the tool-bar button
12562 click only when the mouse button is released on the same item as
12563 where it was pressed. However, when mouse-highlight is disabled,
12564 generate the click when the button is released regardless of the
12565 highlight, since tool-bar items are not highlighted in that
12566 case. */
12567 frame_to_window_pixel_xy (w, &x, &y);
12568 ts = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
12569 if (ts == -1
12570 || (ts != 0 && !NILP (Vmouse_highlight)))
12571 return;
12573 /* When mouse-highlight is off, generate the click for the item
12574 where the button was pressed, disregarding where it was
12575 released. */
12576 if (NILP (Vmouse_highlight) && !down_p)
12577 prop_idx = f->last_tool_bar_item;
12579 /* If item is disabled, do nothing. */
12580 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
12581 if (NILP (enabled_p))
12582 return;
12584 if (down_p)
12586 /* Show item in pressed state. */
12587 if (!NILP (Vmouse_highlight))
12588 show_mouse_face (hlinfo, DRAW_IMAGE_SUNKEN);
12589 f->last_tool_bar_item = prop_idx;
12591 else
12593 Lisp_Object key, frame;
12594 struct input_event event;
12595 EVENT_INIT (event);
12597 /* Show item in released state. */
12598 if (!NILP (Vmouse_highlight))
12599 show_mouse_face (hlinfo, DRAW_IMAGE_RAISED);
12601 key = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_KEY);
12603 XSETFRAME (frame, f);
12604 event.kind = TOOL_BAR_EVENT;
12605 event.frame_or_window = frame;
12606 event.arg = frame;
12607 kbd_buffer_store_event (&event);
12609 event.kind = TOOL_BAR_EVENT;
12610 event.frame_or_window = frame;
12611 event.arg = key;
12612 event.modifiers = modifiers;
12613 kbd_buffer_store_event (&event);
12614 f->last_tool_bar_item = -1;
12619 /* Possibly highlight a tool-bar item on frame F when mouse moves to
12620 tool-bar window-relative coordinates X/Y. Called from
12621 note_mouse_highlight. */
12623 static void
12624 note_tool_bar_highlight (struct frame *f, int x, int y)
12626 Lisp_Object window = f->tool_bar_window;
12627 struct window *w = XWINDOW (window);
12628 Display_Info *dpyinfo = FRAME_DISPLAY_INFO (f);
12629 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12630 int hpos, vpos;
12631 struct glyph *glyph;
12632 struct glyph_row *row;
12633 int i;
12634 Lisp_Object enabled_p;
12635 int prop_idx;
12636 enum draw_glyphs_face draw = DRAW_IMAGE_RAISED;
12637 int mouse_down_p, rc;
12639 /* Function note_mouse_highlight is called with negative X/Y
12640 values when mouse moves outside of the frame. */
12641 if (x <= 0 || y <= 0)
12643 clear_mouse_face (hlinfo);
12644 return;
12647 rc = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
12648 if (rc < 0)
12650 /* Not on tool-bar item. */
12651 clear_mouse_face (hlinfo);
12652 return;
12654 else if (rc == 0)
12655 /* On same tool-bar item as before. */
12656 goto set_help_echo;
12658 clear_mouse_face (hlinfo);
12660 /* Mouse is down, but on different tool-bar item? */
12661 mouse_down_p = (x_mouse_grabbed (dpyinfo)
12662 && f == dpyinfo->last_mouse_frame);
12664 if (mouse_down_p && f->last_tool_bar_item != prop_idx)
12665 return;
12667 draw = mouse_down_p ? DRAW_IMAGE_SUNKEN : DRAW_IMAGE_RAISED;
12669 /* If tool-bar item is not enabled, don't highlight it. */
12670 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
12671 if (!NILP (enabled_p) && !NILP (Vmouse_highlight))
12673 /* Compute the x-position of the glyph. In front and past the
12674 image is a space. We include this in the highlighted area. */
12675 row = MATRIX_ROW (w->current_matrix, vpos);
12676 for (i = x = 0; i < hpos; ++i)
12677 x += row->glyphs[TEXT_AREA][i].pixel_width;
12679 /* Record this as the current active region. */
12680 hlinfo->mouse_face_beg_col = hpos;
12681 hlinfo->mouse_face_beg_row = vpos;
12682 hlinfo->mouse_face_beg_x = x;
12683 hlinfo->mouse_face_past_end = 0;
12685 hlinfo->mouse_face_end_col = hpos + 1;
12686 hlinfo->mouse_face_end_row = vpos;
12687 hlinfo->mouse_face_end_x = x + glyph->pixel_width;
12688 hlinfo->mouse_face_window = window;
12689 hlinfo->mouse_face_face_id = TOOL_BAR_FACE_ID;
12691 /* Display it as active. */
12692 show_mouse_face (hlinfo, draw);
12695 set_help_echo:
12697 /* Set help_echo_string to a help string to display for this tool-bar item.
12698 XTread_socket does the rest. */
12699 help_echo_object = help_echo_window = Qnil;
12700 help_echo_pos = -1;
12701 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_HELP);
12702 if (NILP (help_echo_string))
12703 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_CAPTION);
12706 #endif /* !USE_GTK && !HAVE_NS */
12708 #endif /* HAVE_WINDOW_SYSTEM */
12712 /************************************************************************
12713 Horizontal scrolling
12714 ************************************************************************/
12716 static int hscroll_window_tree (Lisp_Object);
12717 static int hscroll_windows (Lisp_Object);
12719 /* For all leaf windows in the window tree rooted at WINDOW, set their
12720 hscroll value so that PT is (i) visible in the window, and (ii) so
12721 that it is not within a certain margin at the window's left and
12722 right border. Value is non-zero if any window's hscroll has been
12723 changed. */
12725 static int
12726 hscroll_window_tree (Lisp_Object window)
12728 int hscrolled_p = 0;
12729 int hscroll_relative_p = FLOATP (Vhscroll_step);
12730 int hscroll_step_abs = 0;
12731 double hscroll_step_rel = 0;
12733 if (hscroll_relative_p)
12735 hscroll_step_rel = XFLOAT_DATA (Vhscroll_step);
12736 if (hscroll_step_rel < 0)
12738 hscroll_relative_p = 0;
12739 hscroll_step_abs = 0;
12742 else if (TYPE_RANGED_INTEGERP (int, Vhscroll_step))
12744 hscroll_step_abs = XINT (Vhscroll_step);
12745 if (hscroll_step_abs < 0)
12746 hscroll_step_abs = 0;
12748 else
12749 hscroll_step_abs = 0;
12751 while (WINDOWP (window))
12753 struct window *w = XWINDOW (window);
12755 if (WINDOWP (w->contents))
12756 hscrolled_p |= hscroll_window_tree (w->contents);
12757 else if (w->cursor.vpos >= 0)
12759 int h_margin;
12760 int text_area_width;
12761 struct glyph_row *cursor_row;
12762 struct glyph_row *bottom_row;
12763 int row_r2l_p;
12765 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->desired_matrix, w);
12766 if (w->cursor.vpos < bottom_row - w->desired_matrix->rows)
12767 cursor_row = MATRIX_ROW (w->desired_matrix, w->cursor.vpos);
12768 else
12769 cursor_row = bottom_row - 1;
12771 if (!cursor_row->enabled_p)
12773 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
12774 if (w->cursor.vpos < bottom_row - w->current_matrix->rows)
12775 cursor_row = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
12776 else
12777 cursor_row = bottom_row - 1;
12779 row_r2l_p = cursor_row->reversed_p;
12781 text_area_width = window_box_width (w, TEXT_AREA);
12783 /* Scroll when cursor is inside this scroll margin. */
12784 h_margin = hscroll_margin * WINDOW_FRAME_COLUMN_WIDTH (w);
12786 /* If the position of this window's point has explicitly
12787 changed, no more suspend auto hscrolling. */
12788 if (NILP (Fequal (Fwindow_point (window), Fwindow_old_point (window))))
12789 w->suspend_auto_hscroll = 0;
12791 /* Remember window point. */
12792 Fset_marker (w->old_pointm,
12793 ((w == XWINDOW (selected_window))
12794 ? make_number (BUF_PT (XBUFFER (w->contents)))
12795 : Fmarker_position (w->pointm)),
12796 w->contents);
12798 if (!NILP (Fbuffer_local_value (Qauto_hscroll_mode, w->contents))
12799 && w->suspend_auto_hscroll == 0
12800 /* In some pathological cases, like restoring a window
12801 configuration into a frame that is much smaller than
12802 the one from which the configuration was saved, we
12803 get glyph rows whose start and end have zero buffer
12804 positions, which we cannot handle below. Just skip
12805 such windows. */
12806 && CHARPOS (cursor_row->start.pos) >= BUF_BEG (w->contents)
12807 /* For left-to-right rows, hscroll when cursor is either
12808 (i) inside the right hscroll margin, or (ii) if it is
12809 inside the left margin and the window is already
12810 hscrolled. */
12811 && ((!row_r2l_p
12812 && ((w->hscroll && w->cursor.x <= h_margin)
12813 || (cursor_row->enabled_p
12814 && cursor_row->truncated_on_right_p
12815 && (w->cursor.x >= text_area_width - h_margin))))
12816 /* For right-to-left rows, the logic is similar,
12817 except that rules for scrolling to left and right
12818 are reversed. E.g., if cursor.x <= h_margin, we
12819 need to hscroll "to the right" unconditionally,
12820 and that will scroll the screen to the left so as
12821 to reveal the next portion of the row. */
12822 || (row_r2l_p
12823 && ((cursor_row->enabled_p
12824 /* FIXME: It is confusing to set the
12825 truncated_on_right_p flag when R2L rows
12826 are actually truncated on the left. */
12827 && cursor_row->truncated_on_right_p
12828 && w->cursor.x <= h_margin)
12829 || (w->hscroll
12830 && (w->cursor.x >= text_area_width - h_margin))))))
12832 struct it it;
12833 ptrdiff_t hscroll;
12834 struct buffer *saved_current_buffer;
12835 ptrdiff_t pt;
12836 int wanted_x;
12838 /* Find point in a display of infinite width. */
12839 saved_current_buffer = current_buffer;
12840 current_buffer = XBUFFER (w->contents);
12842 if (w == XWINDOW (selected_window))
12843 pt = PT;
12844 else
12845 pt = clip_to_bounds (BEGV, marker_position (w->pointm), ZV);
12847 /* Move iterator to pt starting at cursor_row->start in
12848 a line with infinite width. */
12849 init_to_row_start (&it, w, cursor_row);
12850 it.last_visible_x = INFINITY;
12851 move_it_in_display_line_to (&it, pt, -1, MOVE_TO_POS);
12852 current_buffer = saved_current_buffer;
12854 /* Position cursor in window. */
12855 if (!hscroll_relative_p && hscroll_step_abs == 0)
12856 hscroll = max (0, (it.current_x
12857 - (ITERATOR_AT_END_OF_LINE_P (&it)
12858 ? (text_area_width - 4 * FRAME_COLUMN_WIDTH (it.f))
12859 : (text_area_width / 2))))
12860 / FRAME_COLUMN_WIDTH (it.f);
12861 else if ((!row_r2l_p
12862 && w->cursor.x >= text_area_width - h_margin)
12863 || (row_r2l_p && w->cursor.x <= h_margin))
12865 if (hscroll_relative_p)
12866 wanted_x = text_area_width * (1 - hscroll_step_rel)
12867 - h_margin;
12868 else
12869 wanted_x = text_area_width
12870 - hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
12871 - h_margin;
12872 hscroll
12873 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
12875 else
12877 if (hscroll_relative_p)
12878 wanted_x = text_area_width * hscroll_step_rel
12879 + h_margin;
12880 else
12881 wanted_x = hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
12882 + h_margin;
12883 hscroll
12884 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
12886 hscroll = max (hscroll, w->min_hscroll);
12888 /* Don't prevent redisplay optimizations if hscroll
12889 hasn't changed, as it will unnecessarily slow down
12890 redisplay. */
12891 if (w->hscroll != hscroll)
12893 XBUFFER (w->contents)->prevent_redisplay_optimizations_p = 1;
12894 w->hscroll = hscroll;
12895 hscrolled_p = 1;
12900 window = w->next;
12903 /* Value is non-zero if hscroll of any leaf window has been changed. */
12904 return hscrolled_p;
12908 /* Set hscroll so that cursor is visible and not inside horizontal
12909 scroll margins for all windows in the tree rooted at WINDOW. See
12910 also hscroll_window_tree above. Value is non-zero if any window's
12911 hscroll has been changed. If it has, desired matrices on the frame
12912 of WINDOW are cleared. */
12914 static int
12915 hscroll_windows (Lisp_Object window)
12917 int hscrolled_p = hscroll_window_tree (window);
12918 if (hscrolled_p)
12919 clear_desired_matrices (XFRAME (WINDOW_FRAME (XWINDOW (window))));
12920 return hscrolled_p;
12925 /************************************************************************
12926 Redisplay
12927 ************************************************************************/
12929 /* Variables holding some state of redisplay if GLYPH_DEBUG is defined
12930 to a non-zero value. This is sometimes handy to have in a debugger
12931 session. */
12933 #ifdef GLYPH_DEBUG
12935 /* First and last unchanged row for try_window_id. */
12937 static int debug_first_unchanged_at_end_vpos;
12938 static int debug_last_unchanged_at_beg_vpos;
12940 /* Delta vpos and y. */
12942 static int debug_dvpos, debug_dy;
12944 /* Delta in characters and bytes for try_window_id. */
12946 static ptrdiff_t debug_delta, debug_delta_bytes;
12948 /* Values of window_end_pos and window_end_vpos at the end of
12949 try_window_id. */
12951 static ptrdiff_t debug_end_vpos;
12953 /* Append a string to W->desired_matrix->method. FMT is a printf
12954 format string. If trace_redisplay_p is true also printf the
12955 resulting string to stderr. */
12957 static void debug_method_add (struct window *, char const *, ...)
12958 ATTRIBUTE_FORMAT_PRINTF (2, 3);
12960 static void
12961 debug_method_add (struct window *w, char const *fmt, ...)
12963 void *ptr = w;
12964 char *method = w->desired_matrix->method;
12965 int len = strlen (method);
12966 int size = sizeof w->desired_matrix->method;
12967 int remaining = size - len - 1;
12968 va_list ap;
12970 if (len && remaining)
12972 method[len] = '|';
12973 --remaining, ++len;
12976 va_start (ap, fmt);
12977 vsnprintf (method + len, remaining + 1, fmt, ap);
12978 va_end (ap);
12980 if (trace_redisplay_p)
12981 fprintf (stderr, "%p (%s): %s\n",
12982 ptr,
12983 ((BUFFERP (w->contents)
12984 && STRINGP (BVAR (XBUFFER (w->contents), name)))
12985 ? SSDATA (BVAR (XBUFFER (w->contents), name))
12986 : "no buffer"),
12987 method + len);
12990 #endif /* GLYPH_DEBUG */
12993 /* Value is non-zero if all changes in window W, which displays
12994 current_buffer, are in the text between START and END. START is a
12995 buffer position, END is given as a distance from Z. Used in
12996 redisplay_internal for display optimization. */
12998 static int
12999 text_outside_line_unchanged_p (struct window *w,
13000 ptrdiff_t start, ptrdiff_t end)
13002 int unchanged_p = 1;
13004 /* If text or overlays have changed, see where. */
13005 if (window_outdated (w))
13007 /* Gap in the line? */
13008 if (GPT < start || Z - GPT < end)
13009 unchanged_p = 0;
13011 /* Changes start in front of the line, or end after it? */
13012 if (unchanged_p
13013 && (BEG_UNCHANGED < start - 1
13014 || END_UNCHANGED < end))
13015 unchanged_p = 0;
13017 /* If selective display, can't optimize if changes start at the
13018 beginning of the line. */
13019 if (unchanged_p
13020 && INTEGERP (BVAR (current_buffer, selective_display))
13021 && XINT (BVAR (current_buffer, selective_display)) > 0
13022 && (BEG_UNCHANGED < start || GPT <= start))
13023 unchanged_p = 0;
13025 /* If there are overlays at the start or end of the line, these
13026 may have overlay strings with newlines in them. A change at
13027 START, for instance, may actually concern the display of such
13028 overlay strings as well, and they are displayed on different
13029 lines. So, quickly rule out this case. (For the future, it
13030 might be desirable to implement something more telling than
13031 just BEG/END_UNCHANGED.) */
13032 if (unchanged_p)
13034 if (BEG + BEG_UNCHANGED == start
13035 && overlay_touches_p (start))
13036 unchanged_p = 0;
13037 if (END_UNCHANGED == end
13038 && overlay_touches_p (Z - end))
13039 unchanged_p = 0;
13042 /* Under bidi reordering, adding or deleting a character in the
13043 beginning of a paragraph, before the first strong directional
13044 character, can change the base direction of the paragraph (unless
13045 the buffer specifies a fixed paragraph direction), which will
13046 require to redisplay the whole paragraph. It might be worthwhile
13047 to find the paragraph limits and widen the range of redisplayed
13048 lines to that, but for now just give up this optimization. */
13049 if (!NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering))
13050 && NILP (BVAR (XBUFFER (w->contents), bidi_paragraph_direction)))
13051 unchanged_p = 0;
13054 return unchanged_p;
13058 /* Do a frame update, taking possible shortcuts into account. This is
13059 the main external entry point for redisplay.
13061 If the last redisplay displayed an echo area message and that message
13062 is no longer requested, we clear the echo area or bring back the
13063 mini-buffer if that is in use. */
13065 void
13066 redisplay (void)
13068 redisplay_internal ();
13072 static Lisp_Object
13073 overlay_arrow_string_or_property (Lisp_Object var)
13075 Lisp_Object val;
13077 if (val = Fget (var, Qoverlay_arrow_string), STRINGP (val))
13078 return val;
13080 return Voverlay_arrow_string;
13083 /* Return 1 if there are any overlay-arrows in current_buffer. */
13084 static int
13085 overlay_arrow_in_current_buffer_p (void)
13087 Lisp_Object vlist;
13089 for (vlist = Voverlay_arrow_variable_list;
13090 CONSP (vlist);
13091 vlist = XCDR (vlist))
13093 Lisp_Object var = XCAR (vlist);
13094 Lisp_Object val;
13096 if (!SYMBOLP (var))
13097 continue;
13098 val = find_symbol_value (var);
13099 if (MARKERP (val)
13100 && current_buffer == XMARKER (val)->buffer)
13101 return 1;
13103 return 0;
13107 /* Return 1 if any overlay_arrows have moved or overlay-arrow-string
13108 has changed. */
13110 static int
13111 overlay_arrows_changed_p (void)
13113 Lisp_Object vlist;
13115 for (vlist = Voverlay_arrow_variable_list;
13116 CONSP (vlist);
13117 vlist = XCDR (vlist))
13119 Lisp_Object var = XCAR (vlist);
13120 Lisp_Object val, pstr;
13122 if (!SYMBOLP (var))
13123 continue;
13124 val = find_symbol_value (var);
13125 if (!MARKERP (val))
13126 continue;
13127 if (! EQ (COERCE_MARKER (val),
13128 Fget (var, Qlast_arrow_position))
13129 || ! (pstr = overlay_arrow_string_or_property (var),
13130 EQ (pstr, Fget (var, Qlast_arrow_string))))
13131 return 1;
13133 return 0;
13136 /* Mark overlay arrows to be updated on next redisplay. */
13138 static void
13139 update_overlay_arrows (int up_to_date)
13141 Lisp_Object vlist;
13143 for (vlist = Voverlay_arrow_variable_list;
13144 CONSP (vlist);
13145 vlist = XCDR (vlist))
13147 Lisp_Object var = XCAR (vlist);
13149 if (!SYMBOLP (var))
13150 continue;
13152 if (up_to_date > 0)
13154 Lisp_Object val = find_symbol_value (var);
13155 Fput (var, Qlast_arrow_position,
13156 COERCE_MARKER (val));
13157 Fput (var, Qlast_arrow_string,
13158 overlay_arrow_string_or_property (var));
13160 else if (up_to_date < 0
13161 || !NILP (Fget (var, Qlast_arrow_position)))
13163 Fput (var, Qlast_arrow_position, Qt);
13164 Fput (var, Qlast_arrow_string, Qt);
13170 /* Return overlay arrow string to display at row.
13171 Return integer (bitmap number) for arrow bitmap in left fringe.
13172 Return nil if no overlay arrow. */
13174 static Lisp_Object
13175 overlay_arrow_at_row (struct it *it, struct glyph_row *row)
13177 Lisp_Object vlist;
13179 for (vlist = Voverlay_arrow_variable_list;
13180 CONSP (vlist);
13181 vlist = XCDR (vlist))
13183 Lisp_Object var = XCAR (vlist);
13184 Lisp_Object val;
13186 if (!SYMBOLP (var))
13187 continue;
13189 val = find_symbol_value (var);
13191 if (MARKERP (val)
13192 && current_buffer == XMARKER (val)->buffer
13193 && (MATRIX_ROW_START_CHARPOS (row) == marker_position (val)))
13195 if (FRAME_WINDOW_P (it->f)
13196 /* FIXME: if ROW->reversed_p is set, this should test
13197 the right fringe, not the left one. */
13198 && WINDOW_LEFT_FRINGE_WIDTH (it->w) > 0)
13200 #ifdef HAVE_WINDOW_SYSTEM
13201 if (val = Fget (var, Qoverlay_arrow_bitmap), SYMBOLP (val))
13203 int fringe_bitmap;
13204 if ((fringe_bitmap = lookup_fringe_bitmap (val)) != 0)
13205 return make_number (fringe_bitmap);
13207 #endif
13208 return make_number (-1); /* Use default arrow bitmap. */
13210 return overlay_arrow_string_or_property (var);
13214 return Qnil;
13217 /* Return 1 if point moved out of or into a composition. Otherwise
13218 return 0. PREV_BUF and PREV_PT are the last point buffer and
13219 position. BUF and PT are the current point buffer and position. */
13221 static int
13222 check_point_in_composition (struct buffer *prev_buf, ptrdiff_t prev_pt,
13223 struct buffer *buf, ptrdiff_t pt)
13225 ptrdiff_t start, end;
13226 Lisp_Object prop;
13227 Lisp_Object buffer;
13229 XSETBUFFER (buffer, buf);
13230 /* Check a composition at the last point if point moved within the
13231 same buffer. */
13232 if (prev_buf == buf)
13234 if (prev_pt == pt)
13235 /* Point didn't move. */
13236 return 0;
13238 if (prev_pt > BUF_BEGV (buf) && prev_pt < BUF_ZV (buf)
13239 && find_composition (prev_pt, -1, &start, &end, &prop, buffer)
13240 && composition_valid_p (start, end, prop)
13241 && start < prev_pt && end > prev_pt)
13242 /* The last point was within the composition. Return 1 iff
13243 point moved out of the composition. */
13244 return (pt <= start || pt >= end);
13247 /* Check a composition at the current point. */
13248 return (pt > BUF_BEGV (buf) && pt < BUF_ZV (buf)
13249 && find_composition (pt, -1, &start, &end, &prop, buffer)
13250 && composition_valid_p (start, end, prop)
13251 && start < pt && end > pt);
13254 /* Reconsider the clip changes of buffer which is displayed in W. */
13256 static void
13257 reconsider_clip_changes (struct window *w)
13259 struct buffer *b = XBUFFER (w->contents);
13261 if (b->clip_changed
13262 && w->window_end_valid
13263 && w->current_matrix->buffer == b
13264 && w->current_matrix->zv == BUF_ZV (b)
13265 && w->current_matrix->begv == BUF_BEGV (b))
13266 b->clip_changed = 0;
13268 /* If display wasn't paused, and W is not a tool bar window, see if
13269 point has been moved into or out of a composition. In that case,
13270 we set b->clip_changed to 1 to force updating the screen. If
13271 b->clip_changed has already been set to 1, we can skip this
13272 check. */
13273 if (!b->clip_changed && w->window_end_valid)
13275 ptrdiff_t pt = (w == XWINDOW (selected_window)
13276 ? PT : marker_position (w->pointm));
13278 if ((w->current_matrix->buffer != b || pt != w->last_point)
13279 && check_point_in_composition (w->current_matrix->buffer,
13280 w->last_point, b, pt))
13281 b->clip_changed = 1;
13285 static void
13286 propagate_buffer_redisplay (void)
13287 { /* Resetting b->text->redisplay is problematic!
13288 We can't just reset it in the case that some window that displays
13289 it has not been redisplayed; and such a window can stay
13290 unredisplayed for a long time if it's currently invisible.
13291 But we do want to reset it at the end of redisplay otherwise
13292 its displayed windows will keep being redisplayed over and over
13293 again.
13294 So we copy all b->text->redisplay flags up to their windows here,
13295 such that mark_window_display_accurate can safely reset
13296 b->text->redisplay. */
13297 Lisp_Object ws = window_list ();
13298 for (; CONSP (ws); ws = XCDR (ws))
13300 struct window *thisw = XWINDOW (XCAR (ws));
13301 struct buffer *thisb = XBUFFER (thisw->contents);
13302 if (thisb->text->redisplay)
13303 thisw->redisplay = true;
13307 #define STOP_POLLING \
13308 do { if (! polling_stopped_here) stop_polling (); \
13309 polling_stopped_here = 1; } while (0)
13311 #define RESUME_POLLING \
13312 do { if (polling_stopped_here) start_polling (); \
13313 polling_stopped_here = 0; } while (0)
13316 /* Perhaps in the future avoid recentering windows if it
13317 is not necessary; currently that causes some problems. */
13319 static void
13320 redisplay_internal (void)
13322 struct window *w = XWINDOW (selected_window);
13323 struct window *sw;
13324 struct frame *fr;
13325 bool pending;
13326 bool must_finish = 0, match_p;
13327 struct text_pos tlbufpos, tlendpos;
13328 int number_of_visible_frames;
13329 ptrdiff_t count;
13330 struct frame *sf;
13331 int polling_stopped_here = 0;
13332 Lisp_Object tail, frame;
13334 /* True means redisplay has to consider all windows on all
13335 frames. False, only selected_window is considered. */
13336 bool consider_all_windows_p;
13338 /* True means redisplay has to redisplay the miniwindow. */
13339 bool update_miniwindow_p = false;
13341 TRACE ((stderr, "redisplay_internal %d\n", redisplaying_p));
13343 /* No redisplay if running in batch mode or frame is not yet fully
13344 initialized, or redisplay is explicitly turned off by setting
13345 Vinhibit_redisplay. */
13346 if (FRAME_INITIAL_P (SELECTED_FRAME ())
13347 || !NILP (Vinhibit_redisplay))
13348 return;
13350 /* Don't examine these until after testing Vinhibit_redisplay.
13351 When Emacs is shutting down, perhaps because its connection to
13352 X has dropped, we should not look at them at all. */
13353 fr = XFRAME (w->frame);
13354 sf = SELECTED_FRAME ();
13356 if (!fr->glyphs_initialized_p)
13357 return;
13359 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS)
13360 if (popup_activated ())
13361 return;
13362 #endif
13364 /* I don't think this happens but let's be paranoid. */
13365 if (redisplaying_p)
13366 return;
13368 /* Record a function that clears redisplaying_p
13369 when we leave this function. */
13370 count = SPECPDL_INDEX ();
13371 record_unwind_protect_void (unwind_redisplay);
13372 redisplaying_p = 1;
13373 specbind (Qinhibit_free_realized_faces, Qnil);
13375 /* Record this function, so it appears on the profiler's backtraces. */
13376 record_in_backtrace (Qredisplay_internal, 0, 0);
13378 FOR_EACH_FRAME (tail, frame)
13379 XFRAME (frame)->already_hscrolled_p = 0;
13381 retry:
13382 /* Remember the currently selected window. */
13383 sw = w;
13385 pending = false;
13386 last_escape_glyph_frame = NULL;
13387 last_escape_glyph_face_id = (1 << FACE_ID_BITS);
13388 last_glyphless_glyph_frame = NULL;
13389 last_glyphless_glyph_face_id = (1 << FACE_ID_BITS);
13391 /* If face_change_count is non-zero, init_iterator will free all
13392 realized faces, which includes the faces referenced from current
13393 matrices. So, we can't reuse current matrices in this case. */
13394 if (face_change_count)
13395 windows_or_buffers_changed = 47;
13397 if ((FRAME_TERMCAP_P (sf) || FRAME_MSDOS_P (sf))
13398 && FRAME_TTY (sf)->previous_frame != sf)
13400 /* Since frames on a single ASCII terminal share the same
13401 display area, displaying a different frame means redisplay
13402 the whole thing. */
13403 SET_FRAME_GARBAGED (sf);
13404 #ifndef DOS_NT
13405 set_tty_color_mode (FRAME_TTY (sf), sf);
13406 #endif
13407 FRAME_TTY (sf)->previous_frame = sf;
13410 /* Set the visible flags for all frames. Do this before checking for
13411 resized or garbaged frames; they want to know if their frames are
13412 visible. See the comment in frame.h for FRAME_SAMPLE_VISIBILITY. */
13413 number_of_visible_frames = 0;
13415 FOR_EACH_FRAME (tail, frame)
13417 struct frame *f = XFRAME (frame);
13419 if (FRAME_VISIBLE_P (f))
13421 ++number_of_visible_frames;
13422 /* Adjust matrices for visible frames only. */
13423 if (f->fonts_changed)
13425 adjust_frame_glyphs (f);
13426 f->fonts_changed = 0;
13428 /* If cursor type has been changed on the frame
13429 other than selected, consider all frames. */
13430 if (f != sf && f->cursor_type_changed)
13431 update_mode_lines = 31;
13433 clear_desired_matrices (f);
13436 /* Notice any pending interrupt request to change frame size. */
13437 do_pending_window_change (true);
13439 /* do_pending_window_change could change the selected_window due to
13440 frame resizing which makes the selected window too small. */
13441 if (WINDOWP (selected_window) && (w = XWINDOW (selected_window)) != sw)
13442 sw = w;
13444 /* Clear frames marked as garbaged. */
13445 clear_garbaged_frames ();
13447 /* Build menubar and tool-bar items. */
13448 if (NILP (Vmemory_full))
13449 prepare_menu_bars ();
13451 reconsider_clip_changes (w);
13453 /* In most cases selected window displays current buffer. */
13454 match_p = XBUFFER (w->contents) == current_buffer;
13455 if (match_p)
13457 /* Detect case that we need to write or remove a star in the mode line. */
13458 if ((SAVE_MODIFF < MODIFF) != w->last_had_star)
13459 w->update_mode_line = 1;
13461 if (mode_line_update_needed (w))
13462 w->update_mode_line = 1;
13464 /* If reconsider_clip_changes above decided that the narrowing
13465 in the current buffer changed, make sure all other windows
13466 showing that buffer will be redisplayed. */
13467 if (current_buffer->clip_changed)
13468 bset_update_mode_line (current_buffer);
13471 /* Normally the message* functions will have already displayed and
13472 updated the echo area, but the frame may have been trashed, or
13473 the update may have been preempted, so display the echo area
13474 again here. Checking message_cleared_p captures the case that
13475 the echo area should be cleared. */
13476 if ((!NILP (echo_area_buffer[0]) && !display_last_displayed_message_p)
13477 || (!NILP (echo_area_buffer[1]) && display_last_displayed_message_p)
13478 || (message_cleared_p
13479 && minibuf_level == 0
13480 /* If the mini-window is currently selected, this means the
13481 echo-area doesn't show through. */
13482 && !MINI_WINDOW_P (XWINDOW (selected_window))))
13484 int window_height_changed_p = echo_area_display (false);
13486 if (message_cleared_p)
13487 update_miniwindow_p = true;
13489 must_finish = 1;
13491 /* If we don't display the current message, don't clear the
13492 message_cleared_p flag, because, if we did, we wouldn't clear
13493 the echo area in the next redisplay which doesn't preserve
13494 the echo area. */
13495 if (!display_last_displayed_message_p)
13496 message_cleared_p = 0;
13498 if (window_height_changed_p)
13500 windows_or_buffers_changed = 50;
13502 /* If window configuration was changed, frames may have been
13503 marked garbaged. Clear them or we will experience
13504 surprises wrt scrolling. */
13505 clear_garbaged_frames ();
13508 else if (EQ (selected_window, minibuf_window)
13509 && (current_buffer->clip_changed || window_outdated (w))
13510 && resize_mini_window (w, 0))
13512 /* Resized active mini-window to fit the size of what it is
13513 showing if its contents might have changed. */
13514 must_finish = 1;
13516 /* If window configuration was changed, frames may have been
13517 marked garbaged. Clear them or we will experience
13518 surprises wrt scrolling. */
13519 clear_garbaged_frames ();
13522 if (windows_or_buffers_changed && !update_mode_lines)
13523 /* Code that sets windows_or_buffers_changed doesn't distinguish whether
13524 only the windows's contents needs to be refreshed, or whether the
13525 mode-lines also need a refresh. */
13526 update_mode_lines = (windows_or_buffers_changed == REDISPLAY_SOME
13527 ? REDISPLAY_SOME : 32);
13529 /* If specs for an arrow have changed, do thorough redisplay
13530 to ensure we remove any arrow that should no longer exist. */
13531 if (overlay_arrows_changed_p ())
13532 /* Apparently, this is the only case where we update other windows,
13533 without updating other mode-lines. */
13534 windows_or_buffers_changed = 49;
13536 consider_all_windows_p = (update_mode_lines
13537 || windows_or_buffers_changed);
13539 #define AINC(a,i) \
13540 if (VECTORP (a) && i >= 0 && i < ASIZE (a) && INTEGERP (AREF (a, i))) \
13541 ASET (a, i, make_number (1 + XINT (AREF (a, i))))
13543 AINC (Vredisplay__all_windows_cause, windows_or_buffers_changed);
13544 AINC (Vredisplay__mode_lines_cause, update_mode_lines);
13546 /* Optimize the case that only the line containing the cursor in the
13547 selected window has changed. Variables starting with this_ are
13548 set in display_line and record information about the line
13549 containing the cursor. */
13550 tlbufpos = this_line_start_pos;
13551 tlendpos = this_line_end_pos;
13552 if (!consider_all_windows_p
13553 && CHARPOS (tlbufpos) > 0
13554 && !w->update_mode_line
13555 && !current_buffer->clip_changed
13556 && !current_buffer->prevent_redisplay_optimizations_p
13557 && FRAME_VISIBLE_P (XFRAME (w->frame))
13558 && !FRAME_OBSCURED_P (XFRAME (w->frame))
13559 && !XFRAME (w->frame)->cursor_type_changed
13560 /* Make sure recorded data applies to current buffer, etc. */
13561 && this_line_buffer == current_buffer
13562 && match_p
13563 && !w->force_start
13564 && !w->optional_new_start
13565 /* Point must be on the line that we have info recorded about. */
13566 && PT >= CHARPOS (tlbufpos)
13567 && PT <= Z - CHARPOS (tlendpos)
13568 /* All text outside that line, including its final newline,
13569 must be unchanged. */
13570 && text_outside_line_unchanged_p (w, CHARPOS (tlbufpos),
13571 CHARPOS (tlendpos)))
13573 if (CHARPOS (tlbufpos) > BEGV
13574 && FETCH_BYTE (BYTEPOS (tlbufpos) - 1) != '\n'
13575 && (CHARPOS (tlbufpos) == ZV
13576 || FETCH_BYTE (BYTEPOS (tlbufpos)) == '\n'))
13577 /* Former continuation line has disappeared by becoming empty. */
13578 goto cancel;
13579 else if (window_outdated (w) || MINI_WINDOW_P (w))
13581 /* We have to handle the case of continuation around a
13582 wide-column character (see the comment in indent.c around
13583 line 1340).
13585 For instance, in the following case:
13587 -------- Insert --------
13588 K_A_N_\\ `a' K_A_N_a\ `X_' are wide-column chars.
13589 J_I_ ==> J_I_ `^^' are cursors.
13590 ^^ ^^
13591 -------- --------
13593 As we have to redraw the line above, we cannot use this
13594 optimization. */
13596 struct it it;
13597 int line_height_before = this_line_pixel_height;
13599 /* Note that start_display will handle the case that the
13600 line starting at tlbufpos is a continuation line. */
13601 start_display (&it, w, tlbufpos);
13603 /* Implementation note: It this still necessary? */
13604 if (it.current_x != this_line_start_x)
13605 goto cancel;
13607 TRACE ((stderr, "trying display optimization 1\n"));
13608 w->cursor.vpos = -1;
13609 overlay_arrow_seen = 0;
13610 it.vpos = this_line_vpos;
13611 it.current_y = this_line_y;
13612 it.glyph_row = MATRIX_ROW (w->desired_matrix, this_line_vpos);
13613 display_line (&it);
13615 /* If line contains point, is not continued,
13616 and ends at same distance from eob as before, we win. */
13617 if (w->cursor.vpos >= 0
13618 /* Line is not continued, otherwise this_line_start_pos
13619 would have been set to 0 in display_line. */
13620 && CHARPOS (this_line_start_pos)
13621 /* Line ends as before. */
13622 && CHARPOS (this_line_end_pos) == CHARPOS (tlendpos)
13623 /* Line has same height as before. Otherwise other lines
13624 would have to be shifted up or down. */
13625 && this_line_pixel_height == line_height_before)
13627 /* If this is not the window's last line, we must adjust
13628 the charstarts of the lines below. */
13629 if (it.current_y < it.last_visible_y)
13631 struct glyph_row *row
13632 = MATRIX_ROW (w->current_matrix, this_line_vpos + 1);
13633 ptrdiff_t delta, delta_bytes;
13635 /* We used to distinguish between two cases here,
13636 conditioned by Z - CHARPOS (tlendpos) == ZV, for
13637 when the line ends in a newline or the end of the
13638 buffer's accessible portion. But both cases did
13639 the same, so they were collapsed. */
13640 delta = (Z
13641 - CHARPOS (tlendpos)
13642 - MATRIX_ROW_START_CHARPOS (row));
13643 delta_bytes = (Z_BYTE
13644 - BYTEPOS (tlendpos)
13645 - MATRIX_ROW_START_BYTEPOS (row));
13647 increment_matrix_positions (w->current_matrix,
13648 this_line_vpos + 1,
13649 w->current_matrix->nrows,
13650 delta, delta_bytes);
13653 /* If this row displays text now but previously didn't,
13654 or vice versa, w->window_end_vpos may have to be
13655 adjusted. */
13656 if (MATRIX_ROW_DISPLAYS_TEXT_P (it.glyph_row - 1))
13658 if (w->window_end_vpos < this_line_vpos)
13659 w->window_end_vpos = this_line_vpos;
13661 else if (w->window_end_vpos == this_line_vpos
13662 && this_line_vpos > 0)
13663 w->window_end_vpos = this_line_vpos - 1;
13664 w->window_end_valid = 0;
13666 /* Update hint: No need to try to scroll in update_window. */
13667 w->desired_matrix->no_scrolling_p = 1;
13669 #ifdef GLYPH_DEBUG
13670 *w->desired_matrix->method = 0;
13671 debug_method_add (w, "optimization 1");
13672 #endif
13673 #ifdef HAVE_WINDOW_SYSTEM
13674 update_window_fringes (w, 0);
13675 #endif
13676 goto update;
13678 else
13679 goto cancel;
13681 else if (/* Cursor position hasn't changed. */
13682 PT == w->last_point
13683 /* Make sure the cursor was last displayed
13684 in this window. Otherwise we have to reposition it. */
13686 /* PXW: Must be converted to pixels, probably. */
13687 && 0 <= w->cursor.vpos
13688 && w->cursor.vpos < WINDOW_TOTAL_LINES (w))
13690 if (!must_finish)
13692 do_pending_window_change (true);
13693 /* If selected_window changed, redisplay again. */
13694 if (WINDOWP (selected_window)
13695 && (w = XWINDOW (selected_window)) != sw)
13696 goto retry;
13698 /* We used to always goto end_of_redisplay here, but this
13699 isn't enough if we have a blinking cursor. */
13700 if (w->cursor_off_p == w->last_cursor_off_p)
13701 goto end_of_redisplay;
13703 goto update;
13705 /* If highlighting the region, or if the cursor is in the echo area,
13706 then we can't just move the cursor. */
13707 else if (NILP (Vshow_trailing_whitespace)
13708 && !cursor_in_echo_area)
13710 struct it it;
13711 struct glyph_row *row;
13713 /* Skip from tlbufpos to PT and see where it is. Note that
13714 PT may be in invisible text. If so, we will end at the
13715 next visible position. */
13716 init_iterator (&it, w, CHARPOS (tlbufpos), BYTEPOS (tlbufpos),
13717 NULL, DEFAULT_FACE_ID);
13718 it.current_x = this_line_start_x;
13719 it.current_y = this_line_y;
13720 it.vpos = this_line_vpos;
13722 /* The call to move_it_to stops in front of PT, but
13723 moves over before-strings. */
13724 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
13726 if (it.vpos == this_line_vpos
13727 && (row = MATRIX_ROW (w->current_matrix, this_line_vpos),
13728 row->enabled_p))
13730 eassert (this_line_vpos == it.vpos);
13731 eassert (this_line_y == it.current_y);
13732 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
13733 #ifdef GLYPH_DEBUG
13734 *w->desired_matrix->method = 0;
13735 debug_method_add (w, "optimization 3");
13736 #endif
13737 goto update;
13739 else
13740 goto cancel;
13743 cancel:
13744 /* Text changed drastically or point moved off of line. */
13745 SET_MATRIX_ROW_ENABLED_P (w->desired_matrix, this_line_vpos, false);
13748 CHARPOS (this_line_start_pos) = 0;
13749 ++clear_face_cache_count;
13750 #ifdef HAVE_WINDOW_SYSTEM
13751 ++clear_image_cache_count;
13752 #endif
13754 /* Build desired matrices, and update the display. If
13755 consider_all_windows_p is non-zero, do it for all windows on all
13756 frames. Otherwise do it for selected_window, only. */
13758 if (consider_all_windows_p)
13760 FOR_EACH_FRAME (tail, frame)
13761 XFRAME (frame)->updated_p = 0;
13763 propagate_buffer_redisplay ();
13765 FOR_EACH_FRAME (tail, frame)
13767 struct frame *f = XFRAME (frame);
13769 /* We don't have to do anything for unselected terminal
13770 frames. */
13771 if ((FRAME_TERMCAP_P (f) || FRAME_MSDOS_P (f))
13772 && !EQ (FRAME_TTY (f)->top_frame, frame))
13773 continue;
13775 retry_frame:
13777 if (FRAME_WINDOW_P (f) || FRAME_TERMCAP_P (f) || f == sf)
13779 bool gcscrollbars
13780 /* Only GC scrollbars when we redisplay the whole frame. */
13781 = f->redisplay || !REDISPLAY_SOME_P ();
13782 /* Mark all the scroll bars to be removed; we'll redeem
13783 the ones we want when we redisplay their windows. */
13784 if (gcscrollbars && FRAME_TERMINAL (f)->condemn_scroll_bars_hook)
13785 FRAME_TERMINAL (f)->condemn_scroll_bars_hook (f);
13787 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
13788 redisplay_windows (FRAME_ROOT_WINDOW (f));
13789 /* Remember that the invisible frames need to be redisplayed next
13790 time they're visible. */
13791 else if (!REDISPLAY_SOME_P ())
13792 f->redisplay = true;
13794 /* The X error handler may have deleted that frame. */
13795 if (!FRAME_LIVE_P (f))
13796 continue;
13798 /* Any scroll bars which redisplay_windows should have
13799 nuked should now go away. */
13800 if (gcscrollbars && FRAME_TERMINAL (f)->judge_scroll_bars_hook)
13801 FRAME_TERMINAL (f)->judge_scroll_bars_hook (f);
13803 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
13805 /* If fonts changed on visible frame, display again. */
13806 if (f->fonts_changed)
13808 adjust_frame_glyphs (f);
13809 f->fonts_changed = false;
13810 goto retry_frame;
13813 /* See if we have to hscroll. */
13814 if (!f->already_hscrolled_p)
13816 f->already_hscrolled_p = true;
13817 if (hscroll_windows (f->root_window))
13818 goto retry_frame;
13821 /* Prevent various kinds of signals during display
13822 update. stdio is not robust about handling
13823 signals, which can cause an apparent I/O error. */
13824 if (interrupt_input)
13825 unrequest_sigio ();
13826 STOP_POLLING;
13828 pending |= update_frame (f, false, false);
13829 f->cursor_type_changed = false;
13830 f->updated_p = true;
13835 eassert (EQ (XFRAME (selected_frame)->selected_window, selected_window));
13837 if (!pending)
13839 /* Do the mark_window_display_accurate after all windows have
13840 been redisplayed because this call resets flags in buffers
13841 which are needed for proper redisplay. */
13842 FOR_EACH_FRAME (tail, frame)
13844 struct frame *f = XFRAME (frame);
13845 if (f->updated_p)
13847 f->redisplay = false;
13848 mark_window_display_accurate (f->root_window, 1);
13849 if (FRAME_TERMINAL (f)->frame_up_to_date_hook)
13850 FRAME_TERMINAL (f)->frame_up_to_date_hook (f);
13855 else if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
13857 Lisp_Object mini_window = FRAME_MINIBUF_WINDOW (sf);
13858 struct frame *mini_frame;
13860 displayed_buffer = XBUFFER (XWINDOW (selected_window)->contents);
13861 /* Use list_of_error, not Qerror, so that
13862 we catch only errors and don't run the debugger. */
13863 internal_condition_case_1 (redisplay_window_1, selected_window,
13864 list_of_error,
13865 redisplay_window_error);
13866 if (update_miniwindow_p)
13867 internal_condition_case_1 (redisplay_window_1, mini_window,
13868 list_of_error,
13869 redisplay_window_error);
13871 /* Compare desired and current matrices, perform output. */
13873 update:
13874 /* If fonts changed, display again. */
13875 if (sf->fonts_changed)
13876 goto retry;
13878 /* Prevent various kinds of signals during display update.
13879 stdio is not robust about handling signals,
13880 which can cause an apparent I/O error. */
13881 if (interrupt_input)
13882 unrequest_sigio ();
13883 STOP_POLLING;
13885 if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
13887 if (hscroll_windows (selected_window))
13888 goto retry;
13890 XWINDOW (selected_window)->must_be_updated_p = true;
13891 pending = update_frame (sf, false, false);
13892 sf->cursor_type_changed = false;
13895 /* We may have called echo_area_display at the top of this
13896 function. If the echo area is on another frame, that may
13897 have put text on a frame other than the selected one, so the
13898 above call to update_frame would not have caught it. Catch
13899 it here. */
13900 mini_window = FRAME_MINIBUF_WINDOW (sf);
13901 mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
13903 if (mini_frame != sf && FRAME_WINDOW_P (mini_frame))
13905 XWINDOW (mini_window)->must_be_updated_p = true;
13906 pending |= update_frame (mini_frame, false, false);
13907 mini_frame->cursor_type_changed = false;
13908 if (!pending && hscroll_windows (mini_window))
13909 goto retry;
13913 /* If display was paused because of pending input, make sure we do a
13914 thorough update the next time. */
13915 if (pending)
13917 /* Prevent the optimization at the beginning of
13918 redisplay_internal that tries a single-line update of the
13919 line containing the cursor in the selected window. */
13920 CHARPOS (this_line_start_pos) = 0;
13922 /* Let the overlay arrow be updated the next time. */
13923 update_overlay_arrows (0);
13925 /* If we pause after scrolling, some rows in the current
13926 matrices of some windows are not valid. */
13927 if (!WINDOW_FULL_WIDTH_P (w)
13928 && !FRAME_WINDOW_P (XFRAME (w->frame)))
13929 update_mode_lines = 36;
13931 else
13933 if (!consider_all_windows_p)
13935 /* This has already been done above if
13936 consider_all_windows_p is set. */
13937 if (XBUFFER (w->contents)->text->redisplay
13938 && buffer_window_count (XBUFFER (w->contents)) > 1)
13939 /* This can happen if b->text->redisplay was set during
13940 jit-lock. */
13941 propagate_buffer_redisplay ();
13942 mark_window_display_accurate_1 (w, 1);
13944 /* Say overlay arrows are up to date. */
13945 update_overlay_arrows (1);
13947 if (FRAME_TERMINAL (sf)->frame_up_to_date_hook != 0)
13948 FRAME_TERMINAL (sf)->frame_up_to_date_hook (sf);
13951 update_mode_lines = 0;
13952 windows_or_buffers_changed = 0;
13955 /* Start SIGIO interrupts coming again. Having them off during the
13956 code above makes it less likely one will discard output, but not
13957 impossible, since there might be stuff in the system buffer here.
13958 But it is much hairier to try to do anything about that. */
13959 if (interrupt_input)
13960 request_sigio ();
13961 RESUME_POLLING;
13963 /* If a frame has become visible which was not before, redisplay
13964 again, so that we display it. Expose events for such a frame
13965 (which it gets when becoming visible) don't call the parts of
13966 redisplay constructing glyphs, so simply exposing a frame won't
13967 display anything in this case. So, we have to display these
13968 frames here explicitly. */
13969 if (!pending)
13971 int new_count = 0;
13973 FOR_EACH_FRAME (tail, frame)
13975 if (XFRAME (frame)->visible)
13976 new_count++;
13979 if (new_count != number_of_visible_frames)
13980 windows_or_buffers_changed = 52;
13983 /* Change frame size now if a change is pending. */
13984 do_pending_window_change (true);
13986 /* If we just did a pending size change, or have additional
13987 visible frames, or selected_window changed, redisplay again. */
13988 if ((windows_or_buffers_changed && !pending)
13989 || (WINDOWP (selected_window) && (w = XWINDOW (selected_window)) != sw))
13990 goto retry;
13992 /* Clear the face and image caches.
13994 We used to do this only if consider_all_windows_p. But the cache
13995 needs to be cleared if a timer creates images in the current
13996 buffer (e.g. the test case in Bug#6230). */
13998 if (clear_face_cache_count > CLEAR_FACE_CACHE_COUNT)
14000 clear_face_cache (false);
14001 clear_face_cache_count = 0;
14004 #ifdef HAVE_WINDOW_SYSTEM
14005 if (clear_image_cache_count > CLEAR_IMAGE_CACHE_COUNT)
14007 clear_image_caches (Qnil);
14008 clear_image_cache_count = 0;
14010 #endif /* HAVE_WINDOW_SYSTEM */
14012 end_of_redisplay:
14013 #ifdef HAVE_NS
14014 ns_set_doc_edited ();
14015 #endif
14016 if (interrupt_input && interrupts_deferred)
14017 request_sigio ();
14019 unbind_to (count, Qnil);
14020 RESUME_POLLING;
14024 /* Redisplay, but leave alone any recent echo area message unless
14025 another message has been requested in its place.
14027 This is useful in situations where you need to redisplay but no
14028 user action has occurred, making it inappropriate for the message
14029 area to be cleared. See tracking_off and
14030 wait_reading_process_output for examples of these situations.
14032 FROM_WHERE is an integer saying from where this function was
14033 called. This is useful for debugging. */
14035 void
14036 redisplay_preserve_echo_area (int from_where)
14038 TRACE ((stderr, "redisplay_preserve_echo_area (%d)\n", from_where));
14040 if (!NILP (echo_area_buffer[1]))
14042 /* We have a previously displayed message, but no current
14043 message. Redisplay the previous message. */
14044 display_last_displayed_message_p = true;
14045 redisplay_internal ();
14046 display_last_displayed_message_p = false;
14048 else
14049 redisplay_internal ();
14051 flush_frame (SELECTED_FRAME ());
14055 /* Function registered with record_unwind_protect in redisplay_internal. */
14057 static void
14058 unwind_redisplay (void)
14060 redisplaying_p = 0;
14064 /* Mark the display of leaf window W as accurate or inaccurate.
14065 If ACCURATE_P is non-zero mark display of W as accurate. If
14066 ACCURATE_P is zero, arrange for W to be redisplayed the next
14067 time redisplay_internal is called. */
14069 static void
14070 mark_window_display_accurate_1 (struct window *w, int accurate_p)
14072 struct buffer *b = XBUFFER (w->contents);
14074 w->last_modified = accurate_p ? BUF_MODIFF (b) : 0;
14075 w->last_overlay_modified = accurate_p ? BUF_OVERLAY_MODIFF (b) : 0;
14076 w->last_had_star = BUF_MODIFF (b) > BUF_SAVE_MODIFF (b);
14078 if (accurate_p)
14080 b->clip_changed = false;
14081 b->prevent_redisplay_optimizations_p = false;
14082 eassert (buffer_window_count (b) > 0);
14083 /* Resetting b->text->redisplay is problematic!
14084 In order to make it safer to do it here, redisplay_internal must
14085 have copied all b->text->redisplay to their respective windows. */
14086 b->text->redisplay = false;
14088 BUF_UNCHANGED_MODIFIED (b) = BUF_MODIFF (b);
14089 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = BUF_OVERLAY_MODIFF (b);
14090 BUF_BEG_UNCHANGED (b) = BUF_GPT (b) - BUF_BEG (b);
14091 BUF_END_UNCHANGED (b) = BUF_Z (b) - BUF_GPT (b);
14093 w->current_matrix->buffer = b;
14094 w->current_matrix->begv = BUF_BEGV (b);
14095 w->current_matrix->zv = BUF_ZV (b);
14097 w->last_cursor_vpos = w->cursor.vpos;
14098 w->last_cursor_off_p = w->cursor_off_p;
14100 if (w == XWINDOW (selected_window))
14101 w->last_point = BUF_PT (b);
14102 else
14103 w->last_point = marker_position (w->pointm);
14105 w->window_end_valid = true;
14106 w->update_mode_line = false;
14109 w->redisplay = !accurate_p;
14113 /* Mark the display of windows in the window tree rooted at WINDOW as
14114 accurate or inaccurate. If ACCURATE_P is non-zero mark display of
14115 windows as accurate. If ACCURATE_P is zero, arrange for windows to
14116 be redisplayed the next time redisplay_internal is called. */
14118 void
14119 mark_window_display_accurate (Lisp_Object window, int accurate_p)
14121 struct window *w;
14123 for (; !NILP (window); window = w->next)
14125 w = XWINDOW (window);
14126 if (WINDOWP (w->contents))
14127 mark_window_display_accurate (w->contents, accurate_p);
14128 else
14129 mark_window_display_accurate_1 (w, accurate_p);
14132 if (accurate_p)
14133 update_overlay_arrows (1);
14134 else
14135 /* Force a thorough redisplay the next time by setting
14136 last_arrow_position and last_arrow_string to t, which is
14137 unequal to any useful value of Voverlay_arrow_... */
14138 update_overlay_arrows (-1);
14142 /* Return value in display table DP (Lisp_Char_Table *) for character
14143 C. Since a display table doesn't have any parent, we don't have to
14144 follow parent. Do not call this function directly but use the
14145 macro DISP_CHAR_VECTOR. */
14147 Lisp_Object
14148 disp_char_vector (struct Lisp_Char_Table *dp, int c)
14150 Lisp_Object val;
14152 if (ASCII_CHAR_P (c))
14154 val = dp->ascii;
14155 if (SUB_CHAR_TABLE_P (val))
14156 val = XSUB_CHAR_TABLE (val)->contents[c];
14158 else
14160 Lisp_Object table;
14162 XSETCHAR_TABLE (table, dp);
14163 val = char_table_ref (table, c);
14165 if (NILP (val))
14166 val = dp->defalt;
14167 return val;
14172 /***********************************************************************
14173 Window Redisplay
14174 ***********************************************************************/
14176 /* Redisplay all leaf windows in the window tree rooted at WINDOW. */
14178 static void
14179 redisplay_windows (Lisp_Object window)
14181 while (!NILP (window))
14183 struct window *w = XWINDOW (window);
14185 if (WINDOWP (w->contents))
14186 redisplay_windows (w->contents);
14187 else if (BUFFERP (w->contents))
14189 displayed_buffer = XBUFFER (w->contents);
14190 /* Use list_of_error, not Qerror, so that
14191 we catch only errors and don't run the debugger. */
14192 internal_condition_case_1 (redisplay_window_0, window,
14193 list_of_error,
14194 redisplay_window_error);
14197 window = w->next;
14201 static Lisp_Object
14202 redisplay_window_error (Lisp_Object ignore)
14204 displayed_buffer->display_error_modiff = BUF_MODIFF (displayed_buffer);
14205 return Qnil;
14208 static Lisp_Object
14209 redisplay_window_0 (Lisp_Object window)
14211 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
14212 redisplay_window (window, false);
14213 return Qnil;
14216 static Lisp_Object
14217 redisplay_window_1 (Lisp_Object window)
14219 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
14220 redisplay_window (window, true);
14221 return Qnil;
14225 /* Set cursor position of W. PT is assumed to be displayed in ROW.
14226 DELTA and DELTA_BYTES are the numbers of characters and bytes by
14227 which positions recorded in ROW differ from current buffer
14228 positions.
14230 Return 0 if cursor is not on this row, 1 otherwise. */
14232 static int
14233 set_cursor_from_row (struct window *w, struct glyph_row *row,
14234 struct glyph_matrix *matrix,
14235 ptrdiff_t delta, ptrdiff_t delta_bytes,
14236 int dy, int dvpos)
14238 struct glyph *glyph = row->glyphs[TEXT_AREA];
14239 struct glyph *end = glyph + row->used[TEXT_AREA];
14240 struct glyph *cursor = NULL;
14241 /* The last known character position in row. */
14242 ptrdiff_t last_pos = MATRIX_ROW_START_CHARPOS (row) + delta;
14243 int x = row->x;
14244 ptrdiff_t pt_old = PT - delta;
14245 ptrdiff_t pos_before = MATRIX_ROW_START_CHARPOS (row) + delta;
14246 ptrdiff_t pos_after = MATRIX_ROW_END_CHARPOS (row) + delta;
14247 struct glyph *glyph_before = glyph - 1, *glyph_after = end;
14248 /* A glyph beyond the edge of TEXT_AREA which we should never
14249 touch. */
14250 struct glyph *glyphs_end = end;
14251 /* Non-zero means we've found a match for cursor position, but that
14252 glyph has the avoid_cursor_p flag set. */
14253 int match_with_avoid_cursor = 0;
14254 /* Non-zero means we've seen at least one glyph that came from a
14255 display string. */
14256 int string_seen = 0;
14257 /* Largest and smallest buffer positions seen so far during scan of
14258 glyph row. */
14259 ptrdiff_t bpos_max = pos_before;
14260 ptrdiff_t bpos_min = pos_after;
14261 /* Last buffer position covered by an overlay string with an integer
14262 `cursor' property. */
14263 ptrdiff_t bpos_covered = 0;
14264 /* Non-zero means the display string on which to display the cursor
14265 comes from a text property, not from an overlay. */
14266 int string_from_text_prop = 0;
14268 /* Don't even try doing anything if called for a mode-line or
14269 header-line row, since the rest of the code isn't prepared to
14270 deal with such calamities. */
14271 eassert (!row->mode_line_p);
14272 if (row->mode_line_p)
14273 return 0;
14275 /* Skip over glyphs not having an object at the start and the end of
14276 the row. These are special glyphs like truncation marks on
14277 terminal frames. */
14278 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
14280 if (!row->reversed_p)
14282 while (glyph < end
14283 && NILP (glyph->object)
14284 && glyph->charpos < 0)
14286 x += glyph->pixel_width;
14287 ++glyph;
14289 while (end > glyph
14290 && NILP ((end - 1)->object)
14291 /* CHARPOS is zero for blanks and stretch glyphs
14292 inserted by extend_face_to_end_of_line. */
14293 && (end - 1)->charpos <= 0)
14294 --end;
14295 glyph_before = glyph - 1;
14296 glyph_after = end;
14298 else
14300 struct glyph *g;
14302 /* If the glyph row is reversed, we need to process it from back
14303 to front, so swap the edge pointers. */
14304 glyphs_end = end = glyph - 1;
14305 glyph += row->used[TEXT_AREA] - 1;
14307 while (glyph > end + 1
14308 && NILP (glyph->object)
14309 && glyph->charpos < 0)
14311 --glyph;
14312 x -= glyph->pixel_width;
14314 if (NILP (glyph->object) && glyph->charpos < 0)
14315 --glyph;
14316 /* By default, in reversed rows we put the cursor on the
14317 rightmost (first in the reading order) glyph. */
14318 for (g = end + 1; g < glyph; g++)
14319 x += g->pixel_width;
14320 while (end < glyph
14321 && NILP ((end + 1)->object)
14322 && (end + 1)->charpos <= 0)
14323 ++end;
14324 glyph_before = glyph + 1;
14325 glyph_after = end;
14328 else if (row->reversed_p)
14330 /* In R2L rows that don't display text, put the cursor on the
14331 rightmost glyph. Case in point: an empty last line that is
14332 part of an R2L paragraph. */
14333 cursor = end - 1;
14334 /* Avoid placing the cursor on the last glyph of the row, where
14335 on terminal frames we hold the vertical border between
14336 adjacent windows. */
14337 if (!FRAME_WINDOW_P (WINDOW_XFRAME (w))
14338 && !WINDOW_RIGHTMOST_P (w)
14339 && cursor == row->glyphs[LAST_AREA] - 1)
14340 cursor--;
14341 x = -1; /* will be computed below, at label compute_x */
14344 /* Step 1: Try to find the glyph whose character position
14345 corresponds to point. If that's not possible, find 2 glyphs
14346 whose character positions are the closest to point, one before
14347 point, the other after it. */
14348 if (!row->reversed_p)
14349 while (/* not marched to end of glyph row */
14350 glyph < end
14351 /* glyph was not inserted by redisplay for internal purposes */
14352 && !NILP (glyph->object))
14354 if (BUFFERP (glyph->object))
14356 ptrdiff_t dpos = glyph->charpos - pt_old;
14358 if (glyph->charpos > bpos_max)
14359 bpos_max = glyph->charpos;
14360 if (glyph->charpos < bpos_min)
14361 bpos_min = glyph->charpos;
14362 if (!glyph->avoid_cursor_p)
14364 /* If we hit point, we've found the glyph on which to
14365 display the cursor. */
14366 if (dpos == 0)
14368 match_with_avoid_cursor = 0;
14369 break;
14371 /* See if we've found a better approximation to
14372 POS_BEFORE or to POS_AFTER. */
14373 if (0 > dpos && dpos > pos_before - pt_old)
14375 pos_before = glyph->charpos;
14376 glyph_before = glyph;
14378 else if (0 < dpos && dpos < pos_after - pt_old)
14380 pos_after = glyph->charpos;
14381 glyph_after = glyph;
14384 else if (dpos == 0)
14385 match_with_avoid_cursor = 1;
14387 else if (STRINGP (glyph->object))
14389 Lisp_Object chprop;
14390 ptrdiff_t glyph_pos = glyph->charpos;
14392 chprop = Fget_char_property (make_number (glyph_pos), Qcursor,
14393 glyph->object);
14394 if (!NILP (chprop))
14396 /* If the string came from a `display' text property,
14397 look up the buffer position of that property and
14398 use that position to update bpos_max, as if we
14399 actually saw such a position in one of the row's
14400 glyphs. This helps with supporting integer values
14401 of `cursor' property on the display string in
14402 situations where most or all of the row's buffer
14403 text is completely covered by display properties,
14404 so that no glyph with valid buffer positions is
14405 ever seen in the row. */
14406 ptrdiff_t prop_pos =
14407 string_buffer_position_lim (glyph->object, pos_before,
14408 pos_after, 0);
14410 if (prop_pos >= pos_before)
14411 bpos_max = prop_pos;
14413 if (INTEGERP (chprop))
14415 bpos_covered = bpos_max + XINT (chprop);
14416 /* If the `cursor' property covers buffer positions up
14417 to and including point, we should display cursor on
14418 this glyph. Note that, if a `cursor' property on one
14419 of the string's characters has an integer value, we
14420 will break out of the loop below _before_ we get to
14421 the position match above. IOW, integer values of
14422 the `cursor' property override the "exact match for
14423 point" strategy of positioning the cursor. */
14424 /* Implementation note: bpos_max == pt_old when, e.g.,
14425 we are in an empty line, where bpos_max is set to
14426 MATRIX_ROW_START_CHARPOS, see above. */
14427 if (bpos_max <= pt_old && bpos_covered >= pt_old)
14429 cursor = glyph;
14430 break;
14434 string_seen = 1;
14436 x += glyph->pixel_width;
14437 ++glyph;
14439 else if (glyph > end) /* row is reversed */
14440 while (!NILP (glyph->object))
14442 if (BUFFERP (glyph->object))
14444 ptrdiff_t dpos = glyph->charpos - pt_old;
14446 if (glyph->charpos > bpos_max)
14447 bpos_max = glyph->charpos;
14448 if (glyph->charpos < bpos_min)
14449 bpos_min = glyph->charpos;
14450 if (!glyph->avoid_cursor_p)
14452 if (dpos == 0)
14454 match_with_avoid_cursor = 0;
14455 break;
14457 if (0 > dpos && dpos > pos_before - pt_old)
14459 pos_before = glyph->charpos;
14460 glyph_before = glyph;
14462 else if (0 < dpos && dpos < pos_after - pt_old)
14464 pos_after = glyph->charpos;
14465 glyph_after = glyph;
14468 else if (dpos == 0)
14469 match_with_avoid_cursor = 1;
14471 else if (STRINGP (glyph->object))
14473 Lisp_Object chprop;
14474 ptrdiff_t glyph_pos = glyph->charpos;
14476 chprop = Fget_char_property (make_number (glyph_pos), Qcursor,
14477 glyph->object);
14478 if (!NILP (chprop))
14480 ptrdiff_t prop_pos =
14481 string_buffer_position_lim (glyph->object, pos_before,
14482 pos_after, 0);
14484 if (prop_pos >= pos_before)
14485 bpos_max = prop_pos;
14487 if (INTEGERP (chprop))
14489 bpos_covered = bpos_max + XINT (chprop);
14490 /* If the `cursor' property covers buffer positions up
14491 to and including point, we should display cursor on
14492 this glyph. */
14493 if (bpos_max <= pt_old && bpos_covered >= pt_old)
14495 cursor = glyph;
14496 break;
14499 string_seen = 1;
14501 --glyph;
14502 if (glyph == glyphs_end) /* don't dereference outside TEXT_AREA */
14504 x--; /* can't use any pixel_width */
14505 break;
14507 x -= glyph->pixel_width;
14510 /* Step 2: If we didn't find an exact match for point, we need to
14511 look for a proper place to put the cursor among glyphs between
14512 GLYPH_BEFORE and GLYPH_AFTER. */
14513 if (!((row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end)
14514 && BUFFERP (glyph->object) && glyph->charpos == pt_old)
14515 && !(bpos_max <= pt_old && pt_old <= bpos_covered))
14517 /* An empty line has a single glyph whose OBJECT is nil and
14518 whose CHARPOS is the position of a newline on that line.
14519 Note that on a TTY, there are more glyphs after that, which
14520 were produced by extend_face_to_end_of_line, but their
14521 CHARPOS is zero or negative. */
14522 int empty_line_p =
14523 (row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end)
14524 && NILP (glyph->object) && glyph->charpos > 0
14525 /* On a TTY, continued and truncated rows also have a glyph at
14526 their end whose OBJECT is nil and whose CHARPOS is
14527 positive (the continuation and truncation glyphs), but such
14528 rows are obviously not "empty". */
14529 && !(row->continued_p || row->truncated_on_right_p);
14531 if (row->ends_in_ellipsis_p && pos_after == last_pos)
14533 ptrdiff_t ellipsis_pos;
14535 /* Scan back over the ellipsis glyphs. */
14536 if (!row->reversed_p)
14538 ellipsis_pos = (glyph - 1)->charpos;
14539 while (glyph > row->glyphs[TEXT_AREA]
14540 && (glyph - 1)->charpos == ellipsis_pos)
14541 glyph--, x -= glyph->pixel_width;
14542 /* That loop always goes one position too far, including
14543 the glyph before the ellipsis. So scan forward over
14544 that one. */
14545 x += glyph->pixel_width;
14546 glyph++;
14548 else /* row is reversed */
14550 ellipsis_pos = (glyph + 1)->charpos;
14551 while (glyph < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
14552 && (glyph + 1)->charpos == ellipsis_pos)
14553 glyph++, x += glyph->pixel_width;
14554 x -= glyph->pixel_width;
14555 glyph--;
14558 else if (match_with_avoid_cursor)
14560 cursor = glyph_after;
14561 x = -1;
14563 else if (string_seen)
14565 int incr = row->reversed_p ? -1 : +1;
14567 /* Need to find the glyph that came out of a string which is
14568 present at point. That glyph is somewhere between
14569 GLYPH_BEFORE and GLYPH_AFTER, and it came from a string
14570 positioned between POS_BEFORE and POS_AFTER in the
14571 buffer. */
14572 struct glyph *start, *stop;
14573 ptrdiff_t pos = pos_before;
14575 x = -1;
14577 /* If the row ends in a newline from a display string,
14578 reordering could have moved the glyphs belonging to the
14579 string out of the [GLYPH_BEFORE..GLYPH_AFTER] range. So
14580 in this case we extend the search to the last glyph in
14581 the row that was not inserted by redisplay. */
14582 if (row->ends_in_newline_from_string_p)
14584 glyph_after = end;
14585 pos_after = MATRIX_ROW_END_CHARPOS (row) + delta;
14588 /* GLYPH_BEFORE and GLYPH_AFTER are the glyphs that
14589 correspond to POS_BEFORE and POS_AFTER, respectively. We
14590 need START and STOP in the order that corresponds to the
14591 row's direction as given by its reversed_p flag. If the
14592 directionality of characters between POS_BEFORE and
14593 POS_AFTER is the opposite of the row's base direction,
14594 these characters will have been reordered for display,
14595 and we need to reverse START and STOP. */
14596 if (!row->reversed_p)
14598 start = min (glyph_before, glyph_after);
14599 stop = max (glyph_before, glyph_after);
14601 else
14603 start = max (glyph_before, glyph_after);
14604 stop = min (glyph_before, glyph_after);
14606 for (glyph = start + incr;
14607 row->reversed_p ? glyph > stop : glyph < stop; )
14610 /* Any glyphs that come from the buffer are here because
14611 of bidi reordering. Skip them, and only pay
14612 attention to glyphs that came from some string. */
14613 if (STRINGP (glyph->object))
14615 Lisp_Object str;
14616 ptrdiff_t tem;
14617 /* If the display property covers the newline, we
14618 need to search for it one position farther. */
14619 ptrdiff_t lim = pos_after
14620 + (pos_after == MATRIX_ROW_END_CHARPOS (row) + delta);
14622 string_from_text_prop = 0;
14623 str = glyph->object;
14624 tem = string_buffer_position_lim (str, pos, lim, 0);
14625 if (tem == 0 /* from overlay */
14626 || pos <= tem)
14628 /* If the string from which this glyph came is
14629 found in the buffer at point, or at position
14630 that is closer to point than pos_after, then
14631 we've found the glyph we've been looking for.
14632 If it comes from an overlay (tem == 0), and
14633 it has the `cursor' property on one of its
14634 glyphs, record that glyph as a candidate for
14635 displaying the cursor. (As in the
14636 unidirectional version, we will display the
14637 cursor on the last candidate we find.) */
14638 if (tem == 0
14639 || tem == pt_old
14640 || (tem - pt_old > 0 && tem < pos_after))
14642 /* The glyphs from this string could have
14643 been reordered. Find the one with the
14644 smallest string position. Or there could
14645 be a character in the string with the
14646 `cursor' property, which means display
14647 cursor on that character's glyph. */
14648 ptrdiff_t strpos = glyph->charpos;
14650 if (tem)
14652 cursor = glyph;
14653 string_from_text_prop = 1;
14655 for ( ;
14656 (row->reversed_p ? glyph > stop : glyph < stop)
14657 && EQ (glyph->object, str);
14658 glyph += incr)
14660 Lisp_Object cprop;
14661 ptrdiff_t gpos = glyph->charpos;
14663 cprop = Fget_char_property (make_number (gpos),
14664 Qcursor,
14665 glyph->object);
14666 if (!NILP (cprop))
14668 cursor = glyph;
14669 break;
14671 if (tem && glyph->charpos < strpos)
14673 strpos = glyph->charpos;
14674 cursor = glyph;
14678 if (tem == pt_old
14679 || (tem - pt_old > 0 && tem < pos_after))
14680 goto compute_x;
14682 if (tem)
14683 pos = tem + 1; /* don't find previous instances */
14685 /* This string is not what we want; skip all of the
14686 glyphs that came from it. */
14687 while ((row->reversed_p ? glyph > stop : glyph < stop)
14688 && EQ (glyph->object, str))
14689 glyph += incr;
14691 else
14692 glyph += incr;
14695 /* If we reached the end of the line, and END was from a string,
14696 the cursor is not on this line. */
14697 if (cursor == NULL
14698 && (row->reversed_p ? glyph <= end : glyph >= end)
14699 && (row->reversed_p ? end > glyphs_end : end < glyphs_end)
14700 && STRINGP (end->object)
14701 && row->continued_p)
14702 return 0;
14704 /* A truncated row may not include PT among its character positions.
14705 Setting the cursor inside the scroll margin will trigger
14706 recalculation of hscroll in hscroll_window_tree. But if a
14707 display string covers point, defer to the string-handling
14708 code below to figure this out. */
14709 else if (row->truncated_on_left_p && pt_old < bpos_min)
14711 cursor = glyph_before;
14712 x = -1;
14714 else if ((row->truncated_on_right_p && pt_old > bpos_max)
14715 /* Zero-width characters produce no glyphs. */
14716 || (!empty_line_p
14717 && (row->reversed_p
14718 ? glyph_after > glyphs_end
14719 : glyph_after < glyphs_end)))
14721 cursor = glyph_after;
14722 x = -1;
14726 compute_x:
14727 if (cursor != NULL)
14728 glyph = cursor;
14729 else if (glyph == glyphs_end
14730 && pos_before == pos_after
14731 && STRINGP ((row->reversed_p
14732 ? row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
14733 : row->glyphs[TEXT_AREA])->object))
14735 /* If all the glyphs of this row came from strings, put the
14736 cursor on the first glyph of the row. This avoids having the
14737 cursor outside of the text area in this very rare and hard
14738 use case. */
14739 glyph =
14740 row->reversed_p
14741 ? row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
14742 : row->glyphs[TEXT_AREA];
14744 if (x < 0)
14746 struct glyph *g;
14748 /* Need to compute x that corresponds to GLYPH. */
14749 for (g = row->glyphs[TEXT_AREA], x = row->x; g < glyph; g++)
14751 if (g >= row->glyphs[TEXT_AREA] + row->used[TEXT_AREA])
14752 emacs_abort ();
14753 x += g->pixel_width;
14757 /* ROW could be part of a continued line, which, under bidi
14758 reordering, might have other rows whose start and end charpos
14759 occlude point. Only set w->cursor if we found a better
14760 approximation to the cursor position than we have from previously
14761 examined candidate rows belonging to the same continued line. */
14762 if (/* We already have a candidate row. */
14763 w->cursor.vpos >= 0
14764 /* That candidate is not the row we are processing. */
14765 && MATRIX_ROW (matrix, w->cursor.vpos) != row
14766 /* Make sure cursor.vpos specifies a row whose start and end
14767 charpos occlude point, and it is valid candidate for being a
14768 cursor-row. This is because some callers of this function
14769 leave cursor.vpos at the row where the cursor was displayed
14770 during the last redisplay cycle. */
14771 && MATRIX_ROW_START_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos)) <= pt_old
14772 && pt_old <= MATRIX_ROW_END_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
14773 && cursor_row_p (MATRIX_ROW (matrix, w->cursor.vpos)))
14775 struct glyph *g1
14776 = MATRIX_ROW_GLYPH_START (matrix, w->cursor.vpos) + w->cursor.hpos;
14778 /* Don't consider glyphs that are outside TEXT_AREA. */
14779 if (!(row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end))
14780 return 0;
14781 /* Keep the candidate whose buffer position is the closest to
14782 point or has the `cursor' property. */
14783 if (/* Previous candidate is a glyph in TEXT_AREA of that row. */
14784 w->cursor.hpos >= 0
14785 && w->cursor.hpos < MATRIX_ROW_USED (matrix, w->cursor.vpos)
14786 && ((BUFFERP (g1->object)
14787 && (g1->charpos == pt_old /* An exact match always wins. */
14788 || (BUFFERP (glyph->object)
14789 && eabs (g1->charpos - pt_old)
14790 < eabs (glyph->charpos - pt_old))))
14791 /* Previous candidate is a glyph from a string that has
14792 a non-nil `cursor' property. */
14793 || (STRINGP (g1->object)
14794 && (!NILP (Fget_char_property (make_number (g1->charpos),
14795 Qcursor, g1->object))
14796 /* Previous candidate is from the same display
14797 string as this one, and the display string
14798 came from a text property. */
14799 || (EQ (g1->object, glyph->object)
14800 && string_from_text_prop)
14801 /* this candidate is from newline and its
14802 position is not an exact match */
14803 || (NILP (glyph->object)
14804 && glyph->charpos != pt_old)))))
14805 return 0;
14806 /* If this candidate gives an exact match, use that. */
14807 if (!((BUFFERP (glyph->object) && glyph->charpos == pt_old)
14808 /* If this candidate is a glyph created for the
14809 terminating newline of a line, and point is on that
14810 newline, it wins because it's an exact match. */
14811 || (!row->continued_p
14812 && NILP (glyph->object)
14813 && glyph->charpos == 0
14814 && pt_old == MATRIX_ROW_END_CHARPOS (row) - 1))
14815 /* Otherwise, keep the candidate that comes from a row
14816 spanning less buffer positions. This may win when one or
14817 both candidate positions are on glyphs that came from
14818 display strings, for which we cannot compare buffer
14819 positions. */
14820 && MATRIX_ROW_END_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
14821 - MATRIX_ROW_START_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
14822 < MATRIX_ROW_END_CHARPOS (row) - MATRIX_ROW_START_CHARPOS (row))
14823 return 0;
14825 w->cursor.hpos = glyph - row->glyphs[TEXT_AREA];
14826 w->cursor.x = x;
14827 w->cursor.vpos = MATRIX_ROW_VPOS (row, matrix) + dvpos;
14828 w->cursor.y = row->y + dy;
14830 if (w == XWINDOW (selected_window))
14832 if (!row->continued_p
14833 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
14834 && row->x == 0)
14836 this_line_buffer = XBUFFER (w->contents);
14838 CHARPOS (this_line_start_pos)
14839 = MATRIX_ROW_START_CHARPOS (row) + delta;
14840 BYTEPOS (this_line_start_pos)
14841 = MATRIX_ROW_START_BYTEPOS (row) + delta_bytes;
14843 CHARPOS (this_line_end_pos)
14844 = Z - (MATRIX_ROW_END_CHARPOS (row) + delta);
14845 BYTEPOS (this_line_end_pos)
14846 = Z_BYTE - (MATRIX_ROW_END_BYTEPOS (row) + delta_bytes);
14848 this_line_y = w->cursor.y;
14849 this_line_pixel_height = row->height;
14850 this_line_vpos = w->cursor.vpos;
14851 this_line_start_x = row->x;
14853 else
14854 CHARPOS (this_line_start_pos) = 0;
14857 return 1;
14861 /* Run window scroll functions, if any, for WINDOW with new window
14862 start STARTP. Sets the window start of WINDOW to that position.
14864 We assume that the window's buffer is really current. */
14866 static struct text_pos
14867 run_window_scroll_functions (Lisp_Object window, struct text_pos startp)
14869 struct window *w = XWINDOW (window);
14870 SET_MARKER_FROM_TEXT_POS (w->start, startp);
14872 eassert (current_buffer == XBUFFER (w->contents));
14874 if (!NILP (Vwindow_scroll_functions))
14876 run_hook_with_args_2 (Qwindow_scroll_functions, window,
14877 make_number (CHARPOS (startp)));
14878 SET_TEXT_POS_FROM_MARKER (startp, w->start);
14879 /* In case the hook functions switch buffers. */
14880 set_buffer_internal (XBUFFER (w->contents));
14883 return startp;
14887 /* Make sure the line containing the cursor is fully visible.
14888 A value of 1 means there is nothing to be done.
14889 (Either the line is fully visible, or it cannot be made so,
14890 or we cannot tell.)
14892 If FORCE_P is non-zero, return 0 even if partial visible cursor row
14893 is higher than window.
14895 If CURRENT_MATRIX_P is non-zero, use the information from the
14896 window's current glyph matrix; otherwise use the desired glyph
14897 matrix.
14899 A value of 0 means the caller should do scrolling
14900 as if point had gone off the screen. */
14902 static int
14903 cursor_row_fully_visible_p (struct window *w, int force_p, int current_matrix_p)
14905 struct glyph_matrix *matrix;
14906 struct glyph_row *row;
14907 int window_height;
14909 if (!make_cursor_line_fully_visible_p)
14910 return 1;
14912 /* It's not always possible to find the cursor, e.g, when a window
14913 is full of overlay strings. Don't do anything in that case. */
14914 if (w->cursor.vpos < 0)
14915 return 1;
14917 matrix = current_matrix_p ? w->current_matrix : w->desired_matrix;
14918 row = MATRIX_ROW (matrix, w->cursor.vpos);
14920 /* If the cursor row is not partially visible, there's nothing to do. */
14921 if (!MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row))
14922 return 1;
14924 /* If the row the cursor is in is taller than the window's height,
14925 it's not clear what to do, so do nothing. */
14926 window_height = window_box_height (w);
14927 if (row->height >= window_height)
14929 if (!force_p || MINI_WINDOW_P (w)
14930 || w->vscroll || w->cursor.vpos == 0)
14931 return 1;
14933 return 0;
14937 /* Try scrolling PT into view in window WINDOW. JUST_THIS_ONE_P
14938 non-zero means only WINDOW is redisplayed in redisplay_internal.
14939 TEMP_SCROLL_STEP has the same meaning as emacs_scroll_step, and is used
14940 in redisplay_window to bring a partially visible line into view in
14941 the case that only the cursor has moved.
14943 LAST_LINE_MISFIT should be nonzero if we're scrolling because the
14944 last screen line's vertical height extends past the end of the screen.
14946 Value is
14948 1 if scrolling succeeded
14950 0 if scrolling didn't find point.
14952 -1 if new fonts have been loaded so that we must interrupt
14953 redisplay, adjust glyph matrices, and try again. */
14955 enum
14957 SCROLLING_SUCCESS,
14958 SCROLLING_FAILED,
14959 SCROLLING_NEED_LARGER_MATRICES
14962 /* If scroll-conservatively is more than this, never recenter.
14964 If you change this, don't forget to update the doc string of
14965 `scroll-conservatively' and the Emacs manual. */
14966 #define SCROLL_LIMIT 100
14968 static int
14969 try_scrolling (Lisp_Object window, int just_this_one_p,
14970 ptrdiff_t arg_scroll_conservatively, ptrdiff_t scroll_step,
14971 int temp_scroll_step, int last_line_misfit)
14973 struct window *w = XWINDOW (window);
14974 struct frame *f = XFRAME (w->frame);
14975 struct text_pos pos, startp;
14976 struct it it;
14977 int this_scroll_margin, scroll_max, rc, height;
14978 int dy = 0, amount_to_scroll = 0, scroll_down_p = 0;
14979 int extra_scroll_margin_lines = last_line_misfit ? 1 : 0;
14980 Lisp_Object aggressive;
14981 /* We will never try scrolling more than this number of lines. */
14982 int scroll_limit = SCROLL_LIMIT;
14983 int frame_line_height = default_line_pixel_height (w);
14984 int window_total_lines
14985 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
14987 #ifdef GLYPH_DEBUG
14988 debug_method_add (w, "try_scrolling");
14989 #endif
14991 SET_TEXT_POS_FROM_MARKER (startp, w->start);
14993 /* Compute scroll margin height in pixels. We scroll when point is
14994 within this distance from the top or bottom of the window. */
14995 if (scroll_margin > 0)
14996 this_scroll_margin = min (scroll_margin, window_total_lines / 4)
14997 * frame_line_height;
14998 else
14999 this_scroll_margin = 0;
15001 /* Force arg_scroll_conservatively to have a reasonable value, to
15002 avoid scrolling too far away with slow move_it_* functions. Note
15003 that the user can supply scroll-conservatively equal to
15004 `most-positive-fixnum', which can be larger than INT_MAX. */
15005 if (arg_scroll_conservatively > scroll_limit)
15007 arg_scroll_conservatively = scroll_limit + 1;
15008 scroll_max = scroll_limit * frame_line_height;
15010 else if (scroll_step || arg_scroll_conservatively || temp_scroll_step)
15011 /* Compute how much we should try to scroll maximally to bring
15012 point into view. */
15013 scroll_max = (max (scroll_step,
15014 max (arg_scroll_conservatively, temp_scroll_step))
15015 * frame_line_height);
15016 else if (NUMBERP (BVAR (current_buffer, scroll_down_aggressively))
15017 || NUMBERP (BVAR (current_buffer, scroll_up_aggressively)))
15018 /* We're trying to scroll because of aggressive scrolling but no
15019 scroll_step is set. Choose an arbitrary one. */
15020 scroll_max = 10 * frame_line_height;
15021 else
15022 scroll_max = 0;
15024 too_near_end:
15026 /* Decide whether to scroll down. */
15027 if (PT > CHARPOS (startp))
15029 int scroll_margin_y;
15031 /* Compute the pixel ypos of the scroll margin, then move IT to
15032 either that ypos or PT, whichever comes first. */
15033 start_display (&it, w, startp);
15034 scroll_margin_y = it.last_visible_y - this_scroll_margin
15035 - frame_line_height * extra_scroll_margin_lines;
15036 move_it_to (&it, PT, -1, scroll_margin_y - 1, -1,
15037 (MOVE_TO_POS | MOVE_TO_Y));
15039 if (PT > CHARPOS (it.current.pos))
15041 int y0 = line_bottom_y (&it);
15042 /* Compute how many pixels below window bottom to stop searching
15043 for PT. This avoids costly search for PT that is far away if
15044 the user limited scrolling by a small number of lines, but
15045 always finds PT if scroll_conservatively is set to a large
15046 number, such as most-positive-fixnum. */
15047 int slack = max (scroll_max, 10 * frame_line_height);
15048 int y_to_move = it.last_visible_y + slack;
15050 /* Compute the distance from the scroll margin to PT or to
15051 the scroll limit, whichever comes first. This should
15052 include the height of the cursor line, to make that line
15053 fully visible. */
15054 move_it_to (&it, PT, -1, y_to_move,
15055 -1, MOVE_TO_POS | MOVE_TO_Y);
15056 dy = line_bottom_y (&it) - y0;
15058 if (dy > scroll_max)
15059 return SCROLLING_FAILED;
15061 if (dy > 0)
15062 scroll_down_p = 1;
15066 if (scroll_down_p)
15068 /* Point is in or below the bottom scroll margin, so move the
15069 window start down. If scrolling conservatively, move it just
15070 enough down to make point visible. If scroll_step is set,
15071 move it down by scroll_step. */
15072 if (arg_scroll_conservatively)
15073 amount_to_scroll
15074 = min (max (dy, frame_line_height),
15075 frame_line_height * arg_scroll_conservatively);
15076 else if (scroll_step || temp_scroll_step)
15077 amount_to_scroll = scroll_max;
15078 else
15080 aggressive = BVAR (current_buffer, scroll_up_aggressively);
15081 height = WINDOW_BOX_TEXT_HEIGHT (w);
15082 if (NUMBERP (aggressive))
15084 double float_amount = XFLOATINT (aggressive) * height;
15085 int aggressive_scroll = float_amount;
15086 if (aggressive_scroll == 0 && float_amount > 0)
15087 aggressive_scroll = 1;
15088 /* Don't let point enter the scroll margin near top of
15089 the window. This could happen if the value of
15090 scroll_up_aggressively is too large and there are
15091 non-zero margins, because scroll_up_aggressively
15092 means put point that fraction of window height
15093 _from_the_bottom_margin_. */
15094 if (aggressive_scroll + 2 * this_scroll_margin > height)
15095 aggressive_scroll = height - 2 * this_scroll_margin;
15096 amount_to_scroll = dy + aggressive_scroll;
15100 if (amount_to_scroll <= 0)
15101 return SCROLLING_FAILED;
15103 start_display (&it, w, startp);
15104 if (arg_scroll_conservatively <= scroll_limit)
15105 move_it_vertically (&it, amount_to_scroll);
15106 else
15108 /* Extra precision for users who set scroll-conservatively
15109 to a large number: make sure the amount we scroll
15110 the window start is never less than amount_to_scroll,
15111 which was computed as distance from window bottom to
15112 point. This matters when lines at window top and lines
15113 below window bottom have different height. */
15114 struct it it1;
15115 void *it1data = NULL;
15116 /* We use a temporary it1 because line_bottom_y can modify
15117 its argument, if it moves one line down; see there. */
15118 int start_y;
15120 SAVE_IT (it1, it, it1data);
15121 start_y = line_bottom_y (&it1);
15122 do {
15123 RESTORE_IT (&it, &it, it1data);
15124 move_it_by_lines (&it, 1);
15125 SAVE_IT (it1, it, it1data);
15126 } while (line_bottom_y (&it1) - start_y < amount_to_scroll);
15129 /* If STARTP is unchanged, move it down another screen line. */
15130 if (CHARPOS (it.current.pos) == CHARPOS (startp))
15131 move_it_by_lines (&it, 1);
15132 startp = it.current.pos;
15134 else
15136 struct text_pos scroll_margin_pos = startp;
15137 int y_offset = 0;
15139 /* See if point is inside the scroll margin at the top of the
15140 window. */
15141 if (this_scroll_margin)
15143 int y_start;
15145 start_display (&it, w, startp);
15146 y_start = it.current_y;
15147 move_it_vertically (&it, this_scroll_margin);
15148 scroll_margin_pos = it.current.pos;
15149 /* If we didn't move enough before hitting ZV, request
15150 additional amount of scroll, to move point out of the
15151 scroll margin. */
15152 if (IT_CHARPOS (it) == ZV
15153 && it.current_y - y_start < this_scroll_margin)
15154 y_offset = this_scroll_margin - (it.current_y - y_start);
15157 if (PT < CHARPOS (scroll_margin_pos))
15159 /* Point is in the scroll margin at the top of the window or
15160 above what is displayed in the window. */
15161 int y0, y_to_move;
15163 /* Compute the vertical distance from PT to the scroll
15164 margin position. Move as far as scroll_max allows, or
15165 one screenful, or 10 screen lines, whichever is largest.
15166 Give up if distance is greater than scroll_max or if we
15167 didn't reach the scroll margin position. */
15168 SET_TEXT_POS (pos, PT, PT_BYTE);
15169 start_display (&it, w, pos);
15170 y0 = it.current_y;
15171 y_to_move = max (it.last_visible_y,
15172 max (scroll_max, 10 * frame_line_height));
15173 move_it_to (&it, CHARPOS (scroll_margin_pos), 0,
15174 y_to_move, -1,
15175 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
15176 dy = it.current_y - y0;
15177 if (dy > scroll_max
15178 || IT_CHARPOS (it) < CHARPOS (scroll_margin_pos))
15179 return SCROLLING_FAILED;
15181 /* Additional scroll for when ZV was too close to point. */
15182 dy += y_offset;
15184 /* Compute new window start. */
15185 start_display (&it, w, startp);
15187 if (arg_scroll_conservatively)
15188 amount_to_scroll = max (dy, frame_line_height
15189 * max (scroll_step, temp_scroll_step));
15190 else if (scroll_step || temp_scroll_step)
15191 amount_to_scroll = scroll_max;
15192 else
15194 aggressive = BVAR (current_buffer, scroll_down_aggressively);
15195 height = WINDOW_BOX_TEXT_HEIGHT (w);
15196 if (NUMBERP (aggressive))
15198 double float_amount = XFLOATINT (aggressive) * height;
15199 int aggressive_scroll = float_amount;
15200 if (aggressive_scroll == 0 && float_amount > 0)
15201 aggressive_scroll = 1;
15202 /* Don't let point enter the scroll margin near
15203 bottom of the window, if the value of
15204 scroll_down_aggressively happens to be too
15205 large. */
15206 if (aggressive_scroll + 2 * this_scroll_margin > height)
15207 aggressive_scroll = height - 2 * this_scroll_margin;
15208 amount_to_scroll = dy + aggressive_scroll;
15212 if (amount_to_scroll <= 0)
15213 return SCROLLING_FAILED;
15215 move_it_vertically_backward (&it, amount_to_scroll);
15216 startp = it.current.pos;
15220 /* Run window scroll functions. */
15221 startp = run_window_scroll_functions (window, startp);
15223 /* Display the window. Give up if new fonts are loaded, or if point
15224 doesn't appear. */
15225 if (!try_window (window, startp, 0))
15226 rc = SCROLLING_NEED_LARGER_MATRICES;
15227 else if (w->cursor.vpos < 0)
15229 clear_glyph_matrix (w->desired_matrix);
15230 rc = SCROLLING_FAILED;
15232 else
15234 /* Maybe forget recorded base line for line number display. */
15235 if (!just_this_one_p
15236 || current_buffer->clip_changed
15237 || BEG_UNCHANGED < CHARPOS (startp))
15238 w->base_line_number = 0;
15240 /* If cursor ends up on a partially visible line,
15241 treat that as being off the bottom of the screen. */
15242 if (! cursor_row_fully_visible_p (w, extra_scroll_margin_lines <= 1, 0)
15243 /* It's possible that the cursor is on the first line of the
15244 buffer, which is partially obscured due to a vscroll
15245 (Bug#7537). In that case, avoid looping forever. */
15246 && extra_scroll_margin_lines < w->desired_matrix->nrows - 1)
15248 clear_glyph_matrix (w->desired_matrix);
15249 ++extra_scroll_margin_lines;
15250 goto too_near_end;
15252 rc = SCROLLING_SUCCESS;
15255 return rc;
15259 /* Compute a suitable window start for window W if display of W starts
15260 on a continuation line. Value is non-zero if a new window start
15261 was computed.
15263 The new window start will be computed, based on W's width, starting
15264 from the start of the continued line. It is the start of the
15265 screen line with the minimum distance from the old start W->start. */
15267 static int
15268 compute_window_start_on_continuation_line (struct window *w)
15270 struct text_pos pos, start_pos;
15271 int window_start_changed_p = 0;
15273 SET_TEXT_POS_FROM_MARKER (start_pos, w->start);
15275 /* If window start is on a continuation line... Window start may be
15276 < BEGV in case there's invisible text at the start of the
15277 buffer (M-x rmail, for example). */
15278 if (CHARPOS (start_pos) > BEGV
15279 && FETCH_BYTE (BYTEPOS (start_pos) - 1) != '\n')
15281 struct it it;
15282 struct glyph_row *row;
15284 /* Handle the case that the window start is out of range. */
15285 if (CHARPOS (start_pos) < BEGV)
15286 SET_TEXT_POS (start_pos, BEGV, BEGV_BYTE);
15287 else if (CHARPOS (start_pos) > ZV)
15288 SET_TEXT_POS (start_pos, ZV, ZV_BYTE);
15290 /* Find the start of the continued line. This should be fast
15291 because find_newline is fast (newline cache). */
15292 row = w->desired_matrix->rows + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0);
15293 init_iterator (&it, w, CHARPOS (start_pos), BYTEPOS (start_pos),
15294 row, DEFAULT_FACE_ID);
15295 reseat_at_previous_visible_line_start (&it);
15297 /* If the line start is "too far" away from the window start,
15298 say it takes too much time to compute a new window start. */
15299 if (CHARPOS (start_pos) - IT_CHARPOS (it)
15300 /* PXW: Do we need upper bounds here? */
15301 < WINDOW_TOTAL_LINES (w) * WINDOW_TOTAL_COLS (w))
15303 int min_distance, distance;
15305 /* Move forward by display lines to find the new window
15306 start. If window width was enlarged, the new start can
15307 be expected to be > the old start. If window width was
15308 decreased, the new window start will be < the old start.
15309 So, we're looking for the display line start with the
15310 minimum distance from the old window start. */
15311 pos = it.current.pos;
15312 min_distance = INFINITY;
15313 while ((distance = eabs (CHARPOS (start_pos) - IT_CHARPOS (it))),
15314 distance < min_distance)
15316 min_distance = distance;
15317 pos = it.current.pos;
15318 if (it.line_wrap == WORD_WRAP)
15320 /* Under WORD_WRAP, move_it_by_lines is likely to
15321 overshoot and stop not at the first, but the
15322 second character from the left margin. So in
15323 that case, we need a more tight control on the X
15324 coordinate of the iterator than move_it_by_lines
15325 promises in its contract. The method is to first
15326 go to the last (rightmost) visible character of a
15327 line, then move to the leftmost character on the
15328 next line in a separate call. */
15329 move_it_to (&it, ZV, it.last_visible_x, it.current_y, -1,
15330 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
15331 move_it_to (&it, ZV, 0,
15332 it.current_y + it.max_ascent + it.max_descent, -1,
15333 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
15335 else
15336 move_it_by_lines (&it, 1);
15339 /* Set the window start there. */
15340 SET_MARKER_FROM_TEXT_POS (w->start, pos);
15341 window_start_changed_p = 1;
15345 return window_start_changed_p;
15349 /* Try cursor movement in case text has not changed in window WINDOW,
15350 with window start STARTP. Value is
15352 CURSOR_MOVEMENT_SUCCESS if successful
15354 CURSOR_MOVEMENT_CANNOT_BE_USED if this method cannot be used
15356 CURSOR_MOVEMENT_MUST_SCROLL if we know we have to scroll the
15357 display. *SCROLL_STEP is set to 1, under certain circumstances, if
15358 we want to scroll as if scroll-step were set to 1. See the code.
15360 CURSOR_MOVEMENT_NEED_LARGER_MATRICES if we need larger matrices, in
15361 which case we have to abort this redisplay, and adjust matrices
15362 first. */
15364 enum
15366 CURSOR_MOVEMENT_SUCCESS,
15367 CURSOR_MOVEMENT_CANNOT_BE_USED,
15368 CURSOR_MOVEMENT_MUST_SCROLL,
15369 CURSOR_MOVEMENT_NEED_LARGER_MATRICES
15372 static int
15373 try_cursor_movement (Lisp_Object window, struct text_pos startp, int *scroll_step)
15375 struct window *w = XWINDOW (window);
15376 struct frame *f = XFRAME (w->frame);
15377 int rc = CURSOR_MOVEMENT_CANNOT_BE_USED;
15379 #ifdef GLYPH_DEBUG
15380 if (inhibit_try_cursor_movement)
15381 return rc;
15382 #endif
15384 /* Previously, there was a check for Lisp integer in the
15385 if-statement below. Now, this field is converted to
15386 ptrdiff_t, thus zero means invalid position in a buffer. */
15387 eassert (w->last_point > 0);
15388 /* Likewise there was a check whether window_end_vpos is nil or larger
15389 than the window. Now window_end_vpos is int and so never nil, but
15390 let's leave eassert to check whether it fits in the window. */
15391 eassert (w->window_end_vpos < w->current_matrix->nrows);
15393 /* Handle case where text has not changed, only point, and it has
15394 not moved off the frame. */
15395 if (/* Point may be in this window. */
15396 PT >= CHARPOS (startp)
15397 /* Selective display hasn't changed. */
15398 && !current_buffer->clip_changed
15399 /* Function force-mode-line-update is used to force a thorough
15400 redisplay. It sets either windows_or_buffers_changed or
15401 update_mode_lines. So don't take a shortcut here for these
15402 cases. */
15403 && !update_mode_lines
15404 && !windows_or_buffers_changed
15405 && !f->cursor_type_changed
15406 && NILP (Vshow_trailing_whitespace)
15407 /* This code is not used for mini-buffer for the sake of the case
15408 of redisplaying to replace an echo area message; since in
15409 that case the mini-buffer contents per se are usually
15410 unchanged. This code is of no real use in the mini-buffer
15411 since the handling of this_line_start_pos, etc., in redisplay
15412 handles the same cases. */
15413 && !EQ (window, minibuf_window)
15414 && (FRAME_WINDOW_P (f)
15415 || !overlay_arrow_in_current_buffer_p ()))
15417 int this_scroll_margin, top_scroll_margin;
15418 struct glyph_row *row = NULL;
15419 int frame_line_height = default_line_pixel_height (w);
15420 int window_total_lines
15421 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
15423 #ifdef GLYPH_DEBUG
15424 debug_method_add (w, "cursor movement");
15425 #endif
15427 /* Scroll if point within this distance from the top or bottom
15428 of the window. This is a pixel value. */
15429 if (scroll_margin > 0)
15431 this_scroll_margin = min (scroll_margin, window_total_lines / 4);
15432 this_scroll_margin *= frame_line_height;
15434 else
15435 this_scroll_margin = 0;
15437 top_scroll_margin = this_scroll_margin;
15438 if (WINDOW_WANTS_HEADER_LINE_P (w))
15439 top_scroll_margin += CURRENT_HEADER_LINE_HEIGHT (w);
15441 /* Start with the row the cursor was displayed during the last
15442 not paused redisplay. Give up if that row is not valid. */
15443 if (w->last_cursor_vpos < 0
15444 || w->last_cursor_vpos >= w->current_matrix->nrows)
15445 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15446 else
15448 row = MATRIX_ROW (w->current_matrix, w->last_cursor_vpos);
15449 if (row->mode_line_p)
15450 ++row;
15451 if (!row->enabled_p)
15452 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15455 if (rc == CURSOR_MOVEMENT_CANNOT_BE_USED)
15457 int scroll_p = 0, must_scroll = 0;
15458 int last_y = window_text_bottom_y (w) - this_scroll_margin;
15460 if (PT > w->last_point)
15462 /* Point has moved forward. */
15463 while (MATRIX_ROW_END_CHARPOS (row) < PT
15464 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
15466 eassert (row->enabled_p);
15467 ++row;
15470 /* If the end position of a row equals the start
15471 position of the next row, and PT is at that position,
15472 we would rather display cursor in the next line. */
15473 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
15474 && MATRIX_ROW_END_CHARPOS (row) == PT
15475 && row < MATRIX_MODE_LINE_ROW (w->current_matrix)
15476 && MATRIX_ROW_START_CHARPOS (row+1) == PT
15477 && !cursor_row_p (row))
15478 ++row;
15480 /* If within the scroll margin, scroll. Note that
15481 MATRIX_ROW_BOTTOM_Y gives the pixel position at which
15482 the next line would be drawn, and that
15483 this_scroll_margin can be zero. */
15484 if (MATRIX_ROW_BOTTOM_Y (row) > last_y
15485 || PT > MATRIX_ROW_END_CHARPOS (row)
15486 /* Line is completely visible last line in window
15487 and PT is to be set in the next line. */
15488 || (MATRIX_ROW_BOTTOM_Y (row) == last_y
15489 && PT == MATRIX_ROW_END_CHARPOS (row)
15490 && !row->ends_at_zv_p
15491 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
15492 scroll_p = 1;
15494 else if (PT < w->last_point)
15496 /* Cursor has to be moved backward. Note that PT >=
15497 CHARPOS (startp) because of the outer if-statement. */
15498 while (!row->mode_line_p
15499 && (MATRIX_ROW_START_CHARPOS (row) > PT
15500 || (MATRIX_ROW_START_CHARPOS (row) == PT
15501 && (MATRIX_ROW_STARTS_IN_MIDDLE_OF_CHAR_P (row)
15502 || (/* STARTS_IN_MIDDLE_OF_STRING_P (row) */
15503 row > w->current_matrix->rows
15504 && (row-1)->ends_in_newline_from_string_p))))
15505 && (row->y > top_scroll_margin
15506 || CHARPOS (startp) == BEGV))
15508 eassert (row->enabled_p);
15509 --row;
15512 /* Consider the following case: Window starts at BEGV,
15513 there is invisible, intangible text at BEGV, so that
15514 display starts at some point START > BEGV. It can
15515 happen that we are called with PT somewhere between
15516 BEGV and START. Try to handle that case. */
15517 if (row < w->current_matrix->rows
15518 || row->mode_line_p)
15520 row = w->current_matrix->rows;
15521 if (row->mode_line_p)
15522 ++row;
15525 /* Due to newlines in overlay strings, we may have to
15526 skip forward over overlay strings. */
15527 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
15528 && MATRIX_ROW_END_CHARPOS (row) == PT
15529 && !cursor_row_p (row))
15530 ++row;
15532 /* If within the scroll margin, scroll. */
15533 if (row->y < top_scroll_margin
15534 && CHARPOS (startp) != BEGV)
15535 scroll_p = 1;
15537 else
15539 /* Cursor did not move. So don't scroll even if cursor line
15540 is partially visible, as it was so before. */
15541 rc = CURSOR_MOVEMENT_SUCCESS;
15544 if (PT < MATRIX_ROW_START_CHARPOS (row)
15545 || PT > MATRIX_ROW_END_CHARPOS (row))
15547 /* if PT is not in the glyph row, give up. */
15548 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15549 must_scroll = 1;
15551 else if (rc != CURSOR_MOVEMENT_SUCCESS
15552 && !NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering)))
15554 struct glyph_row *row1;
15556 /* If rows are bidi-reordered and point moved, back up
15557 until we find a row that does not belong to a
15558 continuation line. This is because we must consider
15559 all rows of a continued line as candidates for the
15560 new cursor positioning, since row start and end
15561 positions change non-linearly with vertical position
15562 in such rows. */
15563 /* FIXME: Revisit this when glyph ``spilling'' in
15564 continuation lines' rows is implemented for
15565 bidi-reordered rows. */
15566 for (row1 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
15567 MATRIX_ROW_CONTINUATION_LINE_P (row);
15568 --row)
15570 /* If we hit the beginning of the displayed portion
15571 without finding the first row of a continued
15572 line, give up. */
15573 if (row <= row1)
15575 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15576 break;
15578 eassert (row->enabled_p);
15581 if (must_scroll)
15583 else if (rc != CURSOR_MOVEMENT_SUCCESS
15584 && MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row)
15585 /* Make sure this isn't a header line by any chance, since
15586 then MATRIX_ROW_PARTIALLY_VISIBLE_P might yield non-zero. */
15587 && !row->mode_line_p
15588 && make_cursor_line_fully_visible_p)
15590 if (PT == MATRIX_ROW_END_CHARPOS (row)
15591 && !row->ends_at_zv_p
15592 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
15593 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15594 else if (row->height > window_box_height (w))
15596 /* If we end up in a partially visible line, let's
15597 make it fully visible, except when it's taller
15598 than the window, in which case we can't do much
15599 about it. */
15600 *scroll_step = 1;
15601 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15603 else
15605 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
15606 if (!cursor_row_fully_visible_p (w, 0, 1))
15607 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15608 else
15609 rc = CURSOR_MOVEMENT_SUCCESS;
15612 else if (scroll_p)
15613 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15614 else if (rc != CURSOR_MOVEMENT_SUCCESS
15615 && !NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering)))
15617 /* With bidi-reordered rows, there could be more than
15618 one candidate row whose start and end positions
15619 occlude point. We need to let set_cursor_from_row
15620 find the best candidate. */
15621 /* FIXME: Revisit this when glyph ``spilling'' in
15622 continuation lines' rows is implemented for
15623 bidi-reordered rows. */
15624 int rv = 0;
15628 int at_zv_p = 0, exact_match_p = 0;
15630 if (MATRIX_ROW_START_CHARPOS (row) <= PT
15631 && PT <= MATRIX_ROW_END_CHARPOS (row)
15632 && cursor_row_p (row))
15633 rv |= set_cursor_from_row (w, row, w->current_matrix,
15634 0, 0, 0, 0);
15635 /* As soon as we've found the exact match for point,
15636 or the first suitable row whose ends_at_zv_p flag
15637 is set, we are done. */
15638 if (rv)
15640 at_zv_p = MATRIX_ROW (w->current_matrix,
15641 w->cursor.vpos)->ends_at_zv_p;
15642 if (!at_zv_p
15643 && w->cursor.hpos >= 0
15644 && w->cursor.hpos < MATRIX_ROW_USED (w->current_matrix,
15645 w->cursor.vpos))
15647 struct glyph_row *candidate =
15648 MATRIX_ROW (w->current_matrix, w->cursor.vpos);
15649 struct glyph *g =
15650 candidate->glyphs[TEXT_AREA] + w->cursor.hpos;
15651 ptrdiff_t endpos = MATRIX_ROW_END_CHARPOS (candidate);
15653 exact_match_p =
15654 (BUFFERP (g->object) && g->charpos == PT)
15655 || (NILP (g->object)
15656 && (g->charpos == PT
15657 || (g->charpos == 0 && endpos - 1 == PT)));
15659 if (at_zv_p || exact_match_p)
15661 rc = CURSOR_MOVEMENT_SUCCESS;
15662 break;
15665 if (MATRIX_ROW_BOTTOM_Y (row) == last_y)
15666 break;
15667 ++row;
15669 while (((MATRIX_ROW_CONTINUATION_LINE_P (row)
15670 || row->continued_p)
15671 && MATRIX_ROW_BOTTOM_Y (row) <= last_y)
15672 || (MATRIX_ROW_START_CHARPOS (row) == PT
15673 && MATRIX_ROW_BOTTOM_Y (row) < last_y));
15674 /* If we didn't find any candidate rows, or exited the
15675 loop before all the candidates were examined, signal
15676 to the caller that this method failed. */
15677 if (rc != CURSOR_MOVEMENT_SUCCESS
15678 && !(rv
15679 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
15680 && !row->continued_p))
15681 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15682 else if (rv)
15683 rc = CURSOR_MOVEMENT_SUCCESS;
15685 else
15689 if (set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0))
15691 rc = CURSOR_MOVEMENT_SUCCESS;
15692 break;
15694 ++row;
15696 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
15697 && MATRIX_ROW_START_CHARPOS (row) == PT
15698 && cursor_row_p (row));
15703 return rc;
15707 void
15708 set_vertical_scroll_bar (struct window *w)
15710 ptrdiff_t start, end, whole;
15712 /* Calculate the start and end positions for the current window.
15713 At some point, it would be nice to choose between scrollbars
15714 which reflect the whole buffer size, with special markers
15715 indicating narrowing, and scrollbars which reflect only the
15716 visible region.
15718 Note that mini-buffers sometimes aren't displaying any text. */
15719 if (!MINI_WINDOW_P (w)
15720 || (w == XWINDOW (minibuf_window)
15721 && NILP (echo_area_buffer[0])))
15723 struct buffer *buf = XBUFFER (w->contents);
15724 whole = BUF_ZV (buf) - BUF_BEGV (buf);
15725 start = marker_position (w->start) - BUF_BEGV (buf);
15726 /* I don't think this is guaranteed to be right. For the
15727 moment, we'll pretend it is. */
15728 end = BUF_Z (buf) - w->window_end_pos - BUF_BEGV (buf);
15730 if (end < start)
15731 end = start;
15732 if (whole < (end - start))
15733 whole = end - start;
15735 else
15736 start = end = whole = 0;
15738 /* Indicate what this scroll bar ought to be displaying now. */
15739 if (FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
15740 (*FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
15741 (w, end - start, whole, start);
15745 void
15746 set_horizontal_scroll_bar (struct window *w)
15748 int start, end, whole, portion;
15750 if (!MINI_WINDOW_P (w)
15751 || (w == XWINDOW (minibuf_window)
15752 && NILP (echo_area_buffer[0])))
15754 struct buffer *b = XBUFFER (w->contents);
15755 struct buffer *old_buffer = NULL;
15756 struct it it;
15757 struct text_pos startp;
15759 if (b != current_buffer)
15761 old_buffer = current_buffer;
15762 set_buffer_internal (b);
15765 SET_TEXT_POS_FROM_MARKER (startp, w->start);
15766 start_display (&it, w, startp);
15767 it.last_visible_x = INT_MAX;
15768 whole = move_it_to (&it, -1, INT_MAX, window_box_height (w), -1,
15769 MOVE_TO_X | MOVE_TO_Y);
15770 /* whole = move_it_to (&it, w->window_end_pos, INT_MAX,
15771 window_box_height (w), -1,
15772 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y); */
15774 start = w->hscroll * FRAME_COLUMN_WIDTH (WINDOW_XFRAME (w));
15775 end = start + window_box_width (w, TEXT_AREA);
15776 portion = end - start;
15777 /* After enlarging a horizontally scrolled window such that it
15778 gets at least as wide as the text it contains, make sure that
15779 the thumb doesn't fill the entire scroll bar so we can still
15780 drag it back to see the entire text. */
15781 whole = max (whole, end);
15783 if (it.bidi_p)
15785 Lisp_Object pdir;
15787 pdir = Fcurrent_bidi_paragraph_direction (Qnil);
15788 if (EQ (pdir, Qright_to_left))
15790 start = whole - end;
15791 end = start + portion;
15795 if (old_buffer)
15796 set_buffer_internal (old_buffer);
15798 else
15799 start = end = whole = portion = 0;
15801 w->hscroll_whole = whole;
15803 /* Indicate what this scroll bar ought to be displaying now. */
15804 if (FRAME_TERMINAL (XFRAME (w->frame))->set_horizontal_scroll_bar_hook)
15805 (*FRAME_TERMINAL (XFRAME (w->frame))->set_horizontal_scroll_bar_hook)
15806 (w, portion, whole, start);
15810 /* Redisplay leaf window WINDOW. JUST_THIS_ONE_P non-zero means only
15811 selected_window is redisplayed.
15813 We can return without actually redisplaying the window if fonts has been
15814 changed on window's frame. In that case, redisplay_internal will retry.
15816 As one of the important parts of redisplaying a window, we need to
15817 decide whether the previous window-start position (stored in the
15818 window's w->start marker position) is still valid, and if it isn't,
15819 recompute it. Some details about that:
15821 . The previous window-start could be in a continuation line, in
15822 which case we need to recompute it when the window width
15823 changes. See compute_window_start_on_continuation_line and its
15824 call below.
15826 . The text that changed since last redisplay could include the
15827 previous window-start position. In that case, we try to salvage
15828 what we can from the current glyph matrix by calling
15829 try_scrolling, which see.
15831 . Some Emacs command could force us to use a specific window-start
15832 position by setting the window's force_start flag, or gently
15833 propose doing that by setting the window's optional_new_start
15834 flag. In these cases, we try using the specified start point if
15835 that succeeds (i.e. the window desired matrix is successfully
15836 recomputed, and point location is within the window). In case
15837 of optional_new_start, we first check if the specified start
15838 position is feasible, i.e. if it will allow point to be
15839 displayed in the window. If using the specified start point
15840 fails, e.g., if new fonts are needed to be loaded, we abort the
15841 redisplay cycle and leave it up to the next cycle to figure out
15842 things.
15844 . Note that the window's force_start flag is sometimes set by
15845 redisplay itself, when it decides that the previous window start
15846 point is fine and should be kept. Search for "goto force_start"
15847 below to see the details. Like the values of window-start
15848 specified outside of redisplay, these internally-deduced values
15849 are tested for feasibility, and ignored if found to be
15850 unfeasible.
15852 . Note that the function try_window, used to completely redisplay
15853 a window, accepts the window's start point as its argument.
15854 This is used several times in the redisplay code to control
15855 where the window start will be, according to user options such
15856 as scroll-conservatively, and also to ensure the screen line
15857 showing point will be fully (as opposed to partially) visible on
15858 display. */
15860 static void
15861 redisplay_window (Lisp_Object window, bool just_this_one_p)
15863 struct window *w = XWINDOW (window);
15864 struct frame *f = XFRAME (w->frame);
15865 struct buffer *buffer = XBUFFER (w->contents);
15866 struct buffer *old = current_buffer;
15867 struct text_pos lpoint, opoint, startp;
15868 int update_mode_line;
15869 int tem;
15870 struct it it;
15871 /* Record it now because it's overwritten. */
15872 bool current_matrix_up_to_date_p = false;
15873 bool used_current_matrix_p = false;
15874 /* This is less strict than current_matrix_up_to_date_p.
15875 It indicates that the buffer contents and narrowing are unchanged. */
15876 bool buffer_unchanged_p = false;
15877 int temp_scroll_step = 0;
15878 ptrdiff_t count = SPECPDL_INDEX ();
15879 int rc;
15880 int centering_position = -1;
15881 int last_line_misfit = 0;
15882 ptrdiff_t beg_unchanged, end_unchanged;
15883 int frame_line_height;
15885 SET_TEXT_POS (lpoint, PT, PT_BYTE);
15886 opoint = lpoint;
15888 #ifdef GLYPH_DEBUG
15889 *w->desired_matrix->method = 0;
15890 #endif
15892 if (!just_this_one_p
15893 && REDISPLAY_SOME_P ()
15894 && !w->redisplay
15895 && !f->redisplay
15896 && !buffer->text->redisplay
15897 && BUF_PT (buffer) == w->last_point)
15898 return;
15900 /* Make sure that both W's markers are valid. */
15901 eassert (XMARKER (w->start)->buffer == buffer);
15902 eassert (XMARKER (w->pointm)->buffer == buffer);
15904 /* We come here again if we need to run window-text-change-functions
15905 below. */
15906 restart:
15907 reconsider_clip_changes (w);
15908 frame_line_height = default_line_pixel_height (w);
15910 /* Has the mode line to be updated? */
15911 update_mode_line = (w->update_mode_line
15912 || update_mode_lines
15913 || buffer->clip_changed
15914 || buffer->prevent_redisplay_optimizations_p);
15916 if (!just_this_one_p)
15917 /* If `just_this_one_p' is set, we apparently set must_be_updated_p more
15918 cleverly elsewhere. */
15919 w->must_be_updated_p = true;
15921 if (MINI_WINDOW_P (w))
15923 if (w == XWINDOW (echo_area_window)
15924 && !NILP (echo_area_buffer[0]))
15926 if (update_mode_line)
15927 /* We may have to update a tty frame's menu bar or a
15928 tool-bar. Example `M-x C-h C-h C-g'. */
15929 goto finish_menu_bars;
15930 else
15931 /* We've already displayed the echo area glyphs in this window. */
15932 goto finish_scroll_bars;
15934 else if ((w != XWINDOW (minibuf_window)
15935 || minibuf_level == 0)
15936 /* When buffer is nonempty, redisplay window normally. */
15937 && BUF_Z (XBUFFER (w->contents)) == BUF_BEG (XBUFFER (w->contents))
15938 /* Quail displays non-mini buffers in minibuffer window.
15939 In that case, redisplay the window normally. */
15940 && !NILP (Fmemq (w->contents, Vminibuffer_list)))
15942 /* W is a mini-buffer window, but it's not active, so clear
15943 it. */
15944 int yb = window_text_bottom_y (w);
15945 struct glyph_row *row;
15946 int y;
15948 for (y = 0, row = w->desired_matrix->rows;
15949 y < yb;
15950 y += row->height, ++row)
15951 blank_row (w, row, y);
15952 goto finish_scroll_bars;
15955 clear_glyph_matrix (w->desired_matrix);
15958 /* Otherwise set up data on this window; select its buffer and point
15959 value. */
15960 /* Really select the buffer, for the sake of buffer-local
15961 variables. */
15962 set_buffer_internal_1 (XBUFFER (w->contents));
15964 current_matrix_up_to_date_p
15965 = (w->window_end_valid
15966 && !current_buffer->clip_changed
15967 && !current_buffer->prevent_redisplay_optimizations_p
15968 && !window_outdated (w));
15970 /* Run the window-text-change-functions
15971 if it is possible that the text on the screen has changed
15972 (either due to modification of the text, or any other reason). */
15973 if (!current_matrix_up_to_date_p
15974 && !NILP (Vwindow_text_change_functions))
15976 safe_run_hooks (Qwindow_text_change_functions);
15977 goto restart;
15980 beg_unchanged = BEG_UNCHANGED;
15981 end_unchanged = END_UNCHANGED;
15983 SET_TEXT_POS (opoint, PT, PT_BYTE);
15985 specbind (Qinhibit_point_motion_hooks, Qt);
15987 buffer_unchanged_p
15988 = (w->window_end_valid
15989 && !current_buffer->clip_changed
15990 && !window_outdated (w));
15992 /* When windows_or_buffers_changed is non-zero, we can't rely
15993 on the window end being valid, so set it to zero there. */
15994 if (windows_or_buffers_changed)
15996 /* If window starts on a continuation line, maybe adjust the
15997 window start in case the window's width changed. */
15998 if (XMARKER (w->start)->buffer == current_buffer)
15999 compute_window_start_on_continuation_line (w);
16001 w->window_end_valid = false;
16002 /* If so, we also can't rely on current matrix
16003 and should not fool try_cursor_movement below. */
16004 current_matrix_up_to_date_p = false;
16007 /* Some sanity checks. */
16008 CHECK_WINDOW_END (w);
16009 if (Z == Z_BYTE && CHARPOS (opoint) != BYTEPOS (opoint))
16010 emacs_abort ();
16011 if (BYTEPOS (opoint) < CHARPOS (opoint))
16012 emacs_abort ();
16014 if (mode_line_update_needed (w))
16015 update_mode_line = 1;
16017 /* Point refers normally to the selected window. For any other
16018 window, set up appropriate value. */
16019 if (!EQ (window, selected_window))
16021 ptrdiff_t new_pt = marker_position (w->pointm);
16022 ptrdiff_t new_pt_byte = marker_byte_position (w->pointm);
16024 if (new_pt < BEGV)
16026 new_pt = BEGV;
16027 new_pt_byte = BEGV_BYTE;
16028 set_marker_both (w->pointm, Qnil, BEGV, BEGV_BYTE);
16030 else if (new_pt > (ZV - 1))
16032 new_pt = ZV;
16033 new_pt_byte = ZV_BYTE;
16034 set_marker_both (w->pointm, Qnil, ZV, ZV_BYTE);
16037 /* We don't use SET_PT so that the point-motion hooks don't run. */
16038 TEMP_SET_PT_BOTH (new_pt, new_pt_byte);
16041 /* If any of the character widths specified in the display table
16042 have changed, invalidate the width run cache. It's true that
16043 this may be a bit late to catch such changes, but the rest of
16044 redisplay goes (non-fatally) haywire when the display table is
16045 changed, so why should we worry about doing any better? */
16046 if (current_buffer->width_run_cache
16047 || (current_buffer->base_buffer
16048 && current_buffer->base_buffer->width_run_cache))
16050 struct Lisp_Char_Table *disptab = buffer_display_table ();
16052 if (! disptab_matches_widthtab
16053 (disptab, XVECTOR (BVAR (current_buffer, width_table))))
16055 struct buffer *buf = current_buffer;
16057 if (buf->base_buffer)
16058 buf = buf->base_buffer;
16059 invalidate_region_cache (buf, buf->width_run_cache, BEG, Z);
16060 recompute_width_table (current_buffer, disptab);
16064 /* If window-start is screwed up, choose a new one. */
16065 if (XMARKER (w->start)->buffer != current_buffer)
16066 goto recenter;
16068 SET_TEXT_POS_FROM_MARKER (startp, w->start);
16070 /* If someone specified a new starting point but did not insist,
16071 check whether it can be used. */
16072 if ((w->optional_new_start || window_frozen_p (w))
16073 && CHARPOS (startp) >= BEGV
16074 && CHARPOS (startp) <= ZV)
16076 ptrdiff_t it_charpos;
16078 w->optional_new_start = 0;
16079 start_display (&it, w, startp);
16080 move_it_to (&it, PT, 0, it.last_visible_y, -1,
16081 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
16082 /* Record IT's position now, since line_bottom_y might change
16083 that. */
16084 it_charpos = IT_CHARPOS (it);
16085 /* Make sure we set the force_start flag only if the cursor row
16086 will be fully visible. Otherwise, the code under force_start
16087 label below will try to move point back into view, which is
16088 not what the code which sets optional_new_start wants. */
16089 if ((it.current_y == 0 || line_bottom_y (&it) < it.last_visible_y)
16090 && !w->force_start)
16092 if (it_charpos == PT)
16093 w->force_start = 1;
16094 /* IT may overshoot PT if text at PT is invisible. */
16095 else if (it_charpos > PT && CHARPOS (startp) <= PT)
16096 w->force_start = 1;
16097 #ifdef GLYPH_DEBUG
16098 if (w->force_start)
16100 if (window_frozen_p (w))
16101 debug_method_add (w, "set force_start from frozen window start");
16102 else
16103 debug_method_add (w, "set force_start from optional_new_start");
16105 #endif
16109 force_start:
16111 /* Handle case where place to start displaying has been specified,
16112 unless the specified location is outside the accessible range. */
16113 if (w->force_start)
16115 /* We set this later on if we have to adjust point. */
16116 int new_vpos = -1;
16118 w->force_start = 0;
16119 w->vscroll = 0;
16120 w->window_end_valid = 0;
16122 /* Forget any recorded base line for line number display. */
16123 if (!buffer_unchanged_p)
16124 w->base_line_number = 0;
16126 /* Redisplay the mode line. Select the buffer properly for that.
16127 Also, run the hook window-scroll-functions
16128 because we have scrolled. */
16129 /* Note, we do this after clearing force_start because
16130 if there's an error, it is better to forget about force_start
16131 than to get into an infinite loop calling the hook functions
16132 and having them get more errors. */
16133 if (!update_mode_line
16134 || ! NILP (Vwindow_scroll_functions))
16136 update_mode_line = 1;
16137 w->update_mode_line = 1;
16138 startp = run_window_scroll_functions (window, startp);
16141 if (CHARPOS (startp) < BEGV)
16142 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
16143 else if (CHARPOS (startp) > ZV)
16144 SET_TEXT_POS (startp, ZV, ZV_BYTE);
16146 /* Redisplay, then check if cursor has been set during the
16147 redisplay. Give up if new fonts were loaded. */
16148 /* We used to issue a CHECK_MARGINS argument to try_window here,
16149 but this causes scrolling to fail when point begins inside
16150 the scroll margin (bug#148) -- cyd */
16151 if (!try_window (window, startp, 0))
16153 w->force_start = 1;
16154 clear_glyph_matrix (w->desired_matrix);
16155 goto need_larger_matrices;
16158 if (w->cursor.vpos < 0)
16160 /* If point does not appear, try to move point so it does
16161 appear. The desired matrix has been built above, so we
16162 can use it here. */
16163 new_vpos = window_box_height (w) / 2;
16166 if (!cursor_row_fully_visible_p (w, 0, 0))
16168 /* Point does appear, but on a line partly visible at end of window.
16169 Move it back to a fully-visible line. */
16170 new_vpos = window_box_height (w);
16171 /* But if window_box_height suggests a Y coordinate that is
16172 not less than we already have, that line will clearly not
16173 be fully visible, so give up and scroll the display.
16174 This can happen when the default face uses a font whose
16175 dimensions are different from the frame's default
16176 font. */
16177 if (new_vpos >= w->cursor.y)
16179 w->cursor.vpos = -1;
16180 clear_glyph_matrix (w->desired_matrix);
16181 goto try_to_scroll;
16184 else if (w->cursor.vpos >= 0)
16186 /* Some people insist on not letting point enter the scroll
16187 margin, even though this part handles windows that didn't
16188 scroll at all. */
16189 int window_total_lines
16190 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
16191 int margin = min (scroll_margin, window_total_lines / 4);
16192 int pixel_margin = margin * frame_line_height;
16193 bool header_line = WINDOW_WANTS_HEADER_LINE_P (w);
16195 /* Note: We add an extra FRAME_LINE_HEIGHT, because the loop
16196 below, which finds the row to move point to, advances by
16197 the Y coordinate of the _next_ row, see the definition of
16198 MATRIX_ROW_BOTTOM_Y. */
16199 if (w->cursor.vpos < margin + header_line)
16201 w->cursor.vpos = -1;
16202 clear_glyph_matrix (w->desired_matrix);
16203 goto try_to_scroll;
16205 else
16207 int window_height = window_box_height (w);
16209 if (header_line)
16210 window_height += CURRENT_HEADER_LINE_HEIGHT (w);
16211 if (w->cursor.y >= window_height - pixel_margin)
16213 w->cursor.vpos = -1;
16214 clear_glyph_matrix (w->desired_matrix);
16215 goto try_to_scroll;
16220 /* If we need to move point for either of the above reasons,
16221 now actually do it. */
16222 if (new_vpos >= 0)
16224 struct glyph_row *row;
16226 row = MATRIX_FIRST_TEXT_ROW (w->desired_matrix);
16227 while (MATRIX_ROW_BOTTOM_Y (row) < new_vpos)
16228 ++row;
16230 TEMP_SET_PT_BOTH (MATRIX_ROW_START_CHARPOS (row),
16231 MATRIX_ROW_START_BYTEPOS (row));
16233 if (w != XWINDOW (selected_window))
16234 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
16235 else if (current_buffer == old)
16236 SET_TEXT_POS (lpoint, PT, PT_BYTE);
16238 set_cursor_from_row (w, row, w->desired_matrix, 0, 0, 0, 0);
16240 /* Re-run pre-redisplay-function so it can update the region
16241 according to the new position of point. */
16242 /* Other than the cursor, w's redisplay is done so we can set its
16243 redisplay to false. Also the buffer's redisplay can be set to
16244 false, since propagate_buffer_redisplay should have already
16245 propagated its info to `w' anyway. */
16246 w->redisplay = false;
16247 XBUFFER (w->contents)->text->redisplay = false;
16248 safe__call1 (true, Vpre_redisplay_function, Fcons (window, Qnil));
16250 if (w->redisplay || XBUFFER (w->contents)->text->redisplay)
16252 /* pre-redisplay-function made changes (e.g. move the region)
16253 that require another round of redisplay. */
16254 clear_glyph_matrix (w->desired_matrix);
16255 if (!try_window (window, startp, 0))
16256 goto need_larger_matrices;
16259 if (w->cursor.vpos < 0 || !cursor_row_fully_visible_p (w, 0, 0))
16261 clear_glyph_matrix (w->desired_matrix);
16262 goto try_to_scroll;
16265 #ifdef GLYPH_DEBUG
16266 debug_method_add (w, "forced window start");
16267 #endif
16268 goto done;
16271 /* Handle case where text has not changed, only point, and it has
16272 not moved off the frame, and we are not retrying after hscroll.
16273 (current_matrix_up_to_date_p is nonzero when retrying.) */
16274 if (current_matrix_up_to_date_p
16275 && (rc = try_cursor_movement (window, startp, &temp_scroll_step),
16276 rc != CURSOR_MOVEMENT_CANNOT_BE_USED))
16278 switch (rc)
16280 case CURSOR_MOVEMENT_SUCCESS:
16281 used_current_matrix_p = 1;
16282 goto done;
16284 case CURSOR_MOVEMENT_MUST_SCROLL:
16285 goto try_to_scroll;
16287 default:
16288 emacs_abort ();
16291 /* If current starting point was originally the beginning of a line
16292 but no longer is, find a new starting point. */
16293 else if (w->start_at_line_beg
16294 && !(CHARPOS (startp) <= BEGV
16295 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n'))
16297 #ifdef GLYPH_DEBUG
16298 debug_method_add (w, "recenter 1");
16299 #endif
16300 goto recenter;
16303 /* Try scrolling with try_window_id. Value is > 0 if update has
16304 been done, it is -1 if we know that the same window start will
16305 not work. It is 0 if unsuccessful for some other reason. */
16306 else if ((tem = try_window_id (w)) != 0)
16308 #ifdef GLYPH_DEBUG
16309 debug_method_add (w, "try_window_id %d", tem);
16310 #endif
16312 if (f->fonts_changed)
16313 goto need_larger_matrices;
16314 if (tem > 0)
16315 goto done;
16317 /* Otherwise try_window_id has returned -1 which means that we
16318 don't want the alternative below this comment to execute. */
16320 else if (CHARPOS (startp) >= BEGV
16321 && CHARPOS (startp) <= ZV
16322 && PT >= CHARPOS (startp)
16323 && (CHARPOS (startp) < ZV
16324 /* Avoid starting at end of buffer. */
16325 || CHARPOS (startp) == BEGV
16326 || !window_outdated (w)))
16328 int d1, d2, d5, d6;
16329 int rtop, rbot;
16331 /* If first window line is a continuation line, and window start
16332 is inside the modified region, but the first change is before
16333 current window start, we must select a new window start.
16335 However, if this is the result of a down-mouse event (e.g. by
16336 extending the mouse-drag-overlay), we don't want to select a
16337 new window start, since that would change the position under
16338 the mouse, resulting in an unwanted mouse-movement rather
16339 than a simple mouse-click. */
16340 if (!w->start_at_line_beg
16341 && NILP (do_mouse_tracking)
16342 && CHARPOS (startp) > BEGV
16343 && CHARPOS (startp) > BEG + beg_unchanged
16344 && CHARPOS (startp) <= Z - end_unchanged
16345 /* Even if w->start_at_line_beg is nil, a new window may
16346 start at a line_beg, since that's how set_buffer_window
16347 sets it. So, we need to check the return value of
16348 compute_window_start_on_continuation_line. (See also
16349 bug#197). */
16350 && XMARKER (w->start)->buffer == current_buffer
16351 && compute_window_start_on_continuation_line (w)
16352 /* It doesn't make sense to force the window start like we
16353 do at label force_start if it is already known that point
16354 will not be fully visible in the resulting window, because
16355 doing so will move point from its correct position
16356 instead of scrolling the window to bring point into view.
16357 See bug#9324. */
16358 && pos_visible_p (w, PT, &d1, &d2, &rtop, &rbot, &d5, &d6)
16359 /* A very tall row could need more than the window height,
16360 in which case we accept that it is partially visible. */
16361 && (rtop != 0) == (rbot != 0))
16363 w->force_start = 1;
16364 SET_TEXT_POS_FROM_MARKER (startp, w->start);
16365 #ifdef GLYPH_DEBUG
16366 debug_method_add (w, "recomputed window start in continuation line");
16367 #endif
16368 goto force_start;
16371 #ifdef GLYPH_DEBUG
16372 debug_method_add (w, "same window start");
16373 #endif
16375 /* Try to redisplay starting at same place as before.
16376 If point has not moved off frame, accept the results. */
16377 if (!current_matrix_up_to_date_p
16378 /* Don't use try_window_reusing_current_matrix in this case
16379 because a window scroll function can have changed the
16380 buffer. */
16381 || !NILP (Vwindow_scroll_functions)
16382 || MINI_WINDOW_P (w)
16383 || !(used_current_matrix_p
16384 = try_window_reusing_current_matrix (w)))
16386 IF_DEBUG (debug_method_add (w, "1"));
16387 if (try_window (window, startp, TRY_WINDOW_CHECK_MARGINS) < 0)
16388 /* -1 means we need to scroll.
16389 0 means we need new matrices, but fonts_changed
16390 is set in that case, so we will detect it below. */
16391 goto try_to_scroll;
16394 if (f->fonts_changed)
16395 goto need_larger_matrices;
16397 if (w->cursor.vpos >= 0)
16399 if (!just_this_one_p
16400 || current_buffer->clip_changed
16401 || BEG_UNCHANGED < CHARPOS (startp))
16402 /* Forget any recorded base line for line number display. */
16403 w->base_line_number = 0;
16405 if (!cursor_row_fully_visible_p (w, 1, 0))
16407 clear_glyph_matrix (w->desired_matrix);
16408 last_line_misfit = 1;
16410 /* Drop through and scroll. */
16411 else
16412 goto done;
16414 else
16415 clear_glyph_matrix (w->desired_matrix);
16418 try_to_scroll:
16420 /* Redisplay the mode line. Select the buffer properly for that. */
16421 if (!update_mode_line)
16423 update_mode_line = 1;
16424 w->update_mode_line = 1;
16427 /* Try to scroll by specified few lines. */
16428 if ((scroll_conservatively
16429 || emacs_scroll_step
16430 || temp_scroll_step
16431 || NUMBERP (BVAR (current_buffer, scroll_up_aggressively))
16432 || NUMBERP (BVAR (current_buffer, scroll_down_aggressively)))
16433 && CHARPOS (startp) >= BEGV
16434 && CHARPOS (startp) <= ZV)
16436 /* The function returns -1 if new fonts were loaded, 1 if
16437 successful, 0 if not successful. */
16438 int ss = try_scrolling (window, just_this_one_p,
16439 scroll_conservatively,
16440 emacs_scroll_step,
16441 temp_scroll_step, last_line_misfit);
16442 switch (ss)
16444 case SCROLLING_SUCCESS:
16445 goto done;
16447 case SCROLLING_NEED_LARGER_MATRICES:
16448 goto need_larger_matrices;
16450 case SCROLLING_FAILED:
16451 break;
16453 default:
16454 emacs_abort ();
16458 /* Finally, just choose a place to start which positions point
16459 according to user preferences. */
16461 recenter:
16463 #ifdef GLYPH_DEBUG
16464 debug_method_add (w, "recenter");
16465 #endif
16467 /* Forget any previously recorded base line for line number display. */
16468 if (!buffer_unchanged_p)
16469 w->base_line_number = 0;
16471 /* Determine the window start relative to point. */
16472 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
16473 it.current_y = it.last_visible_y;
16474 if (centering_position < 0)
16476 int window_total_lines
16477 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
16478 int margin
16479 = scroll_margin > 0
16480 ? min (scroll_margin, window_total_lines / 4)
16481 : 0;
16482 ptrdiff_t margin_pos = CHARPOS (startp);
16483 Lisp_Object aggressive;
16484 int scrolling_up;
16486 /* If there is a scroll margin at the top of the window, find
16487 its character position. */
16488 if (margin
16489 /* Cannot call start_display if startp is not in the
16490 accessible region of the buffer. This can happen when we
16491 have just switched to a different buffer and/or changed
16492 its restriction. In that case, startp is initialized to
16493 the character position 1 (BEGV) because we did not yet
16494 have chance to display the buffer even once. */
16495 && BEGV <= CHARPOS (startp) && CHARPOS (startp) <= ZV)
16497 struct it it1;
16498 void *it1data = NULL;
16500 SAVE_IT (it1, it, it1data);
16501 start_display (&it1, w, startp);
16502 move_it_vertically (&it1, margin * frame_line_height);
16503 margin_pos = IT_CHARPOS (it1);
16504 RESTORE_IT (&it, &it, it1data);
16506 scrolling_up = PT > margin_pos;
16507 aggressive =
16508 scrolling_up
16509 ? BVAR (current_buffer, scroll_up_aggressively)
16510 : BVAR (current_buffer, scroll_down_aggressively);
16512 if (!MINI_WINDOW_P (w)
16513 && (scroll_conservatively > SCROLL_LIMIT || NUMBERP (aggressive)))
16515 int pt_offset = 0;
16517 /* Setting scroll-conservatively overrides
16518 scroll-*-aggressively. */
16519 if (!scroll_conservatively && NUMBERP (aggressive))
16521 double float_amount = XFLOATINT (aggressive);
16523 pt_offset = float_amount * WINDOW_BOX_TEXT_HEIGHT (w);
16524 if (pt_offset == 0 && float_amount > 0)
16525 pt_offset = 1;
16526 if (pt_offset && margin > 0)
16527 margin -= 1;
16529 /* Compute how much to move the window start backward from
16530 point so that point will be displayed where the user
16531 wants it. */
16532 if (scrolling_up)
16534 centering_position = it.last_visible_y;
16535 if (pt_offset)
16536 centering_position -= pt_offset;
16537 centering_position -=
16538 frame_line_height * (1 + margin + (last_line_misfit != 0))
16539 + WINDOW_HEADER_LINE_HEIGHT (w);
16540 /* Don't let point enter the scroll margin near top of
16541 the window. */
16542 if (centering_position < margin * frame_line_height)
16543 centering_position = margin * frame_line_height;
16545 else
16546 centering_position = margin * frame_line_height + pt_offset;
16548 else
16549 /* Set the window start half the height of the window backward
16550 from point. */
16551 centering_position = window_box_height (w) / 2;
16553 move_it_vertically_backward (&it, centering_position);
16555 eassert (IT_CHARPOS (it) >= BEGV);
16557 /* The function move_it_vertically_backward may move over more
16558 than the specified y-distance. If it->w is small, e.g. a
16559 mini-buffer window, we may end up in front of the window's
16560 display area. Start displaying at the start of the line
16561 containing PT in this case. */
16562 if (it.current_y <= 0)
16564 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
16565 move_it_vertically_backward (&it, 0);
16566 it.current_y = 0;
16569 it.current_x = it.hpos = 0;
16571 /* Set the window start position here explicitly, to avoid an
16572 infinite loop in case the functions in window-scroll-functions
16573 get errors. */
16574 set_marker_both (w->start, Qnil, IT_CHARPOS (it), IT_BYTEPOS (it));
16576 /* Run scroll hooks. */
16577 startp = run_window_scroll_functions (window, it.current.pos);
16579 /* Redisplay the window. */
16580 if (!current_matrix_up_to_date_p
16581 || windows_or_buffers_changed
16582 || f->cursor_type_changed
16583 /* Don't use try_window_reusing_current_matrix in this case
16584 because it can have changed the buffer. */
16585 || !NILP (Vwindow_scroll_functions)
16586 || !just_this_one_p
16587 || MINI_WINDOW_P (w)
16588 || !(used_current_matrix_p
16589 = try_window_reusing_current_matrix (w)))
16590 try_window (window, startp, 0);
16592 /* If new fonts have been loaded (due to fontsets), give up. We
16593 have to start a new redisplay since we need to re-adjust glyph
16594 matrices. */
16595 if (f->fonts_changed)
16596 goto need_larger_matrices;
16598 /* If cursor did not appear assume that the middle of the window is
16599 in the first line of the window. Do it again with the next line.
16600 (Imagine a window of height 100, displaying two lines of height
16601 60. Moving back 50 from it->last_visible_y will end in the first
16602 line.) */
16603 if (w->cursor.vpos < 0)
16605 if (w->window_end_valid && PT >= Z - w->window_end_pos)
16607 clear_glyph_matrix (w->desired_matrix);
16608 move_it_by_lines (&it, 1);
16609 try_window (window, it.current.pos, 0);
16611 else if (PT < IT_CHARPOS (it))
16613 clear_glyph_matrix (w->desired_matrix);
16614 move_it_by_lines (&it, -1);
16615 try_window (window, it.current.pos, 0);
16617 else
16619 /* Not much we can do about it. */
16623 /* Consider the following case: Window starts at BEGV, there is
16624 invisible, intangible text at BEGV, so that display starts at
16625 some point START > BEGV. It can happen that we are called with
16626 PT somewhere between BEGV and START. Try to handle that case,
16627 and similar ones. */
16628 if (w->cursor.vpos < 0)
16630 /* First, try locating the proper glyph row for PT. */
16631 struct glyph_row *row =
16632 row_containing_pos (w, PT, w->current_matrix->rows, NULL, 0);
16634 /* Sometimes point is at the beginning of invisible text that is
16635 before the 1st character displayed in the row. In that case,
16636 row_containing_pos fails to find the row, because no glyphs
16637 with appropriate buffer positions are present in the row.
16638 Therefore, we next try to find the row which shows the 1st
16639 position after the invisible text. */
16640 if (!row)
16642 Lisp_Object val =
16643 get_char_property_and_overlay (make_number (PT), Qinvisible,
16644 Qnil, NULL);
16646 if (TEXT_PROP_MEANS_INVISIBLE (val))
16648 ptrdiff_t alt_pos;
16649 Lisp_Object invis_end =
16650 Fnext_single_char_property_change (make_number (PT), Qinvisible,
16651 Qnil, Qnil);
16653 if (NATNUMP (invis_end))
16654 alt_pos = XFASTINT (invis_end);
16655 else
16656 alt_pos = ZV;
16657 row = row_containing_pos (w, alt_pos, w->current_matrix->rows,
16658 NULL, 0);
16661 /* Finally, fall back on the first row of the window after the
16662 header line (if any). This is slightly better than not
16663 displaying the cursor at all. */
16664 if (!row)
16666 row = w->current_matrix->rows;
16667 if (row->mode_line_p)
16668 ++row;
16670 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
16673 if (!cursor_row_fully_visible_p (w, 0, 0))
16675 /* If vscroll is enabled, disable it and try again. */
16676 if (w->vscroll)
16678 w->vscroll = 0;
16679 clear_glyph_matrix (w->desired_matrix);
16680 goto recenter;
16683 /* Users who set scroll-conservatively to a large number want
16684 point just above/below the scroll margin. If we ended up
16685 with point's row partially visible, move the window start to
16686 make that row fully visible and out of the margin. */
16687 if (scroll_conservatively > SCROLL_LIMIT)
16689 int window_total_lines
16690 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) * frame_line_height;
16691 int margin =
16692 scroll_margin > 0
16693 ? min (scroll_margin, window_total_lines / 4)
16694 : 0;
16695 int move_down = w->cursor.vpos >= window_total_lines / 2;
16697 move_it_by_lines (&it, move_down ? margin + 1 : -(margin + 1));
16698 clear_glyph_matrix (w->desired_matrix);
16699 if (1 == try_window (window, it.current.pos,
16700 TRY_WINDOW_CHECK_MARGINS))
16701 goto done;
16704 /* If centering point failed to make the whole line visible,
16705 put point at the top instead. That has to make the whole line
16706 visible, if it can be done. */
16707 if (centering_position == 0)
16708 goto done;
16710 clear_glyph_matrix (w->desired_matrix);
16711 centering_position = 0;
16712 goto recenter;
16715 done:
16717 SET_TEXT_POS_FROM_MARKER (startp, w->start);
16718 w->start_at_line_beg = (CHARPOS (startp) == BEGV
16719 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n');
16721 /* Display the mode line, if we must. */
16722 if ((update_mode_line
16723 /* If window not full width, must redo its mode line
16724 if (a) the window to its side is being redone and
16725 (b) we do a frame-based redisplay. This is a consequence
16726 of how inverted lines are drawn in frame-based redisplay. */
16727 || (!just_this_one_p
16728 && !FRAME_WINDOW_P (f)
16729 && !WINDOW_FULL_WIDTH_P (w))
16730 /* Line number to display. */
16731 || w->base_line_pos > 0
16732 /* Column number is displayed and different from the one displayed. */
16733 || (w->column_number_displayed != -1
16734 && (w->column_number_displayed != current_column ())))
16735 /* This means that the window has a mode line. */
16736 && (WINDOW_WANTS_MODELINE_P (w)
16737 || WINDOW_WANTS_HEADER_LINE_P (w)))
16740 display_mode_lines (w);
16742 /* If mode line height has changed, arrange for a thorough
16743 immediate redisplay using the correct mode line height. */
16744 if (WINDOW_WANTS_MODELINE_P (w)
16745 && CURRENT_MODE_LINE_HEIGHT (w) != DESIRED_MODE_LINE_HEIGHT (w))
16747 f->fonts_changed = 1;
16748 w->mode_line_height = -1;
16749 MATRIX_MODE_LINE_ROW (w->current_matrix)->height
16750 = DESIRED_MODE_LINE_HEIGHT (w);
16753 /* If header line height has changed, arrange for a thorough
16754 immediate redisplay using the correct header line height. */
16755 if (WINDOW_WANTS_HEADER_LINE_P (w)
16756 && CURRENT_HEADER_LINE_HEIGHT (w) != DESIRED_HEADER_LINE_HEIGHT (w))
16758 f->fonts_changed = 1;
16759 w->header_line_height = -1;
16760 MATRIX_HEADER_LINE_ROW (w->current_matrix)->height
16761 = DESIRED_HEADER_LINE_HEIGHT (w);
16764 if (f->fonts_changed)
16765 goto need_larger_matrices;
16768 if (!line_number_displayed && w->base_line_pos != -1)
16770 w->base_line_pos = 0;
16771 w->base_line_number = 0;
16774 finish_menu_bars:
16776 /* When we reach a frame's selected window, redo the frame's menu bar. */
16777 if (update_mode_line
16778 && EQ (FRAME_SELECTED_WINDOW (f), window))
16780 int redisplay_menu_p = 0;
16782 if (FRAME_WINDOW_P (f))
16784 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
16785 || defined (HAVE_NS) || defined (USE_GTK)
16786 redisplay_menu_p = FRAME_EXTERNAL_MENU_BAR (f);
16787 #else
16788 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
16789 #endif
16791 else
16792 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
16794 if (redisplay_menu_p)
16795 display_menu_bar (w);
16797 #ifdef HAVE_WINDOW_SYSTEM
16798 if (FRAME_WINDOW_P (f))
16800 #if defined (USE_GTK) || defined (HAVE_NS)
16801 if (FRAME_EXTERNAL_TOOL_BAR (f))
16802 redisplay_tool_bar (f);
16803 #else
16804 if (WINDOWP (f->tool_bar_window)
16805 && (FRAME_TOOL_BAR_LINES (f) > 0
16806 || !NILP (Vauto_resize_tool_bars))
16807 && redisplay_tool_bar (f))
16808 ignore_mouse_drag_p = 1;
16809 #endif
16811 #endif
16814 #ifdef HAVE_WINDOW_SYSTEM
16815 if (FRAME_WINDOW_P (f)
16816 && update_window_fringes (w, (just_this_one_p
16817 || (!used_current_matrix_p && !overlay_arrow_seen)
16818 || w->pseudo_window_p)))
16820 update_begin (f);
16821 block_input ();
16822 if (draw_window_fringes (w, 1))
16824 if (WINDOW_RIGHT_DIVIDER_WIDTH (w))
16825 x_draw_right_divider (w);
16826 else
16827 x_draw_vertical_border (w);
16829 unblock_input ();
16830 update_end (f);
16833 if (WINDOW_BOTTOM_DIVIDER_WIDTH (w))
16834 x_draw_bottom_divider (w);
16835 #endif /* HAVE_WINDOW_SYSTEM */
16837 /* We go to this label, with fonts_changed set, if it is
16838 necessary to try again using larger glyph matrices.
16839 We have to redeem the scroll bar even in this case,
16840 because the loop in redisplay_internal expects that. */
16841 need_larger_matrices:
16843 finish_scroll_bars:
16845 if (WINDOW_HAS_VERTICAL_SCROLL_BAR (w) || WINDOW_HAS_HORIZONTAL_SCROLL_BAR (w))
16847 if (WINDOW_HAS_VERTICAL_SCROLL_BAR (w))
16848 /* Set the thumb's position and size. */
16849 set_vertical_scroll_bar (w);
16851 if (WINDOW_HAS_HORIZONTAL_SCROLL_BAR (w))
16852 /* Set the thumb's position and size. */
16853 set_horizontal_scroll_bar (w);
16855 /* Note that we actually used the scroll bar attached to this
16856 window, so it shouldn't be deleted at the end of redisplay. */
16857 if (FRAME_TERMINAL (f)->redeem_scroll_bar_hook)
16858 (*FRAME_TERMINAL (f)->redeem_scroll_bar_hook) (w);
16861 /* Restore current_buffer and value of point in it. The window
16862 update may have changed the buffer, so first make sure `opoint'
16863 is still valid (Bug#6177). */
16864 if (CHARPOS (opoint) < BEGV)
16865 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
16866 else if (CHARPOS (opoint) > ZV)
16867 TEMP_SET_PT_BOTH (Z, Z_BYTE);
16868 else
16869 TEMP_SET_PT_BOTH (CHARPOS (opoint), BYTEPOS (opoint));
16871 set_buffer_internal_1 (old);
16872 /* Avoid an abort in TEMP_SET_PT_BOTH if the buffer has become
16873 shorter. This can be caused by log truncation in *Messages*. */
16874 if (CHARPOS (lpoint) <= ZV)
16875 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
16877 unbind_to (count, Qnil);
16881 /* Build the complete desired matrix of WINDOW with a window start
16882 buffer position POS.
16884 Value is 1 if successful. It is zero if fonts were loaded during
16885 redisplay which makes re-adjusting glyph matrices necessary, and -1
16886 if point would appear in the scroll margins.
16887 (We check the former only if TRY_WINDOW_IGNORE_FONTS_CHANGE is
16888 unset in FLAGS, and the latter only if TRY_WINDOW_CHECK_MARGINS is
16889 set in FLAGS.) */
16892 try_window (Lisp_Object window, struct text_pos pos, int flags)
16894 struct window *w = XWINDOW (window);
16895 struct it it;
16896 struct glyph_row *last_text_row = NULL;
16897 struct frame *f = XFRAME (w->frame);
16898 int frame_line_height = default_line_pixel_height (w);
16900 /* Make POS the new window start. */
16901 set_marker_both (w->start, Qnil, CHARPOS (pos), BYTEPOS (pos));
16903 /* Mark cursor position as unknown. No overlay arrow seen. */
16904 w->cursor.vpos = -1;
16905 overlay_arrow_seen = 0;
16907 /* Initialize iterator and info to start at POS. */
16908 start_display (&it, w, pos);
16909 it.glyph_row->reversed_p = false;
16911 /* Display all lines of W. */
16912 while (it.current_y < it.last_visible_y)
16914 if (display_line (&it))
16915 last_text_row = it.glyph_row - 1;
16916 if (f->fonts_changed && !(flags & TRY_WINDOW_IGNORE_FONTS_CHANGE))
16917 return 0;
16920 /* Don't let the cursor end in the scroll margins. */
16921 if ((flags & TRY_WINDOW_CHECK_MARGINS)
16922 && !MINI_WINDOW_P (w))
16924 int this_scroll_margin;
16925 int window_total_lines
16926 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
16928 if (scroll_margin > 0)
16930 this_scroll_margin = min (scroll_margin, window_total_lines / 4);
16931 this_scroll_margin *= frame_line_height;
16933 else
16934 this_scroll_margin = 0;
16936 if ((w->cursor.y >= 0 /* not vscrolled */
16937 && w->cursor.y < this_scroll_margin
16938 && CHARPOS (pos) > BEGV
16939 && IT_CHARPOS (it) < ZV)
16940 /* rms: considering make_cursor_line_fully_visible_p here
16941 seems to give wrong results. We don't want to recenter
16942 when the last line is partly visible, we want to allow
16943 that case to be handled in the usual way. */
16944 || w->cursor.y > it.last_visible_y - this_scroll_margin - 1)
16946 w->cursor.vpos = -1;
16947 clear_glyph_matrix (w->desired_matrix);
16948 return -1;
16952 /* If bottom moved off end of frame, change mode line percentage. */
16953 if (w->window_end_pos <= 0 && Z != IT_CHARPOS (it))
16954 w->update_mode_line = 1;
16956 /* Set window_end_pos to the offset of the last character displayed
16957 on the window from the end of current_buffer. Set
16958 window_end_vpos to its row number. */
16959 if (last_text_row)
16961 eassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_text_row));
16962 adjust_window_ends (w, last_text_row, 0);
16963 eassert
16964 (MATRIX_ROW_DISPLAYS_TEXT_P (MATRIX_ROW (w->desired_matrix,
16965 w->window_end_vpos)));
16967 else
16969 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
16970 w->window_end_pos = Z - ZV;
16971 w->window_end_vpos = 0;
16974 /* But that is not valid info until redisplay finishes. */
16975 w->window_end_valid = 0;
16976 return 1;
16981 /************************************************************************
16982 Window redisplay reusing current matrix when buffer has not changed
16983 ************************************************************************/
16985 /* Try redisplay of window W showing an unchanged buffer with a
16986 different window start than the last time it was displayed by
16987 reusing its current matrix. Value is non-zero if successful.
16988 W->start is the new window start. */
16990 static int
16991 try_window_reusing_current_matrix (struct window *w)
16993 struct frame *f = XFRAME (w->frame);
16994 struct glyph_row *bottom_row;
16995 struct it it;
16996 struct run run;
16997 struct text_pos start, new_start;
16998 int nrows_scrolled, i;
16999 struct glyph_row *last_text_row;
17000 struct glyph_row *last_reused_text_row;
17001 struct glyph_row *start_row;
17002 int start_vpos, min_y, max_y;
17004 #ifdef GLYPH_DEBUG
17005 if (inhibit_try_window_reusing)
17006 return 0;
17007 #endif
17009 if (/* This function doesn't handle terminal frames. */
17010 !FRAME_WINDOW_P (f)
17011 /* Don't try to reuse the display if windows have been split
17012 or such. */
17013 || windows_or_buffers_changed
17014 || f->cursor_type_changed)
17015 return 0;
17017 /* Can't do this if showing trailing whitespace. */
17018 if (!NILP (Vshow_trailing_whitespace))
17019 return 0;
17021 /* If top-line visibility has changed, give up. */
17022 if (WINDOW_WANTS_HEADER_LINE_P (w)
17023 != MATRIX_HEADER_LINE_ROW (w->current_matrix)->mode_line_p)
17024 return 0;
17026 /* Give up if old or new display is scrolled vertically. We could
17027 make this function handle this, but right now it doesn't. */
17028 start_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
17029 if (w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row))
17030 return 0;
17032 /* The variable new_start now holds the new window start. The old
17033 start `start' can be determined from the current matrix. */
17034 SET_TEXT_POS_FROM_MARKER (new_start, w->start);
17035 start = start_row->minpos;
17036 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
17038 /* Clear the desired matrix for the display below. */
17039 clear_glyph_matrix (w->desired_matrix);
17041 if (CHARPOS (new_start) <= CHARPOS (start))
17043 /* Don't use this method if the display starts with an ellipsis
17044 displayed for invisible text. It's not easy to handle that case
17045 below, and it's certainly not worth the effort since this is
17046 not a frequent case. */
17047 if (in_ellipses_for_invisible_text_p (&start_row->start, w))
17048 return 0;
17050 IF_DEBUG (debug_method_add (w, "twu1"));
17052 /* Display up to a row that can be reused. The variable
17053 last_text_row is set to the last row displayed that displays
17054 text. Note that it.vpos == 0 if or if not there is a
17055 header-line; it's not the same as the MATRIX_ROW_VPOS! */
17056 start_display (&it, w, new_start);
17057 w->cursor.vpos = -1;
17058 last_text_row = last_reused_text_row = NULL;
17060 while (it.current_y < it.last_visible_y && !f->fonts_changed)
17062 /* If we have reached into the characters in the START row,
17063 that means the line boundaries have changed. So we
17064 can't start copying with the row START. Maybe it will
17065 work to start copying with the following row. */
17066 while (IT_CHARPOS (it) > CHARPOS (start))
17068 /* Advance to the next row as the "start". */
17069 start_row++;
17070 start = start_row->minpos;
17071 /* If there are no more rows to try, or just one, give up. */
17072 if (start_row == MATRIX_MODE_LINE_ROW (w->current_matrix) - 1
17073 || w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row)
17074 || CHARPOS (start) == ZV)
17076 clear_glyph_matrix (w->desired_matrix);
17077 return 0;
17080 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
17082 /* If we have reached alignment, we can copy the rest of the
17083 rows. */
17084 if (IT_CHARPOS (it) == CHARPOS (start)
17085 /* Don't accept "alignment" inside a display vector,
17086 since start_row could have started in the middle of
17087 that same display vector (thus their character
17088 positions match), and we have no way of telling if
17089 that is the case. */
17090 && it.current.dpvec_index < 0)
17091 break;
17093 it.glyph_row->reversed_p = false;
17094 if (display_line (&it))
17095 last_text_row = it.glyph_row - 1;
17099 /* A value of current_y < last_visible_y means that we stopped
17100 at the previous window start, which in turn means that we
17101 have at least one reusable row. */
17102 if (it.current_y < it.last_visible_y)
17104 struct glyph_row *row;
17106 /* IT.vpos always starts from 0; it counts text lines. */
17107 nrows_scrolled = it.vpos - (start_row - MATRIX_FIRST_TEXT_ROW (w->current_matrix));
17109 /* Find PT if not already found in the lines displayed. */
17110 if (w->cursor.vpos < 0)
17112 int dy = it.current_y - start_row->y;
17114 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
17115 row = row_containing_pos (w, PT, row, NULL, dy);
17116 if (row)
17117 set_cursor_from_row (w, row, w->current_matrix, 0, 0,
17118 dy, nrows_scrolled);
17119 else
17121 clear_glyph_matrix (w->desired_matrix);
17122 return 0;
17126 /* Scroll the display. Do it before the current matrix is
17127 changed. The problem here is that update has not yet
17128 run, i.e. part of the current matrix is not up to date.
17129 scroll_run_hook will clear the cursor, and use the
17130 current matrix to get the height of the row the cursor is
17131 in. */
17132 run.current_y = start_row->y;
17133 run.desired_y = it.current_y;
17134 run.height = it.last_visible_y - it.current_y;
17136 if (run.height > 0 && run.current_y != run.desired_y)
17138 update_begin (f);
17139 FRAME_RIF (f)->update_window_begin_hook (w);
17140 FRAME_RIF (f)->clear_window_mouse_face (w);
17141 FRAME_RIF (f)->scroll_run_hook (w, &run);
17142 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
17143 update_end (f);
17146 /* Shift current matrix down by nrows_scrolled lines. */
17147 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
17148 rotate_matrix (w->current_matrix,
17149 start_vpos,
17150 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
17151 nrows_scrolled);
17153 /* Disable lines that must be updated. */
17154 for (i = 0; i < nrows_scrolled; ++i)
17155 (start_row + i)->enabled_p = false;
17157 /* Re-compute Y positions. */
17158 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
17159 max_y = it.last_visible_y;
17160 for (row = start_row + nrows_scrolled;
17161 row < bottom_row;
17162 ++row)
17164 row->y = it.current_y;
17165 row->visible_height = row->height;
17167 if (row->y < min_y)
17168 row->visible_height -= min_y - row->y;
17169 if (row->y + row->height > max_y)
17170 row->visible_height -= row->y + row->height - max_y;
17171 if (row->fringe_bitmap_periodic_p)
17172 row->redraw_fringe_bitmaps_p = 1;
17174 it.current_y += row->height;
17176 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
17177 last_reused_text_row = row;
17178 if (MATRIX_ROW_BOTTOM_Y (row) >= it.last_visible_y)
17179 break;
17182 /* Disable lines in the current matrix which are now
17183 below the window. */
17184 for (++row; row < bottom_row; ++row)
17185 row->enabled_p = row->mode_line_p = 0;
17188 /* Update window_end_pos etc.; last_reused_text_row is the last
17189 reused row from the current matrix containing text, if any.
17190 The value of last_text_row is the last displayed line
17191 containing text. */
17192 if (last_reused_text_row)
17193 adjust_window_ends (w, last_reused_text_row, 1);
17194 else if (last_text_row)
17195 adjust_window_ends (w, last_text_row, 0);
17196 else
17198 /* This window must be completely empty. */
17199 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
17200 w->window_end_pos = Z - ZV;
17201 w->window_end_vpos = 0;
17203 w->window_end_valid = 0;
17205 /* Update hint: don't try scrolling again in update_window. */
17206 w->desired_matrix->no_scrolling_p = 1;
17208 #ifdef GLYPH_DEBUG
17209 debug_method_add (w, "try_window_reusing_current_matrix 1");
17210 #endif
17211 return 1;
17213 else if (CHARPOS (new_start) > CHARPOS (start))
17215 struct glyph_row *pt_row, *row;
17216 struct glyph_row *first_reusable_row;
17217 struct glyph_row *first_row_to_display;
17218 int dy;
17219 int yb = window_text_bottom_y (w);
17221 /* Find the row starting at new_start, if there is one. Don't
17222 reuse a partially visible line at the end. */
17223 first_reusable_row = start_row;
17224 while (first_reusable_row->enabled_p
17225 && MATRIX_ROW_BOTTOM_Y (first_reusable_row) < yb
17226 && (MATRIX_ROW_START_CHARPOS (first_reusable_row)
17227 < CHARPOS (new_start)))
17228 ++first_reusable_row;
17230 /* Give up if there is no row to reuse. */
17231 if (MATRIX_ROW_BOTTOM_Y (first_reusable_row) >= yb
17232 || !first_reusable_row->enabled_p
17233 || (MATRIX_ROW_START_CHARPOS (first_reusable_row)
17234 != CHARPOS (new_start)))
17235 return 0;
17237 /* We can reuse fully visible rows beginning with
17238 first_reusable_row to the end of the window. Set
17239 first_row_to_display to the first row that cannot be reused.
17240 Set pt_row to the row containing point, if there is any. */
17241 pt_row = NULL;
17242 for (first_row_to_display = first_reusable_row;
17243 MATRIX_ROW_BOTTOM_Y (first_row_to_display) < yb;
17244 ++first_row_to_display)
17246 if (PT >= MATRIX_ROW_START_CHARPOS (first_row_to_display)
17247 && (PT < MATRIX_ROW_END_CHARPOS (first_row_to_display)
17248 || (PT == MATRIX_ROW_END_CHARPOS (first_row_to_display)
17249 && first_row_to_display->ends_at_zv_p
17250 && pt_row == NULL)))
17251 pt_row = first_row_to_display;
17254 /* Start displaying at the start of first_row_to_display. */
17255 eassert (first_row_to_display->y < yb);
17256 init_to_row_start (&it, w, first_row_to_display);
17258 nrows_scrolled = (MATRIX_ROW_VPOS (first_reusable_row, w->current_matrix)
17259 - start_vpos);
17260 it.vpos = (MATRIX_ROW_VPOS (first_row_to_display, w->current_matrix)
17261 - nrows_scrolled);
17262 it.current_y = (first_row_to_display->y - first_reusable_row->y
17263 + WINDOW_HEADER_LINE_HEIGHT (w));
17265 /* Display lines beginning with first_row_to_display in the
17266 desired matrix. Set last_text_row to the last row displayed
17267 that displays text. */
17268 it.glyph_row = MATRIX_ROW (w->desired_matrix, it.vpos);
17269 if (pt_row == NULL)
17270 w->cursor.vpos = -1;
17271 last_text_row = NULL;
17272 while (it.current_y < it.last_visible_y && !f->fonts_changed)
17273 if (display_line (&it))
17274 last_text_row = it.glyph_row - 1;
17276 /* If point is in a reused row, adjust y and vpos of the cursor
17277 position. */
17278 if (pt_row)
17280 w->cursor.vpos -= nrows_scrolled;
17281 w->cursor.y -= first_reusable_row->y - start_row->y;
17284 /* Give up if point isn't in a row displayed or reused. (This
17285 also handles the case where w->cursor.vpos < nrows_scrolled
17286 after the calls to display_line, which can happen with scroll
17287 margins. See bug#1295.) */
17288 if (w->cursor.vpos < 0)
17290 clear_glyph_matrix (w->desired_matrix);
17291 return 0;
17294 /* Scroll the display. */
17295 run.current_y = first_reusable_row->y;
17296 run.desired_y = WINDOW_HEADER_LINE_HEIGHT (w);
17297 run.height = it.last_visible_y - run.current_y;
17298 dy = run.current_y - run.desired_y;
17300 if (run.height)
17302 update_begin (f);
17303 FRAME_RIF (f)->update_window_begin_hook (w);
17304 FRAME_RIF (f)->clear_window_mouse_face (w);
17305 FRAME_RIF (f)->scroll_run_hook (w, &run);
17306 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
17307 update_end (f);
17310 /* Adjust Y positions of reused rows. */
17311 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
17312 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
17313 max_y = it.last_visible_y;
17314 for (row = first_reusable_row; row < first_row_to_display; ++row)
17316 row->y -= dy;
17317 row->visible_height = row->height;
17318 if (row->y < min_y)
17319 row->visible_height -= min_y - row->y;
17320 if (row->y + row->height > max_y)
17321 row->visible_height -= row->y + row->height - max_y;
17322 if (row->fringe_bitmap_periodic_p)
17323 row->redraw_fringe_bitmaps_p = 1;
17326 /* Scroll the current matrix. */
17327 eassert (nrows_scrolled > 0);
17328 rotate_matrix (w->current_matrix,
17329 start_vpos,
17330 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
17331 -nrows_scrolled);
17333 /* Disable rows not reused. */
17334 for (row -= nrows_scrolled; row < bottom_row; ++row)
17335 row->enabled_p = false;
17337 /* Point may have moved to a different line, so we cannot assume that
17338 the previous cursor position is valid; locate the correct row. */
17339 if (pt_row)
17341 for (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
17342 row < bottom_row
17343 && PT >= MATRIX_ROW_END_CHARPOS (row)
17344 && !row->ends_at_zv_p;
17345 row++)
17347 w->cursor.vpos++;
17348 w->cursor.y = row->y;
17350 if (row < bottom_row)
17352 /* Can't simply scan the row for point with
17353 bidi-reordered glyph rows. Let set_cursor_from_row
17354 figure out where to put the cursor, and if it fails,
17355 give up. */
17356 if (!NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering)))
17358 if (!set_cursor_from_row (w, row, w->current_matrix,
17359 0, 0, 0, 0))
17361 clear_glyph_matrix (w->desired_matrix);
17362 return 0;
17365 else
17367 struct glyph *glyph = row->glyphs[TEXT_AREA] + w->cursor.hpos;
17368 struct glyph *end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
17370 for (; glyph < end
17371 && (!BUFFERP (glyph->object)
17372 || glyph->charpos < PT);
17373 glyph++)
17375 w->cursor.hpos++;
17376 w->cursor.x += glyph->pixel_width;
17382 /* Adjust window end. A null value of last_text_row means that
17383 the window end is in reused rows which in turn means that
17384 only its vpos can have changed. */
17385 if (last_text_row)
17386 adjust_window_ends (w, last_text_row, 0);
17387 else
17388 w->window_end_vpos -= nrows_scrolled;
17390 w->window_end_valid = 0;
17391 w->desired_matrix->no_scrolling_p = 1;
17393 #ifdef GLYPH_DEBUG
17394 debug_method_add (w, "try_window_reusing_current_matrix 2");
17395 #endif
17396 return 1;
17399 return 0;
17404 /************************************************************************
17405 Window redisplay reusing current matrix when buffer has changed
17406 ************************************************************************/
17408 static struct glyph_row *find_last_unchanged_at_beg_row (struct window *);
17409 static struct glyph_row *find_first_unchanged_at_end_row (struct window *,
17410 ptrdiff_t *, ptrdiff_t *);
17411 static struct glyph_row *
17412 find_last_row_displaying_text (struct glyph_matrix *, struct it *,
17413 struct glyph_row *);
17416 /* Return the last row in MATRIX displaying text. If row START is
17417 non-null, start searching with that row. IT gives the dimensions
17418 of the display. Value is null if matrix is empty; otherwise it is
17419 a pointer to the row found. */
17421 static struct glyph_row *
17422 find_last_row_displaying_text (struct glyph_matrix *matrix, struct it *it,
17423 struct glyph_row *start)
17425 struct glyph_row *row, *row_found;
17427 /* Set row_found to the last row in IT->w's current matrix
17428 displaying text. The loop looks funny but think of partially
17429 visible lines. */
17430 row_found = NULL;
17431 row = start ? start : MATRIX_FIRST_TEXT_ROW (matrix);
17432 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
17434 eassert (row->enabled_p);
17435 row_found = row;
17436 if (MATRIX_ROW_BOTTOM_Y (row) >= it->last_visible_y)
17437 break;
17438 ++row;
17441 return row_found;
17445 /* Return the last row in the current matrix of W that is not affected
17446 by changes at the start of current_buffer that occurred since W's
17447 current matrix was built. Value is null if no such row exists.
17449 BEG_UNCHANGED us the number of characters unchanged at the start of
17450 current_buffer. BEG + BEG_UNCHANGED is the buffer position of the
17451 first changed character in current_buffer. Characters at positions <
17452 BEG + BEG_UNCHANGED are at the same buffer positions as they were
17453 when the current matrix was built. */
17455 static struct glyph_row *
17456 find_last_unchanged_at_beg_row (struct window *w)
17458 ptrdiff_t first_changed_pos = BEG + BEG_UNCHANGED;
17459 struct glyph_row *row;
17460 struct glyph_row *row_found = NULL;
17461 int yb = window_text_bottom_y (w);
17463 /* Find the last row displaying unchanged text. */
17464 for (row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
17465 MATRIX_ROW_DISPLAYS_TEXT_P (row)
17466 && MATRIX_ROW_START_CHARPOS (row) < first_changed_pos;
17467 ++row)
17469 if (/* If row ends before first_changed_pos, it is unchanged,
17470 except in some case. */
17471 MATRIX_ROW_END_CHARPOS (row) <= first_changed_pos
17472 /* When row ends in ZV and we write at ZV it is not
17473 unchanged. */
17474 && !row->ends_at_zv_p
17475 /* When first_changed_pos is the end of a continued line,
17476 row is not unchanged because it may be no longer
17477 continued. */
17478 && !(MATRIX_ROW_END_CHARPOS (row) == first_changed_pos
17479 && (row->continued_p
17480 || row->exact_window_width_line_p))
17481 /* If ROW->end is beyond ZV, then ROW->end is outdated and
17482 needs to be recomputed, so don't consider this row as
17483 unchanged. This happens when the last line was
17484 bidi-reordered and was killed immediately before this
17485 redisplay cycle. In that case, ROW->end stores the
17486 buffer position of the first visual-order character of
17487 the killed text, which is now beyond ZV. */
17488 && CHARPOS (row->end.pos) <= ZV)
17489 row_found = row;
17491 /* Stop if last visible row. */
17492 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
17493 break;
17496 return row_found;
17500 /* Find the first glyph row in the current matrix of W that is not
17501 affected by changes at the end of current_buffer since the
17502 time W's current matrix was built.
17504 Return in *DELTA the number of chars by which buffer positions in
17505 unchanged text at the end of current_buffer must be adjusted.
17507 Return in *DELTA_BYTES the corresponding number of bytes.
17509 Value is null if no such row exists, i.e. all rows are affected by
17510 changes. */
17512 static struct glyph_row *
17513 find_first_unchanged_at_end_row (struct window *w,
17514 ptrdiff_t *delta, ptrdiff_t *delta_bytes)
17516 struct glyph_row *row;
17517 struct glyph_row *row_found = NULL;
17519 *delta = *delta_bytes = 0;
17521 /* Display must not have been paused, otherwise the current matrix
17522 is not up to date. */
17523 eassert (w->window_end_valid);
17525 /* A value of window_end_pos >= END_UNCHANGED means that the window
17526 end is in the range of changed text. If so, there is no
17527 unchanged row at the end of W's current matrix. */
17528 if (w->window_end_pos >= END_UNCHANGED)
17529 return NULL;
17531 /* Set row to the last row in W's current matrix displaying text. */
17532 row = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
17534 /* If matrix is entirely empty, no unchanged row exists. */
17535 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
17537 /* The value of row is the last glyph row in the matrix having a
17538 meaningful buffer position in it. The end position of row
17539 corresponds to window_end_pos. This allows us to translate
17540 buffer positions in the current matrix to current buffer
17541 positions for characters not in changed text. */
17542 ptrdiff_t Z_old =
17543 MATRIX_ROW_END_CHARPOS (row) + w->window_end_pos;
17544 ptrdiff_t Z_BYTE_old =
17545 MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
17546 ptrdiff_t last_unchanged_pos, last_unchanged_pos_old;
17547 struct glyph_row *first_text_row
17548 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
17550 *delta = Z - Z_old;
17551 *delta_bytes = Z_BYTE - Z_BYTE_old;
17553 /* Set last_unchanged_pos to the buffer position of the last
17554 character in the buffer that has not been changed. Z is the
17555 index + 1 of the last character in current_buffer, i.e. by
17556 subtracting END_UNCHANGED we get the index of the last
17557 unchanged character, and we have to add BEG to get its buffer
17558 position. */
17559 last_unchanged_pos = Z - END_UNCHANGED + BEG;
17560 last_unchanged_pos_old = last_unchanged_pos - *delta;
17562 /* Search backward from ROW for a row displaying a line that
17563 starts at a minimum position >= last_unchanged_pos_old. */
17564 for (; row > first_text_row; --row)
17566 /* This used to abort, but it can happen.
17567 It is ok to just stop the search instead here. KFS. */
17568 if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
17569 break;
17571 if (MATRIX_ROW_START_CHARPOS (row) >= last_unchanged_pos_old)
17572 row_found = row;
17576 eassert (!row_found || MATRIX_ROW_DISPLAYS_TEXT_P (row_found));
17578 return row_found;
17582 /* Make sure that glyph rows in the current matrix of window W
17583 reference the same glyph memory as corresponding rows in the
17584 frame's frame matrix. This function is called after scrolling W's
17585 current matrix on a terminal frame in try_window_id and
17586 try_window_reusing_current_matrix. */
17588 static void
17589 sync_frame_with_window_matrix_rows (struct window *w)
17591 struct frame *f = XFRAME (w->frame);
17592 struct glyph_row *window_row, *window_row_end, *frame_row;
17594 /* Preconditions: W must be a leaf window and full-width. Its frame
17595 must have a frame matrix. */
17596 eassert (BUFFERP (w->contents));
17597 eassert (WINDOW_FULL_WIDTH_P (w));
17598 eassert (!FRAME_WINDOW_P (f));
17600 /* If W is a full-width window, glyph pointers in W's current matrix
17601 have, by definition, to be the same as glyph pointers in the
17602 corresponding frame matrix. Note that frame matrices have no
17603 marginal areas (see build_frame_matrix). */
17604 window_row = w->current_matrix->rows;
17605 window_row_end = window_row + w->current_matrix->nrows;
17606 frame_row = f->current_matrix->rows + WINDOW_TOP_EDGE_LINE (w);
17607 while (window_row < window_row_end)
17609 struct glyph *start = window_row->glyphs[LEFT_MARGIN_AREA];
17610 struct glyph *end = window_row->glyphs[LAST_AREA];
17612 frame_row->glyphs[LEFT_MARGIN_AREA] = start;
17613 frame_row->glyphs[TEXT_AREA] = start;
17614 frame_row->glyphs[RIGHT_MARGIN_AREA] = end;
17615 frame_row->glyphs[LAST_AREA] = end;
17617 /* Disable frame rows whose corresponding window rows have
17618 been disabled in try_window_id. */
17619 if (!window_row->enabled_p)
17620 frame_row->enabled_p = false;
17622 ++window_row, ++frame_row;
17627 /* Find the glyph row in window W containing CHARPOS. Consider all
17628 rows between START and END (not inclusive). END null means search
17629 all rows to the end of the display area of W. Value is the row
17630 containing CHARPOS or null. */
17632 struct glyph_row *
17633 row_containing_pos (struct window *w, ptrdiff_t charpos,
17634 struct glyph_row *start, struct glyph_row *end, int dy)
17636 struct glyph_row *row = start;
17637 struct glyph_row *best_row = NULL;
17638 ptrdiff_t mindif = BUF_ZV (XBUFFER (w->contents)) + 1;
17639 int last_y;
17641 /* If we happen to start on a header-line, skip that. */
17642 if (row->mode_line_p)
17643 ++row;
17645 if ((end && row >= end) || !row->enabled_p)
17646 return NULL;
17648 last_y = window_text_bottom_y (w) - dy;
17650 while (1)
17652 /* Give up if we have gone too far. */
17653 if (end && row >= end)
17654 return NULL;
17655 /* This formerly returned if they were equal.
17656 I think that both quantities are of a "last plus one" type;
17657 if so, when they are equal, the row is within the screen. -- rms. */
17658 if (MATRIX_ROW_BOTTOM_Y (row) > last_y)
17659 return NULL;
17661 /* If it is in this row, return this row. */
17662 if (! (MATRIX_ROW_END_CHARPOS (row) < charpos
17663 || (MATRIX_ROW_END_CHARPOS (row) == charpos
17664 /* The end position of a row equals the start
17665 position of the next row. If CHARPOS is there, we
17666 would rather consider it displayed in the next
17667 line, except when this line ends in ZV. */
17668 && !row_for_charpos_p (row, charpos)))
17669 && charpos >= MATRIX_ROW_START_CHARPOS (row))
17671 struct glyph *g;
17673 if (NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering))
17674 || (!best_row && !row->continued_p))
17675 return row;
17676 /* In bidi-reordered rows, there could be several rows whose
17677 edges surround CHARPOS, all of these rows belonging to
17678 the same continued line. We need to find the row which
17679 fits CHARPOS the best. */
17680 for (g = row->glyphs[TEXT_AREA];
17681 g < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
17682 g++)
17684 if (!STRINGP (g->object))
17686 if (g->charpos > 0 && eabs (g->charpos - charpos) < mindif)
17688 mindif = eabs (g->charpos - charpos);
17689 best_row = row;
17690 /* Exact match always wins. */
17691 if (mindif == 0)
17692 return best_row;
17697 else if (best_row && !row->continued_p)
17698 return best_row;
17699 ++row;
17704 /* Try to redisplay window W by reusing its existing display. W's
17705 current matrix must be up to date when this function is called,
17706 i.e. window_end_valid must be nonzero.
17708 Value is
17710 >= 1 if successful, i.e. display has been updated
17711 specifically:
17712 1 means the changes were in front of a newline that precedes
17713 the window start, and the whole current matrix was reused
17714 2 means the changes were after the last position displayed
17715 in the window, and the whole current matrix was reused
17716 3 means portions of the current matrix were reused, while
17717 some of the screen lines were redrawn
17718 -1 if redisplay with same window start is known not to succeed
17719 0 if otherwise unsuccessful
17721 The following steps are performed:
17723 1. Find the last row in the current matrix of W that is not
17724 affected by changes at the start of current_buffer. If no such row
17725 is found, give up.
17727 2. Find the first row in W's current matrix that is not affected by
17728 changes at the end of current_buffer. Maybe there is no such row.
17730 3. Display lines beginning with the row + 1 found in step 1 to the
17731 row found in step 2 or, if step 2 didn't find a row, to the end of
17732 the window.
17734 4. If cursor is not known to appear on the window, give up.
17736 5. If display stopped at the row found in step 2, scroll the
17737 display and current matrix as needed.
17739 6. Maybe display some lines at the end of W, if we must. This can
17740 happen under various circumstances, like a partially visible line
17741 becoming fully visible, or because newly displayed lines are displayed
17742 in smaller font sizes.
17744 7. Update W's window end information. */
17746 static int
17747 try_window_id (struct window *w)
17749 struct frame *f = XFRAME (w->frame);
17750 struct glyph_matrix *current_matrix = w->current_matrix;
17751 struct glyph_matrix *desired_matrix = w->desired_matrix;
17752 struct glyph_row *last_unchanged_at_beg_row;
17753 struct glyph_row *first_unchanged_at_end_row;
17754 struct glyph_row *row;
17755 struct glyph_row *bottom_row;
17756 int bottom_vpos;
17757 struct it it;
17758 ptrdiff_t delta = 0, delta_bytes = 0, stop_pos;
17759 int dvpos, dy;
17760 struct text_pos start_pos;
17761 struct run run;
17762 int first_unchanged_at_end_vpos = 0;
17763 struct glyph_row *last_text_row, *last_text_row_at_end;
17764 struct text_pos start;
17765 ptrdiff_t first_changed_charpos, last_changed_charpos;
17767 #ifdef GLYPH_DEBUG
17768 if (inhibit_try_window_id)
17769 return 0;
17770 #endif
17772 /* This is handy for debugging. */
17773 #if 0
17774 #define GIVE_UP(X) \
17775 do { \
17776 fprintf (stderr, "try_window_id give up %d\n", (X)); \
17777 return 0; \
17778 } while (0)
17779 #else
17780 #define GIVE_UP(X) return 0
17781 #endif
17783 SET_TEXT_POS_FROM_MARKER (start, w->start);
17785 /* Don't use this for mini-windows because these can show
17786 messages and mini-buffers, and we don't handle that here. */
17787 if (MINI_WINDOW_P (w))
17788 GIVE_UP (1);
17790 /* This flag is used to prevent redisplay optimizations. */
17791 if (windows_or_buffers_changed || f->cursor_type_changed)
17792 GIVE_UP (2);
17794 /* This function's optimizations cannot be used if overlays have
17795 changed in the buffer displayed by the window, so give up if they
17796 have. */
17797 if (w->last_overlay_modified != OVERLAY_MODIFF)
17798 GIVE_UP (21);
17800 /* Verify that narrowing has not changed.
17801 Also verify that we were not told to prevent redisplay optimizations.
17802 It would be nice to further
17803 reduce the number of cases where this prevents try_window_id. */
17804 if (current_buffer->clip_changed
17805 || current_buffer->prevent_redisplay_optimizations_p)
17806 GIVE_UP (3);
17808 /* Window must either use window-based redisplay or be full width. */
17809 if (!FRAME_WINDOW_P (f)
17810 && (!FRAME_LINE_INS_DEL_OK (f)
17811 || !WINDOW_FULL_WIDTH_P (w)))
17812 GIVE_UP (4);
17814 /* Give up if point is known NOT to appear in W. */
17815 if (PT < CHARPOS (start))
17816 GIVE_UP (5);
17818 /* Another way to prevent redisplay optimizations. */
17819 if (w->last_modified == 0)
17820 GIVE_UP (6);
17822 /* Verify that window is not hscrolled. */
17823 if (w->hscroll != 0)
17824 GIVE_UP (7);
17826 /* Verify that display wasn't paused. */
17827 if (!w->window_end_valid)
17828 GIVE_UP (8);
17830 /* Likewise if highlighting trailing whitespace. */
17831 if (!NILP (Vshow_trailing_whitespace))
17832 GIVE_UP (11);
17834 /* Can't use this if overlay arrow position and/or string have
17835 changed. */
17836 if (overlay_arrows_changed_p ())
17837 GIVE_UP (12);
17839 /* When word-wrap is on, adding a space to the first word of a
17840 wrapped line can change the wrap position, altering the line
17841 above it. It might be worthwhile to handle this more
17842 intelligently, but for now just redisplay from scratch. */
17843 if (!NILP (BVAR (XBUFFER (w->contents), word_wrap)))
17844 GIVE_UP (21);
17846 /* Under bidi reordering, adding or deleting a character in the
17847 beginning of a paragraph, before the first strong directional
17848 character, can change the base direction of the paragraph (unless
17849 the buffer specifies a fixed paragraph direction), which will
17850 require to redisplay the whole paragraph. It might be worthwhile
17851 to find the paragraph limits and widen the range of redisplayed
17852 lines to that, but for now just give up this optimization and
17853 redisplay from scratch. */
17854 if (!NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering))
17855 && NILP (BVAR (XBUFFER (w->contents), bidi_paragraph_direction)))
17856 GIVE_UP (22);
17858 /* Make sure beg_unchanged and end_unchanged are up to date. Do it
17859 only if buffer has really changed. The reason is that the gap is
17860 initially at Z for freshly visited files. The code below would
17861 set end_unchanged to 0 in that case. */
17862 if (MODIFF > SAVE_MODIFF
17863 /* This seems to happen sometimes after saving a buffer. */
17864 || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
17866 if (GPT - BEG < BEG_UNCHANGED)
17867 BEG_UNCHANGED = GPT - BEG;
17868 if (Z - GPT < END_UNCHANGED)
17869 END_UNCHANGED = Z - GPT;
17872 /* The position of the first and last character that has been changed. */
17873 first_changed_charpos = BEG + BEG_UNCHANGED;
17874 last_changed_charpos = Z - END_UNCHANGED;
17876 /* If window starts after a line end, and the last change is in
17877 front of that newline, then changes don't affect the display.
17878 This case happens with stealth-fontification. Note that although
17879 the display is unchanged, glyph positions in the matrix have to
17880 be adjusted, of course. */
17881 row = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
17882 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
17883 && ((last_changed_charpos < CHARPOS (start)
17884 && CHARPOS (start) == BEGV)
17885 || (last_changed_charpos < CHARPOS (start) - 1
17886 && FETCH_BYTE (BYTEPOS (start) - 1) == '\n')))
17888 ptrdiff_t Z_old, Z_delta, Z_BYTE_old, Z_delta_bytes;
17889 struct glyph_row *r0;
17891 /* Compute how many chars/bytes have been added to or removed
17892 from the buffer. */
17893 Z_old = MATRIX_ROW_END_CHARPOS (row) + w->window_end_pos;
17894 Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
17895 Z_delta = Z - Z_old;
17896 Z_delta_bytes = Z_BYTE - Z_BYTE_old;
17898 /* Give up if PT is not in the window. Note that it already has
17899 been checked at the start of try_window_id that PT is not in
17900 front of the window start. */
17901 if (PT >= MATRIX_ROW_END_CHARPOS (row) + Z_delta)
17902 GIVE_UP (13);
17904 /* If window start is unchanged, we can reuse the whole matrix
17905 as is, after adjusting glyph positions. No need to compute
17906 the window end again, since its offset from Z hasn't changed. */
17907 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
17908 if (CHARPOS (start) == MATRIX_ROW_START_CHARPOS (r0) + Z_delta
17909 && BYTEPOS (start) == MATRIX_ROW_START_BYTEPOS (r0) + Z_delta_bytes
17910 /* PT must not be in a partially visible line. */
17911 && !(PT >= MATRIX_ROW_START_CHARPOS (row) + Z_delta
17912 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
17914 /* Adjust positions in the glyph matrix. */
17915 if (Z_delta || Z_delta_bytes)
17917 struct glyph_row *r1
17918 = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
17919 increment_matrix_positions (w->current_matrix,
17920 MATRIX_ROW_VPOS (r0, current_matrix),
17921 MATRIX_ROW_VPOS (r1, current_matrix),
17922 Z_delta, Z_delta_bytes);
17925 /* Set the cursor. */
17926 row = row_containing_pos (w, PT, r0, NULL, 0);
17927 if (row)
17928 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
17929 return 1;
17933 /* Handle the case that changes are all below what is displayed in
17934 the window, and that PT is in the window. This shortcut cannot
17935 be taken if ZV is visible in the window, and text has been added
17936 there that is visible in the window. */
17937 if (first_changed_charpos >= MATRIX_ROW_END_CHARPOS (row)
17938 /* ZV is not visible in the window, or there are no
17939 changes at ZV, actually. */
17940 && (current_matrix->zv > MATRIX_ROW_END_CHARPOS (row)
17941 || first_changed_charpos == last_changed_charpos))
17943 struct glyph_row *r0;
17945 /* Give up if PT is not in the window. Note that it already has
17946 been checked at the start of try_window_id that PT is not in
17947 front of the window start. */
17948 if (PT >= MATRIX_ROW_END_CHARPOS (row))
17949 GIVE_UP (14);
17951 /* If window start is unchanged, we can reuse the whole matrix
17952 as is, without changing glyph positions since no text has
17953 been added/removed in front of the window end. */
17954 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
17955 if (TEXT_POS_EQUAL_P (start, r0->minpos)
17956 /* PT must not be in a partially visible line. */
17957 && !(PT >= MATRIX_ROW_START_CHARPOS (row)
17958 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
17960 /* We have to compute the window end anew since text
17961 could have been added/removed after it. */
17962 w->window_end_pos = Z - MATRIX_ROW_END_CHARPOS (row);
17963 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
17965 /* Set the cursor. */
17966 row = row_containing_pos (w, PT, r0, NULL, 0);
17967 if (row)
17968 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
17969 return 2;
17973 /* Give up if window start is in the changed area.
17975 The condition used to read
17977 (BEG_UNCHANGED + END_UNCHANGED != Z - BEG && ...)
17979 but why that was tested escapes me at the moment. */
17980 if (CHARPOS (start) >= first_changed_charpos
17981 && CHARPOS (start) <= last_changed_charpos)
17982 GIVE_UP (15);
17984 /* Check that window start agrees with the start of the first glyph
17985 row in its current matrix. Check this after we know the window
17986 start is not in changed text, otherwise positions would not be
17987 comparable. */
17988 row = MATRIX_FIRST_TEXT_ROW (current_matrix);
17989 if (!TEXT_POS_EQUAL_P (start, row->minpos))
17990 GIVE_UP (16);
17992 /* Give up if the window ends in strings. Overlay strings
17993 at the end are difficult to handle, so don't try. */
17994 row = MATRIX_ROW (current_matrix, w->window_end_vpos);
17995 if (MATRIX_ROW_START_CHARPOS (row) == MATRIX_ROW_END_CHARPOS (row))
17996 GIVE_UP (20);
17998 /* Compute the position at which we have to start displaying new
17999 lines. Some of the lines at the top of the window might be
18000 reusable because they are not displaying changed text. Find the
18001 last row in W's current matrix not affected by changes at the
18002 start of current_buffer. Value is null if changes start in the
18003 first line of window. */
18004 last_unchanged_at_beg_row = find_last_unchanged_at_beg_row (w);
18005 if (last_unchanged_at_beg_row)
18007 /* Avoid starting to display in the middle of a character, a TAB
18008 for instance. This is easier than to set up the iterator
18009 exactly, and it's not a frequent case, so the additional
18010 effort wouldn't really pay off. */
18011 while ((MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row)
18012 || last_unchanged_at_beg_row->ends_in_newline_from_string_p)
18013 && last_unchanged_at_beg_row > w->current_matrix->rows)
18014 --last_unchanged_at_beg_row;
18016 if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row))
18017 GIVE_UP (17);
18019 if (init_to_row_end (&it, w, last_unchanged_at_beg_row) == 0)
18020 GIVE_UP (18);
18021 start_pos = it.current.pos;
18023 /* Start displaying new lines in the desired matrix at the same
18024 vpos we would use in the current matrix, i.e. below
18025 last_unchanged_at_beg_row. */
18026 it.vpos = 1 + MATRIX_ROW_VPOS (last_unchanged_at_beg_row,
18027 current_matrix);
18028 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
18029 it.current_y = MATRIX_ROW_BOTTOM_Y (last_unchanged_at_beg_row);
18031 eassert (it.hpos == 0 && it.current_x == 0);
18033 else
18035 /* There are no reusable lines at the start of the window.
18036 Start displaying in the first text line. */
18037 start_display (&it, w, start);
18038 it.vpos = it.first_vpos;
18039 start_pos = it.current.pos;
18042 /* Find the first row that is not affected by changes at the end of
18043 the buffer. Value will be null if there is no unchanged row, in
18044 which case we must redisplay to the end of the window. delta
18045 will be set to the value by which buffer positions beginning with
18046 first_unchanged_at_end_row have to be adjusted due to text
18047 changes. */
18048 first_unchanged_at_end_row
18049 = find_first_unchanged_at_end_row (w, &delta, &delta_bytes);
18050 IF_DEBUG (debug_delta = delta);
18051 IF_DEBUG (debug_delta_bytes = delta_bytes);
18053 /* Set stop_pos to the buffer position up to which we will have to
18054 display new lines. If first_unchanged_at_end_row != NULL, this
18055 is the buffer position of the start of the line displayed in that
18056 row. For first_unchanged_at_end_row == NULL, use 0 to indicate
18057 that we don't stop at a buffer position. */
18058 stop_pos = 0;
18059 if (first_unchanged_at_end_row)
18061 eassert (last_unchanged_at_beg_row == NULL
18062 || first_unchanged_at_end_row >= last_unchanged_at_beg_row);
18064 /* If this is a continuation line, move forward to the next one
18065 that isn't. Changes in lines above affect this line.
18066 Caution: this may move first_unchanged_at_end_row to a row
18067 not displaying text. */
18068 while (MATRIX_ROW_CONTINUATION_LINE_P (first_unchanged_at_end_row)
18069 && MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
18070 && (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
18071 < it.last_visible_y))
18072 ++first_unchanged_at_end_row;
18074 if (!MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
18075 || (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
18076 >= it.last_visible_y))
18077 first_unchanged_at_end_row = NULL;
18078 else
18080 stop_pos = (MATRIX_ROW_START_CHARPOS (first_unchanged_at_end_row)
18081 + delta);
18082 first_unchanged_at_end_vpos
18083 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, current_matrix);
18084 eassert (stop_pos >= Z - END_UNCHANGED);
18087 else if (last_unchanged_at_beg_row == NULL)
18088 GIVE_UP (19);
18091 #ifdef GLYPH_DEBUG
18093 /* Either there is no unchanged row at the end, or the one we have
18094 now displays text. This is a necessary condition for the window
18095 end pos calculation at the end of this function. */
18096 eassert (first_unchanged_at_end_row == NULL
18097 || MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
18099 debug_last_unchanged_at_beg_vpos
18100 = (last_unchanged_at_beg_row
18101 ? MATRIX_ROW_VPOS (last_unchanged_at_beg_row, current_matrix)
18102 : -1);
18103 debug_first_unchanged_at_end_vpos = first_unchanged_at_end_vpos;
18105 #endif /* GLYPH_DEBUG */
18108 /* Display new lines. Set last_text_row to the last new line
18109 displayed which has text on it, i.e. might end up as being the
18110 line where the window_end_vpos is. */
18111 w->cursor.vpos = -1;
18112 last_text_row = NULL;
18113 overlay_arrow_seen = 0;
18114 if (it.current_y < it.last_visible_y
18115 && !f->fonts_changed
18116 && (first_unchanged_at_end_row == NULL
18117 || IT_CHARPOS (it) < stop_pos))
18118 it.glyph_row->reversed_p = false;
18119 while (it.current_y < it.last_visible_y
18120 && !f->fonts_changed
18121 && (first_unchanged_at_end_row == NULL
18122 || IT_CHARPOS (it) < stop_pos))
18124 if (display_line (&it))
18125 last_text_row = it.glyph_row - 1;
18128 if (f->fonts_changed)
18129 return -1;
18132 /* Compute differences in buffer positions, y-positions etc. for
18133 lines reused at the bottom of the window. Compute what we can
18134 scroll. */
18135 if (first_unchanged_at_end_row
18136 /* No lines reused because we displayed everything up to the
18137 bottom of the window. */
18138 && it.current_y < it.last_visible_y)
18140 dvpos = (it.vpos
18141 - MATRIX_ROW_VPOS (first_unchanged_at_end_row,
18142 current_matrix));
18143 dy = it.current_y - first_unchanged_at_end_row->y;
18144 run.current_y = first_unchanged_at_end_row->y;
18145 run.desired_y = run.current_y + dy;
18146 run.height = it.last_visible_y - max (run.current_y, run.desired_y);
18148 else
18150 delta = delta_bytes = dvpos = dy
18151 = run.current_y = run.desired_y = run.height = 0;
18152 first_unchanged_at_end_row = NULL;
18154 IF_DEBUG ((debug_dvpos = dvpos, debug_dy = dy));
18157 /* Find the cursor if not already found. We have to decide whether
18158 PT will appear on this window (it sometimes doesn't, but this is
18159 not a very frequent case.) This decision has to be made before
18160 the current matrix is altered. A value of cursor.vpos < 0 means
18161 that PT is either in one of the lines beginning at
18162 first_unchanged_at_end_row or below the window. Don't care for
18163 lines that might be displayed later at the window end; as
18164 mentioned, this is not a frequent case. */
18165 if (w->cursor.vpos < 0)
18167 /* Cursor in unchanged rows at the top? */
18168 if (PT < CHARPOS (start_pos)
18169 && last_unchanged_at_beg_row)
18171 row = row_containing_pos (w, PT,
18172 MATRIX_FIRST_TEXT_ROW (w->current_matrix),
18173 last_unchanged_at_beg_row + 1, 0);
18174 if (row)
18175 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
18178 /* Start from first_unchanged_at_end_row looking for PT. */
18179 else if (first_unchanged_at_end_row)
18181 row = row_containing_pos (w, PT - delta,
18182 first_unchanged_at_end_row, NULL, 0);
18183 if (row)
18184 set_cursor_from_row (w, row, w->current_matrix, delta,
18185 delta_bytes, dy, dvpos);
18188 /* Give up if cursor was not found. */
18189 if (w->cursor.vpos < 0)
18191 clear_glyph_matrix (w->desired_matrix);
18192 return -1;
18196 /* Don't let the cursor end in the scroll margins. */
18198 int this_scroll_margin, cursor_height;
18199 int frame_line_height = default_line_pixel_height (w);
18200 int window_total_lines
18201 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (it.f) / frame_line_height;
18203 this_scroll_margin =
18204 max (0, min (scroll_margin, window_total_lines / 4));
18205 this_scroll_margin *= frame_line_height;
18206 cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
18208 if ((w->cursor.y < this_scroll_margin
18209 && CHARPOS (start) > BEGV)
18210 /* Old redisplay didn't take scroll margin into account at the bottom,
18211 but then global-hl-line-mode doesn't scroll. KFS 2004-06-14 */
18212 || (w->cursor.y + (make_cursor_line_fully_visible_p
18213 ? cursor_height + this_scroll_margin
18214 : 1)) > it.last_visible_y)
18216 w->cursor.vpos = -1;
18217 clear_glyph_matrix (w->desired_matrix);
18218 return -1;
18222 /* Scroll the display. Do it before changing the current matrix so
18223 that xterm.c doesn't get confused about where the cursor glyph is
18224 found. */
18225 if (dy && run.height)
18227 update_begin (f);
18229 if (FRAME_WINDOW_P (f))
18231 FRAME_RIF (f)->update_window_begin_hook (w);
18232 FRAME_RIF (f)->clear_window_mouse_face (w);
18233 FRAME_RIF (f)->scroll_run_hook (w, &run);
18234 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
18236 else
18238 /* Terminal frame. In this case, dvpos gives the number of
18239 lines to scroll by; dvpos < 0 means scroll up. */
18240 int from_vpos
18241 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, w->current_matrix);
18242 int from = WINDOW_TOP_EDGE_LINE (w) + from_vpos;
18243 int end = (WINDOW_TOP_EDGE_LINE (w)
18244 + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0)
18245 + window_internal_height (w));
18247 #if defined (HAVE_GPM) || defined (MSDOS)
18248 x_clear_window_mouse_face (w);
18249 #endif
18250 /* Perform the operation on the screen. */
18251 if (dvpos > 0)
18253 /* Scroll last_unchanged_at_beg_row to the end of the
18254 window down dvpos lines. */
18255 set_terminal_window (f, end);
18257 /* On dumb terminals delete dvpos lines at the end
18258 before inserting dvpos empty lines. */
18259 if (!FRAME_SCROLL_REGION_OK (f))
18260 ins_del_lines (f, end - dvpos, -dvpos);
18262 /* Insert dvpos empty lines in front of
18263 last_unchanged_at_beg_row. */
18264 ins_del_lines (f, from, dvpos);
18266 else if (dvpos < 0)
18268 /* Scroll up last_unchanged_at_beg_vpos to the end of
18269 the window to last_unchanged_at_beg_vpos - |dvpos|. */
18270 set_terminal_window (f, end);
18272 /* Delete dvpos lines in front of
18273 last_unchanged_at_beg_vpos. ins_del_lines will set
18274 the cursor to the given vpos and emit |dvpos| delete
18275 line sequences. */
18276 ins_del_lines (f, from + dvpos, dvpos);
18278 /* On a dumb terminal insert dvpos empty lines at the
18279 end. */
18280 if (!FRAME_SCROLL_REGION_OK (f))
18281 ins_del_lines (f, end + dvpos, -dvpos);
18284 set_terminal_window (f, 0);
18287 update_end (f);
18290 /* Shift reused rows of the current matrix to the right position.
18291 BOTTOM_ROW is the last + 1 row in the current matrix reserved for
18292 text. */
18293 bottom_row = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
18294 bottom_vpos = MATRIX_ROW_VPOS (bottom_row, current_matrix);
18295 if (dvpos < 0)
18297 rotate_matrix (current_matrix, first_unchanged_at_end_vpos + dvpos,
18298 bottom_vpos, dvpos);
18299 clear_glyph_matrix_rows (current_matrix, bottom_vpos + dvpos,
18300 bottom_vpos);
18302 else if (dvpos > 0)
18304 rotate_matrix (current_matrix, first_unchanged_at_end_vpos,
18305 bottom_vpos, dvpos);
18306 clear_glyph_matrix_rows (current_matrix, first_unchanged_at_end_vpos,
18307 first_unchanged_at_end_vpos + dvpos);
18310 /* For frame-based redisplay, make sure that current frame and window
18311 matrix are in sync with respect to glyph memory. */
18312 if (!FRAME_WINDOW_P (f))
18313 sync_frame_with_window_matrix_rows (w);
18315 /* Adjust buffer positions in reused rows. */
18316 if (delta || delta_bytes)
18317 increment_matrix_positions (current_matrix,
18318 first_unchanged_at_end_vpos + dvpos,
18319 bottom_vpos, delta, delta_bytes);
18321 /* Adjust Y positions. */
18322 if (dy)
18323 shift_glyph_matrix (w, current_matrix,
18324 first_unchanged_at_end_vpos + dvpos,
18325 bottom_vpos, dy);
18327 if (first_unchanged_at_end_row)
18329 first_unchanged_at_end_row += dvpos;
18330 if (first_unchanged_at_end_row->y >= it.last_visible_y
18331 || !MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row))
18332 first_unchanged_at_end_row = NULL;
18335 /* If scrolling up, there may be some lines to display at the end of
18336 the window. */
18337 last_text_row_at_end = NULL;
18338 if (dy < 0)
18340 /* Scrolling up can leave for example a partially visible line
18341 at the end of the window to be redisplayed. */
18342 /* Set last_row to the glyph row in the current matrix where the
18343 window end line is found. It has been moved up or down in
18344 the matrix by dvpos. */
18345 int last_vpos = w->window_end_vpos + dvpos;
18346 struct glyph_row *last_row = MATRIX_ROW (current_matrix, last_vpos);
18348 /* If last_row is the window end line, it should display text. */
18349 eassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_row));
18351 /* If window end line was partially visible before, begin
18352 displaying at that line. Otherwise begin displaying with the
18353 line following it. */
18354 if (MATRIX_ROW_BOTTOM_Y (last_row) - dy >= it.last_visible_y)
18356 init_to_row_start (&it, w, last_row);
18357 it.vpos = last_vpos;
18358 it.current_y = last_row->y;
18360 else
18362 init_to_row_end (&it, w, last_row);
18363 it.vpos = 1 + last_vpos;
18364 it.current_y = MATRIX_ROW_BOTTOM_Y (last_row);
18365 ++last_row;
18368 /* We may start in a continuation line. If so, we have to
18369 get the right continuation_lines_width and current_x. */
18370 it.continuation_lines_width = last_row->continuation_lines_width;
18371 it.hpos = it.current_x = 0;
18373 /* Display the rest of the lines at the window end. */
18374 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
18375 while (it.current_y < it.last_visible_y && !f->fonts_changed)
18377 /* Is it always sure that the display agrees with lines in
18378 the current matrix? I don't think so, so we mark rows
18379 displayed invalid in the current matrix by setting their
18380 enabled_p flag to zero. */
18381 SET_MATRIX_ROW_ENABLED_P (w->current_matrix, it.vpos, false);
18382 if (display_line (&it))
18383 last_text_row_at_end = it.glyph_row - 1;
18387 /* Update window_end_pos and window_end_vpos. */
18388 if (first_unchanged_at_end_row && !last_text_row_at_end)
18390 /* Window end line if one of the preserved rows from the current
18391 matrix. Set row to the last row displaying text in current
18392 matrix starting at first_unchanged_at_end_row, after
18393 scrolling. */
18394 eassert (MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
18395 row = find_last_row_displaying_text (w->current_matrix, &it,
18396 first_unchanged_at_end_row);
18397 eassert (row && MATRIX_ROW_DISPLAYS_TEXT_P (row));
18398 adjust_window_ends (w, row, 1);
18399 eassert (w->window_end_bytepos >= 0);
18400 IF_DEBUG (debug_method_add (w, "A"));
18402 else if (last_text_row_at_end)
18404 adjust_window_ends (w, last_text_row_at_end, 0);
18405 eassert (w->window_end_bytepos >= 0);
18406 IF_DEBUG (debug_method_add (w, "B"));
18408 else if (last_text_row)
18410 /* We have displayed either to the end of the window or at the
18411 end of the window, i.e. the last row with text is to be found
18412 in the desired matrix. */
18413 adjust_window_ends (w, last_text_row, 0);
18414 eassert (w->window_end_bytepos >= 0);
18416 else if (first_unchanged_at_end_row == NULL
18417 && last_text_row == NULL
18418 && last_text_row_at_end == NULL)
18420 /* Displayed to end of window, but no line containing text was
18421 displayed. Lines were deleted at the end of the window. */
18422 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
18423 int vpos = w->window_end_vpos;
18424 struct glyph_row *current_row = current_matrix->rows + vpos;
18425 struct glyph_row *desired_row = desired_matrix->rows + vpos;
18427 for (row = NULL;
18428 row == NULL && vpos >= first_vpos;
18429 --vpos, --current_row, --desired_row)
18431 if (desired_row->enabled_p)
18433 if (MATRIX_ROW_DISPLAYS_TEXT_P (desired_row))
18434 row = desired_row;
18436 else if (MATRIX_ROW_DISPLAYS_TEXT_P (current_row))
18437 row = current_row;
18440 eassert (row != NULL);
18441 w->window_end_vpos = vpos + 1;
18442 w->window_end_pos = Z - MATRIX_ROW_END_CHARPOS (row);
18443 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
18444 eassert (w->window_end_bytepos >= 0);
18445 IF_DEBUG (debug_method_add (w, "C"));
18447 else
18448 emacs_abort ();
18450 IF_DEBUG ((debug_end_pos = w->window_end_pos,
18451 debug_end_vpos = w->window_end_vpos));
18453 /* Record that display has not been completed. */
18454 w->window_end_valid = 0;
18455 w->desired_matrix->no_scrolling_p = 1;
18456 return 3;
18458 #undef GIVE_UP
18463 /***********************************************************************
18464 More debugging support
18465 ***********************************************************************/
18467 #ifdef GLYPH_DEBUG
18469 void dump_glyph_row (struct glyph_row *, int, int) EXTERNALLY_VISIBLE;
18470 void dump_glyph_matrix (struct glyph_matrix *, int) EXTERNALLY_VISIBLE;
18471 void dump_glyph (struct glyph_row *, struct glyph *, int) EXTERNALLY_VISIBLE;
18474 /* Dump the contents of glyph matrix MATRIX on stderr.
18476 GLYPHS 0 means don't show glyph contents.
18477 GLYPHS 1 means show glyphs in short form
18478 GLYPHS > 1 means show glyphs in long form. */
18480 void
18481 dump_glyph_matrix (struct glyph_matrix *matrix, int glyphs)
18483 int i;
18484 for (i = 0; i < matrix->nrows; ++i)
18485 dump_glyph_row (MATRIX_ROW (matrix, i), i, glyphs);
18489 /* Dump contents of glyph GLYPH to stderr. ROW and AREA are
18490 the glyph row and area where the glyph comes from. */
18492 void
18493 dump_glyph (struct glyph_row *row, struct glyph *glyph, int area)
18495 if (glyph->type == CHAR_GLYPH
18496 || glyph->type == GLYPHLESS_GLYPH)
18498 fprintf (stderr,
18499 " %5"pD"d %c %9"pI"d %c %3d 0x%06x %c %4d %1.1d%1.1d\n",
18500 glyph - row->glyphs[TEXT_AREA],
18501 (glyph->type == CHAR_GLYPH
18502 ? 'C'
18503 : 'G'),
18504 glyph->charpos,
18505 (BUFFERP (glyph->object)
18506 ? 'B'
18507 : (STRINGP (glyph->object)
18508 ? 'S'
18509 : (NILP (glyph->object)
18510 ? '0'
18511 : '-'))),
18512 glyph->pixel_width,
18513 glyph->u.ch,
18514 (glyph->u.ch < 0x80 && glyph->u.ch >= ' '
18515 ? glyph->u.ch
18516 : '.'),
18517 glyph->face_id,
18518 glyph->left_box_line_p,
18519 glyph->right_box_line_p);
18521 else if (glyph->type == STRETCH_GLYPH)
18523 fprintf (stderr,
18524 " %5"pD"d %c %9"pI"d %c %3d 0x%06x %c %4d %1.1d%1.1d\n",
18525 glyph - row->glyphs[TEXT_AREA],
18526 'S',
18527 glyph->charpos,
18528 (BUFFERP (glyph->object)
18529 ? 'B'
18530 : (STRINGP (glyph->object)
18531 ? 'S'
18532 : (NILP (glyph->object)
18533 ? '0'
18534 : '-'))),
18535 glyph->pixel_width,
18537 ' ',
18538 glyph->face_id,
18539 glyph->left_box_line_p,
18540 glyph->right_box_line_p);
18542 else if (glyph->type == IMAGE_GLYPH)
18544 fprintf (stderr,
18545 " %5"pD"d %c %9"pI"d %c %3d 0x%06x %c %4d %1.1d%1.1d\n",
18546 glyph - row->glyphs[TEXT_AREA],
18547 'I',
18548 glyph->charpos,
18549 (BUFFERP (glyph->object)
18550 ? 'B'
18551 : (STRINGP (glyph->object)
18552 ? 'S'
18553 : (NILP (glyph->object)
18554 ? '0'
18555 : '-'))),
18556 glyph->pixel_width,
18557 glyph->u.img_id,
18558 '.',
18559 glyph->face_id,
18560 glyph->left_box_line_p,
18561 glyph->right_box_line_p);
18563 else if (glyph->type == COMPOSITE_GLYPH)
18565 fprintf (stderr,
18566 " %5"pD"d %c %9"pI"d %c %3d 0x%06x",
18567 glyph - row->glyphs[TEXT_AREA],
18568 '+',
18569 glyph->charpos,
18570 (BUFFERP (glyph->object)
18571 ? 'B'
18572 : (STRINGP (glyph->object)
18573 ? 'S'
18574 : (NILP (glyph->object)
18575 ? '0'
18576 : '-'))),
18577 glyph->pixel_width,
18578 glyph->u.cmp.id);
18579 if (glyph->u.cmp.automatic)
18580 fprintf (stderr,
18581 "[%d-%d]",
18582 glyph->slice.cmp.from, glyph->slice.cmp.to);
18583 fprintf (stderr, " . %4d %1.1d%1.1d\n",
18584 glyph->face_id,
18585 glyph->left_box_line_p,
18586 glyph->right_box_line_p);
18591 /* Dump the contents of glyph row at VPOS in MATRIX to stderr.
18592 GLYPHS 0 means don't show glyph contents.
18593 GLYPHS 1 means show glyphs in short form
18594 GLYPHS > 1 means show glyphs in long form. */
18596 void
18597 dump_glyph_row (struct glyph_row *row, int vpos, int glyphs)
18599 if (glyphs != 1)
18601 fprintf (stderr, "Row Start End Used oE><\\CTZFesm X Y W H V A P\n");
18602 fprintf (stderr, "==============================================================================\n");
18604 fprintf (stderr, "%3d %9"pI"d %9"pI"d %4d %1.1d%1.1d%1.1d%1.1d\
18605 %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d %4d %4d %4d %4d %4d %4d %4d\n",
18606 vpos,
18607 MATRIX_ROW_START_CHARPOS (row),
18608 MATRIX_ROW_END_CHARPOS (row),
18609 row->used[TEXT_AREA],
18610 row->contains_overlapping_glyphs_p,
18611 row->enabled_p,
18612 row->truncated_on_left_p,
18613 row->truncated_on_right_p,
18614 row->continued_p,
18615 MATRIX_ROW_CONTINUATION_LINE_P (row),
18616 MATRIX_ROW_DISPLAYS_TEXT_P (row),
18617 row->ends_at_zv_p,
18618 row->fill_line_p,
18619 row->ends_in_middle_of_char_p,
18620 row->starts_in_middle_of_char_p,
18621 row->mouse_face_p,
18622 row->x,
18623 row->y,
18624 row->pixel_width,
18625 row->height,
18626 row->visible_height,
18627 row->ascent,
18628 row->phys_ascent);
18629 /* The next 3 lines should align to "Start" in the header. */
18630 fprintf (stderr, " %9"pD"d %9"pD"d\t%5d\n", row->start.overlay_string_index,
18631 row->end.overlay_string_index,
18632 row->continuation_lines_width);
18633 fprintf (stderr, " %9"pI"d %9"pI"d\n",
18634 CHARPOS (row->start.string_pos),
18635 CHARPOS (row->end.string_pos));
18636 fprintf (stderr, " %9d %9d\n", row->start.dpvec_index,
18637 row->end.dpvec_index);
18640 if (glyphs > 1)
18642 int area;
18644 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
18646 struct glyph *glyph = row->glyphs[area];
18647 struct glyph *glyph_end = glyph + row->used[area];
18649 /* Glyph for a line end in text. */
18650 if (area == TEXT_AREA && glyph == glyph_end && glyph->charpos > 0)
18651 ++glyph_end;
18653 if (glyph < glyph_end)
18654 fprintf (stderr, " Glyph# Type Pos O W Code C Face LR\n");
18656 for (; glyph < glyph_end; ++glyph)
18657 dump_glyph (row, glyph, area);
18660 else if (glyphs == 1)
18662 int area;
18663 char s[SHRT_MAX + 4];
18665 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
18667 int i;
18669 for (i = 0; i < row->used[area]; ++i)
18671 struct glyph *glyph = row->glyphs[area] + i;
18672 if (i == row->used[area] - 1
18673 && area == TEXT_AREA
18674 && NILP (glyph->object)
18675 && glyph->type == CHAR_GLYPH
18676 && glyph->u.ch == ' ')
18678 strcpy (&s[i], "[\\n]");
18679 i += 4;
18681 else if (glyph->type == CHAR_GLYPH
18682 && glyph->u.ch < 0x80
18683 && glyph->u.ch >= ' ')
18684 s[i] = glyph->u.ch;
18685 else
18686 s[i] = '.';
18689 s[i] = '\0';
18690 fprintf (stderr, "%3d: (%d) '%s'\n", vpos, row->enabled_p, s);
18696 DEFUN ("dump-glyph-matrix", Fdump_glyph_matrix,
18697 Sdump_glyph_matrix, 0, 1, "p",
18698 doc: /* Dump the current matrix of the selected window to stderr.
18699 Shows contents of glyph row structures. With non-nil
18700 parameter GLYPHS, dump glyphs as well. If GLYPHS is 1 show
18701 glyphs in short form, otherwise show glyphs in long form.
18703 Interactively, no argument means show glyphs in short form;
18704 with numeric argument, its value is passed as the GLYPHS flag. */)
18705 (Lisp_Object glyphs)
18707 struct window *w = XWINDOW (selected_window);
18708 struct buffer *buffer = XBUFFER (w->contents);
18710 fprintf (stderr, "PT = %"pI"d, BEGV = %"pI"d. ZV = %"pI"d\n",
18711 BUF_PT (buffer), BUF_BEGV (buffer), BUF_ZV (buffer));
18712 fprintf (stderr, "Cursor x = %d, y = %d, hpos = %d, vpos = %d\n",
18713 w->cursor.x, w->cursor.y, w->cursor.hpos, w->cursor.vpos);
18714 fprintf (stderr, "=============================================\n");
18715 dump_glyph_matrix (w->current_matrix,
18716 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 0);
18717 return Qnil;
18721 DEFUN ("dump-frame-glyph-matrix", Fdump_frame_glyph_matrix,
18722 Sdump_frame_glyph_matrix, 0, 0, "", doc: /* Dump the current glyph matrix of the selected frame to stderr.
18723 Only text-mode frames have frame glyph matrices. */)
18724 (void)
18726 struct frame *f = XFRAME (selected_frame);
18728 if (f->current_matrix)
18729 dump_glyph_matrix (f->current_matrix, 1);
18730 else
18731 fprintf (stderr, "*** This frame doesn't have a frame glyph matrix ***\n");
18732 return Qnil;
18736 DEFUN ("dump-glyph-row", Fdump_glyph_row, Sdump_glyph_row, 1, 2, "",
18737 doc: /* Dump glyph row ROW to stderr.
18738 GLYPH 0 means don't dump glyphs.
18739 GLYPH 1 means dump glyphs in short form.
18740 GLYPH > 1 or omitted means dump glyphs in long form. */)
18741 (Lisp_Object row, Lisp_Object glyphs)
18743 struct glyph_matrix *matrix;
18744 EMACS_INT vpos;
18746 CHECK_NUMBER (row);
18747 matrix = XWINDOW (selected_window)->current_matrix;
18748 vpos = XINT (row);
18749 if (vpos >= 0 && vpos < matrix->nrows)
18750 dump_glyph_row (MATRIX_ROW (matrix, vpos),
18751 vpos,
18752 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 2);
18753 return Qnil;
18757 DEFUN ("dump-tool-bar-row", Fdump_tool_bar_row, Sdump_tool_bar_row, 1, 2, "",
18758 doc: /* Dump glyph row ROW of the tool-bar of the current frame to stderr.
18759 GLYPH 0 means don't dump glyphs.
18760 GLYPH 1 means dump glyphs in short form.
18761 GLYPH > 1 or omitted means dump glyphs in long form.
18763 If there's no tool-bar, or if the tool-bar is not drawn by Emacs,
18764 do nothing. */)
18765 (Lisp_Object row, Lisp_Object glyphs)
18767 #if defined (HAVE_WINDOW_SYSTEM) && ! defined (USE_GTK) && ! defined (HAVE_NS)
18768 struct frame *sf = SELECTED_FRAME ();
18769 struct glyph_matrix *m = XWINDOW (sf->tool_bar_window)->current_matrix;
18770 EMACS_INT vpos;
18772 CHECK_NUMBER (row);
18773 vpos = XINT (row);
18774 if (vpos >= 0 && vpos < m->nrows)
18775 dump_glyph_row (MATRIX_ROW (m, vpos), vpos,
18776 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 2);
18777 #endif
18778 return Qnil;
18782 DEFUN ("trace-redisplay", Ftrace_redisplay, Strace_redisplay, 0, 1, "P",
18783 doc: /* Toggle tracing of redisplay.
18784 With ARG, turn tracing on if and only if ARG is positive. */)
18785 (Lisp_Object arg)
18787 if (NILP (arg))
18788 trace_redisplay_p = !trace_redisplay_p;
18789 else
18791 arg = Fprefix_numeric_value (arg);
18792 trace_redisplay_p = XINT (arg) > 0;
18795 return Qnil;
18799 DEFUN ("trace-to-stderr", Ftrace_to_stderr, Strace_to_stderr, 1, MANY, "",
18800 doc: /* Like `format', but print result to stderr.
18801 usage: (trace-to-stderr STRING &rest OBJECTS) */)
18802 (ptrdiff_t nargs, Lisp_Object *args)
18804 Lisp_Object s = Fformat (nargs, args);
18805 fprintf (stderr, "%s", SDATA (s));
18806 return Qnil;
18809 #endif /* GLYPH_DEBUG */
18813 /***********************************************************************
18814 Building Desired Matrix Rows
18815 ***********************************************************************/
18817 /* Return a temporary glyph row holding the glyphs of an overlay arrow.
18818 Used for non-window-redisplay windows, and for windows w/o left fringe. */
18820 static struct glyph_row *
18821 get_overlay_arrow_glyph_row (struct window *w, Lisp_Object overlay_arrow_string)
18823 struct frame *f = XFRAME (WINDOW_FRAME (w));
18824 struct buffer *buffer = XBUFFER (w->contents);
18825 struct buffer *old = current_buffer;
18826 const unsigned char *arrow_string = SDATA (overlay_arrow_string);
18827 ptrdiff_t arrow_len = SCHARS (overlay_arrow_string);
18828 const unsigned char *arrow_end = arrow_string + arrow_len;
18829 const unsigned char *p;
18830 struct it it;
18831 bool multibyte_p;
18832 int n_glyphs_before;
18834 set_buffer_temp (buffer);
18835 init_iterator (&it, w, -1, -1, &scratch_glyph_row, DEFAULT_FACE_ID);
18836 scratch_glyph_row.reversed_p = false;
18837 it.glyph_row->used[TEXT_AREA] = 0;
18838 SET_TEXT_POS (it.position, 0, 0);
18840 multibyte_p = !NILP (BVAR (buffer, enable_multibyte_characters));
18841 p = arrow_string;
18842 while (p < arrow_end)
18844 Lisp_Object face, ilisp;
18846 /* Get the next character. */
18847 if (multibyte_p)
18848 it.c = it.char_to_display = string_char_and_length (p, &it.len);
18849 else
18851 it.c = it.char_to_display = *p, it.len = 1;
18852 if (! ASCII_CHAR_P (it.c))
18853 it.char_to_display = BYTE8_TO_CHAR (it.c);
18855 p += it.len;
18857 /* Get its face. */
18858 ilisp = make_number (p - arrow_string);
18859 face = Fget_text_property (ilisp, Qface, overlay_arrow_string);
18860 it.face_id = compute_char_face (f, it.char_to_display, face);
18862 /* Compute its width, get its glyphs. */
18863 n_glyphs_before = it.glyph_row->used[TEXT_AREA];
18864 SET_TEXT_POS (it.position, -1, -1);
18865 PRODUCE_GLYPHS (&it);
18867 /* If this character doesn't fit any more in the line, we have
18868 to remove some glyphs. */
18869 if (it.current_x > it.last_visible_x)
18871 it.glyph_row->used[TEXT_AREA] = n_glyphs_before;
18872 break;
18876 set_buffer_temp (old);
18877 return it.glyph_row;
18881 /* Insert truncation glyphs at the start of IT->glyph_row. Which
18882 glyphs to insert is determined by produce_special_glyphs. */
18884 static void
18885 insert_left_trunc_glyphs (struct it *it)
18887 struct it truncate_it;
18888 struct glyph *from, *end, *to, *toend;
18890 eassert (!FRAME_WINDOW_P (it->f)
18891 || (!it->glyph_row->reversed_p
18892 && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0)
18893 || (it->glyph_row->reversed_p
18894 && WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0));
18896 /* Get the truncation glyphs. */
18897 truncate_it = *it;
18898 truncate_it.current_x = 0;
18899 truncate_it.face_id = DEFAULT_FACE_ID;
18900 truncate_it.glyph_row = &scratch_glyph_row;
18901 truncate_it.area = TEXT_AREA;
18902 truncate_it.glyph_row->used[TEXT_AREA] = 0;
18903 CHARPOS (truncate_it.position) = BYTEPOS (truncate_it.position) = -1;
18904 truncate_it.object = Qnil;
18905 produce_special_glyphs (&truncate_it, IT_TRUNCATION);
18907 /* Overwrite glyphs from IT with truncation glyphs. */
18908 if (!it->glyph_row->reversed_p)
18910 short tused = truncate_it.glyph_row->used[TEXT_AREA];
18912 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
18913 end = from + tused;
18914 to = it->glyph_row->glyphs[TEXT_AREA];
18915 toend = to + it->glyph_row->used[TEXT_AREA];
18916 if (FRAME_WINDOW_P (it->f))
18918 /* On GUI frames, when variable-size fonts are displayed,
18919 the truncation glyphs may need more pixels than the row's
18920 glyphs they overwrite. We overwrite more glyphs to free
18921 enough screen real estate, and enlarge the stretch glyph
18922 on the right (see display_line), if there is one, to
18923 preserve the screen position of the truncation glyphs on
18924 the right. */
18925 int w = 0;
18926 struct glyph *g = to;
18927 short used;
18929 /* The first glyph could be partially visible, in which case
18930 it->glyph_row->x will be negative. But we want the left
18931 truncation glyphs to be aligned at the left margin of the
18932 window, so we override the x coordinate at which the row
18933 will begin. */
18934 it->glyph_row->x = 0;
18935 while (g < toend && w < it->truncation_pixel_width)
18937 w += g->pixel_width;
18938 ++g;
18940 if (g - to - tused > 0)
18942 memmove (to + tused, g, (toend - g) * sizeof(*g));
18943 it->glyph_row->used[TEXT_AREA] -= g - to - tused;
18945 used = it->glyph_row->used[TEXT_AREA];
18946 if (it->glyph_row->truncated_on_right_p
18947 && WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0
18948 && it->glyph_row->glyphs[TEXT_AREA][used - 2].type
18949 == STRETCH_GLYPH)
18951 int extra = w - it->truncation_pixel_width;
18953 it->glyph_row->glyphs[TEXT_AREA][used - 2].pixel_width += extra;
18957 while (from < end)
18958 *to++ = *from++;
18960 /* There may be padding glyphs left over. Overwrite them too. */
18961 if (!FRAME_WINDOW_P (it->f))
18963 while (to < toend && CHAR_GLYPH_PADDING_P (*to))
18965 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
18966 while (from < end)
18967 *to++ = *from++;
18971 if (to > toend)
18972 it->glyph_row->used[TEXT_AREA] = to - it->glyph_row->glyphs[TEXT_AREA];
18974 else
18976 short tused = truncate_it.glyph_row->used[TEXT_AREA];
18978 /* In R2L rows, overwrite the last (rightmost) glyphs, and do
18979 that back to front. */
18980 end = truncate_it.glyph_row->glyphs[TEXT_AREA];
18981 from = end + truncate_it.glyph_row->used[TEXT_AREA] - 1;
18982 toend = it->glyph_row->glyphs[TEXT_AREA];
18983 to = toend + it->glyph_row->used[TEXT_AREA] - 1;
18984 if (FRAME_WINDOW_P (it->f))
18986 int w = 0;
18987 struct glyph *g = to;
18989 while (g >= toend && w < it->truncation_pixel_width)
18991 w += g->pixel_width;
18992 --g;
18994 if (to - g - tused > 0)
18995 to = g + tused;
18996 if (it->glyph_row->truncated_on_right_p
18997 && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0
18998 && it->glyph_row->glyphs[TEXT_AREA][1].type == STRETCH_GLYPH)
19000 int extra = w - it->truncation_pixel_width;
19002 it->glyph_row->glyphs[TEXT_AREA][1].pixel_width += extra;
19006 while (from >= end && to >= toend)
19007 *to-- = *from--;
19008 if (!FRAME_WINDOW_P (it->f))
19010 while (to >= toend && CHAR_GLYPH_PADDING_P (*to))
19012 from =
19013 truncate_it.glyph_row->glyphs[TEXT_AREA]
19014 + truncate_it.glyph_row->used[TEXT_AREA] - 1;
19015 while (from >= end && to >= toend)
19016 *to-- = *from--;
19019 if (from >= end)
19021 /* Need to free some room before prepending additional
19022 glyphs. */
19023 int move_by = from - end + 1;
19024 struct glyph *g0 = it->glyph_row->glyphs[TEXT_AREA];
19025 struct glyph *g = g0 + it->glyph_row->used[TEXT_AREA] - 1;
19027 for ( ; g >= g0; g--)
19028 g[move_by] = *g;
19029 while (from >= end)
19030 *to-- = *from--;
19031 it->glyph_row->used[TEXT_AREA] += move_by;
19036 /* Compute the hash code for ROW. */
19037 unsigned
19038 row_hash (struct glyph_row *row)
19040 int area, k;
19041 unsigned hashval = 0;
19043 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
19044 for (k = 0; k < row->used[area]; ++k)
19045 hashval = ((((hashval << 4) + (hashval >> 24)) & 0x0fffffff)
19046 + row->glyphs[area][k].u.val
19047 + row->glyphs[area][k].face_id
19048 + row->glyphs[area][k].padding_p
19049 + (row->glyphs[area][k].type << 2));
19051 return hashval;
19054 /* Compute the pixel height and width of IT->glyph_row.
19056 Most of the time, ascent and height of a display line will be equal
19057 to the max_ascent and max_height values of the display iterator
19058 structure. This is not the case if
19060 1. We hit ZV without displaying anything. In this case, max_ascent
19061 and max_height will be zero.
19063 2. We have some glyphs that don't contribute to the line height.
19064 (The glyph row flag contributes_to_line_height_p is for future
19065 pixmap extensions).
19067 The first case is easily covered by using default values because in
19068 these cases, the line height does not really matter, except that it
19069 must not be zero. */
19071 static void
19072 compute_line_metrics (struct it *it)
19074 struct glyph_row *row = it->glyph_row;
19076 if (FRAME_WINDOW_P (it->f))
19078 int i, min_y, max_y;
19080 /* The line may consist of one space only, that was added to
19081 place the cursor on it. If so, the row's height hasn't been
19082 computed yet. */
19083 if (row->height == 0)
19085 if (it->max_ascent + it->max_descent == 0)
19086 it->max_descent = it->max_phys_descent = FRAME_LINE_HEIGHT (it->f);
19087 row->ascent = it->max_ascent;
19088 row->height = it->max_ascent + it->max_descent;
19089 row->phys_ascent = it->max_phys_ascent;
19090 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
19091 row->extra_line_spacing = it->max_extra_line_spacing;
19094 /* Compute the width of this line. */
19095 row->pixel_width = row->x;
19096 for (i = 0; i < row->used[TEXT_AREA]; ++i)
19097 row->pixel_width += row->glyphs[TEXT_AREA][i].pixel_width;
19099 eassert (row->pixel_width >= 0);
19100 eassert (row->ascent >= 0 && row->height > 0);
19102 row->overlapping_p = (MATRIX_ROW_OVERLAPS_SUCC_P (row)
19103 || MATRIX_ROW_OVERLAPS_PRED_P (row));
19105 /* If first line's physical ascent is larger than its logical
19106 ascent, use the physical ascent, and make the row taller.
19107 This makes accented characters fully visible. */
19108 if (row == MATRIX_FIRST_TEXT_ROW (it->w->desired_matrix)
19109 && row->phys_ascent > row->ascent)
19111 row->height += row->phys_ascent - row->ascent;
19112 row->ascent = row->phys_ascent;
19115 /* Compute how much of the line is visible. */
19116 row->visible_height = row->height;
19118 min_y = WINDOW_HEADER_LINE_HEIGHT (it->w);
19119 max_y = WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w);
19121 if (row->y < min_y)
19122 row->visible_height -= min_y - row->y;
19123 if (row->y + row->height > max_y)
19124 row->visible_height -= row->y + row->height - max_y;
19126 else
19128 row->pixel_width = row->used[TEXT_AREA];
19129 if (row->continued_p)
19130 row->pixel_width -= it->continuation_pixel_width;
19131 else if (row->truncated_on_right_p)
19132 row->pixel_width -= it->truncation_pixel_width;
19133 row->ascent = row->phys_ascent = 0;
19134 row->height = row->phys_height = row->visible_height = 1;
19135 row->extra_line_spacing = 0;
19138 /* Compute a hash code for this row. */
19139 row->hash = row_hash (row);
19141 it->max_ascent = it->max_descent = 0;
19142 it->max_phys_ascent = it->max_phys_descent = 0;
19146 /* Append one space to the glyph row of iterator IT if doing a
19147 window-based redisplay. The space has the same face as
19148 IT->face_id. Value is non-zero if a space was added.
19150 This function is called to make sure that there is always one glyph
19151 at the end of a glyph row that the cursor can be set on under
19152 window-systems. (If there weren't such a glyph we would not know
19153 how wide and tall a box cursor should be displayed).
19155 At the same time this space let's a nicely handle clearing to the
19156 end of the line if the row ends in italic text. */
19158 static int
19159 append_space_for_newline (struct it *it, int default_face_p)
19161 if (FRAME_WINDOW_P (it->f))
19163 int n = it->glyph_row->used[TEXT_AREA];
19165 if (it->glyph_row->glyphs[TEXT_AREA] + n
19166 < it->glyph_row->glyphs[1 + TEXT_AREA])
19168 /* Save some values that must not be changed.
19169 Must save IT->c and IT->len because otherwise
19170 ITERATOR_AT_END_P wouldn't work anymore after
19171 append_space_for_newline has been called. */
19172 enum display_element_type saved_what = it->what;
19173 int saved_c = it->c, saved_len = it->len;
19174 int saved_char_to_display = it->char_to_display;
19175 int saved_x = it->current_x;
19176 int saved_face_id = it->face_id;
19177 int saved_box_end = it->end_of_box_run_p;
19178 struct text_pos saved_pos;
19179 Lisp_Object saved_object;
19180 struct face *face;
19182 saved_object = it->object;
19183 saved_pos = it->position;
19185 it->what = IT_CHARACTER;
19186 memset (&it->position, 0, sizeof it->position);
19187 it->object = Qnil;
19188 it->c = it->char_to_display = ' ';
19189 it->len = 1;
19191 /* If the default face was remapped, be sure to use the
19192 remapped face for the appended newline. */
19193 if (default_face_p)
19194 it->face_id = lookup_basic_face (it->f, DEFAULT_FACE_ID);
19195 else if (it->face_before_selective_p)
19196 it->face_id = it->saved_face_id;
19197 face = FACE_FROM_ID (it->f, it->face_id);
19198 it->face_id = FACE_FOR_CHAR (it->f, face, 0, -1, Qnil);
19199 /* In R2L rows, we will prepend a stretch glyph that will
19200 have the end_of_box_run_p flag set for it, so there's no
19201 need for the appended newline glyph to have that flag
19202 set. */
19203 if (it->glyph_row->reversed_p
19204 /* But if the appended newline glyph goes all the way to
19205 the end of the row, there will be no stretch glyph,
19206 so leave the box flag set. */
19207 && saved_x + FRAME_COLUMN_WIDTH (it->f) < it->last_visible_x)
19208 it->end_of_box_run_p = 0;
19210 PRODUCE_GLYPHS (it);
19212 it->override_ascent = -1;
19213 it->constrain_row_ascent_descent_p = 0;
19214 it->current_x = saved_x;
19215 it->object = saved_object;
19216 it->position = saved_pos;
19217 it->what = saved_what;
19218 it->face_id = saved_face_id;
19219 it->len = saved_len;
19220 it->c = saved_c;
19221 it->char_to_display = saved_char_to_display;
19222 it->end_of_box_run_p = saved_box_end;
19223 return 1;
19227 return 0;
19231 /* Extend the face of the last glyph in the text area of IT->glyph_row
19232 to the end of the display line. Called from display_line. If the
19233 glyph row is empty, add a space glyph to it so that we know the
19234 face to draw. Set the glyph row flag fill_line_p. If the glyph
19235 row is R2L, prepend a stretch glyph to cover the empty space to the
19236 left of the leftmost glyph. */
19238 static void
19239 extend_face_to_end_of_line (struct it *it)
19241 struct face *face, *default_face;
19242 struct frame *f = it->f;
19244 /* If line is already filled, do nothing. Non window-system frames
19245 get a grace of one more ``pixel'' because their characters are
19246 1-``pixel'' wide, so they hit the equality too early. This grace
19247 is needed only for R2L rows that are not continued, to produce
19248 one extra blank where we could display the cursor. */
19249 if ((it->current_x >= it->last_visible_x
19250 + (!FRAME_WINDOW_P (f)
19251 && it->glyph_row->reversed_p
19252 && !it->glyph_row->continued_p))
19253 /* If the window has display margins, we will need to extend
19254 their face even if the text area is filled. */
19255 && !(WINDOW_LEFT_MARGIN_WIDTH (it->w) > 0
19256 || WINDOW_RIGHT_MARGIN_WIDTH (it->w) > 0))
19257 return;
19259 /* The default face, possibly remapped. */
19260 default_face = FACE_FROM_ID (f, lookup_basic_face (f, DEFAULT_FACE_ID));
19262 /* Face extension extends the background and box of IT->face_id
19263 to the end of the line. If the background equals the background
19264 of the frame, we don't have to do anything. */
19265 if (it->face_before_selective_p)
19266 face = FACE_FROM_ID (f, it->saved_face_id);
19267 else
19268 face = FACE_FROM_ID (f, it->face_id);
19270 if (FRAME_WINDOW_P (f)
19271 && MATRIX_ROW_DISPLAYS_TEXT_P (it->glyph_row)
19272 && face->box == FACE_NO_BOX
19273 && face->background == FRAME_BACKGROUND_PIXEL (f)
19274 #ifdef HAVE_WINDOW_SYSTEM
19275 && !face->stipple
19276 #endif
19277 && !it->glyph_row->reversed_p)
19278 return;
19280 /* Set the glyph row flag indicating that the face of the last glyph
19281 in the text area has to be drawn to the end of the text area. */
19282 it->glyph_row->fill_line_p = 1;
19284 /* If current character of IT is not ASCII, make sure we have the
19285 ASCII face. This will be automatically undone the next time
19286 get_next_display_element returns a multibyte character. Note
19287 that the character will always be single byte in unibyte
19288 text. */
19289 if (!ASCII_CHAR_P (it->c))
19291 it->face_id = FACE_FOR_CHAR (f, face, 0, -1, Qnil);
19294 if (FRAME_WINDOW_P (f))
19296 /* If the row is empty, add a space with the current face of IT,
19297 so that we know which face to draw. */
19298 if (it->glyph_row->used[TEXT_AREA] == 0)
19300 it->glyph_row->glyphs[TEXT_AREA][0] = space_glyph;
19301 it->glyph_row->glyphs[TEXT_AREA][0].face_id = face->id;
19302 it->glyph_row->used[TEXT_AREA] = 1;
19304 /* Mode line and the header line don't have margins, and
19305 likewise the frame's tool-bar window, if there is any. */
19306 if (!(it->glyph_row->mode_line_p
19307 #if defined (HAVE_WINDOW_SYSTEM) && ! defined (USE_GTK) && ! defined (HAVE_NS)
19308 || (WINDOWP (f->tool_bar_window)
19309 && it->w == XWINDOW (f->tool_bar_window))
19310 #endif
19313 if (WINDOW_LEFT_MARGIN_WIDTH (it->w) > 0
19314 && it->glyph_row->used[LEFT_MARGIN_AREA] == 0)
19316 it->glyph_row->glyphs[LEFT_MARGIN_AREA][0] = space_glyph;
19317 it->glyph_row->glyphs[LEFT_MARGIN_AREA][0].face_id =
19318 default_face->id;
19319 it->glyph_row->used[LEFT_MARGIN_AREA] = 1;
19321 if (WINDOW_RIGHT_MARGIN_WIDTH (it->w) > 0
19322 && it->glyph_row->used[RIGHT_MARGIN_AREA] == 0)
19324 it->glyph_row->glyphs[RIGHT_MARGIN_AREA][0] = space_glyph;
19325 it->glyph_row->glyphs[RIGHT_MARGIN_AREA][0].face_id =
19326 default_face->id;
19327 it->glyph_row->used[RIGHT_MARGIN_AREA] = 1;
19330 #ifdef HAVE_WINDOW_SYSTEM
19331 if (it->glyph_row->reversed_p)
19333 /* Prepend a stretch glyph to the row, such that the
19334 rightmost glyph will be drawn flushed all the way to the
19335 right margin of the window. The stretch glyph that will
19336 occupy the empty space, if any, to the left of the
19337 glyphs. */
19338 struct font *font = face->font ? face->font : FRAME_FONT (f);
19339 struct glyph *row_start = it->glyph_row->glyphs[TEXT_AREA];
19340 struct glyph *row_end = row_start + it->glyph_row->used[TEXT_AREA];
19341 struct glyph *g;
19342 int row_width, stretch_ascent, stretch_width;
19343 struct text_pos saved_pos;
19344 int saved_face_id, saved_avoid_cursor, saved_box_start;
19346 for (row_width = 0, g = row_start; g < row_end; g++)
19347 row_width += g->pixel_width;
19349 /* FIXME: There are various minor display glitches in R2L
19350 rows when only one of the fringes is missing. The
19351 strange condition below produces the least bad effect. */
19352 if ((WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0)
19353 == (WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0)
19354 || WINDOW_RIGHT_FRINGE_WIDTH (it->w) != 0)
19355 stretch_width = window_box_width (it->w, TEXT_AREA);
19356 else
19357 stretch_width = it->last_visible_x - it->first_visible_x;
19358 stretch_width -= row_width;
19360 if (stretch_width > 0)
19362 stretch_ascent =
19363 (((it->ascent + it->descent)
19364 * FONT_BASE (font)) / FONT_HEIGHT (font));
19365 saved_pos = it->position;
19366 memset (&it->position, 0, sizeof it->position);
19367 saved_avoid_cursor = it->avoid_cursor_p;
19368 it->avoid_cursor_p = 1;
19369 saved_face_id = it->face_id;
19370 saved_box_start = it->start_of_box_run_p;
19371 /* The last row's stretch glyph should get the default
19372 face, to avoid painting the rest of the window with
19373 the region face, if the region ends at ZV. */
19374 if (it->glyph_row->ends_at_zv_p)
19375 it->face_id = default_face->id;
19376 else
19377 it->face_id = face->id;
19378 it->start_of_box_run_p = 0;
19379 append_stretch_glyph (it, Qnil, stretch_width,
19380 it->ascent + it->descent, stretch_ascent);
19381 it->position = saved_pos;
19382 it->avoid_cursor_p = saved_avoid_cursor;
19383 it->face_id = saved_face_id;
19384 it->start_of_box_run_p = saved_box_start;
19386 /* If stretch_width comes out negative, it means that the
19387 last glyph is only partially visible. In R2L rows, we
19388 want the leftmost glyph to be partially visible, so we
19389 need to give the row the corresponding left offset. */
19390 if (stretch_width < 0)
19391 it->glyph_row->x = stretch_width;
19393 #endif /* HAVE_WINDOW_SYSTEM */
19395 else
19397 /* Save some values that must not be changed. */
19398 int saved_x = it->current_x;
19399 struct text_pos saved_pos;
19400 Lisp_Object saved_object;
19401 enum display_element_type saved_what = it->what;
19402 int saved_face_id = it->face_id;
19404 saved_object = it->object;
19405 saved_pos = it->position;
19407 it->what = IT_CHARACTER;
19408 memset (&it->position, 0, sizeof it->position);
19409 it->object = Qnil;
19410 it->c = it->char_to_display = ' ';
19411 it->len = 1;
19413 if (WINDOW_LEFT_MARGIN_WIDTH (it->w) > 0
19414 && (it->glyph_row->used[LEFT_MARGIN_AREA]
19415 < WINDOW_LEFT_MARGIN_WIDTH (it->w))
19416 && !it->glyph_row->mode_line_p
19417 && default_face->background != FRAME_BACKGROUND_PIXEL (f))
19419 struct glyph *g = it->glyph_row->glyphs[LEFT_MARGIN_AREA];
19420 struct glyph *e = g + it->glyph_row->used[LEFT_MARGIN_AREA];
19422 for (it->current_x = 0; g < e; g++)
19423 it->current_x += g->pixel_width;
19425 it->area = LEFT_MARGIN_AREA;
19426 it->face_id = default_face->id;
19427 while (it->glyph_row->used[LEFT_MARGIN_AREA]
19428 < WINDOW_LEFT_MARGIN_WIDTH (it->w))
19430 PRODUCE_GLYPHS (it);
19431 /* term.c:produce_glyphs advances it->current_x only for
19432 TEXT_AREA. */
19433 it->current_x += it->pixel_width;
19436 it->current_x = saved_x;
19437 it->area = TEXT_AREA;
19440 /* The last row's blank glyphs should get the default face, to
19441 avoid painting the rest of the window with the region face,
19442 if the region ends at ZV. */
19443 if (it->glyph_row->ends_at_zv_p)
19444 it->face_id = default_face->id;
19445 else
19446 it->face_id = face->id;
19447 PRODUCE_GLYPHS (it);
19449 while (it->current_x <= it->last_visible_x)
19450 PRODUCE_GLYPHS (it);
19452 if (WINDOW_RIGHT_MARGIN_WIDTH (it->w) > 0
19453 && (it->glyph_row->used[RIGHT_MARGIN_AREA]
19454 < WINDOW_RIGHT_MARGIN_WIDTH (it->w))
19455 && !it->glyph_row->mode_line_p
19456 && default_face->background != FRAME_BACKGROUND_PIXEL (f))
19458 struct glyph *g = it->glyph_row->glyphs[RIGHT_MARGIN_AREA];
19459 struct glyph *e = g + it->glyph_row->used[RIGHT_MARGIN_AREA];
19461 for ( ; g < e; g++)
19462 it->current_x += g->pixel_width;
19464 it->area = RIGHT_MARGIN_AREA;
19465 it->face_id = default_face->id;
19466 while (it->glyph_row->used[RIGHT_MARGIN_AREA]
19467 < WINDOW_RIGHT_MARGIN_WIDTH (it->w))
19469 PRODUCE_GLYPHS (it);
19470 it->current_x += it->pixel_width;
19473 it->area = TEXT_AREA;
19476 /* Don't count these blanks really. It would let us insert a left
19477 truncation glyph below and make us set the cursor on them, maybe. */
19478 it->current_x = saved_x;
19479 it->object = saved_object;
19480 it->position = saved_pos;
19481 it->what = saved_what;
19482 it->face_id = saved_face_id;
19487 /* Value is non-zero if text starting at CHARPOS in current_buffer is
19488 trailing whitespace. */
19490 static int
19491 trailing_whitespace_p (ptrdiff_t charpos)
19493 ptrdiff_t bytepos = CHAR_TO_BYTE (charpos);
19494 int c = 0;
19496 while (bytepos < ZV_BYTE
19497 && (c = FETCH_CHAR (bytepos),
19498 c == ' ' || c == '\t'))
19499 ++bytepos;
19501 if (bytepos >= ZV_BYTE || c == '\n' || c == '\r')
19503 if (bytepos != PT_BYTE)
19504 return 1;
19506 return 0;
19510 /* Highlight trailing whitespace, if any, in ROW. */
19512 static void
19513 highlight_trailing_whitespace (struct frame *f, struct glyph_row *row)
19515 int used = row->used[TEXT_AREA];
19517 if (used)
19519 struct glyph *start = row->glyphs[TEXT_AREA];
19520 struct glyph *glyph = start + used - 1;
19522 if (row->reversed_p)
19524 /* Right-to-left rows need to be processed in the opposite
19525 direction, so swap the edge pointers. */
19526 glyph = start;
19527 start = row->glyphs[TEXT_AREA] + used - 1;
19530 /* Skip over glyphs inserted to display the cursor at the
19531 end of a line, for extending the face of the last glyph
19532 to the end of the line on terminals, and for truncation
19533 and continuation glyphs. */
19534 if (!row->reversed_p)
19536 while (glyph >= start
19537 && glyph->type == CHAR_GLYPH
19538 && NILP (glyph->object))
19539 --glyph;
19541 else
19543 while (glyph <= start
19544 && glyph->type == CHAR_GLYPH
19545 && NILP (glyph->object))
19546 ++glyph;
19549 /* If last glyph is a space or stretch, and it's trailing
19550 whitespace, set the face of all trailing whitespace glyphs in
19551 IT->glyph_row to `trailing-whitespace'. */
19552 if ((row->reversed_p ? glyph <= start : glyph >= start)
19553 && BUFFERP (glyph->object)
19554 && (glyph->type == STRETCH_GLYPH
19555 || (glyph->type == CHAR_GLYPH
19556 && glyph->u.ch == ' '))
19557 && trailing_whitespace_p (glyph->charpos))
19559 int face_id = lookup_named_face (f, Qtrailing_whitespace, 0);
19560 if (face_id < 0)
19561 return;
19563 if (!row->reversed_p)
19565 while (glyph >= start
19566 && BUFFERP (glyph->object)
19567 && (glyph->type == STRETCH_GLYPH
19568 || (glyph->type == CHAR_GLYPH
19569 && glyph->u.ch == ' ')))
19570 (glyph--)->face_id = face_id;
19572 else
19574 while (glyph <= start
19575 && BUFFERP (glyph->object)
19576 && (glyph->type == STRETCH_GLYPH
19577 || (glyph->type == CHAR_GLYPH
19578 && glyph->u.ch == ' ')))
19579 (glyph++)->face_id = face_id;
19586 /* Value is non-zero if glyph row ROW should be
19587 considered to hold the buffer position CHARPOS. */
19589 static int
19590 row_for_charpos_p (struct glyph_row *row, ptrdiff_t charpos)
19592 int result = 1;
19594 if (charpos == CHARPOS (row->end.pos)
19595 || charpos == MATRIX_ROW_END_CHARPOS (row))
19597 /* Suppose the row ends on a string.
19598 Unless the row is continued, that means it ends on a newline
19599 in the string. If it's anything other than a display string
19600 (e.g., a before-string from an overlay), we don't want the
19601 cursor there. (This heuristic seems to give the optimal
19602 behavior for the various types of multi-line strings.)
19603 One exception: if the string has `cursor' property on one of
19604 its characters, we _do_ want the cursor there. */
19605 if (CHARPOS (row->end.string_pos) >= 0)
19607 if (row->continued_p)
19608 result = 1;
19609 else
19611 /* Check for `display' property. */
19612 struct glyph *beg = row->glyphs[TEXT_AREA];
19613 struct glyph *end = beg + row->used[TEXT_AREA] - 1;
19614 struct glyph *glyph;
19616 result = 0;
19617 for (glyph = end; glyph >= beg; --glyph)
19618 if (STRINGP (glyph->object))
19620 Lisp_Object prop
19621 = Fget_char_property (make_number (charpos),
19622 Qdisplay, Qnil);
19623 result =
19624 (!NILP (prop)
19625 && display_prop_string_p (prop, glyph->object));
19626 /* If there's a `cursor' property on one of the
19627 string's characters, this row is a cursor row,
19628 even though this is not a display string. */
19629 if (!result)
19631 Lisp_Object s = glyph->object;
19633 for ( ; glyph >= beg && EQ (glyph->object, s); --glyph)
19635 ptrdiff_t gpos = glyph->charpos;
19637 if (!NILP (Fget_char_property (make_number (gpos),
19638 Qcursor, s)))
19640 result = 1;
19641 break;
19645 break;
19649 else if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
19651 /* If the row ends in middle of a real character,
19652 and the line is continued, we want the cursor here.
19653 That's because CHARPOS (ROW->end.pos) would equal
19654 PT if PT is before the character. */
19655 if (!row->ends_in_ellipsis_p)
19656 result = row->continued_p;
19657 else
19658 /* If the row ends in an ellipsis, then
19659 CHARPOS (ROW->end.pos) will equal point after the
19660 invisible text. We want that position to be displayed
19661 after the ellipsis. */
19662 result = 0;
19664 /* If the row ends at ZV, display the cursor at the end of that
19665 row instead of at the start of the row below. */
19666 else if (row->ends_at_zv_p)
19667 result = 1;
19668 else
19669 result = 0;
19672 return result;
19675 /* Value is non-zero if glyph row ROW should be
19676 used to hold the cursor. */
19678 static int
19679 cursor_row_p (struct glyph_row *row)
19681 return row_for_charpos_p (row, PT);
19686 /* Push the property PROP so that it will be rendered at the current
19687 position in IT. Return 1 if PROP was successfully pushed, 0
19688 otherwise. Called from handle_line_prefix to handle the
19689 `line-prefix' and `wrap-prefix' properties. */
19691 static int
19692 push_prefix_prop (struct it *it, Lisp_Object prop)
19694 struct text_pos pos =
19695 STRINGP (it->string) ? it->current.string_pos : it->current.pos;
19697 eassert (it->method == GET_FROM_BUFFER
19698 || it->method == GET_FROM_DISPLAY_VECTOR
19699 || it->method == GET_FROM_STRING);
19701 /* We need to save the current buffer/string position, so it will be
19702 restored by pop_it, because iterate_out_of_display_property
19703 depends on that being set correctly, but some situations leave
19704 it->position not yet set when this function is called. */
19705 push_it (it, &pos);
19707 if (STRINGP (prop))
19709 if (SCHARS (prop) == 0)
19711 pop_it (it);
19712 return 0;
19715 it->string = prop;
19716 it->string_from_prefix_prop_p = 1;
19717 it->multibyte_p = STRING_MULTIBYTE (it->string);
19718 it->current.overlay_string_index = -1;
19719 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
19720 it->end_charpos = it->string_nchars = SCHARS (it->string);
19721 it->method = GET_FROM_STRING;
19722 it->stop_charpos = 0;
19723 it->prev_stop = 0;
19724 it->base_level_stop = 0;
19726 /* Force paragraph direction to be that of the parent
19727 buffer/string. */
19728 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
19729 it->paragraph_embedding = it->bidi_it.paragraph_dir;
19730 else
19731 it->paragraph_embedding = L2R;
19733 /* Set up the bidi iterator for this display string. */
19734 if (it->bidi_p)
19736 it->bidi_it.string.lstring = it->string;
19737 it->bidi_it.string.s = NULL;
19738 it->bidi_it.string.schars = it->end_charpos;
19739 it->bidi_it.string.bufpos = IT_CHARPOS (*it);
19740 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
19741 it->bidi_it.string.unibyte = !it->multibyte_p;
19742 it->bidi_it.w = it->w;
19743 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
19746 else if (CONSP (prop) && EQ (XCAR (prop), Qspace))
19748 it->method = GET_FROM_STRETCH;
19749 it->object = prop;
19751 #ifdef HAVE_WINDOW_SYSTEM
19752 else if (IMAGEP (prop))
19754 it->what = IT_IMAGE;
19755 it->image_id = lookup_image (it->f, prop);
19756 it->method = GET_FROM_IMAGE;
19758 #endif /* HAVE_WINDOW_SYSTEM */
19759 else
19761 pop_it (it); /* bogus display property, give up */
19762 return 0;
19765 return 1;
19768 /* Return the character-property PROP at the current position in IT. */
19770 static Lisp_Object
19771 get_it_property (struct it *it, Lisp_Object prop)
19773 Lisp_Object position, object = it->object;
19775 if (STRINGP (object))
19776 position = make_number (IT_STRING_CHARPOS (*it));
19777 else if (BUFFERP (object))
19779 position = make_number (IT_CHARPOS (*it));
19780 object = it->window;
19782 else
19783 return Qnil;
19785 return Fget_char_property (position, prop, object);
19788 /* See if there's a line- or wrap-prefix, and if so, push it on IT. */
19790 static void
19791 handle_line_prefix (struct it *it)
19793 Lisp_Object prefix;
19795 if (it->continuation_lines_width > 0)
19797 prefix = get_it_property (it, Qwrap_prefix);
19798 if (NILP (prefix))
19799 prefix = Vwrap_prefix;
19801 else
19803 prefix = get_it_property (it, Qline_prefix);
19804 if (NILP (prefix))
19805 prefix = Vline_prefix;
19807 if (! NILP (prefix) && push_prefix_prop (it, prefix))
19809 /* If the prefix is wider than the window, and we try to wrap
19810 it, it would acquire its own wrap prefix, and so on till the
19811 iterator stack overflows. So, don't wrap the prefix. */
19812 it->line_wrap = TRUNCATE;
19813 it->avoid_cursor_p = 1;
19819 /* Remove N glyphs at the start of a reversed IT->glyph_row. Called
19820 only for R2L lines from display_line and display_string, when they
19821 decide that too many glyphs were produced by PRODUCE_GLYPHS, and
19822 the line/string needs to be continued on the next glyph row. */
19823 static void
19824 unproduce_glyphs (struct it *it, int n)
19826 struct glyph *glyph, *end;
19828 eassert (it->glyph_row);
19829 eassert (it->glyph_row->reversed_p);
19830 eassert (it->area == TEXT_AREA);
19831 eassert (n <= it->glyph_row->used[TEXT_AREA]);
19833 if (n > it->glyph_row->used[TEXT_AREA])
19834 n = it->glyph_row->used[TEXT_AREA];
19835 glyph = it->glyph_row->glyphs[TEXT_AREA] + n;
19836 end = it->glyph_row->glyphs[TEXT_AREA] + it->glyph_row->used[TEXT_AREA];
19837 for ( ; glyph < end; glyph++)
19838 glyph[-n] = *glyph;
19841 /* Find the positions in a bidi-reordered ROW to serve as ROW->minpos
19842 and ROW->maxpos. */
19843 static void
19844 find_row_edges (struct it *it, struct glyph_row *row,
19845 ptrdiff_t min_pos, ptrdiff_t min_bpos,
19846 ptrdiff_t max_pos, ptrdiff_t max_bpos)
19848 /* FIXME: Revisit this when glyph ``spilling'' in continuation
19849 lines' rows is implemented for bidi-reordered rows. */
19851 /* ROW->minpos is the value of min_pos, the minimal buffer position
19852 we have in ROW, or ROW->start.pos if that is smaller. */
19853 if (min_pos <= ZV && min_pos < row->start.pos.charpos)
19854 SET_TEXT_POS (row->minpos, min_pos, min_bpos);
19855 else
19856 /* We didn't find buffer positions smaller than ROW->start, or
19857 didn't find _any_ valid buffer positions in any of the glyphs,
19858 so we must trust the iterator's computed positions. */
19859 row->minpos = row->start.pos;
19860 if (max_pos <= 0)
19862 max_pos = CHARPOS (it->current.pos);
19863 max_bpos = BYTEPOS (it->current.pos);
19866 /* Here are the various use-cases for ending the row, and the
19867 corresponding values for ROW->maxpos:
19869 Line ends in a newline from buffer eol_pos + 1
19870 Line is continued from buffer max_pos + 1
19871 Line is truncated on right it->current.pos
19872 Line ends in a newline from string max_pos + 1(*)
19873 (*) + 1 only when line ends in a forward scan
19874 Line is continued from string max_pos
19875 Line is continued from display vector max_pos
19876 Line is entirely from a string min_pos == max_pos
19877 Line is entirely from a display vector min_pos == max_pos
19878 Line that ends at ZV ZV
19880 If you discover other use-cases, please add them here as
19881 appropriate. */
19882 if (row->ends_at_zv_p)
19883 row->maxpos = it->current.pos;
19884 else if (row->used[TEXT_AREA])
19886 int seen_this_string = 0;
19887 struct glyph_row *r1 = row - 1;
19889 /* Did we see the same display string on the previous row? */
19890 if (STRINGP (it->object)
19891 /* this is not the first row */
19892 && row > it->w->desired_matrix->rows
19893 /* previous row is not the header line */
19894 && !r1->mode_line_p
19895 /* previous row also ends in a newline from a string */
19896 && r1->ends_in_newline_from_string_p)
19898 struct glyph *start, *end;
19900 /* Search for the last glyph of the previous row that came
19901 from buffer or string. Depending on whether the row is
19902 L2R or R2L, we need to process it front to back or the
19903 other way round. */
19904 if (!r1->reversed_p)
19906 start = r1->glyphs[TEXT_AREA];
19907 end = start + r1->used[TEXT_AREA];
19908 /* Glyphs inserted by redisplay have nil as their object. */
19909 while (end > start
19910 && NILP ((end - 1)->object)
19911 && (end - 1)->charpos <= 0)
19912 --end;
19913 if (end > start)
19915 if (EQ ((end - 1)->object, it->object))
19916 seen_this_string = 1;
19918 else
19919 /* If all the glyphs of the previous row were inserted
19920 by redisplay, it means the previous row was
19921 produced from a single newline, which is only
19922 possible if that newline came from the same string
19923 as the one which produced this ROW. */
19924 seen_this_string = 1;
19926 else
19928 end = r1->glyphs[TEXT_AREA] - 1;
19929 start = end + r1->used[TEXT_AREA];
19930 while (end < start
19931 && NILP ((end + 1)->object)
19932 && (end + 1)->charpos <= 0)
19933 ++end;
19934 if (end < start)
19936 if (EQ ((end + 1)->object, it->object))
19937 seen_this_string = 1;
19939 else
19940 seen_this_string = 1;
19943 /* Take note of each display string that covers a newline only
19944 once, the first time we see it. This is for when a display
19945 string includes more than one newline in it. */
19946 if (row->ends_in_newline_from_string_p && !seen_this_string)
19948 /* If we were scanning the buffer forward when we displayed
19949 the string, we want to account for at least one buffer
19950 position that belongs to this row (position covered by
19951 the display string), so that cursor positioning will
19952 consider this row as a candidate when point is at the end
19953 of the visual line represented by this row. This is not
19954 required when scanning back, because max_pos will already
19955 have a much larger value. */
19956 if (CHARPOS (row->end.pos) > max_pos)
19957 INC_BOTH (max_pos, max_bpos);
19958 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
19960 else if (CHARPOS (it->eol_pos) > 0)
19961 SET_TEXT_POS (row->maxpos,
19962 CHARPOS (it->eol_pos) + 1, BYTEPOS (it->eol_pos) + 1);
19963 else if (row->continued_p)
19965 /* If max_pos is different from IT's current position, it
19966 means IT->method does not belong to the display element
19967 at max_pos. However, it also means that the display
19968 element at max_pos was displayed in its entirety on this
19969 line, which is equivalent to saying that the next line
19970 starts at the next buffer position. */
19971 if (IT_CHARPOS (*it) == max_pos && it->method != GET_FROM_BUFFER)
19972 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
19973 else
19975 INC_BOTH (max_pos, max_bpos);
19976 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
19979 else if (row->truncated_on_right_p)
19980 /* display_line already called reseat_at_next_visible_line_start,
19981 which puts the iterator at the beginning of the next line, in
19982 the logical order. */
19983 row->maxpos = it->current.pos;
19984 else if (max_pos == min_pos && it->method != GET_FROM_BUFFER)
19985 /* A line that is entirely from a string/image/stretch... */
19986 row->maxpos = row->minpos;
19987 else
19988 emacs_abort ();
19990 else
19991 row->maxpos = it->current.pos;
19994 /* Construct the glyph row IT->glyph_row in the desired matrix of
19995 IT->w from text at the current position of IT. See dispextern.h
19996 for an overview of struct it. Value is non-zero if
19997 IT->glyph_row displays text, as opposed to a line displaying ZV
19998 only. */
20000 static int
20001 display_line (struct it *it)
20003 struct glyph_row *row = it->glyph_row;
20004 Lisp_Object overlay_arrow_string;
20005 struct it wrap_it;
20006 void *wrap_data = NULL;
20007 int may_wrap = 0, wrap_x IF_LINT (= 0);
20008 int wrap_row_used = -1;
20009 int wrap_row_ascent IF_LINT (= 0), wrap_row_height IF_LINT (= 0);
20010 int wrap_row_phys_ascent IF_LINT (= 0), wrap_row_phys_height IF_LINT (= 0);
20011 int wrap_row_extra_line_spacing IF_LINT (= 0);
20012 ptrdiff_t wrap_row_min_pos IF_LINT (= 0), wrap_row_min_bpos IF_LINT (= 0);
20013 ptrdiff_t wrap_row_max_pos IF_LINT (= 0), wrap_row_max_bpos IF_LINT (= 0);
20014 int cvpos;
20015 ptrdiff_t min_pos = ZV + 1, max_pos = 0;
20016 ptrdiff_t min_bpos IF_LINT (= 0), max_bpos IF_LINT (= 0);
20017 bool pending_handle_line_prefix = false;
20019 /* We always start displaying at hpos zero even if hscrolled. */
20020 eassert (it->hpos == 0 && it->current_x == 0);
20022 if (MATRIX_ROW_VPOS (row, it->w->desired_matrix)
20023 >= it->w->desired_matrix->nrows)
20025 it->w->nrows_scale_factor++;
20026 it->f->fonts_changed = 1;
20027 return 0;
20030 /* Clear the result glyph row and enable it. */
20031 prepare_desired_row (it->w, row, false);
20033 row->y = it->current_y;
20034 row->start = it->start;
20035 row->continuation_lines_width = it->continuation_lines_width;
20036 row->displays_text_p = 1;
20037 row->starts_in_middle_of_char_p = it->starts_in_middle_of_char_p;
20038 it->starts_in_middle_of_char_p = 0;
20040 /* Arrange the overlays nicely for our purposes. Usually, we call
20041 display_line on only one line at a time, in which case this
20042 can't really hurt too much, or we call it on lines which appear
20043 one after another in the buffer, in which case all calls to
20044 recenter_overlay_lists but the first will be pretty cheap. */
20045 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
20047 /* Move over display elements that are not visible because we are
20048 hscrolled. This may stop at an x-position < IT->first_visible_x
20049 if the first glyph is partially visible or if we hit a line end. */
20050 if (it->current_x < it->first_visible_x)
20052 enum move_it_result move_result;
20054 this_line_min_pos = row->start.pos;
20055 move_result = move_it_in_display_line_to (it, ZV, it->first_visible_x,
20056 MOVE_TO_POS | MOVE_TO_X);
20057 /* If we are under a large hscroll, move_it_in_display_line_to
20058 could hit the end of the line without reaching
20059 it->first_visible_x. Pretend that we did reach it. This is
20060 especially important on a TTY, where we will call
20061 extend_face_to_end_of_line, which needs to know how many
20062 blank glyphs to produce. */
20063 if (it->current_x < it->first_visible_x
20064 && (move_result == MOVE_NEWLINE_OR_CR
20065 || move_result == MOVE_POS_MATCH_OR_ZV))
20066 it->current_x = it->first_visible_x;
20068 /* Record the smallest positions seen while we moved over
20069 display elements that are not visible. This is needed by
20070 redisplay_internal for optimizing the case where the cursor
20071 stays inside the same line. The rest of this function only
20072 considers positions that are actually displayed, so
20073 RECORD_MAX_MIN_POS will not otherwise record positions that
20074 are hscrolled to the left of the left edge of the window. */
20075 min_pos = CHARPOS (this_line_min_pos);
20076 min_bpos = BYTEPOS (this_line_min_pos);
20078 else if (it->area == TEXT_AREA)
20080 /* We only do this when not calling move_it_in_display_line_to
20081 above, because that function calls itself handle_line_prefix. */
20082 handle_line_prefix (it);
20084 else
20086 /* Line-prefix and wrap-prefix are always displayed in the text
20087 area. But if this is the first call to display_line after
20088 init_iterator, the iterator might have been set up to write
20089 into a marginal area, e.g. if the line begins with some
20090 display property that writes to the margins. So we need to
20091 wait with the call to handle_line_prefix until whatever
20092 writes to the margin has done its job. */
20093 pending_handle_line_prefix = true;
20096 /* Get the initial row height. This is either the height of the
20097 text hscrolled, if there is any, or zero. */
20098 row->ascent = it->max_ascent;
20099 row->height = it->max_ascent + it->max_descent;
20100 row->phys_ascent = it->max_phys_ascent;
20101 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
20102 row->extra_line_spacing = it->max_extra_line_spacing;
20104 /* Utility macro to record max and min buffer positions seen until now. */
20105 #define RECORD_MAX_MIN_POS(IT) \
20106 do \
20108 int composition_p = !STRINGP ((IT)->string) \
20109 && ((IT)->what == IT_COMPOSITION); \
20110 ptrdiff_t current_pos = \
20111 composition_p ? (IT)->cmp_it.charpos \
20112 : IT_CHARPOS (*(IT)); \
20113 ptrdiff_t current_bpos = \
20114 composition_p ? CHAR_TO_BYTE (current_pos) \
20115 : IT_BYTEPOS (*(IT)); \
20116 if (current_pos < min_pos) \
20118 min_pos = current_pos; \
20119 min_bpos = current_bpos; \
20121 if (IT_CHARPOS (*it) > max_pos) \
20123 max_pos = IT_CHARPOS (*it); \
20124 max_bpos = IT_BYTEPOS (*it); \
20127 while (0)
20129 /* Loop generating characters. The loop is left with IT on the next
20130 character to display. */
20131 while (1)
20133 int n_glyphs_before, hpos_before, x_before;
20134 int x, nglyphs;
20135 int ascent = 0, descent = 0, phys_ascent = 0, phys_descent = 0;
20137 /* Retrieve the next thing to display. Value is zero if end of
20138 buffer reached. */
20139 if (!get_next_display_element (it))
20141 /* Maybe add a space at the end of this line that is used to
20142 display the cursor there under X. Set the charpos of the
20143 first glyph of blank lines not corresponding to any text
20144 to -1. */
20145 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
20146 row->exact_window_width_line_p = 1;
20147 else if ((append_space_for_newline (it, 1) && row->used[TEXT_AREA] == 1)
20148 || row->used[TEXT_AREA] == 0)
20150 row->glyphs[TEXT_AREA]->charpos = -1;
20151 row->displays_text_p = 0;
20153 if (!NILP (BVAR (XBUFFER (it->w->contents), indicate_empty_lines))
20154 && (!MINI_WINDOW_P (it->w)
20155 || (minibuf_level && EQ (it->window, minibuf_window))))
20156 row->indicate_empty_line_p = 1;
20159 it->continuation_lines_width = 0;
20160 row->ends_at_zv_p = 1;
20161 /* A row that displays right-to-left text must always have
20162 its last face extended all the way to the end of line,
20163 even if this row ends in ZV, because we still write to
20164 the screen left to right. We also need to extend the
20165 last face if the default face is remapped to some
20166 different face, otherwise the functions that clear
20167 portions of the screen will clear with the default face's
20168 background color. */
20169 if (row->reversed_p
20170 || lookup_basic_face (it->f, DEFAULT_FACE_ID) != DEFAULT_FACE_ID)
20171 extend_face_to_end_of_line (it);
20172 break;
20175 /* Now, get the metrics of what we want to display. This also
20176 generates glyphs in `row' (which is IT->glyph_row). */
20177 n_glyphs_before = row->used[TEXT_AREA];
20178 x = it->current_x;
20180 /* Remember the line height so far in case the next element doesn't
20181 fit on the line. */
20182 if (it->line_wrap != TRUNCATE)
20184 ascent = it->max_ascent;
20185 descent = it->max_descent;
20186 phys_ascent = it->max_phys_ascent;
20187 phys_descent = it->max_phys_descent;
20189 if (it->line_wrap == WORD_WRAP && it->area == TEXT_AREA)
20191 if (IT_DISPLAYING_WHITESPACE (it))
20192 may_wrap = 1;
20193 else if (may_wrap)
20195 SAVE_IT (wrap_it, *it, wrap_data);
20196 wrap_x = x;
20197 wrap_row_used = row->used[TEXT_AREA];
20198 wrap_row_ascent = row->ascent;
20199 wrap_row_height = row->height;
20200 wrap_row_phys_ascent = row->phys_ascent;
20201 wrap_row_phys_height = row->phys_height;
20202 wrap_row_extra_line_spacing = row->extra_line_spacing;
20203 wrap_row_min_pos = min_pos;
20204 wrap_row_min_bpos = min_bpos;
20205 wrap_row_max_pos = max_pos;
20206 wrap_row_max_bpos = max_bpos;
20207 may_wrap = 0;
20212 PRODUCE_GLYPHS (it);
20214 /* If this display element was in marginal areas, continue with
20215 the next one. */
20216 if (it->area != TEXT_AREA)
20218 row->ascent = max (row->ascent, it->max_ascent);
20219 row->height = max (row->height, it->max_ascent + it->max_descent);
20220 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
20221 row->phys_height = max (row->phys_height,
20222 it->max_phys_ascent + it->max_phys_descent);
20223 row->extra_line_spacing = max (row->extra_line_spacing,
20224 it->max_extra_line_spacing);
20225 set_iterator_to_next (it, 1);
20226 /* If we didn't handle the line/wrap prefix above, and the
20227 call to set_iterator_to_next just switched to TEXT_AREA,
20228 process the prefix now. */
20229 if (it->area == TEXT_AREA && pending_handle_line_prefix)
20231 pending_handle_line_prefix = false;
20232 handle_line_prefix (it);
20234 continue;
20237 /* Does the display element fit on the line? If we truncate
20238 lines, we should draw past the right edge of the window. If
20239 we don't truncate, we want to stop so that we can display the
20240 continuation glyph before the right margin. If lines are
20241 continued, there are two possible strategies for characters
20242 resulting in more than 1 glyph (e.g. tabs): Display as many
20243 glyphs as possible in this line and leave the rest for the
20244 continuation line, or display the whole element in the next
20245 line. Original redisplay did the former, so we do it also. */
20246 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
20247 hpos_before = it->hpos;
20248 x_before = x;
20250 if (/* Not a newline. */
20251 nglyphs > 0
20252 /* Glyphs produced fit entirely in the line. */
20253 && it->current_x < it->last_visible_x)
20255 it->hpos += nglyphs;
20256 row->ascent = max (row->ascent, it->max_ascent);
20257 row->height = max (row->height, it->max_ascent + it->max_descent);
20258 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
20259 row->phys_height = max (row->phys_height,
20260 it->max_phys_ascent + it->max_phys_descent);
20261 row->extra_line_spacing = max (row->extra_line_spacing,
20262 it->max_extra_line_spacing);
20263 if (it->current_x - it->pixel_width < it->first_visible_x
20264 /* In R2L rows, we arrange in extend_face_to_end_of_line
20265 to add a right offset to the line, by a suitable
20266 change to the stretch glyph that is the leftmost
20267 glyph of the line. */
20268 && !row->reversed_p)
20269 row->x = x - it->first_visible_x;
20270 /* Record the maximum and minimum buffer positions seen so
20271 far in glyphs that will be displayed by this row. */
20272 if (it->bidi_p)
20273 RECORD_MAX_MIN_POS (it);
20275 else
20277 int i, new_x;
20278 struct glyph *glyph;
20280 for (i = 0; i < nglyphs; ++i, x = new_x)
20282 /* Identify the glyphs added by the last call to
20283 PRODUCE_GLYPHS. In R2L rows, they are prepended to
20284 the previous glyphs. */
20285 if (!row->reversed_p)
20286 glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
20287 else
20288 glyph = row->glyphs[TEXT_AREA] + nglyphs - 1 - i;
20289 new_x = x + glyph->pixel_width;
20291 if (/* Lines are continued. */
20292 it->line_wrap != TRUNCATE
20293 && (/* Glyph doesn't fit on the line. */
20294 new_x > it->last_visible_x
20295 /* Or it fits exactly on a window system frame. */
20296 || (new_x == it->last_visible_x
20297 && FRAME_WINDOW_P (it->f)
20298 && (row->reversed_p
20299 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
20300 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)))))
20302 /* End of a continued line. */
20304 if (it->hpos == 0
20305 || (new_x == it->last_visible_x
20306 && FRAME_WINDOW_P (it->f)
20307 && (row->reversed_p
20308 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
20309 : WINDOW_RIGHT_FRINGE_WIDTH (it->w))))
20311 /* Current glyph is the only one on the line or
20312 fits exactly on the line. We must continue
20313 the line because we can't draw the cursor
20314 after the glyph. */
20315 row->continued_p = 1;
20316 it->current_x = new_x;
20317 it->continuation_lines_width += new_x;
20318 ++it->hpos;
20319 if (i == nglyphs - 1)
20321 /* If line-wrap is on, check if a previous
20322 wrap point was found. */
20323 if (!IT_OVERFLOW_NEWLINE_INTO_FRINGE (it)
20324 && wrap_row_used > 0
20325 /* Even if there is a previous wrap
20326 point, continue the line here as
20327 usual, if (i) the previous character
20328 was a space or tab AND (ii) the
20329 current character is not. */
20330 && (!may_wrap
20331 || IT_DISPLAYING_WHITESPACE (it)))
20332 goto back_to_wrap;
20334 /* Record the maximum and minimum buffer
20335 positions seen so far in glyphs that will be
20336 displayed by this row. */
20337 if (it->bidi_p)
20338 RECORD_MAX_MIN_POS (it);
20339 set_iterator_to_next (it, 1);
20340 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
20342 if (!get_next_display_element (it))
20344 row->exact_window_width_line_p = 1;
20345 it->continuation_lines_width = 0;
20346 row->continued_p = 0;
20347 row->ends_at_zv_p = 1;
20349 else if (ITERATOR_AT_END_OF_LINE_P (it))
20351 row->continued_p = 0;
20352 row->exact_window_width_line_p = 1;
20354 /* If line-wrap is on, check if a
20355 previous wrap point was found. */
20356 else if (wrap_row_used > 0
20357 /* Even if there is a previous wrap
20358 point, continue the line here as
20359 usual, if (i) the previous character
20360 was a space or tab AND (ii) the
20361 current character is not. */
20362 && (!may_wrap
20363 || IT_DISPLAYING_WHITESPACE (it)))
20364 goto back_to_wrap;
20368 else if (it->bidi_p)
20369 RECORD_MAX_MIN_POS (it);
20370 if (WINDOW_LEFT_MARGIN_WIDTH (it->w) > 0
20371 || WINDOW_RIGHT_MARGIN_WIDTH (it->w) > 0)
20372 extend_face_to_end_of_line (it);
20374 else if (CHAR_GLYPH_PADDING_P (*glyph)
20375 && !FRAME_WINDOW_P (it->f))
20377 /* A padding glyph that doesn't fit on this line.
20378 This means the whole character doesn't fit
20379 on the line. */
20380 if (row->reversed_p)
20381 unproduce_glyphs (it, row->used[TEXT_AREA]
20382 - n_glyphs_before);
20383 row->used[TEXT_AREA] = n_glyphs_before;
20385 /* Fill the rest of the row with continuation
20386 glyphs like in 20.x. */
20387 while (row->glyphs[TEXT_AREA] + row->used[TEXT_AREA]
20388 < row->glyphs[1 + TEXT_AREA])
20389 produce_special_glyphs (it, IT_CONTINUATION);
20391 row->continued_p = 1;
20392 it->current_x = x_before;
20393 it->continuation_lines_width += x_before;
20395 /* Restore the height to what it was before the
20396 element not fitting on the line. */
20397 it->max_ascent = ascent;
20398 it->max_descent = descent;
20399 it->max_phys_ascent = phys_ascent;
20400 it->max_phys_descent = phys_descent;
20401 if (WINDOW_LEFT_MARGIN_WIDTH (it->w) > 0
20402 || WINDOW_RIGHT_MARGIN_WIDTH (it->w) > 0)
20403 extend_face_to_end_of_line (it);
20405 else if (wrap_row_used > 0)
20407 back_to_wrap:
20408 if (row->reversed_p)
20409 unproduce_glyphs (it,
20410 row->used[TEXT_AREA] - wrap_row_used);
20411 RESTORE_IT (it, &wrap_it, wrap_data);
20412 it->continuation_lines_width += wrap_x;
20413 row->used[TEXT_AREA] = wrap_row_used;
20414 row->ascent = wrap_row_ascent;
20415 row->height = wrap_row_height;
20416 row->phys_ascent = wrap_row_phys_ascent;
20417 row->phys_height = wrap_row_phys_height;
20418 row->extra_line_spacing = wrap_row_extra_line_spacing;
20419 min_pos = wrap_row_min_pos;
20420 min_bpos = wrap_row_min_bpos;
20421 max_pos = wrap_row_max_pos;
20422 max_bpos = wrap_row_max_bpos;
20423 row->continued_p = 1;
20424 row->ends_at_zv_p = 0;
20425 row->exact_window_width_line_p = 0;
20426 it->continuation_lines_width += x;
20428 /* Make sure that a non-default face is extended
20429 up to the right margin of the window. */
20430 extend_face_to_end_of_line (it);
20432 else if (it->c == '\t' && FRAME_WINDOW_P (it->f))
20434 /* A TAB that extends past the right edge of the
20435 window. This produces a single glyph on
20436 window system frames. We leave the glyph in
20437 this row and let it fill the row, but don't
20438 consume the TAB. */
20439 if ((row->reversed_p
20440 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
20441 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
20442 produce_special_glyphs (it, IT_CONTINUATION);
20443 it->continuation_lines_width += it->last_visible_x;
20444 row->ends_in_middle_of_char_p = 1;
20445 row->continued_p = 1;
20446 glyph->pixel_width = it->last_visible_x - x;
20447 it->starts_in_middle_of_char_p = 1;
20448 if (WINDOW_LEFT_MARGIN_WIDTH (it->w) > 0
20449 || WINDOW_RIGHT_MARGIN_WIDTH (it->w) > 0)
20450 extend_face_to_end_of_line (it);
20452 else
20454 /* Something other than a TAB that draws past
20455 the right edge of the window. Restore
20456 positions to values before the element. */
20457 if (row->reversed_p)
20458 unproduce_glyphs (it, row->used[TEXT_AREA]
20459 - (n_glyphs_before + i));
20460 row->used[TEXT_AREA] = n_glyphs_before + i;
20462 /* Display continuation glyphs. */
20463 it->current_x = x_before;
20464 it->continuation_lines_width += x;
20465 if (!FRAME_WINDOW_P (it->f)
20466 || (row->reversed_p
20467 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
20468 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
20469 produce_special_glyphs (it, IT_CONTINUATION);
20470 row->continued_p = 1;
20472 extend_face_to_end_of_line (it);
20474 if (nglyphs > 1 && i > 0)
20476 row->ends_in_middle_of_char_p = 1;
20477 it->starts_in_middle_of_char_p = 1;
20480 /* Restore the height to what it was before the
20481 element not fitting on the line. */
20482 it->max_ascent = ascent;
20483 it->max_descent = descent;
20484 it->max_phys_ascent = phys_ascent;
20485 it->max_phys_descent = phys_descent;
20488 break;
20490 else if (new_x > it->first_visible_x)
20492 /* Increment number of glyphs actually displayed. */
20493 ++it->hpos;
20495 /* Record the maximum and minimum buffer positions
20496 seen so far in glyphs that will be displayed by
20497 this row. */
20498 if (it->bidi_p)
20499 RECORD_MAX_MIN_POS (it);
20501 if (x < it->first_visible_x && !row->reversed_p)
20502 /* Glyph is partially visible, i.e. row starts at
20503 negative X position. Don't do that in R2L
20504 rows, where we arrange to add a right offset to
20505 the line in extend_face_to_end_of_line, by a
20506 suitable change to the stretch glyph that is
20507 the leftmost glyph of the line. */
20508 row->x = x - it->first_visible_x;
20509 /* When the last glyph of an R2L row only fits
20510 partially on the line, we need to set row->x to a
20511 negative offset, so that the leftmost glyph is
20512 the one that is partially visible. But if we are
20513 going to produce the truncation glyph, this will
20514 be taken care of in produce_special_glyphs. */
20515 if (row->reversed_p
20516 && new_x > it->last_visible_x
20517 && !(it->line_wrap == TRUNCATE
20518 && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0))
20520 eassert (FRAME_WINDOW_P (it->f));
20521 row->x = it->last_visible_x - new_x;
20524 else
20526 /* Glyph is completely off the left margin of the
20527 window. This should not happen because of the
20528 move_it_in_display_line at the start of this
20529 function, unless the text display area of the
20530 window is empty. */
20531 eassert (it->first_visible_x <= it->last_visible_x);
20534 /* Even if this display element produced no glyphs at all,
20535 we want to record its position. */
20536 if (it->bidi_p && nglyphs == 0)
20537 RECORD_MAX_MIN_POS (it);
20539 row->ascent = max (row->ascent, it->max_ascent);
20540 row->height = max (row->height, it->max_ascent + it->max_descent);
20541 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
20542 row->phys_height = max (row->phys_height,
20543 it->max_phys_ascent + it->max_phys_descent);
20544 row->extra_line_spacing = max (row->extra_line_spacing,
20545 it->max_extra_line_spacing);
20547 /* End of this display line if row is continued. */
20548 if (row->continued_p || row->ends_at_zv_p)
20549 break;
20552 at_end_of_line:
20553 /* Is this a line end? If yes, we're also done, after making
20554 sure that a non-default face is extended up to the right
20555 margin of the window. */
20556 if (ITERATOR_AT_END_OF_LINE_P (it))
20558 int used_before = row->used[TEXT_AREA];
20560 row->ends_in_newline_from_string_p = STRINGP (it->object);
20562 /* Add a space at the end of the line that is used to
20563 display the cursor there. */
20564 if (!IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
20565 append_space_for_newline (it, 0);
20567 /* Extend the face to the end of the line. */
20568 extend_face_to_end_of_line (it);
20570 /* Make sure we have the position. */
20571 if (used_before == 0)
20572 row->glyphs[TEXT_AREA]->charpos = CHARPOS (it->position);
20574 /* Record the position of the newline, for use in
20575 find_row_edges. */
20576 it->eol_pos = it->current.pos;
20578 /* Consume the line end. This skips over invisible lines. */
20579 set_iterator_to_next (it, 1);
20580 it->continuation_lines_width = 0;
20581 break;
20584 /* Proceed with next display element. Note that this skips
20585 over lines invisible because of selective display. */
20586 set_iterator_to_next (it, 1);
20588 /* If we truncate lines, we are done when the last displayed
20589 glyphs reach past the right margin of the window. */
20590 if (it->line_wrap == TRUNCATE
20591 && ((FRAME_WINDOW_P (it->f)
20592 /* Images are preprocessed in produce_image_glyph such
20593 that they are cropped at the right edge of the
20594 window, so an image glyph will always end exactly at
20595 last_visible_x, even if there's no right fringe. */
20596 && ((row->reversed_p
20597 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
20598 : WINDOW_RIGHT_FRINGE_WIDTH (it->w))
20599 || it->what == IT_IMAGE))
20600 ? (it->current_x >= it->last_visible_x)
20601 : (it->current_x > it->last_visible_x)))
20603 /* Maybe add truncation glyphs. */
20604 if (!FRAME_WINDOW_P (it->f)
20605 || (row->reversed_p
20606 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
20607 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
20609 int i, n;
20611 if (!row->reversed_p)
20613 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
20614 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
20615 break;
20617 else
20619 for (i = 0; i < row->used[TEXT_AREA]; i++)
20620 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
20621 break;
20622 /* Remove any padding glyphs at the front of ROW, to
20623 make room for the truncation glyphs we will be
20624 adding below. The loop below always inserts at
20625 least one truncation glyph, so also remove the
20626 last glyph added to ROW. */
20627 unproduce_glyphs (it, i + 1);
20628 /* Adjust i for the loop below. */
20629 i = row->used[TEXT_AREA] - (i + 1);
20632 /* produce_special_glyphs overwrites the last glyph, so
20633 we don't want that if we want to keep that last
20634 glyph, which means it's an image. */
20635 if (it->current_x > it->last_visible_x)
20637 it->current_x = x_before;
20638 if (!FRAME_WINDOW_P (it->f))
20640 for (n = row->used[TEXT_AREA]; i < n; ++i)
20642 row->used[TEXT_AREA] = i;
20643 produce_special_glyphs (it, IT_TRUNCATION);
20646 else
20648 row->used[TEXT_AREA] = i;
20649 produce_special_glyphs (it, IT_TRUNCATION);
20651 it->hpos = hpos_before;
20654 else if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
20656 /* Don't truncate if we can overflow newline into fringe. */
20657 if (!get_next_display_element (it))
20659 it->continuation_lines_width = 0;
20660 row->ends_at_zv_p = 1;
20661 row->exact_window_width_line_p = 1;
20662 break;
20664 if (ITERATOR_AT_END_OF_LINE_P (it))
20666 row->exact_window_width_line_p = 1;
20667 goto at_end_of_line;
20669 it->current_x = x_before;
20670 it->hpos = hpos_before;
20673 row->truncated_on_right_p = 1;
20674 it->continuation_lines_width = 0;
20675 reseat_at_next_visible_line_start (it, 0);
20676 /* We insist below that IT's position be at ZV because in
20677 bidi-reordered lines the character at visible line start
20678 might not be the character that follows the newline in
20679 the logical order. */
20680 if (IT_BYTEPOS (*it) > BEG_BYTE)
20681 row->ends_at_zv_p =
20682 IT_BYTEPOS (*it) >= ZV_BYTE && FETCH_BYTE (ZV_BYTE - 1) != '\n';
20683 else
20684 row->ends_at_zv_p = false;
20685 break;
20689 if (wrap_data)
20690 bidi_unshelve_cache (wrap_data, 1);
20692 /* If line is not empty and hscrolled, maybe insert truncation glyphs
20693 at the left window margin. */
20694 if (it->first_visible_x
20695 && IT_CHARPOS (*it) != CHARPOS (row->start.pos))
20697 if (!FRAME_WINDOW_P (it->f)
20698 || (((row->reversed_p
20699 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
20700 : WINDOW_LEFT_FRINGE_WIDTH (it->w)) == 0)
20701 /* Don't let insert_left_trunc_glyphs overwrite the
20702 first glyph of the row if it is an image. */
20703 && row->glyphs[TEXT_AREA]->type != IMAGE_GLYPH))
20704 insert_left_trunc_glyphs (it);
20705 row->truncated_on_left_p = 1;
20708 /* Remember the position at which this line ends.
20710 BIDI Note: any code that needs MATRIX_ROW_START/END_CHARPOS
20711 cannot be before the call to find_row_edges below, since that is
20712 where these positions are determined. */
20713 row->end = it->current;
20714 if (!it->bidi_p)
20716 row->minpos = row->start.pos;
20717 row->maxpos = row->end.pos;
20719 else
20721 /* ROW->minpos and ROW->maxpos must be the smallest and
20722 `1 + the largest' buffer positions in ROW. But if ROW was
20723 bidi-reordered, these two positions can be anywhere in the
20724 row, so we must determine them now. */
20725 find_row_edges (it, row, min_pos, min_bpos, max_pos, max_bpos);
20728 /* If the start of this line is the overlay arrow-position, then
20729 mark this glyph row as the one containing the overlay arrow.
20730 This is clearly a mess with variable size fonts. It would be
20731 better to let it be displayed like cursors under X. */
20732 if ((MATRIX_ROW_DISPLAYS_TEXT_P (row) || !overlay_arrow_seen)
20733 && (overlay_arrow_string = overlay_arrow_at_row (it, row),
20734 !NILP (overlay_arrow_string)))
20736 /* Overlay arrow in window redisplay is a fringe bitmap. */
20737 if (STRINGP (overlay_arrow_string))
20739 struct glyph_row *arrow_row
20740 = get_overlay_arrow_glyph_row (it->w, overlay_arrow_string);
20741 struct glyph *glyph = arrow_row->glyphs[TEXT_AREA];
20742 struct glyph *arrow_end = glyph + arrow_row->used[TEXT_AREA];
20743 struct glyph *p = row->glyphs[TEXT_AREA];
20744 struct glyph *p2, *end;
20746 /* Copy the arrow glyphs. */
20747 while (glyph < arrow_end)
20748 *p++ = *glyph++;
20750 /* Throw away padding glyphs. */
20751 p2 = p;
20752 end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
20753 while (p2 < end && CHAR_GLYPH_PADDING_P (*p2))
20754 ++p2;
20755 if (p2 > p)
20757 while (p2 < end)
20758 *p++ = *p2++;
20759 row->used[TEXT_AREA] = p2 - row->glyphs[TEXT_AREA];
20762 else
20764 eassert (INTEGERP (overlay_arrow_string));
20765 row->overlay_arrow_bitmap = XINT (overlay_arrow_string);
20767 overlay_arrow_seen = 1;
20770 /* Highlight trailing whitespace. */
20771 if (!NILP (Vshow_trailing_whitespace))
20772 highlight_trailing_whitespace (it->f, it->glyph_row);
20774 /* Compute pixel dimensions of this line. */
20775 compute_line_metrics (it);
20777 /* Implementation note: No changes in the glyphs of ROW or in their
20778 faces can be done past this point, because compute_line_metrics
20779 computes ROW's hash value and stores it within the glyph_row
20780 structure. */
20782 /* Record whether this row ends inside an ellipsis. */
20783 row->ends_in_ellipsis_p
20784 = (it->method == GET_FROM_DISPLAY_VECTOR
20785 && it->ellipsis_p);
20787 /* Save fringe bitmaps in this row. */
20788 row->left_user_fringe_bitmap = it->left_user_fringe_bitmap;
20789 row->left_user_fringe_face_id = it->left_user_fringe_face_id;
20790 row->right_user_fringe_bitmap = it->right_user_fringe_bitmap;
20791 row->right_user_fringe_face_id = it->right_user_fringe_face_id;
20793 it->left_user_fringe_bitmap = 0;
20794 it->left_user_fringe_face_id = 0;
20795 it->right_user_fringe_bitmap = 0;
20796 it->right_user_fringe_face_id = 0;
20798 /* Maybe set the cursor. */
20799 cvpos = it->w->cursor.vpos;
20800 if ((cvpos < 0
20801 /* In bidi-reordered rows, keep checking for proper cursor
20802 position even if one has been found already, because buffer
20803 positions in such rows change non-linearly with ROW->VPOS,
20804 when a line is continued. One exception: when we are at ZV,
20805 display cursor on the first suitable glyph row, since all
20806 the empty rows after that also have their position set to ZV. */
20807 /* FIXME: Revisit this when glyph ``spilling'' in continuation
20808 lines' rows is implemented for bidi-reordered rows. */
20809 || (it->bidi_p
20810 && !MATRIX_ROW (it->w->desired_matrix, cvpos)->ends_at_zv_p))
20811 && PT >= MATRIX_ROW_START_CHARPOS (row)
20812 && PT <= MATRIX_ROW_END_CHARPOS (row)
20813 && cursor_row_p (row))
20814 set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
20816 /* Prepare for the next line. This line starts horizontally at (X
20817 HPOS) = (0 0). Vertical positions are incremented. As a
20818 convenience for the caller, IT->glyph_row is set to the next
20819 row to be used. */
20820 it->current_x = it->hpos = 0;
20821 it->current_y += row->height;
20822 SET_TEXT_POS (it->eol_pos, 0, 0);
20823 ++it->vpos;
20824 ++it->glyph_row;
20825 /* The next row should by default use the same value of the
20826 reversed_p flag as this one. set_iterator_to_next decides when
20827 it's a new paragraph, and PRODUCE_GLYPHS recomputes the value of
20828 the flag accordingly. */
20829 if (it->glyph_row < MATRIX_BOTTOM_TEXT_ROW (it->w->desired_matrix, it->w))
20830 it->glyph_row->reversed_p = row->reversed_p;
20831 it->start = row->end;
20832 return MATRIX_ROW_DISPLAYS_TEXT_P (row);
20834 #undef RECORD_MAX_MIN_POS
20837 DEFUN ("current-bidi-paragraph-direction", Fcurrent_bidi_paragraph_direction,
20838 Scurrent_bidi_paragraph_direction, 0, 1, 0,
20839 doc: /* Return paragraph direction at point in BUFFER.
20840 Value is either `left-to-right' or `right-to-left'.
20841 If BUFFER is omitted or nil, it defaults to the current buffer.
20843 Paragraph direction determines how the text in the paragraph is displayed.
20844 In left-to-right paragraphs, text begins at the left margin of the window
20845 and the reading direction is generally left to right. In right-to-left
20846 paragraphs, text begins at the right margin and is read from right to left.
20848 See also `bidi-paragraph-direction'. */)
20849 (Lisp_Object buffer)
20851 struct buffer *buf = current_buffer;
20852 struct buffer *old = buf;
20854 if (! NILP (buffer))
20856 CHECK_BUFFER (buffer);
20857 buf = XBUFFER (buffer);
20860 if (NILP (BVAR (buf, bidi_display_reordering))
20861 || NILP (BVAR (buf, enable_multibyte_characters))
20862 /* When we are loading loadup.el, the character property tables
20863 needed for bidi iteration are not yet available. */
20864 || !NILP (Vpurify_flag))
20865 return Qleft_to_right;
20866 else if (!NILP (BVAR (buf, bidi_paragraph_direction)))
20867 return BVAR (buf, bidi_paragraph_direction);
20868 else
20870 /* Determine the direction from buffer text. We could try to
20871 use current_matrix if it is up to date, but this seems fast
20872 enough as it is. */
20873 struct bidi_it itb;
20874 ptrdiff_t pos = BUF_PT (buf);
20875 ptrdiff_t bytepos = BUF_PT_BYTE (buf);
20876 int c;
20877 void *itb_data = bidi_shelve_cache ();
20879 set_buffer_temp (buf);
20880 /* bidi_paragraph_init finds the base direction of the paragraph
20881 by searching forward from paragraph start. We need the base
20882 direction of the current or _previous_ paragraph, so we need
20883 to make sure we are within that paragraph. To that end, find
20884 the previous non-empty line. */
20885 if (pos >= ZV && pos > BEGV)
20886 DEC_BOTH (pos, bytepos);
20887 AUTO_STRING (trailing_white_space, "[\f\t ]*\n");
20888 if (fast_looking_at (trailing_white_space,
20889 pos, bytepos, ZV, ZV_BYTE, Qnil) > 0)
20891 while ((c = FETCH_BYTE (bytepos)) == '\n'
20892 || c == ' ' || c == '\t' || c == '\f')
20894 if (bytepos <= BEGV_BYTE)
20895 break;
20896 bytepos--;
20897 pos--;
20899 while (!CHAR_HEAD_P (FETCH_BYTE (bytepos)))
20900 bytepos--;
20902 bidi_init_it (pos, bytepos, FRAME_WINDOW_P (SELECTED_FRAME ()), &itb);
20903 itb.paragraph_dir = NEUTRAL_DIR;
20904 itb.string.s = NULL;
20905 itb.string.lstring = Qnil;
20906 itb.string.bufpos = 0;
20907 itb.string.from_disp_str = 0;
20908 itb.string.unibyte = 0;
20909 /* We have no window to use here for ignoring window-specific
20910 overlays. Using NULL for window pointer will cause
20911 compute_display_string_pos to use the current buffer. */
20912 itb.w = NULL;
20913 bidi_paragraph_init (NEUTRAL_DIR, &itb, 1);
20914 bidi_unshelve_cache (itb_data, 0);
20915 set_buffer_temp (old);
20916 switch (itb.paragraph_dir)
20918 case L2R:
20919 return Qleft_to_right;
20920 break;
20921 case R2L:
20922 return Qright_to_left;
20923 break;
20924 default:
20925 emacs_abort ();
20930 DEFUN ("bidi-find-overridden-directionality",
20931 Fbidi_find_overridden_directionality,
20932 Sbidi_find_overridden_directionality, 2, 3, 0,
20933 doc: /* Return position between FROM and TO where directionality was overridden.
20935 This function returns the first character position in the specified
20936 region of OBJECT where there is a character whose `bidi-class' property
20937 is `L', but which was forced to display as `R' by a directional
20938 override, and likewise with characters whose `bidi-class' is `R'
20939 or `AL' that were forced to display as `L'.
20941 If no such character is found, the function returns nil.
20943 OBJECT is a Lisp string or buffer to search for overridden
20944 directionality, and defaults to the current buffer if nil or omitted.
20945 OBJECT can also be a window, in which case the function will search
20946 the buffer displayed in that window. Passing the window instead of
20947 a buffer is preferable when the buffer is displayed in some window,
20948 because this function will then be able to correctly account for
20949 window-specific overlays, which can affect the results.
20951 Strong directional characters `L', `R', and `AL' can have their
20952 intrinsic directionality overridden by directional override
20953 control characters RLO \(u+202e) and LRO \(u+202d). See the
20954 function `get-char-code-property' for a way to inquire about
20955 the `bidi-class' property of a character. */)
20956 (Lisp_Object from, Lisp_Object to, Lisp_Object object)
20958 struct buffer *buf = current_buffer;
20959 struct buffer *old = buf;
20960 struct window *w = NULL;
20961 bool frame_window_p = FRAME_WINDOW_P (SELECTED_FRAME ());
20962 struct bidi_it itb;
20963 ptrdiff_t from_pos, to_pos, from_bpos;
20964 void *itb_data;
20966 if (!NILP (object))
20968 if (BUFFERP (object))
20969 buf = XBUFFER (object);
20970 else if (WINDOWP (object))
20972 w = decode_live_window (object);
20973 buf = XBUFFER (w->contents);
20974 frame_window_p = FRAME_WINDOW_P (XFRAME (w->frame));
20976 else
20977 CHECK_STRING (object);
20980 if (STRINGP (object))
20982 /* Characters in unibyte strings are always treated by bidi.c as
20983 strong LTR. */
20984 if (!STRING_MULTIBYTE (object)
20985 /* When we are loading loadup.el, the character property
20986 tables needed for bidi iteration are not yet
20987 available. */
20988 || !NILP (Vpurify_flag))
20989 return Qnil;
20991 validate_subarray (object, from, to, SCHARS (object), &from_pos, &to_pos);
20992 if (from_pos >= SCHARS (object))
20993 return Qnil;
20995 /* Set up the bidi iterator. */
20996 itb_data = bidi_shelve_cache ();
20997 itb.paragraph_dir = NEUTRAL_DIR;
20998 itb.string.lstring = object;
20999 itb.string.s = NULL;
21000 itb.string.schars = SCHARS (object);
21001 itb.string.bufpos = 0;
21002 itb.string.from_disp_str = 0;
21003 itb.string.unibyte = 0;
21004 itb.w = w;
21005 bidi_init_it (0, 0, frame_window_p, &itb);
21007 else
21009 /* Nothing this fancy can happen in unibyte buffers, or in a
21010 buffer that disabled reordering, or if FROM is at EOB. */
21011 if (NILP (BVAR (buf, bidi_display_reordering))
21012 || NILP (BVAR (buf, enable_multibyte_characters))
21013 /* When we are loading loadup.el, the character property
21014 tables needed for bidi iteration are not yet
21015 available. */
21016 || !NILP (Vpurify_flag))
21017 return Qnil;
21019 set_buffer_temp (buf);
21020 validate_region (&from, &to);
21021 from_pos = XINT (from);
21022 to_pos = XINT (to);
21023 if (from_pos >= ZV)
21024 return Qnil;
21026 /* Set up the bidi iterator. */
21027 itb_data = bidi_shelve_cache ();
21028 from_bpos = CHAR_TO_BYTE (from_pos);
21029 if (from_pos == BEGV)
21031 itb.charpos = BEGV;
21032 itb.bytepos = BEGV_BYTE;
21034 else if (FETCH_CHAR (from_bpos - 1) == '\n')
21036 itb.charpos = from_pos;
21037 itb.bytepos = from_bpos;
21039 else
21040 itb.charpos = find_newline_no_quit (from_pos, CHAR_TO_BYTE (from_pos),
21041 -1, &itb.bytepos);
21042 itb.paragraph_dir = NEUTRAL_DIR;
21043 itb.string.s = NULL;
21044 itb.string.lstring = Qnil;
21045 itb.string.bufpos = 0;
21046 itb.string.from_disp_str = 0;
21047 itb.string.unibyte = 0;
21048 itb.w = w;
21049 bidi_init_it (itb.charpos, itb.bytepos, frame_window_p, &itb);
21052 ptrdiff_t found;
21053 do {
21054 /* For the purposes of this function, the actual base direction of
21055 the paragraph doesn't matter, so just set it to L2R. */
21056 bidi_paragraph_init (L2R, &itb, 0);
21057 while ((found = bidi_find_first_overridden (&itb)) < from_pos)
21059 } while (found == ZV && itb.ch == '\n' && itb.charpos < to_pos);
21061 bidi_unshelve_cache (itb_data, 0);
21062 set_buffer_temp (old);
21064 return (from_pos <= found && found < to_pos) ? make_number (found) : Qnil;
21067 DEFUN ("move-point-visually", Fmove_point_visually,
21068 Smove_point_visually, 1, 1, 0,
21069 doc: /* Move point in the visual order in the specified DIRECTION.
21070 DIRECTION can be 1, meaning move to the right, or -1, which moves to the
21071 left.
21073 Value is the new character position of point. */)
21074 (Lisp_Object direction)
21076 struct window *w = XWINDOW (selected_window);
21077 struct buffer *b = XBUFFER (w->contents);
21078 struct glyph_row *row;
21079 int dir;
21080 Lisp_Object paragraph_dir;
21082 #define ROW_GLYPH_NEWLINE_P(ROW,GLYPH) \
21083 (!(ROW)->continued_p \
21084 && NILP ((GLYPH)->object) \
21085 && (GLYPH)->type == CHAR_GLYPH \
21086 && (GLYPH)->u.ch == ' ' \
21087 && (GLYPH)->charpos >= 0 \
21088 && !(GLYPH)->avoid_cursor_p)
21090 CHECK_NUMBER (direction);
21091 dir = XINT (direction);
21092 if (dir > 0)
21093 dir = 1;
21094 else
21095 dir = -1;
21097 /* If current matrix is up-to-date, we can use the information
21098 recorded in the glyphs, at least as long as the goal is on the
21099 screen. */
21100 if (w->window_end_valid
21101 && !windows_or_buffers_changed
21102 && b
21103 && !b->clip_changed
21104 && !b->prevent_redisplay_optimizations_p
21105 && !window_outdated (w)
21106 /* We rely below on the cursor coordinates to be up to date, but
21107 we cannot trust them if some command moved point since the
21108 last complete redisplay. */
21109 && w->last_point == BUF_PT (b)
21110 && w->cursor.vpos >= 0
21111 && w->cursor.vpos < w->current_matrix->nrows
21112 && (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos))->enabled_p)
21114 struct glyph *g = row->glyphs[TEXT_AREA];
21115 struct glyph *e = dir > 0 ? g + row->used[TEXT_AREA] : g - 1;
21116 struct glyph *gpt = g + w->cursor.hpos;
21118 for (g = gpt + dir; (dir > 0 ? g < e : g > e); g += dir)
21120 if (BUFFERP (g->object) && g->charpos != PT)
21122 SET_PT (g->charpos);
21123 w->cursor.vpos = -1;
21124 return make_number (PT);
21126 else if (!NILP (g->object) && !EQ (g->object, gpt->object))
21128 ptrdiff_t new_pos;
21130 if (BUFFERP (gpt->object))
21132 new_pos = PT;
21133 if ((gpt->resolved_level - row->reversed_p) % 2 == 0)
21134 new_pos += (row->reversed_p ? -dir : dir);
21135 else
21136 new_pos -= (row->reversed_p ? -dir : dir);
21138 else if (BUFFERP (g->object))
21139 new_pos = g->charpos;
21140 else
21141 break;
21142 SET_PT (new_pos);
21143 w->cursor.vpos = -1;
21144 return make_number (PT);
21146 else if (ROW_GLYPH_NEWLINE_P (row, g))
21148 /* Glyphs inserted at the end of a non-empty line for
21149 positioning the cursor have zero charpos, so we must
21150 deduce the value of point by other means. */
21151 if (g->charpos > 0)
21152 SET_PT (g->charpos);
21153 else if (row->ends_at_zv_p && PT != ZV)
21154 SET_PT (ZV);
21155 else if (PT != MATRIX_ROW_END_CHARPOS (row) - 1)
21156 SET_PT (MATRIX_ROW_END_CHARPOS (row) - 1);
21157 else
21158 break;
21159 w->cursor.vpos = -1;
21160 return make_number (PT);
21163 if (g == e || NILP (g->object))
21165 if (row->truncated_on_left_p || row->truncated_on_right_p)
21166 goto simulate_display;
21167 if (!row->reversed_p)
21168 row += dir;
21169 else
21170 row -= dir;
21171 if (row < MATRIX_FIRST_TEXT_ROW (w->current_matrix)
21172 || row > MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w))
21173 goto simulate_display;
21175 if (dir > 0)
21177 if (row->reversed_p && !row->continued_p)
21179 SET_PT (MATRIX_ROW_END_CHARPOS (row) - 1);
21180 w->cursor.vpos = -1;
21181 return make_number (PT);
21183 g = row->glyphs[TEXT_AREA];
21184 e = g + row->used[TEXT_AREA];
21185 for ( ; g < e; g++)
21187 if (BUFFERP (g->object)
21188 /* Empty lines have only one glyph, which stands
21189 for the newline, and whose charpos is the
21190 buffer position of the newline. */
21191 || ROW_GLYPH_NEWLINE_P (row, g)
21192 /* When the buffer ends in a newline, the line at
21193 EOB also has one glyph, but its charpos is -1. */
21194 || (row->ends_at_zv_p
21195 && !row->reversed_p
21196 && NILP (g->object)
21197 && g->type == CHAR_GLYPH
21198 && g->u.ch == ' '))
21200 if (g->charpos > 0)
21201 SET_PT (g->charpos);
21202 else if (!row->reversed_p
21203 && row->ends_at_zv_p
21204 && PT != ZV)
21205 SET_PT (ZV);
21206 else
21207 continue;
21208 w->cursor.vpos = -1;
21209 return make_number (PT);
21213 else
21215 if (!row->reversed_p && !row->continued_p)
21217 SET_PT (MATRIX_ROW_END_CHARPOS (row) - 1);
21218 w->cursor.vpos = -1;
21219 return make_number (PT);
21221 e = row->glyphs[TEXT_AREA];
21222 g = e + row->used[TEXT_AREA] - 1;
21223 for ( ; g >= e; g--)
21225 if (BUFFERP (g->object)
21226 || (ROW_GLYPH_NEWLINE_P (row, g)
21227 && g->charpos > 0)
21228 /* Empty R2L lines on GUI frames have the buffer
21229 position of the newline stored in the stretch
21230 glyph. */
21231 || g->type == STRETCH_GLYPH
21232 || (row->ends_at_zv_p
21233 && row->reversed_p
21234 && NILP (g->object)
21235 && g->type == CHAR_GLYPH
21236 && g->u.ch == ' '))
21238 if (g->charpos > 0)
21239 SET_PT (g->charpos);
21240 else if (row->reversed_p
21241 && row->ends_at_zv_p
21242 && PT != ZV)
21243 SET_PT (ZV);
21244 else
21245 continue;
21246 w->cursor.vpos = -1;
21247 return make_number (PT);
21254 simulate_display:
21256 /* If we wind up here, we failed to move by using the glyphs, so we
21257 need to simulate display instead. */
21259 if (b)
21260 paragraph_dir = Fcurrent_bidi_paragraph_direction (w->contents);
21261 else
21262 paragraph_dir = Qleft_to_right;
21263 if (EQ (paragraph_dir, Qright_to_left))
21264 dir = -dir;
21265 if (PT <= BEGV && dir < 0)
21266 xsignal0 (Qbeginning_of_buffer);
21267 else if (PT >= ZV && dir > 0)
21268 xsignal0 (Qend_of_buffer);
21269 else
21271 struct text_pos pt;
21272 struct it it;
21273 int pt_x, target_x, pixel_width, pt_vpos;
21274 bool at_eol_p;
21275 bool overshoot_expected = false;
21276 bool target_is_eol_p = false;
21278 /* Setup the arena. */
21279 SET_TEXT_POS (pt, PT, PT_BYTE);
21280 start_display (&it, w, pt);
21282 if (it.cmp_it.id < 0
21283 && it.method == GET_FROM_STRING
21284 && it.area == TEXT_AREA
21285 && it.string_from_display_prop_p
21286 && (it.sp > 0 && it.stack[it.sp - 1].method == GET_FROM_BUFFER))
21287 overshoot_expected = true;
21289 /* Find the X coordinate of point. We start from the beginning
21290 of this or previous line to make sure we are before point in
21291 the logical order (since the move_it_* functions can only
21292 move forward). */
21293 reseat:
21294 reseat_at_previous_visible_line_start (&it);
21295 it.current_x = it.hpos = it.current_y = it.vpos = 0;
21296 if (IT_CHARPOS (it) != PT)
21298 move_it_to (&it, overshoot_expected ? PT - 1 : PT,
21299 -1, -1, -1, MOVE_TO_POS);
21300 /* If we missed point because the character there is
21301 displayed out of a display vector that has more than one
21302 glyph, retry expecting overshoot. */
21303 if (it.method == GET_FROM_DISPLAY_VECTOR
21304 && it.current.dpvec_index > 0
21305 && !overshoot_expected)
21307 overshoot_expected = true;
21308 goto reseat;
21310 else if (IT_CHARPOS (it) != PT && !overshoot_expected)
21311 move_it_in_display_line (&it, PT, -1, MOVE_TO_POS);
21313 pt_x = it.current_x;
21314 pt_vpos = it.vpos;
21315 if (dir > 0 || overshoot_expected)
21317 struct glyph_row *row = it.glyph_row;
21319 /* When point is at beginning of line, we don't have
21320 information about the glyph there loaded into struct
21321 it. Calling get_next_display_element fixes that. */
21322 if (pt_x == 0)
21323 get_next_display_element (&it);
21324 at_eol_p = ITERATOR_AT_END_OF_LINE_P (&it);
21325 it.glyph_row = NULL;
21326 PRODUCE_GLYPHS (&it); /* compute it.pixel_width */
21327 it.glyph_row = row;
21328 /* PRODUCE_GLYPHS advances it.current_x, so we must restore
21329 it, lest it will become out of sync with it's buffer
21330 position. */
21331 it.current_x = pt_x;
21333 else
21334 at_eol_p = ITERATOR_AT_END_OF_LINE_P (&it);
21335 pixel_width = it.pixel_width;
21336 if (overshoot_expected && at_eol_p)
21337 pixel_width = 0;
21338 else if (pixel_width <= 0)
21339 pixel_width = 1;
21341 /* If there's a display string (or something similar) at point,
21342 we are actually at the glyph to the left of point, so we need
21343 to correct the X coordinate. */
21344 if (overshoot_expected)
21346 if (it.bidi_p)
21347 pt_x += pixel_width * it.bidi_it.scan_dir;
21348 else
21349 pt_x += pixel_width;
21352 /* Compute target X coordinate, either to the left or to the
21353 right of point. On TTY frames, all characters have the same
21354 pixel width of 1, so we can use that. On GUI frames we don't
21355 have an easy way of getting at the pixel width of the
21356 character to the left of point, so we use a different method
21357 of getting to that place. */
21358 if (dir > 0)
21359 target_x = pt_x + pixel_width;
21360 else
21361 target_x = pt_x - (!FRAME_WINDOW_P (it.f)) * pixel_width;
21363 /* Target X coordinate could be one line above or below the line
21364 of point, in which case we need to adjust the target X
21365 coordinate. Also, if moving to the left, we need to begin at
21366 the left edge of the point's screen line. */
21367 if (dir < 0)
21369 if (pt_x > 0)
21371 start_display (&it, w, pt);
21372 reseat_at_previous_visible_line_start (&it);
21373 it.current_x = it.current_y = it.hpos = 0;
21374 if (pt_vpos != 0)
21375 move_it_by_lines (&it, pt_vpos);
21377 else
21379 move_it_by_lines (&it, -1);
21380 target_x = it.last_visible_x - !FRAME_WINDOW_P (it.f);
21381 target_is_eol_p = true;
21382 /* Under word-wrap, we don't know the x coordinate of
21383 the last character displayed on the previous line,
21384 which immediately precedes the wrap point. To find
21385 out its x coordinate, we try moving to the right
21386 margin of the window, which will stop at the wrap
21387 point, and then reset target_x to point at the
21388 character that precedes the wrap point. This is not
21389 needed on GUI frames, because (see below) there we
21390 move from the left margin one grapheme cluster at a
21391 time, and stop when we hit the wrap point. */
21392 if (!FRAME_WINDOW_P (it.f) && it.line_wrap == WORD_WRAP)
21394 void *it_data = NULL;
21395 struct it it2;
21397 SAVE_IT (it2, it, it_data);
21398 move_it_in_display_line_to (&it, ZV, target_x,
21399 MOVE_TO_POS | MOVE_TO_X);
21400 /* If we arrived at target_x, that _is_ the last
21401 character on the previous line. */
21402 if (it.current_x != target_x)
21403 target_x = it.current_x - 1;
21404 RESTORE_IT (&it, &it2, it_data);
21408 else
21410 if (at_eol_p
21411 || (target_x >= it.last_visible_x
21412 && it.line_wrap != TRUNCATE))
21414 if (pt_x > 0)
21415 move_it_by_lines (&it, 0);
21416 move_it_by_lines (&it, 1);
21417 target_x = 0;
21421 /* Move to the target X coordinate. */
21422 #ifdef HAVE_WINDOW_SYSTEM
21423 /* On GUI frames, as we don't know the X coordinate of the
21424 character to the left of point, moving point to the left
21425 requires walking, one grapheme cluster at a time, until we
21426 find ourself at a place immediately to the left of the
21427 character at point. */
21428 if (FRAME_WINDOW_P (it.f) && dir < 0)
21430 struct text_pos new_pos;
21431 enum move_it_result rc = MOVE_X_REACHED;
21433 if (it.current_x == 0)
21434 get_next_display_element (&it);
21435 if (it.what == IT_COMPOSITION)
21437 new_pos.charpos = it.cmp_it.charpos;
21438 new_pos.bytepos = -1;
21440 else
21441 new_pos = it.current.pos;
21443 while (it.current_x + it.pixel_width <= target_x
21444 && (rc == MOVE_X_REACHED
21445 /* Under word-wrap, move_it_in_display_line_to
21446 stops at correct coordinates, but sometimes
21447 returns MOVE_POS_MATCH_OR_ZV. */
21448 || (it.line_wrap == WORD_WRAP
21449 && rc == MOVE_POS_MATCH_OR_ZV)))
21451 int new_x = it.current_x + it.pixel_width;
21453 /* For composed characters, we want the position of the
21454 first character in the grapheme cluster (usually, the
21455 composition's base character), whereas it.current
21456 might give us the position of the _last_ one, e.g. if
21457 the composition is rendered in reverse due to bidi
21458 reordering. */
21459 if (it.what == IT_COMPOSITION)
21461 new_pos.charpos = it.cmp_it.charpos;
21462 new_pos.bytepos = -1;
21464 else
21465 new_pos = it.current.pos;
21466 if (new_x == it.current_x)
21467 new_x++;
21468 rc = move_it_in_display_line_to (&it, ZV, new_x,
21469 MOVE_TO_POS | MOVE_TO_X);
21470 if (ITERATOR_AT_END_OF_LINE_P (&it) && !target_is_eol_p)
21471 break;
21473 /* The previous position we saw in the loop is the one we
21474 want. */
21475 if (new_pos.bytepos == -1)
21476 new_pos.bytepos = CHAR_TO_BYTE (new_pos.charpos);
21477 it.current.pos = new_pos;
21479 else
21480 #endif
21481 if (it.current_x != target_x)
21482 move_it_in_display_line_to (&it, ZV, target_x, MOVE_TO_POS | MOVE_TO_X);
21484 /* When lines are truncated, the above loop will stop at the
21485 window edge. But we want to get to the end of line, even if
21486 it is beyond the window edge; automatic hscroll will then
21487 scroll the window to show point as appropriate. */
21488 if (target_is_eol_p && it.line_wrap == TRUNCATE
21489 && get_next_display_element (&it))
21491 struct text_pos new_pos = it.current.pos;
21493 while (!ITERATOR_AT_END_OF_LINE_P (&it))
21495 set_iterator_to_next (&it, 0);
21496 if (it.method == GET_FROM_BUFFER)
21497 new_pos = it.current.pos;
21498 if (!get_next_display_element (&it))
21499 break;
21502 it.current.pos = new_pos;
21505 /* If we ended up in a display string that covers point, move to
21506 buffer position to the right in the visual order. */
21507 if (dir > 0)
21509 while (IT_CHARPOS (it) == PT)
21511 set_iterator_to_next (&it, 0);
21512 if (!get_next_display_element (&it))
21513 break;
21517 /* Move point to that position. */
21518 SET_PT_BOTH (IT_CHARPOS (it), IT_BYTEPOS (it));
21521 return make_number (PT);
21523 #undef ROW_GLYPH_NEWLINE_P
21526 DEFUN ("bidi-resolved-levels", Fbidi_resolved_levels,
21527 Sbidi_resolved_levels, 0, 1, 0,
21528 doc: /* Return the resolved bidirectional levels of characters at VPOS.
21530 The resolved levels are produced by the Emacs bidi reordering engine
21531 that implements the UBA, the Unicode Bidirectional Algorithm. Please
21532 read the Unicode Standard Annex 9 (UAX#9) for background information
21533 about these levels.
21535 VPOS is the zero-based number of the current window's screen line
21536 for which to produce the resolved levels. If VPOS is nil or omitted,
21537 it defaults to the screen line of point. If the window displays a
21538 header line, VPOS of zero will report on the header line, and first
21539 line of text in the window will have VPOS of 1.
21541 Value is an array of resolved levels, indexed by glyph number.
21542 Glyphs are numbered from zero starting from the beginning of the
21543 screen line, i.e. the left edge of the window for left-to-right lines
21544 and from the right edge for right-to-left lines. The resolved levels
21545 are produced only for the window's text area; text in display margins
21546 is not included.
21548 If the selected window's display is not up-to-date, or if the specified
21549 screen line does not display text, this function returns nil. It is
21550 highly recommended to bind this function to some simple key, like F8,
21551 in order to avoid these problems.
21553 This function exists mainly for testing the correctness of the
21554 Emacs UBA implementation, in particular with the test suite. */)
21555 (Lisp_Object vpos)
21557 struct window *w = XWINDOW (selected_window);
21558 struct buffer *b = XBUFFER (w->contents);
21559 int nrow;
21560 struct glyph_row *row;
21562 if (NILP (vpos))
21564 int d1, d2, d3, d4, d5;
21566 pos_visible_p (w, PT, &d1, &d2, &d3, &d4, &d5, &nrow);
21568 else
21570 CHECK_NUMBER_COERCE_MARKER (vpos);
21571 nrow = XINT (vpos);
21574 /* We require up-to-date glyph matrix for this window. */
21575 if (w->window_end_valid
21576 && !windows_or_buffers_changed
21577 && b
21578 && !b->clip_changed
21579 && !b->prevent_redisplay_optimizations_p
21580 && !window_outdated (w)
21581 && nrow >= 0
21582 && nrow < w->current_matrix->nrows
21583 && (row = MATRIX_ROW (w->current_matrix, nrow))->enabled_p
21584 && MATRIX_ROW_DISPLAYS_TEXT_P (row))
21586 struct glyph *g, *e, *g1;
21587 int nglyphs, i;
21588 Lisp_Object levels;
21590 if (!row->reversed_p) /* Left-to-right glyph row. */
21592 g = g1 = row->glyphs[TEXT_AREA];
21593 e = g + row->used[TEXT_AREA];
21595 /* Skip over glyphs at the start of the row that was
21596 generated by redisplay for its own needs. */
21597 while (g < e
21598 && NILP (g->object)
21599 && g->charpos < 0)
21600 g++;
21601 g1 = g;
21603 /* Count the "interesting" glyphs in this row. */
21604 for (nglyphs = 0; g < e && !NILP (g->object); g++)
21605 nglyphs++;
21607 /* Create and fill the array. */
21608 levels = make_uninit_vector (nglyphs);
21609 for (i = 0; g1 < g; i++, g1++)
21610 ASET (levels, i, make_number (g1->resolved_level));
21612 else /* Right-to-left glyph row. */
21614 g = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
21615 e = row->glyphs[TEXT_AREA] - 1;
21616 while (g > e
21617 && NILP (g->object)
21618 && g->charpos < 0)
21619 g--;
21620 g1 = g;
21621 for (nglyphs = 0; g > e && !NILP (g->object); g--)
21622 nglyphs++;
21623 levels = make_uninit_vector (nglyphs);
21624 for (i = 0; g1 > g; i++, g1--)
21625 ASET (levels, i, make_number (g1->resolved_level));
21627 return levels;
21629 else
21630 return Qnil;
21635 /***********************************************************************
21636 Menu Bar
21637 ***********************************************************************/
21639 /* Redisplay the menu bar in the frame for window W.
21641 The menu bar of X frames that don't have X toolkit support is
21642 displayed in a special window W->frame->menu_bar_window.
21644 The menu bar of terminal frames is treated specially as far as
21645 glyph matrices are concerned. Menu bar lines are not part of
21646 windows, so the update is done directly on the frame matrix rows
21647 for the menu bar. */
21649 static void
21650 display_menu_bar (struct window *w)
21652 struct frame *f = XFRAME (WINDOW_FRAME (w));
21653 struct it it;
21654 Lisp_Object items;
21655 int i;
21657 /* Don't do all this for graphical frames. */
21658 #ifdef HAVE_NTGUI
21659 if (FRAME_W32_P (f))
21660 return;
21661 #endif
21662 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
21663 if (FRAME_X_P (f))
21664 return;
21665 #endif
21667 #ifdef HAVE_NS
21668 if (FRAME_NS_P (f))
21669 return;
21670 #endif /* HAVE_NS */
21672 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
21673 eassert (!FRAME_WINDOW_P (f));
21674 init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MENU_FACE_ID);
21675 it.first_visible_x = 0;
21676 it.last_visible_x = FRAME_PIXEL_WIDTH (f);
21677 #elif defined (HAVE_X_WINDOWS) /* X without toolkit. */
21678 if (FRAME_WINDOW_P (f))
21680 /* Menu bar lines are displayed in the desired matrix of the
21681 dummy window menu_bar_window. */
21682 struct window *menu_w;
21683 menu_w = XWINDOW (f->menu_bar_window);
21684 init_iterator (&it, menu_w, -1, -1, menu_w->desired_matrix->rows,
21685 MENU_FACE_ID);
21686 it.first_visible_x = 0;
21687 it.last_visible_x = FRAME_PIXEL_WIDTH (f);
21689 else
21690 #endif /* not USE_X_TOOLKIT and not USE_GTK */
21692 /* This is a TTY frame, i.e. character hpos/vpos are used as
21693 pixel x/y. */
21694 init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
21695 MENU_FACE_ID);
21696 it.first_visible_x = 0;
21697 it.last_visible_x = FRAME_COLS (f);
21700 /* FIXME: This should be controlled by a user option. See the
21701 comments in redisplay_tool_bar and display_mode_line about
21702 this. */
21703 it.paragraph_embedding = L2R;
21705 /* Clear all rows of the menu bar. */
21706 for (i = 0; i < FRAME_MENU_BAR_LINES (f); ++i)
21708 struct glyph_row *row = it.glyph_row + i;
21709 clear_glyph_row (row);
21710 row->enabled_p = true;
21711 row->full_width_p = 1;
21712 row->reversed_p = false;
21715 /* Display all items of the menu bar. */
21716 items = FRAME_MENU_BAR_ITEMS (it.f);
21717 for (i = 0; i < ASIZE (items); i += 4)
21719 Lisp_Object string;
21721 /* Stop at nil string. */
21722 string = AREF (items, i + 1);
21723 if (NILP (string))
21724 break;
21726 /* Remember where item was displayed. */
21727 ASET (items, i + 3, make_number (it.hpos));
21729 /* Display the item, pad with one space. */
21730 if (it.current_x < it.last_visible_x)
21731 display_string (NULL, string, Qnil, 0, 0, &it,
21732 SCHARS (string) + 1, 0, 0, -1);
21735 /* Fill out the line with spaces. */
21736 if (it.current_x < it.last_visible_x)
21737 display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
21739 /* Compute the total height of the lines. */
21740 compute_line_metrics (&it);
21743 /* Deep copy of a glyph row, including the glyphs. */
21744 static void
21745 deep_copy_glyph_row (struct glyph_row *to, struct glyph_row *from)
21747 struct glyph *pointers[1 + LAST_AREA];
21748 int to_used = to->used[TEXT_AREA];
21750 /* Save glyph pointers of TO. */
21751 memcpy (pointers, to->glyphs, sizeof to->glyphs);
21753 /* Do a structure assignment. */
21754 *to = *from;
21756 /* Restore original glyph pointers of TO. */
21757 memcpy (to->glyphs, pointers, sizeof to->glyphs);
21759 /* Copy the glyphs. */
21760 memcpy (to->glyphs[TEXT_AREA], from->glyphs[TEXT_AREA],
21761 min (from->used[TEXT_AREA], to_used) * sizeof (struct glyph));
21763 /* If we filled only part of the TO row, fill the rest with
21764 space_glyph (which will display as empty space). */
21765 if (to_used > from->used[TEXT_AREA])
21766 fill_up_frame_row_with_spaces (to, to_used);
21769 /* Display one menu item on a TTY, by overwriting the glyphs in the
21770 frame F's desired glyph matrix with glyphs produced from the menu
21771 item text. Called from term.c to display TTY drop-down menus one
21772 item at a time.
21774 ITEM_TEXT is the menu item text as a C string.
21776 FACE_ID is the face ID to be used for this menu item. FACE_ID
21777 could specify one of 3 faces: a face for an enabled item, a face
21778 for a disabled item, or a face for a selected item.
21780 X and Y are coordinates of the first glyph in the frame's desired
21781 matrix to be overwritten by the menu item. Since this is a TTY, Y
21782 is the zero-based number of the glyph row and X is the zero-based
21783 glyph number in the row, starting from left, where to start
21784 displaying the item.
21786 SUBMENU non-zero means this menu item drops down a submenu, which
21787 should be indicated by displaying a proper visual cue after the
21788 item text. */
21790 void
21791 display_tty_menu_item (const char *item_text, int width, int face_id,
21792 int x, int y, int submenu)
21794 struct it it;
21795 struct frame *f = SELECTED_FRAME ();
21796 struct window *w = XWINDOW (f->selected_window);
21797 int saved_used, saved_truncated, saved_width, saved_reversed;
21798 struct glyph_row *row;
21799 size_t item_len = strlen (item_text);
21801 eassert (FRAME_TERMCAP_P (f));
21803 /* Don't write beyond the matrix's last row. This can happen for
21804 TTY screens that are not high enough to show the entire menu.
21805 (This is actually a bit of defensive programming, as
21806 tty_menu_display already limits the number of menu items to one
21807 less than the number of screen lines.) */
21808 if (y >= f->desired_matrix->nrows)
21809 return;
21811 init_iterator (&it, w, -1, -1, f->desired_matrix->rows + y, MENU_FACE_ID);
21812 it.first_visible_x = 0;
21813 it.last_visible_x = FRAME_COLS (f) - 1;
21814 row = it.glyph_row;
21815 /* Start with the row contents from the current matrix. */
21816 deep_copy_glyph_row (row, f->current_matrix->rows + y);
21817 saved_width = row->full_width_p;
21818 row->full_width_p = 1;
21819 saved_reversed = row->reversed_p;
21820 row->reversed_p = 0;
21821 row->enabled_p = true;
21823 /* Arrange for the menu item glyphs to start at (X,Y) and have the
21824 desired face. */
21825 eassert (x < f->desired_matrix->matrix_w);
21826 it.current_x = it.hpos = x;
21827 it.current_y = it.vpos = y;
21828 saved_used = row->used[TEXT_AREA];
21829 saved_truncated = row->truncated_on_right_p;
21830 row->used[TEXT_AREA] = x;
21831 it.face_id = face_id;
21832 it.line_wrap = TRUNCATE;
21834 /* FIXME: This should be controlled by a user option. See the
21835 comments in redisplay_tool_bar and display_mode_line about this.
21836 Also, if paragraph_embedding could ever be R2L, changes will be
21837 needed to avoid shifting to the right the row characters in
21838 term.c:append_glyph. */
21839 it.paragraph_embedding = L2R;
21841 /* Pad with a space on the left. */
21842 display_string (" ", Qnil, Qnil, 0, 0, &it, 1, 0, FRAME_COLS (f) - 1, -1);
21843 width--;
21844 /* Display the menu item, pad with spaces to WIDTH. */
21845 if (submenu)
21847 display_string (item_text, Qnil, Qnil, 0, 0, &it,
21848 item_len, 0, FRAME_COLS (f) - 1, -1);
21849 width -= item_len;
21850 /* Indicate with " >" that there's a submenu. */
21851 display_string (" >", Qnil, Qnil, 0, 0, &it, width, 0,
21852 FRAME_COLS (f) - 1, -1);
21854 else
21855 display_string (item_text, Qnil, Qnil, 0, 0, &it,
21856 width, 0, FRAME_COLS (f) - 1, -1);
21858 row->used[TEXT_AREA] = max (saved_used, row->used[TEXT_AREA]);
21859 row->truncated_on_right_p = saved_truncated;
21860 row->hash = row_hash (row);
21861 row->full_width_p = saved_width;
21862 row->reversed_p = saved_reversed;
21865 /***********************************************************************
21866 Mode Line
21867 ***********************************************************************/
21869 /* Redisplay mode lines in the window tree whose root is WINDOW. If
21870 FORCE is non-zero, redisplay mode lines unconditionally.
21871 Otherwise, redisplay only mode lines that are garbaged. Value is
21872 the number of windows whose mode lines were redisplayed. */
21874 static int
21875 redisplay_mode_lines (Lisp_Object window, bool force)
21877 int nwindows = 0;
21879 while (!NILP (window))
21881 struct window *w = XWINDOW (window);
21883 if (WINDOWP (w->contents))
21884 nwindows += redisplay_mode_lines (w->contents, force);
21885 else if (force
21886 || FRAME_GARBAGED_P (XFRAME (w->frame))
21887 || !MATRIX_MODE_LINE_ROW (w->current_matrix)->enabled_p)
21889 struct text_pos lpoint;
21890 struct buffer *old = current_buffer;
21892 /* Set the window's buffer for the mode line display. */
21893 SET_TEXT_POS (lpoint, PT, PT_BYTE);
21894 set_buffer_internal_1 (XBUFFER (w->contents));
21896 /* Point refers normally to the selected window. For any
21897 other window, set up appropriate value. */
21898 if (!EQ (window, selected_window))
21900 struct text_pos pt;
21902 CLIP_TEXT_POS_FROM_MARKER (pt, w->pointm);
21903 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
21906 /* Display mode lines. */
21907 clear_glyph_matrix (w->desired_matrix);
21908 if (display_mode_lines (w))
21909 ++nwindows;
21911 /* Restore old settings. */
21912 set_buffer_internal_1 (old);
21913 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
21916 window = w->next;
21919 return nwindows;
21923 /* Display the mode and/or header line of window W. Value is the
21924 sum number of mode lines and header lines displayed. */
21926 static int
21927 display_mode_lines (struct window *w)
21929 Lisp_Object old_selected_window = selected_window;
21930 Lisp_Object old_selected_frame = selected_frame;
21931 Lisp_Object new_frame = w->frame;
21932 Lisp_Object old_frame_selected_window = XFRAME (new_frame)->selected_window;
21933 int n = 0;
21935 selected_frame = new_frame;
21936 /* FIXME: If we were to allow the mode-line's computation changing the buffer
21937 or window's point, then we'd need select_window_1 here as well. */
21938 XSETWINDOW (selected_window, w);
21939 XFRAME (new_frame)->selected_window = selected_window;
21941 /* These will be set while the mode line specs are processed. */
21942 line_number_displayed = 0;
21943 w->column_number_displayed = -1;
21945 if (WINDOW_WANTS_MODELINE_P (w))
21947 struct window *sel_w = XWINDOW (old_selected_window);
21949 /* Select mode line face based on the real selected window. */
21950 display_mode_line (w, CURRENT_MODE_LINE_FACE_ID_3 (sel_w, sel_w, w),
21951 BVAR (current_buffer, mode_line_format));
21952 ++n;
21955 if (WINDOW_WANTS_HEADER_LINE_P (w))
21957 display_mode_line (w, HEADER_LINE_FACE_ID,
21958 BVAR (current_buffer, header_line_format));
21959 ++n;
21962 XFRAME (new_frame)->selected_window = old_frame_selected_window;
21963 selected_frame = old_selected_frame;
21964 selected_window = old_selected_window;
21965 if (n > 0)
21966 w->must_be_updated_p = true;
21967 return n;
21971 /* Display mode or header line of window W. FACE_ID specifies which
21972 line to display; it is either MODE_LINE_FACE_ID or
21973 HEADER_LINE_FACE_ID. FORMAT is the mode/header line format to
21974 display. Value is the pixel height of the mode/header line
21975 displayed. */
21977 static int
21978 display_mode_line (struct window *w, enum face_id face_id, Lisp_Object format)
21980 struct it it;
21981 struct face *face;
21982 ptrdiff_t count = SPECPDL_INDEX ();
21984 init_iterator (&it, w, -1, -1, NULL, face_id);
21985 /* Don't extend on a previously drawn mode-line.
21986 This may happen if called from pos_visible_p. */
21987 it.glyph_row->enabled_p = false;
21988 prepare_desired_row (w, it.glyph_row, true);
21990 it.glyph_row->mode_line_p = 1;
21992 /* FIXME: This should be controlled by a user option. But
21993 supporting such an option is not trivial, since the mode line is
21994 made up of many separate strings. */
21995 it.paragraph_embedding = L2R;
21997 record_unwind_protect (unwind_format_mode_line,
21998 format_mode_line_unwind_data (NULL, NULL, Qnil, 0));
22000 mode_line_target = MODE_LINE_DISPLAY;
22002 /* Temporarily make frame's keyboard the current kboard so that
22003 kboard-local variables in the mode_line_format will get the right
22004 values. */
22005 push_kboard (FRAME_KBOARD (it.f));
22006 record_unwind_save_match_data ();
22007 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
22008 pop_kboard ();
22010 unbind_to (count, Qnil);
22012 /* Fill up with spaces. */
22013 display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);
22015 compute_line_metrics (&it);
22016 it.glyph_row->full_width_p = 1;
22017 it.glyph_row->continued_p = 0;
22018 it.glyph_row->truncated_on_left_p = 0;
22019 it.glyph_row->truncated_on_right_p = 0;
22021 /* Make a 3D mode-line have a shadow at its right end. */
22022 face = FACE_FROM_ID (it.f, face_id);
22023 extend_face_to_end_of_line (&it);
22024 if (face->box != FACE_NO_BOX)
22026 struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
22027 + it.glyph_row->used[TEXT_AREA] - 1);
22028 last->right_box_line_p = 1;
22031 return it.glyph_row->height;
22034 /* Move element ELT in LIST to the front of LIST.
22035 Return the updated list. */
22037 static Lisp_Object
22038 move_elt_to_front (Lisp_Object elt, Lisp_Object list)
22040 register Lisp_Object tail, prev;
22041 register Lisp_Object tem;
22043 tail = list;
22044 prev = Qnil;
22045 while (CONSP (tail))
22047 tem = XCAR (tail);
22049 if (EQ (elt, tem))
22051 /* Splice out the link TAIL. */
22052 if (NILP (prev))
22053 list = XCDR (tail);
22054 else
22055 Fsetcdr (prev, XCDR (tail));
22057 /* Now make it the first. */
22058 Fsetcdr (tail, list);
22059 return tail;
22061 else
22062 prev = tail;
22063 tail = XCDR (tail);
22064 QUIT;
22067 /* Not found--return unchanged LIST. */
22068 return list;
22071 /* Contribute ELT to the mode line for window IT->w. How it
22072 translates into text depends on its data type.
22074 IT describes the display environment in which we display, as usual.
22076 DEPTH is the depth in recursion. It is used to prevent
22077 infinite recursion here.
22079 FIELD_WIDTH is the number of characters the display of ELT should
22080 occupy in the mode line, and PRECISION is the maximum number of
22081 characters to display from ELT's representation. See
22082 display_string for details.
22084 Returns the hpos of the end of the text generated by ELT.
22086 PROPS is a property list to add to any string we encounter.
22088 If RISKY is nonzero, remove (disregard) any properties in any string
22089 we encounter, and ignore :eval and :propertize.
22091 The global variable `mode_line_target' determines whether the
22092 output is passed to `store_mode_line_noprop',
22093 `store_mode_line_string', or `display_string'. */
22095 static int
22096 display_mode_element (struct it *it, int depth, int field_width, int precision,
22097 Lisp_Object elt, Lisp_Object props, int risky)
22099 int n = 0, field, prec;
22100 int literal = 0;
22102 tail_recurse:
22103 if (depth > 100)
22104 elt = build_string ("*too-deep*");
22106 depth++;
22108 switch (XTYPE (elt))
22110 case Lisp_String:
22112 /* A string: output it and check for %-constructs within it. */
22113 unsigned char c;
22114 ptrdiff_t offset = 0;
22116 if (SCHARS (elt) > 0
22117 && (!NILP (props) || risky))
22119 Lisp_Object oprops, aelt;
22120 oprops = Ftext_properties_at (make_number (0), elt);
22122 /* If the starting string's properties are not what
22123 we want, translate the string. Also, if the string
22124 is risky, do that anyway. */
22126 if (NILP (Fequal (props, oprops)) || risky)
22128 /* If the starting string has properties,
22129 merge the specified ones onto the existing ones. */
22130 if (! NILP (oprops) && !risky)
22132 Lisp_Object tem;
22134 oprops = Fcopy_sequence (oprops);
22135 tem = props;
22136 while (CONSP (tem))
22138 oprops = Fplist_put (oprops, XCAR (tem),
22139 XCAR (XCDR (tem)));
22140 tem = XCDR (XCDR (tem));
22142 props = oprops;
22145 aelt = Fassoc (elt, mode_line_proptrans_alist);
22146 if (! NILP (aelt) && !NILP (Fequal (props, XCDR (aelt))))
22148 /* AELT is what we want. Move it to the front
22149 without consing. */
22150 elt = XCAR (aelt);
22151 mode_line_proptrans_alist
22152 = move_elt_to_front (aelt, mode_line_proptrans_alist);
22154 else
22156 Lisp_Object tem;
22158 /* If AELT has the wrong props, it is useless.
22159 so get rid of it. */
22160 if (! NILP (aelt))
22161 mode_line_proptrans_alist
22162 = Fdelq (aelt, mode_line_proptrans_alist);
22164 elt = Fcopy_sequence (elt);
22165 Fset_text_properties (make_number (0), Flength (elt),
22166 props, elt);
22167 /* Add this item to mode_line_proptrans_alist. */
22168 mode_line_proptrans_alist
22169 = Fcons (Fcons (elt, props),
22170 mode_line_proptrans_alist);
22171 /* Truncate mode_line_proptrans_alist
22172 to at most 50 elements. */
22173 tem = Fnthcdr (make_number (50),
22174 mode_line_proptrans_alist);
22175 if (! NILP (tem))
22176 XSETCDR (tem, Qnil);
22181 offset = 0;
22183 if (literal)
22185 prec = precision - n;
22186 switch (mode_line_target)
22188 case MODE_LINE_NOPROP:
22189 case MODE_LINE_TITLE:
22190 n += store_mode_line_noprop (SSDATA (elt), -1, prec);
22191 break;
22192 case MODE_LINE_STRING:
22193 n += store_mode_line_string (NULL, elt, 1, 0, prec, Qnil);
22194 break;
22195 case MODE_LINE_DISPLAY:
22196 n += display_string (NULL, elt, Qnil, 0, 0, it,
22197 0, prec, 0, STRING_MULTIBYTE (elt));
22198 break;
22201 break;
22204 /* Handle the non-literal case. */
22206 while ((precision <= 0 || n < precision)
22207 && SREF (elt, offset) != 0
22208 && (mode_line_target != MODE_LINE_DISPLAY
22209 || it->current_x < it->last_visible_x))
22211 ptrdiff_t last_offset = offset;
22213 /* Advance to end of string or next format specifier. */
22214 while ((c = SREF (elt, offset++)) != '\0' && c != '%')
22217 if (offset - 1 != last_offset)
22219 ptrdiff_t nchars, nbytes;
22221 /* Output to end of string or up to '%'. Field width
22222 is length of string. Don't output more than
22223 PRECISION allows us. */
22224 offset--;
22226 prec = c_string_width (SDATA (elt) + last_offset,
22227 offset - last_offset, precision - n,
22228 &nchars, &nbytes);
22230 switch (mode_line_target)
22232 case MODE_LINE_NOPROP:
22233 case MODE_LINE_TITLE:
22234 n += store_mode_line_noprop (SSDATA (elt) + last_offset, 0, prec);
22235 break;
22236 case MODE_LINE_STRING:
22238 ptrdiff_t bytepos = last_offset;
22239 ptrdiff_t charpos = string_byte_to_char (elt, bytepos);
22240 ptrdiff_t endpos = (precision <= 0
22241 ? string_byte_to_char (elt, offset)
22242 : charpos + nchars);
22244 n += store_mode_line_string (NULL,
22245 Fsubstring (elt, make_number (charpos),
22246 make_number (endpos)),
22247 0, 0, 0, Qnil);
22249 break;
22250 case MODE_LINE_DISPLAY:
22252 ptrdiff_t bytepos = last_offset;
22253 ptrdiff_t charpos = string_byte_to_char (elt, bytepos);
22255 if (precision <= 0)
22256 nchars = string_byte_to_char (elt, offset) - charpos;
22257 n += display_string (NULL, elt, Qnil, 0, charpos,
22258 it, 0, nchars, 0,
22259 STRING_MULTIBYTE (elt));
22261 break;
22264 else /* c == '%' */
22266 ptrdiff_t percent_position = offset;
22268 /* Get the specified minimum width. Zero means
22269 don't pad. */
22270 field = 0;
22271 while ((c = SREF (elt, offset++)) >= '0' && c <= '9')
22272 field = field * 10 + c - '0';
22274 /* Don't pad beyond the total padding allowed. */
22275 if (field_width - n > 0 && field > field_width - n)
22276 field = field_width - n;
22278 /* Note that either PRECISION <= 0 or N < PRECISION. */
22279 prec = precision - n;
22281 if (c == 'M')
22282 n += display_mode_element (it, depth, field, prec,
22283 Vglobal_mode_string, props,
22284 risky);
22285 else if (c != 0)
22287 bool multibyte;
22288 ptrdiff_t bytepos, charpos;
22289 const char *spec;
22290 Lisp_Object string;
22292 bytepos = percent_position;
22293 charpos = (STRING_MULTIBYTE (elt)
22294 ? string_byte_to_char (elt, bytepos)
22295 : bytepos);
22296 spec = decode_mode_spec (it->w, c, field, &string);
22297 multibyte = STRINGP (string) && STRING_MULTIBYTE (string);
22299 switch (mode_line_target)
22301 case MODE_LINE_NOPROP:
22302 case MODE_LINE_TITLE:
22303 n += store_mode_line_noprop (spec, field, prec);
22304 break;
22305 case MODE_LINE_STRING:
22307 Lisp_Object tem = build_string (spec);
22308 props = Ftext_properties_at (make_number (charpos), elt);
22309 /* Should only keep face property in props */
22310 n += store_mode_line_string (NULL, tem, 0, field, prec, props);
22312 break;
22313 case MODE_LINE_DISPLAY:
22315 int nglyphs_before, nwritten;
22317 nglyphs_before = it->glyph_row->used[TEXT_AREA];
22318 nwritten = display_string (spec, string, elt,
22319 charpos, 0, it,
22320 field, prec, 0,
22321 multibyte);
22323 /* Assign to the glyphs written above the
22324 string where the `%x' came from, position
22325 of the `%'. */
22326 if (nwritten > 0)
22328 struct glyph *glyph
22329 = (it->glyph_row->glyphs[TEXT_AREA]
22330 + nglyphs_before);
22331 int i;
22333 for (i = 0; i < nwritten; ++i)
22335 glyph[i].object = elt;
22336 glyph[i].charpos = charpos;
22339 n += nwritten;
22342 break;
22345 else /* c == 0 */
22346 break;
22350 break;
22352 case Lisp_Symbol:
22353 /* A symbol: process the value of the symbol recursively
22354 as if it appeared here directly. Avoid error if symbol void.
22355 Special case: if value of symbol is a string, output the string
22356 literally. */
22358 register Lisp_Object tem;
22360 /* If the variable is not marked as risky to set
22361 then its contents are risky to use. */
22362 if (NILP (Fget (elt, Qrisky_local_variable)))
22363 risky = 1;
22365 tem = Fboundp (elt);
22366 if (!NILP (tem))
22368 tem = Fsymbol_value (elt);
22369 /* If value is a string, output that string literally:
22370 don't check for % within it. */
22371 if (STRINGP (tem))
22372 literal = 1;
22374 if (!EQ (tem, elt))
22376 /* Give up right away for nil or t. */
22377 elt = tem;
22378 goto tail_recurse;
22382 break;
22384 case Lisp_Cons:
22386 register Lisp_Object car, tem;
22388 /* A cons cell: five distinct cases.
22389 If first element is :eval or :propertize, do something special.
22390 If first element is a string or a cons, process all the elements
22391 and effectively concatenate them.
22392 If first element is a negative number, truncate displaying cdr to
22393 at most that many characters. If positive, pad (with spaces)
22394 to at least that many characters.
22395 If first element is a symbol, process the cadr or caddr recursively
22396 according to whether the symbol's value is non-nil or nil. */
22397 car = XCAR (elt);
22398 if (EQ (car, QCeval))
22400 /* An element of the form (:eval FORM) means evaluate FORM
22401 and use the result as mode line elements. */
22403 if (risky)
22404 break;
22406 if (CONSP (XCDR (elt)))
22408 Lisp_Object spec;
22409 spec = safe__eval (true, XCAR (XCDR (elt)));
22410 n += display_mode_element (it, depth, field_width - n,
22411 precision - n, spec, props,
22412 risky);
22415 else if (EQ (car, QCpropertize))
22417 /* An element of the form (:propertize ELT PROPS...)
22418 means display ELT but applying properties PROPS. */
22420 if (risky)
22421 break;
22423 if (CONSP (XCDR (elt)))
22424 n += display_mode_element (it, depth, field_width - n,
22425 precision - n, XCAR (XCDR (elt)),
22426 XCDR (XCDR (elt)), risky);
22428 else if (SYMBOLP (car))
22430 tem = Fboundp (car);
22431 elt = XCDR (elt);
22432 if (!CONSP (elt))
22433 goto invalid;
22434 /* elt is now the cdr, and we know it is a cons cell.
22435 Use its car if CAR has a non-nil value. */
22436 if (!NILP (tem))
22438 tem = Fsymbol_value (car);
22439 if (!NILP (tem))
22441 elt = XCAR (elt);
22442 goto tail_recurse;
22445 /* Symbol's value is nil (or symbol is unbound)
22446 Get the cddr of the original list
22447 and if possible find the caddr and use that. */
22448 elt = XCDR (elt);
22449 if (NILP (elt))
22450 break;
22451 else if (!CONSP (elt))
22452 goto invalid;
22453 elt = XCAR (elt);
22454 goto tail_recurse;
22456 else if (INTEGERP (car))
22458 register int lim = XINT (car);
22459 elt = XCDR (elt);
22460 if (lim < 0)
22462 /* Negative int means reduce maximum width. */
22463 if (precision <= 0)
22464 precision = -lim;
22465 else
22466 precision = min (precision, -lim);
22468 else if (lim > 0)
22470 /* Padding specified. Don't let it be more than
22471 current maximum. */
22472 if (precision > 0)
22473 lim = min (precision, lim);
22475 /* If that's more padding than already wanted, queue it.
22476 But don't reduce padding already specified even if
22477 that is beyond the current truncation point. */
22478 field_width = max (lim, field_width);
22480 goto tail_recurse;
22482 else if (STRINGP (car) || CONSP (car))
22484 Lisp_Object halftail = elt;
22485 int len = 0;
22487 while (CONSP (elt)
22488 && (precision <= 0 || n < precision))
22490 n += display_mode_element (it, depth,
22491 /* Do padding only after the last
22492 element in the list. */
22493 (! CONSP (XCDR (elt))
22494 ? field_width - n
22495 : 0),
22496 precision - n, XCAR (elt),
22497 props, risky);
22498 elt = XCDR (elt);
22499 len++;
22500 if ((len & 1) == 0)
22501 halftail = XCDR (halftail);
22502 /* Check for cycle. */
22503 if (EQ (halftail, elt))
22504 break;
22508 break;
22510 default:
22511 invalid:
22512 elt = build_string ("*invalid*");
22513 goto tail_recurse;
22516 /* Pad to FIELD_WIDTH. */
22517 if (field_width > 0 && n < field_width)
22519 switch (mode_line_target)
22521 case MODE_LINE_NOPROP:
22522 case MODE_LINE_TITLE:
22523 n += store_mode_line_noprop ("", field_width - n, 0);
22524 break;
22525 case MODE_LINE_STRING:
22526 n += store_mode_line_string ("", Qnil, 0, field_width - n, 0, Qnil);
22527 break;
22528 case MODE_LINE_DISPLAY:
22529 n += display_string ("", Qnil, Qnil, 0, 0, it, field_width - n,
22530 0, 0, 0);
22531 break;
22535 return n;
22538 /* Store a mode-line string element in mode_line_string_list.
22540 If STRING is non-null, display that C string. Otherwise, the Lisp
22541 string LISP_STRING is displayed.
22543 FIELD_WIDTH is the minimum number of output glyphs to produce.
22544 If STRING has fewer characters than FIELD_WIDTH, pad to the right
22545 with spaces. FIELD_WIDTH <= 0 means don't pad.
22547 PRECISION is the maximum number of characters to output from
22548 STRING. PRECISION <= 0 means don't truncate the string.
22550 If COPY_STRING is non-zero, make a copy of LISP_STRING before adding
22551 properties to the string.
22553 PROPS are the properties to add to the string.
22554 The mode_line_string_face face property is always added to the string.
22557 static int
22558 store_mode_line_string (const char *string, Lisp_Object lisp_string, int copy_string,
22559 int field_width, int precision, Lisp_Object props)
22561 ptrdiff_t len;
22562 int n = 0;
22564 if (string != NULL)
22566 len = strlen (string);
22567 if (precision > 0 && len > precision)
22568 len = precision;
22569 lisp_string = make_string (string, len);
22570 if (NILP (props))
22571 props = mode_line_string_face_prop;
22572 else if (!NILP (mode_line_string_face))
22574 Lisp_Object face = Fplist_get (props, Qface);
22575 props = Fcopy_sequence (props);
22576 if (NILP (face))
22577 face = mode_line_string_face;
22578 else
22579 face = list2 (face, mode_line_string_face);
22580 props = Fplist_put (props, Qface, face);
22582 Fadd_text_properties (make_number (0), make_number (len),
22583 props, lisp_string);
22585 else
22587 len = XFASTINT (Flength (lisp_string));
22588 if (precision > 0 && len > precision)
22590 len = precision;
22591 lisp_string = Fsubstring (lisp_string, make_number (0), make_number (len));
22592 precision = -1;
22594 if (!NILP (mode_line_string_face))
22596 Lisp_Object face;
22597 if (NILP (props))
22598 props = Ftext_properties_at (make_number (0), lisp_string);
22599 face = Fplist_get (props, Qface);
22600 if (NILP (face))
22601 face = mode_line_string_face;
22602 else
22603 face = list2 (face, mode_line_string_face);
22604 props = list2 (Qface, face);
22605 if (copy_string)
22606 lisp_string = Fcopy_sequence (lisp_string);
22608 if (!NILP (props))
22609 Fadd_text_properties (make_number (0), make_number (len),
22610 props, lisp_string);
22613 if (len > 0)
22615 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
22616 n += len;
22619 if (field_width > len)
22621 field_width -= len;
22622 lisp_string = Fmake_string (make_number (field_width), make_number (' '));
22623 if (!NILP (props))
22624 Fadd_text_properties (make_number (0), make_number (field_width),
22625 props, lisp_string);
22626 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
22627 n += field_width;
22630 return n;
22634 DEFUN ("format-mode-line", Fformat_mode_line, Sformat_mode_line,
22635 1, 4, 0,
22636 doc: /* Format a string out of a mode line format specification.
22637 First arg FORMAT specifies the mode line format (see `mode-line-format'
22638 for details) to use.
22640 By default, the format is evaluated for the currently selected window.
22642 Optional second arg FACE specifies the face property to put on all
22643 characters for which no face is specified. The value nil means the
22644 default face. The value t means whatever face the window's mode line
22645 currently uses (either `mode-line' or `mode-line-inactive',
22646 depending on whether the window is the selected window or not).
22647 An integer value means the value string has no text
22648 properties.
22650 Optional third and fourth args WINDOW and BUFFER specify the window
22651 and buffer to use as the context for the formatting (defaults
22652 are the selected window and the WINDOW's buffer). */)
22653 (Lisp_Object format, Lisp_Object face,
22654 Lisp_Object window, Lisp_Object buffer)
22656 struct it it;
22657 int len;
22658 struct window *w;
22659 struct buffer *old_buffer = NULL;
22660 int face_id;
22661 int no_props = INTEGERP (face);
22662 ptrdiff_t count = SPECPDL_INDEX ();
22663 Lisp_Object str;
22664 int string_start = 0;
22666 w = decode_any_window (window);
22667 XSETWINDOW (window, w);
22669 if (NILP (buffer))
22670 buffer = w->contents;
22671 CHECK_BUFFER (buffer);
22673 /* Make formatting the modeline a non-op when noninteractive, otherwise
22674 there will be problems later caused by a partially initialized frame. */
22675 if (NILP (format) || noninteractive)
22676 return empty_unibyte_string;
22678 if (no_props)
22679 face = Qnil;
22681 face_id = (NILP (face) || EQ (face, Qdefault)) ? DEFAULT_FACE_ID
22682 : EQ (face, Qt) ? (EQ (window, selected_window)
22683 ? MODE_LINE_FACE_ID : MODE_LINE_INACTIVE_FACE_ID)
22684 : EQ (face, Qmode_line) ? MODE_LINE_FACE_ID
22685 : EQ (face, Qmode_line_inactive) ? MODE_LINE_INACTIVE_FACE_ID
22686 : EQ (face, Qheader_line) ? HEADER_LINE_FACE_ID
22687 : EQ (face, Qtool_bar) ? TOOL_BAR_FACE_ID
22688 : DEFAULT_FACE_ID;
22690 old_buffer = current_buffer;
22692 /* Save things including mode_line_proptrans_alist,
22693 and set that to nil so that we don't alter the outer value. */
22694 record_unwind_protect (unwind_format_mode_line,
22695 format_mode_line_unwind_data
22696 (XFRAME (WINDOW_FRAME (w)),
22697 old_buffer, selected_window, 1));
22698 mode_line_proptrans_alist = Qnil;
22700 Fselect_window (window, Qt);
22701 set_buffer_internal_1 (XBUFFER (buffer));
22703 init_iterator (&it, w, -1, -1, NULL, face_id);
22705 if (no_props)
22707 mode_line_target = MODE_LINE_NOPROP;
22708 mode_line_string_face_prop = Qnil;
22709 mode_line_string_list = Qnil;
22710 string_start = MODE_LINE_NOPROP_LEN (0);
22712 else
22714 mode_line_target = MODE_LINE_STRING;
22715 mode_line_string_list = Qnil;
22716 mode_line_string_face = face;
22717 mode_line_string_face_prop
22718 = NILP (face) ? Qnil : list2 (Qface, face);
22721 push_kboard (FRAME_KBOARD (it.f));
22722 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
22723 pop_kboard ();
22725 if (no_props)
22727 len = MODE_LINE_NOPROP_LEN (string_start);
22728 str = make_string (mode_line_noprop_buf + string_start, len);
22730 else
22732 mode_line_string_list = Fnreverse (mode_line_string_list);
22733 str = Fmapconcat (intern ("identity"), mode_line_string_list,
22734 empty_unibyte_string);
22737 unbind_to (count, Qnil);
22738 return str;
22741 /* Write a null-terminated, right justified decimal representation of
22742 the positive integer D to BUF using a minimal field width WIDTH. */
22744 static void
22745 pint2str (register char *buf, register int width, register ptrdiff_t d)
22747 register char *p = buf;
22749 if (d <= 0)
22750 *p++ = '0';
22751 else
22753 while (d > 0)
22755 *p++ = d % 10 + '0';
22756 d /= 10;
22760 for (width -= (int) (p - buf); width > 0; --width)
22761 *p++ = ' ';
22762 *p-- = '\0';
22763 while (p > buf)
22765 d = *buf;
22766 *buf++ = *p;
22767 *p-- = d;
22771 /* Write a null-terminated, right justified decimal and "human
22772 readable" representation of the nonnegative integer D to BUF using
22773 a minimal field width WIDTH. D should be smaller than 999.5e24. */
22775 static const char power_letter[] =
22777 0, /* no letter */
22778 'k', /* kilo */
22779 'M', /* mega */
22780 'G', /* giga */
22781 'T', /* tera */
22782 'P', /* peta */
22783 'E', /* exa */
22784 'Z', /* zetta */
22785 'Y' /* yotta */
22788 static void
22789 pint2hrstr (char *buf, int width, ptrdiff_t d)
22791 /* We aim to represent the nonnegative integer D as
22792 QUOTIENT.TENTHS * 10 ^ (3 * EXPONENT). */
22793 ptrdiff_t quotient = d;
22794 int remainder = 0;
22795 /* -1 means: do not use TENTHS. */
22796 int tenths = -1;
22797 int exponent = 0;
22799 /* Length of QUOTIENT.TENTHS as a string. */
22800 int length;
22802 char * psuffix;
22803 char * p;
22805 if (quotient >= 1000)
22807 /* Scale to the appropriate EXPONENT. */
22810 remainder = quotient % 1000;
22811 quotient /= 1000;
22812 exponent++;
22814 while (quotient >= 1000);
22816 /* Round to nearest and decide whether to use TENTHS or not. */
22817 if (quotient <= 9)
22819 tenths = remainder / 100;
22820 if (remainder % 100 >= 50)
22822 if (tenths < 9)
22823 tenths++;
22824 else
22826 quotient++;
22827 if (quotient == 10)
22828 tenths = -1;
22829 else
22830 tenths = 0;
22834 else
22835 if (remainder >= 500)
22837 if (quotient < 999)
22838 quotient++;
22839 else
22841 quotient = 1;
22842 exponent++;
22843 tenths = 0;
22848 /* Calculate the LENGTH of QUOTIENT.TENTHS as a string. */
22849 if (tenths == -1 && quotient <= 99)
22850 if (quotient <= 9)
22851 length = 1;
22852 else
22853 length = 2;
22854 else
22855 length = 3;
22856 p = psuffix = buf + max (width, length);
22858 /* Print EXPONENT. */
22859 *psuffix++ = power_letter[exponent];
22860 *psuffix = '\0';
22862 /* Print TENTHS. */
22863 if (tenths >= 0)
22865 *--p = '0' + tenths;
22866 *--p = '.';
22869 /* Print QUOTIENT. */
22872 int digit = quotient % 10;
22873 *--p = '0' + digit;
22875 while ((quotient /= 10) != 0);
22877 /* Print leading spaces. */
22878 while (buf < p)
22879 *--p = ' ';
22882 /* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
22883 If EOL_FLAG is 1, set also a mnemonic character for end-of-line
22884 type of CODING_SYSTEM. Return updated pointer into BUF. */
22886 static unsigned char invalid_eol_type[] = "(*invalid*)";
22888 static char *
22889 decode_mode_spec_coding (Lisp_Object coding_system, register char *buf, int eol_flag)
22891 Lisp_Object val;
22892 bool multibyte = !NILP (BVAR (current_buffer, enable_multibyte_characters));
22893 const unsigned char *eol_str;
22894 int eol_str_len;
22895 /* The EOL conversion we are using. */
22896 Lisp_Object eoltype;
22898 val = CODING_SYSTEM_SPEC (coding_system);
22899 eoltype = Qnil;
22901 if (!VECTORP (val)) /* Not yet decided. */
22903 *buf++ = multibyte ? '-' : ' ';
22904 if (eol_flag)
22905 eoltype = eol_mnemonic_undecided;
22906 /* Don't mention EOL conversion if it isn't decided. */
22908 else
22910 Lisp_Object attrs;
22911 Lisp_Object eolvalue;
22913 attrs = AREF (val, 0);
22914 eolvalue = AREF (val, 2);
22916 *buf++ = multibyte
22917 ? XFASTINT (CODING_ATTR_MNEMONIC (attrs))
22918 : ' ';
22920 if (eol_flag)
22922 /* The EOL conversion that is normal on this system. */
22924 if (NILP (eolvalue)) /* Not yet decided. */
22925 eoltype = eol_mnemonic_undecided;
22926 else if (VECTORP (eolvalue)) /* Not yet decided. */
22927 eoltype = eol_mnemonic_undecided;
22928 else /* eolvalue is Qunix, Qdos, or Qmac. */
22929 eoltype = (EQ (eolvalue, Qunix)
22930 ? eol_mnemonic_unix
22931 : (EQ (eolvalue, Qdos) == 1
22932 ? eol_mnemonic_dos : eol_mnemonic_mac));
22936 if (eol_flag)
22938 /* Mention the EOL conversion if it is not the usual one. */
22939 if (STRINGP (eoltype))
22941 eol_str = SDATA (eoltype);
22942 eol_str_len = SBYTES (eoltype);
22944 else if (CHARACTERP (eoltype))
22946 int c = XFASTINT (eoltype);
22947 return buf + CHAR_STRING (c, (unsigned char *) buf);
22949 else
22951 eol_str = invalid_eol_type;
22952 eol_str_len = sizeof (invalid_eol_type) - 1;
22954 memcpy (buf, eol_str, eol_str_len);
22955 buf += eol_str_len;
22958 return buf;
22961 /* Return a string for the output of a mode line %-spec for window W,
22962 generated by character C. FIELD_WIDTH > 0 means pad the string
22963 returned with spaces to that value. Return a Lisp string in
22964 *STRING if the resulting string is taken from that Lisp string.
22966 Note we operate on the current buffer for most purposes. */
22968 static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
22970 static const char *
22971 decode_mode_spec (struct window *w, register int c, int field_width,
22972 Lisp_Object *string)
22974 Lisp_Object obj;
22975 struct frame *f = XFRAME (WINDOW_FRAME (w));
22976 char *decode_mode_spec_buf = f->decode_mode_spec_buffer;
22977 /* We are going to use f->decode_mode_spec_buffer as the buffer to
22978 produce strings from numerical values, so limit preposterously
22979 large values of FIELD_WIDTH to avoid overrunning the buffer's
22980 end. The size of the buffer is enough for FRAME_MESSAGE_BUF_SIZE
22981 bytes plus the terminating null. */
22982 int width = min (field_width, FRAME_MESSAGE_BUF_SIZE (f));
22983 struct buffer *b = current_buffer;
22985 obj = Qnil;
22986 *string = Qnil;
22988 switch (c)
22990 case '*':
22991 if (!NILP (BVAR (b, read_only)))
22992 return "%";
22993 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
22994 return "*";
22995 return "-";
22997 case '+':
22998 /* This differs from %* only for a modified read-only buffer. */
22999 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
23000 return "*";
23001 if (!NILP (BVAR (b, read_only)))
23002 return "%";
23003 return "-";
23005 case '&':
23006 /* This differs from %* in ignoring read-only-ness. */
23007 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
23008 return "*";
23009 return "-";
23011 case '%':
23012 return "%";
23014 case '[':
23016 int i;
23017 char *p;
23019 if (command_loop_level > 5)
23020 return "[[[... ";
23021 p = decode_mode_spec_buf;
23022 for (i = 0; i < command_loop_level; i++)
23023 *p++ = '[';
23024 *p = 0;
23025 return decode_mode_spec_buf;
23028 case ']':
23030 int i;
23031 char *p;
23033 if (command_loop_level > 5)
23034 return " ...]]]";
23035 p = decode_mode_spec_buf;
23036 for (i = 0; i < command_loop_level; i++)
23037 *p++ = ']';
23038 *p = 0;
23039 return decode_mode_spec_buf;
23042 case '-':
23044 register int i;
23046 /* Let lots_of_dashes be a string of infinite length. */
23047 if (mode_line_target == MODE_LINE_NOPROP
23048 || mode_line_target == MODE_LINE_STRING)
23049 return "--";
23050 if (field_width <= 0
23051 || field_width > sizeof (lots_of_dashes))
23053 for (i = 0; i < FRAME_MESSAGE_BUF_SIZE (f) - 1; ++i)
23054 decode_mode_spec_buf[i] = '-';
23055 decode_mode_spec_buf[i] = '\0';
23056 return decode_mode_spec_buf;
23058 else
23059 return lots_of_dashes;
23062 case 'b':
23063 obj = BVAR (b, name);
23064 break;
23066 case 'c':
23067 /* %c and %l are ignored in `frame-title-format'.
23068 (In redisplay_internal, the frame title is drawn _before_ the
23069 windows are updated, so the stuff which depends on actual
23070 window contents (such as %l) may fail to render properly, or
23071 even crash emacs.) */
23072 if (mode_line_target == MODE_LINE_TITLE)
23073 return "";
23074 else
23076 ptrdiff_t col = current_column ();
23077 w->column_number_displayed = col;
23078 pint2str (decode_mode_spec_buf, width, col);
23079 return decode_mode_spec_buf;
23082 case 'e':
23083 #if !defined SYSTEM_MALLOC && !defined HYBRID_MALLOC
23085 if (NILP (Vmemory_full))
23086 return "";
23087 else
23088 return "!MEM FULL! ";
23090 #else
23091 return "";
23092 #endif
23094 case 'F':
23095 /* %F displays the frame name. */
23096 if (!NILP (f->title))
23097 return SSDATA (f->title);
23098 if (f->explicit_name || ! FRAME_WINDOW_P (f))
23099 return SSDATA (f->name);
23100 return "Emacs";
23102 case 'f':
23103 obj = BVAR (b, filename);
23104 break;
23106 case 'i':
23108 ptrdiff_t size = ZV - BEGV;
23109 pint2str (decode_mode_spec_buf, width, size);
23110 return decode_mode_spec_buf;
23113 case 'I':
23115 ptrdiff_t size = ZV - BEGV;
23116 pint2hrstr (decode_mode_spec_buf, width, size);
23117 return decode_mode_spec_buf;
23120 case 'l':
23122 ptrdiff_t startpos, startpos_byte, line, linepos, linepos_byte;
23123 ptrdiff_t topline, nlines, height;
23124 ptrdiff_t junk;
23126 /* %c and %l are ignored in `frame-title-format'. */
23127 if (mode_line_target == MODE_LINE_TITLE)
23128 return "";
23130 startpos = marker_position (w->start);
23131 startpos_byte = marker_byte_position (w->start);
23132 height = WINDOW_TOTAL_LINES (w);
23134 /* If we decided that this buffer isn't suitable for line numbers,
23135 don't forget that too fast. */
23136 if (w->base_line_pos == -1)
23137 goto no_value;
23139 /* If the buffer is very big, don't waste time. */
23140 if (INTEGERP (Vline_number_display_limit)
23141 && BUF_ZV (b) - BUF_BEGV (b) > XINT (Vline_number_display_limit))
23143 w->base_line_pos = 0;
23144 w->base_line_number = 0;
23145 goto no_value;
23148 if (w->base_line_number > 0
23149 && w->base_line_pos > 0
23150 && w->base_line_pos <= startpos)
23152 line = w->base_line_number;
23153 linepos = w->base_line_pos;
23154 linepos_byte = buf_charpos_to_bytepos (b, linepos);
23156 else
23158 line = 1;
23159 linepos = BUF_BEGV (b);
23160 linepos_byte = BUF_BEGV_BYTE (b);
23163 /* Count lines from base line to window start position. */
23164 nlines = display_count_lines (linepos_byte,
23165 startpos_byte,
23166 startpos, &junk);
23168 topline = nlines + line;
23170 /* Determine a new base line, if the old one is too close
23171 or too far away, or if we did not have one.
23172 "Too close" means it's plausible a scroll-down would
23173 go back past it. */
23174 if (startpos == BUF_BEGV (b))
23176 w->base_line_number = topline;
23177 w->base_line_pos = BUF_BEGV (b);
23179 else if (nlines < height + 25 || nlines > height * 3 + 50
23180 || linepos == BUF_BEGV (b))
23182 ptrdiff_t limit = BUF_BEGV (b);
23183 ptrdiff_t limit_byte = BUF_BEGV_BYTE (b);
23184 ptrdiff_t position;
23185 ptrdiff_t distance =
23186 (height * 2 + 30) * line_number_display_limit_width;
23188 if (startpos - distance > limit)
23190 limit = startpos - distance;
23191 limit_byte = CHAR_TO_BYTE (limit);
23194 nlines = display_count_lines (startpos_byte,
23195 limit_byte,
23196 - (height * 2 + 30),
23197 &position);
23198 /* If we couldn't find the lines we wanted within
23199 line_number_display_limit_width chars per line,
23200 give up on line numbers for this window. */
23201 if (position == limit_byte && limit == startpos - distance)
23203 w->base_line_pos = -1;
23204 w->base_line_number = 0;
23205 goto no_value;
23208 w->base_line_number = topline - nlines;
23209 w->base_line_pos = BYTE_TO_CHAR (position);
23212 /* Now count lines from the start pos to point. */
23213 nlines = display_count_lines (startpos_byte,
23214 PT_BYTE, PT, &junk);
23216 /* Record that we did display the line number. */
23217 line_number_displayed = 1;
23219 /* Make the string to show. */
23220 pint2str (decode_mode_spec_buf, width, topline + nlines);
23221 return decode_mode_spec_buf;
23222 no_value:
23224 char *p = decode_mode_spec_buf;
23225 int pad = width - 2;
23226 while (pad-- > 0)
23227 *p++ = ' ';
23228 *p++ = '?';
23229 *p++ = '?';
23230 *p = '\0';
23231 return decode_mode_spec_buf;
23234 break;
23236 case 'm':
23237 obj = BVAR (b, mode_name);
23238 break;
23240 case 'n':
23241 if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
23242 return " Narrow";
23243 break;
23245 case 'p':
23247 ptrdiff_t pos = marker_position (w->start);
23248 ptrdiff_t total = BUF_ZV (b) - BUF_BEGV (b);
23250 if (w->window_end_pos <= BUF_Z (b) - BUF_ZV (b))
23252 if (pos <= BUF_BEGV (b))
23253 return "All";
23254 else
23255 return "Bottom";
23257 else if (pos <= BUF_BEGV (b))
23258 return "Top";
23259 else
23261 if (total > 1000000)
23262 /* Do it differently for a large value, to avoid overflow. */
23263 total = ((pos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
23264 else
23265 total = ((pos - BUF_BEGV (b)) * 100 + total - 1) / total;
23266 /* We can't normally display a 3-digit number,
23267 so get us a 2-digit number that is close. */
23268 if (total == 100)
23269 total = 99;
23270 sprintf (decode_mode_spec_buf, "%2"pD"d%%", total);
23271 return decode_mode_spec_buf;
23275 /* Display percentage of size above the bottom of the screen. */
23276 case 'P':
23278 ptrdiff_t toppos = marker_position (w->start);
23279 ptrdiff_t botpos = BUF_Z (b) - w->window_end_pos;
23280 ptrdiff_t total = BUF_ZV (b) - BUF_BEGV (b);
23282 if (botpos >= BUF_ZV (b))
23284 if (toppos <= BUF_BEGV (b))
23285 return "All";
23286 else
23287 return "Bottom";
23289 else
23291 if (total > 1000000)
23292 /* Do it differently for a large value, to avoid overflow. */
23293 total = ((botpos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
23294 else
23295 total = ((botpos - BUF_BEGV (b)) * 100 + total - 1) / total;
23296 /* We can't normally display a 3-digit number,
23297 so get us a 2-digit number that is close. */
23298 if (total == 100)
23299 total = 99;
23300 if (toppos <= BUF_BEGV (b))
23301 sprintf (decode_mode_spec_buf, "Top%2"pD"d%%", total);
23302 else
23303 sprintf (decode_mode_spec_buf, "%2"pD"d%%", total);
23304 return decode_mode_spec_buf;
23308 case 's':
23309 /* status of process */
23310 obj = Fget_buffer_process (Fcurrent_buffer ());
23311 if (NILP (obj))
23312 return "no process";
23313 #ifndef MSDOS
23314 obj = Fsymbol_name (Fprocess_status (obj));
23315 #endif
23316 break;
23318 case '@':
23320 ptrdiff_t count = inhibit_garbage_collection ();
23321 Lisp_Object curdir = BVAR (current_buffer, directory);
23322 Lisp_Object val = Qnil;
23324 if (STRINGP (curdir))
23325 val = call1 (intern ("file-remote-p"), curdir);
23327 unbind_to (count, Qnil);
23329 if (NILP (val))
23330 return "-";
23331 else
23332 return "@";
23335 case 'z':
23336 /* coding-system (not including end-of-line format) */
23337 case 'Z':
23338 /* coding-system (including end-of-line type) */
23340 int eol_flag = (c == 'Z');
23341 char *p = decode_mode_spec_buf;
23343 if (! FRAME_WINDOW_P (f))
23345 /* No need to mention EOL here--the terminal never needs
23346 to do EOL conversion. */
23347 p = decode_mode_spec_coding (CODING_ID_NAME
23348 (FRAME_KEYBOARD_CODING (f)->id),
23349 p, 0);
23350 p = decode_mode_spec_coding (CODING_ID_NAME
23351 (FRAME_TERMINAL_CODING (f)->id),
23352 p, 0);
23354 p = decode_mode_spec_coding (BVAR (b, buffer_file_coding_system),
23355 p, eol_flag);
23357 #if 0 /* This proves to be annoying; I think we can do without. -- rms. */
23358 #ifdef subprocesses
23359 obj = Fget_buffer_process (Fcurrent_buffer ());
23360 if (PROCESSP (obj))
23362 p = decode_mode_spec_coding
23363 (XPROCESS (obj)->decode_coding_system, p, eol_flag);
23364 p = decode_mode_spec_coding
23365 (XPROCESS (obj)->encode_coding_system, p, eol_flag);
23367 #endif /* subprocesses */
23368 #endif /* 0 */
23369 *p = 0;
23370 return decode_mode_spec_buf;
23374 if (STRINGP (obj))
23376 *string = obj;
23377 return SSDATA (obj);
23379 else
23380 return "";
23384 /* Count up to COUNT lines starting from START_BYTE. COUNT negative
23385 means count lines back from START_BYTE. But don't go beyond
23386 LIMIT_BYTE. Return the number of lines thus found (always
23387 nonnegative).
23389 Set *BYTE_POS_PTR to the byte position where we stopped. This is
23390 either the position COUNT lines after/before START_BYTE, if we
23391 found COUNT lines, or LIMIT_BYTE if we hit the limit before finding
23392 COUNT lines. */
23394 static ptrdiff_t
23395 display_count_lines (ptrdiff_t start_byte,
23396 ptrdiff_t limit_byte, ptrdiff_t count,
23397 ptrdiff_t *byte_pos_ptr)
23399 register unsigned char *cursor;
23400 unsigned char *base;
23402 register ptrdiff_t ceiling;
23403 register unsigned char *ceiling_addr;
23404 ptrdiff_t orig_count = count;
23406 /* If we are not in selective display mode,
23407 check only for newlines. */
23408 int selective_display = (!NILP (BVAR (current_buffer, selective_display))
23409 && !INTEGERP (BVAR (current_buffer, selective_display)));
23411 if (count > 0)
23413 while (start_byte < limit_byte)
23415 ceiling = BUFFER_CEILING_OF (start_byte);
23416 ceiling = min (limit_byte - 1, ceiling);
23417 ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
23418 base = (cursor = BYTE_POS_ADDR (start_byte));
23422 if (selective_display)
23424 while (*cursor != '\n' && *cursor != 015
23425 && ++cursor != ceiling_addr)
23426 continue;
23427 if (cursor == ceiling_addr)
23428 break;
23430 else
23432 cursor = memchr (cursor, '\n', ceiling_addr - cursor);
23433 if (! cursor)
23434 break;
23437 cursor++;
23439 if (--count == 0)
23441 start_byte += cursor - base;
23442 *byte_pos_ptr = start_byte;
23443 return orig_count;
23446 while (cursor < ceiling_addr);
23448 start_byte += ceiling_addr - base;
23451 else
23453 while (start_byte > limit_byte)
23455 ceiling = BUFFER_FLOOR_OF (start_byte - 1);
23456 ceiling = max (limit_byte, ceiling);
23457 ceiling_addr = BYTE_POS_ADDR (ceiling);
23458 base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
23459 while (1)
23461 if (selective_display)
23463 while (--cursor >= ceiling_addr
23464 && *cursor != '\n' && *cursor != 015)
23465 continue;
23466 if (cursor < ceiling_addr)
23467 break;
23469 else
23471 cursor = memrchr (ceiling_addr, '\n', cursor - ceiling_addr);
23472 if (! cursor)
23473 break;
23476 if (++count == 0)
23478 start_byte += cursor - base + 1;
23479 *byte_pos_ptr = start_byte;
23480 /* When scanning backwards, we should
23481 not count the newline posterior to which we stop. */
23482 return - orig_count - 1;
23485 start_byte += ceiling_addr - base;
23489 *byte_pos_ptr = limit_byte;
23491 if (count < 0)
23492 return - orig_count + count;
23493 return orig_count - count;
23499 /***********************************************************************
23500 Displaying strings
23501 ***********************************************************************/
23503 /* Display a NUL-terminated string, starting with index START.
23505 If STRING is non-null, display that C string. Otherwise, the Lisp
23506 string LISP_STRING is displayed. There's a case that STRING is
23507 non-null and LISP_STRING is not nil. It means STRING is a string
23508 data of LISP_STRING. In that case, we display LISP_STRING while
23509 ignoring its text properties.
23511 If FACE_STRING is not nil, FACE_STRING_POS is a position in
23512 FACE_STRING. Display STRING or LISP_STRING with the face at
23513 FACE_STRING_POS in FACE_STRING:
23515 Display the string in the environment given by IT, but use the
23516 standard display table, temporarily.
23518 FIELD_WIDTH is the minimum number of output glyphs to produce.
23519 If STRING has fewer characters than FIELD_WIDTH, pad to the right
23520 with spaces. If STRING has more characters, more than FIELD_WIDTH
23521 glyphs will be produced. FIELD_WIDTH <= 0 means don't pad.
23523 PRECISION is the maximum number of characters to output from
23524 STRING. PRECISION < 0 means don't truncate the string.
23526 This is roughly equivalent to printf format specifiers:
23528 FIELD_WIDTH PRECISION PRINTF
23529 ----------------------------------------
23530 -1 -1 %s
23531 -1 10 %.10s
23532 10 -1 %10s
23533 20 10 %20.10s
23535 MULTIBYTE zero means do not display multibyte chars, > 0 means do
23536 display them, and < 0 means obey the current buffer's value of
23537 enable_multibyte_characters.
23539 Value is the number of columns displayed. */
23541 static int
23542 display_string (const char *string, Lisp_Object lisp_string, Lisp_Object face_string,
23543 ptrdiff_t face_string_pos, ptrdiff_t start, struct it *it,
23544 int field_width, int precision, int max_x, int multibyte)
23546 int hpos_at_start = it->hpos;
23547 int saved_face_id = it->face_id;
23548 struct glyph_row *row = it->glyph_row;
23549 ptrdiff_t it_charpos;
23551 /* Initialize the iterator IT for iteration over STRING beginning
23552 with index START. */
23553 reseat_to_string (it, NILP (lisp_string) ? string : NULL, lisp_string, start,
23554 precision, field_width, multibyte);
23555 if (string && STRINGP (lisp_string))
23556 /* LISP_STRING is the one returned by decode_mode_spec. We should
23557 ignore its text properties. */
23558 it->stop_charpos = it->end_charpos;
23560 /* If displaying STRING, set up the face of the iterator from
23561 FACE_STRING, if that's given. */
23562 if (STRINGP (face_string))
23564 ptrdiff_t endptr;
23565 struct face *face;
23567 it->face_id
23568 = face_at_string_position (it->w, face_string, face_string_pos,
23569 0, &endptr, it->base_face_id, 0);
23570 face = FACE_FROM_ID (it->f, it->face_id);
23571 it->face_box_p = face->box != FACE_NO_BOX;
23574 /* Set max_x to the maximum allowed X position. Don't let it go
23575 beyond the right edge of the window. */
23576 if (max_x <= 0)
23577 max_x = it->last_visible_x;
23578 else
23579 max_x = min (max_x, it->last_visible_x);
23581 /* Skip over display elements that are not visible. because IT->w is
23582 hscrolled. */
23583 if (it->current_x < it->first_visible_x)
23584 move_it_in_display_line_to (it, 100000, it->first_visible_x,
23585 MOVE_TO_POS | MOVE_TO_X);
23587 row->ascent = it->max_ascent;
23588 row->height = it->max_ascent + it->max_descent;
23589 row->phys_ascent = it->max_phys_ascent;
23590 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
23591 row->extra_line_spacing = it->max_extra_line_spacing;
23593 if (STRINGP (it->string))
23594 it_charpos = IT_STRING_CHARPOS (*it);
23595 else
23596 it_charpos = IT_CHARPOS (*it);
23598 /* This condition is for the case that we are called with current_x
23599 past last_visible_x. */
23600 while (it->current_x < max_x)
23602 int x_before, x, n_glyphs_before, i, nglyphs;
23604 /* Get the next display element. */
23605 if (!get_next_display_element (it))
23606 break;
23608 /* Produce glyphs. */
23609 x_before = it->current_x;
23610 n_glyphs_before = row->used[TEXT_AREA];
23611 PRODUCE_GLYPHS (it);
23613 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
23614 i = 0;
23615 x = x_before;
23616 while (i < nglyphs)
23618 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
23620 if (it->line_wrap != TRUNCATE
23621 && x + glyph->pixel_width > max_x)
23623 /* End of continued line or max_x reached. */
23624 if (CHAR_GLYPH_PADDING_P (*glyph))
23626 /* A wide character is unbreakable. */
23627 if (row->reversed_p)
23628 unproduce_glyphs (it, row->used[TEXT_AREA]
23629 - n_glyphs_before);
23630 row->used[TEXT_AREA] = n_glyphs_before;
23631 it->current_x = x_before;
23633 else
23635 if (row->reversed_p)
23636 unproduce_glyphs (it, row->used[TEXT_AREA]
23637 - (n_glyphs_before + i));
23638 row->used[TEXT_AREA] = n_glyphs_before + i;
23639 it->current_x = x;
23641 break;
23643 else if (x + glyph->pixel_width >= it->first_visible_x)
23645 /* Glyph is at least partially visible. */
23646 ++it->hpos;
23647 if (x < it->first_visible_x)
23648 row->x = x - it->first_visible_x;
23650 else
23652 /* Glyph is off the left margin of the display area.
23653 Should not happen. */
23654 emacs_abort ();
23657 row->ascent = max (row->ascent, it->max_ascent);
23658 row->height = max (row->height, it->max_ascent + it->max_descent);
23659 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
23660 row->phys_height = max (row->phys_height,
23661 it->max_phys_ascent + it->max_phys_descent);
23662 row->extra_line_spacing = max (row->extra_line_spacing,
23663 it->max_extra_line_spacing);
23664 x += glyph->pixel_width;
23665 ++i;
23668 /* Stop if max_x reached. */
23669 if (i < nglyphs)
23670 break;
23672 /* Stop at line ends. */
23673 if (ITERATOR_AT_END_OF_LINE_P (it))
23675 it->continuation_lines_width = 0;
23676 break;
23679 set_iterator_to_next (it, 1);
23680 if (STRINGP (it->string))
23681 it_charpos = IT_STRING_CHARPOS (*it);
23682 else
23683 it_charpos = IT_CHARPOS (*it);
23685 /* Stop if truncating at the right edge. */
23686 if (it->line_wrap == TRUNCATE
23687 && it->current_x >= it->last_visible_x)
23689 /* Add truncation mark, but don't do it if the line is
23690 truncated at a padding space. */
23691 if (it_charpos < it->string_nchars)
23693 if (!FRAME_WINDOW_P (it->f))
23695 int ii, n;
23697 if (it->current_x > it->last_visible_x)
23699 if (!row->reversed_p)
23701 for (ii = row->used[TEXT_AREA] - 1; ii > 0; --ii)
23702 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][ii]))
23703 break;
23705 else
23707 for (ii = 0; ii < row->used[TEXT_AREA]; ii++)
23708 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][ii]))
23709 break;
23710 unproduce_glyphs (it, ii + 1);
23711 ii = row->used[TEXT_AREA] - (ii + 1);
23713 for (n = row->used[TEXT_AREA]; ii < n; ++ii)
23715 row->used[TEXT_AREA] = ii;
23716 produce_special_glyphs (it, IT_TRUNCATION);
23719 produce_special_glyphs (it, IT_TRUNCATION);
23721 row->truncated_on_right_p = 1;
23723 break;
23727 /* Maybe insert a truncation at the left. */
23728 if (it->first_visible_x
23729 && it_charpos > 0)
23731 if (!FRAME_WINDOW_P (it->f)
23732 || (row->reversed_p
23733 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
23734 : WINDOW_LEFT_FRINGE_WIDTH (it->w)) == 0)
23735 insert_left_trunc_glyphs (it);
23736 row->truncated_on_left_p = 1;
23739 it->face_id = saved_face_id;
23741 /* Value is number of columns displayed. */
23742 return it->hpos - hpos_at_start;
23747 /* This is like a combination of memq and assq. Return 1/2 if PROPVAL
23748 appears as an element of LIST or as the car of an element of LIST.
23749 If PROPVAL is a list, compare each element against LIST in that
23750 way, and return 1/2 if any element of PROPVAL is found in LIST.
23751 Otherwise return 0. This function cannot quit.
23752 The return value is 2 if the text is invisible but with an ellipsis
23753 and 1 if it's invisible and without an ellipsis. */
23756 invisible_p (register Lisp_Object propval, Lisp_Object list)
23758 register Lisp_Object tail, proptail;
23760 for (tail = list; CONSP (tail); tail = XCDR (tail))
23762 register Lisp_Object tem;
23763 tem = XCAR (tail);
23764 if (EQ (propval, tem))
23765 return 1;
23766 if (CONSP (tem) && EQ (propval, XCAR (tem)))
23767 return NILP (XCDR (tem)) ? 1 : 2;
23770 if (CONSP (propval))
23772 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
23774 Lisp_Object propelt;
23775 propelt = XCAR (proptail);
23776 for (tail = list; CONSP (tail); tail = XCDR (tail))
23778 register Lisp_Object tem;
23779 tem = XCAR (tail);
23780 if (EQ (propelt, tem))
23781 return 1;
23782 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
23783 return NILP (XCDR (tem)) ? 1 : 2;
23788 return 0;
23791 DEFUN ("invisible-p", Finvisible_p, Sinvisible_p, 1, 1, 0,
23792 doc: /* Non-nil if the property makes the text invisible.
23793 POS-OR-PROP can be a marker or number, in which case it is taken to be
23794 a position in the current buffer and the value of the `invisible' property
23795 is checked; or it can be some other value, which is then presumed to be the
23796 value of the `invisible' property of the text of interest.
23797 The non-nil value returned can be t for truly invisible text or something
23798 else if the text is replaced by an ellipsis. */)
23799 (Lisp_Object pos_or_prop)
23801 Lisp_Object prop
23802 = (NATNUMP (pos_or_prop) || MARKERP (pos_or_prop)
23803 ? Fget_char_property (pos_or_prop, Qinvisible, Qnil)
23804 : pos_or_prop);
23805 int invis = TEXT_PROP_MEANS_INVISIBLE (prop);
23806 return (invis == 0 ? Qnil
23807 : invis == 1 ? Qt
23808 : make_number (invis));
23811 /* Calculate a width or height in pixels from a specification using
23812 the following elements:
23814 SPEC ::=
23815 NUM - a (fractional) multiple of the default font width/height
23816 (NUM) - specifies exactly NUM pixels
23817 UNIT - a fixed number of pixels, see below.
23818 ELEMENT - size of a display element in pixels, see below.
23819 (NUM . SPEC) - equals NUM * SPEC
23820 (+ SPEC SPEC ...) - add pixel values
23821 (- SPEC SPEC ...) - subtract pixel values
23822 (- SPEC) - negate pixel value
23824 NUM ::=
23825 INT or FLOAT - a number constant
23826 SYMBOL - use symbol's (buffer local) variable binding.
23828 UNIT ::=
23829 in - pixels per inch *)
23830 mm - pixels per 1/1000 meter *)
23831 cm - pixels per 1/100 meter *)
23832 width - width of current font in pixels.
23833 height - height of current font in pixels.
23835 *) using the ratio(s) defined in display-pixels-per-inch.
23837 ELEMENT ::=
23839 left-fringe - left fringe width in pixels
23840 right-fringe - right fringe width in pixels
23842 left-margin - left margin width in pixels
23843 right-margin - right margin width in pixels
23845 scroll-bar - scroll-bar area width in pixels
23847 Examples:
23849 Pixels corresponding to 5 inches:
23850 (5 . in)
23852 Total width of non-text areas on left side of window (if scroll-bar is on left):
23853 '(space :width (+ left-fringe left-margin scroll-bar))
23855 Align to first text column (in header line):
23856 '(space :align-to 0)
23858 Align to middle of text area minus half the width of variable `my-image'
23859 containing a loaded image:
23860 '(space :align-to (0.5 . (- text my-image)))
23862 Width of left margin minus width of 1 character in the default font:
23863 '(space :width (- left-margin 1))
23865 Width of left margin minus width of 2 characters in the current font:
23866 '(space :width (- left-margin (2 . width)))
23868 Center 1 character over left-margin (in header line):
23869 '(space :align-to (+ left-margin (0.5 . left-margin) -0.5))
23871 Different ways to express width of left fringe plus left margin minus one pixel:
23872 '(space :width (- (+ left-fringe left-margin) (1)))
23873 '(space :width (+ left-fringe left-margin (- (1))))
23874 '(space :width (+ left-fringe left-margin (-1)))
23878 static int
23879 calc_pixel_width_or_height (double *res, struct it *it, Lisp_Object prop,
23880 struct font *font, int width_p, int *align_to)
23882 double pixels;
23884 #define OK_PIXELS(val) ((*res = (double)(val)), 1)
23885 #define OK_ALIGN_TO(val) ((*align_to = (int)(val)), 1)
23887 if (NILP (prop))
23888 return OK_PIXELS (0);
23890 eassert (FRAME_LIVE_P (it->f));
23892 if (SYMBOLP (prop))
23894 if (SCHARS (SYMBOL_NAME (prop)) == 2)
23896 char *unit = SSDATA (SYMBOL_NAME (prop));
23898 if (unit[0] == 'i' && unit[1] == 'n')
23899 pixels = 1.0;
23900 else if (unit[0] == 'm' && unit[1] == 'm')
23901 pixels = 25.4;
23902 else if (unit[0] == 'c' && unit[1] == 'm')
23903 pixels = 2.54;
23904 else
23905 pixels = 0;
23906 if (pixels > 0)
23908 double ppi = (width_p ? FRAME_RES_X (it->f)
23909 : FRAME_RES_Y (it->f));
23911 if (ppi > 0)
23912 return OK_PIXELS (ppi / pixels);
23913 return 0;
23917 #ifdef HAVE_WINDOW_SYSTEM
23918 if (EQ (prop, Qheight))
23919 return OK_PIXELS (font ? FONT_HEIGHT (font) : FRAME_LINE_HEIGHT (it->f));
23920 if (EQ (prop, Qwidth))
23921 return OK_PIXELS (font ? FONT_WIDTH (font) : FRAME_COLUMN_WIDTH (it->f));
23922 #else
23923 if (EQ (prop, Qheight) || EQ (prop, Qwidth))
23924 return OK_PIXELS (1);
23925 #endif
23927 if (EQ (prop, Qtext))
23928 return OK_PIXELS (width_p
23929 ? window_box_width (it->w, TEXT_AREA)
23930 : WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w));
23932 if (align_to && *align_to < 0)
23934 *res = 0;
23935 if (EQ (prop, Qleft))
23936 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA));
23937 if (EQ (prop, Qright))
23938 return OK_ALIGN_TO (window_box_right_offset (it->w, TEXT_AREA));
23939 if (EQ (prop, Qcenter))
23940 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA)
23941 + window_box_width (it->w, TEXT_AREA) / 2);
23942 if (EQ (prop, Qleft_fringe))
23943 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
23944 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (it->w)
23945 : window_box_right_offset (it->w, LEFT_MARGIN_AREA));
23946 if (EQ (prop, Qright_fringe))
23947 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
23948 ? window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
23949 : window_box_right_offset (it->w, TEXT_AREA));
23950 if (EQ (prop, Qleft_margin))
23951 return OK_ALIGN_TO (window_box_left_offset (it->w, LEFT_MARGIN_AREA));
23952 if (EQ (prop, Qright_margin))
23953 return OK_ALIGN_TO (window_box_left_offset (it->w, RIGHT_MARGIN_AREA));
23954 if (EQ (prop, Qscroll_bar))
23955 return OK_ALIGN_TO (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (it->w)
23957 : (window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
23958 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
23959 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
23960 : 0)));
23962 else
23964 if (EQ (prop, Qleft_fringe))
23965 return OK_PIXELS (WINDOW_LEFT_FRINGE_WIDTH (it->w));
23966 if (EQ (prop, Qright_fringe))
23967 return OK_PIXELS (WINDOW_RIGHT_FRINGE_WIDTH (it->w));
23968 if (EQ (prop, Qleft_margin))
23969 return OK_PIXELS (WINDOW_LEFT_MARGIN_WIDTH (it->w));
23970 if (EQ (prop, Qright_margin))
23971 return OK_PIXELS (WINDOW_RIGHT_MARGIN_WIDTH (it->w));
23972 if (EQ (prop, Qscroll_bar))
23973 return OK_PIXELS (WINDOW_SCROLL_BAR_AREA_WIDTH (it->w));
23976 prop = buffer_local_value (prop, it->w->contents);
23977 if (EQ (prop, Qunbound))
23978 prop = Qnil;
23981 if (INTEGERP (prop) || FLOATP (prop))
23983 int base_unit = (width_p
23984 ? FRAME_COLUMN_WIDTH (it->f)
23985 : FRAME_LINE_HEIGHT (it->f));
23986 return OK_PIXELS (XFLOATINT (prop) * base_unit);
23989 if (CONSP (prop))
23991 Lisp_Object car = XCAR (prop);
23992 Lisp_Object cdr = XCDR (prop);
23994 if (SYMBOLP (car))
23996 #ifdef HAVE_WINDOW_SYSTEM
23997 if (FRAME_WINDOW_P (it->f)
23998 && valid_image_p (prop))
24000 ptrdiff_t id = lookup_image (it->f, prop);
24001 struct image *img = IMAGE_FROM_ID (it->f, id);
24003 return OK_PIXELS (width_p ? img->width : img->height);
24005 #endif
24006 if (EQ (car, Qplus) || EQ (car, Qminus))
24008 int first = 1;
24009 double px;
24011 pixels = 0;
24012 while (CONSP (cdr))
24014 if (!calc_pixel_width_or_height (&px, it, XCAR (cdr),
24015 font, width_p, align_to))
24016 return 0;
24017 if (first)
24018 pixels = (EQ (car, Qplus) ? px : -px), first = 0;
24019 else
24020 pixels += px;
24021 cdr = XCDR (cdr);
24023 if (EQ (car, Qminus))
24024 pixels = -pixels;
24025 return OK_PIXELS (pixels);
24028 car = buffer_local_value (car, it->w->contents);
24029 if (EQ (car, Qunbound))
24030 car = Qnil;
24033 if (INTEGERP (car) || FLOATP (car))
24035 double fact;
24036 pixels = XFLOATINT (car);
24037 if (NILP (cdr))
24038 return OK_PIXELS (pixels);
24039 if (calc_pixel_width_or_height (&fact, it, cdr,
24040 font, width_p, align_to))
24041 return OK_PIXELS (pixels * fact);
24042 return 0;
24045 return 0;
24048 return 0;
24052 /***********************************************************************
24053 Glyph Display
24054 ***********************************************************************/
24056 #ifdef HAVE_WINDOW_SYSTEM
24058 #ifdef GLYPH_DEBUG
24060 void
24061 dump_glyph_string (struct glyph_string *s)
24063 fprintf (stderr, "glyph string\n");
24064 fprintf (stderr, " x, y, w, h = %d, %d, %d, %d\n",
24065 s->x, s->y, s->width, s->height);
24066 fprintf (stderr, " ybase = %d\n", s->ybase);
24067 fprintf (stderr, " hl = %d\n", s->hl);
24068 fprintf (stderr, " left overhang = %d, right = %d\n",
24069 s->left_overhang, s->right_overhang);
24070 fprintf (stderr, " nchars = %d\n", s->nchars);
24071 fprintf (stderr, " extends to end of line = %d\n",
24072 s->extends_to_end_of_line_p);
24073 fprintf (stderr, " font height = %d\n", FONT_HEIGHT (s->font));
24074 fprintf (stderr, " bg width = %d\n", s->background_width);
24077 #endif /* GLYPH_DEBUG */
24079 /* Initialize glyph string S. CHAR2B is a suitably allocated vector
24080 of XChar2b structures for S; it can't be allocated in
24081 init_glyph_string because it must be allocated via `alloca'. W
24082 is the window on which S is drawn. ROW and AREA are the glyph row
24083 and area within the row from which S is constructed. START is the
24084 index of the first glyph structure covered by S. HL is a
24085 face-override for drawing S. */
24087 #ifdef HAVE_NTGUI
24088 #define OPTIONAL_HDC(hdc) HDC hdc,
24089 #define DECLARE_HDC(hdc) HDC hdc;
24090 #define ALLOCATE_HDC(hdc, f) hdc = get_frame_dc ((f))
24091 #define RELEASE_HDC(hdc, f) release_frame_dc ((f), (hdc))
24092 #endif
24094 #ifndef OPTIONAL_HDC
24095 #define OPTIONAL_HDC(hdc)
24096 #define DECLARE_HDC(hdc)
24097 #define ALLOCATE_HDC(hdc, f)
24098 #define RELEASE_HDC(hdc, f)
24099 #endif
24101 static void
24102 init_glyph_string (struct glyph_string *s,
24103 OPTIONAL_HDC (hdc)
24104 XChar2b *char2b, struct window *w, struct glyph_row *row,
24105 enum glyph_row_area area, int start, enum draw_glyphs_face hl)
24107 memset (s, 0, sizeof *s);
24108 s->w = w;
24109 s->f = XFRAME (w->frame);
24110 #ifdef HAVE_NTGUI
24111 s->hdc = hdc;
24112 #endif
24113 s->display = FRAME_X_DISPLAY (s->f);
24114 s->window = FRAME_X_WINDOW (s->f);
24115 s->char2b = char2b;
24116 s->hl = hl;
24117 s->row = row;
24118 s->area = area;
24119 s->first_glyph = row->glyphs[area] + start;
24120 s->height = row->height;
24121 s->y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
24122 s->ybase = s->y + row->ascent;
24126 /* Append the list of glyph strings with head H and tail T to the list
24127 with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the result. */
24129 static void
24130 append_glyph_string_lists (struct glyph_string **head, struct glyph_string **tail,
24131 struct glyph_string *h, struct glyph_string *t)
24133 if (h)
24135 if (*head)
24136 (*tail)->next = h;
24137 else
24138 *head = h;
24139 h->prev = *tail;
24140 *tail = t;
24145 /* Prepend the list of glyph strings with head H and tail T to the
24146 list with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the
24147 result. */
24149 static void
24150 prepend_glyph_string_lists (struct glyph_string **head, struct glyph_string **tail,
24151 struct glyph_string *h, struct glyph_string *t)
24153 if (h)
24155 if (*head)
24156 (*head)->prev = t;
24157 else
24158 *tail = t;
24159 t->next = *head;
24160 *head = h;
24165 /* Append glyph string S to the list with head *HEAD and tail *TAIL.
24166 Set *HEAD and *TAIL to the resulting list. */
24168 static void
24169 append_glyph_string (struct glyph_string **head, struct glyph_string **tail,
24170 struct glyph_string *s)
24172 s->next = s->prev = NULL;
24173 append_glyph_string_lists (head, tail, s, s);
24177 /* Get face and two-byte form of character C in face FACE_ID on frame F.
24178 The encoding of C is returned in *CHAR2B. DISPLAY_P non-zero means
24179 make sure that X resources for the face returned are allocated.
24180 Value is a pointer to a realized face that is ready for display if
24181 DISPLAY_P is non-zero. */
24183 static struct face *
24184 get_char_face_and_encoding (struct frame *f, int c, int face_id,
24185 XChar2b *char2b, int display_p)
24187 struct face *face = FACE_FROM_ID (f, face_id);
24188 unsigned code = 0;
24190 if (face->font)
24192 code = face->font->driver->encode_char (face->font, c);
24194 if (code == FONT_INVALID_CODE)
24195 code = 0;
24197 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
24199 /* Make sure X resources of the face are allocated. */
24200 #ifdef HAVE_X_WINDOWS
24201 if (display_p)
24202 #endif
24204 eassert (face != NULL);
24205 prepare_face_for_display (f, face);
24208 return face;
24212 /* Get face and two-byte form of character glyph GLYPH on frame F.
24213 The encoding of GLYPH->u.ch is returned in *CHAR2B. Value is
24214 a pointer to a realized face that is ready for display. */
24216 static struct face *
24217 get_glyph_face_and_encoding (struct frame *f, struct glyph *glyph,
24218 XChar2b *char2b, int *two_byte_p)
24220 struct face *face;
24221 unsigned code = 0;
24223 eassert (glyph->type == CHAR_GLYPH);
24224 face = FACE_FROM_ID (f, glyph->face_id);
24226 /* Make sure X resources of the face are allocated. */
24227 eassert (face != NULL);
24228 prepare_face_for_display (f, face);
24230 if (two_byte_p)
24231 *two_byte_p = 0;
24233 if (face->font)
24235 if (CHAR_BYTE8_P (glyph->u.ch))
24236 code = CHAR_TO_BYTE8 (glyph->u.ch);
24237 else
24238 code = face->font->driver->encode_char (face->font, glyph->u.ch);
24240 if (code == FONT_INVALID_CODE)
24241 code = 0;
24244 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
24245 return face;
24249 /* Get glyph code of character C in FONT in the two-byte form CHAR2B.
24250 Return 1 if FONT has a glyph for C, otherwise return 0. */
24252 static int
24253 get_char_glyph_code (int c, struct font *font, XChar2b *char2b)
24255 unsigned code;
24257 if (CHAR_BYTE8_P (c))
24258 code = CHAR_TO_BYTE8 (c);
24259 else
24260 code = font->driver->encode_char (font, c);
24262 if (code == FONT_INVALID_CODE)
24263 return 0;
24264 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
24265 return 1;
24269 /* Fill glyph string S with composition components specified by S->cmp.
24271 BASE_FACE is the base face of the composition.
24272 S->cmp_from is the index of the first component for S.
24274 OVERLAPS non-zero means S should draw the foreground only, and use
24275 its physical height for clipping. See also draw_glyphs.
24277 Value is the index of a component not in S. */
24279 static int
24280 fill_composite_glyph_string (struct glyph_string *s, struct face *base_face,
24281 int overlaps)
24283 int i;
24284 /* For all glyphs of this composition, starting at the offset
24285 S->cmp_from, until we reach the end of the definition or encounter a
24286 glyph that requires the different face, add it to S. */
24287 struct face *face;
24289 eassert (s);
24291 s->for_overlaps = overlaps;
24292 s->face = NULL;
24293 s->font = NULL;
24294 for (i = s->cmp_from; i < s->cmp->glyph_len; i++)
24296 int c = COMPOSITION_GLYPH (s->cmp, i);
24298 /* TAB in a composition means display glyphs with padding space
24299 on the left or right. */
24300 if (c != '\t')
24302 int face_id = FACE_FOR_CHAR (s->f, base_face->ascii_face, c,
24303 -1, Qnil);
24305 face = get_char_face_and_encoding (s->f, c, face_id,
24306 s->char2b + i, 1);
24307 if (face)
24309 if (! s->face)
24311 s->face = face;
24312 s->font = s->face->font;
24314 else if (s->face != face)
24315 break;
24318 ++s->nchars;
24320 s->cmp_to = i;
24322 if (s->face == NULL)
24324 s->face = base_face->ascii_face;
24325 s->font = s->face->font;
24328 /* All glyph strings for the same composition has the same width,
24329 i.e. the width set for the first component of the composition. */
24330 s->width = s->first_glyph->pixel_width;
24332 /* If the specified font could not be loaded, use the frame's
24333 default font, but record the fact that we couldn't load it in
24334 the glyph string so that we can draw rectangles for the
24335 characters of the glyph string. */
24336 if (s->font == NULL)
24338 s->font_not_found_p = 1;
24339 s->font = FRAME_FONT (s->f);
24342 /* Adjust base line for subscript/superscript text. */
24343 s->ybase += s->first_glyph->voffset;
24345 /* This glyph string must always be drawn with 16-bit functions. */
24346 s->two_byte_p = 1;
24348 return s->cmp_to;
24351 static int
24352 fill_gstring_glyph_string (struct glyph_string *s, int face_id,
24353 int start, int end, int overlaps)
24355 struct glyph *glyph, *last;
24356 Lisp_Object lgstring;
24357 int i;
24359 s->for_overlaps = overlaps;
24360 glyph = s->row->glyphs[s->area] + start;
24361 last = s->row->glyphs[s->area] + end;
24362 s->cmp_id = glyph->u.cmp.id;
24363 s->cmp_from = glyph->slice.cmp.from;
24364 s->cmp_to = glyph->slice.cmp.to + 1;
24365 s->face = FACE_FROM_ID (s->f, face_id);
24366 lgstring = composition_gstring_from_id (s->cmp_id);
24367 s->font = XFONT_OBJECT (LGSTRING_FONT (lgstring));
24368 glyph++;
24369 while (glyph < last
24370 && glyph->u.cmp.automatic
24371 && glyph->u.cmp.id == s->cmp_id
24372 && s->cmp_to == glyph->slice.cmp.from)
24373 s->cmp_to = (glyph++)->slice.cmp.to + 1;
24375 for (i = s->cmp_from; i < s->cmp_to; i++)
24377 Lisp_Object lglyph = LGSTRING_GLYPH (lgstring, i);
24378 unsigned code = LGLYPH_CODE (lglyph);
24380 STORE_XCHAR2B ((s->char2b + i), code >> 8, code & 0xFF);
24382 s->width = composition_gstring_width (lgstring, s->cmp_from, s->cmp_to, NULL);
24383 return glyph - s->row->glyphs[s->area];
24387 /* Fill glyph string S from a sequence glyphs for glyphless characters.
24388 See the comment of fill_glyph_string for arguments.
24389 Value is the index of the first glyph not in S. */
24392 static int
24393 fill_glyphless_glyph_string (struct glyph_string *s, int face_id,
24394 int start, int end, int overlaps)
24396 struct glyph *glyph, *last;
24397 int voffset;
24399 eassert (s->first_glyph->type == GLYPHLESS_GLYPH);
24400 s->for_overlaps = overlaps;
24401 glyph = s->row->glyphs[s->area] + start;
24402 last = s->row->glyphs[s->area] + end;
24403 voffset = glyph->voffset;
24404 s->face = FACE_FROM_ID (s->f, face_id);
24405 s->font = s->face->font ? s->face->font : FRAME_FONT (s->f);
24406 s->nchars = 1;
24407 s->width = glyph->pixel_width;
24408 glyph++;
24409 while (glyph < last
24410 && glyph->type == GLYPHLESS_GLYPH
24411 && glyph->voffset == voffset
24412 && glyph->face_id == face_id)
24414 s->nchars++;
24415 s->width += glyph->pixel_width;
24416 glyph++;
24418 s->ybase += voffset;
24419 return glyph - s->row->glyphs[s->area];
24423 /* Fill glyph string S from a sequence of character glyphs.
24425 FACE_ID is the face id of the string. START is the index of the
24426 first glyph to consider, END is the index of the last + 1.
24427 OVERLAPS non-zero means S should draw the foreground only, and use
24428 its physical height for clipping. See also draw_glyphs.
24430 Value is the index of the first glyph not in S. */
24432 static int
24433 fill_glyph_string (struct glyph_string *s, int face_id,
24434 int start, int end, int overlaps)
24436 struct glyph *glyph, *last;
24437 int voffset;
24438 int glyph_not_available_p;
24440 eassert (s->f == XFRAME (s->w->frame));
24441 eassert (s->nchars == 0);
24442 eassert (start >= 0 && end > start);
24444 s->for_overlaps = overlaps;
24445 glyph = s->row->glyphs[s->area] + start;
24446 last = s->row->glyphs[s->area] + end;
24447 voffset = glyph->voffset;
24448 s->padding_p = glyph->padding_p;
24449 glyph_not_available_p = glyph->glyph_not_available_p;
24451 while (glyph < last
24452 && glyph->type == CHAR_GLYPH
24453 && glyph->voffset == voffset
24454 /* Same face id implies same font, nowadays. */
24455 && glyph->face_id == face_id
24456 && glyph->glyph_not_available_p == glyph_not_available_p)
24458 int two_byte_p;
24460 s->face = get_glyph_face_and_encoding (s->f, glyph,
24461 s->char2b + s->nchars,
24462 &two_byte_p);
24463 s->two_byte_p = two_byte_p;
24464 ++s->nchars;
24465 eassert (s->nchars <= end - start);
24466 s->width += glyph->pixel_width;
24467 if (glyph++->padding_p != s->padding_p)
24468 break;
24471 s->font = s->face->font;
24473 /* If the specified font could not be loaded, use the frame's font,
24474 but record the fact that we couldn't load it in
24475 S->font_not_found_p so that we can draw rectangles for the
24476 characters of the glyph string. */
24477 if (s->font == NULL || glyph_not_available_p)
24479 s->font_not_found_p = 1;
24480 s->font = FRAME_FONT (s->f);
24483 /* Adjust base line for subscript/superscript text. */
24484 s->ybase += voffset;
24486 eassert (s->face && s->face->gc);
24487 return glyph - s->row->glyphs[s->area];
24491 /* Fill glyph string S from image glyph S->first_glyph. */
24493 static void
24494 fill_image_glyph_string (struct glyph_string *s)
24496 eassert (s->first_glyph->type == IMAGE_GLYPH);
24497 s->img = IMAGE_FROM_ID (s->f, s->first_glyph->u.img_id);
24498 eassert (s->img);
24499 s->slice = s->first_glyph->slice.img;
24500 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
24501 s->font = s->face->font;
24502 s->width = s->first_glyph->pixel_width;
24504 /* Adjust base line for subscript/superscript text. */
24505 s->ybase += s->first_glyph->voffset;
24509 /* Fill glyph string S from a sequence of stretch glyphs.
24511 START is the index of the first glyph to consider,
24512 END is the index of the last + 1.
24514 Value is the index of the first glyph not in S. */
24516 static int
24517 fill_stretch_glyph_string (struct glyph_string *s, int start, int end)
24519 struct glyph *glyph, *last;
24520 int voffset, face_id;
24522 eassert (s->first_glyph->type == STRETCH_GLYPH);
24524 glyph = s->row->glyphs[s->area] + start;
24525 last = s->row->glyphs[s->area] + end;
24526 face_id = glyph->face_id;
24527 s->face = FACE_FROM_ID (s->f, face_id);
24528 s->font = s->face->font;
24529 s->width = glyph->pixel_width;
24530 s->nchars = 1;
24531 voffset = glyph->voffset;
24533 for (++glyph;
24534 (glyph < last
24535 && glyph->type == STRETCH_GLYPH
24536 && glyph->voffset == voffset
24537 && glyph->face_id == face_id);
24538 ++glyph)
24539 s->width += glyph->pixel_width;
24541 /* Adjust base line for subscript/superscript text. */
24542 s->ybase += voffset;
24544 /* The case that face->gc == 0 is handled when drawing the glyph
24545 string by calling prepare_face_for_display. */
24546 eassert (s->face);
24547 return glyph - s->row->glyphs[s->area];
24550 static struct font_metrics *
24551 get_per_char_metric (struct font *font, XChar2b *char2b)
24553 static struct font_metrics metrics;
24554 unsigned code;
24556 if (! font)
24557 return NULL;
24558 code = (XCHAR2B_BYTE1 (char2b) << 8) | XCHAR2B_BYTE2 (char2b);
24559 if (code == FONT_INVALID_CODE)
24560 return NULL;
24561 font->driver->text_extents (font, &code, 1, &metrics);
24562 return &metrics;
24565 /* EXPORT for RIF:
24566 Set *LEFT and *RIGHT to the left and right overhang of GLYPH on
24567 frame F. Overhangs of glyphs other than type CHAR_GLYPH are
24568 assumed to be zero. */
24570 void
24571 x_get_glyph_overhangs (struct glyph *glyph, struct frame *f, int *left, int *right)
24573 *left = *right = 0;
24575 if (glyph->type == CHAR_GLYPH)
24577 struct face *face;
24578 XChar2b char2b;
24579 struct font_metrics *pcm;
24581 face = get_glyph_face_and_encoding (f, glyph, &char2b, NULL);
24582 if (face->font && (pcm = get_per_char_metric (face->font, &char2b)))
24584 if (pcm->rbearing > pcm->width)
24585 *right = pcm->rbearing - pcm->width;
24586 if (pcm->lbearing < 0)
24587 *left = -pcm->lbearing;
24590 else if (glyph->type == COMPOSITE_GLYPH)
24592 if (! glyph->u.cmp.automatic)
24594 struct composition *cmp = composition_table[glyph->u.cmp.id];
24596 if (cmp->rbearing > cmp->pixel_width)
24597 *right = cmp->rbearing - cmp->pixel_width;
24598 if (cmp->lbearing < 0)
24599 *left = - cmp->lbearing;
24601 else
24603 Lisp_Object gstring = composition_gstring_from_id (glyph->u.cmp.id);
24604 struct font_metrics metrics;
24606 composition_gstring_width (gstring, glyph->slice.cmp.from,
24607 glyph->slice.cmp.to + 1, &metrics);
24608 if (metrics.rbearing > metrics.width)
24609 *right = metrics.rbearing - metrics.width;
24610 if (metrics.lbearing < 0)
24611 *left = - metrics.lbearing;
24617 /* Return the index of the first glyph preceding glyph string S that
24618 is overwritten by S because of S's left overhang. Value is -1
24619 if no glyphs are overwritten. */
24621 static int
24622 left_overwritten (struct glyph_string *s)
24624 int k;
24626 if (s->left_overhang)
24628 int x = 0, i;
24629 struct glyph *glyphs = s->row->glyphs[s->area];
24630 int first = s->first_glyph - glyphs;
24632 for (i = first - 1; i >= 0 && x > -s->left_overhang; --i)
24633 x -= glyphs[i].pixel_width;
24635 k = i + 1;
24637 else
24638 k = -1;
24640 return k;
24644 /* Return the index of the first glyph preceding glyph string S that
24645 is overwriting S because of its right overhang. Value is -1 if no
24646 glyph in front of S overwrites S. */
24648 static int
24649 left_overwriting (struct glyph_string *s)
24651 int i, k, x;
24652 struct glyph *glyphs = s->row->glyphs[s->area];
24653 int first = s->first_glyph - glyphs;
24655 k = -1;
24656 x = 0;
24657 for (i = first - 1; i >= 0; --i)
24659 int left, right;
24660 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
24661 if (x + right > 0)
24662 k = i;
24663 x -= glyphs[i].pixel_width;
24666 return k;
24670 /* Return the index of the last glyph following glyph string S that is
24671 overwritten by S because of S's right overhang. Value is -1 if
24672 no such glyph is found. */
24674 static int
24675 right_overwritten (struct glyph_string *s)
24677 int k = -1;
24679 if (s->right_overhang)
24681 int x = 0, i;
24682 struct glyph *glyphs = s->row->glyphs[s->area];
24683 int first = (s->first_glyph - glyphs
24684 + (s->first_glyph->type == COMPOSITE_GLYPH ? 1 : s->nchars));
24685 int end = s->row->used[s->area];
24687 for (i = first; i < end && s->right_overhang > x; ++i)
24688 x += glyphs[i].pixel_width;
24690 k = i;
24693 return k;
24697 /* Return the index of the last glyph following glyph string S that
24698 overwrites S because of its left overhang. Value is negative
24699 if no such glyph is found. */
24701 static int
24702 right_overwriting (struct glyph_string *s)
24704 int i, k, x;
24705 int end = s->row->used[s->area];
24706 struct glyph *glyphs = s->row->glyphs[s->area];
24707 int first = (s->first_glyph - glyphs
24708 + (s->first_glyph->type == COMPOSITE_GLYPH ? 1 : s->nchars));
24710 k = -1;
24711 x = 0;
24712 for (i = first; i < end; ++i)
24714 int left, right;
24715 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
24716 if (x - left < 0)
24717 k = i;
24718 x += glyphs[i].pixel_width;
24721 return k;
24725 /* Set background width of glyph string S. START is the index of the
24726 first glyph following S. LAST_X is the right-most x-position + 1
24727 in the drawing area. */
24729 static void
24730 set_glyph_string_background_width (struct glyph_string *s, int start, int last_x)
24732 /* If the face of this glyph string has to be drawn to the end of
24733 the drawing area, set S->extends_to_end_of_line_p. */
24735 if (start == s->row->used[s->area]
24736 && ((s->row->fill_line_p
24737 && (s->hl == DRAW_NORMAL_TEXT
24738 || s->hl == DRAW_IMAGE_RAISED
24739 || s->hl == DRAW_IMAGE_SUNKEN))
24740 || s->hl == DRAW_MOUSE_FACE))
24741 s->extends_to_end_of_line_p = 1;
24743 /* If S extends its face to the end of the line, set its
24744 background_width to the distance to the right edge of the drawing
24745 area. */
24746 if (s->extends_to_end_of_line_p)
24747 s->background_width = last_x - s->x + 1;
24748 else
24749 s->background_width = s->width;
24753 /* Compute overhangs and x-positions for glyph string S and its
24754 predecessors, or successors. X is the starting x-position for S.
24755 BACKWARD_P non-zero means process predecessors. */
24757 static void
24758 compute_overhangs_and_x (struct glyph_string *s, int x, int backward_p)
24760 if (backward_p)
24762 while (s)
24764 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
24765 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
24766 x -= s->width;
24767 s->x = x;
24768 s = s->prev;
24771 else
24773 while (s)
24775 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
24776 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
24777 s->x = x;
24778 x += s->width;
24779 s = s->next;
24786 /* The following macros are only called from draw_glyphs below.
24787 They reference the following parameters of that function directly:
24788 `w', `row', `area', and `overlap_p'
24789 as well as the following local variables:
24790 `s', `f', and `hdc' (in W32) */
24792 #ifdef HAVE_NTGUI
24793 /* On W32, silently add local `hdc' variable to argument list of
24794 init_glyph_string. */
24795 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
24796 init_glyph_string (s, hdc, char2b, w, row, area, start, hl)
24797 #else
24798 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
24799 init_glyph_string (s, char2b, w, row, area, start, hl)
24800 #endif
24802 /* Add a glyph string for a stretch glyph to the list of strings
24803 between HEAD and TAIL. START is the index of the stretch glyph in
24804 row area AREA of glyph row ROW. END is the index of the last glyph
24805 in that glyph row area. X is the current output position assigned
24806 to the new glyph string constructed. HL overrides that face of the
24807 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
24808 is the right-most x-position of the drawing area. */
24810 /* SunOS 4 bundled cc, barfed on continuations in the arg lists here
24811 and below -- keep them on one line. */
24812 #define BUILD_STRETCH_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
24813 do \
24815 s = alloca (sizeof *s); \
24816 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
24817 START = fill_stretch_glyph_string (s, START, END); \
24818 append_glyph_string (&HEAD, &TAIL, s); \
24819 s->x = (X); \
24821 while (0)
24824 /* Add a glyph string for an image glyph to the list of strings
24825 between HEAD and TAIL. START is the index of the image glyph in
24826 row area AREA of glyph row ROW. END is the index of the last glyph
24827 in that glyph row area. X is the current output position assigned
24828 to the new glyph string constructed. HL overrides that face of the
24829 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
24830 is the right-most x-position of the drawing area. */
24832 #define BUILD_IMAGE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
24833 do \
24835 s = alloca (sizeof *s); \
24836 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
24837 fill_image_glyph_string (s); \
24838 append_glyph_string (&HEAD, &TAIL, s); \
24839 ++START; \
24840 s->x = (X); \
24842 while (0)
24845 /* Add a glyph string for a sequence of character glyphs to the list
24846 of strings between HEAD and TAIL. START is the index of the first
24847 glyph in row area AREA of glyph row ROW that is part of the new
24848 glyph string. END is the index of the last glyph in that glyph row
24849 area. X is the current output position assigned to the new glyph
24850 string constructed. HL overrides that face of the glyph; e.g. it
24851 is DRAW_CURSOR if a cursor has to be drawn. LAST_X is the
24852 right-most x-position of the drawing area. */
24854 #define BUILD_CHAR_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
24855 do \
24857 int face_id; \
24858 XChar2b *char2b; \
24860 face_id = (row)->glyphs[area][START].face_id; \
24862 s = alloca (sizeof *s); \
24863 SAFE_NALLOCA (char2b, 1, (END) - (START)); \
24864 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
24865 append_glyph_string (&HEAD, &TAIL, s); \
24866 s->x = (X); \
24867 START = fill_glyph_string (s, face_id, START, END, overlaps); \
24869 while (0)
24872 /* Add a glyph string for a composite sequence to the list of strings
24873 between HEAD and TAIL. START is the index of the first glyph in
24874 row area AREA of glyph row ROW that is part of the new glyph
24875 string. END is the index of the last glyph in that glyph row area.
24876 X is the current output position assigned to the new glyph string
24877 constructed. HL overrides that face of the glyph; e.g. it is
24878 DRAW_CURSOR if a cursor has to be drawn. LAST_X is the right-most
24879 x-position of the drawing area. */
24881 #define BUILD_COMPOSITE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
24882 do { \
24883 int face_id = (row)->glyphs[area][START].face_id; \
24884 struct face *base_face = FACE_FROM_ID (f, face_id); \
24885 ptrdiff_t cmp_id = (row)->glyphs[area][START].u.cmp.id; \
24886 struct composition *cmp = composition_table[cmp_id]; \
24887 XChar2b *char2b; \
24888 struct glyph_string *first_s = NULL; \
24889 int n; \
24891 SAFE_NALLOCA (char2b, 1, cmp->glyph_len); \
24893 /* Make glyph_strings for each glyph sequence that is drawable by \
24894 the same face, and append them to HEAD/TAIL. */ \
24895 for (n = 0; n < cmp->glyph_len;) \
24897 s = alloca (sizeof *s); \
24898 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
24899 append_glyph_string (&(HEAD), &(TAIL), s); \
24900 s->cmp = cmp; \
24901 s->cmp_from = n; \
24902 s->x = (X); \
24903 if (n == 0) \
24904 first_s = s; \
24905 n = fill_composite_glyph_string (s, base_face, overlaps); \
24908 ++START; \
24909 s = first_s; \
24910 } while (0)
24913 /* Add a glyph string for a glyph-string sequence to the list of strings
24914 between HEAD and TAIL. */
24916 #define BUILD_GSTRING_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
24917 do { \
24918 int face_id; \
24919 XChar2b *char2b; \
24920 Lisp_Object gstring; \
24922 face_id = (row)->glyphs[area][START].face_id; \
24923 gstring = (composition_gstring_from_id \
24924 ((row)->glyphs[area][START].u.cmp.id)); \
24925 s = alloca (sizeof *s); \
24926 SAFE_NALLOCA (char2b, 1, LGSTRING_GLYPH_LEN (gstring)); \
24927 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
24928 append_glyph_string (&(HEAD), &(TAIL), s); \
24929 s->x = (X); \
24930 START = fill_gstring_glyph_string (s, face_id, START, END, overlaps); \
24931 } while (0)
24934 /* Add a glyph string for a sequence of glyphless character's glyphs
24935 to the list of strings between HEAD and TAIL. The meanings of
24936 arguments are the same as those of BUILD_CHAR_GLYPH_STRINGS. */
24938 #define BUILD_GLYPHLESS_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
24939 do \
24941 int face_id; \
24943 face_id = (row)->glyphs[area][START].face_id; \
24945 s = alloca (sizeof *s); \
24946 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
24947 append_glyph_string (&HEAD, &TAIL, s); \
24948 s->x = (X); \
24949 START = fill_glyphless_glyph_string (s, face_id, START, END, \
24950 overlaps); \
24952 while (0)
24955 /* Build a list of glyph strings between HEAD and TAIL for the glyphs
24956 of AREA of glyph row ROW on window W between indices START and END.
24957 HL overrides the face for drawing glyph strings, e.g. it is
24958 DRAW_CURSOR to draw a cursor. X and LAST_X are start and end
24959 x-positions of the drawing area.
24961 This is an ugly monster macro construct because we must use alloca
24962 to allocate glyph strings (because draw_glyphs can be called
24963 asynchronously). */
24965 #define BUILD_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
24966 do \
24968 HEAD = TAIL = NULL; \
24969 while (START < END) \
24971 struct glyph *first_glyph = (row)->glyphs[area] + START; \
24972 switch (first_glyph->type) \
24974 case CHAR_GLYPH: \
24975 BUILD_CHAR_GLYPH_STRINGS (START, END, HEAD, TAIL, \
24976 HL, X, LAST_X); \
24977 break; \
24979 case COMPOSITE_GLYPH: \
24980 if (first_glyph->u.cmp.automatic) \
24981 BUILD_GSTRING_GLYPH_STRING (START, END, HEAD, TAIL, \
24982 HL, X, LAST_X); \
24983 else \
24984 BUILD_COMPOSITE_GLYPH_STRING (START, END, HEAD, TAIL, \
24985 HL, X, LAST_X); \
24986 break; \
24988 case STRETCH_GLYPH: \
24989 BUILD_STRETCH_GLYPH_STRING (START, END, HEAD, TAIL, \
24990 HL, X, LAST_X); \
24991 break; \
24993 case IMAGE_GLYPH: \
24994 BUILD_IMAGE_GLYPH_STRING (START, END, HEAD, TAIL, \
24995 HL, X, LAST_X); \
24996 break; \
24998 case GLYPHLESS_GLYPH: \
24999 BUILD_GLYPHLESS_GLYPH_STRING (START, END, HEAD, TAIL, \
25000 HL, X, LAST_X); \
25001 break; \
25003 default: \
25004 emacs_abort (); \
25007 if (s) \
25009 set_glyph_string_background_width (s, START, LAST_X); \
25010 (X) += s->width; \
25013 } while (0)
25016 /* Draw glyphs between START and END in AREA of ROW on window W,
25017 starting at x-position X. X is relative to AREA in W. HL is a
25018 face-override with the following meaning:
25020 DRAW_NORMAL_TEXT draw normally
25021 DRAW_CURSOR draw in cursor face
25022 DRAW_MOUSE_FACE draw in mouse face.
25023 DRAW_INVERSE_VIDEO draw in mode line face
25024 DRAW_IMAGE_SUNKEN draw an image with a sunken relief around it
25025 DRAW_IMAGE_RAISED draw an image with a raised relief around it
25027 If OVERLAPS is non-zero, draw only the foreground of characters and
25028 clip to the physical height of ROW. Non-zero value also defines
25029 the overlapping part to be drawn:
25031 OVERLAPS_PRED overlap with preceding rows
25032 OVERLAPS_SUCC overlap with succeeding rows
25033 OVERLAPS_BOTH overlap with both preceding/succeeding rows
25034 OVERLAPS_ERASED_CURSOR overlap with erased cursor area
25036 Value is the x-position reached, relative to AREA of W. */
25038 static int
25039 draw_glyphs (struct window *w, int x, struct glyph_row *row,
25040 enum glyph_row_area area, ptrdiff_t start, ptrdiff_t end,
25041 enum draw_glyphs_face hl, int overlaps)
25043 struct glyph_string *head, *tail;
25044 struct glyph_string *s;
25045 struct glyph_string *clip_head = NULL, *clip_tail = NULL;
25046 int i, j, x_reached, last_x, area_left = 0;
25047 struct frame *f = XFRAME (WINDOW_FRAME (w));
25048 DECLARE_HDC (hdc);
25050 ALLOCATE_HDC (hdc, f);
25052 /* Let's rather be paranoid than getting a SEGV. */
25053 end = min (end, row->used[area]);
25054 start = clip_to_bounds (0, start, end);
25056 /* Translate X to frame coordinates. Set last_x to the right
25057 end of the drawing area. */
25058 if (row->full_width_p)
25060 /* X is relative to the left edge of W, without scroll bars
25061 or fringes. */
25062 area_left = WINDOW_LEFT_EDGE_X (w);
25063 last_x = (WINDOW_LEFT_EDGE_X (w) + WINDOW_PIXEL_WIDTH (w)
25064 - (row->mode_line_p ? WINDOW_RIGHT_DIVIDER_WIDTH (w) : 0));
25066 else
25068 area_left = window_box_left (w, area);
25069 last_x = area_left + window_box_width (w, area);
25071 x += area_left;
25073 /* Build a doubly-linked list of glyph_string structures between
25074 head and tail from what we have to draw. Note that the macro
25075 BUILD_GLYPH_STRINGS will modify its start parameter. That's
25076 the reason we use a separate variable `i'. */
25077 i = start;
25078 USE_SAFE_ALLOCA;
25079 BUILD_GLYPH_STRINGS (i, end, head, tail, hl, x, last_x);
25080 if (tail)
25081 x_reached = tail->x + tail->background_width;
25082 else
25083 x_reached = x;
25085 /* If there are any glyphs with lbearing < 0 or rbearing > width in
25086 the row, redraw some glyphs in front or following the glyph
25087 strings built above. */
25088 if (head && !overlaps && row->contains_overlapping_glyphs_p)
25090 struct glyph_string *h, *t;
25091 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
25092 int mouse_beg_col IF_LINT (= 0), mouse_end_col IF_LINT (= 0);
25093 int check_mouse_face = 0;
25094 int dummy_x = 0;
25096 /* If mouse highlighting is on, we may need to draw adjacent
25097 glyphs using mouse-face highlighting. */
25098 if (area == TEXT_AREA && row->mouse_face_p
25099 && hlinfo->mouse_face_beg_row >= 0
25100 && hlinfo->mouse_face_end_row >= 0)
25102 ptrdiff_t row_vpos = MATRIX_ROW_VPOS (row, w->current_matrix);
25104 if (row_vpos >= hlinfo->mouse_face_beg_row
25105 && row_vpos <= hlinfo->mouse_face_end_row)
25107 check_mouse_face = 1;
25108 mouse_beg_col = (row_vpos == hlinfo->mouse_face_beg_row)
25109 ? hlinfo->mouse_face_beg_col : 0;
25110 mouse_end_col = (row_vpos == hlinfo->mouse_face_end_row)
25111 ? hlinfo->mouse_face_end_col
25112 : row->used[TEXT_AREA];
25116 /* Compute overhangs for all glyph strings. */
25117 if (FRAME_RIF (f)->compute_glyph_string_overhangs)
25118 for (s = head; s; s = s->next)
25119 FRAME_RIF (f)->compute_glyph_string_overhangs (s);
25121 /* Prepend glyph strings for glyphs in front of the first glyph
25122 string that are overwritten because of the first glyph
25123 string's left overhang. The background of all strings
25124 prepended must be drawn because the first glyph string
25125 draws over it. */
25126 i = left_overwritten (head);
25127 if (i >= 0)
25129 enum draw_glyphs_face overlap_hl;
25131 /* If this row contains mouse highlighting, attempt to draw
25132 the overlapped glyphs with the correct highlight. This
25133 code fails if the overlap encompasses more than one glyph
25134 and mouse-highlight spans only some of these glyphs.
25135 However, making it work perfectly involves a lot more
25136 code, and I don't know if the pathological case occurs in
25137 practice, so we'll stick to this for now. --- cyd */
25138 if (check_mouse_face
25139 && mouse_beg_col < start && mouse_end_col > i)
25140 overlap_hl = DRAW_MOUSE_FACE;
25141 else
25142 overlap_hl = DRAW_NORMAL_TEXT;
25144 if (hl != overlap_hl)
25145 clip_head = head;
25146 j = i;
25147 BUILD_GLYPH_STRINGS (j, start, h, t,
25148 overlap_hl, dummy_x, last_x);
25149 start = i;
25150 compute_overhangs_and_x (t, head->x, 1);
25151 prepend_glyph_string_lists (&head, &tail, h, t);
25152 if (clip_head == NULL)
25153 clip_head = head;
25156 /* Prepend glyph strings for glyphs in front of the first glyph
25157 string that overwrite that glyph string because of their
25158 right overhang. For these strings, only the foreground must
25159 be drawn, because it draws over the glyph string at `head'.
25160 The background must not be drawn because this would overwrite
25161 right overhangs of preceding glyphs for which no glyph
25162 strings exist. */
25163 i = left_overwriting (head);
25164 if (i >= 0)
25166 enum draw_glyphs_face overlap_hl;
25168 if (check_mouse_face
25169 && mouse_beg_col < start && mouse_end_col > i)
25170 overlap_hl = DRAW_MOUSE_FACE;
25171 else
25172 overlap_hl = DRAW_NORMAL_TEXT;
25174 if (hl == overlap_hl || clip_head == NULL)
25175 clip_head = head;
25176 BUILD_GLYPH_STRINGS (i, start, h, t,
25177 overlap_hl, dummy_x, last_x);
25178 for (s = h; s; s = s->next)
25179 s->background_filled_p = 1;
25180 compute_overhangs_and_x (t, head->x, 1);
25181 prepend_glyph_string_lists (&head, &tail, h, t);
25184 /* Append glyphs strings for glyphs following the last glyph
25185 string tail that are overwritten by tail. The background of
25186 these strings has to be drawn because tail's foreground draws
25187 over it. */
25188 i = right_overwritten (tail);
25189 if (i >= 0)
25191 enum draw_glyphs_face overlap_hl;
25193 if (check_mouse_face
25194 && mouse_beg_col < i && mouse_end_col > end)
25195 overlap_hl = DRAW_MOUSE_FACE;
25196 else
25197 overlap_hl = DRAW_NORMAL_TEXT;
25199 if (hl != overlap_hl)
25200 clip_tail = tail;
25201 BUILD_GLYPH_STRINGS (end, i, h, t,
25202 overlap_hl, x, last_x);
25203 /* Because BUILD_GLYPH_STRINGS updates the first argument,
25204 we don't have `end = i;' here. */
25205 compute_overhangs_and_x (h, tail->x + tail->width, 0);
25206 append_glyph_string_lists (&head, &tail, h, t);
25207 if (clip_tail == NULL)
25208 clip_tail = tail;
25211 /* Append glyph strings for glyphs following the last glyph
25212 string tail that overwrite tail. The foreground of such
25213 glyphs has to be drawn because it writes into the background
25214 of tail. The background must not be drawn because it could
25215 paint over the foreground of following glyphs. */
25216 i = right_overwriting (tail);
25217 if (i >= 0)
25219 enum draw_glyphs_face overlap_hl;
25220 if (check_mouse_face
25221 && mouse_beg_col < i && mouse_end_col > end)
25222 overlap_hl = DRAW_MOUSE_FACE;
25223 else
25224 overlap_hl = DRAW_NORMAL_TEXT;
25226 if (hl == overlap_hl || clip_tail == NULL)
25227 clip_tail = tail;
25228 i++; /* We must include the Ith glyph. */
25229 BUILD_GLYPH_STRINGS (end, i, h, t,
25230 overlap_hl, x, last_x);
25231 for (s = h; s; s = s->next)
25232 s->background_filled_p = 1;
25233 compute_overhangs_and_x (h, tail->x + tail->width, 0);
25234 append_glyph_string_lists (&head, &tail, h, t);
25236 if (clip_head || clip_tail)
25237 for (s = head; s; s = s->next)
25239 s->clip_head = clip_head;
25240 s->clip_tail = clip_tail;
25244 /* Draw all strings. */
25245 for (s = head; s; s = s->next)
25246 FRAME_RIF (f)->draw_glyph_string (s);
25248 #ifndef HAVE_NS
25249 /* When focus a sole frame and move horizontally, this sets on_p to 0
25250 causing a failure to erase prev cursor position. */
25251 if (area == TEXT_AREA
25252 && !row->full_width_p
25253 /* When drawing overlapping rows, only the glyph strings'
25254 foreground is drawn, which doesn't erase a cursor
25255 completely. */
25256 && !overlaps)
25258 int x0 = clip_head ? clip_head->x : (head ? head->x : x);
25259 int x1 = (clip_tail ? clip_tail->x + clip_tail->background_width
25260 : (tail ? tail->x + tail->background_width : x));
25261 x0 -= area_left;
25262 x1 -= area_left;
25264 notice_overwritten_cursor (w, TEXT_AREA, x0, x1,
25265 row->y, MATRIX_ROW_BOTTOM_Y (row));
25267 #endif
25269 /* Value is the x-position up to which drawn, relative to AREA of W.
25270 This doesn't include parts drawn because of overhangs. */
25271 if (row->full_width_p)
25272 x_reached = FRAME_TO_WINDOW_PIXEL_X (w, x_reached);
25273 else
25274 x_reached -= area_left;
25276 RELEASE_HDC (hdc, f);
25278 SAFE_FREE ();
25279 return x_reached;
25282 /* Expand row matrix if too narrow. Don't expand if area
25283 is not present. */
25285 #define IT_EXPAND_MATRIX_WIDTH(it, area) \
25287 if (!it->f->fonts_changed \
25288 && (it->glyph_row->glyphs[area] \
25289 < it->glyph_row->glyphs[area + 1])) \
25291 it->w->ncols_scale_factor++; \
25292 it->f->fonts_changed = 1; \
25296 /* Store one glyph for IT->char_to_display in IT->glyph_row.
25297 Called from x_produce_glyphs when IT->glyph_row is non-null. */
25299 static void
25300 append_glyph (struct it *it)
25302 struct glyph *glyph;
25303 enum glyph_row_area area = it->area;
25305 eassert (it->glyph_row);
25306 eassert (it->char_to_display != '\n' && it->char_to_display != '\t');
25308 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
25309 if (glyph < it->glyph_row->glyphs[area + 1])
25311 /* If the glyph row is reversed, we need to prepend the glyph
25312 rather than append it. */
25313 if (it->glyph_row->reversed_p && area == TEXT_AREA)
25315 struct glyph *g;
25317 /* Make room for the additional glyph. */
25318 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
25319 g[1] = *g;
25320 glyph = it->glyph_row->glyphs[area];
25322 glyph->charpos = CHARPOS (it->position);
25323 glyph->object = it->object;
25324 if (it->pixel_width > 0)
25326 glyph->pixel_width = it->pixel_width;
25327 glyph->padding_p = 0;
25329 else
25331 /* Assure at least 1-pixel width. Otherwise, cursor can't
25332 be displayed correctly. */
25333 glyph->pixel_width = 1;
25334 glyph->padding_p = 1;
25336 glyph->ascent = it->ascent;
25337 glyph->descent = it->descent;
25338 glyph->voffset = it->voffset;
25339 glyph->type = CHAR_GLYPH;
25340 glyph->avoid_cursor_p = it->avoid_cursor_p;
25341 glyph->multibyte_p = it->multibyte_p;
25342 if (it->glyph_row->reversed_p && area == TEXT_AREA)
25344 /* In R2L rows, the left and the right box edges need to be
25345 drawn in reverse direction. */
25346 glyph->right_box_line_p = it->start_of_box_run_p;
25347 glyph->left_box_line_p = it->end_of_box_run_p;
25349 else
25351 glyph->left_box_line_p = it->start_of_box_run_p;
25352 glyph->right_box_line_p = it->end_of_box_run_p;
25354 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
25355 || it->phys_descent > it->descent);
25356 glyph->glyph_not_available_p = it->glyph_not_available_p;
25357 glyph->face_id = it->face_id;
25358 glyph->u.ch = it->char_to_display;
25359 glyph->slice.img = null_glyph_slice;
25360 glyph->font_type = FONT_TYPE_UNKNOWN;
25361 if (it->bidi_p)
25363 glyph->resolved_level = it->bidi_it.resolved_level;
25364 eassert ((it->bidi_it.type & 7) == it->bidi_it.type);
25365 glyph->bidi_type = it->bidi_it.type;
25367 else
25369 glyph->resolved_level = 0;
25370 glyph->bidi_type = UNKNOWN_BT;
25372 ++it->glyph_row->used[area];
25374 else
25375 IT_EXPAND_MATRIX_WIDTH (it, area);
25378 /* Store one glyph for the composition IT->cmp_it.id in
25379 IT->glyph_row. Called from x_produce_glyphs when IT->glyph_row is
25380 non-null. */
25382 static void
25383 append_composite_glyph (struct it *it)
25385 struct glyph *glyph;
25386 enum glyph_row_area area = it->area;
25388 eassert (it->glyph_row);
25390 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
25391 if (glyph < it->glyph_row->glyphs[area + 1])
25393 /* If the glyph row is reversed, we need to prepend the glyph
25394 rather than append it. */
25395 if (it->glyph_row->reversed_p && it->area == TEXT_AREA)
25397 struct glyph *g;
25399 /* Make room for the new glyph. */
25400 for (g = glyph - 1; g >= it->glyph_row->glyphs[it->area]; g--)
25401 g[1] = *g;
25402 glyph = it->glyph_row->glyphs[it->area];
25404 glyph->charpos = it->cmp_it.charpos;
25405 glyph->object = it->object;
25406 glyph->pixel_width = it->pixel_width;
25407 glyph->ascent = it->ascent;
25408 glyph->descent = it->descent;
25409 glyph->voffset = it->voffset;
25410 glyph->type = COMPOSITE_GLYPH;
25411 if (it->cmp_it.ch < 0)
25413 glyph->u.cmp.automatic = 0;
25414 glyph->u.cmp.id = it->cmp_it.id;
25415 glyph->slice.cmp.from = glyph->slice.cmp.to = 0;
25417 else
25419 glyph->u.cmp.automatic = 1;
25420 glyph->u.cmp.id = it->cmp_it.id;
25421 glyph->slice.cmp.from = it->cmp_it.from;
25422 glyph->slice.cmp.to = it->cmp_it.to - 1;
25424 glyph->avoid_cursor_p = it->avoid_cursor_p;
25425 glyph->multibyte_p = it->multibyte_p;
25426 if (it->glyph_row->reversed_p && area == TEXT_AREA)
25428 /* In R2L rows, the left and the right box edges need to be
25429 drawn in reverse direction. */
25430 glyph->right_box_line_p = it->start_of_box_run_p;
25431 glyph->left_box_line_p = it->end_of_box_run_p;
25433 else
25435 glyph->left_box_line_p = it->start_of_box_run_p;
25436 glyph->right_box_line_p = it->end_of_box_run_p;
25438 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
25439 || it->phys_descent > it->descent);
25440 glyph->padding_p = 0;
25441 glyph->glyph_not_available_p = 0;
25442 glyph->face_id = it->face_id;
25443 glyph->font_type = FONT_TYPE_UNKNOWN;
25444 if (it->bidi_p)
25446 glyph->resolved_level = it->bidi_it.resolved_level;
25447 eassert ((it->bidi_it.type & 7) == it->bidi_it.type);
25448 glyph->bidi_type = it->bidi_it.type;
25450 ++it->glyph_row->used[area];
25452 else
25453 IT_EXPAND_MATRIX_WIDTH (it, area);
25457 /* Change IT->ascent and IT->height according to the setting of
25458 IT->voffset. */
25460 static void
25461 take_vertical_position_into_account (struct it *it)
25463 if (it->voffset)
25465 if (it->voffset < 0)
25466 /* Increase the ascent so that we can display the text higher
25467 in the line. */
25468 it->ascent -= it->voffset;
25469 else
25470 /* Increase the descent so that we can display the text lower
25471 in the line. */
25472 it->descent += it->voffset;
25477 /* Produce glyphs/get display metrics for the image IT is loaded with.
25478 See the description of struct display_iterator in dispextern.h for
25479 an overview of struct display_iterator. */
25481 static void
25482 produce_image_glyph (struct it *it)
25484 struct image *img;
25485 struct face *face;
25486 int glyph_ascent, crop;
25487 struct glyph_slice slice;
25489 eassert (it->what == IT_IMAGE);
25491 face = FACE_FROM_ID (it->f, it->face_id);
25492 eassert (face);
25493 /* Make sure X resources of the face is loaded. */
25494 prepare_face_for_display (it->f, face);
25496 if (it->image_id < 0)
25498 /* Fringe bitmap. */
25499 it->ascent = it->phys_ascent = 0;
25500 it->descent = it->phys_descent = 0;
25501 it->pixel_width = 0;
25502 it->nglyphs = 0;
25503 return;
25506 img = IMAGE_FROM_ID (it->f, it->image_id);
25507 eassert (img);
25508 /* Make sure X resources of the image is loaded. */
25509 prepare_image_for_display (it->f, img);
25511 slice.x = slice.y = 0;
25512 slice.width = img->width;
25513 slice.height = img->height;
25515 if (INTEGERP (it->slice.x))
25516 slice.x = XINT (it->slice.x);
25517 else if (FLOATP (it->slice.x))
25518 slice.x = XFLOAT_DATA (it->slice.x) * img->width;
25520 if (INTEGERP (it->slice.y))
25521 slice.y = XINT (it->slice.y);
25522 else if (FLOATP (it->slice.y))
25523 slice.y = XFLOAT_DATA (it->slice.y) * img->height;
25525 if (INTEGERP (it->slice.width))
25526 slice.width = XINT (it->slice.width);
25527 else if (FLOATP (it->slice.width))
25528 slice.width = XFLOAT_DATA (it->slice.width) * img->width;
25530 if (INTEGERP (it->slice.height))
25531 slice.height = XINT (it->slice.height);
25532 else if (FLOATP (it->slice.height))
25533 slice.height = XFLOAT_DATA (it->slice.height) * img->height;
25535 if (slice.x >= img->width)
25536 slice.x = img->width;
25537 if (slice.y >= img->height)
25538 slice.y = img->height;
25539 if (slice.x + slice.width >= img->width)
25540 slice.width = img->width - slice.x;
25541 if (slice.y + slice.height > img->height)
25542 slice.height = img->height - slice.y;
25544 if (slice.width == 0 || slice.height == 0)
25545 return;
25547 it->ascent = it->phys_ascent = glyph_ascent = image_ascent (img, face, &slice);
25549 it->descent = slice.height - glyph_ascent;
25550 if (slice.y == 0)
25551 it->descent += img->vmargin;
25552 if (slice.y + slice.height == img->height)
25553 it->descent += img->vmargin;
25554 it->phys_descent = it->descent;
25556 it->pixel_width = slice.width;
25557 if (slice.x == 0)
25558 it->pixel_width += img->hmargin;
25559 if (slice.x + slice.width == img->width)
25560 it->pixel_width += img->hmargin;
25562 /* It's quite possible for images to have an ascent greater than
25563 their height, so don't get confused in that case. */
25564 if (it->descent < 0)
25565 it->descent = 0;
25567 it->nglyphs = 1;
25569 if (face->box != FACE_NO_BOX)
25571 if (face->box_line_width > 0)
25573 if (slice.y == 0)
25574 it->ascent += face->box_line_width;
25575 if (slice.y + slice.height == img->height)
25576 it->descent += face->box_line_width;
25579 if (it->start_of_box_run_p && slice.x == 0)
25580 it->pixel_width += eabs (face->box_line_width);
25581 if (it->end_of_box_run_p && slice.x + slice.width == img->width)
25582 it->pixel_width += eabs (face->box_line_width);
25585 take_vertical_position_into_account (it);
25587 /* Automatically crop wide image glyphs at right edge so we can
25588 draw the cursor on same display row. */
25589 if ((crop = it->pixel_width - (it->last_visible_x - it->current_x), crop > 0)
25590 && (it->hpos == 0 || it->pixel_width > it->last_visible_x / 4))
25592 it->pixel_width -= crop;
25593 slice.width -= crop;
25596 if (it->glyph_row)
25598 struct glyph *glyph;
25599 enum glyph_row_area area = it->area;
25601 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
25602 if (glyph < it->glyph_row->glyphs[area + 1])
25604 glyph->charpos = CHARPOS (it->position);
25605 glyph->object = it->object;
25606 glyph->pixel_width = it->pixel_width;
25607 glyph->ascent = glyph_ascent;
25608 glyph->descent = it->descent;
25609 glyph->voffset = it->voffset;
25610 glyph->type = IMAGE_GLYPH;
25611 glyph->avoid_cursor_p = it->avoid_cursor_p;
25612 glyph->multibyte_p = it->multibyte_p;
25613 if (it->glyph_row->reversed_p && area == TEXT_AREA)
25615 /* In R2L rows, the left and the right box edges need to be
25616 drawn in reverse direction. */
25617 glyph->right_box_line_p = it->start_of_box_run_p;
25618 glyph->left_box_line_p = it->end_of_box_run_p;
25620 else
25622 glyph->left_box_line_p = it->start_of_box_run_p;
25623 glyph->right_box_line_p = it->end_of_box_run_p;
25625 glyph->overlaps_vertically_p = 0;
25626 glyph->padding_p = 0;
25627 glyph->glyph_not_available_p = 0;
25628 glyph->face_id = it->face_id;
25629 glyph->u.img_id = img->id;
25630 glyph->slice.img = slice;
25631 glyph->font_type = FONT_TYPE_UNKNOWN;
25632 if (it->bidi_p)
25634 glyph->resolved_level = it->bidi_it.resolved_level;
25635 eassert ((it->bidi_it.type & 7) == it->bidi_it.type);
25636 glyph->bidi_type = it->bidi_it.type;
25638 ++it->glyph_row->used[area];
25640 else
25641 IT_EXPAND_MATRIX_WIDTH (it, area);
25646 /* Append a stretch glyph to IT->glyph_row. OBJECT is the source
25647 of the glyph, WIDTH and HEIGHT are the width and height of the
25648 stretch. ASCENT is the ascent of the glyph (0 <= ASCENT <= HEIGHT). */
25650 static void
25651 append_stretch_glyph (struct it *it, Lisp_Object object,
25652 int width, int height, int ascent)
25654 struct glyph *glyph;
25655 enum glyph_row_area area = it->area;
25657 eassert (ascent >= 0 && ascent <= height);
25659 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
25660 if (glyph < it->glyph_row->glyphs[area + 1])
25662 /* If the glyph row is reversed, we need to prepend the glyph
25663 rather than append it. */
25664 if (it->glyph_row->reversed_p && area == TEXT_AREA)
25666 struct glyph *g;
25668 /* Make room for the additional glyph. */
25669 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
25670 g[1] = *g;
25671 glyph = it->glyph_row->glyphs[area];
25673 /* Decrease the width of the first glyph of the row that
25674 begins before first_visible_x (e.g., due to hscroll).
25675 This is so the overall width of the row becomes smaller
25676 by the scroll amount, and the stretch glyph appended by
25677 extend_face_to_end_of_line will be wider, to shift the
25678 row glyphs to the right. (In L2R rows, the corresponding
25679 left-shift effect is accomplished by setting row->x to a
25680 negative value, which won't work with R2L rows.)
25682 This must leave us with a positive value of WIDTH, since
25683 otherwise the call to move_it_in_display_line_to at the
25684 beginning of display_line would have got past the entire
25685 first glyph, and then it->current_x would have been
25686 greater or equal to it->first_visible_x. */
25687 if (it->current_x < it->first_visible_x)
25688 width -= it->first_visible_x - it->current_x;
25689 eassert (width > 0);
25691 glyph->charpos = CHARPOS (it->position);
25692 glyph->object = object;
25693 glyph->pixel_width = width;
25694 glyph->ascent = ascent;
25695 glyph->descent = height - ascent;
25696 glyph->voffset = it->voffset;
25697 glyph->type = STRETCH_GLYPH;
25698 glyph->avoid_cursor_p = it->avoid_cursor_p;
25699 glyph->multibyte_p = it->multibyte_p;
25700 if (it->glyph_row->reversed_p && area == TEXT_AREA)
25702 /* In R2L rows, the left and the right box edges need to be
25703 drawn in reverse direction. */
25704 glyph->right_box_line_p = it->start_of_box_run_p;
25705 glyph->left_box_line_p = it->end_of_box_run_p;
25707 else
25709 glyph->left_box_line_p = it->start_of_box_run_p;
25710 glyph->right_box_line_p = it->end_of_box_run_p;
25712 glyph->overlaps_vertically_p = 0;
25713 glyph->padding_p = 0;
25714 glyph->glyph_not_available_p = 0;
25715 glyph->face_id = it->face_id;
25716 glyph->u.stretch.ascent = ascent;
25717 glyph->u.stretch.height = height;
25718 glyph->slice.img = null_glyph_slice;
25719 glyph->font_type = FONT_TYPE_UNKNOWN;
25720 if (it->bidi_p)
25722 glyph->resolved_level = it->bidi_it.resolved_level;
25723 eassert ((it->bidi_it.type & 7) == it->bidi_it.type);
25724 glyph->bidi_type = it->bidi_it.type;
25726 else
25728 glyph->resolved_level = 0;
25729 glyph->bidi_type = UNKNOWN_BT;
25731 ++it->glyph_row->used[area];
25733 else
25734 IT_EXPAND_MATRIX_WIDTH (it, area);
25737 #endif /* HAVE_WINDOW_SYSTEM */
25739 /* Produce a stretch glyph for iterator IT. IT->object is the value
25740 of the glyph property displayed. The value must be a list
25741 `(space KEYWORD VALUE ...)' with the following KEYWORD/VALUE pairs
25742 being recognized:
25744 1. `:width WIDTH' specifies that the space should be WIDTH *
25745 canonical char width wide. WIDTH may be an integer or floating
25746 point number.
25748 2. `:relative-width FACTOR' specifies that the width of the stretch
25749 should be computed from the width of the first character having the
25750 `glyph' property, and should be FACTOR times that width.
25752 3. `:align-to HPOS' specifies that the space should be wide enough
25753 to reach HPOS, a value in canonical character units.
25755 Exactly one of the above pairs must be present.
25757 4. `:height HEIGHT' specifies that the height of the stretch produced
25758 should be HEIGHT, measured in canonical character units.
25760 5. `:relative-height FACTOR' specifies that the height of the
25761 stretch should be FACTOR times the height of the characters having
25762 the glyph property.
25764 Either none or exactly one of 4 or 5 must be present.
25766 6. `:ascent ASCENT' specifies that ASCENT percent of the height
25767 of the stretch should be used for the ascent of the stretch.
25768 ASCENT must be in the range 0 <= ASCENT <= 100. */
25770 void
25771 produce_stretch_glyph (struct it *it)
25773 /* (space :width WIDTH :height HEIGHT ...) */
25774 Lisp_Object prop, plist;
25775 int width = 0, height = 0, align_to = -1;
25776 int zero_width_ok_p = 0;
25777 double tem;
25778 struct font *font = NULL;
25780 #ifdef HAVE_WINDOW_SYSTEM
25781 int ascent = 0;
25782 int zero_height_ok_p = 0;
25784 if (FRAME_WINDOW_P (it->f))
25786 struct face *face = FACE_FROM_ID (it->f, it->face_id);
25787 font = face->font ? face->font : FRAME_FONT (it->f);
25788 prepare_face_for_display (it->f, face);
25790 #endif
25792 /* List should start with `space'. */
25793 eassert (CONSP (it->object) && EQ (XCAR (it->object), Qspace));
25794 plist = XCDR (it->object);
25796 /* Compute the width of the stretch. */
25797 if ((prop = Fplist_get (plist, QCwidth), !NILP (prop))
25798 && calc_pixel_width_or_height (&tem, it, prop, font, 1, 0))
25800 /* Absolute width `:width WIDTH' specified and valid. */
25801 zero_width_ok_p = 1;
25802 width = (int)tem;
25804 #ifdef HAVE_WINDOW_SYSTEM
25805 else if (FRAME_WINDOW_P (it->f)
25806 && (prop = Fplist_get (plist, QCrelative_width), NUMVAL (prop) > 0))
25808 /* Relative width `:relative-width FACTOR' specified and valid.
25809 Compute the width of the characters having the `glyph'
25810 property. */
25811 struct it it2;
25812 unsigned char *p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
25814 it2 = *it;
25815 if (it->multibyte_p)
25816 it2.c = it2.char_to_display = STRING_CHAR_AND_LENGTH (p, it2.len);
25817 else
25819 it2.c = it2.char_to_display = *p, it2.len = 1;
25820 if (! ASCII_CHAR_P (it2.c))
25821 it2.char_to_display = BYTE8_TO_CHAR (it2.c);
25824 it2.glyph_row = NULL;
25825 it2.what = IT_CHARACTER;
25826 x_produce_glyphs (&it2);
25827 width = NUMVAL (prop) * it2.pixel_width;
25829 #endif /* HAVE_WINDOW_SYSTEM */
25830 else if ((prop = Fplist_get (plist, QCalign_to), !NILP (prop))
25831 && calc_pixel_width_or_height (&tem, it, prop, font, 1, &align_to))
25833 if (it->glyph_row == NULL || !it->glyph_row->mode_line_p)
25834 align_to = (align_to < 0
25836 : align_to - window_box_left_offset (it->w, TEXT_AREA));
25837 else if (align_to < 0)
25838 align_to = window_box_left_offset (it->w, TEXT_AREA);
25839 width = max (0, (int)tem + align_to - it->current_x);
25840 zero_width_ok_p = 1;
25842 else
25843 /* Nothing specified -> width defaults to canonical char width. */
25844 width = FRAME_COLUMN_WIDTH (it->f);
25846 if (width <= 0 && (width < 0 || !zero_width_ok_p))
25847 width = 1;
25849 #ifdef HAVE_WINDOW_SYSTEM
25850 /* Compute height. */
25851 if (FRAME_WINDOW_P (it->f))
25853 if ((prop = Fplist_get (plist, QCheight), !NILP (prop))
25854 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
25856 height = (int)tem;
25857 zero_height_ok_p = 1;
25859 else if (prop = Fplist_get (plist, QCrelative_height),
25860 NUMVAL (prop) > 0)
25861 height = FONT_HEIGHT (font) * NUMVAL (prop);
25862 else
25863 height = FONT_HEIGHT (font);
25865 if (height <= 0 && (height < 0 || !zero_height_ok_p))
25866 height = 1;
25868 /* Compute percentage of height used for ascent. If
25869 `:ascent ASCENT' is present and valid, use that. Otherwise,
25870 derive the ascent from the font in use. */
25871 if (prop = Fplist_get (plist, QCascent),
25872 NUMVAL (prop) > 0 && NUMVAL (prop) <= 100)
25873 ascent = height * NUMVAL (prop) / 100.0;
25874 else if (!NILP (prop)
25875 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
25876 ascent = min (max (0, (int)tem), height);
25877 else
25878 ascent = (height * FONT_BASE (font)) / FONT_HEIGHT (font);
25880 else
25881 #endif /* HAVE_WINDOW_SYSTEM */
25882 height = 1;
25884 if (width > 0 && it->line_wrap != TRUNCATE
25885 && it->current_x + width > it->last_visible_x)
25887 width = it->last_visible_x - it->current_x;
25888 #ifdef HAVE_WINDOW_SYSTEM
25889 /* Subtract one more pixel from the stretch width, but only on
25890 GUI frames, since on a TTY each glyph is one "pixel" wide. */
25891 width -= FRAME_WINDOW_P (it->f);
25892 #endif
25895 if (width > 0 && height > 0 && it->glyph_row)
25897 Lisp_Object o_object = it->object;
25898 Lisp_Object object = it->stack[it->sp - 1].string;
25899 int n = width;
25901 if (!STRINGP (object))
25902 object = it->w->contents;
25903 #ifdef HAVE_WINDOW_SYSTEM
25904 if (FRAME_WINDOW_P (it->f))
25905 append_stretch_glyph (it, object, width, height, ascent);
25906 else
25907 #endif
25909 it->object = object;
25910 it->char_to_display = ' ';
25911 it->pixel_width = it->len = 1;
25912 while (n--)
25913 tty_append_glyph (it);
25914 it->object = o_object;
25918 it->pixel_width = width;
25919 #ifdef HAVE_WINDOW_SYSTEM
25920 if (FRAME_WINDOW_P (it->f))
25922 it->ascent = it->phys_ascent = ascent;
25923 it->descent = it->phys_descent = height - it->ascent;
25924 it->nglyphs = width > 0 && height > 0 ? 1 : 0;
25925 take_vertical_position_into_account (it);
25927 else
25928 #endif
25929 it->nglyphs = width;
25932 /* Get information about special display element WHAT in an
25933 environment described by IT. WHAT is one of IT_TRUNCATION or
25934 IT_CONTINUATION. Maybe produce glyphs for WHAT if IT has a
25935 non-null glyph_row member. This function ensures that fields like
25936 face_id, c, len of IT are left untouched. */
25938 static void
25939 produce_special_glyphs (struct it *it, enum display_element_type what)
25941 struct it temp_it;
25942 Lisp_Object gc;
25943 GLYPH glyph;
25945 temp_it = *it;
25946 temp_it.object = Qnil;
25947 memset (&temp_it.current, 0, sizeof temp_it.current);
25949 if (what == IT_CONTINUATION)
25951 /* Continuation glyph. For R2L lines, we mirror it by hand. */
25952 if (it->bidi_it.paragraph_dir == R2L)
25953 SET_GLYPH_FROM_CHAR (glyph, '/');
25954 else
25955 SET_GLYPH_FROM_CHAR (glyph, '\\');
25956 if (it->dp
25957 && (gc = DISP_CONTINUE_GLYPH (it->dp), GLYPH_CODE_P (gc)))
25959 /* FIXME: Should we mirror GC for R2L lines? */
25960 SET_GLYPH_FROM_GLYPH_CODE (glyph, gc);
25961 spec_glyph_lookup_face (XWINDOW (it->window), &glyph);
25964 else if (what == IT_TRUNCATION)
25966 /* Truncation glyph. */
25967 SET_GLYPH_FROM_CHAR (glyph, '$');
25968 if (it->dp
25969 && (gc = DISP_TRUNC_GLYPH (it->dp), GLYPH_CODE_P (gc)))
25971 /* FIXME: Should we mirror GC for R2L lines? */
25972 SET_GLYPH_FROM_GLYPH_CODE (glyph, gc);
25973 spec_glyph_lookup_face (XWINDOW (it->window), &glyph);
25976 else
25977 emacs_abort ();
25979 #ifdef HAVE_WINDOW_SYSTEM
25980 /* On a GUI frame, when the right fringe (left fringe for R2L rows)
25981 is turned off, we precede the truncation/continuation glyphs by a
25982 stretch glyph whose width is computed such that these special
25983 glyphs are aligned at the window margin, even when very different
25984 fonts are used in different glyph rows. */
25985 if (FRAME_WINDOW_P (temp_it.f)
25986 /* init_iterator calls this with it->glyph_row == NULL, and it
25987 wants only the pixel width of the truncation/continuation
25988 glyphs. */
25989 && temp_it.glyph_row
25990 /* insert_left_trunc_glyphs calls us at the beginning of the
25991 row, and it has its own calculation of the stretch glyph
25992 width. */
25993 && temp_it.glyph_row->used[TEXT_AREA] > 0
25994 && (temp_it.glyph_row->reversed_p
25995 ? WINDOW_LEFT_FRINGE_WIDTH (temp_it.w)
25996 : WINDOW_RIGHT_FRINGE_WIDTH (temp_it.w)) == 0)
25998 int stretch_width = temp_it.last_visible_x - temp_it.current_x;
26000 if (stretch_width > 0)
26002 struct face *face = FACE_FROM_ID (temp_it.f, temp_it.face_id);
26003 struct font *font =
26004 face->font ? face->font : FRAME_FONT (temp_it.f);
26005 int stretch_ascent =
26006 (((temp_it.ascent + temp_it.descent)
26007 * FONT_BASE (font)) / FONT_HEIGHT (font));
26009 append_stretch_glyph (&temp_it, Qnil, stretch_width,
26010 temp_it.ascent + temp_it.descent,
26011 stretch_ascent);
26014 #endif
26016 temp_it.dp = NULL;
26017 temp_it.what = IT_CHARACTER;
26018 temp_it.c = temp_it.char_to_display = GLYPH_CHAR (glyph);
26019 temp_it.face_id = GLYPH_FACE (glyph);
26020 temp_it.len = CHAR_BYTES (temp_it.c);
26022 PRODUCE_GLYPHS (&temp_it);
26023 it->pixel_width = temp_it.pixel_width;
26024 it->nglyphs = temp_it.nglyphs;
26027 #ifdef HAVE_WINDOW_SYSTEM
26029 /* Calculate line-height and line-spacing properties.
26030 An integer value specifies explicit pixel value.
26031 A float value specifies relative value to current face height.
26032 A cons (float . face-name) specifies relative value to
26033 height of specified face font.
26035 Returns height in pixels, or nil. */
26038 static Lisp_Object
26039 calc_line_height_property (struct it *it, Lisp_Object val, struct font *font,
26040 int boff, int override)
26042 Lisp_Object face_name = Qnil;
26043 int ascent, descent, height;
26045 if (NILP (val) || INTEGERP (val) || (override && EQ (val, Qt)))
26046 return val;
26048 if (CONSP (val))
26050 face_name = XCAR (val);
26051 val = XCDR (val);
26052 if (!NUMBERP (val))
26053 val = make_number (1);
26054 if (NILP (face_name))
26056 height = it->ascent + it->descent;
26057 goto scale;
26061 if (NILP (face_name))
26063 font = FRAME_FONT (it->f);
26064 boff = FRAME_BASELINE_OFFSET (it->f);
26066 else if (EQ (face_name, Qt))
26068 override = 0;
26070 else
26072 int face_id;
26073 struct face *face;
26075 face_id = lookup_named_face (it->f, face_name, 0);
26076 if (face_id < 0)
26077 return make_number (-1);
26079 face = FACE_FROM_ID (it->f, face_id);
26080 font = face->font;
26081 if (font == NULL)
26082 return make_number (-1);
26083 boff = font->baseline_offset;
26084 if (font->vertical_centering)
26085 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
26088 ascent = FONT_BASE (font) + boff;
26089 descent = FONT_DESCENT (font) - boff;
26091 if (override)
26093 it->override_ascent = ascent;
26094 it->override_descent = descent;
26095 it->override_boff = boff;
26098 height = ascent + descent;
26100 scale:
26101 if (FLOATP (val))
26102 height = (int)(XFLOAT_DATA (val) * height);
26103 else if (INTEGERP (val))
26104 height *= XINT (val);
26106 return make_number (height);
26110 /* Append a glyph for a glyphless character to IT->glyph_row. FACE_ID
26111 is a face ID to be used for the glyph. FOR_NO_FONT is nonzero if
26112 and only if this is for a character for which no font was found.
26114 If the display method (it->glyphless_method) is
26115 GLYPHLESS_DISPLAY_ACRONYM or GLYPHLESS_DISPLAY_HEX_CODE, LEN is a
26116 length of the acronym or the hexadecimal string, UPPER_XOFF and
26117 UPPER_YOFF are pixel offsets for the upper part of the string,
26118 LOWER_XOFF and LOWER_YOFF are for the lower part.
26120 For the other display methods, LEN through LOWER_YOFF are zero. */
26122 static void
26123 append_glyphless_glyph (struct it *it, int face_id, int for_no_font, int len,
26124 short upper_xoff, short upper_yoff,
26125 short lower_xoff, short lower_yoff)
26127 struct glyph *glyph;
26128 enum glyph_row_area area = it->area;
26130 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
26131 if (glyph < it->glyph_row->glyphs[area + 1])
26133 /* If the glyph row is reversed, we need to prepend the glyph
26134 rather than append it. */
26135 if (it->glyph_row->reversed_p && area == TEXT_AREA)
26137 struct glyph *g;
26139 /* Make room for the additional glyph. */
26140 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
26141 g[1] = *g;
26142 glyph = it->glyph_row->glyphs[area];
26144 glyph->charpos = CHARPOS (it->position);
26145 glyph->object = it->object;
26146 glyph->pixel_width = it->pixel_width;
26147 glyph->ascent = it->ascent;
26148 glyph->descent = it->descent;
26149 glyph->voffset = it->voffset;
26150 glyph->type = GLYPHLESS_GLYPH;
26151 glyph->u.glyphless.method = it->glyphless_method;
26152 glyph->u.glyphless.for_no_font = for_no_font;
26153 glyph->u.glyphless.len = len;
26154 glyph->u.glyphless.ch = it->c;
26155 glyph->slice.glyphless.upper_xoff = upper_xoff;
26156 glyph->slice.glyphless.upper_yoff = upper_yoff;
26157 glyph->slice.glyphless.lower_xoff = lower_xoff;
26158 glyph->slice.glyphless.lower_yoff = lower_yoff;
26159 glyph->avoid_cursor_p = it->avoid_cursor_p;
26160 glyph->multibyte_p = it->multibyte_p;
26161 if (it->glyph_row->reversed_p && area == TEXT_AREA)
26163 /* In R2L rows, the left and the right box edges need to be
26164 drawn in reverse direction. */
26165 glyph->right_box_line_p = it->start_of_box_run_p;
26166 glyph->left_box_line_p = it->end_of_box_run_p;
26168 else
26170 glyph->left_box_line_p = it->start_of_box_run_p;
26171 glyph->right_box_line_p = it->end_of_box_run_p;
26173 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
26174 || it->phys_descent > it->descent);
26175 glyph->padding_p = 0;
26176 glyph->glyph_not_available_p = 0;
26177 glyph->face_id = face_id;
26178 glyph->font_type = FONT_TYPE_UNKNOWN;
26179 if (it->bidi_p)
26181 glyph->resolved_level = it->bidi_it.resolved_level;
26182 eassert ((it->bidi_it.type & 7) == it->bidi_it.type);
26183 glyph->bidi_type = it->bidi_it.type;
26185 ++it->glyph_row->used[area];
26187 else
26188 IT_EXPAND_MATRIX_WIDTH (it, area);
26192 /* Produce a glyph for a glyphless character for iterator IT.
26193 IT->glyphless_method specifies which method to use for displaying
26194 the character. See the description of enum
26195 glyphless_display_method in dispextern.h for the detail.
26197 FOR_NO_FONT is nonzero if and only if this is for a character for
26198 which no font was found. ACRONYM, if non-nil, is an acronym string
26199 for the character. */
26201 static void
26202 produce_glyphless_glyph (struct it *it, int for_no_font, Lisp_Object acronym)
26204 int face_id;
26205 struct face *face;
26206 struct font *font;
26207 int base_width, base_height, width, height;
26208 short upper_xoff, upper_yoff, lower_xoff, lower_yoff;
26209 int len;
26211 /* Get the metrics of the base font. We always refer to the current
26212 ASCII face. */
26213 face = FACE_FROM_ID (it->f, it->face_id)->ascii_face;
26214 font = face->font ? face->font : FRAME_FONT (it->f);
26215 it->ascent = FONT_BASE (font) + font->baseline_offset;
26216 it->descent = FONT_DESCENT (font) - font->baseline_offset;
26217 base_height = it->ascent + it->descent;
26218 base_width = font->average_width;
26220 face_id = merge_glyphless_glyph_face (it);
26222 if (it->glyphless_method == GLYPHLESS_DISPLAY_THIN_SPACE)
26224 it->pixel_width = THIN_SPACE_WIDTH;
26225 len = 0;
26226 upper_xoff = upper_yoff = lower_xoff = lower_yoff = 0;
26228 else if (it->glyphless_method == GLYPHLESS_DISPLAY_EMPTY_BOX)
26230 width = CHAR_WIDTH (it->c);
26231 if (width == 0)
26232 width = 1;
26233 else if (width > 4)
26234 width = 4;
26235 it->pixel_width = base_width * width;
26236 len = 0;
26237 upper_xoff = upper_yoff = lower_xoff = lower_yoff = 0;
26239 else
26241 char buf[7];
26242 const char *str;
26243 unsigned int code[6];
26244 int upper_len;
26245 int ascent, descent;
26246 struct font_metrics metrics_upper, metrics_lower;
26248 face = FACE_FROM_ID (it->f, face_id);
26249 font = face->font ? face->font : FRAME_FONT (it->f);
26250 prepare_face_for_display (it->f, face);
26252 if (it->glyphless_method == GLYPHLESS_DISPLAY_ACRONYM)
26254 if (! STRINGP (acronym) && CHAR_TABLE_P (Vglyphless_char_display))
26255 acronym = CHAR_TABLE_REF (Vglyphless_char_display, it->c);
26256 if (CONSP (acronym))
26257 acronym = XCAR (acronym);
26258 str = STRINGP (acronym) ? SSDATA (acronym) : "";
26260 else
26262 eassert (it->glyphless_method == GLYPHLESS_DISPLAY_HEX_CODE);
26263 sprintf (buf, "%0*X", it->c < 0x10000 ? 4 : 6, it->c);
26264 str = buf;
26266 for (len = 0; str[len] && ASCII_CHAR_P (str[len]) && len < 6; len++)
26267 code[len] = font->driver->encode_char (font, str[len]);
26268 upper_len = (len + 1) / 2;
26269 font->driver->text_extents (font, code, upper_len,
26270 &metrics_upper);
26271 font->driver->text_extents (font, code + upper_len, len - upper_len,
26272 &metrics_lower);
26276 /* +4 is for vertical bars of a box plus 1-pixel spaces at both side. */
26277 width = max (metrics_upper.width, metrics_lower.width) + 4;
26278 upper_xoff = upper_yoff = 2; /* the typical case */
26279 if (base_width >= width)
26281 /* Align the upper to the left, the lower to the right. */
26282 it->pixel_width = base_width;
26283 lower_xoff = base_width - 2 - metrics_lower.width;
26285 else
26287 /* Center the shorter one. */
26288 it->pixel_width = width;
26289 if (metrics_upper.width >= metrics_lower.width)
26290 lower_xoff = (width - metrics_lower.width) / 2;
26291 else
26293 /* FIXME: This code doesn't look right. It formerly was
26294 missing the "lower_xoff = 0;", which couldn't have
26295 been right since it left lower_xoff uninitialized. */
26296 lower_xoff = 0;
26297 upper_xoff = (width - metrics_upper.width) / 2;
26301 /* +5 is for horizontal bars of a box plus 1-pixel spaces at
26302 top, bottom, and between upper and lower strings. */
26303 height = (metrics_upper.ascent + metrics_upper.descent
26304 + metrics_lower.ascent + metrics_lower.descent) + 5;
26305 /* Center vertically.
26306 H:base_height, D:base_descent
26307 h:height, ld:lower_descent, la:lower_ascent, ud:upper_descent
26309 ascent = - (D - H/2 - h/2 + 1); "+ 1" for rounding up
26310 descent = D - H/2 + h/2;
26311 lower_yoff = descent - 2 - ld;
26312 upper_yoff = lower_yoff - la - 1 - ud; */
26313 ascent = - (it->descent - (base_height + height + 1) / 2);
26314 descent = it->descent - (base_height - height) / 2;
26315 lower_yoff = descent - 2 - metrics_lower.descent;
26316 upper_yoff = (lower_yoff - metrics_lower.ascent - 1
26317 - metrics_upper.descent);
26318 /* Don't make the height shorter than the base height. */
26319 if (height > base_height)
26321 it->ascent = ascent;
26322 it->descent = descent;
26326 it->phys_ascent = it->ascent;
26327 it->phys_descent = it->descent;
26328 if (it->glyph_row)
26329 append_glyphless_glyph (it, face_id, for_no_font, len,
26330 upper_xoff, upper_yoff,
26331 lower_xoff, lower_yoff);
26332 it->nglyphs = 1;
26333 take_vertical_position_into_account (it);
26337 /* RIF:
26338 Produce glyphs/get display metrics for the display element IT is
26339 loaded with. See the description of struct it in dispextern.h
26340 for an overview of struct it. */
26342 void
26343 x_produce_glyphs (struct it *it)
26345 int extra_line_spacing = it->extra_line_spacing;
26347 it->glyph_not_available_p = 0;
26349 if (it->what == IT_CHARACTER)
26351 XChar2b char2b;
26352 struct face *face = FACE_FROM_ID (it->f, it->face_id);
26353 struct font *font = face->font;
26354 struct font_metrics *pcm = NULL;
26355 int boff; /* Baseline offset. */
26357 if (font == NULL)
26359 /* When no suitable font is found, display this character by
26360 the method specified in the first extra slot of
26361 Vglyphless_char_display. */
26362 Lisp_Object acronym = lookup_glyphless_char_display (-1, it);
26364 eassert (it->what == IT_GLYPHLESS);
26365 produce_glyphless_glyph (it, 1, STRINGP (acronym) ? acronym : Qnil);
26366 goto done;
26369 boff = font->baseline_offset;
26370 if (font->vertical_centering)
26371 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
26373 if (it->char_to_display != '\n' && it->char_to_display != '\t')
26375 int stretched_p;
26377 it->nglyphs = 1;
26379 if (it->override_ascent >= 0)
26381 it->ascent = it->override_ascent;
26382 it->descent = it->override_descent;
26383 boff = it->override_boff;
26385 else
26387 it->ascent = FONT_BASE (font) + boff;
26388 it->descent = FONT_DESCENT (font) - boff;
26391 if (get_char_glyph_code (it->char_to_display, font, &char2b))
26393 pcm = get_per_char_metric (font, &char2b);
26394 if (pcm->width == 0
26395 && pcm->rbearing == 0 && pcm->lbearing == 0)
26396 pcm = NULL;
26399 if (pcm)
26401 it->phys_ascent = pcm->ascent + boff;
26402 it->phys_descent = pcm->descent - boff;
26403 it->pixel_width = pcm->width;
26405 else
26407 it->glyph_not_available_p = 1;
26408 it->phys_ascent = it->ascent;
26409 it->phys_descent = it->descent;
26410 it->pixel_width = font->space_width;
26413 if (it->constrain_row_ascent_descent_p)
26415 if (it->descent > it->max_descent)
26417 it->ascent += it->descent - it->max_descent;
26418 it->descent = it->max_descent;
26420 if (it->ascent > it->max_ascent)
26422 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
26423 it->ascent = it->max_ascent;
26425 it->phys_ascent = min (it->phys_ascent, it->ascent);
26426 it->phys_descent = min (it->phys_descent, it->descent);
26427 extra_line_spacing = 0;
26430 /* If this is a space inside a region of text with
26431 `space-width' property, change its width. */
26432 stretched_p = it->char_to_display == ' ' && !NILP (it->space_width);
26433 if (stretched_p)
26434 it->pixel_width *= XFLOATINT (it->space_width);
26436 /* If face has a box, add the box thickness to the character
26437 height. If character has a box line to the left and/or
26438 right, add the box line width to the character's width. */
26439 if (face->box != FACE_NO_BOX)
26441 int thick = face->box_line_width;
26443 if (thick > 0)
26445 it->ascent += thick;
26446 it->descent += thick;
26448 else
26449 thick = -thick;
26451 if (it->start_of_box_run_p)
26452 it->pixel_width += thick;
26453 if (it->end_of_box_run_p)
26454 it->pixel_width += thick;
26457 /* If face has an overline, add the height of the overline
26458 (1 pixel) and a 1 pixel margin to the character height. */
26459 if (face->overline_p)
26460 it->ascent += overline_margin;
26462 if (it->constrain_row_ascent_descent_p)
26464 if (it->ascent > it->max_ascent)
26465 it->ascent = it->max_ascent;
26466 if (it->descent > it->max_descent)
26467 it->descent = it->max_descent;
26470 take_vertical_position_into_account (it);
26472 /* If we have to actually produce glyphs, do it. */
26473 if (it->glyph_row)
26475 if (stretched_p)
26477 /* Translate a space with a `space-width' property
26478 into a stretch glyph. */
26479 int ascent = (((it->ascent + it->descent) * FONT_BASE (font))
26480 / FONT_HEIGHT (font));
26481 append_stretch_glyph (it, it->object, it->pixel_width,
26482 it->ascent + it->descent, ascent);
26484 else
26485 append_glyph (it);
26487 /* If characters with lbearing or rbearing are displayed
26488 in this line, record that fact in a flag of the
26489 glyph row. This is used to optimize X output code. */
26490 if (pcm && (pcm->lbearing < 0 || pcm->rbearing > pcm->width))
26491 it->glyph_row->contains_overlapping_glyphs_p = 1;
26493 if (! stretched_p && it->pixel_width == 0)
26494 /* We assure that all visible glyphs have at least 1-pixel
26495 width. */
26496 it->pixel_width = 1;
26498 else if (it->char_to_display == '\n')
26500 /* A newline has no width, but we need the height of the
26501 line. But if previous part of the line sets a height,
26502 don't increase that height. */
26504 Lisp_Object height;
26505 Lisp_Object total_height = Qnil;
26507 it->override_ascent = -1;
26508 it->pixel_width = 0;
26509 it->nglyphs = 0;
26511 height = get_it_property (it, Qline_height);
26512 /* Split (line-height total-height) list. */
26513 if (CONSP (height)
26514 && CONSP (XCDR (height))
26515 && NILP (XCDR (XCDR (height))))
26517 total_height = XCAR (XCDR (height));
26518 height = XCAR (height);
26520 height = calc_line_height_property (it, height, font, boff, 1);
26522 if (it->override_ascent >= 0)
26524 it->ascent = it->override_ascent;
26525 it->descent = it->override_descent;
26526 boff = it->override_boff;
26528 else
26530 it->ascent = FONT_BASE (font) + boff;
26531 it->descent = FONT_DESCENT (font) - boff;
26534 if (EQ (height, Qt))
26536 if (it->descent > it->max_descent)
26538 it->ascent += it->descent - it->max_descent;
26539 it->descent = it->max_descent;
26541 if (it->ascent > it->max_ascent)
26543 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
26544 it->ascent = it->max_ascent;
26546 it->phys_ascent = min (it->phys_ascent, it->ascent);
26547 it->phys_descent = min (it->phys_descent, it->descent);
26548 it->constrain_row_ascent_descent_p = 1;
26549 extra_line_spacing = 0;
26551 else
26553 Lisp_Object spacing;
26555 it->phys_ascent = it->ascent;
26556 it->phys_descent = it->descent;
26558 if ((it->max_ascent > 0 || it->max_descent > 0)
26559 && face->box != FACE_NO_BOX
26560 && face->box_line_width > 0)
26562 it->ascent += face->box_line_width;
26563 it->descent += face->box_line_width;
26565 if (!NILP (height)
26566 && XINT (height) > it->ascent + it->descent)
26567 it->ascent = XINT (height) - it->descent;
26569 if (!NILP (total_height))
26570 spacing = calc_line_height_property (it, total_height, font, boff, 0);
26571 else
26573 spacing = get_it_property (it, Qline_spacing);
26574 spacing = calc_line_height_property (it, spacing, font, boff, 0);
26576 if (INTEGERP (spacing))
26578 extra_line_spacing = XINT (spacing);
26579 if (!NILP (total_height))
26580 extra_line_spacing -= (it->phys_ascent + it->phys_descent);
26584 else /* i.e. (it->char_to_display == '\t') */
26586 if (font->space_width > 0)
26588 int tab_width = it->tab_width * font->space_width;
26589 int x = it->current_x + it->continuation_lines_width;
26590 int next_tab_x = ((1 + x + tab_width - 1) / tab_width) * tab_width;
26592 /* If the distance from the current position to the next tab
26593 stop is less than a space character width, use the
26594 tab stop after that. */
26595 if (next_tab_x - x < font->space_width)
26596 next_tab_x += tab_width;
26598 it->pixel_width = next_tab_x - x;
26599 it->nglyphs = 1;
26600 it->ascent = it->phys_ascent = FONT_BASE (font) + boff;
26601 it->descent = it->phys_descent = FONT_DESCENT (font) - boff;
26603 if (it->glyph_row)
26605 append_stretch_glyph (it, it->object, it->pixel_width,
26606 it->ascent + it->descent, it->ascent);
26609 else
26611 it->pixel_width = 0;
26612 it->nglyphs = 1;
26616 else if (it->what == IT_COMPOSITION && it->cmp_it.ch < 0)
26618 /* A static composition.
26620 Note: A composition is represented as one glyph in the
26621 glyph matrix. There are no padding glyphs.
26623 Important note: pixel_width, ascent, and descent are the
26624 values of what is drawn by draw_glyphs (i.e. the values of
26625 the overall glyphs composed). */
26626 struct face *face = FACE_FROM_ID (it->f, it->face_id);
26627 int boff; /* baseline offset */
26628 struct composition *cmp = composition_table[it->cmp_it.id];
26629 int glyph_len = cmp->glyph_len;
26630 struct font *font = face->font;
26632 it->nglyphs = 1;
26634 /* If we have not yet calculated pixel size data of glyphs of
26635 the composition for the current face font, calculate them
26636 now. Theoretically, we have to check all fonts for the
26637 glyphs, but that requires much time and memory space. So,
26638 here we check only the font of the first glyph. This may
26639 lead to incorrect display, but it's very rare, and C-l
26640 (recenter-top-bottom) can correct the display anyway. */
26641 if (! cmp->font || cmp->font != font)
26643 /* Ascent and descent of the font of the first character
26644 of this composition (adjusted by baseline offset).
26645 Ascent and descent of overall glyphs should not be less
26646 than these, respectively. */
26647 int font_ascent, font_descent, font_height;
26648 /* Bounding box of the overall glyphs. */
26649 int leftmost, rightmost, lowest, highest;
26650 int lbearing, rbearing;
26651 int i, width, ascent, descent;
26652 int left_padded = 0, right_padded = 0;
26653 int c IF_LINT (= 0); /* cmp->glyph_len can't be zero; see Bug#8512 */
26654 XChar2b char2b;
26655 struct font_metrics *pcm;
26656 int font_not_found_p;
26657 ptrdiff_t pos;
26659 for (glyph_len = cmp->glyph_len; glyph_len > 0; glyph_len--)
26660 if ((c = COMPOSITION_GLYPH (cmp, glyph_len - 1)) != '\t')
26661 break;
26662 if (glyph_len < cmp->glyph_len)
26663 right_padded = 1;
26664 for (i = 0; i < glyph_len; i++)
26666 if ((c = COMPOSITION_GLYPH (cmp, i)) != '\t')
26667 break;
26668 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
26670 if (i > 0)
26671 left_padded = 1;
26673 pos = (STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
26674 : IT_CHARPOS (*it));
26675 /* If no suitable font is found, use the default font. */
26676 font_not_found_p = font == NULL;
26677 if (font_not_found_p)
26679 face = face->ascii_face;
26680 font = face->font;
26682 boff = font->baseline_offset;
26683 if (font->vertical_centering)
26684 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
26685 font_ascent = FONT_BASE (font) + boff;
26686 font_descent = FONT_DESCENT (font) - boff;
26687 font_height = FONT_HEIGHT (font);
26689 cmp->font = font;
26691 pcm = NULL;
26692 if (! font_not_found_p)
26694 get_char_face_and_encoding (it->f, c, it->face_id,
26695 &char2b, 0);
26696 pcm = get_per_char_metric (font, &char2b);
26699 /* Initialize the bounding box. */
26700 if (pcm)
26702 width = cmp->glyph_len > 0 ? pcm->width : 0;
26703 ascent = pcm->ascent;
26704 descent = pcm->descent;
26705 lbearing = pcm->lbearing;
26706 rbearing = pcm->rbearing;
26708 else
26710 width = cmp->glyph_len > 0 ? font->space_width : 0;
26711 ascent = FONT_BASE (font);
26712 descent = FONT_DESCENT (font);
26713 lbearing = 0;
26714 rbearing = width;
26717 rightmost = width;
26718 leftmost = 0;
26719 lowest = - descent + boff;
26720 highest = ascent + boff;
26722 if (! font_not_found_p
26723 && font->default_ascent
26724 && CHAR_TABLE_P (Vuse_default_ascent)
26725 && !NILP (Faref (Vuse_default_ascent,
26726 make_number (it->char_to_display))))
26727 highest = font->default_ascent + boff;
26729 /* Draw the first glyph at the normal position. It may be
26730 shifted to right later if some other glyphs are drawn
26731 at the left. */
26732 cmp->offsets[i * 2] = 0;
26733 cmp->offsets[i * 2 + 1] = boff;
26734 cmp->lbearing = lbearing;
26735 cmp->rbearing = rbearing;
26737 /* Set cmp->offsets for the remaining glyphs. */
26738 for (i++; i < glyph_len; i++)
26740 int left, right, btm, top;
26741 int ch = COMPOSITION_GLYPH (cmp, i);
26742 int face_id;
26743 struct face *this_face;
26745 if (ch == '\t')
26746 ch = ' ';
26747 face_id = FACE_FOR_CHAR (it->f, face, ch, pos, it->string);
26748 this_face = FACE_FROM_ID (it->f, face_id);
26749 font = this_face->font;
26751 if (font == NULL)
26752 pcm = NULL;
26753 else
26755 get_char_face_and_encoding (it->f, ch, face_id,
26756 &char2b, 0);
26757 pcm = get_per_char_metric (font, &char2b);
26759 if (! pcm)
26760 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
26761 else
26763 width = pcm->width;
26764 ascent = pcm->ascent;
26765 descent = pcm->descent;
26766 lbearing = pcm->lbearing;
26767 rbearing = pcm->rbearing;
26768 if (cmp->method != COMPOSITION_WITH_RULE_ALTCHARS)
26770 /* Relative composition with or without
26771 alternate chars. */
26772 left = (leftmost + rightmost - width) / 2;
26773 btm = - descent + boff;
26774 if (font->relative_compose
26775 && (! CHAR_TABLE_P (Vignore_relative_composition)
26776 || NILP (Faref (Vignore_relative_composition,
26777 make_number (ch)))))
26780 if (- descent >= font->relative_compose)
26781 /* One extra pixel between two glyphs. */
26782 btm = highest + 1;
26783 else if (ascent <= 0)
26784 /* One extra pixel between two glyphs. */
26785 btm = lowest - 1 - ascent - descent;
26788 else
26790 /* A composition rule is specified by an integer
26791 value that encodes global and new reference
26792 points (GREF and NREF). GREF and NREF are
26793 specified by numbers as below:
26795 0---1---2 -- ascent
26799 9--10--11 -- center
26801 ---3---4---5--- baseline
26803 6---7---8 -- descent
26805 int rule = COMPOSITION_RULE (cmp, i);
26806 int gref, nref, grefx, grefy, nrefx, nrefy, xoff, yoff;
26808 COMPOSITION_DECODE_RULE (rule, gref, nref, xoff, yoff);
26809 grefx = gref % 3, nrefx = nref % 3;
26810 grefy = gref / 3, nrefy = nref / 3;
26811 if (xoff)
26812 xoff = font_height * (xoff - 128) / 256;
26813 if (yoff)
26814 yoff = font_height * (yoff - 128) / 256;
26816 left = (leftmost
26817 + grefx * (rightmost - leftmost) / 2
26818 - nrefx * width / 2
26819 + xoff);
26821 btm = ((grefy == 0 ? highest
26822 : grefy == 1 ? 0
26823 : grefy == 2 ? lowest
26824 : (highest + lowest) / 2)
26825 - (nrefy == 0 ? ascent + descent
26826 : nrefy == 1 ? descent - boff
26827 : nrefy == 2 ? 0
26828 : (ascent + descent) / 2)
26829 + yoff);
26832 cmp->offsets[i * 2] = left;
26833 cmp->offsets[i * 2 + 1] = btm + descent;
26835 /* Update the bounding box of the overall glyphs. */
26836 if (width > 0)
26838 right = left + width;
26839 if (left < leftmost)
26840 leftmost = left;
26841 if (right > rightmost)
26842 rightmost = right;
26844 top = btm + descent + ascent;
26845 if (top > highest)
26846 highest = top;
26847 if (btm < lowest)
26848 lowest = btm;
26850 if (cmp->lbearing > left + lbearing)
26851 cmp->lbearing = left + lbearing;
26852 if (cmp->rbearing < left + rbearing)
26853 cmp->rbearing = left + rbearing;
26857 /* If there are glyphs whose x-offsets are negative,
26858 shift all glyphs to the right and make all x-offsets
26859 non-negative. */
26860 if (leftmost < 0)
26862 for (i = 0; i < cmp->glyph_len; i++)
26863 cmp->offsets[i * 2] -= leftmost;
26864 rightmost -= leftmost;
26865 cmp->lbearing -= leftmost;
26866 cmp->rbearing -= leftmost;
26869 if (left_padded && cmp->lbearing < 0)
26871 for (i = 0; i < cmp->glyph_len; i++)
26872 cmp->offsets[i * 2] -= cmp->lbearing;
26873 rightmost -= cmp->lbearing;
26874 cmp->rbearing -= cmp->lbearing;
26875 cmp->lbearing = 0;
26877 if (right_padded && rightmost < cmp->rbearing)
26879 rightmost = cmp->rbearing;
26882 cmp->pixel_width = rightmost;
26883 cmp->ascent = highest;
26884 cmp->descent = - lowest;
26885 if (cmp->ascent < font_ascent)
26886 cmp->ascent = font_ascent;
26887 if (cmp->descent < font_descent)
26888 cmp->descent = font_descent;
26891 if (it->glyph_row
26892 && (cmp->lbearing < 0
26893 || cmp->rbearing > cmp->pixel_width))
26894 it->glyph_row->contains_overlapping_glyphs_p = 1;
26896 it->pixel_width = cmp->pixel_width;
26897 it->ascent = it->phys_ascent = cmp->ascent;
26898 it->descent = it->phys_descent = cmp->descent;
26899 if (face->box != FACE_NO_BOX)
26901 int thick = face->box_line_width;
26903 if (thick > 0)
26905 it->ascent += thick;
26906 it->descent += thick;
26908 else
26909 thick = - thick;
26911 if (it->start_of_box_run_p)
26912 it->pixel_width += thick;
26913 if (it->end_of_box_run_p)
26914 it->pixel_width += thick;
26917 /* If face has an overline, add the height of the overline
26918 (1 pixel) and a 1 pixel margin to the character height. */
26919 if (face->overline_p)
26920 it->ascent += overline_margin;
26922 take_vertical_position_into_account (it);
26923 if (it->ascent < 0)
26924 it->ascent = 0;
26925 if (it->descent < 0)
26926 it->descent = 0;
26928 if (it->glyph_row && cmp->glyph_len > 0)
26929 append_composite_glyph (it);
26931 else if (it->what == IT_COMPOSITION)
26933 /* A dynamic (automatic) composition. */
26934 struct face *face = FACE_FROM_ID (it->f, it->face_id);
26935 Lisp_Object gstring;
26936 struct font_metrics metrics;
26938 it->nglyphs = 1;
26940 gstring = composition_gstring_from_id (it->cmp_it.id);
26941 it->pixel_width
26942 = composition_gstring_width (gstring, it->cmp_it.from, it->cmp_it.to,
26943 &metrics);
26944 if (it->glyph_row
26945 && (metrics.lbearing < 0 || metrics.rbearing > metrics.width))
26946 it->glyph_row->contains_overlapping_glyphs_p = 1;
26947 it->ascent = it->phys_ascent = metrics.ascent;
26948 it->descent = it->phys_descent = metrics.descent;
26949 if (face->box != FACE_NO_BOX)
26951 int thick = face->box_line_width;
26953 if (thick > 0)
26955 it->ascent += thick;
26956 it->descent += thick;
26958 else
26959 thick = - thick;
26961 if (it->start_of_box_run_p)
26962 it->pixel_width += thick;
26963 if (it->end_of_box_run_p)
26964 it->pixel_width += thick;
26966 /* If face has an overline, add the height of the overline
26967 (1 pixel) and a 1 pixel margin to the character height. */
26968 if (face->overline_p)
26969 it->ascent += overline_margin;
26970 take_vertical_position_into_account (it);
26971 if (it->ascent < 0)
26972 it->ascent = 0;
26973 if (it->descent < 0)
26974 it->descent = 0;
26976 if (it->glyph_row)
26977 append_composite_glyph (it);
26979 else if (it->what == IT_GLYPHLESS)
26980 produce_glyphless_glyph (it, 0, Qnil);
26981 else if (it->what == IT_IMAGE)
26982 produce_image_glyph (it);
26983 else if (it->what == IT_STRETCH)
26984 produce_stretch_glyph (it);
26986 done:
26987 /* Accumulate dimensions. Note: can't assume that it->descent > 0
26988 because this isn't true for images with `:ascent 100'. */
26989 eassert (it->ascent >= 0 && it->descent >= 0);
26990 if (it->area == TEXT_AREA)
26991 it->current_x += it->pixel_width;
26993 if (extra_line_spacing > 0)
26995 it->descent += extra_line_spacing;
26996 if (extra_line_spacing > it->max_extra_line_spacing)
26997 it->max_extra_line_spacing = extra_line_spacing;
27000 it->max_ascent = max (it->max_ascent, it->ascent);
27001 it->max_descent = max (it->max_descent, it->descent);
27002 it->max_phys_ascent = max (it->max_phys_ascent, it->phys_ascent);
27003 it->max_phys_descent = max (it->max_phys_descent, it->phys_descent);
27006 /* EXPORT for RIF:
27007 Output LEN glyphs starting at START at the nominal cursor position.
27008 Advance the nominal cursor over the text. UPDATED_ROW is the glyph row
27009 being updated, and UPDATED_AREA is the area of that row being updated. */
27011 void
27012 x_write_glyphs (struct window *w, struct glyph_row *updated_row,
27013 struct glyph *start, enum glyph_row_area updated_area, int len)
27015 int x, hpos, chpos = w->phys_cursor.hpos;
27017 eassert (updated_row);
27018 /* When the window is hscrolled, cursor hpos can legitimately be out
27019 of bounds, but we draw the cursor at the corresponding window
27020 margin in that case. */
27021 if (!updated_row->reversed_p && chpos < 0)
27022 chpos = 0;
27023 if (updated_row->reversed_p && chpos >= updated_row->used[TEXT_AREA])
27024 chpos = updated_row->used[TEXT_AREA] - 1;
27026 block_input ();
27028 /* Write glyphs. */
27030 hpos = start - updated_row->glyphs[updated_area];
27031 x = draw_glyphs (w, w->output_cursor.x,
27032 updated_row, updated_area,
27033 hpos, hpos + len,
27034 DRAW_NORMAL_TEXT, 0);
27036 /* Invalidate old phys cursor if the glyph at its hpos is redrawn. */
27037 if (updated_area == TEXT_AREA
27038 && w->phys_cursor_on_p
27039 && w->phys_cursor.vpos == w->output_cursor.vpos
27040 && chpos >= hpos
27041 && chpos < hpos + len)
27042 w->phys_cursor_on_p = 0;
27044 unblock_input ();
27046 /* Advance the output cursor. */
27047 w->output_cursor.hpos += len;
27048 w->output_cursor.x = x;
27052 /* EXPORT for RIF:
27053 Insert LEN glyphs from START at the nominal cursor position. */
27055 void
27056 x_insert_glyphs (struct window *w, struct glyph_row *updated_row,
27057 struct glyph *start, enum glyph_row_area updated_area, int len)
27059 struct frame *f;
27060 int line_height, shift_by_width, shifted_region_width;
27061 struct glyph_row *row;
27062 struct glyph *glyph;
27063 int frame_x, frame_y;
27064 ptrdiff_t hpos;
27066 eassert (updated_row);
27067 block_input ();
27068 f = XFRAME (WINDOW_FRAME (w));
27070 /* Get the height of the line we are in. */
27071 row = updated_row;
27072 line_height = row->height;
27074 /* Get the width of the glyphs to insert. */
27075 shift_by_width = 0;
27076 for (glyph = start; glyph < start + len; ++glyph)
27077 shift_by_width += glyph->pixel_width;
27079 /* Get the width of the region to shift right. */
27080 shifted_region_width = (window_box_width (w, updated_area)
27081 - w->output_cursor.x
27082 - shift_by_width);
27084 /* Shift right. */
27085 frame_x = window_box_left (w, updated_area) + w->output_cursor.x;
27086 frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, w->output_cursor.y);
27088 FRAME_RIF (f)->shift_glyphs_for_insert (f, frame_x, frame_y, shifted_region_width,
27089 line_height, shift_by_width);
27091 /* Write the glyphs. */
27092 hpos = start - row->glyphs[updated_area];
27093 draw_glyphs (w, w->output_cursor.x, row, updated_area,
27094 hpos, hpos + len,
27095 DRAW_NORMAL_TEXT, 0);
27097 /* Advance the output cursor. */
27098 w->output_cursor.hpos += len;
27099 w->output_cursor.x += shift_by_width;
27100 unblock_input ();
27104 /* EXPORT for RIF:
27105 Erase the current text line from the nominal cursor position
27106 (inclusive) to pixel column TO_X (exclusive). The idea is that
27107 everything from TO_X onward is already erased.
27109 TO_X is a pixel position relative to UPDATED_AREA of currently
27110 updated window W. TO_X == -1 means clear to the end of this area. */
27112 void
27113 x_clear_end_of_line (struct window *w, struct glyph_row *updated_row,
27114 enum glyph_row_area updated_area, int to_x)
27116 struct frame *f;
27117 int max_x, min_y, max_y;
27118 int from_x, from_y, to_y;
27120 eassert (updated_row);
27121 f = XFRAME (w->frame);
27123 if (updated_row->full_width_p)
27124 max_x = (WINDOW_PIXEL_WIDTH (w)
27125 - (updated_row->mode_line_p ? WINDOW_RIGHT_DIVIDER_WIDTH (w) : 0));
27126 else
27127 max_x = window_box_width (w, updated_area);
27128 max_y = window_text_bottom_y (w);
27130 /* TO_X == 0 means don't do anything. TO_X < 0 means clear to end
27131 of window. For TO_X > 0, truncate to end of drawing area. */
27132 if (to_x == 0)
27133 return;
27134 else if (to_x < 0)
27135 to_x = max_x;
27136 else
27137 to_x = min (to_x, max_x);
27139 to_y = min (max_y, w->output_cursor.y + updated_row->height);
27141 /* Notice if the cursor will be cleared by this operation. */
27142 if (!updated_row->full_width_p)
27143 notice_overwritten_cursor (w, updated_area,
27144 w->output_cursor.x, -1,
27145 updated_row->y,
27146 MATRIX_ROW_BOTTOM_Y (updated_row));
27148 from_x = w->output_cursor.x;
27150 /* Translate to frame coordinates. */
27151 if (updated_row->full_width_p)
27153 from_x = WINDOW_TO_FRAME_PIXEL_X (w, from_x);
27154 to_x = WINDOW_TO_FRAME_PIXEL_X (w, to_x);
27156 else
27158 int area_left = window_box_left (w, updated_area);
27159 from_x += area_left;
27160 to_x += area_left;
27163 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
27164 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, max (min_y, w->output_cursor.y));
27165 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, to_y);
27167 /* Prevent inadvertently clearing to end of the X window. */
27168 if (to_x > from_x && to_y > from_y)
27170 block_input ();
27171 FRAME_RIF (f)->clear_frame_area (f, from_x, from_y,
27172 to_x - from_x, to_y - from_y);
27173 unblock_input ();
27177 #endif /* HAVE_WINDOW_SYSTEM */
27181 /***********************************************************************
27182 Cursor types
27183 ***********************************************************************/
27185 /* Value is the internal representation of the specified cursor type
27186 ARG. If type is BAR_CURSOR, return in *WIDTH the specified width
27187 of the bar cursor. */
27189 static enum text_cursor_kinds
27190 get_specified_cursor_type (Lisp_Object arg, int *width)
27192 enum text_cursor_kinds type;
27194 if (NILP (arg))
27195 return NO_CURSOR;
27197 if (EQ (arg, Qbox))
27198 return FILLED_BOX_CURSOR;
27200 if (EQ (arg, Qhollow))
27201 return HOLLOW_BOX_CURSOR;
27203 if (EQ (arg, Qbar))
27205 *width = 2;
27206 return BAR_CURSOR;
27209 if (CONSP (arg)
27210 && EQ (XCAR (arg), Qbar)
27211 && RANGED_INTEGERP (0, XCDR (arg), INT_MAX))
27213 *width = XINT (XCDR (arg));
27214 return BAR_CURSOR;
27217 if (EQ (arg, Qhbar))
27219 *width = 2;
27220 return HBAR_CURSOR;
27223 if (CONSP (arg)
27224 && EQ (XCAR (arg), Qhbar)
27225 && RANGED_INTEGERP (0, XCDR (arg), INT_MAX))
27227 *width = XINT (XCDR (arg));
27228 return HBAR_CURSOR;
27231 /* Treat anything unknown as "hollow box cursor".
27232 It was bad to signal an error; people have trouble fixing
27233 .Xdefaults with Emacs, when it has something bad in it. */
27234 type = HOLLOW_BOX_CURSOR;
27236 return type;
27239 /* Set the default cursor types for specified frame. */
27240 void
27241 set_frame_cursor_types (struct frame *f, Lisp_Object arg)
27243 int width = 1;
27244 Lisp_Object tem;
27246 FRAME_DESIRED_CURSOR (f) = get_specified_cursor_type (arg, &width);
27247 FRAME_CURSOR_WIDTH (f) = width;
27249 /* By default, set up the blink-off state depending on the on-state. */
27251 tem = Fassoc (arg, Vblink_cursor_alist);
27252 if (!NILP (tem))
27254 FRAME_BLINK_OFF_CURSOR (f)
27255 = get_specified_cursor_type (XCDR (tem), &width);
27256 FRAME_BLINK_OFF_CURSOR_WIDTH (f) = width;
27258 else
27259 FRAME_BLINK_OFF_CURSOR (f) = DEFAULT_CURSOR;
27261 /* Make sure the cursor gets redrawn. */
27262 f->cursor_type_changed = 1;
27266 #ifdef HAVE_WINDOW_SYSTEM
27268 /* Return the cursor we want to be displayed in window W. Return
27269 width of bar/hbar cursor through WIDTH arg. Return with
27270 ACTIVE_CURSOR arg set to 1 if cursor in window W is `active'
27271 (i.e. if the `system caret' should track this cursor).
27273 In a mini-buffer window, we want the cursor only to appear if we
27274 are reading input from this window. For the selected window, we
27275 want the cursor type given by the frame parameter or buffer local
27276 setting of cursor-type. If explicitly marked off, draw no cursor.
27277 In all other cases, we want a hollow box cursor. */
27279 static enum text_cursor_kinds
27280 get_window_cursor_type (struct window *w, struct glyph *glyph, int *width,
27281 int *active_cursor)
27283 struct frame *f = XFRAME (w->frame);
27284 struct buffer *b = XBUFFER (w->contents);
27285 int cursor_type = DEFAULT_CURSOR;
27286 Lisp_Object alt_cursor;
27287 int non_selected = 0;
27289 *active_cursor = 1;
27291 /* Echo area */
27292 if (cursor_in_echo_area
27293 && FRAME_HAS_MINIBUF_P (f)
27294 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
27296 if (w == XWINDOW (echo_area_window))
27298 if (EQ (BVAR (b, cursor_type), Qt) || NILP (BVAR (b, cursor_type)))
27300 *width = FRAME_CURSOR_WIDTH (f);
27301 return FRAME_DESIRED_CURSOR (f);
27303 else
27304 return get_specified_cursor_type (BVAR (b, cursor_type), width);
27307 *active_cursor = 0;
27308 non_selected = 1;
27311 /* Detect a nonselected window or nonselected frame. */
27312 else if (w != XWINDOW (f->selected_window)
27313 || f != FRAME_DISPLAY_INFO (f)->x_highlight_frame)
27315 *active_cursor = 0;
27317 if (MINI_WINDOW_P (w) && minibuf_level == 0)
27318 return NO_CURSOR;
27320 non_selected = 1;
27323 /* Never display a cursor in a window in which cursor-type is nil. */
27324 if (NILP (BVAR (b, cursor_type)))
27325 return NO_CURSOR;
27327 /* Get the normal cursor type for this window. */
27328 if (EQ (BVAR (b, cursor_type), Qt))
27330 cursor_type = FRAME_DESIRED_CURSOR (f);
27331 *width = FRAME_CURSOR_WIDTH (f);
27333 else
27334 cursor_type = get_specified_cursor_type (BVAR (b, cursor_type), width);
27336 /* Use cursor-in-non-selected-windows instead
27337 for non-selected window or frame. */
27338 if (non_selected)
27340 alt_cursor = BVAR (b, cursor_in_non_selected_windows);
27341 if (!EQ (Qt, alt_cursor))
27342 return get_specified_cursor_type (alt_cursor, width);
27343 /* t means modify the normal cursor type. */
27344 if (cursor_type == FILLED_BOX_CURSOR)
27345 cursor_type = HOLLOW_BOX_CURSOR;
27346 else if (cursor_type == BAR_CURSOR && *width > 1)
27347 --*width;
27348 return cursor_type;
27351 /* Use normal cursor if not blinked off. */
27352 if (!w->cursor_off_p)
27354 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
27356 if (cursor_type == FILLED_BOX_CURSOR)
27358 /* Using a block cursor on large images can be very annoying.
27359 So use a hollow cursor for "large" images.
27360 If image is not transparent (no mask), also use hollow cursor. */
27361 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
27362 if (img != NULL && IMAGEP (img->spec))
27364 /* Arbitrarily, interpret "Large" as >32x32 and >NxN
27365 where N = size of default frame font size.
27366 This should cover most of the "tiny" icons people may use. */
27367 if (!img->mask
27368 || img->width > max (32, WINDOW_FRAME_COLUMN_WIDTH (w))
27369 || img->height > max (32, WINDOW_FRAME_LINE_HEIGHT (w)))
27370 cursor_type = HOLLOW_BOX_CURSOR;
27373 else if (cursor_type != NO_CURSOR)
27375 /* Display current only supports BOX and HOLLOW cursors for images.
27376 So for now, unconditionally use a HOLLOW cursor when cursor is
27377 not a solid box cursor. */
27378 cursor_type = HOLLOW_BOX_CURSOR;
27381 return cursor_type;
27384 /* Cursor is blinked off, so determine how to "toggle" it. */
27386 /* First look for an entry matching the buffer's cursor-type in blink-cursor-alist. */
27387 if ((alt_cursor = Fassoc (BVAR (b, cursor_type), Vblink_cursor_alist), !NILP (alt_cursor)))
27388 return get_specified_cursor_type (XCDR (alt_cursor), width);
27390 /* Then see if frame has specified a specific blink off cursor type. */
27391 if (FRAME_BLINK_OFF_CURSOR (f) != DEFAULT_CURSOR)
27393 *width = FRAME_BLINK_OFF_CURSOR_WIDTH (f);
27394 return FRAME_BLINK_OFF_CURSOR (f);
27397 #if 0
27398 /* Some people liked having a permanently visible blinking cursor,
27399 while others had very strong opinions against it. So it was
27400 decided to remove it. KFS 2003-09-03 */
27402 /* Finally perform built-in cursor blinking:
27403 filled box <-> hollow box
27404 wide [h]bar <-> narrow [h]bar
27405 narrow [h]bar <-> no cursor
27406 other type <-> no cursor */
27408 if (cursor_type == FILLED_BOX_CURSOR)
27409 return HOLLOW_BOX_CURSOR;
27411 if ((cursor_type == BAR_CURSOR || cursor_type == HBAR_CURSOR) && *width > 1)
27413 *width = 1;
27414 return cursor_type;
27416 #endif
27418 return NO_CURSOR;
27422 /* Notice when the text cursor of window W has been completely
27423 overwritten by a drawing operation that outputs glyphs in AREA
27424 starting at X0 and ending at X1 in the line starting at Y0 and
27425 ending at Y1. X coordinates are area-relative. X1 < 0 means all
27426 the rest of the line after X0 has been written. Y coordinates
27427 are window-relative. */
27429 static void
27430 notice_overwritten_cursor (struct window *w, enum glyph_row_area area,
27431 int x0, int x1, int y0, int y1)
27433 int cx0, cx1, cy0, cy1;
27434 struct glyph_row *row;
27436 if (!w->phys_cursor_on_p)
27437 return;
27438 if (area != TEXT_AREA)
27439 return;
27441 if (w->phys_cursor.vpos < 0
27442 || w->phys_cursor.vpos >= w->current_matrix->nrows
27443 || (row = w->current_matrix->rows + w->phys_cursor.vpos,
27444 !(row->enabled_p && MATRIX_ROW_DISPLAYS_TEXT_P (row))))
27445 return;
27447 if (row->cursor_in_fringe_p)
27449 row->cursor_in_fringe_p = 0;
27450 draw_fringe_bitmap (w, row, row->reversed_p);
27451 w->phys_cursor_on_p = 0;
27452 return;
27455 cx0 = w->phys_cursor.x;
27456 cx1 = cx0 + w->phys_cursor_width;
27457 if (x0 > cx0 || (x1 >= 0 && x1 < cx1))
27458 return;
27460 /* The cursor image will be completely removed from the
27461 screen if the output area intersects the cursor area in
27462 y-direction. When we draw in [y0 y1[, and some part of
27463 the cursor is at y < y0, that part must have been drawn
27464 before. When scrolling, the cursor is erased before
27465 actually scrolling, so we don't come here. When not
27466 scrolling, the rows above the old cursor row must have
27467 changed, and in this case these rows must have written
27468 over the cursor image.
27470 Likewise if part of the cursor is below y1, with the
27471 exception of the cursor being in the first blank row at
27472 the buffer and window end because update_text_area
27473 doesn't draw that row. (Except when it does, but
27474 that's handled in update_text_area.) */
27476 cy0 = w->phys_cursor.y;
27477 cy1 = cy0 + w->phys_cursor_height;
27478 if ((y0 < cy0 || y0 >= cy1) && (y1 <= cy0 || y1 >= cy1))
27479 return;
27481 w->phys_cursor_on_p = 0;
27484 #endif /* HAVE_WINDOW_SYSTEM */
27487 /************************************************************************
27488 Mouse Face
27489 ************************************************************************/
27491 #ifdef HAVE_WINDOW_SYSTEM
27493 /* EXPORT for RIF:
27494 Fix the display of area AREA of overlapping row ROW in window W
27495 with respect to the overlapping part OVERLAPS. */
27497 void
27498 x_fix_overlapping_area (struct window *w, struct glyph_row *row,
27499 enum glyph_row_area area, int overlaps)
27501 int i, x;
27503 block_input ();
27505 x = 0;
27506 for (i = 0; i < row->used[area];)
27508 if (row->glyphs[area][i].overlaps_vertically_p)
27510 int start = i, start_x = x;
27514 x += row->glyphs[area][i].pixel_width;
27515 ++i;
27517 while (i < row->used[area]
27518 && row->glyphs[area][i].overlaps_vertically_p);
27520 draw_glyphs (w, start_x, row, area,
27521 start, i,
27522 DRAW_NORMAL_TEXT, overlaps);
27524 else
27526 x += row->glyphs[area][i].pixel_width;
27527 ++i;
27531 unblock_input ();
27535 /* EXPORT:
27536 Draw the cursor glyph of window W in glyph row ROW. See the
27537 comment of draw_glyphs for the meaning of HL. */
27539 void
27540 draw_phys_cursor_glyph (struct window *w, struct glyph_row *row,
27541 enum draw_glyphs_face hl)
27543 /* If cursor hpos is out of bounds, don't draw garbage. This can
27544 happen in mini-buffer windows when switching between echo area
27545 glyphs and mini-buffer. */
27546 if ((row->reversed_p
27547 ? (w->phys_cursor.hpos >= 0)
27548 : (w->phys_cursor.hpos < row->used[TEXT_AREA])))
27550 int on_p = w->phys_cursor_on_p;
27551 int x1;
27552 int hpos = w->phys_cursor.hpos;
27554 /* When the window is hscrolled, cursor hpos can legitimately be
27555 out of bounds, but we draw the cursor at the corresponding
27556 window margin in that case. */
27557 if (!row->reversed_p && hpos < 0)
27558 hpos = 0;
27559 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
27560 hpos = row->used[TEXT_AREA] - 1;
27562 x1 = draw_glyphs (w, w->phys_cursor.x, row, TEXT_AREA, hpos, hpos + 1,
27563 hl, 0);
27564 w->phys_cursor_on_p = on_p;
27566 if (hl == DRAW_CURSOR)
27567 w->phys_cursor_width = x1 - w->phys_cursor.x;
27568 /* When we erase the cursor, and ROW is overlapped by other
27569 rows, make sure that these overlapping parts of other rows
27570 are redrawn. */
27571 else if (hl == DRAW_NORMAL_TEXT && row->overlapped_p)
27573 w->phys_cursor_width = x1 - w->phys_cursor.x;
27575 if (row > w->current_matrix->rows
27576 && MATRIX_ROW_OVERLAPS_SUCC_P (row - 1))
27577 x_fix_overlapping_area (w, row - 1, TEXT_AREA,
27578 OVERLAPS_ERASED_CURSOR);
27580 if (MATRIX_ROW_BOTTOM_Y (row) < window_text_bottom_y (w)
27581 && MATRIX_ROW_OVERLAPS_PRED_P (row + 1))
27582 x_fix_overlapping_area (w, row + 1, TEXT_AREA,
27583 OVERLAPS_ERASED_CURSOR);
27589 /* Erase the image of a cursor of window W from the screen. */
27591 void
27592 erase_phys_cursor (struct window *w)
27594 struct frame *f = XFRAME (w->frame);
27595 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
27596 int hpos = w->phys_cursor.hpos;
27597 int vpos = w->phys_cursor.vpos;
27598 int mouse_face_here_p = 0;
27599 struct glyph_matrix *active_glyphs = w->current_matrix;
27600 struct glyph_row *cursor_row;
27601 struct glyph *cursor_glyph;
27602 enum draw_glyphs_face hl;
27604 /* No cursor displayed or row invalidated => nothing to do on the
27605 screen. */
27606 if (w->phys_cursor_type == NO_CURSOR)
27607 goto mark_cursor_off;
27609 /* VPOS >= active_glyphs->nrows means that window has been resized.
27610 Don't bother to erase the cursor. */
27611 if (vpos >= active_glyphs->nrows)
27612 goto mark_cursor_off;
27614 /* If row containing cursor is marked invalid, there is nothing we
27615 can do. */
27616 cursor_row = MATRIX_ROW (active_glyphs, vpos);
27617 if (!cursor_row->enabled_p)
27618 goto mark_cursor_off;
27620 /* If line spacing is > 0, old cursor may only be partially visible in
27621 window after split-window. So adjust visible height. */
27622 cursor_row->visible_height = min (cursor_row->visible_height,
27623 window_text_bottom_y (w) - cursor_row->y);
27625 /* If row is completely invisible, don't attempt to delete a cursor which
27626 isn't there. This can happen if cursor is at top of a window, and
27627 we switch to a buffer with a header line in that window. */
27628 if (cursor_row->visible_height <= 0)
27629 goto mark_cursor_off;
27631 /* If cursor is in the fringe, erase by drawing actual bitmap there. */
27632 if (cursor_row->cursor_in_fringe_p)
27634 cursor_row->cursor_in_fringe_p = 0;
27635 draw_fringe_bitmap (w, cursor_row, cursor_row->reversed_p);
27636 goto mark_cursor_off;
27639 /* This can happen when the new row is shorter than the old one.
27640 In this case, either draw_glyphs or clear_end_of_line
27641 should have cleared the cursor. Note that we wouldn't be
27642 able to erase the cursor in this case because we don't have a
27643 cursor glyph at hand. */
27644 if ((cursor_row->reversed_p
27645 ? (w->phys_cursor.hpos < 0)
27646 : (w->phys_cursor.hpos >= cursor_row->used[TEXT_AREA])))
27647 goto mark_cursor_off;
27649 /* When the window is hscrolled, cursor hpos can legitimately be out
27650 of bounds, but we draw the cursor at the corresponding window
27651 margin in that case. */
27652 if (!cursor_row->reversed_p && hpos < 0)
27653 hpos = 0;
27654 if (cursor_row->reversed_p && hpos >= cursor_row->used[TEXT_AREA])
27655 hpos = cursor_row->used[TEXT_AREA] - 1;
27657 /* If the cursor is in the mouse face area, redisplay that when
27658 we clear the cursor. */
27659 if (! NILP (hlinfo->mouse_face_window)
27660 && coords_in_mouse_face_p (w, hpos, vpos)
27661 /* Don't redraw the cursor's spot in mouse face if it is at the
27662 end of a line (on a newline). The cursor appears there, but
27663 mouse highlighting does not. */
27664 && cursor_row->used[TEXT_AREA] > hpos && hpos >= 0)
27665 mouse_face_here_p = 1;
27667 /* Maybe clear the display under the cursor. */
27668 if (w->phys_cursor_type == HOLLOW_BOX_CURSOR)
27670 int x, y;
27671 int header_line_height = WINDOW_HEADER_LINE_HEIGHT (w);
27672 int width;
27674 cursor_glyph = get_phys_cursor_glyph (w);
27675 if (cursor_glyph == NULL)
27676 goto mark_cursor_off;
27678 width = cursor_glyph->pixel_width;
27679 x = w->phys_cursor.x;
27680 if (x < 0)
27682 width += x;
27683 x = 0;
27685 width = min (width, window_box_width (w, TEXT_AREA) - x);
27686 y = WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height, cursor_row->y));
27687 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, x);
27689 if (width > 0)
27690 FRAME_RIF (f)->clear_frame_area (f, x, y, width, cursor_row->visible_height);
27693 /* Erase the cursor by redrawing the character underneath it. */
27694 if (mouse_face_here_p)
27695 hl = DRAW_MOUSE_FACE;
27696 else
27697 hl = DRAW_NORMAL_TEXT;
27698 draw_phys_cursor_glyph (w, cursor_row, hl);
27700 mark_cursor_off:
27701 w->phys_cursor_on_p = 0;
27702 w->phys_cursor_type = NO_CURSOR;
27706 /* EXPORT:
27707 Display or clear cursor of window W. If ON is zero, clear the
27708 cursor. If it is non-zero, display the cursor. If ON is nonzero,
27709 where to put the cursor is specified by HPOS, VPOS, X and Y. */
27711 void
27712 display_and_set_cursor (struct window *w, bool on,
27713 int hpos, int vpos, int x, int y)
27715 struct frame *f = XFRAME (w->frame);
27716 int new_cursor_type;
27717 int new_cursor_width;
27718 int active_cursor;
27719 struct glyph_row *glyph_row;
27720 struct glyph *glyph;
27722 /* This is pointless on invisible frames, and dangerous on garbaged
27723 windows and frames; in the latter case, the frame or window may
27724 be in the midst of changing its size, and x and y may be off the
27725 window. */
27726 if (! FRAME_VISIBLE_P (f)
27727 || FRAME_GARBAGED_P (f)
27728 || vpos >= w->current_matrix->nrows
27729 || hpos >= w->current_matrix->matrix_w)
27730 return;
27732 /* If cursor is off and we want it off, return quickly. */
27733 if (!on && !w->phys_cursor_on_p)
27734 return;
27736 glyph_row = MATRIX_ROW (w->current_matrix, vpos);
27737 /* If cursor row is not enabled, we don't really know where to
27738 display the cursor. */
27739 if (!glyph_row->enabled_p)
27741 w->phys_cursor_on_p = 0;
27742 return;
27745 glyph = NULL;
27746 if (!glyph_row->exact_window_width_line_p
27747 || (0 <= hpos && hpos < glyph_row->used[TEXT_AREA]))
27748 glyph = glyph_row->glyphs[TEXT_AREA] + hpos;
27750 eassert (input_blocked_p ());
27752 /* Set new_cursor_type to the cursor we want to be displayed. */
27753 new_cursor_type = get_window_cursor_type (w, glyph,
27754 &new_cursor_width, &active_cursor);
27756 /* If cursor is currently being shown and we don't want it to be or
27757 it is in the wrong place, or the cursor type is not what we want,
27758 erase it. */
27759 if (w->phys_cursor_on_p
27760 && (!on
27761 || w->phys_cursor.x != x
27762 || w->phys_cursor.y != y
27763 /* HPOS can be negative in R2L rows whose
27764 exact_window_width_line_p flag is set (i.e. their newline
27765 would "overflow into the fringe"). */
27766 || hpos < 0
27767 || new_cursor_type != w->phys_cursor_type
27768 || ((new_cursor_type == BAR_CURSOR || new_cursor_type == HBAR_CURSOR)
27769 && new_cursor_width != w->phys_cursor_width)))
27770 erase_phys_cursor (w);
27772 /* Don't check phys_cursor_on_p here because that flag is only set
27773 to zero in some cases where we know that the cursor has been
27774 completely erased, to avoid the extra work of erasing the cursor
27775 twice. In other words, phys_cursor_on_p can be 1 and the cursor
27776 still not be visible, or it has only been partly erased. */
27777 if (on)
27779 w->phys_cursor_ascent = glyph_row->ascent;
27780 w->phys_cursor_height = glyph_row->height;
27782 /* Set phys_cursor_.* before x_draw_.* is called because some
27783 of them may need the information. */
27784 w->phys_cursor.x = x;
27785 w->phys_cursor.y = glyph_row->y;
27786 w->phys_cursor.hpos = hpos;
27787 w->phys_cursor.vpos = vpos;
27790 FRAME_RIF (f)->draw_window_cursor (w, glyph_row, x, y,
27791 new_cursor_type, new_cursor_width,
27792 on, active_cursor);
27796 /* Switch the display of W's cursor on or off, according to the value
27797 of ON. */
27799 static void
27800 update_window_cursor (struct window *w, bool on)
27802 /* Don't update cursor in windows whose frame is in the process
27803 of being deleted. */
27804 if (w->current_matrix)
27806 int hpos = w->phys_cursor.hpos;
27807 int vpos = w->phys_cursor.vpos;
27808 struct glyph_row *row;
27810 if (vpos >= w->current_matrix->nrows
27811 || hpos >= w->current_matrix->matrix_w)
27812 return;
27814 row = MATRIX_ROW (w->current_matrix, vpos);
27816 /* When the window is hscrolled, cursor hpos can legitimately be
27817 out of bounds, but we draw the cursor at the corresponding
27818 window margin in that case. */
27819 if (!row->reversed_p && hpos < 0)
27820 hpos = 0;
27821 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
27822 hpos = row->used[TEXT_AREA] - 1;
27824 block_input ();
27825 display_and_set_cursor (w, on, hpos, vpos,
27826 w->phys_cursor.x, w->phys_cursor.y);
27827 unblock_input ();
27832 /* Call update_window_cursor with parameter ON_P on all leaf windows
27833 in the window tree rooted at W. */
27835 static void
27836 update_cursor_in_window_tree (struct window *w, bool on_p)
27838 while (w)
27840 if (WINDOWP (w->contents))
27841 update_cursor_in_window_tree (XWINDOW (w->contents), on_p);
27842 else
27843 update_window_cursor (w, on_p);
27845 w = NILP (w->next) ? 0 : XWINDOW (w->next);
27850 /* EXPORT:
27851 Display the cursor on window W, or clear it, according to ON_P.
27852 Don't change the cursor's position. */
27854 void
27855 x_update_cursor (struct frame *f, bool on_p)
27857 update_cursor_in_window_tree (XWINDOW (f->root_window), on_p);
27861 /* EXPORT:
27862 Clear the cursor of window W to background color, and mark the
27863 cursor as not shown. This is used when the text where the cursor
27864 is about to be rewritten. */
27866 void
27867 x_clear_cursor (struct window *w)
27869 if (FRAME_VISIBLE_P (XFRAME (w->frame)) && w->phys_cursor_on_p)
27870 update_window_cursor (w, 0);
27873 #endif /* HAVE_WINDOW_SYSTEM */
27875 /* Implementation of draw_row_with_mouse_face for GUI sessions, GPM,
27876 and MSDOS. */
27877 static void
27878 draw_row_with_mouse_face (struct window *w, int start_x, struct glyph_row *row,
27879 int start_hpos, int end_hpos,
27880 enum draw_glyphs_face draw)
27882 #ifdef HAVE_WINDOW_SYSTEM
27883 if (FRAME_WINDOW_P (XFRAME (w->frame)))
27885 draw_glyphs (w, start_x, row, TEXT_AREA, start_hpos, end_hpos, draw, 0);
27886 return;
27888 #endif
27889 #if defined (HAVE_GPM) || defined (MSDOS) || defined (WINDOWSNT)
27890 tty_draw_row_with_mouse_face (w, row, start_hpos, end_hpos, draw);
27891 #endif
27894 /* Display the active region described by mouse_face_* according to DRAW. */
27896 static void
27897 show_mouse_face (Mouse_HLInfo *hlinfo, enum draw_glyphs_face draw)
27899 struct window *w = XWINDOW (hlinfo->mouse_face_window);
27900 struct frame *f = XFRAME (WINDOW_FRAME (w));
27902 if (/* If window is in the process of being destroyed, don't bother
27903 to do anything. */
27904 w->current_matrix != NULL
27905 /* Don't update mouse highlight if hidden. */
27906 && (draw != DRAW_MOUSE_FACE || !hlinfo->mouse_face_hidden)
27907 /* Recognize when we are called to operate on rows that don't exist
27908 anymore. This can happen when a window is split. */
27909 && hlinfo->mouse_face_end_row < w->current_matrix->nrows)
27911 int phys_cursor_on_p = w->phys_cursor_on_p;
27912 struct glyph_row *row, *first, *last;
27914 first = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_beg_row);
27915 last = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_end_row);
27917 for (row = first; row <= last && row->enabled_p; ++row)
27919 int start_hpos, end_hpos, start_x;
27921 /* For all but the first row, the highlight starts at column 0. */
27922 if (row == first)
27924 /* R2L rows have BEG and END in reversed order, but the
27925 screen drawing geometry is always left to right. So
27926 we need to mirror the beginning and end of the
27927 highlighted area in R2L rows. */
27928 if (!row->reversed_p)
27930 start_hpos = hlinfo->mouse_face_beg_col;
27931 start_x = hlinfo->mouse_face_beg_x;
27933 else if (row == last)
27935 start_hpos = hlinfo->mouse_face_end_col;
27936 start_x = hlinfo->mouse_face_end_x;
27938 else
27940 start_hpos = 0;
27941 start_x = 0;
27944 else if (row->reversed_p && row == last)
27946 start_hpos = hlinfo->mouse_face_end_col;
27947 start_x = hlinfo->mouse_face_end_x;
27949 else
27951 start_hpos = 0;
27952 start_x = 0;
27955 if (row == last)
27957 if (!row->reversed_p)
27958 end_hpos = hlinfo->mouse_face_end_col;
27959 else if (row == first)
27960 end_hpos = hlinfo->mouse_face_beg_col;
27961 else
27963 end_hpos = row->used[TEXT_AREA];
27964 if (draw == DRAW_NORMAL_TEXT)
27965 row->fill_line_p = 1; /* Clear to end of line */
27968 else if (row->reversed_p && row == first)
27969 end_hpos = hlinfo->mouse_face_beg_col;
27970 else
27972 end_hpos = row->used[TEXT_AREA];
27973 if (draw == DRAW_NORMAL_TEXT)
27974 row->fill_line_p = 1; /* Clear to end of line */
27977 if (end_hpos > start_hpos)
27979 draw_row_with_mouse_face (w, start_x, row,
27980 start_hpos, end_hpos, draw);
27982 row->mouse_face_p
27983 = draw == DRAW_MOUSE_FACE || draw == DRAW_IMAGE_RAISED;
27987 #ifdef HAVE_WINDOW_SYSTEM
27988 /* When we've written over the cursor, arrange for it to
27989 be displayed again. */
27990 if (FRAME_WINDOW_P (f)
27991 && phys_cursor_on_p && !w->phys_cursor_on_p)
27993 int hpos = w->phys_cursor.hpos;
27995 /* When the window is hscrolled, cursor hpos can legitimately be
27996 out of bounds, but we draw the cursor at the corresponding
27997 window margin in that case. */
27998 if (!row->reversed_p && hpos < 0)
27999 hpos = 0;
28000 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
28001 hpos = row->used[TEXT_AREA] - 1;
28003 block_input ();
28004 display_and_set_cursor (w, 1, hpos, w->phys_cursor.vpos,
28005 w->phys_cursor.x, w->phys_cursor.y);
28006 unblock_input ();
28008 #endif /* HAVE_WINDOW_SYSTEM */
28011 #ifdef HAVE_WINDOW_SYSTEM
28012 /* Change the mouse cursor. */
28013 if (FRAME_WINDOW_P (f) && NILP (do_mouse_tracking))
28015 #if ! defined (USE_GTK) && ! defined (HAVE_NS)
28016 if (draw == DRAW_NORMAL_TEXT
28017 && !EQ (hlinfo->mouse_face_window, f->tool_bar_window))
28018 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->text_cursor);
28019 else
28020 #endif
28021 if (draw == DRAW_MOUSE_FACE)
28022 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->hand_cursor);
28023 else
28024 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->nontext_cursor);
28026 #endif /* HAVE_WINDOW_SYSTEM */
28029 /* EXPORT:
28030 Clear out the mouse-highlighted active region.
28031 Redraw it un-highlighted first. Value is non-zero if mouse
28032 face was actually drawn unhighlighted. */
28035 clear_mouse_face (Mouse_HLInfo *hlinfo)
28037 int cleared = 0;
28039 if (!hlinfo->mouse_face_hidden && !NILP (hlinfo->mouse_face_window))
28041 show_mouse_face (hlinfo, DRAW_NORMAL_TEXT);
28042 cleared = 1;
28045 hlinfo->mouse_face_beg_row = hlinfo->mouse_face_beg_col = -1;
28046 hlinfo->mouse_face_end_row = hlinfo->mouse_face_end_col = -1;
28047 hlinfo->mouse_face_window = Qnil;
28048 hlinfo->mouse_face_overlay = Qnil;
28049 return cleared;
28052 /* Return true if the coordinates HPOS and VPOS on windows W are
28053 within the mouse face on that window. */
28054 static bool
28055 coords_in_mouse_face_p (struct window *w, int hpos, int vpos)
28057 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (XFRAME (w->frame));
28059 /* Quickly resolve the easy cases. */
28060 if (!(WINDOWP (hlinfo->mouse_face_window)
28061 && XWINDOW (hlinfo->mouse_face_window) == w))
28062 return false;
28063 if (vpos < hlinfo->mouse_face_beg_row
28064 || vpos > hlinfo->mouse_face_end_row)
28065 return false;
28066 if (vpos > hlinfo->mouse_face_beg_row
28067 && vpos < hlinfo->mouse_face_end_row)
28068 return true;
28070 if (!MATRIX_ROW (w->current_matrix, vpos)->reversed_p)
28072 if (hlinfo->mouse_face_beg_row == hlinfo->mouse_face_end_row)
28074 if (hlinfo->mouse_face_beg_col <= hpos && hpos < hlinfo->mouse_face_end_col)
28075 return true;
28077 else if ((vpos == hlinfo->mouse_face_beg_row
28078 && hpos >= hlinfo->mouse_face_beg_col)
28079 || (vpos == hlinfo->mouse_face_end_row
28080 && hpos < hlinfo->mouse_face_end_col))
28081 return true;
28083 else
28085 if (hlinfo->mouse_face_beg_row == hlinfo->mouse_face_end_row)
28087 if (hlinfo->mouse_face_end_col < hpos && hpos <= hlinfo->mouse_face_beg_col)
28088 return true;
28090 else if ((vpos == hlinfo->mouse_face_beg_row
28091 && hpos <= hlinfo->mouse_face_beg_col)
28092 || (vpos == hlinfo->mouse_face_end_row
28093 && hpos > hlinfo->mouse_face_end_col))
28094 return true;
28096 return false;
28100 /* EXPORT:
28101 True if physical cursor of window W is within mouse face. */
28103 bool
28104 cursor_in_mouse_face_p (struct window *w)
28106 int hpos = w->phys_cursor.hpos;
28107 int vpos = w->phys_cursor.vpos;
28108 struct glyph_row *row = MATRIX_ROW (w->current_matrix, vpos);
28110 /* When the window is hscrolled, cursor hpos can legitimately be out
28111 of bounds, but we draw the cursor at the corresponding window
28112 margin in that case. */
28113 if (!row->reversed_p && hpos < 0)
28114 hpos = 0;
28115 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
28116 hpos = row->used[TEXT_AREA] - 1;
28118 return coords_in_mouse_face_p (w, hpos, vpos);
28123 /* Find the glyph rows START_ROW and END_ROW of window W that display
28124 characters between buffer positions START_CHARPOS and END_CHARPOS
28125 (excluding END_CHARPOS). DISP_STRING is a display string that
28126 covers these buffer positions. This is similar to
28127 row_containing_pos, but is more accurate when bidi reordering makes
28128 buffer positions change non-linearly with glyph rows. */
28129 static void
28130 rows_from_pos_range (struct window *w,
28131 ptrdiff_t start_charpos, ptrdiff_t end_charpos,
28132 Lisp_Object disp_string,
28133 struct glyph_row **start, struct glyph_row **end)
28135 struct glyph_row *first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
28136 int last_y = window_text_bottom_y (w);
28137 struct glyph_row *row;
28139 *start = NULL;
28140 *end = NULL;
28142 while (!first->enabled_p
28143 && first < MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w))
28144 first++;
28146 /* Find the START row. */
28147 for (row = first;
28148 row->enabled_p && MATRIX_ROW_BOTTOM_Y (row) <= last_y;
28149 row++)
28151 /* A row can potentially be the START row if the range of the
28152 characters it displays intersects the range
28153 [START_CHARPOS..END_CHARPOS). */
28154 if (! ((start_charpos < MATRIX_ROW_START_CHARPOS (row)
28155 && end_charpos < MATRIX_ROW_START_CHARPOS (row))
28156 /* See the commentary in row_containing_pos, for the
28157 explanation of the complicated way to check whether
28158 some position is beyond the end of the characters
28159 displayed by a row. */
28160 || ((start_charpos > MATRIX_ROW_END_CHARPOS (row)
28161 || (start_charpos == MATRIX_ROW_END_CHARPOS (row)
28162 && !row->ends_at_zv_p
28163 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
28164 && (end_charpos > MATRIX_ROW_END_CHARPOS (row)
28165 || (end_charpos == MATRIX_ROW_END_CHARPOS (row)
28166 && !row->ends_at_zv_p
28167 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))))))
28169 /* Found a candidate row. Now make sure at least one of the
28170 glyphs it displays has a charpos from the range
28171 [START_CHARPOS..END_CHARPOS).
28173 This is not obvious because bidi reordering could make
28174 buffer positions of a row be 1,2,3,102,101,100, and if we
28175 want to highlight characters in [50..60), we don't want
28176 this row, even though [50..60) does intersect [1..103),
28177 the range of character positions given by the row's start
28178 and end positions. */
28179 struct glyph *g = row->glyphs[TEXT_AREA];
28180 struct glyph *e = g + row->used[TEXT_AREA];
28182 while (g < e)
28184 if (((BUFFERP (g->object) || NILP (g->object))
28185 && start_charpos <= g->charpos && g->charpos < end_charpos)
28186 /* A glyph that comes from DISP_STRING is by
28187 definition to be highlighted. */
28188 || EQ (g->object, disp_string))
28189 *start = row;
28190 g++;
28192 if (*start)
28193 break;
28197 /* Find the END row. */
28198 if (!*start
28199 /* If the last row is partially visible, start looking for END
28200 from that row, instead of starting from FIRST. */
28201 && !(row->enabled_p
28202 && row->y < last_y && MATRIX_ROW_BOTTOM_Y (row) > last_y))
28203 row = first;
28204 for ( ; row->enabled_p && MATRIX_ROW_BOTTOM_Y (row) <= last_y; row++)
28206 struct glyph_row *next = row + 1;
28207 ptrdiff_t next_start = MATRIX_ROW_START_CHARPOS (next);
28209 if (!next->enabled_p
28210 || next >= MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w)
28211 /* The first row >= START whose range of displayed characters
28212 does NOT intersect the range [START_CHARPOS..END_CHARPOS]
28213 is the row END + 1. */
28214 || (start_charpos < next_start
28215 && end_charpos < next_start)
28216 || ((start_charpos > MATRIX_ROW_END_CHARPOS (next)
28217 || (start_charpos == MATRIX_ROW_END_CHARPOS (next)
28218 && !next->ends_at_zv_p
28219 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (next)))
28220 && (end_charpos > MATRIX_ROW_END_CHARPOS (next)
28221 || (end_charpos == MATRIX_ROW_END_CHARPOS (next)
28222 && !next->ends_at_zv_p
28223 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (next)))))
28225 *end = row;
28226 break;
28228 else
28230 /* If the next row's edges intersect [START_CHARPOS..END_CHARPOS],
28231 but none of the characters it displays are in the range, it is
28232 also END + 1. */
28233 struct glyph *g = next->glyphs[TEXT_AREA];
28234 struct glyph *s = g;
28235 struct glyph *e = g + next->used[TEXT_AREA];
28237 while (g < e)
28239 if (((BUFFERP (g->object) || NILP (g->object))
28240 && ((start_charpos <= g->charpos && g->charpos < end_charpos)
28241 /* If the buffer position of the first glyph in
28242 the row is equal to END_CHARPOS, it means
28243 the last character to be highlighted is the
28244 newline of ROW, and we must consider NEXT as
28245 END, not END+1. */
28246 || (((!next->reversed_p && g == s)
28247 || (next->reversed_p && g == e - 1))
28248 && (g->charpos == end_charpos
28249 /* Special case for when NEXT is an
28250 empty line at ZV. */
28251 || (g->charpos == -1
28252 && !row->ends_at_zv_p
28253 && next_start == end_charpos)))))
28254 /* A glyph that comes from DISP_STRING is by
28255 definition to be highlighted. */
28256 || EQ (g->object, disp_string))
28257 break;
28258 g++;
28260 if (g == e)
28262 *end = row;
28263 break;
28265 /* The first row that ends at ZV must be the last to be
28266 highlighted. */
28267 else if (next->ends_at_zv_p)
28269 *end = next;
28270 break;
28276 /* This function sets the mouse_face_* elements of HLINFO, assuming
28277 the mouse cursor is on a glyph with buffer charpos MOUSE_CHARPOS in
28278 window WINDOW. START_CHARPOS and END_CHARPOS are buffer positions
28279 for the overlay or run of text properties specifying the mouse
28280 face. BEFORE_STRING and AFTER_STRING, if non-nil, are a
28281 before-string and after-string that must also be highlighted.
28282 DISP_STRING, if non-nil, is a display string that may cover some
28283 or all of the highlighted text. */
28285 static void
28286 mouse_face_from_buffer_pos (Lisp_Object window,
28287 Mouse_HLInfo *hlinfo,
28288 ptrdiff_t mouse_charpos,
28289 ptrdiff_t start_charpos,
28290 ptrdiff_t end_charpos,
28291 Lisp_Object before_string,
28292 Lisp_Object after_string,
28293 Lisp_Object disp_string)
28295 struct window *w = XWINDOW (window);
28296 struct glyph_row *first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
28297 struct glyph_row *r1, *r2;
28298 struct glyph *glyph, *end;
28299 ptrdiff_t ignore, pos;
28300 int x;
28302 eassert (NILP (disp_string) || STRINGP (disp_string));
28303 eassert (NILP (before_string) || STRINGP (before_string));
28304 eassert (NILP (after_string) || STRINGP (after_string));
28306 /* Find the rows corresponding to START_CHARPOS and END_CHARPOS. */
28307 rows_from_pos_range (w, start_charpos, end_charpos, disp_string, &r1, &r2);
28308 if (r1 == NULL)
28309 r1 = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
28310 /* If the before-string or display-string contains newlines,
28311 rows_from_pos_range skips to its last row. Move back. */
28312 if (!NILP (before_string) || !NILP (disp_string))
28314 struct glyph_row *prev;
28315 while ((prev = r1 - 1, prev >= first)
28316 && MATRIX_ROW_END_CHARPOS (prev) == start_charpos
28317 && prev->used[TEXT_AREA] > 0)
28319 struct glyph *beg = prev->glyphs[TEXT_AREA];
28320 glyph = beg + prev->used[TEXT_AREA];
28321 while (--glyph >= beg && NILP (glyph->object));
28322 if (glyph < beg
28323 || !(EQ (glyph->object, before_string)
28324 || EQ (glyph->object, disp_string)))
28325 break;
28326 r1 = prev;
28329 if (r2 == NULL)
28331 r2 = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
28332 hlinfo->mouse_face_past_end = 1;
28334 else if (!NILP (after_string))
28336 /* If the after-string has newlines, advance to its last row. */
28337 struct glyph_row *next;
28338 struct glyph_row *last
28339 = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
28341 for (next = r2 + 1;
28342 next <= last
28343 && next->used[TEXT_AREA] > 0
28344 && EQ (next->glyphs[TEXT_AREA]->object, after_string);
28345 ++next)
28346 r2 = next;
28348 /* The rest of the display engine assumes that mouse_face_beg_row is
28349 either above mouse_face_end_row or identical to it. But with
28350 bidi-reordered continued lines, the row for START_CHARPOS could
28351 be below the row for END_CHARPOS. If so, swap the rows and store
28352 them in correct order. */
28353 if (r1->y > r2->y)
28355 struct glyph_row *tem = r2;
28357 r2 = r1;
28358 r1 = tem;
28361 hlinfo->mouse_face_beg_row = MATRIX_ROW_VPOS (r1, w->current_matrix);
28362 hlinfo->mouse_face_end_row = MATRIX_ROW_VPOS (r2, w->current_matrix);
28364 /* For a bidi-reordered row, the positions of BEFORE_STRING,
28365 AFTER_STRING, DISP_STRING, START_CHARPOS, and END_CHARPOS
28366 could be anywhere in the row and in any order. The strategy
28367 below is to find the leftmost and the rightmost glyph that
28368 belongs to either of these 3 strings, or whose position is
28369 between START_CHARPOS and END_CHARPOS, and highlight all the
28370 glyphs between those two. This may cover more than just the text
28371 between START_CHARPOS and END_CHARPOS if the range of characters
28372 strides the bidi level boundary, e.g. if the beginning is in R2L
28373 text while the end is in L2R text or vice versa. */
28374 if (!r1->reversed_p)
28376 /* This row is in a left to right paragraph. Scan it left to
28377 right. */
28378 glyph = r1->glyphs[TEXT_AREA];
28379 end = glyph + r1->used[TEXT_AREA];
28380 x = r1->x;
28382 /* Skip truncation glyphs at the start of the glyph row. */
28383 if (MATRIX_ROW_DISPLAYS_TEXT_P (r1))
28384 for (; glyph < end
28385 && NILP (glyph->object)
28386 && glyph->charpos < 0;
28387 ++glyph)
28388 x += glyph->pixel_width;
28390 /* Scan the glyph row, looking for BEFORE_STRING, AFTER_STRING,
28391 or DISP_STRING, and the first glyph from buffer whose
28392 position is between START_CHARPOS and END_CHARPOS. */
28393 for (; glyph < end
28394 && !NILP (glyph->object)
28395 && !EQ (glyph->object, disp_string)
28396 && !(BUFFERP (glyph->object)
28397 && (glyph->charpos >= start_charpos
28398 && glyph->charpos < end_charpos));
28399 ++glyph)
28401 /* BEFORE_STRING or AFTER_STRING are only relevant if they
28402 are present at buffer positions between START_CHARPOS and
28403 END_CHARPOS, or if they come from an overlay. */
28404 if (EQ (glyph->object, before_string))
28406 pos = string_buffer_position (before_string,
28407 start_charpos);
28408 /* If pos == 0, it means before_string came from an
28409 overlay, not from a buffer position. */
28410 if (!pos || (pos >= start_charpos && pos < end_charpos))
28411 break;
28413 else if (EQ (glyph->object, after_string))
28415 pos = string_buffer_position (after_string, end_charpos);
28416 if (!pos || (pos >= start_charpos && pos < end_charpos))
28417 break;
28419 x += glyph->pixel_width;
28421 hlinfo->mouse_face_beg_x = x;
28422 hlinfo->mouse_face_beg_col = glyph - r1->glyphs[TEXT_AREA];
28424 else
28426 /* This row is in a right to left paragraph. Scan it right to
28427 left. */
28428 struct glyph *g;
28430 end = r1->glyphs[TEXT_AREA] - 1;
28431 glyph = end + r1->used[TEXT_AREA];
28433 /* Skip truncation glyphs at the start of the glyph row. */
28434 if (MATRIX_ROW_DISPLAYS_TEXT_P (r1))
28435 for (; glyph > end
28436 && NILP (glyph->object)
28437 && glyph->charpos < 0;
28438 --glyph)
28441 /* Scan the glyph row, looking for BEFORE_STRING, AFTER_STRING,
28442 or DISP_STRING, and the first glyph from buffer whose
28443 position is between START_CHARPOS and END_CHARPOS. */
28444 for (; glyph > end
28445 && !NILP (glyph->object)
28446 && !EQ (glyph->object, disp_string)
28447 && !(BUFFERP (glyph->object)
28448 && (glyph->charpos >= start_charpos
28449 && glyph->charpos < end_charpos));
28450 --glyph)
28452 /* BEFORE_STRING or AFTER_STRING are only relevant if they
28453 are present at buffer positions between START_CHARPOS and
28454 END_CHARPOS, or if they come from an overlay. */
28455 if (EQ (glyph->object, before_string))
28457 pos = string_buffer_position (before_string, start_charpos);
28458 /* If pos == 0, it means before_string came from an
28459 overlay, not from a buffer position. */
28460 if (!pos || (pos >= start_charpos && pos < end_charpos))
28461 break;
28463 else if (EQ (glyph->object, after_string))
28465 pos = string_buffer_position (after_string, end_charpos);
28466 if (!pos || (pos >= start_charpos && pos < end_charpos))
28467 break;
28471 glyph++; /* first glyph to the right of the highlighted area */
28472 for (g = r1->glyphs[TEXT_AREA], x = r1->x; g < glyph; g++)
28473 x += g->pixel_width;
28474 hlinfo->mouse_face_beg_x = x;
28475 hlinfo->mouse_face_beg_col = glyph - r1->glyphs[TEXT_AREA];
28478 /* If the highlight ends in a different row, compute GLYPH and END
28479 for the end row. Otherwise, reuse the values computed above for
28480 the row where the highlight begins. */
28481 if (r2 != r1)
28483 if (!r2->reversed_p)
28485 glyph = r2->glyphs[TEXT_AREA];
28486 end = glyph + r2->used[TEXT_AREA];
28487 x = r2->x;
28489 else
28491 end = r2->glyphs[TEXT_AREA] - 1;
28492 glyph = end + r2->used[TEXT_AREA];
28496 if (!r2->reversed_p)
28498 /* Skip truncation and continuation glyphs near the end of the
28499 row, and also blanks and stretch glyphs inserted by
28500 extend_face_to_end_of_line. */
28501 while (end > glyph
28502 && NILP ((end - 1)->object))
28503 --end;
28504 /* Scan the rest of the glyph row from the end, looking for the
28505 first glyph that comes from BEFORE_STRING, AFTER_STRING, or
28506 DISP_STRING, or whose position is between START_CHARPOS
28507 and END_CHARPOS */
28508 for (--end;
28509 end > glyph
28510 && !NILP (end->object)
28511 && !EQ (end->object, disp_string)
28512 && !(BUFFERP (end->object)
28513 && (end->charpos >= start_charpos
28514 && end->charpos < end_charpos));
28515 --end)
28517 /* BEFORE_STRING or AFTER_STRING are only relevant if they
28518 are present at buffer positions between START_CHARPOS and
28519 END_CHARPOS, or if they come from an overlay. */
28520 if (EQ (end->object, before_string))
28522 pos = string_buffer_position (before_string, start_charpos);
28523 if (!pos || (pos >= start_charpos && pos < end_charpos))
28524 break;
28526 else if (EQ (end->object, after_string))
28528 pos = string_buffer_position (after_string, end_charpos);
28529 if (!pos || (pos >= start_charpos && pos < end_charpos))
28530 break;
28533 /* Find the X coordinate of the last glyph to be highlighted. */
28534 for (; glyph <= end; ++glyph)
28535 x += glyph->pixel_width;
28537 hlinfo->mouse_face_end_x = x;
28538 hlinfo->mouse_face_end_col = glyph - r2->glyphs[TEXT_AREA];
28540 else
28542 /* Skip truncation and continuation glyphs near the end of the
28543 row, and also blanks and stretch glyphs inserted by
28544 extend_face_to_end_of_line. */
28545 x = r2->x;
28546 end++;
28547 while (end < glyph
28548 && NILP (end->object))
28550 x += end->pixel_width;
28551 ++end;
28553 /* Scan the rest of the glyph row from the end, looking for the
28554 first glyph that comes from BEFORE_STRING, AFTER_STRING, or
28555 DISP_STRING, or whose position is between START_CHARPOS
28556 and END_CHARPOS */
28557 for ( ;
28558 end < glyph
28559 && !NILP (end->object)
28560 && !EQ (end->object, disp_string)
28561 && !(BUFFERP (end->object)
28562 && (end->charpos >= start_charpos
28563 && end->charpos < end_charpos));
28564 ++end)
28566 /* BEFORE_STRING or AFTER_STRING are only relevant if they
28567 are present at buffer positions between START_CHARPOS and
28568 END_CHARPOS, or if they come from an overlay. */
28569 if (EQ (end->object, before_string))
28571 pos = string_buffer_position (before_string, start_charpos);
28572 if (!pos || (pos >= start_charpos && pos < end_charpos))
28573 break;
28575 else if (EQ (end->object, after_string))
28577 pos = string_buffer_position (after_string, end_charpos);
28578 if (!pos || (pos >= start_charpos && pos < end_charpos))
28579 break;
28581 x += end->pixel_width;
28583 /* If we exited the above loop because we arrived at the last
28584 glyph of the row, and its buffer position is still not in
28585 range, it means the last character in range is the preceding
28586 newline. Bump the end column and x values to get past the
28587 last glyph. */
28588 if (end == glyph
28589 && BUFFERP (end->object)
28590 && (end->charpos < start_charpos
28591 || end->charpos >= end_charpos))
28593 x += end->pixel_width;
28594 ++end;
28596 hlinfo->mouse_face_end_x = x;
28597 hlinfo->mouse_face_end_col = end - r2->glyphs[TEXT_AREA];
28600 hlinfo->mouse_face_window = window;
28601 hlinfo->mouse_face_face_id
28602 = face_at_buffer_position (w, mouse_charpos, &ignore,
28603 mouse_charpos + 1,
28604 !hlinfo->mouse_face_hidden, -1);
28605 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
28608 /* The following function is not used anymore (replaced with
28609 mouse_face_from_string_pos), but I leave it here for the time
28610 being, in case someone would. */
28612 #if 0 /* not used */
28614 /* Find the position of the glyph for position POS in OBJECT in
28615 window W's current matrix, and return in *X, *Y the pixel
28616 coordinates, and return in *HPOS, *VPOS the column/row of the glyph.
28618 RIGHT_P non-zero means return the position of the right edge of the
28619 glyph, RIGHT_P zero means return the left edge position.
28621 If no glyph for POS exists in the matrix, return the position of
28622 the glyph with the next smaller position that is in the matrix, if
28623 RIGHT_P is zero. If RIGHT_P is non-zero, and no glyph for POS
28624 exists in the matrix, return the position of the glyph with the
28625 next larger position in OBJECT.
28627 Value is non-zero if a glyph was found. */
28629 static int
28630 fast_find_string_pos (struct window *w, ptrdiff_t pos, Lisp_Object object,
28631 int *hpos, int *vpos, int *x, int *y, int right_p)
28633 int yb = window_text_bottom_y (w);
28634 struct glyph_row *r;
28635 struct glyph *best_glyph = NULL;
28636 struct glyph_row *best_row = NULL;
28637 int best_x = 0;
28639 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
28640 r->enabled_p && r->y < yb;
28641 ++r)
28643 struct glyph *g = r->glyphs[TEXT_AREA];
28644 struct glyph *e = g + r->used[TEXT_AREA];
28645 int gx;
28647 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
28648 if (EQ (g->object, object))
28650 if (g->charpos == pos)
28652 best_glyph = g;
28653 best_x = gx;
28654 best_row = r;
28655 goto found;
28657 else if (best_glyph == NULL
28658 || ((eabs (g->charpos - pos)
28659 < eabs (best_glyph->charpos - pos))
28660 && (right_p
28661 ? g->charpos < pos
28662 : g->charpos > pos)))
28664 best_glyph = g;
28665 best_x = gx;
28666 best_row = r;
28671 found:
28673 if (best_glyph)
28675 *x = best_x;
28676 *hpos = best_glyph - best_row->glyphs[TEXT_AREA];
28678 if (right_p)
28680 *x += best_glyph->pixel_width;
28681 ++*hpos;
28684 *y = best_row->y;
28685 *vpos = MATRIX_ROW_VPOS (best_row, w->current_matrix);
28688 return best_glyph != NULL;
28690 #endif /* not used */
28692 /* Find the positions of the first and the last glyphs in window W's
28693 current matrix that occlude positions [STARTPOS..ENDPOS) in OBJECT
28694 (assumed to be a string), and return in HLINFO's mouse_face_*
28695 members the pixel and column/row coordinates of those glyphs. */
28697 static void
28698 mouse_face_from_string_pos (struct window *w, Mouse_HLInfo *hlinfo,
28699 Lisp_Object object,
28700 ptrdiff_t startpos, ptrdiff_t endpos)
28702 int yb = window_text_bottom_y (w);
28703 struct glyph_row *r;
28704 struct glyph *g, *e;
28705 int gx;
28706 int found = 0;
28708 /* Find the glyph row with at least one position in the range
28709 [STARTPOS..ENDPOS), and the first glyph in that row whose
28710 position belongs to that range. */
28711 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
28712 r->enabled_p && r->y < yb;
28713 ++r)
28715 if (!r->reversed_p)
28717 g = r->glyphs[TEXT_AREA];
28718 e = g + r->used[TEXT_AREA];
28719 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
28720 if (EQ (g->object, object)
28721 && startpos <= g->charpos && g->charpos < endpos)
28723 hlinfo->mouse_face_beg_row
28724 = MATRIX_ROW_VPOS (r, w->current_matrix);
28725 hlinfo->mouse_face_beg_col = g - r->glyphs[TEXT_AREA];
28726 hlinfo->mouse_face_beg_x = gx;
28727 found = 1;
28728 break;
28731 else
28733 struct glyph *g1;
28735 e = r->glyphs[TEXT_AREA];
28736 g = e + r->used[TEXT_AREA];
28737 for ( ; g > e; --g)
28738 if (EQ ((g-1)->object, object)
28739 && startpos <= (g-1)->charpos && (g-1)->charpos < endpos)
28741 hlinfo->mouse_face_beg_row
28742 = MATRIX_ROW_VPOS (r, w->current_matrix);
28743 hlinfo->mouse_face_beg_col = g - r->glyphs[TEXT_AREA];
28744 for (gx = r->x, g1 = r->glyphs[TEXT_AREA]; g1 < g; ++g1)
28745 gx += g1->pixel_width;
28746 hlinfo->mouse_face_beg_x = gx;
28747 found = 1;
28748 break;
28751 if (found)
28752 break;
28755 if (!found)
28756 return;
28758 /* Starting with the next row, look for the first row which does NOT
28759 include any glyphs whose positions are in the range. */
28760 for (++r; r->enabled_p && r->y < yb; ++r)
28762 g = r->glyphs[TEXT_AREA];
28763 e = g + r->used[TEXT_AREA];
28764 found = 0;
28765 for ( ; g < e; ++g)
28766 if (EQ (g->object, object)
28767 && startpos <= g->charpos && g->charpos < endpos)
28769 found = 1;
28770 break;
28772 if (!found)
28773 break;
28776 /* The highlighted region ends on the previous row. */
28777 r--;
28779 /* Set the end row. */
28780 hlinfo->mouse_face_end_row = MATRIX_ROW_VPOS (r, w->current_matrix);
28782 /* Compute and set the end column and the end column's horizontal
28783 pixel coordinate. */
28784 if (!r->reversed_p)
28786 g = r->glyphs[TEXT_AREA];
28787 e = g + r->used[TEXT_AREA];
28788 for ( ; e > g; --e)
28789 if (EQ ((e-1)->object, object)
28790 && startpos <= (e-1)->charpos && (e-1)->charpos < endpos)
28791 break;
28792 hlinfo->mouse_face_end_col = e - g;
28794 for (gx = r->x; g < e; ++g)
28795 gx += g->pixel_width;
28796 hlinfo->mouse_face_end_x = gx;
28798 else
28800 e = r->glyphs[TEXT_AREA];
28801 g = e + r->used[TEXT_AREA];
28802 for (gx = r->x ; e < g; ++e)
28804 if (EQ (e->object, object)
28805 && startpos <= e->charpos && e->charpos < endpos)
28806 break;
28807 gx += e->pixel_width;
28809 hlinfo->mouse_face_end_col = e - r->glyphs[TEXT_AREA];
28810 hlinfo->mouse_face_end_x = gx;
28814 #ifdef HAVE_WINDOW_SYSTEM
28816 /* See if position X, Y is within a hot-spot of an image. */
28818 static int
28819 on_hot_spot_p (Lisp_Object hot_spot, int x, int y)
28821 if (!CONSP (hot_spot))
28822 return 0;
28824 if (EQ (XCAR (hot_spot), Qrect))
28826 /* CDR is (Top-Left . Bottom-Right) = ((x0 . y0) . (x1 . y1)) */
28827 Lisp_Object rect = XCDR (hot_spot);
28828 Lisp_Object tem;
28829 if (!CONSP (rect))
28830 return 0;
28831 if (!CONSP (XCAR (rect)))
28832 return 0;
28833 if (!CONSP (XCDR (rect)))
28834 return 0;
28835 if (!(tem = XCAR (XCAR (rect)), INTEGERP (tem) && x >= XINT (tem)))
28836 return 0;
28837 if (!(tem = XCDR (XCAR (rect)), INTEGERP (tem) && y >= XINT (tem)))
28838 return 0;
28839 if (!(tem = XCAR (XCDR (rect)), INTEGERP (tem) && x <= XINT (tem)))
28840 return 0;
28841 if (!(tem = XCDR (XCDR (rect)), INTEGERP (tem) && y <= XINT (tem)))
28842 return 0;
28843 return 1;
28845 else if (EQ (XCAR (hot_spot), Qcircle))
28847 /* CDR is (Center . Radius) = ((x0 . y0) . r) */
28848 Lisp_Object circ = XCDR (hot_spot);
28849 Lisp_Object lr, lx0, ly0;
28850 if (CONSP (circ)
28851 && CONSP (XCAR (circ))
28852 && (lr = XCDR (circ), INTEGERP (lr) || FLOATP (lr))
28853 && (lx0 = XCAR (XCAR (circ)), INTEGERP (lx0))
28854 && (ly0 = XCDR (XCAR (circ)), INTEGERP (ly0)))
28856 double r = XFLOATINT (lr);
28857 double dx = XINT (lx0) - x;
28858 double dy = XINT (ly0) - y;
28859 return (dx * dx + dy * dy <= r * r);
28862 else if (EQ (XCAR (hot_spot), Qpoly))
28864 /* CDR is [x0 y0 x1 y1 x2 y2 ...x(n-1) y(n-1)] */
28865 if (VECTORP (XCDR (hot_spot)))
28867 struct Lisp_Vector *v = XVECTOR (XCDR (hot_spot));
28868 Lisp_Object *poly = v->contents;
28869 ptrdiff_t n = v->header.size;
28870 ptrdiff_t i;
28871 int inside = 0;
28872 Lisp_Object lx, ly;
28873 int x0, y0;
28875 /* Need an even number of coordinates, and at least 3 edges. */
28876 if (n < 6 || n & 1)
28877 return 0;
28879 /* Count edge segments intersecting line from (X,Y) to (X,infinity).
28880 If count is odd, we are inside polygon. Pixels on edges
28881 may or may not be included depending on actual geometry of the
28882 polygon. */
28883 if ((lx = poly[n-2], !INTEGERP (lx))
28884 || (ly = poly[n-1], !INTEGERP (lx)))
28885 return 0;
28886 x0 = XINT (lx), y0 = XINT (ly);
28887 for (i = 0; i < n; i += 2)
28889 int x1 = x0, y1 = y0;
28890 if ((lx = poly[i], !INTEGERP (lx))
28891 || (ly = poly[i+1], !INTEGERP (ly)))
28892 return 0;
28893 x0 = XINT (lx), y0 = XINT (ly);
28895 /* Does this segment cross the X line? */
28896 if (x0 >= x)
28898 if (x1 >= x)
28899 continue;
28901 else if (x1 < x)
28902 continue;
28903 if (y > y0 && y > y1)
28904 continue;
28905 if (y < y0 + ((y1 - y0) * (x - x0)) / (x1 - x0))
28906 inside = !inside;
28908 return inside;
28911 return 0;
28914 Lisp_Object
28915 find_hot_spot (Lisp_Object map, int x, int y)
28917 while (CONSP (map))
28919 if (CONSP (XCAR (map))
28920 && on_hot_spot_p (XCAR (XCAR (map)), x, y))
28921 return XCAR (map);
28922 map = XCDR (map);
28925 return Qnil;
28928 DEFUN ("lookup-image-map", Flookup_image_map, Slookup_image_map,
28929 3, 3, 0,
28930 doc: /* Lookup in image map MAP coordinates X and Y.
28931 An image map is an alist where each element has the format (AREA ID PLIST).
28932 An AREA is specified as either a rectangle, a circle, or a polygon:
28933 A rectangle is a cons (rect . ((x0 . y0) . (x1 . y1))) specifying the
28934 pixel coordinates of the upper left and bottom right corners.
28935 A circle is a cons (circle . ((x0 . y0) . r)) specifying the center
28936 and the radius of the circle; r may be a float or integer.
28937 A polygon is a cons (poly . [x0 y0 x1 y1 ...]) where each pair in the
28938 vector describes one corner in the polygon.
28939 Returns the alist element for the first matching AREA in MAP. */)
28940 (Lisp_Object map, Lisp_Object x, Lisp_Object y)
28942 if (NILP (map))
28943 return Qnil;
28945 CHECK_NUMBER (x);
28946 CHECK_NUMBER (y);
28948 return find_hot_spot (map,
28949 clip_to_bounds (INT_MIN, XINT (x), INT_MAX),
28950 clip_to_bounds (INT_MIN, XINT (y), INT_MAX));
28954 /* Display frame CURSOR, optionally using shape defined by POINTER. */
28955 static void
28956 define_frame_cursor1 (struct frame *f, Cursor cursor, Lisp_Object pointer)
28958 /* Do not change cursor shape while dragging mouse. */
28959 if (!NILP (do_mouse_tracking))
28960 return;
28962 if (!NILP (pointer))
28964 if (EQ (pointer, Qarrow))
28965 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
28966 else if (EQ (pointer, Qhand))
28967 cursor = FRAME_X_OUTPUT (f)->hand_cursor;
28968 else if (EQ (pointer, Qtext))
28969 cursor = FRAME_X_OUTPUT (f)->text_cursor;
28970 else if (EQ (pointer, intern ("hdrag")))
28971 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
28972 else if (EQ (pointer, intern ("nhdrag")))
28973 cursor = FRAME_X_OUTPUT (f)->vertical_drag_cursor;
28974 #ifdef HAVE_X_WINDOWS
28975 else if (EQ (pointer, intern ("vdrag")))
28976 cursor = FRAME_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
28977 #endif
28978 else if (EQ (pointer, intern ("hourglass")))
28979 cursor = FRAME_X_OUTPUT (f)->hourglass_cursor;
28980 else if (EQ (pointer, Qmodeline))
28981 cursor = FRAME_X_OUTPUT (f)->modeline_cursor;
28982 else
28983 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
28986 if (cursor != No_Cursor)
28987 FRAME_RIF (f)->define_frame_cursor (f, cursor);
28990 #endif /* HAVE_WINDOW_SYSTEM */
28992 /* Take proper action when mouse has moved to the mode or header line
28993 or marginal area AREA of window W, x-position X and y-position Y.
28994 X is relative to the start of the text display area of W, so the
28995 width of bitmap areas and scroll bars must be subtracted to get a
28996 position relative to the start of the mode line. */
28998 static void
28999 note_mode_line_or_margin_highlight (Lisp_Object window, int x, int y,
29000 enum window_part area)
29002 struct window *w = XWINDOW (window);
29003 struct frame *f = XFRAME (w->frame);
29004 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
29005 #ifdef HAVE_WINDOW_SYSTEM
29006 Display_Info *dpyinfo;
29007 #endif
29008 Cursor cursor = No_Cursor;
29009 Lisp_Object pointer = Qnil;
29010 int dx, dy, width, height;
29011 ptrdiff_t charpos;
29012 Lisp_Object string, object = Qnil;
29013 Lisp_Object pos IF_LINT (= Qnil), help;
29015 Lisp_Object mouse_face;
29016 int original_x_pixel = x;
29017 struct glyph * glyph = NULL, * row_start_glyph = NULL;
29018 struct glyph_row *row IF_LINT (= 0);
29020 if (area == ON_MODE_LINE || area == ON_HEADER_LINE)
29022 int x0;
29023 struct glyph *end;
29025 /* Kludge alert: mode_line_string takes X/Y in pixels, but
29026 returns them in row/column units! */
29027 string = mode_line_string (w, area, &x, &y, &charpos,
29028 &object, &dx, &dy, &width, &height);
29030 row = (area == ON_MODE_LINE
29031 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
29032 : MATRIX_HEADER_LINE_ROW (w->current_matrix));
29034 /* Find the glyph under the mouse pointer. */
29035 if (row->mode_line_p && row->enabled_p)
29037 glyph = row_start_glyph = row->glyphs[TEXT_AREA];
29038 end = glyph + row->used[TEXT_AREA];
29040 for (x0 = original_x_pixel;
29041 glyph < end && x0 >= glyph->pixel_width;
29042 ++glyph)
29043 x0 -= glyph->pixel_width;
29045 if (glyph >= end)
29046 glyph = NULL;
29049 else
29051 x -= WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
29052 /* Kludge alert: marginal_area_string takes X/Y in pixels, but
29053 returns them in row/column units! */
29054 string = marginal_area_string (w, area, &x, &y, &charpos,
29055 &object, &dx, &dy, &width, &height);
29058 help = Qnil;
29060 #ifdef HAVE_WINDOW_SYSTEM
29061 if (IMAGEP (object))
29063 Lisp_Object image_map, hotspot;
29064 if ((image_map = Fplist_get (XCDR (object), QCmap),
29065 !NILP (image_map))
29066 && (hotspot = find_hot_spot (image_map, dx, dy),
29067 CONSP (hotspot))
29068 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
29070 Lisp_Object plist;
29072 /* Could check XCAR (hotspot) to see if we enter/leave this hot-spot.
29073 If so, we could look for mouse-enter, mouse-leave
29074 properties in PLIST (and do something...). */
29075 hotspot = XCDR (hotspot);
29076 if (CONSP (hotspot)
29077 && (plist = XCAR (hotspot), CONSP (plist)))
29079 pointer = Fplist_get (plist, Qpointer);
29080 if (NILP (pointer))
29081 pointer = Qhand;
29082 help = Fplist_get (plist, Qhelp_echo);
29083 if (!NILP (help))
29085 help_echo_string = help;
29086 XSETWINDOW (help_echo_window, w);
29087 help_echo_object = w->contents;
29088 help_echo_pos = charpos;
29092 if (NILP (pointer))
29093 pointer = Fplist_get (XCDR (object), QCpointer);
29095 #endif /* HAVE_WINDOW_SYSTEM */
29097 if (STRINGP (string))
29098 pos = make_number (charpos);
29100 /* Set the help text and mouse pointer. If the mouse is on a part
29101 of the mode line without any text (e.g. past the right edge of
29102 the mode line text), use the default help text and pointer. */
29103 if (STRINGP (string) || area == ON_MODE_LINE)
29105 /* Arrange to display the help by setting the global variables
29106 help_echo_string, help_echo_object, and help_echo_pos. */
29107 if (NILP (help))
29109 if (STRINGP (string))
29110 help = Fget_text_property (pos, Qhelp_echo, string);
29112 if (!NILP (help))
29114 help_echo_string = help;
29115 XSETWINDOW (help_echo_window, w);
29116 help_echo_object = string;
29117 help_echo_pos = charpos;
29119 else if (area == ON_MODE_LINE)
29121 Lisp_Object default_help
29122 = buffer_local_value (Qmode_line_default_help_echo,
29123 w->contents);
29125 if (STRINGP (default_help))
29127 help_echo_string = default_help;
29128 XSETWINDOW (help_echo_window, w);
29129 help_echo_object = Qnil;
29130 help_echo_pos = -1;
29135 #ifdef HAVE_WINDOW_SYSTEM
29136 /* Change the mouse pointer according to what is under it. */
29137 if (FRAME_WINDOW_P (f))
29139 bool draggable = (! WINDOW_BOTTOMMOST_P (w)
29140 || minibuf_level
29141 || NILP (Vresize_mini_windows));
29143 dpyinfo = FRAME_DISPLAY_INFO (f);
29144 if (STRINGP (string))
29146 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
29148 if (NILP (pointer))
29149 pointer = Fget_text_property (pos, Qpointer, string);
29151 /* Change the mouse pointer according to what is under X/Y. */
29152 if (NILP (pointer)
29153 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE)))
29155 Lisp_Object map;
29156 map = Fget_text_property (pos, Qlocal_map, string);
29157 if (!KEYMAPP (map))
29158 map = Fget_text_property (pos, Qkeymap, string);
29159 if (!KEYMAPP (map) && draggable)
29160 cursor = dpyinfo->vertical_scroll_bar_cursor;
29163 else if (draggable)
29164 /* Default mode-line pointer. */
29165 cursor = FRAME_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
29167 #endif
29170 /* Change the mouse face according to what is under X/Y. */
29171 if (STRINGP (string))
29173 mouse_face = Fget_text_property (pos, Qmouse_face, string);
29174 if (!NILP (Vmouse_highlight) && !NILP (mouse_face)
29175 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
29176 && glyph)
29178 Lisp_Object b, e;
29180 struct glyph * tmp_glyph;
29182 int gpos;
29183 int gseq_length;
29184 int total_pixel_width;
29185 ptrdiff_t begpos, endpos, ignore;
29187 int vpos, hpos;
29189 b = Fprevious_single_property_change (make_number (charpos + 1),
29190 Qmouse_face, string, Qnil);
29191 if (NILP (b))
29192 begpos = 0;
29193 else
29194 begpos = XINT (b);
29196 e = Fnext_single_property_change (pos, Qmouse_face, string, Qnil);
29197 if (NILP (e))
29198 endpos = SCHARS (string);
29199 else
29200 endpos = XINT (e);
29202 /* Calculate the glyph position GPOS of GLYPH in the
29203 displayed string, relative to the beginning of the
29204 highlighted part of the string.
29206 Note: GPOS is different from CHARPOS. CHARPOS is the
29207 position of GLYPH in the internal string object. A mode
29208 line string format has structures which are converted to
29209 a flattened string by the Emacs Lisp interpreter. The
29210 internal string is an element of those structures. The
29211 displayed string is the flattened string. */
29212 tmp_glyph = row_start_glyph;
29213 while (tmp_glyph < glyph
29214 && (!(EQ (tmp_glyph->object, glyph->object)
29215 && begpos <= tmp_glyph->charpos
29216 && tmp_glyph->charpos < endpos)))
29217 tmp_glyph++;
29218 gpos = glyph - tmp_glyph;
29220 /* Calculate the length GSEQ_LENGTH of the glyph sequence of
29221 the highlighted part of the displayed string to which
29222 GLYPH belongs. Note: GSEQ_LENGTH is different from
29223 SCHARS (STRING), because the latter returns the length of
29224 the internal string. */
29225 for (tmp_glyph = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
29226 tmp_glyph > glyph
29227 && (!(EQ (tmp_glyph->object, glyph->object)
29228 && begpos <= tmp_glyph->charpos
29229 && tmp_glyph->charpos < endpos));
29230 tmp_glyph--)
29232 gseq_length = gpos + (tmp_glyph - glyph) + 1;
29234 /* Calculate the total pixel width of all the glyphs between
29235 the beginning of the highlighted area and GLYPH. */
29236 total_pixel_width = 0;
29237 for (tmp_glyph = glyph - gpos; tmp_glyph != glyph; tmp_glyph++)
29238 total_pixel_width += tmp_glyph->pixel_width;
29240 /* Pre calculation of re-rendering position. Note: X is in
29241 column units here, after the call to mode_line_string or
29242 marginal_area_string. */
29243 hpos = x - gpos;
29244 vpos = (area == ON_MODE_LINE
29245 ? (w->current_matrix)->nrows - 1
29246 : 0);
29248 /* If GLYPH's position is included in the region that is
29249 already drawn in mouse face, we have nothing to do. */
29250 if ( EQ (window, hlinfo->mouse_face_window)
29251 && (!row->reversed_p
29252 ? (hlinfo->mouse_face_beg_col <= hpos
29253 && hpos < hlinfo->mouse_face_end_col)
29254 /* In R2L rows we swap BEG and END, see below. */
29255 : (hlinfo->mouse_face_end_col <= hpos
29256 && hpos < hlinfo->mouse_face_beg_col))
29257 && hlinfo->mouse_face_beg_row == vpos )
29258 return;
29260 if (clear_mouse_face (hlinfo))
29261 cursor = No_Cursor;
29263 if (!row->reversed_p)
29265 hlinfo->mouse_face_beg_col = hpos;
29266 hlinfo->mouse_face_beg_x = original_x_pixel
29267 - (total_pixel_width + dx);
29268 hlinfo->mouse_face_end_col = hpos + gseq_length;
29269 hlinfo->mouse_face_end_x = 0;
29271 else
29273 /* In R2L rows, show_mouse_face expects BEG and END
29274 coordinates to be swapped. */
29275 hlinfo->mouse_face_end_col = hpos;
29276 hlinfo->mouse_face_end_x = original_x_pixel
29277 - (total_pixel_width + dx);
29278 hlinfo->mouse_face_beg_col = hpos + gseq_length;
29279 hlinfo->mouse_face_beg_x = 0;
29282 hlinfo->mouse_face_beg_row = vpos;
29283 hlinfo->mouse_face_end_row = hlinfo->mouse_face_beg_row;
29284 hlinfo->mouse_face_past_end = 0;
29285 hlinfo->mouse_face_window = window;
29287 hlinfo->mouse_face_face_id = face_at_string_position (w, string,
29288 charpos,
29289 0, &ignore,
29290 glyph->face_id,
29292 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
29294 if (NILP (pointer))
29295 pointer = Qhand;
29297 else if ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
29298 clear_mouse_face (hlinfo);
29300 #ifdef HAVE_WINDOW_SYSTEM
29301 if (FRAME_WINDOW_P (f))
29302 define_frame_cursor1 (f, cursor, pointer);
29303 #endif
29307 /* EXPORT:
29308 Take proper action when the mouse has moved to position X, Y on
29309 frame F with regards to highlighting portions of display that have
29310 mouse-face properties. Also de-highlight portions of display where
29311 the mouse was before, set the mouse pointer shape as appropriate
29312 for the mouse coordinates, and activate help echo (tooltips).
29313 X and Y can be negative or out of range. */
29315 void
29316 note_mouse_highlight (struct frame *f, int x, int y)
29318 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
29319 enum window_part part = ON_NOTHING;
29320 Lisp_Object window;
29321 struct window *w;
29322 Cursor cursor = No_Cursor;
29323 Lisp_Object pointer = Qnil; /* Takes precedence over cursor! */
29324 struct buffer *b;
29326 /* When a menu is active, don't highlight because this looks odd. */
29327 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS) || defined (MSDOS)
29328 if (popup_activated ())
29329 return;
29330 #endif
29332 if (!f->glyphs_initialized_p
29333 || f->pointer_invisible)
29334 return;
29336 hlinfo->mouse_face_mouse_x = x;
29337 hlinfo->mouse_face_mouse_y = y;
29338 hlinfo->mouse_face_mouse_frame = f;
29340 if (hlinfo->mouse_face_defer)
29341 return;
29343 /* Which window is that in? */
29344 window = window_from_coordinates (f, x, y, &part, 1);
29346 /* If displaying active text in another window, clear that. */
29347 if (! EQ (window, hlinfo->mouse_face_window)
29348 /* Also clear if we move out of text area in same window. */
29349 || (!NILP (hlinfo->mouse_face_window)
29350 && !NILP (window)
29351 && part != ON_TEXT
29352 && part != ON_MODE_LINE
29353 && part != ON_HEADER_LINE))
29354 clear_mouse_face (hlinfo);
29356 /* Not on a window -> return. */
29357 if (!WINDOWP (window))
29358 return;
29360 /* Reset help_echo_string. It will get recomputed below. */
29361 help_echo_string = Qnil;
29363 /* Convert to window-relative pixel coordinates. */
29364 w = XWINDOW (window);
29365 frame_to_window_pixel_xy (w, &x, &y);
29367 #if defined (HAVE_WINDOW_SYSTEM) && ! defined (USE_GTK) && ! defined (HAVE_NS)
29368 /* Handle tool-bar window differently since it doesn't display a
29369 buffer. */
29370 if (EQ (window, f->tool_bar_window))
29372 note_tool_bar_highlight (f, x, y);
29373 return;
29375 #endif
29377 /* Mouse is on the mode, header line or margin? */
29378 if (part == ON_MODE_LINE || part == ON_HEADER_LINE
29379 || part == ON_LEFT_MARGIN || part == ON_RIGHT_MARGIN)
29381 note_mode_line_or_margin_highlight (window, x, y, part);
29383 #ifdef HAVE_WINDOW_SYSTEM
29384 if (part == ON_LEFT_MARGIN || part == ON_RIGHT_MARGIN)
29386 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
29387 /* Show non-text cursor (Bug#16647). */
29388 goto set_cursor;
29390 else
29391 #endif
29392 return;
29395 #ifdef HAVE_WINDOW_SYSTEM
29396 if (part == ON_VERTICAL_BORDER)
29398 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
29399 help_echo_string = build_string ("drag-mouse-1: resize");
29401 else if (part == ON_RIGHT_DIVIDER)
29403 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
29404 help_echo_string = build_string ("drag-mouse-1: resize");
29406 else if (part == ON_BOTTOM_DIVIDER)
29407 if (! WINDOW_BOTTOMMOST_P (w)
29408 || minibuf_level
29409 || NILP (Vresize_mini_windows))
29411 cursor = FRAME_X_OUTPUT (f)->vertical_drag_cursor;
29412 help_echo_string = build_string ("drag-mouse-1: resize");
29414 else
29415 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
29416 else if (part == ON_LEFT_FRINGE || part == ON_RIGHT_FRINGE
29417 || part == ON_VERTICAL_SCROLL_BAR
29418 || part == ON_HORIZONTAL_SCROLL_BAR)
29419 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
29420 else
29421 cursor = FRAME_X_OUTPUT (f)->text_cursor;
29422 #endif
29424 /* Are we in a window whose display is up to date?
29425 And verify the buffer's text has not changed. */
29426 b = XBUFFER (w->contents);
29427 if (part == ON_TEXT && w->window_end_valid && !window_outdated (w))
29429 int hpos, vpos, dx, dy, area = LAST_AREA;
29430 ptrdiff_t pos;
29431 struct glyph *glyph;
29432 Lisp_Object object;
29433 Lisp_Object mouse_face = Qnil, position;
29434 Lisp_Object *overlay_vec = NULL;
29435 ptrdiff_t i, noverlays;
29436 struct buffer *obuf;
29437 ptrdiff_t obegv, ozv;
29438 int same_region;
29440 /* Find the glyph under X/Y. */
29441 glyph = x_y_to_hpos_vpos (w, x, y, &hpos, &vpos, &dx, &dy, &area);
29443 #ifdef HAVE_WINDOW_SYSTEM
29444 /* Look for :pointer property on image. */
29445 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
29447 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
29448 if (img != NULL && IMAGEP (img->spec))
29450 Lisp_Object image_map, hotspot;
29451 if ((image_map = Fplist_get (XCDR (img->spec), QCmap),
29452 !NILP (image_map))
29453 && (hotspot = find_hot_spot (image_map,
29454 glyph->slice.img.x + dx,
29455 glyph->slice.img.y + dy),
29456 CONSP (hotspot))
29457 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
29459 Lisp_Object plist;
29461 /* Could check XCAR (hotspot) to see if we enter/leave
29462 this hot-spot.
29463 If so, we could look for mouse-enter, mouse-leave
29464 properties in PLIST (and do something...). */
29465 hotspot = XCDR (hotspot);
29466 if (CONSP (hotspot)
29467 && (plist = XCAR (hotspot), CONSP (plist)))
29469 pointer = Fplist_get (plist, Qpointer);
29470 if (NILP (pointer))
29471 pointer = Qhand;
29472 help_echo_string = Fplist_get (plist, Qhelp_echo);
29473 if (!NILP (help_echo_string))
29475 help_echo_window = window;
29476 help_echo_object = glyph->object;
29477 help_echo_pos = glyph->charpos;
29481 if (NILP (pointer))
29482 pointer = Fplist_get (XCDR (img->spec), QCpointer);
29485 #endif /* HAVE_WINDOW_SYSTEM */
29487 /* Clear mouse face if X/Y not over text. */
29488 if (glyph == NULL
29489 || area != TEXT_AREA
29490 || !MATRIX_ROW_DISPLAYS_TEXT_P (MATRIX_ROW (w->current_matrix, vpos))
29491 /* Glyph's OBJECT is nil for glyphs inserted by the
29492 display engine for its internal purposes, like truncation
29493 and continuation glyphs and blanks beyond the end of
29494 line's text on text terminals. If we are over such a
29495 glyph, we are not over any text. */
29496 || NILP (glyph->object)
29497 /* R2L rows have a stretch glyph at their front, which
29498 stands for no text, whereas L2R rows have no glyphs at
29499 all beyond the end of text. Treat such stretch glyphs
29500 like we do with NULL glyphs in L2R rows. */
29501 || (MATRIX_ROW (w->current_matrix, vpos)->reversed_p
29502 && glyph == MATRIX_ROW_GLYPH_START (w->current_matrix, vpos)
29503 && glyph->type == STRETCH_GLYPH
29504 && glyph->avoid_cursor_p))
29506 if (clear_mouse_face (hlinfo))
29507 cursor = No_Cursor;
29508 #ifdef HAVE_WINDOW_SYSTEM
29509 if (FRAME_WINDOW_P (f) && NILP (pointer))
29511 if (area != TEXT_AREA)
29512 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
29513 else
29514 pointer = Vvoid_text_area_pointer;
29516 #endif
29517 goto set_cursor;
29520 pos = glyph->charpos;
29521 object = glyph->object;
29522 if (!STRINGP (object) && !BUFFERP (object))
29523 goto set_cursor;
29525 /* If we get an out-of-range value, return now; avoid an error. */
29526 if (BUFFERP (object) && pos > BUF_Z (b))
29527 goto set_cursor;
29529 /* Make the window's buffer temporarily current for
29530 overlays_at and compute_char_face. */
29531 obuf = current_buffer;
29532 current_buffer = b;
29533 obegv = BEGV;
29534 ozv = ZV;
29535 BEGV = BEG;
29536 ZV = Z;
29538 /* Is this char mouse-active or does it have help-echo? */
29539 position = make_number (pos);
29541 USE_SAFE_ALLOCA;
29543 if (BUFFERP (object))
29545 /* Put all the overlays we want in a vector in overlay_vec. */
29546 GET_OVERLAYS_AT (pos, overlay_vec, noverlays, NULL, 0);
29547 /* Sort overlays into increasing priority order. */
29548 noverlays = sort_overlays (overlay_vec, noverlays, w);
29550 else
29551 noverlays = 0;
29553 if (NILP (Vmouse_highlight))
29555 clear_mouse_face (hlinfo);
29556 goto check_help_echo;
29559 same_region = coords_in_mouse_face_p (w, hpos, vpos);
29561 if (same_region)
29562 cursor = No_Cursor;
29564 /* Check mouse-face highlighting. */
29565 if (! same_region
29566 /* If there exists an overlay with mouse-face overlapping
29567 the one we are currently highlighting, we have to
29568 check if we enter the overlapping overlay, and then
29569 highlight only that. */
29570 || (OVERLAYP (hlinfo->mouse_face_overlay)
29571 && mouse_face_overlay_overlaps (hlinfo->mouse_face_overlay)))
29573 /* Find the highest priority overlay with a mouse-face. */
29574 Lisp_Object overlay = Qnil;
29575 for (i = noverlays - 1; i >= 0 && NILP (overlay); --i)
29577 mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
29578 if (!NILP (mouse_face))
29579 overlay = overlay_vec[i];
29582 /* If we're highlighting the same overlay as before, there's
29583 no need to do that again. */
29584 if (!NILP (overlay) && EQ (overlay, hlinfo->mouse_face_overlay))
29585 goto check_help_echo;
29586 hlinfo->mouse_face_overlay = overlay;
29588 /* Clear the display of the old active region, if any. */
29589 if (clear_mouse_face (hlinfo))
29590 cursor = No_Cursor;
29592 /* If no overlay applies, get a text property. */
29593 if (NILP (overlay))
29594 mouse_face = Fget_text_property (position, Qmouse_face, object);
29596 /* Next, compute the bounds of the mouse highlighting and
29597 display it. */
29598 if (!NILP (mouse_face) && STRINGP (object))
29600 /* The mouse-highlighting comes from a display string
29601 with a mouse-face. */
29602 Lisp_Object s, e;
29603 ptrdiff_t ignore;
29605 s = Fprevious_single_property_change
29606 (make_number (pos + 1), Qmouse_face, object, Qnil);
29607 e = Fnext_single_property_change
29608 (position, Qmouse_face, object, Qnil);
29609 if (NILP (s))
29610 s = make_number (0);
29611 if (NILP (e))
29612 e = make_number (SCHARS (object));
29613 mouse_face_from_string_pos (w, hlinfo, object,
29614 XINT (s), XINT (e));
29615 hlinfo->mouse_face_past_end = 0;
29616 hlinfo->mouse_face_window = window;
29617 hlinfo->mouse_face_face_id
29618 = face_at_string_position (w, object, pos, 0, &ignore,
29619 glyph->face_id, 1);
29620 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
29621 cursor = No_Cursor;
29623 else
29625 /* The mouse-highlighting, if any, comes from an overlay
29626 or text property in the buffer. */
29627 Lisp_Object buffer IF_LINT (= Qnil);
29628 Lisp_Object disp_string IF_LINT (= Qnil);
29630 if (STRINGP (object))
29632 /* If we are on a display string with no mouse-face,
29633 check if the text under it has one. */
29634 struct glyph_row *r = MATRIX_ROW (w->current_matrix, vpos);
29635 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
29636 pos = string_buffer_position (object, start);
29637 if (pos > 0)
29639 mouse_face = get_char_property_and_overlay
29640 (make_number (pos), Qmouse_face, w->contents, &overlay);
29641 buffer = w->contents;
29642 disp_string = object;
29645 else
29647 buffer = object;
29648 disp_string = Qnil;
29651 if (!NILP (mouse_face))
29653 Lisp_Object before, after;
29654 Lisp_Object before_string, after_string;
29655 /* To correctly find the limits of mouse highlight
29656 in a bidi-reordered buffer, we must not use the
29657 optimization of limiting the search in
29658 previous-single-property-change and
29659 next-single-property-change, because
29660 rows_from_pos_range needs the real start and end
29661 positions to DTRT in this case. That's because
29662 the first row visible in a window does not
29663 necessarily display the character whose position
29664 is the smallest. */
29665 Lisp_Object lim1
29666 = NILP (BVAR (XBUFFER (buffer), bidi_display_reordering))
29667 ? Fmarker_position (w->start)
29668 : Qnil;
29669 Lisp_Object lim2
29670 = NILP (BVAR (XBUFFER (buffer), bidi_display_reordering))
29671 ? make_number (BUF_Z (XBUFFER (buffer))
29672 - w->window_end_pos)
29673 : Qnil;
29675 if (NILP (overlay))
29677 /* Handle the text property case. */
29678 before = Fprevious_single_property_change
29679 (make_number (pos + 1), Qmouse_face, buffer, lim1);
29680 after = Fnext_single_property_change
29681 (make_number (pos), Qmouse_face, buffer, lim2);
29682 before_string = after_string = Qnil;
29684 else
29686 /* Handle the overlay case. */
29687 before = Foverlay_start (overlay);
29688 after = Foverlay_end (overlay);
29689 before_string = Foverlay_get (overlay, Qbefore_string);
29690 after_string = Foverlay_get (overlay, Qafter_string);
29692 if (!STRINGP (before_string)) before_string = Qnil;
29693 if (!STRINGP (after_string)) after_string = Qnil;
29696 mouse_face_from_buffer_pos (window, hlinfo, pos,
29697 NILP (before)
29699 : XFASTINT (before),
29700 NILP (after)
29701 ? BUF_Z (XBUFFER (buffer))
29702 : XFASTINT (after),
29703 before_string, after_string,
29704 disp_string);
29705 cursor = No_Cursor;
29710 check_help_echo:
29712 /* Look for a `help-echo' property. */
29713 if (NILP (help_echo_string)) {
29714 Lisp_Object help, overlay;
29716 /* Check overlays first. */
29717 help = overlay = Qnil;
29718 for (i = noverlays - 1; i >= 0 && NILP (help); --i)
29720 overlay = overlay_vec[i];
29721 help = Foverlay_get (overlay, Qhelp_echo);
29724 if (!NILP (help))
29726 help_echo_string = help;
29727 help_echo_window = window;
29728 help_echo_object = overlay;
29729 help_echo_pos = pos;
29731 else
29733 Lisp_Object obj = glyph->object;
29734 ptrdiff_t charpos = glyph->charpos;
29736 /* Try text properties. */
29737 if (STRINGP (obj)
29738 && charpos >= 0
29739 && charpos < SCHARS (obj))
29741 help = Fget_text_property (make_number (charpos),
29742 Qhelp_echo, obj);
29743 if (NILP (help))
29745 /* If the string itself doesn't specify a help-echo,
29746 see if the buffer text ``under'' it does. */
29747 struct glyph_row *r
29748 = MATRIX_ROW (w->current_matrix, vpos);
29749 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
29750 ptrdiff_t p = string_buffer_position (obj, start);
29751 if (p > 0)
29753 help = Fget_char_property (make_number (p),
29754 Qhelp_echo, w->contents);
29755 if (!NILP (help))
29757 charpos = p;
29758 obj = w->contents;
29763 else if (BUFFERP (obj)
29764 && charpos >= BEGV
29765 && charpos < ZV)
29766 help = Fget_text_property (make_number (charpos), Qhelp_echo,
29767 obj);
29769 if (!NILP (help))
29771 help_echo_string = help;
29772 help_echo_window = window;
29773 help_echo_object = obj;
29774 help_echo_pos = charpos;
29779 #ifdef HAVE_WINDOW_SYSTEM
29780 /* Look for a `pointer' property. */
29781 if (FRAME_WINDOW_P (f) && NILP (pointer))
29783 /* Check overlays first. */
29784 for (i = noverlays - 1; i >= 0 && NILP (pointer); --i)
29785 pointer = Foverlay_get (overlay_vec[i], Qpointer);
29787 if (NILP (pointer))
29789 Lisp_Object obj = glyph->object;
29790 ptrdiff_t charpos = glyph->charpos;
29792 /* Try text properties. */
29793 if (STRINGP (obj)
29794 && charpos >= 0
29795 && charpos < SCHARS (obj))
29797 pointer = Fget_text_property (make_number (charpos),
29798 Qpointer, obj);
29799 if (NILP (pointer))
29801 /* If the string itself doesn't specify a pointer,
29802 see if the buffer text ``under'' it does. */
29803 struct glyph_row *r
29804 = MATRIX_ROW (w->current_matrix, vpos);
29805 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
29806 ptrdiff_t p = string_buffer_position (obj, start);
29807 if (p > 0)
29808 pointer = Fget_char_property (make_number (p),
29809 Qpointer, w->contents);
29812 else if (BUFFERP (obj)
29813 && charpos >= BEGV
29814 && charpos < ZV)
29815 pointer = Fget_text_property (make_number (charpos),
29816 Qpointer, obj);
29819 #endif /* HAVE_WINDOW_SYSTEM */
29821 BEGV = obegv;
29822 ZV = ozv;
29823 current_buffer = obuf;
29824 SAFE_FREE ();
29827 set_cursor:
29829 #ifdef HAVE_WINDOW_SYSTEM
29830 if (FRAME_WINDOW_P (f))
29831 define_frame_cursor1 (f, cursor, pointer);
29832 #else
29833 /* This is here to prevent a compiler error, about "label at end of
29834 compound statement". */
29835 return;
29836 #endif
29840 /* EXPORT for RIF:
29841 Clear any mouse-face on window W. This function is part of the
29842 redisplay interface, and is called from try_window_id and similar
29843 functions to ensure the mouse-highlight is off. */
29845 void
29846 x_clear_window_mouse_face (struct window *w)
29848 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (XFRAME (w->frame));
29849 Lisp_Object window;
29851 block_input ();
29852 XSETWINDOW (window, w);
29853 if (EQ (window, hlinfo->mouse_face_window))
29854 clear_mouse_face (hlinfo);
29855 unblock_input ();
29859 /* EXPORT:
29860 Just discard the mouse face information for frame F, if any.
29861 This is used when the size of F is changed. */
29863 void
29864 cancel_mouse_face (struct frame *f)
29866 Lisp_Object window;
29867 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
29869 window = hlinfo->mouse_face_window;
29870 if (! NILP (window) && XFRAME (XWINDOW (window)->frame) == f)
29871 reset_mouse_highlight (hlinfo);
29876 /***********************************************************************
29877 Exposure Events
29878 ***********************************************************************/
29880 #ifdef HAVE_WINDOW_SYSTEM
29882 /* Redraw the part of glyph row area AREA of glyph row ROW on window W
29883 which intersects rectangle R. R is in window-relative coordinates. */
29885 static void
29886 expose_area (struct window *w, struct glyph_row *row, XRectangle *r,
29887 enum glyph_row_area area)
29889 struct glyph *first = row->glyphs[area];
29890 struct glyph *end = row->glyphs[area] + row->used[area];
29891 struct glyph *last;
29892 int first_x, start_x, x;
29894 if (area == TEXT_AREA && row->fill_line_p)
29895 /* If row extends face to end of line write the whole line. */
29896 draw_glyphs (w, 0, row, area,
29897 0, row->used[area],
29898 DRAW_NORMAL_TEXT, 0);
29899 else
29901 /* Set START_X to the window-relative start position for drawing glyphs of
29902 AREA. The first glyph of the text area can be partially visible.
29903 The first glyphs of other areas cannot. */
29904 start_x = window_box_left_offset (w, area);
29905 x = start_x;
29906 if (area == TEXT_AREA)
29907 x += row->x;
29909 /* Find the first glyph that must be redrawn. */
29910 while (first < end
29911 && x + first->pixel_width < r->x)
29913 x += first->pixel_width;
29914 ++first;
29917 /* Find the last one. */
29918 last = first;
29919 first_x = x;
29920 while (last < end
29921 && x < r->x + r->width)
29923 x += last->pixel_width;
29924 ++last;
29927 /* Repaint. */
29928 if (last > first)
29929 draw_glyphs (w, first_x - start_x, row, area,
29930 first - row->glyphs[area], last - row->glyphs[area],
29931 DRAW_NORMAL_TEXT, 0);
29936 /* Redraw the parts of the glyph row ROW on window W intersecting
29937 rectangle R. R is in window-relative coordinates. Value is
29938 non-zero if mouse-face was overwritten. */
29940 static int
29941 expose_line (struct window *w, struct glyph_row *row, XRectangle *r)
29943 eassert (row->enabled_p);
29945 if (row->mode_line_p || w->pseudo_window_p)
29946 draw_glyphs (w, 0, row, TEXT_AREA,
29947 0, row->used[TEXT_AREA],
29948 DRAW_NORMAL_TEXT, 0);
29949 else
29951 if (row->used[LEFT_MARGIN_AREA])
29952 expose_area (w, row, r, LEFT_MARGIN_AREA);
29953 if (row->used[TEXT_AREA])
29954 expose_area (w, row, r, TEXT_AREA);
29955 if (row->used[RIGHT_MARGIN_AREA])
29956 expose_area (w, row, r, RIGHT_MARGIN_AREA);
29957 draw_row_fringe_bitmaps (w, row);
29960 return row->mouse_face_p;
29964 /* Redraw those parts of glyphs rows during expose event handling that
29965 overlap other rows. Redrawing of an exposed line writes over parts
29966 of lines overlapping that exposed line; this function fixes that.
29968 W is the window being exposed. FIRST_OVERLAPPING_ROW is the first
29969 row in W's current matrix that is exposed and overlaps other rows.
29970 LAST_OVERLAPPING_ROW is the last such row. */
29972 static void
29973 expose_overlaps (struct window *w,
29974 struct glyph_row *first_overlapping_row,
29975 struct glyph_row *last_overlapping_row,
29976 XRectangle *r)
29978 struct glyph_row *row;
29980 for (row = first_overlapping_row; row <= last_overlapping_row; ++row)
29981 if (row->overlapping_p)
29983 eassert (row->enabled_p && !row->mode_line_p);
29985 row->clip = r;
29986 if (row->used[LEFT_MARGIN_AREA])
29987 x_fix_overlapping_area (w, row, LEFT_MARGIN_AREA, OVERLAPS_BOTH);
29989 if (row->used[TEXT_AREA])
29990 x_fix_overlapping_area (w, row, TEXT_AREA, OVERLAPS_BOTH);
29992 if (row->used[RIGHT_MARGIN_AREA])
29993 x_fix_overlapping_area (w, row, RIGHT_MARGIN_AREA, OVERLAPS_BOTH);
29994 row->clip = NULL;
29999 /* Return non-zero if W's cursor intersects rectangle R. */
30001 static int
30002 phys_cursor_in_rect_p (struct window *w, XRectangle *r)
30004 XRectangle cr, result;
30005 struct glyph *cursor_glyph;
30006 struct glyph_row *row;
30008 if (w->phys_cursor.vpos >= 0
30009 && w->phys_cursor.vpos < w->current_matrix->nrows
30010 && (row = MATRIX_ROW (w->current_matrix, w->phys_cursor.vpos),
30011 row->enabled_p)
30012 && row->cursor_in_fringe_p)
30014 /* Cursor is in the fringe. */
30015 cr.x = window_box_right_offset (w,
30016 (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
30017 ? RIGHT_MARGIN_AREA
30018 : TEXT_AREA));
30019 cr.y = row->y;
30020 cr.width = WINDOW_RIGHT_FRINGE_WIDTH (w);
30021 cr.height = row->height;
30022 return x_intersect_rectangles (&cr, r, &result);
30025 cursor_glyph = get_phys_cursor_glyph (w);
30026 if (cursor_glyph)
30028 /* r is relative to W's box, but w->phys_cursor.x is relative
30029 to left edge of W's TEXT area. Adjust it. */
30030 cr.x = window_box_left_offset (w, TEXT_AREA) + w->phys_cursor.x;
30031 cr.y = w->phys_cursor.y;
30032 cr.width = cursor_glyph->pixel_width;
30033 cr.height = w->phys_cursor_height;
30034 /* ++KFS: W32 version used W32-specific IntersectRect here, but
30035 I assume the effect is the same -- and this is portable. */
30036 return x_intersect_rectangles (&cr, r, &result);
30038 /* If we don't understand the format, pretend we're not in the hot-spot. */
30039 return 0;
30043 /* EXPORT:
30044 Draw a vertical window border to the right of window W if W doesn't
30045 have vertical scroll bars. */
30047 void
30048 x_draw_vertical_border (struct window *w)
30050 struct frame *f = XFRAME (WINDOW_FRAME (w));
30052 /* We could do better, if we knew what type of scroll-bar the adjacent
30053 windows (on either side) have... But we don't :-(
30054 However, I think this works ok. ++KFS 2003-04-25 */
30056 /* Redraw borders between horizontally adjacent windows. Don't
30057 do it for frames with vertical scroll bars because either the
30058 right scroll bar of a window, or the left scroll bar of its
30059 neighbor will suffice as a border. */
30060 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f) || FRAME_RIGHT_DIVIDER_WIDTH (f))
30061 return;
30063 /* Note: It is necessary to redraw both the left and the right
30064 borders, for when only this single window W is being
30065 redisplayed. */
30066 if (!WINDOW_RIGHTMOST_P (w)
30067 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w))
30069 int x0, x1, y0, y1;
30071 window_box_edges (w, &x0, &y0, &x1, &y1);
30072 y1 -= 1;
30074 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
30075 x1 -= 1;
30077 FRAME_RIF (f)->draw_vertical_window_border (w, x1, y0, y1);
30080 if (!WINDOW_LEFTMOST_P (w)
30081 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w))
30083 int x0, x1, y0, y1;
30085 window_box_edges (w, &x0, &y0, &x1, &y1);
30086 y1 -= 1;
30088 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
30089 x0 -= 1;
30091 FRAME_RIF (f)->draw_vertical_window_border (w, x0, y0, y1);
30096 /* Draw window dividers for window W. */
30098 void
30099 x_draw_right_divider (struct window *w)
30101 struct frame *f = WINDOW_XFRAME (w);
30103 if (w->mini || w->pseudo_window_p)
30104 return;
30105 else if (WINDOW_RIGHT_DIVIDER_WIDTH (w))
30107 int x0 = WINDOW_RIGHT_EDGE_X (w) - WINDOW_RIGHT_DIVIDER_WIDTH (w);
30108 int x1 = WINDOW_RIGHT_EDGE_X (w);
30109 int y0 = WINDOW_TOP_EDGE_Y (w);
30110 /* The bottom divider prevails. */
30111 int y1 = WINDOW_BOTTOM_EDGE_Y (w) - WINDOW_BOTTOM_DIVIDER_WIDTH (w);
30113 FRAME_RIF (f)->draw_window_divider (w, x0, x1, y0, y1);
30117 static void
30118 x_draw_bottom_divider (struct window *w)
30120 struct frame *f = XFRAME (WINDOW_FRAME (w));
30122 if (w->mini || w->pseudo_window_p)
30123 return;
30124 else if (WINDOW_BOTTOM_DIVIDER_WIDTH (w))
30126 int x0 = WINDOW_LEFT_EDGE_X (w);
30127 int x1 = WINDOW_RIGHT_EDGE_X (w);
30128 int y0 = WINDOW_BOTTOM_EDGE_Y (w) - WINDOW_BOTTOM_DIVIDER_WIDTH (w);
30129 int y1 = WINDOW_BOTTOM_EDGE_Y (w);
30131 FRAME_RIF (f)->draw_window_divider (w, x0, x1, y0, y1);
30135 /* Redraw the part of window W intersection rectangle FR. Pixel
30136 coordinates in FR are frame-relative. Call this function with
30137 input blocked. Value is non-zero if the exposure overwrites
30138 mouse-face. */
30140 static int
30141 expose_window (struct window *w, XRectangle *fr)
30143 struct frame *f = XFRAME (w->frame);
30144 XRectangle wr, r;
30145 int mouse_face_overwritten_p = 0;
30147 /* If window is not yet fully initialized, do nothing. This can
30148 happen when toolkit scroll bars are used and a window is split.
30149 Reconfiguring the scroll bar will generate an expose for a newly
30150 created window. */
30151 if (w->current_matrix == NULL)
30152 return 0;
30154 /* When we're currently updating the window, display and current
30155 matrix usually don't agree. Arrange for a thorough display
30156 later. */
30157 if (w->must_be_updated_p)
30159 SET_FRAME_GARBAGED (f);
30160 return 0;
30163 /* Frame-relative pixel rectangle of W. */
30164 wr.x = WINDOW_LEFT_EDGE_X (w);
30165 wr.y = WINDOW_TOP_EDGE_Y (w);
30166 wr.width = WINDOW_PIXEL_WIDTH (w);
30167 wr.height = WINDOW_PIXEL_HEIGHT (w);
30169 if (x_intersect_rectangles (fr, &wr, &r))
30171 int yb = window_text_bottom_y (w);
30172 struct glyph_row *row;
30173 int cursor_cleared_p, phys_cursor_on_p;
30174 struct glyph_row *first_overlapping_row, *last_overlapping_row;
30176 TRACE ((stderr, "expose_window (%d, %d, %d, %d)\n",
30177 r.x, r.y, r.width, r.height));
30179 /* Convert to window coordinates. */
30180 r.x -= WINDOW_LEFT_EDGE_X (w);
30181 r.y -= WINDOW_TOP_EDGE_Y (w);
30183 /* Turn off the cursor. */
30184 if (!w->pseudo_window_p
30185 && phys_cursor_in_rect_p (w, &r))
30187 x_clear_cursor (w);
30188 cursor_cleared_p = 1;
30190 else
30191 cursor_cleared_p = 0;
30193 /* If the row containing the cursor extends face to end of line,
30194 then expose_area might overwrite the cursor outside the
30195 rectangle and thus notice_overwritten_cursor might clear
30196 w->phys_cursor_on_p. We remember the original value and
30197 check later if it is changed. */
30198 phys_cursor_on_p = w->phys_cursor_on_p;
30200 /* Update lines intersecting rectangle R. */
30201 first_overlapping_row = last_overlapping_row = NULL;
30202 for (row = w->current_matrix->rows;
30203 row->enabled_p;
30204 ++row)
30206 int y0 = row->y;
30207 int y1 = MATRIX_ROW_BOTTOM_Y (row);
30209 if ((y0 >= r.y && y0 < r.y + r.height)
30210 || (y1 > r.y && y1 < r.y + r.height)
30211 || (r.y >= y0 && r.y < y1)
30212 || (r.y + r.height > y0 && r.y + r.height < y1))
30214 /* A header line may be overlapping, but there is no need
30215 to fix overlapping areas for them. KFS 2005-02-12 */
30216 if (row->overlapping_p && !row->mode_line_p)
30218 if (first_overlapping_row == NULL)
30219 first_overlapping_row = row;
30220 last_overlapping_row = row;
30223 row->clip = fr;
30224 if (expose_line (w, row, &r))
30225 mouse_face_overwritten_p = 1;
30226 row->clip = NULL;
30228 else if (row->overlapping_p)
30230 /* We must redraw a row overlapping the exposed area. */
30231 if (y0 < r.y
30232 ? y0 + row->phys_height > r.y
30233 : y0 + row->ascent - row->phys_ascent < r.y +r.height)
30235 if (first_overlapping_row == NULL)
30236 first_overlapping_row = row;
30237 last_overlapping_row = row;
30241 if (y1 >= yb)
30242 break;
30245 /* Display the mode line if there is one. */
30246 if (WINDOW_WANTS_MODELINE_P (w)
30247 && (row = MATRIX_MODE_LINE_ROW (w->current_matrix),
30248 row->enabled_p)
30249 && row->y < r.y + r.height)
30251 if (expose_line (w, row, &r))
30252 mouse_face_overwritten_p = 1;
30255 if (!w->pseudo_window_p)
30257 /* Fix the display of overlapping rows. */
30258 if (first_overlapping_row)
30259 expose_overlaps (w, first_overlapping_row, last_overlapping_row,
30260 fr);
30262 /* Draw border between windows. */
30263 if (WINDOW_RIGHT_DIVIDER_WIDTH (w))
30264 x_draw_right_divider (w);
30265 else
30266 x_draw_vertical_border (w);
30268 if (WINDOW_BOTTOM_DIVIDER_WIDTH (w))
30269 x_draw_bottom_divider (w);
30271 /* Turn the cursor on again. */
30272 if (cursor_cleared_p
30273 || (phys_cursor_on_p && !w->phys_cursor_on_p))
30274 update_window_cursor (w, 1);
30278 return mouse_face_overwritten_p;
30283 /* Redraw (parts) of all windows in the window tree rooted at W that
30284 intersect R. R contains frame pixel coordinates. Value is
30285 non-zero if the exposure overwrites mouse-face. */
30287 static int
30288 expose_window_tree (struct window *w, XRectangle *r)
30290 struct frame *f = XFRAME (w->frame);
30291 int mouse_face_overwritten_p = 0;
30293 while (w && !FRAME_GARBAGED_P (f))
30295 if (WINDOWP (w->contents))
30296 mouse_face_overwritten_p
30297 |= expose_window_tree (XWINDOW (w->contents), r);
30298 else
30299 mouse_face_overwritten_p |= expose_window (w, r);
30301 w = NILP (w->next) ? NULL : XWINDOW (w->next);
30304 return mouse_face_overwritten_p;
30308 /* EXPORT:
30309 Redisplay an exposed area of frame F. X and Y are the upper-left
30310 corner of the exposed rectangle. W and H are width and height of
30311 the exposed area. All are pixel values. W or H zero means redraw
30312 the entire frame. */
30314 void
30315 expose_frame (struct frame *f, int x, int y, int w, int h)
30317 XRectangle r;
30318 int mouse_face_overwritten_p = 0;
30320 TRACE ((stderr, "expose_frame "));
30322 /* No need to redraw if frame will be redrawn soon. */
30323 if (FRAME_GARBAGED_P (f))
30325 TRACE ((stderr, " garbaged\n"));
30326 return;
30329 /* If basic faces haven't been realized yet, there is no point in
30330 trying to redraw anything. This can happen when we get an expose
30331 event while Emacs is starting, e.g. by moving another window. */
30332 if (FRAME_FACE_CACHE (f) == NULL
30333 || FRAME_FACE_CACHE (f)->used < BASIC_FACE_ID_SENTINEL)
30335 TRACE ((stderr, " no faces\n"));
30336 return;
30339 if (w == 0 || h == 0)
30341 r.x = r.y = 0;
30342 r.width = FRAME_TEXT_WIDTH (f);
30343 r.height = FRAME_TEXT_HEIGHT (f);
30345 else
30347 r.x = x;
30348 r.y = y;
30349 r.width = w;
30350 r.height = h;
30353 TRACE ((stderr, "(%d, %d, %d, %d)\n", r.x, r.y, r.width, r.height));
30354 mouse_face_overwritten_p = expose_window_tree (XWINDOW (f->root_window), &r);
30356 #if ! defined (USE_GTK) && ! defined (HAVE_NS)
30357 if (WINDOWP (f->tool_bar_window))
30358 mouse_face_overwritten_p
30359 |= expose_window (XWINDOW (f->tool_bar_window), &r);
30360 #endif
30362 #ifdef HAVE_X_WINDOWS
30363 #ifndef MSDOS
30364 #if ! defined (USE_X_TOOLKIT) && ! defined (USE_GTK)
30365 if (WINDOWP (f->menu_bar_window))
30366 mouse_face_overwritten_p
30367 |= expose_window (XWINDOW (f->menu_bar_window), &r);
30368 #endif /* not USE_X_TOOLKIT and not USE_GTK */
30369 #endif
30370 #endif
30372 /* Some window managers support a focus-follows-mouse style with
30373 delayed raising of frames. Imagine a partially obscured frame,
30374 and moving the mouse into partially obscured mouse-face on that
30375 frame. The visible part of the mouse-face will be highlighted,
30376 then the WM raises the obscured frame. With at least one WM, KDE
30377 2.1, Emacs is not getting any event for the raising of the frame
30378 (even tried with SubstructureRedirectMask), only Expose events.
30379 These expose events will draw text normally, i.e. not
30380 highlighted. Which means we must redo the highlight here.
30381 Subsume it under ``we love X''. --gerd 2001-08-15 */
30382 /* Included in Windows version because Windows most likely does not
30383 do the right thing if any third party tool offers
30384 focus-follows-mouse with delayed raise. --jason 2001-10-12 */
30385 if (mouse_face_overwritten_p && !FRAME_GARBAGED_P (f))
30387 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
30388 if (f == hlinfo->mouse_face_mouse_frame)
30390 int mouse_x = hlinfo->mouse_face_mouse_x;
30391 int mouse_y = hlinfo->mouse_face_mouse_y;
30392 clear_mouse_face (hlinfo);
30393 note_mouse_highlight (f, mouse_x, mouse_y);
30399 /* EXPORT:
30400 Determine the intersection of two rectangles R1 and R2. Return
30401 the intersection in *RESULT. Value is non-zero if RESULT is not
30402 empty. */
30405 x_intersect_rectangles (XRectangle *r1, XRectangle *r2, XRectangle *result)
30407 XRectangle *left, *right;
30408 XRectangle *upper, *lower;
30409 int intersection_p = 0;
30411 /* Rearrange so that R1 is the left-most rectangle. */
30412 if (r1->x < r2->x)
30413 left = r1, right = r2;
30414 else
30415 left = r2, right = r1;
30417 /* X0 of the intersection is right.x0, if this is inside R1,
30418 otherwise there is no intersection. */
30419 if (right->x <= left->x + left->width)
30421 result->x = right->x;
30423 /* The right end of the intersection is the minimum of
30424 the right ends of left and right. */
30425 result->width = (min (left->x + left->width, right->x + right->width)
30426 - result->x);
30428 /* Same game for Y. */
30429 if (r1->y < r2->y)
30430 upper = r1, lower = r2;
30431 else
30432 upper = r2, lower = r1;
30434 /* The upper end of the intersection is lower.y0, if this is inside
30435 of upper. Otherwise, there is no intersection. */
30436 if (lower->y <= upper->y + upper->height)
30438 result->y = lower->y;
30440 /* The lower end of the intersection is the minimum of the lower
30441 ends of upper and lower. */
30442 result->height = (min (lower->y + lower->height,
30443 upper->y + upper->height)
30444 - result->y);
30445 intersection_p = 1;
30449 return intersection_p;
30452 #endif /* HAVE_WINDOW_SYSTEM */
30455 /***********************************************************************
30456 Initialization
30457 ***********************************************************************/
30459 void
30460 syms_of_xdisp (void)
30462 Vwith_echo_area_save_vector = Qnil;
30463 staticpro (&Vwith_echo_area_save_vector);
30465 Vmessage_stack = Qnil;
30466 staticpro (&Vmessage_stack);
30468 /* Non-nil means don't actually do any redisplay. */
30469 DEFSYM (Qinhibit_redisplay, "inhibit-redisplay");
30471 DEFSYM (Qredisplay_internal, "redisplay_internal (C function)");
30473 message_dolog_marker1 = Fmake_marker ();
30474 staticpro (&message_dolog_marker1);
30475 message_dolog_marker2 = Fmake_marker ();
30476 staticpro (&message_dolog_marker2);
30477 message_dolog_marker3 = Fmake_marker ();
30478 staticpro (&message_dolog_marker3);
30480 #ifdef GLYPH_DEBUG
30481 defsubr (&Sdump_frame_glyph_matrix);
30482 defsubr (&Sdump_glyph_matrix);
30483 defsubr (&Sdump_glyph_row);
30484 defsubr (&Sdump_tool_bar_row);
30485 defsubr (&Strace_redisplay);
30486 defsubr (&Strace_to_stderr);
30487 #endif
30488 #ifdef HAVE_WINDOW_SYSTEM
30489 defsubr (&Stool_bar_height);
30490 defsubr (&Slookup_image_map);
30491 #endif
30492 defsubr (&Sline_pixel_height);
30493 defsubr (&Sformat_mode_line);
30494 defsubr (&Sinvisible_p);
30495 defsubr (&Scurrent_bidi_paragraph_direction);
30496 defsubr (&Swindow_text_pixel_size);
30497 defsubr (&Smove_point_visually);
30498 defsubr (&Sbidi_find_overridden_directionality);
30500 DEFSYM (Qmenu_bar_update_hook, "menu-bar-update-hook");
30501 DEFSYM (Qoverriding_terminal_local_map, "overriding-terminal-local-map");
30502 DEFSYM (Qoverriding_local_map, "overriding-local-map");
30503 DEFSYM (Qwindow_scroll_functions, "window-scroll-functions");
30504 DEFSYM (Qwindow_text_change_functions, "window-text-change-functions");
30505 DEFSYM (Qredisplay_end_trigger_functions, "redisplay-end-trigger-functions");
30506 DEFSYM (Qinhibit_point_motion_hooks, "inhibit-point-motion-hooks");
30507 DEFSYM (Qeval, "eval");
30508 DEFSYM (QCdata, ":data");
30510 /* Names of text properties relevant for redisplay. */
30511 DEFSYM (Qdisplay, "display");
30512 DEFSYM (Qspace_width, "space-width");
30513 DEFSYM (Qraise, "raise");
30514 DEFSYM (Qslice, "slice");
30515 DEFSYM (Qspace, "space");
30516 DEFSYM (Qmargin, "margin");
30517 DEFSYM (Qpointer, "pointer");
30518 DEFSYM (Qleft_margin, "left-margin");
30519 DEFSYM (Qright_margin, "right-margin");
30520 DEFSYM (Qcenter, "center");
30521 DEFSYM (Qline_height, "line-height");
30522 DEFSYM (QCalign_to, ":align-to");
30523 DEFSYM (QCrelative_width, ":relative-width");
30524 DEFSYM (QCrelative_height, ":relative-height");
30525 DEFSYM (QCeval, ":eval");
30526 DEFSYM (QCpropertize, ":propertize");
30527 DEFSYM (QCfile, ":file");
30528 DEFSYM (Qfontified, "fontified");
30529 DEFSYM (Qfontification_functions, "fontification-functions");
30531 /* Name of the face used to highlight trailing whitespace. */
30532 DEFSYM (Qtrailing_whitespace, "trailing-whitespace");
30534 /* Name and number of the face used to highlight escape glyphs. */
30535 DEFSYM (Qescape_glyph, "escape-glyph");
30537 /* Name and number of the face used to highlight non-breaking spaces. */
30538 DEFSYM (Qnobreak_space, "nobreak-space");
30540 /* The symbol 'image' which is the car of the lists used to represent
30541 images in Lisp. Also a tool bar style. */
30542 DEFSYM (Qimage, "image");
30544 /* Tool bar styles. */
30545 DEFSYM (Qtext, "text");
30546 DEFSYM (Qboth, "both");
30547 DEFSYM (Qboth_horiz, "both-horiz");
30548 DEFSYM (Qtext_image_horiz, "text-image-horiz");
30550 /* The image map types. */
30551 DEFSYM (QCmap, ":map");
30552 DEFSYM (QCpointer, ":pointer");
30553 DEFSYM (Qrect, "rect");
30554 DEFSYM (Qcircle, "circle");
30555 DEFSYM (Qpoly, "poly");
30557 /* The symbol `inhibit-menubar-update' and its DEFVAR_BOOL variable. */
30558 DEFSYM (Qinhibit_menubar_update, "inhibit-menubar-update");
30559 DEFSYM (Qmessage_truncate_lines, "message-truncate-lines");
30561 DEFSYM (Qgrow_only, "grow-only");
30562 DEFSYM (Qinhibit_eval_during_redisplay, "inhibit-eval-during-redisplay");
30563 DEFSYM (Qposition, "position");
30564 DEFSYM (Qbuffer_position, "buffer-position");
30565 DEFSYM (Qobject, "object");
30567 /* Cursor shapes. */
30568 DEFSYM (Qbar, "bar");
30569 DEFSYM (Qhbar, "hbar");
30570 DEFSYM (Qbox, "box");
30571 DEFSYM (Qhollow, "hollow");
30573 /* Pointer shapes. */
30574 DEFSYM (Qhand, "hand");
30575 DEFSYM (Qarrow, "arrow");
30576 /* also Qtext */
30578 DEFSYM (Qinhibit_free_realized_faces, "inhibit-free-realized-faces");
30580 list_of_error = list1 (list2 (intern_c_string ("error"),
30581 intern_c_string ("void-variable")));
30582 staticpro (&list_of_error);
30584 /* Values of those variables at last redisplay are stored as
30585 properties on 'overlay-arrow-position' symbol. However, if
30586 Voverlay_arrow_position is a marker, last-arrow-position is its
30587 numerical position. */
30588 DEFSYM (Qlast_arrow_position, "last-arrow-position");
30589 DEFSYM (Qlast_arrow_string, "last-arrow-string");
30591 /* Alternative overlay-arrow-string and overlay-arrow-bitmap
30592 properties on a symbol in overlay-arrow-variable-list. */
30593 DEFSYM (Qoverlay_arrow_string, "overlay-arrow-string");
30594 DEFSYM (Qoverlay_arrow_bitmap, "overlay-arrow-bitmap");
30596 echo_buffer[0] = echo_buffer[1] = Qnil;
30597 staticpro (&echo_buffer[0]);
30598 staticpro (&echo_buffer[1]);
30600 echo_area_buffer[0] = echo_area_buffer[1] = Qnil;
30601 staticpro (&echo_area_buffer[0]);
30602 staticpro (&echo_area_buffer[1]);
30604 Vmessages_buffer_name = build_pure_c_string ("*Messages*");
30605 staticpro (&Vmessages_buffer_name);
30607 mode_line_proptrans_alist = Qnil;
30608 staticpro (&mode_line_proptrans_alist);
30609 mode_line_string_list = Qnil;
30610 staticpro (&mode_line_string_list);
30611 mode_line_string_face = Qnil;
30612 staticpro (&mode_line_string_face);
30613 mode_line_string_face_prop = Qnil;
30614 staticpro (&mode_line_string_face_prop);
30615 Vmode_line_unwind_vector = Qnil;
30616 staticpro (&Vmode_line_unwind_vector);
30618 DEFSYM (Qmode_line_default_help_echo, "mode-line-default-help-echo");
30620 help_echo_string = Qnil;
30621 staticpro (&help_echo_string);
30622 help_echo_object = Qnil;
30623 staticpro (&help_echo_object);
30624 help_echo_window = Qnil;
30625 staticpro (&help_echo_window);
30626 previous_help_echo_string = Qnil;
30627 staticpro (&previous_help_echo_string);
30628 help_echo_pos = -1;
30630 DEFSYM (Qright_to_left, "right-to-left");
30631 DEFSYM (Qleft_to_right, "left-to-right");
30632 defsubr (&Sbidi_resolved_levels);
30634 #ifdef HAVE_WINDOW_SYSTEM
30635 DEFVAR_BOOL ("x-stretch-cursor", x_stretch_cursor_p,
30636 doc: /* Non-nil means draw block cursor as wide as the glyph under it.
30637 For example, if a block cursor is over a tab, it will be drawn as
30638 wide as that tab on the display. */);
30639 x_stretch_cursor_p = 0;
30640 #endif
30642 DEFVAR_LISP ("show-trailing-whitespace", Vshow_trailing_whitespace,
30643 doc: /* Non-nil means highlight trailing whitespace.
30644 The face used for trailing whitespace is `trailing-whitespace'. */);
30645 Vshow_trailing_whitespace = Qnil;
30647 DEFVAR_LISP ("nobreak-char-display", Vnobreak_char_display,
30648 doc: /* Control highlighting of non-ASCII space and hyphen chars.
30649 If the value is t, Emacs highlights non-ASCII chars which have the
30650 same appearance as an ASCII space or hyphen, using the `nobreak-space'
30651 or `escape-glyph' face respectively.
30653 U+00A0 (no-break space), U+00AD (soft hyphen), U+2010 (hyphen), and
30654 U+2011 (non-breaking hyphen) are affected.
30656 Any other non-nil value means to display these characters as a escape
30657 glyph followed by an ordinary space or hyphen.
30659 A value of nil means no special handling of these characters. */);
30660 Vnobreak_char_display = Qt;
30662 DEFVAR_LISP ("void-text-area-pointer", Vvoid_text_area_pointer,
30663 doc: /* The pointer shape to show in void text areas.
30664 A value of nil means to show the text pointer. Other options are
30665 `arrow', `text', `hand', `vdrag', `hdrag', `nhdrag', `modeline', and
30666 `hourglass'. */);
30667 Vvoid_text_area_pointer = Qarrow;
30669 DEFVAR_LISP ("inhibit-redisplay", Vinhibit_redisplay,
30670 doc: /* Non-nil means don't actually do any redisplay.
30671 This is used for internal purposes. */);
30672 Vinhibit_redisplay = Qnil;
30674 DEFVAR_LISP ("global-mode-string", Vglobal_mode_string,
30675 doc: /* String (or mode line construct) included (normally) in `mode-line-format'. */);
30676 Vglobal_mode_string = Qnil;
30678 DEFVAR_LISP ("overlay-arrow-position", Voverlay_arrow_position,
30679 doc: /* Marker for where to display an arrow on top of the buffer text.
30680 This must be the beginning of a line in order to work.
30681 See also `overlay-arrow-string'. */);
30682 Voverlay_arrow_position = Qnil;
30684 DEFVAR_LISP ("overlay-arrow-string", Voverlay_arrow_string,
30685 doc: /* String to display as an arrow in non-window frames.
30686 See also `overlay-arrow-position'. */);
30687 Voverlay_arrow_string = build_pure_c_string ("=>");
30689 DEFVAR_LISP ("overlay-arrow-variable-list", Voverlay_arrow_variable_list,
30690 doc: /* List of variables (symbols) which hold markers for overlay arrows.
30691 The symbols on this list are examined during redisplay to determine
30692 where to display overlay arrows. */);
30693 Voverlay_arrow_variable_list
30694 = list1 (intern_c_string ("overlay-arrow-position"));
30696 DEFVAR_INT ("scroll-step", emacs_scroll_step,
30697 doc: /* The number of lines to try scrolling a window by when point moves out.
30698 If that fails to bring point back on frame, point is centered instead.
30699 If this is zero, point is always centered after it moves off frame.
30700 If you want scrolling to always be a line at a time, you should set
30701 `scroll-conservatively' to a large value rather than set this to 1. */);
30703 DEFVAR_INT ("scroll-conservatively", scroll_conservatively,
30704 doc: /* Scroll up to this many lines, to bring point back on screen.
30705 If point moves off-screen, redisplay will scroll by up to
30706 `scroll-conservatively' lines in order to bring point just barely
30707 onto the screen again. If that cannot be done, then redisplay
30708 recenters point as usual.
30710 If the value is greater than 100, redisplay will never recenter point,
30711 but will always scroll just enough text to bring point into view, even
30712 if you move far away.
30714 A value of zero means always recenter point if it moves off screen. */);
30715 scroll_conservatively = 0;
30717 DEFVAR_INT ("scroll-margin", scroll_margin,
30718 doc: /* Number of lines of margin at the top and bottom of a window.
30719 Recenter the window whenever point gets within this many lines
30720 of the top or bottom of the window. */);
30721 scroll_margin = 0;
30723 DEFVAR_LISP ("display-pixels-per-inch", Vdisplay_pixels_per_inch,
30724 doc: /* Pixels per inch value for non-window system displays.
30725 Value is a number or a cons (WIDTH-DPI . HEIGHT-DPI). */);
30726 Vdisplay_pixels_per_inch = make_float (72.0);
30728 #ifdef GLYPH_DEBUG
30729 DEFVAR_INT ("debug-end-pos", debug_end_pos, doc: /* Don't ask. */);
30730 #endif
30732 DEFVAR_LISP ("truncate-partial-width-windows",
30733 Vtruncate_partial_width_windows,
30734 doc: /* Non-nil means truncate lines in windows narrower than the frame.
30735 For an integer value, truncate lines in each window narrower than the
30736 full frame width, provided the window width is less than that integer;
30737 otherwise, respect the value of `truncate-lines'.
30739 For any other non-nil value, truncate lines in all windows that do
30740 not span the full frame width.
30742 A value of nil means to respect the value of `truncate-lines'.
30744 If `word-wrap' is enabled, you might want to reduce this. */);
30745 Vtruncate_partial_width_windows = make_number (50);
30747 DEFVAR_LISP ("line-number-display-limit", Vline_number_display_limit,
30748 doc: /* Maximum buffer size for which line number should be displayed.
30749 If the buffer is bigger than this, the line number does not appear
30750 in the mode line. A value of nil means no limit. */);
30751 Vline_number_display_limit = Qnil;
30753 DEFVAR_INT ("line-number-display-limit-width",
30754 line_number_display_limit_width,
30755 doc: /* Maximum line width (in characters) for line number display.
30756 If the average length of the lines near point is bigger than this, then the
30757 line number may be omitted from the mode line. */);
30758 line_number_display_limit_width = 200;
30760 DEFVAR_BOOL ("highlight-nonselected-windows", highlight_nonselected_windows,
30761 doc: /* Non-nil means highlight region even in nonselected windows. */);
30762 highlight_nonselected_windows = 0;
30764 DEFVAR_BOOL ("multiple-frames", multiple_frames,
30765 doc: /* Non-nil if more than one frame is visible on this display.
30766 Minibuffer-only frames don't count, but iconified frames do.
30767 This variable is not guaranteed to be accurate except while processing
30768 `frame-title-format' and `icon-title-format'. */);
30770 DEFVAR_LISP ("frame-title-format", Vframe_title_format,
30771 doc: /* Template for displaying the title bar of visible frames.
30772 \(Assuming the window manager supports this feature.)
30774 This variable has the same structure as `mode-line-format', except that
30775 the %c and %l constructs are ignored. It is used only on frames for
30776 which no explicit name has been set \(see `modify-frame-parameters'). */);
30778 DEFVAR_LISP ("icon-title-format", Vicon_title_format,
30779 doc: /* Template for displaying the title bar of an iconified frame.
30780 \(Assuming the window manager supports this feature.)
30781 This variable has the same structure as `mode-line-format' (which see),
30782 and is used only on frames for which no explicit name has been set
30783 \(see `modify-frame-parameters'). */);
30784 Vicon_title_format
30785 = Vframe_title_format
30786 = listn (CONSTYPE_PURE, 3,
30787 intern_c_string ("multiple-frames"),
30788 build_pure_c_string ("%b"),
30789 listn (CONSTYPE_PURE, 4,
30790 empty_unibyte_string,
30791 intern_c_string ("invocation-name"),
30792 build_pure_c_string ("@"),
30793 intern_c_string ("system-name")));
30795 DEFVAR_LISP ("message-log-max", Vmessage_log_max,
30796 doc: /* Maximum number of lines to keep in the message log buffer.
30797 If nil, disable message logging. If t, log messages but don't truncate
30798 the buffer when it becomes large. */);
30799 Vmessage_log_max = make_number (1000);
30801 DEFVAR_LISP ("window-size-change-functions", Vwindow_size_change_functions,
30802 doc: /* Functions called before redisplay, if window sizes have changed.
30803 The value should be a list of functions that take one argument.
30804 Just before redisplay, for each frame, if any of its windows have changed
30805 size since the last redisplay, or have been split or deleted,
30806 all the functions in the list are called, with the frame as argument. */);
30807 Vwindow_size_change_functions = Qnil;
30809 DEFVAR_LISP ("window-scroll-functions", Vwindow_scroll_functions,
30810 doc: /* List of functions to call before redisplaying a window with scrolling.
30811 Each function is called with two arguments, the window and its new
30812 display-start position.
30813 These functions are called whenever the `window-start' marker is modified,
30814 either to point into another buffer (e.g. via `set-window-buffer') or another
30815 place in the same buffer.
30816 Note that the value of `window-end' is not valid when these functions are
30817 called.
30819 Warning: Do not use this feature to alter the way the window
30820 is scrolled. It is not designed for that, and such use probably won't
30821 work. */);
30822 Vwindow_scroll_functions = Qnil;
30824 DEFVAR_LISP ("window-text-change-functions",
30825 Vwindow_text_change_functions,
30826 doc: /* Functions to call in redisplay when text in the window might change. */);
30827 Vwindow_text_change_functions = Qnil;
30829 DEFVAR_LISP ("redisplay-end-trigger-functions", Vredisplay_end_trigger_functions,
30830 doc: /* Functions called when redisplay of a window reaches the end trigger.
30831 Each function is called with two arguments, the window and the end trigger value.
30832 See `set-window-redisplay-end-trigger'. */);
30833 Vredisplay_end_trigger_functions = Qnil;
30835 DEFVAR_LISP ("mouse-autoselect-window", Vmouse_autoselect_window,
30836 doc: /* Non-nil means autoselect window with mouse pointer.
30837 If nil, do not autoselect windows.
30838 A positive number means delay autoselection by that many seconds: a
30839 window is autoselected only after the mouse has remained in that
30840 window for the duration of the delay.
30841 A negative number has a similar effect, but causes windows to be
30842 autoselected only after the mouse has stopped moving. \(Because of
30843 the way Emacs compares mouse events, you will occasionally wait twice
30844 that time before the window gets selected.\)
30845 Any other value means to autoselect window instantaneously when the
30846 mouse pointer enters it.
30848 Autoselection selects the minibuffer only if it is active, and never
30849 unselects the minibuffer if it is active.
30851 When customizing this variable make sure that the actual value of
30852 `focus-follows-mouse' matches the behavior of your window manager. */);
30853 Vmouse_autoselect_window = Qnil;
30855 DEFVAR_LISP ("auto-resize-tool-bars", Vauto_resize_tool_bars,
30856 doc: /* Non-nil means automatically resize tool-bars.
30857 This dynamically changes the tool-bar's height to the minimum height
30858 that is needed to make all tool-bar items visible.
30859 If value is `grow-only', the tool-bar's height is only increased
30860 automatically; to decrease the tool-bar height, use \\[recenter]. */);
30861 Vauto_resize_tool_bars = Qt;
30863 DEFVAR_BOOL ("auto-raise-tool-bar-buttons", auto_raise_tool_bar_buttons_p,
30864 doc: /* Non-nil means raise tool-bar buttons when the mouse moves over them. */);
30865 auto_raise_tool_bar_buttons_p = 1;
30867 DEFVAR_BOOL ("make-cursor-line-fully-visible", make_cursor_line_fully_visible_p,
30868 doc: /* Non-nil means to scroll (recenter) cursor line if it is not fully visible. */);
30869 make_cursor_line_fully_visible_p = 1;
30871 DEFVAR_LISP ("tool-bar-border", Vtool_bar_border,
30872 doc: /* Border below tool-bar in pixels.
30873 If an integer, use it as the height of the border.
30874 If it is one of `internal-border-width' or `border-width', use the
30875 value of the corresponding frame parameter.
30876 Otherwise, no border is added below the tool-bar. */);
30877 Vtool_bar_border = Qinternal_border_width;
30879 DEFVAR_LISP ("tool-bar-button-margin", Vtool_bar_button_margin,
30880 doc: /* Margin around tool-bar buttons in pixels.
30881 If an integer, use that for both horizontal and vertical margins.
30882 Otherwise, value should be a pair of integers `(HORZ . VERT)' with
30883 HORZ specifying the horizontal margin, and VERT specifying the
30884 vertical margin. */);
30885 Vtool_bar_button_margin = make_number (DEFAULT_TOOL_BAR_BUTTON_MARGIN);
30887 DEFVAR_INT ("tool-bar-button-relief", tool_bar_button_relief,
30888 doc: /* Relief thickness of tool-bar buttons. */);
30889 tool_bar_button_relief = DEFAULT_TOOL_BAR_BUTTON_RELIEF;
30891 DEFVAR_LISP ("tool-bar-style", Vtool_bar_style,
30892 doc: /* Tool bar style to use.
30893 It can be one of
30894 image - show images only
30895 text - show text only
30896 both - show both, text below image
30897 both-horiz - show text to the right of the image
30898 text-image-horiz - show text to the left of the image
30899 any other - use system default or image if no system default.
30901 This variable only affects the GTK+ toolkit version of Emacs. */);
30902 Vtool_bar_style = Qnil;
30904 DEFVAR_INT ("tool-bar-max-label-size", tool_bar_max_label_size,
30905 doc: /* Maximum number of characters a label can have to be shown.
30906 The tool bar style must also show labels for this to have any effect, see
30907 `tool-bar-style'. */);
30908 tool_bar_max_label_size = DEFAULT_TOOL_BAR_LABEL_SIZE;
30910 DEFVAR_LISP ("fontification-functions", Vfontification_functions,
30911 doc: /* List of functions to call to fontify regions of text.
30912 Each function is called with one argument POS. Functions must
30913 fontify a region starting at POS in the current buffer, and give
30914 fontified regions the property `fontified'. */);
30915 Vfontification_functions = Qnil;
30916 Fmake_variable_buffer_local (Qfontification_functions);
30918 DEFVAR_BOOL ("unibyte-display-via-language-environment",
30919 unibyte_display_via_language_environment,
30920 doc: /* Non-nil means display unibyte text according to language environment.
30921 Specifically, this means that raw bytes in the range 160-255 decimal
30922 are displayed by converting them to the equivalent multibyte characters
30923 according to the current language environment. As a result, they are
30924 displayed according to the current fontset.
30926 Note that this variable affects only how these bytes are displayed,
30927 but does not change the fact they are interpreted as raw bytes. */);
30928 unibyte_display_via_language_environment = 0;
30930 DEFVAR_LISP ("max-mini-window-height", Vmax_mini_window_height,
30931 doc: /* Maximum height for resizing mini-windows (the minibuffer and the echo area).
30932 If a float, it specifies a fraction of the mini-window frame's height.
30933 If an integer, it specifies a number of lines. */);
30934 Vmax_mini_window_height = make_float (0.25);
30936 DEFVAR_LISP ("resize-mini-windows", Vresize_mini_windows,
30937 doc: /* How to resize mini-windows (the minibuffer and the echo area).
30938 A value of nil means don't automatically resize mini-windows.
30939 A value of t means resize them to fit the text displayed in them.
30940 A value of `grow-only', the default, means let mini-windows grow only;
30941 they return to their normal size when the minibuffer is closed, or the
30942 echo area becomes empty. */);
30943 Vresize_mini_windows = Qgrow_only;
30945 DEFVAR_LISP ("blink-cursor-alist", Vblink_cursor_alist,
30946 doc: /* Alist specifying how to blink the cursor off.
30947 Each element has the form (ON-STATE . OFF-STATE). Whenever the
30948 `cursor-type' frame-parameter or variable equals ON-STATE,
30949 comparing using `equal', Emacs uses OFF-STATE to specify
30950 how to blink it off. ON-STATE and OFF-STATE are values for
30951 the `cursor-type' frame parameter.
30953 If a frame's ON-STATE has no entry in this list,
30954 the frame's other specifications determine how to blink the cursor off. */);
30955 Vblink_cursor_alist = Qnil;
30957 DEFVAR_BOOL ("auto-hscroll-mode", automatic_hscrolling_p,
30958 doc: /* Allow or disallow automatic horizontal scrolling of windows.
30959 If non-nil, windows are automatically scrolled horizontally to make
30960 point visible. */);
30961 automatic_hscrolling_p = 1;
30962 DEFSYM (Qauto_hscroll_mode, "auto-hscroll-mode");
30964 DEFVAR_INT ("hscroll-margin", hscroll_margin,
30965 doc: /* How many columns away from the window edge point is allowed to get
30966 before automatic hscrolling will horizontally scroll the window. */);
30967 hscroll_margin = 5;
30969 DEFVAR_LISP ("hscroll-step", Vhscroll_step,
30970 doc: /* How many columns to scroll the window when point gets too close to the edge.
30971 When point is less than `hscroll-margin' columns from the window
30972 edge, automatic hscrolling will scroll the window by the amount of columns
30973 determined by this variable. If its value is a positive integer, scroll that
30974 many columns. If it's a positive floating-point number, it specifies the
30975 fraction of the window's width to scroll. If it's nil or zero, point will be
30976 centered horizontally after the scroll. Any other value, including negative
30977 numbers, are treated as if the value were zero.
30979 Automatic hscrolling always moves point outside the scroll margin, so if
30980 point was more than scroll step columns inside the margin, the window will
30981 scroll more than the value given by the scroll step.
30983 Note that the lower bound for automatic hscrolling specified by `scroll-left'
30984 and `scroll-right' overrides this variable's effect. */);
30985 Vhscroll_step = make_number (0);
30987 DEFVAR_BOOL ("message-truncate-lines", message_truncate_lines,
30988 doc: /* If non-nil, messages are truncated instead of resizing the echo area.
30989 Bind this around calls to `message' to let it take effect. */);
30990 message_truncate_lines = 0;
30992 DEFVAR_LISP ("menu-bar-update-hook", Vmenu_bar_update_hook,
30993 doc: /* Normal hook run to update the menu bar definitions.
30994 Redisplay runs this hook before it redisplays the menu bar.
30995 This is used to update menus such as Buffers, whose contents depend on
30996 various data. */);
30997 Vmenu_bar_update_hook = Qnil;
30999 DEFVAR_LISP ("menu-updating-frame", Vmenu_updating_frame,
31000 doc: /* Frame for which we are updating a menu.
31001 The enable predicate for a menu binding should check this variable. */);
31002 Vmenu_updating_frame = Qnil;
31004 DEFVAR_BOOL ("inhibit-menubar-update", inhibit_menubar_update,
31005 doc: /* Non-nil means don't update menu bars. Internal use only. */);
31006 inhibit_menubar_update = 0;
31008 DEFVAR_LISP ("wrap-prefix", Vwrap_prefix,
31009 doc: /* Prefix prepended to all continuation lines at display time.
31010 The value may be a string, an image, or a stretch-glyph; it is
31011 interpreted in the same way as the value of a `display' text property.
31013 This variable is overridden by any `wrap-prefix' text or overlay
31014 property.
31016 To add a prefix to non-continuation lines, use `line-prefix'. */);
31017 Vwrap_prefix = Qnil;
31018 DEFSYM (Qwrap_prefix, "wrap-prefix");
31019 Fmake_variable_buffer_local (Qwrap_prefix);
31021 DEFVAR_LISP ("line-prefix", Vline_prefix,
31022 doc: /* Prefix prepended to all non-continuation lines at display time.
31023 The value may be a string, an image, or a stretch-glyph; it is
31024 interpreted in the same way as the value of a `display' text property.
31026 This variable is overridden by any `line-prefix' text or overlay
31027 property.
31029 To add a prefix to continuation lines, use `wrap-prefix'. */);
31030 Vline_prefix = Qnil;
31031 DEFSYM (Qline_prefix, "line-prefix");
31032 Fmake_variable_buffer_local (Qline_prefix);
31034 DEFVAR_BOOL ("inhibit-eval-during-redisplay", inhibit_eval_during_redisplay,
31035 doc: /* Non-nil means don't eval Lisp during redisplay. */);
31036 inhibit_eval_during_redisplay = 0;
31038 DEFVAR_BOOL ("inhibit-free-realized-faces", inhibit_free_realized_faces,
31039 doc: /* Non-nil means don't free realized faces. Internal use only. */);
31040 inhibit_free_realized_faces = 0;
31042 DEFVAR_BOOL ("inhibit-bidi-mirroring", inhibit_bidi_mirroring,
31043 doc: /* Non-nil means don't mirror characters even when bidi context requires that.
31044 Intended for use during debugging and for testing bidi display;
31045 see biditest.el in the test suite. */);
31046 inhibit_bidi_mirroring = 0;
31048 #ifdef GLYPH_DEBUG
31049 DEFVAR_BOOL ("inhibit-try-window-id", inhibit_try_window_id,
31050 doc: /* Inhibit try_window_id display optimization. */);
31051 inhibit_try_window_id = 0;
31053 DEFVAR_BOOL ("inhibit-try-window-reusing", inhibit_try_window_reusing,
31054 doc: /* Inhibit try_window_reusing display optimization. */);
31055 inhibit_try_window_reusing = 0;
31057 DEFVAR_BOOL ("inhibit-try-cursor-movement", inhibit_try_cursor_movement,
31058 doc: /* Inhibit try_cursor_movement display optimization. */);
31059 inhibit_try_cursor_movement = 0;
31060 #endif /* GLYPH_DEBUG */
31062 DEFVAR_INT ("overline-margin", overline_margin,
31063 doc: /* Space between overline and text, in pixels.
31064 The default value is 2: the height of the overline (1 pixel) plus 1 pixel
31065 margin to the character height. */);
31066 overline_margin = 2;
31068 DEFVAR_INT ("underline-minimum-offset",
31069 underline_minimum_offset,
31070 doc: /* Minimum distance between baseline and underline.
31071 This can improve legibility of underlined text at small font sizes,
31072 particularly when using variable `x-use-underline-position-properties'
31073 with fonts that specify an UNDERLINE_POSITION relatively close to the
31074 baseline. The default value is 1. */);
31075 underline_minimum_offset = 1;
31077 DEFVAR_BOOL ("display-hourglass", display_hourglass_p,
31078 doc: /* Non-nil means show an hourglass pointer, when Emacs is busy.
31079 This feature only works when on a window system that can change
31080 cursor shapes. */);
31081 display_hourglass_p = 1;
31083 DEFVAR_LISP ("hourglass-delay", Vhourglass_delay,
31084 doc: /* Seconds to wait before displaying an hourglass pointer when Emacs is busy. */);
31085 Vhourglass_delay = make_number (DEFAULT_HOURGLASS_DELAY);
31087 #ifdef HAVE_WINDOW_SYSTEM
31088 hourglass_atimer = NULL;
31089 hourglass_shown_p = 0;
31090 #endif /* HAVE_WINDOW_SYSTEM */
31092 /* Name of the face used to display glyphless characters. */
31093 DEFSYM (Qglyphless_char, "glyphless-char");
31095 /* Method symbols for Vglyphless_char_display. */
31096 DEFSYM (Qhex_code, "hex-code");
31097 DEFSYM (Qempty_box, "empty-box");
31098 DEFSYM (Qthin_space, "thin-space");
31099 DEFSYM (Qzero_width, "zero-width");
31101 DEFVAR_LISP ("pre-redisplay-function", Vpre_redisplay_function,
31102 doc: /* Function run just before redisplay.
31103 It is called with one argument, which is the set of windows that are to
31104 be redisplayed. This set can be nil (meaning, only the selected window),
31105 or t (meaning all windows). */);
31106 Vpre_redisplay_function = intern ("ignore");
31108 /* Symbol for the purpose of Vglyphless_char_display. */
31109 DEFSYM (Qglyphless_char_display, "glyphless-char-display");
31110 Fput (Qglyphless_char_display, Qchar_table_extra_slots, make_number (1));
31112 DEFVAR_LISP ("glyphless-char-display", Vglyphless_char_display,
31113 doc: /* Char-table defining glyphless characters.
31114 Each element, if non-nil, should be one of the following:
31115 an ASCII acronym string: display this string in a box
31116 `hex-code': display the hexadecimal code of a character in a box
31117 `empty-box': display as an empty box
31118 `thin-space': display as 1-pixel width space
31119 `zero-width': don't display
31120 An element may also be a cons cell (GRAPHICAL . TEXT), which specifies the
31121 display method for graphical terminals and text terminals respectively.
31122 GRAPHICAL and TEXT should each have one of the values listed above.
31124 The char-table has one extra slot to control the display of a character for
31125 which no font is found. This slot only takes effect on graphical terminals.
31126 Its value should be an ASCII acronym string, `hex-code', `empty-box', or
31127 `thin-space'. The default is `empty-box'.
31129 If a character has a non-nil entry in an active display table, the
31130 display table takes effect; in this case, Emacs does not consult
31131 `glyphless-char-display' at all. */);
31132 Vglyphless_char_display = Fmake_char_table (Qglyphless_char_display, Qnil);
31133 Fset_char_table_extra_slot (Vglyphless_char_display, make_number (0),
31134 Qempty_box);
31136 DEFVAR_LISP ("debug-on-message", Vdebug_on_message,
31137 doc: /* If non-nil, debug if a message matching this regexp is displayed. */);
31138 Vdebug_on_message = Qnil;
31140 DEFVAR_LISP ("redisplay--all-windows-cause", Vredisplay__all_windows_cause,
31141 doc: /* */);
31142 Vredisplay__all_windows_cause
31143 = Fmake_vector (make_number (100), make_number (0));
31145 DEFVAR_LISP ("redisplay--mode-lines-cause", Vredisplay__mode_lines_cause,
31146 doc: /* */);
31147 Vredisplay__mode_lines_cause
31148 = Fmake_vector (make_number (100), make_number (0));
31152 /* Initialize this module when Emacs starts. */
31154 void
31155 init_xdisp (void)
31157 CHARPOS (this_line_start_pos) = 0;
31159 if (!noninteractive)
31161 struct window *m = XWINDOW (minibuf_window);
31162 Lisp_Object frame = m->frame;
31163 struct frame *f = XFRAME (frame);
31164 Lisp_Object root = FRAME_ROOT_WINDOW (f);
31165 struct window *r = XWINDOW (root);
31166 int i;
31168 echo_area_window = minibuf_window;
31170 r->top_line = FRAME_TOP_MARGIN (f);
31171 r->pixel_top = r->top_line * FRAME_LINE_HEIGHT (f);
31172 r->total_cols = FRAME_COLS (f);
31173 r->pixel_width = r->total_cols * FRAME_COLUMN_WIDTH (f);
31174 r->total_lines = FRAME_TOTAL_LINES (f) - 1 - FRAME_TOP_MARGIN (f);
31175 r->pixel_height = r->total_lines * FRAME_LINE_HEIGHT (f);
31177 m->top_line = FRAME_TOTAL_LINES (f) - 1;
31178 m->pixel_top = m->top_line * FRAME_LINE_HEIGHT (f);
31179 m->total_cols = FRAME_COLS (f);
31180 m->pixel_width = m->total_cols * FRAME_COLUMN_WIDTH (f);
31181 m->total_lines = 1;
31182 m->pixel_height = m->total_lines * FRAME_LINE_HEIGHT (f);
31184 scratch_glyph_row.glyphs[TEXT_AREA] = scratch_glyphs;
31185 scratch_glyph_row.glyphs[TEXT_AREA + 1]
31186 = scratch_glyphs + MAX_SCRATCH_GLYPHS;
31188 /* The default ellipsis glyphs `...'. */
31189 for (i = 0; i < 3; ++i)
31190 default_invis_vector[i] = make_number ('.');
31194 /* Allocate the buffer for frame titles.
31195 Also used for `format-mode-line'. */
31196 int size = 100;
31197 mode_line_noprop_buf = xmalloc (size);
31198 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
31199 mode_line_noprop_ptr = mode_line_noprop_buf;
31200 mode_line_target = MODE_LINE_DISPLAY;
31203 help_echo_showing_p = 0;
31206 #ifdef HAVE_WINDOW_SYSTEM
31208 /* Platform-independent portion of hourglass implementation. */
31210 /* Timer function of hourglass_atimer. */
31212 static void
31213 show_hourglass (struct atimer *timer)
31215 /* The timer implementation will cancel this timer automatically
31216 after this function has run. Set hourglass_atimer to null
31217 so that we know the timer doesn't have to be canceled. */
31218 hourglass_atimer = NULL;
31220 if (!hourglass_shown_p)
31222 Lisp_Object tail, frame;
31224 block_input ();
31226 FOR_EACH_FRAME (tail, frame)
31228 struct frame *f = XFRAME (frame);
31230 if (FRAME_LIVE_P (f) && FRAME_WINDOW_P (f)
31231 && FRAME_RIF (f)->show_hourglass)
31232 FRAME_RIF (f)->show_hourglass (f);
31235 hourglass_shown_p = 1;
31236 unblock_input ();
31240 /* Cancel a currently active hourglass timer, and start a new one. */
31242 void
31243 start_hourglass (void)
31245 struct timespec delay;
31247 cancel_hourglass ();
31249 if (INTEGERP (Vhourglass_delay)
31250 && XINT (Vhourglass_delay) > 0)
31251 delay = make_timespec (min (XINT (Vhourglass_delay),
31252 TYPE_MAXIMUM (time_t)),
31254 else if (FLOATP (Vhourglass_delay)
31255 && XFLOAT_DATA (Vhourglass_delay) > 0)
31256 delay = dtotimespec (XFLOAT_DATA (Vhourglass_delay));
31257 else
31258 delay = make_timespec (DEFAULT_HOURGLASS_DELAY, 0);
31260 hourglass_atimer = start_atimer (ATIMER_RELATIVE, delay,
31261 show_hourglass, NULL);
31264 /* Cancel the hourglass cursor timer if active, hide a busy cursor if
31265 shown. */
31267 void
31268 cancel_hourglass (void)
31270 if (hourglass_atimer)
31272 cancel_atimer (hourglass_atimer);
31273 hourglass_atimer = NULL;
31276 if (hourglass_shown_p)
31278 Lisp_Object tail, frame;
31280 block_input ();
31282 FOR_EACH_FRAME (tail, frame)
31284 struct frame *f = XFRAME (frame);
31286 if (FRAME_LIVE_P (f) && FRAME_WINDOW_P (f)
31287 && FRAME_RIF (f)->hide_hourglass)
31288 FRAME_RIF (f)->hide_hourglass (f);
31289 #ifdef HAVE_NTGUI
31290 /* No cursors on non GUI frames - restore to stock arrow cursor. */
31291 else if (!FRAME_W32_P (f))
31292 w32_arrow_cursor ();
31293 #endif
31296 hourglass_shown_p = 0;
31297 unblock_input ();
31301 #endif /* HAVE_WINDOW_SYSTEM */