Correctly treat progn contents as toplevel forms when byte compiling
[emacs.git] / src / xdisp.c
blobbb91d6f5e1f462a8262feafd13370d17b4298b16
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);
1265 struct buffer *old_buffer = NULL;
1266 Lisp_Object result;
1268 if (XBUFFER (w->contents) != current_buffer)
1270 old_buffer = current_buffer;
1271 set_buffer_internal_1 (XBUFFER (w->contents));
1273 SET_TEXT_POS (pt, PT, PT_BYTE);
1274 start_display (&it, w, pt);
1275 it.vpos = it.current_y = 0;
1276 last_height = 0;
1277 result = make_number (line_bottom_y (&it));
1278 if (old_buffer)
1279 set_buffer_internal_1 (old_buffer);
1281 return result;
1284 /* Return the default pixel height of text lines in window W. The
1285 value is the canonical height of the W frame's default font, plus
1286 any extra space required by the line-spacing variable or frame
1287 parameter.
1289 Implementation note: this ignores any line-spacing text properties
1290 put on the newline characters. This is because those properties
1291 only affect the _screen_ line ending in the newline (i.e., in a
1292 continued line, only the last screen line will be affected), which
1293 means only a small number of lines in a buffer can ever use this
1294 feature. Since this function is used to compute the default pixel
1295 equivalent of text lines in a window, we can safely ignore those
1296 few lines. For the same reasons, we ignore the line-height
1297 properties. */
1299 default_line_pixel_height (struct window *w)
1301 struct frame *f = WINDOW_XFRAME (w);
1302 int height = FRAME_LINE_HEIGHT (f);
1304 if (!FRAME_INITIAL_P (f) && BUFFERP (w->contents))
1306 struct buffer *b = XBUFFER (w->contents);
1307 Lisp_Object val = BVAR (b, extra_line_spacing);
1309 if (NILP (val))
1310 val = BVAR (&buffer_defaults, extra_line_spacing);
1311 if (!NILP (val))
1313 if (RANGED_INTEGERP (0, val, INT_MAX))
1314 height += XFASTINT (val);
1315 else if (FLOATP (val))
1317 int addon = XFLOAT_DATA (val) * height + 0.5;
1319 if (addon >= 0)
1320 height += addon;
1323 else
1324 height += f->extra_line_spacing;
1327 return height;
1330 /* Subroutine of pos_visible_p below. Extracts a display string, if
1331 any, from the display spec given as its argument. */
1332 static Lisp_Object
1333 string_from_display_spec (Lisp_Object spec)
1335 if (CONSP (spec))
1337 while (CONSP (spec))
1339 if (STRINGP (XCAR (spec)))
1340 return XCAR (spec);
1341 spec = XCDR (spec);
1344 else if (VECTORP (spec))
1346 ptrdiff_t i;
1348 for (i = 0; i < ASIZE (spec); i++)
1350 if (STRINGP (AREF (spec, i)))
1351 return AREF (spec, i);
1353 return Qnil;
1356 return spec;
1360 /* Limit insanely large values of W->hscroll on frame F to the largest
1361 value that will still prevent first_visible_x and last_visible_x of
1362 'struct it' from overflowing an int. */
1363 static int
1364 window_hscroll_limited (struct window *w, struct frame *f)
1366 ptrdiff_t window_hscroll = w->hscroll;
1367 int window_text_width = window_box_width (w, TEXT_AREA);
1368 int colwidth = FRAME_COLUMN_WIDTH (f);
1370 if (window_hscroll > (INT_MAX - window_text_width) / colwidth - 1)
1371 window_hscroll = (INT_MAX - window_text_width) / colwidth - 1;
1373 return window_hscroll;
1376 /* Return 1 if position CHARPOS is visible in window W.
1377 CHARPOS < 0 means return info about WINDOW_END position.
1378 If visible, set *X and *Y to pixel coordinates of top left corner.
1379 Set *RTOP and *RBOT to pixel height of an invisible area of glyph at POS.
1380 Set *ROWH and *VPOS to row's visible height and VPOS (row number). */
1383 pos_visible_p (struct window *w, ptrdiff_t charpos, int *x, int *y,
1384 int *rtop, int *rbot, int *rowh, int *vpos)
1386 struct it it;
1387 void *itdata = bidi_shelve_cache ();
1388 struct text_pos top;
1389 int visible_p = 0;
1390 struct buffer *old_buffer = NULL;
1392 if (FRAME_INITIAL_P (XFRAME (WINDOW_FRAME (w))))
1393 return visible_p;
1395 if (XBUFFER (w->contents) != current_buffer)
1397 old_buffer = current_buffer;
1398 set_buffer_internal_1 (XBUFFER (w->contents));
1401 SET_TEXT_POS_FROM_MARKER (top, w->start);
1402 /* Scrolling a minibuffer window via scroll bar when the echo area
1403 shows long text sometimes resets the minibuffer contents behind
1404 our backs. */
1405 if (CHARPOS (top) > ZV)
1406 SET_TEXT_POS (top, BEGV, BEGV_BYTE);
1408 /* Compute exact mode line heights. */
1409 if (WINDOW_WANTS_MODELINE_P (w))
1410 w->mode_line_height
1411 = display_mode_line (w, CURRENT_MODE_LINE_FACE_ID (w),
1412 BVAR (current_buffer, mode_line_format));
1414 if (WINDOW_WANTS_HEADER_LINE_P (w))
1415 w->header_line_height
1416 = display_mode_line (w, HEADER_LINE_FACE_ID,
1417 BVAR (current_buffer, header_line_format));
1419 start_display (&it, w, top);
1420 move_it_to (&it, charpos, -1, it.last_visible_y - 1, -1,
1421 (charpos >= 0 ? MOVE_TO_POS : 0) | MOVE_TO_Y);
1423 if (charpos >= 0
1424 && (((!it.bidi_p || it.bidi_it.scan_dir == 1)
1425 && IT_CHARPOS (it) >= charpos)
1426 /* When scanning backwards under bidi iteration, move_it_to
1427 stops at or _before_ CHARPOS, because it stops at or to
1428 the _right_ of the character at CHARPOS. */
1429 || (it.bidi_p && it.bidi_it.scan_dir == -1
1430 && IT_CHARPOS (it) <= charpos)))
1432 /* We have reached CHARPOS, or passed it. How the call to
1433 move_it_to can overshoot: (i) If CHARPOS is on invisible text
1434 or covered by a display property, move_it_to stops at the end
1435 of the invisible text, to the right of CHARPOS. (ii) If
1436 CHARPOS is in a display vector, move_it_to stops on its last
1437 glyph. */
1438 int top_x = it.current_x;
1439 int top_y = it.current_y;
1440 /* Calling line_bottom_y may change it.method, it.position, etc. */
1441 enum it_method it_method = it.method;
1442 int bottom_y = (last_height = 0, line_bottom_y (&it));
1443 int window_top_y = WINDOW_HEADER_LINE_HEIGHT (w);
1445 if (top_y < window_top_y)
1446 visible_p = bottom_y > window_top_y;
1447 else if (top_y < it.last_visible_y)
1448 visible_p = true;
1449 if (bottom_y >= it.last_visible_y
1450 && it.bidi_p && it.bidi_it.scan_dir == -1
1451 && IT_CHARPOS (it) < charpos)
1453 /* When the last line of the window is scanned backwards
1454 under bidi iteration, we could be duped into thinking
1455 that we have passed CHARPOS, when in fact move_it_to
1456 simply stopped short of CHARPOS because it reached
1457 last_visible_y. To see if that's what happened, we call
1458 move_it_to again with a slightly larger vertical limit,
1459 and see if it actually moved vertically; if it did, we
1460 didn't really reach CHARPOS, which is beyond window end. */
1461 struct it save_it = it;
1462 /* Why 10? because we don't know how many canonical lines
1463 will the height of the next line(s) be. So we guess. */
1464 int ten_more_lines = 10 * default_line_pixel_height (w);
1466 move_it_to (&it, charpos, -1, bottom_y + ten_more_lines, -1,
1467 MOVE_TO_POS | MOVE_TO_Y);
1468 if (it.current_y > top_y)
1469 visible_p = 0;
1471 it = save_it;
1473 if (visible_p)
1475 if (it_method == GET_FROM_DISPLAY_VECTOR)
1477 /* We stopped on the last glyph of a display vector.
1478 Try and recompute. Hack alert! */
1479 if (charpos < 2 || top.charpos >= charpos)
1480 top_x = it.glyph_row->x;
1481 else
1483 struct it it2, it2_prev;
1484 /* The idea is to get to the previous buffer
1485 position, consume the character there, and use
1486 the pixel coordinates we get after that. But if
1487 the previous buffer position is also displayed
1488 from a display vector, we need to consume all of
1489 the glyphs from that display vector. */
1490 start_display (&it2, w, top);
1491 move_it_to (&it2, charpos - 1, -1, -1, -1, MOVE_TO_POS);
1492 /* If we didn't get to CHARPOS - 1, there's some
1493 replacing display property at that position, and
1494 we stopped after it. That is exactly the place
1495 whose coordinates we want. */
1496 if (IT_CHARPOS (it2) != charpos - 1)
1497 it2_prev = it2;
1498 else
1500 /* Iterate until we get out of the display
1501 vector that displays the character at
1502 CHARPOS - 1. */
1503 do {
1504 get_next_display_element (&it2);
1505 PRODUCE_GLYPHS (&it2);
1506 it2_prev = it2;
1507 set_iterator_to_next (&it2, 1);
1508 } while (it2.method == GET_FROM_DISPLAY_VECTOR
1509 && IT_CHARPOS (it2) < charpos);
1511 if (ITERATOR_AT_END_OF_LINE_P (&it2_prev)
1512 || it2_prev.current_x > it2_prev.last_visible_x)
1513 top_x = it.glyph_row->x;
1514 else
1516 top_x = it2_prev.current_x;
1517 top_y = it2_prev.current_y;
1521 else if (IT_CHARPOS (it) != charpos)
1523 Lisp_Object cpos = make_number (charpos);
1524 Lisp_Object spec = Fget_char_property (cpos, Qdisplay, Qnil);
1525 Lisp_Object string = string_from_display_spec (spec);
1526 struct text_pos tpos;
1527 int replacing_spec_p;
1528 bool newline_in_string
1529 = (STRINGP (string)
1530 && memchr (SDATA (string), '\n', SBYTES (string)));
1532 SET_TEXT_POS (tpos, charpos, CHAR_TO_BYTE (charpos));
1533 replacing_spec_p
1534 = (!NILP (spec)
1535 && handle_display_spec (NULL, spec, Qnil, Qnil, &tpos,
1536 charpos, FRAME_WINDOW_P (it.f)));
1537 /* The tricky code below is needed because there's a
1538 discrepancy between move_it_to and how we set cursor
1539 when PT is at the beginning of a portion of text
1540 covered by a display property or an overlay with a
1541 display property, or the display line ends in a
1542 newline from a display string. move_it_to will stop
1543 _after_ such display strings, whereas
1544 set_cursor_from_row conspires with cursor_row_p to
1545 place the cursor on the first glyph produced from the
1546 display string. */
1548 /* We have overshoot PT because it is covered by a
1549 display property that replaces the text it covers.
1550 If the string includes embedded newlines, we are also
1551 in the wrong display line. Backtrack to the correct
1552 line, where the display property begins. */
1553 if (replacing_spec_p)
1555 Lisp_Object startpos, endpos;
1556 EMACS_INT start, end;
1557 struct it it3;
1558 int it3_moved;
1560 /* Find the first and the last buffer positions
1561 covered by the display string. */
1562 endpos =
1563 Fnext_single_char_property_change (cpos, Qdisplay,
1564 Qnil, Qnil);
1565 startpos =
1566 Fprevious_single_char_property_change (endpos, Qdisplay,
1567 Qnil, Qnil);
1568 start = XFASTINT (startpos);
1569 end = XFASTINT (endpos);
1570 /* Move to the last buffer position before the
1571 display property. */
1572 start_display (&it3, w, top);
1573 move_it_to (&it3, start - 1, -1, -1, -1, MOVE_TO_POS);
1574 /* Move forward one more line if the position before
1575 the display string is a newline or if it is the
1576 rightmost character on a line that is
1577 continued or word-wrapped. */
1578 if (it3.method == GET_FROM_BUFFER
1579 && (it3.c == '\n'
1580 || FETCH_BYTE (IT_BYTEPOS (it3)) == '\n'))
1581 move_it_by_lines (&it3, 1);
1582 else if (move_it_in_display_line_to (&it3, -1,
1583 it3.current_x
1584 + it3.pixel_width,
1585 MOVE_TO_X)
1586 == MOVE_LINE_CONTINUED)
1588 move_it_by_lines (&it3, 1);
1589 /* When we are under word-wrap, the #$@%!
1590 move_it_by_lines moves 2 lines, so we need to
1591 fix that up. */
1592 if (it3.line_wrap == WORD_WRAP)
1593 move_it_by_lines (&it3, -1);
1596 /* Record the vertical coordinate of the display
1597 line where we wound up. */
1598 top_y = it3.current_y;
1599 if (it3.bidi_p)
1601 /* When characters are reordered for display,
1602 the character displayed to the left of the
1603 display string could be _after_ the display
1604 property in the logical order. Use the
1605 smallest vertical position of these two. */
1606 start_display (&it3, w, top);
1607 move_it_to (&it3, end + 1, -1, -1, -1, MOVE_TO_POS);
1608 if (it3.current_y < top_y)
1609 top_y = it3.current_y;
1611 /* Move from the top of the window to the beginning
1612 of the display line where the display string
1613 begins. */
1614 start_display (&it3, w, top);
1615 move_it_to (&it3, -1, 0, top_y, -1, MOVE_TO_X | MOVE_TO_Y);
1616 /* If it3_moved stays zero after the 'while' loop
1617 below, that means we already were at a newline
1618 before the loop (e.g., the display string begins
1619 with a newline), so we don't need to (and cannot)
1620 inspect the glyphs of it3.glyph_row, because
1621 PRODUCE_GLYPHS will not produce anything for a
1622 newline, and thus it3.glyph_row stays at its
1623 stale content it got at top of the window. */
1624 it3_moved = 0;
1625 /* Finally, advance the iterator until we hit the
1626 first display element whose character position is
1627 CHARPOS, or until the first newline from the
1628 display string, which signals the end of the
1629 display line. */
1630 while (get_next_display_element (&it3))
1632 PRODUCE_GLYPHS (&it3);
1633 if (IT_CHARPOS (it3) == charpos
1634 || ITERATOR_AT_END_OF_LINE_P (&it3))
1635 break;
1636 it3_moved = 1;
1637 set_iterator_to_next (&it3, 0);
1639 top_x = it3.current_x - it3.pixel_width;
1640 /* Normally, we would exit the above loop because we
1641 found the display element whose character
1642 position is CHARPOS. For the contingency that we
1643 didn't, and stopped at the first newline from the
1644 display string, move back over the glyphs
1645 produced from the string, until we find the
1646 rightmost glyph not from the string. */
1647 if (it3_moved
1648 && newline_in_string
1649 && IT_CHARPOS (it3) != charpos && EQ (it3.object, string))
1651 struct glyph *g = it3.glyph_row->glyphs[TEXT_AREA]
1652 + it3.glyph_row->used[TEXT_AREA];
1654 while (EQ ((g - 1)->object, string))
1656 --g;
1657 top_x -= g->pixel_width;
1659 eassert (g < it3.glyph_row->glyphs[TEXT_AREA]
1660 + it3.glyph_row->used[TEXT_AREA]);
1665 *x = top_x;
1666 *y = max (top_y + max (0, it.max_ascent - it.ascent), window_top_y);
1667 *rtop = max (0, window_top_y - top_y);
1668 *rbot = max (0, bottom_y - it.last_visible_y);
1669 *rowh = max (0, (min (bottom_y, it.last_visible_y)
1670 - max (top_y, window_top_y)));
1671 *vpos = it.vpos;
1674 else
1676 /* We were asked to provide info about WINDOW_END. */
1677 struct it it2;
1678 void *it2data = NULL;
1680 SAVE_IT (it2, it, it2data);
1681 if (IT_CHARPOS (it) < ZV && FETCH_BYTE (IT_BYTEPOS (it)) != '\n')
1682 move_it_by_lines (&it, 1);
1683 if (charpos < IT_CHARPOS (it)
1684 || (it.what == IT_EOB && charpos == IT_CHARPOS (it)))
1686 visible_p = true;
1687 RESTORE_IT (&it2, &it2, it2data);
1688 move_it_to (&it2, charpos, -1, -1, -1, MOVE_TO_POS);
1689 *x = it2.current_x;
1690 *y = it2.current_y + it2.max_ascent - it2.ascent;
1691 *rtop = max (0, -it2.current_y);
1692 *rbot = max (0, ((it2.current_y + it2.max_ascent + it2.max_descent)
1693 - it.last_visible_y));
1694 *rowh = max (0, (min (it2.current_y + it2.max_ascent + it2.max_descent,
1695 it.last_visible_y)
1696 - max (it2.current_y,
1697 WINDOW_HEADER_LINE_HEIGHT (w))));
1698 *vpos = it2.vpos;
1700 else
1701 bidi_unshelve_cache (it2data, 1);
1703 bidi_unshelve_cache (itdata, 0);
1705 if (old_buffer)
1706 set_buffer_internal_1 (old_buffer);
1708 if (visible_p && w->hscroll > 0)
1709 *x -=
1710 window_hscroll_limited (w, WINDOW_XFRAME (w))
1711 * WINDOW_FRAME_COLUMN_WIDTH (w);
1713 #if 0
1714 /* Debugging code. */
1715 if (visible_p)
1716 fprintf (stderr, "+pv pt=%d vs=%d --> x=%d y=%d rt=%d rb=%d rh=%d vp=%d\n",
1717 charpos, w->vscroll, *x, *y, *rtop, *rbot, *rowh, *vpos);
1718 else
1719 fprintf (stderr, "-pv pt=%d vs=%d\n", charpos, w->vscroll);
1720 #endif
1722 return visible_p;
1726 /* Return the next character from STR. Return in *LEN the length of
1727 the character. This is like STRING_CHAR_AND_LENGTH but never
1728 returns an invalid character. If we find one, we return a `?', but
1729 with the length of the invalid character. */
1731 static int
1732 string_char_and_length (const unsigned char *str, int *len)
1734 int c;
1736 c = STRING_CHAR_AND_LENGTH (str, *len);
1737 if (!CHAR_VALID_P (c))
1738 /* We may not change the length here because other places in Emacs
1739 don't use this function, i.e. they silently accept invalid
1740 characters. */
1741 c = '?';
1743 return c;
1748 /* Given a position POS containing a valid character and byte position
1749 in STRING, return the position NCHARS ahead (NCHARS >= 0). */
1751 static struct text_pos
1752 string_pos_nchars_ahead (struct text_pos pos, Lisp_Object string, ptrdiff_t nchars)
1754 eassert (STRINGP (string) && nchars >= 0);
1756 if (STRING_MULTIBYTE (string))
1758 const unsigned char *p = SDATA (string) + BYTEPOS (pos);
1759 int len;
1761 while (nchars--)
1763 string_char_and_length (p, &len);
1764 p += len;
1765 CHARPOS (pos) += 1;
1766 BYTEPOS (pos) += len;
1769 else
1770 SET_TEXT_POS (pos, CHARPOS (pos) + nchars, BYTEPOS (pos) + nchars);
1772 return pos;
1776 /* Value is the text position, i.e. character and byte position,
1777 for character position CHARPOS in STRING. */
1779 static struct text_pos
1780 string_pos (ptrdiff_t charpos, Lisp_Object string)
1782 struct text_pos pos;
1783 eassert (STRINGP (string));
1784 eassert (charpos >= 0);
1785 SET_TEXT_POS (pos, charpos, string_char_to_byte (string, charpos));
1786 return pos;
1790 /* Value is a text position, i.e. character and byte position, for
1791 character position CHARPOS in C string S. MULTIBYTE_P non-zero
1792 means recognize multibyte characters. */
1794 static struct text_pos
1795 c_string_pos (ptrdiff_t charpos, const char *s, bool multibyte_p)
1797 struct text_pos pos;
1799 eassert (s != NULL);
1800 eassert (charpos >= 0);
1802 if (multibyte_p)
1804 int len;
1806 SET_TEXT_POS (pos, 0, 0);
1807 while (charpos--)
1809 string_char_and_length ((const unsigned char *) s, &len);
1810 s += len;
1811 CHARPOS (pos) += 1;
1812 BYTEPOS (pos) += len;
1815 else
1816 SET_TEXT_POS (pos, charpos, charpos);
1818 return pos;
1822 /* Value is the number of characters in C string S. MULTIBYTE_P
1823 non-zero means recognize multibyte characters. */
1825 static ptrdiff_t
1826 number_of_chars (const char *s, bool multibyte_p)
1828 ptrdiff_t nchars;
1830 if (multibyte_p)
1832 ptrdiff_t rest = strlen (s);
1833 int len;
1834 const unsigned char *p = (const unsigned char *) s;
1836 for (nchars = 0; rest > 0; ++nchars)
1838 string_char_and_length (p, &len);
1839 rest -= len, p += len;
1842 else
1843 nchars = strlen (s);
1845 return nchars;
1849 /* Compute byte position NEWPOS->bytepos corresponding to
1850 NEWPOS->charpos. POS is a known position in string STRING.
1851 NEWPOS->charpos must be >= POS.charpos. */
1853 static void
1854 compute_string_pos (struct text_pos *newpos, struct text_pos pos, Lisp_Object string)
1856 eassert (STRINGP (string));
1857 eassert (CHARPOS (*newpos) >= CHARPOS (pos));
1859 if (STRING_MULTIBYTE (string))
1860 *newpos = string_pos_nchars_ahead (pos, string,
1861 CHARPOS (*newpos) - CHARPOS (pos));
1862 else
1863 BYTEPOS (*newpos) = CHARPOS (*newpos);
1866 /* EXPORT:
1867 Return an estimation of the pixel height of mode or header lines on
1868 frame F. FACE_ID specifies what line's height to estimate. */
1871 estimate_mode_line_height (struct frame *f, enum face_id face_id)
1873 #ifdef HAVE_WINDOW_SYSTEM
1874 if (FRAME_WINDOW_P (f))
1876 int height = FONT_HEIGHT (FRAME_FONT (f));
1878 /* This function is called so early when Emacs starts that the face
1879 cache and mode line face are not yet initialized. */
1880 if (FRAME_FACE_CACHE (f))
1882 struct face *face = FACE_FROM_ID (f, face_id);
1883 if (face)
1885 if (face->font)
1886 height = FONT_HEIGHT (face->font);
1887 if (face->box_line_width > 0)
1888 height += 2 * face->box_line_width;
1892 return height;
1894 #endif
1896 return 1;
1899 /* Given a pixel position (PIX_X, PIX_Y) on frame F, return glyph
1900 co-ordinates in (*X, *Y). Set *BOUNDS to the rectangle that the
1901 glyph at X, Y occupies, if BOUNDS != 0. If NOCLIP is non-zero, do
1902 not force the value into range. */
1904 void
1905 pixel_to_glyph_coords (struct frame *f, register int pix_x, register int pix_y,
1906 int *x, int *y, NativeRectangle *bounds, int noclip)
1909 #ifdef HAVE_WINDOW_SYSTEM
1910 if (FRAME_WINDOW_P (f))
1912 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to round down
1913 even for negative values. */
1914 if (pix_x < 0)
1915 pix_x -= FRAME_COLUMN_WIDTH (f) - 1;
1916 if (pix_y < 0)
1917 pix_y -= FRAME_LINE_HEIGHT (f) - 1;
1919 pix_x = FRAME_PIXEL_X_TO_COL (f, pix_x);
1920 pix_y = FRAME_PIXEL_Y_TO_LINE (f, pix_y);
1922 if (bounds)
1923 STORE_NATIVE_RECT (*bounds,
1924 FRAME_COL_TO_PIXEL_X (f, pix_x),
1925 FRAME_LINE_TO_PIXEL_Y (f, pix_y),
1926 FRAME_COLUMN_WIDTH (f) - 1,
1927 FRAME_LINE_HEIGHT (f) - 1);
1929 /* PXW: Should we clip pixels before converting to columns/lines? */
1930 if (!noclip)
1932 if (pix_x < 0)
1933 pix_x = 0;
1934 else if (pix_x > FRAME_TOTAL_COLS (f))
1935 pix_x = FRAME_TOTAL_COLS (f);
1937 if (pix_y < 0)
1938 pix_y = 0;
1939 else if (pix_y > FRAME_LINES (f))
1940 pix_y = FRAME_LINES (f);
1943 #endif
1945 *x = pix_x;
1946 *y = pix_y;
1950 /* Find the glyph under window-relative coordinates X/Y in window W.
1951 Consider only glyphs from buffer text, i.e. no glyphs from overlay
1952 strings. Return in *HPOS and *VPOS the row and column number of
1953 the glyph found. Return in *AREA the glyph area containing X.
1954 Value is a pointer to the glyph found or null if X/Y is not on
1955 text, or we can't tell because W's current matrix is not up to
1956 date. */
1958 static struct glyph *
1959 x_y_to_hpos_vpos (struct window *w, int x, int y, int *hpos, int *vpos,
1960 int *dx, int *dy, int *area)
1962 struct glyph *glyph, *end;
1963 struct glyph_row *row = NULL;
1964 int x0, i;
1966 /* Find row containing Y. Give up if some row is not enabled. */
1967 for (i = 0; i < w->current_matrix->nrows; ++i)
1969 row = MATRIX_ROW (w->current_matrix, i);
1970 if (!row->enabled_p)
1971 return NULL;
1972 if (y >= row->y && y < MATRIX_ROW_BOTTOM_Y (row))
1973 break;
1976 *vpos = i;
1977 *hpos = 0;
1979 /* Give up if Y is not in the window. */
1980 if (i == w->current_matrix->nrows)
1981 return NULL;
1983 /* Get the glyph area containing X. */
1984 if (w->pseudo_window_p)
1986 *area = TEXT_AREA;
1987 x0 = 0;
1989 else
1991 if (x < window_box_left_offset (w, TEXT_AREA))
1993 *area = LEFT_MARGIN_AREA;
1994 x0 = window_box_left_offset (w, LEFT_MARGIN_AREA);
1996 else if (x < window_box_right_offset (w, TEXT_AREA))
1998 *area = TEXT_AREA;
1999 x0 = window_box_left_offset (w, TEXT_AREA) + min (row->x, 0);
2001 else
2003 *area = RIGHT_MARGIN_AREA;
2004 x0 = window_box_left_offset (w, RIGHT_MARGIN_AREA);
2008 /* Find glyph containing X. */
2009 glyph = row->glyphs[*area];
2010 end = glyph + row->used[*area];
2011 x -= x0;
2012 while (glyph < end && x >= glyph->pixel_width)
2014 x -= glyph->pixel_width;
2015 ++glyph;
2018 if (glyph == end)
2019 return NULL;
2021 if (dx)
2023 *dx = x;
2024 *dy = y - (row->y + row->ascent - glyph->ascent);
2027 *hpos = glyph - row->glyphs[*area];
2028 return glyph;
2031 /* Convert frame-relative x/y to coordinates relative to window W.
2032 Takes pseudo-windows into account. */
2034 static void
2035 frame_to_window_pixel_xy (struct window *w, int *x, int *y)
2037 if (w->pseudo_window_p)
2039 /* A pseudo-window is always full-width, and starts at the
2040 left edge of the frame, plus a frame border. */
2041 struct frame *f = XFRAME (w->frame);
2042 *x -= FRAME_INTERNAL_BORDER_WIDTH (f);
2043 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
2045 else
2047 *x -= WINDOW_LEFT_EDGE_X (w);
2048 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
2052 #ifdef HAVE_WINDOW_SYSTEM
2054 /* EXPORT:
2055 Return in RECTS[] at most N clipping rectangles for glyph string S.
2056 Return the number of stored rectangles. */
2059 get_glyph_string_clip_rects (struct glyph_string *s, NativeRectangle *rects, int n)
2061 XRectangle r;
2063 if (n <= 0)
2064 return 0;
2066 if (s->row->full_width_p)
2068 /* Draw full-width. X coordinates are relative to S->w->left_col. */
2069 r.x = WINDOW_LEFT_EDGE_X (s->w);
2070 if (s->row->mode_line_p)
2071 r.width = WINDOW_PIXEL_WIDTH (s->w) - WINDOW_RIGHT_DIVIDER_WIDTH (s->w);
2072 else
2073 r.width = WINDOW_PIXEL_WIDTH (s->w);
2075 /* Unless displaying a mode or menu bar line, which are always
2076 fully visible, clip to the visible part of the row. */
2077 if (s->w->pseudo_window_p)
2078 r.height = s->row->visible_height;
2079 else
2080 r.height = s->height;
2082 else
2084 /* This is a text line that may be partially visible. */
2085 r.x = window_box_left (s->w, s->area);
2086 r.width = window_box_width (s->w, s->area);
2087 r.height = s->row->visible_height;
2090 if (s->clip_head)
2091 if (r.x < s->clip_head->x)
2093 if (r.width >= s->clip_head->x - r.x)
2094 r.width -= s->clip_head->x - r.x;
2095 else
2096 r.width = 0;
2097 r.x = s->clip_head->x;
2099 if (s->clip_tail)
2100 if (r.x + r.width > s->clip_tail->x + s->clip_tail->background_width)
2102 if (s->clip_tail->x + s->clip_tail->background_width >= r.x)
2103 r.width = s->clip_tail->x + s->clip_tail->background_width - r.x;
2104 else
2105 r.width = 0;
2108 /* If S draws overlapping rows, it's sufficient to use the top and
2109 bottom of the window for clipping because this glyph string
2110 intentionally draws over other lines. */
2111 if (s->for_overlaps)
2113 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
2114 r.height = window_text_bottom_y (s->w) - r.y;
2116 /* Alas, the above simple strategy does not work for the
2117 environments with anti-aliased text: if the same text is
2118 drawn onto the same place multiple times, it gets thicker.
2119 If the overlap we are processing is for the erased cursor, we
2120 take the intersection with the rectangle of the cursor. */
2121 if (s->for_overlaps & OVERLAPS_ERASED_CURSOR)
2123 XRectangle rc, r_save = r;
2125 rc.x = WINDOW_TEXT_TO_FRAME_PIXEL_X (s->w, s->w->phys_cursor.x);
2126 rc.y = s->w->phys_cursor.y;
2127 rc.width = s->w->phys_cursor_width;
2128 rc.height = s->w->phys_cursor_height;
2130 x_intersect_rectangles (&r_save, &rc, &r);
2133 else
2135 /* Don't use S->y for clipping because it doesn't take partially
2136 visible lines into account. For example, it can be negative for
2137 partially visible lines at the top of a window. */
2138 if (!s->row->full_width_p
2139 && MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (s->w, s->row))
2140 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
2141 else
2142 r.y = max (0, s->row->y);
2145 r.y = WINDOW_TO_FRAME_PIXEL_Y (s->w, r.y);
2147 /* If drawing the cursor, don't let glyph draw outside its
2148 advertised boundaries. Cleartype does this under some circumstances. */
2149 if (s->hl == DRAW_CURSOR)
2151 struct glyph *glyph = s->first_glyph;
2152 int height, max_y;
2154 if (s->x > r.x)
2156 r.width -= s->x - r.x;
2157 r.x = s->x;
2159 r.width = min (r.width, glyph->pixel_width);
2161 /* If r.y is below window bottom, ensure that we still see a cursor. */
2162 height = min (glyph->ascent + glyph->descent,
2163 min (FRAME_LINE_HEIGHT (s->f), s->row->visible_height));
2164 max_y = window_text_bottom_y (s->w) - height;
2165 max_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, max_y);
2166 if (s->ybase - glyph->ascent > max_y)
2168 r.y = max_y;
2169 r.height = height;
2171 else
2173 /* Don't draw cursor glyph taller than our actual glyph. */
2174 height = max (FRAME_LINE_HEIGHT (s->f), glyph->ascent + glyph->descent);
2175 if (height < r.height)
2177 max_y = r.y + r.height;
2178 r.y = min (max_y, max (r.y, s->ybase + glyph->descent - height));
2179 r.height = min (max_y - r.y, height);
2184 if (s->row->clip)
2186 XRectangle r_save = r;
2188 if (! x_intersect_rectangles (&r_save, s->row->clip, &r))
2189 r.width = 0;
2192 if ((s->for_overlaps & OVERLAPS_BOTH) == 0
2193 || ((s->for_overlaps & OVERLAPS_BOTH) == OVERLAPS_BOTH && n == 1))
2195 #ifdef CONVERT_FROM_XRECT
2196 CONVERT_FROM_XRECT (r, *rects);
2197 #else
2198 *rects = r;
2199 #endif
2200 return 1;
2202 else
2204 /* If we are processing overlapping and allowed to return
2205 multiple clipping rectangles, we exclude the row of the glyph
2206 string from the clipping rectangle. This is to avoid drawing
2207 the same text on the environment with anti-aliasing. */
2208 #ifdef CONVERT_FROM_XRECT
2209 XRectangle rs[2];
2210 #else
2211 XRectangle *rs = rects;
2212 #endif
2213 int i = 0, row_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, s->row->y);
2215 if (s->for_overlaps & OVERLAPS_PRED)
2217 rs[i] = r;
2218 if (r.y + r.height > row_y)
2220 if (r.y < row_y)
2221 rs[i].height = row_y - r.y;
2222 else
2223 rs[i].height = 0;
2225 i++;
2227 if (s->for_overlaps & OVERLAPS_SUCC)
2229 rs[i] = r;
2230 if (r.y < row_y + s->row->visible_height)
2232 if (r.y + r.height > row_y + s->row->visible_height)
2234 rs[i].y = row_y + s->row->visible_height;
2235 rs[i].height = r.y + r.height - rs[i].y;
2237 else
2238 rs[i].height = 0;
2240 i++;
2243 n = i;
2244 #ifdef CONVERT_FROM_XRECT
2245 for (i = 0; i < n; i++)
2246 CONVERT_FROM_XRECT (rs[i], rects[i]);
2247 #endif
2248 return n;
2252 /* EXPORT:
2253 Return in *NR the clipping rectangle for glyph string S. */
2255 void
2256 get_glyph_string_clip_rect (struct glyph_string *s, NativeRectangle *nr)
2258 get_glyph_string_clip_rects (s, nr, 1);
2262 /* EXPORT:
2263 Return the position and height of the phys cursor in window W.
2264 Set w->phys_cursor_width to width of phys cursor.
2267 void
2268 get_phys_cursor_geometry (struct window *w, struct glyph_row *row,
2269 struct glyph *glyph, int *xp, int *yp, int *heightp)
2271 struct frame *f = XFRAME (WINDOW_FRAME (w));
2272 int x, y, wd, h, h0, y0;
2274 /* Compute the width of the rectangle to draw. If on a stretch
2275 glyph, and `x-stretch-block-cursor' is nil, don't draw a
2276 rectangle as wide as the glyph, but use a canonical character
2277 width instead. */
2278 wd = glyph->pixel_width - 1;
2279 #if defined (HAVE_NTGUI) || defined (HAVE_NS)
2280 wd++; /* Why? */
2281 #endif
2283 x = w->phys_cursor.x;
2284 if (x < 0)
2286 wd += x;
2287 x = 0;
2290 if (glyph->type == STRETCH_GLYPH
2291 && !x_stretch_cursor_p)
2292 wd = min (FRAME_COLUMN_WIDTH (f), wd);
2293 w->phys_cursor_width = wd;
2295 y = w->phys_cursor.y + row->ascent - glyph->ascent;
2297 /* If y is below window bottom, ensure that we still see a cursor. */
2298 h0 = min (FRAME_LINE_HEIGHT (f), row->visible_height);
2300 h = max (h0, glyph->ascent + glyph->descent);
2301 h0 = min (h0, glyph->ascent + glyph->descent);
2303 y0 = WINDOW_HEADER_LINE_HEIGHT (w);
2304 if (y < y0)
2306 h = max (h - (y0 - y) + 1, h0);
2307 y = y0 - 1;
2309 else
2311 y0 = window_text_bottom_y (w) - h0;
2312 if (y > y0)
2314 h += y - y0;
2315 y = y0;
2319 *xp = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, x);
2320 *yp = WINDOW_TO_FRAME_PIXEL_Y (w, y);
2321 *heightp = h;
2325 * Remember which glyph the mouse is over.
2328 void
2329 remember_mouse_glyph (struct frame *f, int gx, int gy, NativeRectangle *rect)
2331 Lisp_Object window;
2332 struct window *w;
2333 struct glyph_row *r, *gr, *end_row;
2334 enum window_part part;
2335 enum glyph_row_area area;
2336 int x, y, width, height;
2338 /* Try to determine frame pixel position and size of the glyph under
2339 frame pixel coordinates X/Y on frame F. */
2341 if (window_resize_pixelwise)
2343 width = height = 1;
2344 goto virtual_glyph;
2346 else if (!f->glyphs_initialized_p
2347 || (window = window_from_coordinates (f, gx, gy, &part, 0),
2348 NILP (window)))
2350 width = FRAME_SMALLEST_CHAR_WIDTH (f);
2351 height = FRAME_SMALLEST_FONT_HEIGHT (f);
2352 goto virtual_glyph;
2355 w = XWINDOW (window);
2356 width = WINDOW_FRAME_COLUMN_WIDTH (w);
2357 height = WINDOW_FRAME_LINE_HEIGHT (w);
2359 x = window_relative_x_coord (w, part, gx);
2360 y = gy - WINDOW_TOP_EDGE_Y (w);
2362 r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
2363 end_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
2365 if (w->pseudo_window_p)
2367 area = TEXT_AREA;
2368 part = ON_MODE_LINE; /* Don't adjust margin. */
2369 goto text_glyph;
2372 switch (part)
2374 case ON_LEFT_MARGIN:
2375 area = LEFT_MARGIN_AREA;
2376 goto text_glyph;
2378 case ON_RIGHT_MARGIN:
2379 area = RIGHT_MARGIN_AREA;
2380 goto text_glyph;
2382 case ON_HEADER_LINE:
2383 case ON_MODE_LINE:
2384 gr = (part == ON_HEADER_LINE
2385 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
2386 : MATRIX_MODE_LINE_ROW (w->current_matrix));
2387 gy = gr->y;
2388 area = TEXT_AREA;
2389 goto text_glyph_row_found;
2391 case ON_TEXT:
2392 area = TEXT_AREA;
2394 text_glyph:
2395 gr = 0; gy = 0;
2396 for (; r <= end_row && r->enabled_p; ++r)
2397 if (r->y + r->height > y)
2399 gr = r; gy = r->y;
2400 break;
2403 text_glyph_row_found:
2404 if (gr && gy <= y)
2406 struct glyph *g = gr->glyphs[area];
2407 struct glyph *end = g + gr->used[area];
2409 height = gr->height;
2410 for (gx = gr->x; g < end; gx += g->pixel_width, ++g)
2411 if (gx + g->pixel_width > x)
2412 break;
2414 if (g < end)
2416 if (g->type == IMAGE_GLYPH)
2418 /* Don't remember when mouse is over image, as
2419 image may have hot-spots. */
2420 STORE_NATIVE_RECT (*rect, 0, 0, 0, 0);
2421 return;
2423 width = g->pixel_width;
2425 else
2427 /* Use nominal char spacing at end of line. */
2428 x -= gx;
2429 gx += (x / width) * width;
2432 if (part != ON_MODE_LINE && part != ON_HEADER_LINE)
2434 gx += window_box_left_offset (w, area);
2435 /* Don't expand over the modeline to make sure the vertical
2436 drag cursor is shown early enough. */
2437 height = min (height,
2438 max (0, WINDOW_BOX_HEIGHT_NO_MODE_LINE (w) - gy));
2441 else
2443 /* Use nominal line height at end of window. */
2444 gx = (x / width) * width;
2445 y -= gy;
2446 gy += (y / height) * height;
2447 if (part != ON_MODE_LINE && part != ON_HEADER_LINE)
2448 /* See comment above. */
2449 height = min (height,
2450 max (0, WINDOW_BOX_HEIGHT_NO_MODE_LINE (w) - gy));
2452 break;
2454 case ON_LEFT_FRINGE:
2455 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2456 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w)
2457 : window_box_right_offset (w, LEFT_MARGIN_AREA));
2458 width = WINDOW_LEFT_FRINGE_WIDTH (w);
2459 goto row_glyph;
2461 case ON_RIGHT_FRINGE:
2462 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2463 ? window_box_right_offset (w, RIGHT_MARGIN_AREA)
2464 : window_box_right_offset (w, TEXT_AREA));
2465 if (WINDOW_RIGHT_DIVIDER_WIDTH (w) == 0
2466 && !WINDOW_HAS_VERTICAL_SCROLL_BAR (w)
2467 && !WINDOW_RIGHTMOST_P (w))
2468 if (gx < WINDOW_PIXEL_WIDTH (w) - width)
2469 /* Make sure the vertical border can get her own glyph to the
2470 right of the one we build here. */
2471 width = WINDOW_RIGHT_FRINGE_WIDTH (w) - width;
2472 else
2473 width = WINDOW_PIXEL_WIDTH (w) - gx;
2474 else
2475 width = WINDOW_RIGHT_FRINGE_WIDTH (w);
2477 goto row_glyph;
2479 case ON_VERTICAL_BORDER:
2480 gx = WINDOW_PIXEL_WIDTH (w) - width;
2481 goto row_glyph;
2483 case ON_SCROLL_BAR:
2484 gx = (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w)
2486 : (window_box_right_offset (w, RIGHT_MARGIN_AREA)
2487 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2488 ? WINDOW_RIGHT_FRINGE_WIDTH (w)
2489 : 0)));
2490 width = WINDOW_SCROLL_BAR_AREA_WIDTH (w);
2492 row_glyph:
2493 gr = 0, gy = 0;
2494 for (; r <= end_row && r->enabled_p; ++r)
2495 if (r->y + r->height > y)
2497 gr = r; gy = r->y;
2498 break;
2501 if (gr && gy <= y)
2502 height = gr->height;
2503 else
2505 /* Use nominal line height at end of window. */
2506 y -= gy;
2507 gy += (y / height) * height;
2509 break;
2511 case ON_RIGHT_DIVIDER:
2512 gx = WINDOW_PIXEL_WIDTH (w) - WINDOW_RIGHT_DIVIDER_WIDTH (w);
2513 width = WINDOW_RIGHT_DIVIDER_WIDTH (w);
2514 gy = 0;
2515 /* The bottom divider prevails. */
2516 height = WINDOW_PIXEL_HEIGHT (w) - WINDOW_BOTTOM_DIVIDER_WIDTH (w);
2517 goto add_edge;;
2519 case ON_BOTTOM_DIVIDER:
2520 gx = 0;
2521 width = WINDOW_PIXEL_WIDTH (w);
2522 gy = WINDOW_PIXEL_HEIGHT (w) - WINDOW_BOTTOM_DIVIDER_WIDTH (w);
2523 height = WINDOW_BOTTOM_DIVIDER_WIDTH (w);
2524 goto add_edge;
2526 default:
2528 virtual_glyph:
2529 /* If there is no glyph under the mouse, then we divide the screen
2530 into a grid of the smallest glyph in the frame, and use that
2531 as our "glyph". */
2533 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to
2534 round down even for negative values. */
2535 if (gx < 0)
2536 gx -= width - 1;
2537 if (gy < 0)
2538 gy -= height - 1;
2540 gx = (gx / width) * width;
2541 gy = (gy / height) * height;
2543 goto store_rect;
2546 add_edge:
2547 gx += WINDOW_LEFT_EDGE_X (w);
2548 gy += WINDOW_TOP_EDGE_Y (w);
2550 store_rect:
2551 STORE_NATIVE_RECT (*rect, gx, gy, width, height);
2553 /* Visible feedback for debugging. */
2554 #if 0
2555 #if HAVE_X_WINDOWS
2556 XDrawRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
2557 f->output_data.x->normal_gc,
2558 gx, gy, width, height);
2559 #endif
2560 #endif
2564 #endif /* HAVE_WINDOW_SYSTEM */
2566 static void
2567 adjust_window_ends (struct window *w, struct glyph_row *row, bool current)
2569 eassert (w);
2570 w->window_end_pos = Z - MATRIX_ROW_END_CHARPOS (row);
2571 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
2572 w->window_end_vpos
2573 = MATRIX_ROW_VPOS (row, current ? w->current_matrix : w->desired_matrix);
2576 /***********************************************************************
2577 Lisp form evaluation
2578 ***********************************************************************/
2580 /* Error handler for safe_eval and safe_call. */
2582 static Lisp_Object
2583 safe_eval_handler (Lisp_Object arg, ptrdiff_t nargs, Lisp_Object *args)
2585 add_to_log ("Error during redisplay: %S signaled %S",
2586 Flist (nargs, args), arg);
2587 return Qnil;
2590 /* Call function FUNC with the rest of NARGS - 1 arguments
2591 following. Return the result, or nil if something went
2592 wrong. Prevent redisplay during the evaluation. */
2594 Lisp_Object
2595 safe_call (ptrdiff_t nargs, Lisp_Object func, ...)
2597 Lisp_Object val;
2599 if (inhibit_eval_during_redisplay)
2600 val = Qnil;
2601 else
2603 va_list ap;
2604 ptrdiff_t i;
2605 ptrdiff_t count = SPECPDL_INDEX ();
2606 struct gcpro gcpro1;
2607 Lisp_Object *args = alloca (nargs * word_size);
2609 args[0] = func;
2610 va_start (ap, func);
2611 for (i = 1; i < nargs; i++)
2612 args[i] = va_arg (ap, Lisp_Object);
2613 va_end (ap);
2615 GCPRO1 (args[0]);
2616 gcpro1.nvars = nargs;
2617 specbind (Qinhibit_redisplay, Qt);
2618 /* Use Qt to ensure debugger does not run,
2619 so there is no possibility of wanting to redisplay. */
2620 val = internal_condition_case_n (Ffuncall, nargs, args, Qt,
2621 safe_eval_handler);
2622 UNGCPRO;
2623 val = unbind_to (count, val);
2626 return val;
2630 /* Call function FN with one argument ARG.
2631 Return the result, or nil if something went wrong. */
2633 Lisp_Object
2634 safe_call1 (Lisp_Object fn, Lisp_Object arg)
2636 return safe_call (2, fn, arg);
2639 static Lisp_Object Qeval;
2641 Lisp_Object
2642 safe_eval (Lisp_Object sexpr)
2644 return safe_call1 (Qeval, sexpr);
2647 /* Call function FN with two arguments ARG1 and ARG2.
2648 Return the result, or nil if something went wrong. */
2650 Lisp_Object
2651 safe_call2 (Lisp_Object fn, Lisp_Object arg1, Lisp_Object arg2)
2653 return safe_call (3, fn, arg1, arg2);
2658 /***********************************************************************
2659 Debugging
2660 ***********************************************************************/
2662 #if 0
2664 /* Define CHECK_IT to perform sanity checks on iterators.
2665 This is for debugging. It is too slow to do unconditionally. */
2667 static void
2668 check_it (struct it *it)
2670 if (it->method == GET_FROM_STRING)
2672 eassert (STRINGP (it->string));
2673 eassert (IT_STRING_CHARPOS (*it) >= 0);
2675 else
2677 eassert (IT_STRING_CHARPOS (*it) < 0);
2678 if (it->method == GET_FROM_BUFFER)
2680 /* Check that character and byte positions agree. */
2681 eassert (IT_CHARPOS (*it) == BYTE_TO_CHAR (IT_BYTEPOS (*it)));
2685 if (it->dpvec)
2686 eassert (it->current.dpvec_index >= 0);
2687 else
2688 eassert (it->current.dpvec_index < 0);
2691 #define CHECK_IT(IT) check_it ((IT))
2693 #else /* not 0 */
2695 #define CHECK_IT(IT) (void) 0
2697 #endif /* not 0 */
2700 #if defined GLYPH_DEBUG && defined ENABLE_CHECKING
2702 /* Check that the window end of window W is what we expect it
2703 to be---the last row in the current matrix displaying text. */
2705 static void
2706 check_window_end (struct window *w)
2708 if (!MINI_WINDOW_P (w) && w->window_end_valid)
2710 struct glyph_row *row;
2711 eassert ((row = MATRIX_ROW (w->current_matrix, w->window_end_vpos),
2712 !row->enabled_p
2713 || MATRIX_ROW_DISPLAYS_TEXT_P (row)
2714 || MATRIX_ROW_VPOS (row, w->current_matrix) == 0));
2718 #define CHECK_WINDOW_END(W) check_window_end ((W))
2720 #else
2722 #define CHECK_WINDOW_END(W) (void) 0
2724 #endif /* GLYPH_DEBUG and ENABLE_CHECKING */
2726 /***********************************************************************
2727 Iterator initialization
2728 ***********************************************************************/
2730 /* Initialize IT for displaying current_buffer in window W, starting
2731 at character position CHARPOS. CHARPOS < 0 means that no buffer
2732 position is specified which is useful when the iterator is assigned
2733 a position later. BYTEPOS is the byte position corresponding to
2734 CHARPOS.
2736 If ROW is not null, calls to produce_glyphs with IT as parameter
2737 will produce glyphs in that row.
2739 BASE_FACE_ID is the id of a base face to use. It must be one of
2740 DEFAULT_FACE_ID for normal text, MODE_LINE_FACE_ID,
2741 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID for displaying
2742 mode lines, or TOOL_BAR_FACE_ID for displaying the tool-bar.
2744 If ROW is null and BASE_FACE_ID is equal to MODE_LINE_FACE_ID,
2745 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID, the iterator
2746 will be initialized to use the corresponding mode line glyph row of
2747 the desired matrix of W. */
2749 void
2750 init_iterator (struct it *it, struct window *w,
2751 ptrdiff_t charpos, ptrdiff_t bytepos,
2752 struct glyph_row *row, enum face_id base_face_id)
2754 enum face_id remapped_base_face_id = base_face_id;
2756 /* Some precondition checks. */
2757 eassert (w != NULL && it != NULL);
2758 eassert (charpos < 0 || (charpos >= BUF_BEG (current_buffer)
2759 && charpos <= ZV));
2761 /* If face attributes have been changed since the last redisplay,
2762 free realized faces now because they depend on face definitions
2763 that might have changed. Don't free faces while there might be
2764 desired matrices pending which reference these faces. */
2765 if (face_change_count && !inhibit_free_realized_faces)
2767 face_change_count = 0;
2768 free_all_realized_faces (Qnil);
2771 /* Perhaps remap BASE_FACE_ID to a user-specified alternative. */
2772 if (! NILP (Vface_remapping_alist))
2773 remapped_base_face_id
2774 = lookup_basic_face (XFRAME (w->frame), base_face_id);
2776 /* Use one of the mode line rows of W's desired matrix if
2777 appropriate. */
2778 if (row == NULL)
2780 if (base_face_id == MODE_LINE_FACE_ID
2781 || base_face_id == MODE_LINE_INACTIVE_FACE_ID)
2782 row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
2783 else if (base_face_id == HEADER_LINE_FACE_ID)
2784 row = MATRIX_HEADER_LINE_ROW (w->desired_matrix);
2787 /* Clear IT. */
2788 memset (it, 0, sizeof *it);
2789 it->current.overlay_string_index = -1;
2790 it->current.dpvec_index = -1;
2791 it->base_face_id = remapped_base_face_id;
2792 it->string = Qnil;
2793 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
2794 it->paragraph_embedding = L2R;
2795 it->bidi_it.string.lstring = Qnil;
2796 it->bidi_it.string.s = NULL;
2797 it->bidi_it.string.bufpos = 0;
2798 it->bidi_it.w = w;
2800 /* The window in which we iterate over current_buffer: */
2801 XSETWINDOW (it->window, w);
2802 it->w = w;
2803 it->f = XFRAME (w->frame);
2805 it->cmp_it.id = -1;
2807 /* Extra space between lines (on window systems only). */
2808 if (base_face_id == DEFAULT_FACE_ID
2809 && FRAME_WINDOW_P (it->f))
2811 if (NATNUMP (BVAR (current_buffer, extra_line_spacing)))
2812 it->extra_line_spacing = XFASTINT (BVAR (current_buffer, extra_line_spacing));
2813 else if (FLOATP (BVAR (current_buffer, extra_line_spacing)))
2814 it->extra_line_spacing = (XFLOAT_DATA (BVAR (current_buffer, extra_line_spacing))
2815 * FRAME_LINE_HEIGHT (it->f));
2816 else if (it->f->extra_line_spacing > 0)
2817 it->extra_line_spacing = it->f->extra_line_spacing;
2818 it->max_extra_line_spacing = 0;
2821 /* If realized faces have been removed, e.g. because of face
2822 attribute changes of named faces, recompute them. When running
2823 in batch mode, the face cache of the initial frame is null. If
2824 we happen to get called, make a dummy face cache. */
2825 if (FRAME_FACE_CACHE (it->f) == NULL)
2826 init_frame_faces (it->f);
2827 if (FRAME_FACE_CACHE (it->f)->used == 0)
2828 recompute_basic_faces (it->f);
2830 /* Current value of the `slice', `space-width', and 'height' properties. */
2831 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
2832 it->space_width = Qnil;
2833 it->font_height = Qnil;
2834 it->override_ascent = -1;
2836 /* Are control characters displayed as `^C'? */
2837 it->ctl_arrow_p = !NILP (BVAR (current_buffer, ctl_arrow));
2839 /* -1 means everything between a CR and the following line end
2840 is invisible. >0 means lines indented more than this value are
2841 invisible. */
2842 it->selective = (INTEGERP (BVAR (current_buffer, selective_display))
2843 ? (clip_to_bounds
2844 (-1, XINT (BVAR (current_buffer, selective_display)),
2845 PTRDIFF_MAX))
2846 : (!NILP (BVAR (current_buffer, selective_display))
2847 ? -1 : 0));
2848 it->selective_display_ellipsis_p
2849 = !NILP (BVAR (current_buffer, selective_display_ellipses));
2851 /* Display table to use. */
2852 it->dp = window_display_table (w);
2854 /* Are multibyte characters enabled in current_buffer? */
2855 it->multibyte_p = !NILP (BVAR (current_buffer, enable_multibyte_characters));
2857 /* Get the position at which the redisplay_end_trigger hook should
2858 be run, if it is to be run at all. */
2859 if (MARKERP (w->redisplay_end_trigger)
2860 && XMARKER (w->redisplay_end_trigger)->buffer != 0)
2861 it->redisplay_end_trigger_charpos
2862 = marker_position (w->redisplay_end_trigger);
2863 else if (INTEGERP (w->redisplay_end_trigger))
2864 it->redisplay_end_trigger_charpos
2865 = clip_to_bounds (PTRDIFF_MIN, XINT (w->redisplay_end_trigger),
2866 PTRDIFF_MAX);
2868 it->tab_width = SANE_TAB_WIDTH (current_buffer);
2870 /* Are lines in the display truncated? */
2871 if (base_face_id != DEFAULT_FACE_ID
2872 || it->w->hscroll
2873 || (! WINDOW_FULL_WIDTH_P (it->w)
2874 && ((!NILP (Vtruncate_partial_width_windows)
2875 && !INTEGERP (Vtruncate_partial_width_windows))
2876 || (INTEGERP (Vtruncate_partial_width_windows)
2877 /* PXW: Shall we do something about this? */
2878 && (WINDOW_TOTAL_COLS (it->w)
2879 < XINT (Vtruncate_partial_width_windows))))))
2880 it->line_wrap = TRUNCATE;
2881 else if (NILP (BVAR (current_buffer, truncate_lines)))
2882 it->line_wrap = NILP (BVAR (current_buffer, word_wrap))
2883 ? WINDOW_WRAP : WORD_WRAP;
2884 else
2885 it->line_wrap = TRUNCATE;
2887 /* Get dimensions of truncation and continuation glyphs. These are
2888 displayed as fringe bitmaps under X, but we need them for such
2889 frames when the fringes are turned off. But leave the dimensions
2890 zero for tooltip frames, as these glyphs look ugly there and also
2891 sabotage calculations of tooltip dimensions in x-show-tip. */
2892 #ifdef HAVE_WINDOW_SYSTEM
2893 if (!(FRAME_WINDOW_P (it->f)
2894 && FRAMEP (tip_frame)
2895 && it->f == XFRAME (tip_frame)))
2896 #endif
2898 if (it->line_wrap == TRUNCATE)
2900 /* We will need the truncation glyph. */
2901 eassert (it->glyph_row == NULL);
2902 produce_special_glyphs (it, IT_TRUNCATION);
2903 it->truncation_pixel_width = it->pixel_width;
2905 else
2907 /* We will need the continuation glyph. */
2908 eassert (it->glyph_row == NULL);
2909 produce_special_glyphs (it, IT_CONTINUATION);
2910 it->continuation_pixel_width = it->pixel_width;
2914 /* Reset these values to zero because the produce_special_glyphs
2915 above has changed them. */
2916 it->pixel_width = it->ascent = it->descent = 0;
2917 it->phys_ascent = it->phys_descent = 0;
2919 /* Set this after getting the dimensions of truncation and
2920 continuation glyphs, so that we don't produce glyphs when calling
2921 produce_special_glyphs, above. */
2922 it->glyph_row = row;
2923 it->area = TEXT_AREA;
2925 /* Forget any previous info about this row being reversed. */
2926 if (it->glyph_row)
2927 it->glyph_row->reversed_p = 0;
2929 /* Get the dimensions of the display area. The display area
2930 consists of the visible window area plus a horizontally scrolled
2931 part to the left of the window. All x-values are relative to the
2932 start of this total display area. */
2933 if (base_face_id != DEFAULT_FACE_ID)
2935 /* Mode lines, menu bar in terminal frames. */
2936 it->first_visible_x = 0;
2937 it->last_visible_x = WINDOW_PIXEL_WIDTH (w);
2939 else
2941 it->first_visible_x
2942 = window_hscroll_limited (it->w, it->f) * FRAME_COLUMN_WIDTH (it->f);
2943 it->last_visible_x = (it->first_visible_x
2944 + window_box_width (w, TEXT_AREA));
2946 /* If we truncate lines, leave room for the truncation glyph(s) at
2947 the right margin. Otherwise, leave room for the continuation
2948 glyph(s). Done only if the window has no fringes. Since we
2949 don't know at this point whether there will be any R2L lines in
2950 the window, we reserve space for truncation/continuation glyphs
2951 even if only one of the fringes is absent. */
2952 if (WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0
2953 || (it->bidi_p && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0))
2955 if (it->line_wrap == TRUNCATE)
2956 it->last_visible_x -= it->truncation_pixel_width;
2957 else
2958 it->last_visible_x -= it->continuation_pixel_width;
2961 it->header_line_p = WINDOW_WANTS_HEADER_LINE_P (w);
2962 it->current_y = WINDOW_HEADER_LINE_HEIGHT (w) + w->vscroll;
2965 /* Leave room for a border glyph. */
2966 if (!FRAME_WINDOW_P (it->f)
2967 && !WINDOW_RIGHTMOST_P (it->w))
2968 it->last_visible_x -= 1;
2970 it->last_visible_y = window_text_bottom_y (w);
2972 /* For mode lines and alike, arrange for the first glyph having a
2973 left box line if the face specifies a box. */
2974 if (base_face_id != DEFAULT_FACE_ID)
2976 struct face *face;
2978 it->face_id = remapped_base_face_id;
2980 /* If we have a boxed mode line, make the first character appear
2981 with a left box line. */
2982 face = FACE_FROM_ID (it->f, remapped_base_face_id);
2983 if (face && face->box != FACE_NO_BOX)
2984 it->start_of_box_run_p = true;
2987 /* If a buffer position was specified, set the iterator there,
2988 getting overlays and face properties from that position. */
2989 if (charpos >= BUF_BEG (current_buffer))
2991 it->end_charpos = ZV;
2992 eassert (charpos == BYTE_TO_CHAR (bytepos));
2993 IT_CHARPOS (*it) = charpos;
2994 IT_BYTEPOS (*it) = bytepos;
2996 /* We will rely on `reseat' to set this up properly, via
2997 handle_face_prop. */
2998 it->face_id = it->base_face_id;
3000 it->start = it->current;
3001 /* Do we need to reorder bidirectional text? Not if this is a
3002 unibyte buffer: by definition, none of the single-byte
3003 characters are strong R2L, so no reordering is needed. And
3004 bidi.c doesn't support unibyte buffers anyway. Also, don't
3005 reorder while we are loading loadup.el, since the tables of
3006 character properties needed for reordering are not yet
3007 available. */
3008 it->bidi_p =
3009 NILP (Vpurify_flag)
3010 && !NILP (BVAR (current_buffer, bidi_display_reordering))
3011 && it->multibyte_p;
3013 /* If we are to reorder bidirectional text, init the bidi
3014 iterator. */
3015 if (it->bidi_p)
3017 /* Note the paragraph direction that this buffer wants to
3018 use. */
3019 if (EQ (BVAR (current_buffer, bidi_paragraph_direction),
3020 Qleft_to_right))
3021 it->paragraph_embedding = L2R;
3022 else if (EQ (BVAR (current_buffer, bidi_paragraph_direction),
3023 Qright_to_left))
3024 it->paragraph_embedding = R2L;
3025 else
3026 it->paragraph_embedding = NEUTRAL_DIR;
3027 bidi_unshelve_cache (NULL, 0);
3028 bidi_init_it (charpos, IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
3029 &it->bidi_it);
3032 /* Compute faces etc. */
3033 reseat (it, it->current.pos, 1);
3036 CHECK_IT (it);
3040 /* Initialize IT for the display of window W with window start POS. */
3042 void
3043 start_display (struct it *it, struct window *w, struct text_pos pos)
3045 struct glyph_row *row;
3046 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
3048 row = w->desired_matrix->rows + first_vpos;
3049 init_iterator (it, w, CHARPOS (pos), BYTEPOS (pos), row, DEFAULT_FACE_ID);
3050 it->first_vpos = first_vpos;
3052 /* Don't reseat to previous visible line start if current start
3053 position is in a string or image. */
3054 if (it->method == GET_FROM_BUFFER && it->line_wrap != TRUNCATE)
3056 int start_at_line_beg_p;
3057 int first_y = it->current_y;
3059 /* If window start is not at a line start, skip forward to POS to
3060 get the correct continuation lines width. */
3061 start_at_line_beg_p = (CHARPOS (pos) == BEGV
3062 || FETCH_BYTE (BYTEPOS (pos) - 1) == '\n');
3063 if (!start_at_line_beg_p)
3065 int new_x;
3067 reseat_at_previous_visible_line_start (it);
3068 move_it_to (it, CHARPOS (pos), -1, -1, -1, MOVE_TO_POS);
3070 new_x = it->current_x + it->pixel_width;
3072 /* If lines are continued, this line may end in the middle
3073 of a multi-glyph character (e.g. a control character
3074 displayed as \003, or in the middle of an overlay
3075 string). In this case move_it_to above will not have
3076 taken us to the start of the continuation line but to the
3077 end of the continued line. */
3078 if (it->current_x > 0
3079 && it->line_wrap != TRUNCATE /* Lines are continued. */
3080 && (/* And glyph doesn't fit on the line. */
3081 new_x > it->last_visible_x
3082 /* Or it fits exactly and we're on a window
3083 system frame. */
3084 || (new_x == it->last_visible_x
3085 && FRAME_WINDOW_P (it->f)
3086 && ((it->bidi_p && it->bidi_it.paragraph_dir == R2L)
3087 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
3088 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)))))
3090 if ((it->current.dpvec_index >= 0
3091 || it->current.overlay_string_index >= 0)
3092 /* If we are on a newline from a display vector or
3093 overlay string, then we are already at the end of
3094 a screen line; no need to go to the next line in
3095 that case, as this line is not really continued.
3096 (If we do go to the next line, C-e will not DTRT.) */
3097 && it->c != '\n')
3099 set_iterator_to_next (it, 1);
3100 move_it_in_display_line_to (it, -1, -1, 0);
3103 it->continuation_lines_width += it->current_x;
3105 /* If the character at POS is displayed via a display
3106 vector, move_it_to above stops at the final glyph of
3107 IT->dpvec. To make the caller redisplay that character
3108 again (a.k.a. start at POS), we need to reset the
3109 dpvec_index to the beginning of IT->dpvec. */
3110 else if (it->current.dpvec_index >= 0)
3111 it->current.dpvec_index = 0;
3113 /* We're starting a new display line, not affected by the
3114 height of the continued line, so clear the appropriate
3115 fields in the iterator structure. */
3116 it->max_ascent = it->max_descent = 0;
3117 it->max_phys_ascent = it->max_phys_descent = 0;
3119 it->current_y = first_y;
3120 it->vpos = 0;
3121 it->current_x = it->hpos = 0;
3127 /* Return 1 if POS is a position in ellipses displayed for invisible
3128 text. W is the window we display, for text property lookup. */
3130 static int
3131 in_ellipses_for_invisible_text_p (struct display_pos *pos, struct window *w)
3133 Lisp_Object prop, window;
3134 int ellipses_p = 0;
3135 ptrdiff_t charpos = CHARPOS (pos->pos);
3137 /* If POS specifies a position in a display vector, this might
3138 be for an ellipsis displayed for invisible text. We won't
3139 get the iterator set up for delivering that ellipsis unless
3140 we make sure that it gets aware of the invisible text. */
3141 if (pos->dpvec_index >= 0
3142 && pos->overlay_string_index < 0
3143 && CHARPOS (pos->string_pos) < 0
3144 && charpos > BEGV
3145 && (XSETWINDOW (window, w),
3146 prop = Fget_char_property (make_number (charpos),
3147 Qinvisible, window),
3148 !TEXT_PROP_MEANS_INVISIBLE (prop)))
3150 prop = Fget_char_property (make_number (charpos - 1), Qinvisible,
3151 window);
3152 ellipses_p = 2 == TEXT_PROP_MEANS_INVISIBLE (prop);
3155 return ellipses_p;
3159 /* Initialize IT for stepping through current_buffer in window W,
3160 starting at position POS that includes overlay string and display
3161 vector/ control character translation position information. Value
3162 is zero if there are overlay strings with newlines at POS. */
3164 static int
3165 init_from_display_pos (struct it *it, struct window *w, struct display_pos *pos)
3167 ptrdiff_t charpos = CHARPOS (pos->pos), bytepos = BYTEPOS (pos->pos);
3168 int i, overlay_strings_with_newlines = 0;
3170 /* If POS specifies a position in a display vector, this might
3171 be for an ellipsis displayed for invisible text. We won't
3172 get the iterator set up for delivering that ellipsis unless
3173 we make sure that it gets aware of the invisible text. */
3174 if (in_ellipses_for_invisible_text_p (pos, w))
3176 --charpos;
3177 bytepos = 0;
3180 /* Keep in mind: the call to reseat in init_iterator skips invisible
3181 text, so we might end up at a position different from POS. This
3182 is only a problem when POS is a row start after a newline and an
3183 overlay starts there with an after-string, and the overlay has an
3184 invisible property. Since we don't skip invisible text in
3185 display_line and elsewhere immediately after consuming the
3186 newline before the row start, such a POS will not be in a string,
3187 but the call to init_iterator below will move us to the
3188 after-string. */
3189 init_iterator (it, w, charpos, bytepos, NULL, DEFAULT_FACE_ID);
3191 /* This only scans the current chunk -- it should scan all chunks.
3192 However, OVERLAY_STRING_CHUNK_SIZE has been increased from 3 in 21.1
3193 to 16 in 22.1 to make this a lesser problem. */
3194 for (i = 0; i < it->n_overlay_strings && i < OVERLAY_STRING_CHUNK_SIZE; ++i)
3196 const char *s = SSDATA (it->overlay_strings[i]);
3197 const char *e = s + SBYTES (it->overlay_strings[i]);
3199 while (s < e && *s != '\n')
3200 ++s;
3202 if (s < e)
3204 overlay_strings_with_newlines = 1;
3205 break;
3209 /* If position is within an overlay string, set up IT to the right
3210 overlay string. */
3211 if (pos->overlay_string_index >= 0)
3213 int relative_index;
3215 /* If the first overlay string happens to have a `display'
3216 property for an image, the iterator will be set up for that
3217 image, and we have to undo that setup first before we can
3218 correct the overlay string index. */
3219 if (it->method == GET_FROM_IMAGE)
3220 pop_it (it);
3222 /* We already have the first chunk of overlay strings in
3223 IT->overlay_strings. Load more until the one for
3224 pos->overlay_string_index is in IT->overlay_strings. */
3225 if (pos->overlay_string_index >= OVERLAY_STRING_CHUNK_SIZE)
3227 ptrdiff_t n = pos->overlay_string_index / OVERLAY_STRING_CHUNK_SIZE;
3228 it->current.overlay_string_index = 0;
3229 while (n--)
3231 load_overlay_strings (it, 0);
3232 it->current.overlay_string_index += OVERLAY_STRING_CHUNK_SIZE;
3236 it->current.overlay_string_index = pos->overlay_string_index;
3237 relative_index = (it->current.overlay_string_index
3238 % OVERLAY_STRING_CHUNK_SIZE);
3239 it->string = it->overlay_strings[relative_index];
3240 eassert (STRINGP (it->string));
3241 it->current.string_pos = pos->string_pos;
3242 it->method = GET_FROM_STRING;
3243 it->end_charpos = SCHARS (it->string);
3244 /* Set up the bidi iterator for this overlay string. */
3245 if (it->bidi_p)
3247 it->bidi_it.string.lstring = it->string;
3248 it->bidi_it.string.s = NULL;
3249 it->bidi_it.string.schars = SCHARS (it->string);
3250 it->bidi_it.string.bufpos = it->overlay_strings_charpos;
3251 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
3252 it->bidi_it.string.unibyte = !it->multibyte_p;
3253 it->bidi_it.w = it->w;
3254 bidi_init_it (IT_STRING_CHARPOS (*it), IT_STRING_BYTEPOS (*it),
3255 FRAME_WINDOW_P (it->f), &it->bidi_it);
3257 /* Synchronize the state of the bidi iterator with
3258 pos->string_pos. For any string position other than
3259 zero, this will be done automagically when we resume
3260 iteration over the string and get_visually_first_element
3261 is called. But if string_pos is zero, and the string is
3262 to be reordered for display, we need to resync manually,
3263 since it could be that the iteration state recorded in
3264 pos ended at string_pos of 0 moving backwards in string. */
3265 if (CHARPOS (pos->string_pos) == 0)
3267 get_visually_first_element (it);
3268 if (IT_STRING_CHARPOS (*it) != 0)
3269 do {
3270 /* Paranoia. */
3271 eassert (it->bidi_it.charpos < it->bidi_it.string.schars);
3272 bidi_move_to_visually_next (&it->bidi_it);
3273 } while (it->bidi_it.charpos != 0);
3275 eassert (IT_STRING_CHARPOS (*it) == it->bidi_it.charpos
3276 && IT_STRING_BYTEPOS (*it) == it->bidi_it.bytepos);
3280 if (CHARPOS (pos->string_pos) >= 0)
3282 /* Recorded position is not in an overlay string, but in another
3283 string. This can only be a string from a `display' property.
3284 IT should already be filled with that string. */
3285 it->current.string_pos = pos->string_pos;
3286 eassert (STRINGP (it->string));
3287 if (it->bidi_p)
3288 bidi_init_it (IT_STRING_CHARPOS (*it), IT_STRING_BYTEPOS (*it),
3289 FRAME_WINDOW_P (it->f), &it->bidi_it);
3292 /* Restore position in display vector translations, control
3293 character translations or ellipses. */
3294 if (pos->dpvec_index >= 0)
3296 if (it->dpvec == NULL)
3297 get_next_display_element (it);
3298 eassert (it->dpvec && it->current.dpvec_index == 0);
3299 it->current.dpvec_index = pos->dpvec_index;
3302 CHECK_IT (it);
3303 return !overlay_strings_with_newlines;
3307 /* Initialize IT for stepping through current_buffer in window W
3308 starting at ROW->start. */
3310 static void
3311 init_to_row_start (struct it *it, struct window *w, struct glyph_row *row)
3313 init_from_display_pos (it, w, &row->start);
3314 it->start = row->start;
3315 it->continuation_lines_width = row->continuation_lines_width;
3316 CHECK_IT (it);
3320 /* Initialize IT for stepping through current_buffer in window W
3321 starting in the line following ROW, i.e. starting at ROW->end.
3322 Value is zero if there are overlay strings with newlines at ROW's
3323 end position. */
3325 static int
3326 init_to_row_end (struct it *it, struct window *w, struct glyph_row *row)
3328 int success = 0;
3330 if (init_from_display_pos (it, w, &row->end))
3332 if (row->continued_p)
3333 it->continuation_lines_width
3334 = row->continuation_lines_width + row->pixel_width;
3335 CHECK_IT (it);
3336 success = 1;
3339 return success;
3345 /***********************************************************************
3346 Text properties
3347 ***********************************************************************/
3349 /* Called when IT reaches IT->stop_charpos. Handle text property and
3350 overlay changes. Set IT->stop_charpos to the next position where
3351 to stop. */
3353 static void
3354 handle_stop (struct it *it)
3356 enum prop_handled handled;
3357 int handle_overlay_change_p;
3358 struct props *p;
3360 it->dpvec = NULL;
3361 it->current.dpvec_index = -1;
3362 handle_overlay_change_p = !it->ignore_overlay_strings_at_pos_p;
3363 it->ignore_overlay_strings_at_pos_p = 0;
3364 it->ellipsis_p = 0;
3366 /* Use face of preceding text for ellipsis (if invisible) */
3367 if (it->selective_display_ellipsis_p)
3368 it->saved_face_id = it->face_id;
3372 handled = HANDLED_NORMALLY;
3374 /* Call text property handlers. */
3375 for (p = it_props; p->handler; ++p)
3377 handled = p->handler (it);
3379 if (handled == HANDLED_RECOMPUTE_PROPS)
3380 break;
3381 else if (handled == HANDLED_RETURN)
3383 /* We still want to show before and after strings from
3384 overlays even if the actual buffer text is replaced. */
3385 if (!handle_overlay_change_p
3386 || it->sp > 1
3387 /* Don't call get_overlay_strings_1 if we already
3388 have overlay strings loaded, because doing so
3389 will load them again and push the iterator state
3390 onto the stack one more time, which is not
3391 expected by the rest of the code that processes
3392 overlay strings. */
3393 || (it->current.overlay_string_index < 0
3394 ? !get_overlay_strings_1 (it, 0, 0)
3395 : 0))
3397 if (it->ellipsis_p)
3398 setup_for_ellipsis (it, 0);
3399 /* When handling a display spec, we might load an
3400 empty string. In that case, discard it here. We
3401 used to discard it in handle_single_display_spec,
3402 but that causes get_overlay_strings_1, above, to
3403 ignore overlay strings that we must check. */
3404 if (STRINGP (it->string) && !SCHARS (it->string))
3405 pop_it (it);
3406 return;
3408 else if (STRINGP (it->string) && !SCHARS (it->string))
3409 pop_it (it);
3410 else
3412 it->ignore_overlay_strings_at_pos_p = true;
3413 it->string_from_display_prop_p = 0;
3414 it->from_disp_prop_p = 0;
3415 handle_overlay_change_p = 0;
3417 handled = HANDLED_RECOMPUTE_PROPS;
3418 break;
3420 else if (handled == HANDLED_OVERLAY_STRING_CONSUMED)
3421 handle_overlay_change_p = 0;
3424 if (handled != HANDLED_RECOMPUTE_PROPS)
3426 /* Don't check for overlay strings below when set to deliver
3427 characters from a display vector. */
3428 if (it->method == GET_FROM_DISPLAY_VECTOR)
3429 handle_overlay_change_p = 0;
3431 /* Handle overlay changes.
3432 This sets HANDLED to HANDLED_RECOMPUTE_PROPS
3433 if it finds overlays. */
3434 if (handle_overlay_change_p)
3435 handled = handle_overlay_change (it);
3438 if (it->ellipsis_p)
3440 setup_for_ellipsis (it, 0);
3441 break;
3444 while (handled == HANDLED_RECOMPUTE_PROPS);
3446 /* Determine where to stop next. */
3447 if (handled == HANDLED_NORMALLY)
3448 compute_stop_pos (it);
3452 /* Compute IT->stop_charpos from text property and overlay change
3453 information for IT's current position. */
3455 static void
3456 compute_stop_pos (struct it *it)
3458 register INTERVAL iv, next_iv;
3459 Lisp_Object object, limit, position;
3460 ptrdiff_t charpos, bytepos;
3462 if (STRINGP (it->string))
3464 /* Strings are usually short, so don't limit the search for
3465 properties. */
3466 it->stop_charpos = it->end_charpos;
3467 object = it->string;
3468 limit = Qnil;
3469 charpos = IT_STRING_CHARPOS (*it);
3470 bytepos = IT_STRING_BYTEPOS (*it);
3472 else
3474 ptrdiff_t pos;
3476 /* If end_charpos is out of range for some reason, such as a
3477 misbehaving display function, rationalize it (Bug#5984). */
3478 if (it->end_charpos > ZV)
3479 it->end_charpos = ZV;
3480 it->stop_charpos = it->end_charpos;
3482 /* If next overlay change is in front of the current stop pos
3483 (which is IT->end_charpos), stop there. Note: value of
3484 next_overlay_change is point-max if no overlay change
3485 follows. */
3486 charpos = IT_CHARPOS (*it);
3487 bytepos = IT_BYTEPOS (*it);
3488 pos = next_overlay_change (charpos);
3489 if (pos < it->stop_charpos)
3490 it->stop_charpos = pos;
3492 /* Set up variables for computing the stop position from text
3493 property changes. */
3494 XSETBUFFER (object, current_buffer);
3495 limit = make_number (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT);
3498 /* Get the interval containing IT's position. Value is a null
3499 interval if there isn't such an interval. */
3500 position = make_number (charpos);
3501 iv = validate_interval_range (object, &position, &position, 0);
3502 if (iv)
3504 Lisp_Object values_here[LAST_PROP_IDX];
3505 struct props *p;
3507 /* Get properties here. */
3508 for (p = it_props; p->handler; ++p)
3509 values_here[p->idx] = textget (iv->plist, *p->name);
3511 /* Look for an interval following iv that has different
3512 properties. */
3513 for (next_iv = next_interval (iv);
3514 (next_iv
3515 && (NILP (limit)
3516 || XFASTINT (limit) > next_iv->position));
3517 next_iv = next_interval (next_iv))
3519 for (p = it_props; p->handler; ++p)
3521 Lisp_Object new_value;
3523 new_value = textget (next_iv->plist, *p->name);
3524 if (!EQ (values_here[p->idx], new_value))
3525 break;
3528 if (p->handler)
3529 break;
3532 if (next_iv)
3534 if (INTEGERP (limit)
3535 && next_iv->position >= XFASTINT (limit))
3536 /* No text property change up to limit. */
3537 it->stop_charpos = min (XFASTINT (limit), it->stop_charpos);
3538 else
3539 /* Text properties change in next_iv. */
3540 it->stop_charpos = min (it->stop_charpos, next_iv->position);
3544 if (it->cmp_it.id < 0)
3546 ptrdiff_t stoppos = it->end_charpos;
3548 if (it->bidi_p && it->bidi_it.scan_dir < 0)
3549 stoppos = -1;
3550 composition_compute_stop_pos (&it->cmp_it, charpos, bytepos,
3551 stoppos, it->string);
3554 eassert (STRINGP (it->string)
3555 || (it->stop_charpos >= BEGV
3556 && it->stop_charpos >= IT_CHARPOS (*it)));
3560 /* Return the position of the next overlay change after POS in
3561 current_buffer. Value is point-max if no overlay change
3562 follows. This is like `next-overlay-change' but doesn't use
3563 xmalloc. */
3565 static ptrdiff_t
3566 next_overlay_change (ptrdiff_t pos)
3568 ptrdiff_t i, noverlays;
3569 ptrdiff_t endpos;
3570 Lisp_Object *overlays;
3572 /* Get all overlays at the given position. */
3573 GET_OVERLAYS_AT (pos, overlays, noverlays, &endpos, 1);
3575 /* If any of these overlays ends before endpos,
3576 use its ending point instead. */
3577 for (i = 0; i < noverlays; ++i)
3579 Lisp_Object oend;
3580 ptrdiff_t oendpos;
3582 oend = OVERLAY_END (overlays[i]);
3583 oendpos = OVERLAY_POSITION (oend);
3584 endpos = min (endpos, oendpos);
3587 return endpos;
3590 /* How many characters forward to search for a display property or
3591 display string. Searching too far forward makes the bidi display
3592 sluggish, especially in small windows. */
3593 #define MAX_DISP_SCAN 250
3595 /* Return the character position of a display string at or after
3596 position specified by POSITION. If no display string exists at or
3597 after POSITION, return ZV. A display string is either an overlay
3598 with `display' property whose value is a string, or a `display'
3599 text property whose value is a string. STRING is data about the
3600 string to iterate; if STRING->lstring is nil, we are iterating a
3601 buffer. FRAME_WINDOW_P is non-zero when we are displaying a window
3602 on a GUI frame. DISP_PROP is set to zero if we searched
3603 MAX_DISP_SCAN characters forward without finding any display
3604 strings, non-zero otherwise. It is set to 2 if the display string
3605 uses any kind of `(space ...)' spec that will produce a stretch of
3606 white space in the text area. */
3607 ptrdiff_t
3608 compute_display_string_pos (struct text_pos *position,
3609 struct bidi_string_data *string,
3610 struct window *w,
3611 int frame_window_p, int *disp_prop)
3613 /* OBJECT = nil means current buffer. */
3614 Lisp_Object object, object1;
3615 Lisp_Object pos, spec, limpos;
3616 int string_p = (string && (STRINGP (string->lstring) || string->s));
3617 ptrdiff_t eob = string_p ? string->schars : ZV;
3618 ptrdiff_t begb = string_p ? 0 : BEGV;
3619 ptrdiff_t bufpos, charpos = CHARPOS (*position);
3620 ptrdiff_t lim =
3621 (charpos < eob - MAX_DISP_SCAN) ? charpos + MAX_DISP_SCAN : eob;
3622 struct text_pos tpos;
3623 int rv = 0;
3625 if (string && STRINGP (string->lstring))
3626 object1 = object = string->lstring;
3627 else if (w && !string_p)
3629 XSETWINDOW (object, w);
3630 object1 = Qnil;
3632 else
3633 object1 = object = Qnil;
3635 *disp_prop = 1;
3637 if (charpos >= eob
3638 /* We don't support display properties whose values are strings
3639 that have display string properties. */
3640 || string->from_disp_str
3641 /* C strings cannot have display properties. */
3642 || (string->s && !STRINGP (object)))
3644 *disp_prop = 0;
3645 return eob;
3648 /* If the character at CHARPOS is where the display string begins,
3649 return CHARPOS. */
3650 pos = make_number (charpos);
3651 if (STRINGP (object))
3652 bufpos = string->bufpos;
3653 else
3654 bufpos = charpos;
3655 tpos = *position;
3656 if (!NILP (spec = Fget_char_property (pos, Qdisplay, object))
3657 && (charpos <= begb
3658 || !EQ (Fget_char_property (make_number (charpos - 1), Qdisplay,
3659 object),
3660 spec))
3661 && (rv = handle_display_spec (NULL, spec, object, Qnil, &tpos, bufpos,
3662 frame_window_p)))
3664 if (rv == 2)
3665 *disp_prop = 2;
3666 return charpos;
3669 /* Look forward for the first character with a `display' property
3670 that will replace the underlying text when displayed. */
3671 limpos = make_number (lim);
3672 do {
3673 pos = Fnext_single_char_property_change (pos, Qdisplay, object1, limpos);
3674 CHARPOS (tpos) = XFASTINT (pos);
3675 if (CHARPOS (tpos) >= lim)
3677 *disp_prop = 0;
3678 break;
3680 if (STRINGP (object))
3681 BYTEPOS (tpos) = string_char_to_byte (object, CHARPOS (tpos));
3682 else
3683 BYTEPOS (tpos) = CHAR_TO_BYTE (CHARPOS (tpos));
3684 spec = Fget_char_property (pos, Qdisplay, object);
3685 if (!STRINGP (object))
3686 bufpos = CHARPOS (tpos);
3687 } while (NILP (spec)
3688 || !(rv = handle_display_spec (NULL, spec, object, Qnil, &tpos,
3689 bufpos, frame_window_p)));
3690 if (rv == 2)
3691 *disp_prop = 2;
3693 return CHARPOS (tpos);
3696 /* Return the character position of the end of the display string that
3697 started at CHARPOS. If there's no display string at CHARPOS,
3698 return -1. A display string is either an overlay with `display'
3699 property whose value is a string or a `display' text property whose
3700 value is a string. */
3701 ptrdiff_t
3702 compute_display_string_end (ptrdiff_t charpos, struct bidi_string_data *string)
3704 /* OBJECT = nil means current buffer. */
3705 Lisp_Object object =
3706 (string && STRINGP (string->lstring)) ? string->lstring : Qnil;
3707 Lisp_Object pos = make_number (charpos);
3708 ptrdiff_t eob =
3709 (STRINGP (object) || (string && string->s)) ? string->schars : ZV;
3711 if (charpos >= eob || (string->s && !STRINGP (object)))
3712 return eob;
3714 /* It could happen that the display property or overlay was removed
3715 since we found it in compute_display_string_pos above. One way
3716 this can happen is if JIT font-lock was called (through
3717 handle_fontified_prop), and jit-lock-functions remove text
3718 properties or overlays from the portion of buffer that includes
3719 CHARPOS. Muse mode is known to do that, for example. In this
3720 case, we return -1 to the caller, to signal that no display
3721 string is actually present at CHARPOS. See bidi_fetch_char for
3722 how this is handled.
3724 An alternative would be to never look for display properties past
3725 it->stop_charpos. But neither compute_display_string_pos nor
3726 bidi_fetch_char that calls it know or care where the next
3727 stop_charpos is. */
3728 if (NILP (Fget_char_property (pos, Qdisplay, object)))
3729 return -1;
3731 /* Look forward for the first character where the `display' property
3732 changes. */
3733 pos = Fnext_single_char_property_change (pos, Qdisplay, object, Qnil);
3735 return XFASTINT (pos);
3740 /***********************************************************************
3741 Fontification
3742 ***********************************************************************/
3744 /* Handle changes in the `fontified' property of the current buffer by
3745 calling hook functions from Qfontification_functions to fontify
3746 regions of text. */
3748 static enum prop_handled
3749 handle_fontified_prop (struct it *it)
3751 Lisp_Object prop, pos;
3752 enum prop_handled handled = HANDLED_NORMALLY;
3754 if (!NILP (Vmemory_full))
3755 return handled;
3757 /* Get the value of the `fontified' property at IT's current buffer
3758 position. (The `fontified' property doesn't have a special
3759 meaning in strings.) If the value is nil, call functions from
3760 Qfontification_functions. */
3761 if (!STRINGP (it->string)
3762 && it->s == NULL
3763 && !NILP (Vfontification_functions)
3764 && !NILP (Vrun_hooks)
3765 && (pos = make_number (IT_CHARPOS (*it)),
3766 prop = Fget_char_property (pos, Qfontified, Qnil),
3767 /* Ignore the special cased nil value always present at EOB since
3768 no amount of fontifying will be able to change it. */
3769 NILP (prop) && IT_CHARPOS (*it) < Z))
3771 ptrdiff_t count = SPECPDL_INDEX ();
3772 Lisp_Object val;
3773 struct buffer *obuf = current_buffer;
3774 ptrdiff_t begv = BEGV, zv = ZV;
3775 bool old_clip_changed = current_buffer->clip_changed;
3777 val = Vfontification_functions;
3778 specbind (Qfontification_functions, Qnil);
3780 eassert (it->end_charpos == ZV);
3782 if (!CONSP (val) || EQ (XCAR (val), Qlambda))
3783 safe_call1 (val, pos);
3784 else
3786 Lisp_Object fns, fn;
3787 struct gcpro gcpro1, gcpro2;
3789 fns = Qnil;
3790 GCPRO2 (val, fns);
3792 for (; CONSP (val); val = XCDR (val))
3794 fn = XCAR (val);
3796 if (EQ (fn, Qt))
3798 /* A value of t indicates this hook has a local
3799 binding; it means to run the global binding too.
3800 In a global value, t should not occur. If it
3801 does, we must ignore it to avoid an endless
3802 loop. */
3803 for (fns = Fdefault_value (Qfontification_functions);
3804 CONSP (fns);
3805 fns = XCDR (fns))
3807 fn = XCAR (fns);
3808 if (!EQ (fn, Qt))
3809 safe_call1 (fn, pos);
3812 else
3813 safe_call1 (fn, pos);
3816 UNGCPRO;
3819 unbind_to (count, Qnil);
3821 /* Fontification functions routinely call `save-restriction'.
3822 Normally, this tags clip_changed, which can confuse redisplay
3823 (see discussion in Bug#6671). Since we don't perform any
3824 special handling of fontification changes in the case where
3825 `save-restriction' isn't called, there's no point doing so in
3826 this case either. So, if the buffer's restrictions are
3827 actually left unchanged, reset clip_changed. */
3828 if (obuf == current_buffer)
3830 if (begv == BEGV && zv == ZV)
3831 current_buffer->clip_changed = old_clip_changed;
3833 /* There isn't much we can reasonably do to protect against
3834 misbehaving fontification, but here's a fig leaf. */
3835 else if (BUFFER_LIVE_P (obuf))
3836 set_buffer_internal_1 (obuf);
3838 /* The fontification code may have added/removed text.
3839 It could do even a lot worse, but let's at least protect against
3840 the most obvious case where only the text past `pos' gets changed',
3841 as is/was done in grep.el where some escapes sequences are turned
3842 into face properties (bug#7876). */
3843 it->end_charpos = ZV;
3845 /* Return HANDLED_RECOMPUTE_PROPS only if function fontified
3846 something. This avoids an endless loop if they failed to
3847 fontify the text for which reason ever. */
3848 if (!NILP (Fget_char_property (pos, Qfontified, Qnil)))
3849 handled = HANDLED_RECOMPUTE_PROPS;
3852 return handled;
3857 /***********************************************************************
3858 Faces
3859 ***********************************************************************/
3861 /* Set up iterator IT from face properties at its current position.
3862 Called from handle_stop. */
3864 static enum prop_handled
3865 handle_face_prop (struct it *it)
3867 int new_face_id;
3868 ptrdiff_t next_stop;
3870 if (!STRINGP (it->string))
3872 new_face_id
3873 = face_at_buffer_position (it->w,
3874 IT_CHARPOS (*it),
3875 &next_stop,
3876 (IT_CHARPOS (*it)
3877 + TEXT_PROP_DISTANCE_LIMIT),
3878 0, it->base_face_id);
3880 /* Is this a start of a run of characters with box face?
3881 Caveat: this can be called for a freshly initialized
3882 iterator; face_id is -1 in this case. We know that the new
3883 face will not change until limit, i.e. if the new face has a
3884 box, all characters up to limit will have one. But, as
3885 usual, we don't know whether limit is really the end. */
3886 if (new_face_id != it->face_id)
3888 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3889 /* If it->face_id is -1, old_face below will be NULL, see
3890 the definition of FACE_FROM_ID. This will happen if this
3891 is the initial call that gets the face. */
3892 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
3894 /* If the value of face_id of the iterator is -1, we have to
3895 look in front of IT's position and see whether there is a
3896 face there that's different from new_face_id. */
3897 if (!old_face && IT_CHARPOS (*it) > BEG)
3899 int prev_face_id = face_before_it_pos (it);
3901 old_face = FACE_FROM_ID (it->f, prev_face_id);
3904 /* If the new face has a box, but the old face does not,
3905 this is the start of a run of characters with box face,
3906 i.e. this character has a shadow on the left side. */
3907 it->start_of_box_run_p = (new_face->box != FACE_NO_BOX
3908 && (old_face == NULL || !old_face->box));
3909 it->face_box_p = new_face->box != FACE_NO_BOX;
3912 else
3914 int base_face_id;
3915 ptrdiff_t bufpos;
3916 int i;
3917 Lisp_Object from_overlay
3918 = (it->current.overlay_string_index >= 0
3919 ? it->string_overlays[it->current.overlay_string_index
3920 % OVERLAY_STRING_CHUNK_SIZE]
3921 : Qnil);
3923 /* See if we got to this string directly or indirectly from
3924 an overlay property. That includes the before-string or
3925 after-string of an overlay, strings in display properties
3926 provided by an overlay, their text properties, etc.
3928 FROM_OVERLAY is the overlay that brought us here, or nil if none. */
3929 if (! NILP (from_overlay))
3930 for (i = it->sp - 1; i >= 0; i--)
3932 if (it->stack[i].current.overlay_string_index >= 0)
3933 from_overlay
3934 = it->string_overlays[it->stack[i].current.overlay_string_index
3935 % OVERLAY_STRING_CHUNK_SIZE];
3936 else if (! NILP (it->stack[i].from_overlay))
3937 from_overlay = it->stack[i].from_overlay;
3939 if (!NILP (from_overlay))
3940 break;
3943 if (! NILP (from_overlay))
3945 bufpos = IT_CHARPOS (*it);
3946 /* For a string from an overlay, the base face depends
3947 only on text properties and ignores overlays. */
3948 base_face_id
3949 = face_for_overlay_string (it->w,
3950 IT_CHARPOS (*it),
3951 &next_stop,
3952 (IT_CHARPOS (*it)
3953 + TEXT_PROP_DISTANCE_LIMIT),
3955 from_overlay);
3957 else
3959 bufpos = 0;
3961 /* For strings from a `display' property, use the face at
3962 IT's current buffer position as the base face to merge
3963 with, so that overlay strings appear in the same face as
3964 surrounding text, unless they specify their own faces.
3965 For strings from wrap-prefix and line-prefix properties,
3966 use the default face, possibly remapped via
3967 Vface_remapping_alist. */
3968 /* Note that the fact that we use the face at _buffer_
3969 position means that a 'display' property on an overlay
3970 string will not inherit the face of that overlay string,
3971 but will instead revert to the face of buffer text
3972 covered by the overlay. This is visible, e.g., when the
3973 overlay specifies a box face, but neither the buffer nor
3974 the display string do. This sounds like a design bug,
3975 but Emacs always did that since v21.1, so changing that
3976 might be a big deal. */
3977 base_face_id = it->string_from_prefix_prop_p
3978 ? (!NILP (Vface_remapping_alist)
3979 ? lookup_basic_face (it->f, DEFAULT_FACE_ID)
3980 : DEFAULT_FACE_ID)
3981 : underlying_face_id (it);
3984 new_face_id = face_at_string_position (it->w,
3985 it->string,
3986 IT_STRING_CHARPOS (*it),
3987 bufpos,
3988 &next_stop,
3989 base_face_id, 0);
3991 /* Is this a start of a run of characters with box? Caveat:
3992 this can be called for a freshly allocated iterator; face_id
3993 is -1 is this case. We know that the new face will not
3994 change until the next check pos, i.e. if the new face has a
3995 box, all characters up to that position will have a
3996 box. But, as usual, we don't know whether that position
3997 is really the end. */
3998 if (new_face_id != it->face_id)
4000 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
4001 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
4003 /* If new face has a box but old face hasn't, this is the
4004 start of a run of characters with box, i.e. it has a
4005 shadow on the left side. */
4006 it->start_of_box_run_p
4007 = new_face->box && (old_face == NULL || !old_face->box);
4008 it->face_box_p = new_face->box != FACE_NO_BOX;
4012 it->face_id = new_face_id;
4013 return HANDLED_NORMALLY;
4017 /* Return the ID of the face ``underlying'' IT's current position,
4018 which is in a string. If the iterator is associated with a
4019 buffer, return the face at IT's current buffer position.
4020 Otherwise, use the iterator's base_face_id. */
4022 static int
4023 underlying_face_id (struct it *it)
4025 int face_id = it->base_face_id, i;
4027 eassert (STRINGP (it->string));
4029 for (i = it->sp - 1; i >= 0; --i)
4030 if (NILP (it->stack[i].string))
4031 face_id = it->stack[i].face_id;
4033 return face_id;
4037 /* Compute the face one character before or after the current position
4038 of IT, in the visual order. BEFORE_P non-zero means get the face
4039 in front (to the left in L2R paragraphs, to the right in R2L
4040 paragraphs) of IT's screen position. Value is the ID of the face. */
4042 static int
4043 face_before_or_after_it_pos (struct it *it, int before_p)
4045 int face_id, limit;
4046 ptrdiff_t next_check_charpos;
4047 struct it it_copy;
4048 void *it_copy_data = NULL;
4050 eassert (it->s == NULL);
4052 if (STRINGP (it->string))
4054 ptrdiff_t bufpos, charpos;
4055 int base_face_id;
4057 /* No face change past the end of the string (for the case
4058 we are padding with spaces). No face change before the
4059 string start. */
4060 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string)
4061 || (IT_STRING_CHARPOS (*it) == 0 && before_p))
4062 return it->face_id;
4064 if (!it->bidi_p)
4066 /* Set charpos to the position before or after IT's current
4067 position, in the logical order, which in the non-bidi
4068 case is the same as the visual order. */
4069 if (before_p)
4070 charpos = IT_STRING_CHARPOS (*it) - 1;
4071 else if (it->what == IT_COMPOSITION)
4072 /* For composition, we must check the character after the
4073 composition. */
4074 charpos = IT_STRING_CHARPOS (*it) + it->cmp_it.nchars;
4075 else
4076 charpos = IT_STRING_CHARPOS (*it) + 1;
4078 else
4080 if (before_p)
4082 /* With bidi iteration, the character before the current
4083 in the visual order cannot be found by simple
4084 iteration, because "reverse" reordering is not
4085 supported. Instead, we need to use the move_it_*
4086 family of functions. */
4087 /* Ignore face changes before the first visible
4088 character on this display line. */
4089 if (it->current_x <= it->first_visible_x)
4090 return it->face_id;
4091 SAVE_IT (it_copy, *it, it_copy_data);
4092 /* Implementation note: Since move_it_in_display_line
4093 works in the iterator geometry, and thinks the first
4094 character is always the leftmost, even in R2L lines,
4095 we don't need to distinguish between the R2L and L2R
4096 cases here. */
4097 move_it_in_display_line (&it_copy, SCHARS (it_copy.string),
4098 it_copy.current_x - 1, MOVE_TO_X);
4099 charpos = IT_STRING_CHARPOS (it_copy);
4100 RESTORE_IT (it, it, it_copy_data);
4102 else
4104 /* Set charpos to the string position of the character
4105 that comes after IT's current position in the visual
4106 order. */
4107 int n = (it->what == IT_COMPOSITION ? it->cmp_it.nchars : 1);
4109 it_copy = *it;
4110 while (n--)
4111 bidi_move_to_visually_next (&it_copy.bidi_it);
4113 charpos = it_copy.bidi_it.charpos;
4116 eassert (0 <= charpos && charpos <= SCHARS (it->string));
4118 if (it->current.overlay_string_index >= 0)
4119 bufpos = IT_CHARPOS (*it);
4120 else
4121 bufpos = 0;
4123 base_face_id = underlying_face_id (it);
4125 /* Get the face for ASCII, or unibyte. */
4126 face_id = face_at_string_position (it->w,
4127 it->string,
4128 charpos,
4129 bufpos,
4130 &next_check_charpos,
4131 base_face_id, 0);
4133 /* Correct the face for charsets different from ASCII. Do it
4134 for the multibyte case only. The face returned above is
4135 suitable for unibyte text if IT->string is unibyte. */
4136 if (STRING_MULTIBYTE (it->string))
4138 struct text_pos pos1 = string_pos (charpos, it->string);
4139 const unsigned char *p = SDATA (it->string) + BYTEPOS (pos1);
4140 int c, len;
4141 struct face *face = FACE_FROM_ID (it->f, face_id);
4143 c = string_char_and_length (p, &len);
4144 face_id = FACE_FOR_CHAR (it->f, face, c, charpos, it->string);
4147 else
4149 struct text_pos pos;
4151 if ((IT_CHARPOS (*it) >= ZV && !before_p)
4152 || (IT_CHARPOS (*it) <= BEGV && before_p))
4153 return it->face_id;
4155 limit = IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT;
4156 pos = it->current.pos;
4158 if (!it->bidi_p)
4160 if (before_p)
4161 DEC_TEXT_POS (pos, it->multibyte_p);
4162 else
4164 if (it->what == IT_COMPOSITION)
4166 /* For composition, we must check the position after
4167 the composition. */
4168 pos.charpos += it->cmp_it.nchars;
4169 pos.bytepos += it->len;
4171 else
4172 INC_TEXT_POS (pos, it->multibyte_p);
4175 else
4177 if (before_p)
4179 /* With bidi iteration, the character before the current
4180 in the visual order cannot be found by simple
4181 iteration, because "reverse" reordering is not
4182 supported. Instead, we need to use the move_it_*
4183 family of functions. */
4184 /* Ignore face changes before the first visible
4185 character on this display line. */
4186 if (it->current_x <= it->first_visible_x)
4187 return it->face_id;
4188 SAVE_IT (it_copy, *it, it_copy_data);
4189 /* Implementation note: Since move_it_in_display_line
4190 works in the iterator geometry, and thinks the first
4191 character is always the leftmost, even in R2L lines,
4192 we don't need to distinguish between the R2L and L2R
4193 cases here. */
4194 move_it_in_display_line (&it_copy, ZV,
4195 it_copy.current_x - 1, MOVE_TO_X);
4196 pos = it_copy.current.pos;
4197 RESTORE_IT (it, it, it_copy_data);
4199 else
4201 /* Set charpos to the buffer position of the character
4202 that comes after IT's current position in the visual
4203 order. */
4204 int n = (it->what == IT_COMPOSITION ? it->cmp_it.nchars : 1);
4206 it_copy = *it;
4207 while (n--)
4208 bidi_move_to_visually_next (&it_copy.bidi_it);
4210 SET_TEXT_POS (pos,
4211 it_copy.bidi_it.charpos, it_copy.bidi_it.bytepos);
4214 eassert (BEGV <= CHARPOS (pos) && CHARPOS (pos) <= ZV);
4216 /* Determine face for CHARSET_ASCII, or unibyte. */
4217 face_id = face_at_buffer_position (it->w,
4218 CHARPOS (pos),
4219 &next_check_charpos,
4220 limit, 0, -1);
4222 /* Correct the face for charsets different from ASCII. Do it
4223 for the multibyte case only. The face returned above is
4224 suitable for unibyte text if current_buffer is unibyte. */
4225 if (it->multibyte_p)
4227 int c = FETCH_MULTIBYTE_CHAR (BYTEPOS (pos));
4228 struct face *face = FACE_FROM_ID (it->f, face_id);
4229 face_id = FACE_FOR_CHAR (it->f, face, c, CHARPOS (pos), Qnil);
4233 return face_id;
4238 /***********************************************************************
4239 Invisible text
4240 ***********************************************************************/
4242 /* Set up iterator IT from invisible properties at its current
4243 position. Called from handle_stop. */
4245 static enum prop_handled
4246 handle_invisible_prop (struct it *it)
4248 enum prop_handled handled = HANDLED_NORMALLY;
4249 int invis_p;
4250 Lisp_Object prop;
4252 if (STRINGP (it->string))
4254 Lisp_Object end_charpos, limit, charpos;
4256 /* Get the value of the invisible text property at the
4257 current position. Value will be nil if there is no such
4258 property. */
4259 charpos = make_number (IT_STRING_CHARPOS (*it));
4260 prop = Fget_text_property (charpos, Qinvisible, it->string);
4261 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
4263 if (invis_p && IT_STRING_CHARPOS (*it) < it->end_charpos)
4265 /* Record whether we have to display an ellipsis for the
4266 invisible text. */
4267 int display_ellipsis_p = (invis_p == 2);
4268 ptrdiff_t len, endpos;
4270 handled = HANDLED_RECOMPUTE_PROPS;
4272 /* Get the position at which the next visible text can be
4273 found in IT->string, if any. */
4274 endpos = len = SCHARS (it->string);
4275 XSETINT (limit, len);
4278 end_charpos = Fnext_single_property_change (charpos, Qinvisible,
4279 it->string, limit);
4280 if (INTEGERP (end_charpos))
4282 endpos = XFASTINT (end_charpos);
4283 prop = Fget_text_property (end_charpos, Qinvisible, it->string);
4284 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
4285 if (invis_p == 2)
4286 display_ellipsis_p = true;
4289 while (invis_p && endpos < len);
4291 if (display_ellipsis_p)
4292 it->ellipsis_p = true;
4294 if (endpos < len)
4296 /* Text at END_CHARPOS is visible. Move IT there. */
4297 struct text_pos old;
4298 ptrdiff_t oldpos;
4300 old = it->current.string_pos;
4301 oldpos = CHARPOS (old);
4302 if (it->bidi_p)
4304 if (it->bidi_it.first_elt
4305 && it->bidi_it.charpos < SCHARS (it->string))
4306 bidi_paragraph_init (it->paragraph_embedding,
4307 &it->bidi_it, 1);
4308 /* Bidi-iterate out of the invisible text. */
4311 bidi_move_to_visually_next (&it->bidi_it);
4313 while (oldpos <= it->bidi_it.charpos
4314 && it->bidi_it.charpos < endpos);
4316 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
4317 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
4318 if (IT_CHARPOS (*it) >= endpos)
4319 it->prev_stop = endpos;
4321 else
4323 IT_STRING_CHARPOS (*it) = XFASTINT (end_charpos);
4324 compute_string_pos (&it->current.string_pos, old, it->string);
4327 else
4329 /* The rest of the string is invisible. If this is an
4330 overlay string, proceed with the next overlay string
4331 or whatever comes and return a character from there. */
4332 if (it->current.overlay_string_index >= 0
4333 && !display_ellipsis_p)
4335 next_overlay_string (it);
4336 /* Don't check for overlay strings when we just
4337 finished processing them. */
4338 handled = HANDLED_OVERLAY_STRING_CONSUMED;
4340 else
4342 IT_STRING_CHARPOS (*it) = SCHARS (it->string);
4343 IT_STRING_BYTEPOS (*it) = SBYTES (it->string);
4348 else
4350 ptrdiff_t newpos, next_stop, start_charpos, tem;
4351 Lisp_Object pos, overlay;
4353 /* First of all, is there invisible text at this position? */
4354 tem = start_charpos = IT_CHARPOS (*it);
4355 pos = make_number (tem);
4356 prop = get_char_property_and_overlay (pos, Qinvisible, it->window,
4357 &overlay);
4358 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
4360 /* If we are on invisible text, skip over it. */
4361 if (invis_p && start_charpos < it->end_charpos)
4363 /* Record whether we have to display an ellipsis for the
4364 invisible text. */
4365 int display_ellipsis_p = invis_p == 2;
4367 handled = HANDLED_RECOMPUTE_PROPS;
4369 /* Loop skipping over invisible text. The loop is left at
4370 ZV or with IT on the first char being visible again. */
4373 /* Try to skip some invisible text. Return value is the
4374 position reached which can be equal to where we start
4375 if there is nothing invisible there. This skips both
4376 over invisible text properties and overlays with
4377 invisible property. */
4378 newpos = skip_invisible (tem, &next_stop, ZV, it->window);
4380 /* If we skipped nothing at all we weren't at invisible
4381 text in the first place. If everything to the end of
4382 the buffer was skipped, end the loop. */
4383 if (newpos == tem || newpos >= ZV)
4384 invis_p = 0;
4385 else
4387 /* We skipped some characters but not necessarily
4388 all there are. Check if we ended up on visible
4389 text. Fget_char_property returns the property of
4390 the char before the given position, i.e. if we
4391 get invis_p = 0, this means that the char at
4392 newpos is visible. */
4393 pos = make_number (newpos);
4394 prop = Fget_char_property (pos, Qinvisible, it->window);
4395 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
4398 /* If we ended up on invisible text, proceed to
4399 skip starting with next_stop. */
4400 if (invis_p)
4401 tem = next_stop;
4403 /* If there are adjacent invisible texts, don't lose the
4404 second one's ellipsis. */
4405 if (invis_p == 2)
4406 display_ellipsis_p = true;
4408 while (invis_p);
4410 /* The position newpos is now either ZV or on visible text. */
4411 if (it->bidi_p)
4413 ptrdiff_t bpos = CHAR_TO_BYTE (newpos);
4414 int on_newline
4415 = bpos == ZV_BYTE || FETCH_BYTE (bpos) == '\n';
4416 int after_newline
4417 = newpos <= BEGV || FETCH_BYTE (bpos - 1) == '\n';
4419 /* If the invisible text ends on a newline or on a
4420 character after a newline, we can avoid the costly,
4421 character by character, bidi iteration to NEWPOS, and
4422 instead simply reseat the iterator there. That's
4423 because all bidi reordering information is tossed at
4424 the newline. This is a big win for modes that hide
4425 complete lines, like Outline, Org, etc. */
4426 if (on_newline || after_newline)
4428 struct text_pos tpos;
4429 bidi_dir_t pdir = it->bidi_it.paragraph_dir;
4431 SET_TEXT_POS (tpos, newpos, bpos);
4432 reseat_1 (it, tpos, 0);
4433 /* If we reseat on a newline/ZV, we need to prep the
4434 bidi iterator for advancing to the next character
4435 after the newline/EOB, keeping the current paragraph
4436 direction (so that PRODUCE_GLYPHS does TRT wrt
4437 prepending/appending glyphs to a glyph row). */
4438 if (on_newline)
4440 it->bidi_it.first_elt = 0;
4441 it->bidi_it.paragraph_dir = pdir;
4442 it->bidi_it.ch = (bpos == ZV_BYTE) ? -1 : '\n';
4443 it->bidi_it.nchars = 1;
4444 it->bidi_it.ch_len = 1;
4447 else /* Must use the slow method. */
4449 /* With bidi iteration, the region of invisible text
4450 could start and/or end in the middle of a
4451 non-base embedding level. Therefore, we need to
4452 skip invisible text using the bidi iterator,
4453 starting at IT's current position, until we find
4454 ourselves outside of the invisible text.
4455 Skipping invisible text _after_ bidi iteration
4456 avoids affecting the visual order of the
4457 displayed text when invisible properties are
4458 added or removed. */
4459 if (it->bidi_it.first_elt && it->bidi_it.charpos < ZV)
4461 /* If we were `reseat'ed to a new paragraph,
4462 determine the paragraph base direction. We
4463 need to do it now because
4464 next_element_from_buffer may not have a
4465 chance to do it, if we are going to skip any
4466 text at the beginning, which resets the
4467 FIRST_ELT flag. */
4468 bidi_paragraph_init (it->paragraph_embedding,
4469 &it->bidi_it, 1);
4473 bidi_move_to_visually_next (&it->bidi_it);
4475 while (it->stop_charpos <= it->bidi_it.charpos
4476 && it->bidi_it.charpos < newpos);
4477 IT_CHARPOS (*it) = it->bidi_it.charpos;
4478 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
4479 /* If we overstepped NEWPOS, record its position in
4480 the iterator, so that we skip invisible text if
4481 later the bidi iteration lands us in the
4482 invisible region again. */
4483 if (IT_CHARPOS (*it) >= newpos)
4484 it->prev_stop = newpos;
4487 else
4489 IT_CHARPOS (*it) = newpos;
4490 IT_BYTEPOS (*it) = CHAR_TO_BYTE (newpos);
4493 /* If there are before-strings at the start of invisible
4494 text, and the text is invisible because of a text
4495 property, arrange to show before-strings because 20.x did
4496 it that way. (If the text is invisible because of an
4497 overlay property instead of a text property, this is
4498 already handled in the overlay code.) */
4499 if (NILP (overlay)
4500 && get_overlay_strings (it, it->stop_charpos))
4502 handled = HANDLED_RECOMPUTE_PROPS;
4503 it->stack[it->sp - 1].display_ellipsis_p = display_ellipsis_p;
4505 else if (display_ellipsis_p)
4507 /* Make sure that the glyphs of the ellipsis will get
4508 correct `charpos' values. If we would not update
4509 it->position here, the glyphs would belong to the
4510 last visible character _before_ the invisible
4511 text, which confuses `set_cursor_from_row'.
4513 We use the last invisible position instead of the
4514 first because this way the cursor is always drawn on
4515 the first "." of the ellipsis, whenever PT is inside
4516 the invisible text. Otherwise the cursor would be
4517 placed _after_ the ellipsis when the point is after the
4518 first invisible character. */
4519 if (!STRINGP (it->object))
4521 it->position.charpos = newpos - 1;
4522 it->position.bytepos = CHAR_TO_BYTE (it->position.charpos);
4524 it->ellipsis_p = true;
4525 /* Let the ellipsis display before
4526 considering any properties of the following char.
4527 Fixes jasonr@gnu.org 01 Oct 07 bug. */
4528 handled = HANDLED_RETURN;
4533 return handled;
4537 /* Make iterator IT return `...' next.
4538 Replaces LEN characters from buffer. */
4540 static void
4541 setup_for_ellipsis (struct it *it, int len)
4543 /* Use the display table definition for `...'. Invalid glyphs
4544 will be handled by the method returning elements from dpvec. */
4545 if (it->dp && VECTORP (DISP_INVIS_VECTOR (it->dp)))
4547 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
4548 it->dpvec = v->contents;
4549 it->dpend = v->contents + v->header.size;
4551 else
4553 /* Default `...'. */
4554 it->dpvec = default_invis_vector;
4555 it->dpend = default_invis_vector + 3;
4558 it->dpvec_char_len = len;
4559 it->current.dpvec_index = 0;
4560 it->dpvec_face_id = -1;
4562 /* Remember the current face id in case glyphs specify faces.
4563 IT's face is restored in set_iterator_to_next.
4564 saved_face_id was set to preceding char's face in handle_stop. */
4565 if (it->saved_face_id < 0 || it->saved_face_id != it->face_id)
4566 it->saved_face_id = it->face_id = DEFAULT_FACE_ID;
4568 it->method = GET_FROM_DISPLAY_VECTOR;
4569 it->ellipsis_p = true;
4574 /***********************************************************************
4575 'display' property
4576 ***********************************************************************/
4578 /* Set up iterator IT from `display' property at its current position.
4579 Called from handle_stop.
4580 We return HANDLED_RETURN if some part of the display property
4581 overrides the display of the buffer text itself.
4582 Otherwise we return HANDLED_NORMALLY. */
4584 static enum prop_handled
4585 handle_display_prop (struct it *it)
4587 Lisp_Object propval, object, overlay;
4588 struct text_pos *position;
4589 ptrdiff_t bufpos;
4590 /* Nonzero if some property replaces the display of the text itself. */
4591 int display_replaced_p = 0;
4593 if (STRINGP (it->string))
4595 object = it->string;
4596 position = &it->current.string_pos;
4597 bufpos = CHARPOS (it->current.pos);
4599 else
4601 XSETWINDOW (object, it->w);
4602 position = &it->current.pos;
4603 bufpos = CHARPOS (*position);
4606 /* Reset those iterator values set from display property values. */
4607 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
4608 it->space_width = Qnil;
4609 it->font_height = Qnil;
4610 it->voffset = 0;
4612 /* We don't support recursive `display' properties, i.e. string
4613 values that have a string `display' property, that have a string
4614 `display' property etc. */
4615 if (!it->string_from_display_prop_p)
4616 it->area = TEXT_AREA;
4618 propval = get_char_property_and_overlay (make_number (position->charpos),
4619 Qdisplay, object, &overlay);
4620 if (NILP (propval))
4621 return HANDLED_NORMALLY;
4622 /* Now OVERLAY is the overlay that gave us this property, or nil
4623 if it was a text property. */
4625 if (!STRINGP (it->string))
4626 object = it->w->contents;
4628 display_replaced_p = handle_display_spec (it, propval, object, overlay,
4629 position, bufpos,
4630 FRAME_WINDOW_P (it->f));
4632 return display_replaced_p ? HANDLED_RETURN : HANDLED_NORMALLY;
4635 /* Subroutine of handle_display_prop. Returns non-zero if the display
4636 specification in SPEC is a replacing specification, i.e. it would
4637 replace the text covered by `display' property with something else,
4638 such as an image or a display string. If SPEC includes any kind or
4639 `(space ...) specification, the value is 2; this is used by
4640 compute_display_string_pos, which see.
4642 See handle_single_display_spec for documentation of arguments.
4643 frame_window_p is non-zero if the window being redisplayed is on a
4644 GUI frame; this argument is used only if IT is NULL, see below.
4646 IT can be NULL, if this is called by the bidi reordering code
4647 through compute_display_string_pos, which see. In that case, this
4648 function only examines SPEC, but does not otherwise "handle" it, in
4649 the sense that it doesn't set up members of IT from the display
4650 spec. */
4651 static int
4652 handle_display_spec (struct it *it, Lisp_Object spec, Lisp_Object object,
4653 Lisp_Object overlay, struct text_pos *position,
4654 ptrdiff_t bufpos, int frame_window_p)
4656 int replacing_p = 0;
4657 int rv;
4659 if (CONSP (spec)
4660 /* Simple specifications. */
4661 && !EQ (XCAR (spec), Qimage)
4662 && !EQ (XCAR (spec), Qspace)
4663 && !EQ (XCAR (spec), Qwhen)
4664 && !EQ (XCAR (spec), Qslice)
4665 && !EQ (XCAR (spec), Qspace_width)
4666 && !EQ (XCAR (spec), Qheight)
4667 && !EQ (XCAR (spec), Qraise)
4668 /* Marginal area specifications. */
4669 && !(CONSP (XCAR (spec)) && EQ (XCAR (XCAR (spec)), Qmargin))
4670 && !EQ (XCAR (spec), Qleft_fringe)
4671 && !EQ (XCAR (spec), Qright_fringe)
4672 && !NILP (XCAR (spec)))
4674 for (; CONSP (spec); spec = XCDR (spec))
4676 if ((rv = handle_single_display_spec (it, XCAR (spec), object,
4677 overlay, position, bufpos,
4678 replacing_p, frame_window_p)))
4680 replacing_p = rv;
4681 /* If some text in a string is replaced, `position' no
4682 longer points to the position of `object'. */
4683 if (!it || STRINGP (object))
4684 break;
4688 else if (VECTORP (spec))
4690 ptrdiff_t i;
4691 for (i = 0; i < ASIZE (spec); ++i)
4692 if ((rv = handle_single_display_spec (it, AREF (spec, i), object,
4693 overlay, position, bufpos,
4694 replacing_p, frame_window_p)))
4696 replacing_p = rv;
4697 /* If some text in a string is replaced, `position' no
4698 longer points to the position of `object'. */
4699 if (!it || STRINGP (object))
4700 break;
4703 else
4705 if ((rv = handle_single_display_spec (it, spec, object, overlay,
4706 position, bufpos, 0,
4707 frame_window_p)))
4708 replacing_p = rv;
4711 return replacing_p;
4714 /* Value is the position of the end of the `display' property starting
4715 at START_POS in OBJECT. */
4717 static struct text_pos
4718 display_prop_end (struct it *it, Lisp_Object object, struct text_pos start_pos)
4720 Lisp_Object end;
4721 struct text_pos end_pos;
4723 end = Fnext_single_char_property_change (make_number (CHARPOS (start_pos)),
4724 Qdisplay, object, Qnil);
4725 CHARPOS (end_pos) = XFASTINT (end);
4726 if (STRINGP (object))
4727 compute_string_pos (&end_pos, start_pos, it->string);
4728 else
4729 BYTEPOS (end_pos) = CHAR_TO_BYTE (XFASTINT (end));
4731 return end_pos;
4735 /* Set up IT from a single `display' property specification SPEC. OBJECT
4736 is the object in which the `display' property was found. *POSITION
4737 is the position in OBJECT at which the `display' property was found.
4738 BUFPOS is the buffer position of OBJECT (different from POSITION if
4739 OBJECT is not a buffer). DISPLAY_REPLACED_P non-zero means that we
4740 previously saw a display specification which already replaced text
4741 display with something else, for example an image; we ignore such
4742 properties after the first one has been processed.
4744 OVERLAY is the overlay this `display' property came from,
4745 or nil if it was a text property.
4747 If SPEC is a `space' or `image' specification, and in some other
4748 cases too, set *POSITION to the position where the `display'
4749 property ends.
4751 If IT is NULL, only examine the property specification in SPEC, but
4752 don't set up IT. In that case, FRAME_WINDOW_P non-zero means SPEC
4753 is intended to be displayed in a window on a GUI frame.
4755 Value is non-zero if something was found which replaces the display
4756 of buffer or string text. */
4758 static int
4759 handle_single_display_spec (struct it *it, Lisp_Object spec, Lisp_Object object,
4760 Lisp_Object overlay, struct text_pos *position,
4761 ptrdiff_t bufpos, int display_replaced_p,
4762 int frame_window_p)
4764 Lisp_Object form;
4765 Lisp_Object location, value;
4766 struct text_pos start_pos = *position;
4767 int valid_p;
4769 /* If SPEC is a list of the form `(when FORM . VALUE)', evaluate FORM.
4770 If the result is non-nil, use VALUE instead of SPEC. */
4771 form = Qt;
4772 if (CONSP (spec) && EQ (XCAR (spec), Qwhen))
4774 spec = XCDR (spec);
4775 if (!CONSP (spec))
4776 return 0;
4777 form = XCAR (spec);
4778 spec = XCDR (spec);
4781 if (!NILP (form) && !EQ (form, Qt))
4783 ptrdiff_t count = SPECPDL_INDEX ();
4784 struct gcpro gcpro1;
4786 /* Bind `object' to the object having the `display' property, a
4787 buffer or string. Bind `position' to the position in the
4788 object where the property was found, and `buffer-position'
4789 to the current position in the buffer. */
4791 if (NILP (object))
4792 XSETBUFFER (object, current_buffer);
4793 specbind (Qobject, object);
4794 specbind (Qposition, make_number (CHARPOS (*position)));
4795 specbind (Qbuffer_position, make_number (bufpos));
4796 GCPRO1 (form);
4797 form = safe_eval (form);
4798 UNGCPRO;
4799 unbind_to (count, Qnil);
4802 if (NILP (form))
4803 return 0;
4805 /* Handle `(height HEIGHT)' specifications. */
4806 if (CONSP (spec)
4807 && EQ (XCAR (spec), Qheight)
4808 && CONSP (XCDR (spec)))
4810 if (it)
4812 if (!FRAME_WINDOW_P (it->f))
4813 return 0;
4815 it->font_height = XCAR (XCDR (spec));
4816 if (!NILP (it->font_height))
4818 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4819 int new_height = -1;
4821 if (CONSP (it->font_height)
4822 && (EQ (XCAR (it->font_height), Qplus)
4823 || EQ (XCAR (it->font_height), Qminus))
4824 && CONSP (XCDR (it->font_height))
4825 && RANGED_INTEGERP (0, XCAR (XCDR (it->font_height)), INT_MAX))
4827 /* `(+ N)' or `(- N)' where N is an integer. */
4828 int steps = XINT (XCAR (XCDR (it->font_height)));
4829 if (EQ (XCAR (it->font_height), Qplus))
4830 steps = - steps;
4831 it->face_id = smaller_face (it->f, it->face_id, steps);
4833 else if (FUNCTIONP (it->font_height))
4835 /* Call function with current height as argument.
4836 Value is the new height. */
4837 Lisp_Object height;
4838 height = safe_call1 (it->font_height,
4839 face->lface[LFACE_HEIGHT_INDEX]);
4840 if (NUMBERP (height))
4841 new_height = XFLOATINT (height);
4843 else if (NUMBERP (it->font_height))
4845 /* Value is a multiple of the canonical char height. */
4846 struct face *f;
4848 f = FACE_FROM_ID (it->f,
4849 lookup_basic_face (it->f, DEFAULT_FACE_ID));
4850 new_height = (XFLOATINT (it->font_height)
4851 * XINT (f->lface[LFACE_HEIGHT_INDEX]));
4853 else
4855 /* Evaluate IT->font_height with `height' bound to the
4856 current specified height to get the new height. */
4857 ptrdiff_t count = SPECPDL_INDEX ();
4859 specbind (Qheight, face->lface[LFACE_HEIGHT_INDEX]);
4860 value = safe_eval (it->font_height);
4861 unbind_to (count, Qnil);
4863 if (NUMBERP (value))
4864 new_height = XFLOATINT (value);
4867 if (new_height > 0)
4868 it->face_id = face_with_height (it->f, it->face_id, new_height);
4872 return 0;
4875 /* Handle `(space-width WIDTH)'. */
4876 if (CONSP (spec)
4877 && EQ (XCAR (spec), Qspace_width)
4878 && CONSP (XCDR (spec)))
4880 if (it)
4882 if (!FRAME_WINDOW_P (it->f))
4883 return 0;
4885 value = XCAR (XCDR (spec));
4886 if (NUMBERP (value) && XFLOATINT (value) > 0)
4887 it->space_width = value;
4890 return 0;
4893 /* Handle `(slice X Y WIDTH HEIGHT)'. */
4894 if (CONSP (spec)
4895 && EQ (XCAR (spec), Qslice))
4897 Lisp_Object tem;
4899 if (it)
4901 if (!FRAME_WINDOW_P (it->f))
4902 return 0;
4904 if (tem = XCDR (spec), CONSP (tem))
4906 it->slice.x = XCAR (tem);
4907 if (tem = XCDR (tem), CONSP (tem))
4909 it->slice.y = XCAR (tem);
4910 if (tem = XCDR (tem), CONSP (tem))
4912 it->slice.width = XCAR (tem);
4913 if (tem = XCDR (tem), CONSP (tem))
4914 it->slice.height = XCAR (tem);
4920 return 0;
4923 /* Handle `(raise FACTOR)'. */
4924 if (CONSP (spec)
4925 && EQ (XCAR (spec), Qraise)
4926 && CONSP (XCDR (spec)))
4928 if (it)
4930 if (!FRAME_WINDOW_P (it->f))
4931 return 0;
4933 #ifdef HAVE_WINDOW_SYSTEM
4934 value = XCAR (XCDR (spec));
4935 if (NUMBERP (value))
4937 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4938 it->voffset = - (XFLOATINT (value)
4939 * (FONT_HEIGHT (face->font)));
4941 #endif /* HAVE_WINDOW_SYSTEM */
4944 return 0;
4947 /* Don't handle the other kinds of display specifications
4948 inside a string that we got from a `display' property. */
4949 if (it && it->string_from_display_prop_p)
4950 return 0;
4952 /* Characters having this form of property are not displayed, so
4953 we have to find the end of the property. */
4954 if (it)
4956 start_pos = *position;
4957 *position = display_prop_end (it, object, start_pos);
4959 value = Qnil;
4961 /* Stop the scan at that end position--we assume that all
4962 text properties change there. */
4963 if (it)
4964 it->stop_charpos = position->charpos;
4966 /* Handle `(left-fringe BITMAP [FACE])'
4967 and `(right-fringe BITMAP [FACE])'. */
4968 if (CONSP (spec)
4969 && (EQ (XCAR (spec), Qleft_fringe)
4970 || EQ (XCAR (spec), Qright_fringe))
4971 && CONSP (XCDR (spec)))
4973 int fringe_bitmap;
4975 if (it)
4977 if (!FRAME_WINDOW_P (it->f))
4978 /* If we return here, POSITION has been advanced
4979 across the text with this property. */
4981 /* Synchronize the bidi iterator with POSITION. This is
4982 needed because we are not going to push the iterator
4983 on behalf of this display property, so there will be
4984 no pop_it call to do this synchronization for us. */
4985 if (it->bidi_p)
4987 it->position = *position;
4988 iterate_out_of_display_property (it);
4989 *position = it->position;
4991 return 1;
4994 else if (!frame_window_p)
4995 return 1;
4997 #ifdef HAVE_WINDOW_SYSTEM
4998 value = XCAR (XCDR (spec));
4999 if (!SYMBOLP (value)
5000 || !(fringe_bitmap = lookup_fringe_bitmap (value)))
5001 /* If we return here, POSITION has been advanced
5002 across the text with this property. */
5004 if (it && it->bidi_p)
5006 it->position = *position;
5007 iterate_out_of_display_property (it);
5008 *position = it->position;
5010 return 1;
5013 if (it)
5015 int face_id = lookup_basic_face (it->f, DEFAULT_FACE_ID);;
5017 if (CONSP (XCDR (XCDR (spec))))
5019 Lisp_Object face_name = XCAR (XCDR (XCDR (spec)));
5020 int face_id2 = lookup_derived_face (it->f, face_name,
5021 FRINGE_FACE_ID, 0);
5022 if (face_id2 >= 0)
5023 face_id = face_id2;
5026 /* Save current settings of IT so that we can restore them
5027 when we are finished with the glyph property value. */
5028 push_it (it, position);
5030 it->area = TEXT_AREA;
5031 it->what = IT_IMAGE;
5032 it->image_id = -1; /* no image */
5033 it->position = start_pos;
5034 it->object = NILP (object) ? it->w->contents : object;
5035 it->method = GET_FROM_IMAGE;
5036 it->from_overlay = Qnil;
5037 it->face_id = face_id;
5038 it->from_disp_prop_p = true;
5040 /* Say that we haven't consumed the characters with
5041 `display' property yet. The call to pop_it in
5042 set_iterator_to_next will clean this up. */
5043 *position = start_pos;
5045 if (EQ (XCAR (spec), Qleft_fringe))
5047 it->left_user_fringe_bitmap = fringe_bitmap;
5048 it->left_user_fringe_face_id = face_id;
5050 else
5052 it->right_user_fringe_bitmap = fringe_bitmap;
5053 it->right_user_fringe_face_id = face_id;
5056 #endif /* HAVE_WINDOW_SYSTEM */
5057 return 1;
5060 /* Prepare to handle `((margin left-margin) ...)',
5061 `((margin right-margin) ...)' and `((margin nil) ...)'
5062 prefixes for display specifications. */
5063 location = Qunbound;
5064 if (CONSP (spec) && CONSP (XCAR (spec)))
5066 Lisp_Object tem;
5068 value = XCDR (spec);
5069 if (CONSP (value))
5070 value = XCAR (value);
5072 tem = XCAR (spec);
5073 if (EQ (XCAR (tem), Qmargin)
5074 && (tem = XCDR (tem),
5075 tem = CONSP (tem) ? XCAR (tem) : Qnil,
5076 (NILP (tem)
5077 || EQ (tem, Qleft_margin)
5078 || EQ (tem, Qright_margin))))
5079 location = tem;
5082 if (EQ (location, Qunbound))
5084 location = Qnil;
5085 value = spec;
5088 /* After this point, VALUE is the property after any
5089 margin prefix has been stripped. It must be a string,
5090 an image specification, or `(space ...)'.
5092 LOCATION specifies where to display: `left-margin',
5093 `right-margin' or nil. */
5095 valid_p = (STRINGP (value)
5096 #ifdef HAVE_WINDOW_SYSTEM
5097 || ((it ? FRAME_WINDOW_P (it->f) : frame_window_p)
5098 && valid_image_p (value))
5099 #endif /* not HAVE_WINDOW_SYSTEM */
5100 || (CONSP (value) && EQ (XCAR (value), Qspace)));
5102 if (valid_p && !display_replaced_p)
5104 int retval = 1;
5106 if (!it)
5108 /* Callers need to know whether the display spec is any kind
5109 of `(space ...)' spec that is about to affect text-area
5110 display. */
5111 if (CONSP (value) && EQ (XCAR (value), Qspace) && NILP (location))
5112 retval = 2;
5113 return retval;
5116 /* Save current settings of IT so that we can restore them
5117 when we are finished with the glyph property value. */
5118 push_it (it, position);
5119 it->from_overlay = overlay;
5120 it->from_disp_prop_p = true;
5122 if (NILP (location))
5123 it->area = TEXT_AREA;
5124 else if (EQ (location, Qleft_margin))
5125 it->area = LEFT_MARGIN_AREA;
5126 else
5127 it->area = RIGHT_MARGIN_AREA;
5129 if (STRINGP (value))
5131 it->string = value;
5132 it->multibyte_p = STRING_MULTIBYTE (it->string);
5133 it->current.overlay_string_index = -1;
5134 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
5135 it->end_charpos = it->string_nchars = SCHARS (it->string);
5136 it->method = GET_FROM_STRING;
5137 it->stop_charpos = 0;
5138 it->prev_stop = 0;
5139 it->base_level_stop = 0;
5140 it->string_from_display_prop_p = true;
5141 /* Say that we haven't consumed the characters with
5142 `display' property yet. The call to pop_it in
5143 set_iterator_to_next will clean this up. */
5144 if (BUFFERP (object))
5145 *position = start_pos;
5147 /* Force paragraph direction to be that of the parent
5148 object. If the parent object's paragraph direction is
5149 not yet determined, default to L2R. */
5150 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
5151 it->paragraph_embedding = it->bidi_it.paragraph_dir;
5152 else
5153 it->paragraph_embedding = L2R;
5155 /* Set up the bidi iterator for this display string. */
5156 if (it->bidi_p)
5158 it->bidi_it.string.lstring = it->string;
5159 it->bidi_it.string.s = NULL;
5160 it->bidi_it.string.schars = it->end_charpos;
5161 it->bidi_it.string.bufpos = bufpos;
5162 it->bidi_it.string.from_disp_str = 1;
5163 it->bidi_it.string.unibyte = !it->multibyte_p;
5164 it->bidi_it.w = it->w;
5165 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
5168 else if (CONSP (value) && EQ (XCAR (value), Qspace))
5170 it->method = GET_FROM_STRETCH;
5171 it->object = value;
5172 *position = it->position = start_pos;
5173 retval = 1 + (it->area == TEXT_AREA);
5175 #ifdef HAVE_WINDOW_SYSTEM
5176 else
5178 it->what = IT_IMAGE;
5179 it->image_id = lookup_image (it->f, value);
5180 it->position = start_pos;
5181 it->object = NILP (object) ? it->w->contents : object;
5182 it->method = GET_FROM_IMAGE;
5184 /* Say that we haven't consumed the characters with
5185 `display' property yet. The call to pop_it in
5186 set_iterator_to_next will clean this up. */
5187 *position = start_pos;
5189 #endif /* HAVE_WINDOW_SYSTEM */
5191 return retval;
5194 /* Invalid property or property not supported. Restore
5195 POSITION to what it was before. */
5196 *position = start_pos;
5197 return 0;
5200 /* Check if PROP is a display property value whose text should be
5201 treated as intangible. OVERLAY is the overlay from which PROP
5202 came, or nil if it came from a text property. CHARPOS and BYTEPOS
5203 specify the buffer position covered by PROP. */
5206 display_prop_intangible_p (Lisp_Object prop, Lisp_Object overlay,
5207 ptrdiff_t charpos, ptrdiff_t bytepos)
5209 int frame_window_p = FRAME_WINDOW_P (XFRAME (selected_frame));
5210 struct text_pos position;
5212 SET_TEXT_POS (position, charpos, bytepos);
5213 return handle_display_spec (NULL, prop, Qnil, overlay,
5214 &position, charpos, frame_window_p);
5218 /* Return 1 if PROP is a display sub-property value containing STRING.
5220 Implementation note: this and the following function are really
5221 special cases of handle_display_spec and
5222 handle_single_display_spec, and should ideally use the same code.
5223 Until they do, these two pairs must be consistent and must be
5224 modified in sync. */
5226 static int
5227 single_display_spec_string_p (Lisp_Object prop, Lisp_Object string)
5229 if (EQ (string, prop))
5230 return 1;
5232 /* Skip over `when FORM'. */
5233 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
5235 prop = XCDR (prop);
5236 if (!CONSP (prop))
5237 return 0;
5238 /* Actually, the condition following `when' should be eval'ed,
5239 like handle_single_display_spec does, and we should return
5240 zero if it evaluates to nil. However, this function is
5241 called only when the buffer was already displayed and some
5242 glyph in the glyph matrix was found to come from a display
5243 string. Therefore, the condition was already evaluated, and
5244 the result was non-nil, otherwise the display string wouldn't
5245 have been displayed and we would have never been called for
5246 this property. Thus, we can skip the evaluation and assume
5247 its result is non-nil. */
5248 prop = XCDR (prop);
5251 if (CONSP (prop))
5252 /* Skip over `margin LOCATION'. */
5253 if (EQ (XCAR (prop), Qmargin))
5255 prop = XCDR (prop);
5256 if (!CONSP (prop))
5257 return 0;
5259 prop = XCDR (prop);
5260 if (!CONSP (prop))
5261 return 0;
5264 return EQ (prop, string) || (CONSP (prop) && EQ (XCAR (prop), string));
5268 /* Return 1 if STRING appears in the `display' property PROP. */
5270 static int
5271 display_prop_string_p (Lisp_Object prop, Lisp_Object string)
5273 if (CONSP (prop)
5274 && !EQ (XCAR (prop), Qwhen)
5275 && !(CONSP (XCAR (prop)) && EQ (Qmargin, XCAR (XCAR (prop)))))
5277 /* A list of sub-properties. */
5278 while (CONSP (prop))
5280 if (single_display_spec_string_p (XCAR (prop), string))
5281 return 1;
5282 prop = XCDR (prop);
5285 else if (VECTORP (prop))
5287 /* A vector of sub-properties. */
5288 ptrdiff_t i;
5289 for (i = 0; i < ASIZE (prop); ++i)
5290 if (single_display_spec_string_p (AREF (prop, i), string))
5291 return 1;
5293 else
5294 return single_display_spec_string_p (prop, string);
5296 return 0;
5299 /* Look for STRING in overlays and text properties in the current
5300 buffer, between character positions FROM and TO (excluding TO).
5301 BACK_P non-zero means look back (in this case, TO is supposed to be
5302 less than FROM).
5303 Value is the first character position where STRING was found, or
5304 zero if it wasn't found before hitting TO.
5306 This function may only use code that doesn't eval because it is
5307 called asynchronously from note_mouse_highlight. */
5309 static ptrdiff_t
5310 string_buffer_position_lim (Lisp_Object string,
5311 ptrdiff_t from, ptrdiff_t to, int back_p)
5313 Lisp_Object limit, prop, pos;
5314 int found = 0;
5316 pos = make_number (max (from, BEGV));
5318 if (!back_p) /* looking forward */
5320 limit = make_number (min (to, ZV));
5321 while (!found && !EQ (pos, limit))
5323 prop = Fget_char_property (pos, Qdisplay, Qnil);
5324 if (!NILP (prop) && display_prop_string_p (prop, string))
5325 found = 1;
5326 else
5327 pos = Fnext_single_char_property_change (pos, Qdisplay, Qnil,
5328 limit);
5331 else /* looking back */
5333 limit = make_number (max (to, BEGV));
5334 while (!found && !EQ (pos, limit))
5336 prop = Fget_char_property (pos, Qdisplay, Qnil);
5337 if (!NILP (prop) && display_prop_string_p (prop, string))
5338 found = 1;
5339 else
5340 pos = Fprevious_single_char_property_change (pos, Qdisplay, Qnil,
5341 limit);
5345 return found ? XINT (pos) : 0;
5348 /* Determine which buffer position in current buffer STRING comes from.
5349 AROUND_CHARPOS is an approximate position where it could come from.
5350 Value is the buffer position or 0 if it couldn't be determined.
5352 This function is necessary because we don't record buffer positions
5353 in glyphs generated from strings (to keep struct glyph small).
5354 This function may only use code that doesn't eval because it is
5355 called asynchronously from note_mouse_highlight. */
5357 static ptrdiff_t
5358 string_buffer_position (Lisp_Object string, ptrdiff_t around_charpos)
5360 const int MAX_DISTANCE = 1000;
5361 ptrdiff_t found = string_buffer_position_lim (string, around_charpos,
5362 around_charpos + MAX_DISTANCE,
5365 if (!found)
5366 found = string_buffer_position_lim (string, around_charpos,
5367 around_charpos - MAX_DISTANCE, 1);
5368 return found;
5373 /***********************************************************************
5374 `composition' property
5375 ***********************************************************************/
5377 /* Set up iterator IT from `composition' property at its current
5378 position. Called from handle_stop. */
5380 static enum prop_handled
5381 handle_composition_prop (struct it *it)
5383 Lisp_Object prop, string;
5384 ptrdiff_t pos, pos_byte, start, end;
5386 if (STRINGP (it->string))
5388 unsigned char *s;
5390 pos = IT_STRING_CHARPOS (*it);
5391 pos_byte = IT_STRING_BYTEPOS (*it);
5392 string = it->string;
5393 s = SDATA (string) + pos_byte;
5394 it->c = STRING_CHAR (s);
5396 else
5398 pos = IT_CHARPOS (*it);
5399 pos_byte = IT_BYTEPOS (*it);
5400 string = Qnil;
5401 it->c = FETCH_CHAR (pos_byte);
5404 /* If there's a valid composition and point is not inside of the
5405 composition (in the case that the composition is from the current
5406 buffer), draw a glyph composed from the composition components. */
5407 if (find_composition (pos, -1, &start, &end, &prop, string)
5408 && composition_valid_p (start, end, prop)
5409 && (STRINGP (it->string) || (PT <= start || PT >= end)))
5411 if (start < pos)
5412 /* As we can't handle this situation (perhaps font-lock added
5413 a new composition), we just return here hoping that next
5414 redisplay will detect this composition much earlier. */
5415 return HANDLED_NORMALLY;
5416 if (start != pos)
5418 if (STRINGP (it->string))
5419 pos_byte = string_char_to_byte (it->string, start);
5420 else
5421 pos_byte = CHAR_TO_BYTE (start);
5423 it->cmp_it.id = get_composition_id (start, pos_byte, end - start,
5424 prop, string);
5426 if (it->cmp_it.id >= 0)
5428 it->cmp_it.ch = -1;
5429 it->cmp_it.nchars = COMPOSITION_LENGTH (prop);
5430 it->cmp_it.nglyphs = -1;
5434 return HANDLED_NORMALLY;
5439 /***********************************************************************
5440 Overlay strings
5441 ***********************************************************************/
5443 /* The following structure is used to record overlay strings for
5444 later sorting in load_overlay_strings. */
5446 struct overlay_entry
5448 Lisp_Object overlay;
5449 Lisp_Object string;
5450 EMACS_INT priority;
5451 int after_string_p;
5455 /* Set up iterator IT from overlay strings at its current position.
5456 Called from handle_stop. */
5458 static enum prop_handled
5459 handle_overlay_change (struct it *it)
5461 if (!STRINGP (it->string) && get_overlay_strings (it, 0))
5462 return HANDLED_RECOMPUTE_PROPS;
5463 else
5464 return HANDLED_NORMALLY;
5468 /* Set up the next overlay string for delivery by IT, if there is an
5469 overlay string to deliver. Called by set_iterator_to_next when the
5470 end of the current overlay string is reached. If there are more
5471 overlay strings to display, IT->string and
5472 IT->current.overlay_string_index are set appropriately here.
5473 Otherwise IT->string is set to nil. */
5475 static void
5476 next_overlay_string (struct it *it)
5478 ++it->current.overlay_string_index;
5479 if (it->current.overlay_string_index == it->n_overlay_strings)
5481 /* No more overlay strings. Restore IT's settings to what
5482 they were before overlay strings were processed, and
5483 continue to deliver from current_buffer. */
5485 it->ellipsis_p = (it->stack[it->sp - 1].display_ellipsis_p != 0);
5486 pop_it (it);
5487 eassert (it->sp > 0
5488 || (NILP (it->string)
5489 && it->method == GET_FROM_BUFFER
5490 && it->stop_charpos >= BEGV
5491 && it->stop_charpos <= it->end_charpos));
5492 it->current.overlay_string_index = -1;
5493 it->n_overlay_strings = 0;
5494 it->overlay_strings_charpos = -1;
5495 /* If there's an empty display string on the stack, pop the
5496 stack, to resync the bidi iterator with IT's position. Such
5497 empty strings are pushed onto the stack in
5498 get_overlay_strings_1. */
5499 if (it->sp > 0 && STRINGP (it->string) && !SCHARS (it->string))
5500 pop_it (it);
5502 /* If we're at the end of the buffer, record that we have
5503 processed the overlay strings there already, so that
5504 next_element_from_buffer doesn't try it again. */
5505 if (NILP (it->string) && IT_CHARPOS (*it) >= it->end_charpos)
5506 it->overlay_strings_at_end_processed_p = true;
5508 else
5510 /* There are more overlay strings to process. If
5511 IT->current.overlay_string_index has advanced to a position
5512 where we must load IT->overlay_strings with more strings, do
5513 it. We must load at the IT->overlay_strings_charpos where
5514 IT->n_overlay_strings was originally computed; when invisible
5515 text is present, this might not be IT_CHARPOS (Bug#7016). */
5516 int i = it->current.overlay_string_index % OVERLAY_STRING_CHUNK_SIZE;
5518 if (it->current.overlay_string_index && i == 0)
5519 load_overlay_strings (it, it->overlay_strings_charpos);
5521 /* Initialize IT to deliver display elements from the overlay
5522 string. */
5523 it->string = it->overlay_strings[i];
5524 it->multibyte_p = STRING_MULTIBYTE (it->string);
5525 SET_TEXT_POS (it->current.string_pos, 0, 0);
5526 it->method = GET_FROM_STRING;
5527 it->stop_charpos = 0;
5528 it->end_charpos = SCHARS (it->string);
5529 if (it->cmp_it.stop_pos >= 0)
5530 it->cmp_it.stop_pos = 0;
5531 it->prev_stop = 0;
5532 it->base_level_stop = 0;
5534 /* Set up the bidi iterator for this overlay string. */
5535 if (it->bidi_p)
5537 it->bidi_it.string.lstring = it->string;
5538 it->bidi_it.string.s = NULL;
5539 it->bidi_it.string.schars = SCHARS (it->string);
5540 it->bidi_it.string.bufpos = it->overlay_strings_charpos;
5541 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
5542 it->bidi_it.string.unibyte = !it->multibyte_p;
5543 it->bidi_it.w = it->w;
5544 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
5548 CHECK_IT (it);
5552 /* Compare two overlay_entry structures E1 and E2. Used as a
5553 comparison function for qsort in load_overlay_strings. Overlay
5554 strings for the same position are sorted so that
5556 1. All after-strings come in front of before-strings, except
5557 when they come from the same overlay.
5559 2. Within after-strings, strings are sorted so that overlay strings
5560 from overlays with higher priorities come first.
5562 2. Within before-strings, strings are sorted so that overlay
5563 strings from overlays with higher priorities come last.
5565 Value is analogous to strcmp. */
5568 static int
5569 compare_overlay_entries (const void *e1, const void *e2)
5571 struct overlay_entry const *entry1 = e1;
5572 struct overlay_entry const *entry2 = e2;
5573 int result;
5575 if (entry1->after_string_p != entry2->after_string_p)
5577 /* Let after-strings appear in front of before-strings if
5578 they come from different overlays. */
5579 if (EQ (entry1->overlay, entry2->overlay))
5580 result = entry1->after_string_p ? 1 : -1;
5581 else
5582 result = entry1->after_string_p ? -1 : 1;
5584 else if (entry1->priority != entry2->priority)
5586 if (entry1->after_string_p)
5587 /* After-strings sorted in order of decreasing priority. */
5588 result = entry2->priority < entry1->priority ? -1 : 1;
5589 else
5590 /* Before-strings sorted in order of increasing priority. */
5591 result = entry1->priority < entry2->priority ? -1 : 1;
5593 else
5594 result = 0;
5596 return result;
5600 /* Load the vector IT->overlay_strings with overlay strings from IT's
5601 current buffer position, or from CHARPOS if that is > 0. Set
5602 IT->n_overlays to the total number of overlay strings found.
5604 Overlay strings are processed OVERLAY_STRING_CHUNK_SIZE strings at
5605 a time. On entry into load_overlay_strings,
5606 IT->current.overlay_string_index gives the number of overlay
5607 strings that have already been loaded by previous calls to this
5608 function.
5610 IT->add_overlay_start contains an additional overlay start
5611 position to consider for taking overlay strings from, if non-zero.
5612 This position comes into play when the overlay has an `invisible'
5613 property, and both before and after-strings. When we've skipped to
5614 the end of the overlay, because of its `invisible' property, we
5615 nevertheless want its before-string to appear.
5616 IT->add_overlay_start will contain the overlay start position
5617 in this case.
5619 Overlay strings are sorted so that after-string strings come in
5620 front of before-string strings. Within before and after-strings,
5621 strings are sorted by overlay priority. See also function
5622 compare_overlay_entries. */
5624 static void
5625 load_overlay_strings (struct it *it, ptrdiff_t charpos)
5627 Lisp_Object overlay, window, str, invisible;
5628 struct Lisp_Overlay *ov;
5629 ptrdiff_t start, end;
5630 ptrdiff_t size = 20;
5631 ptrdiff_t n = 0, i, j;
5632 int invis_p;
5633 struct overlay_entry *entries = alloca (size * sizeof *entries);
5634 USE_SAFE_ALLOCA;
5636 if (charpos <= 0)
5637 charpos = IT_CHARPOS (*it);
5639 /* Append the overlay string STRING of overlay OVERLAY to vector
5640 `entries' which has size `size' and currently contains `n'
5641 elements. AFTER_P non-zero means STRING is an after-string of
5642 OVERLAY. */
5643 #define RECORD_OVERLAY_STRING(OVERLAY, STRING, AFTER_P) \
5644 do \
5646 Lisp_Object priority; \
5648 if (n == size) \
5650 struct overlay_entry *old = entries; \
5651 SAFE_NALLOCA (entries, 2, size); \
5652 memcpy (entries, old, size * sizeof *entries); \
5653 size *= 2; \
5656 entries[n].string = (STRING); \
5657 entries[n].overlay = (OVERLAY); \
5658 priority = Foverlay_get ((OVERLAY), Qpriority); \
5659 entries[n].priority = INTEGERP (priority) ? XINT (priority) : 0; \
5660 entries[n].after_string_p = (AFTER_P); \
5661 ++n; \
5663 while (0)
5665 /* Process overlay before the overlay center. */
5666 for (ov = current_buffer->overlays_before; ov; ov = ov->next)
5668 XSETMISC (overlay, ov);
5669 eassert (OVERLAYP (overlay));
5670 start = OVERLAY_POSITION (OVERLAY_START (overlay));
5671 end = OVERLAY_POSITION (OVERLAY_END (overlay));
5673 if (end < charpos)
5674 break;
5676 /* Skip this overlay if it doesn't start or end at IT's current
5677 position. */
5678 if (end != charpos && start != charpos)
5679 continue;
5681 /* Skip this overlay if it doesn't apply to IT->w. */
5682 window = Foverlay_get (overlay, Qwindow);
5683 if (WINDOWP (window) && XWINDOW (window) != it->w)
5684 continue;
5686 /* If the text ``under'' the overlay is invisible, both before-
5687 and after-strings from this overlay are visible; start and
5688 end position are indistinguishable. */
5689 invisible = Foverlay_get (overlay, Qinvisible);
5690 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
5692 /* If overlay has a non-empty before-string, record it. */
5693 if ((start == charpos || (end == charpos && invis_p))
5694 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
5695 && SCHARS (str))
5696 RECORD_OVERLAY_STRING (overlay, str, 0);
5698 /* If overlay has a non-empty after-string, record it. */
5699 if ((end == charpos || (start == charpos && invis_p))
5700 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
5701 && SCHARS (str))
5702 RECORD_OVERLAY_STRING (overlay, str, 1);
5705 /* Process overlays after the overlay center. */
5706 for (ov = current_buffer->overlays_after; ov; ov = ov->next)
5708 XSETMISC (overlay, ov);
5709 eassert (OVERLAYP (overlay));
5710 start = OVERLAY_POSITION (OVERLAY_START (overlay));
5711 end = OVERLAY_POSITION (OVERLAY_END (overlay));
5713 if (start > charpos)
5714 break;
5716 /* Skip this overlay if it doesn't start or end at IT's current
5717 position. */
5718 if (end != charpos && start != charpos)
5719 continue;
5721 /* Skip this overlay if it doesn't apply to IT->w. */
5722 window = Foverlay_get (overlay, Qwindow);
5723 if (WINDOWP (window) && XWINDOW (window) != it->w)
5724 continue;
5726 /* If the text ``under'' the overlay is invisible, it has a zero
5727 dimension, and both before- and after-strings apply. */
5728 invisible = Foverlay_get (overlay, Qinvisible);
5729 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
5731 /* If overlay has a non-empty before-string, record it. */
5732 if ((start == charpos || (end == charpos && invis_p))
5733 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
5734 && SCHARS (str))
5735 RECORD_OVERLAY_STRING (overlay, str, 0);
5737 /* If overlay has a non-empty after-string, record it. */
5738 if ((end == charpos || (start == charpos && invis_p))
5739 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
5740 && SCHARS (str))
5741 RECORD_OVERLAY_STRING (overlay, str, 1);
5744 #undef RECORD_OVERLAY_STRING
5746 /* Sort entries. */
5747 if (n > 1)
5748 qsort (entries, n, sizeof *entries, compare_overlay_entries);
5750 /* Record number of overlay strings, and where we computed it. */
5751 it->n_overlay_strings = n;
5752 it->overlay_strings_charpos = charpos;
5754 /* IT->current.overlay_string_index is the number of overlay strings
5755 that have already been consumed by IT. Copy some of the
5756 remaining overlay strings to IT->overlay_strings. */
5757 i = 0;
5758 j = it->current.overlay_string_index;
5759 while (i < OVERLAY_STRING_CHUNK_SIZE && j < n)
5761 it->overlay_strings[i] = entries[j].string;
5762 it->string_overlays[i++] = entries[j++].overlay;
5765 CHECK_IT (it);
5766 SAFE_FREE ();
5770 /* Get the first chunk of overlay strings at IT's current buffer
5771 position, or at CHARPOS if that is > 0. Value is non-zero if at
5772 least one overlay string was found. */
5774 static int
5775 get_overlay_strings_1 (struct it *it, ptrdiff_t charpos, int compute_stop_p)
5777 /* Get the first OVERLAY_STRING_CHUNK_SIZE overlay strings to
5778 process. This fills IT->overlay_strings with strings, and sets
5779 IT->n_overlay_strings to the total number of strings to process.
5780 IT->pos.overlay_string_index has to be set temporarily to zero
5781 because load_overlay_strings needs this; it must be set to -1
5782 when no overlay strings are found because a zero value would
5783 indicate a position in the first overlay string. */
5784 it->current.overlay_string_index = 0;
5785 load_overlay_strings (it, charpos);
5787 /* If we found overlay strings, set up IT to deliver display
5788 elements from the first one. Otherwise set up IT to deliver
5789 from current_buffer. */
5790 if (it->n_overlay_strings)
5792 /* Make sure we know settings in current_buffer, so that we can
5793 restore meaningful values when we're done with the overlay
5794 strings. */
5795 if (compute_stop_p)
5796 compute_stop_pos (it);
5797 eassert (it->face_id >= 0);
5799 /* Save IT's settings. They are restored after all overlay
5800 strings have been processed. */
5801 eassert (!compute_stop_p || it->sp == 0);
5803 /* When called from handle_stop, there might be an empty display
5804 string loaded. In that case, don't bother saving it. But
5805 don't use this optimization with the bidi iterator, since we
5806 need the corresponding pop_it call to resync the bidi
5807 iterator's position with IT's position, after we are done
5808 with the overlay strings. (The corresponding call to pop_it
5809 in case of an empty display string is in
5810 next_overlay_string.) */
5811 if (!(!it->bidi_p
5812 && STRINGP (it->string) && !SCHARS (it->string)))
5813 push_it (it, NULL);
5815 /* Set up IT to deliver display elements from the first overlay
5816 string. */
5817 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
5818 it->string = it->overlay_strings[0];
5819 it->from_overlay = Qnil;
5820 it->stop_charpos = 0;
5821 eassert (STRINGP (it->string));
5822 it->end_charpos = SCHARS (it->string);
5823 it->prev_stop = 0;
5824 it->base_level_stop = 0;
5825 it->multibyte_p = STRING_MULTIBYTE (it->string);
5826 it->method = GET_FROM_STRING;
5827 it->from_disp_prop_p = 0;
5829 /* Force paragraph direction to be that of the parent
5830 buffer. */
5831 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
5832 it->paragraph_embedding = it->bidi_it.paragraph_dir;
5833 else
5834 it->paragraph_embedding = L2R;
5836 /* Set up the bidi iterator for this overlay string. */
5837 if (it->bidi_p)
5839 ptrdiff_t pos = (charpos > 0 ? charpos : IT_CHARPOS (*it));
5841 it->bidi_it.string.lstring = it->string;
5842 it->bidi_it.string.s = NULL;
5843 it->bidi_it.string.schars = SCHARS (it->string);
5844 it->bidi_it.string.bufpos = pos;
5845 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
5846 it->bidi_it.string.unibyte = !it->multibyte_p;
5847 it->bidi_it.w = it->w;
5848 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
5850 return 1;
5853 it->current.overlay_string_index = -1;
5854 return 0;
5857 static int
5858 get_overlay_strings (struct it *it, ptrdiff_t charpos)
5860 it->string = Qnil;
5861 it->method = GET_FROM_BUFFER;
5863 (void) get_overlay_strings_1 (it, charpos, 1);
5865 CHECK_IT (it);
5867 /* Value is non-zero if we found at least one overlay string. */
5868 return STRINGP (it->string);
5873 /***********************************************************************
5874 Saving and restoring state
5875 ***********************************************************************/
5877 /* Save current settings of IT on IT->stack. Called, for example,
5878 before setting up IT for an overlay string, to be able to restore
5879 IT's settings to what they were after the overlay string has been
5880 processed. If POSITION is non-NULL, it is the position to save on
5881 the stack instead of IT->position. */
5883 static void
5884 push_it (struct it *it, struct text_pos *position)
5886 struct iterator_stack_entry *p;
5888 eassert (it->sp < IT_STACK_SIZE);
5889 p = it->stack + it->sp;
5891 p->stop_charpos = it->stop_charpos;
5892 p->prev_stop = it->prev_stop;
5893 p->base_level_stop = it->base_level_stop;
5894 p->cmp_it = it->cmp_it;
5895 eassert (it->face_id >= 0);
5896 p->face_id = it->face_id;
5897 p->string = it->string;
5898 p->method = it->method;
5899 p->from_overlay = it->from_overlay;
5900 switch (p->method)
5902 case GET_FROM_IMAGE:
5903 p->u.image.object = it->object;
5904 p->u.image.image_id = it->image_id;
5905 p->u.image.slice = it->slice;
5906 break;
5907 case GET_FROM_STRETCH:
5908 p->u.stretch.object = it->object;
5909 break;
5911 p->position = position ? *position : it->position;
5912 p->current = it->current;
5913 p->end_charpos = it->end_charpos;
5914 p->string_nchars = it->string_nchars;
5915 p->area = it->area;
5916 p->multibyte_p = it->multibyte_p;
5917 p->avoid_cursor_p = it->avoid_cursor_p;
5918 p->space_width = it->space_width;
5919 p->font_height = it->font_height;
5920 p->voffset = it->voffset;
5921 p->string_from_display_prop_p = it->string_from_display_prop_p;
5922 p->string_from_prefix_prop_p = it->string_from_prefix_prop_p;
5923 p->display_ellipsis_p = 0;
5924 p->line_wrap = it->line_wrap;
5925 p->bidi_p = it->bidi_p;
5926 p->paragraph_embedding = it->paragraph_embedding;
5927 p->from_disp_prop_p = it->from_disp_prop_p;
5928 ++it->sp;
5930 /* Save the state of the bidi iterator as well. */
5931 if (it->bidi_p)
5932 bidi_push_it (&it->bidi_it);
5935 static void
5936 iterate_out_of_display_property (struct it *it)
5938 int buffer_p = !STRINGP (it->string);
5939 ptrdiff_t eob = (buffer_p ? ZV : it->end_charpos);
5940 ptrdiff_t bob = (buffer_p ? BEGV : 0);
5942 eassert (eob >= CHARPOS (it->position) && CHARPOS (it->position) >= bob);
5944 /* Maybe initialize paragraph direction. If we are at the beginning
5945 of a new paragraph, next_element_from_buffer may not have a
5946 chance to do that. */
5947 if (it->bidi_it.first_elt && it->bidi_it.charpos < eob)
5948 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
5949 /* prev_stop can be zero, so check against BEGV as well. */
5950 while (it->bidi_it.charpos >= bob
5951 && it->prev_stop <= it->bidi_it.charpos
5952 && it->bidi_it.charpos < CHARPOS (it->position)
5953 && it->bidi_it.charpos < eob)
5954 bidi_move_to_visually_next (&it->bidi_it);
5955 /* Record the stop_pos we just crossed, for when we cross it
5956 back, maybe. */
5957 if (it->bidi_it.charpos > CHARPOS (it->position))
5958 it->prev_stop = CHARPOS (it->position);
5959 /* If we ended up not where pop_it put us, resync IT's
5960 positional members with the bidi iterator. */
5961 if (it->bidi_it.charpos != CHARPOS (it->position))
5962 SET_TEXT_POS (it->position, it->bidi_it.charpos, it->bidi_it.bytepos);
5963 if (buffer_p)
5964 it->current.pos = it->position;
5965 else
5966 it->current.string_pos = it->position;
5969 /* Restore IT's settings from IT->stack. Called, for example, when no
5970 more overlay strings must be processed, and we return to delivering
5971 display elements from a buffer, or when the end of a string from a
5972 `display' property is reached and we return to delivering display
5973 elements from an overlay string, or from a buffer. */
5975 static void
5976 pop_it (struct it *it)
5978 struct iterator_stack_entry *p;
5979 int from_display_prop = it->from_disp_prop_p;
5981 eassert (it->sp > 0);
5982 --it->sp;
5983 p = it->stack + it->sp;
5984 it->stop_charpos = p->stop_charpos;
5985 it->prev_stop = p->prev_stop;
5986 it->base_level_stop = p->base_level_stop;
5987 it->cmp_it = p->cmp_it;
5988 it->face_id = p->face_id;
5989 it->current = p->current;
5990 it->position = p->position;
5991 it->string = p->string;
5992 it->from_overlay = p->from_overlay;
5993 if (NILP (it->string))
5994 SET_TEXT_POS (it->current.string_pos, -1, -1);
5995 it->method = p->method;
5996 switch (it->method)
5998 case GET_FROM_IMAGE:
5999 it->image_id = p->u.image.image_id;
6000 it->object = p->u.image.object;
6001 it->slice = p->u.image.slice;
6002 break;
6003 case GET_FROM_STRETCH:
6004 it->object = p->u.stretch.object;
6005 break;
6006 case GET_FROM_BUFFER:
6007 it->object = it->w->contents;
6008 break;
6009 case GET_FROM_STRING:
6011 struct face *face = FACE_FROM_ID (it->f, it->face_id);
6013 /* Restore the face_box_p flag, since it could have been
6014 overwritten by the face of the object that we just finished
6015 displaying. */
6016 if (face)
6017 it->face_box_p = face->box != FACE_NO_BOX;
6018 it->object = it->string;
6020 break;
6021 case GET_FROM_DISPLAY_VECTOR:
6022 if (it->s)
6023 it->method = GET_FROM_C_STRING;
6024 else if (STRINGP (it->string))
6025 it->method = GET_FROM_STRING;
6026 else
6028 it->method = GET_FROM_BUFFER;
6029 it->object = it->w->contents;
6032 it->end_charpos = p->end_charpos;
6033 it->string_nchars = p->string_nchars;
6034 it->area = p->area;
6035 it->multibyte_p = p->multibyte_p;
6036 it->avoid_cursor_p = p->avoid_cursor_p;
6037 it->space_width = p->space_width;
6038 it->font_height = p->font_height;
6039 it->voffset = p->voffset;
6040 it->string_from_display_prop_p = p->string_from_display_prop_p;
6041 it->string_from_prefix_prop_p = p->string_from_prefix_prop_p;
6042 it->line_wrap = p->line_wrap;
6043 it->bidi_p = p->bidi_p;
6044 it->paragraph_embedding = p->paragraph_embedding;
6045 it->from_disp_prop_p = p->from_disp_prop_p;
6046 if (it->bidi_p)
6048 bidi_pop_it (&it->bidi_it);
6049 /* Bidi-iterate until we get out of the portion of text, if any,
6050 covered by a `display' text property or by an overlay with
6051 `display' property. (We cannot just jump there, because the
6052 internal coherency of the bidi iterator state can not be
6053 preserved across such jumps.) We also must determine the
6054 paragraph base direction if the overlay we just processed is
6055 at the beginning of a new paragraph. */
6056 if (from_display_prop
6057 && (it->method == GET_FROM_BUFFER || it->method == GET_FROM_STRING))
6058 iterate_out_of_display_property (it);
6060 eassert ((BUFFERP (it->object)
6061 && IT_CHARPOS (*it) == it->bidi_it.charpos
6062 && IT_BYTEPOS (*it) == it->bidi_it.bytepos)
6063 || (STRINGP (it->object)
6064 && IT_STRING_CHARPOS (*it) == it->bidi_it.charpos
6065 && IT_STRING_BYTEPOS (*it) == it->bidi_it.bytepos)
6066 || (CONSP (it->object) && it->method == GET_FROM_STRETCH));
6072 /***********************************************************************
6073 Moving over lines
6074 ***********************************************************************/
6076 /* Set IT's current position to the previous line start. */
6078 static void
6079 back_to_previous_line_start (struct it *it)
6081 ptrdiff_t cp = IT_CHARPOS (*it), bp = IT_BYTEPOS (*it);
6083 DEC_BOTH (cp, bp);
6084 IT_CHARPOS (*it) = find_newline_no_quit (cp, bp, -1, &IT_BYTEPOS (*it));
6088 /* Move IT to the next line start.
6090 Value is non-zero if a newline was found. Set *SKIPPED_P to 1 if
6091 we skipped over part of the text (as opposed to moving the iterator
6092 continuously over the text). Otherwise, don't change the value
6093 of *SKIPPED_P.
6095 If BIDI_IT_PREV is non-NULL, store into it the state of the bidi
6096 iterator on the newline, if it was found.
6098 Newlines may come from buffer text, overlay strings, or strings
6099 displayed via the `display' property. That's the reason we can't
6100 simply use find_newline_no_quit.
6102 Note that this function may not skip over invisible text that is so
6103 because of text properties and immediately follows a newline. If
6104 it would, function reseat_at_next_visible_line_start, when called
6105 from set_iterator_to_next, would effectively make invisible
6106 characters following a newline part of the wrong glyph row, which
6107 leads to wrong cursor motion. */
6109 static int
6110 forward_to_next_line_start (struct it *it, int *skipped_p,
6111 struct bidi_it *bidi_it_prev)
6113 ptrdiff_t old_selective;
6114 int newline_found_p, n;
6115 const int MAX_NEWLINE_DISTANCE = 500;
6117 /* If already on a newline, just consume it to avoid unintended
6118 skipping over invisible text below. */
6119 if (it->what == IT_CHARACTER
6120 && it->c == '\n'
6121 && CHARPOS (it->position) == IT_CHARPOS (*it))
6123 if (it->bidi_p && bidi_it_prev)
6124 *bidi_it_prev = it->bidi_it;
6125 set_iterator_to_next (it, 0);
6126 it->c = 0;
6127 return 1;
6130 /* Don't handle selective display in the following. It's (a)
6131 unnecessary because it's done by the caller, and (b) leads to an
6132 infinite recursion because next_element_from_ellipsis indirectly
6133 calls this function. */
6134 old_selective = it->selective;
6135 it->selective = 0;
6137 /* Scan for a newline within MAX_NEWLINE_DISTANCE display elements
6138 from buffer text. */
6139 for (n = newline_found_p = 0;
6140 !newline_found_p && n < MAX_NEWLINE_DISTANCE;
6141 n += STRINGP (it->string) ? 0 : 1)
6143 if (!get_next_display_element (it))
6144 return 0;
6145 newline_found_p = it->what == IT_CHARACTER && it->c == '\n';
6146 if (newline_found_p && it->bidi_p && bidi_it_prev)
6147 *bidi_it_prev = it->bidi_it;
6148 set_iterator_to_next (it, 0);
6151 /* If we didn't find a newline near enough, see if we can use a
6152 short-cut. */
6153 if (!newline_found_p)
6155 ptrdiff_t bytepos, start = IT_CHARPOS (*it);
6156 ptrdiff_t limit = find_newline_no_quit (start, IT_BYTEPOS (*it),
6157 1, &bytepos);
6158 Lisp_Object pos;
6160 eassert (!STRINGP (it->string));
6162 /* If there isn't any `display' property in sight, and no
6163 overlays, we can just use the position of the newline in
6164 buffer text. */
6165 if (it->stop_charpos >= limit
6166 || ((pos = Fnext_single_property_change (make_number (start),
6167 Qdisplay, Qnil,
6168 make_number (limit)),
6169 NILP (pos))
6170 && next_overlay_change (start) == ZV))
6172 if (!it->bidi_p)
6174 IT_CHARPOS (*it) = limit;
6175 IT_BYTEPOS (*it) = bytepos;
6177 else
6179 struct bidi_it bprev;
6181 /* Help bidi.c avoid expensive searches for display
6182 properties and overlays, by telling it that there are
6183 none up to `limit'. */
6184 if (it->bidi_it.disp_pos < limit)
6186 it->bidi_it.disp_pos = limit;
6187 it->bidi_it.disp_prop = 0;
6189 do {
6190 bprev = it->bidi_it;
6191 bidi_move_to_visually_next (&it->bidi_it);
6192 } while (it->bidi_it.charpos != limit);
6193 IT_CHARPOS (*it) = limit;
6194 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6195 if (bidi_it_prev)
6196 *bidi_it_prev = bprev;
6198 *skipped_p = newline_found_p = true;
6200 else
6202 while (get_next_display_element (it)
6203 && !newline_found_p)
6205 newline_found_p = ITERATOR_AT_END_OF_LINE_P (it);
6206 if (newline_found_p && it->bidi_p && bidi_it_prev)
6207 *bidi_it_prev = it->bidi_it;
6208 set_iterator_to_next (it, 0);
6213 it->selective = old_selective;
6214 return newline_found_p;
6218 /* Set IT's current position to the previous visible line start. Skip
6219 invisible text that is so either due to text properties or due to
6220 selective display. Caution: this does not change IT->current_x and
6221 IT->hpos. */
6223 static void
6224 back_to_previous_visible_line_start (struct it *it)
6226 while (IT_CHARPOS (*it) > BEGV)
6228 back_to_previous_line_start (it);
6230 if (IT_CHARPOS (*it) <= BEGV)
6231 break;
6233 /* If selective > 0, then lines indented more than its value are
6234 invisible. */
6235 if (it->selective > 0
6236 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
6237 it->selective))
6238 continue;
6240 /* Check the newline before point for invisibility. */
6242 Lisp_Object prop;
6243 prop = Fget_char_property (make_number (IT_CHARPOS (*it) - 1),
6244 Qinvisible, it->window);
6245 if (TEXT_PROP_MEANS_INVISIBLE (prop))
6246 continue;
6249 if (IT_CHARPOS (*it) <= BEGV)
6250 break;
6253 struct it it2;
6254 void *it2data = NULL;
6255 ptrdiff_t pos;
6256 ptrdiff_t beg, end;
6257 Lisp_Object val, overlay;
6259 SAVE_IT (it2, *it, it2data);
6261 /* If newline is part of a composition, continue from start of composition */
6262 if (find_composition (IT_CHARPOS (*it), -1, &beg, &end, &val, Qnil)
6263 && beg < IT_CHARPOS (*it))
6264 goto replaced;
6266 /* If newline is replaced by a display property, find start of overlay
6267 or interval and continue search from that point. */
6268 pos = --IT_CHARPOS (it2);
6269 --IT_BYTEPOS (it2);
6270 it2.sp = 0;
6271 bidi_unshelve_cache (NULL, 0);
6272 it2.string_from_display_prop_p = 0;
6273 it2.from_disp_prop_p = 0;
6274 if (handle_display_prop (&it2) == HANDLED_RETURN
6275 && !NILP (val = get_char_property_and_overlay
6276 (make_number (pos), Qdisplay, Qnil, &overlay))
6277 && (OVERLAYP (overlay)
6278 ? (beg = OVERLAY_POSITION (OVERLAY_START (overlay)))
6279 : get_property_and_range (pos, Qdisplay, &val, &beg, &end, Qnil)))
6281 RESTORE_IT (it, it, it2data);
6282 goto replaced;
6285 /* Newline is not replaced by anything -- so we are done. */
6286 RESTORE_IT (it, it, it2data);
6287 break;
6289 replaced:
6290 if (beg < BEGV)
6291 beg = BEGV;
6292 IT_CHARPOS (*it) = beg;
6293 IT_BYTEPOS (*it) = buf_charpos_to_bytepos (current_buffer, beg);
6297 it->continuation_lines_width = 0;
6299 eassert (IT_CHARPOS (*it) >= BEGV);
6300 eassert (IT_CHARPOS (*it) == BEGV
6301 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
6302 CHECK_IT (it);
6306 /* Reseat iterator IT at the previous visible line start. Skip
6307 invisible text that is so either due to text properties or due to
6308 selective display. At the end, update IT's overlay information,
6309 face information etc. */
6311 void
6312 reseat_at_previous_visible_line_start (struct it *it)
6314 back_to_previous_visible_line_start (it);
6315 reseat (it, it->current.pos, 1);
6316 CHECK_IT (it);
6320 /* Reseat iterator IT on the next visible line start in the current
6321 buffer. ON_NEWLINE_P non-zero means position IT on the newline
6322 preceding the line start. Skip over invisible text that is so
6323 because of selective display. Compute faces, overlays etc at the
6324 new position. Note that this function does not skip over text that
6325 is invisible because of text properties. */
6327 static void
6328 reseat_at_next_visible_line_start (struct it *it, int on_newline_p)
6330 int newline_found_p, skipped_p = 0;
6331 struct bidi_it bidi_it_prev;
6333 newline_found_p = forward_to_next_line_start (it, &skipped_p, &bidi_it_prev);
6335 /* Skip over lines that are invisible because they are indented
6336 more than the value of IT->selective. */
6337 if (it->selective > 0)
6338 while (IT_CHARPOS (*it) < ZV
6339 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
6340 it->selective))
6342 eassert (IT_BYTEPOS (*it) == BEGV
6343 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
6344 newline_found_p =
6345 forward_to_next_line_start (it, &skipped_p, &bidi_it_prev);
6348 /* Position on the newline if that's what's requested. */
6349 if (on_newline_p && newline_found_p)
6351 if (STRINGP (it->string))
6353 if (IT_STRING_CHARPOS (*it) > 0)
6355 if (!it->bidi_p)
6357 --IT_STRING_CHARPOS (*it);
6358 --IT_STRING_BYTEPOS (*it);
6360 else
6362 /* We need to restore the bidi iterator to the state
6363 it had on the newline, and resync the IT's
6364 position with that. */
6365 it->bidi_it = bidi_it_prev;
6366 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
6367 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
6371 else if (IT_CHARPOS (*it) > BEGV)
6373 if (!it->bidi_p)
6375 --IT_CHARPOS (*it);
6376 --IT_BYTEPOS (*it);
6378 else
6380 /* We need to restore the bidi iterator to the state it
6381 had on the newline and resync IT with that. */
6382 it->bidi_it = bidi_it_prev;
6383 IT_CHARPOS (*it) = it->bidi_it.charpos;
6384 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6386 reseat (it, it->current.pos, 0);
6389 else if (skipped_p)
6390 reseat (it, it->current.pos, 0);
6392 CHECK_IT (it);
6397 /***********************************************************************
6398 Changing an iterator's position
6399 ***********************************************************************/
6401 /* Change IT's current position to POS in current_buffer. If FORCE_P
6402 is non-zero, always check for text properties at the new position.
6403 Otherwise, text properties are only looked up if POS >=
6404 IT->check_charpos of a property. */
6406 static void
6407 reseat (struct it *it, struct text_pos pos, int force_p)
6409 ptrdiff_t original_pos = IT_CHARPOS (*it);
6411 reseat_1 (it, pos, 0);
6413 /* Determine where to check text properties. Avoid doing it
6414 where possible because text property lookup is very expensive. */
6415 if (force_p
6416 || CHARPOS (pos) > it->stop_charpos
6417 || CHARPOS (pos) < original_pos)
6419 if (it->bidi_p)
6421 /* For bidi iteration, we need to prime prev_stop and
6422 base_level_stop with our best estimations. */
6423 /* Implementation note: Of course, POS is not necessarily a
6424 stop position, so assigning prev_pos to it is a lie; we
6425 should have called compute_stop_backwards. However, if
6426 the current buffer does not include any R2L characters,
6427 that call would be a waste of cycles, because the
6428 iterator will never move back, and thus never cross this
6429 "fake" stop position. So we delay that backward search
6430 until the time we really need it, in next_element_from_buffer. */
6431 if (CHARPOS (pos) != it->prev_stop)
6432 it->prev_stop = CHARPOS (pos);
6433 if (CHARPOS (pos) < it->base_level_stop)
6434 it->base_level_stop = 0; /* meaning it's unknown */
6435 handle_stop (it);
6437 else
6439 handle_stop (it);
6440 it->prev_stop = it->base_level_stop = 0;
6445 CHECK_IT (it);
6449 /* Change IT's buffer position to POS. SET_STOP_P non-zero means set
6450 IT->stop_pos to POS, also. */
6452 static void
6453 reseat_1 (struct it *it, struct text_pos pos, int set_stop_p)
6455 /* Don't call this function when scanning a C string. */
6456 eassert (it->s == NULL);
6458 /* POS must be a reasonable value. */
6459 eassert (CHARPOS (pos) >= BEGV && CHARPOS (pos) <= ZV);
6461 it->current.pos = it->position = pos;
6462 it->end_charpos = ZV;
6463 it->dpvec = NULL;
6464 it->current.dpvec_index = -1;
6465 it->current.overlay_string_index = -1;
6466 IT_STRING_CHARPOS (*it) = -1;
6467 IT_STRING_BYTEPOS (*it) = -1;
6468 it->string = Qnil;
6469 it->method = GET_FROM_BUFFER;
6470 it->object = it->w->contents;
6471 it->area = TEXT_AREA;
6472 it->multibyte_p = !NILP (BVAR (current_buffer, enable_multibyte_characters));
6473 it->sp = 0;
6474 it->string_from_display_prop_p = 0;
6475 it->string_from_prefix_prop_p = 0;
6477 it->from_disp_prop_p = 0;
6478 it->face_before_selective_p = 0;
6479 if (it->bidi_p)
6481 bidi_init_it (IT_CHARPOS (*it), IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
6482 &it->bidi_it);
6483 bidi_unshelve_cache (NULL, 0);
6484 it->bidi_it.paragraph_dir = NEUTRAL_DIR;
6485 it->bidi_it.string.s = NULL;
6486 it->bidi_it.string.lstring = Qnil;
6487 it->bidi_it.string.bufpos = 0;
6488 it->bidi_it.string.from_disp_str = 0;
6489 it->bidi_it.string.unibyte = 0;
6490 it->bidi_it.w = it->w;
6493 if (set_stop_p)
6495 it->stop_charpos = CHARPOS (pos);
6496 it->base_level_stop = CHARPOS (pos);
6498 /* This make the information stored in it->cmp_it invalidate. */
6499 it->cmp_it.id = -1;
6503 /* Set up IT for displaying a string, starting at CHARPOS in window W.
6504 If S is non-null, it is a C string to iterate over. Otherwise,
6505 STRING gives a Lisp string to iterate over.
6507 If PRECISION > 0, don't return more then PRECISION number of
6508 characters from the string.
6510 If FIELD_WIDTH > 0, return padding spaces until FIELD_WIDTH
6511 characters have been returned. FIELD_WIDTH < 0 means an infinite
6512 field width.
6514 MULTIBYTE = 0 means disable processing of multibyte characters,
6515 MULTIBYTE > 0 means enable it,
6516 MULTIBYTE < 0 means use IT->multibyte_p.
6518 IT must be initialized via a prior call to init_iterator before
6519 calling this function. */
6521 static void
6522 reseat_to_string (struct it *it, const char *s, Lisp_Object string,
6523 ptrdiff_t charpos, ptrdiff_t precision, int field_width,
6524 int multibyte)
6526 /* No text property checks performed by default, but see below. */
6527 it->stop_charpos = -1;
6529 /* Set iterator position and end position. */
6530 memset (&it->current, 0, sizeof it->current);
6531 it->current.overlay_string_index = -1;
6532 it->current.dpvec_index = -1;
6533 eassert (charpos >= 0);
6535 /* If STRING is specified, use its multibyteness, otherwise use the
6536 setting of MULTIBYTE, if specified. */
6537 if (multibyte >= 0)
6538 it->multibyte_p = multibyte > 0;
6540 /* Bidirectional reordering of strings is controlled by the default
6541 value of bidi-display-reordering. Don't try to reorder while
6542 loading loadup.el, as the necessary character property tables are
6543 not yet available. */
6544 it->bidi_p =
6545 NILP (Vpurify_flag)
6546 && !NILP (BVAR (&buffer_defaults, bidi_display_reordering));
6548 if (s == NULL)
6550 eassert (STRINGP (string));
6551 it->string = string;
6552 it->s = NULL;
6553 it->end_charpos = it->string_nchars = SCHARS (string);
6554 it->method = GET_FROM_STRING;
6555 it->current.string_pos = string_pos (charpos, string);
6557 if (it->bidi_p)
6559 it->bidi_it.string.lstring = string;
6560 it->bidi_it.string.s = NULL;
6561 it->bidi_it.string.schars = it->end_charpos;
6562 it->bidi_it.string.bufpos = 0;
6563 it->bidi_it.string.from_disp_str = 0;
6564 it->bidi_it.string.unibyte = !it->multibyte_p;
6565 it->bidi_it.w = it->w;
6566 bidi_init_it (charpos, IT_STRING_BYTEPOS (*it),
6567 FRAME_WINDOW_P (it->f), &it->bidi_it);
6570 else
6572 it->s = (const unsigned char *) s;
6573 it->string = Qnil;
6575 /* Note that we use IT->current.pos, not it->current.string_pos,
6576 for displaying C strings. */
6577 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
6578 if (it->multibyte_p)
6580 it->current.pos = c_string_pos (charpos, s, 1);
6581 it->end_charpos = it->string_nchars = number_of_chars (s, 1);
6583 else
6585 IT_CHARPOS (*it) = IT_BYTEPOS (*it) = charpos;
6586 it->end_charpos = it->string_nchars = strlen (s);
6589 if (it->bidi_p)
6591 it->bidi_it.string.lstring = Qnil;
6592 it->bidi_it.string.s = (const unsigned char *) s;
6593 it->bidi_it.string.schars = it->end_charpos;
6594 it->bidi_it.string.bufpos = 0;
6595 it->bidi_it.string.from_disp_str = 0;
6596 it->bidi_it.string.unibyte = !it->multibyte_p;
6597 it->bidi_it.w = it->w;
6598 bidi_init_it (charpos, IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
6599 &it->bidi_it);
6601 it->method = GET_FROM_C_STRING;
6604 /* PRECISION > 0 means don't return more than PRECISION characters
6605 from the string. */
6606 if (precision > 0 && it->end_charpos - charpos > precision)
6608 it->end_charpos = it->string_nchars = charpos + precision;
6609 if (it->bidi_p)
6610 it->bidi_it.string.schars = it->end_charpos;
6613 /* FIELD_WIDTH > 0 means pad with spaces until FIELD_WIDTH
6614 characters have been returned. FIELD_WIDTH == 0 means don't pad,
6615 FIELD_WIDTH < 0 means infinite field width. This is useful for
6616 padding with `-' at the end of a mode line. */
6617 if (field_width < 0)
6618 field_width = INFINITY;
6619 /* Implementation note: We deliberately don't enlarge
6620 it->bidi_it.string.schars here to fit it->end_charpos, because
6621 the bidi iterator cannot produce characters out of thin air. */
6622 if (field_width > it->end_charpos - charpos)
6623 it->end_charpos = charpos + field_width;
6625 /* Use the standard display table for displaying strings. */
6626 if (DISP_TABLE_P (Vstandard_display_table))
6627 it->dp = XCHAR_TABLE (Vstandard_display_table);
6629 it->stop_charpos = charpos;
6630 it->prev_stop = charpos;
6631 it->base_level_stop = 0;
6632 if (it->bidi_p)
6634 it->bidi_it.first_elt = 1;
6635 it->bidi_it.paragraph_dir = NEUTRAL_DIR;
6636 it->bidi_it.disp_pos = -1;
6638 if (s == NULL && it->multibyte_p)
6640 ptrdiff_t endpos = SCHARS (it->string);
6641 if (endpos > it->end_charpos)
6642 endpos = it->end_charpos;
6643 composition_compute_stop_pos (&it->cmp_it, charpos, -1, endpos,
6644 it->string);
6646 CHECK_IT (it);
6651 /***********************************************************************
6652 Iteration
6653 ***********************************************************************/
6655 /* Map enum it_method value to corresponding next_element_from_* function. */
6657 static int (* get_next_element[NUM_IT_METHODS]) (struct it *it) =
6659 next_element_from_buffer,
6660 next_element_from_display_vector,
6661 next_element_from_string,
6662 next_element_from_c_string,
6663 next_element_from_image,
6664 next_element_from_stretch
6667 #define GET_NEXT_DISPLAY_ELEMENT(it) (*get_next_element[(it)->method]) (it)
6670 /* Return 1 iff a character at CHARPOS (and BYTEPOS) is composed
6671 (possibly with the following characters). */
6673 #define CHAR_COMPOSED_P(IT,CHARPOS,BYTEPOS,END_CHARPOS) \
6674 ((IT)->cmp_it.id >= 0 \
6675 || ((IT)->cmp_it.stop_pos == (CHARPOS) \
6676 && composition_reseat_it (&(IT)->cmp_it, CHARPOS, BYTEPOS, \
6677 END_CHARPOS, (IT)->w, \
6678 FACE_FROM_ID ((IT)->f, (IT)->face_id), \
6679 (IT)->string)))
6682 /* Lookup the char-table Vglyphless_char_display for character C (-1
6683 if we want information for no-font case), and return the display
6684 method symbol. By side-effect, update it->what and
6685 it->glyphless_method. This function is called from
6686 get_next_display_element for each character element, and from
6687 x_produce_glyphs when no suitable font was found. */
6689 Lisp_Object
6690 lookup_glyphless_char_display (int c, struct it *it)
6692 Lisp_Object glyphless_method = Qnil;
6694 if (CHAR_TABLE_P (Vglyphless_char_display)
6695 && CHAR_TABLE_EXTRA_SLOTS (XCHAR_TABLE (Vglyphless_char_display)) >= 1)
6697 if (c >= 0)
6699 glyphless_method = CHAR_TABLE_REF (Vglyphless_char_display, c);
6700 if (CONSP (glyphless_method))
6701 glyphless_method = FRAME_WINDOW_P (it->f)
6702 ? XCAR (glyphless_method)
6703 : XCDR (glyphless_method);
6705 else
6706 glyphless_method = XCHAR_TABLE (Vglyphless_char_display)->extras[0];
6709 retry:
6710 if (NILP (glyphless_method))
6712 if (c >= 0)
6713 /* The default is to display the character by a proper font. */
6714 return Qnil;
6715 /* The default for the no-font case is to display an empty box. */
6716 glyphless_method = Qempty_box;
6718 if (EQ (glyphless_method, Qzero_width))
6720 if (c >= 0)
6721 return glyphless_method;
6722 /* This method can't be used for the no-font case. */
6723 glyphless_method = Qempty_box;
6725 if (EQ (glyphless_method, Qthin_space))
6726 it->glyphless_method = GLYPHLESS_DISPLAY_THIN_SPACE;
6727 else if (EQ (glyphless_method, Qempty_box))
6728 it->glyphless_method = GLYPHLESS_DISPLAY_EMPTY_BOX;
6729 else if (EQ (glyphless_method, Qhex_code))
6730 it->glyphless_method = GLYPHLESS_DISPLAY_HEX_CODE;
6731 else if (STRINGP (glyphless_method))
6732 it->glyphless_method = GLYPHLESS_DISPLAY_ACRONYM;
6733 else
6735 /* Invalid value. We use the default method. */
6736 glyphless_method = Qnil;
6737 goto retry;
6739 it->what = IT_GLYPHLESS;
6740 return glyphless_method;
6743 /* Merge escape glyph face and cache the result. */
6745 static struct frame *last_escape_glyph_frame = NULL;
6746 static int last_escape_glyph_face_id = (1 << FACE_ID_BITS);
6747 static int last_escape_glyph_merged_face_id = 0;
6749 static int
6750 merge_escape_glyph_face (struct it *it)
6752 int face_id;
6754 if (it->f == last_escape_glyph_frame
6755 && it->face_id == last_escape_glyph_face_id)
6756 face_id = last_escape_glyph_merged_face_id;
6757 else
6759 /* Merge the `escape-glyph' face into the current face. */
6760 face_id = merge_faces (it->f, Qescape_glyph, 0, it->face_id);
6761 last_escape_glyph_frame = it->f;
6762 last_escape_glyph_face_id = it->face_id;
6763 last_escape_glyph_merged_face_id = face_id;
6765 return face_id;
6768 /* Likewise for glyphless glyph face. */
6770 static struct frame *last_glyphless_glyph_frame = NULL;
6771 static int last_glyphless_glyph_face_id = (1 << FACE_ID_BITS);
6772 static int last_glyphless_glyph_merged_face_id = 0;
6775 merge_glyphless_glyph_face (struct it *it)
6777 int face_id;
6779 if (it->f == last_glyphless_glyph_frame
6780 && it->face_id == last_glyphless_glyph_face_id)
6781 face_id = last_glyphless_glyph_merged_face_id;
6782 else
6784 /* Merge the `glyphless-char' face into the current face. */
6785 face_id = merge_faces (it->f, Qglyphless_char, 0, it->face_id);
6786 last_glyphless_glyph_frame = it->f;
6787 last_glyphless_glyph_face_id = it->face_id;
6788 last_glyphless_glyph_merged_face_id = face_id;
6790 return face_id;
6793 /* Load IT's display element fields with information about the next
6794 display element from the current position of IT. Value is zero if
6795 end of buffer (or C string) is reached. */
6797 static int
6798 get_next_display_element (struct it *it)
6800 /* Non-zero means that we found a display element. Zero means that
6801 we hit the end of what we iterate over. Performance note: the
6802 function pointer `method' used here turns out to be faster than
6803 using a sequence of if-statements. */
6804 int success_p;
6806 get_next:
6807 success_p = GET_NEXT_DISPLAY_ELEMENT (it);
6809 if (it->what == IT_CHARACTER)
6811 /* UAX#9, L4: "A character is depicted by a mirrored glyph if
6812 and only if (a) the resolved directionality of that character
6813 is R..." */
6814 /* FIXME: Do we need an exception for characters from display
6815 tables? */
6816 if (it->bidi_p && it->bidi_it.type == STRONG_R)
6817 it->c = bidi_mirror_char (it->c);
6818 /* Map via display table or translate control characters.
6819 IT->c, IT->len etc. have been set to the next character by
6820 the function call above. If we have a display table, and it
6821 contains an entry for IT->c, translate it. Don't do this if
6822 IT->c itself comes from a display table, otherwise we could
6823 end up in an infinite recursion. (An alternative could be to
6824 count the recursion depth of this function and signal an
6825 error when a certain maximum depth is reached.) Is it worth
6826 it? */
6827 if (success_p && it->dpvec == NULL)
6829 Lisp_Object dv;
6830 struct charset *unibyte = CHARSET_FROM_ID (charset_unibyte);
6831 int nonascii_space_p = 0;
6832 int nonascii_hyphen_p = 0;
6833 int c = it->c; /* This is the character to display. */
6835 if (! it->multibyte_p && ! ASCII_CHAR_P (c))
6837 eassert (SINGLE_BYTE_CHAR_P (c));
6838 if (unibyte_display_via_language_environment)
6840 c = DECODE_CHAR (unibyte, c);
6841 if (c < 0)
6842 c = BYTE8_TO_CHAR (it->c);
6844 else
6845 c = BYTE8_TO_CHAR (it->c);
6848 if (it->dp
6849 && (dv = DISP_CHAR_VECTOR (it->dp, c),
6850 VECTORP (dv)))
6852 struct Lisp_Vector *v = XVECTOR (dv);
6854 /* Return the first character from the display table
6855 entry, if not empty. If empty, don't display the
6856 current character. */
6857 if (v->header.size)
6859 it->dpvec_char_len = it->len;
6860 it->dpvec = v->contents;
6861 it->dpend = v->contents + v->header.size;
6862 it->current.dpvec_index = 0;
6863 it->dpvec_face_id = -1;
6864 it->saved_face_id = it->face_id;
6865 it->method = GET_FROM_DISPLAY_VECTOR;
6866 it->ellipsis_p = 0;
6868 else
6870 set_iterator_to_next (it, 0);
6872 goto get_next;
6875 if (! NILP (lookup_glyphless_char_display (c, it)))
6877 if (it->what == IT_GLYPHLESS)
6878 goto done;
6879 /* Don't display this character. */
6880 set_iterator_to_next (it, 0);
6881 goto get_next;
6884 /* If `nobreak-char-display' is non-nil, we display
6885 non-ASCII spaces and hyphens specially. */
6886 if (! ASCII_CHAR_P (c) && ! NILP (Vnobreak_char_display))
6888 if (c == 0xA0)
6889 nonascii_space_p = true;
6890 else if (c == 0xAD || c == 0x2010 || c == 0x2011)
6891 nonascii_hyphen_p = true;
6894 /* Translate control characters into `\003' or `^C' form.
6895 Control characters coming from a display table entry are
6896 currently not translated because we use IT->dpvec to hold
6897 the translation. This could easily be changed but I
6898 don't believe that it is worth doing.
6900 The characters handled by `nobreak-char-display' must be
6901 translated too.
6903 Non-printable characters and raw-byte characters are also
6904 translated to octal form. */
6905 if (((c < ' ' || c == 127) /* ASCII control chars. */
6906 ? (it->area != TEXT_AREA
6907 /* In mode line, treat \n, \t like other crl chars. */
6908 || (c != '\t'
6909 && it->glyph_row
6910 && (it->glyph_row->mode_line_p || it->avoid_cursor_p))
6911 || (c != '\n' && c != '\t'))
6912 : (nonascii_space_p
6913 || nonascii_hyphen_p
6914 || CHAR_BYTE8_P (c)
6915 || ! CHAR_PRINTABLE_P (c))))
6917 /* C is a control character, non-ASCII space/hyphen,
6918 raw-byte, or a non-printable character which must be
6919 displayed either as '\003' or as `^C' where the '\\'
6920 and '^' can be defined in the display table. Fill
6921 IT->ctl_chars with glyphs for what we have to
6922 display. Then, set IT->dpvec to these glyphs. */
6923 Lisp_Object gc;
6924 int ctl_len;
6925 int face_id;
6926 int lface_id = 0;
6927 int escape_glyph;
6929 /* Handle control characters with ^. */
6931 if (ASCII_CHAR_P (c) && it->ctl_arrow_p)
6933 int g;
6935 g = '^'; /* default glyph for Control */
6936 /* Set IT->ctl_chars[0] to the glyph for `^'. */
6937 if (it->dp
6938 && (gc = DISP_CTRL_GLYPH (it->dp), GLYPH_CODE_P (gc)))
6940 g = GLYPH_CODE_CHAR (gc);
6941 lface_id = GLYPH_CODE_FACE (gc);
6944 face_id = (lface_id
6945 ? merge_faces (it->f, Qt, lface_id, it->face_id)
6946 : merge_escape_glyph_face (it));
6948 XSETINT (it->ctl_chars[0], g);
6949 XSETINT (it->ctl_chars[1], c ^ 0100);
6950 ctl_len = 2;
6951 goto display_control;
6954 /* Handle non-ascii space in the mode where it only gets
6955 highlighting. */
6957 if (nonascii_space_p && EQ (Vnobreak_char_display, Qt))
6959 /* Merge `nobreak-space' into the current face. */
6960 face_id = merge_faces (it->f, Qnobreak_space, 0,
6961 it->face_id);
6962 XSETINT (it->ctl_chars[0], ' ');
6963 ctl_len = 1;
6964 goto display_control;
6967 /* Handle sequences that start with the "escape glyph". */
6969 /* the default escape glyph is \. */
6970 escape_glyph = '\\';
6972 if (it->dp
6973 && (gc = DISP_ESCAPE_GLYPH (it->dp), GLYPH_CODE_P (gc)))
6975 escape_glyph = GLYPH_CODE_CHAR (gc);
6976 lface_id = GLYPH_CODE_FACE (gc);
6979 face_id = (lface_id
6980 ? merge_faces (it->f, Qt, lface_id, it->face_id)
6981 : merge_escape_glyph_face (it));
6983 /* Draw non-ASCII hyphen with just highlighting: */
6985 if (nonascii_hyphen_p && EQ (Vnobreak_char_display, Qt))
6987 XSETINT (it->ctl_chars[0], '-');
6988 ctl_len = 1;
6989 goto display_control;
6992 /* Draw non-ASCII space/hyphen with escape glyph: */
6994 if (nonascii_space_p || nonascii_hyphen_p)
6996 XSETINT (it->ctl_chars[0], escape_glyph);
6997 XSETINT (it->ctl_chars[1], nonascii_space_p ? ' ' : '-');
6998 ctl_len = 2;
6999 goto display_control;
7003 char str[10];
7004 int len, i;
7006 if (CHAR_BYTE8_P (c))
7007 /* Display \200 instead of \17777600. */
7008 c = CHAR_TO_BYTE8 (c);
7009 len = sprintf (str, "%03o", c);
7011 XSETINT (it->ctl_chars[0], escape_glyph);
7012 for (i = 0; i < len; i++)
7013 XSETINT (it->ctl_chars[i + 1], str[i]);
7014 ctl_len = len + 1;
7017 display_control:
7018 /* Set up IT->dpvec and return first character from it. */
7019 it->dpvec_char_len = it->len;
7020 it->dpvec = it->ctl_chars;
7021 it->dpend = it->dpvec + ctl_len;
7022 it->current.dpvec_index = 0;
7023 it->dpvec_face_id = face_id;
7024 it->saved_face_id = it->face_id;
7025 it->method = GET_FROM_DISPLAY_VECTOR;
7026 it->ellipsis_p = 0;
7027 goto get_next;
7029 it->char_to_display = c;
7031 else if (success_p)
7033 it->char_to_display = it->c;
7037 #ifdef HAVE_WINDOW_SYSTEM
7038 /* Adjust face id for a multibyte character. There are no multibyte
7039 character in unibyte text. */
7040 if ((it->what == IT_CHARACTER || it->what == IT_COMPOSITION)
7041 && it->multibyte_p
7042 && success_p
7043 && FRAME_WINDOW_P (it->f))
7045 struct face *face = FACE_FROM_ID (it->f, it->face_id);
7047 if (it->what == IT_COMPOSITION && it->cmp_it.ch >= 0)
7049 /* Automatic composition with glyph-string. */
7050 Lisp_Object gstring = composition_gstring_from_id (it->cmp_it.id);
7052 it->face_id = face_for_font (it->f, LGSTRING_FONT (gstring), face);
7054 else
7056 ptrdiff_t pos = (it->s ? -1
7057 : STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
7058 : IT_CHARPOS (*it));
7059 int c;
7061 if (it->what == IT_CHARACTER)
7062 c = it->char_to_display;
7063 else
7065 struct composition *cmp = composition_table[it->cmp_it.id];
7066 int i;
7068 c = ' ';
7069 for (i = 0; i < cmp->glyph_len; i++)
7070 /* TAB in a composition means display glyphs with
7071 padding space on the left or right. */
7072 if ((c = COMPOSITION_GLYPH (cmp, i)) != '\t')
7073 break;
7075 it->face_id = FACE_FOR_CHAR (it->f, face, c, pos, it->string);
7078 #endif /* HAVE_WINDOW_SYSTEM */
7080 done:
7081 /* Is this character the last one of a run of characters with
7082 box? If yes, set IT->end_of_box_run_p to 1. */
7083 if (it->face_box_p
7084 && it->s == NULL)
7086 if (it->method == GET_FROM_STRING && it->sp)
7088 int face_id = underlying_face_id (it);
7089 struct face *face = FACE_FROM_ID (it->f, face_id);
7091 if (face)
7093 if (face->box == FACE_NO_BOX)
7095 /* If the box comes from face properties in a
7096 display string, check faces in that string. */
7097 int string_face_id = face_after_it_pos (it);
7098 it->end_of_box_run_p
7099 = (FACE_FROM_ID (it->f, string_face_id)->box
7100 == FACE_NO_BOX);
7102 /* Otherwise, the box comes from the underlying face.
7103 If this is the last string character displayed, check
7104 the next buffer location. */
7105 else if ((IT_STRING_CHARPOS (*it) >= SCHARS (it->string) - 1)
7106 /* n_overlay_strings is unreliable unless
7107 overlay_string_index is non-negative. */
7108 && ((it->current.overlay_string_index >= 0
7109 && (it->current.overlay_string_index
7110 == it->n_overlay_strings - 1))
7111 /* A string from display property. */
7112 || it->from_disp_prop_p))
7114 ptrdiff_t ignore;
7115 int next_face_id;
7116 struct text_pos pos = it->current.pos;
7118 /* For a string from a display property, the next
7119 buffer position is stored in the 'position'
7120 member of the iteration stack slot below the
7121 current one, see handle_single_display_spec. By
7122 contrast, it->current.pos was is not yet updated
7123 to point to that buffer position; that will
7124 happen in pop_it, after we finish displaying the
7125 current string. Note that we already checked
7126 above that it->sp is positive, so subtracting one
7127 from it is safe. */
7128 if (it->from_disp_prop_p)
7129 pos = (it->stack + it->sp - 1)->position;
7130 else
7131 INC_TEXT_POS (pos, it->multibyte_p);
7133 if (CHARPOS (pos) >= ZV)
7134 it->end_of_box_run_p = true;
7135 else
7137 next_face_id = face_at_buffer_position
7138 (it->w, CHARPOS (pos), &ignore,
7139 CHARPOS (pos) + TEXT_PROP_DISTANCE_LIMIT, 0, -1);
7140 it->end_of_box_run_p
7141 = (FACE_FROM_ID (it->f, next_face_id)->box
7142 == FACE_NO_BOX);
7147 /* next_element_from_display_vector sets this flag according to
7148 faces of the display vector glyphs, see there. */
7149 else if (it->method != GET_FROM_DISPLAY_VECTOR)
7151 int face_id = face_after_it_pos (it);
7152 it->end_of_box_run_p
7153 = (face_id != it->face_id
7154 && FACE_FROM_ID (it->f, face_id)->box == FACE_NO_BOX);
7157 /* If we reached the end of the object we've been iterating (e.g., a
7158 display string or an overlay string), and there's something on
7159 IT->stack, proceed with what's on the stack. It doesn't make
7160 sense to return zero if there's unprocessed stuff on the stack,
7161 because otherwise that stuff will never be displayed. */
7162 if (!success_p && it->sp > 0)
7164 set_iterator_to_next (it, 0);
7165 success_p = get_next_display_element (it);
7168 /* Value is 0 if end of buffer or string reached. */
7169 return success_p;
7173 /* Move IT to the next display element.
7175 RESEAT_P non-zero means if called on a newline in buffer text,
7176 skip to the next visible line start.
7178 Functions get_next_display_element and set_iterator_to_next are
7179 separate because I find this arrangement easier to handle than a
7180 get_next_display_element function that also increments IT's
7181 position. The way it is we can first look at an iterator's current
7182 display element, decide whether it fits on a line, and if it does,
7183 increment the iterator position. The other way around we probably
7184 would either need a flag indicating whether the iterator has to be
7185 incremented the next time, or we would have to implement a
7186 decrement position function which would not be easy to write. */
7188 void
7189 set_iterator_to_next (struct it *it, int reseat_p)
7191 /* Reset flags indicating start and end of a sequence of characters
7192 with box. Reset them at the start of this function because
7193 moving the iterator to a new position might set them. */
7194 it->start_of_box_run_p = it->end_of_box_run_p = 0;
7196 switch (it->method)
7198 case GET_FROM_BUFFER:
7199 /* The current display element of IT is a character from
7200 current_buffer. Advance in the buffer, and maybe skip over
7201 invisible lines that are so because of selective display. */
7202 if (ITERATOR_AT_END_OF_LINE_P (it) && reseat_p)
7203 reseat_at_next_visible_line_start (it, 0);
7204 else if (it->cmp_it.id >= 0)
7206 /* We are currently getting glyphs from a composition. */
7207 int i;
7209 if (! it->bidi_p)
7211 IT_CHARPOS (*it) += it->cmp_it.nchars;
7212 IT_BYTEPOS (*it) += it->cmp_it.nbytes;
7213 if (it->cmp_it.to < it->cmp_it.nglyphs)
7215 it->cmp_it.from = it->cmp_it.to;
7217 else
7219 it->cmp_it.id = -1;
7220 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
7221 IT_BYTEPOS (*it),
7222 it->end_charpos, Qnil);
7225 else if (! it->cmp_it.reversed_p)
7227 /* Composition created while scanning forward. */
7228 /* Update IT's char/byte positions to point to the first
7229 character of the next grapheme cluster, or to the
7230 character visually after the current composition. */
7231 for (i = 0; i < it->cmp_it.nchars; i++)
7232 bidi_move_to_visually_next (&it->bidi_it);
7233 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7234 IT_CHARPOS (*it) = it->bidi_it.charpos;
7236 if (it->cmp_it.to < it->cmp_it.nglyphs)
7238 /* Proceed to the next grapheme cluster. */
7239 it->cmp_it.from = it->cmp_it.to;
7241 else
7243 /* No more grapheme clusters in this composition.
7244 Find the next stop position. */
7245 ptrdiff_t stop = it->end_charpos;
7246 if (it->bidi_it.scan_dir < 0)
7247 /* Now we are scanning backward and don't know
7248 where to stop. */
7249 stop = -1;
7250 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
7251 IT_BYTEPOS (*it), stop, Qnil);
7254 else
7256 /* Composition created while scanning backward. */
7257 /* Update IT's char/byte positions to point to the last
7258 character of the previous grapheme cluster, or the
7259 character visually after the current composition. */
7260 for (i = 0; i < it->cmp_it.nchars; i++)
7261 bidi_move_to_visually_next (&it->bidi_it);
7262 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7263 IT_CHARPOS (*it) = it->bidi_it.charpos;
7264 if (it->cmp_it.from > 0)
7266 /* Proceed to the previous grapheme cluster. */
7267 it->cmp_it.to = it->cmp_it.from;
7269 else
7271 /* No more grapheme clusters in this composition.
7272 Find the next stop position. */
7273 ptrdiff_t stop = it->end_charpos;
7274 if (it->bidi_it.scan_dir < 0)
7275 /* Now we are scanning backward and don't know
7276 where to stop. */
7277 stop = -1;
7278 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
7279 IT_BYTEPOS (*it), stop, Qnil);
7283 else
7285 eassert (it->len != 0);
7287 if (!it->bidi_p)
7289 IT_BYTEPOS (*it) += it->len;
7290 IT_CHARPOS (*it) += 1;
7292 else
7294 int prev_scan_dir = it->bidi_it.scan_dir;
7295 /* If this is a new paragraph, determine its base
7296 direction (a.k.a. its base embedding level). */
7297 if (it->bidi_it.new_paragraph)
7298 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 0);
7299 bidi_move_to_visually_next (&it->bidi_it);
7300 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7301 IT_CHARPOS (*it) = it->bidi_it.charpos;
7302 if (prev_scan_dir != it->bidi_it.scan_dir)
7304 /* As the scan direction was changed, we must
7305 re-compute the stop position for composition. */
7306 ptrdiff_t stop = it->end_charpos;
7307 if (it->bidi_it.scan_dir < 0)
7308 stop = -1;
7309 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
7310 IT_BYTEPOS (*it), stop, Qnil);
7313 eassert (IT_BYTEPOS (*it) == CHAR_TO_BYTE (IT_CHARPOS (*it)));
7315 break;
7317 case GET_FROM_C_STRING:
7318 /* Current display element of IT is from a C string. */
7319 if (!it->bidi_p
7320 /* If the string position is beyond string's end, it means
7321 next_element_from_c_string is padding the string with
7322 blanks, in which case we bypass the bidi iterator,
7323 because it cannot deal with such virtual characters. */
7324 || IT_CHARPOS (*it) >= it->bidi_it.string.schars)
7326 IT_BYTEPOS (*it) += it->len;
7327 IT_CHARPOS (*it) += 1;
7329 else
7331 bidi_move_to_visually_next (&it->bidi_it);
7332 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7333 IT_CHARPOS (*it) = it->bidi_it.charpos;
7335 break;
7337 case GET_FROM_DISPLAY_VECTOR:
7338 /* Current display element of IT is from a display table entry.
7339 Advance in the display table definition. Reset it to null if
7340 end reached, and continue with characters from buffers/
7341 strings. */
7342 ++it->current.dpvec_index;
7344 /* Restore face of the iterator to what they were before the
7345 display vector entry (these entries may contain faces). */
7346 it->face_id = it->saved_face_id;
7348 if (it->dpvec + it->current.dpvec_index >= it->dpend)
7350 int recheck_faces = it->ellipsis_p;
7352 if (it->s)
7353 it->method = GET_FROM_C_STRING;
7354 else if (STRINGP (it->string))
7355 it->method = GET_FROM_STRING;
7356 else
7358 it->method = GET_FROM_BUFFER;
7359 it->object = it->w->contents;
7362 it->dpvec = NULL;
7363 it->current.dpvec_index = -1;
7365 /* Skip over characters which were displayed via IT->dpvec. */
7366 if (it->dpvec_char_len < 0)
7367 reseat_at_next_visible_line_start (it, 1);
7368 else if (it->dpvec_char_len > 0)
7370 if (it->method == GET_FROM_STRING
7371 && it->current.overlay_string_index >= 0
7372 && it->n_overlay_strings > 0)
7373 it->ignore_overlay_strings_at_pos_p = true;
7374 it->len = it->dpvec_char_len;
7375 set_iterator_to_next (it, reseat_p);
7378 /* Maybe recheck faces after display vector. */
7379 if (recheck_faces)
7380 it->stop_charpos = IT_CHARPOS (*it);
7382 break;
7384 case GET_FROM_STRING:
7385 /* Current display element is a character from a Lisp string. */
7386 eassert (it->s == NULL && STRINGP (it->string));
7387 /* Don't advance past string end. These conditions are true
7388 when set_iterator_to_next is called at the end of
7389 get_next_display_element, in which case the Lisp string is
7390 already exhausted, and all we want is pop the iterator
7391 stack. */
7392 if (it->current.overlay_string_index >= 0)
7394 /* This is an overlay string, so there's no padding with
7395 spaces, and the number of characters in the string is
7396 where the string ends. */
7397 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7398 goto consider_string_end;
7400 else
7402 /* Not an overlay string. There could be padding, so test
7403 against it->end_charpos. */
7404 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
7405 goto consider_string_end;
7407 if (it->cmp_it.id >= 0)
7409 int i;
7411 if (! it->bidi_p)
7413 IT_STRING_CHARPOS (*it) += it->cmp_it.nchars;
7414 IT_STRING_BYTEPOS (*it) += it->cmp_it.nbytes;
7415 if (it->cmp_it.to < it->cmp_it.nglyphs)
7416 it->cmp_it.from = it->cmp_it.to;
7417 else
7419 it->cmp_it.id = -1;
7420 composition_compute_stop_pos (&it->cmp_it,
7421 IT_STRING_CHARPOS (*it),
7422 IT_STRING_BYTEPOS (*it),
7423 it->end_charpos, it->string);
7426 else if (! it->cmp_it.reversed_p)
7428 for (i = 0; i < it->cmp_it.nchars; i++)
7429 bidi_move_to_visually_next (&it->bidi_it);
7430 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7431 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7433 if (it->cmp_it.to < it->cmp_it.nglyphs)
7434 it->cmp_it.from = it->cmp_it.to;
7435 else
7437 ptrdiff_t stop = it->end_charpos;
7438 if (it->bidi_it.scan_dir < 0)
7439 stop = -1;
7440 composition_compute_stop_pos (&it->cmp_it,
7441 IT_STRING_CHARPOS (*it),
7442 IT_STRING_BYTEPOS (*it), stop,
7443 it->string);
7446 else
7448 for (i = 0; i < it->cmp_it.nchars; i++)
7449 bidi_move_to_visually_next (&it->bidi_it);
7450 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7451 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7452 if (it->cmp_it.from > 0)
7453 it->cmp_it.to = it->cmp_it.from;
7454 else
7456 ptrdiff_t stop = it->end_charpos;
7457 if (it->bidi_it.scan_dir < 0)
7458 stop = -1;
7459 composition_compute_stop_pos (&it->cmp_it,
7460 IT_STRING_CHARPOS (*it),
7461 IT_STRING_BYTEPOS (*it), stop,
7462 it->string);
7466 else
7468 if (!it->bidi_p
7469 /* If the string position is beyond string's end, it
7470 means next_element_from_string is padding the string
7471 with blanks, in which case we bypass the bidi
7472 iterator, because it cannot deal with such virtual
7473 characters. */
7474 || IT_STRING_CHARPOS (*it) >= it->bidi_it.string.schars)
7476 IT_STRING_BYTEPOS (*it) += it->len;
7477 IT_STRING_CHARPOS (*it) += 1;
7479 else
7481 int prev_scan_dir = it->bidi_it.scan_dir;
7483 bidi_move_to_visually_next (&it->bidi_it);
7484 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7485 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7486 if (prev_scan_dir != it->bidi_it.scan_dir)
7488 ptrdiff_t stop = it->end_charpos;
7490 if (it->bidi_it.scan_dir < 0)
7491 stop = -1;
7492 composition_compute_stop_pos (&it->cmp_it,
7493 IT_STRING_CHARPOS (*it),
7494 IT_STRING_BYTEPOS (*it), stop,
7495 it->string);
7500 consider_string_end:
7502 if (it->current.overlay_string_index >= 0)
7504 /* IT->string is an overlay string. Advance to the
7505 next, if there is one. */
7506 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7508 it->ellipsis_p = 0;
7509 next_overlay_string (it);
7510 if (it->ellipsis_p)
7511 setup_for_ellipsis (it, 0);
7514 else
7516 /* IT->string is not an overlay string. If we reached
7517 its end, and there is something on IT->stack, proceed
7518 with what is on the stack. This can be either another
7519 string, this time an overlay string, or a buffer. */
7520 if (IT_STRING_CHARPOS (*it) == SCHARS (it->string)
7521 && it->sp > 0)
7523 pop_it (it);
7524 if (it->method == GET_FROM_STRING)
7525 goto consider_string_end;
7528 break;
7530 case GET_FROM_IMAGE:
7531 case GET_FROM_STRETCH:
7532 /* The position etc with which we have to proceed are on
7533 the stack. The position may be at the end of a string,
7534 if the `display' property takes up the whole string. */
7535 eassert (it->sp > 0);
7536 pop_it (it);
7537 if (it->method == GET_FROM_STRING)
7538 goto consider_string_end;
7539 break;
7541 default:
7542 /* There are no other methods defined, so this should be a bug. */
7543 emacs_abort ();
7546 eassert (it->method != GET_FROM_STRING
7547 || (STRINGP (it->string)
7548 && IT_STRING_CHARPOS (*it) >= 0));
7551 /* Load IT's display element fields with information about the next
7552 display element which comes from a display table entry or from the
7553 result of translating a control character to one of the forms `^C'
7554 or `\003'.
7556 IT->dpvec holds the glyphs to return as characters.
7557 IT->saved_face_id holds the face id before the display vector--it
7558 is restored into IT->face_id in set_iterator_to_next. */
7560 static int
7561 next_element_from_display_vector (struct it *it)
7563 Lisp_Object gc;
7564 int prev_face_id = it->face_id;
7565 int next_face_id;
7567 /* Precondition. */
7568 eassert (it->dpvec && it->current.dpvec_index >= 0);
7570 it->face_id = it->saved_face_id;
7572 /* KFS: This code used to check ip->dpvec[0] instead of the current element.
7573 That seemed totally bogus - so I changed it... */
7574 gc = it->dpvec[it->current.dpvec_index];
7576 if (GLYPH_CODE_P (gc))
7578 struct face *this_face, *prev_face, *next_face;
7580 it->c = GLYPH_CODE_CHAR (gc);
7581 it->len = CHAR_BYTES (it->c);
7583 /* The entry may contain a face id to use. Such a face id is
7584 the id of a Lisp face, not a realized face. A face id of
7585 zero means no face is specified. */
7586 if (it->dpvec_face_id >= 0)
7587 it->face_id = it->dpvec_face_id;
7588 else
7590 int lface_id = GLYPH_CODE_FACE (gc);
7591 if (lface_id > 0)
7592 it->face_id = merge_faces (it->f, Qt, lface_id,
7593 it->saved_face_id);
7596 /* Glyphs in the display vector could have the box face, so we
7597 need to set the related flags in the iterator, as
7598 appropriate. */
7599 this_face = FACE_FROM_ID (it->f, it->face_id);
7600 prev_face = FACE_FROM_ID (it->f, prev_face_id);
7602 /* Is this character the first character of a box-face run? */
7603 it->start_of_box_run_p = (this_face && this_face->box != FACE_NO_BOX
7604 && (!prev_face
7605 || prev_face->box == FACE_NO_BOX));
7607 /* For the last character of the box-face run, we need to look
7608 either at the next glyph from the display vector, or at the
7609 face we saw before the display vector. */
7610 next_face_id = it->saved_face_id;
7611 if (it->current.dpvec_index < it->dpend - it->dpvec - 1)
7613 if (it->dpvec_face_id >= 0)
7614 next_face_id = it->dpvec_face_id;
7615 else
7617 int lface_id =
7618 GLYPH_CODE_FACE (it->dpvec[it->current.dpvec_index + 1]);
7620 if (lface_id > 0)
7621 next_face_id = merge_faces (it->f, Qt, lface_id,
7622 it->saved_face_id);
7625 next_face = FACE_FROM_ID (it->f, next_face_id);
7626 it->end_of_box_run_p = (this_face && this_face->box != FACE_NO_BOX
7627 && (!next_face
7628 || next_face->box == FACE_NO_BOX));
7629 it->face_box_p = this_face && this_face->box != FACE_NO_BOX;
7631 else
7632 /* Display table entry is invalid. Return a space. */
7633 it->c = ' ', it->len = 1;
7635 /* Don't change position and object of the iterator here. They are
7636 still the values of the character that had this display table
7637 entry or was translated, and that's what we want. */
7638 it->what = IT_CHARACTER;
7639 return 1;
7642 /* Get the first element of string/buffer in the visual order, after
7643 being reseated to a new position in a string or a buffer. */
7644 static void
7645 get_visually_first_element (struct it *it)
7647 int string_p = STRINGP (it->string) || it->s;
7648 ptrdiff_t eob = (string_p ? it->bidi_it.string.schars : ZV);
7649 ptrdiff_t bob = (string_p ? 0 : BEGV);
7651 if (STRINGP (it->string))
7653 it->bidi_it.charpos = IT_STRING_CHARPOS (*it);
7654 it->bidi_it.bytepos = IT_STRING_BYTEPOS (*it);
7656 else
7658 it->bidi_it.charpos = IT_CHARPOS (*it);
7659 it->bidi_it.bytepos = IT_BYTEPOS (*it);
7662 if (it->bidi_it.charpos == eob)
7664 /* Nothing to do, but reset the FIRST_ELT flag, like
7665 bidi_paragraph_init does, because we are not going to
7666 call it. */
7667 it->bidi_it.first_elt = 0;
7669 else if (it->bidi_it.charpos == bob
7670 || (!string_p
7671 && (FETCH_CHAR (it->bidi_it.bytepos - 1) == '\n'
7672 || FETCH_CHAR (it->bidi_it.bytepos) == '\n')))
7674 /* If we are at the beginning of a line/string, we can produce
7675 the next element right away. */
7676 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
7677 bidi_move_to_visually_next (&it->bidi_it);
7679 else
7681 ptrdiff_t orig_bytepos = it->bidi_it.bytepos;
7683 /* We need to prime the bidi iterator starting at the line's or
7684 string's beginning, before we will be able to produce the
7685 next element. */
7686 if (string_p)
7687 it->bidi_it.charpos = it->bidi_it.bytepos = 0;
7688 else
7689 it->bidi_it.charpos = find_newline_no_quit (IT_CHARPOS (*it),
7690 IT_BYTEPOS (*it), -1,
7691 &it->bidi_it.bytepos);
7692 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
7695 /* Now return to buffer/string position where we were asked
7696 to get the next display element, and produce that. */
7697 bidi_move_to_visually_next (&it->bidi_it);
7699 while (it->bidi_it.bytepos != orig_bytepos
7700 && it->bidi_it.charpos < eob);
7703 /* Adjust IT's position information to where we ended up. */
7704 if (STRINGP (it->string))
7706 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7707 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7709 else
7711 IT_CHARPOS (*it) = it->bidi_it.charpos;
7712 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7715 if (STRINGP (it->string) || !it->s)
7717 ptrdiff_t stop, charpos, bytepos;
7719 if (STRINGP (it->string))
7721 eassert (!it->s);
7722 stop = SCHARS (it->string);
7723 if (stop > it->end_charpos)
7724 stop = it->end_charpos;
7725 charpos = IT_STRING_CHARPOS (*it);
7726 bytepos = IT_STRING_BYTEPOS (*it);
7728 else
7730 stop = it->end_charpos;
7731 charpos = IT_CHARPOS (*it);
7732 bytepos = IT_BYTEPOS (*it);
7734 if (it->bidi_it.scan_dir < 0)
7735 stop = -1;
7736 composition_compute_stop_pos (&it->cmp_it, charpos, bytepos, stop,
7737 it->string);
7741 /* Load IT with the next display element from Lisp string IT->string.
7742 IT->current.string_pos is the current position within the string.
7743 If IT->current.overlay_string_index >= 0, the Lisp string is an
7744 overlay string. */
7746 static int
7747 next_element_from_string (struct it *it)
7749 struct text_pos position;
7751 eassert (STRINGP (it->string));
7752 eassert (!it->bidi_p || EQ (it->string, it->bidi_it.string.lstring));
7753 eassert (IT_STRING_CHARPOS (*it) >= 0);
7754 position = it->current.string_pos;
7756 /* With bidi reordering, the character to display might not be the
7757 character at IT_STRING_CHARPOS. BIDI_IT.FIRST_ELT non-zero means
7758 that we were reseat()ed to a new string, whose paragraph
7759 direction is not known. */
7760 if (it->bidi_p && it->bidi_it.first_elt)
7762 get_visually_first_element (it);
7763 SET_TEXT_POS (position, IT_STRING_CHARPOS (*it), IT_STRING_BYTEPOS (*it));
7766 /* Time to check for invisible text? */
7767 if (IT_STRING_CHARPOS (*it) < it->end_charpos)
7769 if (IT_STRING_CHARPOS (*it) >= it->stop_charpos)
7771 if (!(!it->bidi_p
7772 || BIDI_AT_BASE_LEVEL (it->bidi_it)
7773 || IT_STRING_CHARPOS (*it) == it->stop_charpos))
7775 /* With bidi non-linear iteration, we could find
7776 ourselves far beyond the last computed stop_charpos,
7777 with several other stop positions in between that we
7778 missed. Scan them all now, in buffer's logical
7779 order, until we find and handle the last stop_charpos
7780 that precedes our current position. */
7781 handle_stop_backwards (it, it->stop_charpos);
7782 return GET_NEXT_DISPLAY_ELEMENT (it);
7784 else
7786 if (it->bidi_p)
7788 /* Take note of the stop position we just moved
7789 across, for when we will move back across it. */
7790 it->prev_stop = it->stop_charpos;
7791 /* If we are at base paragraph embedding level, take
7792 note of the last stop position seen at this
7793 level. */
7794 if (BIDI_AT_BASE_LEVEL (it->bidi_it))
7795 it->base_level_stop = it->stop_charpos;
7797 handle_stop (it);
7799 /* Since a handler may have changed IT->method, we must
7800 recurse here. */
7801 return GET_NEXT_DISPLAY_ELEMENT (it);
7804 else if (it->bidi_p
7805 /* If we are before prev_stop, we may have overstepped
7806 on our way backwards a stop_pos, and if so, we need
7807 to handle that stop_pos. */
7808 && IT_STRING_CHARPOS (*it) < it->prev_stop
7809 /* We can sometimes back up for reasons that have nothing
7810 to do with bidi reordering. E.g., compositions. The
7811 code below is only needed when we are above the base
7812 embedding level, so test for that explicitly. */
7813 && !BIDI_AT_BASE_LEVEL (it->bidi_it))
7815 /* If we lost track of base_level_stop, we have no better
7816 place for handle_stop_backwards to start from than string
7817 beginning. This happens, e.g., when we were reseated to
7818 the previous screenful of text by vertical-motion. */
7819 if (it->base_level_stop <= 0
7820 || IT_STRING_CHARPOS (*it) < it->base_level_stop)
7821 it->base_level_stop = 0;
7822 handle_stop_backwards (it, it->base_level_stop);
7823 return GET_NEXT_DISPLAY_ELEMENT (it);
7827 if (it->current.overlay_string_index >= 0)
7829 /* Get the next character from an overlay string. In overlay
7830 strings, there is no field width or padding with spaces to
7831 do. */
7832 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7834 it->what = IT_EOB;
7835 return 0;
7837 else if (CHAR_COMPOSED_P (it, IT_STRING_CHARPOS (*it),
7838 IT_STRING_BYTEPOS (*it),
7839 it->bidi_it.scan_dir < 0
7840 ? -1
7841 : SCHARS (it->string))
7842 && next_element_from_composition (it))
7844 return 1;
7846 else if (STRING_MULTIBYTE (it->string))
7848 const unsigned char *s = (SDATA (it->string)
7849 + IT_STRING_BYTEPOS (*it));
7850 it->c = string_char_and_length (s, &it->len);
7852 else
7854 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
7855 it->len = 1;
7858 else
7860 /* Get the next character from a Lisp string that is not an
7861 overlay string. Such strings come from the mode line, for
7862 example. We may have to pad with spaces, or truncate the
7863 string. See also next_element_from_c_string. */
7864 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
7866 it->what = IT_EOB;
7867 return 0;
7869 else if (IT_STRING_CHARPOS (*it) >= it->string_nchars)
7871 /* Pad with spaces. */
7872 it->c = ' ', it->len = 1;
7873 CHARPOS (position) = BYTEPOS (position) = -1;
7875 else if (CHAR_COMPOSED_P (it, IT_STRING_CHARPOS (*it),
7876 IT_STRING_BYTEPOS (*it),
7877 it->bidi_it.scan_dir < 0
7878 ? -1
7879 : it->string_nchars)
7880 && next_element_from_composition (it))
7882 return 1;
7884 else if (STRING_MULTIBYTE (it->string))
7886 const unsigned char *s = (SDATA (it->string)
7887 + IT_STRING_BYTEPOS (*it));
7888 it->c = string_char_and_length (s, &it->len);
7890 else
7892 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
7893 it->len = 1;
7897 /* Record what we have and where it came from. */
7898 it->what = IT_CHARACTER;
7899 it->object = it->string;
7900 it->position = position;
7901 return 1;
7905 /* Load IT with next display element from C string IT->s.
7906 IT->string_nchars is the maximum number of characters to return
7907 from the string. IT->end_charpos may be greater than
7908 IT->string_nchars when this function is called, in which case we
7909 may have to return padding spaces. Value is zero if end of string
7910 reached, including padding spaces. */
7912 static int
7913 next_element_from_c_string (struct it *it)
7915 bool success_p = true;
7917 eassert (it->s);
7918 eassert (!it->bidi_p || it->s == it->bidi_it.string.s);
7919 it->what = IT_CHARACTER;
7920 BYTEPOS (it->position) = CHARPOS (it->position) = 0;
7921 it->object = Qnil;
7923 /* With bidi reordering, the character to display might not be the
7924 character at IT_CHARPOS. BIDI_IT.FIRST_ELT non-zero means that
7925 we were reseated to a new string, whose paragraph direction is
7926 not known. */
7927 if (it->bidi_p && it->bidi_it.first_elt)
7928 get_visually_first_element (it);
7930 /* IT's position can be greater than IT->string_nchars in case a
7931 field width or precision has been specified when the iterator was
7932 initialized. */
7933 if (IT_CHARPOS (*it) >= it->end_charpos)
7935 /* End of the game. */
7936 it->what = IT_EOB;
7937 success_p = 0;
7939 else if (IT_CHARPOS (*it) >= it->string_nchars)
7941 /* Pad with spaces. */
7942 it->c = ' ', it->len = 1;
7943 BYTEPOS (it->position) = CHARPOS (it->position) = -1;
7945 else if (it->multibyte_p)
7946 it->c = string_char_and_length (it->s + IT_BYTEPOS (*it), &it->len);
7947 else
7948 it->c = it->s[IT_BYTEPOS (*it)], it->len = 1;
7950 return success_p;
7954 /* Set up IT to return characters from an ellipsis, if appropriate.
7955 The definition of the ellipsis glyphs may come from a display table
7956 entry. This function fills IT with the first glyph from the
7957 ellipsis if an ellipsis is to be displayed. */
7959 static int
7960 next_element_from_ellipsis (struct it *it)
7962 if (it->selective_display_ellipsis_p)
7963 setup_for_ellipsis (it, it->len);
7964 else
7966 /* The face at the current position may be different from the
7967 face we find after the invisible text. Remember what it
7968 was in IT->saved_face_id, and signal that it's there by
7969 setting face_before_selective_p. */
7970 it->saved_face_id = it->face_id;
7971 it->method = GET_FROM_BUFFER;
7972 it->object = it->w->contents;
7973 reseat_at_next_visible_line_start (it, 1);
7974 it->face_before_selective_p = true;
7977 return GET_NEXT_DISPLAY_ELEMENT (it);
7981 /* Deliver an image display element. The iterator IT is already
7982 filled with image information (done in handle_display_prop). Value
7983 is always 1. */
7986 static int
7987 next_element_from_image (struct it *it)
7989 it->what = IT_IMAGE;
7990 it->ignore_overlay_strings_at_pos_p = 0;
7991 return 1;
7995 /* Fill iterator IT with next display element from a stretch glyph
7996 property. IT->object is the value of the text property. Value is
7997 always 1. */
7999 static int
8000 next_element_from_stretch (struct it *it)
8002 it->what = IT_STRETCH;
8003 return 1;
8006 /* Scan backwards from IT's current position until we find a stop
8007 position, or until BEGV. This is called when we find ourself
8008 before both the last known prev_stop and base_level_stop while
8009 reordering bidirectional text. */
8011 static void
8012 compute_stop_pos_backwards (struct it *it)
8014 const int SCAN_BACK_LIMIT = 1000;
8015 struct text_pos pos;
8016 struct display_pos save_current = it->current;
8017 struct text_pos save_position = it->position;
8018 ptrdiff_t charpos = IT_CHARPOS (*it);
8019 ptrdiff_t where_we_are = charpos;
8020 ptrdiff_t save_stop_pos = it->stop_charpos;
8021 ptrdiff_t save_end_pos = it->end_charpos;
8023 eassert (NILP (it->string) && !it->s);
8024 eassert (it->bidi_p);
8025 it->bidi_p = 0;
8028 it->end_charpos = min (charpos + 1, ZV);
8029 charpos = max (charpos - SCAN_BACK_LIMIT, BEGV);
8030 SET_TEXT_POS (pos, charpos, CHAR_TO_BYTE (charpos));
8031 reseat_1 (it, pos, 0);
8032 compute_stop_pos (it);
8033 /* We must advance forward, right? */
8034 if (it->stop_charpos <= charpos)
8035 emacs_abort ();
8037 while (charpos > BEGV && it->stop_charpos >= it->end_charpos);
8039 if (it->stop_charpos <= where_we_are)
8040 it->prev_stop = it->stop_charpos;
8041 else
8042 it->prev_stop = BEGV;
8043 it->bidi_p = true;
8044 it->current = save_current;
8045 it->position = save_position;
8046 it->stop_charpos = save_stop_pos;
8047 it->end_charpos = save_end_pos;
8050 /* Scan forward from CHARPOS in the current buffer/string, until we
8051 find a stop position > current IT's position. Then handle the stop
8052 position before that. This is called when we bump into a stop
8053 position while reordering bidirectional text. CHARPOS should be
8054 the last previously processed stop_pos (or BEGV/0, if none were
8055 processed yet) whose position is less that IT's current
8056 position. */
8058 static void
8059 handle_stop_backwards (struct it *it, ptrdiff_t charpos)
8061 int bufp = !STRINGP (it->string);
8062 ptrdiff_t where_we_are = (bufp ? IT_CHARPOS (*it) : IT_STRING_CHARPOS (*it));
8063 struct display_pos save_current = it->current;
8064 struct text_pos save_position = it->position;
8065 struct text_pos pos1;
8066 ptrdiff_t next_stop;
8068 /* Scan in strict logical order. */
8069 eassert (it->bidi_p);
8070 it->bidi_p = 0;
8073 it->prev_stop = charpos;
8074 if (bufp)
8076 SET_TEXT_POS (pos1, charpos, CHAR_TO_BYTE (charpos));
8077 reseat_1 (it, pos1, 0);
8079 else
8080 it->current.string_pos = string_pos (charpos, it->string);
8081 compute_stop_pos (it);
8082 /* We must advance forward, right? */
8083 if (it->stop_charpos <= it->prev_stop)
8084 emacs_abort ();
8085 charpos = it->stop_charpos;
8087 while (charpos <= where_we_are);
8089 it->bidi_p = true;
8090 it->current = save_current;
8091 it->position = save_position;
8092 next_stop = it->stop_charpos;
8093 it->stop_charpos = it->prev_stop;
8094 handle_stop (it);
8095 it->stop_charpos = next_stop;
8098 /* Load IT with the next display element from current_buffer. Value
8099 is zero if end of buffer reached. IT->stop_charpos is the next
8100 position at which to stop and check for text properties or buffer
8101 end. */
8103 static int
8104 next_element_from_buffer (struct it *it)
8106 bool success_p = true;
8108 eassert (IT_CHARPOS (*it) >= BEGV);
8109 eassert (NILP (it->string) && !it->s);
8110 eassert (!it->bidi_p
8111 || (EQ (it->bidi_it.string.lstring, Qnil)
8112 && it->bidi_it.string.s == NULL));
8114 /* With bidi reordering, the character to display might not be the
8115 character at IT_CHARPOS. BIDI_IT.FIRST_ELT non-zero means that
8116 we were reseat()ed to a new buffer position, which is potentially
8117 a different paragraph. */
8118 if (it->bidi_p && it->bidi_it.first_elt)
8120 get_visually_first_element (it);
8121 SET_TEXT_POS (it->position, IT_CHARPOS (*it), IT_BYTEPOS (*it));
8124 if (IT_CHARPOS (*it) >= it->stop_charpos)
8126 if (IT_CHARPOS (*it) >= it->end_charpos)
8128 int overlay_strings_follow_p;
8130 /* End of the game, except when overlay strings follow that
8131 haven't been returned yet. */
8132 if (it->overlay_strings_at_end_processed_p)
8133 overlay_strings_follow_p = 0;
8134 else
8136 it->overlay_strings_at_end_processed_p = true;
8137 overlay_strings_follow_p = get_overlay_strings (it, 0);
8140 if (overlay_strings_follow_p)
8141 success_p = GET_NEXT_DISPLAY_ELEMENT (it);
8142 else
8144 it->what = IT_EOB;
8145 it->position = it->current.pos;
8146 success_p = 0;
8149 else if (!(!it->bidi_p
8150 || BIDI_AT_BASE_LEVEL (it->bidi_it)
8151 || IT_CHARPOS (*it) == it->stop_charpos))
8153 /* With bidi non-linear iteration, we could find ourselves
8154 far beyond the last computed stop_charpos, with several
8155 other stop positions in between that we missed. Scan
8156 them all now, in buffer's logical order, until we find
8157 and handle the last stop_charpos that precedes our
8158 current position. */
8159 handle_stop_backwards (it, it->stop_charpos);
8160 return GET_NEXT_DISPLAY_ELEMENT (it);
8162 else
8164 if (it->bidi_p)
8166 /* Take note of the stop position we just moved across,
8167 for when we will move back across it. */
8168 it->prev_stop = it->stop_charpos;
8169 /* If we are at base paragraph embedding level, take
8170 note of the last stop position seen at this
8171 level. */
8172 if (BIDI_AT_BASE_LEVEL (it->bidi_it))
8173 it->base_level_stop = it->stop_charpos;
8175 handle_stop (it);
8176 return GET_NEXT_DISPLAY_ELEMENT (it);
8179 else if (it->bidi_p
8180 /* If we are before prev_stop, we may have overstepped on
8181 our way backwards a stop_pos, and if so, we need to
8182 handle that stop_pos. */
8183 && IT_CHARPOS (*it) < it->prev_stop
8184 /* We can sometimes back up for reasons that have nothing
8185 to do with bidi reordering. E.g., compositions. The
8186 code below is only needed when we are above the base
8187 embedding level, so test for that explicitly. */
8188 && !BIDI_AT_BASE_LEVEL (it->bidi_it))
8190 if (it->base_level_stop <= 0
8191 || IT_CHARPOS (*it) < it->base_level_stop)
8193 /* If we lost track of base_level_stop, we need to find
8194 prev_stop by looking backwards. This happens, e.g., when
8195 we were reseated to the previous screenful of text by
8196 vertical-motion. */
8197 it->base_level_stop = BEGV;
8198 compute_stop_pos_backwards (it);
8199 handle_stop_backwards (it, it->prev_stop);
8201 else
8202 handle_stop_backwards (it, it->base_level_stop);
8203 return GET_NEXT_DISPLAY_ELEMENT (it);
8205 else
8207 /* No face changes, overlays etc. in sight, so just return a
8208 character from current_buffer. */
8209 unsigned char *p;
8210 ptrdiff_t stop;
8212 /* Maybe run the redisplay end trigger hook. Performance note:
8213 This doesn't seem to cost measurable time. */
8214 if (it->redisplay_end_trigger_charpos
8215 && it->glyph_row
8216 && IT_CHARPOS (*it) >= it->redisplay_end_trigger_charpos)
8217 run_redisplay_end_trigger_hook (it);
8219 stop = it->bidi_it.scan_dir < 0 ? -1 : it->end_charpos;
8220 if (CHAR_COMPOSED_P (it, IT_CHARPOS (*it), IT_BYTEPOS (*it),
8221 stop)
8222 && next_element_from_composition (it))
8224 return 1;
8227 /* Get the next character, maybe multibyte. */
8228 p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
8229 if (it->multibyte_p && !ASCII_BYTE_P (*p))
8230 it->c = STRING_CHAR_AND_LENGTH (p, it->len);
8231 else
8232 it->c = *p, it->len = 1;
8234 /* Record what we have and where it came from. */
8235 it->what = IT_CHARACTER;
8236 it->object = it->w->contents;
8237 it->position = it->current.pos;
8239 /* Normally we return the character found above, except when we
8240 really want to return an ellipsis for selective display. */
8241 if (it->selective)
8243 if (it->c == '\n')
8245 /* A value of selective > 0 means hide lines indented more
8246 than that number of columns. */
8247 if (it->selective > 0
8248 && IT_CHARPOS (*it) + 1 < ZV
8249 && indented_beyond_p (IT_CHARPOS (*it) + 1,
8250 IT_BYTEPOS (*it) + 1,
8251 it->selective))
8253 success_p = next_element_from_ellipsis (it);
8254 it->dpvec_char_len = -1;
8257 else if (it->c == '\r' && it->selective == -1)
8259 /* A value of selective == -1 means that everything from the
8260 CR to the end of the line is invisible, with maybe an
8261 ellipsis displayed for it. */
8262 success_p = next_element_from_ellipsis (it);
8263 it->dpvec_char_len = -1;
8268 /* Value is zero if end of buffer reached. */
8269 eassert (!success_p || it->what != IT_CHARACTER || it->len > 0);
8270 return success_p;
8274 /* Run the redisplay end trigger hook for IT. */
8276 static void
8277 run_redisplay_end_trigger_hook (struct it *it)
8279 Lisp_Object args[3];
8281 /* IT->glyph_row should be non-null, i.e. we should be actually
8282 displaying something, or otherwise we should not run the hook. */
8283 eassert (it->glyph_row);
8285 /* Set up hook arguments. */
8286 args[0] = Qredisplay_end_trigger_functions;
8287 args[1] = it->window;
8288 XSETINT (args[2], it->redisplay_end_trigger_charpos);
8289 it->redisplay_end_trigger_charpos = 0;
8291 /* Since we are *trying* to run these functions, don't try to run
8292 them again, even if they get an error. */
8293 wset_redisplay_end_trigger (it->w, Qnil);
8294 Frun_hook_with_args (3, args);
8296 /* Notice if it changed the face of the character we are on. */
8297 handle_face_prop (it);
8301 /* Deliver a composition display element. Unlike the other
8302 next_element_from_XXX, this function is not registered in the array
8303 get_next_element[]. It is called from next_element_from_buffer and
8304 next_element_from_string when necessary. */
8306 static int
8307 next_element_from_composition (struct it *it)
8309 it->what = IT_COMPOSITION;
8310 it->len = it->cmp_it.nbytes;
8311 if (STRINGP (it->string))
8313 if (it->c < 0)
8315 IT_STRING_CHARPOS (*it) += it->cmp_it.nchars;
8316 IT_STRING_BYTEPOS (*it) += it->cmp_it.nbytes;
8317 return 0;
8319 it->position = it->current.string_pos;
8320 it->object = it->string;
8321 it->c = composition_update_it (&it->cmp_it, IT_STRING_CHARPOS (*it),
8322 IT_STRING_BYTEPOS (*it), it->string);
8324 else
8326 if (it->c < 0)
8328 IT_CHARPOS (*it) += it->cmp_it.nchars;
8329 IT_BYTEPOS (*it) += it->cmp_it.nbytes;
8330 if (it->bidi_p)
8332 if (it->bidi_it.new_paragraph)
8333 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 0);
8334 /* Resync the bidi iterator with IT's new position.
8335 FIXME: this doesn't support bidirectional text. */
8336 while (it->bidi_it.charpos < IT_CHARPOS (*it))
8337 bidi_move_to_visually_next (&it->bidi_it);
8339 return 0;
8341 it->position = it->current.pos;
8342 it->object = it->w->contents;
8343 it->c = composition_update_it (&it->cmp_it, IT_CHARPOS (*it),
8344 IT_BYTEPOS (*it), Qnil);
8346 return 1;
8351 /***********************************************************************
8352 Moving an iterator without producing glyphs
8353 ***********************************************************************/
8355 /* Check if iterator is at a position corresponding to a valid buffer
8356 position after some move_it_ call. */
8358 #define IT_POS_VALID_AFTER_MOVE_P(it) \
8359 ((it)->method == GET_FROM_STRING \
8360 ? IT_STRING_CHARPOS (*it) == 0 \
8361 : 1)
8364 /* Move iterator IT to a specified buffer or X position within one
8365 line on the display without producing glyphs.
8367 OP should be a bit mask including some or all of these bits:
8368 MOVE_TO_X: Stop upon reaching x-position TO_X.
8369 MOVE_TO_POS: Stop upon reaching buffer or string position TO_CHARPOS.
8370 Regardless of OP's value, stop upon reaching the end of the display line.
8372 TO_X is normally a value 0 <= TO_X <= IT->last_visible_x.
8373 This means, in particular, that TO_X includes window's horizontal
8374 scroll amount.
8376 The return value has several possible values that
8377 say what condition caused the scan to stop:
8379 MOVE_POS_MATCH_OR_ZV
8380 - when TO_POS or ZV was reached.
8382 MOVE_X_REACHED
8383 -when TO_X was reached before TO_POS or ZV were reached.
8385 MOVE_LINE_CONTINUED
8386 - when we reached the end of the display area and the line must
8387 be continued.
8389 MOVE_LINE_TRUNCATED
8390 - when we reached the end of the display area and the line is
8391 truncated.
8393 MOVE_NEWLINE_OR_CR
8394 - when we stopped at a line end, i.e. a newline or a CR and selective
8395 display is on. */
8397 static enum move_it_result
8398 move_it_in_display_line_to (struct it *it,
8399 ptrdiff_t to_charpos, int to_x,
8400 enum move_operation_enum op)
8402 enum move_it_result result = MOVE_UNDEFINED;
8403 struct glyph_row *saved_glyph_row;
8404 struct it wrap_it, atpos_it, atx_it, ppos_it;
8405 void *wrap_data = NULL, *atpos_data = NULL, *atx_data = NULL;
8406 void *ppos_data = NULL;
8407 int may_wrap = 0;
8408 enum it_method prev_method = it->method;
8409 ptrdiff_t closest_pos IF_LINT (= 0), prev_pos = IT_CHARPOS (*it);
8410 int saw_smaller_pos = prev_pos < to_charpos;
8412 /* Don't produce glyphs in produce_glyphs. */
8413 saved_glyph_row = it->glyph_row;
8414 it->glyph_row = NULL;
8416 /* Use wrap_it to save a copy of IT wherever a word wrap could
8417 occur. Use atpos_it to save a copy of IT at the desired buffer
8418 position, if found, so that we can scan ahead and check if the
8419 word later overshoots the window edge. Use atx_it similarly, for
8420 pixel positions. */
8421 wrap_it.sp = -1;
8422 atpos_it.sp = -1;
8423 atx_it.sp = -1;
8425 /* Use ppos_it under bidi reordering to save a copy of IT for the
8426 initial position. We restore that position in IT when we have
8427 scanned the entire display line without finding a match for
8428 TO_CHARPOS and all the character positions are greater than
8429 TO_CHARPOS. We then restart the scan from the initial position,
8430 and stop at CLOSEST_POS, which is a position > TO_CHARPOS that is
8431 the closest to TO_CHARPOS. */
8432 if (it->bidi_p)
8434 if ((op & MOVE_TO_POS) && IT_CHARPOS (*it) >= to_charpos)
8436 SAVE_IT (ppos_it, *it, ppos_data);
8437 closest_pos = IT_CHARPOS (*it);
8439 else
8440 closest_pos = ZV;
8443 #define BUFFER_POS_REACHED_P() \
8444 ((op & MOVE_TO_POS) != 0 \
8445 && BUFFERP (it->object) \
8446 && (IT_CHARPOS (*it) == to_charpos \
8447 || ((!it->bidi_p \
8448 || BIDI_AT_BASE_LEVEL (it->bidi_it)) \
8449 && IT_CHARPOS (*it) > to_charpos) \
8450 || (it->what == IT_COMPOSITION \
8451 && ((IT_CHARPOS (*it) > to_charpos \
8452 && to_charpos >= it->cmp_it.charpos) \
8453 || (IT_CHARPOS (*it) < to_charpos \
8454 && to_charpos <= it->cmp_it.charpos)))) \
8455 && (it->method == GET_FROM_BUFFER \
8456 || (it->method == GET_FROM_DISPLAY_VECTOR \
8457 && it->dpvec + it->current.dpvec_index + 1 >= it->dpend)))
8459 /* If there's a line-/wrap-prefix, handle it. */
8460 if (it->hpos == 0 && it->method == GET_FROM_BUFFER
8461 && it->current_y < it->last_visible_y)
8462 handle_line_prefix (it);
8464 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8465 SET_TEXT_POS (this_line_min_pos, IT_CHARPOS (*it), IT_BYTEPOS (*it));
8467 while (1)
8469 int x, i, ascent = 0, descent = 0;
8471 /* Utility macro to reset an iterator with x, ascent, and descent. */
8472 #define IT_RESET_X_ASCENT_DESCENT(IT) \
8473 ((IT)->current_x = x, (IT)->max_ascent = ascent, \
8474 (IT)->max_descent = descent)
8476 /* Stop if we move beyond TO_CHARPOS (after an image or a
8477 display string or stretch glyph). */
8478 if ((op & MOVE_TO_POS) != 0
8479 && BUFFERP (it->object)
8480 && it->method == GET_FROM_BUFFER
8481 && (((!it->bidi_p
8482 /* When the iterator is at base embedding level, we
8483 are guaranteed that characters are delivered for
8484 display in strictly increasing order of their
8485 buffer positions. */
8486 || BIDI_AT_BASE_LEVEL (it->bidi_it))
8487 && IT_CHARPOS (*it) > to_charpos)
8488 || (it->bidi_p
8489 && (prev_method == GET_FROM_IMAGE
8490 || prev_method == GET_FROM_STRETCH
8491 || prev_method == GET_FROM_STRING)
8492 /* Passed TO_CHARPOS from left to right. */
8493 && ((prev_pos < to_charpos
8494 && IT_CHARPOS (*it) > to_charpos)
8495 /* Passed TO_CHARPOS from right to left. */
8496 || (prev_pos > to_charpos
8497 && IT_CHARPOS (*it) < to_charpos)))))
8499 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8501 result = MOVE_POS_MATCH_OR_ZV;
8502 break;
8504 else if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
8505 /* If wrap_it is valid, the current position might be in a
8506 word that is wrapped. So, save the iterator in
8507 atpos_it and continue to see if wrapping happens. */
8508 SAVE_IT (atpos_it, *it, atpos_data);
8511 /* Stop when ZV reached.
8512 We used to stop here when TO_CHARPOS reached as well, but that is
8513 too soon if this glyph does not fit on this line. So we handle it
8514 explicitly below. */
8515 if (!get_next_display_element (it))
8517 result = MOVE_POS_MATCH_OR_ZV;
8518 break;
8521 if (it->line_wrap == TRUNCATE)
8523 if (BUFFER_POS_REACHED_P ())
8525 result = MOVE_POS_MATCH_OR_ZV;
8526 break;
8529 else
8531 if (it->line_wrap == WORD_WRAP)
8533 if (IT_DISPLAYING_WHITESPACE (it))
8534 may_wrap = 1;
8535 else if (may_wrap)
8537 /* We have reached a glyph that follows one or more
8538 whitespace characters. If the position is
8539 already found, we are done. */
8540 if (atpos_it.sp >= 0)
8542 RESTORE_IT (it, &atpos_it, atpos_data);
8543 result = MOVE_POS_MATCH_OR_ZV;
8544 goto done;
8546 if (atx_it.sp >= 0)
8548 RESTORE_IT (it, &atx_it, atx_data);
8549 result = MOVE_X_REACHED;
8550 goto done;
8552 /* Otherwise, we can wrap here. */
8553 SAVE_IT (wrap_it, *it, wrap_data);
8554 may_wrap = 0;
8559 /* Remember the line height for the current line, in case
8560 the next element doesn't fit on the line. */
8561 ascent = it->max_ascent;
8562 descent = it->max_descent;
8564 /* The call to produce_glyphs will get the metrics of the
8565 display element IT is loaded with. Record the x-position
8566 before this display element, in case it doesn't fit on the
8567 line. */
8568 x = it->current_x;
8570 PRODUCE_GLYPHS (it);
8572 if (it->area != TEXT_AREA)
8574 prev_method = it->method;
8575 if (it->method == GET_FROM_BUFFER)
8576 prev_pos = IT_CHARPOS (*it);
8577 set_iterator_to_next (it, 1);
8578 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8579 SET_TEXT_POS (this_line_min_pos,
8580 IT_CHARPOS (*it), IT_BYTEPOS (*it));
8581 if (it->bidi_p
8582 && (op & MOVE_TO_POS)
8583 && IT_CHARPOS (*it) > to_charpos
8584 && IT_CHARPOS (*it) < closest_pos)
8585 closest_pos = IT_CHARPOS (*it);
8586 continue;
8589 /* The number of glyphs we get back in IT->nglyphs will normally
8590 be 1 except when IT->c is (i) a TAB, or (ii) a multi-glyph
8591 character on a terminal frame, or (iii) a line end. For the
8592 second case, IT->nglyphs - 1 padding glyphs will be present.
8593 (On X frames, there is only one glyph produced for a
8594 composite character.)
8596 The behavior implemented below means, for continuation lines,
8597 that as many spaces of a TAB as fit on the current line are
8598 displayed there. For terminal frames, as many glyphs of a
8599 multi-glyph character are displayed in the current line, too.
8600 This is what the old redisplay code did, and we keep it that
8601 way. Under X, the whole shape of a complex character must
8602 fit on the line or it will be completely displayed in the
8603 next line.
8605 Note that both for tabs and padding glyphs, all glyphs have
8606 the same width. */
8607 if (it->nglyphs)
8609 /* More than one glyph or glyph doesn't fit on line. All
8610 glyphs have the same width. */
8611 int single_glyph_width = it->pixel_width / it->nglyphs;
8612 int new_x;
8613 int x_before_this_char = x;
8614 int hpos_before_this_char = it->hpos;
8616 for (i = 0; i < it->nglyphs; ++i, x = new_x)
8618 new_x = x + single_glyph_width;
8620 /* We want to leave anything reaching TO_X to the caller. */
8621 if ((op & MOVE_TO_X) && new_x > to_x)
8623 if (BUFFER_POS_REACHED_P ())
8625 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8626 goto buffer_pos_reached;
8627 if (atpos_it.sp < 0)
8629 SAVE_IT (atpos_it, *it, atpos_data);
8630 IT_RESET_X_ASCENT_DESCENT (&atpos_it);
8633 else
8635 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8637 it->current_x = x;
8638 result = MOVE_X_REACHED;
8639 break;
8641 if (atx_it.sp < 0)
8643 SAVE_IT (atx_it, *it, atx_data);
8644 IT_RESET_X_ASCENT_DESCENT (&atx_it);
8649 if (/* Lines are continued. */
8650 it->line_wrap != TRUNCATE
8651 && (/* And glyph doesn't fit on the line. */
8652 new_x > it->last_visible_x
8653 /* Or it fits exactly and we're on a window
8654 system frame. */
8655 || (new_x == it->last_visible_x
8656 && FRAME_WINDOW_P (it->f)
8657 && ((it->bidi_p && it->bidi_it.paragraph_dir == R2L)
8658 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
8659 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)))))
8661 if (/* IT->hpos == 0 means the very first glyph
8662 doesn't fit on the line, e.g. a wide image. */
8663 it->hpos == 0
8664 || (new_x == it->last_visible_x
8665 && FRAME_WINDOW_P (it->f)
8666 /* When word-wrap is ON and we have a valid
8667 wrap point, we don't allow the last glyph
8668 to "just barely fit" on the line. */
8669 && (it->line_wrap != WORD_WRAP
8670 || wrap_it.sp < 0)))
8672 ++it->hpos;
8673 it->current_x = new_x;
8675 /* The character's last glyph just barely fits
8676 in this row. */
8677 if (i == it->nglyphs - 1)
8679 /* If this is the destination position,
8680 return a position *before* it in this row,
8681 now that we know it fits in this row. */
8682 if (BUFFER_POS_REACHED_P ())
8684 if (it->line_wrap != WORD_WRAP
8685 || wrap_it.sp < 0)
8687 it->hpos = hpos_before_this_char;
8688 it->current_x = x_before_this_char;
8689 result = MOVE_POS_MATCH_OR_ZV;
8690 break;
8692 if (it->line_wrap == WORD_WRAP
8693 && atpos_it.sp < 0)
8695 SAVE_IT (atpos_it, *it, atpos_data);
8696 atpos_it.current_x = x_before_this_char;
8697 atpos_it.hpos = hpos_before_this_char;
8701 prev_method = it->method;
8702 if (it->method == GET_FROM_BUFFER)
8703 prev_pos = IT_CHARPOS (*it);
8704 set_iterator_to_next (it, 1);
8705 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8706 SET_TEXT_POS (this_line_min_pos,
8707 IT_CHARPOS (*it), IT_BYTEPOS (*it));
8708 /* On graphical terminals, newlines may
8709 "overflow" into the fringe if
8710 overflow-newline-into-fringe is non-nil.
8711 On text terminals, and on graphical
8712 terminals with no right margin, newlines
8713 may overflow into the last glyph on the
8714 display line.*/
8715 if (!FRAME_WINDOW_P (it->f)
8716 || ((it->bidi_p
8717 && it->bidi_it.paragraph_dir == R2L)
8718 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
8719 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0
8720 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
8722 if (!get_next_display_element (it))
8724 result = MOVE_POS_MATCH_OR_ZV;
8725 break;
8727 if (BUFFER_POS_REACHED_P ())
8729 if (ITERATOR_AT_END_OF_LINE_P (it))
8730 result = MOVE_POS_MATCH_OR_ZV;
8731 else
8732 result = MOVE_LINE_CONTINUED;
8733 break;
8735 if (ITERATOR_AT_END_OF_LINE_P (it)
8736 && (it->line_wrap != WORD_WRAP
8737 || wrap_it.sp < 0))
8739 result = MOVE_NEWLINE_OR_CR;
8740 break;
8745 else
8746 IT_RESET_X_ASCENT_DESCENT (it);
8748 if (wrap_it.sp >= 0)
8750 RESTORE_IT (it, &wrap_it, wrap_data);
8751 atpos_it.sp = -1;
8752 atx_it.sp = -1;
8755 TRACE_MOVE ((stderr, "move_it_in: continued at %d\n",
8756 IT_CHARPOS (*it)));
8757 result = MOVE_LINE_CONTINUED;
8758 break;
8761 if (BUFFER_POS_REACHED_P ())
8763 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8764 goto buffer_pos_reached;
8765 if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
8767 SAVE_IT (atpos_it, *it, atpos_data);
8768 IT_RESET_X_ASCENT_DESCENT (&atpos_it);
8772 if (new_x > it->first_visible_x)
8774 /* Glyph is visible. Increment number of glyphs that
8775 would be displayed. */
8776 ++it->hpos;
8780 if (result != MOVE_UNDEFINED)
8781 break;
8783 else if (BUFFER_POS_REACHED_P ())
8785 buffer_pos_reached:
8786 IT_RESET_X_ASCENT_DESCENT (it);
8787 result = MOVE_POS_MATCH_OR_ZV;
8788 break;
8790 else if ((op & MOVE_TO_X) && it->current_x >= to_x)
8792 /* Stop when TO_X specified and reached. This check is
8793 necessary here because of lines consisting of a line end,
8794 only. The line end will not produce any glyphs and we
8795 would never get MOVE_X_REACHED. */
8796 eassert (it->nglyphs == 0);
8797 result = MOVE_X_REACHED;
8798 break;
8801 /* Is this a line end? If yes, we're done. */
8802 if (ITERATOR_AT_END_OF_LINE_P (it))
8804 /* If we are past TO_CHARPOS, but never saw any character
8805 positions smaller than TO_CHARPOS, return
8806 MOVE_POS_MATCH_OR_ZV, like the unidirectional display
8807 did. */
8808 if (it->bidi_p && (op & MOVE_TO_POS) != 0)
8810 if (!saw_smaller_pos && IT_CHARPOS (*it) > to_charpos)
8812 if (closest_pos < ZV)
8814 RESTORE_IT (it, &ppos_it, ppos_data);
8815 move_it_in_display_line_to (it, closest_pos, -1,
8816 MOVE_TO_POS);
8817 result = MOVE_POS_MATCH_OR_ZV;
8819 else
8820 goto buffer_pos_reached;
8822 else if (it->line_wrap == WORD_WRAP && atpos_it.sp >= 0
8823 && IT_CHARPOS (*it) > to_charpos)
8824 goto buffer_pos_reached;
8825 else
8826 result = MOVE_NEWLINE_OR_CR;
8828 else
8829 result = MOVE_NEWLINE_OR_CR;
8830 break;
8833 prev_method = it->method;
8834 if (it->method == GET_FROM_BUFFER)
8835 prev_pos = IT_CHARPOS (*it);
8836 /* The current display element has been consumed. Advance
8837 to the next. */
8838 set_iterator_to_next (it, 1);
8839 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8840 SET_TEXT_POS (this_line_min_pos, IT_CHARPOS (*it), IT_BYTEPOS (*it));
8841 if (IT_CHARPOS (*it) < to_charpos)
8842 saw_smaller_pos = 1;
8843 if (it->bidi_p
8844 && (op & MOVE_TO_POS)
8845 && IT_CHARPOS (*it) >= to_charpos
8846 && IT_CHARPOS (*it) < closest_pos)
8847 closest_pos = IT_CHARPOS (*it);
8849 /* Stop if lines are truncated and IT's current x-position is
8850 past the right edge of the window now. */
8851 if (it->line_wrap == TRUNCATE
8852 && it->current_x >= it->last_visible_x)
8854 if (!FRAME_WINDOW_P (it->f)
8855 || ((it->bidi_p && it->bidi_it.paragraph_dir == R2L)
8856 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
8857 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0
8858 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
8860 int at_eob_p = 0;
8862 if ((at_eob_p = !get_next_display_element (it))
8863 || BUFFER_POS_REACHED_P ()
8864 /* If we are past TO_CHARPOS, but never saw any
8865 character positions smaller than TO_CHARPOS,
8866 return MOVE_POS_MATCH_OR_ZV, like the
8867 unidirectional display did. */
8868 || (it->bidi_p && (op & MOVE_TO_POS) != 0
8869 && !saw_smaller_pos
8870 && IT_CHARPOS (*it) > to_charpos))
8872 if (it->bidi_p
8873 && !BUFFER_POS_REACHED_P ()
8874 && !at_eob_p && closest_pos < ZV)
8876 RESTORE_IT (it, &ppos_it, ppos_data);
8877 move_it_in_display_line_to (it, closest_pos, -1,
8878 MOVE_TO_POS);
8880 result = MOVE_POS_MATCH_OR_ZV;
8881 break;
8883 if (ITERATOR_AT_END_OF_LINE_P (it))
8885 result = MOVE_NEWLINE_OR_CR;
8886 break;
8889 else if (it->bidi_p && (op & MOVE_TO_POS) != 0
8890 && !saw_smaller_pos
8891 && IT_CHARPOS (*it) > to_charpos)
8893 if (closest_pos < ZV)
8895 RESTORE_IT (it, &ppos_it, ppos_data);
8896 move_it_in_display_line_to (it, closest_pos, -1, MOVE_TO_POS);
8898 result = MOVE_POS_MATCH_OR_ZV;
8899 break;
8901 result = MOVE_LINE_TRUNCATED;
8902 break;
8904 #undef IT_RESET_X_ASCENT_DESCENT
8907 #undef BUFFER_POS_REACHED_P
8909 /* If we scanned beyond to_pos and didn't find a point to wrap at,
8910 restore the saved iterator. */
8911 if (atpos_it.sp >= 0)
8912 RESTORE_IT (it, &atpos_it, atpos_data);
8913 else if (atx_it.sp >= 0)
8914 RESTORE_IT (it, &atx_it, atx_data);
8916 done:
8918 if (atpos_data)
8919 bidi_unshelve_cache (atpos_data, 1);
8920 if (atx_data)
8921 bidi_unshelve_cache (atx_data, 1);
8922 if (wrap_data)
8923 bidi_unshelve_cache (wrap_data, 1);
8924 if (ppos_data)
8925 bidi_unshelve_cache (ppos_data, 1);
8927 /* Restore the iterator settings altered at the beginning of this
8928 function. */
8929 it->glyph_row = saved_glyph_row;
8930 return result;
8933 /* For external use. */
8934 void
8935 move_it_in_display_line (struct it *it,
8936 ptrdiff_t to_charpos, int to_x,
8937 enum move_operation_enum op)
8939 if (it->line_wrap == WORD_WRAP
8940 && (op & MOVE_TO_X))
8942 struct it save_it;
8943 void *save_data = NULL;
8944 int skip;
8946 SAVE_IT (save_it, *it, save_data);
8947 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
8948 /* When word-wrap is on, TO_X may lie past the end
8949 of a wrapped line. Then it->current is the
8950 character on the next line, so backtrack to the
8951 space before the wrap point. */
8952 if (skip == MOVE_LINE_CONTINUED)
8954 int prev_x = max (it->current_x - 1, 0);
8955 RESTORE_IT (it, &save_it, save_data);
8956 move_it_in_display_line_to
8957 (it, -1, prev_x, MOVE_TO_X);
8959 else
8960 bidi_unshelve_cache (save_data, 1);
8962 else
8963 move_it_in_display_line_to (it, to_charpos, to_x, op);
8967 /* Move IT forward until it satisfies one or more of the criteria in
8968 TO_CHARPOS, TO_X, TO_Y, and TO_VPOS.
8970 OP is a bit-mask that specifies where to stop, and in particular,
8971 which of those four position arguments makes a difference. See the
8972 description of enum move_operation_enum.
8974 If TO_CHARPOS is in invisible text, e.g. a truncated part of a
8975 screen line, this function will set IT to the next position that is
8976 displayed to the right of TO_CHARPOS on the screen.
8978 Return the maximum pixel length of any line scanned but never more
8979 than it.last_visible_x. */
8982 move_it_to (struct it *it, ptrdiff_t to_charpos, int to_x, int to_y, int to_vpos, int op)
8984 enum move_it_result skip, skip2 = MOVE_X_REACHED;
8985 int line_height, line_start_x = 0, reached = 0;
8986 int max_current_x = 0;
8987 void *backup_data = NULL;
8989 for (;;)
8991 if (op & MOVE_TO_VPOS)
8993 /* If no TO_CHARPOS and no TO_X specified, stop at the
8994 start of the line TO_VPOS. */
8995 if ((op & (MOVE_TO_X | MOVE_TO_POS)) == 0)
8997 if (it->vpos == to_vpos)
8999 reached = 1;
9000 break;
9002 else
9003 skip = move_it_in_display_line_to (it, -1, -1, 0);
9005 else
9007 /* TO_VPOS >= 0 means stop at TO_X in the line at
9008 TO_VPOS, or at TO_POS, whichever comes first. */
9009 if (it->vpos == to_vpos)
9011 reached = 2;
9012 break;
9015 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
9017 if (skip == MOVE_POS_MATCH_OR_ZV || it->vpos == to_vpos)
9019 reached = 3;
9020 break;
9022 else if (skip == MOVE_X_REACHED && it->vpos != to_vpos)
9024 /* We have reached TO_X but not in the line we want. */
9025 skip = move_it_in_display_line_to (it, to_charpos,
9026 -1, MOVE_TO_POS);
9027 if (skip == MOVE_POS_MATCH_OR_ZV)
9029 reached = 4;
9030 break;
9035 else if (op & MOVE_TO_Y)
9037 struct it it_backup;
9039 if (it->line_wrap == WORD_WRAP)
9040 SAVE_IT (it_backup, *it, backup_data);
9042 /* TO_Y specified means stop at TO_X in the line containing
9043 TO_Y---or at TO_CHARPOS if this is reached first. The
9044 problem is that we can't really tell whether the line
9045 contains TO_Y before we have completely scanned it, and
9046 this may skip past TO_X. What we do is to first scan to
9047 TO_X.
9049 If TO_X is not specified, use a TO_X of zero. The reason
9050 is to make the outcome of this function more predictable.
9051 If we didn't use TO_X == 0, we would stop at the end of
9052 the line which is probably not what a caller would expect
9053 to happen. */
9054 skip = move_it_in_display_line_to
9055 (it, to_charpos, ((op & MOVE_TO_X) ? to_x : 0),
9056 (MOVE_TO_X | (op & MOVE_TO_POS)));
9058 /* If TO_CHARPOS is reached or ZV, we don't have to do more. */
9059 if (skip == MOVE_POS_MATCH_OR_ZV)
9060 reached = 5;
9061 else if (skip == MOVE_X_REACHED)
9063 /* If TO_X was reached, we want to know whether TO_Y is
9064 in the line. We know this is the case if the already
9065 scanned glyphs make the line tall enough. Otherwise,
9066 we must check by scanning the rest of the line. */
9067 line_height = it->max_ascent + it->max_descent;
9068 if (to_y >= it->current_y
9069 && to_y < it->current_y + line_height)
9071 reached = 6;
9072 break;
9074 SAVE_IT (it_backup, *it, backup_data);
9075 TRACE_MOVE ((stderr, "move_it: from %d\n", IT_CHARPOS (*it)));
9076 skip2 = move_it_in_display_line_to (it, to_charpos, -1,
9077 op & MOVE_TO_POS);
9078 TRACE_MOVE ((stderr, "move_it: to %d\n", IT_CHARPOS (*it)));
9079 line_height = it->max_ascent + it->max_descent;
9080 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
9082 if (to_y >= it->current_y
9083 && to_y < it->current_y + line_height)
9085 /* If TO_Y is in this line and TO_X was reached
9086 above, we scanned too far. We have to restore
9087 IT's settings to the ones before skipping. But
9088 keep the more accurate values of max_ascent and
9089 max_descent we've found while skipping the rest
9090 of the line, for the sake of callers, such as
9091 pos_visible_p, that need to know the line
9092 height. */
9093 int max_ascent = it->max_ascent;
9094 int max_descent = it->max_descent;
9096 RESTORE_IT (it, &it_backup, backup_data);
9097 it->max_ascent = max_ascent;
9098 it->max_descent = max_descent;
9099 reached = 6;
9101 else
9103 skip = skip2;
9104 if (skip == MOVE_POS_MATCH_OR_ZV)
9105 reached = 7;
9108 else
9110 /* Check whether TO_Y is in this line. */
9111 line_height = it->max_ascent + it->max_descent;
9112 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
9114 if (to_y >= it->current_y
9115 && to_y < it->current_y + line_height)
9117 if (to_y > it->current_y)
9118 max_current_x = max (it->current_x, max_current_x);
9120 /* When word-wrap is on, TO_X may lie past the end
9121 of a wrapped line. Then it->current is the
9122 character on the next line, so backtrack to the
9123 space before the wrap point. */
9124 if (skip == MOVE_LINE_CONTINUED
9125 && it->line_wrap == WORD_WRAP)
9127 int prev_x = max (it->current_x - 1, 0);
9128 RESTORE_IT (it, &it_backup, backup_data);
9129 skip = move_it_in_display_line_to
9130 (it, -1, prev_x, MOVE_TO_X);
9133 reached = 6;
9137 if (reached)
9139 max_current_x = max (it->current_x, max_current_x);
9140 break;
9143 else if (BUFFERP (it->object)
9144 && (it->method == GET_FROM_BUFFER
9145 || it->method == GET_FROM_STRETCH)
9146 && IT_CHARPOS (*it) >= to_charpos
9147 /* Under bidi iteration, a call to set_iterator_to_next
9148 can scan far beyond to_charpos if the initial
9149 portion of the next line needs to be reordered. In
9150 that case, give move_it_in_display_line_to another
9151 chance below. */
9152 && !(it->bidi_p
9153 && it->bidi_it.scan_dir == -1))
9154 skip = MOVE_POS_MATCH_OR_ZV;
9155 else
9156 skip = move_it_in_display_line_to (it, to_charpos, -1, MOVE_TO_POS);
9158 switch (skip)
9160 case MOVE_POS_MATCH_OR_ZV:
9161 max_current_x = max (it->current_x, max_current_x);
9162 reached = 8;
9163 goto out;
9165 case MOVE_NEWLINE_OR_CR:
9166 max_current_x = max (it->current_x, max_current_x);
9167 set_iterator_to_next (it, 1);
9168 it->continuation_lines_width = 0;
9169 break;
9171 case MOVE_LINE_TRUNCATED:
9172 max_current_x = it->last_visible_x;
9173 it->continuation_lines_width = 0;
9174 reseat_at_next_visible_line_start (it, 0);
9175 if ((op & MOVE_TO_POS) != 0
9176 && IT_CHARPOS (*it) > to_charpos)
9178 reached = 9;
9179 goto out;
9181 break;
9183 case MOVE_LINE_CONTINUED:
9184 max_current_x = it->last_visible_x;
9185 /* For continued lines ending in a tab, some of the glyphs
9186 associated with the tab are displayed on the current
9187 line. Since it->current_x does not include these glyphs,
9188 we use it->last_visible_x instead. */
9189 if (it->c == '\t')
9191 it->continuation_lines_width += it->last_visible_x;
9192 /* When moving by vpos, ensure that the iterator really
9193 advances to the next line (bug#847, bug#969). Fixme:
9194 do we need to do this in other circumstances? */
9195 if (it->current_x != it->last_visible_x
9196 && (op & MOVE_TO_VPOS)
9197 && !(op & (MOVE_TO_X | MOVE_TO_POS)))
9199 line_start_x = it->current_x + it->pixel_width
9200 - it->last_visible_x;
9201 set_iterator_to_next (it, 0);
9204 else
9205 it->continuation_lines_width += it->current_x;
9206 break;
9208 default:
9209 emacs_abort ();
9212 /* Reset/increment for the next run. */
9213 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
9214 it->current_x = line_start_x;
9215 line_start_x = 0;
9216 it->hpos = 0;
9217 it->current_y += it->max_ascent + it->max_descent;
9218 ++it->vpos;
9219 last_height = it->max_ascent + it->max_descent;
9220 it->max_ascent = it->max_descent = 0;
9223 out:
9225 /* On text terminals, we may stop at the end of a line in the middle
9226 of a multi-character glyph. If the glyph itself is continued,
9227 i.e. it is actually displayed on the next line, don't treat this
9228 stopping point as valid; move to the next line instead (unless
9229 that brings us offscreen). */
9230 if (!FRAME_WINDOW_P (it->f)
9231 && op & MOVE_TO_POS
9232 && IT_CHARPOS (*it) == to_charpos
9233 && it->what == IT_CHARACTER
9234 && it->nglyphs > 1
9235 && it->line_wrap == WINDOW_WRAP
9236 && it->current_x == it->last_visible_x - 1
9237 && it->c != '\n'
9238 && it->c != '\t'
9239 && it->vpos < it->w->window_end_vpos)
9241 it->continuation_lines_width += it->current_x;
9242 it->current_x = it->hpos = it->max_ascent = it->max_descent = 0;
9243 it->current_y += it->max_ascent + it->max_descent;
9244 ++it->vpos;
9245 last_height = it->max_ascent + it->max_descent;
9248 if (backup_data)
9249 bidi_unshelve_cache (backup_data, 1);
9251 TRACE_MOVE ((stderr, "move_it_to: reached %d\n", reached));
9253 return max_current_x;
9257 /* Move iterator IT backward by a specified y-distance DY, DY >= 0.
9259 If DY > 0, move IT backward at least that many pixels. DY = 0
9260 means move IT backward to the preceding line start or BEGV. This
9261 function may move over more than DY pixels if IT->current_y - DY
9262 ends up in the middle of a line; in this case IT->current_y will be
9263 set to the top of the line moved to. */
9265 void
9266 move_it_vertically_backward (struct it *it, int dy)
9268 int nlines, h;
9269 struct it it2, it3;
9270 void *it2data = NULL, *it3data = NULL;
9271 ptrdiff_t start_pos;
9272 int nchars_per_row
9273 = (it->last_visible_x - it->first_visible_x) / FRAME_COLUMN_WIDTH (it->f);
9274 ptrdiff_t pos_limit;
9276 move_further_back:
9277 eassert (dy >= 0);
9279 start_pos = IT_CHARPOS (*it);
9281 /* Estimate how many newlines we must move back. */
9282 nlines = max (1, dy / default_line_pixel_height (it->w));
9283 if (it->line_wrap == TRUNCATE)
9284 pos_limit = BEGV;
9285 else
9286 pos_limit = max (start_pos - nlines * nchars_per_row, BEGV);
9288 /* Set the iterator's position that many lines back. But don't go
9289 back more than NLINES full screen lines -- this wins a day with
9290 buffers which have very long lines. */
9291 while (nlines-- && IT_CHARPOS (*it) > pos_limit)
9292 back_to_previous_visible_line_start (it);
9294 /* Reseat the iterator here. When moving backward, we don't want
9295 reseat to skip forward over invisible text, set up the iterator
9296 to deliver from overlay strings at the new position etc. So,
9297 use reseat_1 here. */
9298 reseat_1 (it, it->current.pos, 1);
9300 /* We are now surely at a line start. */
9301 it->current_x = it->hpos = 0; /* FIXME: this is incorrect when bidi
9302 reordering is in effect. */
9303 it->continuation_lines_width = 0;
9305 /* Move forward and see what y-distance we moved. First move to the
9306 start of the next line so that we get its height. We need this
9307 height to be able to tell whether we reached the specified
9308 y-distance. */
9309 SAVE_IT (it2, *it, it2data);
9310 it2.max_ascent = it2.max_descent = 0;
9313 move_it_to (&it2, start_pos, -1, -1, it2.vpos + 1,
9314 MOVE_TO_POS | MOVE_TO_VPOS);
9316 while (!(IT_POS_VALID_AFTER_MOVE_P (&it2)
9317 /* If we are in a display string which starts at START_POS,
9318 and that display string includes a newline, and we are
9319 right after that newline (i.e. at the beginning of a
9320 display line), exit the loop, because otherwise we will
9321 infloop, since move_it_to will see that it is already at
9322 START_POS and will not move. */
9323 || (it2.method == GET_FROM_STRING
9324 && IT_CHARPOS (it2) == start_pos
9325 && SREF (it2.string, IT_STRING_BYTEPOS (it2) - 1) == '\n')));
9326 eassert (IT_CHARPOS (*it) >= BEGV);
9327 SAVE_IT (it3, it2, it3data);
9329 move_it_to (&it2, start_pos, -1, -1, -1, MOVE_TO_POS);
9330 eassert (IT_CHARPOS (*it) >= BEGV);
9331 /* H is the actual vertical distance from the position in *IT
9332 and the starting position. */
9333 h = it2.current_y - it->current_y;
9334 /* NLINES is the distance in number of lines. */
9335 nlines = it2.vpos - it->vpos;
9337 /* Correct IT's y and vpos position
9338 so that they are relative to the starting point. */
9339 it->vpos -= nlines;
9340 it->current_y -= h;
9342 if (dy == 0)
9344 /* DY == 0 means move to the start of the screen line. The
9345 value of nlines is > 0 if continuation lines were involved,
9346 or if the original IT position was at start of a line. */
9347 RESTORE_IT (it, it, it2data);
9348 if (nlines > 0)
9349 move_it_by_lines (it, nlines);
9350 /* The above code moves us to some position NLINES down,
9351 usually to its first glyph (leftmost in an L2R line), but
9352 that's not necessarily the start of the line, under bidi
9353 reordering. We want to get to the character position
9354 that is immediately after the newline of the previous
9355 line. */
9356 if (it->bidi_p
9357 && !it->continuation_lines_width
9358 && !STRINGP (it->string)
9359 && IT_CHARPOS (*it) > BEGV
9360 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
9362 ptrdiff_t cp = IT_CHARPOS (*it), bp = IT_BYTEPOS (*it);
9364 DEC_BOTH (cp, bp);
9365 cp = find_newline_no_quit (cp, bp, -1, NULL);
9366 move_it_to (it, cp, -1, -1, -1, MOVE_TO_POS);
9368 bidi_unshelve_cache (it3data, 1);
9370 else
9372 /* The y-position we try to reach, relative to *IT.
9373 Note that H has been subtracted in front of the if-statement. */
9374 int target_y = it->current_y + h - dy;
9375 int y0 = it3.current_y;
9376 int y1;
9377 int line_height;
9379 RESTORE_IT (&it3, &it3, it3data);
9380 y1 = line_bottom_y (&it3);
9381 line_height = y1 - y0;
9382 RESTORE_IT (it, it, it2data);
9383 /* If we did not reach target_y, try to move further backward if
9384 we can. If we moved too far backward, try to move forward. */
9385 if (target_y < it->current_y
9386 /* This is heuristic. In a window that's 3 lines high, with
9387 a line height of 13 pixels each, recentering with point
9388 on the bottom line will try to move -39/2 = 19 pixels
9389 backward. Try to avoid moving into the first line. */
9390 && (it->current_y - target_y
9391 > min (window_box_height (it->w), line_height * 2 / 3))
9392 && IT_CHARPOS (*it) > BEGV)
9394 TRACE_MOVE ((stderr, " not far enough -> move_vert %d\n",
9395 target_y - it->current_y));
9396 dy = it->current_y - target_y;
9397 goto move_further_back;
9399 else if (target_y >= it->current_y + line_height
9400 && IT_CHARPOS (*it) < ZV)
9402 /* Should move forward by at least one line, maybe more.
9404 Note: Calling move_it_by_lines can be expensive on
9405 terminal frames, where compute_motion is used (via
9406 vmotion) to do the job, when there are very long lines
9407 and truncate-lines is nil. That's the reason for
9408 treating terminal frames specially here. */
9410 if (!FRAME_WINDOW_P (it->f))
9411 move_it_vertically (it, target_y - (it->current_y + line_height));
9412 else
9416 move_it_by_lines (it, 1);
9418 while (target_y >= line_bottom_y (it) && IT_CHARPOS (*it) < ZV);
9425 /* Move IT by a specified amount of pixel lines DY. DY negative means
9426 move backwards. DY = 0 means move to start of screen line. At the
9427 end, IT will be on the start of a screen line. */
9429 void
9430 move_it_vertically (struct it *it, int dy)
9432 if (dy <= 0)
9433 move_it_vertically_backward (it, -dy);
9434 else
9436 TRACE_MOVE ((stderr, "move_it_v: from %d, %d\n", IT_CHARPOS (*it), dy));
9437 move_it_to (it, ZV, -1, it->current_y + dy, -1,
9438 MOVE_TO_POS | MOVE_TO_Y);
9439 TRACE_MOVE ((stderr, "move_it_v: to %d\n", IT_CHARPOS (*it)));
9441 /* If buffer ends in ZV without a newline, move to the start of
9442 the line to satisfy the post-condition. */
9443 if (IT_CHARPOS (*it) == ZV
9444 && ZV > BEGV
9445 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
9446 move_it_by_lines (it, 0);
9451 /* Move iterator IT past the end of the text line it is in. */
9453 void
9454 move_it_past_eol (struct it *it)
9456 enum move_it_result rc;
9458 rc = move_it_in_display_line_to (it, Z, 0, MOVE_TO_POS);
9459 if (rc == MOVE_NEWLINE_OR_CR)
9460 set_iterator_to_next (it, 0);
9464 /* Move IT by a specified number DVPOS of screen lines down. DVPOS
9465 negative means move up. DVPOS == 0 means move to the start of the
9466 screen line.
9468 Optimization idea: If we would know that IT->f doesn't use
9469 a face with proportional font, we could be faster for
9470 truncate-lines nil. */
9472 void
9473 move_it_by_lines (struct it *it, ptrdiff_t dvpos)
9476 /* The commented-out optimization uses vmotion on terminals. This
9477 gives bad results, because elements like it->what, on which
9478 callers such as pos_visible_p rely, aren't updated. */
9479 /* struct position pos;
9480 if (!FRAME_WINDOW_P (it->f))
9482 struct text_pos textpos;
9484 pos = *vmotion (IT_CHARPOS (*it), dvpos, it->w);
9485 SET_TEXT_POS (textpos, pos.bufpos, pos.bytepos);
9486 reseat (it, textpos, 1);
9487 it->vpos += pos.vpos;
9488 it->current_y += pos.vpos;
9490 else */
9492 if (dvpos == 0)
9494 /* DVPOS == 0 means move to the start of the screen line. */
9495 move_it_vertically_backward (it, 0);
9496 /* Let next call to line_bottom_y calculate real line height. */
9497 last_height = 0;
9499 else if (dvpos > 0)
9501 move_it_to (it, -1, -1, -1, it->vpos + dvpos, MOVE_TO_VPOS);
9502 if (!IT_POS_VALID_AFTER_MOVE_P (it))
9504 /* Only move to the next buffer position if we ended up in a
9505 string from display property, not in an overlay string
9506 (before-string or after-string). That is because the
9507 latter don't conceal the underlying buffer position, so
9508 we can ask to move the iterator to the exact position we
9509 are interested in. Note that, even if we are already at
9510 IT_CHARPOS (*it), the call below is not a no-op, as it
9511 will detect that we are at the end of the string, pop the
9512 iterator, and compute it->current_x and it->hpos
9513 correctly. */
9514 move_it_to (it, IT_CHARPOS (*it) + it->string_from_display_prop_p,
9515 -1, -1, -1, MOVE_TO_POS);
9518 else
9520 struct it it2;
9521 void *it2data = NULL;
9522 ptrdiff_t start_charpos, i;
9523 int nchars_per_row
9524 = (it->last_visible_x - it->first_visible_x) / FRAME_COLUMN_WIDTH (it->f);
9525 bool hit_pos_limit = false;
9526 ptrdiff_t pos_limit;
9528 /* Start at the beginning of the screen line containing IT's
9529 position. This may actually move vertically backwards,
9530 in case of overlays, so adjust dvpos accordingly. */
9531 dvpos += it->vpos;
9532 move_it_vertically_backward (it, 0);
9533 dvpos -= it->vpos;
9535 /* Go back -DVPOS buffer lines, but no farther than -DVPOS full
9536 screen lines, and reseat the iterator there. */
9537 start_charpos = IT_CHARPOS (*it);
9538 if (it->line_wrap == TRUNCATE)
9539 pos_limit = BEGV;
9540 else
9541 pos_limit = max (start_charpos + dvpos * nchars_per_row, BEGV);
9543 for (i = -dvpos; i > 0 && IT_CHARPOS (*it) > pos_limit; --i)
9544 back_to_previous_visible_line_start (it);
9545 if (i > 0 && IT_CHARPOS (*it) <= pos_limit)
9546 hit_pos_limit = true;
9547 reseat (it, it->current.pos, 1);
9549 /* Move further back if we end up in a string or an image. */
9550 while (!IT_POS_VALID_AFTER_MOVE_P (it))
9552 /* First try to move to start of display line. */
9553 dvpos += it->vpos;
9554 move_it_vertically_backward (it, 0);
9555 dvpos -= it->vpos;
9556 if (IT_POS_VALID_AFTER_MOVE_P (it))
9557 break;
9558 /* If start of line is still in string or image,
9559 move further back. */
9560 back_to_previous_visible_line_start (it);
9561 reseat (it, it->current.pos, 1);
9562 dvpos--;
9565 it->current_x = it->hpos = 0;
9567 /* Above call may have moved too far if continuation lines
9568 are involved. Scan forward and see if it did. */
9569 SAVE_IT (it2, *it, it2data);
9570 it2.vpos = it2.current_y = 0;
9571 move_it_to (&it2, start_charpos, -1, -1, -1, MOVE_TO_POS);
9572 it->vpos -= it2.vpos;
9573 it->current_y -= it2.current_y;
9574 it->current_x = it->hpos = 0;
9576 /* If we moved too far back, move IT some lines forward. */
9577 if (it2.vpos > -dvpos)
9579 int delta = it2.vpos + dvpos;
9581 RESTORE_IT (&it2, &it2, it2data);
9582 SAVE_IT (it2, *it, it2data);
9583 move_it_to (it, -1, -1, -1, it->vpos + delta, MOVE_TO_VPOS);
9584 /* Move back again if we got too far ahead. */
9585 if (IT_CHARPOS (*it) >= start_charpos)
9586 RESTORE_IT (it, &it2, it2data);
9587 else
9588 bidi_unshelve_cache (it2data, 1);
9590 else if (hit_pos_limit && pos_limit > BEGV
9591 && dvpos < 0 && it2.vpos < -dvpos)
9593 /* If we hit the limit, but still didn't make it far enough
9594 back, that means there's a display string with a newline
9595 covering a large chunk of text, and that caused
9596 back_to_previous_visible_line_start try to go too far.
9597 Punish those who commit such atrocities by going back
9598 until we've reached DVPOS, after lifting the limit, which
9599 could make it slow for very long lines. "If it hurts,
9600 don't do that!" */
9601 dvpos += it2.vpos;
9602 RESTORE_IT (it, it, it2data);
9603 for (i = -dvpos; i > 0; --i)
9605 back_to_previous_visible_line_start (it);
9606 it->vpos--;
9609 else
9610 RESTORE_IT (it, it, it2data);
9614 /* Return true if IT points into the middle of a display vector. */
9616 bool
9617 in_display_vector_p (struct it *it)
9619 return (it->method == GET_FROM_DISPLAY_VECTOR
9620 && it->current.dpvec_index > 0
9621 && it->dpvec + it->current.dpvec_index != it->dpend);
9624 DEFUN ("window-text-pixel-size", Fwindow_text_pixel_size, Swindow_text_pixel_size, 0, 6, 0,
9625 doc: /* Return the size of the text of WINDOW's buffer in pixels.
9626 WINDOW must be a live window and defaults to the selected one. The
9627 return value is a cons of the maximum pixel-width of any text line and
9628 the maximum pixel-height of all text lines.
9630 The optional argument FROM, if non-nil, specifies the first text
9631 position and defaults to the minimum accessible position of the buffer.
9632 If FROM is t, use the minimum accessible position that is not a newline
9633 character. TO, if non-nil, specifies the last text position and
9634 defaults to the maximum accessible position of the buffer. If TO is t,
9635 use the maximum accessible position that is not a newline character.
9637 The optional argument X-LIMIT, if non-nil, specifies the maximum text
9638 width that can be returned. X-LIMIT nil or omitted, means to use the
9639 pixel-width of WINDOW's body; use this if you do not intend to change
9640 the width of WINDOW. Use the maximum width WINDOW may assume if you
9641 intend to change WINDOW's width. In any case, text whose x-coordinate
9642 is beyond X-LIMIT is ignored. Since calculating the width of long lines
9643 can take some time, it's always a good idea to make this argument as
9644 small as possible; in particular, if the buffer contains long lines that
9645 shall be truncated anyway.
9647 The optional argument Y-LIMIT, if non-nil, specifies the maximum text
9648 height that can be returned. Text lines whose y-coordinate is beyond
9649 Y-LIMIT are ignored. Since calculating the text height of a large
9650 buffer can take some time, it makes sense to specify this argument if
9651 the size of the buffer is unknown.
9653 Optional argument MODE-AND-HEADER-LINE nil or omitted means do not
9654 include the height of the mode- or header-line of WINDOW in the return
9655 value. If it is either the symbol `mode-line' or `header-line', include
9656 only the height of that line, if present, in the return value. If t,
9657 include the height of both, if present, in the return value. */)
9658 (Lisp_Object window, Lisp_Object from, Lisp_Object to, Lisp_Object x_limit, Lisp_Object y_limit,
9659 Lisp_Object mode_and_header_line)
9661 struct window *w = decode_live_window (window);
9662 Lisp_Object buf;
9663 struct buffer *b;
9664 struct it it;
9665 struct buffer *old_buffer = NULL;
9666 ptrdiff_t start, end, pos;
9667 struct text_pos startp;
9668 void *itdata = NULL;
9669 int c, max_y = -1, x = 0, y = 0;
9671 buf = w->contents;
9672 CHECK_BUFFER (buf);
9673 b = XBUFFER (buf);
9675 if (b != current_buffer)
9677 old_buffer = current_buffer;
9678 set_buffer_internal (b);
9681 if (NILP (from))
9682 start = BEGV;
9683 else if (EQ (from, Qt))
9685 start = pos = BEGV;
9686 while ((pos++ < ZV) && (c = FETCH_CHAR (pos))
9687 && (c == ' ' || c == '\t' || c == '\n' || c == '\r'))
9688 start = pos;
9689 while ((pos-- > BEGV) && (c = FETCH_CHAR (pos)) && (c == ' ' || c == '\t'))
9690 start = pos;
9692 else
9694 CHECK_NUMBER_COERCE_MARKER (from);
9695 start = min (max (XINT (from), BEGV), ZV);
9698 if (NILP (to))
9699 end = ZV;
9700 else if (EQ (to, Qt))
9702 end = pos = ZV;
9703 while ((pos-- > BEGV) && (c = FETCH_CHAR (pos))
9704 && (c == ' ' || c == '\t' || c == '\n' || c == '\r'))
9705 end = pos;
9706 while ((pos++ < ZV) && (c = FETCH_CHAR (pos)) && (c == ' ' || c == '\t'))
9707 end = pos;
9709 else
9711 CHECK_NUMBER_COERCE_MARKER (to);
9712 end = max (start, min (XINT (to), ZV));
9715 if (!NILP (y_limit))
9717 CHECK_NUMBER (y_limit);
9718 max_y = min (XINT (y_limit), INT_MAX);
9721 itdata = bidi_shelve_cache ();
9722 SET_TEXT_POS (startp, start, CHAR_TO_BYTE (start));
9723 start_display (&it, w, startp);
9725 if (NILP (x_limit))
9726 x = move_it_to (&it, end, -1, max_y, -1, MOVE_TO_POS | MOVE_TO_Y);
9727 else
9729 CHECK_NUMBER (x_limit);
9730 it.last_visible_x = min (XINT (x_limit), INFINITY);
9731 /* Actually, we never want move_it_to stop at to_x. But to make
9732 sure that move_it_in_display_line_to always moves far enough,
9733 we set it to INT_MAX and specify MOVE_TO_X. */
9734 x = move_it_to (&it, end, INT_MAX, max_y, -1,
9735 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
9738 y = it.current_y + it.max_ascent + it.max_descent;
9740 if (!EQ (mode_and_header_line, Qheader_line)
9741 && !EQ (mode_and_header_line, Qt))
9742 /* Do not count the header-line which was counted automatically by
9743 start_display. */
9744 y = y - WINDOW_HEADER_LINE_HEIGHT (w);
9746 if (EQ (mode_and_header_line, Qmode_line)
9747 || EQ (mode_and_header_line, Qt))
9748 /* Do count the mode-line which is not included automatically by
9749 start_display. */
9750 y = y + WINDOW_MODE_LINE_HEIGHT (w);
9752 bidi_unshelve_cache (itdata, 0);
9754 if (old_buffer)
9755 set_buffer_internal (old_buffer);
9757 return Fcons (make_number (x), make_number (y));
9760 /***********************************************************************
9761 Messages
9762 ***********************************************************************/
9765 /* Add a message with format string FORMAT and arguments ARG1 and ARG2
9766 to *Messages*. */
9768 void
9769 add_to_log (const char *format, Lisp_Object arg1, Lisp_Object arg2)
9771 Lisp_Object args[3];
9772 Lisp_Object msg, fmt;
9773 char *buffer;
9774 ptrdiff_t len;
9775 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
9776 USE_SAFE_ALLOCA;
9778 fmt = msg = Qnil;
9779 GCPRO4 (fmt, msg, arg1, arg2);
9781 args[0] = fmt = build_string (format);
9782 args[1] = arg1;
9783 args[2] = arg2;
9784 msg = Fformat (3, args);
9786 len = SBYTES (msg) + 1;
9787 buffer = SAFE_ALLOCA (len);
9788 memcpy (buffer, SDATA (msg), len);
9790 message_dolog (buffer, len - 1, 1, 0);
9791 SAFE_FREE ();
9793 UNGCPRO;
9797 /* Output a newline in the *Messages* buffer if "needs" one. */
9799 void
9800 message_log_maybe_newline (void)
9802 if (message_log_need_newline)
9803 message_dolog ("", 0, 1, 0);
9807 /* Add a string M of length NBYTES to the message log, optionally
9808 terminated with a newline when NLFLAG is true. MULTIBYTE, if
9809 true, means interpret the contents of M as multibyte. This
9810 function calls low-level routines in order to bypass text property
9811 hooks, etc. which might not be safe to run.
9813 This may GC (insert may run before/after change hooks),
9814 so the buffer M must NOT point to a Lisp string. */
9816 void
9817 message_dolog (const char *m, ptrdiff_t nbytes, bool nlflag, bool multibyte)
9819 const unsigned char *msg = (const unsigned char *) m;
9821 if (!NILP (Vmemory_full))
9822 return;
9824 if (!NILP (Vmessage_log_max))
9826 struct buffer *oldbuf;
9827 Lisp_Object oldpoint, oldbegv, oldzv;
9828 int old_windows_or_buffers_changed = windows_or_buffers_changed;
9829 ptrdiff_t point_at_end = 0;
9830 ptrdiff_t zv_at_end = 0;
9831 Lisp_Object old_deactivate_mark;
9832 struct gcpro gcpro1;
9834 old_deactivate_mark = Vdeactivate_mark;
9835 oldbuf = current_buffer;
9837 /* Ensure the Messages buffer exists, and switch to it.
9838 If we created it, set the major-mode. */
9840 int newbuffer = 0;
9841 if (NILP (Fget_buffer (Vmessages_buffer_name))) newbuffer = 1;
9843 Fset_buffer (Fget_buffer_create (Vmessages_buffer_name));
9845 if (newbuffer
9846 && !NILP (Ffboundp (intern ("messages-buffer-mode"))))
9847 call0 (intern ("messages-buffer-mode"));
9850 bset_undo_list (current_buffer, Qt);
9851 bset_cache_long_scans (current_buffer, Qnil);
9853 oldpoint = message_dolog_marker1;
9854 set_marker_restricted_both (oldpoint, Qnil, PT, PT_BYTE);
9855 oldbegv = message_dolog_marker2;
9856 set_marker_restricted_both (oldbegv, Qnil, BEGV, BEGV_BYTE);
9857 oldzv = message_dolog_marker3;
9858 set_marker_restricted_both (oldzv, Qnil, ZV, ZV_BYTE);
9859 GCPRO1 (old_deactivate_mark);
9861 if (PT == Z)
9862 point_at_end = 1;
9863 if (ZV == Z)
9864 zv_at_end = 1;
9866 BEGV = BEG;
9867 BEGV_BYTE = BEG_BYTE;
9868 ZV = Z;
9869 ZV_BYTE = Z_BYTE;
9870 TEMP_SET_PT_BOTH (Z, Z_BYTE);
9872 /* Insert the string--maybe converting multibyte to single byte
9873 or vice versa, so that all the text fits the buffer. */
9874 if (multibyte
9875 && NILP (BVAR (current_buffer, enable_multibyte_characters)))
9877 ptrdiff_t i;
9878 int c, char_bytes;
9879 char work[1];
9881 /* Convert a multibyte string to single-byte
9882 for the *Message* buffer. */
9883 for (i = 0; i < nbytes; i += char_bytes)
9885 c = string_char_and_length (msg + i, &char_bytes);
9886 work[0] = (ASCII_CHAR_P (c)
9888 : multibyte_char_to_unibyte (c));
9889 insert_1_both (work, 1, 1, 1, 0, 0);
9892 else if (! multibyte
9893 && ! NILP (BVAR (current_buffer, enable_multibyte_characters)))
9895 ptrdiff_t i;
9896 int c, char_bytes;
9897 unsigned char str[MAX_MULTIBYTE_LENGTH];
9898 /* Convert a single-byte string to multibyte
9899 for the *Message* buffer. */
9900 for (i = 0; i < nbytes; i++)
9902 c = msg[i];
9903 MAKE_CHAR_MULTIBYTE (c);
9904 char_bytes = CHAR_STRING (c, str);
9905 insert_1_both ((char *) str, 1, char_bytes, 1, 0, 0);
9908 else if (nbytes)
9909 insert_1_both (m, chars_in_text (msg, nbytes), nbytes, 1, 0, 0);
9911 if (nlflag)
9913 ptrdiff_t this_bol, this_bol_byte, prev_bol, prev_bol_byte;
9914 printmax_t dups;
9916 insert_1_both ("\n", 1, 1, 1, 0, 0);
9918 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE, -2, 0);
9919 this_bol = PT;
9920 this_bol_byte = PT_BYTE;
9922 /* See if this line duplicates the previous one.
9923 If so, combine duplicates. */
9924 if (this_bol > BEG)
9926 scan_newline (PT, PT_BYTE, BEG, BEG_BYTE, -2, 0);
9927 prev_bol = PT;
9928 prev_bol_byte = PT_BYTE;
9930 dups = message_log_check_duplicate (prev_bol_byte,
9931 this_bol_byte);
9932 if (dups)
9934 del_range_both (prev_bol, prev_bol_byte,
9935 this_bol, this_bol_byte, 0);
9936 if (dups > 1)
9938 char dupstr[sizeof " [ times]"
9939 + INT_STRLEN_BOUND (printmax_t)];
9941 /* If you change this format, don't forget to also
9942 change message_log_check_duplicate. */
9943 int duplen = sprintf (dupstr, " [%"pMd" times]", dups);
9944 TEMP_SET_PT_BOTH (Z - 1, Z_BYTE - 1);
9945 insert_1_both (dupstr, duplen, duplen, 1, 0, 1);
9950 /* If we have more than the desired maximum number of lines
9951 in the *Messages* buffer now, delete the oldest ones.
9952 This is safe because we don't have undo in this buffer. */
9954 if (NATNUMP (Vmessage_log_max))
9956 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE,
9957 -XFASTINT (Vmessage_log_max) - 1, 0);
9958 del_range_both (BEG, BEG_BYTE, PT, PT_BYTE, 0);
9961 BEGV = marker_position (oldbegv);
9962 BEGV_BYTE = marker_byte_position (oldbegv);
9964 if (zv_at_end)
9966 ZV = Z;
9967 ZV_BYTE = Z_BYTE;
9969 else
9971 ZV = marker_position (oldzv);
9972 ZV_BYTE = marker_byte_position (oldzv);
9975 if (point_at_end)
9976 TEMP_SET_PT_BOTH (Z, Z_BYTE);
9977 else
9978 /* We can't do Fgoto_char (oldpoint) because it will run some
9979 Lisp code. */
9980 TEMP_SET_PT_BOTH (marker_position (oldpoint),
9981 marker_byte_position (oldpoint));
9983 UNGCPRO;
9984 unchain_marker (XMARKER (oldpoint));
9985 unchain_marker (XMARKER (oldbegv));
9986 unchain_marker (XMARKER (oldzv));
9988 /* We called insert_1_both above with its 5th argument (PREPARE)
9989 zero, which prevents insert_1_both from calling
9990 prepare_to_modify_buffer, which in turns prevents us from
9991 incrementing windows_or_buffers_changed even if *Messages* is
9992 shown in some window. So we must manually set
9993 windows_or_buffers_changed here to make up for that. */
9994 windows_or_buffers_changed = old_windows_or_buffers_changed;
9995 bset_redisplay (current_buffer);
9997 set_buffer_internal (oldbuf);
9999 message_log_need_newline = !nlflag;
10000 Vdeactivate_mark = old_deactivate_mark;
10005 /* We are at the end of the buffer after just having inserted a newline.
10006 (Note: We depend on the fact we won't be crossing the gap.)
10007 Check to see if the most recent message looks a lot like the previous one.
10008 Return 0 if different, 1 if the new one should just replace it, or a
10009 value N > 1 if we should also append " [N times]". */
10011 static intmax_t
10012 message_log_check_duplicate (ptrdiff_t prev_bol_byte, ptrdiff_t this_bol_byte)
10014 ptrdiff_t i;
10015 ptrdiff_t len = Z_BYTE - 1 - this_bol_byte;
10016 int seen_dots = 0;
10017 unsigned char *p1 = BUF_BYTE_ADDRESS (current_buffer, prev_bol_byte);
10018 unsigned char *p2 = BUF_BYTE_ADDRESS (current_buffer, this_bol_byte);
10020 for (i = 0; i < len; i++)
10022 if (i >= 3 && p1[i - 3] == '.' && p1[i - 2] == '.' && p1[i - 1] == '.')
10023 seen_dots = 1;
10024 if (p1[i] != p2[i])
10025 return seen_dots;
10027 p1 += len;
10028 if (*p1 == '\n')
10029 return 2;
10030 if (*p1++ == ' ' && *p1++ == '[')
10032 char *pend;
10033 intmax_t n = strtoimax ((char *) p1, &pend, 10);
10034 if (0 < n && n < INTMAX_MAX && strncmp (pend, " times]\n", 8) == 0)
10035 return n + 1;
10037 return 0;
10041 /* Display an echo area message M with a specified length of NBYTES
10042 bytes. The string may include null characters. If M is not a
10043 string, clear out any existing message, and let the mini-buffer
10044 text show through.
10046 This function cancels echoing. */
10048 void
10049 message3 (Lisp_Object m)
10051 struct gcpro gcpro1;
10053 GCPRO1 (m);
10054 clear_message (true, true);
10055 cancel_echoing ();
10057 /* First flush out any partial line written with print. */
10058 message_log_maybe_newline ();
10059 if (STRINGP (m))
10061 ptrdiff_t nbytes = SBYTES (m);
10062 bool multibyte = STRING_MULTIBYTE (m);
10063 USE_SAFE_ALLOCA;
10064 char *buffer = SAFE_ALLOCA (nbytes);
10065 memcpy (buffer, SDATA (m), nbytes);
10066 message_dolog (buffer, nbytes, 1, multibyte);
10067 SAFE_FREE ();
10069 message3_nolog (m);
10071 UNGCPRO;
10075 /* The non-logging version of message3.
10076 This does not cancel echoing, because it is used for echoing.
10077 Perhaps we need to make a separate function for echoing
10078 and make this cancel echoing. */
10080 void
10081 message3_nolog (Lisp_Object m)
10083 struct frame *sf = SELECTED_FRAME ();
10085 if (FRAME_INITIAL_P (sf))
10087 if (noninteractive_need_newline)
10088 putc ('\n', stderr);
10089 noninteractive_need_newline = 0;
10090 if (STRINGP (m))
10092 Lisp_Object s = ENCODE_SYSTEM (m);
10094 fwrite (SDATA (s), SBYTES (s), 1, stderr);
10096 if (cursor_in_echo_area == 0)
10097 fprintf (stderr, "\n");
10098 fflush (stderr);
10100 /* Error messages get reported properly by cmd_error, so this must be just an
10101 informative message; if the frame hasn't really been initialized yet, just
10102 toss it. */
10103 else if (INTERACTIVE && sf->glyphs_initialized_p)
10105 /* Get the frame containing the mini-buffer
10106 that the selected frame is using. */
10107 Lisp_Object mini_window = FRAME_MINIBUF_WINDOW (sf);
10108 Lisp_Object frame = XWINDOW (mini_window)->frame;
10109 struct frame *f = XFRAME (frame);
10111 if (FRAME_VISIBLE_P (sf) && !FRAME_VISIBLE_P (f))
10112 Fmake_frame_visible (frame);
10114 if (STRINGP (m) && SCHARS (m) > 0)
10116 set_message (m);
10117 if (minibuffer_auto_raise)
10118 Fraise_frame (frame);
10119 /* Assume we are not echoing.
10120 (If we are, echo_now will override this.) */
10121 echo_message_buffer = Qnil;
10123 else
10124 clear_message (true, true);
10126 do_pending_window_change (0);
10127 echo_area_display (1);
10128 do_pending_window_change (0);
10129 if (FRAME_TERMINAL (f)->frame_up_to_date_hook)
10130 (*FRAME_TERMINAL (f)->frame_up_to_date_hook) (f);
10135 /* Display a null-terminated echo area message M. If M is 0, clear
10136 out any existing message, and let the mini-buffer text show through.
10138 The buffer M must continue to exist until after the echo area gets
10139 cleared or some other message gets displayed there. Do not pass
10140 text that is stored in a Lisp string. Do not pass text in a buffer
10141 that was alloca'd. */
10143 void
10144 message1 (const char *m)
10146 message3 (m ? build_unibyte_string (m) : Qnil);
10150 /* The non-logging counterpart of message1. */
10152 void
10153 message1_nolog (const char *m)
10155 message3_nolog (m ? build_unibyte_string (m) : Qnil);
10158 /* Display a message M which contains a single %s
10159 which gets replaced with STRING. */
10161 void
10162 message_with_string (const char *m, Lisp_Object string, int log)
10164 CHECK_STRING (string);
10166 if (noninteractive)
10168 if (m)
10170 /* ENCODE_SYSTEM below can GC and/or relocate the Lisp
10171 String whose data pointer might be passed to us in M. So
10172 we use a local copy. */
10173 char *fmt = xstrdup (m);
10175 if (noninteractive_need_newline)
10176 putc ('\n', stderr);
10177 noninteractive_need_newline = 0;
10178 fprintf (stderr, fmt, SDATA (ENCODE_SYSTEM (string)));
10179 if (!cursor_in_echo_area)
10180 fprintf (stderr, "\n");
10181 fflush (stderr);
10182 xfree (fmt);
10185 else if (INTERACTIVE)
10187 /* The frame whose minibuffer we're going to display the message on.
10188 It may be larger than the selected frame, so we need
10189 to use its buffer, not the selected frame's buffer. */
10190 Lisp_Object mini_window;
10191 struct frame *f, *sf = SELECTED_FRAME ();
10193 /* Get the frame containing the minibuffer
10194 that the selected frame is using. */
10195 mini_window = FRAME_MINIBUF_WINDOW (sf);
10196 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
10198 /* Error messages get reported properly by cmd_error, so this must be
10199 just an informative message; if the frame hasn't really been
10200 initialized yet, just toss it. */
10201 if (f->glyphs_initialized_p)
10203 Lisp_Object args[2], msg;
10204 struct gcpro gcpro1, gcpro2;
10206 args[0] = build_string (m);
10207 args[1] = msg = string;
10208 GCPRO2 (args[0], msg);
10209 gcpro1.nvars = 2;
10211 msg = Fformat (2, args);
10213 if (log)
10214 message3 (msg);
10215 else
10216 message3_nolog (msg);
10218 UNGCPRO;
10220 /* Print should start at the beginning of the message
10221 buffer next time. */
10222 message_buf_print = 0;
10228 /* Dump an informative message to the minibuf. If M is 0, clear out
10229 any existing message, and let the mini-buffer text show through. */
10231 static void
10232 vmessage (const char *m, va_list ap)
10234 if (noninteractive)
10236 if (m)
10238 if (noninteractive_need_newline)
10239 putc ('\n', stderr);
10240 noninteractive_need_newline = 0;
10241 vfprintf (stderr, m, ap);
10242 if (cursor_in_echo_area == 0)
10243 fprintf (stderr, "\n");
10244 fflush (stderr);
10247 else if (INTERACTIVE)
10249 /* The frame whose mini-buffer we're going to display the message
10250 on. It may be larger than the selected frame, so we need to
10251 use its buffer, not the selected frame's buffer. */
10252 Lisp_Object mini_window;
10253 struct frame *f, *sf = SELECTED_FRAME ();
10255 /* Get the frame containing the mini-buffer
10256 that the selected frame is using. */
10257 mini_window = FRAME_MINIBUF_WINDOW (sf);
10258 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
10260 /* Error messages get reported properly by cmd_error, so this must be
10261 just an informative message; if the frame hasn't really been
10262 initialized yet, just toss it. */
10263 if (f->glyphs_initialized_p)
10265 if (m)
10267 ptrdiff_t len;
10268 ptrdiff_t maxsize = FRAME_MESSAGE_BUF_SIZE (f);
10269 char *message_buf = alloca (maxsize + 1);
10271 len = doprnt (message_buf, maxsize, m, 0, ap);
10273 message3 (make_string (message_buf, len));
10275 else
10276 message1 (0);
10278 /* Print should start at the beginning of the message
10279 buffer next time. */
10280 message_buf_print = 0;
10285 void
10286 message (const char *m, ...)
10288 va_list ap;
10289 va_start (ap, m);
10290 vmessage (m, ap);
10291 va_end (ap);
10295 #if 0
10296 /* The non-logging version of message. */
10298 void
10299 message_nolog (const char *m, ...)
10301 Lisp_Object old_log_max;
10302 va_list ap;
10303 va_start (ap, m);
10304 old_log_max = Vmessage_log_max;
10305 Vmessage_log_max = Qnil;
10306 vmessage (m, ap);
10307 Vmessage_log_max = old_log_max;
10308 va_end (ap);
10310 #endif
10313 /* Display the current message in the current mini-buffer. This is
10314 only called from error handlers in process.c, and is not time
10315 critical. */
10317 void
10318 update_echo_area (void)
10320 if (!NILP (echo_area_buffer[0]))
10322 Lisp_Object string;
10323 string = Fcurrent_message ();
10324 message3 (string);
10329 /* Make sure echo area buffers in `echo_buffers' are live.
10330 If they aren't, make new ones. */
10332 static void
10333 ensure_echo_area_buffers (void)
10335 int i;
10337 for (i = 0; i < 2; ++i)
10338 if (!BUFFERP (echo_buffer[i])
10339 || !BUFFER_LIVE_P (XBUFFER (echo_buffer[i])))
10341 char name[30];
10342 Lisp_Object old_buffer;
10343 int j;
10345 old_buffer = echo_buffer[i];
10346 echo_buffer[i] = Fget_buffer_create
10347 (make_formatted_string (name, " *Echo Area %d*", i));
10348 bset_truncate_lines (XBUFFER (echo_buffer[i]), Qnil);
10349 /* to force word wrap in echo area -
10350 it was decided to postpone this*/
10351 /* XBUFFER (echo_buffer[i])->word_wrap = Qt; */
10353 for (j = 0; j < 2; ++j)
10354 if (EQ (old_buffer, echo_area_buffer[j]))
10355 echo_area_buffer[j] = echo_buffer[i];
10360 /* Call FN with args A1..A2 with either the current or last displayed
10361 echo_area_buffer as current buffer.
10363 WHICH zero means use the current message buffer
10364 echo_area_buffer[0]. If that is nil, choose a suitable buffer
10365 from echo_buffer[] and clear it.
10367 WHICH > 0 means use echo_area_buffer[1]. If that is nil, choose a
10368 suitable buffer from echo_buffer[] and clear it.
10370 If WHICH < 0, set echo_area_buffer[1] to echo_area_buffer[0], so
10371 that the current message becomes the last displayed one, make
10372 choose a suitable buffer for echo_area_buffer[0], and clear it.
10374 Value is what FN returns. */
10376 static int
10377 with_echo_area_buffer (struct window *w, int which,
10378 int (*fn) (ptrdiff_t, Lisp_Object),
10379 ptrdiff_t a1, Lisp_Object a2)
10381 Lisp_Object buffer;
10382 int this_one, the_other, clear_buffer_p, rc;
10383 ptrdiff_t count = SPECPDL_INDEX ();
10385 /* If buffers aren't live, make new ones. */
10386 ensure_echo_area_buffers ();
10388 clear_buffer_p = 0;
10390 if (which == 0)
10391 this_one = 0, the_other = 1;
10392 else if (which > 0)
10393 this_one = 1, the_other = 0;
10394 else
10396 this_one = 0, the_other = 1;
10397 clear_buffer_p = true;
10399 /* We need a fresh one in case the current echo buffer equals
10400 the one containing the last displayed echo area message. */
10401 if (!NILP (echo_area_buffer[this_one])
10402 && EQ (echo_area_buffer[this_one], echo_area_buffer[the_other]))
10403 echo_area_buffer[this_one] = Qnil;
10406 /* Choose a suitable buffer from echo_buffer[] is we don't
10407 have one. */
10408 if (NILP (echo_area_buffer[this_one]))
10410 echo_area_buffer[this_one]
10411 = (EQ (echo_area_buffer[the_other], echo_buffer[this_one])
10412 ? echo_buffer[the_other]
10413 : echo_buffer[this_one]);
10414 clear_buffer_p = true;
10417 buffer = echo_area_buffer[this_one];
10419 /* Don't get confused by reusing the buffer used for echoing
10420 for a different purpose. */
10421 if (echo_kboard == NULL && EQ (buffer, echo_message_buffer))
10422 cancel_echoing ();
10424 record_unwind_protect (unwind_with_echo_area_buffer,
10425 with_echo_area_buffer_unwind_data (w));
10427 /* Make the echo area buffer current. Note that for display
10428 purposes, it is not necessary that the displayed window's buffer
10429 == current_buffer, except for text property lookup. So, let's
10430 only set that buffer temporarily here without doing a full
10431 Fset_window_buffer. We must also change w->pointm, though,
10432 because otherwise an assertions in unshow_buffer fails, and Emacs
10433 aborts. */
10434 set_buffer_internal_1 (XBUFFER (buffer));
10435 if (w)
10437 wset_buffer (w, buffer);
10438 set_marker_both (w->pointm, buffer, BEG, BEG_BYTE);
10441 bset_undo_list (current_buffer, Qt);
10442 bset_read_only (current_buffer, Qnil);
10443 specbind (Qinhibit_read_only, Qt);
10444 specbind (Qinhibit_modification_hooks, Qt);
10446 if (clear_buffer_p && Z > BEG)
10447 del_range (BEG, Z);
10449 eassert (BEGV >= BEG);
10450 eassert (ZV <= Z && ZV >= BEGV);
10452 rc = fn (a1, a2);
10454 eassert (BEGV >= BEG);
10455 eassert (ZV <= Z && ZV >= BEGV);
10457 unbind_to (count, Qnil);
10458 return rc;
10462 /* Save state that should be preserved around the call to the function
10463 FN called in with_echo_area_buffer. */
10465 static Lisp_Object
10466 with_echo_area_buffer_unwind_data (struct window *w)
10468 int i = 0;
10469 Lisp_Object vector, tmp;
10471 /* Reduce consing by keeping one vector in
10472 Vwith_echo_area_save_vector. */
10473 vector = Vwith_echo_area_save_vector;
10474 Vwith_echo_area_save_vector = Qnil;
10476 if (NILP (vector))
10477 vector = Fmake_vector (make_number (9), Qnil);
10479 XSETBUFFER (tmp, current_buffer); ASET (vector, i, tmp); ++i;
10480 ASET (vector, i, Vdeactivate_mark); ++i;
10481 ASET (vector, i, make_number (windows_or_buffers_changed)); ++i;
10483 if (w)
10485 XSETWINDOW (tmp, w); ASET (vector, i, tmp); ++i;
10486 ASET (vector, i, w->contents); ++i;
10487 ASET (vector, i, make_number (marker_position (w->pointm))); ++i;
10488 ASET (vector, i, make_number (marker_byte_position (w->pointm))); ++i;
10489 ASET (vector, i, make_number (marker_position (w->start))); ++i;
10490 ASET (vector, i, make_number (marker_byte_position (w->start))); ++i;
10492 else
10494 int end = i + 6;
10495 for (; i < end; ++i)
10496 ASET (vector, i, Qnil);
10499 eassert (i == ASIZE (vector));
10500 return vector;
10504 /* Restore global state from VECTOR which was created by
10505 with_echo_area_buffer_unwind_data. */
10507 static void
10508 unwind_with_echo_area_buffer (Lisp_Object vector)
10510 set_buffer_internal_1 (XBUFFER (AREF (vector, 0)));
10511 Vdeactivate_mark = AREF (vector, 1);
10512 windows_or_buffers_changed = XFASTINT (AREF (vector, 2));
10514 if (WINDOWP (AREF (vector, 3)))
10516 struct window *w;
10517 Lisp_Object buffer;
10519 w = XWINDOW (AREF (vector, 3));
10520 buffer = AREF (vector, 4);
10522 wset_buffer (w, buffer);
10523 set_marker_both (w->pointm, buffer,
10524 XFASTINT (AREF (vector, 5)),
10525 XFASTINT (AREF (vector, 6)));
10526 set_marker_both (w->start, buffer,
10527 XFASTINT (AREF (vector, 7)),
10528 XFASTINT (AREF (vector, 8)));
10531 Vwith_echo_area_save_vector = vector;
10535 /* Set up the echo area for use by print functions. MULTIBYTE_P
10536 non-zero means we will print multibyte. */
10538 void
10539 setup_echo_area_for_printing (int multibyte_p)
10541 /* If we can't find an echo area any more, exit. */
10542 if (! FRAME_LIVE_P (XFRAME (selected_frame)))
10543 Fkill_emacs (Qnil);
10545 ensure_echo_area_buffers ();
10547 if (!message_buf_print)
10549 /* A message has been output since the last time we printed.
10550 Choose a fresh echo area buffer. */
10551 if (EQ (echo_area_buffer[1], echo_buffer[0]))
10552 echo_area_buffer[0] = echo_buffer[1];
10553 else
10554 echo_area_buffer[0] = echo_buffer[0];
10556 /* Switch to that buffer and clear it. */
10557 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
10558 bset_truncate_lines (current_buffer, Qnil);
10560 if (Z > BEG)
10562 ptrdiff_t count = SPECPDL_INDEX ();
10563 specbind (Qinhibit_read_only, Qt);
10564 /* Note that undo recording is always disabled. */
10565 del_range (BEG, Z);
10566 unbind_to (count, Qnil);
10568 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
10570 /* Set up the buffer for the multibyteness we need. */
10571 if (multibyte_p
10572 != !NILP (BVAR (current_buffer, enable_multibyte_characters)))
10573 Fset_buffer_multibyte (multibyte_p ? Qt : Qnil);
10575 /* Raise the frame containing the echo area. */
10576 if (minibuffer_auto_raise)
10578 struct frame *sf = SELECTED_FRAME ();
10579 Lisp_Object mini_window;
10580 mini_window = FRAME_MINIBUF_WINDOW (sf);
10581 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
10584 message_log_maybe_newline ();
10585 message_buf_print = 1;
10587 else
10589 if (NILP (echo_area_buffer[0]))
10591 if (EQ (echo_area_buffer[1], echo_buffer[0]))
10592 echo_area_buffer[0] = echo_buffer[1];
10593 else
10594 echo_area_buffer[0] = echo_buffer[0];
10597 if (current_buffer != XBUFFER (echo_area_buffer[0]))
10599 /* Someone switched buffers between print requests. */
10600 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
10601 bset_truncate_lines (current_buffer, Qnil);
10607 /* Display an echo area message in window W. Value is non-zero if W's
10608 height is changed. If display_last_displayed_message_p is
10609 non-zero, display the message that was last displayed, otherwise
10610 display the current message. */
10612 static int
10613 display_echo_area (struct window *w)
10615 int i, no_message_p, window_height_changed_p;
10617 /* Temporarily disable garbage collections while displaying the echo
10618 area. This is done because a GC can print a message itself.
10619 That message would modify the echo area buffer's contents while a
10620 redisplay of the buffer is going on, and seriously confuse
10621 redisplay. */
10622 ptrdiff_t count = inhibit_garbage_collection ();
10624 /* If there is no message, we must call display_echo_area_1
10625 nevertheless because it resizes the window. But we will have to
10626 reset the echo_area_buffer in question to nil at the end because
10627 with_echo_area_buffer will sets it to an empty buffer. */
10628 i = display_last_displayed_message_p ? 1 : 0;
10629 no_message_p = NILP (echo_area_buffer[i]);
10631 window_height_changed_p
10632 = with_echo_area_buffer (w, display_last_displayed_message_p,
10633 display_echo_area_1,
10634 (intptr_t) w, Qnil);
10636 if (no_message_p)
10637 echo_area_buffer[i] = Qnil;
10639 unbind_to (count, Qnil);
10640 return window_height_changed_p;
10644 /* Helper for display_echo_area. Display the current buffer which
10645 contains the current echo area message in window W, a mini-window,
10646 a pointer to which is passed in A1. A2..A4 are currently not used.
10647 Change the height of W so that all of the message is displayed.
10648 Value is non-zero if height of W was changed. */
10650 static int
10651 display_echo_area_1 (ptrdiff_t a1, Lisp_Object a2)
10653 intptr_t i1 = a1;
10654 struct window *w = (struct window *) i1;
10655 Lisp_Object window;
10656 struct text_pos start;
10657 int window_height_changed_p = 0;
10659 /* Do this before displaying, so that we have a large enough glyph
10660 matrix for the display. If we can't get enough space for the
10661 whole text, display the last N lines. That works by setting w->start. */
10662 window_height_changed_p = resize_mini_window (w, 0);
10664 /* Use the starting position chosen by resize_mini_window. */
10665 SET_TEXT_POS_FROM_MARKER (start, w->start);
10667 /* Display. */
10668 clear_glyph_matrix (w->desired_matrix);
10669 XSETWINDOW (window, w);
10670 try_window (window, start, 0);
10672 return window_height_changed_p;
10676 /* Resize the echo area window to exactly the size needed for the
10677 currently displayed message, if there is one. If a mini-buffer
10678 is active, don't shrink it. */
10680 void
10681 resize_echo_area_exactly (void)
10683 if (BUFFERP (echo_area_buffer[0])
10684 && WINDOWP (echo_area_window))
10686 struct window *w = XWINDOW (echo_area_window);
10687 Lisp_Object resize_exactly = (minibuf_level == 0 ? Qt : Qnil);
10688 int resized_p = with_echo_area_buffer (w, 0, resize_mini_window_1,
10689 (intptr_t) w, resize_exactly);
10690 if (resized_p)
10692 windows_or_buffers_changed = 42;
10693 update_mode_lines = 30;
10694 redisplay_internal ();
10700 /* Callback function for with_echo_area_buffer, when used from
10701 resize_echo_area_exactly. A1 contains a pointer to the window to
10702 resize, EXACTLY non-nil means resize the mini-window exactly to the
10703 size of the text displayed. A3 and A4 are not used. Value is what
10704 resize_mini_window returns. */
10706 static int
10707 resize_mini_window_1 (ptrdiff_t a1, Lisp_Object exactly)
10709 intptr_t i1 = a1;
10710 return resize_mini_window ((struct window *) i1, !NILP (exactly));
10714 /* Resize mini-window W to fit the size of its contents. EXACT_P
10715 means size the window exactly to the size needed. Otherwise, it's
10716 only enlarged until W's buffer is empty.
10718 Set W->start to the right place to begin display. If the whole
10719 contents fit, start at the beginning. Otherwise, start so as
10720 to make the end of the contents appear. This is particularly
10721 important for y-or-n-p, but seems desirable generally.
10723 Value is non-zero if the window height has been changed. */
10726 resize_mini_window (struct window *w, int exact_p)
10728 struct frame *f = XFRAME (w->frame);
10729 int window_height_changed_p = 0;
10731 eassert (MINI_WINDOW_P (w));
10733 /* By default, start display at the beginning. */
10734 set_marker_both (w->start, w->contents,
10735 BUF_BEGV (XBUFFER (w->contents)),
10736 BUF_BEGV_BYTE (XBUFFER (w->contents)));
10738 /* Don't resize windows while redisplaying a window; it would
10739 confuse redisplay functions when the size of the window they are
10740 displaying changes from under them. Such a resizing can happen,
10741 for instance, when which-func prints a long message while
10742 we are running fontification-functions. We're running these
10743 functions with safe_call which binds inhibit-redisplay to t. */
10744 if (!NILP (Vinhibit_redisplay))
10745 return 0;
10747 /* Nil means don't try to resize. */
10748 if (NILP (Vresize_mini_windows)
10749 || (FRAME_X_P (f) && FRAME_X_OUTPUT (f) == NULL))
10750 return 0;
10752 if (!FRAME_MINIBUF_ONLY_P (f))
10754 struct it it;
10755 int total_height = (WINDOW_PIXEL_HEIGHT (XWINDOW (FRAME_ROOT_WINDOW (f)))
10756 + WINDOW_PIXEL_HEIGHT (w));
10757 int unit = FRAME_LINE_HEIGHT (f);
10758 int height, max_height;
10759 struct text_pos start;
10760 struct buffer *old_current_buffer = NULL;
10762 if (current_buffer != XBUFFER (w->contents))
10764 old_current_buffer = current_buffer;
10765 set_buffer_internal (XBUFFER (w->contents));
10768 init_iterator (&it, w, BEGV, BEGV_BYTE, NULL, DEFAULT_FACE_ID);
10770 /* Compute the max. number of lines specified by the user. */
10771 if (FLOATP (Vmax_mini_window_height))
10772 max_height = XFLOATINT (Vmax_mini_window_height) * total_height;
10773 else if (INTEGERP (Vmax_mini_window_height))
10774 max_height = XINT (Vmax_mini_window_height) * unit;
10775 else
10776 max_height = total_height / 4;
10778 /* Correct that max. height if it's bogus. */
10779 max_height = clip_to_bounds (unit, max_height, total_height);
10781 /* Find out the height of the text in the window. */
10782 if (it.line_wrap == TRUNCATE)
10783 height = unit;
10784 else
10786 last_height = 0;
10787 move_it_to (&it, ZV, -1, -1, -1, MOVE_TO_POS);
10788 if (it.max_ascent == 0 && it.max_descent == 0)
10789 height = it.current_y + last_height;
10790 else
10791 height = it.current_y + it.max_ascent + it.max_descent;
10792 height -= min (it.extra_line_spacing, it.max_extra_line_spacing);
10795 /* Compute a suitable window start. */
10796 if (height > max_height)
10798 height = (max_height / unit) * unit;
10799 init_iterator (&it, w, ZV, ZV_BYTE, NULL, DEFAULT_FACE_ID);
10800 move_it_vertically_backward (&it, height - unit);
10801 start = it.current.pos;
10803 else
10804 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
10805 SET_MARKER_FROM_TEXT_POS (w->start, start);
10807 if (EQ (Vresize_mini_windows, Qgrow_only))
10809 /* Let it grow only, until we display an empty message, in which
10810 case the window shrinks again. */
10811 if (height > WINDOW_PIXEL_HEIGHT (w))
10813 int old_height = WINDOW_PIXEL_HEIGHT (w);
10815 FRAME_WINDOWS_FROZEN (f) = 1;
10816 grow_mini_window (w, height - WINDOW_PIXEL_HEIGHT (w), 1);
10817 window_height_changed_p = WINDOW_PIXEL_HEIGHT (w) != old_height;
10819 else if (height < WINDOW_PIXEL_HEIGHT (w)
10820 && (exact_p || BEGV == ZV))
10822 int old_height = WINDOW_PIXEL_HEIGHT (w);
10824 FRAME_WINDOWS_FROZEN (f) = 0;
10825 shrink_mini_window (w, 1);
10826 window_height_changed_p = WINDOW_PIXEL_HEIGHT (w) != old_height;
10829 else
10831 /* Always resize to exact size needed. */
10832 if (height > WINDOW_PIXEL_HEIGHT (w))
10834 int old_height = WINDOW_PIXEL_HEIGHT (w);
10836 FRAME_WINDOWS_FROZEN (f) = 1;
10837 grow_mini_window (w, height - WINDOW_PIXEL_HEIGHT (w), 1);
10838 window_height_changed_p = WINDOW_PIXEL_HEIGHT (w) != old_height;
10840 else if (height < WINDOW_PIXEL_HEIGHT (w))
10842 int old_height = WINDOW_PIXEL_HEIGHT (w);
10844 FRAME_WINDOWS_FROZEN (f) = 0;
10845 shrink_mini_window (w, 1);
10847 if (height)
10849 FRAME_WINDOWS_FROZEN (f) = 1;
10850 grow_mini_window (w, height - WINDOW_PIXEL_HEIGHT (w), 1);
10853 window_height_changed_p = WINDOW_PIXEL_HEIGHT (w) != old_height;
10857 if (old_current_buffer)
10858 set_buffer_internal (old_current_buffer);
10861 return window_height_changed_p;
10865 /* Value is the current message, a string, or nil if there is no
10866 current message. */
10868 Lisp_Object
10869 current_message (void)
10871 Lisp_Object msg;
10873 if (!BUFFERP (echo_area_buffer[0]))
10874 msg = Qnil;
10875 else
10877 with_echo_area_buffer (0, 0, current_message_1,
10878 (intptr_t) &msg, Qnil);
10879 if (NILP (msg))
10880 echo_area_buffer[0] = Qnil;
10883 return msg;
10887 static int
10888 current_message_1 (ptrdiff_t a1, Lisp_Object a2)
10890 intptr_t i1 = a1;
10891 Lisp_Object *msg = (Lisp_Object *) i1;
10893 if (Z > BEG)
10894 *msg = make_buffer_string (BEG, Z, 1);
10895 else
10896 *msg = Qnil;
10897 return 0;
10901 /* Push the current message on Vmessage_stack for later restoration
10902 by restore_message. Value is non-zero if the current message isn't
10903 empty. This is a relatively infrequent operation, so it's not
10904 worth optimizing. */
10906 bool
10907 push_message (void)
10909 Lisp_Object msg = current_message ();
10910 Vmessage_stack = Fcons (msg, Vmessage_stack);
10911 return STRINGP (msg);
10915 /* Restore message display from the top of Vmessage_stack. */
10917 void
10918 restore_message (void)
10920 eassert (CONSP (Vmessage_stack));
10921 message3_nolog (XCAR (Vmessage_stack));
10925 /* Handler for unwind-protect calling pop_message. */
10927 void
10928 pop_message_unwind (void)
10930 /* Pop the top-most entry off Vmessage_stack. */
10931 eassert (CONSP (Vmessage_stack));
10932 Vmessage_stack = XCDR (Vmessage_stack);
10936 /* Check that Vmessage_stack is nil. Called from emacs.c when Emacs
10937 exits. If the stack is not empty, we have a missing pop_message
10938 somewhere. */
10940 void
10941 check_message_stack (void)
10943 if (!NILP (Vmessage_stack))
10944 emacs_abort ();
10948 /* Truncate to NCHARS what will be displayed in the echo area the next
10949 time we display it---but don't redisplay it now. */
10951 void
10952 truncate_echo_area (ptrdiff_t nchars)
10954 if (nchars == 0)
10955 echo_area_buffer[0] = Qnil;
10956 else if (!noninteractive
10957 && INTERACTIVE
10958 && !NILP (echo_area_buffer[0]))
10960 struct frame *sf = SELECTED_FRAME ();
10961 /* Error messages get reported properly by cmd_error, so this must be
10962 just an informative message; if the frame hasn't really been
10963 initialized yet, just toss it. */
10964 if (sf->glyphs_initialized_p)
10965 with_echo_area_buffer (0, 0, truncate_message_1, nchars, Qnil);
10970 /* Helper function for truncate_echo_area. Truncate the current
10971 message to at most NCHARS characters. */
10973 static int
10974 truncate_message_1 (ptrdiff_t nchars, Lisp_Object a2)
10976 if (BEG + nchars < Z)
10977 del_range (BEG + nchars, Z);
10978 if (Z == BEG)
10979 echo_area_buffer[0] = Qnil;
10980 return 0;
10983 /* Set the current message to STRING. */
10985 static void
10986 set_message (Lisp_Object string)
10988 eassert (STRINGP (string));
10990 message_enable_multibyte = STRING_MULTIBYTE (string);
10992 with_echo_area_buffer (0, -1, set_message_1, 0, string);
10993 message_buf_print = 0;
10994 help_echo_showing_p = 0;
10996 if (STRINGP (Vdebug_on_message)
10997 && STRINGP (string)
10998 && fast_string_match (Vdebug_on_message, string) >= 0)
10999 call_debugger (list2 (Qerror, string));
11003 /* Helper function for set_message. First argument is ignored and second
11004 argument has the same meaning as for set_message.
11005 This function is called with the echo area buffer being current. */
11007 static int
11008 set_message_1 (ptrdiff_t a1, Lisp_Object string)
11010 eassert (STRINGP (string));
11012 /* Change multibyteness of the echo buffer appropriately. */
11013 if (message_enable_multibyte
11014 != !NILP (BVAR (current_buffer, enable_multibyte_characters)))
11015 Fset_buffer_multibyte (message_enable_multibyte ? Qt : Qnil);
11017 bset_truncate_lines (current_buffer, message_truncate_lines ? Qt : Qnil);
11018 if (!NILP (BVAR (current_buffer, bidi_display_reordering)))
11019 bset_bidi_paragraph_direction (current_buffer, Qleft_to_right);
11021 /* Insert new message at BEG. */
11022 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
11024 /* This function takes care of single/multibyte conversion.
11025 We just have to ensure that the echo area buffer has the right
11026 setting of enable_multibyte_characters. */
11027 insert_from_string (string, 0, 0, SCHARS (string), SBYTES (string), 1);
11029 return 0;
11033 /* Clear messages. CURRENT_P non-zero means clear the current
11034 message. LAST_DISPLAYED_P non-zero means clear the message
11035 last displayed. */
11037 void
11038 clear_message (bool current_p, bool last_displayed_p)
11040 if (current_p)
11042 echo_area_buffer[0] = Qnil;
11043 message_cleared_p = true;
11046 if (last_displayed_p)
11047 echo_area_buffer[1] = Qnil;
11049 message_buf_print = 0;
11052 /* Clear garbaged frames.
11054 This function is used where the old redisplay called
11055 redraw_garbaged_frames which in turn called redraw_frame which in
11056 turn called clear_frame. The call to clear_frame was a source of
11057 flickering. I believe a clear_frame is not necessary. It should
11058 suffice in the new redisplay to invalidate all current matrices,
11059 and ensure a complete redisplay of all windows. */
11061 static void
11062 clear_garbaged_frames (void)
11064 if (frame_garbaged)
11066 Lisp_Object tail, frame;
11068 FOR_EACH_FRAME (tail, frame)
11070 struct frame *f = XFRAME (frame);
11072 if (FRAME_VISIBLE_P (f) && FRAME_GARBAGED_P (f))
11074 if (f->resized_p)
11075 redraw_frame (f);
11076 else
11077 clear_current_matrices (f);
11078 fset_redisplay (f);
11079 f->garbaged = false;
11080 f->resized_p = false;
11084 frame_garbaged = false;
11089 /* Redisplay the echo area of the selected frame. If UPDATE_FRAME_P
11090 is non-zero update selected_frame. Value is non-zero if the
11091 mini-windows height has been changed. */
11093 static int
11094 echo_area_display (int update_frame_p)
11096 Lisp_Object mini_window;
11097 struct window *w;
11098 struct frame *f;
11099 int window_height_changed_p = 0;
11100 struct frame *sf = SELECTED_FRAME ();
11102 mini_window = FRAME_MINIBUF_WINDOW (sf);
11103 w = XWINDOW (mini_window);
11104 f = XFRAME (WINDOW_FRAME (w));
11106 /* Don't display if frame is invisible or not yet initialized. */
11107 if (!FRAME_VISIBLE_P (f) || !f->glyphs_initialized_p)
11108 return 0;
11110 #ifdef HAVE_WINDOW_SYSTEM
11111 /* When Emacs starts, selected_frame may be the initial terminal
11112 frame. If we let this through, a message would be displayed on
11113 the terminal. */
11114 if (FRAME_INITIAL_P (XFRAME (selected_frame)))
11115 return 0;
11116 #endif /* HAVE_WINDOW_SYSTEM */
11118 /* Redraw garbaged frames. */
11119 clear_garbaged_frames ();
11121 if (!NILP (echo_area_buffer[0]) || minibuf_level == 0)
11123 echo_area_window = mini_window;
11124 window_height_changed_p = display_echo_area (w);
11125 w->must_be_updated_p = true;
11127 /* Update the display, unless called from redisplay_internal.
11128 Also don't update the screen during redisplay itself. The
11129 update will happen at the end of redisplay, and an update
11130 here could cause confusion. */
11131 if (update_frame_p && !redisplaying_p)
11133 int n = 0;
11135 /* If the display update has been interrupted by pending
11136 input, update mode lines in the frame. Due to the
11137 pending input, it might have been that redisplay hasn't
11138 been called, so that mode lines above the echo area are
11139 garbaged. This looks odd, so we prevent it here. */
11140 if (!display_completed)
11141 n = redisplay_mode_lines (FRAME_ROOT_WINDOW (f), false);
11143 if (window_height_changed_p
11144 /* Don't do this if Emacs is shutting down. Redisplay
11145 needs to run hooks. */
11146 && !NILP (Vrun_hooks))
11148 /* Must update other windows. Likewise as in other
11149 cases, don't let this update be interrupted by
11150 pending input. */
11151 ptrdiff_t count = SPECPDL_INDEX ();
11152 specbind (Qredisplay_dont_pause, Qt);
11153 windows_or_buffers_changed = 44;
11154 redisplay_internal ();
11155 unbind_to (count, Qnil);
11157 else if (FRAME_WINDOW_P (f) && n == 0)
11159 /* Window configuration is the same as before.
11160 Can do with a display update of the echo area,
11161 unless we displayed some mode lines. */
11162 update_single_window (w, 1);
11163 flush_frame (f);
11165 else
11166 update_frame (f, 1, 1);
11168 /* If cursor is in the echo area, make sure that the next
11169 redisplay displays the minibuffer, so that the cursor will
11170 be replaced with what the minibuffer wants. */
11171 if (cursor_in_echo_area)
11172 wset_redisplay (XWINDOW (mini_window));
11175 else if (!EQ (mini_window, selected_window))
11176 wset_redisplay (XWINDOW (mini_window));
11178 /* Last displayed message is now the current message. */
11179 echo_area_buffer[1] = echo_area_buffer[0];
11180 /* Inform read_char that we're not echoing. */
11181 echo_message_buffer = Qnil;
11183 /* Prevent redisplay optimization in redisplay_internal by resetting
11184 this_line_start_pos. This is done because the mini-buffer now
11185 displays the message instead of its buffer text. */
11186 if (EQ (mini_window, selected_window))
11187 CHARPOS (this_line_start_pos) = 0;
11189 return window_height_changed_p;
11192 /* Nonzero if W's buffer was changed but not saved. */
11194 static int
11195 window_buffer_changed (struct window *w)
11197 struct buffer *b = XBUFFER (w->contents);
11199 eassert (BUFFER_LIVE_P (b));
11201 return (((BUF_SAVE_MODIFF (b) < BUF_MODIFF (b)) != w->last_had_star));
11204 /* Nonzero if W has %c in its mode line and mode line should be updated. */
11206 static int
11207 mode_line_update_needed (struct window *w)
11209 return (w->column_number_displayed != -1
11210 && !(PT == w->last_point && !window_outdated (w))
11211 && (w->column_number_displayed != current_column ()));
11214 /* Nonzero if window start of W is frozen and may not be changed during
11215 redisplay. */
11217 static bool
11218 window_frozen_p (struct window *w)
11220 if (FRAME_WINDOWS_FROZEN (XFRAME (WINDOW_FRAME (w))))
11222 Lisp_Object window;
11224 XSETWINDOW (window, w);
11225 if (MINI_WINDOW_P (w))
11226 return 0;
11227 else if (EQ (window, selected_window))
11228 return 0;
11229 else if (MINI_WINDOW_P (XWINDOW (selected_window))
11230 && EQ (window, Vminibuf_scroll_window))
11231 /* This special window can't be frozen too. */
11232 return 0;
11233 else
11234 return 1;
11236 return 0;
11239 /***********************************************************************
11240 Mode Lines and Frame Titles
11241 ***********************************************************************/
11243 /* A buffer for constructing non-propertized mode-line strings and
11244 frame titles in it; allocated from the heap in init_xdisp and
11245 resized as needed in store_mode_line_noprop_char. */
11247 static char *mode_line_noprop_buf;
11249 /* The buffer's end, and a current output position in it. */
11251 static char *mode_line_noprop_buf_end;
11252 static char *mode_line_noprop_ptr;
11254 #define MODE_LINE_NOPROP_LEN(start) \
11255 ((mode_line_noprop_ptr - mode_line_noprop_buf) - start)
11257 static enum {
11258 MODE_LINE_DISPLAY = 0,
11259 MODE_LINE_TITLE,
11260 MODE_LINE_NOPROP,
11261 MODE_LINE_STRING
11262 } mode_line_target;
11264 /* Alist that caches the results of :propertize.
11265 Each element is (PROPERTIZED-STRING . PROPERTY-LIST). */
11266 static Lisp_Object mode_line_proptrans_alist;
11268 /* List of strings making up the mode-line. */
11269 static Lisp_Object mode_line_string_list;
11271 /* Base face property when building propertized mode line string. */
11272 static Lisp_Object mode_line_string_face;
11273 static Lisp_Object mode_line_string_face_prop;
11276 /* Unwind data for mode line strings */
11278 static Lisp_Object Vmode_line_unwind_vector;
11280 static Lisp_Object
11281 format_mode_line_unwind_data (struct frame *target_frame,
11282 struct buffer *obuf,
11283 Lisp_Object owin,
11284 int save_proptrans)
11286 Lisp_Object vector, tmp;
11288 /* Reduce consing by keeping one vector in
11289 Vwith_echo_area_save_vector. */
11290 vector = Vmode_line_unwind_vector;
11291 Vmode_line_unwind_vector = Qnil;
11293 if (NILP (vector))
11294 vector = Fmake_vector (make_number (10), Qnil);
11296 ASET (vector, 0, make_number (mode_line_target));
11297 ASET (vector, 1, make_number (MODE_LINE_NOPROP_LEN (0)));
11298 ASET (vector, 2, mode_line_string_list);
11299 ASET (vector, 3, save_proptrans ? mode_line_proptrans_alist : Qt);
11300 ASET (vector, 4, mode_line_string_face);
11301 ASET (vector, 5, mode_line_string_face_prop);
11303 if (obuf)
11304 XSETBUFFER (tmp, obuf);
11305 else
11306 tmp = Qnil;
11307 ASET (vector, 6, tmp);
11308 ASET (vector, 7, owin);
11309 if (target_frame)
11311 /* Similarly to `with-selected-window', if the operation selects
11312 a window on another frame, we must restore that frame's
11313 selected window, and (for a tty) the top-frame. */
11314 ASET (vector, 8, target_frame->selected_window);
11315 if (FRAME_TERMCAP_P (target_frame))
11316 ASET (vector, 9, FRAME_TTY (target_frame)->top_frame);
11319 return vector;
11322 static void
11323 unwind_format_mode_line (Lisp_Object vector)
11325 Lisp_Object old_window = AREF (vector, 7);
11326 Lisp_Object target_frame_window = AREF (vector, 8);
11327 Lisp_Object old_top_frame = AREF (vector, 9);
11329 mode_line_target = XINT (AREF (vector, 0));
11330 mode_line_noprop_ptr = mode_line_noprop_buf + XINT (AREF (vector, 1));
11331 mode_line_string_list = AREF (vector, 2);
11332 if (! EQ (AREF (vector, 3), Qt))
11333 mode_line_proptrans_alist = AREF (vector, 3);
11334 mode_line_string_face = AREF (vector, 4);
11335 mode_line_string_face_prop = AREF (vector, 5);
11337 /* Select window before buffer, since it may change the buffer. */
11338 if (!NILP (old_window))
11340 /* If the operation that we are unwinding had selected a window
11341 on a different frame, reset its frame-selected-window. For a
11342 text terminal, reset its top-frame if necessary. */
11343 if (!NILP (target_frame_window))
11345 Lisp_Object frame
11346 = WINDOW_FRAME (XWINDOW (target_frame_window));
11348 if (!EQ (frame, WINDOW_FRAME (XWINDOW (old_window))))
11349 Fselect_window (target_frame_window, Qt);
11351 if (!NILP (old_top_frame) && !EQ (old_top_frame, frame))
11352 Fselect_frame (old_top_frame, Qt);
11355 Fselect_window (old_window, Qt);
11358 if (!NILP (AREF (vector, 6)))
11360 set_buffer_internal_1 (XBUFFER (AREF (vector, 6)));
11361 ASET (vector, 6, Qnil);
11364 Vmode_line_unwind_vector = vector;
11368 /* Store a single character C for the frame title in mode_line_noprop_buf.
11369 Re-allocate mode_line_noprop_buf if necessary. */
11371 static void
11372 store_mode_line_noprop_char (char c)
11374 /* If output position has reached the end of the allocated buffer,
11375 increase the buffer's size. */
11376 if (mode_line_noprop_ptr == mode_line_noprop_buf_end)
11378 ptrdiff_t len = MODE_LINE_NOPROP_LEN (0);
11379 ptrdiff_t size = len;
11380 mode_line_noprop_buf =
11381 xpalloc (mode_line_noprop_buf, &size, 1, STRING_BYTES_BOUND, 1);
11382 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
11383 mode_line_noprop_ptr = mode_line_noprop_buf + len;
11386 *mode_line_noprop_ptr++ = c;
11390 /* Store part of a frame title in mode_line_noprop_buf, beginning at
11391 mode_line_noprop_ptr. STRING is the string to store. Do not copy
11392 characters that yield more columns than PRECISION; PRECISION <= 0
11393 means copy the whole string. Pad with spaces until FIELD_WIDTH
11394 number of characters have been copied; FIELD_WIDTH <= 0 means don't
11395 pad. Called from display_mode_element when it is used to build a
11396 frame title. */
11398 static int
11399 store_mode_line_noprop (const char *string, int field_width, int precision)
11401 const unsigned char *str = (const unsigned char *) string;
11402 int n = 0;
11403 ptrdiff_t dummy, nbytes;
11405 /* Copy at most PRECISION chars from STR. */
11406 nbytes = strlen (string);
11407 n += c_string_width (str, nbytes, precision, &dummy, &nbytes);
11408 while (nbytes--)
11409 store_mode_line_noprop_char (*str++);
11411 /* Fill up with spaces until FIELD_WIDTH reached. */
11412 while (field_width > 0
11413 && n < field_width)
11415 store_mode_line_noprop_char (' ');
11416 ++n;
11419 return n;
11422 /***********************************************************************
11423 Frame Titles
11424 ***********************************************************************/
11426 #ifdef HAVE_WINDOW_SYSTEM
11428 /* Set the title of FRAME, if it has changed. The title format is
11429 Vicon_title_format if FRAME is iconified, otherwise it is
11430 frame_title_format. */
11432 static void
11433 x_consider_frame_title (Lisp_Object frame)
11435 struct frame *f = XFRAME (frame);
11437 if (FRAME_WINDOW_P (f)
11438 || FRAME_MINIBUF_ONLY_P (f)
11439 || f->explicit_name)
11441 /* Do we have more than one visible frame on this X display? */
11442 Lisp_Object tail, other_frame, fmt;
11443 ptrdiff_t title_start;
11444 char *title;
11445 ptrdiff_t len;
11446 struct it it;
11447 ptrdiff_t count = SPECPDL_INDEX ();
11449 FOR_EACH_FRAME (tail, other_frame)
11451 struct frame *tf = XFRAME (other_frame);
11453 if (tf != f
11454 && FRAME_KBOARD (tf) == FRAME_KBOARD (f)
11455 && !FRAME_MINIBUF_ONLY_P (tf)
11456 && !EQ (other_frame, tip_frame)
11457 && (FRAME_VISIBLE_P (tf) || FRAME_ICONIFIED_P (tf)))
11458 break;
11461 /* Set global variable indicating that multiple frames exist. */
11462 multiple_frames = CONSP (tail);
11464 /* Switch to the buffer of selected window of the frame. Set up
11465 mode_line_target so that display_mode_element will output into
11466 mode_line_noprop_buf; then display the title. */
11467 record_unwind_protect (unwind_format_mode_line,
11468 format_mode_line_unwind_data
11469 (f, current_buffer, selected_window, 0));
11471 Fselect_window (f->selected_window, Qt);
11472 set_buffer_internal_1
11473 (XBUFFER (XWINDOW (f->selected_window)->contents));
11474 fmt = FRAME_ICONIFIED_P (f) ? Vicon_title_format : Vframe_title_format;
11476 mode_line_target = MODE_LINE_TITLE;
11477 title_start = MODE_LINE_NOPROP_LEN (0);
11478 init_iterator (&it, XWINDOW (f->selected_window), -1, -1,
11479 NULL, DEFAULT_FACE_ID);
11480 display_mode_element (&it, 0, -1, -1, fmt, Qnil, 0);
11481 len = MODE_LINE_NOPROP_LEN (title_start);
11482 title = mode_line_noprop_buf + title_start;
11483 unbind_to (count, Qnil);
11485 /* Set the title only if it's changed. This avoids consing in
11486 the common case where it hasn't. (If it turns out that we've
11487 already wasted too much time by walking through the list with
11488 display_mode_element, then we might need to optimize at a
11489 higher level than this.) */
11490 if (! STRINGP (f->name)
11491 || SBYTES (f->name) != len
11492 || memcmp (title, SDATA (f->name), len) != 0)
11493 x_implicitly_set_name (f, make_string (title, len), Qnil);
11497 #endif /* not HAVE_WINDOW_SYSTEM */
11500 /***********************************************************************
11501 Menu Bars
11502 ***********************************************************************/
11504 /* Non-zero if we will not redisplay all visible windows. */
11505 #define REDISPLAY_SOME_P() \
11506 ((windows_or_buffers_changed == 0 \
11507 || windows_or_buffers_changed == REDISPLAY_SOME) \
11508 && (update_mode_lines == 0 \
11509 || update_mode_lines == REDISPLAY_SOME))
11511 /* Prepare for redisplay by updating menu-bar item lists when
11512 appropriate. This can call eval. */
11514 static void
11515 prepare_menu_bars (void)
11517 bool all_windows = windows_or_buffers_changed || update_mode_lines;
11518 bool some_windows = REDISPLAY_SOME_P ();
11519 struct gcpro gcpro1, gcpro2;
11520 Lisp_Object tooltip_frame;
11522 #ifdef HAVE_WINDOW_SYSTEM
11523 tooltip_frame = tip_frame;
11524 #else
11525 tooltip_frame = Qnil;
11526 #endif
11528 if (FUNCTIONP (Vpre_redisplay_function))
11530 Lisp_Object windows = all_windows ? Qt : Qnil;
11531 if (all_windows && some_windows)
11533 Lisp_Object ws = window_list ();
11534 for (windows = Qnil; CONSP (ws); ws = XCDR (ws))
11536 Lisp_Object this = XCAR (ws);
11537 struct window *w = XWINDOW (this);
11538 if (w->redisplay
11539 || XFRAME (w->frame)->redisplay
11540 || XBUFFER (w->contents)->text->redisplay)
11542 windows = Fcons (this, windows);
11546 safe_call1 (Vpre_redisplay_function, windows);
11549 /* Update all frame titles based on their buffer names, etc. We do
11550 this before the menu bars so that the buffer-menu will show the
11551 up-to-date frame titles. */
11552 #ifdef HAVE_WINDOW_SYSTEM
11553 if (all_windows)
11555 Lisp_Object tail, frame;
11557 FOR_EACH_FRAME (tail, frame)
11559 struct frame *f = XFRAME (frame);
11560 struct window *w = XWINDOW (FRAME_SELECTED_WINDOW (f));
11561 if (some_windows
11562 && !f->redisplay
11563 && !w->redisplay
11564 && !XBUFFER (w->contents)->text->redisplay)
11565 continue;
11567 if (!EQ (frame, tooltip_frame)
11568 && (FRAME_ICONIFIED_P (f)
11569 || FRAME_VISIBLE_P (f) == 1
11570 /* Exclude TTY frames that are obscured because they
11571 are not the top frame on their console. This is
11572 because x_consider_frame_title actually switches
11573 to the frame, which for TTY frames means it is
11574 marked as garbaged, and will be completely
11575 redrawn on the next redisplay cycle. This causes
11576 TTY frames to be completely redrawn, when there
11577 are more than one of them, even though nothing
11578 should be changed on display. */
11579 || (FRAME_VISIBLE_P (f) == 2 && FRAME_WINDOW_P (f))))
11580 x_consider_frame_title (frame);
11583 #endif /* HAVE_WINDOW_SYSTEM */
11585 /* Update the menu bar item lists, if appropriate. This has to be
11586 done before any actual redisplay or generation of display lines. */
11588 if (all_windows)
11590 Lisp_Object tail, frame;
11591 ptrdiff_t count = SPECPDL_INDEX ();
11592 /* 1 means that update_menu_bar has run its hooks
11593 so any further calls to update_menu_bar shouldn't do so again. */
11594 int menu_bar_hooks_run = 0;
11596 record_unwind_save_match_data ();
11598 FOR_EACH_FRAME (tail, frame)
11600 struct frame *f = XFRAME (frame);
11601 struct window *w = XWINDOW (FRAME_SELECTED_WINDOW (f));
11603 /* Ignore tooltip frame. */
11604 if (EQ (frame, tooltip_frame))
11605 continue;
11607 if (some_windows
11608 && !f->redisplay
11609 && !w->redisplay
11610 && !XBUFFER (w->contents)->text->redisplay)
11611 continue;
11613 /* If a window on this frame changed size, report that to
11614 the user and clear the size-change flag. */
11615 if (FRAME_WINDOW_SIZES_CHANGED (f))
11617 Lisp_Object functions;
11619 /* Clear flag first in case we get an error below. */
11620 FRAME_WINDOW_SIZES_CHANGED (f) = 0;
11621 functions = Vwindow_size_change_functions;
11622 GCPRO2 (tail, functions);
11624 while (CONSP (functions))
11626 if (!EQ (XCAR (functions), Qt))
11627 call1 (XCAR (functions), frame);
11628 functions = XCDR (functions);
11630 UNGCPRO;
11633 GCPRO1 (tail);
11634 menu_bar_hooks_run = update_menu_bar (f, 0, menu_bar_hooks_run);
11635 #ifdef HAVE_WINDOW_SYSTEM
11636 update_tool_bar (f, 0);
11637 #endif
11638 #ifdef HAVE_NS
11639 if (windows_or_buffers_changed
11640 && FRAME_NS_P (f))
11641 ns_set_doc_edited
11642 (f, Fbuffer_modified_p (XWINDOW (f->selected_window)->contents));
11643 #endif
11644 UNGCPRO;
11647 unbind_to (count, Qnil);
11649 else
11651 struct frame *sf = SELECTED_FRAME ();
11652 update_menu_bar (sf, 1, 0);
11653 #ifdef HAVE_WINDOW_SYSTEM
11654 update_tool_bar (sf, 1);
11655 #endif
11660 /* Update the menu bar item list for frame F. This has to be done
11661 before we start to fill in any display lines, because it can call
11662 eval.
11664 If SAVE_MATCH_DATA is non-zero, we must save and restore it here.
11666 If HOOKS_RUN is 1, that means a previous call to update_menu_bar
11667 already ran the menu bar hooks for this redisplay, so there
11668 is no need to run them again. The return value is the
11669 updated value of this flag, to pass to the next call. */
11671 static int
11672 update_menu_bar (struct frame *f, int save_match_data, int hooks_run)
11674 Lisp_Object window;
11675 register struct window *w;
11677 /* If called recursively during a menu update, do nothing. This can
11678 happen when, for instance, an activate-menubar-hook causes a
11679 redisplay. */
11680 if (inhibit_menubar_update)
11681 return hooks_run;
11683 window = FRAME_SELECTED_WINDOW (f);
11684 w = XWINDOW (window);
11686 if (FRAME_WINDOW_P (f)
11688 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
11689 || defined (HAVE_NS) || defined (USE_GTK)
11690 FRAME_EXTERNAL_MENU_BAR (f)
11691 #else
11692 FRAME_MENU_BAR_LINES (f) > 0
11693 #endif
11694 : FRAME_MENU_BAR_LINES (f) > 0)
11696 /* If the user has switched buffers or windows, we need to
11697 recompute to reflect the new bindings. But we'll
11698 recompute when update_mode_lines is set too; that means
11699 that people can use force-mode-line-update to request
11700 that the menu bar be recomputed. The adverse effect on
11701 the rest of the redisplay algorithm is about the same as
11702 windows_or_buffers_changed anyway. */
11703 if (windows_or_buffers_changed
11704 /* This used to test w->update_mode_line, but we believe
11705 there is no need to recompute the menu in that case. */
11706 || update_mode_lines
11707 || window_buffer_changed (w))
11709 struct buffer *prev = current_buffer;
11710 ptrdiff_t count = SPECPDL_INDEX ();
11712 specbind (Qinhibit_menubar_update, Qt);
11714 set_buffer_internal_1 (XBUFFER (w->contents));
11715 if (save_match_data)
11716 record_unwind_save_match_data ();
11717 if (NILP (Voverriding_local_map_menu_flag))
11719 specbind (Qoverriding_terminal_local_map, Qnil);
11720 specbind (Qoverriding_local_map, Qnil);
11723 if (!hooks_run)
11725 /* Run the Lucid hook. */
11726 safe_run_hooks (Qactivate_menubar_hook);
11728 /* If it has changed current-menubar from previous value,
11729 really recompute the menu-bar from the value. */
11730 if (! NILP (Vlucid_menu_bar_dirty_flag))
11731 call0 (Qrecompute_lucid_menubar);
11733 safe_run_hooks (Qmenu_bar_update_hook);
11735 hooks_run = 1;
11738 XSETFRAME (Vmenu_updating_frame, f);
11739 fset_menu_bar_items (f, menu_bar_items (FRAME_MENU_BAR_ITEMS (f)));
11741 /* Redisplay the menu bar in case we changed it. */
11742 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
11743 || defined (HAVE_NS) || defined (USE_GTK)
11744 if (FRAME_WINDOW_P (f))
11746 #if defined (HAVE_NS)
11747 /* All frames on Mac OS share the same menubar. So only
11748 the selected frame should be allowed to set it. */
11749 if (f == SELECTED_FRAME ())
11750 #endif
11751 set_frame_menubar (f, 0, 0);
11753 else
11754 /* On a terminal screen, the menu bar is an ordinary screen
11755 line, and this makes it get updated. */
11756 w->update_mode_line = 1;
11757 #else /* ! (USE_X_TOOLKIT || HAVE_NTGUI || HAVE_NS || USE_GTK) */
11758 /* In the non-toolkit version, the menu bar is an ordinary screen
11759 line, and this makes it get updated. */
11760 w->update_mode_line = 1;
11761 #endif /* ! (USE_X_TOOLKIT || HAVE_NTGUI || HAVE_NS || USE_GTK) */
11763 unbind_to (count, Qnil);
11764 set_buffer_internal_1 (prev);
11768 return hooks_run;
11771 /***********************************************************************
11772 Tool-bars
11773 ***********************************************************************/
11775 #ifdef HAVE_WINDOW_SYSTEM
11777 /* Tool-bar item index of the item on which a mouse button was pressed
11778 or -1. */
11780 int last_tool_bar_item;
11782 /* Select `frame' temporarily without running all the code in
11783 do_switch_frame.
11784 FIXME: Maybe do_switch_frame should be trimmed down similarly
11785 when `norecord' is set. */
11786 static void
11787 fast_set_selected_frame (Lisp_Object frame)
11789 if (!EQ (selected_frame, frame))
11791 selected_frame = frame;
11792 selected_window = XFRAME (frame)->selected_window;
11796 /* Update the tool-bar item list for frame F. This has to be done
11797 before we start to fill in any display lines. Called from
11798 prepare_menu_bars. If SAVE_MATCH_DATA is non-zero, we must save
11799 and restore it here. */
11801 static void
11802 update_tool_bar (struct frame *f, int save_match_data)
11804 #if defined (USE_GTK) || defined (HAVE_NS)
11805 int do_update = FRAME_EXTERNAL_TOOL_BAR (f);
11806 #else
11807 int do_update = (WINDOWP (f->tool_bar_window)
11808 && WINDOW_PIXEL_HEIGHT (XWINDOW (f->tool_bar_window)) > 0);
11809 #endif
11811 if (do_update)
11813 Lisp_Object window;
11814 struct window *w;
11816 window = FRAME_SELECTED_WINDOW (f);
11817 w = XWINDOW (window);
11819 /* If the user has switched buffers or windows, we need to
11820 recompute to reflect the new bindings. But we'll
11821 recompute when update_mode_lines is set too; that means
11822 that people can use force-mode-line-update to request
11823 that the menu bar be recomputed. The adverse effect on
11824 the rest of the redisplay algorithm is about the same as
11825 windows_or_buffers_changed anyway. */
11826 if (windows_or_buffers_changed
11827 || w->update_mode_line
11828 || update_mode_lines
11829 || window_buffer_changed (w))
11831 struct buffer *prev = current_buffer;
11832 ptrdiff_t count = SPECPDL_INDEX ();
11833 Lisp_Object frame, new_tool_bar;
11834 int new_n_tool_bar;
11835 struct gcpro gcpro1;
11837 /* Set current_buffer to the buffer of the selected
11838 window of the frame, so that we get the right local
11839 keymaps. */
11840 set_buffer_internal_1 (XBUFFER (w->contents));
11842 /* Save match data, if we must. */
11843 if (save_match_data)
11844 record_unwind_save_match_data ();
11846 /* Make sure that we don't accidentally use bogus keymaps. */
11847 if (NILP (Voverriding_local_map_menu_flag))
11849 specbind (Qoverriding_terminal_local_map, Qnil);
11850 specbind (Qoverriding_local_map, Qnil);
11853 GCPRO1 (new_tool_bar);
11855 /* We must temporarily set the selected frame to this frame
11856 before calling tool_bar_items, because the calculation of
11857 the tool-bar keymap uses the selected frame (see
11858 `tool-bar-make-keymap' in tool-bar.el). */
11859 eassert (EQ (selected_window,
11860 /* Since we only explicitly preserve selected_frame,
11861 check that selected_window would be redundant. */
11862 XFRAME (selected_frame)->selected_window));
11863 record_unwind_protect (fast_set_selected_frame, selected_frame);
11864 XSETFRAME (frame, f);
11865 fast_set_selected_frame (frame);
11867 /* Build desired tool-bar items from keymaps. */
11868 new_tool_bar
11869 = tool_bar_items (Fcopy_sequence (f->tool_bar_items),
11870 &new_n_tool_bar);
11872 /* Redisplay the tool-bar if we changed it. */
11873 if (new_n_tool_bar != f->n_tool_bar_items
11874 || NILP (Fequal (new_tool_bar, f->tool_bar_items)))
11876 /* Redisplay that happens asynchronously due to an expose event
11877 may access f->tool_bar_items. Make sure we update both
11878 variables within BLOCK_INPUT so no such event interrupts. */
11879 block_input ();
11880 fset_tool_bar_items (f, new_tool_bar);
11881 f->n_tool_bar_items = new_n_tool_bar;
11882 w->update_mode_line = 1;
11883 unblock_input ();
11886 UNGCPRO;
11888 unbind_to (count, Qnil);
11889 set_buffer_internal_1 (prev);
11894 #if ! defined (USE_GTK) && ! defined (HAVE_NS)
11896 /* Set F->desired_tool_bar_string to a Lisp string representing frame
11897 F's desired tool-bar contents. F->tool_bar_items must have
11898 been set up previously by calling prepare_menu_bars. */
11900 static void
11901 build_desired_tool_bar_string (struct frame *f)
11903 int i, size, size_needed;
11904 struct gcpro gcpro1, gcpro2, gcpro3;
11905 Lisp_Object image, plist, props;
11907 image = plist = props = Qnil;
11908 GCPRO3 (image, plist, props);
11910 /* Prepare F->desired_tool_bar_string. If we can reuse it, do so.
11911 Otherwise, make a new string. */
11913 /* The size of the string we might be able to reuse. */
11914 size = (STRINGP (f->desired_tool_bar_string)
11915 ? SCHARS (f->desired_tool_bar_string)
11916 : 0);
11918 /* We need one space in the string for each image. */
11919 size_needed = f->n_tool_bar_items;
11921 /* Reuse f->desired_tool_bar_string, if possible. */
11922 if (size < size_needed || NILP (f->desired_tool_bar_string))
11923 fset_desired_tool_bar_string
11924 (f, Fmake_string (make_number (size_needed), make_number (' ')));
11925 else
11927 props = list4 (Qdisplay, Qnil, Qmenu_item, Qnil);
11928 Fremove_text_properties (make_number (0), make_number (size),
11929 props, f->desired_tool_bar_string);
11932 /* Put a `display' property on the string for the images to display,
11933 put a `menu_item' property on tool-bar items with a value that
11934 is the index of the item in F's tool-bar item vector. */
11935 for (i = 0; i < f->n_tool_bar_items; ++i)
11937 #define PROP(IDX) \
11938 AREF (f->tool_bar_items, i * TOOL_BAR_ITEM_NSLOTS + (IDX))
11940 int enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
11941 int selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
11942 int hmargin, vmargin, relief, idx, end;
11944 /* If image is a vector, choose the image according to the
11945 button state. */
11946 image = PROP (TOOL_BAR_ITEM_IMAGES);
11947 if (VECTORP (image))
11949 if (enabled_p)
11950 idx = (selected_p
11951 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
11952 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
11953 else
11954 idx = (selected_p
11955 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
11956 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
11958 eassert (ASIZE (image) >= idx);
11959 image = AREF (image, idx);
11961 else
11962 idx = -1;
11964 /* Ignore invalid image specifications. */
11965 if (!valid_image_p (image))
11966 continue;
11968 /* Display the tool-bar button pressed, or depressed. */
11969 plist = Fcopy_sequence (XCDR (image));
11971 /* Compute margin and relief to draw. */
11972 relief = (tool_bar_button_relief >= 0
11973 ? tool_bar_button_relief
11974 : DEFAULT_TOOL_BAR_BUTTON_RELIEF);
11975 hmargin = vmargin = relief;
11977 if (RANGED_INTEGERP (1, Vtool_bar_button_margin,
11978 INT_MAX - max (hmargin, vmargin)))
11980 hmargin += XFASTINT (Vtool_bar_button_margin);
11981 vmargin += XFASTINT (Vtool_bar_button_margin);
11983 else if (CONSP (Vtool_bar_button_margin))
11985 if (RANGED_INTEGERP (1, XCAR (Vtool_bar_button_margin),
11986 INT_MAX - hmargin))
11987 hmargin += XFASTINT (XCAR (Vtool_bar_button_margin));
11989 if (RANGED_INTEGERP (1, XCDR (Vtool_bar_button_margin),
11990 INT_MAX - vmargin))
11991 vmargin += XFASTINT (XCDR (Vtool_bar_button_margin));
11994 if (auto_raise_tool_bar_buttons_p)
11996 /* Add a `:relief' property to the image spec if the item is
11997 selected. */
11998 if (selected_p)
12000 plist = Fplist_put (plist, QCrelief, make_number (-relief));
12001 hmargin -= relief;
12002 vmargin -= relief;
12005 else
12007 /* If image is selected, display it pressed, i.e. with a
12008 negative relief. If it's not selected, display it with a
12009 raised relief. */
12010 plist = Fplist_put (plist, QCrelief,
12011 (selected_p
12012 ? make_number (-relief)
12013 : make_number (relief)));
12014 hmargin -= relief;
12015 vmargin -= relief;
12018 /* Put a margin around the image. */
12019 if (hmargin || vmargin)
12021 if (hmargin == vmargin)
12022 plist = Fplist_put (plist, QCmargin, make_number (hmargin));
12023 else
12024 plist = Fplist_put (plist, QCmargin,
12025 Fcons (make_number (hmargin),
12026 make_number (vmargin)));
12029 /* If button is not enabled, and we don't have special images
12030 for the disabled state, make the image appear disabled by
12031 applying an appropriate algorithm to it. */
12032 if (!enabled_p && idx < 0)
12033 plist = Fplist_put (plist, QCconversion, Qdisabled);
12035 /* Put a `display' text property on the string for the image to
12036 display. Put a `menu-item' property on the string that gives
12037 the start of this item's properties in the tool-bar items
12038 vector. */
12039 image = Fcons (Qimage, plist);
12040 props = list4 (Qdisplay, image,
12041 Qmenu_item, make_number (i * TOOL_BAR_ITEM_NSLOTS));
12043 /* Let the last image hide all remaining spaces in the tool bar
12044 string. The string can be longer than needed when we reuse a
12045 previous string. */
12046 if (i + 1 == f->n_tool_bar_items)
12047 end = SCHARS (f->desired_tool_bar_string);
12048 else
12049 end = i + 1;
12050 Fadd_text_properties (make_number (i), make_number (end),
12051 props, f->desired_tool_bar_string);
12052 #undef PROP
12055 UNGCPRO;
12059 /* Display one line of the tool-bar of frame IT->f.
12061 HEIGHT specifies the desired height of the tool-bar line.
12062 If the actual height of the glyph row is less than HEIGHT, the
12063 row's height is increased to HEIGHT, and the icons are centered
12064 vertically in the new height.
12066 If HEIGHT is -1, we are counting needed tool-bar lines, so don't
12067 count a final empty row in case the tool-bar width exactly matches
12068 the window width.
12071 static void
12072 display_tool_bar_line (struct it *it, int height)
12074 struct glyph_row *row = it->glyph_row;
12075 int max_x = it->last_visible_x;
12076 struct glyph *last;
12078 /* Don't extend on a previously drawn tool bar items (Bug#16058). */
12079 clear_glyph_row (row);
12080 row->enabled_p = true;
12081 row->y = it->current_y;
12083 /* Note that this isn't made use of if the face hasn't a box,
12084 so there's no need to check the face here. */
12085 it->start_of_box_run_p = 1;
12087 while (it->current_x < max_x)
12089 int x, n_glyphs_before, i, nglyphs;
12090 struct it it_before;
12092 /* Get the next display element. */
12093 if (!get_next_display_element (it))
12095 /* Don't count empty row if we are counting needed tool-bar lines. */
12096 if (height < 0 && !it->hpos)
12097 return;
12098 break;
12101 /* Produce glyphs. */
12102 n_glyphs_before = row->used[TEXT_AREA];
12103 it_before = *it;
12105 PRODUCE_GLYPHS (it);
12107 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
12108 i = 0;
12109 x = it_before.current_x;
12110 while (i < nglyphs)
12112 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
12114 if (x + glyph->pixel_width > max_x)
12116 /* Glyph doesn't fit on line. Backtrack. */
12117 row->used[TEXT_AREA] = n_glyphs_before;
12118 *it = it_before;
12119 /* If this is the only glyph on this line, it will never fit on the
12120 tool-bar, so skip it. But ensure there is at least one glyph,
12121 so we don't accidentally disable the tool-bar. */
12122 if (n_glyphs_before == 0
12123 && (it->vpos > 0 || IT_STRING_CHARPOS (*it) < it->end_charpos-1))
12124 break;
12125 goto out;
12128 ++it->hpos;
12129 x += glyph->pixel_width;
12130 ++i;
12133 /* Stop at line end. */
12134 if (ITERATOR_AT_END_OF_LINE_P (it))
12135 break;
12137 set_iterator_to_next (it, 1);
12140 out:;
12142 row->displays_text_p = row->used[TEXT_AREA] != 0;
12144 /* Use default face for the border below the tool bar.
12146 FIXME: When auto-resize-tool-bars is grow-only, there is
12147 no additional border below the possibly empty tool-bar lines.
12148 So to make the extra empty lines look "normal", we have to
12149 use the tool-bar face for the border too. */
12150 if (!MATRIX_ROW_DISPLAYS_TEXT_P (row)
12151 && !EQ (Vauto_resize_tool_bars, Qgrow_only))
12152 it->face_id = DEFAULT_FACE_ID;
12154 extend_face_to_end_of_line (it);
12155 last = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
12156 last->right_box_line_p = 1;
12157 if (last == row->glyphs[TEXT_AREA])
12158 last->left_box_line_p = 1;
12160 /* Make line the desired height and center it vertically. */
12161 if ((height -= it->max_ascent + it->max_descent) > 0)
12163 /* Don't add more than one line height. */
12164 height %= FRAME_LINE_HEIGHT (it->f);
12165 it->max_ascent += height / 2;
12166 it->max_descent += (height + 1) / 2;
12169 compute_line_metrics (it);
12171 /* If line is empty, make it occupy the rest of the tool-bar. */
12172 if (!MATRIX_ROW_DISPLAYS_TEXT_P (row))
12174 row->height = row->phys_height = it->last_visible_y - row->y;
12175 row->visible_height = row->height;
12176 row->ascent = row->phys_ascent = 0;
12177 row->extra_line_spacing = 0;
12180 row->full_width_p = 1;
12181 row->continued_p = 0;
12182 row->truncated_on_left_p = 0;
12183 row->truncated_on_right_p = 0;
12185 it->current_x = it->hpos = 0;
12186 it->current_y += row->height;
12187 ++it->vpos;
12188 ++it->glyph_row;
12192 /* Max tool-bar height. Basically, this is what makes all other windows
12193 disappear when the frame gets too small. Rethink this! */
12195 #define MAX_FRAME_TOOL_BAR_HEIGHT(f) \
12196 ((FRAME_LINE_HEIGHT (f) * FRAME_LINES (f)))
12198 /* Value is the number of pixels needed to make all tool-bar items of
12199 frame F visible. The actual number of glyph rows needed is
12200 returned in *N_ROWS if non-NULL. */
12202 static int
12203 tool_bar_height (struct frame *f, int *n_rows, bool pixelwise)
12205 struct window *w = XWINDOW (f->tool_bar_window);
12206 struct it it;
12207 /* tool_bar_height is called from redisplay_tool_bar after building
12208 the desired matrix, so use (unused) mode-line row as temporary row to
12209 avoid destroying the first tool-bar row. */
12210 struct glyph_row *temp_row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
12212 /* Initialize an iterator for iteration over
12213 F->desired_tool_bar_string in the tool-bar window of frame F. */
12214 init_iterator (&it, w, -1, -1, temp_row, TOOL_BAR_FACE_ID);
12215 it.first_visible_x = 0;
12216 it.last_visible_x = WINDOW_PIXEL_WIDTH (w);
12217 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
12218 it.paragraph_embedding = L2R;
12220 while (!ITERATOR_AT_END_P (&it))
12222 clear_glyph_row (temp_row);
12223 it.glyph_row = temp_row;
12224 display_tool_bar_line (&it, -1);
12226 clear_glyph_row (temp_row);
12228 /* f->n_tool_bar_rows == 0 means "unknown"; -1 means no tool-bar. */
12229 if (n_rows)
12230 *n_rows = it.vpos > 0 ? it.vpos : -1;
12232 if (pixelwise)
12233 return it.current_y;
12234 else
12235 return (it.current_y + FRAME_LINE_HEIGHT (f) - 1) / FRAME_LINE_HEIGHT (f);
12238 #endif /* !USE_GTK && !HAVE_NS */
12240 #if defined USE_GTK || defined HAVE_NS
12241 EXFUN (Ftool_bar_height, 2) ATTRIBUTE_CONST;
12242 EXFUN (Ftool_bar_lines_needed, 1) ATTRIBUTE_CONST;
12243 #endif
12245 DEFUN ("tool-bar-height", Ftool_bar_height, Stool_bar_height,
12246 0, 2, 0,
12247 doc: /* Return the number of lines occupied by the tool bar of FRAME.
12248 If FRAME is nil or omitted, use the selected frame. Optional argument
12249 PIXELWISE non-nil means return the height of the tool bar in pixels. */)
12250 (Lisp_Object frame, Lisp_Object pixelwise)
12252 int height = 0;
12254 #if ! defined (USE_GTK) && ! defined (HAVE_NS)
12255 struct frame *f = decode_any_frame (frame);
12257 if (WINDOWP (f->tool_bar_window)
12258 && WINDOW_PIXEL_HEIGHT (XWINDOW (f->tool_bar_window)) > 0)
12260 update_tool_bar (f, 1);
12261 if (f->n_tool_bar_items)
12263 build_desired_tool_bar_string (f);
12264 height = tool_bar_height (f, NULL, NILP (pixelwise) ? 0 : 1);
12267 #endif
12269 return make_number (height);
12273 /* Display the tool-bar of frame F. Value is non-zero if tool-bar's
12274 height should be changed. */
12276 static int
12277 redisplay_tool_bar (struct frame *f)
12279 #if defined (USE_GTK) || defined (HAVE_NS)
12281 if (FRAME_EXTERNAL_TOOL_BAR (f))
12282 update_frame_tool_bar (f);
12283 return 0;
12285 #else /* !USE_GTK && !HAVE_NS */
12287 struct window *w;
12288 struct it it;
12289 struct glyph_row *row;
12291 /* If frame hasn't a tool-bar window or if it is zero-height, don't
12292 do anything. This means you must start with tool-bar-lines
12293 non-zero to get the auto-sizing effect. Or in other words, you
12294 can turn off tool-bars by specifying tool-bar-lines zero. */
12295 if (!WINDOWP (f->tool_bar_window)
12296 || (w = XWINDOW (f->tool_bar_window),
12297 WINDOW_PIXEL_HEIGHT (w) == 0))
12298 return 0;
12300 /* Set up an iterator for the tool-bar window. */
12301 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
12302 it.first_visible_x = 0;
12303 it.last_visible_x = WINDOW_PIXEL_WIDTH (w);
12304 row = it.glyph_row;
12306 /* Build a string that represents the contents of the tool-bar. */
12307 build_desired_tool_bar_string (f);
12308 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
12309 /* FIXME: This should be controlled by a user option. But it
12310 doesn't make sense to have an R2L tool bar if the menu bar cannot
12311 be drawn also R2L, and making the menu bar R2L is tricky due
12312 toolkit-specific code that implements it. If an R2L tool bar is
12313 ever supported, display_tool_bar_line should also be augmented to
12314 call unproduce_glyphs like display_line and display_string
12315 do. */
12316 it.paragraph_embedding = L2R;
12318 if (f->n_tool_bar_rows == 0)
12320 int new_height = tool_bar_height (f, &f->n_tool_bar_rows, 1);
12322 if (new_height != WINDOW_PIXEL_HEIGHT (w))
12324 Lisp_Object frame;
12325 int new_lines = ((new_height + FRAME_LINE_HEIGHT (f) - 1)
12326 / FRAME_LINE_HEIGHT (f));
12328 XSETFRAME (frame, f);
12329 Fmodify_frame_parameters (frame,
12330 list1 (Fcons (Qtool_bar_lines,
12331 make_number (new_lines))));
12332 /* Always do that now. */
12333 clear_glyph_matrix (w->desired_matrix);
12334 f->fonts_changed = 1;
12335 return 1;
12339 /* Display as many lines as needed to display all tool-bar items. */
12341 if (f->n_tool_bar_rows > 0)
12343 int border, rows, height, extra;
12345 if (TYPE_RANGED_INTEGERP (int, Vtool_bar_border))
12346 border = XINT (Vtool_bar_border);
12347 else if (EQ (Vtool_bar_border, Qinternal_border_width))
12348 border = FRAME_INTERNAL_BORDER_WIDTH (f);
12349 else if (EQ (Vtool_bar_border, Qborder_width))
12350 border = f->border_width;
12351 else
12352 border = 0;
12353 if (border < 0)
12354 border = 0;
12356 rows = f->n_tool_bar_rows;
12357 height = max (1, (it.last_visible_y - border) / rows);
12358 extra = it.last_visible_y - border - height * rows;
12360 while (it.current_y < it.last_visible_y)
12362 int h = 0;
12363 if (extra > 0 && rows-- > 0)
12365 h = (extra + rows - 1) / rows;
12366 extra -= h;
12368 display_tool_bar_line (&it, height + h);
12371 else
12373 while (it.current_y < it.last_visible_y)
12374 display_tool_bar_line (&it, 0);
12377 /* It doesn't make much sense to try scrolling in the tool-bar
12378 window, so don't do it. */
12379 w->desired_matrix->no_scrolling_p = 1;
12380 w->must_be_updated_p = 1;
12382 if (!NILP (Vauto_resize_tool_bars))
12384 /* Do we really allow the toolbar to occupy the whole frame? */
12385 int max_tool_bar_height = MAX_FRAME_TOOL_BAR_HEIGHT (f);
12386 int change_height_p = 0;
12388 /* If we couldn't display everything, change the tool-bar's
12389 height if there is room for more. */
12390 if (IT_STRING_CHARPOS (it) < it.end_charpos
12391 && it.current_y < max_tool_bar_height)
12392 change_height_p = 1;
12394 /* We subtract 1 because display_tool_bar_line advances the
12395 glyph_row pointer before returning to its caller. We want to
12396 examine the last glyph row produced by
12397 display_tool_bar_line. */
12398 row = it.glyph_row - 1;
12400 /* If there are blank lines at the end, except for a partially
12401 visible blank line at the end that is smaller than
12402 FRAME_LINE_HEIGHT, change the tool-bar's height. */
12403 if (!MATRIX_ROW_DISPLAYS_TEXT_P (row)
12404 && row->height >= FRAME_LINE_HEIGHT (f))
12405 change_height_p = 1;
12407 /* If row displays tool-bar items, but is partially visible,
12408 change the tool-bar's height. */
12409 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
12410 && MATRIX_ROW_BOTTOM_Y (row) > it.last_visible_y
12411 && MATRIX_ROW_BOTTOM_Y (row) < max_tool_bar_height)
12412 change_height_p = 1;
12414 /* Resize windows as needed by changing the `tool-bar-lines'
12415 frame parameter. */
12416 if (change_height_p)
12418 Lisp_Object frame;
12419 int nrows;
12420 int new_height = tool_bar_height (f, &nrows, 1);
12422 change_height_p = ((EQ (Vauto_resize_tool_bars, Qgrow_only)
12423 && !f->minimize_tool_bar_window_p)
12424 ? (new_height > WINDOW_PIXEL_HEIGHT (w))
12425 : (new_height != WINDOW_PIXEL_HEIGHT (w)));
12426 f->minimize_tool_bar_window_p = 0;
12428 if (change_height_p)
12430 /* Current size of the tool-bar window in canonical line
12431 units. */
12432 int old_lines = WINDOW_TOTAL_LINES (w);
12433 /* Required size of the tool-bar window in canonical
12434 line units. */
12435 int new_lines = ((new_height + FRAME_LINE_HEIGHT (f) - 1)
12436 / FRAME_LINE_HEIGHT (f));
12437 /* Maximum size of the tool-bar window in canonical line
12438 units that this frame can allow. */
12439 int max_lines =
12440 WINDOW_TOTAL_LINES (XWINDOW (FRAME_ROOT_WINDOW (f))) - 1;
12442 /* Don't try to change the tool-bar window size and set
12443 the fonts_changed flag unless really necessary. That
12444 flag causes redisplay to give up and retry
12445 redisplaying the frame from scratch, so setting it
12446 unnecessarily can lead to nasty redisplay loops. */
12447 if (new_lines <= max_lines
12448 && eabs (new_lines - old_lines) >= 1)
12450 XSETFRAME (frame, f);
12451 Fmodify_frame_parameters (frame,
12452 list1 (Fcons (Qtool_bar_lines,
12453 make_number (new_lines))));
12454 clear_glyph_matrix (w->desired_matrix);
12455 f->n_tool_bar_rows = nrows;
12456 f->fonts_changed = 1;
12457 return 1;
12463 f->minimize_tool_bar_window_p = 0;
12464 return 0;
12466 #endif /* USE_GTK || HAVE_NS */
12469 #if ! defined (USE_GTK) && ! defined (HAVE_NS)
12471 /* Get information about the tool-bar item which is displayed in GLYPH
12472 on frame F. Return in *PROP_IDX the index where tool-bar item
12473 properties start in F->tool_bar_items. Value is zero if
12474 GLYPH doesn't display a tool-bar item. */
12476 static int
12477 tool_bar_item_info (struct frame *f, struct glyph *glyph, int *prop_idx)
12479 Lisp_Object prop;
12480 int success_p;
12481 int charpos;
12483 /* This function can be called asynchronously, which means we must
12484 exclude any possibility that Fget_text_property signals an
12485 error. */
12486 charpos = min (SCHARS (f->current_tool_bar_string), glyph->charpos);
12487 charpos = max (0, charpos);
12489 /* Get the text property `menu-item' at pos. The value of that
12490 property is the start index of this item's properties in
12491 F->tool_bar_items. */
12492 prop = Fget_text_property (make_number (charpos),
12493 Qmenu_item, f->current_tool_bar_string);
12494 if (INTEGERP (prop))
12496 *prop_idx = XINT (prop);
12497 success_p = 1;
12499 else
12500 success_p = 0;
12502 return success_p;
12506 /* Get information about the tool-bar item at position X/Y on frame F.
12507 Return in *GLYPH a pointer to the glyph of the tool-bar item in
12508 the current matrix of the tool-bar window of F, or NULL if not
12509 on a tool-bar item. Return in *PROP_IDX the index of the tool-bar
12510 item in F->tool_bar_items. Value is
12512 -1 if X/Y is not on a tool-bar item
12513 0 if X/Y is on the same item that was highlighted before.
12514 1 otherwise. */
12516 static int
12517 get_tool_bar_item (struct frame *f, int x, int y, struct glyph **glyph,
12518 int *hpos, int *vpos, int *prop_idx)
12520 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12521 struct window *w = XWINDOW (f->tool_bar_window);
12522 int area;
12524 /* Find the glyph under X/Y. */
12525 *glyph = x_y_to_hpos_vpos (w, x, y, hpos, vpos, 0, 0, &area);
12526 if (*glyph == NULL)
12527 return -1;
12529 /* Get the start of this tool-bar item's properties in
12530 f->tool_bar_items. */
12531 if (!tool_bar_item_info (f, *glyph, prop_idx))
12532 return -1;
12534 /* Is mouse on the highlighted item? */
12535 if (EQ (f->tool_bar_window, hlinfo->mouse_face_window)
12536 && *vpos >= hlinfo->mouse_face_beg_row
12537 && *vpos <= hlinfo->mouse_face_end_row
12538 && (*vpos > hlinfo->mouse_face_beg_row
12539 || *hpos >= hlinfo->mouse_face_beg_col)
12540 && (*vpos < hlinfo->mouse_face_end_row
12541 || *hpos < hlinfo->mouse_face_end_col
12542 || hlinfo->mouse_face_past_end))
12543 return 0;
12545 return 1;
12549 /* EXPORT:
12550 Handle mouse button event on the tool-bar of frame F, at
12551 frame-relative coordinates X/Y. DOWN_P is 1 for a button press,
12552 0 for button release. MODIFIERS is event modifiers for button
12553 release. */
12555 void
12556 handle_tool_bar_click (struct frame *f, int x, int y, int down_p,
12557 int modifiers)
12559 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12560 struct window *w = XWINDOW (f->tool_bar_window);
12561 int hpos, vpos, prop_idx;
12562 struct glyph *glyph;
12563 Lisp_Object enabled_p;
12564 int ts;
12566 /* If not on the highlighted tool-bar item, and mouse-highlight is
12567 non-nil, return. This is so we generate the tool-bar button
12568 click only when the mouse button is released on the same item as
12569 where it was pressed. However, when mouse-highlight is disabled,
12570 generate the click when the button is released regardless of the
12571 highlight, since tool-bar items are not highlighted in that
12572 case. */
12573 frame_to_window_pixel_xy (w, &x, &y);
12574 ts = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
12575 if (ts == -1
12576 || (ts != 0 && !NILP (Vmouse_highlight)))
12577 return;
12579 /* When mouse-highlight is off, generate the click for the item
12580 where the button was pressed, disregarding where it was
12581 released. */
12582 if (NILP (Vmouse_highlight) && !down_p)
12583 prop_idx = last_tool_bar_item;
12585 /* If item is disabled, do nothing. */
12586 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
12587 if (NILP (enabled_p))
12588 return;
12590 if (down_p)
12592 /* Show item in pressed state. */
12593 if (!NILP (Vmouse_highlight))
12594 show_mouse_face (hlinfo, DRAW_IMAGE_SUNKEN);
12595 last_tool_bar_item = prop_idx;
12597 else
12599 Lisp_Object key, frame;
12600 struct input_event event;
12601 EVENT_INIT (event);
12603 /* Show item in released state. */
12604 if (!NILP (Vmouse_highlight))
12605 show_mouse_face (hlinfo, DRAW_IMAGE_RAISED);
12607 key = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_KEY);
12609 XSETFRAME (frame, f);
12610 event.kind = TOOL_BAR_EVENT;
12611 event.frame_or_window = frame;
12612 event.arg = frame;
12613 kbd_buffer_store_event (&event);
12615 event.kind = TOOL_BAR_EVENT;
12616 event.frame_or_window = frame;
12617 event.arg = key;
12618 event.modifiers = modifiers;
12619 kbd_buffer_store_event (&event);
12620 last_tool_bar_item = -1;
12625 /* Possibly highlight a tool-bar item on frame F when mouse moves to
12626 tool-bar window-relative coordinates X/Y. Called from
12627 note_mouse_highlight. */
12629 static void
12630 note_tool_bar_highlight (struct frame *f, int x, int y)
12632 Lisp_Object window = f->tool_bar_window;
12633 struct window *w = XWINDOW (window);
12634 Display_Info *dpyinfo = FRAME_DISPLAY_INFO (f);
12635 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12636 int hpos, vpos;
12637 struct glyph *glyph;
12638 struct glyph_row *row;
12639 int i;
12640 Lisp_Object enabled_p;
12641 int prop_idx;
12642 enum draw_glyphs_face draw = DRAW_IMAGE_RAISED;
12643 int mouse_down_p, rc;
12645 /* Function note_mouse_highlight is called with negative X/Y
12646 values when mouse moves outside of the frame. */
12647 if (x <= 0 || y <= 0)
12649 clear_mouse_face (hlinfo);
12650 return;
12653 rc = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
12654 if (rc < 0)
12656 /* Not on tool-bar item. */
12657 clear_mouse_face (hlinfo);
12658 return;
12660 else if (rc == 0)
12661 /* On same tool-bar item as before. */
12662 goto set_help_echo;
12664 clear_mouse_face (hlinfo);
12666 /* Mouse is down, but on different tool-bar item? */
12667 mouse_down_p = (x_mouse_grabbed (dpyinfo)
12668 && f == dpyinfo->last_mouse_frame);
12670 if (mouse_down_p
12671 && last_tool_bar_item != prop_idx)
12672 return;
12674 draw = mouse_down_p ? DRAW_IMAGE_SUNKEN : DRAW_IMAGE_RAISED;
12676 /* If tool-bar item is not enabled, don't highlight it. */
12677 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
12678 if (!NILP (enabled_p) && !NILP (Vmouse_highlight))
12680 /* Compute the x-position of the glyph. In front and past the
12681 image is a space. We include this in the highlighted area. */
12682 row = MATRIX_ROW (w->current_matrix, vpos);
12683 for (i = x = 0; i < hpos; ++i)
12684 x += row->glyphs[TEXT_AREA][i].pixel_width;
12686 /* Record this as the current active region. */
12687 hlinfo->mouse_face_beg_col = hpos;
12688 hlinfo->mouse_face_beg_row = vpos;
12689 hlinfo->mouse_face_beg_x = x;
12690 hlinfo->mouse_face_past_end = 0;
12692 hlinfo->mouse_face_end_col = hpos + 1;
12693 hlinfo->mouse_face_end_row = vpos;
12694 hlinfo->mouse_face_end_x = x + glyph->pixel_width;
12695 hlinfo->mouse_face_window = window;
12696 hlinfo->mouse_face_face_id = TOOL_BAR_FACE_ID;
12698 /* Display it as active. */
12699 show_mouse_face (hlinfo, draw);
12702 set_help_echo:
12704 /* Set help_echo_string to a help string to display for this tool-bar item.
12705 XTread_socket does the rest. */
12706 help_echo_object = help_echo_window = Qnil;
12707 help_echo_pos = -1;
12708 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_HELP);
12709 if (NILP (help_echo_string))
12710 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_CAPTION);
12713 #endif /* !USE_GTK && !HAVE_NS */
12715 #endif /* HAVE_WINDOW_SYSTEM */
12719 /************************************************************************
12720 Horizontal scrolling
12721 ************************************************************************/
12723 static int hscroll_window_tree (Lisp_Object);
12724 static int hscroll_windows (Lisp_Object);
12726 /* For all leaf windows in the window tree rooted at WINDOW, set their
12727 hscroll value so that PT is (i) visible in the window, and (ii) so
12728 that it is not within a certain margin at the window's left and
12729 right border. Value is non-zero if any window's hscroll has been
12730 changed. */
12732 static int
12733 hscroll_window_tree (Lisp_Object window)
12735 int hscrolled_p = 0;
12736 int hscroll_relative_p = FLOATP (Vhscroll_step);
12737 int hscroll_step_abs = 0;
12738 double hscroll_step_rel = 0;
12740 if (hscroll_relative_p)
12742 hscroll_step_rel = XFLOAT_DATA (Vhscroll_step);
12743 if (hscroll_step_rel < 0)
12745 hscroll_relative_p = 0;
12746 hscroll_step_abs = 0;
12749 else if (TYPE_RANGED_INTEGERP (int, Vhscroll_step))
12751 hscroll_step_abs = XINT (Vhscroll_step);
12752 if (hscroll_step_abs < 0)
12753 hscroll_step_abs = 0;
12755 else
12756 hscroll_step_abs = 0;
12758 while (WINDOWP (window))
12760 struct window *w = XWINDOW (window);
12762 if (WINDOWP (w->contents))
12763 hscrolled_p |= hscroll_window_tree (w->contents);
12764 else if (w->cursor.vpos >= 0)
12766 int h_margin;
12767 int text_area_width;
12768 struct glyph_row *cursor_row;
12769 struct glyph_row *bottom_row;
12770 int row_r2l_p;
12772 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->desired_matrix, w);
12773 if (w->cursor.vpos < bottom_row - w->desired_matrix->rows)
12774 cursor_row = MATRIX_ROW (w->desired_matrix, w->cursor.vpos);
12775 else
12776 cursor_row = bottom_row - 1;
12778 if (!cursor_row->enabled_p)
12780 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
12781 if (w->cursor.vpos < bottom_row - w->current_matrix->rows)
12782 cursor_row = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
12783 else
12784 cursor_row = bottom_row - 1;
12786 row_r2l_p = cursor_row->reversed_p;
12788 text_area_width = window_box_width (w, TEXT_AREA);
12790 /* Scroll when cursor is inside this scroll margin. */
12791 h_margin = hscroll_margin * WINDOW_FRAME_COLUMN_WIDTH (w);
12793 if (!NILP (Fbuffer_local_value (Qauto_hscroll_mode, w->contents))
12794 /* For left-to-right rows, hscroll when cursor is either
12795 (i) inside the right hscroll margin, or (ii) if it is
12796 inside the left margin and the window is already
12797 hscrolled. */
12798 && ((!row_r2l_p
12799 && ((w->hscroll
12800 && w->cursor.x <= h_margin)
12801 || (cursor_row->enabled_p
12802 && cursor_row->truncated_on_right_p
12803 && (w->cursor.x >= text_area_width - h_margin))))
12804 /* For right-to-left rows, the logic is similar,
12805 except that rules for scrolling to left and right
12806 are reversed. E.g., if cursor.x <= h_margin, we
12807 need to hscroll "to the right" unconditionally,
12808 and that will scroll the screen to the left so as
12809 to reveal the next portion of the row. */
12810 || (row_r2l_p
12811 && ((cursor_row->enabled_p
12812 /* FIXME: It is confusing to set the
12813 truncated_on_right_p flag when R2L rows
12814 are actually truncated on the left. */
12815 && cursor_row->truncated_on_right_p
12816 && w->cursor.x <= h_margin)
12817 || (w->hscroll
12818 && (w->cursor.x >= text_area_width - h_margin))))))
12820 struct it it;
12821 ptrdiff_t hscroll;
12822 struct buffer *saved_current_buffer;
12823 ptrdiff_t pt;
12824 int wanted_x;
12826 /* Find point in a display of infinite width. */
12827 saved_current_buffer = current_buffer;
12828 current_buffer = XBUFFER (w->contents);
12830 if (w == XWINDOW (selected_window))
12831 pt = PT;
12832 else
12833 pt = clip_to_bounds (BEGV, marker_position (w->pointm), ZV);
12835 /* Move iterator to pt starting at cursor_row->start in
12836 a line with infinite width. */
12837 init_to_row_start (&it, w, cursor_row);
12838 it.last_visible_x = INFINITY;
12839 move_it_in_display_line_to (&it, pt, -1, MOVE_TO_POS);
12840 current_buffer = saved_current_buffer;
12842 /* Position cursor in window. */
12843 if (!hscroll_relative_p && hscroll_step_abs == 0)
12844 hscroll = max (0, (it.current_x
12845 - (ITERATOR_AT_END_OF_LINE_P (&it)
12846 ? (text_area_width - 4 * FRAME_COLUMN_WIDTH (it.f))
12847 : (text_area_width / 2))))
12848 / FRAME_COLUMN_WIDTH (it.f);
12849 else if ((!row_r2l_p
12850 && w->cursor.x >= text_area_width - h_margin)
12851 || (row_r2l_p && w->cursor.x <= h_margin))
12853 if (hscroll_relative_p)
12854 wanted_x = text_area_width * (1 - hscroll_step_rel)
12855 - h_margin;
12856 else
12857 wanted_x = text_area_width
12858 - 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 else
12865 if (hscroll_relative_p)
12866 wanted_x = text_area_width * hscroll_step_rel
12867 + h_margin;
12868 else
12869 wanted_x = hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
12870 + h_margin;
12871 hscroll
12872 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
12874 hscroll = max (hscroll, w->min_hscroll);
12876 /* Don't prevent redisplay optimizations if hscroll
12877 hasn't changed, as it will unnecessarily slow down
12878 redisplay. */
12879 if (w->hscroll != hscroll)
12881 XBUFFER (w->contents)->prevent_redisplay_optimizations_p = 1;
12882 w->hscroll = hscroll;
12883 hscrolled_p = 1;
12888 window = w->next;
12891 /* Value is non-zero if hscroll of any leaf window has been changed. */
12892 return hscrolled_p;
12896 /* Set hscroll so that cursor is visible and not inside horizontal
12897 scroll margins for all windows in the tree rooted at WINDOW. See
12898 also hscroll_window_tree above. Value is non-zero if any window's
12899 hscroll has been changed. If it has, desired matrices on the frame
12900 of WINDOW are cleared. */
12902 static int
12903 hscroll_windows (Lisp_Object window)
12905 int hscrolled_p = hscroll_window_tree (window);
12906 if (hscrolled_p)
12907 clear_desired_matrices (XFRAME (WINDOW_FRAME (XWINDOW (window))));
12908 return hscrolled_p;
12913 /************************************************************************
12914 Redisplay
12915 ************************************************************************/
12917 /* Variables holding some state of redisplay if GLYPH_DEBUG is defined
12918 to a non-zero value. This is sometimes handy to have in a debugger
12919 session. */
12921 #ifdef GLYPH_DEBUG
12923 /* First and last unchanged row for try_window_id. */
12925 static int debug_first_unchanged_at_end_vpos;
12926 static int debug_last_unchanged_at_beg_vpos;
12928 /* Delta vpos and y. */
12930 static int debug_dvpos, debug_dy;
12932 /* Delta in characters and bytes for try_window_id. */
12934 static ptrdiff_t debug_delta, debug_delta_bytes;
12936 /* Values of window_end_pos and window_end_vpos at the end of
12937 try_window_id. */
12939 static ptrdiff_t debug_end_vpos;
12941 /* Append a string to W->desired_matrix->method. FMT is a printf
12942 format string. If trace_redisplay_p is true also printf the
12943 resulting string to stderr. */
12945 static void debug_method_add (struct window *, char const *, ...)
12946 ATTRIBUTE_FORMAT_PRINTF (2, 3);
12948 static void
12949 debug_method_add (struct window *w, char const *fmt, ...)
12951 void *ptr = w;
12952 char *method = w->desired_matrix->method;
12953 int len = strlen (method);
12954 int size = sizeof w->desired_matrix->method;
12955 int remaining = size - len - 1;
12956 va_list ap;
12958 if (len && remaining)
12960 method[len] = '|';
12961 --remaining, ++len;
12964 va_start (ap, fmt);
12965 vsnprintf (method + len, remaining + 1, fmt, ap);
12966 va_end (ap);
12968 if (trace_redisplay_p)
12969 fprintf (stderr, "%p (%s): %s\n",
12970 ptr,
12971 ((BUFFERP (w->contents)
12972 && STRINGP (BVAR (XBUFFER (w->contents), name)))
12973 ? SSDATA (BVAR (XBUFFER (w->contents), name))
12974 : "no buffer"),
12975 method + len);
12978 #endif /* GLYPH_DEBUG */
12981 /* Value is non-zero if all changes in window W, which displays
12982 current_buffer, are in the text between START and END. START is a
12983 buffer position, END is given as a distance from Z. Used in
12984 redisplay_internal for display optimization. */
12986 static int
12987 text_outside_line_unchanged_p (struct window *w,
12988 ptrdiff_t start, ptrdiff_t end)
12990 int unchanged_p = 1;
12992 /* If text or overlays have changed, see where. */
12993 if (window_outdated (w))
12995 /* Gap in the line? */
12996 if (GPT < start || Z - GPT < end)
12997 unchanged_p = 0;
12999 /* Changes start in front of the line, or end after it? */
13000 if (unchanged_p
13001 && (BEG_UNCHANGED < start - 1
13002 || END_UNCHANGED < end))
13003 unchanged_p = 0;
13005 /* If selective display, can't optimize if changes start at the
13006 beginning of the line. */
13007 if (unchanged_p
13008 && INTEGERP (BVAR (current_buffer, selective_display))
13009 && XINT (BVAR (current_buffer, selective_display)) > 0
13010 && (BEG_UNCHANGED < start || GPT <= start))
13011 unchanged_p = 0;
13013 /* If there are overlays at the start or end of the line, these
13014 may have overlay strings with newlines in them. A change at
13015 START, for instance, may actually concern the display of such
13016 overlay strings as well, and they are displayed on different
13017 lines. So, quickly rule out this case. (For the future, it
13018 might be desirable to implement something more telling than
13019 just BEG/END_UNCHANGED.) */
13020 if (unchanged_p)
13022 if (BEG + BEG_UNCHANGED == start
13023 && overlay_touches_p (start))
13024 unchanged_p = 0;
13025 if (END_UNCHANGED == end
13026 && overlay_touches_p (Z - end))
13027 unchanged_p = 0;
13030 /* Under bidi reordering, adding or deleting a character in the
13031 beginning of a paragraph, before the first strong directional
13032 character, can change the base direction of the paragraph (unless
13033 the buffer specifies a fixed paragraph direction), which will
13034 require to redisplay the whole paragraph. It might be worthwhile
13035 to find the paragraph limits and widen the range of redisplayed
13036 lines to that, but for now just give up this optimization. */
13037 if (!NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering))
13038 && NILP (BVAR (XBUFFER (w->contents), bidi_paragraph_direction)))
13039 unchanged_p = 0;
13042 return unchanged_p;
13046 /* Do a frame update, taking possible shortcuts into account. This is
13047 the main external entry point for redisplay.
13049 If the last redisplay displayed an echo area message and that message
13050 is no longer requested, we clear the echo area or bring back the
13051 mini-buffer if that is in use. */
13053 void
13054 redisplay (void)
13056 redisplay_internal ();
13060 static Lisp_Object
13061 overlay_arrow_string_or_property (Lisp_Object var)
13063 Lisp_Object val;
13065 if (val = Fget (var, Qoverlay_arrow_string), STRINGP (val))
13066 return val;
13068 return Voverlay_arrow_string;
13071 /* Return 1 if there are any overlay-arrows in current_buffer. */
13072 static int
13073 overlay_arrow_in_current_buffer_p (void)
13075 Lisp_Object vlist;
13077 for (vlist = Voverlay_arrow_variable_list;
13078 CONSP (vlist);
13079 vlist = XCDR (vlist))
13081 Lisp_Object var = XCAR (vlist);
13082 Lisp_Object val;
13084 if (!SYMBOLP (var))
13085 continue;
13086 val = find_symbol_value (var);
13087 if (MARKERP (val)
13088 && current_buffer == XMARKER (val)->buffer)
13089 return 1;
13091 return 0;
13095 /* Return 1 if any overlay_arrows have moved or overlay-arrow-string
13096 has changed. */
13098 static int
13099 overlay_arrows_changed_p (void)
13101 Lisp_Object vlist;
13103 for (vlist = Voverlay_arrow_variable_list;
13104 CONSP (vlist);
13105 vlist = XCDR (vlist))
13107 Lisp_Object var = XCAR (vlist);
13108 Lisp_Object val, pstr;
13110 if (!SYMBOLP (var))
13111 continue;
13112 val = find_symbol_value (var);
13113 if (!MARKERP (val))
13114 continue;
13115 if (! EQ (COERCE_MARKER (val),
13116 Fget (var, Qlast_arrow_position))
13117 || ! (pstr = overlay_arrow_string_or_property (var),
13118 EQ (pstr, Fget (var, Qlast_arrow_string))))
13119 return 1;
13121 return 0;
13124 /* Mark overlay arrows to be updated on next redisplay. */
13126 static void
13127 update_overlay_arrows (int up_to_date)
13129 Lisp_Object vlist;
13131 for (vlist = Voverlay_arrow_variable_list;
13132 CONSP (vlist);
13133 vlist = XCDR (vlist))
13135 Lisp_Object var = XCAR (vlist);
13137 if (!SYMBOLP (var))
13138 continue;
13140 if (up_to_date > 0)
13142 Lisp_Object val = find_symbol_value (var);
13143 Fput (var, Qlast_arrow_position,
13144 COERCE_MARKER (val));
13145 Fput (var, Qlast_arrow_string,
13146 overlay_arrow_string_or_property (var));
13148 else if (up_to_date < 0
13149 || !NILP (Fget (var, Qlast_arrow_position)))
13151 Fput (var, Qlast_arrow_position, Qt);
13152 Fput (var, Qlast_arrow_string, Qt);
13158 /* Return overlay arrow string to display at row.
13159 Return integer (bitmap number) for arrow bitmap in left fringe.
13160 Return nil if no overlay arrow. */
13162 static Lisp_Object
13163 overlay_arrow_at_row (struct it *it, struct glyph_row *row)
13165 Lisp_Object vlist;
13167 for (vlist = Voverlay_arrow_variable_list;
13168 CONSP (vlist);
13169 vlist = XCDR (vlist))
13171 Lisp_Object var = XCAR (vlist);
13172 Lisp_Object val;
13174 if (!SYMBOLP (var))
13175 continue;
13177 val = find_symbol_value (var);
13179 if (MARKERP (val)
13180 && current_buffer == XMARKER (val)->buffer
13181 && (MATRIX_ROW_START_CHARPOS (row) == marker_position (val)))
13183 if (FRAME_WINDOW_P (it->f)
13184 /* FIXME: if ROW->reversed_p is set, this should test
13185 the right fringe, not the left one. */
13186 && WINDOW_LEFT_FRINGE_WIDTH (it->w) > 0)
13188 #ifdef HAVE_WINDOW_SYSTEM
13189 if (val = Fget (var, Qoverlay_arrow_bitmap), SYMBOLP (val))
13191 int fringe_bitmap;
13192 if ((fringe_bitmap = lookup_fringe_bitmap (val)) != 0)
13193 return make_number (fringe_bitmap);
13195 #endif
13196 return make_number (-1); /* Use default arrow bitmap. */
13198 return overlay_arrow_string_or_property (var);
13202 return Qnil;
13205 /* Return 1 if point moved out of or into a composition. Otherwise
13206 return 0. PREV_BUF and PREV_PT are the last point buffer and
13207 position. BUF and PT are the current point buffer and position. */
13209 static int
13210 check_point_in_composition (struct buffer *prev_buf, ptrdiff_t prev_pt,
13211 struct buffer *buf, ptrdiff_t pt)
13213 ptrdiff_t start, end;
13214 Lisp_Object prop;
13215 Lisp_Object buffer;
13217 XSETBUFFER (buffer, buf);
13218 /* Check a composition at the last point if point moved within the
13219 same buffer. */
13220 if (prev_buf == buf)
13222 if (prev_pt == pt)
13223 /* Point didn't move. */
13224 return 0;
13226 if (prev_pt > BUF_BEGV (buf) && prev_pt < BUF_ZV (buf)
13227 && find_composition (prev_pt, -1, &start, &end, &prop, buffer)
13228 && composition_valid_p (start, end, prop)
13229 && start < prev_pt && end > prev_pt)
13230 /* The last point was within the composition. Return 1 iff
13231 point moved out of the composition. */
13232 return (pt <= start || pt >= end);
13235 /* Check a composition at the current point. */
13236 return (pt > BUF_BEGV (buf) && pt < BUF_ZV (buf)
13237 && find_composition (pt, -1, &start, &end, &prop, buffer)
13238 && composition_valid_p (start, end, prop)
13239 && start < pt && end > pt);
13242 /* Reconsider the clip changes of buffer which is displayed in W. */
13244 static void
13245 reconsider_clip_changes (struct window *w)
13247 struct buffer *b = XBUFFER (w->contents);
13249 if (b->clip_changed
13250 && w->window_end_valid
13251 && w->current_matrix->buffer == b
13252 && w->current_matrix->zv == BUF_ZV (b)
13253 && w->current_matrix->begv == BUF_BEGV (b))
13254 b->clip_changed = 0;
13256 /* If display wasn't paused, and W is not a tool bar window, see if
13257 point has been moved into or out of a composition. In that case,
13258 we set b->clip_changed to 1 to force updating the screen. If
13259 b->clip_changed has already been set to 1, we can skip this
13260 check. */
13261 if (!b->clip_changed && w->window_end_valid)
13263 ptrdiff_t pt = (w == XWINDOW (selected_window)
13264 ? PT : marker_position (w->pointm));
13266 if ((w->current_matrix->buffer != b || pt != w->last_point)
13267 && check_point_in_composition (w->current_matrix->buffer,
13268 w->last_point, b, pt))
13269 b->clip_changed = 1;
13273 static void
13274 propagate_buffer_redisplay (void)
13275 { /* Resetting b->text->redisplay is problematic!
13276 We can't just reset it in the case that some window that displays
13277 it has not been redisplayed; and such a window can stay
13278 unredisplayed for a long time if it's currently invisible.
13279 But we do want to reset it at the end of redisplay otherwise
13280 its displayed windows will keep being redisplayed over and over
13281 again.
13282 So we copy all b->text->redisplay flags up to their windows here,
13283 such that mark_window_display_accurate can safely reset
13284 b->text->redisplay. */
13285 Lisp_Object ws = window_list ();
13286 for (; CONSP (ws); ws = XCDR (ws))
13288 struct window *thisw = XWINDOW (XCAR (ws));
13289 struct buffer *thisb = XBUFFER (thisw->contents);
13290 if (thisb->text->redisplay)
13291 thisw->redisplay = true;
13295 #define STOP_POLLING \
13296 do { if (! polling_stopped_here) stop_polling (); \
13297 polling_stopped_here = 1; } while (0)
13299 #define RESUME_POLLING \
13300 do { if (polling_stopped_here) start_polling (); \
13301 polling_stopped_here = 0; } while (0)
13304 /* Perhaps in the future avoid recentering windows if it
13305 is not necessary; currently that causes some problems. */
13307 static void
13308 redisplay_internal (void)
13310 struct window *w = XWINDOW (selected_window);
13311 struct window *sw;
13312 struct frame *fr;
13313 int pending;
13314 bool must_finish = 0, match_p;
13315 struct text_pos tlbufpos, tlendpos;
13316 int number_of_visible_frames;
13317 ptrdiff_t count;
13318 struct frame *sf;
13319 int polling_stopped_here = 0;
13320 Lisp_Object tail, frame;
13322 /* True means redisplay has to consider all windows on all
13323 frames. False, only selected_window is considered. */
13324 bool consider_all_windows_p;
13326 /* True means redisplay has to redisplay the miniwindow. */
13327 bool update_miniwindow_p = false;
13329 TRACE ((stderr, "redisplay_internal %d\n", redisplaying_p));
13331 /* No redisplay if running in batch mode or frame is not yet fully
13332 initialized, or redisplay is explicitly turned off by setting
13333 Vinhibit_redisplay. */
13334 if (FRAME_INITIAL_P (SELECTED_FRAME ())
13335 || !NILP (Vinhibit_redisplay))
13336 return;
13338 /* Don't examine these until after testing Vinhibit_redisplay.
13339 When Emacs is shutting down, perhaps because its connection to
13340 X has dropped, we should not look at them at all. */
13341 fr = XFRAME (w->frame);
13342 sf = SELECTED_FRAME ();
13344 if (!fr->glyphs_initialized_p)
13345 return;
13347 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS)
13348 if (popup_activated ())
13349 return;
13350 #endif
13352 /* I don't think this happens but let's be paranoid. */
13353 if (redisplaying_p)
13354 return;
13356 /* Record a function that clears redisplaying_p
13357 when we leave this function. */
13358 count = SPECPDL_INDEX ();
13359 record_unwind_protect_void (unwind_redisplay);
13360 redisplaying_p = 1;
13361 specbind (Qinhibit_free_realized_faces, Qnil);
13363 /* Record this function, so it appears on the profiler's backtraces. */
13364 record_in_backtrace (Qredisplay_internal, &Qnil, 0);
13366 FOR_EACH_FRAME (tail, frame)
13367 XFRAME (frame)->already_hscrolled_p = 0;
13369 retry:
13370 /* Remember the currently selected window. */
13371 sw = w;
13373 pending = 0;
13374 last_escape_glyph_frame = NULL;
13375 last_escape_glyph_face_id = (1 << FACE_ID_BITS);
13376 last_glyphless_glyph_frame = NULL;
13377 last_glyphless_glyph_face_id = (1 << FACE_ID_BITS);
13379 /* If face_change_count is non-zero, init_iterator will free all
13380 realized faces, which includes the faces referenced from current
13381 matrices. So, we can't reuse current matrices in this case. */
13382 if (face_change_count)
13383 windows_or_buffers_changed = 47;
13385 if ((FRAME_TERMCAP_P (sf) || FRAME_MSDOS_P (sf))
13386 && FRAME_TTY (sf)->previous_frame != sf)
13388 /* Since frames on a single ASCII terminal share the same
13389 display area, displaying a different frame means redisplay
13390 the whole thing. */
13391 SET_FRAME_GARBAGED (sf);
13392 #ifndef DOS_NT
13393 set_tty_color_mode (FRAME_TTY (sf), sf);
13394 #endif
13395 FRAME_TTY (sf)->previous_frame = sf;
13398 /* Set the visible flags for all frames. Do this before checking for
13399 resized or garbaged frames; they want to know if their frames are
13400 visible. See the comment in frame.h for FRAME_SAMPLE_VISIBILITY. */
13401 number_of_visible_frames = 0;
13403 FOR_EACH_FRAME (tail, frame)
13405 struct frame *f = XFRAME (frame);
13407 if (FRAME_VISIBLE_P (f))
13409 ++number_of_visible_frames;
13410 /* Adjust matrices for visible frames only. */
13411 if (f->fonts_changed)
13413 adjust_frame_glyphs (f);
13414 f->fonts_changed = 0;
13416 /* If cursor type has been changed on the frame
13417 other than selected, consider all frames. */
13418 if (f != sf && f->cursor_type_changed)
13419 update_mode_lines = 31;
13421 clear_desired_matrices (f);
13424 /* Notice any pending interrupt request to change frame size. */
13425 do_pending_window_change (1);
13427 /* do_pending_window_change could change the selected_window due to
13428 frame resizing which makes the selected window too small. */
13429 if (WINDOWP (selected_window) && (w = XWINDOW (selected_window)) != sw)
13430 sw = w;
13432 /* Clear frames marked as garbaged. */
13433 clear_garbaged_frames ();
13435 /* Build menubar and tool-bar items. */
13436 if (NILP (Vmemory_full))
13437 prepare_menu_bars ();
13439 reconsider_clip_changes (w);
13441 /* In most cases selected window displays current buffer. */
13442 match_p = XBUFFER (w->contents) == current_buffer;
13443 if (match_p)
13445 /* Detect case that we need to write or remove a star in the mode line. */
13446 if ((SAVE_MODIFF < MODIFF) != w->last_had_star)
13447 w->update_mode_line = 1;
13449 if (mode_line_update_needed (w))
13450 w->update_mode_line = 1;
13453 /* Normally the message* functions will have already displayed and
13454 updated the echo area, but the frame may have been trashed, or
13455 the update may have been preempted, so display the echo area
13456 again here. Checking message_cleared_p captures the case that
13457 the echo area should be cleared. */
13458 if ((!NILP (echo_area_buffer[0]) && !display_last_displayed_message_p)
13459 || (!NILP (echo_area_buffer[1]) && display_last_displayed_message_p)
13460 || (message_cleared_p
13461 && minibuf_level == 0
13462 /* If the mini-window is currently selected, this means the
13463 echo-area doesn't show through. */
13464 && !MINI_WINDOW_P (XWINDOW (selected_window))))
13466 int window_height_changed_p = echo_area_display (0);
13468 if (message_cleared_p)
13469 update_miniwindow_p = true;
13471 must_finish = 1;
13473 /* If we don't display the current message, don't clear the
13474 message_cleared_p flag, because, if we did, we wouldn't clear
13475 the echo area in the next redisplay which doesn't preserve
13476 the echo area. */
13477 if (!display_last_displayed_message_p)
13478 message_cleared_p = 0;
13480 if (window_height_changed_p)
13482 windows_or_buffers_changed = 50;
13484 /* If window configuration was changed, frames may have been
13485 marked garbaged. Clear them or we will experience
13486 surprises wrt scrolling. */
13487 clear_garbaged_frames ();
13490 else if (EQ (selected_window, minibuf_window)
13491 && (current_buffer->clip_changed || window_outdated (w))
13492 && resize_mini_window (w, 0))
13494 /* Resized active mini-window to fit the size of what it is
13495 showing if its contents might have changed. */
13496 must_finish = 1;
13498 /* If window configuration was changed, frames may have been
13499 marked garbaged. Clear them or we will experience
13500 surprises wrt scrolling. */
13501 clear_garbaged_frames ();
13504 if (windows_or_buffers_changed && !update_mode_lines)
13505 /* Code that sets windows_or_buffers_changed doesn't distinguish whether
13506 only the windows's contents needs to be refreshed, or whether the
13507 mode-lines also need a refresh. */
13508 update_mode_lines = (windows_or_buffers_changed == REDISPLAY_SOME
13509 ? REDISPLAY_SOME : 32);
13511 /* If specs for an arrow have changed, do thorough redisplay
13512 to ensure we remove any arrow that should no longer exist. */
13513 if (overlay_arrows_changed_p ())
13514 /* Apparently, this is the only case where we update other windows,
13515 without updating other mode-lines. */
13516 windows_or_buffers_changed = 49;
13518 consider_all_windows_p = (update_mode_lines
13519 || windows_or_buffers_changed);
13521 #define AINC(a,i) \
13522 if (VECTORP (a) && i >= 0 && i < ASIZE (a) && INTEGERP (AREF (a, i))) \
13523 ASET (a, i, make_number (1 + XINT (AREF (a, i))))
13525 AINC (Vredisplay__all_windows_cause, windows_or_buffers_changed);
13526 AINC (Vredisplay__mode_lines_cause, update_mode_lines);
13528 /* Optimize the case that only the line containing the cursor in the
13529 selected window has changed. Variables starting with this_ are
13530 set in display_line and record information about the line
13531 containing the cursor. */
13532 tlbufpos = this_line_start_pos;
13533 tlendpos = this_line_end_pos;
13534 if (!consider_all_windows_p
13535 && CHARPOS (tlbufpos) > 0
13536 && !w->update_mode_line
13537 && !current_buffer->clip_changed
13538 && !current_buffer->prevent_redisplay_optimizations_p
13539 && FRAME_VISIBLE_P (XFRAME (w->frame))
13540 && !FRAME_OBSCURED_P (XFRAME (w->frame))
13541 && !XFRAME (w->frame)->cursor_type_changed
13542 /* Make sure recorded data applies to current buffer, etc. */
13543 && this_line_buffer == current_buffer
13544 && match_p
13545 && !w->force_start
13546 && !w->optional_new_start
13547 /* Point must be on the line that we have info recorded about. */
13548 && PT >= CHARPOS (tlbufpos)
13549 && PT <= Z - CHARPOS (tlendpos)
13550 /* All text outside that line, including its final newline,
13551 must be unchanged. */
13552 && text_outside_line_unchanged_p (w, CHARPOS (tlbufpos),
13553 CHARPOS (tlendpos)))
13555 if (CHARPOS (tlbufpos) > BEGV
13556 && FETCH_BYTE (BYTEPOS (tlbufpos) - 1) != '\n'
13557 && (CHARPOS (tlbufpos) == ZV
13558 || FETCH_BYTE (BYTEPOS (tlbufpos)) == '\n'))
13559 /* Former continuation line has disappeared by becoming empty. */
13560 goto cancel;
13561 else if (window_outdated (w) || MINI_WINDOW_P (w))
13563 /* We have to handle the case of continuation around a
13564 wide-column character (see the comment in indent.c around
13565 line 1340).
13567 For instance, in the following case:
13569 -------- Insert --------
13570 K_A_N_\\ `a' K_A_N_a\ `X_' are wide-column chars.
13571 J_I_ ==> J_I_ `^^' are cursors.
13572 ^^ ^^
13573 -------- --------
13575 As we have to redraw the line above, we cannot use this
13576 optimization. */
13578 struct it it;
13579 int line_height_before = this_line_pixel_height;
13581 /* Note that start_display will handle the case that the
13582 line starting at tlbufpos is a continuation line. */
13583 start_display (&it, w, tlbufpos);
13585 /* Implementation note: It this still necessary? */
13586 if (it.current_x != this_line_start_x)
13587 goto cancel;
13589 TRACE ((stderr, "trying display optimization 1\n"));
13590 w->cursor.vpos = -1;
13591 overlay_arrow_seen = 0;
13592 it.vpos = this_line_vpos;
13593 it.current_y = this_line_y;
13594 it.glyph_row = MATRIX_ROW (w->desired_matrix, this_line_vpos);
13595 display_line (&it);
13597 /* If line contains point, is not continued,
13598 and ends at same distance from eob as before, we win. */
13599 if (w->cursor.vpos >= 0
13600 /* Line is not continued, otherwise this_line_start_pos
13601 would have been set to 0 in display_line. */
13602 && CHARPOS (this_line_start_pos)
13603 /* Line ends as before. */
13604 && CHARPOS (this_line_end_pos) == CHARPOS (tlendpos)
13605 /* Line has same height as before. Otherwise other lines
13606 would have to be shifted up or down. */
13607 && this_line_pixel_height == line_height_before)
13609 /* If this is not the window's last line, we must adjust
13610 the charstarts of the lines below. */
13611 if (it.current_y < it.last_visible_y)
13613 struct glyph_row *row
13614 = MATRIX_ROW (w->current_matrix, this_line_vpos + 1);
13615 ptrdiff_t delta, delta_bytes;
13617 /* We used to distinguish between two cases here,
13618 conditioned by Z - CHARPOS (tlendpos) == ZV, for
13619 when the line ends in a newline or the end of the
13620 buffer's accessible portion. But both cases did
13621 the same, so they were collapsed. */
13622 delta = (Z
13623 - CHARPOS (tlendpos)
13624 - MATRIX_ROW_START_CHARPOS (row));
13625 delta_bytes = (Z_BYTE
13626 - BYTEPOS (tlendpos)
13627 - MATRIX_ROW_START_BYTEPOS (row));
13629 increment_matrix_positions (w->current_matrix,
13630 this_line_vpos + 1,
13631 w->current_matrix->nrows,
13632 delta, delta_bytes);
13635 /* If this row displays text now but previously didn't,
13636 or vice versa, w->window_end_vpos may have to be
13637 adjusted. */
13638 if (MATRIX_ROW_DISPLAYS_TEXT_P (it.glyph_row - 1))
13640 if (w->window_end_vpos < this_line_vpos)
13641 w->window_end_vpos = this_line_vpos;
13643 else if (w->window_end_vpos == this_line_vpos
13644 && this_line_vpos > 0)
13645 w->window_end_vpos = this_line_vpos - 1;
13646 w->window_end_valid = 0;
13648 /* Update hint: No need to try to scroll in update_window. */
13649 w->desired_matrix->no_scrolling_p = 1;
13651 #ifdef GLYPH_DEBUG
13652 *w->desired_matrix->method = 0;
13653 debug_method_add (w, "optimization 1");
13654 #endif
13655 #ifdef HAVE_WINDOW_SYSTEM
13656 update_window_fringes (w, 0);
13657 #endif
13658 goto update;
13660 else
13661 goto cancel;
13663 else if (/* Cursor position hasn't changed. */
13664 PT == w->last_point
13665 /* Make sure the cursor was last displayed
13666 in this window. Otherwise we have to reposition it. */
13668 /* PXW: Must be converted to pixels, probably. */
13669 && 0 <= w->cursor.vpos
13670 && w->cursor.vpos < WINDOW_TOTAL_LINES (w))
13672 if (!must_finish)
13674 do_pending_window_change (1);
13675 /* If selected_window changed, redisplay again. */
13676 if (WINDOWP (selected_window)
13677 && (w = XWINDOW (selected_window)) != sw)
13678 goto retry;
13680 /* We used to always goto end_of_redisplay here, but this
13681 isn't enough if we have a blinking cursor. */
13682 if (w->cursor_off_p == w->last_cursor_off_p)
13683 goto end_of_redisplay;
13685 goto update;
13687 /* If highlighting the region, or if the cursor is in the echo area,
13688 then we can't just move the cursor. */
13689 else if (NILP (Vshow_trailing_whitespace)
13690 && !cursor_in_echo_area)
13692 struct it it;
13693 struct glyph_row *row;
13695 /* Skip from tlbufpos to PT and see where it is. Note that
13696 PT may be in invisible text. If so, we will end at the
13697 next visible position. */
13698 init_iterator (&it, w, CHARPOS (tlbufpos), BYTEPOS (tlbufpos),
13699 NULL, DEFAULT_FACE_ID);
13700 it.current_x = this_line_start_x;
13701 it.current_y = this_line_y;
13702 it.vpos = this_line_vpos;
13704 /* The call to move_it_to stops in front of PT, but
13705 moves over before-strings. */
13706 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
13708 if (it.vpos == this_line_vpos
13709 && (row = MATRIX_ROW (w->current_matrix, this_line_vpos),
13710 row->enabled_p))
13712 eassert (this_line_vpos == it.vpos);
13713 eassert (this_line_y == it.current_y);
13714 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
13715 #ifdef GLYPH_DEBUG
13716 *w->desired_matrix->method = 0;
13717 debug_method_add (w, "optimization 3");
13718 #endif
13719 goto update;
13721 else
13722 goto cancel;
13725 cancel:
13726 /* Text changed drastically or point moved off of line. */
13727 SET_MATRIX_ROW_ENABLED_P (w->desired_matrix, this_line_vpos, false);
13730 CHARPOS (this_line_start_pos) = 0;
13731 ++clear_face_cache_count;
13732 #ifdef HAVE_WINDOW_SYSTEM
13733 ++clear_image_cache_count;
13734 #endif
13736 /* Build desired matrices, and update the display. If
13737 consider_all_windows_p is non-zero, do it for all windows on all
13738 frames. Otherwise do it for selected_window, only. */
13740 if (consider_all_windows_p)
13742 FOR_EACH_FRAME (tail, frame)
13743 XFRAME (frame)->updated_p = 0;
13745 propagate_buffer_redisplay ();
13747 FOR_EACH_FRAME (tail, frame)
13749 struct frame *f = XFRAME (frame);
13751 /* We don't have to do anything for unselected terminal
13752 frames. */
13753 if ((FRAME_TERMCAP_P (f) || FRAME_MSDOS_P (f))
13754 && !EQ (FRAME_TTY (f)->top_frame, frame))
13755 continue;
13757 retry_frame:
13759 if (FRAME_WINDOW_P (f) || FRAME_TERMCAP_P (f) || f == sf)
13761 bool gcscrollbars
13762 /* Only GC scrollbars when we redisplay the whole frame. */
13763 = f->redisplay || !REDISPLAY_SOME_P ();
13764 /* Mark all the scroll bars to be removed; we'll redeem
13765 the ones we want when we redisplay their windows. */
13766 if (gcscrollbars && FRAME_TERMINAL (f)->condemn_scroll_bars_hook)
13767 FRAME_TERMINAL (f)->condemn_scroll_bars_hook (f);
13769 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
13770 redisplay_windows (FRAME_ROOT_WINDOW (f));
13771 /* Remember that the invisible frames need to be redisplayed next
13772 time they're visible. */
13773 else if (!REDISPLAY_SOME_P ())
13774 f->redisplay = true;
13776 /* The X error handler may have deleted that frame. */
13777 if (!FRAME_LIVE_P (f))
13778 continue;
13780 /* Any scroll bars which redisplay_windows should have
13781 nuked should now go away. */
13782 if (gcscrollbars && FRAME_TERMINAL (f)->judge_scroll_bars_hook)
13783 FRAME_TERMINAL (f)->judge_scroll_bars_hook (f);
13785 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
13787 /* If fonts changed on visible frame, display again. */
13788 if (f->fonts_changed)
13790 adjust_frame_glyphs (f);
13791 f->fonts_changed = 0;
13792 goto retry_frame;
13795 /* See if we have to hscroll. */
13796 if (!f->already_hscrolled_p)
13798 f->already_hscrolled_p = 1;
13799 if (hscroll_windows (f->root_window))
13800 goto retry_frame;
13803 /* Prevent various kinds of signals during display
13804 update. stdio is not robust about handling
13805 signals, which can cause an apparent I/O error. */
13806 if (interrupt_input)
13807 unrequest_sigio ();
13808 STOP_POLLING;
13810 pending |= update_frame (f, 0, 0);
13811 f->cursor_type_changed = 0;
13812 f->updated_p = 1;
13817 eassert (EQ (XFRAME (selected_frame)->selected_window, selected_window));
13819 if (!pending)
13821 /* Do the mark_window_display_accurate after all windows have
13822 been redisplayed because this call resets flags in buffers
13823 which are needed for proper redisplay. */
13824 FOR_EACH_FRAME (tail, frame)
13826 struct frame *f = XFRAME (frame);
13827 if (f->updated_p)
13829 f->redisplay = false;
13830 mark_window_display_accurate (f->root_window, 1);
13831 if (FRAME_TERMINAL (f)->frame_up_to_date_hook)
13832 FRAME_TERMINAL (f)->frame_up_to_date_hook (f);
13837 else if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
13839 Lisp_Object mini_window = FRAME_MINIBUF_WINDOW (sf);
13840 struct frame *mini_frame;
13842 displayed_buffer = XBUFFER (XWINDOW (selected_window)->contents);
13843 /* Use list_of_error, not Qerror, so that
13844 we catch only errors and don't run the debugger. */
13845 internal_condition_case_1 (redisplay_window_1, selected_window,
13846 list_of_error,
13847 redisplay_window_error);
13848 if (update_miniwindow_p)
13849 internal_condition_case_1 (redisplay_window_1, mini_window,
13850 list_of_error,
13851 redisplay_window_error);
13853 /* Compare desired and current matrices, perform output. */
13855 update:
13856 /* If fonts changed, display again. */
13857 if (sf->fonts_changed)
13858 goto retry;
13860 /* Prevent various kinds of signals during display update.
13861 stdio is not robust about handling signals,
13862 which can cause an apparent I/O error. */
13863 if (interrupt_input)
13864 unrequest_sigio ();
13865 STOP_POLLING;
13867 if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
13869 if (hscroll_windows (selected_window))
13870 goto retry;
13872 XWINDOW (selected_window)->must_be_updated_p = true;
13873 pending = update_frame (sf, 0, 0);
13874 sf->cursor_type_changed = 0;
13877 /* We may have called echo_area_display at the top of this
13878 function. If the echo area is on another frame, that may
13879 have put text on a frame other than the selected one, so the
13880 above call to update_frame would not have caught it. Catch
13881 it here. */
13882 mini_window = FRAME_MINIBUF_WINDOW (sf);
13883 mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
13885 if (mini_frame != sf && FRAME_WINDOW_P (mini_frame))
13887 XWINDOW (mini_window)->must_be_updated_p = true;
13888 pending |= update_frame (mini_frame, 0, 0);
13889 mini_frame->cursor_type_changed = 0;
13890 if (!pending && hscroll_windows (mini_window))
13891 goto retry;
13895 /* If display was paused because of pending input, make sure we do a
13896 thorough update the next time. */
13897 if (pending)
13899 /* Prevent the optimization at the beginning of
13900 redisplay_internal that tries a single-line update of the
13901 line containing the cursor in the selected window. */
13902 CHARPOS (this_line_start_pos) = 0;
13904 /* Let the overlay arrow be updated the next time. */
13905 update_overlay_arrows (0);
13907 /* If we pause after scrolling, some rows in the current
13908 matrices of some windows are not valid. */
13909 if (!WINDOW_FULL_WIDTH_P (w)
13910 && !FRAME_WINDOW_P (XFRAME (w->frame)))
13911 update_mode_lines = 36;
13913 else
13915 if (!consider_all_windows_p)
13917 /* This has already been done above if
13918 consider_all_windows_p is set. */
13919 if (XBUFFER (w->contents)->text->redisplay
13920 && buffer_window_count (XBUFFER (w->contents)) > 1)
13921 /* This can happen if b->text->redisplay was set during
13922 jit-lock. */
13923 propagate_buffer_redisplay ();
13924 mark_window_display_accurate_1 (w, 1);
13926 /* Say overlay arrows are up to date. */
13927 update_overlay_arrows (1);
13929 if (FRAME_TERMINAL (sf)->frame_up_to_date_hook != 0)
13930 FRAME_TERMINAL (sf)->frame_up_to_date_hook (sf);
13933 update_mode_lines = 0;
13934 windows_or_buffers_changed = 0;
13937 /* Start SIGIO interrupts coming again. Having them off during the
13938 code above makes it less likely one will discard output, but not
13939 impossible, since there might be stuff in the system buffer here.
13940 But it is much hairier to try to do anything about that. */
13941 if (interrupt_input)
13942 request_sigio ();
13943 RESUME_POLLING;
13945 /* If a frame has become visible which was not before, redisplay
13946 again, so that we display it. Expose events for such a frame
13947 (which it gets when becoming visible) don't call the parts of
13948 redisplay constructing glyphs, so simply exposing a frame won't
13949 display anything in this case. So, we have to display these
13950 frames here explicitly. */
13951 if (!pending)
13953 int new_count = 0;
13955 FOR_EACH_FRAME (tail, frame)
13957 if (XFRAME (frame)->visible)
13958 new_count++;
13961 if (new_count != number_of_visible_frames)
13962 windows_or_buffers_changed = 52;
13965 /* Change frame size now if a change is pending. */
13966 do_pending_window_change (1);
13968 /* If we just did a pending size change, or have additional
13969 visible frames, or selected_window changed, redisplay again. */
13970 if ((windows_or_buffers_changed && !pending)
13971 || (WINDOWP (selected_window) && (w = XWINDOW (selected_window)) != sw))
13972 goto retry;
13974 /* Clear the face and image caches.
13976 We used to do this only if consider_all_windows_p. But the cache
13977 needs to be cleared if a timer creates images in the current
13978 buffer (e.g. the test case in Bug#6230). */
13980 if (clear_face_cache_count > CLEAR_FACE_CACHE_COUNT)
13982 clear_face_cache (0);
13983 clear_face_cache_count = 0;
13986 #ifdef HAVE_WINDOW_SYSTEM
13987 if (clear_image_cache_count > CLEAR_IMAGE_CACHE_COUNT)
13989 clear_image_caches (Qnil);
13990 clear_image_cache_count = 0;
13992 #endif /* HAVE_WINDOW_SYSTEM */
13994 end_of_redisplay:
13995 if (interrupt_input && interrupts_deferred)
13996 request_sigio ();
13998 unbind_to (count, Qnil);
13999 RESUME_POLLING;
14003 /* Redisplay, but leave alone any recent echo area message unless
14004 another message has been requested in its place.
14006 This is useful in situations where you need to redisplay but no
14007 user action has occurred, making it inappropriate for the message
14008 area to be cleared. See tracking_off and
14009 wait_reading_process_output for examples of these situations.
14011 FROM_WHERE is an integer saying from where this function was
14012 called. This is useful for debugging. */
14014 void
14015 redisplay_preserve_echo_area (int from_where)
14017 TRACE ((stderr, "redisplay_preserve_echo_area (%d)\n", from_where));
14019 if (!NILP (echo_area_buffer[1]))
14021 /* We have a previously displayed message, but no current
14022 message. Redisplay the previous message. */
14023 display_last_displayed_message_p = 1;
14024 redisplay_internal ();
14025 display_last_displayed_message_p = 0;
14027 else
14028 redisplay_internal ();
14030 flush_frame (SELECTED_FRAME ());
14034 /* Function registered with record_unwind_protect in redisplay_internal. */
14036 static void
14037 unwind_redisplay (void)
14039 redisplaying_p = 0;
14043 /* Mark the display of leaf window W as accurate or inaccurate.
14044 If ACCURATE_P is non-zero mark display of W as accurate. If
14045 ACCURATE_P is zero, arrange for W to be redisplayed the next
14046 time redisplay_internal is called. */
14048 static void
14049 mark_window_display_accurate_1 (struct window *w, int accurate_p)
14051 struct buffer *b = XBUFFER (w->contents);
14053 w->last_modified = accurate_p ? BUF_MODIFF (b) : 0;
14054 w->last_overlay_modified = accurate_p ? BUF_OVERLAY_MODIFF (b) : 0;
14055 w->last_had_star = BUF_MODIFF (b) > BUF_SAVE_MODIFF (b);
14057 if (accurate_p)
14059 b->clip_changed = false;
14060 b->prevent_redisplay_optimizations_p = false;
14061 eassert (buffer_window_count (b) > 0);
14062 /* Resetting b->text->redisplay is problematic!
14063 In order to make it safer to do it here, redisplay_internal must
14064 have copied all b->text->redisplay to their respective windows. */
14065 b->text->redisplay = false;
14067 BUF_UNCHANGED_MODIFIED (b) = BUF_MODIFF (b);
14068 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = BUF_OVERLAY_MODIFF (b);
14069 BUF_BEG_UNCHANGED (b) = BUF_GPT (b) - BUF_BEG (b);
14070 BUF_END_UNCHANGED (b) = BUF_Z (b) - BUF_GPT (b);
14072 w->current_matrix->buffer = b;
14073 w->current_matrix->begv = BUF_BEGV (b);
14074 w->current_matrix->zv = BUF_ZV (b);
14076 w->last_cursor_vpos = w->cursor.vpos;
14077 w->last_cursor_off_p = w->cursor_off_p;
14079 if (w == XWINDOW (selected_window))
14080 w->last_point = BUF_PT (b);
14081 else
14082 w->last_point = marker_position (w->pointm);
14084 w->window_end_valid = true;
14085 w->update_mode_line = false;
14088 w->redisplay = !accurate_p;
14092 /* Mark the display of windows in the window tree rooted at WINDOW as
14093 accurate or inaccurate. If ACCURATE_P is non-zero mark display of
14094 windows as accurate. If ACCURATE_P is zero, arrange for windows to
14095 be redisplayed the next time redisplay_internal is called. */
14097 void
14098 mark_window_display_accurate (Lisp_Object window, int accurate_p)
14100 struct window *w;
14102 for (; !NILP (window); window = w->next)
14104 w = XWINDOW (window);
14105 if (WINDOWP (w->contents))
14106 mark_window_display_accurate (w->contents, accurate_p);
14107 else
14108 mark_window_display_accurate_1 (w, accurate_p);
14111 if (accurate_p)
14112 update_overlay_arrows (1);
14113 else
14114 /* Force a thorough redisplay the next time by setting
14115 last_arrow_position and last_arrow_string to t, which is
14116 unequal to any useful value of Voverlay_arrow_... */
14117 update_overlay_arrows (-1);
14121 /* Return value in display table DP (Lisp_Char_Table *) for character
14122 C. Since a display table doesn't have any parent, we don't have to
14123 follow parent. Do not call this function directly but use the
14124 macro DISP_CHAR_VECTOR. */
14126 Lisp_Object
14127 disp_char_vector (struct Lisp_Char_Table *dp, int c)
14129 Lisp_Object val;
14131 if (ASCII_CHAR_P (c))
14133 val = dp->ascii;
14134 if (SUB_CHAR_TABLE_P (val))
14135 val = XSUB_CHAR_TABLE (val)->contents[c];
14137 else
14139 Lisp_Object table;
14141 XSETCHAR_TABLE (table, dp);
14142 val = char_table_ref (table, c);
14144 if (NILP (val))
14145 val = dp->defalt;
14146 return val;
14151 /***********************************************************************
14152 Window Redisplay
14153 ***********************************************************************/
14155 /* Redisplay all leaf windows in the window tree rooted at WINDOW. */
14157 static void
14158 redisplay_windows (Lisp_Object window)
14160 while (!NILP (window))
14162 struct window *w = XWINDOW (window);
14164 if (WINDOWP (w->contents))
14165 redisplay_windows (w->contents);
14166 else if (BUFFERP (w->contents))
14168 displayed_buffer = XBUFFER (w->contents);
14169 /* Use list_of_error, not Qerror, so that
14170 we catch only errors and don't run the debugger. */
14171 internal_condition_case_1 (redisplay_window_0, window,
14172 list_of_error,
14173 redisplay_window_error);
14176 window = w->next;
14180 static Lisp_Object
14181 redisplay_window_error (Lisp_Object ignore)
14183 displayed_buffer->display_error_modiff = BUF_MODIFF (displayed_buffer);
14184 return Qnil;
14187 static Lisp_Object
14188 redisplay_window_0 (Lisp_Object window)
14190 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
14191 redisplay_window (window, false);
14192 return Qnil;
14195 static Lisp_Object
14196 redisplay_window_1 (Lisp_Object window)
14198 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
14199 redisplay_window (window, true);
14200 return Qnil;
14204 /* Set cursor position of W. PT is assumed to be displayed in ROW.
14205 DELTA and DELTA_BYTES are the numbers of characters and bytes by
14206 which positions recorded in ROW differ from current buffer
14207 positions.
14209 Return 0 if cursor is not on this row, 1 otherwise. */
14211 static int
14212 set_cursor_from_row (struct window *w, struct glyph_row *row,
14213 struct glyph_matrix *matrix,
14214 ptrdiff_t delta, ptrdiff_t delta_bytes,
14215 int dy, int dvpos)
14217 struct glyph *glyph = row->glyphs[TEXT_AREA];
14218 struct glyph *end = glyph + row->used[TEXT_AREA];
14219 struct glyph *cursor = NULL;
14220 /* The last known character position in row. */
14221 ptrdiff_t last_pos = MATRIX_ROW_START_CHARPOS (row) + delta;
14222 int x = row->x;
14223 ptrdiff_t pt_old = PT - delta;
14224 ptrdiff_t pos_before = MATRIX_ROW_START_CHARPOS (row) + delta;
14225 ptrdiff_t pos_after = MATRIX_ROW_END_CHARPOS (row) + delta;
14226 struct glyph *glyph_before = glyph - 1, *glyph_after = end;
14227 /* A glyph beyond the edge of TEXT_AREA which we should never
14228 touch. */
14229 struct glyph *glyphs_end = end;
14230 /* Non-zero means we've found a match for cursor position, but that
14231 glyph has the avoid_cursor_p flag set. */
14232 int match_with_avoid_cursor = 0;
14233 /* Non-zero means we've seen at least one glyph that came from a
14234 display string. */
14235 int string_seen = 0;
14236 /* Largest and smallest buffer positions seen so far during scan of
14237 glyph row. */
14238 ptrdiff_t bpos_max = pos_before;
14239 ptrdiff_t bpos_min = pos_after;
14240 /* Last buffer position covered by an overlay string with an integer
14241 `cursor' property. */
14242 ptrdiff_t bpos_covered = 0;
14243 /* Non-zero means the display string on which to display the cursor
14244 comes from a text property, not from an overlay. */
14245 int string_from_text_prop = 0;
14247 /* Don't even try doing anything if called for a mode-line or
14248 header-line row, since the rest of the code isn't prepared to
14249 deal with such calamities. */
14250 eassert (!row->mode_line_p);
14251 if (row->mode_line_p)
14252 return 0;
14254 /* Skip over glyphs not having an object at the start and the end of
14255 the row. These are special glyphs like truncation marks on
14256 terminal frames. */
14257 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
14259 if (!row->reversed_p)
14261 while (glyph < end
14262 && INTEGERP (glyph->object)
14263 && glyph->charpos < 0)
14265 x += glyph->pixel_width;
14266 ++glyph;
14268 while (end > glyph
14269 && INTEGERP ((end - 1)->object)
14270 /* CHARPOS is zero for blanks and stretch glyphs
14271 inserted by extend_face_to_end_of_line. */
14272 && (end - 1)->charpos <= 0)
14273 --end;
14274 glyph_before = glyph - 1;
14275 glyph_after = end;
14277 else
14279 struct glyph *g;
14281 /* If the glyph row is reversed, we need to process it from back
14282 to front, so swap the edge pointers. */
14283 glyphs_end = end = glyph - 1;
14284 glyph += row->used[TEXT_AREA] - 1;
14286 while (glyph > end + 1
14287 && INTEGERP (glyph->object)
14288 && glyph->charpos < 0)
14290 --glyph;
14291 x -= glyph->pixel_width;
14293 if (INTEGERP (glyph->object) && glyph->charpos < 0)
14294 --glyph;
14295 /* By default, in reversed rows we put the cursor on the
14296 rightmost (first in the reading order) glyph. */
14297 for (g = end + 1; g < glyph; g++)
14298 x += g->pixel_width;
14299 while (end < glyph
14300 && INTEGERP ((end + 1)->object)
14301 && (end + 1)->charpos <= 0)
14302 ++end;
14303 glyph_before = glyph + 1;
14304 glyph_after = end;
14307 else if (row->reversed_p)
14309 /* In R2L rows that don't display text, put the cursor on the
14310 rightmost glyph. Case in point: an empty last line that is
14311 part of an R2L paragraph. */
14312 cursor = end - 1;
14313 /* Avoid placing the cursor on the last glyph of the row, where
14314 on terminal frames we hold the vertical border between
14315 adjacent windows. */
14316 if (!FRAME_WINDOW_P (WINDOW_XFRAME (w))
14317 && !WINDOW_RIGHTMOST_P (w)
14318 && cursor == row->glyphs[LAST_AREA] - 1)
14319 cursor--;
14320 x = -1; /* will be computed below, at label compute_x */
14323 /* Step 1: Try to find the glyph whose character position
14324 corresponds to point. If that's not possible, find 2 glyphs
14325 whose character positions are the closest to point, one before
14326 point, the other after it. */
14327 if (!row->reversed_p)
14328 while (/* not marched to end of glyph row */
14329 glyph < end
14330 /* glyph was not inserted by redisplay for internal purposes */
14331 && !INTEGERP (glyph->object))
14333 if (BUFFERP (glyph->object))
14335 ptrdiff_t dpos = glyph->charpos - pt_old;
14337 if (glyph->charpos > bpos_max)
14338 bpos_max = glyph->charpos;
14339 if (glyph->charpos < bpos_min)
14340 bpos_min = glyph->charpos;
14341 if (!glyph->avoid_cursor_p)
14343 /* If we hit point, we've found the glyph on which to
14344 display the cursor. */
14345 if (dpos == 0)
14347 match_with_avoid_cursor = 0;
14348 break;
14350 /* See if we've found a better approximation to
14351 POS_BEFORE or to POS_AFTER. */
14352 if (0 > dpos && dpos > pos_before - pt_old)
14354 pos_before = glyph->charpos;
14355 glyph_before = glyph;
14357 else if (0 < dpos && dpos < pos_after - pt_old)
14359 pos_after = glyph->charpos;
14360 glyph_after = glyph;
14363 else if (dpos == 0)
14364 match_with_avoid_cursor = 1;
14366 else if (STRINGP (glyph->object))
14368 Lisp_Object chprop;
14369 ptrdiff_t glyph_pos = glyph->charpos;
14371 chprop = Fget_char_property (make_number (glyph_pos), Qcursor,
14372 glyph->object);
14373 if (!NILP (chprop))
14375 /* If the string came from a `display' text property,
14376 look up the buffer position of that property and
14377 use that position to update bpos_max, as if we
14378 actually saw such a position in one of the row's
14379 glyphs. This helps with supporting integer values
14380 of `cursor' property on the display string in
14381 situations where most or all of the row's buffer
14382 text is completely covered by display properties,
14383 so that no glyph with valid buffer positions is
14384 ever seen in the row. */
14385 ptrdiff_t prop_pos =
14386 string_buffer_position_lim (glyph->object, pos_before,
14387 pos_after, 0);
14389 if (prop_pos >= pos_before)
14390 bpos_max = prop_pos - 1;
14392 if (INTEGERP (chprop))
14394 bpos_covered = bpos_max + XINT (chprop);
14395 /* If the `cursor' property covers buffer positions up
14396 to and including point, we should display cursor on
14397 this glyph. Note that, if a `cursor' property on one
14398 of the string's characters has an integer value, we
14399 will break out of the loop below _before_ we get to
14400 the position match above. IOW, integer values of
14401 the `cursor' property override the "exact match for
14402 point" strategy of positioning the cursor. */
14403 /* Implementation note: bpos_max == pt_old when, e.g.,
14404 we are in an empty line, where bpos_max is set to
14405 MATRIX_ROW_START_CHARPOS, see above. */
14406 if (bpos_max <= pt_old && bpos_covered >= pt_old)
14408 cursor = glyph;
14409 break;
14413 string_seen = 1;
14415 x += glyph->pixel_width;
14416 ++glyph;
14418 else if (glyph > end) /* row is reversed */
14419 while (!INTEGERP (glyph->object))
14421 if (BUFFERP (glyph->object))
14423 ptrdiff_t dpos = glyph->charpos - pt_old;
14425 if (glyph->charpos > bpos_max)
14426 bpos_max = glyph->charpos;
14427 if (glyph->charpos < bpos_min)
14428 bpos_min = glyph->charpos;
14429 if (!glyph->avoid_cursor_p)
14431 if (dpos == 0)
14433 match_with_avoid_cursor = 0;
14434 break;
14436 if (0 > dpos && dpos > pos_before - pt_old)
14438 pos_before = glyph->charpos;
14439 glyph_before = glyph;
14441 else if (0 < dpos && dpos < pos_after - pt_old)
14443 pos_after = glyph->charpos;
14444 glyph_after = glyph;
14447 else if (dpos == 0)
14448 match_with_avoid_cursor = 1;
14450 else if (STRINGP (glyph->object))
14452 Lisp_Object chprop;
14453 ptrdiff_t glyph_pos = glyph->charpos;
14455 chprop = Fget_char_property (make_number (glyph_pos), Qcursor,
14456 glyph->object);
14457 if (!NILP (chprop))
14459 ptrdiff_t prop_pos =
14460 string_buffer_position_lim (glyph->object, pos_before,
14461 pos_after, 0);
14463 if (prop_pos >= pos_before)
14464 bpos_max = prop_pos - 1;
14466 if (INTEGERP (chprop))
14468 bpos_covered = bpos_max + XINT (chprop);
14469 /* If the `cursor' property covers buffer positions up
14470 to and including point, we should display cursor on
14471 this glyph. */
14472 if (bpos_max <= pt_old && bpos_covered >= pt_old)
14474 cursor = glyph;
14475 break;
14478 string_seen = 1;
14480 --glyph;
14481 if (glyph == glyphs_end) /* don't dereference outside TEXT_AREA */
14483 x--; /* can't use any pixel_width */
14484 break;
14486 x -= glyph->pixel_width;
14489 /* Step 2: If we didn't find an exact match for point, we need to
14490 look for a proper place to put the cursor among glyphs between
14491 GLYPH_BEFORE and GLYPH_AFTER. */
14492 if (!((row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end)
14493 && BUFFERP (glyph->object) && glyph->charpos == pt_old)
14494 && !(bpos_max < pt_old && pt_old <= bpos_covered))
14496 /* An empty line has a single glyph whose OBJECT is zero and
14497 whose CHARPOS is the position of a newline on that line.
14498 Note that on a TTY, there are more glyphs after that, which
14499 were produced by extend_face_to_end_of_line, but their
14500 CHARPOS is zero or negative. */
14501 int empty_line_p =
14502 (row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end)
14503 && INTEGERP (glyph->object) && glyph->charpos > 0
14504 /* On a TTY, continued and truncated rows also have a glyph at
14505 their end whose OBJECT is zero and whose CHARPOS is
14506 positive (the continuation and truncation glyphs), but such
14507 rows are obviously not "empty". */
14508 && !(row->continued_p || row->truncated_on_right_p);
14510 if (row->ends_in_ellipsis_p && pos_after == last_pos)
14512 ptrdiff_t ellipsis_pos;
14514 /* Scan back over the ellipsis glyphs. */
14515 if (!row->reversed_p)
14517 ellipsis_pos = (glyph - 1)->charpos;
14518 while (glyph > row->glyphs[TEXT_AREA]
14519 && (glyph - 1)->charpos == ellipsis_pos)
14520 glyph--, x -= glyph->pixel_width;
14521 /* That loop always goes one position too far, including
14522 the glyph before the ellipsis. So scan forward over
14523 that one. */
14524 x += glyph->pixel_width;
14525 glyph++;
14527 else /* row is reversed */
14529 ellipsis_pos = (glyph + 1)->charpos;
14530 while (glyph < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
14531 && (glyph + 1)->charpos == ellipsis_pos)
14532 glyph++, x += glyph->pixel_width;
14533 x -= glyph->pixel_width;
14534 glyph--;
14537 else if (match_with_avoid_cursor)
14539 cursor = glyph_after;
14540 x = -1;
14542 else if (string_seen)
14544 int incr = row->reversed_p ? -1 : +1;
14546 /* Need to find the glyph that came out of a string which is
14547 present at point. That glyph is somewhere between
14548 GLYPH_BEFORE and GLYPH_AFTER, and it came from a string
14549 positioned between POS_BEFORE and POS_AFTER in the
14550 buffer. */
14551 struct glyph *start, *stop;
14552 ptrdiff_t pos = pos_before;
14554 x = -1;
14556 /* If the row ends in a newline from a display string,
14557 reordering could have moved the glyphs belonging to the
14558 string out of the [GLYPH_BEFORE..GLYPH_AFTER] range. So
14559 in this case we extend the search to the last glyph in
14560 the row that was not inserted by redisplay. */
14561 if (row->ends_in_newline_from_string_p)
14563 glyph_after = end;
14564 pos_after = MATRIX_ROW_END_CHARPOS (row) + delta;
14567 /* GLYPH_BEFORE and GLYPH_AFTER are the glyphs that
14568 correspond to POS_BEFORE and POS_AFTER, respectively. We
14569 need START and STOP in the order that corresponds to the
14570 row's direction as given by its reversed_p flag. If the
14571 directionality of characters between POS_BEFORE and
14572 POS_AFTER is the opposite of the row's base direction,
14573 these characters will have been reordered for display,
14574 and we need to reverse START and STOP. */
14575 if (!row->reversed_p)
14577 start = min (glyph_before, glyph_after);
14578 stop = max (glyph_before, glyph_after);
14580 else
14582 start = max (glyph_before, glyph_after);
14583 stop = min (glyph_before, glyph_after);
14585 for (glyph = start + incr;
14586 row->reversed_p ? glyph > stop : glyph < stop; )
14589 /* Any glyphs that come from the buffer are here because
14590 of bidi reordering. Skip them, and only pay
14591 attention to glyphs that came from some string. */
14592 if (STRINGP (glyph->object))
14594 Lisp_Object str;
14595 ptrdiff_t tem;
14596 /* If the display property covers the newline, we
14597 need to search for it one position farther. */
14598 ptrdiff_t lim = pos_after
14599 + (pos_after == MATRIX_ROW_END_CHARPOS (row) + delta);
14601 string_from_text_prop = 0;
14602 str = glyph->object;
14603 tem = string_buffer_position_lim (str, pos, lim, 0);
14604 if (tem == 0 /* from overlay */
14605 || pos <= tem)
14607 /* If the string from which this glyph came is
14608 found in the buffer at point, or at position
14609 that is closer to point than pos_after, then
14610 we've found the glyph we've been looking for.
14611 If it comes from an overlay (tem == 0), and
14612 it has the `cursor' property on one of its
14613 glyphs, record that glyph as a candidate for
14614 displaying the cursor. (As in the
14615 unidirectional version, we will display the
14616 cursor on the last candidate we find.) */
14617 if (tem == 0
14618 || tem == pt_old
14619 || (tem - pt_old > 0 && tem < pos_after))
14621 /* The glyphs from this string could have
14622 been reordered. Find the one with the
14623 smallest string position. Or there could
14624 be a character in the string with the
14625 `cursor' property, which means display
14626 cursor on that character's glyph. */
14627 ptrdiff_t strpos = glyph->charpos;
14629 if (tem)
14631 cursor = glyph;
14632 string_from_text_prop = 1;
14634 for ( ;
14635 (row->reversed_p ? glyph > stop : glyph < stop)
14636 && EQ (glyph->object, str);
14637 glyph += incr)
14639 Lisp_Object cprop;
14640 ptrdiff_t gpos = glyph->charpos;
14642 cprop = Fget_char_property (make_number (gpos),
14643 Qcursor,
14644 glyph->object);
14645 if (!NILP (cprop))
14647 cursor = glyph;
14648 break;
14650 if (tem && glyph->charpos < strpos)
14652 strpos = glyph->charpos;
14653 cursor = glyph;
14657 if (tem == pt_old
14658 || (tem - pt_old > 0 && tem < pos_after))
14659 goto compute_x;
14661 if (tem)
14662 pos = tem + 1; /* don't find previous instances */
14664 /* This string is not what we want; skip all of the
14665 glyphs that came from it. */
14666 while ((row->reversed_p ? glyph > stop : glyph < stop)
14667 && EQ (glyph->object, str))
14668 glyph += incr;
14670 else
14671 glyph += incr;
14674 /* If we reached the end of the line, and END was from a string,
14675 the cursor is not on this line. */
14676 if (cursor == NULL
14677 && (row->reversed_p ? glyph <= end : glyph >= end)
14678 && (row->reversed_p ? end > glyphs_end : end < glyphs_end)
14679 && STRINGP (end->object)
14680 && row->continued_p)
14681 return 0;
14683 /* A truncated row may not include PT among its character positions.
14684 Setting the cursor inside the scroll margin will trigger
14685 recalculation of hscroll in hscroll_window_tree. But if a
14686 display string covers point, defer to the string-handling
14687 code below to figure this out. */
14688 else if (row->truncated_on_left_p && pt_old < bpos_min)
14690 cursor = glyph_before;
14691 x = -1;
14693 else if ((row->truncated_on_right_p && pt_old > bpos_max)
14694 /* Zero-width characters produce no glyphs. */
14695 || (!empty_line_p
14696 && (row->reversed_p
14697 ? glyph_after > glyphs_end
14698 : glyph_after < glyphs_end)))
14700 cursor = glyph_after;
14701 x = -1;
14705 compute_x:
14706 if (cursor != NULL)
14707 glyph = cursor;
14708 else if (glyph == glyphs_end
14709 && pos_before == pos_after
14710 && STRINGP ((row->reversed_p
14711 ? row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
14712 : row->glyphs[TEXT_AREA])->object))
14714 /* If all the glyphs of this row came from strings, put the
14715 cursor on the first glyph of the row. This avoids having the
14716 cursor outside of the text area in this very rare and hard
14717 use case. */
14718 glyph =
14719 row->reversed_p
14720 ? row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
14721 : row->glyphs[TEXT_AREA];
14723 if (x < 0)
14725 struct glyph *g;
14727 /* Need to compute x that corresponds to GLYPH. */
14728 for (g = row->glyphs[TEXT_AREA], x = row->x; g < glyph; g++)
14730 if (g >= row->glyphs[TEXT_AREA] + row->used[TEXT_AREA])
14731 emacs_abort ();
14732 x += g->pixel_width;
14736 /* ROW could be part of a continued line, which, under bidi
14737 reordering, might have other rows whose start and end charpos
14738 occlude point. Only set w->cursor if we found a better
14739 approximation to the cursor position than we have from previously
14740 examined candidate rows belonging to the same continued line. */
14741 if (/* We already have a candidate row. */
14742 w->cursor.vpos >= 0
14743 /* That candidate is not the row we are processing. */
14744 && MATRIX_ROW (matrix, w->cursor.vpos) != row
14745 /* Make sure cursor.vpos specifies a row whose start and end
14746 charpos occlude point, and it is valid candidate for being a
14747 cursor-row. This is because some callers of this function
14748 leave cursor.vpos at the row where the cursor was displayed
14749 during the last redisplay cycle. */
14750 && MATRIX_ROW_START_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos)) <= pt_old
14751 && pt_old <= MATRIX_ROW_END_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
14752 && cursor_row_p (MATRIX_ROW (matrix, w->cursor.vpos)))
14754 struct glyph *g1
14755 = MATRIX_ROW_GLYPH_START (matrix, w->cursor.vpos) + w->cursor.hpos;
14757 /* Don't consider glyphs that are outside TEXT_AREA. */
14758 if (!(row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end))
14759 return 0;
14760 /* Keep the candidate whose buffer position is the closest to
14761 point or has the `cursor' property. */
14762 if (/* Previous candidate is a glyph in TEXT_AREA of that row. */
14763 w->cursor.hpos >= 0
14764 && w->cursor.hpos < MATRIX_ROW_USED (matrix, w->cursor.vpos)
14765 && ((BUFFERP (g1->object)
14766 && (g1->charpos == pt_old /* An exact match always wins. */
14767 || (BUFFERP (glyph->object)
14768 && eabs (g1->charpos - pt_old)
14769 < eabs (glyph->charpos - pt_old))))
14770 /* Previous candidate is a glyph from a string that has
14771 a non-nil `cursor' property. */
14772 || (STRINGP (g1->object)
14773 && (!NILP (Fget_char_property (make_number (g1->charpos),
14774 Qcursor, g1->object))
14775 /* Previous candidate is from the same display
14776 string as this one, and the display string
14777 came from a text property. */
14778 || (EQ (g1->object, glyph->object)
14779 && string_from_text_prop)
14780 /* this candidate is from newline and its
14781 position is not an exact match */
14782 || (INTEGERP (glyph->object)
14783 && glyph->charpos != pt_old)))))
14784 return 0;
14785 /* If this candidate gives an exact match, use that. */
14786 if (!((BUFFERP (glyph->object) && glyph->charpos == pt_old)
14787 /* If this candidate is a glyph created for the
14788 terminating newline of a line, and point is on that
14789 newline, it wins because it's an exact match. */
14790 || (!row->continued_p
14791 && INTEGERP (glyph->object)
14792 && glyph->charpos == 0
14793 && pt_old == MATRIX_ROW_END_CHARPOS (row) - 1))
14794 /* Otherwise, keep the candidate that comes from a row
14795 spanning less buffer positions. This may win when one or
14796 both candidate positions are on glyphs that came from
14797 display strings, for which we cannot compare buffer
14798 positions. */
14799 && MATRIX_ROW_END_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
14800 - MATRIX_ROW_START_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
14801 < MATRIX_ROW_END_CHARPOS (row) - MATRIX_ROW_START_CHARPOS (row))
14802 return 0;
14804 w->cursor.hpos = glyph - row->glyphs[TEXT_AREA];
14805 w->cursor.x = x;
14806 w->cursor.vpos = MATRIX_ROW_VPOS (row, matrix) + dvpos;
14807 w->cursor.y = row->y + dy;
14809 if (w == XWINDOW (selected_window))
14811 if (!row->continued_p
14812 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
14813 && row->x == 0)
14815 this_line_buffer = XBUFFER (w->contents);
14817 CHARPOS (this_line_start_pos)
14818 = MATRIX_ROW_START_CHARPOS (row) + delta;
14819 BYTEPOS (this_line_start_pos)
14820 = MATRIX_ROW_START_BYTEPOS (row) + delta_bytes;
14822 CHARPOS (this_line_end_pos)
14823 = Z - (MATRIX_ROW_END_CHARPOS (row) + delta);
14824 BYTEPOS (this_line_end_pos)
14825 = Z_BYTE - (MATRIX_ROW_END_BYTEPOS (row) + delta_bytes);
14827 this_line_y = w->cursor.y;
14828 this_line_pixel_height = row->height;
14829 this_line_vpos = w->cursor.vpos;
14830 this_line_start_x = row->x;
14832 else
14833 CHARPOS (this_line_start_pos) = 0;
14836 return 1;
14840 /* Run window scroll functions, if any, for WINDOW with new window
14841 start STARTP. Sets the window start of WINDOW to that position.
14843 We assume that the window's buffer is really current. */
14845 static struct text_pos
14846 run_window_scroll_functions (Lisp_Object window, struct text_pos startp)
14848 struct window *w = XWINDOW (window);
14849 SET_MARKER_FROM_TEXT_POS (w->start, startp);
14851 eassert (current_buffer == XBUFFER (w->contents));
14853 if (!NILP (Vwindow_scroll_functions))
14855 run_hook_with_args_2 (Qwindow_scroll_functions, window,
14856 make_number (CHARPOS (startp)));
14857 SET_TEXT_POS_FROM_MARKER (startp, w->start);
14858 /* In case the hook functions switch buffers. */
14859 set_buffer_internal (XBUFFER (w->contents));
14862 return startp;
14866 /* Make sure the line containing the cursor is fully visible.
14867 A value of 1 means there is nothing to be done.
14868 (Either the line is fully visible, or it cannot be made so,
14869 or we cannot tell.)
14871 If FORCE_P is non-zero, return 0 even if partial visible cursor row
14872 is higher than window.
14874 A value of 0 means the caller should do scrolling
14875 as if point had gone off the screen. */
14877 static int
14878 cursor_row_fully_visible_p (struct window *w, int force_p, int current_matrix_p)
14880 struct glyph_matrix *matrix;
14881 struct glyph_row *row;
14882 int window_height;
14884 if (!make_cursor_line_fully_visible_p)
14885 return 1;
14887 /* It's not always possible to find the cursor, e.g, when a window
14888 is full of overlay strings. Don't do anything in that case. */
14889 if (w->cursor.vpos < 0)
14890 return 1;
14892 matrix = current_matrix_p ? w->current_matrix : w->desired_matrix;
14893 row = MATRIX_ROW (matrix, w->cursor.vpos);
14895 /* If the cursor row is not partially visible, there's nothing to do. */
14896 if (!MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row))
14897 return 1;
14899 /* If the row the cursor is in is taller than the window's height,
14900 it's not clear what to do, so do nothing. */
14901 window_height = window_box_height (w);
14902 if (row->height >= window_height)
14904 if (!force_p || MINI_WINDOW_P (w)
14905 || w->vscroll || w->cursor.vpos == 0)
14906 return 1;
14908 return 0;
14912 /* Try scrolling PT into view in window WINDOW. JUST_THIS_ONE_P
14913 non-zero means only WINDOW is redisplayed in redisplay_internal.
14914 TEMP_SCROLL_STEP has the same meaning as emacs_scroll_step, and is used
14915 in redisplay_window to bring a partially visible line into view in
14916 the case that only the cursor has moved.
14918 LAST_LINE_MISFIT should be nonzero if we're scrolling because the
14919 last screen line's vertical height extends past the end of the screen.
14921 Value is
14923 1 if scrolling succeeded
14925 0 if scrolling didn't find point.
14927 -1 if new fonts have been loaded so that we must interrupt
14928 redisplay, adjust glyph matrices, and try again. */
14930 enum
14932 SCROLLING_SUCCESS,
14933 SCROLLING_FAILED,
14934 SCROLLING_NEED_LARGER_MATRICES
14937 /* If scroll-conservatively is more than this, never recenter.
14939 If you change this, don't forget to update the doc string of
14940 `scroll-conservatively' and the Emacs manual. */
14941 #define SCROLL_LIMIT 100
14943 static int
14944 try_scrolling (Lisp_Object window, int just_this_one_p,
14945 ptrdiff_t arg_scroll_conservatively, ptrdiff_t scroll_step,
14946 int temp_scroll_step, int last_line_misfit)
14948 struct window *w = XWINDOW (window);
14949 struct frame *f = XFRAME (w->frame);
14950 struct text_pos pos, startp;
14951 struct it it;
14952 int this_scroll_margin, scroll_max, rc, height;
14953 int dy = 0, amount_to_scroll = 0, scroll_down_p = 0;
14954 int extra_scroll_margin_lines = last_line_misfit ? 1 : 0;
14955 Lisp_Object aggressive;
14956 /* We will never try scrolling more than this number of lines. */
14957 int scroll_limit = SCROLL_LIMIT;
14958 int frame_line_height = default_line_pixel_height (w);
14959 int window_total_lines
14960 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
14962 #ifdef GLYPH_DEBUG
14963 debug_method_add (w, "try_scrolling");
14964 #endif
14966 SET_TEXT_POS_FROM_MARKER (startp, w->start);
14968 /* Compute scroll margin height in pixels. We scroll when point is
14969 within this distance from the top or bottom of the window. */
14970 if (scroll_margin > 0)
14971 this_scroll_margin = min (scroll_margin, window_total_lines / 4)
14972 * frame_line_height;
14973 else
14974 this_scroll_margin = 0;
14976 /* Force arg_scroll_conservatively to have a reasonable value, to
14977 avoid scrolling too far away with slow move_it_* functions. Note
14978 that the user can supply scroll-conservatively equal to
14979 `most-positive-fixnum', which can be larger than INT_MAX. */
14980 if (arg_scroll_conservatively > scroll_limit)
14982 arg_scroll_conservatively = scroll_limit + 1;
14983 scroll_max = scroll_limit * frame_line_height;
14985 else if (scroll_step || arg_scroll_conservatively || temp_scroll_step)
14986 /* Compute how much we should try to scroll maximally to bring
14987 point into view. */
14988 scroll_max = (max (scroll_step,
14989 max (arg_scroll_conservatively, temp_scroll_step))
14990 * frame_line_height);
14991 else if (NUMBERP (BVAR (current_buffer, scroll_down_aggressively))
14992 || NUMBERP (BVAR (current_buffer, scroll_up_aggressively)))
14993 /* We're trying to scroll because of aggressive scrolling but no
14994 scroll_step is set. Choose an arbitrary one. */
14995 scroll_max = 10 * frame_line_height;
14996 else
14997 scroll_max = 0;
14999 too_near_end:
15001 /* Decide whether to scroll down. */
15002 if (PT > CHARPOS (startp))
15004 int scroll_margin_y;
15006 /* Compute the pixel ypos of the scroll margin, then move IT to
15007 either that ypos or PT, whichever comes first. */
15008 start_display (&it, w, startp);
15009 scroll_margin_y = it.last_visible_y - this_scroll_margin
15010 - frame_line_height * extra_scroll_margin_lines;
15011 move_it_to (&it, PT, -1, scroll_margin_y - 1, -1,
15012 (MOVE_TO_POS | MOVE_TO_Y));
15014 if (PT > CHARPOS (it.current.pos))
15016 int y0 = line_bottom_y (&it);
15017 /* Compute how many pixels below window bottom to stop searching
15018 for PT. This avoids costly search for PT that is far away if
15019 the user limited scrolling by a small number of lines, but
15020 always finds PT if scroll_conservatively is set to a large
15021 number, such as most-positive-fixnum. */
15022 int slack = max (scroll_max, 10 * frame_line_height);
15023 int y_to_move = it.last_visible_y + slack;
15025 /* Compute the distance from the scroll margin to PT or to
15026 the scroll limit, whichever comes first. This should
15027 include the height of the cursor line, to make that line
15028 fully visible. */
15029 move_it_to (&it, PT, -1, y_to_move,
15030 -1, MOVE_TO_POS | MOVE_TO_Y);
15031 dy = line_bottom_y (&it) - y0;
15033 if (dy > scroll_max)
15034 return SCROLLING_FAILED;
15036 if (dy > 0)
15037 scroll_down_p = 1;
15041 if (scroll_down_p)
15043 /* Point is in or below the bottom scroll margin, so move the
15044 window start down. If scrolling conservatively, move it just
15045 enough down to make point visible. If scroll_step is set,
15046 move it down by scroll_step. */
15047 if (arg_scroll_conservatively)
15048 amount_to_scroll
15049 = min (max (dy, frame_line_height),
15050 frame_line_height * arg_scroll_conservatively);
15051 else if (scroll_step || temp_scroll_step)
15052 amount_to_scroll = scroll_max;
15053 else
15055 aggressive = BVAR (current_buffer, scroll_up_aggressively);
15056 height = WINDOW_BOX_TEXT_HEIGHT (w);
15057 if (NUMBERP (aggressive))
15059 double float_amount = XFLOATINT (aggressive) * height;
15060 int aggressive_scroll = float_amount;
15061 if (aggressive_scroll == 0 && float_amount > 0)
15062 aggressive_scroll = 1;
15063 /* Don't let point enter the scroll margin near top of
15064 the window. This could happen if the value of
15065 scroll_up_aggressively is too large and there are
15066 non-zero margins, because scroll_up_aggressively
15067 means put point that fraction of window height
15068 _from_the_bottom_margin_. */
15069 if (aggressive_scroll + 2*this_scroll_margin > height)
15070 aggressive_scroll = height - 2*this_scroll_margin;
15071 amount_to_scroll = dy + aggressive_scroll;
15075 if (amount_to_scroll <= 0)
15076 return SCROLLING_FAILED;
15078 start_display (&it, w, startp);
15079 if (arg_scroll_conservatively <= scroll_limit)
15080 move_it_vertically (&it, amount_to_scroll);
15081 else
15083 /* Extra precision for users who set scroll-conservatively
15084 to a large number: make sure the amount we scroll
15085 the window start is never less than amount_to_scroll,
15086 which was computed as distance from window bottom to
15087 point. This matters when lines at window top and lines
15088 below window bottom have different height. */
15089 struct it it1;
15090 void *it1data = NULL;
15091 /* We use a temporary it1 because line_bottom_y can modify
15092 its argument, if it moves one line down; see there. */
15093 int start_y;
15095 SAVE_IT (it1, it, it1data);
15096 start_y = line_bottom_y (&it1);
15097 do {
15098 RESTORE_IT (&it, &it, it1data);
15099 move_it_by_lines (&it, 1);
15100 SAVE_IT (it1, it, it1data);
15101 } while (line_bottom_y (&it1) - start_y < amount_to_scroll);
15104 /* If STARTP is unchanged, move it down another screen line. */
15105 if (CHARPOS (it.current.pos) == CHARPOS (startp))
15106 move_it_by_lines (&it, 1);
15107 startp = it.current.pos;
15109 else
15111 struct text_pos scroll_margin_pos = startp;
15112 int y_offset = 0;
15114 /* See if point is inside the scroll margin at the top of the
15115 window. */
15116 if (this_scroll_margin)
15118 int y_start;
15120 start_display (&it, w, startp);
15121 y_start = it.current_y;
15122 move_it_vertically (&it, this_scroll_margin);
15123 scroll_margin_pos = it.current.pos;
15124 /* If we didn't move enough before hitting ZV, request
15125 additional amount of scroll, to move point out of the
15126 scroll margin. */
15127 if (IT_CHARPOS (it) == ZV
15128 && it.current_y - y_start < this_scroll_margin)
15129 y_offset = this_scroll_margin - (it.current_y - y_start);
15132 if (PT < CHARPOS (scroll_margin_pos))
15134 /* Point is in the scroll margin at the top of the window or
15135 above what is displayed in the window. */
15136 int y0, y_to_move;
15138 /* Compute the vertical distance from PT to the scroll
15139 margin position. Move as far as scroll_max allows, or
15140 one screenful, or 10 screen lines, whichever is largest.
15141 Give up if distance is greater than scroll_max or if we
15142 didn't reach the scroll margin position. */
15143 SET_TEXT_POS (pos, PT, PT_BYTE);
15144 start_display (&it, w, pos);
15145 y0 = it.current_y;
15146 y_to_move = max (it.last_visible_y,
15147 max (scroll_max, 10 * frame_line_height));
15148 move_it_to (&it, CHARPOS (scroll_margin_pos), 0,
15149 y_to_move, -1,
15150 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
15151 dy = it.current_y - y0;
15152 if (dy > scroll_max
15153 || IT_CHARPOS (it) < CHARPOS (scroll_margin_pos))
15154 return SCROLLING_FAILED;
15156 /* Additional scroll for when ZV was too close to point. */
15157 dy += y_offset;
15159 /* Compute new window start. */
15160 start_display (&it, w, startp);
15162 if (arg_scroll_conservatively)
15163 amount_to_scroll = max (dy, frame_line_height *
15164 max (scroll_step, temp_scroll_step));
15165 else if (scroll_step || temp_scroll_step)
15166 amount_to_scroll = scroll_max;
15167 else
15169 aggressive = BVAR (current_buffer, scroll_down_aggressively);
15170 height = WINDOW_BOX_TEXT_HEIGHT (w);
15171 if (NUMBERP (aggressive))
15173 double float_amount = XFLOATINT (aggressive) * height;
15174 int aggressive_scroll = float_amount;
15175 if (aggressive_scroll == 0 && float_amount > 0)
15176 aggressive_scroll = 1;
15177 /* Don't let point enter the scroll margin near
15178 bottom of the window, if the value of
15179 scroll_down_aggressively happens to be too
15180 large. */
15181 if (aggressive_scroll + 2*this_scroll_margin > height)
15182 aggressive_scroll = height - 2*this_scroll_margin;
15183 amount_to_scroll = dy + aggressive_scroll;
15187 if (amount_to_scroll <= 0)
15188 return SCROLLING_FAILED;
15190 move_it_vertically_backward (&it, amount_to_scroll);
15191 startp = it.current.pos;
15195 /* Run window scroll functions. */
15196 startp = run_window_scroll_functions (window, startp);
15198 /* Display the window. Give up if new fonts are loaded, or if point
15199 doesn't appear. */
15200 if (!try_window (window, startp, 0))
15201 rc = SCROLLING_NEED_LARGER_MATRICES;
15202 else if (w->cursor.vpos < 0)
15204 clear_glyph_matrix (w->desired_matrix);
15205 rc = SCROLLING_FAILED;
15207 else
15209 /* Maybe forget recorded base line for line number display. */
15210 if (!just_this_one_p
15211 || current_buffer->clip_changed
15212 || BEG_UNCHANGED < CHARPOS (startp))
15213 w->base_line_number = 0;
15215 /* If cursor ends up on a partially visible line,
15216 treat that as being off the bottom of the screen. */
15217 if (! cursor_row_fully_visible_p (w, extra_scroll_margin_lines <= 1, 0)
15218 /* It's possible that the cursor is on the first line of the
15219 buffer, which is partially obscured due to a vscroll
15220 (Bug#7537). In that case, avoid looping forever. */
15221 && extra_scroll_margin_lines < w->desired_matrix->nrows - 1)
15223 clear_glyph_matrix (w->desired_matrix);
15224 ++extra_scroll_margin_lines;
15225 goto too_near_end;
15227 rc = SCROLLING_SUCCESS;
15230 return rc;
15234 /* Compute a suitable window start for window W if display of W starts
15235 on a continuation line. Value is non-zero if a new window start
15236 was computed.
15238 The new window start will be computed, based on W's width, starting
15239 from the start of the continued line. It is the start of the
15240 screen line with the minimum distance from the old start W->start. */
15242 static int
15243 compute_window_start_on_continuation_line (struct window *w)
15245 struct text_pos pos, start_pos;
15246 int window_start_changed_p = 0;
15248 SET_TEXT_POS_FROM_MARKER (start_pos, w->start);
15250 /* If window start is on a continuation line... Window start may be
15251 < BEGV in case there's invisible text at the start of the
15252 buffer (M-x rmail, for example). */
15253 if (CHARPOS (start_pos) > BEGV
15254 && FETCH_BYTE (BYTEPOS (start_pos) - 1) != '\n')
15256 struct it it;
15257 struct glyph_row *row;
15259 /* Handle the case that the window start is out of range. */
15260 if (CHARPOS (start_pos) < BEGV)
15261 SET_TEXT_POS (start_pos, BEGV, BEGV_BYTE);
15262 else if (CHARPOS (start_pos) > ZV)
15263 SET_TEXT_POS (start_pos, ZV, ZV_BYTE);
15265 /* Find the start of the continued line. This should be fast
15266 because find_newline is fast (newline cache). */
15267 row = w->desired_matrix->rows + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0);
15268 init_iterator (&it, w, CHARPOS (start_pos), BYTEPOS (start_pos),
15269 row, DEFAULT_FACE_ID);
15270 reseat_at_previous_visible_line_start (&it);
15272 /* If the line start is "too far" away from the window start,
15273 say it takes too much time to compute a new window start. */
15274 if (CHARPOS (start_pos) - IT_CHARPOS (it)
15275 /* PXW: Do we need upper bounds here? */
15276 < WINDOW_TOTAL_LINES (w) * WINDOW_TOTAL_COLS (w))
15278 int min_distance, distance;
15280 /* Move forward by display lines to find the new window
15281 start. If window width was enlarged, the new start can
15282 be expected to be > the old start. If window width was
15283 decreased, the new window start will be < the old start.
15284 So, we're looking for the display line start with the
15285 minimum distance from the old window start. */
15286 pos = it.current.pos;
15287 min_distance = INFINITY;
15288 while ((distance = eabs (CHARPOS (start_pos) - IT_CHARPOS (it))),
15289 distance < min_distance)
15291 min_distance = distance;
15292 pos = it.current.pos;
15293 if (it.line_wrap == WORD_WRAP)
15295 /* Under WORD_WRAP, move_it_by_lines is likely to
15296 overshoot and stop not at the first, but the
15297 second character from the left margin. So in
15298 that case, we need a more tight control on the X
15299 coordinate of the iterator than move_it_by_lines
15300 promises in its contract. The method is to first
15301 go to the last (rightmost) visible character of a
15302 line, then move to the leftmost character on the
15303 next line in a separate call. */
15304 move_it_to (&it, ZV, it.last_visible_x, it.current_y, -1,
15305 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
15306 move_it_to (&it, ZV, 0,
15307 it.current_y + it.max_ascent + it.max_descent, -1,
15308 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
15310 else
15311 move_it_by_lines (&it, 1);
15314 /* Set the window start there. */
15315 SET_MARKER_FROM_TEXT_POS (w->start, pos);
15316 window_start_changed_p = 1;
15320 return window_start_changed_p;
15324 /* Try cursor movement in case text has not changed in window WINDOW,
15325 with window start STARTP. Value is
15327 CURSOR_MOVEMENT_SUCCESS if successful
15329 CURSOR_MOVEMENT_CANNOT_BE_USED if this method cannot be used
15331 CURSOR_MOVEMENT_MUST_SCROLL if we know we have to scroll the
15332 display. *SCROLL_STEP is set to 1, under certain circumstances, if
15333 we want to scroll as if scroll-step were set to 1. See the code.
15335 CURSOR_MOVEMENT_NEED_LARGER_MATRICES if we need larger matrices, in
15336 which case we have to abort this redisplay, and adjust matrices
15337 first. */
15339 enum
15341 CURSOR_MOVEMENT_SUCCESS,
15342 CURSOR_MOVEMENT_CANNOT_BE_USED,
15343 CURSOR_MOVEMENT_MUST_SCROLL,
15344 CURSOR_MOVEMENT_NEED_LARGER_MATRICES
15347 static int
15348 try_cursor_movement (Lisp_Object window, struct text_pos startp, int *scroll_step)
15350 struct window *w = XWINDOW (window);
15351 struct frame *f = XFRAME (w->frame);
15352 int rc = CURSOR_MOVEMENT_CANNOT_BE_USED;
15354 #ifdef GLYPH_DEBUG
15355 if (inhibit_try_cursor_movement)
15356 return rc;
15357 #endif
15359 /* Previously, there was a check for Lisp integer in the
15360 if-statement below. Now, this field is converted to
15361 ptrdiff_t, thus zero means invalid position in a buffer. */
15362 eassert (w->last_point > 0);
15363 /* Likewise there was a check whether window_end_vpos is nil or larger
15364 than the window. Now window_end_vpos is int and so never nil, but
15365 let's leave eassert to check whether it fits in the window. */
15366 eassert (w->window_end_vpos < w->current_matrix->nrows);
15368 /* Handle case where text has not changed, only point, and it has
15369 not moved off the frame. */
15370 if (/* Point may be in this window. */
15371 PT >= CHARPOS (startp)
15372 /* Selective display hasn't changed. */
15373 && !current_buffer->clip_changed
15374 /* Function force-mode-line-update is used to force a thorough
15375 redisplay. It sets either windows_or_buffers_changed or
15376 update_mode_lines. So don't take a shortcut here for these
15377 cases. */
15378 && !update_mode_lines
15379 && !windows_or_buffers_changed
15380 && !f->cursor_type_changed
15381 && NILP (Vshow_trailing_whitespace)
15382 /* This code is not used for mini-buffer for the sake of the case
15383 of redisplaying to replace an echo area message; since in
15384 that case the mini-buffer contents per se are usually
15385 unchanged. This code is of no real use in the mini-buffer
15386 since the handling of this_line_start_pos, etc., in redisplay
15387 handles the same cases. */
15388 && !EQ (window, minibuf_window)
15389 && (FRAME_WINDOW_P (f)
15390 || !overlay_arrow_in_current_buffer_p ()))
15392 int this_scroll_margin, top_scroll_margin;
15393 struct glyph_row *row = NULL;
15394 int frame_line_height = default_line_pixel_height (w);
15395 int window_total_lines
15396 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
15398 #ifdef GLYPH_DEBUG
15399 debug_method_add (w, "cursor movement");
15400 #endif
15402 /* Scroll if point within this distance from the top or bottom
15403 of the window. This is a pixel value. */
15404 if (scroll_margin > 0)
15406 this_scroll_margin = min (scroll_margin, window_total_lines / 4);
15407 this_scroll_margin *= frame_line_height;
15409 else
15410 this_scroll_margin = 0;
15412 top_scroll_margin = this_scroll_margin;
15413 if (WINDOW_WANTS_HEADER_LINE_P (w))
15414 top_scroll_margin += CURRENT_HEADER_LINE_HEIGHT (w);
15416 /* Start with the row the cursor was displayed during the last
15417 not paused redisplay. Give up if that row is not valid. */
15418 if (w->last_cursor_vpos < 0
15419 || w->last_cursor_vpos >= w->current_matrix->nrows)
15420 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15421 else
15423 row = MATRIX_ROW (w->current_matrix, w->last_cursor_vpos);
15424 if (row->mode_line_p)
15425 ++row;
15426 if (!row->enabled_p)
15427 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15430 if (rc == CURSOR_MOVEMENT_CANNOT_BE_USED)
15432 int scroll_p = 0, must_scroll = 0;
15433 int last_y = window_text_bottom_y (w) - this_scroll_margin;
15435 if (PT > w->last_point)
15437 /* Point has moved forward. */
15438 while (MATRIX_ROW_END_CHARPOS (row) < PT
15439 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
15441 eassert (row->enabled_p);
15442 ++row;
15445 /* If the end position of a row equals the start
15446 position of the next row, and PT is at that position,
15447 we would rather display cursor in the next line. */
15448 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
15449 && MATRIX_ROW_END_CHARPOS (row) == PT
15450 && row < MATRIX_MODE_LINE_ROW (w->current_matrix)
15451 && MATRIX_ROW_START_CHARPOS (row+1) == PT
15452 && !cursor_row_p (row))
15453 ++row;
15455 /* If within the scroll margin, scroll. Note that
15456 MATRIX_ROW_BOTTOM_Y gives the pixel position at which
15457 the next line would be drawn, and that
15458 this_scroll_margin can be zero. */
15459 if (MATRIX_ROW_BOTTOM_Y (row) > last_y
15460 || PT > MATRIX_ROW_END_CHARPOS (row)
15461 /* Line is completely visible last line in window
15462 and PT is to be set in the next line. */
15463 || (MATRIX_ROW_BOTTOM_Y (row) == last_y
15464 && PT == MATRIX_ROW_END_CHARPOS (row)
15465 && !row->ends_at_zv_p
15466 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
15467 scroll_p = 1;
15469 else if (PT < w->last_point)
15471 /* Cursor has to be moved backward. Note that PT >=
15472 CHARPOS (startp) because of the outer if-statement. */
15473 while (!row->mode_line_p
15474 && (MATRIX_ROW_START_CHARPOS (row) > PT
15475 || (MATRIX_ROW_START_CHARPOS (row) == PT
15476 && (MATRIX_ROW_STARTS_IN_MIDDLE_OF_CHAR_P (row)
15477 || (/* STARTS_IN_MIDDLE_OF_STRING_P (row) */
15478 row > w->current_matrix->rows
15479 && (row-1)->ends_in_newline_from_string_p))))
15480 && (row->y > top_scroll_margin
15481 || CHARPOS (startp) == BEGV))
15483 eassert (row->enabled_p);
15484 --row;
15487 /* Consider the following case: Window starts at BEGV,
15488 there is invisible, intangible text at BEGV, so that
15489 display starts at some point START > BEGV. It can
15490 happen that we are called with PT somewhere between
15491 BEGV and START. Try to handle that case. */
15492 if (row < w->current_matrix->rows
15493 || row->mode_line_p)
15495 row = w->current_matrix->rows;
15496 if (row->mode_line_p)
15497 ++row;
15500 /* Due to newlines in overlay strings, we may have to
15501 skip forward over overlay strings. */
15502 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
15503 && MATRIX_ROW_END_CHARPOS (row) == PT
15504 && !cursor_row_p (row))
15505 ++row;
15507 /* If within the scroll margin, scroll. */
15508 if (row->y < top_scroll_margin
15509 && CHARPOS (startp) != BEGV)
15510 scroll_p = 1;
15512 else
15514 /* Cursor did not move. So don't scroll even if cursor line
15515 is partially visible, as it was so before. */
15516 rc = CURSOR_MOVEMENT_SUCCESS;
15519 if (PT < MATRIX_ROW_START_CHARPOS (row)
15520 || PT > MATRIX_ROW_END_CHARPOS (row))
15522 /* if PT is not in the glyph row, give up. */
15523 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15524 must_scroll = 1;
15526 else if (rc != CURSOR_MOVEMENT_SUCCESS
15527 && !NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering)))
15529 struct glyph_row *row1;
15531 /* If rows are bidi-reordered and point moved, back up
15532 until we find a row that does not belong to a
15533 continuation line. This is because we must consider
15534 all rows of a continued line as candidates for the
15535 new cursor positioning, since row start and end
15536 positions change non-linearly with vertical position
15537 in such rows. */
15538 /* FIXME: Revisit this when glyph ``spilling'' in
15539 continuation lines' rows is implemented for
15540 bidi-reordered rows. */
15541 for (row1 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
15542 MATRIX_ROW_CONTINUATION_LINE_P (row);
15543 --row)
15545 /* If we hit the beginning of the displayed portion
15546 without finding the first row of a continued
15547 line, give up. */
15548 if (row <= row1)
15550 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15551 break;
15553 eassert (row->enabled_p);
15556 if (must_scroll)
15558 else if (rc != CURSOR_MOVEMENT_SUCCESS
15559 && MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row)
15560 /* Make sure this isn't a header line by any chance, since
15561 then MATRIX_ROW_PARTIALLY_VISIBLE_P might yield non-zero. */
15562 && !row->mode_line_p
15563 && make_cursor_line_fully_visible_p)
15565 if (PT == MATRIX_ROW_END_CHARPOS (row)
15566 && !row->ends_at_zv_p
15567 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
15568 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15569 else if (row->height > window_box_height (w))
15571 /* If we end up in a partially visible line, let's
15572 make it fully visible, except when it's taller
15573 than the window, in which case we can't do much
15574 about it. */
15575 *scroll_step = 1;
15576 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15578 else
15580 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
15581 if (!cursor_row_fully_visible_p (w, 0, 1))
15582 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15583 else
15584 rc = CURSOR_MOVEMENT_SUCCESS;
15587 else if (scroll_p)
15588 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15589 else if (rc != CURSOR_MOVEMENT_SUCCESS
15590 && !NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering)))
15592 /* With bidi-reordered rows, there could be more than
15593 one candidate row whose start and end positions
15594 occlude point. We need to let set_cursor_from_row
15595 find the best candidate. */
15596 /* FIXME: Revisit this when glyph ``spilling'' in
15597 continuation lines' rows is implemented for
15598 bidi-reordered rows. */
15599 int rv = 0;
15603 int at_zv_p = 0, exact_match_p = 0;
15605 if (MATRIX_ROW_START_CHARPOS (row) <= PT
15606 && PT <= MATRIX_ROW_END_CHARPOS (row)
15607 && cursor_row_p (row))
15608 rv |= set_cursor_from_row (w, row, w->current_matrix,
15609 0, 0, 0, 0);
15610 /* As soon as we've found the exact match for point,
15611 or the first suitable row whose ends_at_zv_p flag
15612 is set, we are done. */
15613 if (rv)
15615 at_zv_p = MATRIX_ROW (w->current_matrix,
15616 w->cursor.vpos)->ends_at_zv_p;
15617 if (!at_zv_p
15618 && w->cursor.hpos >= 0
15619 && w->cursor.hpos < MATRIX_ROW_USED (w->current_matrix,
15620 w->cursor.vpos))
15622 struct glyph_row *candidate =
15623 MATRIX_ROW (w->current_matrix, w->cursor.vpos);
15624 struct glyph *g =
15625 candidate->glyphs[TEXT_AREA] + w->cursor.hpos;
15626 ptrdiff_t endpos = MATRIX_ROW_END_CHARPOS (candidate);
15628 exact_match_p =
15629 (BUFFERP (g->object) && g->charpos == PT)
15630 || (INTEGERP (g->object)
15631 && (g->charpos == PT
15632 || (g->charpos == 0 && endpos - 1 == PT)));
15634 if (at_zv_p || exact_match_p)
15636 rc = CURSOR_MOVEMENT_SUCCESS;
15637 break;
15640 if (MATRIX_ROW_BOTTOM_Y (row) == last_y)
15641 break;
15642 ++row;
15644 while (((MATRIX_ROW_CONTINUATION_LINE_P (row)
15645 || row->continued_p)
15646 && MATRIX_ROW_BOTTOM_Y (row) <= last_y)
15647 || (MATRIX_ROW_START_CHARPOS (row) == PT
15648 && MATRIX_ROW_BOTTOM_Y (row) < last_y));
15649 /* If we didn't find any candidate rows, or exited the
15650 loop before all the candidates were examined, signal
15651 to the caller that this method failed. */
15652 if (rc != CURSOR_MOVEMENT_SUCCESS
15653 && !(rv
15654 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
15655 && !row->continued_p))
15656 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15657 else if (rv)
15658 rc = CURSOR_MOVEMENT_SUCCESS;
15660 else
15664 if (set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0))
15666 rc = CURSOR_MOVEMENT_SUCCESS;
15667 break;
15669 ++row;
15671 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
15672 && MATRIX_ROW_START_CHARPOS (row) == PT
15673 && cursor_row_p (row));
15678 return rc;
15681 #if !defined USE_TOOLKIT_SCROLL_BARS || defined USE_GTK
15682 static
15683 #endif
15684 void
15685 set_vertical_scroll_bar (struct window *w)
15687 ptrdiff_t start, end, whole;
15689 /* Calculate the start and end positions for the current window.
15690 At some point, it would be nice to choose between scrollbars
15691 which reflect the whole buffer size, with special markers
15692 indicating narrowing, and scrollbars which reflect only the
15693 visible region.
15695 Note that mini-buffers sometimes aren't displaying any text. */
15696 if (!MINI_WINDOW_P (w)
15697 || (w == XWINDOW (minibuf_window)
15698 && NILP (echo_area_buffer[0])))
15700 struct buffer *buf = XBUFFER (w->contents);
15701 whole = BUF_ZV (buf) - BUF_BEGV (buf);
15702 start = marker_position (w->start) - BUF_BEGV (buf);
15703 /* I don't think this is guaranteed to be right. For the
15704 moment, we'll pretend it is. */
15705 end = BUF_Z (buf) - w->window_end_pos - BUF_BEGV (buf);
15707 if (end < start)
15708 end = start;
15709 if (whole < (end - start))
15710 whole = end - start;
15712 else
15713 start = end = whole = 0;
15715 /* Indicate what this scroll bar ought to be displaying now. */
15716 if (FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
15717 (*FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
15718 (w, end - start, whole, start);
15722 /* Redisplay leaf window WINDOW. JUST_THIS_ONE_P non-zero means only
15723 selected_window is redisplayed.
15725 We can return without actually redisplaying the window if fonts has been
15726 changed on window's frame. In that case, redisplay_internal will retry. */
15728 static void
15729 redisplay_window (Lisp_Object window, bool just_this_one_p)
15731 struct window *w = XWINDOW (window);
15732 struct frame *f = XFRAME (w->frame);
15733 struct buffer *buffer = XBUFFER (w->contents);
15734 struct buffer *old = current_buffer;
15735 struct text_pos lpoint, opoint, startp;
15736 int update_mode_line;
15737 int tem;
15738 struct it it;
15739 /* Record it now because it's overwritten. */
15740 bool current_matrix_up_to_date_p = false;
15741 bool used_current_matrix_p = false;
15742 /* This is less strict than current_matrix_up_to_date_p.
15743 It indicates that the buffer contents and narrowing are unchanged. */
15744 bool buffer_unchanged_p = false;
15745 int temp_scroll_step = 0;
15746 ptrdiff_t count = SPECPDL_INDEX ();
15747 int rc;
15748 int centering_position = -1;
15749 int last_line_misfit = 0;
15750 ptrdiff_t beg_unchanged, end_unchanged;
15751 int frame_line_height;
15753 SET_TEXT_POS (lpoint, PT, PT_BYTE);
15754 opoint = lpoint;
15756 #ifdef GLYPH_DEBUG
15757 *w->desired_matrix->method = 0;
15758 #endif
15760 if (!just_this_one_p
15761 && REDISPLAY_SOME_P ()
15762 && !w->redisplay
15763 && !f->redisplay
15764 && !buffer->text->redisplay
15765 && BUF_PT (buffer) == w->last_point)
15766 return;
15768 /* Make sure that both W's markers are valid. */
15769 eassert (XMARKER (w->start)->buffer == buffer);
15770 eassert (XMARKER (w->pointm)->buffer == buffer);
15772 restart:
15773 reconsider_clip_changes (w);
15774 frame_line_height = default_line_pixel_height (w);
15776 /* Has the mode line to be updated? */
15777 update_mode_line = (w->update_mode_line
15778 || update_mode_lines
15779 || buffer->clip_changed
15780 || buffer->prevent_redisplay_optimizations_p);
15782 if (!just_this_one_p)
15783 /* If `just_this_one_p' is set, we apparently set must_be_updated_p more
15784 cleverly elsewhere. */
15785 w->must_be_updated_p = true;
15787 if (MINI_WINDOW_P (w))
15789 if (w == XWINDOW (echo_area_window)
15790 && !NILP (echo_area_buffer[0]))
15792 if (update_mode_line)
15793 /* We may have to update a tty frame's menu bar or a
15794 tool-bar. Example `M-x C-h C-h C-g'. */
15795 goto finish_menu_bars;
15796 else
15797 /* We've already displayed the echo area glyphs in this window. */
15798 goto finish_scroll_bars;
15800 else if ((w != XWINDOW (minibuf_window)
15801 || minibuf_level == 0)
15802 /* When buffer is nonempty, redisplay window normally. */
15803 && BUF_Z (XBUFFER (w->contents)) == BUF_BEG (XBUFFER (w->contents))
15804 /* Quail displays non-mini buffers in minibuffer window.
15805 In that case, redisplay the window normally. */
15806 && !NILP (Fmemq (w->contents, Vminibuffer_list)))
15808 /* W is a mini-buffer window, but it's not active, so clear
15809 it. */
15810 int yb = window_text_bottom_y (w);
15811 struct glyph_row *row;
15812 int y;
15814 for (y = 0, row = w->desired_matrix->rows;
15815 y < yb;
15816 y += row->height, ++row)
15817 blank_row (w, row, y);
15818 goto finish_scroll_bars;
15821 clear_glyph_matrix (w->desired_matrix);
15824 /* Otherwise set up data on this window; select its buffer and point
15825 value. */
15826 /* Really select the buffer, for the sake of buffer-local
15827 variables. */
15828 set_buffer_internal_1 (XBUFFER (w->contents));
15830 current_matrix_up_to_date_p
15831 = (w->window_end_valid
15832 && !current_buffer->clip_changed
15833 && !current_buffer->prevent_redisplay_optimizations_p
15834 && !window_outdated (w));
15836 /* Run the window-bottom-change-functions
15837 if it is possible that the text on the screen has changed
15838 (either due to modification of the text, or any other reason). */
15839 if (!current_matrix_up_to_date_p
15840 && !NILP (Vwindow_text_change_functions))
15842 safe_run_hooks (Qwindow_text_change_functions);
15843 goto restart;
15846 beg_unchanged = BEG_UNCHANGED;
15847 end_unchanged = END_UNCHANGED;
15849 SET_TEXT_POS (opoint, PT, PT_BYTE);
15851 specbind (Qinhibit_point_motion_hooks, Qt);
15853 buffer_unchanged_p
15854 = (w->window_end_valid
15855 && !current_buffer->clip_changed
15856 && !window_outdated (w));
15858 /* When windows_or_buffers_changed is non-zero, we can't rely
15859 on the window end being valid, so set it to zero there. */
15860 if (windows_or_buffers_changed)
15862 /* If window starts on a continuation line, maybe adjust the
15863 window start in case the window's width changed. */
15864 if (XMARKER (w->start)->buffer == current_buffer)
15865 compute_window_start_on_continuation_line (w);
15867 w->window_end_valid = false;
15868 /* If so, we also can't rely on current matrix
15869 and should not fool try_cursor_movement below. */
15870 current_matrix_up_to_date_p = false;
15873 /* Some sanity checks. */
15874 CHECK_WINDOW_END (w);
15875 if (Z == Z_BYTE && CHARPOS (opoint) != BYTEPOS (opoint))
15876 emacs_abort ();
15877 if (BYTEPOS (opoint) < CHARPOS (opoint))
15878 emacs_abort ();
15880 if (mode_line_update_needed (w))
15881 update_mode_line = 1;
15883 /* Point refers normally to the selected window. For any other
15884 window, set up appropriate value. */
15885 if (!EQ (window, selected_window))
15887 ptrdiff_t new_pt = marker_position (w->pointm);
15888 ptrdiff_t new_pt_byte = marker_byte_position (w->pointm);
15889 if (new_pt < BEGV)
15891 new_pt = BEGV;
15892 new_pt_byte = BEGV_BYTE;
15893 set_marker_both (w->pointm, Qnil, BEGV, BEGV_BYTE);
15895 else if (new_pt > (ZV - 1))
15897 new_pt = ZV;
15898 new_pt_byte = ZV_BYTE;
15899 set_marker_both (w->pointm, Qnil, ZV, ZV_BYTE);
15902 /* We don't use SET_PT so that the point-motion hooks don't run. */
15903 TEMP_SET_PT_BOTH (new_pt, new_pt_byte);
15906 /* If any of the character widths specified in the display table
15907 have changed, invalidate the width run cache. It's true that
15908 this may be a bit late to catch such changes, but the rest of
15909 redisplay goes (non-fatally) haywire when the display table is
15910 changed, so why should we worry about doing any better? */
15911 if (current_buffer->width_run_cache
15912 || (current_buffer->base_buffer
15913 && current_buffer->base_buffer->width_run_cache))
15915 struct Lisp_Char_Table *disptab = buffer_display_table ();
15917 if (! disptab_matches_widthtab
15918 (disptab, XVECTOR (BVAR (current_buffer, width_table))))
15920 struct buffer *buf = current_buffer;
15922 if (buf->base_buffer)
15923 buf = buf->base_buffer;
15924 invalidate_region_cache (buf, buf->width_run_cache, BEG, Z);
15925 recompute_width_table (current_buffer, disptab);
15929 /* If window-start is screwed up, choose a new one. */
15930 if (XMARKER (w->start)->buffer != current_buffer)
15931 goto recenter;
15933 SET_TEXT_POS_FROM_MARKER (startp, w->start);
15935 /* If someone specified a new starting point but did not insist,
15936 check whether it can be used. */
15937 if (w->optional_new_start
15938 && CHARPOS (startp) >= BEGV
15939 && CHARPOS (startp) <= ZV)
15941 w->optional_new_start = 0;
15942 start_display (&it, w, startp);
15943 move_it_to (&it, PT, 0, it.last_visible_y, -1,
15944 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
15945 if (IT_CHARPOS (it) == PT)
15946 w->force_start = 1;
15947 /* IT may overshoot PT if text at PT is invisible. */
15948 else if (IT_CHARPOS (it) > PT && CHARPOS (startp) <= PT)
15949 w->force_start = 1;
15952 force_start:
15954 /* Handle case where place to start displaying has been specified,
15955 unless the specified location is outside the accessible range. */
15956 if (w->force_start || window_frozen_p (w))
15958 /* We set this later on if we have to adjust point. */
15959 int new_vpos = -1;
15961 w->force_start = 0;
15962 w->vscroll = 0;
15963 w->window_end_valid = 0;
15965 /* Forget any recorded base line for line number display. */
15966 if (!buffer_unchanged_p)
15967 w->base_line_number = 0;
15969 /* Redisplay the mode line. Select the buffer properly for that.
15970 Also, run the hook window-scroll-functions
15971 because we have scrolled. */
15972 /* Note, we do this after clearing force_start because
15973 if there's an error, it is better to forget about force_start
15974 than to get into an infinite loop calling the hook functions
15975 and having them get more errors. */
15976 if (!update_mode_line
15977 || ! NILP (Vwindow_scroll_functions))
15979 update_mode_line = 1;
15980 w->update_mode_line = 1;
15981 startp = run_window_scroll_functions (window, startp);
15984 if (CHARPOS (startp) < BEGV)
15985 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
15986 else if (CHARPOS (startp) > ZV)
15987 SET_TEXT_POS (startp, ZV, ZV_BYTE);
15989 /* Redisplay, then check if cursor has been set during the
15990 redisplay. Give up if new fonts were loaded. */
15991 /* We used to issue a CHECK_MARGINS argument to try_window here,
15992 but this causes scrolling to fail when point begins inside
15993 the scroll margin (bug#148) -- cyd */
15994 if (!try_window (window, startp, 0))
15996 w->force_start = 1;
15997 clear_glyph_matrix (w->desired_matrix);
15998 goto need_larger_matrices;
16001 if (w->cursor.vpos < 0 && !window_frozen_p (w))
16003 /* If point does not appear, try to move point so it does
16004 appear. The desired matrix has been built above, so we
16005 can use it here. */
16006 new_vpos = window_box_height (w) / 2;
16009 if (!cursor_row_fully_visible_p (w, 0, 0))
16011 /* Point does appear, but on a line partly visible at end of window.
16012 Move it back to a fully-visible line. */
16013 new_vpos = window_box_height (w);
16015 else if (w->cursor.vpos >= 0)
16017 /* Some people insist on not letting point enter the scroll
16018 margin, even though this part handles windows that didn't
16019 scroll at all. */
16020 int window_total_lines
16021 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
16022 int margin = min (scroll_margin, window_total_lines / 4);
16023 int pixel_margin = margin * frame_line_height;
16024 bool header_line = WINDOW_WANTS_HEADER_LINE_P (w);
16026 /* Note: We add an extra FRAME_LINE_HEIGHT, because the loop
16027 below, which finds the row to move point to, advances by
16028 the Y coordinate of the _next_ row, see the definition of
16029 MATRIX_ROW_BOTTOM_Y. */
16030 if (w->cursor.vpos < margin + header_line)
16032 w->cursor.vpos = -1;
16033 clear_glyph_matrix (w->desired_matrix);
16034 goto try_to_scroll;
16036 else
16038 int window_height = window_box_height (w);
16040 if (header_line)
16041 window_height += CURRENT_HEADER_LINE_HEIGHT (w);
16042 if (w->cursor.y >= window_height - pixel_margin)
16044 w->cursor.vpos = -1;
16045 clear_glyph_matrix (w->desired_matrix);
16046 goto try_to_scroll;
16051 /* If we need to move point for either of the above reasons,
16052 now actually do it. */
16053 if (new_vpos >= 0)
16055 struct glyph_row *row;
16057 row = MATRIX_FIRST_TEXT_ROW (w->desired_matrix);
16058 while (MATRIX_ROW_BOTTOM_Y (row) < new_vpos)
16059 ++row;
16061 TEMP_SET_PT_BOTH (MATRIX_ROW_START_CHARPOS (row),
16062 MATRIX_ROW_START_BYTEPOS (row));
16064 if (w != XWINDOW (selected_window))
16065 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
16066 else if (current_buffer == old)
16067 SET_TEXT_POS (lpoint, PT, PT_BYTE);
16069 set_cursor_from_row (w, row, w->desired_matrix, 0, 0, 0, 0);
16071 /* If we are highlighting the region, then we just changed
16072 the region, so redisplay to show it. */
16073 /* FIXME: We need to (re)run pre-redisplay-function! */
16074 /* if (markpos_of_region () >= 0)
16076 clear_glyph_matrix (w->desired_matrix);
16077 if (!try_window (window, startp, 0))
16078 goto need_larger_matrices;
16083 #ifdef GLYPH_DEBUG
16084 debug_method_add (w, "forced window start");
16085 #endif
16086 goto done;
16089 /* Handle case where text has not changed, only point, and it has
16090 not moved off the frame, and we are not retrying after hscroll.
16091 (current_matrix_up_to_date_p is nonzero when retrying.) */
16092 if (current_matrix_up_to_date_p
16093 && (rc = try_cursor_movement (window, startp, &temp_scroll_step),
16094 rc != CURSOR_MOVEMENT_CANNOT_BE_USED))
16096 switch (rc)
16098 case CURSOR_MOVEMENT_SUCCESS:
16099 used_current_matrix_p = 1;
16100 goto done;
16102 case CURSOR_MOVEMENT_MUST_SCROLL:
16103 goto try_to_scroll;
16105 default:
16106 emacs_abort ();
16109 /* If current starting point was originally the beginning of a line
16110 but no longer is, find a new starting point. */
16111 else if (w->start_at_line_beg
16112 && !(CHARPOS (startp) <= BEGV
16113 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n'))
16115 #ifdef GLYPH_DEBUG
16116 debug_method_add (w, "recenter 1");
16117 #endif
16118 goto recenter;
16121 /* Try scrolling with try_window_id. Value is > 0 if update has
16122 been done, it is -1 if we know that the same window start will
16123 not work. It is 0 if unsuccessful for some other reason. */
16124 else if ((tem = try_window_id (w)) != 0)
16126 #ifdef GLYPH_DEBUG
16127 debug_method_add (w, "try_window_id %d", tem);
16128 #endif
16130 if (f->fonts_changed)
16131 goto need_larger_matrices;
16132 if (tem > 0)
16133 goto done;
16135 /* Otherwise try_window_id has returned -1 which means that we
16136 don't want the alternative below this comment to execute. */
16138 else if (CHARPOS (startp) >= BEGV
16139 && CHARPOS (startp) <= ZV
16140 && PT >= CHARPOS (startp)
16141 && (CHARPOS (startp) < ZV
16142 /* Avoid starting at end of buffer. */
16143 || CHARPOS (startp) == BEGV
16144 || !window_outdated (w)))
16146 int d1, d2, d3, d4, d5, d6;
16148 /* If first window line is a continuation line, and window start
16149 is inside the modified region, but the first change is before
16150 current window start, we must select a new window start.
16152 However, if this is the result of a down-mouse event (e.g. by
16153 extending the mouse-drag-overlay), we don't want to select a
16154 new window start, since that would change the position under
16155 the mouse, resulting in an unwanted mouse-movement rather
16156 than a simple mouse-click. */
16157 if (!w->start_at_line_beg
16158 && NILP (do_mouse_tracking)
16159 && CHARPOS (startp) > BEGV
16160 && CHARPOS (startp) > BEG + beg_unchanged
16161 && CHARPOS (startp) <= Z - end_unchanged
16162 /* Even if w->start_at_line_beg is nil, a new window may
16163 start at a line_beg, since that's how set_buffer_window
16164 sets it. So, we need to check the return value of
16165 compute_window_start_on_continuation_line. (See also
16166 bug#197). */
16167 && XMARKER (w->start)->buffer == current_buffer
16168 && compute_window_start_on_continuation_line (w)
16169 /* It doesn't make sense to force the window start like we
16170 do at label force_start if it is already known that point
16171 will not be visible in the resulting window, because
16172 doing so will move point from its correct position
16173 instead of scrolling the window to bring point into view.
16174 See bug#9324. */
16175 && pos_visible_p (w, PT, &d1, &d2, &d3, &d4, &d5, &d6))
16177 w->force_start = 1;
16178 SET_TEXT_POS_FROM_MARKER (startp, w->start);
16179 goto force_start;
16182 #ifdef GLYPH_DEBUG
16183 debug_method_add (w, "same window start");
16184 #endif
16186 /* Try to redisplay starting at same place as before.
16187 If point has not moved off frame, accept the results. */
16188 if (!current_matrix_up_to_date_p
16189 /* Don't use try_window_reusing_current_matrix in this case
16190 because a window scroll function can have changed the
16191 buffer. */
16192 || !NILP (Vwindow_scroll_functions)
16193 || MINI_WINDOW_P (w)
16194 || !(used_current_matrix_p
16195 = try_window_reusing_current_matrix (w)))
16197 IF_DEBUG (debug_method_add (w, "1"));
16198 if (try_window (window, startp, TRY_WINDOW_CHECK_MARGINS) < 0)
16199 /* -1 means we need to scroll.
16200 0 means we need new matrices, but fonts_changed
16201 is set in that case, so we will detect it below. */
16202 goto try_to_scroll;
16205 if (f->fonts_changed)
16206 goto need_larger_matrices;
16208 if (w->cursor.vpos >= 0)
16210 if (!just_this_one_p
16211 || current_buffer->clip_changed
16212 || BEG_UNCHANGED < CHARPOS (startp))
16213 /* Forget any recorded base line for line number display. */
16214 w->base_line_number = 0;
16216 if (!cursor_row_fully_visible_p (w, 1, 0))
16218 clear_glyph_matrix (w->desired_matrix);
16219 last_line_misfit = 1;
16221 /* Drop through and scroll. */
16222 else
16223 goto done;
16225 else
16226 clear_glyph_matrix (w->desired_matrix);
16229 try_to_scroll:
16231 /* Redisplay the mode line. Select the buffer properly for that. */
16232 if (!update_mode_line)
16234 update_mode_line = 1;
16235 w->update_mode_line = 1;
16238 /* Try to scroll by specified few lines. */
16239 if ((scroll_conservatively
16240 || emacs_scroll_step
16241 || temp_scroll_step
16242 || NUMBERP (BVAR (current_buffer, scroll_up_aggressively))
16243 || NUMBERP (BVAR (current_buffer, scroll_down_aggressively)))
16244 && CHARPOS (startp) >= BEGV
16245 && CHARPOS (startp) <= ZV)
16247 /* The function returns -1 if new fonts were loaded, 1 if
16248 successful, 0 if not successful. */
16249 int ss = try_scrolling (window, just_this_one_p,
16250 scroll_conservatively,
16251 emacs_scroll_step,
16252 temp_scroll_step, last_line_misfit);
16253 switch (ss)
16255 case SCROLLING_SUCCESS:
16256 goto done;
16258 case SCROLLING_NEED_LARGER_MATRICES:
16259 goto need_larger_matrices;
16261 case SCROLLING_FAILED:
16262 break;
16264 default:
16265 emacs_abort ();
16269 /* Finally, just choose a place to start which positions point
16270 according to user preferences. */
16272 recenter:
16274 #ifdef GLYPH_DEBUG
16275 debug_method_add (w, "recenter");
16276 #endif
16278 /* Forget any previously recorded base line for line number display. */
16279 if (!buffer_unchanged_p)
16280 w->base_line_number = 0;
16282 /* Determine the window start relative to point. */
16283 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
16284 it.current_y = it.last_visible_y;
16285 if (centering_position < 0)
16287 int window_total_lines
16288 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
16289 int margin =
16290 scroll_margin > 0
16291 ? min (scroll_margin, window_total_lines / 4)
16292 : 0;
16293 ptrdiff_t margin_pos = CHARPOS (startp);
16294 Lisp_Object aggressive;
16295 int scrolling_up;
16297 /* If there is a scroll margin at the top of the window, find
16298 its character position. */
16299 if (margin
16300 /* Cannot call start_display if startp is not in the
16301 accessible region of the buffer. This can happen when we
16302 have just switched to a different buffer and/or changed
16303 its restriction. In that case, startp is initialized to
16304 the character position 1 (BEGV) because we did not yet
16305 have chance to display the buffer even once. */
16306 && BEGV <= CHARPOS (startp) && CHARPOS (startp) <= ZV)
16308 struct it it1;
16309 void *it1data = NULL;
16311 SAVE_IT (it1, it, it1data);
16312 start_display (&it1, w, startp);
16313 move_it_vertically (&it1, margin * frame_line_height);
16314 margin_pos = IT_CHARPOS (it1);
16315 RESTORE_IT (&it, &it, it1data);
16317 scrolling_up = PT > margin_pos;
16318 aggressive =
16319 scrolling_up
16320 ? BVAR (current_buffer, scroll_up_aggressively)
16321 : BVAR (current_buffer, scroll_down_aggressively);
16323 if (!MINI_WINDOW_P (w)
16324 && (scroll_conservatively > SCROLL_LIMIT || NUMBERP (aggressive)))
16326 int pt_offset = 0;
16328 /* Setting scroll-conservatively overrides
16329 scroll-*-aggressively. */
16330 if (!scroll_conservatively && NUMBERP (aggressive))
16332 double float_amount = XFLOATINT (aggressive);
16334 pt_offset = float_amount * WINDOW_BOX_TEXT_HEIGHT (w);
16335 if (pt_offset == 0 && float_amount > 0)
16336 pt_offset = 1;
16337 if (pt_offset && margin > 0)
16338 margin -= 1;
16340 /* Compute how much to move the window start backward from
16341 point so that point will be displayed where the user
16342 wants it. */
16343 if (scrolling_up)
16345 centering_position = it.last_visible_y;
16346 if (pt_offset)
16347 centering_position -= pt_offset;
16348 centering_position -=
16349 frame_line_height * (1 + margin + (last_line_misfit != 0))
16350 + WINDOW_HEADER_LINE_HEIGHT (w);
16351 /* Don't let point enter the scroll margin near top of
16352 the window. */
16353 if (centering_position < margin * frame_line_height)
16354 centering_position = margin * frame_line_height;
16356 else
16357 centering_position = margin * frame_line_height + pt_offset;
16359 else
16360 /* Set the window start half the height of the window backward
16361 from point. */
16362 centering_position = window_box_height (w) / 2;
16364 move_it_vertically_backward (&it, centering_position);
16366 eassert (IT_CHARPOS (it) >= BEGV);
16368 /* The function move_it_vertically_backward may move over more
16369 than the specified y-distance. If it->w is small, e.g. a
16370 mini-buffer window, we may end up in front of the window's
16371 display area. Start displaying at the start of the line
16372 containing PT in this case. */
16373 if (it.current_y <= 0)
16375 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
16376 move_it_vertically_backward (&it, 0);
16377 it.current_y = 0;
16380 it.current_x = it.hpos = 0;
16382 /* Set the window start position here explicitly, to avoid an
16383 infinite loop in case the functions in window-scroll-functions
16384 get errors. */
16385 set_marker_both (w->start, Qnil, IT_CHARPOS (it), IT_BYTEPOS (it));
16387 /* Run scroll hooks. */
16388 startp = run_window_scroll_functions (window, it.current.pos);
16390 /* Redisplay the window. */
16391 if (!current_matrix_up_to_date_p
16392 || windows_or_buffers_changed
16393 || f->cursor_type_changed
16394 /* Don't use try_window_reusing_current_matrix in this case
16395 because it can have changed the buffer. */
16396 || !NILP (Vwindow_scroll_functions)
16397 || !just_this_one_p
16398 || MINI_WINDOW_P (w)
16399 || !(used_current_matrix_p
16400 = try_window_reusing_current_matrix (w)))
16401 try_window (window, startp, 0);
16403 /* If new fonts have been loaded (due to fontsets), give up. We
16404 have to start a new redisplay since we need to re-adjust glyph
16405 matrices. */
16406 if (f->fonts_changed)
16407 goto need_larger_matrices;
16409 /* If cursor did not appear assume that the middle of the window is
16410 in the first line of the window. Do it again with the next line.
16411 (Imagine a window of height 100, displaying two lines of height
16412 60. Moving back 50 from it->last_visible_y will end in the first
16413 line.) */
16414 if (w->cursor.vpos < 0)
16416 if (w->window_end_valid && PT >= Z - w->window_end_pos)
16418 clear_glyph_matrix (w->desired_matrix);
16419 move_it_by_lines (&it, 1);
16420 try_window (window, it.current.pos, 0);
16422 else if (PT < IT_CHARPOS (it))
16424 clear_glyph_matrix (w->desired_matrix);
16425 move_it_by_lines (&it, -1);
16426 try_window (window, it.current.pos, 0);
16428 else
16430 /* Not much we can do about it. */
16434 /* Consider the following case: Window starts at BEGV, there is
16435 invisible, intangible text at BEGV, so that display starts at
16436 some point START > BEGV. It can happen that we are called with
16437 PT somewhere between BEGV and START. Try to handle that case,
16438 and similar ones. */
16439 if (w->cursor.vpos < 0)
16441 /* First, try locating the proper glyph row for PT. */
16442 struct glyph_row *row =
16443 row_containing_pos (w, PT, w->current_matrix->rows, NULL, 0);
16445 /* Sometimes point is at the beginning of invisible text that is
16446 before the 1st character displayed in the row. In that case,
16447 row_containing_pos fails to find the row, because no glyphs
16448 with appropriate buffer positions are present in the row.
16449 Therefore, we next try to find the row which shows the 1st
16450 position after the invisible text. */
16451 if (!row)
16453 Lisp_Object val =
16454 get_char_property_and_overlay (make_number (PT), Qinvisible,
16455 Qnil, NULL);
16457 if (TEXT_PROP_MEANS_INVISIBLE (val))
16459 ptrdiff_t alt_pos;
16460 Lisp_Object invis_end =
16461 Fnext_single_char_property_change (make_number (PT), Qinvisible,
16462 Qnil, Qnil);
16464 if (NATNUMP (invis_end))
16465 alt_pos = XFASTINT (invis_end);
16466 else
16467 alt_pos = ZV;
16468 row = row_containing_pos (w, alt_pos, w->current_matrix->rows,
16469 NULL, 0);
16472 /* Finally, fall back on the first row of the window after the
16473 header line (if any). This is slightly better than not
16474 displaying the cursor at all. */
16475 if (!row)
16477 row = w->current_matrix->rows;
16478 if (row->mode_line_p)
16479 ++row;
16481 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
16484 if (!cursor_row_fully_visible_p (w, 0, 0))
16486 /* If vscroll is enabled, disable it and try again. */
16487 if (w->vscroll)
16489 w->vscroll = 0;
16490 clear_glyph_matrix (w->desired_matrix);
16491 goto recenter;
16494 /* Users who set scroll-conservatively to a large number want
16495 point just above/below the scroll margin. If we ended up
16496 with point's row partially visible, move the window start to
16497 make that row fully visible and out of the margin. */
16498 if (scroll_conservatively > SCROLL_LIMIT)
16500 int window_total_lines
16501 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) * frame_line_height;
16502 int margin =
16503 scroll_margin > 0
16504 ? min (scroll_margin, window_total_lines / 4)
16505 : 0;
16506 int move_down = w->cursor.vpos >= window_total_lines / 2;
16508 move_it_by_lines (&it, move_down ? margin + 1 : -(margin + 1));
16509 clear_glyph_matrix (w->desired_matrix);
16510 if (1 == try_window (window, it.current.pos,
16511 TRY_WINDOW_CHECK_MARGINS))
16512 goto done;
16515 /* If centering point failed to make the whole line visible,
16516 put point at the top instead. That has to make the whole line
16517 visible, if it can be done. */
16518 if (centering_position == 0)
16519 goto done;
16521 clear_glyph_matrix (w->desired_matrix);
16522 centering_position = 0;
16523 goto recenter;
16526 done:
16528 SET_TEXT_POS_FROM_MARKER (startp, w->start);
16529 w->start_at_line_beg = (CHARPOS (startp) == BEGV
16530 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n');
16532 /* Display the mode line, if we must. */
16533 if ((update_mode_line
16534 /* If window not full width, must redo its mode line
16535 if (a) the window to its side is being redone and
16536 (b) we do a frame-based redisplay. This is a consequence
16537 of how inverted lines are drawn in frame-based redisplay. */
16538 || (!just_this_one_p
16539 && !FRAME_WINDOW_P (f)
16540 && !WINDOW_FULL_WIDTH_P (w))
16541 /* Line number to display. */
16542 || w->base_line_pos > 0
16543 /* Column number is displayed and different from the one displayed. */
16544 || (w->column_number_displayed != -1
16545 && (w->column_number_displayed != current_column ())))
16546 /* This means that the window has a mode line. */
16547 && (WINDOW_WANTS_MODELINE_P (w)
16548 || WINDOW_WANTS_HEADER_LINE_P (w)))
16551 display_mode_lines (w);
16553 /* If mode line height has changed, arrange for a thorough
16554 immediate redisplay using the correct mode line height. */
16555 if (WINDOW_WANTS_MODELINE_P (w)
16556 && CURRENT_MODE_LINE_HEIGHT (w) != DESIRED_MODE_LINE_HEIGHT (w))
16558 f->fonts_changed = 1;
16559 w->mode_line_height = -1;
16560 MATRIX_MODE_LINE_ROW (w->current_matrix)->height
16561 = DESIRED_MODE_LINE_HEIGHT (w);
16564 /* If header line height has changed, arrange for a thorough
16565 immediate redisplay using the correct header line height. */
16566 if (WINDOW_WANTS_HEADER_LINE_P (w)
16567 && CURRENT_HEADER_LINE_HEIGHT (w) != DESIRED_HEADER_LINE_HEIGHT (w))
16569 f->fonts_changed = 1;
16570 w->header_line_height = -1;
16571 MATRIX_HEADER_LINE_ROW (w->current_matrix)->height
16572 = DESIRED_HEADER_LINE_HEIGHT (w);
16575 if (f->fonts_changed)
16576 goto need_larger_matrices;
16579 if (!line_number_displayed && w->base_line_pos != -1)
16581 w->base_line_pos = 0;
16582 w->base_line_number = 0;
16585 finish_menu_bars:
16587 /* When we reach a frame's selected window, redo the frame's menu bar. */
16588 if (update_mode_line
16589 && EQ (FRAME_SELECTED_WINDOW (f), window))
16591 int redisplay_menu_p = 0;
16593 if (FRAME_WINDOW_P (f))
16595 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
16596 || defined (HAVE_NS) || defined (USE_GTK)
16597 redisplay_menu_p = FRAME_EXTERNAL_MENU_BAR (f);
16598 #else
16599 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
16600 #endif
16602 else
16603 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
16605 if (redisplay_menu_p)
16606 display_menu_bar (w);
16608 #ifdef HAVE_WINDOW_SYSTEM
16609 if (FRAME_WINDOW_P (f))
16611 #if defined (USE_GTK) || defined (HAVE_NS)
16612 if (FRAME_EXTERNAL_TOOL_BAR (f))
16613 redisplay_tool_bar (f);
16614 #else
16615 if (WINDOWP (f->tool_bar_window)
16616 && (FRAME_TOOL_BAR_HEIGHT (f) > 0
16617 || !NILP (Vauto_resize_tool_bars))
16618 && redisplay_tool_bar (f))
16619 ignore_mouse_drag_p = 1;
16620 #endif
16622 #endif
16625 #ifdef HAVE_WINDOW_SYSTEM
16626 if (FRAME_WINDOW_P (f)
16627 && update_window_fringes (w, (just_this_one_p
16628 || (!used_current_matrix_p && !overlay_arrow_seen)
16629 || w->pseudo_window_p)))
16631 update_begin (f);
16632 block_input ();
16633 if (draw_window_fringes (w, 1))
16635 if (WINDOW_RIGHT_DIVIDER_WIDTH (w))
16636 x_draw_right_divider (w);
16637 else
16638 x_draw_vertical_border (w);
16640 unblock_input ();
16641 update_end (f);
16644 if (WINDOW_BOTTOM_DIVIDER_WIDTH (w))
16645 x_draw_bottom_divider (w);
16646 #endif /* HAVE_WINDOW_SYSTEM */
16648 /* We go to this label, with fonts_changed set, if it is
16649 necessary to try again using larger glyph matrices.
16650 We have to redeem the scroll bar even in this case,
16651 because the loop in redisplay_internal expects that. */
16652 need_larger_matrices:
16654 finish_scroll_bars:
16656 if (WINDOW_HAS_VERTICAL_SCROLL_BAR (w))
16658 /* Set the thumb's position and size. */
16659 set_vertical_scroll_bar (w);
16661 /* Note that we actually used the scroll bar attached to this
16662 window, so it shouldn't be deleted at the end of redisplay. */
16663 if (FRAME_TERMINAL (f)->redeem_scroll_bar_hook)
16664 (*FRAME_TERMINAL (f)->redeem_scroll_bar_hook) (w);
16667 /* Restore current_buffer and value of point in it. The window
16668 update may have changed the buffer, so first make sure `opoint'
16669 is still valid (Bug#6177). */
16670 if (CHARPOS (opoint) < BEGV)
16671 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
16672 else if (CHARPOS (opoint) > ZV)
16673 TEMP_SET_PT_BOTH (Z, Z_BYTE);
16674 else
16675 TEMP_SET_PT_BOTH (CHARPOS (opoint), BYTEPOS (opoint));
16677 set_buffer_internal_1 (old);
16678 /* Avoid an abort in TEMP_SET_PT_BOTH if the buffer has become
16679 shorter. This can be caused by log truncation in *Messages*. */
16680 if (CHARPOS (lpoint) <= ZV)
16681 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
16683 unbind_to (count, Qnil);
16687 /* Build the complete desired matrix of WINDOW with a window start
16688 buffer position POS.
16690 Value is 1 if successful. It is zero if fonts were loaded during
16691 redisplay which makes re-adjusting glyph matrices necessary, and -1
16692 if point would appear in the scroll margins.
16693 (We check the former only if TRY_WINDOW_IGNORE_FONTS_CHANGE is
16694 unset in FLAGS, and the latter only if TRY_WINDOW_CHECK_MARGINS is
16695 set in FLAGS.) */
16698 try_window (Lisp_Object window, struct text_pos pos, int flags)
16700 struct window *w = XWINDOW (window);
16701 struct it it;
16702 struct glyph_row *last_text_row = NULL;
16703 struct frame *f = XFRAME (w->frame);
16704 int frame_line_height = default_line_pixel_height (w);
16706 /* Make POS the new window start. */
16707 set_marker_both (w->start, Qnil, CHARPOS (pos), BYTEPOS (pos));
16709 /* Mark cursor position as unknown. No overlay arrow seen. */
16710 w->cursor.vpos = -1;
16711 overlay_arrow_seen = 0;
16713 /* Initialize iterator and info to start at POS. */
16714 start_display (&it, w, pos);
16716 /* Display all lines of W. */
16717 while (it.current_y < it.last_visible_y)
16719 if (display_line (&it))
16720 last_text_row = it.glyph_row - 1;
16721 if (f->fonts_changed && !(flags & TRY_WINDOW_IGNORE_FONTS_CHANGE))
16722 return 0;
16725 /* Don't let the cursor end in the scroll margins. */
16726 if ((flags & TRY_WINDOW_CHECK_MARGINS)
16727 && !MINI_WINDOW_P (w))
16729 int this_scroll_margin;
16730 int window_total_lines
16731 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
16733 if (scroll_margin > 0)
16735 this_scroll_margin = min (scroll_margin, window_total_lines / 4);
16736 this_scroll_margin *= frame_line_height;
16738 else
16739 this_scroll_margin = 0;
16741 if ((w->cursor.y >= 0 /* not vscrolled */
16742 && w->cursor.y < this_scroll_margin
16743 && CHARPOS (pos) > BEGV
16744 && IT_CHARPOS (it) < ZV)
16745 /* rms: considering make_cursor_line_fully_visible_p here
16746 seems to give wrong results. We don't want to recenter
16747 when the last line is partly visible, we want to allow
16748 that case to be handled in the usual way. */
16749 || w->cursor.y > it.last_visible_y - this_scroll_margin - 1)
16751 w->cursor.vpos = -1;
16752 clear_glyph_matrix (w->desired_matrix);
16753 return -1;
16757 /* If bottom moved off end of frame, change mode line percentage. */
16758 if (w->window_end_pos <= 0 && Z != IT_CHARPOS (it))
16759 w->update_mode_line = 1;
16761 /* Set window_end_pos to the offset of the last character displayed
16762 on the window from the end of current_buffer. Set
16763 window_end_vpos to its row number. */
16764 if (last_text_row)
16766 eassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_text_row));
16767 adjust_window_ends (w, last_text_row, 0);
16768 eassert
16769 (MATRIX_ROW_DISPLAYS_TEXT_P (MATRIX_ROW (w->desired_matrix,
16770 w->window_end_vpos)));
16772 else
16774 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
16775 w->window_end_pos = Z - ZV;
16776 w->window_end_vpos = 0;
16779 /* But that is not valid info until redisplay finishes. */
16780 w->window_end_valid = 0;
16781 return 1;
16786 /************************************************************************
16787 Window redisplay reusing current matrix when buffer has not changed
16788 ************************************************************************/
16790 /* Try redisplay of window W showing an unchanged buffer with a
16791 different window start than the last time it was displayed by
16792 reusing its current matrix. Value is non-zero if successful.
16793 W->start is the new window start. */
16795 static int
16796 try_window_reusing_current_matrix (struct window *w)
16798 struct frame *f = XFRAME (w->frame);
16799 struct glyph_row *bottom_row;
16800 struct it it;
16801 struct run run;
16802 struct text_pos start, new_start;
16803 int nrows_scrolled, i;
16804 struct glyph_row *last_text_row;
16805 struct glyph_row *last_reused_text_row;
16806 struct glyph_row *start_row;
16807 int start_vpos, min_y, max_y;
16809 #ifdef GLYPH_DEBUG
16810 if (inhibit_try_window_reusing)
16811 return 0;
16812 #endif
16814 if (/* This function doesn't handle terminal frames. */
16815 !FRAME_WINDOW_P (f)
16816 /* Don't try to reuse the display if windows have been split
16817 or such. */
16818 || windows_or_buffers_changed
16819 || f->cursor_type_changed)
16820 return 0;
16822 /* Can't do this if showing trailing whitespace. */
16823 if (!NILP (Vshow_trailing_whitespace))
16824 return 0;
16826 /* If top-line visibility has changed, give up. */
16827 if (WINDOW_WANTS_HEADER_LINE_P (w)
16828 != MATRIX_HEADER_LINE_ROW (w->current_matrix)->mode_line_p)
16829 return 0;
16831 /* Give up if old or new display is scrolled vertically. We could
16832 make this function handle this, but right now it doesn't. */
16833 start_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
16834 if (w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row))
16835 return 0;
16837 /* The variable new_start now holds the new window start. The old
16838 start `start' can be determined from the current matrix. */
16839 SET_TEXT_POS_FROM_MARKER (new_start, w->start);
16840 start = start_row->minpos;
16841 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
16843 /* Clear the desired matrix for the display below. */
16844 clear_glyph_matrix (w->desired_matrix);
16846 if (CHARPOS (new_start) <= CHARPOS (start))
16848 /* Don't use this method if the display starts with an ellipsis
16849 displayed for invisible text. It's not easy to handle that case
16850 below, and it's certainly not worth the effort since this is
16851 not a frequent case. */
16852 if (in_ellipses_for_invisible_text_p (&start_row->start, w))
16853 return 0;
16855 IF_DEBUG (debug_method_add (w, "twu1"));
16857 /* Display up to a row that can be reused. The variable
16858 last_text_row is set to the last row displayed that displays
16859 text. Note that it.vpos == 0 if or if not there is a
16860 header-line; it's not the same as the MATRIX_ROW_VPOS! */
16861 start_display (&it, w, new_start);
16862 w->cursor.vpos = -1;
16863 last_text_row = last_reused_text_row = NULL;
16865 while (it.current_y < it.last_visible_y && !f->fonts_changed)
16867 /* If we have reached into the characters in the START row,
16868 that means the line boundaries have changed. So we
16869 can't start copying with the row START. Maybe it will
16870 work to start copying with the following row. */
16871 while (IT_CHARPOS (it) > CHARPOS (start))
16873 /* Advance to the next row as the "start". */
16874 start_row++;
16875 start = start_row->minpos;
16876 /* If there are no more rows to try, or just one, give up. */
16877 if (start_row == MATRIX_MODE_LINE_ROW (w->current_matrix) - 1
16878 || w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row)
16879 || CHARPOS (start) == ZV)
16881 clear_glyph_matrix (w->desired_matrix);
16882 return 0;
16885 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
16887 /* If we have reached alignment, we can copy the rest of the
16888 rows. */
16889 if (IT_CHARPOS (it) == CHARPOS (start)
16890 /* Don't accept "alignment" inside a display vector,
16891 since start_row could have started in the middle of
16892 that same display vector (thus their character
16893 positions match), and we have no way of telling if
16894 that is the case. */
16895 && it.current.dpvec_index < 0)
16896 break;
16898 if (display_line (&it))
16899 last_text_row = it.glyph_row - 1;
16903 /* A value of current_y < last_visible_y means that we stopped
16904 at the previous window start, which in turn means that we
16905 have at least one reusable row. */
16906 if (it.current_y < it.last_visible_y)
16908 struct glyph_row *row;
16910 /* IT.vpos always starts from 0; it counts text lines. */
16911 nrows_scrolled = it.vpos - (start_row - MATRIX_FIRST_TEXT_ROW (w->current_matrix));
16913 /* Find PT if not already found in the lines displayed. */
16914 if (w->cursor.vpos < 0)
16916 int dy = it.current_y - start_row->y;
16918 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
16919 row = row_containing_pos (w, PT, row, NULL, dy);
16920 if (row)
16921 set_cursor_from_row (w, row, w->current_matrix, 0, 0,
16922 dy, nrows_scrolled);
16923 else
16925 clear_glyph_matrix (w->desired_matrix);
16926 return 0;
16930 /* Scroll the display. Do it before the current matrix is
16931 changed. The problem here is that update has not yet
16932 run, i.e. part of the current matrix is not up to date.
16933 scroll_run_hook will clear the cursor, and use the
16934 current matrix to get the height of the row the cursor is
16935 in. */
16936 run.current_y = start_row->y;
16937 run.desired_y = it.current_y;
16938 run.height = it.last_visible_y - it.current_y;
16940 if (run.height > 0 && run.current_y != run.desired_y)
16942 update_begin (f);
16943 FRAME_RIF (f)->update_window_begin_hook (w);
16944 FRAME_RIF (f)->clear_window_mouse_face (w);
16945 FRAME_RIF (f)->scroll_run_hook (w, &run);
16946 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
16947 update_end (f);
16950 /* Shift current matrix down by nrows_scrolled lines. */
16951 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
16952 rotate_matrix (w->current_matrix,
16953 start_vpos,
16954 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
16955 nrows_scrolled);
16957 /* Disable lines that must be updated. */
16958 for (i = 0; i < nrows_scrolled; ++i)
16959 (start_row + i)->enabled_p = false;
16961 /* Re-compute Y positions. */
16962 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
16963 max_y = it.last_visible_y;
16964 for (row = start_row + nrows_scrolled;
16965 row < bottom_row;
16966 ++row)
16968 row->y = it.current_y;
16969 row->visible_height = row->height;
16971 if (row->y < min_y)
16972 row->visible_height -= min_y - row->y;
16973 if (row->y + row->height > max_y)
16974 row->visible_height -= row->y + row->height - max_y;
16975 if (row->fringe_bitmap_periodic_p)
16976 row->redraw_fringe_bitmaps_p = 1;
16978 it.current_y += row->height;
16980 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
16981 last_reused_text_row = row;
16982 if (MATRIX_ROW_BOTTOM_Y (row) >= it.last_visible_y)
16983 break;
16986 /* Disable lines in the current matrix which are now
16987 below the window. */
16988 for (++row; row < bottom_row; ++row)
16989 row->enabled_p = row->mode_line_p = 0;
16992 /* Update window_end_pos etc.; last_reused_text_row is the last
16993 reused row from the current matrix containing text, if any.
16994 The value of last_text_row is the last displayed line
16995 containing text. */
16996 if (last_reused_text_row)
16997 adjust_window_ends (w, last_reused_text_row, 1);
16998 else if (last_text_row)
16999 adjust_window_ends (w, last_text_row, 0);
17000 else
17002 /* This window must be completely empty. */
17003 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
17004 w->window_end_pos = Z - ZV;
17005 w->window_end_vpos = 0;
17007 w->window_end_valid = 0;
17009 /* Update hint: don't try scrolling again in update_window. */
17010 w->desired_matrix->no_scrolling_p = 1;
17012 #ifdef GLYPH_DEBUG
17013 debug_method_add (w, "try_window_reusing_current_matrix 1");
17014 #endif
17015 return 1;
17017 else if (CHARPOS (new_start) > CHARPOS (start))
17019 struct glyph_row *pt_row, *row;
17020 struct glyph_row *first_reusable_row;
17021 struct glyph_row *first_row_to_display;
17022 int dy;
17023 int yb = window_text_bottom_y (w);
17025 /* Find the row starting at new_start, if there is one. Don't
17026 reuse a partially visible line at the end. */
17027 first_reusable_row = start_row;
17028 while (first_reusable_row->enabled_p
17029 && MATRIX_ROW_BOTTOM_Y (first_reusable_row) < yb
17030 && (MATRIX_ROW_START_CHARPOS (first_reusable_row)
17031 < CHARPOS (new_start)))
17032 ++first_reusable_row;
17034 /* Give up if there is no row to reuse. */
17035 if (MATRIX_ROW_BOTTOM_Y (first_reusable_row) >= yb
17036 || !first_reusable_row->enabled_p
17037 || (MATRIX_ROW_START_CHARPOS (first_reusable_row)
17038 != CHARPOS (new_start)))
17039 return 0;
17041 /* We can reuse fully visible rows beginning with
17042 first_reusable_row to the end of the window. Set
17043 first_row_to_display to the first row that cannot be reused.
17044 Set pt_row to the row containing point, if there is any. */
17045 pt_row = NULL;
17046 for (first_row_to_display = first_reusable_row;
17047 MATRIX_ROW_BOTTOM_Y (first_row_to_display) < yb;
17048 ++first_row_to_display)
17050 if (PT >= MATRIX_ROW_START_CHARPOS (first_row_to_display)
17051 && (PT < MATRIX_ROW_END_CHARPOS (first_row_to_display)
17052 || (PT == MATRIX_ROW_END_CHARPOS (first_row_to_display)
17053 && first_row_to_display->ends_at_zv_p
17054 && pt_row == NULL)))
17055 pt_row = first_row_to_display;
17058 /* Start displaying at the start of first_row_to_display. */
17059 eassert (first_row_to_display->y < yb);
17060 init_to_row_start (&it, w, first_row_to_display);
17062 nrows_scrolled = (MATRIX_ROW_VPOS (first_reusable_row, w->current_matrix)
17063 - start_vpos);
17064 it.vpos = (MATRIX_ROW_VPOS (first_row_to_display, w->current_matrix)
17065 - nrows_scrolled);
17066 it.current_y = (first_row_to_display->y - first_reusable_row->y
17067 + WINDOW_HEADER_LINE_HEIGHT (w));
17069 /* Display lines beginning with first_row_to_display in the
17070 desired matrix. Set last_text_row to the last row displayed
17071 that displays text. */
17072 it.glyph_row = MATRIX_ROW (w->desired_matrix, it.vpos);
17073 if (pt_row == NULL)
17074 w->cursor.vpos = -1;
17075 last_text_row = NULL;
17076 while (it.current_y < it.last_visible_y && !f->fonts_changed)
17077 if (display_line (&it))
17078 last_text_row = it.glyph_row - 1;
17080 /* If point is in a reused row, adjust y and vpos of the cursor
17081 position. */
17082 if (pt_row)
17084 w->cursor.vpos -= nrows_scrolled;
17085 w->cursor.y -= first_reusable_row->y - start_row->y;
17088 /* Give up if point isn't in a row displayed or reused. (This
17089 also handles the case where w->cursor.vpos < nrows_scrolled
17090 after the calls to display_line, which can happen with scroll
17091 margins. See bug#1295.) */
17092 if (w->cursor.vpos < 0)
17094 clear_glyph_matrix (w->desired_matrix);
17095 return 0;
17098 /* Scroll the display. */
17099 run.current_y = first_reusable_row->y;
17100 run.desired_y = WINDOW_HEADER_LINE_HEIGHT (w);
17101 run.height = it.last_visible_y - run.current_y;
17102 dy = run.current_y - run.desired_y;
17104 if (run.height)
17106 update_begin (f);
17107 FRAME_RIF (f)->update_window_begin_hook (w);
17108 FRAME_RIF (f)->clear_window_mouse_face (w);
17109 FRAME_RIF (f)->scroll_run_hook (w, &run);
17110 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
17111 update_end (f);
17114 /* Adjust Y positions of reused rows. */
17115 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
17116 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
17117 max_y = it.last_visible_y;
17118 for (row = first_reusable_row; row < first_row_to_display; ++row)
17120 row->y -= dy;
17121 row->visible_height = row->height;
17122 if (row->y < min_y)
17123 row->visible_height -= min_y - row->y;
17124 if (row->y + row->height > max_y)
17125 row->visible_height -= row->y + row->height - max_y;
17126 if (row->fringe_bitmap_periodic_p)
17127 row->redraw_fringe_bitmaps_p = 1;
17130 /* Scroll the current matrix. */
17131 eassert (nrows_scrolled > 0);
17132 rotate_matrix (w->current_matrix,
17133 start_vpos,
17134 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
17135 -nrows_scrolled);
17137 /* Disable rows not reused. */
17138 for (row -= nrows_scrolled; row < bottom_row; ++row)
17139 row->enabled_p = false;
17141 /* Point may have moved to a different line, so we cannot assume that
17142 the previous cursor position is valid; locate the correct row. */
17143 if (pt_row)
17145 for (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
17146 row < bottom_row
17147 && PT >= MATRIX_ROW_END_CHARPOS (row)
17148 && !row->ends_at_zv_p;
17149 row++)
17151 w->cursor.vpos++;
17152 w->cursor.y = row->y;
17154 if (row < bottom_row)
17156 /* Can't simply scan the row for point with
17157 bidi-reordered glyph rows. Let set_cursor_from_row
17158 figure out where to put the cursor, and if it fails,
17159 give up. */
17160 if (!NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering)))
17162 if (!set_cursor_from_row (w, row, w->current_matrix,
17163 0, 0, 0, 0))
17165 clear_glyph_matrix (w->desired_matrix);
17166 return 0;
17169 else
17171 struct glyph *glyph = row->glyphs[TEXT_AREA] + w->cursor.hpos;
17172 struct glyph *end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
17174 for (; glyph < end
17175 && (!BUFFERP (glyph->object)
17176 || glyph->charpos < PT);
17177 glyph++)
17179 w->cursor.hpos++;
17180 w->cursor.x += glyph->pixel_width;
17186 /* Adjust window end. A null value of last_text_row means that
17187 the window end is in reused rows which in turn means that
17188 only its vpos can have changed. */
17189 if (last_text_row)
17190 adjust_window_ends (w, last_text_row, 0);
17191 else
17192 w->window_end_vpos -= nrows_scrolled;
17194 w->window_end_valid = 0;
17195 w->desired_matrix->no_scrolling_p = 1;
17197 #ifdef GLYPH_DEBUG
17198 debug_method_add (w, "try_window_reusing_current_matrix 2");
17199 #endif
17200 return 1;
17203 return 0;
17208 /************************************************************************
17209 Window redisplay reusing current matrix when buffer has changed
17210 ************************************************************************/
17212 static struct glyph_row *find_last_unchanged_at_beg_row (struct window *);
17213 static struct glyph_row *find_first_unchanged_at_end_row (struct window *,
17214 ptrdiff_t *, ptrdiff_t *);
17215 static struct glyph_row *
17216 find_last_row_displaying_text (struct glyph_matrix *, struct it *,
17217 struct glyph_row *);
17220 /* Return the last row in MATRIX displaying text. If row START is
17221 non-null, start searching with that row. IT gives the dimensions
17222 of the display. Value is null if matrix is empty; otherwise it is
17223 a pointer to the row found. */
17225 static struct glyph_row *
17226 find_last_row_displaying_text (struct glyph_matrix *matrix, struct it *it,
17227 struct glyph_row *start)
17229 struct glyph_row *row, *row_found;
17231 /* Set row_found to the last row in IT->w's current matrix
17232 displaying text. The loop looks funny but think of partially
17233 visible lines. */
17234 row_found = NULL;
17235 row = start ? start : MATRIX_FIRST_TEXT_ROW (matrix);
17236 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
17238 eassert (row->enabled_p);
17239 row_found = row;
17240 if (MATRIX_ROW_BOTTOM_Y (row) >= it->last_visible_y)
17241 break;
17242 ++row;
17245 return row_found;
17249 /* Return the last row in the current matrix of W that is not affected
17250 by changes at the start of current_buffer that occurred since W's
17251 current matrix was built. Value is null if no such row exists.
17253 BEG_UNCHANGED us the number of characters unchanged at the start of
17254 current_buffer. BEG + BEG_UNCHANGED is the buffer position of the
17255 first changed character in current_buffer. Characters at positions <
17256 BEG + BEG_UNCHANGED are at the same buffer positions as they were
17257 when the current matrix was built. */
17259 static struct glyph_row *
17260 find_last_unchanged_at_beg_row (struct window *w)
17262 ptrdiff_t first_changed_pos = BEG + BEG_UNCHANGED;
17263 struct glyph_row *row;
17264 struct glyph_row *row_found = NULL;
17265 int yb = window_text_bottom_y (w);
17267 /* Find the last row displaying unchanged text. */
17268 for (row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
17269 MATRIX_ROW_DISPLAYS_TEXT_P (row)
17270 && MATRIX_ROW_START_CHARPOS (row) < first_changed_pos;
17271 ++row)
17273 if (/* If row ends before first_changed_pos, it is unchanged,
17274 except in some case. */
17275 MATRIX_ROW_END_CHARPOS (row) <= first_changed_pos
17276 /* When row ends in ZV and we write at ZV it is not
17277 unchanged. */
17278 && !row->ends_at_zv_p
17279 /* When first_changed_pos is the end of a continued line,
17280 row is not unchanged because it may be no longer
17281 continued. */
17282 && !(MATRIX_ROW_END_CHARPOS (row) == first_changed_pos
17283 && (row->continued_p
17284 || row->exact_window_width_line_p))
17285 /* If ROW->end is beyond ZV, then ROW->end is outdated and
17286 needs to be recomputed, so don't consider this row as
17287 unchanged. This happens when the last line was
17288 bidi-reordered and was killed immediately before this
17289 redisplay cycle. In that case, ROW->end stores the
17290 buffer position of the first visual-order character of
17291 the killed text, which is now beyond ZV. */
17292 && CHARPOS (row->end.pos) <= ZV)
17293 row_found = row;
17295 /* Stop if last visible row. */
17296 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
17297 break;
17300 return row_found;
17304 /* Find the first glyph row in the current matrix of W that is not
17305 affected by changes at the end of current_buffer since the
17306 time W's current matrix was built.
17308 Return in *DELTA the number of chars by which buffer positions in
17309 unchanged text at the end of current_buffer must be adjusted.
17311 Return in *DELTA_BYTES the corresponding number of bytes.
17313 Value is null if no such row exists, i.e. all rows are affected by
17314 changes. */
17316 static struct glyph_row *
17317 find_first_unchanged_at_end_row (struct window *w,
17318 ptrdiff_t *delta, ptrdiff_t *delta_bytes)
17320 struct glyph_row *row;
17321 struct glyph_row *row_found = NULL;
17323 *delta = *delta_bytes = 0;
17325 /* Display must not have been paused, otherwise the current matrix
17326 is not up to date. */
17327 eassert (w->window_end_valid);
17329 /* A value of window_end_pos >= END_UNCHANGED means that the window
17330 end is in the range of changed text. If so, there is no
17331 unchanged row at the end of W's current matrix. */
17332 if (w->window_end_pos >= END_UNCHANGED)
17333 return NULL;
17335 /* Set row to the last row in W's current matrix displaying text. */
17336 row = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
17338 /* If matrix is entirely empty, no unchanged row exists. */
17339 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
17341 /* The value of row is the last glyph row in the matrix having a
17342 meaningful buffer position in it. The end position of row
17343 corresponds to window_end_pos. This allows us to translate
17344 buffer positions in the current matrix to current buffer
17345 positions for characters not in changed text. */
17346 ptrdiff_t Z_old =
17347 MATRIX_ROW_END_CHARPOS (row) + w->window_end_pos;
17348 ptrdiff_t Z_BYTE_old =
17349 MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
17350 ptrdiff_t last_unchanged_pos, last_unchanged_pos_old;
17351 struct glyph_row *first_text_row
17352 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
17354 *delta = Z - Z_old;
17355 *delta_bytes = Z_BYTE - Z_BYTE_old;
17357 /* Set last_unchanged_pos to the buffer position of the last
17358 character in the buffer that has not been changed. Z is the
17359 index + 1 of the last character in current_buffer, i.e. by
17360 subtracting END_UNCHANGED we get the index of the last
17361 unchanged character, and we have to add BEG to get its buffer
17362 position. */
17363 last_unchanged_pos = Z - END_UNCHANGED + BEG;
17364 last_unchanged_pos_old = last_unchanged_pos - *delta;
17366 /* Search backward from ROW for a row displaying a line that
17367 starts at a minimum position >= last_unchanged_pos_old. */
17368 for (; row > first_text_row; --row)
17370 /* This used to abort, but it can happen.
17371 It is ok to just stop the search instead here. KFS. */
17372 if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
17373 break;
17375 if (MATRIX_ROW_START_CHARPOS (row) >= last_unchanged_pos_old)
17376 row_found = row;
17380 eassert (!row_found || MATRIX_ROW_DISPLAYS_TEXT_P (row_found));
17382 return row_found;
17386 /* Make sure that glyph rows in the current matrix of window W
17387 reference the same glyph memory as corresponding rows in the
17388 frame's frame matrix. This function is called after scrolling W's
17389 current matrix on a terminal frame in try_window_id and
17390 try_window_reusing_current_matrix. */
17392 static void
17393 sync_frame_with_window_matrix_rows (struct window *w)
17395 struct frame *f = XFRAME (w->frame);
17396 struct glyph_row *window_row, *window_row_end, *frame_row;
17398 /* Preconditions: W must be a leaf window and full-width. Its frame
17399 must have a frame matrix. */
17400 eassert (BUFFERP (w->contents));
17401 eassert (WINDOW_FULL_WIDTH_P (w));
17402 eassert (!FRAME_WINDOW_P (f));
17404 /* If W is a full-width window, glyph pointers in W's current matrix
17405 have, by definition, to be the same as glyph pointers in the
17406 corresponding frame matrix. Note that frame matrices have no
17407 marginal areas (see build_frame_matrix). */
17408 window_row = w->current_matrix->rows;
17409 window_row_end = window_row + w->current_matrix->nrows;
17410 frame_row = f->current_matrix->rows + WINDOW_TOP_EDGE_LINE (w);
17411 while (window_row < window_row_end)
17413 struct glyph *start = window_row->glyphs[LEFT_MARGIN_AREA];
17414 struct glyph *end = window_row->glyphs[LAST_AREA];
17416 frame_row->glyphs[LEFT_MARGIN_AREA] = start;
17417 frame_row->glyphs[TEXT_AREA] = start;
17418 frame_row->glyphs[RIGHT_MARGIN_AREA] = end;
17419 frame_row->glyphs[LAST_AREA] = end;
17421 /* Disable frame rows whose corresponding window rows have
17422 been disabled in try_window_id. */
17423 if (!window_row->enabled_p)
17424 frame_row->enabled_p = false;
17426 ++window_row, ++frame_row;
17431 /* Find the glyph row in window W containing CHARPOS. Consider all
17432 rows between START and END (not inclusive). END null means search
17433 all rows to the end of the display area of W. Value is the row
17434 containing CHARPOS or null. */
17436 struct glyph_row *
17437 row_containing_pos (struct window *w, ptrdiff_t charpos,
17438 struct glyph_row *start, struct glyph_row *end, int dy)
17440 struct glyph_row *row = start;
17441 struct glyph_row *best_row = NULL;
17442 ptrdiff_t mindif = BUF_ZV (XBUFFER (w->contents)) + 1;
17443 int last_y;
17445 /* If we happen to start on a header-line, skip that. */
17446 if (row->mode_line_p)
17447 ++row;
17449 if ((end && row >= end) || !row->enabled_p)
17450 return NULL;
17452 last_y = window_text_bottom_y (w) - dy;
17454 while (1)
17456 /* Give up if we have gone too far. */
17457 if (end && row >= end)
17458 return NULL;
17459 /* This formerly returned if they were equal.
17460 I think that both quantities are of a "last plus one" type;
17461 if so, when they are equal, the row is within the screen. -- rms. */
17462 if (MATRIX_ROW_BOTTOM_Y (row) > last_y)
17463 return NULL;
17465 /* If it is in this row, return this row. */
17466 if (! (MATRIX_ROW_END_CHARPOS (row) < charpos
17467 || (MATRIX_ROW_END_CHARPOS (row) == charpos
17468 /* The end position of a row equals the start
17469 position of the next row. If CHARPOS is there, we
17470 would rather consider it displayed in the next
17471 line, except when this line ends in ZV. */
17472 && !row_for_charpos_p (row, charpos)))
17473 && charpos >= MATRIX_ROW_START_CHARPOS (row))
17475 struct glyph *g;
17477 if (NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering))
17478 || (!best_row && !row->continued_p))
17479 return row;
17480 /* In bidi-reordered rows, there could be several rows whose
17481 edges surround CHARPOS, all of these rows belonging to
17482 the same continued line. We need to find the row which
17483 fits CHARPOS the best. */
17484 for (g = row->glyphs[TEXT_AREA];
17485 g < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
17486 g++)
17488 if (!STRINGP (g->object))
17490 if (g->charpos > 0 && eabs (g->charpos - charpos) < mindif)
17492 mindif = eabs (g->charpos - charpos);
17493 best_row = row;
17494 /* Exact match always wins. */
17495 if (mindif == 0)
17496 return best_row;
17501 else if (best_row && !row->continued_p)
17502 return best_row;
17503 ++row;
17508 /* Try to redisplay window W by reusing its existing display. W's
17509 current matrix must be up to date when this function is called,
17510 i.e. window_end_valid must be nonzero.
17512 Value is
17514 >= 1 if successful, i.e. display has been updated
17515 specifically:
17516 1 means the changes were in front of a newline that precedes
17517 the window start, and the whole current matrix was reused
17518 2 means the changes were after the last position displayed
17519 in the window, and the whole current matrix was reused
17520 3 means portions of the current matrix were reused, while
17521 some of the screen lines were redrawn
17522 -1 if redisplay with same window start is known not to succeed
17523 0 if otherwise unsuccessful
17525 The following steps are performed:
17527 1. Find the last row in the current matrix of W that is not
17528 affected by changes at the start of current_buffer. If no such row
17529 is found, give up.
17531 2. Find the first row in W's current matrix that is not affected by
17532 changes at the end of current_buffer. Maybe there is no such row.
17534 3. Display lines beginning with the row + 1 found in step 1 to the
17535 row found in step 2 or, if step 2 didn't find a row, to the end of
17536 the window.
17538 4. If cursor is not known to appear on the window, give up.
17540 5. If display stopped at the row found in step 2, scroll the
17541 display and current matrix as needed.
17543 6. Maybe display some lines at the end of W, if we must. This can
17544 happen under various circumstances, like a partially visible line
17545 becoming fully visible, or because newly displayed lines are displayed
17546 in smaller font sizes.
17548 7. Update W's window end information. */
17550 static int
17551 try_window_id (struct window *w)
17553 struct frame *f = XFRAME (w->frame);
17554 struct glyph_matrix *current_matrix = w->current_matrix;
17555 struct glyph_matrix *desired_matrix = w->desired_matrix;
17556 struct glyph_row *last_unchanged_at_beg_row;
17557 struct glyph_row *first_unchanged_at_end_row;
17558 struct glyph_row *row;
17559 struct glyph_row *bottom_row;
17560 int bottom_vpos;
17561 struct it it;
17562 ptrdiff_t delta = 0, delta_bytes = 0, stop_pos;
17563 int dvpos, dy;
17564 struct text_pos start_pos;
17565 struct run run;
17566 int first_unchanged_at_end_vpos = 0;
17567 struct glyph_row *last_text_row, *last_text_row_at_end;
17568 struct text_pos start;
17569 ptrdiff_t first_changed_charpos, last_changed_charpos;
17571 #ifdef GLYPH_DEBUG
17572 if (inhibit_try_window_id)
17573 return 0;
17574 #endif
17576 /* This is handy for debugging. */
17577 #if 0
17578 #define GIVE_UP(X) \
17579 do { \
17580 fprintf (stderr, "try_window_id give up %d\n", (X)); \
17581 return 0; \
17582 } while (0)
17583 #else
17584 #define GIVE_UP(X) return 0
17585 #endif
17587 SET_TEXT_POS_FROM_MARKER (start, w->start);
17589 /* Don't use this for mini-windows because these can show
17590 messages and mini-buffers, and we don't handle that here. */
17591 if (MINI_WINDOW_P (w))
17592 GIVE_UP (1);
17594 /* This flag is used to prevent redisplay optimizations. */
17595 if (windows_or_buffers_changed || f->cursor_type_changed)
17596 GIVE_UP (2);
17598 /* This function's optimizations cannot be used if overlays have
17599 changed in the buffer displayed by the window, so give up if they
17600 have. */
17601 if (w->last_overlay_modified != OVERLAY_MODIFF)
17602 GIVE_UP (21);
17604 /* Verify that narrowing has not changed.
17605 Also verify that we were not told to prevent redisplay optimizations.
17606 It would be nice to further
17607 reduce the number of cases where this prevents try_window_id. */
17608 if (current_buffer->clip_changed
17609 || current_buffer->prevent_redisplay_optimizations_p)
17610 GIVE_UP (3);
17612 /* Window must either use window-based redisplay or be full width. */
17613 if (!FRAME_WINDOW_P (f)
17614 && (!FRAME_LINE_INS_DEL_OK (f)
17615 || !WINDOW_FULL_WIDTH_P (w)))
17616 GIVE_UP (4);
17618 /* Give up if point is known NOT to appear in W. */
17619 if (PT < CHARPOS (start))
17620 GIVE_UP (5);
17622 /* Another way to prevent redisplay optimizations. */
17623 if (w->last_modified == 0)
17624 GIVE_UP (6);
17626 /* Verify that window is not hscrolled. */
17627 if (w->hscroll != 0)
17628 GIVE_UP (7);
17630 /* Verify that display wasn't paused. */
17631 if (!w->window_end_valid)
17632 GIVE_UP (8);
17634 /* Likewise if highlighting trailing whitespace. */
17635 if (!NILP (Vshow_trailing_whitespace))
17636 GIVE_UP (11);
17638 /* Can't use this if overlay arrow position and/or string have
17639 changed. */
17640 if (overlay_arrows_changed_p ())
17641 GIVE_UP (12);
17643 /* When word-wrap is on, adding a space to the first word of a
17644 wrapped line can change the wrap position, altering the line
17645 above it. It might be worthwhile to handle this more
17646 intelligently, but for now just redisplay from scratch. */
17647 if (!NILP (BVAR (XBUFFER (w->contents), word_wrap)))
17648 GIVE_UP (21);
17650 /* Under bidi reordering, adding or deleting a character in the
17651 beginning of a paragraph, before the first strong directional
17652 character, can change the base direction of the paragraph (unless
17653 the buffer specifies a fixed paragraph direction), which will
17654 require to redisplay the whole paragraph. It might be worthwhile
17655 to find the paragraph limits and widen the range of redisplayed
17656 lines to that, but for now just give up this optimization and
17657 redisplay from scratch. */
17658 if (!NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering))
17659 && NILP (BVAR (XBUFFER (w->contents), bidi_paragraph_direction)))
17660 GIVE_UP (22);
17662 /* Make sure beg_unchanged and end_unchanged are up to date. Do it
17663 only if buffer has really changed. The reason is that the gap is
17664 initially at Z for freshly visited files. The code below would
17665 set end_unchanged to 0 in that case. */
17666 if (MODIFF > SAVE_MODIFF
17667 /* This seems to happen sometimes after saving a buffer. */
17668 || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
17670 if (GPT - BEG < BEG_UNCHANGED)
17671 BEG_UNCHANGED = GPT - BEG;
17672 if (Z - GPT < END_UNCHANGED)
17673 END_UNCHANGED = Z - GPT;
17676 /* The position of the first and last character that has been changed. */
17677 first_changed_charpos = BEG + BEG_UNCHANGED;
17678 last_changed_charpos = Z - END_UNCHANGED;
17680 /* If window starts after a line end, and the last change is in
17681 front of that newline, then changes don't affect the display.
17682 This case happens with stealth-fontification. Note that although
17683 the display is unchanged, glyph positions in the matrix have to
17684 be adjusted, of course. */
17685 row = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
17686 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
17687 && ((last_changed_charpos < CHARPOS (start)
17688 && CHARPOS (start) == BEGV)
17689 || (last_changed_charpos < CHARPOS (start) - 1
17690 && FETCH_BYTE (BYTEPOS (start) - 1) == '\n')))
17692 ptrdiff_t Z_old, Z_delta, Z_BYTE_old, Z_delta_bytes;
17693 struct glyph_row *r0;
17695 /* Compute how many chars/bytes have been added to or removed
17696 from the buffer. */
17697 Z_old = MATRIX_ROW_END_CHARPOS (row) + w->window_end_pos;
17698 Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
17699 Z_delta = Z - Z_old;
17700 Z_delta_bytes = Z_BYTE - Z_BYTE_old;
17702 /* Give up if PT is not in the window. Note that it already has
17703 been checked at the start of try_window_id that PT is not in
17704 front of the window start. */
17705 if (PT >= MATRIX_ROW_END_CHARPOS (row) + Z_delta)
17706 GIVE_UP (13);
17708 /* If window start is unchanged, we can reuse the whole matrix
17709 as is, after adjusting glyph positions. No need to compute
17710 the window end again, since its offset from Z hasn't changed. */
17711 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
17712 if (CHARPOS (start) == MATRIX_ROW_START_CHARPOS (r0) + Z_delta
17713 && BYTEPOS (start) == MATRIX_ROW_START_BYTEPOS (r0) + Z_delta_bytes
17714 /* PT must not be in a partially visible line. */
17715 && !(PT >= MATRIX_ROW_START_CHARPOS (row) + Z_delta
17716 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
17718 /* Adjust positions in the glyph matrix. */
17719 if (Z_delta || Z_delta_bytes)
17721 struct glyph_row *r1
17722 = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
17723 increment_matrix_positions (w->current_matrix,
17724 MATRIX_ROW_VPOS (r0, current_matrix),
17725 MATRIX_ROW_VPOS (r1, current_matrix),
17726 Z_delta, Z_delta_bytes);
17729 /* Set the cursor. */
17730 row = row_containing_pos (w, PT, r0, NULL, 0);
17731 if (row)
17732 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
17733 return 1;
17737 /* Handle the case that changes are all below what is displayed in
17738 the window, and that PT is in the window. This shortcut cannot
17739 be taken if ZV is visible in the window, and text has been added
17740 there that is visible in the window. */
17741 if (first_changed_charpos >= MATRIX_ROW_END_CHARPOS (row)
17742 /* ZV is not visible in the window, or there are no
17743 changes at ZV, actually. */
17744 && (current_matrix->zv > MATRIX_ROW_END_CHARPOS (row)
17745 || first_changed_charpos == last_changed_charpos))
17747 struct glyph_row *r0;
17749 /* Give up if PT is not in the window. Note that it already has
17750 been checked at the start of try_window_id that PT is not in
17751 front of the window start. */
17752 if (PT >= MATRIX_ROW_END_CHARPOS (row))
17753 GIVE_UP (14);
17755 /* If window start is unchanged, we can reuse the whole matrix
17756 as is, without changing glyph positions since no text has
17757 been added/removed in front of the window end. */
17758 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
17759 if (TEXT_POS_EQUAL_P (start, r0->minpos)
17760 /* PT must not be in a partially visible line. */
17761 && !(PT >= MATRIX_ROW_START_CHARPOS (row)
17762 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
17764 /* We have to compute the window end anew since text
17765 could have been added/removed after it. */
17766 w->window_end_pos = Z - MATRIX_ROW_END_CHARPOS (row);
17767 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
17769 /* Set the cursor. */
17770 row = row_containing_pos (w, PT, r0, NULL, 0);
17771 if (row)
17772 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
17773 return 2;
17777 /* Give up if window start is in the changed area.
17779 The condition used to read
17781 (BEG_UNCHANGED + END_UNCHANGED != Z - BEG && ...)
17783 but why that was tested escapes me at the moment. */
17784 if (CHARPOS (start) >= first_changed_charpos
17785 && CHARPOS (start) <= last_changed_charpos)
17786 GIVE_UP (15);
17788 /* Check that window start agrees with the start of the first glyph
17789 row in its current matrix. Check this after we know the window
17790 start is not in changed text, otherwise positions would not be
17791 comparable. */
17792 row = MATRIX_FIRST_TEXT_ROW (current_matrix);
17793 if (!TEXT_POS_EQUAL_P (start, row->minpos))
17794 GIVE_UP (16);
17796 /* Give up if the window ends in strings. Overlay strings
17797 at the end are difficult to handle, so don't try. */
17798 row = MATRIX_ROW (current_matrix, w->window_end_vpos);
17799 if (MATRIX_ROW_START_CHARPOS (row) == MATRIX_ROW_END_CHARPOS (row))
17800 GIVE_UP (20);
17802 /* Compute the position at which we have to start displaying new
17803 lines. Some of the lines at the top of the window might be
17804 reusable because they are not displaying changed text. Find the
17805 last row in W's current matrix not affected by changes at the
17806 start of current_buffer. Value is null if changes start in the
17807 first line of window. */
17808 last_unchanged_at_beg_row = find_last_unchanged_at_beg_row (w);
17809 if (last_unchanged_at_beg_row)
17811 /* Avoid starting to display in the middle of a character, a TAB
17812 for instance. This is easier than to set up the iterator
17813 exactly, and it's not a frequent case, so the additional
17814 effort wouldn't really pay off. */
17815 while ((MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row)
17816 || last_unchanged_at_beg_row->ends_in_newline_from_string_p)
17817 && last_unchanged_at_beg_row > w->current_matrix->rows)
17818 --last_unchanged_at_beg_row;
17820 if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row))
17821 GIVE_UP (17);
17823 if (init_to_row_end (&it, w, last_unchanged_at_beg_row) == 0)
17824 GIVE_UP (18);
17825 start_pos = it.current.pos;
17827 /* Start displaying new lines in the desired matrix at the same
17828 vpos we would use in the current matrix, i.e. below
17829 last_unchanged_at_beg_row. */
17830 it.vpos = 1 + MATRIX_ROW_VPOS (last_unchanged_at_beg_row,
17831 current_matrix);
17832 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
17833 it.current_y = MATRIX_ROW_BOTTOM_Y (last_unchanged_at_beg_row);
17835 eassert (it.hpos == 0 && it.current_x == 0);
17837 else
17839 /* There are no reusable lines at the start of the window.
17840 Start displaying in the first text line. */
17841 start_display (&it, w, start);
17842 it.vpos = it.first_vpos;
17843 start_pos = it.current.pos;
17846 /* Find the first row that is not affected by changes at the end of
17847 the buffer. Value will be null if there is no unchanged row, in
17848 which case we must redisplay to the end of the window. delta
17849 will be set to the value by which buffer positions beginning with
17850 first_unchanged_at_end_row have to be adjusted due to text
17851 changes. */
17852 first_unchanged_at_end_row
17853 = find_first_unchanged_at_end_row (w, &delta, &delta_bytes);
17854 IF_DEBUG (debug_delta = delta);
17855 IF_DEBUG (debug_delta_bytes = delta_bytes);
17857 /* Set stop_pos to the buffer position up to which we will have to
17858 display new lines. If first_unchanged_at_end_row != NULL, this
17859 is the buffer position of the start of the line displayed in that
17860 row. For first_unchanged_at_end_row == NULL, use 0 to indicate
17861 that we don't stop at a buffer position. */
17862 stop_pos = 0;
17863 if (first_unchanged_at_end_row)
17865 eassert (last_unchanged_at_beg_row == NULL
17866 || first_unchanged_at_end_row >= last_unchanged_at_beg_row);
17868 /* If this is a continuation line, move forward to the next one
17869 that isn't. Changes in lines above affect this line.
17870 Caution: this may move first_unchanged_at_end_row to a row
17871 not displaying text. */
17872 while (MATRIX_ROW_CONTINUATION_LINE_P (first_unchanged_at_end_row)
17873 && MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
17874 && (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
17875 < it.last_visible_y))
17876 ++first_unchanged_at_end_row;
17878 if (!MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
17879 || (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
17880 >= it.last_visible_y))
17881 first_unchanged_at_end_row = NULL;
17882 else
17884 stop_pos = (MATRIX_ROW_START_CHARPOS (first_unchanged_at_end_row)
17885 + delta);
17886 first_unchanged_at_end_vpos
17887 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, current_matrix);
17888 eassert (stop_pos >= Z - END_UNCHANGED);
17891 else if (last_unchanged_at_beg_row == NULL)
17892 GIVE_UP (19);
17895 #ifdef GLYPH_DEBUG
17897 /* Either there is no unchanged row at the end, or the one we have
17898 now displays text. This is a necessary condition for the window
17899 end pos calculation at the end of this function. */
17900 eassert (first_unchanged_at_end_row == NULL
17901 || MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
17903 debug_last_unchanged_at_beg_vpos
17904 = (last_unchanged_at_beg_row
17905 ? MATRIX_ROW_VPOS (last_unchanged_at_beg_row, current_matrix)
17906 : -1);
17907 debug_first_unchanged_at_end_vpos = first_unchanged_at_end_vpos;
17909 #endif /* GLYPH_DEBUG */
17912 /* Display new lines. Set last_text_row to the last new line
17913 displayed which has text on it, i.e. might end up as being the
17914 line where the window_end_vpos is. */
17915 w->cursor.vpos = -1;
17916 last_text_row = NULL;
17917 overlay_arrow_seen = 0;
17918 while (it.current_y < it.last_visible_y
17919 && !f->fonts_changed
17920 && (first_unchanged_at_end_row == NULL
17921 || IT_CHARPOS (it) < stop_pos))
17923 if (display_line (&it))
17924 last_text_row = it.glyph_row - 1;
17927 if (f->fonts_changed)
17928 return -1;
17931 /* Compute differences in buffer positions, y-positions etc. for
17932 lines reused at the bottom of the window. Compute what we can
17933 scroll. */
17934 if (first_unchanged_at_end_row
17935 /* No lines reused because we displayed everything up to the
17936 bottom of the window. */
17937 && it.current_y < it.last_visible_y)
17939 dvpos = (it.vpos
17940 - MATRIX_ROW_VPOS (first_unchanged_at_end_row,
17941 current_matrix));
17942 dy = it.current_y - first_unchanged_at_end_row->y;
17943 run.current_y = first_unchanged_at_end_row->y;
17944 run.desired_y = run.current_y + dy;
17945 run.height = it.last_visible_y - max (run.current_y, run.desired_y);
17947 else
17949 delta = delta_bytes = dvpos = dy
17950 = run.current_y = run.desired_y = run.height = 0;
17951 first_unchanged_at_end_row = NULL;
17953 IF_DEBUG ((debug_dvpos = dvpos, debug_dy = dy));
17956 /* Find the cursor if not already found. We have to decide whether
17957 PT will appear on this window (it sometimes doesn't, but this is
17958 not a very frequent case.) This decision has to be made before
17959 the current matrix is altered. A value of cursor.vpos < 0 means
17960 that PT is either in one of the lines beginning at
17961 first_unchanged_at_end_row or below the window. Don't care for
17962 lines that might be displayed later at the window end; as
17963 mentioned, this is not a frequent case. */
17964 if (w->cursor.vpos < 0)
17966 /* Cursor in unchanged rows at the top? */
17967 if (PT < CHARPOS (start_pos)
17968 && last_unchanged_at_beg_row)
17970 row = row_containing_pos (w, PT,
17971 MATRIX_FIRST_TEXT_ROW (w->current_matrix),
17972 last_unchanged_at_beg_row + 1, 0);
17973 if (row)
17974 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
17977 /* Start from first_unchanged_at_end_row looking for PT. */
17978 else if (first_unchanged_at_end_row)
17980 row = row_containing_pos (w, PT - delta,
17981 first_unchanged_at_end_row, NULL, 0);
17982 if (row)
17983 set_cursor_from_row (w, row, w->current_matrix, delta,
17984 delta_bytes, dy, dvpos);
17987 /* Give up if cursor was not found. */
17988 if (w->cursor.vpos < 0)
17990 clear_glyph_matrix (w->desired_matrix);
17991 return -1;
17995 /* Don't let the cursor end in the scroll margins. */
17997 int this_scroll_margin, cursor_height;
17998 int frame_line_height = default_line_pixel_height (w);
17999 int window_total_lines
18000 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (it.f) / frame_line_height;
18002 this_scroll_margin =
18003 max (0, min (scroll_margin, window_total_lines / 4));
18004 this_scroll_margin *= frame_line_height;
18005 cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
18007 if ((w->cursor.y < this_scroll_margin
18008 && CHARPOS (start) > BEGV)
18009 /* Old redisplay didn't take scroll margin into account at the bottom,
18010 but then global-hl-line-mode doesn't scroll. KFS 2004-06-14 */
18011 || (w->cursor.y + (make_cursor_line_fully_visible_p
18012 ? cursor_height + this_scroll_margin
18013 : 1)) > it.last_visible_y)
18015 w->cursor.vpos = -1;
18016 clear_glyph_matrix (w->desired_matrix);
18017 return -1;
18021 /* Scroll the display. Do it before changing the current matrix so
18022 that xterm.c doesn't get confused about where the cursor glyph is
18023 found. */
18024 if (dy && run.height)
18026 update_begin (f);
18028 if (FRAME_WINDOW_P (f))
18030 FRAME_RIF (f)->update_window_begin_hook (w);
18031 FRAME_RIF (f)->clear_window_mouse_face (w);
18032 FRAME_RIF (f)->scroll_run_hook (w, &run);
18033 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
18035 else
18037 /* Terminal frame. In this case, dvpos gives the number of
18038 lines to scroll by; dvpos < 0 means scroll up. */
18039 int from_vpos
18040 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, w->current_matrix);
18041 int from = WINDOW_TOP_EDGE_LINE (w) + from_vpos;
18042 int end = (WINDOW_TOP_EDGE_LINE (w)
18043 + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0)
18044 + window_internal_height (w));
18046 #if defined (HAVE_GPM) || defined (MSDOS)
18047 x_clear_window_mouse_face (w);
18048 #endif
18049 /* Perform the operation on the screen. */
18050 if (dvpos > 0)
18052 /* Scroll last_unchanged_at_beg_row to the end of the
18053 window down dvpos lines. */
18054 set_terminal_window (f, end);
18056 /* On dumb terminals delete dvpos lines at the end
18057 before inserting dvpos empty lines. */
18058 if (!FRAME_SCROLL_REGION_OK (f))
18059 ins_del_lines (f, end - dvpos, -dvpos);
18061 /* Insert dvpos empty lines in front of
18062 last_unchanged_at_beg_row. */
18063 ins_del_lines (f, from, dvpos);
18065 else if (dvpos < 0)
18067 /* Scroll up last_unchanged_at_beg_vpos to the end of
18068 the window to last_unchanged_at_beg_vpos - |dvpos|. */
18069 set_terminal_window (f, end);
18071 /* Delete dvpos lines in front of
18072 last_unchanged_at_beg_vpos. ins_del_lines will set
18073 the cursor to the given vpos and emit |dvpos| delete
18074 line sequences. */
18075 ins_del_lines (f, from + dvpos, dvpos);
18077 /* On a dumb terminal insert dvpos empty lines at the
18078 end. */
18079 if (!FRAME_SCROLL_REGION_OK (f))
18080 ins_del_lines (f, end + dvpos, -dvpos);
18083 set_terminal_window (f, 0);
18086 update_end (f);
18089 /* Shift reused rows of the current matrix to the right position.
18090 BOTTOM_ROW is the last + 1 row in the current matrix reserved for
18091 text. */
18092 bottom_row = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
18093 bottom_vpos = MATRIX_ROW_VPOS (bottom_row, current_matrix);
18094 if (dvpos < 0)
18096 rotate_matrix (current_matrix, first_unchanged_at_end_vpos + dvpos,
18097 bottom_vpos, dvpos);
18098 clear_glyph_matrix_rows (current_matrix, bottom_vpos + dvpos,
18099 bottom_vpos);
18101 else if (dvpos > 0)
18103 rotate_matrix (current_matrix, first_unchanged_at_end_vpos,
18104 bottom_vpos, dvpos);
18105 clear_glyph_matrix_rows (current_matrix, first_unchanged_at_end_vpos,
18106 first_unchanged_at_end_vpos + dvpos);
18109 /* For frame-based redisplay, make sure that current frame and window
18110 matrix are in sync with respect to glyph memory. */
18111 if (!FRAME_WINDOW_P (f))
18112 sync_frame_with_window_matrix_rows (w);
18114 /* Adjust buffer positions in reused rows. */
18115 if (delta || delta_bytes)
18116 increment_matrix_positions (current_matrix,
18117 first_unchanged_at_end_vpos + dvpos,
18118 bottom_vpos, delta, delta_bytes);
18120 /* Adjust Y positions. */
18121 if (dy)
18122 shift_glyph_matrix (w, current_matrix,
18123 first_unchanged_at_end_vpos + dvpos,
18124 bottom_vpos, dy);
18126 if (first_unchanged_at_end_row)
18128 first_unchanged_at_end_row += dvpos;
18129 if (first_unchanged_at_end_row->y >= it.last_visible_y
18130 || !MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row))
18131 first_unchanged_at_end_row = NULL;
18134 /* If scrolling up, there may be some lines to display at the end of
18135 the window. */
18136 last_text_row_at_end = NULL;
18137 if (dy < 0)
18139 /* Scrolling up can leave for example a partially visible line
18140 at the end of the window to be redisplayed. */
18141 /* Set last_row to the glyph row in the current matrix where the
18142 window end line is found. It has been moved up or down in
18143 the matrix by dvpos. */
18144 int last_vpos = w->window_end_vpos + dvpos;
18145 struct glyph_row *last_row = MATRIX_ROW (current_matrix, last_vpos);
18147 /* If last_row is the window end line, it should display text. */
18148 eassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_row));
18150 /* If window end line was partially visible before, begin
18151 displaying at that line. Otherwise begin displaying with the
18152 line following it. */
18153 if (MATRIX_ROW_BOTTOM_Y (last_row) - dy >= it.last_visible_y)
18155 init_to_row_start (&it, w, last_row);
18156 it.vpos = last_vpos;
18157 it.current_y = last_row->y;
18159 else
18161 init_to_row_end (&it, w, last_row);
18162 it.vpos = 1 + last_vpos;
18163 it.current_y = MATRIX_ROW_BOTTOM_Y (last_row);
18164 ++last_row;
18167 /* We may start in a continuation line. If so, we have to
18168 get the right continuation_lines_width and current_x. */
18169 it.continuation_lines_width = last_row->continuation_lines_width;
18170 it.hpos = it.current_x = 0;
18172 /* Display the rest of the lines at the window end. */
18173 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
18174 while (it.current_y < it.last_visible_y && !f->fonts_changed)
18176 /* Is it always sure that the display agrees with lines in
18177 the current matrix? I don't think so, so we mark rows
18178 displayed invalid in the current matrix by setting their
18179 enabled_p flag to zero. */
18180 SET_MATRIX_ROW_ENABLED_P (w->current_matrix, it.vpos, false);
18181 if (display_line (&it))
18182 last_text_row_at_end = it.glyph_row - 1;
18186 /* Update window_end_pos and window_end_vpos. */
18187 if (first_unchanged_at_end_row && !last_text_row_at_end)
18189 /* Window end line if one of the preserved rows from the current
18190 matrix. Set row to the last row displaying text in current
18191 matrix starting at first_unchanged_at_end_row, after
18192 scrolling. */
18193 eassert (MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
18194 row = find_last_row_displaying_text (w->current_matrix, &it,
18195 first_unchanged_at_end_row);
18196 eassert (row && MATRIX_ROW_DISPLAYS_TEXT_P (row));
18197 adjust_window_ends (w, row, 1);
18198 eassert (w->window_end_bytepos >= 0);
18199 IF_DEBUG (debug_method_add (w, "A"));
18201 else if (last_text_row_at_end)
18203 adjust_window_ends (w, last_text_row_at_end, 0);
18204 eassert (w->window_end_bytepos >= 0);
18205 IF_DEBUG (debug_method_add (w, "B"));
18207 else if (last_text_row)
18209 /* We have displayed either to the end of the window or at the
18210 end of the window, i.e. the last row with text is to be found
18211 in the desired matrix. */
18212 adjust_window_ends (w, last_text_row, 0);
18213 eassert (w->window_end_bytepos >= 0);
18215 else if (first_unchanged_at_end_row == NULL
18216 && last_text_row == NULL
18217 && last_text_row_at_end == NULL)
18219 /* Displayed to end of window, but no line containing text was
18220 displayed. Lines were deleted at the end of the window. */
18221 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
18222 int vpos = w->window_end_vpos;
18223 struct glyph_row *current_row = current_matrix->rows + vpos;
18224 struct glyph_row *desired_row = desired_matrix->rows + vpos;
18226 for (row = NULL;
18227 row == NULL && vpos >= first_vpos;
18228 --vpos, --current_row, --desired_row)
18230 if (desired_row->enabled_p)
18232 if (MATRIX_ROW_DISPLAYS_TEXT_P (desired_row))
18233 row = desired_row;
18235 else if (MATRIX_ROW_DISPLAYS_TEXT_P (current_row))
18236 row = current_row;
18239 eassert (row != NULL);
18240 w->window_end_vpos = vpos + 1;
18241 w->window_end_pos = Z - MATRIX_ROW_END_CHARPOS (row);
18242 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
18243 eassert (w->window_end_bytepos >= 0);
18244 IF_DEBUG (debug_method_add (w, "C"));
18246 else
18247 emacs_abort ();
18249 IF_DEBUG ((debug_end_pos = w->window_end_pos,
18250 debug_end_vpos = w->window_end_vpos));
18252 /* Record that display has not been completed. */
18253 w->window_end_valid = 0;
18254 w->desired_matrix->no_scrolling_p = 1;
18255 return 3;
18257 #undef GIVE_UP
18262 /***********************************************************************
18263 More debugging support
18264 ***********************************************************************/
18266 #ifdef GLYPH_DEBUG
18268 void dump_glyph_row (struct glyph_row *, int, int) EXTERNALLY_VISIBLE;
18269 void dump_glyph_matrix (struct glyph_matrix *, int) EXTERNALLY_VISIBLE;
18270 void dump_glyph (struct glyph_row *, struct glyph *, int) EXTERNALLY_VISIBLE;
18273 /* Dump the contents of glyph matrix MATRIX on stderr.
18275 GLYPHS 0 means don't show glyph contents.
18276 GLYPHS 1 means show glyphs in short form
18277 GLYPHS > 1 means show glyphs in long form. */
18279 void
18280 dump_glyph_matrix (struct glyph_matrix *matrix, int glyphs)
18282 int i;
18283 for (i = 0; i < matrix->nrows; ++i)
18284 dump_glyph_row (MATRIX_ROW (matrix, i), i, glyphs);
18288 /* Dump contents of glyph GLYPH to stderr. ROW and AREA are
18289 the glyph row and area where the glyph comes from. */
18291 void
18292 dump_glyph (struct glyph_row *row, struct glyph *glyph, int area)
18294 if (glyph->type == CHAR_GLYPH
18295 || glyph->type == GLYPHLESS_GLYPH)
18297 fprintf (stderr,
18298 " %5"pD"d %c %9"pI"d %c %3d 0x%06x %c %4d %1.1d%1.1d\n",
18299 glyph - row->glyphs[TEXT_AREA],
18300 (glyph->type == CHAR_GLYPH
18301 ? 'C'
18302 : 'G'),
18303 glyph->charpos,
18304 (BUFFERP (glyph->object)
18305 ? 'B'
18306 : (STRINGP (glyph->object)
18307 ? 'S'
18308 : (INTEGERP (glyph->object)
18309 ? '0'
18310 : '-'))),
18311 glyph->pixel_width,
18312 glyph->u.ch,
18313 (glyph->u.ch < 0x80 && glyph->u.ch >= ' '
18314 ? glyph->u.ch
18315 : '.'),
18316 glyph->face_id,
18317 glyph->left_box_line_p,
18318 glyph->right_box_line_p);
18320 else if (glyph->type == STRETCH_GLYPH)
18322 fprintf (stderr,
18323 " %5"pD"d %c %9"pI"d %c %3d 0x%06x %c %4d %1.1d%1.1d\n",
18324 glyph - row->glyphs[TEXT_AREA],
18325 'S',
18326 glyph->charpos,
18327 (BUFFERP (glyph->object)
18328 ? 'B'
18329 : (STRINGP (glyph->object)
18330 ? 'S'
18331 : (INTEGERP (glyph->object)
18332 ? '0'
18333 : '-'))),
18334 glyph->pixel_width,
18336 ' ',
18337 glyph->face_id,
18338 glyph->left_box_line_p,
18339 glyph->right_box_line_p);
18341 else if (glyph->type == IMAGE_GLYPH)
18343 fprintf (stderr,
18344 " %5"pD"d %c %9"pI"d %c %3d 0x%06x %c %4d %1.1d%1.1d\n",
18345 glyph - row->glyphs[TEXT_AREA],
18346 'I',
18347 glyph->charpos,
18348 (BUFFERP (glyph->object)
18349 ? 'B'
18350 : (STRINGP (glyph->object)
18351 ? 'S'
18352 : (INTEGERP (glyph->object)
18353 ? '0'
18354 : '-'))),
18355 glyph->pixel_width,
18356 glyph->u.img_id,
18357 '.',
18358 glyph->face_id,
18359 glyph->left_box_line_p,
18360 glyph->right_box_line_p);
18362 else if (glyph->type == COMPOSITE_GLYPH)
18364 fprintf (stderr,
18365 " %5"pD"d %c %9"pI"d %c %3d 0x%06x",
18366 glyph - row->glyphs[TEXT_AREA],
18367 '+',
18368 glyph->charpos,
18369 (BUFFERP (glyph->object)
18370 ? 'B'
18371 : (STRINGP (glyph->object)
18372 ? 'S'
18373 : (INTEGERP (glyph->object)
18374 ? '0'
18375 : '-'))),
18376 glyph->pixel_width,
18377 glyph->u.cmp.id);
18378 if (glyph->u.cmp.automatic)
18379 fprintf (stderr,
18380 "[%d-%d]",
18381 glyph->slice.cmp.from, glyph->slice.cmp.to);
18382 fprintf (stderr, " . %4d %1.1d%1.1d\n",
18383 glyph->face_id,
18384 glyph->left_box_line_p,
18385 glyph->right_box_line_p);
18390 /* Dump the contents of glyph row at VPOS in MATRIX to stderr.
18391 GLYPHS 0 means don't show glyph contents.
18392 GLYPHS 1 means show glyphs in short form
18393 GLYPHS > 1 means show glyphs in long form. */
18395 void
18396 dump_glyph_row (struct glyph_row *row, int vpos, int glyphs)
18398 if (glyphs != 1)
18400 fprintf (stderr, "Row Start End Used oE><\\CTZFesm X Y W H V A P\n");
18401 fprintf (stderr, "==============================================================================\n");
18403 fprintf (stderr, "%3d %9"pI"d %9"pI"d %4d %1.1d%1.1d%1.1d%1.1d\
18404 %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d %4d %4d %4d %4d %4d %4d %4d\n",
18405 vpos,
18406 MATRIX_ROW_START_CHARPOS (row),
18407 MATRIX_ROW_END_CHARPOS (row),
18408 row->used[TEXT_AREA],
18409 row->contains_overlapping_glyphs_p,
18410 row->enabled_p,
18411 row->truncated_on_left_p,
18412 row->truncated_on_right_p,
18413 row->continued_p,
18414 MATRIX_ROW_CONTINUATION_LINE_P (row),
18415 MATRIX_ROW_DISPLAYS_TEXT_P (row),
18416 row->ends_at_zv_p,
18417 row->fill_line_p,
18418 row->ends_in_middle_of_char_p,
18419 row->starts_in_middle_of_char_p,
18420 row->mouse_face_p,
18421 row->x,
18422 row->y,
18423 row->pixel_width,
18424 row->height,
18425 row->visible_height,
18426 row->ascent,
18427 row->phys_ascent);
18428 /* The next 3 lines should align to "Start" in the header. */
18429 fprintf (stderr, " %9"pD"d %9"pD"d\t%5d\n", row->start.overlay_string_index,
18430 row->end.overlay_string_index,
18431 row->continuation_lines_width);
18432 fprintf (stderr, " %9"pI"d %9"pI"d\n",
18433 CHARPOS (row->start.string_pos),
18434 CHARPOS (row->end.string_pos));
18435 fprintf (stderr, " %9d %9d\n", row->start.dpvec_index,
18436 row->end.dpvec_index);
18439 if (glyphs > 1)
18441 int area;
18443 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
18445 struct glyph *glyph = row->glyphs[area];
18446 struct glyph *glyph_end = glyph + row->used[area];
18448 /* Glyph for a line end in text. */
18449 if (area == TEXT_AREA && glyph == glyph_end && glyph->charpos > 0)
18450 ++glyph_end;
18452 if (glyph < glyph_end)
18453 fprintf (stderr, " Glyph# Type Pos O W Code C Face LR\n");
18455 for (; glyph < glyph_end; ++glyph)
18456 dump_glyph (row, glyph, area);
18459 else if (glyphs == 1)
18461 int area;
18463 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
18465 char *s = alloca (row->used[area] + 4);
18466 int i;
18468 for (i = 0; i < row->used[area]; ++i)
18470 struct glyph *glyph = row->glyphs[area] + i;
18471 if (i == row->used[area] - 1
18472 && area == TEXT_AREA
18473 && INTEGERP (glyph->object)
18474 && glyph->type == CHAR_GLYPH
18475 && glyph->u.ch == ' ')
18477 strcpy (&s[i], "[\\n]");
18478 i += 4;
18480 else if (glyph->type == CHAR_GLYPH
18481 && glyph->u.ch < 0x80
18482 && glyph->u.ch >= ' ')
18483 s[i] = glyph->u.ch;
18484 else
18485 s[i] = '.';
18488 s[i] = '\0';
18489 fprintf (stderr, "%3d: (%d) '%s'\n", vpos, row->enabled_p, s);
18495 DEFUN ("dump-glyph-matrix", Fdump_glyph_matrix,
18496 Sdump_glyph_matrix, 0, 1, "p",
18497 doc: /* Dump the current matrix of the selected window to stderr.
18498 Shows contents of glyph row structures. With non-nil
18499 parameter GLYPHS, dump glyphs as well. If GLYPHS is 1 show
18500 glyphs in short form, otherwise show glyphs in long form. */)
18501 (Lisp_Object glyphs)
18503 struct window *w = XWINDOW (selected_window);
18504 struct buffer *buffer = XBUFFER (w->contents);
18506 fprintf (stderr, "PT = %"pI"d, BEGV = %"pI"d. ZV = %"pI"d\n",
18507 BUF_PT (buffer), BUF_BEGV (buffer), BUF_ZV (buffer));
18508 fprintf (stderr, "Cursor x = %d, y = %d, hpos = %d, vpos = %d\n",
18509 w->cursor.x, w->cursor.y, w->cursor.hpos, w->cursor.vpos);
18510 fprintf (stderr, "=============================================\n");
18511 dump_glyph_matrix (w->current_matrix,
18512 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 0);
18513 return Qnil;
18517 DEFUN ("dump-frame-glyph-matrix", Fdump_frame_glyph_matrix,
18518 Sdump_frame_glyph_matrix, 0, 0, "", doc: /* */)
18519 (void)
18521 struct frame *f = XFRAME (selected_frame);
18522 dump_glyph_matrix (f->current_matrix, 1);
18523 return Qnil;
18527 DEFUN ("dump-glyph-row", Fdump_glyph_row, Sdump_glyph_row, 1, 2, "",
18528 doc: /* Dump glyph row ROW to stderr.
18529 GLYPH 0 means don't dump glyphs.
18530 GLYPH 1 means dump glyphs in short form.
18531 GLYPH > 1 or omitted means dump glyphs in long form. */)
18532 (Lisp_Object row, Lisp_Object glyphs)
18534 struct glyph_matrix *matrix;
18535 EMACS_INT vpos;
18537 CHECK_NUMBER (row);
18538 matrix = XWINDOW (selected_window)->current_matrix;
18539 vpos = XINT (row);
18540 if (vpos >= 0 && vpos < matrix->nrows)
18541 dump_glyph_row (MATRIX_ROW (matrix, vpos),
18542 vpos,
18543 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 2);
18544 return Qnil;
18548 DEFUN ("dump-tool-bar-row", Fdump_tool_bar_row, Sdump_tool_bar_row, 1, 2, "",
18549 doc: /* Dump glyph row ROW of the tool-bar of the current frame to stderr.
18550 GLYPH 0 means don't dump glyphs.
18551 GLYPH 1 means dump glyphs in short form.
18552 GLYPH > 1 or omitted means dump glyphs in long form.
18554 If there's no tool-bar, or if the tool-bar is not drawn by Emacs,
18555 do nothing. */)
18556 (Lisp_Object row, Lisp_Object glyphs)
18558 #if defined (HAVE_WINDOW_SYSTEM) && ! defined (USE_GTK) && ! defined (HAVE_NS)
18559 struct frame *sf = SELECTED_FRAME ();
18560 struct glyph_matrix *m = XWINDOW (sf->tool_bar_window)->current_matrix;
18561 EMACS_INT vpos;
18563 CHECK_NUMBER (row);
18564 vpos = XINT (row);
18565 if (vpos >= 0 && vpos < m->nrows)
18566 dump_glyph_row (MATRIX_ROW (m, vpos), vpos,
18567 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 2);
18568 #endif
18569 return Qnil;
18573 DEFUN ("trace-redisplay", Ftrace_redisplay, Strace_redisplay, 0, 1, "P",
18574 doc: /* Toggle tracing of redisplay.
18575 With ARG, turn tracing on if and only if ARG is positive. */)
18576 (Lisp_Object arg)
18578 if (NILP (arg))
18579 trace_redisplay_p = !trace_redisplay_p;
18580 else
18582 arg = Fprefix_numeric_value (arg);
18583 trace_redisplay_p = XINT (arg) > 0;
18586 return Qnil;
18590 DEFUN ("trace-to-stderr", Ftrace_to_stderr, Strace_to_stderr, 1, MANY, "",
18591 doc: /* Like `format', but print result to stderr.
18592 usage: (trace-to-stderr STRING &rest OBJECTS) */)
18593 (ptrdiff_t nargs, Lisp_Object *args)
18595 Lisp_Object s = Fformat (nargs, args);
18596 fprintf (stderr, "%s", SDATA (s));
18597 return Qnil;
18600 #endif /* GLYPH_DEBUG */
18604 /***********************************************************************
18605 Building Desired Matrix Rows
18606 ***********************************************************************/
18608 /* Return a temporary glyph row holding the glyphs of an overlay arrow.
18609 Used for non-window-redisplay windows, and for windows w/o left fringe. */
18611 static struct glyph_row *
18612 get_overlay_arrow_glyph_row (struct window *w, Lisp_Object overlay_arrow_string)
18614 struct frame *f = XFRAME (WINDOW_FRAME (w));
18615 struct buffer *buffer = XBUFFER (w->contents);
18616 struct buffer *old = current_buffer;
18617 const unsigned char *arrow_string = SDATA (overlay_arrow_string);
18618 int arrow_len = SCHARS (overlay_arrow_string);
18619 const unsigned char *arrow_end = arrow_string + arrow_len;
18620 const unsigned char *p;
18621 struct it it;
18622 bool multibyte_p;
18623 int n_glyphs_before;
18625 set_buffer_temp (buffer);
18626 init_iterator (&it, w, -1, -1, &scratch_glyph_row, DEFAULT_FACE_ID);
18627 it.glyph_row->used[TEXT_AREA] = 0;
18628 SET_TEXT_POS (it.position, 0, 0);
18630 multibyte_p = !NILP (BVAR (buffer, enable_multibyte_characters));
18631 p = arrow_string;
18632 while (p < arrow_end)
18634 Lisp_Object face, ilisp;
18636 /* Get the next character. */
18637 if (multibyte_p)
18638 it.c = it.char_to_display = string_char_and_length (p, &it.len);
18639 else
18641 it.c = it.char_to_display = *p, it.len = 1;
18642 if (! ASCII_CHAR_P (it.c))
18643 it.char_to_display = BYTE8_TO_CHAR (it.c);
18645 p += it.len;
18647 /* Get its face. */
18648 ilisp = make_number (p - arrow_string);
18649 face = Fget_text_property (ilisp, Qface, overlay_arrow_string);
18650 it.face_id = compute_char_face (f, it.char_to_display, face);
18652 /* Compute its width, get its glyphs. */
18653 n_glyphs_before = it.glyph_row->used[TEXT_AREA];
18654 SET_TEXT_POS (it.position, -1, -1);
18655 PRODUCE_GLYPHS (&it);
18657 /* If this character doesn't fit any more in the line, we have
18658 to remove some glyphs. */
18659 if (it.current_x > it.last_visible_x)
18661 it.glyph_row->used[TEXT_AREA] = n_glyphs_before;
18662 break;
18666 set_buffer_temp (old);
18667 return it.glyph_row;
18671 /* Insert truncation glyphs at the start of IT->glyph_row. Which
18672 glyphs to insert is determined by produce_special_glyphs. */
18674 static void
18675 insert_left_trunc_glyphs (struct it *it)
18677 struct it truncate_it;
18678 struct glyph *from, *end, *to, *toend;
18680 eassert (!FRAME_WINDOW_P (it->f)
18681 || (!it->glyph_row->reversed_p
18682 && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0)
18683 || (it->glyph_row->reversed_p
18684 && WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0));
18686 /* Get the truncation glyphs. */
18687 truncate_it = *it;
18688 truncate_it.current_x = 0;
18689 truncate_it.face_id = DEFAULT_FACE_ID;
18690 truncate_it.glyph_row = &scratch_glyph_row;
18691 truncate_it.area = TEXT_AREA;
18692 truncate_it.glyph_row->used[TEXT_AREA] = 0;
18693 CHARPOS (truncate_it.position) = BYTEPOS (truncate_it.position) = -1;
18694 truncate_it.object = make_number (0);
18695 produce_special_glyphs (&truncate_it, IT_TRUNCATION);
18697 /* Overwrite glyphs from IT with truncation glyphs. */
18698 if (!it->glyph_row->reversed_p)
18700 short tused = truncate_it.glyph_row->used[TEXT_AREA];
18702 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
18703 end = from + tused;
18704 to = it->glyph_row->glyphs[TEXT_AREA];
18705 toend = to + it->glyph_row->used[TEXT_AREA];
18706 if (FRAME_WINDOW_P (it->f))
18708 /* On GUI frames, when variable-size fonts are displayed,
18709 the truncation glyphs may need more pixels than the row's
18710 glyphs they overwrite. We overwrite more glyphs to free
18711 enough screen real estate, and enlarge the stretch glyph
18712 on the right (see display_line), if there is one, to
18713 preserve the screen position of the truncation glyphs on
18714 the right. */
18715 int w = 0;
18716 struct glyph *g = to;
18717 short used;
18719 /* The first glyph could be partially visible, in which case
18720 it->glyph_row->x will be negative. But we want the left
18721 truncation glyphs to be aligned at the left margin of the
18722 window, so we override the x coordinate at which the row
18723 will begin. */
18724 it->glyph_row->x = 0;
18725 while (g < toend && w < it->truncation_pixel_width)
18727 w += g->pixel_width;
18728 ++g;
18730 if (g - to - tused > 0)
18732 memmove (to + tused, g, (toend - g) * sizeof(*g));
18733 it->glyph_row->used[TEXT_AREA] -= g - to - tused;
18735 used = it->glyph_row->used[TEXT_AREA];
18736 if (it->glyph_row->truncated_on_right_p
18737 && WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0
18738 && it->glyph_row->glyphs[TEXT_AREA][used - 2].type
18739 == STRETCH_GLYPH)
18741 int extra = w - it->truncation_pixel_width;
18743 it->glyph_row->glyphs[TEXT_AREA][used - 2].pixel_width += extra;
18747 while (from < end)
18748 *to++ = *from++;
18750 /* There may be padding glyphs left over. Overwrite them too. */
18751 if (!FRAME_WINDOW_P (it->f))
18753 while (to < toend && CHAR_GLYPH_PADDING_P (*to))
18755 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
18756 while (from < end)
18757 *to++ = *from++;
18761 if (to > toend)
18762 it->glyph_row->used[TEXT_AREA] = to - it->glyph_row->glyphs[TEXT_AREA];
18764 else
18766 short tused = truncate_it.glyph_row->used[TEXT_AREA];
18768 /* In R2L rows, overwrite the last (rightmost) glyphs, and do
18769 that back to front. */
18770 end = truncate_it.glyph_row->glyphs[TEXT_AREA];
18771 from = end + truncate_it.glyph_row->used[TEXT_AREA] - 1;
18772 toend = it->glyph_row->glyphs[TEXT_AREA];
18773 to = toend + it->glyph_row->used[TEXT_AREA] - 1;
18774 if (FRAME_WINDOW_P (it->f))
18776 int w = 0;
18777 struct glyph *g = to;
18779 while (g >= toend && w < it->truncation_pixel_width)
18781 w += g->pixel_width;
18782 --g;
18784 if (to - g - tused > 0)
18785 to = g + tused;
18786 if (it->glyph_row->truncated_on_right_p
18787 && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0
18788 && it->glyph_row->glyphs[TEXT_AREA][1].type == STRETCH_GLYPH)
18790 int extra = w - it->truncation_pixel_width;
18792 it->glyph_row->glyphs[TEXT_AREA][1].pixel_width += extra;
18796 while (from >= end && to >= toend)
18797 *to-- = *from--;
18798 if (!FRAME_WINDOW_P (it->f))
18800 while (to >= toend && CHAR_GLYPH_PADDING_P (*to))
18802 from =
18803 truncate_it.glyph_row->glyphs[TEXT_AREA]
18804 + truncate_it.glyph_row->used[TEXT_AREA] - 1;
18805 while (from >= end && to >= toend)
18806 *to-- = *from--;
18809 if (from >= end)
18811 /* Need to free some room before prepending additional
18812 glyphs. */
18813 int move_by = from - end + 1;
18814 struct glyph *g0 = it->glyph_row->glyphs[TEXT_AREA];
18815 struct glyph *g = g0 + it->glyph_row->used[TEXT_AREA] - 1;
18817 for ( ; g >= g0; g--)
18818 g[move_by] = *g;
18819 while (from >= end)
18820 *to-- = *from--;
18821 it->glyph_row->used[TEXT_AREA] += move_by;
18826 /* Compute the hash code for ROW. */
18827 unsigned
18828 row_hash (struct glyph_row *row)
18830 int area, k;
18831 unsigned hashval = 0;
18833 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
18834 for (k = 0; k < row->used[area]; ++k)
18835 hashval = ((((hashval << 4) + (hashval >> 24)) & 0x0fffffff)
18836 + row->glyphs[area][k].u.val
18837 + row->glyphs[area][k].face_id
18838 + row->glyphs[area][k].padding_p
18839 + (row->glyphs[area][k].type << 2));
18841 return hashval;
18844 /* Compute the pixel height and width of IT->glyph_row.
18846 Most of the time, ascent and height of a display line will be equal
18847 to the max_ascent and max_height values of the display iterator
18848 structure. This is not the case if
18850 1. We hit ZV without displaying anything. In this case, max_ascent
18851 and max_height will be zero.
18853 2. We have some glyphs that don't contribute to the line height.
18854 (The glyph row flag contributes_to_line_height_p is for future
18855 pixmap extensions).
18857 The first case is easily covered by using default values because in
18858 these cases, the line height does not really matter, except that it
18859 must not be zero. */
18861 static void
18862 compute_line_metrics (struct it *it)
18864 struct glyph_row *row = it->glyph_row;
18866 if (FRAME_WINDOW_P (it->f))
18868 int i, min_y, max_y;
18870 /* The line may consist of one space only, that was added to
18871 place the cursor on it. If so, the row's height hasn't been
18872 computed yet. */
18873 if (row->height == 0)
18875 if (it->max_ascent + it->max_descent == 0)
18876 it->max_descent = it->max_phys_descent = FRAME_LINE_HEIGHT (it->f);
18877 row->ascent = it->max_ascent;
18878 row->height = it->max_ascent + it->max_descent;
18879 row->phys_ascent = it->max_phys_ascent;
18880 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
18881 row->extra_line_spacing = it->max_extra_line_spacing;
18884 /* Compute the width of this line. */
18885 row->pixel_width = row->x;
18886 for (i = 0; i < row->used[TEXT_AREA]; ++i)
18887 row->pixel_width += row->glyphs[TEXT_AREA][i].pixel_width;
18889 eassert (row->pixel_width >= 0);
18890 eassert (row->ascent >= 0 && row->height > 0);
18892 row->overlapping_p = (MATRIX_ROW_OVERLAPS_SUCC_P (row)
18893 || MATRIX_ROW_OVERLAPS_PRED_P (row));
18895 /* If first line's physical ascent is larger than its logical
18896 ascent, use the physical ascent, and make the row taller.
18897 This makes accented characters fully visible. */
18898 if (row == MATRIX_FIRST_TEXT_ROW (it->w->desired_matrix)
18899 && row->phys_ascent > row->ascent)
18901 row->height += row->phys_ascent - row->ascent;
18902 row->ascent = row->phys_ascent;
18905 /* Compute how much of the line is visible. */
18906 row->visible_height = row->height;
18908 min_y = WINDOW_HEADER_LINE_HEIGHT (it->w);
18909 max_y = WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w);
18911 if (row->y < min_y)
18912 row->visible_height -= min_y - row->y;
18913 if (row->y + row->height > max_y)
18914 row->visible_height -= row->y + row->height - max_y;
18916 else
18918 row->pixel_width = row->used[TEXT_AREA];
18919 if (row->continued_p)
18920 row->pixel_width -= it->continuation_pixel_width;
18921 else if (row->truncated_on_right_p)
18922 row->pixel_width -= it->truncation_pixel_width;
18923 row->ascent = row->phys_ascent = 0;
18924 row->height = row->phys_height = row->visible_height = 1;
18925 row->extra_line_spacing = 0;
18928 /* Compute a hash code for this row. */
18929 row->hash = row_hash (row);
18931 it->max_ascent = it->max_descent = 0;
18932 it->max_phys_ascent = it->max_phys_descent = 0;
18936 /* Append one space to the glyph row of iterator IT if doing a
18937 window-based redisplay. The space has the same face as
18938 IT->face_id. Value is non-zero if a space was added.
18940 This function is called to make sure that there is always one glyph
18941 at the end of a glyph row that the cursor can be set on under
18942 window-systems. (If there weren't such a glyph we would not know
18943 how wide and tall a box cursor should be displayed).
18945 At the same time this space let's a nicely handle clearing to the
18946 end of the line if the row ends in italic text. */
18948 static int
18949 append_space_for_newline (struct it *it, int default_face_p)
18951 if (FRAME_WINDOW_P (it->f))
18953 int n = it->glyph_row->used[TEXT_AREA];
18955 if (it->glyph_row->glyphs[TEXT_AREA] + n
18956 < it->glyph_row->glyphs[1 + TEXT_AREA])
18958 /* Save some values that must not be changed.
18959 Must save IT->c and IT->len because otherwise
18960 ITERATOR_AT_END_P wouldn't work anymore after
18961 append_space_for_newline has been called. */
18962 enum display_element_type saved_what = it->what;
18963 int saved_c = it->c, saved_len = it->len;
18964 int saved_char_to_display = it->char_to_display;
18965 int saved_x = it->current_x;
18966 int saved_face_id = it->face_id;
18967 int saved_box_end = it->end_of_box_run_p;
18968 struct text_pos saved_pos;
18969 Lisp_Object saved_object;
18970 struct face *face;
18972 saved_object = it->object;
18973 saved_pos = it->position;
18975 it->what = IT_CHARACTER;
18976 memset (&it->position, 0, sizeof it->position);
18977 it->object = make_number (0);
18978 it->c = it->char_to_display = ' ';
18979 it->len = 1;
18981 /* If the default face was remapped, be sure to use the
18982 remapped face for the appended newline. */
18983 if (default_face_p)
18984 it->face_id = lookup_basic_face (it->f, DEFAULT_FACE_ID);
18985 else if (it->face_before_selective_p)
18986 it->face_id = it->saved_face_id;
18987 face = FACE_FROM_ID (it->f, it->face_id);
18988 it->face_id = FACE_FOR_CHAR (it->f, face, 0, -1, Qnil);
18989 /* In R2L rows, we will prepend a stretch glyph that will
18990 have the end_of_box_run_p flag set for it, so there's no
18991 need for the appended newline glyph to have that flag
18992 set. */
18993 if (it->glyph_row->reversed_p
18994 /* But if the appended newline glyph goes all the way to
18995 the end of the row, there will be no stretch glyph,
18996 so leave the box flag set. */
18997 && saved_x + FRAME_COLUMN_WIDTH (it->f) < it->last_visible_x)
18998 it->end_of_box_run_p = 0;
19000 PRODUCE_GLYPHS (it);
19002 it->override_ascent = -1;
19003 it->constrain_row_ascent_descent_p = 0;
19004 it->current_x = saved_x;
19005 it->object = saved_object;
19006 it->position = saved_pos;
19007 it->what = saved_what;
19008 it->face_id = saved_face_id;
19009 it->len = saved_len;
19010 it->c = saved_c;
19011 it->char_to_display = saved_char_to_display;
19012 it->end_of_box_run_p = saved_box_end;
19013 return 1;
19017 return 0;
19021 /* Extend the face of the last glyph in the text area of IT->glyph_row
19022 to the end of the display line. Called from display_line. If the
19023 glyph row is empty, add a space glyph to it so that we know the
19024 face to draw. Set the glyph row flag fill_line_p. If the glyph
19025 row is R2L, prepend a stretch glyph to cover the empty space to the
19026 left of the leftmost glyph. */
19028 static void
19029 extend_face_to_end_of_line (struct it *it)
19031 struct face *face, *default_face;
19032 struct frame *f = it->f;
19034 /* If line is already filled, do nothing. Non window-system frames
19035 get a grace of one more ``pixel'' because their characters are
19036 1-``pixel'' wide, so they hit the equality too early. This grace
19037 is needed only for R2L rows that are not continued, to produce
19038 one extra blank where we could display the cursor. */
19039 if ((it->current_x >= it->last_visible_x
19040 + (!FRAME_WINDOW_P (f)
19041 && it->glyph_row->reversed_p
19042 && !it->glyph_row->continued_p))
19043 /* If the window has display margins, we will need to extend
19044 their face even if the text area is filled. */
19045 && !(WINDOW_LEFT_MARGIN_WIDTH (it->w) > 0
19046 || WINDOW_RIGHT_MARGIN_WIDTH (it->w) > 0))
19047 return;
19049 /* The default face, possibly remapped. */
19050 default_face = FACE_FROM_ID (f, lookup_basic_face (f, DEFAULT_FACE_ID));
19052 /* Face extension extends the background and box of IT->face_id
19053 to the end of the line. If the background equals the background
19054 of the frame, we don't have to do anything. */
19055 if (it->face_before_selective_p)
19056 face = FACE_FROM_ID (f, it->saved_face_id);
19057 else
19058 face = FACE_FROM_ID (f, it->face_id);
19060 if (FRAME_WINDOW_P (f)
19061 && MATRIX_ROW_DISPLAYS_TEXT_P (it->glyph_row)
19062 && face->box == FACE_NO_BOX
19063 && face->background == FRAME_BACKGROUND_PIXEL (f)
19064 #ifdef HAVE_WINDOW_SYSTEM
19065 && !face->stipple
19066 #endif
19067 && !it->glyph_row->reversed_p)
19068 return;
19070 /* Set the glyph row flag indicating that the face of the last glyph
19071 in the text area has to be drawn to the end of the text area. */
19072 it->glyph_row->fill_line_p = 1;
19074 /* If current character of IT is not ASCII, make sure we have the
19075 ASCII face. This will be automatically undone the next time
19076 get_next_display_element returns a multibyte character. Note
19077 that the character will always be single byte in unibyte
19078 text. */
19079 if (!ASCII_CHAR_P (it->c))
19081 it->face_id = FACE_FOR_CHAR (f, face, 0, -1, Qnil);
19084 if (FRAME_WINDOW_P (f))
19086 /* If the row is empty, add a space with the current face of IT,
19087 so that we know which face to draw. */
19088 if (it->glyph_row->used[TEXT_AREA] == 0)
19090 it->glyph_row->glyphs[TEXT_AREA][0] = space_glyph;
19091 it->glyph_row->glyphs[TEXT_AREA][0].face_id = face->id;
19092 it->glyph_row->used[TEXT_AREA] = 1;
19094 /* Mode line and the header line don't have margins, and
19095 likewise the frame's tool-bar window, if there is any. */
19096 if (!(it->glyph_row->mode_line_p
19097 #if defined (HAVE_WINDOW_SYSTEM) && ! defined (USE_GTK) && ! defined (HAVE_NS)
19098 || (WINDOWP (f->tool_bar_window)
19099 && it->w == XWINDOW (f->tool_bar_window))
19100 #endif
19103 if (WINDOW_LEFT_MARGIN_WIDTH (it->w) > 0
19104 && it->glyph_row->used[LEFT_MARGIN_AREA] == 0)
19106 it->glyph_row->glyphs[LEFT_MARGIN_AREA][0] = space_glyph;
19107 it->glyph_row->glyphs[LEFT_MARGIN_AREA][0].face_id =
19108 default_face->id;
19109 it->glyph_row->used[LEFT_MARGIN_AREA] = 1;
19111 if (WINDOW_RIGHT_MARGIN_WIDTH (it->w) > 0
19112 && it->glyph_row->used[RIGHT_MARGIN_AREA] == 0)
19114 it->glyph_row->glyphs[RIGHT_MARGIN_AREA][0] = space_glyph;
19115 it->glyph_row->glyphs[RIGHT_MARGIN_AREA][0].face_id =
19116 default_face->id;
19117 it->glyph_row->used[RIGHT_MARGIN_AREA] = 1;
19120 #ifdef HAVE_WINDOW_SYSTEM
19121 if (it->glyph_row->reversed_p)
19123 /* Prepend a stretch glyph to the row, such that the
19124 rightmost glyph will be drawn flushed all the way to the
19125 right margin of the window. The stretch glyph that will
19126 occupy the empty space, if any, to the left of the
19127 glyphs. */
19128 struct font *font = face->font ? face->font : FRAME_FONT (f);
19129 struct glyph *row_start = it->glyph_row->glyphs[TEXT_AREA];
19130 struct glyph *row_end = row_start + it->glyph_row->used[TEXT_AREA];
19131 struct glyph *g;
19132 int row_width, stretch_ascent, stretch_width;
19133 struct text_pos saved_pos;
19134 int saved_face_id, saved_avoid_cursor, saved_box_start;
19136 for (row_width = 0, g = row_start; g < row_end; g++)
19137 row_width += g->pixel_width;
19138 stretch_width = window_box_width (it->w, TEXT_AREA) - row_width;
19139 if (stretch_width > 0)
19141 stretch_ascent =
19142 (((it->ascent + it->descent)
19143 * FONT_BASE (font)) / FONT_HEIGHT (font));
19144 saved_pos = it->position;
19145 memset (&it->position, 0, sizeof it->position);
19146 saved_avoid_cursor = it->avoid_cursor_p;
19147 it->avoid_cursor_p = 1;
19148 saved_face_id = it->face_id;
19149 saved_box_start = it->start_of_box_run_p;
19150 /* The last row's stretch glyph should get the default
19151 face, to avoid painting the rest of the window with
19152 the region face, if the region ends at ZV. */
19153 if (it->glyph_row->ends_at_zv_p)
19154 it->face_id = default_face->id;
19155 else
19156 it->face_id = face->id;
19157 it->start_of_box_run_p = 0;
19158 append_stretch_glyph (it, make_number (0), stretch_width,
19159 it->ascent + it->descent, stretch_ascent);
19160 it->position = saved_pos;
19161 it->avoid_cursor_p = saved_avoid_cursor;
19162 it->face_id = saved_face_id;
19163 it->start_of_box_run_p = saved_box_start;
19166 #endif /* HAVE_WINDOW_SYSTEM */
19168 else
19170 /* Save some values that must not be changed. */
19171 int saved_x = it->current_x;
19172 struct text_pos saved_pos;
19173 Lisp_Object saved_object;
19174 enum display_element_type saved_what = it->what;
19175 int saved_face_id = it->face_id;
19177 saved_object = it->object;
19178 saved_pos = it->position;
19180 it->what = IT_CHARACTER;
19181 memset (&it->position, 0, sizeof it->position);
19182 it->object = make_number (0);
19183 it->c = it->char_to_display = ' ';
19184 it->len = 1;
19186 if (WINDOW_LEFT_MARGIN_WIDTH (it->w) > 0
19187 && (it->glyph_row->used[LEFT_MARGIN_AREA]
19188 < WINDOW_LEFT_MARGIN_WIDTH (it->w))
19189 && !it->glyph_row->mode_line_p
19190 && default_face->background != FRAME_BACKGROUND_PIXEL (f))
19192 struct glyph *g = it->glyph_row->glyphs[LEFT_MARGIN_AREA];
19193 struct glyph *e = g + it->glyph_row->used[LEFT_MARGIN_AREA];
19195 for (it->current_x = 0; g < e; g++)
19196 it->current_x += g->pixel_width;
19198 it->area = LEFT_MARGIN_AREA;
19199 it->face_id = default_face->id;
19200 while (it->glyph_row->used[LEFT_MARGIN_AREA]
19201 < WINDOW_LEFT_MARGIN_WIDTH (it->w))
19203 PRODUCE_GLYPHS (it);
19204 /* term.c:produce_glyphs advances it->current_x only for
19205 TEXT_AREA. */
19206 it->current_x += it->pixel_width;
19209 it->current_x = saved_x;
19210 it->area = TEXT_AREA;
19213 /* The last row's blank glyphs should get the default face, to
19214 avoid painting the rest of the window with the region face,
19215 if the region ends at ZV. */
19216 if (it->glyph_row->ends_at_zv_p)
19217 it->face_id = default_face->id;
19218 else
19219 it->face_id = face->id;
19220 PRODUCE_GLYPHS (it);
19222 while (it->current_x <= it->last_visible_x)
19223 PRODUCE_GLYPHS (it);
19225 if (WINDOW_RIGHT_MARGIN_WIDTH (it->w) > 0
19226 && (it->glyph_row->used[RIGHT_MARGIN_AREA]
19227 < WINDOW_RIGHT_MARGIN_WIDTH (it->w))
19228 && !it->glyph_row->mode_line_p
19229 && default_face->background != FRAME_BACKGROUND_PIXEL (f))
19231 struct glyph *g = it->glyph_row->glyphs[RIGHT_MARGIN_AREA];
19232 struct glyph *e = g + it->glyph_row->used[RIGHT_MARGIN_AREA];
19234 for ( ; g < e; g++)
19235 it->current_x += g->pixel_width;
19237 it->area = RIGHT_MARGIN_AREA;
19238 it->face_id = default_face->id;
19239 while (it->glyph_row->used[RIGHT_MARGIN_AREA]
19240 < WINDOW_RIGHT_MARGIN_WIDTH (it->w))
19242 PRODUCE_GLYPHS (it);
19243 it->current_x += it->pixel_width;
19246 it->area = TEXT_AREA;
19249 /* Don't count these blanks really. It would let us insert a left
19250 truncation glyph below and make us set the cursor on them, maybe. */
19251 it->current_x = saved_x;
19252 it->object = saved_object;
19253 it->position = saved_pos;
19254 it->what = saved_what;
19255 it->face_id = saved_face_id;
19260 /* Value is non-zero if text starting at CHARPOS in current_buffer is
19261 trailing whitespace. */
19263 static int
19264 trailing_whitespace_p (ptrdiff_t charpos)
19266 ptrdiff_t bytepos = CHAR_TO_BYTE (charpos);
19267 int c = 0;
19269 while (bytepos < ZV_BYTE
19270 && (c = FETCH_CHAR (bytepos),
19271 c == ' ' || c == '\t'))
19272 ++bytepos;
19274 if (bytepos >= ZV_BYTE || c == '\n' || c == '\r')
19276 if (bytepos != PT_BYTE)
19277 return 1;
19279 return 0;
19283 /* Highlight trailing whitespace, if any, in ROW. */
19285 static void
19286 highlight_trailing_whitespace (struct frame *f, struct glyph_row *row)
19288 int used = row->used[TEXT_AREA];
19290 if (used)
19292 struct glyph *start = row->glyphs[TEXT_AREA];
19293 struct glyph *glyph = start + used - 1;
19295 if (row->reversed_p)
19297 /* Right-to-left rows need to be processed in the opposite
19298 direction, so swap the edge pointers. */
19299 glyph = start;
19300 start = row->glyphs[TEXT_AREA] + used - 1;
19303 /* Skip over glyphs inserted to display the cursor at the
19304 end of a line, for extending the face of the last glyph
19305 to the end of the line on terminals, and for truncation
19306 and continuation glyphs. */
19307 if (!row->reversed_p)
19309 while (glyph >= start
19310 && glyph->type == CHAR_GLYPH
19311 && INTEGERP (glyph->object))
19312 --glyph;
19314 else
19316 while (glyph <= start
19317 && glyph->type == CHAR_GLYPH
19318 && INTEGERP (glyph->object))
19319 ++glyph;
19322 /* If last glyph is a space or stretch, and it's trailing
19323 whitespace, set the face of all trailing whitespace glyphs in
19324 IT->glyph_row to `trailing-whitespace'. */
19325 if ((row->reversed_p ? glyph <= start : glyph >= start)
19326 && BUFFERP (glyph->object)
19327 && (glyph->type == STRETCH_GLYPH
19328 || (glyph->type == CHAR_GLYPH
19329 && glyph->u.ch == ' '))
19330 && trailing_whitespace_p (glyph->charpos))
19332 int face_id = lookup_named_face (f, Qtrailing_whitespace, 0);
19333 if (face_id < 0)
19334 return;
19336 if (!row->reversed_p)
19338 while (glyph >= start
19339 && BUFFERP (glyph->object)
19340 && (glyph->type == STRETCH_GLYPH
19341 || (glyph->type == CHAR_GLYPH
19342 && glyph->u.ch == ' ')))
19343 (glyph--)->face_id = face_id;
19345 else
19347 while (glyph <= start
19348 && BUFFERP (glyph->object)
19349 && (glyph->type == STRETCH_GLYPH
19350 || (glyph->type == CHAR_GLYPH
19351 && glyph->u.ch == ' ')))
19352 (glyph++)->face_id = face_id;
19359 /* Value is non-zero if glyph row ROW should be
19360 considered to hold the buffer position CHARPOS. */
19362 static int
19363 row_for_charpos_p (struct glyph_row *row, ptrdiff_t charpos)
19365 int result = 1;
19367 if (charpos == CHARPOS (row->end.pos)
19368 || charpos == MATRIX_ROW_END_CHARPOS (row))
19370 /* Suppose the row ends on a string.
19371 Unless the row is continued, that means it ends on a newline
19372 in the string. If it's anything other than a display string
19373 (e.g., a before-string from an overlay), we don't want the
19374 cursor there. (This heuristic seems to give the optimal
19375 behavior for the various types of multi-line strings.)
19376 One exception: if the string has `cursor' property on one of
19377 its characters, we _do_ want the cursor there. */
19378 if (CHARPOS (row->end.string_pos) >= 0)
19380 if (row->continued_p)
19381 result = 1;
19382 else
19384 /* Check for `display' property. */
19385 struct glyph *beg = row->glyphs[TEXT_AREA];
19386 struct glyph *end = beg + row->used[TEXT_AREA] - 1;
19387 struct glyph *glyph;
19389 result = 0;
19390 for (glyph = end; glyph >= beg; --glyph)
19391 if (STRINGP (glyph->object))
19393 Lisp_Object prop
19394 = Fget_char_property (make_number (charpos),
19395 Qdisplay, Qnil);
19396 result =
19397 (!NILP (prop)
19398 && display_prop_string_p (prop, glyph->object));
19399 /* If there's a `cursor' property on one of the
19400 string's characters, this row is a cursor row,
19401 even though this is not a display string. */
19402 if (!result)
19404 Lisp_Object s = glyph->object;
19406 for ( ; glyph >= beg && EQ (glyph->object, s); --glyph)
19408 ptrdiff_t gpos = glyph->charpos;
19410 if (!NILP (Fget_char_property (make_number (gpos),
19411 Qcursor, s)))
19413 result = 1;
19414 break;
19418 break;
19422 else if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
19424 /* If the row ends in middle of a real character,
19425 and the line is continued, we want the cursor here.
19426 That's because CHARPOS (ROW->end.pos) would equal
19427 PT if PT is before the character. */
19428 if (!row->ends_in_ellipsis_p)
19429 result = row->continued_p;
19430 else
19431 /* If the row ends in an ellipsis, then
19432 CHARPOS (ROW->end.pos) will equal point after the
19433 invisible text. We want that position to be displayed
19434 after the ellipsis. */
19435 result = 0;
19437 /* If the row ends at ZV, display the cursor at the end of that
19438 row instead of at the start of the row below. */
19439 else if (row->ends_at_zv_p)
19440 result = 1;
19441 else
19442 result = 0;
19445 return result;
19448 /* Value is non-zero if glyph row ROW should be
19449 used to hold the cursor. */
19451 static int
19452 cursor_row_p (struct glyph_row *row)
19454 return row_for_charpos_p (row, PT);
19459 /* Push the property PROP so that it will be rendered at the current
19460 position in IT. Return 1 if PROP was successfully pushed, 0
19461 otherwise. Called from handle_line_prefix to handle the
19462 `line-prefix' and `wrap-prefix' properties. */
19464 static int
19465 push_prefix_prop (struct it *it, Lisp_Object prop)
19467 struct text_pos pos =
19468 STRINGP (it->string) ? it->current.string_pos : it->current.pos;
19470 eassert (it->method == GET_FROM_BUFFER
19471 || it->method == GET_FROM_DISPLAY_VECTOR
19472 || it->method == GET_FROM_STRING);
19474 /* We need to save the current buffer/string position, so it will be
19475 restored by pop_it, because iterate_out_of_display_property
19476 depends on that being set correctly, but some situations leave
19477 it->position not yet set when this function is called. */
19478 push_it (it, &pos);
19480 if (STRINGP (prop))
19482 if (SCHARS (prop) == 0)
19484 pop_it (it);
19485 return 0;
19488 it->string = prop;
19489 it->string_from_prefix_prop_p = 1;
19490 it->multibyte_p = STRING_MULTIBYTE (it->string);
19491 it->current.overlay_string_index = -1;
19492 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
19493 it->end_charpos = it->string_nchars = SCHARS (it->string);
19494 it->method = GET_FROM_STRING;
19495 it->stop_charpos = 0;
19496 it->prev_stop = 0;
19497 it->base_level_stop = 0;
19499 /* Force paragraph direction to be that of the parent
19500 buffer/string. */
19501 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
19502 it->paragraph_embedding = it->bidi_it.paragraph_dir;
19503 else
19504 it->paragraph_embedding = L2R;
19506 /* Set up the bidi iterator for this display string. */
19507 if (it->bidi_p)
19509 it->bidi_it.string.lstring = it->string;
19510 it->bidi_it.string.s = NULL;
19511 it->bidi_it.string.schars = it->end_charpos;
19512 it->bidi_it.string.bufpos = IT_CHARPOS (*it);
19513 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
19514 it->bidi_it.string.unibyte = !it->multibyte_p;
19515 it->bidi_it.w = it->w;
19516 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
19519 else if (CONSP (prop) && EQ (XCAR (prop), Qspace))
19521 it->method = GET_FROM_STRETCH;
19522 it->object = prop;
19524 #ifdef HAVE_WINDOW_SYSTEM
19525 else if (IMAGEP (prop))
19527 it->what = IT_IMAGE;
19528 it->image_id = lookup_image (it->f, prop);
19529 it->method = GET_FROM_IMAGE;
19531 #endif /* HAVE_WINDOW_SYSTEM */
19532 else
19534 pop_it (it); /* bogus display property, give up */
19535 return 0;
19538 return 1;
19541 /* Return the character-property PROP at the current position in IT. */
19543 static Lisp_Object
19544 get_it_property (struct it *it, Lisp_Object prop)
19546 Lisp_Object position, object = it->object;
19548 if (STRINGP (object))
19549 position = make_number (IT_STRING_CHARPOS (*it));
19550 else if (BUFFERP (object))
19552 position = make_number (IT_CHARPOS (*it));
19553 object = it->window;
19555 else
19556 return Qnil;
19558 return Fget_char_property (position, prop, object);
19561 /* See if there's a line- or wrap-prefix, and if so, push it on IT. */
19563 static void
19564 handle_line_prefix (struct it *it)
19566 Lisp_Object prefix;
19568 if (it->continuation_lines_width > 0)
19570 prefix = get_it_property (it, Qwrap_prefix);
19571 if (NILP (prefix))
19572 prefix = Vwrap_prefix;
19574 else
19576 prefix = get_it_property (it, Qline_prefix);
19577 if (NILP (prefix))
19578 prefix = Vline_prefix;
19580 if (! NILP (prefix) && push_prefix_prop (it, prefix))
19582 /* If the prefix is wider than the window, and we try to wrap
19583 it, it would acquire its own wrap prefix, and so on till the
19584 iterator stack overflows. So, don't wrap the prefix. */
19585 it->line_wrap = TRUNCATE;
19586 it->avoid_cursor_p = 1;
19592 /* Remove N glyphs at the start of a reversed IT->glyph_row. Called
19593 only for R2L lines from display_line and display_string, when they
19594 decide that too many glyphs were produced by PRODUCE_GLYPHS, and
19595 the line/string needs to be continued on the next glyph row. */
19596 static void
19597 unproduce_glyphs (struct it *it, int n)
19599 struct glyph *glyph, *end;
19601 eassert (it->glyph_row);
19602 eassert (it->glyph_row->reversed_p);
19603 eassert (it->area == TEXT_AREA);
19604 eassert (n <= it->glyph_row->used[TEXT_AREA]);
19606 if (n > it->glyph_row->used[TEXT_AREA])
19607 n = it->glyph_row->used[TEXT_AREA];
19608 glyph = it->glyph_row->glyphs[TEXT_AREA] + n;
19609 end = it->glyph_row->glyphs[TEXT_AREA] + it->glyph_row->used[TEXT_AREA];
19610 for ( ; glyph < end; glyph++)
19611 glyph[-n] = *glyph;
19614 /* Find the positions in a bidi-reordered ROW to serve as ROW->minpos
19615 and ROW->maxpos. */
19616 static void
19617 find_row_edges (struct it *it, struct glyph_row *row,
19618 ptrdiff_t min_pos, ptrdiff_t min_bpos,
19619 ptrdiff_t max_pos, ptrdiff_t max_bpos)
19621 /* FIXME: Revisit this when glyph ``spilling'' in continuation
19622 lines' rows is implemented for bidi-reordered rows. */
19624 /* ROW->minpos is the value of min_pos, the minimal buffer position
19625 we have in ROW, or ROW->start.pos if that is smaller. */
19626 if (min_pos <= ZV && min_pos < row->start.pos.charpos)
19627 SET_TEXT_POS (row->minpos, min_pos, min_bpos);
19628 else
19629 /* We didn't find buffer positions smaller than ROW->start, or
19630 didn't find _any_ valid buffer positions in any of the glyphs,
19631 so we must trust the iterator's computed positions. */
19632 row->minpos = row->start.pos;
19633 if (max_pos <= 0)
19635 max_pos = CHARPOS (it->current.pos);
19636 max_bpos = BYTEPOS (it->current.pos);
19639 /* Here are the various use-cases for ending the row, and the
19640 corresponding values for ROW->maxpos:
19642 Line ends in a newline from buffer eol_pos + 1
19643 Line is continued from buffer max_pos + 1
19644 Line is truncated on right it->current.pos
19645 Line ends in a newline from string max_pos + 1(*)
19646 (*) + 1 only when line ends in a forward scan
19647 Line is continued from string max_pos
19648 Line is continued from display vector max_pos
19649 Line is entirely from a string min_pos == max_pos
19650 Line is entirely from a display vector min_pos == max_pos
19651 Line that ends at ZV ZV
19653 If you discover other use-cases, please add them here as
19654 appropriate. */
19655 if (row->ends_at_zv_p)
19656 row->maxpos = it->current.pos;
19657 else if (row->used[TEXT_AREA])
19659 int seen_this_string = 0;
19660 struct glyph_row *r1 = row - 1;
19662 /* Did we see the same display string on the previous row? */
19663 if (STRINGP (it->object)
19664 /* this is not the first row */
19665 && row > it->w->desired_matrix->rows
19666 /* previous row is not the header line */
19667 && !r1->mode_line_p
19668 /* previous row also ends in a newline from a string */
19669 && r1->ends_in_newline_from_string_p)
19671 struct glyph *start, *end;
19673 /* Search for the last glyph of the previous row that came
19674 from buffer or string. Depending on whether the row is
19675 L2R or R2L, we need to process it front to back or the
19676 other way round. */
19677 if (!r1->reversed_p)
19679 start = r1->glyphs[TEXT_AREA];
19680 end = start + r1->used[TEXT_AREA];
19681 /* Glyphs inserted by redisplay have an integer (zero)
19682 as their object. */
19683 while (end > start
19684 && INTEGERP ((end - 1)->object)
19685 && (end - 1)->charpos <= 0)
19686 --end;
19687 if (end > start)
19689 if (EQ ((end - 1)->object, it->object))
19690 seen_this_string = 1;
19692 else
19693 /* If all the glyphs of the previous row were inserted
19694 by redisplay, it means the previous row was
19695 produced from a single newline, which is only
19696 possible if that newline came from the same string
19697 as the one which produced this ROW. */
19698 seen_this_string = 1;
19700 else
19702 end = r1->glyphs[TEXT_AREA] - 1;
19703 start = end + r1->used[TEXT_AREA];
19704 while (end < start
19705 && INTEGERP ((end + 1)->object)
19706 && (end + 1)->charpos <= 0)
19707 ++end;
19708 if (end < start)
19710 if (EQ ((end + 1)->object, it->object))
19711 seen_this_string = 1;
19713 else
19714 seen_this_string = 1;
19717 /* Take note of each display string that covers a newline only
19718 once, the first time we see it. This is for when a display
19719 string includes more than one newline in it. */
19720 if (row->ends_in_newline_from_string_p && !seen_this_string)
19722 /* If we were scanning the buffer forward when we displayed
19723 the string, we want to account for at least one buffer
19724 position that belongs to this row (position covered by
19725 the display string), so that cursor positioning will
19726 consider this row as a candidate when point is at the end
19727 of the visual line represented by this row. This is not
19728 required when scanning back, because max_pos will already
19729 have a much larger value. */
19730 if (CHARPOS (row->end.pos) > max_pos)
19731 INC_BOTH (max_pos, max_bpos);
19732 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
19734 else if (CHARPOS (it->eol_pos) > 0)
19735 SET_TEXT_POS (row->maxpos,
19736 CHARPOS (it->eol_pos) + 1, BYTEPOS (it->eol_pos) + 1);
19737 else if (row->continued_p)
19739 /* If max_pos is different from IT's current position, it
19740 means IT->method does not belong to the display element
19741 at max_pos. However, it also means that the display
19742 element at max_pos was displayed in its entirety on this
19743 line, which is equivalent to saying that the next line
19744 starts at the next buffer position. */
19745 if (IT_CHARPOS (*it) == max_pos && it->method != GET_FROM_BUFFER)
19746 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
19747 else
19749 INC_BOTH (max_pos, max_bpos);
19750 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
19753 else if (row->truncated_on_right_p)
19754 /* display_line already called reseat_at_next_visible_line_start,
19755 which puts the iterator at the beginning of the next line, in
19756 the logical order. */
19757 row->maxpos = it->current.pos;
19758 else if (max_pos == min_pos && it->method != GET_FROM_BUFFER)
19759 /* A line that is entirely from a string/image/stretch... */
19760 row->maxpos = row->minpos;
19761 else
19762 emacs_abort ();
19764 else
19765 row->maxpos = it->current.pos;
19768 /* Construct the glyph row IT->glyph_row in the desired matrix of
19769 IT->w from text at the current position of IT. See dispextern.h
19770 for an overview of struct it. Value is non-zero if
19771 IT->glyph_row displays text, as opposed to a line displaying ZV
19772 only. */
19774 static int
19775 display_line (struct it *it)
19777 struct glyph_row *row = it->glyph_row;
19778 Lisp_Object overlay_arrow_string;
19779 struct it wrap_it;
19780 void *wrap_data = NULL;
19781 int may_wrap = 0, wrap_x IF_LINT (= 0);
19782 int wrap_row_used = -1;
19783 int wrap_row_ascent IF_LINT (= 0), wrap_row_height IF_LINT (= 0);
19784 int wrap_row_phys_ascent IF_LINT (= 0), wrap_row_phys_height IF_LINT (= 0);
19785 int wrap_row_extra_line_spacing IF_LINT (= 0);
19786 ptrdiff_t wrap_row_min_pos IF_LINT (= 0), wrap_row_min_bpos IF_LINT (= 0);
19787 ptrdiff_t wrap_row_max_pos IF_LINT (= 0), wrap_row_max_bpos IF_LINT (= 0);
19788 int cvpos;
19789 ptrdiff_t min_pos = ZV + 1, max_pos = 0;
19790 ptrdiff_t min_bpos IF_LINT (= 0), max_bpos IF_LINT (= 0);
19792 /* We always start displaying at hpos zero even if hscrolled. */
19793 eassert (it->hpos == 0 && it->current_x == 0);
19795 if (MATRIX_ROW_VPOS (row, it->w->desired_matrix)
19796 >= it->w->desired_matrix->nrows)
19798 it->w->nrows_scale_factor++;
19799 it->f->fonts_changed = 1;
19800 return 0;
19803 /* Clear the result glyph row and enable it. */
19804 prepare_desired_row (row);
19806 row->y = it->current_y;
19807 row->start = it->start;
19808 row->continuation_lines_width = it->continuation_lines_width;
19809 row->displays_text_p = 1;
19810 row->starts_in_middle_of_char_p = it->starts_in_middle_of_char_p;
19811 it->starts_in_middle_of_char_p = 0;
19813 /* Arrange the overlays nicely for our purposes. Usually, we call
19814 display_line on only one line at a time, in which case this
19815 can't really hurt too much, or we call it on lines which appear
19816 one after another in the buffer, in which case all calls to
19817 recenter_overlay_lists but the first will be pretty cheap. */
19818 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
19820 /* Move over display elements that are not visible because we are
19821 hscrolled. This may stop at an x-position < IT->first_visible_x
19822 if the first glyph is partially visible or if we hit a line end. */
19823 if (it->current_x < it->first_visible_x)
19825 enum move_it_result move_result;
19827 this_line_min_pos = row->start.pos;
19828 move_result = move_it_in_display_line_to (it, ZV, it->first_visible_x,
19829 MOVE_TO_POS | MOVE_TO_X);
19830 /* If we are under a large hscroll, move_it_in_display_line_to
19831 could hit the end of the line without reaching
19832 it->first_visible_x. Pretend that we did reach it. This is
19833 especially important on a TTY, where we will call
19834 extend_face_to_end_of_line, which needs to know how many
19835 blank glyphs to produce. */
19836 if (it->current_x < it->first_visible_x
19837 && (move_result == MOVE_NEWLINE_OR_CR
19838 || move_result == MOVE_POS_MATCH_OR_ZV))
19839 it->current_x = it->first_visible_x;
19841 /* Record the smallest positions seen while we moved over
19842 display elements that are not visible. This is needed by
19843 redisplay_internal for optimizing the case where the cursor
19844 stays inside the same line. The rest of this function only
19845 considers positions that are actually displayed, so
19846 RECORD_MAX_MIN_POS will not otherwise record positions that
19847 are hscrolled to the left of the left edge of the window. */
19848 min_pos = CHARPOS (this_line_min_pos);
19849 min_bpos = BYTEPOS (this_line_min_pos);
19851 else
19853 /* We only do this when not calling `move_it_in_display_line_to'
19854 above, because move_it_in_display_line_to calls
19855 handle_line_prefix itself. */
19856 handle_line_prefix (it);
19859 /* Get the initial row height. This is either the height of the
19860 text hscrolled, if there is any, or zero. */
19861 row->ascent = it->max_ascent;
19862 row->height = it->max_ascent + it->max_descent;
19863 row->phys_ascent = it->max_phys_ascent;
19864 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
19865 row->extra_line_spacing = it->max_extra_line_spacing;
19867 /* Utility macro to record max and min buffer positions seen until now. */
19868 #define RECORD_MAX_MIN_POS(IT) \
19869 do \
19871 int composition_p = !STRINGP ((IT)->string) \
19872 && ((IT)->what == IT_COMPOSITION); \
19873 ptrdiff_t current_pos = \
19874 composition_p ? (IT)->cmp_it.charpos \
19875 : IT_CHARPOS (*(IT)); \
19876 ptrdiff_t current_bpos = \
19877 composition_p ? CHAR_TO_BYTE (current_pos) \
19878 : IT_BYTEPOS (*(IT)); \
19879 if (current_pos < min_pos) \
19881 min_pos = current_pos; \
19882 min_bpos = current_bpos; \
19884 if (IT_CHARPOS (*it) > max_pos) \
19886 max_pos = IT_CHARPOS (*it); \
19887 max_bpos = IT_BYTEPOS (*it); \
19890 while (0)
19892 /* Loop generating characters. The loop is left with IT on the next
19893 character to display. */
19894 while (1)
19896 int n_glyphs_before, hpos_before, x_before;
19897 int x, nglyphs;
19898 int ascent = 0, descent = 0, phys_ascent = 0, phys_descent = 0;
19900 /* Retrieve the next thing to display. Value is zero if end of
19901 buffer reached. */
19902 if (!get_next_display_element (it))
19904 /* Maybe add a space at the end of this line that is used to
19905 display the cursor there under X. Set the charpos of the
19906 first glyph of blank lines not corresponding to any text
19907 to -1. */
19908 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
19909 row->exact_window_width_line_p = 1;
19910 else if ((append_space_for_newline (it, 1) && row->used[TEXT_AREA] == 1)
19911 || row->used[TEXT_AREA] == 0)
19913 row->glyphs[TEXT_AREA]->charpos = -1;
19914 row->displays_text_p = 0;
19916 if (!NILP (BVAR (XBUFFER (it->w->contents), indicate_empty_lines))
19917 && (!MINI_WINDOW_P (it->w)
19918 || (minibuf_level && EQ (it->window, minibuf_window))))
19919 row->indicate_empty_line_p = 1;
19922 it->continuation_lines_width = 0;
19923 row->ends_at_zv_p = 1;
19924 /* A row that displays right-to-left text must always have
19925 its last face extended all the way to the end of line,
19926 even if this row ends in ZV, because we still write to
19927 the screen left to right. We also need to extend the
19928 last face if the default face is remapped to some
19929 different face, otherwise the functions that clear
19930 portions of the screen will clear with the default face's
19931 background color. */
19932 if (row->reversed_p
19933 || lookup_basic_face (it->f, DEFAULT_FACE_ID) != DEFAULT_FACE_ID)
19934 extend_face_to_end_of_line (it);
19935 break;
19938 /* Now, get the metrics of what we want to display. This also
19939 generates glyphs in `row' (which is IT->glyph_row). */
19940 n_glyphs_before = row->used[TEXT_AREA];
19941 x = it->current_x;
19943 /* Remember the line height so far in case the next element doesn't
19944 fit on the line. */
19945 if (it->line_wrap != TRUNCATE)
19947 ascent = it->max_ascent;
19948 descent = it->max_descent;
19949 phys_ascent = it->max_phys_ascent;
19950 phys_descent = it->max_phys_descent;
19952 if (it->line_wrap == WORD_WRAP && it->area == TEXT_AREA)
19954 if (IT_DISPLAYING_WHITESPACE (it))
19955 may_wrap = 1;
19956 else if (may_wrap)
19958 SAVE_IT (wrap_it, *it, wrap_data);
19959 wrap_x = x;
19960 wrap_row_used = row->used[TEXT_AREA];
19961 wrap_row_ascent = row->ascent;
19962 wrap_row_height = row->height;
19963 wrap_row_phys_ascent = row->phys_ascent;
19964 wrap_row_phys_height = row->phys_height;
19965 wrap_row_extra_line_spacing = row->extra_line_spacing;
19966 wrap_row_min_pos = min_pos;
19967 wrap_row_min_bpos = min_bpos;
19968 wrap_row_max_pos = max_pos;
19969 wrap_row_max_bpos = max_bpos;
19970 may_wrap = 0;
19975 PRODUCE_GLYPHS (it);
19977 /* If this display element was in marginal areas, continue with
19978 the next one. */
19979 if (it->area != TEXT_AREA)
19981 row->ascent = max (row->ascent, it->max_ascent);
19982 row->height = max (row->height, it->max_ascent + it->max_descent);
19983 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
19984 row->phys_height = max (row->phys_height,
19985 it->max_phys_ascent + it->max_phys_descent);
19986 row->extra_line_spacing = max (row->extra_line_spacing,
19987 it->max_extra_line_spacing);
19988 set_iterator_to_next (it, 1);
19989 continue;
19992 /* Does the display element fit on the line? If we truncate
19993 lines, we should draw past the right edge of the window. If
19994 we don't truncate, we want to stop so that we can display the
19995 continuation glyph before the right margin. If lines are
19996 continued, there are two possible strategies for characters
19997 resulting in more than 1 glyph (e.g. tabs): Display as many
19998 glyphs as possible in this line and leave the rest for the
19999 continuation line, or display the whole element in the next
20000 line. Original redisplay did the former, so we do it also. */
20001 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
20002 hpos_before = it->hpos;
20003 x_before = x;
20005 if (/* Not a newline. */
20006 nglyphs > 0
20007 /* Glyphs produced fit entirely in the line. */
20008 && it->current_x < it->last_visible_x)
20010 it->hpos += nglyphs;
20011 row->ascent = max (row->ascent, it->max_ascent);
20012 row->height = max (row->height, it->max_ascent + it->max_descent);
20013 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
20014 row->phys_height = max (row->phys_height,
20015 it->max_phys_ascent + it->max_phys_descent);
20016 row->extra_line_spacing = max (row->extra_line_spacing,
20017 it->max_extra_line_spacing);
20018 if (it->current_x - it->pixel_width < it->first_visible_x)
20019 row->x = x - it->first_visible_x;
20020 /* Record the maximum and minimum buffer positions seen so
20021 far in glyphs that will be displayed by this row. */
20022 if (it->bidi_p)
20023 RECORD_MAX_MIN_POS (it);
20025 else
20027 int i, new_x;
20028 struct glyph *glyph;
20030 for (i = 0; i < nglyphs; ++i, x = new_x)
20032 glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
20033 new_x = x + glyph->pixel_width;
20035 if (/* Lines are continued. */
20036 it->line_wrap != TRUNCATE
20037 && (/* Glyph doesn't fit on the line. */
20038 new_x > it->last_visible_x
20039 /* Or it fits exactly on a window system frame. */
20040 || (new_x == it->last_visible_x
20041 && FRAME_WINDOW_P (it->f)
20042 && (row->reversed_p
20043 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
20044 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)))))
20046 /* End of a continued line. */
20048 if (it->hpos == 0
20049 || (new_x == it->last_visible_x
20050 && FRAME_WINDOW_P (it->f)
20051 && (row->reversed_p
20052 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
20053 : WINDOW_RIGHT_FRINGE_WIDTH (it->w))))
20055 /* Current glyph is the only one on the line or
20056 fits exactly on the line. We must continue
20057 the line because we can't draw the cursor
20058 after the glyph. */
20059 row->continued_p = 1;
20060 it->current_x = new_x;
20061 it->continuation_lines_width += new_x;
20062 ++it->hpos;
20063 if (i == nglyphs - 1)
20065 /* If line-wrap is on, check if a previous
20066 wrap point was found. */
20067 if (wrap_row_used > 0
20068 /* Even if there is a previous wrap
20069 point, continue the line here as
20070 usual, if (i) the previous character
20071 was a space or tab AND (ii) the
20072 current character is not. */
20073 && (!may_wrap
20074 || IT_DISPLAYING_WHITESPACE (it)))
20075 goto back_to_wrap;
20077 /* Record the maximum and minimum buffer
20078 positions seen so far in glyphs that will be
20079 displayed by this row. */
20080 if (it->bidi_p)
20081 RECORD_MAX_MIN_POS (it);
20082 set_iterator_to_next (it, 1);
20083 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
20085 if (!get_next_display_element (it))
20087 row->exact_window_width_line_p = 1;
20088 it->continuation_lines_width = 0;
20089 row->continued_p = 0;
20090 row->ends_at_zv_p = 1;
20092 else if (ITERATOR_AT_END_OF_LINE_P (it))
20094 row->continued_p = 0;
20095 row->exact_window_width_line_p = 1;
20099 else if (it->bidi_p)
20100 RECORD_MAX_MIN_POS (it);
20101 if (WINDOW_LEFT_MARGIN_WIDTH (it->w) > 0
20102 || WINDOW_RIGHT_MARGIN_WIDTH (it->w) > 0)
20103 extend_face_to_end_of_line (it);
20105 else if (CHAR_GLYPH_PADDING_P (*glyph)
20106 && !FRAME_WINDOW_P (it->f))
20108 /* A padding glyph that doesn't fit on this line.
20109 This means the whole character doesn't fit
20110 on the line. */
20111 if (row->reversed_p)
20112 unproduce_glyphs (it, row->used[TEXT_AREA]
20113 - n_glyphs_before);
20114 row->used[TEXT_AREA] = n_glyphs_before;
20116 /* Fill the rest of the row with continuation
20117 glyphs like in 20.x. */
20118 while (row->glyphs[TEXT_AREA] + row->used[TEXT_AREA]
20119 < row->glyphs[1 + TEXT_AREA])
20120 produce_special_glyphs (it, IT_CONTINUATION);
20122 row->continued_p = 1;
20123 it->current_x = x_before;
20124 it->continuation_lines_width += x_before;
20126 /* Restore the height to what it was before the
20127 element not fitting on the line. */
20128 it->max_ascent = ascent;
20129 it->max_descent = descent;
20130 it->max_phys_ascent = phys_ascent;
20131 it->max_phys_descent = phys_descent;
20132 if (WINDOW_LEFT_MARGIN_WIDTH (it->w) > 0
20133 || WINDOW_RIGHT_MARGIN_WIDTH (it->w) > 0)
20134 extend_face_to_end_of_line (it);
20136 else if (wrap_row_used > 0)
20138 back_to_wrap:
20139 if (row->reversed_p)
20140 unproduce_glyphs (it,
20141 row->used[TEXT_AREA] - wrap_row_used);
20142 RESTORE_IT (it, &wrap_it, wrap_data);
20143 it->continuation_lines_width += wrap_x;
20144 row->used[TEXT_AREA] = wrap_row_used;
20145 row->ascent = wrap_row_ascent;
20146 row->height = wrap_row_height;
20147 row->phys_ascent = wrap_row_phys_ascent;
20148 row->phys_height = wrap_row_phys_height;
20149 row->extra_line_spacing = wrap_row_extra_line_spacing;
20150 min_pos = wrap_row_min_pos;
20151 min_bpos = wrap_row_min_bpos;
20152 max_pos = wrap_row_max_pos;
20153 max_bpos = wrap_row_max_bpos;
20154 row->continued_p = 1;
20155 row->ends_at_zv_p = 0;
20156 row->exact_window_width_line_p = 0;
20157 it->continuation_lines_width += x;
20159 /* Make sure that a non-default face is extended
20160 up to the right margin of the window. */
20161 extend_face_to_end_of_line (it);
20163 else if (it->c == '\t' && FRAME_WINDOW_P (it->f))
20165 /* A TAB that extends past the right edge of the
20166 window. This produces a single glyph on
20167 window system frames. We leave the glyph in
20168 this row and let it fill the row, but don't
20169 consume the TAB. */
20170 if ((row->reversed_p
20171 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
20172 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
20173 produce_special_glyphs (it, IT_CONTINUATION);
20174 it->continuation_lines_width += it->last_visible_x;
20175 row->ends_in_middle_of_char_p = 1;
20176 row->continued_p = 1;
20177 glyph->pixel_width = it->last_visible_x - x;
20178 it->starts_in_middle_of_char_p = 1;
20179 if (WINDOW_LEFT_MARGIN_WIDTH (it->w) > 0
20180 || WINDOW_RIGHT_MARGIN_WIDTH (it->w) > 0)
20181 extend_face_to_end_of_line (it);
20183 else
20185 /* Something other than a TAB that draws past
20186 the right edge of the window. Restore
20187 positions to values before the element. */
20188 if (row->reversed_p)
20189 unproduce_glyphs (it, row->used[TEXT_AREA]
20190 - (n_glyphs_before + i));
20191 row->used[TEXT_AREA] = n_glyphs_before + i;
20193 /* Display continuation glyphs. */
20194 it->current_x = x_before;
20195 it->continuation_lines_width += x;
20196 if (!FRAME_WINDOW_P (it->f)
20197 || (row->reversed_p
20198 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
20199 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
20200 produce_special_glyphs (it, IT_CONTINUATION);
20201 row->continued_p = 1;
20203 extend_face_to_end_of_line (it);
20205 if (nglyphs > 1 && i > 0)
20207 row->ends_in_middle_of_char_p = 1;
20208 it->starts_in_middle_of_char_p = 1;
20211 /* Restore the height to what it was before the
20212 element not fitting on the line. */
20213 it->max_ascent = ascent;
20214 it->max_descent = descent;
20215 it->max_phys_ascent = phys_ascent;
20216 it->max_phys_descent = phys_descent;
20219 break;
20221 else if (new_x > it->first_visible_x)
20223 /* Increment number of glyphs actually displayed. */
20224 ++it->hpos;
20226 /* Record the maximum and minimum buffer positions
20227 seen so far in glyphs that will be displayed by
20228 this row. */
20229 if (it->bidi_p)
20230 RECORD_MAX_MIN_POS (it);
20232 if (x < it->first_visible_x)
20233 /* Glyph is partially visible, i.e. row starts at
20234 negative X position. */
20235 row->x = x - it->first_visible_x;
20237 else
20239 /* Glyph is completely off the left margin of the
20240 window. This should not happen because of the
20241 move_it_in_display_line at the start of this
20242 function, unless the text display area of the
20243 window is empty. */
20244 eassert (it->first_visible_x <= it->last_visible_x);
20247 /* Even if this display element produced no glyphs at all,
20248 we want to record its position. */
20249 if (it->bidi_p && nglyphs == 0)
20250 RECORD_MAX_MIN_POS (it);
20252 row->ascent = max (row->ascent, it->max_ascent);
20253 row->height = max (row->height, it->max_ascent + it->max_descent);
20254 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
20255 row->phys_height = max (row->phys_height,
20256 it->max_phys_ascent + it->max_phys_descent);
20257 row->extra_line_spacing = max (row->extra_line_spacing,
20258 it->max_extra_line_spacing);
20260 /* End of this display line if row is continued. */
20261 if (row->continued_p || row->ends_at_zv_p)
20262 break;
20265 at_end_of_line:
20266 /* Is this a line end? If yes, we're also done, after making
20267 sure that a non-default face is extended up to the right
20268 margin of the window. */
20269 if (ITERATOR_AT_END_OF_LINE_P (it))
20271 int used_before = row->used[TEXT_AREA];
20273 row->ends_in_newline_from_string_p = STRINGP (it->object);
20275 /* Add a space at the end of the line that is used to
20276 display the cursor there. */
20277 if (!IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
20278 append_space_for_newline (it, 0);
20280 /* Extend the face to the end of the line. */
20281 extend_face_to_end_of_line (it);
20283 /* Make sure we have the position. */
20284 if (used_before == 0)
20285 row->glyphs[TEXT_AREA]->charpos = CHARPOS (it->position);
20287 /* Record the position of the newline, for use in
20288 find_row_edges. */
20289 it->eol_pos = it->current.pos;
20291 /* Consume the line end. This skips over invisible lines. */
20292 set_iterator_to_next (it, 1);
20293 it->continuation_lines_width = 0;
20294 break;
20297 /* Proceed with next display element. Note that this skips
20298 over lines invisible because of selective display. */
20299 set_iterator_to_next (it, 1);
20301 /* If we truncate lines, we are done when the last displayed
20302 glyphs reach past the right margin of the window. */
20303 if (it->line_wrap == TRUNCATE
20304 && ((FRAME_WINDOW_P (it->f)
20305 /* Images are preprocessed in produce_image_glyph such
20306 that they are cropped at the right edge of the
20307 window, so an image glyph will always end exactly at
20308 last_visible_x, even if there's no right fringe. */
20309 && (WINDOW_RIGHT_FRINGE_WIDTH (it->w) || it->what == IT_IMAGE))
20310 ? (it->current_x >= it->last_visible_x)
20311 : (it->current_x > it->last_visible_x)))
20313 /* Maybe add truncation glyphs. */
20314 if (!FRAME_WINDOW_P (it->f)
20315 || (row->reversed_p
20316 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
20317 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
20319 int i, n;
20321 if (!row->reversed_p)
20323 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
20324 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
20325 break;
20327 else
20329 for (i = 0; i < row->used[TEXT_AREA]; i++)
20330 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
20331 break;
20332 /* Remove any padding glyphs at the front of ROW, to
20333 make room for the truncation glyphs we will be
20334 adding below. The loop below always inserts at
20335 least one truncation glyph, so also remove the
20336 last glyph added to ROW. */
20337 unproduce_glyphs (it, i + 1);
20338 /* Adjust i for the loop below. */
20339 i = row->used[TEXT_AREA] - (i + 1);
20342 /* produce_special_glyphs overwrites the last glyph, so
20343 we don't want that if we want to keep that last
20344 glyph, which means it's an image. */
20345 if (it->current_x > it->last_visible_x)
20347 it->current_x = x_before;
20348 if (!FRAME_WINDOW_P (it->f))
20350 for (n = row->used[TEXT_AREA]; i < n; ++i)
20352 row->used[TEXT_AREA] = i;
20353 produce_special_glyphs (it, IT_TRUNCATION);
20356 else
20358 row->used[TEXT_AREA] = i;
20359 produce_special_glyphs (it, IT_TRUNCATION);
20361 it->hpos = hpos_before;
20364 else if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
20366 /* Don't truncate if we can overflow newline into fringe. */
20367 if (!get_next_display_element (it))
20369 it->continuation_lines_width = 0;
20370 row->ends_at_zv_p = 1;
20371 row->exact_window_width_line_p = 1;
20372 break;
20374 if (ITERATOR_AT_END_OF_LINE_P (it))
20376 row->exact_window_width_line_p = 1;
20377 goto at_end_of_line;
20379 it->current_x = x_before;
20380 it->hpos = hpos_before;
20383 row->truncated_on_right_p = 1;
20384 it->continuation_lines_width = 0;
20385 reseat_at_next_visible_line_start (it, 0);
20386 row->ends_at_zv_p = FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n';
20387 break;
20391 if (wrap_data)
20392 bidi_unshelve_cache (wrap_data, 1);
20394 /* If line is not empty and hscrolled, maybe insert truncation glyphs
20395 at the left window margin. */
20396 if (it->first_visible_x
20397 && IT_CHARPOS (*it) != CHARPOS (row->start.pos))
20399 if (!FRAME_WINDOW_P (it->f)
20400 || (((row->reversed_p
20401 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
20402 : WINDOW_LEFT_FRINGE_WIDTH (it->w)) == 0)
20403 /* Don't let insert_left_trunc_glyphs overwrite the
20404 first glyph of the row if it is an image. */
20405 && row->glyphs[TEXT_AREA]->type != IMAGE_GLYPH))
20406 insert_left_trunc_glyphs (it);
20407 row->truncated_on_left_p = 1;
20410 /* Remember the position at which this line ends.
20412 BIDI Note: any code that needs MATRIX_ROW_START/END_CHARPOS
20413 cannot be before the call to find_row_edges below, since that is
20414 where these positions are determined. */
20415 row->end = it->current;
20416 if (!it->bidi_p)
20418 row->minpos = row->start.pos;
20419 row->maxpos = row->end.pos;
20421 else
20423 /* ROW->minpos and ROW->maxpos must be the smallest and
20424 `1 + the largest' buffer positions in ROW. But if ROW was
20425 bidi-reordered, these two positions can be anywhere in the
20426 row, so we must determine them now. */
20427 find_row_edges (it, row, min_pos, min_bpos, max_pos, max_bpos);
20430 /* If the start of this line is the overlay arrow-position, then
20431 mark this glyph row as the one containing the overlay arrow.
20432 This is clearly a mess with variable size fonts. It would be
20433 better to let it be displayed like cursors under X. */
20434 if ((MATRIX_ROW_DISPLAYS_TEXT_P (row) || !overlay_arrow_seen)
20435 && (overlay_arrow_string = overlay_arrow_at_row (it, row),
20436 !NILP (overlay_arrow_string)))
20438 /* Overlay arrow in window redisplay is a fringe bitmap. */
20439 if (STRINGP (overlay_arrow_string))
20441 struct glyph_row *arrow_row
20442 = get_overlay_arrow_glyph_row (it->w, overlay_arrow_string);
20443 struct glyph *glyph = arrow_row->glyphs[TEXT_AREA];
20444 struct glyph *arrow_end = glyph + arrow_row->used[TEXT_AREA];
20445 struct glyph *p = row->glyphs[TEXT_AREA];
20446 struct glyph *p2, *end;
20448 /* Copy the arrow glyphs. */
20449 while (glyph < arrow_end)
20450 *p++ = *glyph++;
20452 /* Throw away padding glyphs. */
20453 p2 = p;
20454 end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
20455 while (p2 < end && CHAR_GLYPH_PADDING_P (*p2))
20456 ++p2;
20457 if (p2 > p)
20459 while (p2 < end)
20460 *p++ = *p2++;
20461 row->used[TEXT_AREA] = p2 - row->glyphs[TEXT_AREA];
20464 else
20466 eassert (INTEGERP (overlay_arrow_string));
20467 row->overlay_arrow_bitmap = XINT (overlay_arrow_string);
20469 overlay_arrow_seen = 1;
20472 /* Highlight trailing whitespace. */
20473 if (!NILP (Vshow_trailing_whitespace))
20474 highlight_trailing_whitespace (it->f, it->glyph_row);
20476 /* Compute pixel dimensions of this line. */
20477 compute_line_metrics (it);
20479 /* Implementation note: No changes in the glyphs of ROW or in their
20480 faces can be done past this point, because compute_line_metrics
20481 computes ROW's hash value and stores it within the glyph_row
20482 structure. */
20484 /* Record whether this row ends inside an ellipsis. */
20485 row->ends_in_ellipsis_p
20486 = (it->method == GET_FROM_DISPLAY_VECTOR
20487 && it->ellipsis_p);
20489 /* Save fringe bitmaps in this row. */
20490 row->left_user_fringe_bitmap = it->left_user_fringe_bitmap;
20491 row->left_user_fringe_face_id = it->left_user_fringe_face_id;
20492 row->right_user_fringe_bitmap = it->right_user_fringe_bitmap;
20493 row->right_user_fringe_face_id = it->right_user_fringe_face_id;
20495 it->left_user_fringe_bitmap = 0;
20496 it->left_user_fringe_face_id = 0;
20497 it->right_user_fringe_bitmap = 0;
20498 it->right_user_fringe_face_id = 0;
20500 /* Maybe set the cursor. */
20501 cvpos = it->w->cursor.vpos;
20502 if ((cvpos < 0
20503 /* In bidi-reordered rows, keep checking for proper cursor
20504 position even if one has been found already, because buffer
20505 positions in such rows change non-linearly with ROW->VPOS,
20506 when a line is continued. One exception: when we are at ZV,
20507 display cursor on the first suitable glyph row, since all
20508 the empty rows after that also have their position set to ZV. */
20509 /* FIXME: Revisit this when glyph ``spilling'' in continuation
20510 lines' rows is implemented for bidi-reordered rows. */
20511 || (it->bidi_p
20512 && !MATRIX_ROW (it->w->desired_matrix, cvpos)->ends_at_zv_p))
20513 && PT >= MATRIX_ROW_START_CHARPOS (row)
20514 && PT <= MATRIX_ROW_END_CHARPOS (row)
20515 && cursor_row_p (row))
20516 set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
20518 /* Prepare for the next line. This line starts horizontally at (X
20519 HPOS) = (0 0). Vertical positions are incremented. As a
20520 convenience for the caller, IT->glyph_row is set to the next
20521 row to be used. */
20522 it->current_x = it->hpos = 0;
20523 it->current_y += row->height;
20524 SET_TEXT_POS (it->eol_pos, 0, 0);
20525 ++it->vpos;
20526 ++it->glyph_row;
20527 /* The next row should by default use the same value of the
20528 reversed_p flag as this one. set_iterator_to_next decides when
20529 it's a new paragraph, and PRODUCE_GLYPHS recomputes the value of
20530 the flag accordingly. */
20531 if (it->glyph_row < MATRIX_BOTTOM_TEXT_ROW (it->w->desired_matrix, it->w))
20532 it->glyph_row->reversed_p = row->reversed_p;
20533 it->start = row->end;
20534 return MATRIX_ROW_DISPLAYS_TEXT_P (row);
20536 #undef RECORD_MAX_MIN_POS
20539 DEFUN ("current-bidi-paragraph-direction", Fcurrent_bidi_paragraph_direction,
20540 Scurrent_bidi_paragraph_direction, 0, 1, 0,
20541 doc: /* Return paragraph direction at point in BUFFER.
20542 Value is either `left-to-right' or `right-to-left'.
20543 If BUFFER is omitted or nil, it defaults to the current buffer.
20545 Paragraph direction determines how the text in the paragraph is displayed.
20546 In left-to-right paragraphs, text begins at the left margin of the window
20547 and the reading direction is generally left to right. In right-to-left
20548 paragraphs, text begins at the right margin and is read from right to left.
20550 See also `bidi-paragraph-direction'. */)
20551 (Lisp_Object buffer)
20553 struct buffer *buf = current_buffer;
20554 struct buffer *old = buf;
20556 if (! NILP (buffer))
20558 CHECK_BUFFER (buffer);
20559 buf = XBUFFER (buffer);
20562 if (NILP (BVAR (buf, bidi_display_reordering))
20563 || NILP (BVAR (buf, enable_multibyte_characters))
20564 /* When we are loading loadup.el, the character property tables
20565 needed for bidi iteration are not yet available. */
20566 || !NILP (Vpurify_flag))
20567 return Qleft_to_right;
20568 else if (!NILP (BVAR (buf, bidi_paragraph_direction)))
20569 return BVAR (buf, bidi_paragraph_direction);
20570 else
20572 /* Determine the direction from buffer text. We could try to
20573 use current_matrix if it is up to date, but this seems fast
20574 enough as it is. */
20575 struct bidi_it itb;
20576 ptrdiff_t pos = BUF_PT (buf);
20577 ptrdiff_t bytepos = BUF_PT_BYTE (buf);
20578 int c;
20579 void *itb_data = bidi_shelve_cache ();
20581 set_buffer_temp (buf);
20582 /* bidi_paragraph_init finds the base direction of the paragraph
20583 by searching forward from paragraph start. We need the base
20584 direction of the current or _previous_ paragraph, so we need
20585 to make sure we are within that paragraph. To that end, find
20586 the previous non-empty line. */
20587 if (pos >= ZV && pos > BEGV)
20588 DEC_BOTH (pos, bytepos);
20589 if (fast_looking_at (build_string ("[\f\t ]*\n"),
20590 pos, bytepos, ZV, ZV_BYTE, Qnil) > 0)
20592 while ((c = FETCH_BYTE (bytepos)) == '\n'
20593 || c == ' ' || c == '\t' || c == '\f')
20595 if (bytepos <= BEGV_BYTE)
20596 break;
20597 bytepos--;
20598 pos--;
20600 while (!CHAR_HEAD_P (FETCH_BYTE (bytepos)))
20601 bytepos--;
20603 bidi_init_it (pos, bytepos, FRAME_WINDOW_P (SELECTED_FRAME ()), &itb);
20604 itb.paragraph_dir = NEUTRAL_DIR;
20605 itb.string.s = NULL;
20606 itb.string.lstring = Qnil;
20607 itb.string.bufpos = 0;
20608 itb.string.from_disp_str = 0;
20609 itb.string.unibyte = 0;
20610 /* We have no window to use here for ignoring window-specific
20611 overlays. Using NULL for window pointer will cause
20612 compute_display_string_pos to use the current buffer. */
20613 itb.w = NULL;
20614 bidi_paragraph_init (NEUTRAL_DIR, &itb, 1);
20615 bidi_unshelve_cache (itb_data, 0);
20616 set_buffer_temp (old);
20617 switch (itb.paragraph_dir)
20619 case L2R:
20620 return Qleft_to_right;
20621 break;
20622 case R2L:
20623 return Qright_to_left;
20624 break;
20625 default:
20626 emacs_abort ();
20631 DEFUN ("move-point-visually", Fmove_point_visually,
20632 Smove_point_visually, 1, 1, 0,
20633 doc: /* Move point in the visual order in the specified DIRECTION.
20634 DIRECTION can be 1, meaning move to the right, or -1, which moves to the
20635 left.
20637 Value is the new character position of point. */)
20638 (Lisp_Object direction)
20640 struct window *w = XWINDOW (selected_window);
20641 struct buffer *b = XBUFFER (w->contents);
20642 struct glyph_row *row;
20643 int dir;
20644 Lisp_Object paragraph_dir;
20646 #define ROW_GLYPH_NEWLINE_P(ROW,GLYPH) \
20647 (!(ROW)->continued_p \
20648 && INTEGERP ((GLYPH)->object) \
20649 && (GLYPH)->type == CHAR_GLYPH \
20650 && (GLYPH)->u.ch == ' ' \
20651 && (GLYPH)->charpos >= 0 \
20652 && !(GLYPH)->avoid_cursor_p)
20654 CHECK_NUMBER (direction);
20655 dir = XINT (direction);
20656 if (dir > 0)
20657 dir = 1;
20658 else
20659 dir = -1;
20661 /* If current matrix is up-to-date, we can use the information
20662 recorded in the glyphs, at least as long as the goal is on the
20663 screen. */
20664 if (w->window_end_valid
20665 && !windows_or_buffers_changed
20666 && b
20667 && !b->clip_changed
20668 && !b->prevent_redisplay_optimizations_p
20669 && !window_outdated (w)
20670 && w->cursor.vpos >= 0
20671 && w->cursor.vpos < w->current_matrix->nrows
20672 && (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos))->enabled_p)
20674 struct glyph *g = row->glyphs[TEXT_AREA];
20675 struct glyph *e = dir > 0 ? g + row->used[TEXT_AREA] : g - 1;
20676 struct glyph *gpt = g + w->cursor.hpos;
20678 for (g = gpt + dir; (dir > 0 ? g < e : g > e); g += dir)
20680 if (BUFFERP (g->object) && g->charpos != PT)
20682 SET_PT (g->charpos);
20683 w->cursor.vpos = -1;
20684 return make_number (PT);
20686 else if (!INTEGERP (g->object) && !EQ (g->object, gpt->object))
20688 ptrdiff_t new_pos;
20690 if (BUFFERP (gpt->object))
20692 new_pos = PT;
20693 if ((gpt->resolved_level - row->reversed_p) % 2 == 0)
20694 new_pos += (row->reversed_p ? -dir : dir);
20695 else
20696 new_pos -= (row->reversed_p ? -dir : dir);;
20698 else if (BUFFERP (g->object))
20699 new_pos = g->charpos;
20700 else
20701 break;
20702 SET_PT (new_pos);
20703 w->cursor.vpos = -1;
20704 return make_number (PT);
20706 else if (ROW_GLYPH_NEWLINE_P (row, g))
20708 /* Glyphs inserted at the end of a non-empty line for
20709 positioning the cursor have zero charpos, so we must
20710 deduce the value of point by other means. */
20711 if (g->charpos > 0)
20712 SET_PT (g->charpos);
20713 else if (row->ends_at_zv_p && PT != ZV)
20714 SET_PT (ZV);
20715 else if (PT != MATRIX_ROW_END_CHARPOS (row) - 1)
20716 SET_PT (MATRIX_ROW_END_CHARPOS (row) - 1);
20717 else
20718 break;
20719 w->cursor.vpos = -1;
20720 return make_number (PT);
20723 if (g == e || INTEGERP (g->object))
20725 if (row->truncated_on_left_p || row->truncated_on_right_p)
20726 goto simulate_display;
20727 if (!row->reversed_p)
20728 row += dir;
20729 else
20730 row -= dir;
20731 if (row < MATRIX_FIRST_TEXT_ROW (w->current_matrix)
20732 || row > MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w))
20733 goto simulate_display;
20735 if (dir > 0)
20737 if (row->reversed_p && !row->continued_p)
20739 SET_PT (MATRIX_ROW_END_CHARPOS (row) - 1);
20740 w->cursor.vpos = -1;
20741 return make_number (PT);
20743 g = row->glyphs[TEXT_AREA];
20744 e = g + row->used[TEXT_AREA];
20745 for ( ; g < e; g++)
20747 if (BUFFERP (g->object)
20748 /* Empty lines have only one glyph, which stands
20749 for the newline, and whose charpos is the
20750 buffer position of the newline. */
20751 || ROW_GLYPH_NEWLINE_P (row, g)
20752 /* When the buffer ends in a newline, the line at
20753 EOB also has one glyph, but its charpos is -1. */
20754 || (row->ends_at_zv_p
20755 && !row->reversed_p
20756 && INTEGERP (g->object)
20757 && g->type == CHAR_GLYPH
20758 && g->u.ch == ' '))
20760 if (g->charpos > 0)
20761 SET_PT (g->charpos);
20762 else if (!row->reversed_p
20763 && row->ends_at_zv_p
20764 && PT != ZV)
20765 SET_PT (ZV);
20766 else
20767 continue;
20768 w->cursor.vpos = -1;
20769 return make_number (PT);
20773 else
20775 if (!row->reversed_p && !row->continued_p)
20777 SET_PT (MATRIX_ROW_END_CHARPOS (row) - 1);
20778 w->cursor.vpos = -1;
20779 return make_number (PT);
20781 e = row->glyphs[TEXT_AREA];
20782 g = e + row->used[TEXT_AREA] - 1;
20783 for ( ; g >= e; g--)
20785 if (BUFFERP (g->object)
20786 || (ROW_GLYPH_NEWLINE_P (row, g)
20787 && g->charpos > 0)
20788 /* Empty R2L lines on GUI frames have the buffer
20789 position of the newline stored in the stretch
20790 glyph. */
20791 || g->type == STRETCH_GLYPH
20792 || (row->ends_at_zv_p
20793 && row->reversed_p
20794 && INTEGERP (g->object)
20795 && g->type == CHAR_GLYPH
20796 && g->u.ch == ' '))
20798 if (g->charpos > 0)
20799 SET_PT (g->charpos);
20800 else if (row->reversed_p
20801 && row->ends_at_zv_p
20802 && PT != ZV)
20803 SET_PT (ZV);
20804 else
20805 continue;
20806 w->cursor.vpos = -1;
20807 return make_number (PT);
20814 simulate_display:
20816 /* If we wind up here, we failed to move by using the glyphs, so we
20817 need to simulate display instead. */
20819 if (b)
20820 paragraph_dir = Fcurrent_bidi_paragraph_direction (w->contents);
20821 else
20822 paragraph_dir = Qleft_to_right;
20823 if (EQ (paragraph_dir, Qright_to_left))
20824 dir = -dir;
20825 if (PT <= BEGV && dir < 0)
20826 xsignal0 (Qbeginning_of_buffer);
20827 else if (PT >= ZV && dir > 0)
20828 xsignal0 (Qend_of_buffer);
20829 else
20831 struct text_pos pt;
20832 struct it it;
20833 int pt_x, target_x, pixel_width, pt_vpos;
20834 bool at_eol_p;
20835 bool overshoot_expected = false;
20836 bool target_is_eol_p = false;
20838 /* Setup the arena. */
20839 SET_TEXT_POS (pt, PT, PT_BYTE);
20840 start_display (&it, w, pt);
20842 if (it.cmp_it.id < 0
20843 && it.method == GET_FROM_STRING
20844 && it.area == TEXT_AREA
20845 && it.string_from_display_prop_p
20846 && (it.sp > 0 && it.stack[it.sp - 1].method == GET_FROM_BUFFER))
20847 overshoot_expected = true;
20849 /* Find the X coordinate of point. We start from the beginning
20850 of this or previous line to make sure we are before point in
20851 the logical order (since the move_it_* functions can only
20852 move forward). */
20853 reseat:
20854 reseat_at_previous_visible_line_start (&it);
20855 it.current_x = it.hpos = it.current_y = it.vpos = 0;
20856 if (IT_CHARPOS (it) != PT)
20858 move_it_to (&it, overshoot_expected ? PT - 1 : PT,
20859 -1, -1, -1, MOVE_TO_POS);
20860 /* If we missed point because the character there is
20861 displayed out of a display vector that has more than one
20862 glyph, retry expecting overshoot. */
20863 if (it.method == GET_FROM_DISPLAY_VECTOR
20864 && it.current.dpvec_index > 0
20865 && !overshoot_expected)
20867 overshoot_expected = true;
20868 goto reseat;
20870 else if (IT_CHARPOS (it) != PT && !overshoot_expected)
20871 move_it_in_display_line (&it, PT, -1, MOVE_TO_POS);
20873 pt_x = it.current_x;
20874 pt_vpos = it.vpos;
20875 if (dir > 0 || overshoot_expected)
20877 struct glyph_row *row = it.glyph_row;
20879 /* When point is at beginning of line, we don't have
20880 information about the glyph there loaded into struct
20881 it. Calling get_next_display_element fixes that. */
20882 if (pt_x == 0)
20883 get_next_display_element (&it);
20884 at_eol_p = ITERATOR_AT_END_OF_LINE_P (&it);
20885 it.glyph_row = NULL;
20886 PRODUCE_GLYPHS (&it); /* compute it.pixel_width */
20887 it.glyph_row = row;
20888 /* PRODUCE_GLYPHS advances it.current_x, so we must restore
20889 it, lest it will become out of sync with it's buffer
20890 position. */
20891 it.current_x = pt_x;
20893 else
20894 at_eol_p = ITERATOR_AT_END_OF_LINE_P (&it);
20895 pixel_width = it.pixel_width;
20896 if (overshoot_expected && at_eol_p)
20897 pixel_width = 0;
20898 else if (pixel_width <= 0)
20899 pixel_width = 1;
20901 /* If there's a display string (or something similar) at point,
20902 we are actually at the glyph to the left of point, so we need
20903 to correct the X coordinate. */
20904 if (overshoot_expected)
20906 if (it.bidi_p)
20907 pt_x += pixel_width * it.bidi_it.scan_dir;
20908 else
20909 pt_x += pixel_width;
20912 /* Compute target X coordinate, either to the left or to the
20913 right of point. On TTY frames, all characters have the same
20914 pixel width of 1, so we can use that. On GUI frames we don't
20915 have an easy way of getting at the pixel width of the
20916 character to the left of point, so we use a different method
20917 of getting to that place. */
20918 if (dir > 0)
20919 target_x = pt_x + pixel_width;
20920 else
20921 target_x = pt_x - (!FRAME_WINDOW_P (it.f)) * pixel_width;
20923 /* Target X coordinate could be one line above or below the line
20924 of point, in which case we need to adjust the target X
20925 coordinate. Also, if moving to the left, we need to begin at
20926 the left edge of the point's screen line. */
20927 if (dir < 0)
20929 if (pt_x > 0)
20931 start_display (&it, w, pt);
20932 reseat_at_previous_visible_line_start (&it);
20933 it.current_x = it.current_y = it.hpos = 0;
20934 if (pt_vpos != 0)
20935 move_it_by_lines (&it, pt_vpos);
20937 else
20939 move_it_by_lines (&it, -1);
20940 target_x = it.last_visible_x - !FRAME_WINDOW_P (it.f);
20941 target_is_eol_p = true;
20942 /* Under word-wrap, we don't know the x coordinate of
20943 the last character displayed on the previous line,
20944 which immediately precedes the wrap point. To find
20945 out its x coordinate, we try moving to the right
20946 margin of the window, which will stop at the wrap
20947 point, and then reset target_x to point at the
20948 character that precedes the wrap point. This is not
20949 needed on GUI frames, because (see below) there we
20950 move from the left margin one grapheme cluster at a
20951 time, and stop when we hit the wrap point. */
20952 if (!FRAME_WINDOW_P (it.f) && it.line_wrap == WORD_WRAP)
20954 void *it_data = NULL;
20955 struct it it2;
20957 SAVE_IT (it2, it, it_data);
20958 move_it_in_display_line_to (&it, ZV, target_x,
20959 MOVE_TO_POS | MOVE_TO_X);
20960 /* If we arrived at target_x, that _is_ the last
20961 character on the previous line. */
20962 if (it.current_x != target_x)
20963 target_x = it.current_x - 1;
20964 RESTORE_IT (&it, &it2, it_data);
20968 else
20970 if (at_eol_p
20971 || (target_x >= it.last_visible_x
20972 && it.line_wrap != TRUNCATE))
20974 if (pt_x > 0)
20975 move_it_by_lines (&it, 0);
20976 move_it_by_lines (&it, 1);
20977 target_x = 0;
20981 /* Move to the target X coordinate. */
20982 #ifdef HAVE_WINDOW_SYSTEM
20983 /* On GUI frames, as we don't know the X coordinate of the
20984 character to the left of point, moving point to the left
20985 requires walking, one grapheme cluster at a time, until we
20986 find ourself at a place immediately to the left of the
20987 character at point. */
20988 if (FRAME_WINDOW_P (it.f) && dir < 0)
20990 struct text_pos new_pos;
20991 enum move_it_result rc = MOVE_X_REACHED;
20993 if (it.current_x == 0)
20994 get_next_display_element (&it);
20995 if (it.what == IT_COMPOSITION)
20997 new_pos.charpos = it.cmp_it.charpos;
20998 new_pos.bytepos = -1;
21000 else
21001 new_pos = it.current.pos;
21003 while (it.current_x + it.pixel_width <= target_x
21004 && (rc == MOVE_X_REACHED
21005 /* Under word-wrap, move_it_in_display_line_to
21006 stops at correct coordinates, but sometimes
21007 returns MOVE_POS_MATCH_OR_ZV. */
21008 || (it.line_wrap == WORD_WRAP
21009 && rc == MOVE_POS_MATCH_OR_ZV)))
21011 int new_x = it.current_x + it.pixel_width;
21013 /* For composed characters, we want the position of the
21014 first character in the grapheme cluster (usually, the
21015 composition's base character), whereas it.current
21016 might give us the position of the _last_ one, e.g. if
21017 the composition is rendered in reverse due to bidi
21018 reordering. */
21019 if (it.what == IT_COMPOSITION)
21021 new_pos.charpos = it.cmp_it.charpos;
21022 new_pos.bytepos = -1;
21024 else
21025 new_pos = it.current.pos;
21026 if (new_x == it.current_x)
21027 new_x++;
21028 rc = move_it_in_display_line_to (&it, ZV, new_x,
21029 MOVE_TO_POS | MOVE_TO_X);
21030 if (ITERATOR_AT_END_OF_LINE_P (&it) && !target_is_eol_p)
21031 break;
21033 /* The previous position we saw in the loop is the one we
21034 want. */
21035 if (new_pos.bytepos == -1)
21036 new_pos.bytepos = CHAR_TO_BYTE (new_pos.charpos);
21037 it.current.pos = new_pos;
21039 else
21040 #endif
21041 if (it.current_x != target_x)
21042 move_it_in_display_line_to (&it, ZV, target_x, MOVE_TO_POS | MOVE_TO_X);
21044 /* When lines are truncated, the above loop will stop at the
21045 window edge. But we want to get to the end of line, even if
21046 it is beyond the window edge; automatic hscroll will then
21047 scroll the window to show point as appropriate. */
21048 if (target_is_eol_p && it.line_wrap == TRUNCATE
21049 && get_next_display_element (&it))
21051 struct text_pos new_pos = it.current.pos;
21053 while (!ITERATOR_AT_END_OF_LINE_P (&it))
21055 set_iterator_to_next (&it, 0);
21056 if (it.method == GET_FROM_BUFFER)
21057 new_pos = it.current.pos;
21058 if (!get_next_display_element (&it))
21059 break;
21062 it.current.pos = new_pos;
21065 /* If we ended up in a display string that covers point, move to
21066 buffer position to the right in the visual order. */
21067 if (dir > 0)
21069 while (IT_CHARPOS (it) == PT)
21071 set_iterator_to_next (&it, 0);
21072 if (!get_next_display_element (&it))
21073 break;
21077 /* Move point to that position. */
21078 SET_PT_BOTH (IT_CHARPOS (it), IT_BYTEPOS (it));
21081 return make_number (PT);
21083 #undef ROW_GLYPH_NEWLINE_P
21087 /***********************************************************************
21088 Menu Bar
21089 ***********************************************************************/
21091 /* Redisplay the menu bar in the frame for window W.
21093 The menu bar of X frames that don't have X toolkit support is
21094 displayed in a special window W->frame->menu_bar_window.
21096 The menu bar of terminal frames is treated specially as far as
21097 glyph matrices are concerned. Menu bar lines are not part of
21098 windows, so the update is done directly on the frame matrix rows
21099 for the menu bar. */
21101 static void
21102 display_menu_bar (struct window *w)
21104 struct frame *f = XFRAME (WINDOW_FRAME (w));
21105 struct it it;
21106 Lisp_Object items;
21107 int i;
21109 /* Don't do all this for graphical frames. */
21110 #ifdef HAVE_NTGUI
21111 if (FRAME_W32_P (f))
21112 return;
21113 #endif
21114 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
21115 if (FRAME_X_P (f))
21116 return;
21117 #endif
21119 #ifdef HAVE_NS
21120 if (FRAME_NS_P (f))
21121 return;
21122 #endif /* HAVE_NS */
21124 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
21125 eassert (!FRAME_WINDOW_P (f));
21126 init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MENU_FACE_ID);
21127 it.first_visible_x = 0;
21128 it.last_visible_x = FRAME_PIXEL_WIDTH (f);
21129 #elif defined (HAVE_X_WINDOWS) /* X without toolkit. */
21130 if (FRAME_WINDOW_P (f))
21132 /* Menu bar lines are displayed in the desired matrix of the
21133 dummy window menu_bar_window. */
21134 struct window *menu_w;
21135 menu_w = XWINDOW (f->menu_bar_window);
21136 init_iterator (&it, menu_w, -1, -1, menu_w->desired_matrix->rows,
21137 MENU_FACE_ID);
21138 it.first_visible_x = 0;
21139 it.last_visible_x = FRAME_PIXEL_WIDTH (f);
21141 else
21142 #endif /* not USE_X_TOOLKIT and not USE_GTK */
21144 /* This is a TTY frame, i.e. character hpos/vpos are used as
21145 pixel x/y. */
21146 init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
21147 MENU_FACE_ID);
21148 it.first_visible_x = 0;
21149 it.last_visible_x = FRAME_COLS (f);
21152 /* FIXME: This should be controlled by a user option. See the
21153 comments in redisplay_tool_bar and display_mode_line about
21154 this. */
21155 it.paragraph_embedding = L2R;
21157 /* Clear all rows of the menu bar. */
21158 for (i = 0; i < FRAME_MENU_BAR_LINES (f); ++i)
21160 struct glyph_row *row = it.glyph_row + i;
21161 clear_glyph_row (row);
21162 row->enabled_p = true;
21163 row->full_width_p = 1;
21166 /* Display all items of the menu bar. */
21167 items = FRAME_MENU_BAR_ITEMS (it.f);
21168 for (i = 0; i < ASIZE (items); i += 4)
21170 Lisp_Object string;
21172 /* Stop at nil string. */
21173 string = AREF (items, i + 1);
21174 if (NILP (string))
21175 break;
21177 /* Remember where item was displayed. */
21178 ASET (items, i + 3, make_number (it.hpos));
21180 /* Display the item, pad with one space. */
21181 if (it.current_x < it.last_visible_x)
21182 display_string (NULL, string, Qnil, 0, 0, &it,
21183 SCHARS (string) + 1, 0, 0, -1);
21186 /* Fill out the line with spaces. */
21187 if (it.current_x < it.last_visible_x)
21188 display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
21190 /* Compute the total height of the lines. */
21191 compute_line_metrics (&it);
21194 /* Deep copy of a glyph row, including the glyphs. */
21195 static void
21196 deep_copy_glyph_row (struct glyph_row *to, struct glyph_row *from)
21198 struct glyph *pointers[1 + LAST_AREA];
21199 int to_used = to->used[TEXT_AREA];
21201 /* Save glyph pointers of TO. */
21202 memcpy (pointers, to->glyphs, sizeof to->glyphs);
21204 /* Do a structure assignment. */
21205 *to = *from;
21207 /* Restore original glyph pointers of TO. */
21208 memcpy (to->glyphs, pointers, sizeof to->glyphs);
21210 /* Copy the glyphs. */
21211 memcpy (to->glyphs[TEXT_AREA], from->glyphs[TEXT_AREA],
21212 min (from->used[TEXT_AREA], to_used) * sizeof (struct glyph));
21214 /* If we filled only part of the TO row, fill the rest with
21215 space_glyph (which will display as empty space). */
21216 if (to_used > from->used[TEXT_AREA])
21217 fill_up_frame_row_with_spaces (to, to_used);
21220 /* Display one menu item on a TTY, by overwriting the glyphs in the
21221 frame F's desired glyph matrix with glyphs produced from the menu
21222 item text. Called from term.c to display TTY drop-down menus one
21223 item at a time.
21225 ITEM_TEXT is the menu item text as a C string.
21227 FACE_ID is the face ID to be used for this menu item. FACE_ID
21228 could specify one of 3 faces: a face for an enabled item, a face
21229 for a disabled item, or a face for a selected item.
21231 X and Y are coordinates of the first glyph in the frame's desired
21232 matrix to be overwritten by the menu item. Since this is a TTY, Y
21233 is the zero-based number of the glyph row and X is the zero-based
21234 glyph number in the row, starting from left, where to start
21235 displaying the item.
21237 SUBMENU non-zero means this menu item drops down a submenu, which
21238 should be indicated by displaying a proper visual cue after the
21239 item text. */
21241 void
21242 display_tty_menu_item (const char *item_text, int width, int face_id,
21243 int x, int y, int submenu)
21245 struct it it;
21246 struct frame *f = SELECTED_FRAME ();
21247 struct window *w = XWINDOW (f->selected_window);
21248 int saved_used, saved_truncated, saved_width, saved_reversed;
21249 struct glyph_row *row;
21250 size_t item_len = strlen (item_text);
21252 eassert (FRAME_TERMCAP_P (f));
21254 /* Don't write beyond the matrix's last row. This can happen for
21255 TTY screens that are not high enough to show the entire menu.
21256 (This is actually a bit of defensive programming, as
21257 tty_menu_display already limits the number of menu items to one
21258 less than the number of screen lines.) */
21259 if (y >= f->desired_matrix->nrows)
21260 return;
21262 init_iterator (&it, w, -1, -1, f->desired_matrix->rows + y, MENU_FACE_ID);
21263 it.first_visible_x = 0;
21264 it.last_visible_x = FRAME_COLS (f) - 1;
21265 row = it.glyph_row;
21266 /* Start with the row contents from the current matrix. */
21267 deep_copy_glyph_row (row, f->current_matrix->rows + y);
21268 saved_width = row->full_width_p;
21269 row->full_width_p = 1;
21270 saved_reversed = row->reversed_p;
21271 row->reversed_p = 0;
21272 row->enabled_p = true;
21274 /* Arrange for the menu item glyphs to start at (X,Y) and have the
21275 desired face. */
21276 eassert (x < f->desired_matrix->matrix_w);
21277 it.current_x = it.hpos = x;
21278 it.current_y = it.vpos = y;
21279 saved_used = row->used[TEXT_AREA];
21280 saved_truncated = row->truncated_on_right_p;
21281 row->used[TEXT_AREA] = x;
21282 it.face_id = face_id;
21283 it.line_wrap = TRUNCATE;
21285 /* FIXME: This should be controlled by a user option. See the
21286 comments in redisplay_tool_bar and display_mode_line about this.
21287 Also, if paragraph_embedding could ever be R2L, changes will be
21288 needed to avoid shifting to the right the row characters in
21289 term.c:append_glyph. */
21290 it.paragraph_embedding = L2R;
21292 /* Pad with a space on the left. */
21293 display_string (" ", Qnil, Qnil, 0, 0, &it, 1, 0, FRAME_COLS (f) - 1, -1);
21294 width--;
21295 /* Display the menu item, pad with spaces to WIDTH. */
21296 if (submenu)
21298 display_string (item_text, Qnil, Qnil, 0, 0, &it,
21299 item_len, 0, FRAME_COLS (f) - 1, -1);
21300 width -= item_len;
21301 /* Indicate with " >" that there's a submenu. */
21302 display_string (" >", Qnil, Qnil, 0, 0, &it, width, 0,
21303 FRAME_COLS (f) - 1, -1);
21305 else
21306 display_string (item_text, Qnil, Qnil, 0, 0, &it,
21307 width, 0, FRAME_COLS (f) - 1, -1);
21309 row->used[TEXT_AREA] = max (saved_used, row->used[TEXT_AREA]);
21310 row->truncated_on_right_p = saved_truncated;
21311 row->hash = row_hash (row);
21312 row->full_width_p = saved_width;
21313 row->reversed_p = saved_reversed;
21316 /***********************************************************************
21317 Mode Line
21318 ***********************************************************************/
21320 /* Redisplay mode lines in the window tree whose root is WINDOW. If
21321 FORCE is non-zero, redisplay mode lines unconditionally.
21322 Otherwise, redisplay only mode lines that are garbaged. Value is
21323 the number of windows whose mode lines were redisplayed. */
21325 static int
21326 redisplay_mode_lines (Lisp_Object window, bool force)
21328 int nwindows = 0;
21330 while (!NILP (window))
21332 struct window *w = XWINDOW (window);
21334 if (WINDOWP (w->contents))
21335 nwindows += redisplay_mode_lines (w->contents, force);
21336 else if (force
21337 || FRAME_GARBAGED_P (XFRAME (w->frame))
21338 || !MATRIX_MODE_LINE_ROW (w->current_matrix)->enabled_p)
21340 struct text_pos lpoint;
21341 struct buffer *old = current_buffer;
21343 /* Set the window's buffer for the mode line display. */
21344 SET_TEXT_POS (lpoint, PT, PT_BYTE);
21345 set_buffer_internal_1 (XBUFFER (w->contents));
21347 /* Point refers normally to the selected window. For any
21348 other window, set up appropriate value. */
21349 if (!EQ (window, selected_window))
21351 struct text_pos pt;
21353 CLIP_TEXT_POS_FROM_MARKER (pt, w->pointm);
21354 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
21357 /* Display mode lines. */
21358 clear_glyph_matrix (w->desired_matrix);
21359 if (display_mode_lines (w))
21360 ++nwindows;
21362 /* Restore old settings. */
21363 set_buffer_internal_1 (old);
21364 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
21367 window = w->next;
21370 return nwindows;
21374 /* Display the mode and/or header line of window W. Value is the
21375 sum number of mode lines and header lines displayed. */
21377 static int
21378 display_mode_lines (struct window *w)
21380 Lisp_Object old_selected_window = selected_window;
21381 Lisp_Object old_selected_frame = selected_frame;
21382 Lisp_Object new_frame = w->frame;
21383 Lisp_Object old_frame_selected_window = XFRAME (new_frame)->selected_window;
21384 int n = 0;
21386 selected_frame = new_frame;
21387 /* FIXME: If we were to allow the mode-line's computation changing the buffer
21388 or window's point, then we'd need select_window_1 here as well. */
21389 XSETWINDOW (selected_window, w);
21390 XFRAME (new_frame)->selected_window = selected_window;
21392 /* These will be set while the mode line specs are processed. */
21393 line_number_displayed = 0;
21394 w->column_number_displayed = -1;
21396 if (WINDOW_WANTS_MODELINE_P (w))
21398 struct window *sel_w = XWINDOW (old_selected_window);
21400 /* Select mode line face based on the real selected window. */
21401 display_mode_line (w, CURRENT_MODE_LINE_FACE_ID_3 (sel_w, sel_w, w),
21402 BVAR (current_buffer, mode_line_format));
21403 ++n;
21406 if (WINDOW_WANTS_HEADER_LINE_P (w))
21408 display_mode_line (w, HEADER_LINE_FACE_ID,
21409 BVAR (current_buffer, header_line_format));
21410 ++n;
21413 XFRAME (new_frame)->selected_window = old_frame_selected_window;
21414 selected_frame = old_selected_frame;
21415 selected_window = old_selected_window;
21416 if (n > 0)
21417 w->must_be_updated_p = true;
21418 return n;
21422 /* Display mode or header line of window W. FACE_ID specifies which
21423 line to display; it is either MODE_LINE_FACE_ID or
21424 HEADER_LINE_FACE_ID. FORMAT is the mode/header line format to
21425 display. Value is the pixel height of the mode/header line
21426 displayed. */
21428 static int
21429 display_mode_line (struct window *w, enum face_id face_id, Lisp_Object format)
21431 struct it it;
21432 struct face *face;
21433 ptrdiff_t count = SPECPDL_INDEX ();
21435 init_iterator (&it, w, -1, -1, NULL, face_id);
21436 /* Don't extend on a previously drawn mode-line.
21437 This may happen if called from pos_visible_p. */
21438 it.glyph_row->enabled_p = false;
21439 prepare_desired_row (it.glyph_row);
21441 it.glyph_row->mode_line_p = 1;
21443 /* FIXME: This should be controlled by a user option. But
21444 supporting such an option is not trivial, since the mode line is
21445 made up of many separate strings. */
21446 it.paragraph_embedding = L2R;
21448 record_unwind_protect (unwind_format_mode_line,
21449 format_mode_line_unwind_data (NULL, NULL, Qnil, 0));
21451 mode_line_target = MODE_LINE_DISPLAY;
21453 /* Temporarily make frame's keyboard the current kboard so that
21454 kboard-local variables in the mode_line_format will get the right
21455 values. */
21456 push_kboard (FRAME_KBOARD (it.f));
21457 record_unwind_save_match_data ();
21458 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
21459 pop_kboard ();
21461 unbind_to (count, Qnil);
21463 /* Fill up with spaces. */
21464 display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);
21466 compute_line_metrics (&it);
21467 it.glyph_row->full_width_p = 1;
21468 it.glyph_row->continued_p = 0;
21469 it.glyph_row->truncated_on_left_p = 0;
21470 it.glyph_row->truncated_on_right_p = 0;
21472 /* Make a 3D mode-line have a shadow at its right end. */
21473 face = FACE_FROM_ID (it.f, face_id);
21474 extend_face_to_end_of_line (&it);
21475 if (face->box != FACE_NO_BOX)
21477 struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
21478 + it.glyph_row->used[TEXT_AREA] - 1);
21479 last->right_box_line_p = 1;
21482 return it.glyph_row->height;
21485 /* Move element ELT in LIST to the front of LIST.
21486 Return the updated list. */
21488 static Lisp_Object
21489 move_elt_to_front (Lisp_Object elt, Lisp_Object list)
21491 register Lisp_Object tail, prev;
21492 register Lisp_Object tem;
21494 tail = list;
21495 prev = Qnil;
21496 while (CONSP (tail))
21498 tem = XCAR (tail);
21500 if (EQ (elt, tem))
21502 /* Splice out the link TAIL. */
21503 if (NILP (prev))
21504 list = XCDR (tail);
21505 else
21506 Fsetcdr (prev, XCDR (tail));
21508 /* Now make it the first. */
21509 Fsetcdr (tail, list);
21510 return tail;
21512 else
21513 prev = tail;
21514 tail = XCDR (tail);
21515 QUIT;
21518 /* Not found--return unchanged LIST. */
21519 return list;
21522 /* Contribute ELT to the mode line for window IT->w. How it
21523 translates into text depends on its data type.
21525 IT describes the display environment in which we display, as usual.
21527 DEPTH is the depth in recursion. It is used to prevent
21528 infinite recursion here.
21530 FIELD_WIDTH is the number of characters the display of ELT should
21531 occupy in the mode line, and PRECISION is the maximum number of
21532 characters to display from ELT's representation. See
21533 display_string for details.
21535 Returns the hpos of the end of the text generated by ELT.
21537 PROPS is a property list to add to any string we encounter.
21539 If RISKY is nonzero, remove (disregard) any properties in any string
21540 we encounter, and ignore :eval and :propertize.
21542 The global variable `mode_line_target' determines whether the
21543 output is passed to `store_mode_line_noprop',
21544 `store_mode_line_string', or `display_string'. */
21546 static int
21547 display_mode_element (struct it *it, int depth, int field_width, int precision,
21548 Lisp_Object elt, Lisp_Object props, int risky)
21550 int n = 0, field, prec;
21551 int literal = 0;
21553 tail_recurse:
21554 if (depth > 100)
21555 elt = build_string ("*too-deep*");
21557 depth++;
21559 switch (XTYPE (elt))
21561 case Lisp_String:
21563 /* A string: output it and check for %-constructs within it. */
21564 unsigned char c;
21565 ptrdiff_t offset = 0;
21567 if (SCHARS (elt) > 0
21568 && (!NILP (props) || risky))
21570 Lisp_Object oprops, aelt;
21571 oprops = Ftext_properties_at (make_number (0), elt);
21573 /* If the starting string's properties are not what
21574 we want, translate the string. Also, if the string
21575 is risky, do that anyway. */
21577 if (NILP (Fequal (props, oprops)) || risky)
21579 /* If the starting string has properties,
21580 merge the specified ones onto the existing ones. */
21581 if (! NILP (oprops) && !risky)
21583 Lisp_Object tem;
21585 oprops = Fcopy_sequence (oprops);
21586 tem = props;
21587 while (CONSP (tem))
21589 oprops = Fplist_put (oprops, XCAR (tem),
21590 XCAR (XCDR (tem)));
21591 tem = XCDR (XCDR (tem));
21593 props = oprops;
21596 aelt = Fassoc (elt, mode_line_proptrans_alist);
21597 if (! NILP (aelt) && !NILP (Fequal (props, XCDR (aelt))))
21599 /* AELT is what we want. Move it to the front
21600 without consing. */
21601 elt = XCAR (aelt);
21602 mode_line_proptrans_alist
21603 = move_elt_to_front (aelt, mode_line_proptrans_alist);
21605 else
21607 Lisp_Object tem;
21609 /* If AELT has the wrong props, it is useless.
21610 so get rid of it. */
21611 if (! NILP (aelt))
21612 mode_line_proptrans_alist
21613 = Fdelq (aelt, mode_line_proptrans_alist);
21615 elt = Fcopy_sequence (elt);
21616 Fset_text_properties (make_number (0), Flength (elt),
21617 props, elt);
21618 /* Add this item to mode_line_proptrans_alist. */
21619 mode_line_proptrans_alist
21620 = Fcons (Fcons (elt, props),
21621 mode_line_proptrans_alist);
21622 /* Truncate mode_line_proptrans_alist
21623 to at most 50 elements. */
21624 tem = Fnthcdr (make_number (50),
21625 mode_line_proptrans_alist);
21626 if (! NILP (tem))
21627 XSETCDR (tem, Qnil);
21632 offset = 0;
21634 if (literal)
21636 prec = precision - n;
21637 switch (mode_line_target)
21639 case MODE_LINE_NOPROP:
21640 case MODE_LINE_TITLE:
21641 n += store_mode_line_noprop (SSDATA (elt), -1, prec);
21642 break;
21643 case MODE_LINE_STRING:
21644 n += store_mode_line_string (NULL, elt, 1, 0, prec, Qnil);
21645 break;
21646 case MODE_LINE_DISPLAY:
21647 n += display_string (NULL, elt, Qnil, 0, 0, it,
21648 0, prec, 0, STRING_MULTIBYTE (elt));
21649 break;
21652 break;
21655 /* Handle the non-literal case. */
21657 while ((precision <= 0 || n < precision)
21658 && SREF (elt, offset) != 0
21659 && (mode_line_target != MODE_LINE_DISPLAY
21660 || it->current_x < it->last_visible_x))
21662 ptrdiff_t last_offset = offset;
21664 /* Advance to end of string or next format specifier. */
21665 while ((c = SREF (elt, offset++)) != '\0' && c != '%')
21668 if (offset - 1 != last_offset)
21670 ptrdiff_t nchars, nbytes;
21672 /* Output to end of string or up to '%'. Field width
21673 is length of string. Don't output more than
21674 PRECISION allows us. */
21675 offset--;
21677 prec = c_string_width (SDATA (elt) + last_offset,
21678 offset - last_offset, precision - n,
21679 &nchars, &nbytes);
21681 switch (mode_line_target)
21683 case MODE_LINE_NOPROP:
21684 case MODE_LINE_TITLE:
21685 n += store_mode_line_noprop (SSDATA (elt) + last_offset, 0, prec);
21686 break;
21687 case MODE_LINE_STRING:
21689 ptrdiff_t bytepos = last_offset;
21690 ptrdiff_t charpos = string_byte_to_char (elt, bytepos);
21691 ptrdiff_t endpos = (precision <= 0
21692 ? string_byte_to_char (elt, offset)
21693 : charpos + nchars);
21695 n += store_mode_line_string (NULL,
21696 Fsubstring (elt, make_number (charpos),
21697 make_number (endpos)),
21698 0, 0, 0, Qnil);
21700 break;
21701 case MODE_LINE_DISPLAY:
21703 ptrdiff_t bytepos = last_offset;
21704 ptrdiff_t charpos = string_byte_to_char (elt, bytepos);
21706 if (precision <= 0)
21707 nchars = string_byte_to_char (elt, offset) - charpos;
21708 n += display_string (NULL, elt, Qnil, 0, charpos,
21709 it, 0, nchars, 0,
21710 STRING_MULTIBYTE (elt));
21712 break;
21715 else /* c == '%' */
21717 ptrdiff_t percent_position = offset;
21719 /* Get the specified minimum width. Zero means
21720 don't pad. */
21721 field = 0;
21722 while ((c = SREF (elt, offset++)) >= '0' && c <= '9')
21723 field = field * 10 + c - '0';
21725 /* Don't pad beyond the total padding allowed. */
21726 if (field_width - n > 0 && field > field_width - n)
21727 field = field_width - n;
21729 /* Note that either PRECISION <= 0 or N < PRECISION. */
21730 prec = precision - n;
21732 if (c == 'M')
21733 n += display_mode_element (it, depth, field, prec,
21734 Vglobal_mode_string, props,
21735 risky);
21736 else if (c != 0)
21738 bool multibyte;
21739 ptrdiff_t bytepos, charpos;
21740 const char *spec;
21741 Lisp_Object string;
21743 bytepos = percent_position;
21744 charpos = (STRING_MULTIBYTE (elt)
21745 ? string_byte_to_char (elt, bytepos)
21746 : bytepos);
21747 spec = decode_mode_spec (it->w, c, field, &string);
21748 multibyte = STRINGP (string) && STRING_MULTIBYTE (string);
21750 switch (mode_line_target)
21752 case MODE_LINE_NOPROP:
21753 case MODE_LINE_TITLE:
21754 n += store_mode_line_noprop (spec, field, prec);
21755 break;
21756 case MODE_LINE_STRING:
21758 Lisp_Object tem = build_string (spec);
21759 props = Ftext_properties_at (make_number (charpos), elt);
21760 /* Should only keep face property in props */
21761 n += store_mode_line_string (NULL, tem, 0, field, prec, props);
21763 break;
21764 case MODE_LINE_DISPLAY:
21766 int nglyphs_before, nwritten;
21768 nglyphs_before = it->glyph_row->used[TEXT_AREA];
21769 nwritten = display_string (spec, string, elt,
21770 charpos, 0, it,
21771 field, prec, 0,
21772 multibyte);
21774 /* Assign to the glyphs written above the
21775 string where the `%x' came from, position
21776 of the `%'. */
21777 if (nwritten > 0)
21779 struct glyph *glyph
21780 = (it->glyph_row->glyphs[TEXT_AREA]
21781 + nglyphs_before);
21782 int i;
21784 for (i = 0; i < nwritten; ++i)
21786 glyph[i].object = elt;
21787 glyph[i].charpos = charpos;
21790 n += nwritten;
21793 break;
21796 else /* c == 0 */
21797 break;
21801 break;
21803 case Lisp_Symbol:
21804 /* A symbol: process the value of the symbol recursively
21805 as if it appeared here directly. Avoid error if symbol void.
21806 Special case: if value of symbol is a string, output the string
21807 literally. */
21809 register Lisp_Object tem;
21811 /* If the variable is not marked as risky to set
21812 then its contents are risky to use. */
21813 if (NILP (Fget (elt, Qrisky_local_variable)))
21814 risky = 1;
21816 tem = Fboundp (elt);
21817 if (!NILP (tem))
21819 tem = Fsymbol_value (elt);
21820 /* If value is a string, output that string literally:
21821 don't check for % within it. */
21822 if (STRINGP (tem))
21823 literal = 1;
21825 if (!EQ (tem, elt))
21827 /* Give up right away for nil or t. */
21828 elt = tem;
21829 goto tail_recurse;
21833 break;
21835 case Lisp_Cons:
21837 register Lisp_Object car, tem;
21839 /* A cons cell: five distinct cases.
21840 If first element is :eval or :propertize, do something special.
21841 If first element is a string or a cons, process all the elements
21842 and effectively concatenate them.
21843 If first element is a negative number, truncate displaying cdr to
21844 at most that many characters. If positive, pad (with spaces)
21845 to at least that many characters.
21846 If first element is a symbol, process the cadr or caddr recursively
21847 according to whether the symbol's value is non-nil or nil. */
21848 car = XCAR (elt);
21849 if (EQ (car, QCeval))
21851 /* An element of the form (:eval FORM) means evaluate FORM
21852 and use the result as mode line elements. */
21854 if (risky)
21855 break;
21857 if (CONSP (XCDR (elt)))
21859 Lisp_Object spec;
21860 spec = safe_eval (XCAR (XCDR (elt)));
21861 n += display_mode_element (it, depth, field_width - n,
21862 precision - n, spec, props,
21863 risky);
21866 else if (EQ (car, QCpropertize))
21868 /* An element of the form (:propertize ELT PROPS...)
21869 means display ELT but applying properties PROPS. */
21871 if (risky)
21872 break;
21874 if (CONSP (XCDR (elt)))
21875 n += display_mode_element (it, depth, field_width - n,
21876 precision - n, XCAR (XCDR (elt)),
21877 XCDR (XCDR (elt)), risky);
21879 else if (SYMBOLP (car))
21881 tem = Fboundp (car);
21882 elt = XCDR (elt);
21883 if (!CONSP (elt))
21884 goto invalid;
21885 /* elt is now the cdr, and we know it is a cons cell.
21886 Use its car if CAR has a non-nil value. */
21887 if (!NILP (tem))
21889 tem = Fsymbol_value (car);
21890 if (!NILP (tem))
21892 elt = XCAR (elt);
21893 goto tail_recurse;
21896 /* Symbol's value is nil (or symbol is unbound)
21897 Get the cddr of the original list
21898 and if possible find the caddr and use that. */
21899 elt = XCDR (elt);
21900 if (NILP (elt))
21901 break;
21902 else if (!CONSP (elt))
21903 goto invalid;
21904 elt = XCAR (elt);
21905 goto tail_recurse;
21907 else if (INTEGERP (car))
21909 register int lim = XINT (car);
21910 elt = XCDR (elt);
21911 if (lim < 0)
21913 /* Negative int means reduce maximum width. */
21914 if (precision <= 0)
21915 precision = -lim;
21916 else
21917 precision = min (precision, -lim);
21919 else if (lim > 0)
21921 /* Padding specified. Don't let it be more than
21922 current maximum. */
21923 if (precision > 0)
21924 lim = min (precision, lim);
21926 /* If that's more padding than already wanted, queue it.
21927 But don't reduce padding already specified even if
21928 that is beyond the current truncation point. */
21929 field_width = max (lim, field_width);
21931 goto tail_recurse;
21933 else if (STRINGP (car) || CONSP (car))
21935 Lisp_Object halftail = elt;
21936 int len = 0;
21938 while (CONSP (elt)
21939 && (precision <= 0 || n < precision))
21941 n += display_mode_element (it, depth,
21942 /* Do padding only after the last
21943 element in the list. */
21944 (! CONSP (XCDR (elt))
21945 ? field_width - n
21946 : 0),
21947 precision - n, XCAR (elt),
21948 props, risky);
21949 elt = XCDR (elt);
21950 len++;
21951 if ((len & 1) == 0)
21952 halftail = XCDR (halftail);
21953 /* Check for cycle. */
21954 if (EQ (halftail, elt))
21955 break;
21959 break;
21961 default:
21962 invalid:
21963 elt = build_string ("*invalid*");
21964 goto tail_recurse;
21967 /* Pad to FIELD_WIDTH. */
21968 if (field_width > 0 && n < field_width)
21970 switch (mode_line_target)
21972 case MODE_LINE_NOPROP:
21973 case MODE_LINE_TITLE:
21974 n += store_mode_line_noprop ("", field_width - n, 0);
21975 break;
21976 case MODE_LINE_STRING:
21977 n += store_mode_line_string ("", Qnil, 0, field_width - n, 0, Qnil);
21978 break;
21979 case MODE_LINE_DISPLAY:
21980 n += display_string ("", Qnil, Qnil, 0, 0, it, field_width - n,
21981 0, 0, 0);
21982 break;
21986 return n;
21989 /* Store a mode-line string element in mode_line_string_list.
21991 If STRING is non-null, display that C string. Otherwise, the Lisp
21992 string LISP_STRING is displayed.
21994 FIELD_WIDTH is the minimum number of output glyphs to produce.
21995 If STRING has fewer characters than FIELD_WIDTH, pad to the right
21996 with spaces. FIELD_WIDTH <= 0 means don't pad.
21998 PRECISION is the maximum number of characters to output from
21999 STRING. PRECISION <= 0 means don't truncate the string.
22001 If COPY_STRING is non-zero, make a copy of LISP_STRING before adding
22002 properties to the string.
22004 PROPS are the properties to add to the string.
22005 The mode_line_string_face face property is always added to the string.
22008 static int
22009 store_mode_line_string (const char *string, Lisp_Object lisp_string, int copy_string,
22010 int field_width, int precision, Lisp_Object props)
22012 ptrdiff_t len;
22013 int n = 0;
22015 if (string != NULL)
22017 len = strlen (string);
22018 if (precision > 0 && len > precision)
22019 len = precision;
22020 lisp_string = make_string (string, len);
22021 if (NILP (props))
22022 props = mode_line_string_face_prop;
22023 else if (!NILP (mode_line_string_face))
22025 Lisp_Object face = Fplist_get (props, Qface);
22026 props = Fcopy_sequence (props);
22027 if (NILP (face))
22028 face = mode_line_string_face;
22029 else
22030 face = list2 (face, mode_line_string_face);
22031 props = Fplist_put (props, Qface, face);
22033 Fadd_text_properties (make_number (0), make_number (len),
22034 props, lisp_string);
22036 else
22038 len = XFASTINT (Flength (lisp_string));
22039 if (precision > 0 && len > precision)
22041 len = precision;
22042 lisp_string = Fsubstring (lisp_string, make_number (0), make_number (len));
22043 precision = -1;
22045 if (!NILP (mode_line_string_face))
22047 Lisp_Object face;
22048 if (NILP (props))
22049 props = Ftext_properties_at (make_number (0), lisp_string);
22050 face = Fplist_get (props, Qface);
22051 if (NILP (face))
22052 face = mode_line_string_face;
22053 else
22054 face = list2 (face, mode_line_string_face);
22055 props = list2 (Qface, face);
22056 if (copy_string)
22057 lisp_string = Fcopy_sequence (lisp_string);
22059 if (!NILP (props))
22060 Fadd_text_properties (make_number (0), make_number (len),
22061 props, lisp_string);
22064 if (len > 0)
22066 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
22067 n += len;
22070 if (field_width > len)
22072 field_width -= len;
22073 lisp_string = Fmake_string (make_number (field_width), make_number (' '));
22074 if (!NILP (props))
22075 Fadd_text_properties (make_number (0), make_number (field_width),
22076 props, lisp_string);
22077 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
22078 n += field_width;
22081 return n;
22085 DEFUN ("format-mode-line", Fformat_mode_line, Sformat_mode_line,
22086 1, 4, 0,
22087 doc: /* Format a string out of a mode line format specification.
22088 First arg FORMAT specifies the mode line format (see `mode-line-format'
22089 for details) to use.
22091 By default, the format is evaluated for the currently selected window.
22093 Optional second arg FACE specifies the face property to put on all
22094 characters for which no face is specified. The value nil means the
22095 default face. The value t means whatever face the window's mode line
22096 currently uses (either `mode-line' or `mode-line-inactive',
22097 depending on whether the window is the selected window or not).
22098 An integer value means the value string has no text
22099 properties.
22101 Optional third and fourth args WINDOW and BUFFER specify the window
22102 and buffer to use as the context for the formatting (defaults
22103 are the selected window and the WINDOW's buffer). */)
22104 (Lisp_Object format, Lisp_Object face,
22105 Lisp_Object window, Lisp_Object buffer)
22107 struct it it;
22108 int len;
22109 struct window *w;
22110 struct buffer *old_buffer = NULL;
22111 int face_id;
22112 int no_props = INTEGERP (face);
22113 ptrdiff_t count = SPECPDL_INDEX ();
22114 Lisp_Object str;
22115 int string_start = 0;
22117 w = decode_any_window (window);
22118 XSETWINDOW (window, w);
22120 if (NILP (buffer))
22121 buffer = w->contents;
22122 CHECK_BUFFER (buffer);
22124 /* Make formatting the modeline a non-op when noninteractive, otherwise
22125 there will be problems later caused by a partially initialized frame. */
22126 if (NILP (format) || noninteractive)
22127 return empty_unibyte_string;
22129 if (no_props)
22130 face = Qnil;
22132 face_id = (NILP (face) || EQ (face, Qdefault)) ? DEFAULT_FACE_ID
22133 : EQ (face, Qt) ? (EQ (window, selected_window)
22134 ? MODE_LINE_FACE_ID : MODE_LINE_INACTIVE_FACE_ID)
22135 : EQ (face, Qmode_line) ? MODE_LINE_FACE_ID
22136 : EQ (face, Qmode_line_inactive) ? MODE_LINE_INACTIVE_FACE_ID
22137 : EQ (face, Qheader_line) ? HEADER_LINE_FACE_ID
22138 : EQ (face, Qtool_bar) ? TOOL_BAR_FACE_ID
22139 : DEFAULT_FACE_ID;
22141 old_buffer = current_buffer;
22143 /* Save things including mode_line_proptrans_alist,
22144 and set that to nil so that we don't alter the outer value. */
22145 record_unwind_protect (unwind_format_mode_line,
22146 format_mode_line_unwind_data
22147 (XFRAME (WINDOW_FRAME (w)),
22148 old_buffer, selected_window, 1));
22149 mode_line_proptrans_alist = Qnil;
22151 Fselect_window (window, Qt);
22152 set_buffer_internal_1 (XBUFFER (buffer));
22154 init_iterator (&it, w, -1, -1, NULL, face_id);
22156 if (no_props)
22158 mode_line_target = MODE_LINE_NOPROP;
22159 mode_line_string_face_prop = Qnil;
22160 mode_line_string_list = Qnil;
22161 string_start = MODE_LINE_NOPROP_LEN (0);
22163 else
22165 mode_line_target = MODE_LINE_STRING;
22166 mode_line_string_list = Qnil;
22167 mode_line_string_face = face;
22168 mode_line_string_face_prop
22169 = NILP (face) ? Qnil : list2 (Qface, face);
22172 push_kboard (FRAME_KBOARD (it.f));
22173 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
22174 pop_kboard ();
22176 if (no_props)
22178 len = MODE_LINE_NOPROP_LEN (string_start);
22179 str = make_string (mode_line_noprop_buf + string_start, len);
22181 else
22183 mode_line_string_list = Fnreverse (mode_line_string_list);
22184 str = Fmapconcat (intern ("identity"), mode_line_string_list,
22185 empty_unibyte_string);
22188 unbind_to (count, Qnil);
22189 return str;
22192 /* Write a null-terminated, right justified decimal representation of
22193 the positive integer D to BUF using a minimal field width WIDTH. */
22195 static void
22196 pint2str (register char *buf, register int width, register ptrdiff_t d)
22198 register char *p = buf;
22200 if (d <= 0)
22201 *p++ = '0';
22202 else
22204 while (d > 0)
22206 *p++ = d % 10 + '0';
22207 d /= 10;
22211 for (width -= (int) (p - buf); width > 0; --width)
22212 *p++ = ' ';
22213 *p-- = '\0';
22214 while (p > buf)
22216 d = *buf;
22217 *buf++ = *p;
22218 *p-- = d;
22222 /* Write a null-terminated, right justified decimal and "human
22223 readable" representation of the nonnegative integer D to BUF using
22224 a minimal field width WIDTH. D should be smaller than 999.5e24. */
22226 static const char power_letter[] =
22228 0, /* no letter */
22229 'k', /* kilo */
22230 'M', /* mega */
22231 'G', /* giga */
22232 'T', /* tera */
22233 'P', /* peta */
22234 'E', /* exa */
22235 'Z', /* zetta */
22236 'Y' /* yotta */
22239 static void
22240 pint2hrstr (char *buf, int width, ptrdiff_t d)
22242 /* We aim to represent the nonnegative integer D as
22243 QUOTIENT.TENTHS * 10 ^ (3 * EXPONENT). */
22244 ptrdiff_t quotient = d;
22245 int remainder = 0;
22246 /* -1 means: do not use TENTHS. */
22247 int tenths = -1;
22248 int exponent = 0;
22250 /* Length of QUOTIENT.TENTHS as a string. */
22251 int length;
22253 char * psuffix;
22254 char * p;
22256 if (quotient >= 1000)
22258 /* Scale to the appropriate EXPONENT. */
22261 remainder = quotient % 1000;
22262 quotient /= 1000;
22263 exponent++;
22265 while (quotient >= 1000);
22267 /* Round to nearest and decide whether to use TENTHS or not. */
22268 if (quotient <= 9)
22270 tenths = remainder / 100;
22271 if (remainder % 100 >= 50)
22273 if (tenths < 9)
22274 tenths++;
22275 else
22277 quotient++;
22278 if (quotient == 10)
22279 tenths = -1;
22280 else
22281 tenths = 0;
22285 else
22286 if (remainder >= 500)
22288 if (quotient < 999)
22289 quotient++;
22290 else
22292 quotient = 1;
22293 exponent++;
22294 tenths = 0;
22299 /* Calculate the LENGTH of QUOTIENT.TENTHS as a string. */
22300 if (tenths == -1 && quotient <= 99)
22301 if (quotient <= 9)
22302 length = 1;
22303 else
22304 length = 2;
22305 else
22306 length = 3;
22307 p = psuffix = buf + max (width, length);
22309 /* Print EXPONENT. */
22310 *psuffix++ = power_letter[exponent];
22311 *psuffix = '\0';
22313 /* Print TENTHS. */
22314 if (tenths >= 0)
22316 *--p = '0' + tenths;
22317 *--p = '.';
22320 /* Print QUOTIENT. */
22323 int digit = quotient % 10;
22324 *--p = '0' + digit;
22326 while ((quotient /= 10) != 0);
22328 /* Print leading spaces. */
22329 while (buf < p)
22330 *--p = ' ';
22333 /* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
22334 If EOL_FLAG is 1, set also a mnemonic character for end-of-line
22335 type of CODING_SYSTEM. Return updated pointer into BUF. */
22337 static unsigned char invalid_eol_type[] = "(*invalid*)";
22339 static char *
22340 decode_mode_spec_coding (Lisp_Object coding_system, register char *buf, int eol_flag)
22342 Lisp_Object val;
22343 bool multibyte = !NILP (BVAR (current_buffer, enable_multibyte_characters));
22344 const unsigned char *eol_str;
22345 int eol_str_len;
22346 /* The EOL conversion we are using. */
22347 Lisp_Object eoltype;
22349 val = CODING_SYSTEM_SPEC (coding_system);
22350 eoltype = Qnil;
22352 if (!VECTORP (val)) /* Not yet decided. */
22354 *buf++ = multibyte ? '-' : ' ';
22355 if (eol_flag)
22356 eoltype = eol_mnemonic_undecided;
22357 /* Don't mention EOL conversion if it isn't decided. */
22359 else
22361 Lisp_Object attrs;
22362 Lisp_Object eolvalue;
22364 attrs = AREF (val, 0);
22365 eolvalue = AREF (val, 2);
22367 *buf++ = multibyte
22368 ? XFASTINT (CODING_ATTR_MNEMONIC (attrs))
22369 : ' ';
22371 if (eol_flag)
22373 /* The EOL conversion that is normal on this system. */
22375 if (NILP (eolvalue)) /* Not yet decided. */
22376 eoltype = eol_mnemonic_undecided;
22377 else if (VECTORP (eolvalue)) /* Not yet decided. */
22378 eoltype = eol_mnemonic_undecided;
22379 else /* eolvalue is Qunix, Qdos, or Qmac. */
22380 eoltype = (EQ (eolvalue, Qunix)
22381 ? eol_mnemonic_unix
22382 : (EQ (eolvalue, Qdos) == 1
22383 ? eol_mnemonic_dos : eol_mnemonic_mac));
22387 if (eol_flag)
22389 /* Mention the EOL conversion if it is not the usual one. */
22390 if (STRINGP (eoltype))
22392 eol_str = SDATA (eoltype);
22393 eol_str_len = SBYTES (eoltype);
22395 else if (CHARACTERP (eoltype))
22397 unsigned char *tmp = alloca (MAX_MULTIBYTE_LENGTH);
22398 int c = XFASTINT (eoltype);
22399 eol_str_len = CHAR_STRING (c, tmp);
22400 eol_str = tmp;
22402 else
22404 eol_str = invalid_eol_type;
22405 eol_str_len = sizeof (invalid_eol_type) - 1;
22407 memcpy (buf, eol_str, eol_str_len);
22408 buf += eol_str_len;
22411 return buf;
22414 /* Return a string for the output of a mode line %-spec for window W,
22415 generated by character C. FIELD_WIDTH > 0 means pad the string
22416 returned with spaces to that value. Return a Lisp string in
22417 *STRING if the resulting string is taken from that Lisp string.
22419 Note we operate on the current buffer for most purposes. */
22421 static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
22423 static const char *
22424 decode_mode_spec (struct window *w, register int c, int field_width,
22425 Lisp_Object *string)
22427 Lisp_Object obj;
22428 struct frame *f = XFRAME (WINDOW_FRAME (w));
22429 char *decode_mode_spec_buf = f->decode_mode_spec_buffer;
22430 /* We are going to use f->decode_mode_spec_buffer as the buffer to
22431 produce strings from numerical values, so limit preposterously
22432 large values of FIELD_WIDTH to avoid overrunning the buffer's
22433 end. The size of the buffer is enough for FRAME_MESSAGE_BUF_SIZE
22434 bytes plus the terminating null. */
22435 int width = min (field_width, FRAME_MESSAGE_BUF_SIZE (f));
22436 struct buffer *b = current_buffer;
22438 obj = Qnil;
22439 *string = Qnil;
22441 switch (c)
22443 case '*':
22444 if (!NILP (BVAR (b, read_only)))
22445 return "%";
22446 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
22447 return "*";
22448 return "-";
22450 case '+':
22451 /* This differs from %* only for a modified read-only buffer. */
22452 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
22453 return "*";
22454 if (!NILP (BVAR (b, read_only)))
22455 return "%";
22456 return "-";
22458 case '&':
22459 /* This differs from %* in ignoring read-only-ness. */
22460 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
22461 return "*";
22462 return "-";
22464 case '%':
22465 return "%";
22467 case '[':
22469 int i;
22470 char *p;
22472 if (command_loop_level > 5)
22473 return "[[[... ";
22474 p = decode_mode_spec_buf;
22475 for (i = 0; i < command_loop_level; i++)
22476 *p++ = '[';
22477 *p = 0;
22478 return decode_mode_spec_buf;
22481 case ']':
22483 int i;
22484 char *p;
22486 if (command_loop_level > 5)
22487 return " ...]]]";
22488 p = decode_mode_spec_buf;
22489 for (i = 0; i < command_loop_level; i++)
22490 *p++ = ']';
22491 *p = 0;
22492 return decode_mode_spec_buf;
22495 case '-':
22497 register int i;
22499 /* Let lots_of_dashes be a string of infinite length. */
22500 if (mode_line_target == MODE_LINE_NOPROP
22501 || mode_line_target == MODE_LINE_STRING)
22502 return "--";
22503 if (field_width <= 0
22504 || field_width > sizeof (lots_of_dashes))
22506 for (i = 0; i < FRAME_MESSAGE_BUF_SIZE (f) - 1; ++i)
22507 decode_mode_spec_buf[i] = '-';
22508 decode_mode_spec_buf[i] = '\0';
22509 return decode_mode_spec_buf;
22511 else
22512 return lots_of_dashes;
22515 case 'b':
22516 obj = BVAR (b, name);
22517 break;
22519 case 'c':
22520 /* %c and %l are ignored in `frame-title-format'.
22521 (In redisplay_internal, the frame title is drawn _before_ the
22522 windows are updated, so the stuff which depends on actual
22523 window contents (such as %l) may fail to render properly, or
22524 even crash emacs.) */
22525 if (mode_line_target == MODE_LINE_TITLE)
22526 return "";
22527 else
22529 ptrdiff_t col = current_column ();
22530 w->column_number_displayed = col;
22531 pint2str (decode_mode_spec_buf, width, col);
22532 return decode_mode_spec_buf;
22535 case 'e':
22536 #ifndef SYSTEM_MALLOC
22538 if (NILP (Vmemory_full))
22539 return "";
22540 else
22541 return "!MEM FULL! ";
22543 #else
22544 return "";
22545 #endif
22547 case 'F':
22548 /* %F displays the frame name. */
22549 if (!NILP (f->title))
22550 return SSDATA (f->title);
22551 if (f->explicit_name || ! FRAME_WINDOW_P (f))
22552 return SSDATA (f->name);
22553 return "Emacs";
22555 case 'f':
22556 obj = BVAR (b, filename);
22557 break;
22559 case 'i':
22561 ptrdiff_t size = ZV - BEGV;
22562 pint2str (decode_mode_spec_buf, width, size);
22563 return decode_mode_spec_buf;
22566 case 'I':
22568 ptrdiff_t size = ZV - BEGV;
22569 pint2hrstr (decode_mode_spec_buf, width, size);
22570 return decode_mode_spec_buf;
22573 case 'l':
22575 ptrdiff_t startpos, startpos_byte, line, linepos, linepos_byte;
22576 ptrdiff_t topline, nlines, height;
22577 ptrdiff_t junk;
22579 /* %c and %l are ignored in `frame-title-format'. */
22580 if (mode_line_target == MODE_LINE_TITLE)
22581 return "";
22583 startpos = marker_position (w->start);
22584 startpos_byte = marker_byte_position (w->start);
22585 height = WINDOW_TOTAL_LINES (w);
22587 /* If we decided that this buffer isn't suitable for line numbers,
22588 don't forget that too fast. */
22589 if (w->base_line_pos == -1)
22590 goto no_value;
22592 /* If the buffer is very big, don't waste time. */
22593 if (INTEGERP (Vline_number_display_limit)
22594 && BUF_ZV (b) - BUF_BEGV (b) > XINT (Vline_number_display_limit))
22596 w->base_line_pos = 0;
22597 w->base_line_number = 0;
22598 goto no_value;
22601 if (w->base_line_number > 0
22602 && w->base_line_pos > 0
22603 && w->base_line_pos <= startpos)
22605 line = w->base_line_number;
22606 linepos = w->base_line_pos;
22607 linepos_byte = buf_charpos_to_bytepos (b, linepos);
22609 else
22611 line = 1;
22612 linepos = BUF_BEGV (b);
22613 linepos_byte = BUF_BEGV_BYTE (b);
22616 /* Count lines from base line to window start position. */
22617 nlines = display_count_lines (linepos_byte,
22618 startpos_byte,
22619 startpos, &junk);
22621 topline = nlines + line;
22623 /* Determine a new base line, if the old one is too close
22624 or too far away, or if we did not have one.
22625 "Too close" means it's plausible a scroll-down would
22626 go back past it. */
22627 if (startpos == BUF_BEGV (b))
22629 w->base_line_number = topline;
22630 w->base_line_pos = BUF_BEGV (b);
22632 else if (nlines < height + 25 || nlines > height * 3 + 50
22633 || linepos == BUF_BEGV (b))
22635 ptrdiff_t limit = BUF_BEGV (b);
22636 ptrdiff_t limit_byte = BUF_BEGV_BYTE (b);
22637 ptrdiff_t position;
22638 ptrdiff_t distance =
22639 (height * 2 + 30) * line_number_display_limit_width;
22641 if (startpos - distance > limit)
22643 limit = startpos - distance;
22644 limit_byte = CHAR_TO_BYTE (limit);
22647 nlines = display_count_lines (startpos_byte,
22648 limit_byte,
22649 - (height * 2 + 30),
22650 &position);
22651 /* If we couldn't find the lines we wanted within
22652 line_number_display_limit_width chars per line,
22653 give up on line numbers for this window. */
22654 if (position == limit_byte && limit == startpos - distance)
22656 w->base_line_pos = -1;
22657 w->base_line_number = 0;
22658 goto no_value;
22661 w->base_line_number = topline - nlines;
22662 w->base_line_pos = BYTE_TO_CHAR (position);
22665 /* Now count lines from the start pos to point. */
22666 nlines = display_count_lines (startpos_byte,
22667 PT_BYTE, PT, &junk);
22669 /* Record that we did display the line number. */
22670 line_number_displayed = 1;
22672 /* Make the string to show. */
22673 pint2str (decode_mode_spec_buf, width, topline + nlines);
22674 return decode_mode_spec_buf;
22675 no_value:
22677 char *p = decode_mode_spec_buf;
22678 int pad = width - 2;
22679 while (pad-- > 0)
22680 *p++ = ' ';
22681 *p++ = '?';
22682 *p++ = '?';
22683 *p = '\0';
22684 return decode_mode_spec_buf;
22687 break;
22689 case 'm':
22690 obj = BVAR (b, mode_name);
22691 break;
22693 case 'n':
22694 if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
22695 return " Narrow";
22696 break;
22698 case 'p':
22700 ptrdiff_t pos = marker_position (w->start);
22701 ptrdiff_t total = BUF_ZV (b) - BUF_BEGV (b);
22703 if (w->window_end_pos <= BUF_Z (b) - BUF_ZV (b))
22705 if (pos <= BUF_BEGV (b))
22706 return "All";
22707 else
22708 return "Bottom";
22710 else if (pos <= BUF_BEGV (b))
22711 return "Top";
22712 else
22714 if (total > 1000000)
22715 /* Do it differently for a large value, to avoid overflow. */
22716 total = ((pos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
22717 else
22718 total = ((pos - BUF_BEGV (b)) * 100 + total - 1) / total;
22719 /* We can't normally display a 3-digit number,
22720 so get us a 2-digit number that is close. */
22721 if (total == 100)
22722 total = 99;
22723 sprintf (decode_mode_spec_buf, "%2"pD"d%%", total);
22724 return decode_mode_spec_buf;
22728 /* Display percentage of size above the bottom of the screen. */
22729 case 'P':
22731 ptrdiff_t toppos = marker_position (w->start);
22732 ptrdiff_t botpos = BUF_Z (b) - w->window_end_pos;
22733 ptrdiff_t total = BUF_ZV (b) - BUF_BEGV (b);
22735 if (botpos >= BUF_ZV (b))
22737 if (toppos <= BUF_BEGV (b))
22738 return "All";
22739 else
22740 return "Bottom";
22742 else
22744 if (total > 1000000)
22745 /* Do it differently for a large value, to avoid overflow. */
22746 total = ((botpos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
22747 else
22748 total = ((botpos - BUF_BEGV (b)) * 100 + total - 1) / total;
22749 /* We can't normally display a 3-digit number,
22750 so get us a 2-digit number that is close. */
22751 if (total == 100)
22752 total = 99;
22753 if (toppos <= BUF_BEGV (b))
22754 sprintf (decode_mode_spec_buf, "Top%2"pD"d%%", total);
22755 else
22756 sprintf (decode_mode_spec_buf, "%2"pD"d%%", total);
22757 return decode_mode_spec_buf;
22761 case 's':
22762 /* status of process */
22763 obj = Fget_buffer_process (Fcurrent_buffer ());
22764 if (NILP (obj))
22765 return "no process";
22766 #ifndef MSDOS
22767 obj = Fsymbol_name (Fprocess_status (obj));
22768 #endif
22769 break;
22771 case '@':
22773 ptrdiff_t count = inhibit_garbage_collection ();
22774 Lisp_Object val = call1 (intern ("file-remote-p"),
22775 BVAR (current_buffer, directory));
22776 unbind_to (count, Qnil);
22778 if (NILP (val))
22779 return "-";
22780 else
22781 return "@";
22784 case 'z':
22785 /* coding-system (not including end-of-line format) */
22786 case 'Z':
22787 /* coding-system (including end-of-line type) */
22789 int eol_flag = (c == 'Z');
22790 char *p = decode_mode_spec_buf;
22792 if (! FRAME_WINDOW_P (f))
22794 /* No need to mention EOL here--the terminal never needs
22795 to do EOL conversion. */
22796 p = decode_mode_spec_coding (CODING_ID_NAME
22797 (FRAME_KEYBOARD_CODING (f)->id),
22798 p, 0);
22799 p = decode_mode_spec_coding (CODING_ID_NAME
22800 (FRAME_TERMINAL_CODING (f)->id),
22801 p, 0);
22803 p = decode_mode_spec_coding (BVAR (b, buffer_file_coding_system),
22804 p, eol_flag);
22806 #if 0 /* This proves to be annoying; I think we can do without. -- rms. */
22807 #ifdef subprocesses
22808 obj = Fget_buffer_process (Fcurrent_buffer ());
22809 if (PROCESSP (obj))
22811 p = decode_mode_spec_coding
22812 (XPROCESS (obj)->decode_coding_system, p, eol_flag);
22813 p = decode_mode_spec_coding
22814 (XPROCESS (obj)->encode_coding_system, p, eol_flag);
22816 #endif /* subprocesses */
22817 #endif /* 0 */
22818 *p = 0;
22819 return decode_mode_spec_buf;
22823 if (STRINGP (obj))
22825 *string = obj;
22826 return SSDATA (obj);
22828 else
22829 return "";
22833 /* Count up to COUNT lines starting from START_BYTE. COUNT negative
22834 means count lines back from START_BYTE. But don't go beyond
22835 LIMIT_BYTE. Return the number of lines thus found (always
22836 nonnegative).
22838 Set *BYTE_POS_PTR to the byte position where we stopped. This is
22839 either the position COUNT lines after/before START_BYTE, if we
22840 found COUNT lines, or LIMIT_BYTE if we hit the limit before finding
22841 COUNT lines. */
22843 static ptrdiff_t
22844 display_count_lines (ptrdiff_t start_byte,
22845 ptrdiff_t limit_byte, ptrdiff_t count,
22846 ptrdiff_t *byte_pos_ptr)
22848 register unsigned char *cursor;
22849 unsigned char *base;
22851 register ptrdiff_t ceiling;
22852 register unsigned char *ceiling_addr;
22853 ptrdiff_t orig_count = count;
22855 /* If we are not in selective display mode,
22856 check only for newlines. */
22857 int selective_display = (!NILP (BVAR (current_buffer, selective_display))
22858 && !INTEGERP (BVAR (current_buffer, selective_display)));
22860 if (count > 0)
22862 while (start_byte < limit_byte)
22864 ceiling = BUFFER_CEILING_OF (start_byte);
22865 ceiling = min (limit_byte - 1, ceiling);
22866 ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
22867 base = (cursor = BYTE_POS_ADDR (start_byte));
22871 if (selective_display)
22873 while (*cursor != '\n' && *cursor != 015
22874 && ++cursor != ceiling_addr)
22875 continue;
22876 if (cursor == ceiling_addr)
22877 break;
22879 else
22881 cursor = memchr (cursor, '\n', ceiling_addr - cursor);
22882 if (! cursor)
22883 break;
22886 cursor++;
22888 if (--count == 0)
22890 start_byte += cursor - base;
22891 *byte_pos_ptr = start_byte;
22892 return orig_count;
22895 while (cursor < ceiling_addr);
22897 start_byte += ceiling_addr - base;
22900 else
22902 while (start_byte > limit_byte)
22904 ceiling = BUFFER_FLOOR_OF (start_byte - 1);
22905 ceiling = max (limit_byte, ceiling);
22906 ceiling_addr = BYTE_POS_ADDR (ceiling);
22907 base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
22908 while (1)
22910 if (selective_display)
22912 while (--cursor >= ceiling_addr
22913 && *cursor != '\n' && *cursor != 015)
22914 continue;
22915 if (cursor < ceiling_addr)
22916 break;
22918 else
22920 cursor = memrchr (ceiling_addr, '\n', cursor - ceiling_addr);
22921 if (! cursor)
22922 break;
22925 if (++count == 0)
22927 start_byte += cursor - base + 1;
22928 *byte_pos_ptr = start_byte;
22929 /* When scanning backwards, we should
22930 not count the newline posterior to which we stop. */
22931 return - orig_count - 1;
22934 start_byte += ceiling_addr - base;
22938 *byte_pos_ptr = limit_byte;
22940 if (count < 0)
22941 return - orig_count + count;
22942 return orig_count - count;
22948 /***********************************************************************
22949 Displaying strings
22950 ***********************************************************************/
22952 /* Display a NUL-terminated string, starting with index START.
22954 If STRING is non-null, display that C string. Otherwise, the Lisp
22955 string LISP_STRING is displayed. There's a case that STRING is
22956 non-null and LISP_STRING is not nil. It means STRING is a string
22957 data of LISP_STRING. In that case, we display LISP_STRING while
22958 ignoring its text properties.
22960 If FACE_STRING is not nil, FACE_STRING_POS is a position in
22961 FACE_STRING. Display STRING or LISP_STRING with the face at
22962 FACE_STRING_POS in FACE_STRING:
22964 Display the string in the environment given by IT, but use the
22965 standard display table, temporarily.
22967 FIELD_WIDTH is the minimum number of output glyphs to produce.
22968 If STRING has fewer characters than FIELD_WIDTH, pad to the right
22969 with spaces. If STRING has more characters, more than FIELD_WIDTH
22970 glyphs will be produced. FIELD_WIDTH <= 0 means don't pad.
22972 PRECISION is the maximum number of characters to output from
22973 STRING. PRECISION < 0 means don't truncate the string.
22975 This is roughly equivalent to printf format specifiers:
22977 FIELD_WIDTH PRECISION PRINTF
22978 ----------------------------------------
22979 -1 -1 %s
22980 -1 10 %.10s
22981 10 -1 %10s
22982 20 10 %20.10s
22984 MULTIBYTE zero means do not display multibyte chars, > 0 means do
22985 display them, and < 0 means obey the current buffer's value of
22986 enable_multibyte_characters.
22988 Value is the number of columns displayed. */
22990 static int
22991 display_string (const char *string, Lisp_Object lisp_string, Lisp_Object face_string,
22992 ptrdiff_t face_string_pos, ptrdiff_t start, struct it *it,
22993 int field_width, int precision, int max_x, int multibyte)
22995 int hpos_at_start = it->hpos;
22996 int saved_face_id = it->face_id;
22997 struct glyph_row *row = it->glyph_row;
22998 ptrdiff_t it_charpos;
23000 /* Initialize the iterator IT for iteration over STRING beginning
23001 with index START. */
23002 reseat_to_string (it, NILP (lisp_string) ? string : NULL, lisp_string, start,
23003 precision, field_width, multibyte);
23004 if (string && STRINGP (lisp_string))
23005 /* LISP_STRING is the one returned by decode_mode_spec. We should
23006 ignore its text properties. */
23007 it->stop_charpos = it->end_charpos;
23009 /* If displaying STRING, set up the face of the iterator from
23010 FACE_STRING, if that's given. */
23011 if (STRINGP (face_string))
23013 ptrdiff_t endptr;
23014 struct face *face;
23016 it->face_id
23017 = face_at_string_position (it->w, face_string, face_string_pos,
23018 0, &endptr, it->base_face_id, 0);
23019 face = FACE_FROM_ID (it->f, it->face_id);
23020 it->face_box_p = face->box != FACE_NO_BOX;
23023 /* Set max_x to the maximum allowed X position. Don't let it go
23024 beyond the right edge of the window. */
23025 if (max_x <= 0)
23026 max_x = it->last_visible_x;
23027 else
23028 max_x = min (max_x, it->last_visible_x);
23030 /* Skip over display elements that are not visible. because IT->w is
23031 hscrolled. */
23032 if (it->current_x < it->first_visible_x)
23033 move_it_in_display_line_to (it, 100000, it->first_visible_x,
23034 MOVE_TO_POS | MOVE_TO_X);
23036 row->ascent = it->max_ascent;
23037 row->height = it->max_ascent + it->max_descent;
23038 row->phys_ascent = it->max_phys_ascent;
23039 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
23040 row->extra_line_spacing = it->max_extra_line_spacing;
23042 if (STRINGP (it->string))
23043 it_charpos = IT_STRING_CHARPOS (*it);
23044 else
23045 it_charpos = IT_CHARPOS (*it);
23047 /* This condition is for the case that we are called with current_x
23048 past last_visible_x. */
23049 while (it->current_x < max_x)
23051 int x_before, x, n_glyphs_before, i, nglyphs;
23053 /* Get the next display element. */
23054 if (!get_next_display_element (it))
23055 break;
23057 /* Produce glyphs. */
23058 x_before = it->current_x;
23059 n_glyphs_before = row->used[TEXT_AREA];
23060 PRODUCE_GLYPHS (it);
23062 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
23063 i = 0;
23064 x = x_before;
23065 while (i < nglyphs)
23067 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
23069 if (it->line_wrap != TRUNCATE
23070 && x + glyph->pixel_width > max_x)
23072 /* End of continued line or max_x reached. */
23073 if (CHAR_GLYPH_PADDING_P (*glyph))
23075 /* A wide character is unbreakable. */
23076 if (row->reversed_p)
23077 unproduce_glyphs (it, row->used[TEXT_AREA]
23078 - n_glyphs_before);
23079 row->used[TEXT_AREA] = n_glyphs_before;
23080 it->current_x = x_before;
23082 else
23084 if (row->reversed_p)
23085 unproduce_glyphs (it, row->used[TEXT_AREA]
23086 - (n_glyphs_before + i));
23087 row->used[TEXT_AREA] = n_glyphs_before + i;
23088 it->current_x = x;
23090 break;
23092 else if (x + glyph->pixel_width >= it->first_visible_x)
23094 /* Glyph is at least partially visible. */
23095 ++it->hpos;
23096 if (x < it->first_visible_x)
23097 row->x = x - it->first_visible_x;
23099 else
23101 /* Glyph is off the left margin of the display area.
23102 Should not happen. */
23103 emacs_abort ();
23106 row->ascent = max (row->ascent, it->max_ascent);
23107 row->height = max (row->height, it->max_ascent + it->max_descent);
23108 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
23109 row->phys_height = max (row->phys_height,
23110 it->max_phys_ascent + it->max_phys_descent);
23111 row->extra_line_spacing = max (row->extra_line_spacing,
23112 it->max_extra_line_spacing);
23113 x += glyph->pixel_width;
23114 ++i;
23117 /* Stop if max_x reached. */
23118 if (i < nglyphs)
23119 break;
23121 /* Stop at line ends. */
23122 if (ITERATOR_AT_END_OF_LINE_P (it))
23124 it->continuation_lines_width = 0;
23125 break;
23128 set_iterator_to_next (it, 1);
23129 if (STRINGP (it->string))
23130 it_charpos = IT_STRING_CHARPOS (*it);
23131 else
23132 it_charpos = IT_CHARPOS (*it);
23134 /* Stop if truncating at the right edge. */
23135 if (it->line_wrap == TRUNCATE
23136 && it->current_x >= it->last_visible_x)
23138 /* Add truncation mark, but don't do it if the line is
23139 truncated at a padding space. */
23140 if (it_charpos < it->string_nchars)
23142 if (!FRAME_WINDOW_P (it->f))
23144 int ii, n;
23146 if (it->current_x > it->last_visible_x)
23148 if (!row->reversed_p)
23150 for (ii = row->used[TEXT_AREA] - 1; ii > 0; --ii)
23151 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][ii]))
23152 break;
23154 else
23156 for (ii = 0; ii < row->used[TEXT_AREA]; ii++)
23157 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][ii]))
23158 break;
23159 unproduce_glyphs (it, ii + 1);
23160 ii = row->used[TEXT_AREA] - (ii + 1);
23162 for (n = row->used[TEXT_AREA]; ii < n; ++ii)
23164 row->used[TEXT_AREA] = ii;
23165 produce_special_glyphs (it, IT_TRUNCATION);
23168 produce_special_glyphs (it, IT_TRUNCATION);
23170 row->truncated_on_right_p = 1;
23172 break;
23176 /* Maybe insert a truncation at the left. */
23177 if (it->first_visible_x
23178 && it_charpos > 0)
23180 if (!FRAME_WINDOW_P (it->f)
23181 || (row->reversed_p
23182 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
23183 : WINDOW_LEFT_FRINGE_WIDTH (it->w)) == 0)
23184 insert_left_trunc_glyphs (it);
23185 row->truncated_on_left_p = 1;
23188 it->face_id = saved_face_id;
23190 /* Value is number of columns displayed. */
23191 return it->hpos - hpos_at_start;
23196 /* This is like a combination of memq and assq. Return 1/2 if PROPVAL
23197 appears as an element of LIST or as the car of an element of LIST.
23198 If PROPVAL is a list, compare each element against LIST in that
23199 way, and return 1/2 if any element of PROPVAL is found in LIST.
23200 Otherwise return 0. This function cannot quit.
23201 The return value is 2 if the text is invisible but with an ellipsis
23202 and 1 if it's invisible and without an ellipsis. */
23205 invisible_p (register Lisp_Object propval, Lisp_Object list)
23207 register Lisp_Object tail, proptail;
23209 for (tail = list; CONSP (tail); tail = XCDR (tail))
23211 register Lisp_Object tem;
23212 tem = XCAR (tail);
23213 if (EQ (propval, tem))
23214 return 1;
23215 if (CONSP (tem) && EQ (propval, XCAR (tem)))
23216 return NILP (XCDR (tem)) ? 1 : 2;
23219 if (CONSP (propval))
23221 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
23223 Lisp_Object propelt;
23224 propelt = XCAR (proptail);
23225 for (tail = list; CONSP (tail); tail = XCDR (tail))
23227 register Lisp_Object tem;
23228 tem = XCAR (tail);
23229 if (EQ (propelt, tem))
23230 return 1;
23231 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
23232 return NILP (XCDR (tem)) ? 1 : 2;
23237 return 0;
23240 DEFUN ("invisible-p", Finvisible_p, Sinvisible_p, 1, 1, 0,
23241 doc: /* Non-nil if the property makes the text invisible.
23242 POS-OR-PROP can be a marker or number, in which case it is taken to be
23243 a position in the current buffer and the value of the `invisible' property
23244 is checked; or it can be some other value, which is then presumed to be the
23245 value of the `invisible' property of the text of interest.
23246 The non-nil value returned can be t for truly invisible text or something
23247 else if the text is replaced by an ellipsis. */)
23248 (Lisp_Object pos_or_prop)
23250 Lisp_Object prop
23251 = (NATNUMP (pos_or_prop) || MARKERP (pos_or_prop)
23252 ? Fget_char_property (pos_or_prop, Qinvisible, Qnil)
23253 : pos_or_prop);
23254 int invis = TEXT_PROP_MEANS_INVISIBLE (prop);
23255 return (invis == 0 ? Qnil
23256 : invis == 1 ? Qt
23257 : make_number (invis));
23260 /* Calculate a width or height in pixels from a specification using
23261 the following elements:
23263 SPEC ::=
23264 NUM - a (fractional) multiple of the default font width/height
23265 (NUM) - specifies exactly NUM pixels
23266 UNIT - a fixed number of pixels, see below.
23267 ELEMENT - size of a display element in pixels, see below.
23268 (NUM . SPEC) - equals NUM * SPEC
23269 (+ SPEC SPEC ...) - add pixel values
23270 (- SPEC SPEC ...) - subtract pixel values
23271 (- SPEC) - negate pixel value
23273 NUM ::=
23274 INT or FLOAT - a number constant
23275 SYMBOL - use symbol's (buffer local) variable binding.
23277 UNIT ::=
23278 in - pixels per inch *)
23279 mm - pixels per 1/1000 meter *)
23280 cm - pixels per 1/100 meter *)
23281 width - width of current font in pixels.
23282 height - height of current font in pixels.
23284 *) using the ratio(s) defined in display-pixels-per-inch.
23286 ELEMENT ::=
23288 left-fringe - left fringe width in pixels
23289 right-fringe - right fringe width in pixels
23291 left-margin - left margin width in pixels
23292 right-margin - right margin width in pixels
23294 scroll-bar - scroll-bar area width in pixels
23296 Examples:
23298 Pixels corresponding to 5 inches:
23299 (5 . in)
23301 Total width of non-text areas on left side of window (if scroll-bar is on left):
23302 '(space :width (+ left-fringe left-margin scroll-bar))
23304 Align to first text column (in header line):
23305 '(space :align-to 0)
23307 Align to middle of text area minus half the width of variable `my-image'
23308 containing a loaded image:
23309 '(space :align-to (0.5 . (- text my-image)))
23311 Width of left margin minus width of 1 character in the default font:
23312 '(space :width (- left-margin 1))
23314 Width of left margin minus width of 2 characters in the current font:
23315 '(space :width (- left-margin (2 . width)))
23317 Center 1 character over left-margin (in header line):
23318 '(space :align-to (+ left-margin (0.5 . left-margin) -0.5))
23320 Different ways to express width of left fringe plus left margin minus one pixel:
23321 '(space :width (- (+ left-fringe left-margin) (1)))
23322 '(space :width (+ left-fringe left-margin (- (1))))
23323 '(space :width (+ left-fringe left-margin (-1)))
23327 static int
23328 calc_pixel_width_or_height (double *res, struct it *it, Lisp_Object prop,
23329 struct font *font, int width_p, int *align_to)
23331 double pixels;
23333 #define OK_PIXELS(val) ((*res = (double)(val)), 1)
23334 #define OK_ALIGN_TO(val) ((*align_to = (int)(val)), 1)
23336 if (NILP (prop))
23337 return OK_PIXELS (0);
23339 eassert (FRAME_LIVE_P (it->f));
23341 if (SYMBOLP (prop))
23343 if (SCHARS (SYMBOL_NAME (prop)) == 2)
23345 char *unit = SSDATA (SYMBOL_NAME (prop));
23347 if (unit[0] == 'i' && unit[1] == 'n')
23348 pixels = 1.0;
23349 else if (unit[0] == 'm' && unit[1] == 'm')
23350 pixels = 25.4;
23351 else if (unit[0] == 'c' && unit[1] == 'm')
23352 pixels = 2.54;
23353 else
23354 pixels = 0;
23355 if (pixels > 0)
23357 double ppi = (width_p ? FRAME_RES_X (it->f)
23358 : FRAME_RES_Y (it->f));
23360 if (ppi > 0)
23361 return OK_PIXELS (ppi / pixels);
23362 return 0;
23366 #ifdef HAVE_WINDOW_SYSTEM
23367 if (EQ (prop, Qheight))
23368 return OK_PIXELS (font ? FONT_HEIGHT (font) : FRAME_LINE_HEIGHT (it->f));
23369 if (EQ (prop, Qwidth))
23370 return OK_PIXELS (font ? FONT_WIDTH (font) : FRAME_COLUMN_WIDTH (it->f));
23371 #else
23372 if (EQ (prop, Qheight) || EQ (prop, Qwidth))
23373 return OK_PIXELS (1);
23374 #endif
23376 if (EQ (prop, Qtext))
23377 return OK_PIXELS (width_p
23378 ? window_box_width (it->w, TEXT_AREA)
23379 : WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w));
23381 if (align_to && *align_to < 0)
23383 *res = 0;
23384 if (EQ (prop, Qleft))
23385 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA));
23386 if (EQ (prop, Qright))
23387 return OK_ALIGN_TO (window_box_right_offset (it->w, TEXT_AREA));
23388 if (EQ (prop, Qcenter))
23389 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA)
23390 + window_box_width (it->w, TEXT_AREA) / 2);
23391 if (EQ (prop, Qleft_fringe))
23392 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
23393 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (it->w)
23394 : window_box_right_offset (it->w, LEFT_MARGIN_AREA));
23395 if (EQ (prop, Qright_fringe))
23396 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
23397 ? window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
23398 : window_box_right_offset (it->w, TEXT_AREA));
23399 if (EQ (prop, Qleft_margin))
23400 return OK_ALIGN_TO (window_box_left_offset (it->w, LEFT_MARGIN_AREA));
23401 if (EQ (prop, Qright_margin))
23402 return OK_ALIGN_TO (window_box_left_offset (it->w, RIGHT_MARGIN_AREA));
23403 if (EQ (prop, Qscroll_bar))
23404 return OK_ALIGN_TO (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (it->w)
23406 : (window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
23407 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
23408 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
23409 : 0)));
23411 else
23413 if (EQ (prop, Qleft_fringe))
23414 return OK_PIXELS (WINDOW_LEFT_FRINGE_WIDTH (it->w));
23415 if (EQ (prop, Qright_fringe))
23416 return OK_PIXELS (WINDOW_RIGHT_FRINGE_WIDTH (it->w));
23417 if (EQ (prop, Qleft_margin))
23418 return OK_PIXELS (WINDOW_LEFT_MARGIN_WIDTH (it->w));
23419 if (EQ (prop, Qright_margin))
23420 return OK_PIXELS (WINDOW_RIGHT_MARGIN_WIDTH (it->w));
23421 if (EQ (prop, Qscroll_bar))
23422 return OK_PIXELS (WINDOW_SCROLL_BAR_AREA_WIDTH (it->w));
23425 prop = buffer_local_value_1 (prop, it->w->contents);
23426 if (EQ (prop, Qunbound))
23427 prop = Qnil;
23430 if (INTEGERP (prop) || FLOATP (prop))
23432 int base_unit = (width_p
23433 ? FRAME_COLUMN_WIDTH (it->f)
23434 : FRAME_LINE_HEIGHT (it->f));
23435 return OK_PIXELS (XFLOATINT (prop) * base_unit);
23438 if (CONSP (prop))
23440 Lisp_Object car = XCAR (prop);
23441 Lisp_Object cdr = XCDR (prop);
23443 if (SYMBOLP (car))
23445 #ifdef HAVE_WINDOW_SYSTEM
23446 if (FRAME_WINDOW_P (it->f)
23447 && valid_image_p (prop))
23449 ptrdiff_t id = lookup_image (it->f, prop);
23450 struct image *img = IMAGE_FROM_ID (it->f, id);
23452 return OK_PIXELS (width_p ? img->width : img->height);
23454 #endif
23455 if (EQ (car, Qplus) || EQ (car, Qminus))
23457 int first = 1;
23458 double px;
23460 pixels = 0;
23461 while (CONSP (cdr))
23463 if (!calc_pixel_width_or_height (&px, it, XCAR (cdr),
23464 font, width_p, align_to))
23465 return 0;
23466 if (first)
23467 pixels = (EQ (car, Qplus) ? px : -px), first = 0;
23468 else
23469 pixels += px;
23470 cdr = XCDR (cdr);
23472 if (EQ (car, Qminus))
23473 pixels = -pixels;
23474 return OK_PIXELS (pixels);
23477 car = buffer_local_value_1 (car, it->w->contents);
23478 if (EQ (car, Qunbound))
23479 car = Qnil;
23482 if (INTEGERP (car) || FLOATP (car))
23484 double fact;
23485 pixels = XFLOATINT (car);
23486 if (NILP (cdr))
23487 return OK_PIXELS (pixels);
23488 if (calc_pixel_width_or_height (&fact, it, cdr,
23489 font, width_p, align_to))
23490 return OK_PIXELS (pixels * fact);
23491 return 0;
23494 return 0;
23497 return 0;
23501 /***********************************************************************
23502 Glyph Display
23503 ***********************************************************************/
23505 #ifdef HAVE_WINDOW_SYSTEM
23507 #ifdef GLYPH_DEBUG
23509 void
23510 dump_glyph_string (struct glyph_string *s)
23512 fprintf (stderr, "glyph string\n");
23513 fprintf (stderr, " x, y, w, h = %d, %d, %d, %d\n",
23514 s->x, s->y, s->width, s->height);
23515 fprintf (stderr, " ybase = %d\n", s->ybase);
23516 fprintf (stderr, " hl = %d\n", s->hl);
23517 fprintf (stderr, " left overhang = %d, right = %d\n",
23518 s->left_overhang, s->right_overhang);
23519 fprintf (stderr, " nchars = %d\n", s->nchars);
23520 fprintf (stderr, " extends to end of line = %d\n",
23521 s->extends_to_end_of_line_p);
23522 fprintf (stderr, " font height = %d\n", FONT_HEIGHT (s->font));
23523 fprintf (stderr, " bg width = %d\n", s->background_width);
23526 #endif /* GLYPH_DEBUG */
23528 /* Initialize glyph string S. CHAR2B is a suitably allocated vector
23529 of XChar2b structures for S; it can't be allocated in
23530 init_glyph_string because it must be allocated via `alloca'. W
23531 is the window on which S is drawn. ROW and AREA are the glyph row
23532 and area within the row from which S is constructed. START is the
23533 index of the first glyph structure covered by S. HL is a
23534 face-override for drawing S. */
23536 #ifdef HAVE_NTGUI
23537 #define OPTIONAL_HDC(hdc) HDC hdc,
23538 #define DECLARE_HDC(hdc) HDC hdc;
23539 #define ALLOCATE_HDC(hdc, f) hdc = get_frame_dc ((f))
23540 #define RELEASE_HDC(hdc, f) release_frame_dc ((f), (hdc))
23541 #endif
23543 #ifndef OPTIONAL_HDC
23544 #define OPTIONAL_HDC(hdc)
23545 #define DECLARE_HDC(hdc)
23546 #define ALLOCATE_HDC(hdc, f)
23547 #define RELEASE_HDC(hdc, f)
23548 #endif
23550 static void
23551 init_glyph_string (struct glyph_string *s,
23552 OPTIONAL_HDC (hdc)
23553 XChar2b *char2b, struct window *w, struct glyph_row *row,
23554 enum glyph_row_area area, int start, enum draw_glyphs_face hl)
23556 memset (s, 0, sizeof *s);
23557 s->w = w;
23558 s->f = XFRAME (w->frame);
23559 #ifdef HAVE_NTGUI
23560 s->hdc = hdc;
23561 #endif
23562 s->display = FRAME_X_DISPLAY (s->f);
23563 s->window = FRAME_X_WINDOW (s->f);
23564 s->char2b = char2b;
23565 s->hl = hl;
23566 s->row = row;
23567 s->area = area;
23568 s->first_glyph = row->glyphs[area] + start;
23569 s->height = row->height;
23570 s->y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
23571 s->ybase = s->y + row->ascent;
23575 /* Append the list of glyph strings with head H and tail T to the list
23576 with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the result. */
23578 static void
23579 append_glyph_string_lists (struct glyph_string **head, struct glyph_string **tail,
23580 struct glyph_string *h, struct glyph_string *t)
23582 if (h)
23584 if (*head)
23585 (*tail)->next = h;
23586 else
23587 *head = h;
23588 h->prev = *tail;
23589 *tail = t;
23594 /* Prepend the list of glyph strings with head H and tail T to the
23595 list with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the
23596 result. */
23598 static void
23599 prepend_glyph_string_lists (struct glyph_string **head, struct glyph_string **tail,
23600 struct glyph_string *h, struct glyph_string *t)
23602 if (h)
23604 if (*head)
23605 (*head)->prev = t;
23606 else
23607 *tail = t;
23608 t->next = *head;
23609 *head = h;
23614 /* Append glyph string S to the list with head *HEAD and tail *TAIL.
23615 Set *HEAD and *TAIL to the resulting list. */
23617 static void
23618 append_glyph_string (struct glyph_string **head, struct glyph_string **tail,
23619 struct glyph_string *s)
23621 s->next = s->prev = NULL;
23622 append_glyph_string_lists (head, tail, s, s);
23626 /* Get face and two-byte form of character C in face FACE_ID on frame F.
23627 The encoding of C is returned in *CHAR2B. DISPLAY_P non-zero means
23628 make sure that X resources for the face returned are allocated.
23629 Value is a pointer to a realized face that is ready for display if
23630 DISPLAY_P is non-zero. */
23632 static struct face *
23633 get_char_face_and_encoding (struct frame *f, int c, int face_id,
23634 XChar2b *char2b, int display_p)
23636 struct face *face = FACE_FROM_ID (f, face_id);
23637 unsigned code = 0;
23639 if (face->font)
23641 code = face->font->driver->encode_char (face->font, c);
23643 if (code == FONT_INVALID_CODE)
23644 code = 0;
23646 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
23648 /* Make sure X resources of the face are allocated. */
23649 #ifdef HAVE_X_WINDOWS
23650 if (display_p)
23651 #endif
23653 eassert (face != NULL);
23654 PREPARE_FACE_FOR_DISPLAY (f, face);
23657 return face;
23661 /* Get face and two-byte form of character glyph GLYPH on frame F.
23662 The encoding of GLYPH->u.ch is returned in *CHAR2B. Value is
23663 a pointer to a realized face that is ready for display. */
23665 static struct face *
23666 get_glyph_face_and_encoding (struct frame *f, struct glyph *glyph,
23667 XChar2b *char2b, int *two_byte_p)
23669 struct face *face;
23670 unsigned code = 0;
23672 eassert (glyph->type == CHAR_GLYPH);
23673 face = FACE_FROM_ID (f, glyph->face_id);
23675 /* Make sure X resources of the face are allocated. */
23676 eassert (face != NULL);
23677 PREPARE_FACE_FOR_DISPLAY (f, face);
23679 if (two_byte_p)
23680 *two_byte_p = 0;
23682 if (face->font)
23684 if (CHAR_BYTE8_P (glyph->u.ch))
23685 code = CHAR_TO_BYTE8 (glyph->u.ch);
23686 else
23687 code = face->font->driver->encode_char (face->font, glyph->u.ch);
23689 if (code == FONT_INVALID_CODE)
23690 code = 0;
23693 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
23694 return face;
23698 /* Get glyph code of character C in FONT in the two-byte form CHAR2B.
23699 Return 1 if FONT has a glyph for C, otherwise return 0. */
23701 static int
23702 get_char_glyph_code (int c, struct font *font, XChar2b *char2b)
23704 unsigned code;
23706 if (CHAR_BYTE8_P (c))
23707 code = CHAR_TO_BYTE8 (c);
23708 else
23709 code = font->driver->encode_char (font, c);
23711 if (code == FONT_INVALID_CODE)
23712 return 0;
23713 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
23714 return 1;
23718 /* Fill glyph string S with composition components specified by S->cmp.
23720 BASE_FACE is the base face of the composition.
23721 S->cmp_from is the index of the first component for S.
23723 OVERLAPS non-zero means S should draw the foreground only, and use
23724 its physical height for clipping. See also draw_glyphs.
23726 Value is the index of a component not in S. */
23728 static int
23729 fill_composite_glyph_string (struct glyph_string *s, struct face *base_face,
23730 int overlaps)
23732 int i;
23733 /* For all glyphs of this composition, starting at the offset
23734 S->cmp_from, until we reach the end of the definition or encounter a
23735 glyph that requires the different face, add it to S. */
23736 struct face *face;
23738 eassert (s);
23740 s->for_overlaps = overlaps;
23741 s->face = NULL;
23742 s->font = NULL;
23743 for (i = s->cmp_from; i < s->cmp->glyph_len; i++)
23745 int c = COMPOSITION_GLYPH (s->cmp, i);
23747 /* TAB in a composition means display glyphs with padding space
23748 on the left or right. */
23749 if (c != '\t')
23751 int face_id = FACE_FOR_CHAR (s->f, base_face->ascii_face, c,
23752 -1, Qnil);
23754 face = get_char_face_and_encoding (s->f, c, face_id,
23755 s->char2b + i, 1);
23756 if (face)
23758 if (! s->face)
23760 s->face = face;
23761 s->font = s->face->font;
23763 else if (s->face != face)
23764 break;
23767 ++s->nchars;
23769 s->cmp_to = i;
23771 if (s->face == NULL)
23773 s->face = base_face->ascii_face;
23774 s->font = s->face->font;
23777 /* All glyph strings for the same composition has the same width,
23778 i.e. the width set for the first component of the composition. */
23779 s->width = s->first_glyph->pixel_width;
23781 /* If the specified font could not be loaded, use the frame's
23782 default font, but record the fact that we couldn't load it in
23783 the glyph string so that we can draw rectangles for the
23784 characters of the glyph string. */
23785 if (s->font == NULL)
23787 s->font_not_found_p = 1;
23788 s->font = FRAME_FONT (s->f);
23791 /* Adjust base line for subscript/superscript text. */
23792 s->ybase += s->first_glyph->voffset;
23794 /* This glyph string must always be drawn with 16-bit functions. */
23795 s->two_byte_p = 1;
23797 return s->cmp_to;
23800 static int
23801 fill_gstring_glyph_string (struct glyph_string *s, int face_id,
23802 int start, int end, int overlaps)
23804 struct glyph *glyph, *last;
23805 Lisp_Object lgstring;
23806 int i;
23808 s->for_overlaps = overlaps;
23809 glyph = s->row->glyphs[s->area] + start;
23810 last = s->row->glyphs[s->area] + end;
23811 s->cmp_id = glyph->u.cmp.id;
23812 s->cmp_from = glyph->slice.cmp.from;
23813 s->cmp_to = glyph->slice.cmp.to + 1;
23814 s->face = FACE_FROM_ID (s->f, face_id);
23815 lgstring = composition_gstring_from_id (s->cmp_id);
23816 s->font = XFONT_OBJECT (LGSTRING_FONT (lgstring));
23817 glyph++;
23818 while (glyph < last
23819 && glyph->u.cmp.automatic
23820 && glyph->u.cmp.id == s->cmp_id
23821 && s->cmp_to == glyph->slice.cmp.from)
23822 s->cmp_to = (glyph++)->slice.cmp.to + 1;
23824 for (i = s->cmp_from; i < s->cmp_to; i++)
23826 Lisp_Object lglyph = LGSTRING_GLYPH (lgstring, i);
23827 unsigned code = LGLYPH_CODE (lglyph);
23829 STORE_XCHAR2B ((s->char2b + i), code >> 8, code & 0xFF);
23831 s->width = composition_gstring_width (lgstring, s->cmp_from, s->cmp_to, NULL);
23832 return glyph - s->row->glyphs[s->area];
23836 /* Fill glyph string S from a sequence glyphs for glyphless characters.
23837 See the comment of fill_glyph_string for arguments.
23838 Value is the index of the first glyph not in S. */
23841 static int
23842 fill_glyphless_glyph_string (struct glyph_string *s, int face_id,
23843 int start, int end, int overlaps)
23845 struct glyph *glyph, *last;
23846 int voffset;
23848 eassert (s->first_glyph->type == GLYPHLESS_GLYPH);
23849 s->for_overlaps = overlaps;
23850 glyph = s->row->glyphs[s->area] + start;
23851 last = s->row->glyphs[s->area] + end;
23852 voffset = glyph->voffset;
23853 s->face = FACE_FROM_ID (s->f, face_id);
23854 s->font = s->face->font ? s->face->font : FRAME_FONT (s->f);
23855 s->nchars = 1;
23856 s->width = glyph->pixel_width;
23857 glyph++;
23858 while (glyph < last
23859 && glyph->type == GLYPHLESS_GLYPH
23860 && glyph->voffset == voffset
23861 && glyph->face_id == face_id)
23863 s->nchars++;
23864 s->width += glyph->pixel_width;
23865 glyph++;
23867 s->ybase += voffset;
23868 return glyph - s->row->glyphs[s->area];
23872 /* Fill glyph string S from a sequence of character glyphs.
23874 FACE_ID is the face id of the string. START is the index of the
23875 first glyph to consider, END is the index of the last + 1.
23876 OVERLAPS non-zero means S should draw the foreground only, and use
23877 its physical height for clipping. See also draw_glyphs.
23879 Value is the index of the first glyph not in S. */
23881 static int
23882 fill_glyph_string (struct glyph_string *s, int face_id,
23883 int start, int end, int overlaps)
23885 struct glyph *glyph, *last;
23886 int voffset;
23887 int glyph_not_available_p;
23889 eassert (s->f == XFRAME (s->w->frame));
23890 eassert (s->nchars == 0);
23891 eassert (start >= 0 && end > start);
23893 s->for_overlaps = overlaps;
23894 glyph = s->row->glyphs[s->area] + start;
23895 last = s->row->glyphs[s->area] + end;
23896 voffset = glyph->voffset;
23897 s->padding_p = glyph->padding_p;
23898 glyph_not_available_p = glyph->glyph_not_available_p;
23900 while (glyph < last
23901 && glyph->type == CHAR_GLYPH
23902 && glyph->voffset == voffset
23903 /* Same face id implies same font, nowadays. */
23904 && glyph->face_id == face_id
23905 && glyph->glyph_not_available_p == glyph_not_available_p)
23907 int two_byte_p;
23909 s->face = get_glyph_face_and_encoding (s->f, glyph,
23910 s->char2b + s->nchars,
23911 &two_byte_p);
23912 s->two_byte_p = two_byte_p;
23913 ++s->nchars;
23914 eassert (s->nchars <= end - start);
23915 s->width += glyph->pixel_width;
23916 if (glyph++->padding_p != s->padding_p)
23917 break;
23920 s->font = s->face->font;
23922 /* If the specified font could not be loaded, use the frame's font,
23923 but record the fact that we couldn't load it in
23924 S->font_not_found_p so that we can draw rectangles for the
23925 characters of the glyph string. */
23926 if (s->font == NULL || glyph_not_available_p)
23928 s->font_not_found_p = 1;
23929 s->font = FRAME_FONT (s->f);
23932 /* Adjust base line for subscript/superscript text. */
23933 s->ybase += voffset;
23935 eassert (s->face && s->face->gc);
23936 return glyph - s->row->glyphs[s->area];
23940 /* Fill glyph string S from image glyph S->first_glyph. */
23942 static void
23943 fill_image_glyph_string (struct glyph_string *s)
23945 eassert (s->first_glyph->type == IMAGE_GLYPH);
23946 s->img = IMAGE_FROM_ID (s->f, s->first_glyph->u.img_id);
23947 eassert (s->img);
23948 s->slice = s->first_glyph->slice.img;
23949 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
23950 s->font = s->face->font;
23951 s->width = s->first_glyph->pixel_width;
23953 /* Adjust base line for subscript/superscript text. */
23954 s->ybase += s->first_glyph->voffset;
23958 /* Fill glyph string S from a sequence of stretch glyphs.
23960 START is the index of the first glyph to consider,
23961 END is the index of the last + 1.
23963 Value is the index of the first glyph not in S. */
23965 static int
23966 fill_stretch_glyph_string (struct glyph_string *s, int start, int end)
23968 struct glyph *glyph, *last;
23969 int voffset, face_id;
23971 eassert (s->first_glyph->type == STRETCH_GLYPH);
23973 glyph = s->row->glyphs[s->area] + start;
23974 last = s->row->glyphs[s->area] + end;
23975 face_id = glyph->face_id;
23976 s->face = FACE_FROM_ID (s->f, face_id);
23977 s->font = s->face->font;
23978 s->width = glyph->pixel_width;
23979 s->nchars = 1;
23980 voffset = glyph->voffset;
23982 for (++glyph;
23983 (glyph < last
23984 && glyph->type == STRETCH_GLYPH
23985 && glyph->voffset == voffset
23986 && glyph->face_id == face_id);
23987 ++glyph)
23988 s->width += glyph->pixel_width;
23990 /* Adjust base line for subscript/superscript text. */
23991 s->ybase += voffset;
23993 /* The case that face->gc == 0 is handled when drawing the glyph
23994 string by calling PREPARE_FACE_FOR_DISPLAY. */
23995 eassert (s->face);
23996 return glyph - s->row->glyphs[s->area];
23999 static struct font_metrics *
24000 get_per_char_metric (struct font *font, XChar2b *char2b)
24002 static struct font_metrics metrics;
24003 unsigned code;
24005 if (! font)
24006 return NULL;
24007 code = (XCHAR2B_BYTE1 (char2b) << 8) | XCHAR2B_BYTE2 (char2b);
24008 if (code == FONT_INVALID_CODE)
24009 return NULL;
24010 font->driver->text_extents (font, &code, 1, &metrics);
24011 return &metrics;
24014 /* EXPORT for RIF:
24015 Set *LEFT and *RIGHT to the left and right overhang of GLYPH on
24016 frame F. Overhangs of glyphs other than type CHAR_GLYPH are
24017 assumed to be zero. */
24019 void
24020 x_get_glyph_overhangs (struct glyph *glyph, struct frame *f, int *left, int *right)
24022 *left = *right = 0;
24024 if (glyph->type == CHAR_GLYPH)
24026 struct face *face;
24027 XChar2b char2b;
24028 struct font_metrics *pcm;
24030 face = get_glyph_face_and_encoding (f, glyph, &char2b, NULL);
24031 if (face->font && (pcm = get_per_char_metric (face->font, &char2b)))
24033 if (pcm->rbearing > pcm->width)
24034 *right = pcm->rbearing - pcm->width;
24035 if (pcm->lbearing < 0)
24036 *left = -pcm->lbearing;
24039 else if (glyph->type == COMPOSITE_GLYPH)
24041 if (! glyph->u.cmp.automatic)
24043 struct composition *cmp = composition_table[glyph->u.cmp.id];
24045 if (cmp->rbearing > cmp->pixel_width)
24046 *right = cmp->rbearing - cmp->pixel_width;
24047 if (cmp->lbearing < 0)
24048 *left = - cmp->lbearing;
24050 else
24052 Lisp_Object gstring = composition_gstring_from_id (glyph->u.cmp.id);
24053 struct font_metrics metrics;
24055 composition_gstring_width (gstring, glyph->slice.cmp.from,
24056 glyph->slice.cmp.to + 1, &metrics);
24057 if (metrics.rbearing > metrics.width)
24058 *right = metrics.rbearing - metrics.width;
24059 if (metrics.lbearing < 0)
24060 *left = - metrics.lbearing;
24066 /* Return the index of the first glyph preceding glyph string S that
24067 is overwritten by S because of S's left overhang. Value is -1
24068 if no glyphs are overwritten. */
24070 static int
24071 left_overwritten (struct glyph_string *s)
24073 int k;
24075 if (s->left_overhang)
24077 int x = 0, i;
24078 struct glyph *glyphs = s->row->glyphs[s->area];
24079 int first = s->first_glyph - glyphs;
24081 for (i = first - 1; i >= 0 && x > -s->left_overhang; --i)
24082 x -= glyphs[i].pixel_width;
24084 k = i + 1;
24086 else
24087 k = -1;
24089 return k;
24093 /* Return the index of the first glyph preceding glyph string S that
24094 is overwriting S because of its right overhang. Value is -1 if no
24095 glyph in front of S overwrites S. */
24097 static int
24098 left_overwriting (struct glyph_string *s)
24100 int i, k, x;
24101 struct glyph *glyphs = s->row->glyphs[s->area];
24102 int first = s->first_glyph - glyphs;
24104 k = -1;
24105 x = 0;
24106 for (i = first - 1; i >= 0; --i)
24108 int left, right;
24109 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
24110 if (x + right > 0)
24111 k = i;
24112 x -= glyphs[i].pixel_width;
24115 return k;
24119 /* Return the index of the last glyph following glyph string S that is
24120 overwritten by S because of S's right overhang. Value is -1 if
24121 no such glyph is found. */
24123 static int
24124 right_overwritten (struct glyph_string *s)
24126 int k = -1;
24128 if (s->right_overhang)
24130 int x = 0, i;
24131 struct glyph *glyphs = s->row->glyphs[s->area];
24132 int first = (s->first_glyph - glyphs
24133 + (s->first_glyph->type == COMPOSITE_GLYPH ? 1 : s->nchars));
24134 int end = s->row->used[s->area];
24136 for (i = first; i < end && s->right_overhang > x; ++i)
24137 x += glyphs[i].pixel_width;
24139 k = i;
24142 return k;
24146 /* Return the index of the last glyph following glyph string S that
24147 overwrites S because of its left overhang. Value is negative
24148 if no such glyph is found. */
24150 static int
24151 right_overwriting (struct glyph_string *s)
24153 int i, k, x;
24154 int end = s->row->used[s->area];
24155 struct glyph *glyphs = s->row->glyphs[s->area];
24156 int first = (s->first_glyph - glyphs
24157 + (s->first_glyph->type == COMPOSITE_GLYPH ? 1 : s->nchars));
24159 k = -1;
24160 x = 0;
24161 for (i = first; i < end; ++i)
24163 int left, right;
24164 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
24165 if (x - left < 0)
24166 k = i;
24167 x += glyphs[i].pixel_width;
24170 return k;
24174 /* Set background width of glyph string S. START is the index of the
24175 first glyph following S. LAST_X is the right-most x-position + 1
24176 in the drawing area. */
24178 static void
24179 set_glyph_string_background_width (struct glyph_string *s, int start, int last_x)
24181 /* If the face of this glyph string has to be drawn to the end of
24182 the drawing area, set S->extends_to_end_of_line_p. */
24184 if (start == s->row->used[s->area]
24185 && ((s->row->fill_line_p
24186 && (s->hl == DRAW_NORMAL_TEXT
24187 || s->hl == DRAW_IMAGE_RAISED
24188 || s->hl == DRAW_IMAGE_SUNKEN))
24189 || s->hl == DRAW_MOUSE_FACE))
24190 s->extends_to_end_of_line_p = 1;
24192 /* If S extends its face to the end of the line, set its
24193 background_width to the distance to the right edge of the drawing
24194 area. */
24195 if (s->extends_to_end_of_line_p)
24196 s->background_width = last_x - s->x + 1;
24197 else
24198 s->background_width = s->width;
24202 /* Compute overhangs and x-positions for glyph string S and its
24203 predecessors, or successors. X is the starting x-position for S.
24204 BACKWARD_P non-zero means process predecessors. */
24206 static void
24207 compute_overhangs_and_x (struct glyph_string *s, int x, int backward_p)
24209 if (backward_p)
24211 while (s)
24213 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
24214 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
24215 x -= s->width;
24216 s->x = x;
24217 s = s->prev;
24220 else
24222 while (s)
24224 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
24225 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
24226 s->x = x;
24227 x += s->width;
24228 s = s->next;
24235 /* The following macros are only called from draw_glyphs below.
24236 They reference the following parameters of that function directly:
24237 `w', `row', `area', and `overlap_p'
24238 as well as the following local variables:
24239 `s', `f', and `hdc' (in W32) */
24241 #ifdef HAVE_NTGUI
24242 /* On W32, silently add local `hdc' variable to argument list of
24243 init_glyph_string. */
24244 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
24245 init_glyph_string (s, hdc, char2b, w, row, area, start, hl)
24246 #else
24247 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
24248 init_glyph_string (s, char2b, w, row, area, start, hl)
24249 #endif
24251 /* Add a glyph string for a stretch glyph to the list of strings
24252 between HEAD and TAIL. START is the index of the stretch glyph in
24253 row area AREA of glyph row ROW. END is the index of the last glyph
24254 in that glyph row area. X is the current output position assigned
24255 to the new glyph string constructed. HL overrides that face of the
24256 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
24257 is the right-most x-position of the drawing area. */
24259 /* SunOS 4 bundled cc, barfed on continuations in the arg lists here
24260 and below -- keep them on one line. */
24261 #define BUILD_STRETCH_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
24262 do \
24264 s = alloca (sizeof *s); \
24265 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
24266 START = fill_stretch_glyph_string (s, START, END); \
24267 append_glyph_string (&HEAD, &TAIL, s); \
24268 s->x = (X); \
24270 while (0)
24273 /* Add a glyph string for an image glyph to the list of strings
24274 between HEAD and TAIL. START is the index of the image glyph in
24275 row area AREA of glyph row ROW. END is the index of the last glyph
24276 in that glyph row area. X is the current output position assigned
24277 to the new glyph string constructed. HL overrides that face of the
24278 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
24279 is the right-most x-position of the drawing area. */
24281 #define BUILD_IMAGE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
24282 do \
24284 s = alloca (sizeof *s); \
24285 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
24286 fill_image_glyph_string (s); \
24287 append_glyph_string (&HEAD, &TAIL, s); \
24288 ++START; \
24289 s->x = (X); \
24291 while (0)
24294 /* Add a glyph string for a sequence of character glyphs to the list
24295 of strings between HEAD and TAIL. START is the index of the first
24296 glyph in row area AREA of glyph row ROW that is part of the new
24297 glyph string. END is the index of the last glyph in that glyph row
24298 area. X is the current output position assigned to the new glyph
24299 string constructed. HL overrides that face of the glyph; e.g. it
24300 is DRAW_CURSOR if a cursor has to be drawn. LAST_X is the
24301 right-most x-position of the drawing area. */
24303 #define BUILD_CHAR_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
24304 do \
24306 int face_id; \
24307 XChar2b *char2b; \
24309 face_id = (row)->glyphs[area][START].face_id; \
24311 s = alloca (sizeof *s); \
24312 char2b = alloca ((END - START) * sizeof *char2b); \
24313 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
24314 append_glyph_string (&HEAD, &TAIL, s); \
24315 s->x = (X); \
24316 START = fill_glyph_string (s, face_id, START, END, overlaps); \
24318 while (0)
24321 /* Add a glyph string for a composite sequence to the list of strings
24322 between HEAD and TAIL. START is the index of the first glyph in
24323 row area AREA of glyph row ROW that is part of the new glyph
24324 string. END is the index of the last glyph in that glyph row area.
24325 X is the current output position assigned to the new glyph string
24326 constructed. HL overrides that face of the glyph; e.g. it is
24327 DRAW_CURSOR if a cursor has to be drawn. LAST_X is the right-most
24328 x-position of the drawing area. */
24330 #define BUILD_COMPOSITE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
24331 do { \
24332 int face_id = (row)->glyphs[area][START].face_id; \
24333 struct face *base_face = FACE_FROM_ID (f, face_id); \
24334 ptrdiff_t cmp_id = (row)->glyphs[area][START].u.cmp.id; \
24335 struct composition *cmp = composition_table[cmp_id]; \
24336 XChar2b *char2b; \
24337 struct glyph_string *first_s = NULL; \
24338 int n; \
24340 char2b = alloca (cmp->glyph_len * sizeof *char2b); \
24342 /* Make glyph_strings for each glyph sequence that is drawable by \
24343 the same face, and append them to HEAD/TAIL. */ \
24344 for (n = 0; n < cmp->glyph_len;) \
24346 s = alloca (sizeof *s); \
24347 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
24348 append_glyph_string (&(HEAD), &(TAIL), s); \
24349 s->cmp = cmp; \
24350 s->cmp_from = n; \
24351 s->x = (X); \
24352 if (n == 0) \
24353 first_s = s; \
24354 n = fill_composite_glyph_string (s, base_face, overlaps); \
24357 ++START; \
24358 s = first_s; \
24359 } while (0)
24362 /* Add a glyph string for a glyph-string sequence to the list of strings
24363 between HEAD and TAIL. */
24365 #define BUILD_GSTRING_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
24366 do { \
24367 int face_id; \
24368 XChar2b *char2b; \
24369 Lisp_Object gstring; \
24371 face_id = (row)->glyphs[area][START].face_id; \
24372 gstring = (composition_gstring_from_id \
24373 ((row)->glyphs[area][START].u.cmp.id)); \
24374 s = alloca (sizeof *s); \
24375 char2b = alloca (LGSTRING_GLYPH_LEN (gstring) * sizeof *char2b); \
24376 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
24377 append_glyph_string (&(HEAD), &(TAIL), s); \
24378 s->x = (X); \
24379 START = fill_gstring_glyph_string (s, face_id, START, END, overlaps); \
24380 } while (0)
24383 /* Add a glyph string for a sequence of glyphless character's glyphs
24384 to the list of strings between HEAD and TAIL. The meanings of
24385 arguments are the same as those of BUILD_CHAR_GLYPH_STRINGS. */
24387 #define BUILD_GLYPHLESS_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
24388 do \
24390 int face_id; \
24392 face_id = (row)->glyphs[area][START].face_id; \
24394 s = alloca (sizeof *s); \
24395 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
24396 append_glyph_string (&HEAD, &TAIL, s); \
24397 s->x = (X); \
24398 START = fill_glyphless_glyph_string (s, face_id, START, END, \
24399 overlaps); \
24401 while (0)
24404 /* Build a list of glyph strings between HEAD and TAIL for the glyphs
24405 of AREA of glyph row ROW on window W between indices START and END.
24406 HL overrides the face for drawing glyph strings, e.g. it is
24407 DRAW_CURSOR to draw a cursor. X and LAST_X are start and end
24408 x-positions of the drawing area.
24410 This is an ugly monster macro construct because we must use alloca
24411 to allocate glyph strings (because draw_glyphs can be called
24412 asynchronously). */
24414 #define BUILD_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
24415 do \
24417 HEAD = TAIL = NULL; \
24418 while (START < END) \
24420 struct glyph *first_glyph = (row)->glyphs[area] + START; \
24421 switch (first_glyph->type) \
24423 case CHAR_GLYPH: \
24424 BUILD_CHAR_GLYPH_STRINGS (START, END, HEAD, TAIL, \
24425 HL, X, LAST_X); \
24426 break; \
24428 case COMPOSITE_GLYPH: \
24429 if (first_glyph->u.cmp.automatic) \
24430 BUILD_GSTRING_GLYPH_STRING (START, END, HEAD, TAIL, \
24431 HL, X, LAST_X); \
24432 else \
24433 BUILD_COMPOSITE_GLYPH_STRING (START, END, HEAD, TAIL, \
24434 HL, X, LAST_X); \
24435 break; \
24437 case STRETCH_GLYPH: \
24438 BUILD_STRETCH_GLYPH_STRING (START, END, HEAD, TAIL, \
24439 HL, X, LAST_X); \
24440 break; \
24442 case IMAGE_GLYPH: \
24443 BUILD_IMAGE_GLYPH_STRING (START, END, HEAD, TAIL, \
24444 HL, X, LAST_X); \
24445 break; \
24447 case GLYPHLESS_GLYPH: \
24448 BUILD_GLYPHLESS_GLYPH_STRING (START, END, HEAD, TAIL, \
24449 HL, X, LAST_X); \
24450 break; \
24452 default: \
24453 emacs_abort (); \
24456 if (s) \
24458 set_glyph_string_background_width (s, START, LAST_X); \
24459 (X) += s->width; \
24462 } while (0)
24465 /* Draw glyphs between START and END in AREA of ROW on window W,
24466 starting at x-position X. X is relative to AREA in W. HL is a
24467 face-override with the following meaning:
24469 DRAW_NORMAL_TEXT draw normally
24470 DRAW_CURSOR draw in cursor face
24471 DRAW_MOUSE_FACE draw in mouse face.
24472 DRAW_INVERSE_VIDEO draw in mode line face
24473 DRAW_IMAGE_SUNKEN draw an image with a sunken relief around it
24474 DRAW_IMAGE_RAISED draw an image with a raised relief around it
24476 If OVERLAPS is non-zero, draw only the foreground of characters and
24477 clip to the physical height of ROW. Non-zero value also defines
24478 the overlapping part to be drawn:
24480 OVERLAPS_PRED overlap with preceding rows
24481 OVERLAPS_SUCC overlap with succeeding rows
24482 OVERLAPS_BOTH overlap with both preceding/succeeding rows
24483 OVERLAPS_ERASED_CURSOR overlap with erased cursor area
24485 Value is the x-position reached, relative to AREA of W. */
24487 static int
24488 draw_glyphs (struct window *w, int x, struct glyph_row *row,
24489 enum glyph_row_area area, ptrdiff_t start, ptrdiff_t end,
24490 enum draw_glyphs_face hl, int overlaps)
24492 struct glyph_string *head, *tail;
24493 struct glyph_string *s;
24494 struct glyph_string *clip_head = NULL, *clip_tail = NULL;
24495 int i, j, x_reached, last_x, area_left = 0;
24496 struct frame *f = XFRAME (WINDOW_FRAME (w));
24497 DECLARE_HDC (hdc);
24499 ALLOCATE_HDC (hdc, f);
24501 /* Let's rather be paranoid than getting a SEGV. */
24502 end = min (end, row->used[area]);
24503 start = clip_to_bounds (0, start, end);
24505 /* Translate X to frame coordinates. Set last_x to the right
24506 end of the drawing area. */
24507 if (row->full_width_p)
24509 /* X is relative to the left edge of W, without scroll bars
24510 or fringes. */
24511 area_left = WINDOW_LEFT_EDGE_X (w);
24512 last_x = (WINDOW_LEFT_EDGE_X (w) + WINDOW_PIXEL_WIDTH (w)
24513 - (row->mode_line_p ? WINDOW_RIGHT_DIVIDER_WIDTH (w) : 0));
24515 else
24517 area_left = window_box_left (w, area);
24518 last_x = area_left + window_box_width (w, area);
24520 x += area_left;
24522 /* Build a doubly-linked list of glyph_string structures between
24523 head and tail from what we have to draw. Note that the macro
24524 BUILD_GLYPH_STRINGS will modify its start parameter. That's
24525 the reason we use a separate variable `i'. */
24526 i = start;
24527 BUILD_GLYPH_STRINGS (i, end, head, tail, hl, x, last_x);
24528 if (tail)
24529 x_reached = tail->x + tail->background_width;
24530 else
24531 x_reached = x;
24533 /* If there are any glyphs with lbearing < 0 or rbearing > width in
24534 the row, redraw some glyphs in front or following the glyph
24535 strings built above. */
24536 if (head && !overlaps && row->contains_overlapping_glyphs_p)
24538 struct glyph_string *h, *t;
24539 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
24540 int mouse_beg_col IF_LINT (= 0), mouse_end_col IF_LINT (= 0);
24541 int check_mouse_face = 0;
24542 int dummy_x = 0;
24544 /* If mouse highlighting is on, we may need to draw adjacent
24545 glyphs using mouse-face highlighting. */
24546 if (area == TEXT_AREA && row->mouse_face_p
24547 && hlinfo->mouse_face_beg_row >= 0
24548 && hlinfo->mouse_face_end_row >= 0)
24550 ptrdiff_t row_vpos = MATRIX_ROW_VPOS (row, w->current_matrix);
24552 if (row_vpos >= hlinfo->mouse_face_beg_row
24553 && row_vpos <= hlinfo->mouse_face_end_row)
24555 check_mouse_face = 1;
24556 mouse_beg_col = (row_vpos == hlinfo->mouse_face_beg_row)
24557 ? hlinfo->mouse_face_beg_col : 0;
24558 mouse_end_col = (row_vpos == hlinfo->mouse_face_end_row)
24559 ? hlinfo->mouse_face_end_col
24560 : row->used[TEXT_AREA];
24564 /* Compute overhangs for all glyph strings. */
24565 if (FRAME_RIF (f)->compute_glyph_string_overhangs)
24566 for (s = head; s; s = s->next)
24567 FRAME_RIF (f)->compute_glyph_string_overhangs (s);
24569 /* Prepend glyph strings for glyphs in front of the first glyph
24570 string that are overwritten because of the first glyph
24571 string's left overhang. The background of all strings
24572 prepended must be drawn because the first glyph string
24573 draws over it. */
24574 i = left_overwritten (head);
24575 if (i >= 0)
24577 enum draw_glyphs_face overlap_hl;
24579 /* If this row contains mouse highlighting, attempt to draw
24580 the overlapped glyphs with the correct highlight. This
24581 code fails if the overlap encompasses more than one glyph
24582 and mouse-highlight spans only some of these glyphs.
24583 However, making it work perfectly involves a lot more
24584 code, and I don't know if the pathological case occurs in
24585 practice, so we'll stick to this for now. --- cyd */
24586 if (check_mouse_face
24587 && mouse_beg_col < start && mouse_end_col > i)
24588 overlap_hl = DRAW_MOUSE_FACE;
24589 else
24590 overlap_hl = DRAW_NORMAL_TEXT;
24592 j = i;
24593 BUILD_GLYPH_STRINGS (j, start, h, t,
24594 overlap_hl, dummy_x, last_x);
24595 start = i;
24596 compute_overhangs_and_x (t, head->x, 1);
24597 prepend_glyph_string_lists (&head, &tail, h, t);
24598 clip_head = head;
24601 /* Prepend glyph strings for glyphs in front of the first glyph
24602 string that overwrite that glyph string because of their
24603 right overhang. For these strings, only the foreground must
24604 be drawn, because it draws over the glyph string at `head'.
24605 The background must not be drawn because this would overwrite
24606 right overhangs of preceding glyphs for which no glyph
24607 strings exist. */
24608 i = left_overwriting (head);
24609 if (i >= 0)
24611 enum draw_glyphs_face overlap_hl;
24613 if (check_mouse_face
24614 && mouse_beg_col < start && mouse_end_col > i)
24615 overlap_hl = DRAW_MOUSE_FACE;
24616 else
24617 overlap_hl = DRAW_NORMAL_TEXT;
24619 clip_head = head;
24620 BUILD_GLYPH_STRINGS (i, start, h, t,
24621 overlap_hl, dummy_x, last_x);
24622 for (s = h; s; s = s->next)
24623 s->background_filled_p = 1;
24624 compute_overhangs_and_x (t, head->x, 1);
24625 prepend_glyph_string_lists (&head, &tail, h, t);
24628 /* Append glyphs strings for glyphs following the last glyph
24629 string tail that are overwritten by tail. The background of
24630 these strings has to be drawn because tail's foreground draws
24631 over it. */
24632 i = right_overwritten (tail);
24633 if (i >= 0)
24635 enum draw_glyphs_face overlap_hl;
24637 if (check_mouse_face
24638 && mouse_beg_col < i && mouse_end_col > end)
24639 overlap_hl = DRAW_MOUSE_FACE;
24640 else
24641 overlap_hl = DRAW_NORMAL_TEXT;
24643 BUILD_GLYPH_STRINGS (end, i, h, t,
24644 overlap_hl, x, last_x);
24645 /* Because BUILD_GLYPH_STRINGS updates the first argument,
24646 we don't have `end = i;' here. */
24647 compute_overhangs_and_x (h, tail->x + tail->width, 0);
24648 append_glyph_string_lists (&head, &tail, h, t);
24649 clip_tail = tail;
24652 /* Append glyph strings for glyphs following the last glyph
24653 string tail that overwrite tail. The foreground of such
24654 glyphs has to be drawn because it writes into the background
24655 of tail. The background must not be drawn because it could
24656 paint over the foreground of following glyphs. */
24657 i = right_overwriting (tail);
24658 if (i >= 0)
24660 enum draw_glyphs_face overlap_hl;
24661 if (check_mouse_face
24662 && mouse_beg_col < i && mouse_end_col > end)
24663 overlap_hl = DRAW_MOUSE_FACE;
24664 else
24665 overlap_hl = DRAW_NORMAL_TEXT;
24667 clip_tail = tail;
24668 i++; /* We must include the Ith glyph. */
24669 BUILD_GLYPH_STRINGS (end, i, h, t,
24670 overlap_hl, x, last_x);
24671 for (s = h; s; s = s->next)
24672 s->background_filled_p = 1;
24673 compute_overhangs_and_x (h, tail->x + tail->width, 0);
24674 append_glyph_string_lists (&head, &tail, h, t);
24676 if (clip_head || clip_tail)
24677 for (s = head; s; s = s->next)
24679 s->clip_head = clip_head;
24680 s->clip_tail = clip_tail;
24684 /* Draw all strings. */
24685 for (s = head; s; s = s->next)
24686 FRAME_RIF (f)->draw_glyph_string (s);
24688 #ifndef HAVE_NS
24689 /* When focus a sole frame and move horizontally, this sets on_p to 0
24690 causing a failure to erase prev cursor position. */
24691 if (area == TEXT_AREA
24692 && !row->full_width_p
24693 /* When drawing overlapping rows, only the glyph strings'
24694 foreground is drawn, which doesn't erase a cursor
24695 completely. */
24696 && !overlaps)
24698 int x0 = clip_head ? clip_head->x : (head ? head->x : x);
24699 int x1 = (clip_tail ? clip_tail->x + clip_tail->background_width
24700 : (tail ? tail->x + tail->background_width : x));
24701 x0 -= area_left;
24702 x1 -= area_left;
24704 notice_overwritten_cursor (w, TEXT_AREA, x0, x1,
24705 row->y, MATRIX_ROW_BOTTOM_Y (row));
24707 #endif
24709 /* Value is the x-position up to which drawn, relative to AREA of W.
24710 This doesn't include parts drawn because of overhangs. */
24711 if (row->full_width_p)
24712 x_reached = FRAME_TO_WINDOW_PIXEL_X (w, x_reached);
24713 else
24714 x_reached -= area_left;
24716 RELEASE_HDC (hdc, f);
24718 return x_reached;
24721 /* Expand row matrix if too narrow. Don't expand if area
24722 is not present. */
24724 #define IT_EXPAND_MATRIX_WIDTH(it, area) \
24726 if (!it->f->fonts_changed \
24727 && (it->glyph_row->glyphs[area] \
24728 < it->glyph_row->glyphs[area + 1])) \
24730 it->w->ncols_scale_factor++; \
24731 it->f->fonts_changed = 1; \
24735 /* Store one glyph for IT->char_to_display in IT->glyph_row.
24736 Called from x_produce_glyphs when IT->glyph_row is non-null. */
24738 static void
24739 append_glyph (struct it *it)
24741 struct glyph *glyph;
24742 enum glyph_row_area area = it->area;
24744 eassert (it->glyph_row);
24745 eassert (it->char_to_display != '\n' && it->char_to_display != '\t');
24747 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
24748 if (glyph < it->glyph_row->glyphs[area + 1])
24750 /* If the glyph row is reversed, we need to prepend the glyph
24751 rather than append it. */
24752 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24754 struct glyph *g;
24756 /* Make room for the additional glyph. */
24757 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
24758 g[1] = *g;
24759 glyph = it->glyph_row->glyphs[area];
24761 glyph->charpos = CHARPOS (it->position);
24762 glyph->object = it->object;
24763 if (it->pixel_width > 0)
24765 glyph->pixel_width = it->pixel_width;
24766 glyph->padding_p = 0;
24768 else
24770 /* Assure at least 1-pixel width. Otherwise, cursor can't
24771 be displayed correctly. */
24772 glyph->pixel_width = 1;
24773 glyph->padding_p = 1;
24775 glyph->ascent = it->ascent;
24776 glyph->descent = it->descent;
24777 glyph->voffset = it->voffset;
24778 glyph->type = CHAR_GLYPH;
24779 glyph->avoid_cursor_p = it->avoid_cursor_p;
24780 glyph->multibyte_p = it->multibyte_p;
24781 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24783 /* In R2L rows, the left and the right box edges need to be
24784 drawn in reverse direction. */
24785 glyph->right_box_line_p = it->start_of_box_run_p;
24786 glyph->left_box_line_p = it->end_of_box_run_p;
24788 else
24790 glyph->left_box_line_p = it->start_of_box_run_p;
24791 glyph->right_box_line_p = it->end_of_box_run_p;
24793 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
24794 || it->phys_descent > it->descent);
24795 glyph->glyph_not_available_p = it->glyph_not_available_p;
24796 glyph->face_id = it->face_id;
24797 glyph->u.ch = it->char_to_display;
24798 glyph->slice.img = null_glyph_slice;
24799 glyph->font_type = FONT_TYPE_UNKNOWN;
24800 if (it->bidi_p)
24802 glyph->resolved_level = it->bidi_it.resolved_level;
24803 if ((it->bidi_it.type & 7) != it->bidi_it.type)
24804 emacs_abort ();
24805 glyph->bidi_type = it->bidi_it.type;
24807 else
24809 glyph->resolved_level = 0;
24810 glyph->bidi_type = UNKNOWN_BT;
24812 ++it->glyph_row->used[area];
24814 else
24815 IT_EXPAND_MATRIX_WIDTH (it, area);
24818 /* Store one glyph for the composition IT->cmp_it.id in
24819 IT->glyph_row. Called from x_produce_glyphs when IT->glyph_row is
24820 non-null. */
24822 static void
24823 append_composite_glyph (struct it *it)
24825 struct glyph *glyph;
24826 enum glyph_row_area area = it->area;
24828 eassert (it->glyph_row);
24830 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
24831 if (glyph < it->glyph_row->glyphs[area + 1])
24833 /* If the glyph row is reversed, we need to prepend the glyph
24834 rather than append it. */
24835 if (it->glyph_row->reversed_p && it->area == TEXT_AREA)
24837 struct glyph *g;
24839 /* Make room for the new glyph. */
24840 for (g = glyph - 1; g >= it->glyph_row->glyphs[it->area]; g--)
24841 g[1] = *g;
24842 glyph = it->glyph_row->glyphs[it->area];
24844 glyph->charpos = it->cmp_it.charpos;
24845 glyph->object = it->object;
24846 glyph->pixel_width = it->pixel_width;
24847 glyph->ascent = it->ascent;
24848 glyph->descent = it->descent;
24849 glyph->voffset = it->voffset;
24850 glyph->type = COMPOSITE_GLYPH;
24851 if (it->cmp_it.ch < 0)
24853 glyph->u.cmp.automatic = 0;
24854 glyph->u.cmp.id = it->cmp_it.id;
24855 glyph->slice.cmp.from = glyph->slice.cmp.to = 0;
24857 else
24859 glyph->u.cmp.automatic = 1;
24860 glyph->u.cmp.id = it->cmp_it.id;
24861 glyph->slice.cmp.from = it->cmp_it.from;
24862 glyph->slice.cmp.to = it->cmp_it.to - 1;
24864 glyph->avoid_cursor_p = it->avoid_cursor_p;
24865 glyph->multibyte_p = it->multibyte_p;
24866 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24868 /* In R2L rows, the left and the right box edges need to be
24869 drawn in reverse direction. */
24870 glyph->right_box_line_p = it->start_of_box_run_p;
24871 glyph->left_box_line_p = it->end_of_box_run_p;
24873 else
24875 glyph->left_box_line_p = it->start_of_box_run_p;
24876 glyph->right_box_line_p = it->end_of_box_run_p;
24878 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
24879 || it->phys_descent > it->descent);
24880 glyph->padding_p = 0;
24881 glyph->glyph_not_available_p = 0;
24882 glyph->face_id = it->face_id;
24883 glyph->font_type = FONT_TYPE_UNKNOWN;
24884 if (it->bidi_p)
24886 glyph->resolved_level = it->bidi_it.resolved_level;
24887 if ((it->bidi_it.type & 7) != it->bidi_it.type)
24888 emacs_abort ();
24889 glyph->bidi_type = it->bidi_it.type;
24891 ++it->glyph_row->used[area];
24893 else
24894 IT_EXPAND_MATRIX_WIDTH (it, area);
24898 /* Change IT->ascent and IT->height according to the setting of
24899 IT->voffset. */
24901 static void
24902 take_vertical_position_into_account (struct it *it)
24904 if (it->voffset)
24906 if (it->voffset < 0)
24907 /* Increase the ascent so that we can display the text higher
24908 in the line. */
24909 it->ascent -= it->voffset;
24910 else
24911 /* Increase the descent so that we can display the text lower
24912 in the line. */
24913 it->descent += it->voffset;
24918 /* Produce glyphs/get display metrics for the image IT is loaded with.
24919 See the description of struct display_iterator in dispextern.h for
24920 an overview of struct display_iterator. */
24922 static void
24923 produce_image_glyph (struct it *it)
24925 struct image *img;
24926 struct face *face;
24927 int glyph_ascent, crop;
24928 struct glyph_slice slice;
24930 eassert (it->what == IT_IMAGE);
24932 face = FACE_FROM_ID (it->f, it->face_id);
24933 eassert (face);
24934 /* Make sure X resources of the face is loaded. */
24935 PREPARE_FACE_FOR_DISPLAY (it->f, face);
24937 if (it->image_id < 0)
24939 /* Fringe bitmap. */
24940 it->ascent = it->phys_ascent = 0;
24941 it->descent = it->phys_descent = 0;
24942 it->pixel_width = 0;
24943 it->nglyphs = 0;
24944 return;
24947 img = IMAGE_FROM_ID (it->f, it->image_id);
24948 eassert (img);
24949 /* Make sure X resources of the image is loaded. */
24950 prepare_image_for_display (it->f, img);
24952 slice.x = slice.y = 0;
24953 slice.width = img->width;
24954 slice.height = img->height;
24956 if (INTEGERP (it->slice.x))
24957 slice.x = XINT (it->slice.x);
24958 else if (FLOATP (it->slice.x))
24959 slice.x = XFLOAT_DATA (it->slice.x) * img->width;
24961 if (INTEGERP (it->slice.y))
24962 slice.y = XINT (it->slice.y);
24963 else if (FLOATP (it->slice.y))
24964 slice.y = XFLOAT_DATA (it->slice.y) * img->height;
24966 if (INTEGERP (it->slice.width))
24967 slice.width = XINT (it->slice.width);
24968 else if (FLOATP (it->slice.width))
24969 slice.width = XFLOAT_DATA (it->slice.width) * img->width;
24971 if (INTEGERP (it->slice.height))
24972 slice.height = XINT (it->slice.height);
24973 else if (FLOATP (it->slice.height))
24974 slice.height = XFLOAT_DATA (it->slice.height) * img->height;
24976 if (slice.x >= img->width)
24977 slice.x = img->width;
24978 if (slice.y >= img->height)
24979 slice.y = img->height;
24980 if (slice.x + slice.width >= img->width)
24981 slice.width = img->width - slice.x;
24982 if (slice.y + slice.height > img->height)
24983 slice.height = img->height - slice.y;
24985 if (slice.width == 0 || slice.height == 0)
24986 return;
24988 it->ascent = it->phys_ascent = glyph_ascent = image_ascent (img, face, &slice);
24990 it->descent = slice.height - glyph_ascent;
24991 if (slice.y == 0)
24992 it->descent += img->vmargin;
24993 if (slice.y + slice.height == img->height)
24994 it->descent += img->vmargin;
24995 it->phys_descent = it->descent;
24997 it->pixel_width = slice.width;
24998 if (slice.x == 0)
24999 it->pixel_width += img->hmargin;
25000 if (slice.x + slice.width == img->width)
25001 it->pixel_width += img->hmargin;
25003 /* It's quite possible for images to have an ascent greater than
25004 their height, so don't get confused in that case. */
25005 if (it->descent < 0)
25006 it->descent = 0;
25008 it->nglyphs = 1;
25010 if (face->box != FACE_NO_BOX)
25012 if (face->box_line_width > 0)
25014 if (slice.y == 0)
25015 it->ascent += face->box_line_width;
25016 if (slice.y + slice.height == img->height)
25017 it->descent += face->box_line_width;
25020 if (it->start_of_box_run_p && slice.x == 0)
25021 it->pixel_width += eabs (face->box_line_width);
25022 if (it->end_of_box_run_p && slice.x + slice.width == img->width)
25023 it->pixel_width += eabs (face->box_line_width);
25026 take_vertical_position_into_account (it);
25028 /* Automatically crop wide image glyphs at right edge so we can
25029 draw the cursor on same display row. */
25030 if ((crop = it->pixel_width - (it->last_visible_x - it->current_x), crop > 0)
25031 && (it->hpos == 0 || it->pixel_width > it->last_visible_x / 4))
25033 it->pixel_width -= crop;
25034 slice.width -= crop;
25037 if (it->glyph_row)
25039 struct glyph *glyph;
25040 enum glyph_row_area area = it->area;
25042 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
25043 if (glyph < it->glyph_row->glyphs[area + 1])
25045 glyph->charpos = CHARPOS (it->position);
25046 glyph->object = it->object;
25047 glyph->pixel_width = it->pixel_width;
25048 glyph->ascent = glyph_ascent;
25049 glyph->descent = it->descent;
25050 glyph->voffset = it->voffset;
25051 glyph->type = IMAGE_GLYPH;
25052 glyph->avoid_cursor_p = it->avoid_cursor_p;
25053 glyph->multibyte_p = it->multibyte_p;
25054 if (it->glyph_row->reversed_p && area == TEXT_AREA)
25056 /* In R2L rows, the left and the right box edges need to be
25057 drawn in reverse direction. */
25058 glyph->right_box_line_p = it->start_of_box_run_p;
25059 glyph->left_box_line_p = it->end_of_box_run_p;
25061 else
25063 glyph->left_box_line_p = it->start_of_box_run_p;
25064 glyph->right_box_line_p = it->end_of_box_run_p;
25066 glyph->overlaps_vertically_p = 0;
25067 glyph->padding_p = 0;
25068 glyph->glyph_not_available_p = 0;
25069 glyph->face_id = it->face_id;
25070 glyph->u.img_id = img->id;
25071 glyph->slice.img = slice;
25072 glyph->font_type = FONT_TYPE_UNKNOWN;
25073 if (it->bidi_p)
25075 glyph->resolved_level = it->bidi_it.resolved_level;
25076 if ((it->bidi_it.type & 7) != it->bidi_it.type)
25077 emacs_abort ();
25078 glyph->bidi_type = it->bidi_it.type;
25080 ++it->glyph_row->used[area];
25082 else
25083 IT_EXPAND_MATRIX_WIDTH (it, area);
25088 /* Append a stretch glyph to IT->glyph_row. OBJECT is the source
25089 of the glyph, WIDTH and HEIGHT are the width and height of the
25090 stretch. ASCENT is the ascent of the glyph (0 <= ASCENT <= HEIGHT). */
25092 static void
25093 append_stretch_glyph (struct it *it, Lisp_Object object,
25094 int width, int height, int ascent)
25096 struct glyph *glyph;
25097 enum glyph_row_area area = it->area;
25099 eassert (ascent >= 0 && ascent <= height);
25101 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
25102 if (glyph < it->glyph_row->glyphs[area + 1])
25104 /* If the glyph row is reversed, we need to prepend the glyph
25105 rather than append it. */
25106 if (it->glyph_row->reversed_p && area == TEXT_AREA)
25108 struct glyph *g;
25110 /* Make room for the additional glyph. */
25111 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
25112 g[1] = *g;
25113 glyph = it->glyph_row->glyphs[area];
25115 glyph->charpos = CHARPOS (it->position);
25116 glyph->object = object;
25117 glyph->pixel_width = width;
25118 glyph->ascent = ascent;
25119 glyph->descent = height - ascent;
25120 glyph->voffset = it->voffset;
25121 glyph->type = STRETCH_GLYPH;
25122 glyph->avoid_cursor_p = it->avoid_cursor_p;
25123 glyph->multibyte_p = it->multibyte_p;
25124 if (it->glyph_row->reversed_p && area == TEXT_AREA)
25126 /* In R2L rows, the left and the right box edges need to be
25127 drawn in reverse direction. */
25128 glyph->right_box_line_p = it->start_of_box_run_p;
25129 glyph->left_box_line_p = it->end_of_box_run_p;
25131 else
25133 glyph->left_box_line_p = it->start_of_box_run_p;
25134 glyph->right_box_line_p = it->end_of_box_run_p;
25136 glyph->overlaps_vertically_p = 0;
25137 glyph->padding_p = 0;
25138 glyph->glyph_not_available_p = 0;
25139 glyph->face_id = it->face_id;
25140 glyph->u.stretch.ascent = ascent;
25141 glyph->u.stretch.height = height;
25142 glyph->slice.img = null_glyph_slice;
25143 glyph->font_type = FONT_TYPE_UNKNOWN;
25144 if (it->bidi_p)
25146 glyph->resolved_level = it->bidi_it.resolved_level;
25147 if ((it->bidi_it.type & 7) != it->bidi_it.type)
25148 emacs_abort ();
25149 glyph->bidi_type = it->bidi_it.type;
25151 else
25153 glyph->resolved_level = 0;
25154 glyph->bidi_type = UNKNOWN_BT;
25156 ++it->glyph_row->used[area];
25158 else
25159 IT_EXPAND_MATRIX_WIDTH (it, area);
25162 #endif /* HAVE_WINDOW_SYSTEM */
25164 /* Produce a stretch glyph for iterator IT. IT->object is the value
25165 of the glyph property displayed. The value must be a list
25166 `(space KEYWORD VALUE ...)' with the following KEYWORD/VALUE pairs
25167 being recognized:
25169 1. `:width WIDTH' specifies that the space should be WIDTH *
25170 canonical char width wide. WIDTH may be an integer or floating
25171 point number.
25173 2. `:relative-width FACTOR' specifies that the width of the stretch
25174 should be computed from the width of the first character having the
25175 `glyph' property, and should be FACTOR times that width.
25177 3. `:align-to HPOS' specifies that the space should be wide enough
25178 to reach HPOS, a value in canonical character units.
25180 Exactly one of the above pairs must be present.
25182 4. `:height HEIGHT' specifies that the height of the stretch produced
25183 should be HEIGHT, measured in canonical character units.
25185 5. `:relative-height FACTOR' specifies that the height of the
25186 stretch should be FACTOR times the height of the characters having
25187 the glyph property.
25189 Either none or exactly one of 4 or 5 must be present.
25191 6. `:ascent ASCENT' specifies that ASCENT percent of the height
25192 of the stretch should be used for the ascent of the stretch.
25193 ASCENT must be in the range 0 <= ASCENT <= 100. */
25195 void
25196 produce_stretch_glyph (struct it *it)
25198 /* (space :width WIDTH :height HEIGHT ...) */
25199 Lisp_Object prop, plist;
25200 int width = 0, height = 0, align_to = -1;
25201 int zero_width_ok_p = 0;
25202 double tem;
25203 struct font *font = NULL;
25205 #ifdef HAVE_WINDOW_SYSTEM
25206 int ascent = 0;
25207 int zero_height_ok_p = 0;
25209 if (FRAME_WINDOW_P (it->f))
25211 struct face *face = FACE_FROM_ID (it->f, it->face_id);
25212 font = face->font ? face->font : FRAME_FONT (it->f);
25213 PREPARE_FACE_FOR_DISPLAY (it->f, face);
25215 #endif
25217 /* List should start with `space'. */
25218 eassert (CONSP (it->object) && EQ (XCAR (it->object), Qspace));
25219 plist = XCDR (it->object);
25221 /* Compute the width of the stretch. */
25222 if ((prop = Fplist_get (plist, QCwidth), !NILP (prop))
25223 && calc_pixel_width_or_height (&tem, it, prop, font, 1, 0))
25225 /* Absolute width `:width WIDTH' specified and valid. */
25226 zero_width_ok_p = 1;
25227 width = (int)tem;
25229 #ifdef HAVE_WINDOW_SYSTEM
25230 else if (FRAME_WINDOW_P (it->f)
25231 && (prop = Fplist_get (plist, QCrelative_width), NUMVAL (prop) > 0))
25233 /* Relative width `:relative-width FACTOR' specified and valid.
25234 Compute the width of the characters having the `glyph'
25235 property. */
25236 struct it it2;
25237 unsigned char *p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
25239 it2 = *it;
25240 if (it->multibyte_p)
25241 it2.c = it2.char_to_display = STRING_CHAR_AND_LENGTH (p, it2.len);
25242 else
25244 it2.c = it2.char_to_display = *p, it2.len = 1;
25245 if (! ASCII_CHAR_P (it2.c))
25246 it2.char_to_display = BYTE8_TO_CHAR (it2.c);
25249 it2.glyph_row = NULL;
25250 it2.what = IT_CHARACTER;
25251 x_produce_glyphs (&it2);
25252 width = NUMVAL (prop) * it2.pixel_width;
25254 #endif /* HAVE_WINDOW_SYSTEM */
25255 else if ((prop = Fplist_get (plist, QCalign_to), !NILP (prop))
25256 && calc_pixel_width_or_height (&tem, it, prop, font, 1, &align_to))
25258 if (it->glyph_row == NULL || !it->glyph_row->mode_line_p)
25259 align_to = (align_to < 0
25261 : align_to - window_box_left_offset (it->w, TEXT_AREA));
25262 else if (align_to < 0)
25263 align_to = window_box_left_offset (it->w, TEXT_AREA);
25264 width = max (0, (int)tem + align_to - it->current_x);
25265 zero_width_ok_p = 1;
25267 else
25268 /* Nothing specified -> width defaults to canonical char width. */
25269 width = FRAME_COLUMN_WIDTH (it->f);
25271 if (width <= 0 && (width < 0 || !zero_width_ok_p))
25272 width = 1;
25274 #ifdef HAVE_WINDOW_SYSTEM
25275 /* Compute height. */
25276 if (FRAME_WINDOW_P (it->f))
25278 if ((prop = Fplist_get (plist, QCheight), !NILP (prop))
25279 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
25281 height = (int)tem;
25282 zero_height_ok_p = 1;
25284 else if (prop = Fplist_get (plist, QCrelative_height),
25285 NUMVAL (prop) > 0)
25286 height = FONT_HEIGHT (font) * NUMVAL (prop);
25287 else
25288 height = FONT_HEIGHT (font);
25290 if (height <= 0 && (height < 0 || !zero_height_ok_p))
25291 height = 1;
25293 /* Compute percentage of height used for ascent. If
25294 `:ascent ASCENT' is present and valid, use that. Otherwise,
25295 derive the ascent from the font in use. */
25296 if (prop = Fplist_get (plist, QCascent),
25297 NUMVAL (prop) > 0 && NUMVAL (prop) <= 100)
25298 ascent = height * NUMVAL (prop) / 100.0;
25299 else if (!NILP (prop)
25300 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
25301 ascent = min (max (0, (int)tem), height);
25302 else
25303 ascent = (height * FONT_BASE (font)) / FONT_HEIGHT (font);
25305 else
25306 #endif /* HAVE_WINDOW_SYSTEM */
25307 height = 1;
25309 if (width > 0 && it->line_wrap != TRUNCATE
25310 && it->current_x + width > it->last_visible_x)
25312 width = it->last_visible_x - it->current_x;
25313 #ifdef HAVE_WINDOW_SYSTEM
25314 /* Subtract one more pixel from the stretch width, but only on
25315 GUI frames, since on a TTY each glyph is one "pixel" wide. */
25316 width -= FRAME_WINDOW_P (it->f);
25317 #endif
25320 if (width > 0 && height > 0 && it->glyph_row)
25322 Lisp_Object o_object = it->object;
25323 Lisp_Object object = it->stack[it->sp - 1].string;
25324 int n = width;
25326 if (!STRINGP (object))
25327 object = it->w->contents;
25328 #ifdef HAVE_WINDOW_SYSTEM
25329 if (FRAME_WINDOW_P (it->f))
25330 append_stretch_glyph (it, object, width, height, ascent);
25331 else
25332 #endif
25334 it->object = object;
25335 it->char_to_display = ' ';
25336 it->pixel_width = it->len = 1;
25337 while (n--)
25338 tty_append_glyph (it);
25339 it->object = o_object;
25343 it->pixel_width = width;
25344 #ifdef HAVE_WINDOW_SYSTEM
25345 if (FRAME_WINDOW_P (it->f))
25347 it->ascent = it->phys_ascent = ascent;
25348 it->descent = it->phys_descent = height - it->ascent;
25349 it->nglyphs = width > 0 && height > 0 ? 1 : 0;
25350 take_vertical_position_into_account (it);
25352 else
25353 #endif
25354 it->nglyphs = width;
25357 /* Get information about special display element WHAT in an
25358 environment described by IT. WHAT is one of IT_TRUNCATION or
25359 IT_CONTINUATION. Maybe produce glyphs for WHAT if IT has a
25360 non-null glyph_row member. This function ensures that fields like
25361 face_id, c, len of IT are left untouched. */
25363 static void
25364 produce_special_glyphs (struct it *it, enum display_element_type what)
25366 struct it temp_it;
25367 Lisp_Object gc;
25368 GLYPH glyph;
25370 temp_it = *it;
25371 temp_it.object = make_number (0);
25372 memset (&temp_it.current, 0, sizeof temp_it.current);
25374 if (what == IT_CONTINUATION)
25376 /* Continuation glyph. For R2L lines, we mirror it by hand. */
25377 if (it->bidi_it.paragraph_dir == R2L)
25378 SET_GLYPH_FROM_CHAR (glyph, '/');
25379 else
25380 SET_GLYPH_FROM_CHAR (glyph, '\\');
25381 if (it->dp
25382 && (gc = DISP_CONTINUE_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 if (what == IT_TRUNCATION)
25391 /* Truncation glyph. */
25392 SET_GLYPH_FROM_CHAR (glyph, '$');
25393 if (it->dp
25394 && (gc = DISP_TRUNC_GLYPH (it->dp), GLYPH_CODE_P (gc)))
25396 /* FIXME: Should we mirror GC for R2L lines? */
25397 SET_GLYPH_FROM_GLYPH_CODE (glyph, gc);
25398 spec_glyph_lookup_face (XWINDOW (it->window), &glyph);
25401 else
25402 emacs_abort ();
25404 #ifdef HAVE_WINDOW_SYSTEM
25405 /* On a GUI frame, when the right fringe (left fringe for R2L rows)
25406 is turned off, we precede the truncation/continuation glyphs by a
25407 stretch glyph whose width is computed such that these special
25408 glyphs are aligned at the window margin, even when very different
25409 fonts are used in different glyph rows. */
25410 if (FRAME_WINDOW_P (temp_it.f)
25411 /* init_iterator calls this with it->glyph_row == NULL, and it
25412 wants only the pixel width of the truncation/continuation
25413 glyphs. */
25414 && temp_it.glyph_row
25415 /* insert_left_trunc_glyphs calls us at the beginning of the
25416 row, and it has its own calculation of the stretch glyph
25417 width. */
25418 && temp_it.glyph_row->used[TEXT_AREA] > 0
25419 && (temp_it.glyph_row->reversed_p
25420 ? WINDOW_LEFT_FRINGE_WIDTH (temp_it.w)
25421 : WINDOW_RIGHT_FRINGE_WIDTH (temp_it.w)) == 0)
25423 int stretch_width = temp_it.last_visible_x - temp_it.current_x;
25425 if (stretch_width > 0)
25427 struct face *face = FACE_FROM_ID (temp_it.f, temp_it.face_id);
25428 struct font *font =
25429 face->font ? face->font : FRAME_FONT (temp_it.f);
25430 int stretch_ascent =
25431 (((temp_it.ascent + temp_it.descent)
25432 * FONT_BASE (font)) / FONT_HEIGHT (font));
25434 append_stretch_glyph (&temp_it, make_number (0), stretch_width,
25435 temp_it.ascent + temp_it.descent,
25436 stretch_ascent);
25439 #endif
25441 temp_it.dp = NULL;
25442 temp_it.what = IT_CHARACTER;
25443 temp_it.len = 1;
25444 temp_it.c = temp_it.char_to_display = GLYPH_CHAR (glyph);
25445 temp_it.face_id = GLYPH_FACE (glyph);
25446 temp_it.len = CHAR_BYTES (temp_it.c);
25448 PRODUCE_GLYPHS (&temp_it);
25449 it->pixel_width = temp_it.pixel_width;
25450 it->nglyphs = temp_it.pixel_width;
25453 #ifdef HAVE_WINDOW_SYSTEM
25455 /* Calculate line-height and line-spacing properties.
25456 An integer value specifies explicit pixel value.
25457 A float value specifies relative value to current face height.
25458 A cons (float . face-name) specifies relative value to
25459 height of specified face font.
25461 Returns height in pixels, or nil. */
25464 static Lisp_Object
25465 calc_line_height_property (struct it *it, Lisp_Object val, struct font *font,
25466 int boff, int override)
25468 Lisp_Object face_name = Qnil;
25469 int ascent, descent, height;
25471 if (NILP (val) || INTEGERP (val) || (override && EQ (val, Qt)))
25472 return val;
25474 if (CONSP (val))
25476 face_name = XCAR (val);
25477 val = XCDR (val);
25478 if (!NUMBERP (val))
25479 val = make_number (1);
25480 if (NILP (face_name))
25482 height = it->ascent + it->descent;
25483 goto scale;
25487 if (NILP (face_name))
25489 font = FRAME_FONT (it->f);
25490 boff = FRAME_BASELINE_OFFSET (it->f);
25492 else if (EQ (face_name, Qt))
25494 override = 0;
25496 else
25498 int face_id;
25499 struct face *face;
25501 face_id = lookup_named_face (it->f, face_name, 0);
25502 if (face_id < 0)
25503 return make_number (-1);
25505 face = FACE_FROM_ID (it->f, face_id);
25506 font = face->font;
25507 if (font == NULL)
25508 return make_number (-1);
25509 boff = font->baseline_offset;
25510 if (font->vertical_centering)
25511 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
25514 ascent = FONT_BASE (font) + boff;
25515 descent = FONT_DESCENT (font) - boff;
25517 if (override)
25519 it->override_ascent = ascent;
25520 it->override_descent = descent;
25521 it->override_boff = boff;
25524 height = ascent + descent;
25526 scale:
25527 if (FLOATP (val))
25528 height = (int)(XFLOAT_DATA (val) * height);
25529 else if (INTEGERP (val))
25530 height *= XINT (val);
25532 return make_number (height);
25536 /* Append a glyph for a glyphless character to IT->glyph_row. FACE_ID
25537 is a face ID to be used for the glyph. FOR_NO_FONT is nonzero if
25538 and only if this is for a character for which no font was found.
25540 If the display method (it->glyphless_method) is
25541 GLYPHLESS_DISPLAY_ACRONYM or GLYPHLESS_DISPLAY_HEX_CODE, LEN is a
25542 length of the acronym or the hexadecimal string, UPPER_XOFF and
25543 UPPER_YOFF are pixel offsets for the upper part of the string,
25544 LOWER_XOFF and LOWER_YOFF are for the lower part.
25546 For the other display methods, LEN through LOWER_YOFF are zero. */
25548 static void
25549 append_glyphless_glyph (struct it *it, int face_id, int for_no_font, int len,
25550 short upper_xoff, short upper_yoff,
25551 short lower_xoff, short lower_yoff)
25553 struct glyph *glyph;
25554 enum glyph_row_area area = it->area;
25556 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
25557 if (glyph < it->glyph_row->glyphs[area + 1])
25559 /* If the glyph row is reversed, we need to prepend the glyph
25560 rather than append it. */
25561 if (it->glyph_row->reversed_p && area == TEXT_AREA)
25563 struct glyph *g;
25565 /* Make room for the additional glyph. */
25566 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
25567 g[1] = *g;
25568 glyph = it->glyph_row->glyphs[area];
25570 glyph->charpos = CHARPOS (it->position);
25571 glyph->object = it->object;
25572 glyph->pixel_width = it->pixel_width;
25573 glyph->ascent = it->ascent;
25574 glyph->descent = it->descent;
25575 glyph->voffset = it->voffset;
25576 glyph->type = GLYPHLESS_GLYPH;
25577 glyph->u.glyphless.method = it->glyphless_method;
25578 glyph->u.glyphless.for_no_font = for_no_font;
25579 glyph->u.glyphless.len = len;
25580 glyph->u.glyphless.ch = it->c;
25581 glyph->slice.glyphless.upper_xoff = upper_xoff;
25582 glyph->slice.glyphless.upper_yoff = upper_yoff;
25583 glyph->slice.glyphless.lower_xoff = lower_xoff;
25584 glyph->slice.glyphless.lower_yoff = lower_yoff;
25585 glyph->avoid_cursor_p = it->avoid_cursor_p;
25586 glyph->multibyte_p = it->multibyte_p;
25587 if (it->glyph_row->reversed_p && area == TEXT_AREA)
25589 /* In R2L rows, the left and the right box edges need to be
25590 drawn in reverse direction. */
25591 glyph->right_box_line_p = it->start_of_box_run_p;
25592 glyph->left_box_line_p = it->end_of_box_run_p;
25594 else
25596 glyph->left_box_line_p = it->start_of_box_run_p;
25597 glyph->right_box_line_p = it->end_of_box_run_p;
25599 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
25600 || it->phys_descent > it->descent);
25601 glyph->padding_p = 0;
25602 glyph->glyph_not_available_p = 0;
25603 glyph->face_id = face_id;
25604 glyph->font_type = FONT_TYPE_UNKNOWN;
25605 if (it->bidi_p)
25607 glyph->resolved_level = it->bidi_it.resolved_level;
25608 if ((it->bidi_it.type & 7) != it->bidi_it.type)
25609 emacs_abort ();
25610 glyph->bidi_type = it->bidi_it.type;
25612 ++it->glyph_row->used[area];
25614 else
25615 IT_EXPAND_MATRIX_WIDTH (it, area);
25619 /* Produce a glyph for a glyphless character for iterator IT.
25620 IT->glyphless_method specifies which method to use for displaying
25621 the character. See the description of enum
25622 glyphless_display_method in dispextern.h for the detail.
25624 FOR_NO_FONT is nonzero if and only if this is for a character for
25625 which no font was found. ACRONYM, if non-nil, is an acronym string
25626 for the character. */
25628 static void
25629 produce_glyphless_glyph (struct it *it, int for_no_font, Lisp_Object acronym)
25631 int face_id;
25632 struct face *face;
25633 struct font *font;
25634 int base_width, base_height, width, height;
25635 short upper_xoff, upper_yoff, lower_xoff, lower_yoff;
25636 int len;
25638 /* Get the metrics of the base font. We always refer to the current
25639 ASCII face. */
25640 face = FACE_FROM_ID (it->f, it->face_id)->ascii_face;
25641 font = face->font ? face->font : FRAME_FONT (it->f);
25642 it->ascent = FONT_BASE (font) + font->baseline_offset;
25643 it->descent = FONT_DESCENT (font) - font->baseline_offset;
25644 base_height = it->ascent + it->descent;
25645 base_width = font->average_width;
25647 face_id = merge_glyphless_glyph_face (it);
25649 if (it->glyphless_method == GLYPHLESS_DISPLAY_THIN_SPACE)
25651 it->pixel_width = THIN_SPACE_WIDTH;
25652 len = 0;
25653 upper_xoff = upper_yoff = lower_xoff = lower_yoff = 0;
25655 else if (it->glyphless_method == GLYPHLESS_DISPLAY_EMPTY_BOX)
25657 width = CHAR_WIDTH (it->c);
25658 if (width == 0)
25659 width = 1;
25660 else if (width > 4)
25661 width = 4;
25662 it->pixel_width = base_width * width;
25663 len = 0;
25664 upper_xoff = upper_yoff = lower_xoff = lower_yoff = 0;
25666 else
25668 char buf[7];
25669 const char *str;
25670 unsigned int code[6];
25671 int upper_len;
25672 int ascent, descent;
25673 struct font_metrics metrics_upper, metrics_lower;
25675 face = FACE_FROM_ID (it->f, face_id);
25676 font = face->font ? face->font : FRAME_FONT (it->f);
25677 PREPARE_FACE_FOR_DISPLAY (it->f, face);
25679 if (it->glyphless_method == GLYPHLESS_DISPLAY_ACRONYM)
25681 if (! STRINGP (acronym) && CHAR_TABLE_P (Vglyphless_char_display))
25682 acronym = CHAR_TABLE_REF (Vglyphless_char_display, it->c);
25683 if (CONSP (acronym))
25684 acronym = XCAR (acronym);
25685 str = STRINGP (acronym) ? SSDATA (acronym) : "";
25687 else
25689 eassert (it->glyphless_method == GLYPHLESS_DISPLAY_HEX_CODE);
25690 sprintf (buf, "%0*X", it->c < 0x10000 ? 4 : 6, it->c);
25691 str = buf;
25693 for (len = 0; str[len] && ASCII_BYTE_P (str[len]) && len < 6; len++)
25694 code[len] = font->driver->encode_char (font, str[len]);
25695 upper_len = (len + 1) / 2;
25696 font->driver->text_extents (font, code, upper_len,
25697 &metrics_upper);
25698 font->driver->text_extents (font, code + upper_len, len - upper_len,
25699 &metrics_lower);
25703 /* +4 is for vertical bars of a box plus 1-pixel spaces at both side. */
25704 width = max (metrics_upper.width, metrics_lower.width) + 4;
25705 upper_xoff = upper_yoff = 2; /* the typical case */
25706 if (base_width >= width)
25708 /* Align the upper to the left, the lower to the right. */
25709 it->pixel_width = base_width;
25710 lower_xoff = base_width - 2 - metrics_lower.width;
25712 else
25714 /* Center the shorter one. */
25715 it->pixel_width = width;
25716 if (metrics_upper.width >= metrics_lower.width)
25717 lower_xoff = (width - metrics_lower.width) / 2;
25718 else
25720 /* FIXME: This code doesn't look right. It formerly was
25721 missing the "lower_xoff = 0;", which couldn't have
25722 been right since it left lower_xoff uninitialized. */
25723 lower_xoff = 0;
25724 upper_xoff = (width - metrics_upper.width) / 2;
25728 /* +5 is for horizontal bars of a box plus 1-pixel spaces at
25729 top, bottom, and between upper and lower strings. */
25730 height = (metrics_upper.ascent + metrics_upper.descent
25731 + metrics_lower.ascent + metrics_lower.descent) + 5;
25732 /* Center vertically.
25733 H:base_height, D:base_descent
25734 h:height, ld:lower_descent, la:lower_ascent, ud:upper_descent
25736 ascent = - (D - H/2 - h/2 + 1); "+ 1" for rounding up
25737 descent = D - H/2 + h/2;
25738 lower_yoff = descent - 2 - ld;
25739 upper_yoff = lower_yoff - la - 1 - ud; */
25740 ascent = - (it->descent - (base_height + height + 1) / 2);
25741 descent = it->descent - (base_height - height) / 2;
25742 lower_yoff = descent - 2 - metrics_lower.descent;
25743 upper_yoff = (lower_yoff - metrics_lower.ascent - 1
25744 - metrics_upper.descent);
25745 /* Don't make the height shorter than the base height. */
25746 if (height > base_height)
25748 it->ascent = ascent;
25749 it->descent = descent;
25753 it->phys_ascent = it->ascent;
25754 it->phys_descent = it->descent;
25755 if (it->glyph_row)
25756 append_glyphless_glyph (it, face_id, for_no_font, len,
25757 upper_xoff, upper_yoff,
25758 lower_xoff, lower_yoff);
25759 it->nglyphs = 1;
25760 take_vertical_position_into_account (it);
25764 /* RIF:
25765 Produce glyphs/get display metrics for the display element IT is
25766 loaded with. See the description of struct it in dispextern.h
25767 for an overview of struct it. */
25769 void
25770 x_produce_glyphs (struct it *it)
25772 int extra_line_spacing = it->extra_line_spacing;
25774 it->glyph_not_available_p = 0;
25776 if (it->what == IT_CHARACTER)
25778 XChar2b char2b;
25779 struct face *face = FACE_FROM_ID (it->f, it->face_id);
25780 struct font *font = face->font;
25781 struct font_metrics *pcm = NULL;
25782 int boff; /* Baseline offset. */
25784 if (font == NULL)
25786 /* When no suitable font is found, display this character by
25787 the method specified in the first extra slot of
25788 Vglyphless_char_display. */
25789 Lisp_Object acronym = lookup_glyphless_char_display (-1, it);
25791 eassert (it->what == IT_GLYPHLESS);
25792 produce_glyphless_glyph (it, 1, STRINGP (acronym) ? acronym : Qnil);
25793 goto done;
25796 boff = font->baseline_offset;
25797 if (font->vertical_centering)
25798 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
25800 if (it->char_to_display != '\n' && it->char_to_display != '\t')
25802 int stretched_p;
25804 it->nglyphs = 1;
25806 if (it->override_ascent >= 0)
25808 it->ascent = it->override_ascent;
25809 it->descent = it->override_descent;
25810 boff = it->override_boff;
25812 else
25814 it->ascent = FONT_BASE (font) + boff;
25815 it->descent = FONT_DESCENT (font) - boff;
25818 if (get_char_glyph_code (it->char_to_display, font, &char2b))
25820 pcm = get_per_char_metric (font, &char2b);
25821 if (pcm->width == 0
25822 && pcm->rbearing == 0 && pcm->lbearing == 0)
25823 pcm = NULL;
25826 if (pcm)
25828 it->phys_ascent = pcm->ascent + boff;
25829 it->phys_descent = pcm->descent - boff;
25830 it->pixel_width = pcm->width;
25832 else
25834 it->glyph_not_available_p = 1;
25835 it->phys_ascent = it->ascent;
25836 it->phys_descent = it->descent;
25837 it->pixel_width = font->space_width;
25840 if (it->constrain_row_ascent_descent_p)
25842 if (it->descent > it->max_descent)
25844 it->ascent += it->descent - it->max_descent;
25845 it->descent = it->max_descent;
25847 if (it->ascent > it->max_ascent)
25849 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
25850 it->ascent = it->max_ascent;
25852 it->phys_ascent = min (it->phys_ascent, it->ascent);
25853 it->phys_descent = min (it->phys_descent, it->descent);
25854 extra_line_spacing = 0;
25857 /* If this is a space inside a region of text with
25858 `space-width' property, change its width. */
25859 stretched_p = it->char_to_display == ' ' && !NILP (it->space_width);
25860 if (stretched_p)
25861 it->pixel_width *= XFLOATINT (it->space_width);
25863 /* If face has a box, add the box thickness to the character
25864 height. If character has a box line to the left and/or
25865 right, add the box line width to the character's width. */
25866 if (face->box != FACE_NO_BOX)
25868 int thick = face->box_line_width;
25870 if (thick > 0)
25872 it->ascent += thick;
25873 it->descent += thick;
25875 else
25876 thick = -thick;
25878 if (it->start_of_box_run_p)
25879 it->pixel_width += thick;
25880 if (it->end_of_box_run_p)
25881 it->pixel_width += thick;
25884 /* If face has an overline, add the height of the overline
25885 (1 pixel) and a 1 pixel margin to the character height. */
25886 if (face->overline_p)
25887 it->ascent += overline_margin;
25889 if (it->constrain_row_ascent_descent_p)
25891 if (it->ascent > it->max_ascent)
25892 it->ascent = it->max_ascent;
25893 if (it->descent > it->max_descent)
25894 it->descent = it->max_descent;
25897 take_vertical_position_into_account (it);
25899 /* If we have to actually produce glyphs, do it. */
25900 if (it->glyph_row)
25902 if (stretched_p)
25904 /* Translate a space with a `space-width' property
25905 into a stretch glyph. */
25906 int ascent = (((it->ascent + it->descent) * FONT_BASE (font))
25907 / FONT_HEIGHT (font));
25908 append_stretch_glyph (it, it->object, it->pixel_width,
25909 it->ascent + it->descent, ascent);
25911 else
25912 append_glyph (it);
25914 /* If characters with lbearing or rbearing are displayed
25915 in this line, record that fact in a flag of the
25916 glyph row. This is used to optimize X output code. */
25917 if (pcm && (pcm->lbearing < 0 || pcm->rbearing > pcm->width))
25918 it->glyph_row->contains_overlapping_glyphs_p = 1;
25920 if (! stretched_p && it->pixel_width == 0)
25921 /* We assure that all visible glyphs have at least 1-pixel
25922 width. */
25923 it->pixel_width = 1;
25925 else if (it->char_to_display == '\n')
25927 /* A newline has no width, but we need the height of the
25928 line. But if previous part of the line sets a height,
25929 don't increase that height. */
25931 Lisp_Object height;
25932 Lisp_Object total_height = Qnil;
25934 it->override_ascent = -1;
25935 it->pixel_width = 0;
25936 it->nglyphs = 0;
25938 height = get_it_property (it, Qline_height);
25939 /* Split (line-height total-height) list. */
25940 if (CONSP (height)
25941 && CONSP (XCDR (height))
25942 && NILP (XCDR (XCDR (height))))
25944 total_height = XCAR (XCDR (height));
25945 height = XCAR (height);
25947 height = calc_line_height_property (it, height, font, boff, 1);
25949 if (it->override_ascent >= 0)
25951 it->ascent = it->override_ascent;
25952 it->descent = it->override_descent;
25953 boff = it->override_boff;
25955 else
25957 it->ascent = FONT_BASE (font) + boff;
25958 it->descent = FONT_DESCENT (font) - boff;
25961 if (EQ (height, Qt))
25963 if (it->descent > it->max_descent)
25965 it->ascent += it->descent - it->max_descent;
25966 it->descent = it->max_descent;
25968 if (it->ascent > it->max_ascent)
25970 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
25971 it->ascent = it->max_ascent;
25973 it->phys_ascent = min (it->phys_ascent, it->ascent);
25974 it->phys_descent = min (it->phys_descent, it->descent);
25975 it->constrain_row_ascent_descent_p = 1;
25976 extra_line_spacing = 0;
25978 else
25980 Lisp_Object spacing;
25982 it->phys_ascent = it->ascent;
25983 it->phys_descent = it->descent;
25985 if ((it->max_ascent > 0 || it->max_descent > 0)
25986 && face->box != FACE_NO_BOX
25987 && face->box_line_width > 0)
25989 it->ascent += face->box_line_width;
25990 it->descent += face->box_line_width;
25992 if (!NILP (height)
25993 && XINT (height) > it->ascent + it->descent)
25994 it->ascent = XINT (height) - it->descent;
25996 if (!NILP (total_height))
25997 spacing = calc_line_height_property (it, total_height, font, boff, 0);
25998 else
26000 spacing = get_it_property (it, Qline_spacing);
26001 spacing = calc_line_height_property (it, spacing, font, boff, 0);
26003 if (INTEGERP (spacing))
26005 extra_line_spacing = XINT (spacing);
26006 if (!NILP (total_height))
26007 extra_line_spacing -= (it->phys_ascent + it->phys_descent);
26011 else /* i.e. (it->char_to_display == '\t') */
26013 if (font->space_width > 0)
26015 int tab_width = it->tab_width * font->space_width;
26016 int x = it->current_x + it->continuation_lines_width;
26017 int next_tab_x = ((1 + x + tab_width - 1) / tab_width) * tab_width;
26019 /* If the distance from the current position to the next tab
26020 stop is less than a space character width, use the
26021 tab stop after that. */
26022 if (next_tab_x - x < font->space_width)
26023 next_tab_x += tab_width;
26025 it->pixel_width = next_tab_x - x;
26026 it->nglyphs = 1;
26027 it->ascent = it->phys_ascent = FONT_BASE (font) + boff;
26028 it->descent = it->phys_descent = FONT_DESCENT (font) - boff;
26030 if (it->glyph_row)
26032 append_stretch_glyph (it, it->object, it->pixel_width,
26033 it->ascent + it->descent, it->ascent);
26036 else
26038 it->pixel_width = 0;
26039 it->nglyphs = 1;
26043 else if (it->what == IT_COMPOSITION && it->cmp_it.ch < 0)
26045 /* A static composition.
26047 Note: A composition is represented as one glyph in the
26048 glyph matrix. There are no padding glyphs.
26050 Important note: pixel_width, ascent, and descent are the
26051 values of what is drawn by draw_glyphs (i.e. the values of
26052 the overall glyphs composed). */
26053 struct face *face = FACE_FROM_ID (it->f, it->face_id);
26054 int boff; /* baseline offset */
26055 struct composition *cmp = composition_table[it->cmp_it.id];
26056 int glyph_len = cmp->glyph_len;
26057 struct font *font = face->font;
26059 it->nglyphs = 1;
26061 /* If we have not yet calculated pixel size data of glyphs of
26062 the composition for the current face font, calculate them
26063 now. Theoretically, we have to check all fonts for the
26064 glyphs, but that requires much time and memory space. So,
26065 here we check only the font of the first glyph. This may
26066 lead to incorrect display, but it's very rare, and C-l
26067 (recenter-top-bottom) can correct the display anyway. */
26068 if (! cmp->font || cmp->font != font)
26070 /* Ascent and descent of the font of the first character
26071 of this composition (adjusted by baseline offset).
26072 Ascent and descent of overall glyphs should not be less
26073 than these, respectively. */
26074 int font_ascent, font_descent, font_height;
26075 /* Bounding box of the overall glyphs. */
26076 int leftmost, rightmost, lowest, highest;
26077 int lbearing, rbearing;
26078 int i, width, ascent, descent;
26079 int left_padded = 0, right_padded = 0;
26080 int c IF_LINT (= 0); /* cmp->glyph_len can't be zero; see Bug#8512 */
26081 XChar2b char2b;
26082 struct font_metrics *pcm;
26083 int font_not_found_p;
26084 ptrdiff_t pos;
26086 for (glyph_len = cmp->glyph_len; glyph_len > 0; glyph_len--)
26087 if ((c = COMPOSITION_GLYPH (cmp, glyph_len - 1)) != '\t')
26088 break;
26089 if (glyph_len < cmp->glyph_len)
26090 right_padded = 1;
26091 for (i = 0; i < glyph_len; i++)
26093 if ((c = COMPOSITION_GLYPH (cmp, i)) != '\t')
26094 break;
26095 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
26097 if (i > 0)
26098 left_padded = 1;
26100 pos = (STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
26101 : IT_CHARPOS (*it));
26102 /* If no suitable font is found, use the default font. */
26103 font_not_found_p = font == NULL;
26104 if (font_not_found_p)
26106 face = face->ascii_face;
26107 font = face->font;
26109 boff = font->baseline_offset;
26110 if (font->vertical_centering)
26111 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
26112 font_ascent = FONT_BASE (font) + boff;
26113 font_descent = FONT_DESCENT (font) - boff;
26114 font_height = FONT_HEIGHT (font);
26116 cmp->font = font;
26118 pcm = NULL;
26119 if (! font_not_found_p)
26121 get_char_face_and_encoding (it->f, c, it->face_id,
26122 &char2b, 0);
26123 pcm = get_per_char_metric (font, &char2b);
26126 /* Initialize the bounding box. */
26127 if (pcm)
26129 width = cmp->glyph_len > 0 ? pcm->width : 0;
26130 ascent = pcm->ascent;
26131 descent = pcm->descent;
26132 lbearing = pcm->lbearing;
26133 rbearing = pcm->rbearing;
26135 else
26137 width = cmp->glyph_len > 0 ? font->space_width : 0;
26138 ascent = FONT_BASE (font);
26139 descent = FONT_DESCENT (font);
26140 lbearing = 0;
26141 rbearing = width;
26144 rightmost = width;
26145 leftmost = 0;
26146 lowest = - descent + boff;
26147 highest = ascent + boff;
26149 if (! font_not_found_p
26150 && font->default_ascent
26151 && CHAR_TABLE_P (Vuse_default_ascent)
26152 && !NILP (Faref (Vuse_default_ascent,
26153 make_number (it->char_to_display))))
26154 highest = font->default_ascent + boff;
26156 /* Draw the first glyph at the normal position. It may be
26157 shifted to right later if some other glyphs are drawn
26158 at the left. */
26159 cmp->offsets[i * 2] = 0;
26160 cmp->offsets[i * 2 + 1] = boff;
26161 cmp->lbearing = lbearing;
26162 cmp->rbearing = rbearing;
26164 /* Set cmp->offsets for the remaining glyphs. */
26165 for (i++; i < glyph_len; i++)
26167 int left, right, btm, top;
26168 int ch = COMPOSITION_GLYPH (cmp, i);
26169 int face_id;
26170 struct face *this_face;
26172 if (ch == '\t')
26173 ch = ' ';
26174 face_id = FACE_FOR_CHAR (it->f, face, ch, pos, it->string);
26175 this_face = FACE_FROM_ID (it->f, face_id);
26176 font = this_face->font;
26178 if (font == NULL)
26179 pcm = NULL;
26180 else
26182 get_char_face_and_encoding (it->f, ch, face_id,
26183 &char2b, 0);
26184 pcm = get_per_char_metric (font, &char2b);
26186 if (! pcm)
26187 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
26188 else
26190 width = pcm->width;
26191 ascent = pcm->ascent;
26192 descent = pcm->descent;
26193 lbearing = pcm->lbearing;
26194 rbearing = pcm->rbearing;
26195 if (cmp->method != COMPOSITION_WITH_RULE_ALTCHARS)
26197 /* Relative composition with or without
26198 alternate chars. */
26199 left = (leftmost + rightmost - width) / 2;
26200 btm = - descent + boff;
26201 if (font->relative_compose
26202 && (! CHAR_TABLE_P (Vignore_relative_composition)
26203 || NILP (Faref (Vignore_relative_composition,
26204 make_number (ch)))))
26207 if (- descent >= font->relative_compose)
26208 /* One extra pixel between two glyphs. */
26209 btm = highest + 1;
26210 else if (ascent <= 0)
26211 /* One extra pixel between two glyphs. */
26212 btm = lowest - 1 - ascent - descent;
26215 else
26217 /* A composition rule is specified by an integer
26218 value that encodes global and new reference
26219 points (GREF and NREF). GREF and NREF are
26220 specified by numbers as below:
26222 0---1---2 -- ascent
26226 9--10--11 -- center
26228 ---3---4---5--- baseline
26230 6---7---8 -- descent
26232 int rule = COMPOSITION_RULE (cmp, i);
26233 int gref, nref, grefx, grefy, nrefx, nrefy, xoff, yoff;
26235 COMPOSITION_DECODE_RULE (rule, gref, nref, xoff, yoff);
26236 grefx = gref % 3, nrefx = nref % 3;
26237 grefy = gref / 3, nrefy = nref / 3;
26238 if (xoff)
26239 xoff = font_height * (xoff - 128) / 256;
26240 if (yoff)
26241 yoff = font_height * (yoff - 128) / 256;
26243 left = (leftmost
26244 + grefx * (rightmost - leftmost) / 2
26245 - nrefx * width / 2
26246 + xoff);
26248 btm = ((grefy == 0 ? highest
26249 : grefy == 1 ? 0
26250 : grefy == 2 ? lowest
26251 : (highest + lowest) / 2)
26252 - (nrefy == 0 ? ascent + descent
26253 : nrefy == 1 ? descent - boff
26254 : nrefy == 2 ? 0
26255 : (ascent + descent) / 2)
26256 + yoff);
26259 cmp->offsets[i * 2] = left;
26260 cmp->offsets[i * 2 + 1] = btm + descent;
26262 /* Update the bounding box of the overall glyphs. */
26263 if (width > 0)
26265 right = left + width;
26266 if (left < leftmost)
26267 leftmost = left;
26268 if (right > rightmost)
26269 rightmost = right;
26271 top = btm + descent + ascent;
26272 if (top > highest)
26273 highest = top;
26274 if (btm < lowest)
26275 lowest = btm;
26277 if (cmp->lbearing > left + lbearing)
26278 cmp->lbearing = left + lbearing;
26279 if (cmp->rbearing < left + rbearing)
26280 cmp->rbearing = left + rbearing;
26284 /* If there are glyphs whose x-offsets are negative,
26285 shift all glyphs to the right and make all x-offsets
26286 non-negative. */
26287 if (leftmost < 0)
26289 for (i = 0; i < cmp->glyph_len; i++)
26290 cmp->offsets[i * 2] -= leftmost;
26291 rightmost -= leftmost;
26292 cmp->lbearing -= leftmost;
26293 cmp->rbearing -= leftmost;
26296 if (left_padded && cmp->lbearing < 0)
26298 for (i = 0; i < cmp->glyph_len; i++)
26299 cmp->offsets[i * 2] -= cmp->lbearing;
26300 rightmost -= cmp->lbearing;
26301 cmp->rbearing -= cmp->lbearing;
26302 cmp->lbearing = 0;
26304 if (right_padded && rightmost < cmp->rbearing)
26306 rightmost = cmp->rbearing;
26309 cmp->pixel_width = rightmost;
26310 cmp->ascent = highest;
26311 cmp->descent = - lowest;
26312 if (cmp->ascent < font_ascent)
26313 cmp->ascent = font_ascent;
26314 if (cmp->descent < font_descent)
26315 cmp->descent = font_descent;
26318 if (it->glyph_row
26319 && (cmp->lbearing < 0
26320 || cmp->rbearing > cmp->pixel_width))
26321 it->glyph_row->contains_overlapping_glyphs_p = 1;
26323 it->pixel_width = cmp->pixel_width;
26324 it->ascent = it->phys_ascent = cmp->ascent;
26325 it->descent = it->phys_descent = cmp->descent;
26326 if (face->box != FACE_NO_BOX)
26328 int thick = face->box_line_width;
26330 if (thick > 0)
26332 it->ascent += thick;
26333 it->descent += thick;
26335 else
26336 thick = - thick;
26338 if (it->start_of_box_run_p)
26339 it->pixel_width += thick;
26340 if (it->end_of_box_run_p)
26341 it->pixel_width += thick;
26344 /* If face has an overline, add the height of the overline
26345 (1 pixel) and a 1 pixel margin to the character height. */
26346 if (face->overline_p)
26347 it->ascent += overline_margin;
26349 take_vertical_position_into_account (it);
26350 if (it->ascent < 0)
26351 it->ascent = 0;
26352 if (it->descent < 0)
26353 it->descent = 0;
26355 if (it->glyph_row && cmp->glyph_len > 0)
26356 append_composite_glyph (it);
26358 else if (it->what == IT_COMPOSITION)
26360 /* A dynamic (automatic) composition. */
26361 struct face *face = FACE_FROM_ID (it->f, it->face_id);
26362 Lisp_Object gstring;
26363 struct font_metrics metrics;
26365 it->nglyphs = 1;
26367 gstring = composition_gstring_from_id (it->cmp_it.id);
26368 it->pixel_width
26369 = composition_gstring_width (gstring, it->cmp_it.from, it->cmp_it.to,
26370 &metrics);
26371 if (it->glyph_row
26372 && (metrics.lbearing < 0 || metrics.rbearing > metrics.width))
26373 it->glyph_row->contains_overlapping_glyphs_p = 1;
26374 it->ascent = it->phys_ascent = metrics.ascent;
26375 it->descent = it->phys_descent = metrics.descent;
26376 if (face->box != FACE_NO_BOX)
26378 int thick = face->box_line_width;
26380 if (thick > 0)
26382 it->ascent += thick;
26383 it->descent += thick;
26385 else
26386 thick = - thick;
26388 if (it->start_of_box_run_p)
26389 it->pixel_width += thick;
26390 if (it->end_of_box_run_p)
26391 it->pixel_width += thick;
26393 /* If face has an overline, add the height of the overline
26394 (1 pixel) and a 1 pixel margin to the character height. */
26395 if (face->overline_p)
26396 it->ascent += overline_margin;
26397 take_vertical_position_into_account (it);
26398 if (it->ascent < 0)
26399 it->ascent = 0;
26400 if (it->descent < 0)
26401 it->descent = 0;
26403 if (it->glyph_row)
26404 append_composite_glyph (it);
26406 else if (it->what == IT_GLYPHLESS)
26407 produce_glyphless_glyph (it, 0, Qnil);
26408 else if (it->what == IT_IMAGE)
26409 produce_image_glyph (it);
26410 else if (it->what == IT_STRETCH)
26411 produce_stretch_glyph (it);
26413 done:
26414 /* Accumulate dimensions. Note: can't assume that it->descent > 0
26415 because this isn't true for images with `:ascent 100'. */
26416 eassert (it->ascent >= 0 && it->descent >= 0);
26417 if (it->area == TEXT_AREA)
26418 it->current_x += it->pixel_width;
26420 if (extra_line_spacing > 0)
26422 it->descent += extra_line_spacing;
26423 if (extra_line_spacing > it->max_extra_line_spacing)
26424 it->max_extra_line_spacing = extra_line_spacing;
26427 it->max_ascent = max (it->max_ascent, it->ascent);
26428 it->max_descent = max (it->max_descent, it->descent);
26429 it->max_phys_ascent = max (it->max_phys_ascent, it->phys_ascent);
26430 it->max_phys_descent = max (it->max_phys_descent, it->phys_descent);
26433 /* EXPORT for RIF:
26434 Output LEN glyphs starting at START at the nominal cursor position.
26435 Advance the nominal cursor over the text. UPDATED_ROW is the glyph row
26436 being updated, and UPDATED_AREA is the area of that row being updated. */
26438 void
26439 x_write_glyphs (struct window *w, struct glyph_row *updated_row,
26440 struct glyph *start, enum glyph_row_area updated_area, int len)
26442 int x, hpos, chpos = w->phys_cursor.hpos;
26444 eassert (updated_row);
26445 /* When the window is hscrolled, cursor hpos can legitimately be out
26446 of bounds, but we draw the cursor at the corresponding window
26447 margin in that case. */
26448 if (!updated_row->reversed_p && chpos < 0)
26449 chpos = 0;
26450 if (updated_row->reversed_p && chpos >= updated_row->used[TEXT_AREA])
26451 chpos = updated_row->used[TEXT_AREA] - 1;
26453 block_input ();
26455 /* Write glyphs. */
26457 hpos = start - updated_row->glyphs[updated_area];
26458 x = draw_glyphs (w, w->output_cursor.x,
26459 updated_row, updated_area,
26460 hpos, hpos + len,
26461 DRAW_NORMAL_TEXT, 0);
26463 /* Invalidate old phys cursor if the glyph at its hpos is redrawn. */
26464 if (updated_area == TEXT_AREA
26465 && w->phys_cursor_on_p
26466 && w->phys_cursor.vpos == w->output_cursor.vpos
26467 && chpos >= hpos
26468 && chpos < hpos + len)
26469 w->phys_cursor_on_p = 0;
26471 unblock_input ();
26473 /* Advance the output cursor. */
26474 w->output_cursor.hpos += len;
26475 w->output_cursor.x = x;
26479 /* EXPORT for RIF:
26480 Insert LEN glyphs from START at the nominal cursor position. */
26482 void
26483 x_insert_glyphs (struct window *w, struct glyph_row *updated_row,
26484 struct glyph *start, enum glyph_row_area updated_area, int len)
26486 struct frame *f;
26487 int line_height, shift_by_width, shifted_region_width;
26488 struct glyph_row *row;
26489 struct glyph *glyph;
26490 int frame_x, frame_y;
26491 ptrdiff_t hpos;
26493 eassert (updated_row);
26494 block_input ();
26495 f = XFRAME (WINDOW_FRAME (w));
26497 /* Get the height of the line we are in. */
26498 row = updated_row;
26499 line_height = row->height;
26501 /* Get the width of the glyphs to insert. */
26502 shift_by_width = 0;
26503 for (glyph = start; glyph < start + len; ++glyph)
26504 shift_by_width += glyph->pixel_width;
26506 /* Get the width of the region to shift right. */
26507 shifted_region_width = (window_box_width (w, updated_area)
26508 - w->output_cursor.x
26509 - shift_by_width);
26511 /* Shift right. */
26512 frame_x = window_box_left (w, updated_area) + w->output_cursor.x;
26513 frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, w->output_cursor.y);
26515 FRAME_RIF (f)->shift_glyphs_for_insert (f, frame_x, frame_y, shifted_region_width,
26516 line_height, shift_by_width);
26518 /* Write the glyphs. */
26519 hpos = start - row->glyphs[updated_area];
26520 draw_glyphs (w, w->output_cursor.x, row, updated_area,
26521 hpos, hpos + len,
26522 DRAW_NORMAL_TEXT, 0);
26524 /* Advance the output cursor. */
26525 w->output_cursor.hpos += len;
26526 w->output_cursor.x += shift_by_width;
26527 unblock_input ();
26531 /* EXPORT for RIF:
26532 Erase the current text line from the nominal cursor position
26533 (inclusive) to pixel column TO_X (exclusive). The idea is that
26534 everything from TO_X onward is already erased.
26536 TO_X is a pixel position relative to UPDATED_AREA of currently
26537 updated window W. TO_X == -1 means clear to the end of this area. */
26539 void
26540 x_clear_end_of_line (struct window *w, struct glyph_row *updated_row,
26541 enum glyph_row_area updated_area, int to_x)
26543 struct frame *f;
26544 int max_x, min_y, max_y;
26545 int from_x, from_y, to_y;
26547 eassert (updated_row);
26548 f = XFRAME (w->frame);
26550 if (updated_row->full_width_p)
26551 max_x = (WINDOW_PIXEL_WIDTH (w)
26552 - (updated_row->mode_line_p ? WINDOW_RIGHT_DIVIDER_WIDTH (w) : 0));
26553 else
26554 max_x = window_box_width (w, updated_area);
26555 max_y = window_text_bottom_y (w);
26557 /* TO_X == 0 means don't do anything. TO_X < 0 means clear to end
26558 of window. For TO_X > 0, truncate to end of drawing area. */
26559 if (to_x == 0)
26560 return;
26561 else if (to_x < 0)
26562 to_x = max_x;
26563 else
26564 to_x = min (to_x, max_x);
26566 to_y = min (max_y, w->output_cursor.y + updated_row->height);
26568 /* Notice if the cursor will be cleared by this operation. */
26569 if (!updated_row->full_width_p)
26570 notice_overwritten_cursor (w, updated_area,
26571 w->output_cursor.x, -1,
26572 updated_row->y,
26573 MATRIX_ROW_BOTTOM_Y (updated_row));
26575 from_x = w->output_cursor.x;
26577 /* Translate to frame coordinates. */
26578 if (updated_row->full_width_p)
26580 from_x = WINDOW_TO_FRAME_PIXEL_X (w, from_x);
26581 to_x = WINDOW_TO_FRAME_PIXEL_X (w, to_x);
26583 else
26585 int area_left = window_box_left (w, updated_area);
26586 from_x += area_left;
26587 to_x += area_left;
26590 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
26591 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, max (min_y, w->output_cursor.y));
26592 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, to_y);
26594 /* Prevent inadvertently clearing to end of the X window. */
26595 if (to_x > from_x && to_y > from_y)
26597 block_input ();
26598 FRAME_RIF (f)->clear_frame_area (f, from_x, from_y,
26599 to_x - from_x, to_y - from_y);
26600 unblock_input ();
26604 #endif /* HAVE_WINDOW_SYSTEM */
26608 /***********************************************************************
26609 Cursor types
26610 ***********************************************************************/
26612 /* Value is the internal representation of the specified cursor type
26613 ARG. If type is BAR_CURSOR, return in *WIDTH the specified width
26614 of the bar cursor. */
26616 static enum text_cursor_kinds
26617 get_specified_cursor_type (Lisp_Object arg, int *width)
26619 enum text_cursor_kinds type;
26621 if (NILP (arg))
26622 return NO_CURSOR;
26624 if (EQ (arg, Qbox))
26625 return FILLED_BOX_CURSOR;
26627 if (EQ (arg, Qhollow))
26628 return HOLLOW_BOX_CURSOR;
26630 if (EQ (arg, Qbar))
26632 *width = 2;
26633 return BAR_CURSOR;
26636 if (CONSP (arg)
26637 && EQ (XCAR (arg), Qbar)
26638 && RANGED_INTEGERP (0, XCDR (arg), INT_MAX))
26640 *width = XINT (XCDR (arg));
26641 return BAR_CURSOR;
26644 if (EQ (arg, Qhbar))
26646 *width = 2;
26647 return HBAR_CURSOR;
26650 if (CONSP (arg)
26651 && EQ (XCAR (arg), Qhbar)
26652 && RANGED_INTEGERP (0, XCDR (arg), INT_MAX))
26654 *width = XINT (XCDR (arg));
26655 return HBAR_CURSOR;
26658 /* Treat anything unknown as "hollow box cursor".
26659 It was bad to signal an error; people have trouble fixing
26660 .Xdefaults with Emacs, when it has something bad in it. */
26661 type = HOLLOW_BOX_CURSOR;
26663 return type;
26666 /* Set the default cursor types for specified frame. */
26667 void
26668 set_frame_cursor_types (struct frame *f, Lisp_Object arg)
26670 int width = 1;
26671 Lisp_Object tem;
26673 FRAME_DESIRED_CURSOR (f) = get_specified_cursor_type (arg, &width);
26674 FRAME_CURSOR_WIDTH (f) = width;
26676 /* By default, set up the blink-off state depending on the on-state. */
26678 tem = Fassoc (arg, Vblink_cursor_alist);
26679 if (!NILP (tem))
26681 FRAME_BLINK_OFF_CURSOR (f)
26682 = get_specified_cursor_type (XCDR (tem), &width);
26683 FRAME_BLINK_OFF_CURSOR_WIDTH (f) = width;
26685 else
26686 FRAME_BLINK_OFF_CURSOR (f) = DEFAULT_CURSOR;
26688 /* Make sure the cursor gets redrawn. */
26689 f->cursor_type_changed = 1;
26693 #ifdef HAVE_WINDOW_SYSTEM
26695 /* Return the cursor we want to be displayed in window W. Return
26696 width of bar/hbar cursor through WIDTH arg. Return with
26697 ACTIVE_CURSOR arg set to 1 if cursor in window W is `active'
26698 (i.e. if the `system caret' should track this cursor).
26700 In a mini-buffer window, we want the cursor only to appear if we
26701 are reading input from this window. For the selected window, we
26702 want the cursor type given by the frame parameter or buffer local
26703 setting of cursor-type. If explicitly marked off, draw no cursor.
26704 In all other cases, we want a hollow box cursor. */
26706 static enum text_cursor_kinds
26707 get_window_cursor_type (struct window *w, struct glyph *glyph, int *width,
26708 int *active_cursor)
26710 struct frame *f = XFRAME (w->frame);
26711 struct buffer *b = XBUFFER (w->contents);
26712 int cursor_type = DEFAULT_CURSOR;
26713 Lisp_Object alt_cursor;
26714 int non_selected = 0;
26716 *active_cursor = 1;
26718 /* Echo area */
26719 if (cursor_in_echo_area
26720 && FRAME_HAS_MINIBUF_P (f)
26721 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
26723 if (w == XWINDOW (echo_area_window))
26725 if (EQ (BVAR (b, cursor_type), Qt) || NILP (BVAR (b, cursor_type)))
26727 *width = FRAME_CURSOR_WIDTH (f);
26728 return FRAME_DESIRED_CURSOR (f);
26730 else
26731 return get_specified_cursor_type (BVAR (b, cursor_type), width);
26734 *active_cursor = 0;
26735 non_selected = 1;
26738 /* Detect a nonselected window or nonselected frame. */
26739 else if (w != XWINDOW (f->selected_window)
26740 || f != FRAME_DISPLAY_INFO (f)->x_highlight_frame)
26742 *active_cursor = 0;
26744 if (MINI_WINDOW_P (w) && minibuf_level == 0)
26745 return NO_CURSOR;
26747 non_selected = 1;
26750 /* Never display a cursor in a window in which cursor-type is nil. */
26751 if (NILP (BVAR (b, cursor_type)))
26752 return NO_CURSOR;
26754 /* Get the normal cursor type for this window. */
26755 if (EQ (BVAR (b, cursor_type), Qt))
26757 cursor_type = FRAME_DESIRED_CURSOR (f);
26758 *width = FRAME_CURSOR_WIDTH (f);
26760 else
26761 cursor_type = get_specified_cursor_type (BVAR (b, cursor_type), width);
26763 /* Use cursor-in-non-selected-windows instead
26764 for non-selected window or frame. */
26765 if (non_selected)
26767 alt_cursor = BVAR (b, cursor_in_non_selected_windows);
26768 if (!EQ (Qt, alt_cursor))
26769 return get_specified_cursor_type (alt_cursor, width);
26770 /* t means modify the normal cursor type. */
26771 if (cursor_type == FILLED_BOX_CURSOR)
26772 cursor_type = HOLLOW_BOX_CURSOR;
26773 else if (cursor_type == BAR_CURSOR && *width > 1)
26774 --*width;
26775 return cursor_type;
26778 /* Use normal cursor if not blinked off. */
26779 if (!w->cursor_off_p)
26781 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
26783 if (cursor_type == FILLED_BOX_CURSOR)
26785 /* Using a block cursor on large images can be very annoying.
26786 So use a hollow cursor for "large" images.
26787 If image is not transparent (no mask), also use hollow cursor. */
26788 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
26789 if (img != NULL && IMAGEP (img->spec))
26791 /* Arbitrarily, interpret "Large" as >32x32 and >NxN
26792 where N = size of default frame font size.
26793 This should cover most of the "tiny" icons people may use. */
26794 if (!img->mask
26795 || img->width > max (32, WINDOW_FRAME_COLUMN_WIDTH (w))
26796 || img->height > max (32, WINDOW_FRAME_LINE_HEIGHT (w)))
26797 cursor_type = HOLLOW_BOX_CURSOR;
26800 else if (cursor_type != NO_CURSOR)
26802 /* Display current only supports BOX and HOLLOW cursors for images.
26803 So for now, unconditionally use a HOLLOW cursor when cursor is
26804 not a solid box cursor. */
26805 cursor_type = HOLLOW_BOX_CURSOR;
26808 return cursor_type;
26811 /* Cursor is blinked off, so determine how to "toggle" it. */
26813 /* First look for an entry matching the buffer's cursor-type in blink-cursor-alist. */
26814 if ((alt_cursor = Fassoc (BVAR (b, cursor_type), Vblink_cursor_alist), !NILP (alt_cursor)))
26815 return get_specified_cursor_type (XCDR (alt_cursor), width);
26817 /* Then see if frame has specified a specific blink off cursor type. */
26818 if (FRAME_BLINK_OFF_CURSOR (f) != DEFAULT_CURSOR)
26820 *width = FRAME_BLINK_OFF_CURSOR_WIDTH (f);
26821 return FRAME_BLINK_OFF_CURSOR (f);
26824 #if 0
26825 /* Some people liked having a permanently visible blinking cursor,
26826 while others had very strong opinions against it. So it was
26827 decided to remove it. KFS 2003-09-03 */
26829 /* Finally perform built-in cursor blinking:
26830 filled box <-> hollow box
26831 wide [h]bar <-> narrow [h]bar
26832 narrow [h]bar <-> no cursor
26833 other type <-> no cursor */
26835 if (cursor_type == FILLED_BOX_CURSOR)
26836 return HOLLOW_BOX_CURSOR;
26838 if ((cursor_type == BAR_CURSOR || cursor_type == HBAR_CURSOR) && *width > 1)
26840 *width = 1;
26841 return cursor_type;
26843 #endif
26845 return NO_CURSOR;
26849 /* Notice when the text cursor of window W has been completely
26850 overwritten by a drawing operation that outputs glyphs in AREA
26851 starting at X0 and ending at X1 in the line starting at Y0 and
26852 ending at Y1. X coordinates are area-relative. X1 < 0 means all
26853 the rest of the line after X0 has been written. Y coordinates
26854 are window-relative. */
26856 static void
26857 notice_overwritten_cursor (struct window *w, enum glyph_row_area area,
26858 int x0, int x1, int y0, int y1)
26860 int cx0, cx1, cy0, cy1;
26861 struct glyph_row *row;
26863 if (!w->phys_cursor_on_p)
26864 return;
26865 if (area != TEXT_AREA)
26866 return;
26868 if (w->phys_cursor.vpos < 0
26869 || w->phys_cursor.vpos >= w->current_matrix->nrows
26870 || (row = w->current_matrix->rows + w->phys_cursor.vpos,
26871 !(row->enabled_p && MATRIX_ROW_DISPLAYS_TEXT_P (row))))
26872 return;
26874 if (row->cursor_in_fringe_p)
26876 row->cursor_in_fringe_p = 0;
26877 draw_fringe_bitmap (w, row, row->reversed_p);
26878 w->phys_cursor_on_p = 0;
26879 return;
26882 cx0 = w->phys_cursor.x;
26883 cx1 = cx0 + w->phys_cursor_width;
26884 if (x0 > cx0 || (x1 >= 0 && x1 < cx1))
26885 return;
26887 /* The cursor image will be completely removed from the
26888 screen if the output area intersects the cursor area in
26889 y-direction. When we draw in [y0 y1[, and some part of
26890 the cursor is at y < y0, that part must have been drawn
26891 before. When scrolling, the cursor is erased before
26892 actually scrolling, so we don't come here. When not
26893 scrolling, the rows above the old cursor row must have
26894 changed, and in this case these rows must have written
26895 over the cursor image.
26897 Likewise if part of the cursor is below y1, with the
26898 exception of the cursor being in the first blank row at
26899 the buffer and window end because update_text_area
26900 doesn't draw that row. (Except when it does, but
26901 that's handled in update_text_area.) */
26903 cy0 = w->phys_cursor.y;
26904 cy1 = cy0 + w->phys_cursor_height;
26905 if ((y0 < cy0 || y0 >= cy1) && (y1 <= cy0 || y1 >= cy1))
26906 return;
26908 w->phys_cursor_on_p = 0;
26911 #endif /* HAVE_WINDOW_SYSTEM */
26914 /************************************************************************
26915 Mouse Face
26916 ************************************************************************/
26918 #ifdef HAVE_WINDOW_SYSTEM
26920 /* EXPORT for RIF:
26921 Fix the display of area AREA of overlapping row ROW in window W
26922 with respect to the overlapping part OVERLAPS. */
26924 void
26925 x_fix_overlapping_area (struct window *w, struct glyph_row *row,
26926 enum glyph_row_area area, int overlaps)
26928 int i, x;
26930 block_input ();
26932 x = 0;
26933 for (i = 0; i < row->used[area];)
26935 if (row->glyphs[area][i].overlaps_vertically_p)
26937 int start = i, start_x = x;
26941 x += row->glyphs[area][i].pixel_width;
26942 ++i;
26944 while (i < row->used[area]
26945 && row->glyphs[area][i].overlaps_vertically_p);
26947 draw_glyphs (w, start_x, row, area,
26948 start, i,
26949 DRAW_NORMAL_TEXT, overlaps);
26951 else
26953 x += row->glyphs[area][i].pixel_width;
26954 ++i;
26958 unblock_input ();
26962 /* EXPORT:
26963 Draw the cursor glyph of window W in glyph row ROW. See the
26964 comment of draw_glyphs for the meaning of HL. */
26966 void
26967 draw_phys_cursor_glyph (struct window *w, struct glyph_row *row,
26968 enum draw_glyphs_face hl)
26970 /* If cursor hpos is out of bounds, don't draw garbage. This can
26971 happen in mini-buffer windows when switching between echo area
26972 glyphs and mini-buffer. */
26973 if ((row->reversed_p
26974 ? (w->phys_cursor.hpos >= 0)
26975 : (w->phys_cursor.hpos < row->used[TEXT_AREA])))
26977 int on_p = w->phys_cursor_on_p;
26978 int x1;
26979 int hpos = w->phys_cursor.hpos;
26981 /* When the window is hscrolled, cursor hpos can legitimately be
26982 out of bounds, but we draw the cursor at the corresponding
26983 window margin in that case. */
26984 if (!row->reversed_p && hpos < 0)
26985 hpos = 0;
26986 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
26987 hpos = row->used[TEXT_AREA] - 1;
26989 x1 = draw_glyphs (w, w->phys_cursor.x, row, TEXT_AREA, hpos, hpos + 1,
26990 hl, 0);
26991 w->phys_cursor_on_p = on_p;
26993 if (hl == DRAW_CURSOR)
26994 w->phys_cursor_width = x1 - w->phys_cursor.x;
26995 /* When we erase the cursor, and ROW is overlapped by other
26996 rows, make sure that these overlapping parts of other rows
26997 are redrawn. */
26998 else if (hl == DRAW_NORMAL_TEXT && row->overlapped_p)
27000 w->phys_cursor_width = x1 - w->phys_cursor.x;
27002 if (row > w->current_matrix->rows
27003 && MATRIX_ROW_OVERLAPS_SUCC_P (row - 1))
27004 x_fix_overlapping_area (w, row - 1, TEXT_AREA,
27005 OVERLAPS_ERASED_CURSOR);
27007 if (MATRIX_ROW_BOTTOM_Y (row) < window_text_bottom_y (w)
27008 && MATRIX_ROW_OVERLAPS_PRED_P (row + 1))
27009 x_fix_overlapping_area (w, row + 1, TEXT_AREA,
27010 OVERLAPS_ERASED_CURSOR);
27016 /* Erase the image of a cursor of window W from the screen. */
27018 #ifndef HAVE_NTGUI
27019 static
27020 #endif
27021 void
27022 erase_phys_cursor (struct window *w)
27024 struct frame *f = XFRAME (w->frame);
27025 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
27026 int hpos = w->phys_cursor.hpos;
27027 int vpos = w->phys_cursor.vpos;
27028 int mouse_face_here_p = 0;
27029 struct glyph_matrix *active_glyphs = w->current_matrix;
27030 struct glyph_row *cursor_row;
27031 struct glyph *cursor_glyph;
27032 enum draw_glyphs_face hl;
27034 /* No cursor displayed or row invalidated => nothing to do on the
27035 screen. */
27036 if (w->phys_cursor_type == NO_CURSOR)
27037 goto mark_cursor_off;
27039 /* VPOS >= active_glyphs->nrows means that window has been resized.
27040 Don't bother to erase the cursor. */
27041 if (vpos >= active_glyphs->nrows)
27042 goto mark_cursor_off;
27044 /* If row containing cursor is marked invalid, there is nothing we
27045 can do. */
27046 cursor_row = MATRIX_ROW (active_glyphs, vpos);
27047 if (!cursor_row->enabled_p)
27048 goto mark_cursor_off;
27050 /* If line spacing is > 0, old cursor may only be partially visible in
27051 window after split-window. So adjust visible height. */
27052 cursor_row->visible_height = min (cursor_row->visible_height,
27053 window_text_bottom_y (w) - cursor_row->y);
27055 /* If row is completely invisible, don't attempt to delete a cursor which
27056 isn't there. This can happen if cursor is at top of a window, and
27057 we switch to a buffer with a header line in that window. */
27058 if (cursor_row->visible_height <= 0)
27059 goto mark_cursor_off;
27061 /* If cursor is in the fringe, erase by drawing actual bitmap there. */
27062 if (cursor_row->cursor_in_fringe_p)
27064 cursor_row->cursor_in_fringe_p = 0;
27065 draw_fringe_bitmap (w, cursor_row, cursor_row->reversed_p);
27066 goto mark_cursor_off;
27069 /* This can happen when the new row is shorter than the old one.
27070 In this case, either draw_glyphs or clear_end_of_line
27071 should have cleared the cursor. Note that we wouldn't be
27072 able to erase the cursor in this case because we don't have a
27073 cursor glyph at hand. */
27074 if ((cursor_row->reversed_p
27075 ? (w->phys_cursor.hpos < 0)
27076 : (w->phys_cursor.hpos >= cursor_row->used[TEXT_AREA])))
27077 goto mark_cursor_off;
27079 /* When the window is hscrolled, cursor hpos can legitimately be out
27080 of bounds, but we draw the cursor at the corresponding window
27081 margin in that case. */
27082 if (!cursor_row->reversed_p && hpos < 0)
27083 hpos = 0;
27084 if (cursor_row->reversed_p && hpos >= cursor_row->used[TEXT_AREA])
27085 hpos = cursor_row->used[TEXT_AREA] - 1;
27087 /* If the cursor is in the mouse face area, redisplay that when
27088 we clear the cursor. */
27089 if (! NILP (hlinfo->mouse_face_window)
27090 && coords_in_mouse_face_p (w, hpos, vpos)
27091 /* Don't redraw the cursor's spot in mouse face if it is at the
27092 end of a line (on a newline). The cursor appears there, but
27093 mouse highlighting does not. */
27094 && cursor_row->used[TEXT_AREA] > hpos && hpos >= 0)
27095 mouse_face_here_p = 1;
27097 /* Maybe clear the display under the cursor. */
27098 if (w->phys_cursor_type == HOLLOW_BOX_CURSOR)
27100 int x, y, left_x;
27101 int header_line_height = WINDOW_HEADER_LINE_HEIGHT (w);
27102 int width;
27104 cursor_glyph = get_phys_cursor_glyph (w);
27105 if (cursor_glyph == NULL)
27106 goto mark_cursor_off;
27108 width = cursor_glyph->pixel_width;
27109 left_x = window_box_left_offset (w, TEXT_AREA);
27110 x = w->phys_cursor.x;
27111 if (x < left_x)
27112 width -= left_x - x;
27113 width = min (width, window_box_width (w, TEXT_AREA) - x);
27114 y = WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height, cursor_row->y));
27115 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, max (x, left_x));
27117 if (width > 0)
27118 FRAME_RIF (f)->clear_frame_area (f, x, y, width, cursor_row->visible_height);
27121 /* Erase the cursor by redrawing the character underneath it. */
27122 if (mouse_face_here_p)
27123 hl = DRAW_MOUSE_FACE;
27124 else
27125 hl = DRAW_NORMAL_TEXT;
27126 draw_phys_cursor_glyph (w, cursor_row, hl);
27128 mark_cursor_off:
27129 w->phys_cursor_on_p = 0;
27130 w->phys_cursor_type = NO_CURSOR;
27134 /* EXPORT:
27135 Display or clear cursor of window W. If ON is zero, clear the
27136 cursor. If it is non-zero, display the cursor. If ON is nonzero,
27137 where to put the cursor is specified by HPOS, VPOS, X and Y. */
27139 void
27140 display_and_set_cursor (struct window *w, bool on,
27141 int hpos, int vpos, int x, int y)
27143 struct frame *f = XFRAME (w->frame);
27144 int new_cursor_type;
27145 int new_cursor_width;
27146 int active_cursor;
27147 struct glyph_row *glyph_row;
27148 struct glyph *glyph;
27150 /* This is pointless on invisible frames, and dangerous on garbaged
27151 windows and frames; in the latter case, the frame or window may
27152 be in the midst of changing its size, and x and y may be off the
27153 window. */
27154 if (! FRAME_VISIBLE_P (f)
27155 || FRAME_GARBAGED_P (f)
27156 || vpos >= w->current_matrix->nrows
27157 || hpos >= w->current_matrix->matrix_w)
27158 return;
27160 /* If cursor is off and we want it off, return quickly. */
27161 if (!on && !w->phys_cursor_on_p)
27162 return;
27164 glyph_row = MATRIX_ROW (w->current_matrix, vpos);
27165 /* If cursor row is not enabled, we don't really know where to
27166 display the cursor. */
27167 if (!glyph_row->enabled_p)
27169 w->phys_cursor_on_p = 0;
27170 return;
27173 glyph = NULL;
27174 if (!glyph_row->exact_window_width_line_p
27175 || (0 <= hpos && hpos < glyph_row->used[TEXT_AREA]))
27176 glyph = glyph_row->glyphs[TEXT_AREA] + hpos;
27178 eassert (input_blocked_p ());
27180 /* Set new_cursor_type to the cursor we want to be displayed. */
27181 new_cursor_type = get_window_cursor_type (w, glyph,
27182 &new_cursor_width, &active_cursor);
27184 /* If cursor is currently being shown and we don't want it to be or
27185 it is in the wrong place, or the cursor type is not what we want,
27186 erase it. */
27187 if (w->phys_cursor_on_p
27188 && (!on
27189 || w->phys_cursor.x != x
27190 || w->phys_cursor.y != y
27191 || new_cursor_type != w->phys_cursor_type
27192 || ((new_cursor_type == BAR_CURSOR || new_cursor_type == HBAR_CURSOR)
27193 && new_cursor_width != w->phys_cursor_width)))
27194 erase_phys_cursor (w);
27196 /* Don't check phys_cursor_on_p here because that flag is only set
27197 to zero in some cases where we know that the cursor has been
27198 completely erased, to avoid the extra work of erasing the cursor
27199 twice. In other words, phys_cursor_on_p can be 1 and the cursor
27200 still not be visible, or it has only been partly erased. */
27201 if (on)
27203 w->phys_cursor_ascent = glyph_row->ascent;
27204 w->phys_cursor_height = glyph_row->height;
27206 /* Set phys_cursor_.* before x_draw_.* is called because some
27207 of them may need the information. */
27208 w->phys_cursor.x = x;
27209 w->phys_cursor.y = glyph_row->y;
27210 w->phys_cursor.hpos = hpos;
27211 w->phys_cursor.vpos = vpos;
27214 FRAME_RIF (f)->draw_window_cursor (w, glyph_row, x, y,
27215 new_cursor_type, new_cursor_width,
27216 on, active_cursor);
27220 /* Switch the display of W's cursor on or off, according to the value
27221 of ON. */
27223 static void
27224 update_window_cursor (struct window *w, bool on)
27226 /* Don't update cursor in windows whose frame is in the process
27227 of being deleted. */
27228 if (w->current_matrix)
27230 int hpos = w->phys_cursor.hpos;
27231 int vpos = w->phys_cursor.vpos;
27232 struct glyph_row *row;
27234 if (vpos >= w->current_matrix->nrows
27235 || hpos >= w->current_matrix->matrix_w)
27236 return;
27238 row = MATRIX_ROW (w->current_matrix, vpos);
27240 /* When the window is hscrolled, cursor hpos can legitimately be
27241 out of bounds, but we draw the cursor at the corresponding
27242 window margin in that case. */
27243 if (!row->reversed_p && hpos < 0)
27244 hpos = 0;
27245 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
27246 hpos = row->used[TEXT_AREA] - 1;
27248 block_input ();
27249 display_and_set_cursor (w, on, hpos, vpos,
27250 w->phys_cursor.x, w->phys_cursor.y);
27251 unblock_input ();
27256 /* Call update_window_cursor with parameter ON_P on all leaf windows
27257 in the window tree rooted at W. */
27259 static void
27260 update_cursor_in_window_tree (struct window *w, bool on_p)
27262 while (w)
27264 if (WINDOWP (w->contents))
27265 update_cursor_in_window_tree (XWINDOW (w->contents), on_p);
27266 else
27267 update_window_cursor (w, on_p);
27269 w = NILP (w->next) ? 0 : XWINDOW (w->next);
27274 /* EXPORT:
27275 Display the cursor on window W, or clear it, according to ON_P.
27276 Don't change the cursor's position. */
27278 void
27279 x_update_cursor (struct frame *f, bool on_p)
27281 update_cursor_in_window_tree (XWINDOW (f->root_window), on_p);
27285 /* EXPORT:
27286 Clear the cursor of window W to background color, and mark the
27287 cursor as not shown. This is used when the text where the cursor
27288 is about to be rewritten. */
27290 void
27291 x_clear_cursor (struct window *w)
27293 if (FRAME_VISIBLE_P (XFRAME (w->frame)) && w->phys_cursor_on_p)
27294 update_window_cursor (w, 0);
27297 #endif /* HAVE_WINDOW_SYSTEM */
27299 /* Implementation of draw_row_with_mouse_face for GUI sessions, GPM,
27300 and MSDOS. */
27301 static void
27302 draw_row_with_mouse_face (struct window *w, int start_x, struct glyph_row *row,
27303 int start_hpos, int end_hpos,
27304 enum draw_glyphs_face draw)
27306 #ifdef HAVE_WINDOW_SYSTEM
27307 if (FRAME_WINDOW_P (XFRAME (w->frame)))
27309 draw_glyphs (w, start_x, row, TEXT_AREA, start_hpos, end_hpos, draw, 0);
27310 return;
27312 #endif
27313 #if defined (HAVE_GPM) || defined (MSDOS) || defined (WINDOWSNT)
27314 tty_draw_row_with_mouse_face (w, row, start_hpos, end_hpos, draw);
27315 #endif
27318 /* Display the active region described by mouse_face_* according to DRAW. */
27320 static void
27321 show_mouse_face (Mouse_HLInfo *hlinfo, enum draw_glyphs_face draw)
27323 struct window *w = XWINDOW (hlinfo->mouse_face_window);
27324 struct frame *f = XFRAME (WINDOW_FRAME (w));
27326 if (/* If window is in the process of being destroyed, don't bother
27327 to do anything. */
27328 w->current_matrix != NULL
27329 /* Don't update mouse highlight if hidden. */
27330 && (draw != DRAW_MOUSE_FACE || !hlinfo->mouse_face_hidden)
27331 /* Recognize when we are called to operate on rows that don't exist
27332 anymore. This can happen when a window is split. */
27333 && hlinfo->mouse_face_end_row < w->current_matrix->nrows)
27335 int phys_cursor_on_p = w->phys_cursor_on_p;
27336 struct glyph_row *row, *first, *last;
27338 first = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_beg_row);
27339 last = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_end_row);
27341 for (row = first; row <= last && row->enabled_p; ++row)
27343 int start_hpos, end_hpos, start_x;
27345 /* For all but the first row, the highlight starts at column 0. */
27346 if (row == first)
27348 /* R2L rows have BEG and END in reversed order, but the
27349 screen drawing geometry is always left to right. So
27350 we need to mirror the beginning and end of the
27351 highlighted area in R2L rows. */
27352 if (!row->reversed_p)
27354 start_hpos = hlinfo->mouse_face_beg_col;
27355 start_x = hlinfo->mouse_face_beg_x;
27357 else if (row == last)
27359 start_hpos = hlinfo->mouse_face_end_col;
27360 start_x = hlinfo->mouse_face_end_x;
27362 else
27364 start_hpos = 0;
27365 start_x = 0;
27368 else if (row->reversed_p && row == last)
27370 start_hpos = hlinfo->mouse_face_end_col;
27371 start_x = hlinfo->mouse_face_end_x;
27373 else
27375 start_hpos = 0;
27376 start_x = 0;
27379 if (row == last)
27381 if (!row->reversed_p)
27382 end_hpos = hlinfo->mouse_face_end_col;
27383 else if (row == first)
27384 end_hpos = hlinfo->mouse_face_beg_col;
27385 else
27387 end_hpos = row->used[TEXT_AREA];
27388 if (draw == DRAW_NORMAL_TEXT)
27389 row->fill_line_p = 1; /* Clear to end of line */
27392 else if (row->reversed_p && row == first)
27393 end_hpos = hlinfo->mouse_face_beg_col;
27394 else
27396 end_hpos = row->used[TEXT_AREA];
27397 if (draw == DRAW_NORMAL_TEXT)
27398 row->fill_line_p = 1; /* Clear to end of line */
27401 if (end_hpos > start_hpos)
27403 draw_row_with_mouse_face (w, start_x, row,
27404 start_hpos, end_hpos, draw);
27406 row->mouse_face_p
27407 = draw == DRAW_MOUSE_FACE || draw == DRAW_IMAGE_RAISED;
27411 #ifdef HAVE_WINDOW_SYSTEM
27412 /* When we've written over the cursor, arrange for it to
27413 be displayed again. */
27414 if (FRAME_WINDOW_P (f)
27415 && phys_cursor_on_p && !w->phys_cursor_on_p)
27417 int hpos = w->phys_cursor.hpos;
27419 /* When the window is hscrolled, cursor hpos can legitimately be
27420 out of bounds, but we draw the cursor at the corresponding
27421 window margin in that case. */
27422 if (!row->reversed_p && hpos < 0)
27423 hpos = 0;
27424 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
27425 hpos = row->used[TEXT_AREA] - 1;
27427 block_input ();
27428 display_and_set_cursor (w, 1, hpos, w->phys_cursor.vpos,
27429 w->phys_cursor.x, w->phys_cursor.y);
27430 unblock_input ();
27432 #endif /* HAVE_WINDOW_SYSTEM */
27435 #ifdef HAVE_WINDOW_SYSTEM
27436 /* Change the mouse cursor. */
27437 if (FRAME_WINDOW_P (f))
27439 #if ! defined (USE_GTK) && ! defined (HAVE_NS)
27440 if (draw == DRAW_NORMAL_TEXT
27441 && !EQ (hlinfo->mouse_face_window, f->tool_bar_window))
27442 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->text_cursor);
27443 else
27444 #endif
27445 if (draw == DRAW_MOUSE_FACE)
27446 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->hand_cursor);
27447 else
27448 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->nontext_cursor);
27450 #endif /* HAVE_WINDOW_SYSTEM */
27453 /* EXPORT:
27454 Clear out the mouse-highlighted active region.
27455 Redraw it un-highlighted first. Value is non-zero if mouse
27456 face was actually drawn unhighlighted. */
27459 clear_mouse_face (Mouse_HLInfo *hlinfo)
27461 int cleared = 0;
27463 if (!hlinfo->mouse_face_hidden && !NILP (hlinfo->mouse_face_window))
27465 show_mouse_face (hlinfo, DRAW_NORMAL_TEXT);
27466 cleared = 1;
27469 hlinfo->mouse_face_beg_row = hlinfo->mouse_face_beg_col = -1;
27470 hlinfo->mouse_face_end_row = hlinfo->mouse_face_end_col = -1;
27471 hlinfo->mouse_face_window = Qnil;
27472 hlinfo->mouse_face_overlay = Qnil;
27473 return cleared;
27476 /* Return true if the coordinates HPOS and VPOS on windows W are
27477 within the mouse face on that window. */
27478 static bool
27479 coords_in_mouse_face_p (struct window *w, int hpos, int vpos)
27481 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (XFRAME (w->frame));
27483 /* Quickly resolve the easy cases. */
27484 if (!(WINDOWP (hlinfo->mouse_face_window)
27485 && XWINDOW (hlinfo->mouse_face_window) == w))
27486 return false;
27487 if (vpos < hlinfo->mouse_face_beg_row
27488 || vpos > hlinfo->mouse_face_end_row)
27489 return false;
27490 if (vpos > hlinfo->mouse_face_beg_row
27491 && vpos < hlinfo->mouse_face_end_row)
27492 return true;
27494 if (!MATRIX_ROW (w->current_matrix, vpos)->reversed_p)
27496 if (hlinfo->mouse_face_beg_row == hlinfo->mouse_face_end_row)
27498 if (hlinfo->mouse_face_beg_col <= hpos && hpos < hlinfo->mouse_face_end_col)
27499 return true;
27501 else if ((vpos == hlinfo->mouse_face_beg_row
27502 && hpos >= hlinfo->mouse_face_beg_col)
27503 || (vpos == hlinfo->mouse_face_end_row
27504 && hpos < hlinfo->mouse_face_end_col))
27505 return true;
27507 else
27509 if (hlinfo->mouse_face_beg_row == hlinfo->mouse_face_end_row)
27511 if (hlinfo->mouse_face_end_col < hpos && hpos <= hlinfo->mouse_face_beg_col)
27512 return true;
27514 else if ((vpos == hlinfo->mouse_face_beg_row
27515 && hpos <= hlinfo->mouse_face_beg_col)
27516 || (vpos == hlinfo->mouse_face_end_row
27517 && hpos > hlinfo->mouse_face_end_col))
27518 return true;
27520 return false;
27524 /* EXPORT:
27525 True if physical cursor of window W is within mouse face. */
27527 bool
27528 cursor_in_mouse_face_p (struct window *w)
27530 int hpos = w->phys_cursor.hpos;
27531 int vpos = w->phys_cursor.vpos;
27532 struct glyph_row *row = MATRIX_ROW (w->current_matrix, vpos);
27534 /* When the window is hscrolled, cursor hpos can legitimately be out
27535 of bounds, but we draw the cursor at the corresponding window
27536 margin in that case. */
27537 if (!row->reversed_p && hpos < 0)
27538 hpos = 0;
27539 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
27540 hpos = row->used[TEXT_AREA] - 1;
27542 return coords_in_mouse_face_p (w, hpos, vpos);
27547 /* Find the glyph rows START_ROW and END_ROW of window W that display
27548 characters between buffer positions START_CHARPOS and END_CHARPOS
27549 (excluding END_CHARPOS). DISP_STRING is a display string that
27550 covers these buffer positions. This is similar to
27551 row_containing_pos, but is more accurate when bidi reordering makes
27552 buffer positions change non-linearly with glyph rows. */
27553 static void
27554 rows_from_pos_range (struct window *w,
27555 ptrdiff_t start_charpos, ptrdiff_t end_charpos,
27556 Lisp_Object disp_string,
27557 struct glyph_row **start, struct glyph_row **end)
27559 struct glyph_row *first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
27560 int last_y = window_text_bottom_y (w);
27561 struct glyph_row *row;
27563 *start = NULL;
27564 *end = NULL;
27566 while (!first->enabled_p
27567 && first < MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w))
27568 first++;
27570 /* Find the START row. */
27571 for (row = first;
27572 row->enabled_p && MATRIX_ROW_BOTTOM_Y (row) <= last_y;
27573 row++)
27575 /* A row can potentially be the START row if the range of the
27576 characters it displays intersects the range
27577 [START_CHARPOS..END_CHARPOS). */
27578 if (! ((start_charpos < MATRIX_ROW_START_CHARPOS (row)
27579 && end_charpos < MATRIX_ROW_START_CHARPOS (row))
27580 /* See the commentary in row_containing_pos, for the
27581 explanation of the complicated way to check whether
27582 some position is beyond the end of the characters
27583 displayed by a row. */
27584 || ((start_charpos > MATRIX_ROW_END_CHARPOS (row)
27585 || (start_charpos == MATRIX_ROW_END_CHARPOS (row)
27586 && !row->ends_at_zv_p
27587 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
27588 && (end_charpos > MATRIX_ROW_END_CHARPOS (row)
27589 || (end_charpos == MATRIX_ROW_END_CHARPOS (row)
27590 && !row->ends_at_zv_p
27591 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))))))
27593 /* Found a candidate row. Now make sure at least one of the
27594 glyphs it displays has a charpos from the range
27595 [START_CHARPOS..END_CHARPOS).
27597 This is not obvious because bidi reordering could make
27598 buffer positions of a row be 1,2,3,102,101,100, and if we
27599 want to highlight characters in [50..60), we don't want
27600 this row, even though [50..60) does intersect [1..103),
27601 the range of character positions given by the row's start
27602 and end positions. */
27603 struct glyph *g = row->glyphs[TEXT_AREA];
27604 struct glyph *e = g + row->used[TEXT_AREA];
27606 while (g < e)
27608 if (((BUFFERP (g->object) || INTEGERP (g->object))
27609 && start_charpos <= g->charpos && g->charpos < end_charpos)
27610 /* A glyph that comes from DISP_STRING is by
27611 definition to be highlighted. */
27612 || EQ (g->object, disp_string))
27613 *start = row;
27614 g++;
27616 if (*start)
27617 break;
27621 /* Find the END row. */
27622 if (!*start
27623 /* If the last row is partially visible, start looking for END
27624 from that row, instead of starting from FIRST. */
27625 && !(row->enabled_p
27626 && row->y < last_y && MATRIX_ROW_BOTTOM_Y (row) > last_y))
27627 row = first;
27628 for ( ; row->enabled_p && MATRIX_ROW_BOTTOM_Y (row) <= last_y; row++)
27630 struct glyph_row *next = row + 1;
27631 ptrdiff_t next_start = MATRIX_ROW_START_CHARPOS (next);
27633 if (!next->enabled_p
27634 || next >= MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w)
27635 /* The first row >= START whose range of displayed characters
27636 does NOT intersect the range [START_CHARPOS..END_CHARPOS]
27637 is the row END + 1. */
27638 || (start_charpos < next_start
27639 && end_charpos < next_start)
27640 || ((start_charpos > MATRIX_ROW_END_CHARPOS (next)
27641 || (start_charpos == MATRIX_ROW_END_CHARPOS (next)
27642 && !next->ends_at_zv_p
27643 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (next)))
27644 && (end_charpos > MATRIX_ROW_END_CHARPOS (next)
27645 || (end_charpos == MATRIX_ROW_END_CHARPOS (next)
27646 && !next->ends_at_zv_p
27647 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (next)))))
27649 *end = row;
27650 break;
27652 else
27654 /* If the next row's edges intersect [START_CHARPOS..END_CHARPOS],
27655 but none of the characters it displays are in the range, it is
27656 also END + 1. */
27657 struct glyph *g = next->glyphs[TEXT_AREA];
27658 struct glyph *s = g;
27659 struct glyph *e = g + next->used[TEXT_AREA];
27661 while (g < e)
27663 if (((BUFFERP (g->object) || INTEGERP (g->object))
27664 && ((start_charpos <= g->charpos && g->charpos < end_charpos)
27665 /* If the buffer position of the first glyph in
27666 the row is equal to END_CHARPOS, it means
27667 the last character to be highlighted is the
27668 newline of ROW, and we must consider NEXT as
27669 END, not END+1. */
27670 || (((!next->reversed_p && g == s)
27671 || (next->reversed_p && g == e - 1))
27672 && (g->charpos == end_charpos
27673 /* Special case for when NEXT is an
27674 empty line at ZV. */
27675 || (g->charpos == -1
27676 && !row->ends_at_zv_p
27677 && next_start == end_charpos)))))
27678 /* A glyph that comes from DISP_STRING is by
27679 definition to be highlighted. */
27680 || EQ (g->object, disp_string))
27681 break;
27682 g++;
27684 if (g == e)
27686 *end = row;
27687 break;
27689 /* The first row that ends at ZV must be the last to be
27690 highlighted. */
27691 else if (next->ends_at_zv_p)
27693 *end = next;
27694 break;
27700 /* This function sets the mouse_face_* elements of HLINFO, assuming
27701 the mouse cursor is on a glyph with buffer charpos MOUSE_CHARPOS in
27702 window WINDOW. START_CHARPOS and END_CHARPOS are buffer positions
27703 for the overlay or run of text properties specifying the mouse
27704 face. BEFORE_STRING and AFTER_STRING, if non-nil, are a
27705 before-string and after-string that must also be highlighted.
27706 DISP_STRING, if non-nil, is a display string that may cover some
27707 or all of the highlighted text. */
27709 static void
27710 mouse_face_from_buffer_pos (Lisp_Object window,
27711 Mouse_HLInfo *hlinfo,
27712 ptrdiff_t mouse_charpos,
27713 ptrdiff_t start_charpos,
27714 ptrdiff_t end_charpos,
27715 Lisp_Object before_string,
27716 Lisp_Object after_string,
27717 Lisp_Object disp_string)
27719 struct window *w = XWINDOW (window);
27720 struct glyph_row *first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
27721 struct glyph_row *r1, *r2;
27722 struct glyph *glyph, *end;
27723 ptrdiff_t ignore, pos;
27724 int x;
27726 eassert (NILP (disp_string) || STRINGP (disp_string));
27727 eassert (NILP (before_string) || STRINGP (before_string));
27728 eassert (NILP (after_string) || STRINGP (after_string));
27730 /* Find the rows corresponding to START_CHARPOS and END_CHARPOS. */
27731 rows_from_pos_range (w, start_charpos, end_charpos, disp_string, &r1, &r2);
27732 if (r1 == NULL)
27733 r1 = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
27734 /* If the before-string or display-string contains newlines,
27735 rows_from_pos_range skips to its last row. Move back. */
27736 if (!NILP (before_string) || !NILP (disp_string))
27738 struct glyph_row *prev;
27739 while ((prev = r1 - 1, prev >= first)
27740 && MATRIX_ROW_END_CHARPOS (prev) == start_charpos
27741 && prev->used[TEXT_AREA] > 0)
27743 struct glyph *beg = prev->glyphs[TEXT_AREA];
27744 glyph = beg + prev->used[TEXT_AREA];
27745 while (--glyph >= beg && INTEGERP (glyph->object));
27746 if (glyph < beg
27747 || !(EQ (glyph->object, before_string)
27748 || EQ (glyph->object, disp_string)))
27749 break;
27750 r1 = prev;
27753 if (r2 == NULL)
27755 r2 = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
27756 hlinfo->mouse_face_past_end = 1;
27758 else if (!NILP (after_string))
27760 /* If the after-string has newlines, advance to its last row. */
27761 struct glyph_row *next;
27762 struct glyph_row *last
27763 = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
27765 for (next = r2 + 1;
27766 next <= last
27767 && next->used[TEXT_AREA] > 0
27768 && EQ (next->glyphs[TEXT_AREA]->object, after_string);
27769 ++next)
27770 r2 = next;
27772 /* The rest of the display engine assumes that mouse_face_beg_row is
27773 either above mouse_face_end_row or identical to it. But with
27774 bidi-reordered continued lines, the row for START_CHARPOS could
27775 be below the row for END_CHARPOS. If so, swap the rows and store
27776 them in correct order. */
27777 if (r1->y > r2->y)
27779 struct glyph_row *tem = r2;
27781 r2 = r1;
27782 r1 = tem;
27785 hlinfo->mouse_face_beg_row = MATRIX_ROW_VPOS (r1, w->current_matrix);
27786 hlinfo->mouse_face_end_row = MATRIX_ROW_VPOS (r2, w->current_matrix);
27788 /* For a bidi-reordered row, the positions of BEFORE_STRING,
27789 AFTER_STRING, DISP_STRING, START_CHARPOS, and END_CHARPOS
27790 could be anywhere in the row and in any order. The strategy
27791 below is to find the leftmost and the rightmost glyph that
27792 belongs to either of these 3 strings, or whose position is
27793 between START_CHARPOS and END_CHARPOS, and highlight all the
27794 glyphs between those two. This may cover more than just the text
27795 between START_CHARPOS and END_CHARPOS if the range of characters
27796 strides the bidi level boundary, e.g. if the beginning is in R2L
27797 text while the end is in L2R text or vice versa. */
27798 if (!r1->reversed_p)
27800 /* This row is in a left to right paragraph. Scan it left to
27801 right. */
27802 glyph = r1->glyphs[TEXT_AREA];
27803 end = glyph + r1->used[TEXT_AREA];
27804 x = r1->x;
27806 /* Skip truncation glyphs at the start of the glyph row. */
27807 if (MATRIX_ROW_DISPLAYS_TEXT_P (r1))
27808 for (; glyph < end
27809 && INTEGERP (glyph->object)
27810 && glyph->charpos < 0;
27811 ++glyph)
27812 x += glyph->pixel_width;
27814 /* Scan the glyph row, looking for BEFORE_STRING, AFTER_STRING,
27815 or DISP_STRING, and the first glyph from buffer whose
27816 position is between START_CHARPOS and END_CHARPOS. */
27817 for (; glyph < end
27818 && !INTEGERP (glyph->object)
27819 && !EQ (glyph->object, disp_string)
27820 && !(BUFFERP (glyph->object)
27821 && (glyph->charpos >= start_charpos
27822 && glyph->charpos < end_charpos));
27823 ++glyph)
27825 /* BEFORE_STRING or AFTER_STRING are only relevant if they
27826 are present at buffer positions between START_CHARPOS and
27827 END_CHARPOS, or if they come from an overlay. */
27828 if (EQ (glyph->object, before_string))
27830 pos = string_buffer_position (before_string,
27831 start_charpos);
27832 /* If pos == 0, it means before_string came from an
27833 overlay, not from a buffer position. */
27834 if (!pos || (pos >= start_charpos && pos < end_charpos))
27835 break;
27837 else if (EQ (glyph->object, after_string))
27839 pos = string_buffer_position (after_string, end_charpos);
27840 if (!pos || (pos >= start_charpos && pos < end_charpos))
27841 break;
27843 x += glyph->pixel_width;
27845 hlinfo->mouse_face_beg_x = x;
27846 hlinfo->mouse_face_beg_col = glyph - r1->glyphs[TEXT_AREA];
27848 else
27850 /* This row is in a right to left paragraph. Scan it right to
27851 left. */
27852 struct glyph *g;
27854 end = r1->glyphs[TEXT_AREA] - 1;
27855 glyph = end + r1->used[TEXT_AREA];
27857 /* Skip truncation glyphs at the start of the glyph row. */
27858 if (MATRIX_ROW_DISPLAYS_TEXT_P (r1))
27859 for (; glyph > end
27860 && INTEGERP (glyph->object)
27861 && glyph->charpos < 0;
27862 --glyph)
27865 /* Scan the glyph row, looking for BEFORE_STRING, AFTER_STRING,
27866 or DISP_STRING, and the first glyph from buffer whose
27867 position is between START_CHARPOS and END_CHARPOS. */
27868 for (; glyph > end
27869 && !INTEGERP (glyph->object)
27870 && !EQ (glyph->object, disp_string)
27871 && !(BUFFERP (glyph->object)
27872 && (glyph->charpos >= start_charpos
27873 && glyph->charpos < end_charpos));
27874 --glyph)
27876 /* BEFORE_STRING or AFTER_STRING are only relevant if they
27877 are present at buffer positions between START_CHARPOS and
27878 END_CHARPOS, or if they come from an overlay. */
27879 if (EQ (glyph->object, before_string))
27881 pos = string_buffer_position (before_string, start_charpos);
27882 /* If pos == 0, it means before_string came from an
27883 overlay, not from a buffer position. */
27884 if (!pos || (pos >= start_charpos && pos < end_charpos))
27885 break;
27887 else if (EQ (glyph->object, after_string))
27889 pos = string_buffer_position (after_string, end_charpos);
27890 if (!pos || (pos >= start_charpos && pos < end_charpos))
27891 break;
27895 glyph++; /* first glyph to the right of the highlighted area */
27896 for (g = r1->glyphs[TEXT_AREA], x = r1->x; g < glyph; g++)
27897 x += g->pixel_width;
27898 hlinfo->mouse_face_beg_x = x;
27899 hlinfo->mouse_face_beg_col = glyph - r1->glyphs[TEXT_AREA];
27902 /* If the highlight ends in a different row, compute GLYPH and END
27903 for the end row. Otherwise, reuse the values computed above for
27904 the row where the highlight begins. */
27905 if (r2 != r1)
27907 if (!r2->reversed_p)
27909 glyph = r2->glyphs[TEXT_AREA];
27910 end = glyph + r2->used[TEXT_AREA];
27911 x = r2->x;
27913 else
27915 end = r2->glyphs[TEXT_AREA] - 1;
27916 glyph = end + r2->used[TEXT_AREA];
27920 if (!r2->reversed_p)
27922 /* Skip truncation and continuation glyphs near the end of the
27923 row, and also blanks and stretch glyphs inserted by
27924 extend_face_to_end_of_line. */
27925 while (end > glyph
27926 && INTEGERP ((end - 1)->object))
27927 --end;
27928 /* Scan the rest of the glyph row from the end, looking for the
27929 first glyph that comes from BEFORE_STRING, AFTER_STRING, or
27930 DISP_STRING, or whose position is between START_CHARPOS
27931 and END_CHARPOS */
27932 for (--end;
27933 end > glyph
27934 && !INTEGERP (end->object)
27935 && !EQ (end->object, disp_string)
27936 && !(BUFFERP (end->object)
27937 && (end->charpos >= start_charpos
27938 && end->charpos < end_charpos));
27939 --end)
27941 /* BEFORE_STRING or AFTER_STRING are only relevant if they
27942 are present at buffer positions between START_CHARPOS and
27943 END_CHARPOS, or if they come from an overlay. */
27944 if (EQ (end->object, before_string))
27946 pos = string_buffer_position (before_string, start_charpos);
27947 if (!pos || (pos >= start_charpos && pos < end_charpos))
27948 break;
27950 else if (EQ (end->object, after_string))
27952 pos = string_buffer_position (after_string, end_charpos);
27953 if (!pos || (pos >= start_charpos && pos < end_charpos))
27954 break;
27957 /* Find the X coordinate of the last glyph to be highlighted. */
27958 for (; glyph <= end; ++glyph)
27959 x += glyph->pixel_width;
27961 hlinfo->mouse_face_end_x = x;
27962 hlinfo->mouse_face_end_col = glyph - r2->glyphs[TEXT_AREA];
27964 else
27966 /* Skip truncation and continuation glyphs near the end of the
27967 row, and also blanks and stretch glyphs inserted by
27968 extend_face_to_end_of_line. */
27969 x = r2->x;
27970 end++;
27971 while (end < glyph
27972 && INTEGERP (end->object))
27974 x += end->pixel_width;
27975 ++end;
27977 /* Scan the rest of the glyph row from the end, looking for the
27978 first glyph that comes from BEFORE_STRING, AFTER_STRING, or
27979 DISP_STRING, or whose position is between START_CHARPOS
27980 and END_CHARPOS */
27981 for ( ;
27982 end < glyph
27983 && !INTEGERP (end->object)
27984 && !EQ (end->object, disp_string)
27985 && !(BUFFERP (end->object)
27986 && (end->charpos >= start_charpos
27987 && end->charpos < end_charpos));
27988 ++end)
27990 /* BEFORE_STRING or AFTER_STRING are only relevant if they
27991 are present at buffer positions between START_CHARPOS and
27992 END_CHARPOS, or if they come from an overlay. */
27993 if (EQ (end->object, before_string))
27995 pos = string_buffer_position (before_string, start_charpos);
27996 if (!pos || (pos >= start_charpos && pos < end_charpos))
27997 break;
27999 else if (EQ (end->object, after_string))
28001 pos = string_buffer_position (after_string, end_charpos);
28002 if (!pos || (pos >= start_charpos && pos < end_charpos))
28003 break;
28005 x += end->pixel_width;
28007 /* If we exited the above loop because we arrived at the last
28008 glyph of the row, and its buffer position is still not in
28009 range, it means the last character in range is the preceding
28010 newline. Bump the end column and x values to get past the
28011 last glyph. */
28012 if (end == glyph
28013 && BUFFERP (end->object)
28014 && (end->charpos < start_charpos
28015 || end->charpos >= end_charpos))
28017 x += end->pixel_width;
28018 ++end;
28020 hlinfo->mouse_face_end_x = x;
28021 hlinfo->mouse_face_end_col = end - r2->glyphs[TEXT_AREA];
28024 hlinfo->mouse_face_window = window;
28025 hlinfo->mouse_face_face_id
28026 = face_at_buffer_position (w, mouse_charpos, &ignore,
28027 mouse_charpos + 1,
28028 !hlinfo->mouse_face_hidden, -1);
28029 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
28032 /* The following function is not used anymore (replaced with
28033 mouse_face_from_string_pos), but I leave it here for the time
28034 being, in case someone would. */
28036 #if 0 /* not used */
28038 /* Find the position of the glyph for position POS in OBJECT in
28039 window W's current matrix, and return in *X, *Y the pixel
28040 coordinates, and return in *HPOS, *VPOS the column/row of the glyph.
28042 RIGHT_P non-zero means return the position of the right edge of the
28043 glyph, RIGHT_P zero means return the left edge position.
28045 If no glyph for POS exists in the matrix, return the position of
28046 the glyph with the next smaller position that is in the matrix, if
28047 RIGHT_P is zero. If RIGHT_P is non-zero, and no glyph for POS
28048 exists in the matrix, return the position of the glyph with the
28049 next larger position in OBJECT.
28051 Value is non-zero if a glyph was found. */
28053 static int
28054 fast_find_string_pos (struct window *w, ptrdiff_t pos, Lisp_Object object,
28055 int *hpos, int *vpos, int *x, int *y, int right_p)
28057 int yb = window_text_bottom_y (w);
28058 struct glyph_row *r;
28059 struct glyph *best_glyph = NULL;
28060 struct glyph_row *best_row = NULL;
28061 int best_x = 0;
28063 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
28064 r->enabled_p && r->y < yb;
28065 ++r)
28067 struct glyph *g = r->glyphs[TEXT_AREA];
28068 struct glyph *e = g + r->used[TEXT_AREA];
28069 int gx;
28071 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
28072 if (EQ (g->object, object))
28074 if (g->charpos == pos)
28076 best_glyph = g;
28077 best_x = gx;
28078 best_row = r;
28079 goto found;
28081 else if (best_glyph == NULL
28082 || ((eabs (g->charpos - pos)
28083 < eabs (best_glyph->charpos - pos))
28084 && (right_p
28085 ? g->charpos < pos
28086 : g->charpos > pos)))
28088 best_glyph = g;
28089 best_x = gx;
28090 best_row = r;
28095 found:
28097 if (best_glyph)
28099 *x = best_x;
28100 *hpos = best_glyph - best_row->glyphs[TEXT_AREA];
28102 if (right_p)
28104 *x += best_glyph->pixel_width;
28105 ++*hpos;
28108 *y = best_row->y;
28109 *vpos = MATRIX_ROW_VPOS (best_row, w->current_matrix);
28112 return best_glyph != NULL;
28114 #endif /* not used */
28116 /* Find the positions of the first and the last glyphs in window W's
28117 current matrix that occlude positions [STARTPOS..ENDPOS) in OBJECT
28118 (assumed to be a string), and return in HLINFO's mouse_face_*
28119 members the pixel and column/row coordinates of those glyphs. */
28121 static void
28122 mouse_face_from_string_pos (struct window *w, Mouse_HLInfo *hlinfo,
28123 Lisp_Object object,
28124 ptrdiff_t startpos, ptrdiff_t endpos)
28126 int yb = window_text_bottom_y (w);
28127 struct glyph_row *r;
28128 struct glyph *g, *e;
28129 int gx;
28130 int found = 0;
28132 /* Find the glyph row with at least one position in the range
28133 [STARTPOS..ENDPOS), and the first glyph in that row whose
28134 position belongs to that range. */
28135 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
28136 r->enabled_p && r->y < yb;
28137 ++r)
28139 if (!r->reversed_p)
28141 g = r->glyphs[TEXT_AREA];
28142 e = g + r->used[TEXT_AREA];
28143 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
28144 if (EQ (g->object, object)
28145 && startpos <= g->charpos && g->charpos < endpos)
28147 hlinfo->mouse_face_beg_row
28148 = MATRIX_ROW_VPOS (r, w->current_matrix);
28149 hlinfo->mouse_face_beg_col = g - r->glyphs[TEXT_AREA];
28150 hlinfo->mouse_face_beg_x = gx;
28151 found = 1;
28152 break;
28155 else
28157 struct glyph *g1;
28159 e = r->glyphs[TEXT_AREA];
28160 g = e + r->used[TEXT_AREA];
28161 for ( ; g > e; --g)
28162 if (EQ ((g-1)->object, object)
28163 && startpos <= (g-1)->charpos && (g-1)->charpos < endpos)
28165 hlinfo->mouse_face_beg_row
28166 = MATRIX_ROW_VPOS (r, w->current_matrix);
28167 hlinfo->mouse_face_beg_col = g - r->glyphs[TEXT_AREA];
28168 for (gx = r->x, g1 = r->glyphs[TEXT_AREA]; g1 < g; ++g1)
28169 gx += g1->pixel_width;
28170 hlinfo->mouse_face_beg_x = gx;
28171 found = 1;
28172 break;
28175 if (found)
28176 break;
28179 if (!found)
28180 return;
28182 /* Starting with the next row, look for the first row which does NOT
28183 include any glyphs whose positions are in the range. */
28184 for (++r; r->enabled_p && r->y < yb; ++r)
28186 g = r->glyphs[TEXT_AREA];
28187 e = g + r->used[TEXT_AREA];
28188 found = 0;
28189 for ( ; g < e; ++g)
28190 if (EQ (g->object, object)
28191 && startpos <= g->charpos && g->charpos < endpos)
28193 found = 1;
28194 break;
28196 if (!found)
28197 break;
28200 /* The highlighted region ends on the previous row. */
28201 r--;
28203 /* Set the end row. */
28204 hlinfo->mouse_face_end_row = MATRIX_ROW_VPOS (r, w->current_matrix);
28206 /* Compute and set the end column and the end column's horizontal
28207 pixel coordinate. */
28208 if (!r->reversed_p)
28210 g = r->glyphs[TEXT_AREA];
28211 e = g + r->used[TEXT_AREA];
28212 for ( ; e > g; --e)
28213 if (EQ ((e-1)->object, object)
28214 && startpos <= (e-1)->charpos && (e-1)->charpos < endpos)
28215 break;
28216 hlinfo->mouse_face_end_col = e - g;
28218 for (gx = r->x; g < e; ++g)
28219 gx += g->pixel_width;
28220 hlinfo->mouse_face_end_x = gx;
28222 else
28224 e = r->glyphs[TEXT_AREA];
28225 g = e + r->used[TEXT_AREA];
28226 for (gx = r->x ; e < g; ++e)
28228 if (EQ (e->object, object)
28229 && startpos <= e->charpos && e->charpos < endpos)
28230 break;
28231 gx += e->pixel_width;
28233 hlinfo->mouse_face_end_col = e - r->glyphs[TEXT_AREA];
28234 hlinfo->mouse_face_end_x = gx;
28238 #ifdef HAVE_WINDOW_SYSTEM
28240 /* See if position X, Y is within a hot-spot of an image. */
28242 static int
28243 on_hot_spot_p (Lisp_Object hot_spot, int x, int y)
28245 if (!CONSP (hot_spot))
28246 return 0;
28248 if (EQ (XCAR (hot_spot), Qrect))
28250 /* CDR is (Top-Left . Bottom-Right) = ((x0 . y0) . (x1 . y1)) */
28251 Lisp_Object rect = XCDR (hot_spot);
28252 Lisp_Object tem;
28253 if (!CONSP (rect))
28254 return 0;
28255 if (!CONSP (XCAR (rect)))
28256 return 0;
28257 if (!CONSP (XCDR (rect)))
28258 return 0;
28259 if (!(tem = XCAR (XCAR (rect)), INTEGERP (tem) && x >= XINT (tem)))
28260 return 0;
28261 if (!(tem = XCDR (XCAR (rect)), INTEGERP (tem) && y >= XINT (tem)))
28262 return 0;
28263 if (!(tem = XCAR (XCDR (rect)), INTEGERP (tem) && x <= XINT (tem)))
28264 return 0;
28265 if (!(tem = XCDR (XCDR (rect)), INTEGERP (tem) && y <= XINT (tem)))
28266 return 0;
28267 return 1;
28269 else if (EQ (XCAR (hot_spot), Qcircle))
28271 /* CDR is (Center . Radius) = ((x0 . y0) . r) */
28272 Lisp_Object circ = XCDR (hot_spot);
28273 Lisp_Object lr, lx0, ly0;
28274 if (CONSP (circ)
28275 && CONSP (XCAR (circ))
28276 && (lr = XCDR (circ), INTEGERP (lr) || FLOATP (lr))
28277 && (lx0 = XCAR (XCAR (circ)), INTEGERP (lx0))
28278 && (ly0 = XCDR (XCAR (circ)), INTEGERP (ly0)))
28280 double r = XFLOATINT (lr);
28281 double dx = XINT (lx0) - x;
28282 double dy = XINT (ly0) - y;
28283 return (dx * dx + dy * dy <= r * r);
28286 else if (EQ (XCAR (hot_spot), Qpoly))
28288 /* CDR is [x0 y0 x1 y1 x2 y2 ...x(n-1) y(n-1)] */
28289 if (VECTORP (XCDR (hot_spot)))
28291 struct Lisp_Vector *v = XVECTOR (XCDR (hot_spot));
28292 Lisp_Object *poly = v->contents;
28293 ptrdiff_t n = v->header.size;
28294 ptrdiff_t i;
28295 int inside = 0;
28296 Lisp_Object lx, ly;
28297 int x0, y0;
28299 /* Need an even number of coordinates, and at least 3 edges. */
28300 if (n < 6 || n & 1)
28301 return 0;
28303 /* Count edge segments intersecting line from (X,Y) to (X,infinity).
28304 If count is odd, we are inside polygon. Pixels on edges
28305 may or may not be included depending on actual geometry of the
28306 polygon. */
28307 if ((lx = poly[n-2], !INTEGERP (lx))
28308 || (ly = poly[n-1], !INTEGERP (lx)))
28309 return 0;
28310 x0 = XINT (lx), y0 = XINT (ly);
28311 for (i = 0; i < n; i += 2)
28313 int x1 = x0, y1 = y0;
28314 if ((lx = poly[i], !INTEGERP (lx))
28315 || (ly = poly[i+1], !INTEGERP (ly)))
28316 return 0;
28317 x0 = XINT (lx), y0 = XINT (ly);
28319 /* Does this segment cross the X line? */
28320 if (x0 >= x)
28322 if (x1 >= x)
28323 continue;
28325 else if (x1 < x)
28326 continue;
28327 if (y > y0 && y > y1)
28328 continue;
28329 if (y < y0 + ((y1 - y0) * (x - x0)) / (x1 - x0))
28330 inside = !inside;
28332 return inside;
28335 return 0;
28338 Lisp_Object
28339 find_hot_spot (Lisp_Object map, int x, int y)
28341 while (CONSP (map))
28343 if (CONSP (XCAR (map))
28344 && on_hot_spot_p (XCAR (XCAR (map)), x, y))
28345 return XCAR (map);
28346 map = XCDR (map);
28349 return Qnil;
28352 DEFUN ("lookup-image-map", Flookup_image_map, Slookup_image_map,
28353 3, 3, 0,
28354 doc: /* Lookup in image map MAP coordinates X and Y.
28355 An image map is an alist where each element has the format (AREA ID PLIST).
28356 An AREA is specified as either a rectangle, a circle, or a polygon:
28357 A rectangle is a cons (rect . ((x0 . y0) . (x1 . y1))) specifying the
28358 pixel coordinates of the upper left and bottom right corners.
28359 A circle is a cons (circle . ((x0 . y0) . r)) specifying the center
28360 and the radius of the circle; r may be a float or integer.
28361 A polygon is a cons (poly . [x0 y0 x1 y1 ...]) where each pair in the
28362 vector describes one corner in the polygon.
28363 Returns the alist element for the first matching AREA in MAP. */)
28364 (Lisp_Object map, Lisp_Object x, Lisp_Object y)
28366 if (NILP (map))
28367 return Qnil;
28369 CHECK_NUMBER (x);
28370 CHECK_NUMBER (y);
28372 return find_hot_spot (map,
28373 clip_to_bounds (INT_MIN, XINT (x), INT_MAX),
28374 clip_to_bounds (INT_MIN, XINT (y), INT_MAX));
28378 /* Display frame CURSOR, optionally using shape defined by POINTER. */
28379 static void
28380 define_frame_cursor1 (struct frame *f, Cursor cursor, Lisp_Object pointer)
28382 /* Do not change cursor shape while dragging mouse. */
28383 if (!NILP (do_mouse_tracking))
28384 return;
28386 if (!NILP (pointer))
28388 if (EQ (pointer, Qarrow))
28389 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
28390 else if (EQ (pointer, Qhand))
28391 cursor = FRAME_X_OUTPUT (f)->hand_cursor;
28392 else if (EQ (pointer, Qtext))
28393 cursor = FRAME_X_OUTPUT (f)->text_cursor;
28394 else if (EQ (pointer, intern ("hdrag")))
28395 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
28396 else if (EQ (pointer, intern ("nhdrag")))
28397 cursor = FRAME_X_OUTPUT (f)->vertical_drag_cursor;
28398 #ifdef HAVE_X_WINDOWS
28399 else if (EQ (pointer, intern ("vdrag")))
28400 cursor = FRAME_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
28401 #endif
28402 else if (EQ (pointer, intern ("hourglass")))
28403 cursor = FRAME_X_OUTPUT (f)->hourglass_cursor;
28404 else if (EQ (pointer, Qmodeline))
28405 cursor = FRAME_X_OUTPUT (f)->modeline_cursor;
28406 else
28407 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
28410 if (cursor != No_Cursor)
28411 FRAME_RIF (f)->define_frame_cursor (f, cursor);
28414 #endif /* HAVE_WINDOW_SYSTEM */
28416 /* Take proper action when mouse has moved to the mode or header line
28417 or marginal area AREA of window W, x-position X and y-position Y.
28418 X is relative to the start of the text display area of W, so the
28419 width of bitmap areas and scroll bars must be subtracted to get a
28420 position relative to the start of the mode line. */
28422 static void
28423 note_mode_line_or_margin_highlight (Lisp_Object window, int x, int y,
28424 enum window_part area)
28426 struct window *w = XWINDOW (window);
28427 struct frame *f = XFRAME (w->frame);
28428 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
28429 #ifdef HAVE_WINDOW_SYSTEM
28430 Display_Info *dpyinfo;
28431 #endif
28432 Cursor cursor = No_Cursor;
28433 Lisp_Object pointer = Qnil;
28434 int dx, dy, width, height;
28435 ptrdiff_t charpos;
28436 Lisp_Object string, object = Qnil;
28437 Lisp_Object pos IF_LINT (= Qnil), help;
28439 Lisp_Object mouse_face;
28440 int original_x_pixel = x;
28441 struct glyph * glyph = NULL, * row_start_glyph = NULL;
28442 struct glyph_row *row IF_LINT (= 0);
28444 if (area == ON_MODE_LINE || area == ON_HEADER_LINE)
28446 int x0;
28447 struct glyph *end;
28449 /* Kludge alert: mode_line_string takes X/Y in pixels, but
28450 returns them in row/column units! */
28451 string = mode_line_string (w, area, &x, &y, &charpos,
28452 &object, &dx, &dy, &width, &height);
28454 row = (area == ON_MODE_LINE
28455 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
28456 : MATRIX_HEADER_LINE_ROW (w->current_matrix));
28458 /* Find the glyph under the mouse pointer. */
28459 if (row->mode_line_p && row->enabled_p)
28461 glyph = row_start_glyph = row->glyphs[TEXT_AREA];
28462 end = glyph + row->used[TEXT_AREA];
28464 for (x0 = original_x_pixel;
28465 glyph < end && x0 >= glyph->pixel_width;
28466 ++glyph)
28467 x0 -= glyph->pixel_width;
28469 if (glyph >= end)
28470 glyph = NULL;
28473 else
28475 x -= WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
28476 /* Kludge alert: marginal_area_string takes X/Y in pixels, but
28477 returns them in row/column units! */
28478 string = marginal_area_string (w, area, &x, &y, &charpos,
28479 &object, &dx, &dy, &width, &height);
28482 help = Qnil;
28484 #ifdef HAVE_WINDOW_SYSTEM
28485 if (IMAGEP (object))
28487 Lisp_Object image_map, hotspot;
28488 if ((image_map = Fplist_get (XCDR (object), QCmap),
28489 !NILP (image_map))
28490 && (hotspot = find_hot_spot (image_map, dx, dy),
28491 CONSP (hotspot))
28492 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
28494 Lisp_Object plist;
28496 /* Could check XCAR (hotspot) to see if we enter/leave this hot-spot.
28497 If so, we could look for mouse-enter, mouse-leave
28498 properties in PLIST (and do something...). */
28499 hotspot = XCDR (hotspot);
28500 if (CONSP (hotspot)
28501 && (plist = XCAR (hotspot), CONSP (plist)))
28503 pointer = Fplist_get (plist, Qpointer);
28504 if (NILP (pointer))
28505 pointer = Qhand;
28506 help = Fplist_get (plist, Qhelp_echo);
28507 if (!NILP (help))
28509 help_echo_string = help;
28510 XSETWINDOW (help_echo_window, w);
28511 help_echo_object = w->contents;
28512 help_echo_pos = charpos;
28516 if (NILP (pointer))
28517 pointer = Fplist_get (XCDR (object), QCpointer);
28519 #endif /* HAVE_WINDOW_SYSTEM */
28521 if (STRINGP (string))
28522 pos = make_number (charpos);
28524 /* Set the help text and mouse pointer. If the mouse is on a part
28525 of the mode line without any text (e.g. past the right edge of
28526 the mode line text), use the default help text and pointer. */
28527 if (STRINGP (string) || area == ON_MODE_LINE)
28529 /* Arrange to display the help by setting the global variables
28530 help_echo_string, help_echo_object, and help_echo_pos. */
28531 if (NILP (help))
28533 if (STRINGP (string))
28534 help = Fget_text_property (pos, Qhelp_echo, string);
28536 if (!NILP (help))
28538 help_echo_string = help;
28539 XSETWINDOW (help_echo_window, w);
28540 help_echo_object = string;
28541 help_echo_pos = charpos;
28543 else if (area == ON_MODE_LINE)
28545 Lisp_Object default_help
28546 = buffer_local_value_1 (Qmode_line_default_help_echo,
28547 w->contents);
28549 if (STRINGP (default_help))
28551 help_echo_string = default_help;
28552 XSETWINDOW (help_echo_window, w);
28553 help_echo_object = Qnil;
28554 help_echo_pos = -1;
28559 #ifdef HAVE_WINDOW_SYSTEM
28560 /* Change the mouse pointer according to what is under it. */
28561 if (FRAME_WINDOW_P (f))
28563 bool draggable = (! WINDOW_BOTTOMMOST_P (w)
28564 || minibuf_level
28565 || NILP (Vresize_mini_windows));
28567 dpyinfo = FRAME_DISPLAY_INFO (f);
28568 if (STRINGP (string))
28570 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
28572 if (NILP (pointer))
28573 pointer = Fget_text_property (pos, Qpointer, string);
28575 /* Change the mouse pointer according to what is under X/Y. */
28576 if (NILP (pointer)
28577 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE)))
28579 Lisp_Object map;
28580 map = Fget_text_property (pos, Qlocal_map, string);
28581 if (!KEYMAPP (map))
28582 map = Fget_text_property (pos, Qkeymap, string);
28583 if (!KEYMAPP (map) && draggable)
28584 cursor = dpyinfo->vertical_scroll_bar_cursor;
28587 else if (draggable)
28588 /* Default mode-line pointer. */
28589 cursor = FRAME_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
28591 #endif
28594 /* Change the mouse face according to what is under X/Y. */
28595 if (STRINGP (string))
28597 mouse_face = Fget_text_property (pos, Qmouse_face, string);
28598 if (!NILP (Vmouse_highlight) && !NILP (mouse_face)
28599 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
28600 && glyph)
28602 Lisp_Object b, e;
28604 struct glyph * tmp_glyph;
28606 int gpos;
28607 int gseq_length;
28608 int total_pixel_width;
28609 ptrdiff_t begpos, endpos, ignore;
28611 int vpos, hpos;
28613 b = Fprevious_single_property_change (make_number (charpos + 1),
28614 Qmouse_face, string, Qnil);
28615 if (NILP (b))
28616 begpos = 0;
28617 else
28618 begpos = XINT (b);
28620 e = Fnext_single_property_change (pos, Qmouse_face, string, Qnil);
28621 if (NILP (e))
28622 endpos = SCHARS (string);
28623 else
28624 endpos = XINT (e);
28626 /* Calculate the glyph position GPOS of GLYPH in the
28627 displayed string, relative to the beginning of the
28628 highlighted part of the string.
28630 Note: GPOS is different from CHARPOS. CHARPOS is the
28631 position of GLYPH in the internal string object. A mode
28632 line string format has structures which are converted to
28633 a flattened string by the Emacs Lisp interpreter. The
28634 internal string is an element of those structures. The
28635 displayed string is the flattened string. */
28636 tmp_glyph = row_start_glyph;
28637 while (tmp_glyph < glyph
28638 && (!(EQ (tmp_glyph->object, glyph->object)
28639 && begpos <= tmp_glyph->charpos
28640 && tmp_glyph->charpos < endpos)))
28641 tmp_glyph++;
28642 gpos = glyph - tmp_glyph;
28644 /* Calculate the length GSEQ_LENGTH of the glyph sequence of
28645 the highlighted part of the displayed string to which
28646 GLYPH belongs. Note: GSEQ_LENGTH is different from
28647 SCHARS (STRING), because the latter returns the length of
28648 the internal string. */
28649 for (tmp_glyph = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
28650 tmp_glyph > glyph
28651 && (!(EQ (tmp_glyph->object, glyph->object)
28652 && begpos <= tmp_glyph->charpos
28653 && tmp_glyph->charpos < endpos));
28654 tmp_glyph--)
28656 gseq_length = gpos + (tmp_glyph - glyph) + 1;
28658 /* Calculate the total pixel width of all the glyphs between
28659 the beginning of the highlighted area and GLYPH. */
28660 total_pixel_width = 0;
28661 for (tmp_glyph = glyph - gpos; tmp_glyph != glyph; tmp_glyph++)
28662 total_pixel_width += tmp_glyph->pixel_width;
28664 /* Pre calculation of re-rendering position. Note: X is in
28665 column units here, after the call to mode_line_string or
28666 marginal_area_string. */
28667 hpos = x - gpos;
28668 vpos = (area == ON_MODE_LINE
28669 ? (w->current_matrix)->nrows - 1
28670 : 0);
28672 /* If GLYPH's position is included in the region that is
28673 already drawn in mouse face, we have nothing to do. */
28674 if ( EQ (window, hlinfo->mouse_face_window)
28675 && (!row->reversed_p
28676 ? (hlinfo->mouse_face_beg_col <= hpos
28677 && hpos < hlinfo->mouse_face_end_col)
28678 /* In R2L rows we swap BEG and END, see below. */
28679 : (hlinfo->mouse_face_end_col <= hpos
28680 && hpos < hlinfo->mouse_face_beg_col))
28681 && hlinfo->mouse_face_beg_row == vpos )
28682 return;
28684 if (clear_mouse_face (hlinfo))
28685 cursor = No_Cursor;
28687 if (!row->reversed_p)
28689 hlinfo->mouse_face_beg_col = hpos;
28690 hlinfo->mouse_face_beg_x = original_x_pixel
28691 - (total_pixel_width + dx);
28692 hlinfo->mouse_face_end_col = hpos + gseq_length;
28693 hlinfo->mouse_face_end_x = 0;
28695 else
28697 /* In R2L rows, show_mouse_face expects BEG and END
28698 coordinates to be swapped. */
28699 hlinfo->mouse_face_end_col = hpos;
28700 hlinfo->mouse_face_end_x = original_x_pixel
28701 - (total_pixel_width + dx);
28702 hlinfo->mouse_face_beg_col = hpos + gseq_length;
28703 hlinfo->mouse_face_beg_x = 0;
28706 hlinfo->mouse_face_beg_row = vpos;
28707 hlinfo->mouse_face_end_row = hlinfo->mouse_face_beg_row;
28708 hlinfo->mouse_face_past_end = 0;
28709 hlinfo->mouse_face_window = window;
28711 hlinfo->mouse_face_face_id = face_at_string_position (w, string,
28712 charpos,
28713 0, &ignore,
28714 glyph->face_id,
28716 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
28718 if (NILP (pointer))
28719 pointer = Qhand;
28721 else if ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
28722 clear_mouse_face (hlinfo);
28724 #ifdef HAVE_WINDOW_SYSTEM
28725 if (FRAME_WINDOW_P (f))
28726 define_frame_cursor1 (f, cursor, pointer);
28727 #endif
28731 /* EXPORT:
28732 Take proper action when the mouse has moved to position X, Y on
28733 frame F with regards to highlighting portions of display that have
28734 mouse-face properties. Also de-highlight portions of display where
28735 the mouse was before, set the mouse pointer shape as appropriate
28736 for the mouse coordinates, and activate help echo (tooltips).
28737 X and Y can be negative or out of range. */
28739 void
28740 note_mouse_highlight (struct frame *f, int x, int y)
28742 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
28743 enum window_part part = ON_NOTHING;
28744 Lisp_Object window;
28745 struct window *w;
28746 Cursor cursor = No_Cursor;
28747 Lisp_Object pointer = Qnil; /* Takes precedence over cursor! */
28748 struct buffer *b;
28750 /* When a menu is active, don't highlight because this looks odd. */
28751 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS) || defined (MSDOS)
28752 if (popup_activated ())
28753 return;
28754 #endif
28756 if (!f->glyphs_initialized_p
28757 || f->pointer_invisible)
28758 return;
28760 hlinfo->mouse_face_mouse_x = x;
28761 hlinfo->mouse_face_mouse_y = y;
28762 hlinfo->mouse_face_mouse_frame = f;
28764 if (hlinfo->mouse_face_defer)
28765 return;
28767 /* Which window is that in? */
28768 window = window_from_coordinates (f, x, y, &part, 1);
28770 /* If displaying active text in another window, clear that. */
28771 if (! EQ (window, hlinfo->mouse_face_window)
28772 /* Also clear if we move out of text area in same window. */
28773 || (!NILP (hlinfo->mouse_face_window)
28774 && !NILP (window)
28775 && part != ON_TEXT
28776 && part != ON_MODE_LINE
28777 && part != ON_HEADER_LINE))
28778 clear_mouse_face (hlinfo);
28780 /* Not on a window -> return. */
28781 if (!WINDOWP (window))
28782 return;
28784 /* Reset help_echo_string. It will get recomputed below. */
28785 help_echo_string = Qnil;
28787 /* Convert to window-relative pixel coordinates. */
28788 w = XWINDOW (window);
28789 frame_to_window_pixel_xy (w, &x, &y);
28791 #if defined (HAVE_WINDOW_SYSTEM) && ! defined (USE_GTK) && ! defined (HAVE_NS)
28792 /* Handle tool-bar window differently since it doesn't display a
28793 buffer. */
28794 if (EQ (window, f->tool_bar_window))
28796 note_tool_bar_highlight (f, x, y);
28797 return;
28799 #endif
28801 /* Mouse is on the mode, header line or margin? */
28802 if (part == ON_MODE_LINE || part == ON_HEADER_LINE
28803 || part == ON_LEFT_MARGIN || part == ON_RIGHT_MARGIN)
28805 note_mode_line_or_margin_highlight (window, x, y, part);
28807 #ifdef HAVE_WINDOW_SYSTEM
28808 if (part == ON_LEFT_MARGIN || part == ON_RIGHT_MARGIN)
28810 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
28811 /* Show non-text cursor (Bug#16647). */
28812 goto set_cursor;
28814 else
28815 #endif
28816 return;
28819 #ifdef HAVE_WINDOW_SYSTEM
28820 if (part == ON_VERTICAL_BORDER)
28822 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
28823 help_echo_string = build_string ("drag-mouse-1: resize");
28825 else if (part == ON_RIGHT_DIVIDER)
28827 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
28828 help_echo_string = build_string ("drag-mouse-1: resize");
28830 else if (part == ON_BOTTOM_DIVIDER)
28831 if (! WINDOW_BOTTOMMOST_P (w)
28832 || minibuf_level
28833 || NILP (Vresize_mini_windows))
28835 cursor = FRAME_X_OUTPUT (f)->vertical_drag_cursor;
28836 help_echo_string = build_string ("drag-mouse-1: resize");
28838 else
28839 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
28840 else if (part == ON_LEFT_FRINGE || part == ON_RIGHT_FRINGE
28841 || part == ON_SCROLL_BAR)
28842 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
28843 else
28844 cursor = FRAME_X_OUTPUT (f)->text_cursor;
28845 #endif
28847 /* Are we in a window whose display is up to date?
28848 And verify the buffer's text has not changed. */
28849 b = XBUFFER (w->contents);
28850 if (part == ON_TEXT && w->window_end_valid && !window_outdated (w))
28852 int hpos, vpos, dx, dy, area = LAST_AREA;
28853 ptrdiff_t pos;
28854 struct glyph *glyph;
28855 Lisp_Object object;
28856 Lisp_Object mouse_face = Qnil, position;
28857 Lisp_Object *overlay_vec = NULL;
28858 ptrdiff_t i, noverlays;
28859 struct buffer *obuf;
28860 ptrdiff_t obegv, ozv;
28861 int same_region;
28863 /* Find the glyph under X/Y. */
28864 glyph = x_y_to_hpos_vpos (w, x, y, &hpos, &vpos, &dx, &dy, &area);
28866 #ifdef HAVE_WINDOW_SYSTEM
28867 /* Look for :pointer property on image. */
28868 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
28870 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
28871 if (img != NULL && IMAGEP (img->spec))
28873 Lisp_Object image_map, hotspot;
28874 if ((image_map = Fplist_get (XCDR (img->spec), QCmap),
28875 !NILP (image_map))
28876 && (hotspot = find_hot_spot (image_map,
28877 glyph->slice.img.x + dx,
28878 glyph->slice.img.y + dy),
28879 CONSP (hotspot))
28880 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
28882 Lisp_Object plist;
28884 /* Could check XCAR (hotspot) to see if we enter/leave
28885 this hot-spot.
28886 If so, we could look for mouse-enter, mouse-leave
28887 properties in PLIST (and do something...). */
28888 hotspot = XCDR (hotspot);
28889 if (CONSP (hotspot)
28890 && (plist = XCAR (hotspot), CONSP (plist)))
28892 pointer = Fplist_get (plist, Qpointer);
28893 if (NILP (pointer))
28894 pointer = Qhand;
28895 help_echo_string = Fplist_get (plist, Qhelp_echo);
28896 if (!NILP (help_echo_string))
28898 help_echo_window = window;
28899 help_echo_object = glyph->object;
28900 help_echo_pos = glyph->charpos;
28904 if (NILP (pointer))
28905 pointer = Fplist_get (XCDR (img->spec), QCpointer);
28908 #endif /* HAVE_WINDOW_SYSTEM */
28910 /* Clear mouse face if X/Y not over text. */
28911 if (glyph == NULL
28912 || area != TEXT_AREA
28913 || !MATRIX_ROW_DISPLAYS_TEXT_P (MATRIX_ROW (w->current_matrix, vpos))
28914 /* Glyph's OBJECT is an integer for glyphs inserted by the
28915 display engine for its internal purposes, like truncation
28916 and continuation glyphs and blanks beyond the end of
28917 line's text on text terminals. If we are over such a
28918 glyph, we are not over any text. */
28919 || INTEGERP (glyph->object)
28920 /* R2L rows have a stretch glyph at their front, which
28921 stands for no text, whereas L2R rows have no glyphs at
28922 all beyond the end of text. Treat such stretch glyphs
28923 like we do with NULL glyphs in L2R rows. */
28924 || (MATRIX_ROW (w->current_matrix, vpos)->reversed_p
28925 && glyph == MATRIX_ROW_GLYPH_START (w->current_matrix, vpos)
28926 && glyph->type == STRETCH_GLYPH
28927 && glyph->avoid_cursor_p))
28929 if (clear_mouse_face (hlinfo))
28930 cursor = No_Cursor;
28931 #ifdef HAVE_WINDOW_SYSTEM
28932 if (FRAME_WINDOW_P (f) && NILP (pointer))
28934 if (area != TEXT_AREA)
28935 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
28936 else
28937 pointer = Vvoid_text_area_pointer;
28939 #endif
28940 goto set_cursor;
28943 pos = glyph->charpos;
28944 object = glyph->object;
28945 if (!STRINGP (object) && !BUFFERP (object))
28946 goto set_cursor;
28948 /* If we get an out-of-range value, return now; avoid an error. */
28949 if (BUFFERP (object) && pos > BUF_Z (b))
28950 goto set_cursor;
28952 /* Make the window's buffer temporarily current for
28953 overlays_at and compute_char_face. */
28954 obuf = current_buffer;
28955 current_buffer = b;
28956 obegv = BEGV;
28957 ozv = ZV;
28958 BEGV = BEG;
28959 ZV = Z;
28961 /* Is this char mouse-active or does it have help-echo? */
28962 position = make_number (pos);
28964 if (BUFFERP (object))
28966 /* Put all the overlays we want in a vector in overlay_vec. */
28967 GET_OVERLAYS_AT (pos, overlay_vec, noverlays, NULL, 0);
28968 /* Sort overlays into increasing priority order. */
28969 noverlays = sort_overlays (overlay_vec, noverlays, w);
28971 else
28972 noverlays = 0;
28974 if (NILP (Vmouse_highlight))
28976 clear_mouse_face (hlinfo);
28977 goto check_help_echo;
28980 same_region = coords_in_mouse_face_p (w, hpos, vpos);
28982 if (same_region)
28983 cursor = No_Cursor;
28985 /* Check mouse-face highlighting. */
28986 if (! same_region
28987 /* If there exists an overlay with mouse-face overlapping
28988 the one we are currently highlighting, we have to
28989 check if we enter the overlapping overlay, and then
28990 highlight only that. */
28991 || (OVERLAYP (hlinfo->mouse_face_overlay)
28992 && mouse_face_overlay_overlaps (hlinfo->mouse_face_overlay)))
28994 /* Find the highest priority overlay with a mouse-face. */
28995 Lisp_Object overlay = Qnil;
28996 for (i = noverlays - 1; i >= 0 && NILP (overlay); --i)
28998 mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
28999 if (!NILP (mouse_face))
29000 overlay = overlay_vec[i];
29003 /* If we're highlighting the same overlay as before, there's
29004 no need to do that again. */
29005 if (!NILP (overlay) && EQ (overlay, hlinfo->mouse_face_overlay))
29006 goto check_help_echo;
29007 hlinfo->mouse_face_overlay = overlay;
29009 /* Clear the display of the old active region, if any. */
29010 if (clear_mouse_face (hlinfo))
29011 cursor = No_Cursor;
29013 /* If no overlay applies, get a text property. */
29014 if (NILP (overlay))
29015 mouse_face = Fget_text_property (position, Qmouse_face, object);
29017 /* Next, compute the bounds of the mouse highlighting and
29018 display it. */
29019 if (!NILP (mouse_face) && STRINGP (object))
29021 /* The mouse-highlighting comes from a display string
29022 with a mouse-face. */
29023 Lisp_Object s, e;
29024 ptrdiff_t ignore;
29026 s = Fprevious_single_property_change
29027 (make_number (pos + 1), Qmouse_face, object, Qnil);
29028 e = Fnext_single_property_change
29029 (position, Qmouse_face, object, Qnil);
29030 if (NILP (s))
29031 s = make_number (0);
29032 if (NILP (e))
29033 e = make_number (SCHARS (object));
29034 mouse_face_from_string_pos (w, hlinfo, object,
29035 XINT (s), XINT (e));
29036 hlinfo->mouse_face_past_end = 0;
29037 hlinfo->mouse_face_window = window;
29038 hlinfo->mouse_face_face_id
29039 = face_at_string_position (w, object, pos, 0, &ignore,
29040 glyph->face_id, 1);
29041 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
29042 cursor = No_Cursor;
29044 else
29046 /* The mouse-highlighting, if any, comes from an overlay
29047 or text property in the buffer. */
29048 Lisp_Object buffer IF_LINT (= Qnil);
29049 Lisp_Object disp_string IF_LINT (= Qnil);
29051 if (STRINGP (object))
29053 /* If we are on a display string with no mouse-face,
29054 check if the text under it has one. */
29055 struct glyph_row *r = MATRIX_ROW (w->current_matrix, vpos);
29056 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
29057 pos = string_buffer_position (object, start);
29058 if (pos > 0)
29060 mouse_face = get_char_property_and_overlay
29061 (make_number (pos), Qmouse_face, w->contents, &overlay);
29062 buffer = w->contents;
29063 disp_string = object;
29066 else
29068 buffer = object;
29069 disp_string = Qnil;
29072 if (!NILP (mouse_face))
29074 Lisp_Object before, after;
29075 Lisp_Object before_string, after_string;
29076 /* To correctly find the limits of mouse highlight
29077 in a bidi-reordered buffer, we must not use the
29078 optimization of limiting the search in
29079 previous-single-property-change and
29080 next-single-property-change, because
29081 rows_from_pos_range needs the real start and end
29082 positions to DTRT in this case. That's because
29083 the first row visible in a window does not
29084 necessarily display the character whose position
29085 is the smallest. */
29086 Lisp_Object lim1
29087 = NILP (BVAR (XBUFFER (buffer), bidi_display_reordering))
29088 ? Fmarker_position (w->start)
29089 : Qnil;
29090 Lisp_Object lim2
29091 = NILP (BVAR (XBUFFER (buffer), bidi_display_reordering))
29092 ? make_number (BUF_Z (XBUFFER (buffer))
29093 - w->window_end_pos)
29094 : Qnil;
29096 if (NILP (overlay))
29098 /* Handle the text property case. */
29099 before = Fprevious_single_property_change
29100 (make_number (pos + 1), Qmouse_face, buffer, lim1);
29101 after = Fnext_single_property_change
29102 (make_number (pos), Qmouse_face, buffer, lim2);
29103 before_string = after_string = Qnil;
29105 else
29107 /* Handle the overlay case. */
29108 before = Foverlay_start (overlay);
29109 after = Foverlay_end (overlay);
29110 before_string = Foverlay_get (overlay, Qbefore_string);
29111 after_string = Foverlay_get (overlay, Qafter_string);
29113 if (!STRINGP (before_string)) before_string = Qnil;
29114 if (!STRINGP (after_string)) after_string = Qnil;
29117 mouse_face_from_buffer_pos (window, hlinfo, pos,
29118 NILP (before)
29120 : XFASTINT (before),
29121 NILP (after)
29122 ? BUF_Z (XBUFFER (buffer))
29123 : XFASTINT (after),
29124 before_string, after_string,
29125 disp_string);
29126 cursor = No_Cursor;
29131 check_help_echo:
29133 /* Look for a `help-echo' property. */
29134 if (NILP (help_echo_string)) {
29135 Lisp_Object help, overlay;
29137 /* Check overlays first. */
29138 help = overlay = Qnil;
29139 for (i = noverlays - 1; i >= 0 && NILP (help); --i)
29141 overlay = overlay_vec[i];
29142 help = Foverlay_get (overlay, Qhelp_echo);
29145 if (!NILP (help))
29147 help_echo_string = help;
29148 help_echo_window = window;
29149 help_echo_object = overlay;
29150 help_echo_pos = pos;
29152 else
29154 Lisp_Object obj = glyph->object;
29155 ptrdiff_t charpos = glyph->charpos;
29157 /* Try text properties. */
29158 if (STRINGP (obj)
29159 && charpos >= 0
29160 && charpos < SCHARS (obj))
29162 help = Fget_text_property (make_number (charpos),
29163 Qhelp_echo, obj);
29164 if (NILP (help))
29166 /* If the string itself doesn't specify a help-echo,
29167 see if the buffer text ``under'' it does. */
29168 struct glyph_row *r
29169 = MATRIX_ROW (w->current_matrix, vpos);
29170 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
29171 ptrdiff_t p = string_buffer_position (obj, start);
29172 if (p > 0)
29174 help = Fget_char_property (make_number (p),
29175 Qhelp_echo, w->contents);
29176 if (!NILP (help))
29178 charpos = p;
29179 obj = w->contents;
29184 else if (BUFFERP (obj)
29185 && charpos >= BEGV
29186 && charpos < ZV)
29187 help = Fget_text_property (make_number (charpos), Qhelp_echo,
29188 obj);
29190 if (!NILP (help))
29192 help_echo_string = help;
29193 help_echo_window = window;
29194 help_echo_object = obj;
29195 help_echo_pos = charpos;
29200 #ifdef HAVE_WINDOW_SYSTEM
29201 /* Look for a `pointer' property. */
29202 if (FRAME_WINDOW_P (f) && NILP (pointer))
29204 /* Check overlays first. */
29205 for (i = noverlays - 1; i >= 0 && NILP (pointer); --i)
29206 pointer = Foverlay_get (overlay_vec[i], Qpointer);
29208 if (NILP (pointer))
29210 Lisp_Object obj = glyph->object;
29211 ptrdiff_t charpos = glyph->charpos;
29213 /* Try text properties. */
29214 if (STRINGP (obj)
29215 && charpos >= 0
29216 && charpos < SCHARS (obj))
29218 pointer = Fget_text_property (make_number (charpos),
29219 Qpointer, obj);
29220 if (NILP (pointer))
29222 /* If the string itself doesn't specify a pointer,
29223 see if the buffer text ``under'' it does. */
29224 struct glyph_row *r
29225 = MATRIX_ROW (w->current_matrix, vpos);
29226 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
29227 ptrdiff_t p = string_buffer_position (obj, start);
29228 if (p > 0)
29229 pointer = Fget_char_property (make_number (p),
29230 Qpointer, w->contents);
29233 else if (BUFFERP (obj)
29234 && charpos >= BEGV
29235 && charpos < ZV)
29236 pointer = Fget_text_property (make_number (charpos),
29237 Qpointer, obj);
29240 #endif /* HAVE_WINDOW_SYSTEM */
29242 BEGV = obegv;
29243 ZV = ozv;
29244 current_buffer = obuf;
29247 set_cursor:
29249 #ifdef HAVE_WINDOW_SYSTEM
29250 if (FRAME_WINDOW_P (f))
29251 define_frame_cursor1 (f, cursor, pointer);
29252 #else
29253 /* This is here to prevent a compiler error, about "label at end of
29254 compound statement". */
29255 return;
29256 #endif
29260 /* EXPORT for RIF:
29261 Clear any mouse-face on window W. This function is part of the
29262 redisplay interface, and is called from try_window_id and similar
29263 functions to ensure the mouse-highlight is off. */
29265 void
29266 x_clear_window_mouse_face (struct window *w)
29268 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (XFRAME (w->frame));
29269 Lisp_Object window;
29271 block_input ();
29272 XSETWINDOW (window, w);
29273 if (EQ (window, hlinfo->mouse_face_window))
29274 clear_mouse_face (hlinfo);
29275 unblock_input ();
29279 /* EXPORT:
29280 Just discard the mouse face information for frame F, if any.
29281 This is used when the size of F is changed. */
29283 void
29284 cancel_mouse_face (struct frame *f)
29286 Lisp_Object window;
29287 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
29289 window = hlinfo->mouse_face_window;
29290 if (! NILP (window) && XFRAME (XWINDOW (window)->frame) == f)
29291 reset_mouse_highlight (hlinfo);
29296 /***********************************************************************
29297 Exposure Events
29298 ***********************************************************************/
29300 #ifdef HAVE_WINDOW_SYSTEM
29302 /* Redraw the part of glyph row area AREA of glyph row ROW on window W
29303 which intersects rectangle R. R is in window-relative coordinates. */
29305 static void
29306 expose_area (struct window *w, struct glyph_row *row, XRectangle *r,
29307 enum glyph_row_area area)
29309 struct glyph *first = row->glyphs[area];
29310 struct glyph *end = row->glyphs[area] + row->used[area];
29311 struct glyph *last;
29312 int first_x, start_x, x;
29314 if (area == TEXT_AREA && row->fill_line_p)
29315 /* If row extends face to end of line write the whole line. */
29316 draw_glyphs (w, 0, row, area,
29317 0, row->used[area],
29318 DRAW_NORMAL_TEXT, 0);
29319 else
29321 /* Set START_X to the window-relative start position for drawing glyphs of
29322 AREA. The first glyph of the text area can be partially visible.
29323 The first glyphs of other areas cannot. */
29324 start_x = window_box_left_offset (w, area);
29325 x = start_x;
29326 if (area == TEXT_AREA)
29327 x += row->x;
29329 /* Find the first glyph that must be redrawn. */
29330 while (first < end
29331 && x + first->pixel_width < r->x)
29333 x += first->pixel_width;
29334 ++first;
29337 /* Find the last one. */
29338 last = first;
29339 first_x = x;
29340 while (last < end
29341 && x < r->x + r->width)
29343 x += last->pixel_width;
29344 ++last;
29347 /* Repaint. */
29348 if (last > first)
29349 draw_glyphs (w, first_x - start_x, row, area,
29350 first - row->glyphs[area], last - row->glyphs[area],
29351 DRAW_NORMAL_TEXT, 0);
29356 /* Redraw the parts of the glyph row ROW on window W intersecting
29357 rectangle R. R is in window-relative coordinates. Value is
29358 non-zero if mouse-face was overwritten. */
29360 static int
29361 expose_line (struct window *w, struct glyph_row *row, XRectangle *r)
29363 eassert (row->enabled_p);
29365 if (row->mode_line_p || w->pseudo_window_p)
29366 draw_glyphs (w, 0, row, TEXT_AREA,
29367 0, row->used[TEXT_AREA],
29368 DRAW_NORMAL_TEXT, 0);
29369 else
29371 if (row->used[LEFT_MARGIN_AREA])
29372 expose_area (w, row, r, LEFT_MARGIN_AREA);
29373 if (row->used[TEXT_AREA])
29374 expose_area (w, row, r, TEXT_AREA);
29375 if (row->used[RIGHT_MARGIN_AREA])
29376 expose_area (w, row, r, RIGHT_MARGIN_AREA);
29377 draw_row_fringe_bitmaps (w, row);
29380 return row->mouse_face_p;
29384 /* Redraw those parts of glyphs rows during expose event handling that
29385 overlap other rows. Redrawing of an exposed line writes over parts
29386 of lines overlapping that exposed line; this function fixes that.
29388 W is the window being exposed. FIRST_OVERLAPPING_ROW is the first
29389 row in W's current matrix that is exposed and overlaps other rows.
29390 LAST_OVERLAPPING_ROW is the last such row. */
29392 static void
29393 expose_overlaps (struct window *w,
29394 struct glyph_row *first_overlapping_row,
29395 struct glyph_row *last_overlapping_row,
29396 XRectangle *r)
29398 struct glyph_row *row;
29400 for (row = first_overlapping_row; row <= last_overlapping_row; ++row)
29401 if (row->overlapping_p)
29403 eassert (row->enabled_p && !row->mode_line_p);
29405 row->clip = r;
29406 if (row->used[LEFT_MARGIN_AREA])
29407 x_fix_overlapping_area (w, row, LEFT_MARGIN_AREA, OVERLAPS_BOTH);
29409 if (row->used[TEXT_AREA])
29410 x_fix_overlapping_area (w, row, TEXT_AREA, OVERLAPS_BOTH);
29412 if (row->used[RIGHT_MARGIN_AREA])
29413 x_fix_overlapping_area (w, row, RIGHT_MARGIN_AREA, OVERLAPS_BOTH);
29414 row->clip = NULL;
29419 /* Return non-zero if W's cursor intersects rectangle R. */
29421 static int
29422 phys_cursor_in_rect_p (struct window *w, XRectangle *r)
29424 XRectangle cr, result;
29425 struct glyph *cursor_glyph;
29426 struct glyph_row *row;
29428 if (w->phys_cursor.vpos >= 0
29429 && w->phys_cursor.vpos < w->current_matrix->nrows
29430 && (row = MATRIX_ROW (w->current_matrix, w->phys_cursor.vpos),
29431 row->enabled_p)
29432 && row->cursor_in_fringe_p)
29434 /* Cursor is in the fringe. */
29435 cr.x = window_box_right_offset (w,
29436 (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
29437 ? RIGHT_MARGIN_AREA
29438 : TEXT_AREA));
29439 cr.y = row->y;
29440 cr.width = WINDOW_RIGHT_FRINGE_WIDTH (w);
29441 cr.height = row->height;
29442 return x_intersect_rectangles (&cr, r, &result);
29445 cursor_glyph = get_phys_cursor_glyph (w);
29446 if (cursor_glyph)
29448 /* r is relative to W's box, but w->phys_cursor.x is relative
29449 to left edge of W's TEXT area. Adjust it. */
29450 cr.x = window_box_left_offset (w, TEXT_AREA) + w->phys_cursor.x;
29451 cr.y = w->phys_cursor.y;
29452 cr.width = cursor_glyph->pixel_width;
29453 cr.height = w->phys_cursor_height;
29454 /* ++KFS: W32 version used W32-specific IntersectRect here, but
29455 I assume the effect is the same -- and this is portable. */
29456 return x_intersect_rectangles (&cr, r, &result);
29458 /* If we don't understand the format, pretend we're not in the hot-spot. */
29459 return 0;
29463 /* EXPORT:
29464 Draw a vertical window border to the right of window W if W doesn't
29465 have vertical scroll bars. */
29467 void
29468 x_draw_vertical_border (struct window *w)
29470 struct frame *f = XFRAME (WINDOW_FRAME (w));
29472 /* We could do better, if we knew what type of scroll-bar the adjacent
29473 windows (on either side) have... But we don't :-(
29474 However, I think this works ok. ++KFS 2003-04-25 */
29476 /* Redraw borders between horizontally adjacent windows. Don't
29477 do it for frames with vertical scroll bars because either the
29478 right scroll bar of a window, or the left scroll bar of its
29479 neighbor will suffice as a border. */
29480 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f) || FRAME_RIGHT_DIVIDER_WIDTH (f))
29481 return;
29483 /* Note: It is necessary to redraw both the left and the right
29484 borders, for when only this single window W is being
29485 redisplayed. */
29486 if (!WINDOW_RIGHTMOST_P (w)
29487 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w))
29489 int x0, x1, y0, y1;
29491 window_box_edges (w, &x0, &y0, &x1, &y1);
29492 y1 -= 1;
29494 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
29495 x1 -= 1;
29497 FRAME_RIF (f)->draw_vertical_window_border (w, x1, y0, y1);
29500 if (!WINDOW_LEFTMOST_P (w)
29501 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w))
29503 int x0, x1, y0, y1;
29505 window_box_edges (w, &x0, &y0, &x1, &y1);
29506 y1 -= 1;
29508 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
29509 x0 -= 1;
29511 FRAME_RIF (f)->draw_vertical_window_border (w, x0, y0, y1);
29516 /* Draw window dividers for window W. */
29518 void
29519 x_draw_right_divider (struct window *w)
29521 struct frame *f = WINDOW_XFRAME (w);
29523 if (w->mini || w->pseudo_window_p)
29524 return;
29525 else if (WINDOW_RIGHT_DIVIDER_WIDTH (w))
29527 int x0 = WINDOW_RIGHT_EDGE_X (w) - WINDOW_RIGHT_DIVIDER_WIDTH (w);
29528 int x1 = WINDOW_RIGHT_EDGE_X (w);
29529 int y0 = WINDOW_TOP_EDGE_Y (w);
29530 /* The bottom divider prevails. */
29531 int y1 = WINDOW_BOTTOM_EDGE_Y (w) - WINDOW_BOTTOM_DIVIDER_WIDTH (w);
29533 FRAME_RIF (f)->draw_window_divider (w, x0, x1, y0, y1);
29537 static void
29538 x_draw_bottom_divider (struct window *w)
29540 struct frame *f = XFRAME (WINDOW_FRAME (w));
29542 if (w->mini || w->pseudo_window_p)
29543 return;
29544 else if (WINDOW_BOTTOM_DIVIDER_WIDTH (w))
29546 int x0 = WINDOW_LEFT_EDGE_X (w);
29547 int x1 = WINDOW_RIGHT_EDGE_X (w);
29548 int y0 = WINDOW_BOTTOM_EDGE_Y (w) - WINDOW_BOTTOM_DIVIDER_WIDTH (w);
29549 int y1 = WINDOW_BOTTOM_EDGE_Y (w);
29551 FRAME_RIF (f)->draw_window_divider (w, x0, x1, y0, y1);
29555 /* Redraw the part of window W intersection rectangle FR. Pixel
29556 coordinates in FR are frame-relative. Call this function with
29557 input blocked. Value is non-zero if the exposure overwrites
29558 mouse-face. */
29560 static int
29561 expose_window (struct window *w, XRectangle *fr)
29563 struct frame *f = XFRAME (w->frame);
29564 XRectangle wr, r;
29565 int mouse_face_overwritten_p = 0;
29567 /* If window is not yet fully initialized, do nothing. This can
29568 happen when toolkit scroll bars are used and a window is split.
29569 Reconfiguring the scroll bar will generate an expose for a newly
29570 created window. */
29571 if (w->current_matrix == NULL)
29572 return 0;
29574 /* When we're currently updating the window, display and current
29575 matrix usually don't agree. Arrange for a thorough display
29576 later. */
29577 if (w->must_be_updated_p)
29579 SET_FRAME_GARBAGED (f);
29580 return 0;
29583 /* Frame-relative pixel rectangle of W. */
29584 wr.x = WINDOW_LEFT_EDGE_X (w);
29585 wr.y = WINDOW_TOP_EDGE_Y (w);
29586 wr.width = WINDOW_PIXEL_WIDTH (w);
29587 wr.height = WINDOW_PIXEL_HEIGHT (w);
29589 if (x_intersect_rectangles (fr, &wr, &r))
29591 int yb = window_text_bottom_y (w);
29592 struct glyph_row *row;
29593 int cursor_cleared_p, phys_cursor_on_p;
29594 struct glyph_row *first_overlapping_row, *last_overlapping_row;
29596 TRACE ((stderr, "expose_window (%d, %d, %d, %d)\n",
29597 r.x, r.y, r.width, r.height));
29599 /* Convert to window coordinates. */
29600 r.x -= WINDOW_LEFT_EDGE_X (w);
29601 r.y -= WINDOW_TOP_EDGE_Y (w);
29603 /* Turn off the cursor. */
29604 if (!w->pseudo_window_p
29605 && phys_cursor_in_rect_p (w, &r))
29607 x_clear_cursor (w);
29608 cursor_cleared_p = 1;
29610 else
29611 cursor_cleared_p = 0;
29613 /* If the row containing the cursor extends face to end of line,
29614 then expose_area might overwrite the cursor outside the
29615 rectangle and thus notice_overwritten_cursor might clear
29616 w->phys_cursor_on_p. We remember the original value and
29617 check later if it is changed. */
29618 phys_cursor_on_p = w->phys_cursor_on_p;
29620 /* Update lines intersecting rectangle R. */
29621 first_overlapping_row = last_overlapping_row = NULL;
29622 for (row = w->current_matrix->rows;
29623 row->enabled_p;
29624 ++row)
29626 int y0 = row->y;
29627 int y1 = MATRIX_ROW_BOTTOM_Y (row);
29629 if ((y0 >= r.y && y0 < r.y + r.height)
29630 || (y1 > r.y && y1 < r.y + r.height)
29631 || (r.y >= y0 && r.y < y1)
29632 || (r.y + r.height > y0 && r.y + r.height < y1))
29634 /* A header line may be overlapping, but there is no need
29635 to fix overlapping areas for them. KFS 2005-02-12 */
29636 if (row->overlapping_p && !row->mode_line_p)
29638 if (first_overlapping_row == NULL)
29639 first_overlapping_row = row;
29640 last_overlapping_row = row;
29643 row->clip = fr;
29644 if (expose_line (w, row, &r))
29645 mouse_face_overwritten_p = 1;
29646 row->clip = NULL;
29648 else if (row->overlapping_p)
29650 /* We must redraw a row overlapping the exposed area. */
29651 if (y0 < r.y
29652 ? y0 + row->phys_height > r.y
29653 : y0 + row->ascent - row->phys_ascent < r.y +r.height)
29655 if (first_overlapping_row == NULL)
29656 first_overlapping_row = row;
29657 last_overlapping_row = row;
29661 if (y1 >= yb)
29662 break;
29665 /* Display the mode line if there is one. */
29666 if (WINDOW_WANTS_MODELINE_P (w)
29667 && (row = MATRIX_MODE_LINE_ROW (w->current_matrix),
29668 row->enabled_p)
29669 && row->y < r.y + r.height)
29671 if (expose_line (w, row, &r))
29672 mouse_face_overwritten_p = 1;
29675 if (!w->pseudo_window_p)
29677 /* Fix the display of overlapping rows. */
29678 if (first_overlapping_row)
29679 expose_overlaps (w, first_overlapping_row, last_overlapping_row,
29680 fr);
29682 /* Draw border between windows. */
29683 if (WINDOW_RIGHT_DIVIDER_WIDTH (w))
29684 x_draw_right_divider (w);
29685 else
29686 x_draw_vertical_border (w);
29688 if (WINDOW_BOTTOM_DIVIDER_WIDTH (w))
29689 x_draw_bottom_divider (w);
29691 /* Turn the cursor on again. */
29692 if (cursor_cleared_p
29693 || (phys_cursor_on_p && !w->phys_cursor_on_p))
29694 update_window_cursor (w, 1);
29698 return mouse_face_overwritten_p;
29703 /* Redraw (parts) of all windows in the window tree rooted at W that
29704 intersect R. R contains frame pixel coordinates. Value is
29705 non-zero if the exposure overwrites mouse-face. */
29707 static int
29708 expose_window_tree (struct window *w, XRectangle *r)
29710 struct frame *f = XFRAME (w->frame);
29711 int mouse_face_overwritten_p = 0;
29713 while (w && !FRAME_GARBAGED_P (f))
29715 if (WINDOWP (w->contents))
29716 mouse_face_overwritten_p
29717 |= expose_window_tree (XWINDOW (w->contents), r);
29718 else
29719 mouse_face_overwritten_p |= expose_window (w, r);
29721 w = NILP (w->next) ? NULL : XWINDOW (w->next);
29724 return mouse_face_overwritten_p;
29728 /* EXPORT:
29729 Redisplay an exposed area of frame F. X and Y are the upper-left
29730 corner of the exposed rectangle. W and H are width and height of
29731 the exposed area. All are pixel values. W or H zero means redraw
29732 the entire frame. */
29734 void
29735 expose_frame (struct frame *f, int x, int y, int w, int h)
29737 XRectangle r;
29738 int mouse_face_overwritten_p = 0;
29740 TRACE ((stderr, "expose_frame "));
29742 /* No need to redraw if frame will be redrawn soon. */
29743 if (FRAME_GARBAGED_P (f))
29745 TRACE ((stderr, " garbaged\n"));
29746 return;
29749 /* If basic faces haven't been realized yet, there is no point in
29750 trying to redraw anything. This can happen when we get an expose
29751 event while Emacs is starting, e.g. by moving another window. */
29752 if (FRAME_FACE_CACHE (f) == NULL
29753 || FRAME_FACE_CACHE (f)->used < BASIC_FACE_ID_SENTINEL)
29755 TRACE ((stderr, " no faces\n"));
29756 return;
29759 if (w == 0 || h == 0)
29761 r.x = r.y = 0;
29762 r.width = FRAME_COLUMN_WIDTH (f) * FRAME_COLS (f);
29763 r.height = FRAME_LINE_HEIGHT (f) * FRAME_LINES (f);
29765 else
29767 r.x = x;
29768 r.y = y;
29769 r.width = w;
29770 r.height = h;
29773 TRACE ((stderr, "(%d, %d, %d, %d)\n", r.x, r.y, r.width, r.height));
29774 mouse_face_overwritten_p = expose_window_tree (XWINDOW (f->root_window), &r);
29776 #if ! defined (USE_GTK) && ! defined (HAVE_NS)
29777 if (WINDOWP (f->tool_bar_window))
29778 mouse_face_overwritten_p
29779 |= expose_window (XWINDOW (f->tool_bar_window), &r);
29780 #endif
29782 #ifdef HAVE_X_WINDOWS
29783 #ifndef MSDOS
29784 #if ! defined (USE_X_TOOLKIT) && ! defined (USE_GTK)
29785 if (WINDOWP (f->menu_bar_window))
29786 mouse_face_overwritten_p
29787 |= expose_window (XWINDOW (f->menu_bar_window), &r);
29788 #endif /* not USE_X_TOOLKIT and not USE_GTK */
29789 #endif
29790 #endif
29792 /* Some window managers support a focus-follows-mouse style with
29793 delayed raising of frames. Imagine a partially obscured frame,
29794 and moving the mouse into partially obscured mouse-face on that
29795 frame. The visible part of the mouse-face will be highlighted,
29796 then the WM raises the obscured frame. With at least one WM, KDE
29797 2.1, Emacs is not getting any event for the raising of the frame
29798 (even tried with SubstructureRedirectMask), only Expose events.
29799 These expose events will draw text normally, i.e. not
29800 highlighted. Which means we must redo the highlight here.
29801 Subsume it under ``we love X''. --gerd 2001-08-15 */
29802 /* Included in Windows version because Windows most likely does not
29803 do the right thing if any third party tool offers
29804 focus-follows-mouse with delayed raise. --jason 2001-10-12 */
29805 if (mouse_face_overwritten_p && !FRAME_GARBAGED_P (f))
29807 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
29808 if (f == hlinfo->mouse_face_mouse_frame)
29810 int mouse_x = hlinfo->mouse_face_mouse_x;
29811 int mouse_y = hlinfo->mouse_face_mouse_y;
29812 clear_mouse_face (hlinfo);
29813 note_mouse_highlight (f, mouse_x, mouse_y);
29819 /* EXPORT:
29820 Determine the intersection of two rectangles R1 and R2. Return
29821 the intersection in *RESULT. Value is non-zero if RESULT is not
29822 empty. */
29825 x_intersect_rectangles (XRectangle *r1, XRectangle *r2, XRectangle *result)
29827 XRectangle *left, *right;
29828 XRectangle *upper, *lower;
29829 int intersection_p = 0;
29831 /* Rearrange so that R1 is the left-most rectangle. */
29832 if (r1->x < r2->x)
29833 left = r1, right = r2;
29834 else
29835 left = r2, right = r1;
29837 /* X0 of the intersection is right.x0, if this is inside R1,
29838 otherwise there is no intersection. */
29839 if (right->x <= left->x + left->width)
29841 result->x = right->x;
29843 /* The right end of the intersection is the minimum of
29844 the right ends of left and right. */
29845 result->width = (min (left->x + left->width, right->x + right->width)
29846 - result->x);
29848 /* Same game for Y. */
29849 if (r1->y < r2->y)
29850 upper = r1, lower = r2;
29851 else
29852 upper = r2, lower = r1;
29854 /* The upper end of the intersection is lower.y0, if this is inside
29855 of upper. Otherwise, there is no intersection. */
29856 if (lower->y <= upper->y + upper->height)
29858 result->y = lower->y;
29860 /* The lower end of the intersection is the minimum of the lower
29861 ends of upper and lower. */
29862 result->height = (min (lower->y + lower->height,
29863 upper->y + upper->height)
29864 - result->y);
29865 intersection_p = 1;
29869 return intersection_p;
29872 #endif /* HAVE_WINDOW_SYSTEM */
29875 /***********************************************************************
29876 Initialization
29877 ***********************************************************************/
29879 void
29880 syms_of_xdisp (void)
29882 Vwith_echo_area_save_vector = Qnil;
29883 staticpro (&Vwith_echo_area_save_vector);
29885 Vmessage_stack = Qnil;
29886 staticpro (&Vmessage_stack);
29888 DEFSYM (Qinhibit_redisplay, "inhibit-redisplay");
29889 DEFSYM (Qredisplay_internal, "redisplay_internal (C function)");
29891 message_dolog_marker1 = Fmake_marker ();
29892 staticpro (&message_dolog_marker1);
29893 message_dolog_marker2 = Fmake_marker ();
29894 staticpro (&message_dolog_marker2);
29895 message_dolog_marker3 = Fmake_marker ();
29896 staticpro (&message_dolog_marker3);
29898 #ifdef GLYPH_DEBUG
29899 defsubr (&Sdump_frame_glyph_matrix);
29900 defsubr (&Sdump_glyph_matrix);
29901 defsubr (&Sdump_glyph_row);
29902 defsubr (&Sdump_tool_bar_row);
29903 defsubr (&Strace_redisplay);
29904 defsubr (&Strace_to_stderr);
29905 #endif
29906 #ifdef HAVE_WINDOW_SYSTEM
29907 defsubr (&Stool_bar_height);
29908 defsubr (&Slookup_image_map);
29909 #endif
29910 defsubr (&Sline_pixel_height);
29911 defsubr (&Sformat_mode_line);
29912 defsubr (&Sinvisible_p);
29913 defsubr (&Scurrent_bidi_paragraph_direction);
29914 defsubr (&Swindow_text_pixel_size);
29915 defsubr (&Smove_point_visually);
29917 DEFSYM (Qmenu_bar_update_hook, "menu-bar-update-hook");
29918 DEFSYM (Qoverriding_terminal_local_map, "overriding-terminal-local-map");
29919 DEFSYM (Qoverriding_local_map, "overriding-local-map");
29920 DEFSYM (Qwindow_scroll_functions, "window-scroll-functions");
29921 DEFSYM (Qwindow_text_change_functions, "window-text-change-functions");
29922 DEFSYM (Qredisplay_end_trigger_functions, "redisplay-end-trigger-functions");
29923 DEFSYM (Qinhibit_point_motion_hooks, "inhibit-point-motion-hooks");
29924 DEFSYM (Qeval, "eval");
29925 DEFSYM (QCdata, ":data");
29926 DEFSYM (Qdisplay, "display");
29927 DEFSYM (Qspace_width, "space-width");
29928 DEFSYM (Qraise, "raise");
29929 DEFSYM (Qslice, "slice");
29930 DEFSYM (Qspace, "space");
29931 DEFSYM (Qmargin, "margin");
29932 DEFSYM (Qpointer, "pointer");
29933 DEFSYM (Qleft_margin, "left-margin");
29934 DEFSYM (Qright_margin, "right-margin");
29935 DEFSYM (Qcenter, "center");
29936 DEFSYM (Qline_height, "line-height");
29937 DEFSYM (QCalign_to, ":align-to");
29938 DEFSYM (QCrelative_width, ":relative-width");
29939 DEFSYM (QCrelative_height, ":relative-height");
29940 DEFSYM (QCeval, ":eval");
29941 DEFSYM (QCpropertize, ":propertize");
29942 DEFSYM (QCfile, ":file");
29943 DEFSYM (Qfontified, "fontified");
29944 DEFSYM (Qfontification_functions, "fontification-functions");
29945 DEFSYM (Qtrailing_whitespace, "trailing-whitespace");
29946 DEFSYM (Qescape_glyph, "escape-glyph");
29947 DEFSYM (Qnobreak_space, "nobreak-space");
29948 DEFSYM (Qimage, "image");
29949 DEFSYM (Qtext, "text");
29950 DEFSYM (Qboth, "both");
29951 DEFSYM (Qboth_horiz, "both-horiz");
29952 DEFSYM (Qtext_image_horiz, "text-image-horiz");
29953 DEFSYM (QCmap, ":map");
29954 DEFSYM (QCpointer, ":pointer");
29955 DEFSYM (Qrect, "rect");
29956 DEFSYM (Qcircle, "circle");
29957 DEFSYM (Qpoly, "poly");
29958 DEFSYM (Qmessage_truncate_lines, "message-truncate-lines");
29959 DEFSYM (Qgrow_only, "grow-only");
29960 DEFSYM (Qinhibit_menubar_update, "inhibit-menubar-update");
29961 DEFSYM (Qinhibit_eval_during_redisplay, "inhibit-eval-during-redisplay");
29962 DEFSYM (Qposition, "position");
29963 DEFSYM (Qbuffer_position, "buffer-position");
29964 DEFSYM (Qobject, "object");
29965 DEFSYM (Qbar, "bar");
29966 DEFSYM (Qhbar, "hbar");
29967 DEFSYM (Qbox, "box");
29968 DEFSYM (Qhollow, "hollow");
29969 DEFSYM (Qhand, "hand");
29970 DEFSYM (Qarrow, "arrow");
29971 DEFSYM (Qinhibit_free_realized_faces, "inhibit-free-realized-faces");
29973 list_of_error = list1 (list2 (intern_c_string ("error"),
29974 intern_c_string ("void-variable")));
29975 staticpro (&list_of_error);
29977 DEFSYM (Qlast_arrow_position, "last-arrow-position");
29978 DEFSYM (Qlast_arrow_string, "last-arrow-string");
29979 DEFSYM (Qoverlay_arrow_string, "overlay-arrow-string");
29980 DEFSYM (Qoverlay_arrow_bitmap, "overlay-arrow-bitmap");
29982 echo_buffer[0] = echo_buffer[1] = Qnil;
29983 staticpro (&echo_buffer[0]);
29984 staticpro (&echo_buffer[1]);
29986 echo_area_buffer[0] = echo_area_buffer[1] = Qnil;
29987 staticpro (&echo_area_buffer[0]);
29988 staticpro (&echo_area_buffer[1]);
29990 Vmessages_buffer_name = build_pure_c_string ("*Messages*");
29991 staticpro (&Vmessages_buffer_name);
29993 mode_line_proptrans_alist = Qnil;
29994 staticpro (&mode_line_proptrans_alist);
29995 mode_line_string_list = Qnil;
29996 staticpro (&mode_line_string_list);
29997 mode_line_string_face = Qnil;
29998 staticpro (&mode_line_string_face);
29999 mode_line_string_face_prop = Qnil;
30000 staticpro (&mode_line_string_face_prop);
30001 Vmode_line_unwind_vector = Qnil;
30002 staticpro (&Vmode_line_unwind_vector);
30004 DEFSYM (Qmode_line_default_help_echo, "mode-line-default-help-echo");
30006 help_echo_string = Qnil;
30007 staticpro (&help_echo_string);
30008 help_echo_object = Qnil;
30009 staticpro (&help_echo_object);
30010 help_echo_window = Qnil;
30011 staticpro (&help_echo_window);
30012 previous_help_echo_string = Qnil;
30013 staticpro (&previous_help_echo_string);
30014 help_echo_pos = -1;
30016 DEFSYM (Qright_to_left, "right-to-left");
30017 DEFSYM (Qleft_to_right, "left-to-right");
30019 #ifdef HAVE_WINDOW_SYSTEM
30020 DEFVAR_BOOL ("x-stretch-cursor", x_stretch_cursor_p,
30021 doc: /* Non-nil means draw block cursor as wide as the glyph under it.
30022 For example, if a block cursor is over a tab, it will be drawn as
30023 wide as that tab on the display. */);
30024 x_stretch_cursor_p = 0;
30025 #endif
30027 DEFVAR_LISP ("show-trailing-whitespace", Vshow_trailing_whitespace,
30028 doc: /* Non-nil means highlight trailing whitespace.
30029 The face used for trailing whitespace is `trailing-whitespace'. */);
30030 Vshow_trailing_whitespace = Qnil;
30032 DEFVAR_LISP ("nobreak-char-display", Vnobreak_char_display,
30033 doc: /* Control highlighting of non-ASCII space and hyphen chars.
30034 If the value is t, Emacs highlights non-ASCII chars which have the
30035 same appearance as an ASCII space or hyphen, using the `nobreak-space'
30036 or `escape-glyph' face respectively.
30038 U+00A0 (no-break space), U+00AD (soft hyphen), U+2010 (hyphen), and
30039 U+2011 (non-breaking hyphen) are affected.
30041 Any other non-nil value means to display these characters as a escape
30042 glyph followed by an ordinary space or hyphen.
30044 A value of nil means no special handling of these characters. */);
30045 Vnobreak_char_display = Qt;
30047 DEFVAR_LISP ("void-text-area-pointer", Vvoid_text_area_pointer,
30048 doc: /* The pointer shape to show in void text areas.
30049 A value of nil means to show the text pointer. Other options are `arrow',
30050 `text', `hand', `vdrag', `hdrag', `modeline', and `hourglass'. */);
30051 Vvoid_text_area_pointer = Qarrow;
30053 DEFVAR_LISP ("inhibit-redisplay", Vinhibit_redisplay,
30054 doc: /* Non-nil means don't actually do any redisplay.
30055 This is used for internal purposes. */);
30056 Vinhibit_redisplay = Qnil;
30058 DEFVAR_LISP ("global-mode-string", Vglobal_mode_string,
30059 doc: /* String (or mode line construct) included (normally) in `mode-line-format'. */);
30060 Vglobal_mode_string = Qnil;
30062 DEFVAR_LISP ("overlay-arrow-position", Voverlay_arrow_position,
30063 doc: /* Marker for where to display an arrow on top of the buffer text.
30064 This must be the beginning of a line in order to work.
30065 See also `overlay-arrow-string'. */);
30066 Voverlay_arrow_position = Qnil;
30068 DEFVAR_LISP ("overlay-arrow-string", Voverlay_arrow_string,
30069 doc: /* String to display as an arrow in non-window frames.
30070 See also `overlay-arrow-position'. */);
30071 Voverlay_arrow_string = build_pure_c_string ("=>");
30073 DEFVAR_LISP ("overlay-arrow-variable-list", Voverlay_arrow_variable_list,
30074 doc: /* List of variables (symbols) which hold markers for overlay arrows.
30075 The symbols on this list are examined during redisplay to determine
30076 where to display overlay arrows. */);
30077 Voverlay_arrow_variable_list
30078 = list1 (intern_c_string ("overlay-arrow-position"));
30080 DEFVAR_INT ("scroll-step", emacs_scroll_step,
30081 doc: /* The number of lines to try scrolling a window by when point moves out.
30082 If that fails to bring point back on frame, point is centered instead.
30083 If this is zero, point is always centered after it moves off frame.
30084 If you want scrolling to always be a line at a time, you should set
30085 `scroll-conservatively' to a large value rather than set this to 1. */);
30087 DEFVAR_INT ("scroll-conservatively", scroll_conservatively,
30088 doc: /* Scroll up to this many lines, to bring point back on screen.
30089 If point moves off-screen, redisplay will scroll by up to
30090 `scroll-conservatively' lines in order to bring point just barely
30091 onto the screen again. If that cannot be done, then redisplay
30092 recenters point as usual.
30094 If the value is greater than 100, redisplay will never recenter point,
30095 but will always scroll just enough text to bring point into view, even
30096 if you move far away.
30098 A value of zero means always recenter point if it moves off screen. */);
30099 scroll_conservatively = 0;
30101 DEFVAR_INT ("scroll-margin", scroll_margin,
30102 doc: /* Number of lines of margin at the top and bottom of a window.
30103 Recenter the window whenever point gets within this many lines
30104 of the top or bottom of the window. */);
30105 scroll_margin = 0;
30107 DEFVAR_LISP ("display-pixels-per-inch", Vdisplay_pixels_per_inch,
30108 doc: /* Pixels per inch value for non-window system displays.
30109 Value is a number or a cons (WIDTH-DPI . HEIGHT-DPI). */);
30110 Vdisplay_pixels_per_inch = make_float (72.0);
30112 #ifdef GLYPH_DEBUG
30113 DEFVAR_INT ("debug-end-pos", debug_end_pos, doc: /* Don't ask. */);
30114 #endif
30116 DEFVAR_LISP ("truncate-partial-width-windows",
30117 Vtruncate_partial_width_windows,
30118 doc: /* Non-nil means truncate lines in windows narrower than the frame.
30119 For an integer value, truncate lines in each window narrower than the
30120 full frame width, provided the window width is less than that integer;
30121 otherwise, respect the value of `truncate-lines'.
30123 For any other non-nil value, truncate lines in all windows that do
30124 not span the full frame width.
30126 A value of nil means to respect the value of `truncate-lines'.
30128 If `word-wrap' is enabled, you might want to reduce this. */);
30129 Vtruncate_partial_width_windows = make_number (50);
30131 DEFVAR_LISP ("line-number-display-limit", Vline_number_display_limit,
30132 doc: /* Maximum buffer size for which line number should be displayed.
30133 If the buffer is bigger than this, the line number does not appear
30134 in the mode line. A value of nil means no limit. */);
30135 Vline_number_display_limit = Qnil;
30137 DEFVAR_INT ("line-number-display-limit-width",
30138 line_number_display_limit_width,
30139 doc: /* Maximum line width (in characters) for line number display.
30140 If the average length of the lines near point is bigger than this, then the
30141 line number may be omitted from the mode line. */);
30142 line_number_display_limit_width = 200;
30144 DEFVAR_BOOL ("highlight-nonselected-windows", highlight_nonselected_windows,
30145 doc: /* Non-nil means highlight region even in nonselected windows. */);
30146 highlight_nonselected_windows = 0;
30148 DEFVAR_BOOL ("multiple-frames", multiple_frames,
30149 doc: /* Non-nil if more than one frame is visible on this display.
30150 Minibuffer-only frames don't count, but iconified frames do.
30151 This variable is not guaranteed to be accurate except while processing
30152 `frame-title-format' and `icon-title-format'. */);
30154 DEFVAR_LISP ("frame-title-format", Vframe_title_format,
30155 doc: /* Template for displaying the title bar of visible frames.
30156 \(Assuming the window manager supports this feature.)
30158 This variable has the same structure as `mode-line-format', except that
30159 the %c and %l constructs are ignored. It is used only on frames for
30160 which no explicit name has been set \(see `modify-frame-parameters'). */);
30162 DEFVAR_LISP ("icon-title-format", Vicon_title_format,
30163 doc: /* Template for displaying the title bar of an iconified frame.
30164 \(Assuming the window manager supports this feature.)
30165 This variable has the same structure as `mode-line-format' (which see),
30166 and is used only on frames for which no explicit name has been set
30167 \(see `modify-frame-parameters'). */);
30168 Vicon_title_format
30169 = Vframe_title_format
30170 = listn (CONSTYPE_PURE, 3,
30171 intern_c_string ("multiple-frames"),
30172 build_pure_c_string ("%b"),
30173 listn (CONSTYPE_PURE, 4,
30174 empty_unibyte_string,
30175 intern_c_string ("invocation-name"),
30176 build_pure_c_string ("@"),
30177 intern_c_string ("system-name")));
30179 DEFVAR_LISP ("message-log-max", Vmessage_log_max,
30180 doc: /* Maximum number of lines to keep in the message log buffer.
30181 If nil, disable message logging. If t, log messages but don't truncate
30182 the buffer when it becomes large. */);
30183 Vmessage_log_max = make_number (1000);
30185 DEFVAR_LISP ("window-size-change-functions", Vwindow_size_change_functions,
30186 doc: /* Functions called before redisplay, if window sizes have changed.
30187 The value should be a list of functions that take one argument.
30188 Just before redisplay, for each frame, if any of its windows have changed
30189 size since the last redisplay, or have been split or deleted,
30190 all the functions in the list are called, with the frame as argument. */);
30191 Vwindow_size_change_functions = Qnil;
30193 DEFVAR_LISP ("window-scroll-functions", Vwindow_scroll_functions,
30194 doc: /* List of functions to call before redisplaying a window with scrolling.
30195 Each function is called with two arguments, the window and its new
30196 display-start position. Note that these functions are also called by
30197 `set-window-buffer'. Also note that the value of `window-end' is not
30198 valid when these functions are called.
30200 Warning: Do not use this feature to alter the way the window
30201 is scrolled. It is not designed for that, and such use probably won't
30202 work. */);
30203 Vwindow_scroll_functions = Qnil;
30205 DEFVAR_LISP ("window-text-change-functions",
30206 Vwindow_text_change_functions,
30207 doc: /* Functions to call in redisplay when text in the window might change. */);
30208 Vwindow_text_change_functions = Qnil;
30210 DEFVAR_LISP ("redisplay-end-trigger-functions", Vredisplay_end_trigger_functions,
30211 doc: /* Functions called when redisplay of a window reaches the end trigger.
30212 Each function is called with two arguments, the window and the end trigger value.
30213 See `set-window-redisplay-end-trigger'. */);
30214 Vredisplay_end_trigger_functions = Qnil;
30216 DEFVAR_LISP ("mouse-autoselect-window", Vmouse_autoselect_window,
30217 doc: /* Non-nil means autoselect window with mouse pointer.
30218 If nil, do not autoselect windows.
30219 A positive number means delay autoselection by that many seconds: a
30220 window is autoselected only after the mouse has remained in that
30221 window for the duration of the delay.
30222 A negative number has a similar effect, but causes windows to be
30223 autoselected only after the mouse has stopped moving. \(Because of
30224 the way Emacs compares mouse events, you will occasionally wait twice
30225 that time before the window gets selected.\)
30226 Any other value means to autoselect window instantaneously when the
30227 mouse pointer enters it.
30229 Autoselection selects the minibuffer only if it is active, and never
30230 unselects the minibuffer if it is active.
30232 When customizing this variable make sure that the actual value of
30233 `focus-follows-mouse' matches the behavior of your window manager. */);
30234 Vmouse_autoselect_window = Qnil;
30236 DEFVAR_LISP ("auto-resize-tool-bars", Vauto_resize_tool_bars,
30237 doc: /* Non-nil means automatically resize tool-bars.
30238 This dynamically changes the tool-bar's height to the minimum height
30239 that is needed to make all tool-bar items visible.
30240 If value is `grow-only', the tool-bar's height is only increased
30241 automatically; to decrease the tool-bar height, use \\[recenter]. */);
30242 Vauto_resize_tool_bars = Qt;
30244 DEFVAR_BOOL ("auto-raise-tool-bar-buttons", auto_raise_tool_bar_buttons_p,
30245 doc: /* Non-nil means raise tool-bar buttons when the mouse moves over them. */);
30246 auto_raise_tool_bar_buttons_p = 1;
30248 DEFVAR_BOOL ("make-cursor-line-fully-visible", make_cursor_line_fully_visible_p,
30249 doc: /* Non-nil means to scroll (recenter) cursor line if it is not fully visible. */);
30250 make_cursor_line_fully_visible_p = 1;
30252 DEFVAR_LISP ("tool-bar-border", Vtool_bar_border,
30253 doc: /* Border below tool-bar in pixels.
30254 If an integer, use it as the height of the border.
30255 If it is one of `internal-border-width' or `border-width', use the
30256 value of the corresponding frame parameter.
30257 Otherwise, no border is added below the tool-bar. */);
30258 Vtool_bar_border = Qinternal_border_width;
30260 DEFVAR_LISP ("tool-bar-button-margin", Vtool_bar_button_margin,
30261 doc: /* Margin around tool-bar buttons in pixels.
30262 If an integer, use that for both horizontal and vertical margins.
30263 Otherwise, value should be a pair of integers `(HORZ . VERT)' with
30264 HORZ specifying the horizontal margin, and VERT specifying the
30265 vertical margin. */);
30266 Vtool_bar_button_margin = make_number (DEFAULT_TOOL_BAR_BUTTON_MARGIN);
30268 DEFVAR_INT ("tool-bar-button-relief", tool_bar_button_relief,
30269 doc: /* Relief thickness of tool-bar buttons. */);
30270 tool_bar_button_relief = DEFAULT_TOOL_BAR_BUTTON_RELIEF;
30272 DEFVAR_LISP ("tool-bar-style", Vtool_bar_style,
30273 doc: /* Tool bar style to use.
30274 It can be one of
30275 image - show images only
30276 text - show text only
30277 both - show both, text below image
30278 both-horiz - show text to the right of the image
30279 text-image-horiz - show text to the left of the image
30280 any other - use system default or image if no system default.
30282 This variable only affects the GTK+ toolkit version of Emacs. */);
30283 Vtool_bar_style = Qnil;
30285 DEFVAR_INT ("tool-bar-max-label-size", tool_bar_max_label_size,
30286 doc: /* Maximum number of characters a label can have to be shown.
30287 The tool bar style must also show labels for this to have any effect, see
30288 `tool-bar-style'. */);
30289 tool_bar_max_label_size = DEFAULT_TOOL_BAR_LABEL_SIZE;
30291 DEFVAR_LISP ("fontification-functions", Vfontification_functions,
30292 doc: /* List of functions to call to fontify regions of text.
30293 Each function is called with one argument POS. Functions must
30294 fontify a region starting at POS in the current buffer, and give
30295 fontified regions the property `fontified'. */);
30296 Vfontification_functions = Qnil;
30297 Fmake_variable_buffer_local (Qfontification_functions);
30299 DEFVAR_BOOL ("unibyte-display-via-language-environment",
30300 unibyte_display_via_language_environment,
30301 doc: /* Non-nil means display unibyte text according to language environment.
30302 Specifically, this means that raw bytes in the range 160-255 decimal
30303 are displayed by converting them to the equivalent multibyte characters
30304 according to the current language environment. As a result, they are
30305 displayed according to the current fontset.
30307 Note that this variable affects only how these bytes are displayed,
30308 but does not change the fact they are interpreted as raw bytes. */);
30309 unibyte_display_via_language_environment = 0;
30311 DEFVAR_LISP ("max-mini-window-height", Vmax_mini_window_height,
30312 doc: /* Maximum height for resizing mini-windows (the minibuffer and the echo area).
30313 If a float, it specifies a fraction of the mini-window frame's height.
30314 If an integer, it specifies a number of lines. */);
30315 Vmax_mini_window_height = make_float (0.25);
30317 DEFVAR_LISP ("resize-mini-windows", Vresize_mini_windows,
30318 doc: /* How to resize mini-windows (the minibuffer and the echo area).
30319 A value of nil means don't automatically resize mini-windows.
30320 A value of t means resize them to fit the text displayed in them.
30321 A value of `grow-only', the default, means let mini-windows grow only;
30322 they return to their normal size when the minibuffer is closed, or the
30323 echo area becomes empty. */);
30324 Vresize_mini_windows = Qgrow_only;
30326 DEFVAR_LISP ("blink-cursor-alist", Vblink_cursor_alist,
30327 doc: /* Alist specifying how to blink the cursor off.
30328 Each element has the form (ON-STATE . OFF-STATE). Whenever the
30329 `cursor-type' frame-parameter or variable equals ON-STATE,
30330 comparing using `equal', Emacs uses OFF-STATE to specify
30331 how to blink it off. ON-STATE and OFF-STATE are values for
30332 the `cursor-type' frame parameter.
30334 If a frame's ON-STATE has no entry in this list,
30335 the frame's other specifications determine how to blink the cursor off. */);
30336 Vblink_cursor_alist = Qnil;
30338 DEFVAR_BOOL ("auto-hscroll-mode", automatic_hscrolling_p,
30339 doc: /* Allow or disallow automatic horizontal scrolling of windows.
30340 If non-nil, windows are automatically scrolled horizontally to make
30341 point visible. */);
30342 automatic_hscrolling_p = 1;
30343 DEFSYM (Qauto_hscroll_mode, "auto-hscroll-mode");
30345 DEFVAR_INT ("hscroll-margin", hscroll_margin,
30346 doc: /* How many columns away from the window edge point is allowed to get
30347 before automatic hscrolling will horizontally scroll the window. */);
30348 hscroll_margin = 5;
30350 DEFVAR_LISP ("hscroll-step", Vhscroll_step,
30351 doc: /* How many columns to scroll the window when point gets too close to the edge.
30352 When point is less than `hscroll-margin' columns from the window
30353 edge, automatic hscrolling will scroll the window by the amount of columns
30354 determined by this variable. If its value is a positive integer, scroll that
30355 many columns. If it's a positive floating-point number, it specifies the
30356 fraction of the window's width to scroll. If it's nil or zero, point will be
30357 centered horizontally after the scroll. Any other value, including negative
30358 numbers, are treated as if the value were zero.
30360 Automatic hscrolling always moves point outside the scroll margin, so if
30361 point was more than scroll step columns inside the margin, the window will
30362 scroll more than the value given by the scroll step.
30364 Note that the lower bound for automatic hscrolling specified by `scroll-left'
30365 and `scroll-right' overrides this variable's effect. */);
30366 Vhscroll_step = make_number (0);
30368 DEFVAR_BOOL ("message-truncate-lines", message_truncate_lines,
30369 doc: /* If non-nil, messages are truncated instead of resizing the echo area.
30370 Bind this around calls to `message' to let it take effect. */);
30371 message_truncate_lines = 0;
30373 DEFVAR_LISP ("menu-bar-update-hook", Vmenu_bar_update_hook,
30374 doc: /* Normal hook run to update the menu bar definitions.
30375 Redisplay runs this hook before it redisplays the menu bar.
30376 This is used to update menus such as Buffers, whose contents depend on
30377 various data. */);
30378 Vmenu_bar_update_hook = Qnil;
30380 DEFVAR_LISP ("menu-updating-frame", Vmenu_updating_frame,
30381 doc: /* Frame for which we are updating a menu.
30382 The enable predicate for a menu binding should check this variable. */);
30383 Vmenu_updating_frame = Qnil;
30385 DEFVAR_BOOL ("inhibit-menubar-update", inhibit_menubar_update,
30386 doc: /* Non-nil means don't update menu bars. Internal use only. */);
30387 inhibit_menubar_update = 0;
30389 DEFVAR_LISP ("wrap-prefix", Vwrap_prefix,
30390 doc: /* Prefix prepended to all continuation lines at display time.
30391 The value may be a string, an image, or a stretch-glyph; it is
30392 interpreted in the same way as the value of a `display' text property.
30394 This variable is overridden by any `wrap-prefix' text or overlay
30395 property.
30397 To add a prefix to non-continuation lines, use `line-prefix'. */);
30398 Vwrap_prefix = Qnil;
30399 DEFSYM (Qwrap_prefix, "wrap-prefix");
30400 Fmake_variable_buffer_local (Qwrap_prefix);
30402 DEFVAR_LISP ("line-prefix", Vline_prefix,
30403 doc: /* Prefix prepended to all non-continuation lines at display time.
30404 The value may be a string, an image, or a stretch-glyph; it is
30405 interpreted in the same way as the value of a `display' text property.
30407 This variable is overridden by any `line-prefix' text or overlay
30408 property.
30410 To add a prefix to continuation lines, use `wrap-prefix'. */);
30411 Vline_prefix = Qnil;
30412 DEFSYM (Qline_prefix, "line-prefix");
30413 Fmake_variable_buffer_local (Qline_prefix);
30415 DEFVAR_BOOL ("inhibit-eval-during-redisplay", inhibit_eval_during_redisplay,
30416 doc: /* Non-nil means don't eval Lisp during redisplay. */);
30417 inhibit_eval_during_redisplay = 0;
30419 DEFVAR_BOOL ("inhibit-free-realized-faces", inhibit_free_realized_faces,
30420 doc: /* Non-nil means don't free realized faces. Internal use only. */);
30421 inhibit_free_realized_faces = 0;
30423 #ifdef GLYPH_DEBUG
30424 DEFVAR_BOOL ("inhibit-try-window-id", inhibit_try_window_id,
30425 doc: /* Inhibit try_window_id display optimization. */);
30426 inhibit_try_window_id = 0;
30428 DEFVAR_BOOL ("inhibit-try-window-reusing", inhibit_try_window_reusing,
30429 doc: /* Inhibit try_window_reusing display optimization. */);
30430 inhibit_try_window_reusing = 0;
30432 DEFVAR_BOOL ("inhibit-try-cursor-movement", inhibit_try_cursor_movement,
30433 doc: /* Inhibit try_cursor_movement display optimization. */);
30434 inhibit_try_cursor_movement = 0;
30435 #endif /* GLYPH_DEBUG */
30437 DEFVAR_INT ("overline-margin", overline_margin,
30438 doc: /* Space between overline and text, in pixels.
30439 The default value is 2: the height of the overline (1 pixel) plus 1 pixel
30440 margin to the character height. */);
30441 overline_margin = 2;
30443 DEFVAR_INT ("underline-minimum-offset",
30444 underline_minimum_offset,
30445 doc: /* Minimum distance between baseline and underline.
30446 This can improve legibility of underlined text at small font sizes,
30447 particularly when using variable `x-use-underline-position-properties'
30448 with fonts that specify an UNDERLINE_POSITION relatively close to the
30449 baseline. The default value is 1. */);
30450 underline_minimum_offset = 1;
30452 DEFVAR_BOOL ("display-hourglass", display_hourglass_p,
30453 doc: /* Non-nil means show an hourglass pointer, when Emacs is busy.
30454 This feature only works when on a window system that can change
30455 cursor shapes. */);
30456 display_hourglass_p = 1;
30458 DEFVAR_LISP ("hourglass-delay", Vhourglass_delay,
30459 doc: /* Seconds to wait before displaying an hourglass pointer when Emacs is busy. */);
30460 Vhourglass_delay = make_number (DEFAULT_HOURGLASS_DELAY);
30462 #ifdef HAVE_WINDOW_SYSTEM
30463 hourglass_atimer = NULL;
30464 hourglass_shown_p = 0;
30465 #endif /* HAVE_WINDOW_SYSTEM */
30467 DEFSYM (Qglyphless_char, "glyphless-char");
30468 DEFSYM (Qhex_code, "hex-code");
30469 DEFSYM (Qempty_box, "empty-box");
30470 DEFSYM (Qthin_space, "thin-space");
30471 DEFSYM (Qzero_width, "zero-width");
30473 DEFVAR_LISP ("pre-redisplay-function", Vpre_redisplay_function,
30474 doc: /* Function run just before redisplay.
30475 It is called with one argument, which is the set of windows that are to
30476 be redisplayed. This set can be nil (meaning, only the selected window),
30477 or t (meaning all windows). */);
30478 Vpre_redisplay_function = intern ("ignore");
30480 DEFSYM (Qglyphless_char_display, "glyphless-char-display");
30481 Fput (Qglyphless_char_display, Qchar_table_extra_slots, make_number (1));
30483 DEFVAR_LISP ("glyphless-char-display", Vglyphless_char_display,
30484 doc: /* Char-table defining glyphless characters.
30485 Each element, if non-nil, should be one of the following:
30486 an ASCII acronym string: display this string in a box
30487 `hex-code': display the hexadecimal code of a character in a box
30488 `empty-box': display as an empty box
30489 `thin-space': display as 1-pixel width space
30490 `zero-width': don't display
30491 An element may also be a cons cell (GRAPHICAL . TEXT), which specifies the
30492 display method for graphical terminals and text terminals respectively.
30493 GRAPHICAL and TEXT should each have one of the values listed above.
30495 The char-table has one extra slot to control the display of a character for
30496 which no font is found. This slot only takes effect on graphical terminals.
30497 Its value should be an ASCII acronym string, `hex-code', `empty-box', or
30498 `thin-space'. The default is `empty-box'.
30500 If a character has a non-nil entry in an active display table, the
30501 display table takes effect; in this case, Emacs does not consult
30502 `glyphless-char-display' at all. */);
30503 Vglyphless_char_display = Fmake_char_table (Qglyphless_char_display, Qnil);
30504 Fset_char_table_extra_slot (Vglyphless_char_display, make_number (0),
30505 Qempty_box);
30507 DEFVAR_LISP ("debug-on-message", Vdebug_on_message,
30508 doc: /* If non-nil, debug if a message matching this regexp is displayed. */);
30509 Vdebug_on_message = Qnil;
30511 DEFVAR_LISP ("redisplay--all-windows-cause", Vredisplay__all_windows_cause,
30512 doc: /* */);
30513 Vredisplay__all_windows_cause
30514 = Fmake_vector (make_number (100), make_number (0));
30516 DEFVAR_LISP ("redisplay--mode-lines-cause", Vredisplay__mode_lines_cause,
30517 doc: /* */);
30518 Vredisplay__mode_lines_cause
30519 = Fmake_vector (make_number (100), make_number (0));
30523 /* Initialize this module when Emacs starts. */
30525 void
30526 init_xdisp (void)
30528 CHARPOS (this_line_start_pos) = 0;
30530 if (!noninteractive)
30532 struct window *m = XWINDOW (minibuf_window);
30533 Lisp_Object frame = m->frame;
30534 struct frame *f = XFRAME (frame);
30535 Lisp_Object root = FRAME_ROOT_WINDOW (f);
30536 struct window *r = XWINDOW (root);
30537 int i;
30539 echo_area_window = minibuf_window;
30541 r->top_line = FRAME_TOP_MARGIN (f);
30542 r->pixel_top = r->top_line * FRAME_LINE_HEIGHT (f);
30543 r->total_cols = FRAME_COLS (f);
30544 r->pixel_width = r->total_cols * FRAME_COLUMN_WIDTH (f);
30545 r->total_lines = FRAME_LINES (f) - 1 - FRAME_TOP_MARGIN (f);
30546 r->pixel_height = r->total_lines * FRAME_LINE_HEIGHT (f);
30548 m->top_line = FRAME_LINES (f) - 1;
30549 m->pixel_top = m->top_line * FRAME_LINE_HEIGHT (f);
30550 m->total_cols = FRAME_COLS (f);
30551 m->pixel_width = m->total_cols * FRAME_COLUMN_WIDTH (f);
30552 m->total_lines = 1;
30553 m->pixel_height = m->total_lines * FRAME_LINE_HEIGHT (f);
30555 scratch_glyph_row.glyphs[TEXT_AREA] = scratch_glyphs;
30556 scratch_glyph_row.glyphs[TEXT_AREA + 1]
30557 = scratch_glyphs + MAX_SCRATCH_GLYPHS;
30559 /* The default ellipsis glyphs `...'. */
30560 for (i = 0; i < 3; ++i)
30561 default_invis_vector[i] = make_number ('.');
30565 /* Allocate the buffer for frame titles.
30566 Also used for `format-mode-line'. */
30567 int size = 100;
30568 mode_line_noprop_buf = xmalloc (size);
30569 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
30570 mode_line_noprop_ptr = mode_line_noprop_buf;
30571 mode_line_target = MODE_LINE_DISPLAY;
30574 help_echo_showing_p = 0;
30577 #ifdef HAVE_WINDOW_SYSTEM
30579 /* Platform-independent portion of hourglass implementation. */
30581 /* Cancel a currently active hourglass timer, and start a new one. */
30582 void
30583 start_hourglass (void)
30585 struct timespec delay;
30587 cancel_hourglass ();
30589 if (INTEGERP (Vhourglass_delay)
30590 && XINT (Vhourglass_delay) > 0)
30591 delay = make_timespec (min (XINT (Vhourglass_delay),
30592 TYPE_MAXIMUM (time_t)),
30594 else if (FLOATP (Vhourglass_delay)
30595 && XFLOAT_DATA (Vhourglass_delay) > 0)
30596 delay = dtotimespec (XFLOAT_DATA (Vhourglass_delay));
30597 else
30598 delay = make_timespec (DEFAULT_HOURGLASS_DELAY, 0);
30600 #ifdef HAVE_NTGUI
30602 extern void w32_note_current_window (void);
30603 w32_note_current_window ();
30605 #endif /* HAVE_NTGUI */
30607 hourglass_atimer = start_atimer (ATIMER_RELATIVE, delay,
30608 show_hourglass, NULL);
30612 /* Cancel the hourglass cursor timer if active, hide a busy cursor if
30613 shown. */
30614 void
30615 cancel_hourglass (void)
30617 if (hourglass_atimer)
30619 cancel_atimer (hourglass_atimer);
30620 hourglass_atimer = NULL;
30623 if (hourglass_shown_p)
30624 hide_hourglass ();
30627 #endif /* HAVE_WINDOW_SYSTEM */