Fix bug #17281 with infloop in line-pixel-height.
[emacs.git] / src / xdisp.c
blobad5f6a6bf83c8c418f6e690b04de1c81b2bcd3b7
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.glyph_row->used[TEXT_AREA] = 0;
18692 CHARPOS (truncate_it.position) = BYTEPOS (truncate_it.position) = -1;
18693 truncate_it.object = make_number (0);
18694 produce_special_glyphs (&truncate_it, IT_TRUNCATION);
18696 /* Overwrite glyphs from IT with truncation glyphs. */
18697 if (!it->glyph_row->reversed_p)
18699 short tused = truncate_it.glyph_row->used[TEXT_AREA];
18701 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
18702 end = from + tused;
18703 to = it->glyph_row->glyphs[TEXT_AREA];
18704 toend = to + it->glyph_row->used[TEXT_AREA];
18705 if (FRAME_WINDOW_P (it->f))
18707 /* On GUI frames, when variable-size fonts are displayed,
18708 the truncation glyphs may need more pixels than the row's
18709 glyphs they overwrite. We overwrite more glyphs to free
18710 enough screen real estate, and enlarge the stretch glyph
18711 on the right (see display_line), if there is one, to
18712 preserve the screen position of the truncation glyphs on
18713 the right. */
18714 int w = 0;
18715 struct glyph *g = to;
18716 short used;
18718 /* The first glyph could be partially visible, in which case
18719 it->glyph_row->x will be negative. But we want the left
18720 truncation glyphs to be aligned at the left margin of the
18721 window, so we override the x coordinate at which the row
18722 will begin. */
18723 it->glyph_row->x = 0;
18724 while (g < toend && w < it->truncation_pixel_width)
18726 w += g->pixel_width;
18727 ++g;
18729 if (g - to - tused > 0)
18731 memmove (to + tused, g, (toend - g) * sizeof(*g));
18732 it->glyph_row->used[TEXT_AREA] -= g - to - tused;
18734 used = it->glyph_row->used[TEXT_AREA];
18735 if (it->glyph_row->truncated_on_right_p
18736 && WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0
18737 && it->glyph_row->glyphs[TEXT_AREA][used - 2].type
18738 == STRETCH_GLYPH)
18740 int extra = w - it->truncation_pixel_width;
18742 it->glyph_row->glyphs[TEXT_AREA][used - 2].pixel_width += extra;
18746 while (from < end)
18747 *to++ = *from++;
18749 /* There may be padding glyphs left over. Overwrite them too. */
18750 if (!FRAME_WINDOW_P (it->f))
18752 while (to < toend && CHAR_GLYPH_PADDING_P (*to))
18754 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
18755 while (from < end)
18756 *to++ = *from++;
18760 if (to > toend)
18761 it->glyph_row->used[TEXT_AREA] = to - it->glyph_row->glyphs[TEXT_AREA];
18763 else
18765 short tused = truncate_it.glyph_row->used[TEXT_AREA];
18767 /* In R2L rows, overwrite the last (rightmost) glyphs, and do
18768 that back to front. */
18769 end = truncate_it.glyph_row->glyphs[TEXT_AREA];
18770 from = end + truncate_it.glyph_row->used[TEXT_AREA] - 1;
18771 toend = it->glyph_row->glyphs[TEXT_AREA];
18772 to = toend + it->glyph_row->used[TEXT_AREA] - 1;
18773 if (FRAME_WINDOW_P (it->f))
18775 int w = 0;
18776 struct glyph *g = to;
18778 while (g >= toend && w < it->truncation_pixel_width)
18780 w += g->pixel_width;
18781 --g;
18783 if (to - g - tused > 0)
18784 to = g + tused;
18785 if (it->glyph_row->truncated_on_right_p
18786 && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0
18787 && it->glyph_row->glyphs[TEXT_AREA][1].type == STRETCH_GLYPH)
18789 int extra = w - it->truncation_pixel_width;
18791 it->glyph_row->glyphs[TEXT_AREA][1].pixel_width += extra;
18795 while (from >= end && to >= toend)
18796 *to-- = *from--;
18797 if (!FRAME_WINDOW_P (it->f))
18799 while (to >= toend && CHAR_GLYPH_PADDING_P (*to))
18801 from =
18802 truncate_it.glyph_row->glyphs[TEXT_AREA]
18803 + truncate_it.glyph_row->used[TEXT_AREA] - 1;
18804 while (from >= end && to >= toend)
18805 *to-- = *from--;
18808 if (from >= end)
18810 /* Need to free some room before prepending additional
18811 glyphs. */
18812 int move_by = from - end + 1;
18813 struct glyph *g0 = it->glyph_row->glyphs[TEXT_AREA];
18814 struct glyph *g = g0 + it->glyph_row->used[TEXT_AREA] - 1;
18816 for ( ; g >= g0; g--)
18817 g[move_by] = *g;
18818 while (from >= end)
18819 *to-- = *from--;
18820 it->glyph_row->used[TEXT_AREA] += move_by;
18825 /* Compute the hash code for ROW. */
18826 unsigned
18827 row_hash (struct glyph_row *row)
18829 int area, k;
18830 unsigned hashval = 0;
18832 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
18833 for (k = 0; k < row->used[area]; ++k)
18834 hashval = ((((hashval << 4) + (hashval >> 24)) & 0x0fffffff)
18835 + row->glyphs[area][k].u.val
18836 + row->glyphs[area][k].face_id
18837 + row->glyphs[area][k].padding_p
18838 + (row->glyphs[area][k].type << 2));
18840 return hashval;
18843 /* Compute the pixel height and width of IT->glyph_row.
18845 Most of the time, ascent and height of a display line will be equal
18846 to the max_ascent and max_height values of the display iterator
18847 structure. This is not the case if
18849 1. We hit ZV without displaying anything. In this case, max_ascent
18850 and max_height will be zero.
18852 2. We have some glyphs that don't contribute to the line height.
18853 (The glyph row flag contributes_to_line_height_p is for future
18854 pixmap extensions).
18856 The first case is easily covered by using default values because in
18857 these cases, the line height does not really matter, except that it
18858 must not be zero. */
18860 static void
18861 compute_line_metrics (struct it *it)
18863 struct glyph_row *row = it->glyph_row;
18865 if (FRAME_WINDOW_P (it->f))
18867 int i, min_y, max_y;
18869 /* The line may consist of one space only, that was added to
18870 place the cursor on it. If so, the row's height hasn't been
18871 computed yet. */
18872 if (row->height == 0)
18874 if (it->max_ascent + it->max_descent == 0)
18875 it->max_descent = it->max_phys_descent = FRAME_LINE_HEIGHT (it->f);
18876 row->ascent = it->max_ascent;
18877 row->height = it->max_ascent + it->max_descent;
18878 row->phys_ascent = it->max_phys_ascent;
18879 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
18880 row->extra_line_spacing = it->max_extra_line_spacing;
18883 /* Compute the width of this line. */
18884 row->pixel_width = row->x;
18885 for (i = 0; i < row->used[TEXT_AREA]; ++i)
18886 row->pixel_width += row->glyphs[TEXT_AREA][i].pixel_width;
18888 eassert (row->pixel_width >= 0);
18889 eassert (row->ascent >= 0 && row->height > 0);
18891 row->overlapping_p = (MATRIX_ROW_OVERLAPS_SUCC_P (row)
18892 || MATRIX_ROW_OVERLAPS_PRED_P (row));
18894 /* If first line's physical ascent is larger than its logical
18895 ascent, use the physical ascent, and make the row taller.
18896 This makes accented characters fully visible. */
18897 if (row == MATRIX_FIRST_TEXT_ROW (it->w->desired_matrix)
18898 && row->phys_ascent > row->ascent)
18900 row->height += row->phys_ascent - row->ascent;
18901 row->ascent = row->phys_ascent;
18904 /* Compute how much of the line is visible. */
18905 row->visible_height = row->height;
18907 min_y = WINDOW_HEADER_LINE_HEIGHT (it->w);
18908 max_y = WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w);
18910 if (row->y < min_y)
18911 row->visible_height -= min_y - row->y;
18912 if (row->y + row->height > max_y)
18913 row->visible_height -= row->y + row->height - max_y;
18915 else
18917 row->pixel_width = row->used[TEXT_AREA];
18918 if (row->continued_p)
18919 row->pixel_width -= it->continuation_pixel_width;
18920 else if (row->truncated_on_right_p)
18921 row->pixel_width -= it->truncation_pixel_width;
18922 row->ascent = row->phys_ascent = 0;
18923 row->height = row->phys_height = row->visible_height = 1;
18924 row->extra_line_spacing = 0;
18927 /* Compute a hash code for this row. */
18928 row->hash = row_hash (row);
18930 it->max_ascent = it->max_descent = 0;
18931 it->max_phys_ascent = it->max_phys_descent = 0;
18935 /* Append one space to the glyph row of iterator IT if doing a
18936 window-based redisplay. The space has the same face as
18937 IT->face_id. Value is non-zero if a space was added.
18939 This function is called to make sure that there is always one glyph
18940 at the end of a glyph row that the cursor can be set on under
18941 window-systems. (If there weren't such a glyph we would not know
18942 how wide and tall a box cursor should be displayed).
18944 At the same time this space let's a nicely handle clearing to the
18945 end of the line if the row ends in italic text. */
18947 static int
18948 append_space_for_newline (struct it *it, int default_face_p)
18950 if (FRAME_WINDOW_P (it->f))
18952 int n = it->glyph_row->used[TEXT_AREA];
18954 if (it->glyph_row->glyphs[TEXT_AREA] + n
18955 < it->glyph_row->glyphs[1 + TEXT_AREA])
18957 /* Save some values that must not be changed.
18958 Must save IT->c and IT->len because otherwise
18959 ITERATOR_AT_END_P wouldn't work anymore after
18960 append_space_for_newline has been called. */
18961 enum display_element_type saved_what = it->what;
18962 int saved_c = it->c, saved_len = it->len;
18963 int saved_char_to_display = it->char_to_display;
18964 int saved_x = it->current_x;
18965 int saved_face_id = it->face_id;
18966 int saved_box_end = it->end_of_box_run_p;
18967 struct text_pos saved_pos;
18968 Lisp_Object saved_object;
18969 struct face *face;
18971 saved_object = it->object;
18972 saved_pos = it->position;
18974 it->what = IT_CHARACTER;
18975 memset (&it->position, 0, sizeof it->position);
18976 it->object = make_number (0);
18977 it->c = it->char_to_display = ' ';
18978 it->len = 1;
18980 /* If the default face was remapped, be sure to use the
18981 remapped face for the appended newline. */
18982 if (default_face_p)
18983 it->face_id = lookup_basic_face (it->f, DEFAULT_FACE_ID);
18984 else if (it->face_before_selective_p)
18985 it->face_id = it->saved_face_id;
18986 face = FACE_FROM_ID (it->f, it->face_id);
18987 it->face_id = FACE_FOR_CHAR (it->f, face, 0, -1, Qnil);
18988 /* In R2L rows, we will prepend a stretch glyph that will
18989 have the end_of_box_run_p flag set for it, so there's no
18990 need for the appended newline glyph to have that flag
18991 set. */
18992 if (it->glyph_row->reversed_p
18993 /* But if the appended newline glyph goes all the way to
18994 the end of the row, there will be no stretch glyph,
18995 so leave the box flag set. */
18996 && saved_x + FRAME_COLUMN_WIDTH (it->f) < it->last_visible_x)
18997 it->end_of_box_run_p = 0;
18999 PRODUCE_GLYPHS (it);
19001 it->override_ascent = -1;
19002 it->constrain_row_ascent_descent_p = 0;
19003 it->current_x = saved_x;
19004 it->object = saved_object;
19005 it->position = saved_pos;
19006 it->what = saved_what;
19007 it->face_id = saved_face_id;
19008 it->len = saved_len;
19009 it->c = saved_c;
19010 it->char_to_display = saved_char_to_display;
19011 it->end_of_box_run_p = saved_box_end;
19012 return 1;
19016 return 0;
19020 /* Extend the face of the last glyph in the text area of IT->glyph_row
19021 to the end of the display line. Called from display_line. If the
19022 glyph row is empty, add a space glyph to it so that we know the
19023 face to draw. Set the glyph row flag fill_line_p. If the glyph
19024 row is R2L, prepend a stretch glyph to cover the empty space to the
19025 left of the leftmost glyph. */
19027 static void
19028 extend_face_to_end_of_line (struct it *it)
19030 struct face *face, *default_face;
19031 struct frame *f = it->f;
19033 /* If line is already filled, do nothing. Non window-system frames
19034 get a grace of one more ``pixel'' because their characters are
19035 1-``pixel'' wide, so they hit the equality too early. This grace
19036 is needed only for R2L rows that are not continued, to produce
19037 one extra blank where we could display the cursor. */
19038 if ((it->current_x >= it->last_visible_x
19039 + (!FRAME_WINDOW_P (f)
19040 && it->glyph_row->reversed_p
19041 && !it->glyph_row->continued_p))
19042 /* If the window has display margins, we will need to extend
19043 their face even if the text area is filled. */
19044 && !(WINDOW_LEFT_MARGIN_WIDTH (it->w) > 0
19045 || WINDOW_RIGHT_MARGIN_WIDTH (it->w) > 0))
19046 return;
19048 /* The default face, possibly remapped. */
19049 default_face = FACE_FROM_ID (f, lookup_basic_face (f, DEFAULT_FACE_ID));
19051 /* Face extension extends the background and box of IT->face_id
19052 to the end of the line. If the background equals the background
19053 of the frame, we don't have to do anything. */
19054 if (it->face_before_selective_p)
19055 face = FACE_FROM_ID (f, it->saved_face_id);
19056 else
19057 face = FACE_FROM_ID (f, it->face_id);
19059 if (FRAME_WINDOW_P (f)
19060 && MATRIX_ROW_DISPLAYS_TEXT_P (it->glyph_row)
19061 && face->box == FACE_NO_BOX
19062 && face->background == FRAME_BACKGROUND_PIXEL (f)
19063 #ifdef HAVE_WINDOW_SYSTEM
19064 && !face->stipple
19065 #endif
19066 && !it->glyph_row->reversed_p)
19067 return;
19069 /* Set the glyph row flag indicating that the face of the last glyph
19070 in the text area has to be drawn to the end of the text area. */
19071 it->glyph_row->fill_line_p = 1;
19073 /* If current character of IT is not ASCII, make sure we have the
19074 ASCII face. This will be automatically undone the next time
19075 get_next_display_element returns a multibyte character. Note
19076 that the character will always be single byte in unibyte
19077 text. */
19078 if (!ASCII_CHAR_P (it->c))
19080 it->face_id = FACE_FOR_CHAR (f, face, 0, -1, Qnil);
19083 if (FRAME_WINDOW_P (f))
19085 /* If the row is empty, add a space with the current face of IT,
19086 so that we know which face to draw. */
19087 if (it->glyph_row->used[TEXT_AREA] == 0)
19089 it->glyph_row->glyphs[TEXT_AREA][0] = space_glyph;
19090 it->glyph_row->glyphs[TEXT_AREA][0].face_id = face->id;
19091 it->glyph_row->used[TEXT_AREA] = 1;
19093 /* Mode line and the header line don't have margins, and
19094 likewise the frame's tool-bar window, if there is any. */
19095 if (!(it->glyph_row->mode_line_p
19096 #if defined (HAVE_WINDOW_SYSTEM) && ! defined (USE_GTK) && ! defined (HAVE_NS)
19097 || (WINDOWP (f->tool_bar_window)
19098 && it->w == XWINDOW (f->tool_bar_window))
19099 #endif
19102 if (WINDOW_LEFT_MARGIN_WIDTH (it->w) > 0
19103 && it->glyph_row->used[LEFT_MARGIN_AREA] == 0)
19105 it->glyph_row->glyphs[LEFT_MARGIN_AREA][0] = space_glyph;
19106 it->glyph_row->glyphs[LEFT_MARGIN_AREA][0].face_id =
19107 default_face->id;
19108 it->glyph_row->used[LEFT_MARGIN_AREA] = 1;
19110 if (WINDOW_RIGHT_MARGIN_WIDTH (it->w) > 0
19111 && it->glyph_row->used[RIGHT_MARGIN_AREA] == 0)
19113 it->glyph_row->glyphs[RIGHT_MARGIN_AREA][0] = space_glyph;
19114 it->glyph_row->glyphs[RIGHT_MARGIN_AREA][0].face_id =
19115 default_face->id;
19116 it->glyph_row->used[RIGHT_MARGIN_AREA] = 1;
19119 #ifdef HAVE_WINDOW_SYSTEM
19120 if (it->glyph_row->reversed_p)
19122 /* Prepend a stretch glyph to the row, such that the
19123 rightmost glyph will be drawn flushed all the way to the
19124 right margin of the window. The stretch glyph that will
19125 occupy the empty space, if any, to the left of the
19126 glyphs. */
19127 struct font *font = face->font ? face->font : FRAME_FONT (f);
19128 struct glyph *row_start = it->glyph_row->glyphs[TEXT_AREA];
19129 struct glyph *row_end = row_start + it->glyph_row->used[TEXT_AREA];
19130 struct glyph *g;
19131 int row_width, stretch_ascent, stretch_width;
19132 struct text_pos saved_pos;
19133 int saved_face_id, saved_avoid_cursor, saved_box_start;
19135 for (row_width = 0, g = row_start; g < row_end; g++)
19136 row_width += g->pixel_width;
19137 stretch_width = window_box_width (it->w, TEXT_AREA) - row_width;
19138 if (stretch_width > 0)
19140 stretch_ascent =
19141 (((it->ascent + it->descent)
19142 * FONT_BASE (font)) / FONT_HEIGHT (font));
19143 saved_pos = it->position;
19144 memset (&it->position, 0, sizeof it->position);
19145 saved_avoid_cursor = it->avoid_cursor_p;
19146 it->avoid_cursor_p = 1;
19147 saved_face_id = it->face_id;
19148 saved_box_start = it->start_of_box_run_p;
19149 /* The last row's stretch glyph should get the default
19150 face, to avoid painting the rest of the window with
19151 the region face, if the region ends at ZV. */
19152 if (it->glyph_row->ends_at_zv_p)
19153 it->face_id = default_face->id;
19154 else
19155 it->face_id = face->id;
19156 it->start_of_box_run_p = 0;
19157 append_stretch_glyph (it, make_number (0), stretch_width,
19158 it->ascent + it->descent, stretch_ascent);
19159 it->position = saved_pos;
19160 it->avoid_cursor_p = saved_avoid_cursor;
19161 it->face_id = saved_face_id;
19162 it->start_of_box_run_p = saved_box_start;
19165 #endif /* HAVE_WINDOW_SYSTEM */
19167 else
19169 /* Save some values that must not be changed. */
19170 int saved_x = it->current_x;
19171 struct text_pos saved_pos;
19172 Lisp_Object saved_object;
19173 enum display_element_type saved_what = it->what;
19174 int saved_face_id = it->face_id;
19176 saved_object = it->object;
19177 saved_pos = it->position;
19179 it->what = IT_CHARACTER;
19180 memset (&it->position, 0, sizeof it->position);
19181 it->object = make_number (0);
19182 it->c = it->char_to_display = ' ';
19183 it->len = 1;
19185 if (WINDOW_LEFT_MARGIN_WIDTH (it->w) > 0
19186 && (it->glyph_row->used[LEFT_MARGIN_AREA]
19187 < WINDOW_LEFT_MARGIN_WIDTH (it->w))
19188 && !it->glyph_row->mode_line_p
19189 && default_face->background != FRAME_BACKGROUND_PIXEL (f))
19191 struct glyph *g = it->glyph_row->glyphs[LEFT_MARGIN_AREA];
19192 struct glyph *e = g + it->glyph_row->used[LEFT_MARGIN_AREA];
19194 for (it->current_x = 0; g < e; g++)
19195 it->current_x += g->pixel_width;
19197 it->area = LEFT_MARGIN_AREA;
19198 it->face_id = default_face->id;
19199 while (it->glyph_row->used[LEFT_MARGIN_AREA]
19200 < WINDOW_LEFT_MARGIN_WIDTH (it->w))
19202 PRODUCE_GLYPHS (it);
19203 /* term.c:produce_glyphs advances it->current_x only for
19204 TEXT_AREA. */
19205 it->current_x += it->pixel_width;
19208 it->current_x = saved_x;
19209 it->area = TEXT_AREA;
19212 /* The last row's blank glyphs should get the default face, to
19213 avoid painting the rest of the window with the region face,
19214 if the region ends at ZV. */
19215 if (it->glyph_row->ends_at_zv_p)
19216 it->face_id = default_face->id;
19217 else
19218 it->face_id = face->id;
19219 PRODUCE_GLYPHS (it);
19221 while (it->current_x <= it->last_visible_x)
19222 PRODUCE_GLYPHS (it);
19224 if (WINDOW_RIGHT_MARGIN_WIDTH (it->w) > 0
19225 && (it->glyph_row->used[RIGHT_MARGIN_AREA]
19226 < WINDOW_RIGHT_MARGIN_WIDTH (it->w))
19227 && !it->glyph_row->mode_line_p
19228 && default_face->background != FRAME_BACKGROUND_PIXEL (f))
19230 struct glyph *g = it->glyph_row->glyphs[RIGHT_MARGIN_AREA];
19231 struct glyph *e = g + it->glyph_row->used[RIGHT_MARGIN_AREA];
19233 for ( ; g < e; g++)
19234 it->current_x += g->pixel_width;
19236 it->area = RIGHT_MARGIN_AREA;
19237 it->face_id = default_face->id;
19238 while (it->glyph_row->used[RIGHT_MARGIN_AREA]
19239 < WINDOW_RIGHT_MARGIN_WIDTH (it->w))
19241 PRODUCE_GLYPHS (it);
19242 it->current_x += it->pixel_width;
19245 it->area = TEXT_AREA;
19248 /* Don't count these blanks really. It would let us insert a left
19249 truncation glyph below and make us set the cursor on them, maybe. */
19250 it->current_x = saved_x;
19251 it->object = saved_object;
19252 it->position = saved_pos;
19253 it->what = saved_what;
19254 it->face_id = saved_face_id;
19259 /* Value is non-zero if text starting at CHARPOS in current_buffer is
19260 trailing whitespace. */
19262 static int
19263 trailing_whitespace_p (ptrdiff_t charpos)
19265 ptrdiff_t bytepos = CHAR_TO_BYTE (charpos);
19266 int c = 0;
19268 while (bytepos < ZV_BYTE
19269 && (c = FETCH_CHAR (bytepos),
19270 c == ' ' || c == '\t'))
19271 ++bytepos;
19273 if (bytepos >= ZV_BYTE || c == '\n' || c == '\r')
19275 if (bytepos != PT_BYTE)
19276 return 1;
19278 return 0;
19282 /* Highlight trailing whitespace, if any, in ROW. */
19284 static void
19285 highlight_trailing_whitespace (struct frame *f, struct glyph_row *row)
19287 int used = row->used[TEXT_AREA];
19289 if (used)
19291 struct glyph *start = row->glyphs[TEXT_AREA];
19292 struct glyph *glyph = start + used - 1;
19294 if (row->reversed_p)
19296 /* Right-to-left rows need to be processed in the opposite
19297 direction, so swap the edge pointers. */
19298 glyph = start;
19299 start = row->glyphs[TEXT_AREA] + used - 1;
19302 /* Skip over glyphs inserted to display the cursor at the
19303 end of a line, for extending the face of the last glyph
19304 to the end of the line on terminals, and for truncation
19305 and continuation glyphs. */
19306 if (!row->reversed_p)
19308 while (glyph >= start
19309 && glyph->type == CHAR_GLYPH
19310 && INTEGERP (glyph->object))
19311 --glyph;
19313 else
19315 while (glyph <= start
19316 && glyph->type == CHAR_GLYPH
19317 && INTEGERP (glyph->object))
19318 ++glyph;
19321 /* If last glyph is a space or stretch, and it's trailing
19322 whitespace, set the face of all trailing whitespace glyphs in
19323 IT->glyph_row to `trailing-whitespace'. */
19324 if ((row->reversed_p ? glyph <= start : glyph >= start)
19325 && BUFFERP (glyph->object)
19326 && (glyph->type == STRETCH_GLYPH
19327 || (glyph->type == CHAR_GLYPH
19328 && glyph->u.ch == ' '))
19329 && trailing_whitespace_p (glyph->charpos))
19331 int face_id = lookup_named_face (f, Qtrailing_whitespace, 0);
19332 if (face_id < 0)
19333 return;
19335 if (!row->reversed_p)
19337 while (glyph >= start
19338 && BUFFERP (glyph->object)
19339 && (glyph->type == STRETCH_GLYPH
19340 || (glyph->type == CHAR_GLYPH
19341 && glyph->u.ch == ' ')))
19342 (glyph--)->face_id = face_id;
19344 else
19346 while (glyph <= start
19347 && BUFFERP (glyph->object)
19348 && (glyph->type == STRETCH_GLYPH
19349 || (glyph->type == CHAR_GLYPH
19350 && glyph->u.ch == ' ')))
19351 (glyph++)->face_id = face_id;
19358 /* Value is non-zero if glyph row ROW should be
19359 considered to hold the buffer position CHARPOS. */
19361 static int
19362 row_for_charpos_p (struct glyph_row *row, ptrdiff_t charpos)
19364 int result = 1;
19366 if (charpos == CHARPOS (row->end.pos)
19367 || charpos == MATRIX_ROW_END_CHARPOS (row))
19369 /* Suppose the row ends on a string.
19370 Unless the row is continued, that means it ends on a newline
19371 in the string. If it's anything other than a display string
19372 (e.g., a before-string from an overlay), we don't want the
19373 cursor there. (This heuristic seems to give the optimal
19374 behavior for the various types of multi-line strings.)
19375 One exception: if the string has `cursor' property on one of
19376 its characters, we _do_ want the cursor there. */
19377 if (CHARPOS (row->end.string_pos) >= 0)
19379 if (row->continued_p)
19380 result = 1;
19381 else
19383 /* Check for `display' property. */
19384 struct glyph *beg = row->glyphs[TEXT_AREA];
19385 struct glyph *end = beg + row->used[TEXT_AREA] - 1;
19386 struct glyph *glyph;
19388 result = 0;
19389 for (glyph = end; glyph >= beg; --glyph)
19390 if (STRINGP (glyph->object))
19392 Lisp_Object prop
19393 = Fget_char_property (make_number (charpos),
19394 Qdisplay, Qnil);
19395 result =
19396 (!NILP (prop)
19397 && display_prop_string_p (prop, glyph->object));
19398 /* If there's a `cursor' property on one of the
19399 string's characters, this row is a cursor row,
19400 even though this is not a display string. */
19401 if (!result)
19403 Lisp_Object s = glyph->object;
19405 for ( ; glyph >= beg && EQ (glyph->object, s); --glyph)
19407 ptrdiff_t gpos = glyph->charpos;
19409 if (!NILP (Fget_char_property (make_number (gpos),
19410 Qcursor, s)))
19412 result = 1;
19413 break;
19417 break;
19421 else if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
19423 /* If the row ends in middle of a real character,
19424 and the line is continued, we want the cursor here.
19425 That's because CHARPOS (ROW->end.pos) would equal
19426 PT if PT is before the character. */
19427 if (!row->ends_in_ellipsis_p)
19428 result = row->continued_p;
19429 else
19430 /* If the row ends in an ellipsis, then
19431 CHARPOS (ROW->end.pos) will equal point after the
19432 invisible text. We want that position to be displayed
19433 after the ellipsis. */
19434 result = 0;
19436 /* If the row ends at ZV, display the cursor at the end of that
19437 row instead of at the start of the row below. */
19438 else if (row->ends_at_zv_p)
19439 result = 1;
19440 else
19441 result = 0;
19444 return result;
19447 /* Value is non-zero if glyph row ROW should be
19448 used to hold the cursor. */
19450 static int
19451 cursor_row_p (struct glyph_row *row)
19453 return row_for_charpos_p (row, PT);
19458 /* Push the property PROP so that it will be rendered at the current
19459 position in IT. Return 1 if PROP was successfully pushed, 0
19460 otherwise. Called from handle_line_prefix to handle the
19461 `line-prefix' and `wrap-prefix' properties. */
19463 static int
19464 push_prefix_prop (struct it *it, Lisp_Object prop)
19466 struct text_pos pos =
19467 STRINGP (it->string) ? it->current.string_pos : it->current.pos;
19469 eassert (it->method == GET_FROM_BUFFER
19470 || it->method == GET_FROM_DISPLAY_VECTOR
19471 || it->method == GET_FROM_STRING);
19473 /* We need to save the current buffer/string position, so it will be
19474 restored by pop_it, because iterate_out_of_display_property
19475 depends on that being set correctly, but some situations leave
19476 it->position not yet set when this function is called. */
19477 push_it (it, &pos);
19479 if (STRINGP (prop))
19481 if (SCHARS (prop) == 0)
19483 pop_it (it);
19484 return 0;
19487 it->string = prop;
19488 it->string_from_prefix_prop_p = 1;
19489 it->multibyte_p = STRING_MULTIBYTE (it->string);
19490 it->current.overlay_string_index = -1;
19491 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
19492 it->end_charpos = it->string_nchars = SCHARS (it->string);
19493 it->method = GET_FROM_STRING;
19494 it->stop_charpos = 0;
19495 it->prev_stop = 0;
19496 it->base_level_stop = 0;
19498 /* Force paragraph direction to be that of the parent
19499 buffer/string. */
19500 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
19501 it->paragraph_embedding = it->bidi_it.paragraph_dir;
19502 else
19503 it->paragraph_embedding = L2R;
19505 /* Set up the bidi iterator for this display string. */
19506 if (it->bidi_p)
19508 it->bidi_it.string.lstring = it->string;
19509 it->bidi_it.string.s = NULL;
19510 it->bidi_it.string.schars = it->end_charpos;
19511 it->bidi_it.string.bufpos = IT_CHARPOS (*it);
19512 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
19513 it->bidi_it.string.unibyte = !it->multibyte_p;
19514 it->bidi_it.w = it->w;
19515 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
19518 else if (CONSP (prop) && EQ (XCAR (prop), Qspace))
19520 it->method = GET_FROM_STRETCH;
19521 it->object = prop;
19523 #ifdef HAVE_WINDOW_SYSTEM
19524 else if (IMAGEP (prop))
19526 it->what = IT_IMAGE;
19527 it->image_id = lookup_image (it->f, prop);
19528 it->method = GET_FROM_IMAGE;
19530 #endif /* HAVE_WINDOW_SYSTEM */
19531 else
19533 pop_it (it); /* bogus display property, give up */
19534 return 0;
19537 return 1;
19540 /* Return the character-property PROP at the current position in IT. */
19542 static Lisp_Object
19543 get_it_property (struct it *it, Lisp_Object prop)
19545 Lisp_Object position, object = it->object;
19547 if (STRINGP (object))
19548 position = make_number (IT_STRING_CHARPOS (*it));
19549 else if (BUFFERP (object))
19551 position = make_number (IT_CHARPOS (*it));
19552 object = it->window;
19554 else
19555 return Qnil;
19557 return Fget_char_property (position, prop, object);
19560 /* See if there's a line- or wrap-prefix, and if so, push it on IT. */
19562 static void
19563 handle_line_prefix (struct it *it)
19565 Lisp_Object prefix;
19567 if (it->continuation_lines_width > 0)
19569 prefix = get_it_property (it, Qwrap_prefix);
19570 if (NILP (prefix))
19571 prefix = Vwrap_prefix;
19573 else
19575 prefix = get_it_property (it, Qline_prefix);
19576 if (NILP (prefix))
19577 prefix = Vline_prefix;
19579 if (! NILP (prefix) && push_prefix_prop (it, prefix))
19581 /* If the prefix is wider than the window, and we try to wrap
19582 it, it would acquire its own wrap prefix, and so on till the
19583 iterator stack overflows. So, don't wrap the prefix. */
19584 it->line_wrap = TRUNCATE;
19585 it->avoid_cursor_p = 1;
19591 /* Remove N glyphs at the start of a reversed IT->glyph_row. Called
19592 only for R2L lines from display_line and display_string, when they
19593 decide that too many glyphs were produced by PRODUCE_GLYPHS, and
19594 the line/string needs to be continued on the next glyph row. */
19595 static void
19596 unproduce_glyphs (struct it *it, int n)
19598 struct glyph *glyph, *end;
19600 eassert (it->glyph_row);
19601 eassert (it->glyph_row->reversed_p);
19602 eassert (it->area == TEXT_AREA);
19603 eassert (n <= it->glyph_row->used[TEXT_AREA]);
19605 if (n > it->glyph_row->used[TEXT_AREA])
19606 n = it->glyph_row->used[TEXT_AREA];
19607 glyph = it->glyph_row->glyphs[TEXT_AREA] + n;
19608 end = it->glyph_row->glyphs[TEXT_AREA] + it->glyph_row->used[TEXT_AREA];
19609 for ( ; glyph < end; glyph++)
19610 glyph[-n] = *glyph;
19613 /* Find the positions in a bidi-reordered ROW to serve as ROW->minpos
19614 and ROW->maxpos. */
19615 static void
19616 find_row_edges (struct it *it, struct glyph_row *row,
19617 ptrdiff_t min_pos, ptrdiff_t min_bpos,
19618 ptrdiff_t max_pos, ptrdiff_t max_bpos)
19620 /* FIXME: Revisit this when glyph ``spilling'' in continuation
19621 lines' rows is implemented for bidi-reordered rows. */
19623 /* ROW->minpos is the value of min_pos, the minimal buffer position
19624 we have in ROW, or ROW->start.pos if that is smaller. */
19625 if (min_pos <= ZV && min_pos < row->start.pos.charpos)
19626 SET_TEXT_POS (row->minpos, min_pos, min_bpos);
19627 else
19628 /* We didn't find buffer positions smaller than ROW->start, or
19629 didn't find _any_ valid buffer positions in any of the glyphs,
19630 so we must trust the iterator's computed positions. */
19631 row->minpos = row->start.pos;
19632 if (max_pos <= 0)
19634 max_pos = CHARPOS (it->current.pos);
19635 max_bpos = BYTEPOS (it->current.pos);
19638 /* Here are the various use-cases for ending the row, and the
19639 corresponding values for ROW->maxpos:
19641 Line ends in a newline from buffer eol_pos + 1
19642 Line is continued from buffer max_pos + 1
19643 Line is truncated on right it->current.pos
19644 Line ends in a newline from string max_pos + 1(*)
19645 (*) + 1 only when line ends in a forward scan
19646 Line is continued from string max_pos
19647 Line is continued from display vector max_pos
19648 Line is entirely from a string min_pos == max_pos
19649 Line is entirely from a display vector min_pos == max_pos
19650 Line that ends at ZV ZV
19652 If you discover other use-cases, please add them here as
19653 appropriate. */
19654 if (row->ends_at_zv_p)
19655 row->maxpos = it->current.pos;
19656 else if (row->used[TEXT_AREA])
19658 int seen_this_string = 0;
19659 struct glyph_row *r1 = row - 1;
19661 /* Did we see the same display string on the previous row? */
19662 if (STRINGP (it->object)
19663 /* this is not the first row */
19664 && row > it->w->desired_matrix->rows
19665 /* previous row is not the header line */
19666 && !r1->mode_line_p
19667 /* previous row also ends in a newline from a string */
19668 && r1->ends_in_newline_from_string_p)
19670 struct glyph *start, *end;
19672 /* Search for the last glyph of the previous row that came
19673 from buffer or string. Depending on whether the row is
19674 L2R or R2L, we need to process it front to back or the
19675 other way round. */
19676 if (!r1->reversed_p)
19678 start = r1->glyphs[TEXT_AREA];
19679 end = start + r1->used[TEXT_AREA];
19680 /* Glyphs inserted by redisplay have an integer (zero)
19681 as their object. */
19682 while (end > start
19683 && INTEGERP ((end - 1)->object)
19684 && (end - 1)->charpos <= 0)
19685 --end;
19686 if (end > start)
19688 if (EQ ((end - 1)->object, it->object))
19689 seen_this_string = 1;
19691 else
19692 /* If all the glyphs of the previous row were inserted
19693 by redisplay, it means the previous row was
19694 produced from a single newline, which is only
19695 possible if that newline came from the same string
19696 as the one which produced this ROW. */
19697 seen_this_string = 1;
19699 else
19701 end = r1->glyphs[TEXT_AREA] - 1;
19702 start = end + r1->used[TEXT_AREA];
19703 while (end < start
19704 && INTEGERP ((end + 1)->object)
19705 && (end + 1)->charpos <= 0)
19706 ++end;
19707 if (end < start)
19709 if (EQ ((end + 1)->object, it->object))
19710 seen_this_string = 1;
19712 else
19713 seen_this_string = 1;
19716 /* Take note of each display string that covers a newline only
19717 once, the first time we see it. This is for when a display
19718 string includes more than one newline in it. */
19719 if (row->ends_in_newline_from_string_p && !seen_this_string)
19721 /* If we were scanning the buffer forward when we displayed
19722 the string, we want to account for at least one buffer
19723 position that belongs to this row (position covered by
19724 the display string), so that cursor positioning will
19725 consider this row as a candidate when point is at the end
19726 of the visual line represented by this row. This is not
19727 required when scanning back, because max_pos will already
19728 have a much larger value. */
19729 if (CHARPOS (row->end.pos) > max_pos)
19730 INC_BOTH (max_pos, max_bpos);
19731 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
19733 else if (CHARPOS (it->eol_pos) > 0)
19734 SET_TEXT_POS (row->maxpos,
19735 CHARPOS (it->eol_pos) + 1, BYTEPOS (it->eol_pos) + 1);
19736 else if (row->continued_p)
19738 /* If max_pos is different from IT's current position, it
19739 means IT->method does not belong to the display element
19740 at max_pos. However, it also means that the display
19741 element at max_pos was displayed in its entirety on this
19742 line, which is equivalent to saying that the next line
19743 starts at the next buffer position. */
19744 if (IT_CHARPOS (*it) == max_pos && it->method != GET_FROM_BUFFER)
19745 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
19746 else
19748 INC_BOTH (max_pos, max_bpos);
19749 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
19752 else if (row->truncated_on_right_p)
19753 /* display_line already called reseat_at_next_visible_line_start,
19754 which puts the iterator at the beginning of the next line, in
19755 the logical order. */
19756 row->maxpos = it->current.pos;
19757 else if (max_pos == min_pos && it->method != GET_FROM_BUFFER)
19758 /* A line that is entirely from a string/image/stretch... */
19759 row->maxpos = row->minpos;
19760 else
19761 emacs_abort ();
19763 else
19764 row->maxpos = it->current.pos;
19767 /* Construct the glyph row IT->glyph_row in the desired matrix of
19768 IT->w from text at the current position of IT. See dispextern.h
19769 for an overview of struct it. Value is non-zero if
19770 IT->glyph_row displays text, as opposed to a line displaying ZV
19771 only. */
19773 static int
19774 display_line (struct it *it)
19776 struct glyph_row *row = it->glyph_row;
19777 Lisp_Object overlay_arrow_string;
19778 struct it wrap_it;
19779 void *wrap_data = NULL;
19780 int may_wrap = 0, wrap_x IF_LINT (= 0);
19781 int wrap_row_used = -1;
19782 int wrap_row_ascent IF_LINT (= 0), wrap_row_height IF_LINT (= 0);
19783 int wrap_row_phys_ascent IF_LINT (= 0), wrap_row_phys_height IF_LINT (= 0);
19784 int wrap_row_extra_line_spacing IF_LINT (= 0);
19785 ptrdiff_t wrap_row_min_pos IF_LINT (= 0), wrap_row_min_bpos IF_LINT (= 0);
19786 ptrdiff_t wrap_row_max_pos IF_LINT (= 0), wrap_row_max_bpos IF_LINT (= 0);
19787 int cvpos;
19788 ptrdiff_t min_pos = ZV + 1, max_pos = 0;
19789 ptrdiff_t min_bpos IF_LINT (= 0), max_bpos IF_LINT (= 0);
19791 /* We always start displaying at hpos zero even if hscrolled. */
19792 eassert (it->hpos == 0 && it->current_x == 0);
19794 if (MATRIX_ROW_VPOS (row, it->w->desired_matrix)
19795 >= it->w->desired_matrix->nrows)
19797 it->w->nrows_scale_factor++;
19798 it->f->fonts_changed = 1;
19799 return 0;
19802 /* Clear the result glyph row and enable it. */
19803 prepare_desired_row (row);
19805 row->y = it->current_y;
19806 row->start = it->start;
19807 row->continuation_lines_width = it->continuation_lines_width;
19808 row->displays_text_p = 1;
19809 row->starts_in_middle_of_char_p = it->starts_in_middle_of_char_p;
19810 it->starts_in_middle_of_char_p = 0;
19812 /* Arrange the overlays nicely for our purposes. Usually, we call
19813 display_line on only one line at a time, in which case this
19814 can't really hurt too much, or we call it on lines which appear
19815 one after another in the buffer, in which case all calls to
19816 recenter_overlay_lists but the first will be pretty cheap. */
19817 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
19819 /* Move over display elements that are not visible because we are
19820 hscrolled. This may stop at an x-position < IT->first_visible_x
19821 if the first glyph is partially visible or if we hit a line end. */
19822 if (it->current_x < it->first_visible_x)
19824 enum move_it_result move_result;
19826 this_line_min_pos = row->start.pos;
19827 move_result = move_it_in_display_line_to (it, ZV, it->first_visible_x,
19828 MOVE_TO_POS | MOVE_TO_X);
19829 /* If we are under a large hscroll, move_it_in_display_line_to
19830 could hit the end of the line without reaching
19831 it->first_visible_x. Pretend that we did reach it. This is
19832 especially important on a TTY, where we will call
19833 extend_face_to_end_of_line, which needs to know how many
19834 blank glyphs to produce. */
19835 if (it->current_x < it->first_visible_x
19836 && (move_result == MOVE_NEWLINE_OR_CR
19837 || move_result == MOVE_POS_MATCH_OR_ZV))
19838 it->current_x = it->first_visible_x;
19840 /* Record the smallest positions seen while we moved over
19841 display elements that are not visible. This is needed by
19842 redisplay_internal for optimizing the case where the cursor
19843 stays inside the same line. The rest of this function only
19844 considers positions that are actually displayed, so
19845 RECORD_MAX_MIN_POS will not otherwise record positions that
19846 are hscrolled to the left of the left edge of the window. */
19847 min_pos = CHARPOS (this_line_min_pos);
19848 min_bpos = BYTEPOS (this_line_min_pos);
19850 else
19852 /* We only do this when not calling `move_it_in_display_line_to'
19853 above, because move_it_in_display_line_to calls
19854 handle_line_prefix itself. */
19855 handle_line_prefix (it);
19858 /* Get the initial row height. This is either the height of the
19859 text hscrolled, if there is any, or zero. */
19860 row->ascent = it->max_ascent;
19861 row->height = it->max_ascent + it->max_descent;
19862 row->phys_ascent = it->max_phys_ascent;
19863 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
19864 row->extra_line_spacing = it->max_extra_line_spacing;
19866 /* Utility macro to record max and min buffer positions seen until now. */
19867 #define RECORD_MAX_MIN_POS(IT) \
19868 do \
19870 int composition_p = !STRINGP ((IT)->string) \
19871 && ((IT)->what == IT_COMPOSITION); \
19872 ptrdiff_t current_pos = \
19873 composition_p ? (IT)->cmp_it.charpos \
19874 : IT_CHARPOS (*(IT)); \
19875 ptrdiff_t current_bpos = \
19876 composition_p ? CHAR_TO_BYTE (current_pos) \
19877 : IT_BYTEPOS (*(IT)); \
19878 if (current_pos < min_pos) \
19880 min_pos = current_pos; \
19881 min_bpos = current_bpos; \
19883 if (IT_CHARPOS (*it) > max_pos) \
19885 max_pos = IT_CHARPOS (*it); \
19886 max_bpos = IT_BYTEPOS (*it); \
19889 while (0)
19891 /* Loop generating characters. The loop is left with IT on the next
19892 character to display. */
19893 while (1)
19895 int n_glyphs_before, hpos_before, x_before;
19896 int x, nglyphs;
19897 int ascent = 0, descent = 0, phys_ascent = 0, phys_descent = 0;
19899 /* Retrieve the next thing to display. Value is zero if end of
19900 buffer reached. */
19901 if (!get_next_display_element (it))
19903 /* Maybe add a space at the end of this line that is used to
19904 display the cursor there under X. Set the charpos of the
19905 first glyph of blank lines not corresponding to any text
19906 to -1. */
19907 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
19908 row->exact_window_width_line_p = 1;
19909 else if ((append_space_for_newline (it, 1) && row->used[TEXT_AREA] == 1)
19910 || row->used[TEXT_AREA] == 0)
19912 row->glyphs[TEXT_AREA]->charpos = -1;
19913 row->displays_text_p = 0;
19915 if (!NILP (BVAR (XBUFFER (it->w->contents), indicate_empty_lines))
19916 && (!MINI_WINDOW_P (it->w)
19917 || (minibuf_level && EQ (it->window, minibuf_window))))
19918 row->indicate_empty_line_p = 1;
19921 it->continuation_lines_width = 0;
19922 row->ends_at_zv_p = 1;
19923 /* A row that displays right-to-left text must always have
19924 its last face extended all the way to the end of line,
19925 even if this row ends in ZV, because we still write to
19926 the screen left to right. We also need to extend the
19927 last face if the default face is remapped to some
19928 different face, otherwise the functions that clear
19929 portions of the screen will clear with the default face's
19930 background color. */
19931 if (row->reversed_p
19932 || lookup_basic_face (it->f, DEFAULT_FACE_ID) != DEFAULT_FACE_ID)
19933 extend_face_to_end_of_line (it);
19934 break;
19937 /* Now, get the metrics of what we want to display. This also
19938 generates glyphs in `row' (which is IT->glyph_row). */
19939 n_glyphs_before = row->used[TEXT_AREA];
19940 x = it->current_x;
19942 /* Remember the line height so far in case the next element doesn't
19943 fit on the line. */
19944 if (it->line_wrap != TRUNCATE)
19946 ascent = it->max_ascent;
19947 descent = it->max_descent;
19948 phys_ascent = it->max_phys_ascent;
19949 phys_descent = it->max_phys_descent;
19951 if (it->line_wrap == WORD_WRAP && it->area == TEXT_AREA)
19953 if (IT_DISPLAYING_WHITESPACE (it))
19954 may_wrap = 1;
19955 else if (may_wrap)
19957 SAVE_IT (wrap_it, *it, wrap_data);
19958 wrap_x = x;
19959 wrap_row_used = row->used[TEXT_AREA];
19960 wrap_row_ascent = row->ascent;
19961 wrap_row_height = row->height;
19962 wrap_row_phys_ascent = row->phys_ascent;
19963 wrap_row_phys_height = row->phys_height;
19964 wrap_row_extra_line_spacing = row->extra_line_spacing;
19965 wrap_row_min_pos = min_pos;
19966 wrap_row_min_bpos = min_bpos;
19967 wrap_row_max_pos = max_pos;
19968 wrap_row_max_bpos = max_bpos;
19969 may_wrap = 0;
19974 PRODUCE_GLYPHS (it);
19976 /* If this display element was in marginal areas, continue with
19977 the next one. */
19978 if (it->area != TEXT_AREA)
19980 row->ascent = max (row->ascent, it->max_ascent);
19981 row->height = max (row->height, it->max_ascent + it->max_descent);
19982 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
19983 row->phys_height = max (row->phys_height,
19984 it->max_phys_ascent + it->max_phys_descent);
19985 row->extra_line_spacing = max (row->extra_line_spacing,
19986 it->max_extra_line_spacing);
19987 set_iterator_to_next (it, 1);
19988 continue;
19991 /* Does the display element fit on the line? If we truncate
19992 lines, we should draw past the right edge of the window. If
19993 we don't truncate, we want to stop so that we can display the
19994 continuation glyph before the right margin. If lines are
19995 continued, there are two possible strategies for characters
19996 resulting in more than 1 glyph (e.g. tabs): Display as many
19997 glyphs as possible in this line and leave the rest for the
19998 continuation line, or display the whole element in the next
19999 line. Original redisplay did the former, so we do it also. */
20000 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
20001 hpos_before = it->hpos;
20002 x_before = x;
20004 if (/* Not a newline. */
20005 nglyphs > 0
20006 /* Glyphs produced fit entirely in the line. */
20007 && it->current_x < it->last_visible_x)
20009 it->hpos += nglyphs;
20010 row->ascent = max (row->ascent, it->max_ascent);
20011 row->height = max (row->height, it->max_ascent + it->max_descent);
20012 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
20013 row->phys_height = max (row->phys_height,
20014 it->max_phys_ascent + it->max_phys_descent);
20015 row->extra_line_spacing = max (row->extra_line_spacing,
20016 it->max_extra_line_spacing);
20017 if (it->current_x - it->pixel_width < it->first_visible_x)
20018 row->x = x - it->first_visible_x;
20019 /* Record the maximum and minimum buffer positions seen so
20020 far in glyphs that will be displayed by this row. */
20021 if (it->bidi_p)
20022 RECORD_MAX_MIN_POS (it);
20024 else
20026 int i, new_x;
20027 struct glyph *glyph;
20029 for (i = 0; i < nglyphs; ++i, x = new_x)
20031 glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
20032 new_x = x + glyph->pixel_width;
20034 if (/* Lines are continued. */
20035 it->line_wrap != TRUNCATE
20036 && (/* Glyph doesn't fit on the line. */
20037 new_x > it->last_visible_x
20038 /* Or it fits exactly on a window system frame. */
20039 || (new_x == it->last_visible_x
20040 && FRAME_WINDOW_P (it->f)
20041 && (row->reversed_p
20042 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
20043 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)))))
20045 /* End of a continued line. */
20047 if (it->hpos == 0
20048 || (new_x == it->last_visible_x
20049 && FRAME_WINDOW_P (it->f)
20050 && (row->reversed_p
20051 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
20052 : WINDOW_RIGHT_FRINGE_WIDTH (it->w))))
20054 /* Current glyph is the only one on the line or
20055 fits exactly on the line. We must continue
20056 the line because we can't draw the cursor
20057 after the glyph. */
20058 row->continued_p = 1;
20059 it->current_x = new_x;
20060 it->continuation_lines_width += new_x;
20061 ++it->hpos;
20062 if (i == nglyphs - 1)
20064 /* If line-wrap is on, check if a previous
20065 wrap point was found. */
20066 if (wrap_row_used > 0
20067 /* Even if there is a previous wrap
20068 point, continue the line here as
20069 usual, if (i) the previous character
20070 was a space or tab AND (ii) the
20071 current character is not. */
20072 && (!may_wrap
20073 || IT_DISPLAYING_WHITESPACE (it)))
20074 goto back_to_wrap;
20076 /* Record the maximum and minimum buffer
20077 positions seen so far in glyphs that will be
20078 displayed by this row. */
20079 if (it->bidi_p)
20080 RECORD_MAX_MIN_POS (it);
20081 set_iterator_to_next (it, 1);
20082 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
20084 if (!get_next_display_element (it))
20086 row->exact_window_width_line_p = 1;
20087 it->continuation_lines_width = 0;
20088 row->continued_p = 0;
20089 row->ends_at_zv_p = 1;
20091 else if (ITERATOR_AT_END_OF_LINE_P (it))
20093 row->continued_p = 0;
20094 row->exact_window_width_line_p = 1;
20098 else if (it->bidi_p)
20099 RECORD_MAX_MIN_POS (it);
20100 if (WINDOW_LEFT_MARGIN_WIDTH (it->w) > 0
20101 || WINDOW_RIGHT_MARGIN_WIDTH (it->w) > 0)
20102 extend_face_to_end_of_line (it);
20104 else if (CHAR_GLYPH_PADDING_P (*glyph)
20105 && !FRAME_WINDOW_P (it->f))
20107 /* A padding glyph that doesn't fit on this line.
20108 This means the whole character doesn't fit
20109 on the line. */
20110 if (row->reversed_p)
20111 unproduce_glyphs (it, row->used[TEXT_AREA]
20112 - n_glyphs_before);
20113 row->used[TEXT_AREA] = n_glyphs_before;
20115 /* Fill the rest of the row with continuation
20116 glyphs like in 20.x. */
20117 while (row->glyphs[TEXT_AREA] + row->used[TEXT_AREA]
20118 < row->glyphs[1 + TEXT_AREA])
20119 produce_special_glyphs (it, IT_CONTINUATION);
20121 row->continued_p = 1;
20122 it->current_x = x_before;
20123 it->continuation_lines_width += x_before;
20125 /* Restore the height to what it was before the
20126 element not fitting on the line. */
20127 it->max_ascent = ascent;
20128 it->max_descent = descent;
20129 it->max_phys_ascent = phys_ascent;
20130 it->max_phys_descent = phys_descent;
20131 if (WINDOW_LEFT_MARGIN_WIDTH (it->w) > 0
20132 || WINDOW_RIGHT_MARGIN_WIDTH (it->w) > 0)
20133 extend_face_to_end_of_line (it);
20135 else if (wrap_row_used > 0)
20137 back_to_wrap:
20138 if (row->reversed_p)
20139 unproduce_glyphs (it,
20140 row->used[TEXT_AREA] - wrap_row_used);
20141 RESTORE_IT (it, &wrap_it, wrap_data);
20142 it->continuation_lines_width += wrap_x;
20143 row->used[TEXT_AREA] = wrap_row_used;
20144 row->ascent = wrap_row_ascent;
20145 row->height = wrap_row_height;
20146 row->phys_ascent = wrap_row_phys_ascent;
20147 row->phys_height = wrap_row_phys_height;
20148 row->extra_line_spacing = wrap_row_extra_line_spacing;
20149 min_pos = wrap_row_min_pos;
20150 min_bpos = wrap_row_min_bpos;
20151 max_pos = wrap_row_max_pos;
20152 max_bpos = wrap_row_max_bpos;
20153 row->continued_p = 1;
20154 row->ends_at_zv_p = 0;
20155 row->exact_window_width_line_p = 0;
20156 it->continuation_lines_width += x;
20158 /* Make sure that a non-default face is extended
20159 up to the right margin of the window. */
20160 extend_face_to_end_of_line (it);
20162 else if (it->c == '\t' && FRAME_WINDOW_P (it->f))
20164 /* A TAB that extends past the right edge of the
20165 window. This produces a single glyph on
20166 window system frames. We leave the glyph in
20167 this row and let it fill the row, but don't
20168 consume the TAB. */
20169 if ((row->reversed_p
20170 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
20171 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
20172 produce_special_glyphs (it, IT_CONTINUATION);
20173 it->continuation_lines_width += it->last_visible_x;
20174 row->ends_in_middle_of_char_p = 1;
20175 row->continued_p = 1;
20176 glyph->pixel_width = it->last_visible_x - x;
20177 it->starts_in_middle_of_char_p = 1;
20178 if (WINDOW_LEFT_MARGIN_WIDTH (it->w) > 0
20179 || WINDOW_RIGHT_MARGIN_WIDTH (it->w) > 0)
20180 extend_face_to_end_of_line (it);
20182 else
20184 /* Something other than a TAB that draws past
20185 the right edge of the window. Restore
20186 positions to values before the element. */
20187 if (row->reversed_p)
20188 unproduce_glyphs (it, row->used[TEXT_AREA]
20189 - (n_glyphs_before + i));
20190 row->used[TEXT_AREA] = n_glyphs_before + i;
20192 /* Display continuation glyphs. */
20193 it->current_x = x_before;
20194 it->continuation_lines_width += x;
20195 if (!FRAME_WINDOW_P (it->f)
20196 || (row->reversed_p
20197 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
20198 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
20199 produce_special_glyphs (it, IT_CONTINUATION);
20200 row->continued_p = 1;
20202 extend_face_to_end_of_line (it);
20204 if (nglyphs > 1 && i > 0)
20206 row->ends_in_middle_of_char_p = 1;
20207 it->starts_in_middle_of_char_p = 1;
20210 /* Restore the height to what it was before the
20211 element not fitting on the line. */
20212 it->max_ascent = ascent;
20213 it->max_descent = descent;
20214 it->max_phys_ascent = phys_ascent;
20215 it->max_phys_descent = phys_descent;
20218 break;
20220 else if (new_x > it->first_visible_x)
20222 /* Increment number of glyphs actually displayed. */
20223 ++it->hpos;
20225 /* Record the maximum and minimum buffer positions
20226 seen so far in glyphs that will be displayed by
20227 this row. */
20228 if (it->bidi_p)
20229 RECORD_MAX_MIN_POS (it);
20231 if (x < it->first_visible_x)
20232 /* Glyph is partially visible, i.e. row starts at
20233 negative X position. */
20234 row->x = x - it->first_visible_x;
20236 else
20238 /* Glyph is completely off the left margin of the
20239 window. This should not happen because of the
20240 move_it_in_display_line at the start of this
20241 function, unless the text display area of the
20242 window is empty. */
20243 eassert (it->first_visible_x <= it->last_visible_x);
20246 /* Even if this display element produced no glyphs at all,
20247 we want to record its position. */
20248 if (it->bidi_p && nglyphs == 0)
20249 RECORD_MAX_MIN_POS (it);
20251 row->ascent = max (row->ascent, it->max_ascent);
20252 row->height = max (row->height, it->max_ascent + it->max_descent);
20253 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
20254 row->phys_height = max (row->phys_height,
20255 it->max_phys_ascent + it->max_phys_descent);
20256 row->extra_line_spacing = max (row->extra_line_spacing,
20257 it->max_extra_line_spacing);
20259 /* End of this display line if row is continued. */
20260 if (row->continued_p || row->ends_at_zv_p)
20261 break;
20264 at_end_of_line:
20265 /* Is this a line end? If yes, we're also done, after making
20266 sure that a non-default face is extended up to the right
20267 margin of the window. */
20268 if (ITERATOR_AT_END_OF_LINE_P (it))
20270 int used_before = row->used[TEXT_AREA];
20272 row->ends_in_newline_from_string_p = STRINGP (it->object);
20274 /* Add a space at the end of the line that is used to
20275 display the cursor there. */
20276 if (!IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
20277 append_space_for_newline (it, 0);
20279 /* Extend the face to the end of the line. */
20280 extend_face_to_end_of_line (it);
20282 /* Make sure we have the position. */
20283 if (used_before == 0)
20284 row->glyphs[TEXT_AREA]->charpos = CHARPOS (it->position);
20286 /* Record the position of the newline, for use in
20287 find_row_edges. */
20288 it->eol_pos = it->current.pos;
20290 /* Consume the line end. This skips over invisible lines. */
20291 set_iterator_to_next (it, 1);
20292 it->continuation_lines_width = 0;
20293 break;
20296 /* Proceed with next display element. Note that this skips
20297 over lines invisible because of selective display. */
20298 set_iterator_to_next (it, 1);
20300 /* If we truncate lines, we are done when the last displayed
20301 glyphs reach past the right margin of the window. */
20302 if (it->line_wrap == TRUNCATE
20303 && ((FRAME_WINDOW_P (it->f)
20304 /* Images are preprocessed in produce_image_glyph such
20305 that they are cropped at the right edge of the
20306 window, so an image glyph will always end exactly at
20307 last_visible_x, even if there's no right fringe. */
20308 && (WINDOW_RIGHT_FRINGE_WIDTH (it->w) || it->what == IT_IMAGE))
20309 ? (it->current_x >= it->last_visible_x)
20310 : (it->current_x > it->last_visible_x)))
20312 /* Maybe add truncation glyphs. */
20313 if (!FRAME_WINDOW_P (it->f)
20314 || (row->reversed_p
20315 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
20316 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
20318 int i, n;
20320 if (!row->reversed_p)
20322 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
20323 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
20324 break;
20326 else
20328 for (i = 0; i < row->used[TEXT_AREA]; i++)
20329 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
20330 break;
20331 /* Remove any padding glyphs at the front of ROW, to
20332 make room for the truncation glyphs we will be
20333 adding below. The loop below always inserts at
20334 least one truncation glyph, so also remove the
20335 last glyph added to ROW. */
20336 unproduce_glyphs (it, i + 1);
20337 /* Adjust i for the loop below. */
20338 i = row->used[TEXT_AREA] - (i + 1);
20341 /* produce_special_glyphs overwrites the last glyph, so
20342 we don't want that if we want to keep that last
20343 glyph, which means it's an image. */
20344 if (it->current_x > it->last_visible_x)
20346 it->current_x = x_before;
20347 if (!FRAME_WINDOW_P (it->f))
20349 for (n = row->used[TEXT_AREA]; i < n; ++i)
20351 row->used[TEXT_AREA] = i;
20352 produce_special_glyphs (it, IT_TRUNCATION);
20355 else
20357 row->used[TEXT_AREA] = i;
20358 produce_special_glyphs (it, IT_TRUNCATION);
20360 it->hpos = hpos_before;
20363 else if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
20365 /* Don't truncate if we can overflow newline into fringe. */
20366 if (!get_next_display_element (it))
20368 it->continuation_lines_width = 0;
20369 row->ends_at_zv_p = 1;
20370 row->exact_window_width_line_p = 1;
20371 break;
20373 if (ITERATOR_AT_END_OF_LINE_P (it))
20375 row->exact_window_width_line_p = 1;
20376 goto at_end_of_line;
20378 it->current_x = x_before;
20379 it->hpos = hpos_before;
20382 row->truncated_on_right_p = 1;
20383 it->continuation_lines_width = 0;
20384 reseat_at_next_visible_line_start (it, 0);
20385 row->ends_at_zv_p = FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n';
20386 break;
20390 if (wrap_data)
20391 bidi_unshelve_cache (wrap_data, 1);
20393 /* If line is not empty and hscrolled, maybe insert truncation glyphs
20394 at the left window margin. */
20395 if (it->first_visible_x
20396 && IT_CHARPOS (*it) != CHARPOS (row->start.pos))
20398 if (!FRAME_WINDOW_P (it->f)
20399 || (((row->reversed_p
20400 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
20401 : WINDOW_LEFT_FRINGE_WIDTH (it->w)) == 0)
20402 /* Don't let insert_left_trunc_glyphs overwrite the
20403 first glyph of the row if it is an image. */
20404 && row->glyphs[TEXT_AREA]->type != IMAGE_GLYPH))
20405 insert_left_trunc_glyphs (it);
20406 row->truncated_on_left_p = 1;
20409 /* Remember the position at which this line ends.
20411 BIDI Note: any code that needs MATRIX_ROW_START/END_CHARPOS
20412 cannot be before the call to find_row_edges below, since that is
20413 where these positions are determined. */
20414 row->end = it->current;
20415 if (!it->bidi_p)
20417 row->minpos = row->start.pos;
20418 row->maxpos = row->end.pos;
20420 else
20422 /* ROW->minpos and ROW->maxpos must be the smallest and
20423 `1 + the largest' buffer positions in ROW. But if ROW was
20424 bidi-reordered, these two positions can be anywhere in the
20425 row, so we must determine them now. */
20426 find_row_edges (it, row, min_pos, min_bpos, max_pos, max_bpos);
20429 /* If the start of this line is the overlay arrow-position, then
20430 mark this glyph row as the one containing the overlay arrow.
20431 This is clearly a mess with variable size fonts. It would be
20432 better to let it be displayed like cursors under X. */
20433 if ((MATRIX_ROW_DISPLAYS_TEXT_P (row) || !overlay_arrow_seen)
20434 && (overlay_arrow_string = overlay_arrow_at_row (it, row),
20435 !NILP (overlay_arrow_string)))
20437 /* Overlay arrow in window redisplay is a fringe bitmap. */
20438 if (STRINGP (overlay_arrow_string))
20440 struct glyph_row *arrow_row
20441 = get_overlay_arrow_glyph_row (it->w, overlay_arrow_string);
20442 struct glyph *glyph = arrow_row->glyphs[TEXT_AREA];
20443 struct glyph *arrow_end = glyph + arrow_row->used[TEXT_AREA];
20444 struct glyph *p = row->glyphs[TEXT_AREA];
20445 struct glyph *p2, *end;
20447 /* Copy the arrow glyphs. */
20448 while (glyph < arrow_end)
20449 *p++ = *glyph++;
20451 /* Throw away padding glyphs. */
20452 p2 = p;
20453 end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
20454 while (p2 < end && CHAR_GLYPH_PADDING_P (*p2))
20455 ++p2;
20456 if (p2 > p)
20458 while (p2 < end)
20459 *p++ = *p2++;
20460 row->used[TEXT_AREA] = p2 - row->glyphs[TEXT_AREA];
20463 else
20465 eassert (INTEGERP (overlay_arrow_string));
20466 row->overlay_arrow_bitmap = XINT (overlay_arrow_string);
20468 overlay_arrow_seen = 1;
20471 /* Highlight trailing whitespace. */
20472 if (!NILP (Vshow_trailing_whitespace))
20473 highlight_trailing_whitespace (it->f, it->glyph_row);
20475 /* Compute pixel dimensions of this line. */
20476 compute_line_metrics (it);
20478 /* Implementation note: No changes in the glyphs of ROW or in their
20479 faces can be done past this point, because compute_line_metrics
20480 computes ROW's hash value and stores it within the glyph_row
20481 structure. */
20483 /* Record whether this row ends inside an ellipsis. */
20484 row->ends_in_ellipsis_p
20485 = (it->method == GET_FROM_DISPLAY_VECTOR
20486 && it->ellipsis_p);
20488 /* Save fringe bitmaps in this row. */
20489 row->left_user_fringe_bitmap = it->left_user_fringe_bitmap;
20490 row->left_user_fringe_face_id = it->left_user_fringe_face_id;
20491 row->right_user_fringe_bitmap = it->right_user_fringe_bitmap;
20492 row->right_user_fringe_face_id = it->right_user_fringe_face_id;
20494 it->left_user_fringe_bitmap = 0;
20495 it->left_user_fringe_face_id = 0;
20496 it->right_user_fringe_bitmap = 0;
20497 it->right_user_fringe_face_id = 0;
20499 /* Maybe set the cursor. */
20500 cvpos = it->w->cursor.vpos;
20501 if ((cvpos < 0
20502 /* In bidi-reordered rows, keep checking for proper cursor
20503 position even if one has been found already, because buffer
20504 positions in such rows change non-linearly with ROW->VPOS,
20505 when a line is continued. One exception: when we are at ZV,
20506 display cursor on the first suitable glyph row, since all
20507 the empty rows after that also have their position set to ZV. */
20508 /* FIXME: Revisit this when glyph ``spilling'' in continuation
20509 lines' rows is implemented for bidi-reordered rows. */
20510 || (it->bidi_p
20511 && !MATRIX_ROW (it->w->desired_matrix, cvpos)->ends_at_zv_p))
20512 && PT >= MATRIX_ROW_START_CHARPOS (row)
20513 && PT <= MATRIX_ROW_END_CHARPOS (row)
20514 && cursor_row_p (row))
20515 set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
20517 /* Prepare for the next line. This line starts horizontally at (X
20518 HPOS) = (0 0). Vertical positions are incremented. As a
20519 convenience for the caller, IT->glyph_row is set to the next
20520 row to be used. */
20521 it->current_x = it->hpos = 0;
20522 it->current_y += row->height;
20523 SET_TEXT_POS (it->eol_pos, 0, 0);
20524 ++it->vpos;
20525 ++it->glyph_row;
20526 /* The next row should by default use the same value of the
20527 reversed_p flag as this one. set_iterator_to_next decides when
20528 it's a new paragraph, and PRODUCE_GLYPHS recomputes the value of
20529 the flag accordingly. */
20530 if (it->glyph_row < MATRIX_BOTTOM_TEXT_ROW (it->w->desired_matrix, it->w))
20531 it->glyph_row->reversed_p = row->reversed_p;
20532 it->start = row->end;
20533 return MATRIX_ROW_DISPLAYS_TEXT_P (row);
20535 #undef RECORD_MAX_MIN_POS
20538 DEFUN ("current-bidi-paragraph-direction", Fcurrent_bidi_paragraph_direction,
20539 Scurrent_bidi_paragraph_direction, 0, 1, 0,
20540 doc: /* Return paragraph direction at point in BUFFER.
20541 Value is either `left-to-right' or `right-to-left'.
20542 If BUFFER is omitted or nil, it defaults to the current buffer.
20544 Paragraph direction determines how the text in the paragraph is displayed.
20545 In left-to-right paragraphs, text begins at the left margin of the window
20546 and the reading direction is generally left to right. In right-to-left
20547 paragraphs, text begins at the right margin and is read from right to left.
20549 See also `bidi-paragraph-direction'. */)
20550 (Lisp_Object buffer)
20552 struct buffer *buf = current_buffer;
20553 struct buffer *old = buf;
20555 if (! NILP (buffer))
20557 CHECK_BUFFER (buffer);
20558 buf = XBUFFER (buffer);
20561 if (NILP (BVAR (buf, bidi_display_reordering))
20562 || NILP (BVAR (buf, enable_multibyte_characters))
20563 /* When we are loading loadup.el, the character property tables
20564 needed for bidi iteration are not yet available. */
20565 || !NILP (Vpurify_flag))
20566 return Qleft_to_right;
20567 else if (!NILP (BVAR (buf, bidi_paragraph_direction)))
20568 return BVAR (buf, bidi_paragraph_direction);
20569 else
20571 /* Determine the direction from buffer text. We could try to
20572 use current_matrix if it is up to date, but this seems fast
20573 enough as it is. */
20574 struct bidi_it itb;
20575 ptrdiff_t pos = BUF_PT (buf);
20576 ptrdiff_t bytepos = BUF_PT_BYTE (buf);
20577 int c;
20578 void *itb_data = bidi_shelve_cache ();
20580 set_buffer_temp (buf);
20581 /* bidi_paragraph_init finds the base direction of the paragraph
20582 by searching forward from paragraph start. We need the base
20583 direction of the current or _previous_ paragraph, so we need
20584 to make sure we are within that paragraph. To that end, find
20585 the previous non-empty line. */
20586 if (pos >= ZV && pos > BEGV)
20587 DEC_BOTH (pos, bytepos);
20588 if (fast_looking_at (build_string ("[\f\t ]*\n"),
20589 pos, bytepos, ZV, ZV_BYTE, Qnil) > 0)
20591 while ((c = FETCH_BYTE (bytepos)) == '\n'
20592 || c == ' ' || c == '\t' || c == '\f')
20594 if (bytepos <= BEGV_BYTE)
20595 break;
20596 bytepos--;
20597 pos--;
20599 while (!CHAR_HEAD_P (FETCH_BYTE (bytepos)))
20600 bytepos--;
20602 bidi_init_it (pos, bytepos, FRAME_WINDOW_P (SELECTED_FRAME ()), &itb);
20603 itb.paragraph_dir = NEUTRAL_DIR;
20604 itb.string.s = NULL;
20605 itb.string.lstring = Qnil;
20606 itb.string.bufpos = 0;
20607 itb.string.from_disp_str = 0;
20608 itb.string.unibyte = 0;
20609 /* We have no window to use here for ignoring window-specific
20610 overlays. Using NULL for window pointer will cause
20611 compute_display_string_pos to use the current buffer. */
20612 itb.w = NULL;
20613 bidi_paragraph_init (NEUTRAL_DIR, &itb, 1);
20614 bidi_unshelve_cache (itb_data, 0);
20615 set_buffer_temp (old);
20616 switch (itb.paragraph_dir)
20618 case L2R:
20619 return Qleft_to_right;
20620 break;
20621 case R2L:
20622 return Qright_to_left;
20623 break;
20624 default:
20625 emacs_abort ();
20630 DEFUN ("move-point-visually", Fmove_point_visually,
20631 Smove_point_visually, 1, 1, 0,
20632 doc: /* Move point in the visual order in the specified DIRECTION.
20633 DIRECTION can be 1, meaning move to the right, or -1, which moves to the
20634 left.
20636 Value is the new character position of point. */)
20637 (Lisp_Object direction)
20639 struct window *w = XWINDOW (selected_window);
20640 struct buffer *b = XBUFFER (w->contents);
20641 struct glyph_row *row;
20642 int dir;
20643 Lisp_Object paragraph_dir;
20645 #define ROW_GLYPH_NEWLINE_P(ROW,GLYPH) \
20646 (!(ROW)->continued_p \
20647 && INTEGERP ((GLYPH)->object) \
20648 && (GLYPH)->type == CHAR_GLYPH \
20649 && (GLYPH)->u.ch == ' ' \
20650 && (GLYPH)->charpos >= 0 \
20651 && !(GLYPH)->avoid_cursor_p)
20653 CHECK_NUMBER (direction);
20654 dir = XINT (direction);
20655 if (dir > 0)
20656 dir = 1;
20657 else
20658 dir = -1;
20660 /* If current matrix is up-to-date, we can use the information
20661 recorded in the glyphs, at least as long as the goal is on the
20662 screen. */
20663 if (w->window_end_valid
20664 && !windows_or_buffers_changed
20665 && b
20666 && !b->clip_changed
20667 && !b->prevent_redisplay_optimizations_p
20668 && !window_outdated (w)
20669 && w->cursor.vpos >= 0
20670 && w->cursor.vpos < w->current_matrix->nrows
20671 && (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos))->enabled_p)
20673 struct glyph *g = row->glyphs[TEXT_AREA];
20674 struct glyph *e = dir > 0 ? g + row->used[TEXT_AREA] : g - 1;
20675 struct glyph *gpt = g + w->cursor.hpos;
20677 for (g = gpt + dir; (dir > 0 ? g < e : g > e); g += dir)
20679 if (BUFFERP (g->object) && g->charpos != PT)
20681 SET_PT (g->charpos);
20682 w->cursor.vpos = -1;
20683 return make_number (PT);
20685 else if (!INTEGERP (g->object) && !EQ (g->object, gpt->object))
20687 ptrdiff_t new_pos;
20689 if (BUFFERP (gpt->object))
20691 new_pos = PT;
20692 if ((gpt->resolved_level - row->reversed_p) % 2 == 0)
20693 new_pos += (row->reversed_p ? -dir : dir);
20694 else
20695 new_pos -= (row->reversed_p ? -dir : dir);;
20697 else if (BUFFERP (g->object))
20698 new_pos = g->charpos;
20699 else
20700 break;
20701 SET_PT (new_pos);
20702 w->cursor.vpos = -1;
20703 return make_number (PT);
20705 else if (ROW_GLYPH_NEWLINE_P (row, g))
20707 /* Glyphs inserted at the end of a non-empty line for
20708 positioning the cursor have zero charpos, so we must
20709 deduce the value of point by other means. */
20710 if (g->charpos > 0)
20711 SET_PT (g->charpos);
20712 else if (row->ends_at_zv_p && PT != ZV)
20713 SET_PT (ZV);
20714 else if (PT != MATRIX_ROW_END_CHARPOS (row) - 1)
20715 SET_PT (MATRIX_ROW_END_CHARPOS (row) - 1);
20716 else
20717 break;
20718 w->cursor.vpos = -1;
20719 return make_number (PT);
20722 if (g == e || INTEGERP (g->object))
20724 if (row->truncated_on_left_p || row->truncated_on_right_p)
20725 goto simulate_display;
20726 if (!row->reversed_p)
20727 row += dir;
20728 else
20729 row -= dir;
20730 if (row < MATRIX_FIRST_TEXT_ROW (w->current_matrix)
20731 || row > MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w))
20732 goto simulate_display;
20734 if (dir > 0)
20736 if (row->reversed_p && !row->continued_p)
20738 SET_PT (MATRIX_ROW_END_CHARPOS (row) - 1);
20739 w->cursor.vpos = -1;
20740 return make_number (PT);
20742 g = row->glyphs[TEXT_AREA];
20743 e = g + row->used[TEXT_AREA];
20744 for ( ; g < e; g++)
20746 if (BUFFERP (g->object)
20747 /* Empty lines have only one glyph, which stands
20748 for the newline, and whose charpos is the
20749 buffer position of the newline. */
20750 || ROW_GLYPH_NEWLINE_P (row, g)
20751 /* When the buffer ends in a newline, the line at
20752 EOB also has one glyph, but its charpos is -1. */
20753 || (row->ends_at_zv_p
20754 && !row->reversed_p
20755 && INTEGERP (g->object)
20756 && g->type == CHAR_GLYPH
20757 && g->u.ch == ' '))
20759 if (g->charpos > 0)
20760 SET_PT (g->charpos);
20761 else if (!row->reversed_p
20762 && row->ends_at_zv_p
20763 && PT != ZV)
20764 SET_PT (ZV);
20765 else
20766 continue;
20767 w->cursor.vpos = -1;
20768 return make_number (PT);
20772 else
20774 if (!row->reversed_p && !row->continued_p)
20776 SET_PT (MATRIX_ROW_END_CHARPOS (row) - 1);
20777 w->cursor.vpos = -1;
20778 return make_number (PT);
20780 e = row->glyphs[TEXT_AREA];
20781 g = e + row->used[TEXT_AREA] - 1;
20782 for ( ; g >= e; g--)
20784 if (BUFFERP (g->object)
20785 || (ROW_GLYPH_NEWLINE_P (row, g)
20786 && g->charpos > 0)
20787 /* Empty R2L lines on GUI frames have the buffer
20788 position of the newline stored in the stretch
20789 glyph. */
20790 || g->type == STRETCH_GLYPH
20791 || (row->ends_at_zv_p
20792 && row->reversed_p
20793 && INTEGERP (g->object)
20794 && g->type == CHAR_GLYPH
20795 && g->u.ch == ' '))
20797 if (g->charpos > 0)
20798 SET_PT (g->charpos);
20799 else if (row->reversed_p
20800 && row->ends_at_zv_p
20801 && PT != ZV)
20802 SET_PT (ZV);
20803 else
20804 continue;
20805 w->cursor.vpos = -1;
20806 return make_number (PT);
20813 simulate_display:
20815 /* If we wind up here, we failed to move by using the glyphs, so we
20816 need to simulate display instead. */
20818 if (b)
20819 paragraph_dir = Fcurrent_bidi_paragraph_direction (w->contents);
20820 else
20821 paragraph_dir = Qleft_to_right;
20822 if (EQ (paragraph_dir, Qright_to_left))
20823 dir = -dir;
20824 if (PT <= BEGV && dir < 0)
20825 xsignal0 (Qbeginning_of_buffer);
20826 else if (PT >= ZV && dir > 0)
20827 xsignal0 (Qend_of_buffer);
20828 else
20830 struct text_pos pt;
20831 struct it it;
20832 int pt_x, target_x, pixel_width, pt_vpos;
20833 bool at_eol_p;
20834 bool overshoot_expected = false;
20835 bool target_is_eol_p = false;
20837 /* Setup the arena. */
20838 SET_TEXT_POS (pt, PT, PT_BYTE);
20839 start_display (&it, w, pt);
20841 if (it.cmp_it.id < 0
20842 && it.method == GET_FROM_STRING
20843 && it.area == TEXT_AREA
20844 && it.string_from_display_prop_p
20845 && (it.sp > 0 && it.stack[it.sp - 1].method == GET_FROM_BUFFER))
20846 overshoot_expected = true;
20848 /* Find the X coordinate of point. We start from the beginning
20849 of this or previous line to make sure we are before point in
20850 the logical order (since the move_it_* functions can only
20851 move forward). */
20852 reseat:
20853 reseat_at_previous_visible_line_start (&it);
20854 it.current_x = it.hpos = it.current_y = it.vpos = 0;
20855 if (IT_CHARPOS (it) != PT)
20857 move_it_to (&it, overshoot_expected ? PT - 1 : PT,
20858 -1, -1, -1, MOVE_TO_POS);
20859 /* If we missed point because the character there is
20860 displayed out of a display vector that has more than one
20861 glyph, retry expecting overshoot. */
20862 if (it.method == GET_FROM_DISPLAY_VECTOR
20863 && it.current.dpvec_index > 0
20864 && !overshoot_expected)
20866 overshoot_expected = true;
20867 goto reseat;
20869 else if (IT_CHARPOS (it) != PT && !overshoot_expected)
20870 move_it_in_display_line (&it, PT, -1, MOVE_TO_POS);
20872 pt_x = it.current_x;
20873 pt_vpos = it.vpos;
20874 if (dir > 0 || overshoot_expected)
20876 struct glyph_row *row = it.glyph_row;
20878 /* When point is at beginning of line, we don't have
20879 information about the glyph there loaded into struct
20880 it. Calling get_next_display_element fixes that. */
20881 if (pt_x == 0)
20882 get_next_display_element (&it);
20883 at_eol_p = ITERATOR_AT_END_OF_LINE_P (&it);
20884 it.glyph_row = NULL;
20885 PRODUCE_GLYPHS (&it); /* compute it.pixel_width */
20886 it.glyph_row = row;
20887 /* PRODUCE_GLYPHS advances it.current_x, so we must restore
20888 it, lest it will become out of sync with it's buffer
20889 position. */
20890 it.current_x = pt_x;
20892 else
20893 at_eol_p = ITERATOR_AT_END_OF_LINE_P (&it);
20894 pixel_width = it.pixel_width;
20895 if (overshoot_expected && at_eol_p)
20896 pixel_width = 0;
20897 else if (pixel_width <= 0)
20898 pixel_width = 1;
20900 /* If there's a display string (or something similar) at point,
20901 we are actually at the glyph to the left of point, so we need
20902 to correct the X coordinate. */
20903 if (overshoot_expected)
20905 if (it.bidi_p)
20906 pt_x += pixel_width * it.bidi_it.scan_dir;
20907 else
20908 pt_x += pixel_width;
20911 /* Compute target X coordinate, either to the left or to the
20912 right of point. On TTY frames, all characters have the same
20913 pixel width of 1, so we can use that. On GUI frames we don't
20914 have an easy way of getting at the pixel width of the
20915 character to the left of point, so we use a different method
20916 of getting to that place. */
20917 if (dir > 0)
20918 target_x = pt_x + pixel_width;
20919 else
20920 target_x = pt_x - (!FRAME_WINDOW_P (it.f)) * pixel_width;
20922 /* Target X coordinate could be one line above or below the line
20923 of point, in which case we need to adjust the target X
20924 coordinate. Also, if moving to the left, we need to begin at
20925 the left edge of the point's screen line. */
20926 if (dir < 0)
20928 if (pt_x > 0)
20930 start_display (&it, w, pt);
20931 reseat_at_previous_visible_line_start (&it);
20932 it.current_x = it.current_y = it.hpos = 0;
20933 if (pt_vpos != 0)
20934 move_it_by_lines (&it, pt_vpos);
20936 else
20938 move_it_by_lines (&it, -1);
20939 target_x = it.last_visible_x - !FRAME_WINDOW_P (it.f);
20940 target_is_eol_p = true;
20941 /* Under word-wrap, we don't know the x coordinate of
20942 the last character displayed on the previous line,
20943 which immediately precedes the wrap point. To find
20944 out its x coordinate, we try moving to the right
20945 margin of the window, which will stop at the wrap
20946 point, and then reset target_x to point at the
20947 character that precedes the wrap point. This is not
20948 needed on GUI frames, because (see below) there we
20949 move from the left margin one grapheme cluster at a
20950 time, and stop when we hit the wrap point. */
20951 if (!FRAME_WINDOW_P (it.f) && it.line_wrap == WORD_WRAP)
20953 void *it_data = NULL;
20954 struct it it2;
20956 SAVE_IT (it2, it, it_data);
20957 move_it_in_display_line_to (&it, ZV, target_x,
20958 MOVE_TO_POS | MOVE_TO_X);
20959 /* If we arrived at target_x, that _is_ the last
20960 character on the previous line. */
20961 if (it.current_x != target_x)
20962 target_x = it.current_x - 1;
20963 RESTORE_IT (&it, &it2, it_data);
20967 else
20969 if (at_eol_p
20970 || (target_x >= it.last_visible_x
20971 && it.line_wrap != TRUNCATE))
20973 if (pt_x > 0)
20974 move_it_by_lines (&it, 0);
20975 move_it_by_lines (&it, 1);
20976 target_x = 0;
20980 /* Move to the target X coordinate. */
20981 #ifdef HAVE_WINDOW_SYSTEM
20982 /* On GUI frames, as we don't know the X coordinate of the
20983 character to the left of point, moving point to the left
20984 requires walking, one grapheme cluster at a time, until we
20985 find ourself at a place immediately to the left of the
20986 character at point. */
20987 if (FRAME_WINDOW_P (it.f) && dir < 0)
20989 struct text_pos new_pos;
20990 enum move_it_result rc = MOVE_X_REACHED;
20992 if (it.current_x == 0)
20993 get_next_display_element (&it);
20994 if (it.what == IT_COMPOSITION)
20996 new_pos.charpos = it.cmp_it.charpos;
20997 new_pos.bytepos = -1;
20999 else
21000 new_pos = it.current.pos;
21002 while (it.current_x + it.pixel_width <= target_x
21003 && (rc == MOVE_X_REACHED
21004 /* Under word-wrap, move_it_in_display_line_to
21005 stops at correct coordinates, but sometimes
21006 returns MOVE_POS_MATCH_OR_ZV. */
21007 || (it.line_wrap == WORD_WRAP
21008 && rc == MOVE_POS_MATCH_OR_ZV)))
21010 int new_x = it.current_x + it.pixel_width;
21012 /* For composed characters, we want the position of the
21013 first character in the grapheme cluster (usually, the
21014 composition's base character), whereas it.current
21015 might give us the position of the _last_ one, e.g. if
21016 the composition is rendered in reverse due to bidi
21017 reordering. */
21018 if (it.what == IT_COMPOSITION)
21020 new_pos.charpos = it.cmp_it.charpos;
21021 new_pos.bytepos = -1;
21023 else
21024 new_pos = it.current.pos;
21025 if (new_x == it.current_x)
21026 new_x++;
21027 rc = move_it_in_display_line_to (&it, ZV, new_x,
21028 MOVE_TO_POS | MOVE_TO_X);
21029 if (ITERATOR_AT_END_OF_LINE_P (&it) && !target_is_eol_p)
21030 break;
21032 /* The previous position we saw in the loop is the one we
21033 want. */
21034 if (new_pos.bytepos == -1)
21035 new_pos.bytepos = CHAR_TO_BYTE (new_pos.charpos);
21036 it.current.pos = new_pos;
21038 else
21039 #endif
21040 if (it.current_x != target_x)
21041 move_it_in_display_line_to (&it, ZV, target_x, MOVE_TO_POS | MOVE_TO_X);
21043 /* When lines are truncated, the above loop will stop at the
21044 window edge. But we want to get to the end of line, even if
21045 it is beyond the window edge; automatic hscroll will then
21046 scroll the window to show point as appropriate. */
21047 if (target_is_eol_p && it.line_wrap == TRUNCATE
21048 && get_next_display_element (&it))
21050 struct text_pos new_pos = it.current.pos;
21052 while (!ITERATOR_AT_END_OF_LINE_P (&it))
21054 set_iterator_to_next (&it, 0);
21055 if (it.method == GET_FROM_BUFFER)
21056 new_pos = it.current.pos;
21057 if (!get_next_display_element (&it))
21058 break;
21061 it.current.pos = new_pos;
21064 /* If we ended up in a display string that covers point, move to
21065 buffer position to the right in the visual order. */
21066 if (dir > 0)
21068 while (IT_CHARPOS (it) == PT)
21070 set_iterator_to_next (&it, 0);
21071 if (!get_next_display_element (&it))
21072 break;
21076 /* Move point to that position. */
21077 SET_PT_BOTH (IT_CHARPOS (it), IT_BYTEPOS (it));
21080 return make_number (PT);
21082 #undef ROW_GLYPH_NEWLINE_P
21086 /***********************************************************************
21087 Menu Bar
21088 ***********************************************************************/
21090 /* Redisplay the menu bar in the frame for window W.
21092 The menu bar of X frames that don't have X toolkit support is
21093 displayed in a special window W->frame->menu_bar_window.
21095 The menu bar of terminal frames is treated specially as far as
21096 glyph matrices are concerned. Menu bar lines are not part of
21097 windows, so the update is done directly on the frame matrix rows
21098 for the menu bar. */
21100 static void
21101 display_menu_bar (struct window *w)
21103 struct frame *f = XFRAME (WINDOW_FRAME (w));
21104 struct it it;
21105 Lisp_Object items;
21106 int i;
21108 /* Don't do all this for graphical frames. */
21109 #ifdef HAVE_NTGUI
21110 if (FRAME_W32_P (f))
21111 return;
21112 #endif
21113 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
21114 if (FRAME_X_P (f))
21115 return;
21116 #endif
21118 #ifdef HAVE_NS
21119 if (FRAME_NS_P (f))
21120 return;
21121 #endif /* HAVE_NS */
21123 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
21124 eassert (!FRAME_WINDOW_P (f));
21125 init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MENU_FACE_ID);
21126 it.first_visible_x = 0;
21127 it.last_visible_x = FRAME_PIXEL_WIDTH (f);
21128 #elif defined (HAVE_X_WINDOWS) /* X without toolkit. */
21129 if (FRAME_WINDOW_P (f))
21131 /* Menu bar lines are displayed in the desired matrix of the
21132 dummy window menu_bar_window. */
21133 struct window *menu_w;
21134 menu_w = XWINDOW (f->menu_bar_window);
21135 init_iterator (&it, menu_w, -1, -1, menu_w->desired_matrix->rows,
21136 MENU_FACE_ID);
21137 it.first_visible_x = 0;
21138 it.last_visible_x = FRAME_PIXEL_WIDTH (f);
21140 else
21141 #endif /* not USE_X_TOOLKIT and not USE_GTK */
21143 /* This is a TTY frame, i.e. character hpos/vpos are used as
21144 pixel x/y. */
21145 init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
21146 MENU_FACE_ID);
21147 it.first_visible_x = 0;
21148 it.last_visible_x = FRAME_COLS (f);
21151 /* FIXME: This should be controlled by a user option. See the
21152 comments in redisplay_tool_bar and display_mode_line about
21153 this. */
21154 it.paragraph_embedding = L2R;
21156 /* Clear all rows of the menu bar. */
21157 for (i = 0; i < FRAME_MENU_BAR_LINES (f); ++i)
21159 struct glyph_row *row = it.glyph_row + i;
21160 clear_glyph_row (row);
21161 row->enabled_p = true;
21162 row->full_width_p = 1;
21165 /* Display all items of the menu bar. */
21166 items = FRAME_MENU_BAR_ITEMS (it.f);
21167 for (i = 0; i < ASIZE (items); i += 4)
21169 Lisp_Object string;
21171 /* Stop at nil string. */
21172 string = AREF (items, i + 1);
21173 if (NILP (string))
21174 break;
21176 /* Remember where item was displayed. */
21177 ASET (items, i + 3, make_number (it.hpos));
21179 /* Display the item, pad with one space. */
21180 if (it.current_x < it.last_visible_x)
21181 display_string (NULL, string, Qnil, 0, 0, &it,
21182 SCHARS (string) + 1, 0, 0, -1);
21185 /* Fill out the line with spaces. */
21186 if (it.current_x < it.last_visible_x)
21187 display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
21189 /* Compute the total height of the lines. */
21190 compute_line_metrics (&it);
21193 /* Deep copy of a glyph row, including the glyphs. */
21194 static void
21195 deep_copy_glyph_row (struct glyph_row *to, struct glyph_row *from)
21197 struct glyph *pointers[1 + LAST_AREA];
21198 int to_used = to->used[TEXT_AREA];
21200 /* Save glyph pointers of TO. */
21201 memcpy (pointers, to->glyphs, sizeof to->glyphs);
21203 /* Do a structure assignment. */
21204 *to = *from;
21206 /* Restore original glyph pointers of TO. */
21207 memcpy (to->glyphs, pointers, sizeof to->glyphs);
21209 /* Copy the glyphs. */
21210 memcpy (to->glyphs[TEXT_AREA], from->glyphs[TEXT_AREA],
21211 min (from->used[TEXT_AREA], to_used) * sizeof (struct glyph));
21213 /* If we filled only part of the TO row, fill the rest with
21214 space_glyph (which will display as empty space). */
21215 if (to_used > from->used[TEXT_AREA])
21216 fill_up_frame_row_with_spaces (to, to_used);
21219 /* Display one menu item on a TTY, by overwriting the glyphs in the
21220 frame F's desired glyph matrix with glyphs produced from the menu
21221 item text. Called from term.c to display TTY drop-down menus one
21222 item at a time.
21224 ITEM_TEXT is the menu item text as a C string.
21226 FACE_ID is the face ID to be used for this menu item. FACE_ID
21227 could specify one of 3 faces: a face for an enabled item, a face
21228 for a disabled item, or a face for a selected item.
21230 X and Y are coordinates of the first glyph in the frame's desired
21231 matrix to be overwritten by the menu item. Since this is a TTY, Y
21232 is the zero-based number of the glyph row and X is the zero-based
21233 glyph number in the row, starting from left, where to start
21234 displaying the item.
21236 SUBMENU non-zero means this menu item drops down a submenu, which
21237 should be indicated by displaying a proper visual cue after the
21238 item text. */
21240 void
21241 display_tty_menu_item (const char *item_text, int width, int face_id,
21242 int x, int y, int submenu)
21244 struct it it;
21245 struct frame *f = SELECTED_FRAME ();
21246 struct window *w = XWINDOW (f->selected_window);
21247 int saved_used, saved_truncated, saved_width, saved_reversed;
21248 struct glyph_row *row;
21249 size_t item_len = strlen (item_text);
21251 eassert (FRAME_TERMCAP_P (f));
21253 /* Don't write beyond the matrix's last row. This can happen for
21254 TTY screens that are not high enough to show the entire menu.
21255 (This is actually a bit of defensive programming, as
21256 tty_menu_display already limits the number of menu items to one
21257 less than the number of screen lines.) */
21258 if (y >= f->desired_matrix->nrows)
21259 return;
21261 init_iterator (&it, w, -1, -1, f->desired_matrix->rows + y, MENU_FACE_ID);
21262 it.first_visible_x = 0;
21263 it.last_visible_x = FRAME_COLS (f) - 1;
21264 row = it.glyph_row;
21265 /* Start with the row contents from the current matrix. */
21266 deep_copy_glyph_row (row, f->current_matrix->rows + y);
21267 saved_width = row->full_width_p;
21268 row->full_width_p = 1;
21269 saved_reversed = row->reversed_p;
21270 row->reversed_p = 0;
21271 row->enabled_p = true;
21273 /* Arrange for the menu item glyphs to start at (X,Y) and have the
21274 desired face. */
21275 eassert (x < f->desired_matrix->matrix_w);
21276 it.current_x = it.hpos = x;
21277 it.current_y = it.vpos = y;
21278 saved_used = row->used[TEXT_AREA];
21279 saved_truncated = row->truncated_on_right_p;
21280 row->used[TEXT_AREA] = x;
21281 it.face_id = face_id;
21282 it.line_wrap = TRUNCATE;
21284 /* FIXME: This should be controlled by a user option. See the
21285 comments in redisplay_tool_bar and display_mode_line about this.
21286 Also, if paragraph_embedding could ever be R2L, changes will be
21287 needed to avoid shifting to the right the row characters in
21288 term.c:append_glyph. */
21289 it.paragraph_embedding = L2R;
21291 /* Pad with a space on the left. */
21292 display_string (" ", Qnil, Qnil, 0, 0, &it, 1, 0, FRAME_COLS (f) - 1, -1);
21293 width--;
21294 /* Display the menu item, pad with spaces to WIDTH. */
21295 if (submenu)
21297 display_string (item_text, Qnil, Qnil, 0, 0, &it,
21298 item_len, 0, FRAME_COLS (f) - 1, -1);
21299 width -= item_len;
21300 /* Indicate with " >" that there's a submenu. */
21301 display_string (" >", Qnil, Qnil, 0, 0, &it, width, 0,
21302 FRAME_COLS (f) - 1, -1);
21304 else
21305 display_string (item_text, Qnil, Qnil, 0, 0, &it,
21306 width, 0, FRAME_COLS (f) - 1, -1);
21308 row->used[TEXT_AREA] = max (saved_used, row->used[TEXT_AREA]);
21309 row->truncated_on_right_p = saved_truncated;
21310 row->hash = row_hash (row);
21311 row->full_width_p = saved_width;
21312 row->reversed_p = saved_reversed;
21315 /***********************************************************************
21316 Mode Line
21317 ***********************************************************************/
21319 /* Redisplay mode lines in the window tree whose root is WINDOW. If
21320 FORCE is non-zero, redisplay mode lines unconditionally.
21321 Otherwise, redisplay only mode lines that are garbaged. Value is
21322 the number of windows whose mode lines were redisplayed. */
21324 static int
21325 redisplay_mode_lines (Lisp_Object window, bool force)
21327 int nwindows = 0;
21329 while (!NILP (window))
21331 struct window *w = XWINDOW (window);
21333 if (WINDOWP (w->contents))
21334 nwindows += redisplay_mode_lines (w->contents, force);
21335 else if (force
21336 || FRAME_GARBAGED_P (XFRAME (w->frame))
21337 || !MATRIX_MODE_LINE_ROW (w->current_matrix)->enabled_p)
21339 struct text_pos lpoint;
21340 struct buffer *old = current_buffer;
21342 /* Set the window's buffer for the mode line display. */
21343 SET_TEXT_POS (lpoint, PT, PT_BYTE);
21344 set_buffer_internal_1 (XBUFFER (w->contents));
21346 /* Point refers normally to the selected window. For any
21347 other window, set up appropriate value. */
21348 if (!EQ (window, selected_window))
21350 struct text_pos pt;
21352 CLIP_TEXT_POS_FROM_MARKER (pt, w->pointm);
21353 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
21356 /* Display mode lines. */
21357 clear_glyph_matrix (w->desired_matrix);
21358 if (display_mode_lines (w))
21359 ++nwindows;
21361 /* Restore old settings. */
21362 set_buffer_internal_1 (old);
21363 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
21366 window = w->next;
21369 return nwindows;
21373 /* Display the mode and/or header line of window W. Value is the
21374 sum number of mode lines and header lines displayed. */
21376 static int
21377 display_mode_lines (struct window *w)
21379 Lisp_Object old_selected_window = selected_window;
21380 Lisp_Object old_selected_frame = selected_frame;
21381 Lisp_Object new_frame = w->frame;
21382 Lisp_Object old_frame_selected_window = XFRAME (new_frame)->selected_window;
21383 int n = 0;
21385 selected_frame = new_frame;
21386 /* FIXME: If we were to allow the mode-line's computation changing the buffer
21387 or window's point, then we'd need select_window_1 here as well. */
21388 XSETWINDOW (selected_window, w);
21389 XFRAME (new_frame)->selected_window = selected_window;
21391 /* These will be set while the mode line specs are processed. */
21392 line_number_displayed = 0;
21393 w->column_number_displayed = -1;
21395 if (WINDOW_WANTS_MODELINE_P (w))
21397 struct window *sel_w = XWINDOW (old_selected_window);
21399 /* Select mode line face based on the real selected window. */
21400 display_mode_line (w, CURRENT_MODE_LINE_FACE_ID_3 (sel_w, sel_w, w),
21401 BVAR (current_buffer, mode_line_format));
21402 ++n;
21405 if (WINDOW_WANTS_HEADER_LINE_P (w))
21407 display_mode_line (w, HEADER_LINE_FACE_ID,
21408 BVAR (current_buffer, header_line_format));
21409 ++n;
21412 XFRAME (new_frame)->selected_window = old_frame_selected_window;
21413 selected_frame = old_selected_frame;
21414 selected_window = old_selected_window;
21415 if (n > 0)
21416 w->must_be_updated_p = true;
21417 return n;
21421 /* Display mode or header line of window W. FACE_ID specifies which
21422 line to display; it is either MODE_LINE_FACE_ID or
21423 HEADER_LINE_FACE_ID. FORMAT is the mode/header line format to
21424 display. Value is the pixel height of the mode/header line
21425 displayed. */
21427 static int
21428 display_mode_line (struct window *w, enum face_id face_id, Lisp_Object format)
21430 struct it it;
21431 struct face *face;
21432 ptrdiff_t count = SPECPDL_INDEX ();
21434 init_iterator (&it, w, -1, -1, NULL, face_id);
21435 /* Don't extend on a previously drawn mode-line.
21436 This may happen if called from pos_visible_p. */
21437 it.glyph_row->enabled_p = false;
21438 prepare_desired_row (it.glyph_row);
21440 it.glyph_row->mode_line_p = 1;
21442 /* FIXME: This should be controlled by a user option. But
21443 supporting such an option is not trivial, since the mode line is
21444 made up of many separate strings. */
21445 it.paragraph_embedding = L2R;
21447 record_unwind_protect (unwind_format_mode_line,
21448 format_mode_line_unwind_data (NULL, NULL, Qnil, 0));
21450 mode_line_target = MODE_LINE_DISPLAY;
21452 /* Temporarily make frame's keyboard the current kboard so that
21453 kboard-local variables in the mode_line_format will get the right
21454 values. */
21455 push_kboard (FRAME_KBOARD (it.f));
21456 record_unwind_save_match_data ();
21457 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
21458 pop_kboard ();
21460 unbind_to (count, Qnil);
21462 /* Fill up with spaces. */
21463 display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);
21465 compute_line_metrics (&it);
21466 it.glyph_row->full_width_p = 1;
21467 it.glyph_row->continued_p = 0;
21468 it.glyph_row->truncated_on_left_p = 0;
21469 it.glyph_row->truncated_on_right_p = 0;
21471 /* Make a 3D mode-line have a shadow at its right end. */
21472 face = FACE_FROM_ID (it.f, face_id);
21473 extend_face_to_end_of_line (&it);
21474 if (face->box != FACE_NO_BOX)
21476 struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
21477 + it.glyph_row->used[TEXT_AREA] - 1);
21478 last->right_box_line_p = 1;
21481 return it.glyph_row->height;
21484 /* Move element ELT in LIST to the front of LIST.
21485 Return the updated list. */
21487 static Lisp_Object
21488 move_elt_to_front (Lisp_Object elt, Lisp_Object list)
21490 register Lisp_Object tail, prev;
21491 register Lisp_Object tem;
21493 tail = list;
21494 prev = Qnil;
21495 while (CONSP (tail))
21497 tem = XCAR (tail);
21499 if (EQ (elt, tem))
21501 /* Splice out the link TAIL. */
21502 if (NILP (prev))
21503 list = XCDR (tail);
21504 else
21505 Fsetcdr (prev, XCDR (tail));
21507 /* Now make it the first. */
21508 Fsetcdr (tail, list);
21509 return tail;
21511 else
21512 prev = tail;
21513 tail = XCDR (tail);
21514 QUIT;
21517 /* Not found--return unchanged LIST. */
21518 return list;
21521 /* Contribute ELT to the mode line for window IT->w. How it
21522 translates into text depends on its data type.
21524 IT describes the display environment in which we display, as usual.
21526 DEPTH is the depth in recursion. It is used to prevent
21527 infinite recursion here.
21529 FIELD_WIDTH is the number of characters the display of ELT should
21530 occupy in the mode line, and PRECISION is the maximum number of
21531 characters to display from ELT's representation. See
21532 display_string for details.
21534 Returns the hpos of the end of the text generated by ELT.
21536 PROPS is a property list to add to any string we encounter.
21538 If RISKY is nonzero, remove (disregard) any properties in any string
21539 we encounter, and ignore :eval and :propertize.
21541 The global variable `mode_line_target' determines whether the
21542 output is passed to `store_mode_line_noprop',
21543 `store_mode_line_string', or `display_string'. */
21545 static int
21546 display_mode_element (struct it *it, int depth, int field_width, int precision,
21547 Lisp_Object elt, Lisp_Object props, int risky)
21549 int n = 0, field, prec;
21550 int literal = 0;
21552 tail_recurse:
21553 if (depth > 100)
21554 elt = build_string ("*too-deep*");
21556 depth++;
21558 switch (XTYPE (elt))
21560 case Lisp_String:
21562 /* A string: output it and check for %-constructs within it. */
21563 unsigned char c;
21564 ptrdiff_t offset = 0;
21566 if (SCHARS (elt) > 0
21567 && (!NILP (props) || risky))
21569 Lisp_Object oprops, aelt;
21570 oprops = Ftext_properties_at (make_number (0), elt);
21572 /* If the starting string's properties are not what
21573 we want, translate the string. Also, if the string
21574 is risky, do that anyway. */
21576 if (NILP (Fequal (props, oprops)) || risky)
21578 /* If the starting string has properties,
21579 merge the specified ones onto the existing ones. */
21580 if (! NILP (oprops) && !risky)
21582 Lisp_Object tem;
21584 oprops = Fcopy_sequence (oprops);
21585 tem = props;
21586 while (CONSP (tem))
21588 oprops = Fplist_put (oprops, XCAR (tem),
21589 XCAR (XCDR (tem)));
21590 tem = XCDR (XCDR (tem));
21592 props = oprops;
21595 aelt = Fassoc (elt, mode_line_proptrans_alist);
21596 if (! NILP (aelt) && !NILP (Fequal (props, XCDR (aelt))))
21598 /* AELT is what we want. Move it to the front
21599 without consing. */
21600 elt = XCAR (aelt);
21601 mode_line_proptrans_alist
21602 = move_elt_to_front (aelt, mode_line_proptrans_alist);
21604 else
21606 Lisp_Object tem;
21608 /* If AELT has the wrong props, it is useless.
21609 so get rid of it. */
21610 if (! NILP (aelt))
21611 mode_line_proptrans_alist
21612 = Fdelq (aelt, mode_line_proptrans_alist);
21614 elt = Fcopy_sequence (elt);
21615 Fset_text_properties (make_number (0), Flength (elt),
21616 props, elt);
21617 /* Add this item to mode_line_proptrans_alist. */
21618 mode_line_proptrans_alist
21619 = Fcons (Fcons (elt, props),
21620 mode_line_proptrans_alist);
21621 /* Truncate mode_line_proptrans_alist
21622 to at most 50 elements. */
21623 tem = Fnthcdr (make_number (50),
21624 mode_line_proptrans_alist);
21625 if (! NILP (tem))
21626 XSETCDR (tem, Qnil);
21631 offset = 0;
21633 if (literal)
21635 prec = precision - n;
21636 switch (mode_line_target)
21638 case MODE_LINE_NOPROP:
21639 case MODE_LINE_TITLE:
21640 n += store_mode_line_noprop (SSDATA (elt), -1, prec);
21641 break;
21642 case MODE_LINE_STRING:
21643 n += store_mode_line_string (NULL, elt, 1, 0, prec, Qnil);
21644 break;
21645 case MODE_LINE_DISPLAY:
21646 n += display_string (NULL, elt, Qnil, 0, 0, it,
21647 0, prec, 0, STRING_MULTIBYTE (elt));
21648 break;
21651 break;
21654 /* Handle the non-literal case. */
21656 while ((precision <= 0 || n < precision)
21657 && SREF (elt, offset) != 0
21658 && (mode_line_target != MODE_LINE_DISPLAY
21659 || it->current_x < it->last_visible_x))
21661 ptrdiff_t last_offset = offset;
21663 /* Advance to end of string or next format specifier. */
21664 while ((c = SREF (elt, offset++)) != '\0' && c != '%')
21667 if (offset - 1 != last_offset)
21669 ptrdiff_t nchars, nbytes;
21671 /* Output to end of string or up to '%'. Field width
21672 is length of string. Don't output more than
21673 PRECISION allows us. */
21674 offset--;
21676 prec = c_string_width (SDATA (elt) + last_offset,
21677 offset - last_offset, precision - n,
21678 &nchars, &nbytes);
21680 switch (mode_line_target)
21682 case MODE_LINE_NOPROP:
21683 case MODE_LINE_TITLE:
21684 n += store_mode_line_noprop (SSDATA (elt) + last_offset, 0, prec);
21685 break;
21686 case MODE_LINE_STRING:
21688 ptrdiff_t bytepos = last_offset;
21689 ptrdiff_t charpos = string_byte_to_char (elt, bytepos);
21690 ptrdiff_t endpos = (precision <= 0
21691 ? string_byte_to_char (elt, offset)
21692 : charpos + nchars);
21694 n += store_mode_line_string (NULL,
21695 Fsubstring (elt, make_number (charpos),
21696 make_number (endpos)),
21697 0, 0, 0, Qnil);
21699 break;
21700 case MODE_LINE_DISPLAY:
21702 ptrdiff_t bytepos = last_offset;
21703 ptrdiff_t charpos = string_byte_to_char (elt, bytepos);
21705 if (precision <= 0)
21706 nchars = string_byte_to_char (elt, offset) - charpos;
21707 n += display_string (NULL, elt, Qnil, 0, charpos,
21708 it, 0, nchars, 0,
21709 STRING_MULTIBYTE (elt));
21711 break;
21714 else /* c == '%' */
21716 ptrdiff_t percent_position = offset;
21718 /* Get the specified minimum width. Zero means
21719 don't pad. */
21720 field = 0;
21721 while ((c = SREF (elt, offset++)) >= '0' && c <= '9')
21722 field = field * 10 + c - '0';
21724 /* Don't pad beyond the total padding allowed. */
21725 if (field_width - n > 0 && field > field_width - n)
21726 field = field_width - n;
21728 /* Note that either PRECISION <= 0 or N < PRECISION. */
21729 prec = precision - n;
21731 if (c == 'M')
21732 n += display_mode_element (it, depth, field, prec,
21733 Vglobal_mode_string, props,
21734 risky);
21735 else if (c != 0)
21737 bool multibyte;
21738 ptrdiff_t bytepos, charpos;
21739 const char *spec;
21740 Lisp_Object string;
21742 bytepos = percent_position;
21743 charpos = (STRING_MULTIBYTE (elt)
21744 ? string_byte_to_char (elt, bytepos)
21745 : bytepos);
21746 spec = decode_mode_spec (it->w, c, field, &string);
21747 multibyte = STRINGP (string) && STRING_MULTIBYTE (string);
21749 switch (mode_line_target)
21751 case MODE_LINE_NOPROP:
21752 case MODE_LINE_TITLE:
21753 n += store_mode_line_noprop (spec, field, prec);
21754 break;
21755 case MODE_LINE_STRING:
21757 Lisp_Object tem = build_string (spec);
21758 props = Ftext_properties_at (make_number (charpos), elt);
21759 /* Should only keep face property in props */
21760 n += store_mode_line_string (NULL, tem, 0, field, prec, props);
21762 break;
21763 case MODE_LINE_DISPLAY:
21765 int nglyphs_before, nwritten;
21767 nglyphs_before = it->glyph_row->used[TEXT_AREA];
21768 nwritten = display_string (spec, string, elt,
21769 charpos, 0, it,
21770 field, prec, 0,
21771 multibyte);
21773 /* Assign to the glyphs written above the
21774 string where the `%x' came from, position
21775 of the `%'. */
21776 if (nwritten > 0)
21778 struct glyph *glyph
21779 = (it->glyph_row->glyphs[TEXT_AREA]
21780 + nglyphs_before);
21781 int i;
21783 for (i = 0; i < nwritten; ++i)
21785 glyph[i].object = elt;
21786 glyph[i].charpos = charpos;
21789 n += nwritten;
21792 break;
21795 else /* c == 0 */
21796 break;
21800 break;
21802 case Lisp_Symbol:
21803 /* A symbol: process the value of the symbol recursively
21804 as if it appeared here directly. Avoid error if symbol void.
21805 Special case: if value of symbol is a string, output the string
21806 literally. */
21808 register Lisp_Object tem;
21810 /* If the variable is not marked as risky to set
21811 then its contents are risky to use. */
21812 if (NILP (Fget (elt, Qrisky_local_variable)))
21813 risky = 1;
21815 tem = Fboundp (elt);
21816 if (!NILP (tem))
21818 tem = Fsymbol_value (elt);
21819 /* If value is a string, output that string literally:
21820 don't check for % within it. */
21821 if (STRINGP (tem))
21822 literal = 1;
21824 if (!EQ (tem, elt))
21826 /* Give up right away for nil or t. */
21827 elt = tem;
21828 goto tail_recurse;
21832 break;
21834 case Lisp_Cons:
21836 register Lisp_Object car, tem;
21838 /* A cons cell: five distinct cases.
21839 If first element is :eval or :propertize, do something special.
21840 If first element is a string or a cons, process all the elements
21841 and effectively concatenate them.
21842 If first element is a negative number, truncate displaying cdr to
21843 at most that many characters. If positive, pad (with spaces)
21844 to at least that many characters.
21845 If first element is a symbol, process the cadr or caddr recursively
21846 according to whether the symbol's value is non-nil or nil. */
21847 car = XCAR (elt);
21848 if (EQ (car, QCeval))
21850 /* An element of the form (:eval FORM) means evaluate FORM
21851 and use the result as mode line elements. */
21853 if (risky)
21854 break;
21856 if (CONSP (XCDR (elt)))
21858 Lisp_Object spec;
21859 spec = safe_eval (XCAR (XCDR (elt)));
21860 n += display_mode_element (it, depth, field_width - n,
21861 precision - n, spec, props,
21862 risky);
21865 else if (EQ (car, QCpropertize))
21867 /* An element of the form (:propertize ELT PROPS...)
21868 means display ELT but applying properties PROPS. */
21870 if (risky)
21871 break;
21873 if (CONSP (XCDR (elt)))
21874 n += display_mode_element (it, depth, field_width - n,
21875 precision - n, XCAR (XCDR (elt)),
21876 XCDR (XCDR (elt)), risky);
21878 else if (SYMBOLP (car))
21880 tem = Fboundp (car);
21881 elt = XCDR (elt);
21882 if (!CONSP (elt))
21883 goto invalid;
21884 /* elt is now the cdr, and we know it is a cons cell.
21885 Use its car if CAR has a non-nil value. */
21886 if (!NILP (tem))
21888 tem = Fsymbol_value (car);
21889 if (!NILP (tem))
21891 elt = XCAR (elt);
21892 goto tail_recurse;
21895 /* Symbol's value is nil (or symbol is unbound)
21896 Get the cddr of the original list
21897 and if possible find the caddr and use that. */
21898 elt = XCDR (elt);
21899 if (NILP (elt))
21900 break;
21901 else if (!CONSP (elt))
21902 goto invalid;
21903 elt = XCAR (elt);
21904 goto tail_recurse;
21906 else if (INTEGERP (car))
21908 register int lim = XINT (car);
21909 elt = XCDR (elt);
21910 if (lim < 0)
21912 /* Negative int means reduce maximum width. */
21913 if (precision <= 0)
21914 precision = -lim;
21915 else
21916 precision = min (precision, -lim);
21918 else if (lim > 0)
21920 /* Padding specified. Don't let it be more than
21921 current maximum. */
21922 if (precision > 0)
21923 lim = min (precision, lim);
21925 /* If that's more padding than already wanted, queue it.
21926 But don't reduce padding already specified even if
21927 that is beyond the current truncation point. */
21928 field_width = max (lim, field_width);
21930 goto tail_recurse;
21932 else if (STRINGP (car) || CONSP (car))
21934 Lisp_Object halftail = elt;
21935 int len = 0;
21937 while (CONSP (elt)
21938 && (precision <= 0 || n < precision))
21940 n += display_mode_element (it, depth,
21941 /* Do padding only after the last
21942 element in the list. */
21943 (! CONSP (XCDR (elt))
21944 ? field_width - n
21945 : 0),
21946 precision - n, XCAR (elt),
21947 props, risky);
21948 elt = XCDR (elt);
21949 len++;
21950 if ((len & 1) == 0)
21951 halftail = XCDR (halftail);
21952 /* Check for cycle. */
21953 if (EQ (halftail, elt))
21954 break;
21958 break;
21960 default:
21961 invalid:
21962 elt = build_string ("*invalid*");
21963 goto tail_recurse;
21966 /* Pad to FIELD_WIDTH. */
21967 if (field_width > 0 && n < field_width)
21969 switch (mode_line_target)
21971 case MODE_LINE_NOPROP:
21972 case MODE_LINE_TITLE:
21973 n += store_mode_line_noprop ("", field_width - n, 0);
21974 break;
21975 case MODE_LINE_STRING:
21976 n += store_mode_line_string ("", Qnil, 0, field_width - n, 0, Qnil);
21977 break;
21978 case MODE_LINE_DISPLAY:
21979 n += display_string ("", Qnil, Qnil, 0, 0, it, field_width - n,
21980 0, 0, 0);
21981 break;
21985 return n;
21988 /* Store a mode-line string element in mode_line_string_list.
21990 If STRING is non-null, display that C string. Otherwise, the Lisp
21991 string LISP_STRING is displayed.
21993 FIELD_WIDTH is the minimum number of output glyphs to produce.
21994 If STRING has fewer characters than FIELD_WIDTH, pad to the right
21995 with spaces. FIELD_WIDTH <= 0 means don't pad.
21997 PRECISION is the maximum number of characters to output from
21998 STRING. PRECISION <= 0 means don't truncate the string.
22000 If COPY_STRING is non-zero, make a copy of LISP_STRING before adding
22001 properties to the string.
22003 PROPS are the properties to add to the string.
22004 The mode_line_string_face face property is always added to the string.
22007 static int
22008 store_mode_line_string (const char *string, Lisp_Object lisp_string, int copy_string,
22009 int field_width, int precision, Lisp_Object props)
22011 ptrdiff_t len;
22012 int n = 0;
22014 if (string != NULL)
22016 len = strlen (string);
22017 if (precision > 0 && len > precision)
22018 len = precision;
22019 lisp_string = make_string (string, len);
22020 if (NILP (props))
22021 props = mode_line_string_face_prop;
22022 else if (!NILP (mode_line_string_face))
22024 Lisp_Object face = Fplist_get (props, Qface);
22025 props = Fcopy_sequence (props);
22026 if (NILP (face))
22027 face = mode_line_string_face;
22028 else
22029 face = list2 (face, mode_line_string_face);
22030 props = Fplist_put (props, Qface, face);
22032 Fadd_text_properties (make_number (0), make_number (len),
22033 props, lisp_string);
22035 else
22037 len = XFASTINT (Flength (lisp_string));
22038 if (precision > 0 && len > precision)
22040 len = precision;
22041 lisp_string = Fsubstring (lisp_string, make_number (0), make_number (len));
22042 precision = -1;
22044 if (!NILP (mode_line_string_face))
22046 Lisp_Object face;
22047 if (NILP (props))
22048 props = Ftext_properties_at (make_number (0), lisp_string);
22049 face = Fplist_get (props, Qface);
22050 if (NILP (face))
22051 face = mode_line_string_face;
22052 else
22053 face = list2 (face, mode_line_string_face);
22054 props = list2 (Qface, face);
22055 if (copy_string)
22056 lisp_string = Fcopy_sequence (lisp_string);
22058 if (!NILP (props))
22059 Fadd_text_properties (make_number (0), make_number (len),
22060 props, lisp_string);
22063 if (len > 0)
22065 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
22066 n += len;
22069 if (field_width > len)
22071 field_width -= len;
22072 lisp_string = Fmake_string (make_number (field_width), make_number (' '));
22073 if (!NILP (props))
22074 Fadd_text_properties (make_number (0), make_number (field_width),
22075 props, lisp_string);
22076 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
22077 n += field_width;
22080 return n;
22084 DEFUN ("format-mode-line", Fformat_mode_line, Sformat_mode_line,
22085 1, 4, 0,
22086 doc: /* Format a string out of a mode line format specification.
22087 First arg FORMAT specifies the mode line format (see `mode-line-format'
22088 for details) to use.
22090 By default, the format is evaluated for the currently selected window.
22092 Optional second arg FACE specifies the face property to put on all
22093 characters for which no face is specified. The value nil means the
22094 default face. The value t means whatever face the window's mode line
22095 currently uses (either `mode-line' or `mode-line-inactive',
22096 depending on whether the window is the selected window or not).
22097 An integer value means the value string has no text
22098 properties.
22100 Optional third and fourth args WINDOW and BUFFER specify the window
22101 and buffer to use as the context for the formatting (defaults
22102 are the selected window and the WINDOW's buffer). */)
22103 (Lisp_Object format, Lisp_Object face,
22104 Lisp_Object window, Lisp_Object buffer)
22106 struct it it;
22107 int len;
22108 struct window *w;
22109 struct buffer *old_buffer = NULL;
22110 int face_id;
22111 int no_props = INTEGERP (face);
22112 ptrdiff_t count = SPECPDL_INDEX ();
22113 Lisp_Object str;
22114 int string_start = 0;
22116 w = decode_any_window (window);
22117 XSETWINDOW (window, w);
22119 if (NILP (buffer))
22120 buffer = w->contents;
22121 CHECK_BUFFER (buffer);
22123 /* Make formatting the modeline a non-op when noninteractive, otherwise
22124 there will be problems later caused by a partially initialized frame. */
22125 if (NILP (format) || noninteractive)
22126 return empty_unibyte_string;
22128 if (no_props)
22129 face = Qnil;
22131 face_id = (NILP (face) || EQ (face, Qdefault)) ? DEFAULT_FACE_ID
22132 : EQ (face, Qt) ? (EQ (window, selected_window)
22133 ? MODE_LINE_FACE_ID : MODE_LINE_INACTIVE_FACE_ID)
22134 : EQ (face, Qmode_line) ? MODE_LINE_FACE_ID
22135 : EQ (face, Qmode_line_inactive) ? MODE_LINE_INACTIVE_FACE_ID
22136 : EQ (face, Qheader_line) ? HEADER_LINE_FACE_ID
22137 : EQ (face, Qtool_bar) ? TOOL_BAR_FACE_ID
22138 : DEFAULT_FACE_ID;
22140 old_buffer = current_buffer;
22142 /* Save things including mode_line_proptrans_alist,
22143 and set that to nil so that we don't alter the outer value. */
22144 record_unwind_protect (unwind_format_mode_line,
22145 format_mode_line_unwind_data
22146 (XFRAME (WINDOW_FRAME (w)),
22147 old_buffer, selected_window, 1));
22148 mode_line_proptrans_alist = Qnil;
22150 Fselect_window (window, Qt);
22151 set_buffer_internal_1 (XBUFFER (buffer));
22153 init_iterator (&it, w, -1, -1, NULL, face_id);
22155 if (no_props)
22157 mode_line_target = MODE_LINE_NOPROP;
22158 mode_line_string_face_prop = Qnil;
22159 mode_line_string_list = Qnil;
22160 string_start = MODE_LINE_NOPROP_LEN (0);
22162 else
22164 mode_line_target = MODE_LINE_STRING;
22165 mode_line_string_list = Qnil;
22166 mode_line_string_face = face;
22167 mode_line_string_face_prop
22168 = NILP (face) ? Qnil : list2 (Qface, face);
22171 push_kboard (FRAME_KBOARD (it.f));
22172 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
22173 pop_kboard ();
22175 if (no_props)
22177 len = MODE_LINE_NOPROP_LEN (string_start);
22178 str = make_string (mode_line_noprop_buf + string_start, len);
22180 else
22182 mode_line_string_list = Fnreverse (mode_line_string_list);
22183 str = Fmapconcat (intern ("identity"), mode_line_string_list,
22184 empty_unibyte_string);
22187 unbind_to (count, Qnil);
22188 return str;
22191 /* Write a null-terminated, right justified decimal representation of
22192 the positive integer D to BUF using a minimal field width WIDTH. */
22194 static void
22195 pint2str (register char *buf, register int width, register ptrdiff_t d)
22197 register char *p = buf;
22199 if (d <= 0)
22200 *p++ = '0';
22201 else
22203 while (d > 0)
22205 *p++ = d % 10 + '0';
22206 d /= 10;
22210 for (width -= (int) (p - buf); width > 0; --width)
22211 *p++ = ' ';
22212 *p-- = '\0';
22213 while (p > buf)
22215 d = *buf;
22216 *buf++ = *p;
22217 *p-- = d;
22221 /* Write a null-terminated, right justified decimal and "human
22222 readable" representation of the nonnegative integer D to BUF using
22223 a minimal field width WIDTH. D should be smaller than 999.5e24. */
22225 static const char power_letter[] =
22227 0, /* no letter */
22228 'k', /* kilo */
22229 'M', /* mega */
22230 'G', /* giga */
22231 'T', /* tera */
22232 'P', /* peta */
22233 'E', /* exa */
22234 'Z', /* zetta */
22235 'Y' /* yotta */
22238 static void
22239 pint2hrstr (char *buf, int width, ptrdiff_t d)
22241 /* We aim to represent the nonnegative integer D as
22242 QUOTIENT.TENTHS * 10 ^ (3 * EXPONENT). */
22243 ptrdiff_t quotient = d;
22244 int remainder = 0;
22245 /* -1 means: do not use TENTHS. */
22246 int tenths = -1;
22247 int exponent = 0;
22249 /* Length of QUOTIENT.TENTHS as a string. */
22250 int length;
22252 char * psuffix;
22253 char * p;
22255 if (quotient >= 1000)
22257 /* Scale to the appropriate EXPONENT. */
22260 remainder = quotient % 1000;
22261 quotient /= 1000;
22262 exponent++;
22264 while (quotient >= 1000);
22266 /* Round to nearest and decide whether to use TENTHS or not. */
22267 if (quotient <= 9)
22269 tenths = remainder / 100;
22270 if (remainder % 100 >= 50)
22272 if (tenths < 9)
22273 tenths++;
22274 else
22276 quotient++;
22277 if (quotient == 10)
22278 tenths = -1;
22279 else
22280 tenths = 0;
22284 else
22285 if (remainder >= 500)
22287 if (quotient < 999)
22288 quotient++;
22289 else
22291 quotient = 1;
22292 exponent++;
22293 tenths = 0;
22298 /* Calculate the LENGTH of QUOTIENT.TENTHS as a string. */
22299 if (tenths == -1 && quotient <= 99)
22300 if (quotient <= 9)
22301 length = 1;
22302 else
22303 length = 2;
22304 else
22305 length = 3;
22306 p = psuffix = buf + max (width, length);
22308 /* Print EXPONENT. */
22309 *psuffix++ = power_letter[exponent];
22310 *psuffix = '\0';
22312 /* Print TENTHS. */
22313 if (tenths >= 0)
22315 *--p = '0' + tenths;
22316 *--p = '.';
22319 /* Print QUOTIENT. */
22322 int digit = quotient % 10;
22323 *--p = '0' + digit;
22325 while ((quotient /= 10) != 0);
22327 /* Print leading spaces. */
22328 while (buf < p)
22329 *--p = ' ';
22332 /* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
22333 If EOL_FLAG is 1, set also a mnemonic character for end-of-line
22334 type of CODING_SYSTEM. Return updated pointer into BUF. */
22336 static unsigned char invalid_eol_type[] = "(*invalid*)";
22338 static char *
22339 decode_mode_spec_coding (Lisp_Object coding_system, register char *buf, int eol_flag)
22341 Lisp_Object val;
22342 bool multibyte = !NILP (BVAR (current_buffer, enable_multibyte_characters));
22343 const unsigned char *eol_str;
22344 int eol_str_len;
22345 /* The EOL conversion we are using. */
22346 Lisp_Object eoltype;
22348 val = CODING_SYSTEM_SPEC (coding_system);
22349 eoltype = Qnil;
22351 if (!VECTORP (val)) /* Not yet decided. */
22353 *buf++ = multibyte ? '-' : ' ';
22354 if (eol_flag)
22355 eoltype = eol_mnemonic_undecided;
22356 /* Don't mention EOL conversion if it isn't decided. */
22358 else
22360 Lisp_Object attrs;
22361 Lisp_Object eolvalue;
22363 attrs = AREF (val, 0);
22364 eolvalue = AREF (val, 2);
22366 *buf++ = multibyte
22367 ? XFASTINT (CODING_ATTR_MNEMONIC (attrs))
22368 : ' ';
22370 if (eol_flag)
22372 /* The EOL conversion that is normal on this system. */
22374 if (NILP (eolvalue)) /* Not yet decided. */
22375 eoltype = eol_mnemonic_undecided;
22376 else if (VECTORP (eolvalue)) /* Not yet decided. */
22377 eoltype = eol_mnemonic_undecided;
22378 else /* eolvalue is Qunix, Qdos, or Qmac. */
22379 eoltype = (EQ (eolvalue, Qunix)
22380 ? eol_mnemonic_unix
22381 : (EQ (eolvalue, Qdos) == 1
22382 ? eol_mnemonic_dos : eol_mnemonic_mac));
22386 if (eol_flag)
22388 /* Mention the EOL conversion if it is not the usual one. */
22389 if (STRINGP (eoltype))
22391 eol_str = SDATA (eoltype);
22392 eol_str_len = SBYTES (eoltype);
22394 else if (CHARACTERP (eoltype))
22396 unsigned char *tmp = alloca (MAX_MULTIBYTE_LENGTH);
22397 int c = XFASTINT (eoltype);
22398 eol_str_len = CHAR_STRING (c, tmp);
22399 eol_str = tmp;
22401 else
22403 eol_str = invalid_eol_type;
22404 eol_str_len = sizeof (invalid_eol_type) - 1;
22406 memcpy (buf, eol_str, eol_str_len);
22407 buf += eol_str_len;
22410 return buf;
22413 /* Return a string for the output of a mode line %-spec for window W,
22414 generated by character C. FIELD_WIDTH > 0 means pad the string
22415 returned with spaces to that value. Return a Lisp string in
22416 *STRING if the resulting string is taken from that Lisp string.
22418 Note we operate on the current buffer for most purposes. */
22420 static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
22422 static const char *
22423 decode_mode_spec (struct window *w, register int c, int field_width,
22424 Lisp_Object *string)
22426 Lisp_Object obj;
22427 struct frame *f = XFRAME (WINDOW_FRAME (w));
22428 char *decode_mode_spec_buf = f->decode_mode_spec_buffer;
22429 /* We are going to use f->decode_mode_spec_buffer as the buffer to
22430 produce strings from numerical values, so limit preposterously
22431 large values of FIELD_WIDTH to avoid overrunning the buffer's
22432 end. The size of the buffer is enough for FRAME_MESSAGE_BUF_SIZE
22433 bytes plus the terminating null. */
22434 int width = min (field_width, FRAME_MESSAGE_BUF_SIZE (f));
22435 struct buffer *b = current_buffer;
22437 obj = Qnil;
22438 *string = Qnil;
22440 switch (c)
22442 case '*':
22443 if (!NILP (BVAR (b, read_only)))
22444 return "%";
22445 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
22446 return "*";
22447 return "-";
22449 case '+':
22450 /* This differs from %* only for a modified read-only buffer. */
22451 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
22452 return "*";
22453 if (!NILP (BVAR (b, read_only)))
22454 return "%";
22455 return "-";
22457 case '&':
22458 /* This differs from %* in ignoring read-only-ness. */
22459 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
22460 return "*";
22461 return "-";
22463 case '%':
22464 return "%";
22466 case '[':
22468 int i;
22469 char *p;
22471 if (command_loop_level > 5)
22472 return "[[[... ";
22473 p = decode_mode_spec_buf;
22474 for (i = 0; i < command_loop_level; i++)
22475 *p++ = '[';
22476 *p = 0;
22477 return decode_mode_spec_buf;
22480 case ']':
22482 int i;
22483 char *p;
22485 if (command_loop_level > 5)
22486 return " ...]]]";
22487 p = decode_mode_spec_buf;
22488 for (i = 0; i < command_loop_level; i++)
22489 *p++ = ']';
22490 *p = 0;
22491 return decode_mode_spec_buf;
22494 case '-':
22496 register int i;
22498 /* Let lots_of_dashes be a string of infinite length. */
22499 if (mode_line_target == MODE_LINE_NOPROP
22500 || mode_line_target == MODE_LINE_STRING)
22501 return "--";
22502 if (field_width <= 0
22503 || field_width > sizeof (lots_of_dashes))
22505 for (i = 0; i < FRAME_MESSAGE_BUF_SIZE (f) - 1; ++i)
22506 decode_mode_spec_buf[i] = '-';
22507 decode_mode_spec_buf[i] = '\0';
22508 return decode_mode_spec_buf;
22510 else
22511 return lots_of_dashes;
22514 case 'b':
22515 obj = BVAR (b, name);
22516 break;
22518 case 'c':
22519 /* %c and %l are ignored in `frame-title-format'.
22520 (In redisplay_internal, the frame title is drawn _before_ the
22521 windows are updated, so the stuff which depends on actual
22522 window contents (such as %l) may fail to render properly, or
22523 even crash emacs.) */
22524 if (mode_line_target == MODE_LINE_TITLE)
22525 return "";
22526 else
22528 ptrdiff_t col = current_column ();
22529 w->column_number_displayed = col;
22530 pint2str (decode_mode_spec_buf, width, col);
22531 return decode_mode_spec_buf;
22534 case 'e':
22535 #ifndef SYSTEM_MALLOC
22537 if (NILP (Vmemory_full))
22538 return "";
22539 else
22540 return "!MEM FULL! ";
22542 #else
22543 return "";
22544 #endif
22546 case 'F':
22547 /* %F displays the frame name. */
22548 if (!NILP (f->title))
22549 return SSDATA (f->title);
22550 if (f->explicit_name || ! FRAME_WINDOW_P (f))
22551 return SSDATA (f->name);
22552 return "Emacs";
22554 case 'f':
22555 obj = BVAR (b, filename);
22556 break;
22558 case 'i':
22560 ptrdiff_t size = ZV - BEGV;
22561 pint2str (decode_mode_spec_buf, width, size);
22562 return decode_mode_spec_buf;
22565 case 'I':
22567 ptrdiff_t size = ZV - BEGV;
22568 pint2hrstr (decode_mode_spec_buf, width, size);
22569 return decode_mode_spec_buf;
22572 case 'l':
22574 ptrdiff_t startpos, startpos_byte, line, linepos, linepos_byte;
22575 ptrdiff_t topline, nlines, height;
22576 ptrdiff_t junk;
22578 /* %c and %l are ignored in `frame-title-format'. */
22579 if (mode_line_target == MODE_LINE_TITLE)
22580 return "";
22582 startpos = marker_position (w->start);
22583 startpos_byte = marker_byte_position (w->start);
22584 height = WINDOW_TOTAL_LINES (w);
22586 /* If we decided that this buffer isn't suitable for line numbers,
22587 don't forget that too fast. */
22588 if (w->base_line_pos == -1)
22589 goto no_value;
22591 /* If the buffer is very big, don't waste time. */
22592 if (INTEGERP (Vline_number_display_limit)
22593 && BUF_ZV (b) - BUF_BEGV (b) > XINT (Vline_number_display_limit))
22595 w->base_line_pos = 0;
22596 w->base_line_number = 0;
22597 goto no_value;
22600 if (w->base_line_number > 0
22601 && w->base_line_pos > 0
22602 && w->base_line_pos <= startpos)
22604 line = w->base_line_number;
22605 linepos = w->base_line_pos;
22606 linepos_byte = buf_charpos_to_bytepos (b, linepos);
22608 else
22610 line = 1;
22611 linepos = BUF_BEGV (b);
22612 linepos_byte = BUF_BEGV_BYTE (b);
22615 /* Count lines from base line to window start position. */
22616 nlines = display_count_lines (linepos_byte,
22617 startpos_byte,
22618 startpos, &junk);
22620 topline = nlines + line;
22622 /* Determine a new base line, if the old one is too close
22623 or too far away, or if we did not have one.
22624 "Too close" means it's plausible a scroll-down would
22625 go back past it. */
22626 if (startpos == BUF_BEGV (b))
22628 w->base_line_number = topline;
22629 w->base_line_pos = BUF_BEGV (b);
22631 else if (nlines < height + 25 || nlines > height * 3 + 50
22632 || linepos == BUF_BEGV (b))
22634 ptrdiff_t limit = BUF_BEGV (b);
22635 ptrdiff_t limit_byte = BUF_BEGV_BYTE (b);
22636 ptrdiff_t position;
22637 ptrdiff_t distance =
22638 (height * 2 + 30) * line_number_display_limit_width;
22640 if (startpos - distance > limit)
22642 limit = startpos - distance;
22643 limit_byte = CHAR_TO_BYTE (limit);
22646 nlines = display_count_lines (startpos_byte,
22647 limit_byte,
22648 - (height * 2 + 30),
22649 &position);
22650 /* If we couldn't find the lines we wanted within
22651 line_number_display_limit_width chars per line,
22652 give up on line numbers for this window. */
22653 if (position == limit_byte && limit == startpos - distance)
22655 w->base_line_pos = -1;
22656 w->base_line_number = 0;
22657 goto no_value;
22660 w->base_line_number = topline - nlines;
22661 w->base_line_pos = BYTE_TO_CHAR (position);
22664 /* Now count lines from the start pos to point. */
22665 nlines = display_count_lines (startpos_byte,
22666 PT_BYTE, PT, &junk);
22668 /* Record that we did display the line number. */
22669 line_number_displayed = 1;
22671 /* Make the string to show. */
22672 pint2str (decode_mode_spec_buf, width, topline + nlines);
22673 return decode_mode_spec_buf;
22674 no_value:
22676 char* p = decode_mode_spec_buf;
22677 int pad = width - 2;
22678 while (pad-- > 0)
22679 *p++ = ' ';
22680 *p++ = '?';
22681 *p++ = '?';
22682 *p = '\0';
22683 return decode_mode_spec_buf;
22686 break;
22688 case 'm':
22689 obj = BVAR (b, mode_name);
22690 break;
22692 case 'n':
22693 if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
22694 return " Narrow";
22695 break;
22697 case 'p':
22699 ptrdiff_t pos = marker_position (w->start);
22700 ptrdiff_t total = BUF_ZV (b) - BUF_BEGV (b);
22702 if (w->window_end_pos <= BUF_Z (b) - BUF_ZV (b))
22704 if (pos <= BUF_BEGV (b))
22705 return "All";
22706 else
22707 return "Bottom";
22709 else if (pos <= BUF_BEGV (b))
22710 return "Top";
22711 else
22713 if (total > 1000000)
22714 /* Do it differently for a large value, to avoid overflow. */
22715 total = ((pos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
22716 else
22717 total = ((pos - BUF_BEGV (b)) * 100 + total - 1) / total;
22718 /* We can't normally display a 3-digit number,
22719 so get us a 2-digit number that is close. */
22720 if (total == 100)
22721 total = 99;
22722 sprintf (decode_mode_spec_buf, "%2"pD"d%%", total);
22723 return decode_mode_spec_buf;
22727 /* Display percentage of size above the bottom of the screen. */
22728 case 'P':
22730 ptrdiff_t toppos = marker_position (w->start);
22731 ptrdiff_t botpos = BUF_Z (b) - w->window_end_pos;
22732 ptrdiff_t total = BUF_ZV (b) - BUF_BEGV (b);
22734 if (botpos >= BUF_ZV (b))
22736 if (toppos <= BUF_BEGV (b))
22737 return "All";
22738 else
22739 return "Bottom";
22741 else
22743 if (total > 1000000)
22744 /* Do it differently for a large value, to avoid overflow. */
22745 total = ((botpos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
22746 else
22747 total = ((botpos - BUF_BEGV (b)) * 100 + total - 1) / total;
22748 /* We can't normally display a 3-digit number,
22749 so get us a 2-digit number that is close. */
22750 if (total == 100)
22751 total = 99;
22752 if (toppos <= BUF_BEGV (b))
22753 sprintf (decode_mode_spec_buf, "Top%2"pD"d%%", total);
22754 else
22755 sprintf (decode_mode_spec_buf, "%2"pD"d%%", total);
22756 return decode_mode_spec_buf;
22760 case 's':
22761 /* status of process */
22762 obj = Fget_buffer_process (Fcurrent_buffer ());
22763 if (NILP (obj))
22764 return "no process";
22765 #ifndef MSDOS
22766 obj = Fsymbol_name (Fprocess_status (obj));
22767 #endif
22768 break;
22770 case '@':
22772 ptrdiff_t count = inhibit_garbage_collection ();
22773 Lisp_Object val = call1 (intern ("file-remote-p"),
22774 BVAR (current_buffer, directory));
22775 unbind_to (count, Qnil);
22777 if (NILP (val))
22778 return "-";
22779 else
22780 return "@";
22783 case 'z':
22784 /* coding-system (not including end-of-line format) */
22785 case 'Z':
22786 /* coding-system (including end-of-line type) */
22788 int eol_flag = (c == 'Z');
22789 char *p = decode_mode_spec_buf;
22791 if (! FRAME_WINDOW_P (f))
22793 /* No need to mention EOL here--the terminal never needs
22794 to do EOL conversion. */
22795 p = decode_mode_spec_coding (CODING_ID_NAME
22796 (FRAME_KEYBOARD_CODING (f)->id),
22797 p, 0);
22798 p = decode_mode_spec_coding (CODING_ID_NAME
22799 (FRAME_TERMINAL_CODING (f)->id),
22800 p, 0);
22802 p = decode_mode_spec_coding (BVAR (b, buffer_file_coding_system),
22803 p, eol_flag);
22805 #if 0 /* This proves to be annoying; I think we can do without. -- rms. */
22806 #ifdef subprocesses
22807 obj = Fget_buffer_process (Fcurrent_buffer ());
22808 if (PROCESSP (obj))
22810 p = decode_mode_spec_coding
22811 (XPROCESS (obj)->decode_coding_system, p, eol_flag);
22812 p = decode_mode_spec_coding
22813 (XPROCESS (obj)->encode_coding_system, p, eol_flag);
22815 #endif /* subprocesses */
22816 #endif /* 0 */
22817 *p = 0;
22818 return decode_mode_spec_buf;
22822 if (STRINGP (obj))
22824 *string = obj;
22825 return SSDATA (obj);
22827 else
22828 return "";
22832 /* Count up to COUNT lines starting from START_BYTE. COUNT negative
22833 means count lines back from START_BYTE. But don't go beyond
22834 LIMIT_BYTE. Return the number of lines thus found (always
22835 nonnegative).
22837 Set *BYTE_POS_PTR to the byte position where we stopped. This is
22838 either the position COUNT lines after/before START_BYTE, if we
22839 found COUNT lines, or LIMIT_BYTE if we hit the limit before finding
22840 COUNT lines. */
22842 static ptrdiff_t
22843 display_count_lines (ptrdiff_t start_byte,
22844 ptrdiff_t limit_byte, ptrdiff_t count,
22845 ptrdiff_t *byte_pos_ptr)
22847 register unsigned char *cursor;
22848 unsigned char *base;
22850 register ptrdiff_t ceiling;
22851 register unsigned char *ceiling_addr;
22852 ptrdiff_t orig_count = count;
22854 /* If we are not in selective display mode,
22855 check only for newlines. */
22856 int selective_display = (!NILP (BVAR (current_buffer, selective_display))
22857 && !INTEGERP (BVAR (current_buffer, selective_display)));
22859 if (count > 0)
22861 while (start_byte < limit_byte)
22863 ceiling = BUFFER_CEILING_OF (start_byte);
22864 ceiling = min (limit_byte - 1, ceiling);
22865 ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
22866 base = (cursor = BYTE_POS_ADDR (start_byte));
22870 if (selective_display)
22872 while (*cursor != '\n' && *cursor != 015
22873 && ++cursor != ceiling_addr)
22874 continue;
22875 if (cursor == ceiling_addr)
22876 break;
22878 else
22880 cursor = memchr (cursor, '\n', ceiling_addr - cursor);
22881 if (! cursor)
22882 break;
22885 cursor++;
22887 if (--count == 0)
22889 start_byte += cursor - base;
22890 *byte_pos_ptr = start_byte;
22891 return orig_count;
22894 while (cursor < ceiling_addr);
22896 start_byte += ceiling_addr - base;
22899 else
22901 while (start_byte > limit_byte)
22903 ceiling = BUFFER_FLOOR_OF (start_byte - 1);
22904 ceiling = max (limit_byte, ceiling);
22905 ceiling_addr = BYTE_POS_ADDR (ceiling);
22906 base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
22907 while (1)
22909 if (selective_display)
22911 while (--cursor >= ceiling_addr
22912 && *cursor != '\n' && *cursor != 015)
22913 continue;
22914 if (cursor < ceiling_addr)
22915 break;
22917 else
22919 cursor = memrchr (ceiling_addr, '\n', cursor - ceiling_addr);
22920 if (! cursor)
22921 break;
22924 if (++count == 0)
22926 start_byte += cursor - base + 1;
22927 *byte_pos_ptr = start_byte;
22928 /* When scanning backwards, we should
22929 not count the newline posterior to which we stop. */
22930 return - orig_count - 1;
22933 start_byte += ceiling_addr - base;
22937 *byte_pos_ptr = limit_byte;
22939 if (count < 0)
22940 return - orig_count + count;
22941 return orig_count - count;
22947 /***********************************************************************
22948 Displaying strings
22949 ***********************************************************************/
22951 /* Display a NUL-terminated string, starting with index START.
22953 If STRING is non-null, display that C string. Otherwise, the Lisp
22954 string LISP_STRING is displayed. There's a case that STRING is
22955 non-null and LISP_STRING is not nil. It means STRING is a string
22956 data of LISP_STRING. In that case, we display LISP_STRING while
22957 ignoring its text properties.
22959 If FACE_STRING is not nil, FACE_STRING_POS is a position in
22960 FACE_STRING. Display STRING or LISP_STRING with the face at
22961 FACE_STRING_POS in FACE_STRING:
22963 Display the string in the environment given by IT, but use the
22964 standard display table, temporarily.
22966 FIELD_WIDTH is the minimum number of output glyphs to produce.
22967 If STRING has fewer characters than FIELD_WIDTH, pad to the right
22968 with spaces. If STRING has more characters, more than FIELD_WIDTH
22969 glyphs will be produced. FIELD_WIDTH <= 0 means don't pad.
22971 PRECISION is the maximum number of characters to output from
22972 STRING. PRECISION < 0 means don't truncate the string.
22974 This is roughly equivalent to printf format specifiers:
22976 FIELD_WIDTH PRECISION PRINTF
22977 ----------------------------------------
22978 -1 -1 %s
22979 -1 10 %.10s
22980 10 -1 %10s
22981 20 10 %20.10s
22983 MULTIBYTE zero means do not display multibyte chars, > 0 means do
22984 display them, and < 0 means obey the current buffer's value of
22985 enable_multibyte_characters.
22987 Value is the number of columns displayed. */
22989 static int
22990 display_string (const char *string, Lisp_Object lisp_string, Lisp_Object face_string,
22991 ptrdiff_t face_string_pos, ptrdiff_t start, struct it *it,
22992 int field_width, int precision, int max_x, int multibyte)
22994 int hpos_at_start = it->hpos;
22995 int saved_face_id = it->face_id;
22996 struct glyph_row *row = it->glyph_row;
22997 ptrdiff_t it_charpos;
22999 /* Initialize the iterator IT for iteration over STRING beginning
23000 with index START. */
23001 reseat_to_string (it, NILP (lisp_string) ? string : NULL, lisp_string, start,
23002 precision, field_width, multibyte);
23003 if (string && STRINGP (lisp_string))
23004 /* LISP_STRING is the one returned by decode_mode_spec. We should
23005 ignore its text properties. */
23006 it->stop_charpos = it->end_charpos;
23008 /* If displaying STRING, set up the face of the iterator from
23009 FACE_STRING, if that's given. */
23010 if (STRINGP (face_string))
23012 ptrdiff_t endptr;
23013 struct face *face;
23015 it->face_id
23016 = face_at_string_position (it->w, face_string, face_string_pos,
23017 0, &endptr, it->base_face_id, 0);
23018 face = FACE_FROM_ID (it->f, it->face_id);
23019 it->face_box_p = face->box != FACE_NO_BOX;
23022 /* Set max_x to the maximum allowed X position. Don't let it go
23023 beyond the right edge of the window. */
23024 if (max_x <= 0)
23025 max_x = it->last_visible_x;
23026 else
23027 max_x = min (max_x, it->last_visible_x);
23029 /* Skip over display elements that are not visible. because IT->w is
23030 hscrolled. */
23031 if (it->current_x < it->first_visible_x)
23032 move_it_in_display_line_to (it, 100000, it->first_visible_x,
23033 MOVE_TO_POS | MOVE_TO_X);
23035 row->ascent = it->max_ascent;
23036 row->height = it->max_ascent + it->max_descent;
23037 row->phys_ascent = it->max_phys_ascent;
23038 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
23039 row->extra_line_spacing = it->max_extra_line_spacing;
23041 if (STRINGP (it->string))
23042 it_charpos = IT_STRING_CHARPOS (*it);
23043 else
23044 it_charpos = IT_CHARPOS (*it);
23046 /* This condition is for the case that we are called with current_x
23047 past last_visible_x. */
23048 while (it->current_x < max_x)
23050 int x_before, x, n_glyphs_before, i, nglyphs;
23052 /* Get the next display element. */
23053 if (!get_next_display_element (it))
23054 break;
23056 /* Produce glyphs. */
23057 x_before = it->current_x;
23058 n_glyphs_before = row->used[TEXT_AREA];
23059 PRODUCE_GLYPHS (it);
23061 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
23062 i = 0;
23063 x = x_before;
23064 while (i < nglyphs)
23066 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
23068 if (it->line_wrap != TRUNCATE
23069 && x + glyph->pixel_width > max_x)
23071 /* End of continued line or max_x reached. */
23072 if (CHAR_GLYPH_PADDING_P (*glyph))
23074 /* A wide character is unbreakable. */
23075 if (row->reversed_p)
23076 unproduce_glyphs (it, row->used[TEXT_AREA]
23077 - n_glyphs_before);
23078 row->used[TEXT_AREA] = n_glyphs_before;
23079 it->current_x = x_before;
23081 else
23083 if (row->reversed_p)
23084 unproduce_glyphs (it, row->used[TEXT_AREA]
23085 - (n_glyphs_before + i));
23086 row->used[TEXT_AREA] = n_glyphs_before + i;
23087 it->current_x = x;
23089 break;
23091 else if (x + glyph->pixel_width >= it->first_visible_x)
23093 /* Glyph is at least partially visible. */
23094 ++it->hpos;
23095 if (x < it->first_visible_x)
23096 row->x = x - it->first_visible_x;
23098 else
23100 /* Glyph is off the left margin of the display area.
23101 Should not happen. */
23102 emacs_abort ();
23105 row->ascent = max (row->ascent, it->max_ascent);
23106 row->height = max (row->height, it->max_ascent + it->max_descent);
23107 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
23108 row->phys_height = max (row->phys_height,
23109 it->max_phys_ascent + it->max_phys_descent);
23110 row->extra_line_spacing = max (row->extra_line_spacing,
23111 it->max_extra_line_spacing);
23112 x += glyph->pixel_width;
23113 ++i;
23116 /* Stop if max_x reached. */
23117 if (i < nglyphs)
23118 break;
23120 /* Stop at line ends. */
23121 if (ITERATOR_AT_END_OF_LINE_P (it))
23123 it->continuation_lines_width = 0;
23124 break;
23127 set_iterator_to_next (it, 1);
23128 if (STRINGP (it->string))
23129 it_charpos = IT_STRING_CHARPOS (*it);
23130 else
23131 it_charpos = IT_CHARPOS (*it);
23133 /* Stop if truncating at the right edge. */
23134 if (it->line_wrap == TRUNCATE
23135 && it->current_x >= it->last_visible_x)
23137 /* Add truncation mark, but don't do it if the line is
23138 truncated at a padding space. */
23139 if (it_charpos < it->string_nchars)
23141 if (!FRAME_WINDOW_P (it->f))
23143 int ii, n;
23145 if (it->current_x > it->last_visible_x)
23147 if (!row->reversed_p)
23149 for (ii = row->used[TEXT_AREA] - 1; ii > 0; --ii)
23150 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][ii]))
23151 break;
23153 else
23155 for (ii = 0; ii < row->used[TEXT_AREA]; ii++)
23156 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][ii]))
23157 break;
23158 unproduce_glyphs (it, ii + 1);
23159 ii = row->used[TEXT_AREA] - (ii + 1);
23161 for (n = row->used[TEXT_AREA]; ii < n; ++ii)
23163 row->used[TEXT_AREA] = ii;
23164 produce_special_glyphs (it, IT_TRUNCATION);
23167 produce_special_glyphs (it, IT_TRUNCATION);
23169 row->truncated_on_right_p = 1;
23171 break;
23175 /* Maybe insert a truncation at the left. */
23176 if (it->first_visible_x
23177 && it_charpos > 0)
23179 if (!FRAME_WINDOW_P (it->f)
23180 || (row->reversed_p
23181 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
23182 : WINDOW_LEFT_FRINGE_WIDTH (it->w)) == 0)
23183 insert_left_trunc_glyphs (it);
23184 row->truncated_on_left_p = 1;
23187 it->face_id = saved_face_id;
23189 /* Value is number of columns displayed. */
23190 return it->hpos - hpos_at_start;
23195 /* This is like a combination of memq and assq. Return 1/2 if PROPVAL
23196 appears as an element of LIST or as the car of an element of LIST.
23197 If PROPVAL is a list, compare each element against LIST in that
23198 way, and return 1/2 if any element of PROPVAL is found in LIST.
23199 Otherwise return 0. This function cannot quit.
23200 The return value is 2 if the text is invisible but with an ellipsis
23201 and 1 if it's invisible and without an ellipsis. */
23204 invisible_p (register Lisp_Object propval, Lisp_Object list)
23206 register Lisp_Object tail, proptail;
23208 for (tail = list; CONSP (tail); tail = XCDR (tail))
23210 register Lisp_Object tem;
23211 tem = XCAR (tail);
23212 if (EQ (propval, tem))
23213 return 1;
23214 if (CONSP (tem) && EQ (propval, XCAR (tem)))
23215 return NILP (XCDR (tem)) ? 1 : 2;
23218 if (CONSP (propval))
23220 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
23222 Lisp_Object propelt;
23223 propelt = XCAR (proptail);
23224 for (tail = list; CONSP (tail); tail = XCDR (tail))
23226 register Lisp_Object tem;
23227 tem = XCAR (tail);
23228 if (EQ (propelt, tem))
23229 return 1;
23230 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
23231 return NILP (XCDR (tem)) ? 1 : 2;
23236 return 0;
23239 DEFUN ("invisible-p", Finvisible_p, Sinvisible_p, 1, 1, 0,
23240 doc: /* Non-nil if the property makes the text invisible.
23241 POS-OR-PROP can be a marker or number, in which case it is taken to be
23242 a position in the current buffer and the value of the `invisible' property
23243 is checked; or it can be some other value, which is then presumed to be the
23244 value of the `invisible' property of the text of interest.
23245 The non-nil value returned can be t for truly invisible text or something
23246 else if the text is replaced by an ellipsis. */)
23247 (Lisp_Object pos_or_prop)
23249 Lisp_Object prop
23250 = (NATNUMP (pos_or_prop) || MARKERP (pos_or_prop)
23251 ? Fget_char_property (pos_or_prop, Qinvisible, Qnil)
23252 : pos_or_prop);
23253 int invis = TEXT_PROP_MEANS_INVISIBLE (prop);
23254 return (invis == 0 ? Qnil
23255 : invis == 1 ? Qt
23256 : make_number (invis));
23259 /* Calculate a width or height in pixels from a specification using
23260 the following elements:
23262 SPEC ::=
23263 NUM - a (fractional) multiple of the default font width/height
23264 (NUM) - specifies exactly NUM pixels
23265 UNIT - a fixed number of pixels, see below.
23266 ELEMENT - size of a display element in pixels, see below.
23267 (NUM . SPEC) - equals NUM * SPEC
23268 (+ SPEC SPEC ...) - add pixel values
23269 (- SPEC SPEC ...) - subtract pixel values
23270 (- SPEC) - negate pixel value
23272 NUM ::=
23273 INT or FLOAT - a number constant
23274 SYMBOL - use symbol's (buffer local) variable binding.
23276 UNIT ::=
23277 in - pixels per inch *)
23278 mm - pixels per 1/1000 meter *)
23279 cm - pixels per 1/100 meter *)
23280 width - width of current font in pixels.
23281 height - height of current font in pixels.
23283 *) using the ratio(s) defined in display-pixels-per-inch.
23285 ELEMENT ::=
23287 left-fringe - left fringe width in pixels
23288 right-fringe - right fringe width in pixels
23290 left-margin - left margin width in pixels
23291 right-margin - right margin width in pixels
23293 scroll-bar - scroll-bar area width in pixels
23295 Examples:
23297 Pixels corresponding to 5 inches:
23298 (5 . in)
23300 Total width of non-text areas on left side of window (if scroll-bar is on left):
23301 '(space :width (+ left-fringe left-margin scroll-bar))
23303 Align to first text column (in header line):
23304 '(space :align-to 0)
23306 Align to middle of text area minus half the width of variable `my-image'
23307 containing a loaded image:
23308 '(space :align-to (0.5 . (- text my-image)))
23310 Width of left margin minus width of 1 character in the default font:
23311 '(space :width (- left-margin 1))
23313 Width of left margin minus width of 2 characters in the current font:
23314 '(space :width (- left-margin (2 . width)))
23316 Center 1 character over left-margin (in header line):
23317 '(space :align-to (+ left-margin (0.5 . left-margin) -0.5))
23319 Different ways to express width of left fringe plus left margin minus one pixel:
23320 '(space :width (- (+ left-fringe left-margin) (1)))
23321 '(space :width (+ left-fringe left-margin (- (1))))
23322 '(space :width (+ left-fringe left-margin (-1)))
23326 static int
23327 calc_pixel_width_or_height (double *res, struct it *it, Lisp_Object prop,
23328 struct font *font, int width_p, int *align_to)
23330 double pixels;
23332 #define OK_PIXELS(val) ((*res = (double)(val)), 1)
23333 #define OK_ALIGN_TO(val) ((*align_to = (int)(val)), 1)
23335 if (NILP (prop))
23336 return OK_PIXELS (0);
23338 eassert (FRAME_LIVE_P (it->f));
23340 if (SYMBOLP (prop))
23342 if (SCHARS (SYMBOL_NAME (prop)) == 2)
23344 char *unit = SSDATA (SYMBOL_NAME (prop));
23346 if (unit[0] == 'i' && unit[1] == 'n')
23347 pixels = 1.0;
23348 else if (unit[0] == 'm' && unit[1] == 'm')
23349 pixels = 25.4;
23350 else if (unit[0] == 'c' && unit[1] == 'm')
23351 pixels = 2.54;
23352 else
23353 pixels = 0;
23354 if (pixels > 0)
23356 double ppi = (width_p ? FRAME_RES_X (it->f)
23357 : FRAME_RES_Y (it->f));
23359 if (ppi > 0)
23360 return OK_PIXELS (ppi / pixels);
23361 return 0;
23365 #ifdef HAVE_WINDOW_SYSTEM
23366 if (EQ (prop, Qheight))
23367 return OK_PIXELS (font ? FONT_HEIGHT (font) : FRAME_LINE_HEIGHT (it->f));
23368 if (EQ (prop, Qwidth))
23369 return OK_PIXELS (font ? FONT_WIDTH (font) : FRAME_COLUMN_WIDTH (it->f));
23370 #else
23371 if (EQ (prop, Qheight) || EQ (prop, Qwidth))
23372 return OK_PIXELS (1);
23373 #endif
23375 if (EQ (prop, Qtext))
23376 return OK_PIXELS (width_p
23377 ? window_box_width (it->w, TEXT_AREA)
23378 : WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w));
23380 if (align_to && *align_to < 0)
23382 *res = 0;
23383 if (EQ (prop, Qleft))
23384 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA));
23385 if (EQ (prop, Qright))
23386 return OK_ALIGN_TO (window_box_right_offset (it->w, TEXT_AREA));
23387 if (EQ (prop, Qcenter))
23388 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA)
23389 + window_box_width (it->w, TEXT_AREA) / 2);
23390 if (EQ (prop, Qleft_fringe))
23391 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
23392 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (it->w)
23393 : window_box_right_offset (it->w, LEFT_MARGIN_AREA));
23394 if (EQ (prop, Qright_fringe))
23395 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
23396 ? window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
23397 : window_box_right_offset (it->w, TEXT_AREA));
23398 if (EQ (prop, Qleft_margin))
23399 return OK_ALIGN_TO (window_box_left_offset (it->w, LEFT_MARGIN_AREA));
23400 if (EQ (prop, Qright_margin))
23401 return OK_ALIGN_TO (window_box_left_offset (it->w, RIGHT_MARGIN_AREA));
23402 if (EQ (prop, Qscroll_bar))
23403 return OK_ALIGN_TO (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (it->w)
23405 : (window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
23406 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
23407 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
23408 : 0)));
23410 else
23412 if (EQ (prop, Qleft_fringe))
23413 return OK_PIXELS (WINDOW_LEFT_FRINGE_WIDTH (it->w));
23414 if (EQ (prop, Qright_fringe))
23415 return OK_PIXELS (WINDOW_RIGHT_FRINGE_WIDTH (it->w));
23416 if (EQ (prop, Qleft_margin))
23417 return OK_PIXELS (WINDOW_LEFT_MARGIN_WIDTH (it->w));
23418 if (EQ (prop, Qright_margin))
23419 return OK_PIXELS (WINDOW_RIGHT_MARGIN_WIDTH (it->w));
23420 if (EQ (prop, Qscroll_bar))
23421 return OK_PIXELS (WINDOW_SCROLL_BAR_AREA_WIDTH (it->w));
23424 prop = buffer_local_value_1 (prop, it->w->contents);
23425 if (EQ (prop, Qunbound))
23426 prop = Qnil;
23429 if (INTEGERP (prop) || FLOATP (prop))
23431 int base_unit = (width_p
23432 ? FRAME_COLUMN_WIDTH (it->f)
23433 : FRAME_LINE_HEIGHT (it->f));
23434 return OK_PIXELS (XFLOATINT (prop) * base_unit);
23437 if (CONSP (prop))
23439 Lisp_Object car = XCAR (prop);
23440 Lisp_Object cdr = XCDR (prop);
23442 if (SYMBOLP (car))
23444 #ifdef HAVE_WINDOW_SYSTEM
23445 if (FRAME_WINDOW_P (it->f)
23446 && valid_image_p (prop))
23448 ptrdiff_t id = lookup_image (it->f, prop);
23449 struct image *img = IMAGE_FROM_ID (it->f, id);
23451 return OK_PIXELS (width_p ? img->width : img->height);
23453 #endif
23454 if (EQ (car, Qplus) || EQ (car, Qminus))
23456 int first = 1;
23457 double px;
23459 pixels = 0;
23460 while (CONSP (cdr))
23462 if (!calc_pixel_width_or_height (&px, it, XCAR (cdr),
23463 font, width_p, align_to))
23464 return 0;
23465 if (first)
23466 pixels = (EQ (car, Qplus) ? px : -px), first = 0;
23467 else
23468 pixels += px;
23469 cdr = XCDR (cdr);
23471 if (EQ (car, Qminus))
23472 pixels = -pixels;
23473 return OK_PIXELS (pixels);
23476 car = buffer_local_value_1 (car, it->w->contents);
23477 if (EQ (car, Qunbound))
23478 car = Qnil;
23481 if (INTEGERP (car) || FLOATP (car))
23483 double fact;
23484 pixels = XFLOATINT (car);
23485 if (NILP (cdr))
23486 return OK_PIXELS (pixels);
23487 if (calc_pixel_width_or_height (&fact, it, cdr,
23488 font, width_p, align_to))
23489 return OK_PIXELS (pixels * fact);
23490 return 0;
23493 return 0;
23496 return 0;
23500 /***********************************************************************
23501 Glyph Display
23502 ***********************************************************************/
23504 #ifdef HAVE_WINDOW_SYSTEM
23506 #ifdef GLYPH_DEBUG
23508 void
23509 dump_glyph_string (struct glyph_string *s)
23511 fprintf (stderr, "glyph string\n");
23512 fprintf (stderr, " x, y, w, h = %d, %d, %d, %d\n",
23513 s->x, s->y, s->width, s->height);
23514 fprintf (stderr, " ybase = %d\n", s->ybase);
23515 fprintf (stderr, " hl = %d\n", s->hl);
23516 fprintf (stderr, " left overhang = %d, right = %d\n",
23517 s->left_overhang, s->right_overhang);
23518 fprintf (stderr, " nchars = %d\n", s->nchars);
23519 fprintf (stderr, " extends to end of line = %d\n",
23520 s->extends_to_end_of_line_p);
23521 fprintf (stderr, " font height = %d\n", FONT_HEIGHT (s->font));
23522 fprintf (stderr, " bg width = %d\n", s->background_width);
23525 #endif /* GLYPH_DEBUG */
23527 /* Initialize glyph string S. CHAR2B is a suitably allocated vector
23528 of XChar2b structures for S; it can't be allocated in
23529 init_glyph_string because it must be allocated via `alloca'. W
23530 is the window on which S is drawn. ROW and AREA are the glyph row
23531 and area within the row from which S is constructed. START is the
23532 index of the first glyph structure covered by S. HL is a
23533 face-override for drawing S. */
23535 #ifdef HAVE_NTGUI
23536 #define OPTIONAL_HDC(hdc) HDC hdc,
23537 #define DECLARE_HDC(hdc) HDC hdc;
23538 #define ALLOCATE_HDC(hdc, f) hdc = get_frame_dc ((f))
23539 #define RELEASE_HDC(hdc, f) release_frame_dc ((f), (hdc))
23540 #endif
23542 #ifndef OPTIONAL_HDC
23543 #define OPTIONAL_HDC(hdc)
23544 #define DECLARE_HDC(hdc)
23545 #define ALLOCATE_HDC(hdc, f)
23546 #define RELEASE_HDC(hdc, f)
23547 #endif
23549 static void
23550 init_glyph_string (struct glyph_string *s,
23551 OPTIONAL_HDC (hdc)
23552 XChar2b *char2b, struct window *w, struct glyph_row *row,
23553 enum glyph_row_area area, int start, enum draw_glyphs_face hl)
23555 memset (s, 0, sizeof *s);
23556 s->w = w;
23557 s->f = XFRAME (w->frame);
23558 #ifdef HAVE_NTGUI
23559 s->hdc = hdc;
23560 #endif
23561 s->display = FRAME_X_DISPLAY (s->f);
23562 s->window = FRAME_X_WINDOW (s->f);
23563 s->char2b = char2b;
23564 s->hl = hl;
23565 s->row = row;
23566 s->area = area;
23567 s->first_glyph = row->glyphs[area] + start;
23568 s->height = row->height;
23569 s->y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
23570 s->ybase = s->y + row->ascent;
23574 /* Append the list of glyph strings with head H and tail T to the list
23575 with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the result. */
23577 static void
23578 append_glyph_string_lists (struct glyph_string **head, struct glyph_string **tail,
23579 struct glyph_string *h, struct glyph_string *t)
23581 if (h)
23583 if (*head)
23584 (*tail)->next = h;
23585 else
23586 *head = h;
23587 h->prev = *tail;
23588 *tail = t;
23593 /* Prepend the list of glyph strings with head H and tail T to the
23594 list with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the
23595 result. */
23597 static void
23598 prepend_glyph_string_lists (struct glyph_string **head, struct glyph_string **tail,
23599 struct glyph_string *h, struct glyph_string *t)
23601 if (h)
23603 if (*head)
23604 (*head)->prev = t;
23605 else
23606 *tail = t;
23607 t->next = *head;
23608 *head = h;
23613 /* Append glyph string S to the list with head *HEAD and tail *TAIL.
23614 Set *HEAD and *TAIL to the resulting list. */
23616 static void
23617 append_glyph_string (struct glyph_string **head, struct glyph_string **tail,
23618 struct glyph_string *s)
23620 s->next = s->prev = NULL;
23621 append_glyph_string_lists (head, tail, s, s);
23625 /* Get face and two-byte form of character C in face FACE_ID on frame F.
23626 The encoding of C is returned in *CHAR2B. DISPLAY_P non-zero means
23627 make sure that X resources for the face returned are allocated.
23628 Value is a pointer to a realized face that is ready for display if
23629 DISPLAY_P is non-zero. */
23631 static struct face *
23632 get_char_face_and_encoding (struct frame *f, int c, int face_id,
23633 XChar2b *char2b, int display_p)
23635 struct face *face = FACE_FROM_ID (f, face_id);
23636 unsigned code = 0;
23638 if (face->font)
23640 code = face->font->driver->encode_char (face->font, c);
23642 if (code == FONT_INVALID_CODE)
23643 code = 0;
23645 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
23647 /* Make sure X resources of the face are allocated. */
23648 #ifdef HAVE_X_WINDOWS
23649 if (display_p)
23650 #endif
23652 eassert (face != NULL);
23653 PREPARE_FACE_FOR_DISPLAY (f, face);
23656 return face;
23660 /* Get face and two-byte form of character glyph GLYPH on frame F.
23661 The encoding of GLYPH->u.ch is returned in *CHAR2B. Value is
23662 a pointer to a realized face that is ready for display. */
23664 static struct face *
23665 get_glyph_face_and_encoding (struct frame *f, struct glyph *glyph,
23666 XChar2b *char2b, int *two_byte_p)
23668 struct face *face;
23669 unsigned code = 0;
23671 eassert (glyph->type == CHAR_GLYPH);
23672 face = FACE_FROM_ID (f, glyph->face_id);
23674 /* Make sure X resources of the face are allocated. */
23675 eassert (face != NULL);
23676 PREPARE_FACE_FOR_DISPLAY (f, face);
23678 if (two_byte_p)
23679 *two_byte_p = 0;
23681 if (face->font)
23683 if (CHAR_BYTE8_P (glyph->u.ch))
23684 code = CHAR_TO_BYTE8 (glyph->u.ch);
23685 else
23686 code = face->font->driver->encode_char (face->font, glyph->u.ch);
23688 if (code == FONT_INVALID_CODE)
23689 code = 0;
23692 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
23693 return face;
23697 /* Get glyph code of character C in FONT in the two-byte form CHAR2B.
23698 Return 1 if FONT has a glyph for C, otherwise return 0. */
23700 static int
23701 get_char_glyph_code (int c, struct font *font, XChar2b *char2b)
23703 unsigned code;
23705 if (CHAR_BYTE8_P (c))
23706 code = CHAR_TO_BYTE8 (c);
23707 else
23708 code = font->driver->encode_char (font, c);
23710 if (code == FONT_INVALID_CODE)
23711 return 0;
23712 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
23713 return 1;
23717 /* Fill glyph string S with composition components specified by S->cmp.
23719 BASE_FACE is the base face of the composition.
23720 S->cmp_from is the index of the first component for S.
23722 OVERLAPS non-zero means S should draw the foreground only, and use
23723 its physical height for clipping. See also draw_glyphs.
23725 Value is the index of a component not in S. */
23727 static int
23728 fill_composite_glyph_string (struct glyph_string *s, struct face *base_face,
23729 int overlaps)
23731 int i;
23732 /* For all glyphs of this composition, starting at the offset
23733 S->cmp_from, until we reach the end of the definition or encounter a
23734 glyph that requires the different face, add it to S. */
23735 struct face *face;
23737 eassert (s);
23739 s->for_overlaps = overlaps;
23740 s->face = NULL;
23741 s->font = NULL;
23742 for (i = s->cmp_from; i < s->cmp->glyph_len; i++)
23744 int c = COMPOSITION_GLYPH (s->cmp, i);
23746 /* TAB in a composition means display glyphs with padding space
23747 on the left or right. */
23748 if (c != '\t')
23750 int face_id = FACE_FOR_CHAR (s->f, base_face->ascii_face, c,
23751 -1, Qnil);
23753 face = get_char_face_and_encoding (s->f, c, face_id,
23754 s->char2b + i, 1);
23755 if (face)
23757 if (! s->face)
23759 s->face = face;
23760 s->font = s->face->font;
23762 else if (s->face != face)
23763 break;
23766 ++s->nchars;
23768 s->cmp_to = i;
23770 if (s->face == NULL)
23772 s->face = base_face->ascii_face;
23773 s->font = s->face->font;
23776 /* All glyph strings for the same composition has the same width,
23777 i.e. the width set for the first component of the composition. */
23778 s->width = s->first_glyph->pixel_width;
23780 /* If the specified font could not be loaded, use the frame's
23781 default font, but record the fact that we couldn't load it in
23782 the glyph string so that we can draw rectangles for the
23783 characters of the glyph string. */
23784 if (s->font == NULL)
23786 s->font_not_found_p = 1;
23787 s->font = FRAME_FONT (s->f);
23790 /* Adjust base line for subscript/superscript text. */
23791 s->ybase += s->first_glyph->voffset;
23793 /* This glyph string must always be drawn with 16-bit functions. */
23794 s->two_byte_p = 1;
23796 return s->cmp_to;
23799 static int
23800 fill_gstring_glyph_string (struct glyph_string *s, int face_id,
23801 int start, int end, int overlaps)
23803 struct glyph *glyph, *last;
23804 Lisp_Object lgstring;
23805 int i;
23807 s->for_overlaps = overlaps;
23808 glyph = s->row->glyphs[s->area] + start;
23809 last = s->row->glyphs[s->area] + end;
23810 s->cmp_id = glyph->u.cmp.id;
23811 s->cmp_from = glyph->slice.cmp.from;
23812 s->cmp_to = glyph->slice.cmp.to + 1;
23813 s->face = FACE_FROM_ID (s->f, face_id);
23814 lgstring = composition_gstring_from_id (s->cmp_id);
23815 s->font = XFONT_OBJECT (LGSTRING_FONT (lgstring));
23816 glyph++;
23817 while (glyph < last
23818 && glyph->u.cmp.automatic
23819 && glyph->u.cmp.id == s->cmp_id
23820 && s->cmp_to == glyph->slice.cmp.from)
23821 s->cmp_to = (glyph++)->slice.cmp.to + 1;
23823 for (i = s->cmp_from; i < s->cmp_to; i++)
23825 Lisp_Object lglyph = LGSTRING_GLYPH (lgstring, i);
23826 unsigned code = LGLYPH_CODE (lglyph);
23828 STORE_XCHAR2B ((s->char2b + i), code >> 8, code & 0xFF);
23830 s->width = composition_gstring_width (lgstring, s->cmp_from, s->cmp_to, NULL);
23831 return glyph - s->row->glyphs[s->area];
23835 /* Fill glyph string S from a sequence glyphs for glyphless characters.
23836 See the comment of fill_glyph_string for arguments.
23837 Value is the index of the first glyph not in S. */
23840 static int
23841 fill_glyphless_glyph_string (struct glyph_string *s, int face_id,
23842 int start, int end, int overlaps)
23844 struct glyph *glyph, *last;
23845 int voffset;
23847 eassert (s->first_glyph->type == GLYPHLESS_GLYPH);
23848 s->for_overlaps = overlaps;
23849 glyph = s->row->glyphs[s->area] + start;
23850 last = s->row->glyphs[s->area] + end;
23851 voffset = glyph->voffset;
23852 s->face = FACE_FROM_ID (s->f, face_id);
23853 s->font = s->face->font ? s->face->font : FRAME_FONT (s->f);
23854 s->nchars = 1;
23855 s->width = glyph->pixel_width;
23856 glyph++;
23857 while (glyph < last
23858 && glyph->type == GLYPHLESS_GLYPH
23859 && glyph->voffset == voffset
23860 && glyph->face_id == face_id)
23862 s->nchars++;
23863 s->width += glyph->pixel_width;
23864 glyph++;
23866 s->ybase += voffset;
23867 return glyph - s->row->glyphs[s->area];
23871 /* Fill glyph string S from a sequence of character glyphs.
23873 FACE_ID is the face id of the string. START is the index of the
23874 first glyph to consider, END is the index of the last + 1.
23875 OVERLAPS non-zero means S should draw the foreground only, and use
23876 its physical height for clipping. See also draw_glyphs.
23878 Value is the index of the first glyph not in S. */
23880 static int
23881 fill_glyph_string (struct glyph_string *s, int face_id,
23882 int start, int end, int overlaps)
23884 struct glyph *glyph, *last;
23885 int voffset;
23886 int glyph_not_available_p;
23888 eassert (s->f == XFRAME (s->w->frame));
23889 eassert (s->nchars == 0);
23890 eassert (start >= 0 && end > start);
23892 s->for_overlaps = overlaps;
23893 glyph = s->row->glyphs[s->area] + start;
23894 last = s->row->glyphs[s->area] + end;
23895 voffset = glyph->voffset;
23896 s->padding_p = glyph->padding_p;
23897 glyph_not_available_p = glyph->glyph_not_available_p;
23899 while (glyph < last
23900 && glyph->type == CHAR_GLYPH
23901 && glyph->voffset == voffset
23902 /* Same face id implies same font, nowadays. */
23903 && glyph->face_id == face_id
23904 && glyph->glyph_not_available_p == glyph_not_available_p)
23906 int two_byte_p;
23908 s->face = get_glyph_face_and_encoding (s->f, glyph,
23909 s->char2b + s->nchars,
23910 &two_byte_p);
23911 s->two_byte_p = two_byte_p;
23912 ++s->nchars;
23913 eassert (s->nchars <= end - start);
23914 s->width += glyph->pixel_width;
23915 if (glyph++->padding_p != s->padding_p)
23916 break;
23919 s->font = s->face->font;
23921 /* If the specified font could not be loaded, use the frame's font,
23922 but record the fact that we couldn't load it in
23923 S->font_not_found_p so that we can draw rectangles for the
23924 characters of the glyph string. */
23925 if (s->font == NULL || glyph_not_available_p)
23927 s->font_not_found_p = 1;
23928 s->font = FRAME_FONT (s->f);
23931 /* Adjust base line for subscript/superscript text. */
23932 s->ybase += voffset;
23934 eassert (s->face && s->face->gc);
23935 return glyph - s->row->glyphs[s->area];
23939 /* Fill glyph string S from image glyph S->first_glyph. */
23941 static void
23942 fill_image_glyph_string (struct glyph_string *s)
23944 eassert (s->first_glyph->type == IMAGE_GLYPH);
23945 s->img = IMAGE_FROM_ID (s->f, s->first_glyph->u.img_id);
23946 eassert (s->img);
23947 s->slice = s->first_glyph->slice.img;
23948 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
23949 s->font = s->face->font;
23950 s->width = s->first_glyph->pixel_width;
23952 /* Adjust base line for subscript/superscript text. */
23953 s->ybase += s->first_glyph->voffset;
23957 /* Fill glyph string S from a sequence of stretch glyphs.
23959 START is the index of the first glyph to consider,
23960 END is the index of the last + 1.
23962 Value is the index of the first glyph not in S. */
23964 static int
23965 fill_stretch_glyph_string (struct glyph_string *s, int start, int end)
23967 struct glyph *glyph, *last;
23968 int voffset, face_id;
23970 eassert (s->first_glyph->type == STRETCH_GLYPH);
23972 glyph = s->row->glyphs[s->area] + start;
23973 last = s->row->glyphs[s->area] + end;
23974 face_id = glyph->face_id;
23975 s->face = FACE_FROM_ID (s->f, face_id);
23976 s->font = s->face->font;
23977 s->width = glyph->pixel_width;
23978 s->nchars = 1;
23979 voffset = glyph->voffset;
23981 for (++glyph;
23982 (glyph < last
23983 && glyph->type == STRETCH_GLYPH
23984 && glyph->voffset == voffset
23985 && glyph->face_id == face_id);
23986 ++glyph)
23987 s->width += glyph->pixel_width;
23989 /* Adjust base line for subscript/superscript text. */
23990 s->ybase += voffset;
23992 /* The case that face->gc == 0 is handled when drawing the glyph
23993 string by calling PREPARE_FACE_FOR_DISPLAY. */
23994 eassert (s->face);
23995 return glyph - s->row->glyphs[s->area];
23998 static struct font_metrics *
23999 get_per_char_metric (struct font *font, XChar2b *char2b)
24001 static struct font_metrics metrics;
24002 unsigned code;
24004 if (! font)
24005 return NULL;
24006 code = (XCHAR2B_BYTE1 (char2b) << 8) | XCHAR2B_BYTE2 (char2b);
24007 if (code == FONT_INVALID_CODE)
24008 return NULL;
24009 font->driver->text_extents (font, &code, 1, &metrics);
24010 return &metrics;
24013 /* EXPORT for RIF:
24014 Set *LEFT and *RIGHT to the left and right overhang of GLYPH on
24015 frame F. Overhangs of glyphs other than type CHAR_GLYPH are
24016 assumed to be zero. */
24018 void
24019 x_get_glyph_overhangs (struct glyph *glyph, struct frame *f, int *left, int *right)
24021 *left = *right = 0;
24023 if (glyph->type == CHAR_GLYPH)
24025 struct face *face;
24026 XChar2b char2b;
24027 struct font_metrics *pcm;
24029 face = get_glyph_face_and_encoding (f, glyph, &char2b, NULL);
24030 if (face->font && (pcm = get_per_char_metric (face->font, &char2b)))
24032 if (pcm->rbearing > pcm->width)
24033 *right = pcm->rbearing - pcm->width;
24034 if (pcm->lbearing < 0)
24035 *left = -pcm->lbearing;
24038 else if (glyph->type == COMPOSITE_GLYPH)
24040 if (! glyph->u.cmp.automatic)
24042 struct composition *cmp = composition_table[glyph->u.cmp.id];
24044 if (cmp->rbearing > cmp->pixel_width)
24045 *right = cmp->rbearing - cmp->pixel_width;
24046 if (cmp->lbearing < 0)
24047 *left = - cmp->lbearing;
24049 else
24051 Lisp_Object gstring = composition_gstring_from_id (glyph->u.cmp.id);
24052 struct font_metrics metrics;
24054 composition_gstring_width (gstring, glyph->slice.cmp.from,
24055 glyph->slice.cmp.to + 1, &metrics);
24056 if (metrics.rbearing > metrics.width)
24057 *right = metrics.rbearing - metrics.width;
24058 if (metrics.lbearing < 0)
24059 *left = - metrics.lbearing;
24065 /* Return the index of the first glyph preceding glyph string S that
24066 is overwritten by S because of S's left overhang. Value is -1
24067 if no glyphs are overwritten. */
24069 static int
24070 left_overwritten (struct glyph_string *s)
24072 int k;
24074 if (s->left_overhang)
24076 int x = 0, i;
24077 struct glyph *glyphs = s->row->glyphs[s->area];
24078 int first = s->first_glyph - glyphs;
24080 for (i = first - 1; i >= 0 && x > -s->left_overhang; --i)
24081 x -= glyphs[i].pixel_width;
24083 k = i + 1;
24085 else
24086 k = -1;
24088 return k;
24092 /* Return the index of the first glyph preceding glyph string S that
24093 is overwriting S because of its right overhang. Value is -1 if no
24094 glyph in front of S overwrites S. */
24096 static int
24097 left_overwriting (struct glyph_string *s)
24099 int i, k, x;
24100 struct glyph *glyphs = s->row->glyphs[s->area];
24101 int first = s->first_glyph - glyphs;
24103 k = -1;
24104 x = 0;
24105 for (i = first - 1; i >= 0; --i)
24107 int left, right;
24108 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
24109 if (x + right > 0)
24110 k = i;
24111 x -= glyphs[i].pixel_width;
24114 return k;
24118 /* Return the index of the last glyph following glyph string S that is
24119 overwritten by S because of S's right overhang. Value is -1 if
24120 no such glyph is found. */
24122 static int
24123 right_overwritten (struct glyph_string *s)
24125 int k = -1;
24127 if (s->right_overhang)
24129 int x = 0, i;
24130 struct glyph *glyphs = s->row->glyphs[s->area];
24131 int first = (s->first_glyph - glyphs
24132 + (s->first_glyph->type == COMPOSITE_GLYPH ? 1 : s->nchars));
24133 int end = s->row->used[s->area];
24135 for (i = first; i < end && s->right_overhang > x; ++i)
24136 x += glyphs[i].pixel_width;
24138 k = i;
24141 return k;
24145 /* Return the index of the last glyph following glyph string S that
24146 overwrites S because of its left overhang. Value is negative
24147 if no such glyph is found. */
24149 static int
24150 right_overwriting (struct glyph_string *s)
24152 int i, k, x;
24153 int end = s->row->used[s->area];
24154 struct glyph *glyphs = s->row->glyphs[s->area];
24155 int first = (s->first_glyph - glyphs
24156 + (s->first_glyph->type == COMPOSITE_GLYPH ? 1 : s->nchars));
24158 k = -1;
24159 x = 0;
24160 for (i = first; i < end; ++i)
24162 int left, right;
24163 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
24164 if (x - left < 0)
24165 k = i;
24166 x += glyphs[i].pixel_width;
24169 return k;
24173 /* Set background width of glyph string S. START is the index of the
24174 first glyph following S. LAST_X is the right-most x-position + 1
24175 in the drawing area. */
24177 static void
24178 set_glyph_string_background_width (struct glyph_string *s, int start, int last_x)
24180 /* If the face of this glyph string has to be drawn to the end of
24181 the drawing area, set S->extends_to_end_of_line_p. */
24183 if (start == s->row->used[s->area]
24184 && ((s->row->fill_line_p
24185 && (s->hl == DRAW_NORMAL_TEXT
24186 || s->hl == DRAW_IMAGE_RAISED
24187 || s->hl == DRAW_IMAGE_SUNKEN))
24188 || s->hl == DRAW_MOUSE_FACE))
24189 s->extends_to_end_of_line_p = 1;
24191 /* If S extends its face to the end of the line, set its
24192 background_width to the distance to the right edge of the drawing
24193 area. */
24194 if (s->extends_to_end_of_line_p)
24195 s->background_width = last_x - s->x + 1;
24196 else
24197 s->background_width = s->width;
24201 /* Compute overhangs and x-positions for glyph string S and its
24202 predecessors, or successors. X is the starting x-position for S.
24203 BACKWARD_P non-zero means process predecessors. */
24205 static void
24206 compute_overhangs_and_x (struct glyph_string *s, int x, int backward_p)
24208 if (backward_p)
24210 while (s)
24212 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
24213 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
24214 x -= s->width;
24215 s->x = x;
24216 s = s->prev;
24219 else
24221 while (s)
24223 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
24224 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
24225 s->x = x;
24226 x += s->width;
24227 s = s->next;
24234 /* The following macros are only called from draw_glyphs below.
24235 They reference the following parameters of that function directly:
24236 `w', `row', `area', and `overlap_p'
24237 as well as the following local variables:
24238 `s', `f', and `hdc' (in W32) */
24240 #ifdef HAVE_NTGUI
24241 /* On W32, silently add local `hdc' variable to argument list of
24242 init_glyph_string. */
24243 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
24244 init_glyph_string (s, hdc, char2b, w, row, area, start, hl)
24245 #else
24246 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
24247 init_glyph_string (s, char2b, w, row, area, start, hl)
24248 #endif
24250 /* Add a glyph string for a stretch glyph to the list of strings
24251 between HEAD and TAIL. START is the index of the stretch glyph in
24252 row area AREA of glyph row ROW. END is the index of the last glyph
24253 in that glyph row area. X is the current output position assigned
24254 to the new glyph string constructed. HL overrides that face of the
24255 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
24256 is the right-most x-position of the drawing area. */
24258 /* SunOS 4 bundled cc, barfed on continuations in the arg lists here
24259 and below -- keep them on one line. */
24260 #define BUILD_STRETCH_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
24261 do \
24263 s = alloca (sizeof *s); \
24264 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
24265 START = fill_stretch_glyph_string (s, START, END); \
24266 append_glyph_string (&HEAD, &TAIL, s); \
24267 s->x = (X); \
24269 while (0)
24272 /* Add a glyph string for an image glyph to the list of strings
24273 between HEAD and TAIL. START is the index of the image glyph in
24274 row area AREA of glyph row ROW. END is the index of the last glyph
24275 in that glyph row area. X is the current output position assigned
24276 to the new glyph string constructed. HL overrides that face of the
24277 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
24278 is the right-most x-position of the drawing area. */
24280 #define BUILD_IMAGE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
24281 do \
24283 s = alloca (sizeof *s); \
24284 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
24285 fill_image_glyph_string (s); \
24286 append_glyph_string (&HEAD, &TAIL, s); \
24287 ++START; \
24288 s->x = (X); \
24290 while (0)
24293 /* Add a glyph string for a sequence of character glyphs to the list
24294 of strings between HEAD and TAIL. START is the index of the first
24295 glyph in row area AREA of glyph row ROW that is part of the new
24296 glyph string. END is the index of the last glyph in that glyph row
24297 area. X is the current output position assigned to the new glyph
24298 string constructed. HL overrides that face of the glyph; e.g. it
24299 is DRAW_CURSOR if a cursor has to be drawn. LAST_X is the
24300 right-most x-position of the drawing area. */
24302 #define BUILD_CHAR_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
24303 do \
24305 int face_id; \
24306 XChar2b *char2b; \
24308 face_id = (row)->glyphs[area][START].face_id; \
24310 s = alloca (sizeof *s); \
24311 char2b = alloca ((END - START) * sizeof *char2b); \
24312 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
24313 append_glyph_string (&HEAD, &TAIL, s); \
24314 s->x = (X); \
24315 START = fill_glyph_string (s, face_id, START, END, overlaps); \
24317 while (0)
24320 /* Add a glyph string for a composite sequence to the list of strings
24321 between HEAD and TAIL. START is the index of the first glyph in
24322 row area AREA of glyph row ROW that is part of the new glyph
24323 string. END is the index of the last glyph in that glyph row area.
24324 X is the current output position assigned to the new glyph string
24325 constructed. HL overrides that face of the glyph; e.g. it is
24326 DRAW_CURSOR if a cursor has to be drawn. LAST_X is the right-most
24327 x-position of the drawing area. */
24329 #define BUILD_COMPOSITE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
24330 do { \
24331 int face_id = (row)->glyphs[area][START].face_id; \
24332 struct face *base_face = FACE_FROM_ID (f, face_id); \
24333 ptrdiff_t cmp_id = (row)->glyphs[area][START].u.cmp.id; \
24334 struct composition *cmp = composition_table[cmp_id]; \
24335 XChar2b *char2b; \
24336 struct glyph_string *first_s = NULL; \
24337 int n; \
24339 char2b = alloca (cmp->glyph_len * sizeof *char2b); \
24341 /* Make glyph_strings for each glyph sequence that is drawable by \
24342 the same face, and append them to HEAD/TAIL. */ \
24343 for (n = 0; n < cmp->glyph_len;) \
24345 s = alloca (sizeof *s); \
24346 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
24347 append_glyph_string (&(HEAD), &(TAIL), s); \
24348 s->cmp = cmp; \
24349 s->cmp_from = n; \
24350 s->x = (X); \
24351 if (n == 0) \
24352 first_s = s; \
24353 n = fill_composite_glyph_string (s, base_face, overlaps); \
24356 ++START; \
24357 s = first_s; \
24358 } while (0)
24361 /* Add a glyph string for a glyph-string sequence to the list of strings
24362 between HEAD and TAIL. */
24364 #define BUILD_GSTRING_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
24365 do { \
24366 int face_id; \
24367 XChar2b *char2b; \
24368 Lisp_Object gstring; \
24370 face_id = (row)->glyphs[area][START].face_id; \
24371 gstring = (composition_gstring_from_id \
24372 ((row)->glyphs[area][START].u.cmp.id)); \
24373 s = alloca (sizeof *s); \
24374 char2b = alloca (LGSTRING_GLYPH_LEN (gstring) * sizeof *char2b); \
24375 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
24376 append_glyph_string (&(HEAD), &(TAIL), s); \
24377 s->x = (X); \
24378 START = fill_gstring_glyph_string (s, face_id, START, END, overlaps); \
24379 } while (0)
24382 /* Add a glyph string for a sequence of glyphless character's glyphs
24383 to the list of strings between HEAD and TAIL. The meanings of
24384 arguments are the same as those of BUILD_CHAR_GLYPH_STRINGS. */
24386 #define BUILD_GLYPHLESS_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
24387 do \
24389 int face_id; \
24391 face_id = (row)->glyphs[area][START].face_id; \
24393 s = alloca (sizeof *s); \
24394 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
24395 append_glyph_string (&HEAD, &TAIL, s); \
24396 s->x = (X); \
24397 START = fill_glyphless_glyph_string (s, face_id, START, END, \
24398 overlaps); \
24400 while (0)
24403 /* Build a list of glyph strings between HEAD and TAIL for the glyphs
24404 of AREA of glyph row ROW on window W between indices START and END.
24405 HL overrides the face for drawing glyph strings, e.g. it is
24406 DRAW_CURSOR to draw a cursor. X and LAST_X are start and end
24407 x-positions of the drawing area.
24409 This is an ugly monster macro construct because we must use alloca
24410 to allocate glyph strings (because draw_glyphs can be called
24411 asynchronously). */
24413 #define BUILD_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
24414 do \
24416 HEAD = TAIL = NULL; \
24417 while (START < END) \
24419 struct glyph *first_glyph = (row)->glyphs[area] + START; \
24420 switch (first_glyph->type) \
24422 case CHAR_GLYPH: \
24423 BUILD_CHAR_GLYPH_STRINGS (START, END, HEAD, TAIL, \
24424 HL, X, LAST_X); \
24425 break; \
24427 case COMPOSITE_GLYPH: \
24428 if (first_glyph->u.cmp.automatic) \
24429 BUILD_GSTRING_GLYPH_STRING (START, END, HEAD, TAIL, \
24430 HL, X, LAST_X); \
24431 else \
24432 BUILD_COMPOSITE_GLYPH_STRING (START, END, HEAD, TAIL, \
24433 HL, X, LAST_X); \
24434 break; \
24436 case STRETCH_GLYPH: \
24437 BUILD_STRETCH_GLYPH_STRING (START, END, HEAD, TAIL, \
24438 HL, X, LAST_X); \
24439 break; \
24441 case IMAGE_GLYPH: \
24442 BUILD_IMAGE_GLYPH_STRING (START, END, HEAD, TAIL, \
24443 HL, X, LAST_X); \
24444 break; \
24446 case GLYPHLESS_GLYPH: \
24447 BUILD_GLYPHLESS_GLYPH_STRING (START, END, HEAD, TAIL, \
24448 HL, X, LAST_X); \
24449 break; \
24451 default: \
24452 emacs_abort (); \
24455 if (s) \
24457 set_glyph_string_background_width (s, START, LAST_X); \
24458 (X) += s->width; \
24461 } while (0)
24464 /* Draw glyphs between START and END in AREA of ROW on window W,
24465 starting at x-position X. X is relative to AREA in W. HL is a
24466 face-override with the following meaning:
24468 DRAW_NORMAL_TEXT draw normally
24469 DRAW_CURSOR draw in cursor face
24470 DRAW_MOUSE_FACE draw in mouse face.
24471 DRAW_INVERSE_VIDEO draw in mode line face
24472 DRAW_IMAGE_SUNKEN draw an image with a sunken relief around it
24473 DRAW_IMAGE_RAISED draw an image with a raised relief around it
24475 If OVERLAPS is non-zero, draw only the foreground of characters and
24476 clip to the physical height of ROW. Non-zero value also defines
24477 the overlapping part to be drawn:
24479 OVERLAPS_PRED overlap with preceding rows
24480 OVERLAPS_SUCC overlap with succeeding rows
24481 OVERLAPS_BOTH overlap with both preceding/succeeding rows
24482 OVERLAPS_ERASED_CURSOR overlap with erased cursor area
24484 Value is the x-position reached, relative to AREA of W. */
24486 static int
24487 draw_glyphs (struct window *w, int x, struct glyph_row *row,
24488 enum glyph_row_area area, ptrdiff_t start, ptrdiff_t end,
24489 enum draw_glyphs_face hl, int overlaps)
24491 struct glyph_string *head, *tail;
24492 struct glyph_string *s;
24493 struct glyph_string *clip_head = NULL, *clip_tail = NULL;
24494 int i, j, x_reached, last_x, area_left = 0;
24495 struct frame *f = XFRAME (WINDOW_FRAME (w));
24496 DECLARE_HDC (hdc);
24498 ALLOCATE_HDC (hdc, f);
24500 /* Let's rather be paranoid than getting a SEGV. */
24501 end = min (end, row->used[area]);
24502 start = clip_to_bounds (0, start, end);
24504 /* Translate X to frame coordinates. Set last_x to the right
24505 end of the drawing area. */
24506 if (row->full_width_p)
24508 /* X is relative to the left edge of W, without scroll bars
24509 or fringes. */
24510 area_left = WINDOW_LEFT_EDGE_X (w);
24511 last_x = (WINDOW_LEFT_EDGE_X (w) + WINDOW_PIXEL_WIDTH (w)
24512 - (row->mode_line_p ? WINDOW_RIGHT_DIVIDER_WIDTH (w) : 0));
24514 else
24516 area_left = window_box_left (w, area);
24517 last_x = area_left + window_box_width (w, area);
24519 x += area_left;
24521 /* Build a doubly-linked list of glyph_string structures between
24522 head and tail from what we have to draw. Note that the macro
24523 BUILD_GLYPH_STRINGS will modify its start parameter. That's
24524 the reason we use a separate variable `i'. */
24525 i = start;
24526 BUILD_GLYPH_STRINGS (i, end, head, tail, hl, x, last_x);
24527 if (tail)
24528 x_reached = tail->x + tail->background_width;
24529 else
24530 x_reached = x;
24532 /* If there are any glyphs with lbearing < 0 or rbearing > width in
24533 the row, redraw some glyphs in front or following the glyph
24534 strings built above. */
24535 if (head && !overlaps && row->contains_overlapping_glyphs_p)
24537 struct glyph_string *h, *t;
24538 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
24539 int mouse_beg_col IF_LINT (= 0), mouse_end_col IF_LINT (= 0);
24540 int check_mouse_face = 0;
24541 int dummy_x = 0;
24543 /* If mouse highlighting is on, we may need to draw adjacent
24544 glyphs using mouse-face highlighting. */
24545 if (area == TEXT_AREA && row->mouse_face_p
24546 && hlinfo->mouse_face_beg_row >= 0
24547 && hlinfo->mouse_face_end_row >= 0)
24549 ptrdiff_t row_vpos = MATRIX_ROW_VPOS (row, w->current_matrix);
24551 if (row_vpos >= hlinfo->mouse_face_beg_row
24552 && row_vpos <= hlinfo->mouse_face_end_row)
24554 check_mouse_face = 1;
24555 mouse_beg_col = (row_vpos == hlinfo->mouse_face_beg_row)
24556 ? hlinfo->mouse_face_beg_col : 0;
24557 mouse_end_col = (row_vpos == hlinfo->mouse_face_end_row)
24558 ? hlinfo->mouse_face_end_col
24559 : row->used[TEXT_AREA];
24563 /* Compute overhangs for all glyph strings. */
24564 if (FRAME_RIF (f)->compute_glyph_string_overhangs)
24565 for (s = head; s; s = s->next)
24566 FRAME_RIF (f)->compute_glyph_string_overhangs (s);
24568 /* Prepend glyph strings for glyphs in front of the first glyph
24569 string that are overwritten because of the first glyph
24570 string's left overhang. The background of all strings
24571 prepended must be drawn because the first glyph string
24572 draws over it. */
24573 i = left_overwritten (head);
24574 if (i >= 0)
24576 enum draw_glyphs_face overlap_hl;
24578 /* If this row contains mouse highlighting, attempt to draw
24579 the overlapped glyphs with the correct highlight. This
24580 code fails if the overlap encompasses more than one glyph
24581 and mouse-highlight spans only some of these glyphs.
24582 However, making it work perfectly involves a lot more
24583 code, and I don't know if the pathological case occurs in
24584 practice, so we'll stick to this for now. --- cyd */
24585 if (check_mouse_face
24586 && mouse_beg_col < start && mouse_end_col > i)
24587 overlap_hl = DRAW_MOUSE_FACE;
24588 else
24589 overlap_hl = DRAW_NORMAL_TEXT;
24591 j = i;
24592 BUILD_GLYPH_STRINGS (j, start, h, t,
24593 overlap_hl, dummy_x, last_x);
24594 start = i;
24595 compute_overhangs_and_x (t, head->x, 1);
24596 prepend_glyph_string_lists (&head, &tail, h, t);
24597 clip_head = head;
24600 /* Prepend glyph strings for glyphs in front of the first glyph
24601 string that overwrite that glyph string because of their
24602 right overhang. For these strings, only the foreground must
24603 be drawn, because it draws over the glyph string at `head'.
24604 The background must not be drawn because this would overwrite
24605 right overhangs of preceding glyphs for which no glyph
24606 strings exist. */
24607 i = left_overwriting (head);
24608 if (i >= 0)
24610 enum draw_glyphs_face overlap_hl;
24612 if (check_mouse_face
24613 && mouse_beg_col < start && mouse_end_col > i)
24614 overlap_hl = DRAW_MOUSE_FACE;
24615 else
24616 overlap_hl = DRAW_NORMAL_TEXT;
24618 clip_head = head;
24619 BUILD_GLYPH_STRINGS (i, start, h, t,
24620 overlap_hl, dummy_x, last_x);
24621 for (s = h; s; s = s->next)
24622 s->background_filled_p = 1;
24623 compute_overhangs_and_x (t, head->x, 1);
24624 prepend_glyph_string_lists (&head, &tail, h, t);
24627 /* Append glyphs strings for glyphs following the last glyph
24628 string tail that are overwritten by tail. The background of
24629 these strings has to be drawn because tail's foreground draws
24630 over it. */
24631 i = right_overwritten (tail);
24632 if (i >= 0)
24634 enum draw_glyphs_face overlap_hl;
24636 if (check_mouse_face
24637 && mouse_beg_col < i && mouse_end_col > end)
24638 overlap_hl = DRAW_MOUSE_FACE;
24639 else
24640 overlap_hl = DRAW_NORMAL_TEXT;
24642 BUILD_GLYPH_STRINGS (end, i, h, t,
24643 overlap_hl, x, last_x);
24644 /* Because BUILD_GLYPH_STRINGS updates the first argument,
24645 we don't have `end = i;' here. */
24646 compute_overhangs_and_x (h, tail->x + tail->width, 0);
24647 append_glyph_string_lists (&head, &tail, h, t);
24648 clip_tail = tail;
24651 /* Append glyph strings for glyphs following the last glyph
24652 string tail that overwrite tail. The foreground of such
24653 glyphs has to be drawn because it writes into the background
24654 of tail. The background must not be drawn because it could
24655 paint over the foreground of following glyphs. */
24656 i = right_overwriting (tail);
24657 if (i >= 0)
24659 enum draw_glyphs_face overlap_hl;
24660 if (check_mouse_face
24661 && mouse_beg_col < i && mouse_end_col > end)
24662 overlap_hl = DRAW_MOUSE_FACE;
24663 else
24664 overlap_hl = DRAW_NORMAL_TEXT;
24666 clip_tail = tail;
24667 i++; /* We must include the Ith glyph. */
24668 BUILD_GLYPH_STRINGS (end, i, h, t,
24669 overlap_hl, x, last_x);
24670 for (s = h; s; s = s->next)
24671 s->background_filled_p = 1;
24672 compute_overhangs_and_x (h, tail->x + tail->width, 0);
24673 append_glyph_string_lists (&head, &tail, h, t);
24675 if (clip_head || clip_tail)
24676 for (s = head; s; s = s->next)
24678 s->clip_head = clip_head;
24679 s->clip_tail = clip_tail;
24683 /* Draw all strings. */
24684 for (s = head; s; s = s->next)
24685 FRAME_RIF (f)->draw_glyph_string (s);
24687 #ifndef HAVE_NS
24688 /* When focus a sole frame and move horizontally, this sets on_p to 0
24689 causing a failure to erase prev cursor position. */
24690 if (area == TEXT_AREA
24691 && !row->full_width_p
24692 /* When drawing overlapping rows, only the glyph strings'
24693 foreground is drawn, which doesn't erase a cursor
24694 completely. */
24695 && !overlaps)
24697 int x0 = clip_head ? clip_head->x : (head ? head->x : x);
24698 int x1 = (clip_tail ? clip_tail->x + clip_tail->background_width
24699 : (tail ? tail->x + tail->background_width : x));
24700 x0 -= area_left;
24701 x1 -= area_left;
24703 notice_overwritten_cursor (w, TEXT_AREA, x0, x1,
24704 row->y, MATRIX_ROW_BOTTOM_Y (row));
24706 #endif
24708 /* Value is the x-position up to which drawn, relative to AREA of W.
24709 This doesn't include parts drawn because of overhangs. */
24710 if (row->full_width_p)
24711 x_reached = FRAME_TO_WINDOW_PIXEL_X (w, x_reached);
24712 else
24713 x_reached -= area_left;
24715 RELEASE_HDC (hdc, f);
24717 return x_reached;
24720 /* Expand row matrix if too narrow. Don't expand if area
24721 is not present. */
24723 #define IT_EXPAND_MATRIX_WIDTH(it, area) \
24725 if (!it->f->fonts_changed \
24726 && (it->glyph_row->glyphs[area] \
24727 < it->glyph_row->glyphs[area + 1])) \
24729 it->w->ncols_scale_factor++; \
24730 it->f->fonts_changed = 1; \
24734 /* Store one glyph for IT->char_to_display in IT->glyph_row.
24735 Called from x_produce_glyphs when IT->glyph_row is non-null. */
24737 static void
24738 append_glyph (struct it *it)
24740 struct glyph *glyph;
24741 enum glyph_row_area area = it->area;
24743 eassert (it->glyph_row);
24744 eassert (it->char_to_display != '\n' && it->char_to_display != '\t');
24746 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
24747 if (glyph < it->glyph_row->glyphs[area + 1])
24749 /* If the glyph row is reversed, we need to prepend the glyph
24750 rather than append it. */
24751 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24753 struct glyph *g;
24755 /* Make room for the additional glyph. */
24756 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
24757 g[1] = *g;
24758 glyph = it->glyph_row->glyphs[area];
24760 glyph->charpos = CHARPOS (it->position);
24761 glyph->object = it->object;
24762 if (it->pixel_width > 0)
24764 glyph->pixel_width = it->pixel_width;
24765 glyph->padding_p = 0;
24767 else
24769 /* Assure at least 1-pixel width. Otherwise, cursor can't
24770 be displayed correctly. */
24771 glyph->pixel_width = 1;
24772 glyph->padding_p = 1;
24774 glyph->ascent = it->ascent;
24775 glyph->descent = it->descent;
24776 glyph->voffset = it->voffset;
24777 glyph->type = CHAR_GLYPH;
24778 glyph->avoid_cursor_p = it->avoid_cursor_p;
24779 glyph->multibyte_p = it->multibyte_p;
24780 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24782 /* In R2L rows, the left and the right box edges need to be
24783 drawn in reverse direction. */
24784 glyph->right_box_line_p = it->start_of_box_run_p;
24785 glyph->left_box_line_p = it->end_of_box_run_p;
24787 else
24789 glyph->left_box_line_p = it->start_of_box_run_p;
24790 glyph->right_box_line_p = it->end_of_box_run_p;
24792 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
24793 || it->phys_descent > it->descent);
24794 glyph->glyph_not_available_p = it->glyph_not_available_p;
24795 glyph->face_id = it->face_id;
24796 glyph->u.ch = it->char_to_display;
24797 glyph->slice.img = null_glyph_slice;
24798 glyph->font_type = FONT_TYPE_UNKNOWN;
24799 if (it->bidi_p)
24801 glyph->resolved_level = it->bidi_it.resolved_level;
24802 if ((it->bidi_it.type & 7) != it->bidi_it.type)
24803 emacs_abort ();
24804 glyph->bidi_type = it->bidi_it.type;
24806 else
24808 glyph->resolved_level = 0;
24809 glyph->bidi_type = UNKNOWN_BT;
24811 ++it->glyph_row->used[area];
24813 else
24814 IT_EXPAND_MATRIX_WIDTH (it, area);
24817 /* Store one glyph for the composition IT->cmp_it.id in
24818 IT->glyph_row. Called from x_produce_glyphs when IT->glyph_row is
24819 non-null. */
24821 static void
24822 append_composite_glyph (struct it *it)
24824 struct glyph *glyph;
24825 enum glyph_row_area area = it->area;
24827 eassert (it->glyph_row);
24829 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
24830 if (glyph < it->glyph_row->glyphs[area + 1])
24832 /* If the glyph row is reversed, we need to prepend the glyph
24833 rather than append it. */
24834 if (it->glyph_row->reversed_p && it->area == TEXT_AREA)
24836 struct glyph *g;
24838 /* Make room for the new glyph. */
24839 for (g = glyph - 1; g >= it->glyph_row->glyphs[it->area]; g--)
24840 g[1] = *g;
24841 glyph = it->glyph_row->glyphs[it->area];
24843 glyph->charpos = it->cmp_it.charpos;
24844 glyph->object = it->object;
24845 glyph->pixel_width = it->pixel_width;
24846 glyph->ascent = it->ascent;
24847 glyph->descent = it->descent;
24848 glyph->voffset = it->voffset;
24849 glyph->type = COMPOSITE_GLYPH;
24850 if (it->cmp_it.ch < 0)
24852 glyph->u.cmp.automatic = 0;
24853 glyph->u.cmp.id = it->cmp_it.id;
24854 glyph->slice.cmp.from = glyph->slice.cmp.to = 0;
24856 else
24858 glyph->u.cmp.automatic = 1;
24859 glyph->u.cmp.id = it->cmp_it.id;
24860 glyph->slice.cmp.from = it->cmp_it.from;
24861 glyph->slice.cmp.to = it->cmp_it.to - 1;
24863 glyph->avoid_cursor_p = it->avoid_cursor_p;
24864 glyph->multibyte_p = it->multibyte_p;
24865 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24867 /* In R2L rows, the left and the right box edges need to be
24868 drawn in reverse direction. */
24869 glyph->right_box_line_p = it->start_of_box_run_p;
24870 glyph->left_box_line_p = it->end_of_box_run_p;
24872 else
24874 glyph->left_box_line_p = it->start_of_box_run_p;
24875 glyph->right_box_line_p = it->end_of_box_run_p;
24877 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
24878 || it->phys_descent > it->descent);
24879 glyph->padding_p = 0;
24880 glyph->glyph_not_available_p = 0;
24881 glyph->face_id = it->face_id;
24882 glyph->font_type = FONT_TYPE_UNKNOWN;
24883 if (it->bidi_p)
24885 glyph->resolved_level = it->bidi_it.resolved_level;
24886 if ((it->bidi_it.type & 7) != it->bidi_it.type)
24887 emacs_abort ();
24888 glyph->bidi_type = it->bidi_it.type;
24890 ++it->glyph_row->used[area];
24892 else
24893 IT_EXPAND_MATRIX_WIDTH (it, area);
24897 /* Change IT->ascent and IT->height according to the setting of
24898 IT->voffset. */
24900 static void
24901 take_vertical_position_into_account (struct it *it)
24903 if (it->voffset)
24905 if (it->voffset < 0)
24906 /* Increase the ascent so that we can display the text higher
24907 in the line. */
24908 it->ascent -= it->voffset;
24909 else
24910 /* Increase the descent so that we can display the text lower
24911 in the line. */
24912 it->descent += it->voffset;
24917 /* Produce glyphs/get display metrics for the image IT is loaded with.
24918 See the description of struct display_iterator in dispextern.h for
24919 an overview of struct display_iterator. */
24921 static void
24922 produce_image_glyph (struct it *it)
24924 struct image *img;
24925 struct face *face;
24926 int glyph_ascent, crop;
24927 struct glyph_slice slice;
24929 eassert (it->what == IT_IMAGE);
24931 face = FACE_FROM_ID (it->f, it->face_id);
24932 eassert (face);
24933 /* Make sure X resources of the face is loaded. */
24934 PREPARE_FACE_FOR_DISPLAY (it->f, face);
24936 if (it->image_id < 0)
24938 /* Fringe bitmap. */
24939 it->ascent = it->phys_ascent = 0;
24940 it->descent = it->phys_descent = 0;
24941 it->pixel_width = 0;
24942 it->nglyphs = 0;
24943 return;
24946 img = IMAGE_FROM_ID (it->f, it->image_id);
24947 eassert (img);
24948 /* Make sure X resources of the image is loaded. */
24949 prepare_image_for_display (it->f, img);
24951 slice.x = slice.y = 0;
24952 slice.width = img->width;
24953 slice.height = img->height;
24955 if (INTEGERP (it->slice.x))
24956 slice.x = XINT (it->slice.x);
24957 else if (FLOATP (it->slice.x))
24958 slice.x = XFLOAT_DATA (it->slice.x) * img->width;
24960 if (INTEGERP (it->slice.y))
24961 slice.y = XINT (it->slice.y);
24962 else if (FLOATP (it->slice.y))
24963 slice.y = XFLOAT_DATA (it->slice.y) * img->height;
24965 if (INTEGERP (it->slice.width))
24966 slice.width = XINT (it->slice.width);
24967 else if (FLOATP (it->slice.width))
24968 slice.width = XFLOAT_DATA (it->slice.width) * img->width;
24970 if (INTEGERP (it->slice.height))
24971 slice.height = XINT (it->slice.height);
24972 else if (FLOATP (it->slice.height))
24973 slice.height = XFLOAT_DATA (it->slice.height) * img->height;
24975 if (slice.x >= img->width)
24976 slice.x = img->width;
24977 if (slice.y >= img->height)
24978 slice.y = img->height;
24979 if (slice.x + slice.width >= img->width)
24980 slice.width = img->width - slice.x;
24981 if (slice.y + slice.height > img->height)
24982 slice.height = img->height - slice.y;
24984 if (slice.width == 0 || slice.height == 0)
24985 return;
24987 it->ascent = it->phys_ascent = glyph_ascent = image_ascent (img, face, &slice);
24989 it->descent = slice.height - glyph_ascent;
24990 if (slice.y == 0)
24991 it->descent += img->vmargin;
24992 if (slice.y + slice.height == img->height)
24993 it->descent += img->vmargin;
24994 it->phys_descent = it->descent;
24996 it->pixel_width = slice.width;
24997 if (slice.x == 0)
24998 it->pixel_width += img->hmargin;
24999 if (slice.x + slice.width == img->width)
25000 it->pixel_width += img->hmargin;
25002 /* It's quite possible for images to have an ascent greater than
25003 their height, so don't get confused in that case. */
25004 if (it->descent < 0)
25005 it->descent = 0;
25007 it->nglyphs = 1;
25009 if (face->box != FACE_NO_BOX)
25011 if (face->box_line_width > 0)
25013 if (slice.y == 0)
25014 it->ascent += face->box_line_width;
25015 if (slice.y + slice.height == img->height)
25016 it->descent += face->box_line_width;
25019 if (it->start_of_box_run_p && slice.x == 0)
25020 it->pixel_width += eabs (face->box_line_width);
25021 if (it->end_of_box_run_p && slice.x + slice.width == img->width)
25022 it->pixel_width += eabs (face->box_line_width);
25025 take_vertical_position_into_account (it);
25027 /* Automatically crop wide image glyphs at right edge so we can
25028 draw the cursor on same display row. */
25029 if ((crop = it->pixel_width - (it->last_visible_x - it->current_x), crop > 0)
25030 && (it->hpos == 0 || it->pixel_width > it->last_visible_x / 4))
25032 it->pixel_width -= crop;
25033 slice.width -= crop;
25036 if (it->glyph_row)
25038 struct glyph *glyph;
25039 enum glyph_row_area area = it->area;
25041 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
25042 if (glyph < it->glyph_row->glyphs[area + 1])
25044 glyph->charpos = CHARPOS (it->position);
25045 glyph->object = it->object;
25046 glyph->pixel_width = it->pixel_width;
25047 glyph->ascent = glyph_ascent;
25048 glyph->descent = it->descent;
25049 glyph->voffset = it->voffset;
25050 glyph->type = IMAGE_GLYPH;
25051 glyph->avoid_cursor_p = it->avoid_cursor_p;
25052 glyph->multibyte_p = it->multibyte_p;
25053 if (it->glyph_row->reversed_p && area == TEXT_AREA)
25055 /* In R2L rows, the left and the right box edges need to be
25056 drawn in reverse direction. */
25057 glyph->right_box_line_p = it->start_of_box_run_p;
25058 glyph->left_box_line_p = it->end_of_box_run_p;
25060 else
25062 glyph->left_box_line_p = it->start_of_box_run_p;
25063 glyph->right_box_line_p = it->end_of_box_run_p;
25065 glyph->overlaps_vertically_p = 0;
25066 glyph->padding_p = 0;
25067 glyph->glyph_not_available_p = 0;
25068 glyph->face_id = it->face_id;
25069 glyph->u.img_id = img->id;
25070 glyph->slice.img = slice;
25071 glyph->font_type = FONT_TYPE_UNKNOWN;
25072 if (it->bidi_p)
25074 glyph->resolved_level = it->bidi_it.resolved_level;
25075 if ((it->bidi_it.type & 7) != it->bidi_it.type)
25076 emacs_abort ();
25077 glyph->bidi_type = it->bidi_it.type;
25079 ++it->glyph_row->used[area];
25081 else
25082 IT_EXPAND_MATRIX_WIDTH (it, area);
25087 /* Append a stretch glyph to IT->glyph_row. OBJECT is the source
25088 of the glyph, WIDTH and HEIGHT are the width and height of the
25089 stretch. ASCENT is the ascent of the glyph (0 <= ASCENT <= HEIGHT). */
25091 static void
25092 append_stretch_glyph (struct it *it, Lisp_Object object,
25093 int width, int height, int ascent)
25095 struct glyph *glyph;
25096 enum glyph_row_area area = it->area;
25098 eassert (ascent >= 0 && ascent <= height);
25100 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
25101 if (glyph < it->glyph_row->glyphs[area + 1])
25103 /* If the glyph row is reversed, we need to prepend the glyph
25104 rather than append it. */
25105 if (it->glyph_row->reversed_p && area == TEXT_AREA)
25107 struct glyph *g;
25109 /* Make room for the additional glyph. */
25110 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
25111 g[1] = *g;
25112 glyph = it->glyph_row->glyphs[area];
25114 glyph->charpos = CHARPOS (it->position);
25115 glyph->object = object;
25116 glyph->pixel_width = width;
25117 glyph->ascent = ascent;
25118 glyph->descent = height - ascent;
25119 glyph->voffset = it->voffset;
25120 glyph->type = STRETCH_GLYPH;
25121 glyph->avoid_cursor_p = it->avoid_cursor_p;
25122 glyph->multibyte_p = it->multibyte_p;
25123 if (it->glyph_row->reversed_p && area == TEXT_AREA)
25125 /* In R2L rows, the left and the right box edges need to be
25126 drawn in reverse direction. */
25127 glyph->right_box_line_p = it->start_of_box_run_p;
25128 glyph->left_box_line_p = it->end_of_box_run_p;
25130 else
25132 glyph->left_box_line_p = it->start_of_box_run_p;
25133 glyph->right_box_line_p = it->end_of_box_run_p;
25135 glyph->overlaps_vertically_p = 0;
25136 glyph->padding_p = 0;
25137 glyph->glyph_not_available_p = 0;
25138 glyph->face_id = it->face_id;
25139 glyph->u.stretch.ascent = ascent;
25140 glyph->u.stretch.height = height;
25141 glyph->slice.img = null_glyph_slice;
25142 glyph->font_type = FONT_TYPE_UNKNOWN;
25143 if (it->bidi_p)
25145 glyph->resolved_level = it->bidi_it.resolved_level;
25146 if ((it->bidi_it.type & 7) != it->bidi_it.type)
25147 emacs_abort ();
25148 glyph->bidi_type = it->bidi_it.type;
25150 else
25152 glyph->resolved_level = 0;
25153 glyph->bidi_type = UNKNOWN_BT;
25155 ++it->glyph_row->used[area];
25157 else
25158 IT_EXPAND_MATRIX_WIDTH (it, area);
25161 #endif /* HAVE_WINDOW_SYSTEM */
25163 /* Produce a stretch glyph for iterator IT. IT->object is the value
25164 of the glyph property displayed. The value must be a list
25165 `(space KEYWORD VALUE ...)' with the following KEYWORD/VALUE pairs
25166 being recognized:
25168 1. `:width WIDTH' specifies that the space should be WIDTH *
25169 canonical char width wide. WIDTH may be an integer or floating
25170 point number.
25172 2. `:relative-width FACTOR' specifies that the width of the stretch
25173 should be computed from the width of the first character having the
25174 `glyph' property, and should be FACTOR times that width.
25176 3. `:align-to HPOS' specifies that the space should be wide enough
25177 to reach HPOS, a value in canonical character units.
25179 Exactly one of the above pairs must be present.
25181 4. `:height HEIGHT' specifies that the height of the stretch produced
25182 should be HEIGHT, measured in canonical character units.
25184 5. `:relative-height FACTOR' specifies that the height of the
25185 stretch should be FACTOR times the height of the characters having
25186 the glyph property.
25188 Either none or exactly one of 4 or 5 must be present.
25190 6. `:ascent ASCENT' specifies that ASCENT percent of the height
25191 of the stretch should be used for the ascent of the stretch.
25192 ASCENT must be in the range 0 <= ASCENT <= 100. */
25194 void
25195 produce_stretch_glyph (struct it *it)
25197 /* (space :width WIDTH :height HEIGHT ...) */
25198 Lisp_Object prop, plist;
25199 int width = 0, height = 0, align_to = -1;
25200 int zero_width_ok_p = 0;
25201 double tem;
25202 struct font *font = NULL;
25204 #ifdef HAVE_WINDOW_SYSTEM
25205 int ascent = 0;
25206 int zero_height_ok_p = 0;
25208 if (FRAME_WINDOW_P (it->f))
25210 struct face *face = FACE_FROM_ID (it->f, it->face_id);
25211 font = face->font ? face->font : FRAME_FONT (it->f);
25212 PREPARE_FACE_FOR_DISPLAY (it->f, face);
25214 #endif
25216 /* List should start with `space'. */
25217 eassert (CONSP (it->object) && EQ (XCAR (it->object), Qspace));
25218 plist = XCDR (it->object);
25220 /* Compute the width of the stretch. */
25221 if ((prop = Fplist_get (plist, QCwidth), !NILP (prop))
25222 && calc_pixel_width_or_height (&tem, it, prop, font, 1, 0))
25224 /* Absolute width `:width WIDTH' specified and valid. */
25225 zero_width_ok_p = 1;
25226 width = (int)tem;
25228 #ifdef HAVE_WINDOW_SYSTEM
25229 else if (FRAME_WINDOW_P (it->f)
25230 && (prop = Fplist_get (plist, QCrelative_width), NUMVAL (prop) > 0))
25232 /* Relative width `:relative-width FACTOR' specified and valid.
25233 Compute the width of the characters having the `glyph'
25234 property. */
25235 struct it it2;
25236 unsigned char *p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
25238 it2 = *it;
25239 if (it->multibyte_p)
25240 it2.c = it2.char_to_display = STRING_CHAR_AND_LENGTH (p, it2.len);
25241 else
25243 it2.c = it2.char_to_display = *p, it2.len = 1;
25244 if (! ASCII_CHAR_P (it2.c))
25245 it2.char_to_display = BYTE8_TO_CHAR (it2.c);
25248 it2.glyph_row = NULL;
25249 it2.what = IT_CHARACTER;
25250 x_produce_glyphs (&it2);
25251 width = NUMVAL (prop) * it2.pixel_width;
25253 #endif /* HAVE_WINDOW_SYSTEM */
25254 else if ((prop = Fplist_get (plist, QCalign_to), !NILP (prop))
25255 && calc_pixel_width_or_height (&tem, it, prop, font, 1, &align_to))
25257 if (it->glyph_row == NULL || !it->glyph_row->mode_line_p)
25258 align_to = (align_to < 0
25260 : align_to - window_box_left_offset (it->w, TEXT_AREA));
25261 else if (align_to < 0)
25262 align_to = window_box_left_offset (it->w, TEXT_AREA);
25263 width = max (0, (int)tem + align_to - it->current_x);
25264 zero_width_ok_p = 1;
25266 else
25267 /* Nothing specified -> width defaults to canonical char width. */
25268 width = FRAME_COLUMN_WIDTH (it->f);
25270 if (width <= 0 && (width < 0 || !zero_width_ok_p))
25271 width = 1;
25273 #ifdef HAVE_WINDOW_SYSTEM
25274 /* Compute height. */
25275 if (FRAME_WINDOW_P (it->f))
25277 if ((prop = Fplist_get (plist, QCheight), !NILP (prop))
25278 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
25280 height = (int)tem;
25281 zero_height_ok_p = 1;
25283 else if (prop = Fplist_get (plist, QCrelative_height),
25284 NUMVAL (prop) > 0)
25285 height = FONT_HEIGHT (font) * NUMVAL (prop);
25286 else
25287 height = FONT_HEIGHT (font);
25289 if (height <= 0 && (height < 0 || !zero_height_ok_p))
25290 height = 1;
25292 /* Compute percentage of height used for ascent. If
25293 `:ascent ASCENT' is present and valid, use that. Otherwise,
25294 derive the ascent from the font in use. */
25295 if (prop = Fplist_get (plist, QCascent),
25296 NUMVAL (prop) > 0 && NUMVAL (prop) <= 100)
25297 ascent = height * NUMVAL (prop) / 100.0;
25298 else if (!NILP (prop)
25299 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
25300 ascent = min (max (0, (int)tem), height);
25301 else
25302 ascent = (height * FONT_BASE (font)) / FONT_HEIGHT (font);
25304 else
25305 #endif /* HAVE_WINDOW_SYSTEM */
25306 height = 1;
25308 if (width > 0 && it->line_wrap != TRUNCATE
25309 && it->current_x + width > it->last_visible_x)
25311 width = it->last_visible_x - it->current_x;
25312 #ifdef HAVE_WINDOW_SYSTEM
25313 /* Subtract one more pixel from the stretch width, but only on
25314 GUI frames, since on a TTY each glyph is one "pixel" wide. */
25315 width -= FRAME_WINDOW_P (it->f);
25316 #endif
25319 if (width > 0 && height > 0 && it->glyph_row)
25321 Lisp_Object o_object = it->object;
25322 Lisp_Object object = it->stack[it->sp - 1].string;
25323 int n = width;
25325 if (!STRINGP (object))
25326 object = it->w->contents;
25327 #ifdef HAVE_WINDOW_SYSTEM
25328 if (FRAME_WINDOW_P (it->f))
25329 append_stretch_glyph (it, object, width, height, ascent);
25330 else
25331 #endif
25333 it->object = object;
25334 it->char_to_display = ' ';
25335 it->pixel_width = it->len = 1;
25336 while (n--)
25337 tty_append_glyph (it);
25338 it->object = o_object;
25342 it->pixel_width = width;
25343 #ifdef HAVE_WINDOW_SYSTEM
25344 if (FRAME_WINDOW_P (it->f))
25346 it->ascent = it->phys_ascent = ascent;
25347 it->descent = it->phys_descent = height - it->ascent;
25348 it->nglyphs = width > 0 && height > 0 ? 1 : 0;
25349 take_vertical_position_into_account (it);
25351 else
25352 #endif
25353 it->nglyphs = width;
25356 /* Get information about special display element WHAT in an
25357 environment described by IT. WHAT is one of IT_TRUNCATION or
25358 IT_CONTINUATION. Maybe produce glyphs for WHAT if IT has a
25359 non-null glyph_row member. This function ensures that fields like
25360 face_id, c, len of IT are left untouched. */
25362 static void
25363 produce_special_glyphs (struct it *it, enum display_element_type what)
25365 struct it temp_it;
25366 Lisp_Object gc;
25367 GLYPH glyph;
25369 temp_it = *it;
25370 temp_it.object = make_number (0);
25371 memset (&temp_it.current, 0, sizeof temp_it.current);
25373 if (what == IT_CONTINUATION)
25375 /* Continuation glyph. For R2L lines, we mirror it by hand. */
25376 if (it->bidi_it.paragraph_dir == R2L)
25377 SET_GLYPH_FROM_CHAR (glyph, '/');
25378 else
25379 SET_GLYPH_FROM_CHAR (glyph, '\\');
25380 if (it->dp
25381 && (gc = DISP_CONTINUE_GLYPH (it->dp), GLYPH_CODE_P (gc)))
25383 /* FIXME: Should we mirror GC for R2L lines? */
25384 SET_GLYPH_FROM_GLYPH_CODE (glyph, gc);
25385 spec_glyph_lookup_face (XWINDOW (it->window), &glyph);
25388 else if (what == IT_TRUNCATION)
25390 /* Truncation glyph. */
25391 SET_GLYPH_FROM_CHAR (glyph, '$');
25392 if (it->dp
25393 && (gc = DISP_TRUNC_GLYPH (it->dp), GLYPH_CODE_P (gc)))
25395 /* FIXME: Should we mirror GC for R2L lines? */
25396 SET_GLYPH_FROM_GLYPH_CODE (glyph, gc);
25397 spec_glyph_lookup_face (XWINDOW (it->window), &glyph);
25400 else
25401 emacs_abort ();
25403 #ifdef HAVE_WINDOW_SYSTEM
25404 /* On a GUI frame, when the right fringe (left fringe for R2L rows)
25405 is turned off, we precede the truncation/continuation glyphs by a
25406 stretch glyph whose width is computed such that these special
25407 glyphs are aligned at the window margin, even when very different
25408 fonts are used in different glyph rows. */
25409 if (FRAME_WINDOW_P (temp_it.f)
25410 /* init_iterator calls this with it->glyph_row == NULL, and it
25411 wants only the pixel width of the truncation/continuation
25412 glyphs. */
25413 && temp_it.glyph_row
25414 /* insert_left_trunc_glyphs calls us at the beginning of the
25415 row, and it has its own calculation of the stretch glyph
25416 width. */
25417 && temp_it.glyph_row->used[TEXT_AREA] > 0
25418 && (temp_it.glyph_row->reversed_p
25419 ? WINDOW_LEFT_FRINGE_WIDTH (temp_it.w)
25420 : WINDOW_RIGHT_FRINGE_WIDTH (temp_it.w)) == 0)
25422 int stretch_width = temp_it.last_visible_x - temp_it.current_x;
25424 if (stretch_width > 0)
25426 struct face *face = FACE_FROM_ID (temp_it.f, temp_it.face_id);
25427 struct font *font =
25428 face->font ? face->font : FRAME_FONT (temp_it.f);
25429 int stretch_ascent =
25430 (((temp_it.ascent + temp_it.descent)
25431 * FONT_BASE (font)) / FONT_HEIGHT (font));
25433 append_stretch_glyph (&temp_it, make_number (0), stretch_width,
25434 temp_it.ascent + temp_it.descent,
25435 stretch_ascent);
25438 #endif
25440 temp_it.dp = NULL;
25441 temp_it.what = IT_CHARACTER;
25442 temp_it.len = 1;
25443 temp_it.c = temp_it.char_to_display = GLYPH_CHAR (glyph);
25444 temp_it.face_id = GLYPH_FACE (glyph);
25445 temp_it.len = CHAR_BYTES (temp_it.c);
25447 PRODUCE_GLYPHS (&temp_it);
25448 it->pixel_width = temp_it.pixel_width;
25449 it->nglyphs = temp_it.pixel_width;
25452 #ifdef HAVE_WINDOW_SYSTEM
25454 /* Calculate line-height and line-spacing properties.
25455 An integer value specifies explicit pixel value.
25456 A float value specifies relative value to current face height.
25457 A cons (float . face-name) specifies relative value to
25458 height of specified face font.
25460 Returns height in pixels, or nil. */
25463 static Lisp_Object
25464 calc_line_height_property (struct it *it, Lisp_Object val, struct font *font,
25465 int boff, int override)
25467 Lisp_Object face_name = Qnil;
25468 int ascent, descent, height;
25470 if (NILP (val) || INTEGERP (val) || (override && EQ (val, Qt)))
25471 return val;
25473 if (CONSP (val))
25475 face_name = XCAR (val);
25476 val = XCDR (val);
25477 if (!NUMBERP (val))
25478 val = make_number (1);
25479 if (NILP (face_name))
25481 height = it->ascent + it->descent;
25482 goto scale;
25486 if (NILP (face_name))
25488 font = FRAME_FONT (it->f);
25489 boff = FRAME_BASELINE_OFFSET (it->f);
25491 else if (EQ (face_name, Qt))
25493 override = 0;
25495 else
25497 int face_id;
25498 struct face *face;
25500 face_id = lookup_named_face (it->f, face_name, 0);
25501 if (face_id < 0)
25502 return make_number (-1);
25504 face = FACE_FROM_ID (it->f, face_id);
25505 font = face->font;
25506 if (font == NULL)
25507 return make_number (-1);
25508 boff = font->baseline_offset;
25509 if (font->vertical_centering)
25510 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
25513 ascent = FONT_BASE (font) + boff;
25514 descent = FONT_DESCENT (font) - boff;
25516 if (override)
25518 it->override_ascent = ascent;
25519 it->override_descent = descent;
25520 it->override_boff = boff;
25523 height = ascent + descent;
25525 scale:
25526 if (FLOATP (val))
25527 height = (int)(XFLOAT_DATA (val) * height);
25528 else if (INTEGERP (val))
25529 height *= XINT (val);
25531 return make_number (height);
25535 /* Append a glyph for a glyphless character to IT->glyph_row. FACE_ID
25536 is a face ID to be used for the glyph. FOR_NO_FONT is nonzero if
25537 and only if this is for a character for which no font was found.
25539 If the display method (it->glyphless_method) is
25540 GLYPHLESS_DISPLAY_ACRONYM or GLYPHLESS_DISPLAY_HEX_CODE, LEN is a
25541 length of the acronym or the hexadecimal string, UPPER_XOFF and
25542 UPPER_YOFF are pixel offsets for the upper part of the string,
25543 LOWER_XOFF and LOWER_YOFF are for the lower part.
25545 For the other display methods, LEN through LOWER_YOFF are zero. */
25547 static void
25548 append_glyphless_glyph (struct it *it, int face_id, int for_no_font, int len,
25549 short upper_xoff, short upper_yoff,
25550 short lower_xoff, short lower_yoff)
25552 struct glyph *glyph;
25553 enum glyph_row_area area = it->area;
25555 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
25556 if (glyph < it->glyph_row->glyphs[area + 1])
25558 /* If the glyph row is reversed, we need to prepend the glyph
25559 rather than append it. */
25560 if (it->glyph_row->reversed_p && area == TEXT_AREA)
25562 struct glyph *g;
25564 /* Make room for the additional glyph. */
25565 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
25566 g[1] = *g;
25567 glyph = it->glyph_row->glyphs[area];
25569 glyph->charpos = CHARPOS (it->position);
25570 glyph->object = it->object;
25571 glyph->pixel_width = it->pixel_width;
25572 glyph->ascent = it->ascent;
25573 glyph->descent = it->descent;
25574 glyph->voffset = it->voffset;
25575 glyph->type = GLYPHLESS_GLYPH;
25576 glyph->u.glyphless.method = it->glyphless_method;
25577 glyph->u.glyphless.for_no_font = for_no_font;
25578 glyph->u.glyphless.len = len;
25579 glyph->u.glyphless.ch = it->c;
25580 glyph->slice.glyphless.upper_xoff = upper_xoff;
25581 glyph->slice.glyphless.upper_yoff = upper_yoff;
25582 glyph->slice.glyphless.lower_xoff = lower_xoff;
25583 glyph->slice.glyphless.lower_yoff = lower_yoff;
25584 glyph->avoid_cursor_p = it->avoid_cursor_p;
25585 glyph->multibyte_p = it->multibyte_p;
25586 if (it->glyph_row->reversed_p && area == TEXT_AREA)
25588 /* In R2L rows, the left and the right box edges need to be
25589 drawn in reverse direction. */
25590 glyph->right_box_line_p = it->start_of_box_run_p;
25591 glyph->left_box_line_p = it->end_of_box_run_p;
25593 else
25595 glyph->left_box_line_p = it->start_of_box_run_p;
25596 glyph->right_box_line_p = it->end_of_box_run_p;
25598 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
25599 || it->phys_descent > it->descent);
25600 glyph->padding_p = 0;
25601 glyph->glyph_not_available_p = 0;
25602 glyph->face_id = face_id;
25603 glyph->font_type = FONT_TYPE_UNKNOWN;
25604 if (it->bidi_p)
25606 glyph->resolved_level = it->bidi_it.resolved_level;
25607 if ((it->bidi_it.type & 7) != it->bidi_it.type)
25608 emacs_abort ();
25609 glyph->bidi_type = it->bidi_it.type;
25611 ++it->glyph_row->used[area];
25613 else
25614 IT_EXPAND_MATRIX_WIDTH (it, area);
25618 /* Produce a glyph for a glyphless character for iterator IT.
25619 IT->glyphless_method specifies which method to use for displaying
25620 the character. See the description of enum
25621 glyphless_display_method in dispextern.h for the detail.
25623 FOR_NO_FONT is nonzero if and only if this is for a character for
25624 which no font was found. ACRONYM, if non-nil, is an acronym string
25625 for the character. */
25627 static void
25628 produce_glyphless_glyph (struct it *it, int for_no_font, Lisp_Object acronym)
25630 int face_id;
25631 struct face *face;
25632 struct font *font;
25633 int base_width, base_height, width, height;
25634 short upper_xoff, upper_yoff, lower_xoff, lower_yoff;
25635 int len;
25637 /* Get the metrics of the base font. We always refer to the current
25638 ASCII face. */
25639 face = FACE_FROM_ID (it->f, it->face_id)->ascii_face;
25640 font = face->font ? face->font : FRAME_FONT (it->f);
25641 it->ascent = FONT_BASE (font) + font->baseline_offset;
25642 it->descent = FONT_DESCENT (font) - font->baseline_offset;
25643 base_height = it->ascent + it->descent;
25644 base_width = font->average_width;
25646 face_id = merge_glyphless_glyph_face (it);
25648 if (it->glyphless_method == GLYPHLESS_DISPLAY_THIN_SPACE)
25650 it->pixel_width = THIN_SPACE_WIDTH;
25651 len = 0;
25652 upper_xoff = upper_yoff = lower_xoff = lower_yoff = 0;
25654 else if (it->glyphless_method == GLYPHLESS_DISPLAY_EMPTY_BOX)
25656 width = CHAR_WIDTH (it->c);
25657 if (width == 0)
25658 width = 1;
25659 else if (width > 4)
25660 width = 4;
25661 it->pixel_width = base_width * width;
25662 len = 0;
25663 upper_xoff = upper_yoff = lower_xoff = lower_yoff = 0;
25665 else
25667 char buf[7];
25668 const char *str;
25669 unsigned int code[6];
25670 int upper_len;
25671 int ascent, descent;
25672 struct font_metrics metrics_upper, metrics_lower;
25674 face = FACE_FROM_ID (it->f, face_id);
25675 font = face->font ? face->font : FRAME_FONT (it->f);
25676 PREPARE_FACE_FOR_DISPLAY (it->f, face);
25678 if (it->glyphless_method == GLYPHLESS_DISPLAY_ACRONYM)
25680 if (! STRINGP (acronym) && CHAR_TABLE_P (Vglyphless_char_display))
25681 acronym = CHAR_TABLE_REF (Vglyphless_char_display, it->c);
25682 if (CONSP (acronym))
25683 acronym = XCAR (acronym);
25684 str = STRINGP (acronym) ? SSDATA (acronym) : "";
25686 else
25688 eassert (it->glyphless_method == GLYPHLESS_DISPLAY_HEX_CODE);
25689 sprintf (buf, "%0*X", it->c < 0x10000 ? 4 : 6, it->c);
25690 str = buf;
25692 for (len = 0; str[len] && ASCII_BYTE_P (str[len]) && len < 6; len++)
25693 code[len] = font->driver->encode_char (font, str[len]);
25694 upper_len = (len + 1) / 2;
25695 font->driver->text_extents (font, code, upper_len,
25696 &metrics_upper);
25697 font->driver->text_extents (font, code + upper_len, len - upper_len,
25698 &metrics_lower);
25702 /* +4 is for vertical bars of a box plus 1-pixel spaces at both side. */
25703 width = max (metrics_upper.width, metrics_lower.width) + 4;
25704 upper_xoff = upper_yoff = 2; /* the typical case */
25705 if (base_width >= width)
25707 /* Align the upper to the left, the lower to the right. */
25708 it->pixel_width = base_width;
25709 lower_xoff = base_width - 2 - metrics_lower.width;
25711 else
25713 /* Center the shorter one. */
25714 it->pixel_width = width;
25715 if (metrics_upper.width >= metrics_lower.width)
25716 lower_xoff = (width - metrics_lower.width) / 2;
25717 else
25719 /* FIXME: This code doesn't look right. It formerly was
25720 missing the "lower_xoff = 0;", which couldn't have
25721 been right since it left lower_xoff uninitialized. */
25722 lower_xoff = 0;
25723 upper_xoff = (width - metrics_upper.width) / 2;
25727 /* +5 is for horizontal bars of a box plus 1-pixel spaces at
25728 top, bottom, and between upper and lower strings. */
25729 height = (metrics_upper.ascent + metrics_upper.descent
25730 + metrics_lower.ascent + metrics_lower.descent) + 5;
25731 /* Center vertically.
25732 H:base_height, D:base_descent
25733 h:height, ld:lower_descent, la:lower_ascent, ud:upper_descent
25735 ascent = - (D - H/2 - h/2 + 1); "+ 1" for rounding up
25736 descent = D - H/2 + h/2;
25737 lower_yoff = descent - 2 - ld;
25738 upper_yoff = lower_yoff - la - 1 - ud; */
25739 ascent = - (it->descent - (base_height + height + 1) / 2);
25740 descent = it->descent - (base_height - height) / 2;
25741 lower_yoff = descent - 2 - metrics_lower.descent;
25742 upper_yoff = (lower_yoff - metrics_lower.ascent - 1
25743 - metrics_upper.descent);
25744 /* Don't make the height shorter than the base height. */
25745 if (height > base_height)
25747 it->ascent = ascent;
25748 it->descent = descent;
25752 it->phys_ascent = it->ascent;
25753 it->phys_descent = it->descent;
25754 if (it->glyph_row)
25755 append_glyphless_glyph (it, face_id, for_no_font, len,
25756 upper_xoff, upper_yoff,
25757 lower_xoff, lower_yoff);
25758 it->nglyphs = 1;
25759 take_vertical_position_into_account (it);
25763 /* RIF:
25764 Produce glyphs/get display metrics for the display element IT is
25765 loaded with. See the description of struct it in dispextern.h
25766 for an overview of struct it. */
25768 void
25769 x_produce_glyphs (struct it *it)
25771 int extra_line_spacing = it->extra_line_spacing;
25773 it->glyph_not_available_p = 0;
25775 if (it->what == IT_CHARACTER)
25777 XChar2b char2b;
25778 struct face *face = FACE_FROM_ID (it->f, it->face_id);
25779 struct font *font = face->font;
25780 struct font_metrics *pcm = NULL;
25781 int boff; /* Baseline offset. */
25783 if (font == NULL)
25785 /* When no suitable font is found, display this character by
25786 the method specified in the first extra slot of
25787 Vglyphless_char_display. */
25788 Lisp_Object acronym = lookup_glyphless_char_display (-1, it);
25790 eassert (it->what == IT_GLYPHLESS);
25791 produce_glyphless_glyph (it, 1, STRINGP (acronym) ? acronym : Qnil);
25792 goto done;
25795 boff = font->baseline_offset;
25796 if (font->vertical_centering)
25797 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
25799 if (it->char_to_display != '\n' && it->char_to_display != '\t')
25801 int stretched_p;
25803 it->nglyphs = 1;
25805 if (it->override_ascent >= 0)
25807 it->ascent = it->override_ascent;
25808 it->descent = it->override_descent;
25809 boff = it->override_boff;
25811 else
25813 it->ascent = FONT_BASE (font) + boff;
25814 it->descent = FONT_DESCENT (font) - boff;
25817 if (get_char_glyph_code (it->char_to_display, font, &char2b))
25819 pcm = get_per_char_metric (font, &char2b);
25820 if (pcm->width == 0
25821 && pcm->rbearing == 0 && pcm->lbearing == 0)
25822 pcm = NULL;
25825 if (pcm)
25827 it->phys_ascent = pcm->ascent + boff;
25828 it->phys_descent = pcm->descent - boff;
25829 it->pixel_width = pcm->width;
25831 else
25833 it->glyph_not_available_p = 1;
25834 it->phys_ascent = it->ascent;
25835 it->phys_descent = it->descent;
25836 it->pixel_width = font->space_width;
25839 if (it->constrain_row_ascent_descent_p)
25841 if (it->descent > it->max_descent)
25843 it->ascent += it->descent - it->max_descent;
25844 it->descent = it->max_descent;
25846 if (it->ascent > it->max_ascent)
25848 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
25849 it->ascent = it->max_ascent;
25851 it->phys_ascent = min (it->phys_ascent, it->ascent);
25852 it->phys_descent = min (it->phys_descent, it->descent);
25853 extra_line_spacing = 0;
25856 /* If this is a space inside a region of text with
25857 `space-width' property, change its width. */
25858 stretched_p = it->char_to_display == ' ' && !NILP (it->space_width);
25859 if (stretched_p)
25860 it->pixel_width *= XFLOATINT (it->space_width);
25862 /* If face has a box, add the box thickness to the character
25863 height. If character has a box line to the left and/or
25864 right, add the box line width to the character's width. */
25865 if (face->box != FACE_NO_BOX)
25867 int thick = face->box_line_width;
25869 if (thick > 0)
25871 it->ascent += thick;
25872 it->descent += thick;
25874 else
25875 thick = -thick;
25877 if (it->start_of_box_run_p)
25878 it->pixel_width += thick;
25879 if (it->end_of_box_run_p)
25880 it->pixel_width += thick;
25883 /* If face has an overline, add the height of the overline
25884 (1 pixel) and a 1 pixel margin to the character height. */
25885 if (face->overline_p)
25886 it->ascent += overline_margin;
25888 if (it->constrain_row_ascent_descent_p)
25890 if (it->ascent > it->max_ascent)
25891 it->ascent = it->max_ascent;
25892 if (it->descent > it->max_descent)
25893 it->descent = it->max_descent;
25896 take_vertical_position_into_account (it);
25898 /* If we have to actually produce glyphs, do it. */
25899 if (it->glyph_row)
25901 if (stretched_p)
25903 /* Translate a space with a `space-width' property
25904 into a stretch glyph. */
25905 int ascent = (((it->ascent + it->descent) * FONT_BASE (font))
25906 / FONT_HEIGHT (font));
25907 append_stretch_glyph (it, it->object, it->pixel_width,
25908 it->ascent + it->descent, ascent);
25910 else
25911 append_glyph (it);
25913 /* If characters with lbearing or rbearing are displayed
25914 in this line, record that fact in a flag of the
25915 glyph row. This is used to optimize X output code. */
25916 if (pcm && (pcm->lbearing < 0 || pcm->rbearing > pcm->width))
25917 it->glyph_row->contains_overlapping_glyphs_p = 1;
25919 if (! stretched_p && it->pixel_width == 0)
25920 /* We assure that all visible glyphs have at least 1-pixel
25921 width. */
25922 it->pixel_width = 1;
25924 else if (it->char_to_display == '\n')
25926 /* A newline has no width, but we need the height of the
25927 line. But if previous part of the line sets a height,
25928 don't increase that height. */
25930 Lisp_Object height;
25931 Lisp_Object total_height = Qnil;
25933 it->override_ascent = -1;
25934 it->pixel_width = 0;
25935 it->nglyphs = 0;
25937 height = get_it_property (it, Qline_height);
25938 /* Split (line-height total-height) list. */
25939 if (CONSP (height)
25940 && CONSP (XCDR (height))
25941 && NILP (XCDR (XCDR (height))))
25943 total_height = XCAR (XCDR (height));
25944 height = XCAR (height);
25946 height = calc_line_height_property (it, height, font, boff, 1);
25948 if (it->override_ascent >= 0)
25950 it->ascent = it->override_ascent;
25951 it->descent = it->override_descent;
25952 boff = it->override_boff;
25954 else
25956 it->ascent = FONT_BASE (font) + boff;
25957 it->descent = FONT_DESCENT (font) - boff;
25960 if (EQ (height, Qt))
25962 if (it->descent > it->max_descent)
25964 it->ascent += it->descent - it->max_descent;
25965 it->descent = it->max_descent;
25967 if (it->ascent > it->max_ascent)
25969 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
25970 it->ascent = it->max_ascent;
25972 it->phys_ascent = min (it->phys_ascent, it->ascent);
25973 it->phys_descent = min (it->phys_descent, it->descent);
25974 it->constrain_row_ascent_descent_p = 1;
25975 extra_line_spacing = 0;
25977 else
25979 Lisp_Object spacing;
25981 it->phys_ascent = it->ascent;
25982 it->phys_descent = it->descent;
25984 if ((it->max_ascent > 0 || it->max_descent > 0)
25985 && face->box != FACE_NO_BOX
25986 && face->box_line_width > 0)
25988 it->ascent += face->box_line_width;
25989 it->descent += face->box_line_width;
25991 if (!NILP (height)
25992 && XINT (height) > it->ascent + it->descent)
25993 it->ascent = XINT (height) - it->descent;
25995 if (!NILP (total_height))
25996 spacing = calc_line_height_property (it, total_height, font, boff, 0);
25997 else
25999 spacing = get_it_property (it, Qline_spacing);
26000 spacing = calc_line_height_property (it, spacing, font, boff, 0);
26002 if (INTEGERP (spacing))
26004 extra_line_spacing = XINT (spacing);
26005 if (!NILP (total_height))
26006 extra_line_spacing -= (it->phys_ascent + it->phys_descent);
26010 else /* i.e. (it->char_to_display == '\t') */
26012 if (font->space_width > 0)
26014 int tab_width = it->tab_width * font->space_width;
26015 int x = it->current_x + it->continuation_lines_width;
26016 int next_tab_x = ((1 + x + tab_width - 1) / tab_width) * tab_width;
26018 /* If the distance from the current position to the next tab
26019 stop is less than a space character width, use the
26020 tab stop after that. */
26021 if (next_tab_x - x < font->space_width)
26022 next_tab_x += tab_width;
26024 it->pixel_width = next_tab_x - x;
26025 it->nglyphs = 1;
26026 it->ascent = it->phys_ascent = FONT_BASE (font) + boff;
26027 it->descent = it->phys_descent = FONT_DESCENT (font) - boff;
26029 if (it->glyph_row)
26031 append_stretch_glyph (it, it->object, it->pixel_width,
26032 it->ascent + it->descent, it->ascent);
26035 else
26037 it->pixel_width = 0;
26038 it->nglyphs = 1;
26042 else if (it->what == IT_COMPOSITION && it->cmp_it.ch < 0)
26044 /* A static composition.
26046 Note: A composition is represented as one glyph in the
26047 glyph matrix. There are no padding glyphs.
26049 Important note: pixel_width, ascent, and descent are the
26050 values of what is drawn by draw_glyphs (i.e. the values of
26051 the overall glyphs composed). */
26052 struct face *face = FACE_FROM_ID (it->f, it->face_id);
26053 int boff; /* baseline offset */
26054 struct composition *cmp = composition_table[it->cmp_it.id];
26055 int glyph_len = cmp->glyph_len;
26056 struct font *font = face->font;
26058 it->nglyphs = 1;
26060 /* If we have not yet calculated pixel size data of glyphs of
26061 the composition for the current face font, calculate them
26062 now. Theoretically, we have to check all fonts for the
26063 glyphs, but that requires much time and memory space. So,
26064 here we check only the font of the first glyph. This may
26065 lead to incorrect display, but it's very rare, and C-l
26066 (recenter-top-bottom) can correct the display anyway. */
26067 if (! cmp->font || cmp->font != font)
26069 /* Ascent and descent of the font of the first character
26070 of this composition (adjusted by baseline offset).
26071 Ascent and descent of overall glyphs should not be less
26072 than these, respectively. */
26073 int font_ascent, font_descent, font_height;
26074 /* Bounding box of the overall glyphs. */
26075 int leftmost, rightmost, lowest, highest;
26076 int lbearing, rbearing;
26077 int i, width, ascent, descent;
26078 int left_padded = 0, right_padded = 0;
26079 int c IF_LINT (= 0); /* cmp->glyph_len can't be zero; see Bug#8512 */
26080 XChar2b char2b;
26081 struct font_metrics *pcm;
26082 int font_not_found_p;
26083 ptrdiff_t pos;
26085 for (glyph_len = cmp->glyph_len; glyph_len > 0; glyph_len--)
26086 if ((c = COMPOSITION_GLYPH (cmp, glyph_len - 1)) != '\t')
26087 break;
26088 if (glyph_len < cmp->glyph_len)
26089 right_padded = 1;
26090 for (i = 0; i < glyph_len; i++)
26092 if ((c = COMPOSITION_GLYPH (cmp, i)) != '\t')
26093 break;
26094 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
26096 if (i > 0)
26097 left_padded = 1;
26099 pos = (STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
26100 : IT_CHARPOS (*it));
26101 /* If no suitable font is found, use the default font. */
26102 font_not_found_p = font == NULL;
26103 if (font_not_found_p)
26105 face = face->ascii_face;
26106 font = face->font;
26108 boff = font->baseline_offset;
26109 if (font->vertical_centering)
26110 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
26111 font_ascent = FONT_BASE (font) + boff;
26112 font_descent = FONT_DESCENT (font) - boff;
26113 font_height = FONT_HEIGHT (font);
26115 cmp->font = font;
26117 pcm = NULL;
26118 if (! font_not_found_p)
26120 get_char_face_and_encoding (it->f, c, it->face_id,
26121 &char2b, 0);
26122 pcm = get_per_char_metric (font, &char2b);
26125 /* Initialize the bounding box. */
26126 if (pcm)
26128 width = cmp->glyph_len > 0 ? pcm->width : 0;
26129 ascent = pcm->ascent;
26130 descent = pcm->descent;
26131 lbearing = pcm->lbearing;
26132 rbearing = pcm->rbearing;
26134 else
26136 width = cmp->glyph_len > 0 ? font->space_width : 0;
26137 ascent = FONT_BASE (font);
26138 descent = FONT_DESCENT (font);
26139 lbearing = 0;
26140 rbearing = width;
26143 rightmost = width;
26144 leftmost = 0;
26145 lowest = - descent + boff;
26146 highest = ascent + boff;
26148 if (! font_not_found_p
26149 && font->default_ascent
26150 && CHAR_TABLE_P (Vuse_default_ascent)
26151 && !NILP (Faref (Vuse_default_ascent,
26152 make_number (it->char_to_display))))
26153 highest = font->default_ascent + boff;
26155 /* Draw the first glyph at the normal position. It may be
26156 shifted to right later if some other glyphs are drawn
26157 at the left. */
26158 cmp->offsets[i * 2] = 0;
26159 cmp->offsets[i * 2 + 1] = boff;
26160 cmp->lbearing = lbearing;
26161 cmp->rbearing = rbearing;
26163 /* Set cmp->offsets for the remaining glyphs. */
26164 for (i++; i < glyph_len; i++)
26166 int left, right, btm, top;
26167 int ch = COMPOSITION_GLYPH (cmp, i);
26168 int face_id;
26169 struct face *this_face;
26171 if (ch == '\t')
26172 ch = ' ';
26173 face_id = FACE_FOR_CHAR (it->f, face, ch, pos, it->string);
26174 this_face = FACE_FROM_ID (it->f, face_id);
26175 font = this_face->font;
26177 if (font == NULL)
26178 pcm = NULL;
26179 else
26181 get_char_face_and_encoding (it->f, ch, face_id,
26182 &char2b, 0);
26183 pcm = get_per_char_metric (font, &char2b);
26185 if (! pcm)
26186 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
26187 else
26189 width = pcm->width;
26190 ascent = pcm->ascent;
26191 descent = pcm->descent;
26192 lbearing = pcm->lbearing;
26193 rbearing = pcm->rbearing;
26194 if (cmp->method != COMPOSITION_WITH_RULE_ALTCHARS)
26196 /* Relative composition with or without
26197 alternate chars. */
26198 left = (leftmost + rightmost - width) / 2;
26199 btm = - descent + boff;
26200 if (font->relative_compose
26201 && (! CHAR_TABLE_P (Vignore_relative_composition)
26202 || NILP (Faref (Vignore_relative_composition,
26203 make_number (ch)))))
26206 if (- descent >= font->relative_compose)
26207 /* One extra pixel between two glyphs. */
26208 btm = highest + 1;
26209 else if (ascent <= 0)
26210 /* One extra pixel between two glyphs. */
26211 btm = lowest - 1 - ascent - descent;
26214 else
26216 /* A composition rule is specified by an integer
26217 value that encodes global and new reference
26218 points (GREF and NREF). GREF and NREF are
26219 specified by numbers as below:
26221 0---1---2 -- ascent
26225 9--10--11 -- center
26227 ---3---4---5--- baseline
26229 6---7---8 -- descent
26231 int rule = COMPOSITION_RULE (cmp, i);
26232 int gref, nref, grefx, grefy, nrefx, nrefy, xoff, yoff;
26234 COMPOSITION_DECODE_RULE (rule, gref, nref, xoff, yoff);
26235 grefx = gref % 3, nrefx = nref % 3;
26236 grefy = gref / 3, nrefy = nref / 3;
26237 if (xoff)
26238 xoff = font_height * (xoff - 128) / 256;
26239 if (yoff)
26240 yoff = font_height * (yoff - 128) / 256;
26242 left = (leftmost
26243 + grefx * (rightmost - leftmost) / 2
26244 - nrefx * width / 2
26245 + xoff);
26247 btm = ((grefy == 0 ? highest
26248 : grefy == 1 ? 0
26249 : grefy == 2 ? lowest
26250 : (highest + lowest) / 2)
26251 - (nrefy == 0 ? ascent + descent
26252 : nrefy == 1 ? descent - boff
26253 : nrefy == 2 ? 0
26254 : (ascent + descent) / 2)
26255 + yoff);
26258 cmp->offsets[i * 2] = left;
26259 cmp->offsets[i * 2 + 1] = btm + descent;
26261 /* Update the bounding box of the overall glyphs. */
26262 if (width > 0)
26264 right = left + width;
26265 if (left < leftmost)
26266 leftmost = left;
26267 if (right > rightmost)
26268 rightmost = right;
26270 top = btm + descent + ascent;
26271 if (top > highest)
26272 highest = top;
26273 if (btm < lowest)
26274 lowest = btm;
26276 if (cmp->lbearing > left + lbearing)
26277 cmp->lbearing = left + lbearing;
26278 if (cmp->rbearing < left + rbearing)
26279 cmp->rbearing = left + rbearing;
26283 /* If there are glyphs whose x-offsets are negative,
26284 shift all glyphs to the right and make all x-offsets
26285 non-negative. */
26286 if (leftmost < 0)
26288 for (i = 0; i < cmp->glyph_len; i++)
26289 cmp->offsets[i * 2] -= leftmost;
26290 rightmost -= leftmost;
26291 cmp->lbearing -= leftmost;
26292 cmp->rbearing -= leftmost;
26295 if (left_padded && cmp->lbearing < 0)
26297 for (i = 0; i < cmp->glyph_len; i++)
26298 cmp->offsets[i * 2] -= cmp->lbearing;
26299 rightmost -= cmp->lbearing;
26300 cmp->rbearing -= cmp->lbearing;
26301 cmp->lbearing = 0;
26303 if (right_padded && rightmost < cmp->rbearing)
26305 rightmost = cmp->rbearing;
26308 cmp->pixel_width = rightmost;
26309 cmp->ascent = highest;
26310 cmp->descent = - lowest;
26311 if (cmp->ascent < font_ascent)
26312 cmp->ascent = font_ascent;
26313 if (cmp->descent < font_descent)
26314 cmp->descent = font_descent;
26317 if (it->glyph_row
26318 && (cmp->lbearing < 0
26319 || cmp->rbearing > cmp->pixel_width))
26320 it->glyph_row->contains_overlapping_glyphs_p = 1;
26322 it->pixel_width = cmp->pixel_width;
26323 it->ascent = it->phys_ascent = cmp->ascent;
26324 it->descent = it->phys_descent = cmp->descent;
26325 if (face->box != FACE_NO_BOX)
26327 int thick = face->box_line_width;
26329 if (thick > 0)
26331 it->ascent += thick;
26332 it->descent += thick;
26334 else
26335 thick = - thick;
26337 if (it->start_of_box_run_p)
26338 it->pixel_width += thick;
26339 if (it->end_of_box_run_p)
26340 it->pixel_width += thick;
26343 /* If face has an overline, add the height of the overline
26344 (1 pixel) and a 1 pixel margin to the character height. */
26345 if (face->overline_p)
26346 it->ascent += overline_margin;
26348 take_vertical_position_into_account (it);
26349 if (it->ascent < 0)
26350 it->ascent = 0;
26351 if (it->descent < 0)
26352 it->descent = 0;
26354 if (it->glyph_row && cmp->glyph_len > 0)
26355 append_composite_glyph (it);
26357 else if (it->what == IT_COMPOSITION)
26359 /* A dynamic (automatic) composition. */
26360 struct face *face = FACE_FROM_ID (it->f, it->face_id);
26361 Lisp_Object gstring;
26362 struct font_metrics metrics;
26364 it->nglyphs = 1;
26366 gstring = composition_gstring_from_id (it->cmp_it.id);
26367 it->pixel_width
26368 = composition_gstring_width (gstring, it->cmp_it.from, it->cmp_it.to,
26369 &metrics);
26370 if (it->glyph_row
26371 && (metrics.lbearing < 0 || metrics.rbearing > metrics.width))
26372 it->glyph_row->contains_overlapping_glyphs_p = 1;
26373 it->ascent = it->phys_ascent = metrics.ascent;
26374 it->descent = it->phys_descent = metrics.descent;
26375 if (face->box != FACE_NO_BOX)
26377 int thick = face->box_line_width;
26379 if (thick > 0)
26381 it->ascent += thick;
26382 it->descent += thick;
26384 else
26385 thick = - thick;
26387 if (it->start_of_box_run_p)
26388 it->pixel_width += thick;
26389 if (it->end_of_box_run_p)
26390 it->pixel_width += thick;
26392 /* If face has an overline, add the height of the overline
26393 (1 pixel) and a 1 pixel margin to the character height. */
26394 if (face->overline_p)
26395 it->ascent += overline_margin;
26396 take_vertical_position_into_account (it);
26397 if (it->ascent < 0)
26398 it->ascent = 0;
26399 if (it->descent < 0)
26400 it->descent = 0;
26402 if (it->glyph_row)
26403 append_composite_glyph (it);
26405 else if (it->what == IT_GLYPHLESS)
26406 produce_glyphless_glyph (it, 0, Qnil);
26407 else if (it->what == IT_IMAGE)
26408 produce_image_glyph (it);
26409 else if (it->what == IT_STRETCH)
26410 produce_stretch_glyph (it);
26412 done:
26413 /* Accumulate dimensions. Note: can't assume that it->descent > 0
26414 because this isn't true for images with `:ascent 100'. */
26415 eassert (it->ascent >= 0 && it->descent >= 0);
26416 if (it->area == TEXT_AREA)
26417 it->current_x += it->pixel_width;
26419 if (extra_line_spacing > 0)
26421 it->descent += extra_line_spacing;
26422 if (extra_line_spacing > it->max_extra_line_spacing)
26423 it->max_extra_line_spacing = extra_line_spacing;
26426 it->max_ascent = max (it->max_ascent, it->ascent);
26427 it->max_descent = max (it->max_descent, it->descent);
26428 it->max_phys_ascent = max (it->max_phys_ascent, it->phys_ascent);
26429 it->max_phys_descent = max (it->max_phys_descent, it->phys_descent);
26432 /* EXPORT for RIF:
26433 Output LEN glyphs starting at START at the nominal cursor position.
26434 Advance the nominal cursor over the text. UPDATED_ROW is the glyph row
26435 being updated, and UPDATED_AREA is the area of that row being updated. */
26437 void
26438 x_write_glyphs (struct window *w, struct glyph_row *updated_row,
26439 struct glyph *start, enum glyph_row_area updated_area, int len)
26441 int x, hpos, chpos = w->phys_cursor.hpos;
26443 eassert (updated_row);
26444 /* When the window is hscrolled, cursor hpos can legitimately be out
26445 of bounds, but we draw the cursor at the corresponding window
26446 margin in that case. */
26447 if (!updated_row->reversed_p && chpos < 0)
26448 chpos = 0;
26449 if (updated_row->reversed_p && chpos >= updated_row->used[TEXT_AREA])
26450 chpos = updated_row->used[TEXT_AREA] - 1;
26452 block_input ();
26454 /* Write glyphs. */
26456 hpos = start - updated_row->glyphs[updated_area];
26457 x = draw_glyphs (w, w->output_cursor.x,
26458 updated_row, updated_area,
26459 hpos, hpos + len,
26460 DRAW_NORMAL_TEXT, 0);
26462 /* Invalidate old phys cursor if the glyph at its hpos is redrawn. */
26463 if (updated_area == TEXT_AREA
26464 && w->phys_cursor_on_p
26465 && w->phys_cursor.vpos == w->output_cursor.vpos
26466 && chpos >= hpos
26467 && chpos < hpos + len)
26468 w->phys_cursor_on_p = 0;
26470 unblock_input ();
26472 /* Advance the output cursor. */
26473 w->output_cursor.hpos += len;
26474 w->output_cursor.x = x;
26478 /* EXPORT for RIF:
26479 Insert LEN glyphs from START at the nominal cursor position. */
26481 void
26482 x_insert_glyphs (struct window *w, struct glyph_row *updated_row,
26483 struct glyph *start, enum glyph_row_area updated_area, int len)
26485 struct frame *f;
26486 int line_height, shift_by_width, shifted_region_width;
26487 struct glyph_row *row;
26488 struct glyph *glyph;
26489 int frame_x, frame_y;
26490 ptrdiff_t hpos;
26492 eassert (updated_row);
26493 block_input ();
26494 f = XFRAME (WINDOW_FRAME (w));
26496 /* Get the height of the line we are in. */
26497 row = updated_row;
26498 line_height = row->height;
26500 /* Get the width of the glyphs to insert. */
26501 shift_by_width = 0;
26502 for (glyph = start; glyph < start + len; ++glyph)
26503 shift_by_width += glyph->pixel_width;
26505 /* Get the width of the region to shift right. */
26506 shifted_region_width = (window_box_width (w, updated_area)
26507 - w->output_cursor.x
26508 - shift_by_width);
26510 /* Shift right. */
26511 frame_x = window_box_left (w, updated_area) + w->output_cursor.x;
26512 frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, w->output_cursor.y);
26514 FRAME_RIF (f)->shift_glyphs_for_insert (f, frame_x, frame_y, shifted_region_width,
26515 line_height, shift_by_width);
26517 /* Write the glyphs. */
26518 hpos = start - row->glyphs[updated_area];
26519 draw_glyphs (w, w->output_cursor.x, row, updated_area,
26520 hpos, hpos + len,
26521 DRAW_NORMAL_TEXT, 0);
26523 /* Advance the output cursor. */
26524 w->output_cursor.hpos += len;
26525 w->output_cursor.x += shift_by_width;
26526 unblock_input ();
26530 /* EXPORT for RIF:
26531 Erase the current text line from the nominal cursor position
26532 (inclusive) to pixel column TO_X (exclusive). The idea is that
26533 everything from TO_X onward is already erased.
26535 TO_X is a pixel position relative to UPDATED_AREA of currently
26536 updated window W. TO_X == -1 means clear to the end of this area. */
26538 void
26539 x_clear_end_of_line (struct window *w, struct glyph_row *updated_row,
26540 enum glyph_row_area updated_area, int to_x)
26542 struct frame *f;
26543 int max_x, min_y, max_y;
26544 int from_x, from_y, to_y;
26546 eassert (updated_row);
26547 f = XFRAME (w->frame);
26549 if (updated_row->full_width_p)
26550 max_x = (WINDOW_PIXEL_WIDTH (w)
26551 - (updated_row->mode_line_p ? WINDOW_RIGHT_DIVIDER_WIDTH (w) : 0));
26552 else
26553 max_x = window_box_width (w, updated_area);
26554 max_y = window_text_bottom_y (w);
26556 /* TO_X == 0 means don't do anything. TO_X < 0 means clear to end
26557 of window. For TO_X > 0, truncate to end of drawing area. */
26558 if (to_x == 0)
26559 return;
26560 else if (to_x < 0)
26561 to_x = max_x;
26562 else
26563 to_x = min (to_x, max_x);
26565 to_y = min (max_y, w->output_cursor.y + updated_row->height);
26567 /* Notice if the cursor will be cleared by this operation. */
26568 if (!updated_row->full_width_p)
26569 notice_overwritten_cursor (w, updated_area,
26570 w->output_cursor.x, -1,
26571 updated_row->y,
26572 MATRIX_ROW_BOTTOM_Y (updated_row));
26574 from_x = w->output_cursor.x;
26576 /* Translate to frame coordinates. */
26577 if (updated_row->full_width_p)
26579 from_x = WINDOW_TO_FRAME_PIXEL_X (w, from_x);
26580 to_x = WINDOW_TO_FRAME_PIXEL_X (w, to_x);
26582 else
26584 int area_left = window_box_left (w, updated_area);
26585 from_x += area_left;
26586 to_x += area_left;
26589 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
26590 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, max (min_y, w->output_cursor.y));
26591 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, to_y);
26593 /* Prevent inadvertently clearing to end of the X window. */
26594 if (to_x > from_x && to_y > from_y)
26596 block_input ();
26597 FRAME_RIF (f)->clear_frame_area (f, from_x, from_y,
26598 to_x - from_x, to_y - from_y);
26599 unblock_input ();
26603 #endif /* HAVE_WINDOW_SYSTEM */
26607 /***********************************************************************
26608 Cursor types
26609 ***********************************************************************/
26611 /* Value is the internal representation of the specified cursor type
26612 ARG. If type is BAR_CURSOR, return in *WIDTH the specified width
26613 of the bar cursor. */
26615 static enum text_cursor_kinds
26616 get_specified_cursor_type (Lisp_Object arg, int *width)
26618 enum text_cursor_kinds type;
26620 if (NILP (arg))
26621 return NO_CURSOR;
26623 if (EQ (arg, Qbox))
26624 return FILLED_BOX_CURSOR;
26626 if (EQ (arg, Qhollow))
26627 return HOLLOW_BOX_CURSOR;
26629 if (EQ (arg, Qbar))
26631 *width = 2;
26632 return BAR_CURSOR;
26635 if (CONSP (arg)
26636 && EQ (XCAR (arg), Qbar)
26637 && RANGED_INTEGERP (0, XCDR (arg), INT_MAX))
26639 *width = XINT (XCDR (arg));
26640 return BAR_CURSOR;
26643 if (EQ (arg, Qhbar))
26645 *width = 2;
26646 return HBAR_CURSOR;
26649 if (CONSP (arg)
26650 && EQ (XCAR (arg), Qhbar)
26651 && RANGED_INTEGERP (0, XCDR (arg), INT_MAX))
26653 *width = XINT (XCDR (arg));
26654 return HBAR_CURSOR;
26657 /* Treat anything unknown as "hollow box cursor".
26658 It was bad to signal an error; people have trouble fixing
26659 .Xdefaults with Emacs, when it has something bad in it. */
26660 type = HOLLOW_BOX_CURSOR;
26662 return type;
26665 /* Set the default cursor types for specified frame. */
26666 void
26667 set_frame_cursor_types (struct frame *f, Lisp_Object arg)
26669 int width = 1;
26670 Lisp_Object tem;
26672 FRAME_DESIRED_CURSOR (f) = get_specified_cursor_type (arg, &width);
26673 FRAME_CURSOR_WIDTH (f) = width;
26675 /* By default, set up the blink-off state depending on the on-state. */
26677 tem = Fassoc (arg, Vblink_cursor_alist);
26678 if (!NILP (tem))
26680 FRAME_BLINK_OFF_CURSOR (f)
26681 = get_specified_cursor_type (XCDR (tem), &width);
26682 FRAME_BLINK_OFF_CURSOR_WIDTH (f) = width;
26684 else
26685 FRAME_BLINK_OFF_CURSOR (f) = DEFAULT_CURSOR;
26687 /* Make sure the cursor gets redrawn. */
26688 f->cursor_type_changed = 1;
26692 #ifdef HAVE_WINDOW_SYSTEM
26694 /* Return the cursor we want to be displayed in window W. Return
26695 width of bar/hbar cursor through WIDTH arg. Return with
26696 ACTIVE_CURSOR arg set to 1 if cursor in window W is `active'
26697 (i.e. if the `system caret' should track this cursor).
26699 In a mini-buffer window, we want the cursor only to appear if we
26700 are reading input from this window. For the selected window, we
26701 want the cursor type given by the frame parameter or buffer local
26702 setting of cursor-type. If explicitly marked off, draw no cursor.
26703 In all other cases, we want a hollow box cursor. */
26705 static enum text_cursor_kinds
26706 get_window_cursor_type (struct window *w, struct glyph *glyph, int *width,
26707 int *active_cursor)
26709 struct frame *f = XFRAME (w->frame);
26710 struct buffer *b = XBUFFER (w->contents);
26711 int cursor_type = DEFAULT_CURSOR;
26712 Lisp_Object alt_cursor;
26713 int non_selected = 0;
26715 *active_cursor = 1;
26717 /* Echo area */
26718 if (cursor_in_echo_area
26719 && FRAME_HAS_MINIBUF_P (f)
26720 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
26722 if (w == XWINDOW (echo_area_window))
26724 if (EQ (BVAR (b, cursor_type), Qt) || NILP (BVAR (b, cursor_type)))
26726 *width = FRAME_CURSOR_WIDTH (f);
26727 return FRAME_DESIRED_CURSOR (f);
26729 else
26730 return get_specified_cursor_type (BVAR (b, cursor_type), width);
26733 *active_cursor = 0;
26734 non_selected = 1;
26737 /* Detect a nonselected window or nonselected frame. */
26738 else if (w != XWINDOW (f->selected_window)
26739 || f != FRAME_DISPLAY_INFO (f)->x_highlight_frame)
26741 *active_cursor = 0;
26743 if (MINI_WINDOW_P (w) && minibuf_level == 0)
26744 return NO_CURSOR;
26746 non_selected = 1;
26749 /* Never display a cursor in a window in which cursor-type is nil. */
26750 if (NILP (BVAR (b, cursor_type)))
26751 return NO_CURSOR;
26753 /* Get the normal cursor type for this window. */
26754 if (EQ (BVAR (b, cursor_type), Qt))
26756 cursor_type = FRAME_DESIRED_CURSOR (f);
26757 *width = FRAME_CURSOR_WIDTH (f);
26759 else
26760 cursor_type = get_specified_cursor_type (BVAR (b, cursor_type), width);
26762 /* Use cursor-in-non-selected-windows instead
26763 for non-selected window or frame. */
26764 if (non_selected)
26766 alt_cursor = BVAR (b, cursor_in_non_selected_windows);
26767 if (!EQ (Qt, alt_cursor))
26768 return get_specified_cursor_type (alt_cursor, width);
26769 /* t means modify the normal cursor type. */
26770 if (cursor_type == FILLED_BOX_CURSOR)
26771 cursor_type = HOLLOW_BOX_CURSOR;
26772 else if (cursor_type == BAR_CURSOR && *width > 1)
26773 --*width;
26774 return cursor_type;
26777 /* Use normal cursor if not blinked off. */
26778 if (!w->cursor_off_p)
26780 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
26782 if (cursor_type == FILLED_BOX_CURSOR)
26784 /* Using a block cursor on large images can be very annoying.
26785 So use a hollow cursor for "large" images.
26786 If image is not transparent (no mask), also use hollow cursor. */
26787 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
26788 if (img != NULL && IMAGEP (img->spec))
26790 /* Arbitrarily, interpret "Large" as >32x32 and >NxN
26791 where N = size of default frame font size.
26792 This should cover most of the "tiny" icons people may use. */
26793 if (!img->mask
26794 || img->width > max (32, WINDOW_FRAME_COLUMN_WIDTH (w))
26795 || img->height > max (32, WINDOW_FRAME_LINE_HEIGHT (w)))
26796 cursor_type = HOLLOW_BOX_CURSOR;
26799 else if (cursor_type != NO_CURSOR)
26801 /* Display current only supports BOX and HOLLOW cursors for images.
26802 So for now, unconditionally use a HOLLOW cursor when cursor is
26803 not a solid box cursor. */
26804 cursor_type = HOLLOW_BOX_CURSOR;
26807 return cursor_type;
26810 /* Cursor is blinked off, so determine how to "toggle" it. */
26812 /* First look for an entry matching the buffer's cursor-type in blink-cursor-alist. */
26813 if ((alt_cursor = Fassoc (BVAR (b, cursor_type), Vblink_cursor_alist), !NILP (alt_cursor)))
26814 return get_specified_cursor_type (XCDR (alt_cursor), width);
26816 /* Then see if frame has specified a specific blink off cursor type. */
26817 if (FRAME_BLINK_OFF_CURSOR (f) != DEFAULT_CURSOR)
26819 *width = FRAME_BLINK_OFF_CURSOR_WIDTH (f);
26820 return FRAME_BLINK_OFF_CURSOR (f);
26823 #if 0
26824 /* Some people liked having a permanently visible blinking cursor,
26825 while others had very strong opinions against it. So it was
26826 decided to remove it. KFS 2003-09-03 */
26828 /* Finally perform built-in cursor blinking:
26829 filled box <-> hollow box
26830 wide [h]bar <-> narrow [h]bar
26831 narrow [h]bar <-> no cursor
26832 other type <-> no cursor */
26834 if (cursor_type == FILLED_BOX_CURSOR)
26835 return HOLLOW_BOX_CURSOR;
26837 if ((cursor_type == BAR_CURSOR || cursor_type == HBAR_CURSOR) && *width > 1)
26839 *width = 1;
26840 return cursor_type;
26842 #endif
26844 return NO_CURSOR;
26848 /* Notice when the text cursor of window W has been completely
26849 overwritten by a drawing operation that outputs glyphs in AREA
26850 starting at X0 and ending at X1 in the line starting at Y0 and
26851 ending at Y1. X coordinates are area-relative. X1 < 0 means all
26852 the rest of the line after X0 has been written. Y coordinates
26853 are window-relative. */
26855 static void
26856 notice_overwritten_cursor (struct window *w, enum glyph_row_area area,
26857 int x0, int x1, int y0, int y1)
26859 int cx0, cx1, cy0, cy1;
26860 struct glyph_row *row;
26862 if (!w->phys_cursor_on_p)
26863 return;
26864 if (area != TEXT_AREA)
26865 return;
26867 if (w->phys_cursor.vpos < 0
26868 || w->phys_cursor.vpos >= w->current_matrix->nrows
26869 || (row = w->current_matrix->rows + w->phys_cursor.vpos,
26870 !(row->enabled_p && MATRIX_ROW_DISPLAYS_TEXT_P (row))))
26871 return;
26873 if (row->cursor_in_fringe_p)
26875 row->cursor_in_fringe_p = 0;
26876 draw_fringe_bitmap (w, row, row->reversed_p);
26877 w->phys_cursor_on_p = 0;
26878 return;
26881 cx0 = w->phys_cursor.x;
26882 cx1 = cx0 + w->phys_cursor_width;
26883 if (x0 > cx0 || (x1 >= 0 && x1 < cx1))
26884 return;
26886 /* The cursor image will be completely removed from the
26887 screen if the output area intersects the cursor area in
26888 y-direction. When we draw in [y0 y1[, and some part of
26889 the cursor is at y < y0, that part must have been drawn
26890 before. When scrolling, the cursor is erased before
26891 actually scrolling, so we don't come here. When not
26892 scrolling, the rows above the old cursor row must have
26893 changed, and in this case these rows must have written
26894 over the cursor image.
26896 Likewise if part of the cursor is below y1, with the
26897 exception of the cursor being in the first blank row at
26898 the buffer and window end because update_text_area
26899 doesn't draw that row. (Except when it does, but
26900 that's handled in update_text_area.) */
26902 cy0 = w->phys_cursor.y;
26903 cy1 = cy0 + w->phys_cursor_height;
26904 if ((y0 < cy0 || y0 >= cy1) && (y1 <= cy0 || y1 >= cy1))
26905 return;
26907 w->phys_cursor_on_p = 0;
26910 #endif /* HAVE_WINDOW_SYSTEM */
26913 /************************************************************************
26914 Mouse Face
26915 ************************************************************************/
26917 #ifdef HAVE_WINDOW_SYSTEM
26919 /* EXPORT for RIF:
26920 Fix the display of area AREA of overlapping row ROW in window W
26921 with respect to the overlapping part OVERLAPS. */
26923 void
26924 x_fix_overlapping_area (struct window *w, struct glyph_row *row,
26925 enum glyph_row_area area, int overlaps)
26927 int i, x;
26929 block_input ();
26931 x = 0;
26932 for (i = 0; i < row->used[area];)
26934 if (row->glyphs[area][i].overlaps_vertically_p)
26936 int start = i, start_x = x;
26940 x += row->glyphs[area][i].pixel_width;
26941 ++i;
26943 while (i < row->used[area]
26944 && row->glyphs[area][i].overlaps_vertically_p);
26946 draw_glyphs (w, start_x, row, area,
26947 start, i,
26948 DRAW_NORMAL_TEXT, overlaps);
26950 else
26952 x += row->glyphs[area][i].pixel_width;
26953 ++i;
26957 unblock_input ();
26961 /* EXPORT:
26962 Draw the cursor glyph of window W in glyph row ROW. See the
26963 comment of draw_glyphs for the meaning of HL. */
26965 void
26966 draw_phys_cursor_glyph (struct window *w, struct glyph_row *row,
26967 enum draw_glyphs_face hl)
26969 /* If cursor hpos is out of bounds, don't draw garbage. This can
26970 happen in mini-buffer windows when switching between echo area
26971 glyphs and mini-buffer. */
26972 if ((row->reversed_p
26973 ? (w->phys_cursor.hpos >= 0)
26974 : (w->phys_cursor.hpos < row->used[TEXT_AREA])))
26976 int on_p = w->phys_cursor_on_p;
26977 int x1;
26978 int hpos = w->phys_cursor.hpos;
26980 /* When the window is hscrolled, cursor hpos can legitimately be
26981 out of bounds, but we draw the cursor at the corresponding
26982 window margin in that case. */
26983 if (!row->reversed_p && hpos < 0)
26984 hpos = 0;
26985 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
26986 hpos = row->used[TEXT_AREA] - 1;
26988 x1 = draw_glyphs (w, w->phys_cursor.x, row, TEXT_AREA, hpos, hpos + 1,
26989 hl, 0);
26990 w->phys_cursor_on_p = on_p;
26992 if (hl == DRAW_CURSOR)
26993 w->phys_cursor_width = x1 - w->phys_cursor.x;
26994 /* When we erase the cursor, and ROW is overlapped by other
26995 rows, make sure that these overlapping parts of other rows
26996 are redrawn. */
26997 else if (hl == DRAW_NORMAL_TEXT && row->overlapped_p)
26999 w->phys_cursor_width = x1 - w->phys_cursor.x;
27001 if (row > w->current_matrix->rows
27002 && MATRIX_ROW_OVERLAPS_SUCC_P (row - 1))
27003 x_fix_overlapping_area (w, row - 1, TEXT_AREA,
27004 OVERLAPS_ERASED_CURSOR);
27006 if (MATRIX_ROW_BOTTOM_Y (row) < window_text_bottom_y (w)
27007 && MATRIX_ROW_OVERLAPS_PRED_P (row + 1))
27008 x_fix_overlapping_area (w, row + 1, TEXT_AREA,
27009 OVERLAPS_ERASED_CURSOR);
27015 /* Erase the image of a cursor of window W from the screen. */
27017 #ifndef HAVE_NTGUI
27018 static
27019 #endif
27020 void
27021 erase_phys_cursor (struct window *w)
27023 struct frame *f = XFRAME (w->frame);
27024 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
27025 int hpos = w->phys_cursor.hpos;
27026 int vpos = w->phys_cursor.vpos;
27027 int mouse_face_here_p = 0;
27028 struct glyph_matrix *active_glyphs = w->current_matrix;
27029 struct glyph_row *cursor_row;
27030 struct glyph *cursor_glyph;
27031 enum draw_glyphs_face hl;
27033 /* No cursor displayed or row invalidated => nothing to do on the
27034 screen. */
27035 if (w->phys_cursor_type == NO_CURSOR)
27036 goto mark_cursor_off;
27038 /* VPOS >= active_glyphs->nrows means that window has been resized.
27039 Don't bother to erase the cursor. */
27040 if (vpos >= active_glyphs->nrows)
27041 goto mark_cursor_off;
27043 /* If row containing cursor is marked invalid, there is nothing we
27044 can do. */
27045 cursor_row = MATRIX_ROW (active_glyphs, vpos);
27046 if (!cursor_row->enabled_p)
27047 goto mark_cursor_off;
27049 /* If line spacing is > 0, old cursor may only be partially visible in
27050 window after split-window. So adjust visible height. */
27051 cursor_row->visible_height = min (cursor_row->visible_height,
27052 window_text_bottom_y (w) - cursor_row->y);
27054 /* If row is completely invisible, don't attempt to delete a cursor which
27055 isn't there. This can happen if cursor is at top of a window, and
27056 we switch to a buffer with a header line in that window. */
27057 if (cursor_row->visible_height <= 0)
27058 goto mark_cursor_off;
27060 /* If cursor is in the fringe, erase by drawing actual bitmap there. */
27061 if (cursor_row->cursor_in_fringe_p)
27063 cursor_row->cursor_in_fringe_p = 0;
27064 draw_fringe_bitmap (w, cursor_row, cursor_row->reversed_p);
27065 goto mark_cursor_off;
27068 /* This can happen when the new row is shorter than the old one.
27069 In this case, either draw_glyphs or clear_end_of_line
27070 should have cleared the cursor. Note that we wouldn't be
27071 able to erase the cursor in this case because we don't have a
27072 cursor glyph at hand. */
27073 if ((cursor_row->reversed_p
27074 ? (w->phys_cursor.hpos < 0)
27075 : (w->phys_cursor.hpos >= cursor_row->used[TEXT_AREA])))
27076 goto mark_cursor_off;
27078 /* When the window is hscrolled, cursor hpos can legitimately be out
27079 of bounds, but we draw the cursor at the corresponding window
27080 margin in that case. */
27081 if (!cursor_row->reversed_p && hpos < 0)
27082 hpos = 0;
27083 if (cursor_row->reversed_p && hpos >= cursor_row->used[TEXT_AREA])
27084 hpos = cursor_row->used[TEXT_AREA] - 1;
27086 /* If the cursor is in the mouse face area, redisplay that when
27087 we clear the cursor. */
27088 if (! NILP (hlinfo->mouse_face_window)
27089 && coords_in_mouse_face_p (w, hpos, vpos)
27090 /* Don't redraw the cursor's spot in mouse face if it is at the
27091 end of a line (on a newline). The cursor appears there, but
27092 mouse highlighting does not. */
27093 && cursor_row->used[TEXT_AREA] > hpos && hpos >= 0)
27094 mouse_face_here_p = 1;
27096 /* Maybe clear the display under the cursor. */
27097 if (w->phys_cursor_type == HOLLOW_BOX_CURSOR)
27099 int x, y, left_x;
27100 int header_line_height = WINDOW_HEADER_LINE_HEIGHT (w);
27101 int width;
27103 cursor_glyph = get_phys_cursor_glyph (w);
27104 if (cursor_glyph == NULL)
27105 goto mark_cursor_off;
27107 width = cursor_glyph->pixel_width;
27108 left_x = window_box_left_offset (w, TEXT_AREA);
27109 x = w->phys_cursor.x;
27110 if (x < left_x)
27111 width -= left_x - x;
27112 width = min (width, window_box_width (w, TEXT_AREA) - x);
27113 y = WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height, cursor_row->y));
27114 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, max (x, left_x));
27116 if (width > 0)
27117 FRAME_RIF (f)->clear_frame_area (f, x, y, width, cursor_row->visible_height);
27120 /* Erase the cursor by redrawing the character underneath it. */
27121 if (mouse_face_here_p)
27122 hl = DRAW_MOUSE_FACE;
27123 else
27124 hl = DRAW_NORMAL_TEXT;
27125 draw_phys_cursor_glyph (w, cursor_row, hl);
27127 mark_cursor_off:
27128 w->phys_cursor_on_p = 0;
27129 w->phys_cursor_type = NO_CURSOR;
27133 /* EXPORT:
27134 Display or clear cursor of window W. If ON is zero, clear the
27135 cursor. If it is non-zero, display the cursor. If ON is nonzero,
27136 where to put the cursor is specified by HPOS, VPOS, X and Y. */
27138 void
27139 display_and_set_cursor (struct window *w, bool on,
27140 int hpos, int vpos, int x, int y)
27142 struct frame *f = XFRAME (w->frame);
27143 int new_cursor_type;
27144 int new_cursor_width;
27145 int active_cursor;
27146 struct glyph_row *glyph_row;
27147 struct glyph *glyph;
27149 /* This is pointless on invisible frames, and dangerous on garbaged
27150 windows and frames; in the latter case, the frame or window may
27151 be in the midst of changing its size, and x and y may be off the
27152 window. */
27153 if (! FRAME_VISIBLE_P (f)
27154 || FRAME_GARBAGED_P (f)
27155 || vpos >= w->current_matrix->nrows
27156 || hpos >= w->current_matrix->matrix_w)
27157 return;
27159 /* If cursor is off and we want it off, return quickly. */
27160 if (!on && !w->phys_cursor_on_p)
27161 return;
27163 glyph_row = MATRIX_ROW (w->current_matrix, vpos);
27164 /* If cursor row is not enabled, we don't really know where to
27165 display the cursor. */
27166 if (!glyph_row->enabled_p)
27168 w->phys_cursor_on_p = 0;
27169 return;
27172 glyph = NULL;
27173 if (!glyph_row->exact_window_width_line_p
27174 || (0 <= hpos && hpos < glyph_row->used[TEXT_AREA]))
27175 glyph = glyph_row->glyphs[TEXT_AREA] + hpos;
27177 eassert (input_blocked_p ());
27179 /* Set new_cursor_type to the cursor we want to be displayed. */
27180 new_cursor_type = get_window_cursor_type (w, glyph,
27181 &new_cursor_width, &active_cursor);
27183 /* If cursor is currently being shown and we don't want it to be or
27184 it is in the wrong place, or the cursor type is not what we want,
27185 erase it. */
27186 if (w->phys_cursor_on_p
27187 && (!on
27188 || w->phys_cursor.x != x
27189 || w->phys_cursor.y != y
27190 || new_cursor_type != w->phys_cursor_type
27191 || ((new_cursor_type == BAR_CURSOR || new_cursor_type == HBAR_CURSOR)
27192 && new_cursor_width != w->phys_cursor_width)))
27193 erase_phys_cursor (w);
27195 /* Don't check phys_cursor_on_p here because that flag is only set
27196 to zero in some cases where we know that the cursor has been
27197 completely erased, to avoid the extra work of erasing the cursor
27198 twice. In other words, phys_cursor_on_p can be 1 and the cursor
27199 still not be visible, or it has only been partly erased. */
27200 if (on)
27202 w->phys_cursor_ascent = glyph_row->ascent;
27203 w->phys_cursor_height = glyph_row->height;
27205 /* Set phys_cursor_.* before x_draw_.* is called because some
27206 of them may need the information. */
27207 w->phys_cursor.x = x;
27208 w->phys_cursor.y = glyph_row->y;
27209 w->phys_cursor.hpos = hpos;
27210 w->phys_cursor.vpos = vpos;
27213 FRAME_RIF (f)->draw_window_cursor (w, glyph_row, x, y,
27214 new_cursor_type, new_cursor_width,
27215 on, active_cursor);
27219 /* Switch the display of W's cursor on or off, according to the value
27220 of ON. */
27222 static void
27223 update_window_cursor (struct window *w, bool on)
27225 /* Don't update cursor in windows whose frame is in the process
27226 of being deleted. */
27227 if (w->current_matrix)
27229 int hpos = w->phys_cursor.hpos;
27230 int vpos = w->phys_cursor.vpos;
27231 struct glyph_row *row;
27233 if (vpos >= w->current_matrix->nrows
27234 || hpos >= w->current_matrix->matrix_w)
27235 return;
27237 row = MATRIX_ROW (w->current_matrix, vpos);
27239 /* When the window is hscrolled, cursor hpos can legitimately be
27240 out of bounds, but we draw the cursor at the corresponding
27241 window margin in that case. */
27242 if (!row->reversed_p && hpos < 0)
27243 hpos = 0;
27244 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
27245 hpos = row->used[TEXT_AREA] - 1;
27247 block_input ();
27248 display_and_set_cursor (w, on, hpos, vpos,
27249 w->phys_cursor.x, w->phys_cursor.y);
27250 unblock_input ();
27255 /* Call update_window_cursor with parameter ON_P on all leaf windows
27256 in the window tree rooted at W. */
27258 static void
27259 update_cursor_in_window_tree (struct window *w, bool on_p)
27261 while (w)
27263 if (WINDOWP (w->contents))
27264 update_cursor_in_window_tree (XWINDOW (w->contents), on_p);
27265 else
27266 update_window_cursor (w, on_p);
27268 w = NILP (w->next) ? 0 : XWINDOW (w->next);
27273 /* EXPORT:
27274 Display the cursor on window W, or clear it, according to ON_P.
27275 Don't change the cursor's position. */
27277 void
27278 x_update_cursor (struct frame *f, bool on_p)
27280 update_cursor_in_window_tree (XWINDOW (f->root_window), on_p);
27284 /* EXPORT:
27285 Clear the cursor of window W to background color, and mark the
27286 cursor as not shown. This is used when the text where the cursor
27287 is about to be rewritten. */
27289 void
27290 x_clear_cursor (struct window *w)
27292 if (FRAME_VISIBLE_P (XFRAME (w->frame)) && w->phys_cursor_on_p)
27293 update_window_cursor (w, 0);
27296 #endif /* HAVE_WINDOW_SYSTEM */
27298 /* Implementation of draw_row_with_mouse_face for GUI sessions, GPM,
27299 and MSDOS. */
27300 static void
27301 draw_row_with_mouse_face (struct window *w, int start_x, struct glyph_row *row,
27302 int start_hpos, int end_hpos,
27303 enum draw_glyphs_face draw)
27305 #ifdef HAVE_WINDOW_SYSTEM
27306 if (FRAME_WINDOW_P (XFRAME (w->frame)))
27308 draw_glyphs (w, start_x, row, TEXT_AREA, start_hpos, end_hpos, draw, 0);
27309 return;
27311 #endif
27312 #if defined (HAVE_GPM) || defined (MSDOS) || defined (WINDOWSNT)
27313 tty_draw_row_with_mouse_face (w, row, start_hpos, end_hpos, draw);
27314 #endif
27317 /* Display the active region described by mouse_face_* according to DRAW. */
27319 static void
27320 show_mouse_face (Mouse_HLInfo *hlinfo, enum draw_glyphs_face draw)
27322 struct window *w = XWINDOW (hlinfo->mouse_face_window);
27323 struct frame *f = XFRAME (WINDOW_FRAME (w));
27325 if (/* If window is in the process of being destroyed, don't bother
27326 to do anything. */
27327 w->current_matrix != NULL
27328 /* Don't update mouse highlight if hidden. */
27329 && (draw != DRAW_MOUSE_FACE || !hlinfo->mouse_face_hidden)
27330 /* Recognize when we are called to operate on rows that don't exist
27331 anymore. This can happen when a window is split. */
27332 && hlinfo->mouse_face_end_row < w->current_matrix->nrows)
27334 int phys_cursor_on_p = w->phys_cursor_on_p;
27335 struct glyph_row *row, *first, *last;
27337 first = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_beg_row);
27338 last = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_end_row);
27340 for (row = first; row <= last && row->enabled_p; ++row)
27342 int start_hpos, end_hpos, start_x;
27344 /* For all but the first row, the highlight starts at column 0. */
27345 if (row == first)
27347 /* R2L rows have BEG and END in reversed order, but the
27348 screen drawing geometry is always left to right. So
27349 we need to mirror the beginning and end of the
27350 highlighted area in R2L rows. */
27351 if (!row->reversed_p)
27353 start_hpos = hlinfo->mouse_face_beg_col;
27354 start_x = hlinfo->mouse_face_beg_x;
27356 else if (row == last)
27358 start_hpos = hlinfo->mouse_face_end_col;
27359 start_x = hlinfo->mouse_face_end_x;
27361 else
27363 start_hpos = 0;
27364 start_x = 0;
27367 else if (row->reversed_p && row == last)
27369 start_hpos = hlinfo->mouse_face_end_col;
27370 start_x = hlinfo->mouse_face_end_x;
27372 else
27374 start_hpos = 0;
27375 start_x = 0;
27378 if (row == last)
27380 if (!row->reversed_p)
27381 end_hpos = hlinfo->mouse_face_end_col;
27382 else if (row == first)
27383 end_hpos = hlinfo->mouse_face_beg_col;
27384 else
27386 end_hpos = row->used[TEXT_AREA];
27387 if (draw == DRAW_NORMAL_TEXT)
27388 row->fill_line_p = 1; /* Clear to end of line */
27391 else if (row->reversed_p && row == first)
27392 end_hpos = hlinfo->mouse_face_beg_col;
27393 else
27395 end_hpos = row->used[TEXT_AREA];
27396 if (draw == DRAW_NORMAL_TEXT)
27397 row->fill_line_p = 1; /* Clear to end of line */
27400 if (end_hpos > start_hpos)
27402 draw_row_with_mouse_face (w, start_x, row,
27403 start_hpos, end_hpos, draw);
27405 row->mouse_face_p
27406 = draw == DRAW_MOUSE_FACE || draw == DRAW_IMAGE_RAISED;
27410 #ifdef HAVE_WINDOW_SYSTEM
27411 /* When we've written over the cursor, arrange for it to
27412 be displayed again. */
27413 if (FRAME_WINDOW_P (f)
27414 && phys_cursor_on_p && !w->phys_cursor_on_p)
27416 int hpos = w->phys_cursor.hpos;
27418 /* When the window is hscrolled, cursor hpos can legitimately be
27419 out of bounds, but we draw the cursor at the corresponding
27420 window margin in that case. */
27421 if (!row->reversed_p && hpos < 0)
27422 hpos = 0;
27423 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
27424 hpos = row->used[TEXT_AREA] - 1;
27426 block_input ();
27427 display_and_set_cursor (w, 1, hpos, w->phys_cursor.vpos,
27428 w->phys_cursor.x, w->phys_cursor.y);
27429 unblock_input ();
27431 #endif /* HAVE_WINDOW_SYSTEM */
27434 #ifdef HAVE_WINDOW_SYSTEM
27435 /* Change the mouse cursor. */
27436 if (FRAME_WINDOW_P (f))
27438 #if ! defined (USE_GTK) && ! defined (HAVE_NS)
27439 if (draw == DRAW_NORMAL_TEXT
27440 && !EQ (hlinfo->mouse_face_window, f->tool_bar_window))
27441 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->text_cursor);
27442 else
27443 #endif
27444 if (draw == DRAW_MOUSE_FACE)
27445 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->hand_cursor);
27446 else
27447 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->nontext_cursor);
27449 #endif /* HAVE_WINDOW_SYSTEM */
27452 /* EXPORT:
27453 Clear out the mouse-highlighted active region.
27454 Redraw it un-highlighted first. Value is non-zero if mouse
27455 face was actually drawn unhighlighted. */
27458 clear_mouse_face (Mouse_HLInfo *hlinfo)
27460 int cleared = 0;
27462 if (!hlinfo->mouse_face_hidden && !NILP (hlinfo->mouse_face_window))
27464 show_mouse_face (hlinfo, DRAW_NORMAL_TEXT);
27465 cleared = 1;
27468 hlinfo->mouse_face_beg_row = hlinfo->mouse_face_beg_col = -1;
27469 hlinfo->mouse_face_end_row = hlinfo->mouse_face_end_col = -1;
27470 hlinfo->mouse_face_window = Qnil;
27471 hlinfo->mouse_face_overlay = Qnil;
27472 return cleared;
27475 /* Return true if the coordinates HPOS and VPOS on windows W are
27476 within the mouse face on that window. */
27477 static bool
27478 coords_in_mouse_face_p (struct window *w, int hpos, int vpos)
27480 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (XFRAME (w->frame));
27482 /* Quickly resolve the easy cases. */
27483 if (!(WINDOWP (hlinfo->mouse_face_window)
27484 && XWINDOW (hlinfo->mouse_face_window) == w))
27485 return false;
27486 if (vpos < hlinfo->mouse_face_beg_row
27487 || vpos > hlinfo->mouse_face_end_row)
27488 return false;
27489 if (vpos > hlinfo->mouse_face_beg_row
27490 && vpos < hlinfo->mouse_face_end_row)
27491 return true;
27493 if (!MATRIX_ROW (w->current_matrix, vpos)->reversed_p)
27495 if (hlinfo->mouse_face_beg_row == hlinfo->mouse_face_end_row)
27497 if (hlinfo->mouse_face_beg_col <= hpos && hpos < hlinfo->mouse_face_end_col)
27498 return true;
27500 else if ((vpos == hlinfo->mouse_face_beg_row
27501 && hpos >= hlinfo->mouse_face_beg_col)
27502 || (vpos == hlinfo->mouse_face_end_row
27503 && hpos < hlinfo->mouse_face_end_col))
27504 return true;
27506 else
27508 if (hlinfo->mouse_face_beg_row == hlinfo->mouse_face_end_row)
27510 if (hlinfo->mouse_face_end_col < hpos && hpos <= hlinfo->mouse_face_beg_col)
27511 return true;
27513 else if ((vpos == hlinfo->mouse_face_beg_row
27514 && hpos <= hlinfo->mouse_face_beg_col)
27515 || (vpos == hlinfo->mouse_face_end_row
27516 && hpos > hlinfo->mouse_face_end_col))
27517 return true;
27519 return false;
27523 /* EXPORT:
27524 True if physical cursor of window W is within mouse face. */
27526 bool
27527 cursor_in_mouse_face_p (struct window *w)
27529 int hpos = w->phys_cursor.hpos;
27530 int vpos = w->phys_cursor.vpos;
27531 struct glyph_row *row = MATRIX_ROW (w->current_matrix, vpos);
27533 /* When the window is hscrolled, cursor hpos can legitimately be out
27534 of bounds, but we draw the cursor at the corresponding window
27535 margin in that case. */
27536 if (!row->reversed_p && hpos < 0)
27537 hpos = 0;
27538 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
27539 hpos = row->used[TEXT_AREA] - 1;
27541 return coords_in_mouse_face_p (w, hpos, vpos);
27546 /* Find the glyph rows START_ROW and END_ROW of window W that display
27547 characters between buffer positions START_CHARPOS and END_CHARPOS
27548 (excluding END_CHARPOS). DISP_STRING is a display string that
27549 covers these buffer positions. This is similar to
27550 row_containing_pos, but is more accurate when bidi reordering makes
27551 buffer positions change non-linearly with glyph rows. */
27552 static void
27553 rows_from_pos_range (struct window *w,
27554 ptrdiff_t start_charpos, ptrdiff_t end_charpos,
27555 Lisp_Object disp_string,
27556 struct glyph_row **start, struct glyph_row **end)
27558 struct glyph_row *first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
27559 int last_y = window_text_bottom_y (w);
27560 struct glyph_row *row;
27562 *start = NULL;
27563 *end = NULL;
27565 while (!first->enabled_p
27566 && first < MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w))
27567 first++;
27569 /* Find the START row. */
27570 for (row = first;
27571 row->enabled_p && MATRIX_ROW_BOTTOM_Y (row) <= last_y;
27572 row++)
27574 /* A row can potentially be the START row if the range of the
27575 characters it displays intersects the range
27576 [START_CHARPOS..END_CHARPOS). */
27577 if (! ((start_charpos < MATRIX_ROW_START_CHARPOS (row)
27578 && end_charpos < MATRIX_ROW_START_CHARPOS (row))
27579 /* See the commentary in row_containing_pos, for the
27580 explanation of the complicated way to check whether
27581 some position is beyond the end of the characters
27582 displayed by a row. */
27583 || ((start_charpos > MATRIX_ROW_END_CHARPOS (row)
27584 || (start_charpos == MATRIX_ROW_END_CHARPOS (row)
27585 && !row->ends_at_zv_p
27586 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
27587 && (end_charpos > MATRIX_ROW_END_CHARPOS (row)
27588 || (end_charpos == MATRIX_ROW_END_CHARPOS (row)
27589 && !row->ends_at_zv_p
27590 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))))))
27592 /* Found a candidate row. Now make sure at least one of the
27593 glyphs it displays has a charpos from the range
27594 [START_CHARPOS..END_CHARPOS).
27596 This is not obvious because bidi reordering could make
27597 buffer positions of a row be 1,2,3,102,101,100, and if we
27598 want to highlight characters in [50..60), we don't want
27599 this row, even though [50..60) does intersect [1..103),
27600 the range of character positions given by the row's start
27601 and end positions. */
27602 struct glyph *g = row->glyphs[TEXT_AREA];
27603 struct glyph *e = g + row->used[TEXT_AREA];
27605 while (g < e)
27607 if (((BUFFERP (g->object) || INTEGERP (g->object))
27608 && start_charpos <= g->charpos && g->charpos < end_charpos)
27609 /* A glyph that comes from DISP_STRING is by
27610 definition to be highlighted. */
27611 || EQ (g->object, disp_string))
27612 *start = row;
27613 g++;
27615 if (*start)
27616 break;
27620 /* Find the END row. */
27621 if (!*start
27622 /* If the last row is partially visible, start looking for END
27623 from that row, instead of starting from FIRST. */
27624 && !(row->enabled_p
27625 && row->y < last_y && MATRIX_ROW_BOTTOM_Y (row) > last_y))
27626 row = first;
27627 for ( ; row->enabled_p && MATRIX_ROW_BOTTOM_Y (row) <= last_y; row++)
27629 struct glyph_row *next = row + 1;
27630 ptrdiff_t next_start = MATRIX_ROW_START_CHARPOS (next);
27632 if (!next->enabled_p
27633 || next >= MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w)
27634 /* The first row >= START whose range of displayed characters
27635 does NOT intersect the range [START_CHARPOS..END_CHARPOS]
27636 is the row END + 1. */
27637 || (start_charpos < next_start
27638 && end_charpos < next_start)
27639 || ((start_charpos > MATRIX_ROW_END_CHARPOS (next)
27640 || (start_charpos == MATRIX_ROW_END_CHARPOS (next)
27641 && !next->ends_at_zv_p
27642 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (next)))
27643 && (end_charpos > MATRIX_ROW_END_CHARPOS (next)
27644 || (end_charpos == MATRIX_ROW_END_CHARPOS (next)
27645 && !next->ends_at_zv_p
27646 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (next)))))
27648 *end = row;
27649 break;
27651 else
27653 /* If the next row's edges intersect [START_CHARPOS..END_CHARPOS],
27654 but none of the characters it displays are in the range, it is
27655 also END + 1. */
27656 struct glyph *g = next->glyphs[TEXT_AREA];
27657 struct glyph *s = g;
27658 struct glyph *e = g + next->used[TEXT_AREA];
27660 while (g < e)
27662 if (((BUFFERP (g->object) || INTEGERP (g->object))
27663 && ((start_charpos <= g->charpos && g->charpos < end_charpos)
27664 /* If the buffer position of the first glyph in
27665 the row is equal to END_CHARPOS, it means
27666 the last character to be highlighted is the
27667 newline of ROW, and we must consider NEXT as
27668 END, not END+1. */
27669 || (((!next->reversed_p && g == s)
27670 || (next->reversed_p && g == e - 1))
27671 && (g->charpos == end_charpos
27672 /* Special case for when NEXT is an
27673 empty line at ZV. */
27674 || (g->charpos == -1
27675 && !row->ends_at_zv_p
27676 && next_start == end_charpos)))))
27677 /* A glyph that comes from DISP_STRING is by
27678 definition to be highlighted. */
27679 || EQ (g->object, disp_string))
27680 break;
27681 g++;
27683 if (g == e)
27685 *end = row;
27686 break;
27688 /* The first row that ends at ZV must be the last to be
27689 highlighted. */
27690 else if (next->ends_at_zv_p)
27692 *end = next;
27693 break;
27699 /* This function sets the mouse_face_* elements of HLINFO, assuming
27700 the mouse cursor is on a glyph with buffer charpos MOUSE_CHARPOS in
27701 window WINDOW. START_CHARPOS and END_CHARPOS are buffer positions
27702 for the overlay or run of text properties specifying the mouse
27703 face. BEFORE_STRING and AFTER_STRING, if non-nil, are a
27704 before-string and after-string that must also be highlighted.
27705 DISP_STRING, if non-nil, is a display string that may cover some
27706 or all of the highlighted text. */
27708 static void
27709 mouse_face_from_buffer_pos (Lisp_Object window,
27710 Mouse_HLInfo *hlinfo,
27711 ptrdiff_t mouse_charpos,
27712 ptrdiff_t start_charpos,
27713 ptrdiff_t end_charpos,
27714 Lisp_Object before_string,
27715 Lisp_Object after_string,
27716 Lisp_Object disp_string)
27718 struct window *w = XWINDOW (window);
27719 struct glyph_row *first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
27720 struct glyph_row *r1, *r2;
27721 struct glyph *glyph, *end;
27722 ptrdiff_t ignore, pos;
27723 int x;
27725 eassert (NILP (disp_string) || STRINGP (disp_string));
27726 eassert (NILP (before_string) || STRINGP (before_string));
27727 eassert (NILP (after_string) || STRINGP (after_string));
27729 /* Find the rows corresponding to START_CHARPOS and END_CHARPOS. */
27730 rows_from_pos_range (w, start_charpos, end_charpos, disp_string, &r1, &r2);
27731 if (r1 == NULL)
27732 r1 = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
27733 /* If the before-string or display-string contains newlines,
27734 rows_from_pos_range skips to its last row. Move back. */
27735 if (!NILP (before_string) || !NILP (disp_string))
27737 struct glyph_row *prev;
27738 while ((prev = r1 - 1, prev >= first)
27739 && MATRIX_ROW_END_CHARPOS (prev) == start_charpos
27740 && prev->used[TEXT_AREA] > 0)
27742 struct glyph *beg = prev->glyphs[TEXT_AREA];
27743 glyph = beg + prev->used[TEXT_AREA];
27744 while (--glyph >= beg && INTEGERP (glyph->object));
27745 if (glyph < beg
27746 || !(EQ (glyph->object, before_string)
27747 || EQ (glyph->object, disp_string)))
27748 break;
27749 r1 = prev;
27752 if (r2 == NULL)
27754 r2 = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
27755 hlinfo->mouse_face_past_end = 1;
27757 else if (!NILP (after_string))
27759 /* If the after-string has newlines, advance to its last row. */
27760 struct glyph_row *next;
27761 struct glyph_row *last
27762 = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
27764 for (next = r2 + 1;
27765 next <= last
27766 && next->used[TEXT_AREA] > 0
27767 && EQ (next->glyphs[TEXT_AREA]->object, after_string);
27768 ++next)
27769 r2 = next;
27771 /* The rest of the display engine assumes that mouse_face_beg_row is
27772 either above mouse_face_end_row or identical to it. But with
27773 bidi-reordered continued lines, the row for START_CHARPOS could
27774 be below the row for END_CHARPOS. If so, swap the rows and store
27775 them in correct order. */
27776 if (r1->y > r2->y)
27778 struct glyph_row *tem = r2;
27780 r2 = r1;
27781 r1 = tem;
27784 hlinfo->mouse_face_beg_row = MATRIX_ROW_VPOS (r1, w->current_matrix);
27785 hlinfo->mouse_face_end_row = MATRIX_ROW_VPOS (r2, w->current_matrix);
27787 /* For a bidi-reordered row, the positions of BEFORE_STRING,
27788 AFTER_STRING, DISP_STRING, START_CHARPOS, and END_CHARPOS
27789 could be anywhere in the row and in any order. The strategy
27790 below is to find the leftmost and the rightmost glyph that
27791 belongs to either of these 3 strings, or whose position is
27792 between START_CHARPOS and END_CHARPOS, and highlight all the
27793 glyphs between those two. This may cover more than just the text
27794 between START_CHARPOS and END_CHARPOS if the range of characters
27795 strides the bidi level boundary, e.g. if the beginning is in R2L
27796 text while the end is in L2R text or vice versa. */
27797 if (!r1->reversed_p)
27799 /* This row is in a left to right paragraph. Scan it left to
27800 right. */
27801 glyph = r1->glyphs[TEXT_AREA];
27802 end = glyph + r1->used[TEXT_AREA];
27803 x = r1->x;
27805 /* Skip truncation glyphs at the start of the glyph row. */
27806 if (MATRIX_ROW_DISPLAYS_TEXT_P (r1))
27807 for (; glyph < end
27808 && INTEGERP (glyph->object)
27809 && glyph->charpos < 0;
27810 ++glyph)
27811 x += glyph->pixel_width;
27813 /* Scan the glyph row, looking for BEFORE_STRING, AFTER_STRING,
27814 or DISP_STRING, and the first glyph from buffer whose
27815 position is between START_CHARPOS and END_CHARPOS. */
27816 for (; glyph < end
27817 && !INTEGERP (glyph->object)
27818 && !EQ (glyph->object, disp_string)
27819 && !(BUFFERP (glyph->object)
27820 && (glyph->charpos >= start_charpos
27821 && glyph->charpos < end_charpos));
27822 ++glyph)
27824 /* BEFORE_STRING or AFTER_STRING are only relevant if they
27825 are present at buffer positions between START_CHARPOS and
27826 END_CHARPOS, or if they come from an overlay. */
27827 if (EQ (glyph->object, before_string))
27829 pos = string_buffer_position (before_string,
27830 start_charpos);
27831 /* If pos == 0, it means before_string came from an
27832 overlay, not from a buffer position. */
27833 if (!pos || (pos >= start_charpos && pos < end_charpos))
27834 break;
27836 else if (EQ (glyph->object, after_string))
27838 pos = string_buffer_position (after_string, end_charpos);
27839 if (!pos || (pos >= start_charpos && pos < end_charpos))
27840 break;
27842 x += glyph->pixel_width;
27844 hlinfo->mouse_face_beg_x = x;
27845 hlinfo->mouse_face_beg_col = glyph - r1->glyphs[TEXT_AREA];
27847 else
27849 /* This row is in a right to left paragraph. Scan it right to
27850 left. */
27851 struct glyph *g;
27853 end = r1->glyphs[TEXT_AREA] - 1;
27854 glyph = end + r1->used[TEXT_AREA];
27856 /* Skip truncation glyphs at the start of the glyph row. */
27857 if (MATRIX_ROW_DISPLAYS_TEXT_P (r1))
27858 for (; glyph > end
27859 && INTEGERP (glyph->object)
27860 && glyph->charpos < 0;
27861 --glyph)
27864 /* Scan the glyph row, looking for BEFORE_STRING, AFTER_STRING,
27865 or DISP_STRING, and the first glyph from buffer whose
27866 position is between START_CHARPOS and END_CHARPOS. */
27867 for (; glyph > end
27868 && !INTEGERP (glyph->object)
27869 && !EQ (glyph->object, disp_string)
27870 && !(BUFFERP (glyph->object)
27871 && (glyph->charpos >= start_charpos
27872 && glyph->charpos < end_charpos));
27873 --glyph)
27875 /* BEFORE_STRING or AFTER_STRING are only relevant if they
27876 are present at buffer positions between START_CHARPOS and
27877 END_CHARPOS, or if they come from an overlay. */
27878 if (EQ (glyph->object, before_string))
27880 pos = string_buffer_position (before_string, start_charpos);
27881 /* If pos == 0, it means before_string came from an
27882 overlay, not from a buffer position. */
27883 if (!pos || (pos >= start_charpos && pos < end_charpos))
27884 break;
27886 else if (EQ (glyph->object, after_string))
27888 pos = string_buffer_position (after_string, end_charpos);
27889 if (!pos || (pos >= start_charpos && pos < end_charpos))
27890 break;
27894 glyph++; /* first glyph to the right of the highlighted area */
27895 for (g = r1->glyphs[TEXT_AREA], x = r1->x; g < glyph; g++)
27896 x += g->pixel_width;
27897 hlinfo->mouse_face_beg_x = x;
27898 hlinfo->mouse_face_beg_col = glyph - r1->glyphs[TEXT_AREA];
27901 /* If the highlight ends in a different row, compute GLYPH and END
27902 for the end row. Otherwise, reuse the values computed above for
27903 the row where the highlight begins. */
27904 if (r2 != r1)
27906 if (!r2->reversed_p)
27908 glyph = r2->glyphs[TEXT_AREA];
27909 end = glyph + r2->used[TEXT_AREA];
27910 x = r2->x;
27912 else
27914 end = r2->glyphs[TEXT_AREA] - 1;
27915 glyph = end + r2->used[TEXT_AREA];
27919 if (!r2->reversed_p)
27921 /* Skip truncation and continuation glyphs near the end of the
27922 row, and also blanks and stretch glyphs inserted by
27923 extend_face_to_end_of_line. */
27924 while (end > glyph
27925 && INTEGERP ((end - 1)->object))
27926 --end;
27927 /* Scan the rest of the glyph row from the end, looking for the
27928 first glyph that comes from BEFORE_STRING, AFTER_STRING, or
27929 DISP_STRING, or whose position is between START_CHARPOS
27930 and END_CHARPOS */
27931 for (--end;
27932 end > glyph
27933 && !INTEGERP (end->object)
27934 && !EQ (end->object, disp_string)
27935 && !(BUFFERP (end->object)
27936 && (end->charpos >= start_charpos
27937 && end->charpos < end_charpos));
27938 --end)
27940 /* BEFORE_STRING or AFTER_STRING are only relevant if they
27941 are present at buffer positions between START_CHARPOS and
27942 END_CHARPOS, or if they come from an overlay. */
27943 if (EQ (end->object, before_string))
27945 pos = string_buffer_position (before_string, start_charpos);
27946 if (!pos || (pos >= start_charpos && pos < end_charpos))
27947 break;
27949 else if (EQ (end->object, after_string))
27951 pos = string_buffer_position (after_string, end_charpos);
27952 if (!pos || (pos >= start_charpos && pos < end_charpos))
27953 break;
27956 /* Find the X coordinate of the last glyph to be highlighted. */
27957 for (; glyph <= end; ++glyph)
27958 x += glyph->pixel_width;
27960 hlinfo->mouse_face_end_x = x;
27961 hlinfo->mouse_face_end_col = glyph - r2->glyphs[TEXT_AREA];
27963 else
27965 /* Skip truncation and continuation glyphs near the end of the
27966 row, and also blanks and stretch glyphs inserted by
27967 extend_face_to_end_of_line. */
27968 x = r2->x;
27969 end++;
27970 while (end < glyph
27971 && INTEGERP (end->object))
27973 x += end->pixel_width;
27974 ++end;
27976 /* Scan the rest of the glyph row from the end, looking for the
27977 first glyph that comes from BEFORE_STRING, AFTER_STRING, or
27978 DISP_STRING, or whose position is between START_CHARPOS
27979 and END_CHARPOS */
27980 for ( ;
27981 end < glyph
27982 && !INTEGERP (end->object)
27983 && !EQ (end->object, disp_string)
27984 && !(BUFFERP (end->object)
27985 && (end->charpos >= start_charpos
27986 && end->charpos < end_charpos));
27987 ++end)
27989 /* BEFORE_STRING or AFTER_STRING are only relevant if they
27990 are present at buffer positions between START_CHARPOS and
27991 END_CHARPOS, or if they come from an overlay. */
27992 if (EQ (end->object, before_string))
27994 pos = string_buffer_position (before_string, start_charpos);
27995 if (!pos || (pos >= start_charpos && pos < end_charpos))
27996 break;
27998 else if (EQ (end->object, after_string))
28000 pos = string_buffer_position (after_string, end_charpos);
28001 if (!pos || (pos >= start_charpos && pos < end_charpos))
28002 break;
28004 x += end->pixel_width;
28006 /* If we exited the above loop because we arrived at the last
28007 glyph of the row, and its buffer position is still not in
28008 range, it means the last character in range is the preceding
28009 newline. Bump the end column and x values to get past the
28010 last glyph. */
28011 if (end == glyph
28012 && BUFFERP (end->object)
28013 && (end->charpos < start_charpos
28014 || end->charpos >= end_charpos))
28016 x += end->pixel_width;
28017 ++end;
28019 hlinfo->mouse_face_end_x = x;
28020 hlinfo->mouse_face_end_col = end - r2->glyphs[TEXT_AREA];
28023 hlinfo->mouse_face_window = window;
28024 hlinfo->mouse_face_face_id
28025 = face_at_buffer_position (w, mouse_charpos, &ignore,
28026 mouse_charpos + 1,
28027 !hlinfo->mouse_face_hidden, -1);
28028 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
28031 /* The following function is not used anymore (replaced with
28032 mouse_face_from_string_pos), but I leave it here for the time
28033 being, in case someone would. */
28035 #if 0 /* not used */
28037 /* Find the position of the glyph for position POS in OBJECT in
28038 window W's current matrix, and return in *X, *Y the pixel
28039 coordinates, and return in *HPOS, *VPOS the column/row of the glyph.
28041 RIGHT_P non-zero means return the position of the right edge of the
28042 glyph, RIGHT_P zero means return the left edge position.
28044 If no glyph for POS exists in the matrix, return the position of
28045 the glyph with the next smaller position that is in the matrix, if
28046 RIGHT_P is zero. If RIGHT_P is non-zero, and no glyph for POS
28047 exists in the matrix, return the position of the glyph with the
28048 next larger position in OBJECT.
28050 Value is non-zero if a glyph was found. */
28052 static int
28053 fast_find_string_pos (struct window *w, ptrdiff_t pos, Lisp_Object object,
28054 int *hpos, int *vpos, int *x, int *y, int right_p)
28056 int yb = window_text_bottom_y (w);
28057 struct glyph_row *r;
28058 struct glyph *best_glyph = NULL;
28059 struct glyph_row *best_row = NULL;
28060 int best_x = 0;
28062 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
28063 r->enabled_p && r->y < yb;
28064 ++r)
28066 struct glyph *g = r->glyphs[TEXT_AREA];
28067 struct glyph *e = g + r->used[TEXT_AREA];
28068 int gx;
28070 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
28071 if (EQ (g->object, object))
28073 if (g->charpos == pos)
28075 best_glyph = g;
28076 best_x = gx;
28077 best_row = r;
28078 goto found;
28080 else if (best_glyph == NULL
28081 || ((eabs (g->charpos - pos)
28082 < eabs (best_glyph->charpos - pos))
28083 && (right_p
28084 ? g->charpos < pos
28085 : g->charpos > pos)))
28087 best_glyph = g;
28088 best_x = gx;
28089 best_row = r;
28094 found:
28096 if (best_glyph)
28098 *x = best_x;
28099 *hpos = best_glyph - best_row->glyphs[TEXT_AREA];
28101 if (right_p)
28103 *x += best_glyph->pixel_width;
28104 ++*hpos;
28107 *y = best_row->y;
28108 *vpos = MATRIX_ROW_VPOS (best_row, w->current_matrix);
28111 return best_glyph != NULL;
28113 #endif /* not used */
28115 /* Find the positions of the first and the last glyphs in window W's
28116 current matrix that occlude positions [STARTPOS..ENDPOS) in OBJECT
28117 (assumed to be a string), and return in HLINFO's mouse_face_*
28118 members the pixel and column/row coordinates of those glyphs. */
28120 static void
28121 mouse_face_from_string_pos (struct window *w, Mouse_HLInfo *hlinfo,
28122 Lisp_Object object,
28123 ptrdiff_t startpos, ptrdiff_t endpos)
28125 int yb = window_text_bottom_y (w);
28126 struct glyph_row *r;
28127 struct glyph *g, *e;
28128 int gx;
28129 int found = 0;
28131 /* Find the glyph row with at least one position in the range
28132 [STARTPOS..ENDPOS), and the first glyph in that row whose
28133 position belongs to that range. */
28134 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
28135 r->enabled_p && r->y < yb;
28136 ++r)
28138 if (!r->reversed_p)
28140 g = r->glyphs[TEXT_AREA];
28141 e = g + r->used[TEXT_AREA];
28142 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
28143 if (EQ (g->object, object)
28144 && startpos <= g->charpos && g->charpos < endpos)
28146 hlinfo->mouse_face_beg_row
28147 = MATRIX_ROW_VPOS (r, w->current_matrix);
28148 hlinfo->mouse_face_beg_col = g - r->glyphs[TEXT_AREA];
28149 hlinfo->mouse_face_beg_x = gx;
28150 found = 1;
28151 break;
28154 else
28156 struct glyph *g1;
28158 e = r->glyphs[TEXT_AREA];
28159 g = e + r->used[TEXT_AREA];
28160 for ( ; g > e; --g)
28161 if (EQ ((g-1)->object, object)
28162 && startpos <= (g-1)->charpos && (g-1)->charpos < endpos)
28164 hlinfo->mouse_face_beg_row
28165 = MATRIX_ROW_VPOS (r, w->current_matrix);
28166 hlinfo->mouse_face_beg_col = g - r->glyphs[TEXT_AREA];
28167 for (gx = r->x, g1 = r->glyphs[TEXT_AREA]; g1 < g; ++g1)
28168 gx += g1->pixel_width;
28169 hlinfo->mouse_face_beg_x = gx;
28170 found = 1;
28171 break;
28174 if (found)
28175 break;
28178 if (!found)
28179 return;
28181 /* Starting with the next row, look for the first row which does NOT
28182 include any glyphs whose positions are in the range. */
28183 for (++r; r->enabled_p && r->y < yb; ++r)
28185 g = r->glyphs[TEXT_AREA];
28186 e = g + r->used[TEXT_AREA];
28187 found = 0;
28188 for ( ; g < e; ++g)
28189 if (EQ (g->object, object)
28190 && startpos <= g->charpos && g->charpos < endpos)
28192 found = 1;
28193 break;
28195 if (!found)
28196 break;
28199 /* The highlighted region ends on the previous row. */
28200 r--;
28202 /* Set the end row. */
28203 hlinfo->mouse_face_end_row = MATRIX_ROW_VPOS (r, w->current_matrix);
28205 /* Compute and set the end column and the end column's horizontal
28206 pixel coordinate. */
28207 if (!r->reversed_p)
28209 g = r->glyphs[TEXT_AREA];
28210 e = g + r->used[TEXT_AREA];
28211 for ( ; e > g; --e)
28212 if (EQ ((e-1)->object, object)
28213 && startpos <= (e-1)->charpos && (e-1)->charpos < endpos)
28214 break;
28215 hlinfo->mouse_face_end_col = e - g;
28217 for (gx = r->x; g < e; ++g)
28218 gx += g->pixel_width;
28219 hlinfo->mouse_face_end_x = gx;
28221 else
28223 e = r->glyphs[TEXT_AREA];
28224 g = e + r->used[TEXT_AREA];
28225 for (gx = r->x ; e < g; ++e)
28227 if (EQ (e->object, object)
28228 && startpos <= e->charpos && e->charpos < endpos)
28229 break;
28230 gx += e->pixel_width;
28232 hlinfo->mouse_face_end_col = e - r->glyphs[TEXT_AREA];
28233 hlinfo->mouse_face_end_x = gx;
28237 #ifdef HAVE_WINDOW_SYSTEM
28239 /* See if position X, Y is within a hot-spot of an image. */
28241 static int
28242 on_hot_spot_p (Lisp_Object hot_spot, int x, int y)
28244 if (!CONSP (hot_spot))
28245 return 0;
28247 if (EQ (XCAR (hot_spot), Qrect))
28249 /* CDR is (Top-Left . Bottom-Right) = ((x0 . y0) . (x1 . y1)) */
28250 Lisp_Object rect = XCDR (hot_spot);
28251 Lisp_Object tem;
28252 if (!CONSP (rect))
28253 return 0;
28254 if (!CONSP (XCAR (rect)))
28255 return 0;
28256 if (!CONSP (XCDR (rect)))
28257 return 0;
28258 if (!(tem = XCAR (XCAR (rect)), INTEGERP (tem) && x >= XINT (tem)))
28259 return 0;
28260 if (!(tem = XCDR (XCAR (rect)), INTEGERP (tem) && y >= XINT (tem)))
28261 return 0;
28262 if (!(tem = XCAR (XCDR (rect)), INTEGERP (tem) && x <= XINT (tem)))
28263 return 0;
28264 if (!(tem = XCDR (XCDR (rect)), INTEGERP (tem) && y <= XINT (tem)))
28265 return 0;
28266 return 1;
28268 else if (EQ (XCAR (hot_spot), Qcircle))
28270 /* CDR is (Center . Radius) = ((x0 . y0) . r) */
28271 Lisp_Object circ = XCDR (hot_spot);
28272 Lisp_Object lr, lx0, ly0;
28273 if (CONSP (circ)
28274 && CONSP (XCAR (circ))
28275 && (lr = XCDR (circ), INTEGERP (lr) || FLOATP (lr))
28276 && (lx0 = XCAR (XCAR (circ)), INTEGERP (lx0))
28277 && (ly0 = XCDR (XCAR (circ)), INTEGERP (ly0)))
28279 double r = XFLOATINT (lr);
28280 double dx = XINT (lx0) - x;
28281 double dy = XINT (ly0) - y;
28282 return (dx * dx + dy * dy <= r * r);
28285 else if (EQ (XCAR (hot_spot), Qpoly))
28287 /* CDR is [x0 y0 x1 y1 x2 y2 ...x(n-1) y(n-1)] */
28288 if (VECTORP (XCDR (hot_spot)))
28290 struct Lisp_Vector *v = XVECTOR (XCDR (hot_spot));
28291 Lisp_Object *poly = v->contents;
28292 ptrdiff_t n = v->header.size;
28293 ptrdiff_t i;
28294 int inside = 0;
28295 Lisp_Object lx, ly;
28296 int x0, y0;
28298 /* Need an even number of coordinates, and at least 3 edges. */
28299 if (n < 6 || n & 1)
28300 return 0;
28302 /* Count edge segments intersecting line from (X,Y) to (X,infinity).
28303 If count is odd, we are inside polygon. Pixels on edges
28304 may or may not be included depending on actual geometry of the
28305 polygon. */
28306 if ((lx = poly[n-2], !INTEGERP (lx))
28307 || (ly = poly[n-1], !INTEGERP (lx)))
28308 return 0;
28309 x0 = XINT (lx), y0 = XINT (ly);
28310 for (i = 0; i < n; i += 2)
28312 int x1 = x0, y1 = y0;
28313 if ((lx = poly[i], !INTEGERP (lx))
28314 || (ly = poly[i+1], !INTEGERP (ly)))
28315 return 0;
28316 x0 = XINT (lx), y0 = XINT (ly);
28318 /* Does this segment cross the X line? */
28319 if (x0 >= x)
28321 if (x1 >= x)
28322 continue;
28324 else if (x1 < x)
28325 continue;
28326 if (y > y0 && y > y1)
28327 continue;
28328 if (y < y0 + ((y1 - y0) * (x - x0)) / (x1 - x0))
28329 inside = !inside;
28331 return inside;
28334 return 0;
28337 Lisp_Object
28338 find_hot_spot (Lisp_Object map, int x, int y)
28340 while (CONSP (map))
28342 if (CONSP (XCAR (map))
28343 && on_hot_spot_p (XCAR (XCAR (map)), x, y))
28344 return XCAR (map);
28345 map = XCDR (map);
28348 return Qnil;
28351 DEFUN ("lookup-image-map", Flookup_image_map, Slookup_image_map,
28352 3, 3, 0,
28353 doc: /* Lookup in image map MAP coordinates X and Y.
28354 An image map is an alist where each element has the format (AREA ID PLIST).
28355 An AREA is specified as either a rectangle, a circle, or a polygon:
28356 A rectangle is a cons (rect . ((x0 . y0) . (x1 . y1))) specifying the
28357 pixel coordinates of the upper left and bottom right corners.
28358 A circle is a cons (circle . ((x0 . y0) . r)) specifying the center
28359 and the radius of the circle; r may be a float or integer.
28360 A polygon is a cons (poly . [x0 y0 x1 y1 ...]) where each pair in the
28361 vector describes one corner in the polygon.
28362 Returns the alist element for the first matching AREA in MAP. */)
28363 (Lisp_Object map, Lisp_Object x, Lisp_Object y)
28365 if (NILP (map))
28366 return Qnil;
28368 CHECK_NUMBER (x);
28369 CHECK_NUMBER (y);
28371 return find_hot_spot (map,
28372 clip_to_bounds (INT_MIN, XINT (x), INT_MAX),
28373 clip_to_bounds (INT_MIN, XINT (y), INT_MAX));
28377 /* Display frame CURSOR, optionally using shape defined by POINTER. */
28378 static void
28379 define_frame_cursor1 (struct frame *f, Cursor cursor, Lisp_Object pointer)
28381 /* Do not change cursor shape while dragging mouse. */
28382 if (!NILP (do_mouse_tracking))
28383 return;
28385 if (!NILP (pointer))
28387 if (EQ (pointer, Qarrow))
28388 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
28389 else if (EQ (pointer, Qhand))
28390 cursor = FRAME_X_OUTPUT (f)->hand_cursor;
28391 else if (EQ (pointer, Qtext))
28392 cursor = FRAME_X_OUTPUT (f)->text_cursor;
28393 else if (EQ (pointer, intern ("hdrag")))
28394 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
28395 else if (EQ (pointer, intern ("nhdrag")))
28396 cursor = FRAME_X_OUTPUT (f)->vertical_drag_cursor;
28397 #ifdef HAVE_X_WINDOWS
28398 else if (EQ (pointer, intern ("vdrag")))
28399 cursor = FRAME_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
28400 #endif
28401 else if (EQ (pointer, intern ("hourglass")))
28402 cursor = FRAME_X_OUTPUT (f)->hourglass_cursor;
28403 else if (EQ (pointer, Qmodeline))
28404 cursor = FRAME_X_OUTPUT (f)->modeline_cursor;
28405 else
28406 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
28409 if (cursor != No_Cursor)
28410 FRAME_RIF (f)->define_frame_cursor (f, cursor);
28413 #endif /* HAVE_WINDOW_SYSTEM */
28415 /* Take proper action when mouse has moved to the mode or header line
28416 or marginal area AREA of window W, x-position X and y-position Y.
28417 X is relative to the start of the text display area of W, so the
28418 width of bitmap areas and scroll bars must be subtracted to get a
28419 position relative to the start of the mode line. */
28421 static void
28422 note_mode_line_or_margin_highlight (Lisp_Object window, int x, int y,
28423 enum window_part area)
28425 struct window *w = XWINDOW (window);
28426 struct frame *f = XFRAME (w->frame);
28427 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
28428 #ifdef HAVE_WINDOW_SYSTEM
28429 Display_Info *dpyinfo;
28430 #endif
28431 Cursor cursor = No_Cursor;
28432 Lisp_Object pointer = Qnil;
28433 int dx, dy, width, height;
28434 ptrdiff_t charpos;
28435 Lisp_Object string, object = Qnil;
28436 Lisp_Object pos IF_LINT (= Qnil), help;
28438 Lisp_Object mouse_face;
28439 int original_x_pixel = x;
28440 struct glyph * glyph = NULL, * row_start_glyph = NULL;
28441 struct glyph_row *row IF_LINT (= 0);
28443 if (area == ON_MODE_LINE || area == ON_HEADER_LINE)
28445 int x0;
28446 struct glyph *end;
28448 /* Kludge alert: mode_line_string takes X/Y in pixels, but
28449 returns them in row/column units! */
28450 string = mode_line_string (w, area, &x, &y, &charpos,
28451 &object, &dx, &dy, &width, &height);
28453 row = (area == ON_MODE_LINE
28454 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
28455 : MATRIX_HEADER_LINE_ROW (w->current_matrix));
28457 /* Find the glyph under the mouse pointer. */
28458 if (row->mode_line_p && row->enabled_p)
28460 glyph = row_start_glyph = row->glyphs[TEXT_AREA];
28461 end = glyph + row->used[TEXT_AREA];
28463 for (x0 = original_x_pixel;
28464 glyph < end && x0 >= glyph->pixel_width;
28465 ++glyph)
28466 x0 -= glyph->pixel_width;
28468 if (glyph >= end)
28469 glyph = NULL;
28472 else
28474 x -= WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
28475 /* Kludge alert: marginal_area_string takes X/Y in pixels, but
28476 returns them in row/column units! */
28477 string = marginal_area_string (w, area, &x, &y, &charpos,
28478 &object, &dx, &dy, &width, &height);
28481 help = Qnil;
28483 #ifdef HAVE_WINDOW_SYSTEM
28484 if (IMAGEP (object))
28486 Lisp_Object image_map, hotspot;
28487 if ((image_map = Fplist_get (XCDR (object), QCmap),
28488 !NILP (image_map))
28489 && (hotspot = find_hot_spot (image_map, dx, dy),
28490 CONSP (hotspot))
28491 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
28493 Lisp_Object plist;
28495 /* Could check XCAR (hotspot) to see if we enter/leave this hot-spot.
28496 If so, we could look for mouse-enter, mouse-leave
28497 properties in PLIST (and do something...). */
28498 hotspot = XCDR (hotspot);
28499 if (CONSP (hotspot)
28500 && (plist = XCAR (hotspot), CONSP (plist)))
28502 pointer = Fplist_get (plist, Qpointer);
28503 if (NILP (pointer))
28504 pointer = Qhand;
28505 help = Fplist_get (plist, Qhelp_echo);
28506 if (!NILP (help))
28508 help_echo_string = help;
28509 XSETWINDOW (help_echo_window, w);
28510 help_echo_object = w->contents;
28511 help_echo_pos = charpos;
28515 if (NILP (pointer))
28516 pointer = Fplist_get (XCDR (object), QCpointer);
28518 #endif /* HAVE_WINDOW_SYSTEM */
28520 if (STRINGP (string))
28521 pos = make_number (charpos);
28523 /* Set the help text and mouse pointer. If the mouse is on a part
28524 of the mode line without any text (e.g. past the right edge of
28525 the mode line text), use the default help text and pointer. */
28526 if (STRINGP (string) || area == ON_MODE_LINE)
28528 /* Arrange to display the help by setting the global variables
28529 help_echo_string, help_echo_object, and help_echo_pos. */
28530 if (NILP (help))
28532 if (STRINGP (string))
28533 help = Fget_text_property (pos, Qhelp_echo, string);
28535 if (!NILP (help))
28537 help_echo_string = help;
28538 XSETWINDOW (help_echo_window, w);
28539 help_echo_object = string;
28540 help_echo_pos = charpos;
28542 else if (area == ON_MODE_LINE)
28544 Lisp_Object default_help
28545 = buffer_local_value_1 (Qmode_line_default_help_echo,
28546 w->contents);
28548 if (STRINGP (default_help))
28550 help_echo_string = default_help;
28551 XSETWINDOW (help_echo_window, w);
28552 help_echo_object = Qnil;
28553 help_echo_pos = -1;
28558 #ifdef HAVE_WINDOW_SYSTEM
28559 /* Change the mouse pointer according to what is under it. */
28560 if (FRAME_WINDOW_P (f))
28562 bool draggable = (! WINDOW_BOTTOMMOST_P (w)
28563 || minibuf_level
28564 || NILP (Vresize_mini_windows));
28566 dpyinfo = FRAME_DISPLAY_INFO (f);
28567 if (STRINGP (string))
28569 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
28571 if (NILP (pointer))
28572 pointer = Fget_text_property (pos, Qpointer, string);
28574 /* Change the mouse pointer according to what is under X/Y. */
28575 if (NILP (pointer)
28576 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE)))
28578 Lisp_Object map;
28579 map = Fget_text_property (pos, Qlocal_map, string);
28580 if (!KEYMAPP (map))
28581 map = Fget_text_property (pos, Qkeymap, string);
28582 if (!KEYMAPP (map) && draggable)
28583 cursor = dpyinfo->vertical_scroll_bar_cursor;
28586 else if (draggable)
28587 /* Default mode-line pointer. */
28588 cursor = FRAME_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
28590 #endif
28593 /* Change the mouse face according to what is under X/Y. */
28594 if (STRINGP (string))
28596 mouse_face = Fget_text_property (pos, Qmouse_face, string);
28597 if (!NILP (Vmouse_highlight) && !NILP (mouse_face)
28598 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
28599 && glyph)
28601 Lisp_Object b, e;
28603 struct glyph * tmp_glyph;
28605 int gpos;
28606 int gseq_length;
28607 int total_pixel_width;
28608 ptrdiff_t begpos, endpos, ignore;
28610 int vpos, hpos;
28612 b = Fprevious_single_property_change (make_number (charpos + 1),
28613 Qmouse_face, string, Qnil);
28614 if (NILP (b))
28615 begpos = 0;
28616 else
28617 begpos = XINT (b);
28619 e = Fnext_single_property_change (pos, Qmouse_face, string, Qnil);
28620 if (NILP (e))
28621 endpos = SCHARS (string);
28622 else
28623 endpos = XINT (e);
28625 /* Calculate the glyph position GPOS of GLYPH in the
28626 displayed string, relative to the beginning of the
28627 highlighted part of the string.
28629 Note: GPOS is different from CHARPOS. CHARPOS is the
28630 position of GLYPH in the internal string object. A mode
28631 line string format has structures which are converted to
28632 a flattened string by the Emacs Lisp interpreter. The
28633 internal string is an element of those structures. The
28634 displayed string is the flattened string. */
28635 tmp_glyph = row_start_glyph;
28636 while (tmp_glyph < glyph
28637 && (!(EQ (tmp_glyph->object, glyph->object)
28638 && begpos <= tmp_glyph->charpos
28639 && tmp_glyph->charpos < endpos)))
28640 tmp_glyph++;
28641 gpos = glyph - tmp_glyph;
28643 /* Calculate the length GSEQ_LENGTH of the glyph sequence of
28644 the highlighted part of the displayed string to which
28645 GLYPH belongs. Note: GSEQ_LENGTH is different from
28646 SCHARS (STRING), because the latter returns the length of
28647 the internal string. */
28648 for (tmp_glyph = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
28649 tmp_glyph > glyph
28650 && (!(EQ (tmp_glyph->object, glyph->object)
28651 && begpos <= tmp_glyph->charpos
28652 && tmp_glyph->charpos < endpos));
28653 tmp_glyph--)
28655 gseq_length = gpos + (tmp_glyph - glyph) + 1;
28657 /* Calculate the total pixel width of all the glyphs between
28658 the beginning of the highlighted area and GLYPH. */
28659 total_pixel_width = 0;
28660 for (tmp_glyph = glyph - gpos; tmp_glyph != glyph; tmp_glyph++)
28661 total_pixel_width += tmp_glyph->pixel_width;
28663 /* Pre calculation of re-rendering position. Note: X is in
28664 column units here, after the call to mode_line_string or
28665 marginal_area_string. */
28666 hpos = x - gpos;
28667 vpos = (area == ON_MODE_LINE
28668 ? (w->current_matrix)->nrows - 1
28669 : 0);
28671 /* If GLYPH's position is included in the region that is
28672 already drawn in mouse face, we have nothing to do. */
28673 if ( EQ (window, hlinfo->mouse_face_window)
28674 && (!row->reversed_p
28675 ? (hlinfo->mouse_face_beg_col <= hpos
28676 && hpos < hlinfo->mouse_face_end_col)
28677 /* In R2L rows we swap BEG and END, see below. */
28678 : (hlinfo->mouse_face_end_col <= hpos
28679 && hpos < hlinfo->mouse_face_beg_col))
28680 && hlinfo->mouse_face_beg_row == vpos )
28681 return;
28683 if (clear_mouse_face (hlinfo))
28684 cursor = No_Cursor;
28686 if (!row->reversed_p)
28688 hlinfo->mouse_face_beg_col = hpos;
28689 hlinfo->mouse_face_beg_x = original_x_pixel
28690 - (total_pixel_width + dx);
28691 hlinfo->mouse_face_end_col = hpos + gseq_length;
28692 hlinfo->mouse_face_end_x = 0;
28694 else
28696 /* In R2L rows, show_mouse_face expects BEG and END
28697 coordinates to be swapped. */
28698 hlinfo->mouse_face_end_col = hpos;
28699 hlinfo->mouse_face_end_x = original_x_pixel
28700 - (total_pixel_width + dx);
28701 hlinfo->mouse_face_beg_col = hpos + gseq_length;
28702 hlinfo->mouse_face_beg_x = 0;
28705 hlinfo->mouse_face_beg_row = vpos;
28706 hlinfo->mouse_face_end_row = hlinfo->mouse_face_beg_row;
28707 hlinfo->mouse_face_past_end = 0;
28708 hlinfo->mouse_face_window = window;
28710 hlinfo->mouse_face_face_id = face_at_string_position (w, string,
28711 charpos,
28712 0, &ignore,
28713 glyph->face_id,
28715 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
28717 if (NILP (pointer))
28718 pointer = Qhand;
28720 else if ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
28721 clear_mouse_face (hlinfo);
28723 #ifdef HAVE_WINDOW_SYSTEM
28724 if (FRAME_WINDOW_P (f))
28725 define_frame_cursor1 (f, cursor, pointer);
28726 #endif
28730 /* EXPORT:
28731 Take proper action when the mouse has moved to position X, Y on
28732 frame F with regards to highlighting portions of display that have
28733 mouse-face properties. Also de-highlight portions of display where
28734 the mouse was before, set the mouse pointer shape as appropriate
28735 for the mouse coordinates, and activate help echo (tooltips).
28736 X and Y can be negative or out of range. */
28738 void
28739 note_mouse_highlight (struct frame *f, int x, int y)
28741 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
28742 enum window_part part = ON_NOTHING;
28743 Lisp_Object window;
28744 struct window *w;
28745 Cursor cursor = No_Cursor;
28746 Lisp_Object pointer = Qnil; /* Takes precedence over cursor! */
28747 struct buffer *b;
28749 /* When a menu is active, don't highlight because this looks odd. */
28750 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS) || defined (MSDOS)
28751 if (popup_activated ())
28752 return;
28753 #endif
28755 if (!f->glyphs_initialized_p
28756 || f->pointer_invisible)
28757 return;
28759 hlinfo->mouse_face_mouse_x = x;
28760 hlinfo->mouse_face_mouse_y = y;
28761 hlinfo->mouse_face_mouse_frame = f;
28763 if (hlinfo->mouse_face_defer)
28764 return;
28766 /* Which window is that in? */
28767 window = window_from_coordinates (f, x, y, &part, 1);
28769 /* If displaying active text in another window, clear that. */
28770 if (! EQ (window, hlinfo->mouse_face_window)
28771 /* Also clear if we move out of text area in same window. */
28772 || (!NILP (hlinfo->mouse_face_window)
28773 && !NILP (window)
28774 && part != ON_TEXT
28775 && part != ON_MODE_LINE
28776 && part != ON_HEADER_LINE))
28777 clear_mouse_face (hlinfo);
28779 /* Not on a window -> return. */
28780 if (!WINDOWP (window))
28781 return;
28783 /* Reset help_echo_string. It will get recomputed below. */
28784 help_echo_string = Qnil;
28786 /* Convert to window-relative pixel coordinates. */
28787 w = XWINDOW (window);
28788 frame_to_window_pixel_xy (w, &x, &y);
28790 #if defined (HAVE_WINDOW_SYSTEM) && ! defined (USE_GTK) && ! defined (HAVE_NS)
28791 /* Handle tool-bar window differently since it doesn't display a
28792 buffer. */
28793 if (EQ (window, f->tool_bar_window))
28795 note_tool_bar_highlight (f, x, y);
28796 return;
28798 #endif
28800 /* Mouse is on the mode, header line or margin? */
28801 if (part == ON_MODE_LINE || part == ON_HEADER_LINE
28802 || part == ON_LEFT_MARGIN || part == ON_RIGHT_MARGIN)
28804 note_mode_line_or_margin_highlight (window, x, y, part);
28806 #ifdef HAVE_WINDOW_SYSTEM
28807 if (part == ON_LEFT_MARGIN || part == ON_RIGHT_MARGIN)
28809 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
28810 /* Show non-text cursor (Bug#16647). */
28811 goto set_cursor;
28813 else
28814 #endif
28815 return;
28818 #ifdef HAVE_WINDOW_SYSTEM
28819 if (part == ON_VERTICAL_BORDER)
28821 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
28822 help_echo_string = build_string ("drag-mouse-1: resize");
28824 else if (part == ON_RIGHT_DIVIDER)
28826 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
28827 help_echo_string = build_string ("drag-mouse-1: resize");
28829 else if (part == ON_BOTTOM_DIVIDER)
28830 if (! WINDOW_BOTTOMMOST_P (w)
28831 || minibuf_level
28832 || NILP (Vresize_mini_windows))
28834 cursor = FRAME_X_OUTPUT (f)->vertical_drag_cursor;
28835 help_echo_string = build_string ("drag-mouse-1: resize");
28837 else
28838 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
28839 else if (part == ON_LEFT_FRINGE || part == ON_RIGHT_FRINGE
28840 || part == ON_SCROLL_BAR)
28841 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
28842 else
28843 cursor = FRAME_X_OUTPUT (f)->text_cursor;
28844 #endif
28846 /* Are we in a window whose display is up to date?
28847 And verify the buffer's text has not changed. */
28848 b = XBUFFER (w->contents);
28849 if (part == ON_TEXT && w->window_end_valid && !window_outdated (w))
28851 int hpos, vpos, dx, dy, area = LAST_AREA;
28852 ptrdiff_t pos;
28853 struct glyph *glyph;
28854 Lisp_Object object;
28855 Lisp_Object mouse_face = Qnil, position;
28856 Lisp_Object *overlay_vec = NULL;
28857 ptrdiff_t i, noverlays;
28858 struct buffer *obuf;
28859 ptrdiff_t obegv, ozv;
28860 int same_region;
28862 /* Find the glyph under X/Y. */
28863 glyph = x_y_to_hpos_vpos (w, x, y, &hpos, &vpos, &dx, &dy, &area);
28865 #ifdef HAVE_WINDOW_SYSTEM
28866 /* Look for :pointer property on image. */
28867 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
28869 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
28870 if (img != NULL && IMAGEP (img->spec))
28872 Lisp_Object image_map, hotspot;
28873 if ((image_map = Fplist_get (XCDR (img->spec), QCmap),
28874 !NILP (image_map))
28875 && (hotspot = find_hot_spot (image_map,
28876 glyph->slice.img.x + dx,
28877 glyph->slice.img.y + dy),
28878 CONSP (hotspot))
28879 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
28881 Lisp_Object plist;
28883 /* Could check XCAR (hotspot) to see if we enter/leave
28884 this hot-spot.
28885 If so, we could look for mouse-enter, mouse-leave
28886 properties in PLIST (and do something...). */
28887 hotspot = XCDR (hotspot);
28888 if (CONSP (hotspot)
28889 && (plist = XCAR (hotspot), CONSP (plist)))
28891 pointer = Fplist_get (plist, Qpointer);
28892 if (NILP (pointer))
28893 pointer = Qhand;
28894 help_echo_string = Fplist_get (plist, Qhelp_echo);
28895 if (!NILP (help_echo_string))
28897 help_echo_window = window;
28898 help_echo_object = glyph->object;
28899 help_echo_pos = glyph->charpos;
28903 if (NILP (pointer))
28904 pointer = Fplist_get (XCDR (img->spec), QCpointer);
28907 #endif /* HAVE_WINDOW_SYSTEM */
28909 /* Clear mouse face if X/Y not over text. */
28910 if (glyph == NULL
28911 || area != TEXT_AREA
28912 || !MATRIX_ROW_DISPLAYS_TEXT_P (MATRIX_ROW (w->current_matrix, vpos))
28913 /* Glyph's OBJECT is an integer for glyphs inserted by the
28914 display engine for its internal purposes, like truncation
28915 and continuation glyphs and blanks beyond the end of
28916 line's text on text terminals. If we are over such a
28917 glyph, we are not over any text. */
28918 || INTEGERP (glyph->object)
28919 /* R2L rows have a stretch glyph at their front, which
28920 stands for no text, whereas L2R rows have no glyphs at
28921 all beyond the end of text. Treat such stretch glyphs
28922 like we do with NULL glyphs in L2R rows. */
28923 || (MATRIX_ROW (w->current_matrix, vpos)->reversed_p
28924 && glyph == MATRIX_ROW_GLYPH_START (w->current_matrix, vpos)
28925 && glyph->type == STRETCH_GLYPH
28926 && glyph->avoid_cursor_p))
28928 if (clear_mouse_face (hlinfo))
28929 cursor = No_Cursor;
28930 #ifdef HAVE_WINDOW_SYSTEM
28931 if (FRAME_WINDOW_P (f) && NILP (pointer))
28933 if (area != TEXT_AREA)
28934 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
28935 else
28936 pointer = Vvoid_text_area_pointer;
28938 #endif
28939 goto set_cursor;
28942 pos = glyph->charpos;
28943 object = glyph->object;
28944 if (!STRINGP (object) && !BUFFERP (object))
28945 goto set_cursor;
28947 /* If we get an out-of-range value, return now; avoid an error. */
28948 if (BUFFERP (object) && pos > BUF_Z (b))
28949 goto set_cursor;
28951 /* Make the window's buffer temporarily current for
28952 overlays_at and compute_char_face. */
28953 obuf = current_buffer;
28954 current_buffer = b;
28955 obegv = BEGV;
28956 ozv = ZV;
28957 BEGV = BEG;
28958 ZV = Z;
28960 /* Is this char mouse-active or does it have help-echo? */
28961 position = make_number (pos);
28963 if (BUFFERP (object))
28965 /* Put all the overlays we want in a vector in overlay_vec. */
28966 GET_OVERLAYS_AT (pos, overlay_vec, noverlays, NULL, 0);
28967 /* Sort overlays into increasing priority order. */
28968 noverlays = sort_overlays (overlay_vec, noverlays, w);
28970 else
28971 noverlays = 0;
28973 if (NILP (Vmouse_highlight))
28975 clear_mouse_face (hlinfo);
28976 goto check_help_echo;
28979 same_region = coords_in_mouse_face_p (w, hpos, vpos);
28981 if (same_region)
28982 cursor = No_Cursor;
28984 /* Check mouse-face highlighting. */
28985 if (! same_region
28986 /* If there exists an overlay with mouse-face overlapping
28987 the one we are currently highlighting, we have to
28988 check if we enter the overlapping overlay, and then
28989 highlight only that. */
28990 || (OVERLAYP (hlinfo->mouse_face_overlay)
28991 && mouse_face_overlay_overlaps (hlinfo->mouse_face_overlay)))
28993 /* Find the highest priority overlay with a mouse-face. */
28994 Lisp_Object overlay = Qnil;
28995 for (i = noverlays - 1; i >= 0 && NILP (overlay); --i)
28997 mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
28998 if (!NILP (mouse_face))
28999 overlay = overlay_vec[i];
29002 /* If we're highlighting the same overlay as before, there's
29003 no need to do that again. */
29004 if (!NILP (overlay) && EQ (overlay, hlinfo->mouse_face_overlay))
29005 goto check_help_echo;
29006 hlinfo->mouse_face_overlay = overlay;
29008 /* Clear the display of the old active region, if any. */
29009 if (clear_mouse_face (hlinfo))
29010 cursor = No_Cursor;
29012 /* If no overlay applies, get a text property. */
29013 if (NILP (overlay))
29014 mouse_face = Fget_text_property (position, Qmouse_face, object);
29016 /* Next, compute the bounds of the mouse highlighting and
29017 display it. */
29018 if (!NILP (mouse_face) && STRINGP (object))
29020 /* The mouse-highlighting comes from a display string
29021 with a mouse-face. */
29022 Lisp_Object s, e;
29023 ptrdiff_t ignore;
29025 s = Fprevious_single_property_change
29026 (make_number (pos + 1), Qmouse_face, object, Qnil);
29027 e = Fnext_single_property_change
29028 (position, Qmouse_face, object, Qnil);
29029 if (NILP (s))
29030 s = make_number (0);
29031 if (NILP (e))
29032 e = make_number (SCHARS (object));
29033 mouse_face_from_string_pos (w, hlinfo, object,
29034 XINT (s), XINT (e));
29035 hlinfo->mouse_face_past_end = 0;
29036 hlinfo->mouse_face_window = window;
29037 hlinfo->mouse_face_face_id
29038 = face_at_string_position (w, object, pos, 0, &ignore,
29039 glyph->face_id, 1);
29040 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
29041 cursor = No_Cursor;
29043 else
29045 /* The mouse-highlighting, if any, comes from an overlay
29046 or text property in the buffer. */
29047 Lisp_Object buffer IF_LINT (= Qnil);
29048 Lisp_Object disp_string IF_LINT (= Qnil);
29050 if (STRINGP (object))
29052 /* If we are on a display string with no mouse-face,
29053 check if the text under it has one. */
29054 struct glyph_row *r = MATRIX_ROW (w->current_matrix, vpos);
29055 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
29056 pos = string_buffer_position (object, start);
29057 if (pos > 0)
29059 mouse_face = get_char_property_and_overlay
29060 (make_number (pos), Qmouse_face, w->contents, &overlay);
29061 buffer = w->contents;
29062 disp_string = object;
29065 else
29067 buffer = object;
29068 disp_string = Qnil;
29071 if (!NILP (mouse_face))
29073 Lisp_Object before, after;
29074 Lisp_Object before_string, after_string;
29075 /* To correctly find the limits of mouse highlight
29076 in a bidi-reordered buffer, we must not use the
29077 optimization of limiting the search in
29078 previous-single-property-change and
29079 next-single-property-change, because
29080 rows_from_pos_range needs the real start and end
29081 positions to DTRT in this case. That's because
29082 the first row visible in a window does not
29083 necessarily display the character whose position
29084 is the smallest. */
29085 Lisp_Object lim1
29086 = NILP (BVAR (XBUFFER (buffer), bidi_display_reordering))
29087 ? Fmarker_position (w->start)
29088 : Qnil;
29089 Lisp_Object lim2
29090 = NILP (BVAR (XBUFFER (buffer), bidi_display_reordering))
29091 ? make_number (BUF_Z (XBUFFER (buffer))
29092 - w->window_end_pos)
29093 : Qnil;
29095 if (NILP (overlay))
29097 /* Handle the text property case. */
29098 before = Fprevious_single_property_change
29099 (make_number (pos + 1), Qmouse_face, buffer, lim1);
29100 after = Fnext_single_property_change
29101 (make_number (pos), Qmouse_face, buffer, lim2);
29102 before_string = after_string = Qnil;
29104 else
29106 /* Handle the overlay case. */
29107 before = Foverlay_start (overlay);
29108 after = Foverlay_end (overlay);
29109 before_string = Foverlay_get (overlay, Qbefore_string);
29110 after_string = Foverlay_get (overlay, Qafter_string);
29112 if (!STRINGP (before_string)) before_string = Qnil;
29113 if (!STRINGP (after_string)) after_string = Qnil;
29116 mouse_face_from_buffer_pos (window, hlinfo, pos,
29117 NILP (before)
29119 : XFASTINT (before),
29120 NILP (after)
29121 ? BUF_Z (XBUFFER (buffer))
29122 : XFASTINT (after),
29123 before_string, after_string,
29124 disp_string);
29125 cursor = No_Cursor;
29130 check_help_echo:
29132 /* Look for a `help-echo' property. */
29133 if (NILP (help_echo_string)) {
29134 Lisp_Object help, overlay;
29136 /* Check overlays first. */
29137 help = overlay = Qnil;
29138 for (i = noverlays - 1; i >= 0 && NILP (help); --i)
29140 overlay = overlay_vec[i];
29141 help = Foverlay_get (overlay, Qhelp_echo);
29144 if (!NILP (help))
29146 help_echo_string = help;
29147 help_echo_window = window;
29148 help_echo_object = overlay;
29149 help_echo_pos = pos;
29151 else
29153 Lisp_Object obj = glyph->object;
29154 ptrdiff_t charpos = glyph->charpos;
29156 /* Try text properties. */
29157 if (STRINGP (obj)
29158 && charpos >= 0
29159 && charpos < SCHARS (obj))
29161 help = Fget_text_property (make_number (charpos),
29162 Qhelp_echo, obj);
29163 if (NILP (help))
29165 /* If the string itself doesn't specify a help-echo,
29166 see if the buffer text ``under'' it does. */
29167 struct glyph_row *r
29168 = MATRIX_ROW (w->current_matrix, vpos);
29169 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
29170 ptrdiff_t p = string_buffer_position (obj, start);
29171 if (p > 0)
29173 help = Fget_char_property (make_number (p),
29174 Qhelp_echo, w->contents);
29175 if (!NILP (help))
29177 charpos = p;
29178 obj = w->contents;
29183 else if (BUFFERP (obj)
29184 && charpos >= BEGV
29185 && charpos < ZV)
29186 help = Fget_text_property (make_number (charpos), Qhelp_echo,
29187 obj);
29189 if (!NILP (help))
29191 help_echo_string = help;
29192 help_echo_window = window;
29193 help_echo_object = obj;
29194 help_echo_pos = charpos;
29199 #ifdef HAVE_WINDOW_SYSTEM
29200 /* Look for a `pointer' property. */
29201 if (FRAME_WINDOW_P (f) && NILP (pointer))
29203 /* Check overlays first. */
29204 for (i = noverlays - 1; i >= 0 && NILP (pointer); --i)
29205 pointer = Foverlay_get (overlay_vec[i], Qpointer);
29207 if (NILP (pointer))
29209 Lisp_Object obj = glyph->object;
29210 ptrdiff_t charpos = glyph->charpos;
29212 /* Try text properties. */
29213 if (STRINGP (obj)
29214 && charpos >= 0
29215 && charpos < SCHARS (obj))
29217 pointer = Fget_text_property (make_number (charpos),
29218 Qpointer, obj);
29219 if (NILP (pointer))
29221 /* If the string itself doesn't specify a pointer,
29222 see if the buffer text ``under'' it does. */
29223 struct glyph_row *r
29224 = MATRIX_ROW (w->current_matrix, vpos);
29225 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
29226 ptrdiff_t p = string_buffer_position (obj, start);
29227 if (p > 0)
29228 pointer = Fget_char_property (make_number (p),
29229 Qpointer, w->contents);
29232 else if (BUFFERP (obj)
29233 && charpos >= BEGV
29234 && charpos < ZV)
29235 pointer = Fget_text_property (make_number (charpos),
29236 Qpointer, obj);
29239 #endif /* HAVE_WINDOW_SYSTEM */
29241 BEGV = obegv;
29242 ZV = ozv;
29243 current_buffer = obuf;
29246 set_cursor:
29248 #ifdef HAVE_WINDOW_SYSTEM
29249 if (FRAME_WINDOW_P (f))
29250 define_frame_cursor1 (f, cursor, pointer);
29251 #else
29252 /* This is here to prevent a compiler error, about "label at end of
29253 compound statement". */
29254 return;
29255 #endif
29259 /* EXPORT for RIF:
29260 Clear any mouse-face on window W. This function is part of the
29261 redisplay interface, and is called from try_window_id and similar
29262 functions to ensure the mouse-highlight is off. */
29264 void
29265 x_clear_window_mouse_face (struct window *w)
29267 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (XFRAME (w->frame));
29268 Lisp_Object window;
29270 block_input ();
29271 XSETWINDOW (window, w);
29272 if (EQ (window, hlinfo->mouse_face_window))
29273 clear_mouse_face (hlinfo);
29274 unblock_input ();
29278 /* EXPORT:
29279 Just discard the mouse face information for frame F, if any.
29280 This is used when the size of F is changed. */
29282 void
29283 cancel_mouse_face (struct frame *f)
29285 Lisp_Object window;
29286 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
29288 window = hlinfo->mouse_face_window;
29289 if (! NILP (window) && XFRAME (XWINDOW (window)->frame) == f)
29290 reset_mouse_highlight (hlinfo);
29295 /***********************************************************************
29296 Exposure Events
29297 ***********************************************************************/
29299 #ifdef HAVE_WINDOW_SYSTEM
29301 /* Redraw the part of glyph row area AREA of glyph row ROW on window W
29302 which intersects rectangle R. R is in window-relative coordinates. */
29304 static void
29305 expose_area (struct window *w, struct glyph_row *row, XRectangle *r,
29306 enum glyph_row_area area)
29308 struct glyph *first = row->glyphs[area];
29309 struct glyph *end = row->glyphs[area] + row->used[area];
29310 struct glyph *last;
29311 int first_x, start_x, x;
29313 if (area == TEXT_AREA && row->fill_line_p)
29314 /* If row extends face to end of line write the whole line. */
29315 draw_glyphs (w, 0, row, area,
29316 0, row->used[area],
29317 DRAW_NORMAL_TEXT, 0);
29318 else
29320 /* Set START_X to the window-relative start position for drawing glyphs of
29321 AREA. The first glyph of the text area can be partially visible.
29322 The first glyphs of other areas cannot. */
29323 start_x = window_box_left_offset (w, area);
29324 x = start_x;
29325 if (area == TEXT_AREA)
29326 x += row->x;
29328 /* Find the first glyph that must be redrawn. */
29329 while (first < end
29330 && x + first->pixel_width < r->x)
29332 x += first->pixel_width;
29333 ++first;
29336 /* Find the last one. */
29337 last = first;
29338 first_x = x;
29339 while (last < end
29340 && x < r->x + r->width)
29342 x += last->pixel_width;
29343 ++last;
29346 /* Repaint. */
29347 if (last > first)
29348 draw_glyphs (w, first_x - start_x, row, area,
29349 first - row->glyphs[area], last - row->glyphs[area],
29350 DRAW_NORMAL_TEXT, 0);
29355 /* Redraw the parts of the glyph row ROW on window W intersecting
29356 rectangle R. R is in window-relative coordinates. Value is
29357 non-zero if mouse-face was overwritten. */
29359 static int
29360 expose_line (struct window *w, struct glyph_row *row, XRectangle *r)
29362 eassert (row->enabled_p);
29364 if (row->mode_line_p || w->pseudo_window_p)
29365 draw_glyphs (w, 0, row, TEXT_AREA,
29366 0, row->used[TEXT_AREA],
29367 DRAW_NORMAL_TEXT, 0);
29368 else
29370 if (row->used[LEFT_MARGIN_AREA])
29371 expose_area (w, row, r, LEFT_MARGIN_AREA);
29372 if (row->used[TEXT_AREA])
29373 expose_area (w, row, r, TEXT_AREA);
29374 if (row->used[RIGHT_MARGIN_AREA])
29375 expose_area (w, row, r, RIGHT_MARGIN_AREA);
29376 draw_row_fringe_bitmaps (w, row);
29379 return row->mouse_face_p;
29383 /* Redraw those parts of glyphs rows during expose event handling that
29384 overlap other rows. Redrawing of an exposed line writes over parts
29385 of lines overlapping that exposed line; this function fixes that.
29387 W is the window being exposed. FIRST_OVERLAPPING_ROW is the first
29388 row in W's current matrix that is exposed and overlaps other rows.
29389 LAST_OVERLAPPING_ROW is the last such row. */
29391 static void
29392 expose_overlaps (struct window *w,
29393 struct glyph_row *first_overlapping_row,
29394 struct glyph_row *last_overlapping_row,
29395 XRectangle *r)
29397 struct glyph_row *row;
29399 for (row = first_overlapping_row; row <= last_overlapping_row; ++row)
29400 if (row->overlapping_p)
29402 eassert (row->enabled_p && !row->mode_line_p);
29404 row->clip = r;
29405 if (row->used[LEFT_MARGIN_AREA])
29406 x_fix_overlapping_area (w, row, LEFT_MARGIN_AREA, OVERLAPS_BOTH);
29408 if (row->used[TEXT_AREA])
29409 x_fix_overlapping_area (w, row, TEXT_AREA, OVERLAPS_BOTH);
29411 if (row->used[RIGHT_MARGIN_AREA])
29412 x_fix_overlapping_area (w, row, RIGHT_MARGIN_AREA, OVERLAPS_BOTH);
29413 row->clip = NULL;
29418 /* Return non-zero if W's cursor intersects rectangle R. */
29420 static int
29421 phys_cursor_in_rect_p (struct window *w, XRectangle *r)
29423 XRectangle cr, result;
29424 struct glyph *cursor_glyph;
29425 struct glyph_row *row;
29427 if (w->phys_cursor.vpos >= 0
29428 && w->phys_cursor.vpos < w->current_matrix->nrows
29429 && (row = MATRIX_ROW (w->current_matrix, w->phys_cursor.vpos),
29430 row->enabled_p)
29431 && row->cursor_in_fringe_p)
29433 /* Cursor is in the fringe. */
29434 cr.x = window_box_right_offset (w,
29435 (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
29436 ? RIGHT_MARGIN_AREA
29437 : TEXT_AREA));
29438 cr.y = row->y;
29439 cr.width = WINDOW_RIGHT_FRINGE_WIDTH (w);
29440 cr.height = row->height;
29441 return x_intersect_rectangles (&cr, r, &result);
29444 cursor_glyph = get_phys_cursor_glyph (w);
29445 if (cursor_glyph)
29447 /* r is relative to W's box, but w->phys_cursor.x is relative
29448 to left edge of W's TEXT area. Adjust it. */
29449 cr.x = window_box_left_offset (w, TEXT_AREA) + w->phys_cursor.x;
29450 cr.y = w->phys_cursor.y;
29451 cr.width = cursor_glyph->pixel_width;
29452 cr.height = w->phys_cursor_height;
29453 /* ++KFS: W32 version used W32-specific IntersectRect here, but
29454 I assume the effect is the same -- and this is portable. */
29455 return x_intersect_rectangles (&cr, r, &result);
29457 /* If we don't understand the format, pretend we're not in the hot-spot. */
29458 return 0;
29462 /* EXPORT:
29463 Draw a vertical window border to the right of window W if W doesn't
29464 have vertical scroll bars. */
29466 void
29467 x_draw_vertical_border (struct window *w)
29469 struct frame *f = XFRAME (WINDOW_FRAME (w));
29471 /* We could do better, if we knew what type of scroll-bar the adjacent
29472 windows (on either side) have... But we don't :-(
29473 However, I think this works ok. ++KFS 2003-04-25 */
29475 /* Redraw borders between horizontally adjacent windows. Don't
29476 do it for frames with vertical scroll bars because either the
29477 right scroll bar of a window, or the left scroll bar of its
29478 neighbor will suffice as a border. */
29479 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f) || FRAME_RIGHT_DIVIDER_WIDTH (f))
29480 return;
29482 /* Note: It is necessary to redraw both the left and the right
29483 borders, for when only this single window W is being
29484 redisplayed. */
29485 if (!WINDOW_RIGHTMOST_P (w)
29486 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w))
29488 int x0, x1, y0, y1;
29490 window_box_edges (w, &x0, &y0, &x1, &y1);
29491 y1 -= 1;
29493 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
29494 x1 -= 1;
29496 FRAME_RIF (f)->draw_vertical_window_border (w, x1, y0, y1);
29499 if (!WINDOW_LEFTMOST_P (w)
29500 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w))
29502 int x0, x1, y0, y1;
29504 window_box_edges (w, &x0, &y0, &x1, &y1);
29505 y1 -= 1;
29507 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
29508 x0 -= 1;
29510 FRAME_RIF (f)->draw_vertical_window_border (w, x0, y0, y1);
29515 /* Draw window dividers for window W. */
29517 void
29518 x_draw_right_divider (struct window *w)
29520 struct frame *f = WINDOW_XFRAME (w);
29522 if (w->mini || w->pseudo_window_p)
29523 return;
29524 else if (WINDOW_RIGHT_DIVIDER_WIDTH (w))
29526 int x0 = WINDOW_RIGHT_EDGE_X (w) - WINDOW_RIGHT_DIVIDER_WIDTH (w);
29527 int x1 = WINDOW_RIGHT_EDGE_X (w);
29528 int y0 = WINDOW_TOP_EDGE_Y (w);
29529 /* The bottom divider prevails. */
29530 int y1 = WINDOW_BOTTOM_EDGE_Y (w) - WINDOW_BOTTOM_DIVIDER_WIDTH (w);
29532 FRAME_RIF (f)->draw_window_divider (w, x0, x1, y0, y1);
29536 static void
29537 x_draw_bottom_divider (struct window *w)
29539 struct frame *f = XFRAME (WINDOW_FRAME (w));
29541 if (w->mini || w->pseudo_window_p)
29542 return;
29543 else if (WINDOW_BOTTOM_DIVIDER_WIDTH (w))
29545 int x0 = WINDOW_LEFT_EDGE_X (w);
29546 int x1 = WINDOW_RIGHT_EDGE_X (w);
29547 int y0 = WINDOW_BOTTOM_EDGE_Y (w) - WINDOW_BOTTOM_DIVIDER_WIDTH (w);
29548 int y1 = WINDOW_BOTTOM_EDGE_Y (w);
29550 FRAME_RIF (f)->draw_window_divider (w, x0, x1, y0, y1);
29554 /* Redraw the part of window W intersection rectangle FR. Pixel
29555 coordinates in FR are frame-relative. Call this function with
29556 input blocked. Value is non-zero if the exposure overwrites
29557 mouse-face. */
29559 static int
29560 expose_window (struct window *w, XRectangle *fr)
29562 struct frame *f = XFRAME (w->frame);
29563 XRectangle wr, r;
29564 int mouse_face_overwritten_p = 0;
29566 /* If window is not yet fully initialized, do nothing. This can
29567 happen when toolkit scroll bars are used and a window is split.
29568 Reconfiguring the scroll bar will generate an expose for a newly
29569 created window. */
29570 if (w->current_matrix == NULL)
29571 return 0;
29573 /* When we're currently updating the window, display and current
29574 matrix usually don't agree. Arrange for a thorough display
29575 later. */
29576 if (w->must_be_updated_p)
29578 SET_FRAME_GARBAGED (f);
29579 return 0;
29582 /* Frame-relative pixel rectangle of W. */
29583 wr.x = WINDOW_LEFT_EDGE_X (w);
29584 wr.y = WINDOW_TOP_EDGE_Y (w);
29585 wr.width = WINDOW_PIXEL_WIDTH (w);
29586 wr.height = WINDOW_PIXEL_HEIGHT (w);
29588 if (x_intersect_rectangles (fr, &wr, &r))
29590 int yb = window_text_bottom_y (w);
29591 struct glyph_row *row;
29592 int cursor_cleared_p, phys_cursor_on_p;
29593 struct glyph_row *first_overlapping_row, *last_overlapping_row;
29595 TRACE ((stderr, "expose_window (%d, %d, %d, %d)\n",
29596 r.x, r.y, r.width, r.height));
29598 /* Convert to window coordinates. */
29599 r.x -= WINDOW_LEFT_EDGE_X (w);
29600 r.y -= WINDOW_TOP_EDGE_Y (w);
29602 /* Turn off the cursor. */
29603 if (!w->pseudo_window_p
29604 && phys_cursor_in_rect_p (w, &r))
29606 x_clear_cursor (w);
29607 cursor_cleared_p = 1;
29609 else
29610 cursor_cleared_p = 0;
29612 /* If the row containing the cursor extends face to end of line,
29613 then expose_area might overwrite the cursor outside the
29614 rectangle and thus notice_overwritten_cursor might clear
29615 w->phys_cursor_on_p. We remember the original value and
29616 check later if it is changed. */
29617 phys_cursor_on_p = w->phys_cursor_on_p;
29619 /* Update lines intersecting rectangle R. */
29620 first_overlapping_row = last_overlapping_row = NULL;
29621 for (row = w->current_matrix->rows;
29622 row->enabled_p;
29623 ++row)
29625 int y0 = row->y;
29626 int y1 = MATRIX_ROW_BOTTOM_Y (row);
29628 if ((y0 >= r.y && y0 < r.y + r.height)
29629 || (y1 > r.y && y1 < r.y + r.height)
29630 || (r.y >= y0 && r.y < y1)
29631 || (r.y + r.height > y0 && r.y + r.height < y1))
29633 /* A header line may be overlapping, but there is no need
29634 to fix overlapping areas for them. KFS 2005-02-12 */
29635 if (row->overlapping_p && !row->mode_line_p)
29637 if (first_overlapping_row == NULL)
29638 first_overlapping_row = row;
29639 last_overlapping_row = row;
29642 row->clip = fr;
29643 if (expose_line (w, row, &r))
29644 mouse_face_overwritten_p = 1;
29645 row->clip = NULL;
29647 else if (row->overlapping_p)
29649 /* We must redraw a row overlapping the exposed area. */
29650 if (y0 < r.y
29651 ? y0 + row->phys_height > r.y
29652 : y0 + row->ascent - row->phys_ascent < r.y +r.height)
29654 if (first_overlapping_row == NULL)
29655 first_overlapping_row = row;
29656 last_overlapping_row = row;
29660 if (y1 >= yb)
29661 break;
29664 /* Display the mode line if there is one. */
29665 if (WINDOW_WANTS_MODELINE_P (w)
29666 && (row = MATRIX_MODE_LINE_ROW (w->current_matrix),
29667 row->enabled_p)
29668 && row->y < r.y + r.height)
29670 if (expose_line (w, row, &r))
29671 mouse_face_overwritten_p = 1;
29674 if (!w->pseudo_window_p)
29676 /* Fix the display of overlapping rows. */
29677 if (first_overlapping_row)
29678 expose_overlaps (w, first_overlapping_row, last_overlapping_row,
29679 fr);
29681 /* Draw border between windows. */
29682 if (WINDOW_RIGHT_DIVIDER_WIDTH (w))
29683 x_draw_right_divider (w);
29684 else
29685 x_draw_vertical_border (w);
29687 if (WINDOW_BOTTOM_DIVIDER_WIDTH (w))
29688 x_draw_bottom_divider (w);
29690 /* Turn the cursor on again. */
29691 if (cursor_cleared_p
29692 || (phys_cursor_on_p && !w->phys_cursor_on_p))
29693 update_window_cursor (w, 1);
29697 return mouse_face_overwritten_p;
29702 /* Redraw (parts) of all windows in the window tree rooted at W that
29703 intersect R. R contains frame pixel coordinates. Value is
29704 non-zero if the exposure overwrites mouse-face. */
29706 static int
29707 expose_window_tree (struct window *w, XRectangle *r)
29709 struct frame *f = XFRAME (w->frame);
29710 int mouse_face_overwritten_p = 0;
29712 while (w && !FRAME_GARBAGED_P (f))
29714 if (WINDOWP (w->contents))
29715 mouse_face_overwritten_p
29716 |= expose_window_tree (XWINDOW (w->contents), r);
29717 else
29718 mouse_face_overwritten_p |= expose_window (w, r);
29720 w = NILP (w->next) ? NULL : XWINDOW (w->next);
29723 return mouse_face_overwritten_p;
29727 /* EXPORT:
29728 Redisplay an exposed area of frame F. X and Y are the upper-left
29729 corner of the exposed rectangle. W and H are width and height of
29730 the exposed area. All are pixel values. W or H zero means redraw
29731 the entire frame. */
29733 void
29734 expose_frame (struct frame *f, int x, int y, int w, int h)
29736 XRectangle r;
29737 int mouse_face_overwritten_p = 0;
29739 TRACE ((stderr, "expose_frame "));
29741 /* No need to redraw if frame will be redrawn soon. */
29742 if (FRAME_GARBAGED_P (f))
29744 TRACE ((stderr, " garbaged\n"));
29745 return;
29748 /* If basic faces haven't been realized yet, there is no point in
29749 trying to redraw anything. This can happen when we get an expose
29750 event while Emacs is starting, e.g. by moving another window. */
29751 if (FRAME_FACE_CACHE (f) == NULL
29752 || FRAME_FACE_CACHE (f)->used < BASIC_FACE_ID_SENTINEL)
29754 TRACE ((stderr, " no faces\n"));
29755 return;
29758 if (w == 0 || h == 0)
29760 r.x = r.y = 0;
29761 r.width = FRAME_COLUMN_WIDTH (f) * FRAME_COLS (f);
29762 r.height = FRAME_LINE_HEIGHT (f) * FRAME_LINES (f);
29764 else
29766 r.x = x;
29767 r.y = y;
29768 r.width = w;
29769 r.height = h;
29772 TRACE ((stderr, "(%d, %d, %d, %d)\n", r.x, r.y, r.width, r.height));
29773 mouse_face_overwritten_p = expose_window_tree (XWINDOW (f->root_window), &r);
29775 #if ! defined (USE_GTK) && ! defined (HAVE_NS)
29776 if (WINDOWP (f->tool_bar_window))
29777 mouse_face_overwritten_p
29778 |= expose_window (XWINDOW (f->tool_bar_window), &r);
29779 #endif
29781 #ifdef HAVE_X_WINDOWS
29782 #ifndef MSDOS
29783 #if ! defined (USE_X_TOOLKIT) && ! defined (USE_GTK)
29784 if (WINDOWP (f->menu_bar_window))
29785 mouse_face_overwritten_p
29786 |= expose_window (XWINDOW (f->menu_bar_window), &r);
29787 #endif /* not USE_X_TOOLKIT and not USE_GTK */
29788 #endif
29789 #endif
29791 /* Some window managers support a focus-follows-mouse style with
29792 delayed raising of frames. Imagine a partially obscured frame,
29793 and moving the mouse into partially obscured mouse-face on that
29794 frame. The visible part of the mouse-face will be highlighted,
29795 then the WM raises the obscured frame. With at least one WM, KDE
29796 2.1, Emacs is not getting any event for the raising of the frame
29797 (even tried with SubstructureRedirectMask), only Expose events.
29798 These expose events will draw text normally, i.e. not
29799 highlighted. Which means we must redo the highlight here.
29800 Subsume it under ``we love X''. --gerd 2001-08-15 */
29801 /* Included in Windows version because Windows most likely does not
29802 do the right thing if any third party tool offers
29803 focus-follows-mouse with delayed raise. --jason 2001-10-12 */
29804 if (mouse_face_overwritten_p && !FRAME_GARBAGED_P (f))
29806 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
29807 if (f == hlinfo->mouse_face_mouse_frame)
29809 int mouse_x = hlinfo->mouse_face_mouse_x;
29810 int mouse_y = hlinfo->mouse_face_mouse_y;
29811 clear_mouse_face (hlinfo);
29812 note_mouse_highlight (f, mouse_x, mouse_y);
29818 /* EXPORT:
29819 Determine the intersection of two rectangles R1 and R2. Return
29820 the intersection in *RESULT. Value is non-zero if RESULT is not
29821 empty. */
29824 x_intersect_rectangles (XRectangle *r1, XRectangle *r2, XRectangle *result)
29826 XRectangle *left, *right;
29827 XRectangle *upper, *lower;
29828 int intersection_p = 0;
29830 /* Rearrange so that R1 is the left-most rectangle. */
29831 if (r1->x < r2->x)
29832 left = r1, right = r2;
29833 else
29834 left = r2, right = r1;
29836 /* X0 of the intersection is right.x0, if this is inside R1,
29837 otherwise there is no intersection. */
29838 if (right->x <= left->x + left->width)
29840 result->x = right->x;
29842 /* The right end of the intersection is the minimum of
29843 the right ends of left and right. */
29844 result->width = (min (left->x + left->width, right->x + right->width)
29845 - result->x);
29847 /* Same game for Y. */
29848 if (r1->y < r2->y)
29849 upper = r1, lower = r2;
29850 else
29851 upper = r2, lower = r1;
29853 /* The upper end of the intersection is lower.y0, if this is inside
29854 of upper. Otherwise, there is no intersection. */
29855 if (lower->y <= upper->y + upper->height)
29857 result->y = lower->y;
29859 /* The lower end of the intersection is the minimum of the lower
29860 ends of upper and lower. */
29861 result->height = (min (lower->y + lower->height,
29862 upper->y + upper->height)
29863 - result->y);
29864 intersection_p = 1;
29868 return intersection_p;
29871 #endif /* HAVE_WINDOW_SYSTEM */
29874 /***********************************************************************
29875 Initialization
29876 ***********************************************************************/
29878 void
29879 syms_of_xdisp (void)
29881 Vwith_echo_area_save_vector = Qnil;
29882 staticpro (&Vwith_echo_area_save_vector);
29884 Vmessage_stack = Qnil;
29885 staticpro (&Vmessage_stack);
29887 DEFSYM (Qinhibit_redisplay, "inhibit-redisplay");
29888 DEFSYM (Qredisplay_internal, "redisplay_internal (C function)");
29890 message_dolog_marker1 = Fmake_marker ();
29891 staticpro (&message_dolog_marker1);
29892 message_dolog_marker2 = Fmake_marker ();
29893 staticpro (&message_dolog_marker2);
29894 message_dolog_marker3 = Fmake_marker ();
29895 staticpro (&message_dolog_marker3);
29897 #ifdef GLYPH_DEBUG
29898 defsubr (&Sdump_frame_glyph_matrix);
29899 defsubr (&Sdump_glyph_matrix);
29900 defsubr (&Sdump_glyph_row);
29901 defsubr (&Sdump_tool_bar_row);
29902 defsubr (&Strace_redisplay);
29903 defsubr (&Strace_to_stderr);
29904 #endif
29905 #ifdef HAVE_WINDOW_SYSTEM
29906 defsubr (&Stool_bar_height);
29907 defsubr (&Slookup_image_map);
29908 #endif
29909 defsubr (&Sline_pixel_height);
29910 defsubr (&Sformat_mode_line);
29911 defsubr (&Sinvisible_p);
29912 defsubr (&Scurrent_bidi_paragraph_direction);
29913 defsubr (&Swindow_text_pixel_size);
29914 defsubr (&Smove_point_visually);
29916 DEFSYM (Qmenu_bar_update_hook, "menu-bar-update-hook");
29917 DEFSYM (Qoverriding_terminal_local_map, "overriding-terminal-local-map");
29918 DEFSYM (Qoverriding_local_map, "overriding-local-map");
29919 DEFSYM (Qwindow_scroll_functions, "window-scroll-functions");
29920 DEFSYM (Qwindow_text_change_functions, "window-text-change-functions");
29921 DEFSYM (Qredisplay_end_trigger_functions, "redisplay-end-trigger-functions");
29922 DEFSYM (Qinhibit_point_motion_hooks, "inhibit-point-motion-hooks");
29923 DEFSYM (Qeval, "eval");
29924 DEFSYM (QCdata, ":data");
29925 DEFSYM (Qdisplay, "display");
29926 DEFSYM (Qspace_width, "space-width");
29927 DEFSYM (Qraise, "raise");
29928 DEFSYM (Qslice, "slice");
29929 DEFSYM (Qspace, "space");
29930 DEFSYM (Qmargin, "margin");
29931 DEFSYM (Qpointer, "pointer");
29932 DEFSYM (Qleft_margin, "left-margin");
29933 DEFSYM (Qright_margin, "right-margin");
29934 DEFSYM (Qcenter, "center");
29935 DEFSYM (Qline_height, "line-height");
29936 DEFSYM (QCalign_to, ":align-to");
29937 DEFSYM (QCrelative_width, ":relative-width");
29938 DEFSYM (QCrelative_height, ":relative-height");
29939 DEFSYM (QCeval, ":eval");
29940 DEFSYM (QCpropertize, ":propertize");
29941 DEFSYM (QCfile, ":file");
29942 DEFSYM (Qfontified, "fontified");
29943 DEFSYM (Qfontification_functions, "fontification-functions");
29944 DEFSYM (Qtrailing_whitespace, "trailing-whitespace");
29945 DEFSYM (Qescape_glyph, "escape-glyph");
29946 DEFSYM (Qnobreak_space, "nobreak-space");
29947 DEFSYM (Qimage, "image");
29948 DEFSYM (Qtext, "text");
29949 DEFSYM (Qboth, "both");
29950 DEFSYM (Qboth_horiz, "both-horiz");
29951 DEFSYM (Qtext_image_horiz, "text-image-horiz");
29952 DEFSYM (QCmap, ":map");
29953 DEFSYM (QCpointer, ":pointer");
29954 DEFSYM (Qrect, "rect");
29955 DEFSYM (Qcircle, "circle");
29956 DEFSYM (Qpoly, "poly");
29957 DEFSYM (Qmessage_truncate_lines, "message-truncate-lines");
29958 DEFSYM (Qgrow_only, "grow-only");
29959 DEFSYM (Qinhibit_menubar_update, "inhibit-menubar-update");
29960 DEFSYM (Qinhibit_eval_during_redisplay, "inhibit-eval-during-redisplay");
29961 DEFSYM (Qposition, "position");
29962 DEFSYM (Qbuffer_position, "buffer-position");
29963 DEFSYM (Qobject, "object");
29964 DEFSYM (Qbar, "bar");
29965 DEFSYM (Qhbar, "hbar");
29966 DEFSYM (Qbox, "box");
29967 DEFSYM (Qhollow, "hollow");
29968 DEFSYM (Qhand, "hand");
29969 DEFSYM (Qarrow, "arrow");
29970 DEFSYM (Qinhibit_free_realized_faces, "inhibit-free-realized-faces");
29972 list_of_error = list1 (list2 (intern_c_string ("error"),
29973 intern_c_string ("void-variable")));
29974 staticpro (&list_of_error);
29976 DEFSYM (Qlast_arrow_position, "last-arrow-position");
29977 DEFSYM (Qlast_arrow_string, "last-arrow-string");
29978 DEFSYM (Qoverlay_arrow_string, "overlay-arrow-string");
29979 DEFSYM (Qoverlay_arrow_bitmap, "overlay-arrow-bitmap");
29981 echo_buffer[0] = echo_buffer[1] = Qnil;
29982 staticpro (&echo_buffer[0]);
29983 staticpro (&echo_buffer[1]);
29985 echo_area_buffer[0] = echo_area_buffer[1] = Qnil;
29986 staticpro (&echo_area_buffer[0]);
29987 staticpro (&echo_area_buffer[1]);
29989 Vmessages_buffer_name = build_pure_c_string ("*Messages*");
29990 staticpro (&Vmessages_buffer_name);
29992 mode_line_proptrans_alist = Qnil;
29993 staticpro (&mode_line_proptrans_alist);
29994 mode_line_string_list = Qnil;
29995 staticpro (&mode_line_string_list);
29996 mode_line_string_face = Qnil;
29997 staticpro (&mode_line_string_face);
29998 mode_line_string_face_prop = Qnil;
29999 staticpro (&mode_line_string_face_prop);
30000 Vmode_line_unwind_vector = Qnil;
30001 staticpro (&Vmode_line_unwind_vector);
30003 DEFSYM (Qmode_line_default_help_echo, "mode-line-default-help-echo");
30005 help_echo_string = Qnil;
30006 staticpro (&help_echo_string);
30007 help_echo_object = Qnil;
30008 staticpro (&help_echo_object);
30009 help_echo_window = Qnil;
30010 staticpro (&help_echo_window);
30011 previous_help_echo_string = Qnil;
30012 staticpro (&previous_help_echo_string);
30013 help_echo_pos = -1;
30015 DEFSYM (Qright_to_left, "right-to-left");
30016 DEFSYM (Qleft_to_right, "left-to-right");
30018 #ifdef HAVE_WINDOW_SYSTEM
30019 DEFVAR_BOOL ("x-stretch-cursor", x_stretch_cursor_p,
30020 doc: /* Non-nil means draw block cursor as wide as the glyph under it.
30021 For example, if a block cursor is over a tab, it will be drawn as
30022 wide as that tab on the display. */);
30023 x_stretch_cursor_p = 0;
30024 #endif
30026 DEFVAR_LISP ("show-trailing-whitespace", Vshow_trailing_whitespace,
30027 doc: /* Non-nil means highlight trailing whitespace.
30028 The face used for trailing whitespace is `trailing-whitespace'. */);
30029 Vshow_trailing_whitespace = Qnil;
30031 DEFVAR_LISP ("nobreak-char-display", Vnobreak_char_display,
30032 doc: /* Control highlighting of non-ASCII space and hyphen chars.
30033 If the value is t, Emacs highlights non-ASCII chars which have the
30034 same appearance as an ASCII space or hyphen, using the `nobreak-space'
30035 or `escape-glyph' face respectively.
30037 U+00A0 (no-break space), U+00AD (soft hyphen), U+2010 (hyphen), and
30038 U+2011 (non-breaking hyphen) are affected.
30040 Any other non-nil value means to display these characters as a escape
30041 glyph followed by an ordinary space or hyphen.
30043 A value of nil means no special handling of these characters. */);
30044 Vnobreak_char_display = Qt;
30046 DEFVAR_LISP ("void-text-area-pointer", Vvoid_text_area_pointer,
30047 doc: /* The pointer shape to show in void text areas.
30048 A value of nil means to show the text pointer. Other options are `arrow',
30049 `text', `hand', `vdrag', `hdrag', `modeline', and `hourglass'. */);
30050 Vvoid_text_area_pointer = Qarrow;
30052 DEFVAR_LISP ("inhibit-redisplay", Vinhibit_redisplay,
30053 doc: /* Non-nil means don't actually do any redisplay.
30054 This is used for internal purposes. */);
30055 Vinhibit_redisplay = Qnil;
30057 DEFVAR_LISP ("global-mode-string", Vglobal_mode_string,
30058 doc: /* String (or mode line construct) included (normally) in `mode-line-format'. */);
30059 Vglobal_mode_string = Qnil;
30061 DEFVAR_LISP ("overlay-arrow-position", Voverlay_arrow_position,
30062 doc: /* Marker for where to display an arrow on top of the buffer text.
30063 This must be the beginning of a line in order to work.
30064 See also `overlay-arrow-string'. */);
30065 Voverlay_arrow_position = Qnil;
30067 DEFVAR_LISP ("overlay-arrow-string", Voverlay_arrow_string,
30068 doc: /* String to display as an arrow in non-window frames.
30069 See also `overlay-arrow-position'. */);
30070 Voverlay_arrow_string = build_pure_c_string ("=>");
30072 DEFVAR_LISP ("overlay-arrow-variable-list", Voverlay_arrow_variable_list,
30073 doc: /* List of variables (symbols) which hold markers for overlay arrows.
30074 The symbols on this list are examined during redisplay to determine
30075 where to display overlay arrows. */);
30076 Voverlay_arrow_variable_list
30077 = list1 (intern_c_string ("overlay-arrow-position"));
30079 DEFVAR_INT ("scroll-step", emacs_scroll_step,
30080 doc: /* The number of lines to try scrolling a window by when point moves out.
30081 If that fails to bring point back on frame, point is centered instead.
30082 If this is zero, point is always centered after it moves off frame.
30083 If you want scrolling to always be a line at a time, you should set
30084 `scroll-conservatively' to a large value rather than set this to 1. */);
30086 DEFVAR_INT ("scroll-conservatively", scroll_conservatively,
30087 doc: /* Scroll up to this many lines, to bring point back on screen.
30088 If point moves off-screen, redisplay will scroll by up to
30089 `scroll-conservatively' lines in order to bring point just barely
30090 onto the screen again. If that cannot be done, then redisplay
30091 recenters point as usual.
30093 If the value is greater than 100, redisplay will never recenter point,
30094 but will always scroll just enough text to bring point into view, even
30095 if you move far away.
30097 A value of zero means always recenter point if it moves off screen. */);
30098 scroll_conservatively = 0;
30100 DEFVAR_INT ("scroll-margin", scroll_margin,
30101 doc: /* Number of lines of margin at the top and bottom of a window.
30102 Recenter the window whenever point gets within this many lines
30103 of the top or bottom of the window. */);
30104 scroll_margin = 0;
30106 DEFVAR_LISP ("display-pixels-per-inch", Vdisplay_pixels_per_inch,
30107 doc: /* Pixels per inch value for non-window system displays.
30108 Value is a number or a cons (WIDTH-DPI . HEIGHT-DPI). */);
30109 Vdisplay_pixels_per_inch = make_float (72.0);
30111 #ifdef GLYPH_DEBUG
30112 DEFVAR_INT ("debug-end-pos", debug_end_pos, doc: /* Don't ask. */);
30113 #endif
30115 DEFVAR_LISP ("truncate-partial-width-windows",
30116 Vtruncate_partial_width_windows,
30117 doc: /* Non-nil means truncate lines in windows narrower than the frame.
30118 For an integer value, truncate lines in each window narrower than the
30119 full frame width, provided the window width is less than that integer;
30120 otherwise, respect the value of `truncate-lines'.
30122 For any other non-nil value, truncate lines in all windows that do
30123 not span the full frame width.
30125 A value of nil means to respect the value of `truncate-lines'.
30127 If `word-wrap' is enabled, you might want to reduce this. */);
30128 Vtruncate_partial_width_windows = make_number (50);
30130 DEFVAR_LISP ("line-number-display-limit", Vline_number_display_limit,
30131 doc: /* Maximum buffer size for which line number should be displayed.
30132 If the buffer is bigger than this, the line number does not appear
30133 in the mode line. A value of nil means no limit. */);
30134 Vline_number_display_limit = Qnil;
30136 DEFVAR_INT ("line-number-display-limit-width",
30137 line_number_display_limit_width,
30138 doc: /* Maximum line width (in characters) for line number display.
30139 If the average length of the lines near point is bigger than this, then the
30140 line number may be omitted from the mode line. */);
30141 line_number_display_limit_width = 200;
30143 DEFVAR_BOOL ("highlight-nonselected-windows", highlight_nonselected_windows,
30144 doc: /* Non-nil means highlight region even in nonselected windows. */);
30145 highlight_nonselected_windows = 0;
30147 DEFVAR_BOOL ("multiple-frames", multiple_frames,
30148 doc: /* Non-nil if more than one frame is visible on this display.
30149 Minibuffer-only frames don't count, but iconified frames do.
30150 This variable is not guaranteed to be accurate except while processing
30151 `frame-title-format' and `icon-title-format'. */);
30153 DEFVAR_LISP ("frame-title-format", Vframe_title_format,
30154 doc: /* Template for displaying the title bar of visible frames.
30155 \(Assuming the window manager supports this feature.)
30157 This variable has the same structure as `mode-line-format', except that
30158 the %c and %l constructs are ignored. It is used only on frames for
30159 which no explicit name has been set \(see `modify-frame-parameters'). */);
30161 DEFVAR_LISP ("icon-title-format", Vicon_title_format,
30162 doc: /* Template for displaying the title bar of an iconified frame.
30163 \(Assuming the window manager supports this feature.)
30164 This variable has the same structure as `mode-line-format' (which see),
30165 and is used only on frames for which no explicit name has been set
30166 \(see `modify-frame-parameters'). */);
30167 Vicon_title_format
30168 = Vframe_title_format
30169 = listn (CONSTYPE_PURE, 3,
30170 intern_c_string ("multiple-frames"),
30171 build_pure_c_string ("%b"),
30172 listn (CONSTYPE_PURE, 4,
30173 empty_unibyte_string,
30174 intern_c_string ("invocation-name"),
30175 build_pure_c_string ("@"),
30176 intern_c_string ("system-name")));
30178 DEFVAR_LISP ("message-log-max", Vmessage_log_max,
30179 doc: /* Maximum number of lines to keep in the message log buffer.
30180 If nil, disable message logging. If t, log messages but don't truncate
30181 the buffer when it becomes large. */);
30182 Vmessage_log_max = make_number (1000);
30184 DEFVAR_LISP ("window-size-change-functions", Vwindow_size_change_functions,
30185 doc: /* Functions called before redisplay, if window sizes have changed.
30186 The value should be a list of functions that take one argument.
30187 Just before redisplay, for each frame, if any of its windows have changed
30188 size since the last redisplay, or have been split or deleted,
30189 all the functions in the list are called, with the frame as argument. */);
30190 Vwindow_size_change_functions = Qnil;
30192 DEFVAR_LISP ("window-scroll-functions", Vwindow_scroll_functions,
30193 doc: /* List of functions to call before redisplaying a window with scrolling.
30194 Each function is called with two arguments, the window and its new
30195 display-start position. Note that these functions are also called by
30196 `set-window-buffer'. Also note that the value of `window-end' is not
30197 valid when these functions are called.
30199 Warning: Do not use this feature to alter the way the window
30200 is scrolled. It is not designed for that, and such use probably won't
30201 work. */);
30202 Vwindow_scroll_functions = Qnil;
30204 DEFVAR_LISP ("window-text-change-functions",
30205 Vwindow_text_change_functions,
30206 doc: /* Functions to call in redisplay when text in the window might change. */);
30207 Vwindow_text_change_functions = Qnil;
30209 DEFVAR_LISP ("redisplay-end-trigger-functions", Vredisplay_end_trigger_functions,
30210 doc: /* Functions called when redisplay of a window reaches the end trigger.
30211 Each function is called with two arguments, the window and the end trigger value.
30212 See `set-window-redisplay-end-trigger'. */);
30213 Vredisplay_end_trigger_functions = Qnil;
30215 DEFVAR_LISP ("mouse-autoselect-window", Vmouse_autoselect_window,
30216 doc: /* Non-nil means autoselect window with mouse pointer.
30217 If nil, do not autoselect windows.
30218 A positive number means delay autoselection by that many seconds: a
30219 window is autoselected only after the mouse has remained in that
30220 window for the duration of the delay.
30221 A negative number has a similar effect, but causes windows to be
30222 autoselected only after the mouse has stopped moving. \(Because of
30223 the way Emacs compares mouse events, you will occasionally wait twice
30224 that time before the window gets selected.\)
30225 Any other value means to autoselect window instantaneously when the
30226 mouse pointer enters it.
30228 Autoselection selects the minibuffer only if it is active, and never
30229 unselects the minibuffer if it is active.
30231 When customizing this variable make sure that the actual value of
30232 `focus-follows-mouse' matches the behavior of your window manager. */);
30233 Vmouse_autoselect_window = Qnil;
30235 DEFVAR_LISP ("auto-resize-tool-bars", Vauto_resize_tool_bars,
30236 doc: /* Non-nil means automatically resize tool-bars.
30237 This dynamically changes the tool-bar's height to the minimum height
30238 that is needed to make all tool-bar items visible.
30239 If value is `grow-only', the tool-bar's height is only increased
30240 automatically; to decrease the tool-bar height, use \\[recenter]. */);
30241 Vauto_resize_tool_bars = Qt;
30243 DEFVAR_BOOL ("auto-raise-tool-bar-buttons", auto_raise_tool_bar_buttons_p,
30244 doc: /* Non-nil means raise tool-bar buttons when the mouse moves over them. */);
30245 auto_raise_tool_bar_buttons_p = 1;
30247 DEFVAR_BOOL ("make-cursor-line-fully-visible", make_cursor_line_fully_visible_p,
30248 doc: /* Non-nil means to scroll (recenter) cursor line if it is not fully visible. */);
30249 make_cursor_line_fully_visible_p = 1;
30251 DEFVAR_LISP ("tool-bar-border", Vtool_bar_border,
30252 doc: /* Border below tool-bar in pixels.
30253 If an integer, use it as the height of the border.
30254 If it is one of `internal-border-width' or `border-width', use the
30255 value of the corresponding frame parameter.
30256 Otherwise, no border is added below the tool-bar. */);
30257 Vtool_bar_border = Qinternal_border_width;
30259 DEFVAR_LISP ("tool-bar-button-margin", Vtool_bar_button_margin,
30260 doc: /* Margin around tool-bar buttons in pixels.
30261 If an integer, use that for both horizontal and vertical margins.
30262 Otherwise, value should be a pair of integers `(HORZ . VERT)' with
30263 HORZ specifying the horizontal margin, and VERT specifying the
30264 vertical margin. */);
30265 Vtool_bar_button_margin = make_number (DEFAULT_TOOL_BAR_BUTTON_MARGIN);
30267 DEFVAR_INT ("tool-bar-button-relief", tool_bar_button_relief,
30268 doc: /* Relief thickness of tool-bar buttons. */);
30269 tool_bar_button_relief = DEFAULT_TOOL_BAR_BUTTON_RELIEF;
30271 DEFVAR_LISP ("tool-bar-style", Vtool_bar_style,
30272 doc: /* Tool bar style to use.
30273 It can be one of
30274 image - show images only
30275 text - show text only
30276 both - show both, text below image
30277 both-horiz - show text to the right of the image
30278 text-image-horiz - show text to the left of the image
30279 any other - use system default or image if no system default.
30281 This variable only affects the GTK+ toolkit version of Emacs. */);
30282 Vtool_bar_style = Qnil;
30284 DEFVAR_INT ("tool-bar-max-label-size", tool_bar_max_label_size,
30285 doc: /* Maximum number of characters a label can have to be shown.
30286 The tool bar style must also show labels for this to have any effect, see
30287 `tool-bar-style'. */);
30288 tool_bar_max_label_size = DEFAULT_TOOL_BAR_LABEL_SIZE;
30290 DEFVAR_LISP ("fontification-functions", Vfontification_functions,
30291 doc: /* List of functions to call to fontify regions of text.
30292 Each function is called with one argument POS. Functions must
30293 fontify a region starting at POS in the current buffer, and give
30294 fontified regions the property `fontified'. */);
30295 Vfontification_functions = Qnil;
30296 Fmake_variable_buffer_local (Qfontification_functions);
30298 DEFVAR_BOOL ("unibyte-display-via-language-environment",
30299 unibyte_display_via_language_environment,
30300 doc: /* Non-nil means display unibyte text according to language environment.
30301 Specifically, this means that raw bytes in the range 160-255 decimal
30302 are displayed by converting them to the equivalent multibyte characters
30303 according to the current language environment. As a result, they are
30304 displayed according to the current fontset.
30306 Note that this variable affects only how these bytes are displayed,
30307 but does not change the fact they are interpreted as raw bytes. */);
30308 unibyte_display_via_language_environment = 0;
30310 DEFVAR_LISP ("max-mini-window-height", Vmax_mini_window_height,
30311 doc: /* Maximum height for resizing mini-windows (the minibuffer and the echo area).
30312 If a float, it specifies a fraction of the mini-window frame's height.
30313 If an integer, it specifies a number of lines. */);
30314 Vmax_mini_window_height = make_float (0.25);
30316 DEFVAR_LISP ("resize-mini-windows", Vresize_mini_windows,
30317 doc: /* How to resize mini-windows (the minibuffer and the echo area).
30318 A value of nil means don't automatically resize mini-windows.
30319 A value of t means resize them to fit the text displayed in them.
30320 A value of `grow-only', the default, means let mini-windows grow only;
30321 they return to their normal size when the minibuffer is closed, or the
30322 echo area becomes empty. */);
30323 Vresize_mini_windows = Qgrow_only;
30325 DEFVAR_LISP ("blink-cursor-alist", Vblink_cursor_alist,
30326 doc: /* Alist specifying how to blink the cursor off.
30327 Each element has the form (ON-STATE . OFF-STATE). Whenever the
30328 `cursor-type' frame-parameter or variable equals ON-STATE,
30329 comparing using `equal', Emacs uses OFF-STATE to specify
30330 how to blink it off. ON-STATE and OFF-STATE are values for
30331 the `cursor-type' frame parameter.
30333 If a frame's ON-STATE has no entry in this list,
30334 the frame's other specifications determine how to blink the cursor off. */);
30335 Vblink_cursor_alist = Qnil;
30337 DEFVAR_BOOL ("auto-hscroll-mode", automatic_hscrolling_p,
30338 doc: /* Allow or disallow automatic horizontal scrolling of windows.
30339 If non-nil, windows are automatically scrolled horizontally to make
30340 point visible. */);
30341 automatic_hscrolling_p = 1;
30342 DEFSYM (Qauto_hscroll_mode, "auto-hscroll-mode");
30344 DEFVAR_INT ("hscroll-margin", hscroll_margin,
30345 doc: /* How many columns away from the window edge point is allowed to get
30346 before automatic hscrolling will horizontally scroll the window. */);
30347 hscroll_margin = 5;
30349 DEFVAR_LISP ("hscroll-step", Vhscroll_step,
30350 doc: /* How many columns to scroll the window when point gets too close to the edge.
30351 When point is less than `hscroll-margin' columns from the window
30352 edge, automatic hscrolling will scroll the window by the amount of columns
30353 determined by this variable. If its value is a positive integer, scroll that
30354 many columns. If it's a positive floating-point number, it specifies the
30355 fraction of the window's width to scroll. If it's nil or zero, point will be
30356 centered horizontally after the scroll. Any other value, including negative
30357 numbers, are treated as if the value were zero.
30359 Automatic hscrolling always moves point outside the scroll margin, so if
30360 point was more than scroll step columns inside the margin, the window will
30361 scroll more than the value given by the scroll step.
30363 Note that the lower bound for automatic hscrolling specified by `scroll-left'
30364 and `scroll-right' overrides this variable's effect. */);
30365 Vhscroll_step = make_number (0);
30367 DEFVAR_BOOL ("message-truncate-lines", message_truncate_lines,
30368 doc: /* If non-nil, messages are truncated instead of resizing the echo area.
30369 Bind this around calls to `message' to let it take effect. */);
30370 message_truncate_lines = 0;
30372 DEFVAR_LISP ("menu-bar-update-hook", Vmenu_bar_update_hook,
30373 doc: /* Normal hook run to update the menu bar definitions.
30374 Redisplay runs this hook before it redisplays the menu bar.
30375 This is used to update menus such as Buffers, whose contents depend on
30376 various data. */);
30377 Vmenu_bar_update_hook = Qnil;
30379 DEFVAR_LISP ("menu-updating-frame", Vmenu_updating_frame,
30380 doc: /* Frame for which we are updating a menu.
30381 The enable predicate for a menu binding should check this variable. */);
30382 Vmenu_updating_frame = Qnil;
30384 DEFVAR_BOOL ("inhibit-menubar-update", inhibit_menubar_update,
30385 doc: /* Non-nil means don't update menu bars. Internal use only. */);
30386 inhibit_menubar_update = 0;
30388 DEFVAR_LISP ("wrap-prefix", Vwrap_prefix,
30389 doc: /* Prefix prepended to all continuation lines at display time.
30390 The value may be a string, an image, or a stretch-glyph; it is
30391 interpreted in the same way as the value of a `display' text property.
30393 This variable is overridden by any `wrap-prefix' text or overlay
30394 property.
30396 To add a prefix to non-continuation lines, use `line-prefix'. */);
30397 Vwrap_prefix = Qnil;
30398 DEFSYM (Qwrap_prefix, "wrap-prefix");
30399 Fmake_variable_buffer_local (Qwrap_prefix);
30401 DEFVAR_LISP ("line-prefix", Vline_prefix,
30402 doc: /* Prefix prepended to all non-continuation lines at display time.
30403 The value may be a string, an image, or a stretch-glyph; it is
30404 interpreted in the same way as the value of a `display' text property.
30406 This variable is overridden by any `line-prefix' text or overlay
30407 property.
30409 To add a prefix to continuation lines, use `wrap-prefix'. */);
30410 Vline_prefix = Qnil;
30411 DEFSYM (Qline_prefix, "line-prefix");
30412 Fmake_variable_buffer_local (Qline_prefix);
30414 DEFVAR_BOOL ("inhibit-eval-during-redisplay", inhibit_eval_during_redisplay,
30415 doc: /* Non-nil means don't eval Lisp during redisplay. */);
30416 inhibit_eval_during_redisplay = 0;
30418 DEFVAR_BOOL ("inhibit-free-realized-faces", inhibit_free_realized_faces,
30419 doc: /* Non-nil means don't free realized faces. Internal use only. */);
30420 inhibit_free_realized_faces = 0;
30422 #ifdef GLYPH_DEBUG
30423 DEFVAR_BOOL ("inhibit-try-window-id", inhibit_try_window_id,
30424 doc: /* Inhibit try_window_id display optimization. */);
30425 inhibit_try_window_id = 0;
30427 DEFVAR_BOOL ("inhibit-try-window-reusing", inhibit_try_window_reusing,
30428 doc: /* Inhibit try_window_reusing display optimization. */);
30429 inhibit_try_window_reusing = 0;
30431 DEFVAR_BOOL ("inhibit-try-cursor-movement", inhibit_try_cursor_movement,
30432 doc: /* Inhibit try_cursor_movement display optimization. */);
30433 inhibit_try_cursor_movement = 0;
30434 #endif /* GLYPH_DEBUG */
30436 DEFVAR_INT ("overline-margin", overline_margin,
30437 doc: /* Space between overline and text, in pixels.
30438 The default value is 2: the height of the overline (1 pixel) plus 1 pixel
30439 margin to the character height. */);
30440 overline_margin = 2;
30442 DEFVAR_INT ("underline-minimum-offset",
30443 underline_minimum_offset,
30444 doc: /* Minimum distance between baseline and underline.
30445 This can improve legibility of underlined text at small font sizes,
30446 particularly when using variable `x-use-underline-position-properties'
30447 with fonts that specify an UNDERLINE_POSITION relatively close to the
30448 baseline. The default value is 1. */);
30449 underline_minimum_offset = 1;
30451 DEFVAR_BOOL ("display-hourglass", display_hourglass_p,
30452 doc: /* Non-nil means show an hourglass pointer, when Emacs is busy.
30453 This feature only works when on a window system that can change
30454 cursor shapes. */);
30455 display_hourglass_p = 1;
30457 DEFVAR_LISP ("hourglass-delay", Vhourglass_delay,
30458 doc: /* Seconds to wait before displaying an hourglass pointer when Emacs is busy. */);
30459 Vhourglass_delay = make_number (DEFAULT_HOURGLASS_DELAY);
30461 #ifdef HAVE_WINDOW_SYSTEM
30462 hourglass_atimer = NULL;
30463 hourglass_shown_p = 0;
30464 #endif /* HAVE_WINDOW_SYSTEM */
30466 DEFSYM (Qglyphless_char, "glyphless-char");
30467 DEFSYM (Qhex_code, "hex-code");
30468 DEFSYM (Qempty_box, "empty-box");
30469 DEFSYM (Qthin_space, "thin-space");
30470 DEFSYM (Qzero_width, "zero-width");
30472 DEFVAR_LISP ("pre-redisplay-function", Vpre_redisplay_function,
30473 doc: /* Function run just before redisplay.
30474 It is called with one argument, which is the set of windows that are to
30475 be redisplayed. This set can be nil (meaning, only the selected window),
30476 or t (meaning all windows). */);
30477 Vpre_redisplay_function = intern ("ignore");
30479 DEFSYM (Qglyphless_char_display, "glyphless-char-display");
30480 Fput (Qglyphless_char_display, Qchar_table_extra_slots, make_number (1));
30482 DEFVAR_LISP ("glyphless-char-display", Vglyphless_char_display,
30483 doc: /* Char-table defining glyphless characters.
30484 Each element, if non-nil, should be one of the following:
30485 an ASCII acronym string: display this string in a box
30486 `hex-code': display the hexadecimal code of a character in a box
30487 `empty-box': display as an empty box
30488 `thin-space': display as 1-pixel width space
30489 `zero-width': don't display
30490 An element may also be a cons cell (GRAPHICAL . TEXT), which specifies the
30491 display method for graphical terminals and text terminals respectively.
30492 GRAPHICAL and TEXT should each have one of the values listed above.
30494 The char-table has one extra slot to control the display of a character for
30495 which no font is found. This slot only takes effect on graphical terminals.
30496 Its value should be an ASCII acronym string, `hex-code', `empty-box', or
30497 `thin-space'. The default is `empty-box'.
30499 If a character has a non-nil entry in an active display table, the
30500 display table takes effect; in this case, Emacs does not consult
30501 `glyphless-char-display' at all. */);
30502 Vglyphless_char_display = Fmake_char_table (Qglyphless_char_display, Qnil);
30503 Fset_char_table_extra_slot (Vglyphless_char_display, make_number (0),
30504 Qempty_box);
30506 DEFVAR_LISP ("debug-on-message", Vdebug_on_message,
30507 doc: /* If non-nil, debug if a message matching this regexp is displayed. */);
30508 Vdebug_on_message = Qnil;
30510 DEFVAR_LISP ("redisplay--all-windows-cause", Vredisplay__all_windows_cause,
30511 doc: /* */);
30512 Vredisplay__all_windows_cause
30513 = Fmake_vector (make_number (100), make_number (0));
30515 DEFVAR_LISP ("redisplay--mode-lines-cause", Vredisplay__mode_lines_cause,
30516 doc: /* */);
30517 Vredisplay__mode_lines_cause
30518 = Fmake_vector (make_number (100), make_number (0));
30522 /* Initialize this module when Emacs starts. */
30524 void
30525 init_xdisp (void)
30527 CHARPOS (this_line_start_pos) = 0;
30529 if (!noninteractive)
30531 struct window *m = XWINDOW (minibuf_window);
30532 Lisp_Object frame = m->frame;
30533 struct frame *f = XFRAME (frame);
30534 Lisp_Object root = FRAME_ROOT_WINDOW (f);
30535 struct window *r = XWINDOW (root);
30536 int i;
30538 echo_area_window = minibuf_window;
30540 r->top_line = FRAME_TOP_MARGIN (f);
30541 r->pixel_top = r->top_line * FRAME_LINE_HEIGHT (f);
30542 r->total_cols = FRAME_COLS (f);
30543 r->pixel_width = r->total_cols * FRAME_COLUMN_WIDTH (f);
30544 r->total_lines = FRAME_LINES (f) - 1 - FRAME_TOP_MARGIN (f);
30545 r->pixel_height = r->total_lines * FRAME_LINE_HEIGHT (f);
30547 m->top_line = FRAME_LINES (f) - 1;
30548 m->pixel_top = m->top_line * FRAME_LINE_HEIGHT (f);
30549 m->total_cols = FRAME_COLS (f);
30550 m->pixel_width = m->total_cols * FRAME_COLUMN_WIDTH (f);
30551 m->total_lines = 1;
30552 m->pixel_height = m->total_lines * FRAME_LINE_HEIGHT (f);
30554 scratch_glyph_row.glyphs[TEXT_AREA] = scratch_glyphs;
30555 scratch_glyph_row.glyphs[TEXT_AREA + 1]
30556 = scratch_glyphs + MAX_SCRATCH_GLYPHS;
30558 /* The default ellipsis glyphs `...'. */
30559 for (i = 0; i < 3; ++i)
30560 default_invis_vector[i] = make_number ('.');
30564 /* Allocate the buffer for frame titles.
30565 Also used for `format-mode-line'. */
30566 int size = 100;
30567 mode_line_noprop_buf = xmalloc (size);
30568 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
30569 mode_line_noprop_ptr = mode_line_noprop_buf;
30570 mode_line_target = MODE_LINE_DISPLAY;
30573 help_echo_showing_p = 0;
30576 #ifdef HAVE_WINDOW_SYSTEM
30578 /* Platform-independent portion of hourglass implementation. */
30580 /* Cancel a currently active hourglass timer, and start a new one. */
30581 void
30582 start_hourglass (void)
30584 struct timespec delay;
30586 cancel_hourglass ();
30588 if (INTEGERP (Vhourglass_delay)
30589 && XINT (Vhourglass_delay) > 0)
30590 delay = make_timespec (min (XINT (Vhourglass_delay),
30591 TYPE_MAXIMUM (time_t)),
30593 else if (FLOATP (Vhourglass_delay)
30594 && XFLOAT_DATA (Vhourglass_delay) > 0)
30595 delay = dtotimespec (XFLOAT_DATA (Vhourglass_delay));
30596 else
30597 delay = make_timespec (DEFAULT_HOURGLASS_DELAY, 0);
30599 #ifdef HAVE_NTGUI
30601 extern void w32_note_current_window (void);
30602 w32_note_current_window ();
30604 #endif /* HAVE_NTGUI */
30606 hourglass_atimer = start_atimer (ATIMER_RELATIVE, delay,
30607 show_hourglass, NULL);
30611 /* Cancel the hourglass cursor timer if active, hide a busy cursor if
30612 shown. */
30613 void
30614 cancel_hourglass (void)
30616 if (hourglass_atimer)
30618 cancel_atimer (hourglass_atimer);
30619 hourglass_atimer = NULL;
30622 if (hourglass_shown_p)
30623 hide_hourglass ();
30626 #endif /* HAVE_WINDOW_SYSTEM */