Fix bug #17244 with line-move-visual when display string covers a lot of text.
[emacs.git] / src / xdisp.c
blobeb3a6df1fc5afce394ebf13b89aba92e3da0f06c
1 /* Display generation from window structure and buffer text.
3 Copyright (C) 1985-1988, 1993-1995, 1997-2014 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.
103 . try_window
105 This function performs the full redisplay of a single window
106 assuming that its fonts were not changed and that the cursor
107 will not end up in the scroll margins. (Loading fonts requires
108 re-adjustment of dimensions of glyph matrices, which makes this
109 method impossible to use.)
111 These optimizations are tried in sequence (some can be skipped if
112 it is known that they are not applicable). If none of the
113 optimizations were successful, redisplay calls redisplay_windows,
114 which performs a full redisplay of all windows.
116 Desired matrices.
118 Desired matrices are always built per Emacs window. The function
119 `display_line' is the central function to look at if you are
120 interested. It constructs one row in a desired matrix given an
121 iterator structure containing both a buffer position and a
122 description of the environment in which the text is to be
123 displayed. But this is too early, read on.
125 Characters and pixmaps displayed for a range of buffer text depend
126 on various settings of buffers and windows, on overlays and text
127 properties, on display tables, on selective display. The good news
128 is that all this hairy stuff is hidden behind a small set of
129 interface functions taking an iterator structure (struct it)
130 argument.
132 Iteration over things to be displayed is then simple. It is
133 started by initializing an iterator with a call to init_iterator,
134 passing it the buffer position where to start iteration. For
135 iteration over strings, pass -1 as the position to init_iterator,
136 and call reseat_to_string when the string is ready, to initialize
137 the iterator for that string. Thereafter, calls to
138 get_next_display_element fill the iterator structure with relevant
139 information about the next thing to display. Calls to
140 set_iterator_to_next move the iterator to the next thing.
142 Besides this, an iterator also contains information about the
143 display environment in which glyphs for display elements are to be
144 produced. It has fields for the width and height of the display,
145 the information whether long lines are truncated or continued, a
146 current X and Y position, and lots of other stuff you can better
147 see in dispextern.h.
149 Glyphs in a desired matrix are normally constructed in a loop
150 calling get_next_display_element and then PRODUCE_GLYPHS. The call
151 to PRODUCE_GLYPHS will fill the iterator structure with pixel
152 information about the element being displayed and at the same time
153 produce glyphs for it. If the display element fits on the line
154 being displayed, set_iterator_to_next is called next, otherwise the
155 glyphs produced are discarded. The function display_line is the
156 workhorse of filling glyph rows in the desired matrix with glyphs.
157 In addition to producing glyphs, it also handles line truncation
158 and continuation, word wrap, and cursor positioning (for the
159 latter, see also set_cursor_from_row).
161 Frame matrices.
163 That just couldn't be all, could it? What about terminal types not
164 supporting operations on sub-windows of the screen? To update the
165 display on such a terminal, window-based glyph matrices are not
166 well suited. To be able to reuse part of the display (scrolling
167 lines up and down), we must instead have a view of the whole
168 screen. This is what `frame matrices' are for. They are a trick.
170 Frames on terminals like above have a glyph pool. Windows on such
171 a frame sub-allocate their glyph memory from their frame's glyph
172 pool. The frame itself is given its own glyph matrices. By
173 coincidence---or maybe something else---rows in window glyph
174 matrices are slices of corresponding rows in frame matrices. Thus
175 writing to window matrices implicitly updates a frame matrix which
176 provides us with the view of the whole screen that we originally
177 wanted to have without having to move many bytes around. To be
178 honest, there is a little bit more done, but not much more. If you
179 plan to extend that code, take a look at dispnew.c. The function
180 build_frame_matrix is a good starting point.
182 Bidirectional display.
184 Bidirectional display adds quite some hair to this already complex
185 design. The good news are that a large portion of that hairy stuff
186 is hidden in bidi.c behind only 3 interfaces. bidi.c implements a
187 reordering engine which is called by set_iterator_to_next and
188 returns the next character to display in the visual order. See
189 commentary on bidi.c for more details. As far as redisplay is
190 concerned, the effect of calling bidi_move_to_visually_next, the
191 main interface of the reordering engine, is that the iterator gets
192 magically placed on the buffer or string position that is to be
193 displayed next. In other words, a linear iteration through the
194 buffer/string is replaced with a non-linear one. All the rest of
195 the redisplay is oblivious to the bidi reordering.
197 Well, almost oblivious---there are still complications, most of
198 them due to the fact that buffer and string positions no longer
199 change monotonously with glyph indices in a glyph row. Moreover,
200 for continued lines, the buffer positions may not even be
201 monotonously changing with vertical positions. Also, accounting
202 for face changes, overlays, etc. becomes more complex because
203 non-linear iteration could potentially skip many positions with
204 changes, and then cross them again on the way back...
206 One other prominent effect of bidirectional display is that some
207 paragraphs of text need to be displayed starting at the right
208 margin of the window---the so-called right-to-left, or R2L
209 paragraphs. R2L paragraphs are displayed with R2L glyph rows,
210 which have their reversed_p flag set. The bidi reordering engine
211 produces characters in such rows starting from the character which
212 should be the rightmost on display. PRODUCE_GLYPHS then reverses
213 the order, when it fills up the glyph row whose reversed_p flag is
214 set, by prepending each new glyph to what is already there, instead
215 of appending it. When the glyph row is complete, the function
216 extend_face_to_end_of_line fills the empty space to the left of the
217 leftmost character with special glyphs, which will display as,
218 well, empty. On text terminals, these special glyphs are simply
219 blank characters. On graphics terminals, there's a single stretch
220 glyph of a suitably computed width. Both the blanks and the
221 stretch glyph are given the face of the background of the line.
222 This way, the terminal-specific back-end can still draw the glyphs
223 left to right, even for R2L lines.
225 Bidirectional display and character compositions
227 Some scripts cannot be displayed by drawing each character
228 individually, because adjacent characters change each other's shape
229 on display. For example, Arabic and Indic scripts belong to this
230 category.
232 Emacs display supports this by providing "character compositions",
233 most of which is implemented in composite.c. During the buffer
234 scan that delivers characters to PRODUCE_GLYPHS, if the next
235 character to be delivered is a composed character, the iteration
236 calls composition_reseat_it and next_element_from_composition. If
237 they succeed to compose the character with one or more of the
238 following characters, the whole sequence of characters that where
239 composed is recorded in the `struct composition_it' object that is
240 part of the buffer iterator. The composed sequence could produce
241 one or more font glyphs (called "grapheme clusters") on the screen.
242 Each of these grapheme clusters is then delivered to PRODUCE_GLYPHS
243 in the direction corresponding to the current bidi scan direction
244 (recorded in the scan_dir member of the `struct bidi_it' object
245 that is part of the buffer iterator). In particular, if the bidi
246 iterator currently scans the buffer backwards, the grapheme
247 clusters are delivered back to front. This reorders the grapheme
248 clusters as appropriate for the current bidi context. Note that
249 this means that the grapheme clusters are always stored in the
250 LGSTRING object (see composite.c) in the logical order.
252 Moving an iterator in bidirectional text
253 without producing glyphs
255 Note one important detail mentioned above: that the bidi reordering
256 engine, driven by the iterator, produces characters in R2L rows
257 starting at the character that will be the rightmost on display.
258 As far as the iterator is concerned, the geometry of such rows is
259 still left to right, i.e. the iterator "thinks" the first character
260 is at the leftmost pixel position. The iterator does not know that
261 PRODUCE_GLYPHS reverses the order of the glyphs that the iterator
262 delivers. This is important when functions from the move_it_*
263 family are used to get to certain screen position or to match
264 screen coordinates with buffer coordinates: these functions use the
265 iterator geometry, which is left to right even in R2L paragraphs.
266 This works well with most callers of move_it_*, because they need
267 to get to a specific column, and columns are still numbered in the
268 reading order, i.e. the rightmost character in a R2L paragraph is
269 still column zero. But some callers do not get well with this; a
270 notable example is mouse clicks that need to find the character
271 that corresponds to certain pixel coordinates. See
272 buffer_posn_from_coords in dispnew.c for how this is handled. */
274 #include <config.h>
275 #include <stdio.h>
276 #include <limits.h>
278 #include "lisp.h"
279 #include "atimer.h"
280 #include "keyboard.h"
281 #include "frame.h"
282 #include "window.h"
283 #include "termchar.h"
284 #include "dispextern.h"
285 #include "character.h"
286 #include "buffer.h"
287 #include "charset.h"
288 #include "indent.h"
289 #include "commands.h"
290 #include "keymap.h"
291 #include "macros.h"
292 #include "disptab.h"
293 #include "termhooks.h"
294 #include "termopts.h"
295 #include "intervals.h"
296 #include "coding.h"
297 #include "process.h"
298 #include "region-cache.h"
299 #include "font.h"
300 #include "fontset.h"
301 #include "blockinput.h"
302 #ifdef HAVE_WINDOW_SYSTEM
303 #include TERM_HEADER
304 #endif /* HAVE_WINDOW_SYSTEM */
306 #ifndef FRAME_X_OUTPUT
307 #define FRAME_X_OUTPUT(f) ((f)->output_data.x)
308 #endif
310 #define INFINITY 10000000
312 Lisp_Object Qoverriding_local_map, Qoverriding_terminal_local_map;
313 Lisp_Object Qwindow_scroll_functions;
314 static Lisp_Object Qwindow_text_change_functions;
315 static Lisp_Object Qredisplay_end_trigger_functions;
316 Lisp_Object Qinhibit_point_motion_hooks;
317 static Lisp_Object QCeval, QCpropertize;
318 Lisp_Object QCfile, QCdata;
319 static Lisp_Object Qfontified;
320 static Lisp_Object Qgrow_only;
321 static Lisp_Object Qinhibit_eval_during_redisplay;
322 static Lisp_Object Qbuffer_position, Qposition, Qobject;
323 static Lisp_Object Qright_to_left, Qleft_to_right;
325 /* Cursor shapes. */
326 Lisp_Object Qbar, Qhbar, Qbox, Qhollow;
328 /* Pointer shapes. */
329 static Lisp_Object Qarrow, Qhand;
330 Lisp_Object Qtext;
332 /* Holds the list (error). */
333 static Lisp_Object list_of_error;
335 static Lisp_Object Qfontification_functions;
337 static Lisp_Object Qwrap_prefix;
338 static Lisp_Object Qline_prefix;
339 static Lisp_Object Qredisplay_internal;
341 /* Non-nil means don't actually do any redisplay. */
343 Lisp_Object Qinhibit_redisplay;
345 /* Names of text properties relevant for redisplay. */
347 Lisp_Object Qdisplay;
349 Lisp_Object Qspace, QCalign_to;
350 static Lisp_Object QCrelative_width, QCrelative_height;
351 Lisp_Object Qleft_margin, Qright_margin;
352 static Lisp_Object Qspace_width, Qraise;
353 static Lisp_Object Qslice;
354 Lisp_Object Qcenter;
355 static Lisp_Object Qmargin, Qpointer;
356 static Lisp_Object Qline_height;
358 #ifdef HAVE_WINDOW_SYSTEM
360 /* Test if overflow newline into fringe. Called with iterator IT
361 at or past right window margin, and with IT->current_x set. */
363 #define IT_OVERFLOW_NEWLINE_INTO_FRINGE(IT) \
364 (!NILP (Voverflow_newline_into_fringe) \
365 && FRAME_WINDOW_P ((IT)->f) \
366 && ((IT)->bidi_it.paragraph_dir == R2L \
367 ? (WINDOW_LEFT_FRINGE_WIDTH ((IT)->w) > 0) \
368 : (WINDOW_RIGHT_FRINGE_WIDTH ((IT)->w) > 0)) \
369 && (IT)->current_x == (IT)->last_visible_x)
371 #else /* !HAVE_WINDOW_SYSTEM */
372 #define IT_OVERFLOW_NEWLINE_INTO_FRINGE(it) 0
373 #endif /* HAVE_WINDOW_SYSTEM */
375 /* Test if the display element loaded in IT, or the underlying buffer
376 or string character, is a space or a TAB character. This is used
377 to determine where word wrapping can occur. */
379 #define IT_DISPLAYING_WHITESPACE(it) \
380 ((it->what == IT_CHARACTER && (it->c == ' ' || it->c == '\t')) \
381 || ((STRINGP (it->string) \
382 && (SREF (it->string, IT_STRING_BYTEPOS (*it)) == ' ' \
383 || SREF (it->string, IT_STRING_BYTEPOS (*it)) == '\t')) \
384 || (it->s \
385 && (it->s[IT_BYTEPOS (*it)] == ' ' \
386 || it->s[IT_BYTEPOS (*it)] == '\t')) \
387 || (IT_BYTEPOS (*it) < ZV_BYTE \
388 && (*BYTE_POS_ADDR (IT_BYTEPOS (*it)) == ' ' \
389 || *BYTE_POS_ADDR (IT_BYTEPOS (*it)) == '\t')))) \
391 /* Name of the face used to highlight trailing whitespace. */
393 static Lisp_Object Qtrailing_whitespace;
395 /* Name and number of the face used to highlight escape glyphs. */
397 static Lisp_Object Qescape_glyph;
399 /* Name and number of the face used to highlight non-breaking spaces. */
401 static Lisp_Object Qnobreak_space;
403 /* The symbol `image' which is the car of the lists used to represent
404 images in Lisp. Also a tool bar style. */
406 Lisp_Object Qimage;
408 /* The image map types. */
409 Lisp_Object QCmap;
410 static Lisp_Object QCpointer;
411 static Lisp_Object Qrect, Qcircle, Qpoly;
413 /* Tool bar styles */
414 Lisp_Object Qboth, Qboth_horiz, Qtext_image_horiz;
416 /* Non-zero means print newline to stdout before next mini-buffer
417 message. */
419 bool noninteractive_need_newline;
421 /* Non-zero means print newline to message log before next message. */
423 static bool message_log_need_newline;
425 /* Three markers that message_dolog uses.
426 It could allocate them itself, but that causes trouble
427 in handling memory-full errors. */
428 static Lisp_Object message_dolog_marker1;
429 static Lisp_Object message_dolog_marker2;
430 static Lisp_Object message_dolog_marker3;
432 /* The buffer position of the first character appearing entirely or
433 partially on the line of the selected window which contains the
434 cursor; <= 0 if not known. Set by set_cursor_from_row, used for
435 redisplay optimization in redisplay_internal. */
437 static struct text_pos this_line_start_pos;
439 /* Number of characters past the end of the line above, including the
440 terminating newline. */
442 static struct text_pos this_line_end_pos;
444 /* The vertical positions and the height of this line. */
446 static int this_line_vpos;
447 static int this_line_y;
448 static int this_line_pixel_height;
450 /* X position at which this display line starts. Usually zero;
451 negative if first character is partially visible. */
453 static int this_line_start_x;
455 /* The smallest character position seen by move_it_* functions as they
456 move across display lines. Used to set MATRIX_ROW_START_CHARPOS of
457 hscrolled lines, see display_line. */
459 static struct text_pos this_line_min_pos;
461 /* Buffer that this_line_.* variables are referring to. */
463 static struct buffer *this_line_buffer;
466 /* Values of those variables at last redisplay are stored as
467 properties on `overlay-arrow-position' symbol. However, if
468 Voverlay_arrow_position is a marker, last-arrow-position is its
469 numerical position. */
471 static Lisp_Object Qlast_arrow_position, Qlast_arrow_string;
473 /* Alternative overlay-arrow-string and overlay-arrow-bitmap
474 properties on a symbol in overlay-arrow-variable-list. */
476 static Lisp_Object Qoverlay_arrow_string, Qoverlay_arrow_bitmap;
478 Lisp_Object Qmenu_bar_update_hook;
480 /* Nonzero if an overlay arrow has been displayed in this window. */
482 static bool overlay_arrow_seen;
484 /* Vector containing glyphs for an ellipsis `...'. */
486 static Lisp_Object default_invis_vector[3];
488 /* This is the window where the echo area message was displayed. It
489 is always a mini-buffer window, but it may not be the same window
490 currently active as a mini-buffer. */
492 Lisp_Object echo_area_window;
494 /* List of pairs (MESSAGE . MULTIBYTE). The function save_message
495 pushes the current message and the value of
496 message_enable_multibyte on the stack, the function restore_message
497 pops the stack and displays MESSAGE again. */
499 static Lisp_Object Vmessage_stack;
501 /* Nonzero means multibyte characters were enabled when the echo area
502 message was specified. */
504 static bool message_enable_multibyte;
506 /* Nonzero if we should redraw the mode lines on the next redisplay.
507 If it has value REDISPLAY_SOME, then only redisplay the mode lines where
508 the `redisplay' bit has been set. Otherwise, redisplay all mode lines
509 (the number used is then only used to track down the cause for this
510 full-redisplay). */
512 int update_mode_lines;
514 /* Nonzero if window sizes or contents other than selected-window have changed
515 since last redisplay that finished.
516 If it has value REDISPLAY_SOME, then only redisplay the windows where
517 the `redisplay' bit has been set. Otherwise, redisplay all windows
518 (the number used is then only used to track down the cause for this
519 full-redisplay). */
521 int windows_or_buffers_changed;
523 /* Nonzero after display_mode_line if %l was used and it displayed a
524 line number. */
526 static bool line_number_displayed;
528 /* The name of the *Messages* buffer, a string. */
530 static Lisp_Object Vmessages_buffer_name;
532 /* Current, index 0, and last displayed echo area message. Either
533 buffers from echo_buffers, or nil to indicate no message. */
535 Lisp_Object echo_area_buffer[2];
537 /* The buffers referenced from echo_area_buffer. */
539 static Lisp_Object echo_buffer[2];
541 /* A vector saved used in with_area_buffer to reduce consing. */
543 static Lisp_Object Vwith_echo_area_save_vector;
545 /* Non-zero means display_echo_area should display the last echo area
546 message again. Set by redisplay_preserve_echo_area. */
548 static bool display_last_displayed_message_p;
550 /* Nonzero if echo area is being used by print; zero if being used by
551 message. */
553 static bool message_buf_print;
555 /* The symbol `inhibit-menubar-update' and its DEFVAR_BOOL variable. */
557 static Lisp_Object Qinhibit_menubar_update;
558 static Lisp_Object Qmessage_truncate_lines;
560 /* Set to 1 in clear_message to make redisplay_internal aware
561 of an emptied echo area. */
563 static bool message_cleared_p;
565 /* A scratch glyph row with contents used for generating truncation
566 glyphs. Also used in direct_output_for_insert. */
568 #define MAX_SCRATCH_GLYPHS 100
569 static struct glyph_row scratch_glyph_row;
570 static struct glyph scratch_glyphs[MAX_SCRATCH_GLYPHS];
572 /* Ascent and height of the last line processed by move_it_to. */
574 static int last_height;
576 /* Non-zero if there's a help-echo in the echo area. */
578 bool help_echo_showing_p;
580 /* The maximum distance to look ahead for text properties. Values
581 that are too small let us call compute_char_face and similar
582 functions too often which is expensive. Values that are too large
583 let us call compute_char_face and alike too often because we
584 might not be interested in text properties that far away. */
586 #define TEXT_PROP_DISTANCE_LIMIT 100
588 /* SAVE_IT and RESTORE_IT are called when we save a snapshot of the
589 iterator state and later restore it. This is needed because the
590 bidi iterator on bidi.c keeps a stacked cache of its states, which
591 is really a singleton. When we use scratch iterator objects to
592 move around the buffer, we can cause the bidi cache to be pushed or
593 popped, and therefore we need to restore the cache state when we
594 return to the original iterator. */
595 #define SAVE_IT(ITCOPY,ITORIG,CACHE) \
596 do { \
597 if (CACHE) \
598 bidi_unshelve_cache (CACHE, 1); \
599 ITCOPY = ITORIG; \
600 CACHE = bidi_shelve_cache (); \
601 } while (0)
603 #define RESTORE_IT(pITORIG,pITCOPY,CACHE) \
604 do { \
605 if (pITORIG != pITCOPY) \
606 *(pITORIG) = *(pITCOPY); \
607 bidi_unshelve_cache (CACHE, 0); \
608 CACHE = NULL; \
609 } while (0)
611 /* Functions to mark elements as needing redisplay. */
612 enum { REDISPLAY_SOME = 2}; /* Arbitrary choice. */
614 void
615 redisplay_other_windows (void)
617 if (!windows_or_buffers_changed)
618 windows_or_buffers_changed = REDISPLAY_SOME;
621 void
622 wset_redisplay (struct window *w)
624 /* Beware: selected_window can be nil during early stages. */
625 if (!EQ (make_lisp_ptr (w, Lisp_Vectorlike), selected_window))
626 redisplay_other_windows ();
627 w->redisplay = true;
630 void
631 fset_redisplay (struct frame *f)
633 redisplay_other_windows ();
634 f->redisplay = true;
637 void
638 bset_redisplay (struct buffer *b)
640 int count = buffer_window_count (b);
641 if (count > 0)
643 /* ... it's visible in other window than selected, */
644 if (count > 1 || b != XBUFFER (XWINDOW (selected_window)->contents))
645 redisplay_other_windows ();
646 /* Even if we don't set windows_or_buffers_changed, do set `redisplay'
647 so that if we later set windows_or_buffers_changed, this buffer will
648 not be omitted. */
649 b->text->redisplay = true;
653 void
654 bset_update_mode_line (struct buffer *b)
656 if (!update_mode_lines)
657 update_mode_lines = REDISPLAY_SOME;
658 b->text->redisplay = true;
661 #ifdef GLYPH_DEBUG
663 /* Non-zero means print traces of redisplay if compiled with
664 GLYPH_DEBUG defined. */
666 bool trace_redisplay_p;
668 #endif /* GLYPH_DEBUG */
670 #ifdef DEBUG_TRACE_MOVE
671 /* Non-zero means trace with TRACE_MOVE to stderr. */
672 int trace_move;
674 #define TRACE_MOVE(x) if (trace_move) fprintf x; else (void) 0
675 #else
676 #define TRACE_MOVE(x) (void) 0
677 #endif
679 static Lisp_Object Qauto_hscroll_mode;
681 /* Buffer being redisplayed -- for redisplay_window_error. */
683 static struct buffer *displayed_buffer;
685 /* Value returned from text property handlers (see below). */
687 enum prop_handled
689 HANDLED_NORMALLY,
690 HANDLED_RECOMPUTE_PROPS,
691 HANDLED_OVERLAY_STRING_CONSUMED,
692 HANDLED_RETURN
695 /* A description of text properties that redisplay is interested
696 in. */
698 struct props
700 /* The name of the property. */
701 Lisp_Object *name;
703 /* A unique index for the property. */
704 enum prop_idx idx;
706 /* A handler function called to set up iterator IT from the property
707 at IT's current position. Value is used to steer handle_stop. */
708 enum prop_handled (*handler) (struct it *it);
711 static enum prop_handled handle_face_prop (struct it *);
712 static enum prop_handled handle_invisible_prop (struct it *);
713 static enum prop_handled handle_display_prop (struct it *);
714 static enum prop_handled handle_composition_prop (struct it *);
715 static enum prop_handled handle_overlay_change (struct it *);
716 static enum prop_handled handle_fontified_prop (struct it *);
718 /* Properties handled by iterators. */
720 static struct props it_props[] =
722 {&Qfontified, FONTIFIED_PROP_IDX, handle_fontified_prop},
723 /* Handle `face' before `display' because some sub-properties of
724 `display' need to know the face. */
725 {&Qface, FACE_PROP_IDX, handle_face_prop},
726 {&Qdisplay, DISPLAY_PROP_IDX, handle_display_prop},
727 {&Qinvisible, INVISIBLE_PROP_IDX, handle_invisible_prop},
728 {&Qcomposition, COMPOSITION_PROP_IDX, handle_composition_prop},
729 {NULL, 0, NULL}
732 /* Value is the position described by X. If X is a marker, value is
733 the marker_position of X. Otherwise, value is X. */
735 #define COERCE_MARKER(X) (MARKERP ((X)) ? Fmarker_position (X) : (X))
737 /* Enumeration returned by some move_it_.* functions internally. */
739 enum move_it_result
741 /* Not used. Undefined value. */
742 MOVE_UNDEFINED,
744 /* Move ended at the requested buffer position or ZV. */
745 MOVE_POS_MATCH_OR_ZV,
747 /* Move ended at the requested X pixel position. */
748 MOVE_X_REACHED,
750 /* Move within a line ended at the end of a line that must be
751 continued. */
752 MOVE_LINE_CONTINUED,
754 /* Move within a line ended at the end of a line that would
755 be displayed truncated. */
756 MOVE_LINE_TRUNCATED,
758 /* Move within a line ended at a line end. */
759 MOVE_NEWLINE_OR_CR
762 /* This counter is used to clear the face cache every once in a while
763 in redisplay_internal. It is incremented for each redisplay.
764 Every CLEAR_FACE_CACHE_COUNT full redisplays, the face cache is
765 cleared. */
767 #define CLEAR_FACE_CACHE_COUNT 500
768 static int clear_face_cache_count;
770 /* Similarly for the image cache. */
772 #ifdef HAVE_WINDOW_SYSTEM
773 #define CLEAR_IMAGE_CACHE_COUNT 101
774 static int clear_image_cache_count;
776 /* Null glyph slice */
777 static struct glyph_slice null_glyph_slice = { 0, 0, 0, 0 };
778 #endif
780 /* True while redisplay_internal is in progress. */
782 bool redisplaying_p;
784 static Lisp_Object Qinhibit_free_realized_faces;
785 static Lisp_Object Qmode_line_default_help_echo;
787 /* If a string, XTread_socket generates an event to display that string.
788 (The display is done in read_char.) */
790 Lisp_Object help_echo_string;
791 Lisp_Object help_echo_window;
792 Lisp_Object help_echo_object;
793 ptrdiff_t help_echo_pos;
795 /* Temporary variable for XTread_socket. */
797 Lisp_Object previous_help_echo_string;
799 /* Platform-independent portion of hourglass implementation. */
801 #ifdef HAVE_WINDOW_SYSTEM
803 /* Non-zero means an hourglass cursor is currently shown. */
804 bool hourglass_shown_p;
806 /* If non-null, an asynchronous timer that, when it expires, displays
807 an hourglass cursor on all frames. */
808 struct atimer *hourglass_atimer;
810 #endif /* HAVE_WINDOW_SYSTEM */
812 /* Name of the face used to display glyphless characters. */
813 static Lisp_Object Qglyphless_char;
815 /* Symbol for the purpose of Vglyphless_char_display. */
816 static Lisp_Object Qglyphless_char_display;
818 /* Method symbols for Vglyphless_char_display. */
819 static Lisp_Object Qhex_code, Qempty_box, Qthin_space, Qzero_width;
821 /* Default number of seconds to wait before displaying an hourglass
822 cursor. */
823 #define DEFAULT_HOURGLASS_DELAY 1
825 #ifdef HAVE_WINDOW_SYSTEM
827 /* Default pixel width of `thin-space' display method. */
828 #define THIN_SPACE_WIDTH 1
830 #endif /* HAVE_WINDOW_SYSTEM */
832 /* Function prototypes. */
834 static void setup_for_ellipsis (struct it *, int);
835 static void set_iterator_to_next (struct it *, int);
836 static void mark_window_display_accurate_1 (struct window *, int);
837 static int single_display_spec_string_p (Lisp_Object, Lisp_Object);
838 static int display_prop_string_p (Lisp_Object, Lisp_Object);
839 static int row_for_charpos_p (struct glyph_row *, ptrdiff_t);
840 static int cursor_row_p (struct glyph_row *);
841 static int redisplay_mode_lines (Lisp_Object, bool);
842 static char *decode_mode_spec_coding (Lisp_Object, char *, int);
844 static Lisp_Object get_it_property (struct it *it, Lisp_Object prop);
846 static void handle_line_prefix (struct it *);
848 static void pint2str (char *, int, ptrdiff_t);
849 static void pint2hrstr (char *, int, ptrdiff_t);
850 static struct text_pos run_window_scroll_functions (Lisp_Object,
851 struct text_pos);
852 static int text_outside_line_unchanged_p (struct window *,
853 ptrdiff_t, ptrdiff_t);
854 static void store_mode_line_noprop_char (char);
855 static int store_mode_line_noprop (const char *, int, int);
856 static void handle_stop (struct it *);
857 static void handle_stop_backwards (struct it *, ptrdiff_t);
858 static void vmessage (const char *, va_list) ATTRIBUTE_FORMAT_PRINTF (1, 0);
859 static void ensure_echo_area_buffers (void);
860 static void unwind_with_echo_area_buffer (Lisp_Object);
861 static Lisp_Object with_echo_area_buffer_unwind_data (struct window *);
862 static int with_echo_area_buffer (struct window *, int,
863 int (*) (ptrdiff_t, Lisp_Object),
864 ptrdiff_t, Lisp_Object);
865 static void clear_garbaged_frames (void);
866 static int current_message_1 (ptrdiff_t, Lisp_Object);
867 static int truncate_message_1 (ptrdiff_t, Lisp_Object);
868 static void set_message (Lisp_Object);
869 static int set_message_1 (ptrdiff_t, Lisp_Object);
870 static int display_echo_area (struct window *);
871 static int display_echo_area_1 (ptrdiff_t, Lisp_Object);
872 static int resize_mini_window_1 (ptrdiff_t, Lisp_Object);
873 static void unwind_redisplay (void);
874 static int string_char_and_length (const unsigned char *, int *);
875 static struct text_pos display_prop_end (struct it *, Lisp_Object,
876 struct text_pos);
877 static int compute_window_start_on_continuation_line (struct window *);
878 static void insert_left_trunc_glyphs (struct it *);
879 static struct glyph_row *get_overlay_arrow_glyph_row (struct window *,
880 Lisp_Object);
881 static void extend_face_to_end_of_line (struct it *);
882 static int append_space_for_newline (struct it *, int);
883 static int cursor_row_fully_visible_p (struct window *, int, int);
884 static int try_scrolling (Lisp_Object, int, ptrdiff_t, ptrdiff_t, int, int);
885 static int try_cursor_movement (Lisp_Object, struct text_pos, int *);
886 static int trailing_whitespace_p (ptrdiff_t);
887 static intmax_t message_log_check_duplicate (ptrdiff_t, ptrdiff_t);
888 static void push_it (struct it *, struct text_pos *);
889 static void iterate_out_of_display_property (struct it *);
890 static void pop_it (struct it *);
891 static void sync_frame_with_window_matrix_rows (struct window *);
892 static void redisplay_internal (void);
893 static int echo_area_display (int);
894 static void redisplay_windows (Lisp_Object);
895 static void redisplay_window (Lisp_Object, bool);
896 static Lisp_Object redisplay_window_error (Lisp_Object);
897 static Lisp_Object redisplay_window_0 (Lisp_Object);
898 static Lisp_Object redisplay_window_1 (Lisp_Object);
899 static int set_cursor_from_row (struct window *, struct glyph_row *,
900 struct glyph_matrix *, ptrdiff_t, ptrdiff_t,
901 int, int);
902 static int update_menu_bar (struct frame *, int, int);
903 static int try_window_reusing_current_matrix (struct window *);
904 static int try_window_id (struct window *);
905 static int display_line (struct it *);
906 static int display_mode_lines (struct window *);
907 static int display_mode_line (struct window *, enum face_id, Lisp_Object);
908 static int display_mode_element (struct it *, int, int, int, Lisp_Object, Lisp_Object, int);
909 static int store_mode_line_string (const char *, Lisp_Object, int, int, int, Lisp_Object);
910 static const char *decode_mode_spec (struct window *, int, int, Lisp_Object *);
911 static void display_menu_bar (struct window *);
912 static ptrdiff_t display_count_lines (ptrdiff_t, ptrdiff_t, ptrdiff_t,
913 ptrdiff_t *);
914 static int display_string (const char *, Lisp_Object, Lisp_Object,
915 ptrdiff_t, ptrdiff_t, struct it *, int, int, int, int);
916 static void compute_line_metrics (struct it *);
917 static void run_redisplay_end_trigger_hook (struct it *);
918 static int get_overlay_strings (struct it *, ptrdiff_t);
919 static int get_overlay_strings_1 (struct it *, ptrdiff_t, int);
920 static void next_overlay_string (struct it *);
921 static void reseat (struct it *, struct text_pos, int);
922 static void reseat_1 (struct it *, struct text_pos, int);
923 static void back_to_previous_visible_line_start (struct it *);
924 static void reseat_at_next_visible_line_start (struct it *, int);
925 static int next_element_from_ellipsis (struct it *);
926 static int next_element_from_display_vector (struct it *);
927 static int next_element_from_string (struct it *);
928 static int next_element_from_c_string (struct it *);
929 static int next_element_from_buffer (struct it *);
930 static int next_element_from_composition (struct it *);
931 static int next_element_from_image (struct it *);
932 static int next_element_from_stretch (struct it *);
933 static void load_overlay_strings (struct it *, ptrdiff_t);
934 static int init_from_display_pos (struct it *, struct window *,
935 struct display_pos *);
936 static void reseat_to_string (struct it *, const char *,
937 Lisp_Object, ptrdiff_t, ptrdiff_t, int, int);
938 static int get_next_display_element (struct it *);
939 static enum move_it_result
940 move_it_in_display_line_to (struct it *, ptrdiff_t, int,
941 enum move_operation_enum);
942 static void get_visually_first_element (struct it *);
943 static void init_to_row_start (struct it *, struct window *,
944 struct glyph_row *);
945 static int init_to_row_end (struct it *, struct window *,
946 struct glyph_row *);
947 static void back_to_previous_line_start (struct it *);
948 static int forward_to_next_line_start (struct it *, int *, struct bidi_it *);
949 static struct text_pos string_pos_nchars_ahead (struct text_pos,
950 Lisp_Object, ptrdiff_t);
951 static struct text_pos string_pos (ptrdiff_t, Lisp_Object);
952 static struct text_pos c_string_pos (ptrdiff_t, const char *, bool);
953 static ptrdiff_t number_of_chars (const char *, bool);
954 static void compute_stop_pos (struct it *);
955 static void compute_string_pos (struct text_pos *, struct text_pos,
956 Lisp_Object);
957 static int face_before_or_after_it_pos (struct it *, int);
958 static ptrdiff_t next_overlay_change (ptrdiff_t);
959 static int handle_display_spec (struct it *, Lisp_Object, Lisp_Object,
960 Lisp_Object, struct text_pos *, ptrdiff_t, int);
961 static int handle_single_display_spec (struct it *, Lisp_Object,
962 Lisp_Object, Lisp_Object,
963 struct text_pos *, ptrdiff_t, int, int);
964 static int underlying_face_id (struct it *);
965 static int in_ellipses_for_invisible_text_p (struct display_pos *,
966 struct window *);
968 #define face_before_it_pos(IT) face_before_or_after_it_pos ((IT), 1)
969 #define face_after_it_pos(IT) face_before_or_after_it_pos ((IT), 0)
971 #ifdef HAVE_WINDOW_SYSTEM
973 static void x_consider_frame_title (Lisp_Object);
974 static void update_tool_bar (struct frame *, int);
975 static int redisplay_tool_bar (struct frame *);
976 static void x_draw_bottom_divider (struct window *w);
977 static void notice_overwritten_cursor (struct window *,
978 enum glyph_row_area,
979 int, int, int, int);
980 static void append_stretch_glyph (struct it *, Lisp_Object,
981 int, int, int);
984 #endif /* HAVE_WINDOW_SYSTEM */
986 static void produce_special_glyphs (struct it *, enum display_element_type);
987 static void show_mouse_face (Mouse_HLInfo *, enum draw_glyphs_face);
988 static bool coords_in_mouse_face_p (struct window *, int, int);
992 /***********************************************************************
993 Window display dimensions
994 ***********************************************************************/
996 /* Return the bottom boundary y-position for text lines in window W.
997 This is the first y position at which a line cannot start.
998 It is relative to the top of the window.
1000 This is the height of W minus the height of a mode line, if any. */
1003 window_text_bottom_y (struct window *w)
1005 int height = WINDOW_PIXEL_HEIGHT (w);
1007 height -= WINDOW_BOTTOM_DIVIDER_WIDTH (w);
1009 if (WINDOW_WANTS_MODELINE_P (w))
1010 height -= CURRENT_MODE_LINE_HEIGHT (w);
1012 return height;
1015 /* Return the pixel width of display area AREA of window W.
1016 ANY_AREA means return the total width of W, not including
1017 fringes to the left and right of the window. */
1020 window_box_width (struct window *w, enum glyph_row_area area)
1022 int width = w->pixel_width;
1024 if (!w->pseudo_window_p)
1026 width -= WINDOW_SCROLL_BAR_AREA_WIDTH (w);
1027 width -= WINDOW_RIGHT_DIVIDER_WIDTH (w);
1029 if (area == TEXT_AREA)
1030 width -= (WINDOW_MARGINS_WIDTH (w)
1031 + WINDOW_FRINGES_WIDTH (w));
1032 else if (area == LEFT_MARGIN_AREA)
1033 width = WINDOW_LEFT_MARGIN_WIDTH (w);
1034 else if (area == RIGHT_MARGIN_AREA)
1035 width = WINDOW_RIGHT_MARGIN_WIDTH (w);
1038 /* With wide margins, fringes, etc. we might end up with a negative
1039 width, correct that here. */
1040 return max (0, width);
1044 /* Return the pixel height of the display area of window W, not
1045 including mode lines of W, if any. */
1048 window_box_height (struct window *w)
1050 struct frame *f = XFRAME (w->frame);
1051 int height = WINDOW_PIXEL_HEIGHT (w);
1053 eassert (height >= 0);
1055 height -= WINDOW_BOTTOM_DIVIDER_WIDTH (w);
1057 /* Note: the code below that determines the mode-line/header-line
1058 height is essentially the same as that contained in the macro
1059 CURRENT_{MODE,HEADER}_LINE_HEIGHT, except that it checks whether
1060 the appropriate glyph row has its `mode_line_p' flag set,
1061 and if it doesn't, uses estimate_mode_line_height instead. */
1063 if (WINDOW_WANTS_MODELINE_P (w))
1065 struct glyph_row *ml_row
1066 = (w->current_matrix && w->current_matrix->rows
1067 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
1068 : 0);
1069 if (ml_row && ml_row->mode_line_p)
1070 height -= ml_row->height;
1071 else
1072 height -= estimate_mode_line_height (f, CURRENT_MODE_LINE_FACE_ID (w));
1075 if (WINDOW_WANTS_HEADER_LINE_P (w))
1077 struct glyph_row *hl_row
1078 = (w->current_matrix && w->current_matrix->rows
1079 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
1080 : 0);
1081 if (hl_row && hl_row->mode_line_p)
1082 height -= hl_row->height;
1083 else
1084 height -= estimate_mode_line_height (f, HEADER_LINE_FACE_ID);
1087 /* With a very small font and a mode-line that's taller than
1088 default, we might end up with a negative height. */
1089 return max (0, height);
1092 /* Return the window-relative coordinate of the left edge of display
1093 area AREA of window W. ANY_AREA means return the left edge of the
1094 whole window, to the right of the left fringe of W. */
1097 window_box_left_offset (struct window *w, enum glyph_row_area area)
1099 int x;
1101 if (w->pseudo_window_p)
1102 return 0;
1104 x = WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
1106 if (area == TEXT_AREA)
1107 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1108 + window_box_width (w, LEFT_MARGIN_AREA));
1109 else if (area == RIGHT_MARGIN_AREA)
1110 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1111 + window_box_width (w, LEFT_MARGIN_AREA)
1112 + window_box_width (w, TEXT_AREA)
1113 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
1115 : WINDOW_RIGHT_FRINGE_WIDTH (w)));
1116 else if (area == LEFT_MARGIN_AREA
1117 && WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w))
1118 x += WINDOW_LEFT_FRINGE_WIDTH (w);
1120 /* Don't return more than the window's pixel width. */
1121 return min (x, w->pixel_width);
1125 /* Return the window-relative coordinate of the right edge of display
1126 area AREA of window W. ANY_AREA means return the right edge of the
1127 whole window, to the left of the right fringe of W. */
1130 window_box_right_offset (struct window *w, enum glyph_row_area area)
1132 /* Don't return more than the window's pixel width. */
1133 return min (window_box_left_offset (w, area) + window_box_width (w, area),
1134 w->pixel_width);
1137 /* Return the frame-relative coordinate of the left edge of display
1138 area AREA of window W. ANY_AREA means return the left edge of the
1139 whole window, to the right of the left fringe of W. */
1142 window_box_left (struct window *w, enum glyph_row_area area)
1144 struct frame *f = XFRAME (w->frame);
1145 int x;
1147 if (w->pseudo_window_p)
1148 return FRAME_INTERNAL_BORDER_WIDTH (f);
1150 x = (WINDOW_LEFT_EDGE_X (w)
1151 + window_box_left_offset (w, area));
1153 return x;
1157 /* Return the frame-relative coordinate of the right edge of display
1158 area AREA of window W. ANY_AREA means return the right edge of the
1159 whole window, to the left of the right fringe of W. */
1162 window_box_right (struct window *w, enum glyph_row_area area)
1164 return window_box_left (w, area) + window_box_width (w, area);
1167 /* Get the bounding box of the display area AREA of window W, without
1168 mode lines, in frame-relative coordinates. ANY_AREA means the
1169 whole window, not including the left and right fringes of
1170 the window. Return in *BOX_X and *BOX_Y the frame-relative pixel
1171 coordinates of the upper-left corner of the box. Return in
1172 *BOX_WIDTH, and *BOX_HEIGHT the pixel width and height of the box. */
1174 void
1175 window_box (struct window *w, enum glyph_row_area area, int *box_x,
1176 int *box_y, int *box_width, int *box_height)
1178 if (box_width)
1179 *box_width = window_box_width (w, area);
1180 if (box_height)
1181 *box_height = window_box_height (w);
1182 if (box_x)
1183 *box_x = window_box_left (w, area);
1184 if (box_y)
1186 *box_y = WINDOW_TOP_EDGE_Y (w);
1187 if (WINDOW_WANTS_HEADER_LINE_P (w))
1188 *box_y += CURRENT_HEADER_LINE_HEIGHT (w);
1192 #ifdef HAVE_WINDOW_SYSTEM
1194 /* Get the bounding box of the display area AREA of window W, without
1195 mode lines and both fringes of the window. Return in *TOP_LEFT_X
1196 and TOP_LEFT_Y the frame-relative pixel coordinates of the
1197 upper-left corner of the box. Return in *BOTTOM_RIGHT_X, and
1198 *BOTTOM_RIGHT_Y the coordinates of the bottom-right corner of the
1199 box. */
1201 static void
1202 window_box_edges (struct window *w, int *top_left_x, int *top_left_y,
1203 int *bottom_right_x, int *bottom_right_y)
1205 window_box (w, ANY_AREA, top_left_x, top_left_y,
1206 bottom_right_x, bottom_right_y);
1207 *bottom_right_x += *top_left_x;
1208 *bottom_right_y += *top_left_y;
1211 #endif /* HAVE_WINDOW_SYSTEM */
1213 /***********************************************************************
1214 Utilities
1215 ***********************************************************************/
1217 /* Return the bottom y-position of the line the iterator IT is in.
1218 This can modify IT's settings. */
1221 line_bottom_y (struct it *it)
1223 int line_height = it->max_ascent + it->max_descent;
1224 int line_top_y = it->current_y;
1226 if (line_height == 0)
1228 if (last_height)
1229 line_height = last_height;
1230 else if (IT_CHARPOS (*it) < ZV)
1232 move_it_by_lines (it, 1);
1233 line_height = (it->max_ascent || it->max_descent
1234 ? it->max_ascent + it->max_descent
1235 : last_height);
1237 else
1239 struct glyph_row *row = it->glyph_row;
1241 /* Use the default character height. */
1242 it->glyph_row = NULL;
1243 it->what = IT_CHARACTER;
1244 it->c = ' ';
1245 it->len = 1;
1246 PRODUCE_GLYPHS (it);
1247 line_height = it->ascent + it->descent;
1248 it->glyph_row = row;
1252 return line_top_y + line_height;
1255 DEFUN ("line-pixel-height", Fline_pixel_height,
1256 Sline_pixel_height, 0, 0, 0,
1257 doc: /* Return height in pixels of text line in the selected window.
1259 Value is the height in pixels of the line at point. */)
1260 (void)
1262 struct it it;
1263 struct text_pos pt;
1264 struct window *w = XWINDOW (selected_window);
1266 SET_TEXT_POS (pt, PT, PT_BYTE);
1267 start_display (&it, w, pt);
1268 it.vpos = it.current_y = 0;
1269 last_height = 0;
1270 return make_number (line_bottom_y (&it));
1273 /* Return the default pixel height of text lines in window W. The
1274 value is the canonical height of the W frame's default font, plus
1275 any extra space required by the line-spacing variable or frame
1276 parameter.
1278 Implementation note: this ignores any line-spacing text properties
1279 put on the newline characters. This is because those properties
1280 only affect the _screen_ line ending in the newline (i.e., in a
1281 continued line, only the last screen line will be affected), which
1282 means only a small number of lines in a buffer can ever use this
1283 feature. Since this function is used to compute the default pixel
1284 equivalent of text lines in a window, we can safely ignore those
1285 few lines. For the same reasons, we ignore the line-height
1286 properties. */
1288 default_line_pixel_height (struct window *w)
1290 struct frame *f = WINDOW_XFRAME (w);
1291 int height = FRAME_LINE_HEIGHT (f);
1293 if (!FRAME_INITIAL_P (f) && BUFFERP (w->contents))
1295 struct buffer *b = XBUFFER (w->contents);
1296 Lisp_Object val = BVAR (b, extra_line_spacing);
1298 if (NILP (val))
1299 val = BVAR (&buffer_defaults, extra_line_spacing);
1300 if (!NILP (val))
1302 if (RANGED_INTEGERP (0, val, INT_MAX))
1303 height += XFASTINT (val);
1304 else if (FLOATP (val))
1306 int addon = XFLOAT_DATA (val) * height + 0.5;
1308 if (addon >= 0)
1309 height += addon;
1312 else
1313 height += f->extra_line_spacing;
1316 return height;
1319 /* Subroutine of pos_visible_p below. Extracts a display string, if
1320 any, from the display spec given as its argument. */
1321 static Lisp_Object
1322 string_from_display_spec (Lisp_Object spec)
1324 if (CONSP (spec))
1326 while (CONSP (spec))
1328 if (STRINGP (XCAR (spec)))
1329 return XCAR (spec);
1330 spec = XCDR (spec);
1333 else if (VECTORP (spec))
1335 ptrdiff_t i;
1337 for (i = 0; i < ASIZE (spec); i++)
1339 if (STRINGP (AREF (spec, i)))
1340 return AREF (spec, i);
1342 return Qnil;
1345 return spec;
1349 /* Limit insanely large values of W->hscroll on frame F to the largest
1350 value that will still prevent first_visible_x and last_visible_x of
1351 'struct it' from overflowing an int. */
1352 static int
1353 window_hscroll_limited (struct window *w, struct frame *f)
1355 ptrdiff_t window_hscroll = w->hscroll;
1356 int window_text_width = window_box_width (w, TEXT_AREA);
1357 int colwidth = FRAME_COLUMN_WIDTH (f);
1359 if (window_hscroll > (INT_MAX - window_text_width) / colwidth - 1)
1360 window_hscroll = (INT_MAX - window_text_width) / colwidth - 1;
1362 return window_hscroll;
1365 /* Return 1 if position CHARPOS is visible in window W.
1366 CHARPOS < 0 means return info about WINDOW_END position.
1367 If visible, set *X and *Y to pixel coordinates of top left corner.
1368 Set *RTOP and *RBOT to pixel height of an invisible area of glyph at POS.
1369 Set *ROWH and *VPOS to row's visible height and VPOS (row number). */
1372 pos_visible_p (struct window *w, ptrdiff_t charpos, int *x, int *y,
1373 int *rtop, int *rbot, int *rowh, int *vpos)
1375 struct it it;
1376 void *itdata = bidi_shelve_cache ();
1377 struct text_pos top;
1378 int visible_p = 0;
1379 struct buffer *old_buffer = NULL;
1381 if (FRAME_INITIAL_P (XFRAME (WINDOW_FRAME (w))))
1382 return visible_p;
1384 if (XBUFFER (w->contents) != current_buffer)
1386 old_buffer = current_buffer;
1387 set_buffer_internal_1 (XBUFFER (w->contents));
1390 SET_TEXT_POS_FROM_MARKER (top, w->start);
1391 /* Scrolling a minibuffer window via scroll bar when the echo area
1392 shows long text sometimes resets the minibuffer contents behind
1393 our backs. */
1394 if (CHARPOS (top) > ZV)
1395 SET_TEXT_POS (top, BEGV, BEGV_BYTE);
1397 /* Compute exact mode line heights. */
1398 if (WINDOW_WANTS_MODELINE_P (w))
1399 w->mode_line_height
1400 = display_mode_line (w, CURRENT_MODE_LINE_FACE_ID (w),
1401 BVAR (current_buffer, mode_line_format));
1403 if (WINDOW_WANTS_HEADER_LINE_P (w))
1404 w->header_line_height
1405 = display_mode_line (w, HEADER_LINE_FACE_ID,
1406 BVAR (current_buffer, header_line_format));
1408 start_display (&it, w, top);
1409 move_it_to (&it, charpos, -1, it.last_visible_y - 1, -1,
1410 (charpos >= 0 ? MOVE_TO_POS : 0) | MOVE_TO_Y);
1412 if (charpos >= 0
1413 && (((!it.bidi_p || it.bidi_it.scan_dir == 1)
1414 && IT_CHARPOS (it) >= charpos)
1415 /* When scanning backwards under bidi iteration, move_it_to
1416 stops at or _before_ CHARPOS, because it stops at or to
1417 the _right_ of the character at CHARPOS. */
1418 || (it.bidi_p && it.bidi_it.scan_dir == -1
1419 && IT_CHARPOS (it) <= charpos)))
1421 /* We have reached CHARPOS, or passed it. How the call to
1422 move_it_to can overshoot: (i) If CHARPOS is on invisible text
1423 or covered by a display property, move_it_to stops at the end
1424 of the invisible text, to the right of CHARPOS. (ii) If
1425 CHARPOS is in a display vector, move_it_to stops on its last
1426 glyph. */
1427 int top_x = it.current_x;
1428 int top_y = it.current_y;
1429 /* Calling line_bottom_y may change it.method, it.position, etc. */
1430 enum it_method it_method = it.method;
1431 int bottom_y = (last_height = 0, line_bottom_y (&it));
1432 int window_top_y = WINDOW_HEADER_LINE_HEIGHT (w);
1434 if (top_y < window_top_y)
1435 visible_p = bottom_y > window_top_y;
1436 else if (top_y < it.last_visible_y)
1437 visible_p = true;
1438 if (bottom_y >= it.last_visible_y
1439 && it.bidi_p && it.bidi_it.scan_dir == -1
1440 && IT_CHARPOS (it) < charpos)
1442 /* When the last line of the window is scanned backwards
1443 under bidi iteration, we could be duped into thinking
1444 that we have passed CHARPOS, when in fact move_it_to
1445 simply stopped short of CHARPOS because it reached
1446 last_visible_y. To see if that's what happened, we call
1447 move_it_to again with a slightly larger vertical limit,
1448 and see if it actually moved vertically; if it did, we
1449 didn't really reach CHARPOS, which is beyond window end. */
1450 struct it save_it = it;
1451 /* Why 10? because we don't know how many canonical lines
1452 will the height of the next line(s) be. So we guess. */
1453 int ten_more_lines = 10 * default_line_pixel_height (w);
1455 move_it_to (&it, charpos, -1, bottom_y + ten_more_lines, -1,
1456 MOVE_TO_POS | MOVE_TO_Y);
1457 if (it.current_y > top_y)
1458 visible_p = 0;
1460 it = save_it;
1462 if (visible_p)
1464 if (it_method == GET_FROM_DISPLAY_VECTOR)
1466 /* We stopped on the last glyph of a display vector.
1467 Try and recompute. Hack alert! */
1468 if (charpos < 2 || top.charpos >= charpos)
1469 top_x = it.glyph_row->x;
1470 else
1472 struct it it2, it2_prev;
1473 /* The idea is to get to the previous buffer
1474 position, consume the character there, and use
1475 the pixel coordinates we get after that. But if
1476 the previous buffer position is also displayed
1477 from a display vector, we need to consume all of
1478 the glyphs from that display vector. */
1479 start_display (&it2, w, top);
1480 move_it_to (&it2, charpos - 1, -1, -1, -1, MOVE_TO_POS);
1481 /* If we didn't get to CHARPOS - 1, there's some
1482 replacing display property at that position, and
1483 we stopped after it. That is exactly the place
1484 whose coordinates we want. */
1485 if (IT_CHARPOS (it2) != charpos - 1)
1486 it2_prev = it2;
1487 else
1489 /* Iterate until we get out of the display
1490 vector that displays the character at
1491 CHARPOS - 1. */
1492 do {
1493 get_next_display_element (&it2);
1494 PRODUCE_GLYPHS (&it2);
1495 it2_prev = it2;
1496 set_iterator_to_next (&it2, 1);
1497 } while (it2.method == GET_FROM_DISPLAY_VECTOR
1498 && IT_CHARPOS (it2) < charpos);
1500 if (ITERATOR_AT_END_OF_LINE_P (&it2_prev)
1501 || it2_prev.current_x > it2_prev.last_visible_x)
1502 top_x = it.glyph_row->x;
1503 else
1505 top_x = it2_prev.current_x;
1506 top_y = it2_prev.current_y;
1510 else if (IT_CHARPOS (it) != charpos)
1512 Lisp_Object cpos = make_number (charpos);
1513 Lisp_Object spec = Fget_char_property (cpos, Qdisplay, Qnil);
1514 Lisp_Object string = string_from_display_spec (spec);
1515 struct text_pos tpos;
1516 int replacing_spec_p;
1517 bool newline_in_string
1518 = (STRINGP (string)
1519 && memchr (SDATA (string), '\n', SBYTES (string)));
1521 SET_TEXT_POS (tpos, charpos, CHAR_TO_BYTE (charpos));
1522 replacing_spec_p
1523 = (!NILP (spec)
1524 && handle_display_spec (NULL, spec, Qnil, Qnil, &tpos,
1525 charpos, FRAME_WINDOW_P (it.f)));
1526 /* The tricky code below is needed because there's a
1527 discrepancy between move_it_to and how we set cursor
1528 when PT is at the beginning of a portion of text
1529 covered by a display property or an overlay with a
1530 display property, or the display line ends in a
1531 newline from a display string. move_it_to will stop
1532 _after_ such display strings, whereas
1533 set_cursor_from_row conspires with cursor_row_p to
1534 place the cursor on the first glyph produced from the
1535 display string. */
1537 /* We have overshoot PT because it is covered by a
1538 display property that replaces the text it covers.
1539 If the string includes embedded newlines, we are also
1540 in the wrong display line. Backtrack to the correct
1541 line, where the display property begins. */
1542 if (replacing_spec_p)
1544 Lisp_Object startpos, endpos;
1545 EMACS_INT start, end;
1546 struct it it3;
1547 int it3_moved;
1549 /* Find the first and the last buffer positions
1550 covered by the display string. */
1551 endpos =
1552 Fnext_single_char_property_change (cpos, Qdisplay,
1553 Qnil, Qnil);
1554 startpos =
1555 Fprevious_single_char_property_change (endpos, Qdisplay,
1556 Qnil, Qnil);
1557 start = XFASTINT (startpos);
1558 end = XFASTINT (endpos);
1559 /* Move to the last buffer position before the
1560 display property. */
1561 start_display (&it3, w, top);
1562 move_it_to (&it3, start - 1, -1, -1, -1, MOVE_TO_POS);
1563 /* Move forward one more line if the position before
1564 the display string is a newline or if it is the
1565 rightmost character on a line that is
1566 continued or word-wrapped. */
1567 if (it3.method == GET_FROM_BUFFER
1568 && (it3.c == '\n'
1569 || FETCH_BYTE (IT_BYTEPOS (it3)) == '\n'))
1570 move_it_by_lines (&it3, 1);
1571 else if (move_it_in_display_line_to (&it3, -1,
1572 it3.current_x
1573 + it3.pixel_width,
1574 MOVE_TO_X)
1575 == MOVE_LINE_CONTINUED)
1577 move_it_by_lines (&it3, 1);
1578 /* When we are under word-wrap, the #$@%!
1579 move_it_by_lines moves 2 lines, so we need to
1580 fix that up. */
1581 if (it3.line_wrap == WORD_WRAP)
1582 move_it_by_lines (&it3, -1);
1585 /* Record the vertical coordinate of the display
1586 line where we wound up. */
1587 top_y = it3.current_y;
1588 if (it3.bidi_p)
1590 /* When characters are reordered for display,
1591 the character displayed to the left of the
1592 display string could be _after_ the display
1593 property in the logical order. Use the
1594 smallest vertical position of these two. */
1595 start_display (&it3, w, top);
1596 move_it_to (&it3, end + 1, -1, -1, -1, MOVE_TO_POS);
1597 if (it3.current_y < top_y)
1598 top_y = it3.current_y;
1600 /* Move from the top of the window to the beginning
1601 of the display line where the display string
1602 begins. */
1603 start_display (&it3, w, top);
1604 move_it_to (&it3, -1, 0, top_y, -1, MOVE_TO_X | MOVE_TO_Y);
1605 /* If it3_moved stays zero after the 'while' loop
1606 below, that means we already were at a newline
1607 before the loop (e.g., the display string begins
1608 with a newline), so we don't need to (and cannot)
1609 inspect the glyphs of it3.glyph_row, because
1610 PRODUCE_GLYPHS will not produce anything for a
1611 newline, and thus it3.glyph_row stays at its
1612 stale content it got at top of the window. */
1613 it3_moved = 0;
1614 /* Finally, advance the iterator until we hit the
1615 first display element whose character position is
1616 CHARPOS, or until the first newline from the
1617 display string, which signals the end of the
1618 display line. */
1619 while (get_next_display_element (&it3))
1621 PRODUCE_GLYPHS (&it3);
1622 if (IT_CHARPOS (it3) == charpos
1623 || ITERATOR_AT_END_OF_LINE_P (&it3))
1624 break;
1625 it3_moved = 1;
1626 set_iterator_to_next (&it3, 0);
1628 top_x = it3.current_x - it3.pixel_width;
1629 /* Normally, we would exit the above loop because we
1630 found the display element whose character
1631 position is CHARPOS. For the contingency that we
1632 didn't, and stopped at the first newline from the
1633 display string, move back over the glyphs
1634 produced from the string, until we find the
1635 rightmost glyph not from the string. */
1636 if (it3_moved
1637 && newline_in_string
1638 && IT_CHARPOS (it3) != charpos && EQ (it3.object, string))
1640 struct glyph *g = it3.glyph_row->glyphs[TEXT_AREA]
1641 + it3.glyph_row->used[TEXT_AREA];
1643 while (EQ ((g - 1)->object, string))
1645 --g;
1646 top_x -= g->pixel_width;
1648 eassert (g < it3.glyph_row->glyphs[TEXT_AREA]
1649 + it3.glyph_row->used[TEXT_AREA]);
1654 *x = top_x;
1655 *y = max (top_y + max (0, it.max_ascent - it.ascent), window_top_y);
1656 *rtop = max (0, window_top_y - top_y);
1657 *rbot = max (0, bottom_y - it.last_visible_y);
1658 *rowh = max (0, (min (bottom_y, it.last_visible_y)
1659 - max (top_y, window_top_y)));
1660 *vpos = it.vpos;
1663 else
1665 /* We were asked to provide info about WINDOW_END. */
1666 struct it it2;
1667 void *it2data = NULL;
1669 SAVE_IT (it2, it, it2data);
1670 if (IT_CHARPOS (it) < ZV && FETCH_BYTE (IT_BYTEPOS (it)) != '\n')
1671 move_it_by_lines (&it, 1);
1672 if (charpos < IT_CHARPOS (it)
1673 || (it.what == IT_EOB && charpos == IT_CHARPOS (it)))
1675 visible_p = true;
1676 RESTORE_IT (&it2, &it2, it2data);
1677 move_it_to (&it2, charpos, -1, -1, -1, MOVE_TO_POS);
1678 *x = it2.current_x;
1679 *y = it2.current_y + it2.max_ascent - it2.ascent;
1680 *rtop = max (0, -it2.current_y);
1681 *rbot = max (0, ((it2.current_y + it2.max_ascent + it2.max_descent)
1682 - it.last_visible_y));
1683 *rowh = max (0, (min (it2.current_y + it2.max_ascent + it2.max_descent,
1684 it.last_visible_y)
1685 - max (it2.current_y,
1686 WINDOW_HEADER_LINE_HEIGHT (w))));
1687 *vpos = it2.vpos;
1689 else
1690 bidi_unshelve_cache (it2data, 1);
1692 bidi_unshelve_cache (itdata, 0);
1694 if (old_buffer)
1695 set_buffer_internal_1 (old_buffer);
1697 if (visible_p && w->hscroll > 0)
1698 *x -=
1699 window_hscroll_limited (w, WINDOW_XFRAME (w))
1700 * WINDOW_FRAME_COLUMN_WIDTH (w);
1702 #if 0
1703 /* Debugging code. */
1704 if (visible_p)
1705 fprintf (stderr, "+pv pt=%d vs=%d --> x=%d y=%d rt=%d rb=%d rh=%d vp=%d\n",
1706 charpos, w->vscroll, *x, *y, *rtop, *rbot, *rowh, *vpos);
1707 else
1708 fprintf (stderr, "-pv pt=%d vs=%d\n", charpos, w->vscroll);
1709 #endif
1711 return visible_p;
1715 /* Return the next character from STR. Return in *LEN the length of
1716 the character. This is like STRING_CHAR_AND_LENGTH but never
1717 returns an invalid character. If we find one, we return a `?', but
1718 with the length of the invalid character. */
1720 static int
1721 string_char_and_length (const unsigned char *str, int *len)
1723 int c;
1725 c = STRING_CHAR_AND_LENGTH (str, *len);
1726 if (!CHAR_VALID_P (c))
1727 /* We may not change the length here because other places in Emacs
1728 don't use this function, i.e. they silently accept invalid
1729 characters. */
1730 c = '?';
1732 return c;
1737 /* Given a position POS containing a valid character and byte position
1738 in STRING, return the position NCHARS ahead (NCHARS >= 0). */
1740 static struct text_pos
1741 string_pos_nchars_ahead (struct text_pos pos, Lisp_Object string, ptrdiff_t nchars)
1743 eassert (STRINGP (string) && nchars >= 0);
1745 if (STRING_MULTIBYTE (string))
1747 const unsigned char *p = SDATA (string) + BYTEPOS (pos);
1748 int len;
1750 while (nchars--)
1752 string_char_and_length (p, &len);
1753 p += len;
1754 CHARPOS (pos) += 1;
1755 BYTEPOS (pos) += len;
1758 else
1759 SET_TEXT_POS (pos, CHARPOS (pos) + nchars, BYTEPOS (pos) + nchars);
1761 return pos;
1765 /* Value is the text position, i.e. character and byte position,
1766 for character position CHARPOS in STRING. */
1768 static struct text_pos
1769 string_pos (ptrdiff_t charpos, Lisp_Object string)
1771 struct text_pos pos;
1772 eassert (STRINGP (string));
1773 eassert (charpos >= 0);
1774 SET_TEXT_POS (pos, charpos, string_char_to_byte (string, charpos));
1775 return pos;
1779 /* Value is a text position, i.e. character and byte position, for
1780 character position CHARPOS in C string S. MULTIBYTE_P non-zero
1781 means recognize multibyte characters. */
1783 static struct text_pos
1784 c_string_pos (ptrdiff_t charpos, const char *s, bool multibyte_p)
1786 struct text_pos pos;
1788 eassert (s != NULL);
1789 eassert (charpos >= 0);
1791 if (multibyte_p)
1793 int len;
1795 SET_TEXT_POS (pos, 0, 0);
1796 while (charpos--)
1798 string_char_and_length ((const unsigned char *) s, &len);
1799 s += len;
1800 CHARPOS (pos) += 1;
1801 BYTEPOS (pos) += len;
1804 else
1805 SET_TEXT_POS (pos, charpos, charpos);
1807 return pos;
1811 /* Value is the number of characters in C string S. MULTIBYTE_P
1812 non-zero means recognize multibyte characters. */
1814 static ptrdiff_t
1815 number_of_chars (const char *s, bool multibyte_p)
1817 ptrdiff_t nchars;
1819 if (multibyte_p)
1821 ptrdiff_t rest = strlen (s);
1822 int len;
1823 const unsigned char *p = (const unsigned char *) s;
1825 for (nchars = 0; rest > 0; ++nchars)
1827 string_char_and_length (p, &len);
1828 rest -= len, p += len;
1831 else
1832 nchars = strlen (s);
1834 return nchars;
1838 /* Compute byte position NEWPOS->bytepos corresponding to
1839 NEWPOS->charpos. POS is a known position in string STRING.
1840 NEWPOS->charpos must be >= POS.charpos. */
1842 static void
1843 compute_string_pos (struct text_pos *newpos, struct text_pos pos, Lisp_Object string)
1845 eassert (STRINGP (string));
1846 eassert (CHARPOS (*newpos) >= CHARPOS (pos));
1848 if (STRING_MULTIBYTE (string))
1849 *newpos = string_pos_nchars_ahead (pos, string,
1850 CHARPOS (*newpos) - CHARPOS (pos));
1851 else
1852 BYTEPOS (*newpos) = CHARPOS (*newpos);
1855 /* EXPORT:
1856 Return an estimation of the pixel height of mode or header lines on
1857 frame F. FACE_ID specifies what line's height to estimate. */
1860 estimate_mode_line_height (struct frame *f, enum face_id face_id)
1862 #ifdef HAVE_WINDOW_SYSTEM
1863 if (FRAME_WINDOW_P (f))
1865 int height = FONT_HEIGHT (FRAME_FONT (f));
1867 /* This function is called so early when Emacs starts that the face
1868 cache and mode line face are not yet initialized. */
1869 if (FRAME_FACE_CACHE (f))
1871 struct face *face = FACE_FROM_ID (f, face_id);
1872 if (face)
1874 if (face->font)
1875 height = FONT_HEIGHT (face->font);
1876 if (face->box_line_width > 0)
1877 height += 2 * face->box_line_width;
1881 return height;
1883 #endif
1885 return 1;
1888 /* Given a pixel position (PIX_X, PIX_Y) on frame F, return glyph
1889 co-ordinates in (*X, *Y). Set *BOUNDS to the rectangle that the
1890 glyph at X, Y occupies, if BOUNDS != 0. If NOCLIP is non-zero, do
1891 not force the value into range. */
1893 void
1894 pixel_to_glyph_coords (struct frame *f, register int pix_x, register int pix_y,
1895 int *x, int *y, NativeRectangle *bounds, int noclip)
1898 #ifdef HAVE_WINDOW_SYSTEM
1899 if (FRAME_WINDOW_P (f))
1901 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to round down
1902 even for negative values. */
1903 if (pix_x < 0)
1904 pix_x -= FRAME_COLUMN_WIDTH (f) - 1;
1905 if (pix_y < 0)
1906 pix_y -= FRAME_LINE_HEIGHT (f) - 1;
1908 pix_x = FRAME_PIXEL_X_TO_COL (f, pix_x);
1909 pix_y = FRAME_PIXEL_Y_TO_LINE (f, pix_y);
1911 if (bounds)
1912 STORE_NATIVE_RECT (*bounds,
1913 FRAME_COL_TO_PIXEL_X (f, pix_x),
1914 FRAME_LINE_TO_PIXEL_Y (f, pix_y),
1915 FRAME_COLUMN_WIDTH (f) - 1,
1916 FRAME_LINE_HEIGHT (f) - 1);
1918 /* PXW: Should we clip pixels before converting to columns/lines? */
1919 if (!noclip)
1921 if (pix_x < 0)
1922 pix_x = 0;
1923 else if (pix_x > FRAME_TOTAL_COLS (f))
1924 pix_x = FRAME_TOTAL_COLS (f);
1926 if (pix_y < 0)
1927 pix_y = 0;
1928 else if (pix_y > FRAME_LINES (f))
1929 pix_y = FRAME_LINES (f);
1932 #endif
1934 *x = pix_x;
1935 *y = pix_y;
1939 /* Find the glyph under window-relative coordinates X/Y in window W.
1940 Consider only glyphs from buffer text, i.e. no glyphs from overlay
1941 strings. Return in *HPOS and *VPOS the row and column number of
1942 the glyph found. Return in *AREA the glyph area containing X.
1943 Value is a pointer to the glyph found or null if X/Y is not on
1944 text, or we can't tell because W's current matrix is not up to
1945 date. */
1947 static struct glyph *
1948 x_y_to_hpos_vpos (struct window *w, int x, int y, int *hpos, int *vpos,
1949 int *dx, int *dy, int *area)
1951 struct glyph *glyph, *end;
1952 struct glyph_row *row = NULL;
1953 int x0, i;
1955 /* Find row containing Y. Give up if some row is not enabled. */
1956 for (i = 0; i < w->current_matrix->nrows; ++i)
1958 row = MATRIX_ROW (w->current_matrix, i);
1959 if (!row->enabled_p)
1960 return NULL;
1961 if (y >= row->y && y < MATRIX_ROW_BOTTOM_Y (row))
1962 break;
1965 *vpos = i;
1966 *hpos = 0;
1968 /* Give up if Y is not in the window. */
1969 if (i == w->current_matrix->nrows)
1970 return NULL;
1972 /* Get the glyph area containing X. */
1973 if (w->pseudo_window_p)
1975 *area = TEXT_AREA;
1976 x0 = 0;
1978 else
1980 if (x < window_box_left_offset (w, TEXT_AREA))
1982 *area = LEFT_MARGIN_AREA;
1983 x0 = window_box_left_offset (w, LEFT_MARGIN_AREA);
1985 else if (x < window_box_right_offset (w, TEXT_AREA))
1987 *area = TEXT_AREA;
1988 x0 = window_box_left_offset (w, TEXT_AREA) + min (row->x, 0);
1990 else
1992 *area = RIGHT_MARGIN_AREA;
1993 x0 = window_box_left_offset (w, RIGHT_MARGIN_AREA);
1997 /* Find glyph containing X. */
1998 glyph = row->glyphs[*area];
1999 end = glyph + row->used[*area];
2000 x -= x0;
2001 while (glyph < end && x >= glyph->pixel_width)
2003 x -= glyph->pixel_width;
2004 ++glyph;
2007 if (glyph == end)
2008 return NULL;
2010 if (dx)
2012 *dx = x;
2013 *dy = y - (row->y + row->ascent - glyph->ascent);
2016 *hpos = glyph - row->glyphs[*area];
2017 return glyph;
2020 /* Convert frame-relative x/y to coordinates relative to window W.
2021 Takes pseudo-windows into account. */
2023 static void
2024 frame_to_window_pixel_xy (struct window *w, int *x, int *y)
2026 if (w->pseudo_window_p)
2028 /* A pseudo-window is always full-width, and starts at the
2029 left edge of the frame, plus a frame border. */
2030 struct frame *f = XFRAME (w->frame);
2031 *x -= FRAME_INTERNAL_BORDER_WIDTH (f);
2032 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
2034 else
2036 *x -= WINDOW_LEFT_EDGE_X (w);
2037 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
2041 #ifdef HAVE_WINDOW_SYSTEM
2043 /* EXPORT:
2044 Return in RECTS[] at most N clipping rectangles for glyph string S.
2045 Return the number of stored rectangles. */
2048 get_glyph_string_clip_rects (struct glyph_string *s, NativeRectangle *rects, int n)
2050 XRectangle r;
2052 if (n <= 0)
2053 return 0;
2055 if (s->row->full_width_p)
2057 /* Draw full-width. X coordinates are relative to S->w->left_col. */
2058 r.x = WINDOW_LEFT_EDGE_X (s->w);
2059 if (s->row->mode_line_p)
2060 r.width = WINDOW_PIXEL_WIDTH (s->w) - WINDOW_RIGHT_DIVIDER_WIDTH (s->w);
2061 else
2062 r.width = WINDOW_PIXEL_WIDTH (s->w);
2064 /* Unless displaying a mode or menu bar line, which are always
2065 fully visible, clip to the visible part of the row. */
2066 if (s->w->pseudo_window_p)
2067 r.height = s->row->visible_height;
2068 else
2069 r.height = s->height;
2071 else
2073 /* This is a text line that may be partially visible. */
2074 r.x = window_box_left (s->w, s->area);
2075 r.width = window_box_width (s->w, s->area);
2076 r.height = s->row->visible_height;
2079 if (s->clip_head)
2080 if (r.x < s->clip_head->x)
2082 if (r.width >= s->clip_head->x - r.x)
2083 r.width -= s->clip_head->x - r.x;
2084 else
2085 r.width = 0;
2086 r.x = s->clip_head->x;
2088 if (s->clip_tail)
2089 if (r.x + r.width > s->clip_tail->x + s->clip_tail->background_width)
2091 if (s->clip_tail->x + s->clip_tail->background_width >= r.x)
2092 r.width = s->clip_tail->x + s->clip_tail->background_width - r.x;
2093 else
2094 r.width = 0;
2097 /* If S draws overlapping rows, it's sufficient to use the top and
2098 bottom of the window for clipping because this glyph string
2099 intentionally draws over other lines. */
2100 if (s->for_overlaps)
2102 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
2103 r.height = window_text_bottom_y (s->w) - r.y;
2105 /* Alas, the above simple strategy does not work for the
2106 environments with anti-aliased text: if the same text is
2107 drawn onto the same place multiple times, it gets thicker.
2108 If the overlap we are processing is for the erased cursor, we
2109 take the intersection with the rectangle of the cursor. */
2110 if (s->for_overlaps & OVERLAPS_ERASED_CURSOR)
2112 XRectangle rc, r_save = r;
2114 rc.x = WINDOW_TEXT_TO_FRAME_PIXEL_X (s->w, s->w->phys_cursor.x);
2115 rc.y = s->w->phys_cursor.y;
2116 rc.width = s->w->phys_cursor_width;
2117 rc.height = s->w->phys_cursor_height;
2119 x_intersect_rectangles (&r_save, &rc, &r);
2122 else
2124 /* Don't use S->y for clipping because it doesn't take partially
2125 visible lines into account. For example, it can be negative for
2126 partially visible lines at the top of a window. */
2127 if (!s->row->full_width_p
2128 && MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (s->w, s->row))
2129 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
2130 else
2131 r.y = max (0, s->row->y);
2134 r.y = WINDOW_TO_FRAME_PIXEL_Y (s->w, r.y);
2136 /* If drawing the cursor, don't let glyph draw outside its
2137 advertised boundaries. Cleartype does this under some circumstances. */
2138 if (s->hl == DRAW_CURSOR)
2140 struct glyph *glyph = s->first_glyph;
2141 int height, max_y;
2143 if (s->x > r.x)
2145 r.width -= s->x - r.x;
2146 r.x = s->x;
2148 r.width = min (r.width, glyph->pixel_width);
2150 /* If r.y is below window bottom, ensure that we still see a cursor. */
2151 height = min (glyph->ascent + glyph->descent,
2152 min (FRAME_LINE_HEIGHT (s->f), s->row->visible_height));
2153 max_y = window_text_bottom_y (s->w) - height;
2154 max_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, max_y);
2155 if (s->ybase - glyph->ascent > max_y)
2157 r.y = max_y;
2158 r.height = height;
2160 else
2162 /* Don't draw cursor glyph taller than our actual glyph. */
2163 height = max (FRAME_LINE_HEIGHT (s->f), glyph->ascent + glyph->descent);
2164 if (height < r.height)
2166 max_y = r.y + r.height;
2167 r.y = min (max_y, max (r.y, s->ybase + glyph->descent - height));
2168 r.height = min (max_y - r.y, height);
2173 if (s->row->clip)
2175 XRectangle r_save = r;
2177 if (! x_intersect_rectangles (&r_save, s->row->clip, &r))
2178 r.width = 0;
2181 if ((s->for_overlaps & OVERLAPS_BOTH) == 0
2182 || ((s->for_overlaps & OVERLAPS_BOTH) == OVERLAPS_BOTH && n == 1))
2184 #ifdef CONVERT_FROM_XRECT
2185 CONVERT_FROM_XRECT (r, *rects);
2186 #else
2187 *rects = r;
2188 #endif
2189 return 1;
2191 else
2193 /* If we are processing overlapping and allowed to return
2194 multiple clipping rectangles, we exclude the row of the glyph
2195 string from the clipping rectangle. This is to avoid drawing
2196 the same text on the environment with anti-aliasing. */
2197 #ifdef CONVERT_FROM_XRECT
2198 XRectangle rs[2];
2199 #else
2200 XRectangle *rs = rects;
2201 #endif
2202 int i = 0, row_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, s->row->y);
2204 if (s->for_overlaps & OVERLAPS_PRED)
2206 rs[i] = r;
2207 if (r.y + r.height > row_y)
2209 if (r.y < row_y)
2210 rs[i].height = row_y - r.y;
2211 else
2212 rs[i].height = 0;
2214 i++;
2216 if (s->for_overlaps & OVERLAPS_SUCC)
2218 rs[i] = r;
2219 if (r.y < row_y + s->row->visible_height)
2221 if (r.y + r.height > row_y + s->row->visible_height)
2223 rs[i].y = row_y + s->row->visible_height;
2224 rs[i].height = r.y + r.height - rs[i].y;
2226 else
2227 rs[i].height = 0;
2229 i++;
2232 n = i;
2233 #ifdef CONVERT_FROM_XRECT
2234 for (i = 0; i < n; i++)
2235 CONVERT_FROM_XRECT (rs[i], rects[i]);
2236 #endif
2237 return n;
2241 /* EXPORT:
2242 Return in *NR the clipping rectangle for glyph string S. */
2244 void
2245 get_glyph_string_clip_rect (struct glyph_string *s, NativeRectangle *nr)
2247 get_glyph_string_clip_rects (s, nr, 1);
2251 /* EXPORT:
2252 Return the position and height of the phys cursor in window W.
2253 Set w->phys_cursor_width to width of phys cursor.
2256 void
2257 get_phys_cursor_geometry (struct window *w, struct glyph_row *row,
2258 struct glyph *glyph, int *xp, int *yp, int *heightp)
2260 struct frame *f = XFRAME (WINDOW_FRAME (w));
2261 int x, y, wd, h, h0, y0;
2263 /* Compute the width of the rectangle to draw. If on a stretch
2264 glyph, and `x-stretch-block-cursor' is nil, don't draw a
2265 rectangle as wide as the glyph, but use a canonical character
2266 width instead. */
2267 wd = glyph->pixel_width - 1;
2268 #if defined (HAVE_NTGUI) || defined (HAVE_NS)
2269 wd++; /* Why? */
2270 #endif
2272 x = w->phys_cursor.x;
2273 if (x < 0)
2275 wd += x;
2276 x = 0;
2279 if (glyph->type == STRETCH_GLYPH
2280 && !x_stretch_cursor_p)
2281 wd = min (FRAME_COLUMN_WIDTH (f), wd);
2282 w->phys_cursor_width = wd;
2284 y = w->phys_cursor.y + row->ascent - glyph->ascent;
2286 /* If y is below window bottom, ensure that we still see a cursor. */
2287 h0 = min (FRAME_LINE_HEIGHT (f), row->visible_height);
2289 h = max (h0, glyph->ascent + glyph->descent);
2290 h0 = min (h0, glyph->ascent + glyph->descent);
2292 y0 = WINDOW_HEADER_LINE_HEIGHT (w);
2293 if (y < y0)
2295 h = max (h - (y0 - y) + 1, h0);
2296 y = y0 - 1;
2298 else
2300 y0 = window_text_bottom_y (w) - h0;
2301 if (y > y0)
2303 h += y - y0;
2304 y = y0;
2308 *xp = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, x);
2309 *yp = WINDOW_TO_FRAME_PIXEL_Y (w, y);
2310 *heightp = h;
2314 * Remember which glyph the mouse is over.
2317 void
2318 remember_mouse_glyph (struct frame *f, int gx, int gy, NativeRectangle *rect)
2320 Lisp_Object window;
2321 struct window *w;
2322 struct glyph_row *r, *gr, *end_row;
2323 enum window_part part;
2324 enum glyph_row_area area;
2325 int x, y, width, height;
2327 /* Try to determine frame pixel position and size of the glyph under
2328 frame pixel coordinates X/Y on frame F. */
2330 if (window_resize_pixelwise)
2332 width = height = 1;
2333 goto virtual_glyph;
2335 else if (!f->glyphs_initialized_p
2336 || (window = window_from_coordinates (f, gx, gy, &part, 0),
2337 NILP (window)))
2339 width = FRAME_SMALLEST_CHAR_WIDTH (f);
2340 height = FRAME_SMALLEST_FONT_HEIGHT (f);
2341 goto virtual_glyph;
2344 w = XWINDOW (window);
2345 width = WINDOW_FRAME_COLUMN_WIDTH (w);
2346 height = WINDOW_FRAME_LINE_HEIGHT (w);
2348 x = window_relative_x_coord (w, part, gx);
2349 y = gy - WINDOW_TOP_EDGE_Y (w);
2351 r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
2352 end_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
2354 if (w->pseudo_window_p)
2356 area = TEXT_AREA;
2357 part = ON_MODE_LINE; /* Don't adjust margin. */
2358 goto text_glyph;
2361 switch (part)
2363 case ON_LEFT_MARGIN:
2364 area = LEFT_MARGIN_AREA;
2365 goto text_glyph;
2367 case ON_RIGHT_MARGIN:
2368 area = RIGHT_MARGIN_AREA;
2369 goto text_glyph;
2371 case ON_HEADER_LINE:
2372 case ON_MODE_LINE:
2373 gr = (part == ON_HEADER_LINE
2374 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
2375 : MATRIX_MODE_LINE_ROW (w->current_matrix));
2376 gy = gr->y;
2377 area = TEXT_AREA;
2378 goto text_glyph_row_found;
2380 case ON_TEXT:
2381 area = TEXT_AREA;
2383 text_glyph:
2384 gr = 0; gy = 0;
2385 for (; r <= end_row && r->enabled_p; ++r)
2386 if (r->y + r->height > y)
2388 gr = r; gy = r->y;
2389 break;
2392 text_glyph_row_found:
2393 if (gr && gy <= y)
2395 struct glyph *g = gr->glyphs[area];
2396 struct glyph *end = g + gr->used[area];
2398 height = gr->height;
2399 for (gx = gr->x; g < end; gx += g->pixel_width, ++g)
2400 if (gx + g->pixel_width > x)
2401 break;
2403 if (g < end)
2405 if (g->type == IMAGE_GLYPH)
2407 /* Don't remember when mouse is over image, as
2408 image may have hot-spots. */
2409 STORE_NATIVE_RECT (*rect, 0, 0, 0, 0);
2410 return;
2412 width = g->pixel_width;
2414 else
2416 /* Use nominal char spacing at end of line. */
2417 x -= gx;
2418 gx += (x / width) * width;
2421 if (part != ON_MODE_LINE && part != ON_HEADER_LINE)
2423 gx += window_box_left_offset (w, area);
2424 /* Don't expand over the modeline to make sure the vertical
2425 drag cursor is shown early enough. */
2426 height = min (height,
2427 max (0, WINDOW_BOX_HEIGHT_NO_MODE_LINE (w) - gy));
2430 else
2432 /* Use nominal line height at end of window. */
2433 gx = (x / width) * width;
2434 y -= gy;
2435 gy += (y / height) * height;
2436 if (part != ON_MODE_LINE && part != ON_HEADER_LINE)
2437 /* See comment above. */
2438 height = min (height,
2439 max (0, WINDOW_BOX_HEIGHT_NO_MODE_LINE (w) - gy));
2441 break;
2443 case ON_LEFT_FRINGE:
2444 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2445 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w)
2446 : window_box_right_offset (w, LEFT_MARGIN_AREA));
2447 width = WINDOW_LEFT_FRINGE_WIDTH (w);
2448 goto row_glyph;
2450 case ON_RIGHT_FRINGE:
2451 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2452 ? window_box_right_offset (w, RIGHT_MARGIN_AREA)
2453 : window_box_right_offset (w, TEXT_AREA));
2454 if (WINDOW_RIGHT_DIVIDER_WIDTH (w) == 0
2455 && !WINDOW_HAS_VERTICAL_SCROLL_BAR (w)
2456 && !WINDOW_RIGHTMOST_P (w))
2457 if (gx < WINDOW_PIXEL_WIDTH (w) - width)
2458 /* Make sure the vertical border can get her own glyph to the
2459 right of the one we build here. */
2460 width = WINDOW_RIGHT_FRINGE_WIDTH (w) - width;
2461 else
2462 width = WINDOW_PIXEL_WIDTH (w) - gx;
2463 else
2464 width = WINDOW_RIGHT_FRINGE_WIDTH (w);
2466 goto row_glyph;
2468 case ON_VERTICAL_BORDER:
2469 gx = WINDOW_PIXEL_WIDTH (w) - width;
2470 goto row_glyph;
2472 case ON_SCROLL_BAR:
2473 gx = (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w)
2475 : (window_box_right_offset (w, RIGHT_MARGIN_AREA)
2476 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2477 ? WINDOW_RIGHT_FRINGE_WIDTH (w)
2478 : 0)));
2479 width = WINDOW_SCROLL_BAR_AREA_WIDTH (w);
2481 row_glyph:
2482 gr = 0, gy = 0;
2483 for (; r <= end_row && r->enabled_p; ++r)
2484 if (r->y + r->height > y)
2486 gr = r; gy = r->y;
2487 break;
2490 if (gr && gy <= y)
2491 height = gr->height;
2492 else
2494 /* Use nominal line height at end of window. */
2495 y -= gy;
2496 gy += (y / height) * height;
2498 break;
2500 case ON_RIGHT_DIVIDER:
2501 gx = WINDOW_PIXEL_WIDTH (w) - WINDOW_RIGHT_DIVIDER_WIDTH (w);
2502 width = WINDOW_RIGHT_DIVIDER_WIDTH (w);
2503 gy = 0;
2504 /* The bottom divider prevails. */
2505 height = WINDOW_PIXEL_HEIGHT (w) - WINDOW_BOTTOM_DIVIDER_WIDTH (w);
2506 goto add_edge;;
2508 case ON_BOTTOM_DIVIDER:
2509 gx = 0;
2510 width = WINDOW_PIXEL_WIDTH (w);
2511 gy = WINDOW_PIXEL_HEIGHT (w) - WINDOW_BOTTOM_DIVIDER_WIDTH (w);
2512 height = WINDOW_BOTTOM_DIVIDER_WIDTH (w);
2513 goto add_edge;
2515 default:
2517 virtual_glyph:
2518 /* If there is no glyph under the mouse, then we divide the screen
2519 into a grid of the smallest glyph in the frame, and use that
2520 as our "glyph". */
2522 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to
2523 round down even for negative values. */
2524 if (gx < 0)
2525 gx -= width - 1;
2526 if (gy < 0)
2527 gy -= height - 1;
2529 gx = (gx / width) * width;
2530 gy = (gy / height) * height;
2532 goto store_rect;
2535 add_edge:
2536 gx += WINDOW_LEFT_EDGE_X (w);
2537 gy += WINDOW_TOP_EDGE_Y (w);
2539 store_rect:
2540 STORE_NATIVE_RECT (*rect, gx, gy, width, height);
2542 /* Visible feedback for debugging. */
2543 #if 0
2544 #if HAVE_X_WINDOWS
2545 XDrawRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
2546 f->output_data.x->normal_gc,
2547 gx, gy, width, height);
2548 #endif
2549 #endif
2553 #endif /* HAVE_WINDOW_SYSTEM */
2555 static void
2556 adjust_window_ends (struct window *w, struct glyph_row *row, bool current)
2558 eassert (w);
2559 w->window_end_pos = Z - MATRIX_ROW_END_CHARPOS (row);
2560 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
2561 w->window_end_vpos
2562 = MATRIX_ROW_VPOS (row, current ? w->current_matrix : w->desired_matrix);
2565 /***********************************************************************
2566 Lisp form evaluation
2567 ***********************************************************************/
2569 /* Error handler for safe_eval and safe_call. */
2571 static Lisp_Object
2572 safe_eval_handler (Lisp_Object arg, ptrdiff_t nargs, Lisp_Object *args)
2574 add_to_log ("Error during redisplay: %S signaled %S",
2575 Flist (nargs, args), arg);
2576 return Qnil;
2579 /* Call function FUNC with the rest of NARGS - 1 arguments
2580 following. Return the result, or nil if something went
2581 wrong. Prevent redisplay during the evaluation. */
2583 Lisp_Object
2584 safe_call (ptrdiff_t nargs, Lisp_Object func, ...)
2586 Lisp_Object val;
2588 if (inhibit_eval_during_redisplay)
2589 val = Qnil;
2590 else
2592 va_list ap;
2593 ptrdiff_t i;
2594 ptrdiff_t count = SPECPDL_INDEX ();
2595 struct gcpro gcpro1;
2596 Lisp_Object *args = alloca (nargs * word_size);
2598 args[0] = func;
2599 va_start (ap, func);
2600 for (i = 1; i < nargs; i++)
2601 args[i] = va_arg (ap, Lisp_Object);
2602 va_end (ap);
2604 GCPRO1 (args[0]);
2605 gcpro1.nvars = nargs;
2606 specbind (Qinhibit_redisplay, Qt);
2607 /* Use Qt to ensure debugger does not run,
2608 so there is no possibility of wanting to redisplay. */
2609 val = internal_condition_case_n (Ffuncall, nargs, args, Qt,
2610 safe_eval_handler);
2611 UNGCPRO;
2612 val = unbind_to (count, val);
2615 return val;
2619 /* Call function FN with one argument ARG.
2620 Return the result, or nil if something went wrong. */
2622 Lisp_Object
2623 safe_call1 (Lisp_Object fn, Lisp_Object arg)
2625 return safe_call (2, fn, arg);
2628 static Lisp_Object Qeval;
2630 Lisp_Object
2631 safe_eval (Lisp_Object sexpr)
2633 return safe_call1 (Qeval, sexpr);
2636 /* Call function FN with two arguments ARG1 and ARG2.
2637 Return the result, or nil if something went wrong. */
2639 Lisp_Object
2640 safe_call2 (Lisp_Object fn, Lisp_Object arg1, Lisp_Object arg2)
2642 return safe_call (3, fn, arg1, arg2);
2647 /***********************************************************************
2648 Debugging
2649 ***********************************************************************/
2651 #if 0
2653 /* Define CHECK_IT to perform sanity checks on iterators.
2654 This is for debugging. It is too slow to do unconditionally. */
2656 static void
2657 check_it (struct it *it)
2659 if (it->method == GET_FROM_STRING)
2661 eassert (STRINGP (it->string));
2662 eassert (IT_STRING_CHARPOS (*it) >= 0);
2664 else
2666 eassert (IT_STRING_CHARPOS (*it) < 0);
2667 if (it->method == GET_FROM_BUFFER)
2669 /* Check that character and byte positions agree. */
2670 eassert (IT_CHARPOS (*it) == BYTE_TO_CHAR (IT_BYTEPOS (*it)));
2674 if (it->dpvec)
2675 eassert (it->current.dpvec_index >= 0);
2676 else
2677 eassert (it->current.dpvec_index < 0);
2680 #define CHECK_IT(IT) check_it ((IT))
2682 #else /* not 0 */
2684 #define CHECK_IT(IT) (void) 0
2686 #endif /* not 0 */
2689 #if defined GLYPH_DEBUG && defined ENABLE_CHECKING
2691 /* Check that the window end of window W is what we expect it
2692 to be---the last row in the current matrix displaying text. */
2694 static void
2695 check_window_end (struct window *w)
2697 if (!MINI_WINDOW_P (w) && w->window_end_valid)
2699 struct glyph_row *row;
2700 eassert ((row = MATRIX_ROW (w->current_matrix, w->window_end_vpos),
2701 !row->enabled_p
2702 || MATRIX_ROW_DISPLAYS_TEXT_P (row)
2703 || MATRIX_ROW_VPOS (row, w->current_matrix) == 0));
2707 #define CHECK_WINDOW_END(W) check_window_end ((W))
2709 #else
2711 #define CHECK_WINDOW_END(W) (void) 0
2713 #endif /* GLYPH_DEBUG and ENABLE_CHECKING */
2715 /***********************************************************************
2716 Iterator initialization
2717 ***********************************************************************/
2719 /* Initialize IT for displaying current_buffer in window W, starting
2720 at character position CHARPOS. CHARPOS < 0 means that no buffer
2721 position is specified which is useful when the iterator is assigned
2722 a position later. BYTEPOS is the byte position corresponding to
2723 CHARPOS.
2725 If ROW is not null, calls to produce_glyphs with IT as parameter
2726 will produce glyphs in that row.
2728 BASE_FACE_ID is the id of a base face to use. It must be one of
2729 DEFAULT_FACE_ID for normal text, MODE_LINE_FACE_ID,
2730 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID for displaying
2731 mode lines, or TOOL_BAR_FACE_ID for displaying the tool-bar.
2733 If ROW is null and BASE_FACE_ID is equal to MODE_LINE_FACE_ID,
2734 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID, the iterator
2735 will be initialized to use the corresponding mode line glyph row of
2736 the desired matrix of W. */
2738 void
2739 init_iterator (struct it *it, struct window *w,
2740 ptrdiff_t charpos, ptrdiff_t bytepos,
2741 struct glyph_row *row, enum face_id base_face_id)
2743 enum face_id remapped_base_face_id = base_face_id;
2745 /* Some precondition checks. */
2746 eassert (w != NULL && it != NULL);
2747 eassert (charpos < 0 || (charpos >= BUF_BEG (current_buffer)
2748 && charpos <= ZV));
2750 /* If face attributes have been changed since the last redisplay,
2751 free realized faces now because they depend on face definitions
2752 that might have changed. Don't free faces while there might be
2753 desired matrices pending which reference these faces. */
2754 if (face_change_count && !inhibit_free_realized_faces)
2756 face_change_count = 0;
2757 free_all_realized_faces (Qnil);
2760 /* Perhaps remap BASE_FACE_ID to a user-specified alternative. */
2761 if (! NILP (Vface_remapping_alist))
2762 remapped_base_face_id
2763 = lookup_basic_face (XFRAME (w->frame), base_face_id);
2765 /* Use one of the mode line rows of W's desired matrix if
2766 appropriate. */
2767 if (row == NULL)
2769 if (base_face_id == MODE_LINE_FACE_ID
2770 || base_face_id == MODE_LINE_INACTIVE_FACE_ID)
2771 row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
2772 else if (base_face_id == HEADER_LINE_FACE_ID)
2773 row = MATRIX_HEADER_LINE_ROW (w->desired_matrix);
2776 /* Clear IT. */
2777 memset (it, 0, sizeof *it);
2778 it->current.overlay_string_index = -1;
2779 it->current.dpvec_index = -1;
2780 it->base_face_id = remapped_base_face_id;
2781 it->string = Qnil;
2782 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
2783 it->paragraph_embedding = L2R;
2784 it->bidi_it.string.lstring = Qnil;
2785 it->bidi_it.string.s = NULL;
2786 it->bidi_it.string.bufpos = 0;
2787 it->bidi_it.w = w;
2789 /* The window in which we iterate over current_buffer: */
2790 XSETWINDOW (it->window, w);
2791 it->w = w;
2792 it->f = XFRAME (w->frame);
2794 it->cmp_it.id = -1;
2796 /* Extra space between lines (on window systems only). */
2797 if (base_face_id == DEFAULT_FACE_ID
2798 && FRAME_WINDOW_P (it->f))
2800 if (NATNUMP (BVAR (current_buffer, extra_line_spacing)))
2801 it->extra_line_spacing = XFASTINT (BVAR (current_buffer, extra_line_spacing));
2802 else if (FLOATP (BVAR (current_buffer, extra_line_spacing)))
2803 it->extra_line_spacing = (XFLOAT_DATA (BVAR (current_buffer, extra_line_spacing))
2804 * FRAME_LINE_HEIGHT (it->f));
2805 else if (it->f->extra_line_spacing > 0)
2806 it->extra_line_spacing = it->f->extra_line_spacing;
2807 it->max_extra_line_spacing = 0;
2810 /* If realized faces have been removed, e.g. because of face
2811 attribute changes of named faces, recompute them. When running
2812 in batch mode, the face cache of the initial frame is null. If
2813 we happen to get called, make a dummy face cache. */
2814 if (FRAME_FACE_CACHE (it->f) == NULL)
2815 init_frame_faces (it->f);
2816 if (FRAME_FACE_CACHE (it->f)->used == 0)
2817 recompute_basic_faces (it->f);
2819 /* Current value of the `slice', `space-width', and 'height' properties. */
2820 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
2821 it->space_width = Qnil;
2822 it->font_height = Qnil;
2823 it->override_ascent = -1;
2825 /* Are control characters displayed as `^C'? */
2826 it->ctl_arrow_p = !NILP (BVAR (current_buffer, ctl_arrow));
2828 /* -1 means everything between a CR and the following line end
2829 is invisible. >0 means lines indented more than this value are
2830 invisible. */
2831 it->selective = (INTEGERP (BVAR (current_buffer, selective_display))
2832 ? (clip_to_bounds
2833 (-1, XINT (BVAR (current_buffer, selective_display)),
2834 PTRDIFF_MAX))
2835 : (!NILP (BVAR (current_buffer, selective_display))
2836 ? -1 : 0));
2837 it->selective_display_ellipsis_p
2838 = !NILP (BVAR (current_buffer, selective_display_ellipses));
2840 /* Display table to use. */
2841 it->dp = window_display_table (w);
2843 /* Are multibyte characters enabled in current_buffer? */
2844 it->multibyte_p = !NILP (BVAR (current_buffer, enable_multibyte_characters));
2846 /* Get the position at which the redisplay_end_trigger hook should
2847 be run, if it is to be run at all. */
2848 if (MARKERP (w->redisplay_end_trigger)
2849 && XMARKER (w->redisplay_end_trigger)->buffer != 0)
2850 it->redisplay_end_trigger_charpos
2851 = marker_position (w->redisplay_end_trigger);
2852 else if (INTEGERP (w->redisplay_end_trigger))
2853 it->redisplay_end_trigger_charpos
2854 = clip_to_bounds (PTRDIFF_MIN, XINT (w->redisplay_end_trigger),
2855 PTRDIFF_MAX);
2857 it->tab_width = SANE_TAB_WIDTH (current_buffer);
2859 /* Are lines in the display truncated? */
2860 if (base_face_id != DEFAULT_FACE_ID
2861 || it->w->hscroll
2862 || (! WINDOW_FULL_WIDTH_P (it->w)
2863 && ((!NILP (Vtruncate_partial_width_windows)
2864 && !INTEGERP (Vtruncate_partial_width_windows))
2865 || (INTEGERP (Vtruncate_partial_width_windows)
2866 /* PXW: Shall we do something about this? */
2867 && (WINDOW_TOTAL_COLS (it->w)
2868 < XINT (Vtruncate_partial_width_windows))))))
2869 it->line_wrap = TRUNCATE;
2870 else if (NILP (BVAR (current_buffer, truncate_lines)))
2871 it->line_wrap = NILP (BVAR (current_buffer, word_wrap))
2872 ? WINDOW_WRAP : WORD_WRAP;
2873 else
2874 it->line_wrap = TRUNCATE;
2876 /* Get dimensions of truncation and continuation glyphs. These are
2877 displayed as fringe bitmaps under X, but we need them for such
2878 frames when the fringes are turned off. But leave the dimensions
2879 zero for tooltip frames, as these glyphs look ugly there and also
2880 sabotage calculations of tooltip dimensions in x-show-tip. */
2881 #ifdef HAVE_WINDOW_SYSTEM
2882 if (!(FRAME_WINDOW_P (it->f)
2883 && FRAMEP (tip_frame)
2884 && it->f == XFRAME (tip_frame)))
2885 #endif
2887 if (it->line_wrap == TRUNCATE)
2889 /* We will need the truncation glyph. */
2890 eassert (it->glyph_row == NULL);
2891 produce_special_glyphs (it, IT_TRUNCATION);
2892 it->truncation_pixel_width = it->pixel_width;
2894 else
2896 /* We will need the continuation glyph. */
2897 eassert (it->glyph_row == NULL);
2898 produce_special_glyphs (it, IT_CONTINUATION);
2899 it->continuation_pixel_width = it->pixel_width;
2903 /* Reset these values to zero because the produce_special_glyphs
2904 above has changed them. */
2905 it->pixel_width = it->ascent = it->descent = 0;
2906 it->phys_ascent = it->phys_descent = 0;
2908 /* Set this after getting the dimensions of truncation and
2909 continuation glyphs, so that we don't produce glyphs when calling
2910 produce_special_glyphs, above. */
2911 it->glyph_row = row;
2912 it->area = TEXT_AREA;
2914 /* Forget any previous info about this row being reversed. */
2915 if (it->glyph_row)
2916 it->glyph_row->reversed_p = 0;
2918 /* Get the dimensions of the display area. The display area
2919 consists of the visible window area plus a horizontally scrolled
2920 part to the left of the window. All x-values are relative to the
2921 start of this total display area. */
2922 if (base_face_id != DEFAULT_FACE_ID)
2924 /* Mode lines, menu bar in terminal frames. */
2925 it->first_visible_x = 0;
2926 it->last_visible_x = WINDOW_PIXEL_WIDTH (w);
2928 else
2930 it->first_visible_x
2931 = window_hscroll_limited (it->w, it->f) * FRAME_COLUMN_WIDTH (it->f);
2932 it->last_visible_x = (it->first_visible_x
2933 + window_box_width (w, TEXT_AREA));
2935 /* If we truncate lines, leave room for the truncation glyph(s) at
2936 the right margin. Otherwise, leave room for the continuation
2937 glyph(s). Done only if the window has no fringes. Since we
2938 don't know at this point whether there will be any R2L lines in
2939 the window, we reserve space for truncation/continuation glyphs
2940 even if only one of the fringes is absent. */
2941 if (WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0
2942 || (it->bidi_p && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0))
2944 if (it->line_wrap == TRUNCATE)
2945 it->last_visible_x -= it->truncation_pixel_width;
2946 else
2947 it->last_visible_x -= it->continuation_pixel_width;
2950 it->header_line_p = WINDOW_WANTS_HEADER_LINE_P (w);
2951 it->current_y = WINDOW_HEADER_LINE_HEIGHT (w) + w->vscroll;
2954 /* Leave room for a border glyph. */
2955 if (!FRAME_WINDOW_P (it->f)
2956 && !WINDOW_RIGHTMOST_P (it->w))
2957 it->last_visible_x -= 1;
2959 it->last_visible_y = window_text_bottom_y (w);
2961 /* For mode lines and alike, arrange for the first glyph having a
2962 left box line if the face specifies a box. */
2963 if (base_face_id != DEFAULT_FACE_ID)
2965 struct face *face;
2967 it->face_id = remapped_base_face_id;
2969 /* If we have a boxed mode line, make the first character appear
2970 with a left box line. */
2971 face = FACE_FROM_ID (it->f, remapped_base_face_id);
2972 if (face && face->box != FACE_NO_BOX)
2973 it->start_of_box_run_p = true;
2976 /* If a buffer position was specified, set the iterator there,
2977 getting overlays and face properties from that position. */
2978 if (charpos >= BUF_BEG (current_buffer))
2980 it->end_charpos = ZV;
2981 eassert (charpos == BYTE_TO_CHAR (bytepos));
2982 IT_CHARPOS (*it) = charpos;
2983 IT_BYTEPOS (*it) = bytepos;
2985 /* We will rely on `reseat' to set this up properly, via
2986 handle_face_prop. */
2987 it->face_id = it->base_face_id;
2989 it->start = it->current;
2990 /* Do we need to reorder bidirectional text? Not if this is a
2991 unibyte buffer: by definition, none of the single-byte
2992 characters are strong R2L, so no reordering is needed. And
2993 bidi.c doesn't support unibyte buffers anyway. Also, don't
2994 reorder while we are loading loadup.el, since the tables of
2995 character properties needed for reordering are not yet
2996 available. */
2997 it->bidi_p =
2998 NILP (Vpurify_flag)
2999 && !NILP (BVAR (current_buffer, bidi_display_reordering))
3000 && it->multibyte_p;
3002 /* If we are to reorder bidirectional text, init the bidi
3003 iterator. */
3004 if (it->bidi_p)
3006 /* Note the paragraph direction that this buffer wants to
3007 use. */
3008 if (EQ (BVAR (current_buffer, bidi_paragraph_direction),
3009 Qleft_to_right))
3010 it->paragraph_embedding = L2R;
3011 else if (EQ (BVAR (current_buffer, bidi_paragraph_direction),
3012 Qright_to_left))
3013 it->paragraph_embedding = R2L;
3014 else
3015 it->paragraph_embedding = NEUTRAL_DIR;
3016 bidi_unshelve_cache (NULL, 0);
3017 bidi_init_it (charpos, IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
3018 &it->bidi_it);
3021 /* Compute faces etc. */
3022 reseat (it, it->current.pos, 1);
3025 CHECK_IT (it);
3029 /* Initialize IT for the display of window W with window start POS. */
3031 void
3032 start_display (struct it *it, struct window *w, struct text_pos pos)
3034 struct glyph_row *row;
3035 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
3037 row = w->desired_matrix->rows + first_vpos;
3038 init_iterator (it, w, CHARPOS (pos), BYTEPOS (pos), row, DEFAULT_FACE_ID);
3039 it->first_vpos = first_vpos;
3041 /* Don't reseat to previous visible line start if current start
3042 position is in a string or image. */
3043 if (it->method == GET_FROM_BUFFER && it->line_wrap != TRUNCATE)
3045 int start_at_line_beg_p;
3046 int first_y = it->current_y;
3048 /* If window start is not at a line start, skip forward to POS to
3049 get the correct continuation lines width. */
3050 start_at_line_beg_p = (CHARPOS (pos) == BEGV
3051 || FETCH_BYTE (BYTEPOS (pos) - 1) == '\n');
3052 if (!start_at_line_beg_p)
3054 int new_x;
3056 reseat_at_previous_visible_line_start (it);
3057 move_it_to (it, CHARPOS (pos), -1, -1, -1, MOVE_TO_POS);
3059 new_x = it->current_x + it->pixel_width;
3061 /* If lines are continued, this line may end in the middle
3062 of a multi-glyph character (e.g. a control character
3063 displayed as \003, or in the middle of an overlay
3064 string). In this case move_it_to above will not have
3065 taken us to the start of the continuation line but to the
3066 end of the continued line. */
3067 if (it->current_x > 0
3068 && it->line_wrap != TRUNCATE /* Lines are continued. */
3069 && (/* And glyph doesn't fit on the line. */
3070 new_x > it->last_visible_x
3071 /* Or it fits exactly and we're on a window
3072 system frame. */
3073 || (new_x == it->last_visible_x
3074 && FRAME_WINDOW_P (it->f)
3075 && ((it->bidi_p && it->bidi_it.paragraph_dir == R2L)
3076 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
3077 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)))))
3079 if ((it->current.dpvec_index >= 0
3080 || it->current.overlay_string_index >= 0)
3081 /* If we are on a newline from a display vector or
3082 overlay string, then we are already at the end of
3083 a screen line; no need to go to the next line in
3084 that case, as this line is not really continued.
3085 (If we do go to the next line, C-e will not DTRT.) */
3086 && it->c != '\n')
3088 set_iterator_to_next (it, 1);
3089 move_it_in_display_line_to (it, -1, -1, 0);
3092 it->continuation_lines_width += it->current_x;
3094 /* If the character at POS is displayed via a display
3095 vector, move_it_to above stops at the final glyph of
3096 IT->dpvec. To make the caller redisplay that character
3097 again (a.k.a. start at POS), we need to reset the
3098 dpvec_index to the beginning of IT->dpvec. */
3099 else if (it->current.dpvec_index >= 0)
3100 it->current.dpvec_index = 0;
3102 /* We're starting a new display line, not affected by the
3103 height of the continued line, so clear the appropriate
3104 fields in the iterator structure. */
3105 it->max_ascent = it->max_descent = 0;
3106 it->max_phys_ascent = it->max_phys_descent = 0;
3108 it->current_y = first_y;
3109 it->vpos = 0;
3110 it->current_x = it->hpos = 0;
3116 /* Return 1 if POS is a position in ellipses displayed for invisible
3117 text. W is the window we display, for text property lookup. */
3119 static int
3120 in_ellipses_for_invisible_text_p (struct display_pos *pos, struct window *w)
3122 Lisp_Object prop, window;
3123 int ellipses_p = 0;
3124 ptrdiff_t charpos = CHARPOS (pos->pos);
3126 /* If POS specifies a position in a display vector, this might
3127 be for an ellipsis displayed for invisible text. We won't
3128 get the iterator set up for delivering that ellipsis unless
3129 we make sure that it gets aware of the invisible text. */
3130 if (pos->dpvec_index >= 0
3131 && pos->overlay_string_index < 0
3132 && CHARPOS (pos->string_pos) < 0
3133 && charpos > BEGV
3134 && (XSETWINDOW (window, w),
3135 prop = Fget_char_property (make_number (charpos),
3136 Qinvisible, window),
3137 !TEXT_PROP_MEANS_INVISIBLE (prop)))
3139 prop = Fget_char_property (make_number (charpos - 1), Qinvisible,
3140 window);
3141 ellipses_p = 2 == TEXT_PROP_MEANS_INVISIBLE (prop);
3144 return ellipses_p;
3148 /* Initialize IT for stepping through current_buffer in window W,
3149 starting at position POS that includes overlay string and display
3150 vector/ control character translation position information. Value
3151 is zero if there are overlay strings with newlines at POS. */
3153 static int
3154 init_from_display_pos (struct it *it, struct window *w, struct display_pos *pos)
3156 ptrdiff_t charpos = CHARPOS (pos->pos), bytepos = BYTEPOS (pos->pos);
3157 int i, overlay_strings_with_newlines = 0;
3159 /* If POS specifies a position in a display vector, this might
3160 be for an ellipsis displayed for invisible text. We won't
3161 get the iterator set up for delivering that ellipsis unless
3162 we make sure that it gets aware of the invisible text. */
3163 if (in_ellipses_for_invisible_text_p (pos, w))
3165 --charpos;
3166 bytepos = 0;
3169 /* Keep in mind: the call to reseat in init_iterator skips invisible
3170 text, so we might end up at a position different from POS. This
3171 is only a problem when POS is a row start after a newline and an
3172 overlay starts there with an after-string, and the overlay has an
3173 invisible property. Since we don't skip invisible text in
3174 display_line and elsewhere immediately after consuming the
3175 newline before the row start, such a POS will not be in a string,
3176 but the call to init_iterator below will move us to the
3177 after-string. */
3178 init_iterator (it, w, charpos, bytepos, NULL, DEFAULT_FACE_ID);
3180 /* This only scans the current chunk -- it should scan all chunks.
3181 However, OVERLAY_STRING_CHUNK_SIZE has been increased from 3 in 21.1
3182 to 16 in 22.1 to make this a lesser problem. */
3183 for (i = 0; i < it->n_overlay_strings && i < OVERLAY_STRING_CHUNK_SIZE; ++i)
3185 const char *s = SSDATA (it->overlay_strings[i]);
3186 const char *e = s + SBYTES (it->overlay_strings[i]);
3188 while (s < e && *s != '\n')
3189 ++s;
3191 if (s < e)
3193 overlay_strings_with_newlines = 1;
3194 break;
3198 /* If position is within an overlay string, set up IT to the right
3199 overlay string. */
3200 if (pos->overlay_string_index >= 0)
3202 int relative_index;
3204 /* If the first overlay string happens to have a `display'
3205 property for an image, the iterator will be set up for that
3206 image, and we have to undo that setup first before we can
3207 correct the overlay string index. */
3208 if (it->method == GET_FROM_IMAGE)
3209 pop_it (it);
3211 /* We already have the first chunk of overlay strings in
3212 IT->overlay_strings. Load more until the one for
3213 pos->overlay_string_index is in IT->overlay_strings. */
3214 if (pos->overlay_string_index >= OVERLAY_STRING_CHUNK_SIZE)
3216 ptrdiff_t n = pos->overlay_string_index / OVERLAY_STRING_CHUNK_SIZE;
3217 it->current.overlay_string_index = 0;
3218 while (n--)
3220 load_overlay_strings (it, 0);
3221 it->current.overlay_string_index += OVERLAY_STRING_CHUNK_SIZE;
3225 it->current.overlay_string_index = pos->overlay_string_index;
3226 relative_index = (it->current.overlay_string_index
3227 % OVERLAY_STRING_CHUNK_SIZE);
3228 it->string = it->overlay_strings[relative_index];
3229 eassert (STRINGP (it->string));
3230 it->current.string_pos = pos->string_pos;
3231 it->method = GET_FROM_STRING;
3232 it->end_charpos = SCHARS (it->string);
3233 /* Set up the bidi iterator for this overlay string. */
3234 if (it->bidi_p)
3236 it->bidi_it.string.lstring = it->string;
3237 it->bidi_it.string.s = NULL;
3238 it->bidi_it.string.schars = SCHARS (it->string);
3239 it->bidi_it.string.bufpos = it->overlay_strings_charpos;
3240 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
3241 it->bidi_it.string.unibyte = !it->multibyte_p;
3242 it->bidi_it.w = it->w;
3243 bidi_init_it (IT_STRING_CHARPOS (*it), IT_STRING_BYTEPOS (*it),
3244 FRAME_WINDOW_P (it->f), &it->bidi_it);
3246 /* Synchronize the state of the bidi iterator with
3247 pos->string_pos. For any string position other than
3248 zero, this will be done automagically when we resume
3249 iteration over the string and get_visually_first_element
3250 is called. But if string_pos is zero, and the string is
3251 to be reordered for display, we need to resync manually,
3252 since it could be that the iteration state recorded in
3253 pos ended at string_pos of 0 moving backwards in string. */
3254 if (CHARPOS (pos->string_pos) == 0)
3256 get_visually_first_element (it);
3257 if (IT_STRING_CHARPOS (*it) != 0)
3258 do {
3259 /* Paranoia. */
3260 eassert (it->bidi_it.charpos < it->bidi_it.string.schars);
3261 bidi_move_to_visually_next (&it->bidi_it);
3262 } while (it->bidi_it.charpos != 0);
3264 eassert (IT_STRING_CHARPOS (*it) == it->bidi_it.charpos
3265 && IT_STRING_BYTEPOS (*it) == it->bidi_it.bytepos);
3269 if (CHARPOS (pos->string_pos) >= 0)
3271 /* Recorded position is not in an overlay string, but in another
3272 string. This can only be a string from a `display' property.
3273 IT should already be filled with that string. */
3274 it->current.string_pos = pos->string_pos;
3275 eassert (STRINGP (it->string));
3276 if (it->bidi_p)
3277 bidi_init_it (IT_STRING_CHARPOS (*it), IT_STRING_BYTEPOS (*it),
3278 FRAME_WINDOW_P (it->f), &it->bidi_it);
3281 /* Restore position in display vector translations, control
3282 character translations or ellipses. */
3283 if (pos->dpvec_index >= 0)
3285 if (it->dpvec == NULL)
3286 get_next_display_element (it);
3287 eassert (it->dpvec && it->current.dpvec_index == 0);
3288 it->current.dpvec_index = pos->dpvec_index;
3291 CHECK_IT (it);
3292 return !overlay_strings_with_newlines;
3296 /* Initialize IT for stepping through current_buffer in window W
3297 starting at ROW->start. */
3299 static void
3300 init_to_row_start (struct it *it, struct window *w, struct glyph_row *row)
3302 init_from_display_pos (it, w, &row->start);
3303 it->start = row->start;
3304 it->continuation_lines_width = row->continuation_lines_width;
3305 CHECK_IT (it);
3309 /* Initialize IT for stepping through current_buffer in window W
3310 starting in the line following ROW, i.e. starting at ROW->end.
3311 Value is zero if there are overlay strings with newlines at ROW's
3312 end position. */
3314 static int
3315 init_to_row_end (struct it *it, struct window *w, struct glyph_row *row)
3317 int success = 0;
3319 if (init_from_display_pos (it, w, &row->end))
3321 if (row->continued_p)
3322 it->continuation_lines_width
3323 = row->continuation_lines_width + row->pixel_width;
3324 CHECK_IT (it);
3325 success = 1;
3328 return success;
3334 /***********************************************************************
3335 Text properties
3336 ***********************************************************************/
3338 /* Called when IT reaches IT->stop_charpos. Handle text property and
3339 overlay changes. Set IT->stop_charpos to the next position where
3340 to stop. */
3342 static void
3343 handle_stop (struct it *it)
3345 enum prop_handled handled;
3346 int handle_overlay_change_p;
3347 struct props *p;
3349 it->dpvec = NULL;
3350 it->current.dpvec_index = -1;
3351 handle_overlay_change_p = !it->ignore_overlay_strings_at_pos_p;
3352 it->ignore_overlay_strings_at_pos_p = 0;
3353 it->ellipsis_p = 0;
3355 /* Use face of preceding text for ellipsis (if invisible) */
3356 if (it->selective_display_ellipsis_p)
3357 it->saved_face_id = it->face_id;
3361 handled = HANDLED_NORMALLY;
3363 /* Call text property handlers. */
3364 for (p = it_props; p->handler; ++p)
3366 handled = p->handler (it);
3368 if (handled == HANDLED_RECOMPUTE_PROPS)
3369 break;
3370 else if (handled == HANDLED_RETURN)
3372 /* We still want to show before and after strings from
3373 overlays even if the actual buffer text is replaced. */
3374 if (!handle_overlay_change_p
3375 || it->sp > 1
3376 /* Don't call get_overlay_strings_1 if we already
3377 have overlay strings loaded, because doing so
3378 will load them again and push the iterator state
3379 onto the stack one more time, which is not
3380 expected by the rest of the code that processes
3381 overlay strings. */
3382 || (it->current.overlay_string_index < 0
3383 ? !get_overlay_strings_1 (it, 0, 0)
3384 : 0))
3386 if (it->ellipsis_p)
3387 setup_for_ellipsis (it, 0);
3388 /* When handling a display spec, we might load an
3389 empty string. In that case, discard it here. We
3390 used to discard it in handle_single_display_spec,
3391 but that causes get_overlay_strings_1, above, to
3392 ignore overlay strings that we must check. */
3393 if (STRINGP (it->string) && !SCHARS (it->string))
3394 pop_it (it);
3395 return;
3397 else if (STRINGP (it->string) && !SCHARS (it->string))
3398 pop_it (it);
3399 else
3401 it->ignore_overlay_strings_at_pos_p = true;
3402 it->string_from_display_prop_p = 0;
3403 it->from_disp_prop_p = 0;
3404 handle_overlay_change_p = 0;
3406 handled = HANDLED_RECOMPUTE_PROPS;
3407 break;
3409 else if (handled == HANDLED_OVERLAY_STRING_CONSUMED)
3410 handle_overlay_change_p = 0;
3413 if (handled != HANDLED_RECOMPUTE_PROPS)
3415 /* Don't check for overlay strings below when set to deliver
3416 characters from a display vector. */
3417 if (it->method == GET_FROM_DISPLAY_VECTOR)
3418 handle_overlay_change_p = 0;
3420 /* Handle overlay changes.
3421 This sets HANDLED to HANDLED_RECOMPUTE_PROPS
3422 if it finds overlays. */
3423 if (handle_overlay_change_p)
3424 handled = handle_overlay_change (it);
3427 if (it->ellipsis_p)
3429 setup_for_ellipsis (it, 0);
3430 break;
3433 while (handled == HANDLED_RECOMPUTE_PROPS);
3435 /* Determine where to stop next. */
3436 if (handled == HANDLED_NORMALLY)
3437 compute_stop_pos (it);
3441 /* Compute IT->stop_charpos from text property and overlay change
3442 information for IT's current position. */
3444 static void
3445 compute_stop_pos (struct it *it)
3447 register INTERVAL iv, next_iv;
3448 Lisp_Object object, limit, position;
3449 ptrdiff_t charpos, bytepos;
3451 if (STRINGP (it->string))
3453 /* Strings are usually short, so don't limit the search for
3454 properties. */
3455 it->stop_charpos = it->end_charpos;
3456 object = it->string;
3457 limit = Qnil;
3458 charpos = IT_STRING_CHARPOS (*it);
3459 bytepos = IT_STRING_BYTEPOS (*it);
3461 else
3463 ptrdiff_t pos;
3465 /* If end_charpos is out of range for some reason, such as a
3466 misbehaving display function, rationalize it (Bug#5984). */
3467 if (it->end_charpos > ZV)
3468 it->end_charpos = ZV;
3469 it->stop_charpos = it->end_charpos;
3471 /* If next overlay change is in front of the current stop pos
3472 (which is IT->end_charpos), stop there. Note: value of
3473 next_overlay_change is point-max if no overlay change
3474 follows. */
3475 charpos = IT_CHARPOS (*it);
3476 bytepos = IT_BYTEPOS (*it);
3477 pos = next_overlay_change (charpos);
3478 if (pos < it->stop_charpos)
3479 it->stop_charpos = pos;
3481 /* Set up variables for computing the stop position from text
3482 property changes. */
3483 XSETBUFFER (object, current_buffer);
3484 limit = make_number (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT);
3487 /* Get the interval containing IT's position. Value is a null
3488 interval if there isn't such an interval. */
3489 position = make_number (charpos);
3490 iv = validate_interval_range (object, &position, &position, 0);
3491 if (iv)
3493 Lisp_Object values_here[LAST_PROP_IDX];
3494 struct props *p;
3496 /* Get properties here. */
3497 for (p = it_props; p->handler; ++p)
3498 values_here[p->idx] = textget (iv->plist, *p->name);
3500 /* Look for an interval following iv that has different
3501 properties. */
3502 for (next_iv = next_interval (iv);
3503 (next_iv
3504 && (NILP (limit)
3505 || XFASTINT (limit) > next_iv->position));
3506 next_iv = next_interval (next_iv))
3508 for (p = it_props; p->handler; ++p)
3510 Lisp_Object new_value;
3512 new_value = textget (next_iv->plist, *p->name);
3513 if (!EQ (values_here[p->idx], new_value))
3514 break;
3517 if (p->handler)
3518 break;
3521 if (next_iv)
3523 if (INTEGERP (limit)
3524 && next_iv->position >= XFASTINT (limit))
3525 /* No text property change up to limit. */
3526 it->stop_charpos = min (XFASTINT (limit), it->stop_charpos);
3527 else
3528 /* Text properties change in next_iv. */
3529 it->stop_charpos = min (it->stop_charpos, next_iv->position);
3533 if (it->cmp_it.id < 0)
3535 ptrdiff_t stoppos = it->end_charpos;
3537 if (it->bidi_p && it->bidi_it.scan_dir < 0)
3538 stoppos = -1;
3539 composition_compute_stop_pos (&it->cmp_it, charpos, bytepos,
3540 stoppos, it->string);
3543 eassert (STRINGP (it->string)
3544 || (it->stop_charpos >= BEGV
3545 && it->stop_charpos >= IT_CHARPOS (*it)));
3549 /* Return the position of the next overlay change after POS in
3550 current_buffer. Value is point-max if no overlay change
3551 follows. This is like `next-overlay-change' but doesn't use
3552 xmalloc. */
3554 static ptrdiff_t
3555 next_overlay_change (ptrdiff_t pos)
3557 ptrdiff_t i, noverlays;
3558 ptrdiff_t endpos;
3559 Lisp_Object *overlays;
3561 /* Get all overlays at the given position. */
3562 GET_OVERLAYS_AT (pos, overlays, noverlays, &endpos, 1);
3564 /* If any of these overlays ends before endpos,
3565 use its ending point instead. */
3566 for (i = 0; i < noverlays; ++i)
3568 Lisp_Object oend;
3569 ptrdiff_t oendpos;
3571 oend = OVERLAY_END (overlays[i]);
3572 oendpos = OVERLAY_POSITION (oend);
3573 endpos = min (endpos, oendpos);
3576 return endpos;
3579 /* How many characters forward to search for a display property or
3580 display string. Searching too far forward makes the bidi display
3581 sluggish, especially in small windows. */
3582 #define MAX_DISP_SCAN 250
3584 /* Return the character position of a display string at or after
3585 position specified by POSITION. If no display string exists at or
3586 after POSITION, return ZV. A display string is either an overlay
3587 with `display' property whose value is a string, or a `display'
3588 text property whose value is a string. STRING is data about the
3589 string to iterate; if STRING->lstring is nil, we are iterating a
3590 buffer. FRAME_WINDOW_P is non-zero when we are displaying a window
3591 on a GUI frame. DISP_PROP is set to zero if we searched
3592 MAX_DISP_SCAN characters forward without finding any display
3593 strings, non-zero otherwise. It is set to 2 if the display string
3594 uses any kind of `(space ...)' spec that will produce a stretch of
3595 white space in the text area. */
3596 ptrdiff_t
3597 compute_display_string_pos (struct text_pos *position,
3598 struct bidi_string_data *string,
3599 struct window *w,
3600 int frame_window_p, int *disp_prop)
3602 /* OBJECT = nil means current buffer. */
3603 Lisp_Object object, object1;
3604 Lisp_Object pos, spec, limpos;
3605 int string_p = (string && (STRINGP (string->lstring) || string->s));
3606 ptrdiff_t eob = string_p ? string->schars : ZV;
3607 ptrdiff_t begb = string_p ? 0 : BEGV;
3608 ptrdiff_t bufpos, charpos = CHARPOS (*position);
3609 ptrdiff_t lim =
3610 (charpos < eob - MAX_DISP_SCAN) ? charpos + MAX_DISP_SCAN : eob;
3611 struct text_pos tpos;
3612 int rv = 0;
3614 if (string && STRINGP (string->lstring))
3615 object1 = object = string->lstring;
3616 else if (w && !string_p)
3618 XSETWINDOW (object, w);
3619 object1 = Qnil;
3621 else
3622 object1 = object = Qnil;
3624 *disp_prop = 1;
3626 if (charpos >= eob
3627 /* We don't support display properties whose values are strings
3628 that have display string properties. */
3629 || string->from_disp_str
3630 /* C strings cannot have display properties. */
3631 || (string->s && !STRINGP (object)))
3633 *disp_prop = 0;
3634 return eob;
3637 /* If the character at CHARPOS is where the display string begins,
3638 return CHARPOS. */
3639 pos = make_number (charpos);
3640 if (STRINGP (object))
3641 bufpos = string->bufpos;
3642 else
3643 bufpos = charpos;
3644 tpos = *position;
3645 if (!NILP (spec = Fget_char_property (pos, Qdisplay, object))
3646 && (charpos <= begb
3647 || !EQ (Fget_char_property (make_number (charpos - 1), Qdisplay,
3648 object),
3649 spec))
3650 && (rv = handle_display_spec (NULL, spec, object, Qnil, &tpos, bufpos,
3651 frame_window_p)))
3653 if (rv == 2)
3654 *disp_prop = 2;
3655 return charpos;
3658 /* Look forward for the first character with a `display' property
3659 that will replace the underlying text when displayed. */
3660 limpos = make_number (lim);
3661 do {
3662 pos = Fnext_single_char_property_change (pos, Qdisplay, object1, limpos);
3663 CHARPOS (tpos) = XFASTINT (pos);
3664 if (CHARPOS (tpos) >= lim)
3666 *disp_prop = 0;
3667 break;
3669 if (STRINGP (object))
3670 BYTEPOS (tpos) = string_char_to_byte (object, CHARPOS (tpos));
3671 else
3672 BYTEPOS (tpos) = CHAR_TO_BYTE (CHARPOS (tpos));
3673 spec = Fget_char_property (pos, Qdisplay, object);
3674 if (!STRINGP (object))
3675 bufpos = CHARPOS (tpos);
3676 } while (NILP (spec)
3677 || !(rv = handle_display_spec (NULL, spec, object, Qnil, &tpos,
3678 bufpos, frame_window_p)));
3679 if (rv == 2)
3680 *disp_prop = 2;
3682 return CHARPOS (tpos);
3685 /* Return the character position of the end of the display string that
3686 started at CHARPOS. If there's no display string at CHARPOS,
3687 return -1. A display string is either an overlay with `display'
3688 property whose value is a string or a `display' text property whose
3689 value is a string. */
3690 ptrdiff_t
3691 compute_display_string_end (ptrdiff_t charpos, struct bidi_string_data *string)
3693 /* OBJECT = nil means current buffer. */
3694 Lisp_Object object =
3695 (string && STRINGP (string->lstring)) ? string->lstring : Qnil;
3696 Lisp_Object pos = make_number (charpos);
3697 ptrdiff_t eob =
3698 (STRINGP (object) || (string && string->s)) ? string->schars : ZV;
3700 if (charpos >= eob || (string->s && !STRINGP (object)))
3701 return eob;
3703 /* It could happen that the display property or overlay was removed
3704 since we found it in compute_display_string_pos above. One way
3705 this can happen is if JIT font-lock was called (through
3706 handle_fontified_prop), and jit-lock-functions remove text
3707 properties or overlays from the portion of buffer that includes
3708 CHARPOS. Muse mode is known to do that, for example. In this
3709 case, we return -1 to the caller, to signal that no display
3710 string is actually present at CHARPOS. See bidi_fetch_char for
3711 how this is handled.
3713 An alternative would be to never look for display properties past
3714 it->stop_charpos. But neither compute_display_string_pos nor
3715 bidi_fetch_char that calls it know or care where the next
3716 stop_charpos is. */
3717 if (NILP (Fget_char_property (pos, Qdisplay, object)))
3718 return -1;
3720 /* Look forward for the first character where the `display' property
3721 changes. */
3722 pos = Fnext_single_char_property_change (pos, Qdisplay, object, Qnil);
3724 return XFASTINT (pos);
3729 /***********************************************************************
3730 Fontification
3731 ***********************************************************************/
3733 /* Handle changes in the `fontified' property of the current buffer by
3734 calling hook functions from Qfontification_functions to fontify
3735 regions of text. */
3737 static enum prop_handled
3738 handle_fontified_prop (struct it *it)
3740 Lisp_Object prop, pos;
3741 enum prop_handled handled = HANDLED_NORMALLY;
3743 if (!NILP (Vmemory_full))
3744 return handled;
3746 /* Get the value of the `fontified' property at IT's current buffer
3747 position. (The `fontified' property doesn't have a special
3748 meaning in strings.) If the value is nil, call functions from
3749 Qfontification_functions. */
3750 if (!STRINGP (it->string)
3751 && it->s == NULL
3752 && !NILP (Vfontification_functions)
3753 && !NILP (Vrun_hooks)
3754 && (pos = make_number (IT_CHARPOS (*it)),
3755 prop = Fget_char_property (pos, Qfontified, Qnil),
3756 /* Ignore the special cased nil value always present at EOB since
3757 no amount of fontifying will be able to change it. */
3758 NILP (prop) && IT_CHARPOS (*it) < Z))
3760 ptrdiff_t count = SPECPDL_INDEX ();
3761 Lisp_Object val;
3762 struct buffer *obuf = current_buffer;
3763 ptrdiff_t begv = BEGV, zv = ZV;
3764 bool old_clip_changed = current_buffer->clip_changed;
3766 val = Vfontification_functions;
3767 specbind (Qfontification_functions, Qnil);
3769 eassert (it->end_charpos == ZV);
3771 if (!CONSP (val) || EQ (XCAR (val), Qlambda))
3772 safe_call1 (val, pos);
3773 else
3775 Lisp_Object fns, fn;
3776 struct gcpro gcpro1, gcpro2;
3778 fns = Qnil;
3779 GCPRO2 (val, fns);
3781 for (; CONSP (val); val = XCDR (val))
3783 fn = XCAR (val);
3785 if (EQ (fn, Qt))
3787 /* A value of t indicates this hook has a local
3788 binding; it means to run the global binding too.
3789 In a global value, t should not occur. If it
3790 does, we must ignore it to avoid an endless
3791 loop. */
3792 for (fns = Fdefault_value (Qfontification_functions);
3793 CONSP (fns);
3794 fns = XCDR (fns))
3796 fn = XCAR (fns);
3797 if (!EQ (fn, Qt))
3798 safe_call1 (fn, pos);
3801 else
3802 safe_call1 (fn, pos);
3805 UNGCPRO;
3808 unbind_to (count, Qnil);
3810 /* Fontification functions routinely call `save-restriction'.
3811 Normally, this tags clip_changed, which can confuse redisplay
3812 (see discussion in Bug#6671). Since we don't perform any
3813 special handling of fontification changes in the case where
3814 `save-restriction' isn't called, there's no point doing so in
3815 this case either. So, if the buffer's restrictions are
3816 actually left unchanged, reset clip_changed. */
3817 if (obuf == current_buffer)
3819 if (begv == BEGV && zv == ZV)
3820 current_buffer->clip_changed = old_clip_changed;
3822 /* There isn't much we can reasonably do to protect against
3823 misbehaving fontification, but here's a fig leaf. */
3824 else if (BUFFER_LIVE_P (obuf))
3825 set_buffer_internal_1 (obuf);
3827 /* The fontification code may have added/removed text.
3828 It could do even a lot worse, but let's at least protect against
3829 the most obvious case where only the text past `pos' gets changed',
3830 as is/was done in grep.el where some escapes sequences are turned
3831 into face properties (bug#7876). */
3832 it->end_charpos = ZV;
3834 /* Return HANDLED_RECOMPUTE_PROPS only if function fontified
3835 something. This avoids an endless loop if they failed to
3836 fontify the text for which reason ever. */
3837 if (!NILP (Fget_char_property (pos, Qfontified, Qnil)))
3838 handled = HANDLED_RECOMPUTE_PROPS;
3841 return handled;
3846 /***********************************************************************
3847 Faces
3848 ***********************************************************************/
3850 /* Set up iterator IT from face properties at its current position.
3851 Called from handle_stop. */
3853 static enum prop_handled
3854 handle_face_prop (struct it *it)
3856 int new_face_id;
3857 ptrdiff_t next_stop;
3859 if (!STRINGP (it->string))
3861 new_face_id
3862 = face_at_buffer_position (it->w,
3863 IT_CHARPOS (*it),
3864 &next_stop,
3865 (IT_CHARPOS (*it)
3866 + TEXT_PROP_DISTANCE_LIMIT),
3867 0, it->base_face_id);
3869 /* Is this a start of a run of characters with box face?
3870 Caveat: this can be called for a freshly initialized
3871 iterator; face_id is -1 in this case. We know that the new
3872 face will not change until limit, i.e. if the new face has a
3873 box, all characters up to limit will have one. But, as
3874 usual, we don't know whether limit is really the end. */
3875 if (new_face_id != it->face_id)
3877 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3878 /* If it->face_id is -1, old_face below will be NULL, see
3879 the definition of FACE_FROM_ID. This will happen if this
3880 is the initial call that gets the face. */
3881 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
3883 /* If the value of face_id of the iterator is -1, we have to
3884 look in front of IT's position and see whether there is a
3885 face there that's different from new_face_id. */
3886 if (!old_face && IT_CHARPOS (*it) > BEG)
3888 int prev_face_id = face_before_it_pos (it);
3890 old_face = FACE_FROM_ID (it->f, prev_face_id);
3893 /* If the new face has a box, but the old face does not,
3894 this is the start of a run of characters with box face,
3895 i.e. this character has a shadow on the left side. */
3896 it->start_of_box_run_p = (new_face->box != FACE_NO_BOX
3897 && (old_face == NULL || !old_face->box));
3898 it->face_box_p = new_face->box != FACE_NO_BOX;
3901 else
3903 int base_face_id;
3904 ptrdiff_t bufpos;
3905 int i;
3906 Lisp_Object from_overlay
3907 = (it->current.overlay_string_index >= 0
3908 ? it->string_overlays[it->current.overlay_string_index
3909 % OVERLAY_STRING_CHUNK_SIZE]
3910 : Qnil);
3912 /* See if we got to this string directly or indirectly from
3913 an overlay property. That includes the before-string or
3914 after-string of an overlay, strings in display properties
3915 provided by an overlay, their text properties, etc.
3917 FROM_OVERLAY is the overlay that brought us here, or nil if none. */
3918 if (! NILP (from_overlay))
3919 for (i = it->sp - 1; i >= 0; i--)
3921 if (it->stack[i].current.overlay_string_index >= 0)
3922 from_overlay
3923 = it->string_overlays[it->stack[i].current.overlay_string_index
3924 % OVERLAY_STRING_CHUNK_SIZE];
3925 else if (! NILP (it->stack[i].from_overlay))
3926 from_overlay = it->stack[i].from_overlay;
3928 if (!NILP (from_overlay))
3929 break;
3932 if (! NILP (from_overlay))
3934 bufpos = IT_CHARPOS (*it);
3935 /* For a string from an overlay, the base face depends
3936 only on text properties and ignores overlays. */
3937 base_face_id
3938 = face_for_overlay_string (it->w,
3939 IT_CHARPOS (*it),
3940 &next_stop,
3941 (IT_CHARPOS (*it)
3942 + TEXT_PROP_DISTANCE_LIMIT),
3944 from_overlay);
3946 else
3948 bufpos = 0;
3950 /* For strings from a `display' property, use the face at
3951 IT's current buffer position as the base face to merge
3952 with, so that overlay strings appear in the same face as
3953 surrounding text, unless they specify their own faces.
3954 For strings from wrap-prefix and line-prefix properties,
3955 use the default face, possibly remapped via
3956 Vface_remapping_alist. */
3957 /* Note that the fact that we use the face at _buffer_
3958 position means that a 'display' property on an overlay
3959 string will not inherit the face of that overlay string,
3960 but will instead revert to the face of buffer text
3961 covered by the overlay. This is visible, e.g., when the
3962 overlay specifies a box face, but neither the buffer nor
3963 the display string do. This sounds like a design bug,
3964 but Emacs always did that since v21.1, so changing that
3965 might be a big deal. */
3966 base_face_id = it->string_from_prefix_prop_p
3967 ? (!NILP (Vface_remapping_alist)
3968 ? lookup_basic_face (it->f, DEFAULT_FACE_ID)
3969 : DEFAULT_FACE_ID)
3970 : underlying_face_id (it);
3973 new_face_id = face_at_string_position (it->w,
3974 it->string,
3975 IT_STRING_CHARPOS (*it),
3976 bufpos,
3977 &next_stop,
3978 base_face_id, 0);
3980 /* Is this a start of a run of characters with box? Caveat:
3981 this can be called for a freshly allocated iterator; face_id
3982 is -1 is this case. We know that the new face will not
3983 change until the next check pos, i.e. if the new face has a
3984 box, all characters up to that position will have a
3985 box. But, as usual, we don't know whether that position
3986 is really the end. */
3987 if (new_face_id != it->face_id)
3989 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3990 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
3992 /* If new face has a box but old face hasn't, this is the
3993 start of a run of characters with box, i.e. it has a
3994 shadow on the left side. */
3995 it->start_of_box_run_p
3996 = new_face->box && (old_face == NULL || !old_face->box);
3997 it->face_box_p = new_face->box != FACE_NO_BOX;
4001 it->face_id = new_face_id;
4002 return HANDLED_NORMALLY;
4006 /* Return the ID of the face ``underlying'' IT's current position,
4007 which is in a string. If the iterator is associated with a
4008 buffer, return the face at IT's current buffer position.
4009 Otherwise, use the iterator's base_face_id. */
4011 static int
4012 underlying_face_id (struct it *it)
4014 int face_id = it->base_face_id, i;
4016 eassert (STRINGP (it->string));
4018 for (i = it->sp - 1; i >= 0; --i)
4019 if (NILP (it->stack[i].string))
4020 face_id = it->stack[i].face_id;
4022 return face_id;
4026 /* Compute the face one character before or after the current position
4027 of IT, in the visual order. BEFORE_P non-zero means get the face
4028 in front (to the left in L2R paragraphs, to the right in R2L
4029 paragraphs) of IT's screen position. Value is the ID of the face. */
4031 static int
4032 face_before_or_after_it_pos (struct it *it, int before_p)
4034 int face_id, limit;
4035 ptrdiff_t next_check_charpos;
4036 struct it it_copy;
4037 void *it_copy_data = NULL;
4039 eassert (it->s == NULL);
4041 if (STRINGP (it->string))
4043 ptrdiff_t bufpos, charpos;
4044 int base_face_id;
4046 /* No face change past the end of the string (for the case
4047 we are padding with spaces). No face change before the
4048 string start. */
4049 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string)
4050 || (IT_STRING_CHARPOS (*it) == 0 && before_p))
4051 return it->face_id;
4053 if (!it->bidi_p)
4055 /* Set charpos to the position before or after IT's current
4056 position, in the logical order, which in the non-bidi
4057 case is the same as the visual order. */
4058 if (before_p)
4059 charpos = IT_STRING_CHARPOS (*it) - 1;
4060 else if (it->what == IT_COMPOSITION)
4061 /* For composition, we must check the character after the
4062 composition. */
4063 charpos = IT_STRING_CHARPOS (*it) + it->cmp_it.nchars;
4064 else
4065 charpos = IT_STRING_CHARPOS (*it) + 1;
4067 else
4069 if (before_p)
4071 /* With bidi iteration, the character before the current
4072 in the visual order cannot be found by simple
4073 iteration, because "reverse" reordering is not
4074 supported. Instead, we need to use the move_it_*
4075 family of functions. */
4076 /* Ignore face changes before the first visible
4077 character on this display line. */
4078 if (it->current_x <= it->first_visible_x)
4079 return it->face_id;
4080 SAVE_IT (it_copy, *it, it_copy_data);
4081 /* Implementation note: Since move_it_in_display_line
4082 works in the iterator geometry, and thinks the first
4083 character is always the leftmost, even in R2L lines,
4084 we don't need to distinguish between the R2L and L2R
4085 cases here. */
4086 move_it_in_display_line (&it_copy, SCHARS (it_copy.string),
4087 it_copy.current_x - 1, MOVE_TO_X);
4088 charpos = IT_STRING_CHARPOS (it_copy);
4089 RESTORE_IT (it, it, it_copy_data);
4091 else
4093 /* Set charpos to the string position of the character
4094 that comes after IT's current position in the visual
4095 order. */
4096 int n = (it->what == IT_COMPOSITION ? it->cmp_it.nchars : 1);
4098 it_copy = *it;
4099 while (n--)
4100 bidi_move_to_visually_next (&it_copy.bidi_it);
4102 charpos = it_copy.bidi_it.charpos;
4105 eassert (0 <= charpos && charpos <= SCHARS (it->string));
4107 if (it->current.overlay_string_index >= 0)
4108 bufpos = IT_CHARPOS (*it);
4109 else
4110 bufpos = 0;
4112 base_face_id = underlying_face_id (it);
4114 /* Get the face for ASCII, or unibyte. */
4115 face_id = face_at_string_position (it->w,
4116 it->string,
4117 charpos,
4118 bufpos,
4119 &next_check_charpos,
4120 base_face_id, 0);
4122 /* Correct the face for charsets different from ASCII. Do it
4123 for the multibyte case only. The face returned above is
4124 suitable for unibyte text if IT->string is unibyte. */
4125 if (STRING_MULTIBYTE (it->string))
4127 struct text_pos pos1 = string_pos (charpos, it->string);
4128 const unsigned char *p = SDATA (it->string) + BYTEPOS (pos1);
4129 int c, len;
4130 struct face *face = FACE_FROM_ID (it->f, face_id);
4132 c = string_char_and_length (p, &len);
4133 face_id = FACE_FOR_CHAR (it->f, face, c, charpos, it->string);
4136 else
4138 struct text_pos pos;
4140 if ((IT_CHARPOS (*it) >= ZV && !before_p)
4141 || (IT_CHARPOS (*it) <= BEGV && before_p))
4142 return it->face_id;
4144 limit = IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT;
4145 pos = it->current.pos;
4147 if (!it->bidi_p)
4149 if (before_p)
4150 DEC_TEXT_POS (pos, it->multibyte_p);
4151 else
4153 if (it->what == IT_COMPOSITION)
4155 /* For composition, we must check the position after
4156 the composition. */
4157 pos.charpos += it->cmp_it.nchars;
4158 pos.bytepos += it->len;
4160 else
4161 INC_TEXT_POS (pos, it->multibyte_p);
4164 else
4166 if (before_p)
4168 /* With bidi iteration, the character before the current
4169 in the visual order cannot be found by simple
4170 iteration, because "reverse" reordering is not
4171 supported. Instead, we need to use the move_it_*
4172 family of functions. */
4173 /* Ignore face changes before the first visible
4174 character on this display line. */
4175 if (it->current_x <= it->first_visible_x)
4176 return it->face_id;
4177 SAVE_IT (it_copy, *it, it_copy_data);
4178 /* Implementation note: Since move_it_in_display_line
4179 works in the iterator geometry, and thinks the first
4180 character is always the leftmost, even in R2L lines,
4181 we don't need to distinguish between the R2L and L2R
4182 cases here. */
4183 move_it_in_display_line (&it_copy, ZV,
4184 it_copy.current_x - 1, MOVE_TO_X);
4185 pos = it_copy.current.pos;
4186 RESTORE_IT (it, it, it_copy_data);
4188 else
4190 /* Set charpos to the buffer position of the character
4191 that comes after IT's current position in the visual
4192 order. */
4193 int n = (it->what == IT_COMPOSITION ? it->cmp_it.nchars : 1);
4195 it_copy = *it;
4196 while (n--)
4197 bidi_move_to_visually_next (&it_copy.bidi_it);
4199 SET_TEXT_POS (pos,
4200 it_copy.bidi_it.charpos, it_copy.bidi_it.bytepos);
4203 eassert (BEGV <= CHARPOS (pos) && CHARPOS (pos) <= ZV);
4205 /* Determine face for CHARSET_ASCII, or unibyte. */
4206 face_id = face_at_buffer_position (it->w,
4207 CHARPOS (pos),
4208 &next_check_charpos,
4209 limit, 0, -1);
4211 /* Correct the face for charsets different from ASCII. Do it
4212 for the multibyte case only. The face returned above is
4213 suitable for unibyte text if current_buffer is unibyte. */
4214 if (it->multibyte_p)
4216 int c = FETCH_MULTIBYTE_CHAR (BYTEPOS (pos));
4217 struct face *face = FACE_FROM_ID (it->f, face_id);
4218 face_id = FACE_FOR_CHAR (it->f, face, c, CHARPOS (pos), Qnil);
4222 return face_id;
4227 /***********************************************************************
4228 Invisible text
4229 ***********************************************************************/
4231 /* Set up iterator IT from invisible properties at its current
4232 position. Called from handle_stop. */
4234 static enum prop_handled
4235 handle_invisible_prop (struct it *it)
4237 enum prop_handled handled = HANDLED_NORMALLY;
4238 int invis_p;
4239 Lisp_Object prop;
4241 if (STRINGP (it->string))
4243 Lisp_Object end_charpos, limit, charpos;
4245 /* Get the value of the invisible text property at the
4246 current position. Value will be nil if there is no such
4247 property. */
4248 charpos = make_number (IT_STRING_CHARPOS (*it));
4249 prop = Fget_text_property (charpos, Qinvisible, it->string);
4250 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
4252 if (invis_p && IT_STRING_CHARPOS (*it) < it->end_charpos)
4254 /* Record whether we have to display an ellipsis for the
4255 invisible text. */
4256 int display_ellipsis_p = (invis_p == 2);
4257 ptrdiff_t len, endpos;
4259 handled = HANDLED_RECOMPUTE_PROPS;
4261 /* Get the position at which the next visible text can be
4262 found in IT->string, if any. */
4263 endpos = len = SCHARS (it->string);
4264 XSETINT (limit, len);
4267 end_charpos = Fnext_single_property_change (charpos, Qinvisible,
4268 it->string, limit);
4269 if (INTEGERP (end_charpos))
4271 endpos = XFASTINT (end_charpos);
4272 prop = Fget_text_property (end_charpos, Qinvisible, it->string);
4273 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
4274 if (invis_p == 2)
4275 display_ellipsis_p = true;
4278 while (invis_p && endpos < len);
4280 if (display_ellipsis_p)
4281 it->ellipsis_p = true;
4283 if (endpos < len)
4285 /* Text at END_CHARPOS is visible. Move IT there. */
4286 struct text_pos old;
4287 ptrdiff_t oldpos;
4289 old = it->current.string_pos;
4290 oldpos = CHARPOS (old);
4291 if (it->bidi_p)
4293 if (it->bidi_it.first_elt
4294 && it->bidi_it.charpos < SCHARS (it->string))
4295 bidi_paragraph_init (it->paragraph_embedding,
4296 &it->bidi_it, 1);
4297 /* Bidi-iterate out of the invisible text. */
4300 bidi_move_to_visually_next (&it->bidi_it);
4302 while (oldpos <= it->bidi_it.charpos
4303 && it->bidi_it.charpos < endpos);
4305 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
4306 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
4307 if (IT_CHARPOS (*it) >= endpos)
4308 it->prev_stop = endpos;
4310 else
4312 IT_STRING_CHARPOS (*it) = XFASTINT (end_charpos);
4313 compute_string_pos (&it->current.string_pos, old, it->string);
4316 else
4318 /* The rest of the string is invisible. If this is an
4319 overlay string, proceed with the next overlay string
4320 or whatever comes and return a character from there. */
4321 if (it->current.overlay_string_index >= 0
4322 && !display_ellipsis_p)
4324 next_overlay_string (it);
4325 /* Don't check for overlay strings when we just
4326 finished processing them. */
4327 handled = HANDLED_OVERLAY_STRING_CONSUMED;
4329 else
4331 IT_STRING_CHARPOS (*it) = SCHARS (it->string);
4332 IT_STRING_BYTEPOS (*it) = SBYTES (it->string);
4337 else
4339 ptrdiff_t newpos, next_stop, start_charpos, tem;
4340 Lisp_Object pos, overlay;
4342 /* First of all, is there invisible text at this position? */
4343 tem = start_charpos = IT_CHARPOS (*it);
4344 pos = make_number (tem);
4345 prop = get_char_property_and_overlay (pos, Qinvisible, it->window,
4346 &overlay);
4347 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
4349 /* If we are on invisible text, skip over it. */
4350 if (invis_p && start_charpos < it->end_charpos)
4352 /* Record whether we have to display an ellipsis for the
4353 invisible text. */
4354 int display_ellipsis_p = invis_p == 2;
4356 handled = HANDLED_RECOMPUTE_PROPS;
4358 /* Loop skipping over invisible text. The loop is left at
4359 ZV or with IT on the first char being visible again. */
4362 /* Try to skip some invisible text. Return value is the
4363 position reached which can be equal to where we start
4364 if there is nothing invisible there. This skips both
4365 over invisible text properties and overlays with
4366 invisible property. */
4367 newpos = skip_invisible (tem, &next_stop, ZV, it->window);
4369 /* If we skipped nothing at all we weren't at invisible
4370 text in the first place. If everything to the end of
4371 the buffer was skipped, end the loop. */
4372 if (newpos == tem || newpos >= ZV)
4373 invis_p = 0;
4374 else
4376 /* We skipped some characters but not necessarily
4377 all there are. Check if we ended up on visible
4378 text. Fget_char_property returns the property of
4379 the char before the given position, i.e. if we
4380 get invis_p = 0, this means that the char at
4381 newpos is visible. */
4382 pos = make_number (newpos);
4383 prop = Fget_char_property (pos, Qinvisible, it->window);
4384 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
4387 /* If we ended up on invisible text, proceed to
4388 skip starting with next_stop. */
4389 if (invis_p)
4390 tem = next_stop;
4392 /* If there are adjacent invisible texts, don't lose the
4393 second one's ellipsis. */
4394 if (invis_p == 2)
4395 display_ellipsis_p = true;
4397 while (invis_p);
4399 /* The position newpos is now either ZV or on visible text. */
4400 if (it->bidi_p)
4402 ptrdiff_t bpos = CHAR_TO_BYTE (newpos);
4403 int on_newline
4404 = bpos == ZV_BYTE || FETCH_BYTE (bpos) == '\n';
4405 int after_newline
4406 = newpos <= BEGV || FETCH_BYTE (bpos - 1) == '\n';
4408 /* If the invisible text ends on a newline or on a
4409 character after a newline, we can avoid the costly,
4410 character by character, bidi iteration to NEWPOS, and
4411 instead simply reseat the iterator there. That's
4412 because all bidi reordering information is tossed at
4413 the newline. This is a big win for modes that hide
4414 complete lines, like Outline, Org, etc. */
4415 if (on_newline || after_newline)
4417 struct text_pos tpos;
4418 bidi_dir_t pdir = it->bidi_it.paragraph_dir;
4420 SET_TEXT_POS (tpos, newpos, bpos);
4421 reseat_1 (it, tpos, 0);
4422 /* If we reseat on a newline/ZV, we need to prep the
4423 bidi iterator for advancing to the next character
4424 after the newline/EOB, keeping the current paragraph
4425 direction (so that PRODUCE_GLYPHS does TRT wrt
4426 prepending/appending glyphs to a glyph row). */
4427 if (on_newline)
4429 it->bidi_it.first_elt = 0;
4430 it->bidi_it.paragraph_dir = pdir;
4431 it->bidi_it.ch = (bpos == ZV_BYTE) ? -1 : '\n';
4432 it->bidi_it.nchars = 1;
4433 it->bidi_it.ch_len = 1;
4436 else /* Must use the slow method. */
4438 /* With bidi iteration, the region of invisible text
4439 could start and/or end in the middle of a
4440 non-base embedding level. Therefore, we need to
4441 skip invisible text using the bidi iterator,
4442 starting at IT's current position, until we find
4443 ourselves outside of the invisible text.
4444 Skipping invisible text _after_ bidi iteration
4445 avoids affecting the visual order of the
4446 displayed text when invisible properties are
4447 added or removed. */
4448 if (it->bidi_it.first_elt && it->bidi_it.charpos < ZV)
4450 /* If we were `reseat'ed to a new paragraph,
4451 determine the paragraph base direction. We
4452 need to do it now because
4453 next_element_from_buffer may not have a
4454 chance to do it, if we are going to skip any
4455 text at the beginning, which resets the
4456 FIRST_ELT flag. */
4457 bidi_paragraph_init (it->paragraph_embedding,
4458 &it->bidi_it, 1);
4462 bidi_move_to_visually_next (&it->bidi_it);
4464 while (it->stop_charpos <= it->bidi_it.charpos
4465 && it->bidi_it.charpos < newpos);
4466 IT_CHARPOS (*it) = it->bidi_it.charpos;
4467 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
4468 /* If we overstepped NEWPOS, record its position in
4469 the iterator, so that we skip invisible text if
4470 later the bidi iteration lands us in the
4471 invisible region again. */
4472 if (IT_CHARPOS (*it) >= newpos)
4473 it->prev_stop = newpos;
4476 else
4478 IT_CHARPOS (*it) = newpos;
4479 IT_BYTEPOS (*it) = CHAR_TO_BYTE (newpos);
4482 /* If there are before-strings at the start of invisible
4483 text, and the text is invisible because of a text
4484 property, arrange to show before-strings because 20.x did
4485 it that way. (If the text is invisible because of an
4486 overlay property instead of a text property, this is
4487 already handled in the overlay code.) */
4488 if (NILP (overlay)
4489 && get_overlay_strings (it, it->stop_charpos))
4491 handled = HANDLED_RECOMPUTE_PROPS;
4492 it->stack[it->sp - 1].display_ellipsis_p = display_ellipsis_p;
4494 else if (display_ellipsis_p)
4496 /* Make sure that the glyphs of the ellipsis will get
4497 correct `charpos' values. If we would not update
4498 it->position here, the glyphs would belong to the
4499 last visible character _before_ the invisible
4500 text, which confuses `set_cursor_from_row'.
4502 We use the last invisible position instead of the
4503 first because this way the cursor is always drawn on
4504 the first "." of the ellipsis, whenever PT is inside
4505 the invisible text. Otherwise the cursor would be
4506 placed _after_ the ellipsis when the point is after the
4507 first invisible character. */
4508 if (!STRINGP (it->object))
4510 it->position.charpos = newpos - 1;
4511 it->position.bytepos = CHAR_TO_BYTE (it->position.charpos);
4513 it->ellipsis_p = true;
4514 /* Let the ellipsis display before
4515 considering any properties of the following char.
4516 Fixes jasonr@gnu.org 01 Oct 07 bug. */
4517 handled = HANDLED_RETURN;
4522 return handled;
4526 /* Make iterator IT return `...' next.
4527 Replaces LEN characters from buffer. */
4529 static void
4530 setup_for_ellipsis (struct it *it, int len)
4532 /* Use the display table definition for `...'. Invalid glyphs
4533 will be handled by the method returning elements from dpvec. */
4534 if (it->dp && VECTORP (DISP_INVIS_VECTOR (it->dp)))
4536 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
4537 it->dpvec = v->contents;
4538 it->dpend = v->contents + v->header.size;
4540 else
4542 /* Default `...'. */
4543 it->dpvec = default_invis_vector;
4544 it->dpend = default_invis_vector + 3;
4547 it->dpvec_char_len = len;
4548 it->current.dpvec_index = 0;
4549 it->dpvec_face_id = -1;
4551 /* Remember the current face id in case glyphs specify faces.
4552 IT's face is restored in set_iterator_to_next.
4553 saved_face_id was set to preceding char's face in handle_stop. */
4554 if (it->saved_face_id < 0 || it->saved_face_id != it->face_id)
4555 it->saved_face_id = it->face_id = DEFAULT_FACE_ID;
4557 it->method = GET_FROM_DISPLAY_VECTOR;
4558 it->ellipsis_p = true;
4563 /***********************************************************************
4564 'display' property
4565 ***********************************************************************/
4567 /* Set up iterator IT from `display' property at its current position.
4568 Called from handle_stop.
4569 We return HANDLED_RETURN if some part of the display property
4570 overrides the display of the buffer text itself.
4571 Otherwise we return HANDLED_NORMALLY. */
4573 static enum prop_handled
4574 handle_display_prop (struct it *it)
4576 Lisp_Object propval, object, overlay;
4577 struct text_pos *position;
4578 ptrdiff_t bufpos;
4579 /* Nonzero if some property replaces the display of the text itself. */
4580 int display_replaced_p = 0;
4582 if (STRINGP (it->string))
4584 object = it->string;
4585 position = &it->current.string_pos;
4586 bufpos = CHARPOS (it->current.pos);
4588 else
4590 XSETWINDOW (object, it->w);
4591 position = &it->current.pos;
4592 bufpos = CHARPOS (*position);
4595 /* Reset those iterator values set from display property values. */
4596 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
4597 it->space_width = Qnil;
4598 it->font_height = Qnil;
4599 it->voffset = 0;
4601 /* We don't support recursive `display' properties, i.e. string
4602 values that have a string `display' property, that have a string
4603 `display' property etc. */
4604 if (!it->string_from_display_prop_p)
4605 it->area = TEXT_AREA;
4607 propval = get_char_property_and_overlay (make_number (position->charpos),
4608 Qdisplay, object, &overlay);
4609 if (NILP (propval))
4610 return HANDLED_NORMALLY;
4611 /* Now OVERLAY is the overlay that gave us this property, or nil
4612 if it was a text property. */
4614 if (!STRINGP (it->string))
4615 object = it->w->contents;
4617 display_replaced_p = handle_display_spec (it, propval, object, overlay,
4618 position, bufpos,
4619 FRAME_WINDOW_P (it->f));
4621 return display_replaced_p ? HANDLED_RETURN : HANDLED_NORMALLY;
4624 /* Subroutine of handle_display_prop. Returns non-zero if the display
4625 specification in SPEC is a replacing specification, i.e. it would
4626 replace the text covered by `display' property with something else,
4627 such as an image or a display string. If SPEC includes any kind or
4628 `(space ...) specification, the value is 2; this is used by
4629 compute_display_string_pos, which see.
4631 See handle_single_display_spec for documentation of arguments.
4632 frame_window_p is non-zero if the window being redisplayed is on a
4633 GUI frame; this argument is used only if IT is NULL, see below.
4635 IT can be NULL, if this is called by the bidi reordering code
4636 through compute_display_string_pos, which see. In that case, this
4637 function only examines SPEC, but does not otherwise "handle" it, in
4638 the sense that it doesn't set up members of IT from the display
4639 spec. */
4640 static int
4641 handle_display_spec (struct it *it, Lisp_Object spec, Lisp_Object object,
4642 Lisp_Object overlay, struct text_pos *position,
4643 ptrdiff_t bufpos, int frame_window_p)
4645 int replacing_p = 0;
4646 int rv;
4648 if (CONSP (spec)
4649 /* Simple specifications. */
4650 && !EQ (XCAR (spec), Qimage)
4651 && !EQ (XCAR (spec), Qspace)
4652 && !EQ (XCAR (spec), Qwhen)
4653 && !EQ (XCAR (spec), Qslice)
4654 && !EQ (XCAR (spec), Qspace_width)
4655 && !EQ (XCAR (spec), Qheight)
4656 && !EQ (XCAR (spec), Qraise)
4657 /* Marginal area specifications. */
4658 && !(CONSP (XCAR (spec)) && EQ (XCAR (XCAR (spec)), Qmargin))
4659 && !EQ (XCAR (spec), Qleft_fringe)
4660 && !EQ (XCAR (spec), Qright_fringe)
4661 && !NILP (XCAR (spec)))
4663 for (; CONSP (spec); spec = XCDR (spec))
4665 if ((rv = handle_single_display_spec (it, XCAR (spec), object,
4666 overlay, position, bufpos,
4667 replacing_p, frame_window_p)))
4669 replacing_p = rv;
4670 /* If some text in a string is replaced, `position' no
4671 longer points to the position of `object'. */
4672 if (!it || STRINGP (object))
4673 break;
4677 else if (VECTORP (spec))
4679 ptrdiff_t i;
4680 for (i = 0; i < ASIZE (spec); ++i)
4681 if ((rv = handle_single_display_spec (it, AREF (spec, i), object,
4682 overlay, position, bufpos,
4683 replacing_p, frame_window_p)))
4685 replacing_p = rv;
4686 /* If some text in a string is replaced, `position' no
4687 longer points to the position of `object'. */
4688 if (!it || STRINGP (object))
4689 break;
4692 else
4694 if ((rv = handle_single_display_spec (it, spec, object, overlay,
4695 position, bufpos, 0,
4696 frame_window_p)))
4697 replacing_p = rv;
4700 return replacing_p;
4703 /* Value is the position of the end of the `display' property starting
4704 at START_POS in OBJECT. */
4706 static struct text_pos
4707 display_prop_end (struct it *it, Lisp_Object object, struct text_pos start_pos)
4709 Lisp_Object end;
4710 struct text_pos end_pos;
4712 end = Fnext_single_char_property_change (make_number (CHARPOS (start_pos)),
4713 Qdisplay, object, Qnil);
4714 CHARPOS (end_pos) = XFASTINT (end);
4715 if (STRINGP (object))
4716 compute_string_pos (&end_pos, start_pos, it->string);
4717 else
4718 BYTEPOS (end_pos) = CHAR_TO_BYTE (XFASTINT (end));
4720 return end_pos;
4724 /* Set up IT from a single `display' property specification SPEC. OBJECT
4725 is the object in which the `display' property was found. *POSITION
4726 is the position in OBJECT at which the `display' property was found.
4727 BUFPOS is the buffer position of OBJECT (different from POSITION if
4728 OBJECT is not a buffer). DISPLAY_REPLACED_P non-zero means that we
4729 previously saw a display specification which already replaced text
4730 display with something else, for example an image; we ignore such
4731 properties after the first one has been processed.
4733 OVERLAY is the overlay this `display' property came from,
4734 or nil if it was a text property.
4736 If SPEC is a `space' or `image' specification, and in some other
4737 cases too, set *POSITION to the position where the `display'
4738 property ends.
4740 If IT is NULL, only examine the property specification in SPEC, but
4741 don't set up IT. In that case, FRAME_WINDOW_P non-zero means SPEC
4742 is intended to be displayed in a window on a GUI frame.
4744 Value is non-zero if something was found which replaces the display
4745 of buffer or string text. */
4747 static int
4748 handle_single_display_spec (struct it *it, Lisp_Object spec, Lisp_Object object,
4749 Lisp_Object overlay, struct text_pos *position,
4750 ptrdiff_t bufpos, int display_replaced_p,
4751 int frame_window_p)
4753 Lisp_Object form;
4754 Lisp_Object location, value;
4755 struct text_pos start_pos = *position;
4756 int valid_p;
4758 /* If SPEC is a list of the form `(when FORM . VALUE)', evaluate FORM.
4759 If the result is non-nil, use VALUE instead of SPEC. */
4760 form = Qt;
4761 if (CONSP (spec) && EQ (XCAR (spec), Qwhen))
4763 spec = XCDR (spec);
4764 if (!CONSP (spec))
4765 return 0;
4766 form = XCAR (spec);
4767 spec = XCDR (spec);
4770 if (!NILP (form) && !EQ (form, Qt))
4772 ptrdiff_t count = SPECPDL_INDEX ();
4773 struct gcpro gcpro1;
4775 /* Bind `object' to the object having the `display' property, a
4776 buffer or string. Bind `position' to the position in the
4777 object where the property was found, and `buffer-position'
4778 to the current position in the buffer. */
4780 if (NILP (object))
4781 XSETBUFFER (object, current_buffer);
4782 specbind (Qobject, object);
4783 specbind (Qposition, make_number (CHARPOS (*position)));
4784 specbind (Qbuffer_position, make_number (bufpos));
4785 GCPRO1 (form);
4786 form = safe_eval (form);
4787 UNGCPRO;
4788 unbind_to (count, Qnil);
4791 if (NILP (form))
4792 return 0;
4794 /* Handle `(height HEIGHT)' specifications. */
4795 if (CONSP (spec)
4796 && EQ (XCAR (spec), Qheight)
4797 && CONSP (XCDR (spec)))
4799 if (it)
4801 if (!FRAME_WINDOW_P (it->f))
4802 return 0;
4804 it->font_height = XCAR (XCDR (spec));
4805 if (!NILP (it->font_height))
4807 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4808 int new_height = -1;
4810 if (CONSP (it->font_height)
4811 && (EQ (XCAR (it->font_height), Qplus)
4812 || EQ (XCAR (it->font_height), Qminus))
4813 && CONSP (XCDR (it->font_height))
4814 && RANGED_INTEGERP (0, XCAR (XCDR (it->font_height)), INT_MAX))
4816 /* `(+ N)' or `(- N)' where N is an integer. */
4817 int steps = XINT (XCAR (XCDR (it->font_height)));
4818 if (EQ (XCAR (it->font_height), Qplus))
4819 steps = - steps;
4820 it->face_id = smaller_face (it->f, it->face_id, steps);
4822 else if (FUNCTIONP (it->font_height))
4824 /* Call function with current height as argument.
4825 Value is the new height. */
4826 Lisp_Object height;
4827 height = safe_call1 (it->font_height,
4828 face->lface[LFACE_HEIGHT_INDEX]);
4829 if (NUMBERP (height))
4830 new_height = XFLOATINT (height);
4832 else if (NUMBERP (it->font_height))
4834 /* Value is a multiple of the canonical char height. */
4835 struct face *f;
4837 f = FACE_FROM_ID (it->f,
4838 lookup_basic_face (it->f, DEFAULT_FACE_ID));
4839 new_height = (XFLOATINT (it->font_height)
4840 * XINT (f->lface[LFACE_HEIGHT_INDEX]));
4842 else
4844 /* Evaluate IT->font_height with `height' bound to the
4845 current specified height to get the new height. */
4846 ptrdiff_t count = SPECPDL_INDEX ();
4848 specbind (Qheight, face->lface[LFACE_HEIGHT_INDEX]);
4849 value = safe_eval (it->font_height);
4850 unbind_to (count, Qnil);
4852 if (NUMBERP (value))
4853 new_height = XFLOATINT (value);
4856 if (new_height > 0)
4857 it->face_id = face_with_height (it->f, it->face_id, new_height);
4861 return 0;
4864 /* Handle `(space-width WIDTH)'. */
4865 if (CONSP (spec)
4866 && EQ (XCAR (spec), Qspace_width)
4867 && CONSP (XCDR (spec)))
4869 if (it)
4871 if (!FRAME_WINDOW_P (it->f))
4872 return 0;
4874 value = XCAR (XCDR (spec));
4875 if (NUMBERP (value) && XFLOATINT (value) > 0)
4876 it->space_width = value;
4879 return 0;
4882 /* Handle `(slice X Y WIDTH HEIGHT)'. */
4883 if (CONSP (spec)
4884 && EQ (XCAR (spec), Qslice))
4886 Lisp_Object tem;
4888 if (it)
4890 if (!FRAME_WINDOW_P (it->f))
4891 return 0;
4893 if (tem = XCDR (spec), CONSP (tem))
4895 it->slice.x = XCAR (tem);
4896 if (tem = XCDR (tem), CONSP (tem))
4898 it->slice.y = XCAR (tem);
4899 if (tem = XCDR (tem), CONSP (tem))
4901 it->slice.width = XCAR (tem);
4902 if (tem = XCDR (tem), CONSP (tem))
4903 it->slice.height = XCAR (tem);
4909 return 0;
4912 /* Handle `(raise FACTOR)'. */
4913 if (CONSP (spec)
4914 && EQ (XCAR (spec), Qraise)
4915 && CONSP (XCDR (spec)))
4917 if (it)
4919 if (!FRAME_WINDOW_P (it->f))
4920 return 0;
4922 #ifdef HAVE_WINDOW_SYSTEM
4923 value = XCAR (XCDR (spec));
4924 if (NUMBERP (value))
4926 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4927 it->voffset = - (XFLOATINT (value)
4928 * (FONT_HEIGHT (face->font)));
4930 #endif /* HAVE_WINDOW_SYSTEM */
4933 return 0;
4936 /* Don't handle the other kinds of display specifications
4937 inside a string that we got from a `display' property. */
4938 if (it && it->string_from_display_prop_p)
4939 return 0;
4941 /* Characters having this form of property are not displayed, so
4942 we have to find the end of the property. */
4943 if (it)
4945 start_pos = *position;
4946 *position = display_prop_end (it, object, start_pos);
4948 value = Qnil;
4950 /* Stop the scan at that end position--we assume that all
4951 text properties change there. */
4952 if (it)
4953 it->stop_charpos = position->charpos;
4955 /* Handle `(left-fringe BITMAP [FACE])'
4956 and `(right-fringe BITMAP [FACE])'. */
4957 if (CONSP (spec)
4958 && (EQ (XCAR (spec), Qleft_fringe)
4959 || EQ (XCAR (spec), Qright_fringe))
4960 && CONSP (XCDR (spec)))
4962 int fringe_bitmap;
4964 if (it)
4966 if (!FRAME_WINDOW_P (it->f))
4967 /* If we return here, POSITION has been advanced
4968 across the text with this property. */
4970 /* Synchronize the bidi iterator with POSITION. This is
4971 needed because we are not going to push the iterator
4972 on behalf of this display property, so there will be
4973 no pop_it call to do this synchronization for us. */
4974 if (it->bidi_p)
4976 it->position = *position;
4977 iterate_out_of_display_property (it);
4978 *position = it->position;
4980 return 1;
4983 else if (!frame_window_p)
4984 return 1;
4986 #ifdef HAVE_WINDOW_SYSTEM
4987 value = XCAR (XCDR (spec));
4988 if (!SYMBOLP (value)
4989 || !(fringe_bitmap = lookup_fringe_bitmap (value)))
4990 /* If we return here, POSITION has been advanced
4991 across the text with this property. */
4993 if (it && it->bidi_p)
4995 it->position = *position;
4996 iterate_out_of_display_property (it);
4997 *position = it->position;
4999 return 1;
5002 if (it)
5004 int face_id = lookup_basic_face (it->f, DEFAULT_FACE_ID);;
5006 if (CONSP (XCDR (XCDR (spec))))
5008 Lisp_Object face_name = XCAR (XCDR (XCDR (spec)));
5009 int face_id2 = lookup_derived_face (it->f, face_name,
5010 FRINGE_FACE_ID, 0);
5011 if (face_id2 >= 0)
5012 face_id = face_id2;
5015 /* Save current settings of IT so that we can restore them
5016 when we are finished with the glyph property value. */
5017 push_it (it, position);
5019 it->area = TEXT_AREA;
5020 it->what = IT_IMAGE;
5021 it->image_id = -1; /* no image */
5022 it->position = start_pos;
5023 it->object = NILP (object) ? it->w->contents : object;
5024 it->method = GET_FROM_IMAGE;
5025 it->from_overlay = Qnil;
5026 it->face_id = face_id;
5027 it->from_disp_prop_p = true;
5029 /* Say that we haven't consumed the characters with
5030 `display' property yet. The call to pop_it in
5031 set_iterator_to_next will clean this up. */
5032 *position = start_pos;
5034 if (EQ (XCAR (spec), Qleft_fringe))
5036 it->left_user_fringe_bitmap = fringe_bitmap;
5037 it->left_user_fringe_face_id = face_id;
5039 else
5041 it->right_user_fringe_bitmap = fringe_bitmap;
5042 it->right_user_fringe_face_id = face_id;
5045 #endif /* HAVE_WINDOW_SYSTEM */
5046 return 1;
5049 /* Prepare to handle `((margin left-margin) ...)',
5050 `((margin right-margin) ...)' and `((margin nil) ...)'
5051 prefixes for display specifications. */
5052 location = Qunbound;
5053 if (CONSP (spec) && CONSP (XCAR (spec)))
5055 Lisp_Object tem;
5057 value = XCDR (spec);
5058 if (CONSP (value))
5059 value = XCAR (value);
5061 tem = XCAR (spec);
5062 if (EQ (XCAR (tem), Qmargin)
5063 && (tem = XCDR (tem),
5064 tem = CONSP (tem) ? XCAR (tem) : Qnil,
5065 (NILP (tem)
5066 || EQ (tem, Qleft_margin)
5067 || EQ (tem, Qright_margin))))
5068 location = tem;
5071 if (EQ (location, Qunbound))
5073 location = Qnil;
5074 value = spec;
5077 /* After this point, VALUE is the property after any
5078 margin prefix has been stripped. It must be a string,
5079 an image specification, or `(space ...)'.
5081 LOCATION specifies where to display: `left-margin',
5082 `right-margin' or nil. */
5084 valid_p = (STRINGP (value)
5085 #ifdef HAVE_WINDOW_SYSTEM
5086 || ((it ? FRAME_WINDOW_P (it->f) : frame_window_p)
5087 && valid_image_p (value))
5088 #endif /* not HAVE_WINDOW_SYSTEM */
5089 || (CONSP (value) && EQ (XCAR (value), Qspace)));
5091 if (valid_p && !display_replaced_p)
5093 int retval = 1;
5095 if (!it)
5097 /* Callers need to know whether the display spec is any kind
5098 of `(space ...)' spec that is about to affect text-area
5099 display. */
5100 if (CONSP (value) && EQ (XCAR (value), Qspace) && NILP (location))
5101 retval = 2;
5102 return retval;
5105 /* Save current settings of IT so that we can restore them
5106 when we are finished with the glyph property value. */
5107 push_it (it, position);
5108 it->from_overlay = overlay;
5109 it->from_disp_prop_p = true;
5111 if (NILP (location))
5112 it->area = TEXT_AREA;
5113 else if (EQ (location, Qleft_margin))
5114 it->area = LEFT_MARGIN_AREA;
5115 else
5116 it->area = RIGHT_MARGIN_AREA;
5118 if (STRINGP (value))
5120 it->string = value;
5121 it->multibyte_p = STRING_MULTIBYTE (it->string);
5122 it->current.overlay_string_index = -1;
5123 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
5124 it->end_charpos = it->string_nchars = SCHARS (it->string);
5125 it->method = GET_FROM_STRING;
5126 it->stop_charpos = 0;
5127 it->prev_stop = 0;
5128 it->base_level_stop = 0;
5129 it->string_from_display_prop_p = true;
5130 /* Say that we haven't consumed the characters with
5131 `display' property yet. The call to pop_it in
5132 set_iterator_to_next will clean this up. */
5133 if (BUFFERP (object))
5134 *position = start_pos;
5136 /* Force paragraph direction to be that of the parent
5137 object. If the parent object's paragraph direction is
5138 not yet determined, default to L2R. */
5139 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
5140 it->paragraph_embedding = it->bidi_it.paragraph_dir;
5141 else
5142 it->paragraph_embedding = L2R;
5144 /* Set up the bidi iterator for this display string. */
5145 if (it->bidi_p)
5147 it->bidi_it.string.lstring = it->string;
5148 it->bidi_it.string.s = NULL;
5149 it->bidi_it.string.schars = it->end_charpos;
5150 it->bidi_it.string.bufpos = bufpos;
5151 it->bidi_it.string.from_disp_str = 1;
5152 it->bidi_it.string.unibyte = !it->multibyte_p;
5153 it->bidi_it.w = it->w;
5154 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
5157 else if (CONSP (value) && EQ (XCAR (value), Qspace))
5159 it->method = GET_FROM_STRETCH;
5160 it->object = value;
5161 *position = it->position = start_pos;
5162 retval = 1 + (it->area == TEXT_AREA);
5164 #ifdef HAVE_WINDOW_SYSTEM
5165 else
5167 it->what = IT_IMAGE;
5168 it->image_id = lookup_image (it->f, value);
5169 it->position = start_pos;
5170 it->object = NILP (object) ? it->w->contents : object;
5171 it->method = GET_FROM_IMAGE;
5173 /* Say that we haven't consumed the characters with
5174 `display' property yet. The call to pop_it in
5175 set_iterator_to_next will clean this up. */
5176 *position = start_pos;
5178 #endif /* HAVE_WINDOW_SYSTEM */
5180 return retval;
5183 /* Invalid property or property not supported. Restore
5184 POSITION to what it was before. */
5185 *position = start_pos;
5186 return 0;
5189 /* Check if PROP is a display property value whose text should be
5190 treated as intangible. OVERLAY is the overlay from which PROP
5191 came, or nil if it came from a text property. CHARPOS and BYTEPOS
5192 specify the buffer position covered by PROP. */
5195 display_prop_intangible_p (Lisp_Object prop, Lisp_Object overlay,
5196 ptrdiff_t charpos, ptrdiff_t bytepos)
5198 int frame_window_p = FRAME_WINDOW_P (XFRAME (selected_frame));
5199 struct text_pos position;
5201 SET_TEXT_POS (position, charpos, bytepos);
5202 return handle_display_spec (NULL, prop, Qnil, overlay,
5203 &position, charpos, frame_window_p);
5207 /* Return 1 if PROP is a display sub-property value containing STRING.
5209 Implementation note: this and the following function are really
5210 special cases of handle_display_spec and
5211 handle_single_display_spec, and should ideally use the same code.
5212 Until they do, these two pairs must be consistent and must be
5213 modified in sync. */
5215 static int
5216 single_display_spec_string_p (Lisp_Object prop, Lisp_Object string)
5218 if (EQ (string, prop))
5219 return 1;
5221 /* Skip over `when FORM'. */
5222 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
5224 prop = XCDR (prop);
5225 if (!CONSP (prop))
5226 return 0;
5227 /* Actually, the condition following `when' should be eval'ed,
5228 like handle_single_display_spec does, and we should return
5229 zero if it evaluates to nil. However, this function is
5230 called only when the buffer was already displayed and some
5231 glyph in the glyph matrix was found to come from a display
5232 string. Therefore, the condition was already evaluated, and
5233 the result was non-nil, otherwise the display string wouldn't
5234 have been displayed and we would have never been called for
5235 this property. Thus, we can skip the evaluation and assume
5236 its result is non-nil. */
5237 prop = XCDR (prop);
5240 if (CONSP (prop))
5241 /* Skip over `margin LOCATION'. */
5242 if (EQ (XCAR (prop), Qmargin))
5244 prop = XCDR (prop);
5245 if (!CONSP (prop))
5246 return 0;
5248 prop = XCDR (prop);
5249 if (!CONSP (prop))
5250 return 0;
5253 return EQ (prop, string) || (CONSP (prop) && EQ (XCAR (prop), string));
5257 /* Return 1 if STRING appears in the `display' property PROP. */
5259 static int
5260 display_prop_string_p (Lisp_Object prop, Lisp_Object string)
5262 if (CONSP (prop)
5263 && !EQ (XCAR (prop), Qwhen)
5264 && !(CONSP (XCAR (prop)) && EQ (Qmargin, XCAR (XCAR (prop)))))
5266 /* A list of sub-properties. */
5267 while (CONSP (prop))
5269 if (single_display_spec_string_p (XCAR (prop), string))
5270 return 1;
5271 prop = XCDR (prop);
5274 else if (VECTORP (prop))
5276 /* A vector of sub-properties. */
5277 ptrdiff_t i;
5278 for (i = 0; i < ASIZE (prop); ++i)
5279 if (single_display_spec_string_p (AREF (prop, i), string))
5280 return 1;
5282 else
5283 return single_display_spec_string_p (prop, string);
5285 return 0;
5288 /* Look for STRING in overlays and text properties in the current
5289 buffer, between character positions FROM and TO (excluding TO).
5290 BACK_P non-zero means look back (in this case, TO is supposed to be
5291 less than FROM).
5292 Value is the first character position where STRING was found, or
5293 zero if it wasn't found before hitting TO.
5295 This function may only use code that doesn't eval because it is
5296 called asynchronously from note_mouse_highlight. */
5298 static ptrdiff_t
5299 string_buffer_position_lim (Lisp_Object string,
5300 ptrdiff_t from, ptrdiff_t to, int back_p)
5302 Lisp_Object limit, prop, pos;
5303 int found = 0;
5305 pos = make_number (max (from, BEGV));
5307 if (!back_p) /* looking forward */
5309 limit = make_number (min (to, ZV));
5310 while (!found && !EQ (pos, limit))
5312 prop = Fget_char_property (pos, Qdisplay, Qnil);
5313 if (!NILP (prop) && display_prop_string_p (prop, string))
5314 found = 1;
5315 else
5316 pos = Fnext_single_char_property_change (pos, Qdisplay, Qnil,
5317 limit);
5320 else /* looking back */
5322 limit = make_number (max (to, BEGV));
5323 while (!found && !EQ (pos, limit))
5325 prop = Fget_char_property (pos, Qdisplay, Qnil);
5326 if (!NILP (prop) && display_prop_string_p (prop, string))
5327 found = 1;
5328 else
5329 pos = Fprevious_single_char_property_change (pos, Qdisplay, Qnil,
5330 limit);
5334 return found ? XINT (pos) : 0;
5337 /* Determine which buffer position in current buffer STRING comes from.
5338 AROUND_CHARPOS is an approximate position where it could come from.
5339 Value is the buffer position or 0 if it couldn't be determined.
5341 This function is necessary because we don't record buffer positions
5342 in glyphs generated from strings (to keep struct glyph small).
5343 This function may only use code that doesn't eval because it is
5344 called asynchronously from note_mouse_highlight. */
5346 static ptrdiff_t
5347 string_buffer_position (Lisp_Object string, ptrdiff_t around_charpos)
5349 const int MAX_DISTANCE = 1000;
5350 ptrdiff_t found = string_buffer_position_lim (string, around_charpos,
5351 around_charpos + MAX_DISTANCE,
5354 if (!found)
5355 found = string_buffer_position_lim (string, around_charpos,
5356 around_charpos - MAX_DISTANCE, 1);
5357 return found;
5362 /***********************************************************************
5363 `composition' property
5364 ***********************************************************************/
5366 /* Set up iterator IT from `composition' property at its current
5367 position. Called from handle_stop. */
5369 static enum prop_handled
5370 handle_composition_prop (struct it *it)
5372 Lisp_Object prop, string;
5373 ptrdiff_t pos, pos_byte, start, end;
5375 if (STRINGP (it->string))
5377 unsigned char *s;
5379 pos = IT_STRING_CHARPOS (*it);
5380 pos_byte = IT_STRING_BYTEPOS (*it);
5381 string = it->string;
5382 s = SDATA (string) + pos_byte;
5383 it->c = STRING_CHAR (s);
5385 else
5387 pos = IT_CHARPOS (*it);
5388 pos_byte = IT_BYTEPOS (*it);
5389 string = Qnil;
5390 it->c = FETCH_CHAR (pos_byte);
5393 /* If there's a valid composition and point is not inside of the
5394 composition (in the case that the composition is from the current
5395 buffer), draw a glyph composed from the composition components. */
5396 if (find_composition (pos, -1, &start, &end, &prop, string)
5397 && composition_valid_p (start, end, prop)
5398 && (STRINGP (it->string) || (PT <= start || PT >= end)))
5400 if (start < pos)
5401 /* As we can't handle this situation (perhaps font-lock added
5402 a new composition), we just return here hoping that next
5403 redisplay will detect this composition much earlier. */
5404 return HANDLED_NORMALLY;
5405 if (start != pos)
5407 if (STRINGP (it->string))
5408 pos_byte = string_char_to_byte (it->string, start);
5409 else
5410 pos_byte = CHAR_TO_BYTE (start);
5412 it->cmp_it.id = get_composition_id (start, pos_byte, end - start,
5413 prop, string);
5415 if (it->cmp_it.id >= 0)
5417 it->cmp_it.ch = -1;
5418 it->cmp_it.nchars = COMPOSITION_LENGTH (prop);
5419 it->cmp_it.nglyphs = -1;
5423 return HANDLED_NORMALLY;
5428 /***********************************************************************
5429 Overlay strings
5430 ***********************************************************************/
5432 /* The following structure is used to record overlay strings for
5433 later sorting in load_overlay_strings. */
5435 struct overlay_entry
5437 Lisp_Object overlay;
5438 Lisp_Object string;
5439 EMACS_INT priority;
5440 int after_string_p;
5444 /* Set up iterator IT from overlay strings at its current position.
5445 Called from handle_stop. */
5447 static enum prop_handled
5448 handle_overlay_change (struct it *it)
5450 if (!STRINGP (it->string) && get_overlay_strings (it, 0))
5451 return HANDLED_RECOMPUTE_PROPS;
5452 else
5453 return HANDLED_NORMALLY;
5457 /* Set up the next overlay string for delivery by IT, if there is an
5458 overlay string to deliver. Called by set_iterator_to_next when the
5459 end of the current overlay string is reached. If there are more
5460 overlay strings to display, IT->string and
5461 IT->current.overlay_string_index are set appropriately here.
5462 Otherwise IT->string is set to nil. */
5464 static void
5465 next_overlay_string (struct it *it)
5467 ++it->current.overlay_string_index;
5468 if (it->current.overlay_string_index == it->n_overlay_strings)
5470 /* No more overlay strings. Restore IT's settings to what
5471 they were before overlay strings were processed, and
5472 continue to deliver from current_buffer. */
5474 it->ellipsis_p = (it->stack[it->sp - 1].display_ellipsis_p != 0);
5475 pop_it (it);
5476 eassert (it->sp > 0
5477 || (NILP (it->string)
5478 && it->method == GET_FROM_BUFFER
5479 && it->stop_charpos >= BEGV
5480 && it->stop_charpos <= it->end_charpos));
5481 it->current.overlay_string_index = -1;
5482 it->n_overlay_strings = 0;
5483 it->overlay_strings_charpos = -1;
5484 /* If there's an empty display string on the stack, pop the
5485 stack, to resync the bidi iterator with IT's position. Such
5486 empty strings are pushed onto the stack in
5487 get_overlay_strings_1. */
5488 if (it->sp > 0 && STRINGP (it->string) && !SCHARS (it->string))
5489 pop_it (it);
5491 /* If we're at the end of the buffer, record that we have
5492 processed the overlay strings there already, so that
5493 next_element_from_buffer doesn't try it again. */
5494 if (NILP (it->string) && IT_CHARPOS (*it) >= it->end_charpos)
5495 it->overlay_strings_at_end_processed_p = true;
5497 else
5499 /* There are more overlay strings to process. If
5500 IT->current.overlay_string_index has advanced to a position
5501 where we must load IT->overlay_strings with more strings, do
5502 it. We must load at the IT->overlay_strings_charpos where
5503 IT->n_overlay_strings was originally computed; when invisible
5504 text is present, this might not be IT_CHARPOS (Bug#7016). */
5505 int i = it->current.overlay_string_index % OVERLAY_STRING_CHUNK_SIZE;
5507 if (it->current.overlay_string_index && i == 0)
5508 load_overlay_strings (it, it->overlay_strings_charpos);
5510 /* Initialize IT to deliver display elements from the overlay
5511 string. */
5512 it->string = it->overlay_strings[i];
5513 it->multibyte_p = STRING_MULTIBYTE (it->string);
5514 SET_TEXT_POS (it->current.string_pos, 0, 0);
5515 it->method = GET_FROM_STRING;
5516 it->stop_charpos = 0;
5517 it->end_charpos = SCHARS (it->string);
5518 if (it->cmp_it.stop_pos >= 0)
5519 it->cmp_it.stop_pos = 0;
5520 it->prev_stop = 0;
5521 it->base_level_stop = 0;
5523 /* Set up the bidi iterator for this overlay string. */
5524 if (it->bidi_p)
5526 it->bidi_it.string.lstring = it->string;
5527 it->bidi_it.string.s = NULL;
5528 it->bidi_it.string.schars = SCHARS (it->string);
5529 it->bidi_it.string.bufpos = it->overlay_strings_charpos;
5530 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
5531 it->bidi_it.string.unibyte = !it->multibyte_p;
5532 it->bidi_it.w = it->w;
5533 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
5537 CHECK_IT (it);
5541 /* Compare two overlay_entry structures E1 and E2. Used as a
5542 comparison function for qsort in load_overlay_strings. Overlay
5543 strings for the same position are sorted so that
5545 1. All after-strings come in front of before-strings, except
5546 when they come from the same overlay.
5548 2. Within after-strings, strings are sorted so that overlay strings
5549 from overlays with higher priorities come first.
5551 2. Within before-strings, strings are sorted so that overlay
5552 strings from overlays with higher priorities come last.
5554 Value is analogous to strcmp. */
5557 static int
5558 compare_overlay_entries (const void *e1, const void *e2)
5560 struct overlay_entry const *entry1 = e1;
5561 struct overlay_entry const *entry2 = e2;
5562 int result;
5564 if (entry1->after_string_p != entry2->after_string_p)
5566 /* Let after-strings appear in front of before-strings if
5567 they come from different overlays. */
5568 if (EQ (entry1->overlay, entry2->overlay))
5569 result = entry1->after_string_p ? 1 : -1;
5570 else
5571 result = entry1->after_string_p ? -1 : 1;
5573 else if (entry1->priority != entry2->priority)
5575 if (entry1->after_string_p)
5576 /* After-strings sorted in order of decreasing priority. */
5577 result = entry2->priority < entry1->priority ? -1 : 1;
5578 else
5579 /* Before-strings sorted in order of increasing priority. */
5580 result = entry1->priority < entry2->priority ? -1 : 1;
5582 else
5583 result = 0;
5585 return result;
5589 /* Load the vector IT->overlay_strings with overlay strings from IT's
5590 current buffer position, or from CHARPOS if that is > 0. Set
5591 IT->n_overlays to the total number of overlay strings found.
5593 Overlay strings are processed OVERLAY_STRING_CHUNK_SIZE strings at
5594 a time. On entry into load_overlay_strings,
5595 IT->current.overlay_string_index gives the number of overlay
5596 strings that have already been loaded by previous calls to this
5597 function.
5599 IT->add_overlay_start contains an additional overlay start
5600 position to consider for taking overlay strings from, if non-zero.
5601 This position comes into play when the overlay has an `invisible'
5602 property, and both before and after-strings. When we've skipped to
5603 the end of the overlay, because of its `invisible' property, we
5604 nevertheless want its before-string to appear.
5605 IT->add_overlay_start will contain the overlay start position
5606 in this case.
5608 Overlay strings are sorted so that after-string strings come in
5609 front of before-string strings. Within before and after-strings,
5610 strings are sorted by overlay priority. See also function
5611 compare_overlay_entries. */
5613 static void
5614 load_overlay_strings (struct it *it, ptrdiff_t charpos)
5616 Lisp_Object overlay, window, str, invisible;
5617 struct Lisp_Overlay *ov;
5618 ptrdiff_t start, end;
5619 ptrdiff_t size = 20;
5620 ptrdiff_t n = 0, i, j;
5621 int invis_p;
5622 struct overlay_entry *entries = alloca (size * sizeof *entries);
5623 USE_SAFE_ALLOCA;
5625 if (charpos <= 0)
5626 charpos = IT_CHARPOS (*it);
5628 /* Append the overlay string STRING of overlay OVERLAY to vector
5629 `entries' which has size `size' and currently contains `n'
5630 elements. AFTER_P non-zero means STRING is an after-string of
5631 OVERLAY. */
5632 #define RECORD_OVERLAY_STRING(OVERLAY, STRING, AFTER_P) \
5633 do \
5635 Lisp_Object priority; \
5637 if (n == size) \
5639 struct overlay_entry *old = entries; \
5640 SAFE_NALLOCA (entries, 2, size); \
5641 memcpy (entries, old, size * sizeof *entries); \
5642 size *= 2; \
5645 entries[n].string = (STRING); \
5646 entries[n].overlay = (OVERLAY); \
5647 priority = Foverlay_get ((OVERLAY), Qpriority); \
5648 entries[n].priority = INTEGERP (priority) ? XINT (priority) : 0; \
5649 entries[n].after_string_p = (AFTER_P); \
5650 ++n; \
5652 while (0)
5654 /* Process overlay before the overlay center. */
5655 for (ov = current_buffer->overlays_before; ov; ov = ov->next)
5657 XSETMISC (overlay, ov);
5658 eassert (OVERLAYP (overlay));
5659 start = OVERLAY_POSITION (OVERLAY_START (overlay));
5660 end = OVERLAY_POSITION (OVERLAY_END (overlay));
5662 if (end < charpos)
5663 break;
5665 /* Skip this overlay if it doesn't start or end at IT's current
5666 position. */
5667 if (end != charpos && start != charpos)
5668 continue;
5670 /* Skip this overlay if it doesn't apply to IT->w. */
5671 window = Foverlay_get (overlay, Qwindow);
5672 if (WINDOWP (window) && XWINDOW (window) != it->w)
5673 continue;
5675 /* If the text ``under'' the overlay is invisible, both before-
5676 and after-strings from this overlay are visible; start and
5677 end position are indistinguishable. */
5678 invisible = Foverlay_get (overlay, Qinvisible);
5679 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
5681 /* If overlay has a non-empty before-string, record it. */
5682 if ((start == charpos || (end == charpos && invis_p))
5683 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
5684 && SCHARS (str))
5685 RECORD_OVERLAY_STRING (overlay, str, 0);
5687 /* If overlay has a non-empty after-string, record it. */
5688 if ((end == charpos || (start == charpos && invis_p))
5689 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
5690 && SCHARS (str))
5691 RECORD_OVERLAY_STRING (overlay, str, 1);
5694 /* Process overlays after the overlay center. */
5695 for (ov = current_buffer->overlays_after; ov; ov = ov->next)
5697 XSETMISC (overlay, ov);
5698 eassert (OVERLAYP (overlay));
5699 start = OVERLAY_POSITION (OVERLAY_START (overlay));
5700 end = OVERLAY_POSITION (OVERLAY_END (overlay));
5702 if (start > charpos)
5703 break;
5705 /* Skip this overlay if it doesn't start or end at IT's current
5706 position. */
5707 if (end != charpos && start != charpos)
5708 continue;
5710 /* Skip this overlay if it doesn't apply to IT->w. */
5711 window = Foverlay_get (overlay, Qwindow);
5712 if (WINDOWP (window) && XWINDOW (window) != it->w)
5713 continue;
5715 /* If the text ``under'' the overlay is invisible, it has a zero
5716 dimension, and both before- and after-strings apply. */
5717 invisible = Foverlay_get (overlay, Qinvisible);
5718 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
5720 /* If overlay has a non-empty before-string, record it. */
5721 if ((start == charpos || (end == charpos && invis_p))
5722 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
5723 && SCHARS (str))
5724 RECORD_OVERLAY_STRING (overlay, str, 0);
5726 /* If overlay has a non-empty after-string, record it. */
5727 if ((end == charpos || (start == charpos && invis_p))
5728 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
5729 && SCHARS (str))
5730 RECORD_OVERLAY_STRING (overlay, str, 1);
5733 #undef RECORD_OVERLAY_STRING
5735 /* Sort entries. */
5736 if (n > 1)
5737 qsort (entries, n, sizeof *entries, compare_overlay_entries);
5739 /* Record number of overlay strings, and where we computed it. */
5740 it->n_overlay_strings = n;
5741 it->overlay_strings_charpos = charpos;
5743 /* IT->current.overlay_string_index is the number of overlay strings
5744 that have already been consumed by IT. Copy some of the
5745 remaining overlay strings to IT->overlay_strings. */
5746 i = 0;
5747 j = it->current.overlay_string_index;
5748 while (i < OVERLAY_STRING_CHUNK_SIZE && j < n)
5750 it->overlay_strings[i] = entries[j].string;
5751 it->string_overlays[i++] = entries[j++].overlay;
5754 CHECK_IT (it);
5755 SAFE_FREE ();
5759 /* Get the first chunk of overlay strings at IT's current buffer
5760 position, or at CHARPOS if that is > 0. Value is non-zero if at
5761 least one overlay string was found. */
5763 static int
5764 get_overlay_strings_1 (struct it *it, ptrdiff_t charpos, int compute_stop_p)
5766 /* Get the first OVERLAY_STRING_CHUNK_SIZE overlay strings to
5767 process. This fills IT->overlay_strings with strings, and sets
5768 IT->n_overlay_strings to the total number of strings to process.
5769 IT->pos.overlay_string_index has to be set temporarily to zero
5770 because load_overlay_strings needs this; it must be set to -1
5771 when no overlay strings are found because a zero value would
5772 indicate a position in the first overlay string. */
5773 it->current.overlay_string_index = 0;
5774 load_overlay_strings (it, charpos);
5776 /* If we found overlay strings, set up IT to deliver display
5777 elements from the first one. Otherwise set up IT to deliver
5778 from current_buffer. */
5779 if (it->n_overlay_strings)
5781 /* Make sure we know settings in current_buffer, so that we can
5782 restore meaningful values when we're done with the overlay
5783 strings. */
5784 if (compute_stop_p)
5785 compute_stop_pos (it);
5786 eassert (it->face_id >= 0);
5788 /* Save IT's settings. They are restored after all overlay
5789 strings have been processed. */
5790 eassert (!compute_stop_p || it->sp == 0);
5792 /* When called from handle_stop, there might be an empty display
5793 string loaded. In that case, don't bother saving it. But
5794 don't use this optimization with the bidi iterator, since we
5795 need the corresponding pop_it call to resync the bidi
5796 iterator's position with IT's position, after we are done
5797 with the overlay strings. (The corresponding call to pop_it
5798 in case of an empty display string is in
5799 next_overlay_string.) */
5800 if (!(!it->bidi_p
5801 && STRINGP (it->string) && !SCHARS (it->string)))
5802 push_it (it, NULL);
5804 /* Set up IT to deliver display elements from the first overlay
5805 string. */
5806 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
5807 it->string = it->overlay_strings[0];
5808 it->from_overlay = Qnil;
5809 it->stop_charpos = 0;
5810 eassert (STRINGP (it->string));
5811 it->end_charpos = SCHARS (it->string);
5812 it->prev_stop = 0;
5813 it->base_level_stop = 0;
5814 it->multibyte_p = STRING_MULTIBYTE (it->string);
5815 it->method = GET_FROM_STRING;
5816 it->from_disp_prop_p = 0;
5818 /* Force paragraph direction to be that of the parent
5819 buffer. */
5820 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
5821 it->paragraph_embedding = it->bidi_it.paragraph_dir;
5822 else
5823 it->paragraph_embedding = L2R;
5825 /* Set up the bidi iterator for this overlay string. */
5826 if (it->bidi_p)
5828 ptrdiff_t pos = (charpos > 0 ? charpos : IT_CHARPOS (*it));
5830 it->bidi_it.string.lstring = it->string;
5831 it->bidi_it.string.s = NULL;
5832 it->bidi_it.string.schars = SCHARS (it->string);
5833 it->bidi_it.string.bufpos = pos;
5834 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
5835 it->bidi_it.string.unibyte = !it->multibyte_p;
5836 it->bidi_it.w = it->w;
5837 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
5839 return 1;
5842 it->current.overlay_string_index = -1;
5843 return 0;
5846 static int
5847 get_overlay_strings (struct it *it, ptrdiff_t charpos)
5849 it->string = Qnil;
5850 it->method = GET_FROM_BUFFER;
5852 (void) get_overlay_strings_1 (it, charpos, 1);
5854 CHECK_IT (it);
5856 /* Value is non-zero if we found at least one overlay string. */
5857 return STRINGP (it->string);
5862 /***********************************************************************
5863 Saving and restoring state
5864 ***********************************************************************/
5866 /* Save current settings of IT on IT->stack. Called, for example,
5867 before setting up IT for an overlay string, to be able to restore
5868 IT's settings to what they were after the overlay string has been
5869 processed. If POSITION is non-NULL, it is the position to save on
5870 the stack instead of IT->position. */
5872 static void
5873 push_it (struct it *it, struct text_pos *position)
5875 struct iterator_stack_entry *p;
5877 eassert (it->sp < IT_STACK_SIZE);
5878 p = it->stack + it->sp;
5880 p->stop_charpos = it->stop_charpos;
5881 p->prev_stop = it->prev_stop;
5882 p->base_level_stop = it->base_level_stop;
5883 p->cmp_it = it->cmp_it;
5884 eassert (it->face_id >= 0);
5885 p->face_id = it->face_id;
5886 p->string = it->string;
5887 p->method = it->method;
5888 p->from_overlay = it->from_overlay;
5889 switch (p->method)
5891 case GET_FROM_IMAGE:
5892 p->u.image.object = it->object;
5893 p->u.image.image_id = it->image_id;
5894 p->u.image.slice = it->slice;
5895 break;
5896 case GET_FROM_STRETCH:
5897 p->u.stretch.object = it->object;
5898 break;
5900 p->position = position ? *position : it->position;
5901 p->current = it->current;
5902 p->end_charpos = it->end_charpos;
5903 p->string_nchars = it->string_nchars;
5904 p->area = it->area;
5905 p->multibyte_p = it->multibyte_p;
5906 p->avoid_cursor_p = it->avoid_cursor_p;
5907 p->space_width = it->space_width;
5908 p->font_height = it->font_height;
5909 p->voffset = it->voffset;
5910 p->string_from_display_prop_p = it->string_from_display_prop_p;
5911 p->string_from_prefix_prop_p = it->string_from_prefix_prop_p;
5912 p->display_ellipsis_p = 0;
5913 p->line_wrap = it->line_wrap;
5914 p->bidi_p = it->bidi_p;
5915 p->paragraph_embedding = it->paragraph_embedding;
5916 p->from_disp_prop_p = it->from_disp_prop_p;
5917 ++it->sp;
5919 /* Save the state of the bidi iterator as well. */
5920 if (it->bidi_p)
5921 bidi_push_it (&it->bidi_it);
5924 static void
5925 iterate_out_of_display_property (struct it *it)
5927 int buffer_p = !STRINGP (it->string);
5928 ptrdiff_t eob = (buffer_p ? ZV : it->end_charpos);
5929 ptrdiff_t bob = (buffer_p ? BEGV : 0);
5931 eassert (eob >= CHARPOS (it->position) && CHARPOS (it->position) >= bob);
5933 /* Maybe initialize paragraph direction. If we are at the beginning
5934 of a new paragraph, next_element_from_buffer may not have a
5935 chance to do that. */
5936 if (it->bidi_it.first_elt && it->bidi_it.charpos < eob)
5937 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
5938 /* prev_stop can be zero, so check against BEGV as well. */
5939 while (it->bidi_it.charpos >= bob
5940 && it->prev_stop <= it->bidi_it.charpos
5941 && it->bidi_it.charpos < CHARPOS (it->position)
5942 && it->bidi_it.charpos < eob)
5943 bidi_move_to_visually_next (&it->bidi_it);
5944 /* Record the stop_pos we just crossed, for when we cross it
5945 back, maybe. */
5946 if (it->bidi_it.charpos > CHARPOS (it->position))
5947 it->prev_stop = CHARPOS (it->position);
5948 /* If we ended up not where pop_it put us, resync IT's
5949 positional members with the bidi iterator. */
5950 if (it->bidi_it.charpos != CHARPOS (it->position))
5951 SET_TEXT_POS (it->position, it->bidi_it.charpos, it->bidi_it.bytepos);
5952 if (buffer_p)
5953 it->current.pos = it->position;
5954 else
5955 it->current.string_pos = it->position;
5958 /* Restore IT's settings from IT->stack. Called, for example, when no
5959 more overlay strings must be processed, and we return to delivering
5960 display elements from a buffer, or when the end of a string from a
5961 `display' property is reached and we return to delivering display
5962 elements from an overlay string, or from a buffer. */
5964 static void
5965 pop_it (struct it *it)
5967 struct iterator_stack_entry *p;
5968 int from_display_prop = it->from_disp_prop_p;
5970 eassert (it->sp > 0);
5971 --it->sp;
5972 p = it->stack + it->sp;
5973 it->stop_charpos = p->stop_charpos;
5974 it->prev_stop = p->prev_stop;
5975 it->base_level_stop = p->base_level_stop;
5976 it->cmp_it = p->cmp_it;
5977 it->face_id = p->face_id;
5978 it->current = p->current;
5979 it->position = p->position;
5980 it->string = p->string;
5981 it->from_overlay = p->from_overlay;
5982 if (NILP (it->string))
5983 SET_TEXT_POS (it->current.string_pos, -1, -1);
5984 it->method = p->method;
5985 switch (it->method)
5987 case GET_FROM_IMAGE:
5988 it->image_id = p->u.image.image_id;
5989 it->object = p->u.image.object;
5990 it->slice = p->u.image.slice;
5991 break;
5992 case GET_FROM_STRETCH:
5993 it->object = p->u.stretch.object;
5994 break;
5995 case GET_FROM_BUFFER:
5996 it->object = it->w->contents;
5997 break;
5998 case GET_FROM_STRING:
6000 struct face *face = FACE_FROM_ID (it->f, it->face_id);
6002 /* Restore the face_box_p flag, since it could have been
6003 overwritten by the face of the object that we just finished
6004 displaying. */
6005 if (face)
6006 it->face_box_p = face->box != FACE_NO_BOX;
6007 it->object = it->string;
6009 break;
6010 case GET_FROM_DISPLAY_VECTOR:
6011 if (it->s)
6012 it->method = GET_FROM_C_STRING;
6013 else if (STRINGP (it->string))
6014 it->method = GET_FROM_STRING;
6015 else
6017 it->method = GET_FROM_BUFFER;
6018 it->object = it->w->contents;
6021 it->end_charpos = p->end_charpos;
6022 it->string_nchars = p->string_nchars;
6023 it->area = p->area;
6024 it->multibyte_p = p->multibyte_p;
6025 it->avoid_cursor_p = p->avoid_cursor_p;
6026 it->space_width = p->space_width;
6027 it->font_height = p->font_height;
6028 it->voffset = p->voffset;
6029 it->string_from_display_prop_p = p->string_from_display_prop_p;
6030 it->string_from_prefix_prop_p = p->string_from_prefix_prop_p;
6031 it->line_wrap = p->line_wrap;
6032 it->bidi_p = p->bidi_p;
6033 it->paragraph_embedding = p->paragraph_embedding;
6034 it->from_disp_prop_p = p->from_disp_prop_p;
6035 if (it->bidi_p)
6037 bidi_pop_it (&it->bidi_it);
6038 /* Bidi-iterate until we get out of the portion of text, if any,
6039 covered by a `display' text property or by an overlay with
6040 `display' property. (We cannot just jump there, because the
6041 internal coherency of the bidi iterator state can not be
6042 preserved across such jumps.) We also must determine the
6043 paragraph base direction if the overlay we just processed is
6044 at the beginning of a new paragraph. */
6045 if (from_display_prop
6046 && (it->method == GET_FROM_BUFFER || it->method == GET_FROM_STRING))
6047 iterate_out_of_display_property (it);
6049 eassert ((BUFFERP (it->object)
6050 && IT_CHARPOS (*it) == it->bidi_it.charpos
6051 && IT_BYTEPOS (*it) == it->bidi_it.bytepos)
6052 || (STRINGP (it->object)
6053 && IT_STRING_CHARPOS (*it) == it->bidi_it.charpos
6054 && IT_STRING_BYTEPOS (*it) == it->bidi_it.bytepos)
6055 || (CONSP (it->object) && it->method == GET_FROM_STRETCH));
6061 /***********************************************************************
6062 Moving over lines
6063 ***********************************************************************/
6065 /* Set IT's current position to the previous line start. */
6067 static void
6068 back_to_previous_line_start (struct it *it)
6070 ptrdiff_t cp = IT_CHARPOS (*it), bp = IT_BYTEPOS (*it);
6072 DEC_BOTH (cp, bp);
6073 IT_CHARPOS (*it) = find_newline_no_quit (cp, bp, -1, &IT_BYTEPOS (*it));
6077 /* Move IT to the next line start.
6079 Value is non-zero if a newline was found. Set *SKIPPED_P to 1 if
6080 we skipped over part of the text (as opposed to moving the iterator
6081 continuously over the text). Otherwise, don't change the value
6082 of *SKIPPED_P.
6084 If BIDI_IT_PREV is non-NULL, store into it the state of the bidi
6085 iterator on the newline, if it was found.
6087 Newlines may come from buffer text, overlay strings, or strings
6088 displayed via the `display' property. That's the reason we can't
6089 simply use find_newline_no_quit.
6091 Note that this function may not skip over invisible text that is so
6092 because of text properties and immediately follows a newline. If
6093 it would, function reseat_at_next_visible_line_start, when called
6094 from set_iterator_to_next, would effectively make invisible
6095 characters following a newline part of the wrong glyph row, which
6096 leads to wrong cursor motion. */
6098 static int
6099 forward_to_next_line_start (struct it *it, int *skipped_p,
6100 struct bidi_it *bidi_it_prev)
6102 ptrdiff_t old_selective;
6103 int newline_found_p, n;
6104 const int MAX_NEWLINE_DISTANCE = 500;
6106 /* If already on a newline, just consume it to avoid unintended
6107 skipping over invisible text below. */
6108 if (it->what == IT_CHARACTER
6109 && it->c == '\n'
6110 && CHARPOS (it->position) == IT_CHARPOS (*it))
6112 if (it->bidi_p && bidi_it_prev)
6113 *bidi_it_prev = it->bidi_it;
6114 set_iterator_to_next (it, 0);
6115 it->c = 0;
6116 return 1;
6119 /* Don't handle selective display in the following. It's (a)
6120 unnecessary because it's done by the caller, and (b) leads to an
6121 infinite recursion because next_element_from_ellipsis indirectly
6122 calls this function. */
6123 old_selective = it->selective;
6124 it->selective = 0;
6126 /* Scan for a newline within MAX_NEWLINE_DISTANCE display elements
6127 from buffer text. */
6128 for (n = newline_found_p = 0;
6129 !newline_found_p && n < MAX_NEWLINE_DISTANCE;
6130 n += STRINGP (it->string) ? 0 : 1)
6132 if (!get_next_display_element (it))
6133 return 0;
6134 newline_found_p = it->what == IT_CHARACTER && it->c == '\n';
6135 if (newline_found_p && it->bidi_p && bidi_it_prev)
6136 *bidi_it_prev = it->bidi_it;
6137 set_iterator_to_next (it, 0);
6140 /* If we didn't find a newline near enough, see if we can use a
6141 short-cut. */
6142 if (!newline_found_p)
6144 ptrdiff_t bytepos, start = IT_CHARPOS (*it);
6145 ptrdiff_t limit = find_newline_no_quit (start, IT_BYTEPOS (*it),
6146 1, &bytepos);
6147 Lisp_Object pos;
6149 eassert (!STRINGP (it->string));
6151 /* If there isn't any `display' property in sight, and no
6152 overlays, we can just use the position of the newline in
6153 buffer text. */
6154 if (it->stop_charpos >= limit
6155 || ((pos = Fnext_single_property_change (make_number (start),
6156 Qdisplay, Qnil,
6157 make_number (limit)),
6158 NILP (pos))
6159 && next_overlay_change (start) == ZV))
6161 if (!it->bidi_p)
6163 IT_CHARPOS (*it) = limit;
6164 IT_BYTEPOS (*it) = bytepos;
6166 else
6168 struct bidi_it bprev;
6170 /* Help bidi.c avoid expensive searches for display
6171 properties and overlays, by telling it that there are
6172 none up to `limit'. */
6173 if (it->bidi_it.disp_pos < limit)
6175 it->bidi_it.disp_pos = limit;
6176 it->bidi_it.disp_prop = 0;
6178 do {
6179 bprev = it->bidi_it;
6180 bidi_move_to_visually_next (&it->bidi_it);
6181 } while (it->bidi_it.charpos != limit);
6182 IT_CHARPOS (*it) = limit;
6183 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6184 if (bidi_it_prev)
6185 *bidi_it_prev = bprev;
6187 *skipped_p = newline_found_p = true;
6189 else
6191 while (get_next_display_element (it)
6192 && !newline_found_p)
6194 newline_found_p = ITERATOR_AT_END_OF_LINE_P (it);
6195 if (newline_found_p && it->bidi_p && bidi_it_prev)
6196 *bidi_it_prev = it->bidi_it;
6197 set_iterator_to_next (it, 0);
6202 it->selective = old_selective;
6203 return newline_found_p;
6207 /* Set IT's current position to the previous visible line start. Skip
6208 invisible text that is so either due to text properties or due to
6209 selective display. Caution: this does not change IT->current_x and
6210 IT->hpos. */
6212 static void
6213 back_to_previous_visible_line_start (struct it *it)
6215 while (IT_CHARPOS (*it) > BEGV)
6217 back_to_previous_line_start (it);
6219 if (IT_CHARPOS (*it) <= BEGV)
6220 break;
6222 /* If selective > 0, then lines indented more than its value are
6223 invisible. */
6224 if (it->selective > 0
6225 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
6226 it->selective))
6227 continue;
6229 /* Check the newline before point for invisibility. */
6231 Lisp_Object prop;
6232 prop = Fget_char_property (make_number (IT_CHARPOS (*it) - 1),
6233 Qinvisible, it->window);
6234 if (TEXT_PROP_MEANS_INVISIBLE (prop))
6235 continue;
6238 if (IT_CHARPOS (*it) <= BEGV)
6239 break;
6242 struct it it2;
6243 void *it2data = NULL;
6244 ptrdiff_t pos;
6245 ptrdiff_t beg, end;
6246 Lisp_Object val, overlay;
6248 SAVE_IT (it2, *it, it2data);
6250 /* If newline is part of a composition, continue from start of composition */
6251 if (find_composition (IT_CHARPOS (*it), -1, &beg, &end, &val, Qnil)
6252 && beg < IT_CHARPOS (*it))
6253 goto replaced;
6255 /* If newline is replaced by a display property, find start of overlay
6256 or interval and continue search from that point. */
6257 pos = --IT_CHARPOS (it2);
6258 --IT_BYTEPOS (it2);
6259 it2.sp = 0;
6260 bidi_unshelve_cache (NULL, 0);
6261 it2.string_from_display_prop_p = 0;
6262 it2.from_disp_prop_p = 0;
6263 if (handle_display_prop (&it2) == HANDLED_RETURN
6264 && !NILP (val = get_char_property_and_overlay
6265 (make_number (pos), Qdisplay, Qnil, &overlay))
6266 && (OVERLAYP (overlay)
6267 ? (beg = OVERLAY_POSITION (OVERLAY_START (overlay)))
6268 : get_property_and_range (pos, Qdisplay, &val, &beg, &end, Qnil)))
6270 RESTORE_IT (it, it, it2data);
6271 goto replaced;
6274 /* Newline is not replaced by anything -- so we are done. */
6275 RESTORE_IT (it, it, it2data);
6276 break;
6278 replaced:
6279 if (beg < BEGV)
6280 beg = BEGV;
6281 IT_CHARPOS (*it) = beg;
6282 IT_BYTEPOS (*it) = buf_charpos_to_bytepos (current_buffer, beg);
6286 it->continuation_lines_width = 0;
6288 eassert (IT_CHARPOS (*it) >= BEGV);
6289 eassert (IT_CHARPOS (*it) == BEGV
6290 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
6291 CHECK_IT (it);
6295 /* Reseat iterator IT at the previous visible line start. Skip
6296 invisible text that is so either due to text properties or due to
6297 selective display. At the end, update IT's overlay information,
6298 face information etc. */
6300 void
6301 reseat_at_previous_visible_line_start (struct it *it)
6303 back_to_previous_visible_line_start (it);
6304 reseat (it, it->current.pos, 1);
6305 CHECK_IT (it);
6309 /* Reseat iterator IT on the next visible line start in the current
6310 buffer. ON_NEWLINE_P non-zero means position IT on the newline
6311 preceding the line start. Skip over invisible text that is so
6312 because of selective display. Compute faces, overlays etc at the
6313 new position. Note that this function does not skip over text that
6314 is invisible because of text properties. */
6316 static void
6317 reseat_at_next_visible_line_start (struct it *it, int on_newline_p)
6319 int newline_found_p, skipped_p = 0;
6320 struct bidi_it bidi_it_prev;
6322 newline_found_p = forward_to_next_line_start (it, &skipped_p, &bidi_it_prev);
6324 /* Skip over lines that are invisible because they are indented
6325 more than the value of IT->selective. */
6326 if (it->selective > 0)
6327 while (IT_CHARPOS (*it) < ZV
6328 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
6329 it->selective))
6331 eassert (IT_BYTEPOS (*it) == BEGV
6332 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
6333 newline_found_p =
6334 forward_to_next_line_start (it, &skipped_p, &bidi_it_prev);
6337 /* Position on the newline if that's what's requested. */
6338 if (on_newline_p && newline_found_p)
6340 if (STRINGP (it->string))
6342 if (IT_STRING_CHARPOS (*it) > 0)
6344 if (!it->bidi_p)
6346 --IT_STRING_CHARPOS (*it);
6347 --IT_STRING_BYTEPOS (*it);
6349 else
6351 /* We need to restore the bidi iterator to the state
6352 it had on the newline, and resync the IT's
6353 position with that. */
6354 it->bidi_it = bidi_it_prev;
6355 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
6356 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
6360 else if (IT_CHARPOS (*it) > BEGV)
6362 if (!it->bidi_p)
6364 --IT_CHARPOS (*it);
6365 --IT_BYTEPOS (*it);
6367 else
6369 /* We need to restore the bidi iterator to the state it
6370 had on the newline and resync IT with that. */
6371 it->bidi_it = bidi_it_prev;
6372 IT_CHARPOS (*it) = it->bidi_it.charpos;
6373 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6375 reseat (it, it->current.pos, 0);
6378 else if (skipped_p)
6379 reseat (it, it->current.pos, 0);
6381 CHECK_IT (it);
6386 /***********************************************************************
6387 Changing an iterator's position
6388 ***********************************************************************/
6390 /* Change IT's current position to POS in current_buffer. If FORCE_P
6391 is non-zero, always check for text properties at the new position.
6392 Otherwise, text properties are only looked up if POS >=
6393 IT->check_charpos of a property. */
6395 static void
6396 reseat (struct it *it, struct text_pos pos, int force_p)
6398 ptrdiff_t original_pos = IT_CHARPOS (*it);
6400 reseat_1 (it, pos, 0);
6402 /* Determine where to check text properties. Avoid doing it
6403 where possible because text property lookup is very expensive. */
6404 if (force_p
6405 || CHARPOS (pos) > it->stop_charpos
6406 || CHARPOS (pos) < original_pos)
6408 if (it->bidi_p)
6410 /* For bidi iteration, we need to prime prev_stop and
6411 base_level_stop with our best estimations. */
6412 /* Implementation note: Of course, POS is not necessarily a
6413 stop position, so assigning prev_pos to it is a lie; we
6414 should have called compute_stop_backwards. However, if
6415 the current buffer does not include any R2L characters,
6416 that call would be a waste of cycles, because the
6417 iterator will never move back, and thus never cross this
6418 "fake" stop position. So we delay that backward search
6419 until the time we really need it, in next_element_from_buffer. */
6420 if (CHARPOS (pos) != it->prev_stop)
6421 it->prev_stop = CHARPOS (pos);
6422 if (CHARPOS (pos) < it->base_level_stop)
6423 it->base_level_stop = 0; /* meaning it's unknown */
6424 handle_stop (it);
6426 else
6428 handle_stop (it);
6429 it->prev_stop = it->base_level_stop = 0;
6434 CHECK_IT (it);
6438 /* Change IT's buffer position to POS. SET_STOP_P non-zero means set
6439 IT->stop_pos to POS, also. */
6441 static void
6442 reseat_1 (struct it *it, struct text_pos pos, int set_stop_p)
6444 /* Don't call this function when scanning a C string. */
6445 eassert (it->s == NULL);
6447 /* POS must be a reasonable value. */
6448 eassert (CHARPOS (pos) >= BEGV && CHARPOS (pos) <= ZV);
6450 it->current.pos = it->position = pos;
6451 it->end_charpos = ZV;
6452 it->dpvec = NULL;
6453 it->current.dpvec_index = -1;
6454 it->current.overlay_string_index = -1;
6455 IT_STRING_CHARPOS (*it) = -1;
6456 IT_STRING_BYTEPOS (*it) = -1;
6457 it->string = Qnil;
6458 it->method = GET_FROM_BUFFER;
6459 it->object = it->w->contents;
6460 it->area = TEXT_AREA;
6461 it->multibyte_p = !NILP (BVAR (current_buffer, enable_multibyte_characters));
6462 it->sp = 0;
6463 it->string_from_display_prop_p = 0;
6464 it->string_from_prefix_prop_p = 0;
6466 it->from_disp_prop_p = 0;
6467 it->face_before_selective_p = 0;
6468 if (it->bidi_p)
6470 bidi_init_it (IT_CHARPOS (*it), IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
6471 &it->bidi_it);
6472 bidi_unshelve_cache (NULL, 0);
6473 it->bidi_it.paragraph_dir = NEUTRAL_DIR;
6474 it->bidi_it.string.s = NULL;
6475 it->bidi_it.string.lstring = Qnil;
6476 it->bidi_it.string.bufpos = 0;
6477 it->bidi_it.string.from_disp_str = 0;
6478 it->bidi_it.string.unibyte = 0;
6479 it->bidi_it.w = it->w;
6482 if (set_stop_p)
6484 it->stop_charpos = CHARPOS (pos);
6485 it->base_level_stop = CHARPOS (pos);
6487 /* This make the information stored in it->cmp_it invalidate. */
6488 it->cmp_it.id = -1;
6492 /* Set up IT for displaying a string, starting at CHARPOS in window W.
6493 If S is non-null, it is a C string to iterate over. Otherwise,
6494 STRING gives a Lisp string to iterate over.
6496 If PRECISION > 0, don't return more then PRECISION number of
6497 characters from the string.
6499 If FIELD_WIDTH > 0, return padding spaces until FIELD_WIDTH
6500 characters have been returned. FIELD_WIDTH < 0 means an infinite
6501 field width.
6503 MULTIBYTE = 0 means disable processing of multibyte characters,
6504 MULTIBYTE > 0 means enable it,
6505 MULTIBYTE < 0 means use IT->multibyte_p.
6507 IT must be initialized via a prior call to init_iterator before
6508 calling this function. */
6510 static void
6511 reseat_to_string (struct it *it, const char *s, Lisp_Object string,
6512 ptrdiff_t charpos, ptrdiff_t precision, int field_width,
6513 int multibyte)
6515 /* No text property checks performed by default, but see below. */
6516 it->stop_charpos = -1;
6518 /* Set iterator position and end position. */
6519 memset (&it->current, 0, sizeof it->current);
6520 it->current.overlay_string_index = -1;
6521 it->current.dpvec_index = -1;
6522 eassert (charpos >= 0);
6524 /* If STRING is specified, use its multibyteness, otherwise use the
6525 setting of MULTIBYTE, if specified. */
6526 if (multibyte >= 0)
6527 it->multibyte_p = multibyte > 0;
6529 /* Bidirectional reordering of strings is controlled by the default
6530 value of bidi-display-reordering. Don't try to reorder while
6531 loading loadup.el, as the necessary character property tables are
6532 not yet available. */
6533 it->bidi_p =
6534 NILP (Vpurify_flag)
6535 && !NILP (BVAR (&buffer_defaults, bidi_display_reordering));
6537 if (s == NULL)
6539 eassert (STRINGP (string));
6540 it->string = string;
6541 it->s = NULL;
6542 it->end_charpos = it->string_nchars = SCHARS (string);
6543 it->method = GET_FROM_STRING;
6544 it->current.string_pos = string_pos (charpos, string);
6546 if (it->bidi_p)
6548 it->bidi_it.string.lstring = string;
6549 it->bidi_it.string.s = NULL;
6550 it->bidi_it.string.schars = it->end_charpos;
6551 it->bidi_it.string.bufpos = 0;
6552 it->bidi_it.string.from_disp_str = 0;
6553 it->bidi_it.string.unibyte = !it->multibyte_p;
6554 it->bidi_it.w = it->w;
6555 bidi_init_it (charpos, IT_STRING_BYTEPOS (*it),
6556 FRAME_WINDOW_P (it->f), &it->bidi_it);
6559 else
6561 it->s = (const unsigned char *) s;
6562 it->string = Qnil;
6564 /* Note that we use IT->current.pos, not it->current.string_pos,
6565 for displaying C strings. */
6566 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
6567 if (it->multibyte_p)
6569 it->current.pos = c_string_pos (charpos, s, 1);
6570 it->end_charpos = it->string_nchars = number_of_chars (s, 1);
6572 else
6574 IT_CHARPOS (*it) = IT_BYTEPOS (*it) = charpos;
6575 it->end_charpos = it->string_nchars = strlen (s);
6578 if (it->bidi_p)
6580 it->bidi_it.string.lstring = Qnil;
6581 it->bidi_it.string.s = (const unsigned char *) s;
6582 it->bidi_it.string.schars = it->end_charpos;
6583 it->bidi_it.string.bufpos = 0;
6584 it->bidi_it.string.from_disp_str = 0;
6585 it->bidi_it.string.unibyte = !it->multibyte_p;
6586 it->bidi_it.w = it->w;
6587 bidi_init_it (charpos, IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
6588 &it->bidi_it);
6590 it->method = GET_FROM_C_STRING;
6593 /* PRECISION > 0 means don't return more than PRECISION characters
6594 from the string. */
6595 if (precision > 0 && it->end_charpos - charpos > precision)
6597 it->end_charpos = it->string_nchars = charpos + precision;
6598 if (it->bidi_p)
6599 it->bidi_it.string.schars = it->end_charpos;
6602 /* FIELD_WIDTH > 0 means pad with spaces until FIELD_WIDTH
6603 characters have been returned. FIELD_WIDTH == 0 means don't pad,
6604 FIELD_WIDTH < 0 means infinite field width. This is useful for
6605 padding with `-' at the end of a mode line. */
6606 if (field_width < 0)
6607 field_width = INFINITY;
6608 /* Implementation note: We deliberately don't enlarge
6609 it->bidi_it.string.schars here to fit it->end_charpos, because
6610 the bidi iterator cannot produce characters out of thin air. */
6611 if (field_width > it->end_charpos - charpos)
6612 it->end_charpos = charpos + field_width;
6614 /* Use the standard display table for displaying strings. */
6615 if (DISP_TABLE_P (Vstandard_display_table))
6616 it->dp = XCHAR_TABLE (Vstandard_display_table);
6618 it->stop_charpos = charpos;
6619 it->prev_stop = charpos;
6620 it->base_level_stop = 0;
6621 if (it->bidi_p)
6623 it->bidi_it.first_elt = 1;
6624 it->bidi_it.paragraph_dir = NEUTRAL_DIR;
6625 it->bidi_it.disp_pos = -1;
6627 if (s == NULL && it->multibyte_p)
6629 ptrdiff_t endpos = SCHARS (it->string);
6630 if (endpos > it->end_charpos)
6631 endpos = it->end_charpos;
6632 composition_compute_stop_pos (&it->cmp_it, charpos, -1, endpos,
6633 it->string);
6635 CHECK_IT (it);
6640 /***********************************************************************
6641 Iteration
6642 ***********************************************************************/
6644 /* Map enum it_method value to corresponding next_element_from_* function. */
6646 static int (* get_next_element[NUM_IT_METHODS]) (struct it *it) =
6648 next_element_from_buffer,
6649 next_element_from_display_vector,
6650 next_element_from_string,
6651 next_element_from_c_string,
6652 next_element_from_image,
6653 next_element_from_stretch
6656 #define GET_NEXT_DISPLAY_ELEMENT(it) (*get_next_element[(it)->method]) (it)
6659 /* Return 1 iff a character at CHARPOS (and BYTEPOS) is composed
6660 (possibly with the following characters). */
6662 #define CHAR_COMPOSED_P(IT,CHARPOS,BYTEPOS,END_CHARPOS) \
6663 ((IT)->cmp_it.id >= 0 \
6664 || ((IT)->cmp_it.stop_pos == (CHARPOS) \
6665 && composition_reseat_it (&(IT)->cmp_it, CHARPOS, BYTEPOS, \
6666 END_CHARPOS, (IT)->w, \
6667 FACE_FROM_ID ((IT)->f, (IT)->face_id), \
6668 (IT)->string)))
6671 /* Lookup the char-table Vglyphless_char_display for character C (-1
6672 if we want information for no-font case), and return the display
6673 method symbol. By side-effect, update it->what and
6674 it->glyphless_method. This function is called from
6675 get_next_display_element for each character element, and from
6676 x_produce_glyphs when no suitable font was found. */
6678 Lisp_Object
6679 lookup_glyphless_char_display (int c, struct it *it)
6681 Lisp_Object glyphless_method = Qnil;
6683 if (CHAR_TABLE_P (Vglyphless_char_display)
6684 && CHAR_TABLE_EXTRA_SLOTS (XCHAR_TABLE (Vglyphless_char_display)) >= 1)
6686 if (c >= 0)
6688 glyphless_method = CHAR_TABLE_REF (Vglyphless_char_display, c);
6689 if (CONSP (glyphless_method))
6690 glyphless_method = FRAME_WINDOW_P (it->f)
6691 ? XCAR (glyphless_method)
6692 : XCDR (glyphless_method);
6694 else
6695 glyphless_method = XCHAR_TABLE (Vglyphless_char_display)->extras[0];
6698 retry:
6699 if (NILP (glyphless_method))
6701 if (c >= 0)
6702 /* The default is to display the character by a proper font. */
6703 return Qnil;
6704 /* The default for the no-font case is to display an empty box. */
6705 glyphless_method = Qempty_box;
6707 if (EQ (glyphless_method, Qzero_width))
6709 if (c >= 0)
6710 return glyphless_method;
6711 /* This method can't be used for the no-font case. */
6712 glyphless_method = Qempty_box;
6714 if (EQ (glyphless_method, Qthin_space))
6715 it->glyphless_method = GLYPHLESS_DISPLAY_THIN_SPACE;
6716 else if (EQ (glyphless_method, Qempty_box))
6717 it->glyphless_method = GLYPHLESS_DISPLAY_EMPTY_BOX;
6718 else if (EQ (glyphless_method, Qhex_code))
6719 it->glyphless_method = GLYPHLESS_DISPLAY_HEX_CODE;
6720 else if (STRINGP (glyphless_method))
6721 it->glyphless_method = GLYPHLESS_DISPLAY_ACRONYM;
6722 else
6724 /* Invalid value. We use the default method. */
6725 glyphless_method = Qnil;
6726 goto retry;
6728 it->what = IT_GLYPHLESS;
6729 return glyphless_method;
6732 /* Merge escape glyph face and cache the result. */
6734 static struct frame *last_escape_glyph_frame = NULL;
6735 static int last_escape_glyph_face_id = (1 << FACE_ID_BITS);
6736 static int last_escape_glyph_merged_face_id = 0;
6738 static int
6739 merge_escape_glyph_face (struct it *it)
6741 int face_id;
6743 if (it->f == last_escape_glyph_frame
6744 && it->face_id == last_escape_glyph_face_id)
6745 face_id = last_escape_glyph_merged_face_id;
6746 else
6748 /* Merge the `escape-glyph' face into the current face. */
6749 face_id = merge_faces (it->f, Qescape_glyph, 0, it->face_id);
6750 last_escape_glyph_frame = it->f;
6751 last_escape_glyph_face_id = it->face_id;
6752 last_escape_glyph_merged_face_id = face_id;
6754 return face_id;
6757 /* Likewise for glyphless glyph face. */
6759 static struct frame *last_glyphless_glyph_frame = NULL;
6760 static int last_glyphless_glyph_face_id = (1 << FACE_ID_BITS);
6761 static int last_glyphless_glyph_merged_face_id = 0;
6764 merge_glyphless_glyph_face (struct it *it)
6766 int face_id;
6768 if (it->f == last_glyphless_glyph_frame
6769 && it->face_id == last_glyphless_glyph_face_id)
6770 face_id = last_glyphless_glyph_merged_face_id;
6771 else
6773 /* Merge the `glyphless-char' face into the current face. */
6774 face_id = merge_faces (it->f, Qglyphless_char, 0, it->face_id);
6775 last_glyphless_glyph_frame = it->f;
6776 last_glyphless_glyph_face_id = it->face_id;
6777 last_glyphless_glyph_merged_face_id = face_id;
6779 return face_id;
6782 /* Load IT's display element fields with information about the next
6783 display element from the current position of IT. Value is zero if
6784 end of buffer (or C string) is reached. */
6786 static int
6787 get_next_display_element (struct it *it)
6789 /* Non-zero means that we found a display element. Zero means that
6790 we hit the end of what we iterate over. Performance note: the
6791 function pointer `method' used here turns out to be faster than
6792 using a sequence of if-statements. */
6793 int success_p;
6795 get_next:
6796 success_p = GET_NEXT_DISPLAY_ELEMENT (it);
6798 if (it->what == IT_CHARACTER)
6800 /* UAX#9, L4: "A character is depicted by a mirrored glyph if
6801 and only if (a) the resolved directionality of that character
6802 is R..." */
6803 /* FIXME: Do we need an exception for characters from display
6804 tables? */
6805 if (it->bidi_p && it->bidi_it.type == STRONG_R)
6806 it->c = bidi_mirror_char (it->c);
6807 /* Map via display table or translate control characters.
6808 IT->c, IT->len etc. have been set to the next character by
6809 the function call above. If we have a display table, and it
6810 contains an entry for IT->c, translate it. Don't do this if
6811 IT->c itself comes from a display table, otherwise we could
6812 end up in an infinite recursion. (An alternative could be to
6813 count the recursion depth of this function and signal an
6814 error when a certain maximum depth is reached.) Is it worth
6815 it? */
6816 if (success_p && it->dpvec == NULL)
6818 Lisp_Object dv;
6819 struct charset *unibyte = CHARSET_FROM_ID (charset_unibyte);
6820 int nonascii_space_p = 0;
6821 int nonascii_hyphen_p = 0;
6822 int c = it->c; /* This is the character to display. */
6824 if (! it->multibyte_p && ! ASCII_CHAR_P (c))
6826 eassert (SINGLE_BYTE_CHAR_P (c));
6827 if (unibyte_display_via_language_environment)
6829 c = DECODE_CHAR (unibyte, c);
6830 if (c < 0)
6831 c = BYTE8_TO_CHAR (it->c);
6833 else
6834 c = BYTE8_TO_CHAR (it->c);
6837 if (it->dp
6838 && (dv = DISP_CHAR_VECTOR (it->dp, c),
6839 VECTORP (dv)))
6841 struct Lisp_Vector *v = XVECTOR (dv);
6843 /* Return the first character from the display table
6844 entry, if not empty. If empty, don't display the
6845 current character. */
6846 if (v->header.size)
6848 it->dpvec_char_len = it->len;
6849 it->dpvec = v->contents;
6850 it->dpend = v->contents + v->header.size;
6851 it->current.dpvec_index = 0;
6852 it->dpvec_face_id = -1;
6853 it->saved_face_id = it->face_id;
6854 it->method = GET_FROM_DISPLAY_VECTOR;
6855 it->ellipsis_p = 0;
6857 else
6859 set_iterator_to_next (it, 0);
6861 goto get_next;
6864 if (! NILP (lookup_glyphless_char_display (c, it)))
6866 if (it->what == IT_GLYPHLESS)
6867 goto done;
6868 /* Don't display this character. */
6869 set_iterator_to_next (it, 0);
6870 goto get_next;
6873 /* If `nobreak-char-display' is non-nil, we display
6874 non-ASCII spaces and hyphens specially. */
6875 if (! ASCII_CHAR_P (c) && ! NILP (Vnobreak_char_display))
6877 if (c == 0xA0)
6878 nonascii_space_p = true;
6879 else if (c == 0xAD || c == 0x2010 || c == 0x2011)
6880 nonascii_hyphen_p = true;
6883 /* Translate control characters into `\003' or `^C' form.
6884 Control characters coming from a display table entry are
6885 currently not translated because we use IT->dpvec to hold
6886 the translation. This could easily be changed but I
6887 don't believe that it is worth doing.
6889 The characters handled by `nobreak-char-display' must be
6890 translated too.
6892 Non-printable characters and raw-byte characters are also
6893 translated to octal form. */
6894 if (((c < ' ' || c == 127) /* ASCII control chars. */
6895 ? (it->area != TEXT_AREA
6896 /* In mode line, treat \n, \t like other crl chars. */
6897 || (c != '\t'
6898 && it->glyph_row
6899 && (it->glyph_row->mode_line_p || it->avoid_cursor_p))
6900 || (c != '\n' && c != '\t'))
6901 : (nonascii_space_p
6902 || nonascii_hyphen_p
6903 || CHAR_BYTE8_P (c)
6904 || ! CHAR_PRINTABLE_P (c))))
6906 /* C is a control character, non-ASCII space/hyphen,
6907 raw-byte, or a non-printable character which must be
6908 displayed either as '\003' or as `^C' where the '\\'
6909 and '^' can be defined in the display table. Fill
6910 IT->ctl_chars with glyphs for what we have to
6911 display. Then, set IT->dpvec to these glyphs. */
6912 Lisp_Object gc;
6913 int ctl_len;
6914 int face_id;
6915 int lface_id = 0;
6916 int escape_glyph;
6918 /* Handle control characters with ^. */
6920 if (ASCII_CHAR_P (c) && it->ctl_arrow_p)
6922 int g;
6924 g = '^'; /* default glyph for Control */
6925 /* Set IT->ctl_chars[0] to the glyph for `^'. */
6926 if (it->dp
6927 && (gc = DISP_CTRL_GLYPH (it->dp), GLYPH_CODE_P (gc)))
6929 g = GLYPH_CODE_CHAR (gc);
6930 lface_id = GLYPH_CODE_FACE (gc);
6933 face_id = (lface_id
6934 ? merge_faces (it->f, Qt, lface_id, it->face_id)
6935 : merge_escape_glyph_face (it));
6937 XSETINT (it->ctl_chars[0], g);
6938 XSETINT (it->ctl_chars[1], c ^ 0100);
6939 ctl_len = 2;
6940 goto display_control;
6943 /* Handle non-ascii space in the mode where it only gets
6944 highlighting. */
6946 if (nonascii_space_p && EQ (Vnobreak_char_display, Qt))
6948 /* Merge `nobreak-space' into the current face. */
6949 face_id = merge_faces (it->f, Qnobreak_space, 0,
6950 it->face_id);
6951 XSETINT (it->ctl_chars[0], ' ');
6952 ctl_len = 1;
6953 goto display_control;
6956 /* Handle sequences that start with the "escape glyph". */
6958 /* the default escape glyph is \. */
6959 escape_glyph = '\\';
6961 if (it->dp
6962 && (gc = DISP_ESCAPE_GLYPH (it->dp), GLYPH_CODE_P (gc)))
6964 escape_glyph = GLYPH_CODE_CHAR (gc);
6965 lface_id = GLYPH_CODE_FACE (gc);
6968 face_id = (lface_id
6969 ? merge_faces (it->f, Qt, lface_id, it->face_id)
6970 : merge_escape_glyph_face (it));
6972 /* Draw non-ASCII hyphen with just highlighting: */
6974 if (nonascii_hyphen_p && EQ (Vnobreak_char_display, Qt))
6976 XSETINT (it->ctl_chars[0], '-');
6977 ctl_len = 1;
6978 goto display_control;
6981 /* Draw non-ASCII space/hyphen with escape glyph: */
6983 if (nonascii_space_p || nonascii_hyphen_p)
6985 XSETINT (it->ctl_chars[0], escape_glyph);
6986 XSETINT (it->ctl_chars[1], nonascii_space_p ? ' ' : '-');
6987 ctl_len = 2;
6988 goto display_control;
6992 char str[10];
6993 int len, i;
6995 if (CHAR_BYTE8_P (c))
6996 /* Display \200 instead of \17777600. */
6997 c = CHAR_TO_BYTE8 (c);
6998 len = sprintf (str, "%03o", c);
7000 XSETINT (it->ctl_chars[0], escape_glyph);
7001 for (i = 0; i < len; i++)
7002 XSETINT (it->ctl_chars[i + 1], str[i]);
7003 ctl_len = len + 1;
7006 display_control:
7007 /* Set up IT->dpvec and return first character from it. */
7008 it->dpvec_char_len = it->len;
7009 it->dpvec = it->ctl_chars;
7010 it->dpend = it->dpvec + ctl_len;
7011 it->current.dpvec_index = 0;
7012 it->dpvec_face_id = face_id;
7013 it->saved_face_id = it->face_id;
7014 it->method = GET_FROM_DISPLAY_VECTOR;
7015 it->ellipsis_p = 0;
7016 goto get_next;
7018 it->char_to_display = c;
7020 else if (success_p)
7022 it->char_to_display = it->c;
7026 #ifdef HAVE_WINDOW_SYSTEM
7027 /* Adjust face id for a multibyte character. There are no multibyte
7028 character in unibyte text. */
7029 if ((it->what == IT_CHARACTER || it->what == IT_COMPOSITION)
7030 && it->multibyte_p
7031 && success_p
7032 && FRAME_WINDOW_P (it->f))
7034 struct face *face = FACE_FROM_ID (it->f, it->face_id);
7036 if (it->what == IT_COMPOSITION && it->cmp_it.ch >= 0)
7038 /* Automatic composition with glyph-string. */
7039 Lisp_Object gstring = composition_gstring_from_id (it->cmp_it.id);
7041 it->face_id = face_for_font (it->f, LGSTRING_FONT (gstring), face);
7043 else
7045 ptrdiff_t pos = (it->s ? -1
7046 : STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
7047 : IT_CHARPOS (*it));
7048 int c;
7050 if (it->what == IT_CHARACTER)
7051 c = it->char_to_display;
7052 else
7054 struct composition *cmp = composition_table[it->cmp_it.id];
7055 int i;
7057 c = ' ';
7058 for (i = 0; i < cmp->glyph_len; i++)
7059 /* TAB in a composition means display glyphs with
7060 padding space on the left or right. */
7061 if ((c = COMPOSITION_GLYPH (cmp, i)) != '\t')
7062 break;
7064 it->face_id = FACE_FOR_CHAR (it->f, face, c, pos, it->string);
7067 #endif /* HAVE_WINDOW_SYSTEM */
7069 done:
7070 /* Is this character the last one of a run of characters with
7071 box? If yes, set IT->end_of_box_run_p to 1. */
7072 if (it->face_box_p
7073 && it->s == NULL)
7075 if (it->method == GET_FROM_STRING && it->sp)
7077 int face_id = underlying_face_id (it);
7078 struct face *face = FACE_FROM_ID (it->f, face_id);
7080 if (face)
7082 if (face->box == FACE_NO_BOX)
7084 /* If the box comes from face properties in a
7085 display string, check faces in that string. */
7086 int string_face_id = face_after_it_pos (it);
7087 it->end_of_box_run_p
7088 = (FACE_FROM_ID (it->f, string_face_id)->box
7089 == FACE_NO_BOX);
7091 /* Otherwise, the box comes from the underlying face.
7092 If this is the last string character displayed, check
7093 the next buffer location. */
7094 else if ((IT_STRING_CHARPOS (*it) >= SCHARS (it->string) - 1)
7095 /* n_overlay_strings is unreliable unless
7096 overlay_string_index is non-negative. */
7097 && ((it->current.overlay_string_index >= 0
7098 && (it->current.overlay_string_index
7099 == it->n_overlay_strings - 1))
7100 /* A string from display property. */
7101 || it->from_disp_prop_p))
7103 ptrdiff_t ignore;
7104 int next_face_id;
7105 struct text_pos pos = it->current.pos;
7107 /* For a string from a display property, the next
7108 buffer position is stored in the 'position'
7109 member of the iteration stack slot below the
7110 current one, see handle_single_display_spec. By
7111 contrast, it->current.pos was is not yet updated
7112 to point to that buffer position; that will
7113 happen in pop_it, after we finish displaying the
7114 current string. Note that we already checked
7115 above that it->sp is positive, so subtracting one
7116 from it is safe. */
7117 if (it->from_disp_prop_p)
7118 pos = (it->stack + it->sp - 1)->position;
7119 else
7120 INC_TEXT_POS (pos, it->multibyte_p);
7122 if (CHARPOS (pos) >= ZV)
7123 it->end_of_box_run_p = true;
7124 else
7126 next_face_id = face_at_buffer_position
7127 (it->w, CHARPOS (pos), &ignore,
7128 CHARPOS (pos) + TEXT_PROP_DISTANCE_LIMIT, 0, -1);
7129 it->end_of_box_run_p
7130 = (FACE_FROM_ID (it->f, next_face_id)->box
7131 == FACE_NO_BOX);
7136 /* next_element_from_display_vector sets this flag according to
7137 faces of the display vector glyphs, see there. */
7138 else if (it->method != GET_FROM_DISPLAY_VECTOR)
7140 int face_id = face_after_it_pos (it);
7141 it->end_of_box_run_p
7142 = (face_id != it->face_id
7143 && FACE_FROM_ID (it->f, face_id)->box == FACE_NO_BOX);
7146 /* If we reached the end of the object we've been iterating (e.g., a
7147 display string or an overlay string), and there's something on
7148 IT->stack, proceed with what's on the stack. It doesn't make
7149 sense to return zero if there's unprocessed stuff on the stack,
7150 because otherwise that stuff will never be displayed. */
7151 if (!success_p && it->sp > 0)
7153 set_iterator_to_next (it, 0);
7154 success_p = get_next_display_element (it);
7157 /* Value is 0 if end of buffer or string reached. */
7158 return success_p;
7162 /* Move IT to the next display element.
7164 RESEAT_P non-zero means if called on a newline in buffer text,
7165 skip to the next visible line start.
7167 Functions get_next_display_element and set_iterator_to_next are
7168 separate because I find this arrangement easier to handle than a
7169 get_next_display_element function that also increments IT's
7170 position. The way it is we can first look at an iterator's current
7171 display element, decide whether it fits on a line, and if it does,
7172 increment the iterator position. The other way around we probably
7173 would either need a flag indicating whether the iterator has to be
7174 incremented the next time, or we would have to implement a
7175 decrement position function which would not be easy to write. */
7177 void
7178 set_iterator_to_next (struct it *it, int reseat_p)
7180 /* Reset flags indicating start and end of a sequence of characters
7181 with box. Reset them at the start of this function because
7182 moving the iterator to a new position might set them. */
7183 it->start_of_box_run_p = it->end_of_box_run_p = 0;
7185 switch (it->method)
7187 case GET_FROM_BUFFER:
7188 /* The current display element of IT is a character from
7189 current_buffer. Advance in the buffer, and maybe skip over
7190 invisible lines that are so because of selective display. */
7191 if (ITERATOR_AT_END_OF_LINE_P (it) && reseat_p)
7192 reseat_at_next_visible_line_start (it, 0);
7193 else if (it->cmp_it.id >= 0)
7195 /* We are currently getting glyphs from a composition. */
7196 int i;
7198 if (! it->bidi_p)
7200 IT_CHARPOS (*it) += it->cmp_it.nchars;
7201 IT_BYTEPOS (*it) += it->cmp_it.nbytes;
7202 if (it->cmp_it.to < it->cmp_it.nglyphs)
7204 it->cmp_it.from = it->cmp_it.to;
7206 else
7208 it->cmp_it.id = -1;
7209 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
7210 IT_BYTEPOS (*it),
7211 it->end_charpos, Qnil);
7214 else if (! it->cmp_it.reversed_p)
7216 /* Composition created while scanning forward. */
7217 /* Update IT's char/byte positions to point to the first
7218 character of the next grapheme cluster, or to the
7219 character visually after the current composition. */
7220 for (i = 0; i < it->cmp_it.nchars; i++)
7221 bidi_move_to_visually_next (&it->bidi_it);
7222 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7223 IT_CHARPOS (*it) = it->bidi_it.charpos;
7225 if (it->cmp_it.to < it->cmp_it.nglyphs)
7227 /* Proceed to the next grapheme cluster. */
7228 it->cmp_it.from = it->cmp_it.to;
7230 else
7232 /* No more grapheme clusters in this composition.
7233 Find the next stop position. */
7234 ptrdiff_t stop = it->end_charpos;
7235 if (it->bidi_it.scan_dir < 0)
7236 /* Now we are scanning backward and don't know
7237 where to stop. */
7238 stop = -1;
7239 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
7240 IT_BYTEPOS (*it), stop, Qnil);
7243 else
7245 /* Composition created while scanning backward. */
7246 /* Update IT's char/byte positions to point to the last
7247 character of the previous grapheme cluster, or the
7248 character visually after the current composition. */
7249 for (i = 0; i < it->cmp_it.nchars; i++)
7250 bidi_move_to_visually_next (&it->bidi_it);
7251 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7252 IT_CHARPOS (*it) = it->bidi_it.charpos;
7253 if (it->cmp_it.from > 0)
7255 /* Proceed to the previous grapheme cluster. */
7256 it->cmp_it.to = it->cmp_it.from;
7258 else
7260 /* No more grapheme clusters in this composition.
7261 Find the next stop position. */
7262 ptrdiff_t stop = it->end_charpos;
7263 if (it->bidi_it.scan_dir < 0)
7264 /* Now we are scanning backward and don't know
7265 where to stop. */
7266 stop = -1;
7267 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
7268 IT_BYTEPOS (*it), stop, Qnil);
7272 else
7274 eassert (it->len != 0);
7276 if (!it->bidi_p)
7278 IT_BYTEPOS (*it) += it->len;
7279 IT_CHARPOS (*it) += 1;
7281 else
7283 int prev_scan_dir = it->bidi_it.scan_dir;
7284 /* If this is a new paragraph, determine its base
7285 direction (a.k.a. its base embedding level). */
7286 if (it->bidi_it.new_paragraph)
7287 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 0);
7288 bidi_move_to_visually_next (&it->bidi_it);
7289 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7290 IT_CHARPOS (*it) = it->bidi_it.charpos;
7291 if (prev_scan_dir != it->bidi_it.scan_dir)
7293 /* As the scan direction was changed, we must
7294 re-compute the stop position for composition. */
7295 ptrdiff_t stop = it->end_charpos;
7296 if (it->bidi_it.scan_dir < 0)
7297 stop = -1;
7298 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
7299 IT_BYTEPOS (*it), stop, Qnil);
7302 eassert (IT_BYTEPOS (*it) == CHAR_TO_BYTE (IT_CHARPOS (*it)));
7304 break;
7306 case GET_FROM_C_STRING:
7307 /* Current display element of IT is from a C string. */
7308 if (!it->bidi_p
7309 /* If the string position is beyond string's end, it means
7310 next_element_from_c_string is padding the string with
7311 blanks, in which case we bypass the bidi iterator,
7312 because it cannot deal with such virtual characters. */
7313 || IT_CHARPOS (*it) >= it->bidi_it.string.schars)
7315 IT_BYTEPOS (*it) += it->len;
7316 IT_CHARPOS (*it) += 1;
7318 else
7320 bidi_move_to_visually_next (&it->bidi_it);
7321 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7322 IT_CHARPOS (*it) = it->bidi_it.charpos;
7324 break;
7326 case GET_FROM_DISPLAY_VECTOR:
7327 /* Current display element of IT is from a display table entry.
7328 Advance in the display table definition. Reset it to null if
7329 end reached, and continue with characters from buffers/
7330 strings. */
7331 ++it->current.dpvec_index;
7333 /* Restore face of the iterator to what they were before the
7334 display vector entry (these entries may contain faces). */
7335 it->face_id = it->saved_face_id;
7337 if (it->dpvec + it->current.dpvec_index >= it->dpend)
7339 int recheck_faces = it->ellipsis_p;
7341 if (it->s)
7342 it->method = GET_FROM_C_STRING;
7343 else if (STRINGP (it->string))
7344 it->method = GET_FROM_STRING;
7345 else
7347 it->method = GET_FROM_BUFFER;
7348 it->object = it->w->contents;
7351 it->dpvec = NULL;
7352 it->current.dpvec_index = -1;
7354 /* Skip over characters which were displayed via IT->dpvec. */
7355 if (it->dpvec_char_len < 0)
7356 reseat_at_next_visible_line_start (it, 1);
7357 else if (it->dpvec_char_len > 0)
7359 if (it->method == GET_FROM_STRING
7360 && it->current.overlay_string_index >= 0
7361 && it->n_overlay_strings > 0)
7362 it->ignore_overlay_strings_at_pos_p = true;
7363 it->len = it->dpvec_char_len;
7364 set_iterator_to_next (it, reseat_p);
7367 /* Maybe recheck faces after display vector. */
7368 if (recheck_faces)
7369 it->stop_charpos = IT_CHARPOS (*it);
7371 break;
7373 case GET_FROM_STRING:
7374 /* Current display element is a character from a Lisp string. */
7375 eassert (it->s == NULL && STRINGP (it->string));
7376 /* Don't advance past string end. These conditions are true
7377 when set_iterator_to_next is called at the end of
7378 get_next_display_element, in which case the Lisp string is
7379 already exhausted, and all we want is pop the iterator
7380 stack. */
7381 if (it->current.overlay_string_index >= 0)
7383 /* This is an overlay string, so there's no padding with
7384 spaces, and the number of characters in the string is
7385 where the string ends. */
7386 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7387 goto consider_string_end;
7389 else
7391 /* Not an overlay string. There could be padding, so test
7392 against it->end_charpos. */
7393 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
7394 goto consider_string_end;
7396 if (it->cmp_it.id >= 0)
7398 int i;
7400 if (! it->bidi_p)
7402 IT_STRING_CHARPOS (*it) += it->cmp_it.nchars;
7403 IT_STRING_BYTEPOS (*it) += it->cmp_it.nbytes;
7404 if (it->cmp_it.to < it->cmp_it.nglyphs)
7405 it->cmp_it.from = it->cmp_it.to;
7406 else
7408 it->cmp_it.id = -1;
7409 composition_compute_stop_pos (&it->cmp_it,
7410 IT_STRING_CHARPOS (*it),
7411 IT_STRING_BYTEPOS (*it),
7412 it->end_charpos, it->string);
7415 else if (! it->cmp_it.reversed_p)
7417 for (i = 0; i < it->cmp_it.nchars; i++)
7418 bidi_move_to_visually_next (&it->bidi_it);
7419 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7420 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7422 if (it->cmp_it.to < it->cmp_it.nglyphs)
7423 it->cmp_it.from = it->cmp_it.to;
7424 else
7426 ptrdiff_t stop = it->end_charpos;
7427 if (it->bidi_it.scan_dir < 0)
7428 stop = -1;
7429 composition_compute_stop_pos (&it->cmp_it,
7430 IT_STRING_CHARPOS (*it),
7431 IT_STRING_BYTEPOS (*it), stop,
7432 it->string);
7435 else
7437 for (i = 0; i < it->cmp_it.nchars; i++)
7438 bidi_move_to_visually_next (&it->bidi_it);
7439 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7440 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7441 if (it->cmp_it.from > 0)
7442 it->cmp_it.to = it->cmp_it.from;
7443 else
7445 ptrdiff_t stop = it->end_charpos;
7446 if (it->bidi_it.scan_dir < 0)
7447 stop = -1;
7448 composition_compute_stop_pos (&it->cmp_it,
7449 IT_STRING_CHARPOS (*it),
7450 IT_STRING_BYTEPOS (*it), stop,
7451 it->string);
7455 else
7457 if (!it->bidi_p
7458 /* If the string position is beyond string's end, it
7459 means next_element_from_string is padding the string
7460 with blanks, in which case we bypass the bidi
7461 iterator, because it cannot deal with such virtual
7462 characters. */
7463 || IT_STRING_CHARPOS (*it) >= it->bidi_it.string.schars)
7465 IT_STRING_BYTEPOS (*it) += it->len;
7466 IT_STRING_CHARPOS (*it) += 1;
7468 else
7470 int prev_scan_dir = it->bidi_it.scan_dir;
7472 bidi_move_to_visually_next (&it->bidi_it);
7473 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7474 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7475 if (prev_scan_dir != it->bidi_it.scan_dir)
7477 ptrdiff_t stop = it->end_charpos;
7479 if (it->bidi_it.scan_dir < 0)
7480 stop = -1;
7481 composition_compute_stop_pos (&it->cmp_it,
7482 IT_STRING_CHARPOS (*it),
7483 IT_STRING_BYTEPOS (*it), stop,
7484 it->string);
7489 consider_string_end:
7491 if (it->current.overlay_string_index >= 0)
7493 /* IT->string is an overlay string. Advance to the
7494 next, if there is one. */
7495 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7497 it->ellipsis_p = 0;
7498 next_overlay_string (it);
7499 if (it->ellipsis_p)
7500 setup_for_ellipsis (it, 0);
7503 else
7505 /* IT->string is not an overlay string. If we reached
7506 its end, and there is something on IT->stack, proceed
7507 with what is on the stack. This can be either another
7508 string, this time an overlay string, or a buffer. */
7509 if (IT_STRING_CHARPOS (*it) == SCHARS (it->string)
7510 && it->sp > 0)
7512 pop_it (it);
7513 if (it->method == GET_FROM_STRING)
7514 goto consider_string_end;
7517 break;
7519 case GET_FROM_IMAGE:
7520 case GET_FROM_STRETCH:
7521 /* The position etc with which we have to proceed are on
7522 the stack. The position may be at the end of a string,
7523 if the `display' property takes up the whole string. */
7524 eassert (it->sp > 0);
7525 pop_it (it);
7526 if (it->method == GET_FROM_STRING)
7527 goto consider_string_end;
7528 break;
7530 default:
7531 /* There are no other methods defined, so this should be a bug. */
7532 emacs_abort ();
7535 eassert (it->method != GET_FROM_STRING
7536 || (STRINGP (it->string)
7537 && IT_STRING_CHARPOS (*it) >= 0));
7540 /* Load IT's display element fields with information about the next
7541 display element which comes from a display table entry or from the
7542 result of translating a control character to one of the forms `^C'
7543 or `\003'.
7545 IT->dpvec holds the glyphs to return as characters.
7546 IT->saved_face_id holds the face id before the display vector--it
7547 is restored into IT->face_id in set_iterator_to_next. */
7549 static int
7550 next_element_from_display_vector (struct it *it)
7552 Lisp_Object gc;
7553 int prev_face_id = it->face_id;
7554 int next_face_id;
7556 /* Precondition. */
7557 eassert (it->dpvec && it->current.dpvec_index >= 0);
7559 it->face_id = it->saved_face_id;
7561 /* KFS: This code used to check ip->dpvec[0] instead of the current element.
7562 That seemed totally bogus - so I changed it... */
7563 gc = it->dpvec[it->current.dpvec_index];
7565 if (GLYPH_CODE_P (gc))
7567 struct face *this_face, *prev_face, *next_face;
7569 it->c = GLYPH_CODE_CHAR (gc);
7570 it->len = CHAR_BYTES (it->c);
7572 /* The entry may contain a face id to use. Such a face id is
7573 the id of a Lisp face, not a realized face. A face id of
7574 zero means no face is specified. */
7575 if (it->dpvec_face_id >= 0)
7576 it->face_id = it->dpvec_face_id;
7577 else
7579 int lface_id = GLYPH_CODE_FACE (gc);
7580 if (lface_id > 0)
7581 it->face_id = merge_faces (it->f, Qt, lface_id,
7582 it->saved_face_id);
7585 /* Glyphs in the display vector could have the box face, so we
7586 need to set the related flags in the iterator, as
7587 appropriate. */
7588 this_face = FACE_FROM_ID (it->f, it->face_id);
7589 prev_face = FACE_FROM_ID (it->f, prev_face_id);
7591 /* Is this character the first character of a box-face run? */
7592 it->start_of_box_run_p = (this_face && this_face->box != FACE_NO_BOX
7593 && (!prev_face
7594 || prev_face->box == FACE_NO_BOX));
7596 /* For the last character of the box-face run, we need to look
7597 either at the next glyph from the display vector, or at the
7598 face we saw before the display vector. */
7599 next_face_id = it->saved_face_id;
7600 if (it->current.dpvec_index < it->dpend - it->dpvec - 1)
7602 if (it->dpvec_face_id >= 0)
7603 next_face_id = it->dpvec_face_id;
7604 else
7606 int lface_id =
7607 GLYPH_CODE_FACE (it->dpvec[it->current.dpvec_index + 1]);
7609 if (lface_id > 0)
7610 next_face_id = merge_faces (it->f, Qt, lface_id,
7611 it->saved_face_id);
7614 next_face = FACE_FROM_ID (it->f, next_face_id);
7615 it->end_of_box_run_p = (this_face && this_face->box != FACE_NO_BOX
7616 && (!next_face
7617 || next_face->box == FACE_NO_BOX));
7618 it->face_box_p = this_face && this_face->box != FACE_NO_BOX;
7620 else
7621 /* Display table entry is invalid. Return a space. */
7622 it->c = ' ', it->len = 1;
7624 /* Don't change position and object of the iterator here. They are
7625 still the values of the character that had this display table
7626 entry or was translated, and that's what we want. */
7627 it->what = IT_CHARACTER;
7628 return 1;
7631 /* Get the first element of string/buffer in the visual order, after
7632 being reseated to a new position in a string or a buffer. */
7633 static void
7634 get_visually_first_element (struct it *it)
7636 int string_p = STRINGP (it->string) || it->s;
7637 ptrdiff_t eob = (string_p ? it->bidi_it.string.schars : ZV);
7638 ptrdiff_t bob = (string_p ? 0 : BEGV);
7640 if (STRINGP (it->string))
7642 it->bidi_it.charpos = IT_STRING_CHARPOS (*it);
7643 it->bidi_it.bytepos = IT_STRING_BYTEPOS (*it);
7645 else
7647 it->bidi_it.charpos = IT_CHARPOS (*it);
7648 it->bidi_it.bytepos = IT_BYTEPOS (*it);
7651 if (it->bidi_it.charpos == eob)
7653 /* Nothing to do, but reset the FIRST_ELT flag, like
7654 bidi_paragraph_init does, because we are not going to
7655 call it. */
7656 it->bidi_it.first_elt = 0;
7658 else if (it->bidi_it.charpos == bob
7659 || (!string_p
7660 && (FETCH_CHAR (it->bidi_it.bytepos - 1) == '\n'
7661 || FETCH_CHAR (it->bidi_it.bytepos) == '\n')))
7663 /* If we are at the beginning of a line/string, we can produce
7664 the next element right away. */
7665 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
7666 bidi_move_to_visually_next (&it->bidi_it);
7668 else
7670 ptrdiff_t orig_bytepos = it->bidi_it.bytepos;
7672 /* We need to prime the bidi iterator starting at the line's or
7673 string's beginning, before we will be able to produce the
7674 next element. */
7675 if (string_p)
7676 it->bidi_it.charpos = it->bidi_it.bytepos = 0;
7677 else
7678 it->bidi_it.charpos = find_newline_no_quit (IT_CHARPOS (*it),
7679 IT_BYTEPOS (*it), -1,
7680 &it->bidi_it.bytepos);
7681 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
7684 /* Now return to buffer/string position where we were asked
7685 to get the next display element, and produce that. */
7686 bidi_move_to_visually_next (&it->bidi_it);
7688 while (it->bidi_it.bytepos != orig_bytepos
7689 && it->bidi_it.charpos < eob);
7692 /* Adjust IT's position information to where we ended up. */
7693 if (STRINGP (it->string))
7695 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7696 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7698 else
7700 IT_CHARPOS (*it) = it->bidi_it.charpos;
7701 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7704 if (STRINGP (it->string) || !it->s)
7706 ptrdiff_t stop, charpos, bytepos;
7708 if (STRINGP (it->string))
7710 eassert (!it->s);
7711 stop = SCHARS (it->string);
7712 if (stop > it->end_charpos)
7713 stop = it->end_charpos;
7714 charpos = IT_STRING_CHARPOS (*it);
7715 bytepos = IT_STRING_BYTEPOS (*it);
7717 else
7719 stop = it->end_charpos;
7720 charpos = IT_CHARPOS (*it);
7721 bytepos = IT_BYTEPOS (*it);
7723 if (it->bidi_it.scan_dir < 0)
7724 stop = -1;
7725 composition_compute_stop_pos (&it->cmp_it, charpos, bytepos, stop,
7726 it->string);
7730 /* Load IT with the next display element from Lisp string IT->string.
7731 IT->current.string_pos is the current position within the string.
7732 If IT->current.overlay_string_index >= 0, the Lisp string is an
7733 overlay string. */
7735 static int
7736 next_element_from_string (struct it *it)
7738 struct text_pos position;
7740 eassert (STRINGP (it->string));
7741 eassert (!it->bidi_p || EQ (it->string, it->bidi_it.string.lstring));
7742 eassert (IT_STRING_CHARPOS (*it) >= 0);
7743 position = it->current.string_pos;
7745 /* With bidi reordering, the character to display might not be the
7746 character at IT_STRING_CHARPOS. BIDI_IT.FIRST_ELT non-zero means
7747 that we were reseat()ed to a new string, whose paragraph
7748 direction is not known. */
7749 if (it->bidi_p && it->bidi_it.first_elt)
7751 get_visually_first_element (it);
7752 SET_TEXT_POS (position, IT_STRING_CHARPOS (*it), IT_STRING_BYTEPOS (*it));
7755 /* Time to check for invisible text? */
7756 if (IT_STRING_CHARPOS (*it) < it->end_charpos)
7758 if (IT_STRING_CHARPOS (*it) >= it->stop_charpos)
7760 if (!(!it->bidi_p
7761 || BIDI_AT_BASE_LEVEL (it->bidi_it)
7762 || IT_STRING_CHARPOS (*it) == it->stop_charpos))
7764 /* With bidi non-linear iteration, we could find
7765 ourselves far beyond the last computed stop_charpos,
7766 with several other stop positions in between that we
7767 missed. Scan them all now, in buffer's logical
7768 order, until we find and handle the last stop_charpos
7769 that precedes our current position. */
7770 handle_stop_backwards (it, it->stop_charpos);
7771 return GET_NEXT_DISPLAY_ELEMENT (it);
7773 else
7775 if (it->bidi_p)
7777 /* Take note of the stop position we just moved
7778 across, for when we will move back across it. */
7779 it->prev_stop = it->stop_charpos;
7780 /* If we are at base paragraph embedding level, take
7781 note of the last stop position seen at this
7782 level. */
7783 if (BIDI_AT_BASE_LEVEL (it->bidi_it))
7784 it->base_level_stop = it->stop_charpos;
7786 handle_stop (it);
7788 /* Since a handler may have changed IT->method, we must
7789 recurse here. */
7790 return GET_NEXT_DISPLAY_ELEMENT (it);
7793 else if (it->bidi_p
7794 /* If we are before prev_stop, we may have overstepped
7795 on our way backwards a stop_pos, and if so, we need
7796 to handle that stop_pos. */
7797 && IT_STRING_CHARPOS (*it) < it->prev_stop
7798 /* We can sometimes back up for reasons that have nothing
7799 to do with bidi reordering. E.g., compositions. The
7800 code below is only needed when we are above the base
7801 embedding level, so test for that explicitly. */
7802 && !BIDI_AT_BASE_LEVEL (it->bidi_it))
7804 /* If we lost track of base_level_stop, we have no better
7805 place for handle_stop_backwards to start from than string
7806 beginning. This happens, e.g., when we were reseated to
7807 the previous screenful of text by vertical-motion. */
7808 if (it->base_level_stop <= 0
7809 || IT_STRING_CHARPOS (*it) < it->base_level_stop)
7810 it->base_level_stop = 0;
7811 handle_stop_backwards (it, it->base_level_stop);
7812 return GET_NEXT_DISPLAY_ELEMENT (it);
7816 if (it->current.overlay_string_index >= 0)
7818 /* Get the next character from an overlay string. In overlay
7819 strings, there is no field width or padding with spaces to
7820 do. */
7821 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7823 it->what = IT_EOB;
7824 return 0;
7826 else if (CHAR_COMPOSED_P (it, IT_STRING_CHARPOS (*it),
7827 IT_STRING_BYTEPOS (*it),
7828 it->bidi_it.scan_dir < 0
7829 ? -1
7830 : SCHARS (it->string))
7831 && next_element_from_composition (it))
7833 return 1;
7835 else if (STRING_MULTIBYTE (it->string))
7837 const unsigned char *s = (SDATA (it->string)
7838 + IT_STRING_BYTEPOS (*it));
7839 it->c = string_char_and_length (s, &it->len);
7841 else
7843 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
7844 it->len = 1;
7847 else
7849 /* Get the next character from a Lisp string that is not an
7850 overlay string. Such strings come from the mode line, for
7851 example. We may have to pad with spaces, or truncate the
7852 string. See also next_element_from_c_string. */
7853 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
7855 it->what = IT_EOB;
7856 return 0;
7858 else if (IT_STRING_CHARPOS (*it) >= it->string_nchars)
7860 /* Pad with spaces. */
7861 it->c = ' ', it->len = 1;
7862 CHARPOS (position) = BYTEPOS (position) = -1;
7864 else if (CHAR_COMPOSED_P (it, IT_STRING_CHARPOS (*it),
7865 IT_STRING_BYTEPOS (*it),
7866 it->bidi_it.scan_dir < 0
7867 ? -1
7868 : it->string_nchars)
7869 && next_element_from_composition (it))
7871 return 1;
7873 else if (STRING_MULTIBYTE (it->string))
7875 const unsigned char *s = (SDATA (it->string)
7876 + IT_STRING_BYTEPOS (*it));
7877 it->c = string_char_and_length (s, &it->len);
7879 else
7881 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
7882 it->len = 1;
7886 /* Record what we have and where it came from. */
7887 it->what = IT_CHARACTER;
7888 it->object = it->string;
7889 it->position = position;
7890 return 1;
7894 /* Load IT with next display element from C string IT->s.
7895 IT->string_nchars is the maximum number of characters to return
7896 from the string. IT->end_charpos may be greater than
7897 IT->string_nchars when this function is called, in which case we
7898 may have to return padding spaces. Value is zero if end of string
7899 reached, including padding spaces. */
7901 static int
7902 next_element_from_c_string (struct it *it)
7904 bool success_p = true;
7906 eassert (it->s);
7907 eassert (!it->bidi_p || it->s == it->bidi_it.string.s);
7908 it->what = IT_CHARACTER;
7909 BYTEPOS (it->position) = CHARPOS (it->position) = 0;
7910 it->object = Qnil;
7912 /* With bidi reordering, the character to display might not be the
7913 character at IT_CHARPOS. BIDI_IT.FIRST_ELT non-zero means that
7914 we were reseated to a new string, whose paragraph direction is
7915 not known. */
7916 if (it->bidi_p && it->bidi_it.first_elt)
7917 get_visually_first_element (it);
7919 /* IT's position can be greater than IT->string_nchars in case a
7920 field width or precision has been specified when the iterator was
7921 initialized. */
7922 if (IT_CHARPOS (*it) >= it->end_charpos)
7924 /* End of the game. */
7925 it->what = IT_EOB;
7926 success_p = 0;
7928 else if (IT_CHARPOS (*it) >= it->string_nchars)
7930 /* Pad with spaces. */
7931 it->c = ' ', it->len = 1;
7932 BYTEPOS (it->position) = CHARPOS (it->position) = -1;
7934 else if (it->multibyte_p)
7935 it->c = string_char_and_length (it->s + IT_BYTEPOS (*it), &it->len);
7936 else
7937 it->c = it->s[IT_BYTEPOS (*it)], it->len = 1;
7939 return success_p;
7943 /* Set up IT to return characters from an ellipsis, if appropriate.
7944 The definition of the ellipsis glyphs may come from a display table
7945 entry. This function fills IT with the first glyph from the
7946 ellipsis if an ellipsis is to be displayed. */
7948 static int
7949 next_element_from_ellipsis (struct it *it)
7951 if (it->selective_display_ellipsis_p)
7952 setup_for_ellipsis (it, it->len);
7953 else
7955 /* The face at the current position may be different from the
7956 face we find after the invisible text. Remember what it
7957 was in IT->saved_face_id, and signal that it's there by
7958 setting face_before_selective_p. */
7959 it->saved_face_id = it->face_id;
7960 it->method = GET_FROM_BUFFER;
7961 it->object = it->w->contents;
7962 reseat_at_next_visible_line_start (it, 1);
7963 it->face_before_selective_p = true;
7966 return GET_NEXT_DISPLAY_ELEMENT (it);
7970 /* Deliver an image display element. The iterator IT is already
7971 filled with image information (done in handle_display_prop). Value
7972 is always 1. */
7975 static int
7976 next_element_from_image (struct it *it)
7978 it->what = IT_IMAGE;
7979 it->ignore_overlay_strings_at_pos_p = 0;
7980 return 1;
7984 /* Fill iterator IT with next display element from a stretch glyph
7985 property. IT->object is the value of the text property. Value is
7986 always 1. */
7988 static int
7989 next_element_from_stretch (struct it *it)
7991 it->what = IT_STRETCH;
7992 return 1;
7995 /* Scan backwards from IT's current position until we find a stop
7996 position, or until BEGV. This is called when we find ourself
7997 before both the last known prev_stop and base_level_stop while
7998 reordering bidirectional text. */
8000 static void
8001 compute_stop_pos_backwards (struct it *it)
8003 const int SCAN_BACK_LIMIT = 1000;
8004 struct text_pos pos;
8005 struct display_pos save_current = it->current;
8006 struct text_pos save_position = it->position;
8007 ptrdiff_t charpos = IT_CHARPOS (*it);
8008 ptrdiff_t where_we_are = charpos;
8009 ptrdiff_t save_stop_pos = it->stop_charpos;
8010 ptrdiff_t save_end_pos = it->end_charpos;
8012 eassert (NILP (it->string) && !it->s);
8013 eassert (it->bidi_p);
8014 it->bidi_p = 0;
8017 it->end_charpos = min (charpos + 1, ZV);
8018 charpos = max (charpos - SCAN_BACK_LIMIT, BEGV);
8019 SET_TEXT_POS (pos, charpos, CHAR_TO_BYTE (charpos));
8020 reseat_1 (it, pos, 0);
8021 compute_stop_pos (it);
8022 /* We must advance forward, right? */
8023 if (it->stop_charpos <= charpos)
8024 emacs_abort ();
8026 while (charpos > BEGV && it->stop_charpos >= it->end_charpos);
8028 if (it->stop_charpos <= where_we_are)
8029 it->prev_stop = it->stop_charpos;
8030 else
8031 it->prev_stop = BEGV;
8032 it->bidi_p = true;
8033 it->current = save_current;
8034 it->position = save_position;
8035 it->stop_charpos = save_stop_pos;
8036 it->end_charpos = save_end_pos;
8039 /* Scan forward from CHARPOS in the current buffer/string, until we
8040 find a stop position > current IT's position. Then handle the stop
8041 position before that. This is called when we bump into a stop
8042 position while reordering bidirectional text. CHARPOS should be
8043 the last previously processed stop_pos (or BEGV/0, if none were
8044 processed yet) whose position is less that IT's current
8045 position. */
8047 static void
8048 handle_stop_backwards (struct it *it, ptrdiff_t charpos)
8050 int bufp = !STRINGP (it->string);
8051 ptrdiff_t where_we_are = (bufp ? IT_CHARPOS (*it) : IT_STRING_CHARPOS (*it));
8052 struct display_pos save_current = it->current;
8053 struct text_pos save_position = it->position;
8054 struct text_pos pos1;
8055 ptrdiff_t next_stop;
8057 /* Scan in strict logical order. */
8058 eassert (it->bidi_p);
8059 it->bidi_p = 0;
8062 it->prev_stop = charpos;
8063 if (bufp)
8065 SET_TEXT_POS (pos1, charpos, CHAR_TO_BYTE (charpos));
8066 reseat_1 (it, pos1, 0);
8068 else
8069 it->current.string_pos = string_pos (charpos, it->string);
8070 compute_stop_pos (it);
8071 /* We must advance forward, right? */
8072 if (it->stop_charpos <= it->prev_stop)
8073 emacs_abort ();
8074 charpos = it->stop_charpos;
8076 while (charpos <= where_we_are);
8078 it->bidi_p = true;
8079 it->current = save_current;
8080 it->position = save_position;
8081 next_stop = it->stop_charpos;
8082 it->stop_charpos = it->prev_stop;
8083 handle_stop (it);
8084 it->stop_charpos = next_stop;
8087 /* Load IT with the next display element from current_buffer. Value
8088 is zero if end of buffer reached. IT->stop_charpos is the next
8089 position at which to stop and check for text properties or buffer
8090 end. */
8092 static int
8093 next_element_from_buffer (struct it *it)
8095 bool success_p = true;
8097 eassert (IT_CHARPOS (*it) >= BEGV);
8098 eassert (NILP (it->string) && !it->s);
8099 eassert (!it->bidi_p
8100 || (EQ (it->bidi_it.string.lstring, Qnil)
8101 && it->bidi_it.string.s == NULL));
8103 /* With bidi reordering, the character to display might not be the
8104 character at IT_CHARPOS. BIDI_IT.FIRST_ELT non-zero means that
8105 we were reseat()ed to a new buffer position, which is potentially
8106 a different paragraph. */
8107 if (it->bidi_p && it->bidi_it.first_elt)
8109 get_visually_first_element (it);
8110 SET_TEXT_POS (it->position, IT_CHARPOS (*it), IT_BYTEPOS (*it));
8113 if (IT_CHARPOS (*it) >= it->stop_charpos)
8115 if (IT_CHARPOS (*it) >= it->end_charpos)
8117 int overlay_strings_follow_p;
8119 /* End of the game, except when overlay strings follow that
8120 haven't been returned yet. */
8121 if (it->overlay_strings_at_end_processed_p)
8122 overlay_strings_follow_p = 0;
8123 else
8125 it->overlay_strings_at_end_processed_p = true;
8126 overlay_strings_follow_p = get_overlay_strings (it, 0);
8129 if (overlay_strings_follow_p)
8130 success_p = GET_NEXT_DISPLAY_ELEMENT (it);
8131 else
8133 it->what = IT_EOB;
8134 it->position = it->current.pos;
8135 success_p = 0;
8138 else if (!(!it->bidi_p
8139 || BIDI_AT_BASE_LEVEL (it->bidi_it)
8140 || IT_CHARPOS (*it) == it->stop_charpos))
8142 /* With bidi non-linear iteration, we could find ourselves
8143 far beyond the last computed stop_charpos, with several
8144 other stop positions in between that we missed. Scan
8145 them all now, in buffer's logical order, until we find
8146 and handle the last stop_charpos that precedes our
8147 current position. */
8148 handle_stop_backwards (it, it->stop_charpos);
8149 return GET_NEXT_DISPLAY_ELEMENT (it);
8151 else
8153 if (it->bidi_p)
8155 /* Take note of the stop position we just moved across,
8156 for when we will move back across it. */
8157 it->prev_stop = it->stop_charpos;
8158 /* If we are at base paragraph embedding level, take
8159 note of the last stop position seen at this
8160 level. */
8161 if (BIDI_AT_BASE_LEVEL (it->bidi_it))
8162 it->base_level_stop = it->stop_charpos;
8164 handle_stop (it);
8165 return GET_NEXT_DISPLAY_ELEMENT (it);
8168 else if (it->bidi_p
8169 /* If we are before prev_stop, we may have overstepped on
8170 our way backwards a stop_pos, and if so, we need to
8171 handle that stop_pos. */
8172 && IT_CHARPOS (*it) < it->prev_stop
8173 /* We can sometimes back up for reasons that have nothing
8174 to do with bidi reordering. E.g., compositions. The
8175 code below is only needed when we are above the base
8176 embedding level, so test for that explicitly. */
8177 && !BIDI_AT_BASE_LEVEL (it->bidi_it))
8179 if (it->base_level_stop <= 0
8180 || IT_CHARPOS (*it) < it->base_level_stop)
8182 /* If we lost track of base_level_stop, we need to find
8183 prev_stop by looking backwards. This happens, e.g., when
8184 we were reseated to the previous screenful of text by
8185 vertical-motion. */
8186 it->base_level_stop = BEGV;
8187 compute_stop_pos_backwards (it);
8188 handle_stop_backwards (it, it->prev_stop);
8190 else
8191 handle_stop_backwards (it, it->base_level_stop);
8192 return GET_NEXT_DISPLAY_ELEMENT (it);
8194 else
8196 /* No face changes, overlays etc. in sight, so just return a
8197 character from current_buffer. */
8198 unsigned char *p;
8199 ptrdiff_t stop;
8201 /* Maybe run the redisplay end trigger hook. Performance note:
8202 This doesn't seem to cost measurable time. */
8203 if (it->redisplay_end_trigger_charpos
8204 && it->glyph_row
8205 && IT_CHARPOS (*it) >= it->redisplay_end_trigger_charpos)
8206 run_redisplay_end_trigger_hook (it);
8208 stop = it->bidi_it.scan_dir < 0 ? -1 : it->end_charpos;
8209 if (CHAR_COMPOSED_P (it, IT_CHARPOS (*it), IT_BYTEPOS (*it),
8210 stop)
8211 && next_element_from_composition (it))
8213 return 1;
8216 /* Get the next character, maybe multibyte. */
8217 p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
8218 if (it->multibyte_p && !ASCII_BYTE_P (*p))
8219 it->c = STRING_CHAR_AND_LENGTH (p, it->len);
8220 else
8221 it->c = *p, it->len = 1;
8223 /* Record what we have and where it came from. */
8224 it->what = IT_CHARACTER;
8225 it->object = it->w->contents;
8226 it->position = it->current.pos;
8228 /* Normally we return the character found above, except when we
8229 really want to return an ellipsis for selective display. */
8230 if (it->selective)
8232 if (it->c == '\n')
8234 /* A value of selective > 0 means hide lines indented more
8235 than that number of columns. */
8236 if (it->selective > 0
8237 && IT_CHARPOS (*it) + 1 < ZV
8238 && indented_beyond_p (IT_CHARPOS (*it) + 1,
8239 IT_BYTEPOS (*it) + 1,
8240 it->selective))
8242 success_p = next_element_from_ellipsis (it);
8243 it->dpvec_char_len = -1;
8246 else if (it->c == '\r' && it->selective == -1)
8248 /* A value of selective == -1 means that everything from the
8249 CR to the end of the line is invisible, with maybe an
8250 ellipsis displayed for it. */
8251 success_p = next_element_from_ellipsis (it);
8252 it->dpvec_char_len = -1;
8257 /* Value is zero if end of buffer reached. */
8258 eassert (!success_p || it->what != IT_CHARACTER || it->len > 0);
8259 return success_p;
8263 /* Run the redisplay end trigger hook for IT. */
8265 static void
8266 run_redisplay_end_trigger_hook (struct it *it)
8268 Lisp_Object args[3];
8270 /* IT->glyph_row should be non-null, i.e. we should be actually
8271 displaying something, or otherwise we should not run the hook. */
8272 eassert (it->glyph_row);
8274 /* Set up hook arguments. */
8275 args[0] = Qredisplay_end_trigger_functions;
8276 args[1] = it->window;
8277 XSETINT (args[2], it->redisplay_end_trigger_charpos);
8278 it->redisplay_end_trigger_charpos = 0;
8280 /* Since we are *trying* to run these functions, don't try to run
8281 them again, even if they get an error. */
8282 wset_redisplay_end_trigger (it->w, Qnil);
8283 Frun_hook_with_args (3, args);
8285 /* Notice if it changed the face of the character we are on. */
8286 handle_face_prop (it);
8290 /* Deliver a composition display element. Unlike the other
8291 next_element_from_XXX, this function is not registered in the array
8292 get_next_element[]. It is called from next_element_from_buffer and
8293 next_element_from_string when necessary. */
8295 static int
8296 next_element_from_composition (struct it *it)
8298 it->what = IT_COMPOSITION;
8299 it->len = it->cmp_it.nbytes;
8300 if (STRINGP (it->string))
8302 if (it->c < 0)
8304 IT_STRING_CHARPOS (*it) += it->cmp_it.nchars;
8305 IT_STRING_BYTEPOS (*it) += it->cmp_it.nbytes;
8306 return 0;
8308 it->position = it->current.string_pos;
8309 it->object = it->string;
8310 it->c = composition_update_it (&it->cmp_it, IT_STRING_CHARPOS (*it),
8311 IT_STRING_BYTEPOS (*it), it->string);
8313 else
8315 if (it->c < 0)
8317 IT_CHARPOS (*it) += it->cmp_it.nchars;
8318 IT_BYTEPOS (*it) += it->cmp_it.nbytes;
8319 if (it->bidi_p)
8321 if (it->bidi_it.new_paragraph)
8322 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 0);
8323 /* Resync the bidi iterator with IT's new position.
8324 FIXME: this doesn't support bidirectional text. */
8325 while (it->bidi_it.charpos < IT_CHARPOS (*it))
8326 bidi_move_to_visually_next (&it->bidi_it);
8328 return 0;
8330 it->position = it->current.pos;
8331 it->object = it->w->contents;
8332 it->c = composition_update_it (&it->cmp_it, IT_CHARPOS (*it),
8333 IT_BYTEPOS (*it), Qnil);
8335 return 1;
8340 /***********************************************************************
8341 Moving an iterator without producing glyphs
8342 ***********************************************************************/
8344 /* Check if iterator is at a position corresponding to a valid buffer
8345 position after some move_it_ call. */
8347 #define IT_POS_VALID_AFTER_MOVE_P(it) \
8348 ((it)->method == GET_FROM_STRING \
8349 ? IT_STRING_CHARPOS (*it) == 0 \
8350 : 1)
8353 /* Move iterator IT to a specified buffer or X position within one
8354 line on the display without producing glyphs.
8356 OP should be a bit mask including some or all of these bits:
8357 MOVE_TO_X: Stop upon reaching x-position TO_X.
8358 MOVE_TO_POS: Stop upon reaching buffer or string position TO_CHARPOS.
8359 Regardless of OP's value, stop upon reaching the end of the display line.
8361 TO_X is normally a value 0 <= TO_X <= IT->last_visible_x.
8362 This means, in particular, that TO_X includes window's horizontal
8363 scroll amount.
8365 The return value has several possible values that
8366 say what condition caused the scan to stop:
8368 MOVE_POS_MATCH_OR_ZV
8369 - when TO_POS or ZV was reached.
8371 MOVE_X_REACHED
8372 -when TO_X was reached before TO_POS or ZV were reached.
8374 MOVE_LINE_CONTINUED
8375 - when we reached the end of the display area and the line must
8376 be continued.
8378 MOVE_LINE_TRUNCATED
8379 - when we reached the end of the display area and the line is
8380 truncated.
8382 MOVE_NEWLINE_OR_CR
8383 - when we stopped at a line end, i.e. a newline or a CR and selective
8384 display is on. */
8386 static enum move_it_result
8387 move_it_in_display_line_to (struct it *it,
8388 ptrdiff_t to_charpos, int to_x,
8389 enum move_operation_enum op)
8391 enum move_it_result result = MOVE_UNDEFINED;
8392 struct glyph_row *saved_glyph_row;
8393 struct it wrap_it, atpos_it, atx_it, ppos_it;
8394 void *wrap_data = NULL, *atpos_data = NULL, *atx_data = NULL;
8395 void *ppos_data = NULL;
8396 int may_wrap = 0;
8397 enum it_method prev_method = it->method;
8398 ptrdiff_t closest_pos IF_LINT (= 0), prev_pos = IT_CHARPOS (*it);
8399 int saw_smaller_pos = prev_pos < to_charpos;
8401 /* Don't produce glyphs in produce_glyphs. */
8402 saved_glyph_row = it->glyph_row;
8403 it->glyph_row = NULL;
8405 /* Use wrap_it to save a copy of IT wherever a word wrap could
8406 occur. Use atpos_it to save a copy of IT at the desired buffer
8407 position, if found, so that we can scan ahead and check if the
8408 word later overshoots the window edge. Use atx_it similarly, for
8409 pixel positions. */
8410 wrap_it.sp = -1;
8411 atpos_it.sp = -1;
8412 atx_it.sp = -1;
8414 /* Use ppos_it under bidi reordering to save a copy of IT for the
8415 initial position. We restore that position in IT when we have
8416 scanned the entire display line without finding a match for
8417 TO_CHARPOS and all the character positions are greater than
8418 TO_CHARPOS. We then restart the scan from the initial position,
8419 and stop at CLOSEST_POS, which is a position > TO_CHARPOS that is
8420 the closest to TO_CHARPOS. */
8421 if (it->bidi_p)
8423 if ((op & MOVE_TO_POS) && IT_CHARPOS (*it) >= to_charpos)
8425 SAVE_IT (ppos_it, *it, ppos_data);
8426 closest_pos = IT_CHARPOS (*it);
8428 else
8429 closest_pos = ZV;
8432 #define BUFFER_POS_REACHED_P() \
8433 ((op & MOVE_TO_POS) != 0 \
8434 && BUFFERP (it->object) \
8435 && (IT_CHARPOS (*it) == to_charpos \
8436 || ((!it->bidi_p \
8437 || BIDI_AT_BASE_LEVEL (it->bidi_it)) \
8438 && IT_CHARPOS (*it) > to_charpos) \
8439 || (it->what == IT_COMPOSITION \
8440 && ((IT_CHARPOS (*it) > to_charpos \
8441 && to_charpos >= it->cmp_it.charpos) \
8442 || (IT_CHARPOS (*it) < to_charpos \
8443 && to_charpos <= it->cmp_it.charpos)))) \
8444 && (it->method == GET_FROM_BUFFER \
8445 || (it->method == GET_FROM_DISPLAY_VECTOR \
8446 && it->dpvec + it->current.dpvec_index + 1 >= it->dpend)))
8448 /* If there's a line-/wrap-prefix, handle it. */
8449 if (it->hpos == 0 && it->method == GET_FROM_BUFFER
8450 && it->current_y < it->last_visible_y)
8451 handle_line_prefix (it);
8453 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8454 SET_TEXT_POS (this_line_min_pos, IT_CHARPOS (*it), IT_BYTEPOS (*it));
8456 while (1)
8458 int x, i, ascent = 0, descent = 0;
8460 /* Utility macro to reset an iterator with x, ascent, and descent. */
8461 #define IT_RESET_X_ASCENT_DESCENT(IT) \
8462 ((IT)->current_x = x, (IT)->max_ascent = ascent, \
8463 (IT)->max_descent = descent)
8465 /* Stop if we move beyond TO_CHARPOS (after an image or a
8466 display string or stretch glyph). */
8467 if ((op & MOVE_TO_POS) != 0
8468 && BUFFERP (it->object)
8469 && it->method == GET_FROM_BUFFER
8470 && (((!it->bidi_p
8471 /* When the iterator is at base embedding level, we
8472 are guaranteed that characters are delivered for
8473 display in strictly increasing order of their
8474 buffer positions. */
8475 || BIDI_AT_BASE_LEVEL (it->bidi_it))
8476 && IT_CHARPOS (*it) > to_charpos)
8477 || (it->bidi_p
8478 && (prev_method == GET_FROM_IMAGE
8479 || prev_method == GET_FROM_STRETCH
8480 || prev_method == GET_FROM_STRING)
8481 /* Passed TO_CHARPOS from left to right. */
8482 && ((prev_pos < to_charpos
8483 && IT_CHARPOS (*it) > to_charpos)
8484 /* Passed TO_CHARPOS from right to left. */
8485 || (prev_pos > to_charpos
8486 && IT_CHARPOS (*it) < to_charpos)))))
8488 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8490 result = MOVE_POS_MATCH_OR_ZV;
8491 break;
8493 else if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
8494 /* If wrap_it is valid, the current position might be in a
8495 word that is wrapped. So, save the iterator in
8496 atpos_it and continue to see if wrapping happens. */
8497 SAVE_IT (atpos_it, *it, atpos_data);
8500 /* Stop when ZV reached.
8501 We used to stop here when TO_CHARPOS reached as well, but that is
8502 too soon if this glyph does not fit on this line. So we handle it
8503 explicitly below. */
8504 if (!get_next_display_element (it))
8506 result = MOVE_POS_MATCH_OR_ZV;
8507 break;
8510 if (it->line_wrap == TRUNCATE)
8512 if (BUFFER_POS_REACHED_P ())
8514 result = MOVE_POS_MATCH_OR_ZV;
8515 break;
8518 else
8520 if (it->line_wrap == WORD_WRAP)
8522 if (IT_DISPLAYING_WHITESPACE (it))
8523 may_wrap = 1;
8524 else if (may_wrap)
8526 /* We have reached a glyph that follows one or more
8527 whitespace characters. If the position is
8528 already found, we are done. */
8529 if (atpos_it.sp >= 0)
8531 RESTORE_IT (it, &atpos_it, atpos_data);
8532 result = MOVE_POS_MATCH_OR_ZV;
8533 goto done;
8535 if (atx_it.sp >= 0)
8537 RESTORE_IT (it, &atx_it, atx_data);
8538 result = MOVE_X_REACHED;
8539 goto done;
8541 /* Otherwise, we can wrap here. */
8542 SAVE_IT (wrap_it, *it, wrap_data);
8543 may_wrap = 0;
8548 /* Remember the line height for the current line, in case
8549 the next element doesn't fit on the line. */
8550 ascent = it->max_ascent;
8551 descent = it->max_descent;
8553 /* The call to produce_glyphs will get the metrics of the
8554 display element IT is loaded with. Record the x-position
8555 before this display element, in case it doesn't fit on the
8556 line. */
8557 x = it->current_x;
8559 PRODUCE_GLYPHS (it);
8561 if (it->area != TEXT_AREA)
8563 prev_method = it->method;
8564 if (it->method == GET_FROM_BUFFER)
8565 prev_pos = IT_CHARPOS (*it);
8566 set_iterator_to_next (it, 1);
8567 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8568 SET_TEXT_POS (this_line_min_pos,
8569 IT_CHARPOS (*it), IT_BYTEPOS (*it));
8570 if (it->bidi_p
8571 && (op & MOVE_TO_POS)
8572 && IT_CHARPOS (*it) > to_charpos
8573 && IT_CHARPOS (*it) < closest_pos)
8574 closest_pos = IT_CHARPOS (*it);
8575 continue;
8578 /* The number of glyphs we get back in IT->nglyphs will normally
8579 be 1 except when IT->c is (i) a TAB, or (ii) a multi-glyph
8580 character on a terminal frame, or (iii) a line end. For the
8581 second case, IT->nglyphs - 1 padding glyphs will be present.
8582 (On X frames, there is only one glyph produced for a
8583 composite character.)
8585 The behavior implemented below means, for continuation lines,
8586 that as many spaces of a TAB as fit on the current line are
8587 displayed there. For terminal frames, as many glyphs of a
8588 multi-glyph character are displayed in the current line, too.
8589 This is what the old redisplay code did, and we keep it that
8590 way. Under X, the whole shape of a complex character must
8591 fit on the line or it will be completely displayed in the
8592 next line.
8594 Note that both for tabs and padding glyphs, all glyphs have
8595 the same width. */
8596 if (it->nglyphs)
8598 /* More than one glyph or glyph doesn't fit on line. All
8599 glyphs have the same width. */
8600 int single_glyph_width = it->pixel_width / it->nglyphs;
8601 int new_x;
8602 int x_before_this_char = x;
8603 int hpos_before_this_char = it->hpos;
8605 for (i = 0; i < it->nglyphs; ++i, x = new_x)
8607 new_x = x + single_glyph_width;
8609 /* We want to leave anything reaching TO_X to the caller. */
8610 if ((op & MOVE_TO_X) && new_x > to_x)
8612 if (BUFFER_POS_REACHED_P ())
8614 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8615 goto buffer_pos_reached;
8616 if (atpos_it.sp < 0)
8618 SAVE_IT (atpos_it, *it, atpos_data);
8619 IT_RESET_X_ASCENT_DESCENT (&atpos_it);
8622 else
8624 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8626 it->current_x = x;
8627 result = MOVE_X_REACHED;
8628 break;
8630 if (atx_it.sp < 0)
8632 SAVE_IT (atx_it, *it, atx_data);
8633 IT_RESET_X_ASCENT_DESCENT (&atx_it);
8638 if (/* Lines are continued. */
8639 it->line_wrap != TRUNCATE
8640 && (/* And glyph doesn't fit on the line. */
8641 new_x > it->last_visible_x
8642 /* Or it fits exactly and we're on a window
8643 system frame. */
8644 || (new_x == it->last_visible_x
8645 && FRAME_WINDOW_P (it->f)
8646 && ((it->bidi_p && it->bidi_it.paragraph_dir == R2L)
8647 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
8648 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)))))
8650 if (/* IT->hpos == 0 means the very first glyph
8651 doesn't fit on the line, e.g. a wide image. */
8652 it->hpos == 0
8653 || (new_x == it->last_visible_x
8654 && FRAME_WINDOW_P (it->f)
8655 /* When word-wrap is ON and we have a valid
8656 wrap point, we don't allow the last glyph
8657 to "just barely fit" on the line. */
8658 && (it->line_wrap != WORD_WRAP
8659 || wrap_it.sp < 0)))
8661 ++it->hpos;
8662 it->current_x = new_x;
8664 /* The character's last glyph just barely fits
8665 in this row. */
8666 if (i == it->nglyphs - 1)
8668 /* If this is the destination position,
8669 return a position *before* it in this row,
8670 now that we know it fits in this row. */
8671 if (BUFFER_POS_REACHED_P ())
8673 if (it->line_wrap != WORD_WRAP
8674 || wrap_it.sp < 0)
8676 it->hpos = hpos_before_this_char;
8677 it->current_x = x_before_this_char;
8678 result = MOVE_POS_MATCH_OR_ZV;
8679 break;
8681 if (it->line_wrap == WORD_WRAP
8682 && atpos_it.sp < 0)
8684 SAVE_IT (atpos_it, *it, atpos_data);
8685 atpos_it.current_x = x_before_this_char;
8686 atpos_it.hpos = hpos_before_this_char;
8690 prev_method = it->method;
8691 if (it->method == GET_FROM_BUFFER)
8692 prev_pos = IT_CHARPOS (*it);
8693 set_iterator_to_next (it, 1);
8694 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8695 SET_TEXT_POS (this_line_min_pos,
8696 IT_CHARPOS (*it), IT_BYTEPOS (*it));
8697 /* On graphical terminals, newlines may
8698 "overflow" into the fringe if
8699 overflow-newline-into-fringe is non-nil.
8700 On text terminals, and on graphical
8701 terminals with no right margin, newlines
8702 may overflow into the last glyph on the
8703 display line.*/
8704 if (!FRAME_WINDOW_P (it->f)
8705 || ((it->bidi_p
8706 && it->bidi_it.paragraph_dir == R2L)
8707 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
8708 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0
8709 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
8711 if (!get_next_display_element (it))
8713 result = MOVE_POS_MATCH_OR_ZV;
8714 break;
8716 if (BUFFER_POS_REACHED_P ())
8718 if (ITERATOR_AT_END_OF_LINE_P (it))
8719 result = MOVE_POS_MATCH_OR_ZV;
8720 else
8721 result = MOVE_LINE_CONTINUED;
8722 break;
8724 if (ITERATOR_AT_END_OF_LINE_P (it)
8725 && (it->line_wrap != WORD_WRAP
8726 || wrap_it.sp < 0))
8728 result = MOVE_NEWLINE_OR_CR;
8729 break;
8734 else
8735 IT_RESET_X_ASCENT_DESCENT (it);
8737 if (wrap_it.sp >= 0)
8739 RESTORE_IT (it, &wrap_it, wrap_data);
8740 atpos_it.sp = -1;
8741 atx_it.sp = -1;
8744 TRACE_MOVE ((stderr, "move_it_in: continued at %d\n",
8745 IT_CHARPOS (*it)));
8746 result = MOVE_LINE_CONTINUED;
8747 break;
8750 if (BUFFER_POS_REACHED_P ())
8752 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8753 goto buffer_pos_reached;
8754 if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
8756 SAVE_IT (atpos_it, *it, atpos_data);
8757 IT_RESET_X_ASCENT_DESCENT (&atpos_it);
8761 if (new_x > it->first_visible_x)
8763 /* Glyph is visible. Increment number of glyphs that
8764 would be displayed. */
8765 ++it->hpos;
8769 if (result != MOVE_UNDEFINED)
8770 break;
8772 else if (BUFFER_POS_REACHED_P ())
8774 buffer_pos_reached:
8775 IT_RESET_X_ASCENT_DESCENT (it);
8776 result = MOVE_POS_MATCH_OR_ZV;
8777 break;
8779 else if ((op & MOVE_TO_X) && it->current_x >= to_x)
8781 /* Stop when TO_X specified and reached. This check is
8782 necessary here because of lines consisting of a line end,
8783 only. The line end will not produce any glyphs and we
8784 would never get MOVE_X_REACHED. */
8785 eassert (it->nglyphs == 0);
8786 result = MOVE_X_REACHED;
8787 break;
8790 /* Is this a line end? If yes, we're done. */
8791 if (ITERATOR_AT_END_OF_LINE_P (it))
8793 /* If we are past TO_CHARPOS, but never saw any character
8794 positions smaller than TO_CHARPOS, return
8795 MOVE_POS_MATCH_OR_ZV, like the unidirectional display
8796 did. */
8797 if (it->bidi_p && (op & MOVE_TO_POS) != 0)
8799 if (!saw_smaller_pos && IT_CHARPOS (*it) > to_charpos)
8801 if (closest_pos < ZV)
8803 RESTORE_IT (it, &ppos_it, ppos_data);
8804 move_it_in_display_line_to (it, closest_pos, -1,
8805 MOVE_TO_POS);
8806 result = MOVE_POS_MATCH_OR_ZV;
8808 else
8809 goto buffer_pos_reached;
8811 else if (it->line_wrap == WORD_WRAP && atpos_it.sp >= 0
8812 && IT_CHARPOS (*it) > to_charpos)
8813 goto buffer_pos_reached;
8814 else
8815 result = MOVE_NEWLINE_OR_CR;
8817 else
8818 result = MOVE_NEWLINE_OR_CR;
8819 break;
8822 prev_method = it->method;
8823 if (it->method == GET_FROM_BUFFER)
8824 prev_pos = IT_CHARPOS (*it);
8825 /* The current display element has been consumed. Advance
8826 to the next. */
8827 set_iterator_to_next (it, 1);
8828 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8829 SET_TEXT_POS (this_line_min_pos, IT_CHARPOS (*it), IT_BYTEPOS (*it));
8830 if (IT_CHARPOS (*it) < to_charpos)
8831 saw_smaller_pos = 1;
8832 if (it->bidi_p
8833 && (op & MOVE_TO_POS)
8834 && IT_CHARPOS (*it) >= to_charpos
8835 && IT_CHARPOS (*it) < closest_pos)
8836 closest_pos = IT_CHARPOS (*it);
8838 /* Stop if lines are truncated and IT's current x-position is
8839 past the right edge of the window now. */
8840 if (it->line_wrap == TRUNCATE
8841 && it->current_x >= it->last_visible_x)
8843 if (!FRAME_WINDOW_P (it->f)
8844 || ((it->bidi_p && it->bidi_it.paragraph_dir == R2L)
8845 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
8846 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0
8847 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
8849 int at_eob_p = 0;
8851 if ((at_eob_p = !get_next_display_element (it))
8852 || BUFFER_POS_REACHED_P ()
8853 /* If we are past TO_CHARPOS, but never saw any
8854 character positions smaller than TO_CHARPOS,
8855 return MOVE_POS_MATCH_OR_ZV, like the
8856 unidirectional display did. */
8857 || (it->bidi_p && (op & MOVE_TO_POS) != 0
8858 && !saw_smaller_pos
8859 && IT_CHARPOS (*it) > to_charpos))
8861 if (it->bidi_p
8862 && !BUFFER_POS_REACHED_P ()
8863 && !at_eob_p && closest_pos < ZV)
8865 RESTORE_IT (it, &ppos_it, ppos_data);
8866 move_it_in_display_line_to (it, closest_pos, -1,
8867 MOVE_TO_POS);
8869 result = MOVE_POS_MATCH_OR_ZV;
8870 break;
8872 if (ITERATOR_AT_END_OF_LINE_P (it))
8874 result = MOVE_NEWLINE_OR_CR;
8875 break;
8878 else if (it->bidi_p && (op & MOVE_TO_POS) != 0
8879 && !saw_smaller_pos
8880 && IT_CHARPOS (*it) > to_charpos)
8882 if (closest_pos < ZV)
8884 RESTORE_IT (it, &ppos_it, ppos_data);
8885 move_it_in_display_line_to (it, closest_pos, -1, MOVE_TO_POS);
8887 result = MOVE_POS_MATCH_OR_ZV;
8888 break;
8890 result = MOVE_LINE_TRUNCATED;
8891 break;
8893 #undef IT_RESET_X_ASCENT_DESCENT
8896 #undef BUFFER_POS_REACHED_P
8898 /* If we scanned beyond to_pos and didn't find a point to wrap at,
8899 restore the saved iterator. */
8900 if (atpos_it.sp >= 0)
8901 RESTORE_IT (it, &atpos_it, atpos_data);
8902 else if (atx_it.sp >= 0)
8903 RESTORE_IT (it, &atx_it, atx_data);
8905 done:
8907 if (atpos_data)
8908 bidi_unshelve_cache (atpos_data, 1);
8909 if (atx_data)
8910 bidi_unshelve_cache (atx_data, 1);
8911 if (wrap_data)
8912 bidi_unshelve_cache (wrap_data, 1);
8913 if (ppos_data)
8914 bidi_unshelve_cache (ppos_data, 1);
8916 /* Restore the iterator settings altered at the beginning of this
8917 function. */
8918 it->glyph_row = saved_glyph_row;
8919 return result;
8922 /* For external use. */
8923 void
8924 move_it_in_display_line (struct it *it,
8925 ptrdiff_t to_charpos, int to_x,
8926 enum move_operation_enum op)
8928 if (it->line_wrap == WORD_WRAP
8929 && (op & MOVE_TO_X))
8931 struct it save_it;
8932 void *save_data = NULL;
8933 int skip;
8935 SAVE_IT (save_it, *it, save_data);
8936 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
8937 /* When word-wrap is on, TO_X may lie past the end
8938 of a wrapped line. Then it->current is the
8939 character on the next line, so backtrack to the
8940 space before the wrap point. */
8941 if (skip == MOVE_LINE_CONTINUED)
8943 int prev_x = max (it->current_x - 1, 0);
8944 RESTORE_IT (it, &save_it, save_data);
8945 move_it_in_display_line_to
8946 (it, -1, prev_x, MOVE_TO_X);
8948 else
8949 bidi_unshelve_cache (save_data, 1);
8951 else
8952 move_it_in_display_line_to (it, to_charpos, to_x, op);
8956 /* Move IT forward until it satisfies one or more of the criteria in
8957 TO_CHARPOS, TO_X, TO_Y, and TO_VPOS.
8959 OP is a bit-mask that specifies where to stop, and in particular,
8960 which of those four position arguments makes a difference. See the
8961 description of enum move_operation_enum.
8963 If TO_CHARPOS is in invisible text, e.g. a truncated part of a
8964 screen line, this function will set IT to the next position that is
8965 displayed to the right of TO_CHARPOS on the screen.
8967 Return the maximum pixel length of any line scanned but never more
8968 than it.last_visible_x. */
8971 move_it_to (struct it *it, ptrdiff_t to_charpos, int to_x, int to_y, int to_vpos, int op)
8973 enum move_it_result skip, skip2 = MOVE_X_REACHED;
8974 int line_height, line_start_x = 0, reached = 0;
8975 int max_current_x = 0;
8976 void *backup_data = NULL;
8978 for (;;)
8980 if (op & MOVE_TO_VPOS)
8982 /* If no TO_CHARPOS and no TO_X specified, stop at the
8983 start of the line TO_VPOS. */
8984 if ((op & (MOVE_TO_X | MOVE_TO_POS)) == 0)
8986 if (it->vpos == to_vpos)
8988 reached = 1;
8989 break;
8991 else
8992 skip = move_it_in_display_line_to (it, -1, -1, 0);
8994 else
8996 /* TO_VPOS >= 0 means stop at TO_X in the line at
8997 TO_VPOS, or at TO_POS, whichever comes first. */
8998 if (it->vpos == to_vpos)
9000 reached = 2;
9001 break;
9004 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
9006 if (skip == MOVE_POS_MATCH_OR_ZV || it->vpos == to_vpos)
9008 reached = 3;
9009 break;
9011 else if (skip == MOVE_X_REACHED && it->vpos != to_vpos)
9013 /* We have reached TO_X but not in the line we want. */
9014 skip = move_it_in_display_line_to (it, to_charpos,
9015 -1, MOVE_TO_POS);
9016 if (skip == MOVE_POS_MATCH_OR_ZV)
9018 reached = 4;
9019 break;
9024 else if (op & MOVE_TO_Y)
9026 struct it it_backup;
9028 if (it->line_wrap == WORD_WRAP)
9029 SAVE_IT (it_backup, *it, backup_data);
9031 /* TO_Y specified means stop at TO_X in the line containing
9032 TO_Y---or at TO_CHARPOS if this is reached first. The
9033 problem is that we can't really tell whether the line
9034 contains TO_Y before we have completely scanned it, and
9035 this may skip past TO_X. What we do is to first scan to
9036 TO_X.
9038 If TO_X is not specified, use a TO_X of zero. The reason
9039 is to make the outcome of this function more predictable.
9040 If we didn't use TO_X == 0, we would stop at the end of
9041 the line which is probably not what a caller would expect
9042 to happen. */
9043 skip = move_it_in_display_line_to
9044 (it, to_charpos, ((op & MOVE_TO_X) ? to_x : 0),
9045 (MOVE_TO_X | (op & MOVE_TO_POS)));
9047 /* If TO_CHARPOS is reached or ZV, we don't have to do more. */
9048 if (skip == MOVE_POS_MATCH_OR_ZV)
9049 reached = 5;
9050 else if (skip == MOVE_X_REACHED)
9052 /* If TO_X was reached, we want to know whether TO_Y is
9053 in the line. We know this is the case if the already
9054 scanned glyphs make the line tall enough. Otherwise,
9055 we must check by scanning the rest of the line. */
9056 line_height = it->max_ascent + it->max_descent;
9057 if (to_y >= it->current_y
9058 && to_y < it->current_y + line_height)
9060 reached = 6;
9061 break;
9063 SAVE_IT (it_backup, *it, backup_data);
9064 TRACE_MOVE ((stderr, "move_it: from %d\n", IT_CHARPOS (*it)));
9065 skip2 = move_it_in_display_line_to (it, to_charpos, -1,
9066 op & MOVE_TO_POS);
9067 TRACE_MOVE ((stderr, "move_it: to %d\n", IT_CHARPOS (*it)));
9068 line_height = it->max_ascent + it->max_descent;
9069 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
9071 if (to_y >= it->current_y
9072 && to_y < it->current_y + line_height)
9074 /* If TO_Y is in this line and TO_X was reached
9075 above, we scanned too far. We have to restore
9076 IT's settings to the ones before skipping. But
9077 keep the more accurate values of max_ascent and
9078 max_descent we've found while skipping the rest
9079 of the line, for the sake of callers, such as
9080 pos_visible_p, that need to know the line
9081 height. */
9082 int max_ascent = it->max_ascent;
9083 int max_descent = it->max_descent;
9085 RESTORE_IT (it, &it_backup, backup_data);
9086 it->max_ascent = max_ascent;
9087 it->max_descent = max_descent;
9088 reached = 6;
9090 else
9092 skip = skip2;
9093 if (skip == MOVE_POS_MATCH_OR_ZV)
9094 reached = 7;
9097 else
9099 /* Check whether TO_Y is in this line. */
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 > it->current_y)
9107 max_current_x = max (it->current_x, max_current_x);
9109 /* When word-wrap is on, TO_X may lie past the end
9110 of a wrapped line. Then it->current is the
9111 character on the next line, so backtrack to the
9112 space before the wrap point. */
9113 if (skip == MOVE_LINE_CONTINUED
9114 && it->line_wrap == WORD_WRAP)
9116 int prev_x = max (it->current_x - 1, 0);
9117 RESTORE_IT (it, &it_backup, backup_data);
9118 skip = move_it_in_display_line_to
9119 (it, -1, prev_x, MOVE_TO_X);
9122 reached = 6;
9126 if (reached)
9128 max_current_x = max (it->current_x, max_current_x);
9129 break;
9132 else if (BUFFERP (it->object)
9133 && (it->method == GET_FROM_BUFFER
9134 || it->method == GET_FROM_STRETCH)
9135 && IT_CHARPOS (*it) >= to_charpos
9136 /* Under bidi iteration, a call to set_iterator_to_next
9137 can scan far beyond to_charpos if the initial
9138 portion of the next line needs to be reordered. In
9139 that case, give move_it_in_display_line_to another
9140 chance below. */
9141 && !(it->bidi_p
9142 && it->bidi_it.scan_dir == -1))
9143 skip = MOVE_POS_MATCH_OR_ZV;
9144 else
9145 skip = move_it_in_display_line_to (it, to_charpos, -1, MOVE_TO_POS);
9147 switch (skip)
9149 case MOVE_POS_MATCH_OR_ZV:
9150 max_current_x = max (it->current_x, max_current_x);
9151 reached = 8;
9152 goto out;
9154 case MOVE_NEWLINE_OR_CR:
9155 max_current_x = max (it->current_x, max_current_x);
9156 set_iterator_to_next (it, 1);
9157 it->continuation_lines_width = 0;
9158 break;
9160 case MOVE_LINE_TRUNCATED:
9161 max_current_x = it->last_visible_x;
9162 it->continuation_lines_width = 0;
9163 reseat_at_next_visible_line_start (it, 0);
9164 if ((op & MOVE_TO_POS) != 0
9165 && IT_CHARPOS (*it) > to_charpos)
9167 reached = 9;
9168 goto out;
9170 break;
9172 case MOVE_LINE_CONTINUED:
9173 max_current_x = it->last_visible_x;
9174 /* For continued lines ending in a tab, some of the glyphs
9175 associated with the tab are displayed on the current
9176 line. Since it->current_x does not include these glyphs,
9177 we use it->last_visible_x instead. */
9178 if (it->c == '\t')
9180 it->continuation_lines_width += it->last_visible_x;
9181 /* When moving by vpos, ensure that the iterator really
9182 advances to the next line (bug#847, bug#969). Fixme:
9183 do we need to do this in other circumstances? */
9184 if (it->current_x != it->last_visible_x
9185 && (op & MOVE_TO_VPOS)
9186 && !(op & (MOVE_TO_X | MOVE_TO_POS)))
9188 line_start_x = it->current_x + it->pixel_width
9189 - it->last_visible_x;
9190 set_iterator_to_next (it, 0);
9193 else
9194 it->continuation_lines_width += it->current_x;
9195 break;
9197 default:
9198 emacs_abort ();
9201 /* Reset/increment for the next run. */
9202 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
9203 it->current_x = line_start_x;
9204 line_start_x = 0;
9205 it->hpos = 0;
9206 it->current_y += it->max_ascent + it->max_descent;
9207 ++it->vpos;
9208 last_height = it->max_ascent + it->max_descent;
9209 it->max_ascent = it->max_descent = 0;
9212 out:
9214 /* On text terminals, we may stop at the end of a line in the middle
9215 of a multi-character glyph. If the glyph itself is continued,
9216 i.e. it is actually displayed on the next line, don't treat this
9217 stopping point as valid; move to the next line instead (unless
9218 that brings us offscreen). */
9219 if (!FRAME_WINDOW_P (it->f)
9220 && op & MOVE_TO_POS
9221 && IT_CHARPOS (*it) == to_charpos
9222 && it->what == IT_CHARACTER
9223 && it->nglyphs > 1
9224 && it->line_wrap == WINDOW_WRAP
9225 && it->current_x == it->last_visible_x - 1
9226 && it->c != '\n'
9227 && it->c != '\t'
9228 && it->vpos < it->w->window_end_vpos)
9230 it->continuation_lines_width += it->current_x;
9231 it->current_x = it->hpos = it->max_ascent = it->max_descent = 0;
9232 it->current_y += it->max_ascent + it->max_descent;
9233 ++it->vpos;
9234 last_height = it->max_ascent + it->max_descent;
9237 if (backup_data)
9238 bidi_unshelve_cache (backup_data, 1);
9240 TRACE_MOVE ((stderr, "move_it_to: reached %d\n", reached));
9242 return max_current_x;
9246 /* Move iterator IT backward by a specified y-distance DY, DY >= 0.
9248 If DY > 0, move IT backward at least that many pixels. DY = 0
9249 means move IT backward to the preceding line start or BEGV. This
9250 function may move over more than DY pixels if IT->current_y - DY
9251 ends up in the middle of a line; in this case IT->current_y will be
9252 set to the top of the line moved to. */
9254 void
9255 move_it_vertically_backward (struct it *it, int dy)
9257 int nlines, h;
9258 struct it it2, it3;
9259 void *it2data = NULL, *it3data = NULL;
9260 ptrdiff_t start_pos;
9261 int nchars_per_row
9262 = (it->last_visible_x - it->first_visible_x) / FRAME_COLUMN_WIDTH (it->f);
9263 ptrdiff_t pos_limit;
9265 move_further_back:
9266 eassert (dy >= 0);
9268 start_pos = IT_CHARPOS (*it);
9270 /* Estimate how many newlines we must move back. */
9271 nlines = max (1, dy / default_line_pixel_height (it->w));
9272 if (it->line_wrap == TRUNCATE)
9273 pos_limit = BEGV;
9274 else
9275 pos_limit = max (start_pos - nlines * nchars_per_row, BEGV);
9277 /* Set the iterator's position that many lines back. But don't go
9278 back more than NLINES full screen lines -- this wins a day with
9279 buffers which have very long lines. */
9280 while (nlines-- && IT_CHARPOS (*it) > pos_limit)
9281 back_to_previous_visible_line_start (it);
9283 /* Reseat the iterator here. When moving backward, we don't want
9284 reseat to skip forward over invisible text, set up the iterator
9285 to deliver from overlay strings at the new position etc. So,
9286 use reseat_1 here. */
9287 reseat_1 (it, it->current.pos, 1);
9289 /* We are now surely at a line start. */
9290 it->current_x = it->hpos = 0; /* FIXME: this is incorrect when bidi
9291 reordering is in effect. */
9292 it->continuation_lines_width = 0;
9294 /* Move forward and see what y-distance we moved. First move to the
9295 start of the next line so that we get its height. We need this
9296 height to be able to tell whether we reached the specified
9297 y-distance. */
9298 SAVE_IT (it2, *it, it2data);
9299 it2.max_ascent = it2.max_descent = 0;
9302 move_it_to (&it2, start_pos, -1, -1, it2.vpos + 1,
9303 MOVE_TO_POS | MOVE_TO_VPOS);
9305 while (!(IT_POS_VALID_AFTER_MOVE_P (&it2)
9306 /* If we are in a display string which starts at START_POS,
9307 and that display string includes a newline, and we are
9308 right after that newline (i.e. at the beginning of a
9309 display line), exit the loop, because otherwise we will
9310 infloop, since move_it_to will see that it is already at
9311 START_POS and will not move. */
9312 || (it2.method == GET_FROM_STRING
9313 && IT_CHARPOS (it2) == start_pos
9314 && SREF (it2.string, IT_STRING_BYTEPOS (it2) - 1) == '\n')));
9315 eassert (IT_CHARPOS (*it) >= BEGV);
9316 SAVE_IT (it3, it2, it3data);
9318 move_it_to (&it2, start_pos, -1, -1, -1, MOVE_TO_POS);
9319 eassert (IT_CHARPOS (*it) >= BEGV);
9320 /* H is the actual vertical distance from the position in *IT
9321 and the starting position. */
9322 h = it2.current_y - it->current_y;
9323 /* NLINES is the distance in number of lines. */
9324 nlines = it2.vpos - it->vpos;
9326 /* Correct IT's y and vpos position
9327 so that they are relative to the starting point. */
9328 it->vpos -= nlines;
9329 it->current_y -= h;
9331 if (dy == 0)
9333 /* DY == 0 means move to the start of the screen line. The
9334 value of nlines is > 0 if continuation lines were involved,
9335 or if the original IT position was at start of a line. */
9336 RESTORE_IT (it, it, it2data);
9337 if (nlines > 0)
9338 move_it_by_lines (it, nlines);
9339 /* The above code moves us to some position NLINES down,
9340 usually to its first glyph (leftmost in an L2R line), but
9341 that's not necessarily the start of the line, under bidi
9342 reordering. We want to get to the character position
9343 that is immediately after the newline of the previous
9344 line. */
9345 if (it->bidi_p
9346 && !it->continuation_lines_width
9347 && !STRINGP (it->string)
9348 && IT_CHARPOS (*it) > BEGV
9349 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
9351 ptrdiff_t cp = IT_CHARPOS (*it), bp = IT_BYTEPOS (*it);
9353 DEC_BOTH (cp, bp);
9354 cp = find_newline_no_quit (cp, bp, -1, NULL);
9355 move_it_to (it, cp, -1, -1, -1, MOVE_TO_POS);
9357 bidi_unshelve_cache (it3data, 1);
9359 else
9361 /* The y-position we try to reach, relative to *IT.
9362 Note that H has been subtracted in front of the if-statement. */
9363 int target_y = it->current_y + h - dy;
9364 int y0 = it3.current_y;
9365 int y1;
9366 int line_height;
9368 RESTORE_IT (&it3, &it3, it3data);
9369 y1 = line_bottom_y (&it3);
9370 line_height = y1 - y0;
9371 RESTORE_IT (it, it, it2data);
9372 /* If we did not reach target_y, try to move further backward if
9373 we can. If we moved too far backward, try to move forward. */
9374 if (target_y < it->current_y
9375 /* This is heuristic. In a window that's 3 lines high, with
9376 a line height of 13 pixels each, recentering with point
9377 on the bottom line will try to move -39/2 = 19 pixels
9378 backward. Try to avoid moving into the first line. */
9379 && (it->current_y - target_y
9380 > min (window_box_height (it->w), line_height * 2 / 3))
9381 && IT_CHARPOS (*it) > BEGV)
9383 TRACE_MOVE ((stderr, " not far enough -> move_vert %d\n",
9384 target_y - it->current_y));
9385 dy = it->current_y - target_y;
9386 goto move_further_back;
9388 else if (target_y >= it->current_y + line_height
9389 && IT_CHARPOS (*it) < ZV)
9391 /* Should move forward by at least one line, maybe more.
9393 Note: Calling move_it_by_lines can be expensive on
9394 terminal frames, where compute_motion is used (via
9395 vmotion) to do the job, when there are very long lines
9396 and truncate-lines is nil. That's the reason for
9397 treating terminal frames specially here. */
9399 if (!FRAME_WINDOW_P (it->f))
9400 move_it_vertically (it, target_y - (it->current_y + line_height));
9401 else
9405 move_it_by_lines (it, 1);
9407 while (target_y >= line_bottom_y (it) && IT_CHARPOS (*it) < ZV);
9414 /* Move IT by a specified amount of pixel lines DY. DY negative means
9415 move backwards. DY = 0 means move to start of screen line. At the
9416 end, IT will be on the start of a screen line. */
9418 void
9419 move_it_vertically (struct it *it, int dy)
9421 if (dy <= 0)
9422 move_it_vertically_backward (it, -dy);
9423 else
9425 TRACE_MOVE ((stderr, "move_it_v: from %d, %d\n", IT_CHARPOS (*it), dy));
9426 move_it_to (it, ZV, -1, it->current_y + dy, -1,
9427 MOVE_TO_POS | MOVE_TO_Y);
9428 TRACE_MOVE ((stderr, "move_it_v: to %d\n", IT_CHARPOS (*it)));
9430 /* If buffer ends in ZV without a newline, move to the start of
9431 the line to satisfy the post-condition. */
9432 if (IT_CHARPOS (*it) == ZV
9433 && ZV > BEGV
9434 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
9435 move_it_by_lines (it, 0);
9440 /* Move iterator IT past the end of the text line it is in. */
9442 void
9443 move_it_past_eol (struct it *it)
9445 enum move_it_result rc;
9447 rc = move_it_in_display_line_to (it, Z, 0, MOVE_TO_POS);
9448 if (rc == MOVE_NEWLINE_OR_CR)
9449 set_iterator_to_next (it, 0);
9453 /* Move IT by a specified number DVPOS of screen lines down. DVPOS
9454 negative means move up. DVPOS == 0 means move to the start of the
9455 screen line.
9457 Optimization idea: If we would know that IT->f doesn't use
9458 a face with proportional font, we could be faster for
9459 truncate-lines nil. */
9461 void
9462 move_it_by_lines (struct it *it, ptrdiff_t dvpos)
9465 /* The commented-out optimization uses vmotion on terminals. This
9466 gives bad results, because elements like it->what, on which
9467 callers such as pos_visible_p rely, aren't updated. */
9468 /* struct position pos;
9469 if (!FRAME_WINDOW_P (it->f))
9471 struct text_pos textpos;
9473 pos = *vmotion (IT_CHARPOS (*it), dvpos, it->w);
9474 SET_TEXT_POS (textpos, pos.bufpos, pos.bytepos);
9475 reseat (it, textpos, 1);
9476 it->vpos += pos.vpos;
9477 it->current_y += pos.vpos;
9479 else */
9481 if (dvpos == 0)
9483 /* DVPOS == 0 means move to the start of the screen line. */
9484 move_it_vertically_backward (it, 0);
9485 /* Let next call to line_bottom_y calculate real line height. */
9486 last_height = 0;
9488 else if (dvpos > 0)
9490 move_it_to (it, -1, -1, -1, it->vpos + dvpos, MOVE_TO_VPOS);
9491 if (!IT_POS_VALID_AFTER_MOVE_P (it))
9493 /* Only move to the next buffer position if we ended up in a
9494 string from display property, not in an overlay string
9495 (before-string or after-string). That is because the
9496 latter don't conceal the underlying buffer position, so
9497 we can ask to move the iterator to the exact position we
9498 are interested in. Note that, even if we are already at
9499 IT_CHARPOS (*it), the call below is not a no-op, as it
9500 will detect that we are at the end of the string, pop the
9501 iterator, and compute it->current_x and it->hpos
9502 correctly. */
9503 move_it_to (it, IT_CHARPOS (*it) + it->string_from_display_prop_p,
9504 -1, -1, -1, MOVE_TO_POS);
9507 else
9509 struct it it2;
9510 void *it2data = NULL;
9511 ptrdiff_t start_charpos, i;
9512 int nchars_per_row
9513 = (it->last_visible_x - it->first_visible_x) / FRAME_COLUMN_WIDTH (it->f);
9514 bool hit_pos_limit = false;
9515 ptrdiff_t pos_limit;
9517 /* Start at the beginning of the screen line containing IT's
9518 position. This may actually move vertically backwards,
9519 in case of overlays, so adjust dvpos accordingly. */
9520 dvpos += it->vpos;
9521 move_it_vertically_backward (it, 0);
9522 dvpos -= it->vpos;
9524 /* Go back -DVPOS buffer lines, but no farther than -DVPOS full
9525 screen lines, and reseat the iterator there. */
9526 start_charpos = IT_CHARPOS (*it);
9527 if (it->line_wrap == TRUNCATE)
9528 pos_limit = BEGV;
9529 else
9530 pos_limit = max (start_charpos + dvpos * nchars_per_row, BEGV);
9532 for (i = -dvpos; i > 0 && IT_CHARPOS (*it) > pos_limit; --i)
9533 back_to_previous_visible_line_start (it);
9534 if (i > 0 && IT_CHARPOS (*it) <= pos_limit)
9535 hit_pos_limit = true;
9536 reseat (it, it->current.pos, 1);
9538 /* Move further back if we end up in a string or an image. */
9539 while (!IT_POS_VALID_AFTER_MOVE_P (it))
9541 /* First try to move to start of display line. */
9542 dvpos += it->vpos;
9543 move_it_vertically_backward (it, 0);
9544 dvpos -= it->vpos;
9545 if (IT_POS_VALID_AFTER_MOVE_P (it))
9546 break;
9547 /* If start of line is still in string or image,
9548 move further back. */
9549 back_to_previous_visible_line_start (it);
9550 reseat (it, it->current.pos, 1);
9551 dvpos--;
9554 it->current_x = it->hpos = 0;
9556 /* Above call may have moved too far if continuation lines
9557 are involved. Scan forward and see if it did. */
9558 SAVE_IT (it2, *it, it2data);
9559 it2.vpos = it2.current_y = 0;
9560 move_it_to (&it2, start_charpos, -1, -1, -1, MOVE_TO_POS);
9561 it->vpos -= it2.vpos;
9562 it->current_y -= it2.current_y;
9563 it->current_x = it->hpos = 0;
9565 /* If we moved too far back, move IT some lines forward. */
9566 if (it2.vpos > -dvpos)
9568 int delta = it2.vpos + dvpos;
9570 RESTORE_IT (&it2, &it2, it2data);
9571 SAVE_IT (it2, *it, it2data);
9572 move_it_to (it, -1, -1, -1, it->vpos + delta, MOVE_TO_VPOS);
9573 /* Move back again if we got too far ahead. */
9574 if (IT_CHARPOS (*it) >= start_charpos)
9575 RESTORE_IT (it, &it2, it2data);
9576 else
9577 bidi_unshelve_cache (it2data, 1);
9579 else if (hit_pos_limit && pos_limit > BEGV
9580 && dvpos < 0 && it2.vpos < -dvpos)
9582 /* If we hit the limit, but still didn't make it far enough
9583 back, that means there's a display string with a newline
9584 covering a large chunk of text, and that caused
9585 back_to_previous_visible_line_start try to go too far.
9586 Punish those who commit such atrocities by going back
9587 until we've reached DVPOS, after lifting the limit, which
9588 could make it slow for very long lines. "If it hurts,
9589 don't do that!" */
9590 dvpos += it2.vpos;
9591 RESTORE_IT (it, it, it2data);
9592 for (i = -dvpos; i > 0; --i)
9594 back_to_previous_visible_line_start (it);
9595 it->vpos--;
9598 else
9599 RESTORE_IT (it, it, it2data);
9603 /* Return true if IT points into the middle of a display vector. */
9605 bool
9606 in_display_vector_p (struct it *it)
9608 return (it->method == GET_FROM_DISPLAY_VECTOR
9609 && it->current.dpvec_index > 0
9610 && it->dpvec + it->current.dpvec_index != it->dpend);
9613 DEFUN ("window-text-pixel-size", Fwindow_text_pixel_size, Swindow_text_pixel_size, 0, 6, 0,
9614 doc: /* Return the size of the text of WINDOW's buffer in pixels.
9615 WINDOW must be a live window and defaults to the selected one. The
9616 return value is a cons of the maximum pixel-width of any text line and
9617 the maximum pixel-height of all text lines.
9619 The optional argument FROM, if non-nil, specifies the first text
9620 position and defaults to the minimum accessible position of the buffer.
9621 If FROM is t, use the minimum accessible position that is not a newline
9622 character. TO, if non-nil, specifies the last text position and
9623 defaults to the maximum accessible position of the buffer. If TO is t,
9624 use the maximum accessible position that is not a newline character.
9626 The optional argument X-LIMIT, if non-nil, specifies the maximum text
9627 width that can be returned. X-LIMIT nil or omitted, means to use the
9628 pixel-width of WINDOW's body; use this if you do not intend to change
9629 the width of WINDOW. Use the maximum width WINDOW may assume if you
9630 intend to change WINDOW's width. In any case, text whose x-coordinate
9631 is beyond X-LIMIT is ignored. Since calculating the width of long lines
9632 can take some time, it's always a good idea to make this argument as
9633 small as possible; in particular, if the buffer contains long lines that
9634 shall be truncated anyway.
9636 The optional argument Y-LIMIT, if non-nil, specifies the maximum text
9637 height that can be returned. Text lines whose y-coordinate is beyond
9638 Y-LIMIT are ignored. Since calculating the text height of a large
9639 buffer can take some time, it makes sense to specify this argument if
9640 the size of the buffer is unknown.
9642 Optional argument MODE-AND-HEADER-LINE nil or omitted means do not
9643 include the height of the mode- or header-line of WINDOW in the return
9644 value. If it is either the symbol `mode-line' or `header-line', include
9645 only the height of that line, if present, in the return value. If t,
9646 include the height of both, if present, in the return value. */)
9647 (Lisp_Object window, Lisp_Object from, Lisp_Object to, Lisp_Object x_limit, Lisp_Object y_limit,
9648 Lisp_Object mode_and_header_line)
9650 struct window *w = decode_live_window (window);
9651 Lisp_Object buf;
9652 struct buffer *b;
9653 struct it it;
9654 struct buffer *old_buffer = NULL;
9655 ptrdiff_t start, end, pos;
9656 struct text_pos startp;
9657 void *itdata = NULL;
9658 int c, max_y = -1, x = 0, y = 0;
9660 buf = w->contents;
9661 CHECK_BUFFER (buf);
9662 b = XBUFFER (buf);
9664 if (b != current_buffer)
9666 old_buffer = current_buffer;
9667 set_buffer_internal (b);
9670 if (NILP (from))
9671 start = BEGV;
9672 else if (EQ (from, Qt))
9674 start = pos = BEGV;
9675 while ((pos++ < ZV) && (c = FETCH_CHAR (pos))
9676 && (c == ' ' || c == '\t' || c == '\n' || c == '\r'))
9677 start = pos;
9678 while ((pos-- > BEGV) && (c = FETCH_CHAR (pos)) && (c == ' ' || c == '\t'))
9679 start = pos;
9681 else
9683 CHECK_NUMBER_COERCE_MARKER (from);
9684 start = min (max (XINT (from), BEGV), ZV);
9687 if (NILP (to))
9688 end = ZV;
9689 else if (EQ (to, Qt))
9691 end = pos = ZV;
9692 while ((pos-- > BEGV) && (c = FETCH_CHAR (pos))
9693 && (c == ' ' || c == '\t' || c == '\n' || c == '\r'))
9694 end = pos;
9695 while ((pos++ < ZV) && (c = FETCH_CHAR (pos)) && (c == ' ' || c == '\t'))
9696 end = pos;
9698 else
9700 CHECK_NUMBER_COERCE_MARKER (to);
9701 end = max (start, min (XINT (to), ZV));
9704 if (!NILP (y_limit))
9706 CHECK_NUMBER (y_limit);
9707 max_y = min (XINT (y_limit), INT_MAX);
9710 itdata = bidi_shelve_cache ();
9711 SET_TEXT_POS (startp, start, CHAR_TO_BYTE (start));
9712 start_display (&it, w, startp);
9714 if (NILP (x_limit))
9715 x = move_it_to (&it, end, -1, max_y, -1, MOVE_TO_POS | MOVE_TO_Y);
9716 else
9718 CHECK_NUMBER (x_limit);
9719 it.last_visible_x = min (XINT (x_limit), INFINITY);
9720 /* Actually, we never want move_it_to stop at to_x. But to make
9721 sure that move_it_in_display_line_to always moves far enough,
9722 we set it to INT_MAX and specify MOVE_TO_X. */
9723 x = move_it_to (&it, end, INT_MAX, max_y, -1,
9724 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
9727 y = it.current_y + it.max_ascent + it.max_descent;
9729 if (!EQ (mode_and_header_line, Qheader_line)
9730 && !EQ (mode_and_header_line, Qt))
9731 /* Do not count the header-line which was counted automatically by
9732 start_display. */
9733 y = y - WINDOW_HEADER_LINE_HEIGHT (w);
9735 if (EQ (mode_and_header_line, Qmode_line)
9736 || EQ (mode_and_header_line, Qt))
9737 /* Do count the mode-line which is not included automatically by
9738 start_display. */
9739 y = y + WINDOW_MODE_LINE_HEIGHT (w);
9741 bidi_unshelve_cache (itdata, 0);
9743 if (old_buffer)
9744 set_buffer_internal (old_buffer);
9746 return Fcons (make_number (x), make_number (y));
9749 /***********************************************************************
9750 Messages
9751 ***********************************************************************/
9754 /* Add a message with format string FORMAT and arguments ARG1 and ARG2
9755 to *Messages*. */
9757 void
9758 add_to_log (const char *format, Lisp_Object arg1, Lisp_Object arg2)
9760 Lisp_Object args[3];
9761 Lisp_Object msg, fmt;
9762 char *buffer;
9763 ptrdiff_t len;
9764 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
9765 USE_SAFE_ALLOCA;
9767 fmt = msg = Qnil;
9768 GCPRO4 (fmt, msg, arg1, arg2);
9770 args[0] = fmt = build_string (format);
9771 args[1] = arg1;
9772 args[2] = arg2;
9773 msg = Fformat (3, args);
9775 len = SBYTES (msg) + 1;
9776 buffer = SAFE_ALLOCA (len);
9777 memcpy (buffer, SDATA (msg), len);
9779 message_dolog (buffer, len - 1, 1, 0);
9780 SAFE_FREE ();
9782 UNGCPRO;
9786 /* Output a newline in the *Messages* buffer if "needs" one. */
9788 void
9789 message_log_maybe_newline (void)
9791 if (message_log_need_newline)
9792 message_dolog ("", 0, 1, 0);
9796 /* Add a string M of length NBYTES to the message log, optionally
9797 terminated with a newline when NLFLAG is true. MULTIBYTE, if
9798 true, means interpret the contents of M as multibyte. This
9799 function calls low-level routines in order to bypass text property
9800 hooks, etc. which might not be safe to run.
9802 This may GC (insert may run before/after change hooks),
9803 so the buffer M must NOT point to a Lisp string. */
9805 void
9806 message_dolog (const char *m, ptrdiff_t nbytes, bool nlflag, bool multibyte)
9808 const unsigned char *msg = (const unsigned char *) m;
9810 if (!NILP (Vmemory_full))
9811 return;
9813 if (!NILP (Vmessage_log_max))
9815 struct buffer *oldbuf;
9816 Lisp_Object oldpoint, oldbegv, oldzv;
9817 int old_windows_or_buffers_changed = windows_or_buffers_changed;
9818 ptrdiff_t point_at_end = 0;
9819 ptrdiff_t zv_at_end = 0;
9820 Lisp_Object old_deactivate_mark;
9821 struct gcpro gcpro1;
9823 old_deactivate_mark = Vdeactivate_mark;
9824 oldbuf = current_buffer;
9826 /* Ensure the Messages buffer exists, and switch to it.
9827 If we created it, set the major-mode. */
9829 int newbuffer = 0;
9830 if (NILP (Fget_buffer (Vmessages_buffer_name))) newbuffer = 1;
9832 Fset_buffer (Fget_buffer_create (Vmessages_buffer_name));
9834 if (newbuffer
9835 && !NILP (Ffboundp (intern ("messages-buffer-mode"))))
9836 call0 (intern ("messages-buffer-mode"));
9839 bset_undo_list (current_buffer, Qt);
9840 bset_cache_long_scans (current_buffer, Qnil);
9842 oldpoint = message_dolog_marker1;
9843 set_marker_restricted_both (oldpoint, Qnil, PT, PT_BYTE);
9844 oldbegv = message_dolog_marker2;
9845 set_marker_restricted_both (oldbegv, Qnil, BEGV, BEGV_BYTE);
9846 oldzv = message_dolog_marker3;
9847 set_marker_restricted_both (oldzv, Qnil, ZV, ZV_BYTE);
9848 GCPRO1 (old_deactivate_mark);
9850 if (PT == Z)
9851 point_at_end = 1;
9852 if (ZV == Z)
9853 zv_at_end = 1;
9855 BEGV = BEG;
9856 BEGV_BYTE = BEG_BYTE;
9857 ZV = Z;
9858 ZV_BYTE = Z_BYTE;
9859 TEMP_SET_PT_BOTH (Z, Z_BYTE);
9861 /* Insert the string--maybe converting multibyte to single byte
9862 or vice versa, so that all the text fits the buffer. */
9863 if (multibyte
9864 && NILP (BVAR (current_buffer, enable_multibyte_characters)))
9866 ptrdiff_t i;
9867 int c, char_bytes;
9868 char work[1];
9870 /* Convert a multibyte string to single-byte
9871 for the *Message* buffer. */
9872 for (i = 0; i < nbytes; i += char_bytes)
9874 c = string_char_and_length (msg + i, &char_bytes);
9875 work[0] = (ASCII_CHAR_P (c)
9877 : multibyte_char_to_unibyte (c));
9878 insert_1_both (work, 1, 1, 1, 0, 0);
9881 else if (! multibyte
9882 && ! NILP (BVAR (current_buffer, enable_multibyte_characters)))
9884 ptrdiff_t i;
9885 int c, char_bytes;
9886 unsigned char str[MAX_MULTIBYTE_LENGTH];
9887 /* Convert a single-byte string to multibyte
9888 for the *Message* buffer. */
9889 for (i = 0; i < nbytes; i++)
9891 c = msg[i];
9892 MAKE_CHAR_MULTIBYTE (c);
9893 char_bytes = CHAR_STRING (c, str);
9894 insert_1_both ((char *) str, 1, char_bytes, 1, 0, 0);
9897 else if (nbytes)
9898 insert_1_both (m, chars_in_text (msg, nbytes), nbytes, 1, 0, 0);
9900 if (nlflag)
9902 ptrdiff_t this_bol, this_bol_byte, prev_bol, prev_bol_byte;
9903 printmax_t dups;
9905 insert_1_both ("\n", 1, 1, 1, 0, 0);
9907 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE, -2, 0);
9908 this_bol = PT;
9909 this_bol_byte = PT_BYTE;
9911 /* See if this line duplicates the previous one.
9912 If so, combine duplicates. */
9913 if (this_bol > BEG)
9915 scan_newline (PT, PT_BYTE, BEG, BEG_BYTE, -2, 0);
9916 prev_bol = PT;
9917 prev_bol_byte = PT_BYTE;
9919 dups = message_log_check_duplicate (prev_bol_byte,
9920 this_bol_byte);
9921 if (dups)
9923 del_range_both (prev_bol, prev_bol_byte,
9924 this_bol, this_bol_byte, 0);
9925 if (dups > 1)
9927 char dupstr[sizeof " [ times]"
9928 + INT_STRLEN_BOUND (printmax_t)];
9930 /* If you change this format, don't forget to also
9931 change message_log_check_duplicate. */
9932 int duplen = sprintf (dupstr, " [%"pMd" times]", dups);
9933 TEMP_SET_PT_BOTH (Z - 1, Z_BYTE - 1);
9934 insert_1_both (dupstr, duplen, duplen, 1, 0, 1);
9939 /* If we have more than the desired maximum number of lines
9940 in the *Messages* buffer now, delete the oldest ones.
9941 This is safe because we don't have undo in this buffer. */
9943 if (NATNUMP (Vmessage_log_max))
9945 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE,
9946 -XFASTINT (Vmessage_log_max) - 1, 0);
9947 del_range_both (BEG, BEG_BYTE, PT, PT_BYTE, 0);
9950 BEGV = marker_position (oldbegv);
9951 BEGV_BYTE = marker_byte_position (oldbegv);
9953 if (zv_at_end)
9955 ZV = Z;
9956 ZV_BYTE = Z_BYTE;
9958 else
9960 ZV = marker_position (oldzv);
9961 ZV_BYTE = marker_byte_position (oldzv);
9964 if (point_at_end)
9965 TEMP_SET_PT_BOTH (Z, Z_BYTE);
9966 else
9967 /* We can't do Fgoto_char (oldpoint) because it will run some
9968 Lisp code. */
9969 TEMP_SET_PT_BOTH (marker_position (oldpoint),
9970 marker_byte_position (oldpoint));
9972 UNGCPRO;
9973 unchain_marker (XMARKER (oldpoint));
9974 unchain_marker (XMARKER (oldbegv));
9975 unchain_marker (XMARKER (oldzv));
9977 /* We called insert_1_both above with its 5th argument (PREPARE)
9978 zero, which prevents insert_1_both from calling
9979 prepare_to_modify_buffer, which in turns prevents us from
9980 incrementing windows_or_buffers_changed even if *Messages* is
9981 shown in some window. So we must manually set
9982 windows_or_buffers_changed here to make up for that. */
9983 windows_or_buffers_changed = old_windows_or_buffers_changed;
9984 bset_redisplay (current_buffer);
9986 set_buffer_internal (oldbuf);
9988 message_log_need_newline = !nlflag;
9989 Vdeactivate_mark = old_deactivate_mark;
9994 /* We are at the end of the buffer after just having inserted a newline.
9995 (Note: We depend on the fact we won't be crossing the gap.)
9996 Check to see if the most recent message looks a lot like the previous one.
9997 Return 0 if different, 1 if the new one should just replace it, or a
9998 value N > 1 if we should also append " [N times]". */
10000 static intmax_t
10001 message_log_check_duplicate (ptrdiff_t prev_bol_byte, ptrdiff_t this_bol_byte)
10003 ptrdiff_t i;
10004 ptrdiff_t len = Z_BYTE - 1 - this_bol_byte;
10005 int seen_dots = 0;
10006 unsigned char *p1 = BUF_BYTE_ADDRESS (current_buffer, prev_bol_byte);
10007 unsigned char *p2 = BUF_BYTE_ADDRESS (current_buffer, this_bol_byte);
10009 for (i = 0; i < len; i++)
10011 if (i >= 3 && p1[i - 3] == '.' && p1[i - 2] == '.' && p1[i - 1] == '.')
10012 seen_dots = 1;
10013 if (p1[i] != p2[i])
10014 return seen_dots;
10016 p1 += len;
10017 if (*p1 == '\n')
10018 return 2;
10019 if (*p1++ == ' ' && *p1++ == '[')
10021 char *pend;
10022 intmax_t n = strtoimax ((char *) p1, &pend, 10);
10023 if (0 < n && n < INTMAX_MAX && strncmp (pend, " times]\n", 8) == 0)
10024 return n + 1;
10026 return 0;
10030 /* Display an echo area message M with a specified length of NBYTES
10031 bytes. The string may include null characters. If M is not a
10032 string, clear out any existing message, and let the mini-buffer
10033 text show through.
10035 This function cancels echoing. */
10037 void
10038 message3 (Lisp_Object m)
10040 struct gcpro gcpro1;
10042 GCPRO1 (m);
10043 clear_message (true, true);
10044 cancel_echoing ();
10046 /* First flush out any partial line written with print. */
10047 message_log_maybe_newline ();
10048 if (STRINGP (m))
10050 ptrdiff_t nbytes = SBYTES (m);
10051 bool multibyte = STRING_MULTIBYTE (m);
10052 USE_SAFE_ALLOCA;
10053 char *buffer = SAFE_ALLOCA (nbytes);
10054 memcpy (buffer, SDATA (m), nbytes);
10055 message_dolog (buffer, nbytes, 1, multibyte);
10056 SAFE_FREE ();
10058 message3_nolog (m);
10060 UNGCPRO;
10064 /* The non-logging version of message3.
10065 This does not cancel echoing, because it is used for echoing.
10066 Perhaps we need to make a separate function for echoing
10067 and make this cancel echoing. */
10069 void
10070 message3_nolog (Lisp_Object m)
10072 struct frame *sf = SELECTED_FRAME ();
10074 if (FRAME_INITIAL_P (sf))
10076 if (noninteractive_need_newline)
10077 putc ('\n', stderr);
10078 noninteractive_need_newline = 0;
10079 if (STRINGP (m))
10081 Lisp_Object s = ENCODE_SYSTEM (m);
10083 fwrite (SDATA (s), SBYTES (s), 1, stderr);
10085 if (cursor_in_echo_area == 0)
10086 fprintf (stderr, "\n");
10087 fflush (stderr);
10089 /* Error messages get reported properly by cmd_error, so this must be just an
10090 informative message; if the frame hasn't really been initialized yet, just
10091 toss it. */
10092 else if (INTERACTIVE && sf->glyphs_initialized_p)
10094 /* Get the frame containing the mini-buffer
10095 that the selected frame is using. */
10096 Lisp_Object mini_window = FRAME_MINIBUF_WINDOW (sf);
10097 Lisp_Object frame = XWINDOW (mini_window)->frame;
10098 struct frame *f = XFRAME (frame);
10100 if (FRAME_VISIBLE_P (sf) && !FRAME_VISIBLE_P (f))
10101 Fmake_frame_visible (frame);
10103 if (STRINGP (m) && SCHARS (m) > 0)
10105 set_message (m);
10106 if (minibuffer_auto_raise)
10107 Fraise_frame (frame);
10108 /* Assume we are not echoing.
10109 (If we are, echo_now will override this.) */
10110 echo_message_buffer = Qnil;
10112 else
10113 clear_message (true, true);
10115 do_pending_window_change (0);
10116 echo_area_display (1);
10117 do_pending_window_change (0);
10118 if (FRAME_TERMINAL (f)->frame_up_to_date_hook)
10119 (*FRAME_TERMINAL (f)->frame_up_to_date_hook) (f);
10124 /* Display a null-terminated echo area message M. If M is 0, clear
10125 out any existing message, and let the mini-buffer text show through.
10127 The buffer M must continue to exist until after the echo area gets
10128 cleared or some other message gets displayed there. Do not pass
10129 text that is stored in a Lisp string. Do not pass text in a buffer
10130 that was alloca'd. */
10132 void
10133 message1 (const char *m)
10135 message3 (m ? build_unibyte_string (m) : Qnil);
10139 /* The non-logging counterpart of message1. */
10141 void
10142 message1_nolog (const char *m)
10144 message3_nolog (m ? build_unibyte_string (m) : Qnil);
10147 /* Display a message M which contains a single %s
10148 which gets replaced with STRING. */
10150 void
10151 message_with_string (const char *m, Lisp_Object string, int log)
10153 CHECK_STRING (string);
10155 if (noninteractive)
10157 if (m)
10159 /* ENCODE_SYSTEM below can GC and/or relocate the Lisp
10160 String whose data pointer might be passed to us in M. So
10161 we use a local copy. */
10162 char *fmt = xstrdup (m);
10164 if (noninteractive_need_newline)
10165 putc ('\n', stderr);
10166 noninteractive_need_newline = 0;
10167 fprintf (stderr, fmt, SDATA (ENCODE_SYSTEM (string)));
10168 if (!cursor_in_echo_area)
10169 fprintf (stderr, "\n");
10170 fflush (stderr);
10171 xfree (fmt);
10174 else if (INTERACTIVE)
10176 /* The frame whose minibuffer we're going to display the message on.
10177 It may be larger than the selected frame, so we need
10178 to use its buffer, not the selected frame's buffer. */
10179 Lisp_Object mini_window;
10180 struct frame *f, *sf = SELECTED_FRAME ();
10182 /* Get the frame containing the minibuffer
10183 that the selected frame is using. */
10184 mini_window = FRAME_MINIBUF_WINDOW (sf);
10185 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
10187 /* Error messages get reported properly by cmd_error, so this must be
10188 just an informative message; if the frame hasn't really been
10189 initialized yet, just toss it. */
10190 if (f->glyphs_initialized_p)
10192 Lisp_Object args[2], msg;
10193 struct gcpro gcpro1, gcpro2;
10195 args[0] = build_string (m);
10196 args[1] = msg = string;
10197 GCPRO2 (args[0], msg);
10198 gcpro1.nvars = 2;
10200 msg = Fformat (2, args);
10202 if (log)
10203 message3 (msg);
10204 else
10205 message3_nolog (msg);
10207 UNGCPRO;
10209 /* Print should start at the beginning of the message
10210 buffer next time. */
10211 message_buf_print = 0;
10217 /* Dump an informative message to the minibuf. If M is 0, clear out
10218 any existing message, and let the mini-buffer text show through. */
10220 static void
10221 vmessage (const char *m, va_list ap)
10223 if (noninteractive)
10225 if (m)
10227 if (noninteractive_need_newline)
10228 putc ('\n', stderr);
10229 noninteractive_need_newline = 0;
10230 vfprintf (stderr, m, ap);
10231 if (cursor_in_echo_area == 0)
10232 fprintf (stderr, "\n");
10233 fflush (stderr);
10236 else if (INTERACTIVE)
10238 /* The frame whose mini-buffer we're going to display the message
10239 on. It may be larger than the selected frame, so we need to
10240 use its buffer, not the selected frame's buffer. */
10241 Lisp_Object mini_window;
10242 struct frame *f, *sf = SELECTED_FRAME ();
10244 /* Get the frame containing the mini-buffer
10245 that the selected frame is using. */
10246 mini_window = FRAME_MINIBUF_WINDOW (sf);
10247 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
10249 /* Error messages get reported properly by cmd_error, so this must be
10250 just an informative message; if the frame hasn't really been
10251 initialized yet, just toss it. */
10252 if (f->glyphs_initialized_p)
10254 if (m)
10256 ptrdiff_t len;
10257 ptrdiff_t maxsize = FRAME_MESSAGE_BUF_SIZE (f);
10258 char *message_buf = alloca (maxsize + 1);
10260 len = doprnt (message_buf, maxsize, m, 0, ap);
10262 message3 (make_string (message_buf, len));
10264 else
10265 message1 (0);
10267 /* Print should start at the beginning of the message
10268 buffer next time. */
10269 message_buf_print = 0;
10274 void
10275 message (const char *m, ...)
10277 va_list ap;
10278 va_start (ap, m);
10279 vmessage (m, ap);
10280 va_end (ap);
10284 #if 0
10285 /* The non-logging version of message. */
10287 void
10288 message_nolog (const char *m, ...)
10290 Lisp_Object old_log_max;
10291 va_list ap;
10292 va_start (ap, m);
10293 old_log_max = Vmessage_log_max;
10294 Vmessage_log_max = Qnil;
10295 vmessage (m, ap);
10296 Vmessage_log_max = old_log_max;
10297 va_end (ap);
10299 #endif
10302 /* Display the current message in the current mini-buffer. This is
10303 only called from error handlers in process.c, and is not time
10304 critical. */
10306 void
10307 update_echo_area (void)
10309 if (!NILP (echo_area_buffer[0]))
10311 Lisp_Object string;
10312 string = Fcurrent_message ();
10313 message3 (string);
10318 /* Make sure echo area buffers in `echo_buffers' are live.
10319 If they aren't, make new ones. */
10321 static void
10322 ensure_echo_area_buffers (void)
10324 int i;
10326 for (i = 0; i < 2; ++i)
10327 if (!BUFFERP (echo_buffer[i])
10328 || !BUFFER_LIVE_P (XBUFFER (echo_buffer[i])))
10330 char name[30];
10331 Lisp_Object old_buffer;
10332 int j;
10334 old_buffer = echo_buffer[i];
10335 echo_buffer[i] = Fget_buffer_create
10336 (make_formatted_string (name, " *Echo Area %d*", i));
10337 bset_truncate_lines (XBUFFER (echo_buffer[i]), Qnil);
10338 /* to force word wrap in echo area -
10339 it was decided to postpone this*/
10340 /* XBUFFER (echo_buffer[i])->word_wrap = Qt; */
10342 for (j = 0; j < 2; ++j)
10343 if (EQ (old_buffer, echo_area_buffer[j]))
10344 echo_area_buffer[j] = echo_buffer[i];
10349 /* Call FN with args A1..A2 with either the current or last displayed
10350 echo_area_buffer as current buffer.
10352 WHICH zero means use the current message buffer
10353 echo_area_buffer[0]. If that is nil, choose a suitable buffer
10354 from echo_buffer[] and clear it.
10356 WHICH > 0 means use echo_area_buffer[1]. If that is nil, choose a
10357 suitable buffer from echo_buffer[] and clear it.
10359 If WHICH < 0, set echo_area_buffer[1] to echo_area_buffer[0], so
10360 that the current message becomes the last displayed one, make
10361 choose a suitable buffer for echo_area_buffer[0], and clear it.
10363 Value is what FN returns. */
10365 static int
10366 with_echo_area_buffer (struct window *w, int which,
10367 int (*fn) (ptrdiff_t, Lisp_Object),
10368 ptrdiff_t a1, Lisp_Object a2)
10370 Lisp_Object buffer;
10371 int this_one, the_other, clear_buffer_p, rc;
10372 ptrdiff_t count = SPECPDL_INDEX ();
10374 /* If buffers aren't live, make new ones. */
10375 ensure_echo_area_buffers ();
10377 clear_buffer_p = 0;
10379 if (which == 0)
10380 this_one = 0, the_other = 1;
10381 else if (which > 0)
10382 this_one = 1, the_other = 0;
10383 else
10385 this_one = 0, the_other = 1;
10386 clear_buffer_p = true;
10388 /* We need a fresh one in case the current echo buffer equals
10389 the one containing the last displayed echo area message. */
10390 if (!NILP (echo_area_buffer[this_one])
10391 && EQ (echo_area_buffer[this_one], echo_area_buffer[the_other]))
10392 echo_area_buffer[this_one] = Qnil;
10395 /* Choose a suitable buffer from echo_buffer[] is we don't
10396 have one. */
10397 if (NILP (echo_area_buffer[this_one]))
10399 echo_area_buffer[this_one]
10400 = (EQ (echo_area_buffer[the_other], echo_buffer[this_one])
10401 ? echo_buffer[the_other]
10402 : echo_buffer[this_one]);
10403 clear_buffer_p = true;
10406 buffer = echo_area_buffer[this_one];
10408 /* Don't get confused by reusing the buffer used for echoing
10409 for a different purpose. */
10410 if (echo_kboard == NULL && EQ (buffer, echo_message_buffer))
10411 cancel_echoing ();
10413 record_unwind_protect (unwind_with_echo_area_buffer,
10414 with_echo_area_buffer_unwind_data (w));
10416 /* Make the echo area buffer current. Note that for display
10417 purposes, it is not necessary that the displayed window's buffer
10418 == current_buffer, except for text property lookup. So, let's
10419 only set that buffer temporarily here without doing a full
10420 Fset_window_buffer. We must also change w->pointm, though,
10421 because otherwise an assertions in unshow_buffer fails, and Emacs
10422 aborts. */
10423 set_buffer_internal_1 (XBUFFER (buffer));
10424 if (w)
10426 wset_buffer (w, buffer);
10427 set_marker_both (w->pointm, buffer, BEG, BEG_BYTE);
10430 bset_undo_list (current_buffer, Qt);
10431 bset_read_only (current_buffer, Qnil);
10432 specbind (Qinhibit_read_only, Qt);
10433 specbind (Qinhibit_modification_hooks, Qt);
10435 if (clear_buffer_p && Z > BEG)
10436 del_range (BEG, Z);
10438 eassert (BEGV >= BEG);
10439 eassert (ZV <= Z && ZV >= BEGV);
10441 rc = fn (a1, a2);
10443 eassert (BEGV >= BEG);
10444 eassert (ZV <= Z && ZV >= BEGV);
10446 unbind_to (count, Qnil);
10447 return rc;
10451 /* Save state that should be preserved around the call to the function
10452 FN called in with_echo_area_buffer. */
10454 static Lisp_Object
10455 with_echo_area_buffer_unwind_data (struct window *w)
10457 int i = 0;
10458 Lisp_Object vector, tmp;
10460 /* Reduce consing by keeping one vector in
10461 Vwith_echo_area_save_vector. */
10462 vector = Vwith_echo_area_save_vector;
10463 Vwith_echo_area_save_vector = Qnil;
10465 if (NILP (vector))
10466 vector = Fmake_vector (make_number (9), Qnil);
10468 XSETBUFFER (tmp, current_buffer); ASET (vector, i, tmp); ++i;
10469 ASET (vector, i, Vdeactivate_mark); ++i;
10470 ASET (vector, i, make_number (windows_or_buffers_changed)); ++i;
10472 if (w)
10474 XSETWINDOW (tmp, w); ASET (vector, i, tmp); ++i;
10475 ASET (vector, i, w->contents); ++i;
10476 ASET (vector, i, make_number (marker_position (w->pointm))); ++i;
10477 ASET (vector, i, make_number (marker_byte_position (w->pointm))); ++i;
10478 ASET (vector, i, make_number (marker_position (w->start))); ++i;
10479 ASET (vector, i, make_number (marker_byte_position (w->start))); ++i;
10481 else
10483 int end = i + 6;
10484 for (; i < end; ++i)
10485 ASET (vector, i, Qnil);
10488 eassert (i == ASIZE (vector));
10489 return vector;
10493 /* Restore global state from VECTOR which was created by
10494 with_echo_area_buffer_unwind_data. */
10496 static void
10497 unwind_with_echo_area_buffer (Lisp_Object vector)
10499 set_buffer_internal_1 (XBUFFER (AREF (vector, 0)));
10500 Vdeactivate_mark = AREF (vector, 1);
10501 windows_or_buffers_changed = XFASTINT (AREF (vector, 2));
10503 if (WINDOWP (AREF (vector, 3)))
10505 struct window *w;
10506 Lisp_Object buffer;
10508 w = XWINDOW (AREF (vector, 3));
10509 buffer = AREF (vector, 4);
10511 wset_buffer (w, buffer);
10512 set_marker_both (w->pointm, buffer,
10513 XFASTINT (AREF (vector, 5)),
10514 XFASTINT (AREF (vector, 6)));
10515 set_marker_both (w->start, buffer,
10516 XFASTINT (AREF (vector, 7)),
10517 XFASTINT (AREF (vector, 8)));
10520 Vwith_echo_area_save_vector = vector;
10524 /* Set up the echo area for use by print functions. MULTIBYTE_P
10525 non-zero means we will print multibyte. */
10527 void
10528 setup_echo_area_for_printing (int multibyte_p)
10530 /* If we can't find an echo area any more, exit. */
10531 if (! FRAME_LIVE_P (XFRAME (selected_frame)))
10532 Fkill_emacs (Qnil);
10534 ensure_echo_area_buffers ();
10536 if (!message_buf_print)
10538 /* A message has been output since the last time we printed.
10539 Choose a fresh echo area buffer. */
10540 if (EQ (echo_area_buffer[1], echo_buffer[0]))
10541 echo_area_buffer[0] = echo_buffer[1];
10542 else
10543 echo_area_buffer[0] = echo_buffer[0];
10545 /* Switch to that buffer and clear it. */
10546 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
10547 bset_truncate_lines (current_buffer, Qnil);
10549 if (Z > BEG)
10551 ptrdiff_t count = SPECPDL_INDEX ();
10552 specbind (Qinhibit_read_only, Qt);
10553 /* Note that undo recording is always disabled. */
10554 del_range (BEG, Z);
10555 unbind_to (count, Qnil);
10557 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
10559 /* Set up the buffer for the multibyteness we need. */
10560 if (multibyte_p
10561 != !NILP (BVAR (current_buffer, enable_multibyte_characters)))
10562 Fset_buffer_multibyte (multibyte_p ? Qt : Qnil);
10564 /* Raise the frame containing the echo area. */
10565 if (minibuffer_auto_raise)
10567 struct frame *sf = SELECTED_FRAME ();
10568 Lisp_Object mini_window;
10569 mini_window = FRAME_MINIBUF_WINDOW (sf);
10570 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
10573 message_log_maybe_newline ();
10574 message_buf_print = 1;
10576 else
10578 if (NILP (echo_area_buffer[0]))
10580 if (EQ (echo_area_buffer[1], echo_buffer[0]))
10581 echo_area_buffer[0] = echo_buffer[1];
10582 else
10583 echo_area_buffer[0] = echo_buffer[0];
10586 if (current_buffer != XBUFFER (echo_area_buffer[0]))
10588 /* Someone switched buffers between print requests. */
10589 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
10590 bset_truncate_lines (current_buffer, Qnil);
10596 /* Display an echo area message in window W. Value is non-zero if W's
10597 height is changed. If display_last_displayed_message_p is
10598 non-zero, display the message that was last displayed, otherwise
10599 display the current message. */
10601 static int
10602 display_echo_area (struct window *w)
10604 int i, no_message_p, window_height_changed_p;
10606 /* Temporarily disable garbage collections while displaying the echo
10607 area. This is done because a GC can print a message itself.
10608 That message would modify the echo area buffer's contents while a
10609 redisplay of the buffer is going on, and seriously confuse
10610 redisplay. */
10611 ptrdiff_t count = inhibit_garbage_collection ();
10613 /* If there is no message, we must call display_echo_area_1
10614 nevertheless because it resizes the window. But we will have to
10615 reset the echo_area_buffer in question to nil at the end because
10616 with_echo_area_buffer will sets it to an empty buffer. */
10617 i = display_last_displayed_message_p ? 1 : 0;
10618 no_message_p = NILP (echo_area_buffer[i]);
10620 window_height_changed_p
10621 = with_echo_area_buffer (w, display_last_displayed_message_p,
10622 display_echo_area_1,
10623 (intptr_t) w, Qnil);
10625 if (no_message_p)
10626 echo_area_buffer[i] = Qnil;
10628 unbind_to (count, Qnil);
10629 return window_height_changed_p;
10633 /* Helper for display_echo_area. Display the current buffer which
10634 contains the current echo area message in window W, a mini-window,
10635 a pointer to which is passed in A1. A2..A4 are currently not used.
10636 Change the height of W so that all of the message is displayed.
10637 Value is non-zero if height of W was changed. */
10639 static int
10640 display_echo_area_1 (ptrdiff_t a1, Lisp_Object a2)
10642 intptr_t i1 = a1;
10643 struct window *w = (struct window *) i1;
10644 Lisp_Object window;
10645 struct text_pos start;
10646 int window_height_changed_p = 0;
10648 /* Do this before displaying, so that we have a large enough glyph
10649 matrix for the display. If we can't get enough space for the
10650 whole text, display the last N lines. That works by setting w->start. */
10651 window_height_changed_p = resize_mini_window (w, 0);
10653 /* Use the starting position chosen by resize_mini_window. */
10654 SET_TEXT_POS_FROM_MARKER (start, w->start);
10656 /* Display. */
10657 clear_glyph_matrix (w->desired_matrix);
10658 XSETWINDOW (window, w);
10659 try_window (window, start, 0);
10661 return window_height_changed_p;
10665 /* Resize the echo area window to exactly the size needed for the
10666 currently displayed message, if there is one. If a mini-buffer
10667 is active, don't shrink it. */
10669 void
10670 resize_echo_area_exactly (void)
10672 if (BUFFERP (echo_area_buffer[0])
10673 && WINDOWP (echo_area_window))
10675 struct window *w = XWINDOW (echo_area_window);
10676 Lisp_Object resize_exactly = (minibuf_level == 0 ? Qt : Qnil);
10677 int resized_p = with_echo_area_buffer (w, 0, resize_mini_window_1,
10678 (intptr_t) w, resize_exactly);
10679 if (resized_p)
10681 windows_or_buffers_changed = 42;
10682 update_mode_lines = 30;
10683 redisplay_internal ();
10689 /* Callback function for with_echo_area_buffer, when used from
10690 resize_echo_area_exactly. A1 contains a pointer to the window to
10691 resize, EXACTLY non-nil means resize the mini-window exactly to the
10692 size of the text displayed. A3 and A4 are not used. Value is what
10693 resize_mini_window returns. */
10695 static int
10696 resize_mini_window_1 (ptrdiff_t a1, Lisp_Object exactly)
10698 intptr_t i1 = a1;
10699 return resize_mini_window ((struct window *) i1, !NILP (exactly));
10703 /* Resize mini-window W to fit the size of its contents. EXACT_P
10704 means size the window exactly to the size needed. Otherwise, it's
10705 only enlarged until W's buffer is empty.
10707 Set W->start to the right place to begin display. If the whole
10708 contents fit, start at the beginning. Otherwise, start so as
10709 to make the end of the contents appear. This is particularly
10710 important for y-or-n-p, but seems desirable generally.
10712 Value is non-zero if the window height has been changed. */
10715 resize_mini_window (struct window *w, int exact_p)
10717 struct frame *f = XFRAME (w->frame);
10718 int window_height_changed_p = 0;
10720 eassert (MINI_WINDOW_P (w));
10722 /* By default, start display at the beginning. */
10723 set_marker_both (w->start, w->contents,
10724 BUF_BEGV (XBUFFER (w->contents)),
10725 BUF_BEGV_BYTE (XBUFFER (w->contents)));
10727 /* Don't resize windows while redisplaying a window; it would
10728 confuse redisplay functions when the size of the window they are
10729 displaying changes from under them. Such a resizing can happen,
10730 for instance, when which-func prints a long message while
10731 we are running fontification-functions. We're running these
10732 functions with safe_call which binds inhibit-redisplay to t. */
10733 if (!NILP (Vinhibit_redisplay))
10734 return 0;
10736 /* Nil means don't try to resize. */
10737 if (NILP (Vresize_mini_windows)
10738 || (FRAME_X_P (f) && FRAME_X_OUTPUT (f) == NULL))
10739 return 0;
10741 if (!FRAME_MINIBUF_ONLY_P (f))
10743 struct it it;
10744 int total_height = (WINDOW_PIXEL_HEIGHT (XWINDOW (FRAME_ROOT_WINDOW (f)))
10745 + WINDOW_PIXEL_HEIGHT (w));
10746 int unit = FRAME_LINE_HEIGHT (f);
10747 int height, max_height;
10748 struct text_pos start;
10749 struct buffer *old_current_buffer = NULL;
10751 if (current_buffer != XBUFFER (w->contents))
10753 old_current_buffer = current_buffer;
10754 set_buffer_internal (XBUFFER (w->contents));
10757 init_iterator (&it, w, BEGV, BEGV_BYTE, NULL, DEFAULT_FACE_ID);
10759 /* Compute the max. number of lines specified by the user. */
10760 if (FLOATP (Vmax_mini_window_height))
10761 max_height = XFLOATINT (Vmax_mini_window_height) * total_height;
10762 else if (INTEGERP (Vmax_mini_window_height))
10763 max_height = XINT (Vmax_mini_window_height) * unit;
10764 else
10765 max_height = total_height / 4;
10767 /* Correct that max. height if it's bogus. */
10768 max_height = clip_to_bounds (unit, max_height, total_height);
10770 /* Find out the height of the text in the window. */
10771 if (it.line_wrap == TRUNCATE)
10772 height = unit;
10773 else
10775 last_height = 0;
10776 move_it_to (&it, ZV, -1, -1, -1, MOVE_TO_POS);
10777 if (it.max_ascent == 0 && it.max_descent == 0)
10778 height = it.current_y + last_height;
10779 else
10780 height = it.current_y + it.max_ascent + it.max_descent;
10781 height -= min (it.extra_line_spacing, it.max_extra_line_spacing);
10784 /* Compute a suitable window start. */
10785 if (height > max_height)
10787 height = (max_height / unit) * unit;
10788 init_iterator (&it, w, ZV, ZV_BYTE, NULL, DEFAULT_FACE_ID);
10789 move_it_vertically_backward (&it, height - unit);
10790 start = it.current.pos;
10792 else
10793 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
10794 SET_MARKER_FROM_TEXT_POS (w->start, start);
10796 if (EQ (Vresize_mini_windows, Qgrow_only))
10798 /* Let it grow only, until we display an empty message, in which
10799 case the window shrinks again. */
10800 if (height > WINDOW_PIXEL_HEIGHT (w))
10802 int old_height = WINDOW_PIXEL_HEIGHT (w);
10804 FRAME_WINDOWS_FROZEN (f) = 1;
10805 grow_mini_window (w, height - WINDOW_PIXEL_HEIGHT (w), 1);
10806 window_height_changed_p = WINDOW_PIXEL_HEIGHT (w) != old_height;
10808 else if (height < WINDOW_PIXEL_HEIGHT (w)
10809 && (exact_p || BEGV == ZV))
10811 int old_height = WINDOW_PIXEL_HEIGHT (w);
10813 FRAME_WINDOWS_FROZEN (f) = 0;
10814 shrink_mini_window (w, 1);
10815 window_height_changed_p = WINDOW_PIXEL_HEIGHT (w) != old_height;
10818 else
10820 /* Always resize to exact size needed. */
10821 if (height > WINDOW_PIXEL_HEIGHT (w))
10823 int old_height = WINDOW_PIXEL_HEIGHT (w);
10825 FRAME_WINDOWS_FROZEN (f) = 1;
10826 grow_mini_window (w, height - WINDOW_PIXEL_HEIGHT (w), 1);
10827 window_height_changed_p = WINDOW_PIXEL_HEIGHT (w) != old_height;
10829 else if (height < WINDOW_PIXEL_HEIGHT (w))
10831 int old_height = WINDOW_PIXEL_HEIGHT (w);
10833 FRAME_WINDOWS_FROZEN (f) = 0;
10834 shrink_mini_window (w, 1);
10836 if (height)
10838 FRAME_WINDOWS_FROZEN (f) = 1;
10839 grow_mini_window (w, height - WINDOW_PIXEL_HEIGHT (w), 1);
10842 window_height_changed_p = WINDOW_PIXEL_HEIGHT (w) != old_height;
10846 if (old_current_buffer)
10847 set_buffer_internal (old_current_buffer);
10850 return window_height_changed_p;
10854 /* Value is the current message, a string, or nil if there is no
10855 current message. */
10857 Lisp_Object
10858 current_message (void)
10860 Lisp_Object msg;
10862 if (!BUFFERP (echo_area_buffer[0]))
10863 msg = Qnil;
10864 else
10866 with_echo_area_buffer (0, 0, current_message_1,
10867 (intptr_t) &msg, Qnil);
10868 if (NILP (msg))
10869 echo_area_buffer[0] = Qnil;
10872 return msg;
10876 static int
10877 current_message_1 (ptrdiff_t a1, Lisp_Object a2)
10879 intptr_t i1 = a1;
10880 Lisp_Object *msg = (Lisp_Object *) i1;
10882 if (Z > BEG)
10883 *msg = make_buffer_string (BEG, Z, 1);
10884 else
10885 *msg = Qnil;
10886 return 0;
10890 /* Push the current message on Vmessage_stack for later restoration
10891 by restore_message. Value is non-zero if the current message isn't
10892 empty. This is a relatively infrequent operation, so it's not
10893 worth optimizing. */
10895 bool
10896 push_message (void)
10898 Lisp_Object msg = current_message ();
10899 Vmessage_stack = Fcons (msg, Vmessage_stack);
10900 return STRINGP (msg);
10904 /* Restore message display from the top of Vmessage_stack. */
10906 void
10907 restore_message (void)
10909 eassert (CONSP (Vmessage_stack));
10910 message3_nolog (XCAR (Vmessage_stack));
10914 /* Handler for unwind-protect calling pop_message. */
10916 void
10917 pop_message_unwind (void)
10919 /* Pop the top-most entry off Vmessage_stack. */
10920 eassert (CONSP (Vmessage_stack));
10921 Vmessage_stack = XCDR (Vmessage_stack);
10925 /* Check that Vmessage_stack is nil. Called from emacs.c when Emacs
10926 exits. If the stack is not empty, we have a missing pop_message
10927 somewhere. */
10929 void
10930 check_message_stack (void)
10932 if (!NILP (Vmessage_stack))
10933 emacs_abort ();
10937 /* Truncate to NCHARS what will be displayed in the echo area the next
10938 time we display it---but don't redisplay it now. */
10940 void
10941 truncate_echo_area (ptrdiff_t nchars)
10943 if (nchars == 0)
10944 echo_area_buffer[0] = Qnil;
10945 else if (!noninteractive
10946 && INTERACTIVE
10947 && !NILP (echo_area_buffer[0]))
10949 struct frame *sf = SELECTED_FRAME ();
10950 /* Error messages get reported properly by cmd_error, so this must be
10951 just an informative message; if the frame hasn't really been
10952 initialized yet, just toss it. */
10953 if (sf->glyphs_initialized_p)
10954 with_echo_area_buffer (0, 0, truncate_message_1, nchars, Qnil);
10959 /* Helper function for truncate_echo_area. Truncate the current
10960 message to at most NCHARS characters. */
10962 static int
10963 truncate_message_1 (ptrdiff_t nchars, Lisp_Object a2)
10965 if (BEG + nchars < Z)
10966 del_range (BEG + nchars, Z);
10967 if (Z == BEG)
10968 echo_area_buffer[0] = Qnil;
10969 return 0;
10972 /* Set the current message to STRING. */
10974 static void
10975 set_message (Lisp_Object string)
10977 eassert (STRINGP (string));
10979 message_enable_multibyte = STRING_MULTIBYTE (string);
10981 with_echo_area_buffer (0, -1, set_message_1, 0, string);
10982 message_buf_print = 0;
10983 help_echo_showing_p = 0;
10985 if (STRINGP (Vdebug_on_message)
10986 && STRINGP (string)
10987 && fast_string_match (Vdebug_on_message, string) >= 0)
10988 call_debugger (list2 (Qerror, string));
10992 /* Helper function for set_message. First argument is ignored and second
10993 argument has the same meaning as for set_message.
10994 This function is called with the echo area buffer being current. */
10996 static int
10997 set_message_1 (ptrdiff_t a1, Lisp_Object string)
10999 eassert (STRINGP (string));
11001 /* Change multibyteness of the echo buffer appropriately. */
11002 if (message_enable_multibyte
11003 != !NILP (BVAR (current_buffer, enable_multibyte_characters)))
11004 Fset_buffer_multibyte (message_enable_multibyte ? Qt : Qnil);
11006 bset_truncate_lines (current_buffer, message_truncate_lines ? Qt : Qnil);
11007 if (!NILP (BVAR (current_buffer, bidi_display_reordering)))
11008 bset_bidi_paragraph_direction (current_buffer, Qleft_to_right);
11010 /* Insert new message at BEG. */
11011 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
11013 /* This function takes care of single/multibyte conversion.
11014 We just have to ensure that the echo area buffer has the right
11015 setting of enable_multibyte_characters. */
11016 insert_from_string (string, 0, 0, SCHARS (string), SBYTES (string), 1);
11018 return 0;
11022 /* Clear messages. CURRENT_P non-zero means clear the current
11023 message. LAST_DISPLAYED_P non-zero means clear the message
11024 last displayed. */
11026 void
11027 clear_message (bool current_p, bool last_displayed_p)
11029 if (current_p)
11031 echo_area_buffer[0] = Qnil;
11032 message_cleared_p = true;
11035 if (last_displayed_p)
11036 echo_area_buffer[1] = Qnil;
11038 message_buf_print = 0;
11041 /* Clear garbaged frames.
11043 This function is used where the old redisplay called
11044 redraw_garbaged_frames which in turn called redraw_frame which in
11045 turn called clear_frame. The call to clear_frame was a source of
11046 flickering. I believe a clear_frame is not necessary. It should
11047 suffice in the new redisplay to invalidate all current matrices,
11048 and ensure a complete redisplay of all windows. */
11050 static void
11051 clear_garbaged_frames (void)
11053 if (frame_garbaged)
11055 Lisp_Object tail, frame;
11057 FOR_EACH_FRAME (tail, frame)
11059 struct frame *f = XFRAME (frame);
11061 if (FRAME_VISIBLE_P (f) && FRAME_GARBAGED_P (f))
11063 if (f->resized_p)
11064 redraw_frame (f);
11065 else
11066 clear_current_matrices (f);
11067 fset_redisplay (f);
11068 f->garbaged = false;
11069 f->resized_p = false;
11073 frame_garbaged = false;
11078 /* Redisplay the echo area of the selected frame. If UPDATE_FRAME_P
11079 is non-zero update selected_frame. Value is non-zero if the
11080 mini-windows height has been changed. */
11082 static int
11083 echo_area_display (int update_frame_p)
11085 Lisp_Object mini_window;
11086 struct window *w;
11087 struct frame *f;
11088 int window_height_changed_p = 0;
11089 struct frame *sf = SELECTED_FRAME ();
11091 mini_window = FRAME_MINIBUF_WINDOW (sf);
11092 w = XWINDOW (mini_window);
11093 f = XFRAME (WINDOW_FRAME (w));
11095 /* Don't display if frame is invisible or not yet initialized. */
11096 if (!FRAME_VISIBLE_P (f) || !f->glyphs_initialized_p)
11097 return 0;
11099 #ifdef HAVE_WINDOW_SYSTEM
11100 /* When Emacs starts, selected_frame may be the initial terminal
11101 frame. If we let this through, a message would be displayed on
11102 the terminal. */
11103 if (FRAME_INITIAL_P (XFRAME (selected_frame)))
11104 return 0;
11105 #endif /* HAVE_WINDOW_SYSTEM */
11107 /* Redraw garbaged frames. */
11108 clear_garbaged_frames ();
11110 if (!NILP (echo_area_buffer[0]) || minibuf_level == 0)
11112 echo_area_window = mini_window;
11113 window_height_changed_p = display_echo_area (w);
11114 w->must_be_updated_p = true;
11116 /* Update the display, unless called from redisplay_internal.
11117 Also don't update the screen during redisplay itself. The
11118 update will happen at the end of redisplay, and an update
11119 here could cause confusion. */
11120 if (update_frame_p && !redisplaying_p)
11122 int n = 0;
11124 /* If the display update has been interrupted by pending
11125 input, update mode lines in the frame. Due to the
11126 pending input, it might have been that redisplay hasn't
11127 been called, so that mode lines above the echo area are
11128 garbaged. This looks odd, so we prevent it here. */
11129 if (!display_completed)
11130 n = redisplay_mode_lines (FRAME_ROOT_WINDOW (f), false);
11132 if (window_height_changed_p
11133 /* Don't do this if Emacs is shutting down. Redisplay
11134 needs to run hooks. */
11135 && !NILP (Vrun_hooks))
11137 /* Must update other windows. Likewise as in other
11138 cases, don't let this update be interrupted by
11139 pending input. */
11140 ptrdiff_t count = SPECPDL_INDEX ();
11141 specbind (Qredisplay_dont_pause, Qt);
11142 windows_or_buffers_changed = 44;
11143 redisplay_internal ();
11144 unbind_to (count, Qnil);
11146 else if (FRAME_WINDOW_P (f) && n == 0)
11148 /* Window configuration is the same as before.
11149 Can do with a display update of the echo area,
11150 unless we displayed some mode lines. */
11151 update_single_window (w, 1);
11152 flush_frame (f);
11154 else
11155 update_frame (f, 1, 1);
11157 /* If cursor is in the echo area, make sure that the next
11158 redisplay displays the minibuffer, so that the cursor will
11159 be replaced with what the minibuffer wants. */
11160 if (cursor_in_echo_area)
11161 wset_redisplay (XWINDOW (mini_window));
11164 else if (!EQ (mini_window, selected_window))
11165 wset_redisplay (XWINDOW (mini_window));
11167 /* Last displayed message is now the current message. */
11168 echo_area_buffer[1] = echo_area_buffer[0];
11169 /* Inform read_char that we're not echoing. */
11170 echo_message_buffer = Qnil;
11172 /* Prevent redisplay optimization in redisplay_internal by resetting
11173 this_line_start_pos. This is done because the mini-buffer now
11174 displays the message instead of its buffer text. */
11175 if (EQ (mini_window, selected_window))
11176 CHARPOS (this_line_start_pos) = 0;
11178 return window_height_changed_p;
11181 /* Nonzero if W's buffer was changed but not saved. */
11183 static int
11184 window_buffer_changed (struct window *w)
11186 struct buffer *b = XBUFFER (w->contents);
11188 eassert (BUFFER_LIVE_P (b));
11190 return (((BUF_SAVE_MODIFF (b) < BUF_MODIFF (b)) != w->last_had_star));
11193 /* Nonzero if W has %c in its mode line and mode line should be updated. */
11195 static int
11196 mode_line_update_needed (struct window *w)
11198 return (w->column_number_displayed != -1
11199 && !(PT == w->last_point && !window_outdated (w))
11200 && (w->column_number_displayed != current_column ()));
11203 /* Nonzero if window start of W is frozen and may not be changed during
11204 redisplay. */
11206 static bool
11207 window_frozen_p (struct window *w)
11209 if (FRAME_WINDOWS_FROZEN (XFRAME (WINDOW_FRAME (w))))
11211 Lisp_Object window;
11213 XSETWINDOW (window, w);
11214 if (MINI_WINDOW_P (w))
11215 return 0;
11216 else if (EQ (window, selected_window))
11217 return 0;
11218 else if (MINI_WINDOW_P (XWINDOW (selected_window))
11219 && EQ (window, Vminibuf_scroll_window))
11220 /* This special window can't be frozen too. */
11221 return 0;
11222 else
11223 return 1;
11225 return 0;
11228 /***********************************************************************
11229 Mode Lines and Frame Titles
11230 ***********************************************************************/
11232 /* A buffer for constructing non-propertized mode-line strings and
11233 frame titles in it; allocated from the heap in init_xdisp and
11234 resized as needed in store_mode_line_noprop_char. */
11236 static char *mode_line_noprop_buf;
11238 /* The buffer's end, and a current output position in it. */
11240 static char *mode_line_noprop_buf_end;
11241 static char *mode_line_noprop_ptr;
11243 #define MODE_LINE_NOPROP_LEN(start) \
11244 ((mode_line_noprop_ptr - mode_line_noprop_buf) - start)
11246 static enum {
11247 MODE_LINE_DISPLAY = 0,
11248 MODE_LINE_TITLE,
11249 MODE_LINE_NOPROP,
11250 MODE_LINE_STRING
11251 } mode_line_target;
11253 /* Alist that caches the results of :propertize.
11254 Each element is (PROPERTIZED-STRING . PROPERTY-LIST). */
11255 static Lisp_Object mode_line_proptrans_alist;
11257 /* List of strings making up the mode-line. */
11258 static Lisp_Object mode_line_string_list;
11260 /* Base face property when building propertized mode line string. */
11261 static Lisp_Object mode_line_string_face;
11262 static Lisp_Object mode_line_string_face_prop;
11265 /* Unwind data for mode line strings */
11267 static Lisp_Object Vmode_line_unwind_vector;
11269 static Lisp_Object
11270 format_mode_line_unwind_data (struct frame *target_frame,
11271 struct buffer *obuf,
11272 Lisp_Object owin,
11273 int save_proptrans)
11275 Lisp_Object vector, tmp;
11277 /* Reduce consing by keeping one vector in
11278 Vwith_echo_area_save_vector. */
11279 vector = Vmode_line_unwind_vector;
11280 Vmode_line_unwind_vector = Qnil;
11282 if (NILP (vector))
11283 vector = Fmake_vector (make_number (10), Qnil);
11285 ASET (vector, 0, make_number (mode_line_target));
11286 ASET (vector, 1, make_number (MODE_LINE_NOPROP_LEN (0)));
11287 ASET (vector, 2, mode_line_string_list);
11288 ASET (vector, 3, save_proptrans ? mode_line_proptrans_alist : Qt);
11289 ASET (vector, 4, mode_line_string_face);
11290 ASET (vector, 5, mode_line_string_face_prop);
11292 if (obuf)
11293 XSETBUFFER (tmp, obuf);
11294 else
11295 tmp = Qnil;
11296 ASET (vector, 6, tmp);
11297 ASET (vector, 7, owin);
11298 if (target_frame)
11300 /* Similarly to `with-selected-window', if the operation selects
11301 a window on another frame, we must restore that frame's
11302 selected window, and (for a tty) the top-frame. */
11303 ASET (vector, 8, target_frame->selected_window);
11304 if (FRAME_TERMCAP_P (target_frame))
11305 ASET (vector, 9, FRAME_TTY (target_frame)->top_frame);
11308 return vector;
11311 static void
11312 unwind_format_mode_line (Lisp_Object vector)
11314 Lisp_Object old_window = AREF (vector, 7);
11315 Lisp_Object target_frame_window = AREF (vector, 8);
11316 Lisp_Object old_top_frame = AREF (vector, 9);
11318 mode_line_target = XINT (AREF (vector, 0));
11319 mode_line_noprop_ptr = mode_line_noprop_buf + XINT (AREF (vector, 1));
11320 mode_line_string_list = AREF (vector, 2);
11321 if (! EQ (AREF (vector, 3), Qt))
11322 mode_line_proptrans_alist = AREF (vector, 3);
11323 mode_line_string_face = AREF (vector, 4);
11324 mode_line_string_face_prop = AREF (vector, 5);
11326 /* Select window before buffer, since it may change the buffer. */
11327 if (!NILP (old_window))
11329 /* If the operation that we are unwinding had selected a window
11330 on a different frame, reset its frame-selected-window. For a
11331 text terminal, reset its top-frame if necessary. */
11332 if (!NILP (target_frame_window))
11334 Lisp_Object frame
11335 = WINDOW_FRAME (XWINDOW (target_frame_window));
11337 if (!EQ (frame, WINDOW_FRAME (XWINDOW (old_window))))
11338 Fselect_window (target_frame_window, Qt);
11340 if (!NILP (old_top_frame) && !EQ (old_top_frame, frame))
11341 Fselect_frame (old_top_frame, Qt);
11344 Fselect_window (old_window, Qt);
11347 if (!NILP (AREF (vector, 6)))
11349 set_buffer_internal_1 (XBUFFER (AREF (vector, 6)));
11350 ASET (vector, 6, Qnil);
11353 Vmode_line_unwind_vector = vector;
11357 /* Store a single character C for the frame title in mode_line_noprop_buf.
11358 Re-allocate mode_line_noprop_buf if necessary. */
11360 static void
11361 store_mode_line_noprop_char (char c)
11363 /* If output position has reached the end of the allocated buffer,
11364 increase the buffer's size. */
11365 if (mode_line_noprop_ptr == mode_line_noprop_buf_end)
11367 ptrdiff_t len = MODE_LINE_NOPROP_LEN (0);
11368 ptrdiff_t size = len;
11369 mode_line_noprop_buf =
11370 xpalloc (mode_line_noprop_buf, &size, 1, STRING_BYTES_BOUND, 1);
11371 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
11372 mode_line_noprop_ptr = mode_line_noprop_buf + len;
11375 *mode_line_noprop_ptr++ = c;
11379 /* Store part of a frame title in mode_line_noprop_buf, beginning at
11380 mode_line_noprop_ptr. STRING is the string to store. Do not copy
11381 characters that yield more columns than PRECISION; PRECISION <= 0
11382 means copy the whole string. Pad with spaces until FIELD_WIDTH
11383 number of characters have been copied; FIELD_WIDTH <= 0 means don't
11384 pad. Called from display_mode_element when it is used to build a
11385 frame title. */
11387 static int
11388 store_mode_line_noprop (const char *string, int field_width, int precision)
11390 const unsigned char *str = (const unsigned char *) string;
11391 int n = 0;
11392 ptrdiff_t dummy, nbytes;
11394 /* Copy at most PRECISION chars from STR. */
11395 nbytes = strlen (string);
11396 n += c_string_width (str, nbytes, precision, &dummy, &nbytes);
11397 while (nbytes--)
11398 store_mode_line_noprop_char (*str++);
11400 /* Fill up with spaces until FIELD_WIDTH reached. */
11401 while (field_width > 0
11402 && n < field_width)
11404 store_mode_line_noprop_char (' ');
11405 ++n;
11408 return n;
11411 /***********************************************************************
11412 Frame Titles
11413 ***********************************************************************/
11415 #ifdef HAVE_WINDOW_SYSTEM
11417 /* Set the title of FRAME, if it has changed. The title format is
11418 Vicon_title_format if FRAME is iconified, otherwise it is
11419 frame_title_format. */
11421 static void
11422 x_consider_frame_title (Lisp_Object frame)
11424 struct frame *f = XFRAME (frame);
11426 if (FRAME_WINDOW_P (f)
11427 || FRAME_MINIBUF_ONLY_P (f)
11428 || f->explicit_name)
11430 /* Do we have more than one visible frame on this X display? */
11431 Lisp_Object tail, other_frame, fmt;
11432 ptrdiff_t title_start;
11433 char *title;
11434 ptrdiff_t len;
11435 struct it it;
11436 ptrdiff_t count = SPECPDL_INDEX ();
11438 FOR_EACH_FRAME (tail, other_frame)
11440 struct frame *tf = XFRAME (other_frame);
11442 if (tf != f
11443 && FRAME_KBOARD (tf) == FRAME_KBOARD (f)
11444 && !FRAME_MINIBUF_ONLY_P (tf)
11445 && !EQ (other_frame, tip_frame)
11446 && (FRAME_VISIBLE_P (tf) || FRAME_ICONIFIED_P (tf)))
11447 break;
11450 /* Set global variable indicating that multiple frames exist. */
11451 multiple_frames = CONSP (tail);
11453 /* Switch to the buffer of selected window of the frame. Set up
11454 mode_line_target so that display_mode_element will output into
11455 mode_line_noprop_buf; then display the title. */
11456 record_unwind_protect (unwind_format_mode_line,
11457 format_mode_line_unwind_data
11458 (f, current_buffer, selected_window, 0));
11460 Fselect_window (f->selected_window, Qt);
11461 set_buffer_internal_1
11462 (XBUFFER (XWINDOW (f->selected_window)->contents));
11463 fmt = FRAME_ICONIFIED_P (f) ? Vicon_title_format : Vframe_title_format;
11465 mode_line_target = MODE_LINE_TITLE;
11466 title_start = MODE_LINE_NOPROP_LEN (0);
11467 init_iterator (&it, XWINDOW (f->selected_window), -1, -1,
11468 NULL, DEFAULT_FACE_ID);
11469 display_mode_element (&it, 0, -1, -1, fmt, Qnil, 0);
11470 len = MODE_LINE_NOPROP_LEN (title_start);
11471 title = mode_line_noprop_buf + title_start;
11472 unbind_to (count, Qnil);
11474 /* Set the title only if it's changed. This avoids consing in
11475 the common case where it hasn't. (If it turns out that we've
11476 already wasted too much time by walking through the list with
11477 display_mode_element, then we might need to optimize at a
11478 higher level than this.) */
11479 if (! STRINGP (f->name)
11480 || SBYTES (f->name) != len
11481 || memcmp (title, SDATA (f->name), len) != 0)
11482 x_implicitly_set_name (f, make_string (title, len), Qnil);
11486 #endif /* not HAVE_WINDOW_SYSTEM */
11489 /***********************************************************************
11490 Menu Bars
11491 ***********************************************************************/
11493 /* Non-zero if we will not redisplay all visible windows. */
11494 #define REDISPLAY_SOME_P() \
11495 ((windows_or_buffers_changed == 0 \
11496 || windows_or_buffers_changed == REDISPLAY_SOME) \
11497 && (update_mode_lines == 0 \
11498 || update_mode_lines == REDISPLAY_SOME))
11500 /* Prepare for redisplay by updating menu-bar item lists when
11501 appropriate. This can call eval. */
11503 static void
11504 prepare_menu_bars (void)
11506 bool all_windows = windows_or_buffers_changed || update_mode_lines;
11507 bool some_windows = REDISPLAY_SOME_P ();
11508 struct gcpro gcpro1, gcpro2;
11509 Lisp_Object tooltip_frame;
11511 #ifdef HAVE_WINDOW_SYSTEM
11512 tooltip_frame = tip_frame;
11513 #else
11514 tooltip_frame = Qnil;
11515 #endif
11517 if (FUNCTIONP (Vpre_redisplay_function))
11519 Lisp_Object windows = all_windows ? Qt : Qnil;
11520 if (all_windows && some_windows)
11522 Lisp_Object ws = window_list ();
11523 for (windows = Qnil; CONSP (ws); ws = XCDR (ws))
11525 Lisp_Object this = XCAR (ws);
11526 struct window *w = XWINDOW (this);
11527 if (w->redisplay
11528 || XFRAME (w->frame)->redisplay
11529 || XBUFFER (w->contents)->text->redisplay)
11531 windows = Fcons (this, windows);
11535 safe_call1 (Vpre_redisplay_function, windows);
11538 /* Update all frame titles based on their buffer names, etc. We do
11539 this before the menu bars so that the buffer-menu will show the
11540 up-to-date frame titles. */
11541 #ifdef HAVE_WINDOW_SYSTEM
11542 if (all_windows)
11544 Lisp_Object tail, frame;
11546 FOR_EACH_FRAME (tail, frame)
11548 struct frame *f = XFRAME (frame);
11549 struct window *w = XWINDOW (FRAME_SELECTED_WINDOW (f));
11550 if (some_windows
11551 && !f->redisplay
11552 && !w->redisplay
11553 && !XBUFFER (w->contents)->text->redisplay)
11554 continue;
11556 if (!EQ (frame, tooltip_frame)
11557 && (FRAME_ICONIFIED_P (f)
11558 || FRAME_VISIBLE_P (f) == 1
11559 /* Exclude TTY frames that are obscured because they
11560 are not the top frame on their console. This is
11561 because x_consider_frame_title actually switches
11562 to the frame, which for TTY frames means it is
11563 marked as garbaged, and will be completely
11564 redrawn on the next redisplay cycle. This causes
11565 TTY frames to be completely redrawn, when there
11566 are more than one of them, even though nothing
11567 should be changed on display. */
11568 || (FRAME_VISIBLE_P (f) == 2 && FRAME_WINDOW_P (f))))
11569 x_consider_frame_title (frame);
11572 #endif /* HAVE_WINDOW_SYSTEM */
11574 /* Update the menu bar item lists, if appropriate. This has to be
11575 done before any actual redisplay or generation of display lines. */
11577 if (all_windows)
11579 Lisp_Object tail, frame;
11580 ptrdiff_t count = SPECPDL_INDEX ();
11581 /* 1 means that update_menu_bar has run its hooks
11582 so any further calls to update_menu_bar shouldn't do so again. */
11583 int menu_bar_hooks_run = 0;
11585 record_unwind_save_match_data ();
11587 FOR_EACH_FRAME (tail, frame)
11589 struct frame *f = XFRAME (frame);
11590 struct window *w = XWINDOW (FRAME_SELECTED_WINDOW (f));
11592 /* Ignore tooltip frame. */
11593 if (EQ (frame, tooltip_frame))
11594 continue;
11596 if (some_windows
11597 && !f->redisplay
11598 && !w->redisplay
11599 && !XBUFFER (w->contents)->text->redisplay)
11600 continue;
11602 /* If a window on this frame changed size, report that to
11603 the user and clear the size-change flag. */
11604 if (FRAME_WINDOW_SIZES_CHANGED (f))
11606 Lisp_Object functions;
11608 /* Clear flag first in case we get an error below. */
11609 FRAME_WINDOW_SIZES_CHANGED (f) = 0;
11610 functions = Vwindow_size_change_functions;
11611 GCPRO2 (tail, functions);
11613 while (CONSP (functions))
11615 if (!EQ (XCAR (functions), Qt))
11616 call1 (XCAR (functions), frame);
11617 functions = XCDR (functions);
11619 UNGCPRO;
11622 GCPRO1 (tail);
11623 menu_bar_hooks_run = update_menu_bar (f, 0, menu_bar_hooks_run);
11624 #ifdef HAVE_WINDOW_SYSTEM
11625 update_tool_bar (f, 0);
11626 #endif
11627 #ifdef HAVE_NS
11628 if (windows_or_buffers_changed
11629 && FRAME_NS_P (f))
11630 ns_set_doc_edited
11631 (f, Fbuffer_modified_p (XWINDOW (f->selected_window)->contents));
11632 #endif
11633 UNGCPRO;
11636 unbind_to (count, Qnil);
11638 else
11640 struct frame *sf = SELECTED_FRAME ();
11641 update_menu_bar (sf, 1, 0);
11642 #ifdef HAVE_WINDOW_SYSTEM
11643 update_tool_bar (sf, 1);
11644 #endif
11649 /* Update the menu bar item list for frame F. This has to be done
11650 before we start to fill in any display lines, because it can call
11651 eval.
11653 If SAVE_MATCH_DATA is non-zero, we must save and restore it here.
11655 If HOOKS_RUN is 1, that means a previous call to update_menu_bar
11656 already ran the menu bar hooks for this redisplay, so there
11657 is no need to run them again. The return value is the
11658 updated value of this flag, to pass to the next call. */
11660 static int
11661 update_menu_bar (struct frame *f, int save_match_data, int hooks_run)
11663 Lisp_Object window;
11664 register struct window *w;
11666 /* If called recursively during a menu update, do nothing. This can
11667 happen when, for instance, an activate-menubar-hook causes a
11668 redisplay. */
11669 if (inhibit_menubar_update)
11670 return hooks_run;
11672 window = FRAME_SELECTED_WINDOW (f);
11673 w = XWINDOW (window);
11675 if (FRAME_WINDOW_P (f)
11677 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
11678 || defined (HAVE_NS) || defined (USE_GTK)
11679 FRAME_EXTERNAL_MENU_BAR (f)
11680 #else
11681 FRAME_MENU_BAR_LINES (f) > 0
11682 #endif
11683 : FRAME_MENU_BAR_LINES (f) > 0)
11685 /* If the user has switched buffers or windows, we need to
11686 recompute to reflect the new bindings. But we'll
11687 recompute when update_mode_lines is set too; that means
11688 that people can use force-mode-line-update to request
11689 that the menu bar be recomputed. The adverse effect on
11690 the rest of the redisplay algorithm is about the same as
11691 windows_or_buffers_changed anyway. */
11692 if (windows_or_buffers_changed
11693 /* This used to test w->update_mode_line, but we believe
11694 there is no need to recompute the menu in that case. */
11695 || update_mode_lines
11696 || window_buffer_changed (w))
11698 struct buffer *prev = current_buffer;
11699 ptrdiff_t count = SPECPDL_INDEX ();
11701 specbind (Qinhibit_menubar_update, Qt);
11703 set_buffer_internal_1 (XBUFFER (w->contents));
11704 if (save_match_data)
11705 record_unwind_save_match_data ();
11706 if (NILP (Voverriding_local_map_menu_flag))
11708 specbind (Qoverriding_terminal_local_map, Qnil);
11709 specbind (Qoverriding_local_map, Qnil);
11712 if (!hooks_run)
11714 /* Run the Lucid hook. */
11715 safe_run_hooks (Qactivate_menubar_hook);
11717 /* If it has changed current-menubar from previous value,
11718 really recompute the menu-bar from the value. */
11719 if (! NILP (Vlucid_menu_bar_dirty_flag))
11720 call0 (Qrecompute_lucid_menubar);
11722 safe_run_hooks (Qmenu_bar_update_hook);
11724 hooks_run = 1;
11727 XSETFRAME (Vmenu_updating_frame, f);
11728 fset_menu_bar_items (f, menu_bar_items (FRAME_MENU_BAR_ITEMS (f)));
11730 /* Redisplay the menu bar in case we changed it. */
11731 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
11732 || defined (HAVE_NS) || defined (USE_GTK)
11733 if (FRAME_WINDOW_P (f))
11735 #if defined (HAVE_NS)
11736 /* All frames on Mac OS share the same menubar. So only
11737 the selected frame should be allowed to set it. */
11738 if (f == SELECTED_FRAME ())
11739 #endif
11740 set_frame_menubar (f, 0, 0);
11742 else
11743 /* On a terminal screen, the menu bar is an ordinary screen
11744 line, and this makes it get updated. */
11745 w->update_mode_line = 1;
11746 #else /* ! (USE_X_TOOLKIT || HAVE_NTGUI || HAVE_NS || USE_GTK) */
11747 /* In the non-toolkit version, the menu bar is an ordinary screen
11748 line, and this makes it get updated. */
11749 w->update_mode_line = 1;
11750 #endif /* ! (USE_X_TOOLKIT || HAVE_NTGUI || HAVE_NS || USE_GTK) */
11752 unbind_to (count, Qnil);
11753 set_buffer_internal_1 (prev);
11757 return hooks_run;
11760 /***********************************************************************
11761 Tool-bars
11762 ***********************************************************************/
11764 #ifdef HAVE_WINDOW_SYSTEM
11766 /* Tool-bar item index of the item on which a mouse button was pressed
11767 or -1. */
11769 int last_tool_bar_item;
11771 /* Select `frame' temporarily without running all the code in
11772 do_switch_frame.
11773 FIXME: Maybe do_switch_frame should be trimmed down similarly
11774 when `norecord' is set. */
11775 static void
11776 fast_set_selected_frame (Lisp_Object frame)
11778 if (!EQ (selected_frame, frame))
11780 selected_frame = frame;
11781 selected_window = XFRAME (frame)->selected_window;
11785 /* Update the tool-bar item list for frame F. This has to be done
11786 before we start to fill in any display lines. Called from
11787 prepare_menu_bars. If SAVE_MATCH_DATA is non-zero, we must save
11788 and restore it here. */
11790 static void
11791 update_tool_bar (struct frame *f, int save_match_data)
11793 #if defined (USE_GTK) || defined (HAVE_NS)
11794 int do_update = FRAME_EXTERNAL_TOOL_BAR (f);
11795 #else
11796 int do_update = (WINDOWP (f->tool_bar_window)
11797 && WINDOW_PIXEL_HEIGHT (XWINDOW (f->tool_bar_window)) > 0);
11798 #endif
11800 if (do_update)
11802 Lisp_Object window;
11803 struct window *w;
11805 window = FRAME_SELECTED_WINDOW (f);
11806 w = XWINDOW (window);
11808 /* If the user has switched buffers or windows, we need to
11809 recompute to reflect the new bindings. But we'll
11810 recompute when update_mode_lines is set too; that means
11811 that people can use force-mode-line-update to request
11812 that the menu bar be recomputed. The adverse effect on
11813 the rest of the redisplay algorithm is about the same as
11814 windows_or_buffers_changed anyway. */
11815 if (windows_or_buffers_changed
11816 || w->update_mode_line
11817 || update_mode_lines
11818 || window_buffer_changed (w))
11820 struct buffer *prev = current_buffer;
11821 ptrdiff_t count = SPECPDL_INDEX ();
11822 Lisp_Object frame, new_tool_bar;
11823 int new_n_tool_bar;
11824 struct gcpro gcpro1;
11826 /* Set current_buffer to the buffer of the selected
11827 window of the frame, so that we get the right local
11828 keymaps. */
11829 set_buffer_internal_1 (XBUFFER (w->contents));
11831 /* Save match data, if we must. */
11832 if (save_match_data)
11833 record_unwind_save_match_data ();
11835 /* Make sure that we don't accidentally use bogus keymaps. */
11836 if (NILP (Voverriding_local_map_menu_flag))
11838 specbind (Qoverriding_terminal_local_map, Qnil);
11839 specbind (Qoverriding_local_map, Qnil);
11842 GCPRO1 (new_tool_bar);
11844 /* We must temporarily set the selected frame to this frame
11845 before calling tool_bar_items, because the calculation of
11846 the tool-bar keymap uses the selected frame (see
11847 `tool-bar-make-keymap' in tool-bar.el). */
11848 eassert (EQ (selected_window,
11849 /* Since we only explicitly preserve selected_frame,
11850 check that selected_window would be redundant. */
11851 XFRAME (selected_frame)->selected_window));
11852 record_unwind_protect (fast_set_selected_frame, selected_frame);
11853 XSETFRAME (frame, f);
11854 fast_set_selected_frame (frame);
11856 /* Build desired tool-bar items from keymaps. */
11857 new_tool_bar
11858 = tool_bar_items (Fcopy_sequence (f->tool_bar_items),
11859 &new_n_tool_bar);
11861 /* Redisplay the tool-bar if we changed it. */
11862 if (new_n_tool_bar != f->n_tool_bar_items
11863 || NILP (Fequal (new_tool_bar, f->tool_bar_items)))
11865 /* Redisplay that happens asynchronously due to an expose event
11866 may access f->tool_bar_items. Make sure we update both
11867 variables within BLOCK_INPUT so no such event interrupts. */
11868 block_input ();
11869 fset_tool_bar_items (f, new_tool_bar);
11870 f->n_tool_bar_items = new_n_tool_bar;
11871 w->update_mode_line = 1;
11872 unblock_input ();
11875 UNGCPRO;
11877 unbind_to (count, Qnil);
11878 set_buffer_internal_1 (prev);
11883 #if ! defined (USE_GTK) && ! defined (HAVE_NS)
11885 /* Set F->desired_tool_bar_string to a Lisp string representing frame
11886 F's desired tool-bar contents. F->tool_bar_items must have
11887 been set up previously by calling prepare_menu_bars. */
11889 static void
11890 build_desired_tool_bar_string (struct frame *f)
11892 int i, size, size_needed;
11893 struct gcpro gcpro1, gcpro2, gcpro3;
11894 Lisp_Object image, plist, props;
11896 image = plist = props = Qnil;
11897 GCPRO3 (image, plist, props);
11899 /* Prepare F->desired_tool_bar_string. If we can reuse it, do so.
11900 Otherwise, make a new string. */
11902 /* The size of the string we might be able to reuse. */
11903 size = (STRINGP (f->desired_tool_bar_string)
11904 ? SCHARS (f->desired_tool_bar_string)
11905 : 0);
11907 /* We need one space in the string for each image. */
11908 size_needed = f->n_tool_bar_items;
11910 /* Reuse f->desired_tool_bar_string, if possible. */
11911 if (size < size_needed || NILP (f->desired_tool_bar_string))
11912 fset_desired_tool_bar_string
11913 (f, Fmake_string (make_number (size_needed), make_number (' ')));
11914 else
11916 props = list4 (Qdisplay, Qnil, Qmenu_item, Qnil);
11917 Fremove_text_properties (make_number (0), make_number (size),
11918 props, f->desired_tool_bar_string);
11921 /* Put a `display' property on the string for the images to display,
11922 put a `menu_item' property on tool-bar items with a value that
11923 is the index of the item in F's tool-bar item vector. */
11924 for (i = 0; i < f->n_tool_bar_items; ++i)
11926 #define PROP(IDX) \
11927 AREF (f->tool_bar_items, i * TOOL_BAR_ITEM_NSLOTS + (IDX))
11929 int enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
11930 int selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
11931 int hmargin, vmargin, relief, idx, end;
11933 /* If image is a vector, choose the image according to the
11934 button state. */
11935 image = PROP (TOOL_BAR_ITEM_IMAGES);
11936 if (VECTORP (image))
11938 if (enabled_p)
11939 idx = (selected_p
11940 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
11941 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
11942 else
11943 idx = (selected_p
11944 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
11945 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
11947 eassert (ASIZE (image) >= idx);
11948 image = AREF (image, idx);
11950 else
11951 idx = -1;
11953 /* Ignore invalid image specifications. */
11954 if (!valid_image_p (image))
11955 continue;
11957 /* Display the tool-bar button pressed, or depressed. */
11958 plist = Fcopy_sequence (XCDR (image));
11960 /* Compute margin and relief to draw. */
11961 relief = (tool_bar_button_relief >= 0
11962 ? tool_bar_button_relief
11963 : DEFAULT_TOOL_BAR_BUTTON_RELIEF);
11964 hmargin = vmargin = relief;
11966 if (RANGED_INTEGERP (1, Vtool_bar_button_margin,
11967 INT_MAX - max (hmargin, vmargin)))
11969 hmargin += XFASTINT (Vtool_bar_button_margin);
11970 vmargin += XFASTINT (Vtool_bar_button_margin);
11972 else if (CONSP (Vtool_bar_button_margin))
11974 if (RANGED_INTEGERP (1, XCAR (Vtool_bar_button_margin),
11975 INT_MAX - hmargin))
11976 hmargin += XFASTINT (XCAR (Vtool_bar_button_margin));
11978 if (RANGED_INTEGERP (1, XCDR (Vtool_bar_button_margin),
11979 INT_MAX - vmargin))
11980 vmargin += XFASTINT (XCDR (Vtool_bar_button_margin));
11983 if (auto_raise_tool_bar_buttons_p)
11985 /* Add a `:relief' property to the image spec if the item is
11986 selected. */
11987 if (selected_p)
11989 plist = Fplist_put (plist, QCrelief, make_number (-relief));
11990 hmargin -= relief;
11991 vmargin -= relief;
11994 else
11996 /* If image is selected, display it pressed, i.e. with a
11997 negative relief. If it's not selected, display it with a
11998 raised relief. */
11999 plist = Fplist_put (plist, QCrelief,
12000 (selected_p
12001 ? make_number (-relief)
12002 : make_number (relief)));
12003 hmargin -= relief;
12004 vmargin -= relief;
12007 /* Put a margin around the image. */
12008 if (hmargin || vmargin)
12010 if (hmargin == vmargin)
12011 plist = Fplist_put (plist, QCmargin, make_number (hmargin));
12012 else
12013 plist = Fplist_put (plist, QCmargin,
12014 Fcons (make_number (hmargin),
12015 make_number (vmargin)));
12018 /* If button is not enabled, and we don't have special images
12019 for the disabled state, make the image appear disabled by
12020 applying an appropriate algorithm to it. */
12021 if (!enabled_p && idx < 0)
12022 plist = Fplist_put (plist, QCconversion, Qdisabled);
12024 /* Put a `display' text property on the string for the image to
12025 display. Put a `menu-item' property on the string that gives
12026 the start of this item's properties in the tool-bar items
12027 vector. */
12028 image = Fcons (Qimage, plist);
12029 props = list4 (Qdisplay, image,
12030 Qmenu_item, make_number (i * TOOL_BAR_ITEM_NSLOTS));
12032 /* Let the last image hide all remaining spaces in the tool bar
12033 string. The string can be longer than needed when we reuse a
12034 previous string. */
12035 if (i + 1 == f->n_tool_bar_items)
12036 end = SCHARS (f->desired_tool_bar_string);
12037 else
12038 end = i + 1;
12039 Fadd_text_properties (make_number (i), make_number (end),
12040 props, f->desired_tool_bar_string);
12041 #undef PROP
12044 UNGCPRO;
12048 /* Display one line of the tool-bar of frame IT->f.
12050 HEIGHT specifies the desired height of the tool-bar line.
12051 If the actual height of the glyph row is less than HEIGHT, the
12052 row's height is increased to HEIGHT, and the icons are centered
12053 vertically in the new height.
12055 If HEIGHT is -1, we are counting needed tool-bar lines, so don't
12056 count a final empty row in case the tool-bar width exactly matches
12057 the window width.
12060 static void
12061 display_tool_bar_line (struct it *it, int height)
12063 struct glyph_row *row = it->glyph_row;
12064 int max_x = it->last_visible_x;
12065 struct glyph *last;
12067 /* Don't extend on a previously drawn tool bar items (Bug#16058). */
12068 clear_glyph_row (row);
12069 row->enabled_p = true;
12070 row->y = it->current_y;
12072 /* Note that this isn't made use of if the face hasn't a box,
12073 so there's no need to check the face here. */
12074 it->start_of_box_run_p = 1;
12076 while (it->current_x < max_x)
12078 int x, n_glyphs_before, i, nglyphs;
12079 struct it it_before;
12081 /* Get the next display element. */
12082 if (!get_next_display_element (it))
12084 /* Don't count empty row if we are counting needed tool-bar lines. */
12085 if (height < 0 && !it->hpos)
12086 return;
12087 break;
12090 /* Produce glyphs. */
12091 n_glyphs_before = row->used[TEXT_AREA];
12092 it_before = *it;
12094 PRODUCE_GLYPHS (it);
12096 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
12097 i = 0;
12098 x = it_before.current_x;
12099 while (i < nglyphs)
12101 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
12103 if (x + glyph->pixel_width > max_x)
12105 /* Glyph doesn't fit on line. Backtrack. */
12106 row->used[TEXT_AREA] = n_glyphs_before;
12107 *it = it_before;
12108 /* If this is the only glyph on this line, it will never fit on the
12109 tool-bar, so skip it. But ensure there is at least one glyph,
12110 so we don't accidentally disable the tool-bar. */
12111 if (n_glyphs_before == 0
12112 && (it->vpos > 0 || IT_STRING_CHARPOS (*it) < it->end_charpos-1))
12113 break;
12114 goto out;
12117 ++it->hpos;
12118 x += glyph->pixel_width;
12119 ++i;
12122 /* Stop at line end. */
12123 if (ITERATOR_AT_END_OF_LINE_P (it))
12124 break;
12126 set_iterator_to_next (it, 1);
12129 out:;
12131 row->displays_text_p = row->used[TEXT_AREA] != 0;
12133 /* Use default face for the border below the tool bar.
12135 FIXME: When auto-resize-tool-bars is grow-only, there is
12136 no additional border below the possibly empty tool-bar lines.
12137 So to make the extra empty lines look "normal", we have to
12138 use the tool-bar face for the border too. */
12139 if (!MATRIX_ROW_DISPLAYS_TEXT_P (row)
12140 && !EQ (Vauto_resize_tool_bars, Qgrow_only))
12141 it->face_id = DEFAULT_FACE_ID;
12143 extend_face_to_end_of_line (it);
12144 last = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
12145 last->right_box_line_p = 1;
12146 if (last == row->glyphs[TEXT_AREA])
12147 last->left_box_line_p = 1;
12149 /* Make line the desired height and center it vertically. */
12150 if ((height -= it->max_ascent + it->max_descent) > 0)
12152 /* Don't add more than one line height. */
12153 height %= FRAME_LINE_HEIGHT (it->f);
12154 it->max_ascent += height / 2;
12155 it->max_descent += (height + 1) / 2;
12158 compute_line_metrics (it);
12160 /* If line is empty, make it occupy the rest of the tool-bar. */
12161 if (!MATRIX_ROW_DISPLAYS_TEXT_P (row))
12163 row->height = row->phys_height = it->last_visible_y - row->y;
12164 row->visible_height = row->height;
12165 row->ascent = row->phys_ascent = 0;
12166 row->extra_line_spacing = 0;
12169 row->full_width_p = 1;
12170 row->continued_p = 0;
12171 row->truncated_on_left_p = 0;
12172 row->truncated_on_right_p = 0;
12174 it->current_x = it->hpos = 0;
12175 it->current_y += row->height;
12176 ++it->vpos;
12177 ++it->glyph_row;
12181 /* Max tool-bar height. Basically, this is what makes all other windows
12182 disappear when the frame gets too small. Rethink this! */
12184 #define MAX_FRAME_TOOL_BAR_HEIGHT(f) \
12185 ((FRAME_LINE_HEIGHT (f) * FRAME_LINES (f)))
12187 /* Value is the number of pixels needed to make all tool-bar items of
12188 frame F visible. The actual number of glyph rows needed is
12189 returned in *N_ROWS if non-NULL. */
12191 static int
12192 tool_bar_height (struct frame *f, int *n_rows, bool pixelwise)
12194 struct window *w = XWINDOW (f->tool_bar_window);
12195 struct it it;
12196 /* tool_bar_height is called from redisplay_tool_bar after building
12197 the desired matrix, so use (unused) mode-line row as temporary row to
12198 avoid destroying the first tool-bar row. */
12199 struct glyph_row *temp_row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
12201 /* Initialize an iterator for iteration over
12202 F->desired_tool_bar_string in the tool-bar window of frame F. */
12203 init_iterator (&it, w, -1, -1, temp_row, TOOL_BAR_FACE_ID);
12204 it.first_visible_x = 0;
12205 it.last_visible_x = WINDOW_PIXEL_WIDTH (w);
12206 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
12207 it.paragraph_embedding = L2R;
12209 while (!ITERATOR_AT_END_P (&it))
12211 clear_glyph_row (temp_row);
12212 it.glyph_row = temp_row;
12213 display_tool_bar_line (&it, -1);
12215 clear_glyph_row (temp_row);
12217 /* f->n_tool_bar_rows == 0 means "unknown"; -1 means no tool-bar. */
12218 if (n_rows)
12219 *n_rows = it.vpos > 0 ? it.vpos : -1;
12221 if (pixelwise)
12222 return it.current_y;
12223 else
12224 return (it.current_y + FRAME_LINE_HEIGHT (f) - 1) / FRAME_LINE_HEIGHT (f);
12227 #endif /* !USE_GTK && !HAVE_NS */
12229 #if defined USE_GTK || defined HAVE_NS
12230 EXFUN (Ftool_bar_height, 2) ATTRIBUTE_CONST;
12231 EXFUN (Ftool_bar_lines_needed, 1) ATTRIBUTE_CONST;
12232 #endif
12234 DEFUN ("tool-bar-height", Ftool_bar_height, Stool_bar_height,
12235 0, 2, 0,
12236 doc: /* Return the number of lines occupied by the tool bar of FRAME.
12237 If FRAME is nil or omitted, use the selected frame. Optional argument
12238 PIXELWISE non-nil means return the height of the tool bar in pixels. */)
12239 (Lisp_Object frame, Lisp_Object pixelwise)
12241 int height = 0;
12243 #if ! defined (USE_GTK) && ! defined (HAVE_NS)
12244 struct frame *f = decode_any_frame (frame);
12246 if (WINDOWP (f->tool_bar_window)
12247 && WINDOW_PIXEL_HEIGHT (XWINDOW (f->tool_bar_window)) > 0)
12249 update_tool_bar (f, 1);
12250 if (f->n_tool_bar_items)
12252 build_desired_tool_bar_string (f);
12253 height = tool_bar_height (f, NULL, NILP (pixelwise) ? 0 : 1);
12256 #endif
12258 return make_number (height);
12262 /* Display the tool-bar of frame F. Value is non-zero if tool-bar's
12263 height should be changed. */
12265 static int
12266 redisplay_tool_bar (struct frame *f)
12268 #if defined (USE_GTK) || defined (HAVE_NS)
12270 if (FRAME_EXTERNAL_TOOL_BAR (f))
12271 update_frame_tool_bar (f);
12272 return 0;
12274 #else /* !USE_GTK && !HAVE_NS */
12276 struct window *w;
12277 struct it it;
12278 struct glyph_row *row;
12280 /* If frame hasn't a tool-bar window or if it is zero-height, don't
12281 do anything. This means you must start with tool-bar-lines
12282 non-zero to get the auto-sizing effect. Or in other words, you
12283 can turn off tool-bars by specifying tool-bar-lines zero. */
12284 if (!WINDOWP (f->tool_bar_window)
12285 || (w = XWINDOW (f->tool_bar_window),
12286 WINDOW_PIXEL_HEIGHT (w) == 0))
12287 return 0;
12289 /* Set up an iterator for the tool-bar window. */
12290 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
12291 it.first_visible_x = 0;
12292 it.last_visible_x = WINDOW_PIXEL_WIDTH (w);
12293 row = it.glyph_row;
12295 /* Build a string that represents the contents of the tool-bar. */
12296 build_desired_tool_bar_string (f);
12297 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
12298 /* FIXME: This should be controlled by a user option. But it
12299 doesn't make sense to have an R2L tool bar if the menu bar cannot
12300 be drawn also R2L, and making the menu bar R2L is tricky due
12301 toolkit-specific code that implements it. If an R2L tool bar is
12302 ever supported, display_tool_bar_line should also be augmented to
12303 call unproduce_glyphs like display_line and display_string
12304 do. */
12305 it.paragraph_embedding = L2R;
12307 if (f->n_tool_bar_rows == 0)
12309 int new_height = tool_bar_height (f, &f->n_tool_bar_rows, 1);
12311 if (new_height != WINDOW_PIXEL_HEIGHT (w))
12313 Lisp_Object frame;
12314 int new_lines = ((new_height + FRAME_LINE_HEIGHT (f) - 1)
12315 / FRAME_LINE_HEIGHT (f));
12317 XSETFRAME (frame, f);
12318 Fmodify_frame_parameters (frame,
12319 list1 (Fcons (Qtool_bar_lines,
12320 make_number (new_lines))));
12321 /* Always do that now. */
12322 clear_glyph_matrix (w->desired_matrix);
12323 f->fonts_changed = 1;
12324 return 1;
12328 /* Display as many lines as needed to display all tool-bar items. */
12330 if (f->n_tool_bar_rows > 0)
12332 int border, rows, height, extra;
12334 if (TYPE_RANGED_INTEGERP (int, Vtool_bar_border))
12335 border = XINT (Vtool_bar_border);
12336 else if (EQ (Vtool_bar_border, Qinternal_border_width))
12337 border = FRAME_INTERNAL_BORDER_WIDTH (f);
12338 else if (EQ (Vtool_bar_border, Qborder_width))
12339 border = f->border_width;
12340 else
12341 border = 0;
12342 if (border < 0)
12343 border = 0;
12345 rows = f->n_tool_bar_rows;
12346 height = max (1, (it.last_visible_y - border) / rows);
12347 extra = it.last_visible_y - border - height * rows;
12349 while (it.current_y < it.last_visible_y)
12351 int h = 0;
12352 if (extra > 0 && rows-- > 0)
12354 h = (extra + rows - 1) / rows;
12355 extra -= h;
12357 display_tool_bar_line (&it, height + h);
12360 else
12362 while (it.current_y < it.last_visible_y)
12363 display_tool_bar_line (&it, 0);
12366 /* It doesn't make much sense to try scrolling in the tool-bar
12367 window, so don't do it. */
12368 w->desired_matrix->no_scrolling_p = 1;
12369 w->must_be_updated_p = 1;
12371 if (!NILP (Vauto_resize_tool_bars))
12373 /* Do we really allow the toolbar to occupy the whole frame? */
12374 int max_tool_bar_height = MAX_FRAME_TOOL_BAR_HEIGHT (f);
12375 int change_height_p = 0;
12377 /* If we couldn't display everything, change the tool-bar's
12378 height if there is room for more. */
12379 if (IT_STRING_CHARPOS (it) < it.end_charpos
12380 && it.current_y < max_tool_bar_height)
12381 change_height_p = 1;
12383 /* We subtract 1 because display_tool_bar_line advances the
12384 glyph_row pointer before returning to its caller. We want to
12385 examine the last glyph row produced by
12386 display_tool_bar_line. */
12387 row = it.glyph_row - 1;
12389 /* If there are blank lines at the end, except for a partially
12390 visible blank line at the end that is smaller than
12391 FRAME_LINE_HEIGHT, change the tool-bar's height. */
12392 if (!MATRIX_ROW_DISPLAYS_TEXT_P (row)
12393 && row->height >= FRAME_LINE_HEIGHT (f))
12394 change_height_p = 1;
12396 /* If row displays tool-bar items, but is partially visible,
12397 change the tool-bar's height. */
12398 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
12399 && MATRIX_ROW_BOTTOM_Y (row) > it.last_visible_y
12400 && MATRIX_ROW_BOTTOM_Y (row) < max_tool_bar_height)
12401 change_height_p = 1;
12403 /* Resize windows as needed by changing the `tool-bar-lines'
12404 frame parameter. */
12405 if (change_height_p)
12407 Lisp_Object frame;
12408 int nrows;
12409 int new_height = tool_bar_height (f, &nrows, 1);
12411 change_height_p = ((EQ (Vauto_resize_tool_bars, Qgrow_only)
12412 && !f->minimize_tool_bar_window_p)
12413 ? (new_height > WINDOW_PIXEL_HEIGHT (w))
12414 : (new_height != WINDOW_PIXEL_HEIGHT (w)));
12415 f->minimize_tool_bar_window_p = 0;
12417 if (change_height_p)
12419 /* Current size of the tool-bar window in canonical line
12420 units. */
12421 int old_lines = WINDOW_TOTAL_LINES (w);
12422 /* Required size of the tool-bar window in canonical
12423 line units. */
12424 int new_lines = ((new_height + FRAME_LINE_HEIGHT (f) - 1)
12425 / FRAME_LINE_HEIGHT (f));
12426 /* Maximum size of the tool-bar window in canonical line
12427 units that this frame can allow. */
12428 int max_lines =
12429 WINDOW_TOTAL_LINES (XWINDOW (FRAME_ROOT_WINDOW (f))) - 1;
12431 /* Don't try to change the tool-bar window size and set
12432 the fonts_changed flag unless really necessary. That
12433 flag causes redisplay to give up and retry
12434 redisplaying the frame from scratch, so setting it
12435 unnecessarily can lead to nasty redisplay loops. */
12436 if (new_lines <= max_lines
12437 && eabs (new_lines - old_lines) >= 1)
12439 XSETFRAME (frame, f);
12440 Fmodify_frame_parameters (frame,
12441 list1 (Fcons (Qtool_bar_lines,
12442 make_number (new_lines))));
12443 clear_glyph_matrix (w->desired_matrix);
12444 f->n_tool_bar_rows = nrows;
12445 f->fonts_changed = 1;
12446 return 1;
12452 f->minimize_tool_bar_window_p = 0;
12453 return 0;
12455 #endif /* USE_GTK || HAVE_NS */
12458 #if ! defined (USE_GTK) && ! defined (HAVE_NS)
12460 /* Get information about the tool-bar item which is displayed in GLYPH
12461 on frame F. Return in *PROP_IDX the index where tool-bar item
12462 properties start in F->tool_bar_items. Value is zero if
12463 GLYPH doesn't display a tool-bar item. */
12465 static int
12466 tool_bar_item_info (struct frame *f, struct glyph *glyph, int *prop_idx)
12468 Lisp_Object prop;
12469 int success_p;
12470 int charpos;
12472 /* This function can be called asynchronously, which means we must
12473 exclude any possibility that Fget_text_property signals an
12474 error. */
12475 charpos = min (SCHARS (f->current_tool_bar_string), glyph->charpos);
12476 charpos = max (0, charpos);
12478 /* Get the text property `menu-item' at pos. The value of that
12479 property is the start index of this item's properties in
12480 F->tool_bar_items. */
12481 prop = Fget_text_property (make_number (charpos),
12482 Qmenu_item, f->current_tool_bar_string);
12483 if (INTEGERP (prop))
12485 *prop_idx = XINT (prop);
12486 success_p = 1;
12488 else
12489 success_p = 0;
12491 return success_p;
12495 /* Get information about the tool-bar item at position X/Y on frame F.
12496 Return in *GLYPH a pointer to the glyph of the tool-bar item in
12497 the current matrix of the tool-bar window of F, or NULL if not
12498 on a tool-bar item. Return in *PROP_IDX the index of the tool-bar
12499 item in F->tool_bar_items. Value is
12501 -1 if X/Y is not on a tool-bar item
12502 0 if X/Y is on the same item that was highlighted before.
12503 1 otherwise. */
12505 static int
12506 get_tool_bar_item (struct frame *f, int x, int y, struct glyph **glyph,
12507 int *hpos, int *vpos, int *prop_idx)
12509 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12510 struct window *w = XWINDOW (f->tool_bar_window);
12511 int area;
12513 /* Find the glyph under X/Y. */
12514 *glyph = x_y_to_hpos_vpos (w, x, y, hpos, vpos, 0, 0, &area);
12515 if (*glyph == NULL)
12516 return -1;
12518 /* Get the start of this tool-bar item's properties in
12519 f->tool_bar_items. */
12520 if (!tool_bar_item_info (f, *glyph, prop_idx))
12521 return -1;
12523 /* Is mouse on the highlighted item? */
12524 if (EQ (f->tool_bar_window, hlinfo->mouse_face_window)
12525 && *vpos >= hlinfo->mouse_face_beg_row
12526 && *vpos <= hlinfo->mouse_face_end_row
12527 && (*vpos > hlinfo->mouse_face_beg_row
12528 || *hpos >= hlinfo->mouse_face_beg_col)
12529 && (*vpos < hlinfo->mouse_face_end_row
12530 || *hpos < hlinfo->mouse_face_end_col
12531 || hlinfo->mouse_face_past_end))
12532 return 0;
12534 return 1;
12538 /* EXPORT:
12539 Handle mouse button event on the tool-bar of frame F, at
12540 frame-relative coordinates X/Y. DOWN_P is 1 for a button press,
12541 0 for button release. MODIFIERS is event modifiers for button
12542 release. */
12544 void
12545 handle_tool_bar_click (struct frame *f, int x, int y, int down_p,
12546 int modifiers)
12548 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12549 struct window *w = XWINDOW (f->tool_bar_window);
12550 int hpos, vpos, prop_idx;
12551 struct glyph *glyph;
12552 Lisp_Object enabled_p;
12553 int ts;
12555 /* If not on the highlighted tool-bar item, and mouse-highlight is
12556 non-nil, return. This is so we generate the tool-bar button
12557 click only when the mouse button is released on the same item as
12558 where it was pressed. However, when mouse-highlight is disabled,
12559 generate the click when the button is released regardless of the
12560 highlight, since tool-bar items are not highlighted in that
12561 case. */
12562 frame_to_window_pixel_xy (w, &x, &y);
12563 ts = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
12564 if (ts == -1
12565 || (ts != 0 && !NILP (Vmouse_highlight)))
12566 return;
12568 /* When mouse-highlight is off, generate the click for the item
12569 where the button was pressed, disregarding where it was
12570 released. */
12571 if (NILP (Vmouse_highlight) && !down_p)
12572 prop_idx = last_tool_bar_item;
12574 /* If item is disabled, do nothing. */
12575 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
12576 if (NILP (enabled_p))
12577 return;
12579 if (down_p)
12581 /* Show item in pressed state. */
12582 if (!NILP (Vmouse_highlight))
12583 show_mouse_face (hlinfo, DRAW_IMAGE_SUNKEN);
12584 last_tool_bar_item = prop_idx;
12586 else
12588 Lisp_Object key, frame;
12589 struct input_event event;
12590 EVENT_INIT (event);
12592 /* Show item in released state. */
12593 if (!NILP (Vmouse_highlight))
12594 show_mouse_face (hlinfo, DRAW_IMAGE_RAISED);
12596 key = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_KEY);
12598 XSETFRAME (frame, f);
12599 event.kind = TOOL_BAR_EVENT;
12600 event.frame_or_window = frame;
12601 event.arg = frame;
12602 kbd_buffer_store_event (&event);
12604 event.kind = TOOL_BAR_EVENT;
12605 event.frame_or_window = frame;
12606 event.arg = key;
12607 event.modifiers = modifiers;
12608 kbd_buffer_store_event (&event);
12609 last_tool_bar_item = -1;
12614 /* Possibly highlight a tool-bar item on frame F when mouse moves to
12615 tool-bar window-relative coordinates X/Y. Called from
12616 note_mouse_highlight. */
12618 static void
12619 note_tool_bar_highlight (struct frame *f, int x, int y)
12621 Lisp_Object window = f->tool_bar_window;
12622 struct window *w = XWINDOW (window);
12623 Display_Info *dpyinfo = FRAME_DISPLAY_INFO (f);
12624 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12625 int hpos, vpos;
12626 struct glyph *glyph;
12627 struct glyph_row *row;
12628 int i;
12629 Lisp_Object enabled_p;
12630 int prop_idx;
12631 enum draw_glyphs_face draw = DRAW_IMAGE_RAISED;
12632 int mouse_down_p, rc;
12634 /* Function note_mouse_highlight is called with negative X/Y
12635 values when mouse moves outside of the frame. */
12636 if (x <= 0 || y <= 0)
12638 clear_mouse_face (hlinfo);
12639 return;
12642 rc = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
12643 if (rc < 0)
12645 /* Not on tool-bar item. */
12646 clear_mouse_face (hlinfo);
12647 return;
12649 else if (rc == 0)
12650 /* On same tool-bar item as before. */
12651 goto set_help_echo;
12653 clear_mouse_face (hlinfo);
12655 /* Mouse is down, but on different tool-bar item? */
12656 mouse_down_p = (x_mouse_grabbed (dpyinfo)
12657 && f == dpyinfo->last_mouse_frame);
12659 if (mouse_down_p
12660 && last_tool_bar_item != prop_idx)
12661 return;
12663 draw = mouse_down_p ? DRAW_IMAGE_SUNKEN : DRAW_IMAGE_RAISED;
12665 /* If tool-bar item is not enabled, don't highlight it. */
12666 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
12667 if (!NILP (enabled_p) && !NILP (Vmouse_highlight))
12669 /* Compute the x-position of the glyph. In front and past the
12670 image is a space. We include this in the highlighted area. */
12671 row = MATRIX_ROW (w->current_matrix, vpos);
12672 for (i = x = 0; i < hpos; ++i)
12673 x += row->glyphs[TEXT_AREA][i].pixel_width;
12675 /* Record this as the current active region. */
12676 hlinfo->mouse_face_beg_col = hpos;
12677 hlinfo->mouse_face_beg_row = vpos;
12678 hlinfo->mouse_face_beg_x = x;
12679 hlinfo->mouse_face_past_end = 0;
12681 hlinfo->mouse_face_end_col = hpos + 1;
12682 hlinfo->mouse_face_end_row = vpos;
12683 hlinfo->mouse_face_end_x = x + glyph->pixel_width;
12684 hlinfo->mouse_face_window = window;
12685 hlinfo->mouse_face_face_id = TOOL_BAR_FACE_ID;
12687 /* Display it as active. */
12688 show_mouse_face (hlinfo, draw);
12691 set_help_echo:
12693 /* Set help_echo_string to a help string to display for this tool-bar item.
12694 XTread_socket does the rest. */
12695 help_echo_object = help_echo_window = Qnil;
12696 help_echo_pos = -1;
12697 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_HELP);
12698 if (NILP (help_echo_string))
12699 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_CAPTION);
12702 #endif /* !USE_GTK && !HAVE_NS */
12704 #endif /* HAVE_WINDOW_SYSTEM */
12708 /************************************************************************
12709 Horizontal scrolling
12710 ************************************************************************/
12712 static int hscroll_window_tree (Lisp_Object);
12713 static int hscroll_windows (Lisp_Object);
12715 /* For all leaf windows in the window tree rooted at WINDOW, set their
12716 hscroll value so that PT is (i) visible in the window, and (ii) so
12717 that it is not within a certain margin at the window's left and
12718 right border. Value is non-zero if any window's hscroll has been
12719 changed. */
12721 static int
12722 hscroll_window_tree (Lisp_Object window)
12724 int hscrolled_p = 0;
12725 int hscroll_relative_p = FLOATP (Vhscroll_step);
12726 int hscroll_step_abs = 0;
12727 double hscroll_step_rel = 0;
12729 if (hscroll_relative_p)
12731 hscroll_step_rel = XFLOAT_DATA (Vhscroll_step);
12732 if (hscroll_step_rel < 0)
12734 hscroll_relative_p = 0;
12735 hscroll_step_abs = 0;
12738 else if (TYPE_RANGED_INTEGERP (int, Vhscroll_step))
12740 hscroll_step_abs = XINT (Vhscroll_step);
12741 if (hscroll_step_abs < 0)
12742 hscroll_step_abs = 0;
12744 else
12745 hscroll_step_abs = 0;
12747 while (WINDOWP (window))
12749 struct window *w = XWINDOW (window);
12751 if (WINDOWP (w->contents))
12752 hscrolled_p |= hscroll_window_tree (w->contents);
12753 else if (w->cursor.vpos >= 0)
12755 int h_margin;
12756 int text_area_width;
12757 struct glyph_row *cursor_row;
12758 struct glyph_row *bottom_row;
12759 int row_r2l_p;
12761 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->desired_matrix, w);
12762 if (w->cursor.vpos < bottom_row - w->desired_matrix->rows)
12763 cursor_row = MATRIX_ROW (w->desired_matrix, w->cursor.vpos);
12764 else
12765 cursor_row = bottom_row - 1;
12767 if (!cursor_row->enabled_p)
12769 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
12770 if (w->cursor.vpos < bottom_row - w->current_matrix->rows)
12771 cursor_row = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
12772 else
12773 cursor_row = bottom_row - 1;
12775 row_r2l_p = cursor_row->reversed_p;
12777 text_area_width = window_box_width (w, TEXT_AREA);
12779 /* Scroll when cursor is inside this scroll margin. */
12780 h_margin = hscroll_margin * WINDOW_FRAME_COLUMN_WIDTH (w);
12782 if (!NILP (Fbuffer_local_value (Qauto_hscroll_mode, w->contents))
12783 /* For left-to-right rows, hscroll when cursor is either
12784 (i) inside the right hscroll margin, or (ii) if it is
12785 inside the left margin and the window is already
12786 hscrolled. */
12787 && ((!row_r2l_p
12788 && ((w->hscroll
12789 && w->cursor.x <= h_margin)
12790 || (cursor_row->enabled_p
12791 && cursor_row->truncated_on_right_p
12792 && (w->cursor.x >= text_area_width - h_margin))))
12793 /* For right-to-left rows, the logic is similar,
12794 except that rules for scrolling to left and right
12795 are reversed. E.g., if cursor.x <= h_margin, we
12796 need to hscroll "to the right" unconditionally,
12797 and that will scroll the screen to the left so as
12798 to reveal the next portion of the row. */
12799 || (row_r2l_p
12800 && ((cursor_row->enabled_p
12801 /* FIXME: It is confusing to set the
12802 truncated_on_right_p flag when R2L rows
12803 are actually truncated on the left. */
12804 && cursor_row->truncated_on_right_p
12805 && w->cursor.x <= h_margin)
12806 || (w->hscroll
12807 && (w->cursor.x >= text_area_width - h_margin))))))
12809 struct it it;
12810 ptrdiff_t hscroll;
12811 struct buffer *saved_current_buffer;
12812 ptrdiff_t pt;
12813 int wanted_x;
12815 /* Find point in a display of infinite width. */
12816 saved_current_buffer = current_buffer;
12817 current_buffer = XBUFFER (w->contents);
12819 if (w == XWINDOW (selected_window))
12820 pt = PT;
12821 else
12822 pt = clip_to_bounds (BEGV, marker_position (w->pointm), ZV);
12824 /* Move iterator to pt starting at cursor_row->start in
12825 a line with infinite width. */
12826 init_to_row_start (&it, w, cursor_row);
12827 it.last_visible_x = INFINITY;
12828 move_it_in_display_line_to (&it, pt, -1, MOVE_TO_POS);
12829 current_buffer = saved_current_buffer;
12831 /* Position cursor in window. */
12832 if (!hscroll_relative_p && hscroll_step_abs == 0)
12833 hscroll = max (0, (it.current_x
12834 - (ITERATOR_AT_END_OF_LINE_P (&it)
12835 ? (text_area_width - 4 * FRAME_COLUMN_WIDTH (it.f))
12836 : (text_area_width / 2))))
12837 / FRAME_COLUMN_WIDTH (it.f);
12838 else if ((!row_r2l_p
12839 && w->cursor.x >= text_area_width - h_margin)
12840 || (row_r2l_p && w->cursor.x <= h_margin))
12842 if (hscroll_relative_p)
12843 wanted_x = text_area_width * (1 - hscroll_step_rel)
12844 - h_margin;
12845 else
12846 wanted_x = text_area_width
12847 - hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
12848 - h_margin;
12849 hscroll
12850 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
12852 else
12854 if (hscroll_relative_p)
12855 wanted_x = text_area_width * hscroll_step_rel
12856 + h_margin;
12857 else
12858 wanted_x = hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
12859 + h_margin;
12860 hscroll
12861 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
12863 hscroll = max (hscroll, w->min_hscroll);
12865 /* Don't prevent redisplay optimizations if hscroll
12866 hasn't changed, as it will unnecessarily slow down
12867 redisplay. */
12868 if (w->hscroll != hscroll)
12870 XBUFFER (w->contents)->prevent_redisplay_optimizations_p = 1;
12871 w->hscroll = hscroll;
12872 hscrolled_p = 1;
12877 window = w->next;
12880 /* Value is non-zero if hscroll of any leaf window has been changed. */
12881 return hscrolled_p;
12885 /* Set hscroll so that cursor is visible and not inside horizontal
12886 scroll margins for all windows in the tree rooted at WINDOW. See
12887 also hscroll_window_tree above. Value is non-zero if any window's
12888 hscroll has been changed. If it has, desired matrices on the frame
12889 of WINDOW are cleared. */
12891 static int
12892 hscroll_windows (Lisp_Object window)
12894 int hscrolled_p = hscroll_window_tree (window);
12895 if (hscrolled_p)
12896 clear_desired_matrices (XFRAME (WINDOW_FRAME (XWINDOW (window))));
12897 return hscrolled_p;
12902 /************************************************************************
12903 Redisplay
12904 ************************************************************************/
12906 /* Variables holding some state of redisplay if GLYPH_DEBUG is defined
12907 to a non-zero value. This is sometimes handy to have in a debugger
12908 session. */
12910 #ifdef GLYPH_DEBUG
12912 /* First and last unchanged row for try_window_id. */
12914 static int debug_first_unchanged_at_end_vpos;
12915 static int debug_last_unchanged_at_beg_vpos;
12917 /* Delta vpos and y. */
12919 static int debug_dvpos, debug_dy;
12921 /* Delta in characters and bytes for try_window_id. */
12923 static ptrdiff_t debug_delta, debug_delta_bytes;
12925 /* Values of window_end_pos and window_end_vpos at the end of
12926 try_window_id. */
12928 static ptrdiff_t debug_end_vpos;
12930 /* Append a string to W->desired_matrix->method. FMT is a printf
12931 format string. If trace_redisplay_p is true also printf the
12932 resulting string to stderr. */
12934 static void debug_method_add (struct window *, char const *, ...)
12935 ATTRIBUTE_FORMAT_PRINTF (2, 3);
12937 static void
12938 debug_method_add (struct window *w, char const *fmt, ...)
12940 void *ptr = w;
12941 char *method = w->desired_matrix->method;
12942 int len = strlen (method);
12943 int size = sizeof w->desired_matrix->method;
12944 int remaining = size - len - 1;
12945 va_list ap;
12947 if (len && remaining)
12949 method[len] = '|';
12950 --remaining, ++len;
12953 va_start (ap, fmt);
12954 vsnprintf (method + len, remaining + 1, fmt, ap);
12955 va_end (ap);
12957 if (trace_redisplay_p)
12958 fprintf (stderr, "%p (%s): %s\n",
12959 ptr,
12960 ((BUFFERP (w->contents)
12961 && STRINGP (BVAR (XBUFFER (w->contents), name)))
12962 ? SSDATA (BVAR (XBUFFER (w->contents), name))
12963 : "no buffer"),
12964 method + len);
12967 #endif /* GLYPH_DEBUG */
12970 /* Value is non-zero if all changes in window W, which displays
12971 current_buffer, are in the text between START and END. START is a
12972 buffer position, END is given as a distance from Z. Used in
12973 redisplay_internal for display optimization. */
12975 static int
12976 text_outside_line_unchanged_p (struct window *w,
12977 ptrdiff_t start, ptrdiff_t end)
12979 int unchanged_p = 1;
12981 /* If text or overlays have changed, see where. */
12982 if (window_outdated (w))
12984 /* Gap in the line? */
12985 if (GPT < start || Z - GPT < end)
12986 unchanged_p = 0;
12988 /* Changes start in front of the line, or end after it? */
12989 if (unchanged_p
12990 && (BEG_UNCHANGED < start - 1
12991 || END_UNCHANGED < end))
12992 unchanged_p = 0;
12994 /* If selective display, can't optimize if changes start at the
12995 beginning of the line. */
12996 if (unchanged_p
12997 && INTEGERP (BVAR (current_buffer, selective_display))
12998 && XINT (BVAR (current_buffer, selective_display)) > 0
12999 && (BEG_UNCHANGED < start || GPT <= start))
13000 unchanged_p = 0;
13002 /* If there are overlays at the start or end of the line, these
13003 may have overlay strings with newlines in them. A change at
13004 START, for instance, may actually concern the display of such
13005 overlay strings as well, and they are displayed on different
13006 lines. So, quickly rule out this case. (For the future, it
13007 might be desirable to implement something more telling than
13008 just BEG/END_UNCHANGED.) */
13009 if (unchanged_p)
13011 if (BEG + BEG_UNCHANGED == start
13012 && overlay_touches_p (start))
13013 unchanged_p = 0;
13014 if (END_UNCHANGED == end
13015 && overlay_touches_p (Z - end))
13016 unchanged_p = 0;
13019 /* Under bidi reordering, adding or deleting a character in the
13020 beginning of a paragraph, before the first strong directional
13021 character, can change the base direction of the paragraph (unless
13022 the buffer specifies a fixed paragraph direction), which will
13023 require to redisplay the whole paragraph. It might be worthwhile
13024 to find the paragraph limits and widen the range of redisplayed
13025 lines to that, but for now just give up this optimization. */
13026 if (!NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering))
13027 && NILP (BVAR (XBUFFER (w->contents), bidi_paragraph_direction)))
13028 unchanged_p = 0;
13031 return unchanged_p;
13035 /* Do a frame update, taking possible shortcuts into account. This is
13036 the main external entry point for redisplay.
13038 If the last redisplay displayed an echo area message and that message
13039 is no longer requested, we clear the echo area or bring back the
13040 mini-buffer if that is in use. */
13042 void
13043 redisplay (void)
13045 redisplay_internal ();
13049 static Lisp_Object
13050 overlay_arrow_string_or_property (Lisp_Object var)
13052 Lisp_Object val;
13054 if (val = Fget (var, Qoverlay_arrow_string), STRINGP (val))
13055 return val;
13057 return Voverlay_arrow_string;
13060 /* Return 1 if there are any overlay-arrows in current_buffer. */
13061 static int
13062 overlay_arrow_in_current_buffer_p (void)
13064 Lisp_Object vlist;
13066 for (vlist = Voverlay_arrow_variable_list;
13067 CONSP (vlist);
13068 vlist = XCDR (vlist))
13070 Lisp_Object var = XCAR (vlist);
13071 Lisp_Object val;
13073 if (!SYMBOLP (var))
13074 continue;
13075 val = find_symbol_value (var);
13076 if (MARKERP (val)
13077 && current_buffer == XMARKER (val)->buffer)
13078 return 1;
13080 return 0;
13084 /* Return 1 if any overlay_arrows have moved or overlay-arrow-string
13085 has changed. */
13087 static int
13088 overlay_arrows_changed_p (void)
13090 Lisp_Object vlist;
13092 for (vlist = Voverlay_arrow_variable_list;
13093 CONSP (vlist);
13094 vlist = XCDR (vlist))
13096 Lisp_Object var = XCAR (vlist);
13097 Lisp_Object val, pstr;
13099 if (!SYMBOLP (var))
13100 continue;
13101 val = find_symbol_value (var);
13102 if (!MARKERP (val))
13103 continue;
13104 if (! EQ (COERCE_MARKER (val),
13105 Fget (var, Qlast_arrow_position))
13106 || ! (pstr = overlay_arrow_string_or_property (var),
13107 EQ (pstr, Fget (var, Qlast_arrow_string))))
13108 return 1;
13110 return 0;
13113 /* Mark overlay arrows to be updated on next redisplay. */
13115 static void
13116 update_overlay_arrows (int up_to_date)
13118 Lisp_Object vlist;
13120 for (vlist = Voverlay_arrow_variable_list;
13121 CONSP (vlist);
13122 vlist = XCDR (vlist))
13124 Lisp_Object var = XCAR (vlist);
13126 if (!SYMBOLP (var))
13127 continue;
13129 if (up_to_date > 0)
13131 Lisp_Object val = find_symbol_value (var);
13132 Fput (var, Qlast_arrow_position,
13133 COERCE_MARKER (val));
13134 Fput (var, Qlast_arrow_string,
13135 overlay_arrow_string_or_property (var));
13137 else if (up_to_date < 0
13138 || !NILP (Fget (var, Qlast_arrow_position)))
13140 Fput (var, Qlast_arrow_position, Qt);
13141 Fput (var, Qlast_arrow_string, Qt);
13147 /* Return overlay arrow string to display at row.
13148 Return integer (bitmap number) for arrow bitmap in left fringe.
13149 Return nil if no overlay arrow. */
13151 static Lisp_Object
13152 overlay_arrow_at_row (struct it *it, struct glyph_row *row)
13154 Lisp_Object vlist;
13156 for (vlist = Voverlay_arrow_variable_list;
13157 CONSP (vlist);
13158 vlist = XCDR (vlist))
13160 Lisp_Object var = XCAR (vlist);
13161 Lisp_Object val;
13163 if (!SYMBOLP (var))
13164 continue;
13166 val = find_symbol_value (var);
13168 if (MARKERP (val)
13169 && current_buffer == XMARKER (val)->buffer
13170 && (MATRIX_ROW_START_CHARPOS (row) == marker_position (val)))
13172 if (FRAME_WINDOW_P (it->f)
13173 /* FIXME: if ROW->reversed_p is set, this should test
13174 the right fringe, not the left one. */
13175 && WINDOW_LEFT_FRINGE_WIDTH (it->w) > 0)
13177 #ifdef HAVE_WINDOW_SYSTEM
13178 if (val = Fget (var, Qoverlay_arrow_bitmap), SYMBOLP (val))
13180 int fringe_bitmap;
13181 if ((fringe_bitmap = lookup_fringe_bitmap (val)) != 0)
13182 return make_number (fringe_bitmap);
13184 #endif
13185 return make_number (-1); /* Use default arrow bitmap. */
13187 return overlay_arrow_string_or_property (var);
13191 return Qnil;
13194 /* Return 1 if point moved out of or into a composition. Otherwise
13195 return 0. PREV_BUF and PREV_PT are the last point buffer and
13196 position. BUF and PT are the current point buffer and position. */
13198 static int
13199 check_point_in_composition (struct buffer *prev_buf, ptrdiff_t prev_pt,
13200 struct buffer *buf, ptrdiff_t pt)
13202 ptrdiff_t start, end;
13203 Lisp_Object prop;
13204 Lisp_Object buffer;
13206 XSETBUFFER (buffer, buf);
13207 /* Check a composition at the last point if point moved within the
13208 same buffer. */
13209 if (prev_buf == buf)
13211 if (prev_pt == pt)
13212 /* Point didn't move. */
13213 return 0;
13215 if (prev_pt > BUF_BEGV (buf) && prev_pt < BUF_ZV (buf)
13216 && find_composition (prev_pt, -1, &start, &end, &prop, buffer)
13217 && composition_valid_p (start, end, prop)
13218 && start < prev_pt && end > prev_pt)
13219 /* The last point was within the composition. Return 1 iff
13220 point moved out of the composition. */
13221 return (pt <= start || pt >= end);
13224 /* Check a composition at the current point. */
13225 return (pt > BUF_BEGV (buf) && pt < BUF_ZV (buf)
13226 && find_composition (pt, -1, &start, &end, &prop, buffer)
13227 && composition_valid_p (start, end, prop)
13228 && start < pt && end > pt);
13231 /* Reconsider the clip changes of buffer which is displayed in W. */
13233 static void
13234 reconsider_clip_changes (struct window *w)
13236 struct buffer *b = XBUFFER (w->contents);
13238 if (b->clip_changed
13239 && w->window_end_valid
13240 && w->current_matrix->buffer == b
13241 && w->current_matrix->zv == BUF_ZV (b)
13242 && w->current_matrix->begv == BUF_BEGV (b))
13243 b->clip_changed = 0;
13245 /* If display wasn't paused, and W is not a tool bar window, see if
13246 point has been moved into or out of a composition. In that case,
13247 we set b->clip_changed to 1 to force updating the screen. If
13248 b->clip_changed has already been set to 1, we can skip this
13249 check. */
13250 if (!b->clip_changed && w->window_end_valid)
13252 ptrdiff_t pt = (w == XWINDOW (selected_window)
13253 ? PT : marker_position (w->pointm));
13255 if ((w->current_matrix->buffer != b || pt != w->last_point)
13256 && check_point_in_composition (w->current_matrix->buffer,
13257 w->last_point, b, pt))
13258 b->clip_changed = 1;
13262 static void
13263 propagate_buffer_redisplay (void)
13264 { /* Resetting b->text->redisplay is problematic!
13265 We can't just reset it in the case that some window that displays
13266 it has not been redisplayed; and such a window can stay
13267 unredisplayed for a long time if it's currently invisible.
13268 But we do want to reset it at the end of redisplay otherwise
13269 its displayed windows will keep being redisplayed over and over
13270 again.
13271 So we copy all b->text->redisplay flags up to their windows here,
13272 such that mark_window_display_accurate can safely reset
13273 b->text->redisplay. */
13274 Lisp_Object ws = window_list ();
13275 for (; CONSP (ws); ws = XCDR (ws))
13277 struct window *thisw = XWINDOW (XCAR (ws));
13278 struct buffer *thisb = XBUFFER (thisw->contents);
13279 if (thisb->text->redisplay)
13280 thisw->redisplay = true;
13284 #define STOP_POLLING \
13285 do { if (! polling_stopped_here) stop_polling (); \
13286 polling_stopped_here = 1; } while (0)
13288 #define RESUME_POLLING \
13289 do { if (polling_stopped_here) start_polling (); \
13290 polling_stopped_here = 0; } while (0)
13293 /* Perhaps in the future avoid recentering windows if it
13294 is not necessary; currently that causes some problems. */
13296 static void
13297 redisplay_internal (void)
13299 struct window *w = XWINDOW (selected_window);
13300 struct window *sw;
13301 struct frame *fr;
13302 int pending;
13303 bool must_finish = 0, match_p;
13304 struct text_pos tlbufpos, tlendpos;
13305 int number_of_visible_frames;
13306 ptrdiff_t count;
13307 struct frame *sf;
13308 int polling_stopped_here = 0;
13309 Lisp_Object tail, frame;
13311 /* True means redisplay has to consider all windows on all
13312 frames. False, only selected_window is considered. */
13313 bool consider_all_windows_p;
13315 /* True means redisplay has to redisplay the miniwindow. */
13316 bool update_miniwindow_p = false;
13318 TRACE ((stderr, "redisplay_internal %d\n", redisplaying_p));
13320 /* No redisplay if running in batch mode or frame is not yet fully
13321 initialized, or redisplay is explicitly turned off by setting
13322 Vinhibit_redisplay. */
13323 if (FRAME_INITIAL_P (SELECTED_FRAME ())
13324 || !NILP (Vinhibit_redisplay))
13325 return;
13327 /* Don't examine these until after testing Vinhibit_redisplay.
13328 When Emacs is shutting down, perhaps because its connection to
13329 X has dropped, we should not look at them at all. */
13330 fr = XFRAME (w->frame);
13331 sf = SELECTED_FRAME ();
13333 if (!fr->glyphs_initialized_p)
13334 return;
13336 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS)
13337 if (popup_activated ())
13338 return;
13339 #endif
13341 /* I don't think this happens but let's be paranoid. */
13342 if (redisplaying_p)
13343 return;
13345 /* Record a function that clears redisplaying_p
13346 when we leave this function. */
13347 count = SPECPDL_INDEX ();
13348 record_unwind_protect_void (unwind_redisplay);
13349 redisplaying_p = 1;
13350 specbind (Qinhibit_free_realized_faces, Qnil);
13352 /* Record this function, so it appears on the profiler's backtraces. */
13353 record_in_backtrace (Qredisplay_internal, &Qnil, 0);
13355 FOR_EACH_FRAME (tail, frame)
13356 XFRAME (frame)->already_hscrolled_p = 0;
13358 retry:
13359 /* Remember the currently selected window. */
13360 sw = w;
13362 pending = 0;
13363 last_escape_glyph_frame = NULL;
13364 last_escape_glyph_face_id = (1 << FACE_ID_BITS);
13365 last_glyphless_glyph_frame = NULL;
13366 last_glyphless_glyph_face_id = (1 << FACE_ID_BITS);
13368 /* If face_change_count is non-zero, init_iterator will free all
13369 realized faces, which includes the faces referenced from current
13370 matrices. So, we can't reuse current matrices in this case. */
13371 if (face_change_count)
13372 windows_or_buffers_changed = 47;
13374 if ((FRAME_TERMCAP_P (sf) || FRAME_MSDOS_P (sf))
13375 && FRAME_TTY (sf)->previous_frame != sf)
13377 /* Since frames on a single ASCII terminal share the same
13378 display area, displaying a different frame means redisplay
13379 the whole thing. */
13380 SET_FRAME_GARBAGED (sf);
13381 #ifndef DOS_NT
13382 set_tty_color_mode (FRAME_TTY (sf), sf);
13383 #endif
13384 FRAME_TTY (sf)->previous_frame = sf;
13387 /* Set the visible flags for all frames. Do this before checking for
13388 resized or garbaged frames; they want to know if their frames are
13389 visible. See the comment in frame.h for FRAME_SAMPLE_VISIBILITY. */
13390 number_of_visible_frames = 0;
13392 FOR_EACH_FRAME (tail, frame)
13394 struct frame *f = XFRAME (frame);
13396 if (FRAME_VISIBLE_P (f))
13398 ++number_of_visible_frames;
13399 /* Adjust matrices for visible frames only. */
13400 if (f->fonts_changed)
13402 adjust_frame_glyphs (f);
13403 f->fonts_changed = 0;
13405 /* If cursor type has been changed on the frame
13406 other than selected, consider all frames. */
13407 if (f != sf && f->cursor_type_changed)
13408 update_mode_lines = 31;
13410 clear_desired_matrices (f);
13413 /* Notice any pending interrupt request to change frame size. */
13414 do_pending_window_change (1);
13416 /* do_pending_window_change could change the selected_window due to
13417 frame resizing which makes the selected window too small. */
13418 if (WINDOWP (selected_window) && (w = XWINDOW (selected_window)) != sw)
13419 sw = w;
13421 /* Clear frames marked as garbaged. */
13422 clear_garbaged_frames ();
13424 /* Build menubar and tool-bar items. */
13425 if (NILP (Vmemory_full))
13426 prepare_menu_bars ();
13428 reconsider_clip_changes (w);
13430 /* In most cases selected window displays current buffer. */
13431 match_p = XBUFFER (w->contents) == current_buffer;
13432 if (match_p)
13434 /* Detect case that we need to write or remove a star in the mode line. */
13435 if ((SAVE_MODIFF < MODIFF) != w->last_had_star)
13436 w->update_mode_line = 1;
13438 if (mode_line_update_needed (w))
13439 w->update_mode_line = 1;
13442 /* Normally the message* functions will have already displayed and
13443 updated the echo area, but the frame may have been trashed, or
13444 the update may have been preempted, so display the echo area
13445 again here. Checking message_cleared_p captures the case that
13446 the echo area should be cleared. */
13447 if ((!NILP (echo_area_buffer[0]) && !display_last_displayed_message_p)
13448 || (!NILP (echo_area_buffer[1]) && display_last_displayed_message_p)
13449 || (message_cleared_p
13450 && minibuf_level == 0
13451 /* If the mini-window is currently selected, this means the
13452 echo-area doesn't show through. */
13453 && !MINI_WINDOW_P (XWINDOW (selected_window))))
13455 int window_height_changed_p = echo_area_display (0);
13457 if (message_cleared_p)
13458 update_miniwindow_p = true;
13460 must_finish = 1;
13462 /* If we don't display the current message, don't clear the
13463 message_cleared_p flag, because, if we did, we wouldn't clear
13464 the echo area in the next redisplay which doesn't preserve
13465 the echo area. */
13466 if (!display_last_displayed_message_p)
13467 message_cleared_p = 0;
13469 if (window_height_changed_p)
13471 windows_or_buffers_changed = 50;
13473 /* If window configuration was changed, frames may have been
13474 marked garbaged. Clear them or we will experience
13475 surprises wrt scrolling. */
13476 clear_garbaged_frames ();
13479 else if (EQ (selected_window, minibuf_window)
13480 && (current_buffer->clip_changed || window_outdated (w))
13481 && resize_mini_window (w, 0))
13483 /* Resized active mini-window to fit the size of what it is
13484 showing if its contents might have changed. */
13485 must_finish = 1;
13487 /* If window configuration was changed, frames may have been
13488 marked garbaged. Clear them or we will experience
13489 surprises wrt scrolling. */
13490 clear_garbaged_frames ();
13493 if (windows_or_buffers_changed && !update_mode_lines)
13494 /* Code that sets windows_or_buffers_changed doesn't distinguish whether
13495 only the windows's contents needs to be refreshed, or whether the
13496 mode-lines also need a refresh. */
13497 update_mode_lines = (windows_or_buffers_changed == REDISPLAY_SOME
13498 ? REDISPLAY_SOME : 32);
13500 /* If specs for an arrow have changed, do thorough redisplay
13501 to ensure we remove any arrow that should no longer exist. */
13502 if (overlay_arrows_changed_p ())
13503 /* Apparently, this is the only case where we update other windows,
13504 without updating other mode-lines. */
13505 windows_or_buffers_changed = 49;
13507 consider_all_windows_p = (update_mode_lines
13508 || windows_or_buffers_changed);
13510 #define AINC(a,i) \
13511 if (VECTORP (a) && i >= 0 && i < ASIZE (a) && INTEGERP (AREF (a, i))) \
13512 ASET (a, i, make_number (1 + XINT (AREF (a, i))))
13514 AINC (Vredisplay__all_windows_cause, windows_or_buffers_changed);
13515 AINC (Vredisplay__mode_lines_cause, update_mode_lines);
13517 /* Optimize the case that only the line containing the cursor in the
13518 selected window has changed. Variables starting with this_ are
13519 set in display_line and record information about the line
13520 containing the cursor. */
13521 tlbufpos = this_line_start_pos;
13522 tlendpos = this_line_end_pos;
13523 if (!consider_all_windows_p
13524 && CHARPOS (tlbufpos) > 0
13525 && !w->update_mode_line
13526 && !current_buffer->clip_changed
13527 && !current_buffer->prevent_redisplay_optimizations_p
13528 && FRAME_VISIBLE_P (XFRAME (w->frame))
13529 && !FRAME_OBSCURED_P (XFRAME (w->frame))
13530 && !XFRAME (w->frame)->cursor_type_changed
13531 /* Make sure recorded data applies to current buffer, etc. */
13532 && this_line_buffer == current_buffer
13533 && match_p
13534 && !w->force_start
13535 && !w->optional_new_start
13536 /* Point must be on the line that we have info recorded about. */
13537 && PT >= CHARPOS (tlbufpos)
13538 && PT <= Z - CHARPOS (tlendpos)
13539 /* All text outside that line, including its final newline,
13540 must be unchanged. */
13541 && text_outside_line_unchanged_p (w, CHARPOS (tlbufpos),
13542 CHARPOS (tlendpos)))
13544 if (CHARPOS (tlbufpos) > BEGV
13545 && FETCH_BYTE (BYTEPOS (tlbufpos) - 1) != '\n'
13546 && (CHARPOS (tlbufpos) == ZV
13547 || FETCH_BYTE (BYTEPOS (tlbufpos)) == '\n'))
13548 /* Former continuation line has disappeared by becoming empty. */
13549 goto cancel;
13550 else if (window_outdated (w) || MINI_WINDOW_P (w))
13552 /* We have to handle the case of continuation around a
13553 wide-column character (see the comment in indent.c around
13554 line 1340).
13556 For instance, in the following case:
13558 -------- Insert --------
13559 K_A_N_\\ `a' K_A_N_a\ `X_' are wide-column chars.
13560 J_I_ ==> J_I_ `^^' are cursors.
13561 ^^ ^^
13562 -------- --------
13564 As we have to redraw the line above, we cannot use this
13565 optimization. */
13567 struct it it;
13568 int line_height_before = this_line_pixel_height;
13570 /* Note that start_display will handle the case that the
13571 line starting at tlbufpos is a continuation line. */
13572 start_display (&it, w, tlbufpos);
13574 /* Implementation note: It this still necessary? */
13575 if (it.current_x != this_line_start_x)
13576 goto cancel;
13578 TRACE ((stderr, "trying display optimization 1\n"));
13579 w->cursor.vpos = -1;
13580 overlay_arrow_seen = 0;
13581 it.vpos = this_line_vpos;
13582 it.current_y = this_line_y;
13583 it.glyph_row = MATRIX_ROW (w->desired_matrix, this_line_vpos);
13584 display_line (&it);
13586 /* If line contains point, is not continued,
13587 and ends at same distance from eob as before, we win. */
13588 if (w->cursor.vpos >= 0
13589 /* Line is not continued, otherwise this_line_start_pos
13590 would have been set to 0 in display_line. */
13591 && CHARPOS (this_line_start_pos)
13592 /* Line ends as before. */
13593 && CHARPOS (this_line_end_pos) == CHARPOS (tlendpos)
13594 /* Line has same height as before. Otherwise other lines
13595 would have to be shifted up or down. */
13596 && this_line_pixel_height == line_height_before)
13598 /* If this is not the window's last line, we must adjust
13599 the charstarts of the lines below. */
13600 if (it.current_y < it.last_visible_y)
13602 struct glyph_row *row
13603 = MATRIX_ROW (w->current_matrix, this_line_vpos + 1);
13604 ptrdiff_t delta, delta_bytes;
13606 /* We used to distinguish between two cases here,
13607 conditioned by Z - CHARPOS (tlendpos) == ZV, for
13608 when the line ends in a newline or the end of the
13609 buffer's accessible portion. But both cases did
13610 the same, so they were collapsed. */
13611 delta = (Z
13612 - CHARPOS (tlendpos)
13613 - MATRIX_ROW_START_CHARPOS (row));
13614 delta_bytes = (Z_BYTE
13615 - BYTEPOS (tlendpos)
13616 - MATRIX_ROW_START_BYTEPOS (row));
13618 increment_matrix_positions (w->current_matrix,
13619 this_line_vpos + 1,
13620 w->current_matrix->nrows,
13621 delta, delta_bytes);
13624 /* If this row displays text now but previously didn't,
13625 or vice versa, w->window_end_vpos may have to be
13626 adjusted. */
13627 if (MATRIX_ROW_DISPLAYS_TEXT_P (it.glyph_row - 1))
13629 if (w->window_end_vpos < this_line_vpos)
13630 w->window_end_vpos = this_line_vpos;
13632 else if (w->window_end_vpos == this_line_vpos
13633 && this_line_vpos > 0)
13634 w->window_end_vpos = this_line_vpos - 1;
13635 w->window_end_valid = 0;
13637 /* Update hint: No need to try to scroll in update_window. */
13638 w->desired_matrix->no_scrolling_p = 1;
13640 #ifdef GLYPH_DEBUG
13641 *w->desired_matrix->method = 0;
13642 debug_method_add (w, "optimization 1");
13643 #endif
13644 #ifdef HAVE_WINDOW_SYSTEM
13645 update_window_fringes (w, 0);
13646 #endif
13647 goto update;
13649 else
13650 goto cancel;
13652 else if (/* Cursor position hasn't changed. */
13653 PT == w->last_point
13654 /* Make sure the cursor was last displayed
13655 in this window. Otherwise we have to reposition it. */
13657 /* PXW: Must be converted to pixels, probably. */
13658 && 0 <= w->cursor.vpos
13659 && w->cursor.vpos < WINDOW_TOTAL_LINES (w))
13661 if (!must_finish)
13663 do_pending_window_change (1);
13664 /* If selected_window changed, redisplay again. */
13665 if (WINDOWP (selected_window)
13666 && (w = XWINDOW (selected_window)) != sw)
13667 goto retry;
13669 /* We used to always goto end_of_redisplay here, but this
13670 isn't enough if we have a blinking cursor. */
13671 if (w->cursor_off_p == w->last_cursor_off_p)
13672 goto end_of_redisplay;
13674 goto update;
13676 /* If highlighting the region, or if the cursor is in the echo area,
13677 then we can't just move the cursor. */
13678 else if (NILP (Vshow_trailing_whitespace)
13679 && !cursor_in_echo_area)
13681 struct it it;
13682 struct glyph_row *row;
13684 /* Skip from tlbufpos to PT and see where it is. Note that
13685 PT may be in invisible text. If so, we will end at the
13686 next visible position. */
13687 init_iterator (&it, w, CHARPOS (tlbufpos), BYTEPOS (tlbufpos),
13688 NULL, DEFAULT_FACE_ID);
13689 it.current_x = this_line_start_x;
13690 it.current_y = this_line_y;
13691 it.vpos = this_line_vpos;
13693 /* The call to move_it_to stops in front of PT, but
13694 moves over before-strings. */
13695 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
13697 if (it.vpos == this_line_vpos
13698 && (row = MATRIX_ROW (w->current_matrix, this_line_vpos),
13699 row->enabled_p))
13701 eassert (this_line_vpos == it.vpos);
13702 eassert (this_line_y == it.current_y);
13703 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
13704 #ifdef GLYPH_DEBUG
13705 *w->desired_matrix->method = 0;
13706 debug_method_add (w, "optimization 3");
13707 #endif
13708 goto update;
13710 else
13711 goto cancel;
13714 cancel:
13715 /* Text changed drastically or point moved off of line. */
13716 SET_MATRIX_ROW_ENABLED_P (w->desired_matrix, this_line_vpos, false);
13719 CHARPOS (this_line_start_pos) = 0;
13720 ++clear_face_cache_count;
13721 #ifdef HAVE_WINDOW_SYSTEM
13722 ++clear_image_cache_count;
13723 #endif
13725 /* Build desired matrices, and update the display. If
13726 consider_all_windows_p is non-zero, do it for all windows on all
13727 frames. Otherwise do it for selected_window, only. */
13729 if (consider_all_windows_p)
13731 FOR_EACH_FRAME (tail, frame)
13732 XFRAME (frame)->updated_p = 0;
13734 propagate_buffer_redisplay ();
13736 FOR_EACH_FRAME (tail, frame)
13738 struct frame *f = XFRAME (frame);
13740 /* We don't have to do anything for unselected terminal
13741 frames. */
13742 if ((FRAME_TERMCAP_P (f) || FRAME_MSDOS_P (f))
13743 && !EQ (FRAME_TTY (f)->top_frame, frame))
13744 continue;
13746 retry_frame:
13748 if (FRAME_WINDOW_P (f) || FRAME_TERMCAP_P (f) || f == sf)
13750 bool gcscrollbars
13751 /* Only GC scrollbars when we redisplay the whole frame. */
13752 = f->redisplay || !REDISPLAY_SOME_P ();
13753 /* Mark all the scroll bars to be removed; we'll redeem
13754 the ones we want when we redisplay their windows. */
13755 if (gcscrollbars && FRAME_TERMINAL (f)->condemn_scroll_bars_hook)
13756 FRAME_TERMINAL (f)->condemn_scroll_bars_hook (f);
13758 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
13759 redisplay_windows (FRAME_ROOT_WINDOW (f));
13760 /* Remember that the invisible frames need to be redisplayed next
13761 time they're visible. */
13762 else if (!REDISPLAY_SOME_P ())
13763 f->redisplay = true;
13765 /* The X error handler may have deleted that frame. */
13766 if (!FRAME_LIVE_P (f))
13767 continue;
13769 /* Any scroll bars which redisplay_windows should have
13770 nuked should now go away. */
13771 if (gcscrollbars && FRAME_TERMINAL (f)->judge_scroll_bars_hook)
13772 FRAME_TERMINAL (f)->judge_scroll_bars_hook (f);
13774 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
13776 /* If fonts changed on visible frame, display again. */
13777 if (f->fonts_changed)
13779 adjust_frame_glyphs (f);
13780 f->fonts_changed = 0;
13781 goto retry_frame;
13784 /* See if we have to hscroll. */
13785 if (!f->already_hscrolled_p)
13787 f->already_hscrolled_p = 1;
13788 if (hscroll_windows (f->root_window))
13789 goto retry_frame;
13792 /* Prevent various kinds of signals during display
13793 update. stdio is not robust about handling
13794 signals, which can cause an apparent I/O error. */
13795 if (interrupt_input)
13796 unrequest_sigio ();
13797 STOP_POLLING;
13799 pending |= update_frame (f, 0, 0);
13800 f->cursor_type_changed = 0;
13801 f->updated_p = 1;
13806 eassert (EQ (XFRAME (selected_frame)->selected_window, selected_window));
13808 if (!pending)
13810 /* Do the mark_window_display_accurate after all windows have
13811 been redisplayed because this call resets flags in buffers
13812 which are needed for proper redisplay. */
13813 FOR_EACH_FRAME (tail, frame)
13815 struct frame *f = XFRAME (frame);
13816 if (f->updated_p)
13818 f->redisplay = false;
13819 mark_window_display_accurate (f->root_window, 1);
13820 if (FRAME_TERMINAL (f)->frame_up_to_date_hook)
13821 FRAME_TERMINAL (f)->frame_up_to_date_hook (f);
13826 else if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
13828 Lisp_Object mini_window = FRAME_MINIBUF_WINDOW (sf);
13829 struct frame *mini_frame;
13831 displayed_buffer = XBUFFER (XWINDOW (selected_window)->contents);
13832 /* Use list_of_error, not Qerror, so that
13833 we catch only errors and don't run the debugger. */
13834 internal_condition_case_1 (redisplay_window_1, selected_window,
13835 list_of_error,
13836 redisplay_window_error);
13837 if (update_miniwindow_p)
13838 internal_condition_case_1 (redisplay_window_1, mini_window,
13839 list_of_error,
13840 redisplay_window_error);
13842 /* Compare desired and current matrices, perform output. */
13844 update:
13845 /* If fonts changed, display again. */
13846 if (sf->fonts_changed)
13847 goto retry;
13849 /* Prevent various kinds of signals during display update.
13850 stdio is not robust about handling signals,
13851 which can cause an apparent I/O error. */
13852 if (interrupt_input)
13853 unrequest_sigio ();
13854 STOP_POLLING;
13856 if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
13858 if (hscroll_windows (selected_window))
13859 goto retry;
13861 XWINDOW (selected_window)->must_be_updated_p = true;
13862 pending = update_frame (sf, 0, 0);
13863 sf->cursor_type_changed = 0;
13866 /* We may have called echo_area_display at the top of this
13867 function. If the echo area is on another frame, that may
13868 have put text on a frame other than the selected one, so the
13869 above call to update_frame would not have caught it. Catch
13870 it here. */
13871 mini_window = FRAME_MINIBUF_WINDOW (sf);
13872 mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
13874 if (mini_frame != sf && FRAME_WINDOW_P (mini_frame))
13876 XWINDOW (mini_window)->must_be_updated_p = true;
13877 pending |= update_frame (mini_frame, 0, 0);
13878 mini_frame->cursor_type_changed = 0;
13879 if (!pending && hscroll_windows (mini_window))
13880 goto retry;
13884 /* If display was paused because of pending input, make sure we do a
13885 thorough update the next time. */
13886 if (pending)
13888 /* Prevent the optimization at the beginning of
13889 redisplay_internal that tries a single-line update of the
13890 line containing the cursor in the selected window. */
13891 CHARPOS (this_line_start_pos) = 0;
13893 /* Let the overlay arrow be updated the next time. */
13894 update_overlay_arrows (0);
13896 /* If we pause after scrolling, some rows in the current
13897 matrices of some windows are not valid. */
13898 if (!WINDOW_FULL_WIDTH_P (w)
13899 && !FRAME_WINDOW_P (XFRAME (w->frame)))
13900 update_mode_lines = 36;
13902 else
13904 if (!consider_all_windows_p)
13906 /* This has already been done above if
13907 consider_all_windows_p is set. */
13908 if (XBUFFER (w->contents)->text->redisplay
13909 && buffer_window_count (XBUFFER (w->contents)) > 1)
13910 /* This can happen if b->text->redisplay was set during
13911 jit-lock. */
13912 propagate_buffer_redisplay ();
13913 mark_window_display_accurate_1 (w, 1);
13915 /* Say overlay arrows are up to date. */
13916 update_overlay_arrows (1);
13918 if (FRAME_TERMINAL (sf)->frame_up_to_date_hook != 0)
13919 FRAME_TERMINAL (sf)->frame_up_to_date_hook (sf);
13922 update_mode_lines = 0;
13923 windows_or_buffers_changed = 0;
13926 /* Start SIGIO interrupts coming again. Having them off during the
13927 code above makes it less likely one will discard output, but not
13928 impossible, since there might be stuff in the system buffer here.
13929 But it is much hairier to try to do anything about that. */
13930 if (interrupt_input)
13931 request_sigio ();
13932 RESUME_POLLING;
13934 /* If a frame has become visible which was not before, redisplay
13935 again, so that we display it. Expose events for such a frame
13936 (which it gets when becoming visible) don't call the parts of
13937 redisplay constructing glyphs, so simply exposing a frame won't
13938 display anything in this case. So, we have to display these
13939 frames here explicitly. */
13940 if (!pending)
13942 int new_count = 0;
13944 FOR_EACH_FRAME (tail, frame)
13946 if (XFRAME (frame)->visible)
13947 new_count++;
13950 if (new_count != number_of_visible_frames)
13951 windows_or_buffers_changed = 52;
13954 /* Change frame size now if a change is pending. */
13955 do_pending_window_change (1);
13957 /* If we just did a pending size change, or have additional
13958 visible frames, or selected_window changed, redisplay again. */
13959 if ((windows_or_buffers_changed && !pending)
13960 || (WINDOWP (selected_window) && (w = XWINDOW (selected_window)) != sw))
13961 goto retry;
13963 /* Clear the face and image caches.
13965 We used to do this only if consider_all_windows_p. But the cache
13966 needs to be cleared if a timer creates images in the current
13967 buffer (e.g. the test case in Bug#6230). */
13969 if (clear_face_cache_count > CLEAR_FACE_CACHE_COUNT)
13971 clear_face_cache (0);
13972 clear_face_cache_count = 0;
13975 #ifdef HAVE_WINDOW_SYSTEM
13976 if (clear_image_cache_count > CLEAR_IMAGE_CACHE_COUNT)
13978 clear_image_caches (Qnil);
13979 clear_image_cache_count = 0;
13981 #endif /* HAVE_WINDOW_SYSTEM */
13983 end_of_redisplay:
13984 if (interrupt_input && interrupts_deferred)
13985 request_sigio ();
13987 unbind_to (count, Qnil);
13988 RESUME_POLLING;
13992 /* Redisplay, but leave alone any recent echo area message unless
13993 another message has been requested in its place.
13995 This is useful in situations where you need to redisplay but no
13996 user action has occurred, making it inappropriate for the message
13997 area to be cleared. See tracking_off and
13998 wait_reading_process_output for examples of these situations.
14000 FROM_WHERE is an integer saying from where this function was
14001 called. This is useful for debugging. */
14003 void
14004 redisplay_preserve_echo_area (int from_where)
14006 TRACE ((stderr, "redisplay_preserve_echo_area (%d)\n", from_where));
14008 if (!NILP (echo_area_buffer[1]))
14010 /* We have a previously displayed message, but no current
14011 message. Redisplay the previous message. */
14012 display_last_displayed_message_p = 1;
14013 redisplay_internal ();
14014 display_last_displayed_message_p = 0;
14016 else
14017 redisplay_internal ();
14019 flush_frame (SELECTED_FRAME ());
14023 /* Function registered with record_unwind_protect in redisplay_internal. */
14025 static void
14026 unwind_redisplay (void)
14028 redisplaying_p = 0;
14032 /* Mark the display of leaf window W as accurate or inaccurate.
14033 If ACCURATE_P is non-zero mark display of W as accurate. If
14034 ACCURATE_P is zero, arrange for W to be redisplayed the next
14035 time redisplay_internal is called. */
14037 static void
14038 mark_window_display_accurate_1 (struct window *w, int accurate_p)
14040 struct buffer *b = XBUFFER (w->contents);
14042 w->last_modified = accurate_p ? BUF_MODIFF (b) : 0;
14043 w->last_overlay_modified = accurate_p ? BUF_OVERLAY_MODIFF (b) : 0;
14044 w->last_had_star = BUF_MODIFF (b) > BUF_SAVE_MODIFF (b);
14046 if (accurate_p)
14048 b->clip_changed = false;
14049 b->prevent_redisplay_optimizations_p = false;
14050 eassert (buffer_window_count (b) > 0);
14051 /* Resetting b->text->redisplay is problematic!
14052 In order to make it safer to do it here, redisplay_internal must
14053 have copied all b->text->redisplay to their respective windows. */
14054 b->text->redisplay = false;
14056 BUF_UNCHANGED_MODIFIED (b) = BUF_MODIFF (b);
14057 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = BUF_OVERLAY_MODIFF (b);
14058 BUF_BEG_UNCHANGED (b) = BUF_GPT (b) - BUF_BEG (b);
14059 BUF_END_UNCHANGED (b) = BUF_Z (b) - BUF_GPT (b);
14061 w->current_matrix->buffer = b;
14062 w->current_matrix->begv = BUF_BEGV (b);
14063 w->current_matrix->zv = BUF_ZV (b);
14065 w->last_cursor_vpos = w->cursor.vpos;
14066 w->last_cursor_off_p = w->cursor_off_p;
14068 if (w == XWINDOW (selected_window))
14069 w->last_point = BUF_PT (b);
14070 else
14071 w->last_point = marker_position (w->pointm);
14073 w->window_end_valid = true;
14074 w->update_mode_line = false;
14077 w->redisplay = !accurate_p;
14081 /* Mark the display of windows in the window tree rooted at WINDOW as
14082 accurate or inaccurate. If ACCURATE_P is non-zero mark display of
14083 windows as accurate. If ACCURATE_P is zero, arrange for windows to
14084 be redisplayed the next time redisplay_internal is called. */
14086 void
14087 mark_window_display_accurate (Lisp_Object window, int accurate_p)
14089 struct window *w;
14091 for (; !NILP (window); window = w->next)
14093 w = XWINDOW (window);
14094 if (WINDOWP (w->contents))
14095 mark_window_display_accurate (w->contents, accurate_p);
14096 else
14097 mark_window_display_accurate_1 (w, accurate_p);
14100 if (accurate_p)
14101 update_overlay_arrows (1);
14102 else
14103 /* Force a thorough redisplay the next time by setting
14104 last_arrow_position and last_arrow_string to t, which is
14105 unequal to any useful value of Voverlay_arrow_... */
14106 update_overlay_arrows (-1);
14110 /* Return value in display table DP (Lisp_Char_Table *) for character
14111 C. Since a display table doesn't have any parent, we don't have to
14112 follow parent. Do not call this function directly but use the
14113 macro DISP_CHAR_VECTOR. */
14115 Lisp_Object
14116 disp_char_vector (struct Lisp_Char_Table *dp, int c)
14118 Lisp_Object val;
14120 if (ASCII_CHAR_P (c))
14122 val = dp->ascii;
14123 if (SUB_CHAR_TABLE_P (val))
14124 val = XSUB_CHAR_TABLE (val)->contents[c];
14126 else
14128 Lisp_Object table;
14130 XSETCHAR_TABLE (table, dp);
14131 val = char_table_ref (table, c);
14133 if (NILP (val))
14134 val = dp->defalt;
14135 return val;
14140 /***********************************************************************
14141 Window Redisplay
14142 ***********************************************************************/
14144 /* Redisplay all leaf windows in the window tree rooted at WINDOW. */
14146 static void
14147 redisplay_windows (Lisp_Object window)
14149 while (!NILP (window))
14151 struct window *w = XWINDOW (window);
14153 if (WINDOWP (w->contents))
14154 redisplay_windows (w->contents);
14155 else if (BUFFERP (w->contents))
14157 displayed_buffer = XBUFFER (w->contents);
14158 /* Use list_of_error, not Qerror, so that
14159 we catch only errors and don't run the debugger. */
14160 internal_condition_case_1 (redisplay_window_0, window,
14161 list_of_error,
14162 redisplay_window_error);
14165 window = w->next;
14169 static Lisp_Object
14170 redisplay_window_error (Lisp_Object ignore)
14172 displayed_buffer->display_error_modiff = BUF_MODIFF (displayed_buffer);
14173 return Qnil;
14176 static Lisp_Object
14177 redisplay_window_0 (Lisp_Object window)
14179 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
14180 redisplay_window (window, false);
14181 return Qnil;
14184 static Lisp_Object
14185 redisplay_window_1 (Lisp_Object window)
14187 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
14188 redisplay_window (window, true);
14189 return Qnil;
14193 /* Set cursor position of W. PT is assumed to be displayed in ROW.
14194 DELTA and DELTA_BYTES are the numbers of characters and bytes by
14195 which positions recorded in ROW differ from current buffer
14196 positions.
14198 Return 0 if cursor is not on this row, 1 otherwise. */
14200 static int
14201 set_cursor_from_row (struct window *w, struct glyph_row *row,
14202 struct glyph_matrix *matrix,
14203 ptrdiff_t delta, ptrdiff_t delta_bytes,
14204 int dy, int dvpos)
14206 struct glyph *glyph = row->glyphs[TEXT_AREA];
14207 struct glyph *end = glyph + row->used[TEXT_AREA];
14208 struct glyph *cursor = NULL;
14209 /* The last known character position in row. */
14210 ptrdiff_t last_pos = MATRIX_ROW_START_CHARPOS (row) + delta;
14211 int x = row->x;
14212 ptrdiff_t pt_old = PT - delta;
14213 ptrdiff_t pos_before = MATRIX_ROW_START_CHARPOS (row) + delta;
14214 ptrdiff_t pos_after = MATRIX_ROW_END_CHARPOS (row) + delta;
14215 struct glyph *glyph_before = glyph - 1, *glyph_after = end;
14216 /* A glyph beyond the edge of TEXT_AREA which we should never
14217 touch. */
14218 struct glyph *glyphs_end = end;
14219 /* Non-zero means we've found a match for cursor position, but that
14220 glyph has the avoid_cursor_p flag set. */
14221 int match_with_avoid_cursor = 0;
14222 /* Non-zero means we've seen at least one glyph that came from a
14223 display string. */
14224 int string_seen = 0;
14225 /* Largest and smallest buffer positions seen so far during scan of
14226 glyph row. */
14227 ptrdiff_t bpos_max = pos_before;
14228 ptrdiff_t bpos_min = pos_after;
14229 /* Last buffer position covered by an overlay string with an integer
14230 `cursor' property. */
14231 ptrdiff_t bpos_covered = 0;
14232 /* Non-zero means the display string on which to display the cursor
14233 comes from a text property, not from an overlay. */
14234 int string_from_text_prop = 0;
14236 /* Don't even try doing anything if called for a mode-line or
14237 header-line row, since the rest of the code isn't prepared to
14238 deal with such calamities. */
14239 eassert (!row->mode_line_p);
14240 if (row->mode_line_p)
14241 return 0;
14243 /* Skip over glyphs not having an object at the start and the end of
14244 the row. These are special glyphs like truncation marks on
14245 terminal frames. */
14246 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
14248 if (!row->reversed_p)
14250 while (glyph < end
14251 && INTEGERP (glyph->object)
14252 && glyph->charpos < 0)
14254 x += glyph->pixel_width;
14255 ++glyph;
14257 while (end > glyph
14258 && INTEGERP ((end - 1)->object)
14259 /* CHARPOS is zero for blanks and stretch glyphs
14260 inserted by extend_face_to_end_of_line. */
14261 && (end - 1)->charpos <= 0)
14262 --end;
14263 glyph_before = glyph - 1;
14264 glyph_after = end;
14266 else
14268 struct glyph *g;
14270 /* If the glyph row is reversed, we need to process it from back
14271 to front, so swap the edge pointers. */
14272 glyphs_end = end = glyph - 1;
14273 glyph += row->used[TEXT_AREA] - 1;
14275 while (glyph > end + 1
14276 && INTEGERP (glyph->object)
14277 && glyph->charpos < 0)
14279 --glyph;
14280 x -= glyph->pixel_width;
14282 if (INTEGERP (glyph->object) && glyph->charpos < 0)
14283 --glyph;
14284 /* By default, in reversed rows we put the cursor on the
14285 rightmost (first in the reading order) glyph. */
14286 for (g = end + 1; g < glyph; g++)
14287 x += g->pixel_width;
14288 while (end < glyph
14289 && INTEGERP ((end + 1)->object)
14290 && (end + 1)->charpos <= 0)
14291 ++end;
14292 glyph_before = glyph + 1;
14293 glyph_after = end;
14296 else if (row->reversed_p)
14298 /* In R2L rows that don't display text, put the cursor on the
14299 rightmost glyph. Case in point: an empty last line that is
14300 part of an R2L paragraph. */
14301 cursor = end - 1;
14302 /* Avoid placing the cursor on the last glyph of the row, where
14303 on terminal frames we hold the vertical border between
14304 adjacent windows. */
14305 if (!FRAME_WINDOW_P (WINDOW_XFRAME (w))
14306 && !WINDOW_RIGHTMOST_P (w)
14307 && cursor == row->glyphs[LAST_AREA] - 1)
14308 cursor--;
14309 x = -1; /* will be computed below, at label compute_x */
14312 /* Step 1: Try to find the glyph whose character position
14313 corresponds to point. If that's not possible, find 2 glyphs
14314 whose character positions are the closest to point, one before
14315 point, the other after it. */
14316 if (!row->reversed_p)
14317 while (/* not marched to end of glyph row */
14318 glyph < end
14319 /* glyph was not inserted by redisplay for internal purposes */
14320 && !INTEGERP (glyph->object))
14322 if (BUFFERP (glyph->object))
14324 ptrdiff_t dpos = glyph->charpos - pt_old;
14326 if (glyph->charpos > bpos_max)
14327 bpos_max = glyph->charpos;
14328 if (glyph->charpos < bpos_min)
14329 bpos_min = glyph->charpos;
14330 if (!glyph->avoid_cursor_p)
14332 /* If we hit point, we've found the glyph on which to
14333 display the cursor. */
14334 if (dpos == 0)
14336 match_with_avoid_cursor = 0;
14337 break;
14339 /* See if we've found a better approximation to
14340 POS_BEFORE or to POS_AFTER. */
14341 if (0 > dpos && dpos > pos_before - pt_old)
14343 pos_before = glyph->charpos;
14344 glyph_before = glyph;
14346 else if (0 < dpos && dpos < pos_after - pt_old)
14348 pos_after = glyph->charpos;
14349 glyph_after = glyph;
14352 else if (dpos == 0)
14353 match_with_avoid_cursor = 1;
14355 else if (STRINGP (glyph->object))
14357 Lisp_Object chprop;
14358 ptrdiff_t glyph_pos = glyph->charpos;
14360 chprop = Fget_char_property (make_number (glyph_pos), Qcursor,
14361 glyph->object);
14362 if (!NILP (chprop))
14364 /* If the string came from a `display' text property,
14365 look up the buffer position of that property and
14366 use that position to update bpos_max, as if we
14367 actually saw such a position in one of the row's
14368 glyphs. This helps with supporting integer values
14369 of `cursor' property on the display string in
14370 situations where most or all of the row's buffer
14371 text is completely covered by display properties,
14372 so that no glyph with valid buffer positions is
14373 ever seen in the row. */
14374 ptrdiff_t prop_pos =
14375 string_buffer_position_lim (glyph->object, pos_before,
14376 pos_after, 0);
14378 if (prop_pos >= pos_before)
14379 bpos_max = prop_pos - 1;
14381 if (INTEGERP (chprop))
14383 bpos_covered = bpos_max + XINT (chprop);
14384 /* If the `cursor' property covers buffer positions up
14385 to and including point, we should display cursor on
14386 this glyph. Note that, if a `cursor' property on one
14387 of the string's characters has an integer value, we
14388 will break out of the loop below _before_ we get to
14389 the position match above. IOW, integer values of
14390 the `cursor' property override the "exact match for
14391 point" strategy of positioning the cursor. */
14392 /* Implementation note: bpos_max == pt_old when, e.g.,
14393 we are in an empty line, where bpos_max is set to
14394 MATRIX_ROW_START_CHARPOS, see above. */
14395 if (bpos_max <= pt_old && bpos_covered >= pt_old)
14397 cursor = glyph;
14398 break;
14402 string_seen = 1;
14404 x += glyph->pixel_width;
14405 ++glyph;
14407 else if (glyph > end) /* row is reversed */
14408 while (!INTEGERP (glyph->object))
14410 if (BUFFERP (glyph->object))
14412 ptrdiff_t dpos = glyph->charpos - pt_old;
14414 if (glyph->charpos > bpos_max)
14415 bpos_max = glyph->charpos;
14416 if (glyph->charpos < bpos_min)
14417 bpos_min = glyph->charpos;
14418 if (!glyph->avoid_cursor_p)
14420 if (dpos == 0)
14422 match_with_avoid_cursor = 0;
14423 break;
14425 if (0 > dpos && dpos > pos_before - pt_old)
14427 pos_before = glyph->charpos;
14428 glyph_before = glyph;
14430 else if (0 < dpos && dpos < pos_after - pt_old)
14432 pos_after = glyph->charpos;
14433 glyph_after = glyph;
14436 else if (dpos == 0)
14437 match_with_avoid_cursor = 1;
14439 else if (STRINGP (glyph->object))
14441 Lisp_Object chprop;
14442 ptrdiff_t glyph_pos = glyph->charpos;
14444 chprop = Fget_char_property (make_number (glyph_pos), Qcursor,
14445 glyph->object);
14446 if (!NILP (chprop))
14448 ptrdiff_t prop_pos =
14449 string_buffer_position_lim (glyph->object, pos_before,
14450 pos_after, 0);
14452 if (prop_pos >= pos_before)
14453 bpos_max = prop_pos - 1;
14455 if (INTEGERP (chprop))
14457 bpos_covered = bpos_max + XINT (chprop);
14458 /* If the `cursor' property covers buffer positions up
14459 to and including point, we should display cursor on
14460 this glyph. */
14461 if (bpos_max <= pt_old && bpos_covered >= pt_old)
14463 cursor = glyph;
14464 break;
14467 string_seen = 1;
14469 --glyph;
14470 if (glyph == glyphs_end) /* don't dereference outside TEXT_AREA */
14472 x--; /* can't use any pixel_width */
14473 break;
14475 x -= glyph->pixel_width;
14478 /* Step 2: If we didn't find an exact match for point, we need to
14479 look for a proper place to put the cursor among glyphs between
14480 GLYPH_BEFORE and GLYPH_AFTER. */
14481 if (!((row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end)
14482 && BUFFERP (glyph->object) && glyph->charpos == pt_old)
14483 && !(bpos_max < pt_old && pt_old <= bpos_covered))
14485 /* An empty line has a single glyph whose OBJECT is zero and
14486 whose CHARPOS is the position of a newline on that line.
14487 Note that on a TTY, there are more glyphs after that, which
14488 were produced by extend_face_to_end_of_line, but their
14489 CHARPOS is zero or negative. */
14490 int empty_line_p =
14491 (row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end)
14492 && INTEGERP (glyph->object) && glyph->charpos > 0
14493 /* On a TTY, continued and truncated rows also have a glyph at
14494 their end whose OBJECT is zero and whose CHARPOS is
14495 positive (the continuation and truncation glyphs), but such
14496 rows are obviously not "empty". */
14497 && !(row->continued_p || row->truncated_on_right_p);
14499 if (row->ends_in_ellipsis_p && pos_after == last_pos)
14501 ptrdiff_t ellipsis_pos;
14503 /* Scan back over the ellipsis glyphs. */
14504 if (!row->reversed_p)
14506 ellipsis_pos = (glyph - 1)->charpos;
14507 while (glyph > row->glyphs[TEXT_AREA]
14508 && (glyph - 1)->charpos == ellipsis_pos)
14509 glyph--, x -= glyph->pixel_width;
14510 /* That loop always goes one position too far, including
14511 the glyph before the ellipsis. So scan forward over
14512 that one. */
14513 x += glyph->pixel_width;
14514 glyph++;
14516 else /* row is reversed */
14518 ellipsis_pos = (glyph + 1)->charpos;
14519 while (glyph < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
14520 && (glyph + 1)->charpos == ellipsis_pos)
14521 glyph++, x += glyph->pixel_width;
14522 x -= glyph->pixel_width;
14523 glyph--;
14526 else if (match_with_avoid_cursor)
14528 cursor = glyph_after;
14529 x = -1;
14531 else if (string_seen)
14533 int incr = row->reversed_p ? -1 : +1;
14535 /* Need to find the glyph that came out of a string which is
14536 present at point. That glyph is somewhere between
14537 GLYPH_BEFORE and GLYPH_AFTER, and it came from a string
14538 positioned between POS_BEFORE and POS_AFTER in the
14539 buffer. */
14540 struct glyph *start, *stop;
14541 ptrdiff_t pos = pos_before;
14543 x = -1;
14545 /* If the row ends in a newline from a display string,
14546 reordering could have moved the glyphs belonging to the
14547 string out of the [GLYPH_BEFORE..GLYPH_AFTER] range. So
14548 in this case we extend the search to the last glyph in
14549 the row that was not inserted by redisplay. */
14550 if (row->ends_in_newline_from_string_p)
14552 glyph_after = end;
14553 pos_after = MATRIX_ROW_END_CHARPOS (row) + delta;
14556 /* GLYPH_BEFORE and GLYPH_AFTER are the glyphs that
14557 correspond to POS_BEFORE and POS_AFTER, respectively. We
14558 need START and STOP in the order that corresponds to the
14559 row's direction as given by its reversed_p flag. If the
14560 directionality of characters between POS_BEFORE and
14561 POS_AFTER is the opposite of the row's base direction,
14562 these characters will have been reordered for display,
14563 and we need to reverse START and STOP. */
14564 if (!row->reversed_p)
14566 start = min (glyph_before, glyph_after);
14567 stop = max (glyph_before, glyph_after);
14569 else
14571 start = max (glyph_before, glyph_after);
14572 stop = min (glyph_before, glyph_after);
14574 for (glyph = start + incr;
14575 row->reversed_p ? glyph > stop : glyph < stop; )
14578 /* Any glyphs that come from the buffer are here because
14579 of bidi reordering. Skip them, and only pay
14580 attention to glyphs that came from some string. */
14581 if (STRINGP (glyph->object))
14583 Lisp_Object str;
14584 ptrdiff_t tem;
14585 /* If the display property covers the newline, we
14586 need to search for it one position farther. */
14587 ptrdiff_t lim = pos_after
14588 + (pos_after == MATRIX_ROW_END_CHARPOS (row) + delta);
14590 string_from_text_prop = 0;
14591 str = glyph->object;
14592 tem = string_buffer_position_lim (str, pos, lim, 0);
14593 if (tem == 0 /* from overlay */
14594 || pos <= tem)
14596 /* If the string from which this glyph came is
14597 found in the buffer at point, or at position
14598 that is closer to point than pos_after, then
14599 we've found the glyph we've been looking for.
14600 If it comes from an overlay (tem == 0), and
14601 it has the `cursor' property on one of its
14602 glyphs, record that glyph as a candidate for
14603 displaying the cursor. (As in the
14604 unidirectional version, we will display the
14605 cursor on the last candidate we find.) */
14606 if (tem == 0
14607 || tem == pt_old
14608 || (tem - pt_old > 0 && tem < pos_after))
14610 /* The glyphs from this string could have
14611 been reordered. Find the one with the
14612 smallest string position. Or there could
14613 be a character in the string with the
14614 `cursor' property, which means display
14615 cursor on that character's glyph. */
14616 ptrdiff_t strpos = glyph->charpos;
14618 if (tem)
14620 cursor = glyph;
14621 string_from_text_prop = 1;
14623 for ( ;
14624 (row->reversed_p ? glyph > stop : glyph < stop)
14625 && EQ (glyph->object, str);
14626 glyph += incr)
14628 Lisp_Object cprop;
14629 ptrdiff_t gpos = glyph->charpos;
14631 cprop = Fget_char_property (make_number (gpos),
14632 Qcursor,
14633 glyph->object);
14634 if (!NILP (cprop))
14636 cursor = glyph;
14637 break;
14639 if (tem && glyph->charpos < strpos)
14641 strpos = glyph->charpos;
14642 cursor = glyph;
14646 if (tem == pt_old
14647 || (tem - pt_old > 0 && tem < pos_after))
14648 goto compute_x;
14650 if (tem)
14651 pos = tem + 1; /* don't find previous instances */
14653 /* This string is not what we want; skip all of the
14654 glyphs that came from it. */
14655 while ((row->reversed_p ? glyph > stop : glyph < stop)
14656 && EQ (glyph->object, str))
14657 glyph += incr;
14659 else
14660 glyph += incr;
14663 /* If we reached the end of the line, and END was from a string,
14664 the cursor is not on this line. */
14665 if (cursor == NULL
14666 && (row->reversed_p ? glyph <= end : glyph >= end)
14667 && (row->reversed_p ? end > glyphs_end : end < glyphs_end)
14668 && STRINGP (end->object)
14669 && row->continued_p)
14670 return 0;
14672 /* A truncated row may not include PT among its character positions.
14673 Setting the cursor inside the scroll margin will trigger
14674 recalculation of hscroll in hscroll_window_tree. But if a
14675 display string covers point, defer to the string-handling
14676 code below to figure this out. */
14677 else if (row->truncated_on_left_p && pt_old < bpos_min)
14679 cursor = glyph_before;
14680 x = -1;
14682 else if ((row->truncated_on_right_p && pt_old > bpos_max)
14683 /* Zero-width characters produce no glyphs. */
14684 || (!empty_line_p
14685 && (row->reversed_p
14686 ? glyph_after > glyphs_end
14687 : glyph_after < glyphs_end)))
14689 cursor = glyph_after;
14690 x = -1;
14694 compute_x:
14695 if (cursor != NULL)
14696 glyph = cursor;
14697 else if (glyph == glyphs_end
14698 && pos_before == pos_after
14699 && STRINGP ((row->reversed_p
14700 ? row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
14701 : row->glyphs[TEXT_AREA])->object))
14703 /* If all the glyphs of this row came from strings, put the
14704 cursor on the first glyph of the row. This avoids having the
14705 cursor outside of the text area in this very rare and hard
14706 use case. */
14707 glyph =
14708 row->reversed_p
14709 ? row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
14710 : row->glyphs[TEXT_AREA];
14712 if (x < 0)
14714 struct glyph *g;
14716 /* Need to compute x that corresponds to GLYPH. */
14717 for (g = row->glyphs[TEXT_AREA], x = row->x; g < glyph; g++)
14719 if (g >= row->glyphs[TEXT_AREA] + row->used[TEXT_AREA])
14720 emacs_abort ();
14721 x += g->pixel_width;
14725 /* ROW could be part of a continued line, which, under bidi
14726 reordering, might have other rows whose start and end charpos
14727 occlude point. Only set w->cursor if we found a better
14728 approximation to the cursor position than we have from previously
14729 examined candidate rows belonging to the same continued line. */
14730 if (/* We already have a candidate row. */
14731 w->cursor.vpos >= 0
14732 /* That candidate is not the row we are processing. */
14733 && MATRIX_ROW (matrix, w->cursor.vpos) != row
14734 /* Make sure cursor.vpos specifies a row whose start and end
14735 charpos occlude point, and it is valid candidate for being a
14736 cursor-row. This is because some callers of this function
14737 leave cursor.vpos at the row where the cursor was displayed
14738 during the last redisplay cycle. */
14739 && MATRIX_ROW_START_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos)) <= pt_old
14740 && pt_old <= MATRIX_ROW_END_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
14741 && cursor_row_p (MATRIX_ROW (matrix, w->cursor.vpos)))
14743 struct glyph *g1
14744 = MATRIX_ROW_GLYPH_START (matrix, w->cursor.vpos) + w->cursor.hpos;
14746 /* Don't consider glyphs that are outside TEXT_AREA. */
14747 if (!(row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end))
14748 return 0;
14749 /* Keep the candidate whose buffer position is the closest to
14750 point or has the `cursor' property. */
14751 if (/* Previous candidate is a glyph in TEXT_AREA of that row. */
14752 w->cursor.hpos >= 0
14753 && w->cursor.hpos < MATRIX_ROW_USED (matrix, w->cursor.vpos)
14754 && ((BUFFERP (g1->object)
14755 && (g1->charpos == pt_old /* An exact match always wins. */
14756 || (BUFFERP (glyph->object)
14757 && eabs (g1->charpos - pt_old)
14758 < eabs (glyph->charpos - pt_old))))
14759 /* Previous candidate is a glyph from a string that has
14760 a non-nil `cursor' property. */
14761 || (STRINGP (g1->object)
14762 && (!NILP (Fget_char_property (make_number (g1->charpos),
14763 Qcursor, g1->object))
14764 /* Previous candidate is from the same display
14765 string as this one, and the display string
14766 came from a text property. */
14767 || (EQ (g1->object, glyph->object)
14768 && string_from_text_prop)
14769 /* this candidate is from newline and its
14770 position is not an exact match */
14771 || (INTEGERP (glyph->object)
14772 && glyph->charpos != pt_old)))))
14773 return 0;
14774 /* If this candidate gives an exact match, use that. */
14775 if (!((BUFFERP (glyph->object) && glyph->charpos == pt_old)
14776 /* If this candidate is a glyph created for the
14777 terminating newline of a line, and point is on that
14778 newline, it wins because it's an exact match. */
14779 || (!row->continued_p
14780 && INTEGERP (glyph->object)
14781 && glyph->charpos == 0
14782 && pt_old == MATRIX_ROW_END_CHARPOS (row) - 1))
14783 /* Otherwise, keep the candidate that comes from a row
14784 spanning less buffer positions. This may win when one or
14785 both candidate positions are on glyphs that came from
14786 display strings, for which we cannot compare buffer
14787 positions. */
14788 && MATRIX_ROW_END_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
14789 - MATRIX_ROW_START_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
14790 < MATRIX_ROW_END_CHARPOS (row) - MATRIX_ROW_START_CHARPOS (row))
14791 return 0;
14793 w->cursor.hpos = glyph - row->glyphs[TEXT_AREA];
14794 w->cursor.x = x;
14795 w->cursor.vpos = MATRIX_ROW_VPOS (row, matrix) + dvpos;
14796 w->cursor.y = row->y + dy;
14798 if (w == XWINDOW (selected_window))
14800 if (!row->continued_p
14801 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
14802 && row->x == 0)
14804 this_line_buffer = XBUFFER (w->contents);
14806 CHARPOS (this_line_start_pos)
14807 = MATRIX_ROW_START_CHARPOS (row) + delta;
14808 BYTEPOS (this_line_start_pos)
14809 = MATRIX_ROW_START_BYTEPOS (row) + delta_bytes;
14811 CHARPOS (this_line_end_pos)
14812 = Z - (MATRIX_ROW_END_CHARPOS (row) + delta);
14813 BYTEPOS (this_line_end_pos)
14814 = Z_BYTE - (MATRIX_ROW_END_BYTEPOS (row) + delta_bytes);
14816 this_line_y = w->cursor.y;
14817 this_line_pixel_height = row->height;
14818 this_line_vpos = w->cursor.vpos;
14819 this_line_start_x = row->x;
14821 else
14822 CHARPOS (this_line_start_pos) = 0;
14825 return 1;
14829 /* Run window scroll functions, if any, for WINDOW with new window
14830 start STARTP. Sets the window start of WINDOW to that position.
14832 We assume that the window's buffer is really current. */
14834 static struct text_pos
14835 run_window_scroll_functions (Lisp_Object window, struct text_pos startp)
14837 struct window *w = XWINDOW (window);
14838 SET_MARKER_FROM_TEXT_POS (w->start, startp);
14840 eassert (current_buffer == XBUFFER (w->contents));
14842 if (!NILP (Vwindow_scroll_functions))
14844 run_hook_with_args_2 (Qwindow_scroll_functions, window,
14845 make_number (CHARPOS (startp)));
14846 SET_TEXT_POS_FROM_MARKER (startp, w->start);
14847 /* In case the hook functions switch buffers. */
14848 set_buffer_internal (XBUFFER (w->contents));
14851 return startp;
14855 /* Make sure the line containing the cursor is fully visible.
14856 A value of 1 means there is nothing to be done.
14857 (Either the line is fully visible, or it cannot be made so,
14858 or we cannot tell.)
14860 If FORCE_P is non-zero, return 0 even if partial visible cursor row
14861 is higher than window.
14863 A value of 0 means the caller should do scrolling
14864 as if point had gone off the screen. */
14866 static int
14867 cursor_row_fully_visible_p (struct window *w, int force_p, int current_matrix_p)
14869 struct glyph_matrix *matrix;
14870 struct glyph_row *row;
14871 int window_height;
14873 if (!make_cursor_line_fully_visible_p)
14874 return 1;
14876 /* It's not always possible to find the cursor, e.g, when a window
14877 is full of overlay strings. Don't do anything in that case. */
14878 if (w->cursor.vpos < 0)
14879 return 1;
14881 matrix = current_matrix_p ? w->current_matrix : w->desired_matrix;
14882 row = MATRIX_ROW (matrix, w->cursor.vpos);
14884 /* If the cursor row is not partially visible, there's nothing to do. */
14885 if (!MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row))
14886 return 1;
14888 /* If the row the cursor is in is taller than the window's height,
14889 it's not clear what to do, so do nothing. */
14890 window_height = window_box_height (w);
14891 if (row->height >= window_height)
14893 if (!force_p || MINI_WINDOW_P (w)
14894 || w->vscroll || w->cursor.vpos == 0)
14895 return 1;
14897 return 0;
14901 /* Try scrolling PT into view in window WINDOW. JUST_THIS_ONE_P
14902 non-zero means only WINDOW is redisplayed in redisplay_internal.
14903 TEMP_SCROLL_STEP has the same meaning as emacs_scroll_step, and is used
14904 in redisplay_window to bring a partially visible line into view in
14905 the case that only the cursor has moved.
14907 LAST_LINE_MISFIT should be nonzero if we're scrolling because the
14908 last screen line's vertical height extends past the end of the screen.
14910 Value is
14912 1 if scrolling succeeded
14914 0 if scrolling didn't find point.
14916 -1 if new fonts have been loaded so that we must interrupt
14917 redisplay, adjust glyph matrices, and try again. */
14919 enum
14921 SCROLLING_SUCCESS,
14922 SCROLLING_FAILED,
14923 SCROLLING_NEED_LARGER_MATRICES
14926 /* If scroll-conservatively is more than this, never recenter.
14928 If you change this, don't forget to update the doc string of
14929 `scroll-conservatively' and the Emacs manual. */
14930 #define SCROLL_LIMIT 100
14932 static int
14933 try_scrolling (Lisp_Object window, int just_this_one_p,
14934 ptrdiff_t arg_scroll_conservatively, ptrdiff_t scroll_step,
14935 int temp_scroll_step, int last_line_misfit)
14937 struct window *w = XWINDOW (window);
14938 struct frame *f = XFRAME (w->frame);
14939 struct text_pos pos, startp;
14940 struct it it;
14941 int this_scroll_margin, scroll_max, rc, height;
14942 int dy = 0, amount_to_scroll = 0, scroll_down_p = 0;
14943 int extra_scroll_margin_lines = last_line_misfit ? 1 : 0;
14944 Lisp_Object aggressive;
14945 /* We will never try scrolling more than this number of lines. */
14946 int scroll_limit = SCROLL_LIMIT;
14947 int frame_line_height = default_line_pixel_height (w);
14948 int window_total_lines
14949 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
14951 #ifdef GLYPH_DEBUG
14952 debug_method_add (w, "try_scrolling");
14953 #endif
14955 SET_TEXT_POS_FROM_MARKER (startp, w->start);
14957 /* Compute scroll margin height in pixels. We scroll when point is
14958 within this distance from the top or bottom of the window. */
14959 if (scroll_margin > 0)
14960 this_scroll_margin = min (scroll_margin, window_total_lines / 4)
14961 * frame_line_height;
14962 else
14963 this_scroll_margin = 0;
14965 /* Force arg_scroll_conservatively to have a reasonable value, to
14966 avoid scrolling too far away with slow move_it_* functions. Note
14967 that the user can supply scroll-conservatively equal to
14968 `most-positive-fixnum', which can be larger than INT_MAX. */
14969 if (arg_scroll_conservatively > scroll_limit)
14971 arg_scroll_conservatively = scroll_limit + 1;
14972 scroll_max = scroll_limit * frame_line_height;
14974 else if (scroll_step || arg_scroll_conservatively || temp_scroll_step)
14975 /* Compute how much we should try to scroll maximally to bring
14976 point into view. */
14977 scroll_max = (max (scroll_step,
14978 max (arg_scroll_conservatively, temp_scroll_step))
14979 * frame_line_height);
14980 else if (NUMBERP (BVAR (current_buffer, scroll_down_aggressively))
14981 || NUMBERP (BVAR (current_buffer, scroll_up_aggressively)))
14982 /* We're trying to scroll because of aggressive scrolling but no
14983 scroll_step is set. Choose an arbitrary one. */
14984 scroll_max = 10 * frame_line_height;
14985 else
14986 scroll_max = 0;
14988 too_near_end:
14990 /* Decide whether to scroll down. */
14991 if (PT > CHARPOS (startp))
14993 int scroll_margin_y;
14995 /* Compute the pixel ypos of the scroll margin, then move IT to
14996 either that ypos or PT, whichever comes first. */
14997 start_display (&it, w, startp);
14998 scroll_margin_y = it.last_visible_y - this_scroll_margin
14999 - frame_line_height * extra_scroll_margin_lines;
15000 move_it_to (&it, PT, -1, scroll_margin_y - 1, -1,
15001 (MOVE_TO_POS | MOVE_TO_Y));
15003 if (PT > CHARPOS (it.current.pos))
15005 int y0 = line_bottom_y (&it);
15006 /* Compute how many pixels below window bottom to stop searching
15007 for PT. This avoids costly search for PT that is far away if
15008 the user limited scrolling by a small number of lines, but
15009 always finds PT if scroll_conservatively is set to a large
15010 number, such as most-positive-fixnum. */
15011 int slack = max (scroll_max, 10 * frame_line_height);
15012 int y_to_move = it.last_visible_y + slack;
15014 /* Compute the distance from the scroll margin to PT or to
15015 the scroll limit, whichever comes first. This should
15016 include the height of the cursor line, to make that line
15017 fully visible. */
15018 move_it_to (&it, PT, -1, y_to_move,
15019 -1, MOVE_TO_POS | MOVE_TO_Y);
15020 dy = line_bottom_y (&it) - y0;
15022 if (dy > scroll_max)
15023 return SCROLLING_FAILED;
15025 if (dy > 0)
15026 scroll_down_p = 1;
15030 if (scroll_down_p)
15032 /* Point is in or below the bottom scroll margin, so move the
15033 window start down. If scrolling conservatively, move it just
15034 enough down to make point visible. If scroll_step is set,
15035 move it down by scroll_step. */
15036 if (arg_scroll_conservatively)
15037 amount_to_scroll
15038 = min (max (dy, frame_line_height),
15039 frame_line_height * arg_scroll_conservatively);
15040 else if (scroll_step || temp_scroll_step)
15041 amount_to_scroll = scroll_max;
15042 else
15044 aggressive = BVAR (current_buffer, scroll_up_aggressively);
15045 height = WINDOW_BOX_TEXT_HEIGHT (w);
15046 if (NUMBERP (aggressive))
15048 double float_amount = XFLOATINT (aggressive) * height;
15049 int aggressive_scroll = float_amount;
15050 if (aggressive_scroll == 0 && float_amount > 0)
15051 aggressive_scroll = 1;
15052 /* Don't let point enter the scroll margin near top of
15053 the window. This could happen if the value of
15054 scroll_up_aggressively is too large and there are
15055 non-zero margins, because scroll_up_aggressively
15056 means put point that fraction of window height
15057 _from_the_bottom_margin_. */
15058 if (aggressive_scroll + 2*this_scroll_margin > height)
15059 aggressive_scroll = height - 2*this_scroll_margin;
15060 amount_to_scroll = dy + aggressive_scroll;
15064 if (amount_to_scroll <= 0)
15065 return SCROLLING_FAILED;
15067 start_display (&it, w, startp);
15068 if (arg_scroll_conservatively <= scroll_limit)
15069 move_it_vertically (&it, amount_to_scroll);
15070 else
15072 /* Extra precision for users who set scroll-conservatively
15073 to a large number: make sure the amount we scroll
15074 the window start is never less than amount_to_scroll,
15075 which was computed as distance from window bottom to
15076 point. This matters when lines at window top and lines
15077 below window bottom have different height. */
15078 struct it it1;
15079 void *it1data = NULL;
15080 /* We use a temporary it1 because line_bottom_y can modify
15081 its argument, if it moves one line down; see there. */
15082 int start_y;
15084 SAVE_IT (it1, it, it1data);
15085 start_y = line_bottom_y (&it1);
15086 do {
15087 RESTORE_IT (&it, &it, it1data);
15088 move_it_by_lines (&it, 1);
15089 SAVE_IT (it1, it, it1data);
15090 } while (line_bottom_y (&it1) - start_y < amount_to_scroll);
15093 /* If STARTP is unchanged, move it down another screen line. */
15094 if (CHARPOS (it.current.pos) == CHARPOS (startp))
15095 move_it_by_lines (&it, 1);
15096 startp = it.current.pos;
15098 else
15100 struct text_pos scroll_margin_pos = startp;
15101 int y_offset = 0;
15103 /* See if point is inside the scroll margin at the top of the
15104 window. */
15105 if (this_scroll_margin)
15107 int y_start;
15109 start_display (&it, w, startp);
15110 y_start = it.current_y;
15111 move_it_vertically (&it, this_scroll_margin);
15112 scroll_margin_pos = it.current.pos;
15113 /* If we didn't move enough before hitting ZV, request
15114 additional amount of scroll, to move point out of the
15115 scroll margin. */
15116 if (IT_CHARPOS (it) == ZV
15117 && it.current_y - y_start < this_scroll_margin)
15118 y_offset = this_scroll_margin - (it.current_y - y_start);
15121 if (PT < CHARPOS (scroll_margin_pos))
15123 /* Point is in the scroll margin at the top of the window or
15124 above what is displayed in the window. */
15125 int y0, y_to_move;
15127 /* Compute the vertical distance from PT to the scroll
15128 margin position. Move as far as scroll_max allows, or
15129 one screenful, or 10 screen lines, whichever is largest.
15130 Give up if distance is greater than scroll_max or if we
15131 didn't reach the scroll margin position. */
15132 SET_TEXT_POS (pos, PT, PT_BYTE);
15133 start_display (&it, w, pos);
15134 y0 = it.current_y;
15135 y_to_move = max (it.last_visible_y,
15136 max (scroll_max, 10 * frame_line_height));
15137 move_it_to (&it, CHARPOS (scroll_margin_pos), 0,
15138 y_to_move, -1,
15139 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
15140 dy = it.current_y - y0;
15141 if (dy > scroll_max
15142 || IT_CHARPOS (it) < CHARPOS (scroll_margin_pos))
15143 return SCROLLING_FAILED;
15145 /* Additional scroll for when ZV was too close to point. */
15146 dy += y_offset;
15148 /* Compute new window start. */
15149 start_display (&it, w, startp);
15151 if (arg_scroll_conservatively)
15152 amount_to_scroll = max (dy, frame_line_height *
15153 max (scroll_step, temp_scroll_step));
15154 else if (scroll_step || temp_scroll_step)
15155 amount_to_scroll = scroll_max;
15156 else
15158 aggressive = BVAR (current_buffer, scroll_down_aggressively);
15159 height = WINDOW_BOX_TEXT_HEIGHT (w);
15160 if (NUMBERP (aggressive))
15162 double float_amount = XFLOATINT (aggressive) * height;
15163 int aggressive_scroll = float_amount;
15164 if (aggressive_scroll == 0 && float_amount > 0)
15165 aggressive_scroll = 1;
15166 /* Don't let point enter the scroll margin near
15167 bottom of the window, if the value of
15168 scroll_down_aggressively happens to be too
15169 large. */
15170 if (aggressive_scroll + 2*this_scroll_margin > height)
15171 aggressive_scroll = height - 2*this_scroll_margin;
15172 amount_to_scroll = dy + aggressive_scroll;
15176 if (amount_to_scroll <= 0)
15177 return SCROLLING_FAILED;
15179 move_it_vertically_backward (&it, amount_to_scroll);
15180 startp = it.current.pos;
15184 /* Run window scroll functions. */
15185 startp = run_window_scroll_functions (window, startp);
15187 /* Display the window. Give up if new fonts are loaded, or if point
15188 doesn't appear. */
15189 if (!try_window (window, startp, 0))
15190 rc = SCROLLING_NEED_LARGER_MATRICES;
15191 else if (w->cursor.vpos < 0)
15193 clear_glyph_matrix (w->desired_matrix);
15194 rc = SCROLLING_FAILED;
15196 else
15198 /* Maybe forget recorded base line for line number display. */
15199 if (!just_this_one_p
15200 || current_buffer->clip_changed
15201 || BEG_UNCHANGED < CHARPOS (startp))
15202 w->base_line_number = 0;
15204 /* If cursor ends up on a partially visible line,
15205 treat that as being off the bottom of the screen. */
15206 if (! cursor_row_fully_visible_p (w, extra_scroll_margin_lines <= 1, 0)
15207 /* It's possible that the cursor is on the first line of the
15208 buffer, which is partially obscured due to a vscroll
15209 (Bug#7537). In that case, avoid looping forever. */
15210 && extra_scroll_margin_lines < w->desired_matrix->nrows - 1)
15212 clear_glyph_matrix (w->desired_matrix);
15213 ++extra_scroll_margin_lines;
15214 goto too_near_end;
15216 rc = SCROLLING_SUCCESS;
15219 return rc;
15223 /* Compute a suitable window start for window W if display of W starts
15224 on a continuation line. Value is non-zero if a new window start
15225 was computed.
15227 The new window start will be computed, based on W's width, starting
15228 from the start of the continued line. It is the start of the
15229 screen line with the minimum distance from the old start W->start. */
15231 static int
15232 compute_window_start_on_continuation_line (struct window *w)
15234 struct text_pos pos, start_pos;
15235 int window_start_changed_p = 0;
15237 SET_TEXT_POS_FROM_MARKER (start_pos, w->start);
15239 /* If window start is on a continuation line... Window start may be
15240 < BEGV in case there's invisible text at the start of the
15241 buffer (M-x rmail, for example). */
15242 if (CHARPOS (start_pos) > BEGV
15243 && FETCH_BYTE (BYTEPOS (start_pos) - 1) != '\n')
15245 struct it it;
15246 struct glyph_row *row;
15248 /* Handle the case that the window start is out of range. */
15249 if (CHARPOS (start_pos) < BEGV)
15250 SET_TEXT_POS (start_pos, BEGV, BEGV_BYTE);
15251 else if (CHARPOS (start_pos) > ZV)
15252 SET_TEXT_POS (start_pos, ZV, ZV_BYTE);
15254 /* Find the start of the continued line. This should be fast
15255 because find_newline is fast (newline cache). */
15256 row = w->desired_matrix->rows + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0);
15257 init_iterator (&it, w, CHARPOS (start_pos), BYTEPOS (start_pos),
15258 row, DEFAULT_FACE_ID);
15259 reseat_at_previous_visible_line_start (&it);
15261 /* If the line start is "too far" away from the window start,
15262 say it takes too much time to compute a new window start. */
15263 if (CHARPOS (start_pos) - IT_CHARPOS (it)
15264 /* PXW: Do we need upper bounds here? */
15265 < WINDOW_TOTAL_LINES (w) * WINDOW_TOTAL_COLS (w))
15267 int min_distance, distance;
15269 /* Move forward by display lines to find the new window
15270 start. If window width was enlarged, the new start can
15271 be expected to be > the old start. If window width was
15272 decreased, the new window start will be < the old start.
15273 So, we're looking for the display line start with the
15274 minimum distance from the old window start. */
15275 pos = it.current.pos;
15276 min_distance = INFINITY;
15277 while ((distance = eabs (CHARPOS (start_pos) - IT_CHARPOS (it))),
15278 distance < min_distance)
15280 min_distance = distance;
15281 pos = it.current.pos;
15282 if (it.line_wrap == WORD_WRAP)
15284 /* Under WORD_WRAP, move_it_by_lines is likely to
15285 overshoot and stop not at the first, but the
15286 second character from the left margin. So in
15287 that case, we need a more tight control on the X
15288 coordinate of the iterator than move_it_by_lines
15289 promises in its contract. The method is to first
15290 go to the last (rightmost) visible character of a
15291 line, then move to the leftmost character on the
15292 next line in a separate call. */
15293 move_it_to (&it, ZV, it.last_visible_x, it.current_y, -1,
15294 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
15295 move_it_to (&it, ZV, 0,
15296 it.current_y + it.max_ascent + it.max_descent, -1,
15297 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
15299 else
15300 move_it_by_lines (&it, 1);
15303 /* Set the window start there. */
15304 SET_MARKER_FROM_TEXT_POS (w->start, pos);
15305 window_start_changed_p = 1;
15309 return window_start_changed_p;
15313 /* Try cursor movement in case text has not changed in window WINDOW,
15314 with window start STARTP. Value is
15316 CURSOR_MOVEMENT_SUCCESS if successful
15318 CURSOR_MOVEMENT_CANNOT_BE_USED if this method cannot be used
15320 CURSOR_MOVEMENT_MUST_SCROLL if we know we have to scroll the
15321 display. *SCROLL_STEP is set to 1, under certain circumstances, if
15322 we want to scroll as if scroll-step were set to 1. See the code.
15324 CURSOR_MOVEMENT_NEED_LARGER_MATRICES if we need larger matrices, in
15325 which case we have to abort this redisplay, and adjust matrices
15326 first. */
15328 enum
15330 CURSOR_MOVEMENT_SUCCESS,
15331 CURSOR_MOVEMENT_CANNOT_BE_USED,
15332 CURSOR_MOVEMENT_MUST_SCROLL,
15333 CURSOR_MOVEMENT_NEED_LARGER_MATRICES
15336 static int
15337 try_cursor_movement (Lisp_Object window, struct text_pos startp, int *scroll_step)
15339 struct window *w = XWINDOW (window);
15340 struct frame *f = XFRAME (w->frame);
15341 int rc = CURSOR_MOVEMENT_CANNOT_BE_USED;
15343 #ifdef GLYPH_DEBUG
15344 if (inhibit_try_cursor_movement)
15345 return rc;
15346 #endif
15348 /* Previously, there was a check for Lisp integer in the
15349 if-statement below. Now, this field is converted to
15350 ptrdiff_t, thus zero means invalid position in a buffer. */
15351 eassert (w->last_point > 0);
15352 /* Likewise there was a check whether window_end_vpos is nil or larger
15353 than the window. Now window_end_vpos is int and so never nil, but
15354 let's leave eassert to check whether it fits in the window. */
15355 eassert (w->window_end_vpos < w->current_matrix->nrows);
15357 /* Handle case where text has not changed, only point, and it has
15358 not moved off the frame. */
15359 if (/* Point may be in this window. */
15360 PT >= CHARPOS (startp)
15361 /* Selective display hasn't changed. */
15362 && !current_buffer->clip_changed
15363 /* Function force-mode-line-update is used to force a thorough
15364 redisplay. It sets either windows_or_buffers_changed or
15365 update_mode_lines. So don't take a shortcut here for these
15366 cases. */
15367 && !update_mode_lines
15368 && !windows_or_buffers_changed
15369 && !f->cursor_type_changed
15370 && NILP (Vshow_trailing_whitespace)
15371 /* This code is not used for mini-buffer for the sake of the case
15372 of redisplaying to replace an echo area message; since in
15373 that case the mini-buffer contents per se are usually
15374 unchanged. This code is of no real use in the mini-buffer
15375 since the handling of this_line_start_pos, etc., in redisplay
15376 handles the same cases. */
15377 && !EQ (window, minibuf_window)
15378 && (FRAME_WINDOW_P (f)
15379 || !overlay_arrow_in_current_buffer_p ()))
15381 int this_scroll_margin, top_scroll_margin;
15382 struct glyph_row *row = NULL;
15383 int frame_line_height = default_line_pixel_height (w);
15384 int window_total_lines
15385 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
15387 #ifdef GLYPH_DEBUG
15388 debug_method_add (w, "cursor movement");
15389 #endif
15391 /* Scroll if point within this distance from the top or bottom
15392 of the window. This is a pixel value. */
15393 if (scroll_margin > 0)
15395 this_scroll_margin = min (scroll_margin, window_total_lines / 4);
15396 this_scroll_margin *= frame_line_height;
15398 else
15399 this_scroll_margin = 0;
15401 top_scroll_margin = this_scroll_margin;
15402 if (WINDOW_WANTS_HEADER_LINE_P (w))
15403 top_scroll_margin += CURRENT_HEADER_LINE_HEIGHT (w);
15405 /* Start with the row the cursor was displayed during the last
15406 not paused redisplay. Give up if that row is not valid. */
15407 if (w->last_cursor_vpos < 0
15408 || w->last_cursor_vpos >= w->current_matrix->nrows)
15409 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15410 else
15412 row = MATRIX_ROW (w->current_matrix, w->last_cursor_vpos);
15413 if (row->mode_line_p)
15414 ++row;
15415 if (!row->enabled_p)
15416 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15419 if (rc == CURSOR_MOVEMENT_CANNOT_BE_USED)
15421 int scroll_p = 0, must_scroll = 0;
15422 int last_y = window_text_bottom_y (w) - this_scroll_margin;
15424 if (PT > w->last_point)
15426 /* Point has moved forward. */
15427 while (MATRIX_ROW_END_CHARPOS (row) < PT
15428 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
15430 eassert (row->enabled_p);
15431 ++row;
15434 /* If the end position of a row equals the start
15435 position of the next row, and PT is at that position,
15436 we would rather display cursor in the next line. */
15437 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
15438 && MATRIX_ROW_END_CHARPOS (row) == PT
15439 && row < MATRIX_MODE_LINE_ROW (w->current_matrix)
15440 && MATRIX_ROW_START_CHARPOS (row+1) == PT
15441 && !cursor_row_p (row))
15442 ++row;
15444 /* If within the scroll margin, scroll. Note that
15445 MATRIX_ROW_BOTTOM_Y gives the pixel position at which
15446 the next line would be drawn, and that
15447 this_scroll_margin can be zero. */
15448 if (MATRIX_ROW_BOTTOM_Y (row) > last_y
15449 || PT > MATRIX_ROW_END_CHARPOS (row)
15450 /* Line is completely visible last line in window
15451 and PT is to be set in the next line. */
15452 || (MATRIX_ROW_BOTTOM_Y (row) == last_y
15453 && PT == MATRIX_ROW_END_CHARPOS (row)
15454 && !row->ends_at_zv_p
15455 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
15456 scroll_p = 1;
15458 else if (PT < w->last_point)
15460 /* Cursor has to be moved backward. Note that PT >=
15461 CHARPOS (startp) because of the outer if-statement. */
15462 while (!row->mode_line_p
15463 && (MATRIX_ROW_START_CHARPOS (row) > PT
15464 || (MATRIX_ROW_START_CHARPOS (row) == PT
15465 && (MATRIX_ROW_STARTS_IN_MIDDLE_OF_CHAR_P (row)
15466 || (/* STARTS_IN_MIDDLE_OF_STRING_P (row) */
15467 row > w->current_matrix->rows
15468 && (row-1)->ends_in_newline_from_string_p))))
15469 && (row->y > top_scroll_margin
15470 || CHARPOS (startp) == BEGV))
15472 eassert (row->enabled_p);
15473 --row;
15476 /* Consider the following case: Window starts at BEGV,
15477 there is invisible, intangible text at BEGV, so that
15478 display starts at some point START > BEGV. It can
15479 happen that we are called with PT somewhere between
15480 BEGV and START. Try to handle that case. */
15481 if (row < w->current_matrix->rows
15482 || row->mode_line_p)
15484 row = w->current_matrix->rows;
15485 if (row->mode_line_p)
15486 ++row;
15489 /* Due to newlines in overlay strings, we may have to
15490 skip forward over overlay strings. */
15491 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
15492 && MATRIX_ROW_END_CHARPOS (row) == PT
15493 && !cursor_row_p (row))
15494 ++row;
15496 /* If within the scroll margin, scroll. */
15497 if (row->y < top_scroll_margin
15498 && CHARPOS (startp) != BEGV)
15499 scroll_p = 1;
15501 else
15503 /* Cursor did not move. So don't scroll even if cursor line
15504 is partially visible, as it was so before. */
15505 rc = CURSOR_MOVEMENT_SUCCESS;
15508 if (PT < MATRIX_ROW_START_CHARPOS (row)
15509 || PT > MATRIX_ROW_END_CHARPOS (row))
15511 /* if PT is not in the glyph row, give up. */
15512 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15513 must_scroll = 1;
15515 else if (rc != CURSOR_MOVEMENT_SUCCESS
15516 && !NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering)))
15518 struct glyph_row *row1;
15520 /* If rows are bidi-reordered and point moved, back up
15521 until we find a row that does not belong to a
15522 continuation line. This is because we must consider
15523 all rows of a continued line as candidates for the
15524 new cursor positioning, since row start and end
15525 positions change non-linearly with vertical position
15526 in such rows. */
15527 /* FIXME: Revisit this when glyph ``spilling'' in
15528 continuation lines' rows is implemented for
15529 bidi-reordered rows. */
15530 for (row1 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
15531 MATRIX_ROW_CONTINUATION_LINE_P (row);
15532 --row)
15534 /* If we hit the beginning of the displayed portion
15535 without finding the first row of a continued
15536 line, give up. */
15537 if (row <= row1)
15539 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15540 break;
15542 eassert (row->enabled_p);
15545 if (must_scroll)
15547 else if (rc != CURSOR_MOVEMENT_SUCCESS
15548 && MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row)
15549 /* Make sure this isn't a header line by any chance, since
15550 then MATRIX_ROW_PARTIALLY_VISIBLE_P might yield non-zero. */
15551 && !row->mode_line_p
15552 && make_cursor_line_fully_visible_p)
15554 if (PT == MATRIX_ROW_END_CHARPOS (row)
15555 && !row->ends_at_zv_p
15556 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
15557 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15558 else if (row->height > window_box_height (w))
15560 /* If we end up in a partially visible line, let's
15561 make it fully visible, except when it's taller
15562 than the window, in which case we can't do much
15563 about it. */
15564 *scroll_step = 1;
15565 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15567 else
15569 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
15570 if (!cursor_row_fully_visible_p (w, 0, 1))
15571 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15572 else
15573 rc = CURSOR_MOVEMENT_SUCCESS;
15576 else if (scroll_p)
15577 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15578 else if (rc != CURSOR_MOVEMENT_SUCCESS
15579 && !NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering)))
15581 /* With bidi-reordered rows, there could be more than
15582 one candidate row whose start and end positions
15583 occlude point. We need to let set_cursor_from_row
15584 find the best candidate. */
15585 /* FIXME: Revisit this when glyph ``spilling'' in
15586 continuation lines' rows is implemented for
15587 bidi-reordered rows. */
15588 int rv = 0;
15592 int at_zv_p = 0, exact_match_p = 0;
15594 if (MATRIX_ROW_START_CHARPOS (row) <= PT
15595 && PT <= MATRIX_ROW_END_CHARPOS (row)
15596 && cursor_row_p (row))
15597 rv |= set_cursor_from_row (w, row, w->current_matrix,
15598 0, 0, 0, 0);
15599 /* As soon as we've found the exact match for point,
15600 or the first suitable row whose ends_at_zv_p flag
15601 is set, we are done. */
15602 if (rv)
15604 at_zv_p = MATRIX_ROW (w->current_matrix,
15605 w->cursor.vpos)->ends_at_zv_p;
15606 if (!at_zv_p
15607 && w->cursor.hpos >= 0
15608 && w->cursor.hpos < MATRIX_ROW_USED (w->current_matrix,
15609 w->cursor.vpos))
15611 struct glyph_row *candidate =
15612 MATRIX_ROW (w->current_matrix, w->cursor.vpos);
15613 struct glyph *g =
15614 candidate->glyphs[TEXT_AREA] + w->cursor.hpos;
15615 ptrdiff_t endpos = MATRIX_ROW_END_CHARPOS (candidate);
15617 exact_match_p =
15618 (BUFFERP (g->object) && g->charpos == PT)
15619 || (INTEGERP (g->object)
15620 && (g->charpos == PT
15621 || (g->charpos == 0 && endpos - 1 == PT)));
15623 if (at_zv_p || exact_match_p)
15625 rc = CURSOR_MOVEMENT_SUCCESS;
15626 break;
15629 if (MATRIX_ROW_BOTTOM_Y (row) == last_y)
15630 break;
15631 ++row;
15633 while (((MATRIX_ROW_CONTINUATION_LINE_P (row)
15634 || row->continued_p)
15635 && MATRIX_ROW_BOTTOM_Y (row) <= last_y)
15636 || (MATRIX_ROW_START_CHARPOS (row) == PT
15637 && MATRIX_ROW_BOTTOM_Y (row) < last_y));
15638 /* If we didn't find any candidate rows, or exited the
15639 loop before all the candidates were examined, signal
15640 to the caller that this method failed. */
15641 if (rc != CURSOR_MOVEMENT_SUCCESS
15642 && !(rv
15643 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
15644 && !row->continued_p))
15645 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15646 else if (rv)
15647 rc = CURSOR_MOVEMENT_SUCCESS;
15649 else
15653 if (set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0))
15655 rc = CURSOR_MOVEMENT_SUCCESS;
15656 break;
15658 ++row;
15660 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
15661 && MATRIX_ROW_START_CHARPOS (row) == PT
15662 && cursor_row_p (row));
15667 return rc;
15670 #if !defined USE_TOOLKIT_SCROLL_BARS || defined USE_GTK
15671 static
15672 #endif
15673 void
15674 set_vertical_scroll_bar (struct window *w)
15676 ptrdiff_t start, end, whole;
15678 /* Calculate the start and end positions for the current window.
15679 At some point, it would be nice to choose between scrollbars
15680 which reflect the whole buffer size, with special markers
15681 indicating narrowing, and scrollbars which reflect only the
15682 visible region.
15684 Note that mini-buffers sometimes aren't displaying any text. */
15685 if (!MINI_WINDOW_P (w)
15686 || (w == XWINDOW (minibuf_window)
15687 && NILP (echo_area_buffer[0])))
15689 struct buffer *buf = XBUFFER (w->contents);
15690 whole = BUF_ZV (buf) - BUF_BEGV (buf);
15691 start = marker_position (w->start) - BUF_BEGV (buf);
15692 /* I don't think this is guaranteed to be right. For the
15693 moment, we'll pretend it is. */
15694 end = BUF_Z (buf) - w->window_end_pos - BUF_BEGV (buf);
15696 if (end < start)
15697 end = start;
15698 if (whole < (end - start))
15699 whole = end - start;
15701 else
15702 start = end = whole = 0;
15704 /* Indicate what this scroll bar ought to be displaying now. */
15705 if (FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
15706 (*FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
15707 (w, end - start, whole, start);
15711 /* Redisplay leaf window WINDOW. JUST_THIS_ONE_P non-zero means only
15712 selected_window is redisplayed.
15714 We can return without actually redisplaying the window if fonts has been
15715 changed on window's frame. In that case, redisplay_internal will retry. */
15717 static void
15718 redisplay_window (Lisp_Object window, bool just_this_one_p)
15720 struct window *w = XWINDOW (window);
15721 struct frame *f = XFRAME (w->frame);
15722 struct buffer *buffer = XBUFFER (w->contents);
15723 struct buffer *old = current_buffer;
15724 struct text_pos lpoint, opoint, startp;
15725 int update_mode_line;
15726 int tem;
15727 struct it it;
15728 /* Record it now because it's overwritten. */
15729 bool current_matrix_up_to_date_p = false;
15730 bool used_current_matrix_p = false;
15731 /* This is less strict than current_matrix_up_to_date_p.
15732 It indicates that the buffer contents and narrowing are unchanged. */
15733 bool buffer_unchanged_p = false;
15734 int temp_scroll_step = 0;
15735 ptrdiff_t count = SPECPDL_INDEX ();
15736 int rc;
15737 int centering_position = -1;
15738 int last_line_misfit = 0;
15739 ptrdiff_t beg_unchanged, end_unchanged;
15740 int frame_line_height;
15742 SET_TEXT_POS (lpoint, PT, PT_BYTE);
15743 opoint = lpoint;
15745 #ifdef GLYPH_DEBUG
15746 *w->desired_matrix->method = 0;
15747 #endif
15749 if (!just_this_one_p
15750 && REDISPLAY_SOME_P ()
15751 && !w->redisplay
15752 && !f->redisplay
15753 && !buffer->text->redisplay
15754 && BUF_PT (buffer) == w->last_point)
15755 return;
15757 /* Make sure that both W's markers are valid. */
15758 eassert (XMARKER (w->start)->buffer == buffer);
15759 eassert (XMARKER (w->pointm)->buffer == buffer);
15761 restart:
15762 reconsider_clip_changes (w);
15763 frame_line_height = default_line_pixel_height (w);
15765 /* Has the mode line to be updated? */
15766 update_mode_line = (w->update_mode_line
15767 || update_mode_lines
15768 || buffer->clip_changed
15769 || buffer->prevent_redisplay_optimizations_p);
15771 if (!just_this_one_p)
15772 /* If `just_this_one_p' is set, we apparently set must_be_updated_p more
15773 cleverly elsewhere. */
15774 w->must_be_updated_p = true;
15776 if (MINI_WINDOW_P (w))
15778 if (w == XWINDOW (echo_area_window)
15779 && !NILP (echo_area_buffer[0]))
15781 if (update_mode_line)
15782 /* We may have to update a tty frame's menu bar or a
15783 tool-bar. Example `M-x C-h C-h C-g'. */
15784 goto finish_menu_bars;
15785 else
15786 /* We've already displayed the echo area glyphs in this window. */
15787 goto finish_scroll_bars;
15789 else if ((w != XWINDOW (minibuf_window)
15790 || minibuf_level == 0)
15791 /* When buffer is nonempty, redisplay window normally. */
15792 && BUF_Z (XBUFFER (w->contents)) == BUF_BEG (XBUFFER (w->contents))
15793 /* Quail displays non-mini buffers in minibuffer window.
15794 In that case, redisplay the window normally. */
15795 && !NILP (Fmemq (w->contents, Vminibuffer_list)))
15797 /* W is a mini-buffer window, but it's not active, so clear
15798 it. */
15799 int yb = window_text_bottom_y (w);
15800 struct glyph_row *row;
15801 int y;
15803 for (y = 0, row = w->desired_matrix->rows;
15804 y < yb;
15805 y += row->height, ++row)
15806 blank_row (w, row, y);
15807 goto finish_scroll_bars;
15810 clear_glyph_matrix (w->desired_matrix);
15813 /* Otherwise set up data on this window; select its buffer and point
15814 value. */
15815 /* Really select the buffer, for the sake of buffer-local
15816 variables. */
15817 set_buffer_internal_1 (XBUFFER (w->contents));
15819 current_matrix_up_to_date_p
15820 = (w->window_end_valid
15821 && !current_buffer->clip_changed
15822 && !current_buffer->prevent_redisplay_optimizations_p
15823 && !window_outdated (w));
15825 /* Run the window-bottom-change-functions
15826 if it is possible that the text on the screen has changed
15827 (either due to modification of the text, or any other reason). */
15828 if (!current_matrix_up_to_date_p
15829 && !NILP (Vwindow_text_change_functions))
15831 safe_run_hooks (Qwindow_text_change_functions);
15832 goto restart;
15835 beg_unchanged = BEG_UNCHANGED;
15836 end_unchanged = END_UNCHANGED;
15838 SET_TEXT_POS (opoint, PT, PT_BYTE);
15840 specbind (Qinhibit_point_motion_hooks, Qt);
15842 buffer_unchanged_p
15843 = (w->window_end_valid
15844 && !current_buffer->clip_changed
15845 && !window_outdated (w));
15847 /* When windows_or_buffers_changed is non-zero, we can't rely
15848 on the window end being valid, so set it to zero there. */
15849 if (windows_or_buffers_changed)
15851 /* If window starts on a continuation line, maybe adjust the
15852 window start in case the window's width changed. */
15853 if (XMARKER (w->start)->buffer == current_buffer)
15854 compute_window_start_on_continuation_line (w);
15856 w->window_end_valid = false;
15857 /* If so, we also can't rely on current matrix
15858 and should not fool try_cursor_movement below. */
15859 current_matrix_up_to_date_p = false;
15862 /* Some sanity checks. */
15863 CHECK_WINDOW_END (w);
15864 if (Z == Z_BYTE && CHARPOS (opoint) != BYTEPOS (opoint))
15865 emacs_abort ();
15866 if (BYTEPOS (opoint) < CHARPOS (opoint))
15867 emacs_abort ();
15869 if (mode_line_update_needed (w))
15870 update_mode_line = 1;
15872 /* Point refers normally to the selected window. For any other
15873 window, set up appropriate value. */
15874 if (!EQ (window, selected_window))
15876 ptrdiff_t new_pt = marker_position (w->pointm);
15877 ptrdiff_t new_pt_byte = marker_byte_position (w->pointm);
15878 if (new_pt < BEGV)
15880 new_pt = BEGV;
15881 new_pt_byte = BEGV_BYTE;
15882 set_marker_both (w->pointm, Qnil, BEGV, BEGV_BYTE);
15884 else if (new_pt > (ZV - 1))
15886 new_pt = ZV;
15887 new_pt_byte = ZV_BYTE;
15888 set_marker_both (w->pointm, Qnil, ZV, ZV_BYTE);
15891 /* We don't use SET_PT so that the point-motion hooks don't run. */
15892 TEMP_SET_PT_BOTH (new_pt, new_pt_byte);
15895 /* If any of the character widths specified in the display table
15896 have changed, invalidate the width run cache. It's true that
15897 this may be a bit late to catch such changes, but the rest of
15898 redisplay goes (non-fatally) haywire when the display table is
15899 changed, so why should we worry about doing any better? */
15900 if (current_buffer->width_run_cache
15901 || (current_buffer->base_buffer
15902 && current_buffer->base_buffer->width_run_cache))
15904 struct Lisp_Char_Table *disptab = buffer_display_table ();
15906 if (! disptab_matches_widthtab
15907 (disptab, XVECTOR (BVAR (current_buffer, width_table))))
15909 struct buffer *buf = current_buffer;
15911 if (buf->base_buffer)
15912 buf = buf->base_buffer;
15913 invalidate_region_cache (buf, buf->width_run_cache, BEG, Z);
15914 recompute_width_table (current_buffer, disptab);
15918 /* If window-start is screwed up, choose a new one. */
15919 if (XMARKER (w->start)->buffer != current_buffer)
15920 goto recenter;
15922 SET_TEXT_POS_FROM_MARKER (startp, w->start);
15924 /* If someone specified a new starting point but did not insist,
15925 check whether it can be used. */
15926 if (w->optional_new_start
15927 && CHARPOS (startp) >= BEGV
15928 && CHARPOS (startp) <= ZV)
15930 w->optional_new_start = 0;
15931 start_display (&it, w, startp);
15932 move_it_to (&it, PT, 0, it.last_visible_y, -1,
15933 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
15934 if (IT_CHARPOS (it) == PT)
15935 w->force_start = 1;
15936 /* IT may overshoot PT if text at PT is invisible. */
15937 else if (IT_CHARPOS (it) > PT && CHARPOS (startp) <= PT)
15938 w->force_start = 1;
15941 force_start:
15943 /* Handle case where place to start displaying has been specified,
15944 unless the specified location is outside the accessible range. */
15945 if (w->force_start || window_frozen_p (w))
15947 /* We set this later on if we have to adjust point. */
15948 int new_vpos = -1;
15950 w->force_start = 0;
15951 w->vscroll = 0;
15952 w->window_end_valid = 0;
15954 /* Forget any recorded base line for line number display. */
15955 if (!buffer_unchanged_p)
15956 w->base_line_number = 0;
15958 /* Redisplay the mode line. Select the buffer properly for that.
15959 Also, run the hook window-scroll-functions
15960 because we have scrolled. */
15961 /* Note, we do this after clearing force_start because
15962 if there's an error, it is better to forget about force_start
15963 than to get into an infinite loop calling the hook functions
15964 and having them get more errors. */
15965 if (!update_mode_line
15966 || ! NILP (Vwindow_scroll_functions))
15968 update_mode_line = 1;
15969 w->update_mode_line = 1;
15970 startp = run_window_scroll_functions (window, startp);
15973 if (CHARPOS (startp) < BEGV)
15974 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
15975 else if (CHARPOS (startp) > ZV)
15976 SET_TEXT_POS (startp, ZV, ZV_BYTE);
15978 /* Redisplay, then check if cursor has been set during the
15979 redisplay. Give up if new fonts were loaded. */
15980 /* We used to issue a CHECK_MARGINS argument to try_window here,
15981 but this causes scrolling to fail when point begins inside
15982 the scroll margin (bug#148) -- cyd */
15983 if (!try_window (window, startp, 0))
15985 w->force_start = 1;
15986 clear_glyph_matrix (w->desired_matrix);
15987 goto need_larger_matrices;
15990 if (w->cursor.vpos < 0 && !window_frozen_p (w))
15992 /* If point does not appear, try to move point so it does
15993 appear. The desired matrix has been built above, so we
15994 can use it here. */
15995 new_vpos = window_box_height (w) / 2;
15998 if (!cursor_row_fully_visible_p (w, 0, 0))
16000 /* Point does appear, but on a line partly visible at end of window.
16001 Move it back to a fully-visible line. */
16002 new_vpos = window_box_height (w);
16004 else if (w->cursor.vpos >= 0)
16006 /* Some people insist on not letting point enter the scroll
16007 margin, even though this part handles windows that didn't
16008 scroll at all. */
16009 int window_total_lines
16010 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
16011 int margin = min (scroll_margin, window_total_lines / 4);
16012 int pixel_margin = margin * frame_line_height;
16013 bool header_line = WINDOW_WANTS_HEADER_LINE_P (w);
16015 /* Note: We add an extra FRAME_LINE_HEIGHT, because the loop
16016 below, which finds the row to move point to, advances by
16017 the Y coordinate of the _next_ row, see the definition of
16018 MATRIX_ROW_BOTTOM_Y. */
16019 if (w->cursor.vpos < margin + header_line)
16021 w->cursor.vpos = -1;
16022 clear_glyph_matrix (w->desired_matrix);
16023 goto try_to_scroll;
16025 else
16027 int window_height = window_box_height (w);
16029 if (header_line)
16030 window_height += CURRENT_HEADER_LINE_HEIGHT (w);
16031 if (w->cursor.y >= window_height - pixel_margin)
16033 w->cursor.vpos = -1;
16034 clear_glyph_matrix (w->desired_matrix);
16035 goto try_to_scroll;
16040 /* If we need to move point for either of the above reasons,
16041 now actually do it. */
16042 if (new_vpos >= 0)
16044 struct glyph_row *row;
16046 row = MATRIX_FIRST_TEXT_ROW (w->desired_matrix);
16047 while (MATRIX_ROW_BOTTOM_Y (row) < new_vpos)
16048 ++row;
16050 TEMP_SET_PT_BOTH (MATRIX_ROW_START_CHARPOS (row),
16051 MATRIX_ROW_START_BYTEPOS (row));
16053 if (w != XWINDOW (selected_window))
16054 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
16055 else if (current_buffer == old)
16056 SET_TEXT_POS (lpoint, PT, PT_BYTE);
16058 set_cursor_from_row (w, row, w->desired_matrix, 0, 0, 0, 0);
16060 /* If we are highlighting the region, then we just changed
16061 the region, so redisplay to show it. */
16062 /* FIXME: We need to (re)run pre-redisplay-function! */
16063 /* if (markpos_of_region () >= 0)
16065 clear_glyph_matrix (w->desired_matrix);
16066 if (!try_window (window, startp, 0))
16067 goto need_larger_matrices;
16072 #ifdef GLYPH_DEBUG
16073 debug_method_add (w, "forced window start");
16074 #endif
16075 goto done;
16078 /* Handle case where text has not changed, only point, and it has
16079 not moved off the frame, and we are not retrying after hscroll.
16080 (current_matrix_up_to_date_p is nonzero when retrying.) */
16081 if (current_matrix_up_to_date_p
16082 && (rc = try_cursor_movement (window, startp, &temp_scroll_step),
16083 rc != CURSOR_MOVEMENT_CANNOT_BE_USED))
16085 switch (rc)
16087 case CURSOR_MOVEMENT_SUCCESS:
16088 used_current_matrix_p = 1;
16089 goto done;
16091 case CURSOR_MOVEMENT_MUST_SCROLL:
16092 goto try_to_scroll;
16094 default:
16095 emacs_abort ();
16098 /* If current starting point was originally the beginning of a line
16099 but no longer is, find a new starting point. */
16100 else if (w->start_at_line_beg
16101 && !(CHARPOS (startp) <= BEGV
16102 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n'))
16104 #ifdef GLYPH_DEBUG
16105 debug_method_add (w, "recenter 1");
16106 #endif
16107 goto recenter;
16110 /* Try scrolling with try_window_id. Value is > 0 if update has
16111 been done, it is -1 if we know that the same window start will
16112 not work. It is 0 if unsuccessful for some other reason. */
16113 else if ((tem = try_window_id (w)) != 0)
16115 #ifdef GLYPH_DEBUG
16116 debug_method_add (w, "try_window_id %d", tem);
16117 #endif
16119 if (f->fonts_changed)
16120 goto need_larger_matrices;
16121 if (tem > 0)
16122 goto done;
16124 /* Otherwise try_window_id has returned -1 which means that we
16125 don't want the alternative below this comment to execute. */
16127 else if (CHARPOS (startp) >= BEGV
16128 && CHARPOS (startp) <= ZV
16129 && PT >= CHARPOS (startp)
16130 && (CHARPOS (startp) < ZV
16131 /* Avoid starting at end of buffer. */
16132 || CHARPOS (startp) == BEGV
16133 || !window_outdated (w)))
16135 int d1, d2, d3, d4, d5, d6;
16137 /* If first window line is a continuation line, and window start
16138 is inside the modified region, but the first change is before
16139 current window start, we must select a new window start.
16141 However, if this is the result of a down-mouse event (e.g. by
16142 extending the mouse-drag-overlay), we don't want to select a
16143 new window start, since that would change the position under
16144 the mouse, resulting in an unwanted mouse-movement rather
16145 than a simple mouse-click. */
16146 if (!w->start_at_line_beg
16147 && NILP (do_mouse_tracking)
16148 && CHARPOS (startp) > BEGV
16149 && CHARPOS (startp) > BEG + beg_unchanged
16150 && CHARPOS (startp) <= Z - end_unchanged
16151 /* Even if w->start_at_line_beg is nil, a new window may
16152 start at a line_beg, since that's how set_buffer_window
16153 sets it. So, we need to check the return value of
16154 compute_window_start_on_continuation_line. (See also
16155 bug#197). */
16156 && XMARKER (w->start)->buffer == current_buffer
16157 && compute_window_start_on_continuation_line (w)
16158 /* It doesn't make sense to force the window start like we
16159 do at label force_start if it is already known that point
16160 will not be visible in the resulting window, because
16161 doing so will move point from its correct position
16162 instead of scrolling the window to bring point into view.
16163 See bug#9324. */
16164 && pos_visible_p (w, PT, &d1, &d2, &d3, &d4, &d5, &d6))
16166 w->force_start = 1;
16167 SET_TEXT_POS_FROM_MARKER (startp, w->start);
16168 goto force_start;
16171 #ifdef GLYPH_DEBUG
16172 debug_method_add (w, "same window start");
16173 #endif
16175 /* Try to redisplay starting at same place as before.
16176 If point has not moved off frame, accept the results. */
16177 if (!current_matrix_up_to_date_p
16178 /* Don't use try_window_reusing_current_matrix in this case
16179 because a window scroll function can have changed the
16180 buffer. */
16181 || !NILP (Vwindow_scroll_functions)
16182 || MINI_WINDOW_P (w)
16183 || !(used_current_matrix_p
16184 = try_window_reusing_current_matrix (w)))
16186 IF_DEBUG (debug_method_add (w, "1"));
16187 if (try_window (window, startp, TRY_WINDOW_CHECK_MARGINS) < 0)
16188 /* -1 means we need to scroll.
16189 0 means we need new matrices, but fonts_changed
16190 is set in that case, so we will detect it below. */
16191 goto try_to_scroll;
16194 if (f->fonts_changed)
16195 goto need_larger_matrices;
16197 if (w->cursor.vpos >= 0)
16199 if (!just_this_one_p
16200 || current_buffer->clip_changed
16201 || BEG_UNCHANGED < CHARPOS (startp))
16202 /* Forget any recorded base line for line number display. */
16203 w->base_line_number = 0;
16205 if (!cursor_row_fully_visible_p (w, 1, 0))
16207 clear_glyph_matrix (w->desired_matrix);
16208 last_line_misfit = 1;
16210 /* Drop through and scroll. */
16211 else
16212 goto done;
16214 else
16215 clear_glyph_matrix (w->desired_matrix);
16218 try_to_scroll:
16220 /* Redisplay the mode line. Select the buffer properly for that. */
16221 if (!update_mode_line)
16223 update_mode_line = 1;
16224 w->update_mode_line = 1;
16227 /* Try to scroll by specified few lines. */
16228 if ((scroll_conservatively
16229 || emacs_scroll_step
16230 || temp_scroll_step
16231 || NUMBERP (BVAR (current_buffer, scroll_up_aggressively))
16232 || NUMBERP (BVAR (current_buffer, scroll_down_aggressively)))
16233 && CHARPOS (startp) >= BEGV
16234 && CHARPOS (startp) <= ZV)
16236 /* The function returns -1 if new fonts were loaded, 1 if
16237 successful, 0 if not successful. */
16238 int ss = try_scrolling (window, just_this_one_p,
16239 scroll_conservatively,
16240 emacs_scroll_step,
16241 temp_scroll_step, last_line_misfit);
16242 switch (ss)
16244 case SCROLLING_SUCCESS:
16245 goto done;
16247 case SCROLLING_NEED_LARGER_MATRICES:
16248 goto need_larger_matrices;
16250 case SCROLLING_FAILED:
16251 break;
16253 default:
16254 emacs_abort ();
16258 /* Finally, just choose a place to start which positions point
16259 according to user preferences. */
16261 recenter:
16263 #ifdef GLYPH_DEBUG
16264 debug_method_add (w, "recenter");
16265 #endif
16267 /* Forget any previously recorded base line for line number display. */
16268 if (!buffer_unchanged_p)
16269 w->base_line_number = 0;
16271 /* Determine the window start relative to point. */
16272 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
16273 it.current_y = it.last_visible_y;
16274 if (centering_position < 0)
16276 int window_total_lines
16277 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
16278 int margin =
16279 scroll_margin > 0
16280 ? min (scroll_margin, window_total_lines / 4)
16281 : 0;
16282 ptrdiff_t margin_pos = CHARPOS (startp);
16283 Lisp_Object aggressive;
16284 int scrolling_up;
16286 /* If there is a scroll margin at the top of the window, find
16287 its character position. */
16288 if (margin
16289 /* Cannot call start_display if startp is not in the
16290 accessible region of the buffer. This can happen when we
16291 have just switched to a different buffer and/or changed
16292 its restriction. In that case, startp is initialized to
16293 the character position 1 (BEGV) because we did not yet
16294 have chance to display the buffer even once. */
16295 && BEGV <= CHARPOS (startp) && CHARPOS (startp) <= ZV)
16297 struct it it1;
16298 void *it1data = NULL;
16300 SAVE_IT (it1, it, it1data);
16301 start_display (&it1, w, startp);
16302 move_it_vertically (&it1, margin * frame_line_height);
16303 margin_pos = IT_CHARPOS (it1);
16304 RESTORE_IT (&it, &it, it1data);
16306 scrolling_up = PT > margin_pos;
16307 aggressive =
16308 scrolling_up
16309 ? BVAR (current_buffer, scroll_up_aggressively)
16310 : BVAR (current_buffer, scroll_down_aggressively);
16312 if (!MINI_WINDOW_P (w)
16313 && (scroll_conservatively > SCROLL_LIMIT || NUMBERP (aggressive)))
16315 int pt_offset = 0;
16317 /* Setting scroll-conservatively overrides
16318 scroll-*-aggressively. */
16319 if (!scroll_conservatively && NUMBERP (aggressive))
16321 double float_amount = XFLOATINT (aggressive);
16323 pt_offset = float_amount * WINDOW_BOX_TEXT_HEIGHT (w);
16324 if (pt_offset == 0 && float_amount > 0)
16325 pt_offset = 1;
16326 if (pt_offset && margin > 0)
16327 margin -= 1;
16329 /* Compute how much to move the window start backward from
16330 point so that point will be displayed where the user
16331 wants it. */
16332 if (scrolling_up)
16334 centering_position = it.last_visible_y;
16335 if (pt_offset)
16336 centering_position -= pt_offset;
16337 centering_position -=
16338 frame_line_height * (1 + margin + (last_line_misfit != 0))
16339 + WINDOW_HEADER_LINE_HEIGHT (w);
16340 /* Don't let point enter the scroll margin near top of
16341 the window. */
16342 if (centering_position < margin * frame_line_height)
16343 centering_position = margin * frame_line_height;
16345 else
16346 centering_position = margin * frame_line_height + pt_offset;
16348 else
16349 /* Set the window start half the height of the window backward
16350 from point. */
16351 centering_position = window_box_height (w) / 2;
16353 move_it_vertically_backward (&it, centering_position);
16355 eassert (IT_CHARPOS (it) >= BEGV);
16357 /* The function move_it_vertically_backward may move over more
16358 than the specified y-distance. If it->w is small, e.g. a
16359 mini-buffer window, we may end up in front of the window's
16360 display area. Start displaying at the start of the line
16361 containing PT in this case. */
16362 if (it.current_y <= 0)
16364 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
16365 move_it_vertically_backward (&it, 0);
16366 it.current_y = 0;
16369 it.current_x = it.hpos = 0;
16371 /* Set the window start position here explicitly, to avoid an
16372 infinite loop in case the functions in window-scroll-functions
16373 get errors. */
16374 set_marker_both (w->start, Qnil, IT_CHARPOS (it), IT_BYTEPOS (it));
16376 /* Run scroll hooks. */
16377 startp = run_window_scroll_functions (window, it.current.pos);
16379 /* Redisplay the window. */
16380 if (!current_matrix_up_to_date_p
16381 || windows_or_buffers_changed
16382 || f->cursor_type_changed
16383 /* Don't use try_window_reusing_current_matrix in this case
16384 because it can have changed the buffer. */
16385 || !NILP (Vwindow_scroll_functions)
16386 || !just_this_one_p
16387 || MINI_WINDOW_P (w)
16388 || !(used_current_matrix_p
16389 = try_window_reusing_current_matrix (w)))
16390 try_window (window, startp, 0);
16392 /* If new fonts have been loaded (due to fontsets), give up. We
16393 have to start a new redisplay since we need to re-adjust glyph
16394 matrices. */
16395 if (f->fonts_changed)
16396 goto need_larger_matrices;
16398 /* If cursor did not appear assume that the middle of the window is
16399 in the first line of the window. Do it again with the next line.
16400 (Imagine a window of height 100, displaying two lines of height
16401 60. Moving back 50 from it->last_visible_y will end in the first
16402 line.) */
16403 if (w->cursor.vpos < 0)
16405 if (w->window_end_valid && PT >= Z - w->window_end_pos)
16407 clear_glyph_matrix (w->desired_matrix);
16408 move_it_by_lines (&it, 1);
16409 try_window (window, it.current.pos, 0);
16411 else if (PT < IT_CHARPOS (it))
16413 clear_glyph_matrix (w->desired_matrix);
16414 move_it_by_lines (&it, -1);
16415 try_window (window, it.current.pos, 0);
16417 else
16419 /* Not much we can do about it. */
16423 /* Consider the following case: Window starts at BEGV, there is
16424 invisible, intangible text at BEGV, so that display starts at
16425 some point START > BEGV. It can happen that we are called with
16426 PT somewhere between BEGV and START. Try to handle that case,
16427 and similar ones. */
16428 if (w->cursor.vpos < 0)
16430 /* First, try locating the proper glyph row for PT. */
16431 struct glyph_row *row =
16432 row_containing_pos (w, PT, w->current_matrix->rows, NULL, 0);
16434 /* Sometimes point is at the beginning of invisible text that is
16435 before the 1st character displayed in the row. In that case,
16436 row_containing_pos fails to find the row, because no glyphs
16437 with appropriate buffer positions are present in the row.
16438 Therefore, we next try to find the row which shows the 1st
16439 position after the invisible text. */
16440 if (!row)
16442 Lisp_Object val =
16443 get_char_property_and_overlay (make_number (PT), Qinvisible,
16444 Qnil, NULL);
16446 if (TEXT_PROP_MEANS_INVISIBLE (val))
16448 ptrdiff_t alt_pos;
16449 Lisp_Object invis_end =
16450 Fnext_single_char_property_change (make_number (PT), Qinvisible,
16451 Qnil, Qnil);
16453 if (NATNUMP (invis_end))
16454 alt_pos = XFASTINT (invis_end);
16455 else
16456 alt_pos = ZV;
16457 row = row_containing_pos (w, alt_pos, w->current_matrix->rows,
16458 NULL, 0);
16461 /* Finally, fall back on the first row of the window after the
16462 header line (if any). This is slightly better than not
16463 displaying the cursor at all. */
16464 if (!row)
16466 row = w->current_matrix->rows;
16467 if (row->mode_line_p)
16468 ++row;
16470 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
16473 if (!cursor_row_fully_visible_p (w, 0, 0))
16475 /* If vscroll is enabled, disable it and try again. */
16476 if (w->vscroll)
16478 w->vscroll = 0;
16479 clear_glyph_matrix (w->desired_matrix);
16480 goto recenter;
16483 /* Users who set scroll-conservatively to a large number want
16484 point just above/below the scroll margin. If we ended up
16485 with point's row partially visible, move the window start to
16486 make that row fully visible and out of the margin. */
16487 if (scroll_conservatively > SCROLL_LIMIT)
16489 int window_total_lines
16490 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) * frame_line_height;
16491 int margin =
16492 scroll_margin > 0
16493 ? min (scroll_margin, window_total_lines / 4)
16494 : 0;
16495 int move_down = w->cursor.vpos >= window_total_lines / 2;
16497 move_it_by_lines (&it, move_down ? margin + 1 : -(margin + 1));
16498 clear_glyph_matrix (w->desired_matrix);
16499 if (1 == try_window (window, it.current.pos,
16500 TRY_WINDOW_CHECK_MARGINS))
16501 goto done;
16504 /* If centering point failed to make the whole line visible,
16505 put point at the top instead. That has to make the whole line
16506 visible, if it can be done. */
16507 if (centering_position == 0)
16508 goto done;
16510 clear_glyph_matrix (w->desired_matrix);
16511 centering_position = 0;
16512 goto recenter;
16515 done:
16517 SET_TEXT_POS_FROM_MARKER (startp, w->start);
16518 w->start_at_line_beg = (CHARPOS (startp) == BEGV
16519 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n');
16521 /* Display the mode line, if we must. */
16522 if ((update_mode_line
16523 /* If window not full width, must redo its mode line
16524 if (a) the window to its side is being redone and
16525 (b) we do a frame-based redisplay. This is a consequence
16526 of how inverted lines are drawn in frame-based redisplay. */
16527 || (!just_this_one_p
16528 && !FRAME_WINDOW_P (f)
16529 && !WINDOW_FULL_WIDTH_P (w))
16530 /* Line number to display. */
16531 || w->base_line_pos > 0
16532 /* Column number is displayed and different from the one displayed. */
16533 || (w->column_number_displayed != -1
16534 && (w->column_number_displayed != current_column ())))
16535 /* This means that the window has a mode line. */
16536 && (WINDOW_WANTS_MODELINE_P (w)
16537 || WINDOW_WANTS_HEADER_LINE_P (w)))
16540 display_mode_lines (w);
16542 /* If mode line height has changed, arrange for a thorough
16543 immediate redisplay using the correct mode line height. */
16544 if (WINDOW_WANTS_MODELINE_P (w)
16545 && CURRENT_MODE_LINE_HEIGHT (w) != DESIRED_MODE_LINE_HEIGHT (w))
16547 f->fonts_changed = 1;
16548 w->mode_line_height = -1;
16549 MATRIX_MODE_LINE_ROW (w->current_matrix)->height
16550 = DESIRED_MODE_LINE_HEIGHT (w);
16553 /* If header line height has changed, arrange for a thorough
16554 immediate redisplay using the correct header line height. */
16555 if (WINDOW_WANTS_HEADER_LINE_P (w)
16556 && CURRENT_HEADER_LINE_HEIGHT (w) != DESIRED_HEADER_LINE_HEIGHT (w))
16558 f->fonts_changed = 1;
16559 w->header_line_height = -1;
16560 MATRIX_HEADER_LINE_ROW (w->current_matrix)->height
16561 = DESIRED_HEADER_LINE_HEIGHT (w);
16564 if (f->fonts_changed)
16565 goto need_larger_matrices;
16568 if (!line_number_displayed && w->base_line_pos != -1)
16570 w->base_line_pos = 0;
16571 w->base_line_number = 0;
16574 finish_menu_bars:
16576 /* When we reach a frame's selected window, redo the frame's menu bar. */
16577 if (update_mode_line
16578 && EQ (FRAME_SELECTED_WINDOW (f), window))
16580 int redisplay_menu_p = 0;
16582 if (FRAME_WINDOW_P (f))
16584 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
16585 || defined (HAVE_NS) || defined (USE_GTK)
16586 redisplay_menu_p = FRAME_EXTERNAL_MENU_BAR (f);
16587 #else
16588 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
16589 #endif
16591 else
16592 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
16594 if (redisplay_menu_p)
16595 display_menu_bar (w);
16597 #ifdef HAVE_WINDOW_SYSTEM
16598 if (FRAME_WINDOW_P (f))
16600 #if defined (USE_GTK) || defined (HAVE_NS)
16601 if (FRAME_EXTERNAL_TOOL_BAR (f))
16602 redisplay_tool_bar (f);
16603 #else
16604 if (WINDOWP (f->tool_bar_window)
16605 && (FRAME_TOOL_BAR_HEIGHT (f) > 0
16606 || !NILP (Vauto_resize_tool_bars))
16607 && redisplay_tool_bar (f))
16608 ignore_mouse_drag_p = 1;
16609 #endif
16611 #endif
16614 #ifdef HAVE_WINDOW_SYSTEM
16615 if (FRAME_WINDOW_P (f)
16616 && update_window_fringes (w, (just_this_one_p
16617 || (!used_current_matrix_p && !overlay_arrow_seen)
16618 || w->pseudo_window_p)))
16620 update_begin (f);
16621 block_input ();
16622 if (draw_window_fringes (w, 1))
16624 if (WINDOW_RIGHT_DIVIDER_WIDTH (w))
16625 x_draw_right_divider (w);
16626 else
16627 x_draw_vertical_border (w);
16629 unblock_input ();
16630 update_end (f);
16633 if (WINDOW_BOTTOM_DIVIDER_WIDTH (w))
16634 x_draw_bottom_divider (w);
16635 #endif /* HAVE_WINDOW_SYSTEM */
16637 /* We go to this label, with fonts_changed set, if it is
16638 necessary to try again using larger glyph matrices.
16639 We have to redeem the scroll bar even in this case,
16640 because the loop in redisplay_internal expects that. */
16641 need_larger_matrices:
16643 finish_scroll_bars:
16645 if (WINDOW_HAS_VERTICAL_SCROLL_BAR (w))
16647 /* Set the thumb's position and size. */
16648 set_vertical_scroll_bar (w);
16650 /* Note that we actually used the scroll bar attached to this
16651 window, so it shouldn't be deleted at the end of redisplay. */
16652 if (FRAME_TERMINAL (f)->redeem_scroll_bar_hook)
16653 (*FRAME_TERMINAL (f)->redeem_scroll_bar_hook) (w);
16656 /* Restore current_buffer and value of point in it. The window
16657 update may have changed the buffer, so first make sure `opoint'
16658 is still valid (Bug#6177). */
16659 if (CHARPOS (opoint) < BEGV)
16660 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
16661 else if (CHARPOS (opoint) > ZV)
16662 TEMP_SET_PT_BOTH (Z, Z_BYTE);
16663 else
16664 TEMP_SET_PT_BOTH (CHARPOS (opoint), BYTEPOS (opoint));
16666 set_buffer_internal_1 (old);
16667 /* Avoid an abort in TEMP_SET_PT_BOTH if the buffer has become
16668 shorter. This can be caused by log truncation in *Messages*. */
16669 if (CHARPOS (lpoint) <= ZV)
16670 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
16672 unbind_to (count, Qnil);
16676 /* Build the complete desired matrix of WINDOW with a window start
16677 buffer position POS.
16679 Value is 1 if successful. It is zero if fonts were loaded during
16680 redisplay which makes re-adjusting glyph matrices necessary, and -1
16681 if point would appear in the scroll margins.
16682 (We check the former only if TRY_WINDOW_IGNORE_FONTS_CHANGE is
16683 unset in FLAGS, and the latter only if TRY_WINDOW_CHECK_MARGINS is
16684 set in FLAGS.) */
16687 try_window (Lisp_Object window, struct text_pos pos, int flags)
16689 struct window *w = XWINDOW (window);
16690 struct it it;
16691 struct glyph_row *last_text_row = NULL;
16692 struct frame *f = XFRAME (w->frame);
16693 int frame_line_height = default_line_pixel_height (w);
16695 /* Make POS the new window start. */
16696 set_marker_both (w->start, Qnil, CHARPOS (pos), BYTEPOS (pos));
16698 /* Mark cursor position as unknown. No overlay arrow seen. */
16699 w->cursor.vpos = -1;
16700 overlay_arrow_seen = 0;
16702 /* Initialize iterator and info to start at POS. */
16703 start_display (&it, w, pos);
16705 /* Display all lines of W. */
16706 while (it.current_y < it.last_visible_y)
16708 if (display_line (&it))
16709 last_text_row = it.glyph_row - 1;
16710 if (f->fonts_changed && !(flags & TRY_WINDOW_IGNORE_FONTS_CHANGE))
16711 return 0;
16714 /* Don't let the cursor end in the scroll margins. */
16715 if ((flags & TRY_WINDOW_CHECK_MARGINS)
16716 && !MINI_WINDOW_P (w))
16718 int this_scroll_margin;
16719 int window_total_lines
16720 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
16722 if (scroll_margin > 0)
16724 this_scroll_margin = min (scroll_margin, window_total_lines / 4);
16725 this_scroll_margin *= frame_line_height;
16727 else
16728 this_scroll_margin = 0;
16730 if ((w->cursor.y >= 0 /* not vscrolled */
16731 && w->cursor.y < this_scroll_margin
16732 && CHARPOS (pos) > BEGV
16733 && IT_CHARPOS (it) < ZV)
16734 /* rms: considering make_cursor_line_fully_visible_p here
16735 seems to give wrong results. We don't want to recenter
16736 when the last line is partly visible, we want to allow
16737 that case to be handled in the usual way. */
16738 || w->cursor.y > it.last_visible_y - this_scroll_margin - 1)
16740 w->cursor.vpos = -1;
16741 clear_glyph_matrix (w->desired_matrix);
16742 return -1;
16746 /* If bottom moved off end of frame, change mode line percentage. */
16747 if (w->window_end_pos <= 0 && Z != IT_CHARPOS (it))
16748 w->update_mode_line = 1;
16750 /* Set window_end_pos to the offset of the last character displayed
16751 on the window from the end of current_buffer. Set
16752 window_end_vpos to its row number. */
16753 if (last_text_row)
16755 eassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_text_row));
16756 adjust_window_ends (w, last_text_row, 0);
16757 eassert
16758 (MATRIX_ROW_DISPLAYS_TEXT_P (MATRIX_ROW (w->desired_matrix,
16759 w->window_end_vpos)));
16761 else
16763 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
16764 w->window_end_pos = Z - ZV;
16765 w->window_end_vpos = 0;
16768 /* But that is not valid info until redisplay finishes. */
16769 w->window_end_valid = 0;
16770 return 1;
16775 /************************************************************************
16776 Window redisplay reusing current matrix when buffer has not changed
16777 ************************************************************************/
16779 /* Try redisplay of window W showing an unchanged buffer with a
16780 different window start than the last time it was displayed by
16781 reusing its current matrix. Value is non-zero if successful.
16782 W->start is the new window start. */
16784 static int
16785 try_window_reusing_current_matrix (struct window *w)
16787 struct frame *f = XFRAME (w->frame);
16788 struct glyph_row *bottom_row;
16789 struct it it;
16790 struct run run;
16791 struct text_pos start, new_start;
16792 int nrows_scrolled, i;
16793 struct glyph_row *last_text_row;
16794 struct glyph_row *last_reused_text_row;
16795 struct glyph_row *start_row;
16796 int start_vpos, min_y, max_y;
16798 #ifdef GLYPH_DEBUG
16799 if (inhibit_try_window_reusing)
16800 return 0;
16801 #endif
16803 if (/* This function doesn't handle terminal frames. */
16804 !FRAME_WINDOW_P (f)
16805 /* Don't try to reuse the display if windows have been split
16806 or such. */
16807 || windows_or_buffers_changed
16808 || f->cursor_type_changed)
16809 return 0;
16811 /* Can't do this if showing trailing whitespace. */
16812 if (!NILP (Vshow_trailing_whitespace))
16813 return 0;
16815 /* If top-line visibility has changed, give up. */
16816 if (WINDOW_WANTS_HEADER_LINE_P (w)
16817 != MATRIX_HEADER_LINE_ROW (w->current_matrix)->mode_line_p)
16818 return 0;
16820 /* Give up if old or new display is scrolled vertically. We could
16821 make this function handle this, but right now it doesn't. */
16822 start_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
16823 if (w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row))
16824 return 0;
16826 /* The variable new_start now holds the new window start. The old
16827 start `start' can be determined from the current matrix. */
16828 SET_TEXT_POS_FROM_MARKER (new_start, w->start);
16829 start = start_row->minpos;
16830 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
16832 /* Clear the desired matrix for the display below. */
16833 clear_glyph_matrix (w->desired_matrix);
16835 if (CHARPOS (new_start) <= CHARPOS (start))
16837 /* Don't use this method if the display starts with an ellipsis
16838 displayed for invisible text. It's not easy to handle that case
16839 below, and it's certainly not worth the effort since this is
16840 not a frequent case. */
16841 if (in_ellipses_for_invisible_text_p (&start_row->start, w))
16842 return 0;
16844 IF_DEBUG (debug_method_add (w, "twu1"));
16846 /* Display up to a row that can be reused. The variable
16847 last_text_row is set to the last row displayed that displays
16848 text. Note that it.vpos == 0 if or if not there is a
16849 header-line; it's not the same as the MATRIX_ROW_VPOS! */
16850 start_display (&it, w, new_start);
16851 w->cursor.vpos = -1;
16852 last_text_row = last_reused_text_row = NULL;
16854 while (it.current_y < it.last_visible_y && !f->fonts_changed)
16856 /* If we have reached into the characters in the START row,
16857 that means the line boundaries have changed. So we
16858 can't start copying with the row START. Maybe it will
16859 work to start copying with the following row. */
16860 while (IT_CHARPOS (it) > CHARPOS (start))
16862 /* Advance to the next row as the "start". */
16863 start_row++;
16864 start = start_row->minpos;
16865 /* If there are no more rows to try, or just one, give up. */
16866 if (start_row == MATRIX_MODE_LINE_ROW (w->current_matrix) - 1
16867 || w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row)
16868 || CHARPOS (start) == ZV)
16870 clear_glyph_matrix (w->desired_matrix);
16871 return 0;
16874 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
16876 /* If we have reached alignment, we can copy the rest of the
16877 rows. */
16878 if (IT_CHARPOS (it) == CHARPOS (start)
16879 /* Don't accept "alignment" inside a display vector,
16880 since start_row could have started in the middle of
16881 that same display vector (thus their character
16882 positions match), and we have no way of telling if
16883 that is the case. */
16884 && it.current.dpvec_index < 0)
16885 break;
16887 if (display_line (&it))
16888 last_text_row = it.glyph_row - 1;
16892 /* A value of current_y < last_visible_y means that we stopped
16893 at the previous window start, which in turn means that we
16894 have at least one reusable row. */
16895 if (it.current_y < it.last_visible_y)
16897 struct glyph_row *row;
16899 /* IT.vpos always starts from 0; it counts text lines. */
16900 nrows_scrolled = it.vpos - (start_row - MATRIX_FIRST_TEXT_ROW (w->current_matrix));
16902 /* Find PT if not already found in the lines displayed. */
16903 if (w->cursor.vpos < 0)
16905 int dy = it.current_y - start_row->y;
16907 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
16908 row = row_containing_pos (w, PT, row, NULL, dy);
16909 if (row)
16910 set_cursor_from_row (w, row, w->current_matrix, 0, 0,
16911 dy, nrows_scrolled);
16912 else
16914 clear_glyph_matrix (w->desired_matrix);
16915 return 0;
16919 /* Scroll the display. Do it before the current matrix is
16920 changed. The problem here is that update has not yet
16921 run, i.e. part of the current matrix is not up to date.
16922 scroll_run_hook will clear the cursor, and use the
16923 current matrix to get the height of the row the cursor is
16924 in. */
16925 run.current_y = start_row->y;
16926 run.desired_y = it.current_y;
16927 run.height = it.last_visible_y - it.current_y;
16929 if (run.height > 0 && run.current_y != run.desired_y)
16931 update_begin (f);
16932 FRAME_RIF (f)->update_window_begin_hook (w);
16933 FRAME_RIF (f)->clear_window_mouse_face (w);
16934 FRAME_RIF (f)->scroll_run_hook (w, &run);
16935 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
16936 update_end (f);
16939 /* Shift current matrix down by nrows_scrolled lines. */
16940 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
16941 rotate_matrix (w->current_matrix,
16942 start_vpos,
16943 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
16944 nrows_scrolled);
16946 /* Disable lines that must be updated. */
16947 for (i = 0; i < nrows_scrolled; ++i)
16948 (start_row + i)->enabled_p = false;
16950 /* Re-compute Y positions. */
16951 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
16952 max_y = it.last_visible_y;
16953 for (row = start_row + nrows_scrolled;
16954 row < bottom_row;
16955 ++row)
16957 row->y = it.current_y;
16958 row->visible_height = row->height;
16960 if (row->y < min_y)
16961 row->visible_height -= min_y - row->y;
16962 if (row->y + row->height > max_y)
16963 row->visible_height -= row->y + row->height - max_y;
16964 if (row->fringe_bitmap_periodic_p)
16965 row->redraw_fringe_bitmaps_p = 1;
16967 it.current_y += row->height;
16969 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
16970 last_reused_text_row = row;
16971 if (MATRIX_ROW_BOTTOM_Y (row) >= it.last_visible_y)
16972 break;
16975 /* Disable lines in the current matrix which are now
16976 below the window. */
16977 for (++row; row < bottom_row; ++row)
16978 row->enabled_p = row->mode_line_p = 0;
16981 /* Update window_end_pos etc.; last_reused_text_row is the last
16982 reused row from the current matrix containing text, if any.
16983 The value of last_text_row is the last displayed line
16984 containing text. */
16985 if (last_reused_text_row)
16986 adjust_window_ends (w, last_reused_text_row, 1);
16987 else if (last_text_row)
16988 adjust_window_ends (w, last_text_row, 0);
16989 else
16991 /* This window must be completely empty. */
16992 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
16993 w->window_end_pos = Z - ZV;
16994 w->window_end_vpos = 0;
16996 w->window_end_valid = 0;
16998 /* Update hint: don't try scrolling again in update_window. */
16999 w->desired_matrix->no_scrolling_p = 1;
17001 #ifdef GLYPH_DEBUG
17002 debug_method_add (w, "try_window_reusing_current_matrix 1");
17003 #endif
17004 return 1;
17006 else if (CHARPOS (new_start) > CHARPOS (start))
17008 struct glyph_row *pt_row, *row;
17009 struct glyph_row *first_reusable_row;
17010 struct glyph_row *first_row_to_display;
17011 int dy;
17012 int yb = window_text_bottom_y (w);
17014 /* Find the row starting at new_start, if there is one. Don't
17015 reuse a partially visible line at the end. */
17016 first_reusable_row = start_row;
17017 while (first_reusable_row->enabled_p
17018 && MATRIX_ROW_BOTTOM_Y (first_reusable_row) < yb
17019 && (MATRIX_ROW_START_CHARPOS (first_reusable_row)
17020 < CHARPOS (new_start)))
17021 ++first_reusable_row;
17023 /* Give up if there is no row to reuse. */
17024 if (MATRIX_ROW_BOTTOM_Y (first_reusable_row) >= yb
17025 || !first_reusable_row->enabled_p
17026 || (MATRIX_ROW_START_CHARPOS (first_reusable_row)
17027 != CHARPOS (new_start)))
17028 return 0;
17030 /* We can reuse fully visible rows beginning with
17031 first_reusable_row to the end of the window. Set
17032 first_row_to_display to the first row that cannot be reused.
17033 Set pt_row to the row containing point, if there is any. */
17034 pt_row = NULL;
17035 for (first_row_to_display = first_reusable_row;
17036 MATRIX_ROW_BOTTOM_Y (first_row_to_display) < yb;
17037 ++first_row_to_display)
17039 if (PT >= MATRIX_ROW_START_CHARPOS (first_row_to_display)
17040 && (PT < MATRIX_ROW_END_CHARPOS (first_row_to_display)
17041 || (PT == MATRIX_ROW_END_CHARPOS (first_row_to_display)
17042 && first_row_to_display->ends_at_zv_p
17043 && pt_row == NULL)))
17044 pt_row = first_row_to_display;
17047 /* Start displaying at the start of first_row_to_display. */
17048 eassert (first_row_to_display->y < yb);
17049 init_to_row_start (&it, w, first_row_to_display);
17051 nrows_scrolled = (MATRIX_ROW_VPOS (first_reusable_row, w->current_matrix)
17052 - start_vpos);
17053 it.vpos = (MATRIX_ROW_VPOS (first_row_to_display, w->current_matrix)
17054 - nrows_scrolled);
17055 it.current_y = (first_row_to_display->y - first_reusable_row->y
17056 + WINDOW_HEADER_LINE_HEIGHT (w));
17058 /* Display lines beginning with first_row_to_display in the
17059 desired matrix. Set last_text_row to the last row displayed
17060 that displays text. */
17061 it.glyph_row = MATRIX_ROW (w->desired_matrix, it.vpos);
17062 if (pt_row == NULL)
17063 w->cursor.vpos = -1;
17064 last_text_row = NULL;
17065 while (it.current_y < it.last_visible_y && !f->fonts_changed)
17066 if (display_line (&it))
17067 last_text_row = it.glyph_row - 1;
17069 /* If point is in a reused row, adjust y and vpos of the cursor
17070 position. */
17071 if (pt_row)
17073 w->cursor.vpos -= nrows_scrolled;
17074 w->cursor.y -= first_reusable_row->y - start_row->y;
17077 /* Give up if point isn't in a row displayed or reused. (This
17078 also handles the case where w->cursor.vpos < nrows_scrolled
17079 after the calls to display_line, which can happen with scroll
17080 margins. See bug#1295.) */
17081 if (w->cursor.vpos < 0)
17083 clear_glyph_matrix (w->desired_matrix);
17084 return 0;
17087 /* Scroll the display. */
17088 run.current_y = first_reusable_row->y;
17089 run.desired_y = WINDOW_HEADER_LINE_HEIGHT (w);
17090 run.height = it.last_visible_y - run.current_y;
17091 dy = run.current_y - run.desired_y;
17093 if (run.height)
17095 update_begin (f);
17096 FRAME_RIF (f)->update_window_begin_hook (w);
17097 FRAME_RIF (f)->clear_window_mouse_face (w);
17098 FRAME_RIF (f)->scroll_run_hook (w, &run);
17099 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
17100 update_end (f);
17103 /* Adjust Y positions of reused rows. */
17104 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
17105 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
17106 max_y = it.last_visible_y;
17107 for (row = first_reusable_row; row < first_row_to_display; ++row)
17109 row->y -= dy;
17110 row->visible_height = row->height;
17111 if (row->y < min_y)
17112 row->visible_height -= min_y - row->y;
17113 if (row->y + row->height > max_y)
17114 row->visible_height -= row->y + row->height - max_y;
17115 if (row->fringe_bitmap_periodic_p)
17116 row->redraw_fringe_bitmaps_p = 1;
17119 /* Scroll the current matrix. */
17120 eassert (nrows_scrolled > 0);
17121 rotate_matrix (w->current_matrix,
17122 start_vpos,
17123 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
17124 -nrows_scrolled);
17126 /* Disable rows not reused. */
17127 for (row -= nrows_scrolled; row < bottom_row; ++row)
17128 row->enabled_p = false;
17130 /* Point may have moved to a different line, so we cannot assume that
17131 the previous cursor position is valid; locate the correct row. */
17132 if (pt_row)
17134 for (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
17135 row < bottom_row
17136 && PT >= MATRIX_ROW_END_CHARPOS (row)
17137 && !row->ends_at_zv_p;
17138 row++)
17140 w->cursor.vpos++;
17141 w->cursor.y = row->y;
17143 if (row < bottom_row)
17145 /* Can't simply scan the row for point with
17146 bidi-reordered glyph rows. Let set_cursor_from_row
17147 figure out where to put the cursor, and if it fails,
17148 give up. */
17149 if (!NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering)))
17151 if (!set_cursor_from_row (w, row, w->current_matrix,
17152 0, 0, 0, 0))
17154 clear_glyph_matrix (w->desired_matrix);
17155 return 0;
17158 else
17160 struct glyph *glyph = row->glyphs[TEXT_AREA] + w->cursor.hpos;
17161 struct glyph *end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
17163 for (; glyph < end
17164 && (!BUFFERP (glyph->object)
17165 || glyph->charpos < PT);
17166 glyph++)
17168 w->cursor.hpos++;
17169 w->cursor.x += glyph->pixel_width;
17175 /* Adjust window end. A null value of last_text_row means that
17176 the window end is in reused rows which in turn means that
17177 only its vpos can have changed. */
17178 if (last_text_row)
17179 adjust_window_ends (w, last_text_row, 0);
17180 else
17181 w->window_end_vpos -= nrows_scrolled;
17183 w->window_end_valid = 0;
17184 w->desired_matrix->no_scrolling_p = 1;
17186 #ifdef GLYPH_DEBUG
17187 debug_method_add (w, "try_window_reusing_current_matrix 2");
17188 #endif
17189 return 1;
17192 return 0;
17197 /************************************************************************
17198 Window redisplay reusing current matrix when buffer has changed
17199 ************************************************************************/
17201 static struct glyph_row *find_last_unchanged_at_beg_row (struct window *);
17202 static struct glyph_row *find_first_unchanged_at_end_row (struct window *,
17203 ptrdiff_t *, ptrdiff_t *);
17204 static struct glyph_row *
17205 find_last_row_displaying_text (struct glyph_matrix *, struct it *,
17206 struct glyph_row *);
17209 /* Return the last row in MATRIX displaying text. If row START is
17210 non-null, start searching with that row. IT gives the dimensions
17211 of the display. Value is null if matrix is empty; otherwise it is
17212 a pointer to the row found. */
17214 static struct glyph_row *
17215 find_last_row_displaying_text (struct glyph_matrix *matrix, struct it *it,
17216 struct glyph_row *start)
17218 struct glyph_row *row, *row_found;
17220 /* Set row_found to the last row in IT->w's current matrix
17221 displaying text. The loop looks funny but think of partially
17222 visible lines. */
17223 row_found = NULL;
17224 row = start ? start : MATRIX_FIRST_TEXT_ROW (matrix);
17225 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
17227 eassert (row->enabled_p);
17228 row_found = row;
17229 if (MATRIX_ROW_BOTTOM_Y (row) >= it->last_visible_y)
17230 break;
17231 ++row;
17234 return row_found;
17238 /* Return the last row in the current matrix of W that is not affected
17239 by changes at the start of current_buffer that occurred since W's
17240 current matrix was built. Value is null if no such row exists.
17242 BEG_UNCHANGED us the number of characters unchanged at the start of
17243 current_buffer. BEG + BEG_UNCHANGED is the buffer position of the
17244 first changed character in current_buffer. Characters at positions <
17245 BEG + BEG_UNCHANGED are at the same buffer positions as they were
17246 when the current matrix was built. */
17248 static struct glyph_row *
17249 find_last_unchanged_at_beg_row (struct window *w)
17251 ptrdiff_t first_changed_pos = BEG + BEG_UNCHANGED;
17252 struct glyph_row *row;
17253 struct glyph_row *row_found = NULL;
17254 int yb = window_text_bottom_y (w);
17256 /* Find the last row displaying unchanged text. */
17257 for (row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
17258 MATRIX_ROW_DISPLAYS_TEXT_P (row)
17259 && MATRIX_ROW_START_CHARPOS (row) < first_changed_pos;
17260 ++row)
17262 if (/* If row ends before first_changed_pos, it is unchanged,
17263 except in some case. */
17264 MATRIX_ROW_END_CHARPOS (row) <= first_changed_pos
17265 /* When row ends in ZV and we write at ZV it is not
17266 unchanged. */
17267 && !row->ends_at_zv_p
17268 /* When first_changed_pos is the end of a continued line,
17269 row is not unchanged because it may be no longer
17270 continued. */
17271 && !(MATRIX_ROW_END_CHARPOS (row) == first_changed_pos
17272 && (row->continued_p
17273 || row->exact_window_width_line_p))
17274 /* If ROW->end is beyond ZV, then ROW->end is outdated and
17275 needs to be recomputed, so don't consider this row as
17276 unchanged. This happens when the last line was
17277 bidi-reordered and was killed immediately before this
17278 redisplay cycle. In that case, ROW->end stores the
17279 buffer position of the first visual-order character of
17280 the killed text, which is now beyond ZV. */
17281 && CHARPOS (row->end.pos) <= ZV)
17282 row_found = row;
17284 /* Stop if last visible row. */
17285 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
17286 break;
17289 return row_found;
17293 /* Find the first glyph row in the current matrix of W that is not
17294 affected by changes at the end of current_buffer since the
17295 time W's current matrix was built.
17297 Return in *DELTA the number of chars by which buffer positions in
17298 unchanged text at the end of current_buffer must be adjusted.
17300 Return in *DELTA_BYTES the corresponding number of bytes.
17302 Value is null if no such row exists, i.e. all rows are affected by
17303 changes. */
17305 static struct glyph_row *
17306 find_first_unchanged_at_end_row (struct window *w,
17307 ptrdiff_t *delta, ptrdiff_t *delta_bytes)
17309 struct glyph_row *row;
17310 struct glyph_row *row_found = NULL;
17312 *delta = *delta_bytes = 0;
17314 /* Display must not have been paused, otherwise the current matrix
17315 is not up to date. */
17316 eassert (w->window_end_valid);
17318 /* A value of window_end_pos >= END_UNCHANGED means that the window
17319 end is in the range of changed text. If so, there is no
17320 unchanged row at the end of W's current matrix. */
17321 if (w->window_end_pos >= END_UNCHANGED)
17322 return NULL;
17324 /* Set row to the last row in W's current matrix displaying text. */
17325 row = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
17327 /* If matrix is entirely empty, no unchanged row exists. */
17328 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
17330 /* The value of row is the last glyph row in the matrix having a
17331 meaningful buffer position in it. The end position of row
17332 corresponds to window_end_pos. This allows us to translate
17333 buffer positions in the current matrix to current buffer
17334 positions for characters not in changed text. */
17335 ptrdiff_t Z_old =
17336 MATRIX_ROW_END_CHARPOS (row) + w->window_end_pos;
17337 ptrdiff_t Z_BYTE_old =
17338 MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
17339 ptrdiff_t last_unchanged_pos, last_unchanged_pos_old;
17340 struct glyph_row *first_text_row
17341 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
17343 *delta = Z - Z_old;
17344 *delta_bytes = Z_BYTE - Z_BYTE_old;
17346 /* Set last_unchanged_pos to the buffer position of the last
17347 character in the buffer that has not been changed. Z is the
17348 index + 1 of the last character in current_buffer, i.e. by
17349 subtracting END_UNCHANGED we get the index of the last
17350 unchanged character, and we have to add BEG to get its buffer
17351 position. */
17352 last_unchanged_pos = Z - END_UNCHANGED + BEG;
17353 last_unchanged_pos_old = last_unchanged_pos - *delta;
17355 /* Search backward from ROW for a row displaying a line that
17356 starts at a minimum position >= last_unchanged_pos_old. */
17357 for (; row > first_text_row; --row)
17359 /* This used to abort, but it can happen.
17360 It is ok to just stop the search instead here. KFS. */
17361 if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
17362 break;
17364 if (MATRIX_ROW_START_CHARPOS (row) >= last_unchanged_pos_old)
17365 row_found = row;
17369 eassert (!row_found || MATRIX_ROW_DISPLAYS_TEXT_P (row_found));
17371 return row_found;
17375 /* Make sure that glyph rows in the current matrix of window W
17376 reference the same glyph memory as corresponding rows in the
17377 frame's frame matrix. This function is called after scrolling W's
17378 current matrix on a terminal frame in try_window_id and
17379 try_window_reusing_current_matrix. */
17381 static void
17382 sync_frame_with_window_matrix_rows (struct window *w)
17384 struct frame *f = XFRAME (w->frame);
17385 struct glyph_row *window_row, *window_row_end, *frame_row;
17387 /* Preconditions: W must be a leaf window and full-width. Its frame
17388 must have a frame matrix. */
17389 eassert (BUFFERP (w->contents));
17390 eassert (WINDOW_FULL_WIDTH_P (w));
17391 eassert (!FRAME_WINDOW_P (f));
17393 /* If W is a full-width window, glyph pointers in W's current matrix
17394 have, by definition, to be the same as glyph pointers in the
17395 corresponding frame matrix. Note that frame matrices have no
17396 marginal areas (see build_frame_matrix). */
17397 window_row = w->current_matrix->rows;
17398 window_row_end = window_row + w->current_matrix->nrows;
17399 frame_row = f->current_matrix->rows + WINDOW_TOP_EDGE_LINE (w);
17400 while (window_row < window_row_end)
17402 struct glyph *start = window_row->glyphs[LEFT_MARGIN_AREA];
17403 struct glyph *end = window_row->glyphs[LAST_AREA];
17405 frame_row->glyphs[LEFT_MARGIN_AREA] = start;
17406 frame_row->glyphs[TEXT_AREA] = start;
17407 frame_row->glyphs[RIGHT_MARGIN_AREA] = end;
17408 frame_row->glyphs[LAST_AREA] = end;
17410 /* Disable frame rows whose corresponding window rows have
17411 been disabled in try_window_id. */
17412 if (!window_row->enabled_p)
17413 frame_row->enabled_p = false;
17415 ++window_row, ++frame_row;
17420 /* Find the glyph row in window W containing CHARPOS. Consider all
17421 rows between START and END (not inclusive). END null means search
17422 all rows to the end of the display area of W. Value is the row
17423 containing CHARPOS or null. */
17425 struct glyph_row *
17426 row_containing_pos (struct window *w, ptrdiff_t charpos,
17427 struct glyph_row *start, struct glyph_row *end, int dy)
17429 struct glyph_row *row = start;
17430 struct glyph_row *best_row = NULL;
17431 ptrdiff_t mindif = BUF_ZV (XBUFFER (w->contents)) + 1;
17432 int last_y;
17434 /* If we happen to start on a header-line, skip that. */
17435 if (row->mode_line_p)
17436 ++row;
17438 if ((end && row >= end) || !row->enabled_p)
17439 return NULL;
17441 last_y = window_text_bottom_y (w) - dy;
17443 while (1)
17445 /* Give up if we have gone too far. */
17446 if (end && row >= end)
17447 return NULL;
17448 /* This formerly returned if they were equal.
17449 I think that both quantities are of a "last plus one" type;
17450 if so, when they are equal, the row is within the screen. -- rms. */
17451 if (MATRIX_ROW_BOTTOM_Y (row) > last_y)
17452 return NULL;
17454 /* If it is in this row, return this row. */
17455 if (! (MATRIX_ROW_END_CHARPOS (row) < charpos
17456 || (MATRIX_ROW_END_CHARPOS (row) == charpos
17457 /* The end position of a row equals the start
17458 position of the next row. If CHARPOS is there, we
17459 would rather consider it displayed in the next
17460 line, except when this line ends in ZV. */
17461 && !row_for_charpos_p (row, charpos)))
17462 && charpos >= MATRIX_ROW_START_CHARPOS (row))
17464 struct glyph *g;
17466 if (NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering))
17467 || (!best_row && !row->continued_p))
17468 return row;
17469 /* In bidi-reordered rows, there could be several rows whose
17470 edges surround CHARPOS, all of these rows belonging to
17471 the same continued line. We need to find the row which
17472 fits CHARPOS the best. */
17473 for (g = row->glyphs[TEXT_AREA];
17474 g < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
17475 g++)
17477 if (!STRINGP (g->object))
17479 if (g->charpos > 0 && eabs (g->charpos - charpos) < mindif)
17481 mindif = eabs (g->charpos - charpos);
17482 best_row = row;
17483 /* Exact match always wins. */
17484 if (mindif == 0)
17485 return best_row;
17490 else if (best_row && !row->continued_p)
17491 return best_row;
17492 ++row;
17497 /* Try to redisplay window W by reusing its existing display. W's
17498 current matrix must be up to date when this function is called,
17499 i.e. window_end_valid must be nonzero.
17501 Value is
17503 >= 1 if successful, i.e. display has been updated
17504 specifically:
17505 1 means the changes were in front of a newline that precedes
17506 the window start, and the whole current matrix was reused
17507 2 means the changes were after the last position displayed
17508 in the window, and the whole current matrix was reused
17509 3 means portions of the current matrix were reused, while
17510 some of the screen lines were redrawn
17511 -1 if redisplay with same window start is known not to succeed
17512 0 if otherwise unsuccessful
17514 The following steps are performed:
17516 1. Find the last row in the current matrix of W that is not
17517 affected by changes at the start of current_buffer. If no such row
17518 is found, give up.
17520 2. Find the first row in W's current matrix that is not affected by
17521 changes at the end of current_buffer. Maybe there is no such row.
17523 3. Display lines beginning with the row + 1 found in step 1 to the
17524 row found in step 2 or, if step 2 didn't find a row, to the end of
17525 the window.
17527 4. If cursor is not known to appear on the window, give up.
17529 5. If display stopped at the row found in step 2, scroll the
17530 display and current matrix as needed.
17532 6. Maybe display some lines at the end of W, if we must. This can
17533 happen under various circumstances, like a partially visible line
17534 becoming fully visible, or because newly displayed lines are displayed
17535 in smaller font sizes.
17537 7. Update W's window end information. */
17539 static int
17540 try_window_id (struct window *w)
17542 struct frame *f = XFRAME (w->frame);
17543 struct glyph_matrix *current_matrix = w->current_matrix;
17544 struct glyph_matrix *desired_matrix = w->desired_matrix;
17545 struct glyph_row *last_unchanged_at_beg_row;
17546 struct glyph_row *first_unchanged_at_end_row;
17547 struct glyph_row *row;
17548 struct glyph_row *bottom_row;
17549 int bottom_vpos;
17550 struct it it;
17551 ptrdiff_t delta = 0, delta_bytes = 0, stop_pos;
17552 int dvpos, dy;
17553 struct text_pos start_pos;
17554 struct run run;
17555 int first_unchanged_at_end_vpos = 0;
17556 struct glyph_row *last_text_row, *last_text_row_at_end;
17557 struct text_pos start;
17558 ptrdiff_t first_changed_charpos, last_changed_charpos;
17560 #ifdef GLYPH_DEBUG
17561 if (inhibit_try_window_id)
17562 return 0;
17563 #endif
17565 /* This is handy for debugging. */
17566 #if 0
17567 #define GIVE_UP(X) \
17568 do { \
17569 fprintf (stderr, "try_window_id give up %d\n", (X)); \
17570 return 0; \
17571 } while (0)
17572 #else
17573 #define GIVE_UP(X) return 0
17574 #endif
17576 SET_TEXT_POS_FROM_MARKER (start, w->start);
17578 /* Don't use this for mini-windows because these can show
17579 messages and mini-buffers, and we don't handle that here. */
17580 if (MINI_WINDOW_P (w))
17581 GIVE_UP (1);
17583 /* This flag is used to prevent redisplay optimizations. */
17584 if (windows_or_buffers_changed || f->cursor_type_changed)
17585 GIVE_UP (2);
17587 /* This function's optimizations cannot be used if overlays have
17588 changed in the buffer displayed by the window, so give up if they
17589 have. */
17590 if (w->last_overlay_modified != OVERLAY_MODIFF)
17591 GIVE_UP (21);
17593 /* Verify that narrowing has not changed.
17594 Also verify that we were not told to prevent redisplay optimizations.
17595 It would be nice to further
17596 reduce the number of cases where this prevents try_window_id. */
17597 if (current_buffer->clip_changed
17598 || current_buffer->prevent_redisplay_optimizations_p)
17599 GIVE_UP (3);
17601 /* Window must either use window-based redisplay or be full width. */
17602 if (!FRAME_WINDOW_P (f)
17603 && (!FRAME_LINE_INS_DEL_OK (f)
17604 || !WINDOW_FULL_WIDTH_P (w)))
17605 GIVE_UP (4);
17607 /* Give up if point is known NOT to appear in W. */
17608 if (PT < CHARPOS (start))
17609 GIVE_UP (5);
17611 /* Another way to prevent redisplay optimizations. */
17612 if (w->last_modified == 0)
17613 GIVE_UP (6);
17615 /* Verify that window is not hscrolled. */
17616 if (w->hscroll != 0)
17617 GIVE_UP (7);
17619 /* Verify that display wasn't paused. */
17620 if (!w->window_end_valid)
17621 GIVE_UP (8);
17623 /* Likewise if highlighting trailing whitespace. */
17624 if (!NILP (Vshow_trailing_whitespace))
17625 GIVE_UP (11);
17627 /* Can't use this if overlay arrow position and/or string have
17628 changed. */
17629 if (overlay_arrows_changed_p ())
17630 GIVE_UP (12);
17632 /* When word-wrap is on, adding a space to the first word of a
17633 wrapped line can change the wrap position, altering the line
17634 above it. It might be worthwhile to handle this more
17635 intelligently, but for now just redisplay from scratch. */
17636 if (!NILP (BVAR (XBUFFER (w->contents), word_wrap)))
17637 GIVE_UP (21);
17639 /* Under bidi reordering, adding or deleting a character in the
17640 beginning of a paragraph, before the first strong directional
17641 character, can change the base direction of the paragraph (unless
17642 the buffer specifies a fixed paragraph direction), which will
17643 require to redisplay the whole paragraph. It might be worthwhile
17644 to find the paragraph limits and widen the range of redisplayed
17645 lines to that, but for now just give up this optimization and
17646 redisplay from scratch. */
17647 if (!NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering))
17648 && NILP (BVAR (XBUFFER (w->contents), bidi_paragraph_direction)))
17649 GIVE_UP (22);
17651 /* Make sure beg_unchanged and end_unchanged are up to date. Do it
17652 only if buffer has really changed. The reason is that the gap is
17653 initially at Z for freshly visited files. The code below would
17654 set end_unchanged to 0 in that case. */
17655 if (MODIFF > SAVE_MODIFF
17656 /* This seems to happen sometimes after saving a buffer. */
17657 || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
17659 if (GPT - BEG < BEG_UNCHANGED)
17660 BEG_UNCHANGED = GPT - BEG;
17661 if (Z - GPT < END_UNCHANGED)
17662 END_UNCHANGED = Z - GPT;
17665 /* The position of the first and last character that has been changed. */
17666 first_changed_charpos = BEG + BEG_UNCHANGED;
17667 last_changed_charpos = Z - END_UNCHANGED;
17669 /* If window starts after a line end, and the last change is in
17670 front of that newline, then changes don't affect the display.
17671 This case happens with stealth-fontification. Note that although
17672 the display is unchanged, glyph positions in the matrix have to
17673 be adjusted, of course. */
17674 row = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
17675 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
17676 && ((last_changed_charpos < CHARPOS (start)
17677 && CHARPOS (start) == BEGV)
17678 || (last_changed_charpos < CHARPOS (start) - 1
17679 && FETCH_BYTE (BYTEPOS (start) - 1) == '\n')))
17681 ptrdiff_t Z_old, Z_delta, Z_BYTE_old, Z_delta_bytes;
17682 struct glyph_row *r0;
17684 /* Compute how many chars/bytes have been added to or removed
17685 from the buffer. */
17686 Z_old = MATRIX_ROW_END_CHARPOS (row) + w->window_end_pos;
17687 Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
17688 Z_delta = Z - Z_old;
17689 Z_delta_bytes = Z_BYTE - Z_BYTE_old;
17691 /* Give up if PT is not in the window. Note that it already has
17692 been checked at the start of try_window_id that PT is not in
17693 front of the window start. */
17694 if (PT >= MATRIX_ROW_END_CHARPOS (row) + Z_delta)
17695 GIVE_UP (13);
17697 /* If window start is unchanged, we can reuse the whole matrix
17698 as is, after adjusting glyph positions. No need to compute
17699 the window end again, since its offset from Z hasn't changed. */
17700 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
17701 if (CHARPOS (start) == MATRIX_ROW_START_CHARPOS (r0) + Z_delta
17702 && BYTEPOS (start) == MATRIX_ROW_START_BYTEPOS (r0) + Z_delta_bytes
17703 /* PT must not be in a partially visible line. */
17704 && !(PT >= MATRIX_ROW_START_CHARPOS (row) + Z_delta
17705 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
17707 /* Adjust positions in the glyph matrix. */
17708 if (Z_delta || Z_delta_bytes)
17710 struct glyph_row *r1
17711 = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
17712 increment_matrix_positions (w->current_matrix,
17713 MATRIX_ROW_VPOS (r0, current_matrix),
17714 MATRIX_ROW_VPOS (r1, current_matrix),
17715 Z_delta, Z_delta_bytes);
17718 /* Set the cursor. */
17719 row = row_containing_pos (w, PT, r0, NULL, 0);
17720 if (row)
17721 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
17722 return 1;
17726 /* Handle the case that changes are all below what is displayed in
17727 the window, and that PT is in the window. This shortcut cannot
17728 be taken if ZV is visible in the window, and text has been added
17729 there that is visible in the window. */
17730 if (first_changed_charpos >= MATRIX_ROW_END_CHARPOS (row)
17731 /* ZV is not visible in the window, or there are no
17732 changes at ZV, actually. */
17733 && (current_matrix->zv > MATRIX_ROW_END_CHARPOS (row)
17734 || first_changed_charpos == last_changed_charpos))
17736 struct glyph_row *r0;
17738 /* Give up if PT is not in the window. Note that it already has
17739 been checked at the start of try_window_id that PT is not in
17740 front of the window start. */
17741 if (PT >= MATRIX_ROW_END_CHARPOS (row))
17742 GIVE_UP (14);
17744 /* If window start is unchanged, we can reuse the whole matrix
17745 as is, without changing glyph positions since no text has
17746 been added/removed in front of the window end. */
17747 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
17748 if (TEXT_POS_EQUAL_P (start, r0->minpos)
17749 /* PT must not be in a partially visible line. */
17750 && !(PT >= MATRIX_ROW_START_CHARPOS (row)
17751 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
17753 /* We have to compute the window end anew since text
17754 could have been added/removed after it. */
17755 w->window_end_pos = Z - MATRIX_ROW_END_CHARPOS (row);
17756 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
17758 /* Set the cursor. */
17759 row = row_containing_pos (w, PT, r0, NULL, 0);
17760 if (row)
17761 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
17762 return 2;
17766 /* Give up if window start is in the changed area.
17768 The condition used to read
17770 (BEG_UNCHANGED + END_UNCHANGED != Z - BEG && ...)
17772 but why that was tested escapes me at the moment. */
17773 if (CHARPOS (start) >= first_changed_charpos
17774 && CHARPOS (start) <= last_changed_charpos)
17775 GIVE_UP (15);
17777 /* Check that window start agrees with the start of the first glyph
17778 row in its current matrix. Check this after we know the window
17779 start is not in changed text, otherwise positions would not be
17780 comparable. */
17781 row = MATRIX_FIRST_TEXT_ROW (current_matrix);
17782 if (!TEXT_POS_EQUAL_P (start, row->minpos))
17783 GIVE_UP (16);
17785 /* Give up if the window ends in strings. Overlay strings
17786 at the end are difficult to handle, so don't try. */
17787 row = MATRIX_ROW (current_matrix, w->window_end_vpos);
17788 if (MATRIX_ROW_START_CHARPOS (row) == MATRIX_ROW_END_CHARPOS (row))
17789 GIVE_UP (20);
17791 /* Compute the position at which we have to start displaying new
17792 lines. Some of the lines at the top of the window might be
17793 reusable because they are not displaying changed text. Find the
17794 last row in W's current matrix not affected by changes at the
17795 start of current_buffer. Value is null if changes start in the
17796 first line of window. */
17797 last_unchanged_at_beg_row = find_last_unchanged_at_beg_row (w);
17798 if (last_unchanged_at_beg_row)
17800 /* Avoid starting to display in the middle of a character, a TAB
17801 for instance. This is easier than to set up the iterator
17802 exactly, and it's not a frequent case, so the additional
17803 effort wouldn't really pay off. */
17804 while ((MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row)
17805 || last_unchanged_at_beg_row->ends_in_newline_from_string_p)
17806 && last_unchanged_at_beg_row > w->current_matrix->rows)
17807 --last_unchanged_at_beg_row;
17809 if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row))
17810 GIVE_UP (17);
17812 if (init_to_row_end (&it, w, last_unchanged_at_beg_row) == 0)
17813 GIVE_UP (18);
17814 start_pos = it.current.pos;
17816 /* Start displaying new lines in the desired matrix at the same
17817 vpos we would use in the current matrix, i.e. below
17818 last_unchanged_at_beg_row. */
17819 it.vpos = 1 + MATRIX_ROW_VPOS (last_unchanged_at_beg_row,
17820 current_matrix);
17821 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
17822 it.current_y = MATRIX_ROW_BOTTOM_Y (last_unchanged_at_beg_row);
17824 eassert (it.hpos == 0 && it.current_x == 0);
17826 else
17828 /* There are no reusable lines at the start of the window.
17829 Start displaying in the first text line. */
17830 start_display (&it, w, start);
17831 it.vpos = it.first_vpos;
17832 start_pos = it.current.pos;
17835 /* Find the first row that is not affected by changes at the end of
17836 the buffer. Value will be null if there is no unchanged row, in
17837 which case we must redisplay to the end of the window. delta
17838 will be set to the value by which buffer positions beginning with
17839 first_unchanged_at_end_row have to be adjusted due to text
17840 changes. */
17841 first_unchanged_at_end_row
17842 = find_first_unchanged_at_end_row (w, &delta, &delta_bytes);
17843 IF_DEBUG (debug_delta = delta);
17844 IF_DEBUG (debug_delta_bytes = delta_bytes);
17846 /* Set stop_pos to the buffer position up to which we will have to
17847 display new lines. If first_unchanged_at_end_row != NULL, this
17848 is the buffer position of the start of the line displayed in that
17849 row. For first_unchanged_at_end_row == NULL, use 0 to indicate
17850 that we don't stop at a buffer position. */
17851 stop_pos = 0;
17852 if (first_unchanged_at_end_row)
17854 eassert (last_unchanged_at_beg_row == NULL
17855 || first_unchanged_at_end_row >= last_unchanged_at_beg_row);
17857 /* If this is a continuation line, move forward to the next one
17858 that isn't. Changes in lines above affect this line.
17859 Caution: this may move first_unchanged_at_end_row to a row
17860 not displaying text. */
17861 while (MATRIX_ROW_CONTINUATION_LINE_P (first_unchanged_at_end_row)
17862 && MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
17863 && (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
17864 < it.last_visible_y))
17865 ++first_unchanged_at_end_row;
17867 if (!MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
17868 || (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
17869 >= it.last_visible_y))
17870 first_unchanged_at_end_row = NULL;
17871 else
17873 stop_pos = (MATRIX_ROW_START_CHARPOS (first_unchanged_at_end_row)
17874 + delta);
17875 first_unchanged_at_end_vpos
17876 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, current_matrix);
17877 eassert (stop_pos >= Z - END_UNCHANGED);
17880 else if (last_unchanged_at_beg_row == NULL)
17881 GIVE_UP (19);
17884 #ifdef GLYPH_DEBUG
17886 /* Either there is no unchanged row at the end, or the one we have
17887 now displays text. This is a necessary condition for the window
17888 end pos calculation at the end of this function. */
17889 eassert (first_unchanged_at_end_row == NULL
17890 || MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
17892 debug_last_unchanged_at_beg_vpos
17893 = (last_unchanged_at_beg_row
17894 ? MATRIX_ROW_VPOS (last_unchanged_at_beg_row, current_matrix)
17895 : -1);
17896 debug_first_unchanged_at_end_vpos = first_unchanged_at_end_vpos;
17898 #endif /* GLYPH_DEBUG */
17901 /* Display new lines. Set last_text_row to the last new line
17902 displayed which has text on it, i.e. might end up as being the
17903 line where the window_end_vpos is. */
17904 w->cursor.vpos = -1;
17905 last_text_row = NULL;
17906 overlay_arrow_seen = 0;
17907 while (it.current_y < it.last_visible_y
17908 && !f->fonts_changed
17909 && (first_unchanged_at_end_row == NULL
17910 || IT_CHARPOS (it) < stop_pos))
17912 if (display_line (&it))
17913 last_text_row = it.glyph_row - 1;
17916 if (f->fonts_changed)
17917 return -1;
17920 /* Compute differences in buffer positions, y-positions etc. for
17921 lines reused at the bottom of the window. Compute what we can
17922 scroll. */
17923 if (first_unchanged_at_end_row
17924 /* No lines reused because we displayed everything up to the
17925 bottom of the window. */
17926 && it.current_y < it.last_visible_y)
17928 dvpos = (it.vpos
17929 - MATRIX_ROW_VPOS (first_unchanged_at_end_row,
17930 current_matrix));
17931 dy = it.current_y - first_unchanged_at_end_row->y;
17932 run.current_y = first_unchanged_at_end_row->y;
17933 run.desired_y = run.current_y + dy;
17934 run.height = it.last_visible_y - max (run.current_y, run.desired_y);
17936 else
17938 delta = delta_bytes = dvpos = dy
17939 = run.current_y = run.desired_y = run.height = 0;
17940 first_unchanged_at_end_row = NULL;
17942 IF_DEBUG ((debug_dvpos = dvpos, debug_dy = dy));
17945 /* Find the cursor if not already found. We have to decide whether
17946 PT will appear on this window (it sometimes doesn't, but this is
17947 not a very frequent case.) This decision has to be made before
17948 the current matrix is altered. A value of cursor.vpos < 0 means
17949 that PT is either in one of the lines beginning at
17950 first_unchanged_at_end_row or below the window. Don't care for
17951 lines that might be displayed later at the window end; as
17952 mentioned, this is not a frequent case. */
17953 if (w->cursor.vpos < 0)
17955 /* Cursor in unchanged rows at the top? */
17956 if (PT < CHARPOS (start_pos)
17957 && last_unchanged_at_beg_row)
17959 row = row_containing_pos (w, PT,
17960 MATRIX_FIRST_TEXT_ROW (w->current_matrix),
17961 last_unchanged_at_beg_row + 1, 0);
17962 if (row)
17963 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
17966 /* Start from first_unchanged_at_end_row looking for PT. */
17967 else if (first_unchanged_at_end_row)
17969 row = row_containing_pos (w, PT - delta,
17970 first_unchanged_at_end_row, NULL, 0);
17971 if (row)
17972 set_cursor_from_row (w, row, w->current_matrix, delta,
17973 delta_bytes, dy, dvpos);
17976 /* Give up if cursor was not found. */
17977 if (w->cursor.vpos < 0)
17979 clear_glyph_matrix (w->desired_matrix);
17980 return -1;
17984 /* Don't let the cursor end in the scroll margins. */
17986 int this_scroll_margin, cursor_height;
17987 int frame_line_height = default_line_pixel_height (w);
17988 int window_total_lines
17989 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (it.f) / frame_line_height;
17991 this_scroll_margin =
17992 max (0, min (scroll_margin, window_total_lines / 4));
17993 this_scroll_margin *= frame_line_height;
17994 cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
17996 if ((w->cursor.y < this_scroll_margin
17997 && CHARPOS (start) > BEGV)
17998 /* Old redisplay didn't take scroll margin into account at the bottom,
17999 but then global-hl-line-mode doesn't scroll. KFS 2004-06-14 */
18000 || (w->cursor.y + (make_cursor_line_fully_visible_p
18001 ? cursor_height + this_scroll_margin
18002 : 1)) > it.last_visible_y)
18004 w->cursor.vpos = -1;
18005 clear_glyph_matrix (w->desired_matrix);
18006 return -1;
18010 /* Scroll the display. Do it before changing the current matrix so
18011 that xterm.c doesn't get confused about where the cursor glyph is
18012 found. */
18013 if (dy && run.height)
18015 update_begin (f);
18017 if (FRAME_WINDOW_P (f))
18019 FRAME_RIF (f)->update_window_begin_hook (w);
18020 FRAME_RIF (f)->clear_window_mouse_face (w);
18021 FRAME_RIF (f)->scroll_run_hook (w, &run);
18022 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
18024 else
18026 /* Terminal frame. In this case, dvpos gives the number of
18027 lines to scroll by; dvpos < 0 means scroll up. */
18028 int from_vpos
18029 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, w->current_matrix);
18030 int from = WINDOW_TOP_EDGE_LINE (w) + from_vpos;
18031 int end = (WINDOW_TOP_EDGE_LINE (w)
18032 + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0)
18033 + window_internal_height (w));
18035 #if defined (HAVE_GPM) || defined (MSDOS)
18036 x_clear_window_mouse_face (w);
18037 #endif
18038 /* Perform the operation on the screen. */
18039 if (dvpos > 0)
18041 /* Scroll last_unchanged_at_beg_row to the end of the
18042 window down dvpos lines. */
18043 set_terminal_window (f, end);
18045 /* On dumb terminals delete dvpos lines at the end
18046 before inserting dvpos empty lines. */
18047 if (!FRAME_SCROLL_REGION_OK (f))
18048 ins_del_lines (f, end - dvpos, -dvpos);
18050 /* Insert dvpos empty lines in front of
18051 last_unchanged_at_beg_row. */
18052 ins_del_lines (f, from, dvpos);
18054 else if (dvpos < 0)
18056 /* Scroll up last_unchanged_at_beg_vpos to the end of
18057 the window to last_unchanged_at_beg_vpos - |dvpos|. */
18058 set_terminal_window (f, end);
18060 /* Delete dvpos lines in front of
18061 last_unchanged_at_beg_vpos. ins_del_lines will set
18062 the cursor to the given vpos and emit |dvpos| delete
18063 line sequences. */
18064 ins_del_lines (f, from + dvpos, dvpos);
18066 /* On a dumb terminal insert dvpos empty lines at the
18067 end. */
18068 if (!FRAME_SCROLL_REGION_OK (f))
18069 ins_del_lines (f, end + dvpos, -dvpos);
18072 set_terminal_window (f, 0);
18075 update_end (f);
18078 /* Shift reused rows of the current matrix to the right position.
18079 BOTTOM_ROW is the last + 1 row in the current matrix reserved for
18080 text. */
18081 bottom_row = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
18082 bottom_vpos = MATRIX_ROW_VPOS (bottom_row, current_matrix);
18083 if (dvpos < 0)
18085 rotate_matrix (current_matrix, first_unchanged_at_end_vpos + dvpos,
18086 bottom_vpos, dvpos);
18087 clear_glyph_matrix_rows (current_matrix, bottom_vpos + dvpos,
18088 bottom_vpos);
18090 else if (dvpos > 0)
18092 rotate_matrix (current_matrix, first_unchanged_at_end_vpos,
18093 bottom_vpos, dvpos);
18094 clear_glyph_matrix_rows (current_matrix, first_unchanged_at_end_vpos,
18095 first_unchanged_at_end_vpos + dvpos);
18098 /* For frame-based redisplay, make sure that current frame and window
18099 matrix are in sync with respect to glyph memory. */
18100 if (!FRAME_WINDOW_P (f))
18101 sync_frame_with_window_matrix_rows (w);
18103 /* Adjust buffer positions in reused rows. */
18104 if (delta || delta_bytes)
18105 increment_matrix_positions (current_matrix,
18106 first_unchanged_at_end_vpos + dvpos,
18107 bottom_vpos, delta, delta_bytes);
18109 /* Adjust Y positions. */
18110 if (dy)
18111 shift_glyph_matrix (w, current_matrix,
18112 first_unchanged_at_end_vpos + dvpos,
18113 bottom_vpos, dy);
18115 if (first_unchanged_at_end_row)
18117 first_unchanged_at_end_row += dvpos;
18118 if (first_unchanged_at_end_row->y >= it.last_visible_y
18119 || !MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row))
18120 first_unchanged_at_end_row = NULL;
18123 /* If scrolling up, there may be some lines to display at the end of
18124 the window. */
18125 last_text_row_at_end = NULL;
18126 if (dy < 0)
18128 /* Scrolling up can leave for example a partially visible line
18129 at the end of the window to be redisplayed. */
18130 /* Set last_row to the glyph row in the current matrix where the
18131 window end line is found. It has been moved up or down in
18132 the matrix by dvpos. */
18133 int last_vpos = w->window_end_vpos + dvpos;
18134 struct glyph_row *last_row = MATRIX_ROW (current_matrix, last_vpos);
18136 /* If last_row is the window end line, it should display text. */
18137 eassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_row));
18139 /* If window end line was partially visible before, begin
18140 displaying at that line. Otherwise begin displaying with the
18141 line following it. */
18142 if (MATRIX_ROW_BOTTOM_Y (last_row) - dy >= it.last_visible_y)
18144 init_to_row_start (&it, w, last_row);
18145 it.vpos = last_vpos;
18146 it.current_y = last_row->y;
18148 else
18150 init_to_row_end (&it, w, last_row);
18151 it.vpos = 1 + last_vpos;
18152 it.current_y = MATRIX_ROW_BOTTOM_Y (last_row);
18153 ++last_row;
18156 /* We may start in a continuation line. If so, we have to
18157 get the right continuation_lines_width and current_x. */
18158 it.continuation_lines_width = last_row->continuation_lines_width;
18159 it.hpos = it.current_x = 0;
18161 /* Display the rest of the lines at the window end. */
18162 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
18163 while (it.current_y < it.last_visible_y && !f->fonts_changed)
18165 /* Is it always sure that the display agrees with lines in
18166 the current matrix? I don't think so, so we mark rows
18167 displayed invalid in the current matrix by setting their
18168 enabled_p flag to zero. */
18169 SET_MATRIX_ROW_ENABLED_P (w->current_matrix, it.vpos, false);
18170 if (display_line (&it))
18171 last_text_row_at_end = it.glyph_row - 1;
18175 /* Update window_end_pos and window_end_vpos. */
18176 if (first_unchanged_at_end_row && !last_text_row_at_end)
18178 /* Window end line if one of the preserved rows from the current
18179 matrix. Set row to the last row displaying text in current
18180 matrix starting at first_unchanged_at_end_row, after
18181 scrolling. */
18182 eassert (MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
18183 row = find_last_row_displaying_text (w->current_matrix, &it,
18184 first_unchanged_at_end_row);
18185 eassert (row && MATRIX_ROW_DISPLAYS_TEXT_P (row));
18186 adjust_window_ends (w, row, 1);
18187 eassert (w->window_end_bytepos >= 0);
18188 IF_DEBUG (debug_method_add (w, "A"));
18190 else if (last_text_row_at_end)
18192 adjust_window_ends (w, last_text_row_at_end, 0);
18193 eassert (w->window_end_bytepos >= 0);
18194 IF_DEBUG (debug_method_add (w, "B"));
18196 else if (last_text_row)
18198 /* We have displayed either to the end of the window or at the
18199 end of the window, i.e. the last row with text is to be found
18200 in the desired matrix. */
18201 adjust_window_ends (w, last_text_row, 0);
18202 eassert (w->window_end_bytepos >= 0);
18204 else if (first_unchanged_at_end_row == NULL
18205 && last_text_row == NULL
18206 && last_text_row_at_end == NULL)
18208 /* Displayed to end of window, but no line containing text was
18209 displayed. Lines were deleted at the end of the window. */
18210 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
18211 int vpos = w->window_end_vpos;
18212 struct glyph_row *current_row = current_matrix->rows + vpos;
18213 struct glyph_row *desired_row = desired_matrix->rows + vpos;
18215 for (row = NULL;
18216 row == NULL && vpos >= first_vpos;
18217 --vpos, --current_row, --desired_row)
18219 if (desired_row->enabled_p)
18221 if (MATRIX_ROW_DISPLAYS_TEXT_P (desired_row))
18222 row = desired_row;
18224 else if (MATRIX_ROW_DISPLAYS_TEXT_P (current_row))
18225 row = current_row;
18228 eassert (row != NULL);
18229 w->window_end_vpos = vpos + 1;
18230 w->window_end_pos = Z - MATRIX_ROW_END_CHARPOS (row);
18231 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
18232 eassert (w->window_end_bytepos >= 0);
18233 IF_DEBUG (debug_method_add (w, "C"));
18235 else
18236 emacs_abort ();
18238 IF_DEBUG ((debug_end_pos = w->window_end_pos,
18239 debug_end_vpos = w->window_end_vpos));
18241 /* Record that display has not been completed. */
18242 w->window_end_valid = 0;
18243 w->desired_matrix->no_scrolling_p = 1;
18244 return 3;
18246 #undef GIVE_UP
18251 /***********************************************************************
18252 More debugging support
18253 ***********************************************************************/
18255 #ifdef GLYPH_DEBUG
18257 void dump_glyph_row (struct glyph_row *, int, int) EXTERNALLY_VISIBLE;
18258 void dump_glyph_matrix (struct glyph_matrix *, int) EXTERNALLY_VISIBLE;
18259 void dump_glyph (struct glyph_row *, struct glyph *, int) EXTERNALLY_VISIBLE;
18262 /* Dump the contents of glyph matrix MATRIX on stderr.
18264 GLYPHS 0 means don't show glyph contents.
18265 GLYPHS 1 means show glyphs in short form
18266 GLYPHS > 1 means show glyphs in long form. */
18268 void
18269 dump_glyph_matrix (struct glyph_matrix *matrix, int glyphs)
18271 int i;
18272 for (i = 0; i < matrix->nrows; ++i)
18273 dump_glyph_row (MATRIX_ROW (matrix, i), i, glyphs);
18277 /* Dump contents of glyph GLYPH to stderr. ROW and AREA are
18278 the glyph row and area where the glyph comes from. */
18280 void
18281 dump_glyph (struct glyph_row *row, struct glyph *glyph, int area)
18283 if (glyph->type == CHAR_GLYPH
18284 || glyph->type == GLYPHLESS_GLYPH)
18286 fprintf (stderr,
18287 " %5"pD"d %c %9"pI"d %c %3d 0x%06x %c %4d %1.1d%1.1d\n",
18288 glyph - row->glyphs[TEXT_AREA],
18289 (glyph->type == CHAR_GLYPH
18290 ? 'C'
18291 : 'G'),
18292 glyph->charpos,
18293 (BUFFERP (glyph->object)
18294 ? 'B'
18295 : (STRINGP (glyph->object)
18296 ? 'S'
18297 : (INTEGERP (glyph->object)
18298 ? '0'
18299 : '-'))),
18300 glyph->pixel_width,
18301 glyph->u.ch,
18302 (glyph->u.ch < 0x80 && glyph->u.ch >= ' '
18303 ? glyph->u.ch
18304 : '.'),
18305 glyph->face_id,
18306 glyph->left_box_line_p,
18307 glyph->right_box_line_p);
18309 else if (glyph->type == STRETCH_GLYPH)
18311 fprintf (stderr,
18312 " %5"pD"d %c %9"pI"d %c %3d 0x%06x %c %4d %1.1d%1.1d\n",
18313 glyph - row->glyphs[TEXT_AREA],
18314 'S',
18315 glyph->charpos,
18316 (BUFFERP (glyph->object)
18317 ? 'B'
18318 : (STRINGP (glyph->object)
18319 ? 'S'
18320 : (INTEGERP (glyph->object)
18321 ? '0'
18322 : '-'))),
18323 glyph->pixel_width,
18325 ' ',
18326 glyph->face_id,
18327 glyph->left_box_line_p,
18328 glyph->right_box_line_p);
18330 else if (glyph->type == IMAGE_GLYPH)
18332 fprintf (stderr,
18333 " %5"pD"d %c %9"pI"d %c %3d 0x%06x %c %4d %1.1d%1.1d\n",
18334 glyph - row->glyphs[TEXT_AREA],
18335 'I',
18336 glyph->charpos,
18337 (BUFFERP (glyph->object)
18338 ? 'B'
18339 : (STRINGP (glyph->object)
18340 ? 'S'
18341 : (INTEGERP (glyph->object)
18342 ? '0'
18343 : '-'))),
18344 glyph->pixel_width,
18345 glyph->u.img_id,
18346 '.',
18347 glyph->face_id,
18348 glyph->left_box_line_p,
18349 glyph->right_box_line_p);
18351 else if (glyph->type == COMPOSITE_GLYPH)
18353 fprintf (stderr,
18354 " %5"pD"d %c %9"pI"d %c %3d 0x%06x",
18355 glyph - row->glyphs[TEXT_AREA],
18356 '+',
18357 glyph->charpos,
18358 (BUFFERP (glyph->object)
18359 ? 'B'
18360 : (STRINGP (glyph->object)
18361 ? 'S'
18362 : (INTEGERP (glyph->object)
18363 ? '0'
18364 : '-'))),
18365 glyph->pixel_width,
18366 glyph->u.cmp.id);
18367 if (glyph->u.cmp.automatic)
18368 fprintf (stderr,
18369 "[%d-%d]",
18370 glyph->slice.cmp.from, glyph->slice.cmp.to);
18371 fprintf (stderr, " . %4d %1.1d%1.1d\n",
18372 glyph->face_id,
18373 glyph->left_box_line_p,
18374 glyph->right_box_line_p);
18379 /* Dump the contents of glyph row at VPOS in MATRIX to stderr.
18380 GLYPHS 0 means don't show glyph contents.
18381 GLYPHS 1 means show glyphs in short form
18382 GLYPHS > 1 means show glyphs in long form. */
18384 void
18385 dump_glyph_row (struct glyph_row *row, int vpos, int glyphs)
18387 if (glyphs != 1)
18389 fprintf (stderr, "Row Start End Used oE><\\CTZFesm X Y W H V A P\n");
18390 fprintf (stderr, "==============================================================================\n");
18392 fprintf (stderr, "%3d %9"pI"d %9"pI"d %4d %1.1d%1.1d%1.1d%1.1d\
18393 %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d %4d %4d %4d %4d %4d %4d %4d\n",
18394 vpos,
18395 MATRIX_ROW_START_CHARPOS (row),
18396 MATRIX_ROW_END_CHARPOS (row),
18397 row->used[TEXT_AREA],
18398 row->contains_overlapping_glyphs_p,
18399 row->enabled_p,
18400 row->truncated_on_left_p,
18401 row->truncated_on_right_p,
18402 row->continued_p,
18403 MATRIX_ROW_CONTINUATION_LINE_P (row),
18404 MATRIX_ROW_DISPLAYS_TEXT_P (row),
18405 row->ends_at_zv_p,
18406 row->fill_line_p,
18407 row->ends_in_middle_of_char_p,
18408 row->starts_in_middle_of_char_p,
18409 row->mouse_face_p,
18410 row->x,
18411 row->y,
18412 row->pixel_width,
18413 row->height,
18414 row->visible_height,
18415 row->ascent,
18416 row->phys_ascent);
18417 /* The next 3 lines should align to "Start" in the header. */
18418 fprintf (stderr, " %9"pD"d %9"pD"d\t%5d\n", row->start.overlay_string_index,
18419 row->end.overlay_string_index,
18420 row->continuation_lines_width);
18421 fprintf (stderr, " %9"pI"d %9"pI"d\n",
18422 CHARPOS (row->start.string_pos),
18423 CHARPOS (row->end.string_pos));
18424 fprintf (stderr, " %9d %9d\n", row->start.dpvec_index,
18425 row->end.dpvec_index);
18428 if (glyphs > 1)
18430 int area;
18432 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
18434 struct glyph *glyph = row->glyphs[area];
18435 struct glyph *glyph_end = glyph + row->used[area];
18437 /* Glyph for a line end in text. */
18438 if (area == TEXT_AREA && glyph == glyph_end && glyph->charpos > 0)
18439 ++glyph_end;
18441 if (glyph < glyph_end)
18442 fprintf (stderr, " Glyph# Type Pos O W Code C Face LR\n");
18444 for (; glyph < glyph_end; ++glyph)
18445 dump_glyph (row, glyph, area);
18448 else if (glyphs == 1)
18450 int area;
18452 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
18454 char *s = alloca (row->used[area] + 4);
18455 int i;
18457 for (i = 0; i < row->used[area]; ++i)
18459 struct glyph *glyph = row->glyphs[area] + i;
18460 if (i == row->used[area] - 1
18461 && area == TEXT_AREA
18462 && INTEGERP (glyph->object)
18463 && glyph->type == CHAR_GLYPH
18464 && glyph->u.ch == ' ')
18466 strcpy (&s[i], "[\\n]");
18467 i += 4;
18469 else if (glyph->type == CHAR_GLYPH
18470 && glyph->u.ch < 0x80
18471 && glyph->u.ch >= ' ')
18472 s[i] = glyph->u.ch;
18473 else
18474 s[i] = '.';
18477 s[i] = '\0';
18478 fprintf (stderr, "%3d: (%d) '%s'\n", vpos, row->enabled_p, s);
18484 DEFUN ("dump-glyph-matrix", Fdump_glyph_matrix,
18485 Sdump_glyph_matrix, 0, 1, "p",
18486 doc: /* Dump the current matrix of the selected window to stderr.
18487 Shows contents of glyph row structures. With non-nil
18488 parameter GLYPHS, dump glyphs as well. If GLYPHS is 1 show
18489 glyphs in short form, otherwise show glyphs in long form. */)
18490 (Lisp_Object glyphs)
18492 struct window *w = XWINDOW (selected_window);
18493 struct buffer *buffer = XBUFFER (w->contents);
18495 fprintf (stderr, "PT = %"pI"d, BEGV = %"pI"d. ZV = %"pI"d\n",
18496 BUF_PT (buffer), BUF_BEGV (buffer), BUF_ZV (buffer));
18497 fprintf (stderr, "Cursor x = %d, y = %d, hpos = %d, vpos = %d\n",
18498 w->cursor.x, w->cursor.y, w->cursor.hpos, w->cursor.vpos);
18499 fprintf (stderr, "=============================================\n");
18500 dump_glyph_matrix (w->current_matrix,
18501 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 0);
18502 return Qnil;
18506 DEFUN ("dump-frame-glyph-matrix", Fdump_frame_glyph_matrix,
18507 Sdump_frame_glyph_matrix, 0, 0, "", doc: /* */)
18508 (void)
18510 struct frame *f = XFRAME (selected_frame);
18511 dump_glyph_matrix (f->current_matrix, 1);
18512 return Qnil;
18516 DEFUN ("dump-glyph-row", Fdump_glyph_row, Sdump_glyph_row, 1, 2, "",
18517 doc: /* Dump glyph row ROW to stderr.
18518 GLYPH 0 means don't dump glyphs.
18519 GLYPH 1 means dump glyphs in short form.
18520 GLYPH > 1 or omitted means dump glyphs in long form. */)
18521 (Lisp_Object row, Lisp_Object glyphs)
18523 struct glyph_matrix *matrix;
18524 EMACS_INT vpos;
18526 CHECK_NUMBER (row);
18527 matrix = XWINDOW (selected_window)->current_matrix;
18528 vpos = XINT (row);
18529 if (vpos >= 0 && vpos < matrix->nrows)
18530 dump_glyph_row (MATRIX_ROW (matrix, vpos),
18531 vpos,
18532 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 2);
18533 return Qnil;
18537 DEFUN ("dump-tool-bar-row", Fdump_tool_bar_row, Sdump_tool_bar_row, 1, 2, "",
18538 doc: /* Dump glyph row ROW of the tool-bar of the current frame to stderr.
18539 GLYPH 0 means don't dump glyphs.
18540 GLYPH 1 means dump glyphs in short form.
18541 GLYPH > 1 or omitted means dump glyphs in long form.
18543 If there's no tool-bar, or if the tool-bar is not drawn by Emacs,
18544 do nothing. */)
18545 (Lisp_Object row, Lisp_Object glyphs)
18547 #if defined (HAVE_WINDOW_SYSTEM) && ! defined (USE_GTK) && ! defined (HAVE_NS)
18548 struct frame *sf = SELECTED_FRAME ();
18549 struct glyph_matrix *m = XWINDOW (sf->tool_bar_window)->current_matrix;
18550 EMACS_INT vpos;
18552 CHECK_NUMBER (row);
18553 vpos = XINT (row);
18554 if (vpos >= 0 && vpos < m->nrows)
18555 dump_glyph_row (MATRIX_ROW (m, vpos), vpos,
18556 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 2);
18557 #endif
18558 return Qnil;
18562 DEFUN ("trace-redisplay", Ftrace_redisplay, Strace_redisplay, 0, 1, "P",
18563 doc: /* Toggle tracing of redisplay.
18564 With ARG, turn tracing on if and only if ARG is positive. */)
18565 (Lisp_Object arg)
18567 if (NILP (arg))
18568 trace_redisplay_p = !trace_redisplay_p;
18569 else
18571 arg = Fprefix_numeric_value (arg);
18572 trace_redisplay_p = XINT (arg) > 0;
18575 return Qnil;
18579 DEFUN ("trace-to-stderr", Ftrace_to_stderr, Strace_to_stderr, 1, MANY, "",
18580 doc: /* Like `format', but print result to stderr.
18581 usage: (trace-to-stderr STRING &rest OBJECTS) */)
18582 (ptrdiff_t nargs, Lisp_Object *args)
18584 Lisp_Object s = Fformat (nargs, args);
18585 fprintf (stderr, "%s", SDATA (s));
18586 return Qnil;
18589 #endif /* GLYPH_DEBUG */
18593 /***********************************************************************
18594 Building Desired Matrix Rows
18595 ***********************************************************************/
18597 /* Return a temporary glyph row holding the glyphs of an overlay arrow.
18598 Used for non-window-redisplay windows, and for windows w/o left fringe. */
18600 static struct glyph_row *
18601 get_overlay_arrow_glyph_row (struct window *w, Lisp_Object overlay_arrow_string)
18603 struct frame *f = XFRAME (WINDOW_FRAME (w));
18604 struct buffer *buffer = XBUFFER (w->contents);
18605 struct buffer *old = current_buffer;
18606 const unsigned char *arrow_string = SDATA (overlay_arrow_string);
18607 int arrow_len = SCHARS (overlay_arrow_string);
18608 const unsigned char *arrow_end = arrow_string + arrow_len;
18609 const unsigned char *p;
18610 struct it it;
18611 bool multibyte_p;
18612 int n_glyphs_before;
18614 set_buffer_temp (buffer);
18615 init_iterator (&it, w, -1, -1, &scratch_glyph_row, DEFAULT_FACE_ID);
18616 it.glyph_row->used[TEXT_AREA] = 0;
18617 SET_TEXT_POS (it.position, 0, 0);
18619 multibyte_p = !NILP (BVAR (buffer, enable_multibyte_characters));
18620 p = arrow_string;
18621 while (p < arrow_end)
18623 Lisp_Object face, ilisp;
18625 /* Get the next character. */
18626 if (multibyte_p)
18627 it.c = it.char_to_display = string_char_and_length (p, &it.len);
18628 else
18630 it.c = it.char_to_display = *p, it.len = 1;
18631 if (! ASCII_CHAR_P (it.c))
18632 it.char_to_display = BYTE8_TO_CHAR (it.c);
18634 p += it.len;
18636 /* Get its face. */
18637 ilisp = make_number (p - arrow_string);
18638 face = Fget_text_property (ilisp, Qface, overlay_arrow_string);
18639 it.face_id = compute_char_face (f, it.char_to_display, face);
18641 /* Compute its width, get its glyphs. */
18642 n_glyphs_before = it.glyph_row->used[TEXT_AREA];
18643 SET_TEXT_POS (it.position, -1, -1);
18644 PRODUCE_GLYPHS (&it);
18646 /* If this character doesn't fit any more in the line, we have
18647 to remove some glyphs. */
18648 if (it.current_x > it.last_visible_x)
18650 it.glyph_row->used[TEXT_AREA] = n_glyphs_before;
18651 break;
18655 set_buffer_temp (old);
18656 return it.glyph_row;
18660 /* Insert truncation glyphs at the start of IT->glyph_row. Which
18661 glyphs to insert is determined by produce_special_glyphs. */
18663 static void
18664 insert_left_trunc_glyphs (struct it *it)
18666 struct it truncate_it;
18667 struct glyph *from, *end, *to, *toend;
18669 eassert (!FRAME_WINDOW_P (it->f)
18670 || (!it->glyph_row->reversed_p
18671 && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0)
18672 || (it->glyph_row->reversed_p
18673 && WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0));
18675 /* Get the truncation glyphs. */
18676 truncate_it = *it;
18677 truncate_it.current_x = 0;
18678 truncate_it.face_id = DEFAULT_FACE_ID;
18679 truncate_it.glyph_row = &scratch_glyph_row;
18680 truncate_it.glyph_row->used[TEXT_AREA] = 0;
18681 CHARPOS (truncate_it.position) = BYTEPOS (truncate_it.position) = -1;
18682 truncate_it.object = make_number (0);
18683 produce_special_glyphs (&truncate_it, IT_TRUNCATION);
18685 /* Overwrite glyphs from IT with truncation glyphs. */
18686 if (!it->glyph_row->reversed_p)
18688 short tused = truncate_it.glyph_row->used[TEXT_AREA];
18690 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
18691 end = from + tused;
18692 to = it->glyph_row->glyphs[TEXT_AREA];
18693 toend = to + it->glyph_row->used[TEXT_AREA];
18694 if (FRAME_WINDOW_P (it->f))
18696 /* On GUI frames, when variable-size fonts are displayed,
18697 the truncation glyphs may need more pixels than the row's
18698 glyphs they overwrite. We overwrite more glyphs to free
18699 enough screen real estate, and enlarge the stretch glyph
18700 on the right (see display_line), if there is one, to
18701 preserve the screen position of the truncation glyphs on
18702 the right. */
18703 int w = 0;
18704 struct glyph *g = to;
18705 short used;
18707 /* The first glyph could be partially visible, in which case
18708 it->glyph_row->x will be negative. But we want the left
18709 truncation glyphs to be aligned at the left margin of the
18710 window, so we override the x coordinate at which the row
18711 will begin. */
18712 it->glyph_row->x = 0;
18713 while (g < toend && w < it->truncation_pixel_width)
18715 w += g->pixel_width;
18716 ++g;
18718 if (g - to - tused > 0)
18720 memmove (to + tused, g, (toend - g) * sizeof(*g));
18721 it->glyph_row->used[TEXT_AREA] -= g - to - tused;
18723 used = it->glyph_row->used[TEXT_AREA];
18724 if (it->glyph_row->truncated_on_right_p
18725 && WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0
18726 && it->glyph_row->glyphs[TEXT_AREA][used - 2].type
18727 == STRETCH_GLYPH)
18729 int extra = w - it->truncation_pixel_width;
18731 it->glyph_row->glyphs[TEXT_AREA][used - 2].pixel_width += extra;
18735 while (from < end)
18736 *to++ = *from++;
18738 /* There may be padding glyphs left over. Overwrite them too. */
18739 if (!FRAME_WINDOW_P (it->f))
18741 while (to < toend && CHAR_GLYPH_PADDING_P (*to))
18743 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
18744 while (from < end)
18745 *to++ = *from++;
18749 if (to > toend)
18750 it->glyph_row->used[TEXT_AREA] = to - it->glyph_row->glyphs[TEXT_AREA];
18752 else
18754 short tused = truncate_it.glyph_row->used[TEXT_AREA];
18756 /* In R2L rows, overwrite the last (rightmost) glyphs, and do
18757 that back to front. */
18758 end = truncate_it.glyph_row->glyphs[TEXT_AREA];
18759 from = end + truncate_it.glyph_row->used[TEXT_AREA] - 1;
18760 toend = it->glyph_row->glyphs[TEXT_AREA];
18761 to = toend + it->glyph_row->used[TEXT_AREA] - 1;
18762 if (FRAME_WINDOW_P (it->f))
18764 int w = 0;
18765 struct glyph *g = to;
18767 while (g >= toend && w < it->truncation_pixel_width)
18769 w += g->pixel_width;
18770 --g;
18772 if (to - g - tused > 0)
18773 to = g + tused;
18774 if (it->glyph_row->truncated_on_right_p
18775 && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0
18776 && it->glyph_row->glyphs[TEXT_AREA][1].type == STRETCH_GLYPH)
18778 int extra = w - it->truncation_pixel_width;
18780 it->glyph_row->glyphs[TEXT_AREA][1].pixel_width += extra;
18784 while (from >= end && to >= toend)
18785 *to-- = *from--;
18786 if (!FRAME_WINDOW_P (it->f))
18788 while (to >= toend && CHAR_GLYPH_PADDING_P (*to))
18790 from =
18791 truncate_it.glyph_row->glyphs[TEXT_AREA]
18792 + truncate_it.glyph_row->used[TEXT_AREA] - 1;
18793 while (from >= end && to >= toend)
18794 *to-- = *from--;
18797 if (from >= end)
18799 /* Need to free some room before prepending additional
18800 glyphs. */
18801 int move_by = from - end + 1;
18802 struct glyph *g0 = it->glyph_row->glyphs[TEXT_AREA];
18803 struct glyph *g = g0 + it->glyph_row->used[TEXT_AREA] - 1;
18805 for ( ; g >= g0; g--)
18806 g[move_by] = *g;
18807 while (from >= end)
18808 *to-- = *from--;
18809 it->glyph_row->used[TEXT_AREA] += move_by;
18814 /* Compute the hash code for ROW. */
18815 unsigned
18816 row_hash (struct glyph_row *row)
18818 int area, k;
18819 unsigned hashval = 0;
18821 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
18822 for (k = 0; k < row->used[area]; ++k)
18823 hashval = ((((hashval << 4) + (hashval >> 24)) & 0x0fffffff)
18824 + row->glyphs[area][k].u.val
18825 + row->glyphs[area][k].face_id
18826 + row->glyphs[area][k].padding_p
18827 + (row->glyphs[area][k].type << 2));
18829 return hashval;
18832 /* Compute the pixel height and width of IT->glyph_row.
18834 Most of the time, ascent and height of a display line will be equal
18835 to the max_ascent and max_height values of the display iterator
18836 structure. This is not the case if
18838 1. We hit ZV without displaying anything. In this case, max_ascent
18839 and max_height will be zero.
18841 2. We have some glyphs that don't contribute to the line height.
18842 (The glyph row flag contributes_to_line_height_p is for future
18843 pixmap extensions).
18845 The first case is easily covered by using default values because in
18846 these cases, the line height does not really matter, except that it
18847 must not be zero. */
18849 static void
18850 compute_line_metrics (struct it *it)
18852 struct glyph_row *row = it->glyph_row;
18854 if (FRAME_WINDOW_P (it->f))
18856 int i, min_y, max_y;
18858 /* The line may consist of one space only, that was added to
18859 place the cursor on it. If so, the row's height hasn't been
18860 computed yet. */
18861 if (row->height == 0)
18863 if (it->max_ascent + it->max_descent == 0)
18864 it->max_descent = it->max_phys_descent = FRAME_LINE_HEIGHT (it->f);
18865 row->ascent = it->max_ascent;
18866 row->height = it->max_ascent + it->max_descent;
18867 row->phys_ascent = it->max_phys_ascent;
18868 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
18869 row->extra_line_spacing = it->max_extra_line_spacing;
18872 /* Compute the width of this line. */
18873 row->pixel_width = row->x;
18874 for (i = 0; i < row->used[TEXT_AREA]; ++i)
18875 row->pixel_width += row->glyphs[TEXT_AREA][i].pixel_width;
18877 eassert (row->pixel_width >= 0);
18878 eassert (row->ascent >= 0 && row->height > 0);
18880 row->overlapping_p = (MATRIX_ROW_OVERLAPS_SUCC_P (row)
18881 || MATRIX_ROW_OVERLAPS_PRED_P (row));
18883 /* If first line's physical ascent is larger than its logical
18884 ascent, use the physical ascent, and make the row taller.
18885 This makes accented characters fully visible. */
18886 if (row == MATRIX_FIRST_TEXT_ROW (it->w->desired_matrix)
18887 && row->phys_ascent > row->ascent)
18889 row->height += row->phys_ascent - row->ascent;
18890 row->ascent = row->phys_ascent;
18893 /* Compute how much of the line is visible. */
18894 row->visible_height = row->height;
18896 min_y = WINDOW_HEADER_LINE_HEIGHT (it->w);
18897 max_y = WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w);
18899 if (row->y < min_y)
18900 row->visible_height -= min_y - row->y;
18901 if (row->y + row->height > max_y)
18902 row->visible_height -= row->y + row->height - max_y;
18904 else
18906 row->pixel_width = row->used[TEXT_AREA];
18907 if (row->continued_p)
18908 row->pixel_width -= it->continuation_pixel_width;
18909 else if (row->truncated_on_right_p)
18910 row->pixel_width -= it->truncation_pixel_width;
18911 row->ascent = row->phys_ascent = 0;
18912 row->height = row->phys_height = row->visible_height = 1;
18913 row->extra_line_spacing = 0;
18916 /* Compute a hash code for this row. */
18917 row->hash = row_hash (row);
18919 it->max_ascent = it->max_descent = 0;
18920 it->max_phys_ascent = it->max_phys_descent = 0;
18924 /* Append one space to the glyph row of iterator IT if doing a
18925 window-based redisplay. The space has the same face as
18926 IT->face_id. Value is non-zero if a space was added.
18928 This function is called to make sure that there is always one glyph
18929 at the end of a glyph row that the cursor can be set on under
18930 window-systems. (If there weren't such a glyph we would not know
18931 how wide and tall a box cursor should be displayed).
18933 At the same time this space let's a nicely handle clearing to the
18934 end of the line if the row ends in italic text. */
18936 static int
18937 append_space_for_newline (struct it *it, int default_face_p)
18939 if (FRAME_WINDOW_P (it->f))
18941 int n = it->glyph_row->used[TEXT_AREA];
18943 if (it->glyph_row->glyphs[TEXT_AREA] + n
18944 < it->glyph_row->glyphs[1 + TEXT_AREA])
18946 /* Save some values that must not be changed.
18947 Must save IT->c and IT->len because otherwise
18948 ITERATOR_AT_END_P wouldn't work anymore after
18949 append_space_for_newline has been called. */
18950 enum display_element_type saved_what = it->what;
18951 int saved_c = it->c, saved_len = it->len;
18952 int saved_char_to_display = it->char_to_display;
18953 int saved_x = it->current_x;
18954 int saved_face_id = it->face_id;
18955 int saved_box_end = it->end_of_box_run_p;
18956 struct text_pos saved_pos;
18957 Lisp_Object saved_object;
18958 struct face *face;
18960 saved_object = it->object;
18961 saved_pos = it->position;
18963 it->what = IT_CHARACTER;
18964 memset (&it->position, 0, sizeof it->position);
18965 it->object = make_number (0);
18966 it->c = it->char_to_display = ' ';
18967 it->len = 1;
18969 /* If the default face was remapped, be sure to use the
18970 remapped face for the appended newline. */
18971 if (default_face_p)
18972 it->face_id = lookup_basic_face (it->f, DEFAULT_FACE_ID);
18973 else if (it->face_before_selective_p)
18974 it->face_id = it->saved_face_id;
18975 face = FACE_FROM_ID (it->f, it->face_id);
18976 it->face_id = FACE_FOR_CHAR (it->f, face, 0, -1, Qnil);
18977 /* In R2L rows, we will prepend a stretch glyph that will
18978 have the end_of_box_run_p flag set for it, so there's no
18979 need for the appended newline glyph to have that flag
18980 set. */
18981 if (it->glyph_row->reversed_p
18982 /* But if the appended newline glyph goes all the way to
18983 the end of the row, there will be no stretch glyph,
18984 so leave the box flag set. */
18985 && saved_x + FRAME_COLUMN_WIDTH (it->f) < it->last_visible_x)
18986 it->end_of_box_run_p = 0;
18988 PRODUCE_GLYPHS (it);
18990 it->override_ascent = -1;
18991 it->constrain_row_ascent_descent_p = 0;
18992 it->current_x = saved_x;
18993 it->object = saved_object;
18994 it->position = saved_pos;
18995 it->what = saved_what;
18996 it->face_id = saved_face_id;
18997 it->len = saved_len;
18998 it->c = saved_c;
18999 it->char_to_display = saved_char_to_display;
19000 it->end_of_box_run_p = saved_box_end;
19001 return 1;
19005 return 0;
19009 /* Extend the face of the last glyph in the text area of IT->glyph_row
19010 to the end of the display line. Called from display_line. If the
19011 glyph row is empty, add a space glyph to it so that we know the
19012 face to draw. Set the glyph row flag fill_line_p. If the glyph
19013 row is R2L, prepend a stretch glyph to cover the empty space to the
19014 left of the leftmost glyph. */
19016 static void
19017 extend_face_to_end_of_line (struct it *it)
19019 struct face *face, *default_face;
19020 struct frame *f = it->f;
19022 /* If line is already filled, do nothing. Non window-system frames
19023 get a grace of one more ``pixel'' because their characters are
19024 1-``pixel'' wide, so they hit the equality too early. This grace
19025 is needed only for R2L rows that are not continued, to produce
19026 one extra blank where we could display the cursor. */
19027 if ((it->current_x >= it->last_visible_x
19028 + (!FRAME_WINDOW_P (f)
19029 && it->glyph_row->reversed_p
19030 && !it->glyph_row->continued_p))
19031 /* If the window has display margins, we will need to extend
19032 their face even if the text area is filled. */
19033 && !(WINDOW_LEFT_MARGIN_WIDTH (it->w) > 0
19034 || WINDOW_RIGHT_MARGIN_WIDTH (it->w) > 0))
19035 return;
19037 /* The default face, possibly remapped. */
19038 default_face = FACE_FROM_ID (f, lookup_basic_face (f, DEFAULT_FACE_ID));
19040 /* Face extension extends the background and box of IT->face_id
19041 to the end of the line. If the background equals the background
19042 of the frame, we don't have to do anything. */
19043 if (it->face_before_selective_p)
19044 face = FACE_FROM_ID (f, it->saved_face_id);
19045 else
19046 face = FACE_FROM_ID (f, it->face_id);
19048 if (FRAME_WINDOW_P (f)
19049 && MATRIX_ROW_DISPLAYS_TEXT_P (it->glyph_row)
19050 && face->box == FACE_NO_BOX
19051 && face->background == FRAME_BACKGROUND_PIXEL (f)
19052 #ifdef HAVE_WINDOW_SYSTEM
19053 && !face->stipple
19054 #endif
19055 && !it->glyph_row->reversed_p)
19056 return;
19058 /* Set the glyph row flag indicating that the face of the last glyph
19059 in the text area has to be drawn to the end of the text area. */
19060 it->glyph_row->fill_line_p = 1;
19062 /* If current character of IT is not ASCII, make sure we have the
19063 ASCII face. This will be automatically undone the next time
19064 get_next_display_element returns a multibyte character. Note
19065 that the character will always be single byte in unibyte
19066 text. */
19067 if (!ASCII_CHAR_P (it->c))
19069 it->face_id = FACE_FOR_CHAR (f, face, 0, -1, Qnil);
19072 if (FRAME_WINDOW_P (f))
19074 /* If the row is empty, add a space with the current face of IT,
19075 so that we know which face to draw. */
19076 if (it->glyph_row->used[TEXT_AREA] == 0)
19078 it->glyph_row->glyphs[TEXT_AREA][0] = space_glyph;
19079 it->glyph_row->glyphs[TEXT_AREA][0].face_id = face->id;
19080 it->glyph_row->used[TEXT_AREA] = 1;
19082 /* Mode line and the header line don't have margins, and
19083 likewise the frame's tool-bar window, if there is any. */
19084 if (!(it->glyph_row->mode_line_p
19085 #if defined (HAVE_WINDOW_SYSTEM) && ! defined (USE_GTK) && ! defined (HAVE_NS)
19086 || (WINDOWP (f->tool_bar_window)
19087 && it->w == XWINDOW (f->tool_bar_window))
19088 #endif
19091 if (WINDOW_LEFT_MARGIN_WIDTH (it->w) > 0
19092 && it->glyph_row->used[LEFT_MARGIN_AREA] == 0)
19094 it->glyph_row->glyphs[LEFT_MARGIN_AREA][0] = space_glyph;
19095 it->glyph_row->glyphs[LEFT_MARGIN_AREA][0].face_id =
19096 default_face->id;
19097 it->glyph_row->used[LEFT_MARGIN_AREA] = 1;
19099 if (WINDOW_RIGHT_MARGIN_WIDTH (it->w) > 0
19100 && it->glyph_row->used[RIGHT_MARGIN_AREA] == 0)
19102 it->glyph_row->glyphs[RIGHT_MARGIN_AREA][0] = space_glyph;
19103 it->glyph_row->glyphs[RIGHT_MARGIN_AREA][0].face_id =
19104 default_face->id;
19105 it->glyph_row->used[RIGHT_MARGIN_AREA] = 1;
19108 #ifdef HAVE_WINDOW_SYSTEM
19109 if (it->glyph_row->reversed_p)
19111 /* Prepend a stretch glyph to the row, such that the
19112 rightmost glyph will be drawn flushed all the way to the
19113 right margin of the window. The stretch glyph that will
19114 occupy the empty space, if any, to the left of the
19115 glyphs. */
19116 struct font *font = face->font ? face->font : FRAME_FONT (f);
19117 struct glyph *row_start = it->glyph_row->glyphs[TEXT_AREA];
19118 struct glyph *row_end = row_start + it->glyph_row->used[TEXT_AREA];
19119 struct glyph *g;
19120 int row_width, stretch_ascent, stretch_width;
19121 struct text_pos saved_pos;
19122 int saved_face_id, saved_avoid_cursor, saved_box_start;
19124 for (row_width = 0, g = row_start; g < row_end; g++)
19125 row_width += g->pixel_width;
19126 stretch_width = window_box_width (it->w, TEXT_AREA) - row_width;
19127 if (stretch_width > 0)
19129 stretch_ascent =
19130 (((it->ascent + it->descent)
19131 * FONT_BASE (font)) / FONT_HEIGHT (font));
19132 saved_pos = it->position;
19133 memset (&it->position, 0, sizeof it->position);
19134 saved_avoid_cursor = it->avoid_cursor_p;
19135 it->avoid_cursor_p = 1;
19136 saved_face_id = it->face_id;
19137 saved_box_start = it->start_of_box_run_p;
19138 /* The last row's stretch glyph should get the default
19139 face, to avoid painting the rest of the window with
19140 the region face, if the region ends at ZV. */
19141 if (it->glyph_row->ends_at_zv_p)
19142 it->face_id = default_face->id;
19143 else
19144 it->face_id = face->id;
19145 it->start_of_box_run_p = 0;
19146 append_stretch_glyph (it, make_number (0), stretch_width,
19147 it->ascent + it->descent, stretch_ascent);
19148 it->position = saved_pos;
19149 it->avoid_cursor_p = saved_avoid_cursor;
19150 it->face_id = saved_face_id;
19151 it->start_of_box_run_p = saved_box_start;
19154 #endif /* HAVE_WINDOW_SYSTEM */
19156 else
19158 /* Save some values that must not be changed. */
19159 int saved_x = it->current_x;
19160 struct text_pos saved_pos;
19161 Lisp_Object saved_object;
19162 enum display_element_type saved_what = it->what;
19163 int saved_face_id = it->face_id;
19165 saved_object = it->object;
19166 saved_pos = it->position;
19168 it->what = IT_CHARACTER;
19169 memset (&it->position, 0, sizeof it->position);
19170 it->object = make_number (0);
19171 it->c = it->char_to_display = ' ';
19172 it->len = 1;
19174 if (WINDOW_LEFT_MARGIN_WIDTH (it->w) > 0
19175 && (it->glyph_row->used[LEFT_MARGIN_AREA]
19176 < WINDOW_LEFT_MARGIN_WIDTH (it->w))
19177 && !it->glyph_row->mode_line_p
19178 && default_face->background != FRAME_BACKGROUND_PIXEL (f))
19180 struct glyph *g = it->glyph_row->glyphs[LEFT_MARGIN_AREA];
19181 struct glyph *e = g + it->glyph_row->used[LEFT_MARGIN_AREA];
19183 for (it->current_x = 0; g < e; g++)
19184 it->current_x += g->pixel_width;
19186 it->area = LEFT_MARGIN_AREA;
19187 it->face_id = default_face->id;
19188 while (it->glyph_row->used[LEFT_MARGIN_AREA]
19189 < WINDOW_LEFT_MARGIN_WIDTH (it->w))
19191 PRODUCE_GLYPHS (it);
19192 /* term.c:produce_glyphs advances it->current_x only for
19193 TEXT_AREA. */
19194 it->current_x += it->pixel_width;
19197 it->current_x = saved_x;
19198 it->area = TEXT_AREA;
19201 /* The last row's blank glyphs should get the default face, to
19202 avoid painting the rest of the window with the region face,
19203 if the region ends at ZV. */
19204 if (it->glyph_row->ends_at_zv_p)
19205 it->face_id = default_face->id;
19206 else
19207 it->face_id = face->id;
19208 PRODUCE_GLYPHS (it);
19210 while (it->current_x <= it->last_visible_x)
19211 PRODUCE_GLYPHS (it);
19213 if (WINDOW_RIGHT_MARGIN_WIDTH (it->w) > 0
19214 && (it->glyph_row->used[RIGHT_MARGIN_AREA]
19215 < WINDOW_RIGHT_MARGIN_WIDTH (it->w))
19216 && !it->glyph_row->mode_line_p
19217 && default_face->background != FRAME_BACKGROUND_PIXEL (f))
19219 struct glyph *g = it->glyph_row->glyphs[RIGHT_MARGIN_AREA];
19220 struct glyph *e = g + it->glyph_row->used[RIGHT_MARGIN_AREA];
19222 for ( ; g < e; g++)
19223 it->current_x += g->pixel_width;
19225 it->area = RIGHT_MARGIN_AREA;
19226 it->face_id = default_face->id;
19227 while (it->glyph_row->used[RIGHT_MARGIN_AREA]
19228 < WINDOW_RIGHT_MARGIN_WIDTH (it->w))
19230 PRODUCE_GLYPHS (it);
19231 it->current_x += it->pixel_width;
19234 it->area = TEXT_AREA;
19237 /* Don't count these blanks really. It would let us insert a left
19238 truncation glyph below and make us set the cursor on them, maybe. */
19239 it->current_x = saved_x;
19240 it->object = saved_object;
19241 it->position = saved_pos;
19242 it->what = saved_what;
19243 it->face_id = saved_face_id;
19248 /* Value is non-zero if text starting at CHARPOS in current_buffer is
19249 trailing whitespace. */
19251 static int
19252 trailing_whitespace_p (ptrdiff_t charpos)
19254 ptrdiff_t bytepos = CHAR_TO_BYTE (charpos);
19255 int c = 0;
19257 while (bytepos < ZV_BYTE
19258 && (c = FETCH_CHAR (bytepos),
19259 c == ' ' || c == '\t'))
19260 ++bytepos;
19262 if (bytepos >= ZV_BYTE || c == '\n' || c == '\r')
19264 if (bytepos != PT_BYTE)
19265 return 1;
19267 return 0;
19271 /* Highlight trailing whitespace, if any, in ROW. */
19273 static void
19274 highlight_trailing_whitespace (struct frame *f, struct glyph_row *row)
19276 int used = row->used[TEXT_AREA];
19278 if (used)
19280 struct glyph *start = row->glyphs[TEXT_AREA];
19281 struct glyph *glyph = start + used - 1;
19283 if (row->reversed_p)
19285 /* Right-to-left rows need to be processed in the opposite
19286 direction, so swap the edge pointers. */
19287 glyph = start;
19288 start = row->glyphs[TEXT_AREA] + used - 1;
19291 /* Skip over glyphs inserted to display the cursor at the
19292 end of a line, for extending the face of the last glyph
19293 to the end of the line on terminals, and for truncation
19294 and continuation glyphs. */
19295 if (!row->reversed_p)
19297 while (glyph >= start
19298 && glyph->type == CHAR_GLYPH
19299 && INTEGERP (glyph->object))
19300 --glyph;
19302 else
19304 while (glyph <= start
19305 && glyph->type == CHAR_GLYPH
19306 && INTEGERP (glyph->object))
19307 ++glyph;
19310 /* If last glyph is a space or stretch, and it's trailing
19311 whitespace, set the face of all trailing whitespace glyphs in
19312 IT->glyph_row to `trailing-whitespace'. */
19313 if ((row->reversed_p ? glyph <= start : glyph >= start)
19314 && BUFFERP (glyph->object)
19315 && (glyph->type == STRETCH_GLYPH
19316 || (glyph->type == CHAR_GLYPH
19317 && glyph->u.ch == ' '))
19318 && trailing_whitespace_p (glyph->charpos))
19320 int face_id = lookup_named_face (f, Qtrailing_whitespace, 0);
19321 if (face_id < 0)
19322 return;
19324 if (!row->reversed_p)
19326 while (glyph >= start
19327 && BUFFERP (glyph->object)
19328 && (glyph->type == STRETCH_GLYPH
19329 || (glyph->type == CHAR_GLYPH
19330 && glyph->u.ch == ' ')))
19331 (glyph--)->face_id = face_id;
19333 else
19335 while (glyph <= start
19336 && BUFFERP (glyph->object)
19337 && (glyph->type == STRETCH_GLYPH
19338 || (glyph->type == CHAR_GLYPH
19339 && glyph->u.ch == ' ')))
19340 (glyph++)->face_id = face_id;
19347 /* Value is non-zero if glyph row ROW should be
19348 considered to hold the buffer position CHARPOS. */
19350 static int
19351 row_for_charpos_p (struct glyph_row *row, ptrdiff_t charpos)
19353 int result = 1;
19355 if (charpos == CHARPOS (row->end.pos)
19356 || charpos == MATRIX_ROW_END_CHARPOS (row))
19358 /* Suppose the row ends on a string.
19359 Unless the row is continued, that means it ends on a newline
19360 in the string. If it's anything other than a display string
19361 (e.g., a before-string from an overlay), we don't want the
19362 cursor there. (This heuristic seems to give the optimal
19363 behavior for the various types of multi-line strings.)
19364 One exception: if the string has `cursor' property on one of
19365 its characters, we _do_ want the cursor there. */
19366 if (CHARPOS (row->end.string_pos) >= 0)
19368 if (row->continued_p)
19369 result = 1;
19370 else
19372 /* Check for `display' property. */
19373 struct glyph *beg = row->glyphs[TEXT_AREA];
19374 struct glyph *end = beg + row->used[TEXT_AREA] - 1;
19375 struct glyph *glyph;
19377 result = 0;
19378 for (glyph = end; glyph >= beg; --glyph)
19379 if (STRINGP (glyph->object))
19381 Lisp_Object prop
19382 = Fget_char_property (make_number (charpos),
19383 Qdisplay, Qnil);
19384 result =
19385 (!NILP (prop)
19386 && display_prop_string_p (prop, glyph->object));
19387 /* If there's a `cursor' property on one of the
19388 string's characters, this row is a cursor row,
19389 even though this is not a display string. */
19390 if (!result)
19392 Lisp_Object s = glyph->object;
19394 for ( ; glyph >= beg && EQ (glyph->object, s); --glyph)
19396 ptrdiff_t gpos = glyph->charpos;
19398 if (!NILP (Fget_char_property (make_number (gpos),
19399 Qcursor, s)))
19401 result = 1;
19402 break;
19406 break;
19410 else if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
19412 /* If the row ends in middle of a real character,
19413 and the line is continued, we want the cursor here.
19414 That's because CHARPOS (ROW->end.pos) would equal
19415 PT if PT is before the character. */
19416 if (!row->ends_in_ellipsis_p)
19417 result = row->continued_p;
19418 else
19419 /* If the row ends in an ellipsis, then
19420 CHARPOS (ROW->end.pos) will equal point after the
19421 invisible text. We want that position to be displayed
19422 after the ellipsis. */
19423 result = 0;
19425 /* If the row ends at ZV, display the cursor at the end of that
19426 row instead of at the start of the row below. */
19427 else if (row->ends_at_zv_p)
19428 result = 1;
19429 else
19430 result = 0;
19433 return result;
19436 /* Value is non-zero if glyph row ROW should be
19437 used to hold the cursor. */
19439 static int
19440 cursor_row_p (struct glyph_row *row)
19442 return row_for_charpos_p (row, PT);
19447 /* Push the property PROP so that it will be rendered at the current
19448 position in IT. Return 1 if PROP was successfully pushed, 0
19449 otherwise. Called from handle_line_prefix to handle the
19450 `line-prefix' and `wrap-prefix' properties. */
19452 static int
19453 push_prefix_prop (struct it *it, Lisp_Object prop)
19455 struct text_pos pos =
19456 STRINGP (it->string) ? it->current.string_pos : it->current.pos;
19458 eassert (it->method == GET_FROM_BUFFER
19459 || it->method == GET_FROM_DISPLAY_VECTOR
19460 || it->method == GET_FROM_STRING);
19462 /* We need to save the current buffer/string position, so it will be
19463 restored by pop_it, because iterate_out_of_display_property
19464 depends on that being set correctly, but some situations leave
19465 it->position not yet set when this function is called. */
19466 push_it (it, &pos);
19468 if (STRINGP (prop))
19470 if (SCHARS (prop) == 0)
19472 pop_it (it);
19473 return 0;
19476 it->string = prop;
19477 it->string_from_prefix_prop_p = 1;
19478 it->multibyte_p = STRING_MULTIBYTE (it->string);
19479 it->current.overlay_string_index = -1;
19480 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
19481 it->end_charpos = it->string_nchars = SCHARS (it->string);
19482 it->method = GET_FROM_STRING;
19483 it->stop_charpos = 0;
19484 it->prev_stop = 0;
19485 it->base_level_stop = 0;
19487 /* Force paragraph direction to be that of the parent
19488 buffer/string. */
19489 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
19490 it->paragraph_embedding = it->bidi_it.paragraph_dir;
19491 else
19492 it->paragraph_embedding = L2R;
19494 /* Set up the bidi iterator for this display string. */
19495 if (it->bidi_p)
19497 it->bidi_it.string.lstring = it->string;
19498 it->bidi_it.string.s = NULL;
19499 it->bidi_it.string.schars = it->end_charpos;
19500 it->bidi_it.string.bufpos = IT_CHARPOS (*it);
19501 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
19502 it->bidi_it.string.unibyte = !it->multibyte_p;
19503 it->bidi_it.w = it->w;
19504 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
19507 else if (CONSP (prop) && EQ (XCAR (prop), Qspace))
19509 it->method = GET_FROM_STRETCH;
19510 it->object = prop;
19512 #ifdef HAVE_WINDOW_SYSTEM
19513 else if (IMAGEP (prop))
19515 it->what = IT_IMAGE;
19516 it->image_id = lookup_image (it->f, prop);
19517 it->method = GET_FROM_IMAGE;
19519 #endif /* HAVE_WINDOW_SYSTEM */
19520 else
19522 pop_it (it); /* bogus display property, give up */
19523 return 0;
19526 return 1;
19529 /* Return the character-property PROP at the current position in IT. */
19531 static Lisp_Object
19532 get_it_property (struct it *it, Lisp_Object prop)
19534 Lisp_Object position, object = it->object;
19536 if (STRINGP (object))
19537 position = make_number (IT_STRING_CHARPOS (*it));
19538 else if (BUFFERP (object))
19540 position = make_number (IT_CHARPOS (*it));
19541 object = it->window;
19543 else
19544 return Qnil;
19546 return Fget_char_property (position, prop, object);
19549 /* See if there's a line- or wrap-prefix, and if so, push it on IT. */
19551 static void
19552 handle_line_prefix (struct it *it)
19554 Lisp_Object prefix;
19556 if (it->continuation_lines_width > 0)
19558 prefix = get_it_property (it, Qwrap_prefix);
19559 if (NILP (prefix))
19560 prefix = Vwrap_prefix;
19562 else
19564 prefix = get_it_property (it, Qline_prefix);
19565 if (NILP (prefix))
19566 prefix = Vline_prefix;
19568 if (! NILP (prefix) && push_prefix_prop (it, prefix))
19570 /* If the prefix is wider than the window, and we try to wrap
19571 it, it would acquire its own wrap prefix, and so on till the
19572 iterator stack overflows. So, don't wrap the prefix. */
19573 it->line_wrap = TRUNCATE;
19574 it->avoid_cursor_p = 1;
19580 /* Remove N glyphs at the start of a reversed IT->glyph_row. Called
19581 only for R2L lines from display_line and display_string, when they
19582 decide that too many glyphs were produced by PRODUCE_GLYPHS, and
19583 the line/string needs to be continued on the next glyph row. */
19584 static void
19585 unproduce_glyphs (struct it *it, int n)
19587 struct glyph *glyph, *end;
19589 eassert (it->glyph_row);
19590 eassert (it->glyph_row->reversed_p);
19591 eassert (it->area == TEXT_AREA);
19592 eassert (n <= it->glyph_row->used[TEXT_AREA]);
19594 if (n > it->glyph_row->used[TEXT_AREA])
19595 n = it->glyph_row->used[TEXT_AREA];
19596 glyph = it->glyph_row->glyphs[TEXT_AREA] + n;
19597 end = it->glyph_row->glyphs[TEXT_AREA] + it->glyph_row->used[TEXT_AREA];
19598 for ( ; glyph < end; glyph++)
19599 glyph[-n] = *glyph;
19602 /* Find the positions in a bidi-reordered ROW to serve as ROW->minpos
19603 and ROW->maxpos. */
19604 static void
19605 find_row_edges (struct it *it, struct glyph_row *row,
19606 ptrdiff_t min_pos, ptrdiff_t min_bpos,
19607 ptrdiff_t max_pos, ptrdiff_t max_bpos)
19609 /* FIXME: Revisit this when glyph ``spilling'' in continuation
19610 lines' rows is implemented for bidi-reordered rows. */
19612 /* ROW->minpos is the value of min_pos, the minimal buffer position
19613 we have in ROW, or ROW->start.pos if that is smaller. */
19614 if (min_pos <= ZV && min_pos < row->start.pos.charpos)
19615 SET_TEXT_POS (row->minpos, min_pos, min_bpos);
19616 else
19617 /* We didn't find buffer positions smaller than ROW->start, or
19618 didn't find _any_ valid buffer positions in any of the glyphs,
19619 so we must trust the iterator's computed positions. */
19620 row->minpos = row->start.pos;
19621 if (max_pos <= 0)
19623 max_pos = CHARPOS (it->current.pos);
19624 max_bpos = BYTEPOS (it->current.pos);
19627 /* Here are the various use-cases for ending the row, and the
19628 corresponding values for ROW->maxpos:
19630 Line ends in a newline from buffer eol_pos + 1
19631 Line is continued from buffer max_pos + 1
19632 Line is truncated on right it->current.pos
19633 Line ends in a newline from string max_pos + 1(*)
19634 (*) + 1 only when line ends in a forward scan
19635 Line is continued from string max_pos
19636 Line is continued from display vector max_pos
19637 Line is entirely from a string min_pos == max_pos
19638 Line is entirely from a display vector min_pos == max_pos
19639 Line that ends at ZV ZV
19641 If you discover other use-cases, please add them here as
19642 appropriate. */
19643 if (row->ends_at_zv_p)
19644 row->maxpos = it->current.pos;
19645 else if (row->used[TEXT_AREA])
19647 int seen_this_string = 0;
19648 struct glyph_row *r1 = row - 1;
19650 /* Did we see the same display string on the previous row? */
19651 if (STRINGP (it->object)
19652 /* this is not the first row */
19653 && row > it->w->desired_matrix->rows
19654 /* previous row is not the header line */
19655 && !r1->mode_line_p
19656 /* previous row also ends in a newline from a string */
19657 && r1->ends_in_newline_from_string_p)
19659 struct glyph *start, *end;
19661 /* Search for the last glyph of the previous row that came
19662 from buffer or string. Depending on whether the row is
19663 L2R or R2L, we need to process it front to back or the
19664 other way round. */
19665 if (!r1->reversed_p)
19667 start = r1->glyphs[TEXT_AREA];
19668 end = start + r1->used[TEXT_AREA];
19669 /* Glyphs inserted by redisplay have an integer (zero)
19670 as their object. */
19671 while (end > start
19672 && INTEGERP ((end - 1)->object)
19673 && (end - 1)->charpos <= 0)
19674 --end;
19675 if (end > start)
19677 if (EQ ((end - 1)->object, it->object))
19678 seen_this_string = 1;
19680 else
19681 /* If all the glyphs of the previous row were inserted
19682 by redisplay, it means the previous row was
19683 produced from a single newline, which is only
19684 possible if that newline came from the same string
19685 as the one which produced this ROW. */
19686 seen_this_string = 1;
19688 else
19690 end = r1->glyphs[TEXT_AREA] - 1;
19691 start = end + r1->used[TEXT_AREA];
19692 while (end < start
19693 && INTEGERP ((end + 1)->object)
19694 && (end + 1)->charpos <= 0)
19695 ++end;
19696 if (end < start)
19698 if (EQ ((end + 1)->object, it->object))
19699 seen_this_string = 1;
19701 else
19702 seen_this_string = 1;
19705 /* Take note of each display string that covers a newline only
19706 once, the first time we see it. This is for when a display
19707 string includes more than one newline in it. */
19708 if (row->ends_in_newline_from_string_p && !seen_this_string)
19710 /* If we were scanning the buffer forward when we displayed
19711 the string, we want to account for at least one buffer
19712 position that belongs to this row (position covered by
19713 the display string), so that cursor positioning will
19714 consider this row as a candidate when point is at the end
19715 of the visual line represented by this row. This is not
19716 required when scanning back, because max_pos will already
19717 have a much larger value. */
19718 if (CHARPOS (row->end.pos) > max_pos)
19719 INC_BOTH (max_pos, max_bpos);
19720 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
19722 else if (CHARPOS (it->eol_pos) > 0)
19723 SET_TEXT_POS (row->maxpos,
19724 CHARPOS (it->eol_pos) + 1, BYTEPOS (it->eol_pos) + 1);
19725 else if (row->continued_p)
19727 /* If max_pos is different from IT's current position, it
19728 means IT->method does not belong to the display element
19729 at max_pos. However, it also means that the display
19730 element at max_pos was displayed in its entirety on this
19731 line, which is equivalent to saying that the next line
19732 starts at the next buffer position. */
19733 if (IT_CHARPOS (*it) == max_pos && it->method != GET_FROM_BUFFER)
19734 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
19735 else
19737 INC_BOTH (max_pos, max_bpos);
19738 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
19741 else if (row->truncated_on_right_p)
19742 /* display_line already called reseat_at_next_visible_line_start,
19743 which puts the iterator at the beginning of the next line, in
19744 the logical order. */
19745 row->maxpos = it->current.pos;
19746 else if (max_pos == min_pos && it->method != GET_FROM_BUFFER)
19747 /* A line that is entirely from a string/image/stretch... */
19748 row->maxpos = row->minpos;
19749 else
19750 emacs_abort ();
19752 else
19753 row->maxpos = it->current.pos;
19756 /* Construct the glyph row IT->glyph_row in the desired matrix of
19757 IT->w from text at the current position of IT. See dispextern.h
19758 for an overview of struct it. Value is non-zero if
19759 IT->glyph_row displays text, as opposed to a line displaying ZV
19760 only. */
19762 static int
19763 display_line (struct it *it)
19765 struct glyph_row *row = it->glyph_row;
19766 Lisp_Object overlay_arrow_string;
19767 struct it wrap_it;
19768 void *wrap_data = NULL;
19769 int may_wrap = 0, wrap_x IF_LINT (= 0);
19770 int wrap_row_used = -1;
19771 int wrap_row_ascent IF_LINT (= 0), wrap_row_height IF_LINT (= 0);
19772 int wrap_row_phys_ascent IF_LINT (= 0), wrap_row_phys_height IF_LINT (= 0);
19773 int wrap_row_extra_line_spacing IF_LINT (= 0);
19774 ptrdiff_t wrap_row_min_pos IF_LINT (= 0), wrap_row_min_bpos IF_LINT (= 0);
19775 ptrdiff_t wrap_row_max_pos IF_LINT (= 0), wrap_row_max_bpos IF_LINT (= 0);
19776 int cvpos;
19777 ptrdiff_t min_pos = ZV + 1, max_pos = 0;
19778 ptrdiff_t min_bpos IF_LINT (= 0), max_bpos IF_LINT (= 0);
19780 /* We always start displaying at hpos zero even if hscrolled. */
19781 eassert (it->hpos == 0 && it->current_x == 0);
19783 if (MATRIX_ROW_VPOS (row, it->w->desired_matrix)
19784 >= it->w->desired_matrix->nrows)
19786 it->w->nrows_scale_factor++;
19787 it->f->fonts_changed = 1;
19788 return 0;
19791 /* Clear the result glyph row and enable it. */
19792 prepare_desired_row (row);
19794 row->y = it->current_y;
19795 row->start = it->start;
19796 row->continuation_lines_width = it->continuation_lines_width;
19797 row->displays_text_p = 1;
19798 row->starts_in_middle_of_char_p = it->starts_in_middle_of_char_p;
19799 it->starts_in_middle_of_char_p = 0;
19801 /* Arrange the overlays nicely for our purposes. Usually, we call
19802 display_line on only one line at a time, in which case this
19803 can't really hurt too much, or we call it on lines which appear
19804 one after another in the buffer, in which case all calls to
19805 recenter_overlay_lists but the first will be pretty cheap. */
19806 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
19808 /* Move over display elements that are not visible because we are
19809 hscrolled. This may stop at an x-position < IT->first_visible_x
19810 if the first glyph is partially visible or if we hit a line end. */
19811 if (it->current_x < it->first_visible_x)
19813 enum move_it_result move_result;
19815 this_line_min_pos = row->start.pos;
19816 move_result = move_it_in_display_line_to (it, ZV, it->first_visible_x,
19817 MOVE_TO_POS | MOVE_TO_X);
19818 /* If we are under a large hscroll, move_it_in_display_line_to
19819 could hit the end of the line without reaching
19820 it->first_visible_x. Pretend that we did reach it. This is
19821 especially important on a TTY, where we will call
19822 extend_face_to_end_of_line, which needs to know how many
19823 blank glyphs to produce. */
19824 if (it->current_x < it->first_visible_x
19825 && (move_result == MOVE_NEWLINE_OR_CR
19826 || move_result == MOVE_POS_MATCH_OR_ZV))
19827 it->current_x = it->first_visible_x;
19829 /* Record the smallest positions seen while we moved over
19830 display elements that are not visible. This is needed by
19831 redisplay_internal for optimizing the case where the cursor
19832 stays inside the same line. The rest of this function only
19833 considers positions that are actually displayed, so
19834 RECORD_MAX_MIN_POS will not otherwise record positions that
19835 are hscrolled to the left of the left edge of the window. */
19836 min_pos = CHARPOS (this_line_min_pos);
19837 min_bpos = BYTEPOS (this_line_min_pos);
19839 else
19841 /* We only do this when not calling `move_it_in_display_line_to'
19842 above, because move_it_in_display_line_to calls
19843 handle_line_prefix itself. */
19844 handle_line_prefix (it);
19847 /* Get the initial row height. This is either the height of the
19848 text hscrolled, if there is any, or zero. */
19849 row->ascent = it->max_ascent;
19850 row->height = it->max_ascent + it->max_descent;
19851 row->phys_ascent = it->max_phys_ascent;
19852 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
19853 row->extra_line_spacing = it->max_extra_line_spacing;
19855 /* Utility macro to record max and min buffer positions seen until now. */
19856 #define RECORD_MAX_MIN_POS(IT) \
19857 do \
19859 int composition_p = !STRINGP ((IT)->string) \
19860 && ((IT)->what == IT_COMPOSITION); \
19861 ptrdiff_t current_pos = \
19862 composition_p ? (IT)->cmp_it.charpos \
19863 : IT_CHARPOS (*(IT)); \
19864 ptrdiff_t current_bpos = \
19865 composition_p ? CHAR_TO_BYTE (current_pos) \
19866 : IT_BYTEPOS (*(IT)); \
19867 if (current_pos < min_pos) \
19869 min_pos = current_pos; \
19870 min_bpos = current_bpos; \
19872 if (IT_CHARPOS (*it) > max_pos) \
19874 max_pos = IT_CHARPOS (*it); \
19875 max_bpos = IT_BYTEPOS (*it); \
19878 while (0)
19880 /* Loop generating characters. The loop is left with IT on the next
19881 character to display. */
19882 while (1)
19884 int n_glyphs_before, hpos_before, x_before;
19885 int x, nglyphs;
19886 int ascent = 0, descent = 0, phys_ascent = 0, phys_descent = 0;
19888 /* Retrieve the next thing to display. Value is zero if end of
19889 buffer reached. */
19890 if (!get_next_display_element (it))
19892 /* Maybe add a space at the end of this line that is used to
19893 display the cursor there under X. Set the charpos of the
19894 first glyph of blank lines not corresponding to any text
19895 to -1. */
19896 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
19897 row->exact_window_width_line_p = 1;
19898 else if ((append_space_for_newline (it, 1) && row->used[TEXT_AREA] == 1)
19899 || row->used[TEXT_AREA] == 0)
19901 row->glyphs[TEXT_AREA]->charpos = -1;
19902 row->displays_text_p = 0;
19904 if (!NILP (BVAR (XBUFFER (it->w->contents), indicate_empty_lines))
19905 && (!MINI_WINDOW_P (it->w)
19906 || (minibuf_level && EQ (it->window, minibuf_window))))
19907 row->indicate_empty_line_p = 1;
19910 it->continuation_lines_width = 0;
19911 row->ends_at_zv_p = 1;
19912 /* A row that displays right-to-left text must always have
19913 its last face extended all the way to the end of line,
19914 even if this row ends in ZV, because we still write to
19915 the screen left to right. We also need to extend the
19916 last face if the default face is remapped to some
19917 different face, otherwise the functions that clear
19918 portions of the screen will clear with the default face's
19919 background color. */
19920 if (row->reversed_p
19921 || lookup_basic_face (it->f, DEFAULT_FACE_ID) != DEFAULT_FACE_ID)
19922 extend_face_to_end_of_line (it);
19923 break;
19926 /* Now, get the metrics of what we want to display. This also
19927 generates glyphs in `row' (which is IT->glyph_row). */
19928 n_glyphs_before = row->used[TEXT_AREA];
19929 x = it->current_x;
19931 /* Remember the line height so far in case the next element doesn't
19932 fit on the line. */
19933 if (it->line_wrap != TRUNCATE)
19935 ascent = it->max_ascent;
19936 descent = it->max_descent;
19937 phys_ascent = it->max_phys_ascent;
19938 phys_descent = it->max_phys_descent;
19940 if (it->line_wrap == WORD_WRAP && it->area == TEXT_AREA)
19942 if (IT_DISPLAYING_WHITESPACE (it))
19943 may_wrap = 1;
19944 else if (may_wrap)
19946 SAVE_IT (wrap_it, *it, wrap_data);
19947 wrap_x = x;
19948 wrap_row_used = row->used[TEXT_AREA];
19949 wrap_row_ascent = row->ascent;
19950 wrap_row_height = row->height;
19951 wrap_row_phys_ascent = row->phys_ascent;
19952 wrap_row_phys_height = row->phys_height;
19953 wrap_row_extra_line_spacing = row->extra_line_spacing;
19954 wrap_row_min_pos = min_pos;
19955 wrap_row_min_bpos = min_bpos;
19956 wrap_row_max_pos = max_pos;
19957 wrap_row_max_bpos = max_bpos;
19958 may_wrap = 0;
19963 PRODUCE_GLYPHS (it);
19965 /* If this display element was in marginal areas, continue with
19966 the next one. */
19967 if (it->area != TEXT_AREA)
19969 row->ascent = max (row->ascent, it->max_ascent);
19970 row->height = max (row->height, it->max_ascent + it->max_descent);
19971 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
19972 row->phys_height = max (row->phys_height,
19973 it->max_phys_ascent + it->max_phys_descent);
19974 row->extra_line_spacing = max (row->extra_line_spacing,
19975 it->max_extra_line_spacing);
19976 set_iterator_to_next (it, 1);
19977 continue;
19980 /* Does the display element fit on the line? If we truncate
19981 lines, we should draw past the right edge of the window. If
19982 we don't truncate, we want to stop so that we can display the
19983 continuation glyph before the right margin. If lines are
19984 continued, there are two possible strategies for characters
19985 resulting in more than 1 glyph (e.g. tabs): Display as many
19986 glyphs as possible in this line and leave the rest for the
19987 continuation line, or display the whole element in the next
19988 line. Original redisplay did the former, so we do it also. */
19989 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
19990 hpos_before = it->hpos;
19991 x_before = x;
19993 if (/* Not a newline. */
19994 nglyphs > 0
19995 /* Glyphs produced fit entirely in the line. */
19996 && it->current_x < it->last_visible_x)
19998 it->hpos += nglyphs;
19999 row->ascent = max (row->ascent, it->max_ascent);
20000 row->height = max (row->height, it->max_ascent + it->max_descent);
20001 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
20002 row->phys_height = max (row->phys_height,
20003 it->max_phys_ascent + it->max_phys_descent);
20004 row->extra_line_spacing = max (row->extra_line_spacing,
20005 it->max_extra_line_spacing);
20006 if (it->current_x - it->pixel_width < it->first_visible_x)
20007 row->x = x - it->first_visible_x;
20008 /* Record the maximum and minimum buffer positions seen so
20009 far in glyphs that will be displayed by this row. */
20010 if (it->bidi_p)
20011 RECORD_MAX_MIN_POS (it);
20013 else
20015 int i, new_x;
20016 struct glyph *glyph;
20018 for (i = 0; i < nglyphs; ++i, x = new_x)
20020 glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
20021 new_x = x + glyph->pixel_width;
20023 if (/* Lines are continued. */
20024 it->line_wrap != TRUNCATE
20025 && (/* Glyph doesn't fit on the line. */
20026 new_x > it->last_visible_x
20027 /* Or it fits exactly on a window system frame. */
20028 || (new_x == it->last_visible_x
20029 && FRAME_WINDOW_P (it->f)
20030 && (row->reversed_p
20031 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
20032 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)))))
20034 /* End of a continued line. */
20036 if (it->hpos == 0
20037 || (new_x == it->last_visible_x
20038 && FRAME_WINDOW_P (it->f)
20039 && (row->reversed_p
20040 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
20041 : WINDOW_RIGHT_FRINGE_WIDTH (it->w))))
20043 /* Current glyph is the only one on the line or
20044 fits exactly on the line. We must continue
20045 the line because we can't draw the cursor
20046 after the glyph. */
20047 row->continued_p = 1;
20048 it->current_x = new_x;
20049 it->continuation_lines_width += new_x;
20050 ++it->hpos;
20051 if (i == nglyphs - 1)
20053 /* If line-wrap is on, check if a previous
20054 wrap point was found. */
20055 if (wrap_row_used > 0
20056 /* Even if there is a previous wrap
20057 point, continue the line here as
20058 usual, if (i) the previous character
20059 was a space or tab AND (ii) the
20060 current character is not. */
20061 && (!may_wrap
20062 || IT_DISPLAYING_WHITESPACE (it)))
20063 goto back_to_wrap;
20065 /* Record the maximum and minimum buffer
20066 positions seen so far in glyphs that will be
20067 displayed by this row. */
20068 if (it->bidi_p)
20069 RECORD_MAX_MIN_POS (it);
20070 set_iterator_to_next (it, 1);
20071 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
20073 if (!get_next_display_element (it))
20075 row->exact_window_width_line_p = 1;
20076 it->continuation_lines_width = 0;
20077 row->continued_p = 0;
20078 row->ends_at_zv_p = 1;
20080 else if (ITERATOR_AT_END_OF_LINE_P (it))
20082 row->continued_p = 0;
20083 row->exact_window_width_line_p = 1;
20087 else if (it->bidi_p)
20088 RECORD_MAX_MIN_POS (it);
20089 if (WINDOW_LEFT_MARGIN_WIDTH (it->w) > 0
20090 || WINDOW_RIGHT_MARGIN_WIDTH (it->w) > 0)
20091 extend_face_to_end_of_line (it);
20093 else if (CHAR_GLYPH_PADDING_P (*glyph)
20094 && !FRAME_WINDOW_P (it->f))
20096 /* A padding glyph that doesn't fit on this line.
20097 This means the whole character doesn't fit
20098 on the line. */
20099 if (row->reversed_p)
20100 unproduce_glyphs (it, row->used[TEXT_AREA]
20101 - n_glyphs_before);
20102 row->used[TEXT_AREA] = n_glyphs_before;
20104 /* Fill the rest of the row with continuation
20105 glyphs like in 20.x. */
20106 while (row->glyphs[TEXT_AREA] + row->used[TEXT_AREA]
20107 < row->glyphs[1 + TEXT_AREA])
20108 produce_special_glyphs (it, IT_CONTINUATION);
20110 row->continued_p = 1;
20111 it->current_x = x_before;
20112 it->continuation_lines_width += x_before;
20114 /* Restore the height to what it was before the
20115 element not fitting on the line. */
20116 it->max_ascent = ascent;
20117 it->max_descent = descent;
20118 it->max_phys_ascent = phys_ascent;
20119 it->max_phys_descent = phys_descent;
20120 if (WINDOW_LEFT_MARGIN_WIDTH (it->w) > 0
20121 || WINDOW_RIGHT_MARGIN_WIDTH (it->w) > 0)
20122 extend_face_to_end_of_line (it);
20124 else if (wrap_row_used > 0)
20126 back_to_wrap:
20127 if (row->reversed_p)
20128 unproduce_glyphs (it,
20129 row->used[TEXT_AREA] - wrap_row_used);
20130 RESTORE_IT (it, &wrap_it, wrap_data);
20131 it->continuation_lines_width += wrap_x;
20132 row->used[TEXT_AREA] = wrap_row_used;
20133 row->ascent = wrap_row_ascent;
20134 row->height = wrap_row_height;
20135 row->phys_ascent = wrap_row_phys_ascent;
20136 row->phys_height = wrap_row_phys_height;
20137 row->extra_line_spacing = wrap_row_extra_line_spacing;
20138 min_pos = wrap_row_min_pos;
20139 min_bpos = wrap_row_min_bpos;
20140 max_pos = wrap_row_max_pos;
20141 max_bpos = wrap_row_max_bpos;
20142 row->continued_p = 1;
20143 row->ends_at_zv_p = 0;
20144 row->exact_window_width_line_p = 0;
20145 it->continuation_lines_width += x;
20147 /* Make sure that a non-default face is extended
20148 up to the right margin of the window. */
20149 extend_face_to_end_of_line (it);
20151 else if (it->c == '\t' && FRAME_WINDOW_P (it->f))
20153 /* A TAB that extends past the right edge of the
20154 window. This produces a single glyph on
20155 window system frames. We leave the glyph in
20156 this row and let it fill the row, but don't
20157 consume the TAB. */
20158 if ((row->reversed_p
20159 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
20160 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
20161 produce_special_glyphs (it, IT_CONTINUATION);
20162 it->continuation_lines_width += it->last_visible_x;
20163 row->ends_in_middle_of_char_p = 1;
20164 row->continued_p = 1;
20165 glyph->pixel_width = it->last_visible_x - x;
20166 it->starts_in_middle_of_char_p = 1;
20167 if (WINDOW_LEFT_MARGIN_WIDTH (it->w) > 0
20168 || WINDOW_RIGHT_MARGIN_WIDTH (it->w) > 0)
20169 extend_face_to_end_of_line (it);
20171 else
20173 /* Something other than a TAB that draws past
20174 the right edge of the window. Restore
20175 positions to values before the element. */
20176 if (row->reversed_p)
20177 unproduce_glyphs (it, row->used[TEXT_AREA]
20178 - (n_glyphs_before + i));
20179 row->used[TEXT_AREA] = n_glyphs_before + i;
20181 /* Display continuation glyphs. */
20182 it->current_x = x_before;
20183 it->continuation_lines_width += x;
20184 if (!FRAME_WINDOW_P (it->f)
20185 || (row->reversed_p
20186 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
20187 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
20188 produce_special_glyphs (it, IT_CONTINUATION);
20189 row->continued_p = 1;
20191 extend_face_to_end_of_line (it);
20193 if (nglyphs > 1 && i > 0)
20195 row->ends_in_middle_of_char_p = 1;
20196 it->starts_in_middle_of_char_p = 1;
20199 /* Restore the height to what it was before the
20200 element not fitting on the line. */
20201 it->max_ascent = ascent;
20202 it->max_descent = descent;
20203 it->max_phys_ascent = phys_ascent;
20204 it->max_phys_descent = phys_descent;
20207 break;
20209 else if (new_x > it->first_visible_x)
20211 /* Increment number of glyphs actually displayed. */
20212 ++it->hpos;
20214 /* Record the maximum and minimum buffer positions
20215 seen so far in glyphs that will be displayed by
20216 this row. */
20217 if (it->bidi_p)
20218 RECORD_MAX_MIN_POS (it);
20220 if (x < it->first_visible_x)
20221 /* Glyph is partially visible, i.e. row starts at
20222 negative X position. */
20223 row->x = x - it->first_visible_x;
20225 else
20227 /* Glyph is completely off the left margin of the
20228 window. This should not happen because of the
20229 move_it_in_display_line at the start of this
20230 function, unless the text display area of the
20231 window is empty. */
20232 eassert (it->first_visible_x <= it->last_visible_x);
20235 /* Even if this display element produced no glyphs at all,
20236 we want to record its position. */
20237 if (it->bidi_p && nglyphs == 0)
20238 RECORD_MAX_MIN_POS (it);
20240 row->ascent = max (row->ascent, it->max_ascent);
20241 row->height = max (row->height, it->max_ascent + it->max_descent);
20242 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
20243 row->phys_height = max (row->phys_height,
20244 it->max_phys_ascent + it->max_phys_descent);
20245 row->extra_line_spacing = max (row->extra_line_spacing,
20246 it->max_extra_line_spacing);
20248 /* End of this display line if row is continued. */
20249 if (row->continued_p || row->ends_at_zv_p)
20250 break;
20253 at_end_of_line:
20254 /* Is this a line end? If yes, we're also done, after making
20255 sure that a non-default face is extended up to the right
20256 margin of the window. */
20257 if (ITERATOR_AT_END_OF_LINE_P (it))
20259 int used_before = row->used[TEXT_AREA];
20261 row->ends_in_newline_from_string_p = STRINGP (it->object);
20263 /* Add a space at the end of the line that is used to
20264 display the cursor there. */
20265 if (!IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
20266 append_space_for_newline (it, 0);
20268 /* Extend the face to the end of the line. */
20269 extend_face_to_end_of_line (it);
20271 /* Make sure we have the position. */
20272 if (used_before == 0)
20273 row->glyphs[TEXT_AREA]->charpos = CHARPOS (it->position);
20275 /* Record the position of the newline, for use in
20276 find_row_edges. */
20277 it->eol_pos = it->current.pos;
20279 /* Consume the line end. This skips over invisible lines. */
20280 set_iterator_to_next (it, 1);
20281 it->continuation_lines_width = 0;
20282 break;
20285 /* Proceed with next display element. Note that this skips
20286 over lines invisible because of selective display. */
20287 set_iterator_to_next (it, 1);
20289 /* If we truncate lines, we are done when the last displayed
20290 glyphs reach past the right margin of the window. */
20291 if (it->line_wrap == TRUNCATE
20292 && ((FRAME_WINDOW_P (it->f)
20293 /* Images are preprocessed in produce_image_glyph such
20294 that they are cropped at the right edge of the
20295 window, so an image glyph will always end exactly at
20296 last_visible_x, even if there's no right fringe. */
20297 && (WINDOW_RIGHT_FRINGE_WIDTH (it->w) || it->what == IT_IMAGE))
20298 ? (it->current_x >= it->last_visible_x)
20299 : (it->current_x > it->last_visible_x)))
20301 /* Maybe add truncation glyphs. */
20302 if (!FRAME_WINDOW_P (it->f)
20303 || (row->reversed_p
20304 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
20305 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
20307 int i, n;
20309 if (!row->reversed_p)
20311 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
20312 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
20313 break;
20315 else
20317 for (i = 0; i < row->used[TEXT_AREA]; i++)
20318 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
20319 break;
20320 /* Remove any padding glyphs at the front of ROW, to
20321 make room for the truncation glyphs we will be
20322 adding below. The loop below always inserts at
20323 least one truncation glyph, so also remove the
20324 last glyph added to ROW. */
20325 unproduce_glyphs (it, i + 1);
20326 /* Adjust i for the loop below. */
20327 i = row->used[TEXT_AREA] - (i + 1);
20330 /* produce_special_glyphs overwrites the last glyph, so
20331 we don't want that if we want to keep that last
20332 glyph, which means it's an image. */
20333 if (it->current_x > it->last_visible_x)
20335 it->current_x = x_before;
20336 if (!FRAME_WINDOW_P (it->f))
20338 for (n = row->used[TEXT_AREA]; i < n; ++i)
20340 row->used[TEXT_AREA] = i;
20341 produce_special_glyphs (it, IT_TRUNCATION);
20344 else
20346 row->used[TEXT_AREA] = i;
20347 produce_special_glyphs (it, IT_TRUNCATION);
20349 it->hpos = hpos_before;
20352 else if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
20354 /* Don't truncate if we can overflow newline into fringe. */
20355 if (!get_next_display_element (it))
20357 it->continuation_lines_width = 0;
20358 row->ends_at_zv_p = 1;
20359 row->exact_window_width_line_p = 1;
20360 break;
20362 if (ITERATOR_AT_END_OF_LINE_P (it))
20364 row->exact_window_width_line_p = 1;
20365 goto at_end_of_line;
20367 it->current_x = x_before;
20368 it->hpos = hpos_before;
20371 row->truncated_on_right_p = 1;
20372 it->continuation_lines_width = 0;
20373 reseat_at_next_visible_line_start (it, 0);
20374 row->ends_at_zv_p = FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n';
20375 break;
20379 if (wrap_data)
20380 bidi_unshelve_cache (wrap_data, 1);
20382 /* If line is not empty and hscrolled, maybe insert truncation glyphs
20383 at the left window margin. */
20384 if (it->first_visible_x
20385 && IT_CHARPOS (*it) != CHARPOS (row->start.pos))
20387 if (!FRAME_WINDOW_P (it->f)
20388 || (((row->reversed_p
20389 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
20390 : WINDOW_LEFT_FRINGE_WIDTH (it->w)) == 0)
20391 /* Don't let insert_left_trunc_glyphs overwrite the
20392 first glyph of the row if it is an image. */
20393 && row->glyphs[TEXT_AREA]->type != IMAGE_GLYPH))
20394 insert_left_trunc_glyphs (it);
20395 row->truncated_on_left_p = 1;
20398 /* Remember the position at which this line ends.
20400 BIDI Note: any code that needs MATRIX_ROW_START/END_CHARPOS
20401 cannot be before the call to find_row_edges below, since that is
20402 where these positions are determined. */
20403 row->end = it->current;
20404 if (!it->bidi_p)
20406 row->minpos = row->start.pos;
20407 row->maxpos = row->end.pos;
20409 else
20411 /* ROW->minpos and ROW->maxpos must be the smallest and
20412 `1 + the largest' buffer positions in ROW. But if ROW was
20413 bidi-reordered, these two positions can be anywhere in the
20414 row, so we must determine them now. */
20415 find_row_edges (it, row, min_pos, min_bpos, max_pos, max_bpos);
20418 /* If the start of this line is the overlay arrow-position, then
20419 mark this glyph row as the one containing the overlay arrow.
20420 This is clearly a mess with variable size fonts. It would be
20421 better to let it be displayed like cursors under X. */
20422 if ((MATRIX_ROW_DISPLAYS_TEXT_P (row) || !overlay_arrow_seen)
20423 && (overlay_arrow_string = overlay_arrow_at_row (it, row),
20424 !NILP (overlay_arrow_string)))
20426 /* Overlay arrow in window redisplay is a fringe bitmap. */
20427 if (STRINGP (overlay_arrow_string))
20429 struct glyph_row *arrow_row
20430 = get_overlay_arrow_glyph_row (it->w, overlay_arrow_string);
20431 struct glyph *glyph = arrow_row->glyphs[TEXT_AREA];
20432 struct glyph *arrow_end = glyph + arrow_row->used[TEXT_AREA];
20433 struct glyph *p = row->glyphs[TEXT_AREA];
20434 struct glyph *p2, *end;
20436 /* Copy the arrow glyphs. */
20437 while (glyph < arrow_end)
20438 *p++ = *glyph++;
20440 /* Throw away padding glyphs. */
20441 p2 = p;
20442 end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
20443 while (p2 < end && CHAR_GLYPH_PADDING_P (*p2))
20444 ++p2;
20445 if (p2 > p)
20447 while (p2 < end)
20448 *p++ = *p2++;
20449 row->used[TEXT_AREA] = p2 - row->glyphs[TEXT_AREA];
20452 else
20454 eassert (INTEGERP (overlay_arrow_string));
20455 row->overlay_arrow_bitmap = XINT (overlay_arrow_string);
20457 overlay_arrow_seen = 1;
20460 /* Highlight trailing whitespace. */
20461 if (!NILP (Vshow_trailing_whitespace))
20462 highlight_trailing_whitespace (it->f, it->glyph_row);
20464 /* Compute pixel dimensions of this line. */
20465 compute_line_metrics (it);
20467 /* Implementation note: No changes in the glyphs of ROW or in their
20468 faces can be done past this point, because compute_line_metrics
20469 computes ROW's hash value and stores it within the glyph_row
20470 structure. */
20472 /* Record whether this row ends inside an ellipsis. */
20473 row->ends_in_ellipsis_p
20474 = (it->method == GET_FROM_DISPLAY_VECTOR
20475 && it->ellipsis_p);
20477 /* Save fringe bitmaps in this row. */
20478 row->left_user_fringe_bitmap = it->left_user_fringe_bitmap;
20479 row->left_user_fringe_face_id = it->left_user_fringe_face_id;
20480 row->right_user_fringe_bitmap = it->right_user_fringe_bitmap;
20481 row->right_user_fringe_face_id = it->right_user_fringe_face_id;
20483 it->left_user_fringe_bitmap = 0;
20484 it->left_user_fringe_face_id = 0;
20485 it->right_user_fringe_bitmap = 0;
20486 it->right_user_fringe_face_id = 0;
20488 /* Maybe set the cursor. */
20489 cvpos = it->w->cursor.vpos;
20490 if ((cvpos < 0
20491 /* In bidi-reordered rows, keep checking for proper cursor
20492 position even if one has been found already, because buffer
20493 positions in such rows change non-linearly with ROW->VPOS,
20494 when a line is continued. One exception: when we are at ZV,
20495 display cursor on the first suitable glyph row, since all
20496 the empty rows after that also have their position set to ZV. */
20497 /* FIXME: Revisit this when glyph ``spilling'' in continuation
20498 lines' rows is implemented for bidi-reordered rows. */
20499 || (it->bidi_p
20500 && !MATRIX_ROW (it->w->desired_matrix, cvpos)->ends_at_zv_p))
20501 && PT >= MATRIX_ROW_START_CHARPOS (row)
20502 && PT <= MATRIX_ROW_END_CHARPOS (row)
20503 && cursor_row_p (row))
20504 set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
20506 /* Prepare for the next line. This line starts horizontally at (X
20507 HPOS) = (0 0). Vertical positions are incremented. As a
20508 convenience for the caller, IT->glyph_row is set to the next
20509 row to be used. */
20510 it->current_x = it->hpos = 0;
20511 it->current_y += row->height;
20512 SET_TEXT_POS (it->eol_pos, 0, 0);
20513 ++it->vpos;
20514 ++it->glyph_row;
20515 /* The next row should by default use the same value of the
20516 reversed_p flag as this one. set_iterator_to_next decides when
20517 it's a new paragraph, and PRODUCE_GLYPHS recomputes the value of
20518 the flag accordingly. */
20519 if (it->glyph_row < MATRIX_BOTTOM_TEXT_ROW (it->w->desired_matrix, it->w))
20520 it->glyph_row->reversed_p = row->reversed_p;
20521 it->start = row->end;
20522 return MATRIX_ROW_DISPLAYS_TEXT_P (row);
20524 #undef RECORD_MAX_MIN_POS
20527 DEFUN ("current-bidi-paragraph-direction", Fcurrent_bidi_paragraph_direction,
20528 Scurrent_bidi_paragraph_direction, 0, 1, 0,
20529 doc: /* Return paragraph direction at point in BUFFER.
20530 Value is either `left-to-right' or `right-to-left'.
20531 If BUFFER is omitted or nil, it defaults to the current buffer.
20533 Paragraph direction determines how the text in the paragraph is displayed.
20534 In left-to-right paragraphs, text begins at the left margin of the window
20535 and the reading direction is generally left to right. In right-to-left
20536 paragraphs, text begins at the right margin and is read from right to left.
20538 See also `bidi-paragraph-direction'. */)
20539 (Lisp_Object buffer)
20541 struct buffer *buf = current_buffer;
20542 struct buffer *old = buf;
20544 if (! NILP (buffer))
20546 CHECK_BUFFER (buffer);
20547 buf = XBUFFER (buffer);
20550 if (NILP (BVAR (buf, bidi_display_reordering))
20551 || NILP (BVAR (buf, enable_multibyte_characters))
20552 /* When we are loading loadup.el, the character property tables
20553 needed for bidi iteration are not yet available. */
20554 || !NILP (Vpurify_flag))
20555 return Qleft_to_right;
20556 else if (!NILP (BVAR (buf, bidi_paragraph_direction)))
20557 return BVAR (buf, bidi_paragraph_direction);
20558 else
20560 /* Determine the direction from buffer text. We could try to
20561 use current_matrix if it is up to date, but this seems fast
20562 enough as it is. */
20563 struct bidi_it itb;
20564 ptrdiff_t pos = BUF_PT (buf);
20565 ptrdiff_t bytepos = BUF_PT_BYTE (buf);
20566 int c;
20567 void *itb_data = bidi_shelve_cache ();
20569 set_buffer_temp (buf);
20570 /* bidi_paragraph_init finds the base direction of the paragraph
20571 by searching forward from paragraph start. We need the base
20572 direction of the current or _previous_ paragraph, so we need
20573 to make sure we are within that paragraph. To that end, find
20574 the previous non-empty line. */
20575 if (pos >= ZV && pos > BEGV)
20576 DEC_BOTH (pos, bytepos);
20577 if (fast_looking_at (build_string ("[\f\t ]*\n"),
20578 pos, bytepos, ZV, ZV_BYTE, Qnil) > 0)
20580 while ((c = FETCH_BYTE (bytepos)) == '\n'
20581 || c == ' ' || c == '\t' || c == '\f')
20583 if (bytepos <= BEGV_BYTE)
20584 break;
20585 bytepos--;
20586 pos--;
20588 while (!CHAR_HEAD_P (FETCH_BYTE (bytepos)))
20589 bytepos--;
20591 bidi_init_it (pos, bytepos, FRAME_WINDOW_P (SELECTED_FRAME ()), &itb);
20592 itb.paragraph_dir = NEUTRAL_DIR;
20593 itb.string.s = NULL;
20594 itb.string.lstring = Qnil;
20595 itb.string.bufpos = 0;
20596 itb.string.from_disp_str = 0;
20597 itb.string.unibyte = 0;
20598 /* We have no window to use here for ignoring window-specific
20599 overlays. Using NULL for window pointer will cause
20600 compute_display_string_pos to use the current buffer. */
20601 itb.w = NULL;
20602 bidi_paragraph_init (NEUTRAL_DIR, &itb, 1);
20603 bidi_unshelve_cache (itb_data, 0);
20604 set_buffer_temp (old);
20605 switch (itb.paragraph_dir)
20607 case L2R:
20608 return Qleft_to_right;
20609 break;
20610 case R2L:
20611 return Qright_to_left;
20612 break;
20613 default:
20614 emacs_abort ();
20619 DEFUN ("move-point-visually", Fmove_point_visually,
20620 Smove_point_visually, 1, 1, 0,
20621 doc: /* Move point in the visual order in the specified DIRECTION.
20622 DIRECTION can be 1, meaning move to the right, or -1, which moves to the
20623 left.
20625 Value is the new character position of point. */)
20626 (Lisp_Object direction)
20628 struct window *w = XWINDOW (selected_window);
20629 struct buffer *b = XBUFFER (w->contents);
20630 struct glyph_row *row;
20631 int dir;
20632 Lisp_Object paragraph_dir;
20634 #define ROW_GLYPH_NEWLINE_P(ROW,GLYPH) \
20635 (!(ROW)->continued_p \
20636 && INTEGERP ((GLYPH)->object) \
20637 && (GLYPH)->type == CHAR_GLYPH \
20638 && (GLYPH)->u.ch == ' ' \
20639 && (GLYPH)->charpos >= 0 \
20640 && !(GLYPH)->avoid_cursor_p)
20642 CHECK_NUMBER (direction);
20643 dir = XINT (direction);
20644 if (dir > 0)
20645 dir = 1;
20646 else
20647 dir = -1;
20649 /* If current matrix is up-to-date, we can use the information
20650 recorded in the glyphs, at least as long as the goal is on the
20651 screen. */
20652 if (w->window_end_valid
20653 && !windows_or_buffers_changed
20654 && b
20655 && !b->clip_changed
20656 && !b->prevent_redisplay_optimizations_p
20657 && !window_outdated (w)
20658 && w->cursor.vpos >= 0
20659 && w->cursor.vpos < w->current_matrix->nrows
20660 && (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos))->enabled_p)
20662 struct glyph *g = row->glyphs[TEXT_AREA];
20663 struct glyph *e = dir > 0 ? g + row->used[TEXT_AREA] : g - 1;
20664 struct glyph *gpt = g + w->cursor.hpos;
20666 for (g = gpt + dir; (dir > 0 ? g < e : g > e); g += dir)
20668 if (BUFFERP (g->object) && g->charpos != PT)
20670 SET_PT (g->charpos);
20671 w->cursor.vpos = -1;
20672 return make_number (PT);
20674 else if (!INTEGERP (g->object) && !EQ (g->object, gpt->object))
20676 ptrdiff_t new_pos;
20678 if (BUFFERP (gpt->object))
20680 new_pos = PT;
20681 if ((gpt->resolved_level - row->reversed_p) % 2 == 0)
20682 new_pos += (row->reversed_p ? -dir : dir);
20683 else
20684 new_pos -= (row->reversed_p ? -dir : dir);;
20686 else if (BUFFERP (g->object))
20687 new_pos = g->charpos;
20688 else
20689 break;
20690 SET_PT (new_pos);
20691 w->cursor.vpos = -1;
20692 return make_number (PT);
20694 else if (ROW_GLYPH_NEWLINE_P (row, g))
20696 /* Glyphs inserted at the end of a non-empty line for
20697 positioning the cursor have zero charpos, so we must
20698 deduce the value of point by other means. */
20699 if (g->charpos > 0)
20700 SET_PT (g->charpos);
20701 else if (row->ends_at_zv_p && PT != ZV)
20702 SET_PT (ZV);
20703 else if (PT != MATRIX_ROW_END_CHARPOS (row) - 1)
20704 SET_PT (MATRIX_ROW_END_CHARPOS (row) - 1);
20705 else
20706 break;
20707 w->cursor.vpos = -1;
20708 return make_number (PT);
20711 if (g == e || INTEGERP (g->object))
20713 if (row->truncated_on_left_p || row->truncated_on_right_p)
20714 goto simulate_display;
20715 if (!row->reversed_p)
20716 row += dir;
20717 else
20718 row -= dir;
20719 if (row < MATRIX_FIRST_TEXT_ROW (w->current_matrix)
20720 || row > MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w))
20721 goto simulate_display;
20723 if (dir > 0)
20725 if (row->reversed_p && !row->continued_p)
20727 SET_PT (MATRIX_ROW_END_CHARPOS (row) - 1);
20728 w->cursor.vpos = -1;
20729 return make_number (PT);
20731 g = row->glyphs[TEXT_AREA];
20732 e = g + row->used[TEXT_AREA];
20733 for ( ; g < e; g++)
20735 if (BUFFERP (g->object)
20736 /* Empty lines have only one glyph, which stands
20737 for the newline, and whose charpos is the
20738 buffer position of the newline. */
20739 || ROW_GLYPH_NEWLINE_P (row, g)
20740 /* When the buffer ends in a newline, the line at
20741 EOB also has one glyph, but its charpos is -1. */
20742 || (row->ends_at_zv_p
20743 && !row->reversed_p
20744 && INTEGERP (g->object)
20745 && g->type == CHAR_GLYPH
20746 && g->u.ch == ' '))
20748 if (g->charpos > 0)
20749 SET_PT (g->charpos);
20750 else if (!row->reversed_p
20751 && row->ends_at_zv_p
20752 && PT != ZV)
20753 SET_PT (ZV);
20754 else
20755 continue;
20756 w->cursor.vpos = -1;
20757 return make_number (PT);
20761 else
20763 if (!row->reversed_p && !row->continued_p)
20765 SET_PT (MATRIX_ROW_END_CHARPOS (row) - 1);
20766 w->cursor.vpos = -1;
20767 return make_number (PT);
20769 e = row->glyphs[TEXT_AREA];
20770 g = e + row->used[TEXT_AREA] - 1;
20771 for ( ; g >= e; g--)
20773 if (BUFFERP (g->object)
20774 || (ROW_GLYPH_NEWLINE_P (row, g)
20775 && g->charpos > 0)
20776 /* Empty R2L lines on GUI frames have the buffer
20777 position of the newline stored in the stretch
20778 glyph. */
20779 || g->type == STRETCH_GLYPH
20780 || (row->ends_at_zv_p
20781 && row->reversed_p
20782 && INTEGERP (g->object)
20783 && g->type == CHAR_GLYPH
20784 && g->u.ch == ' '))
20786 if (g->charpos > 0)
20787 SET_PT (g->charpos);
20788 else if (row->reversed_p
20789 && row->ends_at_zv_p
20790 && PT != ZV)
20791 SET_PT (ZV);
20792 else
20793 continue;
20794 w->cursor.vpos = -1;
20795 return make_number (PT);
20802 simulate_display:
20804 /* If we wind up here, we failed to move by using the glyphs, so we
20805 need to simulate display instead. */
20807 if (b)
20808 paragraph_dir = Fcurrent_bidi_paragraph_direction (w->contents);
20809 else
20810 paragraph_dir = Qleft_to_right;
20811 if (EQ (paragraph_dir, Qright_to_left))
20812 dir = -dir;
20813 if (PT <= BEGV && dir < 0)
20814 xsignal0 (Qbeginning_of_buffer);
20815 else if (PT >= ZV && dir > 0)
20816 xsignal0 (Qend_of_buffer);
20817 else
20819 struct text_pos pt;
20820 struct it it;
20821 int pt_x, target_x, pixel_width, pt_vpos;
20822 bool at_eol_p;
20823 bool overshoot_expected = false;
20824 bool target_is_eol_p = false;
20826 /* Setup the arena. */
20827 SET_TEXT_POS (pt, PT, PT_BYTE);
20828 start_display (&it, w, pt);
20830 if (it.cmp_it.id < 0
20831 && it.method == GET_FROM_STRING
20832 && it.area == TEXT_AREA
20833 && it.string_from_display_prop_p
20834 && (it.sp > 0 && it.stack[it.sp - 1].method == GET_FROM_BUFFER))
20835 overshoot_expected = true;
20837 /* Find the X coordinate of point. We start from the beginning
20838 of this or previous line to make sure we are before point in
20839 the logical order (since the move_it_* functions can only
20840 move forward). */
20841 reseat:
20842 reseat_at_previous_visible_line_start (&it);
20843 it.current_x = it.hpos = it.current_y = it.vpos = 0;
20844 if (IT_CHARPOS (it) != PT)
20846 move_it_to (&it, overshoot_expected ? PT - 1 : PT,
20847 -1, -1, -1, MOVE_TO_POS);
20848 /* If we missed point because the character there is
20849 displayed out of a display vector that has more than one
20850 glyph, retry expecting overshoot. */
20851 if (it.method == GET_FROM_DISPLAY_VECTOR
20852 && it.current.dpvec_index > 0
20853 && !overshoot_expected)
20855 overshoot_expected = true;
20856 goto reseat;
20858 else if (IT_CHARPOS (it) != PT && !overshoot_expected)
20859 move_it_in_display_line (&it, PT, -1, MOVE_TO_POS);
20861 pt_x = it.current_x;
20862 pt_vpos = it.vpos;
20863 if (dir > 0 || overshoot_expected)
20865 struct glyph_row *row = it.glyph_row;
20867 /* When point is at beginning of line, we don't have
20868 information about the glyph there loaded into struct
20869 it. Calling get_next_display_element fixes that. */
20870 if (pt_x == 0)
20871 get_next_display_element (&it);
20872 at_eol_p = ITERATOR_AT_END_OF_LINE_P (&it);
20873 it.glyph_row = NULL;
20874 PRODUCE_GLYPHS (&it); /* compute it.pixel_width */
20875 it.glyph_row = row;
20876 /* PRODUCE_GLYPHS advances it.current_x, so we must restore
20877 it, lest it will become out of sync with it's buffer
20878 position. */
20879 it.current_x = pt_x;
20881 else
20882 at_eol_p = ITERATOR_AT_END_OF_LINE_P (&it);
20883 pixel_width = it.pixel_width;
20884 if (overshoot_expected && at_eol_p)
20885 pixel_width = 0;
20886 else if (pixel_width <= 0)
20887 pixel_width = 1;
20889 /* If there's a display string (or something similar) at point,
20890 we are actually at the glyph to the left of point, so we need
20891 to correct the X coordinate. */
20892 if (overshoot_expected)
20894 if (it.bidi_p)
20895 pt_x += pixel_width * it.bidi_it.scan_dir;
20896 else
20897 pt_x += pixel_width;
20900 /* Compute target X coordinate, either to the left or to the
20901 right of point. On TTY frames, all characters have the same
20902 pixel width of 1, so we can use that. On GUI frames we don't
20903 have an easy way of getting at the pixel width of the
20904 character to the left of point, so we use a different method
20905 of getting to that place. */
20906 if (dir > 0)
20907 target_x = pt_x + pixel_width;
20908 else
20909 target_x = pt_x - (!FRAME_WINDOW_P (it.f)) * pixel_width;
20911 /* Target X coordinate could be one line above or below the line
20912 of point, in which case we need to adjust the target X
20913 coordinate. Also, if moving to the left, we need to begin at
20914 the left edge of the point's screen line. */
20915 if (dir < 0)
20917 if (pt_x > 0)
20919 start_display (&it, w, pt);
20920 reseat_at_previous_visible_line_start (&it);
20921 it.current_x = it.current_y = it.hpos = 0;
20922 if (pt_vpos != 0)
20923 move_it_by_lines (&it, pt_vpos);
20925 else
20927 move_it_by_lines (&it, -1);
20928 target_x = it.last_visible_x - !FRAME_WINDOW_P (it.f);
20929 target_is_eol_p = true;
20930 /* Under word-wrap, we don't know the x coordinate of
20931 the last character displayed on the previous line,
20932 which immediately precedes the wrap point. To find
20933 out its x coordinate, we try moving to the right
20934 margin of the window, which will stop at the wrap
20935 point, and then reset target_x to point at the
20936 character that precedes the wrap point. This is not
20937 needed on GUI frames, because (see below) there we
20938 move from the left margin one grapheme cluster at a
20939 time, and stop when we hit the wrap point. */
20940 if (!FRAME_WINDOW_P (it.f) && it.line_wrap == WORD_WRAP)
20942 void *it_data = NULL;
20943 struct it it2;
20945 SAVE_IT (it2, it, it_data);
20946 move_it_in_display_line_to (&it, ZV, target_x,
20947 MOVE_TO_POS | MOVE_TO_X);
20948 /* If we arrived at target_x, that _is_ the last
20949 character on the previous line. */
20950 if (it.current_x != target_x)
20951 target_x = it.current_x - 1;
20952 RESTORE_IT (&it, &it2, it_data);
20956 else
20958 if (at_eol_p
20959 || (target_x >= it.last_visible_x
20960 && it.line_wrap != TRUNCATE))
20962 if (pt_x > 0)
20963 move_it_by_lines (&it, 0);
20964 move_it_by_lines (&it, 1);
20965 target_x = 0;
20969 /* Move to the target X coordinate. */
20970 #ifdef HAVE_WINDOW_SYSTEM
20971 /* On GUI frames, as we don't know the X coordinate of the
20972 character to the left of point, moving point to the left
20973 requires walking, one grapheme cluster at a time, until we
20974 find ourself at a place immediately to the left of the
20975 character at point. */
20976 if (FRAME_WINDOW_P (it.f) && dir < 0)
20978 struct text_pos new_pos;
20979 enum move_it_result rc = MOVE_X_REACHED;
20981 if (it.current_x == 0)
20982 get_next_display_element (&it);
20983 if (it.what == IT_COMPOSITION)
20985 new_pos.charpos = it.cmp_it.charpos;
20986 new_pos.bytepos = -1;
20988 else
20989 new_pos = it.current.pos;
20991 while (it.current_x + it.pixel_width <= target_x
20992 && (rc == MOVE_X_REACHED
20993 /* Under word-wrap, move_it_in_display_line_to
20994 stops at correct coordinates, but sometimes
20995 returns MOVE_POS_MATCH_OR_ZV. */
20996 || (it.line_wrap == WORD_WRAP
20997 && rc == MOVE_POS_MATCH_OR_ZV)))
20999 int new_x = it.current_x + it.pixel_width;
21001 /* For composed characters, we want the position of the
21002 first character in the grapheme cluster (usually, the
21003 composition's base character), whereas it.current
21004 might give us the position of the _last_ one, e.g. if
21005 the composition is rendered in reverse due to bidi
21006 reordering. */
21007 if (it.what == IT_COMPOSITION)
21009 new_pos.charpos = it.cmp_it.charpos;
21010 new_pos.bytepos = -1;
21012 else
21013 new_pos = it.current.pos;
21014 if (new_x == it.current_x)
21015 new_x++;
21016 rc = move_it_in_display_line_to (&it, ZV, new_x,
21017 MOVE_TO_POS | MOVE_TO_X);
21018 if (ITERATOR_AT_END_OF_LINE_P (&it) && !target_is_eol_p)
21019 break;
21021 /* The previous position we saw in the loop is the one we
21022 want. */
21023 if (new_pos.bytepos == -1)
21024 new_pos.bytepos = CHAR_TO_BYTE (new_pos.charpos);
21025 it.current.pos = new_pos;
21027 else
21028 #endif
21029 if (it.current_x != target_x)
21030 move_it_in_display_line_to (&it, ZV, target_x, MOVE_TO_POS | MOVE_TO_X);
21032 /* When lines are truncated, the above loop will stop at the
21033 window edge. But we want to get to the end of line, even if
21034 it is beyond the window edge; automatic hscroll will then
21035 scroll the window to show point as appropriate. */
21036 if (target_is_eol_p && it.line_wrap == TRUNCATE
21037 && get_next_display_element (&it))
21039 struct text_pos new_pos = it.current.pos;
21041 while (!ITERATOR_AT_END_OF_LINE_P (&it))
21043 set_iterator_to_next (&it, 0);
21044 if (it.method == GET_FROM_BUFFER)
21045 new_pos = it.current.pos;
21046 if (!get_next_display_element (&it))
21047 break;
21050 it.current.pos = new_pos;
21053 /* If we ended up in a display string that covers point, move to
21054 buffer position to the right in the visual order. */
21055 if (dir > 0)
21057 while (IT_CHARPOS (it) == PT)
21059 set_iterator_to_next (&it, 0);
21060 if (!get_next_display_element (&it))
21061 break;
21065 /* Move point to that position. */
21066 SET_PT_BOTH (IT_CHARPOS (it), IT_BYTEPOS (it));
21069 return make_number (PT);
21071 #undef ROW_GLYPH_NEWLINE_P
21075 /***********************************************************************
21076 Menu Bar
21077 ***********************************************************************/
21079 /* Redisplay the menu bar in the frame for window W.
21081 The menu bar of X frames that don't have X toolkit support is
21082 displayed in a special window W->frame->menu_bar_window.
21084 The menu bar of terminal frames is treated specially as far as
21085 glyph matrices are concerned. Menu bar lines are not part of
21086 windows, so the update is done directly on the frame matrix rows
21087 for the menu bar. */
21089 static void
21090 display_menu_bar (struct window *w)
21092 struct frame *f = XFRAME (WINDOW_FRAME (w));
21093 struct it it;
21094 Lisp_Object items;
21095 int i;
21097 /* Don't do all this for graphical frames. */
21098 #ifdef HAVE_NTGUI
21099 if (FRAME_W32_P (f))
21100 return;
21101 #endif
21102 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
21103 if (FRAME_X_P (f))
21104 return;
21105 #endif
21107 #ifdef HAVE_NS
21108 if (FRAME_NS_P (f))
21109 return;
21110 #endif /* HAVE_NS */
21112 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
21113 eassert (!FRAME_WINDOW_P (f));
21114 init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MENU_FACE_ID);
21115 it.first_visible_x = 0;
21116 it.last_visible_x = FRAME_PIXEL_WIDTH (f);
21117 #elif defined (HAVE_X_WINDOWS) /* X without toolkit. */
21118 if (FRAME_WINDOW_P (f))
21120 /* Menu bar lines are displayed in the desired matrix of the
21121 dummy window menu_bar_window. */
21122 struct window *menu_w;
21123 menu_w = XWINDOW (f->menu_bar_window);
21124 init_iterator (&it, menu_w, -1, -1, menu_w->desired_matrix->rows,
21125 MENU_FACE_ID);
21126 it.first_visible_x = 0;
21127 it.last_visible_x = FRAME_PIXEL_WIDTH (f);
21129 else
21130 #endif /* not USE_X_TOOLKIT and not USE_GTK */
21132 /* This is a TTY frame, i.e. character hpos/vpos are used as
21133 pixel x/y. */
21134 init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
21135 MENU_FACE_ID);
21136 it.first_visible_x = 0;
21137 it.last_visible_x = FRAME_COLS (f);
21140 /* FIXME: This should be controlled by a user option. See the
21141 comments in redisplay_tool_bar and display_mode_line about
21142 this. */
21143 it.paragraph_embedding = L2R;
21145 /* Clear all rows of the menu bar. */
21146 for (i = 0; i < FRAME_MENU_BAR_LINES (f); ++i)
21148 struct glyph_row *row = it.glyph_row + i;
21149 clear_glyph_row (row);
21150 row->enabled_p = true;
21151 row->full_width_p = 1;
21154 /* Display all items of the menu bar. */
21155 items = FRAME_MENU_BAR_ITEMS (it.f);
21156 for (i = 0; i < ASIZE (items); i += 4)
21158 Lisp_Object string;
21160 /* Stop at nil string. */
21161 string = AREF (items, i + 1);
21162 if (NILP (string))
21163 break;
21165 /* Remember where item was displayed. */
21166 ASET (items, i + 3, make_number (it.hpos));
21168 /* Display the item, pad with one space. */
21169 if (it.current_x < it.last_visible_x)
21170 display_string (NULL, string, Qnil, 0, 0, &it,
21171 SCHARS (string) + 1, 0, 0, -1);
21174 /* Fill out the line with spaces. */
21175 if (it.current_x < it.last_visible_x)
21176 display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
21178 /* Compute the total height of the lines. */
21179 compute_line_metrics (&it);
21182 /* Deep copy of a glyph row, including the glyphs. */
21183 static void
21184 deep_copy_glyph_row (struct glyph_row *to, struct glyph_row *from)
21186 struct glyph *pointers[1 + LAST_AREA];
21187 int to_used = to->used[TEXT_AREA];
21189 /* Save glyph pointers of TO. */
21190 memcpy (pointers, to->glyphs, sizeof to->glyphs);
21192 /* Do a structure assignment. */
21193 *to = *from;
21195 /* Restore original glyph pointers of TO. */
21196 memcpy (to->glyphs, pointers, sizeof to->glyphs);
21198 /* Copy the glyphs. */
21199 memcpy (to->glyphs[TEXT_AREA], from->glyphs[TEXT_AREA],
21200 min (from->used[TEXT_AREA], to_used) * sizeof (struct glyph));
21202 /* If we filled only part of the TO row, fill the rest with
21203 space_glyph (which will display as empty space). */
21204 if (to_used > from->used[TEXT_AREA])
21205 fill_up_frame_row_with_spaces (to, to_used);
21208 /* Display one menu item on a TTY, by overwriting the glyphs in the
21209 frame F's desired glyph matrix with glyphs produced from the menu
21210 item text. Called from term.c to display TTY drop-down menus one
21211 item at a time.
21213 ITEM_TEXT is the menu item text as a C string.
21215 FACE_ID is the face ID to be used for this menu item. FACE_ID
21216 could specify one of 3 faces: a face for an enabled item, a face
21217 for a disabled item, or a face for a selected item.
21219 X and Y are coordinates of the first glyph in the frame's desired
21220 matrix to be overwritten by the menu item. Since this is a TTY, Y
21221 is the zero-based number of the glyph row and X is the zero-based
21222 glyph number in the row, starting from left, where to start
21223 displaying the item.
21225 SUBMENU non-zero means this menu item drops down a submenu, which
21226 should be indicated by displaying a proper visual cue after the
21227 item text. */
21229 void
21230 display_tty_menu_item (const char *item_text, int width, int face_id,
21231 int x, int y, int submenu)
21233 struct it it;
21234 struct frame *f = SELECTED_FRAME ();
21235 struct window *w = XWINDOW (f->selected_window);
21236 int saved_used, saved_truncated, saved_width, saved_reversed;
21237 struct glyph_row *row;
21238 size_t item_len = strlen (item_text);
21240 eassert (FRAME_TERMCAP_P (f));
21242 /* Don't write beyond the matrix's last row. This can happen for
21243 TTY screens that are not high enough to show the entire menu.
21244 (This is actually a bit of defensive programming, as
21245 tty_menu_display already limits the number of menu items to one
21246 less than the number of screen lines.) */
21247 if (y >= f->desired_matrix->nrows)
21248 return;
21250 init_iterator (&it, w, -1, -1, f->desired_matrix->rows + y, MENU_FACE_ID);
21251 it.first_visible_x = 0;
21252 it.last_visible_x = FRAME_COLS (f) - 1;
21253 row = it.glyph_row;
21254 /* Start with the row contents from the current matrix. */
21255 deep_copy_glyph_row (row, f->current_matrix->rows + y);
21256 saved_width = row->full_width_p;
21257 row->full_width_p = 1;
21258 saved_reversed = row->reversed_p;
21259 row->reversed_p = 0;
21260 row->enabled_p = true;
21262 /* Arrange for the menu item glyphs to start at (X,Y) and have the
21263 desired face. */
21264 eassert (x < f->desired_matrix->matrix_w);
21265 it.current_x = it.hpos = x;
21266 it.current_y = it.vpos = y;
21267 saved_used = row->used[TEXT_AREA];
21268 saved_truncated = row->truncated_on_right_p;
21269 row->used[TEXT_AREA] = x;
21270 it.face_id = face_id;
21271 it.line_wrap = TRUNCATE;
21273 /* FIXME: This should be controlled by a user option. See the
21274 comments in redisplay_tool_bar and display_mode_line about this.
21275 Also, if paragraph_embedding could ever be R2L, changes will be
21276 needed to avoid shifting to the right the row characters in
21277 term.c:append_glyph. */
21278 it.paragraph_embedding = L2R;
21280 /* Pad with a space on the left. */
21281 display_string (" ", Qnil, Qnil, 0, 0, &it, 1, 0, FRAME_COLS (f) - 1, -1);
21282 width--;
21283 /* Display the menu item, pad with spaces to WIDTH. */
21284 if (submenu)
21286 display_string (item_text, Qnil, Qnil, 0, 0, &it,
21287 item_len, 0, FRAME_COLS (f) - 1, -1);
21288 width -= item_len;
21289 /* Indicate with " >" that there's a submenu. */
21290 display_string (" >", Qnil, Qnil, 0, 0, &it, width, 0,
21291 FRAME_COLS (f) - 1, -1);
21293 else
21294 display_string (item_text, Qnil, Qnil, 0, 0, &it,
21295 width, 0, FRAME_COLS (f) - 1, -1);
21297 row->used[TEXT_AREA] = max (saved_used, row->used[TEXT_AREA]);
21298 row->truncated_on_right_p = saved_truncated;
21299 row->hash = row_hash (row);
21300 row->full_width_p = saved_width;
21301 row->reversed_p = saved_reversed;
21304 /***********************************************************************
21305 Mode Line
21306 ***********************************************************************/
21308 /* Redisplay mode lines in the window tree whose root is WINDOW. If
21309 FORCE is non-zero, redisplay mode lines unconditionally.
21310 Otherwise, redisplay only mode lines that are garbaged. Value is
21311 the number of windows whose mode lines were redisplayed. */
21313 static int
21314 redisplay_mode_lines (Lisp_Object window, bool force)
21316 int nwindows = 0;
21318 while (!NILP (window))
21320 struct window *w = XWINDOW (window);
21322 if (WINDOWP (w->contents))
21323 nwindows += redisplay_mode_lines (w->contents, force);
21324 else if (force
21325 || FRAME_GARBAGED_P (XFRAME (w->frame))
21326 || !MATRIX_MODE_LINE_ROW (w->current_matrix)->enabled_p)
21328 struct text_pos lpoint;
21329 struct buffer *old = current_buffer;
21331 /* Set the window's buffer for the mode line display. */
21332 SET_TEXT_POS (lpoint, PT, PT_BYTE);
21333 set_buffer_internal_1 (XBUFFER (w->contents));
21335 /* Point refers normally to the selected window. For any
21336 other window, set up appropriate value. */
21337 if (!EQ (window, selected_window))
21339 struct text_pos pt;
21341 CLIP_TEXT_POS_FROM_MARKER (pt, w->pointm);
21342 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
21345 /* Display mode lines. */
21346 clear_glyph_matrix (w->desired_matrix);
21347 if (display_mode_lines (w))
21348 ++nwindows;
21350 /* Restore old settings. */
21351 set_buffer_internal_1 (old);
21352 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
21355 window = w->next;
21358 return nwindows;
21362 /* Display the mode and/or header line of window W. Value is the
21363 sum number of mode lines and header lines displayed. */
21365 static int
21366 display_mode_lines (struct window *w)
21368 Lisp_Object old_selected_window = selected_window;
21369 Lisp_Object old_selected_frame = selected_frame;
21370 Lisp_Object new_frame = w->frame;
21371 Lisp_Object old_frame_selected_window = XFRAME (new_frame)->selected_window;
21372 int n = 0;
21374 selected_frame = new_frame;
21375 /* FIXME: If we were to allow the mode-line's computation changing the buffer
21376 or window's point, then we'd need select_window_1 here as well. */
21377 XSETWINDOW (selected_window, w);
21378 XFRAME (new_frame)->selected_window = selected_window;
21380 /* These will be set while the mode line specs are processed. */
21381 line_number_displayed = 0;
21382 w->column_number_displayed = -1;
21384 if (WINDOW_WANTS_MODELINE_P (w))
21386 struct window *sel_w = XWINDOW (old_selected_window);
21388 /* Select mode line face based on the real selected window. */
21389 display_mode_line (w, CURRENT_MODE_LINE_FACE_ID_3 (sel_w, sel_w, w),
21390 BVAR (current_buffer, mode_line_format));
21391 ++n;
21394 if (WINDOW_WANTS_HEADER_LINE_P (w))
21396 display_mode_line (w, HEADER_LINE_FACE_ID,
21397 BVAR (current_buffer, header_line_format));
21398 ++n;
21401 XFRAME (new_frame)->selected_window = old_frame_selected_window;
21402 selected_frame = old_selected_frame;
21403 selected_window = old_selected_window;
21404 if (n > 0)
21405 w->must_be_updated_p = true;
21406 return n;
21410 /* Display mode or header line of window W. FACE_ID specifies which
21411 line to display; it is either MODE_LINE_FACE_ID or
21412 HEADER_LINE_FACE_ID. FORMAT is the mode/header line format to
21413 display. Value is the pixel height of the mode/header line
21414 displayed. */
21416 static int
21417 display_mode_line (struct window *w, enum face_id face_id, Lisp_Object format)
21419 struct it it;
21420 struct face *face;
21421 ptrdiff_t count = SPECPDL_INDEX ();
21423 init_iterator (&it, w, -1, -1, NULL, face_id);
21424 /* Don't extend on a previously drawn mode-line.
21425 This may happen if called from pos_visible_p. */
21426 it.glyph_row->enabled_p = false;
21427 prepare_desired_row (it.glyph_row);
21429 it.glyph_row->mode_line_p = 1;
21431 /* FIXME: This should be controlled by a user option. But
21432 supporting such an option is not trivial, since the mode line is
21433 made up of many separate strings. */
21434 it.paragraph_embedding = L2R;
21436 record_unwind_protect (unwind_format_mode_line,
21437 format_mode_line_unwind_data (NULL, NULL, Qnil, 0));
21439 mode_line_target = MODE_LINE_DISPLAY;
21441 /* Temporarily make frame's keyboard the current kboard so that
21442 kboard-local variables in the mode_line_format will get the right
21443 values. */
21444 push_kboard (FRAME_KBOARD (it.f));
21445 record_unwind_save_match_data ();
21446 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
21447 pop_kboard ();
21449 unbind_to (count, Qnil);
21451 /* Fill up with spaces. */
21452 display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);
21454 compute_line_metrics (&it);
21455 it.glyph_row->full_width_p = 1;
21456 it.glyph_row->continued_p = 0;
21457 it.glyph_row->truncated_on_left_p = 0;
21458 it.glyph_row->truncated_on_right_p = 0;
21460 /* Make a 3D mode-line have a shadow at its right end. */
21461 face = FACE_FROM_ID (it.f, face_id);
21462 extend_face_to_end_of_line (&it);
21463 if (face->box != FACE_NO_BOX)
21465 struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
21466 + it.glyph_row->used[TEXT_AREA] - 1);
21467 last->right_box_line_p = 1;
21470 return it.glyph_row->height;
21473 /* Move element ELT in LIST to the front of LIST.
21474 Return the updated list. */
21476 static Lisp_Object
21477 move_elt_to_front (Lisp_Object elt, Lisp_Object list)
21479 register Lisp_Object tail, prev;
21480 register Lisp_Object tem;
21482 tail = list;
21483 prev = Qnil;
21484 while (CONSP (tail))
21486 tem = XCAR (tail);
21488 if (EQ (elt, tem))
21490 /* Splice out the link TAIL. */
21491 if (NILP (prev))
21492 list = XCDR (tail);
21493 else
21494 Fsetcdr (prev, XCDR (tail));
21496 /* Now make it the first. */
21497 Fsetcdr (tail, list);
21498 return tail;
21500 else
21501 prev = tail;
21502 tail = XCDR (tail);
21503 QUIT;
21506 /* Not found--return unchanged LIST. */
21507 return list;
21510 /* Contribute ELT to the mode line for window IT->w. How it
21511 translates into text depends on its data type.
21513 IT describes the display environment in which we display, as usual.
21515 DEPTH is the depth in recursion. It is used to prevent
21516 infinite recursion here.
21518 FIELD_WIDTH is the number of characters the display of ELT should
21519 occupy in the mode line, and PRECISION is the maximum number of
21520 characters to display from ELT's representation. See
21521 display_string for details.
21523 Returns the hpos of the end of the text generated by ELT.
21525 PROPS is a property list to add to any string we encounter.
21527 If RISKY is nonzero, remove (disregard) any properties in any string
21528 we encounter, and ignore :eval and :propertize.
21530 The global variable `mode_line_target' determines whether the
21531 output is passed to `store_mode_line_noprop',
21532 `store_mode_line_string', or `display_string'. */
21534 static int
21535 display_mode_element (struct it *it, int depth, int field_width, int precision,
21536 Lisp_Object elt, Lisp_Object props, int risky)
21538 int n = 0, field, prec;
21539 int literal = 0;
21541 tail_recurse:
21542 if (depth > 100)
21543 elt = build_string ("*too-deep*");
21545 depth++;
21547 switch (XTYPE (elt))
21549 case Lisp_String:
21551 /* A string: output it and check for %-constructs within it. */
21552 unsigned char c;
21553 ptrdiff_t offset = 0;
21555 if (SCHARS (elt) > 0
21556 && (!NILP (props) || risky))
21558 Lisp_Object oprops, aelt;
21559 oprops = Ftext_properties_at (make_number (0), elt);
21561 /* If the starting string's properties are not what
21562 we want, translate the string. Also, if the string
21563 is risky, do that anyway. */
21565 if (NILP (Fequal (props, oprops)) || risky)
21567 /* If the starting string has properties,
21568 merge the specified ones onto the existing ones. */
21569 if (! NILP (oprops) && !risky)
21571 Lisp_Object tem;
21573 oprops = Fcopy_sequence (oprops);
21574 tem = props;
21575 while (CONSP (tem))
21577 oprops = Fplist_put (oprops, XCAR (tem),
21578 XCAR (XCDR (tem)));
21579 tem = XCDR (XCDR (tem));
21581 props = oprops;
21584 aelt = Fassoc (elt, mode_line_proptrans_alist);
21585 if (! NILP (aelt) && !NILP (Fequal (props, XCDR (aelt))))
21587 /* AELT is what we want. Move it to the front
21588 without consing. */
21589 elt = XCAR (aelt);
21590 mode_line_proptrans_alist
21591 = move_elt_to_front (aelt, mode_line_proptrans_alist);
21593 else
21595 Lisp_Object tem;
21597 /* If AELT has the wrong props, it is useless.
21598 so get rid of it. */
21599 if (! NILP (aelt))
21600 mode_line_proptrans_alist
21601 = Fdelq (aelt, mode_line_proptrans_alist);
21603 elt = Fcopy_sequence (elt);
21604 Fset_text_properties (make_number (0), Flength (elt),
21605 props, elt);
21606 /* Add this item to mode_line_proptrans_alist. */
21607 mode_line_proptrans_alist
21608 = Fcons (Fcons (elt, props),
21609 mode_line_proptrans_alist);
21610 /* Truncate mode_line_proptrans_alist
21611 to at most 50 elements. */
21612 tem = Fnthcdr (make_number (50),
21613 mode_line_proptrans_alist);
21614 if (! NILP (tem))
21615 XSETCDR (tem, Qnil);
21620 offset = 0;
21622 if (literal)
21624 prec = precision - n;
21625 switch (mode_line_target)
21627 case MODE_LINE_NOPROP:
21628 case MODE_LINE_TITLE:
21629 n += store_mode_line_noprop (SSDATA (elt), -1, prec);
21630 break;
21631 case MODE_LINE_STRING:
21632 n += store_mode_line_string (NULL, elt, 1, 0, prec, Qnil);
21633 break;
21634 case MODE_LINE_DISPLAY:
21635 n += display_string (NULL, elt, Qnil, 0, 0, it,
21636 0, prec, 0, STRING_MULTIBYTE (elt));
21637 break;
21640 break;
21643 /* Handle the non-literal case. */
21645 while ((precision <= 0 || n < precision)
21646 && SREF (elt, offset) != 0
21647 && (mode_line_target != MODE_LINE_DISPLAY
21648 || it->current_x < it->last_visible_x))
21650 ptrdiff_t last_offset = offset;
21652 /* Advance to end of string or next format specifier. */
21653 while ((c = SREF (elt, offset++)) != '\0' && c != '%')
21656 if (offset - 1 != last_offset)
21658 ptrdiff_t nchars, nbytes;
21660 /* Output to end of string or up to '%'. Field width
21661 is length of string. Don't output more than
21662 PRECISION allows us. */
21663 offset--;
21665 prec = c_string_width (SDATA (elt) + last_offset,
21666 offset - last_offset, precision - n,
21667 &nchars, &nbytes);
21669 switch (mode_line_target)
21671 case MODE_LINE_NOPROP:
21672 case MODE_LINE_TITLE:
21673 n += store_mode_line_noprop (SSDATA (elt) + last_offset, 0, prec);
21674 break;
21675 case MODE_LINE_STRING:
21677 ptrdiff_t bytepos = last_offset;
21678 ptrdiff_t charpos = string_byte_to_char (elt, bytepos);
21679 ptrdiff_t endpos = (precision <= 0
21680 ? string_byte_to_char (elt, offset)
21681 : charpos + nchars);
21683 n += store_mode_line_string (NULL,
21684 Fsubstring (elt, make_number (charpos),
21685 make_number (endpos)),
21686 0, 0, 0, Qnil);
21688 break;
21689 case MODE_LINE_DISPLAY:
21691 ptrdiff_t bytepos = last_offset;
21692 ptrdiff_t charpos = string_byte_to_char (elt, bytepos);
21694 if (precision <= 0)
21695 nchars = string_byte_to_char (elt, offset) - charpos;
21696 n += display_string (NULL, elt, Qnil, 0, charpos,
21697 it, 0, nchars, 0,
21698 STRING_MULTIBYTE (elt));
21700 break;
21703 else /* c == '%' */
21705 ptrdiff_t percent_position = offset;
21707 /* Get the specified minimum width. Zero means
21708 don't pad. */
21709 field = 0;
21710 while ((c = SREF (elt, offset++)) >= '0' && c <= '9')
21711 field = field * 10 + c - '0';
21713 /* Don't pad beyond the total padding allowed. */
21714 if (field_width - n > 0 && field > field_width - n)
21715 field = field_width - n;
21717 /* Note that either PRECISION <= 0 or N < PRECISION. */
21718 prec = precision - n;
21720 if (c == 'M')
21721 n += display_mode_element (it, depth, field, prec,
21722 Vglobal_mode_string, props,
21723 risky);
21724 else if (c != 0)
21726 bool multibyte;
21727 ptrdiff_t bytepos, charpos;
21728 const char *spec;
21729 Lisp_Object string;
21731 bytepos = percent_position;
21732 charpos = (STRING_MULTIBYTE (elt)
21733 ? string_byte_to_char (elt, bytepos)
21734 : bytepos);
21735 spec = decode_mode_spec (it->w, c, field, &string);
21736 multibyte = STRINGP (string) && STRING_MULTIBYTE (string);
21738 switch (mode_line_target)
21740 case MODE_LINE_NOPROP:
21741 case MODE_LINE_TITLE:
21742 n += store_mode_line_noprop (spec, field, prec);
21743 break;
21744 case MODE_LINE_STRING:
21746 Lisp_Object tem = build_string (spec);
21747 props = Ftext_properties_at (make_number (charpos), elt);
21748 /* Should only keep face property in props */
21749 n += store_mode_line_string (NULL, tem, 0, field, prec, props);
21751 break;
21752 case MODE_LINE_DISPLAY:
21754 int nglyphs_before, nwritten;
21756 nglyphs_before = it->glyph_row->used[TEXT_AREA];
21757 nwritten = display_string (spec, string, elt,
21758 charpos, 0, it,
21759 field, prec, 0,
21760 multibyte);
21762 /* Assign to the glyphs written above the
21763 string where the `%x' came from, position
21764 of the `%'. */
21765 if (nwritten > 0)
21767 struct glyph *glyph
21768 = (it->glyph_row->glyphs[TEXT_AREA]
21769 + nglyphs_before);
21770 int i;
21772 for (i = 0; i < nwritten; ++i)
21774 glyph[i].object = elt;
21775 glyph[i].charpos = charpos;
21778 n += nwritten;
21781 break;
21784 else /* c == 0 */
21785 break;
21789 break;
21791 case Lisp_Symbol:
21792 /* A symbol: process the value of the symbol recursively
21793 as if it appeared here directly. Avoid error if symbol void.
21794 Special case: if value of symbol is a string, output the string
21795 literally. */
21797 register Lisp_Object tem;
21799 /* If the variable is not marked as risky to set
21800 then its contents are risky to use. */
21801 if (NILP (Fget (elt, Qrisky_local_variable)))
21802 risky = 1;
21804 tem = Fboundp (elt);
21805 if (!NILP (tem))
21807 tem = Fsymbol_value (elt);
21808 /* If value is a string, output that string literally:
21809 don't check for % within it. */
21810 if (STRINGP (tem))
21811 literal = 1;
21813 if (!EQ (tem, elt))
21815 /* Give up right away for nil or t. */
21816 elt = tem;
21817 goto tail_recurse;
21821 break;
21823 case Lisp_Cons:
21825 register Lisp_Object car, tem;
21827 /* A cons cell: five distinct cases.
21828 If first element is :eval or :propertize, do something special.
21829 If first element is a string or a cons, process all the elements
21830 and effectively concatenate them.
21831 If first element is a negative number, truncate displaying cdr to
21832 at most that many characters. If positive, pad (with spaces)
21833 to at least that many characters.
21834 If first element is a symbol, process the cadr or caddr recursively
21835 according to whether the symbol's value is non-nil or nil. */
21836 car = XCAR (elt);
21837 if (EQ (car, QCeval))
21839 /* An element of the form (:eval FORM) means evaluate FORM
21840 and use the result as mode line elements. */
21842 if (risky)
21843 break;
21845 if (CONSP (XCDR (elt)))
21847 Lisp_Object spec;
21848 spec = safe_eval (XCAR (XCDR (elt)));
21849 n += display_mode_element (it, depth, field_width - n,
21850 precision - n, spec, props,
21851 risky);
21854 else if (EQ (car, QCpropertize))
21856 /* An element of the form (:propertize ELT PROPS...)
21857 means display ELT but applying properties PROPS. */
21859 if (risky)
21860 break;
21862 if (CONSP (XCDR (elt)))
21863 n += display_mode_element (it, depth, field_width - n,
21864 precision - n, XCAR (XCDR (elt)),
21865 XCDR (XCDR (elt)), risky);
21867 else if (SYMBOLP (car))
21869 tem = Fboundp (car);
21870 elt = XCDR (elt);
21871 if (!CONSP (elt))
21872 goto invalid;
21873 /* elt is now the cdr, and we know it is a cons cell.
21874 Use its car if CAR has a non-nil value. */
21875 if (!NILP (tem))
21877 tem = Fsymbol_value (car);
21878 if (!NILP (tem))
21880 elt = XCAR (elt);
21881 goto tail_recurse;
21884 /* Symbol's value is nil (or symbol is unbound)
21885 Get the cddr of the original list
21886 and if possible find the caddr and use that. */
21887 elt = XCDR (elt);
21888 if (NILP (elt))
21889 break;
21890 else if (!CONSP (elt))
21891 goto invalid;
21892 elt = XCAR (elt);
21893 goto tail_recurse;
21895 else if (INTEGERP (car))
21897 register int lim = XINT (car);
21898 elt = XCDR (elt);
21899 if (lim < 0)
21901 /* Negative int means reduce maximum width. */
21902 if (precision <= 0)
21903 precision = -lim;
21904 else
21905 precision = min (precision, -lim);
21907 else if (lim > 0)
21909 /* Padding specified. Don't let it be more than
21910 current maximum. */
21911 if (precision > 0)
21912 lim = min (precision, lim);
21914 /* If that's more padding than already wanted, queue it.
21915 But don't reduce padding already specified even if
21916 that is beyond the current truncation point. */
21917 field_width = max (lim, field_width);
21919 goto tail_recurse;
21921 else if (STRINGP (car) || CONSP (car))
21923 Lisp_Object halftail = elt;
21924 int len = 0;
21926 while (CONSP (elt)
21927 && (precision <= 0 || n < precision))
21929 n += display_mode_element (it, depth,
21930 /* Do padding only after the last
21931 element in the list. */
21932 (! CONSP (XCDR (elt))
21933 ? field_width - n
21934 : 0),
21935 precision - n, XCAR (elt),
21936 props, risky);
21937 elt = XCDR (elt);
21938 len++;
21939 if ((len & 1) == 0)
21940 halftail = XCDR (halftail);
21941 /* Check for cycle. */
21942 if (EQ (halftail, elt))
21943 break;
21947 break;
21949 default:
21950 invalid:
21951 elt = build_string ("*invalid*");
21952 goto tail_recurse;
21955 /* Pad to FIELD_WIDTH. */
21956 if (field_width > 0 && n < field_width)
21958 switch (mode_line_target)
21960 case MODE_LINE_NOPROP:
21961 case MODE_LINE_TITLE:
21962 n += store_mode_line_noprop ("", field_width - n, 0);
21963 break;
21964 case MODE_LINE_STRING:
21965 n += store_mode_line_string ("", Qnil, 0, field_width - n, 0, Qnil);
21966 break;
21967 case MODE_LINE_DISPLAY:
21968 n += display_string ("", Qnil, Qnil, 0, 0, it, field_width - n,
21969 0, 0, 0);
21970 break;
21974 return n;
21977 /* Store a mode-line string element in mode_line_string_list.
21979 If STRING is non-null, display that C string. Otherwise, the Lisp
21980 string LISP_STRING is displayed.
21982 FIELD_WIDTH is the minimum number of output glyphs to produce.
21983 If STRING has fewer characters than FIELD_WIDTH, pad to the right
21984 with spaces. FIELD_WIDTH <= 0 means don't pad.
21986 PRECISION is the maximum number of characters to output from
21987 STRING. PRECISION <= 0 means don't truncate the string.
21989 If COPY_STRING is non-zero, make a copy of LISP_STRING before adding
21990 properties to the string.
21992 PROPS are the properties to add to the string.
21993 The mode_line_string_face face property is always added to the string.
21996 static int
21997 store_mode_line_string (const char *string, Lisp_Object lisp_string, int copy_string,
21998 int field_width, int precision, Lisp_Object props)
22000 ptrdiff_t len;
22001 int n = 0;
22003 if (string != NULL)
22005 len = strlen (string);
22006 if (precision > 0 && len > precision)
22007 len = precision;
22008 lisp_string = make_string (string, len);
22009 if (NILP (props))
22010 props = mode_line_string_face_prop;
22011 else if (!NILP (mode_line_string_face))
22013 Lisp_Object face = Fplist_get (props, Qface);
22014 props = Fcopy_sequence (props);
22015 if (NILP (face))
22016 face = mode_line_string_face;
22017 else
22018 face = list2 (face, mode_line_string_face);
22019 props = Fplist_put (props, Qface, face);
22021 Fadd_text_properties (make_number (0), make_number (len),
22022 props, lisp_string);
22024 else
22026 len = XFASTINT (Flength (lisp_string));
22027 if (precision > 0 && len > precision)
22029 len = precision;
22030 lisp_string = Fsubstring (lisp_string, make_number (0), make_number (len));
22031 precision = -1;
22033 if (!NILP (mode_line_string_face))
22035 Lisp_Object face;
22036 if (NILP (props))
22037 props = Ftext_properties_at (make_number (0), lisp_string);
22038 face = Fplist_get (props, Qface);
22039 if (NILP (face))
22040 face = mode_line_string_face;
22041 else
22042 face = list2 (face, mode_line_string_face);
22043 props = list2 (Qface, face);
22044 if (copy_string)
22045 lisp_string = Fcopy_sequence (lisp_string);
22047 if (!NILP (props))
22048 Fadd_text_properties (make_number (0), make_number (len),
22049 props, lisp_string);
22052 if (len > 0)
22054 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
22055 n += len;
22058 if (field_width > len)
22060 field_width -= len;
22061 lisp_string = Fmake_string (make_number (field_width), make_number (' '));
22062 if (!NILP (props))
22063 Fadd_text_properties (make_number (0), make_number (field_width),
22064 props, lisp_string);
22065 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
22066 n += field_width;
22069 return n;
22073 DEFUN ("format-mode-line", Fformat_mode_line, Sformat_mode_line,
22074 1, 4, 0,
22075 doc: /* Format a string out of a mode line format specification.
22076 First arg FORMAT specifies the mode line format (see `mode-line-format'
22077 for details) to use.
22079 By default, the format is evaluated for the currently selected window.
22081 Optional second arg FACE specifies the face property to put on all
22082 characters for which no face is specified. The value nil means the
22083 default face. The value t means whatever face the window's mode line
22084 currently uses (either `mode-line' or `mode-line-inactive',
22085 depending on whether the window is the selected window or not).
22086 An integer value means the value string has no text
22087 properties.
22089 Optional third and fourth args WINDOW and BUFFER specify the window
22090 and buffer to use as the context for the formatting (defaults
22091 are the selected window and the WINDOW's buffer). */)
22092 (Lisp_Object format, Lisp_Object face,
22093 Lisp_Object window, Lisp_Object buffer)
22095 struct it it;
22096 int len;
22097 struct window *w;
22098 struct buffer *old_buffer = NULL;
22099 int face_id;
22100 int no_props = INTEGERP (face);
22101 ptrdiff_t count = SPECPDL_INDEX ();
22102 Lisp_Object str;
22103 int string_start = 0;
22105 w = decode_any_window (window);
22106 XSETWINDOW (window, w);
22108 if (NILP (buffer))
22109 buffer = w->contents;
22110 CHECK_BUFFER (buffer);
22112 /* Make formatting the modeline a non-op when noninteractive, otherwise
22113 there will be problems later caused by a partially initialized frame. */
22114 if (NILP (format) || noninteractive)
22115 return empty_unibyte_string;
22117 if (no_props)
22118 face = Qnil;
22120 face_id = (NILP (face) || EQ (face, Qdefault)) ? DEFAULT_FACE_ID
22121 : EQ (face, Qt) ? (EQ (window, selected_window)
22122 ? MODE_LINE_FACE_ID : MODE_LINE_INACTIVE_FACE_ID)
22123 : EQ (face, Qmode_line) ? MODE_LINE_FACE_ID
22124 : EQ (face, Qmode_line_inactive) ? MODE_LINE_INACTIVE_FACE_ID
22125 : EQ (face, Qheader_line) ? HEADER_LINE_FACE_ID
22126 : EQ (face, Qtool_bar) ? TOOL_BAR_FACE_ID
22127 : DEFAULT_FACE_ID;
22129 old_buffer = current_buffer;
22131 /* Save things including mode_line_proptrans_alist,
22132 and set that to nil so that we don't alter the outer value. */
22133 record_unwind_protect (unwind_format_mode_line,
22134 format_mode_line_unwind_data
22135 (XFRAME (WINDOW_FRAME (w)),
22136 old_buffer, selected_window, 1));
22137 mode_line_proptrans_alist = Qnil;
22139 Fselect_window (window, Qt);
22140 set_buffer_internal_1 (XBUFFER (buffer));
22142 init_iterator (&it, w, -1, -1, NULL, face_id);
22144 if (no_props)
22146 mode_line_target = MODE_LINE_NOPROP;
22147 mode_line_string_face_prop = Qnil;
22148 mode_line_string_list = Qnil;
22149 string_start = MODE_LINE_NOPROP_LEN (0);
22151 else
22153 mode_line_target = MODE_LINE_STRING;
22154 mode_line_string_list = Qnil;
22155 mode_line_string_face = face;
22156 mode_line_string_face_prop
22157 = NILP (face) ? Qnil : list2 (Qface, face);
22160 push_kboard (FRAME_KBOARD (it.f));
22161 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
22162 pop_kboard ();
22164 if (no_props)
22166 len = MODE_LINE_NOPROP_LEN (string_start);
22167 str = make_string (mode_line_noprop_buf + string_start, len);
22169 else
22171 mode_line_string_list = Fnreverse (mode_line_string_list);
22172 str = Fmapconcat (intern ("identity"), mode_line_string_list,
22173 empty_unibyte_string);
22176 unbind_to (count, Qnil);
22177 return str;
22180 /* Write a null-terminated, right justified decimal representation of
22181 the positive integer D to BUF using a minimal field width WIDTH. */
22183 static void
22184 pint2str (register char *buf, register int width, register ptrdiff_t d)
22186 register char *p = buf;
22188 if (d <= 0)
22189 *p++ = '0';
22190 else
22192 while (d > 0)
22194 *p++ = d % 10 + '0';
22195 d /= 10;
22199 for (width -= (int) (p - buf); width > 0; --width)
22200 *p++ = ' ';
22201 *p-- = '\0';
22202 while (p > buf)
22204 d = *buf;
22205 *buf++ = *p;
22206 *p-- = d;
22210 /* Write a null-terminated, right justified decimal and "human
22211 readable" representation of the nonnegative integer D to BUF using
22212 a minimal field width WIDTH. D should be smaller than 999.5e24. */
22214 static const char power_letter[] =
22216 0, /* no letter */
22217 'k', /* kilo */
22218 'M', /* mega */
22219 'G', /* giga */
22220 'T', /* tera */
22221 'P', /* peta */
22222 'E', /* exa */
22223 'Z', /* zetta */
22224 'Y' /* yotta */
22227 static void
22228 pint2hrstr (char *buf, int width, ptrdiff_t d)
22230 /* We aim to represent the nonnegative integer D as
22231 QUOTIENT.TENTHS * 10 ^ (3 * EXPONENT). */
22232 ptrdiff_t quotient = d;
22233 int remainder = 0;
22234 /* -1 means: do not use TENTHS. */
22235 int tenths = -1;
22236 int exponent = 0;
22238 /* Length of QUOTIENT.TENTHS as a string. */
22239 int length;
22241 char * psuffix;
22242 char * p;
22244 if (quotient >= 1000)
22246 /* Scale to the appropriate EXPONENT. */
22249 remainder = quotient % 1000;
22250 quotient /= 1000;
22251 exponent++;
22253 while (quotient >= 1000);
22255 /* Round to nearest and decide whether to use TENTHS or not. */
22256 if (quotient <= 9)
22258 tenths = remainder / 100;
22259 if (remainder % 100 >= 50)
22261 if (tenths < 9)
22262 tenths++;
22263 else
22265 quotient++;
22266 if (quotient == 10)
22267 tenths = -1;
22268 else
22269 tenths = 0;
22273 else
22274 if (remainder >= 500)
22276 if (quotient < 999)
22277 quotient++;
22278 else
22280 quotient = 1;
22281 exponent++;
22282 tenths = 0;
22287 /* Calculate the LENGTH of QUOTIENT.TENTHS as a string. */
22288 if (tenths == -1 && quotient <= 99)
22289 if (quotient <= 9)
22290 length = 1;
22291 else
22292 length = 2;
22293 else
22294 length = 3;
22295 p = psuffix = buf + max (width, length);
22297 /* Print EXPONENT. */
22298 *psuffix++ = power_letter[exponent];
22299 *psuffix = '\0';
22301 /* Print TENTHS. */
22302 if (tenths >= 0)
22304 *--p = '0' + tenths;
22305 *--p = '.';
22308 /* Print QUOTIENT. */
22311 int digit = quotient % 10;
22312 *--p = '0' + digit;
22314 while ((quotient /= 10) != 0);
22316 /* Print leading spaces. */
22317 while (buf < p)
22318 *--p = ' ';
22321 /* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
22322 If EOL_FLAG is 1, set also a mnemonic character for end-of-line
22323 type of CODING_SYSTEM. Return updated pointer into BUF. */
22325 static unsigned char invalid_eol_type[] = "(*invalid*)";
22327 static char *
22328 decode_mode_spec_coding (Lisp_Object coding_system, register char *buf, int eol_flag)
22330 Lisp_Object val;
22331 bool multibyte = !NILP (BVAR (current_buffer, enable_multibyte_characters));
22332 const unsigned char *eol_str;
22333 int eol_str_len;
22334 /* The EOL conversion we are using. */
22335 Lisp_Object eoltype;
22337 val = CODING_SYSTEM_SPEC (coding_system);
22338 eoltype = Qnil;
22340 if (!VECTORP (val)) /* Not yet decided. */
22342 *buf++ = multibyte ? '-' : ' ';
22343 if (eol_flag)
22344 eoltype = eol_mnemonic_undecided;
22345 /* Don't mention EOL conversion if it isn't decided. */
22347 else
22349 Lisp_Object attrs;
22350 Lisp_Object eolvalue;
22352 attrs = AREF (val, 0);
22353 eolvalue = AREF (val, 2);
22355 *buf++ = multibyte
22356 ? XFASTINT (CODING_ATTR_MNEMONIC (attrs))
22357 : ' ';
22359 if (eol_flag)
22361 /* The EOL conversion that is normal on this system. */
22363 if (NILP (eolvalue)) /* Not yet decided. */
22364 eoltype = eol_mnemonic_undecided;
22365 else if (VECTORP (eolvalue)) /* Not yet decided. */
22366 eoltype = eol_mnemonic_undecided;
22367 else /* eolvalue is Qunix, Qdos, or Qmac. */
22368 eoltype = (EQ (eolvalue, Qunix)
22369 ? eol_mnemonic_unix
22370 : (EQ (eolvalue, Qdos) == 1
22371 ? eol_mnemonic_dos : eol_mnemonic_mac));
22375 if (eol_flag)
22377 /* Mention the EOL conversion if it is not the usual one. */
22378 if (STRINGP (eoltype))
22380 eol_str = SDATA (eoltype);
22381 eol_str_len = SBYTES (eoltype);
22383 else if (CHARACTERP (eoltype))
22385 unsigned char *tmp = alloca (MAX_MULTIBYTE_LENGTH);
22386 int c = XFASTINT (eoltype);
22387 eol_str_len = CHAR_STRING (c, tmp);
22388 eol_str = tmp;
22390 else
22392 eol_str = invalid_eol_type;
22393 eol_str_len = sizeof (invalid_eol_type) - 1;
22395 memcpy (buf, eol_str, eol_str_len);
22396 buf += eol_str_len;
22399 return buf;
22402 /* Return a string for the output of a mode line %-spec for window W,
22403 generated by character C. FIELD_WIDTH > 0 means pad the string
22404 returned with spaces to that value. Return a Lisp string in
22405 *STRING if the resulting string is taken from that Lisp string.
22407 Note we operate on the current buffer for most purposes. */
22409 static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
22411 static const char *
22412 decode_mode_spec (struct window *w, register int c, int field_width,
22413 Lisp_Object *string)
22415 Lisp_Object obj;
22416 struct frame *f = XFRAME (WINDOW_FRAME (w));
22417 char *decode_mode_spec_buf = f->decode_mode_spec_buffer;
22418 /* We are going to use f->decode_mode_spec_buffer as the buffer to
22419 produce strings from numerical values, so limit preposterously
22420 large values of FIELD_WIDTH to avoid overrunning the buffer's
22421 end. The size of the buffer is enough for FRAME_MESSAGE_BUF_SIZE
22422 bytes plus the terminating null. */
22423 int width = min (field_width, FRAME_MESSAGE_BUF_SIZE (f));
22424 struct buffer *b = current_buffer;
22426 obj = Qnil;
22427 *string = Qnil;
22429 switch (c)
22431 case '*':
22432 if (!NILP (BVAR (b, read_only)))
22433 return "%";
22434 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
22435 return "*";
22436 return "-";
22438 case '+':
22439 /* This differs from %* only for a modified read-only buffer. */
22440 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
22441 return "*";
22442 if (!NILP (BVAR (b, read_only)))
22443 return "%";
22444 return "-";
22446 case '&':
22447 /* This differs from %* in ignoring read-only-ness. */
22448 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
22449 return "*";
22450 return "-";
22452 case '%':
22453 return "%";
22455 case '[':
22457 int i;
22458 char *p;
22460 if (command_loop_level > 5)
22461 return "[[[... ";
22462 p = decode_mode_spec_buf;
22463 for (i = 0; i < command_loop_level; i++)
22464 *p++ = '[';
22465 *p = 0;
22466 return decode_mode_spec_buf;
22469 case ']':
22471 int i;
22472 char *p;
22474 if (command_loop_level > 5)
22475 return " ...]]]";
22476 p = decode_mode_spec_buf;
22477 for (i = 0; i < command_loop_level; i++)
22478 *p++ = ']';
22479 *p = 0;
22480 return decode_mode_spec_buf;
22483 case '-':
22485 register int i;
22487 /* Let lots_of_dashes be a string of infinite length. */
22488 if (mode_line_target == MODE_LINE_NOPROP
22489 || mode_line_target == MODE_LINE_STRING)
22490 return "--";
22491 if (field_width <= 0
22492 || field_width > sizeof (lots_of_dashes))
22494 for (i = 0; i < FRAME_MESSAGE_BUF_SIZE (f) - 1; ++i)
22495 decode_mode_spec_buf[i] = '-';
22496 decode_mode_spec_buf[i] = '\0';
22497 return decode_mode_spec_buf;
22499 else
22500 return lots_of_dashes;
22503 case 'b':
22504 obj = BVAR (b, name);
22505 break;
22507 case 'c':
22508 /* %c and %l are ignored in `frame-title-format'.
22509 (In redisplay_internal, the frame title is drawn _before_ the
22510 windows are updated, so the stuff which depends on actual
22511 window contents (such as %l) may fail to render properly, or
22512 even crash emacs.) */
22513 if (mode_line_target == MODE_LINE_TITLE)
22514 return "";
22515 else
22517 ptrdiff_t col = current_column ();
22518 w->column_number_displayed = col;
22519 pint2str (decode_mode_spec_buf, width, col);
22520 return decode_mode_spec_buf;
22523 case 'e':
22524 #ifndef SYSTEM_MALLOC
22526 if (NILP (Vmemory_full))
22527 return "";
22528 else
22529 return "!MEM FULL! ";
22531 #else
22532 return "";
22533 #endif
22535 case 'F':
22536 /* %F displays the frame name. */
22537 if (!NILP (f->title))
22538 return SSDATA (f->title);
22539 if (f->explicit_name || ! FRAME_WINDOW_P (f))
22540 return SSDATA (f->name);
22541 return "Emacs";
22543 case 'f':
22544 obj = BVAR (b, filename);
22545 break;
22547 case 'i':
22549 ptrdiff_t size = ZV - BEGV;
22550 pint2str (decode_mode_spec_buf, width, size);
22551 return decode_mode_spec_buf;
22554 case 'I':
22556 ptrdiff_t size = ZV - BEGV;
22557 pint2hrstr (decode_mode_spec_buf, width, size);
22558 return decode_mode_spec_buf;
22561 case 'l':
22563 ptrdiff_t startpos, startpos_byte, line, linepos, linepos_byte;
22564 ptrdiff_t topline, nlines, height;
22565 ptrdiff_t junk;
22567 /* %c and %l are ignored in `frame-title-format'. */
22568 if (mode_line_target == MODE_LINE_TITLE)
22569 return "";
22571 startpos = marker_position (w->start);
22572 startpos_byte = marker_byte_position (w->start);
22573 height = WINDOW_TOTAL_LINES (w);
22575 /* If we decided that this buffer isn't suitable for line numbers,
22576 don't forget that too fast. */
22577 if (w->base_line_pos == -1)
22578 goto no_value;
22580 /* If the buffer is very big, don't waste time. */
22581 if (INTEGERP (Vline_number_display_limit)
22582 && BUF_ZV (b) - BUF_BEGV (b) > XINT (Vline_number_display_limit))
22584 w->base_line_pos = 0;
22585 w->base_line_number = 0;
22586 goto no_value;
22589 if (w->base_line_number > 0
22590 && w->base_line_pos > 0
22591 && w->base_line_pos <= startpos)
22593 line = w->base_line_number;
22594 linepos = w->base_line_pos;
22595 linepos_byte = buf_charpos_to_bytepos (b, linepos);
22597 else
22599 line = 1;
22600 linepos = BUF_BEGV (b);
22601 linepos_byte = BUF_BEGV_BYTE (b);
22604 /* Count lines from base line to window start position. */
22605 nlines = display_count_lines (linepos_byte,
22606 startpos_byte,
22607 startpos, &junk);
22609 topline = nlines + line;
22611 /* Determine a new base line, if the old one is too close
22612 or too far away, or if we did not have one.
22613 "Too close" means it's plausible a scroll-down would
22614 go back past it. */
22615 if (startpos == BUF_BEGV (b))
22617 w->base_line_number = topline;
22618 w->base_line_pos = BUF_BEGV (b);
22620 else if (nlines < height + 25 || nlines > height * 3 + 50
22621 || linepos == BUF_BEGV (b))
22623 ptrdiff_t limit = BUF_BEGV (b);
22624 ptrdiff_t limit_byte = BUF_BEGV_BYTE (b);
22625 ptrdiff_t position;
22626 ptrdiff_t distance =
22627 (height * 2 + 30) * line_number_display_limit_width;
22629 if (startpos - distance > limit)
22631 limit = startpos - distance;
22632 limit_byte = CHAR_TO_BYTE (limit);
22635 nlines = display_count_lines (startpos_byte,
22636 limit_byte,
22637 - (height * 2 + 30),
22638 &position);
22639 /* If we couldn't find the lines we wanted within
22640 line_number_display_limit_width chars per line,
22641 give up on line numbers for this window. */
22642 if (position == limit_byte && limit == startpos - distance)
22644 w->base_line_pos = -1;
22645 w->base_line_number = 0;
22646 goto no_value;
22649 w->base_line_number = topline - nlines;
22650 w->base_line_pos = BYTE_TO_CHAR (position);
22653 /* Now count lines from the start pos to point. */
22654 nlines = display_count_lines (startpos_byte,
22655 PT_BYTE, PT, &junk);
22657 /* Record that we did display the line number. */
22658 line_number_displayed = 1;
22660 /* Make the string to show. */
22661 pint2str (decode_mode_spec_buf, width, topline + nlines);
22662 return decode_mode_spec_buf;
22663 no_value:
22665 char* p = decode_mode_spec_buf;
22666 int pad = width - 2;
22667 while (pad-- > 0)
22668 *p++ = ' ';
22669 *p++ = '?';
22670 *p++ = '?';
22671 *p = '\0';
22672 return decode_mode_spec_buf;
22675 break;
22677 case 'm':
22678 obj = BVAR (b, mode_name);
22679 break;
22681 case 'n':
22682 if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
22683 return " Narrow";
22684 break;
22686 case 'p':
22688 ptrdiff_t pos = marker_position (w->start);
22689 ptrdiff_t total = BUF_ZV (b) - BUF_BEGV (b);
22691 if (w->window_end_pos <= BUF_Z (b) - BUF_ZV (b))
22693 if (pos <= BUF_BEGV (b))
22694 return "All";
22695 else
22696 return "Bottom";
22698 else if (pos <= BUF_BEGV (b))
22699 return "Top";
22700 else
22702 if (total > 1000000)
22703 /* Do it differently for a large value, to avoid overflow. */
22704 total = ((pos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
22705 else
22706 total = ((pos - BUF_BEGV (b)) * 100 + total - 1) / total;
22707 /* We can't normally display a 3-digit number,
22708 so get us a 2-digit number that is close. */
22709 if (total == 100)
22710 total = 99;
22711 sprintf (decode_mode_spec_buf, "%2"pD"d%%", total);
22712 return decode_mode_spec_buf;
22716 /* Display percentage of size above the bottom of the screen. */
22717 case 'P':
22719 ptrdiff_t toppos = marker_position (w->start);
22720 ptrdiff_t botpos = BUF_Z (b) - w->window_end_pos;
22721 ptrdiff_t total = BUF_ZV (b) - BUF_BEGV (b);
22723 if (botpos >= BUF_ZV (b))
22725 if (toppos <= BUF_BEGV (b))
22726 return "All";
22727 else
22728 return "Bottom";
22730 else
22732 if (total > 1000000)
22733 /* Do it differently for a large value, to avoid overflow. */
22734 total = ((botpos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
22735 else
22736 total = ((botpos - BUF_BEGV (b)) * 100 + total - 1) / total;
22737 /* We can't normally display a 3-digit number,
22738 so get us a 2-digit number that is close. */
22739 if (total == 100)
22740 total = 99;
22741 if (toppos <= BUF_BEGV (b))
22742 sprintf (decode_mode_spec_buf, "Top%2"pD"d%%", total);
22743 else
22744 sprintf (decode_mode_spec_buf, "%2"pD"d%%", total);
22745 return decode_mode_spec_buf;
22749 case 's':
22750 /* status of process */
22751 obj = Fget_buffer_process (Fcurrent_buffer ());
22752 if (NILP (obj))
22753 return "no process";
22754 #ifndef MSDOS
22755 obj = Fsymbol_name (Fprocess_status (obj));
22756 #endif
22757 break;
22759 case '@':
22761 ptrdiff_t count = inhibit_garbage_collection ();
22762 Lisp_Object val = call1 (intern ("file-remote-p"),
22763 BVAR (current_buffer, directory));
22764 unbind_to (count, Qnil);
22766 if (NILP (val))
22767 return "-";
22768 else
22769 return "@";
22772 case 'z':
22773 /* coding-system (not including end-of-line format) */
22774 case 'Z':
22775 /* coding-system (including end-of-line type) */
22777 int eol_flag = (c == 'Z');
22778 char *p = decode_mode_spec_buf;
22780 if (! FRAME_WINDOW_P (f))
22782 /* No need to mention EOL here--the terminal never needs
22783 to do EOL conversion. */
22784 p = decode_mode_spec_coding (CODING_ID_NAME
22785 (FRAME_KEYBOARD_CODING (f)->id),
22786 p, 0);
22787 p = decode_mode_spec_coding (CODING_ID_NAME
22788 (FRAME_TERMINAL_CODING (f)->id),
22789 p, 0);
22791 p = decode_mode_spec_coding (BVAR (b, buffer_file_coding_system),
22792 p, eol_flag);
22794 #if 0 /* This proves to be annoying; I think we can do without. -- rms. */
22795 #ifdef subprocesses
22796 obj = Fget_buffer_process (Fcurrent_buffer ());
22797 if (PROCESSP (obj))
22799 p = decode_mode_spec_coding
22800 (XPROCESS (obj)->decode_coding_system, p, eol_flag);
22801 p = decode_mode_spec_coding
22802 (XPROCESS (obj)->encode_coding_system, p, eol_flag);
22804 #endif /* subprocesses */
22805 #endif /* 0 */
22806 *p = 0;
22807 return decode_mode_spec_buf;
22811 if (STRINGP (obj))
22813 *string = obj;
22814 return SSDATA (obj);
22816 else
22817 return "";
22821 /* Count up to COUNT lines starting from START_BYTE. COUNT negative
22822 means count lines back from START_BYTE. But don't go beyond
22823 LIMIT_BYTE. Return the number of lines thus found (always
22824 nonnegative).
22826 Set *BYTE_POS_PTR to the byte position where we stopped. This is
22827 either the position COUNT lines after/before START_BYTE, if we
22828 found COUNT lines, or LIMIT_BYTE if we hit the limit before finding
22829 COUNT lines. */
22831 static ptrdiff_t
22832 display_count_lines (ptrdiff_t start_byte,
22833 ptrdiff_t limit_byte, ptrdiff_t count,
22834 ptrdiff_t *byte_pos_ptr)
22836 register unsigned char *cursor;
22837 unsigned char *base;
22839 register ptrdiff_t ceiling;
22840 register unsigned char *ceiling_addr;
22841 ptrdiff_t orig_count = count;
22843 /* If we are not in selective display mode,
22844 check only for newlines. */
22845 int selective_display = (!NILP (BVAR (current_buffer, selective_display))
22846 && !INTEGERP (BVAR (current_buffer, selective_display)));
22848 if (count > 0)
22850 while (start_byte < limit_byte)
22852 ceiling = BUFFER_CEILING_OF (start_byte);
22853 ceiling = min (limit_byte - 1, ceiling);
22854 ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
22855 base = (cursor = BYTE_POS_ADDR (start_byte));
22859 if (selective_display)
22861 while (*cursor != '\n' && *cursor != 015
22862 && ++cursor != ceiling_addr)
22863 continue;
22864 if (cursor == ceiling_addr)
22865 break;
22867 else
22869 cursor = memchr (cursor, '\n', ceiling_addr - cursor);
22870 if (! cursor)
22871 break;
22874 cursor++;
22876 if (--count == 0)
22878 start_byte += cursor - base;
22879 *byte_pos_ptr = start_byte;
22880 return orig_count;
22883 while (cursor < ceiling_addr);
22885 start_byte += ceiling_addr - base;
22888 else
22890 while (start_byte > limit_byte)
22892 ceiling = BUFFER_FLOOR_OF (start_byte - 1);
22893 ceiling = max (limit_byte, ceiling);
22894 ceiling_addr = BYTE_POS_ADDR (ceiling);
22895 base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
22896 while (1)
22898 if (selective_display)
22900 while (--cursor >= ceiling_addr
22901 && *cursor != '\n' && *cursor != 015)
22902 continue;
22903 if (cursor < ceiling_addr)
22904 break;
22906 else
22908 cursor = memrchr (ceiling_addr, '\n', cursor - ceiling_addr);
22909 if (! cursor)
22910 break;
22913 if (++count == 0)
22915 start_byte += cursor - base + 1;
22916 *byte_pos_ptr = start_byte;
22917 /* When scanning backwards, we should
22918 not count the newline posterior to which we stop. */
22919 return - orig_count - 1;
22922 start_byte += ceiling_addr - base;
22926 *byte_pos_ptr = limit_byte;
22928 if (count < 0)
22929 return - orig_count + count;
22930 return orig_count - count;
22936 /***********************************************************************
22937 Displaying strings
22938 ***********************************************************************/
22940 /* Display a NUL-terminated string, starting with index START.
22942 If STRING is non-null, display that C string. Otherwise, the Lisp
22943 string LISP_STRING is displayed. There's a case that STRING is
22944 non-null and LISP_STRING is not nil. It means STRING is a string
22945 data of LISP_STRING. In that case, we display LISP_STRING while
22946 ignoring its text properties.
22948 If FACE_STRING is not nil, FACE_STRING_POS is a position in
22949 FACE_STRING. Display STRING or LISP_STRING with the face at
22950 FACE_STRING_POS in FACE_STRING:
22952 Display the string in the environment given by IT, but use the
22953 standard display table, temporarily.
22955 FIELD_WIDTH is the minimum number of output glyphs to produce.
22956 If STRING has fewer characters than FIELD_WIDTH, pad to the right
22957 with spaces. If STRING has more characters, more than FIELD_WIDTH
22958 glyphs will be produced. FIELD_WIDTH <= 0 means don't pad.
22960 PRECISION is the maximum number of characters to output from
22961 STRING. PRECISION < 0 means don't truncate the string.
22963 This is roughly equivalent to printf format specifiers:
22965 FIELD_WIDTH PRECISION PRINTF
22966 ----------------------------------------
22967 -1 -1 %s
22968 -1 10 %.10s
22969 10 -1 %10s
22970 20 10 %20.10s
22972 MULTIBYTE zero means do not display multibyte chars, > 0 means do
22973 display them, and < 0 means obey the current buffer's value of
22974 enable_multibyte_characters.
22976 Value is the number of columns displayed. */
22978 static int
22979 display_string (const char *string, Lisp_Object lisp_string, Lisp_Object face_string,
22980 ptrdiff_t face_string_pos, ptrdiff_t start, struct it *it,
22981 int field_width, int precision, int max_x, int multibyte)
22983 int hpos_at_start = it->hpos;
22984 int saved_face_id = it->face_id;
22985 struct glyph_row *row = it->glyph_row;
22986 ptrdiff_t it_charpos;
22988 /* Initialize the iterator IT for iteration over STRING beginning
22989 with index START. */
22990 reseat_to_string (it, NILP (lisp_string) ? string : NULL, lisp_string, start,
22991 precision, field_width, multibyte);
22992 if (string && STRINGP (lisp_string))
22993 /* LISP_STRING is the one returned by decode_mode_spec. We should
22994 ignore its text properties. */
22995 it->stop_charpos = it->end_charpos;
22997 /* If displaying STRING, set up the face of the iterator from
22998 FACE_STRING, if that's given. */
22999 if (STRINGP (face_string))
23001 ptrdiff_t endptr;
23002 struct face *face;
23004 it->face_id
23005 = face_at_string_position (it->w, face_string, face_string_pos,
23006 0, &endptr, it->base_face_id, 0);
23007 face = FACE_FROM_ID (it->f, it->face_id);
23008 it->face_box_p = face->box != FACE_NO_BOX;
23011 /* Set max_x to the maximum allowed X position. Don't let it go
23012 beyond the right edge of the window. */
23013 if (max_x <= 0)
23014 max_x = it->last_visible_x;
23015 else
23016 max_x = min (max_x, it->last_visible_x);
23018 /* Skip over display elements that are not visible. because IT->w is
23019 hscrolled. */
23020 if (it->current_x < it->first_visible_x)
23021 move_it_in_display_line_to (it, 100000, it->first_visible_x,
23022 MOVE_TO_POS | MOVE_TO_X);
23024 row->ascent = it->max_ascent;
23025 row->height = it->max_ascent + it->max_descent;
23026 row->phys_ascent = it->max_phys_ascent;
23027 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
23028 row->extra_line_spacing = it->max_extra_line_spacing;
23030 if (STRINGP (it->string))
23031 it_charpos = IT_STRING_CHARPOS (*it);
23032 else
23033 it_charpos = IT_CHARPOS (*it);
23035 /* This condition is for the case that we are called with current_x
23036 past last_visible_x. */
23037 while (it->current_x < max_x)
23039 int x_before, x, n_glyphs_before, i, nglyphs;
23041 /* Get the next display element. */
23042 if (!get_next_display_element (it))
23043 break;
23045 /* Produce glyphs. */
23046 x_before = it->current_x;
23047 n_glyphs_before = row->used[TEXT_AREA];
23048 PRODUCE_GLYPHS (it);
23050 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
23051 i = 0;
23052 x = x_before;
23053 while (i < nglyphs)
23055 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
23057 if (it->line_wrap != TRUNCATE
23058 && x + glyph->pixel_width > max_x)
23060 /* End of continued line or max_x reached. */
23061 if (CHAR_GLYPH_PADDING_P (*glyph))
23063 /* A wide character is unbreakable. */
23064 if (row->reversed_p)
23065 unproduce_glyphs (it, row->used[TEXT_AREA]
23066 - n_glyphs_before);
23067 row->used[TEXT_AREA] = n_glyphs_before;
23068 it->current_x = x_before;
23070 else
23072 if (row->reversed_p)
23073 unproduce_glyphs (it, row->used[TEXT_AREA]
23074 - (n_glyphs_before + i));
23075 row->used[TEXT_AREA] = n_glyphs_before + i;
23076 it->current_x = x;
23078 break;
23080 else if (x + glyph->pixel_width >= it->first_visible_x)
23082 /* Glyph is at least partially visible. */
23083 ++it->hpos;
23084 if (x < it->first_visible_x)
23085 row->x = x - it->first_visible_x;
23087 else
23089 /* Glyph is off the left margin of the display area.
23090 Should not happen. */
23091 emacs_abort ();
23094 row->ascent = max (row->ascent, it->max_ascent);
23095 row->height = max (row->height, it->max_ascent + it->max_descent);
23096 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
23097 row->phys_height = max (row->phys_height,
23098 it->max_phys_ascent + it->max_phys_descent);
23099 row->extra_line_spacing = max (row->extra_line_spacing,
23100 it->max_extra_line_spacing);
23101 x += glyph->pixel_width;
23102 ++i;
23105 /* Stop if max_x reached. */
23106 if (i < nglyphs)
23107 break;
23109 /* Stop at line ends. */
23110 if (ITERATOR_AT_END_OF_LINE_P (it))
23112 it->continuation_lines_width = 0;
23113 break;
23116 set_iterator_to_next (it, 1);
23117 if (STRINGP (it->string))
23118 it_charpos = IT_STRING_CHARPOS (*it);
23119 else
23120 it_charpos = IT_CHARPOS (*it);
23122 /* Stop if truncating at the right edge. */
23123 if (it->line_wrap == TRUNCATE
23124 && it->current_x >= it->last_visible_x)
23126 /* Add truncation mark, but don't do it if the line is
23127 truncated at a padding space. */
23128 if (it_charpos < it->string_nchars)
23130 if (!FRAME_WINDOW_P (it->f))
23132 int ii, n;
23134 if (it->current_x > it->last_visible_x)
23136 if (!row->reversed_p)
23138 for (ii = row->used[TEXT_AREA] - 1; ii > 0; --ii)
23139 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][ii]))
23140 break;
23142 else
23144 for (ii = 0; ii < row->used[TEXT_AREA]; ii++)
23145 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][ii]))
23146 break;
23147 unproduce_glyphs (it, ii + 1);
23148 ii = row->used[TEXT_AREA] - (ii + 1);
23150 for (n = row->used[TEXT_AREA]; ii < n; ++ii)
23152 row->used[TEXT_AREA] = ii;
23153 produce_special_glyphs (it, IT_TRUNCATION);
23156 produce_special_glyphs (it, IT_TRUNCATION);
23158 row->truncated_on_right_p = 1;
23160 break;
23164 /* Maybe insert a truncation at the left. */
23165 if (it->first_visible_x
23166 && it_charpos > 0)
23168 if (!FRAME_WINDOW_P (it->f)
23169 || (row->reversed_p
23170 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
23171 : WINDOW_LEFT_FRINGE_WIDTH (it->w)) == 0)
23172 insert_left_trunc_glyphs (it);
23173 row->truncated_on_left_p = 1;
23176 it->face_id = saved_face_id;
23178 /* Value is number of columns displayed. */
23179 return it->hpos - hpos_at_start;
23184 /* This is like a combination of memq and assq. Return 1/2 if PROPVAL
23185 appears as an element of LIST or as the car of an element of LIST.
23186 If PROPVAL is a list, compare each element against LIST in that
23187 way, and return 1/2 if any element of PROPVAL is found in LIST.
23188 Otherwise return 0. This function cannot quit.
23189 The return value is 2 if the text is invisible but with an ellipsis
23190 and 1 if it's invisible and without an ellipsis. */
23193 invisible_p (register Lisp_Object propval, Lisp_Object list)
23195 register Lisp_Object tail, proptail;
23197 for (tail = list; CONSP (tail); tail = XCDR (tail))
23199 register Lisp_Object tem;
23200 tem = XCAR (tail);
23201 if (EQ (propval, tem))
23202 return 1;
23203 if (CONSP (tem) && EQ (propval, XCAR (tem)))
23204 return NILP (XCDR (tem)) ? 1 : 2;
23207 if (CONSP (propval))
23209 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
23211 Lisp_Object propelt;
23212 propelt = XCAR (proptail);
23213 for (tail = list; CONSP (tail); tail = XCDR (tail))
23215 register Lisp_Object tem;
23216 tem = XCAR (tail);
23217 if (EQ (propelt, tem))
23218 return 1;
23219 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
23220 return NILP (XCDR (tem)) ? 1 : 2;
23225 return 0;
23228 DEFUN ("invisible-p", Finvisible_p, Sinvisible_p, 1, 1, 0,
23229 doc: /* Non-nil if the property makes the text invisible.
23230 POS-OR-PROP can be a marker or number, in which case it is taken to be
23231 a position in the current buffer and the value of the `invisible' property
23232 is checked; or it can be some other value, which is then presumed to be the
23233 value of the `invisible' property of the text of interest.
23234 The non-nil value returned can be t for truly invisible text or something
23235 else if the text is replaced by an ellipsis. */)
23236 (Lisp_Object pos_or_prop)
23238 Lisp_Object prop
23239 = (NATNUMP (pos_or_prop) || MARKERP (pos_or_prop)
23240 ? Fget_char_property (pos_or_prop, Qinvisible, Qnil)
23241 : pos_or_prop);
23242 int invis = TEXT_PROP_MEANS_INVISIBLE (prop);
23243 return (invis == 0 ? Qnil
23244 : invis == 1 ? Qt
23245 : make_number (invis));
23248 /* Calculate a width or height in pixels from a specification using
23249 the following elements:
23251 SPEC ::=
23252 NUM - a (fractional) multiple of the default font width/height
23253 (NUM) - specifies exactly NUM pixels
23254 UNIT - a fixed number of pixels, see below.
23255 ELEMENT - size of a display element in pixels, see below.
23256 (NUM . SPEC) - equals NUM * SPEC
23257 (+ SPEC SPEC ...) - add pixel values
23258 (- SPEC SPEC ...) - subtract pixel values
23259 (- SPEC) - negate pixel value
23261 NUM ::=
23262 INT or FLOAT - a number constant
23263 SYMBOL - use symbol's (buffer local) variable binding.
23265 UNIT ::=
23266 in - pixels per inch *)
23267 mm - pixels per 1/1000 meter *)
23268 cm - pixels per 1/100 meter *)
23269 width - width of current font in pixels.
23270 height - height of current font in pixels.
23272 *) using the ratio(s) defined in display-pixels-per-inch.
23274 ELEMENT ::=
23276 left-fringe - left fringe width in pixels
23277 right-fringe - right fringe width in pixels
23279 left-margin - left margin width in pixels
23280 right-margin - right margin width in pixels
23282 scroll-bar - scroll-bar area width in pixels
23284 Examples:
23286 Pixels corresponding to 5 inches:
23287 (5 . in)
23289 Total width of non-text areas on left side of window (if scroll-bar is on left):
23290 '(space :width (+ left-fringe left-margin scroll-bar))
23292 Align to first text column (in header line):
23293 '(space :align-to 0)
23295 Align to middle of text area minus half the width of variable `my-image'
23296 containing a loaded image:
23297 '(space :align-to (0.5 . (- text my-image)))
23299 Width of left margin minus width of 1 character in the default font:
23300 '(space :width (- left-margin 1))
23302 Width of left margin minus width of 2 characters in the current font:
23303 '(space :width (- left-margin (2 . width)))
23305 Center 1 character over left-margin (in header line):
23306 '(space :align-to (+ left-margin (0.5 . left-margin) -0.5))
23308 Different ways to express width of left fringe plus left margin minus one pixel:
23309 '(space :width (- (+ left-fringe left-margin) (1)))
23310 '(space :width (+ left-fringe left-margin (- (1))))
23311 '(space :width (+ left-fringe left-margin (-1)))
23315 static int
23316 calc_pixel_width_or_height (double *res, struct it *it, Lisp_Object prop,
23317 struct font *font, int width_p, int *align_to)
23319 double pixels;
23321 #define OK_PIXELS(val) ((*res = (double)(val)), 1)
23322 #define OK_ALIGN_TO(val) ((*align_to = (int)(val)), 1)
23324 if (NILP (prop))
23325 return OK_PIXELS (0);
23327 eassert (FRAME_LIVE_P (it->f));
23329 if (SYMBOLP (prop))
23331 if (SCHARS (SYMBOL_NAME (prop)) == 2)
23333 char *unit = SSDATA (SYMBOL_NAME (prop));
23335 if (unit[0] == 'i' && unit[1] == 'n')
23336 pixels = 1.0;
23337 else if (unit[0] == 'm' && unit[1] == 'm')
23338 pixels = 25.4;
23339 else if (unit[0] == 'c' && unit[1] == 'm')
23340 pixels = 2.54;
23341 else
23342 pixels = 0;
23343 if (pixels > 0)
23345 double ppi = (width_p ? FRAME_RES_X (it->f)
23346 : FRAME_RES_Y (it->f));
23348 if (ppi > 0)
23349 return OK_PIXELS (ppi / pixels);
23350 return 0;
23354 #ifdef HAVE_WINDOW_SYSTEM
23355 if (EQ (prop, Qheight))
23356 return OK_PIXELS (font ? FONT_HEIGHT (font) : FRAME_LINE_HEIGHT (it->f));
23357 if (EQ (prop, Qwidth))
23358 return OK_PIXELS (font ? FONT_WIDTH (font) : FRAME_COLUMN_WIDTH (it->f));
23359 #else
23360 if (EQ (prop, Qheight) || EQ (prop, Qwidth))
23361 return OK_PIXELS (1);
23362 #endif
23364 if (EQ (prop, Qtext))
23365 return OK_PIXELS (width_p
23366 ? window_box_width (it->w, TEXT_AREA)
23367 : WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w));
23369 if (align_to && *align_to < 0)
23371 *res = 0;
23372 if (EQ (prop, Qleft))
23373 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA));
23374 if (EQ (prop, Qright))
23375 return OK_ALIGN_TO (window_box_right_offset (it->w, TEXT_AREA));
23376 if (EQ (prop, Qcenter))
23377 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA)
23378 + window_box_width (it->w, TEXT_AREA) / 2);
23379 if (EQ (prop, Qleft_fringe))
23380 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
23381 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (it->w)
23382 : window_box_right_offset (it->w, LEFT_MARGIN_AREA));
23383 if (EQ (prop, Qright_fringe))
23384 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
23385 ? window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
23386 : window_box_right_offset (it->w, TEXT_AREA));
23387 if (EQ (prop, Qleft_margin))
23388 return OK_ALIGN_TO (window_box_left_offset (it->w, LEFT_MARGIN_AREA));
23389 if (EQ (prop, Qright_margin))
23390 return OK_ALIGN_TO (window_box_left_offset (it->w, RIGHT_MARGIN_AREA));
23391 if (EQ (prop, Qscroll_bar))
23392 return OK_ALIGN_TO (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (it->w)
23394 : (window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
23395 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
23396 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
23397 : 0)));
23399 else
23401 if (EQ (prop, Qleft_fringe))
23402 return OK_PIXELS (WINDOW_LEFT_FRINGE_WIDTH (it->w));
23403 if (EQ (prop, Qright_fringe))
23404 return OK_PIXELS (WINDOW_RIGHT_FRINGE_WIDTH (it->w));
23405 if (EQ (prop, Qleft_margin))
23406 return OK_PIXELS (WINDOW_LEFT_MARGIN_WIDTH (it->w));
23407 if (EQ (prop, Qright_margin))
23408 return OK_PIXELS (WINDOW_RIGHT_MARGIN_WIDTH (it->w));
23409 if (EQ (prop, Qscroll_bar))
23410 return OK_PIXELS (WINDOW_SCROLL_BAR_AREA_WIDTH (it->w));
23413 prop = buffer_local_value_1 (prop, it->w->contents);
23414 if (EQ (prop, Qunbound))
23415 prop = Qnil;
23418 if (INTEGERP (prop) || FLOATP (prop))
23420 int base_unit = (width_p
23421 ? FRAME_COLUMN_WIDTH (it->f)
23422 : FRAME_LINE_HEIGHT (it->f));
23423 return OK_PIXELS (XFLOATINT (prop) * base_unit);
23426 if (CONSP (prop))
23428 Lisp_Object car = XCAR (prop);
23429 Lisp_Object cdr = XCDR (prop);
23431 if (SYMBOLP (car))
23433 #ifdef HAVE_WINDOW_SYSTEM
23434 if (FRAME_WINDOW_P (it->f)
23435 && valid_image_p (prop))
23437 ptrdiff_t id = lookup_image (it->f, prop);
23438 struct image *img = IMAGE_FROM_ID (it->f, id);
23440 return OK_PIXELS (width_p ? img->width : img->height);
23442 #endif
23443 if (EQ (car, Qplus) || EQ (car, Qminus))
23445 int first = 1;
23446 double px;
23448 pixels = 0;
23449 while (CONSP (cdr))
23451 if (!calc_pixel_width_or_height (&px, it, XCAR (cdr),
23452 font, width_p, align_to))
23453 return 0;
23454 if (first)
23455 pixels = (EQ (car, Qplus) ? px : -px), first = 0;
23456 else
23457 pixels += px;
23458 cdr = XCDR (cdr);
23460 if (EQ (car, Qminus))
23461 pixels = -pixels;
23462 return OK_PIXELS (pixels);
23465 car = buffer_local_value_1 (car, it->w->contents);
23466 if (EQ (car, Qunbound))
23467 car = Qnil;
23470 if (INTEGERP (car) || FLOATP (car))
23472 double fact;
23473 pixels = XFLOATINT (car);
23474 if (NILP (cdr))
23475 return OK_PIXELS (pixels);
23476 if (calc_pixel_width_or_height (&fact, it, cdr,
23477 font, width_p, align_to))
23478 return OK_PIXELS (pixels * fact);
23479 return 0;
23482 return 0;
23485 return 0;
23489 /***********************************************************************
23490 Glyph Display
23491 ***********************************************************************/
23493 #ifdef HAVE_WINDOW_SYSTEM
23495 #ifdef GLYPH_DEBUG
23497 void
23498 dump_glyph_string (struct glyph_string *s)
23500 fprintf (stderr, "glyph string\n");
23501 fprintf (stderr, " x, y, w, h = %d, %d, %d, %d\n",
23502 s->x, s->y, s->width, s->height);
23503 fprintf (stderr, " ybase = %d\n", s->ybase);
23504 fprintf (stderr, " hl = %d\n", s->hl);
23505 fprintf (stderr, " left overhang = %d, right = %d\n",
23506 s->left_overhang, s->right_overhang);
23507 fprintf (stderr, " nchars = %d\n", s->nchars);
23508 fprintf (stderr, " extends to end of line = %d\n",
23509 s->extends_to_end_of_line_p);
23510 fprintf (stderr, " font height = %d\n", FONT_HEIGHT (s->font));
23511 fprintf (stderr, " bg width = %d\n", s->background_width);
23514 #endif /* GLYPH_DEBUG */
23516 /* Initialize glyph string S. CHAR2B is a suitably allocated vector
23517 of XChar2b structures for S; it can't be allocated in
23518 init_glyph_string because it must be allocated via `alloca'. W
23519 is the window on which S is drawn. ROW and AREA are the glyph row
23520 and area within the row from which S is constructed. START is the
23521 index of the first glyph structure covered by S. HL is a
23522 face-override for drawing S. */
23524 #ifdef HAVE_NTGUI
23525 #define OPTIONAL_HDC(hdc) HDC hdc,
23526 #define DECLARE_HDC(hdc) HDC hdc;
23527 #define ALLOCATE_HDC(hdc, f) hdc = get_frame_dc ((f))
23528 #define RELEASE_HDC(hdc, f) release_frame_dc ((f), (hdc))
23529 #endif
23531 #ifndef OPTIONAL_HDC
23532 #define OPTIONAL_HDC(hdc)
23533 #define DECLARE_HDC(hdc)
23534 #define ALLOCATE_HDC(hdc, f)
23535 #define RELEASE_HDC(hdc, f)
23536 #endif
23538 static void
23539 init_glyph_string (struct glyph_string *s,
23540 OPTIONAL_HDC (hdc)
23541 XChar2b *char2b, struct window *w, struct glyph_row *row,
23542 enum glyph_row_area area, int start, enum draw_glyphs_face hl)
23544 memset (s, 0, sizeof *s);
23545 s->w = w;
23546 s->f = XFRAME (w->frame);
23547 #ifdef HAVE_NTGUI
23548 s->hdc = hdc;
23549 #endif
23550 s->display = FRAME_X_DISPLAY (s->f);
23551 s->window = FRAME_X_WINDOW (s->f);
23552 s->char2b = char2b;
23553 s->hl = hl;
23554 s->row = row;
23555 s->area = area;
23556 s->first_glyph = row->glyphs[area] + start;
23557 s->height = row->height;
23558 s->y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
23559 s->ybase = s->y + row->ascent;
23563 /* Append the list of glyph strings with head H and tail T to the list
23564 with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the result. */
23566 static void
23567 append_glyph_string_lists (struct glyph_string **head, struct glyph_string **tail,
23568 struct glyph_string *h, struct glyph_string *t)
23570 if (h)
23572 if (*head)
23573 (*tail)->next = h;
23574 else
23575 *head = h;
23576 h->prev = *tail;
23577 *tail = t;
23582 /* Prepend the list of glyph strings with head H and tail T to the
23583 list with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the
23584 result. */
23586 static void
23587 prepend_glyph_string_lists (struct glyph_string **head, struct glyph_string **tail,
23588 struct glyph_string *h, struct glyph_string *t)
23590 if (h)
23592 if (*head)
23593 (*head)->prev = t;
23594 else
23595 *tail = t;
23596 t->next = *head;
23597 *head = h;
23602 /* Append glyph string S to the list with head *HEAD and tail *TAIL.
23603 Set *HEAD and *TAIL to the resulting list. */
23605 static void
23606 append_glyph_string (struct glyph_string **head, struct glyph_string **tail,
23607 struct glyph_string *s)
23609 s->next = s->prev = NULL;
23610 append_glyph_string_lists (head, tail, s, s);
23614 /* Get face and two-byte form of character C in face FACE_ID on frame F.
23615 The encoding of C is returned in *CHAR2B. DISPLAY_P non-zero means
23616 make sure that X resources for the face returned are allocated.
23617 Value is a pointer to a realized face that is ready for display if
23618 DISPLAY_P is non-zero. */
23620 static struct face *
23621 get_char_face_and_encoding (struct frame *f, int c, int face_id,
23622 XChar2b *char2b, int display_p)
23624 struct face *face = FACE_FROM_ID (f, face_id);
23625 unsigned code = 0;
23627 if (face->font)
23629 code = face->font->driver->encode_char (face->font, c);
23631 if (code == FONT_INVALID_CODE)
23632 code = 0;
23634 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
23636 /* Make sure X resources of the face are allocated. */
23637 #ifdef HAVE_X_WINDOWS
23638 if (display_p)
23639 #endif
23641 eassert (face != NULL);
23642 PREPARE_FACE_FOR_DISPLAY (f, face);
23645 return face;
23649 /* Get face and two-byte form of character glyph GLYPH on frame F.
23650 The encoding of GLYPH->u.ch is returned in *CHAR2B. Value is
23651 a pointer to a realized face that is ready for display. */
23653 static struct face *
23654 get_glyph_face_and_encoding (struct frame *f, struct glyph *glyph,
23655 XChar2b *char2b, int *two_byte_p)
23657 struct face *face;
23658 unsigned code = 0;
23660 eassert (glyph->type == CHAR_GLYPH);
23661 face = FACE_FROM_ID (f, glyph->face_id);
23663 /* Make sure X resources of the face are allocated. */
23664 eassert (face != NULL);
23665 PREPARE_FACE_FOR_DISPLAY (f, face);
23667 if (two_byte_p)
23668 *two_byte_p = 0;
23670 if (face->font)
23672 if (CHAR_BYTE8_P (glyph->u.ch))
23673 code = CHAR_TO_BYTE8 (glyph->u.ch);
23674 else
23675 code = face->font->driver->encode_char (face->font, glyph->u.ch);
23677 if (code == FONT_INVALID_CODE)
23678 code = 0;
23681 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
23682 return face;
23686 /* Get glyph code of character C in FONT in the two-byte form CHAR2B.
23687 Return 1 if FONT has a glyph for C, otherwise return 0. */
23689 static int
23690 get_char_glyph_code (int c, struct font *font, XChar2b *char2b)
23692 unsigned code;
23694 if (CHAR_BYTE8_P (c))
23695 code = CHAR_TO_BYTE8 (c);
23696 else
23697 code = font->driver->encode_char (font, c);
23699 if (code == FONT_INVALID_CODE)
23700 return 0;
23701 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
23702 return 1;
23706 /* Fill glyph string S with composition components specified by S->cmp.
23708 BASE_FACE is the base face of the composition.
23709 S->cmp_from is the index of the first component for S.
23711 OVERLAPS non-zero means S should draw the foreground only, and use
23712 its physical height for clipping. See also draw_glyphs.
23714 Value is the index of a component not in S. */
23716 static int
23717 fill_composite_glyph_string (struct glyph_string *s, struct face *base_face,
23718 int overlaps)
23720 int i;
23721 /* For all glyphs of this composition, starting at the offset
23722 S->cmp_from, until we reach the end of the definition or encounter a
23723 glyph that requires the different face, add it to S. */
23724 struct face *face;
23726 eassert (s);
23728 s->for_overlaps = overlaps;
23729 s->face = NULL;
23730 s->font = NULL;
23731 for (i = s->cmp_from; i < s->cmp->glyph_len; i++)
23733 int c = COMPOSITION_GLYPH (s->cmp, i);
23735 /* TAB in a composition means display glyphs with padding space
23736 on the left or right. */
23737 if (c != '\t')
23739 int face_id = FACE_FOR_CHAR (s->f, base_face->ascii_face, c,
23740 -1, Qnil);
23742 face = get_char_face_and_encoding (s->f, c, face_id,
23743 s->char2b + i, 1);
23744 if (face)
23746 if (! s->face)
23748 s->face = face;
23749 s->font = s->face->font;
23751 else if (s->face != face)
23752 break;
23755 ++s->nchars;
23757 s->cmp_to = i;
23759 if (s->face == NULL)
23761 s->face = base_face->ascii_face;
23762 s->font = s->face->font;
23765 /* All glyph strings for the same composition has the same width,
23766 i.e. the width set for the first component of the composition. */
23767 s->width = s->first_glyph->pixel_width;
23769 /* If the specified font could not be loaded, use the frame's
23770 default font, but record the fact that we couldn't load it in
23771 the glyph string so that we can draw rectangles for the
23772 characters of the glyph string. */
23773 if (s->font == NULL)
23775 s->font_not_found_p = 1;
23776 s->font = FRAME_FONT (s->f);
23779 /* Adjust base line for subscript/superscript text. */
23780 s->ybase += s->first_glyph->voffset;
23782 /* This glyph string must always be drawn with 16-bit functions. */
23783 s->two_byte_p = 1;
23785 return s->cmp_to;
23788 static int
23789 fill_gstring_glyph_string (struct glyph_string *s, int face_id,
23790 int start, int end, int overlaps)
23792 struct glyph *glyph, *last;
23793 Lisp_Object lgstring;
23794 int i;
23796 s->for_overlaps = overlaps;
23797 glyph = s->row->glyphs[s->area] + start;
23798 last = s->row->glyphs[s->area] + end;
23799 s->cmp_id = glyph->u.cmp.id;
23800 s->cmp_from = glyph->slice.cmp.from;
23801 s->cmp_to = glyph->slice.cmp.to + 1;
23802 s->face = FACE_FROM_ID (s->f, face_id);
23803 lgstring = composition_gstring_from_id (s->cmp_id);
23804 s->font = XFONT_OBJECT (LGSTRING_FONT (lgstring));
23805 glyph++;
23806 while (glyph < last
23807 && glyph->u.cmp.automatic
23808 && glyph->u.cmp.id == s->cmp_id
23809 && s->cmp_to == glyph->slice.cmp.from)
23810 s->cmp_to = (glyph++)->slice.cmp.to + 1;
23812 for (i = s->cmp_from; i < s->cmp_to; i++)
23814 Lisp_Object lglyph = LGSTRING_GLYPH (lgstring, i);
23815 unsigned code = LGLYPH_CODE (lglyph);
23817 STORE_XCHAR2B ((s->char2b + i), code >> 8, code & 0xFF);
23819 s->width = composition_gstring_width (lgstring, s->cmp_from, s->cmp_to, NULL);
23820 return glyph - s->row->glyphs[s->area];
23824 /* Fill glyph string S from a sequence glyphs for glyphless characters.
23825 See the comment of fill_glyph_string for arguments.
23826 Value is the index of the first glyph not in S. */
23829 static int
23830 fill_glyphless_glyph_string (struct glyph_string *s, int face_id,
23831 int start, int end, int overlaps)
23833 struct glyph *glyph, *last;
23834 int voffset;
23836 eassert (s->first_glyph->type == GLYPHLESS_GLYPH);
23837 s->for_overlaps = overlaps;
23838 glyph = s->row->glyphs[s->area] + start;
23839 last = s->row->glyphs[s->area] + end;
23840 voffset = glyph->voffset;
23841 s->face = FACE_FROM_ID (s->f, face_id);
23842 s->font = s->face->font ? s->face->font : FRAME_FONT (s->f);
23843 s->nchars = 1;
23844 s->width = glyph->pixel_width;
23845 glyph++;
23846 while (glyph < last
23847 && glyph->type == GLYPHLESS_GLYPH
23848 && glyph->voffset == voffset
23849 && glyph->face_id == face_id)
23851 s->nchars++;
23852 s->width += glyph->pixel_width;
23853 glyph++;
23855 s->ybase += voffset;
23856 return glyph - s->row->glyphs[s->area];
23860 /* Fill glyph string S from a sequence of character glyphs.
23862 FACE_ID is the face id of the string. START is the index of the
23863 first glyph to consider, END is the index of the last + 1.
23864 OVERLAPS non-zero means S should draw the foreground only, and use
23865 its physical height for clipping. See also draw_glyphs.
23867 Value is the index of the first glyph not in S. */
23869 static int
23870 fill_glyph_string (struct glyph_string *s, int face_id,
23871 int start, int end, int overlaps)
23873 struct glyph *glyph, *last;
23874 int voffset;
23875 int glyph_not_available_p;
23877 eassert (s->f == XFRAME (s->w->frame));
23878 eassert (s->nchars == 0);
23879 eassert (start >= 0 && end > start);
23881 s->for_overlaps = overlaps;
23882 glyph = s->row->glyphs[s->area] + start;
23883 last = s->row->glyphs[s->area] + end;
23884 voffset = glyph->voffset;
23885 s->padding_p = glyph->padding_p;
23886 glyph_not_available_p = glyph->glyph_not_available_p;
23888 while (glyph < last
23889 && glyph->type == CHAR_GLYPH
23890 && glyph->voffset == voffset
23891 /* Same face id implies same font, nowadays. */
23892 && glyph->face_id == face_id
23893 && glyph->glyph_not_available_p == glyph_not_available_p)
23895 int two_byte_p;
23897 s->face = get_glyph_face_and_encoding (s->f, glyph,
23898 s->char2b + s->nchars,
23899 &two_byte_p);
23900 s->two_byte_p = two_byte_p;
23901 ++s->nchars;
23902 eassert (s->nchars <= end - start);
23903 s->width += glyph->pixel_width;
23904 if (glyph++->padding_p != s->padding_p)
23905 break;
23908 s->font = s->face->font;
23910 /* If the specified font could not be loaded, use the frame's font,
23911 but record the fact that we couldn't load it in
23912 S->font_not_found_p so that we can draw rectangles for the
23913 characters of the glyph string. */
23914 if (s->font == NULL || glyph_not_available_p)
23916 s->font_not_found_p = 1;
23917 s->font = FRAME_FONT (s->f);
23920 /* Adjust base line for subscript/superscript text. */
23921 s->ybase += voffset;
23923 eassert (s->face && s->face->gc);
23924 return glyph - s->row->glyphs[s->area];
23928 /* Fill glyph string S from image glyph S->first_glyph. */
23930 static void
23931 fill_image_glyph_string (struct glyph_string *s)
23933 eassert (s->first_glyph->type == IMAGE_GLYPH);
23934 s->img = IMAGE_FROM_ID (s->f, s->first_glyph->u.img_id);
23935 eassert (s->img);
23936 s->slice = s->first_glyph->slice.img;
23937 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
23938 s->font = s->face->font;
23939 s->width = s->first_glyph->pixel_width;
23941 /* Adjust base line for subscript/superscript text. */
23942 s->ybase += s->first_glyph->voffset;
23946 /* Fill glyph string S from a sequence of stretch glyphs.
23948 START is the index of the first glyph to consider,
23949 END is the index of the last + 1.
23951 Value is the index of the first glyph not in S. */
23953 static int
23954 fill_stretch_glyph_string (struct glyph_string *s, int start, int end)
23956 struct glyph *glyph, *last;
23957 int voffset, face_id;
23959 eassert (s->first_glyph->type == STRETCH_GLYPH);
23961 glyph = s->row->glyphs[s->area] + start;
23962 last = s->row->glyphs[s->area] + end;
23963 face_id = glyph->face_id;
23964 s->face = FACE_FROM_ID (s->f, face_id);
23965 s->font = s->face->font;
23966 s->width = glyph->pixel_width;
23967 s->nchars = 1;
23968 voffset = glyph->voffset;
23970 for (++glyph;
23971 (glyph < last
23972 && glyph->type == STRETCH_GLYPH
23973 && glyph->voffset == voffset
23974 && glyph->face_id == face_id);
23975 ++glyph)
23976 s->width += glyph->pixel_width;
23978 /* Adjust base line for subscript/superscript text. */
23979 s->ybase += voffset;
23981 /* The case that face->gc == 0 is handled when drawing the glyph
23982 string by calling PREPARE_FACE_FOR_DISPLAY. */
23983 eassert (s->face);
23984 return glyph - s->row->glyphs[s->area];
23987 static struct font_metrics *
23988 get_per_char_metric (struct font *font, XChar2b *char2b)
23990 static struct font_metrics metrics;
23991 unsigned code;
23993 if (! font)
23994 return NULL;
23995 code = (XCHAR2B_BYTE1 (char2b) << 8) | XCHAR2B_BYTE2 (char2b);
23996 if (code == FONT_INVALID_CODE)
23997 return NULL;
23998 font->driver->text_extents (font, &code, 1, &metrics);
23999 return &metrics;
24002 /* EXPORT for RIF:
24003 Set *LEFT and *RIGHT to the left and right overhang of GLYPH on
24004 frame F. Overhangs of glyphs other than type CHAR_GLYPH are
24005 assumed to be zero. */
24007 void
24008 x_get_glyph_overhangs (struct glyph *glyph, struct frame *f, int *left, int *right)
24010 *left = *right = 0;
24012 if (glyph->type == CHAR_GLYPH)
24014 struct face *face;
24015 XChar2b char2b;
24016 struct font_metrics *pcm;
24018 face = get_glyph_face_and_encoding (f, glyph, &char2b, NULL);
24019 if (face->font && (pcm = get_per_char_metric (face->font, &char2b)))
24021 if (pcm->rbearing > pcm->width)
24022 *right = pcm->rbearing - pcm->width;
24023 if (pcm->lbearing < 0)
24024 *left = -pcm->lbearing;
24027 else if (glyph->type == COMPOSITE_GLYPH)
24029 if (! glyph->u.cmp.automatic)
24031 struct composition *cmp = composition_table[glyph->u.cmp.id];
24033 if (cmp->rbearing > cmp->pixel_width)
24034 *right = cmp->rbearing - cmp->pixel_width;
24035 if (cmp->lbearing < 0)
24036 *left = - cmp->lbearing;
24038 else
24040 Lisp_Object gstring = composition_gstring_from_id (glyph->u.cmp.id);
24041 struct font_metrics metrics;
24043 composition_gstring_width (gstring, glyph->slice.cmp.from,
24044 glyph->slice.cmp.to + 1, &metrics);
24045 if (metrics.rbearing > metrics.width)
24046 *right = metrics.rbearing - metrics.width;
24047 if (metrics.lbearing < 0)
24048 *left = - metrics.lbearing;
24054 /* Return the index of the first glyph preceding glyph string S that
24055 is overwritten by S because of S's left overhang. Value is -1
24056 if no glyphs are overwritten. */
24058 static int
24059 left_overwritten (struct glyph_string *s)
24061 int k;
24063 if (s->left_overhang)
24065 int x = 0, i;
24066 struct glyph *glyphs = s->row->glyphs[s->area];
24067 int first = s->first_glyph - glyphs;
24069 for (i = first - 1; i >= 0 && x > -s->left_overhang; --i)
24070 x -= glyphs[i].pixel_width;
24072 k = i + 1;
24074 else
24075 k = -1;
24077 return k;
24081 /* Return the index of the first glyph preceding glyph string S that
24082 is overwriting S because of its right overhang. Value is -1 if no
24083 glyph in front of S overwrites S. */
24085 static int
24086 left_overwriting (struct glyph_string *s)
24088 int i, k, x;
24089 struct glyph *glyphs = s->row->glyphs[s->area];
24090 int first = s->first_glyph - glyphs;
24092 k = -1;
24093 x = 0;
24094 for (i = first - 1; i >= 0; --i)
24096 int left, right;
24097 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
24098 if (x + right > 0)
24099 k = i;
24100 x -= glyphs[i].pixel_width;
24103 return k;
24107 /* Return the index of the last glyph following glyph string S that is
24108 overwritten by S because of S's right overhang. Value is -1 if
24109 no such glyph is found. */
24111 static int
24112 right_overwritten (struct glyph_string *s)
24114 int k = -1;
24116 if (s->right_overhang)
24118 int x = 0, i;
24119 struct glyph *glyphs = s->row->glyphs[s->area];
24120 int first = (s->first_glyph - glyphs
24121 + (s->first_glyph->type == COMPOSITE_GLYPH ? 1 : s->nchars));
24122 int end = s->row->used[s->area];
24124 for (i = first; i < end && s->right_overhang > x; ++i)
24125 x += glyphs[i].pixel_width;
24127 k = i;
24130 return k;
24134 /* Return the index of the last glyph following glyph string S that
24135 overwrites S because of its left overhang. Value is negative
24136 if no such glyph is found. */
24138 static int
24139 right_overwriting (struct glyph_string *s)
24141 int i, k, x;
24142 int end = s->row->used[s->area];
24143 struct glyph *glyphs = s->row->glyphs[s->area];
24144 int first = (s->first_glyph - glyphs
24145 + (s->first_glyph->type == COMPOSITE_GLYPH ? 1 : s->nchars));
24147 k = -1;
24148 x = 0;
24149 for (i = first; i < end; ++i)
24151 int left, right;
24152 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
24153 if (x - left < 0)
24154 k = i;
24155 x += glyphs[i].pixel_width;
24158 return k;
24162 /* Set background width of glyph string S. START is the index of the
24163 first glyph following S. LAST_X is the right-most x-position + 1
24164 in the drawing area. */
24166 static void
24167 set_glyph_string_background_width (struct glyph_string *s, int start, int last_x)
24169 /* If the face of this glyph string has to be drawn to the end of
24170 the drawing area, set S->extends_to_end_of_line_p. */
24172 if (start == s->row->used[s->area]
24173 && ((s->row->fill_line_p
24174 && (s->hl == DRAW_NORMAL_TEXT
24175 || s->hl == DRAW_IMAGE_RAISED
24176 || s->hl == DRAW_IMAGE_SUNKEN))
24177 || s->hl == DRAW_MOUSE_FACE))
24178 s->extends_to_end_of_line_p = 1;
24180 /* If S extends its face to the end of the line, set its
24181 background_width to the distance to the right edge of the drawing
24182 area. */
24183 if (s->extends_to_end_of_line_p)
24184 s->background_width = last_x - s->x + 1;
24185 else
24186 s->background_width = s->width;
24190 /* Compute overhangs and x-positions for glyph string S and its
24191 predecessors, or successors. X is the starting x-position for S.
24192 BACKWARD_P non-zero means process predecessors. */
24194 static void
24195 compute_overhangs_and_x (struct glyph_string *s, int x, int backward_p)
24197 if (backward_p)
24199 while (s)
24201 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
24202 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
24203 x -= s->width;
24204 s->x = x;
24205 s = s->prev;
24208 else
24210 while (s)
24212 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
24213 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
24214 s->x = x;
24215 x += s->width;
24216 s = s->next;
24223 /* The following macros are only called from draw_glyphs below.
24224 They reference the following parameters of that function directly:
24225 `w', `row', `area', and `overlap_p'
24226 as well as the following local variables:
24227 `s', `f', and `hdc' (in W32) */
24229 #ifdef HAVE_NTGUI
24230 /* On W32, silently add local `hdc' variable to argument list of
24231 init_glyph_string. */
24232 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
24233 init_glyph_string (s, hdc, char2b, w, row, area, start, hl)
24234 #else
24235 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
24236 init_glyph_string (s, char2b, w, row, area, start, hl)
24237 #endif
24239 /* Add a glyph string for a stretch glyph to the list of strings
24240 between HEAD and TAIL. START is the index of the stretch glyph in
24241 row area AREA of glyph row ROW. END is the index of the last glyph
24242 in that glyph row area. X is the current output position assigned
24243 to the new glyph string constructed. HL overrides that face of the
24244 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
24245 is the right-most x-position of the drawing area. */
24247 /* SunOS 4 bundled cc, barfed on continuations in the arg lists here
24248 and below -- keep them on one line. */
24249 #define BUILD_STRETCH_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
24250 do \
24252 s = alloca (sizeof *s); \
24253 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
24254 START = fill_stretch_glyph_string (s, START, END); \
24255 append_glyph_string (&HEAD, &TAIL, s); \
24256 s->x = (X); \
24258 while (0)
24261 /* Add a glyph string for an image glyph to the list of strings
24262 between HEAD and TAIL. START is the index of the image glyph in
24263 row area AREA of glyph row ROW. END is the index of the last glyph
24264 in that glyph row area. X is the current output position assigned
24265 to the new glyph string constructed. HL overrides that face of the
24266 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
24267 is the right-most x-position of the drawing area. */
24269 #define BUILD_IMAGE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
24270 do \
24272 s = alloca (sizeof *s); \
24273 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
24274 fill_image_glyph_string (s); \
24275 append_glyph_string (&HEAD, &TAIL, s); \
24276 ++START; \
24277 s->x = (X); \
24279 while (0)
24282 /* Add a glyph string for a sequence of character glyphs to the list
24283 of strings between HEAD and TAIL. START is the index of the first
24284 glyph in row area AREA of glyph row ROW that is part of the new
24285 glyph string. END is the index of the last glyph in that glyph row
24286 area. X is the current output position assigned to the new glyph
24287 string constructed. HL overrides that face of the glyph; e.g. it
24288 is DRAW_CURSOR if a cursor has to be drawn. LAST_X is the
24289 right-most x-position of the drawing area. */
24291 #define BUILD_CHAR_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
24292 do \
24294 int face_id; \
24295 XChar2b *char2b; \
24297 face_id = (row)->glyphs[area][START].face_id; \
24299 s = alloca (sizeof *s); \
24300 char2b = alloca ((END - START) * sizeof *char2b); \
24301 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
24302 append_glyph_string (&HEAD, &TAIL, s); \
24303 s->x = (X); \
24304 START = fill_glyph_string (s, face_id, START, END, overlaps); \
24306 while (0)
24309 /* Add a glyph string for a composite sequence to the list of strings
24310 between HEAD and TAIL. START is the index of the first glyph in
24311 row area AREA of glyph row ROW that is part of the new glyph
24312 string. END is the index of the last glyph in that glyph row area.
24313 X is the current output position assigned to the new glyph string
24314 constructed. HL overrides that face of the glyph; e.g. it is
24315 DRAW_CURSOR if a cursor has to be drawn. LAST_X is the right-most
24316 x-position of the drawing area. */
24318 #define BUILD_COMPOSITE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
24319 do { \
24320 int face_id = (row)->glyphs[area][START].face_id; \
24321 struct face *base_face = FACE_FROM_ID (f, face_id); \
24322 ptrdiff_t cmp_id = (row)->glyphs[area][START].u.cmp.id; \
24323 struct composition *cmp = composition_table[cmp_id]; \
24324 XChar2b *char2b; \
24325 struct glyph_string *first_s = NULL; \
24326 int n; \
24328 char2b = alloca (cmp->glyph_len * sizeof *char2b); \
24330 /* Make glyph_strings for each glyph sequence that is drawable by \
24331 the same face, and append them to HEAD/TAIL. */ \
24332 for (n = 0; n < cmp->glyph_len;) \
24334 s = alloca (sizeof *s); \
24335 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
24336 append_glyph_string (&(HEAD), &(TAIL), s); \
24337 s->cmp = cmp; \
24338 s->cmp_from = n; \
24339 s->x = (X); \
24340 if (n == 0) \
24341 first_s = s; \
24342 n = fill_composite_glyph_string (s, base_face, overlaps); \
24345 ++START; \
24346 s = first_s; \
24347 } while (0)
24350 /* Add a glyph string for a glyph-string sequence to the list of strings
24351 between HEAD and TAIL. */
24353 #define BUILD_GSTRING_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
24354 do { \
24355 int face_id; \
24356 XChar2b *char2b; \
24357 Lisp_Object gstring; \
24359 face_id = (row)->glyphs[area][START].face_id; \
24360 gstring = (composition_gstring_from_id \
24361 ((row)->glyphs[area][START].u.cmp.id)); \
24362 s = alloca (sizeof *s); \
24363 char2b = alloca (LGSTRING_GLYPH_LEN (gstring) * sizeof *char2b); \
24364 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
24365 append_glyph_string (&(HEAD), &(TAIL), s); \
24366 s->x = (X); \
24367 START = fill_gstring_glyph_string (s, face_id, START, END, overlaps); \
24368 } while (0)
24371 /* Add a glyph string for a sequence of glyphless character's glyphs
24372 to the list of strings between HEAD and TAIL. The meanings of
24373 arguments are the same as those of BUILD_CHAR_GLYPH_STRINGS. */
24375 #define BUILD_GLYPHLESS_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
24376 do \
24378 int face_id; \
24380 face_id = (row)->glyphs[area][START].face_id; \
24382 s = alloca (sizeof *s); \
24383 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
24384 append_glyph_string (&HEAD, &TAIL, s); \
24385 s->x = (X); \
24386 START = fill_glyphless_glyph_string (s, face_id, START, END, \
24387 overlaps); \
24389 while (0)
24392 /* Build a list of glyph strings between HEAD and TAIL for the glyphs
24393 of AREA of glyph row ROW on window W between indices START and END.
24394 HL overrides the face for drawing glyph strings, e.g. it is
24395 DRAW_CURSOR to draw a cursor. X and LAST_X are start and end
24396 x-positions of the drawing area.
24398 This is an ugly monster macro construct because we must use alloca
24399 to allocate glyph strings (because draw_glyphs can be called
24400 asynchronously). */
24402 #define BUILD_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
24403 do \
24405 HEAD = TAIL = NULL; \
24406 while (START < END) \
24408 struct glyph *first_glyph = (row)->glyphs[area] + START; \
24409 switch (first_glyph->type) \
24411 case CHAR_GLYPH: \
24412 BUILD_CHAR_GLYPH_STRINGS (START, END, HEAD, TAIL, \
24413 HL, X, LAST_X); \
24414 break; \
24416 case COMPOSITE_GLYPH: \
24417 if (first_glyph->u.cmp.automatic) \
24418 BUILD_GSTRING_GLYPH_STRING (START, END, HEAD, TAIL, \
24419 HL, X, LAST_X); \
24420 else \
24421 BUILD_COMPOSITE_GLYPH_STRING (START, END, HEAD, TAIL, \
24422 HL, X, LAST_X); \
24423 break; \
24425 case STRETCH_GLYPH: \
24426 BUILD_STRETCH_GLYPH_STRING (START, END, HEAD, TAIL, \
24427 HL, X, LAST_X); \
24428 break; \
24430 case IMAGE_GLYPH: \
24431 BUILD_IMAGE_GLYPH_STRING (START, END, HEAD, TAIL, \
24432 HL, X, LAST_X); \
24433 break; \
24435 case GLYPHLESS_GLYPH: \
24436 BUILD_GLYPHLESS_GLYPH_STRING (START, END, HEAD, TAIL, \
24437 HL, X, LAST_X); \
24438 break; \
24440 default: \
24441 emacs_abort (); \
24444 if (s) \
24446 set_glyph_string_background_width (s, START, LAST_X); \
24447 (X) += s->width; \
24450 } while (0)
24453 /* Draw glyphs between START and END in AREA of ROW on window W,
24454 starting at x-position X. X is relative to AREA in W. HL is a
24455 face-override with the following meaning:
24457 DRAW_NORMAL_TEXT draw normally
24458 DRAW_CURSOR draw in cursor face
24459 DRAW_MOUSE_FACE draw in mouse face.
24460 DRAW_INVERSE_VIDEO draw in mode line face
24461 DRAW_IMAGE_SUNKEN draw an image with a sunken relief around it
24462 DRAW_IMAGE_RAISED draw an image with a raised relief around it
24464 If OVERLAPS is non-zero, draw only the foreground of characters and
24465 clip to the physical height of ROW. Non-zero value also defines
24466 the overlapping part to be drawn:
24468 OVERLAPS_PRED overlap with preceding rows
24469 OVERLAPS_SUCC overlap with succeeding rows
24470 OVERLAPS_BOTH overlap with both preceding/succeeding rows
24471 OVERLAPS_ERASED_CURSOR overlap with erased cursor area
24473 Value is the x-position reached, relative to AREA of W. */
24475 static int
24476 draw_glyphs (struct window *w, int x, struct glyph_row *row,
24477 enum glyph_row_area area, ptrdiff_t start, ptrdiff_t end,
24478 enum draw_glyphs_face hl, int overlaps)
24480 struct glyph_string *head, *tail;
24481 struct glyph_string *s;
24482 struct glyph_string *clip_head = NULL, *clip_tail = NULL;
24483 int i, j, x_reached, last_x, area_left = 0;
24484 struct frame *f = XFRAME (WINDOW_FRAME (w));
24485 DECLARE_HDC (hdc);
24487 ALLOCATE_HDC (hdc, f);
24489 /* Let's rather be paranoid than getting a SEGV. */
24490 end = min (end, row->used[area]);
24491 start = clip_to_bounds (0, start, end);
24493 /* Translate X to frame coordinates. Set last_x to the right
24494 end of the drawing area. */
24495 if (row->full_width_p)
24497 /* X is relative to the left edge of W, without scroll bars
24498 or fringes. */
24499 area_left = WINDOW_LEFT_EDGE_X (w);
24500 last_x = (WINDOW_LEFT_EDGE_X (w) + WINDOW_PIXEL_WIDTH (w)
24501 - (row->mode_line_p ? WINDOW_RIGHT_DIVIDER_WIDTH (w) : 0));
24503 else
24505 area_left = window_box_left (w, area);
24506 last_x = area_left + window_box_width (w, area);
24508 x += area_left;
24510 /* Build a doubly-linked list of glyph_string structures between
24511 head and tail from what we have to draw. Note that the macro
24512 BUILD_GLYPH_STRINGS will modify its start parameter. That's
24513 the reason we use a separate variable `i'. */
24514 i = start;
24515 BUILD_GLYPH_STRINGS (i, end, head, tail, hl, x, last_x);
24516 if (tail)
24517 x_reached = tail->x + tail->background_width;
24518 else
24519 x_reached = x;
24521 /* If there are any glyphs with lbearing < 0 or rbearing > width in
24522 the row, redraw some glyphs in front or following the glyph
24523 strings built above. */
24524 if (head && !overlaps && row->contains_overlapping_glyphs_p)
24526 struct glyph_string *h, *t;
24527 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
24528 int mouse_beg_col IF_LINT (= 0), mouse_end_col IF_LINT (= 0);
24529 int check_mouse_face = 0;
24530 int dummy_x = 0;
24532 /* If mouse highlighting is on, we may need to draw adjacent
24533 glyphs using mouse-face highlighting. */
24534 if (area == TEXT_AREA && row->mouse_face_p
24535 && hlinfo->mouse_face_beg_row >= 0
24536 && hlinfo->mouse_face_end_row >= 0)
24538 ptrdiff_t row_vpos = MATRIX_ROW_VPOS (row, w->current_matrix);
24540 if (row_vpos >= hlinfo->mouse_face_beg_row
24541 && row_vpos <= hlinfo->mouse_face_end_row)
24543 check_mouse_face = 1;
24544 mouse_beg_col = (row_vpos == hlinfo->mouse_face_beg_row)
24545 ? hlinfo->mouse_face_beg_col : 0;
24546 mouse_end_col = (row_vpos == hlinfo->mouse_face_end_row)
24547 ? hlinfo->mouse_face_end_col
24548 : row->used[TEXT_AREA];
24552 /* Compute overhangs for all glyph strings. */
24553 if (FRAME_RIF (f)->compute_glyph_string_overhangs)
24554 for (s = head; s; s = s->next)
24555 FRAME_RIF (f)->compute_glyph_string_overhangs (s);
24557 /* Prepend glyph strings for glyphs in front of the first glyph
24558 string that are overwritten because of the first glyph
24559 string's left overhang. The background of all strings
24560 prepended must be drawn because the first glyph string
24561 draws over it. */
24562 i = left_overwritten (head);
24563 if (i >= 0)
24565 enum draw_glyphs_face overlap_hl;
24567 /* If this row contains mouse highlighting, attempt to draw
24568 the overlapped glyphs with the correct highlight. This
24569 code fails if the overlap encompasses more than one glyph
24570 and mouse-highlight spans only some of these glyphs.
24571 However, making it work perfectly involves a lot more
24572 code, and I don't know if the pathological case occurs in
24573 practice, so we'll stick to this for now. --- cyd */
24574 if (check_mouse_face
24575 && mouse_beg_col < start && mouse_end_col > i)
24576 overlap_hl = DRAW_MOUSE_FACE;
24577 else
24578 overlap_hl = DRAW_NORMAL_TEXT;
24580 j = i;
24581 BUILD_GLYPH_STRINGS (j, start, h, t,
24582 overlap_hl, dummy_x, last_x);
24583 start = i;
24584 compute_overhangs_and_x (t, head->x, 1);
24585 prepend_glyph_string_lists (&head, &tail, h, t);
24586 clip_head = head;
24589 /* Prepend glyph strings for glyphs in front of the first glyph
24590 string that overwrite that glyph string because of their
24591 right overhang. For these strings, only the foreground must
24592 be drawn, because it draws over the glyph string at `head'.
24593 The background must not be drawn because this would overwrite
24594 right overhangs of preceding glyphs for which no glyph
24595 strings exist. */
24596 i = left_overwriting (head);
24597 if (i >= 0)
24599 enum draw_glyphs_face overlap_hl;
24601 if (check_mouse_face
24602 && mouse_beg_col < start && mouse_end_col > i)
24603 overlap_hl = DRAW_MOUSE_FACE;
24604 else
24605 overlap_hl = DRAW_NORMAL_TEXT;
24607 clip_head = head;
24608 BUILD_GLYPH_STRINGS (i, start, h, t,
24609 overlap_hl, dummy_x, last_x);
24610 for (s = h; s; s = s->next)
24611 s->background_filled_p = 1;
24612 compute_overhangs_and_x (t, head->x, 1);
24613 prepend_glyph_string_lists (&head, &tail, h, t);
24616 /* Append glyphs strings for glyphs following the last glyph
24617 string tail that are overwritten by tail. The background of
24618 these strings has to be drawn because tail's foreground draws
24619 over it. */
24620 i = right_overwritten (tail);
24621 if (i >= 0)
24623 enum draw_glyphs_face overlap_hl;
24625 if (check_mouse_face
24626 && mouse_beg_col < i && mouse_end_col > end)
24627 overlap_hl = DRAW_MOUSE_FACE;
24628 else
24629 overlap_hl = DRAW_NORMAL_TEXT;
24631 BUILD_GLYPH_STRINGS (end, i, h, t,
24632 overlap_hl, x, last_x);
24633 /* Because BUILD_GLYPH_STRINGS updates the first argument,
24634 we don't have `end = i;' here. */
24635 compute_overhangs_and_x (h, tail->x + tail->width, 0);
24636 append_glyph_string_lists (&head, &tail, h, t);
24637 clip_tail = tail;
24640 /* Append glyph strings for glyphs following the last glyph
24641 string tail that overwrite tail. The foreground of such
24642 glyphs has to be drawn because it writes into the background
24643 of tail. The background must not be drawn because it could
24644 paint over the foreground of following glyphs. */
24645 i = right_overwriting (tail);
24646 if (i >= 0)
24648 enum draw_glyphs_face overlap_hl;
24649 if (check_mouse_face
24650 && mouse_beg_col < i && mouse_end_col > end)
24651 overlap_hl = DRAW_MOUSE_FACE;
24652 else
24653 overlap_hl = DRAW_NORMAL_TEXT;
24655 clip_tail = tail;
24656 i++; /* We must include the Ith glyph. */
24657 BUILD_GLYPH_STRINGS (end, i, h, t,
24658 overlap_hl, x, last_x);
24659 for (s = h; s; s = s->next)
24660 s->background_filled_p = 1;
24661 compute_overhangs_and_x (h, tail->x + tail->width, 0);
24662 append_glyph_string_lists (&head, &tail, h, t);
24664 if (clip_head || clip_tail)
24665 for (s = head; s; s = s->next)
24667 s->clip_head = clip_head;
24668 s->clip_tail = clip_tail;
24672 /* Draw all strings. */
24673 for (s = head; s; s = s->next)
24674 FRAME_RIF (f)->draw_glyph_string (s);
24676 #ifndef HAVE_NS
24677 /* When focus a sole frame and move horizontally, this sets on_p to 0
24678 causing a failure to erase prev cursor position. */
24679 if (area == TEXT_AREA
24680 && !row->full_width_p
24681 /* When drawing overlapping rows, only the glyph strings'
24682 foreground is drawn, which doesn't erase a cursor
24683 completely. */
24684 && !overlaps)
24686 int x0 = clip_head ? clip_head->x : (head ? head->x : x);
24687 int x1 = (clip_tail ? clip_tail->x + clip_tail->background_width
24688 : (tail ? tail->x + tail->background_width : x));
24689 x0 -= area_left;
24690 x1 -= area_left;
24692 notice_overwritten_cursor (w, TEXT_AREA, x0, x1,
24693 row->y, MATRIX_ROW_BOTTOM_Y (row));
24695 #endif
24697 /* Value is the x-position up to which drawn, relative to AREA of W.
24698 This doesn't include parts drawn because of overhangs. */
24699 if (row->full_width_p)
24700 x_reached = FRAME_TO_WINDOW_PIXEL_X (w, x_reached);
24701 else
24702 x_reached -= area_left;
24704 RELEASE_HDC (hdc, f);
24706 return x_reached;
24709 /* Expand row matrix if too narrow. Don't expand if area
24710 is not present. */
24712 #define IT_EXPAND_MATRIX_WIDTH(it, area) \
24714 if (!it->f->fonts_changed \
24715 && (it->glyph_row->glyphs[area] \
24716 < it->glyph_row->glyphs[area + 1])) \
24718 it->w->ncols_scale_factor++; \
24719 it->f->fonts_changed = 1; \
24723 /* Store one glyph for IT->char_to_display in IT->glyph_row.
24724 Called from x_produce_glyphs when IT->glyph_row is non-null. */
24726 static void
24727 append_glyph (struct it *it)
24729 struct glyph *glyph;
24730 enum glyph_row_area area = it->area;
24732 eassert (it->glyph_row);
24733 eassert (it->char_to_display != '\n' && it->char_to_display != '\t');
24735 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
24736 if (glyph < it->glyph_row->glyphs[area + 1])
24738 /* If the glyph row is reversed, we need to prepend the glyph
24739 rather than append it. */
24740 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24742 struct glyph *g;
24744 /* Make room for the additional glyph. */
24745 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
24746 g[1] = *g;
24747 glyph = it->glyph_row->glyphs[area];
24749 glyph->charpos = CHARPOS (it->position);
24750 glyph->object = it->object;
24751 if (it->pixel_width > 0)
24753 glyph->pixel_width = it->pixel_width;
24754 glyph->padding_p = 0;
24756 else
24758 /* Assure at least 1-pixel width. Otherwise, cursor can't
24759 be displayed correctly. */
24760 glyph->pixel_width = 1;
24761 glyph->padding_p = 1;
24763 glyph->ascent = it->ascent;
24764 glyph->descent = it->descent;
24765 glyph->voffset = it->voffset;
24766 glyph->type = CHAR_GLYPH;
24767 glyph->avoid_cursor_p = it->avoid_cursor_p;
24768 glyph->multibyte_p = it->multibyte_p;
24769 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24771 /* In R2L rows, the left and the right box edges need to be
24772 drawn in reverse direction. */
24773 glyph->right_box_line_p = it->start_of_box_run_p;
24774 glyph->left_box_line_p = it->end_of_box_run_p;
24776 else
24778 glyph->left_box_line_p = it->start_of_box_run_p;
24779 glyph->right_box_line_p = it->end_of_box_run_p;
24781 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
24782 || it->phys_descent > it->descent);
24783 glyph->glyph_not_available_p = it->glyph_not_available_p;
24784 glyph->face_id = it->face_id;
24785 glyph->u.ch = it->char_to_display;
24786 glyph->slice.img = null_glyph_slice;
24787 glyph->font_type = FONT_TYPE_UNKNOWN;
24788 if (it->bidi_p)
24790 glyph->resolved_level = it->bidi_it.resolved_level;
24791 if ((it->bidi_it.type & 7) != it->bidi_it.type)
24792 emacs_abort ();
24793 glyph->bidi_type = it->bidi_it.type;
24795 else
24797 glyph->resolved_level = 0;
24798 glyph->bidi_type = UNKNOWN_BT;
24800 ++it->glyph_row->used[area];
24802 else
24803 IT_EXPAND_MATRIX_WIDTH (it, area);
24806 /* Store one glyph for the composition IT->cmp_it.id in
24807 IT->glyph_row. Called from x_produce_glyphs when IT->glyph_row is
24808 non-null. */
24810 static void
24811 append_composite_glyph (struct it *it)
24813 struct glyph *glyph;
24814 enum glyph_row_area area = it->area;
24816 eassert (it->glyph_row);
24818 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
24819 if (glyph < it->glyph_row->glyphs[area + 1])
24821 /* If the glyph row is reversed, we need to prepend the glyph
24822 rather than append it. */
24823 if (it->glyph_row->reversed_p && it->area == TEXT_AREA)
24825 struct glyph *g;
24827 /* Make room for the new glyph. */
24828 for (g = glyph - 1; g >= it->glyph_row->glyphs[it->area]; g--)
24829 g[1] = *g;
24830 glyph = it->glyph_row->glyphs[it->area];
24832 glyph->charpos = it->cmp_it.charpos;
24833 glyph->object = it->object;
24834 glyph->pixel_width = it->pixel_width;
24835 glyph->ascent = it->ascent;
24836 glyph->descent = it->descent;
24837 glyph->voffset = it->voffset;
24838 glyph->type = COMPOSITE_GLYPH;
24839 if (it->cmp_it.ch < 0)
24841 glyph->u.cmp.automatic = 0;
24842 glyph->u.cmp.id = it->cmp_it.id;
24843 glyph->slice.cmp.from = glyph->slice.cmp.to = 0;
24845 else
24847 glyph->u.cmp.automatic = 1;
24848 glyph->u.cmp.id = it->cmp_it.id;
24849 glyph->slice.cmp.from = it->cmp_it.from;
24850 glyph->slice.cmp.to = it->cmp_it.to - 1;
24852 glyph->avoid_cursor_p = it->avoid_cursor_p;
24853 glyph->multibyte_p = it->multibyte_p;
24854 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24856 /* In R2L rows, the left and the right box edges need to be
24857 drawn in reverse direction. */
24858 glyph->right_box_line_p = it->start_of_box_run_p;
24859 glyph->left_box_line_p = it->end_of_box_run_p;
24861 else
24863 glyph->left_box_line_p = it->start_of_box_run_p;
24864 glyph->right_box_line_p = it->end_of_box_run_p;
24866 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
24867 || it->phys_descent > it->descent);
24868 glyph->padding_p = 0;
24869 glyph->glyph_not_available_p = 0;
24870 glyph->face_id = it->face_id;
24871 glyph->font_type = FONT_TYPE_UNKNOWN;
24872 if (it->bidi_p)
24874 glyph->resolved_level = it->bidi_it.resolved_level;
24875 if ((it->bidi_it.type & 7) != it->bidi_it.type)
24876 emacs_abort ();
24877 glyph->bidi_type = it->bidi_it.type;
24879 ++it->glyph_row->used[area];
24881 else
24882 IT_EXPAND_MATRIX_WIDTH (it, area);
24886 /* Change IT->ascent and IT->height according to the setting of
24887 IT->voffset. */
24889 static void
24890 take_vertical_position_into_account (struct it *it)
24892 if (it->voffset)
24894 if (it->voffset < 0)
24895 /* Increase the ascent so that we can display the text higher
24896 in the line. */
24897 it->ascent -= it->voffset;
24898 else
24899 /* Increase the descent so that we can display the text lower
24900 in the line. */
24901 it->descent += it->voffset;
24906 /* Produce glyphs/get display metrics for the image IT is loaded with.
24907 See the description of struct display_iterator in dispextern.h for
24908 an overview of struct display_iterator. */
24910 static void
24911 produce_image_glyph (struct it *it)
24913 struct image *img;
24914 struct face *face;
24915 int glyph_ascent, crop;
24916 struct glyph_slice slice;
24918 eassert (it->what == IT_IMAGE);
24920 face = FACE_FROM_ID (it->f, it->face_id);
24921 eassert (face);
24922 /* Make sure X resources of the face is loaded. */
24923 PREPARE_FACE_FOR_DISPLAY (it->f, face);
24925 if (it->image_id < 0)
24927 /* Fringe bitmap. */
24928 it->ascent = it->phys_ascent = 0;
24929 it->descent = it->phys_descent = 0;
24930 it->pixel_width = 0;
24931 it->nglyphs = 0;
24932 return;
24935 img = IMAGE_FROM_ID (it->f, it->image_id);
24936 eassert (img);
24937 /* Make sure X resources of the image is loaded. */
24938 prepare_image_for_display (it->f, img);
24940 slice.x = slice.y = 0;
24941 slice.width = img->width;
24942 slice.height = img->height;
24944 if (INTEGERP (it->slice.x))
24945 slice.x = XINT (it->slice.x);
24946 else if (FLOATP (it->slice.x))
24947 slice.x = XFLOAT_DATA (it->slice.x) * img->width;
24949 if (INTEGERP (it->slice.y))
24950 slice.y = XINT (it->slice.y);
24951 else if (FLOATP (it->slice.y))
24952 slice.y = XFLOAT_DATA (it->slice.y) * img->height;
24954 if (INTEGERP (it->slice.width))
24955 slice.width = XINT (it->slice.width);
24956 else if (FLOATP (it->slice.width))
24957 slice.width = XFLOAT_DATA (it->slice.width) * img->width;
24959 if (INTEGERP (it->slice.height))
24960 slice.height = XINT (it->slice.height);
24961 else if (FLOATP (it->slice.height))
24962 slice.height = XFLOAT_DATA (it->slice.height) * img->height;
24964 if (slice.x >= img->width)
24965 slice.x = img->width;
24966 if (slice.y >= img->height)
24967 slice.y = img->height;
24968 if (slice.x + slice.width >= img->width)
24969 slice.width = img->width - slice.x;
24970 if (slice.y + slice.height > img->height)
24971 slice.height = img->height - slice.y;
24973 if (slice.width == 0 || slice.height == 0)
24974 return;
24976 it->ascent = it->phys_ascent = glyph_ascent = image_ascent (img, face, &slice);
24978 it->descent = slice.height - glyph_ascent;
24979 if (slice.y == 0)
24980 it->descent += img->vmargin;
24981 if (slice.y + slice.height == img->height)
24982 it->descent += img->vmargin;
24983 it->phys_descent = it->descent;
24985 it->pixel_width = slice.width;
24986 if (slice.x == 0)
24987 it->pixel_width += img->hmargin;
24988 if (slice.x + slice.width == img->width)
24989 it->pixel_width += img->hmargin;
24991 /* It's quite possible for images to have an ascent greater than
24992 their height, so don't get confused in that case. */
24993 if (it->descent < 0)
24994 it->descent = 0;
24996 it->nglyphs = 1;
24998 if (face->box != FACE_NO_BOX)
25000 if (face->box_line_width > 0)
25002 if (slice.y == 0)
25003 it->ascent += face->box_line_width;
25004 if (slice.y + slice.height == img->height)
25005 it->descent += face->box_line_width;
25008 if (it->start_of_box_run_p && slice.x == 0)
25009 it->pixel_width += eabs (face->box_line_width);
25010 if (it->end_of_box_run_p && slice.x + slice.width == img->width)
25011 it->pixel_width += eabs (face->box_line_width);
25014 take_vertical_position_into_account (it);
25016 /* Automatically crop wide image glyphs at right edge so we can
25017 draw the cursor on same display row. */
25018 if ((crop = it->pixel_width - (it->last_visible_x - it->current_x), crop > 0)
25019 && (it->hpos == 0 || it->pixel_width > it->last_visible_x / 4))
25021 it->pixel_width -= crop;
25022 slice.width -= crop;
25025 if (it->glyph_row)
25027 struct glyph *glyph;
25028 enum glyph_row_area area = it->area;
25030 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
25031 if (glyph < it->glyph_row->glyphs[area + 1])
25033 glyph->charpos = CHARPOS (it->position);
25034 glyph->object = it->object;
25035 glyph->pixel_width = it->pixel_width;
25036 glyph->ascent = glyph_ascent;
25037 glyph->descent = it->descent;
25038 glyph->voffset = it->voffset;
25039 glyph->type = IMAGE_GLYPH;
25040 glyph->avoid_cursor_p = it->avoid_cursor_p;
25041 glyph->multibyte_p = it->multibyte_p;
25042 if (it->glyph_row->reversed_p && area == TEXT_AREA)
25044 /* In R2L rows, the left and the right box edges need to be
25045 drawn in reverse direction. */
25046 glyph->right_box_line_p = it->start_of_box_run_p;
25047 glyph->left_box_line_p = it->end_of_box_run_p;
25049 else
25051 glyph->left_box_line_p = it->start_of_box_run_p;
25052 glyph->right_box_line_p = it->end_of_box_run_p;
25054 glyph->overlaps_vertically_p = 0;
25055 glyph->padding_p = 0;
25056 glyph->glyph_not_available_p = 0;
25057 glyph->face_id = it->face_id;
25058 glyph->u.img_id = img->id;
25059 glyph->slice.img = slice;
25060 glyph->font_type = FONT_TYPE_UNKNOWN;
25061 if (it->bidi_p)
25063 glyph->resolved_level = it->bidi_it.resolved_level;
25064 if ((it->bidi_it.type & 7) != it->bidi_it.type)
25065 emacs_abort ();
25066 glyph->bidi_type = it->bidi_it.type;
25068 ++it->glyph_row->used[area];
25070 else
25071 IT_EXPAND_MATRIX_WIDTH (it, area);
25076 /* Append a stretch glyph to IT->glyph_row. OBJECT is the source
25077 of the glyph, WIDTH and HEIGHT are the width and height of the
25078 stretch. ASCENT is the ascent of the glyph (0 <= ASCENT <= HEIGHT). */
25080 static void
25081 append_stretch_glyph (struct it *it, Lisp_Object object,
25082 int width, int height, int ascent)
25084 struct glyph *glyph;
25085 enum glyph_row_area area = it->area;
25087 eassert (ascent >= 0 && ascent <= height);
25089 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
25090 if (glyph < it->glyph_row->glyphs[area + 1])
25092 /* If the glyph row is reversed, we need to prepend the glyph
25093 rather than append it. */
25094 if (it->glyph_row->reversed_p && area == TEXT_AREA)
25096 struct glyph *g;
25098 /* Make room for the additional glyph. */
25099 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
25100 g[1] = *g;
25101 glyph = it->glyph_row->glyphs[area];
25103 glyph->charpos = CHARPOS (it->position);
25104 glyph->object = object;
25105 glyph->pixel_width = width;
25106 glyph->ascent = ascent;
25107 glyph->descent = height - ascent;
25108 glyph->voffset = it->voffset;
25109 glyph->type = STRETCH_GLYPH;
25110 glyph->avoid_cursor_p = it->avoid_cursor_p;
25111 glyph->multibyte_p = it->multibyte_p;
25112 if (it->glyph_row->reversed_p && area == TEXT_AREA)
25114 /* In R2L rows, the left and the right box edges need to be
25115 drawn in reverse direction. */
25116 glyph->right_box_line_p = it->start_of_box_run_p;
25117 glyph->left_box_line_p = it->end_of_box_run_p;
25119 else
25121 glyph->left_box_line_p = it->start_of_box_run_p;
25122 glyph->right_box_line_p = it->end_of_box_run_p;
25124 glyph->overlaps_vertically_p = 0;
25125 glyph->padding_p = 0;
25126 glyph->glyph_not_available_p = 0;
25127 glyph->face_id = it->face_id;
25128 glyph->u.stretch.ascent = ascent;
25129 glyph->u.stretch.height = height;
25130 glyph->slice.img = null_glyph_slice;
25131 glyph->font_type = FONT_TYPE_UNKNOWN;
25132 if (it->bidi_p)
25134 glyph->resolved_level = it->bidi_it.resolved_level;
25135 if ((it->bidi_it.type & 7) != it->bidi_it.type)
25136 emacs_abort ();
25137 glyph->bidi_type = it->bidi_it.type;
25139 else
25141 glyph->resolved_level = 0;
25142 glyph->bidi_type = UNKNOWN_BT;
25144 ++it->glyph_row->used[area];
25146 else
25147 IT_EXPAND_MATRIX_WIDTH (it, area);
25150 #endif /* HAVE_WINDOW_SYSTEM */
25152 /* Produce a stretch glyph for iterator IT. IT->object is the value
25153 of the glyph property displayed. The value must be a list
25154 `(space KEYWORD VALUE ...)' with the following KEYWORD/VALUE pairs
25155 being recognized:
25157 1. `:width WIDTH' specifies that the space should be WIDTH *
25158 canonical char width wide. WIDTH may be an integer or floating
25159 point number.
25161 2. `:relative-width FACTOR' specifies that the width of the stretch
25162 should be computed from the width of the first character having the
25163 `glyph' property, and should be FACTOR times that width.
25165 3. `:align-to HPOS' specifies that the space should be wide enough
25166 to reach HPOS, a value in canonical character units.
25168 Exactly one of the above pairs must be present.
25170 4. `:height HEIGHT' specifies that the height of the stretch produced
25171 should be HEIGHT, measured in canonical character units.
25173 5. `:relative-height FACTOR' specifies that the height of the
25174 stretch should be FACTOR times the height of the characters having
25175 the glyph property.
25177 Either none or exactly one of 4 or 5 must be present.
25179 6. `:ascent ASCENT' specifies that ASCENT percent of the height
25180 of the stretch should be used for the ascent of the stretch.
25181 ASCENT must be in the range 0 <= ASCENT <= 100. */
25183 void
25184 produce_stretch_glyph (struct it *it)
25186 /* (space :width WIDTH :height HEIGHT ...) */
25187 Lisp_Object prop, plist;
25188 int width = 0, height = 0, align_to = -1;
25189 int zero_width_ok_p = 0;
25190 double tem;
25191 struct font *font = NULL;
25193 #ifdef HAVE_WINDOW_SYSTEM
25194 int ascent = 0;
25195 int zero_height_ok_p = 0;
25197 if (FRAME_WINDOW_P (it->f))
25199 struct face *face = FACE_FROM_ID (it->f, it->face_id);
25200 font = face->font ? face->font : FRAME_FONT (it->f);
25201 PREPARE_FACE_FOR_DISPLAY (it->f, face);
25203 #endif
25205 /* List should start with `space'. */
25206 eassert (CONSP (it->object) && EQ (XCAR (it->object), Qspace));
25207 plist = XCDR (it->object);
25209 /* Compute the width of the stretch. */
25210 if ((prop = Fplist_get (plist, QCwidth), !NILP (prop))
25211 && calc_pixel_width_or_height (&tem, it, prop, font, 1, 0))
25213 /* Absolute width `:width WIDTH' specified and valid. */
25214 zero_width_ok_p = 1;
25215 width = (int)tem;
25217 #ifdef HAVE_WINDOW_SYSTEM
25218 else if (FRAME_WINDOW_P (it->f)
25219 && (prop = Fplist_get (plist, QCrelative_width), NUMVAL (prop) > 0))
25221 /* Relative width `:relative-width FACTOR' specified and valid.
25222 Compute the width of the characters having the `glyph'
25223 property. */
25224 struct it it2;
25225 unsigned char *p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
25227 it2 = *it;
25228 if (it->multibyte_p)
25229 it2.c = it2.char_to_display = STRING_CHAR_AND_LENGTH (p, it2.len);
25230 else
25232 it2.c = it2.char_to_display = *p, it2.len = 1;
25233 if (! ASCII_CHAR_P (it2.c))
25234 it2.char_to_display = BYTE8_TO_CHAR (it2.c);
25237 it2.glyph_row = NULL;
25238 it2.what = IT_CHARACTER;
25239 x_produce_glyphs (&it2);
25240 width = NUMVAL (prop) * it2.pixel_width;
25242 #endif /* HAVE_WINDOW_SYSTEM */
25243 else if ((prop = Fplist_get (plist, QCalign_to), !NILP (prop))
25244 && calc_pixel_width_or_height (&tem, it, prop, font, 1, &align_to))
25246 if (it->glyph_row == NULL || !it->glyph_row->mode_line_p)
25247 align_to = (align_to < 0
25249 : align_to - window_box_left_offset (it->w, TEXT_AREA));
25250 else if (align_to < 0)
25251 align_to = window_box_left_offset (it->w, TEXT_AREA);
25252 width = max (0, (int)tem + align_to - it->current_x);
25253 zero_width_ok_p = 1;
25255 else
25256 /* Nothing specified -> width defaults to canonical char width. */
25257 width = FRAME_COLUMN_WIDTH (it->f);
25259 if (width <= 0 && (width < 0 || !zero_width_ok_p))
25260 width = 1;
25262 #ifdef HAVE_WINDOW_SYSTEM
25263 /* Compute height. */
25264 if (FRAME_WINDOW_P (it->f))
25266 if ((prop = Fplist_get (plist, QCheight), !NILP (prop))
25267 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
25269 height = (int)tem;
25270 zero_height_ok_p = 1;
25272 else if (prop = Fplist_get (plist, QCrelative_height),
25273 NUMVAL (prop) > 0)
25274 height = FONT_HEIGHT (font) * NUMVAL (prop);
25275 else
25276 height = FONT_HEIGHT (font);
25278 if (height <= 0 && (height < 0 || !zero_height_ok_p))
25279 height = 1;
25281 /* Compute percentage of height used for ascent. If
25282 `:ascent ASCENT' is present and valid, use that. Otherwise,
25283 derive the ascent from the font in use. */
25284 if (prop = Fplist_get (plist, QCascent),
25285 NUMVAL (prop) > 0 && NUMVAL (prop) <= 100)
25286 ascent = height * NUMVAL (prop) / 100.0;
25287 else if (!NILP (prop)
25288 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
25289 ascent = min (max (0, (int)tem), height);
25290 else
25291 ascent = (height * FONT_BASE (font)) / FONT_HEIGHT (font);
25293 else
25294 #endif /* HAVE_WINDOW_SYSTEM */
25295 height = 1;
25297 if (width > 0 && it->line_wrap != TRUNCATE
25298 && it->current_x + width > it->last_visible_x)
25300 width = it->last_visible_x - it->current_x;
25301 #ifdef HAVE_WINDOW_SYSTEM
25302 /* Subtract one more pixel from the stretch width, but only on
25303 GUI frames, since on a TTY each glyph is one "pixel" wide. */
25304 width -= FRAME_WINDOW_P (it->f);
25305 #endif
25308 if (width > 0 && height > 0 && it->glyph_row)
25310 Lisp_Object o_object = it->object;
25311 Lisp_Object object = it->stack[it->sp - 1].string;
25312 int n = width;
25314 if (!STRINGP (object))
25315 object = it->w->contents;
25316 #ifdef HAVE_WINDOW_SYSTEM
25317 if (FRAME_WINDOW_P (it->f))
25318 append_stretch_glyph (it, object, width, height, ascent);
25319 else
25320 #endif
25322 it->object = object;
25323 it->char_to_display = ' ';
25324 it->pixel_width = it->len = 1;
25325 while (n--)
25326 tty_append_glyph (it);
25327 it->object = o_object;
25331 it->pixel_width = width;
25332 #ifdef HAVE_WINDOW_SYSTEM
25333 if (FRAME_WINDOW_P (it->f))
25335 it->ascent = it->phys_ascent = ascent;
25336 it->descent = it->phys_descent = height - it->ascent;
25337 it->nglyphs = width > 0 && height > 0 ? 1 : 0;
25338 take_vertical_position_into_account (it);
25340 else
25341 #endif
25342 it->nglyphs = width;
25345 /* Get information about special display element WHAT in an
25346 environment described by IT. WHAT is one of IT_TRUNCATION or
25347 IT_CONTINUATION. Maybe produce glyphs for WHAT if IT has a
25348 non-null glyph_row member. This function ensures that fields like
25349 face_id, c, len of IT are left untouched. */
25351 static void
25352 produce_special_glyphs (struct it *it, enum display_element_type what)
25354 struct it temp_it;
25355 Lisp_Object gc;
25356 GLYPH glyph;
25358 temp_it = *it;
25359 temp_it.object = make_number (0);
25360 memset (&temp_it.current, 0, sizeof temp_it.current);
25362 if (what == IT_CONTINUATION)
25364 /* Continuation glyph. For R2L lines, we mirror it by hand. */
25365 if (it->bidi_it.paragraph_dir == R2L)
25366 SET_GLYPH_FROM_CHAR (glyph, '/');
25367 else
25368 SET_GLYPH_FROM_CHAR (glyph, '\\');
25369 if (it->dp
25370 && (gc = DISP_CONTINUE_GLYPH (it->dp), GLYPH_CODE_P (gc)))
25372 /* FIXME: Should we mirror GC for R2L lines? */
25373 SET_GLYPH_FROM_GLYPH_CODE (glyph, gc);
25374 spec_glyph_lookup_face (XWINDOW (it->window), &glyph);
25377 else if (what == IT_TRUNCATION)
25379 /* Truncation glyph. */
25380 SET_GLYPH_FROM_CHAR (glyph, '$');
25381 if (it->dp
25382 && (gc = DISP_TRUNC_GLYPH (it->dp), GLYPH_CODE_P (gc)))
25384 /* FIXME: Should we mirror GC for R2L lines? */
25385 SET_GLYPH_FROM_GLYPH_CODE (glyph, gc);
25386 spec_glyph_lookup_face (XWINDOW (it->window), &glyph);
25389 else
25390 emacs_abort ();
25392 #ifdef HAVE_WINDOW_SYSTEM
25393 /* On a GUI frame, when the right fringe (left fringe for R2L rows)
25394 is turned off, we precede the truncation/continuation glyphs by a
25395 stretch glyph whose width is computed such that these special
25396 glyphs are aligned at the window margin, even when very different
25397 fonts are used in different glyph rows. */
25398 if (FRAME_WINDOW_P (temp_it.f)
25399 /* init_iterator calls this with it->glyph_row == NULL, and it
25400 wants only the pixel width of the truncation/continuation
25401 glyphs. */
25402 && temp_it.glyph_row
25403 /* insert_left_trunc_glyphs calls us at the beginning of the
25404 row, and it has its own calculation of the stretch glyph
25405 width. */
25406 && temp_it.glyph_row->used[TEXT_AREA] > 0
25407 && (temp_it.glyph_row->reversed_p
25408 ? WINDOW_LEFT_FRINGE_WIDTH (temp_it.w)
25409 : WINDOW_RIGHT_FRINGE_WIDTH (temp_it.w)) == 0)
25411 int stretch_width = temp_it.last_visible_x - temp_it.current_x;
25413 if (stretch_width > 0)
25415 struct face *face = FACE_FROM_ID (temp_it.f, temp_it.face_id);
25416 struct font *font =
25417 face->font ? face->font : FRAME_FONT (temp_it.f);
25418 int stretch_ascent =
25419 (((temp_it.ascent + temp_it.descent)
25420 * FONT_BASE (font)) / FONT_HEIGHT (font));
25422 append_stretch_glyph (&temp_it, make_number (0), stretch_width,
25423 temp_it.ascent + temp_it.descent,
25424 stretch_ascent);
25427 #endif
25429 temp_it.dp = NULL;
25430 temp_it.what = IT_CHARACTER;
25431 temp_it.len = 1;
25432 temp_it.c = temp_it.char_to_display = GLYPH_CHAR (glyph);
25433 temp_it.face_id = GLYPH_FACE (glyph);
25434 temp_it.len = CHAR_BYTES (temp_it.c);
25436 PRODUCE_GLYPHS (&temp_it);
25437 it->pixel_width = temp_it.pixel_width;
25438 it->nglyphs = temp_it.pixel_width;
25441 #ifdef HAVE_WINDOW_SYSTEM
25443 /* Calculate line-height and line-spacing properties.
25444 An integer value specifies explicit pixel value.
25445 A float value specifies relative value to current face height.
25446 A cons (float . face-name) specifies relative value to
25447 height of specified face font.
25449 Returns height in pixels, or nil. */
25452 static Lisp_Object
25453 calc_line_height_property (struct it *it, Lisp_Object val, struct font *font,
25454 int boff, int override)
25456 Lisp_Object face_name = Qnil;
25457 int ascent, descent, height;
25459 if (NILP (val) || INTEGERP (val) || (override && EQ (val, Qt)))
25460 return val;
25462 if (CONSP (val))
25464 face_name = XCAR (val);
25465 val = XCDR (val);
25466 if (!NUMBERP (val))
25467 val = make_number (1);
25468 if (NILP (face_name))
25470 height = it->ascent + it->descent;
25471 goto scale;
25475 if (NILP (face_name))
25477 font = FRAME_FONT (it->f);
25478 boff = FRAME_BASELINE_OFFSET (it->f);
25480 else if (EQ (face_name, Qt))
25482 override = 0;
25484 else
25486 int face_id;
25487 struct face *face;
25489 face_id = lookup_named_face (it->f, face_name, 0);
25490 if (face_id < 0)
25491 return make_number (-1);
25493 face = FACE_FROM_ID (it->f, face_id);
25494 font = face->font;
25495 if (font == NULL)
25496 return make_number (-1);
25497 boff = font->baseline_offset;
25498 if (font->vertical_centering)
25499 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
25502 ascent = FONT_BASE (font) + boff;
25503 descent = FONT_DESCENT (font) - boff;
25505 if (override)
25507 it->override_ascent = ascent;
25508 it->override_descent = descent;
25509 it->override_boff = boff;
25512 height = ascent + descent;
25514 scale:
25515 if (FLOATP (val))
25516 height = (int)(XFLOAT_DATA (val) * height);
25517 else if (INTEGERP (val))
25518 height *= XINT (val);
25520 return make_number (height);
25524 /* Append a glyph for a glyphless character to IT->glyph_row. FACE_ID
25525 is a face ID to be used for the glyph. FOR_NO_FONT is nonzero if
25526 and only if this is for a character for which no font was found.
25528 If the display method (it->glyphless_method) is
25529 GLYPHLESS_DISPLAY_ACRONYM or GLYPHLESS_DISPLAY_HEX_CODE, LEN is a
25530 length of the acronym or the hexadecimal string, UPPER_XOFF and
25531 UPPER_YOFF are pixel offsets for the upper part of the string,
25532 LOWER_XOFF and LOWER_YOFF are for the lower part.
25534 For the other display methods, LEN through LOWER_YOFF are zero. */
25536 static void
25537 append_glyphless_glyph (struct it *it, int face_id, int for_no_font, int len,
25538 short upper_xoff, short upper_yoff,
25539 short lower_xoff, short lower_yoff)
25541 struct glyph *glyph;
25542 enum glyph_row_area area = it->area;
25544 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
25545 if (glyph < it->glyph_row->glyphs[area + 1])
25547 /* If the glyph row is reversed, we need to prepend the glyph
25548 rather than append it. */
25549 if (it->glyph_row->reversed_p && area == TEXT_AREA)
25551 struct glyph *g;
25553 /* Make room for the additional glyph. */
25554 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
25555 g[1] = *g;
25556 glyph = it->glyph_row->glyphs[area];
25558 glyph->charpos = CHARPOS (it->position);
25559 glyph->object = it->object;
25560 glyph->pixel_width = it->pixel_width;
25561 glyph->ascent = it->ascent;
25562 glyph->descent = it->descent;
25563 glyph->voffset = it->voffset;
25564 glyph->type = GLYPHLESS_GLYPH;
25565 glyph->u.glyphless.method = it->glyphless_method;
25566 glyph->u.glyphless.for_no_font = for_no_font;
25567 glyph->u.glyphless.len = len;
25568 glyph->u.glyphless.ch = it->c;
25569 glyph->slice.glyphless.upper_xoff = upper_xoff;
25570 glyph->slice.glyphless.upper_yoff = upper_yoff;
25571 glyph->slice.glyphless.lower_xoff = lower_xoff;
25572 glyph->slice.glyphless.lower_yoff = lower_yoff;
25573 glyph->avoid_cursor_p = it->avoid_cursor_p;
25574 glyph->multibyte_p = it->multibyte_p;
25575 if (it->glyph_row->reversed_p && area == TEXT_AREA)
25577 /* In R2L rows, the left and the right box edges need to be
25578 drawn in reverse direction. */
25579 glyph->right_box_line_p = it->start_of_box_run_p;
25580 glyph->left_box_line_p = it->end_of_box_run_p;
25582 else
25584 glyph->left_box_line_p = it->start_of_box_run_p;
25585 glyph->right_box_line_p = it->end_of_box_run_p;
25587 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
25588 || it->phys_descent > it->descent);
25589 glyph->padding_p = 0;
25590 glyph->glyph_not_available_p = 0;
25591 glyph->face_id = face_id;
25592 glyph->font_type = FONT_TYPE_UNKNOWN;
25593 if (it->bidi_p)
25595 glyph->resolved_level = it->bidi_it.resolved_level;
25596 if ((it->bidi_it.type & 7) != it->bidi_it.type)
25597 emacs_abort ();
25598 glyph->bidi_type = it->bidi_it.type;
25600 ++it->glyph_row->used[area];
25602 else
25603 IT_EXPAND_MATRIX_WIDTH (it, area);
25607 /* Produce a glyph for a glyphless character for iterator IT.
25608 IT->glyphless_method specifies which method to use for displaying
25609 the character. See the description of enum
25610 glyphless_display_method in dispextern.h for the detail.
25612 FOR_NO_FONT is nonzero if and only if this is for a character for
25613 which no font was found. ACRONYM, if non-nil, is an acronym string
25614 for the character. */
25616 static void
25617 produce_glyphless_glyph (struct it *it, int for_no_font, Lisp_Object acronym)
25619 int face_id;
25620 struct face *face;
25621 struct font *font;
25622 int base_width, base_height, width, height;
25623 short upper_xoff, upper_yoff, lower_xoff, lower_yoff;
25624 int len;
25626 /* Get the metrics of the base font. We always refer to the current
25627 ASCII face. */
25628 face = FACE_FROM_ID (it->f, it->face_id)->ascii_face;
25629 font = face->font ? face->font : FRAME_FONT (it->f);
25630 it->ascent = FONT_BASE (font) + font->baseline_offset;
25631 it->descent = FONT_DESCENT (font) - font->baseline_offset;
25632 base_height = it->ascent + it->descent;
25633 base_width = font->average_width;
25635 face_id = merge_glyphless_glyph_face (it);
25637 if (it->glyphless_method == GLYPHLESS_DISPLAY_THIN_SPACE)
25639 it->pixel_width = THIN_SPACE_WIDTH;
25640 len = 0;
25641 upper_xoff = upper_yoff = lower_xoff = lower_yoff = 0;
25643 else if (it->glyphless_method == GLYPHLESS_DISPLAY_EMPTY_BOX)
25645 width = CHAR_WIDTH (it->c);
25646 if (width == 0)
25647 width = 1;
25648 else if (width > 4)
25649 width = 4;
25650 it->pixel_width = base_width * width;
25651 len = 0;
25652 upper_xoff = upper_yoff = lower_xoff = lower_yoff = 0;
25654 else
25656 char buf[7];
25657 const char *str;
25658 unsigned int code[6];
25659 int upper_len;
25660 int ascent, descent;
25661 struct font_metrics metrics_upper, metrics_lower;
25663 face = FACE_FROM_ID (it->f, face_id);
25664 font = face->font ? face->font : FRAME_FONT (it->f);
25665 PREPARE_FACE_FOR_DISPLAY (it->f, face);
25667 if (it->glyphless_method == GLYPHLESS_DISPLAY_ACRONYM)
25669 if (! STRINGP (acronym) && CHAR_TABLE_P (Vglyphless_char_display))
25670 acronym = CHAR_TABLE_REF (Vglyphless_char_display, it->c);
25671 if (CONSP (acronym))
25672 acronym = XCAR (acronym);
25673 str = STRINGP (acronym) ? SSDATA (acronym) : "";
25675 else
25677 eassert (it->glyphless_method == GLYPHLESS_DISPLAY_HEX_CODE);
25678 sprintf (buf, "%0*X", it->c < 0x10000 ? 4 : 6, it->c);
25679 str = buf;
25681 for (len = 0; str[len] && ASCII_BYTE_P (str[len]) && len < 6; len++)
25682 code[len] = font->driver->encode_char (font, str[len]);
25683 upper_len = (len + 1) / 2;
25684 font->driver->text_extents (font, code, upper_len,
25685 &metrics_upper);
25686 font->driver->text_extents (font, code + upper_len, len - upper_len,
25687 &metrics_lower);
25691 /* +4 is for vertical bars of a box plus 1-pixel spaces at both side. */
25692 width = max (metrics_upper.width, metrics_lower.width) + 4;
25693 upper_xoff = upper_yoff = 2; /* the typical case */
25694 if (base_width >= width)
25696 /* Align the upper to the left, the lower to the right. */
25697 it->pixel_width = base_width;
25698 lower_xoff = base_width - 2 - metrics_lower.width;
25700 else
25702 /* Center the shorter one. */
25703 it->pixel_width = width;
25704 if (metrics_upper.width >= metrics_lower.width)
25705 lower_xoff = (width - metrics_lower.width) / 2;
25706 else
25708 /* FIXME: This code doesn't look right. It formerly was
25709 missing the "lower_xoff = 0;", which couldn't have
25710 been right since it left lower_xoff uninitialized. */
25711 lower_xoff = 0;
25712 upper_xoff = (width - metrics_upper.width) / 2;
25716 /* +5 is for horizontal bars of a box plus 1-pixel spaces at
25717 top, bottom, and between upper and lower strings. */
25718 height = (metrics_upper.ascent + metrics_upper.descent
25719 + metrics_lower.ascent + metrics_lower.descent) + 5;
25720 /* Center vertically.
25721 H:base_height, D:base_descent
25722 h:height, ld:lower_descent, la:lower_ascent, ud:upper_descent
25724 ascent = - (D - H/2 - h/2 + 1); "+ 1" for rounding up
25725 descent = D - H/2 + h/2;
25726 lower_yoff = descent - 2 - ld;
25727 upper_yoff = lower_yoff - la - 1 - ud; */
25728 ascent = - (it->descent - (base_height + height + 1) / 2);
25729 descent = it->descent - (base_height - height) / 2;
25730 lower_yoff = descent - 2 - metrics_lower.descent;
25731 upper_yoff = (lower_yoff - metrics_lower.ascent - 1
25732 - metrics_upper.descent);
25733 /* Don't make the height shorter than the base height. */
25734 if (height > base_height)
25736 it->ascent = ascent;
25737 it->descent = descent;
25741 it->phys_ascent = it->ascent;
25742 it->phys_descent = it->descent;
25743 if (it->glyph_row)
25744 append_glyphless_glyph (it, face_id, for_no_font, len,
25745 upper_xoff, upper_yoff,
25746 lower_xoff, lower_yoff);
25747 it->nglyphs = 1;
25748 take_vertical_position_into_account (it);
25752 /* RIF:
25753 Produce glyphs/get display metrics for the display element IT is
25754 loaded with. See the description of struct it in dispextern.h
25755 for an overview of struct it. */
25757 void
25758 x_produce_glyphs (struct it *it)
25760 int extra_line_spacing = it->extra_line_spacing;
25762 it->glyph_not_available_p = 0;
25764 if (it->what == IT_CHARACTER)
25766 XChar2b char2b;
25767 struct face *face = FACE_FROM_ID (it->f, it->face_id);
25768 struct font *font = face->font;
25769 struct font_metrics *pcm = NULL;
25770 int boff; /* Baseline offset. */
25772 if (font == NULL)
25774 /* When no suitable font is found, display this character by
25775 the method specified in the first extra slot of
25776 Vglyphless_char_display. */
25777 Lisp_Object acronym = lookup_glyphless_char_display (-1, it);
25779 eassert (it->what == IT_GLYPHLESS);
25780 produce_glyphless_glyph (it, 1, STRINGP (acronym) ? acronym : Qnil);
25781 goto done;
25784 boff = font->baseline_offset;
25785 if (font->vertical_centering)
25786 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
25788 if (it->char_to_display != '\n' && it->char_to_display != '\t')
25790 int stretched_p;
25792 it->nglyphs = 1;
25794 if (it->override_ascent >= 0)
25796 it->ascent = it->override_ascent;
25797 it->descent = it->override_descent;
25798 boff = it->override_boff;
25800 else
25802 it->ascent = FONT_BASE (font) + boff;
25803 it->descent = FONT_DESCENT (font) - boff;
25806 if (get_char_glyph_code (it->char_to_display, font, &char2b))
25808 pcm = get_per_char_metric (font, &char2b);
25809 if (pcm->width == 0
25810 && pcm->rbearing == 0 && pcm->lbearing == 0)
25811 pcm = NULL;
25814 if (pcm)
25816 it->phys_ascent = pcm->ascent + boff;
25817 it->phys_descent = pcm->descent - boff;
25818 it->pixel_width = pcm->width;
25820 else
25822 it->glyph_not_available_p = 1;
25823 it->phys_ascent = it->ascent;
25824 it->phys_descent = it->descent;
25825 it->pixel_width = font->space_width;
25828 if (it->constrain_row_ascent_descent_p)
25830 if (it->descent > it->max_descent)
25832 it->ascent += it->descent - it->max_descent;
25833 it->descent = it->max_descent;
25835 if (it->ascent > it->max_ascent)
25837 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
25838 it->ascent = it->max_ascent;
25840 it->phys_ascent = min (it->phys_ascent, it->ascent);
25841 it->phys_descent = min (it->phys_descent, it->descent);
25842 extra_line_spacing = 0;
25845 /* If this is a space inside a region of text with
25846 `space-width' property, change its width. */
25847 stretched_p = it->char_to_display == ' ' && !NILP (it->space_width);
25848 if (stretched_p)
25849 it->pixel_width *= XFLOATINT (it->space_width);
25851 /* If face has a box, add the box thickness to the character
25852 height. If character has a box line to the left and/or
25853 right, add the box line width to the character's width. */
25854 if (face->box != FACE_NO_BOX)
25856 int thick = face->box_line_width;
25858 if (thick > 0)
25860 it->ascent += thick;
25861 it->descent += thick;
25863 else
25864 thick = -thick;
25866 if (it->start_of_box_run_p)
25867 it->pixel_width += thick;
25868 if (it->end_of_box_run_p)
25869 it->pixel_width += thick;
25872 /* If face has an overline, add the height of the overline
25873 (1 pixel) and a 1 pixel margin to the character height. */
25874 if (face->overline_p)
25875 it->ascent += overline_margin;
25877 if (it->constrain_row_ascent_descent_p)
25879 if (it->ascent > it->max_ascent)
25880 it->ascent = it->max_ascent;
25881 if (it->descent > it->max_descent)
25882 it->descent = it->max_descent;
25885 take_vertical_position_into_account (it);
25887 /* If we have to actually produce glyphs, do it. */
25888 if (it->glyph_row)
25890 if (stretched_p)
25892 /* Translate a space with a `space-width' property
25893 into a stretch glyph. */
25894 int ascent = (((it->ascent + it->descent) * FONT_BASE (font))
25895 / FONT_HEIGHT (font));
25896 append_stretch_glyph (it, it->object, it->pixel_width,
25897 it->ascent + it->descent, ascent);
25899 else
25900 append_glyph (it);
25902 /* If characters with lbearing or rbearing are displayed
25903 in this line, record that fact in a flag of the
25904 glyph row. This is used to optimize X output code. */
25905 if (pcm && (pcm->lbearing < 0 || pcm->rbearing > pcm->width))
25906 it->glyph_row->contains_overlapping_glyphs_p = 1;
25908 if (! stretched_p && it->pixel_width == 0)
25909 /* We assure that all visible glyphs have at least 1-pixel
25910 width. */
25911 it->pixel_width = 1;
25913 else if (it->char_to_display == '\n')
25915 /* A newline has no width, but we need the height of the
25916 line. But if previous part of the line sets a height,
25917 don't increase that height. */
25919 Lisp_Object height;
25920 Lisp_Object total_height = Qnil;
25922 it->override_ascent = -1;
25923 it->pixel_width = 0;
25924 it->nglyphs = 0;
25926 height = get_it_property (it, Qline_height);
25927 /* Split (line-height total-height) list. */
25928 if (CONSP (height)
25929 && CONSP (XCDR (height))
25930 && NILP (XCDR (XCDR (height))))
25932 total_height = XCAR (XCDR (height));
25933 height = XCAR (height);
25935 height = calc_line_height_property (it, height, font, boff, 1);
25937 if (it->override_ascent >= 0)
25939 it->ascent = it->override_ascent;
25940 it->descent = it->override_descent;
25941 boff = it->override_boff;
25943 else
25945 it->ascent = FONT_BASE (font) + boff;
25946 it->descent = FONT_DESCENT (font) - boff;
25949 if (EQ (height, Qt))
25951 if (it->descent > it->max_descent)
25953 it->ascent += it->descent - it->max_descent;
25954 it->descent = it->max_descent;
25956 if (it->ascent > it->max_ascent)
25958 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
25959 it->ascent = it->max_ascent;
25961 it->phys_ascent = min (it->phys_ascent, it->ascent);
25962 it->phys_descent = min (it->phys_descent, it->descent);
25963 it->constrain_row_ascent_descent_p = 1;
25964 extra_line_spacing = 0;
25966 else
25968 Lisp_Object spacing;
25970 it->phys_ascent = it->ascent;
25971 it->phys_descent = it->descent;
25973 if ((it->max_ascent > 0 || it->max_descent > 0)
25974 && face->box != FACE_NO_BOX
25975 && face->box_line_width > 0)
25977 it->ascent += face->box_line_width;
25978 it->descent += face->box_line_width;
25980 if (!NILP (height)
25981 && XINT (height) > it->ascent + it->descent)
25982 it->ascent = XINT (height) - it->descent;
25984 if (!NILP (total_height))
25985 spacing = calc_line_height_property (it, total_height, font, boff, 0);
25986 else
25988 spacing = get_it_property (it, Qline_spacing);
25989 spacing = calc_line_height_property (it, spacing, font, boff, 0);
25991 if (INTEGERP (spacing))
25993 extra_line_spacing = XINT (spacing);
25994 if (!NILP (total_height))
25995 extra_line_spacing -= (it->phys_ascent + it->phys_descent);
25999 else /* i.e. (it->char_to_display == '\t') */
26001 if (font->space_width > 0)
26003 int tab_width = it->tab_width * font->space_width;
26004 int x = it->current_x + it->continuation_lines_width;
26005 int next_tab_x = ((1 + x + tab_width - 1) / tab_width) * tab_width;
26007 /* If the distance from the current position to the next tab
26008 stop is less than a space character width, use the
26009 tab stop after that. */
26010 if (next_tab_x - x < font->space_width)
26011 next_tab_x += tab_width;
26013 it->pixel_width = next_tab_x - x;
26014 it->nglyphs = 1;
26015 it->ascent = it->phys_ascent = FONT_BASE (font) + boff;
26016 it->descent = it->phys_descent = FONT_DESCENT (font) - boff;
26018 if (it->glyph_row)
26020 append_stretch_glyph (it, it->object, it->pixel_width,
26021 it->ascent + it->descent, it->ascent);
26024 else
26026 it->pixel_width = 0;
26027 it->nglyphs = 1;
26031 else if (it->what == IT_COMPOSITION && it->cmp_it.ch < 0)
26033 /* A static composition.
26035 Note: A composition is represented as one glyph in the
26036 glyph matrix. There are no padding glyphs.
26038 Important note: pixel_width, ascent, and descent are the
26039 values of what is drawn by draw_glyphs (i.e. the values of
26040 the overall glyphs composed). */
26041 struct face *face = FACE_FROM_ID (it->f, it->face_id);
26042 int boff; /* baseline offset */
26043 struct composition *cmp = composition_table[it->cmp_it.id];
26044 int glyph_len = cmp->glyph_len;
26045 struct font *font = face->font;
26047 it->nglyphs = 1;
26049 /* If we have not yet calculated pixel size data of glyphs of
26050 the composition for the current face font, calculate them
26051 now. Theoretically, we have to check all fonts for the
26052 glyphs, but that requires much time and memory space. So,
26053 here we check only the font of the first glyph. This may
26054 lead to incorrect display, but it's very rare, and C-l
26055 (recenter-top-bottom) can correct the display anyway. */
26056 if (! cmp->font || cmp->font != font)
26058 /* Ascent and descent of the font of the first character
26059 of this composition (adjusted by baseline offset).
26060 Ascent and descent of overall glyphs should not be less
26061 than these, respectively. */
26062 int font_ascent, font_descent, font_height;
26063 /* Bounding box of the overall glyphs. */
26064 int leftmost, rightmost, lowest, highest;
26065 int lbearing, rbearing;
26066 int i, width, ascent, descent;
26067 int left_padded = 0, right_padded = 0;
26068 int c IF_LINT (= 0); /* cmp->glyph_len can't be zero; see Bug#8512 */
26069 XChar2b char2b;
26070 struct font_metrics *pcm;
26071 int font_not_found_p;
26072 ptrdiff_t pos;
26074 for (glyph_len = cmp->glyph_len; glyph_len > 0; glyph_len--)
26075 if ((c = COMPOSITION_GLYPH (cmp, glyph_len - 1)) != '\t')
26076 break;
26077 if (glyph_len < cmp->glyph_len)
26078 right_padded = 1;
26079 for (i = 0; i < glyph_len; i++)
26081 if ((c = COMPOSITION_GLYPH (cmp, i)) != '\t')
26082 break;
26083 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
26085 if (i > 0)
26086 left_padded = 1;
26088 pos = (STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
26089 : IT_CHARPOS (*it));
26090 /* If no suitable font is found, use the default font. */
26091 font_not_found_p = font == NULL;
26092 if (font_not_found_p)
26094 face = face->ascii_face;
26095 font = face->font;
26097 boff = font->baseline_offset;
26098 if (font->vertical_centering)
26099 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
26100 font_ascent = FONT_BASE (font) + boff;
26101 font_descent = FONT_DESCENT (font) - boff;
26102 font_height = FONT_HEIGHT (font);
26104 cmp->font = font;
26106 pcm = NULL;
26107 if (! font_not_found_p)
26109 get_char_face_and_encoding (it->f, c, it->face_id,
26110 &char2b, 0);
26111 pcm = get_per_char_metric (font, &char2b);
26114 /* Initialize the bounding box. */
26115 if (pcm)
26117 width = cmp->glyph_len > 0 ? pcm->width : 0;
26118 ascent = pcm->ascent;
26119 descent = pcm->descent;
26120 lbearing = pcm->lbearing;
26121 rbearing = pcm->rbearing;
26123 else
26125 width = cmp->glyph_len > 0 ? font->space_width : 0;
26126 ascent = FONT_BASE (font);
26127 descent = FONT_DESCENT (font);
26128 lbearing = 0;
26129 rbearing = width;
26132 rightmost = width;
26133 leftmost = 0;
26134 lowest = - descent + boff;
26135 highest = ascent + boff;
26137 if (! font_not_found_p
26138 && font->default_ascent
26139 && CHAR_TABLE_P (Vuse_default_ascent)
26140 && !NILP (Faref (Vuse_default_ascent,
26141 make_number (it->char_to_display))))
26142 highest = font->default_ascent + boff;
26144 /* Draw the first glyph at the normal position. It may be
26145 shifted to right later if some other glyphs are drawn
26146 at the left. */
26147 cmp->offsets[i * 2] = 0;
26148 cmp->offsets[i * 2 + 1] = boff;
26149 cmp->lbearing = lbearing;
26150 cmp->rbearing = rbearing;
26152 /* Set cmp->offsets for the remaining glyphs. */
26153 for (i++; i < glyph_len; i++)
26155 int left, right, btm, top;
26156 int ch = COMPOSITION_GLYPH (cmp, i);
26157 int face_id;
26158 struct face *this_face;
26160 if (ch == '\t')
26161 ch = ' ';
26162 face_id = FACE_FOR_CHAR (it->f, face, ch, pos, it->string);
26163 this_face = FACE_FROM_ID (it->f, face_id);
26164 font = this_face->font;
26166 if (font == NULL)
26167 pcm = NULL;
26168 else
26170 get_char_face_and_encoding (it->f, ch, face_id,
26171 &char2b, 0);
26172 pcm = get_per_char_metric (font, &char2b);
26174 if (! pcm)
26175 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
26176 else
26178 width = pcm->width;
26179 ascent = pcm->ascent;
26180 descent = pcm->descent;
26181 lbearing = pcm->lbearing;
26182 rbearing = pcm->rbearing;
26183 if (cmp->method != COMPOSITION_WITH_RULE_ALTCHARS)
26185 /* Relative composition with or without
26186 alternate chars. */
26187 left = (leftmost + rightmost - width) / 2;
26188 btm = - descent + boff;
26189 if (font->relative_compose
26190 && (! CHAR_TABLE_P (Vignore_relative_composition)
26191 || NILP (Faref (Vignore_relative_composition,
26192 make_number (ch)))))
26195 if (- descent >= font->relative_compose)
26196 /* One extra pixel between two glyphs. */
26197 btm = highest + 1;
26198 else if (ascent <= 0)
26199 /* One extra pixel between two glyphs. */
26200 btm = lowest - 1 - ascent - descent;
26203 else
26205 /* A composition rule is specified by an integer
26206 value that encodes global and new reference
26207 points (GREF and NREF). GREF and NREF are
26208 specified by numbers as below:
26210 0---1---2 -- ascent
26214 9--10--11 -- center
26216 ---3---4---5--- baseline
26218 6---7---8 -- descent
26220 int rule = COMPOSITION_RULE (cmp, i);
26221 int gref, nref, grefx, grefy, nrefx, nrefy, xoff, yoff;
26223 COMPOSITION_DECODE_RULE (rule, gref, nref, xoff, yoff);
26224 grefx = gref % 3, nrefx = nref % 3;
26225 grefy = gref / 3, nrefy = nref / 3;
26226 if (xoff)
26227 xoff = font_height * (xoff - 128) / 256;
26228 if (yoff)
26229 yoff = font_height * (yoff - 128) / 256;
26231 left = (leftmost
26232 + grefx * (rightmost - leftmost) / 2
26233 - nrefx * width / 2
26234 + xoff);
26236 btm = ((grefy == 0 ? highest
26237 : grefy == 1 ? 0
26238 : grefy == 2 ? lowest
26239 : (highest + lowest) / 2)
26240 - (nrefy == 0 ? ascent + descent
26241 : nrefy == 1 ? descent - boff
26242 : nrefy == 2 ? 0
26243 : (ascent + descent) / 2)
26244 + yoff);
26247 cmp->offsets[i * 2] = left;
26248 cmp->offsets[i * 2 + 1] = btm + descent;
26250 /* Update the bounding box of the overall glyphs. */
26251 if (width > 0)
26253 right = left + width;
26254 if (left < leftmost)
26255 leftmost = left;
26256 if (right > rightmost)
26257 rightmost = right;
26259 top = btm + descent + ascent;
26260 if (top > highest)
26261 highest = top;
26262 if (btm < lowest)
26263 lowest = btm;
26265 if (cmp->lbearing > left + lbearing)
26266 cmp->lbearing = left + lbearing;
26267 if (cmp->rbearing < left + rbearing)
26268 cmp->rbearing = left + rbearing;
26272 /* If there are glyphs whose x-offsets are negative,
26273 shift all glyphs to the right and make all x-offsets
26274 non-negative. */
26275 if (leftmost < 0)
26277 for (i = 0; i < cmp->glyph_len; i++)
26278 cmp->offsets[i * 2] -= leftmost;
26279 rightmost -= leftmost;
26280 cmp->lbearing -= leftmost;
26281 cmp->rbearing -= leftmost;
26284 if (left_padded && cmp->lbearing < 0)
26286 for (i = 0; i < cmp->glyph_len; i++)
26287 cmp->offsets[i * 2] -= cmp->lbearing;
26288 rightmost -= cmp->lbearing;
26289 cmp->rbearing -= cmp->lbearing;
26290 cmp->lbearing = 0;
26292 if (right_padded && rightmost < cmp->rbearing)
26294 rightmost = cmp->rbearing;
26297 cmp->pixel_width = rightmost;
26298 cmp->ascent = highest;
26299 cmp->descent = - lowest;
26300 if (cmp->ascent < font_ascent)
26301 cmp->ascent = font_ascent;
26302 if (cmp->descent < font_descent)
26303 cmp->descent = font_descent;
26306 if (it->glyph_row
26307 && (cmp->lbearing < 0
26308 || cmp->rbearing > cmp->pixel_width))
26309 it->glyph_row->contains_overlapping_glyphs_p = 1;
26311 it->pixel_width = cmp->pixel_width;
26312 it->ascent = it->phys_ascent = cmp->ascent;
26313 it->descent = it->phys_descent = cmp->descent;
26314 if (face->box != FACE_NO_BOX)
26316 int thick = face->box_line_width;
26318 if (thick > 0)
26320 it->ascent += thick;
26321 it->descent += thick;
26323 else
26324 thick = - thick;
26326 if (it->start_of_box_run_p)
26327 it->pixel_width += thick;
26328 if (it->end_of_box_run_p)
26329 it->pixel_width += thick;
26332 /* If face has an overline, add the height of the overline
26333 (1 pixel) and a 1 pixel margin to the character height. */
26334 if (face->overline_p)
26335 it->ascent += overline_margin;
26337 take_vertical_position_into_account (it);
26338 if (it->ascent < 0)
26339 it->ascent = 0;
26340 if (it->descent < 0)
26341 it->descent = 0;
26343 if (it->glyph_row && cmp->glyph_len > 0)
26344 append_composite_glyph (it);
26346 else if (it->what == IT_COMPOSITION)
26348 /* A dynamic (automatic) composition. */
26349 struct face *face = FACE_FROM_ID (it->f, it->face_id);
26350 Lisp_Object gstring;
26351 struct font_metrics metrics;
26353 it->nglyphs = 1;
26355 gstring = composition_gstring_from_id (it->cmp_it.id);
26356 it->pixel_width
26357 = composition_gstring_width (gstring, it->cmp_it.from, it->cmp_it.to,
26358 &metrics);
26359 if (it->glyph_row
26360 && (metrics.lbearing < 0 || metrics.rbearing > metrics.width))
26361 it->glyph_row->contains_overlapping_glyphs_p = 1;
26362 it->ascent = it->phys_ascent = metrics.ascent;
26363 it->descent = it->phys_descent = metrics.descent;
26364 if (face->box != FACE_NO_BOX)
26366 int thick = face->box_line_width;
26368 if (thick > 0)
26370 it->ascent += thick;
26371 it->descent += thick;
26373 else
26374 thick = - thick;
26376 if (it->start_of_box_run_p)
26377 it->pixel_width += thick;
26378 if (it->end_of_box_run_p)
26379 it->pixel_width += thick;
26381 /* If face has an overline, add the height of the overline
26382 (1 pixel) and a 1 pixel margin to the character height. */
26383 if (face->overline_p)
26384 it->ascent += overline_margin;
26385 take_vertical_position_into_account (it);
26386 if (it->ascent < 0)
26387 it->ascent = 0;
26388 if (it->descent < 0)
26389 it->descent = 0;
26391 if (it->glyph_row)
26392 append_composite_glyph (it);
26394 else if (it->what == IT_GLYPHLESS)
26395 produce_glyphless_glyph (it, 0, Qnil);
26396 else if (it->what == IT_IMAGE)
26397 produce_image_glyph (it);
26398 else if (it->what == IT_STRETCH)
26399 produce_stretch_glyph (it);
26401 done:
26402 /* Accumulate dimensions. Note: can't assume that it->descent > 0
26403 because this isn't true for images with `:ascent 100'. */
26404 eassert (it->ascent >= 0 && it->descent >= 0);
26405 if (it->area == TEXT_AREA)
26406 it->current_x += it->pixel_width;
26408 if (extra_line_spacing > 0)
26410 it->descent += extra_line_spacing;
26411 if (extra_line_spacing > it->max_extra_line_spacing)
26412 it->max_extra_line_spacing = extra_line_spacing;
26415 it->max_ascent = max (it->max_ascent, it->ascent);
26416 it->max_descent = max (it->max_descent, it->descent);
26417 it->max_phys_ascent = max (it->max_phys_ascent, it->phys_ascent);
26418 it->max_phys_descent = max (it->max_phys_descent, it->phys_descent);
26421 /* EXPORT for RIF:
26422 Output LEN glyphs starting at START at the nominal cursor position.
26423 Advance the nominal cursor over the text. UPDATED_ROW is the glyph row
26424 being updated, and UPDATED_AREA is the area of that row being updated. */
26426 void
26427 x_write_glyphs (struct window *w, struct glyph_row *updated_row,
26428 struct glyph *start, enum glyph_row_area updated_area, int len)
26430 int x, hpos, chpos = w->phys_cursor.hpos;
26432 eassert (updated_row);
26433 /* When the window is hscrolled, cursor hpos can legitimately be out
26434 of bounds, but we draw the cursor at the corresponding window
26435 margin in that case. */
26436 if (!updated_row->reversed_p && chpos < 0)
26437 chpos = 0;
26438 if (updated_row->reversed_p && chpos >= updated_row->used[TEXT_AREA])
26439 chpos = updated_row->used[TEXT_AREA] - 1;
26441 block_input ();
26443 /* Write glyphs. */
26445 hpos = start - updated_row->glyphs[updated_area];
26446 x = draw_glyphs (w, w->output_cursor.x,
26447 updated_row, updated_area,
26448 hpos, hpos + len,
26449 DRAW_NORMAL_TEXT, 0);
26451 /* Invalidate old phys cursor if the glyph at its hpos is redrawn. */
26452 if (updated_area == TEXT_AREA
26453 && w->phys_cursor_on_p
26454 && w->phys_cursor.vpos == w->output_cursor.vpos
26455 && chpos >= hpos
26456 && chpos < hpos + len)
26457 w->phys_cursor_on_p = 0;
26459 unblock_input ();
26461 /* Advance the output cursor. */
26462 w->output_cursor.hpos += len;
26463 w->output_cursor.x = x;
26467 /* EXPORT for RIF:
26468 Insert LEN glyphs from START at the nominal cursor position. */
26470 void
26471 x_insert_glyphs (struct window *w, struct glyph_row *updated_row,
26472 struct glyph *start, enum glyph_row_area updated_area, int len)
26474 struct frame *f;
26475 int line_height, shift_by_width, shifted_region_width;
26476 struct glyph_row *row;
26477 struct glyph *glyph;
26478 int frame_x, frame_y;
26479 ptrdiff_t hpos;
26481 eassert (updated_row);
26482 block_input ();
26483 f = XFRAME (WINDOW_FRAME (w));
26485 /* Get the height of the line we are in. */
26486 row = updated_row;
26487 line_height = row->height;
26489 /* Get the width of the glyphs to insert. */
26490 shift_by_width = 0;
26491 for (glyph = start; glyph < start + len; ++glyph)
26492 shift_by_width += glyph->pixel_width;
26494 /* Get the width of the region to shift right. */
26495 shifted_region_width = (window_box_width (w, updated_area)
26496 - w->output_cursor.x
26497 - shift_by_width);
26499 /* Shift right. */
26500 frame_x = window_box_left (w, updated_area) + w->output_cursor.x;
26501 frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, w->output_cursor.y);
26503 FRAME_RIF (f)->shift_glyphs_for_insert (f, frame_x, frame_y, shifted_region_width,
26504 line_height, shift_by_width);
26506 /* Write the glyphs. */
26507 hpos = start - row->glyphs[updated_area];
26508 draw_glyphs (w, w->output_cursor.x, row, updated_area,
26509 hpos, hpos + len,
26510 DRAW_NORMAL_TEXT, 0);
26512 /* Advance the output cursor. */
26513 w->output_cursor.hpos += len;
26514 w->output_cursor.x += shift_by_width;
26515 unblock_input ();
26519 /* EXPORT for RIF:
26520 Erase the current text line from the nominal cursor position
26521 (inclusive) to pixel column TO_X (exclusive). The idea is that
26522 everything from TO_X onward is already erased.
26524 TO_X is a pixel position relative to UPDATED_AREA of currently
26525 updated window W. TO_X == -1 means clear to the end of this area. */
26527 void
26528 x_clear_end_of_line (struct window *w, struct glyph_row *updated_row,
26529 enum glyph_row_area updated_area, int to_x)
26531 struct frame *f;
26532 int max_x, min_y, max_y;
26533 int from_x, from_y, to_y;
26535 eassert (updated_row);
26536 f = XFRAME (w->frame);
26538 if (updated_row->full_width_p)
26539 max_x = (WINDOW_PIXEL_WIDTH (w)
26540 - (updated_row->mode_line_p ? WINDOW_RIGHT_DIVIDER_WIDTH (w) : 0));
26541 else
26542 max_x = window_box_width (w, updated_area);
26543 max_y = window_text_bottom_y (w);
26545 /* TO_X == 0 means don't do anything. TO_X < 0 means clear to end
26546 of window. For TO_X > 0, truncate to end of drawing area. */
26547 if (to_x == 0)
26548 return;
26549 else if (to_x < 0)
26550 to_x = max_x;
26551 else
26552 to_x = min (to_x, max_x);
26554 to_y = min (max_y, w->output_cursor.y + updated_row->height);
26556 /* Notice if the cursor will be cleared by this operation. */
26557 if (!updated_row->full_width_p)
26558 notice_overwritten_cursor (w, updated_area,
26559 w->output_cursor.x, -1,
26560 updated_row->y,
26561 MATRIX_ROW_BOTTOM_Y (updated_row));
26563 from_x = w->output_cursor.x;
26565 /* Translate to frame coordinates. */
26566 if (updated_row->full_width_p)
26568 from_x = WINDOW_TO_FRAME_PIXEL_X (w, from_x);
26569 to_x = WINDOW_TO_FRAME_PIXEL_X (w, to_x);
26571 else
26573 int area_left = window_box_left (w, updated_area);
26574 from_x += area_left;
26575 to_x += area_left;
26578 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
26579 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, max (min_y, w->output_cursor.y));
26580 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, to_y);
26582 /* Prevent inadvertently clearing to end of the X window. */
26583 if (to_x > from_x && to_y > from_y)
26585 block_input ();
26586 FRAME_RIF (f)->clear_frame_area (f, from_x, from_y,
26587 to_x - from_x, to_y - from_y);
26588 unblock_input ();
26592 #endif /* HAVE_WINDOW_SYSTEM */
26596 /***********************************************************************
26597 Cursor types
26598 ***********************************************************************/
26600 /* Value is the internal representation of the specified cursor type
26601 ARG. If type is BAR_CURSOR, return in *WIDTH the specified width
26602 of the bar cursor. */
26604 static enum text_cursor_kinds
26605 get_specified_cursor_type (Lisp_Object arg, int *width)
26607 enum text_cursor_kinds type;
26609 if (NILP (arg))
26610 return NO_CURSOR;
26612 if (EQ (arg, Qbox))
26613 return FILLED_BOX_CURSOR;
26615 if (EQ (arg, Qhollow))
26616 return HOLLOW_BOX_CURSOR;
26618 if (EQ (arg, Qbar))
26620 *width = 2;
26621 return BAR_CURSOR;
26624 if (CONSP (arg)
26625 && EQ (XCAR (arg), Qbar)
26626 && RANGED_INTEGERP (0, XCDR (arg), INT_MAX))
26628 *width = XINT (XCDR (arg));
26629 return BAR_CURSOR;
26632 if (EQ (arg, Qhbar))
26634 *width = 2;
26635 return HBAR_CURSOR;
26638 if (CONSP (arg)
26639 && EQ (XCAR (arg), Qhbar)
26640 && RANGED_INTEGERP (0, XCDR (arg), INT_MAX))
26642 *width = XINT (XCDR (arg));
26643 return HBAR_CURSOR;
26646 /* Treat anything unknown as "hollow box cursor".
26647 It was bad to signal an error; people have trouble fixing
26648 .Xdefaults with Emacs, when it has something bad in it. */
26649 type = HOLLOW_BOX_CURSOR;
26651 return type;
26654 /* Set the default cursor types for specified frame. */
26655 void
26656 set_frame_cursor_types (struct frame *f, Lisp_Object arg)
26658 int width = 1;
26659 Lisp_Object tem;
26661 FRAME_DESIRED_CURSOR (f) = get_specified_cursor_type (arg, &width);
26662 FRAME_CURSOR_WIDTH (f) = width;
26664 /* By default, set up the blink-off state depending on the on-state. */
26666 tem = Fassoc (arg, Vblink_cursor_alist);
26667 if (!NILP (tem))
26669 FRAME_BLINK_OFF_CURSOR (f)
26670 = get_specified_cursor_type (XCDR (tem), &width);
26671 FRAME_BLINK_OFF_CURSOR_WIDTH (f) = width;
26673 else
26674 FRAME_BLINK_OFF_CURSOR (f) = DEFAULT_CURSOR;
26676 /* Make sure the cursor gets redrawn. */
26677 f->cursor_type_changed = 1;
26681 #ifdef HAVE_WINDOW_SYSTEM
26683 /* Return the cursor we want to be displayed in window W. Return
26684 width of bar/hbar cursor through WIDTH arg. Return with
26685 ACTIVE_CURSOR arg set to 1 if cursor in window W is `active'
26686 (i.e. if the `system caret' should track this cursor).
26688 In a mini-buffer window, we want the cursor only to appear if we
26689 are reading input from this window. For the selected window, we
26690 want the cursor type given by the frame parameter or buffer local
26691 setting of cursor-type. If explicitly marked off, draw no cursor.
26692 In all other cases, we want a hollow box cursor. */
26694 static enum text_cursor_kinds
26695 get_window_cursor_type (struct window *w, struct glyph *glyph, int *width,
26696 int *active_cursor)
26698 struct frame *f = XFRAME (w->frame);
26699 struct buffer *b = XBUFFER (w->contents);
26700 int cursor_type = DEFAULT_CURSOR;
26701 Lisp_Object alt_cursor;
26702 int non_selected = 0;
26704 *active_cursor = 1;
26706 /* Echo area */
26707 if (cursor_in_echo_area
26708 && FRAME_HAS_MINIBUF_P (f)
26709 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
26711 if (w == XWINDOW (echo_area_window))
26713 if (EQ (BVAR (b, cursor_type), Qt) || NILP (BVAR (b, cursor_type)))
26715 *width = FRAME_CURSOR_WIDTH (f);
26716 return FRAME_DESIRED_CURSOR (f);
26718 else
26719 return get_specified_cursor_type (BVAR (b, cursor_type), width);
26722 *active_cursor = 0;
26723 non_selected = 1;
26726 /* Detect a nonselected window or nonselected frame. */
26727 else if (w != XWINDOW (f->selected_window)
26728 || f != FRAME_DISPLAY_INFO (f)->x_highlight_frame)
26730 *active_cursor = 0;
26732 if (MINI_WINDOW_P (w) && minibuf_level == 0)
26733 return NO_CURSOR;
26735 non_selected = 1;
26738 /* Never display a cursor in a window in which cursor-type is nil. */
26739 if (NILP (BVAR (b, cursor_type)))
26740 return NO_CURSOR;
26742 /* Get the normal cursor type for this window. */
26743 if (EQ (BVAR (b, cursor_type), Qt))
26745 cursor_type = FRAME_DESIRED_CURSOR (f);
26746 *width = FRAME_CURSOR_WIDTH (f);
26748 else
26749 cursor_type = get_specified_cursor_type (BVAR (b, cursor_type), width);
26751 /* Use cursor-in-non-selected-windows instead
26752 for non-selected window or frame. */
26753 if (non_selected)
26755 alt_cursor = BVAR (b, cursor_in_non_selected_windows);
26756 if (!EQ (Qt, alt_cursor))
26757 return get_specified_cursor_type (alt_cursor, width);
26758 /* t means modify the normal cursor type. */
26759 if (cursor_type == FILLED_BOX_CURSOR)
26760 cursor_type = HOLLOW_BOX_CURSOR;
26761 else if (cursor_type == BAR_CURSOR && *width > 1)
26762 --*width;
26763 return cursor_type;
26766 /* Use normal cursor if not blinked off. */
26767 if (!w->cursor_off_p)
26769 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
26771 if (cursor_type == FILLED_BOX_CURSOR)
26773 /* Using a block cursor on large images can be very annoying.
26774 So use a hollow cursor for "large" images.
26775 If image is not transparent (no mask), also use hollow cursor. */
26776 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
26777 if (img != NULL && IMAGEP (img->spec))
26779 /* Arbitrarily, interpret "Large" as >32x32 and >NxN
26780 where N = size of default frame font size.
26781 This should cover most of the "tiny" icons people may use. */
26782 if (!img->mask
26783 || img->width > max (32, WINDOW_FRAME_COLUMN_WIDTH (w))
26784 || img->height > max (32, WINDOW_FRAME_LINE_HEIGHT (w)))
26785 cursor_type = HOLLOW_BOX_CURSOR;
26788 else if (cursor_type != NO_CURSOR)
26790 /* Display current only supports BOX and HOLLOW cursors for images.
26791 So for now, unconditionally use a HOLLOW cursor when cursor is
26792 not a solid box cursor. */
26793 cursor_type = HOLLOW_BOX_CURSOR;
26796 return cursor_type;
26799 /* Cursor is blinked off, so determine how to "toggle" it. */
26801 /* First look for an entry matching the buffer's cursor-type in blink-cursor-alist. */
26802 if ((alt_cursor = Fassoc (BVAR (b, cursor_type), Vblink_cursor_alist), !NILP (alt_cursor)))
26803 return get_specified_cursor_type (XCDR (alt_cursor), width);
26805 /* Then see if frame has specified a specific blink off cursor type. */
26806 if (FRAME_BLINK_OFF_CURSOR (f) != DEFAULT_CURSOR)
26808 *width = FRAME_BLINK_OFF_CURSOR_WIDTH (f);
26809 return FRAME_BLINK_OFF_CURSOR (f);
26812 #if 0
26813 /* Some people liked having a permanently visible blinking cursor,
26814 while others had very strong opinions against it. So it was
26815 decided to remove it. KFS 2003-09-03 */
26817 /* Finally perform built-in cursor blinking:
26818 filled box <-> hollow box
26819 wide [h]bar <-> narrow [h]bar
26820 narrow [h]bar <-> no cursor
26821 other type <-> no cursor */
26823 if (cursor_type == FILLED_BOX_CURSOR)
26824 return HOLLOW_BOX_CURSOR;
26826 if ((cursor_type == BAR_CURSOR || cursor_type == HBAR_CURSOR) && *width > 1)
26828 *width = 1;
26829 return cursor_type;
26831 #endif
26833 return NO_CURSOR;
26837 /* Notice when the text cursor of window W has been completely
26838 overwritten by a drawing operation that outputs glyphs in AREA
26839 starting at X0 and ending at X1 in the line starting at Y0 and
26840 ending at Y1. X coordinates are area-relative. X1 < 0 means all
26841 the rest of the line after X0 has been written. Y coordinates
26842 are window-relative. */
26844 static void
26845 notice_overwritten_cursor (struct window *w, enum glyph_row_area area,
26846 int x0, int x1, int y0, int y1)
26848 int cx0, cx1, cy0, cy1;
26849 struct glyph_row *row;
26851 if (!w->phys_cursor_on_p)
26852 return;
26853 if (area != TEXT_AREA)
26854 return;
26856 if (w->phys_cursor.vpos < 0
26857 || w->phys_cursor.vpos >= w->current_matrix->nrows
26858 || (row = w->current_matrix->rows + w->phys_cursor.vpos,
26859 !(row->enabled_p && MATRIX_ROW_DISPLAYS_TEXT_P (row))))
26860 return;
26862 if (row->cursor_in_fringe_p)
26864 row->cursor_in_fringe_p = 0;
26865 draw_fringe_bitmap (w, row, row->reversed_p);
26866 w->phys_cursor_on_p = 0;
26867 return;
26870 cx0 = w->phys_cursor.x;
26871 cx1 = cx0 + w->phys_cursor_width;
26872 if (x0 > cx0 || (x1 >= 0 && x1 < cx1))
26873 return;
26875 /* The cursor image will be completely removed from the
26876 screen if the output area intersects the cursor area in
26877 y-direction. When we draw in [y0 y1[, and some part of
26878 the cursor is at y < y0, that part must have been drawn
26879 before. When scrolling, the cursor is erased before
26880 actually scrolling, so we don't come here. When not
26881 scrolling, the rows above the old cursor row must have
26882 changed, and in this case these rows must have written
26883 over the cursor image.
26885 Likewise if part of the cursor is below y1, with the
26886 exception of the cursor being in the first blank row at
26887 the buffer and window end because update_text_area
26888 doesn't draw that row. (Except when it does, but
26889 that's handled in update_text_area.) */
26891 cy0 = w->phys_cursor.y;
26892 cy1 = cy0 + w->phys_cursor_height;
26893 if ((y0 < cy0 || y0 >= cy1) && (y1 <= cy0 || y1 >= cy1))
26894 return;
26896 w->phys_cursor_on_p = 0;
26899 #endif /* HAVE_WINDOW_SYSTEM */
26902 /************************************************************************
26903 Mouse Face
26904 ************************************************************************/
26906 #ifdef HAVE_WINDOW_SYSTEM
26908 /* EXPORT for RIF:
26909 Fix the display of area AREA of overlapping row ROW in window W
26910 with respect to the overlapping part OVERLAPS. */
26912 void
26913 x_fix_overlapping_area (struct window *w, struct glyph_row *row,
26914 enum glyph_row_area area, int overlaps)
26916 int i, x;
26918 block_input ();
26920 x = 0;
26921 for (i = 0; i < row->used[area];)
26923 if (row->glyphs[area][i].overlaps_vertically_p)
26925 int start = i, start_x = x;
26929 x += row->glyphs[area][i].pixel_width;
26930 ++i;
26932 while (i < row->used[area]
26933 && row->glyphs[area][i].overlaps_vertically_p);
26935 draw_glyphs (w, start_x, row, area,
26936 start, i,
26937 DRAW_NORMAL_TEXT, overlaps);
26939 else
26941 x += row->glyphs[area][i].pixel_width;
26942 ++i;
26946 unblock_input ();
26950 /* EXPORT:
26951 Draw the cursor glyph of window W in glyph row ROW. See the
26952 comment of draw_glyphs for the meaning of HL. */
26954 void
26955 draw_phys_cursor_glyph (struct window *w, struct glyph_row *row,
26956 enum draw_glyphs_face hl)
26958 /* If cursor hpos is out of bounds, don't draw garbage. This can
26959 happen in mini-buffer windows when switching between echo area
26960 glyphs and mini-buffer. */
26961 if ((row->reversed_p
26962 ? (w->phys_cursor.hpos >= 0)
26963 : (w->phys_cursor.hpos < row->used[TEXT_AREA])))
26965 int on_p = w->phys_cursor_on_p;
26966 int x1;
26967 int hpos = w->phys_cursor.hpos;
26969 /* When the window is hscrolled, cursor hpos can legitimately be
26970 out of bounds, but we draw the cursor at the corresponding
26971 window margin in that case. */
26972 if (!row->reversed_p && hpos < 0)
26973 hpos = 0;
26974 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
26975 hpos = row->used[TEXT_AREA] - 1;
26977 x1 = draw_glyphs (w, w->phys_cursor.x, row, TEXT_AREA, hpos, hpos + 1,
26978 hl, 0);
26979 w->phys_cursor_on_p = on_p;
26981 if (hl == DRAW_CURSOR)
26982 w->phys_cursor_width = x1 - w->phys_cursor.x;
26983 /* When we erase the cursor, and ROW is overlapped by other
26984 rows, make sure that these overlapping parts of other rows
26985 are redrawn. */
26986 else if (hl == DRAW_NORMAL_TEXT && row->overlapped_p)
26988 w->phys_cursor_width = x1 - w->phys_cursor.x;
26990 if (row > w->current_matrix->rows
26991 && MATRIX_ROW_OVERLAPS_SUCC_P (row - 1))
26992 x_fix_overlapping_area (w, row - 1, TEXT_AREA,
26993 OVERLAPS_ERASED_CURSOR);
26995 if (MATRIX_ROW_BOTTOM_Y (row) < window_text_bottom_y (w)
26996 && MATRIX_ROW_OVERLAPS_PRED_P (row + 1))
26997 x_fix_overlapping_area (w, row + 1, TEXT_AREA,
26998 OVERLAPS_ERASED_CURSOR);
27004 /* Erase the image of a cursor of window W from the screen. */
27006 #ifndef HAVE_NTGUI
27007 static
27008 #endif
27009 void
27010 erase_phys_cursor (struct window *w)
27012 struct frame *f = XFRAME (w->frame);
27013 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
27014 int hpos = w->phys_cursor.hpos;
27015 int vpos = w->phys_cursor.vpos;
27016 int mouse_face_here_p = 0;
27017 struct glyph_matrix *active_glyphs = w->current_matrix;
27018 struct glyph_row *cursor_row;
27019 struct glyph *cursor_glyph;
27020 enum draw_glyphs_face hl;
27022 /* No cursor displayed or row invalidated => nothing to do on the
27023 screen. */
27024 if (w->phys_cursor_type == NO_CURSOR)
27025 goto mark_cursor_off;
27027 /* VPOS >= active_glyphs->nrows means that window has been resized.
27028 Don't bother to erase the cursor. */
27029 if (vpos >= active_glyphs->nrows)
27030 goto mark_cursor_off;
27032 /* If row containing cursor is marked invalid, there is nothing we
27033 can do. */
27034 cursor_row = MATRIX_ROW (active_glyphs, vpos);
27035 if (!cursor_row->enabled_p)
27036 goto mark_cursor_off;
27038 /* If line spacing is > 0, old cursor may only be partially visible in
27039 window after split-window. So adjust visible height. */
27040 cursor_row->visible_height = min (cursor_row->visible_height,
27041 window_text_bottom_y (w) - cursor_row->y);
27043 /* If row is completely invisible, don't attempt to delete a cursor which
27044 isn't there. This can happen if cursor is at top of a window, and
27045 we switch to a buffer with a header line in that window. */
27046 if (cursor_row->visible_height <= 0)
27047 goto mark_cursor_off;
27049 /* If cursor is in the fringe, erase by drawing actual bitmap there. */
27050 if (cursor_row->cursor_in_fringe_p)
27052 cursor_row->cursor_in_fringe_p = 0;
27053 draw_fringe_bitmap (w, cursor_row, cursor_row->reversed_p);
27054 goto mark_cursor_off;
27057 /* This can happen when the new row is shorter than the old one.
27058 In this case, either draw_glyphs or clear_end_of_line
27059 should have cleared the cursor. Note that we wouldn't be
27060 able to erase the cursor in this case because we don't have a
27061 cursor glyph at hand. */
27062 if ((cursor_row->reversed_p
27063 ? (w->phys_cursor.hpos < 0)
27064 : (w->phys_cursor.hpos >= cursor_row->used[TEXT_AREA])))
27065 goto mark_cursor_off;
27067 /* When the window is hscrolled, cursor hpos can legitimately be out
27068 of bounds, but we draw the cursor at the corresponding window
27069 margin in that case. */
27070 if (!cursor_row->reversed_p && hpos < 0)
27071 hpos = 0;
27072 if (cursor_row->reversed_p && hpos >= cursor_row->used[TEXT_AREA])
27073 hpos = cursor_row->used[TEXT_AREA] - 1;
27075 /* If the cursor is in the mouse face area, redisplay that when
27076 we clear the cursor. */
27077 if (! NILP (hlinfo->mouse_face_window)
27078 && coords_in_mouse_face_p (w, hpos, vpos)
27079 /* Don't redraw the cursor's spot in mouse face if it is at the
27080 end of a line (on a newline). The cursor appears there, but
27081 mouse highlighting does not. */
27082 && cursor_row->used[TEXT_AREA] > hpos && hpos >= 0)
27083 mouse_face_here_p = 1;
27085 /* Maybe clear the display under the cursor. */
27086 if (w->phys_cursor_type == HOLLOW_BOX_CURSOR)
27088 int x, y, left_x;
27089 int header_line_height = WINDOW_HEADER_LINE_HEIGHT (w);
27090 int width;
27092 cursor_glyph = get_phys_cursor_glyph (w);
27093 if (cursor_glyph == NULL)
27094 goto mark_cursor_off;
27096 width = cursor_glyph->pixel_width;
27097 left_x = window_box_left_offset (w, TEXT_AREA);
27098 x = w->phys_cursor.x;
27099 if (x < left_x)
27100 width -= left_x - x;
27101 width = min (width, window_box_width (w, TEXT_AREA) - x);
27102 y = WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height, cursor_row->y));
27103 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, max (x, left_x));
27105 if (width > 0)
27106 FRAME_RIF (f)->clear_frame_area (f, x, y, width, cursor_row->visible_height);
27109 /* Erase the cursor by redrawing the character underneath it. */
27110 if (mouse_face_here_p)
27111 hl = DRAW_MOUSE_FACE;
27112 else
27113 hl = DRAW_NORMAL_TEXT;
27114 draw_phys_cursor_glyph (w, cursor_row, hl);
27116 mark_cursor_off:
27117 w->phys_cursor_on_p = 0;
27118 w->phys_cursor_type = NO_CURSOR;
27122 /* EXPORT:
27123 Display or clear cursor of window W. If ON is zero, clear the
27124 cursor. If it is non-zero, display the cursor. If ON is nonzero,
27125 where to put the cursor is specified by HPOS, VPOS, X and Y. */
27127 void
27128 display_and_set_cursor (struct window *w, bool on,
27129 int hpos, int vpos, int x, int y)
27131 struct frame *f = XFRAME (w->frame);
27132 int new_cursor_type;
27133 int new_cursor_width;
27134 int active_cursor;
27135 struct glyph_row *glyph_row;
27136 struct glyph *glyph;
27138 /* This is pointless on invisible frames, and dangerous on garbaged
27139 windows and frames; in the latter case, the frame or window may
27140 be in the midst of changing its size, and x and y may be off the
27141 window. */
27142 if (! FRAME_VISIBLE_P (f)
27143 || FRAME_GARBAGED_P (f)
27144 || vpos >= w->current_matrix->nrows
27145 || hpos >= w->current_matrix->matrix_w)
27146 return;
27148 /* If cursor is off and we want it off, return quickly. */
27149 if (!on && !w->phys_cursor_on_p)
27150 return;
27152 glyph_row = MATRIX_ROW (w->current_matrix, vpos);
27153 /* If cursor row is not enabled, we don't really know where to
27154 display the cursor. */
27155 if (!glyph_row->enabled_p)
27157 w->phys_cursor_on_p = 0;
27158 return;
27161 glyph = NULL;
27162 if (!glyph_row->exact_window_width_line_p
27163 || (0 <= hpos && hpos < glyph_row->used[TEXT_AREA]))
27164 glyph = glyph_row->glyphs[TEXT_AREA] + hpos;
27166 eassert (input_blocked_p ());
27168 /* Set new_cursor_type to the cursor we want to be displayed. */
27169 new_cursor_type = get_window_cursor_type (w, glyph,
27170 &new_cursor_width, &active_cursor);
27172 /* If cursor is currently being shown and we don't want it to be or
27173 it is in the wrong place, or the cursor type is not what we want,
27174 erase it. */
27175 if (w->phys_cursor_on_p
27176 && (!on
27177 || w->phys_cursor.x != x
27178 || w->phys_cursor.y != y
27179 || new_cursor_type != w->phys_cursor_type
27180 || ((new_cursor_type == BAR_CURSOR || new_cursor_type == HBAR_CURSOR)
27181 && new_cursor_width != w->phys_cursor_width)))
27182 erase_phys_cursor (w);
27184 /* Don't check phys_cursor_on_p here because that flag is only set
27185 to zero in some cases where we know that the cursor has been
27186 completely erased, to avoid the extra work of erasing the cursor
27187 twice. In other words, phys_cursor_on_p can be 1 and the cursor
27188 still not be visible, or it has only been partly erased. */
27189 if (on)
27191 w->phys_cursor_ascent = glyph_row->ascent;
27192 w->phys_cursor_height = glyph_row->height;
27194 /* Set phys_cursor_.* before x_draw_.* is called because some
27195 of them may need the information. */
27196 w->phys_cursor.x = x;
27197 w->phys_cursor.y = glyph_row->y;
27198 w->phys_cursor.hpos = hpos;
27199 w->phys_cursor.vpos = vpos;
27202 FRAME_RIF (f)->draw_window_cursor (w, glyph_row, x, y,
27203 new_cursor_type, new_cursor_width,
27204 on, active_cursor);
27208 /* Switch the display of W's cursor on or off, according to the value
27209 of ON. */
27211 static void
27212 update_window_cursor (struct window *w, bool on)
27214 /* Don't update cursor in windows whose frame is in the process
27215 of being deleted. */
27216 if (w->current_matrix)
27218 int hpos = w->phys_cursor.hpos;
27219 int vpos = w->phys_cursor.vpos;
27220 struct glyph_row *row;
27222 if (vpos >= w->current_matrix->nrows
27223 || hpos >= w->current_matrix->matrix_w)
27224 return;
27226 row = MATRIX_ROW (w->current_matrix, vpos);
27228 /* When the window is hscrolled, cursor hpos can legitimately be
27229 out of bounds, but we draw the cursor at the corresponding
27230 window margin in that case. */
27231 if (!row->reversed_p && hpos < 0)
27232 hpos = 0;
27233 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
27234 hpos = row->used[TEXT_AREA] - 1;
27236 block_input ();
27237 display_and_set_cursor (w, on, hpos, vpos,
27238 w->phys_cursor.x, w->phys_cursor.y);
27239 unblock_input ();
27244 /* Call update_window_cursor with parameter ON_P on all leaf windows
27245 in the window tree rooted at W. */
27247 static void
27248 update_cursor_in_window_tree (struct window *w, bool on_p)
27250 while (w)
27252 if (WINDOWP (w->contents))
27253 update_cursor_in_window_tree (XWINDOW (w->contents), on_p);
27254 else
27255 update_window_cursor (w, on_p);
27257 w = NILP (w->next) ? 0 : XWINDOW (w->next);
27262 /* EXPORT:
27263 Display the cursor on window W, or clear it, according to ON_P.
27264 Don't change the cursor's position. */
27266 void
27267 x_update_cursor (struct frame *f, bool on_p)
27269 update_cursor_in_window_tree (XWINDOW (f->root_window), on_p);
27273 /* EXPORT:
27274 Clear the cursor of window W to background color, and mark the
27275 cursor as not shown. This is used when the text where the cursor
27276 is about to be rewritten. */
27278 void
27279 x_clear_cursor (struct window *w)
27281 if (FRAME_VISIBLE_P (XFRAME (w->frame)) && w->phys_cursor_on_p)
27282 update_window_cursor (w, 0);
27285 #endif /* HAVE_WINDOW_SYSTEM */
27287 /* Implementation of draw_row_with_mouse_face for GUI sessions, GPM,
27288 and MSDOS. */
27289 static void
27290 draw_row_with_mouse_face (struct window *w, int start_x, struct glyph_row *row,
27291 int start_hpos, int end_hpos,
27292 enum draw_glyphs_face draw)
27294 #ifdef HAVE_WINDOW_SYSTEM
27295 if (FRAME_WINDOW_P (XFRAME (w->frame)))
27297 draw_glyphs (w, start_x, row, TEXT_AREA, start_hpos, end_hpos, draw, 0);
27298 return;
27300 #endif
27301 #if defined (HAVE_GPM) || defined (MSDOS) || defined (WINDOWSNT)
27302 tty_draw_row_with_mouse_face (w, row, start_hpos, end_hpos, draw);
27303 #endif
27306 /* Display the active region described by mouse_face_* according to DRAW. */
27308 static void
27309 show_mouse_face (Mouse_HLInfo *hlinfo, enum draw_glyphs_face draw)
27311 struct window *w = XWINDOW (hlinfo->mouse_face_window);
27312 struct frame *f = XFRAME (WINDOW_FRAME (w));
27314 if (/* If window is in the process of being destroyed, don't bother
27315 to do anything. */
27316 w->current_matrix != NULL
27317 /* Don't update mouse highlight if hidden. */
27318 && (draw != DRAW_MOUSE_FACE || !hlinfo->mouse_face_hidden)
27319 /* Recognize when we are called to operate on rows that don't exist
27320 anymore. This can happen when a window is split. */
27321 && hlinfo->mouse_face_end_row < w->current_matrix->nrows)
27323 int phys_cursor_on_p = w->phys_cursor_on_p;
27324 struct glyph_row *row, *first, *last;
27326 first = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_beg_row);
27327 last = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_end_row);
27329 for (row = first; row <= last && row->enabled_p; ++row)
27331 int start_hpos, end_hpos, start_x;
27333 /* For all but the first row, the highlight starts at column 0. */
27334 if (row == first)
27336 /* R2L rows have BEG and END in reversed order, but the
27337 screen drawing geometry is always left to right. So
27338 we need to mirror the beginning and end of the
27339 highlighted area in R2L rows. */
27340 if (!row->reversed_p)
27342 start_hpos = hlinfo->mouse_face_beg_col;
27343 start_x = hlinfo->mouse_face_beg_x;
27345 else if (row == last)
27347 start_hpos = hlinfo->mouse_face_end_col;
27348 start_x = hlinfo->mouse_face_end_x;
27350 else
27352 start_hpos = 0;
27353 start_x = 0;
27356 else if (row->reversed_p && row == last)
27358 start_hpos = hlinfo->mouse_face_end_col;
27359 start_x = hlinfo->mouse_face_end_x;
27361 else
27363 start_hpos = 0;
27364 start_x = 0;
27367 if (row == last)
27369 if (!row->reversed_p)
27370 end_hpos = hlinfo->mouse_face_end_col;
27371 else if (row == first)
27372 end_hpos = hlinfo->mouse_face_beg_col;
27373 else
27375 end_hpos = row->used[TEXT_AREA];
27376 if (draw == DRAW_NORMAL_TEXT)
27377 row->fill_line_p = 1; /* Clear to end of line */
27380 else if (row->reversed_p && row == first)
27381 end_hpos = hlinfo->mouse_face_beg_col;
27382 else
27384 end_hpos = row->used[TEXT_AREA];
27385 if (draw == DRAW_NORMAL_TEXT)
27386 row->fill_line_p = 1; /* Clear to end of line */
27389 if (end_hpos > start_hpos)
27391 draw_row_with_mouse_face (w, start_x, row,
27392 start_hpos, end_hpos, draw);
27394 row->mouse_face_p
27395 = draw == DRAW_MOUSE_FACE || draw == DRAW_IMAGE_RAISED;
27399 #ifdef HAVE_WINDOW_SYSTEM
27400 /* When we've written over the cursor, arrange for it to
27401 be displayed again. */
27402 if (FRAME_WINDOW_P (f)
27403 && phys_cursor_on_p && !w->phys_cursor_on_p)
27405 int hpos = w->phys_cursor.hpos;
27407 /* When the window is hscrolled, cursor hpos can legitimately be
27408 out of bounds, but we draw the cursor at the corresponding
27409 window margin in that case. */
27410 if (!row->reversed_p && hpos < 0)
27411 hpos = 0;
27412 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
27413 hpos = row->used[TEXT_AREA] - 1;
27415 block_input ();
27416 display_and_set_cursor (w, 1, hpos, w->phys_cursor.vpos,
27417 w->phys_cursor.x, w->phys_cursor.y);
27418 unblock_input ();
27420 #endif /* HAVE_WINDOW_SYSTEM */
27423 #ifdef HAVE_WINDOW_SYSTEM
27424 /* Change the mouse cursor. */
27425 if (FRAME_WINDOW_P (f))
27427 #if ! defined (USE_GTK) && ! defined (HAVE_NS)
27428 if (draw == DRAW_NORMAL_TEXT
27429 && !EQ (hlinfo->mouse_face_window, f->tool_bar_window))
27430 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->text_cursor);
27431 else
27432 #endif
27433 if (draw == DRAW_MOUSE_FACE)
27434 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->hand_cursor);
27435 else
27436 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->nontext_cursor);
27438 #endif /* HAVE_WINDOW_SYSTEM */
27441 /* EXPORT:
27442 Clear out the mouse-highlighted active region.
27443 Redraw it un-highlighted first. Value is non-zero if mouse
27444 face was actually drawn unhighlighted. */
27447 clear_mouse_face (Mouse_HLInfo *hlinfo)
27449 int cleared = 0;
27451 if (!hlinfo->mouse_face_hidden && !NILP (hlinfo->mouse_face_window))
27453 show_mouse_face (hlinfo, DRAW_NORMAL_TEXT);
27454 cleared = 1;
27457 hlinfo->mouse_face_beg_row = hlinfo->mouse_face_beg_col = -1;
27458 hlinfo->mouse_face_end_row = hlinfo->mouse_face_end_col = -1;
27459 hlinfo->mouse_face_window = Qnil;
27460 hlinfo->mouse_face_overlay = Qnil;
27461 return cleared;
27464 /* Return true if the coordinates HPOS and VPOS on windows W are
27465 within the mouse face on that window. */
27466 static bool
27467 coords_in_mouse_face_p (struct window *w, int hpos, int vpos)
27469 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (XFRAME (w->frame));
27471 /* Quickly resolve the easy cases. */
27472 if (!(WINDOWP (hlinfo->mouse_face_window)
27473 && XWINDOW (hlinfo->mouse_face_window) == w))
27474 return false;
27475 if (vpos < hlinfo->mouse_face_beg_row
27476 || vpos > hlinfo->mouse_face_end_row)
27477 return false;
27478 if (vpos > hlinfo->mouse_face_beg_row
27479 && vpos < hlinfo->mouse_face_end_row)
27480 return true;
27482 if (!MATRIX_ROW (w->current_matrix, vpos)->reversed_p)
27484 if (hlinfo->mouse_face_beg_row == hlinfo->mouse_face_end_row)
27486 if (hlinfo->mouse_face_beg_col <= hpos && hpos < hlinfo->mouse_face_end_col)
27487 return true;
27489 else if ((vpos == hlinfo->mouse_face_beg_row
27490 && hpos >= hlinfo->mouse_face_beg_col)
27491 || (vpos == hlinfo->mouse_face_end_row
27492 && hpos < hlinfo->mouse_face_end_col))
27493 return true;
27495 else
27497 if (hlinfo->mouse_face_beg_row == hlinfo->mouse_face_end_row)
27499 if (hlinfo->mouse_face_end_col < hpos && hpos <= hlinfo->mouse_face_beg_col)
27500 return true;
27502 else if ((vpos == hlinfo->mouse_face_beg_row
27503 && hpos <= hlinfo->mouse_face_beg_col)
27504 || (vpos == hlinfo->mouse_face_end_row
27505 && hpos > hlinfo->mouse_face_end_col))
27506 return true;
27508 return false;
27512 /* EXPORT:
27513 True if physical cursor of window W is within mouse face. */
27515 bool
27516 cursor_in_mouse_face_p (struct window *w)
27518 int hpos = w->phys_cursor.hpos;
27519 int vpos = w->phys_cursor.vpos;
27520 struct glyph_row *row = MATRIX_ROW (w->current_matrix, vpos);
27522 /* When the window is hscrolled, cursor hpos can legitimately be out
27523 of bounds, but we draw the cursor at the corresponding window
27524 margin in that case. */
27525 if (!row->reversed_p && hpos < 0)
27526 hpos = 0;
27527 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
27528 hpos = row->used[TEXT_AREA] - 1;
27530 return coords_in_mouse_face_p (w, hpos, vpos);
27535 /* Find the glyph rows START_ROW and END_ROW of window W that display
27536 characters between buffer positions START_CHARPOS and END_CHARPOS
27537 (excluding END_CHARPOS). DISP_STRING is a display string that
27538 covers these buffer positions. This is similar to
27539 row_containing_pos, but is more accurate when bidi reordering makes
27540 buffer positions change non-linearly with glyph rows. */
27541 static void
27542 rows_from_pos_range (struct window *w,
27543 ptrdiff_t start_charpos, ptrdiff_t end_charpos,
27544 Lisp_Object disp_string,
27545 struct glyph_row **start, struct glyph_row **end)
27547 struct glyph_row *first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
27548 int last_y = window_text_bottom_y (w);
27549 struct glyph_row *row;
27551 *start = NULL;
27552 *end = NULL;
27554 while (!first->enabled_p
27555 && first < MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w))
27556 first++;
27558 /* Find the START row. */
27559 for (row = first;
27560 row->enabled_p && MATRIX_ROW_BOTTOM_Y (row) <= last_y;
27561 row++)
27563 /* A row can potentially be the START row if the range of the
27564 characters it displays intersects the range
27565 [START_CHARPOS..END_CHARPOS). */
27566 if (! ((start_charpos < MATRIX_ROW_START_CHARPOS (row)
27567 && end_charpos < MATRIX_ROW_START_CHARPOS (row))
27568 /* See the commentary in row_containing_pos, for the
27569 explanation of the complicated way to check whether
27570 some position is beyond the end of the characters
27571 displayed by a row. */
27572 || ((start_charpos > MATRIX_ROW_END_CHARPOS (row)
27573 || (start_charpos == MATRIX_ROW_END_CHARPOS (row)
27574 && !row->ends_at_zv_p
27575 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
27576 && (end_charpos > MATRIX_ROW_END_CHARPOS (row)
27577 || (end_charpos == MATRIX_ROW_END_CHARPOS (row)
27578 && !row->ends_at_zv_p
27579 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))))))
27581 /* Found a candidate row. Now make sure at least one of the
27582 glyphs it displays has a charpos from the range
27583 [START_CHARPOS..END_CHARPOS).
27585 This is not obvious because bidi reordering could make
27586 buffer positions of a row be 1,2,3,102,101,100, and if we
27587 want to highlight characters in [50..60), we don't want
27588 this row, even though [50..60) does intersect [1..103),
27589 the range of character positions given by the row's start
27590 and end positions. */
27591 struct glyph *g = row->glyphs[TEXT_AREA];
27592 struct glyph *e = g + row->used[TEXT_AREA];
27594 while (g < e)
27596 if (((BUFFERP (g->object) || INTEGERP (g->object))
27597 && start_charpos <= g->charpos && g->charpos < end_charpos)
27598 /* A glyph that comes from DISP_STRING is by
27599 definition to be highlighted. */
27600 || EQ (g->object, disp_string))
27601 *start = row;
27602 g++;
27604 if (*start)
27605 break;
27609 /* Find the END row. */
27610 if (!*start
27611 /* If the last row is partially visible, start looking for END
27612 from that row, instead of starting from FIRST. */
27613 && !(row->enabled_p
27614 && row->y < last_y && MATRIX_ROW_BOTTOM_Y (row) > last_y))
27615 row = first;
27616 for ( ; row->enabled_p && MATRIX_ROW_BOTTOM_Y (row) <= last_y; row++)
27618 struct glyph_row *next = row + 1;
27619 ptrdiff_t next_start = MATRIX_ROW_START_CHARPOS (next);
27621 if (!next->enabled_p
27622 || next >= MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w)
27623 /* The first row >= START whose range of displayed characters
27624 does NOT intersect the range [START_CHARPOS..END_CHARPOS]
27625 is the row END + 1. */
27626 || (start_charpos < next_start
27627 && end_charpos < next_start)
27628 || ((start_charpos > MATRIX_ROW_END_CHARPOS (next)
27629 || (start_charpos == MATRIX_ROW_END_CHARPOS (next)
27630 && !next->ends_at_zv_p
27631 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (next)))
27632 && (end_charpos > MATRIX_ROW_END_CHARPOS (next)
27633 || (end_charpos == MATRIX_ROW_END_CHARPOS (next)
27634 && !next->ends_at_zv_p
27635 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (next)))))
27637 *end = row;
27638 break;
27640 else
27642 /* If the next row's edges intersect [START_CHARPOS..END_CHARPOS],
27643 but none of the characters it displays are in the range, it is
27644 also END + 1. */
27645 struct glyph *g = next->glyphs[TEXT_AREA];
27646 struct glyph *s = g;
27647 struct glyph *e = g + next->used[TEXT_AREA];
27649 while (g < e)
27651 if (((BUFFERP (g->object) || INTEGERP (g->object))
27652 && ((start_charpos <= g->charpos && g->charpos < end_charpos)
27653 /* If the buffer position of the first glyph in
27654 the row is equal to END_CHARPOS, it means
27655 the last character to be highlighted is the
27656 newline of ROW, and we must consider NEXT as
27657 END, not END+1. */
27658 || (((!next->reversed_p && g == s)
27659 || (next->reversed_p && g == e - 1))
27660 && (g->charpos == end_charpos
27661 /* Special case for when NEXT is an
27662 empty line at ZV. */
27663 || (g->charpos == -1
27664 && !row->ends_at_zv_p
27665 && next_start == end_charpos)))))
27666 /* A glyph that comes from DISP_STRING is by
27667 definition to be highlighted. */
27668 || EQ (g->object, disp_string))
27669 break;
27670 g++;
27672 if (g == e)
27674 *end = row;
27675 break;
27677 /* The first row that ends at ZV must be the last to be
27678 highlighted. */
27679 else if (next->ends_at_zv_p)
27681 *end = next;
27682 break;
27688 /* This function sets the mouse_face_* elements of HLINFO, assuming
27689 the mouse cursor is on a glyph with buffer charpos MOUSE_CHARPOS in
27690 window WINDOW. START_CHARPOS and END_CHARPOS are buffer positions
27691 for the overlay or run of text properties specifying the mouse
27692 face. BEFORE_STRING and AFTER_STRING, if non-nil, are a
27693 before-string and after-string that must also be highlighted.
27694 DISP_STRING, if non-nil, is a display string that may cover some
27695 or all of the highlighted text. */
27697 static void
27698 mouse_face_from_buffer_pos (Lisp_Object window,
27699 Mouse_HLInfo *hlinfo,
27700 ptrdiff_t mouse_charpos,
27701 ptrdiff_t start_charpos,
27702 ptrdiff_t end_charpos,
27703 Lisp_Object before_string,
27704 Lisp_Object after_string,
27705 Lisp_Object disp_string)
27707 struct window *w = XWINDOW (window);
27708 struct glyph_row *first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
27709 struct glyph_row *r1, *r2;
27710 struct glyph *glyph, *end;
27711 ptrdiff_t ignore, pos;
27712 int x;
27714 eassert (NILP (disp_string) || STRINGP (disp_string));
27715 eassert (NILP (before_string) || STRINGP (before_string));
27716 eassert (NILP (after_string) || STRINGP (after_string));
27718 /* Find the rows corresponding to START_CHARPOS and END_CHARPOS. */
27719 rows_from_pos_range (w, start_charpos, end_charpos, disp_string, &r1, &r2);
27720 if (r1 == NULL)
27721 r1 = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
27722 /* If the before-string or display-string contains newlines,
27723 rows_from_pos_range skips to its last row. Move back. */
27724 if (!NILP (before_string) || !NILP (disp_string))
27726 struct glyph_row *prev;
27727 while ((prev = r1 - 1, prev >= first)
27728 && MATRIX_ROW_END_CHARPOS (prev) == start_charpos
27729 && prev->used[TEXT_AREA] > 0)
27731 struct glyph *beg = prev->glyphs[TEXT_AREA];
27732 glyph = beg + prev->used[TEXT_AREA];
27733 while (--glyph >= beg && INTEGERP (glyph->object));
27734 if (glyph < beg
27735 || !(EQ (glyph->object, before_string)
27736 || EQ (glyph->object, disp_string)))
27737 break;
27738 r1 = prev;
27741 if (r2 == NULL)
27743 r2 = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
27744 hlinfo->mouse_face_past_end = 1;
27746 else if (!NILP (after_string))
27748 /* If the after-string has newlines, advance to its last row. */
27749 struct glyph_row *next;
27750 struct glyph_row *last
27751 = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
27753 for (next = r2 + 1;
27754 next <= last
27755 && next->used[TEXT_AREA] > 0
27756 && EQ (next->glyphs[TEXT_AREA]->object, after_string);
27757 ++next)
27758 r2 = next;
27760 /* The rest of the display engine assumes that mouse_face_beg_row is
27761 either above mouse_face_end_row or identical to it. But with
27762 bidi-reordered continued lines, the row for START_CHARPOS could
27763 be below the row for END_CHARPOS. If so, swap the rows and store
27764 them in correct order. */
27765 if (r1->y > r2->y)
27767 struct glyph_row *tem = r2;
27769 r2 = r1;
27770 r1 = tem;
27773 hlinfo->mouse_face_beg_row = MATRIX_ROW_VPOS (r1, w->current_matrix);
27774 hlinfo->mouse_face_end_row = MATRIX_ROW_VPOS (r2, w->current_matrix);
27776 /* For a bidi-reordered row, the positions of BEFORE_STRING,
27777 AFTER_STRING, DISP_STRING, START_CHARPOS, and END_CHARPOS
27778 could be anywhere in the row and in any order. The strategy
27779 below is to find the leftmost and the rightmost glyph that
27780 belongs to either of these 3 strings, or whose position is
27781 between START_CHARPOS and END_CHARPOS, and highlight all the
27782 glyphs between those two. This may cover more than just the text
27783 between START_CHARPOS and END_CHARPOS if the range of characters
27784 strides the bidi level boundary, e.g. if the beginning is in R2L
27785 text while the end is in L2R text or vice versa. */
27786 if (!r1->reversed_p)
27788 /* This row is in a left to right paragraph. Scan it left to
27789 right. */
27790 glyph = r1->glyphs[TEXT_AREA];
27791 end = glyph + r1->used[TEXT_AREA];
27792 x = r1->x;
27794 /* Skip truncation glyphs at the start of the glyph row. */
27795 if (MATRIX_ROW_DISPLAYS_TEXT_P (r1))
27796 for (; glyph < end
27797 && INTEGERP (glyph->object)
27798 && glyph->charpos < 0;
27799 ++glyph)
27800 x += glyph->pixel_width;
27802 /* Scan the glyph row, looking for BEFORE_STRING, AFTER_STRING,
27803 or DISP_STRING, and the first glyph from buffer whose
27804 position is between START_CHARPOS and END_CHARPOS. */
27805 for (; glyph < end
27806 && !INTEGERP (glyph->object)
27807 && !EQ (glyph->object, disp_string)
27808 && !(BUFFERP (glyph->object)
27809 && (glyph->charpos >= start_charpos
27810 && glyph->charpos < end_charpos));
27811 ++glyph)
27813 /* BEFORE_STRING or AFTER_STRING are only relevant if they
27814 are present at buffer positions between START_CHARPOS and
27815 END_CHARPOS, or if they come from an overlay. */
27816 if (EQ (glyph->object, before_string))
27818 pos = string_buffer_position (before_string,
27819 start_charpos);
27820 /* If pos == 0, it means before_string came from an
27821 overlay, not from a buffer position. */
27822 if (!pos || (pos >= start_charpos && pos < end_charpos))
27823 break;
27825 else if (EQ (glyph->object, after_string))
27827 pos = string_buffer_position (after_string, end_charpos);
27828 if (!pos || (pos >= start_charpos && pos < end_charpos))
27829 break;
27831 x += glyph->pixel_width;
27833 hlinfo->mouse_face_beg_x = x;
27834 hlinfo->mouse_face_beg_col = glyph - r1->glyphs[TEXT_AREA];
27836 else
27838 /* This row is in a right to left paragraph. Scan it right to
27839 left. */
27840 struct glyph *g;
27842 end = r1->glyphs[TEXT_AREA] - 1;
27843 glyph = end + r1->used[TEXT_AREA];
27845 /* Skip truncation glyphs at the start of the glyph row. */
27846 if (MATRIX_ROW_DISPLAYS_TEXT_P (r1))
27847 for (; glyph > end
27848 && INTEGERP (glyph->object)
27849 && glyph->charpos < 0;
27850 --glyph)
27853 /* Scan the glyph row, looking for BEFORE_STRING, AFTER_STRING,
27854 or DISP_STRING, and the first glyph from buffer whose
27855 position is between START_CHARPOS and END_CHARPOS. */
27856 for (; glyph > end
27857 && !INTEGERP (glyph->object)
27858 && !EQ (glyph->object, disp_string)
27859 && !(BUFFERP (glyph->object)
27860 && (glyph->charpos >= start_charpos
27861 && glyph->charpos < end_charpos));
27862 --glyph)
27864 /* BEFORE_STRING or AFTER_STRING are only relevant if they
27865 are present at buffer positions between START_CHARPOS and
27866 END_CHARPOS, or if they come from an overlay. */
27867 if (EQ (glyph->object, before_string))
27869 pos = string_buffer_position (before_string, start_charpos);
27870 /* If pos == 0, it means before_string came from an
27871 overlay, not from a buffer position. */
27872 if (!pos || (pos >= start_charpos && pos < end_charpos))
27873 break;
27875 else if (EQ (glyph->object, after_string))
27877 pos = string_buffer_position (after_string, end_charpos);
27878 if (!pos || (pos >= start_charpos && pos < end_charpos))
27879 break;
27883 glyph++; /* first glyph to the right of the highlighted area */
27884 for (g = r1->glyphs[TEXT_AREA], x = r1->x; g < glyph; g++)
27885 x += g->pixel_width;
27886 hlinfo->mouse_face_beg_x = x;
27887 hlinfo->mouse_face_beg_col = glyph - r1->glyphs[TEXT_AREA];
27890 /* If the highlight ends in a different row, compute GLYPH and END
27891 for the end row. Otherwise, reuse the values computed above for
27892 the row where the highlight begins. */
27893 if (r2 != r1)
27895 if (!r2->reversed_p)
27897 glyph = r2->glyphs[TEXT_AREA];
27898 end = glyph + r2->used[TEXT_AREA];
27899 x = r2->x;
27901 else
27903 end = r2->glyphs[TEXT_AREA] - 1;
27904 glyph = end + r2->used[TEXT_AREA];
27908 if (!r2->reversed_p)
27910 /* Skip truncation and continuation glyphs near the end of the
27911 row, and also blanks and stretch glyphs inserted by
27912 extend_face_to_end_of_line. */
27913 while (end > glyph
27914 && INTEGERP ((end - 1)->object))
27915 --end;
27916 /* Scan the rest of the glyph row from the end, looking for the
27917 first glyph that comes from BEFORE_STRING, AFTER_STRING, or
27918 DISP_STRING, or whose position is between START_CHARPOS
27919 and END_CHARPOS */
27920 for (--end;
27921 end > glyph
27922 && !INTEGERP (end->object)
27923 && !EQ (end->object, disp_string)
27924 && !(BUFFERP (end->object)
27925 && (end->charpos >= start_charpos
27926 && end->charpos < end_charpos));
27927 --end)
27929 /* BEFORE_STRING or AFTER_STRING are only relevant if they
27930 are present at buffer positions between START_CHARPOS and
27931 END_CHARPOS, or if they come from an overlay. */
27932 if (EQ (end->object, before_string))
27934 pos = string_buffer_position (before_string, start_charpos);
27935 if (!pos || (pos >= start_charpos && pos < end_charpos))
27936 break;
27938 else if (EQ (end->object, after_string))
27940 pos = string_buffer_position (after_string, end_charpos);
27941 if (!pos || (pos >= start_charpos && pos < end_charpos))
27942 break;
27945 /* Find the X coordinate of the last glyph to be highlighted. */
27946 for (; glyph <= end; ++glyph)
27947 x += glyph->pixel_width;
27949 hlinfo->mouse_face_end_x = x;
27950 hlinfo->mouse_face_end_col = glyph - r2->glyphs[TEXT_AREA];
27952 else
27954 /* Skip truncation and continuation glyphs near the end of the
27955 row, and also blanks and stretch glyphs inserted by
27956 extend_face_to_end_of_line. */
27957 x = r2->x;
27958 end++;
27959 while (end < glyph
27960 && INTEGERP (end->object))
27962 x += end->pixel_width;
27963 ++end;
27965 /* Scan the rest of the glyph row from the end, looking for the
27966 first glyph that comes from BEFORE_STRING, AFTER_STRING, or
27967 DISP_STRING, or whose position is between START_CHARPOS
27968 and END_CHARPOS */
27969 for ( ;
27970 end < glyph
27971 && !INTEGERP (end->object)
27972 && !EQ (end->object, disp_string)
27973 && !(BUFFERP (end->object)
27974 && (end->charpos >= start_charpos
27975 && end->charpos < end_charpos));
27976 ++end)
27978 /* BEFORE_STRING or AFTER_STRING are only relevant if they
27979 are present at buffer positions between START_CHARPOS and
27980 END_CHARPOS, or if they come from an overlay. */
27981 if (EQ (end->object, before_string))
27983 pos = string_buffer_position (before_string, start_charpos);
27984 if (!pos || (pos >= start_charpos && pos < end_charpos))
27985 break;
27987 else if (EQ (end->object, after_string))
27989 pos = string_buffer_position (after_string, end_charpos);
27990 if (!pos || (pos >= start_charpos && pos < end_charpos))
27991 break;
27993 x += end->pixel_width;
27995 /* If we exited the above loop because we arrived at the last
27996 glyph of the row, and its buffer position is still not in
27997 range, it means the last character in range is the preceding
27998 newline. Bump the end column and x values to get past the
27999 last glyph. */
28000 if (end == glyph
28001 && BUFFERP (end->object)
28002 && (end->charpos < start_charpos
28003 || end->charpos >= end_charpos))
28005 x += end->pixel_width;
28006 ++end;
28008 hlinfo->mouse_face_end_x = x;
28009 hlinfo->mouse_face_end_col = end - r2->glyphs[TEXT_AREA];
28012 hlinfo->mouse_face_window = window;
28013 hlinfo->mouse_face_face_id
28014 = face_at_buffer_position (w, mouse_charpos, &ignore,
28015 mouse_charpos + 1,
28016 !hlinfo->mouse_face_hidden, -1);
28017 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
28020 /* The following function is not used anymore (replaced with
28021 mouse_face_from_string_pos), but I leave it here for the time
28022 being, in case someone would. */
28024 #if 0 /* not used */
28026 /* Find the position of the glyph for position POS in OBJECT in
28027 window W's current matrix, and return in *X, *Y the pixel
28028 coordinates, and return in *HPOS, *VPOS the column/row of the glyph.
28030 RIGHT_P non-zero means return the position of the right edge of the
28031 glyph, RIGHT_P zero means return the left edge position.
28033 If no glyph for POS exists in the matrix, return the position of
28034 the glyph with the next smaller position that is in the matrix, if
28035 RIGHT_P is zero. If RIGHT_P is non-zero, and no glyph for POS
28036 exists in the matrix, return the position of the glyph with the
28037 next larger position in OBJECT.
28039 Value is non-zero if a glyph was found. */
28041 static int
28042 fast_find_string_pos (struct window *w, ptrdiff_t pos, Lisp_Object object,
28043 int *hpos, int *vpos, int *x, int *y, int right_p)
28045 int yb = window_text_bottom_y (w);
28046 struct glyph_row *r;
28047 struct glyph *best_glyph = NULL;
28048 struct glyph_row *best_row = NULL;
28049 int best_x = 0;
28051 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
28052 r->enabled_p && r->y < yb;
28053 ++r)
28055 struct glyph *g = r->glyphs[TEXT_AREA];
28056 struct glyph *e = g + r->used[TEXT_AREA];
28057 int gx;
28059 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
28060 if (EQ (g->object, object))
28062 if (g->charpos == pos)
28064 best_glyph = g;
28065 best_x = gx;
28066 best_row = r;
28067 goto found;
28069 else if (best_glyph == NULL
28070 || ((eabs (g->charpos - pos)
28071 < eabs (best_glyph->charpos - pos))
28072 && (right_p
28073 ? g->charpos < pos
28074 : g->charpos > pos)))
28076 best_glyph = g;
28077 best_x = gx;
28078 best_row = r;
28083 found:
28085 if (best_glyph)
28087 *x = best_x;
28088 *hpos = best_glyph - best_row->glyphs[TEXT_AREA];
28090 if (right_p)
28092 *x += best_glyph->pixel_width;
28093 ++*hpos;
28096 *y = best_row->y;
28097 *vpos = MATRIX_ROW_VPOS (best_row, w->current_matrix);
28100 return best_glyph != NULL;
28102 #endif /* not used */
28104 /* Find the positions of the first and the last glyphs in window W's
28105 current matrix that occlude positions [STARTPOS..ENDPOS) in OBJECT
28106 (assumed to be a string), and return in HLINFO's mouse_face_*
28107 members the pixel and column/row coordinates of those glyphs. */
28109 static void
28110 mouse_face_from_string_pos (struct window *w, Mouse_HLInfo *hlinfo,
28111 Lisp_Object object,
28112 ptrdiff_t startpos, ptrdiff_t endpos)
28114 int yb = window_text_bottom_y (w);
28115 struct glyph_row *r;
28116 struct glyph *g, *e;
28117 int gx;
28118 int found = 0;
28120 /* Find the glyph row with at least one position in the range
28121 [STARTPOS..ENDPOS), and the first glyph in that row whose
28122 position belongs to that range. */
28123 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
28124 r->enabled_p && r->y < yb;
28125 ++r)
28127 if (!r->reversed_p)
28129 g = r->glyphs[TEXT_AREA];
28130 e = g + r->used[TEXT_AREA];
28131 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
28132 if (EQ (g->object, object)
28133 && startpos <= g->charpos && g->charpos < endpos)
28135 hlinfo->mouse_face_beg_row
28136 = MATRIX_ROW_VPOS (r, w->current_matrix);
28137 hlinfo->mouse_face_beg_col = g - r->glyphs[TEXT_AREA];
28138 hlinfo->mouse_face_beg_x = gx;
28139 found = 1;
28140 break;
28143 else
28145 struct glyph *g1;
28147 e = r->glyphs[TEXT_AREA];
28148 g = e + r->used[TEXT_AREA];
28149 for ( ; g > e; --g)
28150 if (EQ ((g-1)->object, object)
28151 && startpos <= (g-1)->charpos && (g-1)->charpos < endpos)
28153 hlinfo->mouse_face_beg_row
28154 = MATRIX_ROW_VPOS (r, w->current_matrix);
28155 hlinfo->mouse_face_beg_col = g - r->glyphs[TEXT_AREA];
28156 for (gx = r->x, g1 = r->glyphs[TEXT_AREA]; g1 < g; ++g1)
28157 gx += g1->pixel_width;
28158 hlinfo->mouse_face_beg_x = gx;
28159 found = 1;
28160 break;
28163 if (found)
28164 break;
28167 if (!found)
28168 return;
28170 /* Starting with the next row, look for the first row which does NOT
28171 include any glyphs whose positions are in the range. */
28172 for (++r; r->enabled_p && r->y < yb; ++r)
28174 g = r->glyphs[TEXT_AREA];
28175 e = g + r->used[TEXT_AREA];
28176 found = 0;
28177 for ( ; g < e; ++g)
28178 if (EQ (g->object, object)
28179 && startpos <= g->charpos && g->charpos < endpos)
28181 found = 1;
28182 break;
28184 if (!found)
28185 break;
28188 /* The highlighted region ends on the previous row. */
28189 r--;
28191 /* Set the end row. */
28192 hlinfo->mouse_face_end_row = MATRIX_ROW_VPOS (r, w->current_matrix);
28194 /* Compute and set the end column and the end column's horizontal
28195 pixel coordinate. */
28196 if (!r->reversed_p)
28198 g = r->glyphs[TEXT_AREA];
28199 e = g + r->used[TEXT_AREA];
28200 for ( ; e > g; --e)
28201 if (EQ ((e-1)->object, object)
28202 && startpos <= (e-1)->charpos && (e-1)->charpos < endpos)
28203 break;
28204 hlinfo->mouse_face_end_col = e - g;
28206 for (gx = r->x; g < e; ++g)
28207 gx += g->pixel_width;
28208 hlinfo->mouse_face_end_x = gx;
28210 else
28212 e = r->glyphs[TEXT_AREA];
28213 g = e + r->used[TEXT_AREA];
28214 for (gx = r->x ; e < g; ++e)
28216 if (EQ (e->object, object)
28217 && startpos <= e->charpos && e->charpos < endpos)
28218 break;
28219 gx += e->pixel_width;
28221 hlinfo->mouse_face_end_col = e - r->glyphs[TEXT_AREA];
28222 hlinfo->mouse_face_end_x = gx;
28226 #ifdef HAVE_WINDOW_SYSTEM
28228 /* See if position X, Y is within a hot-spot of an image. */
28230 static int
28231 on_hot_spot_p (Lisp_Object hot_spot, int x, int y)
28233 if (!CONSP (hot_spot))
28234 return 0;
28236 if (EQ (XCAR (hot_spot), Qrect))
28238 /* CDR is (Top-Left . Bottom-Right) = ((x0 . y0) . (x1 . y1)) */
28239 Lisp_Object rect = XCDR (hot_spot);
28240 Lisp_Object tem;
28241 if (!CONSP (rect))
28242 return 0;
28243 if (!CONSP (XCAR (rect)))
28244 return 0;
28245 if (!CONSP (XCDR (rect)))
28246 return 0;
28247 if (!(tem = XCAR (XCAR (rect)), INTEGERP (tem) && x >= XINT (tem)))
28248 return 0;
28249 if (!(tem = XCDR (XCAR (rect)), INTEGERP (tem) && y >= XINT (tem)))
28250 return 0;
28251 if (!(tem = XCAR (XCDR (rect)), INTEGERP (tem) && x <= XINT (tem)))
28252 return 0;
28253 if (!(tem = XCDR (XCDR (rect)), INTEGERP (tem) && y <= XINT (tem)))
28254 return 0;
28255 return 1;
28257 else if (EQ (XCAR (hot_spot), Qcircle))
28259 /* CDR is (Center . Radius) = ((x0 . y0) . r) */
28260 Lisp_Object circ = XCDR (hot_spot);
28261 Lisp_Object lr, lx0, ly0;
28262 if (CONSP (circ)
28263 && CONSP (XCAR (circ))
28264 && (lr = XCDR (circ), INTEGERP (lr) || FLOATP (lr))
28265 && (lx0 = XCAR (XCAR (circ)), INTEGERP (lx0))
28266 && (ly0 = XCDR (XCAR (circ)), INTEGERP (ly0)))
28268 double r = XFLOATINT (lr);
28269 double dx = XINT (lx0) - x;
28270 double dy = XINT (ly0) - y;
28271 return (dx * dx + dy * dy <= r * r);
28274 else if (EQ (XCAR (hot_spot), Qpoly))
28276 /* CDR is [x0 y0 x1 y1 x2 y2 ...x(n-1) y(n-1)] */
28277 if (VECTORP (XCDR (hot_spot)))
28279 struct Lisp_Vector *v = XVECTOR (XCDR (hot_spot));
28280 Lisp_Object *poly = v->contents;
28281 ptrdiff_t n = v->header.size;
28282 ptrdiff_t i;
28283 int inside = 0;
28284 Lisp_Object lx, ly;
28285 int x0, y0;
28287 /* Need an even number of coordinates, and at least 3 edges. */
28288 if (n < 6 || n & 1)
28289 return 0;
28291 /* Count edge segments intersecting line from (X,Y) to (X,infinity).
28292 If count is odd, we are inside polygon. Pixels on edges
28293 may or may not be included depending on actual geometry of the
28294 polygon. */
28295 if ((lx = poly[n-2], !INTEGERP (lx))
28296 || (ly = poly[n-1], !INTEGERP (lx)))
28297 return 0;
28298 x0 = XINT (lx), y0 = XINT (ly);
28299 for (i = 0; i < n; i += 2)
28301 int x1 = x0, y1 = y0;
28302 if ((lx = poly[i], !INTEGERP (lx))
28303 || (ly = poly[i+1], !INTEGERP (ly)))
28304 return 0;
28305 x0 = XINT (lx), y0 = XINT (ly);
28307 /* Does this segment cross the X line? */
28308 if (x0 >= x)
28310 if (x1 >= x)
28311 continue;
28313 else if (x1 < x)
28314 continue;
28315 if (y > y0 && y > y1)
28316 continue;
28317 if (y < y0 + ((y1 - y0) * (x - x0)) / (x1 - x0))
28318 inside = !inside;
28320 return inside;
28323 return 0;
28326 Lisp_Object
28327 find_hot_spot (Lisp_Object map, int x, int y)
28329 while (CONSP (map))
28331 if (CONSP (XCAR (map))
28332 && on_hot_spot_p (XCAR (XCAR (map)), x, y))
28333 return XCAR (map);
28334 map = XCDR (map);
28337 return Qnil;
28340 DEFUN ("lookup-image-map", Flookup_image_map, Slookup_image_map,
28341 3, 3, 0,
28342 doc: /* Lookup in image map MAP coordinates X and Y.
28343 An image map is an alist where each element has the format (AREA ID PLIST).
28344 An AREA is specified as either a rectangle, a circle, or a polygon:
28345 A rectangle is a cons (rect . ((x0 . y0) . (x1 . y1))) specifying the
28346 pixel coordinates of the upper left and bottom right corners.
28347 A circle is a cons (circle . ((x0 . y0) . r)) specifying the center
28348 and the radius of the circle; r may be a float or integer.
28349 A polygon is a cons (poly . [x0 y0 x1 y1 ...]) where each pair in the
28350 vector describes one corner in the polygon.
28351 Returns the alist element for the first matching AREA in MAP. */)
28352 (Lisp_Object map, Lisp_Object x, Lisp_Object y)
28354 if (NILP (map))
28355 return Qnil;
28357 CHECK_NUMBER (x);
28358 CHECK_NUMBER (y);
28360 return find_hot_spot (map,
28361 clip_to_bounds (INT_MIN, XINT (x), INT_MAX),
28362 clip_to_bounds (INT_MIN, XINT (y), INT_MAX));
28366 /* Display frame CURSOR, optionally using shape defined by POINTER. */
28367 static void
28368 define_frame_cursor1 (struct frame *f, Cursor cursor, Lisp_Object pointer)
28370 /* Do not change cursor shape while dragging mouse. */
28371 if (!NILP (do_mouse_tracking))
28372 return;
28374 if (!NILP (pointer))
28376 if (EQ (pointer, Qarrow))
28377 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
28378 else if (EQ (pointer, Qhand))
28379 cursor = FRAME_X_OUTPUT (f)->hand_cursor;
28380 else if (EQ (pointer, Qtext))
28381 cursor = FRAME_X_OUTPUT (f)->text_cursor;
28382 else if (EQ (pointer, intern ("hdrag")))
28383 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
28384 else if (EQ (pointer, intern ("nhdrag")))
28385 cursor = FRAME_X_OUTPUT (f)->vertical_drag_cursor;
28386 #ifdef HAVE_X_WINDOWS
28387 else if (EQ (pointer, intern ("vdrag")))
28388 cursor = FRAME_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
28389 #endif
28390 else if (EQ (pointer, intern ("hourglass")))
28391 cursor = FRAME_X_OUTPUT (f)->hourglass_cursor;
28392 else if (EQ (pointer, Qmodeline))
28393 cursor = FRAME_X_OUTPUT (f)->modeline_cursor;
28394 else
28395 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
28398 if (cursor != No_Cursor)
28399 FRAME_RIF (f)->define_frame_cursor (f, cursor);
28402 #endif /* HAVE_WINDOW_SYSTEM */
28404 /* Take proper action when mouse has moved to the mode or header line
28405 or marginal area AREA of window W, x-position X and y-position Y.
28406 X is relative to the start of the text display area of W, so the
28407 width of bitmap areas and scroll bars must be subtracted to get a
28408 position relative to the start of the mode line. */
28410 static void
28411 note_mode_line_or_margin_highlight (Lisp_Object window, int x, int y,
28412 enum window_part area)
28414 struct window *w = XWINDOW (window);
28415 struct frame *f = XFRAME (w->frame);
28416 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
28417 #ifdef HAVE_WINDOW_SYSTEM
28418 Display_Info *dpyinfo;
28419 #endif
28420 Cursor cursor = No_Cursor;
28421 Lisp_Object pointer = Qnil;
28422 int dx, dy, width, height;
28423 ptrdiff_t charpos;
28424 Lisp_Object string, object = Qnil;
28425 Lisp_Object pos IF_LINT (= Qnil), help;
28427 Lisp_Object mouse_face;
28428 int original_x_pixel = x;
28429 struct glyph * glyph = NULL, * row_start_glyph = NULL;
28430 struct glyph_row *row IF_LINT (= 0);
28432 if (area == ON_MODE_LINE || area == ON_HEADER_LINE)
28434 int x0;
28435 struct glyph *end;
28437 /* Kludge alert: mode_line_string takes X/Y in pixels, but
28438 returns them in row/column units! */
28439 string = mode_line_string (w, area, &x, &y, &charpos,
28440 &object, &dx, &dy, &width, &height);
28442 row = (area == ON_MODE_LINE
28443 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
28444 : MATRIX_HEADER_LINE_ROW (w->current_matrix));
28446 /* Find the glyph under the mouse pointer. */
28447 if (row->mode_line_p && row->enabled_p)
28449 glyph = row_start_glyph = row->glyphs[TEXT_AREA];
28450 end = glyph + row->used[TEXT_AREA];
28452 for (x0 = original_x_pixel;
28453 glyph < end && x0 >= glyph->pixel_width;
28454 ++glyph)
28455 x0 -= glyph->pixel_width;
28457 if (glyph >= end)
28458 glyph = NULL;
28461 else
28463 x -= WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
28464 /* Kludge alert: marginal_area_string takes X/Y in pixels, but
28465 returns them in row/column units! */
28466 string = marginal_area_string (w, area, &x, &y, &charpos,
28467 &object, &dx, &dy, &width, &height);
28470 help = Qnil;
28472 #ifdef HAVE_WINDOW_SYSTEM
28473 if (IMAGEP (object))
28475 Lisp_Object image_map, hotspot;
28476 if ((image_map = Fplist_get (XCDR (object), QCmap),
28477 !NILP (image_map))
28478 && (hotspot = find_hot_spot (image_map, dx, dy),
28479 CONSP (hotspot))
28480 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
28482 Lisp_Object plist;
28484 /* Could check XCAR (hotspot) to see if we enter/leave this hot-spot.
28485 If so, we could look for mouse-enter, mouse-leave
28486 properties in PLIST (and do something...). */
28487 hotspot = XCDR (hotspot);
28488 if (CONSP (hotspot)
28489 && (plist = XCAR (hotspot), CONSP (plist)))
28491 pointer = Fplist_get (plist, Qpointer);
28492 if (NILP (pointer))
28493 pointer = Qhand;
28494 help = Fplist_get (plist, Qhelp_echo);
28495 if (!NILP (help))
28497 help_echo_string = help;
28498 XSETWINDOW (help_echo_window, w);
28499 help_echo_object = w->contents;
28500 help_echo_pos = charpos;
28504 if (NILP (pointer))
28505 pointer = Fplist_get (XCDR (object), QCpointer);
28507 #endif /* HAVE_WINDOW_SYSTEM */
28509 if (STRINGP (string))
28510 pos = make_number (charpos);
28512 /* Set the help text and mouse pointer. If the mouse is on a part
28513 of the mode line without any text (e.g. past the right edge of
28514 the mode line text), use the default help text and pointer. */
28515 if (STRINGP (string) || area == ON_MODE_LINE)
28517 /* Arrange to display the help by setting the global variables
28518 help_echo_string, help_echo_object, and help_echo_pos. */
28519 if (NILP (help))
28521 if (STRINGP (string))
28522 help = Fget_text_property (pos, Qhelp_echo, string);
28524 if (!NILP (help))
28526 help_echo_string = help;
28527 XSETWINDOW (help_echo_window, w);
28528 help_echo_object = string;
28529 help_echo_pos = charpos;
28531 else if (area == ON_MODE_LINE)
28533 Lisp_Object default_help
28534 = buffer_local_value_1 (Qmode_line_default_help_echo,
28535 w->contents);
28537 if (STRINGP (default_help))
28539 help_echo_string = default_help;
28540 XSETWINDOW (help_echo_window, w);
28541 help_echo_object = Qnil;
28542 help_echo_pos = -1;
28547 #ifdef HAVE_WINDOW_SYSTEM
28548 /* Change the mouse pointer according to what is under it. */
28549 if (FRAME_WINDOW_P (f))
28551 bool draggable = (! WINDOW_BOTTOMMOST_P (w)
28552 || minibuf_level
28553 || NILP (Vresize_mini_windows));
28555 dpyinfo = FRAME_DISPLAY_INFO (f);
28556 if (STRINGP (string))
28558 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
28560 if (NILP (pointer))
28561 pointer = Fget_text_property (pos, Qpointer, string);
28563 /* Change the mouse pointer according to what is under X/Y. */
28564 if (NILP (pointer)
28565 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE)))
28567 Lisp_Object map;
28568 map = Fget_text_property (pos, Qlocal_map, string);
28569 if (!KEYMAPP (map))
28570 map = Fget_text_property (pos, Qkeymap, string);
28571 if (!KEYMAPP (map) && draggable)
28572 cursor = dpyinfo->vertical_scroll_bar_cursor;
28575 else if (draggable)
28576 /* Default mode-line pointer. */
28577 cursor = FRAME_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
28579 #endif
28582 /* Change the mouse face according to what is under X/Y. */
28583 if (STRINGP (string))
28585 mouse_face = Fget_text_property (pos, Qmouse_face, string);
28586 if (!NILP (Vmouse_highlight) && !NILP (mouse_face)
28587 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
28588 && glyph)
28590 Lisp_Object b, e;
28592 struct glyph * tmp_glyph;
28594 int gpos;
28595 int gseq_length;
28596 int total_pixel_width;
28597 ptrdiff_t begpos, endpos, ignore;
28599 int vpos, hpos;
28601 b = Fprevious_single_property_change (make_number (charpos + 1),
28602 Qmouse_face, string, Qnil);
28603 if (NILP (b))
28604 begpos = 0;
28605 else
28606 begpos = XINT (b);
28608 e = Fnext_single_property_change (pos, Qmouse_face, string, Qnil);
28609 if (NILP (e))
28610 endpos = SCHARS (string);
28611 else
28612 endpos = XINT (e);
28614 /* Calculate the glyph position GPOS of GLYPH in the
28615 displayed string, relative to the beginning of the
28616 highlighted part of the string.
28618 Note: GPOS is different from CHARPOS. CHARPOS is the
28619 position of GLYPH in the internal string object. A mode
28620 line string format has structures which are converted to
28621 a flattened string by the Emacs Lisp interpreter. The
28622 internal string is an element of those structures. The
28623 displayed string is the flattened string. */
28624 tmp_glyph = row_start_glyph;
28625 while (tmp_glyph < glyph
28626 && (!(EQ (tmp_glyph->object, glyph->object)
28627 && begpos <= tmp_glyph->charpos
28628 && tmp_glyph->charpos < endpos)))
28629 tmp_glyph++;
28630 gpos = glyph - tmp_glyph;
28632 /* Calculate the length GSEQ_LENGTH of the glyph sequence of
28633 the highlighted part of the displayed string to which
28634 GLYPH belongs. Note: GSEQ_LENGTH is different from
28635 SCHARS (STRING), because the latter returns the length of
28636 the internal string. */
28637 for (tmp_glyph = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
28638 tmp_glyph > glyph
28639 && (!(EQ (tmp_glyph->object, glyph->object)
28640 && begpos <= tmp_glyph->charpos
28641 && tmp_glyph->charpos < endpos));
28642 tmp_glyph--)
28644 gseq_length = gpos + (tmp_glyph - glyph) + 1;
28646 /* Calculate the total pixel width of all the glyphs between
28647 the beginning of the highlighted area and GLYPH. */
28648 total_pixel_width = 0;
28649 for (tmp_glyph = glyph - gpos; tmp_glyph != glyph; tmp_glyph++)
28650 total_pixel_width += tmp_glyph->pixel_width;
28652 /* Pre calculation of re-rendering position. Note: X is in
28653 column units here, after the call to mode_line_string or
28654 marginal_area_string. */
28655 hpos = x - gpos;
28656 vpos = (area == ON_MODE_LINE
28657 ? (w->current_matrix)->nrows - 1
28658 : 0);
28660 /* If GLYPH's position is included in the region that is
28661 already drawn in mouse face, we have nothing to do. */
28662 if ( EQ (window, hlinfo->mouse_face_window)
28663 && (!row->reversed_p
28664 ? (hlinfo->mouse_face_beg_col <= hpos
28665 && hpos < hlinfo->mouse_face_end_col)
28666 /* In R2L rows we swap BEG and END, see below. */
28667 : (hlinfo->mouse_face_end_col <= hpos
28668 && hpos < hlinfo->mouse_face_beg_col))
28669 && hlinfo->mouse_face_beg_row == vpos )
28670 return;
28672 if (clear_mouse_face (hlinfo))
28673 cursor = No_Cursor;
28675 if (!row->reversed_p)
28677 hlinfo->mouse_face_beg_col = hpos;
28678 hlinfo->mouse_face_beg_x = original_x_pixel
28679 - (total_pixel_width + dx);
28680 hlinfo->mouse_face_end_col = hpos + gseq_length;
28681 hlinfo->mouse_face_end_x = 0;
28683 else
28685 /* In R2L rows, show_mouse_face expects BEG and END
28686 coordinates to be swapped. */
28687 hlinfo->mouse_face_end_col = hpos;
28688 hlinfo->mouse_face_end_x = original_x_pixel
28689 - (total_pixel_width + dx);
28690 hlinfo->mouse_face_beg_col = hpos + gseq_length;
28691 hlinfo->mouse_face_beg_x = 0;
28694 hlinfo->mouse_face_beg_row = vpos;
28695 hlinfo->mouse_face_end_row = hlinfo->mouse_face_beg_row;
28696 hlinfo->mouse_face_past_end = 0;
28697 hlinfo->mouse_face_window = window;
28699 hlinfo->mouse_face_face_id = face_at_string_position (w, string,
28700 charpos,
28701 0, &ignore,
28702 glyph->face_id,
28704 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
28706 if (NILP (pointer))
28707 pointer = Qhand;
28709 else if ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
28710 clear_mouse_face (hlinfo);
28712 #ifdef HAVE_WINDOW_SYSTEM
28713 if (FRAME_WINDOW_P (f))
28714 define_frame_cursor1 (f, cursor, pointer);
28715 #endif
28719 /* EXPORT:
28720 Take proper action when the mouse has moved to position X, Y on
28721 frame F with regards to highlighting portions of display that have
28722 mouse-face properties. Also de-highlight portions of display where
28723 the mouse was before, set the mouse pointer shape as appropriate
28724 for the mouse coordinates, and activate help echo (tooltips).
28725 X and Y can be negative or out of range. */
28727 void
28728 note_mouse_highlight (struct frame *f, int x, int y)
28730 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
28731 enum window_part part = ON_NOTHING;
28732 Lisp_Object window;
28733 struct window *w;
28734 Cursor cursor = No_Cursor;
28735 Lisp_Object pointer = Qnil; /* Takes precedence over cursor! */
28736 struct buffer *b;
28738 /* When a menu is active, don't highlight because this looks odd. */
28739 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS) || defined (MSDOS)
28740 if (popup_activated ())
28741 return;
28742 #endif
28744 if (!f->glyphs_initialized_p
28745 || f->pointer_invisible)
28746 return;
28748 hlinfo->mouse_face_mouse_x = x;
28749 hlinfo->mouse_face_mouse_y = y;
28750 hlinfo->mouse_face_mouse_frame = f;
28752 if (hlinfo->mouse_face_defer)
28753 return;
28755 /* Which window is that in? */
28756 window = window_from_coordinates (f, x, y, &part, 1);
28758 /* If displaying active text in another window, clear that. */
28759 if (! EQ (window, hlinfo->mouse_face_window)
28760 /* Also clear if we move out of text area in same window. */
28761 || (!NILP (hlinfo->mouse_face_window)
28762 && !NILP (window)
28763 && part != ON_TEXT
28764 && part != ON_MODE_LINE
28765 && part != ON_HEADER_LINE))
28766 clear_mouse_face (hlinfo);
28768 /* Not on a window -> return. */
28769 if (!WINDOWP (window))
28770 return;
28772 /* Reset help_echo_string. It will get recomputed below. */
28773 help_echo_string = Qnil;
28775 /* Convert to window-relative pixel coordinates. */
28776 w = XWINDOW (window);
28777 frame_to_window_pixel_xy (w, &x, &y);
28779 #if defined (HAVE_WINDOW_SYSTEM) && ! defined (USE_GTK) && ! defined (HAVE_NS)
28780 /* Handle tool-bar window differently since it doesn't display a
28781 buffer. */
28782 if (EQ (window, f->tool_bar_window))
28784 note_tool_bar_highlight (f, x, y);
28785 return;
28787 #endif
28789 /* Mouse is on the mode, header line or margin? */
28790 if (part == ON_MODE_LINE || part == ON_HEADER_LINE
28791 || part == ON_LEFT_MARGIN || part == ON_RIGHT_MARGIN)
28793 note_mode_line_or_margin_highlight (window, x, y, part);
28795 #ifdef HAVE_WINDOW_SYSTEM
28796 if (part == ON_LEFT_MARGIN || part == ON_RIGHT_MARGIN)
28798 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
28799 /* Show non-text cursor (Bug#16647). */
28800 goto set_cursor;
28802 else
28803 #endif
28804 return;
28807 #ifdef HAVE_WINDOW_SYSTEM
28808 if (part == ON_VERTICAL_BORDER)
28810 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
28811 help_echo_string = build_string ("drag-mouse-1: resize");
28813 else if (part == ON_RIGHT_DIVIDER)
28815 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
28816 help_echo_string = build_string ("drag-mouse-1: resize");
28818 else if (part == ON_BOTTOM_DIVIDER)
28819 if (! WINDOW_BOTTOMMOST_P (w)
28820 || minibuf_level
28821 || NILP (Vresize_mini_windows))
28823 cursor = FRAME_X_OUTPUT (f)->vertical_drag_cursor;
28824 help_echo_string = build_string ("drag-mouse-1: resize");
28826 else
28827 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
28828 else if (part == ON_LEFT_FRINGE || part == ON_RIGHT_FRINGE
28829 || part == ON_SCROLL_BAR)
28830 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
28831 else
28832 cursor = FRAME_X_OUTPUT (f)->text_cursor;
28833 #endif
28835 /* Are we in a window whose display is up to date?
28836 And verify the buffer's text has not changed. */
28837 b = XBUFFER (w->contents);
28838 if (part == ON_TEXT && w->window_end_valid && !window_outdated (w))
28840 int hpos, vpos, dx, dy, area = LAST_AREA;
28841 ptrdiff_t pos;
28842 struct glyph *glyph;
28843 Lisp_Object object;
28844 Lisp_Object mouse_face = Qnil, position;
28845 Lisp_Object *overlay_vec = NULL;
28846 ptrdiff_t i, noverlays;
28847 struct buffer *obuf;
28848 ptrdiff_t obegv, ozv;
28849 int same_region;
28851 /* Find the glyph under X/Y. */
28852 glyph = x_y_to_hpos_vpos (w, x, y, &hpos, &vpos, &dx, &dy, &area);
28854 #ifdef HAVE_WINDOW_SYSTEM
28855 /* Look for :pointer property on image. */
28856 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
28858 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
28859 if (img != NULL && IMAGEP (img->spec))
28861 Lisp_Object image_map, hotspot;
28862 if ((image_map = Fplist_get (XCDR (img->spec), QCmap),
28863 !NILP (image_map))
28864 && (hotspot = find_hot_spot (image_map,
28865 glyph->slice.img.x + dx,
28866 glyph->slice.img.y + dy),
28867 CONSP (hotspot))
28868 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
28870 Lisp_Object plist;
28872 /* Could check XCAR (hotspot) to see if we enter/leave
28873 this hot-spot.
28874 If so, we could look for mouse-enter, mouse-leave
28875 properties in PLIST (and do something...). */
28876 hotspot = XCDR (hotspot);
28877 if (CONSP (hotspot)
28878 && (plist = XCAR (hotspot), CONSP (plist)))
28880 pointer = Fplist_get (plist, Qpointer);
28881 if (NILP (pointer))
28882 pointer = Qhand;
28883 help_echo_string = Fplist_get (plist, Qhelp_echo);
28884 if (!NILP (help_echo_string))
28886 help_echo_window = window;
28887 help_echo_object = glyph->object;
28888 help_echo_pos = glyph->charpos;
28892 if (NILP (pointer))
28893 pointer = Fplist_get (XCDR (img->spec), QCpointer);
28896 #endif /* HAVE_WINDOW_SYSTEM */
28898 /* Clear mouse face if X/Y not over text. */
28899 if (glyph == NULL
28900 || area != TEXT_AREA
28901 || !MATRIX_ROW_DISPLAYS_TEXT_P (MATRIX_ROW (w->current_matrix, vpos))
28902 /* Glyph's OBJECT is an integer for glyphs inserted by the
28903 display engine for its internal purposes, like truncation
28904 and continuation glyphs and blanks beyond the end of
28905 line's text on text terminals. If we are over such a
28906 glyph, we are not over any text. */
28907 || INTEGERP (glyph->object)
28908 /* R2L rows have a stretch glyph at their front, which
28909 stands for no text, whereas L2R rows have no glyphs at
28910 all beyond the end of text. Treat such stretch glyphs
28911 like we do with NULL glyphs in L2R rows. */
28912 || (MATRIX_ROW (w->current_matrix, vpos)->reversed_p
28913 && glyph == MATRIX_ROW_GLYPH_START (w->current_matrix, vpos)
28914 && glyph->type == STRETCH_GLYPH
28915 && glyph->avoid_cursor_p))
28917 if (clear_mouse_face (hlinfo))
28918 cursor = No_Cursor;
28919 #ifdef HAVE_WINDOW_SYSTEM
28920 if (FRAME_WINDOW_P (f) && NILP (pointer))
28922 if (area != TEXT_AREA)
28923 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
28924 else
28925 pointer = Vvoid_text_area_pointer;
28927 #endif
28928 goto set_cursor;
28931 pos = glyph->charpos;
28932 object = glyph->object;
28933 if (!STRINGP (object) && !BUFFERP (object))
28934 goto set_cursor;
28936 /* If we get an out-of-range value, return now; avoid an error. */
28937 if (BUFFERP (object) && pos > BUF_Z (b))
28938 goto set_cursor;
28940 /* Make the window's buffer temporarily current for
28941 overlays_at and compute_char_face. */
28942 obuf = current_buffer;
28943 current_buffer = b;
28944 obegv = BEGV;
28945 ozv = ZV;
28946 BEGV = BEG;
28947 ZV = Z;
28949 /* Is this char mouse-active or does it have help-echo? */
28950 position = make_number (pos);
28952 if (BUFFERP (object))
28954 /* Put all the overlays we want in a vector in overlay_vec. */
28955 GET_OVERLAYS_AT (pos, overlay_vec, noverlays, NULL, 0);
28956 /* Sort overlays into increasing priority order. */
28957 noverlays = sort_overlays (overlay_vec, noverlays, w);
28959 else
28960 noverlays = 0;
28962 if (NILP (Vmouse_highlight))
28964 clear_mouse_face (hlinfo);
28965 goto check_help_echo;
28968 same_region = coords_in_mouse_face_p (w, hpos, vpos);
28970 if (same_region)
28971 cursor = No_Cursor;
28973 /* Check mouse-face highlighting. */
28974 if (! same_region
28975 /* If there exists an overlay with mouse-face overlapping
28976 the one we are currently highlighting, we have to
28977 check if we enter the overlapping overlay, and then
28978 highlight only that. */
28979 || (OVERLAYP (hlinfo->mouse_face_overlay)
28980 && mouse_face_overlay_overlaps (hlinfo->mouse_face_overlay)))
28982 /* Find the highest priority overlay with a mouse-face. */
28983 Lisp_Object overlay = Qnil;
28984 for (i = noverlays - 1; i >= 0 && NILP (overlay); --i)
28986 mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
28987 if (!NILP (mouse_face))
28988 overlay = overlay_vec[i];
28991 /* If we're highlighting the same overlay as before, there's
28992 no need to do that again. */
28993 if (!NILP (overlay) && EQ (overlay, hlinfo->mouse_face_overlay))
28994 goto check_help_echo;
28995 hlinfo->mouse_face_overlay = overlay;
28997 /* Clear the display of the old active region, if any. */
28998 if (clear_mouse_face (hlinfo))
28999 cursor = No_Cursor;
29001 /* If no overlay applies, get a text property. */
29002 if (NILP (overlay))
29003 mouse_face = Fget_text_property (position, Qmouse_face, object);
29005 /* Next, compute the bounds of the mouse highlighting and
29006 display it. */
29007 if (!NILP (mouse_face) && STRINGP (object))
29009 /* The mouse-highlighting comes from a display string
29010 with a mouse-face. */
29011 Lisp_Object s, e;
29012 ptrdiff_t ignore;
29014 s = Fprevious_single_property_change
29015 (make_number (pos + 1), Qmouse_face, object, Qnil);
29016 e = Fnext_single_property_change
29017 (position, Qmouse_face, object, Qnil);
29018 if (NILP (s))
29019 s = make_number (0);
29020 if (NILP (e))
29021 e = make_number (SCHARS (object));
29022 mouse_face_from_string_pos (w, hlinfo, object,
29023 XINT (s), XINT (e));
29024 hlinfo->mouse_face_past_end = 0;
29025 hlinfo->mouse_face_window = window;
29026 hlinfo->mouse_face_face_id
29027 = face_at_string_position (w, object, pos, 0, &ignore,
29028 glyph->face_id, 1);
29029 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
29030 cursor = No_Cursor;
29032 else
29034 /* The mouse-highlighting, if any, comes from an overlay
29035 or text property in the buffer. */
29036 Lisp_Object buffer IF_LINT (= Qnil);
29037 Lisp_Object disp_string IF_LINT (= Qnil);
29039 if (STRINGP (object))
29041 /* If we are on a display string with no mouse-face,
29042 check if the text under it has one. */
29043 struct glyph_row *r = MATRIX_ROW (w->current_matrix, vpos);
29044 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
29045 pos = string_buffer_position (object, start);
29046 if (pos > 0)
29048 mouse_face = get_char_property_and_overlay
29049 (make_number (pos), Qmouse_face, w->contents, &overlay);
29050 buffer = w->contents;
29051 disp_string = object;
29054 else
29056 buffer = object;
29057 disp_string = Qnil;
29060 if (!NILP (mouse_face))
29062 Lisp_Object before, after;
29063 Lisp_Object before_string, after_string;
29064 /* To correctly find the limits of mouse highlight
29065 in a bidi-reordered buffer, we must not use the
29066 optimization of limiting the search in
29067 previous-single-property-change and
29068 next-single-property-change, because
29069 rows_from_pos_range needs the real start and end
29070 positions to DTRT in this case. That's because
29071 the first row visible in a window does not
29072 necessarily display the character whose position
29073 is the smallest. */
29074 Lisp_Object lim1
29075 = NILP (BVAR (XBUFFER (buffer), bidi_display_reordering))
29076 ? Fmarker_position (w->start)
29077 : Qnil;
29078 Lisp_Object lim2
29079 = NILP (BVAR (XBUFFER (buffer), bidi_display_reordering))
29080 ? make_number (BUF_Z (XBUFFER (buffer))
29081 - w->window_end_pos)
29082 : Qnil;
29084 if (NILP (overlay))
29086 /* Handle the text property case. */
29087 before = Fprevious_single_property_change
29088 (make_number (pos + 1), Qmouse_face, buffer, lim1);
29089 after = Fnext_single_property_change
29090 (make_number (pos), Qmouse_face, buffer, lim2);
29091 before_string = after_string = Qnil;
29093 else
29095 /* Handle the overlay case. */
29096 before = Foverlay_start (overlay);
29097 after = Foverlay_end (overlay);
29098 before_string = Foverlay_get (overlay, Qbefore_string);
29099 after_string = Foverlay_get (overlay, Qafter_string);
29101 if (!STRINGP (before_string)) before_string = Qnil;
29102 if (!STRINGP (after_string)) after_string = Qnil;
29105 mouse_face_from_buffer_pos (window, hlinfo, pos,
29106 NILP (before)
29108 : XFASTINT (before),
29109 NILP (after)
29110 ? BUF_Z (XBUFFER (buffer))
29111 : XFASTINT (after),
29112 before_string, after_string,
29113 disp_string);
29114 cursor = No_Cursor;
29119 check_help_echo:
29121 /* Look for a `help-echo' property. */
29122 if (NILP (help_echo_string)) {
29123 Lisp_Object help, overlay;
29125 /* Check overlays first. */
29126 help = overlay = Qnil;
29127 for (i = noverlays - 1; i >= 0 && NILP (help); --i)
29129 overlay = overlay_vec[i];
29130 help = Foverlay_get (overlay, Qhelp_echo);
29133 if (!NILP (help))
29135 help_echo_string = help;
29136 help_echo_window = window;
29137 help_echo_object = overlay;
29138 help_echo_pos = pos;
29140 else
29142 Lisp_Object obj = glyph->object;
29143 ptrdiff_t charpos = glyph->charpos;
29145 /* Try text properties. */
29146 if (STRINGP (obj)
29147 && charpos >= 0
29148 && charpos < SCHARS (obj))
29150 help = Fget_text_property (make_number (charpos),
29151 Qhelp_echo, obj);
29152 if (NILP (help))
29154 /* If the string itself doesn't specify a help-echo,
29155 see if the buffer text ``under'' it does. */
29156 struct glyph_row *r
29157 = MATRIX_ROW (w->current_matrix, vpos);
29158 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
29159 ptrdiff_t p = string_buffer_position (obj, start);
29160 if (p > 0)
29162 help = Fget_char_property (make_number (p),
29163 Qhelp_echo, w->contents);
29164 if (!NILP (help))
29166 charpos = p;
29167 obj = w->contents;
29172 else if (BUFFERP (obj)
29173 && charpos >= BEGV
29174 && charpos < ZV)
29175 help = Fget_text_property (make_number (charpos), Qhelp_echo,
29176 obj);
29178 if (!NILP (help))
29180 help_echo_string = help;
29181 help_echo_window = window;
29182 help_echo_object = obj;
29183 help_echo_pos = charpos;
29188 #ifdef HAVE_WINDOW_SYSTEM
29189 /* Look for a `pointer' property. */
29190 if (FRAME_WINDOW_P (f) && NILP (pointer))
29192 /* Check overlays first. */
29193 for (i = noverlays - 1; i >= 0 && NILP (pointer); --i)
29194 pointer = Foverlay_get (overlay_vec[i], Qpointer);
29196 if (NILP (pointer))
29198 Lisp_Object obj = glyph->object;
29199 ptrdiff_t charpos = glyph->charpos;
29201 /* Try text properties. */
29202 if (STRINGP (obj)
29203 && charpos >= 0
29204 && charpos < SCHARS (obj))
29206 pointer = Fget_text_property (make_number (charpos),
29207 Qpointer, obj);
29208 if (NILP (pointer))
29210 /* If the string itself doesn't specify a pointer,
29211 see if the buffer text ``under'' it does. */
29212 struct glyph_row *r
29213 = MATRIX_ROW (w->current_matrix, vpos);
29214 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
29215 ptrdiff_t p = string_buffer_position (obj, start);
29216 if (p > 0)
29217 pointer = Fget_char_property (make_number (p),
29218 Qpointer, w->contents);
29221 else if (BUFFERP (obj)
29222 && charpos >= BEGV
29223 && charpos < ZV)
29224 pointer = Fget_text_property (make_number (charpos),
29225 Qpointer, obj);
29228 #endif /* HAVE_WINDOW_SYSTEM */
29230 BEGV = obegv;
29231 ZV = ozv;
29232 current_buffer = obuf;
29235 set_cursor:
29237 #ifdef HAVE_WINDOW_SYSTEM
29238 if (FRAME_WINDOW_P (f))
29239 define_frame_cursor1 (f, cursor, pointer);
29240 #else
29241 /* This is here to prevent a compiler error, about "label at end of
29242 compound statement". */
29243 return;
29244 #endif
29248 /* EXPORT for RIF:
29249 Clear any mouse-face on window W. This function is part of the
29250 redisplay interface, and is called from try_window_id and similar
29251 functions to ensure the mouse-highlight is off. */
29253 void
29254 x_clear_window_mouse_face (struct window *w)
29256 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (XFRAME (w->frame));
29257 Lisp_Object window;
29259 block_input ();
29260 XSETWINDOW (window, w);
29261 if (EQ (window, hlinfo->mouse_face_window))
29262 clear_mouse_face (hlinfo);
29263 unblock_input ();
29267 /* EXPORT:
29268 Just discard the mouse face information for frame F, if any.
29269 This is used when the size of F is changed. */
29271 void
29272 cancel_mouse_face (struct frame *f)
29274 Lisp_Object window;
29275 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
29277 window = hlinfo->mouse_face_window;
29278 if (! NILP (window) && XFRAME (XWINDOW (window)->frame) == f)
29279 reset_mouse_highlight (hlinfo);
29284 /***********************************************************************
29285 Exposure Events
29286 ***********************************************************************/
29288 #ifdef HAVE_WINDOW_SYSTEM
29290 /* Redraw the part of glyph row area AREA of glyph row ROW on window W
29291 which intersects rectangle R. R is in window-relative coordinates. */
29293 static void
29294 expose_area (struct window *w, struct glyph_row *row, XRectangle *r,
29295 enum glyph_row_area area)
29297 struct glyph *first = row->glyphs[area];
29298 struct glyph *end = row->glyphs[area] + row->used[area];
29299 struct glyph *last;
29300 int first_x, start_x, x;
29302 if (area == TEXT_AREA && row->fill_line_p)
29303 /* If row extends face to end of line write the whole line. */
29304 draw_glyphs (w, 0, row, area,
29305 0, row->used[area],
29306 DRAW_NORMAL_TEXT, 0);
29307 else
29309 /* Set START_X to the window-relative start position for drawing glyphs of
29310 AREA. The first glyph of the text area can be partially visible.
29311 The first glyphs of other areas cannot. */
29312 start_x = window_box_left_offset (w, area);
29313 x = start_x;
29314 if (area == TEXT_AREA)
29315 x += row->x;
29317 /* Find the first glyph that must be redrawn. */
29318 while (first < end
29319 && x + first->pixel_width < r->x)
29321 x += first->pixel_width;
29322 ++first;
29325 /* Find the last one. */
29326 last = first;
29327 first_x = x;
29328 while (last < end
29329 && x < r->x + r->width)
29331 x += last->pixel_width;
29332 ++last;
29335 /* Repaint. */
29336 if (last > first)
29337 draw_glyphs (w, first_x - start_x, row, area,
29338 first - row->glyphs[area], last - row->glyphs[area],
29339 DRAW_NORMAL_TEXT, 0);
29344 /* Redraw the parts of the glyph row ROW on window W intersecting
29345 rectangle R. R is in window-relative coordinates. Value is
29346 non-zero if mouse-face was overwritten. */
29348 static int
29349 expose_line (struct window *w, struct glyph_row *row, XRectangle *r)
29351 eassert (row->enabled_p);
29353 if (row->mode_line_p || w->pseudo_window_p)
29354 draw_glyphs (w, 0, row, TEXT_AREA,
29355 0, row->used[TEXT_AREA],
29356 DRAW_NORMAL_TEXT, 0);
29357 else
29359 if (row->used[LEFT_MARGIN_AREA])
29360 expose_area (w, row, r, LEFT_MARGIN_AREA);
29361 if (row->used[TEXT_AREA])
29362 expose_area (w, row, r, TEXT_AREA);
29363 if (row->used[RIGHT_MARGIN_AREA])
29364 expose_area (w, row, r, RIGHT_MARGIN_AREA);
29365 draw_row_fringe_bitmaps (w, row);
29368 return row->mouse_face_p;
29372 /* Redraw those parts of glyphs rows during expose event handling that
29373 overlap other rows. Redrawing of an exposed line writes over parts
29374 of lines overlapping that exposed line; this function fixes that.
29376 W is the window being exposed. FIRST_OVERLAPPING_ROW is the first
29377 row in W's current matrix that is exposed and overlaps other rows.
29378 LAST_OVERLAPPING_ROW is the last such row. */
29380 static void
29381 expose_overlaps (struct window *w,
29382 struct glyph_row *first_overlapping_row,
29383 struct glyph_row *last_overlapping_row,
29384 XRectangle *r)
29386 struct glyph_row *row;
29388 for (row = first_overlapping_row; row <= last_overlapping_row; ++row)
29389 if (row->overlapping_p)
29391 eassert (row->enabled_p && !row->mode_line_p);
29393 row->clip = r;
29394 if (row->used[LEFT_MARGIN_AREA])
29395 x_fix_overlapping_area (w, row, LEFT_MARGIN_AREA, OVERLAPS_BOTH);
29397 if (row->used[TEXT_AREA])
29398 x_fix_overlapping_area (w, row, TEXT_AREA, OVERLAPS_BOTH);
29400 if (row->used[RIGHT_MARGIN_AREA])
29401 x_fix_overlapping_area (w, row, RIGHT_MARGIN_AREA, OVERLAPS_BOTH);
29402 row->clip = NULL;
29407 /* Return non-zero if W's cursor intersects rectangle R. */
29409 static int
29410 phys_cursor_in_rect_p (struct window *w, XRectangle *r)
29412 XRectangle cr, result;
29413 struct glyph *cursor_glyph;
29414 struct glyph_row *row;
29416 if (w->phys_cursor.vpos >= 0
29417 && w->phys_cursor.vpos < w->current_matrix->nrows
29418 && (row = MATRIX_ROW (w->current_matrix, w->phys_cursor.vpos),
29419 row->enabled_p)
29420 && row->cursor_in_fringe_p)
29422 /* Cursor is in the fringe. */
29423 cr.x = window_box_right_offset (w,
29424 (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
29425 ? RIGHT_MARGIN_AREA
29426 : TEXT_AREA));
29427 cr.y = row->y;
29428 cr.width = WINDOW_RIGHT_FRINGE_WIDTH (w);
29429 cr.height = row->height;
29430 return x_intersect_rectangles (&cr, r, &result);
29433 cursor_glyph = get_phys_cursor_glyph (w);
29434 if (cursor_glyph)
29436 /* r is relative to W's box, but w->phys_cursor.x is relative
29437 to left edge of W's TEXT area. Adjust it. */
29438 cr.x = window_box_left_offset (w, TEXT_AREA) + w->phys_cursor.x;
29439 cr.y = w->phys_cursor.y;
29440 cr.width = cursor_glyph->pixel_width;
29441 cr.height = w->phys_cursor_height;
29442 /* ++KFS: W32 version used W32-specific IntersectRect here, but
29443 I assume the effect is the same -- and this is portable. */
29444 return x_intersect_rectangles (&cr, r, &result);
29446 /* If we don't understand the format, pretend we're not in the hot-spot. */
29447 return 0;
29451 /* EXPORT:
29452 Draw a vertical window border to the right of window W if W doesn't
29453 have vertical scroll bars. */
29455 void
29456 x_draw_vertical_border (struct window *w)
29458 struct frame *f = XFRAME (WINDOW_FRAME (w));
29460 /* We could do better, if we knew what type of scroll-bar the adjacent
29461 windows (on either side) have... But we don't :-(
29462 However, I think this works ok. ++KFS 2003-04-25 */
29464 /* Redraw borders between horizontally adjacent windows. Don't
29465 do it for frames with vertical scroll bars because either the
29466 right scroll bar of a window, or the left scroll bar of its
29467 neighbor will suffice as a border. */
29468 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f) || FRAME_RIGHT_DIVIDER_WIDTH (f))
29469 return;
29471 /* Note: It is necessary to redraw both the left and the right
29472 borders, for when only this single window W is being
29473 redisplayed. */
29474 if (!WINDOW_RIGHTMOST_P (w)
29475 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w))
29477 int x0, x1, y0, y1;
29479 window_box_edges (w, &x0, &y0, &x1, &y1);
29480 y1 -= 1;
29482 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
29483 x1 -= 1;
29485 FRAME_RIF (f)->draw_vertical_window_border (w, x1, y0, y1);
29488 if (!WINDOW_LEFTMOST_P (w)
29489 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w))
29491 int x0, x1, y0, y1;
29493 window_box_edges (w, &x0, &y0, &x1, &y1);
29494 y1 -= 1;
29496 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
29497 x0 -= 1;
29499 FRAME_RIF (f)->draw_vertical_window_border (w, x0, y0, y1);
29504 /* Draw window dividers for window W. */
29506 void
29507 x_draw_right_divider (struct window *w)
29509 struct frame *f = WINDOW_XFRAME (w);
29511 if (w->mini || w->pseudo_window_p)
29512 return;
29513 else if (WINDOW_RIGHT_DIVIDER_WIDTH (w))
29515 int x0 = WINDOW_RIGHT_EDGE_X (w) - WINDOW_RIGHT_DIVIDER_WIDTH (w);
29516 int x1 = WINDOW_RIGHT_EDGE_X (w);
29517 int y0 = WINDOW_TOP_EDGE_Y (w);
29518 /* The bottom divider prevails. */
29519 int y1 = WINDOW_BOTTOM_EDGE_Y (w) - WINDOW_BOTTOM_DIVIDER_WIDTH (w);
29521 FRAME_RIF (f)->draw_window_divider (w, x0, x1, y0, y1);
29525 static void
29526 x_draw_bottom_divider (struct window *w)
29528 struct frame *f = XFRAME (WINDOW_FRAME (w));
29530 if (w->mini || w->pseudo_window_p)
29531 return;
29532 else if (WINDOW_BOTTOM_DIVIDER_WIDTH (w))
29534 int x0 = WINDOW_LEFT_EDGE_X (w);
29535 int x1 = WINDOW_RIGHT_EDGE_X (w);
29536 int y0 = WINDOW_BOTTOM_EDGE_Y (w) - WINDOW_BOTTOM_DIVIDER_WIDTH (w);
29537 int y1 = WINDOW_BOTTOM_EDGE_Y (w);
29539 FRAME_RIF (f)->draw_window_divider (w, x0, x1, y0, y1);
29543 /* Redraw the part of window W intersection rectangle FR. Pixel
29544 coordinates in FR are frame-relative. Call this function with
29545 input blocked. Value is non-zero if the exposure overwrites
29546 mouse-face. */
29548 static int
29549 expose_window (struct window *w, XRectangle *fr)
29551 struct frame *f = XFRAME (w->frame);
29552 XRectangle wr, r;
29553 int mouse_face_overwritten_p = 0;
29555 /* If window is not yet fully initialized, do nothing. This can
29556 happen when toolkit scroll bars are used and a window is split.
29557 Reconfiguring the scroll bar will generate an expose for a newly
29558 created window. */
29559 if (w->current_matrix == NULL)
29560 return 0;
29562 /* When we're currently updating the window, display and current
29563 matrix usually don't agree. Arrange for a thorough display
29564 later. */
29565 if (w->must_be_updated_p)
29567 SET_FRAME_GARBAGED (f);
29568 return 0;
29571 /* Frame-relative pixel rectangle of W. */
29572 wr.x = WINDOW_LEFT_EDGE_X (w);
29573 wr.y = WINDOW_TOP_EDGE_Y (w);
29574 wr.width = WINDOW_PIXEL_WIDTH (w);
29575 wr.height = WINDOW_PIXEL_HEIGHT (w);
29577 if (x_intersect_rectangles (fr, &wr, &r))
29579 int yb = window_text_bottom_y (w);
29580 struct glyph_row *row;
29581 int cursor_cleared_p, phys_cursor_on_p;
29582 struct glyph_row *first_overlapping_row, *last_overlapping_row;
29584 TRACE ((stderr, "expose_window (%d, %d, %d, %d)\n",
29585 r.x, r.y, r.width, r.height));
29587 /* Convert to window coordinates. */
29588 r.x -= WINDOW_LEFT_EDGE_X (w);
29589 r.y -= WINDOW_TOP_EDGE_Y (w);
29591 /* Turn off the cursor. */
29592 if (!w->pseudo_window_p
29593 && phys_cursor_in_rect_p (w, &r))
29595 x_clear_cursor (w);
29596 cursor_cleared_p = 1;
29598 else
29599 cursor_cleared_p = 0;
29601 /* If the row containing the cursor extends face to end of line,
29602 then expose_area might overwrite the cursor outside the
29603 rectangle and thus notice_overwritten_cursor might clear
29604 w->phys_cursor_on_p. We remember the original value and
29605 check later if it is changed. */
29606 phys_cursor_on_p = w->phys_cursor_on_p;
29608 /* Update lines intersecting rectangle R. */
29609 first_overlapping_row = last_overlapping_row = NULL;
29610 for (row = w->current_matrix->rows;
29611 row->enabled_p;
29612 ++row)
29614 int y0 = row->y;
29615 int y1 = MATRIX_ROW_BOTTOM_Y (row);
29617 if ((y0 >= r.y && y0 < r.y + r.height)
29618 || (y1 > r.y && y1 < r.y + r.height)
29619 || (r.y >= y0 && r.y < y1)
29620 || (r.y + r.height > y0 && r.y + r.height < y1))
29622 /* A header line may be overlapping, but there is no need
29623 to fix overlapping areas for them. KFS 2005-02-12 */
29624 if (row->overlapping_p && !row->mode_line_p)
29626 if (first_overlapping_row == NULL)
29627 first_overlapping_row = row;
29628 last_overlapping_row = row;
29631 row->clip = fr;
29632 if (expose_line (w, row, &r))
29633 mouse_face_overwritten_p = 1;
29634 row->clip = NULL;
29636 else if (row->overlapping_p)
29638 /* We must redraw a row overlapping the exposed area. */
29639 if (y0 < r.y
29640 ? y0 + row->phys_height > r.y
29641 : y0 + row->ascent - row->phys_ascent < r.y +r.height)
29643 if (first_overlapping_row == NULL)
29644 first_overlapping_row = row;
29645 last_overlapping_row = row;
29649 if (y1 >= yb)
29650 break;
29653 /* Display the mode line if there is one. */
29654 if (WINDOW_WANTS_MODELINE_P (w)
29655 && (row = MATRIX_MODE_LINE_ROW (w->current_matrix),
29656 row->enabled_p)
29657 && row->y < r.y + r.height)
29659 if (expose_line (w, row, &r))
29660 mouse_face_overwritten_p = 1;
29663 if (!w->pseudo_window_p)
29665 /* Fix the display of overlapping rows. */
29666 if (first_overlapping_row)
29667 expose_overlaps (w, first_overlapping_row, last_overlapping_row,
29668 fr);
29670 /* Draw border between windows. */
29671 if (WINDOW_RIGHT_DIVIDER_WIDTH (w))
29672 x_draw_right_divider (w);
29673 else
29674 x_draw_vertical_border (w);
29676 if (WINDOW_BOTTOM_DIVIDER_WIDTH (w))
29677 x_draw_bottom_divider (w);
29679 /* Turn the cursor on again. */
29680 if (cursor_cleared_p
29681 || (phys_cursor_on_p && !w->phys_cursor_on_p))
29682 update_window_cursor (w, 1);
29686 return mouse_face_overwritten_p;
29691 /* Redraw (parts) of all windows in the window tree rooted at W that
29692 intersect R. R contains frame pixel coordinates. Value is
29693 non-zero if the exposure overwrites mouse-face. */
29695 static int
29696 expose_window_tree (struct window *w, XRectangle *r)
29698 struct frame *f = XFRAME (w->frame);
29699 int mouse_face_overwritten_p = 0;
29701 while (w && !FRAME_GARBAGED_P (f))
29703 if (WINDOWP (w->contents))
29704 mouse_face_overwritten_p
29705 |= expose_window_tree (XWINDOW (w->contents), r);
29706 else
29707 mouse_face_overwritten_p |= expose_window (w, r);
29709 w = NILP (w->next) ? NULL : XWINDOW (w->next);
29712 return mouse_face_overwritten_p;
29716 /* EXPORT:
29717 Redisplay an exposed area of frame F. X and Y are the upper-left
29718 corner of the exposed rectangle. W and H are width and height of
29719 the exposed area. All are pixel values. W or H zero means redraw
29720 the entire frame. */
29722 void
29723 expose_frame (struct frame *f, int x, int y, int w, int h)
29725 XRectangle r;
29726 int mouse_face_overwritten_p = 0;
29728 TRACE ((stderr, "expose_frame "));
29730 /* No need to redraw if frame will be redrawn soon. */
29731 if (FRAME_GARBAGED_P (f))
29733 TRACE ((stderr, " garbaged\n"));
29734 return;
29737 /* If basic faces haven't been realized yet, there is no point in
29738 trying to redraw anything. This can happen when we get an expose
29739 event while Emacs is starting, e.g. by moving another window. */
29740 if (FRAME_FACE_CACHE (f) == NULL
29741 || FRAME_FACE_CACHE (f)->used < BASIC_FACE_ID_SENTINEL)
29743 TRACE ((stderr, " no faces\n"));
29744 return;
29747 if (w == 0 || h == 0)
29749 r.x = r.y = 0;
29750 r.width = FRAME_COLUMN_WIDTH (f) * FRAME_COLS (f);
29751 r.height = FRAME_LINE_HEIGHT (f) * FRAME_LINES (f);
29753 else
29755 r.x = x;
29756 r.y = y;
29757 r.width = w;
29758 r.height = h;
29761 TRACE ((stderr, "(%d, %d, %d, %d)\n", r.x, r.y, r.width, r.height));
29762 mouse_face_overwritten_p = expose_window_tree (XWINDOW (f->root_window), &r);
29764 #if ! defined (USE_GTK) && ! defined (HAVE_NS)
29765 if (WINDOWP (f->tool_bar_window))
29766 mouse_face_overwritten_p
29767 |= expose_window (XWINDOW (f->tool_bar_window), &r);
29768 #endif
29770 #ifdef HAVE_X_WINDOWS
29771 #ifndef MSDOS
29772 #if ! defined (USE_X_TOOLKIT) && ! defined (USE_GTK)
29773 if (WINDOWP (f->menu_bar_window))
29774 mouse_face_overwritten_p
29775 |= expose_window (XWINDOW (f->menu_bar_window), &r);
29776 #endif /* not USE_X_TOOLKIT and not USE_GTK */
29777 #endif
29778 #endif
29780 /* Some window managers support a focus-follows-mouse style with
29781 delayed raising of frames. Imagine a partially obscured frame,
29782 and moving the mouse into partially obscured mouse-face on that
29783 frame. The visible part of the mouse-face will be highlighted,
29784 then the WM raises the obscured frame. With at least one WM, KDE
29785 2.1, Emacs is not getting any event for the raising of the frame
29786 (even tried with SubstructureRedirectMask), only Expose events.
29787 These expose events will draw text normally, i.e. not
29788 highlighted. Which means we must redo the highlight here.
29789 Subsume it under ``we love X''. --gerd 2001-08-15 */
29790 /* Included in Windows version because Windows most likely does not
29791 do the right thing if any third party tool offers
29792 focus-follows-mouse with delayed raise. --jason 2001-10-12 */
29793 if (mouse_face_overwritten_p && !FRAME_GARBAGED_P (f))
29795 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
29796 if (f == hlinfo->mouse_face_mouse_frame)
29798 int mouse_x = hlinfo->mouse_face_mouse_x;
29799 int mouse_y = hlinfo->mouse_face_mouse_y;
29800 clear_mouse_face (hlinfo);
29801 note_mouse_highlight (f, mouse_x, mouse_y);
29807 /* EXPORT:
29808 Determine the intersection of two rectangles R1 and R2. Return
29809 the intersection in *RESULT. Value is non-zero if RESULT is not
29810 empty. */
29813 x_intersect_rectangles (XRectangle *r1, XRectangle *r2, XRectangle *result)
29815 XRectangle *left, *right;
29816 XRectangle *upper, *lower;
29817 int intersection_p = 0;
29819 /* Rearrange so that R1 is the left-most rectangle. */
29820 if (r1->x < r2->x)
29821 left = r1, right = r2;
29822 else
29823 left = r2, right = r1;
29825 /* X0 of the intersection is right.x0, if this is inside R1,
29826 otherwise there is no intersection. */
29827 if (right->x <= left->x + left->width)
29829 result->x = right->x;
29831 /* The right end of the intersection is the minimum of
29832 the right ends of left and right. */
29833 result->width = (min (left->x + left->width, right->x + right->width)
29834 - result->x);
29836 /* Same game for Y. */
29837 if (r1->y < r2->y)
29838 upper = r1, lower = r2;
29839 else
29840 upper = r2, lower = r1;
29842 /* The upper end of the intersection is lower.y0, if this is inside
29843 of upper. Otherwise, there is no intersection. */
29844 if (lower->y <= upper->y + upper->height)
29846 result->y = lower->y;
29848 /* The lower end of the intersection is the minimum of the lower
29849 ends of upper and lower. */
29850 result->height = (min (lower->y + lower->height,
29851 upper->y + upper->height)
29852 - result->y);
29853 intersection_p = 1;
29857 return intersection_p;
29860 #endif /* HAVE_WINDOW_SYSTEM */
29863 /***********************************************************************
29864 Initialization
29865 ***********************************************************************/
29867 void
29868 syms_of_xdisp (void)
29870 Vwith_echo_area_save_vector = Qnil;
29871 staticpro (&Vwith_echo_area_save_vector);
29873 Vmessage_stack = Qnil;
29874 staticpro (&Vmessage_stack);
29876 DEFSYM (Qinhibit_redisplay, "inhibit-redisplay");
29877 DEFSYM (Qredisplay_internal, "redisplay_internal (C function)");
29879 message_dolog_marker1 = Fmake_marker ();
29880 staticpro (&message_dolog_marker1);
29881 message_dolog_marker2 = Fmake_marker ();
29882 staticpro (&message_dolog_marker2);
29883 message_dolog_marker3 = Fmake_marker ();
29884 staticpro (&message_dolog_marker3);
29886 #ifdef GLYPH_DEBUG
29887 defsubr (&Sdump_frame_glyph_matrix);
29888 defsubr (&Sdump_glyph_matrix);
29889 defsubr (&Sdump_glyph_row);
29890 defsubr (&Sdump_tool_bar_row);
29891 defsubr (&Strace_redisplay);
29892 defsubr (&Strace_to_stderr);
29893 #endif
29894 #ifdef HAVE_WINDOW_SYSTEM
29895 defsubr (&Stool_bar_height);
29896 defsubr (&Slookup_image_map);
29897 #endif
29898 defsubr (&Sline_pixel_height);
29899 defsubr (&Sformat_mode_line);
29900 defsubr (&Sinvisible_p);
29901 defsubr (&Scurrent_bidi_paragraph_direction);
29902 defsubr (&Swindow_text_pixel_size);
29903 defsubr (&Smove_point_visually);
29905 DEFSYM (Qmenu_bar_update_hook, "menu-bar-update-hook");
29906 DEFSYM (Qoverriding_terminal_local_map, "overriding-terminal-local-map");
29907 DEFSYM (Qoverriding_local_map, "overriding-local-map");
29908 DEFSYM (Qwindow_scroll_functions, "window-scroll-functions");
29909 DEFSYM (Qwindow_text_change_functions, "window-text-change-functions");
29910 DEFSYM (Qredisplay_end_trigger_functions, "redisplay-end-trigger-functions");
29911 DEFSYM (Qinhibit_point_motion_hooks, "inhibit-point-motion-hooks");
29912 DEFSYM (Qeval, "eval");
29913 DEFSYM (QCdata, ":data");
29914 DEFSYM (Qdisplay, "display");
29915 DEFSYM (Qspace_width, "space-width");
29916 DEFSYM (Qraise, "raise");
29917 DEFSYM (Qslice, "slice");
29918 DEFSYM (Qspace, "space");
29919 DEFSYM (Qmargin, "margin");
29920 DEFSYM (Qpointer, "pointer");
29921 DEFSYM (Qleft_margin, "left-margin");
29922 DEFSYM (Qright_margin, "right-margin");
29923 DEFSYM (Qcenter, "center");
29924 DEFSYM (Qline_height, "line-height");
29925 DEFSYM (QCalign_to, ":align-to");
29926 DEFSYM (QCrelative_width, ":relative-width");
29927 DEFSYM (QCrelative_height, ":relative-height");
29928 DEFSYM (QCeval, ":eval");
29929 DEFSYM (QCpropertize, ":propertize");
29930 DEFSYM (QCfile, ":file");
29931 DEFSYM (Qfontified, "fontified");
29932 DEFSYM (Qfontification_functions, "fontification-functions");
29933 DEFSYM (Qtrailing_whitespace, "trailing-whitespace");
29934 DEFSYM (Qescape_glyph, "escape-glyph");
29935 DEFSYM (Qnobreak_space, "nobreak-space");
29936 DEFSYM (Qimage, "image");
29937 DEFSYM (Qtext, "text");
29938 DEFSYM (Qboth, "both");
29939 DEFSYM (Qboth_horiz, "both-horiz");
29940 DEFSYM (Qtext_image_horiz, "text-image-horiz");
29941 DEFSYM (QCmap, ":map");
29942 DEFSYM (QCpointer, ":pointer");
29943 DEFSYM (Qrect, "rect");
29944 DEFSYM (Qcircle, "circle");
29945 DEFSYM (Qpoly, "poly");
29946 DEFSYM (Qmessage_truncate_lines, "message-truncate-lines");
29947 DEFSYM (Qgrow_only, "grow-only");
29948 DEFSYM (Qinhibit_menubar_update, "inhibit-menubar-update");
29949 DEFSYM (Qinhibit_eval_during_redisplay, "inhibit-eval-during-redisplay");
29950 DEFSYM (Qposition, "position");
29951 DEFSYM (Qbuffer_position, "buffer-position");
29952 DEFSYM (Qobject, "object");
29953 DEFSYM (Qbar, "bar");
29954 DEFSYM (Qhbar, "hbar");
29955 DEFSYM (Qbox, "box");
29956 DEFSYM (Qhollow, "hollow");
29957 DEFSYM (Qhand, "hand");
29958 DEFSYM (Qarrow, "arrow");
29959 DEFSYM (Qinhibit_free_realized_faces, "inhibit-free-realized-faces");
29961 list_of_error = list1 (list2 (intern_c_string ("error"),
29962 intern_c_string ("void-variable")));
29963 staticpro (&list_of_error);
29965 DEFSYM (Qlast_arrow_position, "last-arrow-position");
29966 DEFSYM (Qlast_arrow_string, "last-arrow-string");
29967 DEFSYM (Qoverlay_arrow_string, "overlay-arrow-string");
29968 DEFSYM (Qoverlay_arrow_bitmap, "overlay-arrow-bitmap");
29970 echo_buffer[0] = echo_buffer[1] = Qnil;
29971 staticpro (&echo_buffer[0]);
29972 staticpro (&echo_buffer[1]);
29974 echo_area_buffer[0] = echo_area_buffer[1] = Qnil;
29975 staticpro (&echo_area_buffer[0]);
29976 staticpro (&echo_area_buffer[1]);
29978 Vmessages_buffer_name = build_pure_c_string ("*Messages*");
29979 staticpro (&Vmessages_buffer_name);
29981 mode_line_proptrans_alist = Qnil;
29982 staticpro (&mode_line_proptrans_alist);
29983 mode_line_string_list = Qnil;
29984 staticpro (&mode_line_string_list);
29985 mode_line_string_face = Qnil;
29986 staticpro (&mode_line_string_face);
29987 mode_line_string_face_prop = Qnil;
29988 staticpro (&mode_line_string_face_prop);
29989 Vmode_line_unwind_vector = Qnil;
29990 staticpro (&Vmode_line_unwind_vector);
29992 DEFSYM (Qmode_line_default_help_echo, "mode-line-default-help-echo");
29994 help_echo_string = Qnil;
29995 staticpro (&help_echo_string);
29996 help_echo_object = Qnil;
29997 staticpro (&help_echo_object);
29998 help_echo_window = Qnil;
29999 staticpro (&help_echo_window);
30000 previous_help_echo_string = Qnil;
30001 staticpro (&previous_help_echo_string);
30002 help_echo_pos = -1;
30004 DEFSYM (Qright_to_left, "right-to-left");
30005 DEFSYM (Qleft_to_right, "left-to-right");
30007 #ifdef HAVE_WINDOW_SYSTEM
30008 DEFVAR_BOOL ("x-stretch-cursor", x_stretch_cursor_p,
30009 doc: /* Non-nil means draw block cursor as wide as the glyph under it.
30010 For example, if a block cursor is over a tab, it will be drawn as
30011 wide as that tab on the display. */);
30012 x_stretch_cursor_p = 0;
30013 #endif
30015 DEFVAR_LISP ("show-trailing-whitespace", Vshow_trailing_whitespace,
30016 doc: /* Non-nil means highlight trailing whitespace.
30017 The face used for trailing whitespace is `trailing-whitespace'. */);
30018 Vshow_trailing_whitespace = Qnil;
30020 DEFVAR_LISP ("nobreak-char-display", Vnobreak_char_display,
30021 doc: /* Control highlighting of non-ASCII space and hyphen chars.
30022 If the value is t, Emacs highlights non-ASCII chars which have the
30023 same appearance as an ASCII space or hyphen, using the `nobreak-space'
30024 or `escape-glyph' face respectively.
30026 U+00A0 (no-break space), U+00AD (soft hyphen), U+2010 (hyphen), and
30027 U+2011 (non-breaking hyphen) are affected.
30029 Any other non-nil value means to display these characters as a escape
30030 glyph followed by an ordinary space or hyphen.
30032 A value of nil means no special handling of these characters. */);
30033 Vnobreak_char_display = Qt;
30035 DEFVAR_LISP ("void-text-area-pointer", Vvoid_text_area_pointer,
30036 doc: /* The pointer shape to show in void text areas.
30037 A value of nil means to show the text pointer. Other options are `arrow',
30038 `text', `hand', `vdrag', `hdrag', `modeline', and `hourglass'. */);
30039 Vvoid_text_area_pointer = Qarrow;
30041 DEFVAR_LISP ("inhibit-redisplay", Vinhibit_redisplay,
30042 doc: /* Non-nil means don't actually do any redisplay.
30043 This is used for internal purposes. */);
30044 Vinhibit_redisplay = Qnil;
30046 DEFVAR_LISP ("global-mode-string", Vglobal_mode_string,
30047 doc: /* String (or mode line construct) included (normally) in `mode-line-format'. */);
30048 Vglobal_mode_string = Qnil;
30050 DEFVAR_LISP ("overlay-arrow-position", Voverlay_arrow_position,
30051 doc: /* Marker for where to display an arrow on top of the buffer text.
30052 This must be the beginning of a line in order to work.
30053 See also `overlay-arrow-string'. */);
30054 Voverlay_arrow_position = Qnil;
30056 DEFVAR_LISP ("overlay-arrow-string", Voverlay_arrow_string,
30057 doc: /* String to display as an arrow in non-window frames.
30058 See also `overlay-arrow-position'. */);
30059 Voverlay_arrow_string = build_pure_c_string ("=>");
30061 DEFVAR_LISP ("overlay-arrow-variable-list", Voverlay_arrow_variable_list,
30062 doc: /* List of variables (symbols) which hold markers for overlay arrows.
30063 The symbols on this list are examined during redisplay to determine
30064 where to display overlay arrows. */);
30065 Voverlay_arrow_variable_list
30066 = list1 (intern_c_string ("overlay-arrow-position"));
30068 DEFVAR_INT ("scroll-step", emacs_scroll_step,
30069 doc: /* The number of lines to try scrolling a window by when point moves out.
30070 If that fails to bring point back on frame, point is centered instead.
30071 If this is zero, point is always centered after it moves off frame.
30072 If you want scrolling to always be a line at a time, you should set
30073 `scroll-conservatively' to a large value rather than set this to 1. */);
30075 DEFVAR_INT ("scroll-conservatively", scroll_conservatively,
30076 doc: /* Scroll up to this many lines, to bring point back on screen.
30077 If point moves off-screen, redisplay will scroll by up to
30078 `scroll-conservatively' lines in order to bring point just barely
30079 onto the screen again. If that cannot be done, then redisplay
30080 recenters point as usual.
30082 If the value is greater than 100, redisplay will never recenter point,
30083 but will always scroll just enough text to bring point into view, even
30084 if you move far away.
30086 A value of zero means always recenter point if it moves off screen. */);
30087 scroll_conservatively = 0;
30089 DEFVAR_INT ("scroll-margin", scroll_margin,
30090 doc: /* Number of lines of margin at the top and bottom of a window.
30091 Recenter the window whenever point gets within this many lines
30092 of the top or bottom of the window. */);
30093 scroll_margin = 0;
30095 DEFVAR_LISP ("display-pixels-per-inch", Vdisplay_pixels_per_inch,
30096 doc: /* Pixels per inch value for non-window system displays.
30097 Value is a number or a cons (WIDTH-DPI . HEIGHT-DPI). */);
30098 Vdisplay_pixels_per_inch = make_float (72.0);
30100 #ifdef GLYPH_DEBUG
30101 DEFVAR_INT ("debug-end-pos", debug_end_pos, doc: /* Don't ask. */);
30102 #endif
30104 DEFVAR_LISP ("truncate-partial-width-windows",
30105 Vtruncate_partial_width_windows,
30106 doc: /* Non-nil means truncate lines in windows narrower than the frame.
30107 For an integer value, truncate lines in each window narrower than the
30108 full frame width, provided the window width is less than that integer;
30109 otherwise, respect the value of `truncate-lines'.
30111 For any other non-nil value, truncate lines in all windows that do
30112 not span the full frame width.
30114 A value of nil means to respect the value of `truncate-lines'.
30116 If `word-wrap' is enabled, you might want to reduce this. */);
30117 Vtruncate_partial_width_windows = make_number (50);
30119 DEFVAR_LISP ("line-number-display-limit", Vline_number_display_limit,
30120 doc: /* Maximum buffer size for which line number should be displayed.
30121 If the buffer is bigger than this, the line number does not appear
30122 in the mode line. A value of nil means no limit. */);
30123 Vline_number_display_limit = Qnil;
30125 DEFVAR_INT ("line-number-display-limit-width",
30126 line_number_display_limit_width,
30127 doc: /* Maximum line width (in characters) for line number display.
30128 If the average length of the lines near point is bigger than this, then the
30129 line number may be omitted from the mode line. */);
30130 line_number_display_limit_width = 200;
30132 DEFVAR_BOOL ("highlight-nonselected-windows", highlight_nonselected_windows,
30133 doc: /* Non-nil means highlight region even in nonselected windows. */);
30134 highlight_nonselected_windows = 0;
30136 DEFVAR_BOOL ("multiple-frames", multiple_frames,
30137 doc: /* Non-nil if more than one frame is visible on this display.
30138 Minibuffer-only frames don't count, but iconified frames do.
30139 This variable is not guaranteed to be accurate except while processing
30140 `frame-title-format' and `icon-title-format'. */);
30142 DEFVAR_LISP ("frame-title-format", Vframe_title_format,
30143 doc: /* Template for displaying the title bar of visible frames.
30144 \(Assuming the window manager supports this feature.)
30146 This variable has the same structure as `mode-line-format', except that
30147 the %c and %l constructs are ignored. It is used only on frames for
30148 which no explicit name has been set \(see `modify-frame-parameters'). */);
30150 DEFVAR_LISP ("icon-title-format", Vicon_title_format,
30151 doc: /* Template for displaying the title bar of an iconified frame.
30152 \(Assuming the window manager supports this feature.)
30153 This variable has the same structure as `mode-line-format' (which see),
30154 and is used only on frames for which no explicit name has been set
30155 \(see `modify-frame-parameters'). */);
30156 Vicon_title_format
30157 = Vframe_title_format
30158 = listn (CONSTYPE_PURE, 3,
30159 intern_c_string ("multiple-frames"),
30160 build_pure_c_string ("%b"),
30161 listn (CONSTYPE_PURE, 4,
30162 empty_unibyte_string,
30163 intern_c_string ("invocation-name"),
30164 build_pure_c_string ("@"),
30165 intern_c_string ("system-name")));
30167 DEFVAR_LISP ("message-log-max", Vmessage_log_max,
30168 doc: /* Maximum number of lines to keep in the message log buffer.
30169 If nil, disable message logging. If t, log messages but don't truncate
30170 the buffer when it becomes large. */);
30171 Vmessage_log_max = make_number (1000);
30173 DEFVAR_LISP ("window-size-change-functions", Vwindow_size_change_functions,
30174 doc: /* Functions called before redisplay, if window sizes have changed.
30175 The value should be a list of functions that take one argument.
30176 Just before redisplay, for each frame, if any of its windows have changed
30177 size since the last redisplay, or have been split or deleted,
30178 all the functions in the list are called, with the frame as argument. */);
30179 Vwindow_size_change_functions = Qnil;
30181 DEFVAR_LISP ("window-scroll-functions", Vwindow_scroll_functions,
30182 doc: /* List of functions to call before redisplaying a window with scrolling.
30183 Each function is called with two arguments, the window and its new
30184 display-start position. Note that these functions are also called by
30185 `set-window-buffer'. Also note that the value of `window-end' is not
30186 valid when these functions are called.
30188 Warning: Do not use this feature to alter the way the window
30189 is scrolled. It is not designed for that, and such use probably won't
30190 work. */);
30191 Vwindow_scroll_functions = Qnil;
30193 DEFVAR_LISP ("window-text-change-functions",
30194 Vwindow_text_change_functions,
30195 doc: /* Functions to call in redisplay when text in the window might change. */);
30196 Vwindow_text_change_functions = Qnil;
30198 DEFVAR_LISP ("redisplay-end-trigger-functions", Vredisplay_end_trigger_functions,
30199 doc: /* Functions called when redisplay of a window reaches the end trigger.
30200 Each function is called with two arguments, the window and the end trigger value.
30201 See `set-window-redisplay-end-trigger'. */);
30202 Vredisplay_end_trigger_functions = Qnil;
30204 DEFVAR_LISP ("mouse-autoselect-window", Vmouse_autoselect_window,
30205 doc: /* Non-nil means autoselect window with mouse pointer.
30206 If nil, do not autoselect windows.
30207 A positive number means delay autoselection by that many seconds: a
30208 window is autoselected only after the mouse has remained in that
30209 window for the duration of the delay.
30210 A negative number has a similar effect, but causes windows to be
30211 autoselected only after the mouse has stopped moving. \(Because of
30212 the way Emacs compares mouse events, you will occasionally wait twice
30213 that time before the window gets selected.\)
30214 Any other value means to autoselect window instantaneously when the
30215 mouse pointer enters it.
30217 Autoselection selects the minibuffer only if it is active, and never
30218 unselects the minibuffer if it is active.
30220 When customizing this variable make sure that the actual value of
30221 `focus-follows-mouse' matches the behavior of your window manager. */);
30222 Vmouse_autoselect_window = Qnil;
30224 DEFVAR_LISP ("auto-resize-tool-bars", Vauto_resize_tool_bars,
30225 doc: /* Non-nil means automatically resize tool-bars.
30226 This dynamically changes the tool-bar's height to the minimum height
30227 that is needed to make all tool-bar items visible.
30228 If value is `grow-only', the tool-bar's height is only increased
30229 automatically; to decrease the tool-bar height, use \\[recenter]. */);
30230 Vauto_resize_tool_bars = Qt;
30232 DEFVAR_BOOL ("auto-raise-tool-bar-buttons", auto_raise_tool_bar_buttons_p,
30233 doc: /* Non-nil means raise tool-bar buttons when the mouse moves over them. */);
30234 auto_raise_tool_bar_buttons_p = 1;
30236 DEFVAR_BOOL ("make-cursor-line-fully-visible", make_cursor_line_fully_visible_p,
30237 doc: /* Non-nil means to scroll (recenter) cursor line if it is not fully visible. */);
30238 make_cursor_line_fully_visible_p = 1;
30240 DEFVAR_LISP ("tool-bar-border", Vtool_bar_border,
30241 doc: /* Border below tool-bar in pixels.
30242 If an integer, use it as the height of the border.
30243 If it is one of `internal-border-width' or `border-width', use the
30244 value of the corresponding frame parameter.
30245 Otherwise, no border is added below the tool-bar. */);
30246 Vtool_bar_border = Qinternal_border_width;
30248 DEFVAR_LISP ("tool-bar-button-margin", Vtool_bar_button_margin,
30249 doc: /* Margin around tool-bar buttons in pixels.
30250 If an integer, use that for both horizontal and vertical margins.
30251 Otherwise, value should be a pair of integers `(HORZ . VERT)' with
30252 HORZ specifying the horizontal margin, and VERT specifying the
30253 vertical margin. */);
30254 Vtool_bar_button_margin = make_number (DEFAULT_TOOL_BAR_BUTTON_MARGIN);
30256 DEFVAR_INT ("tool-bar-button-relief", tool_bar_button_relief,
30257 doc: /* Relief thickness of tool-bar buttons. */);
30258 tool_bar_button_relief = DEFAULT_TOOL_BAR_BUTTON_RELIEF;
30260 DEFVAR_LISP ("tool-bar-style", Vtool_bar_style,
30261 doc: /* Tool bar style to use.
30262 It can be one of
30263 image - show images only
30264 text - show text only
30265 both - show both, text below image
30266 both-horiz - show text to the right of the image
30267 text-image-horiz - show text to the left of the image
30268 any other - use system default or image if no system default.
30270 This variable only affects the GTK+ toolkit version of Emacs. */);
30271 Vtool_bar_style = Qnil;
30273 DEFVAR_INT ("tool-bar-max-label-size", tool_bar_max_label_size,
30274 doc: /* Maximum number of characters a label can have to be shown.
30275 The tool bar style must also show labels for this to have any effect, see
30276 `tool-bar-style'. */);
30277 tool_bar_max_label_size = DEFAULT_TOOL_BAR_LABEL_SIZE;
30279 DEFVAR_LISP ("fontification-functions", Vfontification_functions,
30280 doc: /* List of functions to call to fontify regions of text.
30281 Each function is called with one argument POS. Functions must
30282 fontify a region starting at POS in the current buffer, and give
30283 fontified regions the property `fontified'. */);
30284 Vfontification_functions = Qnil;
30285 Fmake_variable_buffer_local (Qfontification_functions);
30287 DEFVAR_BOOL ("unibyte-display-via-language-environment",
30288 unibyte_display_via_language_environment,
30289 doc: /* Non-nil means display unibyte text according to language environment.
30290 Specifically, this means that raw bytes in the range 160-255 decimal
30291 are displayed by converting them to the equivalent multibyte characters
30292 according to the current language environment. As a result, they are
30293 displayed according to the current fontset.
30295 Note that this variable affects only how these bytes are displayed,
30296 but does not change the fact they are interpreted as raw bytes. */);
30297 unibyte_display_via_language_environment = 0;
30299 DEFVAR_LISP ("max-mini-window-height", Vmax_mini_window_height,
30300 doc: /* Maximum height for resizing mini-windows (the minibuffer and the echo area).
30301 If a float, it specifies a fraction of the mini-window frame's height.
30302 If an integer, it specifies a number of lines. */);
30303 Vmax_mini_window_height = make_float (0.25);
30305 DEFVAR_LISP ("resize-mini-windows", Vresize_mini_windows,
30306 doc: /* How to resize mini-windows (the minibuffer and the echo area).
30307 A value of nil means don't automatically resize mini-windows.
30308 A value of t means resize them to fit the text displayed in them.
30309 A value of `grow-only', the default, means let mini-windows grow only;
30310 they return to their normal size when the minibuffer is closed, or the
30311 echo area becomes empty. */);
30312 Vresize_mini_windows = Qgrow_only;
30314 DEFVAR_LISP ("blink-cursor-alist", Vblink_cursor_alist,
30315 doc: /* Alist specifying how to blink the cursor off.
30316 Each element has the form (ON-STATE . OFF-STATE). Whenever the
30317 `cursor-type' frame-parameter or variable equals ON-STATE,
30318 comparing using `equal', Emacs uses OFF-STATE to specify
30319 how to blink it off. ON-STATE and OFF-STATE are values for
30320 the `cursor-type' frame parameter.
30322 If a frame's ON-STATE has no entry in this list,
30323 the frame's other specifications determine how to blink the cursor off. */);
30324 Vblink_cursor_alist = Qnil;
30326 DEFVAR_BOOL ("auto-hscroll-mode", automatic_hscrolling_p,
30327 doc: /* Allow or disallow automatic horizontal scrolling of windows.
30328 If non-nil, windows are automatically scrolled horizontally to make
30329 point visible. */);
30330 automatic_hscrolling_p = 1;
30331 DEFSYM (Qauto_hscroll_mode, "auto-hscroll-mode");
30333 DEFVAR_INT ("hscroll-margin", hscroll_margin,
30334 doc: /* How many columns away from the window edge point is allowed to get
30335 before automatic hscrolling will horizontally scroll the window. */);
30336 hscroll_margin = 5;
30338 DEFVAR_LISP ("hscroll-step", Vhscroll_step,
30339 doc: /* How many columns to scroll the window when point gets too close to the edge.
30340 When point is less than `hscroll-margin' columns from the window
30341 edge, automatic hscrolling will scroll the window by the amount of columns
30342 determined by this variable. If its value is a positive integer, scroll that
30343 many columns. If it's a positive floating-point number, it specifies the
30344 fraction of the window's width to scroll. If it's nil or zero, point will be
30345 centered horizontally after the scroll. Any other value, including negative
30346 numbers, are treated as if the value were zero.
30348 Automatic hscrolling always moves point outside the scroll margin, so if
30349 point was more than scroll step columns inside the margin, the window will
30350 scroll more than the value given by the scroll step.
30352 Note that the lower bound for automatic hscrolling specified by `scroll-left'
30353 and `scroll-right' overrides this variable's effect. */);
30354 Vhscroll_step = make_number (0);
30356 DEFVAR_BOOL ("message-truncate-lines", message_truncate_lines,
30357 doc: /* If non-nil, messages are truncated instead of resizing the echo area.
30358 Bind this around calls to `message' to let it take effect. */);
30359 message_truncate_lines = 0;
30361 DEFVAR_LISP ("menu-bar-update-hook", Vmenu_bar_update_hook,
30362 doc: /* Normal hook run to update the menu bar definitions.
30363 Redisplay runs this hook before it redisplays the menu bar.
30364 This is used to update menus such as Buffers, whose contents depend on
30365 various data. */);
30366 Vmenu_bar_update_hook = Qnil;
30368 DEFVAR_LISP ("menu-updating-frame", Vmenu_updating_frame,
30369 doc: /* Frame for which we are updating a menu.
30370 The enable predicate for a menu binding should check this variable. */);
30371 Vmenu_updating_frame = Qnil;
30373 DEFVAR_BOOL ("inhibit-menubar-update", inhibit_menubar_update,
30374 doc: /* Non-nil means don't update menu bars. Internal use only. */);
30375 inhibit_menubar_update = 0;
30377 DEFVAR_LISP ("wrap-prefix", Vwrap_prefix,
30378 doc: /* Prefix prepended to all continuation lines at display time.
30379 The value may be a string, an image, or a stretch-glyph; it is
30380 interpreted in the same way as the value of a `display' text property.
30382 This variable is overridden by any `wrap-prefix' text or overlay
30383 property.
30385 To add a prefix to non-continuation lines, use `line-prefix'. */);
30386 Vwrap_prefix = Qnil;
30387 DEFSYM (Qwrap_prefix, "wrap-prefix");
30388 Fmake_variable_buffer_local (Qwrap_prefix);
30390 DEFVAR_LISP ("line-prefix", Vline_prefix,
30391 doc: /* Prefix prepended to all non-continuation lines at display time.
30392 The value may be a string, an image, or a stretch-glyph; it is
30393 interpreted in the same way as the value of a `display' text property.
30395 This variable is overridden by any `line-prefix' text or overlay
30396 property.
30398 To add a prefix to continuation lines, use `wrap-prefix'. */);
30399 Vline_prefix = Qnil;
30400 DEFSYM (Qline_prefix, "line-prefix");
30401 Fmake_variable_buffer_local (Qline_prefix);
30403 DEFVAR_BOOL ("inhibit-eval-during-redisplay", inhibit_eval_during_redisplay,
30404 doc: /* Non-nil means don't eval Lisp during redisplay. */);
30405 inhibit_eval_during_redisplay = 0;
30407 DEFVAR_BOOL ("inhibit-free-realized-faces", inhibit_free_realized_faces,
30408 doc: /* Non-nil means don't free realized faces. Internal use only. */);
30409 inhibit_free_realized_faces = 0;
30411 #ifdef GLYPH_DEBUG
30412 DEFVAR_BOOL ("inhibit-try-window-id", inhibit_try_window_id,
30413 doc: /* Inhibit try_window_id display optimization. */);
30414 inhibit_try_window_id = 0;
30416 DEFVAR_BOOL ("inhibit-try-window-reusing", inhibit_try_window_reusing,
30417 doc: /* Inhibit try_window_reusing display optimization. */);
30418 inhibit_try_window_reusing = 0;
30420 DEFVAR_BOOL ("inhibit-try-cursor-movement", inhibit_try_cursor_movement,
30421 doc: /* Inhibit try_cursor_movement display optimization. */);
30422 inhibit_try_cursor_movement = 0;
30423 #endif /* GLYPH_DEBUG */
30425 DEFVAR_INT ("overline-margin", overline_margin,
30426 doc: /* Space between overline and text, in pixels.
30427 The default value is 2: the height of the overline (1 pixel) plus 1 pixel
30428 margin to the character height. */);
30429 overline_margin = 2;
30431 DEFVAR_INT ("underline-minimum-offset",
30432 underline_minimum_offset,
30433 doc: /* Minimum distance between baseline and underline.
30434 This can improve legibility of underlined text at small font sizes,
30435 particularly when using variable `x-use-underline-position-properties'
30436 with fonts that specify an UNDERLINE_POSITION relatively close to the
30437 baseline. The default value is 1. */);
30438 underline_minimum_offset = 1;
30440 DEFVAR_BOOL ("display-hourglass", display_hourglass_p,
30441 doc: /* Non-nil means show an hourglass pointer, when Emacs is busy.
30442 This feature only works when on a window system that can change
30443 cursor shapes. */);
30444 display_hourglass_p = 1;
30446 DEFVAR_LISP ("hourglass-delay", Vhourglass_delay,
30447 doc: /* Seconds to wait before displaying an hourglass pointer when Emacs is busy. */);
30448 Vhourglass_delay = make_number (DEFAULT_HOURGLASS_DELAY);
30450 #ifdef HAVE_WINDOW_SYSTEM
30451 hourglass_atimer = NULL;
30452 hourglass_shown_p = 0;
30453 #endif /* HAVE_WINDOW_SYSTEM */
30455 DEFSYM (Qglyphless_char, "glyphless-char");
30456 DEFSYM (Qhex_code, "hex-code");
30457 DEFSYM (Qempty_box, "empty-box");
30458 DEFSYM (Qthin_space, "thin-space");
30459 DEFSYM (Qzero_width, "zero-width");
30461 DEFVAR_LISP ("pre-redisplay-function", Vpre_redisplay_function,
30462 doc: /* Function run just before redisplay.
30463 It is called with one argument, which is the set of windows that are to
30464 be redisplayed. This set can be nil (meaning, only the selected window),
30465 or t (meaning all windows). */);
30466 Vpre_redisplay_function = intern ("ignore");
30468 DEFSYM (Qglyphless_char_display, "glyphless-char-display");
30469 Fput (Qglyphless_char_display, Qchar_table_extra_slots, make_number (1));
30471 DEFVAR_LISP ("glyphless-char-display", Vglyphless_char_display,
30472 doc: /* Char-table defining glyphless characters.
30473 Each element, if non-nil, should be one of the following:
30474 an ASCII acronym string: display this string in a box
30475 `hex-code': display the hexadecimal code of a character in a box
30476 `empty-box': display as an empty box
30477 `thin-space': display as 1-pixel width space
30478 `zero-width': don't display
30479 An element may also be a cons cell (GRAPHICAL . TEXT), which specifies the
30480 display method for graphical terminals and text terminals respectively.
30481 GRAPHICAL and TEXT should each have one of the values listed above.
30483 The char-table has one extra slot to control the display of a character for
30484 which no font is found. This slot only takes effect on graphical terminals.
30485 Its value should be an ASCII acronym string, `hex-code', `empty-box', or
30486 `thin-space'. The default is `empty-box'.
30488 If a character has a non-nil entry in an active display table, the
30489 display table takes effect; in this case, Emacs does not consult
30490 `glyphless-char-display' at all. */);
30491 Vglyphless_char_display = Fmake_char_table (Qglyphless_char_display, Qnil);
30492 Fset_char_table_extra_slot (Vglyphless_char_display, make_number (0),
30493 Qempty_box);
30495 DEFVAR_LISP ("debug-on-message", Vdebug_on_message,
30496 doc: /* If non-nil, debug if a message matching this regexp is displayed. */);
30497 Vdebug_on_message = Qnil;
30499 DEFVAR_LISP ("redisplay--all-windows-cause", Vredisplay__all_windows_cause,
30500 doc: /* */);
30501 Vredisplay__all_windows_cause
30502 = Fmake_vector (make_number (100), make_number (0));
30504 DEFVAR_LISP ("redisplay--mode-lines-cause", Vredisplay__mode_lines_cause,
30505 doc: /* */);
30506 Vredisplay__mode_lines_cause
30507 = Fmake_vector (make_number (100), make_number (0));
30511 /* Initialize this module when Emacs starts. */
30513 void
30514 init_xdisp (void)
30516 CHARPOS (this_line_start_pos) = 0;
30518 if (!noninteractive)
30520 struct window *m = XWINDOW (minibuf_window);
30521 Lisp_Object frame = m->frame;
30522 struct frame *f = XFRAME (frame);
30523 Lisp_Object root = FRAME_ROOT_WINDOW (f);
30524 struct window *r = XWINDOW (root);
30525 int i;
30527 echo_area_window = minibuf_window;
30529 r->top_line = FRAME_TOP_MARGIN (f);
30530 r->pixel_top = r->top_line * FRAME_LINE_HEIGHT (f);
30531 r->total_cols = FRAME_COLS (f);
30532 r->pixel_width = r->total_cols * FRAME_COLUMN_WIDTH (f);
30533 r->total_lines = FRAME_LINES (f) - 1 - FRAME_TOP_MARGIN (f);
30534 r->pixel_height = r->total_lines * FRAME_LINE_HEIGHT (f);
30536 m->top_line = FRAME_LINES (f) - 1;
30537 m->pixel_top = m->top_line * FRAME_LINE_HEIGHT (f);
30538 m->total_cols = FRAME_COLS (f);
30539 m->pixel_width = m->total_cols * FRAME_COLUMN_WIDTH (f);
30540 m->total_lines = 1;
30541 m->pixel_height = m->total_lines * FRAME_LINE_HEIGHT (f);
30543 scratch_glyph_row.glyphs[TEXT_AREA] = scratch_glyphs;
30544 scratch_glyph_row.glyphs[TEXT_AREA + 1]
30545 = scratch_glyphs + MAX_SCRATCH_GLYPHS;
30547 /* The default ellipsis glyphs `...'. */
30548 for (i = 0; i < 3; ++i)
30549 default_invis_vector[i] = make_number ('.');
30553 /* Allocate the buffer for frame titles.
30554 Also used for `format-mode-line'. */
30555 int size = 100;
30556 mode_line_noprop_buf = xmalloc (size);
30557 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
30558 mode_line_noprop_ptr = mode_line_noprop_buf;
30559 mode_line_target = MODE_LINE_DISPLAY;
30562 help_echo_showing_p = 0;
30565 #ifdef HAVE_WINDOW_SYSTEM
30567 /* Platform-independent portion of hourglass implementation. */
30569 /* Cancel a currently active hourglass timer, and start a new one. */
30570 void
30571 start_hourglass (void)
30573 struct timespec delay;
30575 cancel_hourglass ();
30577 if (INTEGERP (Vhourglass_delay)
30578 && XINT (Vhourglass_delay) > 0)
30579 delay = make_timespec (min (XINT (Vhourglass_delay),
30580 TYPE_MAXIMUM (time_t)),
30582 else if (FLOATP (Vhourglass_delay)
30583 && XFLOAT_DATA (Vhourglass_delay) > 0)
30584 delay = dtotimespec (XFLOAT_DATA (Vhourglass_delay));
30585 else
30586 delay = make_timespec (DEFAULT_HOURGLASS_DELAY, 0);
30588 #ifdef HAVE_NTGUI
30590 extern void w32_note_current_window (void);
30591 w32_note_current_window ();
30593 #endif /* HAVE_NTGUI */
30595 hourglass_atimer = start_atimer (ATIMER_RELATIVE, delay,
30596 show_hourglass, NULL);
30600 /* Cancel the hourglass cursor timer if active, hide a busy cursor if
30601 shown. */
30602 void
30603 cancel_hourglass (void)
30605 if (hourglass_atimer)
30607 cancel_atimer (hourglass_atimer);
30608 hourglass_atimer = NULL;
30611 if (hourglass_shown_p)
30612 hide_hourglass ();
30615 #endif /* HAVE_WINDOW_SYSTEM */